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.34 2006/12/20 18:14:38 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>
54 #include <machine/stdarg.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_suspend(device_t dev)
307 struct ata_channel *ch;
309 if (dev == NULL || (ch = device_get_softc(dev)) == NULL)
312 /* wait for the channel to be IDLE or detached before suspending */
315 if (ch->active == ATA_IDLE) {
316 ch->active = ATA_CONTROL;
321 tsleep(ch, 0, "atasusp", hz / 10);
327 ata_resume(device_t dev)
329 return ata_reinit(device_get_softc(dev));
333 ataioctl(struct dev_ioctl_args *ap)
335 struct ata_cmd *iocmd = (struct ata_cmd *)ap->a_data;
336 struct ata_channel *ch;
337 device_t device = devclass_get_device(ata_devclass, iocmd->channel);
340 if (ap->a_cmd != IOCATA)
343 if (iocmd->channel < -1 || iocmd->device < -1 || iocmd->device > SLAVE)
346 switch (iocmd->cmd) {
348 /* should enable channel HW on controller that can SOS XXX */
349 error = ata_probe(device);
351 error = ata_attach(device);
355 error = ata_detach(device);
356 /* should disable channel HW on controller that can SOS XXX */
360 if (!device || !(ch = device_get_softc(device)))
362 crit_enter(); /* interlock non-atomic channel lock */
363 ATA_SLEEPLOCK_CH(ch, ATA_ACTIVE);
364 if ((error = ata_reinit(ch)))
370 if (!device || !(ch = device_get_softc(device)))
373 if ((iocmd->device == MASTER || iocmd->device == -1) &&
374 ch->device[MASTER].driver)
375 iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode;
377 iocmd->u.mode.mode[MASTER] = -1;
379 if ((iocmd->device == SLAVE || iocmd->device == -1) &&
380 ch->device[SLAVE].param)
381 iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode;
383 iocmd->u.mode.mode[SLAVE] = -1;
387 if (!device || !(ch = device_get_softc(device)))
390 if ((iocmd->device == MASTER || iocmd->device == -1) &&
391 iocmd->u.mode.mode[MASTER] >= 0 && ch->device[MASTER].param) {
392 ata_change_mode(&ch->device[MASTER],iocmd->u.mode.mode[MASTER]);
393 iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode;
396 iocmd->u.mode.mode[MASTER] = -1;
398 if ((iocmd->device == SLAVE || iocmd->device == -1) &&
399 iocmd->u.mode.mode[SLAVE] >= 0 && ch->device[SLAVE].param) {
400 ata_change_mode(&ch->device[SLAVE], iocmd->u.mode.mode[SLAVE]);
401 iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode;
404 iocmd->u.mode.mode[SLAVE] = -1;
408 if (!device || !(ch = device_get_softc(device)))
411 iocmd->u.param.type[MASTER] =
412 ch->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER);
413 iocmd->u.param.type[SLAVE] =
414 ch->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE);
416 if (ch->device[MASTER].name)
417 strcpy(iocmd->u.param.name[MASTER], ch->device[MASTER].name);
418 if (ch->device[SLAVE].name)
419 strcpy(iocmd->u.param.name[SLAVE], ch->device[SLAVE].name);
421 if (ch->device[MASTER].param)
422 bcopy(ch->device[MASTER].param, &iocmd->u.param.params[MASTER],
423 sizeof(struct ata_params));
424 if (ch->device[SLAVE].param)
425 bcopy(ch->device[SLAVE].param, &iocmd->u.param.params[SLAVE],
426 sizeof(struct ata_params));
430 struct ata_device *atadev;
432 if (!device || !(ch = device_get_softc(device)))
435 if (iocmd->device == SLAVE)
436 atadev = &ch->device[SLAVE];
438 atadev = &ch->device[MASTER];
440 return ata_enclosure_status(atadev,
441 &iocmd->u.enclosure.fan,
442 &iocmd->u.enclosure.temp,
443 &iocmd->u.enclosure.v05,
444 &iocmd->u.enclosure.v12);
449 return ata_raid_rebuild(iocmd->channel);
452 return ata_raid_create(&iocmd->u.raid_setup);
455 return ata_raid_delete(iocmd->channel);
458 return ata_raid_status(iocmd->channel, &iocmd->u.raid_status);
462 struct ata_device *atadev;
465 if (!device || !(ch = device_get_softc(device)))
468 if (!(atadev = &ch->device[iocmd->device]) ||
469 !(ch->devices & (iocmd->device == MASTER ?
470 ATA_ATAPI_MASTER : ATA_ATAPI_SLAVE)))
473 buf = kmalloc(iocmd->u.atapi.count, M_ATA, M_INTWAIT);
475 if (iocmd->u.atapi.flags & ATAPI_CMD_WRITE) {
476 error = copyin(iocmd->u.atapi.data, buf, iocmd->u.atapi.count);
480 error = atapi_queue_cmd(atadev, iocmd->u.atapi.ccb,
481 buf, iocmd->u.atapi.count,
482 (iocmd->u.atapi.flags == ATAPI_CMD_READ ?
483 ATPR_F_READ : 0) | ATPR_F_QUIET,
484 iocmd->u.atapi.timeout, NULL, NULL);
486 iocmd->u.atapi.error = error;
487 bcopy(&atadev->result, iocmd->u.atapi.sense_data,
488 sizeof(struct atapi_reqsense));
491 else if (iocmd->u.atapi.flags & ATAPI_CMD_READ)
492 error = copyout(buf, iocmd->u.atapi.data, iocmd->u.atapi.count);
505 ata_getparam(struct ata_device *atadev, u_int8_t command)
507 struct ata_params *ata_parm;
510 ata_parm = kmalloc(sizeof(struct ata_params), M_ATA, M_INTWAIT);
512 /* apparently some devices needs this repeated */
514 if (ata_command(atadev, command, 0, 0, 0, ATA_IMMEDIATE)) {
515 ata_prtdev(atadev, "%s identify failed\n",
516 command == ATA_C_ATAPI_IDENTIFY ? "ATAPI" : "ATA");
517 kfree(ata_parm, M_ATA);
521 ata_prtdev(atadev, "%s identify retries exceeded\n",
522 command == ATA_C_ATAPI_IDENTIFY ? "ATAPI" : "ATA");
523 kfree(ata_parm, M_ATA);
526 } while (ata_wait(atadev, ((command == ATA_C_ATAPI_IDENTIFY) ?
527 ATA_S_DRQ : (ATA_S_READY|ATA_S_DSC|ATA_S_DRQ))));
528 ATA_INSW(atadev->channel->r_io, ATA_DATA, (int16_t *)ata_parm,
529 sizeof(struct ata_params)/sizeof(int16_t));
531 if (command == ATA_C_ATA_IDENTIFY ||
532 !((ata_parm->model[0] == 'N' && ata_parm->model[1] == 'E') ||
533 (ata_parm->model[0] == 'F' && ata_parm->model[1] == 'X') ||
534 (ata_parm->model[0] == 'P' && ata_parm->model[1] == 'i')))
535 bswap(ata_parm->model, sizeof(ata_parm->model));
536 btrim(ata_parm->model, sizeof(ata_parm->model));
537 bpack(ata_parm->model, ata_parm->model, sizeof(ata_parm->model));
538 bswap(ata_parm->revision, sizeof(ata_parm->revision));
539 btrim(ata_parm->revision, sizeof(ata_parm->revision));
540 bpack(ata_parm->revision, ata_parm->revision, sizeof(ata_parm->revision));
541 bswap(ata_parm->serial, sizeof(ata_parm->serial));
542 btrim(ata_parm->serial, sizeof(ata_parm->serial));
543 bpack(ata_parm->serial, ata_parm->serial, sizeof(ata_parm->serial));
544 atadev->param = ata_parm;
549 ata_boot_attach(void)
551 struct ata_channel *ch;
554 if (ata_delayed_attach) {
555 config_intrhook_disestablish(ata_delayed_attach);
556 kfree(ata_delayed_attach, M_TEMP);
557 ata_delayed_attach = NULL;
562 * run through all ata devices and look for real ATA & ATAPI devices
563 * using the hints we found in the early probe, this avoids some of
564 * the delays probing of non-exsistent devices can cause.
566 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
567 if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
569 if (ch->devices & ATA_ATA_SLAVE)
570 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
571 ch->devices &= ~ATA_ATA_SLAVE;
572 if (ch->devices & ATA_ATAPI_SLAVE)
573 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
574 ch->devices &= ~ATA_ATAPI_SLAVE;
575 if (ch->devices & ATA_ATA_MASTER)
576 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
577 ch->devices &= ~ATA_ATA_MASTER;
578 if (ch->devices & ATA_ATAPI_MASTER)
579 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
580 ch->devices &= ~ATA_ATAPI_MASTER;
584 /* now we know whats there, do the real attach, first the ATA disks */
585 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
586 if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
588 if (ch->devices & ATA_ATA_MASTER)
589 ad_attach(&ch->device[MASTER], 0);
590 if (ch->devices & ATA_ATA_SLAVE)
591 ad_attach(&ch->device[SLAVE], 0);
596 /* then the atapi devices */
597 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
598 if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
600 if (ch->devices & ATA_ATAPI_MASTER)
601 atapi_attach(&ch->device[MASTER], 0);
602 if (ch->devices & ATA_ATAPI_SLAVE)
603 atapi_attach(&ch->device[SLAVE], 0);
605 atapi_cam_attach_bus(ch);
615 struct ata_channel *ch = (struct ata_channel *)data;
617 * on PCI systems we might share an interrupt line with another
618 * device or our twin ATA channel, so call ch->intr_func to figure
619 * out if it is really an interrupt we should process here
621 if (ch->intr_func && ch->intr_func(ch))
624 /* if drive is busy it didn't interrupt */
625 if (ATA_INB(ch->r_altio, ATA_ALTSTAT) & ATA_S_BUSY) {
627 if (!(ATA_INB(ch->r_altio, ATA_ALTSTAT) & ATA_S_DRQ))
631 /* clear interrupt and get status */
632 ch->status = ATA_INB(ch->r_io, ATA_STATUS);
634 if (ch->status & ATA_S_ERROR)
635 ch->error = ATA_INB(ch->r_io, ATA_ERROR);
637 /* find & call the responsible driver to process this interrupt */
638 switch (ch->active) {
641 if (!ch->running || ad_interrupt(ch->running) == ATA_OP_CONTINUES)
646 case ATA_ACTIVE_ATAPI:
647 if (!ch->running || atapi_interrupt(ch->running) == ATA_OP_CONTINUES)
652 case ATA_WAIT_INTR | ATA_CONTROL:
657 case ATA_WAIT_READY | ATA_CONTROL:
661 if (ch->flags & ATA_QUEUED) {
662 ch->active = ATA_ACTIVE;
663 if (ata_service(ch) == ATA_OP_CONTINUES)
671 static int intr_count = 0;
673 if (intr_count++ < 10)
674 ata_printf(ch, -1, "unwanted interrupt #%d active=%02x s=%02x\n",
675 intr_count, ch->active, ch->status);
680 ch->active &= ATA_CONTROL;
681 if (ch->active & ATA_CONTROL)
689 ata_start(struct ata_channel *ch)
692 struct ad_request *ad_request;
695 struct atapi_request *atapi_request;
698 crit_enter(); /* interlock non-atomic channel lock */
699 if (!ATA_LOCK_CH(ch, ATA_ACTIVE)) {
705 /* find & call the responsible driver if anything on the ATA queue */
706 if (TAILQ_EMPTY(&ch->ata_queue)) {
707 if (ch->devices & (ATA_ATA_MASTER) && ch->device[MASTER].driver)
708 ad_start(&ch->device[MASTER]);
709 if (ch->devices & (ATA_ATA_SLAVE) && ch->device[SLAVE].driver)
710 ad_start(&ch->device[SLAVE]);
712 if ((ad_request = TAILQ_FIRST(&ch->ata_queue))) {
713 TAILQ_REMOVE(&ch->ata_queue, ad_request, chain);
714 ch->active = ATA_ACTIVE_ATA;
715 ch->running = ad_request;
718 * The donecount had better be 0 here because the channel may not
719 * have retained the setup for the request (if a retry).
721 KKASSERT(ad_request->donecount == 0);
722 if (ad_transfer(ad_request) == ATA_OP_CONTINUES) {
730 /* find & call the responsible driver if anything on the ATAPI queue */
731 if (TAILQ_EMPTY(&ch->atapi_queue)) {
732 if (ch->devices & (ATA_ATAPI_MASTER) && ch->device[MASTER].driver)
733 atapi_start(&ch->device[MASTER]);
734 if (ch->devices & (ATA_ATAPI_SLAVE) && ch->device[SLAVE].driver)
735 atapi_start(&ch->device[SLAVE]);
737 if ((atapi_request = TAILQ_FIRST(&ch->atapi_queue))) {
738 TAILQ_REMOVE(&ch->atapi_queue, atapi_request, chain);
739 ch->active = ATA_ACTIVE_ATAPI;
740 ch->running = atapi_request;
741 if (atapi_transfer(atapi_request) == ATA_OP_CONTINUES) {
752 ata_reset(struct ata_channel *ch)
754 u_int8_t lsb, msb, ostat0, ostat1;
755 u_int8_t stat0 = 0, stat1 = 0;
756 int mask = 0, timeout;
758 /* do we have any signs of ATA/ATAPI HW being present ? */
759 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
761 ostat0 = ATA_INB(ch->r_io, ATA_STATUS);
762 if ((ostat0 & 0xf8) != 0xf8 && ostat0 != 0xa5) {
766 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
768 ostat1 = ATA_INB(ch->r_io, ATA_STATUS);
769 if ((ostat1 & 0xf8) != 0xf8 && ostat1 != 0xa5) {
778 /* in some setups we dont want to test for a slave */
779 if (ch->flags & ATA_NO_SLAVE) {
785 ata_printf(ch, -1, "mask=%02x ostat0=%02x ostat2=%02x\n",
786 mask, ostat0, ostat1);
789 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
791 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_RESET);
793 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_IDS);
795 ATA_INB(ch->r_io, ATA_ERROR);
797 /* wait for BUSY to go inactive */
798 for (timeout = 0; timeout < 3100; timeout++) {
799 if (stat0 & ATA_S_BUSY) {
800 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
803 /* check for ATAPI signature while its still there */
804 lsb = ATA_INB(ch->r_io, ATA_CYL_LSB);
805 msb = ATA_INB(ch->r_io, ATA_CYL_MSB);
806 stat0 = ATA_INB(ch->r_io, ATA_STATUS);
807 if (!(stat0 & ATA_S_BUSY)) {
809 ata_printf(ch, ATA_MASTER, "ATAPI %02x %02x\n", lsb, msb);
810 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB)
811 ch->devices |= ATA_ATAPI_MASTER;
814 if (stat1 & ATA_S_BUSY) {
815 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
818 /* check for ATAPI signature while its still there */
819 lsb = ATA_INB(ch->r_io, ATA_CYL_LSB);
820 msb = ATA_INB(ch->r_io, ATA_CYL_MSB);
821 stat1 = ATA_INB(ch->r_io, ATA_STATUS);
822 if (!(stat1 & ATA_S_BUSY)) {
824 ata_printf(ch, ATA_SLAVE, "ATAPI %02x %02x\n", lsb, msb);
825 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB)
826 ch->devices |= ATA_ATAPI_SLAVE;
829 if (mask == 0x01) /* wait for master only */
830 if (!(stat0 & ATA_S_BUSY))
832 if (mask == 0x02) /* wait for slave only */
833 if (!(stat1 & ATA_S_BUSY))
835 if (mask == 0x03) /* wait for both master & slave */
836 if (!(stat0 & ATA_S_BUSY) && !(stat1 & ATA_S_BUSY))
841 * some devices release BUSY before they are ready to accept commands.
842 * We must wait at least 50ms before attempting to issue a command after
846 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
848 if (stat0 & ATA_S_BUSY)
850 if (stat1 & ATA_S_BUSY)
853 ata_printf(ch, -1, "mask=%02x stat0=%02x stat1=%02x\n",
858 if (mask & 0x01 && ostat0 != 0x00 && !(ch->devices & ATA_ATAPI_MASTER)) {
859 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
861 ATA_OUTB(ch->r_io, ATA_ERROR, 0x58);
862 ATA_OUTB(ch->r_io, ATA_CYL_LSB, 0xa5);
863 lsb = ATA_INB(ch->r_io, ATA_ERROR);
864 msb = ATA_INB(ch->r_io, ATA_CYL_LSB);
866 ata_printf(ch, ATA_MASTER, "ATA %02x %02x\n", lsb, msb);
867 if (lsb != 0x58 && msb == 0xa5)
868 ch->devices |= ATA_ATA_MASTER;
870 if (mask & 0x02 && ostat1 != 0x00 && !(ch->devices & ATA_ATAPI_SLAVE)) {
871 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
873 ATA_OUTB(ch->r_io, ATA_ERROR, 0x58);
874 ATA_OUTB(ch->r_io, ATA_CYL_LSB, 0xa5);
875 lsb = ATA_INB(ch->r_io, ATA_ERROR);
876 msb = ATA_INB(ch->r_io, ATA_CYL_LSB);
878 ata_printf(ch, ATA_SLAVE, "ATA %02x %02x\n", lsb, msb);
879 if (lsb != 0x58 && msb == 0xa5)
880 ch->devices |= ATA_ATA_SLAVE;
883 ata_printf(ch, -1, "devices=%02x\n", ch->devices);
887 ata_reinit(struct ata_channel *ch)
889 int devices, misdev, newdev;
891 if (!ch->r_io || !ch->r_altio || !ch->r_irq)
894 ATA_FORCELOCK_CH(ch, ATA_CONTROL);
896 devices = ch->devices;
897 ata_printf(ch, -1, "resetting devices .. ");
900 if ((misdev = devices & ~ch->devices)) {
904 if (misdev & ATA_ATA_MASTER && ch->device[MASTER].driver)
905 ad_detach(&ch->device[MASTER], 0);
906 if (misdev & ATA_ATA_SLAVE && ch->device[SLAVE].driver)
907 ad_detach(&ch->device[SLAVE], 0);
910 if (misdev & ATA_ATAPI_MASTER && ch->device[MASTER].driver)
911 atapi_detach(&ch->device[MASTER]);
912 if (misdev & ATA_ATAPI_SLAVE && ch->device[SLAVE].driver)
913 atapi_detach(&ch->device[SLAVE]);
915 if (misdev & ATA_ATA_MASTER || misdev & ATA_ATAPI_MASTER) {
916 if (ch->device[MASTER].param)
917 kfree(ch->device[MASTER].param, M_ATA);
918 ch->device[MASTER].param = NULL;
920 if (misdev & ATA_ATA_SLAVE || misdev & ATA_ATAPI_SLAVE) {
921 if (ch->device[SLAVE].param)
922 kfree(ch->device[SLAVE].param, M_ATA);
923 ch->device[SLAVE].param = NULL;
926 if ((newdev = ~devices & ch->devices)) {
927 if (newdev & ATA_ATA_MASTER)
928 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
929 ch->devices &= ~ATA_ATA_MASTER;
930 if (newdev & ATA_ATA_SLAVE)
931 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
932 ch->devices &= ~ATA_ATA_SLAVE;
933 if (newdev & ATA_ATAPI_MASTER)
934 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
935 ch->devices &= ~ATA_ATAPI_MASTER;
936 if (newdev & ATA_ATAPI_SLAVE)
937 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
938 ch->devices &= ~ATA_ATAPI_SLAVE;
940 newdev = ~devices & ch->devices;
941 if (!misdev && newdev)
944 if (newdev & ATA_ATA_MASTER && !ch->device[MASTER].driver)
945 ad_attach(&ch->device[MASTER], 1);
946 else if (ch->devices & ATA_ATA_MASTER && ch->device[MASTER].driver) {
947 ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY);
948 ad_reinit(&ch->device[MASTER]);
950 if (newdev & ATA_ATA_SLAVE && !ch->device[SLAVE].driver)
951 ad_attach(&ch->device[SLAVE], 1);
952 else if (ch->devices & (ATA_ATA_SLAVE) && ch->device[SLAVE].driver) {
953 ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY);
954 ad_reinit(&ch->device[SLAVE]);
958 if (newdev & ATA_ATAPI_MASTER && !ch->device[MASTER].driver)
959 atapi_attach(&ch->device[MASTER], 1);
960 else if (ch->devices & (ATA_ATAPI_MASTER) && ch->device[MASTER].driver) {
961 ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY);
962 atapi_reinit(&ch->device[MASTER]);
964 if (newdev & ATA_ATAPI_SLAVE && !ch->device[SLAVE].driver)
965 atapi_attach(&ch->device[SLAVE], 1);
966 else if (ch->devices & (ATA_ATAPI_SLAVE) && ch->device[SLAVE].driver) {
967 ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY);
968 atapi_reinit(&ch->device[SLAVE]);
972 if (ch->devices & (ATA_ATAPI_MASTER | ATA_ATAPI_SLAVE))
973 atapi_cam_reinit_bus(ch);
982 ata_service(struct ata_channel *ch)
984 /* do we have a SERVICE request from the drive ? */
985 if ((ch->status & (ATA_S_SERVICE|ATA_S_ERROR|ATA_S_DRQ)) == ATA_S_SERVICE) {
986 ATA_OUTB(ch->r_bmio, ATA_BMSTAT_PORT,
987 ata_dmastatus(ch) | ATA_BMSTAT_INTERRUPT);
989 if ((ATA_INB(ch->r_io, ATA_DRIVE) & ATA_SLAVE) == ATA_MASTER) {
990 if ((ch->devices & ATA_ATA_MASTER) && ch->device[MASTER].driver)
991 return ad_service((struct ad_softc *)
992 ch->device[MASTER].driver, 0);
995 if ((ch->devices & ATA_ATA_SLAVE) && ch->device[SLAVE].driver)
996 return ad_service((struct ad_softc *)
997 ch->device[SLAVE].driver, 0);
1001 return ATA_OP_FINISHED;
1005 ata_wait(struct ata_device *atadev, u_int8_t mask)
1010 while (timeout < 5000000) { /* timeout 5 secs */
1011 atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS);
1013 /* if drive fails status, reselect the drive just to be sure */
1014 if (atadev->channel->status == 0xff) {
1015 ata_prtdev(atadev, "no status, reselecting device\n");
1016 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM|atadev->unit);
1018 atadev->channel->status = ATA_INB(atadev->channel->r_io,ATA_STATUS);
1019 if (atadev->channel->status == 0xff)
1024 if (!(atadev->channel->status & ATA_S_BUSY))
1027 if (timeout > 1000) {
1036 if (atadev->channel->status & ATA_S_ERROR)
1037 atadev->channel->error = ATA_INB(atadev->channel->r_io, ATA_ERROR);
1038 if (timeout >= 5000000)
1041 return (atadev->channel->status & ATA_S_ERROR);
1043 /* Wait 50 msec for bits wanted. */
1046 atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS);
1047 if ((atadev->channel->status & mask) == mask) {
1048 if (atadev->channel->status & ATA_S_ERROR)
1049 atadev->channel->error=ATA_INB(atadev->channel->r_io,ATA_ERROR);
1050 return (atadev->channel->status & ATA_S_ERROR);
1058 ata_command(struct ata_device *atadev, u_int8_t command,
1059 u_int64_t lba, u_int16_t count, u_int8_t feature, int flags)
1063 ata_prtdev(atadev, "ata_command: addr=%04lx, cmd=%02x, "
1064 "lba=%lld, count=%d, feature=%d, flags=%02x\n",
1065 rman_get_start(atadev->channel->r_io),
1066 command, lba, count, feature, flags);
1070 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1072 /* disable interrupt from device */
1073 if (atadev->channel->flags & ATA_QUEUED)
1074 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_4BIT);
1076 /* ready to issue command ? */
1077 if (ata_wait(atadev, 0) < 0) {
1078 ata_prtdev(atadev, "timeout sending command=%02x s=%02x e=%02x\n",
1079 command, atadev->channel->status, atadev->channel->error);
1083 /* only use 48bit addressing if needed because of the overhead */
1084 if ((lba >= 268435455 || count > 256) && atadev->param &&
1085 atadev->param->support.address48) {
1086 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, (feature>>8) & 0xff);
1087 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, feature);
1088 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, (count>>8) & 0xff);
1089 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, count & 0xff);
1090 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, (lba>>24) & 0xff);
1091 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, lba & 0xff);
1092 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>32) & 0xff);
1093 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>8) & 0xff);
1094 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>40) & 0xff);
1095 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>16) & 0xff);
1096 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_LBA | atadev->unit);
1098 /* translate command into 48bit version */
1101 command = ATA_C_READ48; break;
1102 case ATA_C_READ_MUL:
1103 command = ATA_C_READ_MUL48; break;
1104 case ATA_C_READ_DMA:
1105 command = ATA_C_READ_DMA48; break;
1106 case ATA_C_READ_DMA_QUEUED:
1107 command = ATA_C_READ_DMA_QUEUED48; break;
1109 command = ATA_C_WRITE48; break;
1110 case ATA_C_WRITE_MUL:
1111 command = ATA_C_WRITE_MUL48; break;
1112 case ATA_C_WRITE_DMA:
1113 command = ATA_C_WRITE_DMA48; break;
1114 case ATA_C_WRITE_DMA_QUEUED:
1115 command = ATA_C_WRITE_DMA_QUEUED48; break;
1116 case ATA_C_FLUSHCACHE:
1117 command = ATA_C_FLUSHCACHE48; break;
1119 ata_prtdev(atadev, "can't translate cmd to 48bit version\n");
1124 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, feature);
1125 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, count);
1126 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, lba & 0xff);
1127 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>8) & 0xff);
1128 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>16) & 0xff);
1129 if (atadev->flags & ATA_D_USE_CHS)
1130 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE,
1131 ATA_D_IBM | atadev->unit | ((lba>>24) & 0xf));
1133 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE,
1134 ATA_D_IBM | ATA_D_LBA | atadev->unit | ((lba>>24) &0xf));
1137 switch (flags & ATA_WAIT_MASK) {
1139 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1141 /* enable interrupt */
1142 if (atadev->channel->flags & ATA_QUEUED)
1143 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
1147 atadev->channel->active |= ATA_WAIT_INTR;
1148 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1150 /* enable interrupt */
1151 if (atadev->channel->flags & ATA_QUEUED)
1152 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
1154 if (tsleep((caddr_t)atadev->channel, 0, "atacmd", 10 * hz)) {
1155 ata_prtdev(atadev, "timeout waiting for interrupt\n");
1156 atadev->channel->active &= ~ATA_WAIT_INTR;
1161 case ATA_WAIT_READY:
1162 atadev->channel->active |= ATA_WAIT_READY;
1163 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1164 if (ata_wait(atadev, ATA_S_READY) < 0) {
1165 ata_prtdev(atadev, "timeout waiting for cmd=%02x s=%02x e=%02x\n",
1166 command, atadev->channel->status,atadev->channel->error);
1169 atadev->channel->active &= ~ATA_WAIT_READY;
1176 ata_enclosure_start(struct ata_device *atadev)
1178 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1180 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1182 ATA_INB(atadev->channel->r_io, ATA_CMD);
1184 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1186 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1188 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1190 ATA_INB(atadev->channel->r_io, ATA_COUNT);
1192 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1197 ata_enclosure_end(struct ata_device *atadev)
1199 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1204 ata_enclosure_chip_start(struct ata_device *atadev)
1206 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0b);
1207 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0a);
1209 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08);
1213 ata_enclosure_chip_end(struct ata_device *atadev)
1215 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08);
1217 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0a);
1219 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0b);
1224 ata_enclosure_chip_rdbit(struct ata_device *atadev)
1228 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0);
1230 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x02);
1232 val = ATA_INB(atadev->channel->r_io, ATA_SECTOR) & 0x01;
1238 ata_enclosure_chip_wrbit(struct ata_device *atadev, u_int8_t data)
1240 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08 | (data & 0x01));
1242 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08 | 0x02 | (data & 0x01));
1247 ata_enclosure_chip_rw(struct ata_device *atadev, int rw, u_int8_t val)
1252 for (i = 0; i < 8; i++)
1253 ata_enclosure_chip_wrbit(atadev, (val & (0x80 >> i)) ? 1 : 0);
1256 for (i = 0; i < 8; i++)
1257 val = (val << 1) | ata_enclosure_chip_rdbit(atadev);
1259 ata_enclosure_chip_wrbit(atadev, 0);
1264 ata_enclosure_sensor(struct ata_device *atadev,
1265 int rw, u_int8_t idx, u_int8_t data)
1267 ata_enclosure_start(atadev);
1268 ata_enclosure_chip_start(atadev);
1269 ata_enclosure_chip_rw(atadev, 1, 0x5a);
1270 ata_enclosure_chip_rw(atadev, 1, idx);
1272 ata_enclosure_chip_rw(atadev, 1, data);
1275 ata_enclosure_chip_end(atadev);
1276 ata_enclosure_chip_start(atadev);
1277 ata_enclosure_chip_rw(atadev, 1, 0x5b);
1278 data = ata_enclosure_chip_rw(atadev, 0, 0);
1280 ata_enclosure_chip_end(atadev);
1281 ata_enclosure_end(atadev);
1286 ata_enclosure_status(struct ata_device *atadev,
1287 int *fan, int *temp, int *v05, int *v12)
1289 u_int8_t id1, id2, cnt, div;
1292 if (atadev->flags & ATA_D_ENC_PRESENT) {
1293 ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL);
1294 ata_enclosure_sensor(atadev, 1, 0x4e, 0);
1295 id1 = ata_enclosure_sensor(atadev, 0, 0x4f, 0);
1296 ata_enclosure_sensor(atadev, 1, 0x4e, 0x80);
1297 id2 = ata_enclosure_sensor(atadev, 0, 0x4f, 0);
1298 if (id1 == 0xa3 && id2 == 0x5c) {
1299 div = 1 << (((ata_enclosure_sensor(atadev, 0, 0x5d, 0)&0x20)>>3)+
1300 ((ata_enclosure_sensor(atadev, 0, 0x47, 0)&0x30)>>4)+1);
1301 cnt = ata_enclosure_sensor(atadev, 0, 0x28, 0);
1305 *fan = 1350000 / cnt / div;
1306 ata_enclosure_sensor(atadev, 1, 0x4e, 0x01);
1307 *temp = (ata_enclosure_sensor(atadev, 0, 0x50, 0) * 10) +
1308 (ata_enclosure_sensor(atadev, 0, 0x50, 0) & 0x80 ? 5 : 0);
1309 *v05 = ata_enclosure_sensor(atadev, 0, 0x23, 0) * 27;
1310 *v12 = ata_enclosure_sensor(atadev, 0, 0x24, 0) * 61;
1313 ATA_UNLOCK_CH(atadev->channel);
1319 ata_enclosure_print(struct ata_device *atadev)
1322 int fan, temp, v05, v12;
1324 ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL);
1325 ata_enclosure_start(atadev);
1326 id = ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1328 st = ATA_INB(atadev->channel->r_io, ATA_COUNT);
1330 ata_enclosure_end(atadev);
1331 ATA_UNLOCK_CH(atadev->channel);
1333 switch (id & 0x93) {
1335 ata_prtdev(atadev, "Universal enclosure");
1338 ata_prtdev(atadev, "FastSwap enclosure");
1342 ata_prtdev(atadev, "SuperSwap enclosure");
1345 atadev->flags &= ~ATA_D_ENC_PRESENT;
1348 atadev->flags |= ATA_D_ENC_PRESENT;
1350 if (ata_enclosure_status(atadev, &fan, &temp, &v05, &v12))
1351 printf(" detected\n");
1353 printf(" [FAN:%drpm TEMP:%d.%01dC %d.%03dV %d.%03dV]\n",
1354 fan, temp/10, temp%10, v05/1000, v05%1000, v12/1000, v12%1000);
1358 ata_enclosure_leds(struct ata_device *atadev, u_int8_t color)
1360 if (atadev->flags & ATA_D_ENC_PRESENT) {
1363 ata_enclosure_start(atadev);
1364 reg = ATA_INB(atadev->channel->r_io, ATA_COUNT);
1366 ATA_OUTB(atadev->channel->r_io, ATA_COUNT,
1367 (color & ATA_LED_MASK) | (reg & ~ATA_LED_MASK));
1369 ata_enclosure_end(atadev);
1374 ata_change_mode(struct ata_device *atadev, int mode)
1376 int umode, wmode, pmode;
1378 umode = ata_umode(atadev->param);
1379 wmode = ata_wmode(atadev->param);
1380 pmode = ata_pmode(atadev->param);
1382 switch (mode & ATA_DMA_MASK) {
1384 if ((mode & ATA_MODE_MASK) < umode)
1385 umode = mode & ATA_MODE_MASK;
1388 if ((mode & ATA_MODE_MASK) < wmode)
1389 wmode = mode & ATA_MODE_MASK;
1393 if (((mode & ATA_MODE_MASK) - ATA_PIO0) < pmode)
1394 pmode = (mode & ATA_MODE_MASK) - ATA_PIO0;
1399 crit_enter(); /* interlock non-atomic channel lock */
1400 ATA_SLEEPLOCK_CH(atadev->channel, ATA_ACTIVE);
1401 ata_dmainit(atadev, pmode, wmode, umode);
1402 ATA_UNLOCK_CH(atadev->channel);
1404 ata_start(atadev->channel); /* XXX SOS */
1408 ata_printf(struct ata_channel *ch, int device, const char * fmt, ...)
1414 ret = printf("ata%d: ", device_get_unit(ch->dev));
1416 if (ch->device[ATA_DEV(device)].name)
1417 ret = printf("%s: ", ch->device[ATA_DEV(device)].name);
1419 ret = printf("ata%d-%s: ", device_get_unit(ch->dev),
1420 (device == ATA_MASTER) ? "master" : "slave");
1422 __va_start(ap, fmt);
1423 ret += kvprintf(fmt, ap);
1429 ata_prtdev(struct ata_device *atadev, const char * fmt, ...)
1435 ret = printf("%s: ", atadev->name);
1437 ret = printf("ata%d-%s: ", device_get_unit(atadev->channel->dev),
1438 (atadev->unit == ATA_MASTER) ? "master" : "slave");
1439 __va_start(ap, fmt);
1440 ret += kvprintf(fmt, ap);
1446 ata_set_name(struct ata_device *atadev, char *name, int lun)
1448 atadev->name = kmalloc(strlen(name) + 4, M_ATA, M_INTWAIT);
1449 ksprintf(atadev->name, "%s%d", name, lun);
1453 ata_free_name(struct ata_device *atadev)
1456 kfree(atadev->name, M_ATA);
1457 atadev->name = NULL;
1461 ata_get_lun(u_int32_t *map)
1463 int lun = ffs(~*map) - 1;
1470 ata_test_lun(u_int32_t *map, int lun)
1472 return (*map & (1 << lun));
1476 ata_free_lun(u_int32_t *map, int lun)
1478 *map &= ~(1 << lun);
1482 ata_mode2str(int mode)
1485 case ATA_PIO: return "BIOSPIO";
1486 case ATA_PIO0: return "PIO0";
1487 case ATA_PIO1: return "PIO1";
1488 case ATA_PIO2: return "PIO2";
1489 case ATA_PIO3: return "PIO3";
1490 case ATA_PIO4: return "PIO4";
1491 case ATA_DMA: return "BIOSDMA";
1492 case ATA_WDMA2: return "WDMA2";
1493 case ATA_UDMA2: return "UDMA33";
1494 case ATA_UDMA4: return "UDMA66";
1495 case ATA_UDMA5: return "UDMA100";
1496 case ATA_UDMA6: return "UDMA133";
1497 default: return "???";
1502 ata_pmode(struct ata_params *ap)
1504 if (ap->atavalid & ATA_FLAG_64_70) {
1505 if (ap->apiomodes & 2)
1507 if (ap->apiomodes & 1)
1510 if (ap->retired_piomode == 2)
1512 if (ap->retired_piomode == 1)
1514 if (ap->retired_piomode == 0)
1520 ata_wmode(struct ata_params *ap)
1522 if (ap->mwdmamodes & 0x04)
1524 if (ap->mwdmamodes & 0x02)
1526 if (ap->mwdmamodes & 0x01)
1532 ata_umode(struct ata_params *ap)
1534 if (ap->atavalid & ATA_FLAG_88) {
1535 if (ap->udmamodes & 0x40)
1537 if (ap->udmamodes & 0x20)
1539 if (ap->udmamodes & 0x10)
1541 if (ap->udmamodes & 0x08)
1543 if (ap->udmamodes & 0x04)
1545 if (ap->udmamodes & 0x02)
1547 if (ap->udmamodes & 0x01)
1554 bswap(int8_t *buf, int len)
1556 u_int16_t *ptr = (u_int16_t*)(buf + len);
1558 while (--ptr >= (u_int16_t*)buf)
1563 btrim(int8_t *buf, int len)
1567 for (ptr = buf; ptr < buf+len; ++ptr)
1570 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
1575 bpack(int8_t *src, int8_t *dst, int len)
1579 for (i = j = blank = 0 ; i < len; i++) {
1580 if (blank && src[i] == ' ') continue;
1581 if (blank && src[i] != ' ') {
1586 if (src[i] == ' ') {
1600 /* register controlling device */
1601 dev_ops_add(&ata_ops, 0, 0);
1602 make_dev(&ata_ops, 0, UID_ROOT, GID_OPERATOR, 0600, "ata");
1604 /* register boot attach to be run when interrupts are enabled */
1605 ata_delayed_attach = kmalloc(sizeof(struct intr_config_hook),
1606 M_TEMP, M_WAITOK | M_ZERO);
1608 ata_delayed_attach->ich_func = (void*)ata_boot_attach;
1609 ata_delayed_attach->ich_desc = "ata";
1610 if (config_intrhook_establish(ata_delayed_attach) != 0) {
1611 printf("ata: config_intrhook_establish failed\n");
1612 kfree(ata_delayed_attach, M_TEMP);
1615 SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL)