atapi_wdc.c revision 1.39 1 /* $NetBSD: atapi_wdc.c,v 1.39 2001/01/22 07:00:39 mycroft 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 AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 */
34
35 #ifndef WDCDEBUG
36 #define WDCDEBUG
37 #endif /* WDCDEBUG */
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/file.h>
43 #include <sys/stat.h>
44 #include <sys/buf.h>
45 #include <sys/malloc.h>
46 #include <sys/device.h>
47 #include <sys/syslog.h>
48 #include <sys/proc.h>
49 #include <sys/dvdio.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
66 #define DEBUG_INTR 0x01
67 #define DEBUG_XFERS 0x02
68 #define DEBUG_STATUS 0x04
69 #define DEBUG_FUNCS 0x08
70 #define DEBUG_PROBE 0x10
71 #ifdef WDCDEBUG
72 int wdcdebug_atapi_mask = 0;
73 #define WDCDEBUG_PRINT(args, level) \
74 if (wdcdebug_atapi_mask & (level)) \
75 printf args
76 #else
77 #define WDCDEBUG_PRINT(args, level)
78 #endif
79
80 #define ATAPI_DELAY 10 /* 10 ms, this is used only before sending a cmd */
81
82 void wdc_atapi_minphys __P((struct buf *bp));
83 void wdc_atapi_start __P((struct channel_softc *,struct wdc_xfer *));
84 int wdc_atapi_intr __P((struct channel_softc *, struct wdc_xfer *, int));
85 void wdc_atapi_kill_xfer __P((struct channel_softc *, struct wdc_xfer *));
86 int wdc_atapi_ctrl __P((struct channel_softc *, struct wdc_xfer *, int));
87 void wdc_atapi_done __P((struct channel_softc *, struct wdc_xfer *));
88 void wdc_atapi_reset __P((struct channel_softc *, struct wdc_xfer *));
89 int wdc_atapi_get_params __P((struct scsipi_link *, u_int8_t, int,
90 struct ataparams *));
91 void wdc_atapi_probedev __P(( struct atapibus_softc *, int));
92 int wdc_atapi_send_cmd __P((struct scsipi_xfer *sc_xfer));
93
94 #define MAX_SIZE MAXPHYS
95
96 void
97 wdc_atapibus_attach(chp)
98 struct channel_softc *chp;
99 {
100 struct wdc_softc *wdc = chp->wdc;
101 int channel = chp->channel;
102 struct ata_atapi_attach aa_link;
103
104 /*
105 * Fill in the adapter.
106 */
107 wdc->sc_atapi_adapter._generic.scsipi_cmd = wdc_atapi_send_cmd;
108 wdc->sc_atapi_adapter._generic.scsipi_minphys = wdc_atapi_minphys;
109 wdc->sc_atapi_adapter.atapi_probedev = wdc_atapi_probedev;
110 wdc->sc_atapi_adapter.atapi_kill_pending = atapi_kill_pending;
111
112 memset(&aa_link, 0, sizeof(struct ata_atapi_attach));
113 aa_link.aa_type = T_ATAPI;
114 aa_link.aa_channel = channel;
115 aa_link.aa_openings = 1;
116 aa_link.aa_drv_data = chp->ch_drive; /* pass the whole array */
117 aa_link.aa_bus_private = &wdc->sc_atapi_adapter;
118 chp->atapibus = config_found(&wdc->sc_dev, (void *)&aa_link,
119 atapi_print);
120 }
121
122 void
123 wdc_atapi_minphys (struct buf *bp)
124 {
125 if(bp->b_bcount > MAX_SIZE)
126 bp->b_bcount = MAX_SIZE;
127 minphys(bp);
128 }
129
130 /*
131 * Kill off all pending xfers for a scsipi_link.
132 *
133 * Must be called at splbio().
134 */
135 void
136 atapi_kill_pending(sc_link)
137 struct scsipi_link *sc_link;
138 {
139 struct wdc_softc *wdc = (void *)sc_link->adapter_softc;
140 struct channel_softc *chp =
141 wdc->channels[sc_link->scsipi_atapi.channel];
142
143 wdc_kill_pending(chp);
144 }
145
146 void
147 wdc_atapi_kill_xfer(chp, xfer)
148 struct channel_softc *chp;
149 struct wdc_xfer *xfer;
150 {
151 struct scsipi_xfer *sc_xfer = xfer->cmd;
152
153 callout_stop(&chp->ch_callout);
154 /* remove this command from xfer queue */
155 wdc_free_xfer(chp, xfer);
156 sc_xfer->xs_status |= XS_STS_DONE;
157 sc_xfer->error = XS_DRIVER_STUFFUP;
158 scsipi_done(sc_xfer);
159 }
160
161 int
162 wdc_atapi_get_params(ab_link, drive, flags, id)
163 struct scsipi_link *ab_link;
164 u_int8_t drive;
165 int flags;
166 struct ataparams *id;
167 {
168 struct wdc_softc *wdc = (void*)ab_link->adapter_softc;
169 struct channel_softc *chp =
170 wdc->channels[ab_link->scsipi_atapi.channel];
171 struct wdc_command wdc_c;
172
173 /* if no ATAPI device detected at wdc attach time, skip */
174 /*
175 * XXX this will break scsireprobe if this is of any interest for
176 * ATAPI devices one day.
177 */
178 if ((chp->ch_drive[drive].drive_flags & DRIVE_ATAPI) == 0) {
179 WDCDEBUG_PRINT(("wdc_atapi_get_params: drive %d not present\n",
180 drive), DEBUG_PROBE);
181 return -1;
182 }
183 memset(&wdc_c, 0, sizeof(struct wdc_command));
184 wdc_c.r_command = ATAPI_SOFT_RESET;
185 wdc_c.r_st_bmask = 0;
186 wdc_c.r_st_pmask = 0;
187 wdc_c.flags = AT_POLL;
188 wdc_c.timeout = WDC_RESET_WAIT;
189 if (wdc_exec_command(&chp->ch_drive[drive], &wdc_c) != WDC_COMPLETE) {
190 printf("wdc_atapi_get_params: ATAPI_SOFT_RESET failed for"
191 " drive %s:%d:%d: driver failed\n",
192 chp->wdc->sc_dev.dv_xname, chp->channel, drive);
193 panic("wdc_atapi_get_params");
194 }
195 if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
196 WDCDEBUG_PRINT(("wdc_atapi_get_params: ATAPI_SOFT_RESET "
197 "failed for drive %s:%d:%d: error 0x%x\n",
198 chp->wdc->sc_dev.dv_xname, chp->channel, drive,
199 wdc_c.r_error), DEBUG_PROBE);
200 return -1;
201 }
202 chp->ch_drive[drive].state = 0;
203
204 bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_status);
205
206 /* Some ATAPI devices need a bit more time after software reset. */
207 delay(5000);
208 if (ata_get_params(&chp->ch_drive[drive], AT_POLL, id) != 0) {
209 WDCDEBUG_PRINT(("wdc_atapi_get_params: ATAPI_IDENTIFY_DEVICE "
210 "failed for drive %s:%d:%d: error 0x%x\n",
211 chp->wdc->sc_dev.dv_xname, chp->channel, drive,
212 wdc_c.r_error), DEBUG_PROBE);
213 return -1;
214 }
215 return COMPLETE;
216 }
217
218 void
219 wdc_atapi_probedev(atapi, target)
220 struct atapibus_softc *atapi;
221 int target;
222 {
223 struct scsipi_link *sc_link;
224 struct scsipibus_attach_args sa;
225 struct ataparams ids;
226 struct ataparams *id = &ids;
227 struct ata_drive_datas *drvp = &atapi->sc_drvs[target];
228 char serial_number[21], model[41], firmware_revision[9];
229
230 if (atapi->sc_link[target])
231 return;
232 if (wdc_atapi_get_params(atapi->adapter_link, target,
233 XS_CTL_POLL|XS_CTL_NOSLEEP, id) == COMPLETE) {
234 #ifdef ATAPI_DEBUG_PROBE
235 printf("%s drive %d: cmdsz 0x%x drqtype 0x%x\n",
236 atapi->sc_dev.dv_xname, target,
237 id->atap_config & ATAPI_CFG_CMD_MASK,
238 id->atap_config & ATAPI_CFG_DRQ_MASK);
239 #endif
240 /*
241 * Allocate a device link and try and attach
242 * a driver to this device. If we fail, free
243 * the link.
244 */
245 sc_link = malloc(sizeof(*sc_link), M_DEVBUF, M_NOWAIT);
246 if (sc_link == NULL) {
247 printf("%s: can't allocate link for drive %d\n",
248 atapi->sc_dev.dv_xname, target);
249 return;
250 }
251 /* fill in the link (IDE-specific part) */
252 *sc_link = *atapi->adapter_link;
253 if ((id->atap_config & ATAPI_CFG_CMD_MASK) == ATAPI_CFG_CMD_16)
254 sc_link->scsipi_atapi.cap |= ACAP_LEN;
255 sc_link->scsipi_atapi.cap |=
256 (id->atap_config & ATAPI_CFG_DRQ_MASK);
257 sa.sa_sc_link = sc_link;
258 sa.sa_inqbuf.type = ATAPI_CFG_TYPE(id->atap_config);
259 sa.sa_inqbuf.removable =
260 id->atap_config & ATAPI_CFG_REMOV ? T_REMOV : T_FIXED;
261 if (sa.sa_inqbuf.removable)
262 sc_link->flags |= SDEV_REMOVABLE;
263 scsipi_strvis(model, 40, id->atap_model, 40);
264 scsipi_strvis(serial_number, 20, id->atap_serial, 20);
265 scsipi_strvis(firmware_revision, 8, id->atap_revision, 8);
266 sa.sa_inqbuf.vendor = model;
267 sa.sa_inqbuf.product = serial_number;
268 sa.sa_inqbuf.revision = firmware_revision;
269 sa.sa_inqptr = NULL;
270 drvp->drv_softc = atapi_probedev(atapi, target, sc_link, &sa);
271 if (drvp->drv_softc) {
272 wdc_probe_caps(drvp);
273 return;
274 }
275 }
276 }
277
278 int
279 wdc_atapi_send_cmd(sc_xfer)
280 struct scsipi_xfer *sc_xfer;
281 {
282 struct wdc_softc *wdc = (void*)sc_xfer->sc_link->adapter_softc;
283 struct wdc_xfer *xfer;
284 int flags = sc_xfer->xs_control;
285 int channel = sc_xfer->sc_link->scsipi_atapi.channel;
286 int drive = sc_xfer->sc_link->scsipi_atapi.drive;
287 int s, ret;
288
289 WDCDEBUG_PRINT(("wdc_atapi_send_cmd %s:%d:%d\n",
290 wdc->sc_dev.dv_xname, channel, drive), DEBUG_XFERS);
291
292 if ((wdc->sc_dev.dv_flags & DVF_ACTIVE) == 0) {
293 sc_xfer->xs_status |= XS_STS_DONE;
294 sc_xfer->error = XS_DRIVER_STUFFUP;
295 scsipi_done(sc_xfer);
296 if ((sc_xfer->xs_control & XS_CTL_POLL) == 0)
297 return (SUCCESSFULLY_QUEUED);
298 else
299 return (COMPLETE);
300 }
301
302 xfer = wdc_get_xfer((flags & XS_CTL_NOSLEEP) ?
303 WDC_NOSLEEP : WDC_CANSLEEP);
304 if (xfer == NULL) {
305 return TRY_AGAIN_LATER;
306 }
307 if (sc_xfer->xs_control & XS_CTL_POLL)
308 xfer->c_flags |= C_POLL;
309 xfer->drive = drive;
310 xfer->c_flags |= C_ATAPI;
311 if (sc_xfer->cmd->opcode == GPCMD_REPORT_KEY ||
312 sc_xfer->cmd->opcode == GPCMD_SEND_KEY ||
313 sc_xfer->cmd->opcode == GPCMD_READ_DVD_STRUCTURE) {
314 /*
315 * DVD authentication commands must always be done in PIO mode.
316 */
317 xfer->c_flags |= C_FORCEPIO;
318 }
319 xfer->cmd = sc_xfer;
320 xfer->databuf = sc_xfer->data;
321 xfer->c_bcount = sc_xfer->datalen;
322 xfer->c_start = wdc_atapi_start;
323 xfer->c_intr = wdc_atapi_intr;
324 xfer->c_kill_xfer = wdc_atapi_kill_xfer;
325 s = splbio();
326 wdc_exec_xfer(wdc->channels[channel], xfer);
327 #ifdef DIAGNOSTIC
328 if ((sc_xfer->xs_control & XS_CTL_POLL) != 0 &&
329 (sc_xfer->xs_status & XS_STS_DONE) == 0)
330 panic("wdc_atapi_send_cmd: polled command not done");
331 #endif
332 ret = (sc_xfer->xs_status & XS_STS_DONE) ?
333 COMPLETE : SUCCESSFULLY_QUEUED;
334 splx(s);
335 return ret;
336 }
337
338 void
339 wdc_atapi_start(chp, xfer)
340 struct channel_softc *chp;
341 struct wdc_xfer *xfer;
342 {
343 struct scsipi_xfer *sc_xfer = xfer->cmd;
344 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
345
346 WDCDEBUG_PRINT(("wdc_atapi_start %s:%d:%d, scsi flags 0x%x \n",
347 chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive,
348 sc_xfer->xs_control), DEBUG_XFERS);
349 /* Adjust C_DMA, it may have changed if we are requesting sense */
350 if ((drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA)) &&
351 (sc_xfer->datalen > 0 || (xfer->c_flags & C_SENSE)) &&
352 !(xfer->c_flags & C_FORCEPIO)) {
353 if (drvp->n_xfers <= NXFER)
354 drvp->n_xfers++;
355 xfer->c_flags |= C_DMA;
356 } else {
357 xfer->c_flags &= ~C_DMA;
358 }
359 /* start timeout machinery */
360 if ((sc_xfer->xs_control & XS_CTL_POLL) == 0)
361 callout_reset(&chp->ch_callout, sc_xfer->timeout * hz / 1000,
362 wdctimeout, chp);
363 /* Do control operations specially. */
364 if (drvp->state < READY) {
365 if (drvp->state != RESET) {
366 printf("%s:%d:%d: bad state %d in wdc_atapi_start\n",
367 chp->wdc->sc_dev.dv_xname, chp->channel,
368 xfer->drive, drvp->state);
369 panic("wdc_atapi_start: bad state");
370 }
371 drvp->state = PIOMODE;
372 wdc_atapi_ctrl(chp, xfer, 0);
373 return;
374 }
375 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
376 WDSD_IBM | (xfer->drive << 4));
377 if (wait_for_unbusy(chp, ATAPI_DELAY) < 0) {
378 printf("wdc_atapi_start: not ready, st = %02x\n",
379 chp->ch_status);
380 sc_xfer->error = XS_TIMEOUT;
381 wdc_atapi_reset(chp, xfer);
382 return;
383 }
384
385 /*
386 * Even with WDCS_ERR, the device should accept a command packet
387 * Limit length to what can be stuffed into the cylinder register
388 * (16 bits). Some CD-ROMs seem to interpret '0' as 65536,
389 * but not all devices do that and it's not obvious from the
390 * ATAPI spec that that behaviour should be expected. If more
391 * data is necessary, multiple data transfer phases will be done.
392 */
393
394 wdccommand(chp, xfer->drive, ATAPI_PKT_CMD,
395 xfer->c_bcount <= 0xffff ? xfer->c_bcount : 0xffff,
396 0, 0, 0,
397 (xfer->c_flags & C_DMA) ? ATAPI_PKT_CMD_FTRE_DMA : 0);
398
399 /*
400 * If there is no interrupt for CMD input, busy-wait for it (done in
401 * the interrupt routine. If it is a polled command, call the interrupt
402 * routine until command is done.
403 */
404 if ((sc_xfer->sc_link->scsipi_atapi.cap & ATAPI_CFG_DRQ_MASK) !=
405 ATAPI_CFG_IRQ_DRQ || (sc_xfer->xs_control & XS_CTL_POLL)) {
406 /* Wait for at last 400ns for status bit to be valid */
407 DELAY(1);
408 wdc_atapi_intr(chp, xfer, 0);
409 } else {
410 chp->ch_flags |= WDCF_IRQ_WAIT;
411 }
412 if (sc_xfer->xs_control & XS_CTL_POLL) {
413 while ((sc_xfer->xs_status & XS_STS_DONE) == 0) {
414 /* Wait for at last 400ns for status bit to be valid */
415 DELAY(1);
416 if (chp->ch_flags & WDCF_DMA_WAIT) {
417 wdc_dmawait(chp, xfer, sc_xfer->timeout);
418 chp->ch_flags &= ~WDCF_DMA_WAIT;
419 }
420 wdc_atapi_intr(chp, xfer, 0);
421 }
422 }
423 }
424
425 int
426 wdc_atapi_intr(chp, xfer, irq)
427 struct channel_softc *chp;
428 struct wdc_xfer *xfer;
429 int irq;
430 {
431 struct scsipi_xfer *sc_xfer = xfer->cmd;
432 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
433 int len, phase, i, retries=0;
434 int ire;
435 int dma_flags = 0;
436 struct scsipi_generic _cmd_reqsense;
437 struct scsipi_sense *cmd_reqsense =
438 (struct scsipi_sense *)&_cmd_reqsense;
439 void *cmd;
440
441 WDCDEBUG_PRINT(("wdc_atapi_intr %s:%d:%d\n",
442 chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive), DEBUG_INTR);
443
444 /* Is it not a transfer, but a control operation? */
445 if (drvp->state < READY) {
446 printf("%s:%d:%d: bad state %d in wdc_atapi_intr\n",
447 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
448 drvp->state);
449 panic("wdc_atapi_intr: bad state\n");
450 }
451 /*
452 * If we missed an interrupt in a PIO transfer, reset and restart.
453 * Don't try to continue transfer, we may have missed cycles.
454 */
455 if ((xfer->c_flags & (C_TIMEOU | C_DMA)) == C_TIMEOU) {
456 sc_xfer->error = XS_TIMEOUT;
457 wdc_atapi_reset(chp, xfer);
458 return 1;
459 }
460
461 /* Ack interrupt done in wait_for_unbusy */
462 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
463 WDSD_IBM | (xfer->drive << 4));
464 if (wait_for_unbusy(chp,
465 (irq == 0) ? sc_xfer->timeout : 0) != 0) {
466 if (irq && (xfer->c_flags & C_TIMEOU) == 0)
467 return 0; /* IRQ was not for us */
468 printf("%s:%d:%d: device timeout, c_bcount=%d, c_skip=%d\n",
469 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
470 xfer->c_bcount, xfer->c_skip);
471 if (xfer->c_flags & C_DMA) {
472 ata_dmaerr(drvp);
473 }
474 sc_xfer->error = XS_TIMEOUT;
475 wdc_atapi_reset(chp, xfer);
476 return 1;
477 }
478 if (chp->wdc->cap & WDC_CAPABILITY_IRQACK)
479 chp->wdc->irqack(chp);
480
481 /* If we missed an IRQ and were using DMA, flag it as a DMA error */
482 if ((xfer->c_flags & C_TIMEOU) && (xfer->c_flags & C_DMA)) {
483 ata_dmaerr(drvp);
484 }
485 /*
486 * if the request sense command was aborted, report the short sense
487 * previously recorded, else continue normal processing
488 */
489
490 if ((xfer->c_flags & C_SENSE) != 0 &&
491 (chp->ch_status & WDCS_ERR) != 0 &&
492 (chp->ch_error & WDCE_ABRT) != 0) {
493 WDCDEBUG_PRINT(("wdc_atapi_intr: request_sense aborted, "
494 "calling wdc_atapi_done(), sense 0x%x\n",
495 sc_xfer->sense.atapi_sense), DEBUG_INTR);
496 wdc_atapi_done(chp, xfer);
497 return 1;
498 }
499
500 if (xfer->c_flags & C_DMA)
501 dma_flags = ((sc_xfer->xs_control & XS_CTL_DATA_IN) ||
502 (xfer->c_flags & C_SENSE)) ? WDC_DMA_READ : 0;
503 again:
504 len = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_cyl_lo) +
505 256 * bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_cyl_hi);
506 ire = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_ireason);
507 phase = (ire & (WDCI_CMD | WDCI_IN)) | (chp->ch_status & WDCS_DRQ);
508 WDCDEBUG_PRINT(("wdc_atapi_intr: c_bcount %d len %d st 0x%x err 0x%x "
509 "ire 0x%x :", xfer->c_bcount,
510 len, chp->ch_status, chp->ch_error, ire), DEBUG_INTR);
511
512 switch (phase) {
513 case PHASE_CMDOUT:
514 if (xfer->c_flags & C_SENSE) {
515 memset(cmd_reqsense, 0, sizeof(struct scsipi_generic));
516 cmd_reqsense->opcode = REQUEST_SENSE;
517 cmd_reqsense->length = xfer->c_bcount;
518 cmd = cmd_reqsense;
519 } else {
520 cmd = sc_xfer->cmd;
521 }
522 WDCDEBUG_PRINT(("PHASE_CMDOUT\n"), DEBUG_INTR);
523 /* Init the DMA channel if necessary */
524 if (xfer->c_flags & C_DMA) {
525 if ((*chp->wdc->dma_init)(chp->wdc->dma_arg,
526 chp->channel, xfer->drive,
527 xfer->databuf, xfer->c_bcount, dma_flags) != 0) {
528 sc_xfer->error = XS_DRIVER_STUFFUP;
529 break;
530 }
531 }
532 /* send packet command */
533 /* Commands are 12 or 16 bytes long. It's 32-bit aligned */
534 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) {
535 if (drvp->drive_flags & DRIVE_CAP32) {
536 bus_space_write_multi_4(chp->data32iot,
537 chp->data32ioh, 0,
538 (u_int32_t *)cmd,
539 sc_xfer->cmdlen >> 2);
540 } else {
541 bus_space_write_multi_2(chp->cmd_iot,
542 chp->cmd_ioh, wd_data,
543 (u_int16_t *)cmd,
544 sc_xfer->cmdlen >> 1);
545 }
546 } else {
547 if (drvp->drive_flags & DRIVE_CAP32) {
548 bus_space_write_multi_stream_4(chp->data32iot,
549 chp->data32ioh, 0,
550 (u_int32_t *)cmd,
551 sc_xfer->cmdlen >> 2);
552 } else {
553 bus_space_write_multi_stream_2(chp->cmd_iot,
554 chp->cmd_ioh, wd_data,
555 (u_int16_t *)cmd,
556 sc_xfer->cmdlen >> 1);
557 }
558 }
559 /* Start the DMA channel if necessary */
560 if (xfer->c_flags & C_DMA) {
561 (*chp->wdc->dma_start)(chp->wdc->dma_arg,
562 chp->channel, xfer->drive);
563 chp->ch_flags |= WDCF_DMA_WAIT;
564 }
565
566 if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) {
567 chp->ch_flags |= WDCF_IRQ_WAIT;
568 }
569 return 1;
570
571 case PHASE_DATAOUT:
572 /* write data */
573 WDCDEBUG_PRINT(("PHASE_DATAOUT\n"), DEBUG_INTR);
574 if ((sc_xfer->xs_control & XS_CTL_DATA_OUT) == 0 ||
575 (xfer->c_flags & C_DMA) != 0) {
576 printf("wdc_atapi_intr: bad data phase DATAOUT\n");
577 if (xfer->c_flags & C_DMA) {
578 ata_dmaerr(drvp);
579 }
580 sc_xfer->error = XS_TIMEOUT;
581 wdc_atapi_reset(chp, xfer);
582 return 1;
583 }
584 if (xfer->c_bcount < len) {
585 printf("wdc_atapi_intr: warning: write only "
586 "%d of %d requested bytes\n", xfer->c_bcount, len);
587 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) {
588 bus_space_write_multi_2(chp->cmd_iot,
589 chp->cmd_ioh, wd_data,
590 (u_int16_t *)((char *)xfer->databuf +
591 xfer->c_skip),
592 xfer->c_bcount >> 1);
593 } else {
594 bus_space_write_multi_stream_2(chp->cmd_iot,
595 chp->cmd_ioh, wd_data,
596 (u_int16_t *)((char *)xfer->databuf +
597 xfer->c_skip),
598 xfer->c_bcount >> 1);
599 }
600 for (i = xfer->c_bcount; i < len; i += 2)
601 bus_space_write_2(chp->cmd_iot, chp->cmd_ioh,
602 wd_data, 0);
603 xfer->c_skip += xfer->c_bcount;
604 xfer->c_bcount = 0;
605 } else {
606 if (drvp->drive_flags & DRIVE_CAP32) {
607 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
608 bus_space_write_multi_4(chp->data32iot,
609 chp->data32ioh, 0,
610 (u_int32_t *)((char *)xfer->databuf +
611 xfer->c_skip),
612 len >> 2);
613 else
614 bus_space_write_multi_stream_4(chp->data32iot,
615 chp->data32ioh, wd_data,
616 (u_int32_t *)((char *)xfer->databuf +
617 xfer->c_skip),
618 len >> 2);
619
620 xfer->c_skip += len & 0xfffffffc;
621 xfer->c_bcount -= len & 0xfffffffc;
622 len = len & 0x03;
623 }
624 if (len > 0) {
625 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
626 bus_space_write_multi_2(chp->cmd_iot,
627 chp->cmd_ioh, wd_data,
628 (u_int16_t *)((char *)xfer->databuf +
629 xfer->c_skip),
630 len >> 1);
631 else
632 bus_space_write_multi_stream_2(chp->cmd_iot,
633 chp->cmd_ioh, wd_data,
634 (u_int16_t *)((char *)xfer->databuf +
635 xfer->c_skip),
636 len >> 1);
637 xfer->c_skip += len;
638 xfer->c_bcount -= len;
639 }
640 }
641 if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) {
642 chp->ch_flags |= WDCF_IRQ_WAIT;
643 }
644 return 1;
645
646 case PHASE_DATAIN:
647 /* Read data */
648 WDCDEBUG_PRINT(("PHASE_DATAIN\n"), DEBUG_INTR);
649 if (((sc_xfer->xs_control & XS_CTL_DATA_IN) == 0 &&
650 (xfer->c_flags & C_SENSE) == 0) ||
651 (xfer->c_flags & C_DMA) != 0) {
652 printf("wdc_atapi_intr: bad data phase DATAIN\n");
653 if (xfer->c_flags & C_DMA) {
654 ata_dmaerr(drvp);
655 }
656 sc_xfer->error = XS_TIMEOUT;
657 wdc_atapi_reset(chp, xfer);
658 return 1;
659 }
660 if (xfer->c_bcount < len) {
661 printf("wdc_atapi_intr: warning: reading only "
662 "%d of %d bytes\n", xfer->c_bcount, len);
663 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) {
664 bus_space_read_multi_2(chp->cmd_iot,
665 chp->cmd_ioh, wd_data,
666 (u_int16_t *)((char *)xfer->databuf +
667 xfer->c_skip),
668 xfer->c_bcount >> 1);
669 } else {
670 bus_space_read_multi_stream_2(chp->cmd_iot,
671 chp->cmd_ioh, wd_data,
672 (u_int16_t *)((char *)xfer->databuf +
673 xfer->c_skip),
674 xfer->c_bcount >> 1);
675 }
676 wdcbit_bucket(chp, len - xfer->c_bcount);
677 xfer->c_skip += xfer->c_bcount;
678 xfer->c_bcount = 0;
679 } else {
680 if (drvp->drive_flags & DRIVE_CAP32) {
681 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
682 bus_space_read_multi_4(chp->data32iot,
683 chp->data32ioh, 0,
684 (u_int32_t *)((char *)xfer->databuf +
685 xfer->c_skip),
686 len >> 2);
687 else
688 bus_space_read_multi_stream_4(chp->data32iot,
689 chp->data32ioh, wd_data,
690 (u_int32_t *)((char *)xfer->databuf +
691 xfer->c_skip),
692 len >> 2);
693
694 xfer->c_skip += len & 0xfffffffc;
695 xfer->c_bcount -= len & 0xfffffffc;
696 len = len & 0x03;
697 }
698 if (len > 0) {
699 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
700 bus_space_read_multi_2(chp->cmd_iot,
701 chp->cmd_ioh, wd_data,
702 (u_int16_t *)((char *)xfer->databuf +
703 xfer->c_skip),
704 len >> 1);
705 else
706 bus_space_read_multi_stream_2(chp->cmd_iot,
707 chp->cmd_ioh, wd_data,
708 (u_int16_t *)((char *)xfer->databuf +
709 xfer->c_skip),
710 len >> 1);
711 xfer->c_skip += len;
712 xfer->c_bcount -=len;
713 }
714 }
715 if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) {
716 chp->ch_flags |= WDCF_IRQ_WAIT;
717 }
718 return 1;
719
720 case PHASE_ABORTED:
721 case PHASE_COMPLETED:
722 WDCDEBUG_PRINT(("PHASE_COMPLETED\n"), DEBUG_INTR);
723 /* turn off DMA channel */
724 if (xfer->c_flags & C_DMA) {
725 if (xfer->c_flags & C_SENSE)
726 xfer->c_bcount -=
727 sizeof(sc_xfer->sense.scsi_sense);
728 else
729 xfer->c_bcount -= sc_xfer->datalen;
730 }
731 if (xfer->c_flags & C_SENSE) {
732 if ((chp->ch_status & WDCS_ERR) ||
733 (chp->wdc->dma_status &
734 (WDC_DMAST_NOIRQ | WDC_DMAST_ERR))) {
735 /*
736 * request sense failed ! it's not suppossed
737 * to be possible
738 */
739 if (xfer->c_flags & C_DMA) {
740 ata_dmaerr(drvp);
741 }
742 sc_xfer->error = XS_RESET;
743 wdc_atapi_reset(chp, xfer);
744 return (1);
745 } else if (xfer->c_bcount <
746 sizeof(sc_xfer->sense.scsi_sense)) {
747 /* use the sense we just read */
748 sc_xfer->error = XS_SENSE;
749 } else {
750 /*
751 * command completed, but no data was read.
752 * use the short sense we saved previsouly.
753 */
754 sc_xfer->error = XS_SHORTSENSE;
755 }
756 } else {
757 sc_xfer->resid = xfer->c_bcount;
758 if (chp->ch_status & WDCS_ERR) {
759 /* save the short sense */
760 sc_xfer->error = XS_SHORTSENSE;
761 sc_xfer->sense.atapi_sense = chp->ch_error;
762 if ((sc_xfer->sc_link->quirks &
763 ADEV_NOSENSE) == 0) {
764 /*
765 * let the driver issue a
766 * 'request sense'
767 */
768 xfer->databuf = &sc_xfer->sense;
769 xfer->c_bcount =
770 sizeof(sc_xfer->sense.scsi_sense);
771 xfer->c_skip = 0;
772 xfer->c_flags |= C_SENSE;
773 callout_stop(&chp->ch_callout);
774 wdc_atapi_start(chp, xfer);
775 return 1;
776 }
777 } else if (chp->wdc->dma_status &
778 (WDC_DMAST_NOIRQ | WDC_DMAST_ERR)) {
779 ata_dmaerr(drvp);
780 sc_xfer->error = XS_RESET;
781 wdc_atapi_reset(chp, xfer);
782 return (1);
783 }
784 }
785 if (xfer->c_bcount != 0) {
786 WDCDEBUG_PRINT(("wdc_atapi_intr: bcount value is "
787 "%d after io\n", xfer->c_bcount), DEBUG_XFERS);
788 }
789 #ifdef DIAGNOSTIC
790 if (xfer->c_bcount < 0) {
791 printf("wdc_atapi_intr warning: bcount value "
792 "is %d after io\n", xfer->c_bcount);
793 }
794 #endif
795 break;
796
797 default:
798 if (++retries<500) {
799 DELAY(100);
800 chp->ch_status = bus_space_read_1(chp->cmd_iot,
801 chp->cmd_ioh, wd_status);
802 chp->ch_error = bus_space_read_1(chp->cmd_iot,
803 chp->cmd_ioh, wd_error);
804 goto again;
805 }
806 printf("wdc_atapi_intr: unknown phase 0x%x\n", phase);
807 if (chp->ch_status & WDCS_ERR) {
808 sc_xfer->error = XS_SHORTSENSE;
809 sc_xfer->sense.atapi_sense = chp->ch_error;
810 } else {
811 if (xfer->c_flags & C_DMA) {
812 ata_dmaerr(drvp);
813 }
814 sc_xfer->error = XS_RESET;
815 wdc_atapi_reset(chp, xfer);
816 return (1);
817 }
818 }
819 WDCDEBUG_PRINT(("wdc_atapi_intr: wdc_atapi_done() (end), error 0x%x "
820 "sense 0x%x\n", sc_xfer->error, sc_xfer->sense.atapi_sense),
821 DEBUG_INTR);
822 wdc_atapi_done(chp, xfer);
823 return (1);
824 }
825
826 int
827 wdc_atapi_ctrl(chp, xfer, irq)
828 struct channel_softc *chp;
829 struct wdc_xfer *xfer;
830 int irq;
831 {
832 struct scsipi_xfer *sc_xfer = xfer->cmd;
833 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
834 char *errstring = NULL;
835 int delay = (irq == 0) ? ATAPI_DELAY : 0;
836
837 /* Ack interrupt done in wait_for_unbusy */
838 again:
839 WDCDEBUG_PRINT(("wdc_atapi_ctrl %s:%d:%d state %d\n",
840 chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive, drvp->state),
841 DEBUG_INTR | DEBUG_FUNCS);
842 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
843 WDSD_IBM | (xfer->drive << 4));
844 switch (drvp->state) {
845 case PIOMODE:
846 /* Don't try to set mode if controller can't be adjusted */
847 if ((chp->wdc->cap & WDC_CAPABILITY_MODE) == 0)
848 goto ready;
849 /* Also don't try if the drive didn't report its mode */
850 if ((drvp->drive_flags & DRIVE_MODE) == 0)
851 goto ready;
852 wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
853 0x08 | drvp->PIO_mode, WDSF_SET_MODE);
854 drvp->state = PIOMODE_WAIT;
855 break;
856 case PIOMODE_WAIT:
857 errstring = "piomode";
858 if (wait_for_unbusy(chp, delay))
859 goto timeout;
860 if (chp->wdc->cap & WDC_CAPABILITY_IRQACK)
861 chp->wdc->irqack(chp);
862 if (chp->ch_status & WDCS_ERR) {
863 if (chp->ch_error == WDCE_ABRT) {
864 /*
865 * some ATAPI drives rejects pio settings.
866 * all we can do here is fall back to PIO 0
867 */
868 drvp->drive_flags &= ~DRIVE_MODE;
869 drvp->drive_flags &= ~(DRIVE_DMA|DRIVE_UDMA);
870 drvp->PIO_mode = 0;
871 drvp->DMA_mode = 0;
872 printf("%s:%d:%d: pio setting rejected, "
873 "falling back to PIO mode 0\n",
874 chp->wdc->sc_dev.dv_xname,
875 chp->channel, xfer->drive);
876 chp->wdc->set_modes(chp);
877 goto ready;
878 }
879 goto error;
880 }
881 /* fall through */
882
883 case DMAMODE:
884 if (drvp->drive_flags & DRIVE_UDMA) {
885 wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
886 0x40 | drvp->UDMA_mode, WDSF_SET_MODE);
887 } else if (drvp->drive_flags & DRIVE_DMA) {
888 wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
889 0x20 | drvp->DMA_mode, WDSF_SET_MODE);
890 } else {
891 goto ready;
892 }
893 drvp->state = DMAMODE_WAIT;
894 break;
895 case DMAMODE_WAIT:
896 errstring = "dmamode";
897 if (wait_for_unbusy(chp, delay))
898 goto timeout;
899 if (chp->wdc->cap & WDC_CAPABILITY_IRQACK)
900 chp->wdc->irqack(chp);
901 if (chp->ch_status & WDCS_ERR)
902 goto error;
903 /* fall through */
904
905 case READY:
906 ready:
907 drvp->state = READY;
908 xfer->c_intr = wdc_atapi_intr;
909 callout_stop(&chp->ch_callout);
910 wdc_atapi_start(chp, xfer);
911 return 1;
912 }
913 if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) {
914 chp->ch_flags |= WDCF_IRQ_WAIT;
915 xfer->c_intr = wdc_atapi_ctrl;
916 } else {
917 goto again;
918 }
919 return 1;
920
921 timeout:
922 if (irq && (xfer->c_flags & C_TIMEOU) == 0) {
923 return 0; /* IRQ was not for us */
924 }
925 printf("%s:%d:%d: %s timed out\n",
926 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive, errstring);
927 sc_xfer->error = XS_TIMEOUT;
928 wdc_atapi_reset(chp, xfer);
929 return 1;
930 error:
931 printf("%s:%d:%d: %s ",
932 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
933 errstring);
934 printf("error (0x%x)\n", chp->ch_error);
935 sc_xfer->error = XS_SHORTSENSE;
936 sc_xfer->sense.atapi_sense = chp->ch_error;
937 wdc_atapi_reset(chp, xfer);
938 return 1;
939 }
940
941 void
942 wdc_atapi_done(chp, xfer)
943 struct channel_softc *chp;
944 struct wdc_xfer *xfer;
945 {
946 struct scsipi_xfer *sc_xfer = xfer->cmd;
947
948 WDCDEBUG_PRINT(("wdc_atapi_done %s:%d:%d: flags 0x%x\n",
949 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
950 (u_int)xfer->c_flags), DEBUG_XFERS);
951 callout_stop(&chp->ch_callout);
952 /* remove this command from xfer queue */
953 wdc_free_xfer(chp, xfer);
954 sc_xfer->xs_status |= XS_STS_DONE;
955
956 WDCDEBUG_PRINT(("wdc_atapi_done: scsipi_done\n"), DEBUG_XFERS);
957 scsipi_done(sc_xfer);
958 WDCDEBUG_PRINT(("wdcstart from wdc_atapi_done, flags 0x%x\n",
959 chp->ch_flags), DEBUG_XFERS);
960 wdcstart(chp);
961 }
962
963 void
964 wdc_atapi_reset(chp, xfer)
965 struct channel_softc *chp;
966 struct wdc_xfer *xfer;
967 {
968 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
969 struct scsipi_xfer *sc_xfer = xfer->cmd;
970
971 wdccommandshort(chp, xfer->drive, ATAPI_SOFT_RESET);
972 drvp->state = 0;
973 if (wait_for_unbusy(chp, WDC_RESET_WAIT) != 0) {
974 printf("%s:%d:%d: reset failed\n",
975 chp->wdc->sc_dev.dv_xname, chp->channel,
976 xfer->drive);
977 sc_xfer->error = XS_SELTIMEOUT;
978 }
979 wdc_atapi_done(chp, xfer);
980 return;
981 }
982