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