ata.c revision 1.40 1 /* $NetBSD: ata.c,v 1.40 2004/08/12 21:10:18 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1998, 2001 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 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: ata.c,v 1.40 2004/08/12 21:10:18 thorpej Exp $");
34
35 #ifndef WDCDEBUG
36 #define WDCDEBUG
37 #endif /* WDCDEBUG */
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/device.h>
44 #include <sys/conf.h>
45 #include <sys/fcntl.h>
46 #include <sys/proc.h>
47 #include <sys/kthread.h>
48 #include <sys/errno.h>
49 #include <sys/ataio.h>
50
51 #include <machine/intr.h>
52 #include <machine/bus.h>
53
54 #include <dev/ata/atareg.h>
55 #include <dev/ata/atavar.h>
56 #include <dev/ic/wdcreg.h>
57 #include <dev/ic/wdcvar.h>
58
59 #include "locators.h"
60
61 #define DEBUG_FUNCS 0x08
62 #define DEBUG_PROBE 0x10
63 #define DEBUG_DETACH 0x20
64 #ifdef WDCDEBUG
65 extern int wdcdebug_mask; /* init'ed in wdc.c */
66 #define WDCDEBUG_PRINT(args, level) \
67 if (wdcdebug_mask & (level)) \
68 printf args
69 #else
70 #define WDCDEBUG_PRINT(args, level)
71 #endif
72
73 /*****************************************************************************
74 * ATA bus layer.
75 *
76 * ATA controllers attach an atabus instance, which handles probing the bus
77 * for drives, etc.
78 *****************************************************************************/
79
80 dev_type_open(atabusopen);
81 dev_type_close(atabusclose);
82 dev_type_ioctl(atabusioctl);
83
84 const struct cdevsw atabus_cdevsw = {
85 atabusopen, atabusclose, noread, nowrite, atabusioctl,
86 nostop, notty, nopoll, nommap, nokqfilter,
87 };
88
89 extern struct cfdriver atabus_cd;
90
91
92 /*
93 * atabusprint:
94 *
95 * Autoconfiguration print routine used by ATA controllers when
96 * attaching an atabus instance.
97 */
98 int
99 atabusprint(void *aux, const char *pnp)
100 {
101 struct wdc_channel *chan = aux;
102
103 if (pnp)
104 aprint_normal("atabus at %s", pnp);
105 aprint_normal(" channel %d", chan->ch_channel);
106
107 return (UNCONF);
108 }
109
110 /*
111 * ataprint:
112 *
113 * Autoconfiguration print routine.
114 */
115 int
116 ataprint(void *aux, const char *pnp)
117 {
118 struct ata_device *adev = aux;
119
120 if (pnp)
121 aprint_normal("wd at %s", pnp);
122 aprint_normal(" drive %d", adev->adev_drv_data->drive);
123
124 return (UNCONF);
125 }
126
127 /*
128 * atabus_thread:
129 *
130 * Worker thread for the ATA bus.
131 */
132 static void
133 atabus_thread(void *arg)
134 {
135 struct atabus_softc *sc = arg;
136 struct wdc_channel *chp = sc->sc_chan;
137 struct ata_xfer *xfer;
138 int s;
139
140 s = splbio();
141 chp->ch_flags |= WDCF_TH_RUN;
142 splx(s);
143
144 /* Configure the devices on the bus. */
145 atabusconfig(sc);
146
147 for (;;) {
148 s = splbio();
149 if ((chp->ch_flags & (WDCF_TH_RESET | WDCF_SHUTDOWN)) == 0 &&
150 (chp->ch_queue->active_xfer == NULL ||
151 chp->ch_queue->queue_freeze == 0)) {
152 chp->ch_flags &= ~WDCF_TH_RUN;
153 (void) tsleep(&chp->ch_thread, PRIBIO, "atath", 0);
154 chp->ch_flags |= WDCF_TH_RUN;
155 }
156 splx(s);
157 if (chp->ch_flags & WDCF_SHUTDOWN)
158 break;
159 s = splbio();
160 if (chp->ch_flags & WDCF_TH_RESET) {
161 /*
162 * wdc_reset_channel() will freeze 2 times, so
163 * unfreeze one time. Not a problem as we're at splbio
164 */
165 chp->ch_queue->queue_freeze--;
166 wdc_reset_channel(chp, AT_WAIT | chp->ch_reset_flags);
167 } else if (chp->ch_queue->active_xfer != NULL &&
168 chp->ch_queue->queue_freeze == 1) {
169 /*
170 * Caller has bumped queue_freeze, decrease it.
171 */
172 chp->ch_queue->queue_freeze--;
173 xfer = chp->ch_queue->active_xfer;
174 KASSERT(xfer != NULL);
175 (*xfer->c_start)(chp, xfer);
176 } else if (chp->ch_queue->queue_freeze > 1)
177 panic("ata_thread: queue_freeze");
178 splx(s);
179 }
180 chp->ch_thread = NULL;
181 wakeup((void *)&chp->ch_flags);
182 kthread_exit(0);
183 }
184
185 /*
186 * atabus_create_thread:
187 *
188 * Helper routine to create the ATA bus worker thread.
189 */
190 static void
191 atabus_create_thread(void *arg)
192 {
193 struct atabus_softc *sc = arg;
194 struct wdc_channel *chp = sc->sc_chan;
195 int error;
196
197 if ((error = kthread_create1(atabus_thread, sc, &chp->ch_thread,
198 "%s", sc->sc_dev.dv_xname)) != 0)
199 aprint_error("%s: unable to create kernel thread: error %d\n",
200 sc->sc_dev.dv_xname, error);
201 }
202
203 /*
204 * atabus_match:
205 *
206 * Autoconfiguration match routine.
207 */
208 static int
209 atabus_match(struct device *parent, struct cfdata *cf, void *aux)
210 {
211 struct wdc_channel *chp = aux;
212
213 if (chp == NULL)
214 return (0);
215
216 if (cf->cf_loc[ATACF_CHANNEL] != chp->ch_channel &&
217 cf->cf_loc[ATACF_CHANNEL] != ATACF_CHANNEL_DEFAULT)
218 return (0);
219
220 return (1);
221 }
222
223 /*
224 * atabus_attach:
225 *
226 * Autoconfiguration attach routine.
227 */
228 static void
229 atabus_attach(struct device *parent, struct device *self, void *aux)
230 {
231 struct atabus_softc *sc = (void *) self;
232 struct wdc_channel *chp = aux;
233 struct atabus_initq *initq;
234
235 sc->sc_chan = chp;
236
237 aprint_normal("\n");
238 aprint_naive("\n");
239
240 if (wdc_addref(chp))
241 return;
242
243 initq = malloc(sizeof(*initq), M_DEVBUF, M_WAITOK);
244 initq->atabus_sc = sc;
245 TAILQ_INSERT_TAIL(&atabus_initq_head, initq, atabus_initq);
246 config_pending_incr();
247 kthread_create(atabus_create_thread, sc);
248 }
249
250 /*
251 * atabus_activate:
252 *
253 * Autoconfiguration activation routine.
254 */
255 static int
256 atabus_activate(struct device *self, enum devact act)
257 {
258 struct atabus_softc *sc = (void *) self;
259 struct wdc_channel *chp = sc->sc_chan;
260 struct device *dev = NULL;
261 int s, i, error = 0;
262
263 s = splbio();
264 switch (act) {
265 case DVACT_ACTIVATE:
266 error = EOPNOTSUPP;
267 break;
268
269 case DVACT_DEACTIVATE:
270 /*
271 * We might deactivate the children of atapibus twice
272 * (once bia atapibus, once directly), but since the
273 * generic autoconfiguration code maintains the DVF_ACTIVE
274 * flag, it's safe.
275 */
276 if ((dev = chp->atapibus) != NULL) {
277 error = config_deactivate(dev);
278 if (error)
279 goto out;
280 }
281
282 for (i = 0; i < 2; i++) {
283 if ((dev = chp->ch_drive[i].drv_softc) != NULL) {
284 WDCDEBUG_PRINT(("atabus_activate: %s: "
285 "deactivating %s\n", sc->sc_dev.dv_xname,
286 dev->dv_xname),
287 DEBUG_DETACH);
288 error = config_deactivate(dev);
289 if (error)
290 goto out;
291 }
292 }
293 break;
294 }
295 out:
296 splx(s);
297
298 #ifdef WDCDEBUG
299 if (dev != NULL && error != 0)
300 WDCDEBUG_PRINT(("atabus_activate: %s: "
301 "error %d deactivating %s\n", sc->sc_dev.dv_xname,
302 error, dev->dv_xname), DEBUG_DETACH);
303 #endif /* WDCDEBUG */
304
305 return (error);
306 }
307
308 /*
309 * atabus_detach:
310 *
311 * Autoconfiguration detach routine.
312 */
313 static int
314 atabus_detach(struct device *self, int flags)
315 {
316 struct atabus_softc *sc = (void *) self;
317 struct wdc_channel *chp = sc->sc_chan;
318 struct device *dev = NULL;
319 int i, error = 0;
320
321 /* Shutdown the channel. */
322 /* XXX NEED AN INTERLOCK HERE. */
323 chp->ch_flags |= WDCF_SHUTDOWN;
324 wakeup(&chp->ch_thread);
325 while (chp->ch_thread != NULL)
326 (void) tsleep((void *)&chp->ch_flags, PRIBIO, "atadown", 0);
327
328 /*
329 * Detach atapibus and its children.
330 */
331 if ((dev = chp->atapibus) != NULL) {
332 WDCDEBUG_PRINT(("atabus_detach: %s: detaching %s\n",
333 sc->sc_dev.dv_xname, dev->dv_xname), DEBUG_DETACH);
334 error = config_detach(dev, flags);
335 if (error)
336 goto out;
337 }
338
339 /*
340 * Detach our other children.
341 */
342 for (i = 0; i < 2; i++) {
343 if (chp->ch_drive[i].drive_flags & DRIVE_ATAPI)
344 continue;
345 if ((dev = chp->ch_drive[i].drv_softc) != NULL) {
346 WDCDEBUG_PRINT(("atabus_detach: %s: detaching %s\n",
347 sc->sc_dev.dv_xname, dev->dv_xname),
348 DEBUG_DETACH);
349 error = config_detach(dev, flags);
350 if (error)
351 goto out;
352 }
353 }
354
355 out:
356 #ifdef WDCDEBUG
357 if (dev != NULL && error != 0)
358 WDCDEBUG_PRINT(("atabus_detach: %s: error %d detaching %s\n",
359 sc->sc_dev.dv_xname, error, dev->dv_xname),
360 DEBUG_DETACH);
361 #endif /* WDCDEBUG */
362
363 return (error);
364 }
365
366 CFATTACH_DECL(atabus, sizeof(struct atabus_softc),
367 atabus_match, atabus_attach, atabus_detach, atabus_activate);
368
369 /*****************************************************************************
370 * Common ATA bus operations.
371 *****************************************************************************/
372
373 /* Get the disk's parameters */
374 int
375 ata_get_params(struct ata_drive_datas *drvp, u_int8_t flags,
376 struct ataparams *prms)
377 {
378 char tb[DEV_BSIZE];
379 struct ata_command ata_c;
380
381 #if BYTE_ORDER == LITTLE_ENDIAN
382 int i;
383 u_int16_t *p;
384 #endif
385
386 WDCDEBUG_PRINT(("ata_get_parms\n"), DEBUG_FUNCS);
387
388 memset(tb, 0, DEV_BSIZE);
389 memset(prms, 0, sizeof(struct ataparams));
390 memset(&ata_c, 0, sizeof(struct ata_command));
391
392 if (drvp->drive_flags & DRIVE_ATA) {
393 ata_c.r_command = WDCC_IDENTIFY;
394 ata_c.r_st_bmask = WDCS_DRDY;
395 ata_c.r_st_pmask = 0;
396 ata_c.timeout = 3000; /* 3s */
397 } else if (drvp->drive_flags & DRIVE_ATAPI) {
398 ata_c.r_command = ATAPI_IDENTIFY_DEVICE;
399 ata_c.r_st_bmask = 0;
400 ata_c.r_st_pmask = 0;
401 ata_c.timeout = 10000; /* 10s */
402 } else {
403 WDCDEBUG_PRINT(("ata_get_parms: no disks\n"),
404 DEBUG_FUNCS|DEBUG_PROBE);
405 return CMD_ERR;
406 }
407 ata_c.flags = AT_READ | flags;
408 ata_c.data = tb;
409 ata_c.bcount = DEV_BSIZE;
410 if (wdc_exec_command(drvp, &ata_c) != ATACMD_COMPLETE) {
411 WDCDEBUG_PRINT(("ata_get_parms: wdc_exec_command failed\n"),
412 DEBUG_FUNCS|DEBUG_PROBE);
413 return CMD_AGAIN;
414 }
415 if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
416 WDCDEBUG_PRINT(("ata_get_parms: ata_c.flags=0x%x\n",
417 ata_c.flags), DEBUG_FUNCS|DEBUG_PROBE);
418 return CMD_ERR;
419 } else {
420 /* if we didn't read any data something is wrong */
421 if ((ata_c.flags & AT_XFDONE) == 0)
422 return CMD_ERR;
423 /* Read in parameter block. */
424 memcpy(prms, tb, sizeof(struct ataparams));
425 #if BYTE_ORDER == LITTLE_ENDIAN
426 /*
427 * Shuffle string byte order.
428 * ATAPI Mitsumi and NEC drives don't need this.
429 */
430 if ((prms->atap_config & WDC_CFG_ATAPI_MASK) ==
431 WDC_CFG_ATAPI &&
432 ((prms->atap_model[0] == 'N' &&
433 prms->atap_model[1] == 'E') ||
434 (prms->atap_model[0] == 'F' &&
435 prms->atap_model[1] == 'X')))
436 return 0;
437 for (i = 0; i < sizeof(prms->atap_model); i += 2) {
438 p = (u_short *)(prms->atap_model + i);
439 *p = ntohs(*p);
440 }
441 for (i = 0; i < sizeof(prms->atap_serial); i += 2) {
442 p = (u_short *)(prms->atap_serial + i);
443 *p = ntohs(*p);
444 }
445 for (i = 0; i < sizeof(prms->atap_revision); i += 2) {
446 p = (u_short *)(prms->atap_revision + i);
447 *p = ntohs(*p);
448 }
449 #endif
450 return CMD_OK;
451 }
452 }
453
454 int
455 ata_set_mode(struct ata_drive_datas *drvp, u_int8_t mode, u_int8_t flags)
456 {
457 struct ata_command ata_c;
458
459 WDCDEBUG_PRINT(("ata_set_mode=0x%x\n", mode), DEBUG_FUNCS);
460 memset(&ata_c, 0, sizeof(struct ata_command));
461
462 ata_c.r_command = SET_FEATURES;
463 ata_c.r_st_bmask = 0;
464 ata_c.r_st_pmask = 0;
465 ata_c.r_features = WDSF_SET_MODE;
466 ata_c.r_count = mode;
467 ata_c.flags = flags;
468 ata_c.timeout = 1000; /* 1s */
469 if (wdc_exec_command(drvp, &ata_c) != ATACMD_COMPLETE)
470 return CMD_AGAIN;
471 if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
472 return CMD_ERR;
473 }
474 return CMD_OK;
475 }
476
477 void
478 ata_dmaerr(struct ata_drive_datas *drvp, int flags)
479 {
480 /*
481 * Downgrade decision: if we get NERRS_MAX in NXFER.
482 * We start with n_dmaerrs set to NERRS_MAX-1 so that the
483 * first error within the first NXFER ops will immediatly trigger
484 * a downgrade.
485 * If we got an error and n_xfers is bigger than NXFER reset counters.
486 */
487 drvp->n_dmaerrs++;
488 if (drvp->n_dmaerrs >= NERRS_MAX && drvp->n_xfers <= NXFER) {
489 ata_downgrade_mode(drvp, flags);
490 drvp->n_dmaerrs = NERRS_MAX-1;
491 drvp->n_xfers = 0;
492 return;
493 }
494 if (drvp->n_xfers > NXFER) {
495 drvp->n_dmaerrs = 1; /* just got an error */
496 drvp->n_xfers = 1; /* restart counting from this error */
497 }
498 }
499
500 void
501 ata_print_modes(struct wdc_channel *chp)
502 {
503 struct wdc_softc *wdc = chp->ch_wdc;
504 int drive;
505 struct ata_drive_datas *drvp;
506
507 for (drive = 0; drive < 2; drive++) {
508 drvp = &chp->ch_drive[drive];
509 if ((drvp->drive_flags & DRIVE) == 0)
510 continue;
511 aprint_normal("%s(%s:%d:%d): using PIO mode %d",
512 drvp->drv_softc->dv_xname,
513 wdc->sc_dev.dv_xname,
514 chp->ch_channel, drive, drvp->PIO_mode);
515 if (drvp->drive_flags & DRIVE_DMA)
516 aprint_normal(", DMA mode %d", drvp->DMA_mode);
517 if (drvp->drive_flags & DRIVE_UDMA) {
518 aprint_normal(", Ultra-DMA mode %d", drvp->UDMA_mode);
519 if (drvp->UDMA_mode == 2)
520 aprint_normal(" (Ultra/33)");
521 else if (drvp->UDMA_mode == 4)
522 aprint_normal(" (Ultra/66)");
523 else if (drvp->UDMA_mode == 5)
524 aprint_normal(" (Ultra/100)");
525 else if (drvp->UDMA_mode == 6)
526 aprint_normal(" (Ultra/133)");
527 }
528 if (drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA))
529 aprint_normal(" (using DMA data transfers)");
530 aprint_normal("\n");
531 }
532 }
533
534 /*
535 * downgrade the transfer mode of a drive after an error. return 1 if
536 * downgrade was possible, 0 otherwise.
537 */
538 int
539 ata_downgrade_mode(struct ata_drive_datas *drvp, int flags)
540 {
541 struct wdc_channel *chp = drvp->chnl_softc;
542 struct wdc_softc *wdc = chp->ch_wdc;
543 struct device *drv_dev = drvp->drv_softc;
544 int cf_flags = drv_dev->dv_cfdata->cf_flags;
545
546 /* if drive or controller don't know its mode, we can't do much */
547 if ((drvp->drive_flags & DRIVE_MODE) == 0 ||
548 (wdc->cap & WDC_CAPABILITY_MODE) == 0)
549 return 0;
550 /* current drive mode was set by a config flag, let it this way */
551 if ((cf_flags & ATA_CONFIG_PIO_SET) ||
552 (cf_flags & ATA_CONFIG_DMA_SET) ||
553 (cf_flags & ATA_CONFIG_UDMA_SET))
554 return 0;
555
556 /*
557 * If we were using Ultra-DMA mode, downgrade to the next lower mode.
558 */
559 if ((drvp->drive_flags & DRIVE_UDMA) && drvp->UDMA_mode >= 2) {
560 drvp->UDMA_mode--;
561 printf("%s: transfer error, downgrading to Ultra-DMA mode %d\n",
562 drv_dev->dv_xname, drvp->UDMA_mode);
563 }
564
565 /*
566 * If we were using ultra-DMA, don't downgrade to multiword DMA.
567 */
568 else if (drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA)) {
569 drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
570 drvp->PIO_mode = drvp->PIO_cap;
571 printf("%s: transfer error, downgrading to PIO mode %d\n",
572 drv_dev->dv_xname, drvp->PIO_mode);
573 } else /* already using PIO, can't downgrade */
574 return 0;
575
576 wdc->set_modes(chp);
577 ata_print_modes(chp);
578 /* reset the channel, which will shedule all drives for setup */
579 wdc_reset_channel(chp, flags | AT_RST_NOCMD);
580 return 1;
581 }
582
583 /*
584 * Probe drive's capabilities, for use by the controller later
585 * Assumes drvp points to an existing drive.
586 */
587 void
588 ata_probe_caps(struct ata_drive_datas *drvp)
589 {
590 struct ataparams params, params2;
591 struct wdc_channel *chp = drvp->chnl_softc;
592 struct wdc_softc *wdc = chp->ch_wdc;
593 struct device *drv_dev = drvp->drv_softc;
594 int i, printed;
595 char *sep = "";
596 int cf_flags;
597
598 if (ata_get_params(drvp, AT_WAIT, ¶ms) != CMD_OK) {
599 /* IDENTIFY failed. Can't tell more about the device */
600 return;
601 }
602 if ((wdc->cap & (WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32)) ==
603 (WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32)) {
604 /*
605 * Controller claims 16 and 32 bit transfers.
606 * Re-do an IDENTIFY with 32-bit transfers,
607 * and compare results.
608 */
609 drvp->drive_flags |= DRIVE_CAP32;
610 ata_get_params(drvp, AT_WAIT, ¶ms2);
611 if (memcmp(¶ms, ¶ms2, sizeof(struct ataparams)) != 0) {
612 /* Not good. fall back to 16bits */
613 drvp->drive_flags &= ~DRIVE_CAP32;
614 } else {
615 aprint_normal("%s: 32-bit data port\n",
616 drv_dev->dv_xname);
617 }
618 }
619 #if 0 /* Some ultra-DMA drives claims to only support ATA-3. sigh */
620 if (params.atap_ata_major > 0x01 &&
621 params.atap_ata_major != 0xffff) {
622 for (i = 14; i > 0; i--) {
623 if (params.atap_ata_major & (1 << i)) {
624 aprint_normal("%s: ATA version %d\n",
625 drv_dev->dv_xname, i);
626 drvp->ata_vers = i;
627 break;
628 }
629 }
630 }
631 #endif
632
633 /* An ATAPI device is at last PIO mode 3 */
634 if (drvp->drive_flags & DRIVE_ATAPI)
635 drvp->PIO_mode = 3;
636
637 /*
638 * It's not in the specs, but it seems that some drive
639 * returns 0xffff in atap_extensions when this field is invalid
640 */
641 if (params.atap_extensions != 0xffff &&
642 (params.atap_extensions & WDC_EXT_MODES)) {
643 printed = 0;
644 /*
645 * XXX some drives report something wrong here (they claim to
646 * support PIO mode 8 !). As mode is coded on 3 bits in
647 * SET FEATURE, limit it to 7 (so limit i to 4).
648 * If higher mode than 7 is found, abort.
649 */
650 for (i = 7; i >= 0; i--) {
651 if ((params.atap_piomode_supp & (1 << i)) == 0)
652 continue;
653 if (i > 4)
654 return;
655 /*
656 * See if mode is accepted.
657 * If the controller can't set its PIO mode,
658 * assume the defaults are good, so don't try
659 * to set it
660 */
661 if ((wdc->cap & WDC_CAPABILITY_MODE) != 0)
662 /*
663 * It's OK to pool here, it's fast enouth
664 * to not bother waiting for interrupt
665 */
666 if (ata_set_mode(drvp, 0x08 | (i + 3),
667 AT_WAIT) != CMD_OK)
668 continue;
669 if (!printed) {
670 aprint_normal("%s: drive supports PIO mode %d",
671 drv_dev->dv_xname, i + 3);
672 sep = ",";
673 printed = 1;
674 }
675 /*
676 * If controller's driver can't set its PIO mode,
677 * get the highter one for the drive.
678 */
679 if ((wdc->cap & WDC_CAPABILITY_MODE) == 0 ||
680 wdc->PIO_cap >= i + 3) {
681 drvp->PIO_mode = i + 3;
682 drvp->PIO_cap = i + 3;
683 break;
684 }
685 }
686 if (!printed) {
687 /*
688 * We didn't find a valid PIO mode.
689 * Assume the values returned for DMA are buggy too
690 */
691 return;
692 }
693 drvp->drive_flags |= DRIVE_MODE;
694 printed = 0;
695 for (i = 7; i >= 0; i--) {
696 if ((params.atap_dmamode_supp & (1 << i)) == 0)
697 continue;
698 if ((wdc->cap & WDC_CAPABILITY_DMA) &&
699 (wdc->cap & WDC_CAPABILITY_MODE))
700 if (ata_set_mode(drvp, 0x20 | i, AT_WAIT)
701 != CMD_OK)
702 continue;
703 if (!printed) {
704 aprint_normal("%s DMA mode %d", sep, i);
705 sep = ",";
706 printed = 1;
707 }
708 if (wdc->cap & WDC_CAPABILITY_DMA) {
709 if ((wdc->cap & WDC_CAPABILITY_MODE) &&
710 wdc->DMA_cap < i)
711 continue;
712 drvp->DMA_mode = i;
713 drvp->DMA_cap = i;
714 drvp->drive_flags |= DRIVE_DMA;
715 }
716 break;
717 }
718 if (params.atap_extensions & WDC_EXT_UDMA_MODES) {
719 printed = 0;
720 for (i = 7; i >= 0; i--) {
721 if ((params.atap_udmamode_supp & (1 << i))
722 == 0)
723 continue;
724 if ((wdc->cap & WDC_CAPABILITY_MODE) &&
725 (wdc->cap & WDC_CAPABILITY_UDMA))
726 if (ata_set_mode(drvp, 0x40 | i,
727 AT_WAIT) != CMD_OK)
728 continue;
729 if (!printed) {
730 aprint_normal("%s Ultra-DMA mode %d",
731 sep, i);
732 if (i == 2)
733 aprint_normal(" (Ultra/33)");
734 else if (i == 4)
735 aprint_normal(" (Ultra/66)");
736 else if (i == 5)
737 aprint_normal(" (Ultra/100)");
738 else if (i == 6)
739 aprint_normal(" (Ultra/133)");
740 sep = ",";
741 printed = 1;
742 }
743 if (wdc->cap & WDC_CAPABILITY_UDMA) {
744 if ((wdc->cap & WDC_CAPABILITY_MODE) &&
745 wdc->UDMA_cap < i)
746 continue;
747 drvp->UDMA_mode = i;
748 drvp->UDMA_cap = i;
749 drvp->drive_flags |= DRIVE_UDMA;
750 }
751 break;
752 }
753 }
754 aprint_normal("\n");
755 }
756
757 drvp->drive_flags &= ~DRIVE_NOSTREAM;
758 if (drvp->drive_flags & DRIVE_ATAPI) {
759 if (wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)
760 drvp->drive_flags |= DRIVE_NOSTREAM;
761 } else {
762 if (wdc->cap & WDC_CAPABILITY_ATA_NOSTREAM)
763 drvp->drive_flags |= DRIVE_NOSTREAM;
764 }
765
766 /* Try to guess ATA version here, if it didn't get reported */
767 if (drvp->ata_vers == 0) {
768 if (drvp->drive_flags & DRIVE_UDMA)
769 drvp->ata_vers = 4; /* should be at last ATA-4 */
770 else if (drvp->PIO_cap > 2)
771 drvp->ata_vers = 2; /* should be at last ATA-2 */
772 }
773 cf_flags = drv_dev->dv_cfdata->cf_flags;
774 if (cf_flags & ATA_CONFIG_PIO_SET) {
775 drvp->PIO_mode =
776 (cf_flags & ATA_CONFIG_PIO_MODES) >> ATA_CONFIG_PIO_OFF;
777 drvp->drive_flags |= DRIVE_MODE;
778 }
779 if ((wdc->cap & WDC_CAPABILITY_DMA) == 0) {
780 /* don't care about DMA modes */
781 return;
782 }
783 if (cf_flags & ATA_CONFIG_DMA_SET) {
784 if ((cf_flags & ATA_CONFIG_DMA_MODES) ==
785 ATA_CONFIG_DMA_DISABLE) {
786 drvp->drive_flags &= ~DRIVE_DMA;
787 } else {
788 drvp->DMA_mode = (cf_flags & ATA_CONFIG_DMA_MODES) >>
789 ATA_CONFIG_DMA_OFF;
790 drvp->drive_flags |= DRIVE_DMA | DRIVE_MODE;
791 }
792 }
793 if ((wdc->cap & WDC_CAPABILITY_UDMA) == 0) {
794 /* don't care about UDMA modes */
795 return;
796 }
797 if (cf_flags & ATA_CONFIG_UDMA_SET) {
798 if ((cf_flags & ATA_CONFIG_UDMA_MODES) ==
799 ATA_CONFIG_UDMA_DISABLE) {
800 drvp->drive_flags &= ~DRIVE_UDMA;
801 } else {
802 drvp->UDMA_mode = (cf_flags & ATA_CONFIG_UDMA_MODES) >>
803 ATA_CONFIG_UDMA_OFF;
804 drvp->drive_flags |= DRIVE_UDMA | DRIVE_MODE;
805 }
806 }
807 }
808
809 /* management of the /dev/atabus* devices */
810 int atabusopen(dev, flag, fmt, p)
811 dev_t dev;
812 int flag, fmt;
813 struct proc *p;
814 {
815 struct atabus_softc *sc;
816 int error, unit = minor(dev);
817
818 if (unit >= atabus_cd.cd_ndevs ||
819 (sc = atabus_cd.cd_devs[unit]) == NULL)
820 return (ENXIO);
821
822 if (sc->sc_flags & ATABUSCF_OPEN)
823 return (EBUSY);
824
825 if ((error = wdc_addref(sc->sc_chan)) != 0)
826 return (error);
827
828 sc->sc_flags |= ATABUSCF_OPEN;
829
830 return (0);
831 }
832
833
834 int
835 atabusclose(dev, flag, fmt, p)
836 dev_t dev;
837 int flag, fmt;
838 struct proc *p;
839 {
840 struct atabus_softc *sc = atabus_cd.cd_devs[minor(dev)];
841
842 wdc_delref(sc->sc_chan);
843
844 sc->sc_flags &= ~ATABUSCF_OPEN;
845
846 return (0);
847 }
848
849 int
850 atabusioctl(dev, cmd, addr, flag, p)
851 dev_t dev;
852 u_long cmd;
853 caddr_t addr;
854 int flag;
855 struct proc *p;
856 {
857 struct atabus_softc *sc = atabus_cd.cd_devs[minor(dev)];
858 struct wdc_channel *chp = sc->sc_chan;
859 int min_drive, max_drive, drive;
860 int error;
861 int s;
862
863 /*
864 * Enforce write permission for ioctls that change the
865 * state of the bus. Host adapter specific ioctls must
866 * be checked by the adapter driver.
867 */
868 switch (cmd) {
869 case ATABUSIOSCAN:
870 case ATABUSIODETACH:
871 case ATABUSIORESET:
872 if ((flag & FWRITE) == 0)
873 return (EBADF);
874 }
875
876 switch (cmd) {
877 case ATABUSIORESET:
878 s = splbio();
879 wdc_reset_channel(sc->sc_chan, AT_WAIT | AT_POLL);
880 splx(s);
881 error = 0;
882 break;
883 case ATABUSIOSCAN:
884 {
885 #if 0
886 struct atabusioscan_args *a=
887 (struct atabusioscan_args *)addr;
888 #endif
889 if ((chp->ch_drive[0].drive_flags & DRIVE_OLD) ||
890 (chp->ch_drive[1].drive_flags & DRIVE_OLD))
891 return (EOPNOTSUPP);
892 return (EOPNOTSUPP);
893 }
894 case ATABUSIODETACH:
895 {
896 struct atabusioscan_args *a=
897 (struct atabusioscan_args *)addr;
898 if ((chp->ch_drive[0].drive_flags & DRIVE_OLD) ||
899 (chp->ch_drive[1].drive_flags & DRIVE_OLD))
900 return (EOPNOTSUPP);
901 switch (a->at_dev) {
902 case -1:
903 min_drive = 0;
904 max_drive = 1;
905 break;
906 case 0:
907 case 1:
908 min_drive = max_drive = a->at_dev;
909 break;
910 default:
911 return (EINVAL);
912 }
913 for (drive = min_drive; drive <= max_drive; drive++) {
914 if (chp->ch_drive[drive].drv_softc != NULL) {
915 error = config_detach(
916 chp->ch_drive[drive].drv_softc, 0);
917 if (error)
918 return (error);
919 chp->ch_drive[drive].drv_softc = NULL;
920 }
921 }
922 error = 0;
923 break;
924 }
925 default:
926 error = ENOTTY;
927 }
928 return (error);
929 };
930