xd.c revision 1.35 1 /* $NetBSD: xd.c,v 1.35 2002/07/23 20:49:54 hannken 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 memcpy(bp->b_data, xd_labeldata, 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 memset(xdc->iopbase, 0, 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 memset(xdc->reqs, 0, 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_alloc(&xdc->sc_wq, BUFQ_FCFS);
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 memset(&xd->sc_dk, 0, 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 memcpy(&xd->dkb, dvmabuf, 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 memcpy(&xd->dkb, addr, sizeof(xd->dkb));
834 splx(s);
835 return 0;
836
837 case DIOCGDINFO: /* get disk label */
838 memcpy(addr, xd->sc_dk.dk_label, 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_PEEK(&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_PUT(&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_PEEK(&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 separately.
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_GET(&xdcsc->sc_wq);
1378 if (bp == NULL)
1379 panic("xdc_startbuf bp");
1380 xdsc = xdcsc->sc_drives[DISKUNIT(bp->b_dev)];
1381 }
1382 partno = DISKPART(bp->b_dev);
1383 #ifdef XDC_DEBUG
1384 printf("xdc_startbuf: %s%c: %s block %d\n", xdsc->sc_dev.dv_xname,
1385 'a' + partno, (bp->b_flags & B_READ) ? "read" : "write", bp->b_blkno);
1386 printf("xdc_startbuf: b_bcount %d, b_data 0x%x\n",
1387 bp->b_bcount, bp->b_data);
1388 #endif
1389
1390 /*
1391 * load request. we have to calculate the correct block number based
1392 * on partition info.
1393 *
1394 * also, note that there are two kinds of buf structures, those with
1395 * B_PHYS set and those without B_PHYS. if B_PHYS is set, then it is
1396 * a raw I/O (to a cdevsw) and we are doing I/O directly to the users'
1397 * buffer which has already been mapped into DVMA space. (Not on sun3)
1398 * However, if B_PHYS is not set, then the buffer is a normal system
1399 * buffer which does *not* live in DVMA space. In that case we call
1400 * dvma_mapin to map it into DVMA space so we can do the DMA to it.
1401 *
1402 * in cases where we do a dvma_mapin, note that iorq points to the buffer
1403 * as mapped into DVMA space, where as the bp->b_data points to its
1404 * non-DVMA mapping.
1405 *
1406 * XXX - On the sun3, B_PHYS does NOT mean the buffer is mapped
1407 * into dvma space, only that it was remapped into the kernel.
1408 * We ALWAYS have to remap the kernel buf into DVMA space.
1409 * (It is done inexpensively, using whole segments!)
1410 */
1411
1412 block = bp->b_blkno + ((partno == RAW_PART) ? 0 :
1413 xdsc->sc_dk.dk_label->d_partitions[partno].p_offset);
1414
1415 dbuf = dvma_mapin(bp->b_data, bp->b_bcount, 0);
1416 if (dbuf == NULL) { /* out of DVMA space */
1417 printf("%s: warning: out of DVMA space\n",
1418 xdcsc->sc_dev.dv_xname);
1419 XDC_FREE(xdcsc, rqno);
1420 BUFQ_PUT(&xdcsc->sc_wq, bp);
1421 return (XD_ERR_FAIL); /* XXX: need some sort of
1422 * call-back scheme here? */
1423 }
1424
1425 /* init iorq and load iopb from it */
1426
1427 xdc_rqinit(iorq, xdcsc, xdsc, XD_SUB_NORM | XD_MODE_VERBO, block,
1428 bp->b_bcount / XDFM_BPS, dbuf, bp);
1429
1430 xdc_rqtopb(iorq, iopb, (bp->b_flags & B_READ) ? XDCMD_RD : XDCMD_WR, 0);
1431
1432 /* Instrumentation. */
1433 disk_busy(&xdsc->sc_dk);
1434
1435 /* now submit [note that xdc_submit_iorq can never fail on NORM reqs] */
1436
1437 xdc_submit_iorq(xdcsc, rqno, XD_SUB_NORM);
1438 return (XD_ERR_AOK);
1439 }
1440
1441
1442 /*
1443 * xdc_submit_iorq: submit an iorq for processing. returns XD_ERR_AOK
1444 * if ok. if it fail returns an error code. type is XD_SUB_*.
1445 *
1446 * note: caller frees iorq in all cases except NORM
1447 *
1448 * return value:
1449 * NORM: XD_AOK (req pending), XD_FAIL (couldn't submit request)
1450 * WAIT: XD_AOK (success), <error-code> (failed)
1451 * POLL: <same as WAIT>
1452 * NOQ : <same as NORM>
1453 *
1454 * there are three sources for i/o requests:
1455 * [1] xdstrategy: normal block I/O, using "struct buf" system.
1456 * [2] autoconfig/crash dump: these are polled I/O requests, no interrupts.
1457 * [3] open/ioctl: these are I/O requests done in the context of a process,
1458 * and the process should block until they are done.
1459 *
1460 * software state is stored in the iorq structure. each iorq has an
1461 * iopb structure. the hardware understands the iopb structure.
1462 * every command must go through an iopb. a 7053 can only handle
1463 * XDC_MAXIOPB (31) active iopbs at one time. iopbs are allocated in
1464 * DVMA space at boot up time. what happens if we run out of iopb's?
1465 * for i/o type [1], the buffers are queued at the "buff" layer and
1466 * picked up later by the interrupt routine. for case [2] the
1467 * programmed i/o driver is called with a special flag that says
1468 * return when one iopb is free. for case [3] the process can sleep
1469 * on the iorq free list until some iopbs are avaliable.
1470 */
1471
1472
1473 int
1474 xdc_submit_iorq(xdcsc, iorqno, type)
1475 struct xdc_softc *xdcsc;
1476 int iorqno;
1477 int type;
1478
1479 {
1480 u_long iopbaddr;
1481 struct xd_iorq *iorq = &xdcsc->reqs[iorqno];
1482
1483 #ifdef XDC_DEBUG
1484 printf("xdc_submit_iorq(%s, no=%d, type=%d)\n", xdcsc->sc_dev.dv_xname,
1485 iorqno, type);
1486 #endif
1487
1488 /* first check and see if controller is busy */
1489 if (xdcsc->xdc->xdc_csr & XDC_ADDING) {
1490 #ifdef XDC_DEBUG
1491 printf("xdc_submit_iorq: XDC not ready (ADDING)\n");
1492 #endif
1493 if (type == XD_SUB_NOQ)
1494 return (XD_ERR_FAIL); /* failed */
1495 XDC_TWAIT(xdcsc, iorqno); /* put at end of waitq */
1496 switch (type) {
1497 case XD_SUB_NORM:
1498 return XD_ERR_AOK; /* success */
1499 case XD_SUB_WAIT:
1500 while (iorq->iopb->done == 0) {
1501 (void) tsleep(iorq, PRIBIO, "xdciorq", 0);
1502 }
1503 return (iorq->errno);
1504 case XD_SUB_POLL:
1505 return (xdc_piodriver(xdcsc, iorqno, 0));
1506 default:
1507 panic("xdc_submit_iorq adding");
1508 }
1509 }
1510 #ifdef XDC_DEBUG
1511 {
1512 u_char *rio = (u_char *) iorq->iopb;
1513 int sz = sizeof(struct xd_iopb), lcv;
1514 printf("%s: aio #%d [",
1515 xdcsc->sc_dev.dv_xname, iorq - xdcsc->reqs);
1516 for (lcv = 0; lcv < sz; lcv++)
1517 printf(" %02x", rio[lcv]);
1518 printf("]\n");
1519 }
1520 #endif /* XDC_DEBUG */
1521
1522 /* controller not busy, start command */
1523 iopbaddr = dvma_kvtopa(iorq->iopb, xdcsc->bustype);
1524 XDC_GO(xdcsc->xdc, iopbaddr); /* go! */
1525 xdcsc->nrun++;
1526 /* command now running, wrap it up */
1527 switch (type) {
1528 case XD_SUB_NORM:
1529 case XD_SUB_NOQ:
1530 return (XD_ERR_AOK); /* success */
1531 case XD_SUB_WAIT:
1532 while (iorq->iopb->done == 0) {
1533 (void) tsleep(iorq, PRIBIO, "xdciorq", 0);
1534 }
1535 return (iorq->errno);
1536 case XD_SUB_POLL:
1537 return (xdc_piodriver(xdcsc, iorqno, 0));
1538 default:
1539 panic("xdc_submit_iorq wrap up");
1540 }
1541 panic("xdc_submit_iorq");
1542 return 0; /* not reached */
1543 }
1544
1545
1546 /*
1547 * xdc_piodriver
1548 *
1549 * programmed i/o driver. this function takes over the computer
1550 * and drains off all i/o requests. it returns the status of the iorq
1551 * the caller is interesting in. if freeone is true, then it returns
1552 * when there is a free iorq.
1553 */
1554 int
1555 xdc_piodriver(xdcsc, iorqno, freeone)
1556 struct xdc_softc *xdcsc;
1557 int iorqno;
1558 int freeone;
1559
1560 {
1561 int nreset = 0;
1562 int retval = 0;
1563 u_long count;
1564 struct xdc *xdc = xdcsc->xdc;
1565 #ifdef XDC_DEBUG
1566 printf("xdc_piodriver(%s, %d, freeone=%d)\n", xdcsc->sc_dev.dv_xname,
1567 iorqno, freeone);
1568 #endif
1569
1570 while (xdcsc->nwait || xdcsc->nrun) {
1571 #ifdef XDC_DEBUG
1572 printf("xdc_piodriver: wait=%d, run=%d\n",
1573 xdcsc->nwait, xdcsc->nrun);
1574 #endif
1575 XDC_WAIT(xdc, count, XDC_MAXTIME, (XDC_REMIOPB | XDC_F_ERROR));
1576 #ifdef XDC_DEBUG
1577 printf("xdc_piodriver: done wait with count = %d\n", count);
1578 #endif
1579 /* we expect some progress soon */
1580 if (count == 0 && nreset >= 2) {
1581 xdc_reset(xdcsc, 0, XD_RSET_ALL, XD_ERR_FAIL, 0);
1582 #ifdef XDC_DEBUG
1583 printf("xdc_piodriver: timeout\n");
1584 #endif
1585 return (XD_ERR_FAIL);
1586 }
1587 if (count == 0) {
1588 if (xdc_reset(xdcsc, 0,
1589 (nreset++ == 0) ? XD_RSET_NONE : iorqno,
1590 XD_ERR_FAIL,
1591 0) == XD_ERR_FAIL)
1592 return (XD_ERR_FAIL); /* flushes all but POLL
1593 * requests, resets */
1594 continue;
1595 }
1596 xdc_remove_iorq(xdcsc); /* could resubmit request */
1597 if (freeone) {
1598 if (xdcsc->nrun < XDC_MAXIOPB) {
1599 #ifdef XDC_DEBUG
1600 printf("xdc_piodriver: done: one free\n");
1601 #endif
1602 return (XD_ERR_AOK);
1603 }
1604 continue; /* don't xdc_start */
1605 }
1606 xdc_start(xdcsc, XDC_MAXIOPB);
1607 }
1608
1609 /* get return value */
1610
1611 retval = xdcsc->reqs[iorqno].errno;
1612
1613 #ifdef XDC_DEBUG
1614 printf("xdc_piodriver: done, retval = 0x%x (%s)\n",
1615 xdcsc->reqs[iorqno].errno, xdc_e2str(xdcsc->reqs[iorqno].errno));
1616 #endif
1617
1618 /* now that we've drained everything, start up any bufs that have
1619 * queued */
1620
1621 while (xdcsc->nfree > 0 && BUFQ_PEEK(&xdcsc->sc_wq) != NULL)
1622 if (xdc_startbuf(xdcsc, NULL, NULL) != XD_ERR_AOK)
1623 break;
1624
1625 return (retval);
1626 }
1627
1628 /*
1629 * xdc_reset: reset one drive. NOTE: assumes xdc was just reset.
1630 * we steal iopb[0] for this, but we put it back when we are done.
1631 */
1632 void
1633 xdc_xdreset(xdcsc, xdsc)
1634 struct xdc_softc *xdcsc;
1635 struct xd_softc *xdsc;
1636
1637 {
1638 struct xd_iopb tmpiopb;
1639 u_long addr;
1640 int del;
1641 memcpy(&tmpiopb, xdcsc->iopbase, sizeof(tmpiopb));
1642 memset(xdcsc->iopbase, 0, sizeof(tmpiopb));
1643 xdcsc->iopbase->comm = XDCMD_RST;
1644 xdcsc->iopbase->unit = xdsc->xd_drive;
1645 addr = (u_long) xdcsc->dvmaiopb;
1646 XDC_GO(xdcsc->xdc, addr); /* go! */
1647 XDC_WAIT(xdcsc->xdc, del, XDC_RESETUSEC, XDC_REMIOPB);
1648 if (del <= 0 || xdcsc->iopbase->errs) {
1649 printf("%s: off-line: %s\n", xdcsc->sc_dev.dv_xname,
1650 xdc_e2str(xdcsc->iopbase->errno));
1651 xdcsc->xdc->xdc_csr = XDC_RESET;
1652 XDC_WAIT(xdcsc->xdc, del, XDC_RESETUSEC, XDC_RESET);
1653 if (del <= 0)
1654 panic("xdc_reset");
1655 } else {
1656 xdcsc->xdc->xdc_csr = XDC_CLRRIO; /* clear RIO */
1657 }
1658 memcpy(xdcsc->iopbase, &tmpiopb, sizeof(tmpiopb));
1659 }
1660
1661
1662 /*
1663 * xdc_reset: reset everything: requests are marked as errors except
1664 * a polled request (which is resubmitted)
1665 */
1666 int
1667 xdc_reset(xdcsc, quiet, blastmode, error, xdsc)
1668 struct xdc_softc *xdcsc;
1669 int quiet, blastmode, error;
1670 struct xd_softc *xdsc;
1671
1672 {
1673 int del = 0, lcv, retval = XD_ERR_AOK;
1674 int oldfree = xdcsc->nfree;
1675 struct xd_iorq *iorq;
1676
1677 /* soft reset hardware */
1678
1679 if (!quiet)
1680 printf("%s: soft reset\n", xdcsc->sc_dev.dv_xname);
1681 xdcsc->xdc->xdc_csr = XDC_RESET;
1682 XDC_WAIT(xdcsc->xdc, del, XDC_RESETUSEC, XDC_RESET);
1683 if (del <= 0) {
1684 blastmode = XD_RSET_ALL; /* dead, flush all requests */
1685 retval = XD_ERR_FAIL;
1686 }
1687 if (xdsc)
1688 xdc_xdreset(xdcsc, xdsc);
1689
1690 /* fix queues based on "blast-mode" */
1691
1692 for (lcv = 0; lcv < XDC_MAXIOPB; lcv++) {
1693 iorq = &xdcsc->reqs[lcv];
1694
1695 if (XD_STATE(iorq->mode) != XD_SUB_POLL &&
1696 XD_STATE(iorq->mode) != XD_SUB_WAIT &&
1697 XD_STATE(iorq->mode) != XD_SUB_NORM)
1698 /* is it active? */
1699 continue;
1700
1701 xdcsc->nrun--; /* it isn't running any more */
1702 if (blastmode == XD_RSET_ALL || blastmode != lcv) {
1703 /* failed */
1704 iorq->errno = error;
1705 xdcsc->iopbase[lcv].done = xdcsc->iopbase[lcv].errs = 1;
1706 switch (XD_STATE(iorq->mode)) {
1707 case XD_SUB_NORM:
1708 iorq->buf->b_error = EIO;
1709 iorq->buf->b_flags |= B_ERROR;
1710 iorq->buf->b_resid =
1711 iorq->sectcnt * XDFM_BPS;
1712 /* Sun3: map/unmap regardless of B_PHYS */
1713 dvma_mapout(iorq->dbufbase,
1714 iorq->buf->b_bcount);
1715 disk_unbusy(&iorq->xd->sc_dk,
1716 (iorq->buf->b_bcount - iorq->buf->b_resid));
1717 biodone(iorq->buf);
1718 XDC_FREE(xdcsc, lcv); /* add to free list */
1719 break;
1720 case XD_SUB_WAIT:
1721 wakeup(iorq);
1722 case XD_SUB_POLL:
1723 xdcsc->ndone++;
1724 iorq->mode = XD_NEWSTATE(iorq->mode, XD_SUB_DONE);
1725 break;
1726 }
1727
1728 } else {
1729
1730 /* resubmit, put at front of wait queue */
1731 XDC_HWAIT(xdcsc, lcv);
1732 }
1733 }
1734
1735 /*
1736 * now, if stuff is waiting, start it.
1737 * since we just reset it should go
1738 */
1739 xdc_start(xdcsc, XDC_MAXIOPB);
1740
1741 /* ok, we did it */
1742 if (oldfree == 0 && xdcsc->nfree)
1743 wakeup(&xdcsc->nfree);
1744
1745 #ifdef XDC_DIAG
1746 del = xdcsc->nwait + xdcsc->nrun + xdcsc->nfree + xdcsc->ndone;
1747 if (del != XDC_MAXIOPB)
1748 printf("%s: diag: xdc_reset miscount (%d should be %d)!\n",
1749 xdcsc->sc_dev.dv_xname, del, XDC_MAXIOPB);
1750 else
1751 if (xdcsc->ndone > XDC_MAXIOPB - XDC_SUBWAITLIM)
1752 printf("%s: diag: lots of done jobs (%d)\n",
1753 xdcsc->sc_dev.dv_xname, xdcsc->ndone);
1754 #endif
1755 printf("RESET DONE\n");
1756 return (retval);
1757 }
1758
1759 /*
1760 * xdc_start: start all waiting buffers
1761 */
1762 void
1763 xdc_start(xdcsc, maxio)
1764 struct xdc_softc *xdcsc;
1765 int maxio;
1766
1767 {
1768 int rqno;
1769 while (maxio && xdcsc->nwait &&
1770 (xdcsc->xdc->xdc_csr & XDC_ADDING) == 0) {
1771 XDC_GET_WAITER(xdcsc, rqno); /* note: rqno is an "out"
1772 * param */
1773 if (xdc_submit_iorq(xdcsc, rqno, XD_SUB_NOQ) != XD_ERR_AOK)
1774 panic("xdc_start"); /* should never happen */
1775 maxio--;
1776 }
1777 }
1778
1779 /*
1780 * xdc_remove_iorq: remove "done" IOPB's.
1781 */
1782 int
1783 xdc_remove_iorq(xdcsc)
1784 struct xdc_softc *xdcsc;
1785
1786 {
1787 int errno, rqno, comm, errs;
1788 struct xdc *xdc = xdcsc->xdc;
1789 struct xd_iopb *iopb;
1790 struct xd_iorq *iorq;
1791 struct buf *bp;
1792
1793 if (xdc->xdc_csr & XDC_F_ERROR) {
1794 /*
1795 * FATAL ERROR: should never happen under normal use. This
1796 * error is so bad, you can't even tell which IOPB is bad, so
1797 * we dump them all.
1798 */
1799 errno = xdc->xdc_f_err;
1800 printf("%s: fatal error 0x%02x: %s\n", xdcsc->sc_dev.dv_xname,
1801 errno, xdc_e2str(errno));
1802 if (xdc_reset(xdcsc, 0, XD_RSET_ALL, errno, 0) != XD_ERR_AOK) {
1803 printf("%s: soft reset failed!\n",
1804 xdcsc->sc_dev.dv_xname);
1805 panic("xdc_remove_iorq: controller DEAD");
1806 }
1807 return (XD_ERR_AOK);
1808 }
1809
1810 /*
1811 * get iopb that is done
1812 *
1813 * hmm... I used to read the address of the done IOPB off the VME
1814 * registers and calculate the rqno directly from that. that worked
1815 * until I started putting a load on the controller. when loaded, i
1816 * would get interrupts but neither the REMIOPB or F_ERROR bits would
1817 * be set, even after DELAY'ing a while! later on the timeout
1818 * routine would detect IOPBs that were marked "running" but their
1819 * "done" bit was set. rather than dealing directly with this
1820 * problem, it is just easier to look at all running IOPB's for the
1821 * done bit.
1822 */
1823 if (xdc->xdc_csr & XDC_REMIOPB) {
1824 xdc->xdc_csr = XDC_CLRRIO;
1825 }
1826
1827 for (rqno = 0; rqno < XDC_MAXIOPB; rqno++) {
1828 iorq = &xdcsc->reqs[rqno];
1829 if (iorq->mode == 0 || XD_STATE(iorq->mode) == XD_SUB_DONE)
1830 continue; /* free, or done */
1831 iopb = &xdcsc->iopbase[rqno];
1832 if (iopb->done == 0)
1833 continue; /* not done yet */
1834
1835 #ifdef XDC_DEBUG
1836 {
1837 u_char *rio = (u_char *) iopb;
1838 int sz = sizeof(struct xd_iopb), lcv;
1839 printf("%s: rio #%d [", xdcsc->sc_dev.dv_xname, rqno);
1840 for (lcv = 0; lcv < sz; lcv++)
1841 printf(" %02x", rio[lcv]);
1842 printf("]\n");
1843 }
1844 #endif /* XDC_DEBUG */
1845
1846 xdcsc->nrun--;
1847
1848 comm = iopb->comm;
1849 errs = iopb->errs;
1850
1851 if (errs)
1852 iorq->errno = iopb->errno;
1853 else
1854 iorq->errno = 0;
1855
1856 /* handle non-fatal errors */
1857
1858 if (errs &&
1859 xdc_error(xdcsc, iorq, iopb, rqno, comm) == XD_ERR_AOK)
1860 continue; /* AOK: we resubmitted it */
1861
1862
1863 /* this iorq is now done (hasn't been restarted or anything) */
1864
1865 if ((iorq->mode & XD_MODE_VERBO) && iorq->lasterror)
1866 xdc_perror(iorq, iopb, 0);
1867
1868 /* now, if read/write check to make sure we got all the data
1869 * we needed. (this may not be the case if we got an error in
1870 * the middle of a multisector request). */
1871
1872 if ((iorq->mode & XD_MODE_B144) != 0 && errs == 0 &&
1873 (comm == XDCMD_RD || comm == XDCMD_WR)) {
1874 /* we just successfully processed a bad144 sector
1875 * note: if we are in bad 144 mode, the pointers have
1876 * been advanced already (see above) and are pointing
1877 * at the bad144 sector. to exit bad144 mode, we
1878 * must advance the pointers 1 sector and issue a new
1879 * request if there are still sectors left to process
1880 *
1881 */
1882 XDC_ADVANCE(iorq, 1); /* advance 1 sector */
1883
1884 /* exit b144 mode */
1885 iorq->mode = iorq->mode & (~XD_MODE_B144);
1886
1887 if (iorq->sectcnt) { /* more to go! */
1888 iorq->lasterror = iorq->errno = iopb->errno = 0;
1889 iopb->errs = iopb->done = 0;
1890 iorq->tries = 0;
1891 iopb->sectcnt = iorq->sectcnt;
1892 iopb->cylno = iorq->blockno /
1893 iorq->xd->sectpercyl;
1894 iopb->headno =
1895 (iorq->blockno / iorq->xd->nhead) %
1896 iorq->xd->nhead;
1897 iopb->sectno = iorq->blockno % XDFM_BPS;
1898 iopb->daddr =
1899 dvma_kvtopa(iorq->dbuf, xdcsc->bustype);
1900 XDC_HWAIT(xdcsc, rqno);
1901 xdc_start(xdcsc, 1); /* resubmit */
1902 continue;
1903 }
1904 }
1905 /* final cleanup, totally done with this request */
1906
1907 switch (XD_STATE(iorq->mode)) {
1908 case XD_SUB_NORM:
1909 bp = iorq->buf;
1910 if (errs) {
1911 bp->b_error = EIO;
1912 bp->b_flags |= B_ERROR;
1913 bp->b_resid = iorq->sectcnt * XDFM_BPS;
1914 } else {
1915 bp->b_resid = 0; /* done */
1916 }
1917 /* Sun3: map/unmap regardless of B_PHYS */
1918 dvma_mapout(iorq->dbufbase,
1919 iorq->buf->b_bcount);
1920 disk_unbusy(&iorq->xd->sc_dk,
1921 (bp->b_bcount - bp->b_resid));
1922 XDC_FREE(xdcsc, rqno);
1923 biodone(bp);
1924 break;
1925 case XD_SUB_WAIT:
1926 iorq->mode = XD_NEWSTATE(iorq->mode, XD_SUB_DONE);
1927 xdcsc->ndone++;
1928 wakeup(iorq);
1929 break;
1930 case XD_SUB_POLL:
1931 iorq->mode = XD_NEWSTATE(iorq->mode, XD_SUB_DONE);
1932 xdcsc->ndone++;
1933 break;
1934 }
1935 }
1936
1937 return (XD_ERR_AOK);
1938 }
1939
1940 /*
1941 * xdc_perror: print error.
1942 * - if still_trying is true: we got an error, retried and got a
1943 * different error. in that case lasterror is the old error,
1944 * and errno is the new one.
1945 * - if still_trying is not true, then if we ever had an error it
1946 * is in lasterror. also, if iorq->errno == 0, then we recovered
1947 * from that error (otherwise iorq->errno == iorq->lasterror).
1948 */
1949 void
1950 xdc_perror(iorq, iopb, still_trying)
1951 struct xd_iorq *iorq;
1952 struct xd_iopb *iopb;
1953 int still_trying;
1954
1955 {
1956
1957 int error = iorq->lasterror;
1958
1959 printf("%s", (iorq->xd) ?
1960 iorq->xd->sc_dev.dv_xname :
1961 iorq->xdc->sc_dev.dv_xname);
1962 if (iorq->buf)
1963 printf("%c: ", 'a' + DISKPART(iorq->buf->b_dev));
1964 if (iopb->comm == XDCMD_RD || iopb->comm == XDCMD_WR)
1965 printf("%s %d/%d/%d: ",
1966 (iopb->comm == XDCMD_RD) ? "read" : "write",
1967 iopb->cylno, iopb->headno, iopb->sectno);
1968 printf("%s", xdc_e2str(error));
1969
1970 if (still_trying)
1971 printf(" [still trying, new error=%s]", xdc_e2str(iorq->errno));
1972 else
1973 if (iorq->errno == 0)
1974 printf(" [recovered in %d tries]", iorq->tries);
1975
1976 printf("\n");
1977 }
1978
1979 /*
1980 * xdc_error: non-fatal error encountered... recover.
1981 * return AOK if resubmitted, return FAIL if this iopb is done
1982 */
1983 int
1984 xdc_error(xdcsc, iorq, iopb, rqno, comm)
1985 struct xdc_softc *xdcsc;
1986 struct xd_iorq *iorq;
1987 struct xd_iopb *iopb;
1988 int rqno, comm;
1989
1990 {
1991 int errno = iorq->errno;
1992 int erract = errno & XD_ERA_MASK;
1993 int oldmode, advance, i;
1994
1995 if (erract == XD_ERA_RSET) { /* some errors require a reset */
1996 oldmode = iorq->mode;
1997 iorq->mode = XD_SUB_DONE | (~XD_SUB_MASK & oldmode);
1998 xdcsc->ndone++;
1999 /* make xdc_start ignore us */
2000 xdc_reset(xdcsc, 1, XD_RSET_NONE, errno, iorq->xd);
2001 iorq->mode = oldmode;
2002 xdcsc->ndone--;
2003 }
2004 /* check for read/write to a sector in bad144 table if bad: redirect
2005 * request to bad144 area */
2006
2007 if ((comm == XDCMD_RD || comm == XDCMD_WR) &&
2008 (iorq->mode & XD_MODE_B144) == 0) {
2009 advance = iorq->sectcnt - iopb->sectcnt;
2010 XDC_ADVANCE(iorq, advance);
2011 if ((i = isbad(&iorq->xd->dkb, iorq->blockno / iorq->xd->sectpercyl,
2012 (iorq->blockno / iorq->xd->nsect) % iorq->xd->nhead,
2013 iorq->blockno % iorq->xd->nsect)) != -1) {
2014 iorq->mode |= XD_MODE_B144; /* enter bad144 mode &
2015 * redirect */
2016 iopb->errno = iopb->done = iopb->errs = 0;
2017 iopb->sectcnt = 1;
2018 iopb->cylno = (iorq->xd->ncyl + iorq->xd->acyl) - 2;
2019 /* second to last acyl */
2020 i = iorq->xd->sectpercyl - 1 - i; /* follow bad144
2021 * standard */
2022 iopb->headno = i / iorq->xd->nhead;
2023 iopb->sectno = i % iorq->xd->nhead;
2024 XDC_HWAIT(xdcsc, rqno);
2025 xdc_start(xdcsc, 1); /* resubmit */
2026 return (XD_ERR_AOK); /* recovered! */
2027 }
2028 }
2029
2030 /*
2031 * it isn't a bad144 sector, must be real error! see if we can retry
2032 * it?
2033 */
2034 if ((iorq->mode & XD_MODE_VERBO) && iorq->lasterror)
2035 xdc_perror(iorq, iopb, 1); /* inform of error state
2036 * change */
2037 iorq->lasterror = errno;
2038
2039 if ((erract == XD_ERA_RSET || erract == XD_ERA_HARD)
2040 && iorq->tries < XDC_MAXTRIES) { /* retry? */
2041 iorq->tries++;
2042 iorq->errno = iopb->errno = iopb->done = iopb->errs = 0;
2043 XDC_HWAIT(xdcsc, rqno);
2044 xdc_start(xdcsc, 1); /* restart */
2045 return (XD_ERR_AOK); /* recovered! */
2046 }
2047
2048 /* failed to recover from this error */
2049 return (XD_ERR_FAIL);
2050 }
2051
2052 /*
2053 * xdc_tick: make sure xd is still alive and ticking (err, kicking).
2054 */
2055 void
2056 xdc_tick(arg)
2057 void *arg;
2058
2059 {
2060 struct xdc_softc *xdcsc = arg;
2061 int lcv, s, reset = 0;
2062 #ifdef XDC_DIAG
2063 int wait, run, free, done, whd = 0;
2064 u_char fqc[XDC_MAXIOPB], wqc[XDC_MAXIOPB], mark[XDC_MAXIOPB];
2065 s = splbio();
2066 wait = xdcsc->nwait;
2067 run = xdcsc->nrun;
2068 free = xdcsc->nfree;
2069 done = xdcsc->ndone;
2070 memcpy(wqc, xdcsc->waitq, sizeof(wqc));
2071 memcpy(fqc, xdcsc->freereq, sizeof(fqc));
2072 splx(s);
2073 if (wait + run + free + done != XDC_MAXIOPB) {
2074 printf("%s: diag: IOPB miscount (got w/f/r/d %d/%d/%d/%d, wanted %d)\n",
2075 xdcsc->sc_dev.dv_xname, wait, free, run, done, XDC_MAXIOPB);
2076 memset(mark, 0, sizeof(mark));
2077 printf("FREE: ");
2078 for (lcv = free; lcv > 0; lcv--) {
2079 printf("%d ", fqc[lcv - 1]);
2080 mark[fqc[lcv - 1]] = 1;
2081 }
2082 printf("\nWAIT: ");
2083 lcv = wait;
2084 while (lcv > 0) {
2085 printf("%d ", wqc[whd]);
2086 mark[wqc[whd]] = 1;
2087 whd = (whd + 1) % XDC_MAXIOPB;
2088 lcv--;
2089 }
2090 printf("\n");
2091 for (lcv = 0; lcv < XDC_MAXIOPB; lcv++) {
2092 if (mark[lcv] == 0)
2093 printf("MARK: running %d: mode %d done %d errs %d errno 0x%x ttl %d buf %p\n",
2094 lcv, xdcsc->reqs[lcv].mode,
2095 xdcsc->iopbase[lcv].done,
2096 xdcsc->iopbase[lcv].errs,
2097 xdcsc->iopbase[lcv].errno,
2098 xdcsc->reqs[lcv].ttl,
2099 xdcsc->reqs[lcv].buf);
2100 }
2101 } else
2102 if (done > XDC_MAXIOPB - XDC_SUBWAITLIM)
2103 printf("%s: diag: lots of done jobs (%d)\n",
2104 xdcsc->sc_dev.dv_xname, done);
2105
2106 #endif
2107 #ifdef XDC_DEBUG
2108 printf("%s: tick: csr 0x%x, w/f/r/d %d/%d/%d/%d\n",
2109 xdcsc->sc_dev.dv_xname,
2110 xdcsc->xdc->xdc_csr, xdcsc->nwait, xdcsc->nfree, xdcsc->nrun,
2111 xdcsc->ndone);
2112 for (lcv = 0; lcv < XDC_MAXIOPB; lcv++) {
2113 if (xdcsc->reqs[lcv].mode)
2114 printf("running %d: mode %d done %d errs %d errno 0x%x\n",
2115 lcv,
2116 xdcsc->reqs[lcv].mode, xdcsc->iopbase[lcv].done,
2117 xdcsc->iopbase[lcv].errs, xdcsc->iopbase[lcv].errno);
2118 }
2119 #endif
2120
2121 /* reduce ttl for each request if one goes to zero, reset xdc */
2122 s = splbio();
2123 for (lcv = 0; lcv < XDC_MAXIOPB; lcv++) {
2124 if (xdcsc->reqs[lcv].mode == 0 ||
2125 XD_STATE(xdcsc->reqs[lcv].mode) == XD_SUB_DONE)
2126 continue;
2127 xdcsc->reqs[lcv].ttl--;
2128 if (xdcsc->reqs[lcv].ttl == 0)
2129 reset = 1;
2130 }
2131 if (reset) {
2132 printf("%s: watchdog timeout\n", xdcsc->sc_dev.dv_xname);
2133 xdc_reset(xdcsc, 0, XD_RSET_NONE, XD_ERR_FAIL, NULL);
2134 }
2135 splx(s);
2136
2137 /* until next time */
2138
2139 callout_reset(&xdcsc->sc_tick_ch, XDC_TICKCNT, xdc_tick, xdcsc);
2140 }
2141
2142 /*
2143 * xdc_ioctlcmd: this function provides a user level interface to the
2144 * controller via ioctl. this allows "format" programs to be written
2145 * in user code, and is also useful for some debugging. we return
2146 * an error code. called at user priority.
2147 */
2148 int
2149 xdc_ioctlcmd(xd, dev, xio)
2150 struct xd_softc *xd;
2151 dev_t dev;
2152 struct xd_iocmd *xio;
2153
2154 {
2155 int s, err, rqno;
2156 caddr_t dvmabuf = NULL;
2157 struct xdc_softc *xdcsc;
2158
2159 /* check sanity of requested command */
2160
2161 switch (xio->cmd) {
2162
2163 case XDCMD_NOP: /* no op: everything should be zero */
2164 if (xio->subfn || xio->dptr || xio->dlen ||
2165 xio->block || xio->sectcnt)
2166 return (EINVAL);
2167 break;
2168
2169 case XDCMD_RD: /* read / write sectors (up to XD_IOCMD_MAXS) */
2170 case XDCMD_WR:
2171 if (xio->subfn || xio->sectcnt > XD_IOCMD_MAXS ||
2172 xio->sectcnt * XDFM_BPS != xio->dlen || xio->dptr == NULL)
2173 return (EINVAL);
2174 break;
2175
2176 case XDCMD_SK: /* seek: doesn't seem useful to export this */
2177 return (EINVAL);
2178
2179 case XDCMD_WRP: /* write parameters */
2180 return (EINVAL);/* not useful, except maybe drive
2181 * parameters... but drive parameters should
2182 * go via disklabel changes */
2183
2184 case XDCMD_RDP: /* read parameters */
2185 if (xio->subfn != XDFUN_DRV ||
2186 xio->dlen || xio->block || xio->dptr)
2187 return (EINVAL); /* allow read drive params to
2188 * get hw_spt */
2189 xio->sectcnt = xd->hw_spt; /* we already know the answer */
2190 return (0);
2191 break;
2192
2193 case XDCMD_XRD: /* extended read/write */
2194 case XDCMD_XWR:
2195
2196 switch (xio->subfn) {
2197
2198 case XDFUN_THD:/* track headers */
2199 if (xio->sectcnt != xd->hw_spt ||
2200 (xio->block % xd->nsect) != 0 ||
2201 xio->dlen != XD_IOCMD_HSZ * xd->hw_spt ||
2202 xio->dptr == NULL)
2203 return (EINVAL);
2204 xio->sectcnt = 0;
2205 break;
2206
2207 case XDFUN_FMT:/* NOTE: also XDFUN_VFY */
2208 if (xio->cmd == XDCMD_XRD)
2209 return (EINVAL); /* no XDFUN_VFY */
2210 if (xio->sectcnt || xio->dlen ||
2211 (xio->block % xd->nsect) != 0 || xio->dptr)
2212 return (EINVAL);
2213 break;
2214
2215 case XDFUN_HDR:/* header, header verify, data, data ECC */
2216 return (EINVAL); /* not yet */
2217
2218 case XDFUN_DM: /* defect map */
2219 case XDFUN_DMX:/* defect map (alternate location) */
2220 if (xio->sectcnt || xio->dlen != XD_IOCMD_DMSZ ||
2221 (xio->block % xd->nsect) != 0 || xio->dptr == NULL)
2222 return (EINVAL);
2223 break;
2224
2225 default:
2226 return (EINVAL);
2227 }
2228 break;
2229
2230 case XDCMD_TST: /* diagnostics */
2231 return (EINVAL);
2232
2233 default:
2234 return (EINVAL);/* ??? */
2235 }
2236
2237 /* create DVMA buffer for request if needed */
2238
2239 if (xio->dlen) {
2240 dvmabuf = dvma_malloc(xio->dlen);
2241 if (xio->cmd == XDCMD_WR || xio->cmd == XDCMD_XWR) {
2242 err = copyin(xio->dptr, dvmabuf, xio->dlen);
2243 if (err) {
2244 dvma_free(dvmabuf, xio->dlen);
2245 return (err);
2246 }
2247 }
2248 }
2249 /* do it! */
2250
2251 err = 0;
2252 xdcsc = xd->parent;
2253 s = splbio();
2254 rqno = xdc_cmd(xdcsc, xio->cmd, xio->subfn, xd->xd_drive, xio->block,
2255 xio->sectcnt, dvmabuf, XD_SUB_WAIT);
2256 if (rqno == XD_ERR_FAIL) {
2257 err = EIO;
2258 goto done;
2259 }
2260 xio->errno = xdcsc->reqs[rqno].errno;
2261 xio->tries = xdcsc->reqs[rqno].tries;
2262 XDC_DONE(xdcsc, rqno, err);
2263
2264 if (xio->cmd == XDCMD_RD || xio->cmd == XDCMD_XRD)
2265 err = copyout(dvmabuf, xio->dptr, xio->dlen);
2266
2267 done:
2268 splx(s);
2269 if (dvmabuf)
2270 dvma_free(dvmabuf, xio->dlen);
2271 return (err);
2272 }
2273
2274 /*
2275 * xdc_e2str: convert error code number into an error string
2276 */
2277 char *
2278 xdc_e2str(no)
2279 int no;
2280 {
2281 switch (no) {
2282 case XD_ERR_FAIL:
2283 return ("Software fatal error");
2284 case XD_ERR_AOK:
2285 return ("Successful completion");
2286 case XD_ERR_ICYL:
2287 return ("Illegal cylinder address");
2288 case XD_ERR_IHD:
2289 return ("Illegal head address");
2290 case XD_ERR_ISEC:
2291 return ("Illgal sector address");
2292 case XD_ERR_CZER:
2293 return ("Count zero");
2294 case XD_ERR_UIMP:
2295 return ("Unimplemented command");
2296 case XD_ERR_IF1:
2297 return ("Illegal field length 1");
2298 case XD_ERR_IF2:
2299 return ("Illegal field length 2");
2300 case XD_ERR_IF3:
2301 return ("Illegal field length 3");
2302 case XD_ERR_IF4:
2303 return ("Illegal field length 4");
2304 case XD_ERR_IF5:
2305 return ("Illegal field length 5");
2306 case XD_ERR_IF6:
2307 return ("Illegal field length 6");
2308 case XD_ERR_IF7:
2309 return ("Illegal field length 7");
2310 case XD_ERR_ISG:
2311 return ("Illegal scatter/gather length");
2312 case XD_ERR_ISPT:
2313 return ("Not enough sectors per track");
2314 case XD_ERR_ALGN:
2315 return ("Next IOPB address alignment error");
2316 case XD_ERR_SGAL:
2317 return ("Scatter/gather address alignment error");
2318 case XD_ERR_SGEC:
2319 return ("Scatter/gather with auto-ECC");
2320 case XD_ERR_SECC:
2321 return ("Soft ECC corrected");
2322 case XD_ERR_SIGN:
2323 return ("ECC ignored");
2324 case XD_ERR_ASEK:
2325 return ("Auto-seek retry recovered");
2326 case XD_ERR_RTRY:
2327 return ("Soft retry recovered");
2328 case XD_ERR_HECC:
2329 return ("Hard data ECC");
2330 case XD_ERR_NHDR:
2331 return ("Header not found");
2332 case XD_ERR_NRDY:
2333 return ("Drive not ready");
2334 case XD_ERR_TOUT:
2335 return ("Operation timeout");
2336 case XD_ERR_VTIM:
2337 return ("VMEDMA timeout");
2338 case XD_ERR_DSEQ:
2339 return ("Disk sequencer error");
2340 case XD_ERR_HDEC:
2341 return ("Header ECC error");
2342 case XD_ERR_RVFY:
2343 return ("Read verify");
2344 case XD_ERR_VFER:
2345 return ("Fatail VMEDMA error");
2346 case XD_ERR_VBUS:
2347 return ("VMEbus error");
2348 case XD_ERR_DFLT:
2349 return ("Drive faulted");
2350 case XD_ERR_HECY:
2351 return ("Header error/cyliner");
2352 case XD_ERR_HEHD:
2353 return ("Header error/head");
2354 case XD_ERR_NOCY:
2355 return ("Drive not on-cylinder");
2356 case XD_ERR_SEEK:
2357 return ("Seek error");
2358 case XD_ERR_ILSS:
2359 return ("Illegal sector size");
2360 case XD_ERR_SEC:
2361 return ("Soft ECC");
2362 case XD_ERR_WPER:
2363 return ("Write-protect error");
2364 case XD_ERR_IRAM:
2365 return ("IRAM self test failure");
2366 case XD_ERR_MT3:
2367 return ("Maintenance test 3 failure (DSKCEL RAM)");
2368 case XD_ERR_MT4:
2369 return ("Maintenance test 4 failure (header shift reg)");
2370 case XD_ERR_MT5:
2371 return ("Maintenance test 5 failure (VMEDMA regs)");
2372 case XD_ERR_MT6:
2373 return ("Maintenance test 6 failure (REGCEL chip)");
2374 case XD_ERR_MT7:
2375 return ("Maintenance test 7 failure (buffer parity)");
2376 case XD_ERR_MT8:
2377 return ("Maintenance test 8 failure (disk FIFO)");
2378 case XD_ERR_IOCK:
2379 return ("IOPB checksum miscompare");
2380 case XD_ERR_IODM:
2381 return ("IOPB DMA fatal");
2382 case XD_ERR_IOAL:
2383 return ("IOPB address alignment error");
2384 case XD_ERR_FIRM:
2385 return ("Firmware error");
2386 case XD_ERR_MMOD:
2387 return ("Illegal maintenance mode test number");
2388 case XD_ERR_ACFL:
2389 return ("ACFAIL asserted");
2390 default:
2391 return ("Unknown error");
2392 }
2393 }
2394