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