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