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