wdc.c revision 1.22 1 1.22 mycroft /* $NetBSD: wdc.c,v 1.22 1998/04/26 06:03:24 mycroft Exp $ */
2 1.2 bouyer
3 1.2 bouyer /*
4 1.22 mycroft * Copyright (c) 1994, 1995, 1998 Charles M. Hannum. All rights reserved.
5 1.2 bouyer *
6 1.2 bouyer * DMA and multi-sector PIO handling are derived from code contributed by
7 1.2 bouyer * Onno van der Linden.
8 1.2 bouyer *
9 1.2 bouyer * Atapi support added by Manuel Bouyer.
10 1.2 bouyer *
11 1.12 cgd * Bus_space-ified by Christopher G. Demetriou.
12 1.12 cgd *
13 1.2 bouyer * Redistribution and use in source and binary forms, with or without
14 1.2 bouyer * modification, are permitted provided that the following conditions
15 1.2 bouyer * are met:
16 1.2 bouyer * 1. Redistributions of source code must retain the above copyright
17 1.2 bouyer * notice, this list of conditions and the following disclaimer.
18 1.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
19 1.2 bouyer * notice, this list of conditions and the following disclaimer in the
20 1.2 bouyer * documentation and/or other materials provided with the distribution.
21 1.2 bouyer * 3. All advertising materials mentioning features or use of this software
22 1.2 bouyer * must display the following acknowledgement:
23 1.2 bouyer * This product includes software developed by Charles M. Hannum.
24 1.2 bouyer * 4. The name of the author may not be used to endorse or promote products
25 1.2 bouyer * derived from this software without specific prior written permission.
26 1.2 bouyer *
27 1.2 bouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28 1.2 bouyer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29 1.2 bouyer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 1.2 bouyer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
31 1.2 bouyer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 1.2 bouyer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 1.2 bouyer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 1.2 bouyer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 1.2 bouyer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36 1.2 bouyer * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 1.2 bouyer */
38 1.2 bouyer
39 1.12 cgd /*
40 1.12 cgd * CODE UNTESTED IN THE CURRENT REVISION:
41 1.12 cgd * * DMA
42 1.12 cgd * * 32-bit data port access.
43 1.12 cgd */
44 1.12 cgd
45 1.2 bouyer #include <sys/param.h>
46 1.2 bouyer #include <sys/systm.h>
47 1.2 bouyer #include <sys/kernel.h>
48 1.2 bouyer #include <sys/conf.h>
49 1.2 bouyer #include <sys/file.h>
50 1.2 bouyer #include <sys/stat.h>
51 1.2 bouyer #include <sys/ioctl.h>
52 1.2 bouyer #include <sys/buf.h>
53 1.2 bouyer #include <sys/uio.h>
54 1.2 bouyer #include <sys/malloc.h>
55 1.2 bouyer #include <sys/device.h>
56 1.2 bouyer #include <sys/disklabel.h>
57 1.2 bouyer #include <sys/disk.h>
58 1.2 bouyer #include <sys/syslog.h>
59 1.2 bouyer #include <sys/proc.h>
60 1.2 bouyer
61 1.2 bouyer #include <vm/vm.h>
62 1.2 bouyer
63 1.2 bouyer #include <machine/intr.h>
64 1.2 bouyer #include <machine/bus.h>
65 1.2 bouyer
66 1.17 sakamoto #ifndef __BUS_SPACE_HAS_STREAM_METHODS
67 1.16 sakamoto #define bus_space_write_multi_stream_2 bus_space_write_multi_2
68 1.16 sakamoto #define bus_space_write_multi_stream_4 bus_space_write_multi_4
69 1.16 sakamoto #define bus_space_read_multi_stream_2 bus_space_read_multi_2
70 1.16 sakamoto #define bus_space_read_multi_stream_4 bus_space_read_multi_4
71 1.17 sakamoto #endif /* __BUS_SPACE_HAS_STREAM_METHODS */
72 1.16 sakamoto
73 1.12 cgd #include <dev/ic/wdcreg.h>
74 1.12 cgd #include <dev/ic/wdcvar.h>
75 1.12 cgd #include <dev/ata/wdlink.h>
76 1.2 bouyer #include "atapibus.h"
77 1.12 cgd #include "wdc.h"
78 1.2 bouyer
79 1.2 bouyer #if NATAPIBUS > 0
80 1.2 bouyer #include <dev/scsipi/scsipi_all.h>
81 1.12 cgd #include <dev/scsipi/atapi_all.h>
82 1.2 bouyer #include <dev/scsipi/atapiconf.h>
83 1.2 bouyer #endif
84 1.2 bouyer
85 1.2 bouyer #define WAITTIME (10 * hz) /* time to wait for a completion */
86 1.2 bouyer /* this is a lot for hard drives, but not for cdroms */
87 1.2 bouyer #define RECOVERYTIME hz/2
88 1.2 bouyer #define WDCDELAY 100
89 1.2 bouyer #define WDCNDELAY 100000 /* delay = 100us; so 10s for a controller state change */
90 1.2 bouyer #if 0
91 1.2 bouyer /* If you enable this, it will report any delays more than 100us * N long. */
92 1.2 bouyer #define WDCNDELAY_DEBUG 50
93 1.2 bouyer #endif
94 1.2 bouyer
95 1.2 bouyer #define WDIORETRIES 5 /* number of retries before giving up */
96 1.2 bouyer
97 1.2 bouyer #define WDPART(dev) DISKPART(dev)
98 1.2 bouyer
99 1.2 bouyer LIST_HEAD(xfer_free_list, wdc_xfer) xfer_free_list;
100 1.2 bouyer
101 1.12 cgd int wdc_init_controller __P((struct wdc_softc *,
102 1.12 cgd const struct wdc_attachment_data *));
103 1.2 bouyer void wdcstart __P((struct wdc_softc *));
104 1.12 cgd int wdcreset __P((struct wdc_softc *, int));
105 1.2 bouyer #define VERBOSE 1
106 1.2 bouyer #define SILENT 0
107 1.2 bouyer void wdcrestart __P((void *arg));
108 1.2 bouyer void wdcunwedge __P((struct wdc_softc *));
109 1.2 bouyer void wdctimeout __P((void *arg));
110 1.12 cgd int wdccontrol __P((struct wdc_softc*, struct wd_link *));
111 1.2 bouyer void wdc_free_xfer __P((struct wdc_xfer *));
112 1.2 bouyer void wdcerror __P((struct wdc_softc*, char *));
113 1.2 bouyer void wdcbit_bucket __P(( struct wdc_softc *, int));
114 1.2 bouyer #if NWD > 0
115 1.12 cgd int wdprint __P((void *, const char *));
116 1.12 cgd int wdsetctlr __P((struct wd_link *));
117 1.12 cgd int wdc_ata_intr __P((struct wdc_softc *,struct wdc_xfer *));
118 1.2 bouyer void wdc_ata_start __P((struct wdc_softc *,struct wdc_xfer *));
119 1.2 bouyer void wdc_ata_done __P((struct wdc_softc *, struct wdc_xfer *));
120 1.2 bouyer #endif /* NWD > 0 */
121 1.2 bouyer #if NATAPIBUS > 0
122 1.2 bouyer void wdc_atapi_minphys __P((struct buf *bp));
123 1.2 bouyer void wdc_atapi_start __P((struct wdc_softc *,struct wdc_xfer *));
124 1.12 cgd int wdc_atapi_intr __P((struct wdc_softc *, struct wdc_xfer *));
125 1.2 bouyer void wdc_atapi_done __P((struct wdc_softc *, struct wdc_xfer *));
126 1.12 cgd int wdc_atapi_send_command_packet __P((struct scsipi_xfer *sc_xfer));
127 1.2 bouyer #define MAX_SIZE MAXPHYS /* XXX */
128 1.2 bouyer #endif
129 1.2 bouyer
130 1.8 bouyer #ifdef ATAPI_DEBUG2
131 1.2 bouyer static int wdc_nxfer;
132 1.2 bouyer #endif
133 1.2 bouyer
134 1.2 bouyer #ifdef WDDEBUG
135 1.2 bouyer #define WDDEBUG_PRINT(args) printf args
136 1.2 bouyer #else
137 1.2 bouyer #define WDDEBUG_PRINT(args)
138 1.2 bouyer #endif
139 1.2 bouyer
140 1.2 bouyer #if NATAPIBUS > 0
141 1.2 bouyer static struct scsipi_adapter wdc_switch = {
142 1.2 bouyer wdc_atapi_send_command_packet,
143 1.2 bouyer wdc_atapi_minphys,
144 1.2 bouyer 0,
145 1.2 bouyer 0
146 1.2 bouyer };
147 1.2 bouyer #endif
148 1.2 bouyer
149 1.12 cgd /*
150 1.12 cgd * wdc_init_controller: Does a quick probe/init of the controller.
151 1.12 cgd *
152 1.12 cgd * Return values:
153 1.12 cgd * 0 No controller present (as far as it can tell).
154 1.12 cgd * >0 Controller present and seemingly functional.
155 1.12 cgd * <0 Controller present, but not working correctly.
156 1.12 cgd */
157 1.2 bouyer int
158 1.12 cgd wdc_init_controller(wdc, adp)
159 1.12 cgd struct wdc_softc *wdc;
160 1.12 cgd const struct wdc_attachment_data *adp;
161 1.12 cgd {
162 1.12 cgd bus_space_tag_t iot;
163 1.12 cgd bus_space_handle_t ioh;
164 1.2 bouyer
165 1.12 cgd iot = wdc->sc_iot;
166 1.12 cgd ioh = wdc->sc_ioh;
167 1.2 bouyer
168 1.7 bouyer if (wdcreset(wdc, SILENT) != 0) {
169 1.2 bouyer /*
170 1.12 cgd * If the reset failed, there is no master. test for
171 1.12 cgd * ATAPI signature on the slave device. If no ATAPI
172 1.12 cgd * slave, wait 5s and retry a reset.
173 1.2 bouyer */
174 1.12 cgd bus_space_write_1(iot, ioh, wd_sdh, WDSD_IBM | 0x10); /*slave*/
175 1.12 cgd if (bus_space_read_1(iot, ioh, wd_cyl_lo) == 0x14 &&
176 1.12 cgd bus_space_read_1(iot, ioh, wd_cyl_hi) == 0xeb) {
177 1.7 bouyer wdc->sc_flags |= WDCF_ONESLAVE;
178 1.7 bouyer goto drivefound;
179 1.7 bouyer } else {
180 1.2 bouyer delay(500000);
181 1.2 bouyer if (wdcreset(wdc, SILENT) != 0)
182 1.12 cgd return (0);
183 1.2 bouyer }
184 1.7 bouyer }
185 1.9 fvdl delay(1000);
186 1.12 cgd
187 1.12 cgd /*
188 1.12 cgd * Reset succeeded. Test for ATAPI signature on both master
189 1.12 cgd * and slave.
190 1.12 cgd */
191 1.12 cgd if (bus_space_read_1(iot, ioh, wd_cyl_lo) == 0x14 &&
192 1.12 cgd bus_space_read_1(iot, ioh, wd_cyl_hi) == 0xeb)
193 1.7 bouyer goto drivefound;
194 1.12 cgd bus_space_write_1(iot, ioh, wd_sdh, WDSD_IBM | 0x10);
195 1.12 cgd if (bus_space_read_1(iot, ioh, wd_cyl_lo) == 0x14 &&
196 1.12 cgd bus_space_read_1(iot, ioh, wd_cyl_hi) == 0xeb) {
197 1.2 bouyer wdc->sc_flags |= WDCF_ONESLAVE;
198 1.7 bouyer goto drivefound;
199 1.2 bouyer }
200 1.12 cgd
201 1.12 cgd /*
202 1.12 cgd * Test non-ATAPI registers. Error register not writable,
203 1.12 cgd * but all of cyllo is.
204 1.12 cgd */
205 1.12 cgd bus_space_write_1(iot, ioh, wd_sdh, WDSD_IBM);
206 1.12 cgd bus_space_write_1(iot, ioh, wd_error, 0x58);
207 1.12 cgd bus_space_write_1(iot, ioh, wd_cyl_lo, 0xa5);
208 1.12 cgd if (bus_space_read_1(iot, ioh, wd_error) != 0x58 &&
209 1.12 cgd bus_space_read_1(iot, ioh, wd_cyl_lo) == 0xa5)
210 1.12 cgd goto drivefound;
211 1.12 cgd
212 1.12 cgd /*
213 1.12 cgd * If no drives found, but the resets succeeded, we claim to
214 1.12 cgd * have the controller, at least.
215 1.12 cgd */
216 1.12 cgd return (1);
217 1.12 cgd
218 1.7 bouyer drivefound:
219 1.2 bouyer /* Select drive 0 or ATAPI slave device */
220 1.2 bouyer if (wdc->sc_flags & WDCF_ONESLAVE)
221 1.12 cgd bus_space_write_1(iot, ioh, wd_sdh, WDSD_IBM | 0x10);
222 1.2 bouyer else
223 1.12 cgd bus_space_write_1(iot, ioh, wd_sdh, WDSD_IBM);
224 1.2 bouyer
225 1.2 bouyer /* Wait for controller to become ready. */
226 1.2 bouyer if (wait_for_unbusy(wdc) < 0)
227 1.12 cgd return (-1);
228 1.2 bouyer
229 1.2 bouyer /* Start drive diagnostics. */
230 1.12 cgd bus_space_write_1(iot, ioh, wd_command, WDCC_DIAGNOSE);
231 1.2 bouyer
232 1.2 bouyer /* Wait for command to complete. */
233 1.2 bouyer if (wait_for_unbusy(wdc) < 0)
234 1.12 cgd return (-1);
235 1.2 bouyer
236 1.2 bouyer return 1;
237 1.2 bouyer }
238 1.2 bouyer
239 1.12 cgd int
240 1.12 cgd wdcprobe(adp)
241 1.12 cgd const struct wdc_attachment_data *adp;
242 1.12 cgd {
243 1.12 cgd struct wdc_softc _wdc, *wdc = &_wdc; /* XXX EWWWWW! */
244 1.12 cgd int rv;
245 1.12 cgd
246 1.12 cgd bzero(wdc, sizeof *wdc);
247 1.12 cgd strcpy(wdc->sc_dev.dv_xname, "wdcprobe");
248 1.12 cgd wdc->sc_adp = adp;
249 1.12 cgd
250 1.12 cgd rv = wdc_init_controller(wdc, adp);
251 1.12 cgd
252 1.12 cgd if (rv < 0)
253 1.12 cgd rv = 1;
254 1.12 cgd return (rv);
255 1.12 cgd }
256 1.12 cgd
257 1.2 bouyer void
258 1.12 cgd wdcattach(wdc, adp)
259 1.12 cgd struct wdc_softc *wdc;
260 1.12 cgd const struct wdc_attachment_data *adp;
261 1.2 bouyer {
262 1.2 bouyer #if NWD > 0
263 1.2 bouyer int drive;
264 1.2 bouyer #endif
265 1.2 bouyer
266 1.12 cgd wdc->sc_adp = adp;
267 1.12 cgd if (wdc_init_controller(wdc, adp) <= 0) {
268 1.15 mycroft printf("%s: controller wouldn't initialize properly\n",
269 1.15 mycroft wdc->sc_dev.dv_xname);
270 1.12 cgd return;
271 1.12 cgd }
272 1.12 cgd
273 1.2 bouyer TAILQ_INIT(&wdc->sc_xfer);
274 1.2 bouyer
275 1.12 cgd if (wdc->sc_cap & WDC_CAPABILITY_DMA)
276 1.12 cgd (*wdc->sc_dma_setup)(wdc->sc_dma_arg);
277 1.2 bouyer
278 1.8 bouyer #ifdef ATAPI_DEBUG2
279 1.2 bouyer wdc_nxfer = 0;
280 1.2 bouyer #endif
281 1.2 bouyer
282 1.2 bouyer #if NATAPIBUS > 0
283 1.2 bouyer /*
284 1.2 bouyer * Attach an ATAPI bus, if configured.
285 1.2 bouyer */
286 1.2 bouyer wdc->ab_link = malloc(sizeof(struct scsipi_link), M_DEVBUF, M_NOWAIT);
287 1.2 bouyer if (wdc->ab_link == NULL) {
288 1.12 cgd printf("%s: can't allocate ATAPI link\n",
289 1.12 cgd wdc->sc_dev.dv_xname);
290 1.2 bouyer return;
291 1.2 bouyer }
292 1.2 bouyer bzero(wdc->ab_link,sizeof(struct scsipi_link));
293 1.2 bouyer wdc->ab_link->type = BUS_ATAPI;
294 1.2 bouyer wdc->ab_link->openings = 1;
295 1.2 bouyer wdc->ab_link->scsipi_atapi.type = ATAPI;
296 1.2 bouyer wdc->ab_link->scsipi_atapi.channel = 0;
297 1.2 bouyer wdc->ab_link->adapter_softc = (caddr_t)wdc;
298 1.2 bouyer wdc->ab_link->adapter = &wdc_switch;
299 1.12 cgd (void)config_found(&wdc->sc_dev, (void *)wdc->ab_link, NULL);
300 1.2 bouyer #endif /* NATAPIBUS > 0 */
301 1.2 bouyer #if NWD > 0
302 1.2 bouyer /*
303 1.2 bouyer * Attach standard IDE/ESDI/etc. disks to the controller.
304 1.2 bouyer */
305 1.2 bouyer for (drive = 0; drive < 2; drive++) {
306 1.4 bouyer /* if a disk is already present, skip */
307 1.4 bouyer if ((wdc->sc_drives_mask & (1 << drive)) != 0) {
308 1.4 bouyer continue;
309 1.4 bouyer }
310 1.2 bouyer /* controller active while autoconf */
311 1.2 bouyer wdc->sc_flags |= WDCF_ACTIVE;
312 1.2 bouyer
313 1.2 bouyer if (wdccommandshort(wdc, drive, WDCC_RECAL) != 0 ||
314 1.2 bouyer wait_for_ready(wdc) != 0) {
315 1.2 bouyer wdc->d_link[drive] = NULL;
316 1.2 bouyer wdc->sc_flags &= ~WDCF_ACTIVE;
317 1.2 bouyer } else {
318 1.2 bouyer wdc->sc_flags &= ~WDCF_ACTIVE;
319 1.2 bouyer wdc->d_link[drive] = malloc(sizeof(struct wd_link),
320 1.2 bouyer M_DEVBUF, M_NOWAIT);
321 1.2 bouyer if (wdc->d_link[drive] == NULL) {
322 1.2 bouyer printf("%s: can't allocate link for drive %d\n",
323 1.12 cgd wdc->sc_dev.dv_xname, drive);
324 1.2 bouyer continue;
325 1.2 bouyer }
326 1.2 bouyer bzero(wdc->d_link[drive],sizeof(struct wd_link));
327 1.2 bouyer wdc->d_link[drive]->type = ATA;
328 1.2 bouyer wdc->d_link[drive]->wdc_softc =(caddr_t) wdc;
329 1.2 bouyer wdc->d_link[drive]->drive = drive;
330 1.12 cgd if (wdc->sc_cap & WDC_CAPABILITY_DMA)
331 1.2 bouyer wdc->d_link[drive]->sc_mode = WDM_DMA;
332 1.2 bouyer else
333 1.2 bouyer wdc->d_link[drive]->sc_mode = 0;
334 1.2 bouyer
335 1.4 bouyer wdc->sc_drives_mask |= (1 << drive);
336 1.12 cgd (void)config_found(&wdc->sc_dev,
337 1.12 cgd (void *)wdc->d_link[drive], wdprint);
338 1.2 bouyer }
339 1.2 bouyer }
340 1.2 bouyer #endif /* NWD > 0 */
341 1.10 mikel /* explicitly select an existing drive, to avoid spurious interrupts */
342 1.7 bouyer if (wdc->sc_flags & WDCF_ONESLAVE)
343 1.12 cgd bus_space_write_1(wdc->sc_iot, wdc->sc_ioh, wd_sdh, WDSD_IBM | 0x10); /* slave */
344 1.7 bouyer else
345 1.12 cgd bus_space_write_1(wdc->sc_iot, wdc->sc_ioh, wd_sdh, WDSD_IBM); /* master */
346 1.8 bouyer /*
347 1.8 bouyer * Reset controller. The probe, with some combinations of ATA/ATAPI
348 1.10 mikel * devices keep it in a mostly working, but strange state (with busy
349 1.8 bouyer * led on)
350 1.8 bouyer */
351 1.8 bouyer wdcreset(wdc, VERBOSE);
352 1.2 bouyer }
353 1.2 bouyer
354 1.2 bouyer /*
355 1.2 bouyer * Start I/O on a controller. This does the calculation, and starts a read or
356 1.2 bouyer * write operation. Called to from wdstart() to start a transfer, from
357 1.2 bouyer * wdcintr() to continue a multi-sector transfer or start the next transfer, or
358 1.2 bouyer * wdcrestart() after recovering from an error.
359 1.2 bouyer */
360 1.2 bouyer void
361 1.2 bouyer wdcstart(wdc)
362 1.2 bouyer struct wdc_softc *wdc;
363 1.2 bouyer {
364 1.2 bouyer struct wdc_xfer *xfer;
365 1.2 bouyer
366 1.2 bouyer if ((wdc->sc_flags & WDCF_ACTIVE) != 0 ) {
367 1.2 bouyer WDDEBUG_PRINT(("wdcstart: already active\n"));
368 1.2 bouyer return; /* controller aleady active */
369 1.2 bouyer }
370 1.2 bouyer #ifdef DIAGNOSTIC
371 1.2 bouyer if ((wdc->sc_flags & WDCF_IRQ_WAIT) != 0)
372 1.2 bouyer panic("wdcstart: controller waiting for irq\n");
373 1.2 bouyer #endif
374 1.2 bouyer /* is there a xfer ? */
375 1.2 bouyer xfer = wdc->sc_xfer.tqh_first;
376 1.2 bouyer if (xfer == NULL) {
377 1.2 bouyer #ifdef ATAPI_DEBUG2
378 1.2 bouyer printf("wdcstart: null xfer\n");
379 1.2 bouyer #endif
380 1.2 bouyer /*
381 1.2 bouyer * XXX
382 1.2 bouyer * This is a kluge. See comments in wd_get_parms().
383 1.2 bouyer */
384 1.2 bouyer if ((wdc->sc_flags & WDCF_WANTED) != 0) {
385 1.8 bouyer #ifdef ATAPI_DEBUG2
386 1.2 bouyer printf("WDCF_WANTED\n");
387 1.2 bouyer #endif
388 1.2 bouyer wdc->sc_flags &= ~WDCF_WANTED;
389 1.2 bouyer wakeup(wdc);
390 1.2 bouyer }
391 1.2 bouyer return;
392 1.2 bouyer }
393 1.19 leo
394 1.19 leo if (wdc->sc_cap & WDC_CAPABILITY_HWLOCK)
395 1.19 leo if (!(*wdc->sc_claim_hw)(wdc, 0))
396 1.19 leo return;
397 1.19 leo
398 1.2 bouyer wdc->sc_flags |= WDCF_ACTIVE;
399 1.2 bouyer #ifdef ATAPI_DEBUG2
400 1.2 bouyer printf("wdcstart: drive %d\n", (int)xfer->d_link->drive);
401 1.2 bouyer #endif
402 1.12 cgd bus_space_write_1(wdc->sc_iot, wdc->sc_ioh, wd_sdh, WDSD_IBM | xfer->d_link->drive << 4);
403 1.2 bouyer #if NATAPIBUS > 0 && NWD > 0
404 1.2 bouyer if (xfer->c_flags & C_ATAPI) {
405 1.2 bouyer #ifdef ATAPI_DEBUG_WDC
406 1.2 bouyer printf("wdcstart: atapi\n");
407 1.2 bouyer #endif
408 1.2 bouyer wdc_atapi_start(wdc,xfer);
409 1.2 bouyer } else
410 1.2 bouyer wdc_ata_start(wdc,xfer);
411 1.2 bouyer #else /* NATAPIBUS > 0 && NWD > 0 */
412 1.2 bouyer #if NATAPIBUS > 0
413 1.2 bouyer #ifdef ATAPI_DEBUG_WDC
414 1.2 bouyer printf("wdcstart: atapi\n");
415 1.2 bouyer #endif
416 1.2 bouyer wdc_atapi_start(wdc,xfer);
417 1.2 bouyer #endif /* NATAPIBUS > */
418 1.2 bouyer #if NWD > 0
419 1.2 bouyer wdc_ata_start(wdc,xfer);
420 1.2 bouyer #endif /* NWD > 0 */
421 1.2 bouyer #endif /* NATAPIBUS > 0 && NWD > 0 */
422 1.2 bouyer }
423 1.2 bouyer
424 1.2 bouyer #if NWD > 0
425 1.2 bouyer int
426 1.2 bouyer wdprint(aux, wdc)
427 1.2 bouyer void *aux;
428 1.2 bouyer const char *wdc;
429 1.2 bouyer {
430 1.2 bouyer struct wd_link *d_link = aux;
431 1.2 bouyer
432 1.2 bouyer if (!wdc)
433 1.2 bouyer printf(" drive %d", d_link->drive);
434 1.2 bouyer return QUIET;
435 1.2 bouyer }
436 1.2 bouyer
437 1.2 bouyer void
438 1.2 bouyer wdc_ata_start(wdc, xfer)
439 1.2 bouyer struct wdc_softc *wdc;
440 1.2 bouyer struct wdc_xfer *xfer;
441 1.2 bouyer {
442 1.2 bouyer struct wd_link *d_link;
443 1.2 bouyer struct buf *bp = xfer->c_bp;
444 1.2 bouyer int nblks;
445 1.2 bouyer
446 1.2 bouyer d_link=xfer->d_link;
447 1.2 bouyer
448 1.2 bouyer if (wdc->sc_errors >= WDIORETRIES) {
449 1.2 bouyer wderror(d_link, bp, "wdc_ata_start hard error");
450 1.2 bouyer xfer->c_flags |= C_ERROR;
451 1.2 bouyer wdc_ata_done(wdc, xfer);
452 1.2 bouyer return;
453 1.2 bouyer }
454 1.2 bouyer
455 1.2 bouyer /* Do control operations specially. */
456 1.2 bouyer if (d_link->sc_state < READY) {
457 1.2 bouyer /*
458 1.2 bouyer * Actually, we want to be careful not to mess with the control
459 1.2 bouyer * state if the device is currently busy, but we can assume
460 1.2 bouyer * that we never get to this point if that's the case.
461 1.2 bouyer */
462 1.2 bouyer if (wdccontrol(wdc, d_link) == 0) {
463 1.2 bouyer /* The drive is busy. Wait. */
464 1.2 bouyer return;
465 1.2 bouyer }
466 1.2 bouyer }
467 1.2 bouyer
468 1.2 bouyer /*
469 1.2 bouyer * WDCF_ERROR is set by wdcunwedge() and wdcintr() when an error is
470 1.2 bouyer * encountered. If we are in multi-sector mode, then we switch to
471 1.2 bouyer * single-sector mode and retry the operation from the start.
472 1.2 bouyer */
473 1.2 bouyer if (wdc->sc_flags & WDCF_ERROR) {
474 1.2 bouyer wdc->sc_flags &= ~WDCF_ERROR;
475 1.2 bouyer if ((wdc->sc_flags & WDCF_SINGLE) == 0) {
476 1.2 bouyer wdc->sc_flags |= WDCF_SINGLE;
477 1.2 bouyer xfer->c_skip = 0;
478 1.2 bouyer }
479 1.2 bouyer }
480 1.2 bouyer
481 1.2 bouyer
482 1.2 bouyer /* When starting a transfer... */
483 1.2 bouyer if (xfer->c_skip == 0) {
484 1.2 bouyer WDDEBUG_PRINT(("\n%s: wdc_ata_start %s %d@%d; map ",
485 1.2 bouyer wdc->sc_dev.dv_xname,
486 1.2 bouyer (xfer->c_flags & B_READ) ? "read" : "write",
487 1.2 bouyer xfer->c_bcount, xfer->c_blkno));
488 1.2 bouyer } else {
489 1.2 bouyer WDDEBUG_PRINT((" %d)%x", xfer->c_skip,
490 1.12 cgd bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_altsts)));
491 1.2 bouyer }
492 1.2 bouyer
493 1.2 bouyer /*
494 1.2 bouyer * When starting a multi-sector transfer, or doing single-sector
495 1.2 bouyer * transfers...
496 1.2 bouyer */
497 1.2 bouyer if (xfer->c_skip == 0 || (wdc->sc_flags & WDCF_SINGLE) != 0 ||
498 1.2 bouyer d_link->sc_mode == WDM_DMA) {
499 1.2 bouyer daddr_t blkno = xfer->c_blkno;
500 1.2 bouyer long cylin, head, sector;
501 1.2 bouyer int command;
502 1.2 bouyer
503 1.2 bouyer if ((wdc->sc_flags & WDCF_SINGLE) != 0)
504 1.2 bouyer nblks = 1;
505 1.2 bouyer else if (d_link->sc_mode != WDM_DMA)
506 1.2 bouyer nblks = xfer->c_bcount / d_link->sc_lp->d_secsize;
507 1.2 bouyer else
508 1.2 bouyer nblks =
509 1.2 bouyer min(xfer->c_bcount / d_link->sc_lp->d_secsize, 8);
510 1.2 bouyer
511 1.2 bouyer /* Check for bad sectors and adjust transfer, if necessary. */
512 1.2 bouyer if ((d_link->sc_lp->d_flags & D_BADSECT) != 0
513 1.2 bouyer #ifdef B_FORMAT
514 1.2 bouyer && (bp->b_flags & B_FORMAT) == 0
515 1.2 bouyer #endif
516 1.2 bouyer ) {
517 1.2 bouyer long blkdiff;
518 1.2 bouyer int i;
519 1.2 bouyer
520 1.2 bouyer for (i = 0;
521 1.2 bouyer (blkdiff = d_link->sc_badsect[i]) != -1; i++) {
522 1.2 bouyer blkdiff -= blkno;
523 1.2 bouyer if (blkdiff < 0)
524 1.2 bouyer continue;
525 1.2 bouyer if (blkdiff == 0) {
526 1.2 bouyer /* Replace current block of transfer. */
527 1.2 bouyer blkno =
528 1.2 bouyer d_link->sc_lp->d_secperunit -
529 1.2 bouyer d_link->sc_lp->d_nsectors - i - 1;
530 1.2 bouyer }
531 1.2 bouyer if (blkdiff < nblks) {
532 1.2 bouyer /* Bad block inside transfer. */
533 1.2 bouyer wdc->sc_flags |= WDCF_SINGLE;
534 1.2 bouyer nblks = 1;
535 1.2 bouyer }
536 1.2 bouyer break;
537 1.2 bouyer }
538 1.10 mikel /* Transfer is okay now. */
539 1.2 bouyer }
540 1.2 bouyer
541 1.21 mycroft if ((d_link->sc_flags & WDF_LBA) != 0) {
542 1.2 bouyer sector = (blkno >> 0) & 0xff;
543 1.2 bouyer cylin = (blkno >> 8) & 0xffff;
544 1.2 bouyer head = (blkno >> 24) & 0xf;
545 1.2 bouyer head |= WDSD_LBA;
546 1.2 bouyer } else {
547 1.2 bouyer sector = blkno % d_link->sc_lp->d_nsectors;
548 1.2 bouyer sector++; /* Sectors begin with 1, not 0. */
549 1.2 bouyer blkno /= d_link->sc_lp->d_nsectors;
550 1.2 bouyer head = blkno % d_link->sc_lp->d_ntracks;
551 1.2 bouyer blkno /= d_link->sc_lp->d_ntracks;
552 1.2 bouyer cylin = blkno;
553 1.2 bouyer head |= WDSD_CHS;
554 1.2 bouyer }
555 1.2 bouyer
556 1.2 bouyer if (d_link->sc_mode == WDM_PIOSINGLE ||
557 1.2 bouyer (wdc->sc_flags & WDCF_SINGLE) != 0)
558 1.2 bouyer xfer->c_nblks = 1;
559 1.2 bouyer else if (d_link->sc_mode == WDM_PIOMULTI)
560 1.2 bouyer xfer->c_nblks = min(nblks, d_link->sc_multiple);
561 1.2 bouyer else
562 1.2 bouyer xfer->c_nblks = nblks;
563 1.2 bouyer xfer->c_nbytes = xfer->c_nblks * d_link->sc_lp->d_secsize;
564 1.2 bouyer
565 1.2 bouyer #ifdef B_FORMAT
566 1.2 bouyer if (bp->b_flags & B_FORMAT) {
567 1.2 bouyer sector = d_link->sc_lp->d_gap3;
568 1.2 bouyer nblks = d_link->sc_lp->d_nsectors;
569 1.2 bouyer command = WDCC_FORMAT;
570 1.2 bouyer } else
571 1.2 bouyer #endif
572 1.2 bouyer switch (d_link->sc_mode) {
573 1.2 bouyer case WDM_DMA:
574 1.2 bouyer command = (xfer->c_flags & B_READ) ?
575 1.2 bouyer WDCC_READDMA : WDCC_WRITEDMA;
576 1.2 bouyer /* Start the DMA channel. */
577 1.12 cgd (*wdc->sc_dma_start)(wdc->sc_dma_arg,
578 1.12 cgd xfer->databuf + xfer->c_skip, xfer->c_nbytes,
579 1.12 cgd xfer->c_flags & B_READ);
580 1.2 bouyer break;
581 1.2 bouyer
582 1.2 bouyer case WDM_PIOMULTI:
583 1.2 bouyer command = (xfer->c_flags & B_READ) ?
584 1.2 bouyer WDCC_READMULTI : WDCC_WRITEMULTI;
585 1.2 bouyer break;
586 1.2 bouyer
587 1.2 bouyer case WDM_PIOSINGLE:
588 1.2 bouyer command = (xfer->c_flags & B_READ) ?
589 1.2 bouyer WDCC_READ : WDCC_WRITE;
590 1.2 bouyer break;
591 1.2 bouyer
592 1.2 bouyer default:
593 1.2 bouyer #ifdef DIAGNOSTIC
594 1.2 bouyer panic("bad wd mode");
595 1.2 bouyer #endif
596 1.2 bouyer return;
597 1.2 bouyer }
598 1.2 bouyer
599 1.2 bouyer /* Initiate command! */
600 1.2 bouyer if (wdccommand(wdc, d_link, command, d_link->drive,
601 1.2 bouyer cylin, head, sector, nblks) != 0) {
602 1.2 bouyer wderror(d_link, NULL,
603 1.2 bouyer "wdc_ata_start: timeout waiting for unbusy");
604 1.2 bouyer wdcunwedge(wdc);
605 1.2 bouyer return;
606 1.2 bouyer }
607 1.2 bouyer
608 1.12 cgd WDDEBUG_PRINT(("sector %lu cylin %lu head %lu addr %p sts %x\n",
609 1.2 bouyer sector, cylin, head, xfer->databuf,
610 1.12 cgd bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_altsts)));
611 1.2 bouyer
612 1.2 bouyer } else if (xfer->c_nblks > 1) {
613 1.2 bouyer /* The number of blocks in the last stretch may be smaller. */
614 1.2 bouyer nblks = xfer->c_bcount / d_link->sc_lp->d_secsize;
615 1.2 bouyer if (xfer->c_nblks > nblks) {
616 1.2 bouyer xfer->c_nblks = nblks;
617 1.2 bouyer xfer->c_nbytes = xfer->c_bcount;
618 1.2 bouyer }
619 1.2 bouyer }
620 1.2 bouyer
621 1.2 bouyer /* If this was a write and not using DMA, push the data. */
622 1.2 bouyer if (d_link->sc_mode != WDM_DMA &&
623 1.2 bouyer (xfer->c_flags & (B_READ|B_WRITE)) == B_WRITE) {
624 1.2 bouyer if (wait_for_drq(wdc) < 0) {
625 1.2 bouyer wderror(d_link, NULL,
626 1.2 bouyer "wdc_ata_start: timeout waiting for drq");
627 1.2 bouyer wdcunwedge(wdc);
628 1.2 bouyer return;
629 1.2 bouyer }
630 1.2 bouyer
631 1.12 cgd if ((wdc->sc_cap & WDC_CAPABILITY_DATA32) == 0)
632 1.16 sakamoto bus_space_write_multi_stream_2(wdc->sc_iot, wdc->sc_ioh,
633 1.12 cgd wd_data, xfer->databuf + xfer->c_skip,
634 1.2 bouyer xfer->c_nbytes >> 1);
635 1.2 bouyer else
636 1.16 sakamoto bus_space_write_multi_stream_4(wdc->sc_iot, wdc->sc_ioh,
637 1.12 cgd wd_data, xfer->databuf + xfer->c_skip,
638 1.2 bouyer xfer->c_nbytes >> 2);
639 1.2 bouyer }
640 1.2 bouyer
641 1.2 bouyer wdc->sc_flags |= WDCF_IRQ_WAIT;
642 1.2 bouyer WDDEBUG_PRINT(("wdc_ata_start: timeout "));
643 1.2 bouyer timeout(wdctimeout, wdc, WAITTIME);
644 1.2 bouyer WDDEBUG_PRINT(("done\n"));
645 1.2 bouyer }
646 1.2 bouyer
647 1.2 bouyer int
648 1.2 bouyer wdc_ata_intr(wdc,xfer)
649 1.2 bouyer struct wdc_softc *wdc;
650 1.2 bouyer struct wdc_xfer *xfer;
651 1.2 bouyer {
652 1.2 bouyer struct wd_link *d_link;
653 1.2 bouyer
654 1.2 bouyer d_link = xfer->d_link;
655 1.2 bouyer
656 1.2 bouyer if (wait_for_unbusy(wdc) < 0) {
657 1.2 bouyer wdcerror(wdc, "wdcintr: timeout waiting for unbusy");
658 1.5 bouyer return 0;
659 1.2 bouyer }
660 1.2 bouyer
661 1.8 bouyer wdc->sc_flags &= ~WDCF_IRQ_WAIT;
662 1.2 bouyer untimeout(wdctimeout, wdc);
663 1.2 bouyer
664 1.2 bouyer /* Is it not a transfer, but a control operation? */
665 1.2 bouyer if (d_link->sc_state < READY) {
666 1.2 bouyer if (wdccontrol(wdc, d_link) == 0) {
667 1.2 bouyer /* The drive is busy. Wait. */
668 1.2 bouyer return 1;
669 1.2 bouyer }
670 1.2 bouyer WDDEBUG_PRINT(("wdc_ata_start from wdc_ata_intr(open) flags 0x%x\n",
671 1.12 cgd wdc->sc_flags));
672 1.2 bouyer wdc_ata_start(wdc,xfer);
673 1.2 bouyer return 1;
674 1.2 bouyer }
675 1.2 bouyer
676 1.2 bouyer /* Turn off the DMA channel. */
677 1.2 bouyer if (d_link->sc_mode == WDM_DMA)
678 1.12 cgd (*wdc->sc_dma_finish)(wdc->sc_dma_arg);
679 1.2 bouyer
680 1.2 bouyer /* Have we an error? */
681 1.2 bouyer if (wdc->sc_status & WDCS_ERR) {
682 1.2 bouyer #ifdef WDDEBUG
683 1.2 bouyer wderror(d_link, NULL, "wdc_ata_start");
684 1.2 bouyer #endif
685 1.2 bouyer if ((wdc->sc_flags & WDCF_SINGLE) == 0) {
686 1.2 bouyer wdc->sc_flags |= WDCF_ERROR;
687 1.2 bouyer goto restart;
688 1.2 bouyer }
689 1.2 bouyer
690 1.2 bouyer #ifdef B_FORMAT
691 1.2 bouyer if (bp->b_flags & B_FORMAT)
692 1.2 bouyer goto bad;
693 1.2 bouyer #endif
694 1.2 bouyer
695 1.2 bouyer if (++wdc->sc_errors < WDIORETRIES) {
696 1.2 bouyer if (wdc->sc_errors == (WDIORETRIES + 1) / 2) {
697 1.2 bouyer #if 0
698 1.2 bouyer wderror(wd, NULL, "wedgie");
699 1.2 bouyer #endif
700 1.2 bouyer wdcunwedge(wdc);
701 1.2 bouyer return 1;
702 1.2 bouyer }
703 1.2 bouyer goto restart;
704 1.2 bouyer }
705 1.2 bouyer wderror(d_link, xfer->c_bp, "wdc_ata_intr hard error");
706 1.2 bouyer
707 1.2 bouyer #ifdef B_FORMAT
708 1.2 bouyer bad:
709 1.2 bouyer #endif
710 1.2 bouyer xfer->c_flags |= C_ERROR;
711 1.2 bouyer goto done;
712 1.2 bouyer }
713 1.2 bouyer
714 1.2 bouyer /* If this was a read and not using DMA, fetch the data. */
715 1.2 bouyer if (d_link->sc_mode != WDM_DMA &&
716 1.2 bouyer (xfer->c_flags & (B_READ|B_WRITE)) == B_READ) {
717 1.2 bouyer if ((wdc->sc_status & (WDCS_DRDY | WDCS_DSC | WDCS_DRQ))
718 1.2 bouyer != (WDCS_DRDY | WDCS_DSC | WDCS_DRQ)) {
719 1.2 bouyer wderror(d_link, NULL, "wdcintr: read intr before drq");
720 1.2 bouyer wdcunwedge(wdc);
721 1.2 bouyer return 1;
722 1.2 bouyer }
723 1.2 bouyer
724 1.2 bouyer /* Pull in data. */
725 1.12 cgd if ((wdc->sc_cap & WDC_CAPABILITY_DATA32) == 0)
726 1.16 sakamoto bus_space_read_multi_stream_2(wdc->sc_iot, wdc->sc_ioh,
727 1.12 cgd wd_data, xfer->databuf + xfer->c_skip,
728 1.12 cgd xfer->c_nbytes >> 1);
729 1.2 bouyer else
730 1.16 sakamoto bus_space_read_multi_stream_4(wdc->sc_iot, wdc->sc_ioh,
731 1.12 cgd wd_data, xfer->databuf + xfer->c_skip,
732 1.12 cgd xfer->c_nbytes >> 2);
733 1.2 bouyer }
734 1.2 bouyer
735 1.2 bouyer /* If we encountered any abnormalities, flag it as a soft error. */
736 1.2 bouyer if (wdc->sc_errors > 0 ||
737 1.2 bouyer (wdc->sc_status & WDCS_CORR) != 0) {
738 1.2 bouyer wderror(d_link, xfer->c_bp, "soft error (corrected)");
739 1.2 bouyer wdc->sc_errors = 0;
740 1.2 bouyer }
741 1.2 bouyer
742 1.2 bouyer /* Adjust pointers for the next block, if any. */
743 1.2 bouyer xfer->c_blkno += xfer->c_nblks;
744 1.2 bouyer xfer->c_skip += xfer->c_nbytes;
745 1.2 bouyer xfer->c_bcount -= xfer->c_nbytes;
746 1.2 bouyer
747 1.2 bouyer /* See if this transfer is complete. */
748 1.2 bouyer if (xfer->c_bcount > 0)
749 1.2 bouyer goto restart;
750 1.2 bouyer
751 1.2 bouyer done:
752 1.2 bouyer /* Done with this transfer, with or without error. */
753 1.2 bouyer wdc_ata_done(wdc, xfer);
754 1.5 bouyer return 1;
755 1.2 bouyer
756 1.2 bouyer restart:
757 1.2 bouyer /* Start the next operation */
758 1.2 bouyer WDDEBUG_PRINT(("wdc_ata_start from wdcintr flags 0x%x\n",
759 1.2 bouyer wdc->sc_flags));
760 1.2 bouyer wdc_ata_start(wdc, xfer);
761 1.2 bouyer
762 1.2 bouyer return 1;
763 1.2 bouyer }
764 1.2 bouyer
765 1.2 bouyer void
766 1.2 bouyer wdc_ata_done(wdc, xfer)
767 1.2 bouyer struct wdc_softc *wdc;
768 1.2 bouyer struct wdc_xfer *xfer;
769 1.2 bouyer {
770 1.2 bouyer struct buf *bp = xfer->c_bp;
771 1.2 bouyer struct wd_link *d_link = xfer->d_link;
772 1.2 bouyer int s;
773 1.2 bouyer
774 1.2 bouyer WDDEBUG_PRINT(("wdc_ata_done\n"));
775 1.2 bouyer
776 1.19 leo if (wdc->sc_cap & WDC_CAPABILITY_HWLOCK)
777 1.19 leo (*wdc->sc_free_hw)(wdc);
778 1.19 leo
779 1.2 bouyer /* remove this command from xfer queue */
780 1.2 bouyer s = splbio();
781 1.2 bouyer TAILQ_REMOVE(&wdc->sc_xfer, xfer, c_xferchain);
782 1.2 bouyer wdc->sc_flags &= ~(WDCF_SINGLE | WDCF_ERROR | WDCF_ACTIVE);
783 1.2 bouyer wdc->sc_errors = 0;
784 1.2 bouyer if (bp) {
785 1.2 bouyer if (xfer->c_flags & C_ERROR) {
786 1.2 bouyer bp->b_flags |= B_ERROR;
787 1.2 bouyer bp->b_error = EIO;
788 1.2 bouyer }
789 1.2 bouyer bp->b_resid = xfer->c_bcount;
790 1.2 bouyer wddone(d_link, bp);
791 1.2 bouyer biodone(bp);
792 1.2 bouyer } else {
793 1.2 bouyer wakeup(xfer->databuf);
794 1.2 bouyer }
795 1.2 bouyer xfer->c_skip = 0;
796 1.2 bouyer wdc_free_xfer(xfer);
797 1.2 bouyer d_link->openings++;
798 1.2 bouyer wdstart((void*)d_link->wd_softc);
799 1.2 bouyer WDDEBUG_PRINT(("wdcstart from wdc_ata_done, flags 0x%x\n",
800 1.2 bouyer wdc->sc_flags));
801 1.2 bouyer wdcstart(wdc);
802 1.2 bouyer splx(s);
803 1.2 bouyer }
804 1.2 bouyer
805 1.2 bouyer /*
806 1.2 bouyer * Get the drive parameters, if ESDI or ATA, or create fake ones for ST506.
807 1.2 bouyer */
808 1.2 bouyer int
809 1.2 bouyer wdc_get_parms(wdc, d_link)
810 1.2 bouyer struct wdc_softc * wdc;
811 1.2 bouyer struct wd_link *d_link;
812 1.2 bouyer {
813 1.2 bouyer int i;
814 1.2 bouyer char tb[DEV_BSIZE];
815 1.2 bouyer int s, error;
816 1.2 bouyer
817 1.2 bouyer /*
818 1.2 bouyer * XXX
819 1.2 bouyer * The locking done here, and the length of time this may keep the rest
820 1.2 bouyer * of the system suspended, is a kluge. This should be rewritten to
821 1.2 bouyer * set up a transfer and queue it through wdstart(), but it's called
822 1.2 bouyer * infrequently enough that this isn't a pressing matter.
823 1.2 bouyer */
824 1.2 bouyer
825 1.2 bouyer s = splbio();
826 1.2 bouyer
827 1.2 bouyer while ((wdc->sc_flags & WDCF_ACTIVE) != 0) {
828 1.2 bouyer wdc->sc_flags |= WDCF_WANTED;
829 1.2 bouyer if ((error = tsleep(wdc, PRIBIO | PCATCH, "wdprm", 0)) != 0) {
830 1.2 bouyer splx(s);
831 1.2 bouyer return error;
832 1.2 bouyer }
833 1.2 bouyer }
834 1.19 leo if (wdc->sc_cap & WDC_CAPABILITY_HWLOCK)
835 1.19 leo if (!(*wdc->sc_claim_hw)(wdc, 1))
836 1.19 leo panic("wdc_get_parms: Cannot claim wd-hardware");
837 1.2 bouyer
838 1.2 bouyer wdc->sc_flags |= WDCF_ACTIVE;
839 1.2 bouyer
840 1.2 bouyer if (wdccommandshort(wdc, d_link->drive, WDCC_IDENTIFY) != 0 ||
841 1.2 bouyer wait_for_drq(wdc) != 0) {
842 1.2 bouyer /*
843 1.2 bouyer * We `know' there's a drive here; just assume it's old.
844 1.2 bouyer * This geometry is only used to read the MBR and print a
845 1.2 bouyer * (false) attach message.
846 1.2 bouyer */
847 1.2 bouyer strncpy(d_link->sc_lp->d_typename, "ST506",
848 1.2 bouyer sizeof d_link->sc_lp->d_typename);
849 1.2 bouyer d_link->sc_lp->d_type = DTYPE_ST506;
850 1.2 bouyer
851 1.2 bouyer strncpy(d_link->sc_params.wdp_model, "unknown",
852 1.2 bouyer sizeof d_link->sc_params.wdp_model);
853 1.2 bouyer d_link->sc_params.wdp_config = WD_CFG_FIXED;
854 1.2 bouyer d_link->sc_params.wdp_cylinders = 1024;
855 1.2 bouyer d_link->sc_params.wdp_heads = 8;
856 1.2 bouyer d_link->sc_params.wdp_sectors = 17;
857 1.21 mycroft d_link->sc_params.wdp_multi = 0x0000;
858 1.21 mycroft d_link->sc_params.wdp_capabilities1 = 0x0000;
859 1.21 mycroft d_link->sc_params.wdp_capabilities2 = 0x0000;
860 1.21 mycroft d_link->sc_params.wdp_ataversion = 0x0000;
861 1.2 bouyer } else {
862 1.2 bouyer strncpy(d_link->sc_lp->d_typename, "ESDI/IDE",
863 1.2 bouyer sizeof d_link->sc_lp->d_typename);
864 1.2 bouyer d_link->sc_lp->d_type = DTYPE_ESDI;
865 1.2 bouyer
866 1.2 bouyer /* Read in parameter block. */
867 1.12 cgd bus_space_read_multi_2(wdc->sc_iot, wdc->sc_ioh, wd_data,
868 1.12 cgd (u_int16_t *)tb, sizeof(tb) >> 1);
869 1.2 bouyer bcopy(tb, &d_link->sc_params, sizeof(struct wdparams));
870 1.2 bouyer
871 1.2 bouyer /* Shuffle string byte order. */
872 1.2 bouyer for (i = 0; i < sizeof(d_link->sc_params.wdp_model); i += 2) {
873 1.2 bouyer u_short *p;
874 1.2 bouyer p = (u_short *)(d_link->sc_params.wdp_model + i);
875 1.2 bouyer *p = ntohs(*p);
876 1.2 bouyer }
877 1.2 bouyer }
878 1.2 bouyer
879 1.2 bouyer /* Clear any leftover interrupt. */
880 1.12 cgd (void) bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_status);
881 1.2 bouyer
882 1.19 leo if (wdc->sc_cap & WDC_CAPABILITY_HWLOCK)
883 1.19 leo (*wdc->sc_free_hw)(wdc);
884 1.19 leo
885 1.2 bouyer /* Restart the queue. */
886 1.2 bouyer WDDEBUG_PRINT(("wdcstart from wdc_get_parms flags 0x%x\n",
887 1.2 bouyer wdc->sc_flags));
888 1.2 bouyer wdc->sc_flags &= ~WDCF_ACTIVE;
889 1.2 bouyer wdcstart(wdc);
890 1.2 bouyer
891 1.2 bouyer splx(s);
892 1.2 bouyer return 0;
893 1.2 bouyer }
894 1.2 bouyer
895 1.2 bouyer /*
896 1.2 bouyer * Implement operations needed before read/write.
897 1.2 bouyer * Returns 0 if operation still in progress, 1 if completed.
898 1.2 bouyer */
899 1.2 bouyer int
900 1.2 bouyer wdccontrol(wdc, d_link)
901 1.2 bouyer struct wdc_softc *wdc;
902 1.2 bouyer struct wd_link *d_link;
903 1.2 bouyer {
904 1.2 bouyer WDDEBUG_PRINT(("wdccontrol\n"));
905 1.2 bouyer
906 1.2 bouyer switch (d_link->sc_state) {
907 1.2 bouyer case RECAL: /* Set SDH, step rate, do recal. */
908 1.2 bouyer if (wdccommandshort(wdc, d_link->drive, WDCC_RECAL) != 0) {
909 1.2 bouyer wderror(d_link, NULL, "wdccontrol: recal failed (1)");
910 1.2 bouyer goto bad;
911 1.2 bouyer }
912 1.2 bouyer d_link->sc_state = RECAL_WAIT;
913 1.2 bouyer break;
914 1.2 bouyer
915 1.2 bouyer case RECAL_WAIT:
916 1.2 bouyer if (wdc->sc_status & WDCS_ERR) {
917 1.2 bouyer wderror(d_link, NULL, "wdccontrol: recal failed (2)");
918 1.2 bouyer goto bad;
919 1.2 bouyer }
920 1.2 bouyer /* fall through */
921 1.2 bouyer
922 1.2 bouyer case GEOMETRY:
923 1.21 mycroft if ((d_link->sc_flags & WDF_LBA) != 0)
924 1.2 bouyer goto multimode;
925 1.2 bouyer if (wdsetctlr(d_link) != 0) {
926 1.2 bouyer /* Already printed a message. */
927 1.2 bouyer goto bad;
928 1.2 bouyer }
929 1.2 bouyer d_link->sc_state = GEOMETRY_WAIT;
930 1.2 bouyer break;
931 1.2 bouyer
932 1.2 bouyer case GEOMETRY_WAIT:
933 1.2 bouyer if (wdc->sc_status & WDCS_ERR) {
934 1.2 bouyer wderror(d_link, NULL, "wdccontrol: geometry failed");
935 1.2 bouyer goto bad;
936 1.2 bouyer }
937 1.2 bouyer /* fall through */
938 1.2 bouyer
939 1.2 bouyer case MULTIMODE:
940 1.2 bouyer multimode:
941 1.2 bouyer if (d_link->sc_mode != WDM_PIOMULTI)
942 1.2 bouyer goto ready;
943 1.12 cgd bus_space_write_1(wdc->sc_iot, wdc->sc_ioh, wd_seccnt, d_link->sc_multiple);
944 1.2 bouyer if (wdccommandshort(wdc, d_link->drive,
945 1.2 bouyer WDCC_SETMULTI) != 0) {
946 1.2 bouyer wderror(d_link, NULL,
947 1.2 bouyer "wdccontrol: setmulti failed (1)");
948 1.2 bouyer goto bad;
949 1.2 bouyer }
950 1.2 bouyer d_link->sc_state = MULTIMODE_WAIT;
951 1.2 bouyer break;
952 1.2 bouyer
953 1.2 bouyer case MULTIMODE_WAIT:
954 1.2 bouyer if (wdc->sc_status & WDCS_ERR) {
955 1.2 bouyer wderror(d_link, NULL,
956 1.2 bouyer "wdccontrol: setmulti failed (2)");
957 1.2 bouyer goto bad;
958 1.2 bouyer }
959 1.2 bouyer /* fall through */
960 1.2 bouyer
961 1.2 bouyer case READY:
962 1.2 bouyer ready:
963 1.2 bouyer wdc->sc_errors = 0;
964 1.2 bouyer d_link->sc_state = READY;
965 1.2 bouyer /*
966 1.2 bouyer * The rest of the initialization can be done by normal means.
967 1.2 bouyer */
968 1.2 bouyer return 1;
969 1.2 bouyer
970 1.2 bouyer bad:
971 1.2 bouyer wdcunwedge(wdc);
972 1.2 bouyer return 0;
973 1.2 bouyer }
974 1.2 bouyer
975 1.2 bouyer wdc->sc_flags |= WDCF_IRQ_WAIT;
976 1.2 bouyer timeout(wdctimeout, wdc, WAITTIME);
977 1.2 bouyer return 0;
978 1.2 bouyer }
979 1.2 bouyer
980 1.2 bouyer #endif /* NWD > 0 */
981 1.2 bouyer
982 1.2 bouyer
983 1.2 bouyer /*
984 1.2 bouyer * Interrupt routine for the controller. Acknowledge the interrupt, check for
985 1.2 bouyer * errors on the current operation, mark it done if necessary, and start the
986 1.2 bouyer * next request. Also check for a partially done transfer, and continue with
987 1.2 bouyer * the next chunk if so.
988 1.2 bouyer */
989 1.2 bouyer int
990 1.2 bouyer wdcintr(arg)
991 1.2 bouyer void *arg;
992 1.2 bouyer {
993 1.2 bouyer struct wdc_softc *wdc = arg;
994 1.2 bouyer struct wdc_xfer *xfer;
995 1.2 bouyer
996 1.2 bouyer if ((wdc->sc_flags & WDCF_IRQ_WAIT) == 0) {
997 1.2 bouyer /* Clear the pending interrupt and abort. */
998 1.12 cgd u_char s = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_status);
999 1.2 bouyer
1000 1.2 bouyer #ifdef ATAPI_DEBUG_WDC
1001 1.12 cgd u_char e = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_error);
1002 1.12 cgd u_char i = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_seccnt);
1003 1.2 bouyer printf("wdcintr: inactive controller, "
1004 1.2 bouyer "punting st=%02x er=%02x irr=%02x\n", s, e, i);
1005 1.2 bouyer #else
1006 1.12 cgd bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_error);
1007 1.12 cgd bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_seccnt);
1008 1.2 bouyer #endif
1009 1.2 bouyer
1010 1.2 bouyer if (s & WDCS_DRQ) {
1011 1.12 cgd int len = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_cyl_lo) +
1012 1.12 cgd 256 * bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_cyl_hi);
1013 1.2 bouyer #ifdef ATAPI_DEBUG_WDC
1014 1.2 bouyer printf ("wdcintr: clearing up %d bytes\n", len);
1015 1.2 bouyer #endif
1016 1.2 bouyer wdcbit_bucket (wdc, len);
1017 1.2 bouyer }
1018 1.2 bouyer return 0;
1019 1.2 bouyer }
1020 1.2 bouyer
1021 1.2 bouyer WDDEBUG_PRINT(("wdcintr\n"));
1022 1.2 bouyer
1023 1.2 bouyer xfer = wdc->sc_xfer.tqh_first;
1024 1.2 bouyer #if NATAPIBUS > 0 && NWD > 0
1025 1.2 bouyer if (xfer->c_flags & C_ATAPI) {
1026 1.5 bouyer return wdc_atapi_intr(wdc,xfer);
1027 1.2 bouyer } else
1028 1.2 bouyer return wdc_ata_intr(wdc,xfer);
1029 1.2 bouyer #else /* NATAPIBUS > 0 && NWD > 0 */
1030 1.2 bouyer #if NATAPIBUS > 0
1031 1.5 bouyer return wdc_atapi_intr(wdc,xfer);
1032 1.2 bouyer #endif /* NATAPIBUS > 0 */
1033 1.2 bouyer #if NWD > 0
1034 1.2 bouyer return wdc_ata_intr(wdc,xfer);
1035 1.2 bouyer #endif /* NWD > 0 */
1036 1.2 bouyer #endif /* NATAPIBUS > 0 && NWD > 0 */
1037 1.2 bouyer }
1038 1.2 bouyer
1039 1.2 bouyer int
1040 1.2 bouyer wdcreset(wdc, verb)
1041 1.2 bouyer struct wdc_softc *wdc;
1042 1.2 bouyer int verb;
1043 1.2 bouyer {
1044 1.2 bouyer
1045 1.2 bouyer /* Reset the device. */
1046 1.12 cgd bus_space_write_1(wdc->sc_auxiot, wdc->sc_auxioh, wd_aux_ctlr,
1047 1.12 cgd WDCTL_RST | WDCTL_IDS);
1048 1.2 bouyer delay(1000);
1049 1.12 cgd bus_space_write_1(wdc->sc_auxiot, wdc->sc_auxioh, wd_aux_ctlr,
1050 1.12 cgd WDCTL_IDS);
1051 1.2 bouyer delay(1000);
1052 1.12 cgd (void) bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_error);
1053 1.12 cgd bus_space_write_1(wdc->sc_auxiot, wdc->sc_auxioh, wd_aux_ctlr,
1054 1.12 cgd WDCTL_4BIT);
1055 1.2 bouyer
1056 1.2 bouyer if (wait_for_unbusy(wdc) < 0) {
1057 1.2 bouyer if (verb)
1058 1.2 bouyer printf("%s: reset failed\n", wdc->sc_dev.dv_xname);
1059 1.2 bouyer return 1;
1060 1.2 bouyer }
1061 1.2 bouyer
1062 1.2 bouyer return 0;
1063 1.2 bouyer }
1064 1.2 bouyer
1065 1.2 bouyer void
1066 1.2 bouyer wdcrestart(arg)
1067 1.2 bouyer void *arg;
1068 1.2 bouyer {
1069 1.2 bouyer struct wdc_softc *wdc = arg;
1070 1.2 bouyer int s;
1071 1.2 bouyer
1072 1.2 bouyer s = splbio();
1073 1.2 bouyer wdcstart(wdc);
1074 1.2 bouyer splx(s);
1075 1.2 bouyer }
1076 1.2 bouyer
1077 1.2 bouyer /*
1078 1.2 bouyer * Unwedge the controller after an unexpected error. We do this by resetting
1079 1.2 bouyer * it, marking all drives for recalibration, and stalling the queue for a short
1080 1.2 bouyer * period to give the reset time to finish.
1081 1.2 bouyer * NOTE: We use a timeout here, so this routine must not be called during
1082 1.2 bouyer * autoconfig or dump.
1083 1.2 bouyer */
1084 1.2 bouyer void
1085 1.2 bouyer wdcunwedge(wdc)
1086 1.2 bouyer struct wdc_softc *wdc;
1087 1.2 bouyer {
1088 1.2 bouyer int unit;
1089 1.2 bouyer
1090 1.2 bouyer #ifdef ATAPI_DEBUG
1091 1.2 bouyer printf("wdcunwedge\n");
1092 1.2 bouyer #endif
1093 1.2 bouyer
1094 1.2 bouyer untimeout(wdctimeout, wdc);
1095 1.2 bouyer wdc->sc_flags &= ~WDCF_IRQ_WAIT;
1096 1.2 bouyer (void) wdcreset(wdc, VERBOSE);
1097 1.2 bouyer
1098 1.2 bouyer /* Schedule recalibrate for all drives on this controller. */
1099 1.2 bouyer for (unit = 0; unit < 2; unit++) {
1100 1.8 bouyer if (!wdc->d_link[unit])
1101 1.8 bouyer wdccommandshort(wdc, unit, ATAPI_SOFT_RESET);
1102 1.8 bouyer else if (wdc->d_link[unit]->sc_state > RECAL)
1103 1.2 bouyer wdc->d_link[unit]->sc_state = RECAL;
1104 1.2 bouyer }
1105 1.2 bouyer
1106 1.2 bouyer wdc->sc_flags |= WDCF_ERROR;
1107 1.2 bouyer ++wdc->sc_errors;
1108 1.2 bouyer
1109 1.2 bouyer /* Wake up in a little bit and restart the operation. */
1110 1.2 bouyer WDDEBUG_PRINT(("wdcrestart from wdcunwedge\n"));
1111 1.2 bouyer wdc->sc_flags &= ~WDCF_ACTIVE;
1112 1.2 bouyer timeout(wdcrestart, wdc, RECOVERYTIME);
1113 1.2 bouyer }
1114 1.2 bouyer
1115 1.2 bouyer int
1116 1.2 bouyer wdcwait(wdc, mask)
1117 1.2 bouyer struct wdc_softc *wdc;
1118 1.2 bouyer int mask;
1119 1.2 bouyer {
1120 1.2 bouyer int timeout = 0;
1121 1.2 bouyer u_char status;
1122 1.2 bouyer #ifdef WDCNDELAY_DEBUG
1123 1.2 bouyer extern int cold;
1124 1.2 bouyer #endif
1125 1.2 bouyer
1126 1.12 cgd WDDEBUG_PRINT(("wdcwait\n"));
1127 1.2 bouyer
1128 1.2 bouyer for (;;) {
1129 1.12 cgd wdc->sc_status = status = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_status);
1130 1.2 bouyer /*
1131 1.2 bouyer * XXX
1132 1.2 bouyer * If a single slave ATAPI device is attached, it may
1133 1.2 bouyer * have released the bus. Select it and try again.
1134 1.2 bouyer */
1135 1.2 bouyer if (status == 0xff && wdc->sc_flags & WDCF_ONESLAVE) {
1136 1.12 cgd bus_space_write_1(wdc->sc_iot, wdc->sc_ioh, wd_sdh, WDSD_IBM | 0x10);
1137 1.12 cgd wdc->sc_status = status = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_status);
1138 1.2 bouyer }
1139 1.2 bouyer if ((status & WDCS_BSY) == 0 && (status & mask) == mask)
1140 1.2 bouyer break;
1141 1.2 bouyer if (++timeout > WDCNDELAY) {
1142 1.3 bouyer #ifdef ATAPI_DEBUG
1143 1.12 cgd printf("wdcwait: timeout, status %x error %x\n", status, bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_error));
1144 1.2 bouyer #endif
1145 1.2 bouyer return -1;
1146 1.2 bouyer }
1147 1.2 bouyer delay(WDCDELAY);
1148 1.2 bouyer }
1149 1.2 bouyer if (status & WDCS_ERR) {
1150 1.12 cgd wdc->sc_error = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_error);
1151 1.2 bouyer return WDCS_ERR;
1152 1.2 bouyer }
1153 1.2 bouyer #ifdef WDCNDELAY_DEBUG
1154 1.2 bouyer /* After autoconfig, there should be no long delays. */
1155 1.2 bouyer if (!cold && timeout > WDCNDELAY_DEBUG) {
1156 1.2 bouyer struct wdc_xfer *xfer = wdc->sc_xfer.tqh_first;
1157 1.2 bouyer if (xfer == NULL)
1158 1.2 bouyer printf("%s: warning: busy-wait took %dus\n",
1159 1.2 bouyer wdc->sc_dev.dv_xname, WDCDELAY * timeout);
1160 1.2 bouyer else
1161 1.2 bouyer printf("%s(%s): warning: busy-wait took %dus\n",
1162 1.2 bouyer wdc->sc_dev.dv_xname,
1163 1.2 bouyer ((struct device*)xfer->d_link->wd_softc)->dv_xname,
1164 1.2 bouyer WDCDELAY * timeout);
1165 1.2 bouyer }
1166 1.2 bouyer #endif
1167 1.2 bouyer return 0;
1168 1.2 bouyer }
1169 1.2 bouyer
1170 1.2 bouyer void
1171 1.2 bouyer wdctimeout(arg)
1172 1.2 bouyer void *arg;
1173 1.2 bouyer {
1174 1.2 bouyer struct wdc_softc *wdc = (struct wdc_softc *)arg;
1175 1.8 bouyer struct wdc_xfer *xfer = wdc->sc_xfer.tqh_first;
1176 1.2 bouyer int s;
1177 1.2 bouyer
1178 1.2 bouyer WDDEBUG_PRINT(("wdctimeout\n"));
1179 1.2 bouyer
1180 1.2 bouyer s = splbio();
1181 1.2 bouyer if ((wdc->sc_flags & WDCF_IRQ_WAIT) != 0) {
1182 1.8 bouyer wdcerror(wdc, "lost interrupt");
1183 1.8 bouyer printf("\ttype: %s\n", (xfer->c_flags & C_ATAPI) ? "atapi":"ata");
1184 1.8 bouyer printf("\tc_bcount: %d\n", xfer->c_bcount);
1185 1.8 bouyer printf("\tc_skip: %d\n", xfer->c_skip);
1186 1.8 bouyer wdcintr(wdc);
1187 1.2 bouyer wdc->sc_flags &= ~WDCF_IRQ_WAIT;
1188 1.2 bouyer wdcunwedge(wdc);
1189 1.2 bouyer } else
1190 1.2 bouyer wdcerror(wdc, "missing untimeout");
1191 1.2 bouyer splx(s);
1192 1.2 bouyer }
1193 1.2 bouyer
1194 1.2 bouyer /*
1195 1.2 bouyer * Wait for the drive to become ready and send a command.
1196 1.2 bouyer * Return -1 if busy for too long or 0 otherwise.
1197 1.2 bouyer * Assumes interrupts are blocked.
1198 1.2 bouyer */
1199 1.2 bouyer int
1200 1.2 bouyer wdccommand(wdc, d_link, command, drive, cylin, head, sector, count)
1201 1.2 bouyer struct wdc_softc *wdc;
1202 1.2 bouyer struct wd_link *d_link;
1203 1.2 bouyer int command;
1204 1.2 bouyer int drive, cylin, head, sector, count;
1205 1.2 bouyer {
1206 1.2 bouyer int stat;
1207 1.2 bouyer
1208 1.2 bouyer WDDEBUG_PRINT(("wdccommand drive %d\n", drive));
1209 1.2 bouyer
1210 1.2 bouyer #if defined(DIAGNOSTIC) && defined(WDCDEBUG)
1211 1.2 bouyer if ((wdc->sc_flags & WDCF_ACTIVE) == 0)
1212 1.2 bouyer printf("wdccommand: controler not active (drive %d)\n", drive);
1213 1.2 bouyer #endif
1214 1.2 bouyer
1215 1.2 bouyer /* Select drive, head, and addressing mode. */
1216 1.12 cgd bus_space_write_1(wdc->sc_iot, wdc->sc_ioh, wd_sdh, WDSD_IBM | (drive << 4) | head);
1217 1.2 bouyer
1218 1.2 bouyer /* Wait for it to become ready to accept a command. */
1219 1.2 bouyer if (command == WDCC_IDP || d_link->type == ATAPI)
1220 1.2 bouyer stat = wait_for_unbusy(wdc);
1221 1.2 bouyer else
1222 1.2 bouyer stat = wdcwait(wdc, WDCS_DRDY);
1223 1.2 bouyer
1224 1.2 bouyer if (stat < 0) {
1225 1.2 bouyer #ifdef ATAPI_DEBUG
1226 1.2 bouyer printf("wdcommand: xfer failed (wait_for_unbusy) status %d\n",
1227 1.2 bouyer stat);
1228 1.2 bouyer #endif
1229 1.2 bouyer return -1;
1230 1.2 bouyer }
1231 1.2 bouyer
1232 1.2 bouyer /* Load parameters. */
1233 1.2 bouyer if (d_link->type == ATA && d_link->sc_lp->d_type == DTYPE_ST506)
1234 1.12 cgd bus_space_write_1(wdc->sc_iot, wdc->sc_ioh, wd_precomp, d_link->sc_lp->d_precompcyl / 4);
1235 1.2 bouyer else
1236 1.12 cgd bus_space_write_1(wdc->sc_iot, wdc->sc_ioh, wd_features, 0);
1237 1.12 cgd bus_space_write_1(wdc->sc_iot, wdc->sc_ioh, wd_cyl_lo, cylin);
1238 1.12 cgd bus_space_write_1(wdc->sc_iot, wdc->sc_ioh, wd_cyl_hi, cylin >> 8);
1239 1.12 cgd bus_space_write_1(wdc->sc_iot, wdc->sc_ioh, wd_sector, sector);
1240 1.12 cgd bus_space_write_1(wdc->sc_iot, wdc->sc_ioh, wd_seccnt, count);
1241 1.2 bouyer
1242 1.2 bouyer /* Send command. */
1243 1.12 cgd bus_space_write_1(wdc->sc_iot, wdc->sc_ioh, wd_command, command);
1244 1.2 bouyer
1245 1.2 bouyer return 0;
1246 1.2 bouyer }
1247 1.2 bouyer
1248 1.2 bouyer /*
1249 1.2 bouyer * Simplified version of wdccommand().
1250 1.2 bouyer */
1251 1.2 bouyer int
1252 1.2 bouyer wdccommandshort(wdc, drive, command)
1253 1.2 bouyer struct wdc_softc *wdc;
1254 1.2 bouyer int drive;
1255 1.2 bouyer int command;
1256 1.2 bouyer {
1257 1.2 bouyer
1258 1.2 bouyer WDDEBUG_PRINT(("wdccommandshort\n"));
1259 1.2 bouyer
1260 1.2 bouyer #if defined(DIAGNOSTIC) && defined(WDCDEBUG)
1261 1.2 bouyer if ((wdc->sc_flags & WDCF_ACTIVE) == 0)
1262 1.10 mikel printf("wdccommandshort: controller not active (drive %d)\n",
1263 1.2 bouyer drive);
1264 1.2 bouyer #endif
1265 1.2 bouyer
1266 1.2 bouyer /* Select drive. */
1267 1.12 cgd bus_space_write_1(wdc->sc_iot, wdc->sc_ioh, wd_sdh, WDSD_IBM | (drive << 4));
1268 1.2 bouyer
1269 1.2 bouyer if (wdcwait(wdc, WDCS_DRDY) < 0)
1270 1.2 bouyer return -1;
1271 1.2 bouyer
1272 1.12 cgd bus_space_write_1(wdc->sc_iot, wdc->sc_ioh, wd_command, command);
1273 1.2 bouyer
1274 1.2 bouyer return 0;
1275 1.2 bouyer }
1276 1.2 bouyer
1277 1.2 bouyer void
1278 1.2 bouyer wdc_exec_xfer(wdc, d_link, xfer)
1279 1.2 bouyer struct wdc_softc *wdc;
1280 1.2 bouyer struct wd_link *d_link;
1281 1.2 bouyer struct wdc_xfer *xfer;
1282 1.2 bouyer {
1283 1.2 bouyer int s;
1284 1.2 bouyer
1285 1.2 bouyer WDDEBUG_PRINT(("wdc_exec_xfer\n"));
1286 1.2 bouyer
1287 1.2 bouyer s = splbio();
1288 1.2 bouyer
1289 1.2 bouyer /* insert at the end of command list */
1290 1.12 cgd TAILQ_INSERT_TAIL(&wdc->sc_xfer,xfer , c_xferchain);
1291 1.2 bouyer WDDEBUG_PRINT(("wdcstart from wdc_exec_xfer, flags 0x%x\n",
1292 1.2 bouyer wdc->sc_flags));
1293 1.2 bouyer wdcstart(wdc);
1294 1.2 bouyer xfer->c_flags |= C_NEEDDONE; /* we can now call upper level done() */
1295 1.2 bouyer splx(s);
1296 1.2 bouyer }
1297 1.2 bouyer
1298 1.2 bouyer struct wdc_xfer *
1299 1.2 bouyer wdc_get_xfer(flags)
1300 1.2 bouyer int flags;
1301 1.2 bouyer {
1302 1.2 bouyer struct wdc_xfer *xfer;
1303 1.2 bouyer int s;
1304 1.2 bouyer
1305 1.2 bouyer s = splbio();
1306 1.2 bouyer if ((xfer = xfer_free_list.lh_first) != NULL) {
1307 1.2 bouyer LIST_REMOVE(xfer, free_list);
1308 1.2 bouyer splx(s);
1309 1.2 bouyer #ifdef DIAGNOSTIC
1310 1.2 bouyer if ((xfer->c_flags & C_INUSE) != 0)
1311 1.2 bouyer panic("wdc_get_xfer: xfer already in use\n");
1312 1.2 bouyer #endif
1313 1.2 bouyer } else {
1314 1.2 bouyer splx(s);
1315 1.8 bouyer #ifdef ATAPI_DEBUG2
1316 1.2 bouyer printf("wdc:making xfer %d\n",wdc_nxfer);
1317 1.2 bouyer #endif
1318 1.2 bouyer xfer = malloc(sizeof(*xfer), M_DEVBUF,
1319 1.2 bouyer ((flags & IDE_NOSLEEP) != 0 ? M_NOWAIT : M_WAITOK));
1320 1.2 bouyer if (xfer == NULL)
1321 1.2 bouyer return 0;
1322 1.2 bouyer
1323 1.2 bouyer #ifdef DIAGNOSTIC
1324 1.2 bouyer xfer->c_flags &= ~C_INUSE;
1325 1.2 bouyer #endif
1326 1.8 bouyer #ifdef ATAPI_DEBUG2
1327 1.2 bouyer wdc_nxfer++;
1328 1.2 bouyer #endif
1329 1.2 bouyer }
1330 1.2 bouyer #ifdef DIAGNOSTIC
1331 1.2 bouyer if ((xfer->c_flags & C_INUSE) != 0)
1332 1.2 bouyer panic("wdc_get_xfer: xfer already in use\n");
1333 1.2 bouyer #endif
1334 1.2 bouyer bzero(xfer,sizeof(struct wdc_xfer));
1335 1.2 bouyer xfer->c_flags = C_INUSE;
1336 1.2 bouyer return xfer;
1337 1.2 bouyer }
1338 1.2 bouyer
1339 1.2 bouyer void
1340 1.2 bouyer wdc_free_xfer(xfer)
1341 1.2 bouyer struct wdc_xfer *xfer;
1342 1.2 bouyer {
1343 1.2 bouyer int s;
1344 1.2 bouyer
1345 1.2 bouyer s = splbio();
1346 1.2 bouyer xfer->c_flags &= ~C_INUSE;
1347 1.2 bouyer LIST_INSERT_HEAD(&xfer_free_list, xfer, free_list);
1348 1.2 bouyer splx(s);
1349 1.2 bouyer }
1350 1.2 bouyer
1351 1.2 bouyer void
1352 1.2 bouyer wdcerror(wdc, msg)
1353 1.2 bouyer struct wdc_softc *wdc;
1354 1.2 bouyer char *msg;
1355 1.2 bouyer {
1356 1.2 bouyer struct wdc_xfer *xfer = wdc->sc_xfer.tqh_first;
1357 1.2 bouyer if (xfer == NULL)
1358 1.2 bouyer printf("%s: %s\n", wdc->sc_dev.dv_xname, msg);
1359 1.2 bouyer else
1360 1.2 bouyer printf("%s(%d): %s\n", wdc->sc_dev.dv_xname,
1361 1.2 bouyer xfer->d_link->drive, msg);
1362 1.2 bouyer }
1363 1.2 bouyer
1364 1.2 bouyer /*
1365 1.2 bouyer * the bit bucket
1366 1.2 bouyer */
1367 1.2 bouyer void
1368 1.2 bouyer wdcbit_bucket(wdc, size)
1369 1.2 bouyer struct wdc_softc *wdc;
1370 1.2 bouyer int size;
1371 1.2 bouyer {
1372 1.2 bouyer
1373 1.12 cgd for (; size >= 2; size -= 2)
1374 1.12 cgd (void)bus_space_read_2(wdc->sc_iot, wdc->sc_ioh, wd_data);
1375 1.12 cgd if (size)
1376 1.12 cgd (void)bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_data);
1377 1.2 bouyer }
1378 1.2 bouyer
1379 1.2 bouyer
1380 1.2 bouyer #if NATAPIBUS > 0
1381 1.2 bouyer
1382 1.2 bouyer void
1383 1.2 bouyer wdc_atapi_minphys (struct buf *bp)
1384 1.2 bouyer {
1385 1.2 bouyer if(bp->b_bcount > MAX_SIZE)
1386 1.2 bouyer bp->b_bcount = MAX_SIZE;
1387 1.2 bouyer minphys(bp);
1388 1.2 bouyer }
1389 1.2 bouyer
1390 1.2 bouyer
1391 1.2 bouyer void
1392 1.2 bouyer wdc_atapi_start(wdc, xfer)
1393 1.2 bouyer struct wdc_softc *wdc;
1394 1.2 bouyer struct wdc_xfer *xfer;
1395 1.2 bouyer {
1396 1.2 bouyer struct scsipi_xfer *sc_xfer = xfer->atapi_cmd;
1397 1.2 bouyer
1398 1.2 bouyer #ifdef ATAPI_DEBUG_WDC
1399 1.2 bouyer printf("wdc_atapi_start, acp flags %x \n",sc_xfer->flags);
1400 1.2 bouyer #endif
1401 1.2 bouyer if (wdc->sc_errors >= WDIORETRIES) {
1402 1.2 bouyer if ((wdc->sc_status & WDCS_ERR) == 0) {
1403 1.2 bouyer sc_xfer->error = XS_DRIVER_STUFFUP; /* XXX do we know more ? */
1404 1.2 bouyer } else {
1405 1.2 bouyer sc_xfer->error = XS_SENSE;
1406 1.12 cgd sc_xfer->sense.atapi_sense = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_error);
1407 1.2 bouyer }
1408 1.2 bouyer wdc_atapi_done(wdc, xfer);
1409 1.2 bouyer return;
1410 1.2 bouyer }
1411 1.2 bouyer if (wait_for_unbusy(wdc) != 0) {
1412 1.2 bouyer if ((wdc->sc_status & WDCS_ERR) == 0) {
1413 1.2 bouyer printf("wdc_atapi_start: not ready, st = %02x\n",
1414 1.2 bouyer wdc->sc_status);
1415 1.2 bouyer sc_xfer->error = XS_SELTIMEOUT;
1416 1.2 bouyer }
1417 1.2 bouyer #if 0 /* don't get the sense yet, as this may be just UNIT ATTENTION */
1418 1.2 bouyer else {
1419 1.2 bouyer #ifdef ATAPI_DEBUG_WDC
1420 1.2 bouyer printf("wdc_atapi_start: sense %02x\n", wdc->sc_error);
1421 1.2 bouyer #endif
1422 1.2 bouyer sc_xfer->error = XS_SENSE;
1423 1.2 bouyer sc_xfer->sense.atapi_sense = wdc->sc_error;
1424 1.2 bouyer }
1425 1.2 bouyer wdc_atapi_done(wdc, xfer);
1426 1.2 bouyer return;
1427 1.2 bouyer #endif
1428 1.2 bouyer }
1429 1.2 bouyer
1430 1.12 cgd /*
1431 1.12 cgd * Limit length to what can be stuffed into the cylinder register
1432 1.12 cgd * (16 bits). Some CD-ROMs seem to interpret '0' as 65536,
1433 1.12 cgd * but not all devices do that and it's not obvious from the
1434 1.12 cgd * ATAPI spec that that behaviour should be expected. If more
1435 1.12 cgd * data is necessary, multiple data transfer phases will be done.
1436 1.12 cgd */
1437 1.2 bouyer if (wdccommand(wdc, (struct wd_link*)xfer->d_link, ATAPI_PACKET_COMMAND,
1438 1.12 cgd sc_xfer->sc_link->scsipi_atapi.drive,
1439 1.12 cgd sc_xfer->datalen <= 0xffff ? sc_xfer->datalen : 0xffff,
1440 1.2 bouyer 0, 0, 0) != 0) {
1441 1.10 mikel printf("wdc_atapi_start: can't send atapi packet command\n");
1442 1.2 bouyer sc_xfer->error = XS_DRIVER_STUFFUP;
1443 1.2 bouyer wdc_atapi_done(wdc, xfer);
1444 1.2 bouyer return;
1445 1.2 bouyer }
1446 1.2 bouyer if ((sc_xfer->sc_link->scsipi_atapi.cap & 0x0300) != ACAP_DRQ_INTR) {
1447 1.2 bouyer int i, phase;
1448 1.2 bouyer for (i=20000; i>0; --i) {
1449 1.12 cgd phase = (bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_ireason) &
1450 1.2 bouyer (WDCI_CMD | WDCI_IN)) |
1451 1.12 cgd (bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_status) & WDCS_DRQ);
1452 1.2 bouyer if (phase == PHASE_CMDOUT)
1453 1.2 bouyer break;
1454 1.2 bouyer delay(10);
1455 1.2 bouyer }
1456 1.2 bouyer if (phase != PHASE_CMDOUT ) {
1457 1.10 mikel printf("wdc_atapi_start: timeout waiting PHASE_CMDOUT");
1458 1.2 bouyer sc_xfer->error = XS_SELTIMEOUT;
1459 1.2 bouyer wdc_atapi_done(wdc, xfer);
1460 1.2 bouyer return;
1461 1.2 bouyer }
1462 1.12 cgd bus_space_write_multi_2(wdc->sc_iot, wdc->sc_ioh, wd_data,
1463 1.12 cgd (u_int16_t *)sc_xfer->cmd, sc_xfer->cmdlen >> 1);
1464 1.2 bouyer }
1465 1.2 bouyer wdc->sc_flags |= WDCF_IRQ_WAIT;
1466 1.2 bouyer
1467 1.2 bouyer #ifdef ATAPI_DEBUG2
1468 1.2 bouyer printf("wdc_atapi_start: timeout\n");
1469 1.2 bouyer #endif
1470 1.2 bouyer timeout(wdctimeout, wdc, WAITTIME);
1471 1.2 bouyer return;
1472 1.2 bouyer }
1473 1.2 bouyer
1474 1.2 bouyer
1475 1.2 bouyer int
1476 1.2 bouyer wdc_atapi_get_params(ab_link, drive, id)
1477 1.2 bouyer struct scsipi_link *ab_link;
1478 1.2 bouyer u_int8_t drive;
1479 1.2 bouyer struct atapi_identify *id;
1480 1.2 bouyer {
1481 1.2 bouyer struct wdc_softc *wdc = (void*)ab_link->adapter_softc;
1482 1.2 bouyer int status, len, excess = 0;
1483 1.2 bouyer int s, error;
1484 1.2 bouyer
1485 1.4 bouyer /* if a disk is already present, skip */
1486 1.4 bouyer if ((wdc->sc_drives_mask & (1 << drive)) != 0) {
1487 1.2 bouyer #ifdef ATAPI_DEBUG_PROBE
1488 1.4 bouyer printf("wdc_atapi_get_params: drive %d present\n", drive);
1489 1.2 bouyer #endif
1490 1.2 bouyer return 0;
1491 1.2 bouyer }
1492 1.2 bouyer
1493 1.2 bouyer /*
1494 1.2 bouyer * If there is only one ATAPI slave on the bus,don't probe
1495 1.2 bouyer * drive 0 (master)
1496 1.2 bouyer */
1497 1.2 bouyer
1498 1.2 bouyer if (wdc->sc_flags & WDCF_ONESLAVE && drive != 1)
1499 1.2 bouyer return 0;
1500 1.2 bouyer
1501 1.2 bouyer #ifdef ATAPI_DEBUG_PROBE
1502 1.2 bouyer printf("wdc_atapi_get_params: probing drive %d\n", drive);
1503 1.2 bouyer #endif
1504 1.2 bouyer
1505 1.2 bouyer /*
1506 1.2 bouyer * XXX
1507 1.2 bouyer * The locking done here, and the length of time this may keep the rest
1508 1.2 bouyer * of the system suspended, is a kluge. This should be rewritten to
1509 1.2 bouyer * set up a transfer and queue it through wdstart(), but it's called
1510 1.2 bouyer * infrequently enough that this isn't a pressing matter.
1511 1.2 bouyer */
1512 1.2 bouyer
1513 1.2 bouyer s = splbio();
1514 1.2 bouyer
1515 1.2 bouyer while ((wdc->sc_flags & WDCF_ACTIVE) != 0) {
1516 1.2 bouyer wdc->sc_flags |= WDCF_WANTED;
1517 1.2 bouyer if ((error = tsleep(wdc, PRIBIO | PCATCH, "atprm", 0)) != 0) {
1518 1.2 bouyer splx(s);
1519 1.2 bouyer return error;
1520 1.2 bouyer }
1521 1.2 bouyer }
1522 1.2 bouyer
1523 1.2 bouyer wdc->sc_flags |= WDCF_ACTIVE;
1524 1.2 bouyer error = 1;
1525 1.2 bouyer (void)wdcreset(wdc, VERBOSE);
1526 1.2 bouyer if ((status = wdccommand(wdc, (struct wd_link*)(&(ab_link->scsipi_atapi)),
1527 1.2 bouyer ATAPI_SOFT_RESET, drive, 0, 0, 0, 0)) != 0) {
1528 1.2 bouyer #ifdef ATAPI_DEBUG
1529 1.2 bouyer printf("wdc_atapi_get_params: ATAPI_SOFT_RESET"
1530 1.2 bouyer "failed for drive %d: status %d error %d\n",
1531 1.2 bouyer drive, status, wdc->sc_error);
1532 1.2 bouyer #endif
1533 1.2 bouyer error = 0;
1534 1.2 bouyer goto end;
1535 1.2 bouyer }
1536 1.2 bouyer if ((status = wait_for_unbusy(wdc)) != 0) {
1537 1.2 bouyer #ifdef ATAPI_DEBUG
1538 1.2 bouyer printf("wdc_atapi_get_params: wait_for_unbusy failed "
1539 1.2 bouyer "for drive %d: status %d error %d\n",
1540 1.2 bouyer drive, status, wdc->sc_error);
1541 1.2 bouyer #endif
1542 1.2 bouyer error = 0;
1543 1.2 bouyer goto end;
1544 1.2 bouyer }
1545 1.18 cgd
1546 1.18 cgd /* Some ATAPI devices seem a bit more time after software reset. */
1547 1.18 cgd delay(5000);
1548 1.2 bouyer
1549 1.2 bouyer if (wdccommand(wdc, (struct wd_link*)(&(ab_link->scsipi_atapi)),
1550 1.2 bouyer ATAPI_IDENTIFY_DEVICE, drive, sizeof(struct atapi_identify),
1551 1.2 bouyer 0, 0, 0) != 0 ||
1552 1.2 bouyer atapi_ready(wdc) != 0) {
1553 1.2 bouyer #ifdef ATAPI_DEBUG_PROBE
1554 1.2 bouyer printf("ATAPI_IDENTIFY_DEVICE failed for drive %d\n", drive);
1555 1.2 bouyer #endif
1556 1.2 bouyer error = 0;
1557 1.2 bouyer goto end;
1558 1.2 bouyer }
1559 1.12 cgd len = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_cyl_lo) + 256 *
1560 1.12 cgd bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_cyl_hi);
1561 1.2 bouyer if (len != sizeof(struct atapi_identify)) {
1562 1.2 bouyer printf("Warning drive %d returned %d/%d of "
1563 1.2 bouyer "indentify device data\n", drive, len,
1564 1.2 bouyer sizeof(struct atapi_identify));
1565 1.2 bouyer excess = len - sizeof(struct atapi_identify);
1566 1.2 bouyer if (excess < 0)
1567 1.2 bouyer excess = 0;
1568 1.2 bouyer }
1569 1.12 cgd bus_space_read_multi_2(wdc->sc_iot, wdc->sc_ioh, wd_data,
1570 1.12 cgd (u_int16_t *)id, sizeof(struct atapi_identify) >> 1);
1571 1.2 bouyer wdcbit_bucket(wdc, excess);
1572 1.4 bouyer wdc->sc_drives_mask |= (1 << drive);
1573 1.2 bouyer
1574 1.2 bouyer end: /* Restart the queue. */
1575 1.2 bouyer WDDEBUG_PRINT(("wdcstart from wdc_atapi_get_parms flags 0x%x\n",
1576 1.2 bouyer wdc->sc_flags));
1577 1.2 bouyer wdc->sc_flags &= ~WDCF_ACTIVE;
1578 1.2 bouyer wdcstart(wdc);
1579 1.2 bouyer splx(s);
1580 1.2 bouyer return error;
1581 1.2 bouyer }
1582 1.2 bouyer
1583 1.2 bouyer int
1584 1.2 bouyer wdc_atapi_send_command_packet(sc_xfer)
1585 1.2 bouyer struct scsipi_xfer *sc_xfer;
1586 1.2 bouyer {
1587 1.2 bouyer struct scsipi_link *sc_link = sc_xfer->sc_link;
1588 1.2 bouyer struct wdc_softc *wdc = (void*)sc_link->adapter_softc;
1589 1.2 bouyer struct wdc_xfer *xfer;
1590 1.2 bouyer int flags = sc_xfer->flags;
1591 1.8 bouyer
1592 1.2 bouyer if (flags & SCSI_POLL) { /* should use the queue and wdc_atapi_start */
1593 1.2 bouyer struct wdc_xfer xfer_s;
1594 1.2 bouyer int i, s;
1595 1.2 bouyer
1596 1.2 bouyer s = splbio();
1597 1.2 bouyer #ifdef ATAPI_DEBUG_WDC
1598 1.2 bouyer printf("wdc_atapi_send_cmd: "
1599 1.2 bouyer "flags 0x%x drive %d cmdlen %d datalen %d",
1600 1.2 bouyer sc_xfer->flags, sc_link->scsipi_atapi.drive, sc_xfer->cmdlen,
1601 1.2 bouyer sc_xfer->datalen);
1602 1.2 bouyer #endif
1603 1.2 bouyer xfer = &xfer_s;
1604 1.2 bouyer bzero(xfer, sizeof(xfer_s));
1605 1.2 bouyer xfer->c_flags = C_INUSE|C_ATAPI|flags;
1606 1.2 bouyer xfer->d_link = (struct wd_link *)(&sc_link->scsipi_atapi);
1607 1.2 bouyer xfer->c_bp = sc_xfer->bp;
1608 1.2 bouyer xfer->atapi_cmd = sc_xfer;
1609 1.2 bouyer xfer->c_blkno = 0;
1610 1.2 bouyer xfer->databuf = sc_xfer->data;
1611 1.2 bouyer xfer->c_bcount = sc_xfer->datalen;
1612 1.12 cgd
1613 1.2 bouyer if (wait_for_unbusy (wdc) != 0) {
1614 1.2 bouyer if ((wdc->sc_status & WDCS_ERR) == 0) {
1615 1.2 bouyer printf("wdc_atapi_send_command: not ready, "
1616 1.2 bouyer "st = %02x\n", wdc->sc_status);
1617 1.2 bouyer sc_xfer->error = XS_SELTIMEOUT;
1618 1.2 bouyer } else {
1619 1.2 bouyer sc_xfer->error = XS_SENSE;
1620 1.2 bouyer sc_xfer->sense.atapi_sense = wdc->sc_error;
1621 1.2 bouyer }
1622 1.2 bouyer splx(s);
1623 1.2 bouyer return COMPLETE;
1624 1.2 bouyer }
1625 1.2 bouyer
1626 1.12 cgd /*
1627 1.12 cgd * Limit length to what can be stuffed into the cylinder
1628 1.12 cgd * register (16 bits). Some CD-ROMs seem to interpret '0'
1629 1.12 cgd * as 65536, but not all devices do that and it's not
1630 1.12 cgd * obvious from the ATAPI spec that that behaviour should
1631 1.12 cgd * be expected. If more data is necessary, multiple data
1632 1.12 cgd * transfer phases will be done.
1633 1.12 cgd */
1634 1.2 bouyer if (wdccommand(wdc, (struct wd_link*)(&sc_link->scsipi_atapi),
1635 1.12 cgd ATAPI_PACKET_COMMAND, sc_link->scsipi_atapi.drive,
1636 1.12 cgd sc_xfer->datalen <= 0xffff ? sc_xfer->datalen : 0xffff,
1637 1.2 bouyer 0, 0, 0) != 0) {
1638 1.10 mikel printf("can't send atapi packet command\n");
1639 1.2 bouyer sc_xfer->error = XS_DRIVER_STUFFUP;
1640 1.2 bouyer splx(s);
1641 1.2 bouyer return COMPLETE;
1642 1.2 bouyer }
1643 1.2 bouyer
1644 1.2 bouyer /* Wait for cmd i/o phase. */
1645 1.2 bouyer for (i = 20000; i > 0; --i) {
1646 1.2 bouyer int phase;
1647 1.12 cgd phase = (bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_ireason) &
1648 1.2 bouyer (WDCI_CMD | WDCI_IN)) |
1649 1.12 cgd (bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_status) & WDCS_DRQ);
1650 1.2 bouyer if (phase == PHASE_CMDOUT)
1651 1.2 bouyer break;
1652 1.2 bouyer delay(10);
1653 1.2 bouyer }
1654 1.2 bouyer #ifdef ATAPI_DEBUG_WDC
1655 1.2 bouyer printf("Wait for cmd i/o phase: i = %d\n", i);
1656 1.2 bouyer #endif
1657 1.2 bouyer
1658 1.12 cgd bus_space_write_multi_2(wdc->sc_iot, wdc->sc_ioh, wd_data,
1659 1.12 cgd (u_int16_t *)sc_xfer->cmd, sc_xfer->cmdlen >> 1);
1660 1.2 bouyer
1661 1.2 bouyer /* Wait for data i/o phase. */
1662 1.2 bouyer for ( i= 20000; i > 0; --i) {
1663 1.2 bouyer int phase;
1664 1.12 cgd phase = (bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_ireason) &
1665 1.2 bouyer (WDCI_CMD | WDCI_IN)) |
1666 1.12 cgd (bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_status) & WDCS_DRQ);
1667 1.2 bouyer if (phase != PHASE_CMDOUT)
1668 1.2 bouyer break;
1669 1.2 bouyer delay(10);
1670 1.2 bouyer }
1671 1.2 bouyer
1672 1.2 bouyer #ifdef ATAPI_DEBUG_WDC
1673 1.2 bouyer printf("Wait for data i/o phase: i = %d\n", i);
1674 1.2 bouyer #endif
1675 1.8 bouyer wdc->sc_flags |= WDCF_IRQ_WAIT;
1676 1.5 bouyer while ((sc_xfer->flags & ITSDONE) == 0) {
1677 1.5 bouyer wdc_atapi_intr(wdc, xfer);
1678 1.2 bouyer for (i = 2000; i > 0; --i)
1679 1.12 cgd if ((bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_status)
1680 1.2 bouyer & WDCS_DRQ) == 0)
1681 1.2 bouyer break;
1682 1.2 bouyer #ifdef ATAPI_DEBUG_WDC
1683 1.2 bouyer printf("wdc_atapi_intr: i = %d\n", i);
1684 1.2 bouyer #endif
1685 1.2 bouyer }
1686 1.2 bouyer wdc->sc_flags &= ~(WDCF_IRQ_WAIT | WDCF_SINGLE | WDCF_ERROR);
1687 1.2 bouyer wdc->sc_errors = 0;
1688 1.2 bouyer xfer->c_skip = 0;
1689 1.2 bouyer splx(s);
1690 1.2 bouyer return COMPLETE;
1691 1.2 bouyer } else { /* POLLED */
1692 1.2 bouyer xfer = wdc_get_xfer(flags & SCSI_NOSLEEP ? IDE_NOSLEEP : 0);
1693 1.2 bouyer if (xfer == NULL) {
1694 1.2 bouyer return TRY_AGAIN_LATER;
1695 1.2 bouyer }
1696 1.2 bouyer xfer->c_flags |= C_ATAPI|sc_xfer->flags;
1697 1.2 bouyer xfer->d_link = (struct wd_link*)(&sc_link->scsipi_atapi);
1698 1.2 bouyer xfer->c_bp = sc_xfer->bp;
1699 1.2 bouyer xfer->atapi_cmd = sc_xfer;
1700 1.2 bouyer xfer->c_blkno = 0;
1701 1.2 bouyer xfer->databuf = sc_xfer->data;
1702 1.2 bouyer xfer->c_bcount = sc_xfer->datalen;
1703 1.2 bouyer wdc_exec_xfer(wdc, xfer->d_link, xfer);
1704 1.2 bouyer #ifdef ATAPI_DEBUG_WDC
1705 1.2 bouyer printf("wdc_atapi_send_command_packet: wdc_exec_xfer, flags 0x%x\n",
1706 1.2 bouyer sc_xfer->flags);
1707 1.2 bouyer #endif
1708 1.2 bouyer return (sc_xfer->flags & ITSDONE) ? COMPLETE : SUCCESSFULLY_QUEUED;
1709 1.2 bouyer }
1710 1.2 bouyer }
1711 1.2 bouyer
1712 1.2 bouyer int
1713 1.2 bouyer wdc_atapi_intr(wdc, xfer)
1714 1.2 bouyer struct wdc_softc *wdc;
1715 1.2 bouyer struct wdc_xfer *xfer;
1716 1.2 bouyer {
1717 1.2 bouyer struct scsipi_xfer *sc_xfer = xfer->atapi_cmd;
1718 1.2 bouyer int len, phase, i, retries=0;
1719 1.2 bouyer int err, st, ire;
1720 1.2 bouyer
1721 1.2 bouyer #ifdef ATAPI_DEBUG2
1722 1.2 bouyer printf("wdc_atapi_intr: %s\n", wdc->sc_dev.dv_xname);
1723 1.2 bouyer #endif
1724 1.2 bouyer
1725 1.2 bouyer if (wait_for_unbusy(wdc) < 0) {
1726 1.2 bouyer if ((wdc->sc_status & WDCS_ERR) == 0) {
1727 1.2 bouyer printf("wdc_atapi_intr: controller busy\n");
1728 1.5 bouyer return 0;
1729 1.2 bouyer } else {
1730 1.2 bouyer sc_xfer->error = XS_SENSE;
1731 1.2 bouyer sc_xfer->sense.atapi_sense = wdc->sc_error;
1732 1.2 bouyer }
1733 1.2 bouyer #ifdef ATAPI_DEBUG_WDC
1734 1.2 bouyer printf("wdc_atapi_intr: wdc_atapi_done(), error %d\n",
1735 1.2 bouyer sc_xfer->error);
1736 1.2 bouyer #endif
1737 1.2 bouyer wdc_atapi_done(wdc, xfer);
1738 1.2 bouyer return 0;
1739 1.2 bouyer }
1740 1.2 bouyer
1741 1.2 bouyer
1742 1.2 bouyer again:
1743 1.12 cgd len = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_cyl_lo) +
1744 1.12 cgd 256 * bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_cyl_hi);
1745 1.2 bouyer
1746 1.12 cgd st = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_status);
1747 1.12 cgd err = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_error);
1748 1.12 cgd ire = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_ireason);
1749 1.2 bouyer
1750 1.2 bouyer phase = (ire & (WDCI_CMD | WDCI_IN)) | (st & WDCS_DRQ);
1751 1.2 bouyer #ifdef ATAPI_DEBUG_WDC
1752 1.2 bouyer printf("wdc_atapi_intr: len %d st %d err %d ire %d :",
1753 1.2 bouyer len, st, err, ire);
1754 1.2 bouyer #endif
1755 1.2 bouyer switch (phase) {
1756 1.2 bouyer case PHASE_CMDOUT:
1757 1.2 bouyer /* send packet command */
1758 1.2 bouyer #ifdef ATAPI_DEBUG_WDC
1759 1.2 bouyer printf("PHASE_CMDOUT\n");
1760 1.2 bouyer #endif
1761 1.2 bouyer
1762 1.2 bouyer #ifdef ATAPI_DEBUG_WDC
1763 1.2 bouyer {
1764 1.2 bouyer int i;
1765 1.2 bouyer char *c = (char *)sc_xfer->cmd;
1766 1.2 bouyer printf("wdc_atapi_intr: cmd ");
1767 1.2 bouyer for (i = 0; i < sc_xfer->cmdlen; i++)
1768 1.2 bouyer printf("%x ", c[i]);
1769 1.2 bouyer printf("\n");
1770 1.2 bouyer }
1771 1.2 bouyer #endif
1772 1.2 bouyer
1773 1.12 cgd bus_space_write_multi_2(wdc->sc_iot, wdc->sc_ioh, wd_data,
1774 1.12 cgd (u_int16_t *)sc_xfer->cmd, sc_xfer->cmdlen >> 1);
1775 1.2 bouyer return 1;
1776 1.2 bouyer
1777 1.2 bouyer case PHASE_DATAOUT:
1778 1.2 bouyer /* write data */
1779 1.2 bouyer #ifdef ATAPI_DEBUG_WDC
1780 1.2 bouyer printf("PHASE_DATAOUT\n");
1781 1.2 bouyer #endif
1782 1.2 bouyer if ((sc_xfer->flags & SCSI_DATA_OUT) == 0) {
1783 1.2 bouyer printf("wdc_atapi_intr: bad data phase\n");
1784 1.2 bouyer sc_xfer->error = XS_DRIVER_STUFFUP;
1785 1.5 bouyer return 0;
1786 1.2 bouyer }
1787 1.2 bouyer if (xfer->c_bcount < len) {
1788 1.2 bouyer printf("wdc_atapi_intr: warning: write only "
1789 1.2 bouyer "%d of %d requested bytes\n", xfer->c_bcount, len);
1790 1.12 cgd bus_space_write_multi_2(wdc->sc_iot, wdc->sc_ioh,
1791 1.12 cgd wd_data, xfer->databuf + xfer->c_skip,
1792 1.12 cgd xfer->c_bcount >> 1);
1793 1.12 cgd for (i = xfer->c_bcount; i < len; i += 2)
1794 1.12 cgd bus_space_write_2(wdc->sc_iot, wdc->sc_ioh,
1795 1.12 cgd wd_data, 0);
1796 1.8 bouyer xfer->c_skip += xfer->c_bcount;
1797 1.2 bouyer xfer->c_bcount = 0;
1798 1.2 bouyer } else {
1799 1.12 cgd bus_space_write_multi_2(wdc->sc_iot, wdc->sc_ioh,
1800 1.12 cgd wd_data, xfer->databuf + xfer->c_skip, len >> 1);
1801 1.2 bouyer xfer->c_skip += len;
1802 1.2 bouyer xfer->c_bcount -= len;
1803 1.2 bouyer }
1804 1.8 bouyer return 1;
1805 1.2 bouyer
1806 1.2 bouyer case PHASE_DATAIN:
1807 1.2 bouyer /* Read data */
1808 1.2 bouyer #ifdef ATAPI_DEBUG_WDC
1809 1.2 bouyer printf("PHASE_DATAIN\n");
1810 1.2 bouyer #endif
1811 1.2 bouyer if ((sc_xfer->flags & SCSI_DATA_IN) == 0) {
1812 1.2 bouyer printf("wdc_atapi_intr: bad data phase\n");
1813 1.2 bouyer sc_xfer->error = XS_DRIVER_STUFFUP;
1814 1.5 bouyer return 0;
1815 1.2 bouyer }
1816 1.2 bouyer if (xfer->c_bcount < len) {
1817 1.2 bouyer printf("wdc_atapi_intr: warning: reading only "
1818 1.2 bouyer "%d of %d bytes\n", xfer->c_bcount, len);
1819 1.12 cgd bus_space_read_multi_2(wdc->sc_iot, wdc->sc_ioh,
1820 1.12 cgd wd_data, xfer->databuf + xfer->c_skip,
1821 1.12 cgd xfer->c_bcount >> 1);
1822 1.2 bouyer wdcbit_bucket(wdc, len - xfer->c_bcount);
1823 1.8 bouyer xfer->c_skip += xfer->c_bcount;
1824 1.2 bouyer xfer->c_bcount = 0;
1825 1.2 bouyer } else {
1826 1.12 cgd bus_space_read_multi_2(wdc->sc_iot, wdc->sc_ioh,
1827 1.12 cgd wd_data, xfer->databuf + xfer->c_skip, len >> 1);
1828 1.2 bouyer xfer->c_skip += len;
1829 1.2 bouyer xfer->c_bcount -=len;
1830 1.2 bouyer }
1831 1.8 bouyer return 1;
1832 1.2 bouyer
1833 1.2 bouyer case PHASE_ABORTED:
1834 1.2 bouyer case PHASE_COMPLETED:
1835 1.2 bouyer #ifdef ATAPI_DEBUG_WDC
1836 1.2 bouyer printf("PHASE_COMPLETED\n");
1837 1.2 bouyer #endif
1838 1.2 bouyer if (st & WDCS_ERR) {
1839 1.2 bouyer sc_xfer->error = XS_SENSE;
1840 1.12 cgd sc_xfer->sense.atapi_sense = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_error);
1841 1.2 bouyer }
1842 1.2 bouyer #ifdef ATAPI_DEBUG_WDC
1843 1.2 bouyer if (xfer->c_bcount != 0) {
1844 1.2 bouyer printf("wdc_atapi_intr warning: bcount value "
1845 1.2 bouyer "is %d after io\n", xfer->c_bcount);
1846 1.2 bouyer }
1847 1.2 bouyer #endif
1848 1.2 bouyer break;
1849 1.2 bouyer
1850 1.2 bouyer default:
1851 1.2 bouyer if (++retries<500) {
1852 1.2 bouyer DELAY(100);
1853 1.2 bouyer goto again;
1854 1.2 bouyer }
1855 1.2 bouyer printf("wdc_atapi_intr: unknown phase %d\n", phase);
1856 1.2 bouyer if (st & WDCS_ERR) {
1857 1.2 bouyer sc_xfer->error = XS_SENSE;
1858 1.12 cgd sc_xfer->sense.atapi_sense = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_error);
1859 1.2 bouyer } else {
1860 1.2 bouyer sc_xfer->error = XS_DRIVER_STUFFUP;
1861 1.2 bouyer }
1862 1.2 bouyer }
1863 1.2 bouyer
1864 1.2 bouyer #ifdef ATAPI_DEBUG_WDC
1865 1.2 bouyer printf("wdc_atapi_intr: wdc_atapi_done() (end), error %d\n",
1866 1.2 bouyer sc_xfer->error);
1867 1.2 bouyer #endif
1868 1.2 bouyer wdc_atapi_done(wdc, xfer);
1869 1.5 bouyer return (1);
1870 1.2 bouyer }
1871 1.2 bouyer
1872 1.2 bouyer
1873 1.2 bouyer void
1874 1.2 bouyer wdc_atapi_done(wdc, xfer)
1875 1.2 bouyer struct wdc_softc *wdc;
1876 1.2 bouyer struct wdc_xfer *xfer;
1877 1.2 bouyer {
1878 1.2 bouyer struct scsipi_xfer *sc_xfer = xfer->atapi_cmd;
1879 1.2 bouyer int s;
1880 1.2 bouyer int need_done = xfer->c_flags & C_NEEDDONE;
1881 1.19 leo
1882 1.19 leo if (wdc->sc_cap & WDC_CAPABILITY_HWLOCK)
1883 1.19 leo (*wdc->sc_free_hw)(wdc);
1884 1.2 bouyer
1885 1.2 bouyer #ifdef ATAPI_DEBUG
1886 1.2 bouyer printf("wdc_atapi_done: flags 0x%x\n", (u_int)xfer->c_flags);
1887 1.2 bouyer #endif
1888 1.2 bouyer sc_xfer->resid = xfer->c_bcount;
1889 1.8 bouyer wdc->sc_flags &= ~WDCF_IRQ_WAIT;
1890 1.2 bouyer
1891 1.2 bouyer /* remove this command from xfer queue */
1892 1.2 bouyer wdc->sc_errors = 0;
1893 1.2 bouyer xfer->c_skip = 0;
1894 1.2 bouyer if ((xfer->c_flags & SCSI_POLL) == 0) {
1895 1.2 bouyer s = splbio();
1896 1.2 bouyer untimeout(wdctimeout, wdc);
1897 1.2 bouyer TAILQ_REMOVE(&wdc->sc_xfer, xfer, c_xferchain);
1898 1.2 bouyer wdc->sc_flags &= ~(WDCF_SINGLE | WDCF_ERROR | WDCF_ACTIVE);
1899 1.2 bouyer wdc_free_xfer(xfer);
1900 1.2 bouyer sc_xfer->flags |= ITSDONE;
1901 1.2 bouyer if (need_done) {
1902 1.2 bouyer #ifdef ATAPI_DEBUG
1903 1.2 bouyer printf("wdc_atapi_done: scsipi_done\n");
1904 1.2 bouyer #endif
1905 1.2 bouyer scsipi_done(sc_xfer);
1906 1.2 bouyer }
1907 1.2 bouyer #ifdef WDDEBUG
1908 1.2 bouyer printf("wdcstart from wdc_atapi_intr, flags 0x%x\n",
1909 1.2 bouyer wdc->sc_flags);
1910 1.2 bouyer #endif
1911 1.2 bouyer wdcstart(wdc);
1912 1.2 bouyer splx(s);
1913 1.8 bouyer } else {
1914 1.2 bouyer wdc->sc_flags &= ~(WDCF_SINGLE | WDCF_ERROR | WDCF_ACTIVE);
1915 1.8 bouyer sc_xfer->flags |= ITSDONE;
1916 1.8 bouyer }
1917 1.2 bouyer }
1918 1.2 bouyer
1919 1.2 bouyer #endif /* NATAPIBUS > 0 */
1920