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