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