wdc.c revision 1.181 1 /* $NetBSD: wdc.c,v 1.181 2004/06/23 21:10:52 bouyer Exp $ */
2
3 /*
4 * Copyright (c) 1998, 2001, 2003 Manuel Bouyer. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Manuel Bouyer.
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*-
33 * Copyright (c) 1998, 2003 The NetBSD Foundation, Inc.
34 * All rights reserved.
35 *
36 * This code is derived from software contributed to The NetBSD Foundation
37 * by Charles M. Hannum, by Onno van der Linden and by Manuel Bouyer.
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. All advertising materials mentioning features or use of this software
48 * must display the following acknowledgement:
49 * This product includes software developed by the NetBSD
50 * Foundation, Inc. and its contributors.
51 * 4. Neither the name of The NetBSD Foundation nor the names of its
52 * contributors may be used to endorse or promote products derived
53 * from this software without specific prior written permission.
54 *
55 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
56 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
57 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
58 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
59 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
60 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
61 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
62 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
63 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
64 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
65 * POSSIBILITY OF SUCH DAMAGE.
66 */
67
68 /*
69 * CODE UNTESTED IN THE CURRENT REVISION:
70 */
71
72 #include <sys/cdefs.h>
73 __KERNEL_RCSID(0, "$NetBSD: wdc.c,v 1.181 2004/06/23 21:10:52 bouyer Exp $");
74
75 #ifndef WDCDEBUG
76 #define WDCDEBUG
77 #endif /* WDCDEBUG */
78
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/kernel.h>
82 #include <sys/conf.h>
83 #include <sys/buf.h>
84 #include <sys/device.h>
85 #include <sys/malloc.h>
86 #include <sys/pool.h>
87 #include <sys/syslog.h>
88 #include <sys/proc.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 "locators.h"
106
107 #include "ataraid.h"
108 #include "atapibus.h"
109 #include "wd.h"
110
111 #if NATARAID > 0
112 #include <dev/ata/ata_raidvar.h>
113 #endif
114
115 #define WDCDELAY 100 /* 100 microseconds */
116 #define WDCNDELAY_RST (WDC_RESET_WAIT * 1000 / WDCDELAY)
117 #if 0
118 /* If you enable this, it will report any delays more than WDCDELAY * N long. */
119 #define WDCNDELAY_DEBUG 50
120 #endif
121
122 /* When polling wait that much and then tsleep for 1/hz seconds */
123 #define WDCDELAY_POLL 1 /* ms */
124
125 /* timeout for the control commands */
126 #define WDC_CTRL_DELAY 10000 /* 10s, for the recall command */
127
128 struct pool wdc_xfer_pool;
129
130 #if NWD > 0
131 extern const struct ata_bustype wdc_ata_bustype; /* in ata_wdc.c */
132 #else
133 /* A fake one, the autoconfig will print "wd at foo ... not configured */
134 const struct ata_bustype wdc_ata_bustype = {
135 SCSIPI_BUSTYPE_ATA,
136 NULL,
137 NULL,
138 NULL,
139 NULL,
140 NULL,
141 NULL,
142 NULL
143 };
144 #endif
145
146 static int wdcprobe1(struct wdc_channel*, int);
147 static void __wdcerror(struct wdc_channel*, char *);
148 static int __wdcwait_reset(struct wdc_channel *, int, int);
149 static void __wdccommand_done(struct wdc_channel *, struct ata_xfer *);
150 static void __wdccommand_start(struct wdc_channel *, struct ata_xfer *);
151 static int __wdccommand_intr(struct wdc_channel *, struct ata_xfer *,
152 int);
153 static int __wdcwait(struct wdc_channel *, int, int, int);
154
155 #define DEBUG_INTR 0x01
156 #define DEBUG_XFERS 0x02
157 #define DEBUG_STATUS 0x04
158 #define DEBUG_FUNCS 0x08
159 #define DEBUG_PROBE 0x10
160 #define DEBUG_DETACH 0x20
161 #define DEBUG_DELAY 0x40
162 #ifdef WDCDEBUG
163 int wdcdebug_mask = 0;
164 int wdc_nxfer = 0;
165 #define WDCDEBUG_PRINT(args, level) if (wdcdebug_mask & (level)) printf args
166 #else
167 #define WDCDEBUG_PRINT(args, level)
168 #endif
169
170 /*
171 * A queue of atabus instances, used to ensure the same bus probe order
172 * for a given hardware configuration at each boot.
173 */
174 struct atabus_initq_head atabus_initq_head =
175 TAILQ_HEAD_INITIALIZER(atabus_initq_head);
176 struct simplelock atabus_interlock = SIMPLELOCK_INITIALIZER;
177
178 /*
179 * Initialize the "shadow register" handles for a standard wdc controller.
180 */
181 void
182 wdc_init_shadow_regs(struct wdc_channel *chp)
183 {
184
185 chp->cmd_iohs[wd_status] = chp->cmd_iohs[wd_command];
186 chp->cmd_iohs[wd_features] = chp->cmd_iohs[wd_error];
187 }
188
189 /* Test to see controller with at last one attached drive is there.
190 * Returns a bit for each possible drive found (0x01 for drive 0,
191 * 0x02 for drive 1).
192 * Logic:
193 * - If a status register is at 0xff, assume there is no drive here
194 * (ISA has pull-up resistors). Similarly if the status register has
195 * the value we last wrote to the bus (for IDE interfaces without pullups).
196 * If no drive at all -> return.
197 * - reset the controller, wait for it to complete (may take up to 31s !).
198 * If timeout -> return.
199 * - test ATA/ATAPI signatures. If at last one drive found -> return.
200 * - try an ATA command on the master.
201 */
202
203 static void
204 wdc_drvprobe(struct wdc_channel *chp)
205 {
206 struct ataparams params;
207 struct wdc_softc *wdc = chp->ch_wdc;
208 u_int8_t st0 = 0, st1 = 0;
209 int i, error;
210
211 if (wdcprobe1(chp, 0) == 0) {
212 /* No drives, abort the attach here. */
213 return;
214 }
215
216 /* for ATA/OLD drives, wait for DRDY, 3s timeout */
217 for (i = 0; i < mstohz(3000); i++) {
218 if (chp->ch_drive[0].drive_flags & (DRIVE_ATA|DRIVE_OLD)) {
219 if (wdc != NULL && (wdc->cap & WDC_CAPABILITY_SELECT))
220 wdc->select(chp,0);
221 bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_sdh],
222 0, WDSD_IBM);
223 delay(10); /* 400ns delay */
224 st0 = bus_space_read_1(chp->cmd_iot,
225 chp->cmd_iohs[wd_status], 0);
226 }
227
228 if (chp->ch_drive[1].drive_flags & (DRIVE_ATA|DRIVE_OLD)) {
229 if (wdc != NULL && (wdc->cap & WDC_CAPABILITY_SELECT))
230 wdc->select(chp,1);
231 bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_sdh],
232 0, WDSD_IBM | 0x10);
233 delay(10); /* 400ns delay */
234 st1 = bus_space_read_1(chp->cmd_iot,
235 chp->cmd_iohs[wd_status], 0);
236 }
237
238 if (((chp->ch_drive[0].drive_flags & (DRIVE_ATA|DRIVE_OLD))
239 == 0 ||
240 (st0 & WDCS_DRDY)) &&
241 ((chp->ch_drive[1].drive_flags & (DRIVE_ATA|DRIVE_OLD))
242 == 0 ||
243 (st1 & WDCS_DRDY)))
244 break;
245 tsleep(¶ms, PRIBIO, "atadrdy", 1);
246 }
247 if ((st0 & WDCS_DRDY) == 0)
248 chp->ch_drive[0].drive_flags &= ~(DRIVE_ATA|DRIVE_OLD);
249 if ((st1 & WDCS_DRDY) == 0)
250 chp->ch_drive[1].drive_flags &= ~(DRIVE_ATA|DRIVE_OLD);
251
252 WDCDEBUG_PRINT(("%s:%d: wait DRDY st0 0x%x st1 0x%x\n",
253 wdc->sc_dev.dv_xname,
254 chp->ch_channel, st0, st1), DEBUG_PROBE);
255
256 /* Wait a bit, some devices are weird just after a reset. */
257 delay(5000);
258
259 for (i = 0; i < 2; i++) {
260 /* XXX This should be done by other code. */
261 chp->ch_drive[i].chnl_softc = chp;
262 chp->ch_drive[i].drive = i;
263
264 /*
265 * Init error counter so that an error withing the first xfers
266 * will trigger a downgrade
267 */
268 chp->ch_drive[i].n_dmaerrs = NERRS_MAX-1;
269
270 /* If controller can't do 16bit flag the drives as 32bit */
271 if ((wdc->cap &
272 (WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32)) ==
273 WDC_CAPABILITY_DATA32)
274 chp->ch_drive[i].drive_flags |= DRIVE_CAP32;
275 if ((chp->ch_drive[i].drive_flags & DRIVE) == 0)
276 continue;
277
278 /* Shortcut in case we've been shutdown */
279 if (chp->ch_flags & WDCF_SHUTDOWN)
280 return;
281
282 /* issue an identify, to try to detect ghosts */
283 error = ata_get_params(&chp->ch_drive[i],
284 AT_WAIT | AT_POLL, ¶ms);
285 if (error != CMD_OK) {
286 tsleep(¶ms, PRIBIO, "atacnf", mstohz(1000));
287
288 /* Shortcut in case we've been shutdown */
289 if (chp->ch_flags & WDCF_SHUTDOWN)
290 return;
291
292 error = ata_get_params(&chp->ch_drive[i],
293 AT_WAIT | AT_POLL, ¶ms);
294 }
295 if (error == CMD_OK) {
296 /* If IDENTIFY succeeded, this is not an OLD ctrl */
297 chp->ch_drive[0].drive_flags &= ~DRIVE_OLD;
298 chp->ch_drive[1].drive_flags &= ~DRIVE_OLD;
299 } else {
300 chp->ch_drive[i].drive_flags &=
301 ~(DRIVE_ATA | DRIVE_ATAPI);
302 WDCDEBUG_PRINT(("%s:%d:%d: IDENTIFY failed (%d)\n",
303 wdc->sc_dev.dv_xname,
304 chp->ch_channel, i, error), DEBUG_PROBE);
305 if ((chp->ch_drive[i].drive_flags & DRIVE_OLD) == 0)
306 continue;
307 /*
308 * Pre-ATA drive ?
309 * Test registers writability (Error register not
310 * writable, but cyllo is), then try an ATA command.
311 */
312 if (wdc->cap & WDC_CAPABILITY_SELECT)
313 wdc->select(chp,i);
314 bus_space_write_1(chp->cmd_iot,
315 chp->cmd_iohs[wd_sdh], 0, WDSD_IBM | (i << 4));
316 delay(10); /* 400ns delay */
317 bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_error],
318 0, 0x58);
319 bus_space_write_1(chp->cmd_iot,
320 chp->cmd_iohs[wd_cyl_lo], 0, 0xa5);
321 if (bus_space_read_1(chp->cmd_iot,
322 chp->cmd_iohs[wd_error], 0) == 0x58 ||
323 bus_space_read_1(chp->cmd_iot,
324 chp->cmd_iohs[wd_cyl_lo], 0) != 0xa5) {
325 WDCDEBUG_PRINT(("%s:%d:%d: register "
326 "writability failed\n",
327 wdc->sc_dev.dv_xname,
328 chp->ch_channel, i), DEBUG_PROBE);
329 chp->ch_drive[i].drive_flags &= ~DRIVE_OLD;
330 continue;
331 }
332 if (wdc_wait_for_ready(chp, 10000, 0) == WDCWAIT_TOUT) {
333 WDCDEBUG_PRINT(("%s:%d:%d: not ready\n",
334 wdc->sc_dev.dv_xname,
335 chp->ch_channel, i), DEBUG_PROBE);
336 chp->ch_drive[i].drive_flags &= ~DRIVE_OLD;
337 continue;
338 }
339 bus_space_write_1(chp->cmd_iot,
340 chp->cmd_iohs[wd_command], 0, WDCC_RECAL);
341 delay(10); /* 400ns delay */
342 if (wdc_wait_for_ready(chp, 10000, 0) == WDCWAIT_TOUT) {
343 WDCDEBUG_PRINT(("%s:%d:%d: WDCC_RECAL failed\n",
344 wdc->sc_dev.dv_xname,
345 chp->ch_channel, i), DEBUG_PROBE);
346 chp->ch_drive[i].drive_flags &= ~DRIVE_OLD;
347 } else {
348 chp->ch_drive[0].drive_flags &=
349 ~(DRIVE_ATA | DRIVE_ATAPI);
350 chp->ch_drive[1].drive_flags &=
351 ~(DRIVE_ATA | DRIVE_ATAPI);
352 }
353 }
354 }
355 }
356
357 void
358 atabusconfig(struct atabus_softc *atabus_sc)
359 {
360 struct wdc_channel *chp = atabus_sc->sc_chan;
361 struct wdc_softc *wdc = chp->ch_wdc;
362 int i, error, need_delref = 0;
363 struct atabus_initq *atabus_initq = NULL;
364
365 if ((error = wdc_addref(chp)) != 0) {
366 aprint_error("%s: unable to enable controller\n",
367 wdc->sc_dev.dv_xname);
368 goto out;
369 }
370 need_delref = 1;
371
372 /* Probe for the drives. */
373 (*wdc->drv_probe)(chp);
374
375 WDCDEBUG_PRINT(("atabusattach: ch_drive_flags 0x%x 0x%x\n",
376 chp->ch_drive[0].drive_flags, chp->ch_drive[1].drive_flags),
377 DEBUG_PROBE);
378
379 /* If no drives, abort here */
380 if ((chp->ch_drive[0].drive_flags & DRIVE) == 0 &&
381 (chp->ch_drive[1].drive_flags & DRIVE) == 0)
382 goto out;
383
384 /* Shortcut in case we've been shutdown */
385 if (chp->ch_flags & WDCF_SHUTDOWN)
386 goto out;
387
388 /* Make sure the devices probe in atabus order to avoid jitter. */
389 simple_lock(&atabus_interlock);
390 while(1) {
391 atabus_initq = TAILQ_FIRST(&atabus_initq_head);
392 if (atabus_initq->atabus_sc == atabus_sc)
393 break;
394 ltsleep(&atabus_initq_head, PRIBIO, "ata_initq", 0,
395 &atabus_interlock);
396 }
397 simple_unlock(&atabus_interlock);
398
399 /*
400 * Attach an ATAPI bus, if needed.
401 */
402 if ((chp->ch_drive[0].drive_flags & DRIVE_ATAPI) ||
403 (chp->ch_drive[1].drive_flags & DRIVE_ATAPI)) {
404 #if NATAPIBUS > 0
405 wdc_atapibus_attach(atabus_sc);
406 #else
407 /*
408 * Fake the autoconfig "not configured" message
409 */
410 aprint_normal("atapibus at %s not configured\n",
411 wdc->sc_dev.dv_xname);
412 chp->atapibus = NULL;
413 chp->ch_drive[0].drive_flags &= ~DRIVE_ATAPI;
414 chp->ch_drive[1].drive_flags &= ~DRIVE_ATAPI;
415 #endif
416 }
417
418 for (i = 0; i < 2; i++) {
419 struct ata_device adev;
420 if ((chp->ch_drive[i].drive_flags &
421 (DRIVE_ATA | DRIVE_OLD)) == 0) {
422 continue;
423 }
424 memset(&adev, 0, sizeof(struct ata_device));
425 adev.adev_bustype = &wdc_ata_bustype;
426 adev.adev_channel = chp->ch_channel;
427 adev.adev_openings = 1;
428 adev.adev_drv_data = &chp->ch_drive[i];
429 chp->ata_drives[i] = config_found(&atabus_sc->sc_dev,
430 &adev, ataprint);
431 if (chp->ata_drives[i] != NULL)
432 wdc_probe_caps(&chp->ch_drive[i]);
433 else
434 chp->ch_drive[i].drive_flags &=
435 ~(DRIVE_ATA | DRIVE_OLD);
436 }
437
438 /* now that we know the drives, the controller can set its modes */
439 if (wdc->cap & WDC_CAPABILITY_MODE) {
440 wdc->set_modes(chp);
441 wdc_print_modes(chp);
442 }
443 #if NATARAID > 0
444 if (wdc->cap & WDC_CAPABILITY_RAID)
445 for (i = 0; i < 2; i++)
446 if (chp->ata_drives[i] != NULL)
447 ata_raid_check_component(chp->ata_drives[i]);
448 #endif /* NATARAID > 0 */
449
450 /*
451 * reset drive_flags for unattached devices, reset state for attached
452 * ones
453 */
454 for (i = 0; i < 2; i++) {
455 if (chp->ch_drive[i].drv_softc == NULL)
456 chp->ch_drive[i].drive_flags = 0;
457 else
458 chp->ch_drive[i].state = 0;
459 }
460
461 out:
462 if (atabus_initq == NULL) {
463 simple_lock(&atabus_interlock);
464 while(1) {
465 atabus_initq = TAILQ_FIRST(&atabus_initq_head);
466 if (atabus_initq->atabus_sc == atabus_sc)
467 break;
468 ltsleep(&atabus_initq_head, PRIBIO, "ata_initq", 0,
469 &atabus_interlock);
470 }
471 simple_unlock(&atabus_interlock);
472 }
473 simple_lock(&atabus_interlock);
474 TAILQ_REMOVE(&atabus_initq_head, atabus_initq, atabus_initq);
475 simple_unlock(&atabus_interlock);
476
477 free(atabus_initq, M_DEVBUF);
478 wakeup(&atabus_initq_head);
479
480 config_pending_decr();
481 if (need_delref)
482 wdc_delref(chp);
483 }
484
485 int
486 wdcprobe(struct wdc_channel *chp)
487 {
488
489 return (wdcprobe1(chp, 1));
490 }
491
492 static int
493 wdcprobe1(struct wdc_channel *chp, int poll)
494 {
495 struct wdc_softc *wdc = chp->ch_wdc;
496 u_int8_t st0, st1, sc, sn, cl, ch;
497 u_int8_t ret_value = 0x03;
498 u_int8_t drive;
499 int s;
500
501 /*
502 * Sanity check to see if the wdc channel responds at all.
503 */
504
505 s = splbio();
506 if (wdc == NULL ||
507 (wdc->cap & WDC_CAPABILITY_NO_EXTRA_RESETS) == 0) {
508
509 if (wdc != NULL && (wdc->cap & WDC_CAPABILITY_SELECT))
510 wdc->select(chp,0);
511
512 bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_sdh], 0,
513 WDSD_IBM);
514 delay(10); /* 400ns delay */
515 st0 = bus_space_read_1(chp->cmd_iot,
516 chp->cmd_iohs[wd_status], 0);
517
518 if (wdc != NULL && (wdc->cap & WDC_CAPABILITY_SELECT))
519 wdc->select(chp,1);
520
521 bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_sdh], 0,
522 WDSD_IBM | 0x10);
523 delay(10); /* 400ns delay */
524 st1 = bus_space_read_1(chp->cmd_iot,
525 chp->cmd_iohs[wd_status], 0);
526
527 WDCDEBUG_PRINT(("%s:%d: before reset, st0=0x%x, st1=0x%x\n",
528 wdc != NULL ? wdc->sc_dev.dv_xname : "wdcprobe",
529 chp->ch_channel, st0, st1), DEBUG_PROBE);
530
531 if (st0 == 0xff || st0 == WDSD_IBM)
532 ret_value &= ~0x01;
533 if (st1 == 0xff || st1 == (WDSD_IBM | 0x10))
534 ret_value &= ~0x02;
535 /* Register writability test, drive 0. */
536 if (ret_value & 0x01) {
537 if (wdc != NULL && (wdc->cap & WDC_CAPABILITY_SELECT))
538 wdc->select(chp,0);
539 bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_sdh],
540 0, WDSD_IBM);
541 bus_space_write_1(chp->cmd_iot,
542 chp->cmd_iohs[wd_cyl_lo], 0, 0x02);
543 cl = bus_space_read_1(chp->cmd_iot,
544 chp->cmd_iohs[wd_cyl_lo], 0);
545 if (cl != 0x02) {
546 WDCDEBUG_PRINT(("%s:%d drive 0 wd_cyl_lo: "
547 "got 0x%x != 0x02\n",
548 wdc != NULL ?
549 wdc->sc_dev.dv_xname : "wdcprobe",
550 chp->ch_channel, cl),
551 DEBUG_PROBE);
552 ret_value &= ~0x01;
553 }
554 bus_space_write_1(chp->cmd_iot,
555 chp->cmd_iohs[wd_cyl_lo], 0, 0x01);
556 cl = bus_space_read_1(chp->cmd_iot,
557 chp->cmd_iohs[wd_cyl_lo], 0);
558 if (cl != 0x01) {
559 WDCDEBUG_PRINT(("%s:%d drive 0 wd_cyl_lo: "
560 "got 0x%x != 0x01\n",
561 wdc != NULL ?
562 wdc->sc_dev.dv_xname : "wdcprobe",
563 chp->ch_channel, cl),
564 DEBUG_PROBE);
565 ret_value &= ~0x01;
566 }
567 bus_space_write_1(chp->cmd_iot,
568 chp->cmd_iohs[wd_sector], 0, 0x01);
569 cl = bus_space_read_1(chp->cmd_iot,
570 chp->cmd_iohs[wd_sector], 0);
571 if (cl != 0x01) {
572 WDCDEBUG_PRINT(("%s:%d drive 0 wd_sector: "
573 "got 0x%x != 0x01\n",
574 wdc != NULL ?
575 wdc->sc_dev.dv_xname : "wdcprobe",
576 chp->ch_channel, cl),
577 DEBUG_PROBE);
578 ret_value &= ~0x01;
579 }
580 bus_space_write_1(chp->cmd_iot,
581 chp->cmd_iohs[wd_sector], 0, 0x02);
582 cl = bus_space_read_1(chp->cmd_iot,
583 chp->cmd_iohs[wd_sector], 0);
584 if (cl != 0x02) {
585 WDCDEBUG_PRINT(("%s:%d drive 0 wd_sector: "
586 "got 0x%x != 0x02\n",
587 wdc != NULL ?
588 wdc->sc_dev.dv_xname : "wdcprobe",
589 chp->ch_channel, cl),
590 DEBUG_PROBE);
591 ret_value &= ~0x01;
592 }
593 cl = bus_space_read_1(chp->cmd_iot,
594 chp->cmd_iohs[wd_cyl_lo], 0);
595 if (cl != 0x01) {
596 WDCDEBUG_PRINT(("%s:%d drive 0 wd_cyl_lo(2): "
597 "got 0x%x != 0x01\n",
598 wdc != NULL ?
599 wdc->sc_dev.dv_xname : "wdcprobe",
600 chp->ch_channel, cl),
601 DEBUG_PROBE);
602 ret_value &= ~0x01;
603 }
604 }
605 /* Register writability test, drive 1. */
606 if (ret_value & 0x02) {
607 if (wdc != NULL && (wdc->cap & WDC_CAPABILITY_SELECT))
608 wdc->select(chp,1);
609 bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_sdh],
610 0, WDSD_IBM | 0x10);
611 bus_space_write_1(chp->cmd_iot,
612 chp->cmd_iohs[wd_cyl_lo], 0, 0x02);
613 cl = bus_space_read_1(chp->cmd_iot,
614 chp->cmd_iohs[wd_cyl_lo], 0);
615 if (cl != 0x02) {
616 WDCDEBUG_PRINT(("%s:%d drive 1 wd_cyl_lo: "
617 "got 0x%x != 0x02\n",
618 wdc != NULL ?
619 wdc->sc_dev.dv_xname : "wdcprobe",
620 chp->ch_channel, cl),
621 DEBUG_PROBE);
622 ret_value &= ~0x02;
623 }
624 bus_space_write_1(chp->cmd_iot,
625 chp->cmd_iohs[wd_cyl_lo], 0, 0x01);
626 cl = bus_space_read_1(chp->cmd_iot,
627 chp->cmd_iohs[wd_cyl_lo], 0);
628 if (cl != 0x01) {
629 WDCDEBUG_PRINT(("%s:%d drive 1 wd_cyl_lo: "
630 "got 0x%x != 0x01\n",
631 wdc != NULL ?
632 wdc->sc_dev.dv_xname : "wdcprobe",
633 chp->ch_channel, cl),
634 DEBUG_PROBE);
635 ret_value &= ~0x02;
636 }
637 bus_space_write_1(chp->cmd_iot,
638 chp->cmd_iohs[wd_sector], 0, 0x01);
639 cl = bus_space_read_1(chp->cmd_iot,
640 chp->cmd_iohs[wd_sector], 0);
641 if (cl != 0x01) {
642 WDCDEBUG_PRINT(("%s:%d drive 1 wd_sector: "
643 "got 0x%x != 0x01\n",
644 wdc != NULL ?
645 wdc->sc_dev.dv_xname : "wdcprobe",
646 chp->ch_channel, cl),
647 DEBUG_PROBE);
648 ret_value &= ~0x02;
649 }
650 bus_space_write_1(chp->cmd_iot,
651 chp->cmd_iohs[wd_sector], 0, 0x02);
652 cl = bus_space_read_1(chp->cmd_iot,
653 chp->cmd_iohs[wd_sector], 0);
654 if (cl != 0x02) {
655 WDCDEBUG_PRINT(("%s:%d drive 1 wd_sector: "
656 "got 0x%x != 0x02\n",
657 wdc != NULL ?
658 wdc->sc_dev.dv_xname : "wdcprobe",
659 chp->ch_channel, cl),
660 DEBUG_PROBE);
661 ret_value &= ~0x02;
662 }
663 cl = bus_space_read_1(chp->cmd_iot,
664 chp->cmd_iohs[wd_cyl_lo], 0);
665 if (cl != 0x01) {
666 WDCDEBUG_PRINT(("%s:%d drive 1 wd_cyl_lo(2): "
667 "got 0x%x != 0x01\n",
668 wdc != NULL ?
669 wdc->sc_dev.dv_xname : "wdcprobe",
670 chp->ch_channel, cl),
671 DEBUG_PROBE);
672 ret_value &= ~0x02;
673 }
674 }
675
676 if (ret_value == 0) {
677 splx(s);
678 return 0;
679 }
680 }
681
682
683 #if 0 /* XXX this break some ATA or ATAPI devices */
684 /*
685 * reset bus. Also send an ATAPI_RESET to devices, in case there are
686 * ATAPI device out there which don't react to the bus reset
687 */
688 if (ret_value & 0x01) {
689 if (wdc != NULL && (wdc->cap & WDC_CAPABILITY_SELECT))
690 wdc->select(chp,0);
691 bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_sdh],
692 0, WDSD_IBM);
693 bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_command], 0,
694 ATAPI_SOFT_RESET);
695 }
696 if (ret_value & 0x02) {
697 if (wdc != NULL && (wdc->cap & WDC_CAPABILITY_SELECT))
698 wdc->select(chp,0);
699 bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_sdh],
700 0, WDSD_IBM | 0x10);
701 bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_command], 0,
702 ATAPI_SOFT_RESET);
703 }
704
705 delay(5000);
706 #endif
707
708 if (wdc != NULL && (wdc->cap & WDC_CAPABILITY_SELECT))
709 wdc->select(chp,0);
710 bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_sdh], 0, WDSD_IBM);
711 delay(10); /* 400ns delay */
712 /* assert SRST, wait for reset to complete */
713 bus_space_write_1(chp->ctl_iot, chp->ctl_ioh, wd_aux_ctlr,
714 WDCTL_RST | WDCTL_IDS | WDCTL_4BIT);
715 DELAY(1000);
716 bus_space_write_1(chp->ctl_iot, chp->ctl_ioh, wd_aux_ctlr,
717 WDCTL_IDS | WDCTL_4BIT);
718 DELAY(2000);
719 (void) bus_space_read_1(chp->cmd_iot, chp->cmd_iohs[wd_error], 0);
720 bus_space_write_1(chp->ctl_iot, chp->ctl_ioh, wd_aux_ctlr, WDCTL_4BIT);
721 delay(10); /* 400ns delay */
722 /* ACK interrupt in case there is one pending left (Promise ATA100) */
723 if (wdc != NULL && (wdc->cap & WDC_CAPABILITY_IRQACK))
724 wdc->irqack(chp);
725 splx(s);
726
727 ret_value = __wdcwait_reset(chp, ret_value, poll);
728 WDCDEBUG_PRINT(("%s:%d: after reset, ret_value=0x%d\n",
729 wdc != NULL ? wdc->sc_dev.dv_xname : "wdcprobe", chp->ch_channel,
730 ret_value), DEBUG_PROBE);
731
732 /* if reset failed, there's nothing here */
733 if (ret_value == 0)
734 return 0;
735
736 /*
737 * Test presence of drives. First test register signatures looking
738 * for ATAPI devices. If it's not an ATAPI and reset said there may
739 * be something here assume it's ATA or OLD. Ghost will be killed
740 * later in attach routine.
741 */
742 for (drive = 0; drive < 2; drive++) {
743 if ((ret_value & (0x01 << drive)) == 0)
744 continue;
745 if (wdc != NULL && wdc->cap & WDC_CAPABILITY_SELECT)
746 wdc->select(chp,drive);
747 bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_sdh], 0,
748 WDSD_IBM | (drive << 4));
749 delay(10); /* 400ns delay */
750 /* Save registers contents */
751 sc = bus_space_read_1(chp->cmd_iot,
752 chp->cmd_iohs[wd_seccnt], 0);
753 sn = bus_space_read_1(chp->cmd_iot,
754 chp->cmd_iohs[wd_sector], 0);
755 cl = bus_space_read_1(chp->cmd_iot,
756 chp->cmd_iohs[wd_cyl_lo], 0);
757 ch = bus_space_read_1(chp->cmd_iot,
758 chp->cmd_iohs[wd_cyl_hi], 0);
759
760 WDCDEBUG_PRINT(("%s:%d:%d: after reset, sc=0x%x sn=0x%x "
761 "cl=0x%x ch=0x%x\n",
762 wdc != NULL ? wdc->sc_dev.dv_xname : "wdcprobe",
763 chp->ch_channel, drive, sc, sn, cl, ch), DEBUG_PROBE);
764 /*
765 * sc & sn are supposted to be 0x1 for ATAPI but in some cases
766 * we get wrong values here, so ignore it.
767 */
768 if (cl == 0x14 && ch == 0xeb) {
769 chp->ch_drive[drive].drive_flags |= DRIVE_ATAPI;
770 } else {
771 chp->ch_drive[drive].drive_flags |= DRIVE_ATA;
772 if (wdc == NULL ||
773 (wdc->cap & WDC_CAPABILITY_PREATA) != 0)
774 chp->ch_drive[drive].drive_flags |= DRIVE_OLD;
775 }
776 }
777 return (ret_value);
778 }
779
780 void
781 wdcattach(struct wdc_channel *chp)
782 {
783 struct wdc_softc *wdc = chp->ch_wdc;
784 static int inited = 0;
785
786 if (chp->ch_flags & WDCF_DISABLED)
787 return;
788
789 /* initialise global data */
790 callout_init(&chp->ch_callout);
791 if (wdc->drv_probe == NULL)
792 wdc->drv_probe = wdc_drvprobe;
793 if (inited == 0) {
794 /* Initialize the ata_xfer pool. */
795 pool_init(&wdc_xfer_pool, sizeof(struct ata_xfer), 0,
796 0, 0, "wdcspl", NULL);
797 inited++;
798 }
799 TAILQ_INIT(&chp->ch_queue->queue_xfer);
800 chp->ch_queue->queue_freeze = 0;
801
802 chp->atabus = config_found(&wdc->sc_dev, chp, atabusprint);
803 }
804
805 int
806 wdcactivate(struct device *self, enum devact act)
807 {
808 struct wdc_softc *wdc = (struct wdc_softc *)self;
809 int s, i, error = 0;
810
811 s = splbio();
812 switch (act) {
813 case DVACT_ACTIVATE:
814 error = EOPNOTSUPP;
815 break;
816
817 case DVACT_DEACTIVATE:
818 for (i = 0; i < wdc->nchannels; i++) {
819 error = config_deactivate(wdc->channels[i]->atabus);
820 if (error)
821 break;
822 }
823 break;
824 }
825 splx(s);
826 return (error);
827 }
828
829 int
830 wdcdetach(struct device *self, int flags)
831 {
832 struct wdc_softc *wdc = (struct wdc_softc *)self;
833 struct wdc_channel *chp;
834 int i, error = 0;
835
836 for (i = 0; i < wdc->nchannels; i++) {
837 chp = wdc->channels[i];
838 WDCDEBUG_PRINT(("wdcdetach: %s: detaching %s\n",
839 wdc->sc_dev.dv_xname, chp->atabus->dv_xname), DEBUG_DETACH);
840 error = config_detach(chp->atabus, flags);
841 if (error)
842 break;
843 }
844 return (error);
845 }
846
847 /*
848 * Start I/O on a controller, for the given channel.
849 * The first xfer may be not for our channel if the channel queues
850 * are shared.
851 */
852 void
853 wdcstart(struct wdc_channel *chp)
854 {
855 struct wdc_softc *wdc = chp->ch_wdc;
856 struct ata_xfer *xfer;
857
858 #ifdef WDC_DIAGNOSTIC
859 int spl1, spl2;
860
861 spl1 = splbio();
862 spl2 = splbio();
863 if (spl2 != spl1) {
864 printf("wdcstart: not at splbio()\n");
865 panic("wdcstart");
866 }
867 splx(spl2);
868 splx(spl1);
869 #endif /* WDC_DIAGNOSTIC */
870
871 /* is there a xfer ? */
872 if ((xfer = TAILQ_FIRST(&chp->ch_queue->queue_xfer)) == NULL)
873 return;
874
875 /* adjust chp, in case we have a shared queue */
876 chp = xfer->c_chp;
877
878 if ((chp->ch_flags & WDCF_ACTIVE) != 0 ) {
879 return; /* channel aleady active */
880 }
881 if (__predict_false(chp->ch_queue->queue_freeze > 0)) {
882 return; /* queue froozen */
883 }
884 #ifdef DIAGNOSTIC
885 if ((chp->ch_flags & WDCF_IRQ_WAIT) != 0)
886 panic("wdcstart: channel waiting for irq");
887 #endif
888 if (wdc->cap & WDC_CAPABILITY_HWLOCK)
889 if (!(*wdc->claim_hw)(chp, 0))
890 return;
891
892 WDCDEBUG_PRINT(("wdcstart: xfer %p channel %d drive %d\n", xfer,
893 chp->ch_channel, xfer->c_drive), DEBUG_XFERS);
894 chp->ch_flags |= WDCF_ACTIVE;
895 if (chp->ch_drive[xfer->c_drive].drive_flags & DRIVE_RESET) {
896 chp->ch_drive[xfer->c_drive].drive_flags &= ~DRIVE_RESET;
897 chp->ch_drive[xfer->c_drive].state = 0;
898 }
899 if (wdc->cap & WDC_CAPABILITY_NOIRQ)
900 KASSERT(xfer->c_flags & C_POLL);
901 xfer->c_start(chp, xfer);
902 }
903
904 /* restart an interrupted I/O */
905 void
906 wdcrestart(void *v)
907 {
908 struct wdc_channel *chp = v;
909 int s;
910
911 s = splbio();
912 wdcstart(chp);
913 splx(s);
914 }
915
916
917 /*
918 * Interrupt routine for the controller. Acknowledge the interrupt, check for
919 * errors on the current operation, mark it done if necessary, and start the
920 * next request. Also check for a partially done transfer, and continue with
921 * the next chunk if so.
922 */
923 int
924 wdcintr(void *arg)
925 {
926 struct wdc_channel *chp = arg;
927 struct wdc_softc *wdc = chp->ch_wdc;
928 struct ata_xfer *xfer;
929 int ret;
930
931 if ((wdc->sc_dev.dv_flags & DVF_ACTIVE) == 0) {
932 WDCDEBUG_PRINT(("wdcintr: deactivated controller\n"),
933 DEBUG_INTR);
934 return (0);
935 }
936 if ((chp->ch_flags & WDCF_IRQ_WAIT) == 0) {
937 WDCDEBUG_PRINT(("wdcintr: inactive controller\n"), DEBUG_INTR);
938 /* try to clear the pending interrupt anyway */
939 (void)bus_space_read_1(chp->cmd_iot,
940 chp->cmd_iohs[wd_status], 0);
941 return (0);
942 }
943
944 WDCDEBUG_PRINT(("wdcintr\n"), DEBUG_INTR);
945 xfer = TAILQ_FIRST(&chp->ch_queue->queue_xfer);
946 if (chp->ch_flags & WDCF_DMA_WAIT) {
947 wdc->dma_status =
948 (*wdc->dma_finish)(wdc->dma_arg, chp->ch_channel,
949 xfer->c_drive, 0);
950 if (wdc->dma_status & WDC_DMAST_NOIRQ) {
951 /* IRQ not for us, not detected by DMA engine */
952 return 0;
953 }
954 chp->ch_flags &= ~WDCF_DMA_WAIT;
955 }
956 chp->ch_flags &= ~WDCF_IRQ_WAIT;
957 ret = xfer->c_intr(chp, xfer, 1);
958 if (ret == 0) /* irq was not for us, still waiting for irq */
959 chp->ch_flags |= WDCF_IRQ_WAIT;
960 return (ret);
961 }
962
963 /* Put all disk in RESET state */
964 void
965 wdc_reset_channel(struct ata_drive_datas *drvp, int flags)
966 {
967 struct wdc_channel *chp = drvp->chnl_softc;
968 struct wdc_softc *wdc = chp->ch_wdc;
969 int drive;
970
971 WDCDEBUG_PRINT(("ata_reset_channel %s:%d for drive %d\n",
972 wdc->sc_dev.dv_xname, chp->ch_channel, drvp->drive),
973 DEBUG_FUNCS);
974 if ((flags & AT_POLL) == 0) {
975 if (chp->ch_flags & WDCF_TH_RESET) {
976 /* no need to schedule a reset more than one time */
977 return;
978 }
979 chp->ch_flags |= WDCF_TH_RESET;
980 chp->ch_queue->queue_freeze++;
981 wakeup(&chp->ch_thread);
982 return;
983 }
984 (void) wdcreset(chp, RESET_POLL);
985 for (drive = 0; drive < 2; drive++) {
986 chp->ch_drive[drive].state = 0;
987 }
988 }
989
990 int
991 wdcreset(struct wdc_channel *chp, int poll)
992 {
993 struct wdc_softc *wdc = chp->ch_wdc;
994 int drv_mask1, drv_mask2;
995 int s = 0;
996
997 if (wdc->cap & WDC_CAPABILITY_SELECT)
998 wdc->select(chp,0);
999 if (poll != RESET_SLEEP)
1000 s = splbio();
1001 /* master */
1002 bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_sdh], 0, WDSD_IBM);
1003 delay(10); /* 400ns delay */
1004 bus_space_write_1(chp->ctl_iot, chp->ctl_ioh, wd_aux_ctlr,
1005 WDCTL_RST | WDCTL_IDS | WDCTL_4BIT);
1006 delay(2000);
1007 (void) bus_space_read_1(chp->cmd_iot, chp->cmd_iohs[wd_error], 0);
1008 bus_space_write_1(chp->ctl_iot, chp->ctl_ioh, wd_aux_ctlr,
1009 WDCTL_4BIT | WDCTL_IDS);
1010 delay(10); /* 400ns delay */
1011 if (poll != RESET_SLEEP) {
1012 if (wdc->cap & WDC_CAPABILITY_IRQACK)
1013 wdc->irqack(chp);
1014 splx(s);
1015 }
1016
1017 drv_mask1 = (chp->ch_drive[0].drive_flags & DRIVE) ? 0x01:0x00;
1018 drv_mask1 |= (chp->ch_drive[1].drive_flags & DRIVE) ? 0x02:0x00;
1019 drv_mask2 = __wdcwait_reset(chp, drv_mask1,
1020 (poll == RESET_SLEEP) ? 0 : 1);
1021 if (drv_mask2 != drv_mask1) {
1022 printf("%s channel %d: reset failed for",
1023 wdc->sc_dev.dv_xname, chp->ch_channel);
1024 if ((drv_mask1 & 0x01) != 0 && (drv_mask2 & 0x01) == 0)
1025 printf(" drive 0");
1026 if ((drv_mask1 & 0x02) != 0 && (drv_mask2 & 0x02) == 0)
1027 printf(" drive 1");
1028 printf("\n");
1029 }
1030 bus_space_write_1(chp->ctl_iot, chp->ctl_ioh, wd_aux_ctlr, WDCTL_4BIT);
1031 return (drv_mask1 != drv_mask2) ? 1 : 0;
1032 }
1033
1034 static int
1035 __wdcwait_reset(struct wdc_channel *chp, int drv_mask, int poll)
1036 {
1037 struct wdc_softc *wdc = chp->ch_wdc;
1038 int timeout, nloop;
1039 u_int8_t st0 = 0, st1 = 0;
1040 #ifdef WDCDEBUG
1041 u_int8_t sc0 = 0, sn0 = 0, cl0 = 0, ch0 = 0;
1042 u_int8_t sc1 = 0, sn1 = 0, cl1 = 0, ch1 = 0;
1043 #endif
1044
1045 if (poll)
1046 nloop = WDCNDELAY_RST;
1047 else
1048 nloop = WDC_RESET_WAIT * hz / 1000;
1049 /* wait for BSY to deassert */
1050 for (timeout = 0; timeout < nloop; timeout++) {
1051 if ((drv_mask & 0x01) != 0) {
1052 if (wdc && wdc->cap & WDC_CAPABILITY_SELECT)
1053 wdc->select(chp,0);
1054 bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_sdh],
1055 0, WDSD_IBM); /* master */
1056 delay(10);
1057 st0 = bus_space_read_1(chp->cmd_iot,
1058 chp->cmd_iohs[wd_status], 0);
1059 #ifdef WDCDEBUG
1060 sc0 = bus_space_read_1(chp->cmd_iot,
1061 chp->cmd_iohs[wd_seccnt], 0);
1062 sn0 = bus_space_read_1(chp->cmd_iot,
1063 chp->cmd_iohs[wd_sector], 0);
1064 cl0 = bus_space_read_1(chp->cmd_iot,
1065 chp->cmd_iohs[wd_cyl_lo], 0);
1066 ch0 = bus_space_read_1(chp->cmd_iot,
1067 chp->cmd_iohs[wd_cyl_hi], 0);
1068 #endif
1069 }
1070 if ((drv_mask & 0x02) != 0) {
1071 if (wdc && wdc->cap & WDC_CAPABILITY_SELECT)
1072 wdc->select(chp,1);
1073 bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_sdh],
1074 0, WDSD_IBM | 0x10); /* slave */
1075 delay(10);
1076 st1 = bus_space_read_1(chp->cmd_iot,
1077 chp->cmd_iohs[wd_status], 0);
1078 #ifdef WDCDEBUG
1079 sc1 = bus_space_read_1(chp->cmd_iot,
1080 chp->cmd_iohs[wd_seccnt], 0);
1081 sn1 = bus_space_read_1(chp->cmd_iot,
1082 chp->cmd_iohs[wd_sector], 0);
1083 cl1 = bus_space_read_1(chp->cmd_iot,
1084 chp->cmd_iohs[wd_cyl_lo], 0);
1085 ch1 = bus_space_read_1(chp->cmd_iot,
1086 chp->cmd_iohs[wd_cyl_hi], 0);
1087 #endif
1088 }
1089
1090 if ((drv_mask & 0x01) == 0) {
1091 /* no master */
1092 if ((drv_mask & 0x02) != 0 && (st1 & WDCS_BSY) == 0) {
1093 /* No master, slave is ready, it's done */
1094 goto end;
1095 }
1096 } else if ((drv_mask & 0x02) == 0) {
1097 /* no slave */
1098 if ((drv_mask & 0x01) != 0 && (st0 & WDCS_BSY) == 0) {
1099 /* No slave, master is ready, it's done */
1100 goto end;
1101 }
1102 } else {
1103 /* Wait for both master and slave to be ready */
1104 if ((st0 & WDCS_BSY) == 0 && (st1 & WDCS_BSY) == 0) {
1105 goto end;
1106 }
1107 }
1108 if (poll)
1109 delay(WDCDELAY);
1110 else
1111 tsleep(&nloop, PRIBIO, "atarst", 1);
1112 }
1113 /* Reset timed out. Maybe it's because drv_mask was not right */
1114 if (st0 & WDCS_BSY)
1115 drv_mask &= ~0x01;
1116 if (st1 & WDCS_BSY)
1117 drv_mask &= ~0x02;
1118 end:
1119 WDCDEBUG_PRINT(("%s:%d:0: after reset, sc=0x%x sn=0x%x "
1120 "cl=0x%x ch=0x%x\n",
1121 wdc != NULL ? wdc->sc_dev.dv_xname : "wdcprobe",
1122 chp->ch_channel, sc0, sn0, cl0, ch0), DEBUG_PROBE);
1123 WDCDEBUG_PRINT(("%s:%d:1: after reset, sc=0x%x sn=0x%x "
1124 "cl=0x%x ch=0x%x\n",
1125 wdc != NULL ? wdc->sc_dev.dv_xname : "wdcprobe",
1126 chp->ch_channel, sc1, sn1, cl1, ch1), DEBUG_PROBE);
1127
1128 WDCDEBUG_PRINT(("%s:%d: wdcwait_reset() end, st0=0x%x st1=0x%x\n",
1129 wdc != NULL ? wdc->sc_dev.dv_xname : "wdcprobe", chp->ch_channel,
1130 st0, st1), DEBUG_PROBE);
1131
1132 return drv_mask;
1133 }
1134
1135 /*
1136 * Wait for a drive to be !BSY, and have mask in its status register.
1137 * return -1 for a timeout after "timeout" ms.
1138 */
1139 static int
1140 __wdcwait(struct wdc_channel *chp, int mask, int bits, int timeout)
1141 {
1142 struct wdc_softc *wdc = chp->ch_wdc;
1143 u_char status;
1144 int time = 0;
1145
1146 WDCDEBUG_PRINT(("__wdcwait %s:%d\n", wdc != NULL ?
1147 wdc->sc_dev.dv_xname : "none",
1148 chp->ch_channel), DEBUG_STATUS);
1149 chp->ch_error = 0;
1150
1151 timeout = timeout * 1000 / WDCDELAY; /* delay uses microseconds */
1152
1153 for (;;) {
1154 chp->ch_status = status =
1155 bus_space_read_1(chp->cmd_iot, chp->cmd_iohs[wd_status], 0);
1156 if ((status & (WDCS_BSY | mask)) == bits)
1157 break;
1158 if (++time > timeout) {
1159 WDCDEBUG_PRINT(("__wdcwait: timeout (time=%d), "
1160 "status %x error %x (mask 0x%x bits 0x%x)\n",
1161 time, status,
1162 bus_space_read_1(chp->cmd_iot,
1163 chp->cmd_iohs[wd_error], 0), mask, bits),
1164 DEBUG_STATUS | DEBUG_PROBE | DEBUG_DELAY);
1165 return(WDCWAIT_TOUT);
1166 }
1167 delay(WDCDELAY);
1168 }
1169 #ifdef WDCDEBUG
1170 if (time > 0 && (wdcdebug_mask & DEBUG_DELAY))
1171 printf("__wdcwait: did busy-wait, time=%d\n", time);
1172 #endif
1173 if (status & WDCS_ERR)
1174 chp->ch_error = bus_space_read_1(chp->cmd_iot,
1175 chp->cmd_iohs[wd_error], 0);
1176 #ifdef WDCNDELAY_DEBUG
1177 /* After autoconfig, there should be no long delays. */
1178 if (!cold && time > WDCNDELAY_DEBUG) {
1179 struct ata_xfer *xfer = TAILQ_FIRST(&chp->ch_queue->queue_xfer);
1180 if (xfer == NULL)
1181 printf("%s channel %d: warning: busy-wait took %dus\n",
1182 wdc->sc_dev.dv_xname, chp->ch_channel,
1183 WDCDELAY * time);
1184 else
1185 printf("%s:%d:%d: warning: busy-wait took %dus\n",
1186 wdc->sc_dev.dv_xname, chp->ch_channel,
1187 xfer->drive,
1188 WDCDELAY * time);
1189 }
1190 #endif
1191 return(WDCWAIT_OK);
1192 }
1193
1194 /*
1195 * Call __wdcwait(), polling using tsleep() or waking up the kernel
1196 * thread if possible
1197 */
1198 int
1199 wdcwait(struct wdc_channel *chp, int mask, int bits, int timeout, int flags)
1200 {
1201 int error, i, timeout_hz = mstohz(timeout);
1202
1203 if (timeout_hz == 0 ||
1204 (flags & (AT_WAIT | AT_POLL)) == AT_POLL)
1205 error = __wdcwait(chp, mask, bits, timeout);
1206 else {
1207 error = __wdcwait(chp, mask, bits, WDCDELAY_POLL);
1208 if (error != 0) {
1209 if ((chp->ch_flags & WDCF_TH_RUN) ||
1210 (flags & AT_WAIT)) {
1211 /*
1212 * we're running in the channel thread
1213 * or some userland thread context
1214 */
1215 for (i = 0; i < timeout_hz; i++) {
1216 if (__wdcwait(chp, mask, bits,
1217 WDCDELAY_POLL) == 0) {
1218 error = 0;
1219 break;
1220 }
1221 tsleep(&chp, PRIBIO, "atapoll", 1);
1222 }
1223 } else {
1224 /*
1225 * we're probably in interrupt context,
1226 * ask the thread to come back here
1227 */
1228 #ifdef DIAGNOSTIC
1229 if (chp->ch_queue->queue_freeze > 0)
1230 panic("wdcwait: queue_freeze");
1231 #endif
1232 chp->ch_queue->queue_freeze++;
1233 wakeup(&chp->ch_thread);
1234 return(WDCWAIT_THR);
1235 }
1236 }
1237 }
1238 return (error);
1239 }
1240
1241
1242 /*
1243 * Busy-wait for DMA to complete
1244 */
1245 int
1246 wdc_dmawait(struct wdc_channel *chp, struct ata_xfer *xfer, int timeout)
1247 {
1248 struct wdc_softc *wdc = chp->ch_wdc;
1249 int time;
1250
1251 for (time = 0; time < timeout * 1000 / WDCDELAY; time++) {
1252 wdc->dma_status =
1253 (*wdc->dma_finish)(wdc->dma_arg,
1254 chp->ch_channel, xfer->c_drive, 0);
1255 if ((wdc->dma_status & WDC_DMAST_NOIRQ) == 0)
1256 return 0;
1257 delay(WDCDELAY);
1258 }
1259 /* timeout, force a DMA halt */
1260 wdc->dma_status = (*wdc->dma_finish)(wdc->dma_arg,
1261 chp->ch_channel, xfer->c_drive, 1);
1262 return 1;
1263 }
1264
1265 void
1266 wdctimeout(void *arg)
1267 {
1268 struct wdc_channel *chp = (struct wdc_channel *)arg;
1269 struct wdc_softc *wdc = chp->ch_wdc;
1270 struct ata_xfer *xfer = TAILQ_FIRST(&chp->ch_queue->queue_xfer);
1271 int s;
1272
1273 WDCDEBUG_PRINT(("wdctimeout\n"), DEBUG_FUNCS);
1274
1275 s = splbio();
1276 if ((chp->ch_flags & WDCF_IRQ_WAIT) != 0) {
1277 __wdcerror(chp, "lost interrupt");
1278 printf("\ttype: %s tc_bcount: %d tc_skip: %d\n",
1279 (xfer->c_flags & C_ATAPI) ? "atapi" : "ata",
1280 xfer->c_bcount,
1281 xfer->c_skip);
1282 if (chp->ch_flags & WDCF_DMA_WAIT) {
1283 wdc->dma_status =
1284 (*wdc->dma_finish)(wdc->dma_arg,
1285 chp->ch_channel, xfer->c_drive, 1);
1286 chp->ch_flags &= ~WDCF_DMA_WAIT;
1287 }
1288 /*
1289 * Call the interrupt routine. If we just missed an interrupt,
1290 * it will do what's needed. Else, it will take the needed
1291 * action (reset the device).
1292 * Before that we need to reinstall the timeout callback,
1293 * in case it will miss another irq while in this transfer
1294 * We arbitray chose it to be 1s
1295 */
1296 callout_reset(&chp->ch_callout, hz, wdctimeout, chp);
1297 xfer->c_flags |= C_TIMEOU;
1298 chp->ch_flags &= ~WDCF_IRQ_WAIT;
1299 xfer->c_intr(chp, xfer, 1);
1300 } else
1301 __wdcerror(chp, "missing untimeout");
1302 splx(s);
1303 }
1304
1305 /*
1306 * Probe drive's capabilities, for use by the controller later
1307 * Assumes drvp points to an existing drive.
1308 * XXX this should be a controller-indep function
1309 */
1310 void
1311 wdc_probe_caps(struct ata_drive_datas *drvp)
1312 {
1313 struct ataparams params, params2;
1314 struct wdc_channel *chp = drvp->chnl_softc;
1315 struct wdc_softc *wdc = chp->ch_wdc;
1316 struct device *drv_dev = drvp->drv_softc;
1317 int i, printed;
1318 char *sep = "";
1319 int cf_flags;
1320
1321 if (ata_get_params(drvp, AT_WAIT, ¶ms) != CMD_OK) {
1322 /* IDENTIFY failed. Can't tell more about the device */
1323 return;
1324 }
1325 if ((wdc->cap & (WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32)) ==
1326 (WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32)) {
1327 /*
1328 * Controller claims 16 and 32 bit transfers.
1329 * Re-do an IDENTIFY with 32-bit transfers,
1330 * and compare results.
1331 */
1332 drvp->drive_flags |= DRIVE_CAP32;
1333 ata_get_params(drvp, AT_WAIT, ¶ms2);
1334 if (memcmp(¶ms, ¶ms2, sizeof(struct ataparams)) != 0) {
1335 /* Not good. fall back to 16bits */
1336 drvp->drive_flags &= ~DRIVE_CAP32;
1337 } else {
1338 aprint_normal("%s: 32-bit data port\n",
1339 drv_dev->dv_xname);
1340 }
1341 }
1342 #if 0 /* Some ultra-DMA drives claims to only support ATA-3. sigh */
1343 if (params.atap_ata_major > 0x01 &&
1344 params.atap_ata_major != 0xffff) {
1345 for (i = 14; i > 0; i--) {
1346 if (params.atap_ata_major & (1 << i)) {
1347 aprint_normal("%s: ATA version %d\n",
1348 drv_dev->dv_xname, i);
1349 drvp->ata_vers = i;
1350 break;
1351 }
1352 }
1353 }
1354 #endif
1355
1356 /* An ATAPI device is at last PIO mode 3 */
1357 if (drvp->drive_flags & DRIVE_ATAPI)
1358 drvp->PIO_mode = 3;
1359
1360 /*
1361 * It's not in the specs, but it seems that some drive
1362 * returns 0xffff in atap_extensions when this field is invalid
1363 */
1364 if (params.atap_extensions != 0xffff &&
1365 (params.atap_extensions & WDC_EXT_MODES)) {
1366 printed = 0;
1367 /*
1368 * XXX some drives report something wrong here (they claim to
1369 * support PIO mode 8 !). As mode is coded on 3 bits in
1370 * SET FEATURE, limit it to 7 (so limit i to 4).
1371 * If higher mode than 7 is found, abort.
1372 */
1373 for (i = 7; i >= 0; i--) {
1374 if ((params.atap_piomode_supp & (1 << i)) == 0)
1375 continue;
1376 if (i > 4)
1377 return;
1378 /*
1379 * See if mode is accepted.
1380 * If the controller can't set its PIO mode,
1381 * assume the defaults are good, so don't try
1382 * to set it
1383 */
1384 if ((wdc->cap & WDC_CAPABILITY_MODE) != 0)
1385 /*
1386 * It's OK to pool here, it's fast enouth
1387 * to not bother waiting for interrupt
1388 */
1389 if (ata_set_mode(drvp, 0x08 | (i + 3),
1390 AT_WAIT) != CMD_OK)
1391 continue;
1392 if (!printed) {
1393 aprint_normal("%s: drive supports PIO mode %d",
1394 drv_dev->dv_xname, i + 3);
1395 sep = ",";
1396 printed = 1;
1397 }
1398 /*
1399 * If controller's driver can't set its PIO mode,
1400 * get the highter one for the drive.
1401 */
1402 if ((wdc->cap & WDC_CAPABILITY_MODE) == 0 ||
1403 wdc->PIO_cap >= i + 3) {
1404 drvp->PIO_mode = i + 3;
1405 drvp->PIO_cap = i + 3;
1406 break;
1407 }
1408 }
1409 if (!printed) {
1410 /*
1411 * We didn't find a valid PIO mode.
1412 * Assume the values returned for DMA are buggy too
1413 */
1414 return;
1415 }
1416 drvp->drive_flags |= DRIVE_MODE;
1417 printed = 0;
1418 for (i = 7; i >= 0; i--) {
1419 if ((params.atap_dmamode_supp & (1 << i)) == 0)
1420 continue;
1421 if ((wdc->cap & WDC_CAPABILITY_DMA) &&
1422 (wdc->cap & WDC_CAPABILITY_MODE))
1423 if (ata_set_mode(drvp, 0x20 | i, AT_WAIT)
1424 != CMD_OK)
1425 continue;
1426 if (!printed) {
1427 aprint_normal("%s DMA mode %d", sep, i);
1428 sep = ",";
1429 printed = 1;
1430 }
1431 if (wdc->cap & WDC_CAPABILITY_DMA) {
1432 if ((wdc->cap & WDC_CAPABILITY_MODE) &&
1433 wdc->DMA_cap < i)
1434 continue;
1435 drvp->DMA_mode = i;
1436 drvp->DMA_cap = i;
1437 drvp->drive_flags |= DRIVE_DMA;
1438 }
1439 break;
1440 }
1441 if (params.atap_extensions & WDC_EXT_UDMA_MODES) {
1442 printed = 0;
1443 for (i = 7; i >= 0; i--) {
1444 if ((params.atap_udmamode_supp & (1 << i))
1445 == 0)
1446 continue;
1447 if ((wdc->cap & WDC_CAPABILITY_MODE) &&
1448 (wdc->cap & WDC_CAPABILITY_UDMA))
1449 if (ata_set_mode(drvp, 0x40 | i,
1450 AT_WAIT) != CMD_OK)
1451 continue;
1452 if (!printed) {
1453 aprint_normal("%s Ultra-DMA mode %d",
1454 sep, i);
1455 if (i == 2)
1456 aprint_normal(" (Ultra/33)");
1457 else if (i == 4)
1458 aprint_normal(" (Ultra/66)");
1459 else if (i == 5)
1460 aprint_normal(" (Ultra/100)");
1461 else if (i == 6)
1462 aprint_normal(" (Ultra/133)");
1463 sep = ",";
1464 printed = 1;
1465 }
1466 if (wdc->cap & WDC_CAPABILITY_UDMA) {
1467 if ((wdc->cap & WDC_CAPABILITY_MODE) &&
1468 wdc->UDMA_cap < i)
1469 continue;
1470 drvp->UDMA_mode = i;
1471 drvp->UDMA_cap = i;
1472 drvp->drive_flags |= DRIVE_UDMA;
1473 }
1474 break;
1475 }
1476 }
1477 aprint_normal("\n");
1478 }
1479
1480 /* Try to guess ATA version here, if it didn't get reported */
1481 if (drvp->ata_vers == 0) {
1482 if (drvp->drive_flags & DRIVE_UDMA)
1483 drvp->ata_vers = 4; /* should be at last ATA-4 */
1484 else if (drvp->PIO_cap > 2)
1485 drvp->ata_vers = 2; /* should be at last ATA-2 */
1486 }
1487 cf_flags = drv_dev->dv_cfdata->cf_flags;
1488 if (cf_flags & ATA_CONFIG_PIO_SET) {
1489 drvp->PIO_mode =
1490 (cf_flags & ATA_CONFIG_PIO_MODES) >> ATA_CONFIG_PIO_OFF;
1491 drvp->drive_flags |= DRIVE_MODE;
1492 }
1493 if ((wdc->cap & WDC_CAPABILITY_DMA) == 0) {
1494 /* don't care about DMA modes */
1495 return;
1496 }
1497 if (cf_flags & ATA_CONFIG_DMA_SET) {
1498 if ((cf_flags & ATA_CONFIG_DMA_MODES) ==
1499 ATA_CONFIG_DMA_DISABLE) {
1500 drvp->drive_flags &= ~DRIVE_DMA;
1501 } else {
1502 drvp->DMA_mode = (cf_flags & ATA_CONFIG_DMA_MODES) >>
1503 ATA_CONFIG_DMA_OFF;
1504 drvp->drive_flags |= DRIVE_DMA | DRIVE_MODE;
1505 }
1506 }
1507 if ((wdc->cap & WDC_CAPABILITY_UDMA) == 0) {
1508 /* don't care about UDMA modes */
1509 return;
1510 }
1511 if (cf_flags & ATA_CONFIG_UDMA_SET) {
1512 if ((cf_flags & ATA_CONFIG_UDMA_MODES) ==
1513 ATA_CONFIG_UDMA_DISABLE) {
1514 drvp->drive_flags &= ~DRIVE_UDMA;
1515 } else {
1516 drvp->UDMA_mode = (cf_flags & ATA_CONFIG_UDMA_MODES) >>
1517 ATA_CONFIG_UDMA_OFF;
1518 drvp->drive_flags |= DRIVE_UDMA | DRIVE_MODE;
1519 }
1520 }
1521 }
1522
1523 /*
1524 * downgrade the transfer mode of a drive after an error. return 1 if
1525 * downgrade was possible, 0 otherwise.
1526 */
1527 int
1528 wdc_downgrade_mode(struct ata_drive_datas *drvp, int flags)
1529 {
1530 struct wdc_channel *chp = drvp->chnl_softc;
1531 struct wdc_softc *wdc = chp->ch_wdc;
1532 struct device *drv_dev = drvp->drv_softc;
1533 int cf_flags = drv_dev->dv_cfdata->cf_flags;
1534
1535 /* if drive or controller don't know its mode, we can't do much */
1536 if ((drvp->drive_flags & DRIVE_MODE) == 0 ||
1537 (wdc->cap & WDC_CAPABILITY_MODE) == 0)
1538 return 0;
1539 /* current drive mode was set by a config flag, let it this way */
1540 if ((cf_flags & ATA_CONFIG_PIO_SET) ||
1541 (cf_flags & ATA_CONFIG_DMA_SET) ||
1542 (cf_flags & ATA_CONFIG_UDMA_SET))
1543 return 0;
1544
1545 /*
1546 * If we were using Ultra-DMA mode, downgrade to the next lower mode.
1547 */
1548 if ((drvp->drive_flags & DRIVE_UDMA) && drvp->UDMA_mode >= 2) {
1549 drvp->UDMA_mode--;
1550 printf("%s: transfer error, downgrading to Ultra-DMA mode %d\n",
1551 drv_dev->dv_xname, drvp->UDMA_mode);
1552 }
1553
1554 /*
1555 * If we were using ultra-DMA, don't downgrade to multiword DMA.
1556 */
1557 else if (drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA)) {
1558 drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
1559 drvp->PIO_mode = drvp->PIO_cap;
1560 printf("%s: transfer error, downgrading to PIO mode %d\n",
1561 drv_dev->dv_xname, drvp->PIO_mode);
1562 } else /* already using PIO, can't downgrade */
1563 return 0;
1564
1565 wdc->set_modes(chp);
1566 wdc_print_modes(chp);
1567 /* reset the channel, which will shedule all drives for setup */
1568 wdc_reset_channel(drvp, flags);
1569 return 1;
1570 }
1571
1572 int
1573 wdc_exec_command(struct ata_drive_datas *drvp, struct wdc_command *wdc_c)
1574 {
1575 struct wdc_channel *chp = drvp->chnl_softc;
1576 struct wdc_softc *wdc = chp->ch_wdc;
1577 struct ata_xfer *xfer;
1578 int s, ret;
1579
1580 WDCDEBUG_PRINT(("wdc_exec_command %s:%d:%d\n",
1581 wdc->sc_dev.dv_xname, chp->ch_channel, drvp->drive),
1582 DEBUG_FUNCS);
1583
1584 /* set up an xfer and queue. Wait for completion */
1585 xfer = wdc_get_xfer(wdc_c->flags & AT_WAIT ? WDC_CANSLEEP :
1586 WDC_NOSLEEP);
1587 if (xfer == NULL) {
1588 return WDC_TRY_AGAIN;
1589 }
1590
1591 if (wdc->cap & WDC_CAPABILITY_NOIRQ)
1592 wdc_c->flags |= AT_POLL;
1593 if (wdc_c->flags & AT_POLL)
1594 xfer->c_flags |= C_POLL;
1595 xfer->c_drive = drvp->drive;
1596 xfer->c_databuf = wdc_c->data;
1597 xfer->c_bcount = wdc_c->bcount;
1598 xfer->c_cmd = wdc_c;
1599 xfer->c_start = __wdccommand_start;
1600 xfer->c_intr = __wdccommand_intr;
1601 xfer->c_kill_xfer = __wdccommand_done;
1602
1603 s = splbio();
1604 wdc_exec_xfer(chp, xfer);
1605 #ifdef DIAGNOSTIC
1606 if ((wdc_c->flags & AT_POLL) != 0 &&
1607 (wdc_c->flags & AT_DONE) == 0)
1608 panic("wdc_exec_command: polled command not done");
1609 #endif
1610 if (wdc_c->flags & AT_DONE) {
1611 ret = WDC_COMPLETE;
1612 } else {
1613 if (wdc_c->flags & AT_WAIT) {
1614 while ((wdc_c->flags & AT_DONE) == 0) {
1615 tsleep(wdc_c, PRIBIO, "wdccmd", 0);
1616 }
1617 ret = WDC_COMPLETE;
1618 } else {
1619 ret = WDC_QUEUED;
1620 }
1621 }
1622 splx(s);
1623 return ret;
1624 }
1625
1626 static void
1627 __wdccommand_start(struct wdc_channel *chp, struct ata_xfer *xfer)
1628 {
1629 struct wdc_softc *wdc = chp->ch_wdc;
1630 int drive = xfer->c_drive;
1631 struct wdc_command *wdc_c = xfer->c_cmd;
1632
1633 WDCDEBUG_PRINT(("__wdccommand_start %s:%d:%d\n",
1634 wdc->sc_dev.dv_xname, chp->ch_channel, xfer->c_drive),
1635 DEBUG_FUNCS);
1636
1637 if (wdc->cap & WDC_CAPABILITY_SELECT)
1638 wdc->select(chp,drive);
1639 bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_sdh], 0,
1640 WDSD_IBM | (drive << 4));
1641 switch(wdcwait(chp, wdc_c->r_st_bmask | WDCS_DRQ,
1642 wdc_c->r_st_bmask, wdc_c->timeout, wdc_c->flags)) {
1643 case WDCWAIT_OK:
1644 break;
1645 case WDCWAIT_TOUT:
1646 wdc_c->flags |= AT_TIMEOU;
1647 __wdccommand_done(chp, xfer);
1648 return;
1649 case WDCWAIT_THR:
1650 return;
1651 }
1652 if (wdc_c->flags & AT_POLL) {
1653 /* polled command, disable interrupts */
1654 bus_space_write_1(chp->ctl_iot, chp->ctl_ioh, wd_aux_ctlr,
1655 WDCTL_4BIT | WDCTL_IDS);
1656 }
1657 wdccommand(chp, drive, wdc_c->r_command, wdc_c->r_cyl, wdc_c->r_head,
1658 wdc_c->r_sector, wdc_c->r_count, wdc_c->r_features);
1659
1660 if ((wdc_c->flags & AT_POLL) == 0) {
1661 chp->ch_flags |= WDCF_IRQ_WAIT; /* wait for interrupt */
1662 callout_reset(&chp->ch_callout, wdc_c->timeout / 1000 * hz,
1663 wdctimeout, chp);
1664 return;
1665 }
1666 /*
1667 * Polled command. Wait for drive ready or drq. Done in intr().
1668 * Wait for at last 400ns for status bit to be valid.
1669 */
1670 delay(10); /* 400ns delay */
1671 __wdccommand_intr(chp, xfer, 0);
1672 }
1673
1674 static int
1675 __wdccommand_intr(struct wdc_channel *chp, struct ata_xfer *xfer, int irq)
1676 {
1677 struct wdc_softc *wdc = chp->ch_wdc;
1678 struct wdc_command *wdc_c = xfer->c_cmd;
1679 int bcount = wdc_c->bcount;
1680 char *data = wdc_c->data;
1681 int wflags;
1682
1683 if ((wdc_c->flags & (AT_WAIT | AT_POLL)) == (AT_WAIT | AT_POLL)) {
1684 /* both wait and poll, we can tsleep here */
1685 wflags = AT_WAIT | AT_POLL;
1686 } else {
1687 wflags = AT_POLL;
1688 }
1689
1690 again:
1691 WDCDEBUG_PRINT(("__wdccommand_intr %s:%d:%d\n",
1692 wdc->sc_dev.dv_xname, chp->ch_channel, xfer->c_drive),
1693 DEBUG_INTR);
1694 /*
1695 * after a ATAPI_SOFT_RESET, the device will have released the bus.
1696 * Reselect again, it doesn't hurt for others commands, and the time
1697 * penalty for the extra regiter write is acceptable,
1698 * wdc_exec_command() isn't called often (mosly for autoconfig)
1699 */
1700 bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_sdh], 0,
1701 WDSD_IBM | (xfer->c_drive << 4));
1702 if ((wdc_c->flags & AT_XFDONE) != 0) {
1703 /*
1704 * We have completed a data xfer. The drive should now be
1705 * in its initial state
1706 */
1707 if (wdcwait(chp, wdc_c->r_st_bmask | WDCS_DRQ,
1708 wdc_c->r_st_bmask, (irq == 0) ? wdc_c->timeout : 0,
1709 wflags) == WDCWAIT_TOUT) {
1710 if (irq && (xfer->c_flags & C_TIMEOU) == 0)
1711 return 0; /* IRQ was not for us */
1712 wdc_c->flags |= AT_TIMEOU;
1713 }
1714 goto out;
1715 }
1716 if (wdcwait(chp, wdc_c->r_st_pmask, wdc_c->r_st_pmask,
1717 (irq == 0) ? wdc_c->timeout : 0, wflags) == WDCWAIT_TOUT) {
1718 if (irq && (xfer->c_flags & C_TIMEOU) == 0)
1719 return 0; /* IRQ was not for us */
1720 wdc_c->flags |= AT_TIMEOU;
1721 goto out;
1722 }
1723 if (wdc->cap & WDC_CAPABILITY_IRQACK)
1724 wdc->irqack(chp);
1725 if (wdc_c->flags & AT_READ) {
1726 if ((chp->ch_status & WDCS_DRQ) == 0) {
1727 wdc_c->flags |= AT_TIMEOU;
1728 goto out;
1729 }
1730 if (chp->ch_drive[xfer->c_drive].drive_flags & DRIVE_CAP32) {
1731 bus_space_read_multi_4(chp->data32iot, chp->data32ioh,
1732 0, (u_int32_t*)data, bcount >> 2);
1733 data += bcount & 0xfffffffc;
1734 bcount = bcount & 0x03;
1735 }
1736 if (bcount > 0)
1737 bus_space_read_multi_2(chp->cmd_iot,
1738 chp->cmd_iohs[wd_data], 0,
1739 (u_int16_t *)data, bcount >> 1);
1740 /* at this point the drive should be in its initial state */
1741 wdc_c->flags |= AT_XFDONE;
1742 /* XXX should read status register here ? */
1743 } else if (wdc_c->flags & AT_WRITE) {
1744 if ((chp->ch_status & WDCS_DRQ) == 0) {
1745 wdc_c->flags |= AT_TIMEOU;
1746 goto out;
1747 }
1748 if (chp->ch_drive[xfer->c_drive].drive_flags & DRIVE_CAP32) {
1749 bus_space_write_multi_4(chp->data32iot, chp->data32ioh,
1750 0, (u_int32_t*)data, bcount >> 2);
1751 data += bcount & 0xfffffffc;
1752 bcount = bcount & 0x03;
1753 }
1754 if (bcount > 0)
1755 bus_space_write_multi_2(chp->cmd_iot,
1756 chp->cmd_iohs[wd_data], 0,
1757 (u_int16_t *)data, bcount >> 1);
1758 wdc_c->flags |= AT_XFDONE;
1759 if ((wdc_c->flags & AT_POLL) == 0) {
1760 chp->ch_flags |= WDCF_IRQ_WAIT; /* wait for interrupt */
1761 callout_reset(&chp->ch_callout,
1762 wdc_c->timeout / 1000 * hz, wdctimeout, chp);
1763 return 1;
1764 } else {
1765 goto again;
1766 }
1767 }
1768 out:
1769 __wdccommand_done(chp, xfer);
1770 return 1;
1771 }
1772
1773 static void
1774 __wdccommand_done(struct wdc_channel *chp, struct ata_xfer *xfer)
1775 {
1776 struct wdc_softc *wdc = chp->ch_wdc;
1777 struct wdc_command *wdc_c = xfer->c_cmd;
1778
1779 WDCDEBUG_PRINT(("__wdccommand_done %s:%d:%d\n",
1780 wdc->sc_dev.dv_xname, chp->ch_channel, xfer->c_drive),
1781 DEBUG_FUNCS);
1782
1783 callout_stop(&chp->ch_callout);
1784
1785 if (chp->ch_status & WDCS_DWF)
1786 wdc_c->flags |= AT_DF;
1787 if (chp->ch_status & WDCS_ERR) {
1788 wdc_c->flags |= AT_ERROR;
1789 wdc_c->r_error = chp->ch_error;
1790 }
1791 wdc_c->flags |= AT_DONE;
1792 if ((wdc_c->flags & AT_READREG) != 0 &&
1793 (wdc->sc_dev.dv_flags & DVF_ACTIVE) != 0 &&
1794 (wdc_c->flags & (AT_ERROR | AT_DF)) == 0) {
1795 wdc_c->r_head = bus_space_read_1(chp->cmd_iot,
1796 chp->cmd_iohs[wd_sdh], 0);
1797 wdc_c->r_count = bus_space_read_1(chp->cmd_iot,
1798 chp->cmd_iohs[wd_seccnt], 0);
1799 wdc_c->r_sector = bus_space_read_1(chp->cmd_iot,
1800 chp->cmd_iohs[wd_sector], 0);
1801 wdc_c->r_cyl |= bus_space_read_1(chp->cmd_iot,
1802 chp->cmd_iohs[wd_cyl_lo], 0);
1803 wdc_c->r_cyl = bus_space_read_1(chp->cmd_iot,
1804 chp->cmd_iohs[wd_cyl_hi], 0) << 8;
1805 wdc_c->r_error = bus_space_read_1(chp->cmd_iot,
1806 chp->cmd_iohs[wd_error], 0);
1807 wdc_c->r_features = bus_space_read_1(chp->cmd_iot,
1808 chp->cmd_iohs[wd_features], 0);
1809 }
1810
1811 if (wdc_c->flags & AT_POLL) {
1812 /* enable interrupts */
1813 bus_space_write_1(chp->ctl_iot, chp->ctl_ioh, wd_aux_ctlr,
1814 WDCTL_4BIT);
1815 delay(10); /* some drives need a little delay here */
1816 }
1817 wdc_free_xfer(chp, xfer);
1818 if (wdc_c->flags & AT_WAIT)
1819 wakeup(wdc_c);
1820 else if (wdc_c->callback)
1821 wdc_c->callback(wdc_c->callback_arg);
1822 wdcstart(chp);
1823 return;
1824 }
1825
1826 /*
1827 * Send a command. The drive should be ready.
1828 * Assumes interrupts are blocked.
1829 */
1830 void
1831 wdccommand(struct wdc_channel *chp, u_int8_t drive, u_int8_t command,
1832 u_int16_t cylin, u_int8_t head, u_int8_t sector, u_int8_t count,
1833 u_int8_t features)
1834 {
1835 struct wdc_softc *wdc = chp->ch_wdc;
1836
1837 WDCDEBUG_PRINT(("wdccommand %s:%d:%d: command=0x%x cylin=%d head=%d "
1838 "sector=%d count=%d features=%d\n", wdc->sc_dev.dv_xname,
1839 chp->ch_channel, drive, command, cylin, head, sector, count,
1840 features), DEBUG_FUNCS);
1841
1842 if (wdc->cap & WDC_CAPABILITY_SELECT)
1843 wdc->select(chp,drive);
1844
1845 /* Select drive, head, and addressing mode. */
1846 bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_sdh], 0,
1847 WDSD_IBM | (drive << 4) | head);
1848 /* Load parameters into the wd_features register. */
1849 bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_features], 0,
1850 features);
1851 bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_seccnt], 0, count);
1852 bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_sector], 0, sector);
1853 bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_cyl_lo], 0, cylin);
1854 bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_cyl_hi],
1855 0, cylin >> 8);
1856
1857 /* Send command. */
1858 bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_command], 0, command);
1859 return;
1860 }
1861
1862 /*
1863 * Send a 48-bit addressing command. The drive should be ready.
1864 * Assumes interrupts are blocked.
1865 */
1866 void
1867 wdccommandext(struct wdc_channel *chp, u_int8_t drive, u_int8_t command,
1868 u_int64_t blkno, u_int16_t count)
1869 {
1870 struct wdc_softc *wdc = chp->ch_wdc;
1871
1872 WDCDEBUG_PRINT(("wdccommandext %s:%d:%d: command=0x%x blkno=%d "
1873 "count=%d\n", wdc->sc_dev.dv_xname,
1874 chp->ch_channel, drive, command, (u_int32_t) blkno, count),
1875 DEBUG_FUNCS);
1876
1877 if (wdc->cap & WDC_CAPABILITY_SELECT)
1878 wdc->select(chp,drive);
1879
1880 /* Select drive, head, and addressing mode. */
1881 bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_sdh], 0,
1882 (drive << 4) | WDSD_LBA);
1883
1884 /* previous */
1885 bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_features], 0, 0);
1886 bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_seccnt],
1887 0, count >> 8);
1888 bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_lba_lo],
1889 0, blkno >> 24);
1890 bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_lba_mi],
1891 0, blkno >> 32);
1892 bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_lba_hi],
1893 0, blkno >> 40);
1894
1895 /* current */
1896 bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_features], 0, 0);
1897 bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_seccnt], 0, count);
1898 bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_lba_lo], 0, blkno);
1899 bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_lba_mi],
1900 0, blkno >> 8);
1901 bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_lba_hi],
1902 0, blkno >> 16);
1903
1904 /* Send command. */
1905 bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_command], 0, command);
1906 return;
1907 }
1908
1909 /*
1910 * Simplified version of wdccommand(). Unbusy/ready/drq must be
1911 * tested by the caller.
1912 */
1913 void
1914 wdccommandshort(struct wdc_channel *chp, int drive, int command)
1915 {
1916 struct wdc_softc *wdc = chp->ch_wdc;
1917
1918 WDCDEBUG_PRINT(("wdccommandshort %s:%d:%d command 0x%x\n",
1919 wdc->sc_dev.dv_xname, chp->ch_channel, drive, command),
1920 DEBUG_FUNCS);
1921
1922 if (wdc->cap & WDC_CAPABILITY_SELECT)
1923 wdc->select(chp,drive);
1924
1925 /* Select drive. */
1926 bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_sdh], 0,
1927 WDSD_IBM | (drive << 4));
1928
1929 bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_command], 0, command);
1930 }
1931
1932 /* Add a command to the queue and start controller. Must be called at splbio */
1933 void
1934 wdc_exec_xfer(struct wdc_channel *chp, struct ata_xfer *xfer)
1935 {
1936
1937 WDCDEBUG_PRINT(("wdc_exec_xfer %p channel %d drive %d\n", xfer,
1938 chp->ch_channel, xfer->c_drive), DEBUG_XFERS);
1939
1940 /* complete xfer setup */
1941 xfer->c_chp = chp;
1942
1943 /*
1944 * If we are a polled command, and the list is not empty,
1945 * we are doing a dump. Drop the list to allow the polled command
1946 * to complete, we're going to reboot soon anyway.
1947 */
1948 if ((xfer->c_flags & C_POLL) != 0 &&
1949 TAILQ_FIRST(&chp->ch_queue->queue_xfer) != NULL) {
1950 TAILQ_INIT(&chp->ch_queue->queue_xfer);
1951 }
1952 /* insert at the end of command list */
1953 TAILQ_INSERT_TAIL(&chp->ch_queue->queue_xfer, xfer, c_xferchain);
1954 WDCDEBUG_PRINT(("wdcstart from wdc_exec_xfer, flags 0x%x\n",
1955 chp->ch_flags), DEBUG_XFERS);
1956 wdcstart(chp);
1957 }
1958
1959 struct ata_xfer *
1960 wdc_get_xfer(int flags)
1961 {
1962 struct ata_xfer *xfer;
1963 int s;
1964
1965 s = splbio();
1966 xfer = pool_get(&wdc_xfer_pool,
1967 ((flags & WDC_NOSLEEP) != 0 ? PR_NOWAIT : PR_WAITOK));
1968 splx(s);
1969 if (xfer != NULL) {
1970 memset(xfer, 0, sizeof(struct ata_xfer));
1971 }
1972 return xfer;
1973 }
1974
1975 void
1976 wdc_free_xfer(struct wdc_channel *chp, struct ata_xfer *xfer)
1977 {
1978 struct wdc_softc *wdc = chp->ch_wdc;
1979 int s;
1980
1981 if (wdc->cap & WDC_CAPABILITY_HWLOCK)
1982 (*wdc->free_hw)(chp);
1983 s = splbio();
1984 chp->ch_flags &= ~WDCF_ACTIVE;
1985 TAILQ_REMOVE(&chp->ch_queue->queue_xfer, xfer, c_xferchain);
1986 pool_put(&wdc_xfer_pool, xfer);
1987 splx(s);
1988 }
1989
1990 /*
1991 * Kill off all pending xfers for a wdc_channel.
1992 *
1993 * Must be called at splbio().
1994 */
1995 void
1996 wdc_kill_pending(struct wdc_channel *chp)
1997 {
1998 struct ata_xfer *xfer;
1999
2000 while ((xfer = TAILQ_FIRST(&chp->ch_queue->queue_xfer)) != NULL) {
2001 chp = xfer->c_chp;
2002 (*xfer->c_kill_xfer)(chp, xfer);
2003 }
2004 }
2005
2006 static void
2007 __wdcerror(struct wdc_channel *chp, char *msg)
2008 {
2009 struct wdc_softc *wdc = chp->ch_wdc;
2010 struct ata_xfer *xfer = TAILQ_FIRST(&chp->ch_queue->queue_xfer);
2011
2012 if (xfer == NULL)
2013 printf("%s:%d: %s\n", wdc->sc_dev.dv_xname, chp->ch_channel,
2014 msg);
2015 else
2016 printf("%s:%d:%d: %s\n", wdc->sc_dev.dv_xname,
2017 chp->ch_channel, xfer->c_drive, msg);
2018 }
2019
2020 /*
2021 * the bit bucket
2022 */
2023 void
2024 wdcbit_bucket(struct wdc_channel *chp, int size)
2025 {
2026
2027 for (; size >= 2; size -= 2)
2028 (void)bus_space_read_2(chp->cmd_iot, chp->cmd_iohs[wd_data], 0);
2029 if (size)
2030 (void)bus_space_read_1(chp->cmd_iot, chp->cmd_iohs[wd_data], 0);
2031 }
2032
2033 int
2034 wdc_addref(struct wdc_channel *chp)
2035 {
2036 struct wdc_softc *wdc = chp->ch_wdc;
2037 struct scsipi_adapter *adapt = &wdc->sc_atapi_adapter._generic;
2038 int s, error = 0;
2039
2040 s = splbio();
2041 if (adapt->adapt_refcnt++ == 0 &&
2042 adapt->adapt_enable != NULL) {
2043 error = (*adapt->adapt_enable)(&wdc->sc_dev, 1);
2044 if (error)
2045 adapt->adapt_refcnt--;
2046 }
2047 splx(s);
2048 return (error);
2049 }
2050
2051 void
2052 wdc_delref(struct wdc_channel *chp)
2053 {
2054 struct wdc_softc *wdc = chp->ch_wdc;
2055 struct scsipi_adapter *adapt = &wdc->sc_atapi_adapter._generic;
2056 int s;
2057
2058 s = splbio();
2059 if (adapt->adapt_refcnt-- == 1 &&
2060 adapt->adapt_enable != NULL)
2061 (void) (*adapt->adapt_enable)(&wdc->sc_dev, 0);
2062 splx(s);
2063 }
2064
2065 void
2066 wdc_print_modes(struct wdc_channel *chp)
2067 {
2068 struct wdc_softc *wdc = chp->ch_wdc;
2069 int drive;
2070 struct ata_drive_datas *drvp;
2071
2072 for (drive = 0; drive < 2; drive++) {
2073 drvp = &chp->ch_drive[drive];
2074 if ((drvp->drive_flags & DRIVE) == 0)
2075 continue;
2076 aprint_normal("%s(%s:%d:%d): using PIO mode %d",
2077 drvp->drv_softc->dv_xname,
2078 wdc->sc_dev.dv_xname,
2079 chp->ch_channel, drive, drvp->PIO_mode);
2080 if (drvp->drive_flags & DRIVE_DMA)
2081 aprint_normal(", DMA mode %d", drvp->DMA_mode);
2082 if (drvp->drive_flags & DRIVE_UDMA) {
2083 aprint_normal(", Ultra-DMA mode %d", drvp->UDMA_mode);
2084 if (drvp->UDMA_mode == 2)
2085 aprint_normal(" (Ultra/33)");
2086 else if (drvp->UDMA_mode == 4)
2087 aprint_normal(" (Ultra/66)");
2088 else if (drvp->UDMA_mode == 5)
2089 aprint_normal(" (Ultra/100)");
2090 else if (drvp->UDMA_mode == 6)
2091 aprint_normal(" (Ultra/133)");
2092 }
2093 if (drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA))
2094 aprint_normal(" (using DMA data transfers)");
2095 aprint_normal("\n");
2096 }
2097 }
2098