atapi_wdc.c revision 1.19 1 /* $NetBSD: atapi_wdc.c,v 1.19 1999/03/25 16:17:37 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 *));
90 int wdc_atapi_ctrl __P((struct channel_softc *, struct wdc_xfer *));
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 /* Do control operations specially. */
242 if (drvp->state < READY) {
243 if (drvp->state != PIOMODE) {
244 printf("%s:%d:%d: bad state %d in wdc_atapi_start\n",
245 chp->wdc->sc_dev.dv_xname, chp->channel,
246 xfer->drive, drvp->state);
247 panic("wdc_atapi_start: bad state");
248 }
249 wdc_atapi_ctrl(chp, xfer);
250 return;
251 }
252 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
253 WDSD_IBM | (xfer->drive << 4));
254 if (wait_for_unbusy(chp, ATAPI_DELAY) < 0) {
255 printf("wdc_atapi_start: not ready, st = %02x\n",
256 chp->ch_status);
257 sc_xfer->error = XS_TIMEOUT;
258 wdc_atapi_reset(chp, xfer);
259 return;
260 }
261
262 /*
263 * Even with WDCS_ERR, the device should accept a command packet
264 * Limit length to what can be stuffed into the cylinder register
265 * (16 bits). Some CD-ROMs seem to interpret '0' as 65536,
266 * but not all devices do that and it's not obvious from the
267 * ATAPI spec that that behaviour should be expected. If more
268 * data is necessary, multiple data transfer phases will be done.
269 */
270
271 wdccommand(chp, xfer->drive, ATAPI_PKT_CMD,
272 sc_xfer->datalen <= 0xffff ? sc_xfer->datalen : 0xffff,
273 0, 0, 0,
274 (xfer->c_flags & C_DMA) ? ATAPI_PKT_CMD_FTRE_DMA : 0);
275
276 /*
277 * If there is no interrupt for CMD input, busy-wait for it (done in
278 * the interrupt routine. If it is a polled command, call the interrupt
279 * routine until command is done.
280 */
281 if ((sc_xfer->sc_link->scsipi_atapi.cap & 0x0300) != ACAP_DRQ_INTR ||
282 sc_xfer->flags & SCSI_POLL) {
283 /* Wait for at last 400ns for status bit to be valid */
284 delay(1);
285 if (wdc_atapi_intr(chp, xfer) == 0) {
286 sc_xfer->error = XS_TIMEOUT;
287 wdc_atapi_reset(chp, xfer);
288 return;
289 }
290 }
291 if (sc_xfer->flags & SCSI_POLL) {
292 while ((sc_xfer->flags & ITSDONE) == 0) {
293 /* Wait for at last 400ns for status bit to be valid */
294 delay(1);
295 wdc_atapi_intr(chp, xfer);
296 }
297 }
298 }
299
300 int
301 wdc_atapi_intr(chp, xfer)
302 struct channel_softc *chp;
303 struct wdc_xfer *xfer;
304 {
305 struct scsipi_xfer *sc_xfer = xfer->cmd;
306 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
307 int len, phase, i, retries=0;
308 int ire, dma_err = 0;
309 int dma_flags = 0;
310 struct scsipi_generic _cmd_reqsense;
311 struct scsipi_sense *cmd_reqsense =
312 (struct scsipi_sense *)&_cmd_reqsense;
313 void *cmd;
314
315 WDCDEBUG_PRINT(("wdc_atapi_intr %s:%d:%d\n",
316 chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive), DEBUG_INTR);
317
318 /* Is it not a transfer, but a control operation? */
319 if (drvp->state < READY) {
320 printf("%s:%d:%d: bad state %d in wdc_atapi_intr\n",
321 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
322 drvp->state);
323 panic("wdc_atapi_intr: bad state\n");
324 }
325 /* Ack interrupt done in wait_for_unbusy */
326 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
327 WDSD_IBM | (xfer->drive << 4));
328 if (wait_for_unbusy(chp,
329 (sc_xfer->flags & SCSI_POLL) ? sc_xfer->timeout : 0) != 0) {
330 if ((sc_xfer->flags & SCSI_POLL) == 0 &&
331 (xfer->c_flags & C_TIMEOU) == 0)
332 return 0; /* IRQ was not for us */
333 printf("%s:%d:%d: device timeout, c_bcount=%d, c_skip=%d\n",
334 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
335 xfer->c_bcount, xfer->c_skip);
336 if (xfer->c_flags & C_DMA)
337 drvp->n_dmaerrs++;
338 sc_xfer->error = XS_TIMEOUT;
339 wdc_atapi_reset(chp, xfer);
340 return 1;
341 }
342 /* If we missed an IRQ and were using DMA, flag it as a DMA error */
343 if ((xfer->c_flags & C_TIMEOU) && (xfer->c_flags & C_DMA))
344 drvp->n_dmaerrs++;
345 /*
346 * if the request sense command was aborted, report the short sense
347 * previously recorded, else continue normal processing
348 */
349
350 if ((xfer->c_flags & C_SENSE) != 0 &&
351 (chp->ch_status & WDCS_ERR) != 0 &&
352 (chp->ch_error & WDCE_ABRT) != 0) {
353 WDCDEBUG_PRINT(("wdc_atapi_intr: request_sense aborted, "
354 "calling wdc_atapi_done(), sense 0x%x\n",
355 sc_xfer->sense.atapi_sense), DEBUG_INTR);
356 wdc_atapi_done(chp, xfer);
357 return 1;
358 }
359
360 if (xfer->c_flags & C_DMA) {
361 dma_flags = ((sc_xfer->flags & SCSI_DATA_IN) ||
362 (xfer->c_flags & C_SENSE)) ? WDC_DMA_READ : 0;
363 dma_flags |= sc_xfer->flags & SCSI_POLL ? WDC_DMA_POLL : 0;
364 }
365 again:
366 len = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_cyl_lo) +
367 256 * bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_cyl_hi);
368 ire = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_ireason);
369 phase = (ire & (WDCI_CMD | WDCI_IN)) | (chp->ch_status & WDCS_DRQ);
370 WDCDEBUG_PRINT(("wdc_atapi_intr: c_bcount %d len %d st 0x%x err 0x%x "
371 "ire 0x%x :", xfer->c_bcount,
372 len, chp->ch_status, chp->ch_error, ire), DEBUG_INTR);
373
374 switch (phase) {
375 case PHASE_CMDOUT:
376 if (xfer->c_flags & C_SENSE) {
377 memset(cmd_reqsense, 0, sizeof(struct scsipi_generic));
378 cmd_reqsense->opcode = REQUEST_SENSE;
379 cmd_reqsense->length = xfer->c_bcount;
380 cmd = cmd_reqsense;
381 } else {
382 cmd = sc_xfer->cmd;
383 }
384 WDCDEBUG_PRINT(("PHASE_CMDOUT\n"), DEBUG_INTR);
385 /* Init the DMA channel if necessary */
386 if (xfer->c_flags & C_DMA) {
387 if ((*chp->wdc->dma_init)(chp->wdc->dma_arg,
388 chp->channel, xfer->drive,
389 xfer->databuf, xfer->c_bcount, dma_flags) != 0) {
390 sc_xfer->error = XS_DRIVER_STUFFUP;
391 break;
392 }
393 }
394 /* send packet command */
395 /* Commands are 12 or 16 bytes long. It's 32-bit aligned */
396 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) {
397 if (drvp->drive_flags & DRIVE_CAP32) {
398 bus_space_write_multi_4(chp->data32iot,
399 chp->data32ioh, 0,
400 (u_int32_t *)cmd,
401 sc_xfer->cmdlen >> 2);
402 } else {
403 bus_space_write_multi_2(chp->cmd_iot,
404 chp->cmd_ioh, wd_data,
405 (u_int16_t *)cmd,
406 sc_xfer->cmdlen >> 1);
407 }
408 } else {
409 if (drvp->drive_flags & DRIVE_CAP32) {
410 bus_space_write_multi_stream_4(chp->data32iot,
411 chp->data32ioh, 0,
412 (u_int32_t *)cmd,
413 sc_xfer->cmdlen >> 2);
414 } else {
415 bus_space_write_multi_stream_2(chp->cmd_iot,
416 chp->cmd_ioh, wd_data,
417 (u_int16_t *)cmd,
418 sc_xfer->cmdlen >> 1);
419 }
420 }
421 /* Start the DMA channel if necessary */
422 if (xfer->c_flags & C_DMA) {
423 (*chp->wdc->dma_start)(chp->wdc->dma_arg,
424 chp->channel, xfer->drive, dma_flags);
425 }
426
427 if ((sc_xfer->flags & SCSI_POLL) == 0) {
428 chp->ch_flags |= WDCF_IRQ_WAIT;
429 timeout(wdctimeout, chp, sc_xfer->timeout * hz / 1000);
430 }
431 return 1;
432
433 case PHASE_DATAOUT:
434 /* write data */
435 WDCDEBUG_PRINT(("PHASE_DATAOUT\n"), DEBUG_INTR);
436 if ((sc_xfer->flags & SCSI_DATA_OUT) == 0 ||
437 (xfer->c_flags & C_DMA) != 0) {
438 printf("wdc_atapi_intr: bad data phase DATAOUT\n");
439 if (xfer->c_flags & C_DMA) {
440 (*chp->wdc->dma_finish)(chp->wdc->dma_arg,
441 chp->channel, xfer->drive, dma_flags);
442 drvp->n_dmaerrs++;
443 }
444 sc_xfer->error = XS_TIMEOUT;
445 wdc_atapi_reset(chp, xfer);
446 return 1;
447 }
448 if (xfer->c_bcount < len) {
449 printf("wdc_atapi_intr: warning: write only "
450 "%d of %d requested bytes\n", xfer->c_bcount, len);
451 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) {
452 bus_space_write_multi_2(chp->cmd_iot,
453 chp->cmd_ioh, wd_data,
454 (u_int16_t *)((char *)xfer->databuf +
455 xfer->c_skip),
456 xfer->c_bcount >> 1);
457 } else {
458 bus_space_write_multi_stream_2(chp->cmd_iot,
459 chp->cmd_ioh, wd_data,
460 (u_int16_t *)((char *)xfer->databuf +
461 xfer->c_skip),
462 xfer->c_bcount >> 1);
463 }
464 for (i = xfer->c_bcount; i < len; i += 2)
465 bus_space_write_2(chp->cmd_iot, chp->cmd_ioh,
466 wd_data, 0);
467 xfer->c_skip += xfer->c_bcount;
468 xfer->c_bcount = 0;
469 } else {
470 if (drvp->drive_flags & DRIVE_CAP32) {
471 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
472 bus_space_write_multi_4(chp->data32iot,
473 chp->data32ioh, 0,
474 (u_int32_t *)((char *)xfer->databuf +
475 xfer->c_skip),
476 len >> 2);
477 else
478 bus_space_write_multi_stream_4(chp->data32iot,
479 chp->data32ioh, wd_data,
480 (u_int32_t *)((char *)xfer->databuf +
481 xfer->c_skip),
482 len >> 2);
483
484 xfer->c_skip += len & 0xfffffffc;
485 xfer->c_bcount -= len & 0xfffffffc;
486 len = len & 0x03;
487 }
488 if (len > 0) {
489 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
490 bus_space_write_multi_2(chp->cmd_iot,
491 chp->cmd_ioh, wd_data,
492 (u_int16_t *)((char *)xfer->databuf +
493 xfer->c_skip),
494 len >> 1);
495 else
496 bus_space_write_multi_stream_2(chp->cmd_iot,
497 chp->cmd_ioh, wd_data,
498 (u_int16_t *)((char *)xfer->databuf +
499 xfer->c_skip),
500 len >> 1);
501 xfer->c_skip += len;
502 xfer->c_bcount -= len;
503 }
504 }
505 if ((sc_xfer->flags & SCSI_POLL) == 0) {
506 chp->ch_flags |= WDCF_IRQ_WAIT;
507 timeout(wdctimeout, chp, sc_xfer->timeout * hz / 1000);
508 }
509 return 1;
510
511 case PHASE_DATAIN:
512 /* Read data */
513 WDCDEBUG_PRINT(("PHASE_DATAIN\n"), DEBUG_INTR);
514 if (((sc_xfer->flags & SCSI_DATA_IN) == 0 &&
515 (xfer->c_flags & C_SENSE) == 0) ||
516 (xfer->c_flags & C_DMA) != 0) {
517 printf("wdc_atapi_intr: bad data phase DATAIN\n");
518 if (xfer->c_flags & C_DMA) {
519 (*chp->wdc->dma_finish)(chp->wdc->dma_arg,
520 chp->channel, xfer->drive, dma_flags);
521 drvp->n_dmaerrs++;
522 }
523 sc_xfer->error = XS_TIMEOUT;
524 wdc_atapi_reset(chp, xfer);
525 return 1;
526 }
527 if (xfer->c_bcount < len) {
528 printf("wdc_atapi_intr: warning: reading only "
529 "%d of %d bytes\n", xfer->c_bcount, len);
530 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) {
531 bus_space_read_multi_2(chp->cmd_iot,
532 chp->cmd_ioh, wd_data,
533 (u_int16_t *)((char *)xfer->databuf +
534 xfer->c_skip),
535 xfer->c_bcount >> 1);
536 } else {
537 bus_space_read_multi_stream_2(chp->cmd_iot,
538 chp->cmd_ioh, wd_data,
539 (u_int16_t *)((char *)xfer->databuf +
540 xfer->c_skip),
541 xfer->c_bcount >> 1);
542 }
543 wdcbit_bucket(chp, len - xfer->c_bcount);
544 xfer->c_skip += xfer->c_bcount;
545 xfer->c_bcount = 0;
546 } else {
547 if (drvp->drive_flags & DRIVE_CAP32) {
548 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
549 bus_space_read_multi_4(chp->data32iot,
550 chp->data32ioh, 0,
551 (u_int32_t *)((char *)xfer->databuf +
552 xfer->c_skip),
553 len >> 2);
554 else
555 bus_space_read_multi_stream_4(chp->data32iot,
556 chp->data32ioh, wd_data,
557 (u_int32_t *)((char *)xfer->databuf +
558 xfer->c_skip),
559 len >> 2);
560
561 xfer->c_skip += len & 0xfffffffc;
562 xfer->c_bcount -= len & 0xfffffffc;
563 len = len & 0x03;
564 }
565 if (len > 0) {
566 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
567 bus_space_read_multi_2(chp->cmd_iot,
568 chp->cmd_ioh, wd_data,
569 (u_int16_t *)((char *)xfer->databuf +
570 xfer->c_skip),
571 len >> 1);
572 else
573 bus_space_read_multi_stream_2(chp->cmd_iot,
574 chp->cmd_ioh, wd_data,
575 (u_int16_t *)((char *)xfer->databuf +
576 xfer->c_skip),
577 len >> 1);
578 xfer->c_skip += len;
579 xfer->c_bcount -=len;
580 }
581 }
582 if ((sc_xfer->flags & SCSI_POLL) == 0) {
583 chp->ch_flags |= WDCF_IRQ_WAIT;
584 timeout(wdctimeout, chp, sc_xfer->timeout * hz / 1000);
585 }
586 return 1;
587
588 case PHASE_ABORTED:
589 case PHASE_COMPLETED:
590 WDCDEBUG_PRINT(("PHASE_COMPLETED\n"), DEBUG_INTR);
591 /* turn off DMA channel */
592 if (xfer->c_flags & C_DMA) {
593 dma_err = (*chp->wdc->dma_finish)(chp->wdc->dma_arg,
594 chp->channel, xfer->drive, dma_flags);
595 xfer->c_bcount -= sc_xfer->datalen;
596 }
597 if (xfer->c_flags & C_SENSE) {
598 if ((chp->ch_status & WDCS_ERR) || dma_err < 0) {
599 /*
600 * request sense failed ! it's not suppossed
601 * to be possible
602 */
603 if (dma_err < 0)
604 drvp->n_dmaerrs++;
605 sc_xfer->error = XS_RESET;
606 wdc_atapi_reset(chp, xfer);
607 return (1);
608 } else if (xfer->c_bcount <
609 sizeof(sc_xfer->sense.scsi_sense)) {
610 /* use the sense we just read */
611 sc_xfer->error = XS_SENSE;
612 } else {
613 /*
614 * command completed, but no data was read.
615 * use the short sense we saved previsouly.
616 */
617 sc_xfer->error = XS_SHORTSENSE;
618 }
619 } else {
620 if (chp->ch_status & WDCS_ERR) {
621 /* save the short sense */
622 sc_xfer->error = XS_SHORTSENSE;
623 sc_xfer->sense.atapi_sense = chp->ch_error;
624 if ((sc_xfer->sc_link->quirks &
625 ADEV_NOSENSE) == 0) {
626 /*
627 * let the driver issue a
628 * 'request sense'
629 */
630 xfer->databuf = &sc_xfer->sense;
631 xfer->c_bcount =
632 sizeof(sc_xfer->sense.scsi_sense);
633 xfer->c_flags |= C_SENSE;
634 wdc_atapi_start(chp, xfer);
635 return 1;
636 }
637 } else if (dma_err < 0) {
638 drvp->n_dmaerrs++;
639 sc_xfer->error = XS_RESET;
640 wdc_atapi_reset(chp, xfer);
641 return (1);
642 }
643 }
644 if (xfer->c_bcount != 0) {
645 WDCDEBUG_PRINT(("wdc_atapi_intr: bcount value is "
646 "%d after io\n", xfer->c_bcount), DEBUG_XFERS);
647 }
648 #ifdef DIAGNOSTIC
649 if (xfer->c_bcount < 0) {
650 printf("wdc_atapi_intr warning: bcount value "
651 "is %d after io\n", xfer->c_bcount);
652 }
653 #endif
654 break;
655
656 default:
657 if (++retries<500) {
658 DELAY(100);
659 chp->ch_status = bus_space_read_1(chp->cmd_iot,
660 chp->cmd_ioh, wd_status);
661 chp->ch_error = bus_space_read_1(chp->cmd_iot,
662 chp->cmd_ioh, wd_error);
663 goto again;
664 }
665 printf("wdc_atapi_intr: unknown phase 0x%x\n", phase);
666 if (chp->ch_status & WDCS_ERR) {
667 sc_xfer->error = XS_SHORTSENSE;
668 sc_xfer->sense.atapi_sense = chp->ch_error;
669 } else {
670 sc_xfer->error = XS_RESET;
671 wdc_atapi_reset(chp, xfer);
672 return (1);
673 }
674 }
675 WDCDEBUG_PRINT(("wdc_atapi_intr: wdc_atapi_done() (end), error 0x%x "
676 "sense 0x%x\n", sc_xfer->error, sc_xfer->sense.atapi_sense),
677 DEBUG_INTR);
678 wdc_atapi_done(chp, xfer);
679 return (1);
680 }
681
682 int
683 wdc_atapi_ctrl(chp, xfer)
684 struct channel_softc *chp;
685 struct wdc_xfer *xfer;
686 {
687 struct scsipi_xfer *sc_xfer = xfer->cmd;
688 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
689 char *errstring = NULL;
690 int delay = (sc_xfer->flags & SCSI_POLL) ? ATAPI_DELAY : 0;
691
692 /* Ack interrupt done in wait_for_unbusy */
693 again:
694 WDCDEBUG_PRINT(("wdc_atapi_ctrl %s:%d:%d state %d\n",
695 chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive, drvp->state),
696 DEBUG_INTR | DEBUG_FUNCS);
697 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
698 WDSD_IBM | (xfer->drive << 4));
699 switch (drvp->state) {
700 case PIOMODE:
701 /* Don't try to set mode if controller can't be adjusted */
702 if ((chp->wdc->cap & WDC_CAPABILITY_MODE) == 0)
703 goto ready;
704 /* Also don't try if the drive didn't report its mode */
705 if ((drvp->drive_flags & DRIVE_MODE) == 0)
706 goto ready;;
707 wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
708 0x08 | drvp->PIO_mode, WDSF_SET_MODE);
709 drvp->state = PIOMODE_WAIT;
710 break;
711 case PIOMODE_WAIT:
712 errstring = "piomode";
713 if (wait_for_unbusy(chp, delay))
714 goto timeout;
715 if (chp->ch_status & WDCS_ERR)
716 goto error;
717 /* fall through */
718
719 case DMAMODE:
720 if (drvp->drive_flags & DRIVE_UDMA) {
721 wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
722 0x40 | drvp->UDMA_mode, WDSF_SET_MODE);
723 } else if (drvp->drive_flags & DRIVE_DMA) {
724 wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
725 0x20 | drvp->DMA_mode, WDSF_SET_MODE);
726 } else {
727 goto ready;
728 }
729 drvp->state = DMAMODE_WAIT;
730 break;
731 case DMAMODE_WAIT:
732 errstring = "dmamode";
733 if (wait_for_unbusy(chp, delay))
734 goto timeout;
735 if (chp->ch_status & WDCS_ERR)
736 goto error;
737 /* fall through */
738
739 case READY:
740 ready:
741 drvp->state = READY;
742 xfer->c_intr = wdc_atapi_intr;
743 wdc_atapi_start(chp, xfer);
744 return 1;
745 }
746 if ((sc_xfer->flags & SCSI_POLL) == 0) {
747 chp->ch_flags |= WDCF_IRQ_WAIT;
748 xfer->c_intr = wdc_atapi_ctrl;
749 timeout(wdctimeout, chp, sc_xfer->timeout * hz / 1000);
750 } else {
751 goto again;
752 }
753 return 1;
754
755 timeout:
756 if ((xfer->c_flags & C_TIMEOU) == 0 && delay == 0) {
757 return 0; /* IRQ was not for us */
758 }
759 printf("%s:%d:%d: %s timed out\n",
760 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive, errstring);
761 sc_xfer->error = XS_TIMEOUT;
762 wdc_atapi_reset(chp, xfer);
763 return 1;
764 error:
765 printf("%s:%d:%d: %s ",
766 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
767 errstring);
768 printf("error (0x%x)\n", chp->ch_error);
769 sc_xfer->error = XS_SHORTSENSE;
770 sc_xfer->sense.atapi_sense = chp->ch_error;
771 wdc_atapi_reset(chp, xfer);
772 return 1;
773 }
774
775 void
776 wdc_atapi_done(chp, xfer)
777 struct channel_softc *chp;
778 struct wdc_xfer *xfer;
779 {
780 struct scsipi_xfer *sc_xfer = xfer->cmd;
781 int need_done = xfer->c_flags & C_NEEDDONE;
782 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
783
784 WDCDEBUG_PRINT(("wdc_atapi_done %s:%d:%d: flags 0x%x\n",
785 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
786 (u_int)xfer->c_flags), DEBUG_XFERS);
787 sc_xfer->resid = xfer->c_bcount;
788 /* remove this command from xfer queue */
789 xfer->c_skip = 0;
790 wdc_free_xfer(chp, xfer);
791 sc_xfer->flags |= ITSDONE;
792 if (drvp->n_dmaerrs ||
793 (sc_xfer->error != XS_NOERROR && sc_xfer->error != XS_SENSE &&
794 sc_xfer->error != XS_SHORTSENSE)) {
795 drvp->n_dmaerrs = 0;
796 wdc_downgrade_mode(drvp);
797 }
798
799 if (need_done) {
800 WDCDEBUG_PRINT(("wdc_atapi_done: scsipi_done\n"), DEBUG_XFERS);
801 scsipi_done(sc_xfer);
802 }
803 WDCDEBUG_PRINT(("wdcstart from wdc_atapi_done, flags 0x%x\n",
804 chp->ch_flags), DEBUG_XFERS);
805 wdcstart(chp);
806 }
807
808 void
809 wdc_atapi_reset(chp, xfer)
810 struct channel_softc *chp;
811 struct wdc_xfer *xfer;
812 {
813 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
814 struct scsipi_xfer *sc_xfer = xfer->cmd;
815
816 wdccommandshort(chp, xfer->drive, ATAPI_SOFT_RESET);
817 drvp->state = 0;
818 if (wait_for_unbusy(chp, WDC_RESET_WAIT) != 0) {
819 printf("%s:%d:%d: reset failed\n",
820 chp->wdc->sc_dev.dv_xname, chp->channel,
821 xfer->drive);
822 sc_xfer->error = XS_SELTIMEOUT;
823 }
824 wdc_atapi_done(chp, xfer);
825 return;
826 }
827