1 /* $NetBSD: atapi_wdc.c,v 1.109 2008/11/16 19:31:21 bouyer Exp $ */
4 * Copyright (c) 1998, 2001 Manuel Bouyer.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __KERNEL_RCSID(0, "$NetBSD: atapi_wdc.c,v 1.109 2008/11/16 19:31:21 bouyer Exp $");
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/device.h>
42 #include <sys/syslog.h>
44 #include <sys/dvdio.h>
49 #ifndef __BUS_SPACE_HAS_STREAM_METHODS
50 #define bus_space_write_multi_stream_2 bus_space_write_multi_2
51 #define bus_space_write_multi_stream_4 bus_space_write_multi_4
52 #define bus_space_read_multi_stream_2 bus_space_read_multi_2
53 #define bus_space_read_multi_stream_4 bus_space_read_multi_4
54 #endif /* __BUS_SPACE_HAS_STREAM_METHODS */
56 #include <dev/ata/ataconf.h>
57 #include <dev/ata/atareg.h>
58 #include <dev/ata/atavar.h>
59 #include <dev/ic/wdcreg.h>
60 #include <dev/ic/wdcvar.h>
62 #include <dev/scsipi/scsi_all.h> /* for SCSI status */
64 #define DEBUG_INTR 0x01
65 #define DEBUG_XFERS 0x02
66 #define DEBUG_STATUS 0x04
67 #define DEBUG_FUNCS 0x08
68 #define DEBUG_PROBE 0x10
70 int wdcdebug_atapi_mask
= 0;
71 #define ATADEBUG_PRINT(args, level) \
72 if (wdcdebug_atapi_mask & (level)) \
75 #define ATADEBUG_PRINT(args, level)
78 #define ATAPI_DELAY 10 /* 10 ms, this is used only before sending a cmd */
79 #define ATAPI_MODE_DELAY 1000 /* 1s, timeout for SET_FEATYRE cmds */
81 static int wdc_atapi_get_params(struct scsipi_channel
*, int,
83 static void wdc_atapi_probe_device(struct atapibus_softc
*, int);
84 static void wdc_atapi_minphys (struct buf
*bp
);
85 static void wdc_atapi_start(struct ata_channel
*,struct ata_xfer
*);
86 static int wdc_atapi_intr(struct ata_channel
*, struct ata_xfer
*, int);
87 static void wdc_atapi_kill_xfer(struct ata_channel
*,
88 struct ata_xfer
*, int);
89 static void wdc_atapi_phase_complete(struct ata_xfer
*);
90 static void wdc_atapi_done(struct ata_channel
*, struct ata_xfer
*);
91 static void wdc_atapi_reset(struct ata_channel
*, struct ata_xfer
*);
92 static void wdc_atapi_scsipi_request(struct scsipi_channel
*,
93 scsipi_adapter_req_t
, void *);
94 static void wdc_atapi_kill_pending(struct scsipi_periph
*);
95 static void wdc_atapi_polldsc(void *arg
);
97 #define MAX_SIZE MAXPHYS
99 static const struct scsipi_bustype wdc_atapi_bustype
= {
100 SCSIPI_BUSTYPE_ATAPI
,
102 atapi_interpret_sense
,
104 wdc_atapi_kill_pending
,
108 wdc_atapibus_attach(struct atabus_softc
*ata_sc
)
110 struct ata_channel
*chp
= ata_sc
->sc_chan
;
111 struct atac_softc
*atac
= chp
->ch_atac
;
112 struct scsipi_adapter
*adapt
= &atac
->atac_atapi_adapter
._generic
;
113 struct scsipi_channel
*chan
= &chp
->ch_atapi_channel
;
116 * Fill in the scsipi_adapter.
118 adapt
->adapt_dev
= atac
->atac_dev
;
119 adapt
->adapt_nchannels
= atac
->atac_nchannels
;
120 adapt
->adapt_request
= wdc_atapi_scsipi_request
;
121 adapt
->adapt_minphys
= wdc_atapi_minphys
;
122 if (atac
->atac_cap
& ATAC_CAP_NOIRQ
)
123 adapt
->adapt_flags
|= SCSIPI_ADAPT_POLL_ONLY
;
124 atac
->atac_atapi_adapter
.atapi_probe_device
= wdc_atapi_probe_device
;
127 * Fill in the scsipi_channel.
129 memset(chan
, 0, sizeof(*chan
));
130 chan
->chan_adapter
= adapt
;
131 chan
->chan_bustype
= &wdc_atapi_bustype
;
132 chan
->chan_channel
= chp
->ch_channel
;
133 chan
->chan_flags
= SCSIPI_CHAN_OPENINGS
;
134 chan
->chan_openings
= 1;
135 chan
->chan_max_periph
= 1;
136 chan
->chan_ntargets
= 2;
137 chan
->chan_nluns
= 1;
139 chp
->atapibus
= config_found_ia(ata_sc
->sc_dev
, "atapi", chan
,
144 wdc_atapi_minphys(struct buf
*bp
)
147 if (bp
->b_bcount
> MAX_SIZE
)
148 bp
->b_bcount
= MAX_SIZE
;
153 * Kill off all pending xfers for a periph.
155 * Must be called at splbio().
158 wdc_atapi_kill_pending(struct scsipi_periph
*periph
)
160 struct atac_softc
*atac
=
161 device_private(periph
->periph_channel
->chan_adapter
->adapt_dev
);
162 struct ata_channel
*chp
=
163 atac
->atac_channels
[periph
->periph_channel
->chan_channel
];
165 ata_kill_pending(&chp
->ch_drive
[periph
->periph_target
]);
169 wdc_atapi_kill_xfer(struct ata_channel
*chp
, struct ata_xfer
*xfer
, int reason
)
171 struct scsipi_xfer
*sc_xfer
= xfer
->c_cmd
;
173 /* remove this command from xfer queue */
176 sc_xfer
->error
= XS_DRIVER_STUFFUP
;
179 sc_xfer
->error
= XS_RESET
;
182 printf("wdc_ata_bio_kill_xfer: unknown reason %d\n",
184 panic("wdc_ata_bio_kill_xfer");
186 ata_free_xfer(chp
, xfer
);
187 scsipi_done(sc_xfer
);
191 wdc_atapi_get_params(struct scsipi_channel
*chan
, int drive
,
192 struct ataparams
*id
)
194 struct wdc_softc
*wdc
= device_private(chan
->chan_adapter
->adapt_dev
);
195 struct atac_softc
*atac
= &wdc
->sc_atac
;
196 struct wdc_regs
*wdr
= &wdc
->regs
[chan
->chan_channel
];
197 struct ata_channel
*chp
= atac
->atac_channels
[chan
->chan_channel
];
198 struct ata_command ata_c
;
200 /* if no ATAPI device detected at wdc attach time, skip */
201 if ((chp
->ch_drive
[drive
].drive_flags
& DRIVE_ATAPI
) == 0) {
202 ATADEBUG_PRINT(("wdc_atapi_get_params: drive %d not present\n",
203 drive
), DEBUG_PROBE
);
207 memset(&ata_c
, 0, sizeof(struct ata_command
));
208 ata_c
.r_command
= ATAPI_SOFT_RESET
;
209 ata_c
.r_st_bmask
= 0;
210 ata_c
.r_st_pmask
= 0;
211 ata_c
.flags
= AT_WAIT
| AT_POLL
;
212 ata_c
.timeout
= WDC_RESET_WAIT
;
213 if (wdc_exec_command(&chp
->ch_drive
[drive
], &ata_c
) != ATACMD_COMPLETE
) {
214 printf("wdc_atapi_get_params: ATAPI_SOFT_RESET failed for"
215 " drive %s:%d:%d: driver failed\n",
216 device_xname(atac
->atac_dev
), chp
->ch_channel
, drive
);
217 panic("wdc_atapi_get_params");
219 if (ata_c
.flags
& (AT_ERROR
| AT_TIMEOU
| AT_DF
)) {
220 ATADEBUG_PRINT(("wdc_atapi_get_params: ATAPI_SOFT_RESET "
221 "failed for drive %s:%d:%d: error 0x%x\n",
222 device_xname(atac
->atac_dev
), chp
->ch_channel
, drive
,
223 ata_c
.r_error
), DEBUG_PROBE
);
226 chp
->ch_drive
[drive
].state
= 0;
228 (void)bus_space_read_1(wdr
->cmd_iot
, wdr
->cmd_iohs
[wd_status
], 0);
230 /* Some ATAPI devices need a bit more time after software reset. */
232 if (ata_get_params(&chp
->ch_drive
[drive
], AT_WAIT
, id
) != 0) {
233 ATADEBUG_PRINT(("wdc_atapi_get_params: ATAPI_IDENTIFY_DEVICE "
234 "failed for drive %s:%d:%d: error 0x%x\n",
235 device_xname(atac
->atac_dev
), chp
->ch_channel
, drive
,
236 ata_c
.r_error
), DEBUG_PROBE
);
243 wdc_atapi_probe_device(struct atapibus_softc
*sc
, int target
)
245 struct scsipi_channel
*chan
= sc
->sc_channel
;
246 struct scsipi_periph
*periph
;
247 struct ataparams ids
;
248 struct ataparams
*id
= &ids
;
249 struct wdc_softc
*wdc
= device_private(chan
->chan_adapter
->adapt_dev
);
250 struct atac_softc
*atac
= &wdc
->sc_atac
;
251 struct ata_channel
*chp
= atac
->atac_channels
[chan
->chan_channel
];
252 struct ata_drive_datas
*drvp
= &chp
->ch_drive
[target
];
253 struct scsipibus_attach_args sa
;
254 char serial_number
[21], model
[41], firmware_revision
[9];
257 /* skip if already attached */
258 if (scsipi_lookup_periph(chan
, target
, 0) != NULL
)
261 if (wdc_atapi_get_params(chan
, target
, id
) == 0) {
262 #ifdef ATAPI_DEBUG_PROBE
263 printf("%s drive %d: cmdsz 0x%x drqtype 0x%x\n",
264 device_xname(sc
->sc_dev
), target
,
265 id
->atap_config
& ATAPI_CFG_CMD_MASK
,
266 id
->atap_config
& ATAPI_CFG_DRQ_MASK
);
268 periph
= scsipi_alloc_periph(M_NOWAIT
);
269 if (periph
== NULL
) {
270 aprint_error_dev(sc
->sc_dev
,
271 "unable to allocate periph for drive %d\n",
275 periph
->periph_dev
= NULL
;
276 periph
->periph_channel
= chan
;
277 periph
->periph_switch
= &atapi_probe_periphsw
;
278 periph
->periph_target
= target
;
279 periph
->periph_lun
= 0;
280 periph
->periph_quirks
= PQUIRK_ONLYBIG
;
283 if (SCSIPI_DEBUG_TYPE
== SCSIPI_BUSTYPE_ATAPI
&&
284 SCSIPI_DEBUG_TARGET
== target
)
285 periph
->periph_dbflags
|= SCSIPI_DEBUG_FLAGS
;
287 periph
->periph_type
= ATAPI_CFG_TYPE(id
->atap_config
);
288 if (id
->atap_config
& ATAPI_CFG_REMOV
)
289 periph
->periph_flags
|= PERIPH_REMOVABLE
;
290 if (periph
->periph_type
== T_SEQUENTIAL
) {
292 drvp
->drive_flags
|= DRIVE_ATAPIST
;
296 sa
.sa_periph
= periph
;
297 sa
.sa_inqbuf
.type
= ATAPI_CFG_TYPE(id
->atap_config
);
298 sa
.sa_inqbuf
.removable
= id
->atap_config
& ATAPI_CFG_REMOV
?
300 scsipi_strvis((u_char
*)model
, 40, id
->atap_model
, 40);
301 scsipi_strvis((u_char
*)serial_number
, 20, id
->atap_serial
,
303 scsipi_strvis((u_char
*)firmware_revision
, 8,
304 id
->atap_revision
, 8);
305 sa
.sa_inqbuf
.vendor
= model
;
306 sa
.sa_inqbuf
.product
= serial_number
;
307 sa
.sa_inqbuf
.revision
= firmware_revision
;
310 * Determine the operating mode capabilities of the device.
312 if ((id
->atap_config
& ATAPI_CFG_CMD_MASK
) == ATAPI_CFG_CMD_16
)
313 periph
->periph_cap
|= PERIPH_CAP_CMD16
;
314 /* XXX This is gross. */
315 periph
->periph_cap
|= (id
->atap_config
& ATAPI_CFG_DRQ_MASK
);
317 drvp
->drv_softc
= atapi_probe_device(sc
, target
, periph
, &sa
);
320 ata_probe_caps(drvp
);
323 drvp
->drive_flags
&= ~DRIVE_ATAPI
;
328 drvp
->drive_flags
&= ~DRIVE_ATAPI
;
334 wdc_atapi_scsipi_request(struct scsipi_channel
*chan
, scsipi_adapter_req_t req
,
337 struct scsipi_adapter
*adapt
= chan
->chan_adapter
;
338 struct scsipi_periph
*periph
;
339 struct scsipi_xfer
*sc_xfer
;
340 struct wdc_softc
*wdc
= device_private(adapt
->adapt_dev
);
341 struct atac_softc
*atac
= &wdc
->sc_atac
;
342 struct ata_xfer
*xfer
;
343 int channel
= chan
->chan_channel
;
347 case ADAPTER_REQ_RUN_XFER
:
349 periph
= sc_xfer
->xs_periph
;
350 drive
= periph
->periph_target
;
352 ATADEBUG_PRINT(("wdc_atapi_scsipi_request %s:%d:%d\n",
353 device_xname(atac
->atac_dev
), channel
, drive
),
355 if (!device_is_active(atac
->atac_dev
)) {
356 sc_xfer
->error
= XS_DRIVER_STUFFUP
;
357 scsipi_done(sc_xfer
);
361 xfer
= ata_get_xfer(ATAXF_NOSLEEP
);
363 sc_xfer
->error
= XS_RESOURCE_SHORTAGE
;
364 scsipi_done(sc_xfer
);
368 if (sc_xfer
->xs_control
& XS_CTL_POLL
)
369 xfer
->c_flags
|= C_POLL
;
371 if ((atac
->atac_channels
[channel
]->ch_drive
[drive
].drive_flags
&
372 (DRIVE_DMA
| DRIVE_UDMA
)) && sc_xfer
->datalen
> 0)
373 xfer
->c_flags
|= C_DMA
;
375 #if NATA_DMA && NATA_PIOBM
379 if ((atac
->atac_cap
& ATAC_CAP_PIOBM
) &&
380 sc_xfer
->datalen
> 0)
381 xfer
->c_flags
|= C_PIOBM
;
383 xfer
->c_drive
= drive
;
384 xfer
->c_flags
|= C_ATAPI
;
386 if (sc_xfer
->cmd
->opcode
== GPCMD_REPORT_KEY
||
387 sc_xfer
->cmd
->opcode
== GPCMD_SEND_KEY
||
388 sc_xfer
->cmd
->opcode
== GPCMD_READ_DVD_STRUCTURE
) {
390 * DVD authentication commands must always be done in
393 xfer
->c_flags
&= ~C_DMA
;
397 * DMA normally can't deal with transfers which are not a
398 * multiple of its databus width. It's a bug to request odd
399 * length transfers for ATAPI.
401 * Some devices also can't cope with unaligned DMA xfers
402 * either. Also some devices seem to not handle DMA xfers of
405 * By enforcing at least 4 byte aligned offset and length for
406 * DMA, we might use PIO where DMA could be allowed but better
407 * safe than sorry as recent problems proved.
409 * Offending structures that are thus done by PIO instead of
410 * DMA are normally small structures since all bulkdata is
411 * aligned. But as the request may come from userland, we have
412 * to protect against it anyway.
414 * XXX check for the 32 bit wide flag?
417 if (((uintptr_t) sc_xfer
->data
) & 0x03)
418 xfer
->c_flags
&= ~C_DMA
;
419 if ((sc_xfer
->datalen
< 4) || (sc_xfer
->datalen
& 0x03))
420 xfer
->c_flags
&= ~C_DMA
;
421 #endif /* NATA_DMA */
423 xfer
->c_cmd
= sc_xfer
;
424 xfer
->c_databuf
= sc_xfer
->data
;
425 xfer
->c_bcount
= sc_xfer
->datalen
;
426 xfer
->c_start
= wdc_atapi_start
;
427 xfer
->c_intr
= wdc_atapi_intr
;
428 xfer
->c_kill_xfer
= wdc_atapi_kill_xfer
;
431 ata_exec_xfer(atac
->atac_channels
[channel
], xfer
);
433 if ((sc_xfer
->xs_control
& XS_CTL_POLL
) != 0 &&
434 (sc_xfer
->xs_status
& XS_STS_DONE
) == 0)
435 panic("wdc_atapi_scsipi_request: polled command "
442 /* Not supported, nothing to do. */
448 wdc_atapi_start(struct ata_channel
*chp
, struct ata_xfer
*xfer
)
450 struct atac_softc
*atac
= chp
->ch_atac
;
451 struct wdc_softc
*wdc
= CHAN_TO_WDC(chp
);
452 struct wdc_regs
*wdr
= &wdc
->regs
[chp
->ch_channel
];
453 struct scsipi_xfer
*sc_xfer
= xfer
->c_cmd
;
454 struct ata_drive_datas
*drvp
= &chp
->ch_drive
[xfer
->c_drive
];
455 int wait_flags
= (sc_xfer
->xs_control
& XS_CTL_POLL
) ? AT_POLL
: 0;
456 const char *errstring
;
458 ATADEBUG_PRINT(("wdc_atapi_start %s:%d:%d, scsi flags 0x%x \n",
459 device_xname(atac
->atac_dev
), chp
->ch_channel
, drvp
->drive
,
460 sc_xfer
->xs_control
), DEBUG_XFERS
);
462 if ((xfer
->c_flags
& C_DMA
) && (drvp
->n_xfers
<= NXFER
))
465 /* Do control operations specially. */
466 if (__predict_false(drvp
->state
< READY
)) {
467 /* If it's not a polled command, we need the kernel thread */
468 if ((sc_xfer
->xs_control
& XS_CTL_POLL
) == 0 &&
469 (chp
->ch_flags
& ATACH_TH_RUN
) == 0) {
470 chp
->ch_queue
->queue_freeze
++;
471 wakeup(&chp
->ch_thread
);
475 * disable interrupts, all commands here should be quick
476 * enouth to be able to poll, and we don't go here that often
478 bus_space_write_1(wdr
->ctl_iot
, wdr
->ctl_ioh
, wd_aux_ctlr
,
479 WDCTL_4BIT
| WDCTL_IDS
);
481 wdc
->select(chp
, xfer
->c_drive
);
482 bus_space_write_1(wdr
->cmd_iot
, wdr
->cmd_iohs
[wd_sdh
], 0,
483 WDSD_IBM
| (xfer
->c_drive
<< 4));
484 /* Don't try to set mode if controller can't be adjusted */
485 if (atac
->atac_set_modes
== NULL
)
487 /* Also don't try if the drive didn't report its mode */
488 if ((drvp
->drive_flags
& DRIVE_MODE
) == 0)
490 errstring
= "unbusy";
491 if (wdc_wait_for_unbusy(chp
, ATAPI_DELAY
, wait_flags
))
493 wdccommand(chp
, drvp
->drive
, SET_FEATURES
, 0, 0, 0,
494 0x08 | drvp
->PIO_mode
, WDSF_SET_MODE
);
495 errstring
= "piomode";
496 if (wdc_wait_for_unbusy(chp
, ATAPI_MODE_DELAY
, wait_flags
))
498 if (chp
->ch_status
& WDCS_ERR
) {
499 if (chp
->ch_error
== WDCE_ABRT
) {
501 * Some ATAPI drives reject PIO settings.
502 * Fall back to PIO mode 3 since that's the
505 printf("%s:%d:%d: PIO mode %d rejected, "
506 "falling back to PIO mode 3\n",
507 device_xname(atac
->atac_dev
),
508 chp
->ch_channel
, xfer
->c_drive
,
510 if (drvp
->PIO_mode
> 3)
517 if (drvp
->drive_flags
& DRIVE_UDMA
) {
518 wdccommand(chp
, drvp
->drive
, SET_FEATURES
, 0, 0, 0,
519 0x40 | drvp
->UDMA_mode
, WDSF_SET_MODE
);
522 if (drvp
->drive_flags
& DRIVE_DMA
) {
523 wdccommand(chp
, drvp
->drive
, SET_FEATURES
, 0, 0, 0,
524 0x20 | drvp
->DMA_mode
, WDSF_SET_MODE
);
528 errstring
= "dmamode";
529 if (wdc_wait_for_unbusy(chp
, ATAPI_MODE_DELAY
, wait_flags
))
531 if (chp
->ch_status
& WDCS_ERR
) {
532 if (chp
->ch_error
== WDCE_ABRT
) {
534 if (drvp
->drive_flags
& DRIVE_UDMA
)
540 * The drive rejected our DMA setting.
541 * Fall back to mode 1.
543 printf("%s:%d:%d: DMA mode %d rejected, "
544 "falling back to DMA mode 0\n",
545 device_xname(atac
->atac_dev
),
546 chp
->ch_channel
, xfer
->c_drive
,
548 if (drvp
->DMA_mode
> 0)
554 #endif /* NATA_DMA */
557 bus_space_write_1(wdr
->ctl_iot
, wdr
->ctl_ioh
, wd_aux_ctlr
,
559 delay(10); /* some drives need a little delay here */
561 /* start timeout machinery */
562 if ((sc_xfer
->xs_control
& XS_CTL_POLL
) == 0)
563 callout_reset(&chp
->ch_callout
, mstohz(sc_xfer
->timeout
),
567 wdc
->select(chp
, xfer
->c_drive
);
568 bus_space_write_1(wdr
->cmd_iot
, wdr
->cmd_iohs
[wd_sdh
], 0,
569 WDSD_IBM
| (xfer
->c_drive
<< 4));
570 switch (wdc_wait_for_unbusy(chp
, ATAPI_DELAY
, wait_flags
) < 0) {
574 printf("wdc_atapi_start: not ready, st = %02x\n",
576 sc_xfer
->error
= XS_TIMEOUT
;
577 wdc_atapi_reset(chp
, xfer
);
584 * Even with WDCS_ERR, the device should accept a command packet
585 * Limit length to what can be stuffed into the cylinder register
586 * (16 bits). Some CD-ROMs seem to interpret '0' as 65536,
587 * but not all devices do that and it's not obvious from the
588 * ATAPI spec that that behaviour should be expected. If more
589 * data is necessary, multiple data transfer phases will be done.
592 wdccommand(chp
, xfer
->c_drive
, ATAPI_PKT_CMD
,
593 xfer
->c_bcount
<= 0xffff ? xfer
->c_bcount
: 0xffff,
596 (xfer
->c_flags
& C_DMA
) ? ATAPI_PKT_CMD_FTRE_DMA
:
602 if (xfer
->c_flags
& C_PIOBM
) {
604 int dma_flags
= (sc_xfer
->xs_control
& XS_CTL_DATA_IN
)
606 if (xfer
->c_flags
& C_POLL
) {
607 /* XXX not supported yet --- fall back to PIO */
608 xfer
->c_flags
&= ~C_PIOBM
;
610 /* Init the DMA channel. */
611 error
= (*wdc
->dma_init
)(wdc
->dma_arg
,
612 chp
->ch_channel
, xfer
->c_drive
,
613 (char *)xfer
->c_databuf
,
615 dma_flags
| WDC_DMA_PIOBM_ATAPI
);
617 if (error
== EINVAL
) {
619 * We can't do DMA on this transfer
620 * for some reason. Fall back to
623 xfer
->c_flags
&= ~C_PIOBM
;
626 sc_xfer
->error
= XS_DRIVER_STUFFUP
;
635 * If there is no interrupt for CMD input, busy-wait for it (done in
636 * the interrupt routine. If it is a polled command, call the interrupt
637 * routine until command is done.
639 if ((sc_xfer
->xs_periph
->periph_cap
& ATAPI_CFG_DRQ_MASK
) !=
640 ATAPI_CFG_IRQ_DRQ
|| (sc_xfer
->xs_control
& XS_CTL_POLL
)) {
641 /* Wait for at last 400ns for status bit to be valid */
643 wdc_atapi_intr(chp
, xfer
, 0);
645 chp
->ch_flags
|= ATACH_IRQ_WAIT
;
647 if (sc_xfer
->xs_control
& XS_CTL_POLL
) {
649 if (chp
->ch_flags
& ATACH_DMA_WAIT
) {
650 wdc_dmawait(chp
, xfer
, sc_xfer
->timeout
);
651 chp
->ch_flags
&= ~ATACH_DMA_WAIT
;
654 while ((sc_xfer
->xs_status
& XS_STS_DONE
) == 0) {
655 /* Wait for at last 400ns for status bit to be valid */
657 wdc_atapi_intr(chp
, xfer
, 0);
662 printf("%s:%d:%d: %s timed out\n",
663 device_xname(atac
->atac_dev
), chp
->ch_channel
, xfer
->c_drive
,
665 sc_xfer
->error
= XS_TIMEOUT
;
666 bus_space_write_1(wdr
->ctl_iot
, wdr
->ctl_ioh
, wd_aux_ctlr
, WDCTL_4BIT
);
667 delay(10); /* some drives need a little delay here */
668 wdc_atapi_reset(chp
, xfer
);
671 printf("%s:%d:%d: %s ",
672 device_xname(atac
->atac_dev
), chp
->ch_channel
, xfer
->c_drive
,
674 printf("error (0x%x)\n", chp
->ch_error
);
675 sc_xfer
->error
= XS_SHORTSENSE
;
676 sc_xfer
->sense
.atapi_sense
= chp
->ch_error
;
677 bus_space_write_1(wdr
->ctl_iot
, wdr
->ctl_ioh
, wd_aux_ctlr
, WDCTL_4BIT
);
678 delay(10); /* some drives need a little delay here */
679 wdc_atapi_reset(chp
, xfer
);
684 wdc_atapi_intr(struct ata_channel
*chp
, struct ata_xfer
*xfer
, int irq
)
686 struct atac_softc
*atac
= chp
->ch_atac
;
687 struct wdc_softc
*wdc
= CHAN_TO_WDC(chp
);
688 struct wdc_regs
*wdr
= &wdc
->regs
[chp
->ch_channel
];
689 struct scsipi_xfer
*sc_xfer
= xfer
->c_cmd
;
690 struct ata_drive_datas
*drvp
= &chp
->ch_drive
[xfer
->c_drive
];
691 int len
, phase
, i
, retries
=0;
696 #if NATA_DMA || NATA_PIOBM
701 ATADEBUG_PRINT(("wdc_atapi_intr %s:%d:%d\n",
702 device_xname(atac
->atac_dev
), chp
->ch_channel
, drvp
->drive
),
705 /* Is it not a transfer, but a control operation? */
706 if (drvp
->state
< READY
) {
707 printf("%s:%d:%d: bad state %d in wdc_atapi_intr\n",
708 device_xname(atac
->atac_dev
), chp
->ch_channel
,
709 xfer
->c_drive
, drvp
->state
);
710 panic("wdc_atapi_intr: bad state");
713 * If we missed an interrupt in a PIO transfer, reset and restart.
714 * Don't try to continue transfer, we may have missed cycles.
716 if ((xfer
->c_flags
& (C_TIMEOU
| C_DMA
)) == C_TIMEOU
) {
717 sc_xfer
->error
= XS_TIMEOUT
;
718 wdc_atapi_reset(chp
, xfer
);
723 /* Transfer-done interrupt for busmastering PIO operation */
724 if ((xfer
->c_flags
& C_PIOBM
) && (chp
->ch_flags
& ATACH_PIOBM_WAIT
)) {
725 chp
->ch_flags
&= ~ATACH_PIOBM_WAIT
;
727 /* restore transfer length */
728 len
= xfer
->c_bcount
;
729 if (xfer
->c_lenoff
< 0)
730 len
+= xfer
->c_lenoff
;
732 if (sc_xfer
->xs_control
& XS_CTL_DATA_IN
)
733 goto end_piobm_datain
;
735 goto end_piobm_dataout
;
739 /* Ack interrupt done in wdc_wait_for_unbusy */
741 wdc
->select(chp
, xfer
->c_drive
);
742 bus_space_write_1(wdr
->cmd_iot
, wdr
->cmd_iohs
[wd_sdh
], 0,
743 WDSD_IBM
| (xfer
->c_drive
<< 4));
744 if (wdc_wait_for_unbusy(chp
,
745 (irq
== 0) ? sc_xfer
->timeout
: 0, AT_POLL
) == WDCWAIT_TOUT
) {
746 if (irq
&& (xfer
->c_flags
& C_TIMEOU
) == 0)
747 return 0; /* IRQ was not for us */
748 printf("%s:%d:%d: device timeout, c_bcount=%d, c_skip=%d\n",
749 device_xname(atac
->atac_dev
), chp
->ch_channel
,
750 xfer
->c_drive
, xfer
->c_bcount
, xfer
->c_skip
);
752 if (xfer
->c_flags
& C_DMA
) {
754 (xfer
->c_flags
& C_POLL
) ? AT_POLL
: 0);
757 sc_xfer
->error
= XS_TIMEOUT
;
758 wdc_atapi_reset(chp
, xfer
);
766 * If we missed an IRQ and were using DMA, flag it as a DMA error
769 if ((xfer
->c_flags
& C_TIMEOU
) && (xfer
->c_flags
& C_DMA
)) {
770 ata_dmaerr(drvp
, (xfer
->c_flags
& C_POLL
) ? AT_POLL
: 0);
771 sc_xfer
->error
= XS_RESET
;
772 wdc_atapi_reset(chp
, xfer
);
777 * if the request sense command was aborted, report the short sense
778 * previously recorded, else continue normal processing
781 #if NATA_DMA || NATA_PIOBM
782 if (xfer
->c_flags
& (C_DMA
| C_PIOBM
))
783 dma_flags
= (sc_xfer
->xs_control
& XS_CTL_DATA_IN
)
787 len
= bus_space_read_1(wdr
->cmd_iot
, wdr
->cmd_iohs
[wd_cyl_lo
], 0) +
788 256 * bus_space_read_1(wdr
->cmd_iot
, wdr
->cmd_iohs
[wd_cyl_hi
], 0);
789 ire
= bus_space_read_1(wdr
->cmd_iot
, wdr
->cmd_iohs
[wd_ireason
], 0);
790 phase
= (ire
& (WDCI_CMD
| WDCI_IN
)) | (chp
->ch_status
& WDCS_DRQ
);
791 ATADEBUG_PRINT(("wdc_atapi_intr: c_bcount %d len %d st 0x%x err 0x%x "
792 "ire 0x%x :", xfer
->c_bcount
,
793 len
, chp
->ch_status
, chp
->ch_error
, ire
), DEBUG_INTR
);
798 ATADEBUG_PRINT(("PHASE_CMDOUT\n"), DEBUG_INTR
);
800 /* Init the DMA channel if necessary */
801 if (xfer
->c_flags
& C_DMA
) {
802 error
= (*wdc
->dma_init
)(wdc
->dma_arg
,
803 chp
->ch_channel
, xfer
->c_drive
,
804 xfer
->c_databuf
, xfer
->c_bcount
, dma_flags
);
806 if (error
== EINVAL
) {
808 * We can't do DMA on this transfer
809 * for some reason. Fall back to
812 xfer
->c_flags
&= ~C_DMA
;
815 sc_xfer
->error
= XS_DRIVER_STUFFUP
;
822 /* send packet command */
823 /* Commands are 12 or 16 bytes long. It's 32-bit aligned */
824 wdc
->dataout_pio(chp
, drvp
->drive_flags
, cmd
, sc_xfer
->cmdlen
);
827 /* Start the DMA channel if necessary */
828 if (xfer
->c_flags
& C_DMA
) {
829 (*wdc
->dma_start
)(wdc
->dma_arg
,
830 chp
->ch_channel
, xfer
->c_drive
);
831 chp
->ch_flags
|= ATACH_DMA_WAIT
;
835 if ((sc_xfer
->xs_control
& XS_CTL_POLL
) == 0) {
836 chp
->ch_flags
|= ATACH_IRQ_WAIT
;
842 ATADEBUG_PRINT(("PHASE_DATAOUT\n"), DEBUG_INTR
);
844 if ((sc_xfer
->xs_control
& XS_CTL_DATA_OUT
) == 0 ||
845 (xfer
->c_flags
& C_DMA
) != 0) {
846 printf("wdc_atapi_intr: bad data phase DATAOUT\n");
847 if (xfer
->c_flags
& C_DMA
) {
849 (xfer
->c_flags
& C_POLL
) ? AT_POLL
: 0);
851 sc_xfer
->error
= XS_TIMEOUT
;
852 wdc_atapi_reset(chp
, xfer
);
856 xfer
->c_lenoff
= len
- xfer
->c_bcount
;
857 if (xfer
->c_bcount
< len
) {
858 printf("wdc_atapi_intr: warning: write only "
859 "%d of %d requested bytes\n", xfer
->c_bcount
, len
);
860 len
= xfer
->c_bcount
;
864 if (xfer
->c_flags
& C_PIOBM
) {
865 /* start the busmastering PIO */
866 (*wdc
->piobm_start
)(wdc
->dma_arg
,
867 chp
->ch_channel
, xfer
->c_drive
,
868 xfer
->c_skip
, len
, WDC_PIOBM_XFER_IRQ
);
869 chp
->ch_flags
|= ATACH_DMA_WAIT
| ATACH_IRQ_WAIT
|
874 wdc
->dataout_pio(chp
, drvp
->drive_flags
,
875 (char *)xfer
->c_databuf
+ xfer
->c_skip
, len
);
880 for (i
= xfer
->c_lenoff
; i
> 0; i
-= 2)
881 bus_space_write_2(wdr
->cmd_iot
,
882 wdr
->cmd_iohs
[wd_data
], 0, 0);
885 xfer
->c_bcount
-= len
;
886 if ((sc_xfer
->xs_control
& XS_CTL_POLL
) == 0) {
887 chp
->ch_flags
|= ATACH_IRQ_WAIT
;
893 ATADEBUG_PRINT(("PHASE_DATAIN\n"), DEBUG_INTR
);
895 if ((sc_xfer
->xs_control
& XS_CTL_DATA_IN
) == 0 ||
896 (xfer
->c_flags
& C_DMA
) != 0) {
897 printf("wdc_atapi_intr: bad data phase DATAIN\n");
898 if (xfer
->c_flags
& C_DMA
) {
900 (xfer
->c_flags
& C_POLL
) ? AT_POLL
: 0);
902 sc_xfer
->error
= XS_TIMEOUT
;
903 wdc_atapi_reset(chp
, xfer
);
907 xfer
->c_lenoff
= len
- xfer
->c_bcount
;
908 if (xfer
->c_bcount
< len
) {
909 printf("wdc_atapi_intr: warning: reading only "
910 "%d of %d bytes\n", xfer
->c_bcount
, len
);
911 len
= xfer
->c_bcount
;
915 if (xfer
->c_flags
& C_PIOBM
) {
916 /* start the busmastering PIO */
917 (*wdc
->piobm_start
)(wdc
->dma_arg
,
918 chp
->ch_channel
, xfer
->c_drive
,
919 xfer
->c_skip
, len
, WDC_PIOBM_XFER_IRQ
);
920 chp
->ch_flags
|= ATACH_DMA_WAIT
| ATACH_IRQ_WAIT
|
925 wdc
->datain_pio(chp
, drvp
->drive_flags
,
926 (char *)xfer
->c_databuf
+ xfer
->c_skip
, len
);
931 if (xfer
->c_lenoff
> 0)
932 wdcbit_bucket(chp
, len
- xfer
->c_bcount
);
935 xfer
->c_bcount
-= len
;
936 if ((sc_xfer
->xs_control
& XS_CTL_POLL
) == 0) {
937 chp
->ch_flags
|= ATACH_IRQ_WAIT
;
942 case PHASE_COMPLETED
:
943 ATADEBUG_PRINT(("PHASE_COMPLETED\n"), DEBUG_INTR
);
945 if (xfer
->c_flags
& C_DMA
) {
946 xfer
->c_bcount
-= sc_xfer
->datalen
;
949 sc_xfer
->resid
= xfer
->c_bcount
;
950 wdc_atapi_phase_complete(xfer
);
956 chp
->ch_status
= bus_space_read_1(wdr
->cmd_iot
,
957 wdr
->cmd_iohs
[wd_status
], 0);
958 chp
->ch_error
= bus_space_read_1(wdr
->cmd_iot
,
959 wdr
->cmd_iohs
[wd_error
], 0);
962 printf("wdc_atapi_intr: unknown phase 0x%x\n", phase
);
963 if (chp
->ch_status
& WDCS_ERR
) {
964 sc_xfer
->error
= XS_SHORTSENSE
;
965 sc_xfer
->sense
.atapi_sense
= chp
->ch_error
;
968 if (xfer
->c_flags
& C_DMA
) {
970 (xfer
->c_flags
& C_POLL
) ? AT_POLL
: 0);
973 sc_xfer
->error
= XS_RESET
;
974 wdc_atapi_reset(chp
, xfer
);
978 ATADEBUG_PRINT(("wdc_atapi_intr: wdc_atapi_done() (end), error 0x%x "
979 "sense 0x%x\n", sc_xfer
->error
, sc_xfer
->sense
.atapi_sense
),
981 wdc_atapi_done(chp
, xfer
);
986 wdc_atapi_phase_complete(struct ata_xfer
*xfer
)
988 struct ata_channel
*chp
= xfer
->c_chp
;
989 struct atac_softc
*atac
= chp
->ch_atac
;
990 #if NATA_DMA || NATA_PIOBM
991 struct wdc_softc
*wdc
= CHAN_TO_WDC(chp
);
993 struct scsipi_xfer
*sc_xfer
= xfer
->c_cmd
;
994 struct ata_drive_datas
*drvp
= &chp
->ch_drive
[xfer
->c_drive
];
996 /* wait for DSC if needed */
997 if (drvp
->drive_flags
& DRIVE_ATAPIST
) {
998 ATADEBUG_PRINT(("wdc_atapi_phase_complete(%s:%d:%d) "
999 "polldsc %d\n", device_xname(atac
->atac_dev
),
1001 xfer
->c_drive
, xfer
->c_dscpoll
), DEBUG_XFERS
);
1004 panic("wdc_atapi_phase_complete: cold");
1006 if (wdcwait(chp
, WDCS_DSC
, WDCS_DSC
, 10,
1007 AT_POLL
) == WDCWAIT_TOUT
) {
1008 /* 10ms not enough, try again in 1 tick */
1009 if (xfer
->c_dscpoll
++ >
1010 mstohz(sc_xfer
->timeout
)) {
1011 printf("%s:%d:%d: wait_for_dsc "
1013 device_xname(atac
->atac_dev
),
1014 chp
->ch_channel
, xfer
->c_drive
);
1015 sc_xfer
->error
= XS_TIMEOUT
;
1016 wdc_atapi_reset(chp
, xfer
);
1019 callout_reset(&chp
->ch_callout
, 1,
1020 wdc_atapi_polldsc
, xfer
);
1026 * Some drive occasionally set WDCS_ERR with
1027 * "ATA illegal length indication" in the error
1028 * register. If we read some data the sense is valid
1029 * anyway, so don't report the error.
1031 if (chp
->ch_status
& WDCS_ERR
&&
1032 ((sc_xfer
->xs_control
& XS_CTL_REQSENSE
) == 0 ||
1033 sc_xfer
->resid
== sc_xfer
->datalen
)) {
1034 /* save the short sense */
1035 sc_xfer
->error
= XS_SHORTSENSE
;
1036 sc_xfer
->sense
.atapi_sense
= chp
->ch_error
;
1037 if ((sc_xfer
->xs_periph
->periph_quirks
&
1038 PQUIRK_NOSENSE
) == 0) {
1039 /* ask scsipi to send a REQUEST_SENSE */
1040 sc_xfer
->error
= XS_BUSY
;
1041 sc_xfer
->status
= SCSI_CHECK
;
1043 #if NATA_DMA || NATA_PIOBM
1044 else if (wdc
->dma_status
&
1045 (WDC_DMAST_NOIRQ
| WDC_DMAST_ERR
)) {
1048 (xfer
->c_flags
& C_POLL
) ? AT_POLL
: 0);
1050 sc_xfer
->error
= XS_RESET
;
1051 wdc_atapi_reset(chp
, xfer
);
1056 if (xfer
->c_bcount
!= 0) {
1057 ATADEBUG_PRINT(("wdc_atapi_intr: bcount value is "
1058 "%d after io\n", xfer
->c_bcount
), DEBUG_XFERS
);
1061 if (xfer
->c_bcount
< 0) {
1062 printf("wdc_atapi_intr warning: bcount value "
1063 "is %d after io\n", xfer
->c_bcount
);
1066 ATADEBUG_PRINT(("wdc_atapi_phase_complete: wdc_atapi_done(), "
1067 "error 0x%x sense 0x%x\n", sc_xfer
->error
,
1068 sc_xfer
->sense
.atapi_sense
), DEBUG_INTR
);
1069 wdc_atapi_done(chp
, xfer
);
1073 wdc_atapi_done(struct ata_channel
*chp
, struct ata_xfer
*xfer
)
1075 struct atac_softc
*atac
= chp
->ch_atac
;
1076 struct scsipi_xfer
*sc_xfer
= xfer
->c_cmd
;
1077 int drive
= xfer
->c_drive
;
1079 ATADEBUG_PRINT(("wdc_atapi_done %s:%d:%d: flags 0x%x\n",
1080 device_xname(atac
->atac_dev
), chp
->ch_channel
, xfer
->c_drive
,
1081 (u_int
)xfer
->c_flags
), DEBUG_XFERS
);
1082 callout_stop(&chp
->ch_callout
);
1083 /* mark controller inactive and free the command */
1084 chp
->ch_queue
->active_xfer
= NULL
;
1085 ata_free_xfer(chp
, xfer
);
1087 if (chp
->ch_drive
[drive
].drive_flags
& DRIVE_WAITDRAIN
) {
1088 sc_xfer
->error
= XS_DRIVER_STUFFUP
;
1089 chp
->ch_drive
[drive
].drive_flags
&= ~DRIVE_WAITDRAIN
;
1090 wakeup(&chp
->ch_queue
->active_xfer
);
1093 ATADEBUG_PRINT(("wdc_atapi_done: scsipi_done\n"), DEBUG_XFERS
);
1094 scsipi_done(sc_xfer
);
1095 ATADEBUG_PRINT(("atastart from wdc_atapi_done, flags 0x%x\n",
1096 chp
->ch_flags
), DEBUG_XFERS
);
1101 wdc_atapi_reset(struct ata_channel
*chp
, struct ata_xfer
*xfer
)
1103 struct atac_softc
*atac
= chp
->ch_atac
;
1104 struct ata_drive_datas
*drvp
= &chp
->ch_drive
[xfer
->c_drive
];
1105 struct scsipi_xfer
*sc_xfer
= xfer
->c_cmd
;
1107 wdccommandshort(chp
, xfer
->c_drive
, ATAPI_SOFT_RESET
);
1109 if (wdc_wait_for_unbusy(chp
, WDC_RESET_WAIT
, AT_POLL
) != 0) {
1110 printf("%s:%d:%d: reset failed\n",
1111 device_xname(atac
->atac_dev
), chp
->ch_channel
,
1113 sc_xfer
->error
= XS_SELTIMEOUT
;
1115 wdc_atapi_done(chp
, xfer
);
1120 wdc_atapi_polldsc(void *arg
)
1123 wdc_atapi_phase_complete(arg
);