xy.c revision 1.63 1 /* $NetBSD: xy.c,v 1.63 2006/05/10 06:24:03 skrll 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.63 2006/05/10 06:24:03 skrll 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 = suser(l->l_proc->p_ucred, &l->l_proc->p_acflag)) != 0)
1036 return (error);
1037 return (xyc_ioctlcmd(xy, dev, xio));
1038
1039 default:
1040 return ENOTTY;
1041 }
1042 }
1043
1044 /*
1045 * xyopen: open drive
1046 */
1047
1048 int
1049 xyopen(dev, flag, fmt, l)
1050 dev_t dev;
1051 int flag, fmt;
1052 struct lwp *l;
1053 {
1054 int unit, part;
1055 struct xy_softc *xy;
1056 struct xyc_attach_args xa;
1057
1058 /* first, could it be a valid target? */
1059
1060 unit = DISKUNIT(dev);
1061 if (unit >= xy_cd.cd_ndevs || (xy = xy_cd.cd_devs[unit]) == NULL)
1062 return (ENXIO);
1063 part = DISKPART(dev);
1064
1065 /* do we need to attach the drive? */
1066
1067 if (xy->state == XY_DRIVE_UNKNOWN) {
1068 xa.driveno = xy->xy_drive;
1069 xa.fullmode = XY_SUB_WAIT;
1070 xa.booting = 0;
1071 xyattach((struct device *) xy->parent,
1072 (struct device *) xy, &xa);
1073 if (xy->state == XY_DRIVE_UNKNOWN) {
1074 return (EIO);
1075 }
1076 }
1077 /* check for partition */
1078
1079 if (part != RAW_PART &&
1080 (part >= xy->sc_dk.dk_label->d_npartitions ||
1081 xy->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
1082 return (ENXIO);
1083 }
1084 /* set open masks */
1085
1086 switch (fmt) {
1087 case S_IFCHR:
1088 xy->sc_dk.dk_copenmask |= (1 << part);
1089 break;
1090 case S_IFBLK:
1091 xy->sc_dk.dk_bopenmask |= (1 << part);
1092 break;
1093 }
1094 xy->sc_dk.dk_openmask = xy->sc_dk.dk_copenmask | xy->sc_dk.dk_bopenmask;
1095
1096 return 0;
1097 }
1098
1099 int
1100 xyread(dev, uio, flags)
1101 dev_t dev;
1102 struct uio *uio;
1103 int flags;
1104 {
1105
1106 return (physio(xystrategy, NULL, dev, B_READ, minphys, uio));
1107 }
1108
1109 int
1110 xywrite(dev, uio, flags)
1111 dev_t dev;
1112 struct uio *uio;
1113 int flags;
1114 {
1115
1116 return (physio(xystrategy, NULL, dev, B_WRITE, minphys, uio));
1117 }
1118
1119
1120 /*
1121 * xysize: return size of a partition for a dump
1122 */
1123
1124 int
1125 xysize(dev)
1126 dev_t dev;
1127
1128 {
1129 struct xy_softc *xysc;
1130 int unit, part, size, omask;
1131
1132 /* valid unit? */
1133 unit = DISKUNIT(dev);
1134 if (unit >= xy_cd.cd_ndevs || (xysc = xy_cd.cd_devs[unit]) == NULL)
1135 return (-1);
1136
1137 part = DISKPART(dev);
1138 omask = xysc->sc_dk.dk_openmask & (1 << part);
1139
1140 if (omask == 0 && xyopen(dev, 0, S_IFBLK, NULL) != 0)
1141 return (-1);
1142
1143 /* do it */
1144 if (xysc->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
1145 size = -1; /* only give valid size for swap partitions */
1146 else
1147 size = xysc->sc_dk.dk_label->d_partitions[part].p_size *
1148 (xysc->sc_dk.dk_label->d_secsize / DEV_BSIZE);
1149 if (omask == 0 && xyclose(dev, 0, S_IFBLK, NULL) != 0)
1150 return (-1);
1151 return (size);
1152 }
1153
1154 /*
1155 * xystrategy: buffering system interface to xy.
1156 */
1157
1158 void
1159 xystrategy(bp)
1160 struct buf *bp;
1161
1162 {
1163 struct xy_softc *xy;
1164 int s, unit;
1165 struct xyc_attach_args xa;
1166 struct disklabel *lp;
1167 daddr_t blkno;
1168
1169 unit = DISKUNIT(bp->b_dev);
1170
1171 /* check for live device */
1172
1173 if (unit >= xy_cd.cd_ndevs || (xy = xy_cd.cd_devs[unit]) == 0 ||
1174 bp->b_blkno < 0 ||
1175 (bp->b_bcount % xy->sc_dk.dk_label->d_secsize) != 0) {
1176 bp->b_error = EINVAL;
1177 goto bad;
1178 }
1179 /* do we need to attach the drive? */
1180
1181 if (xy->state == XY_DRIVE_UNKNOWN) {
1182 xa.driveno = xy->xy_drive;
1183 xa.fullmode = XY_SUB_WAIT;
1184 xa.booting = 0;
1185 xyattach((struct device *)xy->parent, (struct device *)xy, &xa);
1186 if (xy->state == XY_DRIVE_UNKNOWN) {
1187 bp->b_error = EIO;
1188 goto bad;
1189 }
1190 }
1191 if (xy->state != XY_DRIVE_ONLINE && DISKPART(bp->b_dev) != RAW_PART) {
1192 /* no I/O to unlabeled disks, unless raw partition */
1193 bp->b_error = EIO;
1194 goto bad;
1195 }
1196 /* short circuit zero length request */
1197
1198 if (bp->b_bcount == 0)
1199 goto done;
1200
1201 /* check bounds with label (disksubr.c). Determine the size of the
1202 * transfer, and make sure it is within the boundaries of the
1203 * partition. Adjust transfer if needed, and signal errors or early
1204 * completion. */
1205
1206 lp = xy->sc_dk.dk_label;
1207
1208 if (bounds_check_with_label(&xy->sc_dk, bp,
1209 (xy->flags & XY_WLABEL) != 0) <= 0)
1210 goto done;
1211
1212 /*
1213 * Now convert the block number to absolute and put it in
1214 * terms of the device's logical block size.
1215 */
1216 blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
1217 if (DISKPART(bp->b_dev) != RAW_PART)
1218 blkno += lp->d_partitions[DISKPART(bp->b_dev)].p_offset;
1219
1220 bp->b_rawblkno = blkno;
1221
1222 /*
1223 * now we know we have a valid buf structure that we need to do I/O
1224 * on.
1225 */
1226 s = splbio(); /* protect the queues */
1227
1228 BUFQ_PUT(xy->xyq, bp);
1229
1230 /* start 'em up */
1231
1232 xyc_start(xy->parent, NULL);
1233
1234 /* done! */
1235
1236 splx(s);
1237 return;
1238
1239 bad: /* tells upper layers we have an error */
1240 bp->b_flags |= B_ERROR;
1241 done: /* tells upper layers we are done with this
1242 * buf */
1243 bp->b_resid = bp->b_bcount;
1244 biodone(bp);
1245 }
1246 /*
1247 * end of {b,c}devsw functions
1248 */
1249
1250 /*
1251 * i n t e r r u p t f u n c t i o n
1252 *
1253 * xycintr: hardware interrupt.
1254 */
1255 int
1256 xycintr(v)
1257 void *v;
1258
1259 {
1260 struct xyc_softc *xycsc = v;
1261
1262 /* kick the event counter */
1263
1264 xycsc->sc_intrcnt.ev_count++;
1265
1266 /* remove as many done IOPBs as possible */
1267
1268 xyc_remove_iorq(xycsc);
1269
1270 /* start any iorq's already waiting */
1271
1272 xyc_start(xycsc, NULL);
1273
1274 return (1);
1275 }
1276 /*
1277 * end of interrupt function
1278 */
1279
1280 /*
1281 * i n t e r n a l f u n c t i o n s
1282 */
1283
1284 /*
1285 * xyc_rqinit: fill out the fields of an I/O request
1286 */
1287
1288 inline void
1289 xyc_rqinit(rq, xyc, xy, md, blk, cnt, db, bp)
1290 struct xy_iorq *rq;
1291 struct xyc_softc *xyc;
1292 struct xy_softc *xy;
1293 int md;
1294 u_long blk;
1295 int cnt;
1296 caddr_t db;
1297 struct buf *bp;
1298 {
1299 rq->xyc = xyc;
1300 rq->xy = xy;
1301 rq->ttl = XYC_MAXTTL + 10;
1302 rq->mode = md;
1303 rq->tries = rq->errno = rq->lasterror = 0;
1304 rq->blockno = blk;
1305 rq->sectcnt = cnt;
1306 rq->dbuf = db;
1307 rq->buf = bp;
1308 }
1309
1310 /*
1311 * xyc_rqtopb: load up an IOPB based on an iorq
1312 */
1313
1314 void
1315 xyc_rqtopb(iorq, iopb, cmd, subfun)
1316 struct xy_iorq *iorq;
1317 struct xy_iopb *iopb;
1318 int cmd, subfun;
1319
1320 {
1321 u_long block, dp;
1322
1323 /* normal IOPB case, standard stuff */
1324
1325 /* chain bit handled later */
1326 iopb->ien = (XY_STATE(iorq->mode) == XY_SUB_POLL) ? 0 : 1;
1327 iopb->com = cmd;
1328 iopb->errno = 0;
1329 iopb->errs = 0;
1330 iopb->done = 0;
1331 if (iorq->xy) {
1332 iopb->unit = iorq->xy->xy_drive;
1333 iopb->dt = iorq->xy->drive_type;
1334 } else {
1335 iopb->unit = 0;
1336 iopb->dt = 0;
1337 }
1338 block = iorq->blockno;
1339 if (iorq->xy == NULL || block == 0) {
1340 iopb->sect = iopb->head = iopb->cyl = 0;
1341 } else {
1342 iopb->sect = block % iorq->xy->nsect;
1343 block = block / iorq->xy->nsect;
1344 iopb->head = block % iorq->xy->nhead;
1345 block = block / iorq->xy->nhead;
1346 iopb->cyl = block;
1347 }
1348 iopb->scnt = iorq->sectcnt;
1349 dp = (u_long) iorq->dbuf;
1350 if (iorq->dbuf == NULL) {
1351 iopb->dataa = 0;
1352 iopb->datar = 0;
1353 } else {
1354 iopb->dataa = (dp & 0xffff);
1355 iopb->datar = ((dp & 0xff0000) >> 16);
1356 }
1357 iopb->subfn = subfun;
1358 }
1359
1360
1361 /*
1362 * xyc_unbusy: wait for the xyc to go unbusy, or timeout.
1363 */
1364
1365 int
1366 xyc_unbusy(xyc, del)
1367
1368 struct xyc *xyc;
1369 int del;
1370
1371 {
1372 while (del-- > 0) {
1373 if ((xyc->xyc_csr & XYC_GBSY) == 0)
1374 break;
1375 DELAY(1);
1376 }
1377 return(del == 0 ? XY_ERR_FAIL : XY_ERR_AOK);
1378 }
1379
1380 /*
1381 * xyc_cmd: front end for POLL'd and WAIT'd commands. Returns 0 or error.
1382 * note that NORM requests are handled separately.
1383 */
1384 int
1385 xyc_cmd(xycsc, cmd, subfn, unit, block, scnt, dptr, fullmode)
1386 struct xyc_softc *xycsc;
1387 int cmd, subfn, unit, block, scnt;
1388 char *dptr;
1389 int fullmode;
1390
1391 {
1392 int submode = XY_STATE(fullmode);
1393 struct xy_iorq *iorq = xycsc->ciorq;
1394 struct xy_iopb *iopb = xycsc->ciopb;
1395
1396 /*
1397 * is someone else using the control iopq wait for it if we can
1398 */
1399 start:
1400 if (submode == XY_SUB_WAIT && XY_STATE(iorq->mode) != XY_SUB_FREE) {
1401 if (tsleep(iorq, PRIBIO, "xyc_cmd", 0))
1402 return(XY_ERR_FAIL);
1403 goto start;
1404 }
1405
1406 if (XY_STATE(iorq->mode) != XY_SUB_FREE) {
1407 DELAY(1000000); /* XY_SUB_POLL: steal the iorq */
1408 iorq->mode = XY_SUB_FREE;
1409 printf("%s: stole control iopb\n", xycsc->sc_dev.dv_xname);
1410 }
1411
1412 /* init iorq/iopb */
1413
1414 xyc_rqinit(iorq, xycsc,
1415 (unit == XYC_NOUNIT) ? NULL : xycsc->sc_drives[unit],
1416 fullmode, block, scnt, dptr, NULL);
1417
1418 /* load IOPB from iorq */
1419
1420 xyc_rqtopb(iorq, iopb, cmd, subfn);
1421
1422 /* submit it for processing */
1423
1424 xyc_submit_iorq(xycsc, iorq, fullmode); /* error code will be in iorq */
1425
1426 return(XY_ERR_AOK);
1427 }
1428
1429 /*
1430 * xyc_startbuf
1431 * start a buffer for running
1432 */
1433
1434 int
1435 xyc_startbuf(xycsc, xysc, bp)
1436 struct xyc_softc *xycsc;
1437 struct xy_softc *xysc;
1438 struct buf *bp;
1439
1440 {
1441 int partno, error;
1442 struct xy_iorq *iorq;
1443 struct xy_iopb *iopb;
1444 u_long block;
1445
1446 iorq = xysc->xyrq;
1447 iopb = iorq->iopb;
1448
1449 /* get buf */
1450
1451 if (bp == NULL)
1452 panic("xyc_startbuf null buf");
1453
1454 partno = DISKPART(bp->b_dev);
1455 #ifdef XYC_DEBUG
1456 printf("xyc_startbuf: %s%c: %s block %d\n", xysc->sc_dev.dv_xname,
1457 'a' + partno, (bp->b_flags & B_READ) ? "read" : "write", bp->b_blkno);
1458 printf("xyc_startbuf: b_bcount %d, b_data 0x%x\n",
1459 bp->b_bcount, bp->b_data);
1460 #endif
1461
1462 /*
1463 * load request.
1464 *
1465 * note that iorq points to the buffer as mapped into DVMA space,
1466 * where as the bp->b_data points to its non-DVMA mapping.
1467 */
1468
1469 block = bp->b_rawblkno;
1470
1471 error = bus_dmamap_load(xycsc->dmatag, iorq->dmamap,
1472 bp->b_data, bp->b_bcount, 0, BUS_DMA_NOWAIT);
1473 if (error != 0) {
1474 printf("%s: warning: cannot load DMA map\n",
1475 xycsc->sc_dev.dv_xname);
1476 return (XY_ERR_FAIL); /* XXX: need some sort of
1477 * call-back scheme here? */
1478 }
1479
1480 bus_dmamap_sync(xycsc->dmatag, iorq->dmamap, 0,
1481 iorq->dmamap->dm_mapsize, (bp->b_flags & B_READ)
1482 ? BUS_DMASYNC_PREREAD
1483 : BUS_DMASYNC_PREWRITE);
1484
1485 /* init iorq and load iopb from it */
1486 xyc_rqinit(iorq, xycsc, xysc, XY_SUB_NORM | XY_MODE_VERBO, block,
1487 bp->b_bcount / XYFM_BPS,
1488 (caddr_t)(u_long)iorq->dmamap->dm_segs[0].ds_addr,
1489 bp);
1490
1491 xyc_rqtopb(iorq, iopb, (bp->b_flags & B_READ) ? XYCMD_RD : XYCMD_WR, 0);
1492
1493 /* Instrumentation. */
1494 disk_busy(&xysc->sc_dk);
1495
1496 return (XY_ERR_AOK);
1497 }
1498
1499
1500 /*
1501 * xyc_submit_iorq: submit an iorq for processing. returns XY_ERR_AOK
1502 * if ok. if it fail returns an error code. type is XY_SUB_*.
1503 *
1504 * note: caller frees iorq in all cases except NORM
1505 *
1506 * return value:
1507 * NORM: XY_AOK (req pending), XY_FAIL (couldn't submit request)
1508 * WAIT: XY_AOK (success), <error-code> (failed)
1509 * POLL: <same as WAIT>
1510 * NOQ : <same as NORM>
1511 *
1512 * there are three sources for i/o requests:
1513 * [1] xystrategy: normal block I/O, using "struct buf" system.
1514 * [2] autoconfig/crash dump: these are polled I/O requests, no interrupts.
1515 * [3] open/ioctl: these are I/O requests done in the context of a process,
1516 * and the process should block until they are done.
1517 *
1518 * software state is stored in the iorq structure. each iorq has an
1519 * iopb structure. the hardware understands the iopb structure.
1520 * every command must go through an iopb. a 450 handles one iopb at a
1521 * time, where as a 451 can take them in chains. [the 450 claims it
1522 * can handle chains, but is appears to be buggy...] iopb are allocated
1523 * in DVMA space at boot up time. each disk gets one iopb, and the
1524 * controller gets one (for POLL and WAIT commands). what happens if
1525 * the iopb is busy? for i/o type [1], the buffers are queued at the
1526 * "buff" layer and * picked up later by the interrupt routine. for case
1527 * [2] we can only be blocked if there is a WAIT type I/O request being
1528 * run. since this can only happen when we are crashing, we wait a sec
1529 * and then steal the IOPB. for case [3] the process can sleep
1530 * on the iorq free list until some iopbs are available.
1531 */
1532
1533
1534 int
1535 xyc_submit_iorq(xycsc, iorq, type)
1536 struct xyc_softc *xycsc;
1537 struct xy_iorq *iorq;
1538 int type;
1539
1540 {
1541 struct xy_iopb *dmaiopb;
1542
1543 #ifdef XYC_DEBUG
1544 printf("xyc_submit_iorq(%s, addr=0x%x, type=%d)\n",
1545 xycsc->sc_dev.dv_xname, iorq, type);
1546 #endif
1547
1548 /* first check and see if controller is busy */
1549 if ((xycsc->xyc->xyc_csr & XYC_GBSY) != 0) {
1550 #ifdef XYC_DEBUG
1551 printf("xyc_submit_iorq: XYC not ready (BUSY)\n");
1552 #endif
1553 if (type == XY_SUB_NOQ)
1554 return (XY_ERR_FAIL); /* failed */
1555 switch (type) {
1556 case XY_SUB_NORM:
1557 return XY_ERR_AOK; /* success */
1558 case XY_SUB_WAIT:
1559 while (iorq->iopb->done == 0) {
1560 (void) tsleep(iorq, PRIBIO, "xyciorq", 0);
1561 }
1562 return (iorq->errno);
1563 case XY_SUB_POLL: /* steal controller */
1564 (void)xycsc->xyc->xyc_rsetup; /* RESET */
1565 if (xyc_unbusy(xycsc->xyc,XYC_RESETUSEC) == XY_ERR_FAIL)
1566 panic("xyc_submit_iorq: stuck xyc");
1567 printf("%s: stole controller\n",
1568 xycsc->sc_dev.dv_xname);
1569 break;
1570 default:
1571 panic("xyc_submit_iorq adding");
1572 }
1573 }
1574
1575 dmaiopb = xyc_chain(xycsc, iorq); /* build chain */
1576 if (dmaiopb == NULL) { /* nothing doing? */
1577 if (type == XY_SUB_NORM || type == XY_SUB_NOQ)
1578 return(XY_ERR_AOK);
1579 panic("xyc_submit_iorq: xyc_chain failed!");
1580 }
1581
1582 XYC_GO(xycsc->xyc, dmaiopb);
1583
1584 /* command now running, wrap it up */
1585 switch (type) {
1586 case XY_SUB_NORM:
1587 case XY_SUB_NOQ:
1588 return (XY_ERR_AOK); /* success */
1589 case XY_SUB_WAIT:
1590 while (iorq->iopb->done == 0) {
1591 (void) tsleep(iorq, PRIBIO, "xyciorq", 0);
1592 }
1593 return (iorq->errno);
1594 case XY_SUB_POLL:
1595 return (xyc_piodriver(xycsc, iorq));
1596 default:
1597 panic("xyc_submit_iorq wrap up");
1598 }
1599 panic("xyc_submit_iorq");
1600 return 0; /* not reached */
1601 }
1602
1603
1604 /*
1605 * xyc_chain: build a chain. return dvma address of first element in
1606 * the chain. iorq != NULL: means we only want that item on the chain.
1607 */
1608
1609 struct xy_iopb *
1610 xyc_chain(xycsc, iorq)
1611 struct xyc_softc *xycsc;
1612 struct xy_iorq *iorq;
1613
1614 {
1615 int togo, chain, hand;
1616
1617 bzero(xycsc->xy_chain, sizeof(xycsc->xy_chain));
1618
1619 /*
1620 * promote control IOPB to the top
1621 */
1622 if (iorq == NULL) {
1623 if ((XY_STATE(xycsc->reqs[XYC_CTLIOPB].mode) == XY_SUB_POLL ||
1624 XY_STATE(xycsc->reqs[XYC_CTLIOPB].mode) == XY_SUB_WAIT) &&
1625 xycsc->iopbase[XYC_CTLIOPB].done == 0)
1626 iorq = &xycsc->reqs[XYC_CTLIOPB];
1627 }
1628
1629 /*
1630 * special case: if iorq != NULL then we have a POLL or WAIT request.
1631 * we let these take priority and do them first.
1632 */
1633 if (iorq) {
1634 xycsc->xy_chain[0] = iorq;
1635 iorq->iopb->chen = 0;
1636 return(iorq->dmaiopb);
1637 }
1638
1639 /*
1640 * NORM case: do round robin and maybe chain (if allowed and possible)
1641 */
1642 chain = 0;
1643 hand = xycsc->xy_hand;
1644 xycsc->xy_hand = (xycsc->xy_hand + 1) % XYC_MAXIOPB;
1645
1646 for (togo = XYC_MAXIOPB; togo > 0;
1647 togo--, hand = (hand + 1) % XYC_MAXIOPB) {
1648 struct xy_iopb *iopb, *prev_iopb, *dmaiopb;
1649
1650 if (XY_STATE(xycsc->reqs[hand].mode) != XY_SUB_NORM ||
1651 xycsc->iopbase[hand].done)
1652 continue; /* not ready-for-i/o */
1653
1654 xycsc->xy_chain[chain] = &xycsc->reqs[hand];
1655 iopb = xycsc->xy_chain[chain]->iopb;
1656 iopb->chen = 0;
1657 if (chain != 0) {
1658 /* adding a link to a chain */
1659 prev_iopb = xycsc->xy_chain[chain-1]->iopb;
1660 prev_iopb->chen = 1;
1661 dmaiopb = xycsc->xy_chain[chain]->dmaiopb;
1662 prev_iopb->nxtiopb = ((u_long)dmaiopb) & 0xffff;
1663 } else {
1664 /* head of chain */
1665 iorq = xycsc->xy_chain[chain];
1666 }
1667 chain++;
1668
1669 /* quit if chaining dis-allowed */
1670 if (xycsc->no_ols)
1671 break;
1672 }
1673
1674 return(iorq ? iorq->dmaiopb : NULL);
1675 }
1676
1677 /*
1678 * xyc_piodriver
1679 *
1680 * programmed i/o driver. this function takes over the computer
1681 * and drains off the polled i/o request. it returns the status of the iorq
1682 * the caller is interesting in.
1683 */
1684 int
1685 xyc_piodriver(xycsc, iorq)
1686 struct xyc_softc *xycsc;
1687 struct xy_iorq *iorq;
1688
1689 {
1690 int nreset = 0;
1691 int retval = 0;
1692 u_long res;
1693 #ifdef XYC_DEBUG
1694 printf("xyc_piodriver(%s, 0x%x)\n", xycsc->sc_dev.dv_xname, iorq);
1695 #endif
1696
1697 while (iorq->iopb->done == 0) {
1698
1699 res = xyc_unbusy(xycsc->xyc, XYC_MAXTIME);
1700
1701 /* we expect some progress soon */
1702 if (res == XY_ERR_FAIL && nreset >= 2) {
1703 xyc_reset(xycsc, 0, XY_RSET_ALL, XY_ERR_FAIL, 0);
1704 #ifdef XYC_DEBUG
1705 printf("xyc_piodriver: timeout\n");
1706 #endif
1707 return (XY_ERR_FAIL);
1708 }
1709 if (res == XY_ERR_FAIL) {
1710 if (xyc_reset(xycsc, 0,
1711 (nreset++ == 0) ? XY_RSET_NONE : iorq,
1712 XY_ERR_FAIL,
1713 0) == XY_ERR_FAIL)
1714 return (XY_ERR_FAIL); /* flushes all but POLL
1715 * requests, resets */
1716 continue;
1717 }
1718
1719 xyc_remove_iorq(xycsc); /* may resubmit request */
1720
1721 if (iorq->iopb->done == 0)
1722 xyc_start(xycsc, iorq);
1723 }
1724
1725 /* get return value */
1726
1727 retval = iorq->errno;
1728
1729 #ifdef XYC_DEBUG
1730 printf("xyc_piodriver: done, retval = 0x%x (%s)\n",
1731 iorq->errno, xyc_e2str(iorq->errno));
1732 #endif
1733
1734 /* start up any bufs that have queued */
1735
1736 xyc_start(xycsc, NULL);
1737
1738 return (retval);
1739 }
1740
1741 /*
1742 * xyc_xyreset: reset one drive. NOTE: assumes xyc was just reset.
1743 * we steal iopb[XYC_CTLIOPB] for this, but we put it back when we are done.
1744 */
1745 void
1746 xyc_xyreset(xycsc, xysc)
1747 struct xyc_softc *xycsc;
1748 struct xy_softc *xysc;
1749
1750 {
1751 struct xy_iopb tmpiopb;
1752 struct xy_iopb *iopb;
1753 int del;
1754
1755 iopb = xycsc->ciopb;
1756
1757 /* Save contents */
1758 bcopy(iopb, &tmpiopb, sizeof(struct xy_iopb));
1759
1760 iopb->chen = iopb->done = iopb->errs = 0;
1761 iopb->ien = 0;
1762 iopb->com = XYCMD_RST;
1763 iopb->unit = xysc->xy_drive;
1764
1765 XYC_GO(xycsc->xyc, xycsc->ciorq->dmaiopb);
1766
1767 del = XYC_RESETUSEC;
1768 while (del > 0) {
1769 if ((xycsc->xyc->xyc_csr & XYC_GBSY) == 0)
1770 break;
1771 DELAY(1);
1772 del--;
1773 }
1774
1775 if (del <= 0 || iopb->errs) {
1776 printf("%s: off-line: %s\n", xycsc->sc_dev.dv_xname,
1777 xyc_e2str(iopb->errno));
1778 del = xycsc->xyc->xyc_rsetup;
1779 if (xyc_unbusy(xycsc->xyc, XYC_RESETUSEC) == XY_ERR_FAIL)
1780 panic("xyc_reset");
1781 } else {
1782 xycsc->xyc->xyc_csr = XYC_IPND; /* clear IPND */
1783 }
1784
1785 /* Restore contents */
1786 bcopy(&tmpiopb, iopb, sizeof(struct xy_iopb));
1787 }
1788
1789
1790 /*
1791 * xyc_reset: reset everything: requests are marked as errors except
1792 * a polled request (which is resubmitted)
1793 */
1794 int
1795 xyc_reset(xycsc, quiet, blastmode, error, xysc)
1796 struct xyc_softc *xycsc;
1797 int quiet, error;
1798 struct xy_iorq *blastmode;
1799 struct xy_softc *xysc;
1800
1801 {
1802 int del = 0, lcv, retval = XY_ERR_AOK;
1803
1804 /* soft reset hardware */
1805
1806 if (!quiet)
1807 printf("%s: soft reset\n", xycsc->sc_dev.dv_xname);
1808 del = xycsc->xyc->xyc_rsetup;
1809 del = xyc_unbusy(xycsc->xyc, XYC_RESETUSEC);
1810 if (del == XY_ERR_FAIL) {
1811 blastmode = XY_RSET_ALL; /* dead, flush all requests */
1812 retval = XY_ERR_FAIL;
1813 }
1814 if (xysc)
1815 xyc_xyreset(xycsc, xysc);
1816
1817 /* fix queues based on "blast-mode" */
1818
1819 for (lcv = 0; lcv < XYC_MAXIOPB; lcv++) {
1820 register struct xy_iorq *iorq = &xycsc->reqs[lcv];
1821
1822 if (XY_STATE(iorq->mode) != XY_SUB_POLL &&
1823 XY_STATE(iorq->mode) != XY_SUB_WAIT &&
1824 XY_STATE(iorq->mode) != XY_SUB_NORM)
1825 /* is it active? */
1826 continue;
1827
1828 if (blastmode == XY_RSET_ALL ||
1829 blastmode != iorq) {
1830 /* failed */
1831 iorq->errno = error;
1832 xycsc->iopbase[lcv].done = xycsc->iopbase[lcv].errs = 1;
1833 switch (XY_STATE(iorq->mode)) {
1834 case XY_SUB_NORM:
1835 iorq->buf->b_error = EIO;
1836 iorq->buf->b_flags |= B_ERROR;
1837 iorq->buf->b_resid = iorq->sectcnt * XYFM_BPS;
1838
1839 bus_dmamap_sync(xycsc->dmatag, iorq->dmamap, 0,
1840 iorq->dmamap->dm_mapsize,
1841 (iorq->buf->b_flags & B_READ)
1842 ? BUS_DMASYNC_POSTREAD
1843 : BUS_DMASYNC_POSTWRITE);
1844
1845 bus_dmamap_unload(xycsc->dmatag, iorq->dmamap);
1846
1847 (void)BUFQ_GET(iorq->xy->xyq);
1848 disk_unbusy(&xycsc->reqs[lcv].xy->sc_dk,
1849 (xycsc->reqs[lcv].buf->b_bcount -
1850 xycsc->reqs[lcv].buf->b_resid),
1851 (xycsc->reqs[lcv].buf->b_flags & B_READ));
1852 biodone(iorq->buf);
1853 iorq->mode = XY_SUB_FREE;
1854 break;
1855 case XY_SUB_WAIT:
1856 wakeup(iorq);
1857 case XY_SUB_POLL:
1858 iorq->mode =
1859 XY_NEWSTATE(iorq->mode, XY_SUB_DONE);
1860 break;
1861 }
1862
1863 } else {
1864
1865 /* resubmit, no need to do anything here */
1866 }
1867 }
1868
1869 /*
1870 * now, if stuff is waiting, start it.
1871 * since we just reset it should go
1872 */
1873 xyc_start(xycsc, NULL);
1874
1875 return (retval);
1876 }
1877
1878 /*
1879 * xyc_start: start waiting buffers
1880 */
1881
1882 void
1883 xyc_start(xycsc, iorq)
1884 struct xyc_softc *xycsc;
1885 struct xy_iorq *iorq;
1886
1887 {
1888 int lcv;
1889 struct xy_softc *xy;
1890
1891 if (iorq == NULL) {
1892 for (lcv = 0; lcv < XYC_MAXDEV ; lcv++) {
1893 if ((xy = xycsc->sc_drives[lcv]) == NULL) continue;
1894 if (BUFQ_PEEK(xy->xyq) == NULL) continue;
1895 if (xy->xyrq->mode != XY_SUB_FREE) continue;
1896 xyc_startbuf(xycsc, xy, BUFQ_PEEK(xy->xyq));
1897 }
1898 }
1899 xyc_submit_iorq(xycsc, iorq, XY_SUB_NOQ);
1900 }
1901
1902 /*
1903 * xyc_remove_iorq: remove "done" IOPB's.
1904 */
1905
1906 int
1907 xyc_remove_iorq(xycsc)
1908 struct xyc_softc *xycsc;
1909
1910 {
1911 int errno, rq, comm, errs;
1912 struct xyc *xyc = xycsc->xyc;
1913 u_long addr;
1914 struct xy_iopb *iopb;
1915 struct xy_iorq *iorq;
1916 struct buf *bp;
1917
1918 if (xyc->xyc_csr & XYC_DERR) {
1919 /*
1920 * DOUBLE ERROR: should never happen under normal use. This
1921 * error is so bad, you can't even tell which IOPB is bad, so
1922 * we dump them all.
1923 */
1924 errno = XY_ERR_DERR;
1925 printf("%s: DOUBLE ERROR!\n", xycsc->sc_dev.dv_xname);
1926 if (xyc_reset(xycsc, 0, XY_RSET_ALL, errno, 0) != XY_ERR_AOK) {
1927 printf("%s: soft reset failed!\n",
1928 xycsc->sc_dev.dv_xname);
1929 panic("xyc_remove_iorq: controller DEAD");
1930 }
1931 return (XY_ERR_AOK);
1932 }
1933
1934 /*
1935 * get iopb that is done, loop down the chain
1936 */
1937
1938 if (xyc->xyc_csr & XYC_ERR) {
1939 xyc->xyc_csr = XYC_ERR; /* clear error condition */
1940 }
1941 if (xyc->xyc_csr & XYC_IPND) {
1942 xyc->xyc_csr = XYC_IPND; /* clear interrupt */
1943 }
1944
1945 for (rq = 0; rq < XYC_MAXIOPB; rq++) {
1946 iorq = xycsc->xy_chain[rq];
1947 if (iorq == NULL) break; /* done ! */
1948 if (iorq->mode == 0 || XY_STATE(iorq->mode) == XY_SUB_DONE)
1949 continue; /* free, or done */
1950 iopb = iorq->iopb;
1951 if (iopb->done == 0)
1952 continue; /* not done yet */
1953
1954 comm = iopb->com;
1955 errs = iopb->errs;
1956
1957 if (errs)
1958 iorq->errno = iopb->errno;
1959 else
1960 iorq->errno = 0;
1961
1962 /* handle non-fatal errors */
1963
1964 if (errs &&
1965 xyc_error(xycsc, iorq, iopb, comm) == XY_ERR_AOK)
1966 continue; /* AOK: we resubmitted it */
1967
1968
1969 /* this iorq is now done (hasn't been restarted or anything) */
1970
1971 if ((iorq->mode & XY_MODE_VERBO) && iorq->lasterror)
1972 xyc_perror(iorq, iopb, 0);
1973
1974 /* now, if read/write check to make sure we got all the data
1975 * we needed. (this may not be the case if we got an error in
1976 * the middle of a multisector request). */
1977
1978 if ((iorq->mode & XY_MODE_B144) != 0 && errs == 0 &&
1979 (comm == XYCMD_RD || comm == XYCMD_WR)) {
1980 /* we just successfully processed a bad144 sector
1981 * note: if we are in bad 144 mode, the pointers have
1982 * been advanced already (see above) and are pointing
1983 * at the bad144 sector. to exit bad144 mode, we
1984 * must advance the pointers 1 sector and issue a new
1985 * request if there are still sectors left to process
1986 *
1987 */
1988 XYC_ADVANCE(iorq, 1); /* advance 1 sector */
1989
1990 /* exit b144 mode */
1991 iorq->mode = iorq->mode & (~XY_MODE_B144);
1992
1993 if (iorq->sectcnt) { /* more to go! */
1994 iorq->lasterror = iorq->errno = iopb->errno = 0;
1995 iopb->errs = iopb->done = 0;
1996 iorq->tries = 0;
1997 iopb->scnt = iorq->sectcnt;
1998 iopb->cyl = iorq->blockno /
1999 iorq->xy->sectpercyl;
2000 iopb->head =
2001 (iorq->blockno / iorq->xy->nhead) %
2002 iorq->xy->nhead;
2003 iopb->sect = iorq->blockno % XYFM_BPS;
2004 addr = (u_long) iorq->dbuf;
2005 iopb->dataa = (addr & 0xffff);
2006 iopb->datar = ((addr & 0xff0000) >> 16);
2007 /* will resubit at end */
2008 continue;
2009 }
2010 }
2011 /* final cleanup, totally done with this request */
2012
2013 switch (XY_STATE(iorq->mode)) {
2014 case XY_SUB_NORM:
2015 bp = iorq->buf;
2016 if (errs) {
2017 bp->b_error = EIO;
2018 bp->b_flags |= B_ERROR;
2019 bp->b_resid = iorq->sectcnt * XYFM_BPS;
2020 } else {
2021 bp->b_resid = 0; /* done */
2022 }
2023 bus_dmamap_sync(xycsc->dmatag, iorq->dmamap, 0,
2024 iorq->dmamap->dm_mapsize,
2025 (iorq->buf->b_flags & B_READ)
2026 ? BUS_DMASYNC_POSTREAD
2027 : BUS_DMASYNC_POSTWRITE);
2028
2029 bus_dmamap_unload(xycsc->dmatag, iorq->dmamap);
2030
2031 (void)BUFQ_GET(iorq->xy->xyq);
2032 disk_unbusy(&iorq->xy->sc_dk,
2033 (bp->b_bcount - bp->b_resid),
2034 (bp->b_flags & B_READ));
2035 iorq->mode = XY_SUB_FREE;
2036 biodone(bp);
2037 break;
2038 case XY_SUB_WAIT:
2039 iorq->mode = XY_NEWSTATE(iorq->mode, XY_SUB_DONE);
2040 wakeup(iorq);
2041 break;
2042 case XY_SUB_POLL:
2043 iorq->mode = XY_NEWSTATE(iorq->mode, XY_SUB_DONE);
2044 break;
2045 }
2046 }
2047
2048 return (XY_ERR_AOK);
2049 }
2050
2051 /*
2052 * xyc_perror: print error.
2053 * - if still_trying is true: we got an error, retried and got a
2054 * different error. in that case lasterror is the old error,
2055 * and errno is the new one.
2056 * - if still_trying is not true, then if we ever had an error it
2057 * is in lasterror. also, if iorq->errno == 0, then we recovered
2058 * from that error (otherwise iorq->errno == iorq->lasterror).
2059 */
2060 void
2061 xyc_perror(iorq, iopb, still_trying)
2062 struct xy_iorq *iorq;
2063 struct xy_iopb *iopb;
2064 int still_trying;
2065
2066 {
2067
2068 int error = iorq->lasterror;
2069
2070 printf("%s", (iorq->xy) ? iorq->xy->sc_dev.dv_xname
2071 : iorq->xyc->sc_dev.dv_xname);
2072 if (iorq->buf)
2073 printf("%c: ", 'a' + DISKPART(iorq->buf->b_dev));
2074 if (iopb->com == XYCMD_RD || iopb->com == XYCMD_WR)
2075 printf("%s %d/%d/%d: ",
2076 (iopb->com == XYCMD_RD) ? "read" : "write",
2077 iopb->cyl, iopb->head, iopb->sect);
2078 printf("%s", xyc_e2str(error));
2079
2080 if (still_trying)
2081 printf(" [still trying, new error=%s]", xyc_e2str(iorq->errno));
2082 else
2083 if (iorq->errno == 0)
2084 printf(" [recovered in %d tries]", iorq->tries);
2085
2086 printf("\n");
2087 }
2088
2089 /*
2090 * xyc_error: non-fatal error encountered... recover.
2091 * return AOK if resubmitted, return FAIL if this iopb is done
2092 */
2093 int
2094 xyc_error(xycsc, iorq, iopb, comm)
2095 struct xyc_softc *xycsc;
2096 struct xy_iorq *iorq;
2097 struct xy_iopb *iopb;
2098 int comm;
2099
2100 {
2101 int errno = iorq->errno;
2102 int erract = xyc_entoact(errno);
2103 int oldmode, advance;
2104 #ifdef __sparc__
2105 int i;
2106 #endif
2107
2108 if (erract == XY_ERA_RSET) { /* some errors require a reset */
2109 oldmode = iorq->mode;
2110 iorq->mode = XY_SUB_DONE | (~XY_SUB_MASK & oldmode);
2111 /* make xyc_start ignore us */
2112 xyc_reset(xycsc, 1, XY_RSET_NONE, errno, iorq->xy);
2113 iorq->mode = oldmode;
2114 }
2115 /* check for read/write to a sector in bad144 table if bad: redirect
2116 * request to bad144 area */
2117
2118 if ((comm == XYCMD_RD || comm == XYCMD_WR) &&
2119 (iorq->mode & XY_MODE_B144) == 0) {
2120 advance = iorq->sectcnt - iopb->scnt;
2121 XYC_ADVANCE(iorq, advance);
2122 #ifdef __sparc__
2123 if ((i = isbad(&iorq->xy->dkb, iorq->blockno / iorq->xy->sectpercyl,
2124 (iorq->blockno / iorq->xy->nsect) % iorq->xy->nhead,
2125 iorq->blockno % iorq->xy->nsect)) != -1) {
2126 iorq->mode |= XY_MODE_B144; /* enter bad144 mode &
2127 * redirect */
2128 iopb->errno = iopb->done = iopb->errs = 0;
2129 iopb->scnt = 1;
2130 iopb->cyl = (iorq->xy->ncyl + iorq->xy->acyl) - 2;
2131 /* second to last acyl */
2132 i = iorq->xy->sectpercyl - 1 - i; /* follow bad144
2133 * standard */
2134 iopb->head = i / iorq->xy->nhead;
2135 iopb->sect = i % iorq->xy->nhead;
2136 /* will resubmit when we come out of remove_iorq */
2137 return (XY_ERR_AOK); /* recovered! */
2138 }
2139 #endif
2140 }
2141
2142 /*
2143 * it isn't a bad144 sector, must be real error! see if we can retry
2144 * it?
2145 */
2146 if ((iorq->mode & XY_MODE_VERBO) && iorq->lasterror)
2147 xyc_perror(iorq, iopb, 1); /* inform of error state
2148 * change */
2149 iorq->lasterror = errno;
2150
2151 if ((erract == XY_ERA_RSET || erract == XY_ERA_HARD)
2152 && iorq->tries < XYC_MAXTRIES) { /* retry? */
2153 iorq->tries++;
2154 iorq->errno = iopb->errno = iopb->done = iopb->errs = 0;
2155 /* will resubmit at end of remove_iorq */
2156 return (XY_ERR_AOK); /* recovered! */
2157 }
2158
2159 /* failed to recover from this error */
2160 return (XY_ERR_FAIL);
2161 }
2162
2163 /*
2164 * xyc_tick: make sure xy is still alive and ticking (err, kicking).
2165 */
2166 void
2167 xyc_tick(arg)
2168 void *arg;
2169
2170 {
2171 struct xyc_softc *xycsc = arg;
2172 int lcv, s, reset = 0;
2173
2174 /* reduce ttl for each request if one goes to zero, reset xyc */
2175 s = splbio();
2176 for (lcv = 0; lcv < XYC_MAXIOPB; lcv++) {
2177 if (xycsc->reqs[lcv].mode == 0 ||
2178 XY_STATE(xycsc->reqs[lcv].mode) == XY_SUB_DONE)
2179 continue;
2180 xycsc->reqs[lcv].ttl--;
2181 if (xycsc->reqs[lcv].ttl == 0)
2182 reset = 1;
2183 }
2184 if (reset) {
2185 printf("%s: watchdog timeout\n", xycsc->sc_dev.dv_xname);
2186 xyc_reset(xycsc, 0, XY_RSET_NONE, XY_ERR_FAIL, NULL);
2187 }
2188 splx(s);
2189
2190 /* until next time */
2191
2192 callout_reset(&xycsc->sc_tick_ch, XYC_TICKCNT, xyc_tick, xycsc);
2193 }
2194
2195 /*
2196 * xyc_ioctlcmd: this function provides a user level interface to the
2197 * controller via ioctl. this allows "format" programs to be written
2198 * in user code, and is also useful for some debugging. we return
2199 * an error code. called at user priority.
2200 *
2201 * XXX missing a few commands (see the 7053 driver for ideas)
2202 */
2203 int
2204 xyc_ioctlcmd(xy, dev, xio)
2205 struct xy_softc *xy;
2206 dev_t dev;
2207 struct xd_iocmd *xio;
2208
2209 {
2210 int s, rqno, dummy = 0;
2211 caddr_t dvmabuf = NULL, buf = NULL;
2212 struct xyc_softc *xycsc;
2213 int rseg, error;
2214 bus_dma_segment_t seg;
2215
2216 /* check sanity of requested command */
2217
2218 switch (xio->cmd) {
2219
2220 case XYCMD_NOP: /* no op: everything should be zero */
2221 if (xio->subfn || xio->dptr || xio->dlen ||
2222 xio->block || xio->sectcnt)
2223 return (EINVAL);
2224 break;
2225
2226 case XYCMD_RD: /* read / write sectors (up to XD_IOCMD_MAXS) */
2227 case XYCMD_WR:
2228 if (xio->subfn || xio->sectcnt > XD_IOCMD_MAXS ||
2229 xio->sectcnt * XYFM_BPS != xio->dlen || xio->dptr == NULL)
2230 return (EINVAL);
2231 break;
2232
2233 case XYCMD_SK: /* seek: doesn't seem useful to export this */
2234 return (EINVAL);
2235
2236 break;
2237
2238 default:
2239 return (EINVAL);/* ??? */
2240 }
2241
2242 xycsc = xy->parent;
2243
2244 /* create DVMA buffer for request if needed */
2245 if (xio->dlen) {
2246 bus_addr_t busbuf;
2247
2248 if ((error = xy_dmamem_alloc(xycsc->dmatag, xycsc->auxmap,
2249 &seg, &rseg,
2250 xio->dlen, &buf,
2251 &busbuf)) != 0) {
2252 return (error);
2253 }
2254 dvmabuf = (caddr_t)(u_long)BUS_ADDR_PADDR(busbuf);
2255
2256 if (xio->cmd == XYCMD_WR) {
2257 if ((error = copyin(xio->dptr, buf, xio->dlen)) != 0) {
2258 bus_dmamem_unmap(xycsc->dmatag, buf, xio->dlen);
2259 bus_dmamem_free(xycsc->dmatag, &seg, rseg);
2260 return (error);
2261 }
2262 }
2263 }
2264 /* do it! */
2265
2266 error = 0;
2267 s = splbio();
2268 rqno = xyc_cmd(xycsc, xio->cmd, xio->subfn, xy->xy_drive, xio->block,
2269 xio->sectcnt, dvmabuf, XY_SUB_WAIT);
2270 if (rqno == XY_ERR_FAIL) {
2271 error = EIO;
2272 goto done;
2273 }
2274 xio->errno = xycsc->ciorq->errno;
2275 xio->tries = xycsc->ciorq->tries;
2276 XYC_DONE(xycsc, dummy);
2277
2278 if (xio->cmd == XYCMD_RD)
2279 error = copyout(buf, xio->dptr, xio->dlen);
2280
2281 done:
2282 splx(s);
2283 if (dvmabuf) {
2284 xy_dmamem_free(xycsc->dmatag, xycsc->auxmap, &seg, rseg,
2285 xio->dlen, buf);
2286 }
2287 return (error);
2288 }
2289
2290 /*
2291 * xyc_e2str: convert error code number into an error string
2292 */
2293 const char *
2294 xyc_e2str(no)
2295 int no;
2296 {
2297 switch (no) {
2298 case XY_ERR_FAIL:
2299 return ("Software fatal error");
2300 case XY_ERR_DERR:
2301 return ("DOUBLE ERROR");
2302 case XY_ERR_AOK:
2303 return ("Successful completion");
2304 case XY_ERR_IPEN:
2305 return("Interrupt pending");
2306 case XY_ERR_BCFL:
2307 return("Busy conflict");
2308 case XY_ERR_TIMO:
2309 return("Operation timeout");
2310 case XY_ERR_NHDR:
2311 return("Header not found");
2312 case XY_ERR_HARD:
2313 return("Hard ECC error");
2314 case XY_ERR_ICYL:
2315 return("Illegal cylinder address");
2316 case XY_ERR_ISEC:
2317 return("Illegal sector address");
2318 case XY_ERR_SMAL:
2319 return("Last sector too small");
2320 case XY_ERR_SACK:
2321 return("Slave ACK error (non-existent memory)");
2322 case XY_ERR_CHER:
2323 return("Cylinder and head/header error");
2324 case XY_ERR_SRTR:
2325 return("Auto-seek retry successful");
2326 case XY_ERR_WPRO:
2327 return("Write-protect error");
2328 case XY_ERR_UIMP:
2329 return("Unimplemented command");
2330 case XY_ERR_DNRY:
2331 return("Drive not ready");
2332 case XY_ERR_SZER:
2333 return("Sector count zero");
2334 case XY_ERR_DFLT:
2335 return("Drive faulted");
2336 case XY_ERR_ISSZ:
2337 return("Illegal sector size");
2338 case XY_ERR_SLTA:
2339 return("Self test A");
2340 case XY_ERR_SLTB:
2341 return("Self test B");
2342 case XY_ERR_SLTC:
2343 return("Self test C");
2344 case XY_ERR_SOFT:
2345 return("Soft ECC error");
2346 case XY_ERR_SFOK:
2347 return("Soft ECC error recovered");
2348 case XY_ERR_IHED:
2349 return("Illegal head");
2350 case XY_ERR_DSEQ:
2351 return("Disk sequencer error");
2352 case XY_ERR_SEEK:
2353 return("Seek error");
2354 default:
2355 return ("Unknown error");
2356 }
2357 }
2358
2359 int
2360 xyc_entoact(errno)
2361
2362 int errno;
2363
2364 {
2365 switch (errno) {
2366 case XY_ERR_FAIL: case XY_ERR_DERR: case XY_ERR_IPEN:
2367 case XY_ERR_BCFL: case XY_ERR_ICYL: case XY_ERR_ISEC:
2368 case XY_ERR_UIMP: case XY_ERR_SZER: case XY_ERR_ISSZ:
2369 case XY_ERR_SLTA: case XY_ERR_SLTB: case XY_ERR_SLTC:
2370 case XY_ERR_IHED: case XY_ERR_SACK: case XY_ERR_SMAL:
2371
2372 return(XY_ERA_PROG); /* program error ! */
2373
2374 case XY_ERR_TIMO: case XY_ERR_NHDR: case XY_ERR_HARD:
2375 case XY_ERR_DNRY: case XY_ERR_CHER: case XY_ERR_SEEK:
2376 case XY_ERR_SOFT:
2377
2378 return(XY_ERA_HARD); /* hard error, retry */
2379
2380 case XY_ERR_DFLT: case XY_ERR_DSEQ:
2381
2382 return(XY_ERA_RSET); /* hard error reset */
2383
2384 case XY_ERR_SRTR: case XY_ERR_SFOK: case XY_ERR_AOK:
2385
2386 return(XY_ERA_SOFT); /* an FYI error */
2387
2388 case XY_ERR_WPRO:
2389
2390 return(XY_ERA_WPRO); /* write protect */
2391 }
2392
2393 return(XY_ERA_PROG); /* ??? */
2394 }
2395