ata.c revision 1.38 1 /* $NetBSD: ata.c,v 1.38 2004/08/12 20:59:27 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.38 2004/08/12 20:59:27 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 wdc_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 /* management of the /dev/atabus* devices */
535 int atabusopen(dev, flag, fmt, p)
536 dev_t dev;
537 int flag, fmt;
538 struct proc *p;
539 {
540 struct atabus_softc *sc;
541 int error, unit = minor(dev);
542
543 if (unit >= atabus_cd.cd_ndevs ||
544 (sc = atabus_cd.cd_devs[unit]) == NULL)
545 return (ENXIO);
546
547 if (sc->sc_flags & ATABUSCF_OPEN)
548 return (EBUSY);
549
550 if ((error = wdc_addref(sc->sc_chan)) != 0)
551 return (error);
552
553 sc->sc_flags |= ATABUSCF_OPEN;
554
555 return (0);
556 }
557
558
559 int
560 atabusclose(dev, flag, fmt, p)
561 dev_t dev;
562 int flag, fmt;
563 struct proc *p;
564 {
565 struct atabus_softc *sc = atabus_cd.cd_devs[minor(dev)];
566
567 wdc_delref(sc->sc_chan);
568
569 sc->sc_flags &= ~ATABUSCF_OPEN;
570
571 return (0);
572 }
573
574 int
575 atabusioctl(dev, cmd, addr, flag, p)
576 dev_t dev;
577 u_long cmd;
578 caddr_t addr;
579 int flag;
580 struct proc *p;
581 {
582 struct atabus_softc *sc = atabus_cd.cd_devs[minor(dev)];
583 struct wdc_channel *chp = sc->sc_chan;
584 int min_drive, max_drive, drive;
585 int error;
586 int s;
587
588 /*
589 * Enforce write permission for ioctls that change the
590 * state of the bus. Host adapter specific ioctls must
591 * be checked by the adapter driver.
592 */
593 switch (cmd) {
594 case ATABUSIOSCAN:
595 case ATABUSIODETACH:
596 case ATABUSIORESET:
597 if ((flag & FWRITE) == 0)
598 return (EBADF);
599 }
600
601 switch (cmd) {
602 case ATABUSIORESET:
603 s = splbio();
604 wdc_reset_channel(sc->sc_chan, AT_WAIT | AT_POLL);
605 splx(s);
606 error = 0;
607 break;
608 case ATABUSIOSCAN:
609 {
610 #if 0
611 struct atabusioscan_args *a=
612 (struct atabusioscan_args *)addr;
613 #endif
614 if ((chp->ch_drive[0].drive_flags & DRIVE_OLD) ||
615 (chp->ch_drive[1].drive_flags & DRIVE_OLD))
616 return (EOPNOTSUPP);
617 return (EOPNOTSUPP);
618 }
619 case ATABUSIODETACH:
620 {
621 struct atabusioscan_args *a=
622 (struct atabusioscan_args *)addr;
623 if ((chp->ch_drive[0].drive_flags & DRIVE_OLD) ||
624 (chp->ch_drive[1].drive_flags & DRIVE_OLD))
625 return (EOPNOTSUPP);
626 switch (a->at_dev) {
627 case -1:
628 min_drive = 0;
629 max_drive = 1;
630 break;
631 case 0:
632 case 1:
633 min_drive = max_drive = a->at_dev;
634 break;
635 default:
636 return (EINVAL);
637 }
638 for (drive = min_drive; drive <= max_drive; drive++) {
639 if (chp->ch_drive[drive].drv_softc != NULL) {
640 error = config_detach(
641 chp->ch_drive[drive].drv_softc, 0);
642 if (error)
643 return (error);
644 chp->ch_drive[drive].drv_softc = NULL;
645 }
646 }
647 error = 0;
648 break;
649 }
650 default:
651 error = ENOTTY;
652 }
653 return (error);
654 };
655