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