atapi_wdc.c revision 1.6 1 /* $NetBSD: atapi_wdc.c,v 1.6 1998/10/13 15:18:48 bouyer Exp $ */
2
3 /*
4 * Copyright (c) 1998 Manuel Bouyer.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the University of
17 * California, Berkeley and its contributors.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 */
35
36 #define WDCDEBUG
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/file.h>
42 #include <sys/stat.h>
43 #include <sys/buf.h>
44 #include <sys/malloc.h>
45 #include <sys/device.h>
46 #include <sys/syslog.h>
47 #include <sys/proc.h>
48
49 #include <vm/vm.h>
50
51 #include <machine/intr.h>
52 #include <machine/bus.h>
53
54 #ifndef __BUS_SPACE_HAS_STREAM_METHODS
55 #define bus_space_write_multi_stream_2 bus_space_write_multi_2
56 #define bus_space_write_multi_stream_4 bus_space_write_multi_4
57 #define bus_space_read_multi_stream_2 bus_space_read_multi_2
58 #define bus_space_read_multi_stream_4 bus_space_read_multi_4
59 #endif /* __BUS_SPACE_HAS_STREAM_METHODS */
60
61 #include <dev/ata/atareg.h>
62 #include <dev/ata/atavar.h>
63 #include <dev/ic/wdcreg.h>
64 #include <dev/ic/wdcvar.h>
65 #include <dev/scsipi/scsipi_all.h>
66 #include <dev/scsipi/scsipiconf.h>
67 #include <dev/scsipi/atapiconf.h>
68
69 #define DEBUG_INTR 0x01
70 #define DEBUG_XFERS 0x02
71 #define DEBUG_STATUS 0x04
72 #define DEBUG_FUNCS 0x08
73 #define DEBUG_PROBE 0x10
74 #ifdef WDCDEBUG
75 int wdcdebug_atapi_mask = 0;
76 #define WDCDEBUG_PRINT(args, level) \
77 if (wdcdebug_atapi_mask & (level)) \
78 printf args
79 #else
80 #define WDCDEBUG_PRINT(args, level)
81 #endif
82
83 #define ATAPI_DELAY 10 /* 10 ms, this is used only before sending a cmd */
84
85 void wdc_atapi_minphys __P((struct buf *bp));
86 void wdc_atapi_start __P((struct channel_softc *,struct wdc_xfer *));
87 int wdc_atapi_intr __P((struct channel_softc *, struct wdc_xfer *));
88 int wdc_atapi_ctrl __P((struct channel_softc *, struct wdc_xfer *));
89 void wdc_atapi_done __P((struct channel_softc *, struct wdc_xfer *));
90 void wdc_atapi_reset __P((struct channel_softc *, struct wdc_xfer *));
91 int wdc_atapi_send_cmd __P((struct scsipi_xfer *sc_xfer));
92
93 #define MAX_SIZE MAXPHYS
94
95 static struct scsipi_adapter wdc_switch = {
96 wdc_atapi_send_cmd,
97 wdc_atapi_minphys,
98 NULL, /* scsipi_ioctl */
99 };
100
101 void
102 wdc_atapibus_attach(chp)
103 struct channel_softc *chp;
104 {
105 struct wdc_softc *wdc = chp->wdc;
106 int channel = chp->channel;
107 struct ata_atapi_attach aa_link;
108
109 memset(&aa_link, 0, sizeof(struct ata_atapi_attach));
110 aa_link.aa_type = T_ATAPI;
111 aa_link.aa_channel = channel;
112 aa_link.aa_openings = 1;
113 aa_link.aa_drv_data = chp->ch_drive; /* pass the whole array */
114 aa_link.aa_bus_private = &wdc_switch;
115 (void)config_found(&wdc->sc_dev, (void *)&aa_link, atapi_print);
116 }
117
118 void
119 wdc_atapi_minphys (struct buf *bp)
120 {
121 if(bp->b_bcount > MAX_SIZE)
122 bp->b_bcount = MAX_SIZE;
123 minphys(bp);
124 }
125
126 int
127 wdc_atapi_get_params(ab_link, drive, flags, id)
128 struct scsipi_link *ab_link;
129 u_int8_t drive;
130 int flags;
131 struct ataparams *id;
132 {
133 struct wdc_softc *wdc = (void*)ab_link->adapter_softc;
134 struct channel_softc *chp =
135 &wdc->channels[ab_link->scsipi_atapi.channel];
136 struct wdc_command wdc_c;
137
138 /* if no ATAPI device detected at wdc attach time, skip */
139 /*
140 * XXX this will break scsireprobe if this is of any interest for
141 * ATAPI devices one day.
142 */
143 if ((chp->ch_drive[drive].drive_flags & DRIVE_ATAPI) == 0) {
144 WDCDEBUG_PRINT(("wdc_atapi_get_params: drive %d not present\n",
145 drive), DEBUG_PROBE);
146 return -1;
147 }
148 memset(&wdc_c, 0, sizeof(struct wdc_command));
149 wdc_c.r_command = ATAPI_SOFT_RESET;
150 wdc_c.r_st_bmask = 0;
151 wdc_c.r_st_pmask = 0;
152 wdc_c.flags = AT_POLL;
153 wdc_c.timeout = WDC_RESET_WAIT;
154 if (wdc_exec_command(&chp->ch_drive[drive], &wdc_c) != WDC_COMPLETE) {
155 printf("wdc_atapi_get_params: ATAPI_SOFT_RESET failed for"
156 " drive %s:%d:%d: driver failed\n",
157 chp->wdc->sc_dev.dv_xname, chp->channel, drive);
158 panic("wdc_atapi_get_params");
159 }
160 if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
161 WDCDEBUG_PRINT(("wdc_atapi_get_params: ATAPI_SOFT_RESET "
162 "failed for drive %s:%d:%d: error 0x%x\n",
163 chp->wdc->sc_dev.dv_xname, chp->channel, drive,
164 wdc_c.r_error), DEBUG_PROBE);
165 return -1;
166 }
167 chp->ch_drive[drive].state = 0;
168
169 bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_status);
170
171 /* Some ATAPI devices need a bit more time after software reset. */
172 delay(5000);
173 if (ata_get_params(&chp->ch_drive[drive], AT_POLL, id) != 0) {
174 WDCDEBUG_PRINT(("wdc_atapi_get_params: ATAPI_IDENTIFY_DEVICE "
175 "failed for drive %s:%d:%d: error 0x%x\n",
176 chp->wdc->sc_dev.dv_xname, chp->channel, drive,
177 wdc_c.r_error), DEBUG_PROBE);
178 return -1;
179 }
180 return COMPLETE;
181 }
182
183 int
184 wdc_atapi_send_cmd(sc_xfer)
185 struct scsipi_xfer *sc_xfer;
186 {
187 struct scsipi_link *sc_link = sc_xfer->sc_link;
188 struct wdc_softc *wdc = (void*)sc_link->adapter_softc;
189 struct wdc_xfer *xfer;
190 struct ata_drive_datas *drvp;
191 int flags = sc_xfer->flags;
192 int channel = sc_xfer->sc_link->scsipi_atapi.channel;
193 int drive = sc_xfer->sc_link->scsipi_atapi.drive;
194 int s, ret;
195
196 WDCDEBUG_PRINT(("wdc_atapi_send_cmd %s:%d:%d\n",
197 wdc->sc_dev.dv_xname, channel, drive), DEBUG_XFERS);
198
199 xfer = wdc_get_xfer(flags & SCSI_NOSLEEP ? WDC_NOSLEEP : WDC_CANSLEEP);
200 if (xfer == NULL) {
201 return TRY_AGAIN_LATER;
202 }
203 if (sc_xfer->flags & SCSI_POLL)
204 xfer->c_flags |= C_POLL;
205 drvp = &wdc->channels[channel].ch_drive[drive];
206 if ((drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA)) &&
207 sc_xfer->datalen > 0)
208 xfer->c_flags |= C_DMA;
209 xfer->drive = drive;
210 xfer->c_flags |= C_ATAPI;
211 xfer->cmd = sc_xfer;
212 xfer->databuf = sc_xfer->data;
213 xfer->c_bcount = sc_xfer->datalen;
214 xfer->c_start = wdc_atapi_start;
215 xfer->c_intr = wdc_atapi_intr;
216 s = splbio();
217 wdc_exec_xfer(&wdc->channels[channel], xfer);
218 #ifdef DIAGNOSTIC
219 if ((sc_xfer->flags & SCSI_POLL) != 0 &&
220 (sc_xfer->flags & ITSDONE) == 0)
221 panic("wdc_atapi_send_cmd: polled command not done");
222 #endif
223 ret = (sc_xfer->flags & ITSDONE) ? COMPLETE : SUCCESSFULLY_QUEUED;
224 splx(s);
225 return ret;
226 }
227
228 void
229 wdc_atapi_start(chp, xfer)
230 struct channel_softc *chp;
231 struct wdc_xfer *xfer;
232 {
233 struct scsipi_xfer *sc_xfer = xfer->cmd;
234 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
235
236 WDCDEBUG_PRINT(("wdc_atapi_start %s:%d:%d, scsi flags 0x%x \n",
237 chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive,
238 sc_xfer->flags), DEBUG_XFERS);
239 /* Do control operations specially. */
240 if (drvp->state < READY) {
241 if (drvp->state != PIOMODE) {
242 printf("%s:%d:%d: bad state %d in wdc_atapi_start\n",
243 chp->wdc->sc_dev.dv_xname, chp->channel,
244 xfer->drive, drvp->state);
245 panic("wdc_atapi_start: bad state");
246 }
247 wdc_atapi_ctrl(chp, xfer);
248 return;
249 }
250 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
251 WDSD_IBM | (xfer->drive << 4));
252 if (wait_for_unbusy(chp, ATAPI_DELAY) < 0) {
253 printf("wdc_atapi_start: not ready, st = %02x\n",
254 chp->ch_status);
255 sc_xfer->error = XS_TIMEOUT;
256 wdc_atapi_reset(chp, xfer);
257 return;
258 }
259
260 /*
261 * Even with WDCS_ERR, the device should accept a command packet
262 * Limit length to what can be stuffed into the cylinder register
263 * (16 bits). Some CD-ROMs seem to interpret '0' as 65536,
264 * but not all devices do that and it's not obvious from the
265 * ATAPI spec that that behaviour should be expected. If more
266 * data is necessary, multiple data transfer phases will be done.
267 */
268
269 wdccommand(chp, xfer->drive, ATAPI_PKT_CMD,
270 sc_xfer->datalen <= 0xffff ? sc_xfer->datalen : 0xffff,
271 0, 0, 0,
272 (xfer->c_flags & C_DMA) ? ATAPI_PKT_CMD_FTRE_DMA : 0);
273
274 /*
275 * If there is no interrupt for CMD input, busy-wait for it (done in
276 * the interrupt routine. If it is a polled command, call the interrupt
277 * routine until command is done.
278 */
279 if ((sc_xfer->sc_link->scsipi_atapi.cap & 0x0300) != ACAP_DRQ_INTR ||
280 sc_xfer->flags & SCSI_POLL) {
281 /* Wait for at last 400ns for status bit to be valid */
282 delay(1);
283 if (wdc_atapi_intr(chp, xfer) == 0) {
284 sc_xfer->error = XS_TIMEOUT;
285 wdc_atapi_reset(chp, xfer);
286 return;
287 }
288 }
289 if (sc_xfer->flags & SCSI_POLL) {
290 while ((sc_xfer->flags & ITSDONE) == 0) {
291 /* Wait for at last 400ns for status bit to be valid */
292 delay(1);
293 if (wdc_atapi_intr(chp, xfer) == 0) {
294 sc_xfer->error = XS_SELTIMEOUT;
295 /* do we know more ? */
296 wdc_atapi_done(chp, xfer);
297 return;
298 }
299 }
300 }
301 }
302
303 int
304 wdc_atapi_intr(chp, xfer)
305 struct channel_softc *chp;
306 struct wdc_xfer *xfer;
307 {
308 struct scsipi_xfer *sc_xfer = xfer->cmd;
309 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
310 int len, phase, i, retries=0;
311 int ire, dma_err = 0;
312 int dma_flags = 0;
313
314 WDCDEBUG_PRINT(("wdc_atapi_intr %s:%d:%d\n",
315 chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive), DEBUG_INTR);
316
317 /* Is it not a transfer, but a control operation? */
318 if (drvp->state < READY) {
319 printf("%s:%d:%d: bad state %d in wdc_atapi_intr\n",
320 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
321 drvp->state);
322 panic("wdc_atapi_intr: bad state\n");
323 }
324 /* Ack interrupt done in wait_for_unbusy */
325 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
326 WDSD_IBM | (xfer->drive << 4));
327 if (wait_for_unbusy(chp, sc_xfer->timeout) != 0) {
328 printf("%s:%d:%d: device timeout, c_bcount=%d, c_skip%d\n",
329 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
330 xfer->c_bcount, xfer->c_skip);
331 sc_xfer->error = XS_TIMEOUT;
332 wdc_atapi_reset(chp, xfer);
333 return 1;
334 }
335 /* Error here only if the command is aborted */
336 if ((chp->ch_status & WDCS_ERR) != 0 &&
337 (chp->ch_error & WDCE_ABRT) != 0) {
338 sc_xfer->error = XS_SENSE;
339 sc_xfer->sense.atapi_sense = chp->ch_error;
340 WDCDEBUG_PRINT(("wdc_atapi_intr: wdc_atapi_done(), "
341 "sense 0x%x\n", sc_xfer->sense.atapi_sense), DEBUG_INTR);
342 wdc_atapi_done(chp, xfer);
343 return 1;
344 }
345
346 if (xfer->c_flags & C_DMA) {
347 dma_flags = (sc_xfer->flags & SCSI_DATA_IN) ?
348 WDC_DMA_READ : 0;
349 dma_flags |= sc_xfer->flags & SCSI_POLL ? WDC_DMA_POLL : 0;
350 }
351 again:
352 len = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_cyl_lo) +
353 256 * bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_cyl_hi);
354 ire = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_ireason);
355 phase = (ire & (WDCI_CMD | WDCI_IN)) | (chp->ch_status & WDCS_DRQ);
356 WDCDEBUG_PRINT(("wdc_atapi_intr: c_bcount %d len %d st 0x%x err 0x%x "
357 "ire 0x%x :", xfer->c_bcount,
358 len, chp->ch_status, chp->ch_error, ire), DEBUG_INTR);
359
360 switch (phase) {
361 case PHASE_CMDOUT:
362 WDCDEBUG_PRINT(("PHASE_CMDOUT\n"), DEBUG_INTR);
363 /* Init the DMA channel if necessary */
364 if (xfer->c_flags & C_DMA) {
365 if ((*chp->wdc->dma_init)(chp->wdc->dma_arg,
366 chp->channel, xfer->drive,
367 xfer->databuf, sc_xfer->datalen, dma_flags) != 0) {
368 sc_xfer->error = XS_DRIVER_STUFFUP;
369 break;
370 }
371 }
372 /* send packet command */
373 /* Commands are 12 or 16 bytes long. It's 32-bit aligned */
374 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) {
375 if (drvp->drive_flags & DRIVE_CAP32) {
376 bus_space_write_multi_4(chp->data32iot,
377 chp->data32ioh, 0,
378 (u_int32_t *)sc_xfer->cmd,
379 sc_xfer->cmdlen >> 2);
380 } else {
381 bus_space_write_multi_2(chp->cmd_iot,
382 chp->cmd_ioh, wd_data,
383 (u_int16_t *)sc_xfer->cmd,
384 sc_xfer->cmdlen >> 1);
385 }
386 } else {
387 if (drvp->drive_flags & DRIVE_CAP32) {
388 bus_space_write_multi_stream_4(chp->data32iot,
389 chp->data32ioh, 0,
390 (u_int32_t *)sc_xfer->cmd,
391 sc_xfer->cmdlen >> 2);
392 } else {
393 bus_space_write_multi_stream_2(chp->cmd_iot,
394 chp->cmd_ioh, wd_data,
395 (u_int16_t *)sc_xfer->cmd,
396 sc_xfer->cmdlen >> 1);
397 }
398 }
399 /* Start the DMA channel if necessary */
400 if (xfer->c_flags & C_DMA) {
401 (*chp->wdc->dma_start)(chp->wdc->dma_arg,
402 chp->channel, xfer->drive, dma_flags);
403 }
404
405 if ((sc_xfer->flags & SCSI_POLL) == 0) {
406 chp->ch_flags |= WDCF_IRQ_WAIT;
407 timeout(wdctimeout, chp, sc_xfer->timeout * hz / 1000);
408 }
409 return 1;
410
411 case PHASE_DATAOUT:
412 /* write data */
413 WDCDEBUG_PRINT(("PHASE_DATAOUT\n"), DEBUG_INTR);
414 if ((sc_xfer->flags & SCSI_DATA_OUT) == 0 ||
415 (xfer->c_flags & C_DMA) != 0) {
416 printf("wdc_atapi_intr: bad data phase DATAOUT");
417 if (xfer->c_flags & C_DMA) {
418 printf(", falling back to PIO\n");
419 drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
420 }
421 sc_xfer->error = XS_TIMEOUT;
422 wdc_atapi_reset(chp, xfer);
423 return 1;
424 }
425 if (xfer->c_bcount < len) {
426 printf("wdc_atapi_intr: warning: write only "
427 "%d of %d requested bytes\n", xfer->c_bcount, len);
428 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) {
429 bus_space_write_multi_2(chp->cmd_iot,
430 chp->cmd_ioh, wd_data,
431 xfer->databuf + xfer->c_skip,
432 xfer->c_bcount >> 1);
433 } else {
434 bus_space_write_multi_stream_2(chp->cmd_iot,
435 chp->cmd_ioh, wd_data,
436 xfer->databuf + xfer->c_skip,
437 xfer->c_bcount >> 1);
438 }
439 for (i = xfer->c_bcount; i < len; i += 2)
440 bus_space_write_2(chp->cmd_iot, chp->cmd_ioh,
441 wd_data, 0);
442 xfer->c_skip += xfer->c_bcount;
443 xfer->c_bcount = 0;
444 } else {
445 if (drvp->drive_flags & DRIVE_CAP32) {
446 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
447 bus_space_write_multi_4(chp->data32iot,
448 chp->data32ioh, 0,
449 xfer->databuf + xfer->c_skip, len >> 2);
450 else
451 bus_space_write_multi_stream_4(chp->cmd_iot,
452 chp->cmd_ioh, wd_data,
453 xfer->databuf + xfer->c_skip, len >> 2);
454
455 xfer->c_skip += len & 0xfffffffc;
456 xfer->c_bcount -= len & 0xfffffffc;
457 len = len & 0x03;
458 }
459 if (len > 0) {
460 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
461 bus_space_write_multi_2(chp->cmd_iot,
462 chp->cmd_ioh, wd_data,
463 xfer->databuf + xfer->c_skip, len >> 1);
464 else
465 bus_space_write_multi_stream_2(chp->cmd_iot,
466 chp->cmd_ioh, wd_data,
467 xfer->databuf + xfer->c_skip, len >> 1);
468 xfer->c_skip += len;
469 xfer->c_bcount -= len;
470 }
471 }
472 if ((sc_xfer->flags & SCSI_POLL) == 0) {
473 chp->ch_flags |= WDCF_IRQ_WAIT;
474 timeout(wdctimeout, chp, sc_xfer->timeout * hz / 1000);
475 }
476 return 1;
477
478 case PHASE_DATAIN:
479 /* Read data */
480 WDCDEBUG_PRINT(("PHASE_DATAIN\n"), DEBUG_INTR);
481 if ((sc_xfer->flags & SCSI_DATA_IN) == 0 ||
482 (xfer->c_flags & C_DMA) != 0) {
483 printf("wdc_atapi_intr: bad data phase DATAIN");
484 if (xfer->c_flags & C_DMA) {
485 printf(", falling back to PIO\n");
486 drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
487 }
488 sc_xfer->error = XS_TIMEOUT;
489 wdc_atapi_reset(chp, xfer);
490 return 1;
491 }
492 if (xfer->c_bcount < len) {
493 printf("wdc_atapi_intr: warning: reading only "
494 "%d of %d bytes\n", xfer->c_bcount, len);
495 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) {
496 bus_space_read_multi_2(chp->cmd_iot,
497 chp->cmd_ioh, wd_data,
498 xfer->databuf + xfer->c_skip, xfer->c_bcount >> 1);
499 } else {
500 bus_space_read_multi_stream_2(chp->cmd_iot,
501 chp->cmd_ioh, wd_data,
502 xfer->databuf + xfer->c_skip, xfer->c_bcount >> 1);
503 }
504 wdcbit_bucket(chp, len - xfer->c_bcount);
505 xfer->c_skip += xfer->c_bcount;
506 xfer->c_bcount = 0;
507 } else {
508 if (drvp->drive_flags & DRIVE_CAP32) {
509 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
510 bus_space_read_multi_4(chp->data32iot,
511 chp->data32ioh, 0,
512 xfer->databuf + xfer->c_skip, len >> 2);
513 else
514 bus_space_read_multi_stream_4(chp->cmd_iot,
515 chp->cmd_ioh, wd_data,
516 xfer->databuf + xfer->c_skip, len >> 2);
517
518 xfer->c_skip += len & 0xfffffffc;
519 xfer->c_bcount -= len & 0xfffffffc;
520 len = len & 0x03;
521 }
522 if (len > 0) {
523 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
524 bus_space_read_multi_2(chp->cmd_iot,
525 chp->cmd_ioh, wd_data,
526 xfer->databuf + xfer->c_skip, len >> 1);
527 else
528 bus_space_read_multi_stream_2(chp->cmd_iot,
529 chp->cmd_ioh, wd_data,
530 xfer->databuf + xfer->c_skip, len >> 1);
531 xfer->c_skip += len;
532 xfer->c_bcount -=len;
533 }
534 }
535 if ((sc_xfer->flags & SCSI_POLL) == 0) {
536 chp->ch_flags |= WDCF_IRQ_WAIT;
537 timeout(wdctimeout, chp, sc_xfer->timeout * hz / 1000);
538 }
539 return 1;
540
541 case PHASE_ABORTED:
542 case PHASE_COMPLETED:
543 WDCDEBUG_PRINT(("PHASE_COMPLETED\n"), DEBUG_INTR);
544 /* turn off DMA channel */
545 if (xfer->c_flags & C_DMA) {
546 dma_err = (*chp->wdc->dma_finish)(chp->wdc->dma_arg,
547 chp->channel, xfer->drive, dma_flags);
548 xfer->c_bcount -= sc_xfer->datalen;
549 }
550
551 if (chp->ch_status & WDCS_ERR) {
552 sc_xfer->error = XS_SENSE;
553 sc_xfer->sense.atapi_sense = chp->ch_error;
554 } else if (dma_err < 0) {
555 sc_xfer->error = XS_DRIVER_STUFFUP;
556 }
557 #ifdef DIAGNOSTIC
558 if (xfer->c_bcount != 0) {
559 printf("wdc_atapi_intr warning: bcount value "
560 "is %d after io\n", xfer->c_bcount);
561 }
562 #endif
563 break;
564
565 default:
566 if (++retries<500) {
567 DELAY(100);
568 chp->ch_status = bus_space_read_1(chp->cmd_iot,
569 chp->cmd_ioh, wd_status);
570 chp->ch_error = bus_space_read_1(chp->cmd_iot,
571 chp->cmd_ioh, wd_error);
572 goto again;
573 }
574 printf("wdc_atapi_intr: unknown phase 0x%x\n", phase);
575 if (chp->ch_status & WDCS_ERR) {
576 sc_xfer->error = XS_SENSE;
577 sc_xfer->sense.atapi_sense = chp->ch_error;
578 } else {
579 sc_xfer->error = XS_DRIVER_STUFFUP;
580 }
581 }
582 WDCDEBUG_PRINT(("wdc_atapi_intr: wdc_atapi_done() (end), error 0x%x "
583 "sense 0x%x\n", sc_xfer->error, sc_xfer->sense.atapi_sense),
584 DEBUG_INTR);
585 wdc_atapi_done(chp, xfer);
586 return (1);
587 }
588
589 int
590 wdc_atapi_ctrl(chp, xfer)
591 struct channel_softc *chp;
592 struct wdc_xfer *xfer;
593 {
594 struct scsipi_xfer *sc_xfer = xfer->cmd;
595 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
596 char *errstring = NULL;
597
598 /* Ack interrupt done in wait_for_unbusy */
599 again:
600 WDCDEBUG_PRINT(("wdc_atapi_ctrl %s:%d:%d state %d\n",
601 chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive, drvp->state),
602 DEBUG_INTR | DEBUG_FUNCS);
603 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
604 WDSD_IBM | (xfer->drive << 4));
605 switch (drvp->state) {
606 case PIOMODE:
607 /* Don't try to set mode if controller can't be adjusted */
608 if ((chp->wdc->cap & WDC_CAPABILITY_MODE) == 0)
609 goto ready;
610 /* Also don't try if the drive didn't report its mode */
611 if ((drvp->drive_flags & DRIVE_MODE) == 0)
612 goto ready;;
613 wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
614 0x08 | drvp->PIO_mode, WDSF_SET_MODE);
615 drvp->state = PIOMODE_WAIT;
616 break;
617 case PIOMODE_WAIT:
618 errstring = "piomode";
619 if (wait_for_unbusy(chp, ATAPI_DELAY))
620 goto timeout;
621 if (chp->ch_status & WDCS_ERR)
622 goto error;
623 /* fall through */
624
625 case DMAMODE:
626 if (drvp->drive_flags & DRIVE_UDMA) {
627 wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
628 0x40 | drvp->UDMA_mode, WDSF_SET_MODE);
629 } else if (drvp->drive_flags & DRIVE_DMA) {
630 wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
631 0x20 | drvp->DMA_mode, WDSF_SET_MODE);
632 } else {
633 goto ready;
634 }
635 drvp->state = DMAMODE_WAIT;
636 break;
637 case DMAMODE_WAIT:
638 errstring = "dmamode";
639 if (wait_for_unbusy(chp, ATAPI_DELAY))
640 goto timeout;
641 if (chp->ch_status & WDCS_ERR)
642 goto error;
643 /* fall through */
644
645 case READY:
646 ready:
647 drvp->state = READY;
648 xfer->c_intr = wdc_atapi_intr;
649 wdc_atapi_start(chp, xfer);
650 return 1;
651 }
652 if ((sc_xfer->flags & SCSI_POLL) == 0) {
653 chp->ch_flags |= WDCF_IRQ_WAIT;
654 xfer->c_intr = wdc_atapi_ctrl;
655 timeout(wdctimeout, chp, sc_xfer->timeout * hz / 1000);
656 } else {
657 goto again;
658 }
659 return 1;
660
661 timeout:
662 if ((xfer->c_flags & C_TIMEOU) == 0 ) {
663 return 0; /* IRQ was not for us */
664 }
665 printf("%s:%d:%d: %s timed out\n",
666 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive, errstring);
667 sc_xfer->error = XS_TIMEOUT;
668 wdc_atapi_reset(chp, xfer);
669 return 1;
670 error:
671 printf("%s:%d:%d: %s ",
672 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
673 errstring);
674 printf("error (0x%x)\n", chp->ch_error);
675 sc_xfer->error = XS_SENSE;
676 sc_xfer->sense.atapi_sense = chp->ch_error;
677 wdc_atapi_reset(chp, xfer);
678 return 1;
679 }
680
681 void
682 wdc_atapi_done(chp, xfer)
683 struct channel_softc *chp;
684 struct wdc_xfer *xfer;
685 {
686 struct scsipi_xfer *sc_xfer = xfer->cmd;
687 struct wdc_softc *wdc = chp->wdc;
688 int need_done = xfer->c_flags & C_NEEDDONE;
689
690 WDCDEBUG_PRINT(("wdc_atapi_done %s:%d:%d: flags 0x%x\n",
691 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
692 (u_int)xfer->c_flags), DEBUG_XFERS);
693 sc_xfer->resid = xfer->c_bcount;
694 /* remove this command from xfer queue */
695 xfer->c_skip = 0;
696 wdc_free_xfer(chp, xfer);
697 sc_xfer->flags |= ITSDONE;
698 if (need_done) {
699 WDCDEBUG_PRINT(("wdc_atapi_done: scsipi_done\n"), DEBUG_XFERS);
700 scsipi_done(sc_xfer);
701 }
702 WDCDEBUG_PRINT(("wdcstart from wdc_atapi_done, flags 0x%x\n",
703 chp->ch_flags), DEBUG_XFERS);
704 wdcstart(wdc, chp->channel);
705 }
706
707 void
708 wdc_atapi_reset(chp, xfer)
709 struct channel_softc *chp;
710 struct wdc_xfer *xfer;
711 {
712 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
713 struct scsipi_xfer *sc_xfer = xfer->cmd;
714
715 wdccommandshort(chp, xfer->drive, ATAPI_SOFT_RESET);
716 drvp->state = 0;
717 if (wait_for_unbusy(chp, WDC_RESET_WAIT) != 0) {
718 printf("%s:%d:%d: reset failed\n",
719 chp->wdc->sc_dev.dv_xname, chp->channel,
720 xfer->drive);
721 sc_xfer->error = XS_SELTIMEOUT;
722 wdc_atapi_done(chp, xfer);
723 return;
724 }
725 wdc_atapi_done(chp, xfer);
726 return;
727 }
728