2 * Copyright (c) 1998,1999,2000,2001,2002 Søren Schmidt <sos@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification, immediately at the beginning of the file.
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. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * $FreeBSD: src/sys/dev/ata/ata-all.c,v 1.50.2.45 2003/03/12 14:47:12 sos Exp $
29 * $DragonFly: src/sys/dev/disk/ata/ata-all.c,v 1.30 2006/09/05 00:55:37 dillon Exp $
34 #include "use_atadisk.h"
35 #include "use_atapicd.h"
36 #include "use_atapifd.h"
37 #include "use_atapist.h"
38 #include "use_atapicam.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
42 #include <sys/kernel.h>
45 #include <sys/module.h>
48 #include <sys/malloc.h>
49 #include <sys/devicestat.h>
50 #include <sys/sysctl.h>
51 #include <sys/thread2.h>
52 #include <machine/stdarg.h>
53 #include <machine/resource.h>
54 #include <machine/bus.h>
55 #include <machine/clock.h>
60 #include "atapi-all.h"
64 struct atapi_request atapi;
67 /* device structures */
68 static d_ioctl_t ataioctl;
69 static struct dev_ops ata_ops = {
77 static void ata_boot_attach(void);
78 static void ata_intr(void *);
79 static int ata_getparam(struct ata_device *, u_int8_t);
80 static int ata_service(struct ata_channel *);
81 static void bswap(int8_t *, int);
82 static void btrim(int8_t *, int);
83 static void bpack(int8_t *, int8_t *, int);
84 static void ata_change_mode(struct ata_device *, int);
85 static u_int8_t ata_enclosure_sensor(struct ata_device *, int, u_int8_t, u_int8_t);
86 static int ata_enclosure_status(struct ata_device *, int *, int *, int *, int *);
89 SYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters");
91 int ata_mpipe_size = 4;
92 TUNABLE_INT("hw.ata.mpipe_size", &ata_mpipe_size);
93 SYSCTL_INT(_hw_ata, OID_AUTO, mpipe_size, CTLFLAG_RW, &ata_mpipe_size, 0,
94 "ATA global I/O pipeline max size");
98 devclass_t ata_devclass;
101 static struct intr_config_hook *ata_delayed_attach = NULL;
102 static MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer");
105 #define DEV_ATAPIALL NATAPICD > 0 || NATAPIFD > 0 || \
106 NATAPIST > 0 || NATAPICAM > 0
109 ata_probe(device_t dev)
111 struct ata_channel *ch;
114 if (!dev || !(ch = device_get_softc(dev)))
117 if (ch->r_io || ch->r_altio || ch->r_irq)
120 /* initialize the softc basics */
121 ch->active = ATA_IDLE;
124 rid = ATA_IOADDR_RID;
125 ch->r_io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
126 ATA_IOSIZE, RF_ACTIVE);
130 rid = ATA_ALTADDR_RID;
131 ch->r_altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
132 ATA_ALTIOSIZE, RF_ACTIVE);
136 rid = ATA_BMADDR_RID;
137 ch->r_bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
138 ATA_BMIOSIZE, RF_ACTIVE);
140 ata_printf(ch, -1, "iobase=0x%04x altiobase=0x%04x bmaddr=0x%04x\n",
141 (int)rman_get_start(ch->r_io),
142 (int)rman_get_start(ch->r_altio),
143 (ch->r_bmio) ? (int)rman_get_start(ch->r_bmio) : 0);
147 ch->device[MASTER].channel = ch;
148 ch->device[MASTER].unit = ATA_MASTER;
149 ch->device[MASTER].mode = ATA_PIO;
150 ch->device[SLAVE].channel = ch;
151 ch->device[SLAVE].unit = ATA_SLAVE;
152 ch->device[SLAVE].mode = ATA_PIO;
153 TAILQ_INIT(&ch->ata_queue);
154 TAILQ_INIT(&ch->atapi_queue);
156 mpipe_init(&ch->req_mpipe, M_ATA, sizeof(union ata_request),
157 4, ata_mpipe_size, 0, NULL);
158 mpipe_init(&ch->dma_mpipe, M_DEVBUF, PAGE_SIZE,
159 4, ata_mpipe_size, MPF_NOZERO, NULL);
165 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ch->r_io);
167 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ch->r_altio);
169 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, ch->r_bmio);
171 ata_printf(ch, -1, "probe allocation failed\n");
176 ata_attach(device_t dev)
178 struct ata_channel *ch;
181 if (!dev || !(ch = device_get_softc(dev)))
185 ch->r_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
186 RF_SHAREABLE | RF_ACTIVE);
188 ata_printf(ch, -1, "unable to allocate interrupt\n");
193 * Traditional ata registers are sensitive to when they can be accessed
194 * in the face of e.g. ongoing DMA. Do not allow the interrupt to be
197 if ((error = bus_setup_intr(dev, ch->r_irq, INTR_NOPOLL,
198 ata_intr, ch, &ch->ih, NULL))) {
199 ata_printf(ch, -1, "unable to setup interrupt\n");
204 * do not attach devices if we are in early boot, this is done later
205 * when interrupts are enabled by a hook into the boot process.
206 * otherwise attach what the probe has found in ch->devices.
208 if (!ata_delayed_attach) {
211 if (ch->devices & ATA_ATA_SLAVE)
212 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
213 ch->devices &= ~ATA_ATA_SLAVE;
214 if (ch->devices & ATA_ATAPI_SLAVE)
215 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
216 ch->devices &= ~ATA_ATAPI_SLAVE;
217 if (ch->devices & ATA_ATA_MASTER)
218 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
219 ch->devices &= ~ATA_ATA_MASTER;
220 if (ch->devices & ATA_ATAPI_MASTER)
221 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
222 ch->devices &= ~ATA_ATAPI_MASTER;
224 if (ch->devices & ATA_ATA_MASTER)
225 ad_attach(&ch->device[MASTER], 0);
226 if (ch->devices & ATA_ATA_SLAVE)
227 ad_attach(&ch->device[SLAVE], 0);
230 if (ch->devices & ATA_ATAPI_MASTER)
231 atapi_attach(&ch->device[MASTER], 0);
232 if (ch->devices & ATA_ATAPI_SLAVE)
233 atapi_attach(&ch->device[SLAVE], 0);
236 atapi_cam_attach_bus(ch);
244 ata_detach(device_t dev)
246 struct ata_channel *ch;
248 if (!dev || !(ch = device_get_softc(dev)) ||
249 !ch->r_io || !ch->r_altio || !ch->r_irq)
252 /* make sure channel is not busy */
254 ATA_SLEEPLOCK_CH(ch, ATA_CONTROL);
256 if (ch->devices & ATA_ATA_MASTER && ch->device[MASTER].driver)
257 ad_detach(&ch->device[MASTER], 1);
258 if (ch->devices & ATA_ATA_SLAVE && ch->device[SLAVE].driver)
259 ad_detach(&ch->device[SLAVE], 1);
262 if (ch->devices & ATA_ATAPI_MASTER && ch->device[MASTER].driver)
263 atapi_detach(&ch->device[MASTER]);
264 if (ch->devices & ATA_ATAPI_SLAVE && ch->device[SLAVE].driver)
265 atapi_detach(&ch->device[SLAVE]);
268 atapi_cam_detach_bus(ch);
272 if (ch->device[MASTER].param) {
273 kfree(ch->device[MASTER].param, M_ATA);
274 ch->device[MASTER].param = NULL;
276 if (ch->device[SLAVE].param) {
277 kfree(ch->device[SLAVE].param, M_ATA);
278 ch->device[SLAVE].param = NULL;
280 ch->device[MASTER].driver = NULL;
281 ch->device[SLAVE].driver = NULL;
282 ch->device[MASTER].mode = ATA_PIO;
283 ch->device[SLAVE].mode = ATA_PIO;
287 bus_teardown_intr(dev, ch->r_irq, ch->ih);
288 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
290 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, ch->r_bmio);
291 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ch->r_altio);
292 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ch->r_io);
297 mpipe_done(&ch->req_mpipe);
298 mpipe_done(&ch->dma_mpipe);
305 ata_resume(device_t dev)
307 return ata_reinit(device_get_softc(dev));
311 ataioctl(struct dev_ioctl_args *ap)
313 struct ata_cmd *iocmd = (struct ata_cmd *)ap->a_data;
314 struct ata_channel *ch;
315 device_t device = devclass_get_device(ata_devclass, iocmd->channel);
318 if (ap->a_cmd != IOCATA)
321 if (iocmd->channel < -1 || iocmd->device < -1 || iocmd->device > SLAVE)
324 switch (iocmd->cmd) {
326 /* should enable channel HW on controller that can SOS XXX */
327 error = ata_probe(device);
329 error = ata_attach(device);
333 error = ata_detach(device);
334 /* should disable channel HW on controller that can SOS XXX */
338 if (!device || !(ch = device_get_softc(device)))
340 crit_enter(); /* interlock non-atomic channel lock */
341 ATA_SLEEPLOCK_CH(ch, ATA_ACTIVE);
342 if ((error = ata_reinit(ch)))
348 if (!device || !(ch = device_get_softc(device)))
351 if ((iocmd->device == MASTER || iocmd->device == -1) &&
352 ch->device[MASTER].driver)
353 iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode;
355 iocmd->u.mode.mode[MASTER] = -1;
357 if ((iocmd->device == SLAVE || iocmd->device == -1) &&
358 ch->device[SLAVE].param)
359 iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode;
361 iocmd->u.mode.mode[SLAVE] = -1;
365 if (!device || !(ch = device_get_softc(device)))
368 if ((iocmd->device == MASTER || iocmd->device == -1) &&
369 iocmd->u.mode.mode[MASTER] >= 0 && ch->device[MASTER].param) {
370 ata_change_mode(&ch->device[MASTER],iocmd->u.mode.mode[MASTER]);
371 iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode;
374 iocmd->u.mode.mode[MASTER] = -1;
376 if ((iocmd->device == SLAVE || iocmd->device == -1) &&
377 iocmd->u.mode.mode[SLAVE] >= 0 && ch->device[SLAVE].param) {
378 ata_change_mode(&ch->device[SLAVE], iocmd->u.mode.mode[SLAVE]);
379 iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode;
382 iocmd->u.mode.mode[SLAVE] = -1;
386 if (!device || !(ch = device_get_softc(device)))
389 iocmd->u.param.type[MASTER] =
390 ch->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER);
391 iocmd->u.param.type[SLAVE] =
392 ch->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE);
394 if (ch->device[MASTER].name)
395 strcpy(iocmd->u.param.name[MASTER], ch->device[MASTER].name);
396 if (ch->device[SLAVE].name)
397 strcpy(iocmd->u.param.name[SLAVE], ch->device[SLAVE].name);
399 if (ch->device[MASTER].param)
400 bcopy(ch->device[MASTER].param, &iocmd->u.param.params[MASTER],
401 sizeof(struct ata_params));
402 if (ch->device[SLAVE].param)
403 bcopy(ch->device[SLAVE].param, &iocmd->u.param.params[SLAVE],
404 sizeof(struct ata_params));
408 struct ata_device *atadev;
410 if (!device || !(ch = device_get_softc(device)))
413 if (iocmd->device == SLAVE)
414 atadev = &ch->device[SLAVE];
416 atadev = &ch->device[MASTER];
418 return ata_enclosure_status(atadev,
419 &iocmd->u.enclosure.fan,
420 &iocmd->u.enclosure.temp,
421 &iocmd->u.enclosure.v05,
422 &iocmd->u.enclosure.v12);
427 return ata_raid_rebuild(iocmd->channel);
430 return ata_raid_create(&iocmd->u.raid_setup);
433 return ata_raid_delete(iocmd->channel);
436 return ata_raid_status(iocmd->channel, &iocmd->u.raid_status);
440 struct ata_device *atadev;
443 if (!device || !(ch = device_get_softc(device)))
446 if (!(atadev = &ch->device[iocmd->device]) ||
447 !(ch->devices & (iocmd->device == MASTER ?
448 ATA_ATAPI_MASTER : ATA_ATAPI_SLAVE)))
451 buf = kmalloc(iocmd->u.atapi.count, M_ATA, M_INTWAIT);
453 if (iocmd->u.atapi.flags & ATAPI_CMD_WRITE) {
454 error = copyin(iocmd->u.atapi.data, buf, iocmd->u.atapi.count);
458 error = atapi_queue_cmd(atadev, iocmd->u.atapi.ccb,
459 buf, iocmd->u.atapi.count,
460 (iocmd->u.atapi.flags == ATAPI_CMD_READ ?
461 ATPR_F_READ : 0) | ATPR_F_QUIET,
462 iocmd->u.atapi.timeout, NULL, NULL);
464 iocmd->u.atapi.error = error;
465 bcopy(&atadev->result, iocmd->u.atapi.sense_data,
466 sizeof(struct atapi_reqsense));
469 else if (iocmd->u.atapi.flags & ATAPI_CMD_READ)
470 error = copyout(buf, iocmd->u.atapi.data, iocmd->u.atapi.count);
483 ata_getparam(struct ata_device *atadev, u_int8_t command)
485 struct ata_params *ata_parm;
488 ata_parm = kmalloc(sizeof(struct ata_params), M_ATA, M_INTWAIT);
490 /* apparently some devices needs this repeated */
492 if (ata_command(atadev, command, 0, 0, 0, ATA_IMMEDIATE)) {
493 ata_prtdev(atadev, "%s identify failed\n",
494 command == ATA_C_ATAPI_IDENTIFY ? "ATAPI" : "ATA");
495 kfree(ata_parm, M_ATA);
499 ata_prtdev(atadev, "%s identify retries exceeded\n",
500 command == ATA_C_ATAPI_IDENTIFY ? "ATAPI" : "ATA");
501 kfree(ata_parm, M_ATA);
504 } while (ata_wait(atadev, ((command == ATA_C_ATAPI_IDENTIFY) ?
505 ATA_S_DRQ : (ATA_S_READY|ATA_S_DSC|ATA_S_DRQ))));
506 ATA_INSW(atadev->channel->r_io, ATA_DATA, (int16_t *)ata_parm,
507 sizeof(struct ata_params)/sizeof(int16_t));
509 if (command == ATA_C_ATA_IDENTIFY ||
510 !((ata_parm->model[0] == 'N' && ata_parm->model[1] == 'E') ||
511 (ata_parm->model[0] == 'F' && ata_parm->model[1] == 'X') ||
512 (ata_parm->model[0] == 'P' && ata_parm->model[1] == 'i')))
513 bswap(ata_parm->model, sizeof(ata_parm->model));
514 btrim(ata_parm->model, sizeof(ata_parm->model));
515 bpack(ata_parm->model, ata_parm->model, sizeof(ata_parm->model));
516 bswap(ata_parm->revision, sizeof(ata_parm->revision));
517 btrim(ata_parm->revision, sizeof(ata_parm->revision));
518 bpack(ata_parm->revision, ata_parm->revision, sizeof(ata_parm->revision));
519 bswap(ata_parm->serial, sizeof(ata_parm->serial));
520 btrim(ata_parm->serial, sizeof(ata_parm->serial));
521 bpack(ata_parm->serial, ata_parm->serial, sizeof(ata_parm->serial));
522 atadev->param = ata_parm;
527 ata_boot_attach(void)
529 struct ata_channel *ch;
532 if (ata_delayed_attach) {
533 config_intrhook_disestablish(ata_delayed_attach);
534 kfree(ata_delayed_attach, M_TEMP);
535 ata_delayed_attach = NULL;
540 * run through all ata devices and look for real ATA & ATAPI devices
541 * using the hints we found in the early probe, this avoids some of
542 * the delays probing of non-exsistent devices can cause.
544 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
545 if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
547 if (ch->devices & ATA_ATA_SLAVE)
548 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
549 ch->devices &= ~ATA_ATA_SLAVE;
550 if (ch->devices & ATA_ATAPI_SLAVE)
551 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
552 ch->devices &= ~ATA_ATAPI_SLAVE;
553 if (ch->devices & ATA_ATA_MASTER)
554 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
555 ch->devices &= ~ATA_ATA_MASTER;
556 if (ch->devices & ATA_ATAPI_MASTER)
557 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
558 ch->devices &= ~ATA_ATAPI_MASTER;
562 /* now we know whats there, do the real attach, first the ATA disks */
563 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
564 if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
566 if (ch->devices & ATA_ATA_MASTER)
567 ad_attach(&ch->device[MASTER], 0);
568 if (ch->devices & ATA_ATA_SLAVE)
569 ad_attach(&ch->device[SLAVE], 0);
574 /* then the atapi devices */
575 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
576 if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
578 if (ch->devices & ATA_ATAPI_MASTER)
579 atapi_attach(&ch->device[MASTER], 0);
580 if (ch->devices & ATA_ATAPI_SLAVE)
581 atapi_attach(&ch->device[SLAVE], 0);
583 atapi_cam_attach_bus(ch);
593 struct ata_channel *ch = (struct ata_channel *)data;
595 * on PCI systems we might share an interrupt line with another
596 * device or our twin ATA channel, so call ch->intr_func to figure
597 * out if it is really an interrupt we should process here
599 if (ch->intr_func && ch->intr_func(ch))
602 /* if drive is busy it didn't interrupt */
603 if (ATA_INB(ch->r_altio, ATA_ALTSTAT) & ATA_S_BUSY) {
605 if (!(ATA_INB(ch->r_altio, ATA_ALTSTAT) & ATA_S_DRQ))
609 /* clear interrupt and get status */
610 ch->status = ATA_INB(ch->r_io, ATA_STATUS);
612 if (ch->status & ATA_S_ERROR)
613 ch->error = ATA_INB(ch->r_io, ATA_ERROR);
615 /* find & call the responsible driver to process this interrupt */
616 switch (ch->active) {
619 if (!ch->running || ad_interrupt(ch->running) == ATA_OP_CONTINUES)
624 case ATA_ACTIVE_ATAPI:
625 if (!ch->running || atapi_interrupt(ch->running) == ATA_OP_CONTINUES)
630 case ATA_WAIT_INTR | ATA_CONTROL:
635 case ATA_WAIT_READY | ATA_CONTROL:
639 if (ch->flags & ATA_QUEUED) {
640 ch->active = ATA_ACTIVE;
641 if (ata_service(ch) == ATA_OP_CONTINUES)
649 static int intr_count = 0;
651 if (intr_count++ < 10)
652 ata_printf(ch, -1, "unwanted interrupt #%d active=%02x s=%02x\n",
653 intr_count, ch->active, ch->status);
658 ch->active &= ATA_CONTROL;
659 if (ch->active & ATA_CONTROL)
667 ata_start(struct ata_channel *ch)
670 struct ad_request *ad_request;
673 struct atapi_request *atapi_request;
676 crit_enter(); /* interlock non-atomic channel lock */
677 if (!ATA_LOCK_CH(ch, ATA_ACTIVE)) {
683 /* find & call the responsible driver if anything on the ATA queue */
684 if (TAILQ_EMPTY(&ch->ata_queue)) {
685 if (ch->devices & (ATA_ATA_MASTER) && ch->device[MASTER].driver)
686 ad_start(&ch->device[MASTER]);
687 if (ch->devices & (ATA_ATA_SLAVE) && ch->device[SLAVE].driver)
688 ad_start(&ch->device[SLAVE]);
690 if ((ad_request = TAILQ_FIRST(&ch->ata_queue))) {
691 TAILQ_REMOVE(&ch->ata_queue, ad_request, chain);
692 ch->active = ATA_ACTIVE_ATA;
693 ch->running = ad_request;
696 * The donecount had better be 0 here because the channel may not
697 * have retained the setup for the request (if a retry).
699 KKASSERT(ad_request->donecount == 0);
700 if (ad_transfer(ad_request) == ATA_OP_CONTINUES) {
708 /* find & call the responsible driver if anything on the ATAPI queue */
709 if (TAILQ_EMPTY(&ch->atapi_queue)) {
710 if (ch->devices & (ATA_ATAPI_MASTER) && ch->device[MASTER].driver)
711 atapi_start(&ch->device[MASTER]);
712 if (ch->devices & (ATA_ATAPI_SLAVE) && ch->device[SLAVE].driver)
713 atapi_start(&ch->device[SLAVE]);
715 if ((atapi_request = TAILQ_FIRST(&ch->atapi_queue))) {
716 TAILQ_REMOVE(&ch->atapi_queue, atapi_request, chain);
717 ch->active = ATA_ACTIVE_ATAPI;
718 ch->running = atapi_request;
719 if (atapi_transfer(atapi_request) == ATA_OP_CONTINUES) {
730 ata_reset(struct ata_channel *ch)
732 u_int8_t lsb, msb, ostat0, ostat1;
733 u_int8_t stat0 = 0, stat1 = 0;
734 int mask = 0, timeout;
736 /* do we have any signs of ATA/ATAPI HW being present ? */
737 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
739 ostat0 = ATA_INB(ch->r_io, ATA_STATUS);
740 if ((ostat0 & 0xf8) != 0xf8 && ostat0 != 0xa5) {
744 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
746 ostat1 = ATA_INB(ch->r_io, ATA_STATUS);
747 if ((ostat1 & 0xf8) != 0xf8 && ostat1 != 0xa5) {
756 /* in some setups we dont want to test for a slave */
757 if (ch->flags & ATA_NO_SLAVE) {
763 ata_printf(ch, -1, "mask=%02x ostat0=%02x ostat2=%02x\n",
764 mask, ostat0, ostat1);
767 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
769 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_RESET);
771 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_IDS);
773 ATA_INB(ch->r_io, ATA_ERROR);
775 /* wait for BUSY to go inactive */
776 for (timeout = 0; timeout < 3100; timeout++) {
777 if (stat0 & ATA_S_BUSY) {
778 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
781 /* check for ATAPI signature while its still there */
782 lsb = ATA_INB(ch->r_io, ATA_CYL_LSB);
783 msb = ATA_INB(ch->r_io, ATA_CYL_MSB);
784 stat0 = ATA_INB(ch->r_io, ATA_STATUS);
785 if (!(stat0 & ATA_S_BUSY)) {
787 ata_printf(ch, ATA_MASTER, "ATAPI %02x %02x\n", lsb, msb);
788 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB)
789 ch->devices |= ATA_ATAPI_MASTER;
792 if (stat1 & ATA_S_BUSY) {
793 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
796 /* check for ATAPI signature while its still there */
797 lsb = ATA_INB(ch->r_io, ATA_CYL_LSB);
798 msb = ATA_INB(ch->r_io, ATA_CYL_MSB);
799 stat1 = ATA_INB(ch->r_io, ATA_STATUS);
800 if (!(stat1 & ATA_S_BUSY)) {
802 ata_printf(ch, ATA_SLAVE, "ATAPI %02x %02x\n", lsb, msb);
803 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB)
804 ch->devices |= ATA_ATAPI_SLAVE;
807 if (mask == 0x01) /* wait for master only */
808 if (!(stat0 & ATA_S_BUSY))
810 if (mask == 0x02) /* wait for slave only */
811 if (!(stat1 & ATA_S_BUSY))
813 if (mask == 0x03) /* wait for both master & slave */
814 if (!(stat0 & ATA_S_BUSY) && !(stat1 & ATA_S_BUSY))
819 * some devices release BUSY before they are ready to accept commands.
820 * We must wait at least 50ms before attempting to issue a command after
824 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
826 if (stat0 & ATA_S_BUSY)
828 if (stat1 & ATA_S_BUSY)
831 ata_printf(ch, -1, "mask=%02x stat0=%02x stat1=%02x\n",
836 if (mask & 0x01 && ostat0 != 0x00 && !(ch->devices & ATA_ATAPI_MASTER)) {
837 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
839 ATA_OUTB(ch->r_io, ATA_ERROR, 0x58);
840 ATA_OUTB(ch->r_io, ATA_CYL_LSB, 0xa5);
841 lsb = ATA_INB(ch->r_io, ATA_ERROR);
842 msb = ATA_INB(ch->r_io, ATA_CYL_LSB);
844 ata_printf(ch, ATA_MASTER, "ATA %02x %02x\n", lsb, msb);
845 if (lsb != 0x58 && msb == 0xa5)
846 ch->devices |= ATA_ATA_MASTER;
848 if (mask & 0x02 && ostat1 != 0x00 && !(ch->devices & ATA_ATAPI_SLAVE)) {
849 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
851 ATA_OUTB(ch->r_io, ATA_ERROR, 0x58);
852 ATA_OUTB(ch->r_io, ATA_CYL_LSB, 0xa5);
853 lsb = ATA_INB(ch->r_io, ATA_ERROR);
854 msb = ATA_INB(ch->r_io, ATA_CYL_LSB);
856 ata_printf(ch, ATA_SLAVE, "ATA %02x %02x\n", lsb, msb);
857 if (lsb != 0x58 && msb == 0xa5)
858 ch->devices |= ATA_ATA_SLAVE;
861 ata_printf(ch, -1, "devices=%02x\n", ch->devices);
865 ata_reinit(struct ata_channel *ch)
867 int devices, misdev, newdev;
869 if (!ch->r_io || !ch->r_altio || !ch->r_irq)
872 ATA_FORCELOCK_CH(ch, ATA_CONTROL);
874 devices = ch->devices;
875 ata_printf(ch, -1, "resetting devices .. ");
878 if ((misdev = devices & ~ch->devices)) {
882 if (misdev & ATA_ATA_MASTER && ch->device[MASTER].driver)
883 ad_detach(&ch->device[MASTER], 0);
884 if (misdev & ATA_ATA_SLAVE && ch->device[SLAVE].driver)
885 ad_detach(&ch->device[SLAVE], 0);
888 if (misdev & ATA_ATAPI_MASTER && ch->device[MASTER].driver)
889 atapi_detach(&ch->device[MASTER]);
890 if (misdev & ATA_ATAPI_SLAVE && ch->device[SLAVE].driver)
891 atapi_detach(&ch->device[SLAVE]);
893 if (misdev & ATA_ATA_MASTER || misdev & ATA_ATAPI_MASTER) {
894 if (ch->device[MASTER].param)
895 kfree(ch->device[MASTER].param, M_ATA);
896 ch->device[MASTER].param = NULL;
898 if (misdev & ATA_ATA_SLAVE || misdev & ATA_ATAPI_SLAVE) {
899 if (ch->device[SLAVE].param)
900 kfree(ch->device[SLAVE].param, M_ATA);
901 ch->device[SLAVE].param = NULL;
904 if ((newdev = ~devices & ch->devices)) {
905 if (newdev & ATA_ATA_MASTER)
906 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
907 ch->devices &= ~ATA_ATA_MASTER;
908 if (newdev & ATA_ATA_SLAVE)
909 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
910 ch->devices &= ~ATA_ATA_SLAVE;
911 if (newdev & ATA_ATAPI_MASTER)
912 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
913 ch->devices &= ~ATA_ATAPI_MASTER;
914 if (newdev & ATA_ATAPI_SLAVE)
915 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
916 ch->devices &= ~ATA_ATAPI_SLAVE;
918 newdev = ~devices & ch->devices;
919 if (!misdev && newdev)
922 if (newdev & ATA_ATA_MASTER && !ch->device[MASTER].driver)
923 ad_attach(&ch->device[MASTER], 1);
924 else if (ch->devices & ATA_ATA_MASTER && ch->device[MASTER].driver) {
925 ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY);
926 ad_reinit(&ch->device[MASTER]);
928 if (newdev & ATA_ATA_SLAVE && !ch->device[SLAVE].driver)
929 ad_attach(&ch->device[SLAVE], 1);
930 else if (ch->devices & (ATA_ATA_SLAVE) && ch->device[SLAVE].driver) {
931 ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY);
932 ad_reinit(&ch->device[SLAVE]);
936 if (newdev & ATA_ATAPI_MASTER && !ch->device[MASTER].driver)
937 atapi_attach(&ch->device[MASTER], 1);
938 else if (ch->devices & (ATA_ATAPI_MASTER) && ch->device[MASTER].driver) {
939 ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY);
940 atapi_reinit(&ch->device[MASTER]);
942 if (newdev & ATA_ATAPI_SLAVE && !ch->device[SLAVE].driver)
943 atapi_attach(&ch->device[SLAVE], 1);
944 else if (ch->devices & (ATA_ATAPI_SLAVE) && ch->device[SLAVE].driver) {
945 ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY);
946 atapi_reinit(&ch->device[SLAVE]);
950 if (ch->devices & (ATA_ATAPI_MASTER | ATA_ATAPI_SLAVE))
951 atapi_cam_reinit_bus(ch);
960 ata_service(struct ata_channel *ch)
962 /* do we have a SERVICE request from the drive ? */
963 if ((ch->status & (ATA_S_SERVICE|ATA_S_ERROR|ATA_S_DRQ)) == ATA_S_SERVICE) {
964 ATA_OUTB(ch->r_bmio, ATA_BMSTAT_PORT,
965 ata_dmastatus(ch) | ATA_BMSTAT_INTERRUPT);
967 if ((ATA_INB(ch->r_io, ATA_DRIVE) & ATA_SLAVE) == ATA_MASTER) {
968 if ((ch->devices & ATA_ATA_MASTER) && ch->device[MASTER].driver)
969 return ad_service((struct ad_softc *)
970 ch->device[MASTER].driver, 0);
973 if ((ch->devices & ATA_ATA_SLAVE) && ch->device[SLAVE].driver)
974 return ad_service((struct ad_softc *)
975 ch->device[SLAVE].driver, 0);
979 return ATA_OP_FINISHED;
983 ata_wait(struct ata_device *atadev, u_int8_t mask)
988 while (timeout < 5000000) { /* timeout 5 secs */
989 atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS);
991 /* if drive fails status, reselect the drive just to be sure */
992 if (atadev->channel->status == 0xff) {
993 ata_prtdev(atadev, "no status, reselecting device\n");
994 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM|atadev->unit);
996 atadev->channel->status = ATA_INB(atadev->channel->r_io,ATA_STATUS);
997 if (atadev->channel->status == 0xff)
1002 if (!(atadev->channel->status & ATA_S_BUSY))
1005 if (timeout > 1000) {
1014 if (atadev->channel->status & ATA_S_ERROR)
1015 atadev->channel->error = ATA_INB(atadev->channel->r_io, ATA_ERROR);
1016 if (timeout >= 5000000)
1019 return (atadev->channel->status & ATA_S_ERROR);
1021 /* Wait 50 msec for bits wanted. */
1024 atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS);
1025 if ((atadev->channel->status & mask) == mask) {
1026 if (atadev->channel->status & ATA_S_ERROR)
1027 atadev->channel->error=ATA_INB(atadev->channel->r_io,ATA_ERROR);
1028 return (atadev->channel->status & ATA_S_ERROR);
1036 ata_command(struct ata_device *atadev, u_int8_t command,
1037 u_int64_t lba, u_int16_t count, u_int8_t feature, int flags)
1041 ata_prtdev(atadev, "ata_command: addr=%04lx, cmd=%02x, "
1042 "lba=%lld, count=%d, feature=%d, flags=%02x\n",
1043 rman_get_start(atadev->channel->r_io),
1044 command, lba, count, feature, flags);
1048 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1050 /* disable interrupt from device */
1051 if (atadev->channel->flags & ATA_QUEUED)
1052 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_4BIT);
1054 /* ready to issue command ? */
1055 if (ata_wait(atadev, 0) < 0) {
1056 ata_prtdev(atadev, "timeout sending command=%02x s=%02x e=%02x\n",
1057 command, atadev->channel->status, atadev->channel->error);
1061 /* only use 48bit addressing if needed because of the overhead */
1062 if ((lba >= 268435455 || count > 256) && atadev->param &&
1063 atadev->param->support.address48) {
1064 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, (feature>>8) & 0xff);
1065 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, feature);
1066 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, (count>>8) & 0xff);
1067 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, count & 0xff);
1068 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, (lba>>24) & 0xff);
1069 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, lba & 0xff);
1070 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>32) & 0xff);
1071 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>8) & 0xff);
1072 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>40) & 0xff);
1073 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>16) & 0xff);
1074 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_LBA | atadev->unit);
1076 /* translate command into 48bit version */
1079 command = ATA_C_READ48; break;
1080 case ATA_C_READ_MUL:
1081 command = ATA_C_READ_MUL48; break;
1082 case ATA_C_READ_DMA:
1083 command = ATA_C_READ_DMA48; break;
1084 case ATA_C_READ_DMA_QUEUED:
1085 command = ATA_C_READ_DMA_QUEUED48; break;
1087 command = ATA_C_WRITE48; break;
1088 case ATA_C_WRITE_MUL:
1089 command = ATA_C_WRITE_MUL48; break;
1090 case ATA_C_WRITE_DMA:
1091 command = ATA_C_WRITE_DMA48; break;
1092 case ATA_C_WRITE_DMA_QUEUED:
1093 command = ATA_C_WRITE_DMA_QUEUED48; break;
1094 case ATA_C_FLUSHCACHE:
1095 command = ATA_C_FLUSHCACHE48; break;
1097 ata_prtdev(atadev, "can't translate cmd to 48bit version\n");
1102 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, feature);
1103 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, count);
1104 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, lba & 0xff);
1105 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>8) & 0xff);
1106 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>16) & 0xff);
1107 if (atadev->flags & ATA_D_USE_CHS)
1108 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE,
1109 ATA_D_IBM | atadev->unit | ((lba>>24) & 0xf));
1111 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE,
1112 ATA_D_IBM | ATA_D_LBA | atadev->unit | ((lba>>24) &0xf));
1115 switch (flags & ATA_WAIT_MASK) {
1117 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1119 /* enable interrupt */
1120 if (atadev->channel->flags & ATA_QUEUED)
1121 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
1125 atadev->channel->active |= ATA_WAIT_INTR;
1126 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1128 /* enable interrupt */
1129 if (atadev->channel->flags & ATA_QUEUED)
1130 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
1132 if (tsleep((caddr_t)atadev->channel, 0, "atacmd", 10 * hz)) {
1133 ata_prtdev(atadev, "timeout waiting for interrupt\n");
1134 atadev->channel->active &= ~ATA_WAIT_INTR;
1139 case ATA_WAIT_READY:
1140 atadev->channel->active |= ATA_WAIT_READY;
1141 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1142 if (ata_wait(atadev, ATA_S_READY) < 0) {
1143 ata_prtdev(atadev, "timeout waiting for cmd=%02x s=%02x e=%02x\n",
1144 command, atadev->channel->status,atadev->channel->error);
1147 atadev->channel->active &= ~ATA_WAIT_READY;
1154 ata_enclosure_start(struct ata_device *atadev)
1156 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1158 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1160 ATA_INB(atadev->channel->r_io, ATA_CMD);
1162 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1164 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1166 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1168 ATA_INB(atadev->channel->r_io, ATA_COUNT);
1170 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1175 ata_enclosure_end(struct ata_device *atadev)
1177 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1182 ata_enclosure_chip_start(struct ata_device *atadev)
1184 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0b);
1185 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0a);
1187 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08);
1191 ata_enclosure_chip_end(struct ata_device *atadev)
1193 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08);
1195 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0a);
1197 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0b);
1202 ata_enclosure_chip_rdbit(struct ata_device *atadev)
1206 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0);
1208 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x02);
1210 val = ATA_INB(atadev->channel->r_io, ATA_SECTOR) & 0x01;
1216 ata_enclosure_chip_wrbit(struct ata_device *atadev, u_int8_t data)
1218 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08 | (data & 0x01));
1220 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08 | 0x02 | (data & 0x01));
1225 ata_enclosure_chip_rw(struct ata_device *atadev, int rw, u_int8_t val)
1230 for (i = 0; i < 8; i++)
1231 ata_enclosure_chip_wrbit(atadev, (val & (0x80 >> i)) ? 1 : 0);
1234 for (i = 0; i < 8; i++)
1235 val = (val << 1) | ata_enclosure_chip_rdbit(atadev);
1237 ata_enclosure_chip_wrbit(atadev, 0);
1242 ata_enclosure_sensor(struct ata_device *atadev,
1243 int rw, u_int8_t idx, u_int8_t data)
1245 ata_enclosure_start(atadev);
1246 ata_enclosure_chip_start(atadev);
1247 ata_enclosure_chip_rw(atadev, 1, 0x5a);
1248 ata_enclosure_chip_rw(atadev, 1, idx);
1250 ata_enclosure_chip_rw(atadev, 1, data);
1253 ata_enclosure_chip_end(atadev);
1254 ata_enclosure_chip_start(atadev);
1255 ata_enclosure_chip_rw(atadev, 1, 0x5b);
1256 data = ata_enclosure_chip_rw(atadev, 0, 0);
1258 ata_enclosure_chip_end(atadev);
1259 ata_enclosure_end(atadev);
1264 ata_enclosure_status(struct ata_device *atadev,
1265 int *fan, int *temp, int *v05, int *v12)
1267 u_int8_t id1, id2, cnt, div;
1270 if (atadev->flags & ATA_D_ENC_PRESENT) {
1271 ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL);
1272 ata_enclosure_sensor(atadev, 1, 0x4e, 0);
1273 id1 = ata_enclosure_sensor(atadev, 0, 0x4f, 0);
1274 ata_enclosure_sensor(atadev, 1, 0x4e, 0x80);
1275 id2 = ata_enclosure_sensor(atadev, 0, 0x4f, 0);
1276 if (id1 == 0xa3 && id2 == 0x5c) {
1277 div = 1 << (((ata_enclosure_sensor(atadev, 0, 0x5d, 0)&0x20)>>3)+
1278 ((ata_enclosure_sensor(atadev, 0, 0x47, 0)&0x30)>>4)+1);
1279 cnt = ata_enclosure_sensor(atadev, 0, 0x28, 0);
1283 *fan = 1350000 / cnt / div;
1284 ata_enclosure_sensor(atadev, 1, 0x4e, 0x01);
1285 *temp = (ata_enclosure_sensor(atadev, 0, 0x50, 0) * 10) +
1286 (ata_enclosure_sensor(atadev, 0, 0x50, 0) & 0x80 ? 5 : 0);
1287 *v05 = ata_enclosure_sensor(atadev, 0, 0x23, 0) * 27;
1288 *v12 = ata_enclosure_sensor(atadev, 0, 0x24, 0) * 61;
1291 ATA_UNLOCK_CH(atadev->channel);
1297 ata_enclosure_print(struct ata_device *atadev)
1300 int fan, temp, v05, v12;
1302 ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL);
1303 ata_enclosure_start(atadev);
1304 id = ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1306 st = ATA_INB(atadev->channel->r_io, ATA_COUNT);
1308 ata_enclosure_end(atadev);
1309 ATA_UNLOCK_CH(atadev->channel);
1311 switch (id & 0x93) {
1313 ata_prtdev(atadev, "Universal enclosure");
1316 ata_prtdev(atadev, "FastSwap enclosure");
1320 ata_prtdev(atadev, "SuperSwap enclosure");
1323 atadev->flags &= ~ATA_D_ENC_PRESENT;
1326 atadev->flags |= ATA_D_ENC_PRESENT;
1328 if (ata_enclosure_status(atadev, &fan, &temp, &v05, &v12))
1329 printf(" detected\n");
1331 printf(" [FAN:%drpm TEMP:%d.%01dC %d.%03dV %d.%03dV]\n",
1332 fan, temp/10, temp%10, v05/1000, v05%1000, v12/1000, v12%1000);
1336 ata_enclosure_leds(struct ata_device *atadev, u_int8_t color)
1338 if (atadev->flags & ATA_D_ENC_PRESENT) {
1341 ata_enclosure_start(atadev);
1342 reg = ATA_INB(atadev->channel->r_io, ATA_COUNT);
1344 ATA_OUTB(atadev->channel->r_io, ATA_COUNT,
1345 (color & ATA_LED_MASK) | (reg & ~ATA_LED_MASK));
1347 ata_enclosure_end(atadev);
1352 ata_change_mode(struct ata_device *atadev, int mode)
1354 int umode, wmode, pmode;
1356 umode = ata_umode(atadev->param);
1357 wmode = ata_wmode(atadev->param);
1358 pmode = ata_pmode(atadev->param);
1360 switch (mode & ATA_DMA_MASK) {
1362 if ((mode & ATA_MODE_MASK) < umode)
1363 umode = mode & ATA_MODE_MASK;
1366 if ((mode & ATA_MODE_MASK) < wmode)
1367 wmode = mode & ATA_MODE_MASK;
1371 if (((mode & ATA_MODE_MASK) - ATA_PIO0) < pmode)
1372 pmode = (mode & ATA_MODE_MASK) - ATA_PIO0;
1377 crit_enter(); /* interlock non-atomic channel lock */
1378 ATA_SLEEPLOCK_CH(atadev->channel, ATA_ACTIVE);
1379 ata_dmainit(atadev, pmode, wmode, umode);
1380 ATA_UNLOCK_CH(atadev->channel);
1382 ata_start(atadev->channel); /* XXX SOS */
1386 ata_printf(struct ata_channel *ch, int device, const char * fmt, ...)
1392 ret = printf("ata%d: ", device_get_unit(ch->dev));
1394 if (ch->device[ATA_DEV(device)].name)
1395 ret = printf("%s: ", ch->device[ATA_DEV(device)].name);
1397 ret = printf("ata%d-%s: ", device_get_unit(ch->dev),
1398 (device == ATA_MASTER) ? "master" : "slave");
1400 __va_start(ap, fmt);
1401 ret += vprintf(fmt, ap);
1407 ata_prtdev(struct ata_device *atadev, const char * fmt, ...)
1413 ret = printf("%s: ", atadev->name);
1415 ret = printf("ata%d-%s: ", device_get_unit(atadev->channel->dev),
1416 (atadev->unit == ATA_MASTER) ? "master" : "slave");
1417 __va_start(ap, fmt);
1418 ret += vprintf(fmt, ap);
1424 ata_set_name(struct ata_device *atadev, char *name, int lun)
1426 atadev->name = kmalloc(strlen(name) + 4, M_ATA, M_INTWAIT);
1427 sprintf(atadev->name, "%s%d", name, lun);
1431 ata_free_name(struct ata_device *atadev)
1434 kfree(atadev->name, M_ATA);
1435 atadev->name = NULL;
1439 ata_get_lun(u_int32_t *map)
1441 int lun = ffs(~*map) - 1;
1448 ata_test_lun(u_int32_t *map, int lun)
1450 return (*map & (1 << lun));
1454 ata_free_lun(u_int32_t *map, int lun)
1456 *map &= ~(1 << lun);
1460 ata_mode2str(int mode)
1463 case ATA_PIO: return "BIOSPIO";
1464 case ATA_PIO0: return "PIO0";
1465 case ATA_PIO1: return "PIO1";
1466 case ATA_PIO2: return "PIO2";
1467 case ATA_PIO3: return "PIO3";
1468 case ATA_PIO4: return "PIO4";
1469 case ATA_DMA: return "BIOSDMA";
1470 case ATA_WDMA2: return "WDMA2";
1471 case ATA_UDMA2: return "UDMA33";
1472 case ATA_UDMA4: return "UDMA66";
1473 case ATA_UDMA5: return "UDMA100";
1474 case ATA_UDMA6: return "UDMA133";
1475 default: return "???";
1480 ata_pmode(struct ata_params *ap)
1482 if (ap->atavalid & ATA_FLAG_64_70) {
1483 if (ap->apiomodes & 2)
1485 if (ap->apiomodes & 1)
1488 if (ap->retired_piomode == 2)
1490 if (ap->retired_piomode == 1)
1492 if (ap->retired_piomode == 0)
1498 ata_wmode(struct ata_params *ap)
1500 if (ap->mwdmamodes & 0x04)
1502 if (ap->mwdmamodes & 0x02)
1504 if (ap->mwdmamodes & 0x01)
1510 ata_umode(struct ata_params *ap)
1512 if (ap->atavalid & ATA_FLAG_88) {
1513 if (ap->udmamodes & 0x40)
1515 if (ap->udmamodes & 0x20)
1517 if (ap->udmamodes & 0x10)
1519 if (ap->udmamodes & 0x08)
1521 if (ap->udmamodes & 0x04)
1523 if (ap->udmamodes & 0x02)
1525 if (ap->udmamodes & 0x01)
1532 bswap(int8_t *buf, int len)
1534 u_int16_t *ptr = (u_int16_t*)(buf + len);
1536 while (--ptr >= (u_int16_t*)buf)
1541 btrim(int8_t *buf, int len)
1545 for (ptr = buf; ptr < buf+len; ++ptr)
1548 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
1553 bpack(int8_t *src, int8_t *dst, int len)
1557 for (i = j = blank = 0 ; i < len; i++) {
1558 if (blank && src[i] == ' ') continue;
1559 if (blank && src[i] != ' ') {
1564 if (src[i] == ' ') {
1578 /* register controlling device */
1579 dev_ops_add(&ata_ops, 0, 0);
1580 make_dev(&ata_ops, 0, UID_ROOT, GID_OPERATOR, 0600, "ata");
1582 /* register boot attach to be run when interrupts are enabled */
1583 ata_delayed_attach = kmalloc(sizeof(struct intr_config_hook),
1584 M_TEMP, M_WAITOK | M_ZERO);
1586 ata_delayed_attach->ich_func = (void*)ata_boot_attach;
1587 ata_delayed_attach->ich_desc = "ata";
1588 if (config_intrhook_establish(ata_delayed_attach) != 0) {
1589 printf("ata: config_intrhook_establish failed\n");
1590 kfree(ata_delayed_attach, M_TEMP);
1593 SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL)