wdc.c revision 1.22 1 /* $NetBSD: wdc.c,v 1.22 1998/04/26 06:03:24 mycroft Exp $ */
2
3 /*
4 * Copyright (c) 1994, 1995, 1998 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
394 if (wdc->sc_cap & WDC_CAPABILITY_HWLOCK)
395 if (!(*wdc->sc_claim_hw)(wdc, 0))
396 return;
397
398 wdc->sc_flags |= WDCF_ACTIVE;
399 #ifdef ATAPI_DEBUG2
400 printf("wdcstart: drive %d\n", (int)xfer->d_link->drive);
401 #endif
402 bus_space_write_1(wdc->sc_iot, wdc->sc_ioh, wd_sdh, WDSD_IBM | xfer->d_link->drive << 4);
403 #if NATAPIBUS > 0 && NWD > 0
404 if (xfer->c_flags & C_ATAPI) {
405 #ifdef ATAPI_DEBUG_WDC
406 printf("wdcstart: atapi\n");
407 #endif
408 wdc_atapi_start(wdc,xfer);
409 } else
410 wdc_ata_start(wdc,xfer);
411 #else /* NATAPIBUS > 0 && NWD > 0 */
412 #if NATAPIBUS > 0
413 #ifdef ATAPI_DEBUG_WDC
414 printf("wdcstart: atapi\n");
415 #endif
416 wdc_atapi_start(wdc,xfer);
417 #endif /* NATAPIBUS > */
418 #if NWD > 0
419 wdc_ata_start(wdc,xfer);
420 #endif /* NWD > 0 */
421 #endif /* NATAPIBUS > 0 && NWD > 0 */
422 }
423
424 #if NWD > 0
425 int
426 wdprint(aux, wdc)
427 void *aux;
428 const char *wdc;
429 {
430 struct wd_link *d_link = aux;
431
432 if (!wdc)
433 printf(" drive %d", d_link->drive);
434 return QUIET;
435 }
436
437 void
438 wdc_ata_start(wdc, xfer)
439 struct wdc_softc *wdc;
440 struct wdc_xfer *xfer;
441 {
442 struct wd_link *d_link;
443 struct buf *bp = xfer->c_bp;
444 int nblks;
445
446 d_link=xfer->d_link;
447
448 if (wdc->sc_errors >= WDIORETRIES) {
449 wderror(d_link, bp, "wdc_ata_start hard error");
450 xfer->c_flags |= C_ERROR;
451 wdc_ata_done(wdc, xfer);
452 return;
453 }
454
455 /* Do control operations specially. */
456 if (d_link->sc_state < READY) {
457 /*
458 * Actually, we want to be careful not to mess with the control
459 * state if the device is currently busy, but we can assume
460 * that we never get to this point if that's the case.
461 */
462 if (wdccontrol(wdc, d_link) == 0) {
463 /* The drive is busy. Wait. */
464 return;
465 }
466 }
467
468 /*
469 * WDCF_ERROR is set by wdcunwedge() and wdcintr() when an error is
470 * encountered. If we are in multi-sector mode, then we switch to
471 * single-sector mode and retry the operation from the start.
472 */
473 if (wdc->sc_flags & WDCF_ERROR) {
474 wdc->sc_flags &= ~WDCF_ERROR;
475 if ((wdc->sc_flags & WDCF_SINGLE) == 0) {
476 wdc->sc_flags |= WDCF_SINGLE;
477 xfer->c_skip = 0;
478 }
479 }
480
481
482 /* When starting a transfer... */
483 if (xfer->c_skip == 0) {
484 WDDEBUG_PRINT(("\n%s: wdc_ata_start %s %d@%d; map ",
485 wdc->sc_dev.dv_xname,
486 (xfer->c_flags & B_READ) ? "read" : "write",
487 xfer->c_bcount, xfer->c_blkno));
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_flags & WDF_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 if (wdc->sc_cap & WDC_CAPABILITY_HWLOCK)
777 (*wdc->sc_free_hw)(wdc);
778
779 /* remove this command from xfer queue */
780 s = splbio();
781 TAILQ_REMOVE(&wdc->sc_xfer, xfer, c_xferchain);
782 wdc->sc_flags &= ~(WDCF_SINGLE | WDCF_ERROR | WDCF_ACTIVE);
783 wdc->sc_errors = 0;
784 if (bp) {
785 if (xfer->c_flags & C_ERROR) {
786 bp->b_flags |= B_ERROR;
787 bp->b_error = EIO;
788 }
789 bp->b_resid = xfer->c_bcount;
790 wddone(d_link, bp);
791 biodone(bp);
792 } else {
793 wakeup(xfer->databuf);
794 }
795 xfer->c_skip = 0;
796 wdc_free_xfer(xfer);
797 d_link->openings++;
798 wdstart((void*)d_link->wd_softc);
799 WDDEBUG_PRINT(("wdcstart from wdc_ata_done, flags 0x%x\n",
800 wdc->sc_flags));
801 wdcstart(wdc);
802 splx(s);
803 }
804
805 /*
806 * Get the drive parameters, if ESDI or ATA, or create fake ones for ST506.
807 */
808 int
809 wdc_get_parms(wdc, d_link)
810 struct wdc_softc * wdc;
811 struct wd_link *d_link;
812 {
813 int i;
814 char tb[DEV_BSIZE];
815 int s, error;
816
817 /*
818 * XXX
819 * The locking done here, and the length of time this may keep the rest
820 * of the system suspended, is a kluge. This should be rewritten to
821 * set up a transfer and queue it through wdstart(), but it's called
822 * infrequently enough that this isn't a pressing matter.
823 */
824
825 s = splbio();
826
827 while ((wdc->sc_flags & WDCF_ACTIVE) != 0) {
828 wdc->sc_flags |= WDCF_WANTED;
829 if ((error = tsleep(wdc, PRIBIO | PCATCH, "wdprm", 0)) != 0) {
830 splx(s);
831 return error;
832 }
833 }
834 if (wdc->sc_cap & WDC_CAPABILITY_HWLOCK)
835 if (!(*wdc->sc_claim_hw)(wdc, 1))
836 panic("wdc_get_parms: Cannot claim wd-hardware");
837
838 wdc->sc_flags |= WDCF_ACTIVE;
839
840 if (wdccommandshort(wdc, d_link->drive, WDCC_IDENTIFY) != 0 ||
841 wait_for_drq(wdc) != 0) {
842 /*
843 * We `know' there's a drive here; just assume it's old.
844 * This geometry is only used to read the MBR and print a
845 * (false) attach message.
846 */
847 strncpy(d_link->sc_lp->d_typename, "ST506",
848 sizeof d_link->sc_lp->d_typename);
849 d_link->sc_lp->d_type = DTYPE_ST506;
850
851 strncpy(d_link->sc_params.wdp_model, "unknown",
852 sizeof d_link->sc_params.wdp_model);
853 d_link->sc_params.wdp_config = WD_CFG_FIXED;
854 d_link->sc_params.wdp_cylinders = 1024;
855 d_link->sc_params.wdp_heads = 8;
856 d_link->sc_params.wdp_sectors = 17;
857 d_link->sc_params.wdp_multi = 0x0000;
858 d_link->sc_params.wdp_capabilities1 = 0x0000;
859 d_link->sc_params.wdp_capabilities2 = 0x0000;
860 d_link->sc_params.wdp_ataversion = 0x0000;
861 } else {
862 strncpy(d_link->sc_lp->d_typename, "ESDI/IDE",
863 sizeof d_link->sc_lp->d_typename);
864 d_link->sc_lp->d_type = DTYPE_ESDI;
865
866 /* Read in parameter block. */
867 bus_space_read_multi_2(wdc->sc_iot, wdc->sc_ioh, wd_data,
868 (u_int16_t *)tb, sizeof(tb) >> 1);
869 bcopy(tb, &d_link->sc_params, sizeof(struct wdparams));
870
871 /* Shuffle string byte order. */
872 for (i = 0; i < sizeof(d_link->sc_params.wdp_model); i += 2) {
873 u_short *p;
874 p = (u_short *)(d_link->sc_params.wdp_model + i);
875 *p = ntohs(*p);
876 }
877 }
878
879 /* Clear any leftover interrupt. */
880 (void) bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_status);
881
882 if (wdc->sc_cap & WDC_CAPABILITY_HWLOCK)
883 (*wdc->sc_free_hw)(wdc);
884
885 /* Restart the queue. */
886 WDDEBUG_PRINT(("wdcstart from wdc_get_parms flags 0x%x\n",
887 wdc->sc_flags));
888 wdc->sc_flags &= ~WDCF_ACTIVE;
889 wdcstart(wdc);
890
891 splx(s);
892 return 0;
893 }
894
895 /*
896 * Implement operations needed before read/write.
897 * Returns 0 if operation still in progress, 1 if completed.
898 */
899 int
900 wdccontrol(wdc, d_link)
901 struct wdc_softc *wdc;
902 struct wd_link *d_link;
903 {
904 WDDEBUG_PRINT(("wdccontrol\n"));
905
906 switch (d_link->sc_state) {
907 case RECAL: /* Set SDH, step rate, do recal. */
908 if (wdccommandshort(wdc, d_link->drive, WDCC_RECAL) != 0) {
909 wderror(d_link, NULL, "wdccontrol: recal failed (1)");
910 goto bad;
911 }
912 d_link->sc_state = RECAL_WAIT;
913 break;
914
915 case RECAL_WAIT:
916 if (wdc->sc_status & WDCS_ERR) {
917 wderror(d_link, NULL, "wdccontrol: recal failed (2)");
918 goto bad;
919 }
920 /* fall through */
921
922 case GEOMETRY:
923 if ((d_link->sc_flags & WDF_LBA) != 0)
924 goto multimode;
925 if (wdsetctlr(d_link) != 0) {
926 /* Already printed a message. */
927 goto bad;
928 }
929 d_link->sc_state = GEOMETRY_WAIT;
930 break;
931
932 case GEOMETRY_WAIT:
933 if (wdc->sc_status & WDCS_ERR) {
934 wderror(d_link, NULL, "wdccontrol: geometry failed");
935 goto bad;
936 }
937 /* fall through */
938
939 case MULTIMODE:
940 multimode:
941 if (d_link->sc_mode != WDM_PIOMULTI)
942 goto ready;
943 bus_space_write_1(wdc->sc_iot, wdc->sc_ioh, wd_seccnt, d_link->sc_multiple);
944 if (wdccommandshort(wdc, d_link->drive,
945 WDCC_SETMULTI) != 0) {
946 wderror(d_link, NULL,
947 "wdccontrol: setmulti failed (1)");
948 goto bad;
949 }
950 d_link->sc_state = MULTIMODE_WAIT;
951 break;
952
953 case MULTIMODE_WAIT:
954 if (wdc->sc_status & WDCS_ERR) {
955 wderror(d_link, NULL,
956 "wdccontrol: setmulti failed (2)");
957 goto bad;
958 }
959 /* fall through */
960
961 case READY:
962 ready:
963 wdc->sc_errors = 0;
964 d_link->sc_state = READY;
965 /*
966 * The rest of the initialization can be done by normal means.
967 */
968 return 1;
969
970 bad:
971 wdcunwedge(wdc);
972 return 0;
973 }
974
975 wdc->sc_flags |= WDCF_IRQ_WAIT;
976 timeout(wdctimeout, wdc, WAITTIME);
977 return 0;
978 }
979
980 #endif /* NWD > 0 */
981
982
983 /*
984 * Interrupt routine for the controller. Acknowledge the interrupt, check for
985 * errors on the current operation, mark it done if necessary, and start the
986 * next request. Also check for a partially done transfer, and continue with
987 * the next chunk if so.
988 */
989 int
990 wdcintr(arg)
991 void *arg;
992 {
993 struct wdc_softc *wdc = arg;
994 struct wdc_xfer *xfer;
995
996 if ((wdc->sc_flags & WDCF_IRQ_WAIT) == 0) {
997 /* Clear the pending interrupt and abort. */
998 u_char s = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_status);
999
1000 #ifdef ATAPI_DEBUG_WDC
1001 u_char e = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_error);
1002 u_char i = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_seccnt);
1003 printf("wdcintr: inactive controller, "
1004 "punting st=%02x er=%02x irr=%02x\n", s, e, i);
1005 #else
1006 bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_error);
1007 bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_seccnt);
1008 #endif
1009
1010 if (s & WDCS_DRQ) {
1011 int len = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_cyl_lo) +
1012 256 * bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_cyl_hi);
1013 #ifdef ATAPI_DEBUG_WDC
1014 printf ("wdcintr: clearing up %d bytes\n", len);
1015 #endif
1016 wdcbit_bucket (wdc, len);
1017 }
1018 return 0;
1019 }
1020
1021 WDDEBUG_PRINT(("wdcintr\n"));
1022
1023 xfer = wdc->sc_xfer.tqh_first;
1024 #if NATAPIBUS > 0 && NWD > 0
1025 if (xfer->c_flags & C_ATAPI) {
1026 return wdc_atapi_intr(wdc,xfer);
1027 } else
1028 return wdc_ata_intr(wdc,xfer);
1029 #else /* NATAPIBUS > 0 && NWD > 0 */
1030 #if NATAPIBUS > 0
1031 return wdc_atapi_intr(wdc,xfer);
1032 #endif /* NATAPIBUS > 0 */
1033 #if NWD > 0
1034 return wdc_ata_intr(wdc,xfer);
1035 #endif /* NWD > 0 */
1036 #endif /* NATAPIBUS > 0 && NWD > 0 */
1037 }
1038
1039 int
1040 wdcreset(wdc, verb)
1041 struct wdc_softc *wdc;
1042 int verb;
1043 {
1044
1045 /* Reset the device. */
1046 bus_space_write_1(wdc->sc_auxiot, wdc->sc_auxioh, wd_aux_ctlr,
1047 WDCTL_RST | WDCTL_IDS);
1048 delay(1000);
1049 bus_space_write_1(wdc->sc_auxiot, wdc->sc_auxioh, wd_aux_ctlr,
1050 WDCTL_IDS);
1051 delay(1000);
1052 (void) bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_error);
1053 bus_space_write_1(wdc->sc_auxiot, wdc->sc_auxioh, wd_aux_ctlr,
1054 WDCTL_4BIT);
1055
1056 if (wait_for_unbusy(wdc) < 0) {
1057 if (verb)
1058 printf("%s: reset failed\n", wdc->sc_dev.dv_xname);
1059 return 1;
1060 }
1061
1062 return 0;
1063 }
1064
1065 void
1066 wdcrestart(arg)
1067 void *arg;
1068 {
1069 struct wdc_softc *wdc = arg;
1070 int s;
1071
1072 s = splbio();
1073 wdcstart(wdc);
1074 splx(s);
1075 }
1076
1077 /*
1078 * Unwedge the controller after an unexpected error. We do this by resetting
1079 * it, marking all drives for recalibration, and stalling the queue for a short
1080 * period to give the reset time to finish.
1081 * NOTE: We use a timeout here, so this routine must not be called during
1082 * autoconfig or dump.
1083 */
1084 void
1085 wdcunwedge(wdc)
1086 struct wdc_softc *wdc;
1087 {
1088 int unit;
1089
1090 #ifdef ATAPI_DEBUG
1091 printf("wdcunwedge\n");
1092 #endif
1093
1094 untimeout(wdctimeout, wdc);
1095 wdc->sc_flags &= ~WDCF_IRQ_WAIT;
1096 (void) wdcreset(wdc, VERBOSE);
1097
1098 /* Schedule recalibrate for all drives on this controller. */
1099 for (unit = 0; unit < 2; unit++) {
1100 if (!wdc->d_link[unit])
1101 wdccommandshort(wdc, unit, ATAPI_SOFT_RESET);
1102 else if (wdc->d_link[unit]->sc_state > RECAL)
1103 wdc->d_link[unit]->sc_state = RECAL;
1104 }
1105
1106 wdc->sc_flags |= WDCF_ERROR;
1107 ++wdc->sc_errors;
1108
1109 /* Wake up in a little bit and restart the operation. */
1110 WDDEBUG_PRINT(("wdcrestart from wdcunwedge\n"));
1111 wdc->sc_flags &= ~WDCF_ACTIVE;
1112 timeout(wdcrestart, wdc, RECOVERYTIME);
1113 }
1114
1115 int
1116 wdcwait(wdc, mask)
1117 struct wdc_softc *wdc;
1118 int mask;
1119 {
1120 int timeout = 0;
1121 u_char status;
1122 #ifdef WDCNDELAY_DEBUG
1123 extern int cold;
1124 #endif
1125
1126 WDDEBUG_PRINT(("wdcwait\n"));
1127
1128 for (;;) {
1129 wdc->sc_status = status = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_status);
1130 /*
1131 * XXX
1132 * If a single slave ATAPI device is attached, it may
1133 * have released the bus. Select it and try again.
1134 */
1135 if (status == 0xff && wdc->sc_flags & WDCF_ONESLAVE) {
1136 bus_space_write_1(wdc->sc_iot, wdc->sc_ioh, wd_sdh, WDSD_IBM | 0x10);
1137 wdc->sc_status = status = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_status);
1138 }
1139 if ((status & WDCS_BSY) == 0 && (status & mask) == mask)
1140 break;
1141 if (++timeout > WDCNDELAY) {
1142 #ifdef ATAPI_DEBUG
1143 printf("wdcwait: timeout, status %x error %x\n", status, bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_error));
1144 #endif
1145 return -1;
1146 }
1147 delay(WDCDELAY);
1148 }
1149 if (status & WDCS_ERR) {
1150 wdc->sc_error = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_error);
1151 return WDCS_ERR;
1152 }
1153 #ifdef WDCNDELAY_DEBUG
1154 /* After autoconfig, there should be no long delays. */
1155 if (!cold && timeout > WDCNDELAY_DEBUG) {
1156 struct wdc_xfer *xfer = wdc->sc_xfer.tqh_first;
1157 if (xfer == NULL)
1158 printf("%s: warning: busy-wait took %dus\n",
1159 wdc->sc_dev.dv_xname, WDCDELAY * timeout);
1160 else
1161 printf("%s(%s): warning: busy-wait took %dus\n",
1162 wdc->sc_dev.dv_xname,
1163 ((struct device*)xfer->d_link->wd_softc)->dv_xname,
1164 WDCDELAY * timeout);
1165 }
1166 #endif
1167 return 0;
1168 }
1169
1170 void
1171 wdctimeout(arg)
1172 void *arg;
1173 {
1174 struct wdc_softc *wdc = (struct wdc_softc *)arg;
1175 struct wdc_xfer *xfer = wdc->sc_xfer.tqh_first;
1176 int s;
1177
1178 WDDEBUG_PRINT(("wdctimeout\n"));
1179
1180 s = splbio();
1181 if ((wdc->sc_flags & WDCF_IRQ_WAIT) != 0) {
1182 wdcerror(wdc, "lost interrupt");
1183 printf("\ttype: %s\n", (xfer->c_flags & C_ATAPI) ? "atapi":"ata");
1184 printf("\tc_bcount: %d\n", xfer->c_bcount);
1185 printf("\tc_skip: %d\n", xfer->c_skip);
1186 wdcintr(wdc);
1187 wdc->sc_flags &= ~WDCF_IRQ_WAIT;
1188 wdcunwedge(wdc);
1189 } else
1190 wdcerror(wdc, "missing untimeout");
1191 splx(s);
1192 }
1193
1194 /*
1195 * Wait for the drive to become ready and send a command.
1196 * Return -1 if busy for too long or 0 otherwise.
1197 * Assumes interrupts are blocked.
1198 */
1199 int
1200 wdccommand(wdc, d_link, command, drive, cylin, head, sector, count)
1201 struct wdc_softc *wdc;
1202 struct wd_link *d_link;
1203 int command;
1204 int drive, cylin, head, sector, count;
1205 {
1206 int stat;
1207
1208 WDDEBUG_PRINT(("wdccommand drive %d\n", drive));
1209
1210 #if defined(DIAGNOSTIC) && defined(WDCDEBUG)
1211 if ((wdc->sc_flags & WDCF_ACTIVE) == 0)
1212 printf("wdccommand: controler not active (drive %d)\n", drive);
1213 #endif
1214
1215 /* Select drive, head, and addressing mode. */
1216 bus_space_write_1(wdc->sc_iot, wdc->sc_ioh, wd_sdh, WDSD_IBM | (drive << 4) | head);
1217
1218 /* Wait for it to become ready to accept a command. */
1219 if (command == WDCC_IDP || d_link->type == ATAPI)
1220 stat = wait_for_unbusy(wdc);
1221 else
1222 stat = wdcwait(wdc, WDCS_DRDY);
1223
1224 if (stat < 0) {
1225 #ifdef ATAPI_DEBUG
1226 printf("wdcommand: xfer failed (wait_for_unbusy) status %d\n",
1227 stat);
1228 #endif
1229 return -1;
1230 }
1231
1232 /* Load parameters. */
1233 if (d_link->type == ATA && d_link->sc_lp->d_type == DTYPE_ST506)
1234 bus_space_write_1(wdc->sc_iot, wdc->sc_ioh, wd_precomp, d_link->sc_lp->d_precompcyl / 4);
1235 else
1236 bus_space_write_1(wdc->sc_iot, wdc->sc_ioh, wd_features, 0);
1237 bus_space_write_1(wdc->sc_iot, wdc->sc_ioh, wd_cyl_lo, cylin);
1238 bus_space_write_1(wdc->sc_iot, wdc->sc_ioh, wd_cyl_hi, cylin >> 8);
1239 bus_space_write_1(wdc->sc_iot, wdc->sc_ioh, wd_sector, sector);
1240 bus_space_write_1(wdc->sc_iot, wdc->sc_ioh, wd_seccnt, count);
1241
1242 /* Send command. */
1243 bus_space_write_1(wdc->sc_iot, wdc->sc_ioh, wd_command, command);
1244
1245 return 0;
1246 }
1247
1248 /*
1249 * Simplified version of wdccommand().
1250 */
1251 int
1252 wdccommandshort(wdc, drive, command)
1253 struct wdc_softc *wdc;
1254 int drive;
1255 int command;
1256 {
1257
1258 WDDEBUG_PRINT(("wdccommandshort\n"));
1259
1260 #if defined(DIAGNOSTIC) && defined(WDCDEBUG)
1261 if ((wdc->sc_flags & WDCF_ACTIVE) == 0)
1262 printf("wdccommandshort: controller not active (drive %d)\n",
1263 drive);
1264 #endif
1265
1266 /* Select drive. */
1267 bus_space_write_1(wdc->sc_iot, wdc->sc_ioh, wd_sdh, WDSD_IBM | (drive << 4));
1268
1269 if (wdcwait(wdc, WDCS_DRDY) < 0)
1270 return -1;
1271
1272 bus_space_write_1(wdc->sc_iot, wdc->sc_ioh, wd_command, command);
1273
1274 return 0;
1275 }
1276
1277 void
1278 wdc_exec_xfer(wdc, d_link, xfer)
1279 struct wdc_softc *wdc;
1280 struct wd_link *d_link;
1281 struct wdc_xfer *xfer;
1282 {
1283 int s;
1284
1285 WDDEBUG_PRINT(("wdc_exec_xfer\n"));
1286
1287 s = splbio();
1288
1289 /* insert at the end of command list */
1290 TAILQ_INSERT_TAIL(&wdc->sc_xfer,xfer , c_xferchain);
1291 WDDEBUG_PRINT(("wdcstart from wdc_exec_xfer, flags 0x%x\n",
1292 wdc->sc_flags));
1293 wdcstart(wdc);
1294 xfer->c_flags |= C_NEEDDONE; /* we can now call upper level done() */
1295 splx(s);
1296 }
1297
1298 struct wdc_xfer *
1299 wdc_get_xfer(flags)
1300 int flags;
1301 {
1302 struct wdc_xfer *xfer;
1303 int s;
1304
1305 s = splbio();
1306 if ((xfer = xfer_free_list.lh_first) != NULL) {
1307 LIST_REMOVE(xfer, free_list);
1308 splx(s);
1309 #ifdef DIAGNOSTIC
1310 if ((xfer->c_flags & C_INUSE) != 0)
1311 panic("wdc_get_xfer: xfer already in use\n");
1312 #endif
1313 } else {
1314 splx(s);
1315 #ifdef ATAPI_DEBUG2
1316 printf("wdc:making xfer %d\n",wdc_nxfer);
1317 #endif
1318 xfer = malloc(sizeof(*xfer), M_DEVBUF,
1319 ((flags & IDE_NOSLEEP) != 0 ? M_NOWAIT : M_WAITOK));
1320 if (xfer == NULL)
1321 return 0;
1322
1323 #ifdef DIAGNOSTIC
1324 xfer->c_flags &= ~C_INUSE;
1325 #endif
1326 #ifdef ATAPI_DEBUG2
1327 wdc_nxfer++;
1328 #endif
1329 }
1330 #ifdef DIAGNOSTIC
1331 if ((xfer->c_flags & C_INUSE) != 0)
1332 panic("wdc_get_xfer: xfer already in use\n");
1333 #endif
1334 bzero(xfer,sizeof(struct wdc_xfer));
1335 xfer->c_flags = C_INUSE;
1336 return xfer;
1337 }
1338
1339 void
1340 wdc_free_xfer(xfer)
1341 struct wdc_xfer *xfer;
1342 {
1343 int s;
1344
1345 s = splbio();
1346 xfer->c_flags &= ~C_INUSE;
1347 LIST_INSERT_HEAD(&xfer_free_list, xfer, free_list);
1348 splx(s);
1349 }
1350
1351 void
1352 wdcerror(wdc, msg)
1353 struct wdc_softc *wdc;
1354 char *msg;
1355 {
1356 struct wdc_xfer *xfer = wdc->sc_xfer.tqh_first;
1357 if (xfer == NULL)
1358 printf("%s: %s\n", wdc->sc_dev.dv_xname, msg);
1359 else
1360 printf("%s(%d): %s\n", wdc->sc_dev.dv_xname,
1361 xfer->d_link->drive, msg);
1362 }
1363
1364 /*
1365 * the bit bucket
1366 */
1367 void
1368 wdcbit_bucket(wdc, size)
1369 struct wdc_softc *wdc;
1370 int size;
1371 {
1372
1373 for (; size >= 2; size -= 2)
1374 (void)bus_space_read_2(wdc->sc_iot, wdc->sc_ioh, wd_data);
1375 if (size)
1376 (void)bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_data);
1377 }
1378
1379
1380 #if NATAPIBUS > 0
1381
1382 void
1383 wdc_atapi_minphys (struct buf *bp)
1384 {
1385 if(bp->b_bcount > MAX_SIZE)
1386 bp->b_bcount = MAX_SIZE;
1387 minphys(bp);
1388 }
1389
1390
1391 void
1392 wdc_atapi_start(wdc, xfer)
1393 struct wdc_softc *wdc;
1394 struct wdc_xfer *xfer;
1395 {
1396 struct scsipi_xfer *sc_xfer = xfer->atapi_cmd;
1397
1398 #ifdef ATAPI_DEBUG_WDC
1399 printf("wdc_atapi_start, acp flags %x \n",sc_xfer->flags);
1400 #endif
1401 if (wdc->sc_errors >= WDIORETRIES) {
1402 if ((wdc->sc_status & WDCS_ERR) == 0) {
1403 sc_xfer->error = XS_DRIVER_STUFFUP; /* XXX do we know more ? */
1404 } else {
1405 sc_xfer->error = XS_SENSE;
1406 sc_xfer->sense.atapi_sense = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_error);
1407 }
1408 wdc_atapi_done(wdc, xfer);
1409 return;
1410 }
1411 if (wait_for_unbusy(wdc) != 0) {
1412 if ((wdc->sc_status & WDCS_ERR) == 0) {
1413 printf("wdc_atapi_start: not ready, st = %02x\n",
1414 wdc->sc_status);
1415 sc_xfer->error = XS_SELTIMEOUT;
1416 }
1417 #if 0 /* don't get the sense yet, as this may be just UNIT ATTENTION */
1418 else {
1419 #ifdef ATAPI_DEBUG_WDC
1420 printf("wdc_atapi_start: sense %02x\n", wdc->sc_error);
1421 #endif
1422 sc_xfer->error = XS_SENSE;
1423 sc_xfer->sense.atapi_sense = wdc->sc_error;
1424 }
1425 wdc_atapi_done(wdc, xfer);
1426 return;
1427 #endif
1428 }
1429
1430 /*
1431 * Limit length to what can be stuffed into the cylinder register
1432 * (16 bits). Some CD-ROMs seem to interpret '0' as 65536,
1433 * but not all devices do that and it's not obvious from the
1434 * ATAPI spec that that behaviour should be expected. If more
1435 * data is necessary, multiple data transfer phases will be done.
1436 */
1437 if (wdccommand(wdc, (struct wd_link*)xfer->d_link, ATAPI_PACKET_COMMAND,
1438 sc_xfer->sc_link->scsipi_atapi.drive,
1439 sc_xfer->datalen <= 0xffff ? sc_xfer->datalen : 0xffff,
1440 0, 0, 0) != 0) {
1441 printf("wdc_atapi_start: can't send atapi packet command\n");
1442 sc_xfer->error = XS_DRIVER_STUFFUP;
1443 wdc_atapi_done(wdc, xfer);
1444 return;
1445 }
1446 if ((sc_xfer->sc_link->scsipi_atapi.cap & 0x0300) != ACAP_DRQ_INTR) {
1447 int i, phase;
1448 for (i=20000; i>0; --i) {
1449 phase = (bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_ireason) &
1450 (WDCI_CMD | WDCI_IN)) |
1451 (bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_status) & WDCS_DRQ);
1452 if (phase == PHASE_CMDOUT)
1453 break;
1454 delay(10);
1455 }
1456 if (phase != PHASE_CMDOUT ) {
1457 printf("wdc_atapi_start: timeout waiting PHASE_CMDOUT");
1458 sc_xfer->error = XS_SELTIMEOUT;
1459 wdc_atapi_done(wdc, xfer);
1460 return;
1461 }
1462 bus_space_write_multi_2(wdc->sc_iot, wdc->sc_ioh, wd_data,
1463 (u_int16_t *)sc_xfer->cmd, sc_xfer->cmdlen >> 1);
1464 }
1465 wdc->sc_flags |= WDCF_IRQ_WAIT;
1466
1467 #ifdef ATAPI_DEBUG2
1468 printf("wdc_atapi_start: timeout\n");
1469 #endif
1470 timeout(wdctimeout, wdc, WAITTIME);
1471 return;
1472 }
1473
1474
1475 int
1476 wdc_atapi_get_params(ab_link, drive, id)
1477 struct scsipi_link *ab_link;
1478 u_int8_t drive;
1479 struct atapi_identify *id;
1480 {
1481 struct wdc_softc *wdc = (void*)ab_link->adapter_softc;
1482 int status, len, excess = 0;
1483 int s, error;
1484
1485 /* if a disk is already present, skip */
1486 if ((wdc->sc_drives_mask & (1 << drive)) != 0) {
1487 #ifdef ATAPI_DEBUG_PROBE
1488 printf("wdc_atapi_get_params: drive %d present\n", drive);
1489 #endif
1490 return 0;
1491 }
1492
1493 /*
1494 * If there is only one ATAPI slave on the bus,don't probe
1495 * drive 0 (master)
1496 */
1497
1498 if (wdc->sc_flags & WDCF_ONESLAVE && drive != 1)
1499 return 0;
1500
1501 #ifdef ATAPI_DEBUG_PROBE
1502 printf("wdc_atapi_get_params: probing drive %d\n", drive);
1503 #endif
1504
1505 /*
1506 * XXX
1507 * The locking done here, and the length of time this may keep the rest
1508 * of the system suspended, is a kluge. This should be rewritten to
1509 * set up a transfer and queue it through wdstart(), but it's called
1510 * infrequently enough that this isn't a pressing matter.
1511 */
1512
1513 s = splbio();
1514
1515 while ((wdc->sc_flags & WDCF_ACTIVE) != 0) {
1516 wdc->sc_flags |= WDCF_WANTED;
1517 if ((error = tsleep(wdc, PRIBIO | PCATCH, "atprm", 0)) != 0) {
1518 splx(s);
1519 return error;
1520 }
1521 }
1522
1523 wdc->sc_flags |= WDCF_ACTIVE;
1524 error = 1;
1525 (void)wdcreset(wdc, VERBOSE);
1526 if ((status = wdccommand(wdc, (struct wd_link*)(&(ab_link->scsipi_atapi)),
1527 ATAPI_SOFT_RESET, drive, 0, 0, 0, 0)) != 0) {
1528 #ifdef ATAPI_DEBUG
1529 printf("wdc_atapi_get_params: ATAPI_SOFT_RESET"
1530 "failed for drive %d: status %d error %d\n",
1531 drive, status, wdc->sc_error);
1532 #endif
1533 error = 0;
1534 goto end;
1535 }
1536 if ((status = wait_for_unbusy(wdc)) != 0) {
1537 #ifdef ATAPI_DEBUG
1538 printf("wdc_atapi_get_params: wait_for_unbusy failed "
1539 "for drive %d: status %d error %d\n",
1540 drive, status, wdc->sc_error);
1541 #endif
1542 error = 0;
1543 goto end;
1544 }
1545
1546 /* Some ATAPI devices seem a bit more time after software reset. */
1547 delay(5000);
1548
1549 if (wdccommand(wdc, (struct wd_link*)(&(ab_link->scsipi_atapi)),
1550 ATAPI_IDENTIFY_DEVICE, drive, sizeof(struct atapi_identify),
1551 0, 0, 0) != 0 ||
1552 atapi_ready(wdc) != 0) {
1553 #ifdef ATAPI_DEBUG_PROBE
1554 printf("ATAPI_IDENTIFY_DEVICE failed for drive %d\n", drive);
1555 #endif
1556 error = 0;
1557 goto end;
1558 }
1559 len = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_cyl_lo) + 256 *
1560 bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_cyl_hi);
1561 if (len != sizeof(struct atapi_identify)) {
1562 printf("Warning drive %d returned %d/%d of "
1563 "indentify device data\n", drive, len,
1564 sizeof(struct atapi_identify));
1565 excess = len - sizeof(struct atapi_identify);
1566 if (excess < 0)
1567 excess = 0;
1568 }
1569 bus_space_read_multi_2(wdc->sc_iot, wdc->sc_ioh, wd_data,
1570 (u_int16_t *)id, sizeof(struct atapi_identify) >> 1);
1571 wdcbit_bucket(wdc, excess);
1572 wdc->sc_drives_mask |= (1 << drive);
1573
1574 end: /* Restart the queue. */
1575 WDDEBUG_PRINT(("wdcstart from wdc_atapi_get_parms flags 0x%x\n",
1576 wdc->sc_flags));
1577 wdc->sc_flags &= ~WDCF_ACTIVE;
1578 wdcstart(wdc);
1579 splx(s);
1580 return error;
1581 }
1582
1583 int
1584 wdc_atapi_send_command_packet(sc_xfer)
1585 struct scsipi_xfer *sc_xfer;
1586 {
1587 struct scsipi_link *sc_link = sc_xfer->sc_link;
1588 struct wdc_softc *wdc = (void*)sc_link->adapter_softc;
1589 struct wdc_xfer *xfer;
1590 int flags = sc_xfer->flags;
1591
1592 if (flags & SCSI_POLL) { /* should use the queue and wdc_atapi_start */
1593 struct wdc_xfer xfer_s;
1594 int i, s;
1595
1596 s = splbio();
1597 #ifdef ATAPI_DEBUG_WDC
1598 printf("wdc_atapi_send_cmd: "
1599 "flags 0x%x drive %d cmdlen %d datalen %d",
1600 sc_xfer->flags, sc_link->scsipi_atapi.drive, sc_xfer->cmdlen,
1601 sc_xfer->datalen);
1602 #endif
1603 xfer = &xfer_s;
1604 bzero(xfer, sizeof(xfer_s));
1605 xfer->c_flags = C_INUSE|C_ATAPI|flags;
1606 xfer->d_link = (struct wd_link *)(&sc_link->scsipi_atapi);
1607 xfer->c_bp = sc_xfer->bp;
1608 xfer->atapi_cmd = sc_xfer;
1609 xfer->c_blkno = 0;
1610 xfer->databuf = sc_xfer->data;
1611 xfer->c_bcount = sc_xfer->datalen;
1612
1613 if (wait_for_unbusy (wdc) != 0) {
1614 if ((wdc->sc_status & WDCS_ERR) == 0) {
1615 printf("wdc_atapi_send_command: not ready, "
1616 "st = %02x\n", wdc->sc_status);
1617 sc_xfer->error = XS_SELTIMEOUT;
1618 } else {
1619 sc_xfer->error = XS_SENSE;
1620 sc_xfer->sense.atapi_sense = wdc->sc_error;
1621 }
1622 splx(s);
1623 return COMPLETE;
1624 }
1625
1626 /*
1627 * Limit length to what can be stuffed into the cylinder
1628 * register (16 bits). Some CD-ROMs seem to interpret '0'
1629 * as 65536, but not all devices do that and it's not
1630 * obvious from the ATAPI spec that that behaviour should
1631 * be expected. If more data is necessary, multiple data
1632 * transfer phases will be done.
1633 */
1634 if (wdccommand(wdc, (struct wd_link*)(&sc_link->scsipi_atapi),
1635 ATAPI_PACKET_COMMAND, sc_link->scsipi_atapi.drive,
1636 sc_xfer->datalen <= 0xffff ? sc_xfer->datalen : 0xffff,
1637 0, 0, 0) != 0) {
1638 printf("can't send atapi packet command\n");
1639 sc_xfer->error = XS_DRIVER_STUFFUP;
1640 splx(s);
1641 return COMPLETE;
1642 }
1643
1644 /* Wait for cmd i/o phase. */
1645 for (i = 20000; i > 0; --i) {
1646 int phase;
1647 phase = (bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_ireason) &
1648 (WDCI_CMD | WDCI_IN)) |
1649 (bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_status) & WDCS_DRQ);
1650 if (phase == PHASE_CMDOUT)
1651 break;
1652 delay(10);
1653 }
1654 #ifdef ATAPI_DEBUG_WDC
1655 printf("Wait for cmd i/o phase: i = %d\n", i);
1656 #endif
1657
1658 bus_space_write_multi_2(wdc->sc_iot, wdc->sc_ioh, wd_data,
1659 (u_int16_t *)sc_xfer->cmd, sc_xfer->cmdlen >> 1);
1660
1661 /* Wait for data i/o phase. */
1662 for ( i= 20000; i > 0; --i) {
1663 int phase;
1664 phase = (bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_ireason) &
1665 (WDCI_CMD | WDCI_IN)) |
1666 (bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_status) & WDCS_DRQ);
1667 if (phase != PHASE_CMDOUT)
1668 break;
1669 delay(10);
1670 }
1671
1672 #ifdef ATAPI_DEBUG_WDC
1673 printf("Wait for data i/o phase: i = %d\n", i);
1674 #endif
1675 wdc->sc_flags |= WDCF_IRQ_WAIT;
1676 while ((sc_xfer->flags & ITSDONE) == 0) {
1677 wdc_atapi_intr(wdc, xfer);
1678 for (i = 2000; i > 0; --i)
1679 if ((bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_status)
1680 & WDCS_DRQ) == 0)
1681 break;
1682 #ifdef ATAPI_DEBUG_WDC
1683 printf("wdc_atapi_intr: i = %d\n", i);
1684 #endif
1685 }
1686 wdc->sc_flags &= ~(WDCF_IRQ_WAIT | WDCF_SINGLE | WDCF_ERROR);
1687 wdc->sc_errors = 0;
1688 xfer->c_skip = 0;
1689 splx(s);
1690 return COMPLETE;
1691 } else { /* POLLED */
1692 xfer = wdc_get_xfer(flags & SCSI_NOSLEEP ? IDE_NOSLEEP : 0);
1693 if (xfer == NULL) {
1694 return TRY_AGAIN_LATER;
1695 }
1696 xfer->c_flags |= C_ATAPI|sc_xfer->flags;
1697 xfer->d_link = (struct wd_link*)(&sc_link->scsipi_atapi);
1698 xfer->c_bp = sc_xfer->bp;
1699 xfer->atapi_cmd = sc_xfer;
1700 xfer->c_blkno = 0;
1701 xfer->databuf = sc_xfer->data;
1702 xfer->c_bcount = sc_xfer->datalen;
1703 wdc_exec_xfer(wdc, xfer->d_link, xfer);
1704 #ifdef ATAPI_DEBUG_WDC
1705 printf("wdc_atapi_send_command_packet: wdc_exec_xfer, flags 0x%x\n",
1706 sc_xfer->flags);
1707 #endif
1708 return (sc_xfer->flags & ITSDONE) ? COMPLETE : SUCCESSFULLY_QUEUED;
1709 }
1710 }
1711
1712 int
1713 wdc_atapi_intr(wdc, xfer)
1714 struct wdc_softc *wdc;
1715 struct wdc_xfer *xfer;
1716 {
1717 struct scsipi_xfer *sc_xfer = xfer->atapi_cmd;
1718 int len, phase, i, retries=0;
1719 int err, st, ire;
1720
1721 #ifdef ATAPI_DEBUG2
1722 printf("wdc_atapi_intr: %s\n", wdc->sc_dev.dv_xname);
1723 #endif
1724
1725 if (wait_for_unbusy(wdc) < 0) {
1726 if ((wdc->sc_status & WDCS_ERR) == 0) {
1727 printf("wdc_atapi_intr: controller busy\n");
1728 return 0;
1729 } else {
1730 sc_xfer->error = XS_SENSE;
1731 sc_xfer->sense.atapi_sense = wdc->sc_error;
1732 }
1733 #ifdef ATAPI_DEBUG_WDC
1734 printf("wdc_atapi_intr: wdc_atapi_done(), error %d\n",
1735 sc_xfer->error);
1736 #endif
1737 wdc_atapi_done(wdc, xfer);
1738 return 0;
1739 }
1740
1741
1742 again:
1743 len = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_cyl_lo) +
1744 256 * bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_cyl_hi);
1745
1746 st = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_status);
1747 err = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_error);
1748 ire = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_ireason);
1749
1750 phase = (ire & (WDCI_CMD | WDCI_IN)) | (st & WDCS_DRQ);
1751 #ifdef ATAPI_DEBUG_WDC
1752 printf("wdc_atapi_intr: len %d st %d err %d ire %d :",
1753 len, st, err, ire);
1754 #endif
1755 switch (phase) {
1756 case PHASE_CMDOUT:
1757 /* send packet command */
1758 #ifdef ATAPI_DEBUG_WDC
1759 printf("PHASE_CMDOUT\n");
1760 #endif
1761
1762 #ifdef ATAPI_DEBUG_WDC
1763 {
1764 int i;
1765 char *c = (char *)sc_xfer->cmd;
1766 printf("wdc_atapi_intr: cmd ");
1767 for (i = 0; i < sc_xfer->cmdlen; i++)
1768 printf("%x ", c[i]);
1769 printf("\n");
1770 }
1771 #endif
1772
1773 bus_space_write_multi_2(wdc->sc_iot, wdc->sc_ioh, wd_data,
1774 (u_int16_t *)sc_xfer->cmd, sc_xfer->cmdlen >> 1);
1775 return 1;
1776
1777 case PHASE_DATAOUT:
1778 /* write data */
1779 #ifdef ATAPI_DEBUG_WDC
1780 printf("PHASE_DATAOUT\n");
1781 #endif
1782 if ((sc_xfer->flags & SCSI_DATA_OUT) == 0) {
1783 printf("wdc_atapi_intr: bad data phase\n");
1784 sc_xfer->error = XS_DRIVER_STUFFUP;
1785 return 0;
1786 }
1787 if (xfer->c_bcount < len) {
1788 printf("wdc_atapi_intr: warning: write only "
1789 "%d of %d requested bytes\n", xfer->c_bcount, len);
1790 bus_space_write_multi_2(wdc->sc_iot, wdc->sc_ioh,
1791 wd_data, xfer->databuf + xfer->c_skip,
1792 xfer->c_bcount >> 1);
1793 for (i = xfer->c_bcount; i < len; i += 2)
1794 bus_space_write_2(wdc->sc_iot, wdc->sc_ioh,
1795 wd_data, 0);
1796 xfer->c_skip += xfer->c_bcount;
1797 xfer->c_bcount = 0;
1798 } else {
1799 bus_space_write_multi_2(wdc->sc_iot, wdc->sc_ioh,
1800 wd_data, xfer->databuf + xfer->c_skip, len >> 1);
1801 xfer->c_skip += len;
1802 xfer->c_bcount -= len;
1803 }
1804 return 1;
1805
1806 case PHASE_DATAIN:
1807 /* Read data */
1808 #ifdef ATAPI_DEBUG_WDC
1809 printf("PHASE_DATAIN\n");
1810 #endif
1811 if ((sc_xfer->flags & SCSI_DATA_IN) == 0) {
1812 printf("wdc_atapi_intr: bad data phase\n");
1813 sc_xfer->error = XS_DRIVER_STUFFUP;
1814 return 0;
1815 }
1816 if (xfer->c_bcount < len) {
1817 printf("wdc_atapi_intr: warning: reading only "
1818 "%d of %d bytes\n", xfer->c_bcount, len);
1819 bus_space_read_multi_2(wdc->sc_iot, wdc->sc_ioh,
1820 wd_data, xfer->databuf + xfer->c_skip,
1821 xfer->c_bcount >> 1);
1822 wdcbit_bucket(wdc, len - xfer->c_bcount);
1823 xfer->c_skip += xfer->c_bcount;
1824 xfer->c_bcount = 0;
1825 } else {
1826 bus_space_read_multi_2(wdc->sc_iot, wdc->sc_ioh,
1827 wd_data, xfer->databuf + xfer->c_skip, len >> 1);
1828 xfer->c_skip += len;
1829 xfer->c_bcount -=len;
1830 }
1831 return 1;
1832
1833 case PHASE_ABORTED:
1834 case PHASE_COMPLETED:
1835 #ifdef ATAPI_DEBUG_WDC
1836 printf("PHASE_COMPLETED\n");
1837 #endif
1838 if (st & WDCS_ERR) {
1839 sc_xfer->error = XS_SENSE;
1840 sc_xfer->sense.atapi_sense = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_error);
1841 }
1842 #ifdef ATAPI_DEBUG_WDC
1843 if (xfer->c_bcount != 0) {
1844 printf("wdc_atapi_intr warning: bcount value "
1845 "is %d after io\n", xfer->c_bcount);
1846 }
1847 #endif
1848 break;
1849
1850 default:
1851 if (++retries<500) {
1852 DELAY(100);
1853 goto again;
1854 }
1855 printf("wdc_atapi_intr: unknown phase %d\n", phase);
1856 if (st & WDCS_ERR) {
1857 sc_xfer->error = XS_SENSE;
1858 sc_xfer->sense.atapi_sense = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_error);
1859 } else {
1860 sc_xfer->error = XS_DRIVER_STUFFUP;
1861 }
1862 }
1863
1864 #ifdef ATAPI_DEBUG_WDC
1865 printf("wdc_atapi_intr: wdc_atapi_done() (end), error %d\n",
1866 sc_xfer->error);
1867 #endif
1868 wdc_atapi_done(wdc, xfer);
1869 return (1);
1870 }
1871
1872
1873 void
1874 wdc_atapi_done(wdc, xfer)
1875 struct wdc_softc *wdc;
1876 struct wdc_xfer *xfer;
1877 {
1878 struct scsipi_xfer *sc_xfer = xfer->atapi_cmd;
1879 int s;
1880 int need_done = xfer->c_flags & C_NEEDDONE;
1881
1882 if (wdc->sc_cap & WDC_CAPABILITY_HWLOCK)
1883 (*wdc->sc_free_hw)(wdc);
1884
1885 #ifdef ATAPI_DEBUG
1886 printf("wdc_atapi_done: flags 0x%x\n", (u_int)xfer->c_flags);
1887 #endif
1888 sc_xfer->resid = xfer->c_bcount;
1889 wdc->sc_flags &= ~WDCF_IRQ_WAIT;
1890
1891 /* remove this command from xfer queue */
1892 wdc->sc_errors = 0;
1893 xfer->c_skip = 0;
1894 if ((xfer->c_flags & SCSI_POLL) == 0) {
1895 s = splbio();
1896 untimeout(wdctimeout, wdc);
1897 TAILQ_REMOVE(&wdc->sc_xfer, xfer, c_xferchain);
1898 wdc->sc_flags &= ~(WDCF_SINGLE | WDCF_ERROR | WDCF_ACTIVE);
1899 wdc_free_xfer(xfer);
1900 sc_xfer->flags |= ITSDONE;
1901 if (need_done) {
1902 #ifdef ATAPI_DEBUG
1903 printf("wdc_atapi_done: scsipi_done\n");
1904 #endif
1905 scsipi_done(sc_xfer);
1906 }
1907 #ifdef WDDEBUG
1908 printf("wdcstart from wdc_atapi_intr, flags 0x%x\n",
1909 wdc->sc_flags);
1910 #endif
1911 wdcstart(wdc);
1912 splx(s);
1913 } else {
1914 wdc->sc_flags &= ~(WDCF_SINGLE | WDCF_ERROR | WDCF_ACTIVE);
1915 sc_xfer->flags |= ITSDONE;
1916 }
1917 }
1918
1919 #endif /* NATAPIBUS > 0 */
1920