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