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