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