xy.c revision 1.64 1 /* $NetBSD: xy.c,v 1.64 2006/05/14 21:47:00 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 y . c x y l o g i c s 4 5 0 / 4 5 1 s m d d r i v e r
37 *
38 * author: Chuck Cranor <chuck (at) ccrc.wustl.edu>
39 * started: 14-Sep-95
40 * references: [1] Xylogics Model 753 User's Manual
41 * part number: 166-753-001, Revision B, May 21, 1988.
42 * "Your Partner For Performance"
43 * [2] other NetBSD disk device drivers
44 * [3] Xylogics Model 450 User's Manual
45 * part number: 166-017-001, Revision B, 1983.
46 * [4] Addendum to Xylogics Model 450 Disk Controller User's
47 * Manual, Jan. 1985.
48 * [5] The 451 Controller, Rev. B3, September 2, 1986.
49 * [6] David Jones <dej (at) achilles.net>'s unfinished 450/451 driver
50 *
51 */
52
53 #include <sys/cdefs.h>
54 __KERNEL_RCSID(0, "$NetBSD: xy.c,v 1.64 2006/05/14 21:47:00 elad Exp $");
55
56 #undef XYC_DEBUG /* full debug */
57 #undef XYC_DIAG /* extra sanity checks */
58 #if defined(DIAGNOSTIC) && !defined(XYC_DIAG)
59 #define XYC_DIAG /* link in with master DIAG option */
60 #endif
61
62 #include <sys/param.h>
63 #include <sys/proc.h>
64 #include <sys/systm.h>
65 #include <sys/kernel.h>
66 #include <sys/file.h>
67 #include <sys/stat.h>
68 #include <sys/ioctl.h>
69 #include <sys/buf.h>
70 #include <sys/bufq.h>
71 #include <sys/uio.h>
72 #include <sys/malloc.h>
73 #include <sys/device.h>
74 #include <sys/disklabel.h>
75 #include <sys/disk.h>
76 #include <sys/syslog.h>
77 #include <sys/dkbad.h>
78 #include <sys/conf.h>
79
80 #include <machine/bus.h>
81 #include <machine/intr.h>
82
83 #if defined(__sparc__) || defined(sun3)
84 #include <dev/sun/disklabel.h>
85 #endif
86
87 #include <dev/vme/vmereg.h>
88 #include <dev/vme/vmevar.h>
89
90 #include <dev/vme/xyreg.h>
91 #include <dev/vme/xyvar.h>
92 #include <dev/vme/xio.h>
93
94 #include "locators.h"
95
96 /*
97 * macros
98 */
99
100 /*
101 * XYC_GO: start iopb ADDR (DVMA addr in a u_long) on XYC
102 */
103 #define XYC_GO(XYC, ADDR) { \
104 u_long addr = (u_long)ADDR; \
105 (XYC)->xyc_addr_lo = ((addr) & 0xff); \
106 (addr) = ((addr) >> 8); \
107 (XYC)->xyc_addr_hi = ((addr) & 0xff); \
108 (addr) = ((addr) >> 8); \
109 (XYC)->xyc_reloc_lo = ((addr) & 0xff); \
110 (addr) = ((addr) >> 8); \
111 (XYC)->xyc_reloc_hi = (addr); \
112 (XYC)->xyc_csr = XYC_GBSY; /* go! */ \
113 }
114
115 /*
116 * XYC_DONE: don't need IORQ, get error code and free (done after xyc_cmd)
117 */
118
119 #define XYC_DONE(SC,ER) { \
120 if ((ER) == XY_ERR_AOK) { \
121 (ER) = (SC)->ciorq->errno; \
122 (SC)->ciorq->mode = XY_SUB_FREE; \
123 wakeup((SC)->ciorq); \
124 } \
125 }
126
127 /*
128 * XYC_ADVANCE: advance iorq's pointers by a number of sectors
129 */
130
131 #define XYC_ADVANCE(IORQ, N) { \
132 if (N) { \
133 (IORQ)->sectcnt -= (N); \
134 (IORQ)->blockno += (N); \
135 (IORQ)->dbuf += ((N)*XYFM_BPS); \
136 } \
137 }
138
139 /*
140 * note - addresses you can sleep on:
141 * [1] & of xy_softc's "state" (waiting for a chance to attach a drive)
142 * [2] & an iorq (waiting for an XY_SUB_WAIT iorq to finish)
143 */
144
145
146 /*
147 * function prototypes
148 * "xyc_*" functions are internal, all others are external interfaces
149 */
150
151 extern int pil_to_vme[]; /* from obio.c */
152
153 /* internals */
154 struct xy_iopb *xyc_chain(struct xyc_softc *, struct xy_iorq *);
155 int xyc_cmd(struct xyc_softc *, int, int, int, int, int, char *, int);
156 const char *xyc_e2str(int);
157 int xyc_entoact(int);
158 int xyc_error(struct xyc_softc *, struct xy_iorq *,
159 struct xy_iopb *, int);
160 int xyc_ioctlcmd(struct xy_softc *, dev_t dev, struct xd_iocmd *);
161 void xyc_perror(struct xy_iorq *, struct xy_iopb *, int);
162 int xyc_piodriver(struct xyc_softc *, struct xy_iorq *);
163 int xyc_remove_iorq(struct xyc_softc *);
164 int xyc_reset(struct xyc_softc *, int, struct xy_iorq *, int,
165 struct xy_softc *);
166 inline void xyc_rqinit(struct xy_iorq *, struct xyc_softc *,
167 struct xy_softc *, int, u_long, int,
168 caddr_t, struct buf *);
169 void xyc_rqtopb(struct xy_iorq *, struct xy_iopb *, int, int);
170 void xyc_start(struct xyc_softc *, struct xy_iorq *);
171 int xyc_startbuf(struct xyc_softc *, struct xy_softc *, struct buf *);
172 int xyc_submit_iorq(struct xyc_softc *, struct xy_iorq *, int);
173 void xyc_tick(void *);
174 int xyc_unbusy(struct xyc *, int);
175 void xyc_xyreset(struct xyc_softc *, struct xy_softc *);
176 int xy_dmamem_alloc(bus_dma_tag_t, bus_dmamap_t, bus_dma_segment_t *,
177 int *, bus_size_t, caddr_t *, bus_addr_t *);
178 void xy_dmamem_free(bus_dma_tag_t, bus_dmamap_t, bus_dma_segment_t *,
179 int, bus_size_t, caddr_t);
180
181 /* machine interrupt hook */
182 int xycintr(void *);
183
184 /* autoconf */
185 int xycmatch(struct device *, struct cfdata *, void *);
186 void xycattach(struct device *, struct device *, void *);
187 int xymatch(struct device *, struct cfdata *, void *);
188 void xyattach(struct device *, struct device *, void *);
189 static int xyc_probe(void *, bus_space_tag_t, bus_space_handle_t);
190
191 static void xydummystrat(struct buf *);
192 int xygetdisklabel(struct xy_softc *, void *);
193
194 /*
195 * cfattach's: device driver interface to autoconfig
196 */
197
198 CFATTACH_DECL(xyc, sizeof(struct xyc_softc),
199 xycmatch, xycattach, NULL, NULL);
200
201 CFATTACH_DECL(xy, sizeof(struct xy_softc),
202 xymatch, xyattach, NULL, NULL);
203
204 extern struct cfdriver xy_cd;
205
206 dev_type_open(xyopen);
207 dev_type_close(xyclose);
208 dev_type_read(xyread);
209 dev_type_write(xywrite);
210 dev_type_ioctl(xyioctl);
211 dev_type_strategy(xystrategy);
212 dev_type_dump(xydump);
213 dev_type_size(xysize);
214
215 const struct bdevsw xy_bdevsw = {
216 xyopen, xyclose, xystrategy, xyioctl, xydump, xysize, D_DISK
217 };
218
219 const struct cdevsw xy_cdevsw = {
220 xyopen, xyclose, xyread, xywrite, xyioctl,
221 nostop, notty, nopoll, nommap, nokqfilter, D_DISK
222 };
223
224 struct xyc_attach_args { /* this is the "aux" args to xyattach */
225 int driveno; /* unit number */
226 int fullmode; /* submit mode */
227 int booting; /* are we booting or not? */
228 };
229
230 /*
231 * dkdriver
232 */
233
234 struct dkdriver xydkdriver = { xystrategy };
235
236 /*
237 * start: disk label fix code (XXX)
238 */
239
240 static void *xy_labeldata;
241
242 static void
243 xydummystrat(bp)
244 struct buf *bp;
245 {
246 if (bp->b_bcount != XYFM_BPS)
247 panic("xydummystrat");
248 bcopy(xy_labeldata, bp->b_data, XYFM_BPS);
249 bp->b_flags |= B_DONE;
250 bp->b_flags &= ~B_BUSY;
251 }
252
253 int
254 xygetdisklabel(xy, b)
255 struct xy_softc *xy;
256 void *b;
257 {
258 const char *err;
259 #if defined(__sparc__) || defined(sun3)
260 struct sun_disklabel *sdl;
261 #endif
262
263 /* We already have the label data in `b'; setup for dummy strategy */
264 xy_labeldata = b;
265
266 /* Required parameter for readdisklabel() */
267 xy->sc_dk.dk_label->d_secsize = XYFM_BPS;
268
269 err = readdisklabel(MAKEDISKDEV(0, device_unit(&xy->sc_dev), RAW_PART),
270 xydummystrat,
271 xy->sc_dk.dk_label, xy->sc_dk.dk_cpulabel);
272 if (err) {
273 printf("%s: %s\n", xy->sc_dev.dv_xname, err);
274 return(XY_ERR_FAIL);
275 }
276
277 #if defined(__sparc__) || defined(sun3)
278 /* Ok, we have the label; fill in `pcyl' if there's SunOS magic */
279 sdl = (struct sun_disklabel *)xy->sc_dk.dk_cpulabel->cd_block;
280 if (sdl->sl_magic == SUN_DKMAGIC) {
281 xy->pcyl = sdl->sl_pcylinders;
282 } else
283 #endif
284 {
285 printf("%s: WARNING: no `pcyl' in disk label.\n",
286 xy->sc_dev.dv_xname);
287 xy->pcyl = xy->sc_dk.dk_label->d_ncylinders +
288 xy->sc_dk.dk_label->d_acylinders;
289 printf("%s: WARNING: guessing pcyl=%d (ncyl+acyl)\n",
290 xy->sc_dev.dv_xname, xy->pcyl);
291 }
292
293 xy->ncyl = xy->sc_dk.dk_label->d_ncylinders;
294 xy->acyl = xy->sc_dk.dk_label->d_acylinders;
295 xy->nhead = xy->sc_dk.dk_label->d_ntracks;
296 xy->nsect = xy->sc_dk.dk_label->d_nsectors;
297 xy->sectpercyl = xy->nhead * xy->nsect;
298 xy->sc_dk.dk_label->d_secsize = XYFM_BPS; /* not handled by
299 * sun->bsd */
300 return(XY_ERR_AOK);
301 }
302
303 /*
304 * end: disk label fix code (XXX)
305 */
306
307 /*
308 * Shorthand for allocating, mapping and loading a DMA buffer
309 */
310 int
311 xy_dmamem_alloc(tag, map, seg, nsegp, len, kvap, dmap)
312 bus_dma_tag_t tag;
313 bus_dmamap_t map;
314 bus_dma_segment_t *seg;
315 int *nsegp;
316 bus_size_t len;
317 caddr_t *kvap;
318 bus_addr_t *dmap;
319 {
320 int nseg;
321 int error;
322
323 if ((error = bus_dmamem_alloc(tag, len, 0, 0,
324 seg, 1, &nseg, BUS_DMA_NOWAIT)) != 0) {
325 return (error);
326 }
327
328 if ((error = bus_dmamem_map(tag, seg, nseg,
329 len, kvap,
330 BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
331 bus_dmamem_free(tag, seg, nseg);
332 return (error);
333 }
334
335 if ((error = bus_dmamap_load(tag, map, *kvap, len, NULL,
336 BUS_DMA_NOWAIT)) != 0) {
337 bus_dmamem_unmap(tag, *kvap, len);
338 bus_dmamem_free(tag, seg, nseg);
339 return (error);
340 }
341
342 *dmap = map->dm_segs[0].ds_addr;
343 *nsegp = nseg;
344 return (0);
345 }
346
347 void
348 xy_dmamem_free(tag, map, seg, nseg, len, kva)
349 bus_dma_tag_t tag;
350 bus_dmamap_t map;
351 bus_dma_segment_t *seg;
352 int nseg;
353 bus_size_t len;
354 caddr_t kva;
355 {
356
357 bus_dmamap_unload(tag, map);
358 bus_dmamem_unmap(tag, kva, len);
359 bus_dmamem_free(tag, seg, nseg);
360 }
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 * xycmatch: determine if xyc is present or not. we do a
369 * soft reset to detect the xyc.
370 */
371 int
372 xyc_probe(arg, tag, handle)
373 void *arg;
374 bus_space_tag_t tag;
375 bus_space_handle_t handle;
376 {
377 struct xyc *xyc = (void *)handle; /* XXX */
378
379 return ((xyc_unbusy(xyc, XYC_RESETUSEC) != XY_ERR_FAIL) ? 0 : EIO);
380 }
381
382 int xycmatch(parent, cf, aux)
383 struct device *parent;
384 struct cfdata *cf;
385 void *aux;
386 {
387 struct vme_attach_args *va = aux;
388 vme_chipset_tag_t ct = va->va_vct;
389 vme_am_t mod;
390 int error;
391
392 mod = VME_AM_A16 | VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA;
393 if (vme_space_alloc(ct, va->r[0].offset, sizeof(struct xyc), mod))
394 return (0);
395
396 error = vme_probe(ct, va->r[0].offset, sizeof(struct xyc),
397 mod, VME_D16, xyc_probe, 0);
398 vme_space_free(va->va_vct, va->r[0].offset, sizeof(struct xyc), mod);
399
400 return (error == 0);
401 }
402
403 /*
404 * xycattach: attach controller
405 */
406 void
407 xycattach(parent, self, aux)
408 struct device *parent, *self;
409 void *aux;
410
411 {
412 struct xyc_softc *xyc = (void *) self;
413 struct vme_attach_args *va = aux;
414 vme_chipset_tag_t ct = va->va_vct;
415 bus_space_tag_t bt;
416 bus_space_handle_t bh;
417 vme_intr_handle_t ih;
418 vme_am_t mod;
419 struct xyc_attach_args xa;
420 int lcv, res, error;
421 bus_dma_segment_t seg;
422 int rseg;
423 vme_mapresc_t resc;
424 bus_addr_t busaddr;
425
426 /* get addressing and intr level stuff from autoconfig and load it
427 * into our xyc_softc. */
428
429 mod = VME_AM_A16 | VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA;
430
431 if (vme_space_alloc(ct, va->r[0].offset, sizeof(struct xyc), mod))
432 panic("xyc: vme alloc");
433
434 if (vme_space_map(ct, va->r[0].offset, sizeof(struct xyc),
435 mod, VME_D16, 0, &bt, &bh, &resc) != 0)
436 panic("xyc: vme_map");
437
438 xyc->xyc = (struct xyc *) bh; /* XXX */
439 xyc->ipl = va->ilevel;
440 xyc->vector = va->ivector;
441 xyc->no_ols = 0; /* XXX should be from config */
442
443 for (lcv = 0; lcv < XYC_MAXDEV; lcv++)
444 xyc->sc_drives[lcv] = (struct xy_softc *) 0;
445
446 /*
447 * allocate and zero buffers
448 * check boundaries of the KVA's ... all IOPBs must reside in
449 * the same 64K region.
450 */
451
452 /* Get DMA handle for misc. transfers */
453 if ((error = vme_dmamap_create(
454 ct, /* VME chip tag */
455 MAXPHYS, /* size */
456 VME_AM_A24, /* address modifier */
457 VME_D16, /* data size */
458 0, /* swap */
459 1, /* nsegments */
460 MAXPHYS, /* maxsegsz */
461 0, /* boundary */
462 BUS_DMA_NOWAIT,
463 &xyc->auxmap)) != 0) {
464
465 printf("%s: DMA buffer map create error %d\n",
466 xyc->sc_dev.dv_xname, error);
467 return;
468 }
469
470 /* Get DMA handle for mapping iorq descriptors */
471 if ((error = vme_dmamap_create(
472 ct, /* VME chip tag */
473 XYC_MAXIOPB * sizeof(struct xy_iopb),
474 VME_AM_A24, /* address modifier */
475 VME_D16, /* data size */
476 0, /* swap */
477 1, /* nsegments */
478 XYC_MAXIOPB * sizeof(struct xy_iopb),
479 64*1024, /* boundary */
480 BUS_DMA_NOWAIT,
481 &xyc->iopmap)) != 0) {
482
483 printf("%s: DMA buffer map create error %d\n",
484 xyc->sc_dev.dv_xname, error);
485 return;
486 }
487
488 /* Get DMA buffer for iorq descriptors */
489 if ((error = xy_dmamem_alloc(xyc->dmatag, xyc->iopmap, &seg, &rseg,
490 XYC_MAXIOPB * sizeof(struct xy_iopb),
491 (caddr_t *)&xyc->iopbase,
492 &busaddr)) != 0) {
493 printf("%s: DMA buffer alloc error %d\n",
494 xyc->sc_dev.dv_xname, error);
495 return;
496 }
497 xyc->dvmaiopb = (struct xy_iopb *)(u_long)BUS_ADDR_PADDR(busaddr);
498
499 bzero(xyc->iopbase, XYC_MAXIOPB * sizeof(struct xy_iopb));
500
501 xyc->reqs = (struct xy_iorq *)
502 malloc(XYC_MAXIOPB * sizeof(struct xy_iorq),
503 M_DEVBUF, M_NOWAIT|M_ZERO);
504 if (xyc->reqs == NULL)
505 panic("xyc malloc");
506
507 /*
508 * init iorq to iopb pointers, and non-zero fields in the
509 * iopb which never change.
510 */
511
512 for (lcv = 0; lcv < XYC_MAXIOPB; lcv++) {
513 xyc->xy_chain[lcv] = NULL;
514 xyc->reqs[lcv].iopb = &xyc->iopbase[lcv];
515 xyc->reqs[lcv].dmaiopb = &xyc->dvmaiopb[lcv];
516 xyc->iopbase[lcv].asr = 1; /* always the same */
517 xyc->iopbase[lcv].eef = 1; /* always the same */
518 xyc->iopbase[lcv].ecm = XY_ECM; /* always the same */
519 xyc->iopbase[lcv].aud = 1; /* always the same */
520 xyc->iopbase[lcv].relo = 1; /* always the same */
521 xyc->iopbase[lcv].thro = XY_THRO;/* always the same */
522
523 if ((error = vme_dmamap_create(
524 ct, /* VME chip tag */
525 MAXPHYS, /* size */
526 VME_AM_A24, /* address modifier */
527 VME_D16, /* data size */
528 0, /* swap */
529 1, /* nsegments */
530 MAXPHYS, /* maxsegsz */
531 0, /* boundary */
532 BUS_DMA_NOWAIT,
533 &xyc->reqs[lcv].dmamap)) != 0) {
534
535 printf("%s: DMA buffer map create error %d\n",
536 xyc->sc_dev.dv_xname, error);
537 return;
538 }
539 }
540 xyc->ciorq = &xyc->reqs[XYC_CTLIOPB]; /* short hand name */
541 xyc->ciopb = &xyc->iopbase[XYC_CTLIOPB]; /* short hand name */
542 xyc->xy_hand = 0;
543
544 /* read controller parameters and insure we have a 450/451 */
545
546 error = xyc_cmd(xyc, XYCMD_ST, 0, 0, 0, 0, 0, XY_SUB_POLL);
547 res = xyc->ciopb->ctyp;
548 XYC_DONE(xyc, error);
549 if (res != XYCT_450) {
550 if (error)
551 printf(": %s: ", xyc_e2str(error));
552 printf(": doesn't identify as a 450/451\n");
553 return;
554 }
555 printf(": Xylogics 450/451");
556 if (xyc->no_ols)
557 printf(" [OLS disabled]"); /* 450 doesn't overlap seek right */
558 printf("\n");
559 if (error) {
560 printf("%s: error: %s\n", xyc->sc_dev.dv_xname,
561 xyc_e2str(error));
562 return;
563 }
564 if ((xyc->xyc->xyc_csr & XYC_ADRM) == 0) {
565 printf("%s: 24 bit addressing turned off\n",
566 xyc->sc_dev.dv_xname);
567 printf("please set hardware jumpers JM1-JM2=in, JM3-JM4=out\n");
568 printf("to enable 24 bit mode and this driver\n");
569 return;
570 }
571
572 /* link in interrupt with higher level software */
573 vme_intr_map(ct, va->ilevel, va->ivector, &ih);
574 vme_intr_establish(ct, ih, IPL_BIO, xycintr, xyc);
575 evcnt_attach_dynamic(&xyc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
576 xyc->sc_dev.dv_xname, "intr");
577
578 callout_init(&xyc->sc_tick_ch);
579
580 /* now we must look for disks using autoconfig */
581 xa.fullmode = XY_SUB_POLL;
582 xa.booting = 1;
583
584 for (xa.driveno = 0; xa.driveno < XYC_MAXDEV; xa.driveno++)
585 (void) config_found(self, (void *) &xa, NULL);
586
587 /* start the watchdog clock */
588 callout_reset(&xyc->sc_tick_ch, XYC_TICKCNT, xyc_tick, xyc);
589
590 }
591
592 /*
593 * xymatch: probe for disk.
594 *
595 * note: we almost always say disk is present. this allows us to
596 * spin up and configure a disk after the system is booted (we can
597 * call xyattach!).
598 */
599 int
600 xymatch(parent, cf, aux)
601 struct device *parent;
602 struct cfdata *cf;
603 void *aux;
604 {
605 struct xyc_attach_args *xa = aux;
606
607 /* looking for autoconf wildcard or exact match */
608
609 if (cf->cf_loc[XYCCF_DRIVE] != XYCCF_DRIVE_DEFAULT &&
610 cf->cf_loc[XYCCF_DRIVE] != xa->driveno)
611 return 0;
612
613 return 1;
614
615 }
616
617 /*
618 * xyattach: attach a disk. this can be called from autoconf and also
619 * from xyopen/xystrategy.
620 */
621 void
622 xyattach(parent, self, aux)
623 struct device *parent, *self;
624 void *aux;
625
626 {
627 struct xy_softc *xy = (void *) self, *oxy;
628 struct xyc_softc *xyc = (void *) parent;
629 struct xyc_attach_args *xa = aux;
630 int spt, mb, blk, lcv, fmode, s = 0, newstate;
631 struct dkbad *dkb;
632 int rseg, error;
633 bus_dma_segment_t seg;
634 bus_addr_t busaddr;
635 caddr_t dmaddr;
636 caddr_t buf;
637
638 /*
639 * Always re-initialize the disk structure. We want statistics
640 * to start with a clean slate.
641 */
642 bzero(&xy->sc_dk, sizeof(xy->sc_dk));
643 xy->sc_dk.dk_driver = &xydkdriver;
644 xy->sc_dk.dk_name = xy->sc_dev.dv_xname;
645
646 /* if booting, init the xy_softc */
647
648 if (xa->booting) {
649 xy->state = XY_DRIVE_UNKNOWN; /* to start */
650 xy->flags = 0;
651 xy->parent = xyc;
652
653 /* init queue of waiting bufs */
654
655 bufq_alloc(&xy->xyq, "disksort", BUFQ_SORT_RAWBLOCK);
656
657 xy->xyrq = &xyc->reqs[xa->driveno];
658
659 }
660 xy->xy_drive = xa->driveno;
661 fmode = xa->fullmode;
662 xyc->sc_drives[xa->driveno] = xy;
663
664 /* if not booting, make sure we are the only process in the attach for
665 * this drive. if locked out, sleep on it. */
666
667 if (!xa->booting) {
668 s = splbio();
669 while (xy->state == XY_DRIVE_ATTACHING) {
670 if (tsleep(&xy->state, PRIBIO, "xyattach", 0)) {
671 splx(s);
672 return;
673 }
674 }
675 printf("%s at %s",
676 xy->sc_dev.dv_xname, xy->parent->sc_dev.dv_xname);
677 }
678
679 /* we now have control */
680 xy->state = XY_DRIVE_ATTACHING;
681 newstate = XY_DRIVE_UNKNOWN;
682
683 buf = NULL;
684 if ((error = xy_dmamem_alloc(xyc->dmatag, xyc->auxmap, &seg, &rseg,
685 XYFM_BPS,
686 (caddr_t *)&buf,
687 &busaddr)) != 0) {
688 printf("%s: DMA buffer alloc error %d\n",
689 xyc->sc_dev.dv_xname, error);
690 return;
691 }
692 dmaddr = (caddr_t)(u_long)BUS_ADDR_PADDR(busaddr);
693
694 /* first try and reset the drive */
695 error = xyc_cmd(xyc, XYCMD_RST, 0, xy->xy_drive, 0, 0, 0, fmode);
696 XYC_DONE(xyc, error);
697 if (error == XY_ERR_DNRY) {
698 printf(" drive %d: off-line\n", xa->driveno);
699 goto done;
700 }
701 if (error) {
702 printf(": ERROR 0x%02x (%s)\n", error, xyc_e2str(error));
703 goto done;
704 }
705 printf(" drive %d: ready", xa->driveno);
706
707 /*
708 * now set drive parameters (to semi-bogus values) so we can read the
709 * disk label.
710 */
711 xy->pcyl = xy->ncyl = 1;
712 xy->acyl = 0;
713 xy->nhead = 1;
714 xy->nsect = 1;
715 xy->sectpercyl = 1;
716 for (lcv = 0; lcv < 126; lcv++) /* init empty bad144 table */
717 xy->dkb.bt_bad[lcv].bt_cyl =
718 xy->dkb.bt_bad[lcv].bt_trksec = 0xffff;
719
720 /* read disk label */
721 for (xy->drive_type = 0 ; xy->drive_type <= XYC_MAXDT ;
722 xy->drive_type++) {
723 error = xyc_cmd(xyc, XYCMD_RD, 0, xy->xy_drive, 0, 1,
724 dmaddr, fmode);
725 XYC_DONE(xyc, error);
726 if (error == XY_ERR_AOK) break;
727 }
728
729 if (error != XY_ERR_AOK) {
730 printf("\n%s: reading disk label failed: %s\n",
731 xy->sc_dev.dv_xname, xyc_e2str(error));
732 goto done;
733 }
734 printf(" (drive type %d)\n", xy->drive_type);
735
736 newstate = XY_DRIVE_NOLABEL;
737
738 xy->hw_spt = spt = 0; /* XXX needed ? */
739 /* Attach the disk: must be before getdisklabel to malloc label */
740 disk_attach(&xy->sc_dk);
741
742 if (xygetdisklabel(xy, buf) != XY_ERR_AOK)
743 goto done;
744
745 /* inform the user of what is up */
746 printf("%s: <%s>, pcyl %d\n", xy->sc_dev.dv_xname,
747 buf, xy->pcyl);
748 mb = xy->ncyl * (xy->nhead * xy->nsect) / (1048576 / XYFM_BPS);
749 printf("%s: %dMB, %d cyl, %d head, %d sec, %d bytes/sec\n",
750 xy->sc_dev.dv_xname, mb, xy->ncyl, xy->nhead, xy->nsect,
751 XYFM_BPS);
752
753 /*
754 * 450/451 stupidity: the drive type is encoded into the format
755 * of the disk. the drive type in the IOPB must match the drive
756 * type in the format, or you will not be able to do I/O to the
757 * disk (you get header not found errors). if you have two drives
758 * of different sizes that have the same drive type in their
759 * formatting then you are out of luck.
760 *
761 * this problem was corrected in the 753/7053.
762 */
763
764 for (lcv = 0 ; lcv < XYC_MAXDEV ; lcv++) {
765 oxy = xyc->sc_drives[lcv];
766 if (oxy == NULL || oxy == xy) continue;
767 if (oxy->drive_type != xy->drive_type) continue;
768 if (xy->nsect != oxy->nsect || xy->pcyl != oxy->pcyl ||
769 xy->nhead != oxy->nhead) {
770 printf("%s: %s and %s must be the same size!\n",
771 xyc->sc_dev.dv_xname, xy->sc_dev.dv_xname,
772 oxy->sc_dev.dv_xname);
773 panic("xy drive size mismatch");
774 }
775 }
776
777
778 /* now set the real drive parameters! */
779
780 blk = (xy->nsect - 1) +
781 ((xy->nhead - 1) * xy->nsect) +
782 ((xy->pcyl - 1) * xy->nsect * xy->nhead);
783 error = xyc_cmd(xyc, XYCMD_SDS, 0, xy->xy_drive, blk, 0, 0, fmode);
784 XYC_DONE(xyc, error);
785 if (error) {
786 printf("%s: write drive size failed: %s\n",
787 xy->sc_dev.dv_xname, xyc_e2str(error));
788 goto done;
789 }
790 newstate = XY_DRIVE_ONLINE;
791
792 /*
793 * read bad144 table. this table resides on the first sector of the
794 * last track of the disk (i.e. second cyl of "acyl" area).
795 */
796
797 blk = (xy->ncyl + xy->acyl - 1) * (xy->nhead * xy->nsect) +
798 /* last cyl */
799 (xy->nhead - 1) * xy->nsect; /* last head */
800 error = xyc_cmd(xyc, XYCMD_RD, 0, xy->xy_drive, blk, 1,
801 dmaddr, fmode);
802 XYC_DONE(xyc, error);
803 if (error) {
804 printf("%s: reading bad144 failed: %s\n",
805 xy->sc_dev.dv_xname, xyc_e2str(error));
806 goto done;
807 }
808
809 /* check dkbad for sanity */
810 dkb = (struct dkbad *) buf;
811 for (lcv = 0; lcv < 126; lcv++) {
812 if ((dkb->bt_bad[lcv].bt_cyl == 0xffff ||
813 dkb->bt_bad[lcv].bt_cyl == 0) &&
814 dkb->bt_bad[lcv].bt_trksec == 0xffff)
815 continue; /* blank */
816 if (dkb->bt_bad[lcv].bt_cyl >= xy->ncyl)
817 break;
818 if ((dkb->bt_bad[lcv].bt_trksec >> 8) >= xy->nhead)
819 break;
820 if ((dkb->bt_bad[lcv].bt_trksec & 0xff) >= xy->nsect)
821 break;
822 }
823 if (lcv != 126) {
824 printf("%s: warning: invalid bad144 sector!\n",
825 xy->sc_dev.dv_xname);
826 } else {
827 bcopy(buf, &xy->dkb, XYFM_BPS);
828 }
829
830 done:
831 if (buf != NULL) {
832 xy_dmamem_free(xyc->dmatag, xyc->auxmap,
833 &seg, rseg, XYFM_BPS, buf);
834 }
835
836 xy->state = newstate;
837 if (!xa->booting) {
838 wakeup(&xy->state);
839 splx(s);
840 }
841 }
842
843 /*
844 * end of autoconfig functions
845 */
846
847 /*
848 * { b , c } d e v s w f u n c t i o n s
849 */
850
851 /*
852 * xyclose: close device
853 */
854 int
855 xyclose(dev, flag, fmt, l)
856 dev_t dev;
857 int flag, fmt;
858 struct lwp *l;
859
860 {
861 struct xy_softc *xy = xy_cd.cd_devs[DISKUNIT(dev)];
862 int part = DISKPART(dev);
863
864 /* clear mask bits */
865
866 switch (fmt) {
867 case S_IFCHR:
868 xy->sc_dk.dk_copenmask &= ~(1 << part);
869 break;
870 case S_IFBLK:
871 xy->sc_dk.dk_bopenmask &= ~(1 << part);
872 break;
873 }
874 xy->sc_dk.dk_openmask = xy->sc_dk.dk_copenmask | xy->sc_dk.dk_bopenmask;
875
876 return 0;
877 }
878
879 /*
880 * xydump: crash dump system
881 */
882 int
883 xydump(dev, blkno, va, size)
884 dev_t dev;
885 daddr_t blkno;
886 caddr_t va;
887 size_t size;
888 {
889 int unit, part;
890 struct xy_softc *xy;
891
892 unit = DISKUNIT(dev);
893 if (unit >= xy_cd.cd_ndevs)
894 return ENXIO;
895 part = DISKPART(dev);
896
897 xy = xy_cd.cd_devs[unit];
898
899 printf("%s%c: crash dump not supported (yet)\n", xy->sc_dev.dv_xname,
900 'a' + part);
901
902 return ENXIO;
903
904 /* outline: globals: "dumplo" == sector number of partition to start
905 * dump at (convert to physical sector with partition table)
906 * "dumpsize" == size of dump in clicks "physmem" == size of physical
907 * memory (clicks, ctob() to get bytes) (normal case: dumpsize ==
908 * physmem)
909 *
910 * dump a copy of physical memory to the dump device starting at sector
911 * "dumplo" in the swap partition (make sure > 0). map in pages as
912 * we go. use polled I/O.
913 *
914 * XXX how to handle NON_CONTIG? */
915
916 }
917
918 /*
919 * xyioctl: ioctls on XY drives. based on ioctl's of other netbsd disks.
920 */
921 int
922 xyioctl(dev, command, addr, flag, l)
923 dev_t dev;
924 u_long command;
925 caddr_t addr;
926 int flag;
927 struct lwp *l;
928
929 {
930 struct xy_softc *xy;
931 struct xd_iocmd *xio;
932 int error, s, unit;
933 #ifdef __HAVE_OLD_DISKLABEL
934 struct disklabel newlabel;
935 #endif
936 struct disklabel *lp;
937
938 unit = DISKUNIT(dev);
939
940 if (unit >= xy_cd.cd_ndevs || (xy = xy_cd.cd_devs[unit]) == NULL)
941 return (ENXIO);
942
943 /* switch on ioctl type */
944
945 switch (command) {
946 case DIOCSBAD: /* set bad144 info */
947 if ((flag & FWRITE) == 0)
948 return EBADF;
949 s = splbio();
950 bcopy(addr, &xy->dkb, sizeof(xy->dkb));
951 splx(s);
952 return 0;
953
954 case DIOCGDINFO: /* get disk label */
955 bcopy(xy->sc_dk.dk_label, addr, sizeof(struct disklabel));
956 return 0;
957 #ifdef __HAVE_OLD_DISKLABEL
958 case ODIOCGDINFO:
959 newlabel = *(xy->sc_dk.dk_label);
960 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
961 return ENOTTY;
962 memcpy(addr, &newlabel, sizeof (struct olddisklabel));
963 return 0;
964 #endif
965
966 case DIOCGPART: /* get partition info */
967 ((struct partinfo *) addr)->disklab = xy->sc_dk.dk_label;
968 ((struct partinfo *) addr)->part =
969 &xy->sc_dk.dk_label->d_partitions[DISKPART(dev)];
970 return 0;
971
972 case DIOCSDINFO: /* set disk label */
973 #ifdef __HAVE_OLD_DISKLABEL
974 case ODIOCSDINFO:
975 if (command == ODIOCSDINFO) {
976 memset(&newlabel, 0, sizeof newlabel);
977 memcpy(&newlabel, addr, sizeof (struct olddisklabel));
978 lp = &newlabel;
979 } else
980 #endif
981 lp = (struct disklabel *)addr;
982
983 if ((flag & FWRITE) == 0)
984 return EBADF;
985 error = setdisklabel(xy->sc_dk.dk_label,
986 lp, /* xy->sc_dk.dk_openmask : */ 0,
987 xy->sc_dk.dk_cpulabel);
988 if (error == 0) {
989 if (xy->state == XY_DRIVE_NOLABEL)
990 xy->state = XY_DRIVE_ONLINE;
991 }
992 return error;
993
994 case DIOCWLABEL: /* change write status of disk label */
995 if ((flag & FWRITE) == 0)
996 return EBADF;
997 if (*(int *) addr)
998 xy->flags |= XY_WLABEL;
999 else
1000 xy->flags &= ~XY_WLABEL;
1001 return 0;
1002
1003 case DIOCWDINFO: /* write disk label */
1004 #ifdef __HAVE_OLD_DISKLABEL
1005 case ODIOCWDINFO:
1006 if (command == ODIOCWDINFO) {
1007 memset(&newlabel, 0, sizeof newlabel);
1008 memcpy(&newlabel, addr, sizeof (struct olddisklabel));
1009 lp = &newlabel;
1010 } else
1011 #endif
1012 lp = (struct disklabel *)addr;
1013
1014 if ((flag & FWRITE) == 0)
1015 return EBADF;
1016 error = setdisklabel(xy->sc_dk.dk_label,
1017 lp, /* xy->sc_dk.dk_openmask : */ 0,
1018 xy->sc_dk.dk_cpulabel);
1019 if (error == 0) {
1020 if (xy->state == XY_DRIVE_NOLABEL)
1021 xy->state = XY_DRIVE_ONLINE;
1022
1023 /* Simulate opening partition 0 so write succeeds. */
1024 xy->sc_dk.dk_openmask |= (1 << 0);
1025 error = writedisklabel(MAKEDISKDEV(major(dev), DISKUNIT(dev), RAW_PART),
1026 xystrategy, xy->sc_dk.dk_label,
1027 xy->sc_dk.dk_cpulabel);
1028 xy->sc_dk.dk_openmask =
1029 xy->sc_dk.dk_copenmask | xy->sc_dk.dk_bopenmask;
1030 }
1031 return error;
1032
1033 case DIOSXDCMD:
1034 xio = (struct xd_iocmd *) addr;
1035 if ((error = kauth_authorize_generic(l->l_proc->p_cred,
1036 KAUTH_GENERIC_ISSUSER,
1037 &l->l_proc->p_acflag)) != 0)
1038 return (error);
1039 return (xyc_ioctlcmd(xy, dev, xio));
1040
1041 default:
1042 return ENOTTY;
1043 }
1044 }
1045
1046 /*
1047 * xyopen: open drive
1048 */
1049
1050 int
1051 xyopen(dev, flag, fmt, l)
1052 dev_t dev;
1053 int flag, fmt;
1054 struct lwp *l;
1055 {
1056 int unit, part;
1057 struct xy_softc *xy;
1058 struct xyc_attach_args xa;
1059
1060 /* first, could it be a valid target? */
1061
1062 unit = DISKUNIT(dev);
1063 if (unit >= xy_cd.cd_ndevs || (xy = xy_cd.cd_devs[unit]) == NULL)
1064 return (ENXIO);
1065 part = DISKPART(dev);
1066
1067 /* do we need to attach the drive? */
1068
1069 if (xy->state == XY_DRIVE_UNKNOWN) {
1070 xa.driveno = xy->xy_drive;
1071 xa.fullmode = XY_SUB_WAIT;
1072 xa.booting = 0;
1073 xyattach((struct device *) xy->parent,
1074 (struct device *) xy, &xa);
1075 if (xy->state == XY_DRIVE_UNKNOWN) {
1076 return (EIO);
1077 }
1078 }
1079 /* check for partition */
1080
1081 if (part != RAW_PART &&
1082 (part >= xy->sc_dk.dk_label->d_npartitions ||
1083 xy->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
1084 return (ENXIO);
1085 }
1086 /* set open masks */
1087
1088 switch (fmt) {
1089 case S_IFCHR:
1090 xy->sc_dk.dk_copenmask |= (1 << part);
1091 break;
1092 case S_IFBLK:
1093 xy->sc_dk.dk_bopenmask |= (1 << part);
1094 break;
1095 }
1096 xy->sc_dk.dk_openmask = xy->sc_dk.dk_copenmask | xy->sc_dk.dk_bopenmask;
1097
1098 return 0;
1099 }
1100
1101 int
1102 xyread(dev, uio, flags)
1103 dev_t dev;
1104 struct uio *uio;
1105 int flags;
1106 {
1107
1108 return (physio(xystrategy, NULL, dev, B_READ, minphys, uio));
1109 }
1110
1111 int
1112 xywrite(dev, uio, flags)
1113 dev_t dev;
1114 struct uio *uio;
1115 int flags;
1116 {
1117
1118 return (physio(xystrategy, NULL, dev, B_WRITE, minphys, uio));
1119 }
1120
1121
1122 /*
1123 * xysize: return size of a partition for a dump
1124 */
1125
1126 int
1127 xysize(dev)
1128 dev_t dev;
1129
1130 {
1131 struct xy_softc *xysc;
1132 int unit, part, size, omask;
1133
1134 /* valid unit? */
1135 unit = DISKUNIT(dev);
1136 if (unit >= xy_cd.cd_ndevs || (xysc = xy_cd.cd_devs[unit]) == NULL)
1137 return (-1);
1138
1139 part = DISKPART(dev);
1140 omask = xysc->sc_dk.dk_openmask & (1 << part);
1141
1142 if (omask == 0 && xyopen(dev, 0, S_IFBLK, NULL) != 0)
1143 return (-1);
1144
1145 /* do it */
1146 if (xysc->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
1147 size = -1; /* only give valid size for swap partitions */
1148 else
1149 size = xysc->sc_dk.dk_label->d_partitions[part].p_size *
1150 (xysc->sc_dk.dk_label->d_secsize / DEV_BSIZE);
1151 if (omask == 0 && xyclose(dev, 0, S_IFBLK, NULL) != 0)
1152 return (-1);
1153 return (size);
1154 }
1155
1156 /*
1157 * xystrategy: buffering system interface to xy.
1158 */
1159
1160 void
1161 xystrategy(bp)
1162 struct buf *bp;
1163
1164 {
1165 struct xy_softc *xy;
1166 int s, unit;
1167 struct xyc_attach_args xa;
1168 struct disklabel *lp;
1169 daddr_t blkno;
1170
1171 unit = DISKUNIT(bp->b_dev);
1172
1173 /* check for live device */
1174
1175 if (unit >= xy_cd.cd_ndevs || (xy = xy_cd.cd_devs[unit]) == 0 ||
1176 bp->b_blkno < 0 ||
1177 (bp->b_bcount % xy->sc_dk.dk_label->d_secsize) != 0) {
1178 bp->b_error = EINVAL;
1179 goto bad;
1180 }
1181 /* do we need to attach the drive? */
1182
1183 if (xy->state == XY_DRIVE_UNKNOWN) {
1184 xa.driveno = xy->xy_drive;
1185 xa.fullmode = XY_SUB_WAIT;
1186 xa.booting = 0;
1187 xyattach((struct device *)xy->parent, (struct device *)xy, &xa);
1188 if (xy->state == XY_DRIVE_UNKNOWN) {
1189 bp->b_error = EIO;
1190 goto bad;
1191 }
1192 }
1193 if (xy->state != XY_DRIVE_ONLINE && DISKPART(bp->b_dev) != RAW_PART) {
1194 /* no I/O to unlabeled disks, unless raw partition */
1195 bp->b_error = EIO;
1196 goto bad;
1197 }
1198 /* short circuit zero length request */
1199
1200 if (bp->b_bcount == 0)
1201 goto done;
1202
1203 /* check bounds with label (disksubr.c). Determine the size of the
1204 * transfer, and make sure it is within the boundaries of the
1205 * partition. Adjust transfer if needed, and signal errors or early
1206 * completion. */
1207
1208 lp = xy->sc_dk.dk_label;
1209
1210 if (bounds_check_with_label(&xy->sc_dk, bp,
1211 (xy->flags & XY_WLABEL) != 0) <= 0)
1212 goto done;
1213
1214 /*
1215 * Now convert the block number to absolute and put it in
1216 * terms of the device's logical block size.
1217 */
1218 blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
1219 if (DISKPART(bp->b_dev) != RAW_PART)
1220 blkno += lp->d_partitions[DISKPART(bp->b_dev)].p_offset;
1221
1222 bp->b_rawblkno = blkno;
1223
1224 /*
1225 * now we know we have a valid buf structure that we need to do I/O
1226 * on.
1227 */
1228 s = splbio(); /* protect the queues */
1229
1230 BUFQ_PUT(xy->xyq, bp);
1231
1232 /* start 'em up */
1233
1234 xyc_start(xy->parent, NULL);
1235
1236 /* done! */
1237
1238 splx(s);
1239 return;
1240
1241 bad: /* tells upper layers we have an error */
1242 bp->b_flags |= B_ERROR;
1243 done: /* tells upper layers we are done with this
1244 * buf */
1245 bp->b_resid = bp->b_bcount;
1246 biodone(bp);
1247 }
1248 /*
1249 * end of {b,c}devsw functions
1250 */
1251
1252 /*
1253 * i n t e r r u p t f u n c t i o n
1254 *
1255 * xycintr: hardware interrupt.
1256 */
1257 int
1258 xycintr(v)
1259 void *v;
1260
1261 {
1262 struct xyc_softc *xycsc = v;
1263
1264 /* kick the event counter */
1265
1266 xycsc->sc_intrcnt.ev_count++;
1267
1268 /* remove as many done IOPBs as possible */
1269
1270 xyc_remove_iorq(xycsc);
1271
1272 /* start any iorq's already waiting */
1273
1274 xyc_start(xycsc, NULL);
1275
1276 return (1);
1277 }
1278 /*
1279 * end of interrupt function
1280 */
1281
1282 /*
1283 * i n t e r n a l f u n c t i o n s
1284 */
1285
1286 /*
1287 * xyc_rqinit: fill out the fields of an I/O request
1288 */
1289
1290 inline void
1291 xyc_rqinit(rq, xyc, xy, md, blk, cnt, db, bp)
1292 struct xy_iorq *rq;
1293 struct xyc_softc *xyc;
1294 struct xy_softc *xy;
1295 int md;
1296 u_long blk;
1297 int cnt;
1298 caddr_t db;
1299 struct buf *bp;
1300 {
1301 rq->xyc = xyc;
1302 rq->xy = xy;
1303 rq->ttl = XYC_MAXTTL + 10;
1304 rq->mode = md;
1305 rq->tries = rq->errno = rq->lasterror = 0;
1306 rq->blockno = blk;
1307 rq->sectcnt = cnt;
1308 rq->dbuf = db;
1309 rq->buf = bp;
1310 }
1311
1312 /*
1313 * xyc_rqtopb: load up an IOPB based on an iorq
1314 */
1315
1316 void
1317 xyc_rqtopb(iorq, iopb, cmd, subfun)
1318 struct xy_iorq *iorq;
1319 struct xy_iopb *iopb;
1320 int cmd, subfun;
1321
1322 {
1323 u_long block, dp;
1324
1325 /* normal IOPB case, standard stuff */
1326
1327 /* chain bit handled later */
1328 iopb->ien = (XY_STATE(iorq->mode) == XY_SUB_POLL) ? 0 : 1;
1329 iopb->com = cmd;
1330 iopb->errno = 0;
1331 iopb->errs = 0;
1332 iopb->done = 0;
1333 if (iorq->xy) {
1334 iopb->unit = iorq->xy->xy_drive;
1335 iopb->dt = iorq->xy->drive_type;
1336 } else {
1337 iopb->unit = 0;
1338 iopb->dt = 0;
1339 }
1340 block = iorq->blockno;
1341 if (iorq->xy == NULL || block == 0) {
1342 iopb->sect = iopb->head = iopb->cyl = 0;
1343 } else {
1344 iopb->sect = block % iorq->xy->nsect;
1345 block = block / iorq->xy->nsect;
1346 iopb->head = block % iorq->xy->nhead;
1347 block = block / iorq->xy->nhead;
1348 iopb->cyl = block;
1349 }
1350 iopb->scnt = iorq->sectcnt;
1351 dp = (u_long) iorq->dbuf;
1352 if (iorq->dbuf == NULL) {
1353 iopb->dataa = 0;
1354 iopb->datar = 0;
1355 } else {
1356 iopb->dataa = (dp & 0xffff);
1357 iopb->datar = ((dp & 0xff0000) >> 16);
1358 }
1359 iopb->subfn = subfun;
1360 }
1361
1362
1363 /*
1364 * xyc_unbusy: wait for the xyc to go unbusy, or timeout.
1365 */
1366
1367 int
1368 xyc_unbusy(xyc, del)
1369
1370 struct xyc *xyc;
1371 int del;
1372
1373 {
1374 while (del-- > 0) {
1375 if ((xyc->xyc_csr & XYC_GBSY) == 0)
1376 break;
1377 DELAY(1);
1378 }
1379 return(del == 0 ? XY_ERR_FAIL : XY_ERR_AOK);
1380 }
1381
1382 /*
1383 * xyc_cmd: front end for POLL'd and WAIT'd commands. Returns 0 or error.
1384 * note that NORM requests are handled separately.
1385 */
1386 int
1387 xyc_cmd(xycsc, cmd, subfn, unit, block, scnt, dptr, fullmode)
1388 struct xyc_softc *xycsc;
1389 int cmd, subfn, unit, block, scnt;
1390 char *dptr;
1391 int fullmode;
1392
1393 {
1394 int submode = XY_STATE(fullmode);
1395 struct xy_iorq *iorq = xycsc->ciorq;
1396 struct xy_iopb *iopb = xycsc->ciopb;
1397
1398 /*
1399 * is someone else using the control iopq wait for it if we can
1400 */
1401 start:
1402 if (submode == XY_SUB_WAIT && XY_STATE(iorq->mode) != XY_SUB_FREE) {
1403 if (tsleep(iorq, PRIBIO, "xyc_cmd", 0))
1404 return(XY_ERR_FAIL);
1405 goto start;
1406 }
1407
1408 if (XY_STATE(iorq->mode) != XY_SUB_FREE) {
1409 DELAY(1000000); /* XY_SUB_POLL: steal the iorq */
1410 iorq->mode = XY_SUB_FREE;
1411 printf("%s: stole control iopb\n", xycsc->sc_dev.dv_xname);
1412 }
1413
1414 /* init iorq/iopb */
1415
1416 xyc_rqinit(iorq, xycsc,
1417 (unit == XYC_NOUNIT) ? NULL : xycsc->sc_drives[unit],
1418 fullmode, block, scnt, dptr, NULL);
1419
1420 /* load IOPB from iorq */
1421
1422 xyc_rqtopb(iorq, iopb, cmd, subfn);
1423
1424 /* submit it for processing */
1425
1426 xyc_submit_iorq(xycsc, iorq, fullmode); /* error code will be in iorq */
1427
1428 return(XY_ERR_AOK);
1429 }
1430
1431 /*
1432 * xyc_startbuf
1433 * start a buffer for running
1434 */
1435
1436 int
1437 xyc_startbuf(xycsc, xysc, bp)
1438 struct xyc_softc *xycsc;
1439 struct xy_softc *xysc;
1440 struct buf *bp;
1441
1442 {
1443 int partno, error;
1444 struct xy_iorq *iorq;
1445 struct xy_iopb *iopb;
1446 u_long block;
1447
1448 iorq = xysc->xyrq;
1449 iopb = iorq->iopb;
1450
1451 /* get buf */
1452
1453 if (bp == NULL)
1454 panic("xyc_startbuf null buf");
1455
1456 partno = DISKPART(bp->b_dev);
1457 #ifdef XYC_DEBUG
1458 printf("xyc_startbuf: %s%c: %s block %d\n", xysc->sc_dev.dv_xname,
1459 'a' + partno, (bp->b_flags & B_READ) ? "read" : "write", bp->b_blkno);
1460 printf("xyc_startbuf: b_bcount %d, b_data 0x%x\n",
1461 bp->b_bcount, bp->b_data);
1462 #endif
1463
1464 /*
1465 * load request.
1466 *
1467 * note that iorq points to the buffer as mapped into DVMA space,
1468 * where as the bp->b_data points to its non-DVMA mapping.
1469 */
1470
1471 block = bp->b_rawblkno;
1472
1473 error = bus_dmamap_load(xycsc->dmatag, iorq->dmamap,
1474 bp->b_data, bp->b_bcount, 0, BUS_DMA_NOWAIT);
1475 if (error != 0) {
1476 printf("%s: warning: cannot load DMA map\n",
1477 xycsc->sc_dev.dv_xname);
1478 return (XY_ERR_FAIL); /* XXX: need some sort of
1479 * call-back scheme here? */
1480 }
1481
1482 bus_dmamap_sync(xycsc->dmatag, iorq->dmamap, 0,
1483 iorq->dmamap->dm_mapsize, (bp->b_flags & B_READ)
1484 ? BUS_DMASYNC_PREREAD
1485 : BUS_DMASYNC_PREWRITE);
1486
1487 /* init iorq and load iopb from it */
1488 xyc_rqinit(iorq, xycsc, xysc, XY_SUB_NORM | XY_MODE_VERBO, block,
1489 bp->b_bcount / XYFM_BPS,
1490 (caddr_t)(u_long)iorq->dmamap->dm_segs[0].ds_addr,
1491 bp);
1492
1493 xyc_rqtopb(iorq, iopb, (bp->b_flags & B_READ) ? XYCMD_RD : XYCMD_WR, 0);
1494
1495 /* Instrumentation. */
1496 disk_busy(&xysc->sc_dk);
1497
1498 return (XY_ERR_AOK);
1499 }
1500
1501
1502 /*
1503 * xyc_submit_iorq: submit an iorq for processing. returns XY_ERR_AOK
1504 * if ok. if it fail returns an error code. type is XY_SUB_*.
1505 *
1506 * note: caller frees iorq in all cases except NORM
1507 *
1508 * return value:
1509 * NORM: XY_AOK (req pending), XY_FAIL (couldn't submit request)
1510 * WAIT: XY_AOK (success), <error-code> (failed)
1511 * POLL: <same as WAIT>
1512 * NOQ : <same as NORM>
1513 *
1514 * there are three sources for i/o requests:
1515 * [1] xystrategy: normal block I/O, using "struct buf" system.
1516 * [2] autoconfig/crash dump: these are polled I/O requests, no interrupts.
1517 * [3] open/ioctl: these are I/O requests done in the context of a process,
1518 * and the process should block until they are done.
1519 *
1520 * software state is stored in the iorq structure. each iorq has an
1521 * iopb structure. the hardware understands the iopb structure.
1522 * every command must go through an iopb. a 450 handles one iopb at a
1523 * time, where as a 451 can take them in chains. [the 450 claims it
1524 * can handle chains, but is appears to be buggy...] iopb are allocated
1525 * in DVMA space at boot up time. each disk gets one iopb, and the
1526 * controller gets one (for POLL and WAIT commands). what happens if
1527 * the iopb is busy? for i/o type [1], the buffers are queued at the
1528 * "buff" layer and * picked up later by the interrupt routine. for case
1529 * [2] we can only be blocked if there is a WAIT type I/O request being
1530 * run. since this can only happen when we are crashing, we wait a sec
1531 * and then steal the IOPB. for case [3] the process can sleep
1532 * on the iorq free list until some iopbs are available.
1533 */
1534
1535
1536 int
1537 xyc_submit_iorq(xycsc, iorq, type)
1538 struct xyc_softc *xycsc;
1539 struct xy_iorq *iorq;
1540 int type;
1541
1542 {
1543 struct xy_iopb *dmaiopb;
1544
1545 #ifdef XYC_DEBUG
1546 printf("xyc_submit_iorq(%s, addr=0x%x, type=%d)\n",
1547 xycsc->sc_dev.dv_xname, iorq, type);
1548 #endif
1549
1550 /* first check and see if controller is busy */
1551 if ((xycsc->xyc->xyc_csr & XYC_GBSY) != 0) {
1552 #ifdef XYC_DEBUG
1553 printf("xyc_submit_iorq: XYC not ready (BUSY)\n");
1554 #endif
1555 if (type == XY_SUB_NOQ)
1556 return (XY_ERR_FAIL); /* failed */
1557 switch (type) {
1558 case XY_SUB_NORM:
1559 return XY_ERR_AOK; /* success */
1560 case XY_SUB_WAIT:
1561 while (iorq->iopb->done == 0) {
1562 (void) tsleep(iorq, PRIBIO, "xyciorq", 0);
1563 }
1564 return (iorq->errno);
1565 case XY_SUB_POLL: /* steal controller */
1566 (void)xycsc->xyc->xyc_rsetup; /* RESET */
1567 if (xyc_unbusy(xycsc->xyc,XYC_RESETUSEC) == XY_ERR_FAIL)
1568 panic("xyc_submit_iorq: stuck xyc");
1569 printf("%s: stole controller\n",
1570 xycsc->sc_dev.dv_xname);
1571 break;
1572 default:
1573 panic("xyc_submit_iorq adding");
1574 }
1575 }
1576
1577 dmaiopb = xyc_chain(xycsc, iorq); /* build chain */
1578 if (dmaiopb == NULL) { /* nothing doing? */
1579 if (type == XY_SUB_NORM || type == XY_SUB_NOQ)
1580 return(XY_ERR_AOK);
1581 panic("xyc_submit_iorq: xyc_chain failed!");
1582 }
1583
1584 XYC_GO(xycsc->xyc, dmaiopb);
1585
1586 /* command now running, wrap it up */
1587 switch (type) {
1588 case XY_SUB_NORM:
1589 case XY_SUB_NOQ:
1590 return (XY_ERR_AOK); /* success */
1591 case XY_SUB_WAIT:
1592 while (iorq->iopb->done == 0) {
1593 (void) tsleep(iorq, PRIBIO, "xyciorq", 0);
1594 }
1595 return (iorq->errno);
1596 case XY_SUB_POLL:
1597 return (xyc_piodriver(xycsc, iorq));
1598 default:
1599 panic("xyc_submit_iorq wrap up");
1600 }
1601 panic("xyc_submit_iorq");
1602 return 0; /* not reached */
1603 }
1604
1605
1606 /*
1607 * xyc_chain: build a chain. return dvma address of first element in
1608 * the chain. iorq != NULL: means we only want that item on the chain.
1609 */
1610
1611 struct xy_iopb *
1612 xyc_chain(xycsc, iorq)
1613 struct xyc_softc *xycsc;
1614 struct xy_iorq *iorq;
1615
1616 {
1617 int togo, chain, hand;
1618
1619 bzero(xycsc->xy_chain, sizeof(xycsc->xy_chain));
1620
1621 /*
1622 * promote control IOPB to the top
1623 */
1624 if (iorq == NULL) {
1625 if ((XY_STATE(xycsc->reqs[XYC_CTLIOPB].mode) == XY_SUB_POLL ||
1626 XY_STATE(xycsc->reqs[XYC_CTLIOPB].mode) == XY_SUB_WAIT) &&
1627 xycsc->iopbase[XYC_CTLIOPB].done == 0)
1628 iorq = &xycsc->reqs[XYC_CTLIOPB];
1629 }
1630
1631 /*
1632 * special case: if iorq != NULL then we have a POLL or WAIT request.
1633 * we let these take priority and do them first.
1634 */
1635 if (iorq) {
1636 xycsc->xy_chain[0] = iorq;
1637 iorq->iopb->chen = 0;
1638 return(iorq->dmaiopb);
1639 }
1640
1641 /*
1642 * NORM case: do round robin and maybe chain (if allowed and possible)
1643 */
1644 chain = 0;
1645 hand = xycsc->xy_hand;
1646 xycsc->xy_hand = (xycsc->xy_hand + 1) % XYC_MAXIOPB;
1647
1648 for (togo = XYC_MAXIOPB; togo > 0;
1649 togo--, hand = (hand + 1) % XYC_MAXIOPB) {
1650 struct xy_iopb *iopb, *prev_iopb, *dmaiopb;
1651
1652 if (XY_STATE(xycsc->reqs[hand].mode) != XY_SUB_NORM ||
1653 xycsc->iopbase[hand].done)
1654 continue; /* not ready-for-i/o */
1655
1656 xycsc->xy_chain[chain] = &xycsc->reqs[hand];
1657 iopb = xycsc->xy_chain[chain]->iopb;
1658 iopb->chen = 0;
1659 if (chain != 0) {
1660 /* adding a link to a chain */
1661 prev_iopb = xycsc->xy_chain[chain-1]->iopb;
1662 prev_iopb->chen = 1;
1663 dmaiopb = xycsc->xy_chain[chain]->dmaiopb;
1664 prev_iopb->nxtiopb = ((u_long)dmaiopb) & 0xffff;
1665 } else {
1666 /* head of chain */
1667 iorq = xycsc->xy_chain[chain];
1668 }
1669 chain++;
1670
1671 /* quit if chaining dis-allowed */
1672 if (xycsc->no_ols)
1673 break;
1674 }
1675
1676 return(iorq ? iorq->dmaiopb : NULL);
1677 }
1678
1679 /*
1680 * xyc_piodriver
1681 *
1682 * programmed i/o driver. this function takes over the computer
1683 * and drains off the polled i/o request. it returns the status of the iorq
1684 * the caller is interesting in.
1685 */
1686 int
1687 xyc_piodriver(xycsc, iorq)
1688 struct xyc_softc *xycsc;
1689 struct xy_iorq *iorq;
1690
1691 {
1692 int nreset = 0;
1693 int retval = 0;
1694 u_long res;
1695 #ifdef XYC_DEBUG
1696 printf("xyc_piodriver(%s, 0x%x)\n", xycsc->sc_dev.dv_xname, iorq);
1697 #endif
1698
1699 while (iorq->iopb->done == 0) {
1700
1701 res = xyc_unbusy(xycsc->xyc, XYC_MAXTIME);
1702
1703 /* we expect some progress soon */
1704 if (res == XY_ERR_FAIL && nreset >= 2) {
1705 xyc_reset(xycsc, 0, XY_RSET_ALL, XY_ERR_FAIL, 0);
1706 #ifdef XYC_DEBUG
1707 printf("xyc_piodriver: timeout\n");
1708 #endif
1709 return (XY_ERR_FAIL);
1710 }
1711 if (res == XY_ERR_FAIL) {
1712 if (xyc_reset(xycsc, 0,
1713 (nreset++ == 0) ? XY_RSET_NONE : iorq,
1714 XY_ERR_FAIL,
1715 0) == XY_ERR_FAIL)
1716 return (XY_ERR_FAIL); /* flushes all but POLL
1717 * requests, resets */
1718 continue;
1719 }
1720
1721 xyc_remove_iorq(xycsc); /* may resubmit request */
1722
1723 if (iorq->iopb->done == 0)
1724 xyc_start(xycsc, iorq);
1725 }
1726
1727 /* get return value */
1728
1729 retval = iorq->errno;
1730
1731 #ifdef XYC_DEBUG
1732 printf("xyc_piodriver: done, retval = 0x%x (%s)\n",
1733 iorq->errno, xyc_e2str(iorq->errno));
1734 #endif
1735
1736 /* start up any bufs that have queued */
1737
1738 xyc_start(xycsc, NULL);
1739
1740 return (retval);
1741 }
1742
1743 /*
1744 * xyc_xyreset: reset one drive. NOTE: assumes xyc was just reset.
1745 * we steal iopb[XYC_CTLIOPB] for this, but we put it back when we are done.
1746 */
1747 void
1748 xyc_xyreset(xycsc, xysc)
1749 struct xyc_softc *xycsc;
1750 struct xy_softc *xysc;
1751
1752 {
1753 struct xy_iopb tmpiopb;
1754 struct xy_iopb *iopb;
1755 int del;
1756
1757 iopb = xycsc->ciopb;
1758
1759 /* Save contents */
1760 bcopy(iopb, &tmpiopb, sizeof(struct xy_iopb));
1761
1762 iopb->chen = iopb->done = iopb->errs = 0;
1763 iopb->ien = 0;
1764 iopb->com = XYCMD_RST;
1765 iopb->unit = xysc->xy_drive;
1766
1767 XYC_GO(xycsc->xyc, xycsc->ciorq->dmaiopb);
1768
1769 del = XYC_RESETUSEC;
1770 while (del > 0) {
1771 if ((xycsc->xyc->xyc_csr & XYC_GBSY) == 0)
1772 break;
1773 DELAY(1);
1774 del--;
1775 }
1776
1777 if (del <= 0 || iopb->errs) {
1778 printf("%s: off-line: %s\n", xycsc->sc_dev.dv_xname,
1779 xyc_e2str(iopb->errno));
1780 del = xycsc->xyc->xyc_rsetup;
1781 if (xyc_unbusy(xycsc->xyc, XYC_RESETUSEC) == XY_ERR_FAIL)
1782 panic("xyc_reset");
1783 } else {
1784 xycsc->xyc->xyc_csr = XYC_IPND; /* clear IPND */
1785 }
1786
1787 /* Restore contents */
1788 bcopy(&tmpiopb, iopb, sizeof(struct xy_iopb));
1789 }
1790
1791
1792 /*
1793 * xyc_reset: reset everything: requests are marked as errors except
1794 * a polled request (which is resubmitted)
1795 */
1796 int
1797 xyc_reset(xycsc, quiet, blastmode, error, xysc)
1798 struct xyc_softc *xycsc;
1799 int quiet, error;
1800 struct xy_iorq *blastmode;
1801 struct xy_softc *xysc;
1802
1803 {
1804 int del = 0, lcv, retval = XY_ERR_AOK;
1805
1806 /* soft reset hardware */
1807
1808 if (!quiet)
1809 printf("%s: soft reset\n", xycsc->sc_dev.dv_xname);
1810 del = xycsc->xyc->xyc_rsetup;
1811 del = xyc_unbusy(xycsc->xyc, XYC_RESETUSEC);
1812 if (del == XY_ERR_FAIL) {
1813 blastmode = XY_RSET_ALL; /* dead, flush all requests */
1814 retval = XY_ERR_FAIL;
1815 }
1816 if (xysc)
1817 xyc_xyreset(xycsc, xysc);
1818
1819 /* fix queues based on "blast-mode" */
1820
1821 for (lcv = 0; lcv < XYC_MAXIOPB; lcv++) {
1822 register struct xy_iorq *iorq = &xycsc->reqs[lcv];
1823
1824 if (XY_STATE(iorq->mode) != XY_SUB_POLL &&
1825 XY_STATE(iorq->mode) != XY_SUB_WAIT &&
1826 XY_STATE(iorq->mode) != XY_SUB_NORM)
1827 /* is it active? */
1828 continue;
1829
1830 if (blastmode == XY_RSET_ALL ||
1831 blastmode != iorq) {
1832 /* failed */
1833 iorq->errno = error;
1834 xycsc->iopbase[lcv].done = xycsc->iopbase[lcv].errs = 1;
1835 switch (XY_STATE(iorq->mode)) {
1836 case XY_SUB_NORM:
1837 iorq->buf->b_error = EIO;
1838 iorq->buf->b_flags |= B_ERROR;
1839 iorq->buf->b_resid = iorq->sectcnt * XYFM_BPS;
1840
1841 bus_dmamap_sync(xycsc->dmatag, iorq->dmamap, 0,
1842 iorq->dmamap->dm_mapsize,
1843 (iorq->buf->b_flags & B_READ)
1844 ? BUS_DMASYNC_POSTREAD
1845 : BUS_DMASYNC_POSTWRITE);
1846
1847 bus_dmamap_unload(xycsc->dmatag, iorq->dmamap);
1848
1849 (void)BUFQ_GET(iorq->xy->xyq);
1850 disk_unbusy(&xycsc->reqs[lcv].xy->sc_dk,
1851 (xycsc->reqs[lcv].buf->b_bcount -
1852 xycsc->reqs[lcv].buf->b_resid),
1853 (xycsc->reqs[lcv].buf->b_flags & B_READ));
1854 biodone(iorq->buf);
1855 iorq->mode = XY_SUB_FREE;
1856 break;
1857 case XY_SUB_WAIT:
1858 wakeup(iorq);
1859 case XY_SUB_POLL:
1860 iorq->mode =
1861 XY_NEWSTATE(iorq->mode, XY_SUB_DONE);
1862 break;
1863 }
1864
1865 } else {
1866
1867 /* resubmit, no need to do anything here */
1868 }
1869 }
1870
1871 /*
1872 * now, if stuff is waiting, start it.
1873 * since we just reset it should go
1874 */
1875 xyc_start(xycsc, NULL);
1876
1877 return (retval);
1878 }
1879
1880 /*
1881 * xyc_start: start waiting buffers
1882 */
1883
1884 void
1885 xyc_start(xycsc, iorq)
1886 struct xyc_softc *xycsc;
1887 struct xy_iorq *iorq;
1888
1889 {
1890 int lcv;
1891 struct xy_softc *xy;
1892
1893 if (iorq == NULL) {
1894 for (lcv = 0; lcv < XYC_MAXDEV ; lcv++) {
1895 if ((xy = xycsc->sc_drives[lcv]) == NULL) continue;
1896 if (BUFQ_PEEK(xy->xyq) == NULL) continue;
1897 if (xy->xyrq->mode != XY_SUB_FREE) continue;
1898 xyc_startbuf(xycsc, xy, BUFQ_PEEK(xy->xyq));
1899 }
1900 }
1901 xyc_submit_iorq(xycsc, iorq, XY_SUB_NOQ);
1902 }
1903
1904 /*
1905 * xyc_remove_iorq: remove "done" IOPB's.
1906 */
1907
1908 int
1909 xyc_remove_iorq(xycsc)
1910 struct xyc_softc *xycsc;
1911
1912 {
1913 int errno, rq, comm, errs;
1914 struct xyc *xyc = xycsc->xyc;
1915 u_long addr;
1916 struct xy_iopb *iopb;
1917 struct xy_iorq *iorq;
1918 struct buf *bp;
1919
1920 if (xyc->xyc_csr & XYC_DERR) {
1921 /*
1922 * DOUBLE ERROR: should never happen under normal use. This
1923 * error is so bad, you can't even tell which IOPB is bad, so
1924 * we dump them all.
1925 */
1926 errno = XY_ERR_DERR;
1927 printf("%s: DOUBLE ERROR!\n", xycsc->sc_dev.dv_xname);
1928 if (xyc_reset(xycsc, 0, XY_RSET_ALL, errno, 0) != XY_ERR_AOK) {
1929 printf("%s: soft reset failed!\n",
1930 xycsc->sc_dev.dv_xname);
1931 panic("xyc_remove_iorq: controller DEAD");
1932 }
1933 return (XY_ERR_AOK);
1934 }
1935
1936 /*
1937 * get iopb that is done, loop down the chain
1938 */
1939
1940 if (xyc->xyc_csr & XYC_ERR) {
1941 xyc->xyc_csr = XYC_ERR; /* clear error condition */
1942 }
1943 if (xyc->xyc_csr & XYC_IPND) {
1944 xyc->xyc_csr = XYC_IPND; /* clear interrupt */
1945 }
1946
1947 for (rq = 0; rq < XYC_MAXIOPB; rq++) {
1948 iorq = xycsc->xy_chain[rq];
1949 if (iorq == NULL) break; /* done ! */
1950 if (iorq->mode == 0 || XY_STATE(iorq->mode) == XY_SUB_DONE)
1951 continue; /* free, or done */
1952 iopb = iorq->iopb;
1953 if (iopb->done == 0)
1954 continue; /* not done yet */
1955
1956 comm = iopb->com;
1957 errs = iopb->errs;
1958
1959 if (errs)
1960 iorq->errno = iopb->errno;
1961 else
1962 iorq->errno = 0;
1963
1964 /* handle non-fatal errors */
1965
1966 if (errs &&
1967 xyc_error(xycsc, iorq, iopb, comm) == XY_ERR_AOK)
1968 continue; /* AOK: we resubmitted it */
1969
1970
1971 /* this iorq is now done (hasn't been restarted or anything) */
1972
1973 if ((iorq->mode & XY_MODE_VERBO) && iorq->lasterror)
1974 xyc_perror(iorq, iopb, 0);
1975
1976 /* now, if read/write check to make sure we got all the data
1977 * we needed. (this may not be the case if we got an error in
1978 * the middle of a multisector request). */
1979
1980 if ((iorq->mode & XY_MODE_B144) != 0 && errs == 0 &&
1981 (comm == XYCMD_RD || comm == XYCMD_WR)) {
1982 /* we just successfully processed a bad144 sector
1983 * note: if we are in bad 144 mode, the pointers have
1984 * been advanced already (see above) and are pointing
1985 * at the bad144 sector. to exit bad144 mode, we
1986 * must advance the pointers 1 sector and issue a new
1987 * request if there are still sectors left to process
1988 *
1989 */
1990 XYC_ADVANCE(iorq, 1); /* advance 1 sector */
1991
1992 /* exit b144 mode */
1993 iorq->mode = iorq->mode & (~XY_MODE_B144);
1994
1995 if (iorq->sectcnt) { /* more to go! */
1996 iorq->lasterror = iorq->errno = iopb->errno = 0;
1997 iopb->errs = iopb->done = 0;
1998 iorq->tries = 0;
1999 iopb->scnt = iorq->sectcnt;
2000 iopb->cyl = iorq->blockno /
2001 iorq->xy->sectpercyl;
2002 iopb->head =
2003 (iorq->blockno / iorq->xy->nhead) %
2004 iorq->xy->nhead;
2005 iopb->sect = iorq->blockno % XYFM_BPS;
2006 addr = (u_long) iorq->dbuf;
2007 iopb->dataa = (addr & 0xffff);
2008 iopb->datar = ((addr & 0xff0000) >> 16);
2009 /* will resubit at end */
2010 continue;
2011 }
2012 }
2013 /* final cleanup, totally done with this request */
2014
2015 switch (XY_STATE(iorq->mode)) {
2016 case XY_SUB_NORM:
2017 bp = iorq->buf;
2018 if (errs) {
2019 bp->b_error = EIO;
2020 bp->b_flags |= B_ERROR;
2021 bp->b_resid = iorq->sectcnt * XYFM_BPS;
2022 } else {
2023 bp->b_resid = 0; /* done */
2024 }
2025 bus_dmamap_sync(xycsc->dmatag, iorq->dmamap, 0,
2026 iorq->dmamap->dm_mapsize,
2027 (iorq->buf->b_flags & B_READ)
2028 ? BUS_DMASYNC_POSTREAD
2029 : BUS_DMASYNC_POSTWRITE);
2030
2031 bus_dmamap_unload(xycsc->dmatag, iorq->dmamap);
2032
2033 (void)BUFQ_GET(iorq->xy->xyq);
2034 disk_unbusy(&iorq->xy->sc_dk,
2035 (bp->b_bcount - bp->b_resid),
2036 (bp->b_flags & B_READ));
2037 iorq->mode = XY_SUB_FREE;
2038 biodone(bp);
2039 break;
2040 case XY_SUB_WAIT:
2041 iorq->mode = XY_NEWSTATE(iorq->mode, XY_SUB_DONE);
2042 wakeup(iorq);
2043 break;
2044 case XY_SUB_POLL:
2045 iorq->mode = XY_NEWSTATE(iorq->mode, XY_SUB_DONE);
2046 break;
2047 }
2048 }
2049
2050 return (XY_ERR_AOK);
2051 }
2052
2053 /*
2054 * xyc_perror: print error.
2055 * - if still_trying is true: we got an error, retried and got a
2056 * different error. in that case lasterror is the old error,
2057 * and errno is the new one.
2058 * - if still_trying is not true, then if we ever had an error it
2059 * is in lasterror. also, if iorq->errno == 0, then we recovered
2060 * from that error (otherwise iorq->errno == iorq->lasterror).
2061 */
2062 void
2063 xyc_perror(iorq, iopb, still_trying)
2064 struct xy_iorq *iorq;
2065 struct xy_iopb *iopb;
2066 int still_trying;
2067
2068 {
2069
2070 int error = iorq->lasterror;
2071
2072 printf("%s", (iorq->xy) ? iorq->xy->sc_dev.dv_xname
2073 : iorq->xyc->sc_dev.dv_xname);
2074 if (iorq->buf)
2075 printf("%c: ", 'a' + DISKPART(iorq->buf->b_dev));
2076 if (iopb->com == XYCMD_RD || iopb->com == XYCMD_WR)
2077 printf("%s %d/%d/%d: ",
2078 (iopb->com == XYCMD_RD) ? "read" : "write",
2079 iopb->cyl, iopb->head, iopb->sect);
2080 printf("%s", xyc_e2str(error));
2081
2082 if (still_trying)
2083 printf(" [still trying, new error=%s]", xyc_e2str(iorq->errno));
2084 else
2085 if (iorq->errno == 0)
2086 printf(" [recovered in %d tries]", iorq->tries);
2087
2088 printf("\n");
2089 }
2090
2091 /*
2092 * xyc_error: non-fatal error encountered... recover.
2093 * return AOK if resubmitted, return FAIL if this iopb is done
2094 */
2095 int
2096 xyc_error(xycsc, iorq, iopb, comm)
2097 struct xyc_softc *xycsc;
2098 struct xy_iorq *iorq;
2099 struct xy_iopb *iopb;
2100 int comm;
2101
2102 {
2103 int errno = iorq->errno;
2104 int erract = xyc_entoact(errno);
2105 int oldmode, advance;
2106 #ifdef __sparc__
2107 int i;
2108 #endif
2109
2110 if (erract == XY_ERA_RSET) { /* some errors require a reset */
2111 oldmode = iorq->mode;
2112 iorq->mode = XY_SUB_DONE | (~XY_SUB_MASK & oldmode);
2113 /* make xyc_start ignore us */
2114 xyc_reset(xycsc, 1, XY_RSET_NONE, errno, iorq->xy);
2115 iorq->mode = oldmode;
2116 }
2117 /* check for read/write to a sector in bad144 table if bad: redirect
2118 * request to bad144 area */
2119
2120 if ((comm == XYCMD_RD || comm == XYCMD_WR) &&
2121 (iorq->mode & XY_MODE_B144) == 0) {
2122 advance = iorq->sectcnt - iopb->scnt;
2123 XYC_ADVANCE(iorq, advance);
2124 #ifdef __sparc__
2125 if ((i = isbad(&iorq->xy->dkb, iorq->blockno / iorq->xy->sectpercyl,
2126 (iorq->blockno / iorq->xy->nsect) % iorq->xy->nhead,
2127 iorq->blockno % iorq->xy->nsect)) != -1) {
2128 iorq->mode |= XY_MODE_B144; /* enter bad144 mode &
2129 * redirect */
2130 iopb->errno = iopb->done = iopb->errs = 0;
2131 iopb->scnt = 1;
2132 iopb->cyl = (iorq->xy->ncyl + iorq->xy->acyl) - 2;
2133 /* second to last acyl */
2134 i = iorq->xy->sectpercyl - 1 - i; /* follow bad144
2135 * standard */
2136 iopb->head = i / iorq->xy->nhead;
2137 iopb->sect = i % iorq->xy->nhead;
2138 /* will resubmit when we come out of remove_iorq */
2139 return (XY_ERR_AOK); /* recovered! */
2140 }
2141 #endif
2142 }
2143
2144 /*
2145 * it isn't a bad144 sector, must be real error! see if we can retry
2146 * it?
2147 */
2148 if ((iorq->mode & XY_MODE_VERBO) && iorq->lasterror)
2149 xyc_perror(iorq, iopb, 1); /* inform of error state
2150 * change */
2151 iorq->lasterror = errno;
2152
2153 if ((erract == XY_ERA_RSET || erract == XY_ERA_HARD)
2154 && iorq->tries < XYC_MAXTRIES) { /* retry? */
2155 iorq->tries++;
2156 iorq->errno = iopb->errno = iopb->done = iopb->errs = 0;
2157 /* will resubmit at end of remove_iorq */
2158 return (XY_ERR_AOK); /* recovered! */
2159 }
2160
2161 /* failed to recover from this error */
2162 return (XY_ERR_FAIL);
2163 }
2164
2165 /*
2166 * xyc_tick: make sure xy is still alive and ticking (err, kicking).
2167 */
2168 void
2169 xyc_tick(arg)
2170 void *arg;
2171
2172 {
2173 struct xyc_softc *xycsc = arg;
2174 int lcv, s, reset = 0;
2175
2176 /* reduce ttl for each request if one goes to zero, reset xyc */
2177 s = splbio();
2178 for (lcv = 0; lcv < XYC_MAXIOPB; lcv++) {
2179 if (xycsc->reqs[lcv].mode == 0 ||
2180 XY_STATE(xycsc->reqs[lcv].mode) == XY_SUB_DONE)
2181 continue;
2182 xycsc->reqs[lcv].ttl--;
2183 if (xycsc->reqs[lcv].ttl == 0)
2184 reset = 1;
2185 }
2186 if (reset) {
2187 printf("%s: watchdog timeout\n", xycsc->sc_dev.dv_xname);
2188 xyc_reset(xycsc, 0, XY_RSET_NONE, XY_ERR_FAIL, NULL);
2189 }
2190 splx(s);
2191
2192 /* until next time */
2193
2194 callout_reset(&xycsc->sc_tick_ch, XYC_TICKCNT, xyc_tick, xycsc);
2195 }
2196
2197 /*
2198 * xyc_ioctlcmd: this function provides a user level interface to the
2199 * controller via ioctl. this allows "format" programs to be written
2200 * in user code, and is also useful for some debugging. we return
2201 * an error code. called at user priority.
2202 *
2203 * XXX missing a few commands (see the 7053 driver for ideas)
2204 */
2205 int
2206 xyc_ioctlcmd(xy, dev, xio)
2207 struct xy_softc *xy;
2208 dev_t dev;
2209 struct xd_iocmd *xio;
2210
2211 {
2212 int s, rqno, dummy = 0;
2213 caddr_t dvmabuf = NULL, buf = NULL;
2214 struct xyc_softc *xycsc;
2215 int rseg, error;
2216 bus_dma_segment_t seg;
2217
2218 /* check sanity of requested command */
2219
2220 switch (xio->cmd) {
2221
2222 case XYCMD_NOP: /* no op: everything should be zero */
2223 if (xio->subfn || xio->dptr || xio->dlen ||
2224 xio->block || xio->sectcnt)
2225 return (EINVAL);
2226 break;
2227
2228 case XYCMD_RD: /* read / write sectors (up to XD_IOCMD_MAXS) */
2229 case XYCMD_WR:
2230 if (xio->subfn || xio->sectcnt > XD_IOCMD_MAXS ||
2231 xio->sectcnt * XYFM_BPS != xio->dlen || xio->dptr == NULL)
2232 return (EINVAL);
2233 break;
2234
2235 case XYCMD_SK: /* seek: doesn't seem useful to export this */
2236 return (EINVAL);
2237
2238 break;
2239
2240 default:
2241 return (EINVAL);/* ??? */
2242 }
2243
2244 xycsc = xy->parent;
2245
2246 /* create DVMA buffer for request if needed */
2247 if (xio->dlen) {
2248 bus_addr_t busbuf;
2249
2250 if ((error = xy_dmamem_alloc(xycsc->dmatag, xycsc->auxmap,
2251 &seg, &rseg,
2252 xio->dlen, &buf,
2253 &busbuf)) != 0) {
2254 return (error);
2255 }
2256 dvmabuf = (caddr_t)(u_long)BUS_ADDR_PADDR(busbuf);
2257
2258 if (xio->cmd == XYCMD_WR) {
2259 if ((error = copyin(xio->dptr, buf, xio->dlen)) != 0) {
2260 bus_dmamem_unmap(xycsc->dmatag, buf, xio->dlen);
2261 bus_dmamem_free(xycsc->dmatag, &seg, rseg);
2262 return (error);
2263 }
2264 }
2265 }
2266 /* do it! */
2267
2268 error = 0;
2269 s = splbio();
2270 rqno = xyc_cmd(xycsc, xio->cmd, xio->subfn, xy->xy_drive, xio->block,
2271 xio->sectcnt, dvmabuf, XY_SUB_WAIT);
2272 if (rqno == XY_ERR_FAIL) {
2273 error = EIO;
2274 goto done;
2275 }
2276 xio->errno = xycsc->ciorq->errno;
2277 xio->tries = xycsc->ciorq->tries;
2278 XYC_DONE(xycsc, dummy);
2279
2280 if (xio->cmd == XYCMD_RD)
2281 error = copyout(buf, xio->dptr, xio->dlen);
2282
2283 done:
2284 splx(s);
2285 if (dvmabuf) {
2286 xy_dmamem_free(xycsc->dmatag, xycsc->auxmap, &seg, rseg,
2287 xio->dlen, buf);
2288 }
2289 return (error);
2290 }
2291
2292 /*
2293 * xyc_e2str: convert error code number into an error string
2294 */
2295 const char *
2296 xyc_e2str(no)
2297 int no;
2298 {
2299 switch (no) {
2300 case XY_ERR_FAIL:
2301 return ("Software fatal error");
2302 case XY_ERR_DERR:
2303 return ("DOUBLE ERROR");
2304 case XY_ERR_AOK:
2305 return ("Successful completion");
2306 case XY_ERR_IPEN:
2307 return("Interrupt pending");
2308 case XY_ERR_BCFL:
2309 return("Busy conflict");
2310 case XY_ERR_TIMO:
2311 return("Operation timeout");
2312 case XY_ERR_NHDR:
2313 return("Header not found");
2314 case XY_ERR_HARD:
2315 return("Hard ECC error");
2316 case XY_ERR_ICYL:
2317 return("Illegal cylinder address");
2318 case XY_ERR_ISEC:
2319 return("Illegal sector address");
2320 case XY_ERR_SMAL:
2321 return("Last sector too small");
2322 case XY_ERR_SACK:
2323 return("Slave ACK error (non-existent memory)");
2324 case XY_ERR_CHER:
2325 return("Cylinder and head/header error");
2326 case XY_ERR_SRTR:
2327 return("Auto-seek retry successful");
2328 case XY_ERR_WPRO:
2329 return("Write-protect error");
2330 case XY_ERR_UIMP:
2331 return("Unimplemented command");
2332 case XY_ERR_DNRY:
2333 return("Drive not ready");
2334 case XY_ERR_SZER:
2335 return("Sector count zero");
2336 case XY_ERR_DFLT:
2337 return("Drive faulted");
2338 case XY_ERR_ISSZ:
2339 return("Illegal sector size");
2340 case XY_ERR_SLTA:
2341 return("Self test A");
2342 case XY_ERR_SLTB:
2343 return("Self test B");
2344 case XY_ERR_SLTC:
2345 return("Self test C");
2346 case XY_ERR_SOFT:
2347 return("Soft ECC error");
2348 case XY_ERR_SFOK:
2349 return("Soft ECC error recovered");
2350 case XY_ERR_IHED:
2351 return("Illegal head");
2352 case XY_ERR_DSEQ:
2353 return("Disk sequencer error");
2354 case XY_ERR_SEEK:
2355 return("Seek error");
2356 default:
2357 return ("Unknown error");
2358 }
2359 }
2360
2361 int
2362 xyc_entoact(errno)
2363
2364 int errno;
2365
2366 {
2367 switch (errno) {
2368 case XY_ERR_FAIL: case XY_ERR_DERR: case XY_ERR_IPEN:
2369 case XY_ERR_BCFL: case XY_ERR_ICYL: case XY_ERR_ISEC:
2370 case XY_ERR_UIMP: case XY_ERR_SZER: case XY_ERR_ISSZ:
2371 case XY_ERR_SLTA: case XY_ERR_SLTB: case XY_ERR_SLTC:
2372 case XY_ERR_IHED: case XY_ERR_SACK: case XY_ERR_SMAL:
2373
2374 return(XY_ERA_PROG); /* program error ! */
2375
2376 case XY_ERR_TIMO: case XY_ERR_NHDR: case XY_ERR_HARD:
2377 case XY_ERR_DNRY: case XY_ERR_CHER: case XY_ERR_SEEK:
2378 case XY_ERR_SOFT:
2379
2380 return(XY_ERA_HARD); /* hard error, retry */
2381
2382 case XY_ERR_DFLT: case XY_ERR_DSEQ:
2383
2384 return(XY_ERA_RSET); /* hard error reset */
2385
2386 case XY_ERR_SRTR: case XY_ERR_SFOK: case XY_ERR_AOK:
2387
2388 return(XY_ERA_SOFT); /* an FYI error */
2389
2390 case XY_ERR_WPRO:
2391
2392 return(XY_ERA_WPRO); /* write protect */
2393 }
2394
2395 return(XY_ERA_PROG); /* ??? */
2396 }
2397