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.26 2005/06/03 21:56:23 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 <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");
203 if ((error = bus_setup_intr(dev, ch->r_irq, INTR_TYPE_BIO,
204 ata_intr, ch, &ch->ih, NULL))) {
205 ata_printf(ch, -1, "unable to setup interrupt\n");
210 * do not attach devices if we are in early boot, this is done later
211 * when interrupts are enabled by a hook into the boot process.
212 * otherwise attach what the probe has found in ch->devices.
214 if (!ata_delayed_attach) {
217 if (ch->devices & ATA_ATA_SLAVE)
218 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
219 ch->devices &= ~ATA_ATA_SLAVE;
220 if (ch->devices & ATA_ATAPI_SLAVE)
221 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
222 ch->devices &= ~ATA_ATAPI_SLAVE;
223 if (ch->devices & ATA_ATA_MASTER)
224 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
225 ch->devices &= ~ATA_ATA_MASTER;
226 if (ch->devices & ATA_ATAPI_MASTER)
227 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
228 ch->devices &= ~ATA_ATAPI_MASTER;
230 if (ch->devices & ATA_ATA_MASTER)
231 ad_attach(&ch->device[MASTER], 0);
232 if (ch->devices & ATA_ATA_SLAVE)
233 ad_attach(&ch->device[SLAVE], 0);
236 if (ch->devices & ATA_ATAPI_MASTER)
237 atapi_attach(&ch->device[MASTER], 0);
238 if (ch->devices & ATA_ATAPI_SLAVE)
239 atapi_attach(&ch->device[SLAVE], 0);
242 atapi_cam_attach_bus(ch);
250 ata_detach(device_t dev)
252 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);
327 if (iocmd->channel < -1 || iocmd->device < -1 || iocmd->device > SLAVE)
330 switch (iocmd->cmd) {
332 /* should enable channel HW on controller that can SOS XXX */
333 error = ata_probe(device);
335 error = ata_attach(device);
339 error = ata_detach(device);
340 /* should disable channel HW on controller that can SOS XXX */
344 if (!device || !(ch = device_get_softc(device)))
346 crit_enter(); /* interlock non-atomic channel lock */
347 ATA_SLEEPLOCK_CH(ch, ATA_ACTIVE);
348 if ((error = ata_reinit(ch)))
354 if (!device || !(ch = device_get_softc(device)))
357 if ((iocmd->device == MASTER || iocmd->device == -1) &&
358 ch->device[MASTER].driver)
359 iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode;
361 iocmd->u.mode.mode[MASTER] = -1;
363 if ((iocmd->device == SLAVE || iocmd->device == -1) &&
364 ch->device[SLAVE].param)
365 iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode;
367 iocmd->u.mode.mode[SLAVE] = -1;
371 if (!device || !(ch = device_get_softc(device)))
374 if ((iocmd->device == MASTER || iocmd->device == -1) &&
375 iocmd->u.mode.mode[MASTER] >= 0 && ch->device[MASTER].param) {
376 ata_change_mode(&ch->device[MASTER],iocmd->u.mode.mode[MASTER]);
377 iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode;
380 iocmd->u.mode.mode[MASTER] = -1;
382 if ((iocmd->device == SLAVE || iocmd->device == -1) &&
383 iocmd->u.mode.mode[SLAVE] >= 0 && ch->device[SLAVE].param) {
384 ata_change_mode(&ch->device[SLAVE], iocmd->u.mode.mode[SLAVE]);
385 iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode;
388 iocmd->u.mode.mode[SLAVE] = -1;
392 if (!device || !(ch = device_get_softc(device)))
395 iocmd->u.param.type[MASTER] =
396 ch->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER);
397 iocmd->u.param.type[SLAVE] =
398 ch->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE);
400 if (ch->device[MASTER].name)
401 strcpy(iocmd->u.param.name[MASTER], ch->device[MASTER].name);
402 if (ch->device[SLAVE].name)
403 strcpy(iocmd->u.param.name[SLAVE], ch->device[SLAVE].name);
405 if (ch->device[MASTER].param)
406 bcopy(ch->device[MASTER].param, &iocmd->u.param.params[MASTER],
407 sizeof(struct ata_params));
408 if (ch->device[SLAVE].param)
409 bcopy(ch->device[SLAVE].param, &iocmd->u.param.params[SLAVE],
410 sizeof(struct ata_params));
414 struct ata_device *atadev;
416 if (!device || !(ch = device_get_softc(device)))
419 if (iocmd->device == SLAVE)
420 atadev = &ch->device[SLAVE];
422 atadev = &ch->device[MASTER];
424 return ata_enclosure_status(atadev,
425 &iocmd->u.enclosure.fan,
426 &iocmd->u.enclosure.temp,
427 &iocmd->u.enclosure.v05,
428 &iocmd->u.enclosure.v12);
433 return ata_raid_rebuild(iocmd->channel);
436 return ata_raid_create(&iocmd->u.raid_setup);
439 return ata_raid_delete(iocmd->channel);
442 return ata_raid_status(iocmd->channel, &iocmd->u.raid_status);
446 struct ata_device *atadev;
449 if (!device || !(ch = device_get_softc(device)))
452 if (!(atadev = &ch->device[iocmd->device]) ||
453 !(ch->devices & (iocmd->device == MASTER ?
454 ATA_ATAPI_MASTER : ATA_ATAPI_SLAVE)))
457 buf = malloc(iocmd->u.atapi.count, M_ATA, M_INTWAIT);
459 if (iocmd->u.atapi.flags & ATAPI_CMD_WRITE) {
460 error = copyin(iocmd->u.atapi.data, buf, iocmd->u.atapi.count);
464 error = atapi_queue_cmd(atadev, iocmd->u.atapi.ccb,
465 buf, iocmd->u.atapi.count,
466 (iocmd->u.atapi.flags == ATAPI_CMD_READ ?
467 ATPR_F_READ : 0) | ATPR_F_QUIET,
468 iocmd->u.atapi.timeout, NULL, NULL);
470 iocmd->u.atapi.error = error;
471 bcopy(&atadev->result, iocmd->u.atapi.sense_data,
472 sizeof(struct atapi_reqsense));
475 else if (iocmd->u.atapi.flags & ATAPI_CMD_READ)
476 error = copyout(buf, iocmd->u.atapi.data, iocmd->u.atapi.count);
489 ata_getparam(struct ata_device *atadev, u_int8_t command)
491 struct ata_params *ata_parm;
494 ata_parm = malloc(sizeof(struct ata_params), M_ATA, M_INTWAIT);
496 /* apparently some devices needs this repeated */
498 if (ata_command(atadev, command, 0, 0, 0, ATA_IMMEDIATE)) {
499 ata_prtdev(atadev, "%s identify failed\n",
500 command == ATA_C_ATAPI_IDENTIFY ? "ATAPI" : "ATA");
501 free(ata_parm, M_ATA);
505 ata_prtdev(atadev, "%s identify retries exceeded\n",
506 command == ATA_C_ATAPI_IDENTIFY ? "ATAPI" : "ATA");
507 free(ata_parm, M_ATA);
510 } while (ata_wait(atadev, ((command == ATA_C_ATAPI_IDENTIFY) ?
511 ATA_S_DRQ : (ATA_S_READY|ATA_S_DSC|ATA_S_DRQ))));
512 ATA_INSW(atadev->channel->r_io, ATA_DATA, (int16_t *)ata_parm,
513 sizeof(struct ata_params)/sizeof(int16_t));
515 if (command == ATA_C_ATA_IDENTIFY ||
516 !((ata_parm->model[0] == 'N' && ata_parm->model[1] == 'E') ||
517 (ata_parm->model[0] == 'F' && ata_parm->model[1] == 'X') ||
518 (ata_parm->model[0] == 'P' && ata_parm->model[1] == 'i')))
519 bswap(ata_parm->model, sizeof(ata_parm->model));
520 btrim(ata_parm->model, sizeof(ata_parm->model));
521 bpack(ata_parm->model, ata_parm->model, sizeof(ata_parm->model));
522 bswap(ata_parm->revision, sizeof(ata_parm->revision));
523 btrim(ata_parm->revision, sizeof(ata_parm->revision));
524 bpack(ata_parm->revision, ata_parm->revision, sizeof(ata_parm->revision));
525 bswap(ata_parm->serial, sizeof(ata_parm->serial));
526 btrim(ata_parm->serial, sizeof(ata_parm->serial));
527 bpack(ata_parm->serial, ata_parm->serial, sizeof(ata_parm->serial));
528 atadev->param = ata_parm;
533 ata_boot_attach(void)
535 struct ata_channel *ch;
538 if (ata_delayed_attach) {
539 config_intrhook_disestablish(ata_delayed_attach);
540 free(ata_delayed_attach, M_TEMP);
541 ata_delayed_attach = NULL;
546 * run through all ata devices and look for real ATA & ATAPI devices
547 * using the hints we found in the early probe, this avoids some of
548 * the delays probing of non-exsistent devices can cause.
550 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
551 if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
553 if (ch->devices & ATA_ATA_SLAVE)
554 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
555 ch->devices &= ~ATA_ATA_SLAVE;
556 if (ch->devices & ATA_ATAPI_SLAVE)
557 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
558 ch->devices &= ~ATA_ATAPI_SLAVE;
559 if (ch->devices & ATA_ATA_MASTER)
560 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
561 ch->devices &= ~ATA_ATA_MASTER;
562 if (ch->devices & ATA_ATAPI_MASTER)
563 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
564 ch->devices &= ~ATA_ATAPI_MASTER;
568 /* now we know whats there, do the real attach, first the ATA disks */
569 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
570 if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
572 if (ch->devices & ATA_ATA_MASTER)
573 ad_attach(&ch->device[MASTER], 0);
574 if (ch->devices & ATA_ATA_SLAVE)
575 ad_attach(&ch->device[SLAVE], 0);
580 /* then the atapi devices */
581 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
582 if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
584 if (ch->devices & ATA_ATAPI_MASTER)
585 atapi_attach(&ch->device[MASTER], 0);
586 if (ch->devices & ATA_ATAPI_SLAVE)
587 atapi_attach(&ch->device[SLAVE], 0);
589 atapi_cam_attach_bus(ch);
599 struct ata_channel *ch = (struct ata_channel *)data;
601 * on PCI systems we might share an interrupt line with another
602 * device or our twin ATA channel, so call ch->intr_func to figure
603 * out if it is really an interrupt we should process here
605 if (ch->intr_func && ch->intr_func(ch))
608 /* if drive is busy it didn't interrupt */
609 if (ATA_INB(ch->r_altio, ATA_ALTSTAT) & ATA_S_BUSY) {
611 if (!(ATA_INB(ch->r_altio, ATA_ALTSTAT) & ATA_S_DRQ))
615 /* clear interrupt and get status */
616 ch->status = ATA_INB(ch->r_io, ATA_STATUS);
618 if (ch->status & ATA_S_ERROR)
619 ch->error = ATA_INB(ch->r_io, ATA_ERROR);
621 /* find & call the responsible driver to process this interrupt */
622 switch (ch->active) {
625 if (!ch->running || ad_interrupt(ch->running) == ATA_OP_CONTINUES)
630 case ATA_ACTIVE_ATAPI:
631 if (!ch->running || atapi_interrupt(ch->running) == ATA_OP_CONTINUES)
636 case ATA_WAIT_INTR | ATA_CONTROL:
641 case ATA_WAIT_READY | ATA_CONTROL:
645 if (ch->flags & ATA_QUEUED) {
646 ch->active = ATA_ACTIVE;
647 if (ata_service(ch) == ATA_OP_CONTINUES)
655 static int intr_count = 0;
657 if (intr_count++ < 10)
658 ata_printf(ch, -1, "unwanted interrupt #%d active=%02x s=%02x\n",
659 intr_count, ch->active, ch->status);
664 ch->active &= ATA_CONTROL;
665 if (ch->active & ATA_CONTROL)
673 ata_start(struct ata_channel *ch)
676 struct ad_request *ad_request;
679 struct atapi_request *atapi_request;
682 crit_enter(); /* interlock non-atomic channel lock */
683 if (!ATA_LOCK_CH(ch, ATA_ACTIVE)) {
689 /* find & call the responsible driver if anything on the ATA queue */
690 if (TAILQ_EMPTY(&ch->ata_queue)) {
691 if (ch->devices & (ATA_ATA_MASTER) && ch->device[MASTER].driver)
692 ad_start(&ch->device[MASTER]);
693 if (ch->devices & (ATA_ATA_SLAVE) && ch->device[SLAVE].driver)
694 ad_start(&ch->device[SLAVE]);
696 if ((ad_request = TAILQ_FIRST(&ch->ata_queue))) {
697 TAILQ_REMOVE(&ch->ata_queue, ad_request, chain);
698 ch->active = ATA_ACTIVE_ATA;
699 ch->running = ad_request;
702 * The donecount had better be 0 here because the channel may not
703 * have retained the setup for the request (if a retry).
705 KKASSERT(ad_request->donecount == 0);
706 if (ad_transfer(ad_request) == ATA_OP_CONTINUES) {
714 /* find & call the responsible driver if anything on the ATAPI queue */
715 if (TAILQ_EMPTY(&ch->atapi_queue)) {
716 if (ch->devices & (ATA_ATAPI_MASTER) && ch->device[MASTER].driver)
717 atapi_start(&ch->device[MASTER]);
718 if (ch->devices & (ATA_ATAPI_SLAVE) && ch->device[SLAVE].driver)
719 atapi_start(&ch->device[SLAVE]);
721 if ((atapi_request = TAILQ_FIRST(&ch->atapi_queue))) {
722 TAILQ_REMOVE(&ch->atapi_queue, atapi_request, chain);
723 ch->active = ATA_ACTIVE_ATAPI;
724 ch->running = atapi_request;
725 if (atapi_transfer(atapi_request) == ATA_OP_CONTINUES) {
736 ata_reset(struct ata_channel *ch)
738 u_int8_t lsb, msb, ostat0, ostat1;
739 u_int8_t stat0 = 0, stat1 = 0;
740 int mask = 0, timeout;
742 /* do we have any signs of ATA/ATAPI HW being present ? */
743 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
745 ostat0 = ATA_INB(ch->r_io, ATA_STATUS);
746 if ((ostat0 & 0xf8) != 0xf8 && ostat0 != 0xa5) {
750 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
752 ostat1 = ATA_INB(ch->r_io, ATA_STATUS);
753 if ((ostat1 & 0xf8) != 0xf8 && ostat1 != 0xa5) {
762 /* in some setups we dont want to test for a slave */
763 if (ch->flags & ATA_NO_SLAVE) {
769 ata_printf(ch, -1, "mask=%02x ostat0=%02x ostat2=%02x\n",
770 mask, ostat0, ostat1);
773 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
775 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_RESET);
777 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_IDS);
779 ATA_INB(ch->r_io, ATA_ERROR);
781 /* wait for BUSY to go inactive */
782 for (timeout = 0; timeout < 3100; timeout++) {
783 if (stat0 & ATA_S_BUSY) {
784 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
787 /* check for ATAPI signature while its still there */
788 lsb = ATA_INB(ch->r_io, ATA_CYL_LSB);
789 msb = ATA_INB(ch->r_io, ATA_CYL_MSB);
790 stat0 = ATA_INB(ch->r_io, ATA_STATUS);
791 if (!(stat0 & ATA_S_BUSY)) {
793 ata_printf(ch, ATA_MASTER, "ATAPI %02x %02x\n", lsb, msb);
794 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB)
795 ch->devices |= ATA_ATAPI_MASTER;
798 if (stat1 & ATA_S_BUSY) {
799 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
802 /* check for ATAPI signature while its still there */
803 lsb = ATA_INB(ch->r_io, ATA_CYL_LSB);
804 msb = ATA_INB(ch->r_io, ATA_CYL_MSB);
805 stat1 = ATA_INB(ch->r_io, ATA_STATUS);
806 if (!(stat1 & ATA_S_BUSY)) {
808 ata_printf(ch, ATA_SLAVE, "ATAPI %02x %02x\n", lsb, msb);
809 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB)
810 ch->devices |= ATA_ATAPI_SLAVE;
813 if (mask == 0x01) /* wait for master only */
814 if (!(stat0 & ATA_S_BUSY))
816 if (mask == 0x02) /* wait for slave only */
817 if (!(stat1 & ATA_S_BUSY))
819 if (mask == 0x03) /* wait for both master & slave */
820 if (!(stat0 & ATA_S_BUSY) && !(stat1 & ATA_S_BUSY))
825 * some devices release BUSY before they are ready to accept commands.
826 * We must wait at least 50ms before attempting to issue a command after
830 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
832 if (stat0 & ATA_S_BUSY)
834 if (stat1 & ATA_S_BUSY)
837 ata_printf(ch, -1, "mask=%02x stat0=%02x stat1=%02x\n",
842 if (mask & 0x01 && ostat0 != 0x00 && !(ch->devices & ATA_ATAPI_MASTER)) {
843 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
845 ATA_OUTB(ch->r_io, ATA_ERROR, 0x58);
846 ATA_OUTB(ch->r_io, ATA_CYL_LSB, 0xa5);
847 lsb = ATA_INB(ch->r_io, ATA_ERROR);
848 msb = ATA_INB(ch->r_io, ATA_CYL_LSB);
850 ata_printf(ch, ATA_MASTER, "ATA %02x %02x\n", lsb, msb);
851 if (lsb != 0x58 && msb == 0xa5)
852 ch->devices |= ATA_ATA_MASTER;
854 if (mask & 0x02 && ostat1 != 0x00 && !(ch->devices & ATA_ATAPI_SLAVE)) {
855 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
857 ATA_OUTB(ch->r_io, ATA_ERROR, 0x58);
858 ATA_OUTB(ch->r_io, ATA_CYL_LSB, 0xa5);
859 lsb = ATA_INB(ch->r_io, ATA_ERROR);
860 msb = ATA_INB(ch->r_io, ATA_CYL_LSB);
862 ata_printf(ch, ATA_SLAVE, "ATA %02x %02x\n", lsb, msb);
863 if (lsb != 0x58 && msb == 0xa5)
864 ch->devices |= ATA_ATA_SLAVE;
867 ata_printf(ch, -1, "devices=%02x\n", ch->devices);
871 ata_reinit(struct ata_channel *ch)
873 int devices, misdev, newdev;
875 if (!ch->r_io || !ch->r_altio || !ch->r_irq)
878 ATA_FORCELOCK_CH(ch, ATA_CONTROL);
880 devices = ch->devices;
881 ata_printf(ch, -1, "resetting devices .. ");
884 if ((misdev = devices & ~ch->devices)) {
888 if (misdev & ATA_ATA_MASTER && ch->device[MASTER].driver)
889 ad_detach(&ch->device[MASTER], 0);
890 if (misdev & ATA_ATA_SLAVE && ch->device[SLAVE].driver)
891 ad_detach(&ch->device[SLAVE], 0);
894 if (misdev & ATA_ATAPI_MASTER && ch->device[MASTER].driver)
895 atapi_detach(&ch->device[MASTER]);
896 if (misdev & ATA_ATAPI_SLAVE && ch->device[SLAVE].driver)
897 atapi_detach(&ch->device[SLAVE]);
899 if (misdev & ATA_ATA_MASTER || misdev & ATA_ATAPI_MASTER) {
900 if (ch->device[MASTER].param)
901 free(ch->device[MASTER].param, M_ATA);
902 ch->device[MASTER].param = NULL;
904 if (misdev & ATA_ATA_SLAVE || misdev & ATA_ATAPI_SLAVE) {
905 if (ch->device[SLAVE].param)
906 free(ch->device[SLAVE].param, M_ATA);
907 ch->device[SLAVE].param = NULL;
910 if ((newdev = ~devices & ch->devices)) {
911 if (newdev & ATA_ATA_MASTER)
912 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
913 ch->devices &= ~ATA_ATA_MASTER;
914 if (newdev & ATA_ATA_SLAVE)
915 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
916 ch->devices &= ~ATA_ATA_SLAVE;
917 if (newdev & ATA_ATAPI_MASTER)
918 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
919 ch->devices &= ~ATA_ATAPI_MASTER;
920 if (newdev & ATA_ATAPI_SLAVE)
921 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
922 ch->devices &= ~ATA_ATAPI_SLAVE;
924 newdev = ~devices & ch->devices;
925 if (!misdev && newdev)
928 if (newdev & ATA_ATA_MASTER && !ch->device[MASTER].driver)
929 ad_attach(&ch->device[MASTER], 1);
930 else if (ch->devices & ATA_ATA_MASTER && ch->device[MASTER].driver) {
931 ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY);
932 ad_reinit(&ch->device[MASTER]);
934 if (newdev & ATA_ATA_SLAVE && !ch->device[SLAVE].driver)
935 ad_attach(&ch->device[SLAVE], 1);
936 else if (ch->devices & (ATA_ATA_SLAVE) && ch->device[SLAVE].driver) {
937 ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY);
938 ad_reinit(&ch->device[SLAVE]);
942 if (newdev & ATA_ATAPI_MASTER && !ch->device[MASTER].driver)
943 atapi_attach(&ch->device[MASTER], 1);
944 else if (ch->devices & (ATA_ATAPI_MASTER) && ch->device[MASTER].driver) {
945 ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY);
946 atapi_reinit(&ch->device[MASTER]);
948 if (newdev & ATA_ATAPI_SLAVE && !ch->device[SLAVE].driver)
949 atapi_attach(&ch->device[SLAVE], 1);
950 else if (ch->devices & (ATA_ATAPI_SLAVE) && ch->device[SLAVE].driver) {
951 ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY);
952 atapi_reinit(&ch->device[SLAVE]);
956 if (ch->devices & (ATA_ATAPI_MASTER | ATA_ATAPI_SLAVE))
957 atapi_cam_reinit_bus(ch);
966 ata_service(struct ata_channel *ch)
968 /* do we have a SERVICE request from the drive ? */
969 if ((ch->status & (ATA_S_SERVICE|ATA_S_ERROR|ATA_S_DRQ)) == ATA_S_SERVICE) {
970 ATA_OUTB(ch->r_bmio, ATA_BMSTAT_PORT,
971 ata_dmastatus(ch) | ATA_BMSTAT_INTERRUPT);
973 if ((ATA_INB(ch->r_io, ATA_DRIVE) & ATA_SLAVE) == ATA_MASTER) {
974 if ((ch->devices & ATA_ATA_MASTER) && ch->device[MASTER].driver)
975 return ad_service((struct ad_softc *)
976 ch->device[MASTER].driver, 0);
979 if ((ch->devices & ATA_ATA_SLAVE) && ch->device[SLAVE].driver)
980 return ad_service((struct ad_softc *)
981 ch->device[SLAVE].driver, 0);
985 return ATA_OP_FINISHED;
989 ata_wait(struct ata_device *atadev, u_int8_t mask)
994 while (timeout < 5000000) { /* timeout 5 secs */
995 atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS);
997 /* if drive fails status, reselect the drive just to be sure */
998 if (atadev->channel->status == 0xff) {
999 ata_prtdev(atadev, "no status, reselecting device\n");
1000 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM|atadev->unit);
1002 atadev->channel->status = ATA_INB(atadev->channel->r_io,ATA_STATUS);
1003 if (atadev->channel->status == 0xff)
1008 if (!(atadev->channel->status & ATA_S_BUSY))
1011 if (timeout > 1000) {
1020 if (atadev->channel->status & ATA_S_ERROR)
1021 atadev->channel->error = ATA_INB(atadev->channel->r_io, ATA_ERROR);
1022 if (timeout >= 5000000)
1025 return (atadev->channel->status & ATA_S_ERROR);
1027 /* Wait 50 msec for bits wanted. */
1030 atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS);
1031 if ((atadev->channel->status & mask) == mask) {
1032 if (atadev->channel->status & ATA_S_ERROR)
1033 atadev->channel->error=ATA_INB(atadev->channel->r_io,ATA_ERROR);
1034 return (atadev->channel->status & ATA_S_ERROR);
1042 ata_command(struct ata_device *atadev, u_int8_t command,
1043 u_int64_t lba, u_int16_t count, u_int8_t feature, int flags)
1047 ata_prtdev(atadev, "ata_command: addr=%04lx, cmd=%02x, "
1048 "lba=%lld, count=%d, feature=%d, flags=%02x\n",
1049 rman_get_start(atadev->channel->r_io),
1050 command, lba, count, feature, flags);
1054 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1056 /* disable interrupt from device */
1057 if (atadev->channel->flags & ATA_QUEUED)
1058 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_4BIT);
1060 /* ready to issue command ? */
1061 if (ata_wait(atadev, 0) < 0) {
1062 ata_prtdev(atadev, "timeout sending command=%02x s=%02x e=%02x\n",
1063 command, atadev->channel->status, atadev->channel->error);
1067 /* only use 48bit addressing if needed because of the overhead */
1068 if ((lba >= 268435455 || count > 256) && atadev->param &&
1069 atadev->param->support.address48) {
1070 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, (feature>>8) & 0xff);
1071 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, feature);
1072 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, (count>>8) & 0xff);
1073 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, count & 0xff);
1074 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, (lba>>24) & 0xff);
1075 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, lba & 0xff);
1076 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>32) & 0xff);
1077 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>8) & 0xff);
1078 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>40) & 0xff);
1079 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>16) & 0xff);
1080 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_LBA | atadev->unit);
1082 /* translate command into 48bit version */
1085 command = ATA_C_READ48; break;
1086 case ATA_C_READ_MUL:
1087 command = ATA_C_READ_MUL48; break;
1088 case ATA_C_READ_DMA:
1089 command = ATA_C_READ_DMA48; break;
1090 case ATA_C_READ_DMA_QUEUED:
1091 command = ATA_C_READ_DMA_QUEUED48; break;
1093 command = ATA_C_WRITE48; break;
1094 case ATA_C_WRITE_MUL:
1095 command = ATA_C_WRITE_MUL48; break;
1096 case ATA_C_WRITE_DMA:
1097 command = ATA_C_WRITE_DMA48; break;
1098 case ATA_C_WRITE_DMA_QUEUED:
1099 command = ATA_C_WRITE_DMA_QUEUED48; break;
1100 case ATA_C_FLUSHCACHE:
1101 command = ATA_C_FLUSHCACHE48; break;
1103 ata_prtdev(atadev, "can't translate cmd to 48bit version\n");
1108 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, feature);
1109 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, count);
1110 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, lba & 0xff);
1111 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>8) & 0xff);
1112 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>16) & 0xff);
1113 if (atadev->flags & ATA_D_USE_CHS)
1114 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE,
1115 ATA_D_IBM | atadev->unit | ((lba>>24) & 0xf));
1117 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE,
1118 ATA_D_IBM | ATA_D_LBA | atadev->unit | ((lba>>24) &0xf));
1121 switch (flags & ATA_WAIT_MASK) {
1123 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1125 /* enable interrupt */
1126 if (atadev->channel->flags & ATA_QUEUED)
1127 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
1131 atadev->channel->active |= ATA_WAIT_INTR;
1132 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1134 /* enable interrupt */
1135 if (atadev->channel->flags & ATA_QUEUED)
1136 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
1138 if (tsleep((caddr_t)atadev->channel, 0, "atacmd", 10 * hz)) {
1139 ata_prtdev(atadev, "timeout waiting for interrupt\n");
1140 atadev->channel->active &= ~ATA_WAIT_INTR;
1145 case ATA_WAIT_READY:
1146 atadev->channel->active |= ATA_WAIT_READY;
1147 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1148 if (ata_wait(atadev, ATA_S_READY) < 0) {
1149 ata_prtdev(atadev, "timeout waiting for cmd=%02x s=%02x e=%02x\n",
1150 command, atadev->channel->status,atadev->channel->error);
1153 atadev->channel->active &= ~ATA_WAIT_READY;
1160 ata_enclosure_start(struct ata_device *atadev)
1162 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1164 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1166 ATA_INB(atadev->channel->r_io, ATA_CMD);
1168 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
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_INB(atadev->channel->r_io, ATA_COUNT);
1176 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1181 ata_enclosure_end(struct ata_device *atadev)
1183 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1188 ata_enclosure_chip_start(struct ata_device *atadev)
1190 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0b);
1191 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0a);
1193 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08);
1197 ata_enclosure_chip_end(struct ata_device *atadev)
1199 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08);
1201 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0a);
1203 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0b);
1208 ata_enclosure_chip_rdbit(struct ata_device *atadev)
1212 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0);
1214 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x02);
1216 val = ATA_INB(atadev->channel->r_io, ATA_SECTOR) & 0x01;
1222 ata_enclosure_chip_wrbit(struct ata_device *atadev, u_int8_t data)
1224 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08 | (data & 0x01));
1226 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08 | 0x02 | (data & 0x01));
1231 ata_enclosure_chip_rw(struct ata_device *atadev, int rw, u_int8_t val)
1236 for (i = 0; i < 8; i++)
1237 ata_enclosure_chip_wrbit(atadev, (val & (0x80 >> i)) ? 1 : 0);
1240 for (i = 0; i < 8; i++)
1241 val = (val << 1) | ata_enclosure_chip_rdbit(atadev);
1243 ata_enclosure_chip_wrbit(atadev, 0);
1248 ata_enclosure_sensor(struct ata_device *atadev,
1249 int rw, u_int8_t idx, u_int8_t data)
1251 ata_enclosure_start(atadev);
1252 ata_enclosure_chip_start(atadev);
1253 ata_enclosure_chip_rw(atadev, 1, 0x5a);
1254 ata_enclosure_chip_rw(atadev, 1, idx);
1256 ata_enclosure_chip_rw(atadev, 1, data);
1259 ata_enclosure_chip_end(atadev);
1260 ata_enclosure_chip_start(atadev);
1261 ata_enclosure_chip_rw(atadev, 1, 0x5b);
1262 data = ata_enclosure_chip_rw(atadev, 0, 0);
1264 ata_enclosure_chip_end(atadev);
1265 ata_enclosure_end(atadev);
1270 ata_enclosure_status(struct ata_device *atadev,
1271 int *fan, int *temp, int *v05, int *v12)
1273 u_int8_t id1, id2, cnt, div;
1276 if (atadev->flags & ATA_D_ENC_PRESENT) {
1277 ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL);
1278 ata_enclosure_sensor(atadev, 1, 0x4e, 0);
1279 id1 = ata_enclosure_sensor(atadev, 0, 0x4f, 0);
1280 ata_enclosure_sensor(atadev, 1, 0x4e, 0x80);
1281 id2 = ata_enclosure_sensor(atadev, 0, 0x4f, 0);
1282 if (id1 == 0xa3 && id2 == 0x5c) {
1283 div = 1 << (((ata_enclosure_sensor(atadev, 0, 0x5d, 0)&0x20)>>3)+
1284 ((ata_enclosure_sensor(atadev, 0, 0x47, 0)&0x30)>>4)+1);
1285 cnt = ata_enclosure_sensor(atadev, 0, 0x28, 0);
1289 *fan = 1350000 / cnt / div;
1290 ata_enclosure_sensor(atadev, 1, 0x4e, 0x01);
1291 *temp = (ata_enclosure_sensor(atadev, 0, 0x50, 0) * 10) +
1292 (ata_enclosure_sensor(atadev, 0, 0x50, 0) & 0x80 ? 5 : 0);
1293 *v05 = ata_enclosure_sensor(atadev, 0, 0x23, 0) * 27;
1294 *v12 = ata_enclosure_sensor(atadev, 0, 0x24, 0) * 61;
1297 ATA_UNLOCK_CH(atadev->channel);
1303 ata_enclosure_print(struct ata_device *atadev)
1306 int fan, temp, v05, v12;
1308 ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL);
1309 ata_enclosure_start(atadev);
1310 id = ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1312 st = ATA_INB(atadev->channel->r_io, ATA_COUNT);
1314 ata_enclosure_end(atadev);
1315 ATA_UNLOCK_CH(atadev->channel);
1317 switch (id & 0x93) {
1319 ata_prtdev(atadev, "Universal enclosure");
1322 ata_prtdev(atadev, "FastSwap enclosure");
1326 ata_prtdev(atadev, "SuperSwap enclosure");
1329 atadev->flags &= ~ATA_D_ENC_PRESENT;
1332 atadev->flags |= ATA_D_ENC_PRESENT;
1334 if (ata_enclosure_status(atadev, &fan, &temp, &v05, &v12))
1335 printf(" detected\n");
1337 printf(" [FAN:%drpm TEMP:%d.%01dC %d.%03dV %d.%03dV]\n",
1338 fan, temp/10, temp%10, v05/1000, v05%1000, v12/1000, v12%1000);
1342 ata_enclosure_leds(struct ata_device *atadev, u_int8_t color)
1344 if (atadev->flags & ATA_D_ENC_PRESENT) {
1347 ata_enclosure_start(atadev);
1348 reg = ATA_INB(atadev->channel->r_io, ATA_COUNT);
1350 ATA_OUTB(atadev->channel->r_io, ATA_COUNT,
1351 (color & ATA_LED_MASK) | (reg & ~ATA_LED_MASK));
1353 ata_enclosure_end(atadev);
1358 ata_change_mode(struct ata_device *atadev, int mode)
1360 int umode, wmode, pmode;
1362 umode = ata_umode(atadev->param);
1363 wmode = ata_wmode(atadev->param);
1364 pmode = ata_pmode(atadev->param);
1366 switch (mode & ATA_DMA_MASK) {
1368 if ((mode & ATA_MODE_MASK) < umode)
1369 umode = mode & ATA_MODE_MASK;
1372 if ((mode & ATA_MODE_MASK) < wmode)
1373 wmode = mode & ATA_MODE_MASK;
1377 if (((mode & ATA_MODE_MASK) - ATA_PIO0) < pmode)
1378 pmode = (mode & ATA_MODE_MASK) - ATA_PIO0;
1383 crit_enter(); /* interlock non-atomic channel lock */
1384 ATA_SLEEPLOCK_CH(atadev->channel, ATA_ACTIVE);
1385 ata_dmainit(atadev, pmode, wmode, umode);
1386 ATA_UNLOCK_CH(atadev->channel);
1388 ata_start(atadev->channel); /* XXX SOS */
1392 ata_printf(struct ata_channel *ch, int device, const char * fmt, ...)
1398 ret = printf("ata%d: ", device_get_unit(ch->dev));
1400 if (ch->device[ATA_DEV(device)].name)
1401 ret = printf("%s: ", ch->device[ATA_DEV(device)].name);
1403 ret = printf("ata%d-%s: ", device_get_unit(ch->dev),
1404 (device == ATA_MASTER) ? "master" : "slave");
1406 __va_start(ap, fmt);
1407 ret += vprintf(fmt, ap);
1413 ata_prtdev(struct ata_device *atadev, const char * fmt, ...)
1419 ret = printf("%s: ", atadev->name);
1421 ret = printf("ata%d-%s: ", device_get_unit(atadev->channel->dev),
1422 (atadev->unit == ATA_MASTER) ? "master" : "slave");
1423 __va_start(ap, fmt);
1424 ret += vprintf(fmt, ap);
1430 ata_set_name(struct ata_device *atadev, char *name, int lun)
1432 atadev->name = malloc(strlen(name) + 4, M_ATA, M_INTWAIT);
1433 sprintf(atadev->name, "%s%d", name, lun);
1437 ata_free_name(struct ata_device *atadev)
1440 free(atadev->name, M_ATA);
1441 atadev->name = NULL;
1445 ata_get_lun(u_int32_t *map)
1447 int lun = ffs(~*map) - 1;
1454 ata_test_lun(u_int32_t *map, int lun)
1456 return (*map & (1 << lun));
1460 ata_free_lun(u_int32_t *map, int lun)
1462 *map &= ~(1 << lun);
1466 ata_mode2str(int mode)
1469 case ATA_PIO: return "BIOSPIO";
1470 case ATA_PIO0: return "PIO0";
1471 case ATA_PIO1: return "PIO1";
1472 case ATA_PIO2: return "PIO2";
1473 case ATA_PIO3: return "PIO3";
1474 case ATA_PIO4: return "PIO4";
1475 case ATA_DMA: return "BIOSDMA";
1476 case ATA_WDMA2: return "WDMA2";
1477 case ATA_UDMA2: return "UDMA33";
1478 case ATA_UDMA4: return "UDMA66";
1479 case ATA_UDMA5: return "UDMA100";
1480 case ATA_UDMA6: return "UDMA133";
1481 default: return "???";
1486 ata_pmode(struct ata_params *ap)
1488 if (ap->atavalid & ATA_FLAG_64_70) {
1489 if (ap->apiomodes & 2)
1491 if (ap->apiomodes & 1)
1494 if (ap->retired_piomode == 2)
1496 if (ap->retired_piomode == 1)
1498 if (ap->retired_piomode == 0)
1504 ata_wmode(struct ata_params *ap)
1506 if (ap->mwdmamodes & 0x04)
1508 if (ap->mwdmamodes & 0x02)
1510 if (ap->mwdmamodes & 0x01)
1516 ata_umode(struct ata_params *ap)
1518 if (ap->atavalid & ATA_FLAG_88) {
1519 if (ap->udmamodes & 0x40)
1521 if (ap->udmamodes & 0x20)
1523 if (ap->udmamodes & 0x10)
1525 if (ap->udmamodes & 0x08)
1527 if (ap->udmamodes & 0x04)
1529 if (ap->udmamodes & 0x02)
1531 if (ap->udmamodes & 0x01)
1538 bswap(int8_t *buf, int len)
1540 u_int16_t *ptr = (u_int16_t*)(buf + len);
1542 while (--ptr >= (u_int16_t*)buf)
1547 btrim(int8_t *buf, int len)
1551 for (ptr = buf; ptr < buf+len; ++ptr)
1554 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
1559 bpack(int8_t *src, int8_t *dst, int len)
1563 for (i = j = blank = 0 ; i < len; i++) {
1564 if (blank && src[i] == ' ') continue;
1565 if (blank && src[i] != ' ') {
1570 if (src[i] == ' ') {
1584 /* register controlling device */
1585 cdevsw_add(&ata_cdevsw, 0, 0);
1586 make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "ata");
1588 /* register boot attach to be run when interrupts are enabled */
1589 ata_delayed_attach = malloc(sizeof(struct intr_config_hook),
1590 M_TEMP, M_WAITOK | M_ZERO);
1592 ata_delayed_attach->ich_func = (void*)ata_boot_attach;
1593 ata_delayed_attach->ich_desc = "ata";
1594 if (config_intrhook_establish(ata_delayed_attach) != 0) {
1595 printf("ata: config_intrhook_establish failed\n");
1596 free(ata_delayed_attach, M_TEMP);
1599 SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL)