atapi_wdc.c revision 1.11 1 /* $NetBSD: atapi_wdc.c,v 1.11 1998/12/16 13:02:04 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 scsipi_link *sc_link = sc_xfer->sc_link;
188 struct wdc_softc *wdc = (void*)sc_link->adapter_softc;
189 struct wdc_xfer *xfer;
190 struct ata_drive_datas *drvp;
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 drvp = &wdc->channels[channel]->ch_drive[drive];
206 if ((drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA)) &&
207 sc_xfer->datalen > 0)
208 xfer->c_flags |= C_DMA;
209 xfer->drive = drive;
210 xfer->c_flags |= C_ATAPI;
211 xfer->cmd = sc_xfer;
212 xfer->databuf = sc_xfer->data;
213 xfer->c_bcount = sc_xfer->datalen;
214 xfer->c_start = wdc_atapi_start;
215 xfer->c_intr = wdc_atapi_intr;
216 s = splbio();
217 wdc_exec_xfer(wdc->channels[channel], xfer);
218 #ifdef DIAGNOSTIC
219 if ((sc_xfer->flags & SCSI_POLL) != 0 &&
220 (sc_xfer->flags & ITSDONE) == 0)
221 panic("wdc_atapi_send_cmd: polled command not done");
222 #endif
223 ret = (sc_xfer->flags & ITSDONE) ? COMPLETE : SUCCESSFULLY_QUEUED;
224 splx(s);
225 return ret;
226 }
227
228 void
229 wdc_atapi_start(chp, xfer)
230 struct channel_softc *chp;
231 struct wdc_xfer *xfer;
232 {
233 struct scsipi_xfer *sc_xfer = xfer->cmd;
234 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
235
236 WDCDEBUG_PRINT(("wdc_atapi_start %s:%d:%d, scsi flags 0x%x \n",
237 chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive,
238 sc_xfer->flags), DEBUG_XFERS);
239 /* Do control operations specially. */
240 if (drvp->state < READY) {
241 if (drvp->state != PIOMODE) {
242 printf("%s:%d:%d: bad state %d in wdc_atapi_start\n",
243 chp->wdc->sc_dev.dv_xname, chp->channel,
244 xfer->drive, drvp->state);
245 panic("wdc_atapi_start: bad state");
246 }
247 wdc_atapi_ctrl(chp, xfer);
248 return;
249 }
250 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
251 WDSD_IBM | (xfer->drive << 4));
252 if (wait_for_unbusy(chp, ATAPI_DELAY) < 0) {
253 printf("wdc_atapi_start: not ready, st = %02x\n",
254 chp->ch_status);
255 sc_xfer->error = XS_TIMEOUT;
256 wdc_atapi_reset(chp, xfer);
257 return;
258 }
259
260 /*
261 * Even with WDCS_ERR, the device should accept a command packet
262 * Limit length to what can be stuffed into the cylinder register
263 * (16 bits). Some CD-ROMs seem to interpret '0' as 65536,
264 * but not all devices do that and it's not obvious from the
265 * ATAPI spec that that behaviour should be expected. If more
266 * data is necessary, multiple data transfer phases will be done.
267 */
268
269 wdccommand(chp, xfer->drive, ATAPI_PKT_CMD,
270 sc_xfer->datalen <= 0xffff ? sc_xfer->datalen : 0xffff,
271 0, 0, 0,
272 (xfer->c_flags & C_DMA) ? ATAPI_PKT_CMD_FTRE_DMA : 0);
273
274 /*
275 * If there is no interrupt for CMD input, busy-wait for it (done in
276 * the interrupt routine. If it is a polled command, call the interrupt
277 * routine until command is done.
278 */
279 if ((sc_xfer->sc_link->scsipi_atapi.cap & 0x0300) != ACAP_DRQ_INTR ||
280 sc_xfer->flags & SCSI_POLL) {
281 /* Wait for at last 400ns for status bit to be valid */
282 delay(1);
283 if (wdc_atapi_intr(chp, xfer) == 0) {
284 sc_xfer->error = XS_TIMEOUT;
285 wdc_atapi_reset(chp, xfer);
286 return;
287 }
288 }
289 if (sc_xfer->flags & SCSI_POLL) {
290 while ((sc_xfer->flags & ITSDONE) == 0) {
291 /* Wait for at last 400ns for status bit to be valid */
292 delay(1);
293 if (wdc_atapi_intr(chp, xfer) == 0) {
294 sc_xfer->error = XS_SELTIMEOUT;
295 /* do we know more ? */
296 wdc_atapi_done(chp, xfer);
297 return;
298 }
299 }
300 }
301 }
302
303 int
304 wdc_atapi_intr(chp, xfer)
305 struct channel_softc *chp;
306 struct wdc_xfer *xfer;
307 {
308 struct scsipi_xfer *sc_xfer = xfer->cmd;
309 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
310 int len, phase, i, retries=0;
311 int ire, dma_err = 0;
312 int dma_flags = 0;
313 struct scsipi_generic _cmd_reqsense;
314 struct scsipi_sense *cmd_reqsense =
315 (struct scsipi_sense *)&_cmd_reqsense;
316 void *cmd;
317
318 WDCDEBUG_PRINT(("wdc_atapi_intr %s:%d:%d\n",
319 chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive), DEBUG_INTR);
320
321 /* Is it not a transfer, but a control operation? */
322 if (drvp->state < READY) {
323 printf("%s:%d:%d: bad state %d in wdc_atapi_intr\n",
324 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
325 drvp->state);
326 panic("wdc_atapi_intr: bad state\n");
327 }
328 /* Ack interrupt done in wait_for_unbusy */
329 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
330 WDSD_IBM | (xfer->drive << 4));
331 if (wait_for_unbusy(chp, sc_xfer->timeout) != 0) {
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 /*
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 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");
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 xfer->databuf + xfer->c_skip,
451 xfer->c_bcount >> 1);
452 } else {
453 bus_space_write_multi_stream_2(chp->cmd_iot,
454 chp->cmd_ioh, wd_data,
455 xfer->databuf + xfer->c_skip,
456 xfer->c_bcount >> 1);
457 }
458 for (i = xfer->c_bcount; i < len; i += 2)
459 bus_space_write_2(chp->cmd_iot, chp->cmd_ioh,
460 wd_data, 0);
461 xfer->c_skip += xfer->c_bcount;
462 xfer->c_bcount = 0;
463 } else {
464 if (drvp->drive_flags & DRIVE_CAP32) {
465 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
466 bus_space_write_multi_4(chp->data32iot,
467 chp->data32ioh, 0,
468 xfer->databuf + xfer->c_skip, len >> 2);
469 else
470 bus_space_write_multi_stream_4(chp->cmd_iot,
471 chp->cmd_ioh, wd_data,
472 xfer->databuf + xfer->c_skip, len >> 2);
473
474 xfer->c_skip += len & 0xfffffffc;
475 xfer->c_bcount -= len & 0xfffffffc;
476 len = len & 0x03;
477 }
478 if (len > 0) {
479 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
480 bus_space_write_multi_2(chp->cmd_iot,
481 chp->cmd_ioh, wd_data,
482 xfer->databuf + xfer->c_skip, len >> 1);
483 else
484 bus_space_write_multi_stream_2(chp->cmd_iot,
485 chp->cmd_ioh, wd_data,
486 xfer->databuf + xfer->c_skip, len >> 1);
487 xfer->c_skip += len;
488 xfer->c_bcount -= len;
489 }
490 }
491 if ((sc_xfer->flags & SCSI_POLL) == 0) {
492 chp->ch_flags |= WDCF_IRQ_WAIT;
493 timeout(wdctimeout, chp, sc_xfer->timeout * hz / 1000);
494 }
495 return 1;
496
497 case PHASE_DATAIN:
498 /* Read data */
499 WDCDEBUG_PRINT(("PHASE_DATAIN\n"), DEBUG_INTR);
500 if (((sc_xfer->flags & SCSI_DATA_IN) == 0 &&
501 (xfer->c_flags & C_SENSE) == 0) ||
502 (xfer->c_flags & C_DMA) != 0) {
503 printf("wdc_atapi_intr: bad data phase DATAIN");
504 if (xfer->c_flags & C_DMA) {
505 (*chp->wdc->dma_finish)(chp->wdc->dma_arg,
506 chp->channel, xfer->drive, dma_flags);
507 drvp->n_dmaerrs++;
508 }
509 sc_xfer->error = XS_TIMEOUT;
510 wdc_atapi_reset(chp, xfer);
511 return 1;
512 }
513 if (xfer->c_bcount < len) {
514 printf("wdc_atapi_intr: warning: reading only "
515 "%d of %d bytes\n", xfer->c_bcount, len);
516 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) {
517 bus_space_read_multi_2(chp->cmd_iot,
518 chp->cmd_ioh, wd_data,
519 xfer->databuf + xfer->c_skip, xfer->c_bcount >> 1);
520 } else {
521 bus_space_read_multi_stream_2(chp->cmd_iot,
522 chp->cmd_ioh, wd_data,
523 xfer->databuf + xfer->c_skip, xfer->c_bcount >> 1);
524 }
525 wdcbit_bucket(chp, len - xfer->c_bcount);
526 xfer->c_skip += xfer->c_bcount;
527 xfer->c_bcount = 0;
528 } else {
529 if (drvp->drive_flags & DRIVE_CAP32) {
530 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
531 bus_space_read_multi_4(chp->data32iot,
532 chp->data32ioh, 0,
533 xfer->databuf + xfer->c_skip, len >> 2);
534 else
535 bus_space_read_multi_stream_4(chp->cmd_iot,
536 chp->cmd_ioh, wd_data,
537 xfer->databuf + xfer->c_skip, len >> 2);
538
539 xfer->c_skip += len & 0xfffffffc;
540 xfer->c_bcount -= len & 0xfffffffc;
541 len = len & 0x03;
542 }
543 if (len > 0) {
544 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
545 bus_space_read_multi_2(chp->cmd_iot,
546 chp->cmd_ioh, wd_data,
547 xfer->databuf + xfer->c_skip, len >> 1);
548 else
549 bus_space_read_multi_stream_2(chp->cmd_iot,
550 chp->cmd_ioh, wd_data,
551 xfer->databuf + xfer->c_skip, len >> 1);
552 xfer->c_skip += len;
553 xfer->c_bcount -=len;
554 }
555 }
556 if ((sc_xfer->flags & SCSI_POLL) == 0) {
557 chp->ch_flags |= WDCF_IRQ_WAIT;
558 timeout(wdctimeout, chp, sc_xfer->timeout * hz / 1000);
559 }
560 return 1;
561
562 case PHASE_ABORTED:
563 case PHASE_COMPLETED:
564 WDCDEBUG_PRINT(("PHASE_COMPLETED\n"), DEBUG_INTR);
565 /* turn off DMA channel */
566 if (xfer->c_flags & C_DMA) {
567 dma_err = (*chp->wdc->dma_finish)(chp->wdc->dma_arg,
568 chp->channel, xfer->drive, dma_flags);
569 xfer->c_bcount -= sc_xfer->datalen;
570 }
571 if (xfer->c_flags & C_SENSE) {
572 if ((chp->ch_status & WDCS_ERR) || dma_err < 0) {
573 /*
574 * request sense failed ! it's not suppossed
575 * to be possible
576 */
577 sc_xfer->error = XS_DRIVER_STUFFUP;
578 } else {
579 /* use the sense we just read */
580 sc_xfer->error = XS_SENSE;
581 }
582 } else {
583 if (chp->ch_status & WDCS_ERR) {
584 /* save the short sense */
585 sc_xfer->error = XS_SHORTSENSE;
586 sc_xfer->sense.atapi_sense = chp->ch_error;
587 /* let the driver issue a 'request sense' */
588 xfer->databuf = &sc_xfer->sense;
589 xfer->c_bcount =
590 sizeof(sc_xfer->sense.scsi_sense);
591 xfer->c_flags |= C_SENSE;
592 wdc_atapi_start(chp, xfer);
593 return 1;
594 } else if (dma_err < 0) {
595 drvp->n_dmaerrs++;
596 sc_xfer->error = XS_DRIVER_STUFFUP;
597 }
598 }
599 if (xfer->c_bcount != 0) {
600 WDCDEBUG_PRINT(("wdc_atapi_intr: bcount value is "
601 "%d after io\n", xfer->c_bcount), DEBUG_XFERS);
602 }
603 #ifdef DIAGNOSTIC
604 if (xfer->c_bcount < 0) {
605 printf("wdc_atapi_intr warning: bcount value "
606 "is %d after io\n", xfer->c_bcount);
607 }
608 #endif
609 break;
610
611 default:
612 if (++retries<500) {
613 DELAY(100);
614 chp->ch_status = bus_space_read_1(chp->cmd_iot,
615 chp->cmd_ioh, wd_status);
616 chp->ch_error = bus_space_read_1(chp->cmd_iot,
617 chp->cmd_ioh, wd_error);
618 goto again;
619 }
620 printf("wdc_atapi_intr: unknown phase 0x%x\n", phase);
621 if (chp->ch_status & WDCS_ERR) {
622 sc_xfer->error = XS_SHORTSENSE;
623 sc_xfer->sense.atapi_sense = chp->ch_error;
624 } else {
625 sc_xfer->error = XS_DRIVER_STUFFUP;
626 }
627 }
628 WDCDEBUG_PRINT(("wdc_atapi_intr: wdc_atapi_done() (end), error 0x%x "
629 "sense 0x%x\n", sc_xfer->error, sc_xfer->sense.atapi_sense),
630 DEBUG_INTR);
631 wdc_atapi_done(chp, xfer);
632 return (1);
633 }
634
635 int
636 wdc_atapi_ctrl(chp, xfer)
637 struct channel_softc *chp;
638 struct wdc_xfer *xfer;
639 {
640 struct scsipi_xfer *sc_xfer = xfer->cmd;
641 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
642 char *errstring = NULL;
643
644 /* Ack interrupt done in wait_for_unbusy */
645 again:
646 WDCDEBUG_PRINT(("wdc_atapi_ctrl %s:%d:%d state %d\n",
647 chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive, drvp->state),
648 DEBUG_INTR | DEBUG_FUNCS);
649 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
650 WDSD_IBM | (xfer->drive << 4));
651 switch (drvp->state) {
652 case PIOMODE:
653 /* Don't try to set mode if controller can't be adjusted */
654 if ((chp->wdc->cap & WDC_CAPABILITY_MODE) == 0)
655 goto ready;
656 /* Also don't try if the drive didn't report its mode */
657 if ((drvp->drive_flags & DRIVE_MODE) == 0)
658 goto ready;;
659 wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
660 0x08 | drvp->PIO_mode, WDSF_SET_MODE);
661 drvp->state = PIOMODE_WAIT;
662 break;
663 case PIOMODE_WAIT:
664 errstring = "piomode";
665 if (wait_for_unbusy(chp, ATAPI_DELAY))
666 goto timeout;
667 if (chp->ch_status & WDCS_ERR)
668 goto error;
669 /* fall through */
670
671 case DMAMODE:
672 if (drvp->drive_flags & DRIVE_UDMA) {
673 wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
674 0x40 | drvp->UDMA_mode, WDSF_SET_MODE);
675 } else if (drvp->drive_flags & DRIVE_DMA) {
676 wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
677 0x20 | drvp->DMA_mode, WDSF_SET_MODE);
678 } else {
679 goto ready;
680 }
681 drvp->state = DMAMODE_WAIT;
682 break;
683 case DMAMODE_WAIT:
684 errstring = "dmamode";
685 if (wait_for_unbusy(chp, ATAPI_DELAY))
686 goto timeout;
687 if (chp->ch_status & WDCS_ERR)
688 goto error;
689 /* fall through */
690
691 case READY:
692 ready:
693 drvp->state = READY;
694 xfer->c_intr = wdc_atapi_intr;
695 wdc_atapi_start(chp, xfer);
696 return 1;
697 }
698 if ((sc_xfer->flags & SCSI_POLL) == 0) {
699 chp->ch_flags |= WDCF_IRQ_WAIT;
700 xfer->c_intr = wdc_atapi_ctrl;
701 timeout(wdctimeout, chp, sc_xfer->timeout * hz / 1000);
702 } else {
703 goto again;
704 }
705 return 1;
706
707 timeout:
708 if ((xfer->c_flags & C_TIMEOU) == 0 ) {
709 return 0; /* IRQ was not for us */
710 }
711 printf("%s:%d:%d: %s timed out\n",
712 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive, errstring);
713 sc_xfer->error = XS_TIMEOUT;
714 wdc_atapi_reset(chp, xfer);
715 return 1;
716 error:
717 printf("%s:%d:%d: %s ",
718 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
719 errstring);
720 printf("error (0x%x)\n", chp->ch_error);
721 sc_xfer->error = XS_SHORTSENSE;
722 sc_xfer->sense.atapi_sense = chp->ch_error;
723 wdc_atapi_reset(chp, xfer);
724 return 1;
725 }
726
727 void
728 wdc_atapi_done(chp, xfer)
729 struct channel_softc *chp;
730 struct wdc_xfer *xfer;
731 {
732 struct scsipi_xfer *sc_xfer = xfer->cmd;
733 int need_done = xfer->c_flags & C_NEEDDONE;
734 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
735
736 WDCDEBUG_PRINT(("wdc_atapi_done %s:%d:%d: flags 0x%x\n",
737 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
738 (u_int)xfer->c_flags), DEBUG_XFERS);
739 sc_xfer->resid = xfer->c_bcount;
740 /* remove this command from xfer queue */
741 xfer->c_skip = 0;
742 wdc_free_xfer(chp, xfer);
743 sc_xfer->flags |= ITSDONE;
744 if (sc_xfer->error == XS_NOERROR ||
745 sc_xfer->error == XS_SENSE ||
746 sc_xfer->error == XS_SHORTSENSE) {
747 drvp->n_dmaerrs = 0;
748 } else {
749 wdc_downgrade_mode(drvp);
750 }
751
752 if (need_done) {
753 WDCDEBUG_PRINT(("wdc_atapi_done: scsipi_done\n"), DEBUG_XFERS);
754 scsipi_done(sc_xfer);
755 }
756 WDCDEBUG_PRINT(("wdcstart from wdc_atapi_done, flags 0x%x\n",
757 chp->ch_flags), DEBUG_XFERS);
758 wdcstart(chp);
759 }
760
761 void
762 wdc_atapi_reset(chp, xfer)
763 struct channel_softc *chp;
764 struct wdc_xfer *xfer;
765 {
766 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
767 struct scsipi_xfer *sc_xfer = xfer->cmd;
768
769 wdccommandshort(chp, xfer->drive, ATAPI_SOFT_RESET);
770 drvp->state = 0;
771 if (wait_for_unbusy(chp, WDC_RESET_WAIT) != 0) {
772 printf("%s:%d:%d: reset failed\n",
773 chp->wdc->sc_dev.dv_xname, chp->channel,
774 xfer->drive);
775 sc_xfer->error = XS_SELTIMEOUT;
776 wdc_atapi_done(chp, xfer);
777 return;
778 }
779 wdc_atapi_done(chp, xfer);
780 return;
781 }
782