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