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