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