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.8 2003/11/09 02:22:34 dillon Exp $
34 #include "use_atadisk.h"
35 #include "use_atapicd.h"
36 #include "use_atapifd.h"
37 #include "use_atapist.h"
38 #include "use_atapicam.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
42 #include <sys/kernel.h>
45 #include <sys/module.h>
48 #include <sys/malloc.h>
49 #include <sys/devicestat.h>
50 #include <sys/sysctl.h>
51 #include <machine/stdarg.h>
52 #include <machine/resource.h>
53 #include <machine/bus.h>
54 #include <machine/clock.h>
57 #include <machine/md_var.h>
62 #include "atapi-all.h"
64 /* device structures */
65 static d_ioctl_t ataioctl;
66 static struct cdevsw ata_cdevsw = {
74 /* close */ nullclose,
80 /* strategy */ nostrategy,
86 static void ata_boot_attach(void);
87 static void ata_intr(void *);
88 static int ata_getparam(struct ata_device *, u_int8_t);
89 static int ata_service(struct ata_channel *);
90 static void bswap(int8_t *, int);
91 static void btrim(int8_t *, int);
92 static void bpack(int8_t *, int8_t *, int);
93 static void ata_change_mode(struct ata_device *, int);
94 static u_int8_t ata_enclosure_sensor(struct ata_device *, int, u_int8_t, u_int8_t);
95 static int ata_enclosure_status(struct ata_device *, int *, int *, int *, int *);
98 SYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters");
101 devclass_t ata_devclass;
104 static struct intr_config_hook *ata_delayed_attach = NULL;
105 static MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer");
108 #define DEV_ATAPIALL NATAPICD > 0 || NATAPIFD > 0 || \
109 NATAPIST > 0 || NATAPICAM > 0
112 ata_probe(device_t dev)
114 struct ata_channel *ch;
117 if (!dev || !(ch = device_get_softc(dev)))
120 if (ch->r_io || ch->r_altio || ch->r_irq)
123 /* initialize the softc basics */
124 ch->active = ATA_IDLE;
127 rid = ATA_IOADDR_RID;
128 ch->r_io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
129 ATA_IOSIZE, RF_ACTIVE);
133 rid = ATA_ALTADDR_RID;
134 ch->r_altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
135 ATA_ALTIOSIZE, RF_ACTIVE);
139 rid = ATA_BMADDR_RID;
140 ch->r_bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
141 ATA_BMIOSIZE, RF_ACTIVE);
143 ata_printf(ch, -1, "iobase=0x%04x altiobase=0x%04x bmaddr=0x%04x\n",
144 (int)rman_get_start(ch->r_io),
145 (int)rman_get_start(ch->r_altio),
146 (ch->r_bmio) ? (int)rman_get_start(ch->r_bmio) : 0);
150 ch->device[MASTER].channel = ch;
151 ch->device[MASTER].unit = ATA_MASTER;
152 ch->device[MASTER].mode = ATA_PIO;
153 ch->device[SLAVE].channel = ch;
154 ch->device[SLAVE].unit = ATA_SLAVE;
155 ch->device[SLAVE].mode = ATA_PIO;
156 TAILQ_INIT(&ch->ata_queue);
157 TAILQ_INIT(&ch->atapi_queue);
162 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ch->r_io);
164 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ch->r_altio);
166 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, ch->r_bmio);
168 ata_printf(ch, -1, "probe allocation failed\n");
173 ata_attach(device_t dev)
175 struct ata_channel *ch;
178 if (!dev || !(ch = device_get_softc(dev)))
182 ch->r_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
183 RF_SHAREABLE | RF_ACTIVE);
185 ata_printf(ch, -1, "unable to allocate interrupt\n");
188 if ((error = bus_setup_intr(dev, ch->r_irq, INTR_TYPE_BIO,
189 ata_intr, ch, &ch->ih))) {
190 ata_printf(ch, -1, "unable to setup interrupt\n");
195 * do not attach devices if we are in early boot, this is done later
196 * when interrupts are enabled by a hook into the boot process.
197 * otherwise attach what the probe has found in ch->devices.
199 if (!ata_delayed_attach) {
202 if (ch->devices & ATA_ATA_SLAVE)
203 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
204 ch->devices &= ~ATA_ATA_SLAVE;
205 if (ch->devices & ATA_ATAPI_SLAVE)
206 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
207 ch->devices &= ~ATA_ATAPI_SLAVE;
208 if (ch->devices & ATA_ATA_MASTER)
209 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
210 ch->devices &= ~ATA_ATA_MASTER;
211 if (ch->devices & ATA_ATAPI_MASTER)
212 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
213 ch->devices &= ~ATA_ATAPI_MASTER;
215 if (ch->devices & ATA_ATA_MASTER)
216 ad_attach(&ch->device[MASTER]);
217 if (ch->devices & ATA_ATA_SLAVE)
218 ad_attach(&ch->device[SLAVE]);
221 if (ch->devices & ATA_ATAPI_MASTER)
222 atapi_attach(&ch->device[MASTER]);
223 if (ch->devices & ATA_ATAPI_SLAVE)
224 atapi_attach(&ch->device[SLAVE]);
227 atapi_cam_attach_bus(ch);
235 ata_detach(device_t dev)
237 struct ata_channel *ch;
240 if (!dev || !(ch = device_get_softc(dev)) ||
241 !ch->r_io || !ch->r_altio || !ch->r_irq)
244 /* make sure channel is not busy */
245 ATA_SLEEPLOCK_CH(ch, ATA_CONTROL);
249 if (ch->devices & ATA_ATA_MASTER && ch->device[MASTER].driver)
250 ad_detach(&ch->device[MASTER], 1);
251 if (ch->devices & ATA_ATA_SLAVE && ch->device[SLAVE].driver)
252 ad_detach(&ch->device[SLAVE], 1);
255 if (ch->devices & ATA_ATAPI_MASTER && ch->device[MASTER].driver)
256 atapi_detach(&ch->device[MASTER]);
257 if (ch->devices & ATA_ATAPI_SLAVE && ch->device[SLAVE].driver)
258 atapi_detach(&ch->device[SLAVE]);
261 atapi_cam_detach_bus(ch);
265 if (ch->device[MASTER].param) {
266 free(ch->device[MASTER].param, M_ATA);
267 ch->device[MASTER].param = NULL;
269 if (ch->device[SLAVE].param) {
270 free(ch->device[SLAVE].param, M_ATA);
271 ch->device[SLAVE].param = NULL;
273 ch->device[MASTER].driver = NULL;
274 ch->device[SLAVE].driver = NULL;
275 ch->device[MASTER].mode = ATA_PIO;
276 ch->device[SLAVE].mode = ATA_PIO;
279 bus_teardown_intr(dev, ch->r_irq, ch->ih);
280 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
282 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, ch->r_bmio);
283 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ch->r_altio);
284 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ch->r_io);
294 ata_resume(device_t dev)
296 return ata_reinit(device_get_softc(dev));
300 ataioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, struct thread *td)
302 struct ata_cmd *iocmd = (struct ata_cmd *)addr;
303 struct ata_channel *ch;
304 device_t device = devclass_get_device(ata_devclass, iocmd->channel);
310 if (iocmd->channel < -1 || iocmd->device < -1 || iocmd->device > SLAVE)
313 switch (iocmd->cmd) {
315 /* should enable channel HW on controller that can SOS XXX */
316 error = ata_probe(device);
318 error = ata_attach(device);
322 error = ata_detach(device);
323 /* should disable channel HW on controller that can SOS XXX */
327 if (!device || !(ch = device_get_softc(device)))
329 ATA_SLEEPLOCK_CH(ch, ATA_ACTIVE);
330 if ((error = ata_reinit(ch)))
335 if (!device || !(ch = device_get_softc(device)))
338 if ((iocmd->device == MASTER || iocmd->device == -1) &&
339 ch->device[MASTER].driver)
340 iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode;
342 iocmd->u.mode.mode[MASTER] = -1;
344 if ((iocmd->device == SLAVE || iocmd->device == -1) &&
345 ch->device[SLAVE].param)
346 iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode;
348 iocmd->u.mode.mode[SLAVE] = -1;
352 if (!device || !(ch = device_get_softc(device)))
355 if ((iocmd->device == MASTER || iocmd->device == -1) &&
356 iocmd->u.mode.mode[MASTER] >= 0 && ch->device[MASTER].param) {
357 ata_change_mode(&ch->device[MASTER],iocmd->u.mode.mode[MASTER]);
358 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 iocmd->u.mode.mode[SLAVE] >= 0 && ch->device[SLAVE].param) {
365 ata_change_mode(&ch->device[SLAVE], iocmd->u.mode.mode[SLAVE]);
366 iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode;
369 iocmd->u.mode.mode[SLAVE] = -1;
373 if (!device || !(ch = device_get_softc(device)))
376 iocmd->u.param.type[MASTER] =
377 ch->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER);
378 iocmd->u.param.type[SLAVE] =
379 ch->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE);
381 if (ch->device[MASTER].name)
382 strcpy(iocmd->u.param.name[MASTER], ch->device[MASTER].name);
383 if (ch->device[SLAVE].name)
384 strcpy(iocmd->u.param.name[SLAVE], ch->device[SLAVE].name);
386 if (ch->device[MASTER].param)
387 bcopy(ch->device[MASTER].param, &iocmd->u.param.params[MASTER],
388 sizeof(struct ata_params));
389 if (ch->device[SLAVE].param)
390 bcopy(ch->device[SLAVE].param, &iocmd->u.param.params[SLAVE],
391 sizeof(struct ata_params));
395 struct ata_device *atadev;
397 if (!device || !(ch = device_get_softc(device)))
400 if (iocmd->device == SLAVE)
401 atadev = &ch->device[SLAVE];
403 atadev = &ch->device[MASTER];
405 return ata_enclosure_status(atadev,
406 &iocmd->u.enclosure.fan,
407 &iocmd->u.enclosure.temp,
408 &iocmd->u.enclosure.v05,
409 &iocmd->u.enclosure.v12);
414 return ata_raid_rebuild(iocmd->channel);
417 return ata_raid_create(&iocmd->u.raid_setup);
420 return ata_raid_delete(iocmd->channel);
423 return ata_raid_status(iocmd->channel, &iocmd->u.raid_status);
427 struct ata_device *atadev;
430 if (!device || !(ch = device_get_softc(device)))
433 if (!(atadev = &ch->device[iocmd->device]) ||
434 !(ch->devices & (iocmd->device == MASTER ?
435 ATA_ATAPI_MASTER : ATA_ATAPI_SLAVE)))
438 if (!(buf = malloc(iocmd->u.atapi.count, M_ATA, M_NOWAIT)))
441 if (iocmd->u.atapi.flags & ATAPI_CMD_WRITE) {
442 error = copyin(iocmd->u.atapi.data, buf, iocmd->u.atapi.count);
446 error = atapi_queue_cmd(atadev, iocmd->u.atapi.ccb,
447 buf, iocmd->u.atapi.count,
448 (iocmd->u.atapi.flags == ATAPI_CMD_READ ?
449 ATPR_F_READ : 0) | ATPR_F_QUIET,
450 iocmd->u.atapi.timeout, NULL, NULL);
452 iocmd->u.atapi.error = error;
453 bcopy(&atadev->result, iocmd->u.atapi.sense_data,
454 sizeof(struct atapi_reqsense));
457 else if (iocmd->u.atapi.flags & ATAPI_CMD_READ)
458 error = copyout(buf, iocmd->u.atapi.data, iocmd->u.atapi.count);
471 ata_getparam(struct ata_device *atadev, u_int8_t command)
473 struct ata_params *ata_parm;
476 if (!(ata_parm = malloc(sizeof(struct ata_params), M_ATA, M_NOWAIT))) {
477 ata_prtdev(atadev, "malloc for identify data failed\n");
481 /* apparently some devices needs this repeated */
483 if (ata_command(atadev, command, 0, 0, 0, ATA_IMMEDIATE)) {
484 ata_prtdev(atadev, "%s identify failed\n",
485 command == ATA_C_ATAPI_IDENTIFY ? "ATAPI" : "ATA");
486 free(ata_parm, M_ATA);
490 ata_prtdev(atadev, "%s identify retries exceeded\n",
491 command == ATA_C_ATAPI_IDENTIFY ? "ATAPI" : "ATA");
492 free(ata_parm, M_ATA);
495 } while (ata_wait(atadev, ((command == ATA_C_ATAPI_IDENTIFY) ?
496 ATA_S_DRQ : (ATA_S_READY|ATA_S_DSC|ATA_S_DRQ))));
497 ATA_INSW(atadev->channel->r_io, ATA_DATA, (int16_t *)ata_parm,
498 sizeof(struct ata_params)/sizeof(int16_t));
500 if (command == ATA_C_ATA_IDENTIFY ||
501 !((ata_parm->model[0] == 'N' && ata_parm->model[1] == 'E') ||
502 (ata_parm->model[0] == 'F' && ata_parm->model[1] == 'X') ||
503 (ata_parm->model[0] == 'P' && ata_parm->model[1] == 'i')))
504 bswap(ata_parm->model, sizeof(ata_parm->model));
505 btrim(ata_parm->model, sizeof(ata_parm->model));
506 bpack(ata_parm->model, ata_parm->model, sizeof(ata_parm->model));
507 bswap(ata_parm->revision, sizeof(ata_parm->revision));
508 btrim(ata_parm->revision, sizeof(ata_parm->revision));
509 bpack(ata_parm->revision, ata_parm->revision, sizeof(ata_parm->revision));
510 bswap(ata_parm->serial, sizeof(ata_parm->serial));
511 btrim(ata_parm->serial, sizeof(ata_parm->serial));
512 bpack(ata_parm->serial, ata_parm->serial, sizeof(ata_parm->serial));
513 atadev->param = ata_parm;
518 ata_boot_attach(void)
520 struct ata_channel *ch;
523 if (ata_delayed_attach) {
524 config_intrhook_disestablish(ata_delayed_attach);
525 free(ata_delayed_attach, M_TEMP);
526 ata_delayed_attach = NULL;
531 * run through all ata devices and look for real ATA & ATAPI devices
532 * using the hints we found in the early probe, this avoids some of
533 * the delays probing of non-exsistent devices can cause.
535 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
536 if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
538 if (ch->devices & ATA_ATA_SLAVE)
539 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
540 ch->devices &= ~ATA_ATA_SLAVE;
541 if (ch->devices & ATA_ATAPI_SLAVE)
542 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
543 ch->devices &= ~ATA_ATAPI_SLAVE;
544 if (ch->devices & ATA_ATA_MASTER)
545 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
546 ch->devices &= ~ATA_ATA_MASTER;
547 if (ch->devices & ATA_ATAPI_MASTER)
548 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
549 ch->devices &= ~ATA_ATAPI_MASTER;
553 /* now we know whats there, do the real attach, first the ATA disks */
554 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
555 if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
557 if (ch->devices & ATA_ATA_MASTER)
558 ad_attach(&ch->device[MASTER]);
559 if (ch->devices & ATA_ATA_SLAVE)
560 ad_attach(&ch->device[SLAVE]);
565 /* then the atapi devices */
566 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
567 if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
569 if (ch->devices & ATA_ATAPI_MASTER)
570 atapi_attach(&ch->device[MASTER]);
571 if (ch->devices & ATA_ATAPI_SLAVE)
572 atapi_attach(&ch->device[SLAVE]);
574 atapi_cam_attach_bus(ch);
584 struct ata_channel *ch = (struct ata_channel *)data;
586 * on PCI systems we might share an interrupt line with another
587 * device or our twin ATA channel, so call ch->intr_func to figure
588 * out if it is really an interrupt we should process here
590 if (ch->intr_func && ch->intr_func(ch))
593 /* if drive is busy it didn't interrupt */
594 if (ATA_INB(ch->r_altio, ATA_ALTSTAT) & ATA_S_BUSY) {
596 if (!(ATA_INB(ch->r_altio, ATA_ALTSTAT) & ATA_S_DRQ))
600 /* clear interrupt and get status */
601 ch->status = ATA_INB(ch->r_io, ATA_STATUS);
603 if (ch->status & ATA_S_ERROR)
604 ch->error = ATA_INB(ch->r_io, ATA_ERROR);
606 /* find & call the responsible driver to process this interrupt */
607 switch (ch->active) {
610 if (!ch->running || ad_interrupt(ch->running) == ATA_OP_CONTINUES)
615 case ATA_ACTIVE_ATAPI:
616 if (!ch->running || atapi_interrupt(ch->running) == ATA_OP_CONTINUES)
621 case ATA_WAIT_INTR | ATA_CONTROL:
626 case ATA_WAIT_READY | ATA_CONTROL:
630 if (ch->flags & ATA_QUEUED) {
631 ch->active = ATA_ACTIVE;
632 if (ata_service(ch) == ATA_OP_CONTINUES)
640 static int intr_count = 0;
642 if (intr_count++ < 10)
643 ata_printf(ch, -1, "unwanted interrupt #%d active=%02x s=%02x\n",
644 intr_count, ch->active, ch->status);
649 ch->active &= ATA_CONTROL;
650 if (ch->active & ATA_CONTROL)
658 ata_start(struct ata_channel *ch)
661 struct ad_request *ad_request;
664 struct atapi_request *atapi_request;
668 if (!ATA_LOCK_CH(ch, ATA_ACTIVE))
673 /* find & call the responsible driver if anything on the ATA queue */
674 if (TAILQ_EMPTY(&ch->ata_queue)) {
675 if (ch->devices & (ATA_ATA_MASTER) && ch->device[MASTER].driver)
676 ad_start(&ch->device[MASTER]);
677 if (ch->devices & (ATA_ATA_SLAVE) && ch->device[SLAVE].driver)
678 ad_start(&ch->device[SLAVE]);
680 if ((ad_request = TAILQ_FIRST(&ch->ata_queue))) {
681 TAILQ_REMOVE(&ch->ata_queue, ad_request, chain);
682 ch->active = ATA_ACTIVE_ATA;
683 ch->running = ad_request;
684 if (ad_transfer(ad_request) == ATA_OP_CONTINUES) {
692 /* find & call the responsible driver if anything on the ATAPI queue */
693 if (TAILQ_EMPTY(&ch->atapi_queue)) {
694 if (ch->devices & (ATA_ATAPI_MASTER) && ch->device[MASTER].driver)
695 atapi_start(&ch->device[MASTER]);
696 if (ch->devices & (ATA_ATAPI_SLAVE) && ch->device[SLAVE].driver)
697 atapi_start(&ch->device[SLAVE]);
699 if ((atapi_request = TAILQ_FIRST(&ch->atapi_queue))) {
700 TAILQ_REMOVE(&ch->atapi_queue, atapi_request, chain);
701 ch->active = ATA_ACTIVE_ATAPI;
702 ch->running = atapi_request;
703 if (atapi_transfer(atapi_request) == ATA_OP_CONTINUES) {
714 ata_reset(struct ata_channel *ch)
716 u_int8_t lsb, msb, ostat0, ostat1;
717 u_int8_t stat0 = 0, stat1 = 0;
718 int mask = 0, timeout;
720 /* do we have any signs of ATA/ATAPI HW being present ? */
721 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
723 ostat0 = ATA_INB(ch->r_io, ATA_STATUS);
724 if ((ostat0 & 0xf8) != 0xf8 && ostat0 != 0xa5) {
728 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
730 ostat1 = ATA_INB(ch->r_io, ATA_STATUS);
731 if ((ostat1 & 0xf8) != 0xf8 && ostat1 != 0xa5) {
740 /* in some setups we dont want to test for a slave */
741 if (ch->flags & ATA_NO_SLAVE) {
747 ata_printf(ch, -1, "mask=%02x ostat0=%02x ostat2=%02x\n",
748 mask, ostat0, ostat1);
751 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
753 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_RESET);
755 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_IDS);
757 ATA_INB(ch->r_io, ATA_ERROR);
759 /* wait for BUSY to go inactive */
760 for (timeout = 0; timeout < 310000; timeout++) {
761 if (stat0 & ATA_S_BUSY) {
762 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
765 /* check for ATAPI signature while its still there */
766 lsb = ATA_INB(ch->r_io, ATA_CYL_LSB);
767 msb = ATA_INB(ch->r_io, ATA_CYL_MSB);
768 stat0 = ATA_INB(ch->r_io, ATA_STATUS);
769 if (!(stat0 & ATA_S_BUSY)) {
771 ata_printf(ch, ATA_MASTER, "ATAPI %02x %02x\n", lsb, msb);
772 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB)
773 ch->devices |= ATA_ATAPI_MASTER;
776 if (stat1 & ATA_S_BUSY) {
777 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
780 /* check for ATAPI signature while its still there */
781 lsb = ATA_INB(ch->r_io, ATA_CYL_LSB);
782 msb = ATA_INB(ch->r_io, ATA_CYL_MSB);
783 stat1 = ATA_INB(ch->r_io, ATA_STATUS);
784 if (!(stat1 & ATA_S_BUSY)) {
786 ata_printf(ch, ATA_SLAVE, "ATAPI %02x %02x\n", lsb, msb);
787 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB)
788 ch->devices |= ATA_ATAPI_SLAVE;
791 if (mask == 0x01) /* wait for master only */
792 if (!(stat0 & ATA_S_BUSY))
794 if (mask == 0x02) /* wait for slave only */
795 if (!(stat1 & ATA_S_BUSY))
797 if (mask == 0x03) /* wait for both master & slave */
798 if (!(stat0 & ATA_S_BUSY) && !(stat1 & ATA_S_BUSY))
803 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
805 if (stat0 & ATA_S_BUSY)
807 if (stat1 & ATA_S_BUSY)
810 ata_printf(ch, -1, "mask=%02x stat0=%02x stat1=%02x\n",
815 if (mask & 0x01 && ostat0 != 0x00 && !(ch->devices & ATA_ATAPI_MASTER)) {
816 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
818 ATA_OUTB(ch->r_io, ATA_ERROR, 0x58);
819 ATA_OUTB(ch->r_io, ATA_CYL_LSB, 0xa5);
820 lsb = ATA_INB(ch->r_io, ATA_ERROR);
821 msb = ATA_INB(ch->r_io, ATA_CYL_LSB);
823 ata_printf(ch, ATA_MASTER, "ATA %02x %02x\n", lsb, msb);
824 if (lsb != 0x58 && msb == 0xa5)
825 ch->devices |= ATA_ATA_MASTER;
827 if (mask & 0x02 && ostat1 != 0x00 && !(ch->devices & ATA_ATAPI_SLAVE)) {
828 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
830 ATA_OUTB(ch->r_io, ATA_ERROR, 0x58);
831 ATA_OUTB(ch->r_io, ATA_CYL_LSB, 0xa5);
832 lsb = ATA_INB(ch->r_io, ATA_ERROR);
833 msb = ATA_INB(ch->r_io, ATA_CYL_LSB);
835 ata_printf(ch, ATA_SLAVE, "ATA %02x %02x\n", lsb, msb);
836 if (lsb != 0x58 && msb == 0xa5)
837 ch->devices |= ATA_ATA_SLAVE;
840 ata_printf(ch, -1, "devices=%02x\n", ch->devices);
844 ata_reinit(struct ata_channel *ch)
846 int devices, misdev, newdev;
848 if (!ch->r_io || !ch->r_altio || !ch->r_irq)
851 ATA_FORCELOCK_CH(ch, ATA_CONTROL);
853 devices = ch->devices;
854 ata_printf(ch, -1, "resetting devices .. ");
857 if ((misdev = devices & ~ch->devices)) {
861 if (misdev & ATA_ATA_MASTER && ch->device[MASTER].driver)
862 ad_detach(&ch->device[MASTER], 0);
863 if (misdev & ATA_ATA_SLAVE && ch->device[SLAVE].driver)
864 ad_detach(&ch->device[SLAVE], 0);
867 if (misdev & ATA_ATAPI_MASTER && ch->device[MASTER].driver)
868 atapi_detach(&ch->device[MASTER]);
869 if (misdev & ATA_ATAPI_SLAVE && ch->device[SLAVE].driver)
870 atapi_detach(&ch->device[SLAVE]);
872 if (misdev & ATA_ATA_MASTER || misdev & ATA_ATAPI_MASTER) {
873 if (ch->device[MASTER].param)
874 free(ch->device[MASTER].param, M_ATA);
875 ch->device[MASTER].param = NULL;
877 if (misdev & ATA_ATA_SLAVE || misdev & ATA_ATAPI_SLAVE) {
878 if (ch->device[SLAVE].param)
879 free(ch->device[SLAVE].param, M_ATA);
880 ch->device[SLAVE].param = NULL;
883 if ((newdev = ~devices & ch->devices)) {
884 if (newdev & ATA_ATA_MASTER)
885 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
886 ch->devices &= ~ATA_ATA_MASTER;
887 if (newdev & ATA_ATA_SLAVE)
888 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
889 ch->devices &= ~ATA_ATA_SLAVE;
890 if (newdev & ATA_ATAPI_MASTER)
891 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
892 ch->devices &= ~ATA_ATAPI_MASTER;
893 if (newdev & ATA_ATAPI_SLAVE)
894 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
895 ch->devices &= ~ATA_ATAPI_SLAVE;
897 newdev = ~devices & ch->devices;
898 if (!misdev && newdev)
901 if (newdev & ATA_ATA_MASTER && !ch->device[MASTER].driver)
902 ad_attach(&ch->device[MASTER]);
903 else if (ch->devices & ATA_ATA_MASTER && ch->device[MASTER].driver) {
904 ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY);
905 ad_reinit(&ch->device[MASTER]);
907 if (newdev & ATA_ATA_SLAVE && !ch->device[SLAVE].driver)
908 ad_attach(&ch->device[SLAVE]);
909 else if (ch->devices & (ATA_ATA_SLAVE) && ch->device[SLAVE].driver) {
910 ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY);
911 ad_reinit(&ch->device[SLAVE]);
915 if (newdev & ATA_ATAPI_MASTER && !ch->device[MASTER].driver)
916 atapi_attach(&ch->device[MASTER]);
917 else if (ch->devices & (ATA_ATAPI_MASTER) && ch->device[MASTER].driver) {
918 ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY);
919 atapi_reinit(&ch->device[MASTER]);
921 if (newdev & ATA_ATAPI_SLAVE && !ch->device[SLAVE].driver)
922 atapi_attach(&ch->device[SLAVE]);
923 else if (ch->devices & (ATA_ATAPI_SLAVE) && ch->device[SLAVE].driver) {
924 ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY);
925 atapi_reinit(&ch->device[SLAVE]);
929 if (ch->devices & (ATA_ATAPI_MASTER | ATA_ATAPI_SLAVE))
930 atapi_cam_reinit_bus(ch);
939 ata_service(struct ata_channel *ch)
941 /* do we have a SERVICE request from the drive ? */
942 if ((ch->status & (ATA_S_SERVICE|ATA_S_ERROR|ATA_S_DRQ)) == ATA_S_SERVICE) {
943 ATA_OUTB(ch->r_bmio, ATA_BMSTAT_PORT,
944 ata_dmastatus(ch) | ATA_BMSTAT_INTERRUPT);
946 if ((ATA_INB(ch->r_io, ATA_DRIVE) & ATA_SLAVE) == ATA_MASTER) {
947 if ((ch->devices & ATA_ATA_MASTER) && ch->device[MASTER].driver)
948 return ad_service((struct ad_softc *)
949 ch->device[MASTER].driver, 0);
952 if ((ch->devices & ATA_ATA_SLAVE) && ch->device[SLAVE].driver)
953 return ad_service((struct ad_softc *)
954 ch->device[SLAVE].driver, 0);
958 return ATA_OP_FINISHED;
962 ata_wait(struct ata_device *atadev, u_int8_t mask)
967 while (timeout < 5000000) { /* timeout 5 secs */
968 atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS);
970 /* if drive fails status, reselect the drive just to be sure */
971 if (atadev->channel->status == 0xff) {
972 ata_prtdev(atadev, "no status, reselecting device\n");
973 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM|atadev->unit);
975 atadev->channel->status = ATA_INB(atadev->channel->r_io,ATA_STATUS);
976 if (atadev->channel->status == 0xff)
981 if (!(atadev->channel->status & ATA_S_BUSY))
984 if (timeout > 1000) {
993 if (atadev->channel->status & ATA_S_ERROR)
994 atadev->channel->error = ATA_INB(atadev->channel->r_io, ATA_ERROR);
995 if (timeout >= 5000000)
998 return (atadev->channel->status & ATA_S_ERROR);
1000 /* Wait 50 msec for bits wanted. */
1003 atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS);
1004 if ((atadev->channel->status & mask) == mask) {
1005 if (atadev->channel->status & ATA_S_ERROR)
1006 atadev->channel->error=ATA_INB(atadev->channel->r_io,ATA_ERROR);
1007 return (atadev->channel->status & ATA_S_ERROR);
1015 ata_command(struct ata_device *atadev, u_int8_t command,
1016 u_int64_t lba, u_int16_t count, u_int8_t feature, int flags)
1020 ata_prtdev(atadev, "ata_command: addr=%04lx, cmd=%02x, "
1021 "lba=%lld, count=%d, feature=%d, flags=%02x\n",
1022 rman_get_start(atadev->channel->r_io),
1023 command, lba, count, feature, flags);
1027 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1029 /* disable interrupt from device */
1030 if (atadev->channel->flags & ATA_QUEUED)
1031 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_4BIT);
1033 /* ready to issue command ? */
1034 if (ata_wait(atadev, 0) < 0) {
1035 ata_prtdev(atadev, "timeout sending command=%02x s=%02x e=%02x\n",
1036 command, atadev->channel->status, atadev->channel->error);
1040 /* only use 48bit addressing if needed because of the overhead */
1041 if ((lba > 268435455 || count > 256) && atadev->param &&
1042 atadev->param->support.address48) {
1043 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, (feature>>8) & 0xff);
1044 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, feature);
1045 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, (count>>8) & 0xff);
1046 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, count & 0xff);
1047 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, (lba>>24) & 0xff);
1048 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, lba & 0xff);
1049 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>32) & 0xff);
1050 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>8) & 0xff);
1051 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>40) & 0xff);
1052 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>16) & 0xff);
1053 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_LBA | atadev->unit);
1055 /* translate command into 48bit version */
1058 command = ATA_C_READ48; break;
1059 case ATA_C_READ_MUL:
1060 command = ATA_C_READ_MUL48; break;
1061 case ATA_C_READ_DMA:
1062 command = ATA_C_READ_DMA48; break;
1063 case ATA_C_READ_DMA_QUEUED:
1064 command = ATA_C_READ_DMA_QUEUED48; break;
1066 command = ATA_C_WRITE48; break;
1067 case ATA_C_WRITE_MUL:
1068 command = ATA_C_WRITE_MUL48; break;
1069 case ATA_C_WRITE_DMA:
1070 command = ATA_C_WRITE_DMA48; break;
1071 case ATA_C_WRITE_DMA_QUEUED:
1072 command = ATA_C_WRITE_DMA_QUEUED48; break;
1073 case ATA_C_FLUSHCACHE:
1074 command = ATA_C_FLUSHCACHE48; break;
1076 ata_prtdev(atadev, "can't translate cmd to 48bit version\n");
1081 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, feature);
1082 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, count);
1083 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, lba & 0xff);
1084 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>8) & 0xff);
1085 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>16) & 0xff);
1086 if (atadev->flags & ATA_D_USE_CHS)
1087 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE,
1088 ATA_D_IBM | atadev->unit | ((lba>>24) & 0xf));
1090 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE,
1091 ATA_D_IBM | ATA_D_LBA | atadev->unit | ((lba>>24) &0xf));
1094 switch (flags & ATA_WAIT_MASK) {
1096 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1098 /* enable interrupt */
1099 if (atadev->channel->flags & ATA_QUEUED)
1100 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
1104 atadev->channel->active |= ATA_WAIT_INTR;
1105 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1107 /* enable interrupt */
1108 if (atadev->channel->flags & ATA_QUEUED)
1109 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
1111 if (tsleep((caddr_t)atadev->channel, 0, "atacmd", 10 * hz)) {
1112 ata_prtdev(atadev, "timeout waiting for interrupt\n");
1113 atadev->channel->active &= ~ATA_WAIT_INTR;
1118 case ATA_WAIT_READY:
1119 atadev->channel->active |= ATA_WAIT_READY;
1120 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1121 if (ata_wait(atadev, ATA_S_READY) < 0) {
1122 ata_prtdev(atadev, "timeout waiting for cmd=%02x s=%02x e=%02x\n",
1123 command, atadev->channel->status,atadev->channel->error);
1126 atadev->channel->active &= ~ATA_WAIT_READY;
1133 ata_enclosure_start(struct ata_device *atadev)
1135 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1137 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1139 ATA_INB(atadev->channel->r_io, ATA_CMD);
1141 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1143 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1145 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1147 ATA_INB(atadev->channel->r_io, ATA_COUNT);
1149 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1154 ata_enclosure_end(struct ata_device *atadev)
1156 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1161 ata_enclosure_chip_start(struct ata_device *atadev)
1163 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0b);
1164 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0a);
1166 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08);
1170 ata_enclosure_chip_end(struct ata_device *atadev)
1172 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08);
1174 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0a);
1176 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0b);
1181 ata_enclosure_chip_rdbit(struct ata_device *atadev)
1185 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0);
1187 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x02);
1189 val = ATA_INB(atadev->channel->r_io, ATA_SECTOR) & 0x01;
1195 ata_enclosure_chip_wrbit(struct ata_device *atadev, u_int8_t data)
1197 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08 | (data & 0x01));
1199 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08 | 0x02 | (data & 0x01));
1204 ata_enclosure_chip_rw(struct ata_device *atadev, int rw, u_int8_t val)
1209 for (i = 0; i < 8; i++)
1210 ata_enclosure_chip_wrbit(atadev, (val & (0x80 >> i)) ? 1 : 0);
1213 for (i = 0; i < 8; i++)
1214 val = (val << 1) | ata_enclosure_chip_rdbit(atadev);
1216 ata_enclosure_chip_wrbit(atadev, 0);
1221 ata_enclosure_sensor(struct ata_device *atadev,
1222 int rw, u_int8_t idx, u_int8_t data)
1224 ata_enclosure_start(atadev);
1225 ata_enclosure_chip_start(atadev);
1226 ata_enclosure_chip_rw(atadev, 1, 0x5a);
1227 ata_enclosure_chip_rw(atadev, 1, idx);
1229 ata_enclosure_chip_rw(atadev, 1, data);
1232 ata_enclosure_chip_end(atadev);
1233 ata_enclosure_chip_start(atadev);
1234 ata_enclosure_chip_rw(atadev, 1, 0x5b);
1235 data = ata_enclosure_chip_rw(atadev, 0, 0);
1237 ata_enclosure_chip_end(atadev);
1238 ata_enclosure_end(atadev);
1243 ata_enclosure_status(struct ata_device *atadev,
1244 int *fan, int *temp, int *v05, int *v12)
1246 u_int8_t id1, id2, cnt, div;
1249 if (atadev->flags & ATA_D_ENC_PRESENT) {
1250 ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL);
1251 ata_enclosure_sensor(atadev, 1, 0x4e, 0);
1252 id1 = ata_enclosure_sensor(atadev, 0, 0x4f, 0);
1253 ata_enclosure_sensor(atadev, 1, 0x4e, 0x80);
1254 id2 = ata_enclosure_sensor(atadev, 0, 0x4f, 0);
1255 if (id1 == 0xa3 && id2 == 0x5c) {
1256 div = 1 << (((ata_enclosure_sensor(atadev, 0, 0x5d, 0)&0x20)>>3)+
1257 ((ata_enclosure_sensor(atadev, 0, 0x47, 0)&0x30)>>4)+1);
1258 cnt = ata_enclosure_sensor(atadev, 0, 0x28, 0);
1262 *fan = 1350000 / cnt / div;
1263 ata_enclosure_sensor(atadev, 1, 0x4e, 0x01);
1264 *temp = (ata_enclosure_sensor(atadev, 0, 0x50, 0) * 10) +
1265 (ata_enclosure_sensor(atadev, 0, 0x50, 0) & 0x80 ? 5 : 0);
1266 *v05 = ata_enclosure_sensor(atadev, 0, 0x23, 0) * 27;
1267 *v12 = ata_enclosure_sensor(atadev, 0, 0x24, 0) * 61;
1270 ATA_UNLOCK_CH(atadev->channel);
1276 ata_enclosure_print(struct ata_device *atadev)
1279 int fan, temp, v05, v12;
1281 ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL);
1282 ata_enclosure_start(atadev);
1283 id = ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1285 st = ATA_INB(atadev->channel->r_io, ATA_COUNT);
1287 ata_enclosure_end(atadev);
1288 ATA_UNLOCK_CH(atadev->channel);
1290 switch (id & 0x93) {
1292 ata_prtdev(atadev, "Universal enclosure");
1295 ata_prtdev(atadev, "FastSwap enclosure");
1299 ata_prtdev(atadev, "SuperSwap enclosure");
1302 atadev->flags &= ~ATA_D_ENC_PRESENT;
1305 atadev->flags |= ATA_D_ENC_PRESENT;
1307 if (ata_enclosure_status(atadev, &fan, &temp, &v05, &v12))
1308 printf(" detected\n");
1310 printf(" [FAN:%drpm TEMP:%d.%01dC %d.%03dV %d.%03dV]\n",
1311 fan, temp/10, temp%10, v05/1000, v05%1000, v12/1000, v12%1000);
1315 ata_enclosure_leds(struct ata_device *atadev, u_int8_t color)
1317 if (atadev->flags & ATA_D_ENC_PRESENT) {
1320 ata_enclosure_start(atadev);
1321 reg = ATA_INB(atadev->channel->r_io, ATA_COUNT);
1323 ATA_OUTB(atadev->channel->r_io, ATA_COUNT,
1324 (color & ATA_LED_MASK) | (reg & ~ATA_LED_MASK));
1326 ata_enclosure_end(atadev);
1331 ata_change_mode(struct ata_device *atadev, int mode)
1333 int umode, wmode, pmode;
1335 umode = ata_umode(atadev->param);
1336 wmode = ata_wmode(atadev->param);
1337 pmode = ata_pmode(atadev->param);
1339 switch (mode & ATA_DMA_MASK) {
1341 if ((mode & ATA_MODE_MASK) < umode)
1342 umode = mode & ATA_MODE_MASK;
1345 if ((mode & ATA_MODE_MASK) < wmode)
1346 wmode = mode & ATA_MODE_MASK;
1350 if (((mode & ATA_MODE_MASK) - ATA_PIO0) < pmode)
1351 pmode = (mode & ATA_MODE_MASK) - ATA_PIO0;
1356 ATA_SLEEPLOCK_CH(atadev->channel, ATA_ACTIVE);
1357 ata_dmainit(atadev->channel, atadev->unit, pmode, wmode, umode);
1358 ATA_UNLOCK_CH(atadev->channel);
1359 ata_start(atadev->channel); /* XXX SOS */
1363 ata_printf(struct ata_channel *ch, int device, const char * fmt, ...)
1369 ret = printf("ata%d: ", device_get_unit(ch->dev));
1371 if (ch->device[ATA_DEV(device)].name)
1372 ret = printf("%s: ", ch->device[ATA_DEV(device)].name);
1374 ret = printf("ata%d-%s: ", device_get_unit(ch->dev),
1375 (device == ATA_MASTER) ? "master" : "slave");
1377 __va_start(ap, fmt);
1378 ret += vprintf(fmt, ap);
1384 ata_prtdev(struct ata_device *atadev, const char * fmt, ...)
1390 ret = printf("%s: ", atadev->name);
1392 ret = printf("ata%d-%s: ", device_get_unit(atadev->channel->dev),
1393 (atadev->unit == ATA_MASTER) ? "master" : "slave");
1394 __va_start(ap, fmt);
1395 ret += vprintf(fmt, ap);
1401 ata_set_name(struct ata_device *atadev, char *name, int lun)
1403 atadev->name = malloc(strlen(name) + 4, M_ATA, M_NOWAIT);
1405 sprintf(atadev->name, "%s%d", name, lun);
1409 ata_free_name(struct ata_device *atadev)
1412 free(atadev->name, M_ATA);
1413 atadev->name = NULL;
1417 ata_get_lun(u_int32_t *map)
1419 int lun = ffs(~*map) - 1;
1426 ata_test_lun(u_int32_t *map, int lun)
1428 return (*map & (1 << lun));
1432 ata_free_lun(u_int32_t *map, int lun)
1434 *map &= ~(1 << lun);
1438 ata_mode2str(int mode)
1441 case ATA_PIO: return "BIOSPIO";
1442 case ATA_PIO0: return "PIO0";
1443 case ATA_PIO1: return "PIO1";
1444 case ATA_PIO2: return "PIO2";
1445 case ATA_PIO3: return "PIO3";
1446 case ATA_PIO4: return "PIO4";
1447 case ATA_DMA: return "BIOSDMA";
1448 case ATA_WDMA2: return "WDMA2";
1449 case ATA_UDMA2: return "UDMA33";
1450 case ATA_UDMA4: return "UDMA66";
1451 case ATA_UDMA5: return "UDMA100";
1452 case ATA_UDMA6: return "UDMA133";
1453 default: return "???";
1458 ata_pmode(struct ata_params *ap)
1460 if (ap->atavalid & ATA_FLAG_64_70) {
1461 if (ap->apiomodes & 2)
1463 if (ap->apiomodes & 1)
1466 if (ap->retired_piomode == 2)
1468 if (ap->retired_piomode == 1)
1470 if (ap->retired_piomode == 0)
1476 ata_wmode(struct ata_params *ap)
1478 if (ap->mwdmamodes & 0x04)
1480 if (ap->mwdmamodes & 0x02)
1482 if (ap->mwdmamodes & 0x01)
1488 ata_umode(struct ata_params *ap)
1490 if (ap->atavalid & ATA_FLAG_88) {
1491 if (ap->udmamodes & 0x40)
1493 if (ap->udmamodes & 0x20)
1495 if (ap->udmamodes & 0x10)
1497 if (ap->udmamodes & 0x08)
1499 if (ap->udmamodes & 0x04)
1501 if (ap->udmamodes & 0x02)
1503 if (ap->udmamodes & 0x01)
1510 bswap(int8_t *buf, int len)
1512 u_int16_t *ptr = (u_int16_t*)(buf + len);
1514 while (--ptr >= (u_int16_t*)buf)
1519 btrim(int8_t *buf, int len)
1523 for (ptr = buf; ptr < buf+len; ++ptr)
1526 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
1531 bpack(int8_t *src, int8_t *dst, int len)
1535 for (i = j = blank = 0 ; i < len; i++) {
1536 if (blank && src[i] == ' ') continue;
1537 if (blank && src[i] != ' ') {
1542 if (src[i] == ' ') {
1556 /* register controlling device */
1557 make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "ata");
1559 /* register boot attach to be run when interrupts are enabled */
1560 if (!(ata_delayed_attach = (struct intr_config_hook *)
1561 malloc(sizeof(struct intr_config_hook),
1562 M_TEMP, M_NOWAIT | M_ZERO))) {
1563 printf("ata: malloc of delayed attach hook failed\n");
1567 ata_delayed_attach->ich_func = (void*)ata_boot_attach;
1568 if (config_intrhook_establish(ata_delayed_attach) != 0) {
1569 printf("ata: config_intrhook_establish failed\n");
1570 free(ata_delayed_attach, M_TEMP);
1573 SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL)