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.28 2005/10/13 00:02:29 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 cdevsw ata_cdevsw = {
77 /* close */ nullclose,
83 /* strategy */ nostrategy,
89 static void ata_boot_attach(void);
90 static void ata_intr(void *);
91 static int ata_getparam(struct ata_device *, u_int8_t);
92 static int ata_service(struct ata_channel *);
93 static void bswap(int8_t *, int);
94 static void btrim(int8_t *, int);
95 static void bpack(int8_t *, int8_t *, int);
96 static void ata_change_mode(struct ata_device *, int);
97 static u_int8_t ata_enclosure_sensor(struct ata_device *, int, u_int8_t, u_int8_t);
98 static int ata_enclosure_status(struct ata_device *, int *, int *, int *, int *);
101 SYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters");
103 int ata_mpipe_size = 4;
104 TUNABLE_INT("hw.ata.mpipe_size", &ata_mpipe_size);
105 SYSCTL_INT(_hw_ata, OID_AUTO, mpipe_size, CTLFLAG_RW, &ata_mpipe_size, 0,
106 "ATA global I/O pipeline max size");
110 devclass_t ata_devclass;
113 static struct intr_config_hook *ata_delayed_attach = NULL;
114 static MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer");
117 #define DEV_ATAPIALL NATAPICD > 0 || NATAPIFD > 0 || \
118 NATAPIST > 0 || NATAPICAM > 0
121 ata_probe(device_t dev)
123 struct ata_channel *ch;
126 if (!dev || !(ch = device_get_softc(dev)))
129 if (ch->r_io || ch->r_altio || ch->r_irq)
132 /* initialize the softc basics */
133 ch->active = ATA_IDLE;
136 rid = ATA_IOADDR_RID;
137 ch->r_io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
138 ATA_IOSIZE, RF_ACTIVE);
142 rid = ATA_ALTADDR_RID;
143 ch->r_altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
144 ATA_ALTIOSIZE, RF_ACTIVE);
148 rid = ATA_BMADDR_RID;
149 ch->r_bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
150 ATA_BMIOSIZE, RF_ACTIVE);
152 ata_printf(ch, -1, "iobase=0x%04x altiobase=0x%04x bmaddr=0x%04x\n",
153 (int)rman_get_start(ch->r_io),
154 (int)rman_get_start(ch->r_altio),
155 (ch->r_bmio) ? (int)rman_get_start(ch->r_bmio) : 0);
159 ch->device[MASTER].channel = ch;
160 ch->device[MASTER].unit = ATA_MASTER;
161 ch->device[MASTER].mode = ATA_PIO;
162 ch->device[SLAVE].channel = ch;
163 ch->device[SLAVE].unit = ATA_SLAVE;
164 ch->device[SLAVE].mode = ATA_PIO;
165 TAILQ_INIT(&ch->ata_queue);
166 TAILQ_INIT(&ch->atapi_queue);
168 mpipe_init(&ch->req_mpipe, M_ATA, sizeof(union ata_request),
169 4, ata_mpipe_size, 0, NULL);
170 mpipe_init(&ch->dma_mpipe, M_DEVBUF, PAGE_SIZE,
171 4, ata_mpipe_size, MPF_NOZERO, NULL);
177 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ch->r_io);
179 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ch->r_altio);
181 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, ch->r_bmio);
183 ata_printf(ch, -1, "probe allocation failed\n");
188 ata_attach(device_t dev)
190 struct ata_channel *ch;
193 if (!dev || !(ch = device_get_softc(dev)))
197 ch->r_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
198 RF_SHAREABLE | RF_ACTIVE);
200 ata_printf(ch, -1, "unable to allocate interrupt\n");
205 * Traditional ata registers are sensitive to when they can be accessed
206 * in the face of e.g. ongoing DMA. Do not allow the interrupt to be
209 if ((error = bus_setup_intr(dev, ch->r_irq, INTR_NOPOLL,
210 ata_intr, ch, &ch->ih, NULL))) {
211 ata_printf(ch, -1, "unable to setup interrupt\n");
216 * do not attach devices if we are in early boot, this is done later
217 * when interrupts are enabled by a hook into the boot process.
218 * otherwise attach what the probe has found in ch->devices.
220 if (!ata_delayed_attach) {
223 if (ch->devices & ATA_ATA_SLAVE)
224 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
225 ch->devices &= ~ATA_ATA_SLAVE;
226 if (ch->devices & ATA_ATAPI_SLAVE)
227 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
228 ch->devices &= ~ATA_ATAPI_SLAVE;
229 if (ch->devices & ATA_ATA_MASTER)
230 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
231 ch->devices &= ~ATA_ATA_MASTER;
232 if (ch->devices & ATA_ATAPI_MASTER)
233 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
234 ch->devices &= ~ATA_ATAPI_MASTER;
236 if (ch->devices & ATA_ATA_MASTER)
237 ad_attach(&ch->device[MASTER], 0);
238 if (ch->devices & ATA_ATA_SLAVE)
239 ad_attach(&ch->device[SLAVE], 0);
242 if (ch->devices & ATA_ATAPI_MASTER)
243 atapi_attach(&ch->device[MASTER], 0);
244 if (ch->devices & ATA_ATAPI_SLAVE)
245 atapi_attach(&ch->device[SLAVE], 0);
248 atapi_cam_attach_bus(ch);
256 ata_detach(device_t dev)
258 struct ata_channel *ch;
260 if (!dev || !(ch = device_get_softc(dev)) ||
261 !ch->r_io || !ch->r_altio || !ch->r_irq)
264 /* make sure channel is not busy */
266 ATA_SLEEPLOCK_CH(ch, ATA_CONTROL);
268 if (ch->devices & ATA_ATA_MASTER && ch->device[MASTER].driver)
269 ad_detach(&ch->device[MASTER], 1);
270 if (ch->devices & ATA_ATA_SLAVE && ch->device[SLAVE].driver)
271 ad_detach(&ch->device[SLAVE], 1);
274 if (ch->devices & ATA_ATAPI_MASTER && ch->device[MASTER].driver)
275 atapi_detach(&ch->device[MASTER]);
276 if (ch->devices & ATA_ATAPI_SLAVE && ch->device[SLAVE].driver)
277 atapi_detach(&ch->device[SLAVE]);
280 atapi_cam_detach_bus(ch);
284 if (ch->device[MASTER].param) {
285 free(ch->device[MASTER].param, M_ATA);
286 ch->device[MASTER].param = NULL;
288 if (ch->device[SLAVE].param) {
289 free(ch->device[SLAVE].param, M_ATA);
290 ch->device[SLAVE].param = NULL;
292 ch->device[MASTER].driver = NULL;
293 ch->device[SLAVE].driver = NULL;
294 ch->device[MASTER].mode = ATA_PIO;
295 ch->device[SLAVE].mode = ATA_PIO;
299 bus_teardown_intr(dev, ch->r_irq, ch->ih);
300 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
302 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, ch->r_bmio);
303 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ch->r_altio);
304 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ch->r_io);
309 mpipe_done(&ch->req_mpipe);
310 mpipe_done(&ch->dma_mpipe);
317 ata_resume(device_t dev)
319 return ata_reinit(device_get_softc(dev));
323 ataioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, struct thread *td)
325 struct ata_cmd *iocmd = (struct ata_cmd *)addr;
326 struct ata_channel *ch;
327 device_t device = devclass_get_device(ata_devclass, iocmd->channel);
333 if (iocmd->channel < -1 || iocmd->device < -1 || iocmd->device > SLAVE)
336 switch (iocmd->cmd) {
338 /* should enable channel HW on controller that can SOS XXX */
339 error = ata_probe(device);
341 error = ata_attach(device);
345 error = ata_detach(device);
346 /* should disable channel HW on controller that can SOS XXX */
350 if (!device || !(ch = device_get_softc(device)))
352 crit_enter(); /* interlock non-atomic channel lock */
353 ATA_SLEEPLOCK_CH(ch, ATA_ACTIVE);
354 if ((error = ata_reinit(ch)))
360 if (!device || !(ch = device_get_softc(device)))
363 if ((iocmd->device == MASTER || iocmd->device == -1) &&
364 ch->device[MASTER].driver)
365 iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode;
367 iocmd->u.mode.mode[MASTER] = -1;
369 if ((iocmd->device == SLAVE || iocmd->device == -1) &&
370 ch->device[SLAVE].param)
371 iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode;
373 iocmd->u.mode.mode[SLAVE] = -1;
377 if (!device || !(ch = device_get_softc(device)))
380 if ((iocmd->device == MASTER || iocmd->device == -1) &&
381 iocmd->u.mode.mode[MASTER] >= 0 && ch->device[MASTER].param) {
382 ata_change_mode(&ch->device[MASTER],iocmd->u.mode.mode[MASTER]);
383 iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode;
386 iocmd->u.mode.mode[MASTER] = -1;
388 if ((iocmd->device == SLAVE || iocmd->device == -1) &&
389 iocmd->u.mode.mode[SLAVE] >= 0 && ch->device[SLAVE].param) {
390 ata_change_mode(&ch->device[SLAVE], iocmd->u.mode.mode[SLAVE]);
391 iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode;
394 iocmd->u.mode.mode[SLAVE] = -1;
398 if (!device || !(ch = device_get_softc(device)))
401 iocmd->u.param.type[MASTER] =
402 ch->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER);
403 iocmd->u.param.type[SLAVE] =
404 ch->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE);
406 if (ch->device[MASTER].name)
407 strcpy(iocmd->u.param.name[MASTER], ch->device[MASTER].name);
408 if (ch->device[SLAVE].name)
409 strcpy(iocmd->u.param.name[SLAVE], ch->device[SLAVE].name);
411 if (ch->device[MASTER].param)
412 bcopy(ch->device[MASTER].param, &iocmd->u.param.params[MASTER],
413 sizeof(struct ata_params));
414 if (ch->device[SLAVE].param)
415 bcopy(ch->device[SLAVE].param, &iocmd->u.param.params[SLAVE],
416 sizeof(struct ata_params));
420 struct ata_device *atadev;
422 if (!device || !(ch = device_get_softc(device)))
425 if (iocmd->device == SLAVE)
426 atadev = &ch->device[SLAVE];
428 atadev = &ch->device[MASTER];
430 return ata_enclosure_status(atadev,
431 &iocmd->u.enclosure.fan,
432 &iocmd->u.enclosure.temp,
433 &iocmd->u.enclosure.v05,
434 &iocmd->u.enclosure.v12);
439 return ata_raid_rebuild(iocmd->channel);
442 return ata_raid_create(&iocmd->u.raid_setup);
445 return ata_raid_delete(iocmd->channel);
448 return ata_raid_status(iocmd->channel, &iocmd->u.raid_status);
452 struct ata_device *atadev;
455 if (!device || !(ch = device_get_softc(device)))
458 if (!(atadev = &ch->device[iocmd->device]) ||
459 !(ch->devices & (iocmd->device == MASTER ?
460 ATA_ATAPI_MASTER : ATA_ATAPI_SLAVE)))
463 buf = malloc(iocmd->u.atapi.count, M_ATA, M_INTWAIT);
465 if (iocmd->u.atapi.flags & ATAPI_CMD_WRITE) {
466 error = copyin(iocmd->u.atapi.data, buf, iocmd->u.atapi.count);
470 error = atapi_queue_cmd(atadev, iocmd->u.atapi.ccb,
471 buf, iocmd->u.atapi.count,
472 (iocmd->u.atapi.flags == ATAPI_CMD_READ ?
473 ATPR_F_READ : 0) | ATPR_F_QUIET,
474 iocmd->u.atapi.timeout, NULL, NULL);
476 iocmd->u.atapi.error = error;
477 bcopy(&atadev->result, iocmd->u.atapi.sense_data,
478 sizeof(struct atapi_reqsense));
481 else if (iocmd->u.atapi.flags & ATAPI_CMD_READ)
482 error = copyout(buf, iocmd->u.atapi.data, iocmd->u.atapi.count);
495 ata_getparam(struct ata_device *atadev, u_int8_t command)
497 struct ata_params *ata_parm;
500 ata_parm = malloc(sizeof(struct ata_params), M_ATA, M_INTWAIT);
502 /* apparently some devices needs this repeated */
504 if (ata_command(atadev, command, 0, 0, 0, ATA_IMMEDIATE)) {
505 ata_prtdev(atadev, "%s identify failed\n",
506 command == ATA_C_ATAPI_IDENTIFY ? "ATAPI" : "ATA");
507 free(ata_parm, M_ATA);
511 ata_prtdev(atadev, "%s identify retries exceeded\n",
512 command == ATA_C_ATAPI_IDENTIFY ? "ATAPI" : "ATA");
513 free(ata_parm, M_ATA);
516 } while (ata_wait(atadev, ((command == ATA_C_ATAPI_IDENTIFY) ?
517 ATA_S_DRQ : (ATA_S_READY|ATA_S_DSC|ATA_S_DRQ))));
518 ATA_INSW(atadev->channel->r_io, ATA_DATA, (int16_t *)ata_parm,
519 sizeof(struct ata_params)/sizeof(int16_t));
521 if (command == ATA_C_ATA_IDENTIFY ||
522 !((ata_parm->model[0] == 'N' && ata_parm->model[1] == 'E') ||
523 (ata_parm->model[0] == 'F' && ata_parm->model[1] == 'X') ||
524 (ata_parm->model[0] == 'P' && ata_parm->model[1] == 'i')))
525 bswap(ata_parm->model, sizeof(ata_parm->model));
526 btrim(ata_parm->model, sizeof(ata_parm->model));
527 bpack(ata_parm->model, ata_parm->model, sizeof(ata_parm->model));
528 bswap(ata_parm->revision, sizeof(ata_parm->revision));
529 btrim(ata_parm->revision, sizeof(ata_parm->revision));
530 bpack(ata_parm->revision, ata_parm->revision, sizeof(ata_parm->revision));
531 bswap(ata_parm->serial, sizeof(ata_parm->serial));
532 btrim(ata_parm->serial, sizeof(ata_parm->serial));
533 bpack(ata_parm->serial, ata_parm->serial, sizeof(ata_parm->serial));
534 atadev->param = ata_parm;
539 ata_boot_attach(void)
541 struct ata_channel *ch;
544 if (ata_delayed_attach) {
545 config_intrhook_disestablish(ata_delayed_attach);
546 free(ata_delayed_attach, M_TEMP);
547 ata_delayed_attach = NULL;
552 * run through all ata devices and look for real ATA & ATAPI devices
553 * using the hints we found in the early probe, this avoids some of
554 * the delays probing of non-exsistent devices can cause.
556 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
557 if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
559 if (ch->devices & ATA_ATA_SLAVE)
560 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
561 ch->devices &= ~ATA_ATA_SLAVE;
562 if (ch->devices & ATA_ATAPI_SLAVE)
563 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
564 ch->devices &= ~ATA_ATAPI_SLAVE;
565 if (ch->devices & ATA_ATA_MASTER)
566 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
567 ch->devices &= ~ATA_ATA_MASTER;
568 if (ch->devices & ATA_ATAPI_MASTER)
569 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
570 ch->devices &= ~ATA_ATAPI_MASTER;
574 /* now we know whats there, do the real attach, first the ATA disks */
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_ATA_MASTER)
579 ad_attach(&ch->device[MASTER], 0);
580 if (ch->devices & ATA_ATA_SLAVE)
581 ad_attach(&ch->device[SLAVE], 0);
586 /* then the atapi devices */
587 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
588 if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
590 if (ch->devices & ATA_ATAPI_MASTER)
591 atapi_attach(&ch->device[MASTER], 0);
592 if (ch->devices & ATA_ATAPI_SLAVE)
593 atapi_attach(&ch->device[SLAVE], 0);
595 atapi_cam_attach_bus(ch);
605 struct ata_channel *ch = (struct ata_channel *)data;
607 * on PCI systems we might share an interrupt line with another
608 * device or our twin ATA channel, so call ch->intr_func to figure
609 * out if it is really an interrupt we should process here
611 if (ch->intr_func && ch->intr_func(ch))
614 /* if drive is busy it didn't interrupt */
615 if (ATA_INB(ch->r_altio, ATA_ALTSTAT) & ATA_S_BUSY) {
617 if (!(ATA_INB(ch->r_altio, ATA_ALTSTAT) & ATA_S_DRQ))
621 /* clear interrupt and get status */
622 ch->status = ATA_INB(ch->r_io, ATA_STATUS);
624 if (ch->status & ATA_S_ERROR)
625 ch->error = ATA_INB(ch->r_io, ATA_ERROR);
627 /* find & call the responsible driver to process this interrupt */
628 switch (ch->active) {
631 if (!ch->running || ad_interrupt(ch->running) == ATA_OP_CONTINUES)
636 case ATA_ACTIVE_ATAPI:
637 if (!ch->running || atapi_interrupt(ch->running) == ATA_OP_CONTINUES)
642 case ATA_WAIT_INTR | ATA_CONTROL:
647 case ATA_WAIT_READY | ATA_CONTROL:
651 if (ch->flags & ATA_QUEUED) {
652 ch->active = ATA_ACTIVE;
653 if (ata_service(ch) == ATA_OP_CONTINUES)
661 static int intr_count = 0;
663 if (intr_count++ < 10)
664 ata_printf(ch, -1, "unwanted interrupt #%d active=%02x s=%02x\n",
665 intr_count, ch->active, ch->status);
670 ch->active &= ATA_CONTROL;
671 if (ch->active & ATA_CONTROL)
679 ata_start(struct ata_channel *ch)
682 struct ad_request *ad_request;
685 struct atapi_request *atapi_request;
688 crit_enter(); /* interlock non-atomic channel lock */
689 if (!ATA_LOCK_CH(ch, ATA_ACTIVE)) {
695 /* find & call the responsible driver if anything on the ATA queue */
696 if (TAILQ_EMPTY(&ch->ata_queue)) {
697 if (ch->devices & (ATA_ATA_MASTER) && ch->device[MASTER].driver)
698 ad_start(&ch->device[MASTER]);
699 if (ch->devices & (ATA_ATA_SLAVE) && ch->device[SLAVE].driver)
700 ad_start(&ch->device[SLAVE]);
702 if ((ad_request = TAILQ_FIRST(&ch->ata_queue))) {
703 TAILQ_REMOVE(&ch->ata_queue, ad_request, chain);
704 ch->active = ATA_ACTIVE_ATA;
705 ch->running = ad_request;
708 * The donecount had better be 0 here because the channel may not
709 * have retained the setup for the request (if a retry).
711 KKASSERT(ad_request->donecount == 0);
712 if (ad_transfer(ad_request) == ATA_OP_CONTINUES) {
720 /* find & call the responsible driver if anything on the ATAPI queue */
721 if (TAILQ_EMPTY(&ch->atapi_queue)) {
722 if (ch->devices & (ATA_ATAPI_MASTER) && ch->device[MASTER].driver)
723 atapi_start(&ch->device[MASTER]);
724 if (ch->devices & (ATA_ATAPI_SLAVE) && ch->device[SLAVE].driver)
725 atapi_start(&ch->device[SLAVE]);
727 if ((atapi_request = TAILQ_FIRST(&ch->atapi_queue))) {
728 TAILQ_REMOVE(&ch->atapi_queue, atapi_request, chain);
729 ch->active = ATA_ACTIVE_ATAPI;
730 ch->running = atapi_request;
731 if (atapi_transfer(atapi_request) == ATA_OP_CONTINUES) {
742 ata_reset(struct ata_channel *ch)
744 u_int8_t lsb, msb, ostat0, ostat1;
745 u_int8_t stat0 = 0, stat1 = 0;
746 int mask = 0, timeout;
748 /* do we have any signs of ATA/ATAPI HW being present ? */
749 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
751 ostat0 = ATA_INB(ch->r_io, ATA_STATUS);
752 if ((ostat0 & 0xf8) != 0xf8 && ostat0 != 0xa5) {
756 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
758 ostat1 = ATA_INB(ch->r_io, ATA_STATUS);
759 if ((ostat1 & 0xf8) != 0xf8 && ostat1 != 0xa5) {
768 /* in some setups we dont want to test for a slave */
769 if (ch->flags & ATA_NO_SLAVE) {
775 ata_printf(ch, -1, "mask=%02x ostat0=%02x ostat2=%02x\n",
776 mask, ostat0, ostat1);
779 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
781 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_RESET);
783 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_IDS);
785 ATA_INB(ch->r_io, ATA_ERROR);
787 /* wait for BUSY to go inactive */
788 for (timeout = 0; timeout < 3100; timeout++) {
789 if (stat0 & ATA_S_BUSY) {
790 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
793 /* check for ATAPI signature while its still there */
794 lsb = ATA_INB(ch->r_io, ATA_CYL_LSB);
795 msb = ATA_INB(ch->r_io, ATA_CYL_MSB);
796 stat0 = ATA_INB(ch->r_io, ATA_STATUS);
797 if (!(stat0 & ATA_S_BUSY)) {
799 ata_printf(ch, ATA_MASTER, "ATAPI %02x %02x\n", lsb, msb);
800 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB)
801 ch->devices |= ATA_ATAPI_MASTER;
804 if (stat1 & ATA_S_BUSY) {
805 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
808 /* check for ATAPI signature while its still there */
809 lsb = ATA_INB(ch->r_io, ATA_CYL_LSB);
810 msb = ATA_INB(ch->r_io, ATA_CYL_MSB);
811 stat1 = ATA_INB(ch->r_io, ATA_STATUS);
812 if (!(stat1 & ATA_S_BUSY)) {
814 ata_printf(ch, ATA_SLAVE, "ATAPI %02x %02x\n", lsb, msb);
815 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB)
816 ch->devices |= ATA_ATAPI_SLAVE;
819 if (mask == 0x01) /* wait for master only */
820 if (!(stat0 & ATA_S_BUSY))
822 if (mask == 0x02) /* wait for slave only */
823 if (!(stat1 & ATA_S_BUSY))
825 if (mask == 0x03) /* wait for both master & slave */
826 if (!(stat0 & ATA_S_BUSY) && !(stat1 & ATA_S_BUSY))
831 * some devices release BUSY before they are ready to accept commands.
832 * We must wait at least 50ms before attempting to issue a command after
836 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
838 if (stat0 & ATA_S_BUSY)
840 if (stat1 & ATA_S_BUSY)
843 ata_printf(ch, -1, "mask=%02x stat0=%02x stat1=%02x\n",
848 if (mask & 0x01 && ostat0 != 0x00 && !(ch->devices & ATA_ATAPI_MASTER)) {
849 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
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_MASTER, "ATA %02x %02x\n", lsb, msb);
857 if (lsb != 0x58 && msb == 0xa5)
858 ch->devices |= ATA_ATA_MASTER;
860 if (mask & 0x02 && ostat1 != 0x00 && !(ch->devices & ATA_ATAPI_SLAVE)) {
861 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
863 ATA_OUTB(ch->r_io, ATA_ERROR, 0x58);
864 ATA_OUTB(ch->r_io, ATA_CYL_LSB, 0xa5);
865 lsb = ATA_INB(ch->r_io, ATA_ERROR);
866 msb = ATA_INB(ch->r_io, ATA_CYL_LSB);
868 ata_printf(ch, ATA_SLAVE, "ATA %02x %02x\n", lsb, msb);
869 if (lsb != 0x58 && msb == 0xa5)
870 ch->devices |= ATA_ATA_SLAVE;
873 ata_printf(ch, -1, "devices=%02x\n", ch->devices);
877 ata_reinit(struct ata_channel *ch)
879 int devices, misdev, newdev;
881 if (!ch->r_io || !ch->r_altio || !ch->r_irq)
884 ATA_FORCELOCK_CH(ch, ATA_CONTROL);
886 devices = ch->devices;
887 ata_printf(ch, -1, "resetting devices .. ");
890 if ((misdev = devices & ~ch->devices)) {
894 if (misdev & ATA_ATA_MASTER && ch->device[MASTER].driver)
895 ad_detach(&ch->device[MASTER], 0);
896 if (misdev & ATA_ATA_SLAVE && ch->device[SLAVE].driver)
897 ad_detach(&ch->device[SLAVE], 0);
900 if (misdev & ATA_ATAPI_MASTER && ch->device[MASTER].driver)
901 atapi_detach(&ch->device[MASTER]);
902 if (misdev & ATA_ATAPI_SLAVE && ch->device[SLAVE].driver)
903 atapi_detach(&ch->device[SLAVE]);
905 if (misdev & ATA_ATA_MASTER || misdev & ATA_ATAPI_MASTER) {
906 if (ch->device[MASTER].param)
907 free(ch->device[MASTER].param, M_ATA);
908 ch->device[MASTER].param = NULL;
910 if (misdev & ATA_ATA_SLAVE || misdev & ATA_ATAPI_SLAVE) {
911 if (ch->device[SLAVE].param)
912 free(ch->device[SLAVE].param, M_ATA);
913 ch->device[SLAVE].param = NULL;
916 if ((newdev = ~devices & ch->devices)) {
917 if (newdev & ATA_ATA_MASTER)
918 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
919 ch->devices &= ~ATA_ATA_MASTER;
920 if (newdev & ATA_ATA_SLAVE)
921 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
922 ch->devices &= ~ATA_ATA_SLAVE;
923 if (newdev & ATA_ATAPI_MASTER)
924 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
925 ch->devices &= ~ATA_ATAPI_MASTER;
926 if (newdev & ATA_ATAPI_SLAVE)
927 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
928 ch->devices &= ~ATA_ATAPI_SLAVE;
930 newdev = ~devices & ch->devices;
931 if (!misdev && newdev)
934 if (newdev & ATA_ATA_MASTER && !ch->device[MASTER].driver)
935 ad_attach(&ch->device[MASTER], 1);
936 else if (ch->devices & ATA_ATA_MASTER && ch->device[MASTER].driver) {
937 ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY);
938 ad_reinit(&ch->device[MASTER]);
940 if (newdev & ATA_ATA_SLAVE && !ch->device[SLAVE].driver)
941 ad_attach(&ch->device[SLAVE], 1);
942 else if (ch->devices & (ATA_ATA_SLAVE) && ch->device[SLAVE].driver) {
943 ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY);
944 ad_reinit(&ch->device[SLAVE]);
948 if (newdev & ATA_ATAPI_MASTER && !ch->device[MASTER].driver)
949 atapi_attach(&ch->device[MASTER], 1);
950 else if (ch->devices & (ATA_ATAPI_MASTER) && ch->device[MASTER].driver) {
951 ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY);
952 atapi_reinit(&ch->device[MASTER]);
954 if (newdev & ATA_ATAPI_SLAVE && !ch->device[SLAVE].driver)
955 atapi_attach(&ch->device[SLAVE], 1);
956 else if (ch->devices & (ATA_ATAPI_SLAVE) && ch->device[SLAVE].driver) {
957 ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY);
958 atapi_reinit(&ch->device[SLAVE]);
962 if (ch->devices & (ATA_ATAPI_MASTER | ATA_ATAPI_SLAVE))
963 atapi_cam_reinit_bus(ch);
972 ata_service(struct ata_channel *ch)
974 /* do we have a SERVICE request from the drive ? */
975 if ((ch->status & (ATA_S_SERVICE|ATA_S_ERROR|ATA_S_DRQ)) == ATA_S_SERVICE) {
976 ATA_OUTB(ch->r_bmio, ATA_BMSTAT_PORT,
977 ata_dmastatus(ch) | ATA_BMSTAT_INTERRUPT);
979 if ((ATA_INB(ch->r_io, ATA_DRIVE) & ATA_SLAVE) == ATA_MASTER) {
980 if ((ch->devices & ATA_ATA_MASTER) && ch->device[MASTER].driver)
981 return ad_service((struct ad_softc *)
982 ch->device[MASTER].driver, 0);
985 if ((ch->devices & ATA_ATA_SLAVE) && ch->device[SLAVE].driver)
986 return ad_service((struct ad_softc *)
987 ch->device[SLAVE].driver, 0);
991 return ATA_OP_FINISHED;
995 ata_wait(struct ata_device *atadev, u_int8_t mask)
1000 while (timeout < 5000000) { /* timeout 5 secs */
1001 atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS);
1003 /* if drive fails status, reselect the drive just to be sure */
1004 if (atadev->channel->status == 0xff) {
1005 ata_prtdev(atadev, "no status, reselecting device\n");
1006 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM|atadev->unit);
1008 atadev->channel->status = ATA_INB(atadev->channel->r_io,ATA_STATUS);
1009 if (atadev->channel->status == 0xff)
1014 if (!(atadev->channel->status & ATA_S_BUSY))
1017 if (timeout > 1000) {
1026 if (atadev->channel->status & ATA_S_ERROR)
1027 atadev->channel->error = ATA_INB(atadev->channel->r_io, ATA_ERROR);
1028 if (timeout >= 5000000)
1031 return (atadev->channel->status & ATA_S_ERROR);
1033 /* Wait 50 msec for bits wanted. */
1036 atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS);
1037 if ((atadev->channel->status & mask) == mask) {
1038 if (atadev->channel->status & ATA_S_ERROR)
1039 atadev->channel->error=ATA_INB(atadev->channel->r_io,ATA_ERROR);
1040 return (atadev->channel->status & ATA_S_ERROR);
1048 ata_command(struct ata_device *atadev, u_int8_t command,
1049 u_int64_t lba, u_int16_t count, u_int8_t feature, int flags)
1053 ata_prtdev(atadev, "ata_command: addr=%04lx, cmd=%02x, "
1054 "lba=%lld, count=%d, feature=%d, flags=%02x\n",
1055 rman_get_start(atadev->channel->r_io),
1056 command, lba, count, feature, flags);
1060 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1062 /* disable interrupt from device */
1063 if (atadev->channel->flags & ATA_QUEUED)
1064 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_4BIT);
1066 /* ready to issue command ? */
1067 if (ata_wait(atadev, 0) < 0) {
1068 ata_prtdev(atadev, "timeout sending command=%02x s=%02x e=%02x\n",
1069 command, atadev->channel->status, atadev->channel->error);
1073 /* only use 48bit addressing if needed because of the overhead */
1074 if ((lba >= 268435455 || count > 256) && atadev->param &&
1075 atadev->param->support.address48) {
1076 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, (feature>>8) & 0xff);
1077 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, feature);
1078 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, (count>>8) & 0xff);
1079 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, count & 0xff);
1080 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, (lba>>24) & 0xff);
1081 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, lba & 0xff);
1082 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>32) & 0xff);
1083 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>8) & 0xff);
1084 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>40) & 0xff);
1085 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>16) & 0xff);
1086 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_LBA | atadev->unit);
1088 /* translate command into 48bit version */
1091 command = ATA_C_READ48; break;
1092 case ATA_C_READ_MUL:
1093 command = ATA_C_READ_MUL48; break;
1094 case ATA_C_READ_DMA:
1095 command = ATA_C_READ_DMA48; break;
1096 case ATA_C_READ_DMA_QUEUED:
1097 command = ATA_C_READ_DMA_QUEUED48; break;
1099 command = ATA_C_WRITE48; break;
1100 case ATA_C_WRITE_MUL:
1101 command = ATA_C_WRITE_MUL48; break;
1102 case ATA_C_WRITE_DMA:
1103 command = ATA_C_WRITE_DMA48; break;
1104 case ATA_C_WRITE_DMA_QUEUED:
1105 command = ATA_C_WRITE_DMA_QUEUED48; break;
1106 case ATA_C_FLUSHCACHE:
1107 command = ATA_C_FLUSHCACHE48; break;
1109 ata_prtdev(atadev, "can't translate cmd to 48bit version\n");
1114 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, feature);
1115 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, count);
1116 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, lba & 0xff);
1117 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>8) & 0xff);
1118 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>16) & 0xff);
1119 if (atadev->flags & ATA_D_USE_CHS)
1120 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE,
1121 ATA_D_IBM | atadev->unit | ((lba>>24) & 0xf));
1123 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE,
1124 ATA_D_IBM | ATA_D_LBA | atadev->unit | ((lba>>24) &0xf));
1127 switch (flags & ATA_WAIT_MASK) {
1129 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1131 /* enable interrupt */
1132 if (atadev->channel->flags & ATA_QUEUED)
1133 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
1137 atadev->channel->active |= ATA_WAIT_INTR;
1138 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1140 /* enable interrupt */
1141 if (atadev->channel->flags & ATA_QUEUED)
1142 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
1144 if (tsleep((caddr_t)atadev->channel, 0, "atacmd", 10 * hz)) {
1145 ata_prtdev(atadev, "timeout waiting for interrupt\n");
1146 atadev->channel->active &= ~ATA_WAIT_INTR;
1151 case ATA_WAIT_READY:
1152 atadev->channel->active |= ATA_WAIT_READY;
1153 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1154 if (ata_wait(atadev, ATA_S_READY) < 0) {
1155 ata_prtdev(atadev, "timeout waiting for cmd=%02x s=%02x e=%02x\n",
1156 command, atadev->channel->status,atadev->channel->error);
1159 atadev->channel->active &= ~ATA_WAIT_READY;
1166 ata_enclosure_start(struct ata_device *atadev)
1168 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1170 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1172 ATA_INB(atadev->channel->r_io, ATA_CMD);
1174 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1176 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1178 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1180 ATA_INB(atadev->channel->r_io, ATA_COUNT);
1182 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1187 ata_enclosure_end(struct ata_device *atadev)
1189 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1194 ata_enclosure_chip_start(struct ata_device *atadev)
1196 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0b);
1197 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0a);
1199 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08);
1203 ata_enclosure_chip_end(struct ata_device *atadev)
1205 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08);
1207 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0a);
1209 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0b);
1214 ata_enclosure_chip_rdbit(struct ata_device *atadev)
1218 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0);
1220 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x02);
1222 val = ATA_INB(atadev->channel->r_io, ATA_SECTOR) & 0x01;
1228 ata_enclosure_chip_wrbit(struct ata_device *atadev, u_int8_t data)
1230 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08 | (data & 0x01));
1232 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08 | 0x02 | (data & 0x01));
1237 ata_enclosure_chip_rw(struct ata_device *atadev, int rw, u_int8_t val)
1242 for (i = 0; i < 8; i++)
1243 ata_enclosure_chip_wrbit(atadev, (val & (0x80 >> i)) ? 1 : 0);
1246 for (i = 0; i < 8; i++)
1247 val = (val << 1) | ata_enclosure_chip_rdbit(atadev);
1249 ata_enclosure_chip_wrbit(atadev, 0);
1254 ata_enclosure_sensor(struct ata_device *atadev,
1255 int rw, u_int8_t idx, u_int8_t data)
1257 ata_enclosure_start(atadev);
1258 ata_enclosure_chip_start(atadev);
1259 ata_enclosure_chip_rw(atadev, 1, 0x5a);
1260 ata_enclosure_chip_rw(atadev, 1, idx);
1262 ata_enclosure_chip_rw(atadev, 1, data);
1265 ata_enclosure_chip_end(atadev);
1266 ata_enclosure_chip_start(atadev);
1267 ata_enclosure_chip_rw(atadev, 1, 0x5b);
1268 data = ata_enclosure_chip_rw(atadev, 0, 0);
1270 ata_enclosure_chip_end(atadev);
1271 ata_enclosure_end(atadev);
1276 ata_enclosure_status(struct ata_device *atadev,
1277 int *fan, int *temp, int *v05, int *v12)
1279 u_int8_t id1, id2, cnt, div;
1282 if (atadev->flags & ATA_D_ENC_PRESENT) {
1283 ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL);
1284 ata_enclosure_sensor(atadev, 1, 0x4e, 0);
1285 id1 = ata_enclosure_sensor(atadev, 0, 0x4f, 0);
1286 ata_enclosure_sensor(atadev, 1, 0x4e, 0x80);
1287 id2 = ata_enclosure_sensor(atadev, 0, 0x4f, 0);
1288 if (id1 == 0xa3 && id2 == 0x5c) {
1289 div = 1 << (((ata_enclosure_sensor(atadev, 0, 0x5d, 0)&0x20)>>3)+
1290 ((ata_enclosure_sensor(atadev, 0, 0x47, 0)&0x30)>>4)+1);
1291 cnt = ata_enclosure_sensor(atadev, 0, 0x28, 0);
1295 *fan = 1350000 / cnt / div;
1296 ata_enclosure_sensor(atadev, 1, 0x4e, 0x01);
1297 *temp = (ata_enclosure_sensor(atadev, 0, 0x50, 0) * 10) +
1298 (ata_enclosure_sensor(atadev, 0, 0x50, 0) & 0x80 ? 5 : 0);
1299 *v05 = ata_enclosure_sensor(atadev, 0, 0x23, 0) * 27;
1300 *v12 = ata_enclosure_sensor(atadev, 0, 0x24, 0) * 61;
1303 ATA_UNLOCK_CH(atadev->channel);
1309 ata_enclosure_print(struct ata_device *atadev)
1312 int fan, temp, v05, v12;
1314 ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL);
1315 ata_enclosure_start(atadev);
1316 id = ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1318 st = ATA_INB(atadev->channel->r_io, ATA_COUNT);
1320 ata_enclosure_end(atadev);
1321 ATA_UNLOCK_CH(atadev->channel);
1323 switch (id & 0x93) {
1325 ata_prtdev(atadev, "Universal enclosure");
1328 ata_prtdev(atadev, "FastSwap enclosure");
1332 ata_prtdev(atadev, "SuperSwap enclosure");
1335 atadev->flags &= ~ATA_D_ENC_PRESENT;
1338 atadev->flags |= ATA_D_ENC_PRESENT;
1340 if (ata_enclosure_status(atadev, &fan, &temp, &v05, &v12))
1341 printf(" detected\n");
1343 printf(" [FAN:%drpm TEMP:%d.%01dC %d.%03dV %d.%03dV]\n",
1344 fan, temp/10, temp%10, v05/1000, v05%1000, v12/1000, v12%1000);
1348 ata_enclosure_leds(struct ata_device *atadev, u_int8_t color)
1350 if (atadev->flags & ATA_D_ENC_PRESENT) {
1353 ata_enclosure_start(atadev);
1354 reg = ATA_INB(atadev->channel->r_io, ATA_COUNT);
1356 ATA_OUTB(atadev->channel->r_io, ATA_COUNT,
1357 (color & ATA_LED_MASK) | (reg & ~ATA_LED_MASK));
1359 ata_enclosure_end(atadev);
1364 ata_change_mode(struct ata_device *atadev, int mode)
1366 int umode, wmode, pmode;
1368 umode = ata_umode(atadev->param);
1369 wmode = ata_wmode(atadev->param);
1370 pmode = ata_pmode(atadev->param);
1372 switch (mode & ATA_DMA_MASK) {
1374 if ((mode & ATA_MODE_MASK) < umode)
1375 umode = mode & ATA_MODE_MASK;
1378 if ((mode & ATA_MODE_MASK) < wmode)
1379 wmode = mode & ATA_MODE_MASK;
1383 if (((mode & ATA_MODE_MASK) - ATA_PIO0) < pmode)
1384 pmode = (mode & ATA_MODE_MASK) - ATA_PIO0;
1389 crit_enter(); /* interlock non-atomic channel lock */
1390 ATA_SLEEPLOCK_CH(atadev->channel, ATA_ACTIVE);
1391 ata_dmainit(atadev, pmode, wmode, umode);
1392 ATA_UNLOCK_CH(atadev->channel);
1394 ata_start(atadev->channel); /* XXX SOS */
1398 ata_printf(struct ata_channel *ch, int device, const char * fmt, ...)
1404 ret = printf("ata%d: ", device_get_unit(ch->dev));
1406 if (ch->device[ATA_DEV(device)].name)
1407 ret = printf("%s: ", ch->device[ATA_DEV(device)].name);
1409 ret = printf("ata%d-%s: ", device_get_unit(ch->dev),
1410 (device == ATA_MASTER) ? "master" : "slave");
1412 __va_start(ap, fmt);
1413 ret += vprintf(fmt, ap);
1419 ata_prtdev(struct ata_device *atadev, const char * fmt, ...)
1425 ret = printf("%s: ", atadev->name);
1427 ret = printf("ata%d-%s: ", device_get_unit(atadev->channel->dev),
1428 (atadev->unit == ATA_MASTER) ? "master" : "slave");
1429 __va_start(ap, fmt);
1430 ret += vprintf(fmt, ap);
1436 ata_set_name(struct ata_device *atadev, char *name, int lun)
1438 atadev->name = malloc(strlen(name) + 4, M_ATA, M_INTWAIT);
1439 sprintf(atadev->name, "%s%d", name, lun);
1443 ata_free_name(struct ata_device *atadev)
1446 free(atadev->name, M_ATA);
1447 atadev->name = NULL;
1451 ata_get_lun(u_int32_t *map)
1453 int lun = ffs(~*map) - 1;
1460 ata_test_lun(u_int32_t *map, int lun)
1462 return (*map & (1 << lun));
1466 ata_free_lun(u_int32_t *map, int lun)
1468 *map &= ~(1 << lun);
1472 ata_mode2str(int mode)
1475 case ATA_PIO: return "BIOSPIO";
1476 case ATA_PIO0: return "PIO0";
1477 case ATA_PIO1: return "PIO1";
1478 case ATA_PIO2: return "PIO2";
1479 case ATA_PIO3: return "PIO3";
1480 case ATA_PIO4: return "PIO4";
1481 case ATA_DMA: return "BIOSDMA";
1482 case ATA_WDMA2: return "WDMA2";
1483 case ATA_UDMA2: return "UDMA33";
1484 case ATA_UDMA4: return "UDMA66";
1485 case ATA_UDMA5: return "UDMA100";
1486 case ATA_UDMA6: return "UDMA133";
1487 default: return "???";
1492 ata_pmode(struct ata_params *ap)
1494 if (ap->atavalid & ATA_FLAG_64_70) {
1495 if (ap->apiomodes & 2)
1497 if (ap->apiomodes & 1)
1500 if (ap->retired_piomode == 2)
1502 if (ap->retired_piomode == 1)
1504 if (ap->retired_piomode == 0)
1510 ata_wmode(struct ata_params *ap)
1512 if (ap->mwdmamodes & 0x04)
1514 if (ap->mwdmamodes & 0x02)
1516 if (ap->mwdmamodes & 0x01)
1522 ata_umode(struct ata_params *ap)
1524 if (ap->atavalid & ATA_FLAG_88) {
1525 if (ap->udmamodes & 0x40)
1527 if (ap->udmamodes & 0x20)
1529 if (ap->udmamodes & 0x10)
1531 if (ap->udmamodes & 0x08)
1533 if (ap->udmamodes & 0x04)
1535 if (ap->udmamodes & 0x02)
1537 if (ap->udmamodes & 0x01)
1544 bswap(int8_t *buf, int len)
1546 u_int16_t *ptr = (u_int16_t*)(buf + len);
1548 while (--ptr >= (u_int16_t*)buf)
1553 btrim(int8_t *buf, int len)
1557 for (ptr = buf; ptr < buf+len; ++ptr)
1560 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
1565 bpack(int8_t *src, int8_t *dst, int len)
1569 for (i = j = blank = 0 ; i < len; i++) {
1570 if (blank && src[i] == ' ') continue;
1571 if (blank && src[i] != ' ') {
1576 if (src[i] == ' ') {
1590 /* register controlling device */
1591 cdevsw_add(&ata_cdevsw, 0, 0);
1592 make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "ata");
1594 /* register boot attach to be run when interrupts are enabled */
1595 ata_delayed_attach = malloc(sizeof(struct intr_config_hook),
1596 M_TEMP, M_WAITOK | M_ZERO);
1598 ata_delayed_attach->ich_func = (void*)ata_boot_attach;
1599 ata_delayed_attach->ich_desc = "ata";
1600 if (config_intrhook_establish(ata_delayed_attach) != 0) {
1601 printf("ata: config_intrhook_establish failed\n");
1602 free(ata_delayed_attach, M_TEMP);
1605 SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL)