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