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.36 2007/05/01 00:05:16 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 MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer");
104 #define DEV_ATAPIALL NATAPICD > 0 || NATAPIFD > 0 || \
105 NATAPIST > 0 || NATAPICAM > 0
108 ata_probe(device_t dev)
110 struct ata_channel *ch;
113 if (!dev || !(ch = device_get_softc(dev)))
116 if (ch->r_io || ch->r_altio || ch->r_irq)
119 /* initialize the softc basics */
120 ch->active = ATA_IDLE;
123 rid = ATA_IOADDR_RID;
124 ch->r_io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
125 ATA_IOSIZE, RF_ACTIVE);
129 rid = ATA_ALTADDR_RID;
130 ch->r_altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
131 ATA_ALTIOSIZE, RF_ACTIVE);
135 rid = ATA_BMADDR_RID;
136 ch->r_bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
137 ATA_BMIOSIZE, RF_ACTIVE);
139 ata_printf(ch, -1, "iobase=0x%04x altiobase=0x%04x bmaddr=0x%04x\n",
140 (int)rman_get_start(ch->r_io),
141 (int)rman_get_start(ch->r_altio),
142 (ch->r_bmio) ? (int)rman_get_start(ch->r_bmio) : 0);
146 ch->device[MASTER].channel = ch;
147 ch->device[MASTER].unit = ATA_MASTER;
148 ch->device[MASTER].mode = ATA_PIO;
149 ch->device[SLAVE].channel = ch;
150 ch->device[SLAVE].unit = ATA_SLAVE;
151 ch->device[SLAVE].mode = ATA_PIO;
152 TAILQ_INIT(&ch->ata_queue);
153 TAILQ_INIT(&ch->atapi_queue);
155 mpipe_init(&ch->req_mpipe, M_ATA, sizeof(union ata_request),
156 4, ata_mpipe_size, 0, NULL);
157 mpipe_init(&ch->dma_mpipe, M_DEVBUF, PAGE_SIZE,
158 4, ata_mpipe_size, MPF_NOZERO, NULL);
164 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ch->r_io);
166 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ch->r_altio);
168 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, ch->r_bmio);
170 ata_printf(ch, -1, "probe allocation failed\n");
175 ata_attach(device_t dev)
177 struct ata_channel *ch;
180 if (!dev || !(ch = device_get_softc(dev)))
184 ch->r_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
185 RF_SHAREABLE | RF_ACTIVE);
187 ata_printf(ch, -1, "unable to allocate interrupt\n");
192 * Traditional ata registers are sensitive to when they can be accessed
193 * in the face of e.g. ongoing DMA. Do not allow the interrupt to be
196 if ((error = bus_setup_intr(dev, ch->r_irq, INTR_NOPOLL,
197 ata_intr, ch, &ch->ih, NULL))) {
198 ata_printf(ch, -1, "unable to setup interrupt\n");
203 * do not attach devices if we are in early boot, this is done later
204 * when interrupts are enabled by a hook into the boot process.
205 * otherwise attach what the probe has found in ch->devices.
209 if (ch->devices & ATA_ATA_SLAVE)
210 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
211 ch->devices &= ~ATA_ATA_SLAVE;
212 if (ch->devices & ATA_ATAPI_SLAVE)
213 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
214 ch->devices &= ~ATA_ATAPI_SLAVE;
215 if (ch->devices & ATA_ATA_MASTER)
216 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
217 ch->devices &= ~ATA_ATA_MASTER;
218 if (ch->devices & ATA_ATAPI_MASTER)
219 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
220 ch->devices &= ~ATA_ATAPI_MASTER;
222 if (ch->devices & ATA_ATA_MASTER)
223 ad_attach(&ch->device[MASTER], 0);
224 if (ch->devices & ATA_ATA_SLAVE)
225 ad_attach(&ch->device[SLAVE], 0);
228 if (ch->devices & ATA_ATAPI_MASTER)
229 atapi_attach(&ch->device[MASTER], 0);
230 if (ch->devices & ATA_ATAPI_SLAVE)
231 atapi_attach(&ch->device[SLAVE], 0);
234 atapi_cam_attach_bus(ch);
240 ata_detach(device_t dev)
242 struct ata_channel *ch;
244 if (!dev || !(ch = device_get_softc(dev)) ||
245 !ch->r_io || !ch->r_altio || !ch->r_irq)
248 /* make sure channel is not busy */
250 ATA_SLEEPLOCK_CH(ch, ATA_CONTROL);
252 if (ch->devices & ATA_ATA_MASTER && ch->device[MASTER].driver)
253 ad_detach(&ch->device[MASTER], 1);
254 if (ch->devices & ATA_ATA_SLAVE && ch->device[SLAVE].driver)
255 ad_detach(&ch->device[SLAVE], 1);
258 if (ch->devices & ATA_ATAPI_MASTER && ch->device[MASTER].driver)
259 atapi_detach(&ch->device[MASTER]);
260 if (ch->devices & ATA_ATAPI_SLAVE && ch->device[SLAVE].driver)
261 atapi_detach(&ch->device[SLAVE]);
264 atapi_cam_detach_bus(ch);
268 if (ch->device[MASTER].param) {
269 kfree(ch->device[MASTER].param, M_ATA);
270 ch->device[MASTER].param = NULL;
272 if (ch->device[SLAVE].param) {
273 kfree(ch->device[SLAVE].param, M_ATA);
274 ch->device[SLAVE].param = NULL;
276 ch->device[MASTER].driver = NULL;
277 ch->device[SLAVE].driver = NULL;
278 ch->device[MASTER].mode = ATA_PIO;
279 ch->device[SLAVE].mode = ATA_PIO;
283 bus_teardown_intr(dev, ch->r_irq, ch->ih);
284 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
286 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, ch->r_bmio);
287 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ch->r_altio);
288 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ch->r_io);
293 mpipe_done(&ch->req_mpipe);
294 mpipe_done(&ch->dma_mpipe);
301 ata_suspend(device_t dev)
303 struct ata_channel *ch;
305 if (dev == NULL || (ch = device_get_softc(dev)) == NULL)
308 /* wait for the channel to be IDLE or detached before suspending */
311 if (ch->active == ATA_IDLE) {
312 ch->active = ATA_CONTROL;
317 tsleep(ch, 0, "atasusp", hz / 10);
323 ata_resume(device_t dev)
325 return ata_reinit(device_get_softc(dev));
329 ataioctl(struct dev_ioctl_args *ap)
331 struct ata_cmd *iocmd = (struct ata_cmd *)ap->a_data;
332 struct ata_channel *ch;
333 device_t device = devclass_get_device(ata_devclass, iocmd->channel);
336 if (ap->a_cmd != IOCATA)
339 if (iocmd->channel < -1 || iocmd->device < -1 || iocmd->device > SLAVE)
342 switch (iocmd->cmd) {
344 /* should enable channel HW on controller that can SOS XXX */
345 error = ata_probe(device);
347 error = ata_attach(device);
351 error = ata_detach(device);
352 /* should disable channel HW on controller that can SOS XXX */
356 if (!device || !(ch = device_get_softc(device)))
358 crit_enter(); /* interlock non-atomic channel lock */
359 ATA_SLEEPLOCK_CH(ch, ATA_ACTIVE);
360 if ((error = ata_reinit(ch)))
366 if (!device || !(ch = device_get_softc(device)))
369 if ((iocmd->device == MASTER || iocmd->device == -1) &&
370 ch->device[MASTER].driver)
371 iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode;
373 iocmd->u.mode.mode[MASTER] = -1;
375 if ((iocmd->device == SLAVE || iocmd->device == -1) &&
376 ch->device[SLAVE].param)
377 iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode;
379 iocmd->u.mode.mode[SLAVE] = -1;
383 if (!device || !(ch = device_get_softc(device)))
386 if ((iocmd->device == MASTER || iocmd->device == -1) &&
387 iocmd->u.mode.mode[MASTER] >= 0 && ch->device[MASTER].param) {
388 ata_change_mode(&ch->device[MASTER],iocmd->u.mode.mode[MASTER]);
389 iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode;
392 iocmd->u.mode.mode[MASTER] = -1;
394 if ((iocmd->device == SLAVE || iocmd->device == -1) &&
395 iocmd->u.mode.mode[SLAVE] >= 0 && ch->device[SLAVE].param) {
396 ata_change_mode(&ch->device[SLAVE], iocmd->u.mode.mode[SLAVE]);
397 iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode;
400 iocmd->u.mode.mode[SLAVE] = -1;
404 if (!device || !(ch = device_get_softc(device)))
407 iocmd->u.param.type[MASTER] =
408 ch->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER);
409 iocmd->u.param.type[SLAVE] =
410 ch->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE);
412 if (ch->device[MASTER].name)
413 strcpy(iocmd->u.param.name[MASTER], ch->device[MASTER].name);
414 if (ch->device[SLAVE].name)
415 strcpy(iocmd->u.param.name[SLAVE], ch->device[SLAVE].name);
417 if (ch->device[MASTER].param)
418 bcopy(ch->device[MASTER].param, &iocmd->u.param.params[MASTER],
419 sizeof(struct ata_params));
420 if (ch->device[SLAVE].param)
421 bcopy(ch->device[SLAVE].param, &iocmd->u.param.params[SLAVE],
422 sizeof(struct ata_params));
426 struct ata_device *atadev;
428 if (!device || !(ch = device_get_softc(device)))
431 if (iocmd->device == SLAVE)
432 atadev = &ch->device[SLAVE];
434 atadev = &ch->device[MASTER];
436 return ata_enclosure_status(atadev,
437 &iocmd->u.enclosure.fan,
438 &iocmd->u.enclosure.temp,
439 &iocmd->u.enclosure.v05,
440 &iocmd->u.enclosure.v12);
445 return ata_raid_rebuild(iocmd->channel);
448 return ata_raid_create(&iocmd->u.raid_setup);
451 return ata_raid_delete(iocmd->channel);
454 return ata_raid_status(iocmd->channel, &iocmd->u.raid_status);
458 struct ata_device *atadev;
461 if (!device || !(ch = device_get_softc(device)))
464 if (!(atadev = &ch->device[iocmd->device]) ||
465 !(ch->devices & (iocmd->device == MASTER ?
466 ATA_ATAPI_MASTER : ATA_ATAPI_SLAVE)))
469 buf = kmalloc(iocmd->u.atapi.count, M_ATA, M_INTWAIT);
471 if (iocmd->u.atapi.flags & ATAPI_CMD_WRITE) {
472 error = copyin(iocmd->u.atapi.data, buf, iocmd->u.atapi.count);
476 error = atapi_queue_cmd(atadev, iocmd->u.atapi.ccb,
477 buf, iocmd->u.atapi.count,
478 (iocmd->u.atapi.flags == ATAPI_CMD_READ ?
479 ATPR_F_READ : 0) | ATPR_F_QUIET,
480 iocmd->u.atapi.timeout, NULL, NULL);
482 iocmd->u.atapi.error = error;
483 bcopy(&atadev->result, iocmd->u.atapi.sense_data,
484 sizeof(struct atapi_reqsense));
487 else if (iocmd->u.atapi.flags & ATAPI_CMD_READ)
488 error = copyout(buf, iocmd->u.atapi.data, iocmd->u.atapi.count);
501 ata_getparam(struct ata_device *atadev, u_int8_t command)
503 struct ata_params *ata_parm;
506 ata_parm = kmalloc(sizeof(struct ata_params), M_ATA, M_INTWAIT);
508 /* apparently some devices needs this repeated */
510 if (ata_command(atadev, command, 0, 0, 0, ATA_IMMEDIATE)) {
511 ata_prtdev(atadev, "%s identify failed\n",
512 command == ATA_C_ATAPI_IDENTIFY ? "ATAPI" : "ATA");
513 kfree(ata_parm, M_ATA);
517 ata_prtdev(atadev, "%s identify retries exceeded\n",
518 command == ATA_C_ATAPI_IDENTIFY ? "ATAPI" : "ATA");
519 kfree(ata_parm, M_ATA);
522 } while (ata_wait(atadev, ((command == ATA_C_ATAPI_IDENTIFY) ?
523 ATA_S_DRQ : (ATA_S_READY|ATA_S_DSC|ATA_S_DRQ))));
524 ATA_INSW(atadev->channel->r_io, ATA_DATA, (int16_t *)ata_parm,
525 sizeof(struct ata_params)/sizeof(int16_t));
527 if (command == ATA_C_ATA_IDENTIFY ||
528 !((ata_parm->model[0] == 'N' && ata_parm->model[1] == 'E') ||
529 (ata_parm->model[0] == 'F' && ata_parm->model[1] == 'X') ||
530 (ata_parm->model[0] == 'P' && ata_parm->model[1] == 'i')))
531 bswap(ata_parm->model, sizeof(ata_parm->model));
532 btrim(ata_parm->model, sizeof(ata_parm->model));
533 bpack(ata_parm->model, ata_parm->model, sizeof(ata_parm->model));
534 bswap(ata_parm->revision, sizeof(ata_parm->revision));
535 btrim(ata_parm->revision, sizeof(ata_parm->revision));
536 bpack(ata_parm->revision, ata_parm->revision, sizeof(ata_parm->revision));
537 bswap(ata_parm->serial, sizeof(ata_parm->serial));
538 btrim(ata_parm->serial, sizeof(ata_parm->serial));
539 bpack(ata_parm->serial, ata_parm->serial, sizeof(ata_parm->serial));
540 atadev->param = ata_parm;
545 ata_boot_attach(void)
547 struct ata_channel *ch;
553 * run through all ata devices and look for real ATA & ATAPI devices
554 * using the hints we found in the early probe, this avoids some of
555 * the delays probing of non-exsistent devices can cause.
557 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
558 if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
560 if (ch->devices & ATA_ATA_SLAVE)
561 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
562 ch->devices &= ~ATA_ATA_SLAVE;
563 if (ch->devices & ATA_ATAPI_SLAVE)
564 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
565 ch->devices &= ~ATA_ATAPI_SLAVE;
566 if (ch->devices & ATA_ATA_MASTER)
567 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
568 ch->devices &= ~ATA_ATA_MASTER;
569 if (ch->devices & ATA_ATAPI_MASTER)
570 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
571 ch->devices &= ~ATA_ATAPI_MASTER;
575 /* now we know whats there, do the real attach, first the ATA disks */
576 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
577 if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
579 if (ch->devices & ATA_ATA_MASTER)
580 ad_attach(&ch->device[MASTER], 0);
581 if (ch->devices & ATA_ATA_SLAVE)
582 ad_attach(&ch->device[SLAVE], 0);
587 /* then the atapi devices */
588 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
589 if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
591 if (ch->devices & ATA_ATAPI_MASTER)
592 atapi_attach(&ch->device[MASTER], 0);
593 if (ch->devices & ATA_ATAPI_SLAVE)
594 atapi_attach(&ch->device[SLAVE], 0);
596 atapi_cam_attach_bus(ch);
606 struct ata_channel *ch = (struct ata_channel *)data;
608 * on PCI systems we might share an interrupt line with another
609 * device or our twin ATA channel, so call ch->intr_func to figure
610 * out if it is really an interrupt we should process here
612 if (ch->intr_func && ch->intr_func(ch))
615 /* if drive is busy it didn't interrupt */
616 if (ATA_INB(ch->r_altio, ATA_ALTSTAT) & ATA_S_BUSY) {
618 if (!(ATA_INB(ch->r_altio, ATA_ALTSTAT) & ATA_S_DRQ))
622 /* clear interrupt and get status */
623 ch->status = ATA_INB(ch->r_io, ATA_STATUS);
625 if (ch->status & ATA_S_ERROR)
626 ch->error = ATA_INB(ch->r_io, ATA_ERROR);
628 /* find & call the responsible driver to process this interrupt */
629 switch (ch->active) {
632 if (!ch->running || ad_interrupt(ch->running) == ATA_OP_CONTINUES)
637 case ATA_ACTIVE_ATAPI:
638 if (!ch->running || atapi_interrupt(ch->running) == ATA_OP_CONTINUES)
643 case ATA_WAIT_INTR | ATA_CONTROL:
648 case ATA_WAIT_READY | ATA_CONTROL:
652 if (ch->flags & ATA_QUEUED) {
653 ch->active = ATA_ACTIVE;
654 if (ata_service(ch) == ATA_OP_CONTINUES)
662 static int intr_count = 0;
664 if (intr_count++ < 10)
665 ata_printf(ch, -1, "unwanted interrupt #%d active=%02x s=%02x\n",
666 intr_count, ch->active, ch->status);
671 ch->active &= ATA_CONTROL;
672 if (ch->active & ATA_CONTROL)
680 ata_start(struct ata_channel *ch)
683 struct ad_request *ad_request;
686 struct atapi_request *atapi_request;
689 crit_enter(); /* interlock non-atomic channel lock */
690 if (!ATA_LOCK_CH(ch, ATA_ACTIVE)) {
696 /* find & call the responsible driver if anything on the ATA queue */
697 if (TAILQ_EMPTY(&ch->ata_queue)) {
698 if (ch->devices & (ATA_ATA_MASTER) && ch->device[MASTER].driver)
699 ad_start(&ch->device[MASTER]);
700 if (ch->devices & (ATA_ATA_SLAVE) && ch->device[SLAVE].driver)
701 ad_start(&ch->device[SLAVE]);
703 if ((ad_request = TAILQ_FIRST(&ch->ata_queue))) {
704 TAILQ_REMOVE(&ch->ata_queue, ad_request, chain);
705 ch->active = ATA_ACTIVE_ATA;
706 ch->running = ad_request;
709 * The donecount had better be 0 here because the channel may not
710 * have retained the setup for the request (if a retry).
712 KKASSERT(ad_request->donecount == 0);
713 if (ad_transfer(ad_request) == ATA_OP_CONTINUES) {
721 /* find & call the responsible driver if anything on the ATAPI queue */
722 if (TAILQ_EMPTY(&ch->atapi_queue)) {
723 if (ch->devices & (ATA_ATAPI_MASTER) && ch->device[MASTER].driver)
724 atapi_start(&ch->device[MASTER]);
725 if (ch->devices & (ATA_ATAPI_SLAVE) && ch->device[SLAVE].driver)
726 atapi_start(&ch->device[SLAVE]);
728 if ((atapi_request = TAILQ_FIRST(&ch->atapi_queue))) {
729 TAILQ_REMOVE(&ch->atapi_queue, atapi_request, chain);
730 ch->active = ATA_ACTIVE_ATAPI;
731 ch->running = atapi_request;
732 if (atapi_transfer(atapi_request) == ATA_OP_CONTINUES) {
743 ata_reset(struct ata_channel *ch)
745 u_int8_t lsb, msb, ostat0, ostat1;
746 u_int8_t stat0 = 0, stat1 = 0;
747 int mask = 0, timeout;
749 /* do we have any signs of ATA/ATAPI HW being present ? */
750 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
752 ostat0 = ATA_INB(ch->r_io, ATA_STATUS);
753 if ((ostat0 & 0xf8) != 0xf8 && ostat0 != 0xa5) {
757 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
759 ostat1 = ATA_INB(ch->r_io, ATA_STATUS);
760 if ((ostat1 & 0xf8) != 0xf8 && ostat1 != 0xa5) {
769 /* in some setups we dont want to test for a slave */
770 if (ch->flags & ATA_NO_SLAVE) {
776 ata_printf(ch, -1, "mask=%02x ostat0=%02x ostat2=%02x\n",
777 mask, ostat0, ostat1);
780 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
782 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_RESET);
784 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_IDS);
786 ATA_INB(ch->r_io, ATA_ERROR);
788 /* wait for BUSY to go inactive */
789 for (timeout = 0; timeout < 3100; timeout++) {
790 if (stat0 & ATA_S_BUSY) {
791 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
794 /* check for ATAPI signature while its still there */
795 lsb = ATA_INB(ch->r_io, ATA_CYL_LSB);
796 msb = ATA_INB(ch->r_io, ATA_CYL_MSB);
797 stat0 = ATA_INB(ch->r_io, ATA_STATUS);
798 if (!(stat0 & ATA_S_BUSY)) {
800 ata_printf(ch, ATA_MASTER, "ATAPI %02x %02x\n", lsb, msb);
801 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB)
802 ch->devices |= ATA_ATAPI_MASTER;
805 if (stat1 & ATA_S_BUSY) {
806 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
809 /* check for ATAPI signature while its still there */
810 lsb = ATA_INB(ch->r_io, ATA_CYL_LSB);
811 msb = ATA_INB(ch->r_io, ATA_CYL_MSB);
812 stat1 = ATA_INB(ch->r_io, ATA_STATUS);
813 if (!(stat1 & ATA_S_BUSY)) {
815 ata_printf(ch, ATA_SLAVE, "ATAPI %02x %02x\n", lsb, msb);
816 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB)
817 ch->devices |= ATA_ATAPI_SLAVE;
820 if (mask == 0x01) /* wait for master only */
821 if (!(stat0 & ATA_S_BUSY))
823 if (mask == 0x02) /* wait for slave only */
824 if (!(stat1 & ATA_S_BUSY))
826 if (mask == 0x03) /* wait for both master & slave */
827 if (!(stat0 & ATA_S_BUSY) && !(stat1 & ATA_S_BUSY))
832 * some devices release BUSY before they are ready to accept commands.
833 * We must wait at least 50ms before attempting to issue a command after
837 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
839 if (stat0 & ATA_S_BUSY)
841 if (stat1 & ATA_S_BUSY)
844 ata_printf(ch, -1, "mask=%02x stat0=%02x stat1=%02x\n",
849 if (mask & 0x01 && ostat0 != 0x00 && !(ch->devices & ATA_ATAPI_MASTER)) {
850 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
852 ATA_OUTB(ch->r_io, ATA_ERROR, 0x58);
853 ATA_OUTB(ch->r_io, ATA_CYL_LSB, 0xa5);
854 lsb = ATA_INB(ch->r_io, ATA_ERROR);
855 msb = ATA_INB(ch->r_io, ATA_CYL_LSB);
857 ata_printf(ch, ATA_MASTER, "ATA %02x %02x\n", lsb, msb);
858 if (lsb != 0x58 && msb == 0xa5)
859 ch->devices |= ATA_ATA_MASTER;
861 if (mask & 0x02 && ostat1 != 0x00 && !(ch->devices & ATA_ATAPI_SLAVE)) {
862 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
864 ATA_OUTB(ch->r_io, ATA_ERROR, 0x58);
865 ATA_OUTB(ch->r_io, ATA_CYL_LSB, 0xa5);
866 lsb = ATA_INB(ch->r_io, ATA_ERROR);
867 msb = ATA_INB(ch->r_io, ATA_CYL_LSB);
869 ata_printf(ch, ATA_SLAVE, "ATA %02x %02x\n", lsb, msb);
870 if (lsb != 0x58 && msb == 0xa5)
871 ch->devices |= ATA_ATA_SLAVE;
874 ata_printf(ch, -1, "devices=%02x\n", ch->devices);
878 ata_reinit(struct ata_channel *ch)
880 int devices, misdev, newdev;
882 if (!ch->r_io || !ch->r_altio || !ch->r_irq)
885 ATA_FORCELOCK_CH(ch, ATA_CONTROL);
887 devices = ch->devices;
888 ata_printf(ch, -1, "resetting devices .. ");
891 if ((misdev = devices & ~ch->devices)) {
895 if (misdev & ATA_ATA_MASTER && ch->device[MASTER].driver)
896 ad_detach(&ch->device[MASTER], 0);
897 if (misdev & ATA_ATA_SLAVE && ch->device[SLAVE].driver)
898 ad_detach(&ch->device[SLAVE], 0);
901 if (misdev & ATA_ATAPI_MASTER && ch->device[MASTER].driver)
902 atapi_detach(&ch->device[MASTER]);
903 if (misdev & ATA_ATAPI_SLAVE && ch->device[SLAVE].driver)
904 atapi_detach(&ch->device[SLAVE]);
906 if (misdev & ATA_ATA_MASTER || misdev & ATA_ATAPI_MASTER) {
907 if (ch->device[MASTER].param)
908 kfree(ch->device[MASTER].param, M_ATA);
909 ch->device[MASTER].param = NULL;
911 if (misdev & ATA_ATA_SLAVE || misdev & ATA_ATAPI_SLAVE) {
912 if (ch->device[SLAVE].param)
913 kfree(ch->device[SLAVE].param, M_ATA);
914 ch->device[SLAVE].param = NULL;
917 if ((newdev = ~devices & ch->devices)) {
918 if (newdev & ATA_ATA_MASTER)
919 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
920 ch->devices &= ~ATA_ATA_MASTER;
921 if (newdev & ATA_ATA_SLAVE)
922 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
923 ch->devices &= ~ATA_ATA_SLAVE;
924 if (newdev & ATA_ATAPI_MASTER)
925 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
926 ch->devices &= ~ATA_ATAPI_MASTER;
927 if (newdev & ATA_ATAPI_SLAVE)
928 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
929 ch->devices &= ~ATA_ATAPI_SLAVE;
931 newdev = ~devices & ch->devices;
932 if (!misdev && newdev)
935 if (newdev & ATA_ATA_MASTER && !ch->device[MASTER].driver)
936 ad_attach(&ch->device[MASTER], 1);
937 else if (ch->devices & ATA_ATA_MASTER && ch->device[MASTER].driver) {
938 ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY);
939 ad_reinit(&ch->device[MASTER]);
941 if (newdev & ATA_ATA_SLAVE && !ch->device[SLAVE].driver)
942 ad_attach(&ch->device[SLAVE], 1);
943 else if (ch->devices & (ATA_ATA_SLAVE) && ch->device[SLAVE].driver) {
944 ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY);
945 ad_reinit(&ch->device[SLAVE]);
949 if (newdev & ATA_ATAPI_MASTER && !ch->device[MASTER].driver)
950 atapi_attach(&ch->device[MASTER], 1);
951 else if (ch->devices & (ATA_ATAPI_MASTER) && ch->device[MASTER].driver) {
952 ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY);
953 atapi_reinit(&ch->device[MASTER]);
955 if (newdev & ATA_ATAPI_SLAVE && !ch->device[SLAVE].driver)
956 atapi_attach(&ch->device[SLAVE], 1);
957 else if (ch->devices & (ATA_ATAPI_SLAVE) && ch->device[SLAVE].driver) {
958 ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY);
959 atapi_reinit(&ch->device[SLAVE]);
963 if (ch->devices & (ATA_ATAPI_MASTER | ATA_ATAPI_SLAVE))
964 atapi_cam_reinit_bus(ch);
973 ata_service(struct ata_channel *ch)
975 /* do we have a SERVICE request from the drive ? */
976 if ((ch->status & (ATA_S_SERVICE|ATA_S_ERROR|ATA_S_DRQ)) == ATA_S_SERVICE) {
977 ATA_OUTB(ch->r_bmio, ATA_BMSTAT_PORT,
978 ata_dmastatus(ch) | ATA_BMSTAT_INTERRUPT);
980 if ((ATA_INB(ch->r_io, ATA_DRIVE) & ATA_SLAVE) == ATA_MASTER) {
981 if ((ch->devices & ATA_ATA_MASTER) && ch->device[MASTER].driver)
982 return ad_service((struct ad_softc *)
983 ch->device[MASTER].driver, 0);
986 if ((ch->devices & ATA_ATA_SLAVE) && ch->device[SLAVE].driver)
987 return ad_service((struct ad_softc *)
988 ch->device[SLAVE].driver, 0);
992 return ATA_OP_FINISHED;
996 ata_wait(struct ata_device *atadev, u_int8_t mask)
1001 while (timeout < 5000000) { /* timeout 5 secs */
1002 atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS);
1004 /* if drive fails status, reselect the drive just to be sure */
1005 if (atadev->channel->status == 0xff) {
1006 ata_prtdev(atadev, "no status, reselecting device\n");
1007 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM|atadev->unit);
1009 atadev->channel->status = ATA_INB(atadev->channel->r_io,ATA_STATUS);
1010 if (atadev->channel->status == 0xff)
1015 if (!(atadev->channel->status & ATA_S_BUSY))
1018 if (timeout > 1000) {
1027 if (atadev->channel->status & ATA_S_ERROR)
1028 atadev->channel->error = ATA_INB(atadev->channel->r_io, ATA_ERROR);
1029 if (timeout >= 5000000)
1032 return (atadev->channel->status & ATA_S_ERROR);
1034 /* Wait 50 msec for bits wanted. */
1037 atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS);
1038 if ((atadev->channel->status & mask) == mask) {
1039 if (atadev->channel->status & ATA_S_ERROR)
1040 atadev->channel->error=ATA_INB(atadev->channel->r_io,ATA_ERROR);
1041 return (atadev->channel->status & ATA_S_ERROR);
1049 ata_command(struct ata_device *atadev, u_int8_t command,
1050 u_int64_t lba, u_int16_t count, u_int8_t feature, int flags)
1054 ata_prtdev(atadev, "ata_command: addr=%04lx, cmd=%02x, "
1055 "lba=%lld, count=%d, feature=%d, flags=%02x\n",
1056 rman_get_start(atadev->channel->r_io),
1057 command, lba, count, feature, flags);
1061 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1063 /* disable interrupt from device */
1064 if (atadev->channel->flags & ATA_QUEUED)
1065 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_4BIT);
1067 /* ready to issue command ? */
1068 if (ata_wait(atadev, 0) < 0) {
1069 ata_prtdev(atadev, "timeout sending command=%02x s=%02x e=%02x\n",
1070 command, atadev->channel->status, atadev->channel->error);
1074 /* only use 48bit addressing if needed because of the overhead */
1075 if ((lba >= 268435455 || count > 256) && atadev->param &&
1076 atadev->param->support.address48) {
1077 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, (feature>>8) & 0xff);
1078 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, feature);
1079 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, (count>>8) & 0xff);
1080 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, count & 0xff);
1081 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, (lba>>24) & 0xff);
1082 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, lba & 0xff);
1083 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>32) & 0xff);
1084 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>8) & 0xff);
1085 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>40) & 0xff);
1086 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>16) & 0xff);
1087 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_LBA | atadev->unit);
1089 /* translate command into 48bit version */
1092 command = ATA_C_READ48; break;
1093 case ATA_C_READ_MUL:
1094 command = ATA_C_READ_MUL48; break;
1095 case ATA_C_READ_DMA:
1096 command = ATA_C_READ_DMA48; break;
1097 case ATA_C_READ_DMA_QUEUED:
1098 command = ATA_C_READ_DMA_QUEUED48; break;
1100 command = ATA_C_WRITE48; break;
1101 case ATA_C_WRITE_MUL:
1102 command = ATA_C_WRITE_MUL48; break;
1103 case ATA_C_WRITE_DMA:
1104 command = ATA_C_WRITE_DMA48; break;
1105 case ATA_C_WRITE_DMA_QUEUED:
1106 command = ATA_C_WRITE_DMA_QUEUED48; break;
1107 case ATA_C_FLUSHCACHE:
1108 command = ATA_C_FLUSHCACHE48; break;
1110 ata_prtdev(atadev, "can't translate cmd to 48bit version\n");
1115 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, feature);
1116 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, count);
1117 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, lba & 0xff);
1118 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>8) & 0xff);
1119 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>16) & 0xff);
1120 if (atadev->flags & ATA_D_USE_CHS)
1121 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE,
1122 ATA_D_IBM | atadev->unit | ((lba>>24) & 0xf));
1124 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE,
1125 ATA_D_IBM | ATA_D_LBA | atadev->unit | ((lba>>24) &0xf));
1128 switch (flags & ATA_WAIT_MASK) {
1130 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1132 /* enable interrupt */
1133 if (atadev->channel->flags & ATA_QUEUED)
1134 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
1138 atadev->channel->active |= ATA_WAIT_INTR;
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);
1145 if (tsleep((caddr_t)atadev->channel, 0, "atacmd", 10 * hz)) {
1146 ata_prtdev(atadev, "timeout waiting for interrupt\n");
1147 atadev->channel->active &= ~ATA_WAIT_INTR;
1152 case ATA_WAIT_READY:
1153 atadev->channel->active |= ATA_WAIT_READY;
1154 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1155 if (ata_wait(atadev, ATA_S_READY) < 0) {
1156 ata_prtdev(atadev, "timeout waiting for cmd=%02x s=%02x e=%02x\n",
1157 command, atadev->channel->status,atadev->channel->error);
1160 atadev->channel->active &= ~ATA_WAIT_READY;
1167 ata_enclosure_start(struct ata_device *atadev)
1169 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1171 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1173 ATA_INB(atadev->channel->r_io, ATA_CMD);
1175 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1177 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1179 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1181 ATA_INB(atadev->channel->r_io, ATA_COUNT);
1183 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1188 ata_enclosure_end(struct ata_device *atadev)
1190 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1195 ata_enclosure_chip_start(struct ata_device *atadev)
1197 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0b);
1198 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0a);
1200 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08);
1204 ata_enclosure_chip_end(struct ata_device *atadev)
1206 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08);
1208 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0a);
1210 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0b);
1215 ata_enclosure_chip_rdbit(struct ata_device *atadev)
1219 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0);
1221 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x02);
1223 val = ATA_INB(atadev->channel->r_io, ATA_SECTOR) & 0x01;
1229 ata_enclosure_chip_wrbit(struct ata_device *atadev, u_int8_t data)
1231 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08 | (data & 0x01));
1233 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08 | 0x02 | (data & 0x01));
1238 ata_enclosure_chip_rw(struct ata_device *atadev, int rw, u_int8_t val)
1243 for (i = 0; i < 8; i++)
1244 ata_enclosure_chip_wrbit(atadev, (val & (0x80 >> i)) ? 1 : 0);
1247 for (i = 0; i < 8; i++)
1248 val = (val << 1) | ata_enclosure_chip_rdbit(atadev);
1250 ata_enclosure_chip_wrbit(atadev, 0);
1255 ata_enclosure_sensor(struct ata_device *atadev,
1256 int rw, u_int8_t idx, u_int8_t data)
1258 ata_enclosure_start(atadev);
1259 ata_enclosure_chip_start(atadev);
1260 ata_enclosure_chip_rw(atadev, 1, 0x5a);
1261 ata_enclosure_chip_rw(atadev, 1, idx);
1263 ata_enclosure_chip_rw(atadev, 1, data);
1266 ata_enclosure_chip_end(atadev);
1267 ata_enclosure_chip_start(atadev);
1268 ata_enclosure_chip_rw(atadev, 1, 0x5b);
1269 data = ata_enclosure_chip_rw(atadev, 0, 0);
1271 ata_enclosure_chip_end(atadev);
1272 ata_enclosure_end(atadev);
1277 ata_enclosure_status(struct ata_device *atadev,
1278 int *fan, int *temp, int *v05, int *v12)
1280 u_int8_t id1, id2, cnt, div;
1283 if (atadev->flags & ATA_D_ENC_PRESENT) {
1284 ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL);
1285 ata_enclosure_sensor(atadev, 1, 0x4e, 0);
1286 id1 = ata_enclosure_sensor(atadev, 0, 0x4f, 0);
1287 ata_enclosure_sensor(atadev, 1, 0x4e, 0x80);
1288 id2 = ata_enclosure_sensor(atadev, 0, 0x4f, 0);
1289 if (id1 == 0xa3 && id2 == 0x5c) {
1290 div = 1 << (((ata_enclosure_sensor(atadev, 0, 0x5d, 0)&0x20)>>3)+
1291 ((ata_enclosure_sensor(atadev, 0, 0x47, 0)&0x30)>>4)+1);
1292 cnt = ata_enclosure_sensor(atadev, 0, 0x28, 0);
1296 *fan = 1350000 / cnt / div;
1297 ata_enclosure_sensor(atadev, 1, 0x4e, 0x01);
1298 *temp = (ata_enclosure_sensor(atadev, 0, 0x50, 0) * 10) +
1299 (ata_enclosure_sensor(atadev, 0, 0x50, 0) & 0x80 ? 5 : 0);
1300 *v05 = ata_enclosure_sensor(atadev, 0, 0x23, 0) * 27;
1301 *v12 = ata_enclosure_sensor(atadev, 0, 0x24, 0) * 61;
1304 ATA_UNLOCK_CH(atadev->channel);
1310 ata_enclosure_print(struct ata_device *atadev)
1313 int fan, temp, v05, v12;
1315 ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL);
1316 ata_enclosure_start(atadev);
1317 id = ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1319 st = ATA_INB(atadev->channel->r_io, ATA_COUNT);
1321 ata_enclosure_end(atadev);
1322 ATA_UNLOCK_CH(atadev->channel);
1324 switch (id & 0x93) {
1326 ata_prtdev(atadev, "Universal enclosure");
1329 ata_prtdev(atadev, "FastSwap enclosure");
1333 ata_prtdev(atadev, "SuperSwap enclosure");
1336 atadev->flags &= ~ATA_D_ENC_PRESENT;
1339 atadev->flags |= ATA_D_ENC_PRESENT;
1341 if (ata_enclosure_status(atadev, &fan, &temp, &v05, &v12))
1342 kprintf(" detected\n");
1344 kprintf(" [FAN:%drpm TEMP:%d.%01dC %d.%03dV %d.%03dV]\n",
1345 fan, temp/10, temp%10, v05/1000, v05%1000, v12/1000, v12%1000);
1349 ata_enclosure_leds(struct ata_device *atadev, u_int8_t color)
1351 if (atadev->flags & ATA_D_ENC_PRESENT) {
1354 ata_enclosure_start(atadev);
1355 reg = ATA_INB(atadev->channel->r_io, ATA_COUNT);
1357 ATA_OUTB(atadev->channel->r_io, ATA_COUNT,
1358 (color & ATA_LED_MASK) | (reg & ~ATA_LED_MASK));
1360 ata_enclosure_end(atadev);
1365 ata_change_mode(struct ata_device *atadev, int mode)
1367 int umode, wmode, pmode;
1369 umode = ata_umode(atadev->param);
1370 wmode = ata_wmode(atadev->param);
1371 pmode = ata_pmode(atadev->param);
1373 switch (mode & ATA_DMA_MASK) {
1375 if ((mode & ATA_MODE_MASK) < umode)
1376 umode = mode & ATA_MODE_MASK;
1379 if ((mode & ATA_MODE_MASK) < wmode)
1380 wmode = mode & ATA_MODE_MASK;
1384 if (((mode & ATA_MODE_MASK) - ATA_PIO0) < pmode)
1385 pmode = (mode & ATA_MODE_MASK) - ATA_PIO0;
1390 crit_enter(); /* interlock non-atomic channel lock */
1391 ATA_SLEEPLOCK_CH(atadev->channel, ATA_ACTIVE);
1392 ata_dmainit(atadev, pmode, wmode, umode);
1393 ATA_UNLOCK_CH(atadev->channel);
1395 ata_start(atadev->channel); /* XXX SOS */
1399 ata_printf(struct ata_channel *ch, int device, const char * fmt, ...)
1405 ret = kprintf("ata%d: ", device_get_unit(ch->dev));
1407 if (ch->device[ATA_DEV(device)].name)
1408 ret = kprintf("%s: ", ch->device[ATA_DEV(device)].name);
1410 ret = kprintf("ata%d-%s: ", device_get_unit(ch->dev),
1411 (device == ATA_MASTER) ? "master" : "slave");
1413 __va_start(ap, fmt);
1414 ret += kvprintf(fmt, ap);
1420 ata_prtdev(struct ata_device *atadev, const char * fmt, ...)
1426 ret = kprintf("%s: ", atadev->name);
1428 ret = kprintf("ata%d-%s: ", device_get_unit(atadev->channel->dev),
1429 (atadev->unit == ATA_MASTER) ? "master" : "slave");
1430 __va_start(ap, fmt);
1431 ret += kvprintf(fmt, ap);
1437 ata_set_name(struct ata_device *atadev, char *name, int lun)
1439 atadev->name = kmalloc(strlen(name) + 4, M_ATA, M_INTWAIT);
1440 ksprintf(atadev->name, "%s%d", name, lun);
1444 ata_free_name(struct ata_device *atadev)
1447 kfree(atadev->name, M_ATA);
1448 atadev->name = NULL;
1452 ata_get_lun(u_int32_t *map)
1454 int lun = ffs(~*map) - 1;
1461 ata_test_lun(u_int32_t *map, int lun)
1463 return (*map & (1 << lun));
1467 ata_free_lun(u_int32_t *map, int lun)
1469 *map &= ~(1 << lun);
1473 ata_mode2str(int mode)
1476 case ATA_PIO: return "BIOSPIO";
1477 case ATA_PIO0: return "PIO0";
1478 case ATA_PIO1: return "PIO1";
1479 case ATA_PIO2: return "PIO2";
1480 case ATA_PIO3: return "PIO3";
1481 case ATA_PIO4: return "PIO4";
1482 case ATA_DMA: return "BIOSDMA";
1483 case ATA_WDMA2: return "WDMA2";
1484 case ATA_UDMA2: return "UDMA33";
1485 case ATA_UDMA4: return "UDMA66";
1486 case ATA_UDMA5: return "UDMA100";
1487 case ATA_UDMA6: return "UDMA133";
1488 default: return "???";
1493 ata_pmode(struct ata_params *ap)
1495 if (ap->atavalid & ATA_FLAG_64_70) {
1496 if (ap->apiomodes & 2)
1498 if (ap->apiomodes & 1)
1501 if (ap->retired_piomode == 2)
1503 if (ap->retired_piomode == 1)
1505 if (ap->retired_piomode == 0)
1511 ata_wmode(struct ata_params *ap)
1513 if (ap->mwdmamodes & 0x04)
1515 if (ap->mwdmamodes & 0x02)
1517 if (ap->mwdmamodes & 0x01)
1523 ata_umode(struct ata_params *ap)
1525 if (ap->atavalid & ATA_FLAG_88) {
1526 if (ap->udmamodes & 0x40)
1528 if (ap->udmamodes & 0x20)
1530 if (ap->udmamodes & 0x10)
1532 if (ap->udmamodes & 0x08)
1534 if (ap->udmamodes & 0x04)
1536 if (ap->udmamodes & 0x02)
1538 if (ap->udmamodes & 0x01)
1545 bswap(int8_t *buf, int len)
1547 u_int16_t *ptr = (u_int16_t*)(buf + len);
1549 while (--ptr >= (u_int16_t*)buf)
1554 btrim(int8_t *buf, int len)
1558 for (ptr = buf; ptr < buf+len; ++ptr)
1561 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
1566 bpack(int8_t *src, int8_t *dst, int len)
1570 for (i = j = blank = 0 ; i < len; i++) {
1571 if (blank && src[i] == ' ') continue;
1572 if (blank && src[i] != ' ') {
1577 if (src[i] == ' ') {
1591 /* register controlling device */
1592 dev_ops_add(&ata_ops, 0, 0);
1593 make_dev(&ata_ops, 0, UID_ROOT, GID_OPERATOR, 0600, "ata");
1595 SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL)