atapi_wdc.c revision 1.24 1 /* $NetBSD: atapi_wdc.c,v 1.24 1999/08/06 12:00:26 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 #ifndef WDCDEBUG
37 #define WDCDEBUG
38 #endif /* WDCDEBUG */
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/file.h>
44 #include <sys/stat.h>
45 #include <sys/buf.h>
46 #include <sys/malloc.h>
47 #include <sys/device.h>
48 #include <sys/syslog.h>
49 #include <sys/proc.h>
50
51 #include <vm/vm.h>
52
53 #include <machine/intr.h>
54 #include <machine/bus.h>
55
56 #ifndef __BUS_SPACE_HAS_STREAM_METHODS
57 #define bus_space_write_multi_stream_2 bus_space_write_multi_2
58 #define bus_space_write_multi_stream_4 bus_space_write_multi_4
59 #define bus_space_read_multi_stream_2 bus_space_read_multi_2
60 #define bus_space_read_multi_stream_4 bus_space_read_multi_4
61 #endif /* __BUS_SPACE_HAS_STREAM_METHODS */
62
63 #include <dev/ata/atareg.h>
64 #include <dev/ata/atavar.h>
65 #include <dev/ic/wdcreg.h>
66 #include <dev/ic/wdcvar.h>
67 #include <dev/scsipi/scsipi_all.h>
68 #include <dev/scsipi/scsipiconf.h>
69 #include <dev/scsipi/atapiconf.h>
70
71 #define DEBUG_INTR 0x01
72 #define DEBUG_XFERS 0x02
73 #define DEBUG_STATUS 0x04
74 #define DEBUG_FUNCS 0x08
75 #define DEBUG_PROBE 0x10
76 #ifdef WDCDEBUG
77 int wdcdebug_atapi_mask = 0;
78 #define WDCDEBUG_PRINT(args, level) \
79 if (wdcdebug_atapi_mask & (level)) \
80 printf args
81 #else
82 #define WDCDEBUG_PRINT(args, level)
83 #endif
84
85 #define ATAPI_DELAY 10 /* 10 ms, this is used only before sending a cmd */
86
87 void wdc_atapi_minphys __P((struct buf *bp));
88 void wdc_atapi_start __P((struct channel_softc *,struct wdc_xfer *));
89 int wdc_atapi_intr __P((struct channel_softc *, struct wdc_xfer *, int));
90 int wdc_atapi_ctrl __P((struct channel_softc *, struct wdc_xfer *, int));
91 void wdc_atapi_done __P((struct channel_softc *, struct wdc_xfer *));
92 void wdc_atapi_reset __P((struct channel_softc *, struct wdc_xfer *));
93 int wdc_atapi_send_cmd __P((struct scsipi_xfer *sc_xfer));
94
95 #define MAX_SIZE MAXPHYS
96
97 void
98 wdc_atapibus_attach(chp)
99 struct channel_softc *chp;
100 {
101 struct wdc_softc *wdc = chp->wdc;
102 int channel = chp->channel;
103 struct ata_atapi_attach aa_link;
104
105 /*
106 * Fill in the adapter.
107 */
108 wdc->sc_atapi_adapter.scsipi_cmd = wdc_atapi_send_cmd;
109 wdc->sc_atapi_adapter.scsipi_minphys = wdc_atapi_minphys;
110
111 memset(&aa_link, 0, sizeof(struct ata_atapi_attach));
112 aa_link.aa_type = T_ATAPI;
113 aa_link.aa_channel = channel;
114 aa_link.aa_openings = 1;
115 aa_link.aa_drv_data = chp->ch_drive; /* pass the whole array */
116 aa_link.aa_bus_private = &wdc->sc_atapi_adapter;
117 (void)config_found(&wdc->sc_dev, (void *)&aa_link, atapi_print);
118 }
119
120 void
121 wdc_atapi_minphys (struct buf *bp)
122 {
123 if(bp->b_bcount > MAX_SIZE)
124 bp->b_bcount = MAX_SIZE;
125 minphys(bp);
126 }
127
128 int
129 wdc_atapi_get_params(ab_link, drive, flags, id)
130 struct scsipi_link *ab_link;
131 u_int8_t drive;
132 int flags;
133 struct ataparams *id;
134 {
135 struct wdc_softc *wdc = (void*)ab_link->adapter_softc;
136 struct channel_softc *chp =
137 wdc->channels[ab_link->scsipi_atapi.channel];
138 struct wdc_command wdc_c;
139
140 /* if no ATAPI device detected at wdc attach time, skip */
141 /*
142 * XXX this will break scsireprobe if this is of any interest for
143 * ATAPI devices one day.
144 */
145 if ((chp->ch_drive[drive].drive_flags & DRIVE_ATAPI) == 0) {
146 WDCDEBUG_PRINT(("wdc_atapi_get_params: drive %d not present\n",
147 drive), DEBUG_PROBE);
148 return -1;
149 }
150 memset(&wdc_c, 0, sizeof(struct wdc_command));
151 wdc_c.r_command = ATAPI_SOFT_RESET;
152 wdc_c.r_st_bmask = 0;
153 wdc_c.r_st_pmask = 0;
154 wdc_c.flags = AT_POLL;
155 wdc_c.timeout = WDC_RESET_WAIT;
156 if (wdc_exec_command(&chp->ch_drive[drive], &wdc_c) != WDC_COMPLETE) {
157 printf("wdc_atapi_get_params: ATAPI_SOFT_RESET failed for"
158 " drive %s:%d:%d: driver failed\n",
159 chp->wdc->sc_dev.dv_xname, chp->channel, drive);
160 panic("wdc_atapi_get_params");
161 }
162 if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
163 WDCDEBUG_PRINT(("wdc_atapi_get_params: ATAPI_SOFT_RESET "
164 "failed for drive %s:%d:%d: error 0x%x\n",
165 chp->wdc->sc_dev.dv_xname, chp->channel, drive,
166 wdc_c.r_error), DEBUG_PROBE);
167 return -1;
168 }
169 chp->ch_drive[drive].state = 0;
170
171 bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_status);
172
173 /* Some ATAPI devices need a bit more time after software reset. */
174 delay(5000);
175 if (ata_get_params(&chp->ch_drive[drive], AT_POLL, id) != 0) {
176 WDCDEBUG_PRINT(("wdc_atapi_get_params: ATAPI_IDENTIFY_DEVICE "
177 "failed for drive %s:%d:%d: error 0x%x\n",
178 chp->wdc->sc_dev.dv_xname, chp->channel, drive,
179 wdc_c.r_error), DEBUG_PROBE);
180 return -1;
181 }
182 return COMPLETE;
183 }
184
185 int
186 wdc_atapi_send_cmd(sc_xfer)
187 struct scsipi_xfer *sc_xfer;
188 {
189 struct wdc_softc *wdc = (void*)sc_xfer->sc_link->adapter_softc;
190 struct wdc_xfer *xfer;
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 xfer->drive = drive;
206 xfer->c_flags |= C_ATAPI;
207 xfer->cmd = sc_xfer;
208 xfer->databuf = sc_xfer->data;
209 xfer->c_bcount = sc_xfer->datalen;
210 xfer->c_start = wdc_atapi_start;
211 xfer->c_intr = wdc_atapi_intr;
212 s = splbio();
213 wdc_exec_xfer(wdc->channels[channel], xfer);
214 #ifdef DIAGNOSTIC
215 if ((sc_xfer->flags & SCSI_POLL) != 0 &&
216 (sc_xfer->flags & ITSDONE) == 0)
217 panic("wdc_atapi_send_cmd: polled command not done");
218 #endif
219 ret = (sc_xfer->flags & ITSDONE) ? COMPLETE : SUCCESSFULLY_QUEUED;
220 splx(s);
221 return ret;
222 }
223
224 void
225 wdc_atapi_start(chp, xfer)
226 struct channel_softc *chp;
227 struct wdc_xfer *xfer;
228 {
229 struct scsipi_xfer *sc_xfer = xfer->cmd;
230 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
231
232 WDCDEBUG_PRINT(("wdc_atapi_start %s:%d:%d, scsi flags 0x%x \n",
233 chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive,
234 sc_xfer->flags), DEBUG_XFERS);
235 /* Adjust C_DMA, it may have changed if we are requesting sense */
236 if ((drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA)) &&
237 (sc_xfer->datalen > 0 || (xfer->c_flags & C_SENSE)))
238 xfer->c_flags |= C_DMA;
239 else
240 xfer->c_flags &= ~C_DMA;
241 /* start timeout machinery */
242 if ((sc_xfer->flags & SCSI_POLL) == 0)
243 timeout(wdctimeout, chp, sc_xfer->timeout * hz / 1000);
244 /* Do control operations specially. */
245 if (drvp->state < READY) {
246 if (drvp->state != PIOMODE) {
247 printf("%s:%d:%d: bad state %d in wdc_atapi_start\n",
248 chp->wdc->sc_dev.dv_xname, chp->channel,
249 xfer->drive, drvp->state);
250 panic("wdc_atapi_start: bad state");
251 }
252 wdc_atapi_ctrl(chp, xfer, 0);
253 return;
254 }
255 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
256 WDSD_IBM | (xfer->drive << 4));
257 if (wait_for_unbusy(chp, ATAPI_DELAY) < 0) {
258 printf("wdc_atapi_start: not ready, st = %02x\n",
259 chp->ch_status);
260 sc_xfer->error = XS_TIMEOUT;
261 wdc_atapi_reset(chp, xfer);
262 return;
263 }
264
265 /*
266 * Even with WDCS_ERR, the device should accept a command packet
267 * Limit length to what can be stuffed into the cylinder register
268 * (16 bits). Some CD-ROMs seem to interpret '0' as 65536,
269 * but not all devices do that and it's not obvious from the
270 * ATAPI spec that that behaviour should be expected. If more
271 * data is necessary, multiple data transfer phases will be done.
272 */
273
274 wdccommand(chp, xfer->drive, ATAPI_PKT_CMD,
275 sc_xfer->datalen <= 0xffff ? sc_xfer->datalen : 0xffff,
276 0, 0, 0,
277 (xfer->c_flags & C_DMA) ? ATAPI_PKT_CMD_FTRE_DMA : 0);
278
279 /*
280 * If there is no interrupt for CMD input, busy-wait for it (done in
281 * the interrupt routine. If it is a polled command, call the interrupt
282 * routine until command is done.
283 */
284 if ((sc_xfer->sc_link->scsipi_atapi.cap & ATAPI_CFG_DRQ_MASK) !=
285 ATAPI_CFG_IRQ_DRQ || (sc_xfer->flags & SCSI_POLL)) {
286 /* Wait for at last 400ns for status bit to be valid */
287 DELAY(1);
288 wdc_atapi_intr(chp, xfer, 0);
289 } else {
290 chp->ch_flags |= WDCF_IRQ_WAIT;
291 }
292 if (sc_xfer->flags & SCSI_POLL) {
293 while ((sc_xfer->flags & ITSDONE) == 0) {
294 /* Wait for at last 400ns for status bit to be valid */
295 DELAY(1);
296 wdc_atapi_intr(chp, xfer, 0);
297 }
298 }
299 }
300
301 int
302 wdc_atapi_intr(chp, xfer, irq)
303 struct channel_softc *chp;
304 struct wdc_xfer *xfer;
305 int irq;
306 {
307 struct scsipi_xfer *sc_xfer = xfer->cmd;
308 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
309 int len, phase, i, retries=0;
310 int ire, dma_err = 0;
311 int dma_flags = 0;
312 struct scsipi_generic _cmd_reqsense;
313 struct scsipi_sense *cmd_reqsense =
314 (struct scsipi_sense *)&_cmd_reqsense;
315 void *cmd;
316
317 WDCDEBUG_PRINT(("wdc_atapi_intr %s:%d:%d\n",
318 chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive), DEBUG_INTR);
319
320 /* Is it not a transfer, but a control operation? */
321 if (drvp->state < READY) {
322 printf("%s:%d:%d: bad state %d in wdc_atapi_intr\n",
323 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
324 drvp->state);
325 panic("wdc_atapi_intr: bad state\n");
326 }
327 /*
328 * If we missed an interrupt in a PIO transfer, reset and restart.
329 * Don't try to continue transfer, we may have missed cycles.
330 */
331 if ((xfer->c_flags & (C_TIMEOU | C_DMA)) == C_TIMEOU) {
332 sc_xfer->error = XS_TIMEOUT;
333 wdc_atapi_reset(chp, xfer);
334 }
335
336 /* Ack interrupt done in wait_for_unbusy */
337 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
338 WDSD_IBM | (xfer->drive << 4));
339 if (wait_for_unbusy(chp,
340 (irq == 0) ? sc_xfer->timeout : 0) != 0) {
341 if (irq && (xfer->c_flags & C_TIMEOU) == 0)
342 return 0; /* IRQ was not for us */
343 printf("%s:%d:%d: device timeout, c_bcount=%d, c_skip=%d\n",
344 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
345 xfer->c_bcount, xfer->c_skip);
346 if (xfer->c_flags & C_DMA)
347 drvp->n_dmaerrs++;
348 sc_xfer->error = XS_TIMEOUT;
349 wdc_atapi_reset(chp, xfer);
350 return 1;
351 }
352 /* If we missed an IRQ and were using DMA, flag it as a DMA error */
353 if ((xfer->c_flags & C_TIMEOU) && (xfer->c_flags & C_DMA))
354 drvp->n_dmaerrs++;
355 /*
356 * if the request sense command was aborted, report the short sense
357 * previously recorded, else continue normal processing
358 */
359
360 if ((xfer->c_flags & C_SENSE) != 0 &&
361 (chp->ch_status & WDCS_ERR) != 0 &&
362 (chp->ch_error & WDCE_ABRT) != 0) {
363 WDCDEBUG_PRINT(("wdc_atapi_intr: request_sense aborted, "
364 "calling wdc_atapi_done(), sense 0x%x\n",
365 sc_xfer->sense.atapi_sense), DEBUG_INTR);
366 wdc_atapi_done(chp, xfer);
367 return 1;
368 }
369
370 if (xfer->c_flags & C_DMA) {
371 dma_flags = ((sc_xfer->flags & SCSI_DATA_IN) ||
372 (xfer->c_flags & C_SENSE)) ? WDC_DMA_READ : 0;
373 dma_flags |= sc_xfer->flags & SCSI_POLL ? WDC_DMA_POLL : 0;
374 }
375 again:
376 len = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_cyl_lo) +
377 256 * bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_cyl_hi);
378 ire = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_ireason);
379 phase = (ire & (WDCI_CMD | WDCI_IN)) | (chp->ch_status & WDCS_DRQ);
380 WDCDEBUG_PRINT(("wdc_atapi_intr: c_bcount %d len %d st 0x%x err 0x%x "
381 "ire 0x%x :", xfer->c_bcount,
382 len, chp->ch_status, chp->ch_error, ire), DEBUG_INTR);
383
384 switch (phase) {
385 case PHASE_CMDOUT:
386 if (xfer->c_flags & C_SENSE) {
387 memset(cmd_reqsense, 0, sizeof(struct scsipi_generic));
388 cmd_reqsense->opcode = REQUEST_SENSE;
389 cmd_reqsense->length = xfer->c_bcount;
390 cmd = cmd_reqsense;
391 } else {
392 cmd = sc_xfer->cmd;
393 }
394 WDCDEBUG_PRINT(("PHASE_CMDOUT\n"), DEBUG_INTR);
395 /* Init the DMA channel if necessary */
396 if (xfer->c_flags & C_DMA) {
397 if ((*chp->wdc->dma_init)(chp->wdc->dma_arg,
398 chp->channel, xfer->drive,
399 xfer->databuf, xfer->c_bcount, dma_flags) != 0) {
400 sc_xfer->error = XS_DRIVER_STUFFUP;
401 break;
402 }
403 }
404 /* send packet command */
405 /* Commands are 12 or 16 bytes long. It's 32-bit aligned */
406 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) {
407 if (drvp->drive_flags & DRIVE_CAP32) {
408 bus_space_write_multi_4(chp->data32iot,
409 chp->data32ioh, 0,
410 (u_int32_t *)cmd,
411 sc_xfer->cmdlen >> 2);
412 } else {
413 bus_space_write_multi_2(chp->cmd_iot,
414 chp->cmd_ioh, wd_data,
415 (u_int16_t *)cmd,
416 sc_xfer->cmdlen >> 1);
417 }
418 } else {
419 if (drvp->drive_flags & DRIVE_CAP32) {
420 bus_space_write_multi_stream_4(chp->data32iot,
421 chp->data32ioh, 0,
422 (u_int32_t *)cmd,
423 sc_xfer->cmdlen >> 2);
424 } else {
425 bus_space_write_multi_stream_2(chp->cmd_iot,
426 chp->cmd_ioh, wd_data,
427 (u_int16_t *)cmd,
428 sc_xfer->cmdlen >> 1);
429 }
430 }
431 /* Start the DMA channel if necessary */
432 if (xfer->c_flags & C_DMA) {
433 (*chp->wdc->dma_start)(chp->wdc->dma_arg,
434 chp->channel, xfer->drive, dma_flags);
435 }
436
437 if ((sc_xfer->flags & SCSI_POLL) == 0) {
438 chp->ch_flags |= WDCF_IRQ_WAIT;
439 }
440 return 1;
441
442 case PHASE_DATAOUT:
443 /* write data */
444 WDCDEBUG_PRINT(("PHASE_DATAOUT\n"), DEBUG_INTR);
445 if ((sc_xfer->flags & SCSI_DATA_OUT) == 0 ||
446 (xfer->c_flags & C_DMA) != 0) {
447 printf("wdc_atapi_intr: bad data phase DATAOUT\n");
448 if (xfer->c_flags & C_DMA) {
449 (*chp->wdc->dma_finish)(chp->wdc->dma_arg,
450 chp->channel, xfer->drive, dma_flags);
451 drvp->n_dmaerrs++;
452 }
453 sc_xfer->error = XS_TIMEOUT;
454 wdc_atapi_reset(chp, xfer);
455 return 1;
456 }
457 if (xfer->c_bcount < len) {
458 printf("wdc_atapi_intr: warning: write only "
459 "%d of %d requested bytes\n", xfer->c_bcount, len);
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 (u_int16_t *)((char *)xfer->databuf +
464 xfer->c_skip),
465 xfer->c_bcount >> 1);
466 } else {
467 bus_space_write_multi_stream_2(chp->cmd_iot,
468 chp->cmd_ioh, wd_data,
469 (u_int16_t *)((char *)xfer->databuf +
470 xfer->c_skip),
471 xfer->c_bcount >> 1);
472 }
473 for (i = xfer->c_bcount; i < len; i += 2)
474 bus_space_write_2(chp->cmd_iot, chp->cmd_ioh,
475 wd_data, 0);
476 xfer->c_skip += xfer->c_bcount;
477 xfer->c_bcount = 0;
478 } else {
479 if (drvp->drive_flags & DRIVE_CAP32) {
480 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
481 bus_space_write_multi_4(chp->data32iot,
482 chp->data32ioh, 0,
483 (u_int32_t *)((char *)xfer->databuf +
484 xfer->c_skip),
485 len >> 2);
486 else
487 bus_space_write_multi_stream_4(chp->data32iot,
488 chp->data32ioh, wd_data,
489 (u_int32_t *)((char *)xfer->databuf +
490 xfer->c_skip),
491 len >> 2);
492
493 xfer->c_skip += len & 0xfffffffc;
494 xfer->c_bcount -= len & 0xfffffffc;
495 len = len & 0x03;
496 }
497 if (len > 0) {
498 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
499 bus_space_write_multi_2(chp->cmd_iot,
500 chp->cmd_ioh, wd_data,
501 (u_int16_t *)((char *)xfer->databuf +
502 xfer->c_skip),
503 len >> 1);
504 else
505 bus_space_write_multi_stream_2(chp->cmd_iot,
506 chp->cmd_ioh, wd_data,
507 (u_int16_t *)((char *)xfer->databuf +
508 xfer->c_skip),
509 len >> 1);
510 xfer->c_skip += len;
511 xfer->c_bcount -= len;
512 }
513 }
514 if ((sc_xfer->flags & SCSI_POLL) == 0) {
515 chp->ch_flags |= WDCF_IRQ_WAIT;
516 }
517 return 1;
518
519 case PHASE_DATAIN:
520 /* Read data */
521 WDCDEBUG_PRINT(("PHASE_DATAIN\n"), DEBUG_INTR);
522 if (((sc_xfer->flags & SCSI_DATA_IN) == 0 &&
523 (xfer->c_flags & C_SENSE) == 0) ||
524 (xfer->c_flags & C_DMA) != 0) {
525 printf("wdc_atapi_intr: bad data phase DATAIN\n");
526 if (xfer->c_flags & C_DMA) {
527 (*chp->wdc->dma_finish)(chp->wdc->dma_arg,
528 chp->channel, xfer->drive, dma_flags);
529 drvp->n_dmaerrs++;
530 }
531 sc_xfer->error = XS_TIMEOUT;
532 wdc_atapi_reset(chp, xfer);
533 return 1;
534 }
535 if (xfer->c_bcount < len) {
536 printf("wdc_atapi_intr: warning: reading only "
537 "%d of %d bytes\n", xfer->c_bcount, len);
538 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) {
539 bus_space_read_multi_2(chp->cmd_iot,
540 chp->cmd_ioh, wd_data,
541 (u_int16_t *)((char *)xfer->databuf +
542 xfer->c_skip),
543 xfer->c_bcount >> 1);
544 } else {
545 bus_space_read_multi_stream_2(chp->cmd_iot,
546 chp->cmd_ioh, wd_data,
547 (u_int16_t *)((char *)xfer->databuf +
548 xfer->c_skip),
549 xfer->c_bcount >> 1);
550 }
551 wdcbit_bucket(chp, len - xfer->c_bcount);
552 xfer->c_skip += xfer->c_bcount;
553 xfer->c_bcount = 0;
554 } else {
555 if (drvp->drive_flags & DRIVE_CAP32) {
556 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
557 bus_space_read_multi_4(chp->data32iot,
558 chp->data32ioh, 0,
559 (u_int32_t *)((char *)xfer->databuf +
560 xfer->c_skip),
561 len >> 2);
562 else
563 bus_space_read_multi_stream_4(chp->data32iot,
564 chp->data32ioh, wd_data,
565 (u_int32_t *)((char *)xfer->databuf +
566 xfer->c_skip),
567 len >> 2);
568
569 xfer->c_skip += len & 0xfffffffc;
570 xfer->c_bcount -= len & 0xfffffffc;
571 len = len & 0x03;
572 }
573 if (len > 0) {
574 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
575 bus_space_read_multi_2(chp->cmd_iot,
576 chp->cmd_ioh, wd_data,
577 (u_int16_t *)((char *)xfer->databuf +
578 xfer->c_skip),
579 len >> 1);
580 else
581 bus_space_read_multi_stream_2(chp->cmd_iot,
582 chp->cmd_ioh, wd_data,
583 (u_int16_t *)((char *)xfer->databuf +
584 xfer->c_skip),
585 len >> 1);
586 xfer->c_skip += len;
587 xfer->c_bcount -=len;
588 }
589 }
590 if ((sc_xfer->flags & SCSI_POLL) == 0) {
591 chp->ch_flags |= WDCF_IRQ_WAIT;
592 }
593 return 1;
594
595 case PHASE_ABORTED:
596 case PHASE_COMPLETED:
597 WDCDEBUG_PRINT(("PHASE_COMPLETED\n"), DEBUG_INTR);
598 /* turn off DMA channel */
599 if (xfer->c_flags & C_DMA) {
600 dma_err = (*chp->wdc->dma_finish)(chp->wdc->dma_arg,
601 chp->channel, xfer->drive, dma_flags);
602 if (xfer->c_flags & C_SENSE)
603 xfer->c_bcount -=
604 sizeof(sc_xfer->sense.scsi_sense);
605 else
606 xfer->c_bcount -= sc_xfer->datalen;
607 }
608 if (xfer->c_flags & C_SENSE) {
609 if ((chp->ch_status & WDCS_ERR) || dma_err < 0) {
610 /*
611 * request sense failed ! it's not suppossed
612 * to be possible
613 */
614 if (dma_err < 0)
615 drvp->n_dmaerrs++;
616 sc_xfer->error = XS_RESET;
617 wdc_atapi_reset(chp, xfer);
618 return (1);
619 } else if (xfer->c_bcount <
620 sizeof(sc_xfer->sense.scsi_sense)) {
621 /* use the sense we just read */
622 sc_xfer->error = XS_SENSE;
623 } else {
624 /*
625 * command completed, but no data was read.
626 * use the short sense we saved previsouly.
627 */
628 sc_xfer->error = XS_SHORTSENSE;
629 }
630 } else {
631 sc_xfer->resid = xfer->c_bcount;
632 if (chp->ch_status & WDCS_ERR) {
633 /* save the short sense */
634 sc_xfer->error = XS_SHORTSENSE;
635 sc_xfer->sense.atapi_sense = chp->ch_error;
636 if ((sc_xfer->sc_link->quirks &
637 ADEV_NOSENSE) == 0) {
638 /*
639 * let the driver issue a
640 * 'request sense'
641 */
642 xfer->databuf = &sc_xfer->sense;
643 xfer->c_bcount =
644 sizeof(sc_xfer->sense.scsi_sense);
645 xfer->c_skip = 0;
646 xfer->c_flags |= C_SENSE;
647 untimeout(wdctimeout, chp);
648 wdc_atapi_start(chp, xfer);
649 return 1;
650 }
651 } else if (dma_err < 0) {
652 drvp->n_dmaerrs++;
653 sc_xfer->error = XS_RESET;
654 wdc_atapi_reset(chp, xfer);
655 return (1);
656 }
657 }
658 if (xfer->c_bcount != 0) {
659 WDCDEBUG_PRINT(("wdc_atapi_intr: bcount value is "
660 "%d after io\n", xfer->c_bcount), DEBUG_XFERS);
661 }
662 #ifdef DIAGNOSTIC
663 if (xfer->c_bcount < 0) {
664 printf("wdc_atapi_intr warning: bcount value "
665 "is %d after io\n", xfer->c_bcount);
666 }
667 #endif
668 break;
669
670 default:
671 if (++retries<500) {
672 DELAY(100);
673 chp->ch_status = bus_space_read_1(chp->cmd_iot,
674 chp->cmd_ioh, wd_status);
675 chp->ch_error = bus_space_read_1(chp->cmd_iot,
676 chp->cmd_ioh, wd_error);
677 goto again;
678 }
679 printf("wdc_atapi_intr: unknown phase 0x%x\n", phase);
680 if (chp->ch_status & WDCS_ERR) {
681 sc_xfer->error = XS_SHORTSENSE;
682 sc_xfer->sense.atapi_sense = chp->ch_error;
683 } else {
684 sc_xfer->error = XS_RESET;
685 wdc_atapi_reset(chp, xfer);
686 return (1);
687 }
688 }
689 WDCDEBUG_PRINT(("wdc_atapi_intr: wdc_atapi_done() (end), error 0x%x "
690 "sense 0x%x\n", sc_xfer->error, sc_xfer->sense.atapi_sense),
691 DEBUG_INTR);
692 wdc_atapi_done(chp, xfer);
693 return (1);
694 }
695
696 int
697 wdc_atapi_ctrl(chp, xfer, irq)
698 struct channel_softc *chp;
699 struct wdc_xfer *xfer;
700 int irq;
701 {
702 struct scsipi_xfer *sc_xfer = xfer->cmd;
703 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
704 char *errstring = NULL;
705 int delay = (irq == 0) ? ATAPI_DELAY : 0;
706
707 /* Ack interrupt done in wait_for_unbusy */
708 again:
709 WDCDEBUG_PRINT(("wdc_atapi_ctrl %s:%d:%d state %d\n",
710 chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive, drvp->state),
711 DEBUG_INTR | DEBUG_FUNCS);
712 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
713 WDSD_IBM | (xfer->drive << 4));
714 switch (drvp->state) {
715 case PIOMODE:
716 piomode:
717 /* Don't try to set mode if controller can't be adjusted */
718 if ((chp->wdc->cap & WDC_CAPABILITY_MODE) == 0)
719 goto ready;
720 /* Also don't try if the drive didn't report its mode */
721 if ((drvp->drive_flags & DRIVE_MODE) == 0)
722 goto ready;;
723 wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
724 0x08 | drvp->PIO_mode, WDSF_SET_MODE);
725 drvp->state = PIOMODE_WAIT;
726 break;
727 case PIOMODE_WAIT:
728 errstring = "piomode";
729 if (wait_for_unbusy(chp, delay))
730 goto timeout;
731 if (chp->ch_status & WDCS_ERR) {
732 if (drvp->PIO_mode < 3) {
733 drvp->PIO_mode = 3;
734 goto piomode;
735 } else {
736 goto error;
737 }
738 }
739 /* fall through */
740
741 case DMAMODE:
742 if (drvp->drive_flags & DRIVE_UDMA) {
743 wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
744 0x40 | drvp->UDMA_mode, WDSF_SET_MODE);
745 } else if (drvp->drive_flags & DRIVE_DMA) {
746 wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
747 0x20 | drvp->DMA_mode, WDSF_SET_MODE);
748 } else {
749 goto ready;
750 }
751 drvp->state = DMAMODE_WAIT;
752 break;
753 case DMAMODE_WAIT:
754 errstring = "dmamode";
755 if (wait_for_unbusy(chp, delay))
756 goto timeout;
757 if (chp->ch_status & WDCS_ERR)
758 goto error;
759 /* fall through */
760
761 case READY:
762 ready:
763 drvp->state = READY;
764 xfer->c_intr = wdc_atapi_intr;
765 untimeout(wdctimeout, chp);
766 wdc_atapi_start(chp, xfer);
767 return 1;
768 }
769 if ((sc_xfer->flags & SCSI_POLL) == 0) {
770 chp->ch_flags |= WDCF_IRQ_WAIT;
771 xfer->c_intr = wdc_atapi_ctrl;
772 } else {
773 goto again;
774 }
775 return 1;
776
777 timeout:
778 if (irq && (xfer->c_flags & C_TIMEOU) == 0) {
779 return 0; /* IRQ was not for us */
780 }
781 printf("%s:%d:%d: %s timed out\n",
782 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive, errstring);
783 sc_xfer->error = XS_TIMEOUT;
784 wdc_atapi_reset(chp, xfer);
785 return 1;
786 error:
787 printf("%s:%d:%d: %s ",
788 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
789 errstring);
790 printf("error (0x%x)\n", chp->ch_error);
791 sc_xfer->error = XS_SHORTSENSE;
792 sc_xfer->sense.atapi_sense = chp->ch_error;
793 wdc_atapi_reset(chp, xfer);
794 return 1;
795 }
796
797 void
798 wdc_atapi_done(chp, xfer)
799 struct channel_softc *chp;
800 struct wdc_xfer *xfer;
801 {
802 struct scsipi_xfer *sc_xfer = xfer->cmd;
803 int need_done = xfer->c_flags & C_NEEDDONE;
804 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
805
806 WDCDEBUG_PRINT(("wdc_atapi_done %s:%d:%d: flags 0x%x\n",
807 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
808 (u_int)xfer->c_flags), DEBUG_XFERS);
809 untimeout(wdctimeout, chp);
810 /* remove this command from xfer queue */
811 wdc_free_xfer(chp, xfer);
812 sc_xfer->flags |= ITSDONE;
813 if (drvp->n_dmaerrs ||
814 (sc_xfer->error != XS_NOERROR && sc_xfer->error != XS_SENSE &&
815 sc_xfer->error != XS_SHORTSENSE)) {
816 drvp->n_dmaerrs = 0;
817 wdc_downgrade_mode(drvp);
818 }
819
820 if (need_done) {
821 WDCDEBUG_PRINT(("wdc_atapi_done: scsipi_done\n"), DEBUG_XFERS);
822 scsipi_done(sc_xfer);
823 }
824 WDCDEBUG_PRINT(("wdcstart from wdc_atapi_done, flags 0x%x\n",
825 chp->ch_flags), DEBUG_XFERS);
826 wdcstart(chp);
827 }
828
829 void
830 wdc_atapi_reset(chp, xfer)
831 struct channel_softc *chp;
832 struct wdc_xfer *xfer;
833 {
834 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
835 struct scsipi_xfer *sc_xfer = xfer->cmd;
836
837 wdccommandshort(chp, xfer->drive, ATAPI_SOFT_RESET);
838 drvp->state = 0;
839 if (wait_for_unbusy(chp, WDC_RESET_WAIT) != 0) {
840 printf("%s:%d:%d: reset failed\n",
841 chp->wdc->sc_dev.dv_xname, chp->channel,
842 xfer->drive);
843 sc_xfer->error = XS_SELTIMEOUT;
844 }
845 wdc_atapi_done(chp, xfer);
846 return;
847 }
848