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 $
38 #include <sys/param.h>
39 #include <sys/systm.h>
41 #include <sys/kernel.h>
44 #include <sys/module.h>
47 #include <sys/malloc.h>
48 #include <sys/devicestat.h>
49 #include <sys/sysctl.h>
50 #include <machine/stdarg.h>
51 #include <machine/resource.h>
52 #include <machine/bus.h>
53 #include <machine/clock.h>
56 #include <machine/md_var.h>
58 #include <dev/ata/ata-all.h>
59 #include <dev/ata/ata-disk.h>
60 #include <dev/ata/ata-raid.h>
61 #include <dev/ata/atapi-all.h>
63 /* device structures */
64 static d_ioctl_t ataioctl;
65 static struct cdevsw ata_cdevsw = {
67 /* close */ nullclose,
73 /* strategy */ nostrategy,
82 static void ata_boot_attach(void);
83 static void ata_intr(void *);
84 static int ata_getparam(struct ata_device *, u_int8_t);
85 static int ata_service(struct ata_channel *);
86 static void bswap(int8_t *, int);
87 static void btrim(int8_t *, int);
88 static void bpack(int8_t *, int8_t *, int);
89 static void ata_change_mode(struct ata_device *, int);
90 static u_int8_t ata_enclosure_sensor(struct ata_device *, int, u_int8_t, u_int8_t);
91 static int ata_enclosure_status(struct ata_device *, int *, int *, int *, int *);
94 SYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters");
97 devclass_t ata_devclass;
100 static struct intr_config_hook *ata_delayed_attach = NULL;
101 static MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer");
104 #define DEV_ATAPIALL NATAPICD > 0 || NATAPIFD > 0 || \
105 NATAPIST > 0 || NATAPICAM > 0
108 ata_probe(device_t dev)
110 struct ata_channel *ch;
113 if (!dev || !(ch = device_get_softc(dev)))
116 if (ch->r_io || ch->r_altio || ch->r_irq)
119 /* initialize the softc basics */
120 ch->active = ATA_IDLE;
123 rid = ATA_IOADDR_RID;
124 ch->r_io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
125 ATA_IOSIZE, RF_ACTIVE);
129 rid = ATA_ALTADDR_RID;
130 ch->r_altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
131 ATA_ALTIOSIZE, RF_ACTIVE);
135 rid = ATA_BMADDR_RID;
136 ch->r_bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
137 ATA_BMIOSIZE, RF_ACTIVE);
139 ata_printf(ch, -1, "iobase=0x%04x altiobase=0x%04x bmaddr=0x%04x\n",
140 (int)rman_get_start(ch->r_io),
141 (int)rman_get_start(ch->r_altio),
142 (ch->r_bmio) ? (int)rman_get_start(ch->r_bmio) : 0);
146 ch->device[MASTER].channel = ch;
147 ch->device[MASTER].unit = ATA_MASTER;
148 ch->device[MASTER].mode = ATA_PIO;
149 ch->device[SLAVE].channel = ch;
150 ch->device[SLAVE].unit = ATA_SLAVE;
151 ch->device[SLAVE].mode = ATA_PIO;
152 TAILQ_INIT(&ch->ata_queue);
153 TAILQ_INIT(&ch->atapi_queue);
158 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ch->r_io);
160 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ch->r_altio);
162 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, ch->r_bmio);
164 ata_printf(ch, -1, "probe allocation failed\n");
169 ata_attach(device_t dev)
171 struct ata_channel *ch;
174 if (!dev || !(ch = device_get_softc(dev)))
178 ch->r_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
179 RF_SHAREABLE | RF_ACTIVE);
181 ata_printf(ch, -1, "unable to allocate interrupt\n");
184 if ((error = bus_setup_intr(dev, ch->r_irq, INTR_TYPE_BIO,
185 ata_intr, ch, &ch->ih))) {
186 ata_printf(ch, -1, "unable to setup interrupt\n");
191 * do not attach devices if we are in early boot, this is done later
192 * when interrupts are enabled by a hook into the boot process.
193 * otherwise attach what the probe has found in ch->devices.
195 if (!ata_delayed_attach) {
198 if (ch->devices & ATA_ATA_SLAVE)
199 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
200 ch->devices &= ~ATA_ATA_SLAVE;
201 if (ch->devices & ATA_ATAPI_SLAVE)
202 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
203 ch->devices &= ~ATA_ATAPI_SLAVE;
204 if (ch->devices & ATA_ATA_MASTER)
205 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
206 ch->devices &= ~ATA_ATA_MASTER;
207 if (ch->devices & ATA_ATAPI_MASTER)
208 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
209 ch->devices &= ~ATA_ATAPI_MASTER;
211 if (ch->devices & ATA_ATA_MASTER)
212 ad_attach(&ch->device[MASTER]);
213 if (ch->devices & ATA_ATA_SLAVE)
214 ad_attach(&ch->device[SLAVE]);
217 if (ch->devices & ATA_ATAPI_MASTER)
218 atapi_attach(&ch->device[MASTER]);
219 if (ch->devices & ATA_ATAPI_SLAVE)
220 atapi_attach(&ch->device[SLAVE]);
223 atapi_cam_attach_bus(ch);
231 ata_detach(device_t dev)
233 struct ata_channel *ch;
236 if (!dev || !(ch = device_get_softc(dev)) ||
237 !ch->r_io || !ch->r_altio || !ch->r_irq)
240 /* make sure channel is not busy */
241 ATA_SLEEPLOCK_CH(ch, ATA_CONTROL);
245 if (ch->devices & ATA_ATA_MASTER && ch->device[MASTER].driver)
246 ad_detach(&ch->device[MASTER], 1);
247 if (ch->devices & ATA_ATA_SLAVE && ch->device[SLAVE].driver)
248 ad_detach(&ch->device[SLAVE], 1);
251 if (ch->devices & ATA_ATAPI_MASTER && ch->device[MASTER].driver)
252 atapi_detach(&ch->device[MASTER]);
253 if (ch->devices & ATA_ATAPI_SLAVE && ch->device[SLAVE].driver)
254 atapi_detach(&ch->device[SLAVE]);
257 atapi_cam_detach_bus(ch);
261 if (ch->device[MASTER].param) {
262 free(ch->device[MASTER].param, M_ATA);
263 ch->device[MASTER].param = NULL;
265 if (ch->device[SLAVE].param) {
266 free(ch->device[SLAVE].param, M_ATA);
267 ch->device[SLAVE].param = NULL;
269 ch->device[MASTER].driver = NULL;
270 ch->device[SLAVE].driver = NULL;
271 ch->device[MASTER].mode = ATA_PIO;
272 ch->device[SLAVE].mode = ATA_PIO;
275 bus_teardown_intr(dev, ch->r_irq, ch->ih);
276 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
278 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, ch->r_bmio);
279 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ch->r_altio);
280 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ch->r_io);
290 ata_resume(device_t dev)
292 return ata_reinit(device_get_softc(dev));
296 ataioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, struct proc *p)
298 struct ata_cmd *iocmd = (struct ata_cmd *)addr;
299 struct ata_channel *ch;
300 device_t device = devclass_get_device(ata_devclass, iocmd->channel);
306 if (iocmd->channel < -1 || iocmd->device < -1 || iocmd->device > SLAVE)
309 switch (iocmd->cmd) {
311 /* should enable channel HW on controller that can SOS XXX */
312 error = ata_probe(device);
314 error = ata_attach(device);
318 error = ata_detach(device);
319 /* should disable channel HW on controller that can SOS XXX */
323 if (!device || !(ch = device_get_softc(device)))
325 ATA_SLEEPLOCK_CH(ch, ATA_ACTIVE);
326 if ((error = ata_reinit(ch)))
331 if (!device || !(ch = device_get_softc(device)))
334 if ((iocmd->device == MASTER || iocmd->device == -1) &&
335 ch->device[MASTER].driver)
336 iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode;
338 iocmd->u.mode.mode[MASTER] = -1;
340 if ((iocmd->device == SLAVE || iocmd->device == -1) &&
341 ch->device[SLAVE].param)
342 iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode;
344 iocmd->u.mode.mode[SLAVE] = -1;
348 if (!device || !(ch = device_get_softc(device)))
351 if ((iocmd->device == MASTER || iocmd->device == -1) &&
352 iocmd->u.mode.mode[MASTER] >= 0 && ch->device[MASTER].param) {
353 ata_change_mode(&ch->device[MASTER],iocmd->u.mode.mode[MASTER]);
354 iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode;
357 iocmd->u.mode.mode[MASTER] = -1;
359 if ((iocmd->device == SLAVE || iocmd->device == -1) &&
360 iocmd->u.mode.mode[SLAVE] >= 0 && ch->device[SLAVE].param) {
361 ata_change_mode(&ch->device[SLAVE], iocmd->u.mode.mode[SLAVE]);
362 iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode;
365 iocmd->u.mode.mode[SLAVE] = -1;
369 if (!device || !(ch = device_get_softc(device)))
372 iocmd->u.param.type[MASTER] =
373 ch->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER);
374 iocmd->u.param.type[SLAVE] =
375 ch->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE);
377 if (ch->device[MASTER].name)
378 strcpy(iocmd->u.param.name[MASTER], ch->device[MASTER].name);
379 if (ch->device[SLAVE].name)
380 strcpy(iocmd->u.param.name[SLAVE], ch->device[SLAVE].name);
382 if (ch->device[MASTER].param)
383 bcopy(ch->device[MASTER].param, &iocmd->u.param.params[MASTER],
384 sizeof(struct ata_params));
385 if (ch->device[SLAVE].param)
386 bcopy(ch->device[SLAVE].param, &iocmd->u.param.params[SLAVE],
387 sizeof(struct ata_params));
391 struct ata_device *atadev;
393 if (!device || !(ch = device_get_softc(device)))
396 if (iocmd->device == SLAVE)
397 atadev = &ch->device[SLAVE];
399 atadev = &ch->device[MASTER];
401 return ata_enclosure_status(atadev,
402 &iocmd->u.enclosure.fan,
403 &iocmd->u.enclosure.temp,
404 &iocmd->u.enclosure.v05,
405 &iocmd->u.enclosure.v12);
410 return ata_raid_rebuild(iocmd->channel);
413 return ata_raid_create(&iocmd->u.raid_setup);
416 return ata_raid_delete(iocmd->channel);
419 return ata_raid_status(iocmd->channel, &iocmd->u.raid_status);
423 struct ata_device *atadev;
426 if (!device || !(ch = device_get_softc(device)))
429 if (!(atadev = &ch->device[iocmd->device]) ||
430 !(ch->devices & (iocmd->device == MASTER ?
431 ATA_ATAPI_MASTER : ATA_ATAPI_SLAVE)))
434 if (!(buf = malloc(iocmd->u.atapi.count, M_ATA, M_NOWAIT)))
437 if (iocmd->u.atapi.flags & ATAPI_CMD_WRITE) {
438 error = copyin(iocmd->u.atapi.data, buf, iocmd->u.atapi.count);
442 error = atapi_queue_cmd(atadev, iocmd->u.atapi.ccb,
443 buf, iocmd->u.atapi.count,
444 (iocmd->u.atapi.flags == ATAPI_CMD_READ ?
445 ATPR_F_READ : 0) | ATPR_F_QUIET,
446 iocmd->u.atapi.timeout, NULL, NULL);
448 iocmd->u.atapi.error = error;
449 bcopy(&atadev->result, iocmd->u.atapi.sense_data,
450 sizeof(struct atapi_reqsense));
453 else if (iocmd->u.atapi.flags & ATAPI_CMD_READ)
454 error = copyout(buf, iocmd->u.atapi.data, iocmd->u.atapi.count);
467 ata_getparam(struct ata_device *atadev, u_int8_t command)
469 struct ata_params *ata_parm;
472 if (!(ata_parm = malloc(sizeof(struct ata_params), M_ATA, M_NOWAIT))) {
473 ata_prtdev(atadev, "malloc for identify data failed\n");
477 /* apparently some devices needs this repeated */
479 if (ata_command(atadev, command, 0, 0, 0, ATA_IMMEDIATE)) {
480 ata_prtdev(atadev, "%s identify failed\n",
481 command == ATA_C_ATAPI_IDENTIFY ? "ATAPI" : "ATA");
482 free(ata_parm, M_ATA);
486 ata_prtdev(atadev, "%s identify retries exceeded\n",
487 command == ATA_C_ATAPI_IDENTIFY ? "ATAPI" : "ATA");
488 free(ata_parm, M_ATA);
491 } while (ata_wait(atadev, ((command == ATA_C_ATAPI_IDENTIFY) ?
492 ATA_S_DRQ : (ATA_S_READY|ATA_S_DSC|ATA_S_DRQ))));
493 ATA_INSW(atadev->channel->r_io, ATA_DATA, (int16_t *)ata_parm,
494 sizeof(struct ata_params)/sizeof(int16_t));
496 if (command == ATA_C_ATA_IDENTIFY ||
497 !((ata_parm->model[0] == 'N' && ata_parm->model[1] == 'E') ||
498 (ata_parm->model[0] == 'F' && ata_parm->model[1] == 'X') ||
499 (ata_parm->model[0] == 'P' && ata_parm->model[1] == 'i')))
500 bswap(ata_parm->model, sizeof(ata_parm->model));
501 btrim(ata_parm->model, sizeof(ata_parm->model));
502 bpack(ata_parm->model, ata_parm->model, sizeof(ata_parm->model));
503 bswap(ata_parm->revision, sizeof(ata_parm->revision));
504 btrim(ata_parm->revision, sizeof(ata_parm->revision));
505 bpack(ata_parm->revision, ata_parm->revision, sizeof(ata_parm->revision));
506 bswap(ata_parm->serial, sizeof(ata_parm->serial));
507 btrim(ata_parm->serial, sizeof(ata_parm->serial));
508 bpack(ata_parm->serial, ata_parm->serial, sizeof(ata_parm->serial));
509 atadev->param = ata_parm;
514 ata_boot_attach(void)
516 struct ata_channel *ch;
519 if (ata_delayed_attach) {
520 config_intrhook_disestablish(ata_delayed_attach);
521 free(ata_delayed_attach, M_TEMP);
522 ata_delayed_attach = NULL;
527 * run through all ata devices and look for real ATA & ATAPI devices
528 * using the hints we found in the early probe, this avoids some of
529 * the delays probing of non-exsistent devices can cause.
531 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
532 if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
534 if (ch->devices & ATA_ATA_SLAVE)
535 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
536 ch->devices &= ~ATA_ATA_SLAVE;
537 if (ch->devices & ATA_ATAPI_SLAVE)
538 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
539 ch->devices &= ~ATA_ATAPI_SLAVE;
540 if (ch->devices & ATA_ATA_MASTER)
541 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
542 ch->devices &= ~ATA_ATA_MASTER;
543 if (ch->devices & ATA_ATAPI_MASTER)
544 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
545 ch->devices &= ~ATA_ATAPI_MASTER;
549 /* now we know whats there, do the real attach, first the ATA disks */
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_MASTER)
554 ad_attach(&ch->device[MASTER]);
555 if (ch->devices & ATA_ATA_SLAVE)
556 ad_attach(&ch->device[SLAVE]);
561 /* then the atapi devices */
562 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
563 if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
565 if (ch->devices & ATA_ATAPI_MASTER)
566 atapi_attach(&ch->device[MASTER]);
567 if (ch->devices & ATA_ATAPI_SLAVE)
568 atapi_attach(&ch->device[SLAVE]);
570 atapi_cam_attach_bus(ch);
580 struct ata_channel *ch = (struct ata_channel *)data;
582 * on PCI systems we might share an interrupt line with another
583 * device or our twin ATA channel, so call ch->intr_func to figure
584 * out if it is really an interrupt we should process here
586 if (ch->intr_func && ch->intr_func(ch))
589 /* if drive is busy it didn't interrupt */
590 if (ATA_INB(ch->r_altio, ATA_ALTSTAT) & ATA_S_BUSY) {
592 if (!(ATA_INB(ch->r_altio, ATA_ALTSTAT) & ATA_S_DRQ))
596 /* clear interrupt and get status */
597 ch->status = ATA_INB(ch->r_io, ATA_STATUS);
599 if (ch->status & ATA_S_ERROR)
600 ch->error = ATA_INB(ch->r_io, ATA_ERROR);
602 /* find & call the responsible driver to process this interrupt */
603 switch (ch->active) {
606 if (!ch->running || ad_interrupt(ch->running) == ATA_OP_CONTINUES)
611 case ATA_ACTIVE_ATAPI:
612 if (!ch->running || atapi_interrupt(ch->running) == ATA_OP_CONTINUES)
617 case ATA_WAIT_INTR | ATA_CONTROL:
622 case ATA_WAIT_READY | ATA_CONTROL:
626 if (ch->flags & ATA_QUEUED) {
627 ch->active = ATA_ACTIVE;
628 if (ata_service(ch) == ATA_OP_CONTINUES)
636 static int intr_count = 0;
638 if (intr_count++ < 10)
639 ata_printf(ch, -1, "unwanted interrupt #%d active=%02x s=%02x\n",
640 intr_count, ch->active, ch->status);
645 ch->active &= ATA_CONTROL;
646 if (ch->active & ATA_CONTROL)
654 ata_start(struct ata_channel *ch)
657 struct ad_request *ad_request;
660 struct atapi_request *atapi_request;
664 if (!ATA_LOCK_CH(ch, ATA_ACTIVE))
669 /* find & call the responsible driver if anything on the ATA queue */
670 if (TAILQ_EMPTY(&ch->ata_queue)) {
671 if (ch->devices & (ATA_ATA_MASTER) && ch->device[MASTER].driver)
672 ad_start(&ch->device[MASTER]);
673 if (ch->devices & (ATA_ATA_SLAVE) && ch->device[SLAVE].driver)
674 ad_start(&ch->device[SLAVE]);
676 if ((ad_request = TAILQ_FIRST(&ch->ata_queue))) {
677 TAILQ_REMOVE(&ch->ata_queue, ad_request, chain);
678 ch->active = ATA_ACTIVE_ATA;
679 ch->running = ad_request;
680 if (ad_transfer(ad_request) == ATA_OP_CONTINUES) {
688 /* find & call the responsible driver if anything on the ATAPI queue */
689 if (TAILQ_EMPTY(&ch->atapi_queue)) {
690 if (ch->devices & (ATA_ATAPI_MASTER) && ch->device[MASTER].driver)
691 atapi_start(&ch->device[MASTER]);
692 if (ch->devices & (ATA_ATAPI_SLAVE) && ch->device[SLAVE].driver)
693 atapi_start(&ch->device[SLAVE]);
695 if ((atapi_request = TAILQ_FIRST(&ch->atapi_queue))) {
696 TAILQ_REMOVE(&ch->atapi_queue, atapi_request, chain);
697 ch->active = ATA_ACTIVE_ATAPI;
698 ch->running = atapi_request;
699 if (atapi_transfer(atapi_request) == ATA_OP_CONTINUES) {
710 ata_reset(struct ata_channel *ch)
712 u_int8_t lsb, msb, ostat0, ostat1;
713 u_int8_t stat0 = 0, stat1 = 0;
714 int mask = 0, timeout;
716 /* do we have any signs of ATA/ATAPI HW being present ? */
717 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
719 ostat0 = ATA_INB(ch->r_io, ATA_STATUS);
720 if ((ostat0 & 0xf8) != 0xf8 && ostat0 != 0xa5) {
724 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
726 ostat1 = ATA_INB(ch->r_io, ATA_STATUS);
727 if ((ostat1 & 0xf8) != 0xf8 && ostat1 != 0xa5) {
736 /* in some setups we dont want to test for a slave */
737 if (ch->flags & ATA_NO_SLAVE) {
743 ata_printf(ch, -1, "mask=%02x ostat0=%02x ostat2=%02x\n",
744 mask, ostat0, ostat1);
747 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
749 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_RESET);
751 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_IDS);
753 ATA_INB(ch->r_io, ATA_ERROR);
755 /* wait for BUSY to go inactive */
756 for (timeout = 0; timeout < 310000; timeout++) {
757 if (stat0 & ATA_S_BUSY) {
758 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
761 /* check for ATAPI signature while its still there */
762 lsb = ATA_INB(ch->r_io, ATA_CYL_LSB);
763 msb = ATA_INB(ch->r_io, ATA_CYL_MSB);
764 stat0 = ATA_INB(ch->r_io, ATA_STATUS);
765 if (!(stat0 & ATA_S_BUSY)) {
767 ata_printf(ch, ATA_MASTER, "ATAPI %02x %02x\n", lsb, msb);
768 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB)
769 ch->devices |= ATA_ATAPI_MASTER;
772 if (stat1 & ATA_S_BUSY) {
773 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
776 /* check for ATAPI signature while its still there */
777 lsb = ATA_INB(ch->r_io, ATA_CYL_LSB);
778 msb = ATA_INB(ch->r_io, ATA_CYL_MSB);
779 stat1 = ATA_INB(ch->r_io, ATA_STATUS);
780 if (!(stat1 & ATA_S_BUSY)) {
782 ata_printf(ch, ATA_SLAVE, "ATAPI %02x %02x\n", lsb, msb);
783 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB)
784 ch->devices |= ATA_ATAPI_SLAVE;
787 if (mask == 0x01) /* wait for master only */
788 if (!(stat0 & ATA_S_BUSY))
790 if (mask == 0x02) /* wait for slave only */
791 if (!(stat1 & ATA_S_BUSY))
793 if (mask == 0x03) /* wait for both master & slave */
794 if (!(stat0 & ATA_S_BUSY) && !(stat1 & ATA_S_BUSY))
799 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
801 if (stat0 & ATA_S_BUSY)
803 if (stat1 & ATA_S_BUSY)
806 ata_printf(ch, -1, "mask=%02x stat0=%02x stat1=%02x\n",
811 if (mask & 0x01 && ostat0 != 0x00 && !(ch->devices & ATA_ATAPI_MASTER)) {
812 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
814 ATA_OUTB(ch->r_io, ATA_ERROR, 0x58);
815 ATA_OUTB(ch->r_io, ATA_CYL_LSB, 0xa5);
816 lsb = ATA_INB(ch->r_io, ATA_ERROR);
817 msb = ATA_INB(ch->r_io, ATA_CYL_LSB);
819 ata_printf(ch, ATA_MASTER, "ATA %02x %02x\n", lsb, msb);
820 if (lsb != 0x58 && msb == 0xa5)
821 ch->devices |= ATA_ATA_MASTER;
823 if (mask & 0x02 && ostat1 != 0x00 && !(ch->devices & ATA_ATAPI_SLAVE)) {
824 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
826 ATA_OUTB(ch->r_io, ATA_ERROR, 0x58);
827 ATA_OUTB(ch->r_io, ATA_CYL_LSB, 0xa5);
828 lsb = ATA_INB(ch->r_io, ATA_ERROR);
829 msb = ATA_INB(ch->r_io, ATA_CYL_LSB);
831 ata_printf(ch, ATA_SLAVE, "ATA %02x %02x\n", lsb, msb);
832 if (lsb != 0x58 && msb == 0xa5)
833 ch->devices |= ATA_ATA_SLAVE;
836 ata_printf(ch, -1, "devices=%02x\n", ch->devices);
840 ata_reinit(struct ata_channel *ch)
842 int devices, misdev, newdev;
844 if (!ch->r_io || !ch->r_altio || !ch->r_irq)
847 ATA_FORCELOCK_CH(ch, ATA_CONTROL);
849 devices = ch->devices;
850 ata_printf(ch, -1, "resetting devices .. ");
853 if ((misdev = devices & ~ch->devices)) {
857 if (misdev & ATA_ATA_MASTER && ch->device[MASTER].driver)
858 ad_detach(&ch->device[MASTER], 0);
859 if (misdev & ATA_ATA_SLAVE && ch->device[SLAVE].driver)
860 ad_detach(&ch->device[SLAVE], 0);
863 if (misdev & ATA_ATAPI_MASTER && ch->device[MASTER].driver)
864 atapi_detach(&ch->device[MASTER]);
865 if (misdev & ATA_ATAPI_SLAVE && ch->device[SLAVE].driver)
866 atapi_detach(&ch->device[SLAVE]);
868 if (misdev & ATA_ATA_MASTER || misdev & ATA_ATAPI_MASTER) {
869 if (ch->device[MASTER].param)
870 free(ch->device[MASTER].param, M_ATA);
871 ch->device[MASTER].param = NULL;
873 if (misdev & ATA_ATA_SLAVE || misdev & ATA_ATAPI_SLAVE) {
874 if (ch->device[SLAVE].param)
875 free(ch->device[SLAVE].param, M_ATA);
876 ch->device[SLAVE].param = NULL;
879 if ((newdev = ~devices & ch->devices)) {
880 if (newdev & ATA_ATA_MASTER)
881 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
882 ch->devices &= ~ATA_ATA_MASTER;
883 if (newdev & ATA_ATA_SLAVE)
884 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
885 ch->devices &= ~ATA_ATA_SLAVE;
886 if (newdev & ATA_ATAPI_MASTER)
887 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
888 ch->devices &= ~ATA_ATAPI_MASTER;
889 if (newdev & ATA_ATAPI_SLAVE)
890 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
891 ch->devices &= ~ATA_ATAPI_SLAVE;
893 newdev = ~devices & ch->devices;
894 if (!misdev && newdev)
897 if (newdev & ATA_ATA_MASTER && !ch->device[MASTER].driver)
898 ad_attach(&ch->device[MASTER]);
899 else if (ch->devices & ATA_ATA_MASTER && ch->device[MASTER].driver) {
900 ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY);
901 ad_reinit(&ch->device[MASTER]);
903 if (newdev & ATA_ATA_SLAVE && !ch->device[SLAVE].driver)
904 ad_attach(&ch->device[SLAVE]);
905 else if (ch->devices & (ATA_ATA_SLAVE) && ch->device[SLAVE].driver) {
906 ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY);
907 ad_reinit(&ch->device[SLAVE]);
911 if (newdev & ATA_ATAPI_MASTER && !ch->device[MASTER].driver)
912 atapi_attach(&ch->device[MASTER]);
913 else if (ch->devices & (ATA_ATAPI_MASTER) && ch->device[MASTER].driver) {
914 ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY);
915 atapi_reinit(&ch->device[MASTER]);
917 if (newdev & ATA_ATAPI_SLAVE && !ch->device[SLAVE].driver)
918 atapi_attach(&ch->device[SLAVE]);
919 else if (ch->devices & (ATA_ATAPI_SLAVE) && ch->device[SLAVE].driver) {
920 ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY);
921 atapi_reinit(&ch->device[SLAVE]);
925 if (ch->devices & (ATA_ATAPI_MASTER | ATA_ATAPI_SLAVE))
926 atapi_cam_reinit_bus(ch);
935 ata_service(struct ata_channel *ch)
937 /* do we have a SERVICE request from the drive ? */
938 if ((ch->status & (ATA_S_SERVICE|ATA_S_ERROR|ATA_S_DRQ)) == ATA_S_SERVICE) {
939 ATA_OUTB(ch->r_bmio, ATA_BMSTAT_PORT,
940 ata_dmastatus(ch) | ATA_BMSTAT_INTERRUPT);
942 if ((ATA_INB(ch->r_io, ATA_DRIVE) & ATA_SLAVE) == ATA_MASTER) {
943 if ((ch->devices & ATA_ATA_MASTER) && ch->device[MASTER].driver)
944 return ad_service((struct ad_softc *)
945 ch->device[MASTER].driver, 0);
948 if ((ch->devices & ATA_ATA_SLAVE) && ch->device[SLAVE].driver)
949 return ad_service((struct ad_softc *)
950 ch->device[SLAVE].driver, 0);
954 return ATA_OP_FINISHED;
958 ata_wait(struct ata_device *atadev, u_int8_t mask)
963 while (timeout < 5000000) { /* timeout 5 secs */
964 atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS);
966 /* if drive fails status, reselect the drive just to be sure */
967 if (atadev->channel->status == 0xff) {
968 ata_prtdev(atadev, "no status, reselecting device\n");
969 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM|atadev->unit);
971 atadev->channel->status = ATA_INB(atadev->channel->r_io,ATA_STATUS);
972 if (atadev->channel->status == 0xff)
977 if (!(atadev->channel->status & ATA_S_BUSY))
980 if (timeout > 1000) {
989 if (atadev->channel->status & ATA_S_ERROR)
990 atadev->channel->error = ATA_INB(atadev->channel->r_io, ATA_ERROR);
991 if (timeout >= 5000000)
994 return (atadev->channel->status & ATA_S_ERROR);
996 /* Wait 50 msec for bits wanted. */
999 atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS);
1000 if ((atadev->channel->status & mask) == mask) {
1001 if (atadev->channel->status & ATA_S_ERROR)
1002 atadev->channel->error=ATA_INB(atadev->channel->r_io,ATA_ERROR);
1003 return (atadev->channel->status & ATA_S_ERROR);
1011 ata_command(struct ata_device *atadev, u_int8_t command,
1012 u_int64_t lba, u_int16_t count, u_int8_t feature, int flags)
1016 ata_prtdev(atadev, "ata_command: addr=%04lx, cmd=%02x, "
1017 "lba=%lld, count=%d, feature=%d, flags=%02x\n",
1018 rman_get_start(atadev->channel->r_io),
1019 command, lba, count, feature, flags);
1023 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1025 /* disable interrupt from device */
1026 if (atadev->channel->flags & ATA_QUEUED)
1027 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_4BIT);
1029 /* ready to issue command ? */
1030 if (ata_wait(atadev, 0) < 0) {
1031 ata_prtdev(atadev, "timeout sending command=%02x s=%02x e=%02x\n",
1032 command, atadev->channel->status, atadev->channel->error);
1036 /* only use 48bit addressing if needed because of the overhead */
1037 if ((lba > 268435455 || count > 256) && atadev->param &&
1038 atadev->param->support.address48) {
1039 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, (feature>>8) & 0xff);
1040 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, feature);
1041 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, (count>>8) & 0xff);
1042 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, count & 0xff);
1043 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, (lba>>24) & 0xff);
1044 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, lba & 0xff);
1045 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>32) & 0xff);
1046 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>8) & 0xff);
1047 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>40) & 0xff);
1048 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>16) & 0xff);
1049 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_LBA | atadev->unit);
1051 /* translate command into 48bit version */
1054 command = ATA_C_READ48; break;
1055 case ATA_C_READ_MUL:
1056 command = ATA_C_READ_MUL48; break;
1057 case ATA_C_READ_DMA:
1058 command = ATA_C_READ_DMA48; break;
1059 case ATA_C_READ_DMA_QUEUED:
1060 command = ATA_C_READ_DMA_QUEUED48; break;
1062 command = ATA_C_WRITE48; break;
1063 case ATA_C_WRITE_MUL:
1064 command = ATA_C_WRITE_MUL48; break;
1065 case ATA_C_WRITE_DMA:
1066 command = ATA_C_WRITE_DMA48; break;
1067 case ATA_C_WRITE_DMA_QUEUED:
1068 command = ATA_C_WRITE_DMA_QUEUED48; break;
1069 case ATA_C_FLUSHCACHE:
1070 command = ATA_C_FLUSHCACHE48; break;
1072 ata_prtdev(atadev, "can't translate cmd to 48bit version\n");
1077 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, feature);
1078 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, count);
1079 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, lba & 0xff);
1080 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>8) & 0xff);
1081 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>16) & 0xff);
1082 if (atadev->flags & ATA_D_USE_CHS)
1083 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE,
1084 ATA_D_IBM | atadev->unit | ((lba>>24) & 0xf));
1086 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE,
1087 ATA_D_IBM | ATA_D_LBA | atadev->unit | ((lba>>24) &0xf));
1090 switch (flags & ATA_WAIT_MASK) {
1092 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1094 /* enable interrupt */
1095 if (atadev->channel->flags & ATA_QUEUED)
1096 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
1100 atadev->channel->active |= ATA_WAIT_INTR;
1101 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1103 /* enable interrupt */
1104 if (atadev->channel->flags & ATA_QUEUED)
1105 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
1107 if (tsleep((caddr_t)atadev->channel, PRIBIO, "atacmd", 10 * hz)) {
1108 ata_prtdev(atadev, "timeout waiting for interrupt\n");
1109 atadev->channel->active &= ~ATA_WAIT_INTR;
1114 case ATA_WAIT_READY:
1115 atadev->channel->active |= ATA_WAIT_READY;
1116 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1117 if (ata_wait(atadev, ATA_S_READY) < 0) {
1118 ata_prtdev(atadev, "timeout waiting for cmd=%02x s=%02x e=%02x\n",
1119 command, atadev->channel->status,atadev->channel->error);
1122 atadev->channel->active &= ~ATA_WAIT_READY;
1129 ata_enclosure_start(struct ata_device *atadev)
1131 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1133 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1135 ATA_INB(atadev->channel->r_io, ATA_CMD);
1137 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1139 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1141 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1143 ATA_INB(atadev->channel->r_io, ATA_COUNT);
1145 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1150 ata_enclosure_end(struct ata_device *atadev)
1152 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1157 ata_enclosure_chip_start(struct ata_device *atadev)
1159 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0b);
1160 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0a);
1162 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08);
1166 ata_enclosure_chip_end(struct ata_device *atadev)
1168 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08);
1170 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0a);
1172 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0b);
1177 ata_enclosure_chip_rdbit(struct ata_device *atadev)
1181 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0);
1183 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x02);
1185 val = ATA_INB(atadev->channel->r_io, ATA_SECTOR) & 0x01;
1191 ata_enclosure_chip_wrbit(struct ata_device *atadev, u_int8_t data)
1193 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08 | (data & 0x01));
1195 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08 | 0x02 | (data & 0x01));
1200 ata_enclosure_chip_rw(struct ata_device *atadev, int rw, u_int8_t val)
1205 for (i = 0; i < 8; i++)
1206 ata_enclosure_chip_wrbit(atadev, (val & (0x80 >> i)) ? 1 : 0);
1209 for (i = 0; i < 8; i++)
1210 val = (val << 1) | ata_enclosure_chip_rdbit(atadev);
1212 ata_enclosure_chip_wrbit(atadev, 0);
1217 ata_enclosure_sensor(struct ata_device *atadev,
1218 int rw, u_int8_t idx, u_int8_t data)
1220 ata_enclosure_start(atadev);
1221 ata_enclosure_chip_start(atadev);
1222 ata_enclosure_chip_rw(atadev, 1, 0x5a);
1223 ata_enclosure_chip_rw(atadev, 1, idx);
1225 ata_enclosure_chip_rw(atadev, 1, data);
1228 ata_enclosure_chip_end(atadev);
1229 ata_enclosure_chip_start(atadev);
1230 ata_enclosure_chip_rw(atadev, 1, 0x5b);
1231 data = ata_enclosure_chip_rw(atadev, 0, 0);
1233 ata_enclosure_chip_end(atadev);
1234 ata_enclosure_end(atadev);
1239 ata_enclosure_status(struct ata_device *atadev,
1240 int *fan, int *temp, int *v05, int *v12)
1242 u_int8_t id1, id2, cnt, div;
1245 if (atadev->flags & ATA_D_ENC_PRESENT) {
1246 ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL);
1247 ata_enclosure_sensor(atadev, 1, 0x4e, 0);
1248 id1 = ata_enclosure_sensor(atadev, 0, 0x4f, 0);
1249 ata_enclosure_sensor(atadev, 1, 0x4e, 0x80);
1250 id2 = ata_enclosure_sensor(atadev, 0, 0x4f, 0);
1251 if (id1 == 0xa3 && id2 == 0x5c) {
1252 div = 1 << (((ata_enclosure_sensor(atadev, 0, 0x5d, 0)&0x20)>>3)+
1253 ((ata_enclosure_sensor(atadev, 0, 0x47, 0)&0x30)>>4)+1);
1254 cnt = ata_enclosure_sensor(atadev, 0, 0x28, 0);
1258 *fan = 1350000 / cnt / div;
1259 ata_enclosure_sensor(atadev, 1, 0x4e, 0x01);
1260 *temp = (ata_enclosure_sensor(atadev, 0, 0x50, 0) * 10) +
1261 (ata_enclosure_sensor(atadev, 0, 0x50, 0) & 0x80 ? 5 : 0);
1262 *v05 = ata_enclosure_sensor(atadev, 0, 0x23, 0) * 27;
1263 *v12 = ata_enclosure_sensor(atadev, 0, 0x24, 0) * 61;
1266 ATA_UNLOCK_CH(atadev->channel);
1272 ata_enclosure_print(struct ata_device *atadev)
1275 int fan, temp, v05, v12;
1277 ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL);
1278 ata_enclosure_start(atadev);
1279 id = ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1281 st = ATA_INB(atadev->channel->r_io, ATA_COUNT);
1283 ata_enclosure_end(atadev);
1284 ATA_UNLOCK_CH(atadev->channel);
1286 switch (id & 0x93) {
1288 ata_prtdev(atadev, "Universal enclosure");
1291 ata_prtdev(atadev, "FastSwap enclosure");
1295 ata_prtdev(atadev, "SuperSwap enclosure");
1298 atadev->flags &= ~ATA_D_ENC_PRESENT;
1301 atadev->flags |= ATA_D_ENC_PRESENT;
1303 if (ata_enclosure_status(atadev, &fan, &temp, &v05, &v12))
1304 printf(" detected\n");
1306 printf(" [FAN:%drpm TEMP:%d.%01dC %d.%03dV %d.%03dV]\n",
1307 fan, temp/10, temp%10, v05/1000, v05%1000, v12/1000, v12%1000);
1311 ata_enclosure_leds(struct ata_device *atadev, u_int8_t color)
1313 if (atadev->flags & ATA_D_ENC_PRESENT) {
1316 ata_enclosure_start(atadev);
1317 reg = ATA_INB(atadev->channel->r_io, ATA_COUNT);
1319 ATA_OUTB(atadev->channel->r_io, ATA_COUNT,
1320 (color & ATA_LED_MASK) | (reg & ~ATA_LED_MASK));
1322 ata_enclosure_end(atadev);
1327 ata_change_mode(struct ata_device *atadev, int mode)
1329 int umode, wmode, pmode;
1331 umode = ata_umode(atadev->param);
1332 wmode = ata_wmode(atadev->param);
1333 pmode = ata_pmode(atadev->param);
1335 switch (mode & ATA_DMA_MASK) {
1337 if ((mode & ATA_MODE_MASK) < umode)
1338 umode = mode & ATA_MODE_MASK;
1341 if ((mode & ATA_MODE_MASK) < wmode)
1342 wmode = mode & ATA_MODE_MASK;
1346 if (((mode & ATA_MODE_MASK) - ATA_PIO0) < pmode)
1347 pmode = (mode & ATA_MODE_MASK) - ATA_PIO0;
1352 ATA_SLEEPLOCK_CH(atadev->channel, ATA_ACTIVE);
1353 ata_dmainit(atadev->channel, atadev->unit, pmode, wmode, umode);
1354 ATA_UNLOCK_CH(atadev->channel);
1355 ata_start(atadev->channel); /* XXX SOS */
1359 ata_printf(struct ata_channel *ch, int device, const char * fmt, ...)
1365 ret = printf("ata%d: ", device_get_unit(ch->dev));
1367 if (ch->device[ATA_DEV(device)].name)
1368 ret = printf("%s: ", ch->device[ATA_DEV(device)].name);
1370 ret = printf("ata%d-%s: ", device_get_unit(ch->dev),
1371 (device == ATA_MASTER) ? "master" : "slave");
1374 ret += vprintf(fmt, ap);
1380 ata_prtdev(struct ata_device *atadev, const char * fmt, ...)
1386 ret = printf("%s: ", atadev->name);
1388 ret = printf("ata%d-%s: ", device_get_unit(atadev->channel->dev),
1389 (atadev->unit == ATA_MASTER) ? "master" : "slave");
1391 ret += vprintf(fmt, ap);
1397 ata_set_name(struct ata_device *atadev, char *name, int lun)
1399 atadev->name = malloc(strlen(name) + 4, M_ATA, M_NOWAIT);
1401 sprintf(atadev->name, "%s%d", name, lun);
1405 ata_free_name(struct ata_device *atadev)
1408 free(atadev->name, M_ATA);
1409 atadev->name = NULL;
1413 ata_get_lun(u_int32_t *map)
1415 int lun = ffs(~*map) - 1;
1422 ata_test_lun(u_int32_t *map, int lun)
1424 return (*map & (1 << lun));
1428 ata_free_lun(u_int32_t *map, int lun)
1430 *map &= ~(1 << lun);
1434 ata_mode2str(int mode)
1437 case ATA_PIO: return "BIOSPIO";
1438 case ATA_PIO0: return "PIO0";
1439 case ATA_PIO1: return "PIO1";
1440 case ATA_PIO2: return "PIO2";
1441 case ATA_PIO3: return "PIO3";
1442 case ATA_PIO4: return "PIO4";
1443 case ATA_DMA: return "BIOSDMA";
1444 case ATA_WDMA2: return "WDMA2";
1445 case ATA_UDMA2: return "UDMA33";
1446 case ATA_UDMA4: return "UDMA66";
1447 case ATA_UDMA5: return "UDMA100";
1448 case ATA_UDMA6: return "UDMA133";
1449 default: return "???";
1454 ata_pmode(struct ata_params *ap)
1456 if (ap->atavalid & ATA_FLAG_64_70) {
1457 if (ap->apiomodes & 2)
1459 if (ap->apiomodes & 1)
1462 if (ap->retired_piomode == 2)
1464 if (ap->retired_piomode == 1)
1466 if (ap->retired_piomode == 0)
1472 ata_wmode(struct ata_params *ap)
1474 if (ap->mwdmamodes & 0x04)
1476 if (ap->mwdmamodes & 0x02)
1478 if (ap->mwdmamodes & 0x01)
1484 ata_umode(struct ata_params *ap)
1486 if (ap->atavalid & ATA_FLAG_88) {
1487 if (ap->udmamodes & 0x40)
1489 if (ap->udmamodes & 0x20)
1491 if (ap->udmamodes & 0x10)
1493 if (ap->udmamodes & 0x08)
1495 if (ap->udmamodes & 0x04)
1497 if (ap->udmamodes & 0x02)
1499 if (ap->udmamodes & 0x01)
1506 bswap(int8_t *buf, int len)
1508 u_int16_t *ptr = (u_int16_t*)(buf + len);
1510 while (--ptr >= (u_int16_t*)buf)
1515 btrim(int8_t *buf, int len)
1519 for (ptr = buf; ptr < buf+len; ++ptr)
1522 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
1527 bpack(int8_t *src, int8_t *dst, int len)
1531 for (i = j = blank = 0 ; i < len; i++) {
1532 if (blank && src[i] == ' ') continue;
1533 if (blank && src[i] != ' ') {
1538 if (src[i] == ' ') {
1552 /* register controlling device */
1553 make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "ata");
1555 /* register boot attach to be run when interrupts are enabled */
1556 if (!(ata_delayed_attach = (struct intr_config_hook *)
1557 malloc(sizeof(struct intr_config_hook),
1558 M_TEMP, M_NOWAIT | M_ZERO))) {
1559 printf("ata: malloc of delayed attach hook failed\n");
1563 ata_delayed_attach->ich_func = (void*)ata_boot_attach;
1564 if (config_intrhook_establish(ata_delayed_attach) != 0) {
1565 printf("ata: config_intrhook_establish failed\n");
1566 free(ata_delayed_attach, M_TEMP);
1569 SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL)