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.
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/kernel.h>
38 #include <sys/module.h>
41 #include <sys/malloc.h>
42 #include <sys/devicestat.h>
43 #include <sys/sysctl.h>
44 #include <machine/stdarg.h>
45 #include <machine/resource.h>
46 #include <machine/bus.h>
49 #include <machine/md_var.h>
51 #include <dev/ata/ata-all.h>
52 #include <dev/ata/ata-disk.h>
53 #include <dev/ata/ata-raid.h>
54 #include <dev/ata/atapi-all.h>
56 /* device structures */
57 static d_ioctl_t ataioctl;
58 static struct cdevsw ata_cdevsw = {
60 /* close */ nullclose,
66 /* strategy */ nostrategy,
75 static void ata_boot_attach(void);
76 static void ata_intr(void *);
77 static int ata_getparam(struct ata_device *, u_int8_t);
78 static int ata_service(struct ata_channel *);
79 static void bswap(int8_t *, int);
80 static void btrim(int8_t *, int);
81 static void bpack(int8_t *, int8_t *, int);
82 static void ata_change_mode(struct ata_device *, int);
83 static u_int8_t ata_drawersensor(struct ata_device *, int, u_int8_t, u_int8_t);
86 SYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters");
89 devclass_t ata_devclass;
92 static struct intr_config_hook *ata_delayed_attach = NULL;
93 static MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer");
96 #define DEV_ATAPIALL defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || \
97 defined(DEV_ATAPIST) || defined(DEV_ATAPICAM)
100 ata_probe(device_t dev)
102 struct ata_channel *ch;
105 if (!dev || !(ch = device_get_softc(dev)))
108 if (ch->r_io || ch->r_altio || ch->r_irq)
111 /* initialize the softc basics */
112 ch->active = ATA_IDLE;
115 rid = ATA_IOADDR_RID;
116 ch->r_io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
117 ATA_IOSIZE, RF_ACTIVE);
121 rid = ATA_ALTADDR_RID;
122 ch->r_altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
123 ATA_ALTIOSIZE, RF_ACTIVE);
127 rid = ATA_BMADDR_RID;
128 ch->r_bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
129 ATA_BMIOSIZE, RF_ACTIVE);
131 ata_printf(ch, -1, "iobase=0x%04x altiobase=0x%04x bmaddr=0x%04x\n",
132 (int)rman_get_start(ch->r_io),
133 (int)rman_get_start(ch->r_altio),
134 (ch->r_bmio) ? (int)rman_get_start(ch->r_bmio) : 0);
138 ch->device[MASTER].channel = ch;
139 ch->device[MASTER].unit = ATA_MASTER;
140 ch->device[MASTER].mode = ATA_PIO;
141 ch->device[SLAVE].channel = ch;
142 ch->device[SLAVE].unit = ATA_SLAVE;
143 ch->device[SLAVE].mode = ATA_PIO;
144 TAILQ_INIT(&ch->ata_queue);
145 TAILQ_INIT(&ch->atapi_queue);
150 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ch->r_io);
152 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ch->r_altio);
154 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, ch->r_bmio);
156 ata_printf(ch, -1, "probe allocation failed\n");
161 ata_attach(device_t dev)
163 struct ata_channel *ch;
166 if (!dev || !(ch = device_get_softc(dev)))
170 ch->r_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
171 RF_SHAREABLE | RF_ACTIVE);
173 ata_printf(ch, -1, "unable to allocate interrupt\n");
176 if ((error = bus_setup_intr(dev, ch->r_irq, INTR_TYPE_BIO | INTR_ENTROPY,
177 ata_intr, ch, &ch->ih))) {
178 ata_printf(ch, -1, "unable to setup interrupt\n");
183 * do not attach devices if we are in early boot, this is done later
184 * when interrupts are enabled by a hook into the boot process.
185 * otherwise attach what the probe has found in ch->devices.
187 if (!ata_delayed_attach) {
188 if (ch->devices & ATA_ATA_SLAVE)
189 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
190 ch->devices &= ~ATA_ATA_SLAVE;
191 if (ch->devices & ATA_ATAPI_SLAVE)
192 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
193 ch->devices &= ~ATA_ATAPI_SLAVE;
194 if (ch->devices & ATA_ATA_MASTER)
195 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
196 ch->devices &= ~ATA_ATA_MASTER;
197 if (ch->devices & ATA_ATAPI_MASTER)
198 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
199 ch->devices &= ~ATA_ATAPI_MASTER;
201 if (ch->devices & ATA_ATA_MASTER)
202 ad_attach(&ch->device[MASTER]);
203 if (ch->devices & ATA_ATA_SLAVE)
204 ad_attach(&ch->device[SLAVE]);
207 if (ch->devices & ATA_ATAPI_MASTER)
208 atapi_attach(&ch->device[MASTER]);
209 if (ch->devices & ATA_ATAPI_SLAVE)
210 atapi_attach(&ch->device[SLAVE]);
213 if (ch->devices & (ATA_ATAPI_MASTER | ATA_ATAPI_SLAVE))
214 atapi_cam_attach_bus(ch);
221 ata_detach(device_t dev)
223 struct ata_channel *ch;
226 if (!dev || !(ch = device_get_softc(dev)) ||
227 !ch->r_io || !ch->r_altio || !ch->r_irq)
230 /* make sure channel is not busy */
231 ATA_SLEEPLOCK_CH(ch, ATA_CONTROL);
235 if (ch->devices & ATA_ATA_MASTER && ch->device[MASTER].driver)
236 ad_detach(&ch->device[MASTER], 1);
237 if (ch->devices & ATA_ATA_SLAVE && ch->device[SLAVE].driver)
238 ad_detach(&ch->device[SLAVE], 1);
241 if (ch->devices & ATA_ATAPI_MASTER && ch->device[MASTER].driver)
242 atapi_detach(&ch->device[MASTER]);
243 if (ch->devices & ATA_ATAPI_SLAVE && ch->device[SLAVE].driver)
244 atapi_detach(&ch->device[SLAVE]);
247 if (ch->devices & (ATA_ATAPI_SLAVE|ATA_ATAPI_MASTER))
248 atapi_cam_detach_bus(ch);
252 if (ch->device[MASTER].param) {
253 free(ch->device[MASTER].param, M_ATA);
254 ch->device[MASTER].param = NULL;
256 if (ch->device[SLAVE].param) {
257 free(ch->device[SLAVE].param, M_ATA);
258 ch->device[SLAVE].param = NULL;
260 ch->device[MASTER].driver = NULL;
261 ch->device[SLAVE].driver = NULL;
262 ch->device[MASTER].mode = ATA_PIO;
263 ch->device[SLAVE].mode = ATA_PIO;
267 bus_teardown_intr(dev, ch->r_irq, ch->ih);
268 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
270 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, ch->r_bmio);
271 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ch->r_altio);
272 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ch->r_io);
282 ata_resume(device_t dev)
284 return ata_reinit(device_get_softc(dev));
288 ataioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, struct thread *td)
290 struct ata_cmd *iocmd = (struct ata_cmd *)addr;
291 struct ata_channel *ch;
292 device_t device = devclass_get_device(ata_devclass, iocmd->channel);
298 if (iocmd->channel < -1 || iocmd->device < -1 || iocmd->device > SLAVE)
301 switch (iocmd->cmd) {
303 /* should enable channel HW on controller that can SOS XXX */
304 error = ata_probe(device);
306 error = ata_attach(device);
310 error = ata_detach(device);
311 /* should disable channel HW on controller that can SOS XXX */
315 if (!device || !(ch = device_get_softc(device)))
317 ATA_SLEEPLOCK_CH(ch, ATA_ACTIVE);
318 if ((error = ata_reinit(ch)))
323 if (!device || !(ch = device_get_softc(device)))
326 if ((iocmd->device == MASTER || iocmd->device == -1) &&
327 ch->device[MASTER].driver)
328 iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode;
330 iocmd->u.mode.mode[MASTER] = -1;
332 if ((iocmd->device == SLAVE || iocmd->device == -1) &&
333 ch->device[SLAVE].param)
334 iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode;
336 iocmd->u.mode.mode[SLAVE] = -1;
340 if (!device || !(ch = device_get_softc(device)))
343 if ((iocmd->device == MASTER || iocmd->device == -1) &&
344 iocmd->u.mode.mode[MASTER] >= 0 && ch->device[MASTER].param) {
345 ata_change_mode(&ch->device[MASTER],iocmd->u.mode.mode[MASTER]);
346 iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode;
349 iocmd->u.mode.mode[MASTER] = -1;
351 if ((iocmd->device == SLAVE || iocmd->device == -1) &&
352 iocmd->u.mode.mode[SLAVE] >= 0 && ch->device[SLAVE].param) {
353 ata_change_mode(&ch->device[SLAVE], iocmd->u.mode.mode[SLAVE]);
354 iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode;
357 iocmd->u.mode.mode[SLAVE] = -1;
361 if (!device || !(ch = device_get_softc(device)))
364 iocmd->u.param.type[MASTER] =
365 ch->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER);
366 iocmd->u.param.type[SLAVE] =
367 ch->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE);
369 if (ch->device[MASTER].name)
370 strcpy(iocmd->u.param.name[MASTER], ch->device[MASTER].name);
371 if (ch->device[SLAVE].name)
372 strcpy(iocmd->u.param.name[SLAVE], ch->device[SLAVE].name);
374 if (ch->device[MASTER].param)
375 bcopy(ch->device[MASTER].param, &iocmd->u.param.params[MASTER],
376 sizeof(struct ata_params));
377 if (ch->device[SLAVE].param)
378 bcopy(ch->device[SLAVE].param, &iocmd->u.param.params[SLAVE],
379 sizeof(struct ata_params));
383 struct ata_device *atadev;
384 u_int8_t id1, id2, cnt, div;
387 if (!device || !(ch = device_get_softc(device)))
390 ATA_SLEEPLOCK_CH(ch, ATA_ACTIVE);
392 if (iocmd->device == SLAVE)
393 atadev = &ch->device[SLAVE];
395 atadev = &ch->device[MASTER];
397 ata_drawersensor(atadev, 1, 0x4e, 0);
398 id1 = ata_drawersensor(atadev, 0, 0x4f, 0);
399 ata_drawersensor(atadev, 1, 0x4e, 0x80);
400 id2 = ata_drawersensor(atadev, 0, 0x4f, 0);
401 if (id1 != 0xa3 || id2 != 0x5c) {
406 div = 1 << (((ata_drawersensor(atadev, 0, 0x5d, 0)&0x20)>>3) +
407 ((ata_drawersensor(atadev, 0, 0x47, 0)&0x30)>>4) + 1);
408 cnt = ata_drawersensor(atadev, 0, 0x28, 0);
412 fan = 1350000 / cnt / div;
413 ata_drawersensor(atadev, 1, 0x4e, 0x01);
414 temp = (ata_drawersensor(atadev, 0, 0x50, 0) * 10) +
415 (ata_drawersensor(atadev, 0, 0x50, 0) & 0x80 ? 5 : 0);
417 iocmd->u.enclosure.fan = fan;
418 iocmd->u.enclosure.temp = temp;
419 iocmd->u.enclosure.v05 = ata_drawersensor(atadev, 0, 0x23, 0) * 27;
420 iocmd->u.enclosure.v12 = ata_drawersensor(atadev, 0, 0x24, 0) * 61;
428 return ata_raid_rebuild(iocmd->channel);
431 return ata_raid_create(&iocmd->u.raid_setup);
434 return ata_raid_delete(iocmd->channel);
437 return ata_raid_status(iocmd->channel, &iocmd->u.raid_status);
441 struct ata_device *atadev;
444 if (!device || !(ch = device_get_softc(device)))
447 if (!(atadev = &ch->device[iocmd->device]) ||
448 !(ch->devices & (iocmd->device == MASTER ?
449 ATA_ATAPI_MASTER : ATA_ATAPI_SLAVE)))
452 if (!(buf = malloc(iocmd->u.atapi.count, M_ATA, M_NOWAIT)))
455 if (iocmd->u.atapi.flags & ATAPI_CMD_WRITE) {
456 error = copyin(iocmd->u.atapi.data, buf, iocmd->u.atapi.count);
460 error = atapi_queue_cmd(atadev, iocmd->u.atapi.ccb,
461 buf, iocmd->u.atapi.count,
462 (iocmd->u.atapi.flags == ATAPI_CMD_READ ?
463 ATPR_F_READ : 0) | ATPR_F_QUIET,
464 iocmd->u.atapi.timeout, NULL, NULL);
466 iocmd->u.atapi.error = error;
467 bcopy(&atadev->result, iocmd->u.atapi.sense_data,
468 sizeof(struct atapi_reqsense));
471 else if (iocmd->u.atapi.flags & ATAPI_CMD_READ)
472 error = copyout(buf, iocmd->u.atapi.data, iocmd->u.atapi.count);
485 ata_getparam(struct ata_device *atadev, u_int8_t command)
487 struct ata_params *ata_parm;
490 if (!(ata_parm = malloc(sizeof(struct ata_params), M_ATA, M_NOWAIT))) {
491 ata_prtdev(atadev, "malloc for identify data failed\n");
495 /* apparently some devices needs this repeated */
497 if (ata_command(atadev, command, 0, 0, 0, ATA_WAIT_INTR)) {
498 ata_prtdev(atadev, "%s identify failed\n",
499 command == ATA_C_ATAPI_IDENTIFY ? "ATAPI" : "ATA");
500 free(ata_parm, M_ATA);
504 ata_prtdev(atadev, "%s identify retries exceeded\n",
505 command == ATA_C_ATAPI_IDENTIFY ? "ATAPI" : "ATA");
506 free(ata_parm, M_ATA);
509 } while (ata_wait(atadev, ((command == ATA_C_ATAPI_IDENTIFY) ?
510 ATA_S_DRQ : (ATA_S_READY|ATA_S_DSC|ATA_S_DRQ))));
511 ATA_INSW(atadev->channel->r_io, ATA_DATA, (int16_t *)ata_parm,
512 sizeof(struct ata_params)/sizeof(int16_t));
514 if (command == ATA_C_ATA_IDENTIFY ||
515 !((ata_parm->model[0] == 'N' && ata_parm->model[1] == 'E') ||
516 (ata_parm->model[0] == 'F' && ata_parm->model[1] == 'X') ||
517 (ata_parm->model[0] == 'P' && ata_parm->model[1] == 'i')))
518 bswap(ata_parm->model, sizeof(ata_parm->model));
519 btrim(ata_parm->model, sizeof(ata_parm->model));
520 bpack(ata_parm->model, ata_parm->model, sizeof(ata_parm->model));
521 bswap(ata_parm->revision, sizeof(ata_parm->revision));
522 btrim(ata_parm->revision, sizeof(ata_parm->revision));
523 bpack(ata_parm->revision, ata_parm->revision, sizeof(ata_parm->revision));
524 bswap(ata_parm->serial, sizeof(ata_parm->serial));
525 btrim(ata_parm->serial, sizeof(ata_parm->serial));
526 bpack(ata_parm->serial, ata_parm->serial, sizeof(ata_parm->serial));
527 atadev->param = ata_parm;
532 ata_boot_attach(void)
534 struct ata_channel *ch;
537 if (ata_delayed_attach) {
538 config_intrhook_disestablish(ata_delayed_attach);
539 free(ata_delayed_attach, M_TEMP);
540 ata_delayed_attach = NULL;
544 * run through all ata devices and look for real ATA & ATAPI devices
545 * using the hints we found in the early probe, this avoids some of
546 * the delays probing of non-exsistent devices can cause.
548 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
549 if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
551 if (ch->devices & ATA_ATA_SLAVE)
552 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
553 ch->devices &= ~ATA_ATA_SLAVE;
554 if (ch->devices & ATA_ATAPI_SLAVE)
555 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
556 ch->devices &= ~ATA_ATAPI_SLAVE;
557 if (ch->devices & ATA_ATA_MASTER)
558 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
559 ch->devices &= ~ATA_ATA_MASTER;
560 if (ch->devices & ATA_ATAPI_MASTER)
561 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
562 ch->devices &= ~ATA_ATAPI_MASTER;
566 /* now we know whats there, do the real attach, first the ATA disks */
567 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
568 if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
570 if (ch->devices & ATA_ATA_MASTER)
571 ad_attach(&ch->device[MASTER]);
572 if (ch->devices & ATA_ATA_SLAVE)
573 ad_attach(&ch->device[SLAVE]);
577 /* then the atapi devices */
578 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
579 if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
582 if (ch->devices & ATA_ATAPI_MASTER)
583 atapi_attach(&ch->device[MASTER]);
584 if (ch->devices & ATA_ATAPI_SLAVE)
585 atapi_attach(&ch->device[SLAVE]);
588 if (ch->devices & (ATA_ATAPI_MASTER | ATA_ATAPI_SLAVE))
589 atapi_cam_attach_bus(ch);
597 struct ata_channel *ch = (struct ata_channel *)data;
599 * on PCI systems we might share an interrupt line with another
600 * device or our twin ATA channel, so call ch->intr_func to figure
601 * out if it is really an interrupt we should process here
603 if (ch->intr_func && ch->intr_func(ch))
606 /* if drive is busy it didn't interrupt */
607 if (ATA_INB(ch->r_altio, ATA_ALTSTAT) & ATA_S_BUSY) {
609 if (!(ATA_INB(ch->r_altio, ATA_ALTSTAT) & ATA_S_DRQ))
613 /* clear interrupt and get status */
614 ch->status = ATA_INB(ch->r_io, ATA_STATUS);
616 if (ch->status & ATA_S_ERROR)
617 ch->error = ATA_INB(ch->r_io, ATA_ERROR);
619 /* find & call the responsible driver to process this interrupt */
620 switch (ch->active) {
623 if (!ch->running || ad_interrupt(ch->running) == ATA_OP_CONTINUES)
628 case ATA_ACTIVE_ATAPI:
629 if (!ch->running || atapi_interrupt(ch->running) == ATA_OP_CONTINUES)
634 if (ch->active & ATA_WAIT_INTR)
638 if (ch->active & ATA_CONTROL) {
639 ATA_FORCELOCK_CH(ch, ATA_CONTROL);
643 if ((ch->flags & ATA_QUEUED) &&
644 ATA_INB(ch->r_altio, ATA_ALTSTAT) & ATA_S_SERVICE) {
645 ATA_FORCELOCK_CH(ch, ATA_ACTIVE);
646 if (ata_service(ch) == ATA_OP_CONTINUES)
656 ata_start(struct ata_channel *ch)
659 struct ad_request *ad_request;
662 struct atapi_request *atapi_request;
666 if (!ATA_LOCK_CH(ch, ATA_ACTIVE))
671 /* find & call the responsible driver if anything on the ATA queue */
672 if (TAILQ_EMPTY(&ch->ata_queue)) {
673 if (ch->devices & (ATA_ATA_MASTER) && ch->device[MASTER].driver)
674 ad_start(&ch->device[MASTER]);
675 if (ch->devices & (ATA_ATA_SLAVE) && ch->device[SLAVE].driver)
676 ad_start(&ch->device[SLAVE]);
678 if ((ad_request = TAILQ_FIRST(&ch->ata_queue))) {
679 TAILQ_REMOVE(&ch->ata_queue, ad_request, chain);
680 ch->active = ATA_ACTIVE_ATA;
681 ch->running = ad_request;
682 if (ad_transfer(ad_request) == ATA_OP_CONTINUES) {
690 /* find & call the responsible driver if anything on the ATAPI queue */
691 if (TAILQ_EMPTY(&ch->atapi_queue)) {
692 if (ch->devices & (ATA_ATAPI_MASTER) && ch->device[MASTER].driver)
693 atapi_start(&ch->device[MASTER]);
694 if (ch->devices & (ATA_ATAPI_SLAVE) && ch->device[SLAVE].driver)
695 atapi_start(&ch->device[SLAVE]);
697 if ((atapi_request = TAILQ_FIRST(&ch->atapi_queue))) {
698 TAILQ_REMOVE(&ch->atapi_queue, atapi_request, chain);
699 ch->active = ATA_ACTIVE_ATAPI;
700 ch->running = atapi_request;
701 if (atapi_transfer(atapi_request) == ATA_OP_CONTINUES) {
712 ata_reset(struct ata_channel *ch)
714 u_int8_t lsb, msb, ostat0, ostat1;
715 u_int8_t stat0 = 0, stat1 = 0;
716 int mask = 0, timeout;
718 /* do we have any signs of ATA/ATAPI HW being present ? */
719 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
721 ostat0 = ATA_INB(ch->r_io, ATA_STATUS);
722 if ((ostat0 & 0xf8) != 0xf8 && ostat0 != 0xa5) {
726 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
728 ostat1 = ATA_INB(ch->r_io, ATA_STATUS);
729 if ((ostat1 & 0xf8) != 0xf8 && ostat1 != 0xa5) {
738 /* in some setups we dont want to test for a slave */
739 if (ch->flags & ATA_NO_SLAVE) {
745 ata_printf(ch, -1, "mask=%02x ostat0=%02x ostat2=%02x\n",
746 mask, ostat0, ostat1);
749 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
751 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_RESET);
753 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_IDS);
755 ATA_INB(ch->r_io, ATA_ERROR);
757 /* wait for BUSY to go inactive */
758 for (timeout = 0; timeout < 310000; timeout++) {
759 if (stat0 & ATA_S_BUSY) {
760 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
763 /* check for ATAPI signature while its still there */
764 lsb = ATA_INB(ch->r_io, ATA_CYL_LSB);
765 msb = ATA_INB(ch->r_io, ATA_CYL_MSB);
766 stat0 = ATA_INB(ch->r_io, ATA_STATUS);
767 if (!(stat0 & ATA_S_BUSY)) {
769 ata_printf(ch, ATA_MASTER, "ATAPI %02x %02x\n", lsb, msb);
770 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB)
771 ch->devices |= ATA_ATAPI_MASTER;
774 if (stat1 & ATA_S_BUSY) {
775 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
778 /* check for ATAPI signature while its still there */
779 lsb = ATA_INB(ch->r_io, ATA_CYL_LSB);
780 msb = ATA_INB(ch->r_io, ATA_CYL_MSB);
781 stat1 = ATA_INB(ch->r_io, ATA_STATUS);
782 if (!(stat1 & ATA_S_BUSY)) {
784 ata_printf(ch, ATA_SLAVE, "ATAPI %02x %02x\n", lsb, msb);
785 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB)
786 ch->devices |= ATA_ATAPI_SLAVE;
789 if (mask == 0x01) /* wait for master only */
790 if (!(stat0 & ATA_S_BUSY))
792 if (mask == 0x02) /* wait for slave only */
793 if (!(stat1 & ATA_S_BUSY))
795 if (mask == 0x03) /* wait for both master & slave */
796 if (!(stat0 & ATA_S_BUSY) && !(stat1 & ATA_S_BUSY))
801 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
803 if (stat0 & ATA_S_BUSY)
805 if (stat1 & ATA_S_BUSY)
808 ata_printf(ch, -1, "mask=%02x stat0=%02x stat1=%02x\n",
813 if (mask & 0x01 && ostat0 != 0x00 && !(ch->devices & ATA_ATAPI_MASTER)) {
814 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
816 ATA_OUTB(ch->r_io, ATA_ERROR, 0x58);
817 ATA_OUTB(ch->r_io, ATA_CYL_LSB, 0xa5);
818 lsb = ATA_INB(ch->r_io, ATA_ERROR);
819 msb = ATA_INB(ch->r_io, ATA_CYL_LSB);
821 ata_printf(ch, ATA_MASTER, "ATA %02x %02x\n", lsb, msb);
822 if (lsb != 0x58 && msb == 0xa5)
823 ch->devices |= ATA_ATA_MASTER;
825 if (mask & 0x02 && ostat1 != 0x00 && !(ch->devices & ATA_ATAPI_SLAVE)) {
826 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
828 ATA_OUTB(ch->r_io, ATA_ERROR, 0x58);
829 ATA_OUTB(ch->r_io, ATA_CYL_LSB, 0xa5);
830 lsb = ATA_INB(ch->r_io, ATA_ERROR);
831 msb = ATA_INB(ch->r_io, ATA_CYL_LSB);
833 ata_printf(ch, ATA_SLAVE, "ATA %02x %02x\n", lsb, msb);
834 if (lsb != 0x58 && msb == 0xa5)
835 ch->devices |= ATA_ATA_SLAVE;
838 ata_printf(ch, -1, "devices=%02x\n", ch->devices);
842 ata_reinit(struct ata_channel *ch)
844 int devices, misdev, newdev;
846 if (!ch->r_io || !ch->r_altio || !ch->r_irq)
849 ATA_FORCELOCK_CH(ch, ATA_CONTROL);
851 devices = ch->devices;
852 ata_printf(ch, -1, "resetting devices ..\n");
856 if (devices & (ATA_ATAPI_SLAVE|ATA_ATAPI_MASTER))
857 atapi_cam_detach_bus(ch);
859 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 newdev &= ~ATA_ATA_MASTER;
887 if (newdev & ATA_ATA_SLAVE)
888 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
889 newdev &= ~ATA_ATA_SLAVE;
890 if (newdev & ATA_ATAPI_MASTER)
891 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
892 newdev &= ~ATA_ATAPI_MASTER;
893 if (newdev & ATA_ATAPI_SLAVE)
894 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
895 newdev &= ~ATA_ATAPI_SLAVE;
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_attach_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_int16_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, PRIBIO, "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_drawer_start(struct ata_device *atadev)
1132 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1134 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1136 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1138 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1140 ATA_INB(atadev->channel->r_io, ATA_COUNT);
1142 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1147 ata_drawer_end(struct ata_device *atadev)
1149 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1154 ata_chip_start(struct ata_device *atadev)
1156 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0b);
1157 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0a);
1159 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08);
1163 ata_chip_end(struct ata_device *atadev)
1165 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08);
1167 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0a);
1169 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0b);
1174 ata_chip_rdbit(struct ata_device *atadev)
1178 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0);
1180 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x02);
1182 val = ATA_INB(atadev->channel->r_io, ATA_SECTOR) & 0x01;
1188 ata_chip_wrbit(struct ata_device *atadev, u_int8_t data)
1190 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08 | (data & 0x01));
1192 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08 | 0x02 | (data & 0x01));
1197 ata_chip_rw(struct ata_device *atadev, int rw, u_int8_t val)
1202 for (i = 0; i < 8; i++)
1203 ata_chip_wrbit(atadev, (val & (0x80 >> i)) ? 1 : 0);
1206 for (i = 0; i < 8; i++)
1207 val = (val << 1) | ata_chip_rdbit(atadev);
1209 ata_chip_wrbit(atadev, 0);
1214 ata_drawersensor(struct ata_device *atadev, int rw, u_int8_t idx, u_int8_t data)
1216 ata_drawer_start(atadev);
1217 ata_chip_start(atadev);
1218 ata_chip_rw(atadev, 1, 0x5a);
1219 ata_chip_rw(atadev, 1, idx);
1221 ata_chip_rw(atadev, 1, data);
1224 ata_chip_end(atadev);
1225 ata_chip_start(atadev);
1226 ata_chip_rw(atadev, 1, 0x5b);
1227 data = ata_chip_rw(atadev, 0, 0);
1229 ata_chip_end(atadev);
1230 ata_drawer_end(atadev);
1235 ata_drawerleds(struct ata_device *atadev, u_int8_t color)
1237 ata_drawer_start(atadev);
1238 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, color);
1240 ata_drawer_end(atadev);
1244 ata_change_mode(struct ata_device *atadev, int mode)
1246 int umode, wmode, pmode;
1248 umode = ata_umode(atadev->param);
1249 wmode = ata_wmode(atadev->param);
1250 pmode = ata_pmode(atadev->param);
1252 switch (mode & ATA_DMA_MASK) {
1254 if ((mode & ATA_MODE_MASK) < umode)
1255 umode = mode & ATA_MODE_MASK;
1258 if ((mode & ATA_MODE_MASK) < wmode)
1259 wmode = mode & ATA_MODE_MASK;
1263 if (((mode & ATA_MODE_MASK) - ATA_PIO0) < pmode)
1264 pmode = (mode & ATA_MODE_MASK) - ATA_PIO0;
1269 ATA_SLEEPLOCK_CH(atadev->channel, ATA_ACTIVE);
1270 ata_dmainit(atadev, pmode, wmode, umode);
1271 ATA_UNLOCK_CH(atadev->channel);
1272 ata_start(atadev->channel); /* XXX SOS */
1276 ata_printf(struct ata_channel *ch, int device, const char * fmt, ...)
1282 ret = printf("ata%d: ", device_get_unit(ch->dev));
1284 if (ch->device[ATA_DEV(device)].name)
1285 ret = printf("%s: ", ch->device[ATA_DEV(device)].name);
1287 ret = printf("ata%d-%s: ", device_get_unit(ch->dev),
1288 (device == ATA_MASTER) ? "master" : "slave");
1291 ret += vprintf(fmt, ap);
1297 ata_prtdev(struct ata_device *atadev, const char * fmt, ...)
1303 ret = printf("%s: ", atadev->name);
1305 ret = printf("ata%d-%s: ", device_get_unit(atadev->channel->dev),
1306 (atadev->unit == ATA_MASTER) ? "master" : "slave");
1308 ret += vprintf(fmt, ap);
1314 ata_set_name(struct ata_device *atadev, char *name, int lun)
1316 atadev->name = malloc(strlen(name) + 4, M_ATA, M_NOWAIT);
1318 sprintf(atadev->name, "%s%d", name, lun);
1322 ata_free_name(struct ata_device *atadev)
1325 free(atadev->name, M_ATA);
1326 atadev->name = NULL;
1330 ata_get_lun(u_int32_t *map)
1332 int lun = ffs(~*map) - 1;
1339 ata_test_lun(u_int32_t *map, int lun)
1341 return (*map & (1 << lun));
1345 ata_free_lun(u_int32_t *map, int lun)
1347 *map &= ~(1 << lun);
1351 ata_mode2str(int mode)
1354 case ATA_PIO: return "BIOSPIO";
1355 case ATA_PIO0: return "PIO0";
1356 case ATA_PIO1: return "PIO1";
1357 case ATA_PIO2: return "PIO2";
1358 case ATA_PIO3: return "PIO3";
1359 case ATA_PIO4: return "PIO4";
1360 case ATA_DMA: return "BIOSDMA";
1361 case ATA_WDMA2: return "WDMA2";
1362 case ATA_UDMA2: return "UDMA33";
1363 case ATA_UDMA4: return "UDMA66";
1364 case ATA_UDMA5: return "UDMA100";
1365 case ATA_UDMA6: return "UDMA133";
1366 default: return "???";
1371 ata_pmode(struct ata_params *ap)
1373 if (ap->atavalid & ATA_FLAG_64_70) {
1374 if (ap->apiomodes & 2)
1376 if (ap->apiomodes & 1)
1379 if (ap->retired_piomode == 2)
1381 if (ap->retired_piomode == 1)
1383 if (ap->retired_piomode == 0)
1389 ata_wmode(struct ata_params *ap)
1391 if (ap->mwdmamodes & 0x04)
1393 if (ap->mwdmamodes & 0x02)
1395 if (ap->mwdmamodes & 0x01)
1401 ata_umode(struct ata_params *ap)
1403 if (ap->atavalid & ATA_FLAG_88) {
1404 if (ap->udmamodes & 0x40)
1406 if (ap->udmamodes & 0x20)
1408 if (ap->udmamodes & 0x10)
1410 if (ap->udmamodes & 0x08)
1412 if (ap->udmamodes & 0x04)
1414 if (ap->udmamodes & 0x02)
1416 if (ap->udmamodes & 0x01)
1423 bswap(int8_t *buf, int len)
1425 u_int16_t *ptr = (u_int16_t*)(buf + len);
1427 while (--ptr >= (u_int16_t*)buf)
1432 btrim(int8_t *buf, int len)
1436 for (ptr = buf; ptr < buf+len; ++ptr)
1439 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
1444 bpack(int8_t *src, int8_t *dst, int len)
1448 for (i = j = blank = 0 ; i < len; i++) {
1449 if (blank && src[i] == ' ') continue;
1450 if (blank && src[i] != ' ') {
1455 if (src[i] == ' ') {
1469 /* register controlling device */
1470 make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "ata");
1472 /* register boot attach to be run when interrupts are enabled */
1473 if (!(ata_delayed_attach = (struct intr_config_hook *)
1474 malloc(sizeof(struct intr_config_hook),
1475 M_TEMP, M_NOWAIT | M_ZERO))) {
1476 printf("ata: malloc of delayed attach hook failed\n");
1480 ata_delayed_attach->ich_func = (void*)ata_boot_attach;
1481 if (config_intrhook_establish(ata_delayed_attach) != 0) {
1482 printf("ata: config_intrhook_establish failed\n");
1483 free(ata_delayed_attach, M_TEMP);
1486 SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL)