wdc.c revision 1.64 1 /* $NetBSD: wdc.c,v 1.64 1999/03/29 08:32:02 bouyer Exp $ */
2
3
4 /*
5 * Copyright (c) 1998 Manuel Bouyer. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Manuel Bouyer.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*-
34 * Copyright (c) 1998 The NetBSD Foundation, Inc.
35 * All rights reserved.
36 *
37 * This code is derived from software contributed to The NetBSD Foundation
38 * by Charles M. Hannum, by Onno van der Linden and by Manuel Bouyer.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by the NetBSD
51 * Foundation, Inc. and its contributors.
52 * 4. Neither the name of The NetBSD Foundation nor the names of its
53 * contributors may be used to endorse or promote products derived
54 * from this software without specific prior written permission.
55 *
56 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
57 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
58 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
59 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
60 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
61 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
62 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
63 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
64 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
65 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
66 * POSSIBILITY OF SUCH DAMAGE.
67 */
68
69 /*
70 * CODE UNTESTED IN THE CURRENT REVISION:
71 *
72 */
73
74 #ifndef WDCDEBUG
75 #define WDCDEBUG
76 #endif /* WDCDEBUG */
77
78 #include <sys/param.h>
79 #include <sys/systm.h>
80 #include <sys/kernel.h>
81 #include <sys/conf.h>
82 #include <sys/buf.h>
83 #include <sys/device.h>
84 #include <sys/malloc.h>
85 #include <sys/syslog.h>
86 #include <sys/proc.h>
87
88 #include <vm/vm.h>
89
90 #include <machine/intr.h>
91 #include <machine/bus.h>
92
93 #ifndef __BUS_SPACE_HAS_STREAM_METHODS
94 #define bus_space_write_multi_stream_2 bus_space_write_multi_2
95 #define bus_space_write_multi_stream_4 bus_space_write_multi_4
96 #define bus_space_read_multi_stream_2 bus_space_read_multi_2
97 #define bus_space_read_multi_stream_4 bus_space_read_multi_4
98 #endif /* __BUS_SPACE_HAS_STREAM_METHODS */
99
100 #include <dev/ata/atavar.h>
101 #include <dev/ata/atareg.h>
102 #include <dev/ic/wdcreg.h>
103 #include <dev/ic/wdcvar.h>
104
105 #include "atapibus.h"
106
107 #define WDCDELAY 100 /* 100 microseconds */
108 #define WDCNDELAY_RST (WDC_RESET_WAIT * 1000 / WDCDELAY)
109 #if 0
110 /* If you enable this, it will report any delays more than WDCDELAY * N long. */
111 #define WDCNDELAY_DEBUG 50
112 #endif
113
114 LIST_HEAD(xfer_free_list, wdc_xfer) xfer_free_list;
115
116 static void __wdcerror __P((struct channel_softc*, char *));
117 static int __wdcwait_reset __P((struct channel_softc *, int));
118 void __wdccommand_done __P((struct channel_softc *, struct wdc_xfer *));
119 void __wdccommand_start __P((struct channel_softc *, struct wdc_xfer *));
120 int __wdccommand_intr __P((struct channel_softc *, struct wdc_xfer *));
121 int wdprint __P((void *, const char *));
122
123
124 #define DEBUG_INTR 0x01
125 #define DEBUG_XFERS 0x02
126 #define DEBUG_STATUS 0x04
127 #define DEBUG_FUNCS 0x08
128 #define DEBUG_PROBE 0x10
129 #ifdef WDCDEBUG
130 int wdcdebug_mask = 0;
131 int wdc_nxfer = 0;
132 #define WDCDEBUG_PRINT(args, level) if (wdcdebug_mask & (level)) printf args
133 #else
134 #define WDCDEBUG_PRINT(args, level)
135 #endif
136
137 int
138 wdprint(aux, pnp)
139 void *aux;
140 const char *pnp;
141 {
142 struct ata_atapi_attach *aa_link = aux;
143 if (pnp)
144 printf("drive at %s", pnp);
145 printf(" channel %d drive %d", aa_link->aa_channel,
146 aa_link->aa_drv_data->drive);
147 return (UNCONF);
148 }
149
150 int
151 atapi_print(aux, pnp)
152 void *aux;
153 const char *pnp;
154 {
155 struct ata_atapi_attach *aa_link = aux;
156 if (pnp)
157 printf("atapibus at %s", pnp);
158 printf(" channel %d", aa_link->aa_channel);
159 return (UNCONF);
160 }
161
162 /* Test to see controller with at last one attached drive is there.
163 * Returns a bit for each possible drive found (0x01 for drive 0,
164 * 0x02 for drive 1).
165 * Logic:
166 * - If a status register is at 0xff, assume there is no drive here
167 * (ISA has pull-up resistors). If no drive at all -> return.
168 * - reset the controller, wait for it to complete (may take up to 31s !).
169 * If timeout -> return.
170 * - test ATA/ATAPI signatures. If at last one drive found -> return.
171 * - try an ATA command on the master.
172 */
173
174 int
175 wdcprobe(chp)
176 struct channel_softc *chp;
177 {
178 u_int8_t st0, st1, sc, sn, cl, ch;
179 u_int8_t ret_value = 0x03;
180 u_int8_t drive;
181
182 /*
183 * Sanity check to see if the wdc channel responds at all.
184 */
185
186 if (chp->wdc == NULL ||
187 (chp->wdc->cap & WDC_CAPABILITY_NO_EXTRA_RESETS) == 0) {
188 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
189 WDSD_IBM);
190 delay(1);
191 st0 = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_status);
192 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
193 WDSD_IBM | 0x10);
194 delay(1);
195 st1 = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_status);
196
197 WDCDEBUG_PRINT(("%s:%d: before reset, st0=0x%x, st1=0x%x\n",
198 chp->wdc ? chp->wdc->sc_dev.dv_xname : "wdcprobe",
199 chp->channel, st0, st1), DEBUG_PROBE);
200
201 if (st0 == 0xff)
202 ret_value &= ~0x01;
203 if (st1 == 0xff)
204 ret_value &= ~0x02;
205 if (ret_value == 0)
206 return 0;
207 }
208
209 /* assert SRST, wait for reset to complete */
210 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
211 WDSD_IBM);
212 delay(1);
213 bus_space_write_1(chp->ctl_iot, chp->ctl_ioh, wd_aux_ctlr,
214 WDCTL_RST | WDCTL_IDS);
215 DELAY(1000);
216 bus_space_write_1(chp->ctl_iot, chp->ctl_ioh, wd_aux_ctlr,
217 WDCTL_IDS);
218 delay(1000);
219 (void) bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_error);
220 bus_space_write_1(chp->ctl_iot, chp->ctl_ioh, wd_aux_ctlr, WDCTL_4BIT);
221 delay(1);
222
223 ret_value = __wdcwait_reset(chp, ret_value);
224 WDCDEBUG_PRINT(("%s:%d: after reset, ret_value=0x%d\n",
225 chp->wdc ? chp->wdc->sc_dev.dv_xname : "wdcprobe", chp->channel,
226 ret_value), DEBUG_PROBE);
227
228 /* if reset failed, there's nothing here */
229 if (ret_value == 0)
230 return 0;
231
232 /*
233 * Test presence of drives. First test register signatures looking for
234 * ATAPI devices , then rescan and try an ATA command, in case it's an
235 * old drive.
236 * Fill in drive_flags accordingly
237 */
238 for (drive = 0; drive < 2; drive++) {
239 if ((ret_value & (0x01 << drive)) == 0)
240 continue;
241 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
242 WDSD_IBM | (drive << 4));
243 delay(1);
244 /* Save registers contents */
245 sc = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_seccnt);
246 sn = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_sector);
247 cl = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_cyl_lo);
248 ch = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_cyl_hi);
249
250 WDCDEBUG_PRINT(("%s:%d:%d: after reset, sc=0x%x sn=0x%x "
251 "cl=0x%x ch=0x%x\n",
252 chp->wdc ? chp->wdc->sc_dev.dv_xname : "wdcprobe",
253 chp->channel, drive, sc, sn, cl, ch), DEBUG_PROBE);
254 /*
255 * sc is supposted to be 0x1 for ATAPI but at last one drive
256 * set it to 0x0.
257 */
258 if ((sc == 0x00 || sc == 0x01) && sn == 0x01 &&
259 cl == 0x14 && ch == 0xeb) {
260 chp->ch_drive[drive].drive_flags |= DRIVE_ATAPI;
261 } else if (sc == 0x01 && sn == 0x01 &&
262 cl == 0x00 && ch == 0x00) {
263 chp->ch_drive[drive].drive_flags |= DRIVE_ATA;
264 }
265 }
266 /*
267 * Maybe there's an old device, try to detect it if we didn't
268 * find a ATA or ATAPI device.
269 */
270 if ((chp->ch_drive[0].drive_flags & DRIVE) != 0 ||
271 (chp->ch_drive[1].drive_flags & DRIVE) != 0)
272 return (ret_value);
273 for (drive = 0; drive < 2; drive++) {
274 if ((ret_value & (0x01 << drive)) == 0)
275 continue;
276 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
277 WDSD_IBM | (drive << 4));
278 delay(1);
279 /*
280 * Test registers writability (Error register not writable,
281 * but cyllo is), then try an ATA command.
282 */
283 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_error, 0x58);
284 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_cyl_lo, 0xa5);
285 if (bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_error) ==
286 0x58 ||
287 bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_cyl_lo) !=
288 0xa5) {
289 WDCDEBUG_PRINT(("%s:%d:%d: register writability "
290 "failed\n",
291 chp->wdc ? chp->wdc->sc_dev.dv_xname : "wdcprobe",
292 chp->channel, drive), DEBUG_PROBE);
293 ret_value &= ~(0x01 << drive);
294 continue;
295 }
296 if (wait_for_ready(chp, 10000) != 0) {
297 WDCDEBUG_PRINT(("%s:%d:%d: not ready\n",
298 chp->wdc ? chp->wdc->sc_dev.dv_xname : "wdcprobe",
299 chp->channel, drive), DEBUG_PROBE);
300 ret_value &= ~(0x01 << drive);
301 continue;
302 }
303 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_command,
304 WDCC_RECAL);
305 if (wait_for_ready(chp, 10000) == 0) {
306 chp->ch_drive[drive].drive_flags |=
307 DRIVE_OLD;
308 } else {
309 WDCDEBUG_PRINT(("%s:%d:%d: WDCC_RECAL failed\n",
310 chp->wdc ? chp->wdc->sc_dev.dv_xname : "wdcprobe",
311 chp->channel, drive), DEBUG_PROBE);
312 ret_value &= ~(0x01 << drive);
313 }
314 }
315 return (ret_value);
316 }
317
318 void
319 wdcattach(chp)
320 struct channel_softc *chp;
321 {
322 int channel_flags, ctrl_flags, i, error;
323 struct ata_atapi_attach aa_link;
324 struct ataparams params;
325 static int inited = 0;
326
327 if ((error = wdc_addref(chp)) != 0) {
328 printf("%s: unable to enable controller\n",
329 chp->wdc->sc_dev.dv_xname);
330 return;
331 }
332
333 if (wdcprobe(chp) == 0) {
334 /* If no drives, abort attach here. */
335 wdc_delref(chp);
336 return;
337 }
338
339 /* init list only once */
340 if (inited == 0) {
341 LIST_INIT(&xfer_free_list);
342 inited++;
343 }
344 TAILQ_INIT(&chp->ch_queue->sc_xfer);
345
346 for (i = 0; i < 2; i++) {
347 chp->ch_drive[i].chnl_softc = chp;
348 chp->ch_drive[i].drive = i;
349 /* If controller can't do 16bit flag the drives as 32bit */
350 if ((chp->wdc->cap &
351 (WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32)) ==
352 WDC_CAPABILITY_DATA32)
353 chp->ch_drive[i].drive_flags |= DRIVE_CAP32;
354
355 /* Issue a IDENTIFY command, to try to detect slave ghost */
356 if (ata_get_params(&chp->ch_drive[i], AT_POLL, ¶ms) !=
357 CMD_OK) {
358 chp->ch_drive[i].drive_flags &=
359 ~(DRIVE_ATA | DRIVE_ATAPI);
360 }
361 }
362 ctrl_flags = chp->wdc->sc_dev.dv_cfdata->cf_flags;
363 channel_flags = (ctrl_flags >> (NBBY * chp->channel)) & 0xff;
364
365 WDCDEBUG_PRINT(("wdcattach: ch_drive_flags 0x%x 0x%x\n",
366 chp->ch_drive[0].drive_flags, chp->ch_drive[1].drive_flags),
367 DEBUG_PROBE);
368
369 /*
370 * Attach an ATAPI bus, if needed.
371 */
372 if ((chp->ch_drive[0].drive_flags & DRIVE_ATAPI) ||
373 (chp->ch_drive[1].drive_flags & DRIVE_ATAPI)) {
374 #if NATAPIBUS > 0
375 wdc_atapibus_attach(chp);
376 #else
377 /*
378 * Fills in a fake aa_link and call config_found, so that
379 * the config machinery will print
380 * "atapibus at xxx not configured"
381 */
382 memset(&aa_link, 0, sizeof(struct ata_atapi_attach));
383 aa_link.aa_type = T_ATAPI;
384 aa_link.aa_channel = chp->channel;
385 aa_link.aa_openings = 1;
386 aa_link.aa_drv_data = 0;
387 aa_link.aa_bus_private = NULL;
388 (void)config_found(&chp->wdc->sc_dev, (void *)&aa_link,
389 atapi_print);
390 #endif
391 }
392
393 for (i = 0; i < 2; i++) {
394 if ((chp->ch_drive[i].drive_flags & DRIVE_ATA) == 0) {
395 continue;
396 }
397 memset(&aa_link, 0, sizeof(struct ata_atapi_attach));
398 aa_link.aa_type = T_ATA;
399 aa_link.aa_channel = chp->channel;
400 aa_link.aa_openings = 1;
401 aa_link.aa_drv_data = &chp->ch_drive[i];
402 if (config_found(&chp->wdc->sc_dev, (void *)&aa_link, wdprint))
403 wdc_probe_caps(&chp->ch_drive[i]);
404 }
405
406 /*
407 * reset drive_flags for unnatached devices, reset state for attached
408 * ones
409 */
410 for (i = 0; i < 2; i++) {
411 if (chp->ch_drive[i].drv_softc == NULL)
412 chp->ch_drive[i].drive_flags = 0;
413 else
414 chp->ch_drive[i].state = 0;
415 }
416
417 /*
418 * Reset channel. The probe, with some combinations of ATA/ATAPI
419 * devices keep it in a mostly working, but strange state (with busy
420 * led on)
421 */
422 if ((chp->wdc->cap & WDC_CAPABILITY_NO_EXTRA_RESETS) == 0) {
423 wdcreset(chp, VERBOSE);
424 /*
425 * Read status registers to avoid spurious interrupts.
426 */
427 for (i = 1; i >= 0; i--) {
428 if (chp->ch_drive[i].drive_flags & DRIVE) {
429 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh,
430 wd_sdh, WDSD_IBM | (i << 4));
431 if (wait_for_unbusy(chp, 10000) < 0)
432 printf("%s:%d:%d: device busy\n",
433 chp->wdc->sc_dev.dv_xname,
434 chp->channel, i);
435 }
436 }
437 }
438 wdc_delref(chp);
439 }
440
441 /*
442 * Start I/O on a controller, for the given channel.
443 * The first xfer may be not for our channel if the channel queues
444 * are shared.
445 */
446 void
447 wdcstart(chp)
448 struct channel_softc *chp;
449 {
450 struct wdc_xfer *xfer;
451
452 #ifdef WDC_DIAGNOSTIC
453 int spl1, spl2;
454
455 spl1 = splbio();
456 spl2 = splbio();
457 if (spl2 != spl1) {
458 printf("wdcstart: not at splbio()\n");
459 panic("wdcstart");
460 }
461 splx(spl2);
462 splx(spl1);
463 #endif /* WDC_DIAGNOSTIC */
464
465 /* is there a xfer ? */
466 if ((xfer = chp->ch_queue->sc_xfer.tqh_first) == NULL)
467 return;
468
469 /* adjust chp, in case we have a shared queue */
470 chp = xfer->chp;
471
472 if ((chp->ch_flags & WDCF_ACTIVE) != 0 ) {
473 return; /* channel aleady active */
474 }
475 #ifdef DIAGNOSTIC
476 if ((chp->ch_flags & WDCF_IRQ_WAIT) != 0)
477 panic("wdcstart: channel waiting for irq\n");
478 #endif
479 if (chp->wdc->cap & WDC_CAPABILITY_HWLOCK)
480 if (!(*chp->wdc->claim_hw)(chp, 0))
481 return;
482
483 WDCDEBUG_PRINT(("wdcstart: xfer %p channel %d drive %d\n", xfer,
484 chp->channel, xfer->drive), DEBUG_XFERS);
485 chp->ch_flags |= WDCF_ACTIVE;
486 if (chp->ch_drive[xfer->drive].drive_flags & DRIVE_RESET) {
487 chp->ch_drive[xfer->drive].drive_flags &= ~DRIVE_RESET;
488 chp->ch_drive[xfer->drive].state = 0;
489 }
490 xfer->c_start(chp, xfer);
491 }
492
493 /* restart an interrupted I/O */
494 void
495 wdcrestart(v)
496 void *v;
497 {
498 struct channel_softc *chp = v;
499 int s;
500
501 s = splbio();
502 wdcstart(chp);
503 splx(s);
504 }
505
506
507 /*
508 * Interrupt routine for the controller. Acknowledge the interrupt, check for
509 * errors on the current operation, mark it done if necessary, and start the
510 * next request. Also check for a partially done transfer, and continue with
511 * the next chunk if so.
512 */
513 int
514 wdcintr(arg)
515 void *arg;
516 {
517 struct channel_softc *chp = arg;
518 struct wdc_xfer *xfer;
519
520 if ((chp->ch_flags & WDCF_IRQ_WAIT) == 0) {
521 #if 0
522 /* Clear the pending interrupt and abort. */
523 u_int8_t s =
524 bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_status);
525 #ifdef WDCDEBUG
526 u_int8_t e =
527 bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_error);
528 u_int8_t i =
529 bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_seccnt);
530 #else
531 bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_error);
532 bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_seccnt);
533 #endif
534
535 WDCDEBUG_PRINT(("wdcintr: inactive controller, "
536 "punting st=%02x er=%02x irr=%02x\n", s, e, i), DEBUG_INTR);
537
538 if (s & WDCS_DRQ) {
539 int len;
540 len = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh,
541 wd_cyl_lo) + 256 * bus_space_read_1(chp->cmd_iot,
542 chp->cmd_ioh, wd_cyl_hi);
543 WDCDEBUG_PRINT(("wdcintr: clearing up %d bytes\n",
544 len), DEBUG_INTR);
545 wdcbit_bucket (chp, len);
546 }
547 #else
548 WDCDEBUG_PRINT(("wdcintr: inactive controller\n"), DEBUG_INTR);
549 #endif
550 return 0;
551 }
552
553 WDCDEBUG_PRINT(("wdcintr\n"), DEBUG_INTR);
554 untimeout(wdctimeout, chp);
555 chp->ch_flags &= ~WDCF_IRQ_WAIT;
556 xfer = chp->ch_queue->sc_xfer.tqh_first;
557 return xfer->c_intr(chp, xfer);
558 }
559
560 /* Put all disk in RESET state */
561 void wdc_reset_channel(drvp)
562 struct ata_drive_datas *drvp;
563 {
564 struct channel_softc *chp = drvp->chnl_softc;
565 int drive;
566 WDCDEBUG_PRINT(("ata_reset_channel %s:%d for drive %d\n",
567 chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive),
568 DEBUG_FUNCS);
569 (void) wdcreset(chp, VERBOSE);
570 for (drive = 0; drive < 2; drive++) {
571 chp->ch_drive[drive].state = 0;
572 }
573 }
574
575 int
576 wdcreset(chp, verb)
577 struct channel_softc *chp;
578 int verb;
579 {
580 int drv_mask1, drv_mask2;
581
582 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
583 WDSD_IBM); /* master */
584 bus_space_write_1(chp->ctl_iot, chp->ctl_ioh, wd_aux_ctlr,
585 WDCTL_RST | WDCTL_IDS);
586 delay(1000);
587 bus_space_write_1(chp->ctl_iot, chp->ctl_ioh, wd_aux_ctlr,
588 WDCTL_IDS);
589 delay(1000);
590 (void) bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_error);
591 bus_space_write_1(chp->ctl_iot, chp->ctl_ioh, wd_aux_ctlr,
592 WDCTL_4BIT);
593
594 drv_mask1 = (chp->ch_drive[0].drive_flags & DRIVE) ? 0x01:0x00;
595 drv_mask1 |= (chp->ch_drive[1].drive_flags & DRIVE) ? 0x02:0x00;
596 drv_mask2 = __wdcwait_reset(chp, drv_mask1);
597 if (verb && drv_mask2 != drv_mask1) {
598 printf("%s channel %d: reset failed for",
599 chp->wdc->sc_dev.dv_xname, chp->channel);
600 if ((drv_mask1 & 0x01) != 0 && (drv_mask2 & 0x01) == 0)
601 printf(" drive 0");
602 if ((drv_mask1 & 0x02) != 0 && (drv_mask2 & 0x02) == 0)
603 printf(" drive 1");
604 printf("\n");
605 }
606 return (drv_mask1 != drv_mask2) ? 1 : 0;
607 }
608
609 static int
610 __wdcwait_reset(chp, drv_mask)
611 struct channel_softc *chp;
612 int drv_mask;
613 {
614 int timeout;
615 u_int8_t st0, st1;
616 /* wait for BSY to deassert */
617 for (timeout = 0; timeout < WDCNDELAY_RST;timeout++) {
618 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
619 WDSD_IBM); /* master */
620 delay(1);
621 st0 = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_status);
622 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
623 WDSD_IBM | 0x10); /* slave */
624 delay(1);
625 st1 = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_status);
626
627 if ((drv_mask & 0x01) == 0) {
628 /* no master */
629 if ((drv_mask & 0x02) != 0 && (st1 & WDCS_BSY) == 0) {
630 /* No master, slave is ready, it's done */
631 return drv_mask;
632 }
633 } else if ((drv_mask & 0x02) == 0) {
634 /* no slave */
635 if ((drv_mask & 0x01) != 0 && (st0 & WDCS_BSY) == 0) {
636 /* No slave, master is ready, it's done */
637 return drv_mask;
638 }
639 } else {
640 /* Wait for both master and slave to be ready */
641 if ((st0 & WDCS_BSY) == 0 && (st1 & WDCS_BSY) == 0) {
642 return drv_mask;
643 }
644 }
645 delay(WDCDELAY);
646 }
647 /* Reset timed out. Maybe it's because drv_mask was not rigth */
648 if (st0 & WDCS_BSY)
649 drv_mask &= ~0x01;
650 if (st1 & WDCS_BSY)
651 drv_mask &= ~0x02;
652 return drv_mask;
653 }
654
655 /*
656 * Wait for a drive to be !BSY, and have mask in its status register.
657 * return -1 for a timeout after "timeout" ms.
658 */
659 int
660 wdcwait(chp, mask, bits, timeout)
661 struct channel_softc *chp;
662 int mask, bits, timeout;
663 {
664 u_char status;
665 int time = 0;
666 #ifdef WDCNDELAY_DEBUG
667 extern int cold;
668 #endif
669
670 WDCDEBUG_PRINT(("wdcwait %s:%d\n", chp->wdc ?chp->wdc->sc_dev.dv_xname
671 :"none", chp->channel), DEBUG_STATUS);
672 chp->ch_error = 0;
673
674 timeout = timeout * 1000 / WDCDELAY; /* delay uses microseconds */
675
676 for (;;) {
677 chp->ch_status = status =
678 bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_status);
679 if ((status & WDCS_BSY) == 0 && (status & mask) == bits)
680 break;
681 if (++time > timeout) {
682 WDCDEBUG_PRINT(("wdcwait: timeout, status %x "
683 "error %x\n", status,
684 bus_space_read_1(chp->cmd_iot, chp->cmd_ioh,
685 wd_error)),
686 DEBUG_STATUS);
687 return -1;
688 }
689 delay(WDCDELAY);
690 }
691 if (status & WDCS_ERR)
692 chp->ch_error = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh,
693 wd_error);
694 #ifdef WDCNDELAY_DEBUG
695 /* After autoconfig, there should be no long delays. */
696 if (!cold && time > WDCNDELAY_DEBUG) {
697 struct wdc_xfer *xfer = chp->ch_queue->sc_xfer.tqh_first;
698 if (xfer == NULL)
699 printf("%s channel %d: warning: busy-wait took %dus\n",
700 chp->wdc->sc_dev.dv_xname, chp->channel,
701 WDCDELAY * time);
702 else
703 printf("%s:%d:%d: warning: busy-wait took %dus\n",
704 chp->wdc->sc_dev.dv_xname, chp->channel,
705 xfer->drive,
706 WDCDELAY * time);
707 }
708 #endif
709 return 0;
710 }
711
712 void
713 wdctimeout(arg)
714 void *arg;
715 {
716 struct channel_softc *chp = (struct channel_softc *)arg;
717 struct wdc_xfer *xfer = chp->ch_queue->sc_xfer.tqh_first;
718 int s;
719
720 WDCDEBUG_PRINT(("wdctimeout\n"), DEBUG_FUNCS);
721
722 s = splbio();
723 if ((chp->ch_flags & WDCF_IRQ_WAIT) != 0) {
724 __wdcerror(chp, "lost interrupt");
725 printf("\ttype: %s\n", (xfer->c_flags & C_ATAPI) ?
726 "atapi":"ata");
727 printf("\tc_bcount: %d\n", xfer->c_bcount);
728 printf("\tc_skip: %d\n", xfer->c_skip);
729 /*
730 * Call the interrupt routine. If we just missed and interrupt,
731 * it will do what's needed. Else, it will take the needed
732 * action (reset the device).
733 */
734 xfer->c_flags |= C_TIMEOU;
735 chp->ch_flags &= ~WDCF_IRQ_WAIT;
736 xfer->c_intr(chp, xfer);
737 } else
738 __wdcerror(chp, "missing untimeout");
739 splx(s);
740 }
741
742 /*
743 * Probe drive's capabilites, for use by the controller later
744 * Assumes drvp points to an existing drive.
745 * XXX this should be a controller-indep function
746 */
747 void
748 wdc_probe_caps(drvp)
749 struct ata_drive_datas *drvp;
750 {
751 struct ataparams params, params2;
752 struct channel_softc *chp = drvp->chnl_softc;
753 struct device *drv_dev = drvp->drv_softc;
754 struct wdc_softc *wdc = chp->wdc;
755 int i, printed;
756 char *sep = "";
757 int cf_flags;
758
759 if (ata_get_params(drvp, AT_POLL, ¶ms) != CMD_OK) {
760 /* IDENTIFY failed. Can't tell more about the device */
761 return;
762 }
763 if ((wdc->cap & (WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32)) ==
764 (WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32)) {
765 /*
766 * Controller claims 16 and 32 bit transfers.
767 * Re-do an IDENTIFY with 32-bit transfers,
768 * and compare results.
769 */
770 drvp->drive_flags |= DRIVE_CAP32;
771 ata_get_params(drvp, AT_POLL, ¶ms2);
772 if (memcmp(¶ms, ¶ms2, sizeof(struct ataparams)) != 0) {
773 /* Not good. fall back to 16bits */
774 drvp->drive_flags &= ~DRIVE_CAP32;
775 } else {
776 printf("%s: 32-bits data port", drv_dev->dv_xname);
777 }
778 }
779 #if 0 /* Some ultra-DMA drives claims to only support ATA-3. sigh */
780 if (params.atap_ata_major > 0x01 &&
781 params.atap_ata_major != 0xffff) {
782 for (i = 14; i > 0; i--) {
783 if (params.atap_ata_major & (1 << i)) {
784 if ((drvp->drive_flags & DRIVE_CAP32) == 0)
785 printf("%s: ", drv_dev->dv_xname);
786 else
787 printf(", ");
788 printf("ATA version %d\n", i);
789 drvp->ata_vers = i;
790 break;
791 }
792 }
793 } else
794 #endif
795 if (drvp->drive_flags & DRIVE_CAP32)
796 printf("\n");
797
798 /* An ATAPI device is at last PIO mode 3 */
799 if (drvp->drive_flags & DRIVE_ATAPI)
800 drvp->PIO_mode = 3;
801
802 /*
803 * It's not in the specs, but it seems that some drive
804 * returns 0xffff in atap_extensions when this field is invalid
805 */
806 if (params.atap_extensions != 0xffff &&
807 (params.atap_extensions & WDC_EXT_MODES)) {
808 printed = 0;
809 /*
810 * XXX some drives report something wrong here (they claim to
811 * support PIO mode 8 !). As mode is coded on 3 bits in
812 * SET FEATURE, limit it to 7 (so limit i to 4).
813 * If higther mode than 7 is found, abort.
814 */
815 for (i = 7; i >= 0; i--) {
816 if ((params.atap_piomode_supp & (1 << i)) == 0)
817 continue;
818 if (i > 4)
819 return;
820 /*
821 * See if mode is accepted.
822 * If the controller can't set its PIO mode,
823 * assume the defaults are good, so don't try
824 * to set it
825 */
826 if ((wdc->cap & WDC_CAPABILITY_MODE) != 0)
827 if (ata_set_mode(drvp, 0x08 | (i + 3),
828 AT_POLL) != CMD_OK)
829 continue;
830 if (!printed) {
831 printf("%s: drive supports PIO mode %d",
832 drv_dev->dv_xname, i + 3);
833 sep = ",";
834 printed = 1;
835 }
836 /*
837 * If controller's driver can't set its PIO mode,
838 * get the highter one for the drive.
839 */
840 if ((wdc->cap & WDC_CAPABILITY_MODE) == 0 ||
841 wdc->PIO_cap >= i + 3) {
842 drvp->PIO_mode = i + 3;
843 drvp->PIO_cap = i + 3;
844 break;
845 }
846 }
847 if (!printed) {
848 /*
849 * We didn't find a valid PIO mode.
850 * Assume the values returned for DMA are buggy too
851 */
852 return;
853 }
854 drvp->drive_flags |= DRIVE_MODE;
855 printed = 0;
856 for (i = 7; i >= 0; i--) {
857 if ((params.atap_dmamode_supp & (1 << i)) == 0)
858 continue;
859 if ((wdc->cap & WDC_CAPABILITY_DMA) &&
860 (wdc->cap & WDC_CAPABILITY_MODE))
861 if (ata_set_mode(drvp, 0x20 | i, AT_POLL)
862 != CMD_OK)
863 continue;
864 if (!printed) {
865 printf("%s DMA mode %d", sep, i);
866 sep = ",";
867 printed = 1;
868 }
869 if (wdc->cap & WDC_CAPABILITY_DMA) {
870 if ((wdc->cap & WDC_CAPABILITY_MODE) &&
871 wdc->DMA_cap < i)
872 continue;
873 drvp->DMA_mode = i;
874 drvp->DMA_cap = i;
875 drvp->drive_flags |= DRIVE_DMA;
876 }
877 break;
878 }
879 if (params.atap_extensions & WDC_EXT_UDMA_MODES) {
880 for (i = 7; i >= 0; i--) {
881 if ((params.atap_udmamode_supp & (1 << i))
882 == 0)
883 continue;
884 if ((wdc->cap & WDC_CAPABILITY_MODE) &&
885 (wdc->cap & WDC_CAPABILITY_UDMA))
886 if (ata_set_mode(drvp, 0x40 | i,
887 AT_POLL) != CMD_OK)
888 continue;
889 printf("%s Ultra-DMA mode %d", sep, i);
890 sep = ",";
891 if (wdc->cap & WDC_CAPABILITY_UDMA) {
892 if ((wdc->cap & WDC_CAPABILITY_MODE) &&
893 wdc->UDMA_cap < i)
894 continue;
895 drvp->UDMA_mode = i;
896 drvp->UDMA_cap = i;
897 drvp->drive_flags |= DRIVE_UDMA;
898 }
899 break;
900 }
901 }
902 printf("\n");
903 }
904
905 /* Try to guess ATA version here, if it didn't get reported */
906 if (drvp->ata_vers == 0) {
907 if (drvp->drive_flags & DRIVE_UDMA)
908 drvp->ata_vers = 4; /* should be at last ATA-4 */
909 else if (drvp->PIO_cap > 2)
910 drvp->ata_vers = 2; /* should be at last ATA-2 */
911 }
912 cf_flags = drv_dev->dv_cfdata->cf_flags;
913 if (cf_flags & ATA_CONFIG_PIO_SET) {
914 drvp->PIO_mode =
915 (cf_flags & ATA_CONFIG_PIO_MODES) >> ATA_CONFIG_PIO_OFF;
916 drvp->drive_flags |= DRIVE_MODE;
917 }
918 if ((wdc->cap & WDC_CAPABILITY_DMA) == 0) {
919 /* don't care about DMA modes */
920 return;
921 }
922 if (cf_flags & ATA_CONFIG_DMA_SET) {
923 if ((cf_flags & ATA_CONFIG_DMA_MODES) ==
924 ATA_CONFIG_DMA_DISABLE) {
925 drvp->drive_flags &= ~DRIVE_DMA;
926 } else {
927 drvp->DMA_mode = (cf_flags & ATA_CONFIG_DMA_MODES) >>
928 ATA_CONFIG_DMA_OFF;
929 drvp->drive_flags |= DRIVE_DMA | DRIVE_MODE;
930 }
931 }
932 if (cf_flags & ATA_CONFIG_UDMA_SET) {
933 if ((cf_flags & ATA_CONFIG_UDMA_MODES) ==
934 ATA_CONFIG_UDMA_DISABLE) {
935 drvp->drive_flags &= ~DRIVE_UDMA;
936 } else {
937 drvp->UDMA_mode = (cf_flags & ATA_CONFIG_UDMA_MODES) >>
938 ATA_CONFIG_UDMA_OFF;
939 drvp->drive_flags |= DRIVE_UDMA | DRIVE_MODE;
940 }
941 }
942 }
943
944 /*
945 * downgrade the transfer mode of a drive after an error. return 1 if
946 * downgrade was possible, 0 otherwise.
947 */
948 int
949 wdc_downgrade_mode(drvp)
950 struct ata_drive_datas *drvp;
951 {
952 struct channel_softc *chp = drvp->chnl_softc;
953 struct device *drv_dev = drvp->drv_softc;
954 struct wdc_softc *wdc = chp->wdc;
955 int cf_flags = drv_dev->dv_cfdata->cf_flags;
956
957 /* if drive or controller don't know its mode, we can't do much */
958 if ((drvp->drive_flags & DRIVE_MODE) == 0 ||
959 (wdc->cap & WDC_CAPABILITY_MODE) == 0)
960 return 0;
961 /* current drive mode was set by a config flag, let it this way */
962 if ((cf_flags & ATA_CONFIG_PIO_SET) ||
963 (cf_flags & ATA_CONFIG_DMA_SET) ||
964 (cf_flags & ATA_CONFIG_UDMA_SET))
965 return 0;
966
967 /*
968 * If we were using ultra-DMA, don't downgrade to multiword DMA
969 * if we noticed a CRC error. It has been noticed that CRC errors
970 * in ultra-DMA lead to silent data corruption in multiword DMA.
971 * Data corruption is less likely to occur in PIO mode.
972 */
973
974 if ((drvp->drive_flags & DRIVE_UDMA) &&
975 (drvp->drive_flags & DRIVE_DMAERR) == 0) {
976 drvp->drive_flags &= ~DRIVE_UDMA;
977 drvp->drive_flags |= DRIVE_DMA;
978 drvp->DMA_mode = drvp->DMA_cap;
979 printf("%s: transfer error, downgrading to DMA mode %d\n",
980 drv_dev->dv_xname, drvp->DMA_mode);
981 } else if (drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA)) {
982 drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
983 drvp->PIO_mode = drvp->PIO_cap;
984 printf("%s: transfer error, downgrading to PIO mode %d\n",
985 drv_dev->dv_xname, drvp->PIO_mode);
986 } else /* already using PIO, can't downgrade */
987 return 0;
988
989 wdc->set_modes(chp);
990 /* reset the channel, which will shedule all drives for setup */
991 wdc_reset_channel(drvp);
992 return 1;
993 }
994
995 int
996 wdc_exec_command(drvp, wdc_c)
997 struct ata_drive_datas *drvp;
998 struct wdc_command *wdc_c;
999 {
1000 struct channel_softc *chp = drvp->chnl_softc;
1001 struct wdc_xfer *xfer;
1002 int s, ret;
1003
1004 WDCDEBUG_PRINT(("wdc_exec_command %s:%d:%d\n",
1005 chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive),
1006 DEBUG_FUNCS);
1007
1008 /* set up an xfer and queue. Wait for completion */
1009 xfer = wdc_get_xfer(wdc_c->flags & AT_WAIT ? WDC_CANSLEEP :
1010 WDC_NOSLEEP);
1011 if (xfer == NULL) {
1012 return WDC_TRY_AGAIN;
1013 }
1014
1015 if (wdc_c->flags & AT_POLL)
1016 xfer->c_flags |= C_POLL;
1017 xfer->drive = drvp->drive;
1018 xfer->databuf = wdc_c->data;
1019 xfer->c_bcount = wdc_c->bcount;
1020 xfer->cmd = wdc_c;
1021 xfer->c_start = __wdccommand_start;
1022 xfer->c_intr = __wdccommand_intr;
1023
1024 s = splbio();
1025 wdc_exec_xfer(chp, xfer);
1026 #ifdef DIAGNOSTIC
1027 if ((wdc_c->flags & AT_POLL) != 0 &&
1028 (wdc_c->flags & AT_DONE) == 0)
1029 panic("wdc_exec_command: polled command not done\n");
1030 #endif
1031 if (wdc_c->flags & AT_DONE) {
1032 ret = WDC_COMPLETE;
1033 } else {
1034 if (wdc_c->flags & AT_WAIT) {
1035 tsleep(wdc_c, PRIBIO, "wdccmd", 0);
1036 ret = WDC_COMPLETE;
1037 } else {
1038 ret = WDC_QUEUED;
1039 }
1040 }
1041 splx(s);
1042 return ret;
1043 }
1044
1045 void
1046 __wdccommand_start(chp, xfer)
1047 struct channel_softc *chp;
1048 struct wdc_xfer *xfer;
1049 {
1050 int drive = xfer->drive;
1051 struct wdc_command *wdc_c = xfer->cmd;
1052
1053 WDCDEBUG_PRINT(("__wdccommand_start %s:%d:%d\n",
1054 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive),
1055 DEBUG_FUNCS);
1056
1057 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
1058 WDSD_IBM | (drive << 4));
1059 if (wdcwait(chp, wdc_c->r_st_bmask, wdc_c->r_st_bmask,
1060 wdc_c->timeout) != 0) {
1061 wdc_c->flags |= AT_TIMEOU;
1062 __wdccommand_done(chp, xfer);
1063 return;
1064 }
1065 wdccommand(chp, drive, wdc_c->r_command, wdc_c->r_cyl, wdc_c->r_head,
1066 wdc_c->r_sector, wdc_c->r_count, wdc_c->r_precomp);
1067 if ((wdc_c->flags & AT_POLL) == 0) {
1068 chp->ch_flags |= WDCF_IRQ_WAIT; /* wait for interrupt */
1069 timeout(wdctimeout, chp, wdc_c->timeout / 1000 * hz);
1070 return;
1071 }
1072 /*
1073 * Polled command. Wait for drive ready or drq. Done in intr().
1074 * Wait for at last 400ns for status bit to be valid.
1075 */
1076 delay(10);
1077 __wdccommand_intr(chp, xfer);
1078 }
1079
1080 int
1081 __wdccommand_intr(chp, xfer)
1082 struct channel_softc *chp;
1083 struct wdc_xfer *xfer;
1084 {
1085 struct wdc_command *wdc_c = xfer->cmd;
1086 int bcount = wdc_c->bcount;
1087 char *data = wdc_c->data;
1088
1089 WDCDEBUG_PRINT(("__wdccommand_intr %s:%d:%d\n",
1090 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive), DEBUG_INTR);
1091 if (wdcwait(chp, wdc_c->r_st_pmask, wdc_c->r_st_pmask,
1092 (wdc_c->flags & AT_POLL) ? wdc_c->timeout : 0)) {
1093 if ((xfer->c_flags & C_TIMEOU) == 0 &&
1094 (wdc_c->flags & AT_POLL) == 0)
1095 return 0; /* IRQ was not for us */
1096 wdc_c->flags |= AT_TIMEOU;
1097 __wdccommand_done(chp, xfer);
1098 return 1;
1099 }
1100 if (wdc_c->flags & AT_READ) {
1101 if (chp->ch_drive[xfer->drive].drive_flags & DRIVE_CAP32) {
1102 bus_space_read_multi_4(chp->data32iot, chp->data32ioh,
1103 0, (u_int32_t*)data, bcount >> 2);
1104 data += bcount & 0xfffffffc;
1105 bcount = bcount & 0x03;
1106 }
1107 if (bcount > 0)
1108 bus_space_read_multi_2(chp->cmd_iot, chp->cmd_ioh,
1109 wd_data, (u_int16_t *)data, bcount >> 1);
1110 } else if (wdc_c->flags & AT_WRITE) {
1111 if (chp->ch_drive[xfer->drive].drive_flags & DRIVE_CAP32) {
1112 bus_space_write_multi_4(chp->data32iot, chp->data32ioh,
1113 0, (u_int32_t*)data, bcount >> 2);
1114 data += bcount & 0xfffffffc;
1115 bcount = bcount & 0x03;
1116 }
1117 if (bcount > 0)
1118 bus_space_write_multi_2(chp->cmd_iot, chp->cmd_ioh,
1119 wd_data, (u_int16_t *)data, bcount >> 1);
1120 }
1121 __wdccommand_done(chp, xfer);
1122 return 1;
1123 }
1124
1125 void
1126 __wdccommand_done(chp, xfer)
1127 struct channel_softc *chp;
1128 struct wdc_xfer *xfer;
1129 {
1130 int needdone = xfer->c_flags & C_NEEDDONE;
1131 struct wdc_command *wdc_c = xfer->cmd;
1132
1133 WDCDEBUG_PRINT(("__wdccommand_done %s:%d:%d\n",
1134 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive), DEBUG_FUNCS);
1135 if (chp->ch_status & WDCS_DWF)
1136 wdc_c->flags |= AT_DF;
1137 if (chp->ch_status & WDCS_ERR) {
1138 wdc_c->flags |= AT_ERROR;
1139 wdc_c->r_error = chp->ch_error;
1140 }
1141 wdc_c->flags |= AT_DONE;
1142 if (wdc_c->flags & AT_READREG && (wdc_c->flags & (AT_ERROR | AT_DF))
1143 == 0) {
1144 wdc_c->r_head = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh,
1145 wd_sdh);
1146 wdc_c->r_cyl = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh,
1147 wd_cyl_hi) << 8;
1148 wdc_c->r_cyl |= bus_space_read_1(chp->cmd_iot, chp->cmd_ioh,
1149 wd_cyl_lo);
1150 wdc_c->r_sector = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh,
1151 wd_sector);
1152 wdc_c->r_count = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh,
1153 wd_seccnt);
1154 wdc_c->r_error = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh,
1155 wd_error);
1156 wdc_c->r_precomp = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh,
1157 wd_precomp);
1158 }
1159 wdc_free_xfer(chp, xfer);
1160 if (needdone) {
1161 if (wdc_c->flags & AT_WAIT)
1162 wakeup(wdc_c);
1163 else
1164 wdc_c->callback(wdc_c->callback_arg);
1165 }
1166 wdcstart(chp);
1167 return;
1168 }
1169
1170 /*
1171 * Send a command. The drive should be ready.
1172 * Assumes interrupts are blocked.
1173 */
1174 void
1175 wdccommand(chp, drive, command, cylin, head, sector, count, precomp)
1176 struct channel_softc *chp;
1177 u_int8_t drive;
1178 u_int8_t command;
1179 u_int16_t cylin;
1180 u_int8_t head, sector, count, precomp;
1181 {
1182 WDCDEBUG_PRINT(("wdccommand %s:%d:%d: command=0x%x cylin=%d head=%d "
1183 "sector=%d count=%d precomp=%d\n", chp->wdc->sc_dev.dv_xname,
1184 chp->channel, drive, command, cylin, head, sector, count, precomp),
1185 DEBUG_FUNCS);
1186
1187 /* Select drive, head, and addressing mode. */
1188 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
1189 WDSD_IBM | (drive << 4) | head);
1190 /* Load parameters. wd_features(ATA/ATAPI) = wd_precomp(ST506) */
1191 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_precomp,
1192 precomp);
1193 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_cyl_lo, cylin);
1194 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_cyl_hi, cylin >> 8);
1195 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sector, sector);
1196 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_seccnt, count);
1197
1198 /* Send command. */
1199 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_command, command);
1200 return;
1201 }
1202
1203 /*
1204 * Simplified version of wdccommand(). Unbusy/ready/drq must be
1205 * tested by the caller.
1206 */
1207 void
1208 wdccommandshort(chp, drive, command)
1209 struct channel_softc *chp;
1210 int drive;
1211 int command;
1212 {
1213
1214 WDCDEBUG_PRINT(("wdccommandshort %s:%d:%d command 0x%x\n",
1215 chp->wdc->sc_dev.dv_xname, chp->channel, drive, command),
1216 DEBUG_FUNCS);
1217
1218 /* Select drive. */
1219 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
1220 WDSD_IBM | (drive << 4));
1221
1222 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_command, command);
1223 }
1224
1225 /* Add a command to the queue and start controller. Must be called at splbio */
1226
1227 void
1228 wdc_exec_xfer(chp, xfer)
1229 struct channel_softc *chp;
1230 struct wdc_xfer *xfer;
1231 {
1232 WDCDEBUG_PRINT(("wdc_exec_xfer %p channel %d drive %d\n", xfer,
1233 chp->channel, xfer->drive), DEBUG_XFERS);
1234
1235 /* complete xfer setup */
1236 xfer->chp = chp;
1237
1238 /*
1239 * If we are a polled command, and the list is not empty,
1240 * we are doing a dump. Drop the list to allow the polled command
1241 * to complete, we're going to reboot soon anyway.
1242 */
1243 if ((xfer->c_flags & C_POLL) != 0 &&
1244 chp->ch_queue->sc_xfer.tqh_first != NULL) {
1245 TAILQ_INIT(&chp->ch_queue->sc_xfer);
1246 }
1247 /* insert at the end of command list */
1248 TAILQ_INSERT_TAIL(&chp->ch_queue->sc_xfer,xfer , c_xferchain);
1249 WDCDEBUG_PRINT(("wdcstart from wdc_exec_xfer, flags 0x%x\n",
1250 chp->ch_flags), DEBUG_XFERS);
1251 wdcstart(chp);
1252 xfer->c_flags |= C_NEEDDONE; /* we can now call upper level done() */
1253 }
1254
1255 struct wdc_xfer *
1256 wdc_get_xfer(flags)
1257 int flags;
1258 {
1259 struct wdc_xfer *xfer;
1260 int s;
1261
1262 s = splbio();
1263 if ((xfer = xfer_free_list.lh_first) != NULL) {
1264 LIST_REMOVE(xfer, free_list);
1265 splx(s);
1266 #ifdef DIAGNOSTIC
1267 if ((xfer->c_flags & C_INUSE) != 0)
1268 panic("wdc_get_xfer: xfer already in use\n");
1269 #endif
1270 } else {
1271 splx(s);
1272 WDCDEBUG_PRINT(("wdc:making xfer %d\n",wdc_nxfer), DEBUG_XFERS);
1273 xfer = malloc(sizeof(*xfer), M_DEVBUF,
1274 ((flags & WDC_NOSLEEP) != 0 ? M_NOWAIT : M_WAITOK));
1275 if (xfer == NULL)
1276 return 0;
1277 #ifdef DIAGNOSTIC
1278 xfer->c_flags &= ~C_INUSE;
1279 #endif
1280 #ifdef WDCDEBUG
1281 wdc_nxfer++;
1282 #endif
1283 }
1284 #ifdef DIAGNOSTIC
1285 if ((xfer->c_flags & C_INUSE) != 0)
1286 panic("wdc_get_xfer: xfer already in use\n");
1287 #endif
1288 memset(xfer, 0, sizeof(struct wdc_xfer));
1289 xfer->c_flags = C_INUSE;
1290 return xfer;
1291 }
1292
1293 void
1294 wdc_free_xfer(chp, xfer)
1295 struct channel_softc *chp;
1296 struct wdc_xfer *xfer;
1297 {
1298 struct wdc_softc *wdc = chp->wdc;
1299 int s;
1300
1301 if (wdc->cap & WDC_CAPABILITY_HWLOCK)
1302 (*wdc->free_hw)(chp);
1303 s = splbio();
1304 chp->ch_flags &= ~WDCF_ACTIVE;
1305 TAILQ_REMOVE(&chp->ch_queue->sc_xfer, xfer, c_xferchain);
1306 xfer->c_flags &= ~C_INUSE;
1307 LIST_INSERT_HEAD(&xfer_free_list, xfer, free_list);
1308 splx(s);
1309 }
1310
1311 static void
1312 __wdcerror(chp, msg)
1313 struct channel_softc *chp;
1314 char *msg;
1315 {
1316 struct wdc_xfer *xfer = chp->ch_queue->sc_xfer.tqh_first;
1317 if (xfer == NULL)
1318 printf("%s:%d: %s\n", chp->wdc->sc_dev.dv_xname, chp->channel,
1319 msg);
1320 else
1321 printf("%s:%d:%d: %s\n", chp->wdc->sc_dev.dv_xname,
1322 chp->channel, xfer->drive, msg);
1323 }
1324
1325 /*
1326 * the bit bucket
1327 */
1328 void
1329 wdcbit_bucket(chp, size)
1330 struct channel_softc *chp;
1331 int size;
1332 {
1333
1334 for (; size >= 2; size -= 2)
1335 (void)bus_space_read_2(chp->cmd_iot, chp->cmd_ioh, wd_data);
1336 if (size)
1337 (void)bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_data);
1338 }
1339
1340 int
1341 wdc_addref(chp)
1342 struct channel_softc *chp;
1343 {
1344 struct wdc_softc *wdc = chp->wdc;
1345 struct scsipi_adapter *adapter = &wdc->sc_atapi_adapter;
1346 int s, error = 0;
1347
1348 s = splbio();
1349 if (adapter->scsipi_refcnt++ == 0 &&
1350 adapter->scsipi_enable != NULL) {
1351 error = (*adapter->scsipi_enable)(wdc, 1);
1352 if (error)
1353 adapter->scsipi_refcnt--;
1354 }
1355 splx(s);
1356 return (error);
1357 }
1358
1359 void
1360 wdc_delref(chp)
1361 struct channel_softc *chp;
1362 {
1363 struct wdc_softc *wdc = chp->wdc;
1364 struct scsipi_adapter *adapter = &wdc->sc_atapi_adapter;
1365 int s;
1366
1367 s = splbio();
1368 if (adapter->scsipi_refcnt-- == 1 &&
1369 adapter->scsipi_enable != NULL)
1370 (void) (*adapter->scsipi_enable)(wdc, 0);
1371 splx(s);
1372 }
1373