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