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.21 2004/08/17 20:59:39 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 <machine/stdarg.h>
52 #include <machine/resource.h>
53 #include <machine/bus.h>
54 #include <machine/clock.h>
57 #include <machine/md_var.h>
62 #include "atapi-all.h"
66 struct atapi_request atapi;
69 /* device structures */
70 static d_ioctl_t ataioctl;
71 static struct cdevsw ata_cdevsw = {
79 /* close */ nullclose,
85 /* strategy */ nostrategy,
91 static void ata_boot_attach(void);
92 static void ata_intr(void *);
93 static int ata_getparam(struct ata_device *, u_int8_t);
94 static int ata_service(struct ata_channel *);
95 static void bswap(int8_t *, int);
96 static void btrim(int8_t *, int);
97 static void bpack(int8_t *, int8_t *, int);
98 static void ata_change_mode(struct ata_device *, int);
99 static u_int8_t ata_enclosure_sensor(struct ata_device *, int, u_int8_t, u_int8_t);
100 static int ata_enclosure_status(struct ata_device *, int *, int *, int *, int *);
103 SYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters");
105 int ata_mpipe_size = 4;
106 TUNABLE_INT("hw.ata.mpipe_size", &ata_mpipe_size);
107 SYSCTL_INT(_hw_ata, OID_AUTO, mpipe_size, CTLFLAG_RW, &ata_mpipe_size, 0,
108 "ATA global I/O pipeline max size");
112 devclass_t ata_devclass;
115 static struct intr_config_hook *ata_delayed_attach = NULL;
116 static MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer");
119 #define DEV_ATAPIALL NATAPICD > 0 || NATAPIFD > 0 || \
120 NATAPIST > 0 || NATAPICAM > 0
123 ata_probe(device_t dev)
125 struct ata_channel *ch;
128 if (!dev || !(ch = device_get_softc(dev)))
131 if (ch->r_io || ch->r_altio || ch->r_irq)
134 /* initialize the softc basics */
135 ch->active = ATA_IDLE;
138 rid = ATA_IOADDR_RID;
139 ch->r_io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
140 ATA_IOSIZE, RF_ACTIVE);
144 rid = ATA_ALTADDR_RID;
145 ch->r_altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
146 ATA_ALTIOSIZE, RF_ACTIVE);
150 rid = ATA_BMADDR_RID;
151 ch->r_bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
152 ATA_BMIOSIZE, RF_ACTIVE);
154 ata_printf(ch, -1, "iobase=0x%04x altiobase=0x%04x bmaddr=0x%04x\n",
155 (int)rman_get_start(ch->r_io),
156 (int)rman_get_start(ch->r_altio),
157 (ch->r_bmio) ? (int)rman_get_start(ch->r_bmio) : 0);
161 ch->device[MASTER].channel = ch;
162 ch->device[MASTER].unit = ATA_MASTER;
163 ch->device[MASTER].mode = ATA_PIO;
164 ch->device[SLAVE].channel = ch;
165 ch->device[SLAVE].unit = ATA_SLAVE;
166 ch->device[SLAVE].mode = ATA_PIO;
167 TAILQ_INIT(&ch->ata_queue);
168 TAILQ_INIT(&ch->atapi_queue);
170 mpipe_init(&ch->req_mpipe, M_ATA, sizeof(union ata_request),
171 4, ata_mpipe_size, 0, NULL);
172 mpipe_init(&ch->dma_mpipe, M_DEVBUF, PAGE_SIZE,
173 4, ata_mpipe_size, MPF_NOZERO, NULL);
179 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ch->r_io);
181 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ch->r_altio);
183 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, ch->r_bmio);
185 ata_printf(ch, -1, "probe allocation failed\n");
190 ata_attach(device_t dev)
192 struct ata_channel *ch;
195 if (!dev || !(ch = device_get_softc(dev)))
199 ch->r_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
200 RF_SHAREABLE | RF_ACTIVE);
202 ata_printf(ch, -1, "unable to allocate interrupt\n");
205 if ((error = bus_setup_intr(dev, ch->r_irq, INTR_TYPE_BIO,
206 ata_intr, ch, &ch->ih))) {
207 ata_printf(ch, -1, "unable to setup interrupt\n");
212 * do not attach devices if we are in early boot, this is done later
213 * when interrupts are enabled by a hook into the boot process.
214 * otherwise attach what the probe has found in ch->devices.
216 if (!ata_delayed_attach) {
219 if (ch->devices & ATA_ATA_SLAVE)
220 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
221 ch->devices &= ~ATA_ATA_SLAVE;
222 if (ch->devices & ATA_ATAPI_SLAVE)
223 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
224 ch->devices &= ~ATA_ATAPI_SLAVE;
225 if (ch->devices & ATA_ATA_MASTER)
226 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
227 ch->devices &= ~ATA_ATA_MASTER;
228 if (ch->devices & ATA_ATAPI_MASTER)
229 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
230 ch->devices &= ~ATA_ATAPI_MASTER;
232 if (ch->devices & ATA_ATA_MASTER)
233 ad_attach(&ch->device[MASTER], 0);
234 if (ch->devices & ATA_ATA_SLAVE)
235 ad_attach(&ch->device[SLAVE], 0);
238 if (ch->devices & ATA_ATAPI_MASTER)
239 atapi_attach(&ch->device[MASTER], 0);
240 if (ch->devices & ATA_ATAPI_SLAVE)
241 atapi_attach(&ch->device[SLAVE], 0);
244 atapi_cam_attach_bus(ch);
252 ata_detach(device_t dev)
254 struct ata_channel *ch;
257 if (!dev || !(ch = device_get_softc(dev)) ||
258 !ch->r_io || !ch->r_altio || !ch->r_irq)
261 /* make sure channel is not busy */
263 ATA_SLEEPLOCK_CH(ch, ATA_CONTROL);
265 if (ch->devices & ATA_ATA_MASTER && ch->device[MASTER].driver)
266 ad_detach(&ch->device[MASTER], 1);
267 if (ch->devices & ATA_ATA_SLAVE && ch->device[SLAVE].driver)
268 ad_detach(&ch->device[SLAVE], 1);
271 if (ch->devices & ATA_ATAPI_MASTER && ch->device[MASTER].driver)
272 atapi_detach(&ch->device[MASTER]);
273 if (ch->devices & ATA_ATAPI_SLAVE && ch->device[SLAVE].driver)
274 atapi_detach(&ch->device[SLAVE]);
277 atapi_cam_detach_bus(ch);
281 if (ch->device[MASTER].param) {
282 free(ch->device[MASTER].param, M_ATA);
283 ch->device[MASTER].param = NULL;
285 if (ch->device[SLAVE].param) {
286 free(ch->device[SLAVE].param, M_ATA);
287 ch->device[SLAVE].param = NULL;
289 ch->device[MASTER].driver = NULL;
290 ch->device[SLAVE].driver = NULL;
291 ch->device[MASTER].mode = ATA_PIO;
292 ch->device[SLAVE].mode = ATA_PIO;
296 bus_teardown_intr(dev, ch->r_irq, ch->ih);
297 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
299 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, ch->r_bmio);
300 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ch->r_altio);
301 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ch->r_io);
306 mpipe_done(&ch->req_mpipe);
307 mpipe_done(&ch->dma_mpipe);
314 ata_resume(device_t dev)
316 return ata_reinit(device_get_softc(dev));
320 ataioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, struct thread *td)
322 struct ata_cmd *iocmd = (struct ata_cmd *)addr;
323 struct ata_channel *ch;
324 device_t device = devclass_get_device(ata_devclass, iocmd->channel);
331 if (iocmd->channel < -1 || iocmd->device < -1 || iocmd->device > SLAVE)
334 switch (iocmd->cmd) {
336 /* should enable channel HW on controller that can SOS XXX */
337 error = ata_probe(device);
339 error = ata_attach(device);
343 error = ata_detach(device);
344 /* should disable channel HW on controller that can SOS XXX */
348 if (!device || !(ch = device_get_softc(device)))
350 s = splbio(); /* interlock non-atomic channel lock */
351 ATA_SLEEPLOCK_CH(ch, ATA_ACTIVE);
352 if ((error = ata_reinit(ch)))
358 if (!device || !(ch = device_get_softc(device)))
361 if ((iocmd->device == MASTER || iocmd->device == -1) &&
362 ch->device[MASTER].driver)
363 iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode;
365 iocmd->u.mode.mode[MASTER] = -1;
367 if ((iocmd->device == SLAVE || iocmd->device == -1) &&
368 ch->device[SLAVE].param)
369 iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode;
371 iocmd->u.mode.mode[SLAVE] = -1;
375 if (!device || !(ch = device_get_softc(device)))
378 if ((iocmd->device == MASTER || iocmd->device == -1) &&
379 iocmd->u.mode.mode[MASTER] >= 0 && ch->device[MASTER].param) {
380 ata_change_mode(&ch->device[MASTER],iocmd->u.mode.mode[MASTER]);
381 iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode;
384 iocmd->u.mode.mode[MASTER] = -1;
386 if ((iocmd->device == SLAVE || iocmd->device == -1) &&
387 iocmd->u.mode.mode[SLAVE] >= 0 && ch->device[SLAVE].param) {
388 ata_change_mode(&ch->device[SLAVE], iocmd->u.mode.mode[SLAVE]);
389 iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode;
392 iocmd->u.mode.mode[SLAVE] = -1;
396 if (!device || !(ch = device_get_softc(device)))
399 iocmd->u.param.type[MASTER] =
400 ch->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER);
401 iocmd->u.param.type[SLAVE] =
402 ch->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE);
404 if (ch->device[MASTER].name)
405 strcpy(iocmd->u.param.name[MASTER], ch->device[MASTER].name);
406 if (ch->device[SLAVE].name)
407 strcpy(iocmd->u.param.name[SLAVE], ch->device[SLAVE].name);
409 if (ch->device[MASTER].param)
410 bcopy(ch->device[MASTER].param, &iocmd->u.param.params[MASTER],
411 sizeof(struct ata_params));
412 if (ch->device[SLAVE].param)
413 bcopy(ch->device[SLAVE].param, &iocmd->u.param.params[SLAVE],
414 sizeof(struct ata_params));
418 struct ata_device *atadev;
420 if (!device || !(ch = device_get_softc(device)))
423 if (iocmd->device == SLAVE)
424 atadev = &ch->device[SLAVE];
426 atadev = &ch->device[MASTER];
428 return ata_enclosure_status(atadev,
429 &iocmd->u.enclosure.fan,
430 &iocmd->u.enclosure.temp,
431 &iocmd->u.enclosure.v05,
432 &iocmd->u.enclosure.v12);
437 return ata_raid_rebuild(iocmd->channel);
440 return ata_raid_create(&iocmd->u.raid_setup);
443 return ata_raid_delete(iocmd->channel);
446 return ata_raid_status(iocmd->channel, &iocmd->u.raid_status);
450 struct ata_device *atadev;
453 if (!device || !(ch = device_get_softc(device)))
456 if (!(atadev = &ch->device[iocmd->device]) ||
457 !(ch->devices & (iocmd->device == MASTER ?
458 ATA_ATAPI_MASTER : ATA_ATAPI_SLAVE)))
461 buf = malloc(iocmd->u.atapi.count, M_ATA, M_INTWAIT);
463 if (iocmd->u.atapi.flags & ATAPI_CMD_WRITE) {
464 error = copyin(iocmd->u.atapi.data, buf, iocmd->u.atapi.count);
468 error = atapi_queue_cmd(atadev, iocmd->u.atapi.ccb,
469 buf, iocmd->u.atapi.count,
470 (iocmd->u.atapi.flags == ATAPI_CMD_READ ?
471 ATPR_F_READ : 0) | ATPR_F_QUIET,
472 iocmd->u.atapi.timeout, NULL, NULL);
474 iocmd->u.atapi.error = error;
475 bcopy(&atadev->result, iocmd->u.atapi.sense_data,
476 sizeof(struct atapi_reqsense));
479 else if (iocmd->u.atapi.flags & ATAPI_CMD_READ)
480 error = copyout(buf, iocmd->u.atapi.data, iocmd->u.atapi.count);
493 ata_getparam(struct ata_device *atadev, u_int8_t command)
495 struct ata_params *ata_parm;
498 ata_parm = malloc(sizeof(struct ata_params), M_ATA, M_INTWAIT);
500 /* apparently some devices needs this repeated */
502 if (ata_command(atadev, command, 0, 0, 0, ATA_IMMEDIATE)) {
503 ata_prtdev(atadev, "%s identify failed\n",
504 command == ATA_C_ATAPI_IDENTIFY ? "ATAPI" : "ATA");
505 free(ata_parm, M_ATA);
509 ata_prtdev(atadev, "%s identify retries exceeded\n",
510 command == ATA_C_ATAPI_IDENTIFY ? "ATAPI" : "ATA");
511 free(ata_parm, M_ATA);
514 } while (ata_wait(atadev, ((command == ATA_C_ATAPI_IDENTIFY) ?
515 ATA_S_DRQ : (ATA_S_READY|ATA_S_DSC|ATA_S_DRQ))));
516 ATA_INSW(atadev->channel->r_io, ATA_DATA, (int16_t *)ata_parm,
517 sizeof(struct ata_params)/sizeof(int16_t));
519 if (command == ATA_C_ATA_IDENTIFY ||
520 !((ata_parm->model[0] == 'N' && ata_parm->model[1] == 'E') ||
521 (ata_parm->model[0] == 'F' && ata_parm->model[1] == 'X') ||
522 (ata_parm->model[0] == 'P' && ata_parm->model[1] == 'i')))
523 bswap(ata_parm->model, sizeof(ata_parm->model));
524 btrim(ata_parm->model, sizeof(ata_parm->model));
525 bpack(ata_parm->model, ata_parm->model, sizeof(ata_parm->model));
526 bswap(ata_parm->revision, sizeof(ata_parm->revision));
527 btrim(ata_parm->revision, sizeof(ata_parm->revision));
528 bpack(ata_parm->revision, ata_parm->revision, sizeof(ata_parm->revision));
529 bswap(ata_parm->serial, sizeof(ata_parm->serial));
530 btrim(ata_parm->serial, sizeof(ata_parm->serial));
531 bpack(ata_parm->serial, ata_parm->serial, sizeof(ata_parm->serial));
532 atadev->param = ata_parm;
537 ata_boot_attach(void)
539 struct ata_channel *ch;
542 if (ata_delayed_attach) {
543 config_intrhook_disestablish(ata_delayed_attach);
544 free(ata_delayed_attach, M_TEMP);
545 ata_delayed_attach = NULL;
550 * run through all ata devices and look for real ATA & ATAPI devices
551 * using the hints we found in the early probe, this avoids some of
552 * the delays probing of non-exsistent devices can cause.
554 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
555 if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
557 if (ch->devices & ATA_ATA_SLAVE)
558 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
559 ch->devices &= ~ATA_ATA_SLAVE;
560 if (ch->devices & ATA_ATAPI_SLAVE)
561 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
562 ch->devices &= ~ATA_ATAPI_SLAVE;
563 if (ch->devices & ATA_ATA_MASTER)
564 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
565 ch->devices &= ~ATA_ATA_MASTER;
566 if (ch->devices & ATA_ATAPI_MASTER)
567 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
568 ch->devices &= ~ATA_ATAPI_MASTER;
572 /* now we know whats there, do the real attach, first the ATA disks */
573 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
574 if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
576 if (ch->devices & ATA_ATA_MASTER)
577 ad_attach(&ch->device[MASTER], 0);
578 if (ch->devices & ATA_ATA_SLAVE)
579 ad_attach(&ch->device[SLAVE], 0);
584 /* then the atapi devices */
585 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
586 if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
588 if (ch->devices & ATA_ATAPI_MASTER)
589 atapi_attach(&ch->device[MASTER], 0);
590 if (ch->devices & ATA_ATAPI_SLAVE)
591 atapi_attach(&ch->device[SLAVE], 0);
593 atapi_cam_attach_bus(ch);
603 struct ata_channel *ch = (struct ata_channel *)data;
605 * on PCI systems we might share an interrupt line with another
606 * device or our twin ATA channel, so call ch->intr_func to figure
607 * out if it is really an interrupt we should process here
609 if (ch->intr_func && ch->intr_func(ch))
612 /* if drive is busy it didn't interrupt */
613 if (ATA_INB(ch->r_altio, ATA_ALTSTAT) & ATA_S_BUSY) {
615 if (!(ATA_INB(ch->r_altio, ATA_ALTSTAT) & ATA_S_DRQ))
619 /* clear interrupt and get status */
620 ch->status = ATA_INB(ch->r_io, ATA_STATUS);
622 if (ch->status & ATA_S_ERROR)
623 ch->error = ATA_INB(ch->r_io, ATA_ERROR);
625 /* find & call the responsible driver to process this interrupt */
626 switch (ch->active) {
629 if (!ch->running || ad_interrupt(ch->running) == ATA_OP_CONTINUES)
634 case ATA_ACTIVE_ATAPI:
635 if (!ch->running || atapi_interrupt(ch->running) == ATA_OP_CONTINUES)
640 case ATA_WAIT_INTR | ATA_CONTROL:
645 case ATA_WAIT_READY | ATA_CONTROL:
649 if (ch->flags & ATA_QUEUED) {
650 ch->active = ATA_ACTIVE;
651 if (ata_service(ch) == ATA_OP_CONTINUES)
659 static int intr_count = 0;
661 if (intr_count++ < 10)
662 ata_printf(ch, -1, "unwanted interrupt #%d active=%02x s=%02x\n",
663 intr_count, ch->active, ch->status);
668 ch->active &= ATA_CONTROL;
669 if (ch->active & ATA_CONTROL)
677 ata_start(struct ata_channel *ch)
680 struct ad_request *ad_request;
683 struct atapi_request *atapi_request;
687 s = splbio(); /* interlock non-atomic channel lock */
688 if (!ATA_LOCK_CH(ch, ATA_ACTIVE)) {
694 /* find & call the responsible driver if anything on the ATA queue */
695 if (TAILQ_EMPTY(&ch->ata_queue)) {
696 if (ch->devices & (ATA_ATA_MASTER) && ch->device[MASTER].driver)
697 ad_start(&ch->device[MASTER]);
698 if (ch->devices & (ATA_ATA_SLAVE) && ch->device[SLAVE].driver)
699 ad_start(&ch->device[SLAVE]);
701 if ((ad_request = TAILQ_FIRST(&ch->ata_queue))) {
702 TAILQ_REMOVE(&ch->ata_queue, ad_request, chain);
703 ch->active = ATA_ACTIVE_ATA;
704 ch->running = ad_request;
707 * The donecount had better be 0 here because the channel may not
708 * have retained the setup for the request (if a retry).
710 KKASSERT(ad_request->donecount == 0);
711 if (ad_transfer(ad_request) == ATA_OP_CONTINUES) {
719 /* find & call the responsible driver if anything on the ATAPI queue */
720 if (TAILQ_EMPTY(&ch->atapi_queue)) {
721 if (ch->devices & (ATA_ATAPI_MASTER) && ch->device[MASTER].driver)
722 atapi_start(&ch->device[MASTER]);
723 if (ch->devices & (ATA_ATAPI_SLAVE) && ch->device[SLAVE].driver)
724 atapi_start(&ch->device[SLAVE]);
726 if ((atapi_request = TAILQ_FIRST(&ch->atapi_queue))) {
727 TAILQ_REMOVE(&ch->atapi_queue, atapi_request, chain);
728 ch->active = ATA_ACTIVE_ATAPI;
729 ch->running = atapi_request;
730 if (atapi_transfer(atapi_request) == ATA_OP_CONTINUES) {
741 ata_reset(struct ata_channel *ch)
743 u_int8_t lsb, msb, ostat0, ostat1;
744 u_int8_t stat0 = 0, stat1 = 0;
745 int mask = 0, timeout;
747 /* do we have any signs of ATA/ATAPI HW being present ? */
748 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
750 ostat0 = ATA_INB(ch->r_io, ATA_STATUS);
751 if ((ostat0 & 0xf8) != 0xf8 && ostat0 != 0xa5) {
755 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
757 ostat1 = ATA_INB(ch->r_io, ATA_STATUS);
758 if ((ostat1 & 0xf8) != 0xf8 && ostat1 != 0xa5) {
767 /* in some setups we dont want to test for a slave */
768 if (ch->flags & ATA_NO_SLAVE) {
774 ata_printf(ch, -1, "mask=%02x ostat0=%02x ostat2=%02x\n",
775 mask, ostat0, ostat1);
778 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
780 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_RESET);
782 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_IDS);
784 ATA_INB(ch->r_io, ATA_ERROR);
786 /* wait for BUSY to go inactive */
787 for (timeout = 0; timeout < 3100; timeout++) {
788 if (stat0 & ATA_S_BUSY) {
789 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
792 /* check for ATAPI signature while its still there */
793 lsb = ATA_INB(ch->r_io, ATA_CYL_LSB);
794 msb = ATA_INB(ch->r_io, ATA_CYL_MSB);
795 stat0 = ATA_INB(ch->r_io, ATA_STATUS);
796 if (!(stat0 & ATA_S_BUSY)) {
798 ata_printf(ch, ATA_MASTER, "ATAPI %02x %02x\n", lsb, msb);
799 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB)
800 ch->devices |= ATA_ATAPI_MASTER;
803 if (stat1 & ATA_S_BUSY) {
804 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
807 /* check for ATAPI signature while its still there */
808 lsb = ATA_INB(ch->r_io, ATA_CYL_LSB);
809 msb = ATA_INB(ch->r_io, ATA_CYL_MSB);
810 stat1 = ATA_INB(ch->r_io, ATA_STATUS);
811 if (!(stat1 & ATA_S_BUSY)) {
813 ata_printf(ch, ATA_SLAVE, "ATAPI %02x %02x\n", lsb, msb);
814 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB)
815 ch->devices |= ATA_ATAPI_SLAVE;
818 if (mask == 0x01) /* wait for master only */
819 if (!(stat0 & ATA_S_BUSY))
821 if (mask == 0x02) /* wait for slave only */
822 if (!(stat1 & ATA_S_BUSY))
824 if (mask == 0x03) /* wait for both master & slave */
825 if (!(stat0 & ATA_S_BUSY) && !(stat1 & ATA_S_BUSY))
830 * some devices release BUSY before they are ready to accept commands.
831 * We must wait at least 50ms before attempting to issue a command after
835 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
837 if (stat0 & ATA_S_BUSY)
839 if (stat1 & ATA_S_BUSY)
842 ata_printf(ch, -1, "mask=%02x stat0=%02x stat1=%02x\n",
847 if (mask & 0x01 && ostat0 != 0x00 && !(ch->devices & ATA_ATAPI_MASTER)) {
848 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
850 ATA_OUTB(ch->r_io, ATA_ERROR, 0x58);
851 ATA_OUTB(ch->r_io, ATA_CYL_LSB, 0xa5);
852 lsb = ATA_INB(ch->r_io, ATA_ERROR);
853 msb = ATA_INB(ch->r_io, ATA_CYL_LSB);
855 ata_printf(ch, ATA_MASTER, "ATA %02x %02x\n", lsb, msb);
856 if (lsb != 0x58 && msb == 0xa5)
857 ch->devices |= ATA_ATA_MASTER;
859 if (mask & 0x02 && ostat1 != 0x00 && !(ch->devices & ATA_ATAPI_SLAVE)) {
860 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
862 ATA_OUTB(ch->r_io, ATA_ERROR, 0x58);
863 ATA_OUTB(ch->r_io, ATA_CYL_LSB, 0xa5);
864 lsb = ATA_INB(ch->r_io, ATA_ERROR);
865 msb = ATA_INB(ch->r_io, ATA_CYL_LSB);
867 ata_printf(ch, ATA_SLAVE, "ATA %02x %02x\n", lsb, msb);
868 if (lsb != 0x58 && msb == 0xa5)
869 ch->devices |= ATA_ATA_SLAVE;
872 ata_printf(ch, -1, "devices=%02x\n", ch->devices);
876 ata_reinit(struct ata_channel *ch)
878 int devices, misdev, newdev;
880 if (!ch->r_io || !ch->r_altio || !ch->r_irq)
883 ATA_FORCELOCK_CH(ch, ATA_CONTROL);
885 devices = ch->devices;
886 ata_printf(ch, -1, "resetting devices .. ");
889 if ((misdev = devices & ~ch->devices)) {
893 if (misdev & ATA_ATA_MASTER && ch->device[MASTER].driver)
894 ad_detach(&ch->device[MASTER], 0);
895 if (misdev & ATA_ATA_SLAVE && ch->device[SLAVE].driver)
896 ad_detach(&ch->device[SLAVE], 0);
899 if (misdev & ATA_ATAPI_MASTER && ch->device[MASTER].driver)
900 atapi_detach(&ch->device[MASTER]);
901 if (misdev & ATA_ATAPI_SLAVE && ch->device[SLAVE].driver)
902 atapi_detach(&ch->device[SLAVE]);
904 if (misdev & ATA_ATA_MASTER || misdev & ATA_ATAPI_MASTER) {
905 if (ch->device[MASTER].param)
906 free(ch->device[MASTER].param, M_ATA);
907 ch->device[MASTER].param = NULL;
909 if (misdev & ATA_ATA_SLAVE || misdev & ATA_ATAPI_SLAVE) {
910 if (ch->device[SLAVE].param)
911 free(ch->device[SLAVE].param, M_ATA);
912 ch->device[SLAVE].param = NULL;
915 if ((newdev = ~devices & ch->devices)) {
916 if (newdev & ATA_ATA_MASTER)
917 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
918 ch->devices &= ~ATA_ATA_MASTER;
919 if (newdev & ATA_ATA_SLAVE)
920 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
921 ch->devices &= ~ATA_ATA_SLAVE;
922 if (newdev & ATA_ATAPI_MASTER)
923 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
924 ch->devices &= ~ATA_ATAPI_MASTER;
925 if (newdev & ATA_ATAPI_SLAVE)
926 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
927 ch->devices &= ~ATA_ATAPI_SLAVE;
929 newdev = ~devices & ch->devices;
930 if (!misdev && newdev)
933 if (newdev & ATA_ATA_MASTER && !ch->device[MASTER].driver)
934 ad_attach(&ch->device[MASTER], 1);
935 else if (ch->devices & ATA_ATA_MASTER && ch->device[MASTER].driver) {
936 ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY);
937 ad_reinit(&ch->device[MASTER]);
939 if (newdev & ATA_ATA_SLAVE && !ch->device[SLAVE].driver)
940 ad_attach(&ch->device[SLAVE], 1);
941 else if (ch->devices & (ATA_ATA_SLAVE) && ch->device[SLAVE].driver) {
942 ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY);
943 ad_reinit(&ch->device[SLAVE]);
947 if (newdev & ATA_ATAPI_MASTER && !ch->device[MASTER].driver)
948 atapi_attach(&ch->device[MASTER], 1);
949 else if (ch->devices & (ATA_ATAPI_MASTER) && ch->device[MASTER].driver) {
950 ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY);
951 atapi_reinit(&ch->device[MASTER]);
953 if (newdev & ATA_ATAPI_SLAVE && !ch->device[SLAVE].driver)
954 atapi_attach(&ch->device[SLAVE], 1);
955 else if (ch->devices & (ATA_ATAPI_SLAVE) && ch->device[SLAVE].driver) {
956 ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY);
957 atapi_reinit(&ch->device[SLAVE]);
961 if (ch->devices & (ATA_ATAPI_MASTER | ATA_ATAPI_SLAVE))
962 atapi_cam_reinit_bus(ch);
971 ata_service(struct ata_channel *ch)
973 /* do we have a SERVICE request from the drive ? */
974 if ((ch->status & (ATA_S_SERVICE|ATA_S_ERROR|ATA_S_DRQ)) == ATA_S_SERVICE) {
975 ATA_OUTB(ch->r_bmio, ATA_BMSTAT_PORT,
976 ata_dmastatus(ch) | ATA_BMSTAT_INTERRUPT);
978 if ((ATA_INB(ch->r_io, ATA_DRIVE) & ATA_SLAVE) == ATA_MASTER) {
979 if ((ch->devices & ATA_ATA_MASTER) && ch->device[MASTER].driver)
980 return ad_service((struct ad_softc *)
981 ch->device[MASTER].driver, 0);
984 if ((ch->devices & ATA_ATA_SLAVE) && ch->device[SLAVE].driver)
985 return ad_service((struct ad_softc *)
986 ch->device[SLAVE].driver, 0);
990 return ATA_OP_FINISHED;
994 ata_wait(struct ata_device *atadev, u_int8_t mask)
999 while (timeout < 5000000) { /* timeout 5 secs */
1000 atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS);
1002 /* if drive fails status, reselect the drive just to be sure */
1003 if (atadev->channel->status == 0xff) {
1004 ata_prtdev(atadev, "no status, reselecting device\n");
1005 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM|atadev->unit);
1007 atadev->channel->status = ATA_INB(atadev->channel->r_io,ATA_STATUS);
1008 if (atadev->channel->status == 0xff)
1013 if (!(atadev->channel->status & ATA_S_BUSY))
1016 if (timeout > 1000) {
1025 if (atadev->channel->status & ATA_S_ERROR)
1026 atadev->channel->error = ATA_INB(atadev->channel->r_io, ATA_ERROR);
1027 if (timeout >= 5000000)
1030 return (atadev->channel->status & ATA_S_ERROR);
1032 /* Wait 50 msec for bits wanted. */
1035 atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS);
1036 if ((atadev->channel->status & mask) == mask) {
1037 if (atadev->channel->status & ATA_S_ERROR)
1038 atadev->channel->error=ATA_INB(atadev->channel->r_io,ATA_ERROR);
1039 return (atadev->channel->status & ATA_S_ERROR);
1047 ata_command(struct ata_device *atadev, u_int8_t command,
1048 u_int64_t lba, u_int16_t count, u_int8_t feature, int flags)
1052 ata_prtdev(atadev, "ata_command: addr=%04lx, cmd=%02x, "
1053 "lba=%lld, count=%d, feature=%d, flags=%02x\n",
1054 rman_get_start(atadev->channel->r_io),
1055 command, lba, count, feature, flags);
1059 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1061 /* disable interrupt from device */
1062 if (atadev->channel->flags & ATA_QUEUED)
1063 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_4BIT);
1065 /* ready to issue command ? */
1066 if (ata_wait(atadev, 0) < 0) {
1067 ata_prtdev(atadev, "timeout sending command=%02x s=%02x e=%02x\n",
1068 command, atadev->channel->status, atadev->channel->error);
1072 /* only use 48bit addressing if needed because of the overhead */
1073 if ((lba > 268435455 || count > 256) && atadev->param &&
1074 atadev->param->support.address48) {
1075 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, (feature>>8) & 0xff);
1076 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, feature);
1077 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, (count>>8) & 0xff);
1078 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, count & 0xff);
1079 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, (lba>>24) & 0xff);
1080 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, lba & 0xff);
1081 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>32) & 0xff);
1082 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>8) & 0xff);
1083 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>40) & 0xff);
1084 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>16) & 0xff);
1085 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_LBA | atadev->unit);
1087 /* translate command into 48bit version */
1090 command = ATA_C_READ48; break;
1091 case ATA_C_READ_MUL:
1092 command = ATA_C_READ_MUL48; break;
1093 case ATA_C_READ_DMA:
1094 command = ATA_C_READ_DMA48; break;
1095 case ATA_C_READ_DMA_QUEUED:
1096 command = ATA_C_READ_DMA_QUEUED48; break;
1098 command = ATA_C_WRITE48; break;
1099 case ATA_C_WRITE_MUL:
1100 command = ATA_C_WRITE_MUL48; break;
1101 case ATA_C_WRITE_DMA:
1102 command = ATA_C_WRITE_DMA48; break;
1103 case ATA_C_WRITE_DMA_QUEUED:
1104 command = ATA_C_WRITE_DMA_QUEUED48; break;
1105 case ATA_C_FLUSHCACHE:
1106 command = ATA_C_FLUSHCACHE48; break;
1108 ata_prtdev(atadev, "can't translate cmd to 48bit version\n");
1113 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, feature);
1114 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, count);
1115 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, lba & 0xff);
1116 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>8) & 0xff);
1117 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>16) & 0xff);
1118 if (atadev->flags & ATA_D_USE_CHS)
1119 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE,
1120 ATA_D_IBM | atadev->unit | ((lba>>24) & 0xf));
1122 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE,
1123 ATA_D_IBM | ATA_D_LBA | atadev->unit | ((lba>>24) &0xf));
1126 switch (flags & ATA_WAIT_MASK) {
1128 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1130 /* enable interrupt */
1131 if (atadev->channel->flags & ATA_QUEUED)
1132 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
1136 atadev->channel->active |= ATA_WAIT_INTR;
1137 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1139 /* enable interrupt */
1140 if (atadev->channel->flags & ATA_QUEUED)
1141 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
1143 if (tsleep((caddr_t)atadev->channel, 0, "atacmd", 10 * hz)) {
1144 ata_prtdev(atadev, "timeout waiting for interrupt\n");
1145 atadev->channel->active &= ~ATA_WAIT_INTR;
1150 case ATA_WAIT_READY:
1151 atadev->channel->active |= ATA_WAIT_READY;
1152 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1153 if (ata_wait(atadev, ATA_S_READY) < 0) {
1154 ata_prtdev(atadev, "timeout waiting for cmd=%02x s=%02x e=%02x\n",
1155 command, atadev->channel->status,atadev->channel->error);
1158 atadev->channel->active &= ~ATA_WAIT_READY;
1165 ata_enclosure_start(struct ata_device *atadev)
1167 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1169 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1171 ATA_INB(atadev->channel->r_io, ATA_CMD);
1173 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
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_INB(atadev->channel->r_io, ATA_COUNT);
1181 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1186 ata_enclosure_end(struct ata_device *atadev)
1188 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1193 ata_enclosure_chip_start(struct ata_device *atadev)
1195 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0b);
1196 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0a);
1198 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08);
1202 ata_enclosure_chip_end(struct ata_device *atadev)
1204 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08);
1206 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0a);
1208 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0b);
1213 ata_enclosure_chip_rdbit(struct ata_device *atadev)
1217 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0);
1219 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x02);
1221 val = ATA_INB(atadev->channel->r_io, ATA_SECTOR) & 0x01;
1227 ata_enclosure_chip_wrbit(struct ata_device *atadev, u_int8_t data)
1229 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08 | (data & 0x01));
1231 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08 | 0x02 | (data & 0x01));
1236 ata_enclosure_chip_rw(struct ata_device *atadev, int rw, u_int8_t val)
1241 for (i = 0; i < 8; i++)
1242 ata_enclosure_chip_wrbit(atadev, (val & (0x80 >> i)) ? 1 : 0);
1245 for (i = 0; i < 8; i++)
1246 val = (val << 1) | ata_enclosure_chip_rdbit(atadev);
1248 ata_enclosure_chip_wrbit(atadev, 0);
1253 ata_enclosure_sensor(struct ata_device *atadev,
1254 int rw, u_int8_t idx, u_int8_t data)
1256 ata_enclosure_start(atadev);
1257 ata_enclosure_chip_start(atadev);
1258 ata_enclosure_chip_rw(atadev, 1, 0x5a);
1259 ata_enclosure_chip_rw(atadev, 1, idx);
1261 ata_enclosure_chip_rw(atadev, 1, data);
1264 ata_enclosure_chip_end(atadev);
1265 ata_enclosure_chip_start(atadev);
1266 ata_enclosure_chip_rw(atadev, 1, 0x5b);
1267 data = ata_enclosure_chip_rw(atadev, 0, 0);
1269 ata_enclosure_chip_end(atadev);
1270 ata_enclosure_end(atadev);
1275 ata_enclosure_status(struct ata_device *atadev,
1276 int *fan, int *temp, int *v05, int *v12)
1278 u_int8_t id1, id2, cnt, div;
1281 if (atadev->flags & ATA_D_ENC_PRESENT) {
1282 ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL);
1283 ata_enclosure_sensor(atadev, 1, 0x4e, 0);
1284 id1 = ata_enclosure_sensor(atadev, 0, 0x4f, 0);
1285 ata_enclosure_sensor(atadev, 1, 0x4e, 0x80);
1286 id2 = ata_enclosure_sensor(atadev, 0, 0x4f, 0);
1287 if (id1 == 0xa3 && id2 == 0x5c) {
1288 div = 1 << (((ata_enclosure_sensor(atadev, 0, 0x5d, 0)&0x20)>>3)+
1289 ((ata_enclosure_sensor(atadev, 0, 0x47, 0)&0x30)>>4)+1);
1290 cnt = ata_enclosure_sensor(atadev, 0, 0x28, 0);
1294 *fan = 1350000 / cnt / div;
1295 ata_enclosure_sensor(atadev, 1, 0x4e, 0x01);
1296 *temp = (ata_enclosure_sensor(atadev, 0, 0x50, 0) * 10) +
1297 (ata_enclosure_sensor(atadev, 0, 0x50, 0) & 0x80 ? 5 : 0);
1298 *v05 = ata_enclosure_sensor(atadev, 0, 0x23, 0) * 27;
1299 *v12 = ata_enclosure_sensor(atadev, 0, 0x24, 0) * 61;
1302 ATA_UNLOCK_CH(atadev->channel);
1308 ata_enclosure_print(struct ata_device *atadev)
1311 int fan, temp, v05, v12;
1313 ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL);
1314 ata_enclosure_start(atadev);
1315 id = ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1317 st = ATA_INB(atadev->channel->r_io, ATA_COUNT);
1319 ata_enclosure_end(atadev);
1320 ATA_UNLOCK_CH(atadev->channel);
1322 switch (id & 0x93) {
1324 ata_prtdev(atadev, "Universal enclosure");
1327 ata_prtdev(atadev, "FastSwap enclosure");
1331 ata_prtdev(atadev, "SuperSwap enclosure");
1334 atadev->flags &= ~ATA_D_ENC_PRESENT;
1337 atadev->flags |= ATA_D_ENC_PRESENT;
1339 if (ata_enclosure_status(atadev, &fan, &temp, &v05, &v12))
1340 printf(" detected\n");
1342 printf(" [FAN:%drpm TEMP:%d.%01dC %d.%03dV %d.%03dV]\n",
1343 fan, temp/10, temp%10, v05/1000, v05%1000, v12/1000, v12%1000);
1347 ata_enclosure_leds(struct ata_device *atadev, u_int8_t color)
1349 if (atadev->flags & ATA_D_ENC_PRESENT) {
1352 ata_enclosure_start(atadev);
1353 reg = ATA_INB(atadev->channel->r_io, ATA_COUNT);
1355 ATA_OUTB(atadev->channel->r_io, ATA_COUNT,
1356 (color & ATA_LED_MASK) | (reg & ~ATA_LED_MASK));
1358 ata_enclosure_end(atadev);
1363 ata_change_mode(struct ata_device *atadev, int mode)
1365 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 s = splbio(); /* 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 if (config_intrhook_establish(ata_delayed_attach) != 0) {
1600 printf("ata: config_intrhook_establish failed\n");
1601 free(ata_delayed_attach, M_TEMP);
1604 SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL)