atapi_wdc.c revision 1.120 1 1.120 tsutsui /* $NetBSD: atapi_wdc.c,v 1.120 2016/03/20 09:47:58 tsutsui Exp $ */
2 1.2 bouyer
3 1.2 bouyer /*
4 1.47 bouyer * Copyright (c) 1998, 2001 Manuel Bouyer.
5 1.2 bouyer *
6 1.2 bouyer * Redistribution and use in source and binary forms, with or without
7 1.2 bouyer * modification, are permitted provided that the following conditions
8 1.2 bouyer * are met:
9 1.2 bouyer * 1. Redistributions of source code must retain the above copyright
10 1.2 bouyer * notice, this list of conditions and the following disclaimer.
11 1.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
12 1.2 bouyer * notice, this list of conditions and the following disclaimer in the
13 1.2 bouyer * documentation and/or other materials provided with the distribution.
14 1.2 bouyer *
15 1.35 bouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.35 bouyer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 1.35 bouyer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 1.92 perry * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 1.35 bouyer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 1.35 bouyer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 1.35 bouyer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 1.35 bouyer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 1.35 bouyer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 1.35 bouyer * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 1.2 bouyer */
26 1.44 lukem
27 1.44 lukem #include <sys/cdefs.h>
28 1.120 tsutsui __KERNEL_RCSID(0, "$NetBSD: atapi_wdc.c,v 1.120 2016/03/20 09:47:58 tsutsui Exp $");
29 1.2 bouyer
30 1.86 thorpej #ifndef ATADEBUG
31 1.86 thorpej #define ATADEBUG
32 1.86 thorpej #endif /* ATADEBUG */
33 1.2 bouyer
34 1.2 bouyer #include <sys/param.h>
35 1.2 bouyer #include <sys/systm.h>
36 1.2 bouyer #include <sys/kernel.h>
37 1.2 bouyer #include <sys/file.h>
38 1.2 bouyer #include <sys/stat.h>
39 1.2 bouyer #include <sys/buf.h>
40 1.2 bouyer #include <sys/malloc.h>
41 1.2 bouyer #include <sys/device.h>
42 1.2 bouyer #include <sys/syslog.h>
43 1.2 bouyer #include <sys/proc.h>
44 1.39 mycroft #include <sys/dvdio.h>
45 1.2 bouyer
46 1.105 ad #include <sys/intr.h>
47 1.105 ad #include <sys/bus.h>
48 1.2 bouyer
49 1.2 bouyer #ifndef __BUS_SPACE_HAS_STREAM_METHODS
50 1.65 thorpej #define bus_space_write_multi_stream_2 bus_space_write_multi_2
51 1.65 thorpej #define bus_space_write_multi_stream_4 bus_space_write_multi_4
52 1.65 thorpej #define bus_space_read_multi_stream_2 bus_space_read_multi_2
53 1.65 thorpej #define bus_space_read_multi_stream_4 bus_space_read_multi_4
54 1.2 bouyer #endif /* __BUS_SPACE_HAS_STREAM_METHODS */
55 1.2 bouyer
56 1.101 itohy #include <dev/ata/ataconf.h>
57 1.2 bouyer #include <dev/ata/atareg.h>
58 1.2 bouyer #include <dev/ata/atavar.h>
59 1.2 bouyer #include <dev/ic/wdcreg.h>
60 1.2 bouyer #include <dev/ic/wdcvar.h>
61 1.2 bouyer
62 1.40 bouyer #include <dev/scsipi/scsi_all.h> /* for SCSI status */
63 1.40 bouyer
64 1.2 bouyer #define DEBUG_INTR 0x01
65 1.2 bouyer #define DEBUG_XFERS 0x02
66 1.2 bouyer #define DEBUG_STATUS 0x04
67 1.2 bouyer #define DEBUG_FUNCS 0x08
68 1.2 bouyer #define DEBUG_PROBE 0x10
69 1.86 thorpej #ifdef ATADEBUG
70 1.3 bouyer int wdcdebug_atapi_mask = 0;
71 1.86 thorpej #define ATADEBUG_PRINT(args, level) \
72 1.2 bouyer if (wdcdebug_atapi_mask & (level)) \
73 1.2 bouyer printf args
74 1.2 bouyer #else
75 1.86 thorpej #define ATADEBUG_PRINT(args, level)
76 1.2 bouyer #endif
77 1.2 bouyer
78 1.2 bouyer #define ATAPI_DELAY 10 /* 10 ms, this is used only before sending a cmd */
79 1.59 bouyer #define ATAPI_MODE_DELAY 1000 /* 1s, timeout for SET_FEATYRE cmds */
80 1.65 thorpej
81 1.65 thorpej static int wdc_atapi_get_params(struct scsipi_channel *, int,
82 1.65 thorpej struct ataparams *);
83 1.65 thorpej static void wdc_atapi_probe_device(struct atapibus_softc *, int);
84 1.65 thorpej static void wdc_atapi_minphys (struct buf *bp);
85 1.87 thorpej static void wdc_atapi_start(struct ata_channel *,struct ata_xfer *);
86 1.87 thorpej static int wdc_atapi_intr(struct ata_channel *, struct ata_xfer *, int);
87 1.87 thorpej static void wdc_atapi_kill_xfer(struct ata_channel *,
88 1.71 bouyer struct ata_xfer *, int);
89 1.65 thorpej static void wdc_atapi_phase_complete(struct ata_xfer *);
90 1.87 thorpej static void wdc_atapi_done(struct ata_channel *, struct ata_xfer *);
91 1.87 thorpej static void wdc_atapi_reset(struct ata_channel *, struct ata_xfer *);
92 1.65 thorpej static void wdc_atapi_scsipi_request(struct scsipi_channel *,
93 1.65 thorpej scsipi_adapter_req_t, void *);
94 1.65 thorpej static void wdc_atapi_kill_pending(struct scsipi_periph *);
95 1.65 thorpej static void wdc_atapi_polldsc(void *arg);
96 1.2 bouyer
97 1.2 bouyer #define MAX_SIZE MAXPHYS
98 1.2 bouyer
99 1.65 thorpej static const struct scsipi_bustype wdc_atapi_bustype = {
100 1.40 bouyer SCSIPI_BUSTYPE_ATAPI,
101 1.40 bouyer atapi_scsipi_cmd,
102 1.40 bouyer atapi_interpret_sense,
103 1.40 bouyer atapi_print_addr,
104 1.40 bouyer wdc_atapi_kill_pending,
105 1.113 bouyer NULL,
106 1.40 bouyer };
107 1.40 bouyer
108 1.2 bouyer void
109 1.65 thorpej wdc_atapibus_attach(struct atabus_softc *ata_sc)
110 1.2 bouyer {
111 1.87 thorpej struct ata_channel *chp = ata_sc->sc_chan;
112 1.88 thorpej struct atac_softc *atac = chp->ch_atac;
113 1.88 thorpej struct scsipi_adapter *adapt = &atac->atac_atapi_adapter._generic;
114 1.40 bouyer struct scsipi_channel *chan = &chp->ch_atapi_channel;
115 1.2 bouyer
116 1.9 thorpej /*
117 1.40 bouyer * Fill in the scsipi_adapter.
118 1.9 thorpej */
119 1.106 cube adapt->adapt_dev = atac->atac_dev;
120 1.88 thorpej adapt->adapt_nchannels = atac->atac_nchannels;
121 1.40 bouyer adapt->adapt_request = wdc_atapi_scsipi_request;
122 1.40 bouyer adapt->adapt_minphys = wdc_atapi_minphys;
123 1.88 thorpej if (atac->atac_cap & ATAC_CAP_NOIRQ)
124 1.42 bjh21 adapt->adapt_flags |= SCSIPI_ADAPT_POLL_ONLY;
125 1.88 thorpej atac->atac_atapi_adapter.atapi_probe_device = wdc_atapi_probe_device;
126 1.40 bouyer
127 1.40 bouyer /*
128 1.40 bouyer * Fill in the scsipi_channel.
129 1.40 bouyer */
130 1.40 bouyer memset(chan, 0, sizeof(*chan));
131 1.40 bouyer chan->chan_adapter = adapt;
132 1.40 bouyer chan->chan_bustype = &wdc_atapi_bustype;
133 1.67 thorpej chan->chan_channel = chp->ch_channel;
134 1.40 bouyer chan->chan_flags = SCSIPI_CHAN_OPENINGS;
135 1.40 bouyer chan->chan_openings = 1;
136 1.40 bouyer chan->chan_max_periph = 1;
137 1.40 bouyer chan->chan_ntargets = 2;
138 1.40 bouyer chan->chan_nluns = 1;
139 1.40 bouyer
140 1.106 cube chp->atapibus = config_found_ia(ata_sc->sc_dev, "atapi", chan,
141 1.96 drochner atapiprint);
142 1.2 bouyer }
143 1.2 bouyer
144 1.65 thorpej static void
145 1.65 thorpej wdc_atapi_minphys(struct buf *bp)
146 1.2 bouyer {
147 1.40 bouyer
148 1.40 bouyer if (bp->b_bcount > MAX_SIZE)
149 1.2 bouyer bp->b_bcount = MAX_SIZE;
150 1.2 bouyer minphys(bp);
151 1.2 bouyer }
152 1.2 bouyer
153 1.28 enami /*
154 1.40 bouyer * Kill off all pending xfers for a periph.
155 1.28 enami *
156 1.28 enami * Must be called at splbio().
157 1.28 enami */
158 1.65 thorpej static void
159 1.65 thorpej wdc_atapi_kill_pending(struct scsipi_periph *periph)
160 1.28 enami {
161 1.88 thorpej struct atac_softc *atac =
162 1.106 cube device_private(periph->periph_channel->chan_adapter->adapt_dev);
163 1.87 thorpej struct ata_channel *chp =
164 1.88 thorpej atac->atac_channels[periph->periph_channel->chan_channel];
165 1.28 enami
166 1.82 thorpej ata_kill_pending(&chp->ch_drive[periph->periph_target]);
167 1.28 enami }
168 1.28 enami
169 1.65 thorpej static void
170 1.87 thorpej wdc_atapi_kill_xfer(struct ata_channel *chp, struct ata_xfer *xfer, int reason)
171 1.28 enami {
172 1.63 thorpej struct scsipi_xfer *sc_xfer = xfer->c_cmd;
173 1.28 enami
174 1.28 enami /* remove this command from xfer queue */
175 1.71 bouyer switch (reason) {
176 1.71 bouyer case KILL_GONE:
177 1.71 bouyer sc_xfer->error = XS_DRIVER_STUFFUP;
178 1.71 bouyer break;
179 1.71 bouyer case KILL_RESET:
180 1.71 bouyer sc_xfer->error = XS_RESET;
181 1.71 bouyer break;
182 1.71 bouyer default:
183 1.71 bouyer printf("wdc_ata_bio_kill_xfer: unknown reason %d\n",
184 1.71 bouyer reason);
185 1.71 bouyer panic("wdc_ata_bio_kill_xfer");
186 1.71 bouyer }
187 1.81 thorpej ata_free_xfer(chp, xfer);
188 1.73 bouyer scsipi_done(sc_xfer);
189 1.28 enami }
190 1.28 enami
191 1.65 thorpej static int
192 1.65 thorpej wdc_atapi_get_params(struct scsipi_channel *chan, int drive,
193 1.65 thorpej struct ataparams *id)
194 1.2 bouyer {
195 1.106 cube struct wdc_softc *wdc = device_private(chan->chan_adapter->adapt_dev);
196 1.88 thorpej struct atac_softc *atac = &wdc->sc_atac;
197 1.87 thorpej struct wdc_regs *wdr = &wdc->regs[chan->chan_channel];
198 1.88 thorpej struct ata_channel *chp = atac->atac_channels[chan->chan_channel];
199 1.78 thorpej struct ata_command ata_c;
200 1.2 bouyer
201 1.78 thorpej memset(&ata_c, 0, sizeof(struct ata_command));
202 1.78 thorpej ata_c.r_command = ATAPI_SOFT_RESET;
203 1.78 thorpej ata_c.r_st_bmask = 0;
204 1.78 thorpej ata_c.r_st_pmask = 0;
205 1.78 thorpej ata_c.flags = AT_WAIT | AT_POLL;
206 1.78 thorpej ata_c.timeout = WDC_RESET_WAIT;
207 1.79 thorpej if (wdc_exec_command(&chp->ch_drive[drive], &ata_c) != ATACMD_COMPLETE) {
208 1.59 bouyer printf("wdc_atapi_get_params: ATAPI_SOFT_RESET failed for"
209 1.59 bouyer " drive %s:%d:%d: driver failed\n",
210 1.106 cube device_xname(atac->atac_dev), chp->ch_channel, drive);
211 1.59 bouyer panic("wdc_atapi_get_params");
212 1.59 bouyer }
213 1.78 thorpej if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
214 1.86 thorpej ATADEBUG_PRINT(("wdc_atapi_get_params: ATAPI_SOFT_RESET "
215 1.59 bouyer "failed for drive %s:%d:%d: error 0x%x\n",
216 1.106 cube device_xname(atac->atac_dev), chp->ch_channel, drive,
217 1.78 thorpej ata_c.r_error), DEBUG_PROBE);
218 1.2 bouyer return -1;
219 1.2 bouyer }
220 1.59 bouyer chp->ch_drive[drive].state = 0;
221 1.2 bouyer
222 1.99 christos (void)bus_space_read_1(wdr->cmd_iot, wdr->cmd_iohs[wd_status], 0);
223 1.92 perry
224 1.2 bouyer /* Some ATAPI devices need a bit more time after software reset. */
225 1.2 bouyer delay(5000);
226 1.57 mycroft if (ata_get_params(&chp->ch_drive[drive], AT_WAIT, id) != 0) {
227 1.86 thorpej ATADEBUG_PRINT(("wdc_atapi_get_params: ATAPI_IDENTIFY_DEVICE "
228 1.2 bouyer "failed for drive %s:%d:%d: error 0x%x\n",
229 1.106 cube device_xname(atac->atac_dev), chp->ch_channel, drive,
230 1.78 thorpej ata_c.r_error), DEBUG_PROBE);
231 1.2 bouyer return -1;
232 1.2 bouyer }
233 1.40 bouyer return 0;
234 1.2 bouyer }
235 1.2 bouyer
236 1.65 thorpej static void
237 1.65 thorpej wdc_atapi_probe_device(struct atapibus_softc *sc, int target)
238 1.33 bouyer {
239 1.40 bouyer struct scsipi_channel *chan = sc->sc_channel;
240 1.40 bouyer struct scsipi_periph *periph;
241 1.33 bouyer struct ataparams ids;
242 1.33 bouyer struct ataparams *id = &ids;
243 1.106 cube struct wdc_softc *wdc = device_private(chan->chan_adapter->adapt_dev);
244 1.88 thorpej struct atac_softc *atac = &wdc->sc_atac;
245 1.88 thorpej struct ata_channel *chp = atac->atac_channels[chan->chan_channel];
246 1.46 bouyer struct ata_drive_datas *drvp = &chp->ch_drive[target];
247 1.40 bouyer struct scsipibus_attach_args sa;
248 1.33 bouyer char serial_number[21], model[41], firmware_revision[9];
249 1.89 thorpej int s;
250 1.33 bouyer
251 1.40 bouyer /* skip if already attached */
252 1.40 bouyer if (scsipi_lookup_periph(chan, target, 0) != NULL)
253 1.33 bouyer return;
254 1.40 bouyer
255 1.119 bouyer /* if no ATAPI device detected at wdc attach time, skip */
256 1.119 bouyer if (drvp->drive_type != ATA_DRIVET_ATAPI) {
257 1.119 bouyer ATADEBUG_PRINT(("wdc_atapi_probe_device: "
258 1.119 bouyer "drive %d not present\n", target), DEBUG_PROBE);
259 1.119 bouyer return;
260 1.119 bouyer }
261 1.119 bouyer
262 1.57 mycroft if (wdc_atapi_get_params(chan, target, id) == 0) {
263 1.33 bouyer #ifdef ATAPI_DEBUG_PROBE
264 1.33 bouyer printf("%s drive %d: cmdsz 0x%x drqtype 0x%x\n",
265 1.107 cube device_xname(sc->sc_dev), target,
266 1.33 bouyer id->atap_config & ATAPI_CFG_CMD_MASK,
267 1.33 bouyer id->atap_config & ATAPI_CFG_DRQ_MASK);
268 1.33 bouyer #endif
269 1.40 bouyer periph = scsipi_alloc_periph(M_NOWAIT);
270 1.40 bouyer if (periph == NULL) {
271 1.107 cube aprint_error_dev(sc->sc_dev,
272 1.107 cube "unable to allocate periph for drive %d\n",
273 1.107 cube target);
274 1.40 bouyer return;
275 1.33 bouyer }
276 1.40 bouyer periph->periph_dev = NULL;
277 1.40 bouyer periph->periph_channel = chan;
278 1.40 bouyer periph->periph_switch = &atapi_probe_periphsw;
279 1.40 bouyer periph->periph_target = target;
280 1.40 bouyer periph->periph_lun = 0;
281 1.56 mycroft periph->periph_quirks = PQUIRK_ONLYBIG;
282 1.40 bouyer
283 1.40 bouyer #ifdef SCSIPI_DEBUG
284 1.40 bouyer if (SCSIPI_DEBUG_TYPE == SCSIPI_BUSTYPE_ATAPI &&
285 1.40 bouyer SCSIPI_DEBUG_TARGET == target)
286 1.40 bouyer periph->periph_dbflags |= SCSIPI_DEBUG_FLAGS;
287 1.40 bouyer #endif
288 1.40 bouyer periph->periph_type = ATAPI_CFG_TYPE(id->atap_config);
289 1.40 bouyer if (id->atap_config & ATAPI_CFG_REMOV)
290 1.40 bouyer periph->periph_flags |= PERIPH_REMOVABLE;
291 1.89 thorpej if (periph->periph_type == T_SEQUENTIAL) {
292 1.89 thorpej s = splbio();
293 1.118 bouyer drvp->drive_flags |= ATA_DRIVE_ATAPIDSCW;
294 1.89 thorpej splx(s);
295 1.89 thorpej }
296 1.40 bouyer
297 1.40 bouyer sa.sa_periph = periph;
298 1.40 bouyer sa.sa_inqbuf.type = ATAPI_CFG_TYPE(id->atap_config);
299 1.40 bouyer sa.sa_inqbuf.removable = id->atap_config & ATAPI_CFG_REMOV ?
300 1.40 bouyer T_REMOV : T_FIXED;
301 1.100 tron scsipi_strvis((u_char *)model, 40, id->atap_model, 40);
302 1.100 tron scsipi_strvis((u_char *)serial_number, 20, id->atap_serial,
303 1.100 tron 20);
304 1.100 tron scsipi_strvis((u_char *)firmware_revision, 8,
305 1.100 tron id->atap_revision, 8);
306 1.33 bouyer sa.sa_inqbuf.vendor = model;
307 1.33 bouyer sa.sa_inqbuf.product = serial_number;
308 1.33 bouyer sa.sa_inqbuf.revision = firmware_revision;
309 1.40 bouyer
310 1.40 bouyer /*
311 1.40 bouyer * Determine the operating mode capabilities of the device.
312 1.40 bouyer */
313 1.40 bouyer if ((id->atap_config & ATAPI_CFG_CMD_MASK) == ATAPI_CFG_CMD_16)
314 1.40 bouyer periph->periph_cap |= PERIPH_CAP_CMD16;
315 1.40 bouyer /* XXX This is gross. */
316 1.40 bouyer periph->periph_cap |= (id->atap_config & ATAPI_CFG_DRQ_MASK);
317 1.40 bouyer
318 1.40 bouyer drvp->drv_softc = atapi_probe_device(sc, target, periph, &sa);
319 1.40 bouyer
320 1.40 bouyer if (drvp->drv_softc)
321 1.80 thorpej ata_probe_caps(drvp);
322 1.89 thorpej else {
323 1.89 thorpej s = splbio();
324 1.118 bouyer drvp->drive_type = ATA_DRIVET_NONE;
325 1.89 thorpej splx(s);
326 1.89 thorpej }
327 1.69 bouyer } else {
328 1.89 thorpej s = splbio();
329 1.118 bouyer drvp->drive_type = ATA_DRIVET_NONE;
330 1.89 thorpej splx(s);
331 1.33 bouyer }
332 1.33 bouyer }
333 1.33 bouyer
334 1.65 thorpej static void
335 1.65 thorpej wdc_atapi_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
336 1.65 thorpej void *arg)
337 1.40 bouyer {
338 1.40 bouyer struct scsipi_adapter *adapt = chan->chan_adapter;
339 1.40 bouyer struct scsipi_periph *periph;
340 1.2 bouyer struct scsipi_xfer *sc_xfer;
341 1.106 cube struct wdc_softc *wdc = device_private(adapt->adapt_dev);
342 1.88 thorpej struct atac_softc *atac = &wdc->sc_atac;
343 1.63 thorpej struct ata_xfer *xfer;
344 1.40 bouyer int channel = chan->chan_channel;
345 1.40 bouyer int drive, s;
346 1.40 bouyer
347 1.40 bouyer switch (req) {
348 1.40 bouyer case ADAPTER_REQ_RUN_XFER:
349 1.40 bouyer sc_xfer = arg;
350 1.40 bouyer periph = sc_xfer->xs_periph;
351 1.40 bouyer drive = periph->periph_target;
352 1.40 bouyer
353 1.86 thorpej ATADEBUG_PRINT(("wdc_atapi_scsipi_request %s:%d:%d\n",
354 1.106 cube device_xname(atac->atac_dev), channel, drive),
355 1.106 cube DEBUG_XFERS);
356 1.106 cube if (!device_is_active(atac->atac_dev)) {
357 1.40 bouyer sc_xfer->error = XS_DRIVER_STUFFUP;
358 1.40 bouyer scsipi_done(sc_xfer);
359 1.40 bouyer return;
360 1.40 bouyer }
361 1.40 bouyer
362 1.81 thorpej xfer = ata_get_xfer(ATAXF_NOSLEEP);
363 1.40 bouyer if (xfer == NULL) {
364 1.40 bouyer sc_xfer->error = XS_RESOURCE_SHORTAGE;
365 1.40 bouyer scsipi_done(sc_xfer);
366 1.40 bouyer return;
367 1.40 bouyer }
368 1.40 bouyer
369 1.40 bouyer if (sc_xfer->xs_control & XS_CTL_POLL)
370 1.40 bouyer xfer->c_flags |= C_POLL;
371 1.102 itohy #if NATA_DMA
372 1.88 thorpej if ((atac->atac_channels[channel]->ch_drive[drive].drive_flags &
373 1.118 bouyer (ATA_DRIVE_DMA | ATA_DRIVE_UDMA)) && sc_xfer->datalen > 0)
374 1.59 bouyer xfer->c_flags |= C_DMA;
375 1.102 itohy #endif
376 1.102 itohy #if NATA_DMA && NATA_PIOBM
377 1.102 itohy else
378 1.102 itohy #endif
379 1.101 itohy #if NATA_PIOBM
380 1.102 itohy if ((atac->atac_cap & ATAC_CAP_PIOBM) &&
381 1.101 itohy sc_xfer->datalen > 0)
382 1.101 itohy xfer->c_flags |= C_PIOBM;
383 1.101 itohy #endif
384 1.63 thorpej xfer->c_drive = drive;
385 1.40 bouyer xfer->c_flags |= C_ATAPI;
386 1.102 itohy #if NATA_DMA
387 1.40 bouyer if (sc_xfer->cmd->opcode == GPCMD_REPORT_KEY ||
388 1.40 bouyer sc_xfer->cmd->opcode == GPCMD_SEND_KEY ||
389 1.40 bouyer sc_xfer->cmd->opcode == GPCMD_READ_DVD_STRUCTURE) {
390 1.40 bouyer /*
391 1.40 bouyer * DVD authentication commands must always be done in
392 1.40 bouyer * PIO mode.
393 1.40 bouyer */
394 1.59 bouyer xfer->c_flags &= ~C_DMA;
395 1.40 bouyer }
396 1.104 reinoud
397 1.52 bouyer /*
398 1.104 reinoud * DMA normally can't deal with transfers which are not a
399 1.104 reinoud * multiple of its databus width. It's a bug to request odd
400 1.104 reinoud * length transfers for ATAPI.
401 1.104 reinoud *
402 1.104 reinoud * Some devices also can't cope with unaligned DMA xfers
403 1.104 reinoud * either. Also some devices seem to not handle DMA xfers of
404 1.104 reinoud * less than 4 bytes.
405 1.104 reinoud *
406 1.104 reinoud * By enforcing at least 4 byte aligned offset and length for
407 1.104 reinoud * DMA, we might use PIO where DMA could be allowed but better
408 1.104 reinoud * safe than sorry as recent problems proved.
409 1.104 reinoud *
410 1.104 reinoud * Offending structures that are thus done by PIO instead of
411 1.104 reinoud * DMA are normally small structures since all bulkdata is
412 1.104 reinoud * aligned. But as the request may come from userland, we have
413 1.104 reinoud * to protect against it anyway.
414 1.104 reinoud *
415 1.104 reinoud * XXX check for the 32 bit wide flag?
416 1.52 bouyer */
417 1.104 reinoud
418 1.104 reinoud if (((uintptr_t) sc_xfer->data) & 0x03)
419 1.103 reinoud xfer->c_flags &= ~C_DMA;
420 1.104 reinoud if ((sc_xfer->datalen < 4) || (sc_xfer->datalen & 0x03))
421 1.59 bouyer xfer->c_flags &= ~C_DMA;
422 1.102 itohy #endif /* NATA_DMA */
423 1.52 bouyer
424 1.63 thorpej xfer->c_cmd = sc_xfer;
425 1.63 thorpej xfer->c_databuf = sc_xfer->data;
426 1.40 bouyer xfer->c_bcount = sc_xfer->datalen;
427 1.40 bouyer xfer->c_start = wdc_atapi_start;
428 1.40 bouyer xfer->c_intr = wdc_atapi_intr;
429 1.40 bouyer xfer->c_kill_xfer = wdc_atapi_kill_xfer;
430 1.45 bouyer xfer->c_dscpoll = 0;
431 1.40 bouyer s = splbio();
432 1.88 thorpej ata_exec_xfer(atac->atac_channels[channel], xfer);
433 1.2 bouyer #ifdef DIAGNOSTIC
434 1.40 bouyer if ((sc_xfer->xs_control & XS_CTL_POLL) != 0 &&
435 1.40 bouyer (sc_xfer->xs_status & XS_STS_DONE) == 0)
436 1.40 bouyer panic("wdc_atapi_scsipi_request: polled command "
437 1.40 bouyer "not done");
438 1.2 bouyer #endif
439 1.40 bouyer splx(s);
440 1.40 bouyer return;
441 1.40 bouyer
442 1.40 bouyer default:
443 1.40 bouyer /* Not supported, nothing to do. */
444 1.41 lukem ;
445 1.40 bouyer }
446 1.2 bouyer }
447 1.2 bouyer
448 1.65 thorpej static void
449 1.87 thorpej wdc_atapi_start(struct ata_channel *chp, struct ata_xfer *xfer)
450 1.2 bouyer {
451 1.88 thorpej struct atac_softc *atac = chp->ch_atac;
452 1.88 thorpej struct wdc_softc *wdc = CHAN_TO_WDC(chp);
453 1.87 thorpej struct wdc_regs *wdr = &wdc->regs[chp->ch_channel];
454 1.63 thorpej struct scsipi_xfer *sc_xfer = xfer->c_cmd;
455 1.63 thorpej struct ata_drive_datas *drvp = &chp->ch_drive[xfer->c_drive];
456 1.59 bouyer int wait_flags = (sc_xfer->xs_control & XS_CTL_POLL) ? AT_POLL : 0;
457 1.93 christos const char *errstring;
458 1.2 bouyer
459 1.86 thorpej ATADEBUG_PRINT(("wdc_atapi_start %s:%d:%d, scsi flags 0x%x \n",
460 1.106 cube device_xname(atac->atac_dev), chp->ch_channel, drvp->drive,
461 1.27 thorpej sc_xfer->xs_control), DEBUG_XFERS);
462 1.102 itohy #if NATA_DMA
463 1.59 bouyer if ((xfer->c_flags & C_DMA) && (drvp->n_xfers <= NXFER))
464 1.59 bouyer drvp->n_xfers++;
465 1.102 itohy #endif
466 1.59 bouyer /* Do control operations specially. */
467 1.59 bouyer if (__predict_false(drvp->state < READY)) {
468 1.94 peter /* If it's not a polled command, we need the kernel thread */
469 1.109 bouyer if ((sc_xfer->xs_control & XS_CTL_POLL) == 0 &&
470 1.109 bouyer (chp->ch_flags & ATACH_TH_RUN) == 0) {
471 1.61 bouyer chp->ch_queue->queue_freeze++;
472 1.68 thorpej wakeup(&chp->ch_thread);
473 1.59 bouyer return;
474 1.59 bouyer }
475 1.59 bouyer /*
476 1.59 bouyer * disable interrupts, all commands here should be quick
477 1.111 snj * enough to be able to poll, and we don't go here that often
478 1.59 bouyer */
479 1.120 tsutsui bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr,
480 1.59 bouyer WDCTL_4BIT | WDCTL_IDS);
481 1.85 thorpej if (wdc->select)
482 1.67 thorpej wdc->select(chp, xfer->c_drive);
483 1.87 thorpej bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0,
484 1.63 thorpej WDSD_IBM | (xfer->c_drive << 4));
485 1.59 bouyer /* Don't try to set mode if controller can't be adjusted */
486 1.88 thorpej if (atac->atac_set_modes == NULL)
487 1.59 bouyer goto ready;
488 1.59 bouyer /* Also don't try if the drive didn't report its mode */
489 1.118 bouyer if ((drvp->drive_flags & ATA_DRIVE_MODE) == 0)
490 1.59 bouyer goto ready;
491 1.59 bouyer errstring = "unbusy";
492 1.64 thorpej if (wdc_wait_for_unbusy(chp, ATAPI_DELAY, wait_flags))
493 1.59 bouyer goto timeout;
494 1.59 bouyer wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
495 1.59 bouyer 0x08 | drvp->PIO_mode, WDSF_SET_MODE);
496 1.59 bouyer errstring = "piomode";
497 1.64 thorpej if (wdc_wait_for_unbusy(chp, ATAPI_MODE_DELAY, wait_flags))
498 1.59 bouyer goto timeout;
499 1.59 bouyer if (chp->ch_status & WDCS_ERR) {
500 1.59 bouyer if (chp->ch_error == WDCE_ABRT) {
501 1.59 bouyer /*
502 1.76 mycroft * Some ATAPI drives reject PIO settings.
503 1.76 mycroft * Fall back to PIO mode 3 since that's the
504 1.76 mycroft * minimum for ATAPI.
505 1.59 bouyer */
506 1.76 mycroft printf("%s:%d:%d: PIO mode %d rejected, "
507 1.76 mycroft "falling back to PIO mode 3\n",
508 1.106 cube device_xname(atac->atac_dev),
509 1.76 mycroft chp->ch_channel, xfer->c_drive,
510 1.76 mycroft drvp->PIO_mode);
511 1.76 mycroft if (drvp->PIO_mode > 3)
512 1.76 mycroft drvp->PIO_mode = 3;
513 1.76 mycroft } else
514 1.76 mycroft goto error;
515 1.59 bouyer }
516 1.102 itohy #if NATA_DMA
517 1.102 itohy #if NATA_UDMA
518 1.118 bouyer if (drvp->drive_flags & ATA_DRIVE_UDMA) {
519 1.59 bouyer wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
520 1.59 bouyer 0x40 | drvp->UDMA_mode, WDSF_SET_MODE);
521 1.102 itohy } else
522 1.102 itohy #endif
523 1.118 bouyer if (drvp->drive_flags & ATA_DRIVE_DMA) {
524 1.59 bouyer wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
525 1.59 bouyer 0x20 | drvp->DMA_mode, WDSF_SET_MODE);
526 1.59 bouyer } else {
527 1.59 bouyer goto ready;
528 1.59 bouyer }
529 1.59 bouyer errstring = "dmamode";
530 1.64 thorpej if (wdc_wait_for_unbusy(chp, ATAPI_MODE_DELAY, wait_flags))
531 1.59 bouyer goto timeout;
532 1.76 mycroft if (chp->ch_status & WDCS_ERR) {
533 1.76 mycroft if (chp->ch_error == WDCE_ABRT) {
534 1.102 itohy #if NATA_UDMA
535 1.118 bouyer if (drvp->drive_flags & ATA_DRIVE_UDMA)
536 1.76 mycroft goto error;
537 1.102 itohy else
538 1.102 itohy #endif
539 1.102 itohy {
540 1.76 mycroft /*
541 1.76 mycroft * The drive rejected our DMA setting.
542 1.76 mycroft * Fall back to mode 1.
543 1.76 mycroft */
544 1.76 mycroft printf("%s:%d:%d: DMA mode %d rejected, "
545 1.76 mycroft "falling back to DMA mode 0\n",
546 1.106 cube device_xname(atac->atac_dev),
547 1.76 mycroft chp->ch_channel, xfer->c_drive,
548 1.76 mycroft drvp->DMA_mode);
549 1.76 mycroft if (drvp->DMA_mode > 0)
550 1.76 mycroft drvp->DMA_mode = 0;
551 1.76 mycroft }
552 1.76 mycroft } else
553 1.76 mycroft goto error;
554 1.76 mycroft }
555 1.102 itohy #endif /* NATA_DMA */
556 1.59 bouyer ready:
557 1.59 bouyer drvp->state = READY;
558 1.87 thorpej bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr,
559 1.59 bouyer WDCTL_4BIT);
560 1.70 bouyer delay(10); /* some drives need a little delay here */
561 1.30 bouyer }
562 1.24 bouyer /* start timeout machinery */
563 1.27 thorpej if ((sc_xfer->xs_control & XS_CTL_POLL) == 0)
564 1.50 bouyer callout_reset(&chp->ch_callout, mstohz(sc_xfer->timeout),
565 1.32 thorpej wdctimeout, chp);
566 1.59 bouyer
567 1.85 thorpej if (wdc->select)
568 1.67 thorpej wdc->select(chp, xfer->c_drive);
569 1.87 thorpej bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0,
570 1.63 thorpej WDSD_IBM | (xfer->c_drive << 4));
571 1.112 joerg switch (wdc_wait_for_unbusy(chp, ATAPI_DELAY, wait_flags)) {
572 1.59 bouyer case WDCWAIT_OK:
573 1.59 bouyer break;
574 1.59 bouyer case WDCWAIT_TOUT:
575 1.2 bouyer printf("wdc_atapi_start: not ready, st = %02x\n",
576 1.2 bouyer chp->ch_status);
577 1.3 bouyer sc_xfer->error = XS_TIMEOUT;
578 1.2 bouyer wdc_atapi_reset(chp, xfer);
579 1.2 bouyer return;
580 1.59 bouyer case WDCWAIT_THR:
581 1.59 bouyer return;
582 1.2 bouyer }
583 1.2 bouyer
584 1.2 bouyer /*
585 1.2 bouyer * Even with WDCS_ERR, the device should accept a command packet
586 1.2 bouyer * Limit length to what can be stuffed into the cylinder register
587 1.2 bouyer * (16 bits). Some CD-ROMs seem to interpret '0' as 65536,
588 1.2 bouyer * but not all devices do that and it's not obvious from the
589 1.2 bouyer * ATAPI spec that that behaviour should be expected. If more
590 1.2 bouyer * data is necessary, multiple data transfer phases will be done.
591 1.2 bouyer */
592 1.5 bouyer
593 1.92 perry wdccommand(chp, xfer->c_drive, ATAPI_PKT_CMD,
594 1.29 bouyer xfer->c_bcount <= 0xffff ? xfer->c_bcount : 0xffff,
595 1.92 perry 0, 0, 0,
596 1.102 itohy #if NATA_DMA
597 1.102 itohy (xfer->c_flags & C_DMA) ? ATAPI_PKT_CMD_FTRE_DMA :
598 1.102 itohy #endif
599 1.102 itohy 0
600 1.102 itohy );
601 1.92 perry
602 1.101 itohy #if NATA_PIOBM
603 1.101 itohy if (xfer->c_flags & C_PIOBM) {
604 1.101 itohy int error;
605 1.101 itohy int dma_flags = (sc_xfer->xs_control & XS_CTL_DATA_IN)
606 1.101 itohy ? WDC_DMA_READ : 0;
607 1.101 itohy if (xfer->c_flags & C_POLL) {
608 1.101 itohy /* XXX not supported yet --- fall back to PIO */
609 1.101 itohy xfer->c_flags &= ~C_PIOBM;
610 1.101 itohy } else {
611 1.101 itohy /* Init the DMA channel. */
612 1.101 itohy error = (*wdc->dma_init)(wdc->dma_arg,
613 1.101 itohy chp->ch_channel, xfer->c_drive,
614 1.101 itohy (char *)xfer->c_databuf,
615 1.101 itohy xfer->c_bcount,
616 1.101 itohy dma_flags | WDC_DMA_PIOBM_ATAPI);
617 1.101 itohy if (error) {
618 1.101 itohy if (error == EINVAL) {
619 1.101 itohy /*
620 1.101 itohy * We can't do DMA on this transfer
621 1.101 itohy * for some reason. Fall back to
622 1.101 itohy * PIO.
623 1.101 itohy */
624 1.101 itohy xfer->c_flags &= ~C_PIOBM;
625 1.101 itohy error = 0;
626 1.101 itohy } else {
627 1.101 itohy sc_xfer->error = XS_DRIVER_STUFFUP;
628 1.101 itohy errstring = "piobm";
629 1.101 itohy goto error;
630 1.101 itohy }
631 1.101 itohy }
632 1.101 itohy }
633 1.101 itohy }
634 1.101 itohy #endif
635 1.2 bouyer /*
636 1.92 perry * If there is no interrupt for CMD input, busy-wait for it (done in
637 1.2 bouyer * the interrupt routine. If it is a polled command, call the interrupt
638 1.2 bouyer * routine until command is done.
639 1.2 bouyer */
640 1.40 bouyer if ((sc_xfer->xs_periph->periph_cap & ATAPI_CFG_DRQ_MASK) !=
641 1.27 thorpej ATAPI_CFG_IRQ_DRQ || (sc_xfer->xs_control & XS_CTL_POLL)) {
642 1.2 bouyer /* Wait for at last 400ns for status bit to be valid */
643 1.20 bouyer DELAY(1);
644 1.20 bouyer wdc_atapi_intr(chp, xfer, 0);
645 1.21 bouyer } else {
646 1.87 thorpej chp->ch_flags |= ATACH_IRQ_WAIT;
647 1.2 bouyer }
648 1.27 thorpej if (sc_xfer->xs_control & XS_CTL_POLL) {
649 1.102 itohy #if NATA_DMA
650 1.87 thorpej if (chp->ch_flags & ATACH_DMA_WAIT) {
651 1.73 bouyer wdc_dmawait(chp, xfer, sc_xfer->timeout);
652 1.87 thorpej chp->ch_flags &= ~ATACH_DMA_WAIT;
653 1.73 bouyer }
654 1.102 itohy #endif
655 1.27 thorpej while ((sc_xfer->xs_status & XS_STS_DONE) == 0) {
656 1.2 bouyer /* Wait for at last 400ns for status bit to be valid */
657 1.20 bouyer DELAY(1);
658 1.20 bouyer wdc_atapi_intr(chp, xfer, 0);
659 1.2 bouyer }
660 1.2 bouyer }
661 1.59 bouyer return;
662 1.59 bouyer timeout:
663 1.59 bouyer printf("%s:%d:%d: %s timed out\n",
664 1.106 cube device_xname(atac->atac_dev), chp->ch_channel, xfer->c_drive,
665 1.67 thorpej errstring);
666 1.59 bouyer sc_xfer->error = XS_TIMEOUT;
667 1.87 thorpej bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr, WDCTL_4BIT);
668 1.70 bouyer delay(10); /* some drives need a little delay here */
669 1.59 bouyer wdc_atapi_reset(chp, xfer);
670 1.59 bouyer return;
671 1.59 bouyer error:
672 1.59 bouyer printf("%s:%d:%d: %s ",
673 1.106 cube device_xname(atac->atac_dev), chp->ch_channel, xfer->c_drive,
674 1.59 bouyer errstring);
675 1.59 bouyer printf("error (0x%x)\n", chp->ch_error);
676 1.59 bouyer sc_xfer->error = XS_SHORTSENSE;
677 1.59 bouyer sc_xfer->sense.atapi_sense = chp->ch_error;
678 1.87 thorpej bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr, WDCTL_4BIT);
679 1.70 bouyer delay(10); /* some drives need a little delay here */
680 1.59 bouyer wdc_atapi_reset(chp, xfer);
681 1.59 bouyer return;
682 1.2 bouyer }
683 1.2 bouyer
684 1.65 thorpej static int
685 1.87 thorpej wdc_atapi_intr(struct ata_channel *chp, struct ata_xfer *xfer, int irq)
686 1.2 bouyer {
687 1.88 thorpej struct atac_softc *atac = chp->ch_atac;
688 1.88 thorpej struct wdc_softc *wdc = CHAN_TO_WDC(chp);
689 1.87 thorpej struct wdc_regs *wdr = &wdc->regs[chp->ch_channel];
690 1.63 thorpej struct scsipi_xfer *sc_xfer = xfer->c_cmd;
691 1.63 thorpej struct ata_drive_datas *drvp = &chp->ch_drive[xfer->c_drive];
692 1.2 bouyer int len, phase, i, retries=0;
693 1.102 itohy int ire;
694 1.102 itohy #if NATA_DMA
695 1.102 itohy int error;
696 1.102 itohy #endif
697 1.102 itohy #if NATA_DMA || NATA_PIOBM
698 1.2 bouyer int dma_flags = 0;
699 1.102 itohy #endif
700 1.8 bouyer void *cmd;
701 1.2 bouyer
702 1.86 thorpej ATADEBUG_PRINT(("wdc_atapi_intr %s:%d:%d\n",
703 1.106 cube device_xname(atac->atac_dev), chp->ch_channel, drvp->drive),
704 1.67 thorpej DEBUG_INTR);
705 1.2 bouyer
706 1.2 bouyer /* Is it not a transfer, but a control operation? */
707 1.2 bouyer if (drvp->state < READY) {
708 1.2 bouyer printf("%s:%d:%d: bad state %d in wdc_atapi_intr\n",
709 1.106 cube device_xname(atac->atac_dev), chp->ch_channel,
710 1.106 cube xfer->c_drive, drvp->state);
711 1.55 provos panic("wdc_atapi_intr: bad state");
712 1.2 bouyer }
713 1.24 bouyer /*
714 1.24 bouyer * If we missed an interrupt in a PIO transfer, reset and restart.
715 1.24 bouyer * Don't try to continue transfer, we may have missed cycles.
716 1.24 bouyer */
717 1.24 bouyer if ((xfer->c_flags & (C_TIMEOU | C_DMA)) == C_TIMEOU) {
718 1.24 bouyer sc_xfer->error = XS_TIMEOUT;
719 1.24 bouyer wdc_atapi_reset(chp, xfer);
720 1.25 bouyer return 1;
721 1.92 perry }
722 1.24 bouyer
723 1.101 itohy #if NATA_PIOBM
724 1.101 itohy /* Transfer-done interrupt for busmastering PIO operation */
725 1.101 itohy if ((xfer->c_flags & C_PIOBM) && (chp->ch_flags & ATACH_PIOBM_WAIT)) {
726 1.101 itohy chp->ch_flags &= ~ATACH_PIOBM_WAIT;
727 1.101 itohy
728 1.101 itohy /* restore transfer length */
729 1.101 itohy len = xfer->c_bcount;
730 1.101 itohy if (xfer->c_lenoff < 0)
731 1.101 itohy len += xfer->c_lenoff;
732 1.101 itohy
733 1.101 itohy if (sc_xfer->xs_control & XS_CTL_DATA_IN)
734 1.101 itohy goto end_piobm_datain;
735 1.101 itohy else
736 1.101 itohy goto end_piobm_dataout;
737 1.101 itohy }
738 1.101 itohy #endif
739 1.101 itohy
740 1.64 thorpej /* Ack interrupt done in wdc_wait_for_unbusy */
741 1.85 thorpej if (wdc->select)
742 1.67 thorpej wdc->select(chp, xfer->c_drive);
743 1.87 thorpej bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0,
744 1.63 thorpej WDSD_IBM | (xfer->c_drive << 4));
745 1.64 thorpej if (wdc_wait_for_unbusy(chp,
746 1.59 bouyer (irq == 0) ? sc_xfer->timeout : 0, AT_POLL) == WDCWAIT_TOUT) {
747 1.20 bouyer if (irq && (xfer->c_flags & C_TIMEOU) == 0)
748 1.19 bouyer return 0; /* IRQ was not for us */
749 1.16 bouyer printf("%s:%d:%d: device timeout, c_bcount=%d, c_skip=%d\n",
750 1.106 cube device_xname(atac->atac_dev), chp->ch_channel,
751 1.106 cube xfer->c_drive, xfer->c_bcount, xfer->c_skip);
752 1.102 itohy #if NATA_DMA
753 1.33 bouyer if (xfer->c_flags & C_DMA) {
754 1.59 bouyer ata_dmaerr(drvp,
755 1.59 bouyer (xfer->c_flags & C_POLL) ? AT_POLL : 0);
756 1.33 bouyer }
757 1.102 itohy #endif
758 1.3 bouyer sc_xfer->error = XS_TIMEOUT;
759 1.2 bouyer wdc_atapi_reset(chp, xfer);
760 1.2 bouyer return 1;
761 1.2 bouyer }
762 1.85 thorpej if (wdc->irqack)
763 1.67 thorpej wdc->irqack(chp);
764 1.36 bouyer
765 1.102 itohy #if NATA_DMA
766 1.48 bouyer /*
767 1.48 bouyer * If we missed an IRQ and were using DMA, flag it as a DMA error
768 1.48 bouyer * and reset device.
769 1.48 bouyer */
770 1.33 bouyer if ((xfer->c_flags & C_TIMEOU) && (xfer->c_flags & C_DMA)) {
771 1.59 bouyer ata_dmaerr(drvp, (xfer->c_flags & C_POLL) ? AT_POLL : 0);
772 1.48 bouyer sc_xfer->error = XS_RESET;
773 1.48 bouyer wdc_atapi_reset(chp, xfer);
774 1.48 bouyer return (1);
775 1.33 bouyer }
776 1.102 itohy #endif
777 1.92 perry /*
778 1.8 bouyer * if the request sense command was aborted, report the short sense
779 1.8 bouyer * previously recorded, else continue normal processing
780 1.8 bouyer */
781 1.8 bouyer
782 1.102 itohy #if NATA_DMA || NATA_PIOBM
783 1.101 itohy if (xfer->c_flags & (C_DMA | C_PIOBM))
784 1.40 bouyer dma_flags = (sc_xfer->xs_control & XS_CTL_DATA_IN)
785 1.40 bouyer ? WDC_DMA_READ : 0;
786 1.102 itohy #endif
787 1.2 bouyer again:
788 1.87 thorpej len = bus_space_read_1(wdr->cmd_iot, wdr->cmd_iohs[wd_cyl_lo], 0) +
789 1.87 thorpej 256 * bus_space_read_1(wdr->cmd_iot, wdr->cmd_iohs[wd_cyl_hi], 0);
790 1.87 thorpej ire = bus_space_read_1(wdr->cmd_iot, wdr->cmd_iohs[wd_ireason], 0);
791 1.2 bouyer phase = (ire & (WDCI_CMD | WDCI_IN)) | (chp->ch_status & WDCS_DRQ);
792 1.86 thorpej ATADEBUG_PRINT(("wdc_atapi_intr: c_bcount %d len %d st 0x%x err 0x%x "
793 1.2 bouyer "ire 0x%x :", xfer->c_bcount,
794 1.2 bouyer len, chp->ch_status, chp->ch_error, ire), DEBUG_INTR);
795 1.2 bouyer
796 1.2 bouyer switch (phase) {
797 1.2 bouyer case PHASE_CMDOUT:
798 1.57 mycroft cmd = sc_xfer->cmd;
799 1.86 thorpej ATADEBUG_PRINT(("PHASE_CMDOUT\n"), DEBUG_INTR);
800 1.102 itohy #if NATA_DMA
801 1.2 bouyer /* Init the DMA channel if necessary */
802 1.2 bouyer if (xfer->c_flags & C_DMA) {
803 1.95 thorpej error = (*wdc->dma_init)(wdc->dma_arg,
804 1.67 thorpej chp->ch_channel, xfer->c_drive,
805 1.95 thorpej xfer->c_databuf, xfer->c_bcount, dma_flags);
806 1.95 thorpej if (error) {
807 1.95 thorpej if (error == EINVAL) {
808 1.95 thorpej /*
809 1.95 thorpej * We can't do DMA on this transfer
810 1.95 thorpej * for some reason. Fall back to
811 1.95 thorpej * PIO.
812 1.95 thorpej */
813 1.95 thorpej xfer->c_flags &= ~C_DMA;
814 1.95 thorpej error = 0;
815 1.95 thorpej } else {
816 1.95 thorpej sc_xfer->error = XS_DRIVER_STUFFUP;
817 1.95 thorpej break;
818 1.95 thorpej }
819 1.2 bouyer }
820 1.2 bouyer }
821 1.102 itohy #endif
822 1.75 mycroft
823 1.2 bouyer /* send packet command */
824 1.2 bouyer /* Commands are 12 or 16 bytes long. It's 32-bit aligned */
825 1.77 mycroft wdc->dataout_pio(chp, drvp->drive_flags, cmd, sc_xfer->cmdlen);
826 1.75 mycroft
827 1.102 itohy #if NATA_DMA
828 1.2 bouyer /* Start the DMA channel if necessary */
829 1.2 bouyer if (xfer->c_flags & C_DMA) {
830 1.67 thorpej (*wdc->dma_start)(wdc->dma_arg,
831 1.67 thorpej chp->ch_channel, xfer->c_drive);
832 1.87 thorpej chp->ch_flags |= ATACH_DMA_WAIT;
833 1.2 bouyer }
834 1.102 itohy #endif
835 1.2 bouyer
836 1.27 thorpej if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) {
837 1.87 thorpej chp->ch_flags |= ATACH_IRQ_WAIT;
838 1.2 bouyer }
839 1.2 bouyer return 1;
840 1.2 bouyer
841 1.2 bouyer case PHASE_DATAOUT:
842 1.2 bouyer /* write data */
843 1.86 thorpej ATADEBUG_PRINT(("PHASE_DATAOUT\n"), DEBUG_INTR);
844 1.102 itohy #if NATA_DMA
845 1.27 thorpej if ((sc_xfer->xs_control & XS_CTL_DATA_OUT) == 0 ||
846 1.2 bouyer (xfer->c_flags & C_DMA) != 0) {
847 1.15 bouyer printf("wdc_atapi_intr: bad data phase DATAOUT\n");
848 1.2 bouyer if (xfer->c_flags & C_DMA) {
849 1.59 bouyer ata_dmaerr(drvp,
850 1.59 bouyer (xfer->c_flags & C_POLL) ? AT_POLL : 0);
851 1.2 bouyer }
852 1.3 bouyer sc_xfer->error = XS_TIMEOUT;
853 1.2 bouyer wdc_atapi_reset(chp, xfer);
854 1.2 bouyer return 1;
855 1.2 bouyer }
856 1.102 itohy #endif
857 1.101 itohy xfer->c_lenoff = len - xfer->c_bcount;
858 1.2 bouyer if (xfer->c_bcount < len) {
859 1.2 bouyer printf("wdc_atapi_intr: warning: write only "
860 1.2 bouyer "%d of %d requested bytes\n", xfer->c_bcount, len);
861 1.101 itohy len = xfer->c_bcount;
862 1.101 itohy }
863 1.101 itohy
864 1.101 itohy #if NATA_PIOBM
865 1.101 itohy if (xfer->c_flags & C_PIOBM) {
866 1.101 itohy /* start the busmastering PIO */
867 1.101 itohy (*wdc->piobm_start)(wdc->dma_arg,
868 1.101 itohy chp->ch_channel, xfer->c_drive,
869 1.101 itohy xfer->c_skip, len, WDC_PIOBM_XFER_IRQ);
870 1.101 itohy chp->ch_flags |= ATACH_DMA_WAIT | ATACH_IRQ_WAIT |
871 1.101 itohy ATACH_PIOBM_WAIT;
872 1.101 itohy return 1;
873 1.2 bouyer }
874 1.101 itohy #endif
875 1.101 itohy wdc->dataout_pio(chp, drvp->drive_flags,
876 1.120 tsutsui (char *)xfer->c_databuf + xfer->c_skip, len);
877 1.101 itohy
878 1.101 itohy #if NATA_PIOBM
879 1.101 itohy end_piobm_dataout:
880 1.101 itohy #endif
881 1.101 itohy for (i = xfer->c_lenoff; i > 0; i -= 2)
882 1.101 itohy bus_space_write_2(wdr->cmd_iot,
883 1.101 itohy wdr->cmd_iohs[wd_data], 0, 0);
884 1.101 itohy
885 1.101 itohy xfer->c_skip += len;
886 1.101 itohy xfer->c_bcount -= len;
887 1.27 thorpej if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) {
888 1.87 thorpej chp->ch_flags |= ATACH_IRQ_WAIT;
889 1.2 bouyer }
890 1.2 bouyer return 1;
891 1.2 bouyer
892 1.2 bouyer case PHASE_DATAIN:
893 1.2 bouyer /* Read data */
894 1.86 thorpej ATADEBUG_PRINT(("PHASE_DATAIN\n"), DEBUG_INTR);
895 1.102 itohy #if NATA_DMA
896 1.92 perry if ((sc_xfer->xs_control & XS_CTL_DATA_IN) == 0 ||
897 1.2 bouyer (xfer->c_flags & C_DMA) != 0) {
898 1.15 bouyer printf("wdc_atapi_intr: bad data phase DATAIN\n");
899 1.2 bouyer if (xfer->c_flags & C_DMA) {
900 1.59 bouyer ata_dmaerr(drvp,
901 1.59 bouyer (xfer->c_flags & C_POLL) ? AT_POLL : 0);
902 1.2 bouyer }
903 1.3 bouyer sc_xfer->error = XS_TIMEOUT;
904 1.2 bouyer wdc_atapi_reset(chp, xfer);
905 1.2 bouyer return 1;
906 1.2 bouyer }
907 1.102 itohy #endif
908 1.101 itohy xfer->c_lenoff = len - xfer->c_bcount;
909 1.2 bouyer if (xfer->c_bcount < len) {
910 1.2 bouyer printf("wdc_atapi_intr: warning: reading only "
911 1.2 bouyer "%d of %d bytes\n", xfer->c_bcount, len);
912 1.101 itohy len = xfer->c_bcount;
913 1.101 itohy }
914 1.101 itohy
915 1.101 itohy #if NATA_PIOBM
916 1.101 itohy if (xfer->c_flags & C_PIOBM) {
917 1.101 itohy /* start the busmastering PIO */
918 1.101 itohy (*wdc->piobm_start)(wdc->dma_arg,
919 1.101 itohy chp->ch_channel, xfer->c_drive,
920 1.101 itohy xfer->c_skip, len, WDC_PIOBM_XFER_IRQ);
921 1.101 itohy chp->ch_flags |= ATACH_DMA_WAIT | ATACH_IRQ_WAIT |
922 1.101 itohy ATACH_PIOBM_WAIT;
923 1.101 itohy return 1;
924 1.101 itohy }
925 1.101 itohy #endif
926 1.101 itohy wdc->datain_pio(chp, drvp->drive_flags,
927 1.101 itohy (char *)xfer->c_databuf + xfer->c_skip, len);
928 1.101 itohy
929 1.101 itohy #if NATA_PIOBM
930 1.101 itohy end_piobm_datain:
931 1.101 itohy #endif
932 1.101 itohy if (xfer->c_lenoff > 0)
933 1.2 bouyer wdcbit_bucket(chp, len - xfer->c_bcount);
934 1.101 itohy
935 1.101 itohy xfer->c_skip += len;
936 1.101 itohy xfer->c_bcount -= len;
937 1.27 thorpej if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) {
938 1.87 thorpej chp->ch_flags |= ATACH_IRQ_WAIT;
939 1.2 bouyer }
940 1.2 bouyer return 1;
941 1.2 bouyer
942 1.2 bouyer case PHASE_ABORTED:
943 1.2 bouyer case PHASE_COMPLETED:
944 1.86 thorpej ATADEBUG_PRINT(("PHASE_COMPLETED\n"), DEBUG_INTR);
945 1.102 itohy #if NATA_DMA
946 1.2 bouyer if (xfer->c_flags & C_DMA) {
947 1.40 bouyer xfer->c_bcount -= sc_xfer->datalen;
948 1.40 bouyer }
949 1.102 itohy #endif
950 1.40 bouyer sc_xfer->resid = xfer->c_bcount;
951 1.45 bouyer wdc_atapi_phase_complete(xfer);
952 1.45 bouyer return(1);
953 1.2 bouyer
954 1.2 bouyer default:
955 1.2 bouyer if (++retries<500) {
956 1.2 bouyer DELAY(100);
957 1.87 thorpej chp->ch_status = bus_space_read_1(wdr->cmd_iot,
958 1.87 thorpej wdr->cmd_iohs[wd_status], 0);
959 1.87 thorpej chp->ch_error = bus_space_read_1(wdr->cmd_iot,
960 1.87 thorpej wdr->cmd_iohs[wd_error], 0);
961 1.2 bouyer goto again;
962 1.2 bouyer }
963 1.2 bouyer printf("wdc_atapi_intr: unknown phase 0x%x\n", phase);
964 1.2 bouyer if (chp->ch_status & WDCS_ERR) {
965 1.8 bouyer sc_xfer->error = XS_SHORTSENSE;
966 1.2 bouyer sc_xfer->sense.atapi_sense = chp->ch_error;
967 1.2 bouyer } else {
968 1.102 itohy #if NATA_DMA
969 1.33 bouyer if (xfer->c_flags & C_DMA) {
970 1.59 bouyer ata_dmaerr(drvp,
971 1.59 bouyer (xfer->c_flags & C_POLL) ? AT_POLL : 0);
972 1.33 bouyer }
973 1.102 itohy #endif
974 1.16 bouyer sc_xfer->error = XS_RESET;
975 1.16 bouyer wdc_atapi_reset(chp, xfer);
976 1.16 bouyer return (1);
977 1.2 bouyer }
978 1.2 bouyer }
979 1.86 thorpej ATADEBUG_PRINT(("wdc_atapi_intr: wdc_atapi_done() (end), error 0x%x "
980 1.2 bouyer "sense 0x%x\n", sc_xfer->error, sc_xfer->sense.atapi_sense),
981 1.2 bouyer DEBUG_INTR);
982 1.2 bouyer wdc_atapi_done(chp, xfer);
983 1.2 bouyer return (1);
984 1.2 bouyer }
985 1.2 bouyer
986 1.65 thorpej static void
987 1.65 thorpej wdc_atapi_phase_complete(struct ata_xfer *xfer)
988 1.45 bouyer {
989 1.87 thorpej struct ata_channel *chp = xfer->c_chp;
990 1.88 thorpej struct atac_softc *atac = chp->ch_atac;
991 1.102 itohy #if NATA_DMA || NATA_PIOBM
992 1.88 thorpej struct wdc_softc *wdc = CHAN_TO_WDC(chp);
993 1.102 itohy #endif
994 1.63 thorpej struct scsipi_xfer *sc_xfer = xfer->c_cmd;
995 1.63 thorpej struct ata_drive_datas *drvp = &chp->ch_drive[xfer->c_drive];
996 1.45 bouyer
997 1.45 bouyer /* wait for DSC if needed */
998 1.118 bouyer if (drvp->drive_flags & ATA_DRIVE_ATAPIDSCW) {
999 1.86 thorpej ATADEBUG_PRINT(("wdc_atapi_phase_complete(%s:%d:%d) "
1000 1.106 cube "polldsc %d\n", device_xname(atac->atac_dev),
1001 1.106 cube chp->ch_channel,
1002 1.63 thorpej xfer->c_drive, xfer->c_dscpoll), DEBUG_XFERS);
1003 1.57 mycroft #if 1
1004 1.57 mycroft if (cold)
1005 1.57 mycroft panic("wdc_atapi_phase_complete: cold");
1006 1.57 mycroft #endif
1007 1.59 bouyer if (wdcwait(chp, WDCS_DSC, WDCS_DSC, 10,
1008 1.59 bouyer AT_POLL) == WDCWAIT_TOUT) {
1009 1.57 mycroft /* 10ms not enough, try again in 1 tick */
1010 1.92 perry if (xfer->c_dscpoll++ >
1011 1.57 mycroft mstohz(sc_xfer->timeout)) {
1012 1.59 bouyer printf("%s:%d:%d: wait_for_dsc "
1013 1.59 bouyer "failed\n",
1014 1.106 cube device_xname(atac->atac_dev),
1015 1.67 thorpej chp->ch_channel, xfer->c_drive);
1016 1.45 bouyer sc_xfer->error = XS_TIMEOUT;
1017 1.45 bouyer wdc_atapi_reset(chp, xfer);
1018 1.59 bouyer return;
1019 1.57 mycroft } else
1020 1.45 bouyer callout_reset(&chp->ch_callout, 1,
1021 1.45 bouyer wdc_atapi_polldsc, xfer);
1022 1.57 mycroft return;
1023 1.45 bouyer }
1024 1.45 bouyer }
1025 1.45 bouyer
1026 1.45 bouyer /*
1027 1.92 perry * Some drive occasionally set WDCS_ERR with
1028 1.45 bouyer * "ATA illegal length indication" in the error
1029 1.45 bouyer * register. If we read some data the sense is valid
1030 1.45 bouyer * anyway, so don't report the error.
1031 1.45 bouyer */
1032 1.45 bouyer if (chp->ch_status & WDCS_ERR &&
1033 1.45 bouyer ((sc_xfer->xs_control & XS_CTL_REQSENSE) == 0 ||
1034 1.45 bouyer sc_xfer->resid == sc_xfer->datalen)) {
1035 1.45 bouyer /* save the short sense */
1036 1.45 bouyer sc_xfer->error = XS_SHORTSENSE;
1037 1.45 bouyer sc_xfer->sense.atapi_sense = chp->ch_error;
1038 1.45 bouyer if ((sc_xfer->xs_periph->periph_quirks &
1039 1.45 bouyer PQUIRK_NOSENSE) == 0) {
1040 1.45 bouyer /* ask scsipi to send a REQUEST_SENSE */
1041 1.45 bouyer sc_xfer->error = XS_BUSY;
1042 1.45 bouyer sc_xfer->status = SCSI_CHECK;
1043 1.102 itohy }
1044 1.102 itohy #if NATA_DMA || NATA_PIOBM
1045 1.102 itohy else if (wdc->dma_status &
1046 1.45 bouyer (WDC_DMAST_NOIRQ | WDC_DMAST_ERR)) {
1047 1.102 itohy #if NATA_DMA
1048 1.59 bouyer ata_dmaerr(drvp,
1049 1.59 bouyer (xfer->c_flags & C_POLL) ? AT_POLL : 0);
1050 1.102 itohy #endif
1051 1.45 bouyer sc_xfer->error = XS_RESET;
1052 1.45 bouyer wdc_atapi_reset(chp, xfer);
1053 1.45 bouyer return;
1054 1.45 bouyer }
1055 1.102 itohy #endif
1056 1.45 bouyer }
1057 1.45 bouyer if (xfer->c_bcount != 0) {
1058 1.86 thorpej ATADEBUG_PRINT(("wdc_atapi_intr: bcount value is "
1059 1.45 bouyer "%d after io\n", xfer->c_bcount), DEBUG_XFERS);
1060 1.45 bouyer }
1061 1.45 bouyer #ifdef DIAGNOSTIC
1062 1.45 bouyer if (xfer->c_bcount < 0) {
1063 1.45 bouyer printf("wdc_atapi_intr warning: bcount value "
1064 1.45 bouyer "is %d after io\n", xfer->c_bcount);
1065 1.45 bouyer }
1066 1.45 bouyer #endif
1067 1.86 thorpej ATADEBUG_PRINT(("wdc_atapi_phase_complete: wdc_atapi_done(), "
1068 1.45 bouyer "error 0x%x sense 0x%x\n", sc_xfer->error,
1069 1.45 bouyer sc_xfer->sense.atapi_sense), DEBUG_INTR);
1070 1.45 bouyer wdc_atapi_done(chp, xfer);
1071 1.45 bouyer }
1072 1.45 bouyer
1073 1.65 thorpej static void
1074 1.87 thorpej wdc_atapi_done(struct ata_channel *chp, struct ata_xfer *xfer)
1075 1.2 bouyer {
1076 1.88 thorpej struct atac_softc *atac = chp->ch_atac;
1077 1.63 thorpej struct scsipi_xfer *sc_xfer = xfer->c_cmd;
1078 1.74 bouyer int drive = xfer->c_drive;
1079 1.2 bouyer
1080 1.86 thorpej ATADEBUG_PRINT(("wdc_atapi_done %s:%d:%d: flags 0x%x\n",
1081 1.106 cube device_xname(atac->atac_dev), chp->ch_channel, xfer->c_drive,
1082 1.91 reinoud (u_int)xfer->c_flags), DEBUG_XFERS);
1083 1.32 thorpej callout_stop(&chp->ch_callout);
1084 1.73 bouyer /* mark controller inactive and free the command */
1085 1.73 bouyer chp->ch_queue->active_xfer = NULL;
1086 1.81 thorpej ata_free_xfer(chp, xfer);
1087 1.40 bouyer
1088 1.118 bouyer if (chp->ch_drive[drive].drive_flags & ATA_DRIVE_WAITDRAIN) {
1089 1.74 bouyer sc_xfer->error = XS_DRIVER_STUFFUP;
1090 1.118 bouyer chp->ch_drive[drive].drive_flags &= ~ATA_DRIVE_WAITDRAIN;
1091 1.74 bouyer wakeup(&chp->ch_queue->active_xfer);
1092 1.74 bouyer }
1093 1.74 bouyer
1094 1.86 thorpej ATADEBUG_PRINT(("wdc_atapi_done: scsipi_done\n"), DEBUG_XFERS);
1095 1.25 bouyer scsipi_done(sc_xfer);
1096 1.86 thorpej ATADEBUG_PRINT(("atastart from wdc_atapi_done, flags 0x%x\n",
1097 1.4 bouyer chp->ch_flags), DEBUG_XFERS);
1098 1.84 thorpej atastart(chp);
1099 1.2 bouyer }
1100 1.2 bouyer
1101 1.65 thorpej static void
1102 1.87 thorpej wdc_atapi_reset(struct ata_channel *chp, struct ata_xfer *xfer)
1103 1.2 bouyer {
1104 1.88 thorpej struct atac_softc *atac = chp->ch_atac;
1105 1.63 thorpej struct ata_drive_datas *drvp = &chp->ch_drive[xfer->c_drive];
1106 1.63 thorpej struct scsipi_xfer *sc_xfer = xfer->c_cmd;
1107 1.2 bouyer
1108 1.63 thorpej wdccommandshort(chp, xfer->c_drive, ATAPI_SOFT_RESET);
1109 1.2 bouyer drvp->state = 0;
1110 1.64 thorpej if (wdc_wait_for_unbusy(chp, WDC_RESET_WAIT, AT_POLL) != 0) {
1111 1.2 bouyer printf("%s:%d:%d: reset failed\n",
1112 1.106 cube device_xname(atac->atac_dev), chp->ch_channel,
1113 1.63 thorpej xfer->c_drive);
1114 1.2 bouyer sc_xfer->error = XS_SELTIMEOUT;
1115 1.2 bouyer }
1116 1.2 bouyer wdc_atapi_done(chp, xfer);
1117 1.2 bouyer return;
1118 1.45 bouyer }
1119 1.45 bouyer
1120 1.59 bouyer static void
1121 1.65 thorpej wdc_atapi_polldsc(void *arg)
1122 1.45 bouyer {
1123 1.65 thorpej
1124 1.45 bouyer wdc_atapi_phase_complete(arg);
1125 1.2 bouyer }
1126