xref: /original-bsd/sys/vax/bi/kdb.c (revision 208c3823)
1 /*
2  * Copyright (c) 1988 Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Chris Torek.
7  *
8  * Redistribution and use in source and binary forms are permitted
9  * provided that the above copyright notice and this paragraph are
10  * duplicated in all such forms and that any documentation,
11  * advertising materials, and other materials related to such
12  * distribution and use acknowledge that the software was developed
13  * by the University of California, Berkeley.  The name of the
14  * University may not be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19  *
20  *	@(#)kdb.c	7.8 (Berkeley) 02/17/90
21  */
22 
23 /*
24  * KDB50/MSCP device driver
25  */
26 
27 /*
28  * TODO
29  *	rethink BI software interface
30  *	write bad block forwarding code
31  */
32 
33 #include "kra.h"		/* XXX */
34 
35 #define	DRIVENAMES	"kra"	/* XXX */
36 
37 #if NKDB > 0
38 
39 /*
40  * CONFIGURATION OPTIONS.  The next three defines are tunable -- tune away!
41  *
42  * NRSPL2 and NCMDL2 control the number of response and command
43  * packets respectively.  They may be any value from 0 to 7, though
44  * setting them higher than 5 is unlikely to be of any value.
45  * If you get warnings about your command ring being too small,
46  * try increasing the values by one.
47  *
48  * MAXUNIT controls the maximum slave number (and hence number of drives
49  * per controller) we are prepared to handle.
50  */
51 #define	NRSPL2	5		/* log2 number of response packets */
52 #define NCMDL2	5		/* log2 number of command packets */
53 #define	MAXUNIT	8		/* maximum allowed unit number */
54 
55 #include "param.h"
56 #include "systm.h"
57 #include "malloc.h"
58 #include "map.h"
59 #include "buf.h"
60 #include "conf.h"
61 #include "user.h"
62 #include "proc.h"
63 #include "vm.h"
64 #include "dkstat.h"
65 #include "cmap.h"
66 #include "syslog.h"
67 #include "kernel.h"
68 
69 #define	NRSP	(1 << NRSPL2)
70 #define	NCMD	(1 << NCMDL2)
71 
72 #include "../vax/pte.h"
73 #include "../vax/cpu.h"
74 #include "../vax/mscp.h"
75 #include "../vax/mscpvar.h"
76 #include "../vax/mtpr.h"
77 
78 #include "bireg.h"
79 #include "kdbreg.h"
80 
81 #include "../vaxuba/ubavar.h"
82 
83 /*
84  * Conversions from kernel virtual to physical and page table addresses.
85  * PHYS works only for kernel text and primary (compile time) data addresses.
86  */
87 #define	PHYS(cast, addr) \
88 	((cast) ((int)(addr) & 0x7fffffff))
89 
90 /*
91  * KDB variables, per controller.
92  */
93 struct kdbinfo {
94 	/* software info, per KDB */
95 	struct	kdb_regs *ki_kdb;	/* KDB registers */
96 	struct	kdb_regs *ki_physkdb;	/* phys address of KDB registers */
97 	short	ki_state;		/* KDB50 state; see below */
98 	short	ki_flags;		/* flags; see below */
99 	int	ki_micro;		/* microcode revision */
100 	short	ki_vec;			/* scb vector offset */
101 	short	ki_wticks;		/* watchdog timer ticks */
102 
103 	/*
104 	 * KDB PTEs must be contiguous.  Some I/O is done on addresses
105 	 * for which this is true (PTEs in Sysmap and Usrptmap), but
106 	 * other transfers may have PTEs that are scattered in physical
107 	 * space.  Ki_map maps a physically contiguous PTE space used
108 	 * for these transfers.
109 	 */
110 #define KI_MAPSIZ	(NCMD + 2)
111 	struct	map *ki_map;		/* resource map */
112 #define KI_PTES		256
113 	struct	pte ki_pte[KI_PTES];	/* contiguous PTE space */
114 	long	ki_ptephys;		/* phys address of &ki_pte[0] */
115 
116 	struct	mscp_info ki_mi;	/* MSCP info (per mscpvar.h) */
117 	struct	buf ki_tab;		/* controller queue */
118 
119 	/* stuff read and written by hardware */
120 	struct	kdbca ki_ca;		/* communications area */
121 	struct	mscp ki_rsp[NRSP];	/* response packets */
122 	struct	mscp ki_cmd[NCMD];	/* command packets */
123 } kdbinfo[NKDB];
124 
125 #define	ki_ctlr	ki_mi.mi_ctlr
126 
127 /*
128  * Controller states
129  */
130 #define	ST_IDLE		0	/* uninitialised */
131 #define	ST_STEP1	1	/* in `STEP 1' */
132 #define	ST_STEP2	2	/* in `STEP 2' */
133 #define	ST_STEP3	3	/* in `STEP 3' */
134 #define	ST_SETCHAR	4	/* in `Set Controller Characteristics' */
135 #define	ST_RUN		5	/* up and running */
136 
137 /*
138  * Flags
139  */
140 #define	KDB_ALIVE	0x01	/* this KDB50 exists */
141 #define	KDB_GRIPED	0x04	/* griped about cmd ring too small */
142 #define	KDB_INSLAVE	0x08	/* inside kdbslave() */
143 #define	KDB_DOWAKE	0x10	/* wakeup when ctlr init done */
144 
145 struct kdbstats kdbstats;	/* statistics */
146 
147 /*
148  * Device to unit number and partition:
149  */
150 #define	UNITSHIFT	3
151 #define	UNITMASK	7
152 #define	kdbunit(dev)	(minor(dev) >> UNITSHIFT)
153 #define	kdbpart(dev)	(minor(dev) & UNITMASK)
154 
155 /* THIS SHOULD BE READ OFF THE PACK, PER DRIVE */
156 /* THESE SHOULD BE SHARED WITH uda.c (but not yet) */
157 struct size {
158 	daddr_t nblocks;
159 	daddr_t blkoff;
160 } kra81_sizes[8] = {
161 #ifdef MARYLAND
162 	67832,	0,		/* A=cyl    0 thru   94 + 2 sectors */
163 	67828,	67832,		/* B=cyl   95 thru  189 - 2 sectors */
164 	-1,	0,		/* C=cyl    0 thru 1247 */
165 	-1,	135660,		/* D=cyl  190 thru 1247 */
166 	449466,	49324,		/* E xxx */
167 	64260,	498790,		/* F xxx */
168 	328022,	563050,		/* G xxx */
169 	0,	0,
170 #else
171 	15884,	0,		/* a */
172 	33440,	15884,		/* b */
173 	-1,	0,		/* c */
174 	-1,	49324,		/* d */
175 	449466,	49324,		/* e */
176 	64260,	498790,		/* f */
177 	328022,	563050,		/* g */
178 	0,	0,
179 #endif
180 }, kra80_sizes[8] = {
181 	15884,	0,		/* A=blk 0 thru 15883 */
182 	33440,	15884,		/* B=blk 15884 thru 49323 */
183 	-1,	0,		/* C=blk 0 thru end */
184 	0,	0,
185 	0,	0,
186 	0,	0,
187 	82080,	49324,		/* G=blk 49324 thru 131403 */
188 	-1,	131404,		/* H=blk 131404 thru end */
189 }, kra60_sizes[8] = {
190 	15884,	0,		/* A=blk 0 thru 15883 */
191 	33440,	15884,		/* B=blk 15884 thru 49323 */
192 	-1,	0,		/* C=blk 0 thru end */
193 	-1,	49324,		/* D=blk 49324 thru end */
194 	0,	0,
195 	0,	0,
196 	82080,	49324,		/* G=blk 49324 thru 131403 */
197 	-1,	131404,		/* H=blk 131404 thru end */
198 };
199 /* END OF STUFF WHICH SHOULD BE READ IN PER DISK */
200 
201 /*
202  * Drive type index decoding table.  `ut_name' is null iff the
203  * type is not known.
204  */
205 struct	kdbtypes {
206 	char	*ut_name;	/* drive type name */
207 	struct	size *ut_sizes;	/* partition tables */
208 } kdbtypes[] = {
209 	NULL,		NULL,
210 	"ra80",		kra80_sizes,	/* 1 = ra80 */
211 	NULL,		NULL,
212 	NULL,		NULL,
213 	"ra60",		kra60_sizes,	/* 4 = ra60 */
214 	"ra81",		kra81_sizes,	/* 5 = ra81 */
215 };
216 
217 #define NTYPES 6
218 
219 /*
220  * Definition of the driver for autoconf and generic MSCP code.
221  * SOME OF THIS IS BOGUS (must fix config)
222  */
223 
224 #ifdef notdef		/* not when driver is for kra disks */
225 /*
226  * Some of these variables (per-drive stuff) are shared
227  * with the UDA50 code (why not, they are the same drives).
228  * N.B.: kdbdinfo must not be shared.
229  */
230 #define	kdbutab		udautab		/* shared */
231 #define	kdbslavereply	udaslavereply	/* shared */
232 #endif
233 
234 int	kdbprobe();		/* XXX */
235 int	kdbslave(), kdbattach();
236 
237 int	kdbdgram(), kdbctlrdone(), kdbunconf(), kdbiodone();
238 int	kdbonline(), kdbgotstatus(), kdbioerror();
239 
240 struct	uba_device *kdbdinfo[NKRA];	/* uba_device indeed! */
241 struct	buf kdbutab[NKRA];	/* per drive transfer queue */
242 
243 u_short kdbstd[] = { 0 };	/* XXX */
244 struct uba_driver kdbdriver =	/* XXX */
245  { kdbprobe, kdbslave, kdbattach, 0, kdbstd, DRIVENAMES, kdbdinfo, "kdb" };
246 
247 struct	mscp_driver kdbmscpdriver =
248  { MAXUNIT, NKRA, UNITSHIFT, kdbutab, (struct disklabel *)0, kdbdinfo,
249    kdbdgram, kdbctlrdone, kdbunconf, kdbiodone,
250    kdbonline, kdbgotstatus, NULL, kdbioerror, NULL,
251    "kdb", DRIVENAMES };
252 
253 /*
254  * Miscellaneous private variables.
255  */
256 char	kdbsr_bits[] = KDBSR_BITS;
257 
258 struct	uba_device *kdbip[NKDB][MAXUNIT];
259 				/* inverting pointers: ctlr & unit => `Unibus'
260 				   device pointer */
261 
262 daddr_t	ra_dsize[NKRA];		/* drive sizes, from on line end packets */
263 
264 struct	mscp kdbslavereply;	/* get unit status response packet, set
265 				   for kdbslave by kdbunconf, via kdbintr */
266 
267 int	kdbwstart, kdbwatch();	/* watchdog timer */
268 int	wakeup();
269 
270 /*
271  * If kdbprobe is called, return 0 to keep Unibus code from attempting
272  * to use this device.	XXX rethink
273  */
274 /* ARGSUSED */
275 kdbprobe(reg, ctlr)
276 	caddr_t reg;
277 	int ctlr;
278 {
279 
280 	return (0);
281 }
282 
283 /*
284  * Configure in a KDB50 controller.
285  */
286 kdbconfig(kdbnum, va, pa, vec)
287 	int kdbnum;
288 	struct biiregs *va, *pa;
289 	int vec;
290 {
291 	register struct kdbinfo *ki;
292 #define mi (&ki->ki_mi)
293 
294 #ifdef lint
295 	extern int (*kdbint0[])();
296 
297 	(*kdbint0[0])(0);	/* this is a config botch */
298 	kdbintr(0);
299 #endif
300 
301 	/*
302 	 * Set up local KDB status.
303 	 */
304 	ki = &kdbinfo[kdbnum];
305 	ki->ki_kdb = (struct kdb_regs *)va;
306 	ki->ki_physkdb = (struct kdb_regs *)pa;
307 	ki->ki_vec = vec;
308 	ki->ki_map =
309 	    (struct map *)malloc((u_long)(KI_MAPSIZ * sizeof(struct map)),
310 	    M_DEVBUF, M_NOWAIT);
311 	if (ki->ki_map == NULL) {
312 		printf("kdb%d: cannot get memory for ptes\n", kdbnum);
313 		return;
314 	}
315 	ki->ki_ptephys = PHYS(long, ki->ki_pte); /* kvtophys(ki->ki_pte) */
316 	ki->ki_flags = KDB_ALIVE;
317 
318 	/* THE FOLLOWING IS ONLY NEEDED TO CIRCUMVENT A BUG IN rminit */
319 	bzero((caddr_t)ki->ki_map, KI_MAPSIZ * sizeof(struct map));
320 
321 	rminit(ki->ki_map, (long)KI_PTES, (long)1, "kdb", KI_MAPSIZ);
322 
323 	/*
324 	 * Set up the generic MSCP structures.
325 	 */
326 	mi->mi_md = &kdbmscpdriver;
327 	mi->mi_ctlr = kdbnum;	/* also sets ki->ki_ctlr */
328 	mi->mi_tab = &ki->ki_tab;
329 	mi->mi_ip = kdbip[kdbnum];
330 	mi->mi_cmd.mri_size = NCMD;
331 	mi->mi_cmd.mri_desc = ki->ki_ca.ca_cmddsc;
332 	mi->mi_cmd.mri_ring = ki->ki_cmd;
333 	mi->mi_rsp.mri_size = NRSP;
334 	mi->mi_rsp.mri_desc = ki->ki_ca.ca_rspdsc;
335 	mi->mi_rsp.mri_ring = ki->ki_rsp;
336 	mi->mi_wtab.av_forw = mi->mi_wtab.av_back = &mi->mi_wtab;
337 #undef mi
338 }
339 
340 /*
341  * Find a slave.
342  * Note that by the time kdbslave is called, the interrupt vector
343  * for the KDB50 has been set up (so that kdbunconf() will be called).
344  */
345 kdbslave(ui)
346 	register struct uba_device *ui;
347 {
348 	register struct kdbinfo *ki;
349 	register struct mscp *mp;
350 	int next = 0, type, timeout, tries, i;
351 
352 #ifdef lint
353 	i = 0; i = i;
354 #endif
355 	/*
356 	 * Make sure the controller is fully initialised, by waiting
357 	 * for it if necessary.
358 	 */
359 	ki = &kdbinfo[ui->ui_ctlr];
360 	if (ki->ki_state == ST_RUN)
361 		goto findunit;
362 	tries = 0;
363 again:
364 	if (kdbinit(ki))
365 		return (0);
366 	timeout = todr() + 1000;		/* 10 seconds */
367 	while (todr() < timeout)
368 		if (ki->ki_state == ST_RUN)	/* made it */
369 			goto findunit;
370 	if (++tries < 2)
371 		goto again;
372 	printf("kdb%d: controller hung\n", ki->ki_ctlr);
373 	return (0);
374 
375 	/*
376 	 * The controller is all set; go find the unit.  Grab an
377 	 * MSCP packet and send out a Get Unit Status command, with
378 	 * the `next unit' modifier if we are looking for a generic
379 	 * unit.  We set the `in slave' flag so that kdbunconf()
380 	 * knows to copy the response to `kdbslavereply'.
381 	 */
382 findunit:
383 	kdbslavereply.mscp_opcode = 0;
384 	ki->ki_flags |= KDB_INSLAVE;
385 	if ((mp = mscp_getcp(&ki->ki_mi, MSCP_DONTWAIT)) == NULL)
386 		panic("kdbslave");		/* `cannot happen' */
387 	mp->mscp_opcode = M_OP_GETUNITST;
388 	if (ui->ui_slave == '?') {
389 		mp->mscp_unit = next;
390 		mp->mscp_modifier = M_GUM_NEXTUNIT;
391 	} else {
392 		mp->mscp_unit = ui->ui_slave;
393 		mp->mscp_modifier = 0;
394 	}
395 	*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
396 	i = ki->ki_kdb->kdb_ip;	/* initiate polling */
397 	mp = &kdbslavereply;
398 	timeout = todr() + 1000;
399 	while (todr() < timeout)
400 		if (mp->mscp_opcode)
401 			goto gotit;
402 	printf("kdb%d: no response to Get Unit Status request\n",
403 		ki->ki_ctlr);
404 	ki->ki_flags &= ~KDB_INSLAVE;
405 	return (0);
406 
407 gotit:
408 	ki->ki_flags &= ~KDB_INSLAVE;
409 
410 	/*
411 	 * Got a slave response.  If the unit is there, use it.
412 	 */
413 	switch (mp->mscp_status & M_ST_MASK) {
414 
415 	case M_ST_SUCCESS:	/* worked */
416 	case M_ST_AVAILABLE:	/* found another drive */
417 		break;		/* use it */
418 
419 	case M_ST_OFFLINE:
420 		/*
421 		 * Figure out why it is off line.  It may be because
422 		 * it is nonexistent, or because it is spun down, or
423 		 * for some other reason.
424 		 */
425 		switch (mp->mscp_status & ~M_ST_MASK) {
426 
427 		case M_OFFLINE_UNKNOWN:
428 			/*
429 			 * No such drive, and there are none with
430 			 * higher unit numbers either, if we are
431 			 * using M_GUM_NEXTUNIT.
432 			 */
433 			return (0);
434 
435 		case M_OFFLINE_UNMOUNTED:
436 			/*
437 			 * The drive is not spun up.  Use it anyway.
438 			 *
439 			 * N.B.: this seems to be a common occurrance
440 			 * after a power failure.  The first attempt
441 			 * to bring it on line seems to spin it up
442 			 * (and thus takes several minutes).  Perhaps
443 			 * we should note here that the on-line may
444 			 * take longer than usual.
445 			 */
446 			break;
447 
448 		default:
449 			/*
450 			 * In service, or something else equally unusable.
451 			 */
452 			printf("kdb%d: unit %d off line:", ki->ki_ctlr,
453 				mp->mscp_unit);
454 			mscp_printevent(mp);
455 			goto try_another;
456 		}
457 		break;
458 
459 	default:
460 		printf("kdb%d: unable to get unit status:", ki->ki_ctlr);
461 		mscp_printevent(mp);
462 		return (0);
463 	}
464 
465 	/*
466 	 * Does this ever happen?  What (if anything) does it mean?
467 	 */
468 	if (mp->mscp_unit < next) {
469 		printf("kdb%d: unit %d, next %d\n",
470 			ki->ki_ctlr, mp->mscp_unit, next);
471 		return (0);
472 	}
473 
474 	if (mp->mscp_unit >= MAXUNIT) {
475 		printf("kdb%d: cannot handle unit number %d (max is %d)\n",
476 			ki->ki_ctlr, mp->mscp_unit, MAXUNIT - 1);
477 		return (0);
478 	}
479 
480 	/*
481 	 * See if we already handle this drive.
482 	 * (Only likely if ui->ui_slave=='?'.)
483 	 */
484 	if (kdbip[ki->ki_ctlr][mp->mscp_unit] != NULL)
485 		goto try_another;
486 
487 	/*
488 	 * Make sure we know about this kind of drive.
489 	 * Others say we should treat unknowns as RA81s; I am
490 	 * not sure this is safe.
491 	 */
492 	type = mp->mscp_guse.guse_drivetype;
493 	if (type >= NTYPES || kdbtypes[type].ut_name == 0) {
494 		register long id = mp->mscp_guse.guse_mediaid;
495 
496 		printf("kdb%d: unit %d: media ID `", ki->ki_ctlr,
497 			mp->mscp_unit);
498 		printf("%c%c %c%c%c%d",
499 			MSCP_MID_CHAR(4, id), MSCP_MID_CHAR(3, id),
500 			MSCP_MID_CHAR(2, id), MSCP_MID_CHAR(1, id),
501 			MSCP_MID_CHAR(0, id), MSCP_MID_NUM(id));
502 		printf("' is of unknown type %d; ignored\n", type);
503 try_another:
504 		if (ui->ui_slave != '?')
505 			return (0);
506 		next = mp->mscp_unit + 1;
507 		goto findunit;
508 	}
509 
510 	/*
511 	 * Voila!
512 	 */
513 	ui->ui_type = type;
514 	ui->ui_flags = 0;	/* not on line, nor anything else */
515 	ui->ui_slave = mp->mscp_unit;
516 	return (1);
517 }
518 
519 /*
520  * Attach a found slave.  Make sure the watchdog timer is running.
521  * If this disk is being profiled, fill in the `wpms' value (used by
522  * what?).  Set up the inverting pointer, and attempt to bring the
523  * drive on line.
524  */
525 kdbattach(ui)
526 	register struct uba_device *ui;
527 {
528 
529 	if (kdbwstart == 0) {
530 		timeout(kdbwatch, (caddr_t)0, hz);
531 		kdbwstart++;
532 	}
533 	if (ui->ui_dk >= 0)
534 		dk_wpms[ui->ui_dk] = (60 * 31 * 256);	/* approx */
535 	kdbip[ui->ui_ctlr][ui->ui_slave] = ui;
536 	(void) kdb_bringonline(ui, 1);
537 	/* should we get its status too? */
538 }
539 
540 /*
541  * Initialise a KDB50.  Return true iff something goes wrong.
542  */
543 kdbinit(ki)
544 	register struct kdbinfo *ki;
545 {
546 	register struct kdb_regs *ka = ki->ki_kdb;
547 	int timo;
548 
549 	/*
550 	 * While we are thinking about it, reset the next command
551 	 * and response indicies.
552 	 */
553 	ki->ki_mi.mi_cmd.mri_next = 0;
554 	ki->ki_mi.mi_rsp.mri_next = 0;
555 
556 	/*
557 	 * Start up the hardware initialisation sequence.
558 	 */
559 #define	STEP0MASK (KDB_ERR | KDB_STEP4 | KDB_STEP3 | KDB_STEP2 | KDB_STEP1)
560 
561 	ki->ki_state = ST_IDLE;	/* in case init fails */
562 
563 	bi_reset(&ka->kdb_bi);	/* reset bi node (but not the BI itself) */
564 
565 	timo = todr() + 1000;
566 	while ((ka->kdb_sa & STEP0MASK) == 0) {
567 		if (todr() > timo) {
568 			printf("kdb%d: timeout during init\n", ki->ki_ctlr);
569 			return (-1);
570 		}
571 	}
572 	if ((ka->kdb_sa & STEP0MASK) != KDB_STEP1) {
573 		printf("kdb%d: init failed, sa=%b\n", ki->ki_ctlr,
574 			ka->kdb_sa, kdbsr_bits);
575 		return (-1);
576 	}
577 
578 	/*
579 	 * Success!  Record new state, and start step 1 initialisation.
580 	 * The rest is done in the interrupt handler.
581 	 */
582 	ki->ki_state = ST_STEP1;
583 	ka->kdb_bi.bi_intrdes = 1 << mastercpu;
584 #ifdef unneeded /* is it? */
585 	ka->kdb_bi.bi_csr = (ka->kdb_bi.bi_csr&~BICSR_ARB_MASK)|BICSR_ARB_???;
586 #endif
587 	ka->kdb_bi.bi_bcicsr |= BCI_STOPEN | BCI_IDENTEN | BCI_UINTEN |
588 		BCI_INTEN;
589 
590 /* I THINK THIS IS WRONG */
591 /* Mach uses 0x601d0, which includes IPL16, but 1d0 is IPL17, nexzvec...? */
592 	ka->kdb_bi.bi_eintrcsr = BIEIC_IPL15 | ki->ki_vec;	/* ??? */
593 /* END I THINK WRONG */
594 
595 	ka->kdb_bi.bi_uintrcsr = ki->ki_vec;
596 	ka->kdb_sw = KDB_ERR | (NCMDL2 << 11) | (NRSPL2 << 8) | KDB_IE |
597 		(ki->ki_vec >> 2);
598 	return (0);
599 }
600 
601 /*
602  * Open a drive.
603  */
604 /*ARGSUSED*/
605 kdbopen(dev, flag)
606 	dev_t dev;
607 	int flag;
608 {
609 	register int unit;
610 	register struct uba_device *ui;
611 	register struct kdbinfo *ki;
612 	int s;
613 
614 	/*
615 	 * Make sure this is a reasonable open request.
616 	 */
617 	unit = kdbunit(dev);
618 	if (unit >= NKRA || (ui = kdbdinfo[unit]) == 0 || ui->ui_alive == 0)
619 		return (ENXIO);
620 
621 	/*
622 	 * Make sure the controller is running, by (re)initialising it if
623 	 * necessary.
624 	 */
625 	ki = &kdbinfo[ui->ui_ctlr];
626 	s = spl5();
627 	if (ki->ki_state != ST_RUN) {
628 		if (ki->ki_state == ST_IDLE && kdbinit(ki)) {
629 			splx(s);
630 			return (EIO);
631 		}
632 		/*
633 		 * In case it does not come up, make sure we will be
634 		 * restarted in 10 seconds.  This corresponds to the
635 		 * 10 second timeouts in kdbprobe() and kdbslave().
636 		 */
637 		ki->ki_flags |= KDB_DOWAKE;
638 		timeout(wakeup, (caddr_t)&ki->ki_flags, 10 * hz);
639 		sleep((caddr_t)&ki->ki_flags, PRIBIO);
640 		if (ki->ki_state != ST_RUN) {
641 			splx(s);
642 			printf("kdb%d: controller hung\n", ui->ui_ctlr);
643 			return (EIO);
644 		}
645 		untimeout(wakeup, (caddr_t)&ki->ki_flags);
646 	}
647 	if ((ui->ui_flags & UNIT_ONLINE) == 0) {
648 		/*
649 		 * Bring the drive on line so we can find out how
650 		 * big it is.  If it is not spun up, it will not
651 		 * come on line; this cannot really be considered
652 		 * an `error condition'.
653 		 */
654 		if (kdb_bringonline(ui, 0)) {
655 			splx(s);
656 			printf("%s%d: drive will not come on line\n",
657 				kdbdriver.ud_dname, unit);
658 			return (EIO);
659 		}
660 	}
661 	splx(s);
662 	return (0);
663 }
664 
665 /*
666  * Bring a drive on line.  In case it fails to respond, we set
667  * a timeout on it.  The `nosleep' parameter should be set if
668  * we are to spin-wait; otherwise this must be called at spl5().
669  */
670 kdb_bringonline(ui, nosleep)
671 	register struct uba_device *ui;
672 	int nosleep;
673 {
674 	register struct kdbinfo *ki = &kdbinfo[ui->ui_ctlr];
675 	register struct mscp *mp;
676 	int i;
677 
678 	if (nosleep) {
679 		mp = mscp_getcp(&ki->ki_mi, MSCP_DONTWAIT);
680 		if (mp == NULL)
681 			return (-1);
682 	} else
683 		mp = mscp_getcp(&ki->ki_mi, MSCP_WAIT);
684 	mp->mscp_opcode = M_OP_ONLINE;
685 	mp->mscp_unit = ui->ui_slave;
686 	mp->mscp_cmdref = (long)&ui->ui_flags;
687 	*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
688 	i = ki->ki_kdb->kdb_ip;
689 
690 	if (nosleep) {
691 		i = todr() + 1000;
692 		while ((ui->ui_flags & UNIT_ONLINE) == 0)
693 			if (todr() > i)
694 				return (-1);
695 	} else {
696 		timeout(wakeup, (caddr_t)&ui->ui_flags, 10 * hz);
697 		sleep((caddr_t)&ui->ui_flags, PRIBIO);
698 		if ((ui->ui_flags & UNIT_ONLINE) == 0)
699 			return (-1);
700 		untimeout(wakeup, (caddr_t)&ui->ui_flags);
701 	}
702 	return (0);	/* made it */
703 }
704 
705 /*
706  * Queue a transfer request, and if possible, hand it to the controller.
707  *
708  * This routine is broken into two so that the internal version
709  * kdbstrat1() can be called by the (nonexistent, as yet) bad block
710  * revectoring routine.
711  */
712 kdbstrategy(bp)
713 	register struct buf *bp;
714 {
715 	register int unit;
716 	register struct uba_device *ui;
717 	register struct size *st;
718 	daddr_t sz, maxsz;
719 
720 	/*
721 	 * Make sure this is a reasonable drive to use.
722 	 */
723 	if ((unit = kdbunit(bp->b_dev)) >= NKRA ||
724 	    (ui = kdbdinfo[unit]) == NULL || ui->ui_alive == 0) {
725 		bp->b_error = ENXIO;
726 		bp->b_flags |= B_ERROR;
727 		biodone(bp);
728 		return;
729 	}
730 
731 	/*
732 	 * Determine the size of the transfer, and make sure it is
733 	 * within the boundaries of the drive.
734 	 */
735 	sz = (bp->b_bcount + 511) >> 9;
736 	st = &kdbtypes[ui->ui_type].ut_sizes[kdbpart(bp->b_dev)];
737 	if ((maxsz = st->nblocks) < 0)
738 		maxsz = ra_dsize[unit] - st->blkoff;
739 	if (bp->b_blkno < 0 || bp->b_blkno + sz > maxsz ||
740 	    st->blkoff >= ra_dsize[unit]) {
741 		/* if exactly at end of disk, return an EOF */
742 		if (bp->b_blkno == maxsz)
743 			bp->b_resid = bp->b_bcount;
744 		else {
745 			bp->b_error = EINVAL;
746 			bp->b_flags |= B_ERROR;
747 		}
748 		biodone(bp);
749 		return;
750 	}
751 	kdbstrat1(bp);
752 }
753 
754 /*
755  * Work routine for kdbstrategy.
756  */
757 kdbstrat1(bp)
758 	register struct buf *bp;
759 {
760 	register int unit = kdbunit(bp->b_dev);
761 	register struct buf *dp;
762 	register struct kdbinfo *ki;
763 	struct uba_device *ui;
764 	int s;
765 
766 	/*
767 	 * Append the buffer to the drive queue, and if it is not
768 	 * already there, the drive to the controller queue.  (However,
769 	 * if the drive queue is marked to be requeued, we must be
770 	 * awaiting an on line or get unit status command; in this
771 	 * case, leave it off the controller queue.)
772 	 */
773 	ui = kdbdinfo[unit];
774 	ki = &kdbinfo[ui->ui_ctlr];
775 	dp = &kdbutab[unit];
776 	s = spl5();
777 	APPEND(bp, dp, av_forw);
778 	if (dp->b_active == 0 && (ui->ui_flags & UNIT_REQUEUE) == 0) {
779 		APPEND(dp, &ki->ki_tab, b_forw);
780 		dp->b_active++;
781 	}
782 
783 	/*
784 	 * Start activity on the controller.
785 	 */
786 	kdbstart(ki);
787 	splx(s);
788 }
789 
790 /*
791  * Find the physical address of some contiguous PTEs that map the
792  * transfer described in `bp', creating them (by copying) if
793  * necessary.  Store the physical base address of the map through
794  * mapbase, and the page offset through offset, and any resource
795  * information in *info (or 0 if none).
796  *
797  * If we cannot allocate space, return a nonzero status.
798  */
799 int
800 kdbmap(ki, bp, mapbase, offset, info)
801 	struct kdbinfo *ki;
802 	register struct buf *bp;
803 	long *mapbase, *offset;
804 	int *info;
805 {
806 	register struct pte *spte, *dpte;
807 	register struct proc *rp;
808 	register int i, a, o;
809 	u_int v;
810 	int npf;
811 
812 	o = (int)bp->b_un.b_addr & PGOFSET;
813 
814 	/* handle contiguous cases */
815 	if ((bp->b_flags & B_PHYS) == 0) {
816 		spte = kvtopte(bp->b_un.b_addr);
817 		kdbstats.ks_sys++;
818 		*mapbase = PHYS(long, spte);
819 		*offset = o;
820 		*info = 0;
821 		return (0);
822 	}
823 	if (bp->b_flags & B_PAGET) {
824 		spte = &Usrptmap[btokmx((struct pte *)bp->b_un.b_addr)];
825 if (spte->pg_v == 0) panic("kdbmap");
826 		kdbstats.ks_paget++;
827 		*mapbase = PHYS(long, spte);
828 		*offset = o;
829 		*info = 0;
830 		return (0);
831 	}
832 
833 	/* potentially discontiguous or invalid ptes */
834 	v = btop(bp->b_un.b_addr);
835 	rp = bp->b_flags & B_DIRTY ? &proc[2] : bp->b_proc;
836 	if (bp->b_flags & B_UAREA)
837 		spte = &rp->p_addr[v];
838 	else
839 		spte = vtopte(rp, v);
840 	npf = btoc(bp->b_bcount + o);
841 
842 #ifdef notdef
843 	/*
844 	 * The current implementation of the VM system requires
845 	 * that all of these be done with a copy.  Even if the
846 	 * PTEs could be used now, they may be snatched out from
847 	 * under us later.  It would be nice if we could stop that....
848 	 */
849 
850 	/* check for invalid */
851 	/* CONSIDER CHANGING VM TO VALIDATE PAGES EARLIER */
852 	for (dpte = spte, i = npf; --i >= 0; dpte++)
853 		if (dpte->pg_v == 0)
854 			goto copy1;
855 	/*
856 	 * Check for discontiguous physical pte addresses.  It is
857 	 * not necessary to check each pte, since they come in clumps
858 	 * of pages.
859 	 */
860 	i = howmany(npf + (((int)spte & PGOFSET) / sizeof (*spte)), NPTEPG);
861 	/* often i==1, and we can avoid work */
862 	if (--i > 0) {
863 		dpte = kvtopte(spte);
864 		a = dpte->pg_pfnum;
865 		while (--i >= 0)
866 			if ((++dpte)->pg_pfnum != ++a)
867 				goto copy2;
868 	}
869 
870 	/* made it */
871 	kdbstats.ks_contig++;
872 	*mapbase = kvtophys(spte);
873 	*offset = o;
874 	*info = 0;
875 	return (0);
876 
877 copy1:
878 	kdbstats.ks_inval++;		/* temp */
879 copy2:
880 #endif /* notdef */
881 	kdbstats.ks_copies++;
882 	i = npf + 1;
883 	if ((a = rmalloc(ki->ki_map, (long)i)) == 0) {
884 		kdbstats.ks_mapwait++;
885 		return (-1);
886 	}
887 	*info = (i << 16) | a;
888 	a--;
889 	/* if offset > PGOFSET, btop(offset) indexes mapbase */
890 	*mapbase = ki->ki_ptephys;
891 	*offset = (a << PGSHIFT) | o;
892 	dpte = &ki->ki_pte[a];
893 	while (--i > 0)
894 		*(int *)dpte++ = PG_V | *(int *)spte++;
895 	*(int *)dpte = 0;
896 	return (0);
897 }
898 
899 #define	KDBFREE(ki, info) if (info) \
900 	rmfree((ki)->ki_map, (long)((info) >> 16), (long)((info) & 0xffff))
901 
902 /*
903  * Start up whatever transfers we can find.
904  * Note that kdbstart() must be called at spl5().
905  */
906 kdbstart(ki)
907 	register struct kdbinfo *ki;
908 {
909 	register struct buf *bp, *dp;
910 	register struct mscp *mp;
911 	register struct uba_device *ui;
912 	long mapbase, offset;
913 	int info, ncmd = 0;
914 
915 	/*
916 	 * If it is not running, try (again and again...) to initialise
917 	 * it.  If it is currently initialising just ignore it for now.
918 	 */
919 	if (ki->ki_state != ST_RUN) {
920 		if (ki->ki_state == ST_IDLE && kdbinit(ki))
921 			printf("kdb%d: still hung\n", ki->ki_ctlr);
922 		return;
923 	}
924 
925 loop:
926 	/* if insufficient credit, avoid overhead */
927 	if (ki->ki_mi.mi_credits <= MSCP_MINCREDITS)
928 		goto out;
929 
930 	/*
931 	 * Service the drive at the head of the queue.  It may not
932 	 * need anything; eventually this will finish up the close
933 	 * protocol, but that is yet to be implemented here.
934 	 */
935 	if ((dp = ki->ki_tab.b_actf) == NULL)
936 		goto out;
937 	if ((bp = dp->b_actf) == NULL) {
938 		dp->b_active = 0;
939 		ki->ki_tab.b_actf = dp->b_forw;
940 		goto loop;
941 	}
942 
943 	if (ki->ki_kdb->kdb_sa & KDB_ERR) {	/* ctlr fatal error */
944 		kdbsaerror(ki);
945 		goto out;
946 	}
947 
948 	 /* find or create maps for this transfer */
949 	 if (kdbmap(ki, bp, &mapbase, &offset, &info))
950 		goto out;	/* effectively, resource wait */
951 
952 	/*
953 	 * Get an MSCP packet, then figure out what to do.  If
954 	 * we cannot get a command packet, the command ring may
955 	 * be too small:  We should have at least as many command
956 	 * packets as credits, for best performance.
957 	 */
958 	if ((mp = mscp_getcp(&ki->ki_mi, MSCP_DONTWAIT)) == NULL) {
959 		if (ki->ki_mi.mi_credits > MSCP_MINCREDITS &&
960 		    (ki->ki_flags & KDB_GRIPED) == 0) {
961 			log(LOG_NOTICE, "kdb%d: command ring too small\n",
962 				ki->ki_ctlr);
963 			ki->ki_flags |= KDB_GRIPED;/* complain only once */
964 		}
965 		KDBFREE(ki, info);
966 		goto out;
967 	}
968 
969 	/*
970 	 * Bring the drive on line if it is not already.  Get its status
971 	 * if we do not already have it.  Otherwise just start the transfer.
972 	 */
973 	ui = kdbdinfo[kdbunit(bp->b_dev)];
974 	if ((ui->ui_flags & UNIT_ONLINE) == 0) {
975 		mp->mscp_opcode = M_OP_ONLINE;
976 		goto common;
977 	}
978 	if ((ui->ui_flags & UNIT_HAVESTATUS) == 0) {
979 		mp->mscp_opcode = M_OP_GETUNITST;
980 common:
981 if (ui->ui_flags & UNIT_REQUEUE) panic("kdbstart");
982 		/*
983 		 * Take the drive off the controller queue.  When the
984 		 * command finishes, make sure the drive is requeued.
985 		 * Give up any mapping (not needed now).  This last is
986 		 * not efficient, but is rare.
987 		 */
988 		KDBFREE(ki, info);
989 		ki->ki_tab.b_actf = dp->b_forw;
990 		dp->b_active = 0;
991 		ui->ui_flags |= UNIT_REQUEUE;
992 		mp->mscp_unit = ui->ui_slave;
993 		*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
994 		ncmd++;
995 		goto loop;
996 	}
997 
998 	mp->mscp_opcode = (bp->b_flags & B_READ) ? M_OP_READ : M_OP_WRITE;
999 	mp->mscp_unit = ui->ui_slave;
1000 	mp->mscp_seq.seq_lbn = bp->b_blkno +
1001 		kdbtypes[ui->ui_type].ut_sizes[kdbpart(bp->b_dev)].blkoff;
1002 	mp->mscp_seq.seq_bytecount = bp->b_bcount;
1003 
1004 	mp->mscp_seq.seq_buffer = offset | KDB_MAP;
1005 	mp->mscp_seq.seq_mapbase = mapbase;
1006 
1007 	/* profile the drive */
1008 	if (ui->ui_dk >= 0) {
1009 		dk_busy |= 1 << ui->ui_dk;
1010 		dk_xfer[ui->ui_dk]++;
1011 		dk_wds[ui->ui_dk] += bp->b_bcount >> 6;
1012 	}
1013 
1014 	/*
1015 	 * Fill in the rest of the MSCP packet and move the buffer to the
1016 	 * I/O wait queue.
1017 	 */
1018 	mscp_go(&ki->ki_mi, mp, info);
1019 	ncmd++;			/* note the transfer */
1020 	ki->ki_tab.b_active++;	/* another one going */
1021 	goto loop;
1022 
1023 out:
1024 	if (ncmd >= KS_MAXC)
1025 		ncmd = KS_MAXC - 1;
1026 	kdbstats.ks_cmd[ncmd]++;
1027 	if (ncmd)		/* start some transfers */
1028 		ncmd = ki->ki_kdb->kdb_ip;
1029 }
1030 
1031 /* ARGSUSED */
1032 kdbiodone(mi, bp, info)
1033 	struct mscp_info *mi;
1034 	struct buf *bp;
1035 	int info;
1036 {
1037 	register struct kdbinfo *ki = &kdbinfo[mi->mi_ctlr];
1038 
1039 	KDBFREE(ki, info);
1040 	biodone(bp);
1041 	ki->ki_tab.b_active--;	/* another one done */
1042 }
1043 
1044 /*
1045  * The error bit was set in the controller status register.  Gripe,
1046  * reset the controller, requeue pending transfers.
1047  */
1048 kdbsaerror(ki)
1049 	register struct kdbinfo *ki;
1050 {
1051 
1052 	printf("kdb%d: controller error, sa=%b\n", ki->ki_ctlr,
1053 		ki->ki_kdb->kdb_sa, kdbsr_bits);
1054 	mscp_requeue(&ki->ki_mi);
1055 	(void) kdbinit(ki);
1056 }
1057 
1058 /*
1059  * Interrupt routine.  Depending on the state of the controller,
1060  * continue initialisation, or acknowledge command and response
1061  * interrupts, and process responses.
1062  */
1063 kdbintr(ctlr)
1064 	int ctlr;
1065 {
1066 	register struct kdbinfo *ki = &kdbinfo[ctlr];
1067 	register struct kdb_regs *kdbaddr = ki->ki_kdb;
1068 	register struct mscp *mp;
1069 	register int i;
1070 
1071 	ki->ki_wticks = 0;	/* reset interrupt watchdog */
1072 
1073 	/*
1074 	 * Combinations during steps 1, 2, and 3: STEPnMASK
1075 	 * corresponds to which bits should be tested;
1076 	 * STEPnGOOD corresponds to the pattern that should
1077 	 * appear after the interrupt from STEPn initialisation.
1078 	 * All steps test the bits in ALLSTEPS.
1079 	 */
1080 #define	ALLSTEPS	(KDB_ERR|KDB_STEP4|KDB_STEP3|KDB_STEP2|KDB_STEP1)
1081 
1082 #define	STEP1MASK	(ALLSTEPS | KDB_IE | KDB_NCNRMASK)
1083 #define	STEP1GOOD	(KDB_STEP2 | KDB_IE | (NCMDL2 << 3) | NRSPL2)
1084 
1085 #define	STEP2MASK	(ALLSTEPS | KDB_IE | KDB_IVECMASK)
1086 #define	STEP2GOOD	(KDB_STEP3 | KDB_IE | (ki->ki_vec >> 2))
1087 
1088 #define	STEP3MASK	ALLSTEPS
1089 #define	STEP3GOOD	KDB_STEP4
1090 
1091 	switch (ki->ki_state) {
1092 
1093 	case ST_IDLE:
1094 		/*
1095 		 * Ignore unsolicited interrupts.
1096 		 */
1097 		log(LOG_WARNING, "kdb%d: stray intr\n", ctlr);
1098 		return;
1099 
1100 	case ST_STEP1:
1101 		/*
1102 		 * Begin step two initialisation.
1103 		 */
1104 		if ((kdbaddr->kdb_sa & STEP1MASK) != STEP1GOOD) {
1105 			i = 1;
1106 initfailed:
1107 			printf("kdb%d: init step %d failed, sa=%b\n",
1108 				ctlr, i, kdbaddr->kdb_sa, kdbsr_bits);
1109 			ki->ki_state = ST_IDLE;
1110 			if (ki->ki_flags & KDB_DOWAKE) {
1111 				ki->ki_flags &= ~KDB_DOWAKE;
1112 				wakeup((caddr_t)&ki->ki_flags);
1113 			}
1114 			return;
1115 		}
1116 		kdbaddr->kdb_sw = PHYS(int, &ki->ki_ca.ca_rspdsc[0]);
1117 		ki->ki_state = ST_STEP2;
1118 		return;
1119 
1120 	case ST_STEP2:
1121 		/*
1122 		 * Begin step 3 initialisation.
1123 		 */
1124 		if ((kdbaddr->kdb_sa & STEP2MASK) != STEP2GOOD) {
1125 			i = 2;
1126 			goto initfailed;
1127 		}
1128 		kdbaddr->kdb_sw = PHYS(int, &ki->ki_ca.ca_rspdsc[0]) >> 16;
1129 		ki->ki_state = ST_STEP3;
1130 		return;
1131 
1132 	case ST_STEP3:
1133 		/*
1134 		 * Set controller characteristics (finish initialisation).
1135 		 */
1136 		if ((kdbaddr->kdb_sa & STEP3MASK) != STEP3GOOD) {
1137 			i = 3;
1138 			goto initfailed;
1139 		}
1140 		i = kdbaddr->kdb_sa & 0xff;
1141 		if (i != ki->ki_micro) {
1142 			ki->ki_micro = i;
1143 			printf("kdb%d: version %d model %d\n",
1144 				ctlr, i & 0xf, i >> 4);
1145 		}
1146 
1147 		kdbaddr->kdb_sw = KDB_GO;
1148 
1149 		/* initialise hardware data structures */
1150 		for (i = 0, mp = ki->ki_rsp; i < NRSP; i++, mp++) {
1151 			ki->ki_ca.ca_rspdsc[i] = MSCP_OWN | MSCP_INT |
1152 				PHYS(long, &ki->ki_rsp[i].mscp_cmdref);
1153 			mp->mscp_addr = &ki->ki_ca.ca_rspdsc[i];
1154 			mp->mscp_msglen = MSCP_MSGLEN;
1155 		}
1156 		for (i = 0, mp = ki->ki_cmd; i < NCMD; i++, mp++) {
1157 			ki->ki_ca.ca_cmddsc[i] = MSCP_INT |
1158 				PHYS(long, &ki->ki_cmd[i].mscp_cmdref);
1159 			mp->mscp_addr = &ki->ki_ca.ca_cmddsc[i];
1160 			mp->mscp_msglen = MSCP_MSGLEN;
1161 		}
1162 
1163 		/*
1164 		 * Before we can get a command packet, we need some
1165 		 * credits.  Fake some up to keep mscp_getcp() happy,
1166 		 * get a packet, and cancel all credits (the right
1167 		 * number should come back in the response to the
1168 		 * SCC packet).
1169 		 */
1170 		ki->ki_mi.mi_credits = MSCP_MINCREDITS + 1;
1171 		mp = mscp_getcp(&ki->ki_mi, MSCP_DONTWAIT);
1172 		if (mp == NULL)	/* `cannot happen' */
1173 			panic("kdbintr");
1174 		ki->ki_mi.mi_credits = 0;
1175 		mp->mscp_opcode = M_OP_SETCTLRC;
1176 		mp->mscp_unit = 0;
1177 		mp->mscp_sccc.sccc_ctlrflags = M_CF_ATTN | M_CF_MISC |
1178 			M_CF_THIS;
1179 		*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
1180 		i = kdbaddr->kdb_ip;
1181 		ki->ki_state = ST_SETCHAR;
1182 		return;
1183 
1184 	case ST_SETCHAR:
1185 	case ST_RUN:
1186 		/*
1187 		 * Handle Set Ctlr Characteristics responses and operational
1188 		 * responses (via mscp_dorsp).
1189 		 */
1190 		break;
1191 
1192 	default:
1193 		log(LOG_ERR, "kdb%d: driver bug, state %d\n", ctlr,
1194 			ki->ki_state);
1195 		return;
1196 	}
1197 
1198 	if (kdbaddr->kdb_sa & KDB_ERR) {/* ctlr fatal error */
1199 		kdbsaerror(ki);
1200 		return;
1201 	}
1202 
1203 	/*
1204 	 * Handle buffer purge requests.
1205 	 * KDB DOES NOT HAVE BDPs
1206 	 */
1207 	if (ki->ki_ca.ca_bdp) {
1208 		printf("kdb%d: purge bdp %d\n", ctlr, ki->ki_ca.ca_bdp);
1209 		panic("kdb purge");
1210 	}
1211 
1212 	/*
1213 	 * Check for response and command ring transitions.
1214 	 */
1215 	if (ki->ki_ca.ca_rspint) {
1216 		ki->ki_ca.ca_rspint = 0;
1217 		mscp_dorsp(&ki->ki_mi);
1218 	}
1219 	if (ki->ki_ca.ca_cmdint) {
1220 		ki->ki_ca.ca_cmdint = 0;
1221 		MSCP_DOCMD(&ki->ki_mi);
1222 	}
1223 	if (ki->ki_tab.b_actf != NULL)
1224 		kdbstart(ki);
1225 }
1226 
1227 /*
1228  * Handle an error datagram.  All we do now is decode it.
1229  */
1230 kdbdgram(mi, mp)
1231 	struct mscp_info *mi;
1232 	struct mscp *mp;
1233 {
1234 
1235 	mscp_decodeerror(mi->mi_md->md_mname, mi->mi_ctlr, mp);
1236 }
1237 
1238 /*
1239  * The Set Controller Characteristics command finished.
1240  * Record the new state of the controller.
1241  */
1242 kdbctlrdone(mi, mp)
1243 	struct mscp_info *mi;
1244 	struct mscp *mp;
1245 {
1246 	register struct kdbinfo *ki = &kdbinfo[mi->mi_ctlr];
1247 
1248 	if ((mp->mscp_status & M_ST_MASK) == M_ST_SUCCESS)
1249 		ki->ki_state = ST_RUN;
1250 	else {
1251 		printf("kdb%d: SETCTLRC failed, status 0x%x\n",
1252 			ki->ki_ctlr, mp->mscp_status);
1253 		ki->ki_state = ST_IDLE;
1254 	}
1255 	if (ki->ki_flags & KDB_DOWAKE) {
1256 		ki->ki_flags &= ~KDB_DOWAKE;
1257 		wakeup((caddr_t)&ki->ki_flags);
1258 	}
1259 }
1260 
1261 /*
1262  * Received a response from an as-yet unconfigured drive.  Configure it
1263  * in, if possible.
1264  */
1265 kdbunconf(mi, mp)
1266 	struct mscp_info *mi;
1267 	register struct mscp *mp;
1268 {
1269 
1270 	/*
1271 	 * If it is a slave response, copy it to kdbslavereply for
1272 	 * kdbslave() to look at.
1273 	 */
1274 	if (mp->mscp_opcode == (M_OP_GETUNITST | M_OP_END) &&
1275 	    (kdbinfo[mi->mi_ctlr].ki_flags & KDB_INSLAVE) != 0) {
1276 		kdbslavereply = *mp;
1277 		return (MSCP_DONE);
1278 	}
1279 
1280 	/*
1281 	 * Otherwise, it had better be an available attention response.
1282 	 */
1283 	if (mp->mscp_opcode != M_OP_AVAILATTN)
1284 		return (MSCP_FAILED);
1285 
1286 	/* do what autoconf does */
1287 	return (MSCP_FAILED);	/* not yet */
1288 }
1289 
1290 /*
1291  * A drive came on line.  Check its type and size.  Return DONE if
1292  * we think the drive is truly on line.  In any case, awaken anyone
1293  * sleeping on the drive on-line-ness.
1294  */
1295 kdbonline(ui, mp)
1296 	register struct uba_device *ui;
1297 	struct mscp *mp;
1298 {
1299 	register int type;
1300 
1301 	wakeup((caddr_t)&ui->ui_flags);
1302 	if ((mp->mscp_status & M_ST_MASK) != M_ST_SUCCESS) {
1303 		printf("kdb%d: attempt to bring %s%d on line failed:",
1304 			ui->ui_ctlr, kdbdriver.ud_dname, ui->ui_unit);
1305 		mscp_printevent(mp);
1306 		return (MSCP_FAILED);
1307 	}
1308 
1309 	type = mp->mscp_onle.onle_drivetype;
1310 	if (type >= NTYPES || kdbtypes[type].ut_name == 0) {
1311 		printf("kdb%d: %s%d: unknown type %d\n",
1312 			ui->ui_ctlr, kdbdriver.ud_dname, ui->ui_unit, type);
1313 		return (MSCP_FAILED);
1314 	}
1315 	/*
1316 	 * Note any change of types.  Not sure if we should do
1317 	 * something special about them, or if so, what....
1318 	 */
1319 	if (type != ui->ui_type) {
1320 		printf("%s%d: changed types! was %s\n",
1321 			kdbdriver.ud_dname, ui->ui_unit,
1322 			kdbtypes[ui->ui_type].ut_name);
1323 		ui->ui_type = type;
1324 	}
1325 	ra_dsize[ui->ui_unit] = (daddr_t) mp->mscp_onle.onle_unitsize;
1326 	printf("%s%d: %s, size = %d sectors\n",
1327 		kdbdriver.ud_dname, ui->ui_unit,
1328 		kdbtypes[type].ut_name, ra_dsize[ui->ui_unit]);
1329 	return (MSCP_DONE);
1330 }
1331 
1332 /*
1333  * We got some (configured) unit's status.  Return DONE if it succeeded.
1334  */
1335 kdbgotstatus(ui, mp)
1336 	register struct uba_device *ui;
1337 	register struct mscp *mp;
1338 {
1339 
1340 	if ((mp->mscp_status & M_ST_MASK) != M_ST_SUCCESS) {
1341 		printf("kdb%d: attempt to get status for %s%d failed:",
1342 			ui->ui_ctlr, kdbdriver.ud_dname, ui->ui_unit);
1343 		mscp_printevent(mp);
1344 		return (MSCP_FAILED);
1345 	}
1346 	/* need to record later for bad block forwarding - for now, print */
1347 	printf("\
1348 %s%d: unit %d, nspt %d, group %d, ngpc %d, rctsize %d, nrpt %d, nrct %d\n",
1349 		kdbdriver.ud_dname, ui->ui_unit, mp->mscp_unit,
1350 		mp->mscp_guse.guse_nspt, mp->mscp_guse.guse_group,
1351 		mp->mscp_guse.guse_ngpc, mp->mscp_guse.guse_rctsize,
1352 		mp->mscp_guse.guse_nrpt, mp->mscp_guse.guse_nrct);
1353 	return (MSCP_DONE);
1354 }
1355 
1356 /*
1357  * A transfer failed.  We get a chance to fix or restart it.
1358  * Need to write the bad block forwaring code first....
1359  */
1360 /*ARGSUSED*/
1361 kdbioerror(ui, mp, bp)
1362 	register struct uba_device *ui;
1363 	register struct mscp *mp;
1364 	struct buf *bp;
1365 {
1366 
1367 	if (mp->mscp_flags & M_EF_BBLKR) {
1368 		/*
1369 		 * A bad block report.  Eventually we will
1370 		 * restart this transfer, but for now, just
1371 		 * log it and give up.
1372 		 */
1373 		log(LOG_ERR, "%s%d: bad block report: %d%s\n",
1374 			kdbdriver.ud_dname, ui->ui_unit, mp->mscp_seq.seq_lbn,
1375 			mp->mscp_flags & M_EF_BBLKU ? " + others" : "");
1376 	} else {
1377 		/*
1378 		 * What the heck IS a `serious exception' anyway?
1379 		 */
1380 		if (mp->mscp_flags & M_EF_SEREX)
1381 			log(LOG_ERR, "%s%d: serious exception reported\n",
1382 				kdbdriver.ud_dname, ui->ui_unit);
1383 	}
1384 	return (MSCP_FAILED);
1385 }
1386 
1387 
1388 #ifdef notyet
1389 /*
1390  * I/O controls.  Not yet!
1391  */
1392 kdbioctl(dev, cmd, flag, data)
1393 	dev_t dev;
1394 	int cmd, flag;
1395 	caddr_t data;
1396 {
1397 	int error = 0;
1398 	register int unit = kdbunit(dev);
1399 
1400 	if (unit >= NKRA || uddinfo[unit] == NULL)
1401 		return (ENXIO);
1402 
1403 	switch (cmd) {
1404 
1405 	case KDBIOCREPLACE:
1406 		/*
1407 		 * Initiate bad block replacement for the given LBN.
1408 		 * (Should we allow modifiers?)
1409 		 */
1410 		error = EOPNOTSUPP;
1411 		break;
1412 
1413 	case KDBIOCGMICRO:
1414 		/*
1415 		 * Return the microcode revision for the KDB50 running
1416 		 * this drive.
1417 		 */
1418 		*(int *)data = kdbinfo[kdbdinfo[unit]->ui_ctlr].ki_micro;
1419 		break;
1420 
1421 	case KDBIOCGSIZE:
1422 		/*
1423 		 * Return the size (in 512 byte blocks) of this
1424 		 * disk drive.
1425 		 */
1426 		*(daddr_t *)data = ra_dsize[unit];
1427 		break;
1428 
1429 	default:
1430 		error = EINVAL;
1431 		break;
1432 	}
1433 	return (error);
1434 }
1435 #endif
1436 
1437 #ifdef notyet
1438 /*
1439  * Reset a KDB50 (self test and all).
1440  * What if it fails?
1441  */
1442 kdbreset(ki)
1443 	register struct kdbinfo *ki;
1444 {
1445 
1446 	printf("reset kdb%d", ki->ki_ctlr);
1447 	bi_selftest(&ki->ki_kdb.kdb_bi);
1448 	ki->ki_state = ST_IDLE;
1449 	rminit(ki->ki_map, (long)KI_PTES, (long)1, "kdb", KI_MAPSIZ);
1450 	mscp_requeue(&ki->ki_mi);
1451 	if (kdbinit(ctlr))
1452 		printf(" (hung)");
1453 	printf("\n");
1454 }
1455 #endif
1456 
1457 /*
1458  * Watchdog timer:  If the controller is active, and no interrupts
1459  * have occurred for 30 seconds, assume it has gone away.
1460  */
1461 kdbwatch()
1462 {
1463 	register struct kdbinfo *ki;
1464 	register int i;
1465 
1466 	timeout(kdbwatch, (caddr_t)0, hz);	/* every second */
1467 	for (i = 0, ki = kdbinfo; i < NKDB; i++, ki++) {
1468 		if ((ki->ki_flags & KDB_ALIVE) == 0)
1469 			continue;
1470 		if (ki->ki_state == ST_IDLE)
1471 			continue;
1472 		if (ki->ki_state == ST_RUN && !ki->ki_tab.b_active)
1473 			ki->ki_wticks = 0;
1474 		else if (++ki->ki_wticks >= 30) {
1475 			ki->ki_wticks = 0;
1476 			printf("kdb%d: lost interrupt\n", i);
1477 			/* kdbreset(ki); */
1478 			panic("kdb lost interrupt");
1479 		}
1480 	}
1481 }
1482 
1483 /*
1484  * Do a panic dump.
1485  */
1486 #define	DBSIZE	32		/* dump 16K at a time */
1487 
1488 struct kdbdumpspace {
1489 	struct	kdb1ca kd_ca;
1490 	struct	mscp kd_rsp;
1491 	struct	mscp kd_cmd;
1492 } kdbdumpspace;
1493 
1494 kdbdump(dev)
1495 	dev_t dev;
1496 {
1497 	register struct kdbdumpspace *kd;
1498 	register struct kdb_regs *k;
1499 	register int i;
1500 	struct uba_device *ui;
1501 	char *start;
1502 	int num, blk, unit, maxsz, blkoff;
1503 
1504 	/*
1505 	 * Make sure the device is a reasonable place on which to dump.
1506 	 */
1507 	unit = kdbunit(dev);
1508 	if (unit >= NKRA)
1509 		return (ENXIO);
1510 	ui = PHYS(struct uba_device *, kdbdinfo[unit]);
1511 	if (ui == NULL || ui->ui_alive == 0)
1512 		return (ENXIO);
1513 
1514 	/*
1515 	 * Find and initialise the KDB; get the physical address of the
1516 	 * device registers, and of communications area and command and
1517 	 * response packet.
1518 	 */
1519 	k = PHYS(struct kdbinfo *, &kdbinfo[ui->ui_ctlr])->ki_physkdb;
1520 	kd = PHYS(struct kdbdumpspace *, &kdbdumpspace);
1521 
1522 	/*
1523 	 * Initialise the controller, with one command and one response
1524 	 * packet.
1525 	 */
1526 	bi_reset(&k->kdb_bi);
1527 	if (kdbdumpwait(k, KDB_STEP1))
1528 		return (EFAULT);
1529 	k->kdb_sw = KDB_ERR;
1530 	if (kdbdumpwait(k, KDB_STEP2))
1531 		return (EFAULT);
1532 	k->kdb_sw = (int)&kd->kd_ca.ca_rspdsc;
1533 	if (kdbdumpwait(k, KDB_STEP3))
1534 		return (EFAULT);
1535 	k->kdb_sw = ((int)&kd->kd_ca.ca_rspdsc) >> 16;
1536 	if (kdbdumpwait(k, KDB_STEP4))
1537 		return (EFAULT);
1538 	k->kdb_sw = KDB_GO;
1539 
1540 	/*
1541 	 * Set up the command and response descriptor, then set the
1542 	 * controller characteristics and bring the drive on line.
1543 	 * Note that all uninitialised locations in kd_cmd are zero.
1544 	 */
1545 	kd->kd_ca.ca_rspdsc = (long)&kd->kd_rsp.mscp_cmdref;
1546 	kd->kd_ca.ca_cmddsc = (long)&kd->kd_cmd.mscp_cmdref;
1547 	/* kd->kd_cmd.mscp_sccc.sccc_ctlrflags = 0; */
1548 	/* kd->kd_cmd.mscp_sccc.sccc_version = 0; */
1549 	if (kdbdumpcmd(M_OP_SETCTLRC, k, kd, ui->ui_ctlr))
1550 		return (EFAULT);
1551 	kd->kd_cmd.mscp_unit = ui->ui_slave;
1552 	if (kdbdumpcmd(M_OP_ONLINE, k, kd, ui->ui_ctlr))
1553 		return (EFAULT);
1554 
1555 	/*
1556 	 * Pick up the drive type from the on line end packet;
1557 	 * convert that to a dump area size and a disk offset.
1558 	 * Note that the assembler uses pc-relative addressing
1559 	 * to get at kdbtypes[], no need for PHYS().
1560 	 */
1561 	i = kd->kd_rsp.mscp_onle.onle_drivetype;
1562 	if (i >= NTYPES || kdbtypes[i].ut_name == 0) {
1563 		printf("disk type %d unknown\ndump ");
1564 		return (EINVAL);
1565 	}
1566 	printf("on %s ", kdbtypes[i].ut_name);
1567 
1568 	maxsz = kdbtypes[i].ut_sizes[kdbpart(dev)].nblocks;
1569 	blkoff = kdbtypes[i].ut_sizes[kdbpart(dev)].blkoff;
1570 
1571 	/*
1572 	 * Dump all of physical memory, or as much as will fit in the
1573 	 * space provided.
1574 	 */
1575 	start = 0;
1576 	num = maxfree;
1577 	if (dumplo < 0)
1578 		return (EINVAL);
1579 	if (dumplo + num >= maxsz)
1580 		num = maxsz - dumplo;
1581 	blkoff += dumplo;
1582 
1583 	/*
1584 	 * Write out memory, DBSIZE pages at a time.
1585 	 * N.B.: this code depends on the fact that the sector
1586 	 * size == the page size.
1587 	 */
1588 	while (num > 0) {
1589 		blk = num > DBSIZE ? DBSIZE : num;
1590 		kd->kd_cmd.mscp_unit = ui->ui_slave;
1591 		kd->kd_cmd.mscp_seq.seq_lbn = btop(start) + blkoff;
1592 		kd->kd_cmd.mscp_seq.seq_bytecount = blk << PGSHIFT;
1593 		kd->kd_cmd.mscp_seq.seq_buffer = (long)start | KDB_PHYS;
1594 		if (kdbdumpcmd(M_OP_WRITE, k, kd, ui->ui_ctlr))
1595 			return (EIO);
1596 		start += blk << PGSHIFT;
1597 		num -= blk;
1598 	}
1599 	return (0);		/* made it! */
1600 }
1601 
1602 /*
1603  * Wait for some of the bits in `bits' to come on.  If the error bit
1604  * comes on, or ten seconds pass without response, return true (error).
1605  */
1606 kdbdumpwait(k, bits)
1607 	register struct kdb_regs *k;
1608 	register int bits;
1609 {
1610 	register int timo = todr() + 1000;
1611 
1612 	while ((k->kdb_sa & bits) == 0) {
1613 		if (k->kdb_sa & KDB_ERR) {
1614 			printf("kdb_sa=%b\ndump ", k->kdb_sa, kdbsr_bits);
1615 			return (1);
1616 		}
1617 		if (todr() >= timo) {
1618 			printf("timeout\ndump ");
1619 			return (1);
1620 		}
1621 	}
1622 	return (0);
1623 }
1624 
1625 /*
1626  * Feed a command to the KDB50, wait for its response, and return
1627  * true iff something went wrong.
1628  */
1629 kdbdumpcmd(op, k, kd, ctlr)
1630 	int op;
1631 	register struct kdb_regs *k;
1632 	register struct kdbdumpspace *kd;
1633 	int ctlr;
1634 {
1635 	register int n;
1636 #define mp (&kd->kd_rsp)
1637 
1638 	kd->kd_cmd.mscp_opcode = op;
1639 	kd->kd_cmd.mscp_msglen = MSCP_MSGLEN;
1640 	kd->kd_rsp.mscp_msglen = MSCP_MSGLEN;
1641 	kd->kd_ca.ca_rspdsc |= MSCP_OWN | MSCP_INT;
1642 	kd->kd_ca.ca_cmddsc |= MSCP_OWN | MSCP_INT;
1643 	if (k->kdb_sa & KDB_ERR) {
1644 		printf("kdb_sa=%b\ndump ", k->kdb_sa, kdbsr_bits);
1645 		return (1);
1646 	}
1647 	n = k->kdb_ip;
1648 	n = todr() + 1000;
1649 	for (;;) {
1650 		if (todr() > n) {
1651 			printf("timeout\ndump ");
1652 			return (1);
1653 		}
1654 		if (kd->kd_ca.ca_cmdint)
1655 			kd->kd_ca.ca_cmdint = 0;
1656 		if (kd->kd_ca.ca_rspint == 0)
1657 			continue;
1658 		kd->kd_ca.ca_rspint = 0;
1659 		if (mp->mscp_opcode == (op | M_OP_END))
1660 			break;
1661 		printf("\n");
1662 		switch (MSCP_MSGTYPE(mp->mscp_msgtc)) {
1663 
1664 		case MSCPT_SEQ:
1665 			printf("sequential");
1666 			break;
1667 
1668 		case MSCPT_DATAGRAM:
1669 			mscp_decodeerror("kdb", ctlr, mp);
1670 			printf("datagram");
1671 			break;
1672 
1673 		case MSCPT_CREDITS:
1674 			printf("credits");
1675 			break;
1676 
1677 		case MSCPT_MAINTENANCE:
1678 			printf("maintenance");
1679 			break;
1680 
1681 		default:
1682 			printf("unknown (type 0x%x)",
1683 				MSCP_MSGTYPE(mp->mscp_msgtc));
1684 			break;
1685 		}
1686 		printf(" ignored\ndump ");
1687 		kd->kd_ca.ca_rspdsc |= MSCP_OWN | MSCP_INT;
1688 	}
1689 	if ((mp->mscp_status & M_ST_MASK) != M_ST_SUCCESS) {
1690 		printf("error: op 0x%x => 0x%x status 0x%x\ndump ", op,
1691 			mp->mscp_opcode, mp->mscp_status);
1692 		return (1);
1693 	}
1694 	return (0);
1695 #undef mp
1696 }
1697 
1698 /*
1699  * Return the size of a partition, if known, or -1 if not.
1700  */
1701 kdbsize(dev)
1702 	dev_t dev;
1703 {
1704 	register int unit = kdbunit(dev);
1705 	register struct uba_device *ui;
1706 	register struct size *st;
1707 
1708 	if (unit >= NKRA || (ui = kdbdinfo[unit]) == NULL || ui->ui_alive == 0)
1709 		return (-1);
1710 	st = &kdbtypes[ui->ui_type].ut_sizes[kdbpart(dev)];
1711 	if (st->nblocks == -1) {
1712 		int s = spl5();
1713 
1714 		/*
1715 		 * We need to have the drive on line to find the size
1716 		 * of this particular partition.
1717 		 * IS IT OKAY TO GO TO SLEEP IN THIS ROUTINE?
1718 		 * (If not, better not page on one of these...)
1719 		 */
1720 		if ((ui->ui_flags & UNIT_ONLINE) == 0) {
1721 			if (kdb_bringonline(ui, 0)) {
1722 				splx(s);
1723 				return (-1);
1724 			}
1725 		}
1726 		splx(s);
1727 		if (st->blkoff > ra_dsize[unit])
1728 			return (-1);
1729 		return (ra_dsize[unit] - st->blkoff);
1730 	}
1731 	return (st->nblocks);
1732 }
1733 
1734 #endif NKDB > 0
1735