xd.c revision 1.23 1 /* $NetBSD: xd.c,v 1.23 1998/12/13 17:57:19 kleink Exp $ */
2
3 /*
4 *
5 * Copyright (c) 1995 Charles D. Cranor
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Charles D. Cranor.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /*
35 *
36 * x d . c x y l o g i c s 7 5 3 / 7 0 5 3 v m e / s m d d r i v e r
37 *
38 * author: Chuck Cranor <chuck (at) ccrc.wustl.edu>
39 * id: &Id: xd.c,v 1.9 1995/09/25 20:12:44 chuck Exp &
40 * started: 27-Feb-95
41 * references: [1] Xylogics Model 753 User's Manual
42 * part number: 166-753-001, Revision B, May 21, 1988.
43 * "Your Partner For Performance"
44 * [2] other NetBSD disk device drivers
45 *
46 * Special thanks go to Scott E. Campbell of Xylogics, Inc. for taking
47 * the time to answer some of my questions about the 753/7053.
48 *
49 * note: the 753 and the 7053 are programmed the same way, but are
50 * different sizes. the 753 is a 6U VME card, while the 7053 is a 9U
51 * VME card (found in many VME based suns).
52 */
53
54 #undef XDC_DEBUG /* full debug */
55 #define XDC_DIAG /* extra sanity checks */
56 #if defined(DIAGNOSTIC) && !defined(XDC_DIAG)
57 #define XDC_DIAG /* link in with master DIAG option */
58 #endif
59
60 #include <sys/param.h>
61 #include <sys/proc.h>
62 #include <sys/systm.h>
63 #include <sys/kernel.h>
64 #include <sys/file.h>
65 #include <sys/stat.h>
66 #include <sys/ioctl.h>
67 #include <sys/buf.h>
68 #include <sys/uio.h>
69 #include <sys/malloc.h>
70 #include <sys/device.h>
71 #include <sys/disklabel.h>
72 #include <sys/disk.h>
73 #include <sys/syslog.h>
74 #include <sys/dkbad.h>
75 #include <sys/conf.h>
76
77 #include <vm/vm.h>
78 #include <vm/vm_kern.h>
79
80 #include <machine/autoconf.h>
81 #include <machine/dvma.h>
82
83 #include <dev/sun/disklabel.h>
84
85 #include <sun3/dev/xdreg.h>
86 #include <sun3/dev/xdvar.h>
87 #include <sun3/dev/xio.h>
88
89 #include "locators.h"
90
91 /*
92 * Print a complaint when no xd children were specified
93 * in the config file. Better than a link error...
94 *
95 * XXX: Some folks say this driver should be split in two,
96 * but that seems pointless with ONLY one type of child.
97 */
98 #include "xd.h"
99 #if NXD == 0
100 #error "xdc but no xd?"
101 #endif
102
103 /*
104 * macros
105 */
106
107 /*
108 * XDC_TWAIT: add iorq "N" to tail of SC's wait queue
109 */
110 #define XDC_TWAIT(SC, N) { \
111 (SC)->waitq[(SC)->waitend] = (N); \
112 (SC)->waitend = ((SC)->waitend + 1) % XDC_MAXIOPB; \
113 (SC)->nwait++; \
114 }
115
116 /*
117 * XDC_HWAIT: add iorq "N" to head of SC's wait queue
118 */
119 #define XDC_HWAIT(SC, N) { \
120 (SC)->waithead = ((SC)->waithead == 0) ? \
121 (XDC_MAXIOPB - 1) : ((SC)->waithead - 1); \
122 (SC)->waitq[(SC)->waithead] = (N); \
123 (SC)->nwait++; \
124 }
125
126 /*
127 * XDC_GET_WAITER: gets the first request waiting on the waitq
128 * and removes it (so it can be submitted)
129 */
130 #define XDC_GET_WAITER(XDCSC, RQ) { \
131 (RQ) = (XDCSC)->waitq[(XDCSC)->waithead]; \
132 (XDCSC)->waithead = ((XDCSC)->waithead + 1) % XDC_MAXIOPB; \
133 xdcsc->nwait--; \
134 }
135
136 /*
137 * XDC_FREE: add iorq "N" to SC's free list
138 */
139 #define XDC_FREE(SC, N) { \
140 (SC)->freereq[(SC)->nfree++] = (N); \
141 (SC)->reqs[N].mode = 0; \
142 if ((SC)->nfree == 1) wakeup(&(SC)->nfree); \
143 }
144
145
146 /*
147 * XDC_RQALLOC: allocate an iorq off the free list (assume nfree > 0).
148 */
149 #define XDC_RQALLOC(XDCSC) (XDCSC)->freereq[--((XDCSC)->nfree)]
150
151 /*
152 * XDC_GO: start iopb ADDR (DVMA addr in a u_long) on XDC
153 */
154 #define XDC_GO(XDC, ADDR) { \
155 (XDC)->xdc_iopbaddr0 = ((ADDR) & 0xff); \
156 (ADDR) = ((ADDR) >> 8); \
157 (XDC)->xdc_iopbaddr1 = ((ADDR) & 0xff); \
158 (ADDR) = ((ADDR) >> 8); \
159 (XDC)->xdc_iopbaddr2 = ((ADDR) & 0xff); \
160 (ADDR) = ((ADDR) >> 8); \
161 (XDC)->xdc_iopbaddr3 = (ADDR); \
162 (XDC)->xdc_iopbamod = XDC_ADDRMOD; \
163 (XDC)->xdc_csr = XDC_ADDIOPB; /* go! */ \
164 }
165
166 /*
167 * XDC_WAIT: wait for XDC's csr "BITS" to come on in "TIME".
168 * LCV is a counter. If it goes to zero then we timed out.
169 */
170 #define XDC_WAIT(XDC, LCV, TIME, BITS) { \
171 (LCV) = (TIME); \
172 while ((LCV) > 0) { \
173 if ((XDC)->xdc_csr & (BITS)) break; \
174 (LCV) = (LCV) - 1; \
175 DELAY(1); \
176 } \
177 }
178
179 /*
180 * XDC_DONE: don't need IORQ, get error code and free (done after xdc_cmd)
181 */
182 #define XDC_DONE(SC,RQ,ER) { \
183 if ((RQ) == XD_ERR_FAIL) { \
184 (ER) = (RQ); \
185 } else { \
186 if ((SC)->ndone-- == XDC_SUBWAITLIM) \
187 wakeup(&(SC)->ndone); \
188 (ER) = (SC)->reqs[RQ].errno; \
189 XDC_FREE((SC), (RQ)); \
190 } \
191 }
192
193 /*
194 * XDC_ADVANCE: advance iorq's pointers by a number of sectors
195 */
196 #define XDC_ADVANCE(IORQ, N) { \
197 if (N) { \
198 (IORQ)->sectcnt -= (N); \
199 (IORQ)->blockno += (N); \
200 (IORQ)->dbuf += ((N)*XDFM_BPS); \
201 } \
202 }
203
204 /*
205 * note - addresses you can sleep on:
206 * [1] & of xd_softc's "state" (waiting for a chance to attach a drive)
207 * [2] & of xdc_softc's "nfree" (waiting for a free iorq/iopb)
208 * [3] & of xdc_softc's "ndone" (waiting for number of done iorq/iopb's
209 * to drop below XDC_SUBWAITLIM)
210 * [4] & an iorq (waiting for an XD_SUB_WAIT iorq to finish)
211 */
212
213
214 /*
215 * function prototypes
216 * "xdc_*" functions are internal, all others are external interfaces
217 */
218
219 /* internals */
220 int xdc_cmd __P((struct xdc_softc *, int, int, int, int, int, char *, int));
221 char *xdc_e2str __P((int));
222 int xdc_error __P((struct xdc_softc *, struct xd_iorq *,
223 struct xd_iopb *, int, int));
224 int xdc_ioctlcmd __P((struct xd_softc *, dev_t dev, struct xd_iocmd *));
225 void xdc_perror __P((struct xd_iorq *, struct xd_iopb *, int));
226 int xdc_piodriver __P((struct xdc_softc *, int, int));
227 int xdc_remove_iorq __P((struct xdc_softc *));
228 int xdc_reset __P((struct xdc_softc *, int, int, int, struct xd_softc *));
229 inline void xdc_rqinit __P((struct xd_iorq *, struct xdc_softc *,
230 struct xd_softc *, int, u_long, int,
231 caddr_t, struct buf *));
232 void xdc_rqtopb __P((struct xd_iorq *, struct xd_iopb *, int, int));
233 void xdc_start __P((struct xdc_softc *, int));
234 int xdc_startbuf __P((struct xdc_softc *, struct xd_softc *, struct buf *));
235 int xdc_submit_iorq __P((struct xdc_softc *, int, int));
236 void xdc_tick __P((void *));
237 void xdc_xdreset __P((struct xdc_softc *, struct xd_softc *));
238
239 /* machine interrupt hook */
240 int xdcintr __P((void *));
241
242 /* bdevsw, cdevsw */
243 bdev_decl(xd);
244 cdev_decl(xd);
245
246 /* autoconf */
247 static int xdcmatch __P((struct device *, struct cfdata *, void *));
248 static void xdcattach __P((struct device *, struct device *, void *));
249 static int xdc_print __P((void *, const char *name));
250
251 static int xdmatch __P((struct device *, struct cfdata *, void *));
252 static void xdattach __P((struct device *, struct device *, void *));
253 static void xd_init __P((struct xd_softc *));
254
255 static void xddummystrat __P((struct buf *));
256 int xdgetdisklabel __P((struct xd_softc *, void *));
257
258 /*
259 * cfattach's: device driver interface to autoconfig
260 */
261
262 struct cfattach xdc_ca = {
263 sizeof(struct xdc_softc), xdcmatch, xdcattach
264 };
265
266 struct cfattach xd_ca = {
267 sizeof(struct xd_softc), xdmatch, xdattach
268 };
269
270 extern struct cfdriver xd_cd;
271
272 struct xdc_attach_args { /* this is the "aux" args to xdattach */
273 int driveno; /* unit number */
274 char *dvmabuf; /* scratch buffer for reading disk label */
275 int fullmode; /* submit mode */
276 int booting; /* are we booting or not? */
277 };
278
279 /*
280 * dkdriver
281 */
282
283 struct dkdriver xddkdriver = {xdstrategy};
284
285 /*
286 * start: disk label fix code (XXX)
287 */
288
289 static void *xd_labeldata;
290
291 static void
292 xddummystrat(bp)
293 struct buf *bp;
294 {
295 if (bp->b_bcount != XDFM_BPS)
296 panic("xddummystrat");
297 bcopy(xd_labeldata, bp->b_un.b_addr, XDFM_BPS);
298 bp->b_flags |= B_DONE;
299 bp->b_flags &= ~B_BUSY;
300 }
301
302 int
303 xdgetdisklabel(xd, b)
304 struct xd_softc *xd;
305 void *b;
306 {
307 char *err;
308 struct sun_disklabel *sdl;
309
310 /* We already have the label data in `b'; setup for dummy strategy */
311 xd_labeldata = b;
312
313 /* Required parameter for readdisklabel() */
314 xd->sc_dk.dk_label->d_secsize = XDFM_BPS;
315
316 err = readdisklabel(MAKEDISKDEV(0, xd->sc_dev.dv_unit, RAW_PART),
317 xddummystrat,
318 xd->sc_dk.dk_label, xd->sc_dk.dk_cpulabel);
319 if (err) {
320 printf("%s: %s\n", xd->sc_dev.dv_xname, err);
321 return(XD_ERR_FAIL);
322 }
323
324 /* Ok, we have the label; fill in `pcyl' if there's SunOS magic */
325 sdl = (struct sun_disklabel *)xd->sc_dk.dk_cpulabel->cd_block;
326 if (sdl->sl_magic == SUN_DKMAGIC)
327 xd->pcyl = sdl->sl_pcyl;
328 else {
329 printf("%s: WARNING: no `pcyl' in disk label.\n",
330 xd->sc_dev.dv_xname);
331 xd->pcyl = xd->sc_dk.dk_label->d_ncylinders +
332 xd->sc_dk.dk_label->d_acylinders;
333 printf("%s: WARNING: guessing pcyl=%d (ncyl+acyl)\n",
334 xd->sc_dev.dv_xname, xd->pcyl);
335 }
336
337 xd->ncyl = xd->sc_dk.dk_label->d_ncylinders;
338 xd->acyl = xd->sc_dk.dk_label->d_acylinders;
339 xd->nhead = xd->sc_dk.dk_label->d_ntracks;
340 xd->nsect = xd->sc_dk.dk_label->d_nsectors;
341 xd->sectpercyl = xd->nhead * xd->nsect;
342 xd->sc_dk.dk_label->d_secsize = XDFM_BPS; /* not handled by
343 * sun->bsd */
344 return(XD_ERR_AOK);
345 }
346
347 /*
348 * end: disk label fix code (XXX)
349 */
350
351 /*
352 * a u t o c o n f i g f u n c t i o n s
353 */
354
355 /*
356 * xdcmatch: determine if xdc is present or not. we do a
357 * soft reset to detect the xdc.
358 */
359
360 int xdcmatch(parent, cf, aux)
361 struct device *parent;
362 struct cfdata *cf;
363 void *aux;
364 {
365 struct confargs *ca = aux;
366
367 /* No default VME address. */
368 if (ca->ca_paddr == -1)
369 return (0);
370
371 /* Make sure something is there... */
372 if (bus_peek(ca->ca_bustype, ca->ca_paddr + 11, 1) == -1)
373 return (0);
374
375 /* Default interrupt priority. */
376 if (ca->ca_intpri == -1)
377 ca->ca_intpri = 2;
378
379 return (1);
380 }
381
382 /*
383 * xdcattach: attach controller
384 */
385 void
386 xdcattach(parent, self, aux)
387 struct device *parent, *self;
388 void *aux;
389 {
390 struct xdc_softc *xdc = (void *) self;
391 struct confargs *ca = aux;
392 struct xdc_attach_args xa;
393 int lcv, rqno, err;
394 struct xd_iopb_ctrl *ctl;
395
396 /* get addressing and intr level stuff from autoconfig and load it
397 * into our xdc_softc. */
398
399 xdc->xdc = (struct xdc *)
400 bus_mapin(ca->ca_bustype, ca->ca_paddr, sizeof(struct xdc));
401 xdc->bustype = ca->ca_bustype;
402 xdc->ipl = ca->ca_intpri;
403 xdc->vector = ca->ca_intvec;
404
405 for (lcv = 0; lcv < XDC_MAXDEV; lcv++)
406 xdc->sc_drives[lcv] = (struct xd_softc *) 0;
407
408 /* allocate and zero buffers
409 *
410 * note: we simplify the code by allocating the max number of iopbs and
411 * iorq's up front. thus, we avoid linked lists and the costs
412 * associated with them in exchange for wasting a little memory. */
413
414 xdc->iopbase = (struct xd_iopb *)
415 dvma_malloc(XDC_MAXIOPB * sizeof(struct xd_iopb)); /* KVA */
416 bzero(xdc->iopbase, XDC_MAXIOPB * sizeof(struct xd_iopb));
417 xdc->dvmaiopb = (struct xd_iopb *)
418 dvma_kvtopa(xdc->iopbase, xdc->bustype);
419 xdc->reqs = (struct xd_iorq *)
420 malloc(XDC_MAXIOPB * sizeof(struct xd_iorq), M_DEVBUF, M_NOWAIT);
421 if (xdc->reqs == NULL)
422 panic("xdc malloc");
423 bzero(xdc->reqs, XDC_MAXIOPB * sizeof(struct xd_iorq));
424
425 /* init free list, iorq to iopb pointers, and non-zero fields in the
426 * iopb which never change. */
427
428 for (lcv = 0; lcv < XDC_MAXIOPB; lcv++) {
429 xdc->reqs[lcv].iopb = &xdc->iopbase[lcv];
430 xdc->freereq[lcv] = lcv;
431 xdc->iopbase[lcv].fixd = 1; /* always the same */
432 xdc->iopbase[lcv].naddrmod = XDC_ADDRMOD; /* always the same */
433 xdc->iopbase[lcv].intr_vec = xdc->vector; /* always the same */
434 }
435 xdc->nfree = XDC_MAXIOPB;
436 xdc->nrun = 0;
437 xdc->waithead = xdc->waitend = xdc->nwait = 0;
438 xdc->ndone = 0;
439
440 /* init queue of waiting bufs */
441
442 xdc->sc_wq.b_active = 0;
443 xdc->sc_wq.b_actf = 0;
444 xdc->sc_wq.b_actb = &xdc->sc_wq.b_actf;
445
446 /*
447 * section 7 of the manual tells us how to init the controller:
448 * - read controller parameters (6/0)
449 * - write controller parameters (5/0)
450 */
451
452 /* read controller parameters and insure we have a 753/7053 */
453
454 rqno = xdc_cmd(xdc, XDCMD_RDP, XDFUN_CTL, 0, 0, 0, 0, XD_SUB_POLL);
455 if (rqno == XD_ERR_FAIL) {
456 printf(": couldn't read controller params\n");
457 return; /* shouldn't ever happen */
458 }
459 ctl = (struct xd_iopb_ctrl *) & xdc->iopbase[rqno];
460 if (ctl->ctype != XDCT_753) {
461 if (xdc->reqs[rqno].errno)
462 printf(": %s: ", xdc_e2str(xdc->reqs[rqno].errno));
463 printf(": doesn't identify as a 753/7053\n");
464 XDC_DONE(xdc, rqno, err);
465 return;
466 }
467 printf(": Xylogics 753/7053, PROM=0x%x.%02x.%02x\n",
468 ctl->eprom_partno, ctl->eprom_lvl, ctl->eprom_rev);
469 XDC_DONE(xdc, rqno, err);
470
471 /* now write controller parameters (xdc_cmd sets all params for us) */
472
473 rqno = xdc_cmd(xdc, XDCMD_WRP, XDFUN_CTL, 0, 0, 0, 0, XD_SUB_POLL);
474 XDC_DONE(xdc, rqno, err);
475 if (err) {
476 printf("%s: controller config error: %s\n",
477 xdc->sc_dev.dv_xname, xdc_e2str(err));
478 return;
479 }
480
481 /* link in interrupt with higher level software */
482 isr_add_vectored(xdcintr, (void *)xdc,
483 ca->ca_intpri, ca->ca_intvec);
484 evcnt_attach(&xdc->sc_dev, "intr", &xdc->sc_intrcnt);
485
486 /* now we must look for disks using autoconfig */
487 xa.booting = 1;
488 for (xa.driveno = 0; xa.driveno < XDC_MAXDEV; xa.driveno++)
489 (void) config_found(self, (void *) &xa, xdc_print);
490
491 /* start the watchdog clock */
492 timeout(xdc_tick, xdc, XDC_TICKCNT);
493 }
494
495 int
496 xdc_print(aux, name)
497 void *aux;
498 const char *name;
499 {
500 struct xdc_attach_args *xa = aux;
501
502 if (name != NULL)
503 printf("%s: ", name);
504
505 if (xa->driveno != -1)
506 printf(" drive %d", xa->driveno);
507
508 return UNCONF;
509 }
510
511 /*
512 * xdmatch: probe for disk.
513 *
514 * note: we almost always say disk is present. this allows us to
515 * spin up and configure a disk after the system is booted (we can
516 * call xdattach!). Also, wire down the relationship between the
517 * xd* and xdc* devices, to simplify boot device identification.
518 */
519 int
520 xdmatch(parent, cf, aux)
521 struct device *parent;
522 struct cfdata *cf;
523 void *aux;
524 {
525 struct xdc_attach_args *xa = aux;
526 int xd_unit;
527
528 /* Match only on the "wired-down" controller+disk. */
529 xd_unit = parent->dv_unit * 2 + xa->driveno;
530 if (cf->cf_unit != xd_unit)
531 return (0);
532
533 return (1);
534 }
535
536 /*
537 * xdattach: attach a disk.
538 */
539 void
540 xdattach(parent, self, aux)
541 struct device *parent, *self;
542 void *aux;
543
544 {
545 struct xd_softc *xd = (void *) self;
546 struct xdc_softc *xdc = (void *) parent;
547 struct xdc_attach_args *xa = aux;
548
549 printf("\n");
550
551 /*
552 * Always re-initialize the disk structure. We want statistics
553 * to start with a clean slate.
554 */
555 bzero(&xd->sc_dk, sizeof(xd->sc_dk));
556 xd->sc_dk.dk_driver = &xddkdriver;
557 xd->sc_dk.dk_name = xd->sc_dev.dv_xname;
558
559 xd->state = XD_DRIVE_UNKNOWN; /* to start */
560 xd->flags = 0;
561 xd->parent = xdc;
562
563 xd->xd_drive = xa->driveno;
564 xdc->sc_drives[xa->driveno] = xd;
565
566 /* Do init work common to attach and open. */
567 xd_init(xd);
568 dk_establish(&xd->sc_dk, &xd->sc_dev);
569 }
570
571 /*
572 * end of autoconfig functions
573 */
574
575 /*
576 * Initialize a disk. This can be called from both autoconf and
577 * also from xdopen/xdstrategy.
578 */
579 static void
580 xd_init(xd)
581 struct xd_softc *xd;
582 {
583 struct xdc_softc *xdc;
584 struct dkbad *dkb;
585 struct xd_iopb_drive *driopb;
586 void *dvmabuf;
587 int rqno, err, spt, mb, blk, lcv, fullmode, newstate;
588 extern int cold;
589
590 xdc = xd->parent;
591 xd->state = XD_DRIVE_ATTACHING;
592 newstate = XD_DRIVE_UNKNOWN;
593 fullmode = (cold) ? XD_SUB_POLL : XD_SUB_WAIT;
594 dvmabuf = dvma_malloc(XDFM_BPS);
595
596 /* first try and reset the drive */
597 rqno = xdc_cmd(xdc, XDCMD_RST, 0, xd->xd_drive, 0, 0, 0, fullmode);
598 XDC_DONE(xdc, rqno, err);
599 if (err == XD_ERR_NRDY) {
600 printf("%s: drive %d: off-line\n",
601 xd->sc_dev.dv_xname, xd->xd_drive);
602 goto done;
603 }
604 if (err) {
605 printf("%s: ERROR 0x%02x (%s)\n",
606 xd->sc_dev.dv_xname, err, xdc_e2str(err));
607 goto done;
608 }
609 printf("%s: drive %d ready\n",
610 xd->sc_dev.dv_xname, xd->xd_drive);
611
612 /* now set format parameters */
613
614 rqno = xdc_cmd(xdc, XDCMD_WRP, XDFUN_FMT, xd->xd_drive,
615 0, 0, 0, fullmode);
616 XDC_DONE(xdc, rqno, err);
617 if (err) {
618 printf("%s: write format parameters failed: %s\n",
619 xd->sc_dev.dv_xname, xdc_e2str(err));
620 goto done;
621 }
622
623 /* get drive parameters */
624 spt = 0;
625 rqno = xdc_cmd(xdc, XDCMD_RDP, XDFUN_DRV, xd->xd_drive,
626 0, 0, 0, fullmode);
627 if (rqno != XD_ERR_FAIL) {
628 driopb = (struct xd_iopb_drive *) & xdc->iopbase[rqno];
629 spt = driopb->sectpertrk;
630 }
631 XDC_DONE(xdc, rqno, err);
632 if (err) {
633 printf("%s: read drive parameters failed: %s\n",
634 xd->sc_dev.dv_xname, xdc_e2str(err));
635 goto done;
636 }
637
638 /*
639 * now set drive parameters (to semi-bogus values) so we can read the
640 * disk label.
641 */
642 xd->pcyl = xd->ncyl = 1;
643 xd->acyl = 0;
644 xd->nhead = 1;
645 xd->nsect = 1;
646 xd->sectpercyl = 1;
647 for (lcv = 0; lcv < 126; lcv++) /* init empty bad144 table */
648 xd->dkb.bt_bad[lcv].bt_cyl =
649 xd->dkb.bt_bad[lcv].bt_trksec = 0xffff;
650 rqno = xdc_cmd(xdc, XDCMD_WRP, XDFUN_DRV, xd->xd_drive,
651 0, 0, 0, fullmode);
652 XDC_DONE(xdc, rqno, err);
653 if (err) {
654 printf("%s: write drive parameters failed: %s\n",
655 xd->sc_dev.dv_xname, xdc_e2str(err));
656 goto done;
657 }
658
659 /* read disk label */
660 rqno = xdc_cmd(xdc, XDCMD_RD, 0, xd->xd_drive,
661 0, 1, dvmabuf, fullmode);
662 XDC_DONE(xdc, rqno, err);
663 if (err) {
664 printf("%s: reading disk label failed: %s\n",
665 xd->sc_dev.dv_xname, xdc_e2str(err));
666 goto done;
667 }
668 newstate = XD_DRIVE_NOLABEL;
669
670 xd->hw_spt = spt;
671 /* Attach the disk: must be before getdisklabel to malloc label */
672 disk_attach(&xd->sc_dk);
673
674 if (xdgetdisklabel(xd, dvmabuf) != XD_ERR_AOK)
675 goto done;
676
677 /* inform the user of what is up */
678 printf("%s: <%s>, pcyl %d, hw_spt %d\n",
679 xd->sc_dev.dv_xname,
680 (char *)dvmabuf, xd->pcyl, spt);
681 mb = xd->ncyl * (xd->nhead * xd->nsect) / (1048576 / XDFM_BPS);
682 printf("%s: %dMB, %d cyl, %d head, %d sec\n",
683 xd->sc_dev.dv_xname, mb,
684 xd->ncyl, xd->nhead, xd->nsect);
685
686 /* now set the real drive parameters! */
687 rqno = xdc_cmd(xdc, XDCMD_WRP, XDFUN_DRV, xd->xd_drive,
688 0, 0, 0, fullmode);
689 XDC_DONE(xdc, rqno, err);
690 if (err) {
691 printf("%s: write real drive parameters failed: %s\n",
692 xd->sc_dev.dv_xname, xdc_e2str(err));
693 goto done;
694 }
695 newstate = XD_DRIVE_ONLINE;
696
697 /*
698 * read bad144 table. this table resides on the first sector of the
699 * last track of the disk (i.e. second cyl of "acyl" area).
700 */
701 blk = (xd->ncyl + xd->acyl - 1) * (xd->nhead * xd->nsect) + /* last cyl */
702 (xd->nhead - 1) * xd->nsect; /* last head */
703 rqno = xdc_cmd(xdc, XDCMD_RD, 0, xd->xd_drive,
704 blk, 1, dvmabuf, fullmode);
705 XDC_DONE(xdc, rqno, err);
706 if (err) {
707 printf("%s: reading bad144 failed: %s\n",
708 xd->sc_dev.dv_xname, xdc_e2str(err));
709 goto done;
710 }
711
712 /* check dkbad for sanity */
713 dkb = (struct dkbad *) dvmabuf;
714 for (lcv = 0; lcv < 126; lcv++) {
715 if ((dkb->bt_bad[lcv].bt_cyl == 0xffff ||
716 dkb->bt_bad[lcv].bt_cyl == 0) &&
717 dkb->bt_bad[lcv].bt_trksec == 0xffff)
718 continue; /* blank */
719 if (dkb->bt_bad[lcv].bt_cyl >= xd->ncyl)
720 break;
721 if ((dkb->bt_bad[lcv].bt_trksec >> 8) >= xd->nhead)
722 break;
723 if ((dkb->bt_bad[lcv].bt_trksec & 0xff) >= xd->nsect)
724 break;
725 }
726 if (lcv != 126) {
727 printf("%s: warning: invalid bad144 sector!\n",
728 xd->sc_dev.dv_xname);
729 } else {
730 bcopy(dvmabuf, &xd->dkb, XDFM_BPS);
731 }
732
733 done:
734 xd->state = newstate;
735 dvma_free(dvmabuf, XDFM_BPS);
736 }
737
738 /*
739 * { b , c } d e v s w f u n c t i o n s
740 */
741
742 /*
743 * xdclose: close device
744 */
745 int
746 xdclose(dev, flag, fmt, p)
747 dev_t dev;
748 int flag, fmt;
749 struct proc *p;
750 {
751 struct xd_softc *xd = xd_cd.cd_devs[DISKUNIT(dev)];
752 int part = DISKPART(dev);
753
754 /* clear mask bits */
755
756 switch (fmt) {
757 case S_IFCHR:
758 xd->sc_dk.dk_copenmask &= ~(1 << part);
759 break;
760 case S_IFBLK:
761 xd->sc_dk.dk_bopenmask &= ~(1 << part);
762 break;
763 }
764 xd->sc_dk.dk_openmask = xd->sc_dk.dk_copenmask | xd->sc_dk.dk_bopenmask;
765
766 return 0;
767 }
768
769 /*
770 * xddump: crash dump system
771 */
772 int
773 xddump(dev, blkno, va, sz)
774 dev_t dev;
775 daddr_t blkno;
776 caddr_t va;
777 size_t sz;
778 {
779 int unit, part;
780 struct xd_softc *xd;
781
782 unit = DISKUNIT(dev);
783 if (unit >= xd_cd.cd_ndevs)
784 return ENXIO;
785 part = DISKPART(dev);
786
787 xd = xd_cd.cd_devs[unit];
788
789 printf("%s%c: crash dump not supported (yet)\n",
790 xd->sc_dev.dv_xname, 'a' + part);
791
792 return ENXIO;
793
794 /* outline: globals: "dumplo" == sector number of partition to start
795 * dump at (convert to physical sector with partition table)
796 * "dumpsize" == size of dump in clicks "physmem" == size of physical
797 * memory (clicks, ctob() to get bytes) (normal case: dumpsize ==
798 * physmem)
799 *
800 * dump a copy of physical memory to the dump device starting at sector
801 * "dumplo" in the swap partition (make sure > 0). map in pages as
802 * we go. use polled I/O.
803 *
804 * XXX how to handle NON_CONTIG?
805 */
806 }
807
808 /*
809 * xdioctl: ioctls on XD drives. based on ioctl's of other netbsd disks.
810 */
811 int
812 xdioctl(dev, command, addr, flag, p)
813 dev_t dev;
814 u_long command;
815 caddr_t addr;
816 int flag;
817 struct proc *p;
818
819 {
820 struct xd_softc *xd;
821 struct xd_iocmd *xio;
822 int error, s, unit;
823
824 unit = DISKUNIT(dev);
825
826 if (unit >= xd_cd.cd_ndevs || (xd = xd_cd.cd_devs[unit]) == NULL)
827 return (ENXIO);
828
829 /* switch on ioctl type */
830
831 switch (command) {
832 case DIOCSBAD: /* set bad144 info */
833 if ((flag & FWRITE) == 0)
834 return EBADF;
835 s = splbio();
836 bcopy(addr, &xd->dkb, sizeof(xd->dkb));
837 splx(s);
838 return 0;
839
840 case DIOCGDINFO: /* get disk label */
841 bcopy(xd->sc_dk.dk_label, addr, sizeof(struct disklabel));
842 return 0;
843
844 case DIOCGPART: /* get partition info */
845 ((struct partinfo *) addr)->disklab = xd->sc_dk.dk_label;
846 ((struct partinfo *) addr)->part =
847 &xd->sc_dk.dk_label->d_partitions[DISKPART(dev)];
848 return 0;
849
850 case DIOCSDINFO: /* set disk label */
851 if ((flag & FWRITE) == 0)
852 return EBADF;
853 error = setdisklabel(xd->sc_dk.dk_label,
854 (struct disklabel *) addr, /* xd->sc_dk.dk_openmask : */ 0,
855 xd->sc_dk.dk_cpulabel);
856 if (error == 0) {
857 if (xd->state == XD_DRIVE_NOLABEL)
858 xd->state = XD_DRIVE_ONLINE;
859 }
860 return error;
861
862 case DIOCWLABEL: /* change write status of disk label */
863 if ((flag & FWRITE) == 0)
864 return EBADF;
865 if (*(int *) addr)
866 xd->flags |= XD_WLABEL;
867 else
868 xd->flags &= ~XD_WLABEL;
869 return 0;
870
871 case DIOCWDINFO: /* write disk label */
872 if ((flag & FWRITE) == 0)
873 return EBADF;
874 error = setdisklabel(xd->sc_dk.dk_label,
875 (struct disklabel *) addr, /* xd->sc_dk.dk_openmask : */ 0,
876 xd->sc_dk.dk_cpulabel);
877 if (error == 0) {
878 if (xd->state == XD_DRIVE_NOLABEL)
879 xd->state = XD_DRIVE_ONLINE;
880
881 /* Simulate opening partition 0 so write succeeds. */
882 xd->sc_dk.dk_openmask |= (1 << 0);
883 error = writedisklabel(MAKEDISKDEV(major(dev), DISKUNIT(dev), RAW_PART),
884 xdstrategy, xd->sc_dk.dk_label,
885 xd->sc_dk.dk_cpulabel);
886 xd->sc_dk.dk_openmask =
887 xd->sc_dk.dk_copenmask | xd->sc_dk.dk_bopenmask;
888 }
889 return error;
890
891 case DIOSXDCMD:
892 xio = (struct xd_iocmd *) addr;
893 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
894 return (error);
895 return (xdc_ioctlcmd(xd, dev, xio));
896
897 default:
898 return ENOTTY;
899 }
900 }
901
902 /*
903 * xdopen: open drive
904 */
905 int
906 xdopen(dev, flag, fmt, p)
907 dev_t dev;
908 int flag, fmt;
909 struct proc *p;
910 {
911 int err, unit, part, s;
912 struct xd_softc *xd;
913
914 /* first, could it be a valid target? */
915 unit = DISKUNIT(dev);
916 if (unit >= xd_cd.cd_ndevs || (xd = xd_cd.cd_devs[unit]) == NULL)
917 return (ENXIO);
918 part = DISKPART(dev);
919 err = 0;
920
921 /*
922 * If some other processing is doing init, sleep.
923 */
924 s = splbio();
925 while (xd->state == XD_DRIVE_ATTACHING) {
926 if (tsleep(&xd->state, PRIBIO, "xdopen", 0)) {
927 err = EINTR;
928 goto done;
929 }
930 }
931 /* Do we need to init the drive? */
932 if (xd->state == XD_DRIVE_UNKNOWN) {
933 xd_init(xd);
934 wakeup(&xd->state);
935 }
936 /* Was the init successful? */
937 if (xd->state == XD_DRIVE_UNKNOWN) {
938 err = EIO;
939 goto done;
940 }
941
942 /* check for partition */
943 if (part != RAW_PART &&
944 (part >= xd->sc_dk.dk_label->d_npartitions ||
945 xd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
946 err = ENXIO;
947 goto done;
948 }
949
950 /* set open masks */
951 switch (fmt) {
952 case S_IFCHR:
953 xd->sc_dk.dk_copenmask |= (1 << part);
954 break;
955 case S_IFBLK:
956 xd->sc_dk.dk_bopenmask |= (1 << part);
957 break;
958 }
959 xd->sc_dk.dk_openmask = xd->sc_dk.dk_copenmask | xd->sc_dk.dk_bopenmask;
960
961 done:
962 splx(s);
963 return (err);
964 }
965
966 int
967 xdread(dev, uio, flags)
968 dev_t dev;
969 struct uio *uio;
970 int flags;
971 {
972
973 return (physio(xdstrategy, NULL, dev, B_READ, minphys, uio));
974 }
975
976 int
977 xdwrite(dev, uio, flags)
978 dev_t dev;
979 struct uio *uio;
980 int flags;
981 {
982
983 return (physio(xdstrategy, NULL, dev, B_WRITE, minphys, uio));
984 }
985
986
987 /*
988 * xdsize: return size of a partition for a dump
989 */
990 int
991 xdsize(dev)
992 dev_t dev;
993
994 {
995 struct xd_softc *xdsc;
996 int unit, part, size, omask;
997
998 /* valid unit? */
999 unit = DISKUNIT(dev);
1000 if (unit >= xd_cd.cd_ndevs || (xdsc = xd_cd.cd_devs[unit]) == NULL)
1001 return (-1);
1002
1003 part = DISKPART(dev);
1004 omask = xdsc->sc_dk.dk_openmask & (1 << part);
1005
1006 if (omask == 0 && xdopen(dev, 0, S_IFBLK, NULL) != 0)
1007 return (-1);
1008
1009 /* do it */
1010 if (xdsc->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
1011 size = -1; /* only give valid size for swap partitions */
1012 else
1013 size = xdsc->sc_dk.dk_label->d_partitions[part].p_size *
1014 (xdsc->sc_dk.dk_label->d_secsize / DEV_BSIZE);
1015 if (omask == 0 && xdclose(dev, 0, S_IFBLK, NULL) != 0)
1016 return (-1);
1017 return (size);
1018 }
1019
1020 /*
1021 * xdstrategy: buffering system interface to xd.
1022 */
1023 void
1024 xdstrategy(bp)
1025 struct buf *bp;
1026
1027 {
1028 struct xd_softc *xd;
1029 struct xdc_softc *parent;
1030 struct buf *wq;
1031 int s, unit;
1032
1033 unit = DISKUNIT(bp->b_dev);
1034
1035 /* check for live device */
1036
1037 if (unit >= xd_cd.cd_ndevs || (xd = xd_cd.cd_devs[unit]) == 0 ||
1038 bp->b_blkno < 0 ||
1039 (bp->b_bcount % xd->sc_dk.dk_label->d_secsize) != 0) {
1040 bp->b_error = EINVAL;
1041 goto bad;
1042 }
1043
1044 /* There should always be an open first. */
1045 if (xd->state == XD_DRIVE_UNKNOWN) {
1046 bp->b_error = EIO;
1047 goto bad;
1048 }
1049
1050 if (xd->state != XD_DRIVE_ONLINE && DISKPART(bp->b_dev) != RAW_PART) {
1051 /* no I/O to unlabeled disks, unless raw partition */
1052 bp->b_error = EIO;
1053 goto bad;
1054 }
1055 /* short circuit zero length request */
1056
1057 if (bp->b_bcount == 0)
1058 goto done;
1059
1060 /* check bounds with label (disksubr.c). Determine the size of the
1061 * transfer, and make sure it is within the boundaries of the
1062 * partition. Adjust transfer if needed, and signal errors or early
1063 * completion. */
1064
1065 if (bounds_check_with_label(bp, xd->sc_dk.dk_label,
1066 (xd->flags & XD_WLABEL) != 0) <= 0)
1067 goto done;
1068
1069 /*
1070 * now we know we have a valid buf structure that we need to do I/O
1071 * on.
1072 *
1073 * note that we don't disksort because the controller has a sorting
1074 * algorithm built into the hardware.
1075 */
1076
1077 s = splbio(); /* protect the queues */
1078
1079 /* first, give jobs in front of us a chance */
1080 parent = xd->parent;
1081 while (parent->nfree > 0 && parent->sc_wq.b_actf)
1082 if (xdc_startbuf(parent, NULL, NULL) != XD_ERR_AOK)
1083 break;
1084
1085 /*
1086 * if there are no free iorq's, then we just queue and return. the
1087 * buffs will get picked up later by xdcintr().
1088 */
1089 if (parent->nfree == 0) {
1090 wq = &xd->parent->sc_wq;
1091 bp->b_actf = 0;
1092 bp->b_actb = wq->b_actb;
1093 *wq->b_actb = bp;
1094 wq->b_actb = &bp->b_actf;
1095 splx(s);
1096 return;
1097 }
1098
1099 /* now we have free iopb's and we are at splbio... start 'em up */
1100 if (xdc_startbuf(parent, xd, bp) != XD_ERR_AOK) {
1101 return;
1102 }
1103
1104 /* done! */
1105
1106 splx(s);
1107 return;
1108
1109 bad: /* tells upper layers we have an error */
1110 bp->b_flags |= B_ERROR;
1111 done: /* tells upper layers we are done with this
1112 * buf */
1113 bp->b_resid = bp->b_bcount;
1114 biodone(bp);
1115 }
1116 /*
1117 * end of {b,c}devsw functions
1118 */
1119
1120 /*
1121 * i n t e r r u p t f u n c t i o n
1122 *
1123 * xdcintr: hardware interrupt.
1124 */
1125 int
1126 xdcintr(v)
1127 void *v;
1128
1129 {
1130 struct xdc_softc *xdcsc = v;
1131
1132 /* kick the event counter */
1133 xdcsc->sc_intrcnt.ev_count++;
1134
1135 /* remove as many done IOPBs as possible */
1136 xdc_remove_iorq(xdcsc);
1137
1138 /* start any iorq's already waiting */
1139 xdc_start(xdcsc, XDC_MAXIOPB);
1140
1141 /* fill up any remaining iorq's with queue'd buffers */
1142 while (xdcsc->nfree > 0 && xdcsc->sc_wq.b_actf)
1143 if (xdc_startbuf(xdcsc, NULL, NULL) != XD_ERR_AOK)
1144 break;
1145
1146 return (1);
1147 }
1148 /*
1149 * end of interrupt function
1150 */
1151
1152 /*
1153 * i n t e r n a l f u n c t i o n s
1154 */
1155
1156 /*
1157 * xdc_rqinit: fill out the fields of an I/O request
1158 */
1159
1160 inline void
1161 xdc_rqinit(rq, xdc, xd, md, blk, cnt, db, bp)
1162 struct xd_iorq *rq;
1163 struct xdc_softc *xdc;
1164 struct xd_softc *xd;
1165 int md;
1166 u_long blk;
1167 int cnt;
1168 caddr_t db;
1169 struct buf *bp;
1170 {
1171 rq->xdc = xdc;
1172 rq->xd = xd;
1173 rq->ttl = XDC_MAXTTL + 10;
1174 rq->mode = md;
1175 rq->tries = rq->errno = rq->lasterror = 0;
1176 rq->blockno = blk;
1177 rq->sectcnt = cnt;
1178 rq->dbuf = rq->dbufbase = db;
1179 rq->buf = bp;
1180 }
1181
1182 /*
1183 * xdc_rqtopb: load up an IOPB based on an iorq
1184 */
1185 void
1186 xdc_rqtopb(iorq, iopb, cmd, subfun)
1187 struct xd_iorq *iorq;
1188 struct xd_iopb *iopb;
1189 int cmd, subfun;
1190
1191 {
1192 u_long block, dp;
1193
1194 /* standard stuff */
1195
1196 iopb->errs = iopb->done = 0;
1197 iopb->comm = cmd;
1198 iopb->errno = iopb->status = 0;
1199 iopb->subfun = subfun;
1200 if (iorq->xd)
1201 iopb->unit = iorq->xd->xd_drive;
1202 else
1203 iopb->unit = 0;
1204
1205 /* check for alternate IOPB format */
1206
1207 if (cmd == XDCMD_WRP) {
1208 switch (subfun) {
1209 case XDFUN_CTL:{
1210 struct xd_iopb_ctrl *ctrl =
1211 (struct xd_iopb_ctrl *) iopb;
1212 iopb->lll = 0;
1213 iopb->intl = (XD_STATE(iorq->mode) == XD_SUB_POLL)
1214 ? 0
1215 : iorq->xdc->ipl;
1216 ctrl->param_a = XDPA_TMOD | XDPA_DACF;
1217 ctrl->param_b = XDPB_ROR | XDPB_TDT_3_2USEC;
1218 ctrl->param_c = XDPC_OVS | XDPC_COP | XDPC_ASR |
1219 XDPC_RBC | XDPC_ECC2;
1220 ctrl->throttle = XDC_THROTTLE;
1221 #ifdef sparc
1222 if (CPU_ISSUN4 && cpuinfo.cpu_type == CPUTYP_4_300)
1223 ctrl->delay = XDC_DELAY_4_300;
1224 else
1225 ctrl->delay = XDC_DELAY_SPARC;
1226 #endif
1227 #ifdef sun3
1228 ctrl->delay = XDC_DELAY_SUN3;
1229 #endif
1230 break;
1231 }
1232 case XDFUN_DRV:{
1233 struct xd_iopb_drive *drv =
1234 (struct xd_iopb_drive *)iopb;
1235 /* we assume that the disk label has the right
1236 * info */
1237 if (XD_STATE(iorq->mode) == XD_SUB_POLL)
1238 drv->dparam_ipl = (XDC_DPARAM << 3);
1239 else
1240 drv->dparam_ipl = (XDC_DPARAM << 3) |
1241 iorq->xdc->ipl;
1242 drv->maxsect = iorq->xd->nsect - 1;
1243 drv->maxsector = drv->maxsect;
1244 /* note: maxsector != maxsect only if you are
1245 * doing cyl sparing */
1246 drv->headoff = 0;
1247 drv->maxcyl = iorq->xd->pcyl - 1;
1248 drv->maxhead = iorq->xd->nhead - 1;
1249 break;
1250 }
1251 case XDFUN_FMT:{
1252 struct xd_iopb_format *form =
1253 (struct xd_iopb_format *) iopb;
1254 if (XD_STATE(iorq->mode) == XD_SUB_POLL)
1255 form->interleave_ipl = (XDC_INTERLEAVE << 3);
1256 else
1257 form->interleave_ipl = (XDC_INTERLEAVE << 3) |
1258 iorq->xdc->ipl;
1259 form->field1 = XDFM_FIELD1;
1260 form->field2 = XDFM_FIELD2;
1261 form->field3 = XDFM_FIELD3;
1262 form->field4 = XDFM_FIELD4;
1263 form->bytespersec = XDFM_BPS;
1264 form->field6 = XDFM_FIELD6;
1265 form->field7 = XDFM_FIELD7;
1266 break;
1267 }
1268 }
1269 } else {
1270
1271 /* normal IOPB case (harmless to RDP command) */
1272
1273 iopb->lll = 0;
1274 iopb->intl = (XD_STATE(iorq->mode) == XD_SUB_POLL)
1275 ? 0
1276 : iorq->xdc->ipl;
1277 iopb->sectcnt = iorq->sectcnt;
1278 block = iorq->blockno;
1279 if (iorq->xd == NULL || block == 0) {
1280 iopb->sectno = iopb->headno = iopb->cylno = 0;
1281 } else {
1282 iopb->sectno = block % iorq->xd->nsect;
1283 block = block / iorq->xd->nsect;
1284 iopb->headno = block % iorq->xd->nhead;
1285 block = block / iorq->xd->nhead;
1286 iopb->cylno = block;
1287 }
1288 iopb->daddr = dp = (iorq->dbuf == NULL) ? 0 :
1289 dvma_kvtopa(iorq->dbuf, iorq->xdc->bustype);
1290 iopb->addrmod = XDC_ADDRMOD;
1291 }
1292 }
1293
1294 /*
1295 * xdc_cmd: front end for POLL'd and WAIT'd commands. Returns rqno.
1296 * If you've already got an IORQ, you can call submit directly (currently
1297 * there is no need to do this). NORM requests are handled seperately.
1298 */
1299 int
1300 xdc_cmd(xdcsc, cmd, subfn, unit, block, scnt, dptr, fullmode)
1301 struct xdc_softc *xdcsc;
1302 int cmd, subfn, unit, block, scnt;
1303 char *dptr;
1304 int fullmode;
1305
1306 {
1307 struct xd_iorq *iorq;
1308 struct xd_iopb *iopb;
1309 int rqno, retry;
1310 int submode = XD_STATE(fullmode);
1311
1312 /* get iorq/iopb */
1313 switch (submode) {
1314 case XD_SUB_POLL:
1315 while (xdcsc->nfree == 0) {
1316 if (xdc_piodriver(xdcsc, 0, 1) != XD_ERR_AOK)
1317 return (XD_ERR_FAIL);
1318 }
1319 break;
1320 case XD_SUB_WAIT:
1321 retry = 1;
1322 while (retry) {
1323 while (xdcsc->nfree == 0) {
1324 if (tsleep(&xdcsc->nfree, PRIBIO, "xdnfree", 0))
1325 return (XD_ERR_FAIL);
1326 }
1327 while (xdcsc->ndone > XDC_SUBWAITLIM) {
1328 if (tsleep(&xdcsc->ndone, PRIBIO, "xdsubwait", 0))
1329 return (XD_ERR_FAIL);
1330 }
1331 if (xdcsc->nfree)
1332 retry = 0; /* got it */
1333 }
1334 break;
1335 default:
1336 return (XD_ERR_FAIL); /* illegal */
1337 }
1338 if (xdcsc->nfree == 0)
1339 panic("xdcmd nfree");
1340 rqno = XDC_RQALLOC(xdcsc);
1341 iorq = &xdcsc->reqs[rqno];
1342 iopb = iorq->iopb;
1343
1344
1345 /* init iorq/iopb */
1346 xdc_rqinit(iorq, xdcsc,
1347 (unit == XDC_NOUNIT) ? NULL : xdcsc->sc_drives[unit],
1348 fullmode, block, scnt, dptr, NULL);
1349
1350 /* load IOPB from iorq */
1351 xdc_rqtopb(iorq, iopb, cmd, subfn);
1352
1353 /* submit it for processing */
1354 xdc_submit_iorq(xdcsc, rqno, fullmode); /* error code will be in iorq */
1355
1356 return (rqno);
1357 }
1358
1359 /*
1360 * xdc_startbuf
1361 * start a buffer running, assumes nfree > 0
1362 */
1363 int
1364 xdc_startbuf(xdcsc, xdsc, bp)
1365 struct xdc_softc *xdcsc;
1366 struct xd_softc *xdsc;
1367 struct buf *bp;
1368
1369 {
1370 int rqno, partno;
1371 struct xd_iorq *iorq;
1372 struct xd_iopb *iopb;
1373 struct buf *wq;
1374 u_long block;
1375 caddr_t dbuf;
1376
1377 if (!xdcsc->nfree)
1378 panic("xdc_startbuf free");
1379 rqno = XDC_RQALLOC(xdcsc);
1380 iorq = &xdcsc->reqs[rqno];
1381 iopb = iorq->iopb;
1382
1383 /* get buf */
1384
1385 if (bp == NULL) {
1386 bp = xdcsc->sc_wq.b_actf;
1387 if (!bp)
1388 panic("xdc_startbuf bp");
1389 wq = bp->b_actf;
1390 if (wq)
1391 wq->b_actb = bp->b_actb;
1392 else
1393 xdcsc->sc_wq.b_actb = bp->b_actb;
1394 *bp->b_actb = wq;
1395 xdsc = xdcsc->sc_drives[DISKUNIT(bp->b_dev)];
1396 }
1397 partno = DISKPART(bp->b_dev);
1398 #ifdef XDC_DEBUG
1399 printf("xdc_startbuf: %s%c: %s block %d\n", xdsc->sc_dev.dv_xname,
1400 'a' + partno, (bp->b_flags & B_READ) ? "read" : "write", bp->b_blkno);
1401 printf("xdc_startbuf: b_bcount %d, b_data 0x%x\n",
1402 bp->b_bcount, bp->b_data);
1403 #endif
1404
1405 /*
1406 * load request. we have to calculate the correct block number based
1407 * on partition info.
1408 *
1409 * also, note that there are two kinds of buf structures, those with
1410 * B_PHYS set and those without B_PHYS. if B_PHYS is set, then it is
1411 * a raw I/O (to a cdevsw) and we are doing I/O directly to the users'
1412 * buffer which has already been mapped into DVMA space. (Not on sun3)
1413 * However, if B_PHYS is not set, then the buffer is a normal system
1414 * buffer which does *not* live in DVMA space. In that case we call
1415 * dvma_mapin to map it into DVMA space so we can do the DMA to it.
1416 *
1417 * in cases where we do a dvma_mapin, note that iorq points to the buffer
1418 * as mapped into DVMA space, where as the bp->b_data points to its
1419 * non-DVMA mapping.
1420 *
1421 * XXX - On the sun3, B_PHYS does NOT mean the buffer is mapped
1422 * into dvma space, only that it was remapped into the kernel.
1423 * We ALWAYS have to remap the kernel buf into DVMA space.
1424 * (It is done inexpensively, using whole segments!)
1425 */
1426
1427 block = bp->b_blkno + ((partno == RAW_PART) ? 0 :
1428 xdsc->sc_dk.dk_label->d_partitions[partno].p_offset);
1429
1430 dbuf = dvma_mapin(bp->b_data, bp->b_bcount, 0);
1431 if (dbuf == NULL) { /* out of DVMA space */
1432 printf("%s: warning: out of DVMA space\n",
1433 xdcsc->sc_dev.dv_xname);
1434 XDC_FREE(xdcsc, rqno);
1435 wq = &xdcsc->sc_wq; /* put at end of queue */
1436 bp->b_actf = 0;
1437 bp->b_actb = wq->b_actb;
1438 *wq->b_actb = bp;
1439 wq->b_actb = &bp->b_actf;
1440 return (XD_ERR_FAIL); /* XXX: need some sort of
1441 * call-back scheme here? */
1442 }
1443
1444 /* init iorq and load iopb from it */
1445
1446 xdc_rqinit(iorq, xdcsc, xdsc, XD_SUB_NORM | XD_MODE_VERBO, block,
1447 bp->b_bcount / XDFM_BPS, dbuf, bp);
1448
1449 xdc_rqtopb(iorq, iopb, (bp->b_flags & B_READ) ? XDCMD_RD : XDCMD_WR, 0);
1450
1451 /* Instrumentation. */
1452 disk_busy(&xdsc->sc_dk);
1453
1454 /* now submit [note that xdc_submit_iorq can never fail on NORM reqs] */
1455
1456 xdc_submit_iorq(xdcsc, rqno, XD_SUB_NORM);
1457 return (XD_ERR_AOK);
1458 }
1459
1460
1461 /*
1462 * xdc_submit_iorq: submit an iorq for processing. returns XD_ERR_AOK
1463 * if ok. if it fail returns an error code. type is XD_SUB_*.
1464 *
1465 * note: caller frees iorq in all cases except NORM
1466 *
1467 * return value:
1468 * NORM: XD_AOK (req pending), XD_FAIL (couldn't submit request)
1469 * WAIT: XD_AOK (success), <error-code> (failed)
1470 * POLL: <same as WAIT>
1471 * NOQ : <same as NORM>
1472 *
1473 * there are three sources for i/o requests:
1474 * [1] xdstrategy: normal block I/O, using "struct buf" system.
1475 * [2] autoconfig/crash dump: these are polled I/O requests, no interrupts.
1476 * [3] open/ioctl: these are I/O requests done in the context of a process,
1477 * and the process should block until they are done.
1478 *
1479 * software state is stored in the iorq structure. each iorq has an
1480 * iopb structure. the hardware understands the iopb structure.
1481 * every command must go through an iopb. a 7053 can only handle
1482 * XDC_MAXIOPB (31) active iopbs at one time. iopbs are allocated in
1483 * DVMA space at boot up time. what happens if we run out of iopb's?
1484 * for i/o type [1], the buffers are queued at the "buff" layer and
1485 * picked up later by the interrupt routine. for case [2] the
1486 * programmed i/o driver is called with a special flag that says
1487 * return when one iopb is free. for case [3] the process can sleep
1488 * on the iorq free list until some iopbs are avaliable.
1489 */
1490
1491
1492 int
1493 xdc_submit_iorq(xdcsc, iorqno, type)
1494 struct xdc_softc *xdcsc;
1495 int iorqno;
1496 int type;
1497
1498 {
1499 u_long iopbaddr;
1500 struct xd_iorq *iorq = &xdcsc->reqs[iorqno];
1501
1502 #ifdef XDC_DEBUG
1503 printf("xdc_submit_iorq(%s, no=%d, type=%d)\n", xdcsc->sc_dev.dv_xname,
1504 iorqno, type);
1505 #endif
1506
1507 /* first check and see if controller is busy */
1508 if (xdcsc->xdc->xdc_csr & XDC_ADDING) {
1509 #ifdef XDC_DEBUG
1510 printf("xdc_submit_iorq: XDC not ready (ADDING)\n");
1511 #endif
1512 if (type == XD_SUB_NOQ)
1513 return (XD_ERR_FAIL); /* failed */
1514 XDC_TWAIT(xdcsc, iorqno); /* put at end of waitq */
1515 switch (type) {
1516 case XD_SUB_NORM:
1517 return XD_ERR_AOK; /* success */
1518 case XD_SUB_WAIT:
1519 while (iorq->iopb->done == 0) {
1520 sleep(iorq, PRIBIO);
1521 }
1522 return (iorq->errno);
1523 case XD_SUB_POLL:
1524 return (xdc_piodriver(xdcsc, iorqno, 0));
1525 default:
1526 panic("xdc_submit_iorq adding");
1527 }
1528 }
1529 #ifdef XDC_DEBUG
1530 {
1531 u_char *rio = (u_char *) iorq->iopb;
1532 int sz = sizeof(struct xd_iopb), lcv;
1533 printf("%s: aio #%d [",
1534 xdcsc->sc_dev.dv_xname, iorq - xdcsc->reqs);
1535 for (lcv = 0; lcv < sz; lcv++)
1536 printf(" %02x", rio[lcv]);
1537 printf("]\n");
1538 }
1539 #endif /* XDC_DEBUG */
1540
1541 /* controller not busy, start command */
1542 iopbaddr = dvma_kvtopa(iorq->iopb, xdcsc->bustype);
1543 XDC_GO(xdcsc->xdc, iopbaddr); /* go! */
1544 xdcsc->nrun++;
1545 /* command now running, wrap it up */
1546 switch (type) {
1547 case XD_SUB_NORM:
1548 case XD_SUB_NOQ:
1549 return (XD_ERR_AOK); /* success */
1550 case XD_SUB_WAIT:
1551 while (iorq->iopb->done == 0) {
1552 sleep(iorq, PRIBIO);
1553 }
1554 return (iorq->errno);
1555 case XD_SUB_POLL:
1556 return (xdc_piodriver(xdcsc, iorqno, 0));
1557 default:
1558 panic("xdc_submit_iorq wrap up");
1559 }
1560 panic("xdc_submit_iorq");
1561 return 0; /* not reached */
1562 }
1563
1564
1565 /*
1566 * xdc_piodriver
1567 *
1568 * programmed i/o driver. this function takes over the computer
1569 * and drains off all i/o requests. it returns the status of the iorq
1570 * the caller is interesting in. if freeone is true, then it returns
1571 * when there is a free iorq.
1572 */
1573 int
1574 xdc_piodriver(xdcsc, iorqno, freeone)
1575 struct xdc_softc *xdcsc;
1576 int iorqno;
1577 int freeone;
1578
1579 {
1580 int nreset = 0;
1581 int retval = 0;
1582 u_long count;
1583 struct xdc *xdc = xdcsc->xdc;
1584 #ifdef XDC_DEBUG
1585 printf("xdc_piodriver(%s, %d, freeone=%d)\n", xdcsc->sc_dev.dv_xname,
1586 iorqno, freeone);
1587 #endif
1588
1589 while (xdcsc->nwait || xdcsc->nrun) {
1590 #ifdef XDC_DEBUG
1591 printf("xdc_piodriver: wait=%d, run=%d\n",
1592 xdcsc->nwait, xdcsc->nrun);
1593 #endif
1594 XDC_WAIT(xdc, count, XDC_MAXTIME, (XDC_REMIOPB | XDC_F_ERROR));
1595 #ifdef XDC_DEBUG
1596 printf("xdc_piodriver: done wait with count = %d\n", count);
1597 #endif
1598 /* we expect some progress soon */
1599 if (count == 0 && nreset >= 2) {
1600 xdc_reset(xdcsc, 0, XD_RSET_ALL, XD_ERR_FAIL, 0);
1601 #ifdef XDC_DEBUG
1602 printf("xdc_piodriver: timeout\n");
1603 #endif
1604 return (XD_ERR_FAIL);
1605 }
1606 if (count == 0) {
1607 if (xdc_reset(xdcsc, 0,
1608 (nreset++ == 0) ? XD_RSET_NONE : iorqno,
1609 XD_ERR_FAIL,
1610 0) == XD_ERR_FAIL)
1611 return (XD_ERR_FAIL); /* flushes all but POLL
1612 * requests, resets */
1613 continue;
1614 }
1615 xdc_remove_iorq(xdcsc); /* could resubmit request */
1616 if (freeone) {
1617 if (xdcsc->nrun < XDC_MAXIOPB) {
1618 #ifdef XDC_DEBUG
1619 printf("xdc_piodriver: done: one free\n");
1620 #endif
1621 return (XD_ERR_AOK);
1622 }
1623 continue; /* don't xdc_start */
1624 }
1625 xdc_start(xdcsc, XDC_MAXIOPB);
1626 }
1627
1628 /* get return value */
1629
1630 retval = xdcsc->reqs[iorqno].errno;
1631
1632 #ifdef XDC_DEBUG
1633 printf("xdc_piodriver: done, retval = 0x%x (%s)\n",
1634 xdcsc->reqs[iorqno].errno, xdc_e2str(xdcsc->reqs[iorqno].errno));
1635 #endif
1636
1637 /* now that we've drained everything, start up any bufs that have
1638 * queued */
1639
1640 while (xdcsc->nfree > 0 && xdcsc->sc_wq.b_actf)
1641 if (xdc_startbuf(xdcsc, NULL, NULL) != XD_ERR_AOK)
1642 break;
1643
1644 return (retval);
1645 }
1646
1647 /*
1648 * xdc_reset: reset one drive. NOTE: assumes xdc was just reset.
1649 * we steal iopb[0] for this, but we put it back when we are done.
1650 */
1651 void
1652 xdc_xdreset(xdcsc, xdsc)
1653 struct xdc_softc *xdcsc;
1654 struct xd_softc *xdsc;
1655
1656 {
1657 struct xd_iopb tmpiopb;
1658 u_long addr;
1659 int del;
1660 bcopy(xdcsc->iopbase, &tmpiopb, sizeof(tmpiopb));
1661 bzero(xdcsc->iopbase, sizeof(tmpiopb));
1662 xdcsc->iopbase->comm = XDCMD_RST;
1663 xdcsc->iopbase->unit = xdsc->xd_drive;
1664 addr = (u_long) xdcsc->dvmaiopb;
1665 XDC_GO(xdcsc->xdc, addr); /* go! */
1666 XDC_WAIT(xdcsc->xdc, del, XDC_RESETUSEC, XDC_REMIOPB);
1667 if (del <= 0 || xdcsc->iopbase->errs) {
1668 printf("%s: off-line: %s\n", xdcsc->sc_dev.dv_xname,
1669 xdc_e2str(xdcsc->iopbase->errno));
1670 xdcsc->xdc->xdc_csr = XDC_RESET;
1671 XDC_WAIT(xdcsc->xdc, del, XDC_RESETUSEC, XDC_RESET);
1672 if (del <= 0)
1673 panic("xdc_reset");
1674 } else {
1675 xdcsc->xdc->xdc_csr = XDC_CLRRIO; /* clear RIO */
1676 }
1677 bcopy(&tmpiopb, xdcsc->iopbase, sizeof(tmpiopb));
1678 }
1679
1680
1681 /*
1682 * xdc_reset: reset everything: requests are marked as errors except
1683 * a polled request (which is resubmitted)
1684 */
1685 int
1686 xdc_reset(xdcsc, quiet, blastmode, error, xdsc)
1687 struct xdc_softc *xdcsc;
1688 int quiet, blastmode, error;
1689 struct xd_softc *xdsc;
1690
1691 {
1692 int del = 0, lcv, retval = XD_ERR_AOK;
1693 int oldfree = xdcsc->nfree;
1694 struct xd_iorq *iorq;
1695
1696 /* soft reset hardware */
1697
1698 if (!quiet)
1699 printf("%s: soft reset\n", xdcsc->sc_dev.dv_xname);
1700 xdcsc->xdc->xdc_csr = XDC_RESET;
1701 XDC_WAIT(xdcsc->xdc, del, XDC_RESETUSEC, XDC_RESET);
1702 if (del <= 0) {
1703 blastmode = XD_RSET_ALL; /* dead, flush all requests */
1704 retval = XD_ERR_FAIL;
1705 }
1706 if (xdsc)
1707 xdc_xdreset(xdcsc, xdsc);
1708
1709 /* fix queues based on "blast-mode" */
1710
1711 for (lcv = 0; lcv < XDC_MAXIOPB; lcv++) {
1712 iorq = &xdcsc->reqs[lcv];
1713
1714 if (XD_STATE(iorq->mode) != XD_SUB_POLL &&
1715 XD_STATE(iorq->mode) != XD_SUB_WAIT &&
1716 XD_STATE(iorq->mode) != XD_SUB_NORM)
1717 /* is it active? */
1718 continue;
1719
1720 xdcsc->nrun--; /* it isn't running any more */
1721 if (blastmode == XD_RSET_ALL || blastmode != lcv) {
1722 /* failed */
1723 iorq->errno = error;
1724 xdcsc->iopbase[lcv].done = xdcsc->iopbase[lcv].errs = 1;
1725 switch (XD_STATE(iorq->mode)) {
1726 case XD_SUB_NORM:
1727 iorq->buf->b_error = EIO;
1728 iorq->buf->b_flags |= B_ERROR;
1729 iorq->buf->b_resid =
1730 iorq->sectcnt * XDFM_BPS;
1731 /* Sun3: map/unmap regardless of B_PHYS */
1732 dvma_mapout(iorq->dbufbase,
1733 iorq->buf->b_bcount);
1734 disk_unbusy(&iorq->xd->sc_dk,
1735 (iorq->buf->b_bcount - iorq->buf->b_resid));
1736 biodone(iorq->buf);
1737 XDC_FREE(xdcsc, lcv); /* add to free list */
1738 break;
1739 case XD_SUB_WAIT:
1740 wakeup(iorq);
1741 case XD_SUB_POLL:
1742 xdcsc->ndone++;
1743 iorq->mode = XD_NEWSTATE(iorq->mode, XD_SUB_DONE);
1744 break;
1745 }
1746
1747 } else {
1748
1749 /* resubmit, put at front of wait queue */
1750 XDC_HWAIT(xdcsc, lcv);
1751 }
1752 }
1753
1754 /*
1755 * now, if stuff is waiting, start it.
1756 * since we just reset it should go
1757 */
1758 xdc_start(xdcsc, XDC_MAXIOPB);
1759
1760 /* ok, we did it */
1761 if (oldfree == 0 && xdcsc->nfree)
1762 wakeup(&xdcsc->nfree);
1763
1764 #ifdef XDC_DIAG
1765 del = xdcsc->nwait + xdcsc->nrun + xdcsc->nfree + xdcsc->ndone;
1766 if (del != XDC_MAXIOPB)
1767 printf("%s: diag: xdc_reset miscount (%d should be %d)!\n",
1768 xdcsc->sc_dev.dv_xname, del, XDC_MAXIOPB);
1769 else
1770 if (xdcsc->ndone > XDC_MAXIOPB - XDC_SUBWAITLIM)
1771 printf("%s: diag: lots of done jobs (%d)\n",
1772 xdcsc->sc_dev.dv_xname, xdcsc->ndone);
1773 #endif
1774 printf("RESET DONE\n");
1775 return (retval);
1776 }
1777
1778 /*
1779 * xdc_start: start all waiting buffers
1780 */
1781 void
1782 xdc_start(xdcsc, maxio)
1783 struct xdc_softc *xdcsc;
1784 int maxio;
1785
1786 {
1787 int rqno;
1788 while (maxio && xdcsc->nwait &&
1789 (xdcsc->xdc->xdc_csr & XDC_ADDING) == 0) {
1790 XDC_GET_WAITER(xdcsc, rqno); /* note: rqno is an "out"
1791 * param */
1792 if (xdc_submit_iorq(xdcsc, rqno, XD_SUB_NOQ) != XD_ERR_AOK)
1793 panic("xdc_start"); /* should never happen */
1794 maxio--;
1795 }
1796 }
1797
1798 /*
1799 * xdc_remove_iorq: remove "done" IOPB's.
1800 */
1801 int
1802 xdc_remove_iorq(xdcsc)
1803 struct xdc_softc *xdcsc;
1804
1805 {
1806 int errno, rqno, comm, errs;
1807 struct xdc *xdc = xdcsc->xdc;
1808 struct xd_iopb *iopb;
1809 struct xd_iorq *iorq;
1810 struct buf *bp;
1811
1812 if (xdc->xdc_csr & XDC_F_ERROR) {
1813 /*
1814 * FATAL ERROR: should never happen under normal use. This
1815 * error is so bad, you can't even tell which IOPB is bad, so
1816 * we dump them all.
1817 */
1818 errno = xdc->xdc_f_err;
1819 printf("%s: fatal error 0x%02x: %s\n", xdcsc->sc_dev.dv_xname,
1820 errno, xdc_e2str(errno));
1821 if (xdc_reset(xdcsc, 0, XD_RSET_ALL, errno, 0) != XD_ERR_AOK) {
1822 printf("%s: soft reset failed!\n",
1823 xdcsc->sc_dev.dv_xname);
1824 panic("xdc_remove_iorq: controller DEAD");
1825 }
1826 return (XD_ERR_AOK);
1827 }
1828
1829 /*
1830 * get iopb that is done
1831 *
1832 * hmm... I used to read the address of the done IOPB off the VME
1833 * registers and calculate the rqno directly from that. that worked
1834 * until I started putting a load on the controller. when loaded, i
1835 * would get interrupts but neither the REMIOPB or F_ERROR bits would
1836 * be set, even after DELAY'ing a while! later on the timeout
1837 * routine would detect IOPBs that were marked "running" but their
1838 * "done" bit was set. rather than dealing directly with this
1839 * problem, it is just easier to look at all running IOPB's for the
1840 * done bit.
1841 */
1842 if (xdc->xdc_csr & XDC_REMIOPB) {
1843 xdc->xdc_csr = XDC_CLRRIO;
1844 }
1845
1846 for (rqno = 0; rqno < XDC_MAXIOPB; rqno++) {
1847 iorq = &xdcsc->reqs[rqno];
1848 if (iorq->mode == 0 || XD_STATE(iorq->mode) == XD_SUB_DONE)
1849 continue; /* free, or done */
1850 iopb = &xdcsc->iopbase[rqno];
1851 if (iopb->done == 0)
1852 continue; /* not done yet */
1853
1854 #ifdef XDC_DEBUG
1855 {
1856 u_char *rio = (u_char *) iopb;
1857 int sz = sizeof(struct xd_iopb), lcv;
1858 printf("%s: rio #%d [", xdcsc->sc_dev.dv_xname, rqno);
1859 for (lcv = 0; lcv < sz; lcv++)
1860 printf(" %02x", rio[lcv]);
1861 printf("]\n");
1862 }
1863 #endif /* XDC_DEBUG */
1864
1865 xdcsc->nrun--;
1866
1867 comm = iopb->comm;
1868 errs = iopb->errs;
1869
1870 if (errs)
1871 iorq->errno = iopb->errno;
1872 else
1873 iorq->errno = 0;
1874
1875 /* handle non-fatal errors */
1876
1877 if (errs &&
1878 xdc_error(xdcsc, iorq, iopb, rqno, comm) == XD_ERR_AOK)
1879 continue; /* AOK: we resubmitted it */
1880
1881
1882 /* this iorq is now done (hasn't been restarted or anything) */
1883
1884 if ((iorq->mode & XD_MODE_VERBO) && iorq->lasterror)
1885 xdc_perror(iorq, iopb, 0);
1886
1887 /* now, if read/write check to make sure we got all the data
1888 * we needed. (this may not be the case if we got an error in
1889 * the middle of a multisector request). */
1890
1891 if ((iorq->mode & XD_MODE_B144) != 0 && errs == 0 &&
1892 (comm == XDCMD_RD || comm == XDCMD_WR)) {
1893 /* we just successfully processed a bad144 sector
1894 * note: if we are in bad 144 mode, the pointers have
1895 * been advanced already (see above) and are pointing
1896 * at the bad144 sector. to exit bad144 mode, we
1897 * must advance the pointers 1 sector and issue a new
1898 * request if there are still sectors left to process
1899 *
1900 */
1901 XDC_ADVANCE(iorq, 1); /* advance 1 sector */
1902
1903 /* exit b144 mode */
1904 iorq->mode = iorq->mode & (~XD_MODE_B144);
1905
1906 if (iorq->sectcnt) { /* more to go! */
1907 iorq->lasterror = iorq->errno = iopb->errno = 0;
1908 iopb->errs = iopb->done = 0;
1909 iorq->tries = 0;
1910 iopb->sectcnt = iorq->sectcnt;
1911 iopb->cylno = iorq->blockno /
1912 iorq->xd->sectpercyl;
1913 iopb->headno =
1914 (iorq->blockno / iorq->xd->nhead) %
1915 iorq->xd->nhead;
1916 iopb->sectno = iorq->blockno % XDFM_BPS;
1917 iopb->daddr =
1918 dvma_kvtopa(iorq->dbuf, xdcsc->bustype);
1919 XDC_HWAIT(xdcsc, rqno);
1920 xdc_start(xdcsc, 1); /* resubmit */
1921 continue;
1922 }
1923 }
1924 /* final cleanup, totally done with this request */
1925
1926 switch (XD_STATE(iorq->mode)) {
1927 case XD_SUB_NORM:
1928 bp = iorq->buf;
1929 if (errs) {
1930 bp->b_error = EIO;
1931 bp->b_flags |= B_ERROR;
1932 bp->b_resid = iorq->sectcnt * XDFM_BPS;
1933 } else {
1934 bp->b_resid = 0; /* done */
1935 }
1936 /* Sun3: map/unmap regardless of B_PHYS */
1937 dvma_mapout(iorq->dbufbase,
1938 iorq->buf->b_bcount);
1939 disk_unbusy(&iorq->xd->sc_dk,
1940 (bp->b_bcount - bp->b_resid));
1941 XDC_FREE(xdcsc, rqno);
1942 biodone(bp);
1943 break;
1944 case XD_SUB_WAIT:
1945 iorq->mode = XD_NEWSTATE(iorq->mode, XD_SUB_DONE);
1946 xdcsc->ndone++;
1947 wakeup(iorq);
1948 break;
1949 case XD_SUB_POLL:
1950 iorq->mode = XD_NEWSTATE(iorq->mode, XD_SUB_DONE);
1951 xdcsc->ndone++;
1952 break;
1953 }
1954 }
1955
1956 return (XD_ERR_AOK);
1957 }
1958
1959 /*
1960 * xdc_perror: print error.
1961 * - if still_trying is true: we got an error, retried and got a
1962 * different error. in that case lasterror is the old error,
1963 * and errno is the new one.
1964 * - if still_trying is not true, then if we ever had an error it
1965 * is in lasterror. also, if iorq->errno == 0, then we recovered
1966 * from that error (otherwise iorq->errno == iorq->lasterror).
1967 */
1968 void
1969 xdc_perror(iorq, iopb, still_trying)
1970 struct xd_iorq *iorq;
1971 struct xd_iopb *iopb;
1972 int still_trying;
1973
1974 {
1975
1976 int error = iorq->lasterror;
1977
1978 printf("%s", (iorq->xd) ?
1979 iorq->xd->sc_dev.dv_xname :
1980 iorq->xdc->sc_dev.dv_xname);
1981 if (iorq->buf)
1982 printf("%c: ", 'a' + DISKPART(iorq->buf->b_dev));
1983 if (iopb->comm == XDCMD_RD || iopb->comm == XDCMD_WR)
1984 printf("%s %d/%d/%d: ",
1985 (iopb->comm == XDCMD_RD) ? "read" : "write",
1986 iopb->cylno, iopb->headno, iopb->sectno);
1987 printf("%s", xdc_e2str(error));
1988
1989 if (still_trying)
1990 printf(" [still trying, new error=%s]", xdc_e2str(iorq->errno));
1991 else
1992 if (iorq->errno == 0)
1993 printf(" [recovered in %d tries]", iorq->tries);
1994
1995 printf("\n");
1996 }
1997
1998 /*
1999 * xdc_error: non-fatal error encountered... recover.
2000 * return AOK if resubmitted, return FAIL if this iopb is done
2001 */
2002 int
2003 xdc_error(xdcsc, iorq, iopb, rqno, comm)
2004 struct xdc_softc *xdcsc;
2005 struct xd_iorq *iorq;
2006 struct xd_iopb *iopb;
2007 int rqno, comm;
2008
2009 {
2010 int errno = iorq->errno;
2011 int erract = errno & XD_ERA_MASK;
2012 int oldmode, advance, i;
2013
2014 if (erract == XD_ERA_RSET) { /* some errors require a reset */
2015 oldmode = iorq->mode;
2016 iorq->mode = XD_SUB_DONE | (~XD_SUB_MASK & oldmode);
2017 xdcsc->ndone++;
2018 /* make xdc_start ignore us */
2019 xdc_reset(xdcsc, 1, XD_RSET_NONE, errno, iorq->xd);
2020 iorq->mode = oldmode;
2021 xdcsc->ndone--;
2022 }
2023 /* check for read/write to a sector in bad144 table if bad: redirect
2024 * request to bad144 area */
2025
2026 if ((comm == XDCMD_RD || comm == XDCMD_WR) &&
2027 (iorq->mode & XD_MODE_B144) == 0) {
2028 advance = iorq->sectcnt - iopb->sectcnt;
2029 XDC_ADVANCE(iorq, advance);
2030 if ((i = isbad(&iorq->xd->dkb, iorq->blockno / iorq->xd->sectpercyl,
2031 (iorq->blockno / iorq->xd->nsect) % iorq->xd->nhead,
2032 iorq->blockno % iorq->xd->nsect)) != -1) {
2033 iorq->mode |= XD_MODE_B144; /* enter bad144 mode &
2034 * redirect */
2035 iopb->errno = iopb->done = iopb->errs = 0;
2036 iopb->sectcnt = 1;
2037 iopb->cylno = (iorq->xd->ncyl + iorq->xd->acyl) - 2;
2038 /* second to last acyl */
2039 i = iorq->xd->sectpercyl - 1 - i; /* follow bad144
2040 * standard */
2041 iopb->headno = i / iorq->xd->nhead;
2042 iopb->sectno = i % iorq->xd->nhead;
2043 XDC_HWAIT(xdcsc, rqno);
2044 xdc_start(xdcsc, 1); /* resubmit */
2045 return (XD_ERR_AOK); /* recovered! */
2046 }
2047 }
2048
2049 /*
2050 * it isn't a bad144 sector, must be real error! see if we can retry
2051 * it?
2052 */
2053 if ((iorq->mode & XD_MODE_VERBO) && iorq->lasterror)
2054 xdc_perror(iorq, iopb, 1); /* inform of error state
2055 * change */
2056 iorq->lasterror = errno;
2057
2058 if ((erract == XD_ERA_RSET || erract == XD_ERA_HARD)
2059 && iorq->tries < XDC_MAXTRIES) { /* retry? */
2060 iorq->tries++;
2061 iorq->errno = iopb->errno = iopb->done = iopb->errs = 0;
2062 XDC_HWAIT(xdcsc, rqno);
2063 xdc_start(xdcsc, 1); /* restart */
2064 return (XD_ERR_AOK); /* recovered! */
2065 }
2066
2067 /* failed to recover from this error */
2068 return (XD_ERR_FAIL);
2069 }
2070
2071 /*
2072 * xdc_tick: make sure xd is still alive and ticking (err, kicking).
2073 */
2074 void
2075 xdc_tick(arg)
2076 void *arg;
2077
2078 {
2079 struct xdc_softc *xdcsc = arg;
2080 int lcv, s, reset = 0;
2081 #ifdef XDC_DIAG
2082 int wait, run, free, done, whd = 0;
2083 u_char fqc[XDC_MAXIOPB], wqc[XDC_MAXIOPB], mark[XDC_MAXIOPB];
2084 s = splbio();
2085 wait = xdcsc->nwait;
2086 run = xdcsc->nrun;
2087 free = xdcsc->nfree;
2088 done = xdcsc->ndone;
2089 bcopy(xdcsc->waitq, wqc, sizeof(wqc));
2090 bcopy(xdcsc->freereq, fqc, sizeof(fqc));
2091 splx(s);
2092 if (wait + run + free + done != XDC_MAXIOPB) {
2093 printf("%s: diag: IOPB miscount (got w/f/r/d %d/%d/%d/%d, wanted %d)\n",
2094 xdcsc->sc_dev.dv_xname, wait, free, run, done, XDC_MAXIOPB);
2095 bzero(mark, sizeof(mark));
2096 printf("FREE: ");
2097 for (lcv = free; lcv > 0; lcv--) {
2098 printf("%d ", fqc[lcv - 1]);
2099 mark[fqc[lcv - 1]] = 1;
2100 }
2101 printf("\nWAIT: ");
2102 lcv = wait;
2103 while (lcv > 0) {
2104 printf("%d ", wqc[whd]);
2105 mark[wqc[whd]] = 1;
2106 whd = (whd + 1) % XDC_MAXIOPB;
2107 lcv--;
2108 }
2109 printf("\n");
2110 for (lcv = 0; lcv < XDC_MAXIOPB; lcv++) {
2111 if (mark[lcv] == 0)
2112 printf("MARK: running %d: mode %d done %d errs %d errno 0x%x ttl %d buf %p\n",
2113 lcv, xdcsc->reqs[lcv].mode,
2114 xdcsc->iopbase[lcv].done,
2115 xdcsc->iopbase[lcv].errs,
2116 xdcsc->iopbase[lcv].errno,
2117 xdcsc->reqs[lcv].ttl,
2118 xdcsc->reqs[lcv].buf);
2119 }
2120 } else
2121 if (done > XDC_MAXIOPB - XDC_SUBWAITLIM)
2122 printf("%s: diag: lots of done jobs (%d)\n",
2123 xdcsc->sc_dev.dv_xname, done);
2124
2125 #endif
2126 #ifdef XDC_DEBUG
2127 printf("%s: tick: csr 0x%x, w/f/r/d %d/%d/%d/%d\n",
2128 xdcsc->sc_dev.dv_xname,
2129 xdcsc->xdc->xdc_csr, xdcsc->nwait, xdcsc->nfree, xdcsc->nrun,
2130 xdcsc->ndone);
2131 for (lcv = 0; lcv < XDC_MAXIOPB; lcv++) {
2132 if (xdcsc->reqs[lcv].mode)
2133 printf("running %d: mode %d done %d errs %d errno 0x%x\n",
2134 lcv,
2135 xdcsc->reqs[lcv].mode, xdcsc->iopbase[lcv].done,
2136 xdcsc->iopbase[lcv].errs, xdcsc->iopbase[lcv].errno);
2137 }
2138 #endif
2139
2140 /* reduce ttl for each request if one goes to zero, reset xdc */
2141 s = splbio();
2142 for (lcv = 0; lcv < XDC_MAXIOPB; lcv++) {
2143 if (xdcsc->reqs[lcv].mode == 0 ||
2144 XD_STATE(xdcsc->reqs[lcv].mode) == XD_SUB_DONE)
2145 continue;
2146 xdcsc->reqs[lcv].ttl--;
2147 if (xdcsc->reqs[lcv].ttl == 0)
2148 reset = 1;
2149 }
2150 if (reset) {
2151 printf("%s: watchdog timeout\n", xdcsc->sc_dev.dv_xname);
2152 xdc_reset(xdcsc, 0, XD_RSET_NONE, XD_ERR_FAIL, NULL);
2153 }
2154 splx(s);
2155
2156 /* until next time */
2157
2158 timeout(xdc_tick, xdcsc, XDC_TICKCNT);
2159 }
2160
2161 /*
2162 * xdc_ioctlcmd: this function provides a user level interface to the
2163 * controller via ioctl. this allows "format" programs to be written
2164 * in user code, and is also useful for some debugging. we return
2165 * an error code. called at user priority.
2166 */
2167 int
2168 xdc_ioctlcmd(xd, dev, xio)
2169 struct xd_softc *xd;
2170 dev_t dev;
2171 struct xd_iocmd *xio;
2172
2173 {
2174 int s, err, rqno;
2175 caddr_t dvmabuf = NULL;
2176 struct xdc_softc *xdcsc;
2177
2178 /* check sanity of requested command */
2179
2180 switch (xio->cmd) {
2181
2182 case XDCMD_NOP: /* no op: everything should be zero */
2183 if (xio->subfn || xio->dptr || xio->dlen ||
2184 xio->block || xio->sectcnt)
2185 return (EINVAL);
2186 break;
2187
2188 case XDCMD_RD: /* read / write sectors (up to XD_IOCMD_MAXS) */
2189 case XDCMD_WR:
2190 if (xio->subfn || xio->sectcnt > XD_IOCMD_MAXS ||
2191 xio->sectcnt * XDFM_BPS != xio->dlen || xio->dptr == NULL)
2192 return (EINVAL);
2193 break;
2194
2195 case XDCMD_SK: /* seek: doesn't seem useful to export this */
2196 return (EINVAL);
2197
2198 case XDCMD_WRP: /* write parameters */
2199 return (EINVAL);/* not useful, except maybe drive
2200 * parameters... but drive parameters should
2201 * go via disklabel changes */
2202
2203 case XDCMD_RDP: /* read parameters */
2204 if (xio->subfn != XDFUN_DRV ||
2205 xio->dlen || xio->block || xio->dptr)
2206 return (EINVAL); /* allow read drive params to
2207 * get hw_spt */
2208 xio->sectcnt = xd->hw_spt; /* we already know the answer */
2209 return (0);
2210 break;
2211
2212 case XDCMD_XRD: /* extended read/write */
2213 case XDCMD_XWR:
2214
2215 switch (xio->subfn) {
2216
2217 case XDFUN_THD:/* track headers */
2218 if (xio->sectcnt != xd->hw_spt ||
2219 (xio->block % xd->nsect) != 0 ||
2220 xio->dlen != XD_IOCMD_HSZ * xd->hw_spt ||
2221 xio->dptr == NULL)
2222 return (EINVAL);
2223 xio->sectcnt = 0;
2224 break;
2225
2226 case XDFUN_FMT:/* NOTE: also XDFUN_VFY */
2227 if (xio->cmd == XDCMD_XRD)
2228 return (EINVAL); /* no XDFUN_VFY */
2229 if (xio->sectcnt || xio->dlen ||
2230 (xio->block % xd->nsect) != 0 || xio->dptr)
2231 return (EINVAL);
2232 break;
2233
2234 case XDFUN_HDR:/* header, header verify, data, data ECC */
2235 return (EINVAL); /* not yet */
2236
2237 case XDFUN_DM: /* defect map */
2238 case XDFUN_DMX:/* defect map (alternate location) */
2239 if (xio->sectcnt || xio->dlen != XD_IOCMD_DMSZ ||
2240 (xio->block % xd->nsect) != 0 || xio->dptr == NULL)
2241 return (EINVAL);
2242 break;
2243
2244 default:
2245 return (EINVAL);
2246 }
2247 break;
2248
2249 case XDCMD_TST: /* diagnostics */
2250 return (EINVAL);
2251
2252 default:
2253 return (EINVAL);/* ??? */
2254 }
2255
2256 /* create DVMA buffer for request if needed */
2257
2258 if (xio->dlen) {
2259 dvmabuf = dvma_malloc(xio->dlen);
2260 if (xio->cmd == XDCMD_WR || xio->cmd == XDCMD_XWR) {
2261 err = copyin(xio->dptr, dvmabuf, xio->dlen);
2262 if (err) {
2263 dvma_free(dvmabuf, xio->dlen);
2264 return (err);
2265 }
2266 }
2267 }
2268 /* do it! */
2269
2270 err = 0;
2271 xdcsc = xd->parent;
2272 s = splbio();
2273 rqno = xdc_cmd(xdcsc, xio->cmd, xio->subfn, xd->xd_drive, xio->block,
2274 xio->sectcnt, dvmabuf, XD_SUB_WAIT);
2275 if (rqno == XD_ERR_FAIL) {
2276 err = EIO;
2277 goto done;
2278 }
2279 xio->errno = xdcsc->reqs[rqno].errno;
2280 xio->tries = xdcsc->reqs[rqno].tries;
2281 XDC_DONE(xdcsc, rqno, err);
2282
2283 if (xio->cmd == XDCMD_RD || xio->cmd == XDCMD_XRD)
2284 err = copyout(dvmabuf, xio->dptr, xio->dlen);
2285
2286 done:
2287 splx(s);
2288 if (dvmabuf)
2289 dvma_free(dvmabuf, xio->dlen);
2290 return (err);
2291 }
2292
2293 /*
2294 * xdc_e2str: convert error code number into an error string
2295 */
2296 char *
2297 xdc_e2str(no)
2298 int no;
2299 {
2300 switch (no) {
2301 case XD_ERR_FAIL:
2302 return ("Software fatal error");
2303 case XD_ERR_AOK:
2304 return ("Successful completion");
2305 case XD_ERR_ICYL:
2306 return ("Illegal cylinder address");
2307 case XD_ERR_IHD:
2308 return ("Illegal head address");
2309 case XD_ERR_ISEC:
2310 return ("Illgal sector address");
2311 case XD_ERR_CZER:
2312 return ("Count zero");
2313 case XD_ERR_UIMP:
2314 return ("Unimplemented command");
2315 case XD_ERR_IF1:
2316 return ("Illegal field length 1");
2317 case XD_ERR_IF2:
2318 return ("Illegal field length 2");
2319 case XD_ERR_IF3:
2320 return ("Illegal field length 3");
2321 case XD_ERR_IF4:
2322 return ("Illegal field length 4");
2323 case XD_ERR_IF5:
2324 return ("Illegal field length 5");
2325 case XD_ERR_IF6:
2326 return ("Illegal field length 6");
2327 case XD_ERR_IF7:
2328 return ("Illegal field length 7");
2329 case XD_ERR_ISG:
2330 return ("Illegal scatter/gather length");
2331 case XD_ERR_ISPT:
2332 return ("Not enough sectors per track");
2333 case XD_ERR_ALGN:
2334 return ("Next IOPB address alignment error");
2335 case XD_ERR_SGAL:
2336 return ("Scatter/gather address alignment error");
2337 case XD_ERR_SGEC:
2338 return ("Scatter/gather with auto-ECC");
2339 case XD_ERR_SECC:
2340 return ("Soft ECC corrected");
2341 case XD_ERR_SIGN:
2342 return ("ECC ignored");
2343 case XD_ERR_ASEK:
2344 return ("Auto-seek retry recovered");
2345 case XD_ERR_RTRY:
2346 return ("Soft retry recovered");
2347 case XD_ERR_HECC:
2348 return ("Hard data ECC");
2349 case XD_ERR_NHDR:
2350 return ("Header not found");
2351 case XD_ERR_NRDY:
2352 return ("Drive not ready");
2353 case XD_ERR_TOUT:
2354 return ("Operation timeout");
2355 case XD_ERR_VTIM:
2356 return ("VMEDMA timeout");
2357 case XD_ERR_DSEQ:
2358 return ("Disk sequencer error");
2359 case XD_ERR_HDEC:
2360 return ("Header ECC error");
2361 case XD_ERR_RVFY:
2362 return ("Read verify");
2363 case XD_ERR_VFER:
2364 return ("Fatail VMEDMA error");
2365 case XD_ERR_VBUS:
2366 return ("VMEbus error");
2367 case XD_ERR_DFLT:
2368 return ("Drive faulted");
2369 case XD_ERR_HECY:
2370 return ("Header error/cyliner");
2371 case XD_ERR_HEHD:
2372 return ("Header error/head");
2373 case XD_ERR_NOCY:
2374 return ("Drive not on-cylinder");
2375 case XD_ERR_SEEK:
2376 return ("Seek error");
2377 case XD_ERR_ILSS:
2378 return ("Illegal sector size");
2379 case XD_ERR_SEC:
2380 return ("Soft ECC");
2381 case XD_ERR_WPER:
2382 return ("Write-protect error");
2383 case XD_ERR_IRAM:
2384 return ("IRAM self test failure");
2385 case XD_ERR_MT3:
2386 return ("Maintenance test 3 failure (DSKCEL RAM)");
2387 case XD_ERR_MT4:
2388 return ("Maintenance test 4 failure (header shift reg)");
2389 case XD_ERR_MT5:
2390 return ("Maintenance test 5 failure (VMEDMA regs)");
2391 case XD_ERR_MT6:
2392 return ("Maintenance test 6 failure (REGCEL chip)");
2393 case XD_ERR_MT7:
2394 return ("Maintenance test 7 failure (buffer parity)");
2395 case XD_ERR_MT8:
2396 return ("Maintenance test 8 failure (disk FIFO)");
2397 case XD_ERR_IOCK:
2398 return ("IOPB checksum miscompare");
2399 case XD_ERR_IODM:
2400 return ("IOPB DMA fatal");
2401 case XD_ERR_IOAL:
2402 return ("IOPB address alignment error");
2403 case XD_ERR_FIRM:
2404 return ("Firmware error");
2405 case XD_ERR_MMOD:
2406 return ("Illegal maintenance mode test number");
2407 case XD_ERR_ACFL:
2408 return ("ACFAIL asserted");
2409 default:
2410 return ("Unknown error");
2411 }
2412 }
2413