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