wdc.c revision 1.3 1 /* $NetBSD: wdc.c,v 1.3 1997/08/28 11:05:01 bouyer Exp $ */
2
3 /*
4 * Copyright (c) 1994, 1995 Charles M. Hannum. All rights reserved.
5 *
6 * DMA and multi-sector PIO handling are derived from code contributed by
7 * Onno van der Linden.
8 *
9 * Atapi support added by Manuel Bouyer.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by Charles M. Hannum.
22 * 4. The name of the author may not be used to endorse or promote products
23 * derived from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36
37 #undef ATAPI_DEBUG_WDC
38 #define WDDEBUG
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/conf.h>
44 #include <sys/file.h>
45 #include <sys/stat.h>
46 #include <sys/ioctl.h>
47 #include <sys/buf.h>
48 #include <sys/uio.h>
49 #include <sys/malloc.h>
50 #include <sys/device.h>
51 #include <sys/disklabel.h>
52 #include <sys/disk.h>
53 #include <sys/syslog.h>
54 #include <sys/proc.h>
55
56 #include <vm/vm.h>
57
58 #include <machine/cpu.h>
59 #include <machine/intr.h>
60 #include <machine/bus.h>
61 #include <machine/pio.h>
62
63 #include <dev/isa/isavar.h>
64 #include <dev/isa/isadmavar.h>
65 #include <dev/isa/wdreg.h>
66 #include <dev/isa/wdlink.h>
67 #include "atapibus.h"
68 #include "wd.h"
69
70 #if NATAPIBUS > 0
71 #include <dev/scsipi/scsipi_all.h>
72 #include <dev/scsipi/atapiconf.h>
73 #endif
74
75 #define WAITTIME (10 * hz) /* time to wait for a completion */
76 /* this is a lot for hard drives, but not for cdroms */
77 #define RECOVERYTIME hz/2
78 #define WDCDELAY 100
79 #define WDCNDELAY 100000 /* delay = 100us; so 10s for a controller state change */
80 #if 0
81 /* If you enable this, it will report any delays more than 100us * N long. */
82 #define WDCNDELAY_DEBUG 50
83 #endif
84
85 #define WDIORETRIES 5 /* number of retries before giving up */
86
87 #define WDPART(dev) DISKPART(dev)
88
89 LIST_HEAD(xfer_free_list, wdc_xfer) xfer_free_list;
90
91 int wdcprobe __P((struct device *, void *, void *));
92 void wdcattach __P((struct device *, struct device *, void *));
93 int wdcintr __P((void *));
94
95 struct cfattach wdc_ca = {
96 sizeof(struct wdc_softc), wdcprobe, wdcattach
97 };
98
99 struct cfdriver wdc_cd = {
100 NULL, "wdc", DV_DULL
101 };
102
103 void wdcstart __P((struct wdc_softc *));
104 int wdcreset __P((struct wdc_softc *, int));
105 #define VERBOSE 1
106 #define SILENT 0
107 void wdcrestart __P((void *arg));
108 void wdcunwedge __P((struct wdc_softc *));
109 void wdctimeout __P((void *arg));
110 int wdccontrol __P((struct wdc_softc*, struct wd_link *));
111 void wdc_free_xfer __P((struct wdc_xfer *));
112 void wdcerror __P((struct wdc_softc*, char *));
113 void wdcbit_bucket __P(( struct wdc_softc *, int));
114 #if NWD > 0
115 int wdprint __P((void *, const char *));
116 int wdsetctlr __P((struct wd_link *));
117 int wdc_ata_intr __P((struct wdc_softc *,struct wdc_xfer *));
118 void wdc_ata_start __P((struct wdc_softc *,struct wdc_xfer *));
119 void wdc_ata_done __P((struct wdc_softc *, struct wdc_xfer *));
120 #endif /* NWD > 0 */
121 #if NATAPIBUS > 0
122 void wdc_atapi_minphys __P((struct buf *bp));
123 void wdc_atapi_start __P((struct wdc_softc *,struct wdc_xfer *));
124 int wdc_atapi_intr __P((struct wdc_softc *, struct wdc_xfer *));
125 void wdc_atapi_done __P((struct wdc_softc *, struct wdc_xfer *));
126 int wdc_atapi_send_command_packet __P((struct scsipi_xfer *sc_xfer));
127 #define MAX_SIZE MAXPHYS /* XXX */
128 #endif
129
130 #ifdef ATAPI_DEBUG
131 static int wdc_nxfer;
132 #endif
133
134 #ifdef WDDEBUG
135 #define WDDEBUG_PRINT(args) printf args
136 #else
137 #define WDDEBUG_PRINT(args)
138 #endif
139
140 #if NATAPIBUS > 0
141 static struct scsipi_adapter wdc_switch = {
142 wdc_atapi_send_command_packet,
143 wdc_atapi_minphys,
144 0,
145 0
146 };
147 #endif
148
149 int
150 wdcprobe(parent, match, aux)
151 struct device *parent;
152 void *match, *aux;
153 {
154 struct wdc_softc *wdc = match;
155 struct isa_attach_args *ia = aux;
156 int iobase;
157
158 wdc->sc_iobase = iobase = ia->ia_iobase;
159
160 /* Check if we have registers that work. */
161 outb(iobase+wd_error, 0x5a); /* Error register not writable, */
162 outb(iobase+wd_cyl_lo, 0xa5); /* but all of cyllo are. */
163 if (inb(iobase+wd_error) == 0x5a || inb(iobase+wd_cyl_lo) != 0xa5) {
164 /*
165 * Test for a controller with no IDE master, just one
166 * ATAPI device. Select drive 1, and try again.
167 */
168 outb(iobase+wd_sdh, WDSD_IBM | 0x10);
169 outb(iobase+wd_error, 0x5a);
170 outb(iobase+wd_cyl_lo, 0xa5);
171 if (inb(iobase+wd_error) == 0x5a || inb(iobase+wd_cyl_lo)
172 != 0xa5)
173 return 0;
174 wdc->sc_flags |= WDCF_ONESLAVE;
175 }
176 if (wdcreset(wdc, SILENT) != 0) {
177 /*
178 * if the reset failed,, there is no master. test for ATAPI signature
179 * on the salve device. If no ATAPI slave, wait 5s and retry a reset.
180 */
181 #if 0
182 outb(iobase+wd_sdh, WDSD_IBM | 0x10); /* slave */
183 if (inb(iobase+ wd_cyl_lo) != 0x14 ||
184 inb(iobase + wd_cyl_hi) != 0xeb) {
185 delay(500000);
186 if (wdcreset(wdc, SILENT) != 0)
187 return 0;
188 }
189 wdc->sc_flags |= WDCF_ONESLAVE;
190 #else
191 delay(500000);
192 if (wdcreset(wdc, SILENT) != 0)
193 return 0;
194 #endif
195 }
196 /* Select drive 0 or ATAPI slave device */
197 if (wdc->sc_flags & WDCF_ONESLAVE)
198 outb(iobase+wd_sdh, WDSD_IBM | 0x10);
199 else
200 outb(iobase+wd_sdh, WDSD_IBM);
201
202 /* Wait for controller to become ready. */
203 if (wait_for_unbusy(wdc) < 0)
204 return 0;
205
206 /* Start drive diagnostics. */
207 outb(iobase+wd_command, WDCC_DIAGNOSE);
208
209 /* Wait for command to complete. */
210 if (wait_for_unbusy(wdc) < 0)
211 return 0;
212
213 ia->ia_iosize = 8;
214 ia->ia_msize = 0;
215 return 1;
216 }
217
218 void
219 wdcattach(parent, self, aux)
220 struct device *parent, *self;
221 void *aux;
222 {
223 struct wdc_softc *wdc = (void *)self;
224 struct isa_attach_args *ia = aux;
225 #if NWD > 0
226 int drive;
227 #endif
228
229 TAILQ_INIT(&wdc->sc_xfer);
230 wdc->sc_drq = ia->ia_drq;
231
232 printf("\n");
233 if (wdc->sc_drq != -1) {
234 if (isa_dmamap_create(parent, wdc->sc_drq, MAXPHYS,
235 BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW)) {
236 printf("%s: can't create map for drq %d\n",
237 wdc->sc_dev.dv_xname, wdc->sc_drq);
238 wdc->sc_drq = -1;
239 }
240 }
241
242 wdc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
243 IPL_BIO, wdcintr, wdc);
244
245 #ifdef ATAPI_DEBUG
246 wdc_nxfer = 0;
247 #endif
248
249 #if NATAPIBUS > 0
250 /*
251 * Attach an ATAPI bus, if configured.
252 */
253 wdc->ab_link = malloc(sizeof(struct scsipi_link), M_DEVBUF, M_NOWAIT);
254 if (wdc->ab_link == NULL) {
255 printf("%s: can't allocate ATAPI link\n", self->dv_xname);
256 return;
257 }
258 bzero(wdc->ab_link,sizeof(struct scsipi_link));
259 wdc->ab_link->type = BUS_ATAPI;
260 wdc->ab_link->openings = 1;
261 wdc->ab_link->scsipi_atapi.type = ATAPI;
262 wdc->ab_link->scsipi_atapi.channel = 0;
263 wdc->ab_link->adapter_softc = (caddr_t)wdc;
264 wdc->ab_link->adapter = &wdc_switch;
265 (void)config_found(self, (void *)wdc->ab_link, NULL);
266 #endif /* NATAPIBUS > 0 */
267 #if NWD > 0
268 /*
269 * Attach standard IDE/ESDI/etc. disks to the controller.
270 */
271 for (drive = 0; drive < 2; drive++) {
272 /* test for ATAPI signature on this drive */
273 outb(wdc->sc_iobase+wd_sdh, WDSD_IBM | (drive << 4));
274 if (inb(wdc->sc_iobase+ wd_cyl_lo) == 0x14 &&
275 inb(wdc->sc_iobase + wd_cyl_hi) == 0xeb) {
276 continue;
277 }
278 /* controller active while autoconf */
279 wdc->sc_flags |= WDCF_ACTIVE;
280
281 if (wdccommandshort(wdc, drive, WDCC_RECAL) != 0 ||
282 wait_for_ready(wdc) != 0) {
283 wdc->d_link[drive] = NULL;
284 wdc->sc_flags &= ~WDCF_ACTIVE;
285 } else {
286 wdc->sc_flags &= ~WDCF_ACTIVE;
287 wdc->d_link[drive] = malloc(sizeof(struct wd_link),
288 M_DEVBUF, M_NOWAIT);
289 if (wdc->d_link[drive] == NULL) {
290 printf("%s: can't allocate link for drive %d\n",
291 self->dv_xname, drive);
292 continue;
293 }
294 bzero(wdc->d_link[drive],sizeof(struct wd_link));
295 wdc->d_link[drive]->type = ATA;
296 wdc->d_link[drive]->wdc_softc =(caddr_t) wdc;
297 wdc->d_link[drive]->drive = drive;
298 if (wdc->sc_drq != DRQUNK)
299 wdc->d_link[drive]->sc_mode = WDM_DMA;
300 else
301 wdc->d_link[drive]->sc_mode = 0;
302
303 (void)config_found(self, (void *)wdc->d_link[drive],
304 wdprint);
305 }
306 }
307 #endif /* NWD > 0 */
308 }
309
310 /*
311 * Start I/O on a controller. This does the calculation, and starts a read or
312 * write operation. Called to from wdstart() to start a transfer, from
313 * wdcintr() to continue a multi-sector transfer or start the next transfer, or
314 * wdcrestart() after recovering from an error.
315 */
316 void
317 wdcstart(wdc)
318 struct wdc_softc *wdc;
319 {
320 struct wdc_xfer *xfer;
321
322 if ((wdc->sc_flags & WDCF_ACTIVE) != 0 ) {
323 WDDEBUG_PRINT(("wdcstart: already active\n"));
324 return; /* controller aleady active */
325 }
326 #ifdef DIAGNOSTIC
327 if ((wdc->sc_flags & WDCF_IRQ_WAIT) != 0)
328 panic("wdcstart: controller waiting for irq\n");
329 #endif
330 /* is there a xfer ? */
331 xfer = wdc->sc_xfer.tqh_first;
332 if (xfer == NULL) {
333 #ifdef ATAPI_DEBUG2
334 printf("wdcstart: null xfer\n");
335 #endif
336 /*
337 * XXX
338 * This is a kluge. See comments in wd_get_parms().
339 */
340 if ((wdc->sc_flags & WDCF_WANTED) != 0) {
341 #ifdef ATAPI_DEBUG_WDC
342 printf("WDCF_WANTED\n");
343 #endif
344 wdc->sc_flags &= ~WDCF_WANTED;
345 wakeup(wdc);
346 }
347 return;
348 }
349 wdc->sc_flags |= WDCF_ACTIVE;
350 #ifdef ATAPI_DEBUG2
351 printf("wdcstart: drive %d\n", (int)xfer->d_link->drive);
352 #endif
353 outb(wdc->sc_iobase+wd_sdh, WDSD_IBM | xfer->d_link->drive << 4);
354 #if NATAPIBUS > 0 && NWD > 0
355 if (xfer->c_flags & C_ATAPI) {
356 #ifdef ATAPI_DEBUG_WDC
357 printf("wdcstart: atapi\n");
358 #endif
359 wdc_atapi_start(wdc,xfer);
360 } else
361 wdc_ata_start(wdc,xfer);
362 #else /* NATAPIBUS > 0 && NWD > 0 */
363 #if NATAPIBUS > 0
364 #ifdef ATAPI_DEBUG_WDC
365 printf("wdcstart: atapi\n");
366 #endif
367 wdc_atapi_start(wdc,xfer);
368 #endif /* NATAPIBUS > */
369 #if NWD > 0
370 wdc_ata_start(wdc,xfer);
371 #endif /* NWD > 0 */
372 #endif /* NATAPIBUS > 0 && NWD > 0 */
373 }
374
375 #if NWD > 0
376 int
377 wdprint(aux, wdc)
378 void *aux;
379 const char *wdc;
380 {
381 struct wd_link *d_link = aux;
382
383 if (!wdc)
384 printf(" drive %d", d_link->drive);
385 return QUIET;
386 }
387
388 void
389 wdc_ata_start(wdc, xfer)
390 struct wdc_softc *wdc;
391 struct wdc_xfer *xfer;
392 {
393 struct wd_link *d_link;
394 struct buf *bp = xfer->c_bp;
395 int nblks;
396
397 d_link=xfer->d_link;
398
399 if (wdc->sc_errors >= WDIORETRIES) {
400 wderror(d_link, bp, "wdc_ata_start hard error");
401 xfer->c_flags |= C_ERROR;
402 wdc_ata_done(wdc, xfer);
403 return;
404 }
405
406 /* Do control operations specially. */
407 if (d_link->sc_state < READY) {
408 /*
409 * Actually, we want to be careful not to mess with the control
410 * state if the device is currently busy, but we can assume
411 * that we never get to this point if that's the case.
412 */
413 if (wdccontrol(wdc, d_link) == 0) {
414 /* The drive is busy. Wait. */
415 return;
416 }
417 }
418
419 /*
420 * WDCF_ERROR is set by wdcunwedge() and wdcintr() when an error is
421 * encountered. If we are in multi-sector mode, then we switch to
422 * single-sector mode and retry the operation from the start.
423 */
424 if (wdc->sc_flags & WDCF_ERROR) {
425 wdc->sc_flags &= ~WDCF_ERROR;
426 if ((wdc->sc_flags & WDCF_SINGLE) == 0) {
427 wdc->sc_flags |= WDCF_SINGLE;
428 xfer->c_skip = 0;
429 }
430 }
431
432
433 /* When starting a transfer... */
434 if (xfer->c_skip == 0) {
435 daddr_t blkno;
436
437 WDDEBUG_PRINT(("\n%s: wdc_ata_start %s %d@%d; map ",
438 wdc->sc_dev.dv_xname,
439 (xfer->c_flags & B_READ) ? "read" : "write",
440 xfer->c_bcount, xfer->c_blkno));
441
442 blkno = xfer->c_blkno+xfer->c_p_offset;
443 xfer->c_blkno = blkno / (d_link->sc_lp->d_secsize / DEV_BSIZE);
444 } else {
445 WDDEBUG_PRINT((" %d)%x", xfer->c_skip,
446 inb(wdc->sc_iobase + wd_altsts)));
447 }
448
449 /*
450 * When starting a multi-sector transfer, or doing single-sector
451 * transfers...
452 */
453 if (xfer->c_skip == 0 || (wdc->sc_flags & WDCF_SINGLE) != 0 ||
454 d_link->sc_mode == WDM_DMA) {
455 daddr_t blkno = xfer->c_blkno;
456 long cylin, head, sector;
457 int command;
458
459 if ((wdc->sc_flags & WDCF_SINGLE) != 0)
460 nblks = 1;
461 else if (d_link->sc_mode != WDM_DMA)
462 nblks = xfer->c_bcount / d_link->sc_lp->d_secsize;
463 else
464 nblks =
465 min(xfer->c_bcount / d_link->sc_lp->d_secsize, 8);
466
467 /* Check for bad sectors and adjust transfer, if necessary. */
468 if ((d_link->sc_lp->d_flags & D_BADSECT) != 0
469 #ifdef B_FORMAT
470 && (bp->b_flags & B_FORMAT) == 0
471 #endif
472 ) {
473 long blkdiff;
474 int i;
475
476 for (i = 0;
477 (blkdiff = d_link->sc_badsect[i]) != -1; i++) {
478 blkdiff -= blkno;
479 if (blkdiff < 0)
480 continue;
481 if (blkdiff == 0) {
482 /* Replace current block of transfer. */
483 blkno =
484 d_link->sc_lp->d_secperunit -
485 d_link->sc_lp->d_nsectors - i - 1;
486 }
487 if (blkdiff < nblks) {
488 /* Bad block inside transfer. */
489 wdc->sc_flags |= WDCF_SINGLE;
490 nblks = 1;
491 }
492 break;
493 }
494 /* Tranfer is okay now. */
495 }
496
497 if ((d_link->sc_params.wdp_capabilities & WD_CAP_LBA) != 0) {
498 sector = (blkno >> 0) & 0xff;
499 cylin = (blkno >> 8) & 0xffff;
500 head = (blkno >> 24) & 0xf;
501 head |= WDSD_LBA;
502 } else {
503 sector = blkno % d_link->sc_lp->d_nsectors;
504 sector++; /* Sectors begin with 1, not 0. */
505 blkno /= d_link->sc_lp->d_nsectors;
506 head = blkno % d_link->sc_lp->d_ntracks;
507 blkno /= d_link->sc_lp->d_ntracks;
508 cylin = blkno;
509 head |= WDSD_CHS;
510 }
511
512 if (d_link->sc_mode == WDM_PIOSINGLE ||
513 (wdc->sc_flags & WDCF_SINGLE) != 0)
514 xfer->c_nblks = 1;
515 else if (d_link->sc_mode == WDM_PIOMULTI)
516 xfer->c_nblks = min(nblks, d_link->sc_multiple);
517 else
518 xfer->c_nblks = nblks;
519 xfer->c_nbytes = xfer->c_nblks * d_link->sc_lp->d_secsize;
520
521 #ifdef B_FORMAT
522 if (bp->b_flags & B_FORMAT) {
523 sector = d_link->sc_lp->d_gap3;
524 nblks = d_link->sc_lp->d_nsectors;
525 command = WDCC_FORMAT;
526 } else
527 #endif
528 switch (d_link->sc_mode) {
529 case WDM_DMA:
530 command = (xfer->c_flags & B_READ) ?
531 WDCC_READDMA : WDCC_WRITEDMA;
532 /* Start the DMA channel. */
533 isa_dmastart(wdc->sc_dev.dv_parent, wdc->sc_drq,
534 xfer->databuf + xfer->c_skip, xfer->c_nbytes,
535 NULL,
536 xfer->c_flags & B_READ ? DMAMODE_READ : DMAMODE_WRITE,
537 BUS_DMA_NOWAIT);
538 break;
539
540 case WDM_PIOMULTI:
541 command = (xfer->c_flags & B_READ) ?
542 WDCC_READMULTI : WDCC_WRITEMULTI;
543 break;
544
545 case WDM_PIOSINGLE:
546 command = (xfer->c_flags & B_READ) ?
547 WDCC_READ : WDCC_WRITE;
548 break;
549
550 default:
551 #ifdef DIAGNOSTIC
552 panic("bad wd mode");
553 #endif
554 return;
555 }
556
557 /* Initiate command! */
558 if (wdccommand(wdc, d_link, command, d_link->drive,
559 cylin, head, sector, nblks) != 0) {
560 wderror(d_link, NULL,
561 "wdc_ata_start: timeout waiting for unbusy");
562 wdcunwedge(wdc);
563 return;
564 }
565
566 WDDEBUG_PRINT(("sector %d cylin %d head %d addr %x sts %x\n",
567 sector, cylin, head, xfer->databuf,
568 inb(wdc->sc_iobase + wd_altsts)));
569
570 } else if (xfer->c_nblks > 1) {
571 /* The number of blocks in the last stretch may be smaller. */
572 nblks = xfer->c_bcount / d_link->sc_lp->d_secsize;
573 if (xfer->c_nblks > nblks) {
574 xfer->c_nblks = nblks;
575 xfer->c_nbytes = xfer->c_bcount;
576 }
577 }
578
579 /* If this was a write and not using DMA, push the data. */
580 if (d_link->sc_mode != WDM_DMA &&
581 (xfer->c_flags & (B_READ|B_WRITE)) == B_WRITE) {
582 if (wait_for_drq(wdc) < 0) {
583 wderror(d_link, NULL,
584 "wdc_ata_start: timeout waiting for drq");
585 wdcunwedge(wdc);
586 return;
587 }
588
589 /* Push out data. */
590 if ((d_link->sc_flags & WDF_32BIT) == 0)
591 outsw(wdc->sc_iobase + wd_data,
592 xfer->databuf + xfer->c_skip,
593 xfer->c_nbytes >> 1);
594 else
595 outsl(wdc->sc_iobase + wd_data,
596 xfer->databuf + xfer->c_skip,
597 xfer->c_nbytes >> 2);
598 }
599
600 wdc->sc_flags |= WDCF_IRQ_WAIT;
601 WDDEBUG_PRINT(("wdc_ata_start: timeout "));
602 timeout(wdctimeout, wdc, WAITTIME);
603 WDDEBUG_PRINT(("done\n"));
604 }
605
606 int
607 wdc_ata_intr(wdc,xfer)
608 struct wdc_softc *wdc;
609 struct wdc_xfer *xfer;
610 {
611 struct wd_link *d_link;
612
613 d_link = xfer->d_link;
614
615 if (wait_for_unbusy(wdc) < 0) {
616 wdcerror(wdc, "wdcintr: timeout waiting for unbusy");
617 wdc->sc_status |= WDCS_ERR; /* XXX */
618 }
619
620 untimeout(wdctimeout, wdc);
621
622 /* Is it not a transfer, but a control operation? */
623 if (d_link->sc_state < READY) {
624 if (wdccontrol(wdc, d_link) == 0) {
625 /* The drive is busy. Wait. */
626 return 1;
627 }
628 WDDEBUG_PRINT(("wdc_ata_start from wdc_ata_intr(open) flags 0x%x\n",
629 dc->sc_flags));
630 wdc_ata_start(wdc,xfer);
631 return 1;
632 }
633
634 /* Turn off the DMA channel. */
635 if (d_link->sc_mode == WDM_DMA)
636 isa_dmadone(wdc->sc_dev.dv_parent, wdc->sc_drq);
637
638 /* Have we an error? */
639 if (wdc->sc_status & WDCS_ERR) {
640 #ifdef WDDEBUG
641 wderror(d_link, NULL, "wdc_ata_start");
642 #endif
643 if ((wdc->sc_flags & WDCF_SINGLE) == 0) {
644 wdc->sc_flags |= WDCF_ERROR;
645 goto restart;
646 }
647
648 #ifdef B_FORMAT
649 if (bp->b_flags & B_FORMAT)
650 goto bad;
651 #endif
652
653 if (++wdc->sc_errors < WDIORETRIES) {
654 if (wdc->sc_errors == (WDIORETRIES + 1) / 2) {
655 #if 0
656 wderror(wd, NULL, "wedgie");
657 #endif
658 wdcunwedge(wdc);
659 return 1;
660 }
661 goto restart;
662 }
663 wderror(d_link, xfer->c_bp, "wdc_ata_intr hard error");
664
665 #ifdef B_FORMAT
666 bad:
667 #endif
668 xfer->c_flags |= C_ERROR;
669 goto done;
670 }
671
672 /* If this was a read and not using DMA, fetch the data. */
673 if (d_link->sc_mode != WDM_DMA &&
674 (xfer->c_flags & (B_READ|B_WRITE)) == B_READ) {
675 if ((wdc->sc_status & (WDCS_DRDY | WDCS_DSC | WDCS_DRQ))
676 != (WDCS_DRDY | WDCS_DSC | WDCS_DRQ)) {
677 wderror(d_link, NULL, "wdcintr: read intr before drq");
678 wdcunwedge(wdc);
679 return 1;
680 }
681
682 /* Pull in data. */
683 if ((d_link->sc_flags & WDF_32BIT) == 0)
684 insw(wdc->sc_iobase+wd_data,
685 xfer->databuf + xfer->c_skip, xfer->c_nbytes >> 1);
686 else
687 insl(wdc->sc_iobase+wd_data,
688 xfer->databuf + xfer->c_skip, xfer->c_nbytes >> 2);
689 }
690
691 /* If we encountered any abnormalities, flag it as a soft error. */
692 if (wdc->sc_errors > 0 ||
693 (wdc->sc_status & WDCS_CORR) != 0) {
694 wderror(d_link, xfer->c_bp, "soft error (corrected)");
695 wdc->sc_errors = 0;
696 }
697
698 /* Adjust pointers for the next block, if any. */
699 xfer->c_blkno += xfer->c_nblks;
700 xfer->c_skip += xfer->c_nbytes;
701 xfer->c_bcount -= xfer->c_nbytes;
702
703 /* See if this transfer is complete. */
704 if (xfer->c_bcount > 0)
705 goto restart;
706
707 done:
708 /* Done with this transfer, with or without error. */
709 wdc_ata_done(wdc, xfer);
710 return 0;
711
712 restart:
713 /* Start the next operation */
714 WDDEBUG_PRINT(("wdc_ata_start from wdcintr flags 0x%x\n",
715 wdc->sc_flags));
716 wdc_ata_start(wdc, xfer);
717
718 return 1;
719 }
720
721 void
722 wdc_ata_done(wdc, xfer)
723 struct wdc_softc *wdc;
724 struct wdc_xfer *xfer;
725 {
726 struct buf *bp = xfer->c_bp;
727 struct wd_link *d_link = xfer->d_link;
728 int s;
729
730 WDDEBUG_PRINT(("wdc_ata_done\n"));
731
732 /* remove this command from xfer queue */
733 s = splbio();
734 TAILQ_REMOVE(&wdc->sc_xfer, xfer, c_xferchain);
735 wdc->sc_flags &= ~(WDCF_SINGLE | WDCF_ERROR | WDCF_ACTIVE);
736 wdc->sc_errors = 0;
737 if (bp) {
738 if (xfer->c_flags & C_ERROR) {
739 bp->b_flags |= B_ERROR;
740 bp->b_error = EIO;
741 }
742 bp->b_resid = xfer->c_bcount;
743 wddone(d_link, bp);
744 biodone(bp);
745 } else {
746 wakeup(xfer->databuf);
747 }
748 xfer->c_skip = 0;
749 wdc_free_xfer(xfer);
750 d_link->openings++;
751 wdstart((void*)d_link->wd_softc);
752 WDDEBUG_PRINT(("wdcstart from wdc_ata_done, flags 0x%x\n",
753 wdc->sc_flags));
754 wdcstart(wdc);
755 splx(s);
756 }
757
758 /*
759 * Get the drive parameters, if ESDI or ATA, or create fake ones for ST506.
760 */
761 int
762 wdc_get_parms(wdc, d_link)
763 struct wdc_softc * wdc;
764 struct wd_link *d_link;
765 {
766 int i;
767 char tb[DEV_BSIZE];
768 int s, error;
769
770 /*
771 * XXX
772 * The locking done here, and the length of time this may keep the rest
773 * of the system suspended, is a kluge. This should be rewritten to
774 * set up a transfer and queue it through wdstart(), but it's called
775 * infrequently enough that this isn't a pressing matter.
776 */
777
778 s = splbio();
779
780 while ((wdc->sc_flags & WDCF_ACTIVE) != 0) {
781 wdc->sc_flags |= WDCF_WANTED;
782 if ((error = tsleep(wdc, PRIBIO | PCATCH, "wdprm", 0)) != 0) {
783 splx(s);
784 return error;
785 }
786 }
787
788 wdc->sc_flags |= WDCF_ACTIVE;
789
790 if (wdccommandshort(wdc, d_link->drive, WDCC_IDENTIFY) != 0 ||
791 wait_for_drq(wdc) != 0) {
792 /*
793 * We `know' there's a drive here; just assume it's old.
794 * This geometry is only used to read the MBR and print a
795 * (false) attach message.
796 */
797 strncpy(d_link->sc_lp->d_typename, "ST506",
798 sizeof d_link->sc_lp->d_typename);
799 d_link->sc_lp->d_type = DTYPE_ST506;
800
801 strncpy(d_link->sc_params.wdp_model, "unknown",
802 sizeof d_link->sc_params.wdp_model);
803 d_link->sc_params.wdp_config = WD_CFG_FIXED;
804 d_link->sc_params.wdp_cylinders = 1024;
805 d_link->sc_params.wdp_heads = 8;
806 d_link->sc_params.wdp_sectors = 17;
807 d_link->sc_params.wdp_maxmulti = 0;
808 d_link->sc_params.wdp_usedmovsd = 0;
809 d_link->sc_params.wdp_capabilities = 0;
810 } else {
811 strncpy(d_link->sc_lp->d_typename, "ESDI/IDE",
812 sizeof d_link->sc_lp->d_typename);
813 d_link->sc_lp->d_type = DTYPE_ESDI;
814
815 /* Read in parameter block. */
816 insw(wdc->sc_iobase + wd_data, tb, sizeof(tb) / sizeof(short));
817 bcopy(tb, &d_link->sc_params, sizeof(struct wdparams));
818
819 /* Shuffle string byte order. */
820 for (i = 0; i < sizeof(d_link->sc_params.wdp_model); i += 2) {
821 u_short *p;
822 p = (u_short *)(d_link->sc_params.wdp_model + i);
823 *p = ntohs(*p);
824 }
825 }
826
827 /* Clear any leftover interrupt. */
828 (void) inb(wdc->sc_iobase + wd_status);
829
830 /* Restart the queue. */
831 WDDEBUG_PRINT(("wdcstart from wdc_get_parms flags 0x%x\n",
832 wdc->sc_flags));
833 wdc->sc_flags &= ~WDCF_ACTIVE;
834 wdcstart(wdc);
835
836 splx(s);
837 return 0;
838 }
839
840 /*
841 * Implement operations needed before read/write.
842 * Returns 0 if operation still in progress, 1 if completed.
843 */
844 int
845 wdccontrol(wdc, d_link)
846 struct wdc_softc *wdc;
847 struct wd_link *d_link;
848 {
849 WDDEBUG_PRINT(("wdccontrol\n"));
850
851 switch (d_link->sc_state) {
852 case RECAL: /* Set SDH, step rate, do recal. */
853 if (wdccommandshort(wdc, d_link->drive, WDCC_RECAL) != 0) {
854 wderror(d_link, NULL, "wdccontrol: recal failed (1)");
855 goto bad;
856 }
857 d_link->sc_state = RECAL_WAIT;
858 break;
859
860 case RECAL_WAIT:
861 if (wdc->sc_status & WDCS_ERR) {
862 wderror(d_link, NULL, "wdccontrol: recal failed (2)");
863 goto bad;
864 }
865 /* fall through */
866
867 case GEOMETRY:
868 if ((d_link->sc_params.wdp_capabilities & WD_CAP_LBA) != 0)
869 goto multimode;
870 if (wdsetctlr(d_link) != 0) {
871 /* Already printed a message. */
872 goto bad;
873 }
874 d_link->sc_state = GEOMETRY_WAIT;
875 break;
876
877 case GEOMETRY_WAIT:
878 if (wdc->sc_status & WDCS_ERR) {
879 wderror(d_link, NULL, "wdccontrol: geometry failed");
880 goto bad;
881 }
882 /* fall through */
883
884 case MULTIMODE:
885 multimode:
886 if (d_link->sc_mode != WDM_PIOMULTI)
887 goto ready;
888 outb(wdc->sc_iobase + wd_seccnt, d_link->sc_multiple);
889 if (wdccommandshort(wdc, d_link->drive,
890 WDCC_SETMULTI) != 0) {
891 wderror(d_link, NULL,
892 "wdccontrol: setmulti failed (1)");
893 goto bad;
894 }
895 d_link->sc_state = MULTIMODE_WAIT;
896 break;
897
898 case MULTIMODE_WAIT:
899 if (wdc->sc_status & WDCS_ERR) {
900 wderror(d_link, NULL,
901 "wdccontrol: setmulti failed (2)");
902 goto bad;
903 }
904 /* fall through */
905
906 case READY:
907 ready:
908 wdc->sc_errors = 0;
909 d_link->sc_state = READY;
910 /*
911 * The rest of the initialization can be done by normal means.
912 */
913 return 1;
914
915 bad:
916 wdcunwedge(wdc);
917 return 0;
918 }
919
920 wdc->sc_flags |= WDCF_IRQ_WAIT;
921 timeout(wdctimeout, wdc, WAITTIME);
922 return 0;
923 }
924
925 #endif /* NWD > 0 */
926
927
928 /*
929 * Interrupt routine for the controller. Acknowledge the interrupt, check for
930 * errors on the current operation, mark it done if necessary, and start the
931 * next request. Also check for a partially done transfer, and continue with
932 * the next chunk if so.
933 */
934 int
935 wdcintr(arg)
936 void *arg;
937 {
938 struct wdc_softc *wdc = arg;
939 struct wdc_xfer *xfer;
940
941 if ((wdc->sc_flags & WDCF_IRQ_WAIT) == 0) {
942 /* Clear the pending interrupt and abort. */
943 u_char s = inb(wdc->sc_iobase+wd_status);
944
945 #ifdef ATAPI_DEBUG_WDC
946 u_char e = inb(wdc->sc_iobase+wd_error);
947 u_char i = inb(wdc->sc_iobase+wd_seccnt);
948 printf("wdcintr: inactive controller, "
949 "punting st=%02x er=%02x irr=%02x\n", s, e, i);
950 #else
951 inb(wdc->sc_iobase+wd_error);
952 inb(wdc->sc_iobase+wd_seccnt);
953 #endif
954
955 if (s & WDCS_DRQ) {
956 int len = inb (wdc->sc_iobase + wd_cyl_lo) +
957 256 * inb (wdc->sc_iobase + wd_cyl_hi);
958 #ifdef ATAPI_DEBUG_WDC
959 printf ("wdcintr: clearing up %d bytes\n", len);
960 #endif
961 wdcbit_bucket (wdc, len);
962 }
963 return 0;
964 }
965
966 WDDEBUG_PRINT(("wdcintr\n"));
967
968 wdc->sc_flags &= ~WDCF_IRQ_WAIT;
969 xfer = wdc->sc_xfer.tqh_first;
970 #if NATAPIBUS > 0 && NWD > 0
971 if (xfer->c_flags & C_ATAPI) {
972 (void) wdc_atapi_intr(wdc,xfer);
973 return 0;
974 } else
975 return wdc_ata_intr(wdc,xfer);
976 #else /* NATAPIBUS > 0 && NWD > 0 */
977 #if NATAPIBUS > 0
978 (void) wdc_atapi_intr(wdc,xfer);
979 return 0;
980 #endif /* NATAPIBUS > 0 */
981 #if NWD > 0
982 return wdc_ata_intr(wdc,xfer);
983 #endif /* NWD > 0 */
984 #endif /* NATAPIBUS > 0 && NWD > 0 */
985 }
986
987 int
988 wdcreset(wdc, verb)
989 struct wdc_softc *wdc;
990 int verb;
991 {
992 int iobase = wdc->sc_iobase;
993
994 /* Reset the device. */
995 outb(iobase+wd_ctlr, WDCTL_RST | WDCTL_IDS);
996 delay(1000);
997 outb(iobase+wd_ctlr, WDCTL_IDS);
998 delay(1000);
999 (void) inb(iobase+wd_error);
1000 outb(iobase+wd_ctlr, WDCTL_4BIT);
1001
1002 if (wait_for_unbusy(wdc) < 0) {
1003 if (verb)
1004 printf("%s: reset failed\n", wdc->sc_dev.dv_xname);
1005 return 1;
1006 }
1007
1008 return 0;
1009 }
1010
1011 void
1012 wdcrestart(arg)
1013 void *arg;
1014 {
1015 struct wdc_softc *wdc = arg;
1016 int s;
1017
1018 s = splbio();
1019 wdcstart(wdc);
1020 splx(s);
1021 }
1022
1023 /*
1024 * Unwedge the controller after an unexpected error. We do this by resetting
1025 * it, marking all drives for recalibration, and stalling the queue for a short
1026 * period to give the reset time to finish.
1027 * NOTE: We use a timeout here, so this routine must not be called during
1028 * autoconfig or dump.
1029 */
1030 void
1031 wdcunwedge(wdc)
1032 struct wdc_softc *wdc;
1033 {
1034 int unit;
1035
1036 #ifdef ATAPI_DEBUG
1037 printf("wdcunwedge\n");
1038 #endif
1039
1040 untimeout(wdctimeout, wdc);
1041 wdc->sc_flags &= ~WDCF_IRQ_WAIT;
1042 (void) wdcreset(wdc, VERBOSE);
1043
1044 /* Schedule recalibrate for all drives on this controller. */
1045 for (unit = 0; unit < 2; unit++) {
1046 if (!wdc->d_link[unit]) continue;
1047 if (wdc->d_link[unit]->sc_state > RECAL)
1048 wdc->d_link[unit]->sc_state = RECAL;
1049 }
1050
1051 wdc->sc_flags |= WDCF_ERROR;
1052 ++wdc->sc_errors;
1053
1054 /* Wake up in a little bit and restart the operation. */
1055 WDDEBUG_PRINT(("wdcrestart from wdcunwedge\n"));
1056 wdc->sc_flags &= ~WDCF_ACTIVE;
1057 timeout(wdcrestart, wdc, RECOVERYTIME);
1058 }
1059
1060 int
1061 wdcwait(wdc, mask)
1062 struct wdc_softc *wdc;
1063 int mask;
1064 {
1065 int iobase = wdc->sc_iobase;
1066 int timeout = 0;
1067 u_char status;
1068 #ifdef WDCNDELAY_DEBUG
1069 extern int cold;
1070 #endif
1071
1072 WDDEBUG_PRINT(("wdcwait iobase %x\n", iobase));
1073
1074 for (;;) {
1075 wdc->sc_status = status = inb(iobase+wd_status);
1076 /*
1077 * XXX
1078 * If a single slave ATAPI device is attached, it may
1079 * have released the bus. Select it and try again.
1080 */
1081 if (status == 0xff && wdc->sc_flags & WDCF_ONESLAVE) {
1082 outb(iobase+wd_sdh, WDSD_IBM | 0x10);
1083 wdc->sc_status = status = inb(iobase+wd_status);
1084 }
1085 if ((status & WDCS_BSY) == 0 && (status & mask) == mask)
1086 break;
1087 if (++timeout > WDCNDELAY) {
1088 #ifdef ATAPI_DEBUG
1089 printf("wdcwait: timeout, status %x error %x\n", status, inb(iobase+wd_error));
1090 #endif
1091 return -1;
1092 }
1093 delay(WDCDELAY);
1094 }
1095 if (status & WDCS_ERR) {
1096 wdc->sc_error = inb(iobase+wd_error);
1097 return WDCS_ERR;
1098 }
1099 #ifdef WDCNDELAY_DEBUG
1100 /* After autoconfig, there should be no long delays. */
1101 if (!cold && timeout > WDCNDELAY_DEBUG) {
1102 struct wdc_xfer *xfer = wdc->sc_xfer.tqh_first;
1103 if (xfer == NULL)
1104 printf("%s: warning: busy-wait took %dus\n",
1105 wdc->sc_dev.dv_xname, WDCDELAY * timeout);
1106 else
1107 printf("%s(%s): warning: busy-wait took %dus\n",
1108 wdc->sc_dev.dv_xname,
1109 ((struct device*)xfer->d_link->wd_softc)->dv_xname,
1110 WDCDELAY * timeout);
1111 }
1112 #endif
1113 return 0;
1114 }
1115
1116 void
1117 wdctimeout(arg)
1118 void *arg;
1119 {
1120 struct wdc_softc *wdc = (struct wdc_softc *)arg;
1121 int s;
1122
1123 WDDEBUG_PRINT(("wdctimeout\n"));
1124
1125 s = splbio();
1126 if ((wdc->sc_flags & WDCF_IRQ_WAIT) != 0) {
1127 wdc->sc_flags &= ~WDCF_IRQ_WAIT;
1128 wdcerror(wdc, "lost interrupt");
1129 wdcunwedge(wdc);
1130 } else
1131 wdcerror(wdc, "missing untimeout");
1132 splx(s);
1133 }
1134
1135 /*
1136 * Wait for the drive to become ready and send a command.
1137 * Return -1 if busy for too long or 0 otherwise.
1138 * Assumes interrupts are blocked.
1139 */
1140 int
1141 wdccommand(wdc, d_link, command, drive, cylin, head, sector, count)
1142 struct wdc_softc *wdc;
1143 struct wd_link *d_link;
1144 int command;
1145 int drive, cylin, head, sector, count;
1146 {
1147 int iobase = wdc->sc_iobase;
1148 int stat;
1149
1150 WDDEBUG_PRINT(("wdccommand drive %d\n", drive));
1151
1152 #if defined(DIAGNOSTIC) && defined(WDCDEBUG)
1153 if ((wdc->sc_flags & WDCF_ACTIVE) == 0)
1154 printf("wdccommand: controler not active (drive %d)\n", drive);
1155 #endif
1156
1157 /* Select drive, head, and addressing mode. */
1158 outb(iobase+wd_sdh, WDSD_IBM | (drive << 4) | head);
1159
1160 /* Wait for it to become ready to accept a command. */
1161 if (command == WDCC_IDP || d_link->type == ATAPI)
1162 stat = wait_for_unbusy(wdc);
1163 else
1164 stat = wdcwait(wdc, WDCS_DRDY);
1165
1166 if (stat < 0) {
1167 #ifdef ATAPI_DEBUG
1168 printf("wdcommand: xfer failed (wait_for_unbusy) status %d\n",
1169 stat);
1170 #endif
1171 return -1;
1172 }
1173
1174 /* Load parameters. */
1175 if (d_link->type == ATA && d_link->sc_lp->d_type == DTYPE_ST506)
1176 outb(iobase + wd_precomp, d_link->sc_lp->d_precompcyl / 4);
1177 else
1178 outb(iobase + wd_features, 0);
1179 outb(iobase + wd_cyl_lo, cylin);
1180 outb(iobase + wd_cyl_hi, cylin >> 8);
1181 outb(iobase + wd_sector, sector);
1182 outb(iobase + wd_seccnt, count);
1183
1184 /* Send command. */
1185 outb(iobase + wd_command, command);
1186
1187 return 0;
1188 }
1189
1190 /*
1191 * Simplified version of wdccommand().
1192 */
1193 int
1194 wdccommandshort(wdc, drive, command)
1195 struct wdc_softc *wdc;
1196 int drive;
1197 int command;
1198 {
1199 int iobase = wdc->sc_iobase;
1200
1201 WDDEBUG_PRINT(("wdccommandshort\n"));
1202
1203 #if defined(DIAGNOSTIC) && defined(WDCDEBUG)
1204 if ((wdc->sc_flags & WDCF_ACTIVE) == 0)
1205 printf("wdccommandshort: controler not active (drive %d)\n",
1206 drive);
1207 #endif
1208
1209 /* Select drive. */
1210 outb(iobase + wd_sdh, WDSD_IBM | (drive << 4));
1211
1212 if (wdcwait(wdc, WDCS_DRDY) < 0)
1213 return -1;
1214
1215 outb(iobase + wd_command, command);
1216
1217 return 0;
1218 }
1219
1220 void
1221 wdc_exec_xfer(wdc, d_link, xfer)
1222 struct wdc_softc *wdc;
1223 struct wd_link *d_link;
1224 struct wdc_xfer *xfer;
1225 {
1226 int s;
1227
1228 WDDEBUG_PRINT(("wdc_exec_xfer\n"));
1229
1230 s = splbio();
1231
1232 /* insert at the end of command list */
1233 TAILQ_INSERT_TAIL(&wdc->sc_xfer,xfer , c_xferchain)
1234 WDDEBUG_PRINT(("wdcstart from wdc_exec_xfer, flags 0x%x\n",
1235 wdc->sc_flags));
1236 wdcstart(wdc);
1237 xfer->c_flags |= C_NEEDDONE; /* we can now call upper level done() */
1238 splx(s);
1239 }
1240
1241 struct wdc_xfer *
1242 wdc_get_xfer(flags)
1243 int flags;
1244 {
1245 struct wdc_xfer *xfer;
1246 int s;
1247
1248 s = splbio();
1249 if ((xfer = xfer_free_list.lh_first) != NULL) {
1250 LIST_REMOVE(xfer, free_list);
1251 splx(s);
1252 #ifdef DIAGNOSTIC
1253 if ((xfer->c_flags & C_INUSE) != 0)
1254 panic("wdc_get_xfer: xfer already in use\n");
1255 #endif
1256 } else {
1257 splx(s);
1258 #ifdef ATAPI_DEBUG
1259 printf("wdc:making xfer %d\n",wdc_nxfer);
1260 #endif
1261 xfer = malloc(sizeof(*xfer), M_DEVBUF,
1262 ((flags & IDE_NOSLEEP) != 0 ? M_NOWAIT : M_WAITOK));
1263 if (xfer == NULL)
1264 return 0;
1265
1266 #ifdef DIAGNOSTIC
1267 xfer->c_flags &= ~C_INUSE;
1268 #endif
1269 #ifdef ATAPI_DEBUG
1270 wdc_nxfer++;
1271 #endif
1272 }
1273 #ifdef DIAGNOSTIC
1274 if ((xfer->c_flags & C_INUSE) != 0)
1275 panic("wdc_get_xfer: xfer already in use\n");
1276 #endif
1277 bzero(xfer,sizeof(struct wdc_xfer));
1278 xfer->c_flags = C_INUSE;
1279 return xfer;
1280 }
1281
1282 void
1283 wdc_free_xfer(xfer)
1284 struct wdc_xfer *xfer;
1285 {
1286 int s;
1287
1288 s = splbio();
1289 xfer->c_flags &= ~C_INUSE;
1290 LIST_INSERT_HEAD(&xfer_free_list, xfer, free_list);
1291 splx(s);
1292 }
1293
1294 void
1295 wdcerror(wdc, msg)
1296 struct wdc_softc *wdc;
1297 char *msg;
1298 {
1299 struct wdc_xfer *xfer = wdc->sc_xfer.tqh_first;
1300 if (xfer == NULL)
1301 printf("%s: %s\n", wdc->sc_dev.dv_xname, msg);
1302 else
1303 printf("%s(%d): %s\n", wdc->sc_dev.dv_xname,
1304 xfer->d_link->drive, msg);
1305 }
1306
1307 /*
1308 * the bit bucket
1309 */
1310 void
1311 wdcbit_bucket(wdc, size)
1312 struct wdc_softc *wdc;
1313 int size;
1314 {
1315 int iobase = wdc->sc_iobase;
1316 int i;
1317
1318 for (i = 0 ; i < size / 2 ; i++) {
1319 short null;
1320 (void)insw(iobase + wd_data, &null, 1);
1321 }
1322
1323 if (size % 2)
1324 (void)inb(iobase + wd_data);
1325 }
1326
1327
1328 #if NATAPIBUS > 0
1329
1330 void
1331 wdc_atapi_minphys (struct buf *bp)
1332 {
1333 if(bp->b_bcount > MAX_SIZE)
1334 bp->b_bcount = MAX_SIZE;
1335 minphys(bp);
1336 }
1337
1338
1339 void
1340 wdc_atapi_start(wdc, xfer)
1341 struct wdc_softc *wdc;
1342 struct wdc_xfer *xfer;
1343 {
1344 struct scsipi_xfer *sc_xfer = xfer->atapi_cmd;
1345
1346 #ifdef ATAPI_DEBUG_WDC
1347 printf("wdc_atapi_start, acp flags %x \n",sc_xfer->flags);
1348 #endif
1349 if (wdc->sc_errors >= WDIORETRIES) {
1350 if ((wdc->sc_status & WDCS_ERR) == 0) {
1351 sc_xfer->error = XS_DRIVER_STUFFUP; /* XXX do we know more ? */
1352 } else {
1353 sc_xfer->error = XS_SENSE;
1354 sc_xfer->sense.atapi_sense = inb (wdc->sc_iobase + wd_error);
1355 }
1356 wdc_atapi_done(wdc, xfer);
1357 return;
1358 }
1359 if (wait_for_unbusy(wdc) != 0) {
1360 if ((wdc->sc_status & WDCS_ERR) == 0) {
1361 printf("wdc_atapi_start: not ready, st = %02x\n",
1362 wdc->sc_status);
1363 sc_xfer->error = XS_SELTIMEOUT;
1364 }
1365 #if 0 /* don't get the sense yet, as this may be just UNIT ATTENTION */
1366 else {
1367 #ifdef ATAPI_DEBUG_WDC
1368 printf("wdc_atapi_start: sense %02x\n", wdc->sc_error);
1369 #endif
1370 sc_xfer->error = XS_SENSE;
1371 sc_xfer->sense.atapi_sense = wdc->sc_error;
1372 }
1373 wdc_atapi_done(wdc, xfer);
1374 return;
1375 #endif
1376 }
1377
1378 if (wdccommand(wdc, (struct wd_link*)xfer->d_link, ATAPI_PACKET_COMMAND,
1379 sc_xfer->sc_link->scsipi_atapi.drive, sc_xfer->datalen,
1380 0, 0, 0) != 0) {
1381 printf("wdc_atapi_start: can't send atapi paket command\n");
1382 sc_xfer->error = XS_DRIVER_STUFFUP;
1383 wdc->sc_flags |= WDCF_IRQ_WAIT;
1384 wdc_atapi_done(wdc, xfer);
1385 return;
1386 }
1387 if ((sc_xfer->sc_link->scsipi_atapi.cap & 0x0300) != ACAP_DRQ_INTR) {
1388 int i, phase;
1389 for (i=20000; i>0; --i) {
1390 phase = (inb(wdc->sc_iobase + wd_ireason) &
1391 (WDCI_CMD | WDCI_IN)) |
1392 (inb(wdc->sc_iobase + wd_status) & WDCS_DRQ);
1393 if (phase == PHASE_CMDOUT)
1394 break;
1395 delay(10);
1396 }
1397 if (phase != PHASE_CMDOUT ) {
1398 printf("wdc_atapi_start: timout waiting PHASE_CMDOUT");
1399 sc_xfer->error = XS_SELTIMEOUT;
1400 wdc_atapi_done(wdc, xfer);
1401 return;
1402 }
1403 outsw(wdc->sc_iobase + wd_data, sc_xfer->cmd,
1404 sc_xfer->cmdlen / sizeof(short));
1405 }
1406 wdc->sc_flags |= WDCF_IRQ_WAIT;
1407
1408 #ifdef ATAPI_DEBUG2
1409 printf("wdc_atapi_start: timeout\n");
1410 #endif
1411 timeout(wdctimeout, wdc, WAITTIME);
1412 return;
1413 }
1414
1415
1416 int
1417 wdc_atapi_get_params(ab_link, drive, id)
1418 struct scsipi_link *ab_link;
1419 u_int8_t drive;
1420 struct atapi_identify *id;
1421 {
1422 struct wdc_softc *wdc = (void*)ab_link->adapter_softc;
1423 int status, len, excess = 0;
1424 int s, error;
1425
1426 if (wdc->d_link[drive] != 0) {
1427 #ifdef ATAPI_DEBUG_PROBE
1428 printf("wdc_atapi_get_params: WD drive %d\n", drive);
1429 #endif
1430 return 0;
1431 }
1432
1433 /*
1434 * If there is only one ATAPI slave on the bus,don't probe
1435 * drive 0 (master)
1436 */
1437
1438 if (wdc->sc_flags & WDCF_ONESLAVE && drive != 1)
1439 return 0;
1440
1441 #ifdef ATAPI_DEBUG_PROBE
1442 printf("wdc_atapi_get_params: probing drive %d\n", drive);
1443 #endif
1444
1445 /*
1446 * XXX
1447 * The locking done here, and the length of time this may keep the rest
1448 * of the system suspended, is a kluge. This should be rewritten to
1449 * set up a transfer and queue it through wdstart(), but it's called
1450 * infrequently enough that this isn't a pressing matter.
1451 */
1452
1453 s = splbio();
1454
1455 while ((wdc->sc_flags & WDCF_ACTIVE) != 0) {
1456 wdc->sc_flags |= WDCF_WANTED;
1457 if ((error = tsleep(wdc, PRIBIO | PCATCH, "atprm", 0)) != 0) {
1458 splx(s);
1459 return error;
1460 }
1461 }
1462
1463 wdc->sc_flags |= WDCF_ACTIVE;
1464 error = 1;
1465 (void)wdcreset(wdc, VERBOSE);
1466 if ((status = wdccommand(wdc, (struct wd_link*)(&(ab_link->scsipi_atapi)),
1467 ATAPI_SOFT_RESET, drive, 0, 0, 0, 0)) != 0) {
1468 #ifdef ATAPI_DEBUG
1469 printf("wdc_atapi_get_params: ATAPI_SOFT_RESET"
1470 "failed for drive %d: status %d error %d\n",
1471 drive, status, wdc->sc_error);
1472 #endif
1473 error = 0;
1474 goto end;
1475 }
1476 if ((status = wait_for_unbusy(wdc)) != 0) {
1477 #ifdef ATAPI_DEBUG
1478 printf("wdc_atapi_get_params: wait_for_unbusy failed "
1479 "for drive %d: status %d error %d\n",
1480 drive, status, wdc->sc_error);
1481 #endif
1482 error = 0;
1483 goto end;
1484 }
1485
1486 if (wdccommand(wdc, (struct wd_link*)(&(ab_link->scsipi_atapi)),
1487 ATAPI_IDENTIFY_DEVICE, drive, sizeof(struct atapi_identify),
1488 0, 0, 0) != 0 ||
1489 atapi_ready(wdc) != 0) {
1490 #ifdef ATAPI_DEBUG_PROBE
1491 printf("ATAPI_IDENTIFY_DEVICE failed for drive %d\n", drive);
1492 #endif
1493 error = 0;
1494 goto end;
1495 }
1496 len = inb(wdc->sc_iobase + wd_cyl_lo) + 256 *
1497 inb(wdc->sc_iobase + wd_cyl_hi);
1498 if (len != sizeof(struct atapi_identify)) {
1499 printf("Warning drive %d returned %d/%d of "
1500 "indentify device data\n", drive, len,
1501 sizeof(struct atapi_identify));
1502 excess = len - sizeof(struct atapi_identify);
1503 if (excess < 0)
1504 excess = 0;
1505 }
1506 insw(wdc->sc_iobase + wd_data, id,
1507 sizeof(struct atapi_identify)/sizeof(short));
1508 wdcbit_bucket(wdc, excess);
1509
1510 end: /* Restart the queue. */
1511 WDDEBUG_PRINT(("wdcstart from wdc_atapi_get_parms flags 0x%x\n",
1512 wdc->sc_flags));
1513 wdc->sc_flags &= ~WDCF_ACTIVE;
1514 wdcstart(wdc);
1515 splx(s);
1516 return error;
1517 }
1518
1519 int
1520 wdc_atapi_send_command_packet(sc_xfer)
1521 struct scsipi_xfer *sc_xfer;
1522 {
1523 struct scsipi_link *sc_link = sc_xfer->sc_link;
1524 struct wdc_softc *wdc = (void*)sc_link->adapter_softc;
1525 struct wdc_xfer *xfer;
1526 int flags = sc_xfer->flags;
1527
1528 if (flags & SCSI_POLL) { /* should use the queue and wdc_atapi_start */
1529 struct wdc_xfer xfer_s;
1530 int i, s;
1531
1532 s = splbio();
1533 #ifdef ATAPI_DEBUG_WDC
1534 printf("wdc_atapi_send_cmd: "
1535 "flags 0x%x drive %d cmdlen %d datalen %d",
1536 sc_xfer->flags, sc_link->scsipi_atapi.drive, sc_xfer->cmdlen,
1537 sc_xfer->datalen);
1538 #endif
1539 xfer = &xfer_s;
1540 bzero(xfer, sizeof(xfer_s));
1541 xfer->c_flags = C_INUSE|C_ATAPI|flags;
1542 xfer->d_link = (struct wd_link *)(&sc_link->scsipi_atapi);
1543 xfer->c_bp = sc_xfer->bp;
1544 xfer->atapi_cmd = sc_xfer;
1545 xfer->c_blkno = 0;
1546 xfer->databuf = sc_xfer->data;
1547 xfer->c_bcount = sc_xfer->datalen;
1548 if (wait_for_unbusy (wdc) != 0) {
1549 if ((wdc->sc_status & WDCS_ERR) == 0) {
1550 printf("wdc_atapi_send_command: not ready, "
1551 "st = %02x\n", wdc->sc_status);
1552 sc_xfer->error = XS_SELTIMEOUT;
1553 } else {
1554 sc_xfer->error = XS_SENSE;
1555 sc_xfer->sense.atapi_sense = wdc->sc_error;
1556 }
1557 splx(s);
1558 return COMPLETE;
1559 }
1560
1561 if (wdccommand(wdc, (struct wd_link*)(&sc_link->scsipi_atapi),
1562 ATAPI_PACKET_COMMAND, sc_link->scsipi_atapi.drive, sc_xfer->datalen,
1563 0, 0, 0) != 0) {
1564 printf("can't send atapi paket command\n");
1565 sc_xfer->error = XS_DRIVER_STUFFUP;
1566 splx(s);
1567 return COMPLETE;
1568 }
1569
1570 /* Wait for cmd i/o phase. */
1571 for (i = 20000; i > 0; --i) {
1572 int phase;
1573 phase = (inb(wdc->sc_iobase + wd_ireason) &
1574 (WDCI_CMD | WDCI_IN)) |
1575 (inb(wdc->sc_iobase + wd_status) & WDCS_DRQ);
1576 if (phase == PHASE_CMDOUT)
1577 break;
1578 delay(10);
1579 }
1580 #ifdef ATAPI_DEBUG_WDC
1581 printf("Wait for cmd i/o phase: i = %d\n", i);
1582 #endif
1583
1584 outsw(wdc->sc_iobase + wd_data, sc_xfer->cmd,
1585 sc_xfer->cmdlen/ sizeof (short));
1586
1587 /* Wait for data i/o phase. */
1588 for ( i= 20000; i > 0; --i) {
1589 int phase;
1590 phase = (inb(wdc->sc_iobase + wd_ireason) &
1591 (WDCI_CMD | WDCI_IN)) |
1592 (inb(wdc->sc_iobase + wd_status) & WDCS_DRQ);
1593 if (phase != PHASE_CMDOUT)
1594 break;
1595 delay(10);
1596 }
1597
1598 #ifdef ATAPI_DEBUG_WDC
1599 printf("Wait for data i/o phase: i = %d\n", i);
1600 #endif
1601 while (wdc_atapi_intr(wdc, xfer)) {
1602 for (i = 2000; i > 0; --i)
1603 if ((inb(wdc->sc_iobase + wd_status)
1604 & WDCS_DRQ) == 0)
1605 break;
1606 #ifdef ATAPI_DEBUG_WDC
1607 printf("wdc_atapi_intr: i = %d\n", i);
1608 #endif
1609 }
1610 wdc->sc_flags &= ~(WDCF_IRQ_WAIT | WDCF_SINGLE | WDCF_ERROR);
1611 wdc->sc_errors = 0;
1612 xfer->c_skip = 0;
1613 splx(s);
1614 return COMPLETE;
1615 } else { /* POLLED */
1616 xfer = wdc_get_xfer(flags & SCSI_NOSLEEP ? IDE_NOSLEEP : 0);
1617 if (xfer == NULL) {
1618 return TRY_AGAIN_LATER;
1619 }
1620 xfer->c_flags |= C_ATAPI|sc_xfer->flags;
1621 xfer->d_link = (struct wd_link*)(&sc_link->scsipi_atapi);
1622 xfer->c_bp = sc_xfer->bp;
1623 xfer->atapi_cmd = sc_xfer;
1624 xfer->c_blkno = 0;
1625 xfer->databuf = sc_xfer->data;
1626 xfer->c_bcount = sc_xfer->datalen;
1627 wdc_exec_xfer(wdc, xfer->d_link, xfer);
1628 #ifdef ATAPI_DEBUG_WDC
1629 printf("wdc_atapi_send_command_packet: wdc_exec_xfer, flags 0x%x\n",
1630 sc_xfer->flags);
1631 #endif
1632 return (sc_xfer->flags & ITSDONE) ? COMPLETE : SUCCESSFULLY_QUEUED;
1633 }
1634 }
1635
1636 int
1637 wdc_atapi_intr(wdc, xfer)
1638 struct wdc_softc *wdc;
1639 struct wdc_xfer *xfer;
1640 {
1641 struct scsipi_xfer *sc_xfer = xfer->atapi_cmd;
1642 int len, phase, i, retries=0;
1643 int err, st, ire;
1644
1645 #ifdef ATAPI_DEBUG2
1646 printf("wdc_atapi_intr: %s\n", wdc->sc_dev.dv_xname);
1647 #endif
1648
1649 if (wait_for_unbusy(wdc) < 0) {
1650 if ((wdc->sc_status & WDCS_ERR) == 0) {
1651 printf("wdc_atapi_intr: controller busy\n");
1652 sc_xfer->error = XS_SELTIMEOUT;
1653 } else {
1654 sc_xfer->error = XS_SENSE;
1655 sc_xfer->sense.atapi_sense = wdc->sc_error;
1656 }
1657 #ifdef ATAPI_DEBUG_WDC
1658 printf("wdc_atapi_intr: wdc_atapi_done(), error %d\n",
1659 sc_xfer->error);
1660 #endif
1661 wdc_atapi_done(wdc, xfer);
1662 return 0;
1663 }
1664
1665
1666 again:
1667 len = inb(wdc->sc_iobase + wd_cyl_lo) +
1668 256 * inb(wdc->sc_iobase + wd_cyl_hi);
1669
1670 st = inb(wdc->sc_iobase + wd_status);
1671 err = inb(wdc->sc_iobase + wd_error);
1672 ire = inb(wdc->sc_iobase + wd_ireason);
1673
1674 phase = (ire & (WDCI_CMD | WDCI_IN)) | (st & WDCS_DRQ);
1675 #ifdef ATAPI_DEBUG_WDC
1676 printf("wdc_atapi_intr: len %d st %d err %d ire %d :",
1677 len, st, err, ire);
1678 #endif
1679 switch (phase) {
1680 case PHASE_CMDOUT:
1681 /* send packet command */
1682 #ifdef ATAPI_DEBUG_WDC
1683 printf("PHASE_CMDOUT\n");
1684 #endif
1685
1686 #ifdef ATAPI_DEBUG_WDC
1687 {
1688 int i;
1689 char *c = (char *)sc_xfer->cmd;
1690 printf("wdc_atapi_intr: cmd ");
1691 for (i = 0; i < sc_xfer->cmdlen; i++)
1692 printf("%x ", c[i]);
1693 printf("\n");
1694 }
1695 #endif
1696
1697 wdc->sc_flags |= WDCF_IRQ_WAIT;
1698 outsw(wdc->sc_iobase + wd_data, sc_xfer->cmd,
1699 sc_xfer->cmdlen/ sizeof (short));
1700 return 1;
1701
1702 case PHASE_DATAOUT:
1703 /* write data */
1704 #ifdef ATAPI_DEBUG_WDC
1705 printf("PHASE_DATAOUT\n");
1706 #endif
1707 if ((sc_xfer->flags & SCSI_DATA_OUT) == 0) {
1708 printf("wdc_atapi_intr: bad data phase\n");
1709 sc_xfer->error = XS_DRIVER_STUFFUP;
1710 return 1;
1711 }
1712 wdc->sc_flags |= WDCF_IRQ_WAIT;
1713 if (xfer->c_bcount < len) {
1714 printf("wdc_atapi_intr: warning: write only "
1715 "%d of %d requested bytes\n", xfer->c_bcount, len);
1716 outsw(wdc->sc_iobase + wd_data,
1717 xfer->databuf + xfer->c_skip,
1718 xfer->c_bcount / sizeof(short));
1719 for (i = xfer->c_bcount; i < len; i += sizeof(short))
1720 outw(wdc->sc_iobase + wd_data, 0);
1721 xfer->c_bcount = 0;
1722 return 1;
1723 } else {
1724 outsw(wdc->sc_iobase + wd_data,
1725 xfer->databuf + xfer->c_skip, len / sizeof(short));
1726 xfer->c_skip += len;
1727 xfer->c_bcount -= len;
1728 return 1;
1729 }
1730
1731 case PHASE_DATAIN:
1732 /* Read data */
1733 #ifdef ATAPI_DEBUG_WDC
1734 printf("PHASE_DATAIN\n");
1735 #endif
1736 if ((sc_xfer->flags & SCSI_DATA_IN) == 0) {
1737 printf("wdc_atapi_intr: bad data phase\n");
1738 sc_xfer->error = XS_DRIVER_STUFFUP;
1739 return 1;
1740 }
1741 wdc->sc_flags |= WDCF_IRQ_WAIT;
1742 if (xfer->c_bcount < len) {
1743 printf("wdc_atapi_intr: warning: reading only "
1744 "%d of %d bytes\n", xfer->c_bcount, len);
1745 insw(wdc->sc_iobase + wd_data,
1746 xfer->databuf + xfer->c_skip,
1747 xfer->c_bcount / sizeof(short));
1748 wdcbit_bucket(wdc, len - xfer->c_bcount);
1749 xfer->c_bcount = 0;
1750 return 1;
1751 } else {
1752 insw(wdc->sc_iobase + wd_data,
1753 xfer->databuf + xfer->c_skip, len / sizeof(short));
1754 xfer->c_skip += len;
1755 xfer->c_bcount -=len;
1756 return 1;
1757 }
1758
1759 case PHASE_ABORTED:
1760 case PHASE_COMPLETED:
1761 #ifdef ATAPI_DEBUG_WDC
1762 printf("PHASE_COMPLETED\n");
1763 #endif
1764 if (st & WDCS_ERR) {
1765 sc_xfer->error = XS_SENSE;
1766 sc_xfer->sense.atapi_sense = inb (wdc->sc_iobase + wd_error);
1767 }
1768 #ifdef ATAPI_DEBUG_WDC
1769 if (xfer->c_bcount != 0) {
1770 printf("wdc_atapi_intr warning: bcount value "
1771 "is %d after io\n", xfer->c_bcount);
1772 }
1773 #endif
1774 break;
1775
1776 default:
1777 if (++retries<500) {
1778 DELAY(100);
1779 goto again;
1780 }
1781 printf("wdc_atapi_intr: unknown phase %d\n", phase);
1782 if (st & WDCS_ERR) {
1783 sc_xfer->error = XS_SENSE;
1784 sc_xfer->sense.atapi_sense = inb (wdc->sc_iobase + wd_error);
1785 } else {
1786 sc_xfer->error = XS_DRIVER_STUFFUP;
1787 }
1788 }
1789
1790 #ifdef ATAPI_DEBUG_WDC
1791 printf("wdc_atapi_intr: wdc_atapi_done() (end), error %d\n",
1792 sc_xfer->error);
1793 #endif
1794 wdc_atapi_done(wdc, xfer);
1795 return (0);
1796 }
1797
1798
1799 void
1800 wdc_atapi_done(wdc, xfer)
1801 struct wdc_softc *wdc;
1802 struct wdc_xfer *xfer;
1803 {
1804 struct scsipi_xfer *sc_xfer = xfer->atapi_cmd;
1805 int s;
1806 int need_done = xfer->c_flags & C_NEEDDONE;
1807
1808 #ifdef ATAPI_DEBUG
1809 printf("wdc_atapi_done: flags 0x%x\n", (u_int)xfer->c_flags);
1810 #endif
1811 sc_xfer->resid = xfer->c_bcount;
1812
1813 /* remove this command from xfer queue */
1814 wdc->sc_errors = 0;
1815 xfer->c_skip = 0;
1816 if ((xfer->c_flags & SCSI_POLL) == 0) {
1817 s = splbio();
1818 untimeout(wdctimeout, wdc);
1819 TAILQ_REMOVE(&wdc->sc_xfer, xfer, c_xferchain);
1820 wdc->sc_flags &= ~(WDCF_SINGLE | WDCF_ERROR | WDCF_ACTIVE);
1821 wdc_free_xfer(xfer);
1822 sc_xfer->flags |= ITSDONE;
1823 if (need_done) {
1824 #ifdef ATAPI_DEBUG
1825 printf("wdc_atapi_done: scsipi_done\n");
1826 #endif
1827 scsipi_done(sc_xfer);
1828 }
1829 #ifdef WDDEBUG
1830 printf("wdcstart from wdc_atapi_intr, flags 0x%x\n",
1831 wdc->sc_flags);
1832 #endif
1833 wdcstart(wdc);
1834 splx(s);
1835 } else
1836 wdc->sc_flags &= ~(WDCF_SINGLE | WDCF_ERROR | WDCF_ACTIVE);
1837
1838 }
1839
1840 #endif /* NATAPIBUS > 0 */
1841