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