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