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