xy.c revision 1.72 1 /* $NetBSD: xy.c,v 1.72 2007/07/29 12:15:44 ad 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.72 2007/07/29 12:15:44 ad 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 #include <sys/kauth.h>
80
81 #include <machine/bus.h>
82 #include <machine/intr.h>
83
84 #if defined(__sparc__) || defined(sun3)
85 #include <dev/sun/disklabel.h>
86 #endif
87
88 #include <dev/vme/vmereg.h>
89 #include <dev/vme/vmevar.h>
90
91 #include <dev/vme/xyreg.h>
92 #include <dev/vme/xyvar.h>
93 #include <dev/vme/xio.h>
94
95 #include "locators.h"
96
97 /*
98 * macros
99 */
100
101 /*
102 * XYC_GO: start iopb ADDR (DVMA addr in a u_long) on XYC
103 */
104 #define XYC_GO(XYC, ADDR) { \
105 u_long addr = (u_long)ADDR; \
106 (XYC)->xyc_addr_lo = ((addr) & 0xff); \
107 (addr) = ((addr) >> 8); \
108 (XYC)->xyc_addr_hi = ((addr) & 0xff); \
109 (addr) = ((addr) >> 8); \
110 (XYC)->xyc_reloc_lo = ((addr) & 0xff); \
111 (addr) = ((addr) >> 8); \
112 (XYC)->xyc_reloc_hi = (addr); \
113 (XYC)->xyc_csr = XYC_GBSY; /* go! */ \
114 }
115
116 /*
117 * XYC_DONE: don't need IORQ, get error code and free (done after xyc_cmd)
118 */
119
120 #define XYC_DONE(SC,ER) { \
121 if ((ER) == XY_ERR_AOK) { \
122 (ER) = (SC)->ciorq->errnum; \
123 (SC)->ciorq->mode = XY_SUB_FREE; \
124 wakeup((SC)->ciorq); \
125 } \
126 }
127
128 /*
129 * XYC_ADVANCE: advance iorq's pointers by a number of sectors
130 */
131
132 #define XYC_ADVANCE(IORQ, N) { \
133 if (N) { \
134 (IORQ)->sectcnt -= (N); \
135 (IORQ)->blockno += (N); \
136 (IORQ)->dbuf += ((N)*XYFM_BPS); \
137 } \
138 }
139
140 /*
141 * note - addresses you can sleep on:
142 * [1] & of xy_softc's "state" (waiting for a chance to attach a drive)
143 * [2] & an iorq (waiting for an XY_SUB_WAIT iorq to finish)
144 */
145
146
147 /*
148 * function prototypes
149 * "xyc_*" functions are internal, all others are external interfaces
150 */
151
152 extern int pil_to_vme[]; /* from obio.c */
153
154 /* internals */
155 struct xy_iopb *xyc_chain(struct xyc_softc *, struct xy_iorq *);
156 int xyc_cmd(struct xyc_softc *, int, int, int, int, int, char *, int);
157 const char *xyc_e2str(int);
158 int xyc_entoact(int);
159 int xyc_error(struct xyc_softc *, struct xy_iorq *,
160 struct xy_iopb *, int);
161 int xyc_ioctlcmd(struct xy_softc *, dev_t dev, struct xd_iocmd *);
162 void xyc_perror(struct xy_iorq *, struct xy_iopb *, int);
163 int xyc_piodriver(struct xyc_softc *, struct xy_iorq *);
164 int xyc_remove_iorq(struct xyc_softc *);
165 int xyc_reset(struct xyc_softc *, int, struct xy_iorq *, int,
166 struct xy_softc *);
167 inline void xyc_rqinit(struct xy_iorq *, struct xyc_softc *,
168 struct xy_softc *, int, u_long, int,
169 void *, struct buf *);
170 void xyc_rqtopb(struct xy_iorq *, struct xy_iopb *, int, int);
171 void xyc_start(struct xyc_softc *, struct xy_iorq *);
172 int xyc_startbuf(struct xyc_softc *, struct xy_softc *, struct buf *);
173 int xyc_submit_iorq(struct xyc_softc *, struct xy_iorq *, int);
174 void xyc_tick(void *);
175 int xyc_unbusy(struct xyc *, int);
176 void xyc_xyreset(struct xyc_softc *, struct xy_softc *);
177 int xy_dmamem_alloc(bus_dma_tag_t, bus_dmamap_t, bus_dma_segment_t *,
178 int *, bus_size_t, void **, bus_addr_t *);
179 void xy_dmamem_free(bus_dma_tag_t, bus_dmamap_t, bus_dma_segment_t *,
180 int, bus_size_t, void *);
181
182 /* machine interrupt hook */
183 int xycintr(void *);
184
185 /* autoconf */
186 int xycmatch(struct device *, struct cfdata *, void *);
187 void xycattach(struct device *, struct device *, void *);
188 int xymatch(struct device *, struct cfdata *, void *);
189 void xyattach(struct device *, struct device *, void *);
190 static int xyc_probe(void *, bus_space_tag_t, bus_space_handle_t);
191
192 static void xydummystrat(struct buf *);
193 int xygetdisklabel(struct xy_softc *, void *);
194
195 /*
196 * cfattach's: device driver interface to autoconfig
197 */
198
199 CFATTACH_DECL(xyc, sizeof(struct xyc_softc),
200 xycmatch, xycattach, NULL, NULL);
201
202 CFATTACH_DECL(xy, sizeof(struct xy_softc),
203 xymatch, xyattach, NULL, NULL);
204
205 extern struct cfdriver xy_cd;
206
207 dev_type_open(xyopen);
208 dev_type_close(xyclose);
209 dev_type_read(xyread);
210 dev_type_write(xywrite);
211 dev_type_ioctl(xyioctl);
212 dev_type_strategy(xystrategy);
213 dev_type_dump(xydump);
214 dev_type_size(xysize);
215
216 const struct bdevsw xy_bdevsw = {
217 xyopen, xyclose, xystrategy, xyioctl, xydump, xysize, D_DISK
218 };
219
220 const struct cdevsw xy_cdevsw = {
221 xyopen, xyclose, xyread, xywrite, xyioctl,
222 nostop, notty, nopoll, nommap, nokqfilter, D_DISK
223 };
224
225 struct xyc_attach_args { /* this is the "aux" args to xyattach */
226 int driveno; /* unit number */
227 int fullmode; /* submit mode */
228 int booting; /* are we booting or not? */
229 };
230
231 /*
232 * dkdriver
233 */
234
235 struct dkdriver xydkdriver = { xystrategy };
236
237 /*
238 * start: disk label fix code (XXX)
239 */
240
241 static void *xy_labeldata;
242
243 static void
244 xydummystrat(bp)
245 struct buf *bp;
246 {
247 if (bp->b_bcount != XYFM_BPS)
248 panic("xydummystrat");
249 bcopy(xy_labeldata, bp->b_data, XYFM_BPS);
250 bp->b_flags |= B_DONE;
251 bp->b_flags &= ~B_BUSY;
252 }
253
254 int
255 xygetdisklabel(xy, b)
256 struct xy_softc *xy;
257 void *b;
258 {
259 const char *err;
260 #if defined(__sparc__) || defined(sun3)
261 struct sun_disklabel *sdl;
262 #endif
263
264 /* We already have the label data in `b'; setup for dummy strategy */
265 xy_labeldata = b;
266
267 /* Required parameter for readdisklabel() */
268 xy->sc_dk.dk_label->d_secsize = XYFM_BPS;
269
270 err = readdisklabel(MAKEDISKDEV(0, device_unit(&xy->sc_dev), RAW_PART),
271 xydummystrat,
272 xy->sc_dk.dk_label, xy->sc_dk.dk_cpulabel);
273 if (err) {
274 printf("%s: %s\n", xy->sc_dev.dv_xname, err);
275 return(XY_ERR_FAIL);
276 }
277
278 #if defined(__sparc__) || defined(sun3)
279 /* Ok, we have the label; fill in `pcyl' if there's SunOS magic */
280 sdl = (struct sun_disklabel *)xy->sc_dk.dk_cpulabel->cd_block;
281 if (sdl->sl_magic == SUN_DKMAGIC) {
282 xy->pcyl = sdl->sl_pcylinders;
283 } else
284 #endif
285 {
286 printf("%s: WARNING: no `pcyl' in disk label.\n",
287 xy->sc_dev.dv_xname);
288 xy->pcyl = xy->sc_dk.dk_label->d_ncylinders +
289 xy->sc_dk.dk_label->d_acylinders;
290 printf("%s: WARNING: guessing pcyl=%d (ncyl+acyl)\n",
291 xy->sc_dev.dv_xname, xy->pcyl);
292 }
293
294 xy->ncyl = xy->sc_dk.dk_label->d_ncylinders;
295 xy->acyl = xy->sc_dk.dk_label->d_acylinders;
296 xy->nhead = xy->sc_dk.dk_label->d_ntracks;
297 xy->nsect = xy->sc_dk.dk_label->d_nsectors;
298 xy->sectpercyl = xy->nhead * xy->nsect;
299 xy->sc_dk.dk_label->d_secsize = XYFM_BPS; /* not handled by
300 * sun->bsd */
301 return(XY_ERR_AOK);
302 }
303
304 /*
305 * end: disk label fix code (XXX)
306 */
307
308 /*
309 * Shorthand for allocating, mapping and loading a DMA buffer
310 */
311 int
312 xy_dmamem_alloc(tag, map, seg, nsegp, len, kvap, dmap)
313 bus_dma_tag_t tag;
314 bus_dmamap_t map;
315 bus_dma_segment_t *seg;
316 int *nsegp;
317 bus_size_t len;
318 void * *kvap;
319 bus_addr_t *dmap;
320 {
321 int nseg;
322 int error;
323
324 if ((error = bus_dmamem_alloc(tag, len, 0, 0,
325 seg, 1, &nseg, BUS_DMA_NOWAIT)) != 0) {
326 return (error);
327 }
328
329 if ((error = bus_dmamem_map(tag, seg, nseg,
330 len, kvap,
331 BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
332 bus_dmamem_free(tag, seg, nseg);
333 return (error);
334 }
335
336 if ((error = bus_dmamap_load(tag, map, *kvap, len, NULL,
337 BUS_DMA_NOWAIT)) != 0) {
338 bus_dmamem_unmap(tag, *kvap, len);
339 bus_dmamem_free(tag, seg, nseg);
340 return (error);
341 }
342
343 *dmap = map->dm_segs[0].ds_addr;
344 *nsegp = nseg;
345 return (0);
346 }
347
348 void
349 xy_dmamem_free(tag, map, seg, nseg, len, kva)
350 bus_dma_tag_t tag;
351 bus_dmamap_t map;
352 bus_dma_segment_t *seg;
353 int nseg;
354 bus_size_t len;
355 void * kva;
356 {
357
358 bus_dmamap_unload(tag, map);
359 bus_dmamem_unmap(tag, kva, len);
360 bus_dmamem_free(tag, seg, nseg);
361 }
362
363
364 /*
365 * a u t o c o n f i g f u n c t i o n s
366 */
367
368 /*
369 * xycmatch: determine if xyc is present or not. we do a
370 * soft reset to detect the xyc.
371 */
372 int
373 xyc_probe(arg, tag, handle)
374 void *arg;
375 bus_space_tag_t tag;
376 bus_space_handle_t handle;
377 {
378 struct xyc *xyc = (void *)handle; /* XXX */
379
380 return ((xyc_unbusy(xyc, XYC_RESETUSEC) != XY_ERR_FAIL) ? 0 : EIO);
381 }
382
383 int xycmatch(parent, cf, aux)
384 struct device *parent;
385 struct cfdata *cf;
386 void *aux;
387 {
388 struct vme_attach_args *va = aux;
389 vme_chipset_tag_t ct = va->va_vct;
390 vme_am_t mod;
391 int error;
392
393 mod = VME_AM_A16 | VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA;
394 if (vme_space_alloc(ct, va->r[0].offset, sizeof(struct xyc), mod))
395 return (0);
396
397 error = vme_probe(ct, va->r[0].offset, sizeof(struct xyc),
398 mod, VME_D16, xyc_probe, 0);
399 vme_space_free(va->va_vct, va->r[0].offset, sizeof(struct xyc), mod);
400
401 return (error == 0);
402 }
403
404 /*
405 * xycattach: attach controller
406 */
407 void
408 xycattach(parent, self, aux)
409 struct device *parent, *self;
410 void *aux;
411
412 {
413 struct xyc_softc *xyc = (void *) self;
414 struct vme_attach_args *va = aux;
415 vme_chipset_tag_t ct = va->va_vct;
416 bus_space_tag_t bt;
417 bus_space_handle_t bh;
418 vme_intr_handle_t ih;
419 vme_am_t mod;
420 struct xyc_attach_args xa;
421 int lcv, res, error;
422 bus_dma_segment_t seg;
423 int rseg;
424 vme_mapresc_t resc;
425 bus_addr_t busaddr;
426
427 /* get addressing and intr level stuff from autoconfig and load it
428 * into our xyc_softc. */
429
430 mod = VME_AM_A16 | VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA;
431
432 if (vme_space_alloc(ct, va->r[0].offset, sizeof(struct xyc), mod))
433 panic("xyc: vme alloc");
434
435 if (vme_space_map(ct, va->r[0].offset, sizeof(struct xyc),
436 mod, VME_D16, 0, &bt, &bh, &resc) != 0)
437 panic("xyc: vme_map");
438
439 xyc->xyc = (struct xyc *) bh; /* XXX */
440 xyc->ipl = va->ilevel;
441 xyc->vector = va->ivector;
442 xyc->no_ols = 0; /* XXX should be from config */
443
444 for (lcv = 0; lcv < XYC_MAXDEV; lcv++)
445 xyc->sc_drives[lcv] = (struct xy_softc *) 0;
446
447 /*
448 * allocate and zero buffers
449 * check boundaries of the KVA's ... all IOPBs must reside in
450 * the same 64K region.
451 */
452
453 /* Get DMA handle for misc. transfers */
454 if ((error = vme_dmamap_create(
455 ct, /* VME chip tag */
456 MAXPHYS, /* size */
457 VME_AM_A24, /* address modifier */
458 VME_D16, /* data size */
459 0, /* swap */
460 1, /* nsegments */
461 MAXPHYS, /* maxsegsz */
462 0, /* boundary */
463 BUS_DMA_NOWAIT,
464 &xyc->auxmap)) != 0) {
465
466 printf("%s: DMA buffer map create error %d\n",
467 xyc->sc_dev.dv_xname, error);
468 return;
469 }
470
471 /* Get DMA handle for mapping iorq descriptors */
472 if ((error = vme_dmamap_create(
473 ct, /* VME chip tag */
474 XYC_MAXIOPB * sizeof(struct xy_iopb),
475 VME_AM_A24, /* address modifier */
476 VME_D16, /* data size */
477 0, /* swap */
478 1, /* nsegments */
479 XYC_MAXIOPB * sizeof(struct xy_iopb),
480 64*1024, /* boundary */
481 BUS_DMA_NOWAIT,
482 &xyc->iopmap)) != 0) {
483
484 printf("%s: DMA buffer map create error %d\n",
485 xyc->sc_dev.dv_xname, error);
486 return;
487 }
488
489 /* Get DMA buffer for iorq descriptors */
490 if ((error = xy_dmamem_alloc(xyc->dmatag, xyc->iopmap, &seg, &rseg,
491 XYC_MAXIOPB * sizeof(struct xy_iopb),
492 (void **)&xyc->iopbase,
493 &busaddr)) != 0) {
494 printf("%s: DMA buffer alloc error %d\n",
495 xyc->sc_dev.dv_xname, error);
496 return;
497 }
498 xyc->dvmaiopb = (struct xy_iopb *)(u_long)BUS_ADDR_PADDR(busaddr);
499
500 bzero(xyc->iopbase, XYC_MAXIOPB * sizeof(struct xy_iopb));
501
502 xyc->reqs = (struct xy_iorq *)
503 malloc(XYC_MAXIOPB * sizeof(struct xy_iorq),
504 M_DEVBUF, M_NOWAIT|M_ZERO);
505 if (xyc->reqs == NULL)
506 panic("xyc malloc");
507
508 /*
509 * init iorq to iopb pointers, and non-zero fields in the
510 * iopb which never change.
511 */
512
513 for (lcv = 0; lcv < XYC_MAXIOPB; lcv++) {
514 xyc->xy_chain[lcv] = NULL;
515 xyc->reqs[lcv].iopb = &xyc->iopbase[lcv];
516 xyc->reqs[lcv].dmaiopb = &xyc->dvmaiopb[lcv];
517 xyc->iopbase[lcv].asr = 1; /* always the same */
518 xyc->iopbase[lcv].eef = 1; /* always the same */
519 xyc->iopbase[lcv].ecm = XY_ECM; /* always the same */
520 xyc->iopbase[lcv].aud = 1; /* always the same */
521 xyc->iopbase[lcv].relo = 1; /* always the same */
522 xyc->iopbase[lcv].thro = XY_THRO;/* always the same */
523
524 if ((error = vme_dmamap_create(
525 ct, /* VME chip tag */
526 MAXPHYS, /* size */
527 VME_AM_A24, /* address modifier */
528 VME_D16, /* data size */
529 0, /* swap */
530 1, /* nsegments */
531 MAXPHYS, /* maxsegsz */
532 0, /* boundary */
533 BUS_DMA_NOWAIT,
534 &xyc->reqs[lcv].dmamap)) != 0) {
535
536 printf("%s: DMA buffer map create error %d\n",
537 xyc->sc_dev.dv_xname, error);
538 return;
539 }
540 }
541 xyc->ciorq = &xyc->reqs[XYC_CTLIOPB]; /* short hand name */
542 xyc->ciopb = &xyc->iopbase[XYC_CTLIOPB]; /* short hand name */
543 xyc->xy_hand = 0;
544
545 /* read controller parameters and insure we have a 450/451 */
546
547 error = xyc_cmd(xyc, XYCMD_ST, 0, 0, 0, 0, 0, XY_SUB_POLL);
548 res = xyc->ciopb->ctyp;
549 XYC_DONE(xyc, error);
550 if (res != XYCT_450) {
551 if (error)
552 printf(": %s: ", xyc_e2str(error));
553 printf(": doesn't identify as a 450/451\n");
554 return;
555 }
556 printf(": Xylogics 450/451");
557 if (xyc->no_ols)
558 printf(" [OLS disabled]"); /* 450 doesn't overlap seek right */
559 printf("\n");
560 if (error) {
561 printf("%s: error: %s\n", xyc->sc_dev.dv_xname,
562 xyc_e2str(error));
563 return;
564 }
565 if ((xyc->xyc->xyc_csr & XYC_ADRM) == 0) {
566 printf("%s: 24 bit addressing turned off\n",
567 xyc->sc_dev.dv_xname);
568 printf("please set hardware jumpers JM1-JM2=in, JM3-JM4=out\n");
569 printf("to enable 24 bit mode and this driver\n");
570 return;
571 }
572
573 /* link in interrupt with higher level software */
574 vme_intr_map(ct, va->ilevel, va->ivector, &ih);
575 vme_intr_establish(ct, ih, IPL_BIO, xycintr, xyc);
576 evcnt_attach_dynamic(&xyc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
577 xyc->sc_dev.dv_xname, "intr");
578
579 callout_init(&xyc->sc_tick_ch, 0);
580
581 /* now we must look for disks using autoconfig */
582 xa.fullmode = XY_SUB_POLL;
583 xa.booting = 1;
584
585 for (xa.driveno = 0; xa.driveno < XYC_MAXDEV; xa.driveno++)
586 (void) config_found(self, (void *) &xa, NULL);
587
588 /* start the watchdog clock */
589 callout_reset(&xyc->sc_tick_ch, XYC_TICKCNT, xyc_tick, xyc);
590
591 }
592
593 /*
594 * xymatch: probe for disk.
595 *
596 * note: we almost always say disk is present. this allows us to
597 * spin up and configure a disk after the system is booted (we can
598 * call xyattach!).
599 */
600 int
601 xymatch(parent, cf, aux)
602 struct device *parent;
603 struct cfdata *cf;
604 void *aux;
605 {
606 struct xyc_attach_args *xa = aux;
607
608 /* looking for autoconf wildcard or exact match */
609
610 if (cf->cf_loc[XYCCF_DRIVE] != XYCCF_DRIVE_DEFAULT &&
611 cf->cf_loc[XYCCF_DRIVE] != xa->driveno)
612 return 0;
613
614 return 1;
615
616 }
617
618 /*
619 * xyattach: attach a disk. this can be called from autoconf and also
620 * from xyopen/xystrategy.
621 */
622 void
623 xyattach(parent, self, aux)
624 struct device *parent, *self;
625 void *aux;
626
627 {
628 struct xy_softc *xy = (void *) self, *oxy;
629 struct xyc_softc *xyc = (void *) parent;
630 struct xyc_attach_args *xa = aux;
631 int spt, mb, blk, lcv, fmode, s = 0, newstate;
632 struct dkbad *dkb;
633 int rseg, error;
634 bus_dma_segment_t seg;
635 bus_addr_t busaddr;
636 void * dmaddr;
637 char * buf;
638
639 /*
640 * Always re-initialize the disk structure. We want statistics
641 * to start with a clean slate.
642 */
643 bzero(&xy->sc_dk, sizeof(xy->sc_dk));
644 xy->sc_dk.dk_driver = &xydkdriver;
645 xy->sc_dk.dk_name = xy->sc_dev.dv_xname;
646
647 /* if booting, init the xy_softc */
648
649 if (xa->booting) {
650 xy->state = XY_DRIVE_UNKNOWN; /* to start */
651 xy->flags = 0;
652 xy->parent = xyc;
653
654 /* init queue of waiting bufs */
655
656 bufq_alloc(&xy->xyq, "disksort", BUFQ_SORT_RAWBLOCK);
657
658 xy->xyrq = &xyc->reqs[xa->driveno];
659
660 }
661 xy->xy_drive = xa->driveno;
662 fmode = xa->fullmode;
663 xyc->sc_drives[xa->driveno] = xy;
664
665 /* if not booting, make sure we are the only process in the attach for
666 * this drive. if locked out, sleep on it. */
667
668 if (!xa->booting) {
669 s = splbio();
670 while (xy->state == XY_DRIVE_ATTACHING) {
671 if (tsleep(&xy->state, PRIBIO, "xyattach", 0)) {
672 splx(s);
673 return;
674 }
675 }
676 printf("%s at %s",
677 xy->sc_dev.dv_xname, xy->parent->sc_dev.dv_xname);
678 }
679
680 /* we now have control */
681 xy->state = XY_DRIVE_ATTACHING;
682 newstate = XY_DRIVE_UNKNOWN;
683
684 buf = NULL;
685 if ((error = xy_dmamem_alloc(xyc->dmatag, xyc->auxmap, &seg, &rseg,
686 XYFM_BPS,
687 (void **)&buf,
688 &busaddr)) != 0) {
689 printf("%s: DMA buffer alloc error %d\n",
690 xyc->sc_dev.dv_xname, error);
691 return;
692 }
693 dmaddr = (void *)(u_long)BUS_ADDR_PADDR(busaddr);
694
695 /* first try and reset the drive */
696 error = xyc_cmd(xyc, XYCMD_RST, 0, xy->xy_drive, 0, 0, 0, fmode);
697 XYC_DONE(xyc, error);
698 if (error == XY_ERR_DNRY) {
699 printf(" drive %d: off-line\n", xa->driveno);
700 goto done;
701 }
702 if (error) {
703 printf(": ERROR 0x%02x (%s)\n", error, xyc_e2str(error));
704 goto done;
705 }
706 printf(" drive %d: ready", xa->driveno);
707
708 /*
709 * now set drive parameters (to semi-bogus values) so we can read the
710 * disk label.
711 */
712 xy->pcyl = xy->ncyl = 1;
713 xy->acyl = 0;
714 xy->nhead = 1;
715 xy->nsect = 1;
716 xy->sectpercyl = 1;
717 for (lcv = 0; lcv < 126; lcv++) /* init empty bad144 table */
718 xy->dkb.bt_bad[lcv].bt_cyl =
719 xy->dkb.bt_bad[lcv].bt_trksec = 0xffff;
720
721 /* read disk label */
722 for (xy->drive_type = 0 ; xy->drive_type <= XYC_MAXDT ;
723 xy->drive_type++) {
724 error = xyc_cmd(xyc, XYCMD_RD, 0, xy->xy_drive, 0, 1,
725 dmaddr, fmode);
726 XYC_DONE(xyc, error);
727 if (error == XY_ERR_AOK) break;
728 }
729
730 if (error != XY_ERR_AOK) {
731 printf("\n%s: reading disk label failed: %s\n",
732 xy->sc_dev.dv_xname, xyc_e2str(error));
733 goto done;
734 }
735 printf(" (drive type %d)\n", xy->drive_type);
736
737 newstate = XY_DRIVE_NOLABEL;
738
739 xy->hw_spt = spt = 0; /* XXX needed ? */
740 /* Attach the disk: must be before getdisklabel to malloc label */
741 disk_attach(&xy->sc_dk);
742
743 if (xygetdisklabel(xy, buf) != XY_ERR_AOK)
744 goto done;
745
746 /* inform the user of what is up */
747 printf("%s: <%s>, pcyl %d\n", xy->sc_dev.dv_xname,
748 buf, xy->pcyl);
749 mb = xy->ncyl * (xy->nhead * xy->nsect) / (1048576 / XYFM_BPS);
750 printf("%s: %dMB, %d cyl, %d head, %d sec, %d bytes/sec\n",
751 xy->sc_dev.dv_xname, mb, xy->ncyl, xy->nhead, xy->nsect,
752 XYFM_BPS);
753
754 /*
755 * 450/451 stupidity: the drive type is encoded into the format
756 * of the disk. the drive type in the IOPB must match the drive
757 * type in the format, or you will not be able to do I/O to the
758 * disk (you get header not found errors). if you have two drives
759 * of different sizes that have the same drive type in their
760 * formatting then you are out of luck.
761 *
762 * this problem was corrected in the 753/7053.
763 */
764
765 for (lcv = 0 ; lcv < XYC_MAXDEV ; lcv++) {
766 oxy = xyc->sc_drives[lcv];
767 if (oxy == NULL || oxy == xy) continue;
768 if (oxy->drive_type != xy->drive_type) continue;
769 if (xy->nsect != oxy->nsect || xy->pcyl != oxy->pcyl ||
770 xy->nhead != oxy->nhead) {
771 printf("%s: %s and %s must be the same size!\n",
772 xyc->sc_dev.dv_xname, xy->sc_dev.dv_xname,
773 oxy->sc_dev.dv_xname);
774 panic("xy drive size mismatch");
775 }
776 }
777
778
779 /* now set the real drive parameters! */
780
781 blk = (xy->nsect - 1) +
782 ((xy->nhead - 1) * xy->nsect) +
783 ((xy->pcyl - 1) * xy->nsect * xy->nhead);
784 error = xyc_cmd(xyc, XYCMD_SDS, 0, xy->xy_drive, blk, 0, 0, fmode);
785 XYC_DONE(xyc, error);
786 if (error) {
787 printf("%s: write drive size failed: %s\n",
788 xy->sc_dev.dv_xname, xyc_e2str(error));
789 goto done;
790 }
791 newstate = XY_DRIVE_ONLINE;
792
793 /*
794 * read bad144 table. this table resides on the first sector of the
795 * last track of the disk (i.e. second cyl of "acyl" area).
796 */
797
798 blk = (xy->ncyl + xy->acyl - 1) * (xy->nhead * xy->nsect) +
799 /* last cyl */
800 (xy->nhead - 1) * xy->nsect; /* last head */
801 error = xyc_cmd(xyc, XYCMD_RD, 0, xy->xy_drive, blk, 1,
802 dmaddr, fmode);
803 XYC_DONE(xyc, error);
804 if (error) {
805 printf("%s: reading bad144 failed: %s\n",
806 xy->sc_dev.dv_xname, xyc_e2str(error));
807 goto done;
808 }
809
810 /* check dkbad for sanity */
811 dkb = (struct dkbad *) buf;
812 for (lcv = 0; lcv < 126; lcv++) {
813 if ((dkb->bt_bad[lcv].bt_cyl == 0xffff ||
814 dkb->bt_bad[lcv].bt_cyl == 0) &&
815 dkb->bt_bad[lcv].bt_trksec == 0xffff)
816 continue; /* blank */
817 if (dkb->bt_bad[lcv].bt_cyl >= xy->ncyl)
818 break;
819 if ((dkb->bt_bad[lcv].bt_trksec >> 8) >= xy->nhead)
820 break;
821 if ((dkb->bt_bad[lcv].bt_trksec & 0xff) >= xy->nsect)
822 break;
823 }
824 if (lcv != 126) {
825 printf("%s: warning: invalid bad144 sector!\n",
826 xy->sc_dev.dv_xname);
827 } else {
828 bcopy(buf, &xy->dkb, XYFM_BPS);
829 }
830
831 done:
832 if (buf != NULL) {
833 xy_dmamem_free(xyc->dmatag, xyc->auxmap,
834 &seg, rseg, XYFM_BPS, buf);
835 }
836
837 xy->state = newstate;
838 if (!xa->booting) {
839 wakeup(&xy->state);
840 splx(s);
841 }
842 }
843
844 /*
845 * end of autoconfig functions
846 */
847
848 /*
849 * { b , c } d e v s w f u n c t i o n s
850 */
851
852 /*
853 * xyclose: close device
854 */
855 int
856 xyclose(dev, flag, fmt, l)
857 dev_t dev;
858 int flag, fmt;
859 struct lwp *l;
860
861 {
862 struct xy_softc *xy = xy_cd.cd_devs[DISKUNIT(dev)];
863 int part = DISKPART(dev);
864
865 /* clear mask bits */
866
867 switch (fmt) {
868 case S_IFCHR:
869 xy->sc_dk.dk_copenmask &= ~(1 << part);
870 break;
871 case S_IFBLK:
872 xy->sc_dk.dk_bopenmask &= ~(1 << part);
873 break;
874 }
875 xy->sc_dk.dk_openmask = xy->sc_dk.dk_copenmask | xy->sc_dk.dk_bopenmask;
876
877 return 0;
878 }
879
880 /*
881 * xydump: crash dump system
882 */
883 int
884 xydump(dev, blkno, va, size)
885 dev_t dev;
886 daddr_t blkno;
887 void *va;
888 size_t size;
889 {
890 int unit, part;
891 struct xy_softc *xy;
892
893 unit = DISKUNIT(dev);
894 if (unit >= xy_cd.cd_ndevs)
895 return ENXIO;
896 part = DISKPART(dev);
897
898 xy = xy_cd.cd_devs[unit];
899
900 printf("%s%c: crash dump not supported (yet)\n", xy->sc_dev.dv_xname,
901 'a' + part);
902
903 return ENXIO;
904
905 /* outline: globals: "dumplo" == sector number of partition to start
906 * dump at (convert to physical sector with partition table)
907 * "dumpsize" == size of dump in clicks "physmem" == size of physical
908 * memory (clicks, ctob() to get bytes) (normal case: dumpsize ==
909 * physmem)
910 *
911 * dump a copy of physical memory to the dump device starting at sector
912 * "dumplo" in the swap partition (make sure > 0). map in pages as
913 * we go. use polled I/O.
914 *
915 * XXX how to handle NON_CONTIG? */
916
917 }
918
919 /*
920 * xyioctl: ioctls on XY drives. based on ioctl's of other netbsd disks.
921 */
922 int
923 xyioctl(dev, command, addr, flag, l)
924 dev_t dev;
925 u_long command;
926 void *addr;
927 int flag;
928 struct lwp *l;
929
930 {
931 struct xy_softc *xy;
932 struct xd_iocmd *xio;
933 int error, s, unit;
934 #ifdef __HAVE_OLD_DISKLABEL
935 struct disklabel newlabel;
936 #endif
937 struct disklabel *lp;
938
939 unit = DISKUNIT(dev);
940
941 if (unit >= xy_cd.cd_ndevs || (xy = xy_cd.cd_devs[unit]) == NULL)
942 return (ENXIO);
943
944 /* switch on ioctl type */
945
946 switch (command) {
947 case DIOCSBAD: /* set bad144 info */
948 if ((flag & FWRITE) == 0)
949 return EBADF;
950 s = splbio();
951 bcopy(addr, &xy->dkb, sizeof(xy->dkb));
952 splx(s);
953 return 0;
954
955 case DIOCGDINFO: /* get disk label */
956 bcopy(xy->sc_dk.dk_label, addr, sizeof(struct disklabel));
957 return 0;
958 #ifdef __HAVE_OLD_DISKLABEL
959 case ODIOCGDINFO:
960 newlabel = *(xy->sc_dk.dk_label);
961 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
962 return ENOTTY;
963 memcpy(addr, &newlabel, sizeof (struct olddisklabel));
964 return 0;
965 #endif
966
967 case DIOCGPART: /* get partition info */
968 ((struct partinfo *) addr)->disklab = xy->sc_dk.dk_label;
969 ((struct partinfo *) addr)->part =
970 &xy->sc_dk.dk_label->d_partitions[DISKPART(dev)];
971 return 0;
972
973 case DIOCSDINFO: /* set disk label */
974 #ifdef __HAVE_OLD_DISKLABEL
975 case ODIOCSDINFO:
976 if (command == ODIOCSDINFO) {
977 memset(&newlabel, 0, sizeof newlabel);
978 memcpy(&newlabel, addr, sizeof (struct olddisklabel));
979 lp = &newlabel;
980 } else
981 #endif
982 lp = (struct disklabel *)addr;
983
984 if ((flag & FWRITE) == 0)
985 return EBADF;
986 error = setdisklabel(xy->sc_dk.dk_label,
987 lp, /* xy->sc_dk.dk_openmask : */ 0,
988 xy->sc_dk.dk_cpulabel);
989 if (error == 0) {
990 if (xy->state == XY_DRIVE_NOLABEL)
991 xy->state = XY_DRIVE_ONLINE;
992 }
993 return error;
994
995 case DIOCWLABEL: /* change write status of disk label */
996 if ((flag & FWRITE) == 0)
997 return EBADF;
998 if (*(int *) addr)
999 xy->flags |= XY_WLABEL;
1000 else
1001 xy->flags &= ~XY_WLABEL;
1002 return 0;
1003
1004 case DIOCWDINFO: /* write disk label */
1005 #ifdef __HAVE_OLD_DISKLABEL
1006 case ODIOCWDINFO:
1007 if (command == ODIOCWDINFO) {
1008 memset(&newlabel, 0, sizeof newlabel);
1009 memcpy(&newlabel, addr, sizeof (struct olddisklabel));
1010 lp = &newlabel;
1011 } else
1012 #endif
1013 lp = (struct disklabel *)addr;
1014
1015 if ((flag & FWRITE) == 0)
1016 return EBADF;
1017 error = setdisklabel(xy->sc_dk.dk_label,
1018 lp, /* xy->sc_dk.dk_openmask : */ 0,
1019 xy->sc_dk.dk_cpulabel);
1020 if (error == 0) {
1021 if (xy->state == XY_DRIVE_NOLABEL)
1022 xy->state = XY_DRIVE_ONLINE;
1023
1024 /* Simulate opening partition 0 so write succeeds. */
1025 xy->sc_dk.dk_openmask |= (1 << 0);
1026 error = writedisklabel(MAKEDISKDEV(major(dev), DISKUNIT(dev), RAW_PART),
1027 xystrategy, xy->sc_dk.dk_label,
1028 xy->sc_dk.dk_cpulabel);
1029 xy->sc_dk.dk_openmask =
1030 xy->sc_dk.dk_copenmask | xy->sc_dk.dk_bopenmask;
1031 }
1032 return error;
1033
1034 case DIOSXDCMD:
1035 xio = (struct xd_iocmd *) addr;
1036 if ((error = kauth_authorize_generic(l->l_cred,
1037 KAUTH_GENERIC_ISSUSER, NULL)) != 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 done;
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 done;
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 done;
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 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 void *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->errnum = 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->errnum = 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 (void *)(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->errnum);
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->errnum);
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->errnum;
1728
1729 #ifdef XYC_DEBUG
1730 printf("xyc_piodriver: done, retval = 0x%x (%s)\n",
1731 iorq->errnum, xyc_e2str(iorq->errnum));
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->errnum));
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->errnum = 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_resid = iorq->sectcnt * XYFM_BPS;
1837
1838 bus_dmamap_sync(xycsc->dmatag, iorq->dmamap, 0,
1839 iorq->dmamap->dm_mapsize,
1840 (iorq->buf->b_flags & B_READ)
1841 ? BUS_DMASYNC_POSTREAD
1842 : BUS_DMASYNC_POSTWRITE);
1843
1844 bus_dmamap_unload(xycsc->dmatag, iorq->dmamap);
1845
1846 (void)BUFQ_GET(iorq->xy->xyq);
1847 disk_unbusy(&xycsc->reqs[lcv].xy->sc_dk,
1848 (xycsc->reqs[lcv].buf->b_bcount -
1849 xycsc->reqs[lcv].buf->b_resid),
1850 (xycsc->reqs[lcv].buf->b_flags & B_READ));
1851 biodone(iorq->buf);
1852 iorq->mode = XY_SUB_FREE;
1853 break;
1854 case XY_SUB_WAIT:
1855 wakeup(iorq);
1856 case XY_SUB_POLL:
1857 iorq->mode =
1858 XY_NEWSTATE(iorq->mode, XY_SUB_DONE);
1859 break;
1860 }
1861
1862 } else {
1863
1864 /* resubmit, no need to do anything here */
1865 }
1866 }
1867
1868 /*
1869 * now, if stuff is waiting, start it.
1870 * since we just reset it should go
1871 */
1872 xyc_start(xycsc, NULL);
1873
1874 return (retval);
1875 }
1876
1877 /*
1878 * xyc_start: start waiting buffers
1879 */
1880
1881 void
1882 xyc_start(xycsc, iorq)
1883 struct xyc_softc *xycsc;
1884 struct xy_iorq *iorq;
1885
1886 {
1887 int lcv;
1888 struct xy_softc *xy;
1889
1890 if (iorq == NULL) {
1891 for (lcv = 0; lcv < XYC_MAXDEV ; lcv++) {
1892 if ((xy = xycsc->sc_drives[lcv]) == NULL) continue;
1893 if (BUFQ_PEEK(xy->xyq) == NULL) continue;
1894 if (xy->xyrq->mode != XY_SUB_FREE) continue;
1895 xyc_startbuf(xycsc, xy, BUFQ_PEEK(xy->xyq));
1896 }
1897 }
1898 xyc_submit_iorq(xycsc, iorq, XY_SUB_NOQ);
1899 }
1900
1901 /*
1902 * xyc_remove_iorq: remove "done" IOPB's.
1903 */
1904
1905 int
1906 xyc_remove_iorq(xycsc)
1907 struct xyc_softc *xycsc;
1908
1909 {
1910 int errnum, rq, comm, errs;
1911 struct xyc *xyc = xycsc->xyc;
1912 u_long addr;
1913 struct xy_iopb *iopb;
1914 struct xy_iorq *iorq;
1915 struct buf *bp;
1916
1917 if (xyc->xyc_csr & XYC_DERR) {
1918 /*
1919 * DOUBLE ERROR: should never happen under normal use. This
1920 * error is so bad, you can't even tell which IOPB is bad, so
1921 * we dump them all.
1922 */
1923 errnum = XY_ERR_DERR;
1924 printf("%s: DOUBLE ERROR!\n", xycsc->sc_dev.dv_xname);
1925 if (xyc_reset(xycsc, 0, XY_RSET_ALL, errnum, 0) != XY_ERR_AOK) {
1926 printf("%s: soft reset failed!\n",
1927 xycsc->sc_dev.dv_xname);
1928 panic("xyc_remove_iorq: controller DEAD");
1929 }
1930 return (XY_ERR_AOK);
1931 }
1932
1933 /*
1934 * get iopb that is done, loop down the chain
1935 */
1936
1937 if (xyc->xyc_csr & XYC_ERR) {
1938 xyc->xyc_csr = XYC_ERR; /* clear error condition */
1939 }
1940 if (xyc->xyc_csr & XYC_IPND) {
1941 xyc->xyc_csr = XYC_IPND; /* clear interrupt */
1942 }
1943
1944 for (rq = 0; rq < XYC_MAXIOPB; rq++) {
1945 iorq = xycsc->xy_chain[rq];
1946 if (iorq == NULL) break; /* done ! */
1947 if (iorq->mode == 0 || XY_STATE(iorq->mode) == XY_SUB_DONE)
1948 continue; /* free, or done */
1949 iopb = iorq->iopb;
1950 if (iopb->done == 0)
1951 continue; /* not done yet */
1952
1953 comm = iopb->com;
1954 errs = iopb->errs;
1955
1956 if (errs)
1957 iorq->errnum = iopb->errnum;
1958 else
1959 iorq->errnum = 0;
1960
1961 /* handle non-fatal errors */
1962
1963 if (errs &&
1964 xyc_error(xycsc, iorq, iopb, comm) == XY_ERR_AOK)
1965 continue; /* AOK: we resubmitted it */
1966
1967
1968 /* this iorq is now done (hasn't been restarted or anything) */
1969
1970 if ((iorq->mode & XY_MODE_VERBO) && iorq->lasterror)
1971 xyc_perror(iorq, iopb, 0);
1972
1973 /* now, if read/write check to make sure we got all the data
1974 * we needed. (this may not be the case if we got an error in
1975 * the middle of a multisector request). */
1976
1977 if ((iorq->mode & XY_MODE_B144) != 0 && errs == 0 &&
1978 (comm == XYCMD_RD || comm == XYCMD_WR)) {
1979 /* we just successfully processed a bad144 sector
1980 * note: if we are in bad 144 mode, the pointers have
1981 * been advanced already (see above) and are pointing
1982 * at the bad144 sector. to exit bad144 mode, we
1983 * must advance the pointers 1 sector and issue a new
1984 * request if there are still sectors left to process
1985 *
1986 */
1987 XYC_ADVANCE(iorq, 1); /* advance 1 sector */
1988
1989 /* exit b144 mode */
1990 iorq->mode = iorq->mode & (~XY_MODE_B144);
1991
1992 if (iorq->sectcnt) { /* more to go! */
1993 iorq->lasterror = iorq->errnum = iopb->errnum = 0;
1994 iopb->errs = iopb->done = 0;
1995 iorq->tries = 0;
1996 iopb->scnt = iorq->sectcnt;
1997 iopb->cyl = iorq->blockno /
1998 iorq->xy->sectpercyl;
1999 iopb->head =
2000 (iorq->blockno / iorq->xy->nhead) %
2001 iorq->xy->nhead;
2002 iopb->sect = iorq->blockno % XYFM_BPS;
2003 addr = (u_long) iorq->dbuf;
2004 iopb->dataa = (addr & 0xffff);
2005 iopb->datar = ((addr & 0xff0000) >> 16);
2006 /* will resubit at end */
2007 continue;
2008 }
2009 }
2010 /* final cleanup, totally done with this request */
2011
2012 switch (XY_STATE(iorq->mode)) {
2013 case XY_SUB_NORM:
2014 bp = iorq->buf;
2015 if (errs) {
2016 bp->b_error = EIO;
2017 bp->b_resid = iorq->sectcnt * XYFM_BPS;
2018 } else {
2019 bp->b_resid = 0; /* done */
2020 }
2021 bus_dmamap_sync(xycsc->dmatag, iorq->dmamap, 0,
2022 iorq->dmamap->dm_mapsize,
2023 (iorq->buf->b_flags & B_READ)
2024 ? BUS_DMASYNC_POSTREAD
2025 : BUS_DMASYNC_POSTWRITE);
2026
2027 bus_dmamap_unload(xycsc->dmatag, iorq->dmamap);
2028
2029 (void)BUFQ_GET(iorq->xy->xyq);
2030 disk_unbusy(&iorq->xy->sc_dk,
2031 (bp->b_bcount - bp->b_resid),
2032 (bp->b_flags & B_READ));
2033 iorq->mode = XY_SUB_FREE;
2034 biodone(bp);
2035 break;
2036 case XY_SUB_WAIT:
2037 iorq->mode = XY_NEWSTATE(iorq->mode, XY_SUB_DONE);
2038 wakeup(iorq);
2039 break;
2040 case XY_SUB_POLL:
2041 iorq->mode = XY_NEWSTATE(iorq->mode, XY_SUB_DONE);
2042 break;
2043 }
2044 }
2045
2046 return (XY_ERR_AOK);
2047 }
2048
2049 /*
2050 * xyc_perror: print error.
2051 * - if still_trying is true: we got an error, retried and got a
2052 * different error. in that case lasterror is the old error,
2053 * and errnum is the new one.
2054 * - if still_trying is not true, then if we ever had an error it
2055 * is in lasterror. also, if iorq->errnum == 0, then we recovered
2056 * from that error (otherwise iorq->errnum == iorq->lasterror).
2057 */
2058 void
2059 xyc_perror(iorq, iopb, still_trying)
2060 struct xy_iorq *iorq;
2061 struct xy_iopb *iopb;
2062 int still_trying;
2063
2064 {
2065
2066 int error = iorq->lasterror;
2067
2068 printf("%s", (iorq->xy) ? iorq->xy->sc_dev.dv_xname
2069 : iorq->xyc->sc_dev.dv_xname);
2070 if (iorq->buf)
2071 printf("%c: ", 'a' + DISKPART(iorq->buf->b_dev));
2072 if (iopb->com == XYCMD_RD || iopb->com == XYCMD_WR)
2073 printf("%s %d/%d/%d: ",
2074 (iopb->com == XYCMD_RD) ? "read" : "write",
2075 iopb->cyl, iopb->head, iopb->sect);
2076 printf("%s", xyc_e2str(error));
2077
2078 if (still_trying)
2079 printf(" [still trying, new error=%s]", xyc_e2str(iorq->errnum));
2080 else
2081 if (iorq->errnum == 0)
2082 printf(" [recovered in %d tries]", iorq->tries);
2083
2084 printf("\n");
2085 }
2086
2087 /*
2088 * xyc_error: non-fatal error encountered... recover.
2089 * return AOK if resubmitted, return FAIL if this iopb is done
2090 */
2091 int
2092 xyc_error(xycsc, iorq, iopb, comm)
2093 struct xyc_softc *xycsc;
2094 struct xy_iorq *iorq;
2095 struct xy_iopb *iopb;
2096 int comm;
2097
2098 {
2099 int errnum = iorq->errnum;
2100 int erract = xyc_entoact(errnum);
2101 int oldmode, advance;
2102 #ifdef __sparc__
2103 int i;
2104 #endif
2105
2106 if (erract == XY_ERA_RSET) { /* some errors require a reset */
2107 oldmode = iorq->mode;
2108 iorq->mode = XY_SUB_DONE | (~XY_SUB_MASK & oldmode);
2109 /* make xyc_start ignore us */
2110 xyc_reset(xycsc, 1, XY_RSET_NONE, errnum, iorq->xy);
2111 iorq->mode = oldmode;
2112 }
2113 /* check for read/write to a sector in bad144 table if bad: redirect
2114 * request to bad144 area */
2115
2116 if ((comm == XYCMD_RD || comm == XYCMD_WR) &&
2117 (iorq->mode & XY_MODE_B144) == 0) {
2118 advance = iorq->sectcnt - iopb->scnt;
2119 XYC_ADVANCE(iorq, advance);
2120 #ifdef __sparc__
2121 if ((i = isbad(&iorq->xy->dkb, iorq->blockno / iorq->xy->sectpercyl,
2122 (iorq->blockno / iorq->xy->nsect) % iorq->xy->nhead,
2123 iorq->blockno % iorq->xy->nsect)) != -1) {
2124 iorq->mode |= XY_MODE_B144; /* enter bad144 mode &
2125 * redirect */
2126 iopb->errnum = iopb->done = iopb->errs = 0;
2127 iopb->scnt = 1;
2128 iopb->cyl = (iorq->xy->ncyl + iorq->xy->acyl) - 2;
2129 /* second to last acyl */
2130 i = iorq->xy->sectpercyl - 1 - i; /* follow bad144
2131 * standard */
2132 iopb->head = i / iorq->xy->nhead;
2133 iopb->sect = i % iorq->xy->nhead;
2134 /* will resubmit when we come out of remove_iorq */
2135 return (XY_ERR_AOK); /* recovered! */
2136 }
2137 #endif
2138 }
2139
2140 /*
2141 * it isn't a bad144 sector, must be real error! see if we can retry
2142 * it?
2143 */
2144 if ((iorq->mode & XY_MODE_VERBO) && iorq->lasterror)
2145 xyc_perror(iorq, iopb, 1); /* inform of error state
2146 * change */
2147 iorq->lasterror = errnum;
2148
2149 if ((erract == XY_ERA_RSET || erract == XY_ERA_HARD)
2150 && iorq->tries < XYC_MAXTRIES) { /* retry? */
2151 iorq->tries++;
2152 iorq->errnum = iopb->errnum = iopb->done = iopb->errs = 0;
2153 /* will resubmit at end of remove_iorq */
2154 return (XY_ERR_AOK); /* recovered! */
2155 }
2156
2157 /* failed to recover from this error */
2158 return (XY_ERR_FAIL);
2159 }
2160
2161 /*
2162 * xyc_tick: make sure xy is still alive and ticking (err, kicking).
2163 */
2164 void
2165 xyc_tick(arg)
2166 void *arg;
2167
2168 {
2169 struct xyc_softc *xycsc = arg;
2170 int lcv, s, reset = 0;
2171
2172 /* reduce ttl for each request if one goes to zero, reset xyc */
2173 s = splbio();
2174 for (lcv = 0; lcv < XYC_MAXIOPB; lcv++) {
2175 if (xycsc->reqs[lcv].mode == 0 ||
2176 XY_STATE(xycsc->reqs[lcv].mode) == XY_SUB_DONE)
2177 continue;
2178 xycsc->reqs[lcv].ttl--;
2179 if (xycsc->reqs[lcv].ttl == 0)
2180 reset = 1;
2181 }
2182 if (reset) {
2183 printf("%s: watchdog timeout\n", xycsc->sc_dev.dv_xname);
2184 xyc_reset(xycsc, 0, XY_RSET_NONE, XY_ERR_FAIL, NULL);
2185 }
2186 splx(s);
2187
2188 /* until next time */
2189
2190 callout_reset(&xycsc->sc_tick_ch, XYC_TICKCNT, xyc_tick, xycsc);
2191 }
2192
2193 /*
2194 * xyc_ioctlcmd: this function provides a user level interface to the
2195 * controller via ioctl. this allows "format" programs to be written
2196 * in user code, and is also useful for some debugging. we return
2197 * an error code. called at user priority.
2198 *
2199 * XXX missing a few commands (see the 7053 driver for ideas)
2200 */
2201 int
2202 xyc_ioctlcmd(xy, dev, xio)
2203 struct xy_softc *xy;
2204 dev_t dev;
2205 struct xd_iocmd *xio;
2206
2207 {
2208 int s, rqno, dummy = 0;
2209 char *dvmabuf = NULL, *buf = NULL;
2210 struct xyc_softc *xycsc;
2211 int rseg, error;
2212 bus_dma_segment_t seg;
2213
2214 /* check sanity of requested command */
2215
2216 switch (xio->cmd) {
2217
2218 case XYCMD_NOP: /* no op: everything should be zero */
2219 if (xio->subfn || xio->dptr || xio->dlen ||
2220 xio->block || xio->sectcnt)
2221 return (EINVAL);
2222 break;
2223
2224 case XYCMD_RD: /* read / write sectors (up to XD_IOCMD_MAXS) */
2225 case XYCMD_WR:
2226 if (xio->subfn || xio->sectcnt > XD_IOCMD_MAXS ||
2227 xio->sectcnt * XYFM_BPS != xio->dlen || xio->dptr == NULL)
2228 return (EINVAL);
2229 break;
2230
2231 case XYCMD_SK: /* seek: doesn't seem useful to export this */
2232 return (EINVAL);
2233
2234 break;
2235
2236 default:
2237 return (EINVAL);/* ??? */
2238 }
2239
2240 xycsc = xy->parent;
2241
2242 /* create DVMA buffer for request if needed */
2243 if (xio->dlen) {
2244 bus_addr_t busbuf;
2245
2246 if ((error = xy_dmamem_alloc(xycsc->dmatag, xycsc->auxmap,
2247 &seg, &rseg,
2248 xio->dlen, (void **)&buf,
2249 &busbuf)) != 0) {
2250 return (error);
2251 }
2252 dvmabuf = (void *)(u_long)BUS_ADDR_PADDR(busbuf);
2253
2254 if (xio->cmd == XYCMD_WR) {
2255 if ((error = copyin(xio->dptr, buf, xio->dlen)) != 0) {
2256 bus_dmamem_unmap(xycsc->dmatag, buf, xio->dlen);
2257 bus_dmamem_free(xycsc->dmatag, &seg, rseg);
2258 return (error);
2259 }
2260 }
2261 }
2262 /* do it! */
2263
2264 error = 0;
2265 s = splbio();
2266 rqno = xyc_cmd(xycsc, xio->cmd, xio->subfn, xy->xy_drive, xio->block,
2267 xio->sectcnt, dvmabuf, XY_SUB_WAIT);
2268 if (rqno == XY_ERR_FAIL) {
2269 error = EIO;
2270 goto done;
2271 }
2272 xio->errnum = xycsc->ciorq->errnum;
2273 xio->tries = xycsc->ciorq->tries;
2274 XYC_DONE(xycsc, dummy);
2275
2276 if (xio->cmd == XYCMD_RD)
2277 error = copyout(buf, xio->dptr, xio->dlen);
2278
2279 done:
2280 splx(s);
2281 if (dvmabuf) {
2282 xy_dmamem_free(xycsc->dmatag, xycsc->auxmap, &seg, rseg,
2283 xio->dlen, buf);
2284 }
2285 return (error);
2286 }
2287
2288 /*
2289 * xyc_e2str: convert error code number into an error string
2290 */
2291 const char *
2292 xyc_e2str(no)
2293 int no;
2294 {
2295 switch (no) {
2296 case XY_ERR_FAIL:
2297 return ("Software fatal error");
2298 case XY_ERR_DERR:
2299 return ("DOUBLE ERROR");
2300 case XY_ERR_AOK:
2301 return ("Successful completion");
2302 case XY_ERR_IPEN:
2303 return("Interrupt pending");
2304 case XY_ERR_BCFL:
2305 return("Busy conflict");
2306 case XY_ERR_TIMO:
2307 return("Operation timeout");
2308 case XY_ERR_NHDR:
2309 return("Header not found");
2310 case XY_ERR_HARD:
2311 return("Hard ECC error");
2312 case XY_ERR_ICYL:
2313 return("Illegal cylinder address");
2314 case XY_ERR_ISEC:
2315 return("Illegal sector address");
2316 case XY_ERR_SMAL:
2317 return("Last sector too small");
2318 case XY_ERR_SACK:
2319 return("Slave ACK error (non-existent memory)");
2320 case XY_ERR_CHER:
2321 return("Cylinder and head/header error");
2322 case XY_ERR_SRTR:
2323 return("Auto-seek retry successful");
2324 case XY_ERR_WPRO:
2325 return("Write-protect error");
2326 case XY_ERR_UIMP:
2327 return("Unimplemented command");
2328 case XY_ERR_DNRY:
2329 return("Drive not ready");
2330 case XY_ERR_SZER:
2331 return("Sector count zero");
2332 case XY_ERR_DFLT:
2333 return("Drive faulted");
2334 case XY_ERR_ISSZ:
2335 return("Illegal sector size");
2336 case XY_ERR_SLTA:
2337 return("Self test A");
2338 case XY_ERR_SLTB:
2339 return("Self test B");
2340 case XY_ERR_SLTC:
2341 return("Self test C");
2342 case XY_ERR_SOFT:
2343 return("Soft ECC error");
2344 case XY_ERR_SFOK:
2345 return("Soft ECC error recovered");
2346 case XY_ERR_IHED:
2347 return("Illegal head");
2348 case XY_ERR_DSEQ:
2349 return("Disk sequencer error");
2350 case XY_ERR_SEEK:
2351 return("Seek error");
2352 default:
2353 return ("Unknown error");
2354 }
2355 }
2356
2357 int
2358 xyc_entoact(errnum)
2359
2360 int errnum;
2361
2362 {
2363 switch (errnum) {
2364 case XY_ERR_FAIL: case XY_ERR_DERR: case XY_ERR_IPEN:
2365 case XY_ERR_BCFL: case XY_ERR_ICYL: case XY_ERR_ISEC:
2366 case XY_ERR_UIMP: case XY_ERR_SZER: case XY_ERR_ISSZ:
2367 case XY_ERR_SLTA: case XY_ERR_SLTB: case XY_ERR_SLTC:
2368 case XY_ERR_IHED: case XY_ERR_SACK: case XY_ERR_SMAL:
2369
2370 return(XY_ERA_PROG); /* program error ! */
2371
2372 case XY_ERR_TIMO: case XY_ERR_NHDR: case XY_ERR_HARD:
2373 case XY_ERR_DNRY: case XY_ERR_CHER: case XY_ERR_SEEK:
2374 case XY_ERR_SOFT:
2375
2376 return(XY_ERA_HARD); /* hard error, retry */
2377
2378 case XY_ERR_DFLT: case XY_ERR_DSEQ:
2379
2380 return(XY_ERA_RSET); /* hard error reset */
2381
2382 case XY_ERR_SRTR: case XY_ERR_SFOK: case XY_ERR_AOK:
2383
2384 return(XY_ERA_SOFT); /* an FYI error */
2385
2386 case XY_ERR_WPRO:
2387
2388 return(XY_ERA_WPRO); /* write protect */
2389 }
2390
2391 return(XY_ERA_PROG); /* ??? */
2392 }
2393