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