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