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.25 2005/05/27 21:12:51 swildner 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 <machine/stdarg.h>
52 #include <machine/resource.h>
53 #include <machine/bus.h>
54 #include <machine/clock.h>
59 #include "atapi-all.h"
63 struct atapi_request atapi;
66 /* device structures */
67 static d_ioctl_t ataioctl;
68 static struct cdevsw ata_cdevsw = {
76 /* close */ nullclose,
82 /* strategy */ nostrategy,
88 static void ata_boot_attach(void);
89 static void ata_intr(void *);
90 static int ata_getparam(struct ata_device *, u_int8_t);
91 static int ata_service(struct ata_channel *);
92 static void bswap(int8_t *, int);
93 static void btrim(int8_t *, int);
94 static void bpack(int8_t *, int8_t *, int);
95 static void ata_change_mode(struct ata_device *, int);
96 static u_int8_t ata_enclosure_sensor(struct ata_device *, int, u_int8_t, u_int8_t);
97 static int ata_enclosure_status(struct ata_device *, int *, int *, int *, int *);
100 SYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters");
102 int ata_mpipe_size = 4;
103 TUNABLE_INT("hw.ata.mpipe_size", &ata_mpipe_size);
104 SYSCTL_INT(_hw_ata, OID_AUTO, mpipe_size, CTLFLAG_RW, &ata_mpipe_size, 0,
105 "ATA global I/O pipeline max size");
109 devclass_t ata_devclass;
112 static struct intr_config_hook *ata_delayed_attach = NULL;
113 static MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer");
116 #define DEV_ATAPIALL NATAPICD > 0 || NATAPIFD > 0 || \
117 NATAPIST > 0 || NATAPICAM > 0
120 ata_probe(device_t dev)
122 struct ata_channel *ch;
125 if (!dev || !(ch = device_get_softc(dev)))
128 if (ch->r_io || ch->r_altio || ch->r_irq)
131 /* initialize the softc basics */
132 ch->active = ATA_IDLE;
135 rid = ATA_IOADDR_RID;
136 ch->r_io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
137 ATA_IOSIZE, RF_ACTIVE);
141 rid = ATA_ALTADDR_RID;
142 ch->r_altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
143 ATA_ALTIOSIZE, RF_ACTIVE);
147 rid = ATA_BMADDR_RID;
148 ch->r_bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
149 ATA_BMIOSIZE, RF_ACTIVE);
151 ata_printf(ch, -1, "iobase=0x%04x altiobase=0x%04x bmaddr=0x%04x\n",
152 (int)rman_get_start(ch->r_io),
153 (int)rman_get_start(ch->r_altio),
154 (ch->r_bmio) ? (int)rman_get_start(ch->r_bmio) : 0);
158 ch->device[MASTER].channel = ch;
159 ch->device[MASTER].unit = ATA_MASTER;
160 ch->device[MASTER].mode = ATA_PIO;
161 ch->device[SLAVE].channel = ch;
162 ch->device[SLAVE].unit = ATA_SLAVE;
163 ch->device[SLAVE].mode = ATA_PIO;
164 TAILQ_INIT(&ch->ata_queue);
165 TAILQ_INIT(&ch->atapi_queue);
167 mpipe_init(&ch->req_mpipe, M_ATA, sizeof(union ata_request),
168 4, ata_mpipe_size, 0, NULL);
169 mpipe_init(&ch->dma_mpipe, M_DEVBUF, PAGE_SIZE,
170 4, ata_mpipe_size, MPF_NOZERO, NULL);
176 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ch->r_io);
178 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ch->r_altio);
180 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, ch->r_bmio);
182 ata_printf(ch, -1, "probe allocation failed\n");
187 ata_attach(device_t dev)
189 struct ata_channel *ch;
192 if (!dev || !(ch = device_get_softc(dev)))
196 ch->r_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
197 RF_SHAREABLE | RF_ACTIVE);
199 ata_printf(ch, -1, "unable to allocate interrupt\n");
202 if ((error = bus_setup_intr(dev, ch->r_irq, INTR_TYPE_BIO,
203 ata_intr, ch, &ch->ih, NULL))) {
204 ata_printf(ch, -1, "unable to setup interrupt\n");
209 * do not attach devices if we are in early boot, this is done later
210 * when interrupts are enabled by a hook into the boot process.
211 * otherwise attach what the probe has found in ch->devices.
213 if (!ata_delayed_attach) {
216 if (ch->devices & ATA_ATA_SLAVE)
217 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
218 ch->devices &= ~ATA_ATA_SLAVE;
219 if (ch->devices & ATA_ATAPI_SLAVE)
220 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
221 ch->devices &= ~ATA_ATAPI_SLAVE;
222 if (ch->devices & ATA_ATA_MASTER)
223 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
224 ch->devices &= ~ATA_ATA_MASTER;
225 if (ch->devices & ATA_ATAPI_MASTER)
226 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
227 ch->devices &= ~ATA_ATAPI_MASTER;
229 if (ch->devices & ATA_ATA_MASTER)
230 ad_attach(&ch->device[MASTER], 0);
231 if (ch->devices & ATA_ATA_SLAVE)
232 ad_attach(&ch->device[SLAVE], 0);
235 if (ch->devices & ATA_ATAPI_MASTER)
236 atapi_attach(&ch->device[MASTER], 0);
237 if (ch->devices & ATA_ATAPI_SLAVE)
238 atapi_attach(&ch->device[SLAVE], 0);
241 atapi_cam_attach_bus(ch);
249 ata_detach(device_t dev)
251 struct ata_channel *ch;
254 if (!dev || !(ch = device_get_softc(dev)) ||
255 !ch->r_io || !ch->r_altio || !ch->r_irq)
258 /* make sure channel is not busy */
260 ATA_SLEEPLOCK_CH(ch, ATA_CONTROL);
262 if (ch->devices & ATA_ATA_MASTER && ch->device[MASTER].driver)
263 ad_detach(&ch->device[MASTER], 1);
264 if (ch->devices & ATA_ATA_SLAVE && ch->device[SLAVE].driver)
265 ad_detach(&ch->device[SLAVE], 1);
268 if (ch->devices & ATA_ATAPI_MASTER && ch->device[MASTER].driver)
269 atapi_detach(&ch->device[MASTER]);
270 if (ch->devices & ATA_ATAPI_SLAVE && ch->device[SLAVE].driver)
271 atapi_detach(&ch->device[SLAVE]);
274 atapi_cam_detach_bus(ch);
278 if (ch->device[MASTER].param) {
279 free(ch->device[MASTER].param, M_ATA);
280 ch->device[MASTER].param = NULL;
282 if (ch->device[SLAVE].param) {
283 free(ch->device[SLAVE].param, M_ATA);
284 ch->device[SLAVE].param = NULL;
286 ch->device[MASTER].driver = NULL;
287 ch->device[SLAVE].driver = NULL;
288 ch->device[MASTER].mode = ATA_PIO;
289 ch->device[SLAVE].mode = ATA_PIO;
293 bus_teardown_intr(dev, ch->r_irq, ch->ih);
294 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
296 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, ch->r_bmio);
297 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ch->r_altio);
298 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ch->r_io);
303 mpipe_done(&ch->req_mpipe);
304 mpipe_done(&ch->dma_mpipe);
311 ata_resume(device_t dev)
313 return ata_reinit(device_get_softc(dev));
317 ataioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, struct thread *td)
319 struct ata_cmd *iocmd = (struct ata_cmd *)addr;
320 struct ata_channel *ch;
321 device_t device = devclass_get_device(ata_devclass, iocmd->channel);
328 if (iocmd->channel < -1 || iocmd->device < -1 || iocmd->device > SLAVE)
331 switch (iocmd->cmd) {
333 /* should enable channel HW on controller that can SOS XXX */
334 error = ata_probe(device);
336 error = ata_attach(device);
340 error = ata_detach(device);
341 /* should disable channel HW on controller that can SOS XXX */
345 if (!device || !(ch = device_get_softc(device)))
347 s = splbio(); /* interlock non-atomic channel lock */
348 ATA_SLEEPLOCK_CH(ch, ATA_ACTIVE);
349 if ((error = ata_reinit(ch)))
355 if (!device || !(ch = device_get_softc(device)))
358 if ((iocmd->device == MASTER || iocmd->device == -1) &&
359 ch->device[MASTER].driver)
360 iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode;
362 iocmd->u.mode.mode[MASTER] = -1;
364 if ((iocmd->device == SLAVE || iocmd->device == -1) &&
365 ch->device[SLAVE].param)
366 iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode;
368 iocmd->u.mode.mode[SLAVE] = -1;
372 if (!device || !(ch = device_get_softc(device)))
375 if ((iocmd->device == MASTER || iocmd->device == -1) &&
376 iocmd->u.mode.mode[MASTER] >= 0 && ch->device[MASTER].param) {
377 ata_change_mode(&ch->device[MASTER],iocmd->u.mode.mode[MASTER]);
378 iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode;
381 iocmd->u.mode.mode[MASTER] = -1;
383 if ((iocmd->device == SLAVE || iocmd->device == -1) &&
384 iocmd->u.mode.mode[SLAVE] >= 0 && ch->device[SLAVE].param) {
385 ata_change_mode(&ch->device[SLAVE], iocmd->u.mode.mode[SLAVE]);
386 iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode;
389 iocmd->u.mode.mode[SLAVE] = -1;
393 if (!device || !(ch = device_get_softc(device)))
396 iocmd->u.param.type[MASTER] =
397 ch->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER);
398 iocmd->u.param.type[SLAVE] =
399 ch->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE);
401 if (ch->device[MASTER].name)
402 strcpy(iocmd->u.param.name[MASTER], ch->device[MASTER].name);
403 if (ch->device[SLAVE].name)
404 strcpy(iocmd->u.param.name[SLAVE], ch->device[SLAVE].name);
406 if (ch->device[MASTER].param)
407 bcopy(ch->device[MASTER].param, &iocmd->u.param.params[MASTER],
408 sizeof(struct ata_params));
409 if (ch->device[SLAVE].param)
410 bcopy(ch->device[SLAVE].param, &iocmd->u.param.params[SLAVE],
411 sizeof(struct ata_params));
415 struct ata_device *atadev;
417 if (!device || !(ch = device_get_softc(device)))
420 if (iocmd->device == SLAVE)
421 atadev = &ch->device[SLAVE];
423 atadev = &ch->device[MASTER];
425 return ata_enclosure_status(atadev,
426 &iocmd->u.enclosure.fan,
427 &iocmd->u.enclosure.temp,
428 &iocmd->u.enclosure.v05,
429 &iocmd->u.enclosure.v12);
434 return ata_raid_rebuild(iocmd->channel);
437 return ata_raid_create(&iocmd->u.raid_setup);
440 return ata_raid_delete(iocmd->channel);
443 return ata_raid_status(iocmd->channel, &iocmd->u.raid_status);
447 struct ata_device *atadev;
450 if (!device || !(ch = device_get_softc(device)))
453 if (!(atadev = &ch->device[iocmd->device]) ||
454 !(ch->devices & (iocmd->device == MASTER ?
455 ATA_ATAPI_MASTER : ATA_ATAPI_SLAVE)))
458 buf = malloc(iocmd->u.atapi.count, M_ATA, M_INTWAIT);
460 if (iocmd->u.atapi.flags & ATAPI_CMD_WRITE) {
461 error = copyin(iocmd->u.atapi.data, buf, iocmd->u.atapi.count);
465 error = atapi_queue_cmd(atadev, iocmd->u.atapi.ccb,
466 buf, iocmd->u.atapi.count,
467 (iocmd->u.atapi.flags == ATAPI_CMD_READ ?
468 ATPR_F_READ : 0) | ATPR_F_QUIET,
469 iocmd->u.atapi.timeout, NULL, NULL);
471 iocmd->u.atapi.error = error;
472 bcopy(&atadev->result, iocmd->u.atapi.sense_data,
473 sizeof(struct atapi_reqsense));
476 else if (iocmd->u.atapi.flags & ATAPI_CMD_READ)
477 error = copyout(buf, iocmd->u.atapi.data, iocmd->u.atapi.count);
490 ata_getparam(struct ata_device *atadev, u_int8_t command)
492 struct ata_params *ata_parm;
495 ata_parm = malloc(sizeof(struct ata_params), M_ATA, M_INTWAIT);
497 /* apparently some devices needs this repeated */
499 if (ata_command(atadev, command, 0, 0, 0, ATA_IMMEDIATE)) {
500 ata_prtdev(atadev, "%s identify failed\n",
501 command == ATA_C_ATAPI_IDENTIFY ? "ATAPI" : "ATA");
502 free(ata_parm, M_ATA);
506 ata_prtdev(atadev, "%s identify retries exceeded\n",
507 command == ATA_C_ATAPI_IDENTIFY ? "ATAPI" : "ATA");
508 free(ata_parm, M_ATA);
511 } while (ata_wait(atadev, ((command == ATA_C_ATAPI_IDENTIFY) ?
512 ATA_S_DRQ : (ATA_S_READY|ATA_S_DSC|ATA_S_DRQ))));
513 ATA_INSW(atadev->channel->r_io, ATA_DATA, (int16_t *)ata_parm,
514 sizeof(struct ata_params)/sizeof(int16_t));
516 if (command == ATA_C_ATA_IDENTIFY ||
517 !((ata_parm->model[0] == 'N' && ata_parm->model[1] == 'E') ||
518 (ata_parm->model[0] == 'F' && ata_parm->model[1] == 'X') ||
519 (ata_parm->model[0] == 'P' && ata_parm->model[1] == 'i')))
520 bswap(ata_parm->model, sizeof(ata_parm->model));
521 btrim(ata_parm->model, sizeof(ata_parm->model));
522 bpack(ata_parm->model, ata_parm->model, sizeof(ata_parm->model));
523 bswap(ata_parm->revision, sizeof(ata_parm->revision));
524 btrim(ata_parm->revision, sizeof(ata_parm->revision));
525 bpack(ata_parm->revision, ata_parm->revision, sizeof(ata_parm->revision));
526 bswap(ata_parm->serial, sizeof(ata_parm->serial));
527 btrim(ata_parm->serial, sizeof(ata_parm->serial));
528 bpack(ata_parm->serial, ata_parm->serial, sizeof(ata_parm->serial));
529 atadev->param = ata_parm;
534 ata_boot_attach(void)
536 struct ata_channel *ch;
539 if (ata_delayed_attach) {
540 config_intrhook_disestablish(ata_delayed_attach);
541 free(ata_delayed_attach, M_TEMP);
542 ata_delayed_attach = NULL;
547 * run through all ata devices and look for real ATA & ATAPI devices
548 * using the hints we found in the early probe, this avoids some of
549 * the delays probing of non-exsistent devices can cause.
551 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
552 if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
554 if (ch->devices & ATA_ATA_SLAVE)
555 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
556 ch->devices &= ~ATA_ATA_SLAVE;
557 if (ch->devices & ATA_ATAPI_SLAVE)
558 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
559 ch->devices &= ~ATA_ATAPI_SLAVE;
560 if (ch->devices & ATA_ATA_MASTER)
561 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
562 ch->devices &= ~ATA_ATA_MASTER;
563 if (ch->devices & ATA_ATAPI_MASTER)
564 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
565 ch->devices &= ~ATA_ATAPI_MASTER;
569 /* now we know whats there, do the real attach, first the ATA disks */
570 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
571 if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
573 if (ch->devices & ATA_ATA_MASTER)
574 ad_attach(&ch->device[MASTER], 0);
575 if (ch->devices & ATA_ATA_SLAVE)
576 ad_attach(&ch->device[SLAVE], 0);
581 /* then the atapi devices */
582 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
583 if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
585 if (ch->devices & ATA_ATAPI_MASTER)
586 atapi_attach(&ch->device[MASTER], 0);
587 if (ch->devices & ATA_ATAPI_SLAVE)
588 atapi_attach(&ch->device[SLAVE], 0);
590 atapi_cam_attach_bus(ch);
600 struct ata_channel *ch = (struct ata_channel *)data;
602 * on PCI systems we might share an interrupt line with another
603 * device or our twin ATA channel, so call ch->intr_func to figure
604 * out if it is really an interrupt we should process here
606 if (ch->intr_func && ch->intr_func(ch))
609 /* if drive is busy it didn't interrupt */
610 if (ATA_INB(ch->r_altio, ATA_ALTSTAT) & ATA_S_BUSY) {
612 if (!(ATA_INB(ch->r_altio, ATA_ALTSTAT) & ATA_S_DRQ))
616 /* clear interrupt and get status */
617 ch->status = ATA_INB(ch->r_io, ATA_STATUS);
619 if (ch->status & ATA_S_ERROR)
620 ch->error = ATA_INB(ch->r_io, ATA_ERROR);
622 /* find & call the responsible driver to process this interrupt */
623 switch (ch->active) {
626 if (!ch->running || ad_interrupt(ch->running) == ATA_OP_CONTINUES)
631 case ATA_ACTIVE_ATAPI:
632 if (!ch->running || atapi_interrupt(ch->running) == ATA_OP_CONTINUES)
637 case ATA_WAIT_INTR | ATA_CONTROL:
642 case ATA_WAIT_READY | ATA_CONTROL:
646 if (ch->flags & ATA_QUEUED) {
647 ch->active = ATA_ACTIVE;
648 if (ata_service(ch) == ATA_OP_CONTINUES)
656 static int intr_count = 0;
658 if (intr_count++ < 10)
659 ata_printf(ch, -1, "unwanted interrupt #%d active=%02x s=%02x\n",
660 intr_count, ch->active, ch->status);
665 ch->active &= ATA_CONTROL;
666 if (ch->active & ATA_CONTROL)
674 ata_start(struct ata_channel *ch)
677 struct ad_request *ad_request;
680 struct atapi_request *atapi_request;
684 s = splbio(); /* interlock non-atomic channel lock */
685 if (!ATA_LOCK_CH(ch, ATA_ACTIVE)) {
691 /* find & call the responsible driver if anything on the ATA queue */
692 if (TAILQ_EMPTY(&ch->ata_queue)) {
693 if (ch->devices & (ATA_ATA_MASTER) && ch->device[MASTER].driver)
694 ad_start(&ch->device[MASTER]);
695 if (ch->devices & (ATA_ATA_SLAVE) && ch->device[SLAVE].driver)
696 ad_start(&ch->device[SLAVE]);
698 if ((ad_request = TAILQ_FIRST(&ch->ata_queue))) {
699 TAILQ_REMOVE(&ch->ata_queue, ad_request, chain);
700 ch->active = ATA_ACTIVE_ATA;
701 ch->running = ad_request;
704 * The donecount had better be 0 here because the channel may not
705 * have retained the setup for the request (if a retry).
707 KKASSERT(ad_request->donecount == 0);
708 if (ad_transfer(ad_request) == ATA_OP_CONTINUES) {
716 /* find & call the responsible driver if anything on the ATAPI queue */
717 if (TAILQ_EMPTY(&ch->atapi_queue)) {
718 if (ch->devices & (ATA_ATAPI_MASTER) && ch->device[MASTER].driver)
719 atapi_start(&ch->device[MASTER]);
720 if (ch->devices & (ATA_ATAPI_SLAVE) && ch->device[SLAVE].driver)
721 atapi_start(&ch->device[SLAVE]);
723 if ((atapi_request = TAILQ_FIRST(&ch->atapi_queue))) {
724 TAILQ_REMOVE(&ch->atapi_queue, atapi_request, chain);
725 ch->active = ATA_ACTIVE_ATAPI;
726 ch->running = atapi_request;
727 if (atapi_transfer(atapi_request) == ATA_OP_CONTINUES) {
738 ata_reset(struct ata_channel *ch)
740 u_int8_t lsb, msb, ostat0, ostat1;
741 u_int8_t stat0 = 0, stat1 = 0;
742 int mask = 0, timeout;
744 /* do we have any signs of ATA/ATAPI HW being present ? */
745 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
747 ostat0 = ATA_INB(ch->r_io, ATA_STATUS);
748 if ((ostat0 & 0xf8) != 0xf8 && ostat0 != 0xa5) {
752 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
754 ostat1 = ATA_INB(ch->r_io, ATA_STATUS);
755 if ((ostat1 & 0xf8) != 0xf8 && ostat1 != 0xa5) {
764 /* in some setups we dont want to test for a slave */
765 if (ch->flags & ATA_NO_SLAVE) {
771 ata_printf(ch, -1, "mask=%02x ostat0=%02x ostat2=%02x\n",
772 mask, ostat0, ostat1);
775 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
777 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_RESET);
779 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_IDS);
781 ATA_INB(ch->r_io, ATA_ERROR);
783 /* wait for BUSY to go inactive */
784 for (timeout = 0; timeout < 3100; timeout++) {
785 if (stat0 & ATA_S_BUSY) {
786 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
789 /* check for ATAPI signature while its still there */
790 lsb = ATA_INB(ch->r_io, ATA_CYL_LSB);
791 msb = ATA_INB(ch->r_io, ATA_CYL_MSB);
792 stat0 = ATA_INB(ch->r_io, ATA_STATUS);
793 if (!(stat0 & ATA_S_BUSY)) {
795 ata_printf(ch, ATA_MASTER, "ATAPI %02x %02x\n", lsb, msb);
796 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB)
797 ch->devices |= ATA_ATAPI_MASTER;
800 if (stat1 & ATA_S_BUSY) {
801 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
804 /* check for ATAPI signature while its still there */
805 lsb = ATA_INB(ch->r_io, ATA_CYL_LSB);
806 msb = ATA_INB(ch->r_io, ATA_CYL_MSB);
807 stat1 = ATA_INB(ch->r_io, ATA_STATUS);
808 if (!(stat1 & ATA_S_BUSY)) {
810 ata_printf(ch, ATA_SLAVE, "ATAPI %02x %02x\n", lsb, msb);
811 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB)
812 ch->devices |= ATA_ATAPI_SLAVE;
815 if (mask == 0x01) /* wait for master only */
816 if (!(stat0 & ATA_S_BUSY))
818 if (mask == 0x02) /* wait for slave only */
819 if (!(stat1 & ATA_S_BUSY))
821 if (mask == 0x03) /* wait for both master & slave */
822 if (!(stat0 & ATA_S_BUSY) && !(stat1 & ATA_S_BUSY))
827 * some devices release BUSY before they are ready to accept commands.
828 * We must wait at least 50ms before attempting to issue a command after
832 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
834 if (stat0 & ATA_S_BUSY)
836 if (stat1 & ATA_S_BUSY)
839 ata_printf(ch, -1, "mask=%02x stat0=%02x stat1=%02x\n",
844 if (mask & 0x01 && ostat0 != 0x00 && !(ch->devices & ATA_ATAPI_MASTER)) {
845 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
847 ATA_OUTB(ch->r_io, ATA_ERROR, 0x58);
848 ATA_OUTB(ch->r_io, ATA_CYL_LSB, 0xa5);
849 lsb = ATA_INB(ch->r_io, ATA_ERROR);
850 msb = ATA_INB(ch->r_io, ATA_CYL_LSB);
852 ata_printf(ch, ATA_MASTER, "ATA %02x %02x\n", lsb, msb);
853 if (lsb != 0x58 && msb == 0xa5)
854 ch->devices |= ATA_ATA_MASTER;
856 if (mask & 0x02 && ostat1 != 0x00 && !(ch->devices & ATA_ATAPI_SLAVE)) {
857 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
859 ATA_OUTB(ch->r_io, ATA_ERROR, 0x58);
860 ATA_OUTB(ch->r_io, ATA_CYL_LSB, 0xa5);
861 lsb = ATA_INB(ch->r_io, ATA_ERROR);
862 msb = ATA_INB(ch->r_io, ATA_CYL_LSB);
864 ata_printf(ch, ATA_SLAVE, "ATA %02x %02x\n", lsb, msb);
865 if (lsb != 0x58 && msb == 0xa5)
866 ch->devices |= ATA_ATA_SLAVE;
869 ata_printf(ch, -1, "devices=%02x\n", ch->devices);
873 ata_reinit(struct ata_channel *ch)
875 int devices, misdev, newdev;
877 if (!ch->r_io || !ch->r_altio || !ch->r_irq)
880 ATA_FORCELOCK_CH(ch, ATA_CONTROL);
882 devices = ch->devices;
883 ata_printf(ch, -1, "resetting devices .. ");
886 if ((misdev = devices & ~ch->devices)) {
890 if (misdev & ATA_ATA_MASTER && ch->device[MASTER].driver)
891 ad_detach(&ch->device[MASTER], 0);
892 if (misdev & ATA_ATA_SLAVE && ch->device[SLAVE].driver)
893 ad_detach(&ch->device[SLAVE], 0);
896 if (misdev & ATA_ATAPI_MASTER && ch->device[MASTER].driver)
897 atapi_detach(&ch->device[MASTER]);
898 if (misdev & ATA_ATAPI_SLAVE && ch->device[SLAVE].driver)
899 atapi_detach(&ch->device[SLAVE]);
901 if (misdev & ATA_ATA_MASTER || misdev & ATA_ATAPI_MASTER) {
902 if (ch->device[MASTER].param)
903 free(ch->device[MASTER].param, M_ATA);
904 ch->device[MASTER].param = NULL;
906 if (misdev & ATA_ATA_SLAVE || misdev & ATA_ATAPI_SLAVE) {
907 if (ch->device[SLAVE].param)
908 free(ch->device[SLAVE].param, M_ATA);
909 ch->device[SLAVE].param = NULL;
912 if ((newdev = ~devices & ch->devices)) {
913 if (newdev & ATA_ATA_MASTER)
914 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
915 ch->devices &= ~ATA_ATA_MASTER;
916 if (newdev & ATA_ATA_SLAVE)
917 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
918 ch->devices &= ~ATA_ATA_SLAVE;
919 if (newdev & ATA_ATAPI_MASTER)
920 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
921 ch->devices &= ~ATA_ATAPI_MASTER;
922 if (newdev & ATA_ATAPI_SLAVE)
923 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
924 ch->devices &= ~ATA_ATAPI_SLAVE;
926 newdev = ~devices & ch->devices;
927 if (!misdev && newdev)
930 if (newdev & ATA_ATA_MASTER && !ch->device[MASTER].driver)
931 ad_attach(&ch->device[MASTER], 1);
932 else if (ch->devices & ATA_ATA_MASTER && ch->device[MASTER].driver) {
933 ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY);
934 ad_reinit(&ch->device[MASTER]);
936 if (newdev & ATA_ATA_SLAVE && !ch->device[SLAVE].driver)
937 ad_attach(&ch->device[SLAVE], 1);
938 else if (ch->devices & (ATA_ATA_SLAVE) && ch->device[SLAVE].driver) {
939 ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY);
940 ad_reinit(&ch->device[SLAVE]);
944 if (newdev & ATA_ATAPI_MASTER && !ch->device[MASTER].driver)
945 atapi_attach(&ch->device[MASTER], 1);
946 else if (ch->devices & (ATA_ATAPI_MASTER) && ch->device[MASTER].driver) {
947 ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY);
948 atapi_reinit(&ch->device[MASTER]);
950 if (newdev & ATA_ATAPI_SLAVE && !ch->device[SLAVE].driver)
951 atapi_attach(&ch->device[SLAVE], 1);
952 else if (ch->devices & (ATA_ATAPI_SLAVE) && ch->device[SLAVE].driver) {
953 ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY);
954 atapi_reinit(&ch->device[SLAVE]);
958 if (ch->devices & (ATA_ATAPI_MASTER | ATA_ATAPI_SLAVE))
959 atapi_cam_reinit_bus(ch);
968 ata_service(struct ata_channel *ch)
970 /* do we have a SERVICE request from the drive ? */
971 if ((ch->status & (ATA_S_SERVICE|ATA_S_ERROR|ATA_S_DRQ)) == ATA_S_SERVICE) {
972 ATA_OUTB(ch->r_bmio, ATA_BMSTAT_PORT,
973 ata_dmastatus(ch) | ATA_BMSTAT_INTERRUPT);
975 if ((ATA_INB(ch->r_io, ATA_DRIVE) & ATA_SLAVE) == ATA_MASTER) {
976 if ((ch->devices & ATA_ATA_MASTER) && ch->device[MASTER].driver)
977 return ad_service((struct ad_softc *)
978 ch->device[MASTER].driver, 0);
981 if ((ch->devices & ATA_ATA_SLAVE) && ch->device[SLAVE].driver)
982 return ad_service((struct ad_softc *)
983 ch->device[SLAVE].driver, 0);
987 return ATA_OP_FINISHED;
991 ata_wait(struct ata_device *atadev, u_int8_t mask)
996 while (timeout < 5000000) { /* timeout 5 secs */
997 atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS);
999 /* if drive fails status, reselect the drive just to be sure */
1000 if (atadev->channel->status == 0xff) {
1001 ata_prtdev(atadev, "no status, reselecting device\n");
1002 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM|atadev->unit);
1004 atadev->channel->status = ATA_INB(atadev->channel->r_io,ATA_STATUS);
1005 if (atadev->channel->status == 0xff)
1010 if (!(atadev->channel->status & ATA_S_BUSY))
1013 if (timeout > 1000) {
1022 if (atadev->channel->status & ATA_S_ERROR)
1023 atadev->channel->error = ATA_INB(atadev->channel->r_io, ATA_ERROR);
1024 if (timeout >= 5000000)
1027 return (atadev->channel->status & ATA_S_ERROR);
1029 /* Wait 50 msec for bits wanted. */
1032 atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS);
1033 if ((atadev->channel->status & mask) == mask) {
1034 if (atadev->channel->status & ATA_S_ERROR)
1035 atadev->channel->error=ATA_INB(atadev->channel->r_io,ATA_ERROR);
1036 return (atadev->channel->status & ATA_S_ERROR);
1044 ata_command(struct ata_device *atadev, u_int8_t command,
1045 u_int64_t lba, u_int16_t count, u_int8_t feature, int flags)
1049 ata_prtdev(atadev, "ata_command: addr=%04lx, cmd=%02x, "
1050 "lba=%lld, count=%d, feature=%d, flags=%02x\n",
1051 rman_get_start(atadev->channel->r_io),
1052 command, lba, count, feature, flags);
1056 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1058 /* disable interrupt from device */
1059 if (atadev->channel->flags & ATA_QUEUED)
1060 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_4BIT);
1062 /* ready to issue command ? */
1063 if (ata_wait(atadev, 0) < 0) {
1064 ata_prtdev(atadev, "timeout sending command=%02x s=%02x e=%02x\n",
1065 command, atadev->channel->status, atadev->channel->error);
1069 /* only use 48bit addressing if needed because of the overhead */
1070 if ((lba >= 268435455 || count > 256) && atadev->param &&
1071 atadev->param->support.address48) {
1072 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, (feature>>8) & 0xff);
1073 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, feature);
1074 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, (count>>8) & 0xff);
1075 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, count & 0xff);
1076 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, (lba>>24) & 0xff);
1077 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, lba & 0xff);
1078 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>32) & 0xff);
1079 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>8) & 0xff);
1080 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>40) & 0xff);
1081 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>16) & 0xff);
1082 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_LBA | atadev->unit);
1084 /* translate command into 48bit version */
1087 command = ATA_C_READ48; break;
1088 case ATA_C_READ_MUL:
1089 command = ATA_C_READ_MUL48; break;
1090 case ATA_C_READ_DMA:
1091 command = ATA_C_READ_DMA48; break;
1092 case ATA_C_READ_DMA_QUEUED:
1093 command = ATA_C_READ_DMA_QUEUED48; break;
1095 command = ATA_C_WRITE48; break;
1096 case ATA_C_WRITE_MUL:
1097 command = ATA_C_WRITE_MUL48; break;
1098 case ATA_C_WRITE_DMA:
1099 command = ATA_C_WRITE_DMA48; break;
1100 case ATA_C_WRITE_DMA_QUEUED:
1101 command = ATA_C_WRITE_DMA_QUEUED48; break;
1102 case ATA_C_FLUSHCACHE:
1103 command = ATA_C_FLUSHCACHE48; break;
1105 ata_prtdev(atadev, "can't translate cmd to 48bit version\n");
1110 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, feature);
1111 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, count);
1112 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, lba & 0xff);
1113 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>8) & 0xff);
1114 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>16) & 0xff);
1115 if (atadev->flags & ATA_D_USE_CHS)
1116 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE,
1117 ATA_D_IBM | atadev->unit | ((lba>>24) & 0xf));
1119 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE,
1120 ATA_D_IBM | ATA_D_LBA | atadev->unit | ((lba>>24) &0xf));
1123 switch (flags & ATA_WAIT_MASK) {
1125 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1127 /* enable interrupt */
1128 if (atadev->channel->flags & ATA_QUEUED)
1129 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
1133 atadev->channel->active |= ATA_WAIT_INTR;
1134 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1136 /* enable interrupt */
1137 if (atadev->channel->flags & ATA_QUEUED)
1138 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
1140 if (tsleep((caddr_t)atadev->channel, 0, "atacmd", 10 * hz)) {
1141 ata_prtdev(atadev, "timeout waiting for interrupt\n");
1142 atadev->channel->active &= ~ATA_WAIT_INTR;
1147 case ATA_WAIT_READY:
1148 atadev->channel->active |= ATA_WAIT_READY;
1149 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1150 if (ata_wait(atadev, ATA_S_READY) < 0) {
1151 ata_prtdev(atadev, "timeout waiting for cmd=%02x s=%02x e=%02x\n",
1152 command, atadev->channel->status,atadev->channel->error);
1155 atadev->channel->active &= ~ATA_WAIT_READY;
1162 ata_enclosure_start(struct ata_device *atadev)
1164 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1166 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1168 ATA_INB(atadev->channel->r_io, ATA_CMD);
1170 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1172 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1174 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1176 ATA_INB(atadev->channel->r_io, ATA_COUNT);
1178 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1183 ata_enclosure_end(struct ata_device *atadev)
1185 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1190 ata_enclosure_chip_start(struct ata_device *atadev)
1192 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0b);
1193 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0a);
1195 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08);
1199 ata_enclosure_chip_end(struct ata_device *atadev)
1201 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08);
1203 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0a);
1205 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0b);
1210 ata_enclosure_chip_rdbit(struct ata_device *atadev)
1214 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0);
1216 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x02);
1218 val = ATA_INB(atadev->channel->r_io, ATA_SECTOR) & 0x01;
1224 ata_enclosure_chip_wrbit(struct ata_device *atadev, u_int8_t data)
1226 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08 | (data & 0x01));
1228 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08 | 0x02 | (data & 0x01));
1233 ata_enclosure_chip_rw(struct ata_device *atadev, int rw, u_int8_t val)
1238 for (i = 0; i < 8; i++)
1239 ata_enclosure_chip_wrbit(atadev, (val & (0x80 >> i)) ? 1 : 0);
1242 for (i = 0; i < 8; i++)
1243 val = (val << 1) | ata_enclosure_chip_rdbit(atadev);
1245 ata_enclosure_chip_wrbit(atadev, 0);
1250 ata_enclosure_sensor(struct ata_device *atadev,
1251 int rw, u_int8_t idx, u_int8_t data)
1253 ata_enclosure_start(atadev);
1254 ata_enclosure_chip_start(atadev);
1255 ata_enclosure_chip_rw(atadev, 1, 0x5a);
1256 ata_enclosure_chip_rw(atadev, 1, idx);
1258 ata_enclosure_chip_rw(atadev, 1, data);
1261 ata_enclosure_chip_end(atadev);
1262 ata_enclosure_chip_start(atadev);
1263 ata_enclosure_chip_rw(atadev, 1, 0x5b);
1264 data = ata_enclosure_chip_rw(atadev, 0, 0);
1266 ata_enclosure_chip_end(atadev);
1267 ata_enclosure_end(atadev);
1272 ata_enclosure_status(struct ata_device *atadev,
1273 int *fan, int *temp, int *v05, int *v12)
1275 u_int8_t id1, id2, cnt, div;
1278 if (atadev->flags & ATA_D_ENC_PRESENT) {
1279 ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL);
1280 ata_enclosure_sensor(atadev, 1, 0x4e, 0);
1281 id1 = ata_enclosure_sensor(atadev, 0, 0x4f, 0);
1282 ata_enclosure_sensor(atadev, 1, 0x4e, 0x80);
1283 id2 = ata_enclosure_sensor(atadev, 0, 0x4f, 0);
1284 if (id1 == 0xa3 && id2 == 0x5c) {
1285 div = 1 << (((ata_enclosure_sensor(atadev, 0, 0x5d, 0)&0x20)>>3)+
1286 ((ata_enclosure_sensor(atadev, 0, 0x47, 0)&0x30)>>4)+1);
1287 cnt = ata_enclosure_sensor(atadev, 0, 0x28, 0);
1291 *fan = 1350000 / cnt / div;
1292 ata_enclosure_sensor(atadev, 1, 0x4e, 0x01);
1293 *temp = (ata_enclosure_sensor(atadev, 0, 0x50, 0) * 10) +
1294 (ata_enclosure_sensor(atadev, 0, 0x50, 0) & 0x80 ? 5 : 0);
1295 *v05 = ata_enclosure_sensor(atadev, 0, 0x23, 0) * 27;
1296 *v12 = ata_enclosure_sensor(atadev, 0, 0x24, 0) * 61;
1299 ATA_UNLOCK_CH(atadev->channel);
1305 ata_enclosure_print(struct ata_device *atadev)
1308 int fan, temp, v05, v12;
1310 ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL);
1311 ata_enclosure_start(atadev);
1312 id = ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1314 st = ATA_INB(atadev->channel->r_io, ATA_COUNT);
1316 ata_enclosure_end(atadev);
1317 ATA_UNLOCK_CH(atadev->channel);
1319 switch (id & 0x93) {
1321 ata_prtdev(atadev, "Universal enclosure");
1324 ata_prtdev(atadev, "FastSwap enclosure");
1328 ata_prtdev(atadev, "SuperSwap enclosure");
1331 atadev->flags &= ~ATA_D_ENC_PRESENT;
1334 atadev->flags |= ATA_D_ENC_PRESENT;
1336 if (ata_enclosure_status(atadev, &fan, &temp, &v05, &v12))
1337 printf(" detected\n");
1339 printf(" [FAN:%drpm TEMP:%d.%01dC %d.%03dV %d.%03dV]\n",
1340 fan, temp/10, temp%10, v05/1000, v05%1000, v12/1000, v12%1000);
1344 ata_enclosure_leds(struct ata_device *atadev, u_int8_t color)
1346 if (atadev->flags & ATA_D_ENC_PRESENT) {
1349 ata_enclosure_start(atadev);
1350 reg = ATA_INB(atadev->channel->r_io, ATA_COUNT);
1352 ATA_OUTB(atadev->channel->r_io, ATA_COUNT,
1353 (color & ATA_LED_MASK) | (reg & ~ATA_LED_MASK));
1355 ata_enclosure_end(atadev);
1360 ata_change_mode(struct ata_device *atadev, int mode)
1362 int umode, wmode, pmode;
1365 umode = ata_umode(atadev->param);
1366 wmode = ata_wmode(atadev->param);
1367 pmode = ata_pmode(atadev->param);
1369 switch (mode & ATA_DMA_MASK) {
1371 if ((mode & ATA_MODE_MASK) < umode)
1372 umode = mode & ATA_MODE_MASK;
1375 if ((mode & ATA_MODE_MASK) < wmode)
1376 wmode = mode & ATA_MODE_MASK;
1380 if (((mode & ATA_MODE_MASK) - ATA_PIO0) < pmode)
1381 pmode = (mode & ATA_MODE_MASK) - ATA_PIO0;
1386 s = splbio(); /* interlock non-atomic channel lock */
1387 ATA_SLEEPLOCK_CH(atadev->channel, ATA_ACTIVE);
1388 ata_dmainit(atadev, pmode, wmode, umode);
1389 ATA_UNLOCK_CH(atadev->channel);
1391 ata_start(atadev->channel); /* XXX SOS */
1395 ata_printf(struct ata_channel *ch, int device, const char * fmt, ...)
1401 ret = printf("ata%d: ", device_get_unit(ch->dev));
1403 if (ch->device[ATA_DEV(device)].name)
1404 ret = printf("%s: ", ch->device[ATA_DEV(device)].name);
1406 ret = printf("ata%d-%s: ", device_get_unit(ch->dev),
1407 (device == ATA_MASTER) ? "master" : "slave");
1409 __va_start(ap, fmt);
1410 ret += vprintf(fmt, ap);
1416 ata_prtdev(struct ata_device *atadev, const char * fmt, ...)
1422 ret = printf("%s: ", atadev->name);
1424 ret = printf("ata%d-%s: ", device_get_unit(atadev->channel->dev),
1425 (atadev->unit == ATA_MASTER) ? "master" : "slave");
1426 __va_start(ap, fmt);
1427 ret += vprintf(fmt, ap);
1433 ata_set_name(struct ata_device *atadev, char *name, int lun)
1435 atadev->name = malloc(strlen(name) + 4, M_ATA, M_INTWAIT);
1436 sprintf(atadev->name, "%s%d", name, lun);
1440 ata_free_name(struct ata_device *atadev)
1443 free(atadev->name, M_ATA);
1444 atadev->name = NULL;
1448 ata_get_lun(u_int32_t *map)
1450 int lun = ffs(~*map) - 1;
1457 ata_test_lun(u_int32_t *map, int lun)
1459 return (*map & (1 << lun));
1463 ata_free_lun(u_int32_t *map, int lun)
1465 *map &= ~(1 << lun);
1469 ata_mode2str(int mode)
1472 case ATA_PIO: return "BIOSPIO";
1473 case ATA_PIO0: return "PIO0";
1474 case ATA_PIO1: return "PIO1";
1475 case ATA_PIO2: return "PIO2";
1476 case ATA_PIO3: return "PIO3";
1477 case ATA_PIO4: return "PIO4";
1478 case ATA_DMA: return "BIOSDMA";
1479 case ATA_WDMA2: return "WDMA2";
1480 case ATA_UDMA2: return "UDMA33";
1481 case ATA_UDMA4: return "UDMA66";
1482 case ATA_UDMA5: return "UDMA100";
1483 case ATA_UDMA6: return "UDMA133";
1484 default: return "???";
1489 ata_pmode(struct ata_params *ap)
1491 if (ap->atavalid & ATA_FLAG_64_70) {
1492 if (ap->apiomodes & 2)
1494 if (ap->apiomodes & 1)
1497 if (ap->retired_piomode == 2)
1499 if (ap->retired_piomode == 1)
1501 if (ap->retired_piomode == 0)
1507 ata_wmode(struct ata_params *ap)
1509 if (ap->mwdmamodes & 0x04)
1511 if (ap->mwdmamodes & 0x02)
1513 if (ap->mwdmamodes & 0x01)
1519 ata_umode(struct ata_params *ap)
1521 if (ap->atavalid & ATA_FLAG_88) {
1522 if (ap->udmamodes & 0x40)
1524 if (ap->udmamodes & 0x20)
1526 if (ap->udmamodes & 0x10)
1528 if (ap->udmamodes & 0x08)
1530 if (ap->udmamodes & 0x04)
1532 if (ap->udmamodes & 0x02)
1534 if (ap->udmamodes & 0x01)
1541 bswap(int8_t *buf, int len)
1543 u_int16_t *ptr = (u_int16_t*)(buf + len);
1545 while (--ptr >= (u_int16_t*)buf)
1550 btrim(int8_t *buf, int len)
1554 for (ptr = buf; ptr < buf+len; ++ptr)
1557 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
1562 bpack(int8_t *src, int8_t *dst, int len)
1566 for (i = j = blank = 0 ; i < len; i++) {
1567 if (blank && src[i] == ' ') continue;
1568 if (blank && src[i] != ' ') {
1573 if (src[i] == ' ') {
1587 /* register controlling device */
1588 cdevsw_add(&ata_cdevsw, 0, 0);
1589 make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "ata");
1591 /* register boot attach to be run when interrupts are enabled */
1592 ata_delayed_attach = malloc(sizeof(struct intr_config_hook),
1593 M_TEMP, M_WAITOK | M_ZERO);
1595 ata_delayed_attach->ich_func = (void*)ata_boot_attach;
1596 ata_delayed_attach->ich_desc = "ata";
1597 if (config_intrhook_establish(ata_delayed_attach) != 0) {
1598 printf("ata: config_intrhook_establish failed\n");
1599 free(ata_delayed_attach, M_TEMP);
1602 SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL)