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