ata.c revision 1.35 1 /* $NetBSD: ata.c,v 1.35 2004/08/10 23:09:38 mycroft 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.35 2004/08/10 23:09:38 mycroft 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 wdc_command wdc_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(&wdc_c, 0, sizeof(struct wdc_command));
391
392 if (drvp->drive_flags & DRIVE_ATA) {
393 wdc_c.r_command = WDCC_IDENTIFY;
394 wdc_c.r_st_bmask = WDCS_DRDY;
395 wdc_c.r_st_pmask = 0;
396 wdc_c.timeout = 3000; /* 3s */
397 } else if (drvp->drive_flags & DRIVE_ATAPI) {
398 wdc_c.r_command = ATAPI_IDENTIFY_DEVICE;
399 wdc_c.r_st_bmask = 0;
400 wdc_c.r_st_pmask = 0;
401 wdc_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 wdc_c.flags = AT_READ | flags;
408 wdc_c.data = tb;
409 wdc_c.bcount = DEV_BSIZE;
410 if (wdc_exec_command(drvp, &wdc_c) != WDC_COMPLETE) {
411 WDCDEBUG_PRINT(("ata_get_parms: wdc_exec_command failed\n"),
412 DEBUG_FUNCS|DEBUG_PROBE);
413 return CMD_AGAIN;
414 }
415 if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
416 WDCDEBUG_PRINT(("ata_get_parms: wdc_c.flags=0x%x\n",
417 wdc_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 ((wdc_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 wdc_command wdc_c;
458
459 WDCDEBUG_PRINT(("ata_set_mode=0x%x\n", mode), DEBUG_FUNCS);
460 memset(&wdc_c, 0, sizeof(struct wdc_command));
461
462 wdc_c.r_command = SET_FEATURES;
463 wdc_c.r_st_bmask = 0;
464 wdc_c.r_st_pmask = 0;
465 wdc_c.r_features = WDSF_SET_MODE;
466 wdc_c.r_count = mode;
467 wdc_c.flags = flags;
468 wdc_c.timeout = 1000; /* 1s */
469 if (wdc_exec_command(drvp, &wdc_c) != WDC_COMPLETE)
470 return CMD_AGAIN;
471 if (wdc_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 /* management of the /dev/atabus* devices */
501 int atabusopen(dev, flag, fmt, p)
502 dev_t dev;
503 int flag, fmt;
504 struct proc *p;
505 {
506 struct atabus_softc *sc;
507 int error, unit = minor(dev);
508
509 if (unit >= atabus_cd.cd_ndevs ||
510 (sc = atabus_cd.cd_devs[unit]) == NULL)
511 return (ENXIO);
512
513 if (sc->sc_flags & ATABUSCF_OPEN)
514 return (EBUSY);
515
516 if ((error = wdc_addref(sc->sc_chan)) != 0)
517 return (error);
518
519 sc->sc_flags |= ATABUSCF_OPEN;
520
521 return (0);
522 }
523
524
525 int
526 atabusclose(dev, flag, fmt, p)
527 dev_t dev;
528 int flag, fmt;
529 struct proc *p;
530 {
531 struct atabus_softc *sc = atabus_cd.cd_devs[minor(dev)];
532
533 wdc_delref(sc->sc_chan);
534
535 sc->sc_flags &= ~ATABUSCF_OPEN;
536
537 return (0);
538 }
539
540 int
541 atabusioctl(dev, cmd, addr, flag, p)
542 dev_t dev;
543 u_long cmd;
544 caddr_t addr;
545 int flag;
546 struct proc *p;
547 {
548 struct atabus_softc *sc = atabus_cd.cd_devs[minor(dev)];
549 struct wdc_channel *chp = sc->sc_chan;
550 int min_drive, max_drive, drive;
551 int error;
552 int s;
553
554 /*
555 * Enforce write permission for ioctls that change the
556 * state of the bus. Host adapter specific ioctls must
557 * be checked by the adapter driver.
558 */
559 switch (cmd) {
560 case ATABUSIOSCAN:
561 case ATABUSIODETACH:
562 case ATABUSIORESET:
563 if ((flag & FWRITE) == 0)
564 return (EBADF);
565 }
566
567 switch (cmd) {
568 case ATABUSIORESET:
569 s = splbio();
570 wdc_reset_channel(sc->sc_chan, AT_WAIT | AT_POLL);
571 splx(s);
572 error = 0;
573 break;
574 case ATABUSIOSCAN:
575 {
576 #if 0
577 struct atabusioscan_args *a=
578 (struct atabusioscan_args *)addr;
579 #endif
580 if ((chp->ch_drive[0].drive_flags & DRIVE_OLD) ||
581 (chp->ch_drive[1].drive_flags & DRIVE_OLD))
582 return (EOPNOTSUPP);
583 return (EOPNOTSUPP);
584 }
585 case ATABUSIODETACH:
586 {
587 struct atabusioscan_args *a=
588 (struct atabusioscan_args *)addr;
589 if ((chp->ch_drive[0].drive_flags & DRIVE_OLD) ||
590 (chp->ch_drive[1].drive_flags & DRIVE_OLD))
591 return (EOPNOTSUPP);
592 switch (a->at_dev) {
593 case -1:
594 min_drive = 0;
595 max_drive = 1;
596 break;
597 case 0:
598 case 1:
599 min_drive = max_drive = a->at_dev;
600 break;
601 default:
602 return (EINVAL);
603 }
604 for (drive = min_drive; drive <= max_drive; drive++) {
605 if (chp->ch_drive[drive].drv_softc != NULL) {
606 error = config_detach(
607 chp->ch_drive[drive].drv_softc, 0);
608 if (error)
609 return (error);
610 chp->ch_drive[drive].drv_softc = NULL;
611 }
612 }
613 error = 0;
614 break;
615 }
616 default:
617 error = ENOTTY;
618 }
619 return (error);
620 };
621