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