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.5 2003/07/19 21:14:18 dillon Exp $
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>
59 #include <dev/ata/ata-all.h>
60 #include <dev/ata/ata-disk.h>
61 #include <dev/ata/ata-raid.h>
62 #include <dev/ata/atapi-all.h>
64 /* device structures */
65 static d_ioctl_t ataioctl;
66 static struct cdevsw ata_cdevsw = {
68 /* close */ nullclose,
74 /* strategy */ nostrategy,
83 static void ata_boot_attach(void);
84 static void ata_intr(void *);
85 static int ata_getparam(struct ata_device *, u_int8_t);
86 static int ata_service(struct ata_channel *);
87 static void bswap(int8_t *, int);
88 static void btrim(int8_t *, int);
89 static void bpack(int8_t *, int8_t *, int);
90 static void ata_change_mode(struct ata_device *, int);
91 static u_int8_t ata_enclosure_sensor(struct ata_device *, int, u_int8_t, u_int8_t);
92 static int ata_enclosure_status(struct ata_device *, int *, int *, int *, int *);
95 SYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters");
98 devclass_t ata_devclass;
101 static struct intr_config_hook *ata_delayed_attach = NULL;
102 static MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer");
105 #define DEV_ATAPIALL NATAPICD > 0 || NATAPIFD > 0 || \
106 NATAPIST > 0 || NATAPICAM > 0
109 ata_probe(device_t dev)
111 struct ata_channel *ch;
114 if (!dev || !(ch = device_get_softc(dev)))
117 if (ch->r_io || ch->r_altio || ch->r_irq)
120 /* initialize the softc basics */
121 ch->active = ATA_IDLE;
124 rid = ATA_IOADDR_RID;
125 ch->r_io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
126 ATA_IOSIZE, RF_ACTIVE);
130 rid = ATA_ALTADDR_RID;
131 ch->r_altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
132 ATA_ALTIOSIZE, RF_ACTIVE);
136 rid = ATA_BMADDR_RID;
137 ch->r_bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
138 ATA_BMIOSIZE, RF_ACTIVE);
140 ata_printf(ch, -1, "iobase=0x%04x altiobase=0x%04x bmaddr=0x%04x\n",
141 (int)rman_get_start(ch->r_io),
142 (int)rman_get_start(ch->r_altio),
143 (ch->r_bmio) ? (int)rman_get_start(ch->r_bmio) : 0);
147 ch->device[MASTER].channel = ch;
148 ch->device[MASTER].unit = ATA_MASTER;
149 ch->device[MASTER].mode = ATA_PIO;
150 ch->device[SLAVE].channel = ch;
151 ch->device[SLAVE].unit = ATA_SLAVE;
152 ch->device[SLAVE].mode = ATA_PIO;
153 TAILQ_INIT(&ch->ata_queue);
154 TAILQ_INIT(&ch->atapi_queue);
159 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ch->r_io);
161 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ch->r_altio);
163 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, ch->r_bmio);
165 ata_printf(ch, -1, "probe allocation failed\n");
170 ata_attach(device_t dev)
172 struct ata_channel *ch;
175 if (!dev || !(ch = device_get_softc(dev)))
179 ch->r_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
180 RF_SHAREABLE | RF_ACTIVE);
182 ata_printf(ch, -1, "unable to allocate interrupt\n");
185 if ((error = bus_setup_intr(dev, ch->r_irq, INTR_TYPE_BIO,
186 ata_intr, ch, &ch->ih))) {
187 ata_printf(ch, -1, "unable to setup interrupt\n");
192 * do not attach devices if we are in early boot, this is done later
193 * when interrupts are enabled by a hook into the boot process.
194 * otherwise attach what the probe has found in ch->devices.
196 if (!ata_delayed_attach) {
199 if (ch->devices & ATA_ATA_SLAVE)
200 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
201 ch->devices &= ~ATA_ATA_SLAVE;
202 if (ch->devices & ATA_ATAPI_SLAVE)
203 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
204 ch->devices &= ~ATA_ATAPI_SLAVE;
205 if (ch->devices & ATA_ATA_MASTER)
206 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
207 ch->devices &= ~ATA_ATA_MASTER;
208 if (ch->devices & ATA_ATAPI_MASTER)
209 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
210 ch->devices &= ~ATA_ATAPI_MASTER;
212 if (ch->devices & ATA_ATA_MASTER)
213 ad_attach(&ch->device[MASTER]);
214 if (ch->devices & ATA_ATA_SLAVE)
215 ad_attach(&ch->device[SLAVE]);
218 if (ch->devices & ATA_ATAPI_MASTER)
219 atapi_attach(&ch->device[MASTER]);
220 if (ch->devices & ATA_ATAPI_SLAVE)
221 atapi_attach(&ch->device[SLAVE]);
224 atapi_cam_attach_bus(ch);
232 ata_detach(device_t dev)
234 struct ata_channel *ch;
237 if (!dev || !(ch = device_get_softc(dev)) ||
238 !ch->r_io || !ch->r_altio || !ch->r_irq)
241 /* make sure channel is not busy */
242 ATA_SLEEPLOCK_CH(ch, ATA_CONTROL);
246 if (ch->devices & ATA_ATA_MASTER && ch->device[MASTER].driver)
247 ad_detach(&ch->device[MASTER], 1);
248 if (ch->devices & ATA_ATA_SLAVE && ch->device[SLAVE].driver)
249 ad_detach(&ch->device[SLAVE], 1);
252 if (ch->devices & ATA_ATAPI_MASTER && ch->device[MASTER].driver)
253 atapi_detach(&ch->device[MASTER]);
254 if (ch->devices & ATA_ATAPI_SLAVE && ch->device[SLAVE].driver)
255 atapi_detach(&ch->device[SLAVE]);
258 atapi_cam_detach_bus(ch);
262 if (ch->device[MASTER].param) {
263 free(ch->device[MASTER].param, M_ATA);
264 ch->device[MASTER].param = NULL;
266 if (ch->device[SLAVE].param) {
267 free(ch->device[SLAVE].param, M_ATA);
268 ch->device[SLAVE].param = NULL;
270 ch->device[MASTER].driver = NULL;
271 ch->device[SLAVE].driver = NULL;
272 ch->device[MASTER].mode = ATA_PIO;
273 ch->device[SLAVE].mode = ATA_PIO;
276 bus_teardown_intr(dev, ch->r_irq, ch->ih);
277 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
279 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, ch->r_bmio);
280 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ch->r_altio);
281 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ch->r_io);
291 ata_resume(device_t dev)
293 return ata_reinit(device_get_softc(dev));
297 ataioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, struct thread *td)
299 struct ata_cmd *iocmd = (struct ata_cmd *)addr;
300 struct ata_channel *ch;
301 device_t device = devclass_get_device(ata_devclass, iocmd->channel);
307 if (iocmd->channel < -1 || iocmd->device < -1 || iocmd->device > SLAVE)
310 switch (iocmd->cmd) {
312 /* should enable channel HW on controller that can SOS XXX */
313 error = ata_probe(device);
315 error = ata_attach(device);
319 error = ata_detach(device);
320 /* should disable channel HW on controller that can SOS XXX */
324 if (!device || !(ch = device_get_softc(device)))
326 ATA_SLEEPLOCK_CH(ch, ATA_ACTIVE);
327 if ((error = ata_reinit(ch)))
332 if (!device || !(ch = device_get_softc(device)))
335 if ((iocmd->device == MASTER || iocmd->device == -1) &&
336 ch->device[MASTER].driver)
337 iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode;
339 iocmd->u.mode.mode[MASTER] = -1;
341 if ((iocmd->device == SLAVE || iocmd->device == -1) &&
342 ch->device[SLAVE].param)
343 iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode;
345 iocmd->u.mode.mode[SLAVE] = -1;
349 if (!device || !(ch = device_get_softc(device)))
352 if ((iocmd->device == MASTER || iocmd->device == -1) &&
353 iocmd->u.mode.mode[MASTER] >= 0 && ch->device[MASTER].param) {
354 ata_change_mode(&ch->device[MASTER],iocmd->u.mode.mode[MASTER]);
355 iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode;
358 iocmd->u.mode.mode[MASTER] = -1;
360 if ((iocmd->device == SLAVE || iocmd->device == -1) &&
361 iocmd->u.mode.mode[SLAVE] >= 0 && ch->device[SLAVE].param) {
362 ata_change_mode(&ch->device[SLAVE], iocmd->u.mode.mode[SLAVE]);
363 iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode;
366 iocmd->u.mode.mode[SLAVE] = -1;
370 if (!device || !(ch = device_get_softc(device)))
373 iocmd->u.param.type[MASTER] =
374 ch->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER);
375 iocmd->u.param.type[SLAVE] =
376 ch->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE);
378 if (ch->device[MASTER].name)
379 strcpy(iocmd->u.param.name[MASTER], ch->device[MASTER].name);
380 if (ch->device[SLAVE].name)
381 strcpy(iocmd->u.param.name[SLAVE], ch->device[SLAVE].name);
383 if (ch->device[MASTER].param)
384 bcopy(ch->device[MASTER].param, &iocmd->u.param.params[MASTER],
385 sizeof(struct ata_params));
386 if (ch->device[SLAVE].param)
387 bcopy(ch->device[SLAVE].param, &iocmd->u.param.params[SLAVE],
388 sizeof(struct ata_params));
392 struct ata_device *atadev;
394 if (!device || !(ch = device_get_softc(device)))
397 if (iocmd->device == SLAVE)
398 atadev = &ch->device[SLAVE];
400 atadev = &ch->device[MASTER];
402 return ata_enclosure_status(atadev,
403 &iocmd->u.enclosure.fan,
404 &iocmd->u.enclosure.temp,
405 &iocmd->u.enclosure.v05,
406 &iocmd->u.enclosure.v12);
411 return ata_raid_rebuild(iocmd->channel);
414 return ata_raid_create(&iocmd->u.raid_setup);
417 return ata_raid_delete(iocmd->channel);
420 return ata_raid_status(iocmd->channel, &iocmd->u.raid_status);
424 struct ata_device *atadev;
427 if (!device || !(ch = device_get_softc(device)))
430 if (!(atadev = &ch->device[iocmd->device]) ||
431 !(ch->devices & (iocmd->device == MASTER ?
432 ATA_ATAPI_MASTER : ATA_ATAPI_SLAVE)))
435 if (!(buf = malloc(iocmd->u.atapi.count, M_ATA, M_NOWAIT)))
438 if (iocmd->u.atapi.flags & ATAPI_CMD_WRITE) {
439 error = copyin(iocmd->u.atapi.data, buf, iocmd->u.atapi.count);
443 error = atapi_queue_cmd(atadev, iocmd->u.atapi.ccb,
444 buf, iocmd->u.atapi.count,
445 (iocmd->u.atapi.flags == ATAPI_CMD_READ ?
446 ATPR_F_READ : 0) | ATPR_F_QUIET,
447 iocmd->u.atapi.timeout, NULL, NULL);
449 iocmd->u.atapi.error = error;
450 bcopy(&atadev->result, iocmd->u.atapi.sense_data,
451 sizeof(struct atapi_reqsense));
454 else if (iocmd->u.atapi.flags & ATAPI_CMD_READ)
455 error = copyout(buf, iocmd->u.atapi.data, iocmd->u.atapi.count);
468 ata_getparam(struct ata_device *atadev, u_int8_t command)
470 struct ata_params *ata_parm;
473 if (!(ata_parm = malloc(sizeof(struct ata_params), M_ATA, M_NOWAIT))) {
474 ata_prtdev(atadev, "malloc for identify data failed\n");
478 /* apparently some devices needs this repeated */
480 if (ata_command(atadev, command, 0, 0, 0, ATA_IMMEDIATE)) {
481 ata_prtdev(atadev, "%s identify failed\n",
482 command == ATA_C_ATAPI_IDENTIFY ? "ATAPI" : "ATA");
483 free(ata_parm, M_ATA);
487 ata_prtdev(atadev, "%s identify retries exceeded\n",
488 command == ATA_C_ATAPI_IDENTIFY ? "ATAPI" : "ATA");
489 free(ata_parm, M_ATA);
492 } while (ata_wait(atadev, ((command == ATA_C_ATAPI_IDENTIFY) ?
493 ATA_S_DRQ : (ATA_S_READY|ATA_S_DSC|ATA_S_DRQ))));
494 ATA_INSW(atadev->channel->r_io, ATA_DATA, (int16_t *)ata_parm,
495 sizeof(struct ata_params)/sizeof(int16_t));
497 if (command == ATA_C_ATA_IDENTIFY ||
498 !((ata_parm->model[0] == 'N' && ata_parm->model[1] == 'E') ||
499 (ata_parm->model[0] == 'F' && ata_parm->model[1] == 'X') ||
500 (ata_parm->model[0] == 'P' && ata_parm->model[1] == 'i')))
501 bswap(ata_parm->model, sizeof(ata_parm->model));
502 btrim(ata_parm->model, sizeof(ata_parm->model));
503 bpack(ata_parm->model, ata_parm->model, sizeof(ata_parm->model));
504 bswap(ata_parm->revision, sizeof(ata_parm->revision));
505 btrim(ata_parm->revision, sizeof(ata_parm->revision));
506 bpack(ata_parm->revision, ata_parm->revision, sizeof(ata_parm->revision));
507 bswap(ata_parm->serial, sizeof(ata_parm->serial));
508 btrim(ata_parm->serial, sizeof(ata_parm->serial));
509 bpack(ata_parm->serial, ata_parm->serial, sizeof(ata_parm->serial));
510 atadev->param = ata_parm;
515 ata_boot_attach(void)
517 struct ata_channel *ch;
520 if (ata_delayed_attach) {
521 config_intrhook_disestablish(ata_delayed_attach);
522 free(ata_delayed_attach, M_TEMP);
523 ata_delayed_attach = NULL;
528 * run through all ata devices and look for real ATA & ATAPI devices
529 * using the hints we found in the early probe, this avoids some of
530 * the delays probing of non-exsistent devices can cause.
532 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
533 if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
535 if (ch->devices & ATA_ATA_SLAVE)
536 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
537 ch->devices &= ~ATA_ATA_SLAVE;
538 if (ch->devices & ATA_ATAPI_SLAVE)
539 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
540 ch->devices &= ~ATA_ATAPI_SLAVE;
541 if (ch->devices & ATA_ATA_MASTER)
542 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
543 ch->devices &= ~ATA_ATA_MASTER;
544 if (ch->devices & ATA_ATAPI_MASTER)
545 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
546 ch->devices &= ~ATA_ATAPI_MASTER;
550 /* now we know whats there, do the real attach, first the ATA disks */
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_MASTER)
555 ad_attach(&ch->device[MASTER]);
556 if (ch->devices & ATA_ATA_SLAVE)
557 ad_attach(&ch->device[SLAVE]);
562 /* then the atapi devices */
563 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
564 if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
566 if (ch->devices & ATA_ATAPI_MASTER)
567 atapi_attach(&ch->device[MASTER]);
568 if (ch->devices & ATA_ATAPI_SLAVE)
569 atapi_attach(&ch->device[SLAVE]);
571 atapi_cam_attach_bus(ch);
581 struct ata_channel *ch = (struct ata_channel *)data;
583 * on PCI systems we might share an interrupt line with another
584 * device or our twin ATA channel, so call ch->intr_func to figure
585 * out if it is really an interrupt we should process here
587 if (ch->intr_func && ch->intr_func(ch))
590 /* if drive is busy it didn't interrupt */
591 if (ATA_INB(ch->r_altio, ATA_ALTSTAT) & ATA_S_BUSY) {
593 if (!(ATA_INB(ch->r_altio, ATA_ALTSTAT) & ATA_S_DRQ))
597 /* clear interrupt and get status */
598 ch->status = ATA_INB(ch->r_io, ATA_STATUS);
600 if (ch->status & ATA_S_ERROR)
601 ch->error = ATA_INB(ch->r_io, ATA_ERROR);
603 /* find & call the responsible driver to process this interrupt */
604 switch (ch->active) {
607 if (!ch->running || ad_interrupt(ch->running) == ATA_OP_CONTINUES)
612 case ATA_ACTIVE_ATAPI:
613 if (!ch->running || atapi_interrupt(ch->running) == ATA_OP_CONTINUES)
618 case ATA_WAIT_INTR | ATA_CONTROL:
623 case ATA_WAIT_READY | ATA_CONTROL:
627 if (ch->flags & ATA_QUEUED) {
628 ch->active = ATA_ACTIVE;
629 if (ata_service(ch) == ATA_OP_CONTINUES)
637 static int intr_count = 0;
639 if (intr_count++ < 10)
640 ata_printf(ch, -1, "unwanted interrupt #%d active=%02x s=%02x\n",
641 intr_count, ch->active, ch->status);
646 ch->active &= ATA_CONTROL;
647 if (ch->active & ATA_CONTROL)
655 ata_start(struct ata_channel *ch)
658 struct ad_request *ad_request;
661 struct atapi_request *atapi_request;
665 if (!ATA_LOCK_CH(ch, ATA_ACTIVE))
670 /* find & call the responsible driver if anything on the ATA queue */
671 if (TAILQ_EMPTY(&ch->ata_queue)) {
672 if (ch->devices & (ATA_ATA_MASTER) && ch->device[MASTER].driver)
673 ad_start(&ch->device[MASTER]);
674 if (ch->devices & (ATA_ATA_SLAVE) && ch->device[SLAVE].driver)
675 ad_start(&ch->device[SLAVE]);
677 if ((ad_request = TAILQ_FIRST(&ch->ata_queue))) {
678 TAILQ_REMOVE(&ch->ata_queue, ad_request, chain);
679 ch->active = ATA_ACTIVE_ATA;
680 ch->running = ad_request;
681 if (ad_transfer(ad_request) == ATA_OP_CONTINUES) {
689 /* find & call the responsible driver if anything on the ATAPI queue */
690 if (TAILQ_EMPTY(&ch->atapi_queue)) {
691 if (ch->devices & (ATA_ATAPI_MASTER) && ch->device[MASTER].driver)
692 atapi_start(&ch->device[MASTER]);
693 if (ch->devices & (ATA_ATAPI_SLAVE) && ch->device[SLAVE].driver)
694 atapi_start(&ch->device[SLAVE]);
696 if ((atapi_request = TAILQ_FIRST(&ch->atapi_queue))) {
697 TAILQ_REMOVE(&ch->atapi_queue, atapi_request, chain);
698 ch->active = ATA_ACTIVE_ATAPI;
699 ch->running = atapi_request;
700 if (atapi_transfer(atapi_request) == ATA_OP_CONTINUES) {
711 ata_reset(struct ata_channel *ch)
713 u_int8_t lsb, msb, ostat0, ostat1;
714 u_int8_t stat0 = 0, stat1 = 0;
715 int mask = 0, timeout;
717 /* do we have any signs of ATA/ATAPI HW being present ? */
718 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
720 ostat0 = ATA_INB(ch->r_io, ATA_STATUS);
721 if ((ostat0 & 0xf8) != 0xf8 && ostat0 != 0xa5) {
725 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
727 ostat1 = ATA_INB(ch->r_io, ATA_STATUS);
728 if ((ostat1 & 0xf8) != 0xf8 && ostat1 != 0xa5) {
737 /* in some setups we dont want to test for a slave */
738 if (ch->flags & ATA_NO_SLAVE) {
744 ata_printf(ch, -1, "mask=%02x ostat0=%02x ostat2=%02x\n",
745 mask, ostat0, ostat1);
748 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
750 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_RESET);
752 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_IDS);
754 ATA_INB(ch->r_io, ATA_ERROR);
756 /* wait for BUSY to go inactive */
757 for (timeout = 0; timeout < 310000; timeout++) {
758 if (stat0 & ATA_S_BUSY) {
759 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
762 /* check for ATAPI signature while its still there */
763 lsb = ATA_INB(ch->r_io, ATA_CYL_LSB);
764 msb = ATA_INB(ch->r_io, ATA_CYL_MSB);
765 stat0 = ATA_INB(ch->r_io, ATA_STATUS);
766 if (!(stat0 & ATA_S_BUSY)) {
768 ata_printf(ch, ATA_MASTER, "ATAPI %02x %02x\n", lsb, msb);
769 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB)
770 ch->devices |= ATA_ATAPI_MASTER;
773 if (stat1 & ATA_S_BUSY) {
774 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
777 /* check for ATAPI signature while its still there */
778 lsb = ATA_INB(ch->r_io, ATA_CYL_LSB);
779 msb = ATA_INB(ch->r_io, ATA_CYL_MSB);
780 stat1 = ATA_INB(ch->r_io, ATA_STATUS);
781 if (!(stat1 & ATA_S_BUSY)) {
783 ata_printf(ch, ATA_SLAVE, "ATAPI %02x %02x\n", lsb, msb);
784 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB)
785 ch->devices |= ATA_ATAPI_SLAVE;
788 if (mask == 0x01) /* wait for master only */
789 if (!(stat0 & ATA_S_BUSY))
791 if (mask == 0x02) /* wait for slave only */
792 if (!(stat1 & ATA_S_BUSY))
794 if (mask == 0x03) /* wait for both master & slave */
795 if (!(stat0 & ATA_S_BUSY) && !(stat1 & ATA_S_BUSY))
800 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
802 if (stat0 & ATA_S_BUSY)
804 if (stat1 & ATA_S_BUSY)
807 ata_printf(ch, -1, "mask=%02x stat0=%02x stat1=%02x\n",
812 if (mask & 0x01 && ostat0 != 0x00 && !(ch->devices & ATA_ATAPI_MASTER)) {
813 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
815 ATA_OUTB(ch->r_io, ATA_ERROR, 0x58);
816 ATA_OUTB(ch->r_io, ATA_CYL_LSB, 0xa5);
817 lsb = ATA_INB(ch->r_io, ATA_ERROR);
818 msb = ATA_INB(ch->r_io, ATA_CYL_LSB);
820 ata_printf(ch, ATA_MASTER, "ATA %02x %02x\n", lsb, msb);
821 if (lsb != 0x58 && msb == 0xa5)
822 ch->devices |= ATA_ATA_MASTER;
824 if (mask & 0x02 && ostat1 != 0x00 && !(ch->devices & ATA_ATAPI_SLAVE)) {
825 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
827 ATA_OUTB(ch->r_io, ATA_ERROR, 0x58);
828 ATA_OUTB(ch->r_io, ATA_CYL_LSB, 0xa5);
829 lsb = ATA_INB(ch->r_io, ATA_ERROR);
830 msb = ATA_INB(ch->r_io, ATA_CYL_LSB);
832 ata_printf(ch, ATA_SLAVE, "ATA %02x %02x\n", lsb, msb);
833 if (lsb != 0x58 && msb == 0xa5)
834 ch->devices |= ATA_ATA_SLAVE;
837 ata_printf(ch, -1, "devices=%02x\n", ch->devices);
841 ata_reinit(struct ata_channel *ch)
843 int devices, misdev, newdev;
845 if (!ch->r_io || !ch->r_altio || !ch->r_irq)
848 ATA_FORCELOCK_CH(ch, ATA_CONTROL);
850 devices = ch->devices;
851 ata_printf(ch, -1, "resetting devices .. ");
854 if ((misdev = devices & ~ch->devices)) {
858 if (misdev & ATA_ATA_MASTER && ch->device[MASTER].driver)
859 ad_detach(&ch->device[MASTER], 0);
860 if (misdev & ATA_ATA_SLAVE && ch->device[SLAVE].driver)
861 ad_detach(&ch->device[SLAVE], 0);
864 if (misdev & ATA_ATAPI_MASTER && ch->device[MASTER].driver)
865 atapi_detach(&ch->device[MASTER]);
866 if (misdev & ATA_ATAPI_SLAVE && ch->device[SLAVE].driver)
867 atapi_detach(&ch->device[SLAVE]);
869 if (misdev & ATA_ATA_MASTER || misdev & ATA_ATAPI_MASTER) {
870 if (ch->device[MASTER].param)
871 free(ch->device[MASTER].param, M_ATA);
872 ch->device[MASTER].param = NULL;
874 if (misdev & ATA_ATA_SLAVE || misdev & ATA_ATAPI_SLAVE) {
875 if (ch->device[SLAVE].param)
876 free(ch->device[SLAVE].param, M_ATA);
877 ch->device[SLAVE].param = NULL;
880 if ((newdev = ~devices & ch->devices)) {
881 if (newdev & ATA_ATA_MASTER)
882 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
883 ch->devices &= ~ATA_ATA_MASTER;
884 if (newdev & ATA_ATA_SLAVE)
885 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
886 ch->devices &= ~ATA_ATA_SLAVE;
887 if (newdev & ATA_ATAPI_MASTER)
888 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
889 ch->devices &= ~ATA_ATAPI_MASTER;
890 if (newdev & ATA_ATAPI_SLAVE)
891 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
892 ch->devices &= ~ATA_ATAPI_SLAVE;
894 newdev = ~devices & ch->devices;
895 if (!misdev && newdev)
898 if (newdev & ATA_ATA_MASTER && !ch->device[MASTER].driver)
899 ad_attach(&ch->device[MASTER]);
900 else if (ch->devices & ATA_ATA_MASTER && ch->device[MASTER].driver) {
901 ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY);
902 ad_reinit(&ch->device[MASTER]);
904 if (newdev & ATA_ATA_SLAVE && !ch->device[SLAVE].driver)
905 ad_attach(&ch->device[SLAVE]);
906 else if (ch->devices & (ATA_ATA_SLAVE) && ch->device[SLAVE].driver) {
907 ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY);
908 ad_reinit(&ch->device[SLAVE]);
912 if (newdev & ATA_ATAPI_MASTER && !ch->device[MASTER].driver)
913 atapi_attach(&ch->device[MASTER]);
914 else if (ch->devices & (ATA_ATAPI_MASTER) && ch->device[MASTER].driver) {
915 ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY);
916 atapi_reinit(&ch->device[MASTER]);
918 if (newdev & ATA_ATAPI_SLAVE && !ch->device[SLAVE].driver)
919 atapi_attach(&ch->device[SLAVE]);
920 else if (ch->devices & (ATA_ATAPI_SLAVE) && ch->device[SLAVE].driver) {
921 ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY);
922 atapi_reinit(&ch->device[SLAVE]);
926 if (ch->devices & (ATA_ATAPI_MASTER | ATA_ATAPI_SLAVE))
927 atapi_cam_reinit_bus(ch);
936 ata_service(struct ata_channel *ch)
938 /* do we have a SERVICE request from the drive ? */
939 if ((ch->status & (ATA_S_SERVICE|ATA_S_ERROR|ATA_S_DRQ)) == ATA_S_SERVICE) {
940 ATA_OUTB(ch->r_bmio, ATA_BMSTAT_PORT,
941 ata_dmastatus(ch) | ATA_BMSTAT_INTERRUPT);
943 if ((ATA_INB(ch->r_io, ATA_DRIVE) & ATA_SLAVE) == ATA_MASTER) {
944 if ((ch->devices & ATA_ATA_MASTER) && ch->device[MASTER].driver)
945 return ad_service((struct ad_softc *)
946 ch->device[MASTER].driver, 0);
949 if ((ch->devices & ATA_ATA_SLAVE) && ch->device[SLAVE].driver)
950 return ad_service((struct ad_softc *)
951 ch->device[SLAVE].driver, 0);
955 return ATA_OP_FINISHED;
959 ata_wait(struct ata_device *atadev, u_int8_t mask)
964 while (timeout < 5000000) { /* timeout 5 secs */
965 atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS);
967 /* if drive fails status, reselect the drive just to be sure */
968 if (atadev->channel->status == 0xff) {
969 ata_prtdev(atadev, "no status, reselecting device\n");
970 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM|atadev->unit);
972 atadev->channel->status = ATA_INB(atadev->channel->r_io,ATA_STATUS);
973 if (atadev->channel->status == 0xff)
978 if (!(atadev->channel->status & ATA_S_BUSY))
981 if (timeout > 1000) {
990 if (atadev->channel->status & ATA_S_ERROR)
991 atadev->channel->error = ATA_INB(atadev->channel->r_io, ATA_ERROR);
992 if (timeout >= 5000000)
995 return (atadev->channel->status & ATA_S_ERROR);
997 /* Wait 50 msec for bits wanted. */
1000 atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS);
1001 if ((atadev->channel->status & mask) == mask) {
1002 if (atadev->channel->status & ATA_S_ERROR)
1003 atadev->channel->error=ATA_INB(atadev->channel->r_io,ATA_ERROR);
1004 return (atadev->channel->status & ATA_S_ERROR);
1012 ata_command(struct ata_device *atadev, u_int8_t command,
1013 u_int64_t lba, u_int16_t count, u_int8_t feature, int flags)
1017 ata_prtdev(atadev, "ata_command: addr=%04lx, cmd=%02x, "
1018 "lba=%lld, count=%d, feature=%d, flags=%02x\n",
1019 rman_get_start(atadev->channel->r_io),
1020 command, lba, count, feature, flags);
1024 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1026 /* disable interrupt from device */
1027 if (atadev->channel->flags & ATA_QUEUED)
1028 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_4BIT);
1030 /* ready to issue command ? */
1031 if (ata_wait(atadev, 0) < 0) {
1032 ata_prtdev(atadev, "timeout sending command=%02x s=%02x e=%02x\n",
1033 command, atadev->channel->status, atadev->channel->error);
1037 /* only use 48bit addressing if needed because of the overhead */
1038 if ((lba > 268435455 || count > 256) && atadev->param &&
1039 atadev->param->support.address48) {
1040 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, (feature>>8) & 0xff);
1041 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, feature);
1042 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, (count>>8) & 0xff);
1043 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, count & 0xff);
1044 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, (lba>>24) & 0xff);
1045 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, lba & 0xff);
1046 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>32) & 0xff);
1047 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>8) & 0xff);
1048 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>40) & 0xff);
1049 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>16) & 0xff);
1050 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_LBA | atadev->unit);
1052 /* translate command into 48bit version */
1055 command = ATA_C_READ48; break;
1056 case ATA_C_READ_MUL:
1057 command = ATA_C_READ_MUL48; break;
1058 case ATA_C_READ_DMA:
1059 command = ATA_C_READ_DMA48; break;
1060 case ATA_C_READ_DMA_QUEUED:
1061 command = ATA_C_READ_DMA_QUEUED48; break;
1063 command = ATA_C_WRITE48; break;
1064 case ATA_C_WRITE_MUL:
1065 command = ATA_C_WRITE_MUL48; break;
1066 case ATA_C_WRITE_DMA:
1067 command = ATA_C_WRITE_DMA48; break;
1068 case ATA_C_WRITE_DMA_QUEUED:
1069 command = ATA_C_WRITE_DMA_QUEUED48; break;
1070 case ATA_C_FLUSHCACHE:
1071 command = ATA_C_FLUSHCACHE48; break;
1073 ata_prtdev(atadev, "can't translate cmd to 48bit version\n");
1078 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, feature);
1079 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, count);
1080 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, lba & 0xff);
1081 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>8) & 0xff);
1082 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>16) & 0xff);
1083 if (atadev->flags & ATA_D_USE_CHS)
1084 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE,
1085 ATA_D_IBM | atadev->unit | ((lba>>24) & 0xf));
1087 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE,
1088 ATA_D_IBM | ATA_D_LBA | atadev->unit | ((lba>>24) &0xf));
1091 switch (flags & ATA_WAIT_MASK) {
1093 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1095 /* enable interrupt */
1096 if (atadev->channel->flags & ATA_QUEUED)
1097 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
1101 atadev->channel->active |= ATA_WAIT_INTR;
1102 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1104 /* enable interrupt */
1105 if (atadev->channel->flags & ATA_QUEUED)
1106 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
1108 if (tsleep((caddr_t)atadev->channel, 0, "atacmd", 10 * hz)) {
1109 ata_prtdev(atadev, "timeout waiting for interrupt\n");
1110 atadev->channel->active &= ~ATA_WAIT_INTR;
1115 case ATA_WAIT_READY:
1116 atadev->channel->active |= ATA_WAIT_READY;
1117 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1118 if (ata_wait(atadev, ATA_S_READY) < 0) {
1119 ata_prtdev(atadev, "timeout waiting for cmd=%02x s=%02x e=%02x\n",
1120 command, atadev->channel->status,atadev->channel->error);
1123 atadev->channel->active &= ~ATA_WAIT_READY;
1130 ata_enclosure_start(struct ata_device *atadev)
1132 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1134 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1136 ATA_INB(atadev->channel->r_io, ATA_CMD);
1138 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1140 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1142 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1144 ATA_INB(atadev->channel->r_io, ATA_COUNT);
1146 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1151 ata_enclosure_end(struct ata_device *atadev)
1153 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1158 ata_enclosure_chip_start(struct ata_device *atadev)
1160 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0b);
1161 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0a);
1163 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08);
1167 ata_enclosure_chip_end(struct ata_device *atadev)
1169 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08);
1171 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0a);
1173 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0b);
1178 ata_enclosure_chip_rdbit(struct ata_device *atadev)
1182 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0);
1184 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x02);
1186 val = ATA_INB(atadev->channel->r_io, ATA_SECTOR) & 0x01;
1192 ata_enclosure_chip_wrbit(struct ata_device *atadev, u_int8_t data)
1194 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08 | (data & 0x01));
1196 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08 | 0x02 | (data & 0x01));
1201 ata_enclosure_chip_rw(struct ata_device *atadev, int rw, u_int8_t val)
1206 for (i = 0; i < 8; i++)
1207 ata_enclosure_chip_wrbit(atadev, (val & (0x80 >> i)) ? 1 : 0);
1210 for (i = 0; i < 8; i++)
1211 val = (val << 1) | ata_enclosure_chip_rdbit(atadev);
1213 ata_enclosure_chip_wrbit(atadev, 0);
1218 ata_enclosure_sensor(struct ata_device *atadev,
1219 int rw, u_int8_t idx, u_int8_t data)
1221 ata_enclosure_start(atadev);
1222 ata_enclosure_chip_start(atadev);
1223 ata_enclosure_chip_rw(atadev, 1, 0x5a);
1224 ata_enclosure_chip_rw(atadev, 1, idx);
1226 ata_enclosure_chip_rw(atadev, 1, data);
1229 ata_enclosure_chip_end(atadev);
1230 ata_enclosure_chip_start(atadev);
1231 ata_enclosure_chip_rw(atadev, 1, 0x5b);
1232 data = ata_enclosure_chip_rw(atadev, 0, 0);
1234 ata_enclosure_chip_end(atadev);
1235 ata_enclosure_end(atadev);
1240 ata_enclosure_status(struct ata_device *atadev,
1241 int *fan, int *temp, int *v05, int *v12)
1243 u_int8_t id1, id2, cnt, div;
1246 if (atadev->flags & ATA_D_ENC_PRESENT) {
1247 ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL);
1248 ata_enclosure_sensor(atadev, 1, 0x4e, 0);
1249 id1 = ata_enclosure_sensor(atadev, 0, 0x4f, 0);
1250 ata_enclosure_sensor(atadev, 1, 0x4e, 0x80);
1251 id2 = ata_enclosure_sensor(atadev, 0, 0x4f, 0);
1252 if (id1 == 0xa3 && id2 == 0x5c) {
1253 div = 1 << (((ata_enclosure_sensor(atadev, 0, 0x5d, 0)&0x20)>>3)+
1254 ((ata_enclosure_sensor(atadev, 0, 0x47, 0)&0x30)>>4)+1);
1255 cnt = ata_enclosure_sensor(atadev, 0, 0x28, 0);
1259 *fan = 1350000 / cnt / div;
1260 ata_enclosure_sensor(atadev, 1, 0x4e, 0x01);
1261 *temp = (ata_enclosure_sensor(atadev, 0, 0x50, 0) * 10) +
1262 (ata_enclosure_sensor(atadev, 0, 0x50, 0) & 0x80 ? 5 : 0);
1263 *v05 = ata_enclosure_sensor(atadev, 0, 0x23, 0) * 27;
1264 *v12 = ata_enclosure_sensor(atadev, 0, 0x24, 0) * 61;
1267 ATA_UNLOCK_CH(atadev->channel);
1273 ata_enclosure_print(struct ata_device *atadev)
1276 int fan, temp, v05, v12;
1278 ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL);
1279 ata_enclosure_start(atadev);
1280 id = ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1282 st = ATA_INB(atadev->channel->r_io, ATA_COUNT);
1284 ata_enclosure_end(atadev);
1285 ATA_UNLOCK_CH(atadev->channel);
1287 switch (id & 0x93) {
1289 ata_prtdev(atadev, "Universal enclosure");
1292 ata_prtdev(atadev, "FastSwap enclosure");
1296 ata_prtdev(atadev, "SuperSwap enclosure");
1299 atadev->flags &= ~ATA_D_ENC_PRESENT;
1302 atadev->flags |= ATA_D_ENC_PRESENT;
1304 if (ata_enclosure_status(atadev, &fan, &temp, &v05, &v12))
1305 printf(" detected\n");
1307 printf(" [FAN:%drpm TEMP:%d.%01dC %d.%03dV %d.%03dV]\n",
1308 fan, temp/10, temp%10, v05/1000, v05%1000, v12/1000, v12%1000);
1312 ata_enclosure_leds(struct ata_device *atadev, u_int8_t color)
1314 if (atadev->flags & ATA_D_ENC_PRESENT) {
1317 ata_enclosure_start(atadev);
1318 reg = ATA_INB(atadev->channel->r_io, ATA_COUNT);
1320 ATA_OUTB(atadev->channel->r_io, ATA_COUNT,
1321 (color & ATA_LED_MASK) | (reg & ~ATA_LED_MASK));
1323 ata_enclosure_end(atadev);
1328 ata_change_mode(struct ata_device *atadev, int mode)
1330 int umode, wmode, pmode;
1332 umode = ata_umode(atadev->param);
1333 wmode = ata_wmode(atadev->param);
1334 pmode = ata_pmode(atadev->param);
1336 switch (mode & ATA_DMA_MASK) {
1338 if ((mode & ATA_MODE_MASK) < umode)
1339 umode = mode & ATA_MODE_MASK;
1342 if ((mode & ATA_MODE_MASK) < wmode)
1343 wmode = mode & ATA_MODE_MASK;
1347 if (((mode & ATA_MODE_MASK) - ATA_PIO0) < pmode)
1348 pmode = (mode & ATA_MODE_MASK) - ATA_PIO0;
1353 ATA_SLEEPLOCK_CH(atadev->channel, ATA_ACTIVE);
1354 ata_dmainit(atadev->channel, atadev->unit, pmode, wmode, umode);
1355 ATA_UNLOCK_CH(atadev->channel);
1356 ata_start(atadev->channel); /* XXX SOS */
1360 ata_printf(struct ata_channel *ch, int device, const char * fmt, ...)
1366 ret = printf("ata%d: ", device_get_unit(ch->dev));
1368 if (ch->device[ATA_DEV(device)].name)
1369 ret = printf("%s: ", ch->device[ATA_DEV(device)].name);
1371 ret = printf("ata%d-%s: ", device_get_unit(ch->dev),
1372 (device == ATA_MASTER) ? "master" : "slave");
1375 ret += vprintf(fmt, ap);
1381 ata_prtdev(struct ata_device *atadev, const char * fmt, ...)
1387 ret = printf("%s: ", atadev->name);
1389 ret = printf("ata%d-%s: ", device_get_unit(atadev->channel->dev),
1390 (atadev->unit == ATA_MASTER) ? "master" : "slave");
1392 ret += vprintf(fmt, ap);
1398 ata_set_name(struct ata_device *atadev, char *name, int lun)
1400 atadev->name = malloc(strlen(name) + 4, M_ATA, M_NOWAIT);
1402 sprintf(atadev->name, "%s%d", name, lun);
1406 ata_free_name(struct ata_device *atadev)
1409 free(atadev->name, M_ATA);
1410 atadev->name = NULL;
1414 ata_get_lun(u_int32_t *map)
1416 int lun = ffs(~*map) - 1;
1423 ata_test_lun(u_int32_t *map, int lun)
1425 return (*map & (1 << lun));
1429 ata_free_lun(u_int32_t *map, int lun)
1431 *map &= ~(1 << lun);
1435 ata_mode2str(int mode)
1438 case ATA_PIO: return "BIOSPIO";
1439 case ATA_PIO0: return "PIO0";
1440 case ATA_PIO1: return "PIO1";
1441 case ATA_PIO2: return "PIO2";
1442 case ATA_PIO3: return "PIO3";
1443 case ATA_PIO4: return "PIO4";
1444 case ATA_DMA: return "BIOSDMA";
1445 case ATA_WDMA2: return "WDMA2";
1446 case ATA_UDMA2: return "UDMA33";
1447 case ATA_UDMA4: return "UDMA66";
1448 case ATA_UDMA5: return "UDMA100";
1449 case ATA_UDMA6: return "UDMA133";
1450 default: return "???";
1455 ata_pmode(struct ata_params *ap)
1457 if (ap->atavalid & ATA_FLAG_64_70) {
1458 if (ap->apiomodes & 2)
1460 if (ap->apiomodes & 1)
1463 if (ap->retired_piomode == 2)
1465 if (ap->retired_piomode == 1)
1467 if (ap->retired_piomode == 0)
1473 ata_wmode(struct ata_params *ap)
1475 if (ap->mwdmamodes & 0x04)
1477 if (ap->mwdmamodes & 0x02)
1479 if (ap->mwdmamodes & 0x01)
1485 ata_umode(struct ata_params *ap)
1487 if (ap->atavalid & ATA_FLAG_88) {
1488 if (ap->udmamodes & 0x40)
1490 if (ap->udmamodes & 0x20)
1492 if (ap->udmamodes & 0x10)
1494 if (ap->udmamodes & 0x08)
1496 if (ap->udmamodes & 0x04)
1498 if (ap->udmamodes & 0x02)
1500 if (ap->udmamodes & 0x01)
1507 bswap(int8_t *buf, int len)
1509 u_int16_t *ptr = (u_int16_t*)(buf + len);
1511 while (--ptr >= (u_int16_t*)buf)
1516 btrim(int8_t *buf, int len)
1520 for (ptr = buf; ptr < buf+len; ++ptr)
1523 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
1528 bpack(int8_t *src, int8_t *dst, int len)
1532 for (i = j = blank = 0 ; i < len; i++) {
1533 if (blank && src[i] == ' ') continue;
1534 if (blank && src[i] != ' ') {
1539 if (src[i] == ' ') {
1553 /* register controlling device */
1554 make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "ata");
1556 /* register boot attach to be run when interrupts are enabled */
1557 if (!(ata_delayed_attach = (struct intr_config_hook *)
1558 malloc(sizeof(struct intr_config_hook),
1559 M_TEMP, M_NOWAIT | M_ZERO))) {
1560 printf("ata: malloc of delayed attach hook failed\n");
1564 ata_delayed_attach->ich_func = (void*)ata_boot_attach;
1565 if (config_intrhook_establish(ata_delayed_attach) != 0) {
1566 printf("ata: config_intrhook_establish failed\n");
1567 free(ata_delayed_attach, M_TEMP);
1570 SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL)