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