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