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 ata_probe(device_t dev)
98 struct ata_channel *ch;
101 if (!dev || !(ch = device_get_softc(dev)))
104 if (ch->r_io || ch->r_altio || ch->r_irq)
107 /* initialize the softc basics */
108 ch->active = ATA_IDLE;
111 rid = ATA_IOADDR_RID;
112 ch->r_io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
113 ATA_IOSIZE, RF_ACTIVE);
117 rid = ATA_ALTADDR_RID;
118 ch->r_altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
119 ATA_ALTIOSIZE, RF_ACTIVE);
123 rid = ATA_BMADDR_RID;
124 ch->r_bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
125 ATA_BMIOSIZE, RF_ACTIVE);
127 ata_printf(ch, -1, "iobase=0x%04x altiobase=0x%04x bmaddr=0x%04x\n",
128 (int)rman_get_start(ch->r_io),
129 (int)rman_get_start(ch->r_altio),
130 (ch->r_bmio) ? (int)rman_get_start(ch->r_bmio) : 0);
134 ch->device[MASTER].channel = ch;
135 ch->device[MASTER].unit = ATA_MASTER;
136 ch->device[MASTER].mode = ATA_PIO;
137 ch->device[SLAVE].channel = ch;
138 ch->device[SLAVE].unit = ATA_SLAVE;
139 ch->device[SLAVE].mode = ATA_PIO;
140 TAILQ_INIT(&ch->ata_queue);
141 TAILQ_INIT(&ch->atapi_queue);
146 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ch->r_io);
148 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ch->r_altio);
150 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, ch->r_bmio);
152 ata_printf(ch, -1, "probe allocation failed\n");
157 ata_attach(device_t dev)
159 struct ata_channel *ch;
162 if (!dev || !(ch = device_get_softc(dev)))
166 ch->r_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
167 RF_SHAREABLE | RF_ACTIVE);
169 ata_printf(ch, -1, "unable to allocate interrupt\n");
172 if ((error = bus_setup_intr(dev, ch->r_irq, INTR_TYPE_BIO | INTR_ENTROPY,
173 ata_intr, ch, &ch->ih))) {
174 ata_printf(ch, -1, "unable to setup interrupt\n");
179 * do not attach devices if we are in early boot, this is done later
180 * when interrupts are enabled by a hook into the boot process.
181 * otherwise attach what the probe has found in ch->devices.
183 if (!ata_delayed_attach) {
184 if (ch->devices & ATA_ATA_SLAVE)
185 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
186 ch->devices &= ~ATA_ATA_SLAVE;
187 if (ch->devices & ATA_ATAPI_SLAVE)
188 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
189 ch->devices &= ~ATA_ATAPI_SLAVE;
190 if (ch->devices & ATA_ATA_MASTER)
191 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
192 ch->devices &= ~ATA_ATA_MASTER;
193 if (ch->devices & ATA_ATAPI_MASTER)
194 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
195 ch->devices &= ~ATA_ATAPI_MASTER;
197 if (ch->devices & ATA_ATA_MASTER)
198 ad_attach(&ch->device[MASTER]);
199 if (ch->devices & ATA_ATA_SLAVE)
200 ad_attach(&ch->device[SLAVE]);
202 #if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST)
203 if (ch->devices & ATA_ATAPI_MASTER)
204 atapi_attach(&ch->device[MASTER]);
205 if (ch->devices & ATA_ATAPI_SLAVE)
206 atapi_attach(&ch->device[SLAVE]);
213 ata_detach(device_t dev)
215 struct ata_channel *ch;
218 if (!dev || !(ch = device_get_softc(dev)))
221 /* make sure channel is not busy */
222 ATA_SLEEPLOCK_CH(ch, ATA_CONTROL);
226 if (ch->devices & ATA_ATA_MASTER && ch->device[MASTER].driver)
227 ad_detach(&ch->device[MASTER], 1);
228 if (ch->devices & ATA_ATA_SLAVE && ch->device[SLAVE].driver)
229 ad_detach(&ch->device[SLAVE], 1);
231 #if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST)
232 if (ch->devices & ATA_ATAPI_MASTER && ch->device[MASTER].driver)
233 atapi_detach(&ch->device[MASTER]);
234 if (ch->devices & ATA_ATAPI_SLAVE && ch->device[SLAVE].driver)
235 atapi_detach(&ch->device[SLAVE]);
239 if (ch->device[MASTER].param) {
240 free(ch->device[MASTER].param, M_ATA);
241 ch->device[MASTER].param = NULL;
243 if (ch->device[SLAVE].param) {
244 free(ch->device[SLAVE].param, M_ATA);
245 ch->device[SLAVE].param = NULL;
247 ch->device[MASTER].driver = NULL;
248 ch->device[SLAVE].driver = NULL;
249 ch->device[MASTER].mode = ATA_PIO;
250 ch->device[SLAVE].mode = ATA_PIO;
253 bus_teardown_intr(dev, ch->r_irq, ch->ih);
254 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
256 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, ch->r_bmio);
257 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ch->r_altio);
258 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ch->r_io);
268 ata_resume(device_t dev)
270 return ata_reinit(device_get_softc(dev));
274 ataioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, struct thread *td)
276 struct ata_cmd *iocmd = (struct ata_cmd *)addr;
277 struct ata_channel *ch;
278 device_t device = devclass_get_device(ata_devclass, iocmd->channel);
284 if (iocmd->channel < -1 || iocmd->device < -1 || iocmd->device > SLAVE)
287 switch (iocmd->cmd) {
289 /* should enable channel HW on controller that can SOS XXX */
290 error = ata_probe(device);
292 error = ata_attach(device);
296 error = ata_detach(device);
297 /* should disable channel HW on controller that can SOS XXX */
301 if (!device || !(ch = device_get_softc(device)))
303 ATA_SLEEPLOCK_CH(ch, ATA_ACTIVE);
304 error = ata_reinit(ch);
308 if (!device || !(ch = device_get_softc(device)))
311 if ((iocmd->device == MASTER || iocmd->device == -1) &&
312 ch->device[MASTER].driver)
313 iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode;
315 iocmd->u.mode.mode[MASTER] = -1;
317 if ((iocmd->device == SLAVE || iocmd->device == -1) &&
318 ch->device[SLAVE].param)
319 iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode;
321 iocmd->u.mode.mode[SLAVE] = -1;
325 if (!device || !(ch = device_get_softc(device)))
328 if ((iocmd->device == MASTER || iocmd->device == -1) &&
329 iocmd->u.mode.mode[MASTER] >= 0 && ch->device[MASTER].param) {
330 ata_change_mode(&ch->device[MASTER],iocmd->u.mode.mode[MASTER]);
331 iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode;
334 iocmd->u.mode.mode[MASTER] = -1;
336 if ((iocmd->device == SLAVE || iocmd->device == -1) &&
337 iocmd->u.mode.mode[SLAVE] >= 0 && ch->device[SLAVE].param) {
338 ata_change_mode(&ch->device[SLAVE], iocmd->u.mode.mode[SLAVE]);
339 iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode;
342 iocmd->u.mode.mode[SLAVE] = -1;
346 if (!device || !(ch = device_get_softc(device)))
349 iocmd->u.param.type[MASTER] =
350 ch->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER);
351 iocmd->u.param.type[SLAVE] =
352 ch->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE);
354 if (ch->device[MASTER].name)
355 strcpy(iocmd->u.param.name[MASTER], ch->device[MASTER].name);
356 if (ch->device[SLAVE].name)
357 strcpy(iocmd->u.param.name[SLAVE], ch->device[SLAVE].name);
359 if (ch->device[MASTER].param)
360 bcopy(ch->device[MASTER].param, &iocmd->u.param.params[MASTER],
361 sizeof(struct ata_params));
362 if (ch->device[SLAVE].param)
363 bcopy(ch->device[SLAVE].param, &iocmd->u.param.params[SLAVE],
364 sizeof(struct ata_params));
368 struct ata_device *atadev;
369 u_int8_t id1, id2, cnt, div;
372 if (!device || !(ch = device_get_softc(device)))
375 ATA_SLEEPLOCK_CH(ch, ATA_ACTIVE);
377 if (iocmd->device == SLAVE)
378 atadev = &ch->device[SLAVE];
380 atadev = &ch->device[MASTER];
382 ata_drawersensor(atadev, 1, 0x4e, 0);
383 id1 = ata_drawersensor(atadev, 0, 0x4f, 0);
384 ata_drawersensor(atadev, 1, 0x4e, 0x80);
385 id2 = ata_drawersensor(atadev, 0, 0x4f, 0);
386 if (id1 != 0xa3 || id2 != 0x5c)
389 div = 1 << (((ata_drawersensor(atadev, 0, 0x5d, 0)&0x20)>>3) +
390 ((ata_drawersensor(atadev, 0, 0x47, 0)&0x30)>>4) + 1);
391 cnt = ata_drawersensor(atadev, 0, 0x28, 0);
395 fan = 1350000 / cnt / div;
396 ata_drawersensor(atadev, 1, 0x4e, 0x01);
397 temp = (ata_drawersensor(atadev, 0, 0x50, 0) * 10) +
398 (ata_drawersensor(atadev, 0, 0x50, 0) & 0x80 ? 5 : 0);
400 iocmd->u.enclosure.fan = fan;
401 iocmd->u.enclosure.temp = temp;
402 iocmd->u.enclosure.v05 = ata_drawersensor(atadev, 0, 0x23, 0) * 27;
403 iocmd->u.enclosure.v12 = ata_drawersensor(atadev, 0, 0x24, 0) * 61;
411 return ata_raid_rebuild(iocmd->channel);
414 return ata_raid_create(&iocmd->u.raid_setup);
417 return ata_raid_delete(iocmd->channel);
419 #if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST)
421 struct ata_device *atadev;
424 if (!device || !(ch = device_get_softc(device)))
427 if (!(atadev = &ch->device[iocmd->device]) ||
428 !(ch->devices & (iocmd->device == MASTER ?
429 ATA_ATAPI_MASTER : ATA_ATAPI_SLAVE)))
432 if (!(buf = malloc(iocmd->u.atapi.count, M_ATA, M_NOWAIT)))
435 if (iocmd->u.atapi.flags & ATAPI_CMD_WRITE) {
436 error = copyin(iocmd->u.atapi.data, buf, iocmd->u.atapi.count);
440 error = atapi_queue_cmd(atadev, iocmd->u.atapi.ccb,
441 buf, iocmd->u.atapi.count,
442 (iocmd->u.atapi.flags == ATAPI_CMD_READ ?
443 ATPR_F_READ : 0) | ATPR_F_QUIET,
444 iocmd->u.atapi.timeout, NULL, NULL);
446 iocmd->u.atapi.error = error;
447 bcopy(&atadev->result, iocmd->u.atapi.sense_data,
448 sizeof(struct atapi_reqsense));
451 else if (iocmd->u.atapi.flags & ATAPI_CMD_READ)
452 error = copyout(buf, iocmd->u.atapi.data, iocmd->u.atapi.count);
465 ata_getparam(struct ata_device *atadev, u_int8_t command)
467 struct ata_params *ata_parm;
470 if (!(ata_parm = malloc(sizeof(struct ata_params), M_ATA, M_NOWAIT))) {
471 ata_prtdev(atadev, "malloc for identify data failed\n");
475 /* apparently some devices needs this repeated */
477 if (ata_command(atadev, command, 0, 0, 0, ATA_WAIT_INTR)) {
478 ata_prtdev(atadev, "%s identify failed\n",
479 command == ATA_C_ATAPI_IDENTIFY ? "ATAPI" : "ATA");
480 free(ata_parm, M_ATA);
484 ata_prtdev(atadev, "%s identify retries exceeded\n",
485 command == ATA_C_ATAPI_IDENTIFY ? "ATAPI" : "ATA");
486 free(ata_parm, M_ATA);
489 } while (ata_wait(atadev, ((command == ATA_C_ATAPI_IDENTIFY) ?
490 ATA_S_DRQ : (ATA_S_READY|ATA_S_DSC|ATA_S_DRQ))));
491 ATA_INSW(atadev->channel->r_io, ATA_DATA, (int16_t *)ata_parm,
492 sizeof(struct ata_params)/sizeof(int16_t));
494 if (command == ATA_C_ATA_IDENTIFY ||
495 !((ata_parm->model[0] == 'N' && ata_parm->model[1] == 'E') ||
496 (ata_parm->model[0] == 'F' && ata_parm->model[1] == 'X') ||
497 (ata_parm->model[0] == 'P' && ata_parm->model[1] == 'i')))
498 bswap(ata_parm->model, sizeof(ata_parm->model));
499 btrim(ata_parm->model, sizeof(ata_parm->model));
500 bpack(ata_parm->model, ata_parm->model, sizeof(ata_parm->model));
501 bswap(ata_parm->revision, sizeof(ata_parm->revision));
502 btrim(ata_parm->revision, sizeof(ata_parm->revision));
503 bpack(ata_parm->revision, ata_parm->revision, sizeof(ata_parm->revision));
504 atadev->param = ata_parm;
509 ata_boot_attach(void)
511 struct ata_channel *ch;
514 if (ata_delayed_attach) {
515 config_intrhook_disestablish(ata_delayed_attach);
516 free(ata_delayed_attach, M_TEMP);
517 ata_delayed_attach = NULL;
521 * run through all ata devices and look for real ATA & ATAPI devices
522 * using the hints we found in the early probe, this avoids some of
523 * the delays probing of non-exsistent devices can cause.
525 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
526 if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
528 if (ch->devices & ATA_ATA_SLAVE)
529 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
530 ch->devices &= ~ATA_ATA_SLAVE;
531 if (ch->devices & ATA_ATAPI_SLAVE)
532 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
533 ch->devices &= ~ATA_ATAPI_SLAVE;
534 if (ch->devices & ATA_ATA_MASTER)
535 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
536 ch->devices &= ~ATA_ATA_MASTER;
537 if (ch->devices & ATA_ATAPI_MASTER)
538 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
539 ch->devices &= ~ATA_ATAPI_MASTER;
543 /* now we know whats there, do the real attach, first the ATA disks */
544 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
545 if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
547 if (ch->devices & ATA_ATA_MASTER)
548 ad_attach(&ch->device[MASTER]);
549 if (ch->devices & ATA_ATA_SLAVE)
550 ad_attach(&ch->device[SLAVE]);
552 u_int8_t id1, id2, fan;
554 ata_drawersensor(&ch->device[MASTER], 1, 0x4e, 0);
555 id1 = ata_drawersensor(&ch->device[MASTER], 0, 0x4f, 0);
556 ata_drawersensor(&ch->device[MASTER], 1, 0x4e, 0x80);
557 id2 = ata_drawersensor(&ch->device[MASTER], 0, 0x4f, 0);
558 fan = ata_drawersensor(&ch->device[MASTER], 0, 0x28, 0);
559 printf("winbond ID 0x%02x 0x%02x 0x%02x\n", id1, id2, fan);
564 #if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST)
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]);
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)
610 #if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST)
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;
659 #if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST)
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) {
687 #if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST)
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);
760 stat0 = ATA_INB(ch->r_io, ATA_STATUS);
761 if (!(stat0 & ATA_S_BUSY)) {
762 /* check for ATAPI signature while its still there */
763 lsb = ATA_INB(ch->r_io, ATA_CYL_LSB);
764 msb = ATA_INB(ch->r_io, ATA_CYL_MSB);
766 ata_printf(ch, ATA_MASTER, "ATAPI %02x %02x\n", lsb, msb);
767 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB)
768 ch->devices |= ATA_ATAPI_MASTER;
771 if (stat1 & ATA_S_BUSY) {
772 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
774 stat1 = ATA_INB(ch->r_io, ATA_STATUS);
775 if (!(stat1 & ATA_S_BUSY)) {
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);
780 ata_printf(ch, ATA_SLAVE, "ATAPI %02x %02x\n", lsb, msb);
781 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB)
782 ch->devices |= ATA_ATAPI_SLAVE;
785 if (mask == 0x01) /* wait for master only */
786 if (!(stat0 & ATA_S_BUSY))
788 if (mask == 0x02) /* wait for slave only */
789 if (!(stat1 & ATA_S_BUSY))
791 if (mask == 0x03) /* wait for both master & slave */
792 if (!(stat0 & ATA_S_BUSY) && !(stat1 & ATA_S_BUSY))
797 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
799 if (stat0 & ATA_S_BUSY)
801 if (stat1 & ATA_S_BUSY)
804 ata_printf(ch, -1, "mask=%02x stat0=%02x stat1=%02x\n",
809 if (mask & 0x01 && ostat0 != 0x00 && !(ch->devices & ATA_ATAPI_MASTER)) {
810 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
812 ATA_OUTB(ch->r_io, ATA_ERROR, 0x58);
813 ATA_OUTB(ch->r_io, ATA_CYL_LSB, 0xa5);
814 lsb = ATA_INB(ch->r_io, ATA_ERROR);
815 msb = ATA_INB(ch->r_io, ATA_CYL_LSB);
817 ata_printf(ch, ATA_MASTER, "ATA %02x %02x\n", lsb, msb);
818 if (lsb != 0x58 && msb == 0xa5)
819 ch->devices |= ATA_ATA_MASTER;
821 if (mask & 0x02 && ostat1 != 0x00 && !(ch->devices & ATA_ATAPI_SLAVE)) {
822 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
824 ATA_OUTB(ch->r_io, ATA_ERROR, 0x58);
825 ATA_OUTB(ch->r_io, ATA_CYL_LSB, 0xa5);
826 lsb = ATA_INB(ch->r_io, ATA_ERROR);
827 msb = ATA_INB(ch->r_io, ATA_CYL_LSB);
829 ata_printf(ch, ATA_SLAVE, "ATA %02x %02x\n", lsb, msb);
830 if (lsb != 0x58 && msb == 0xa5)
831 ch->devices |= ATA_ATA_SLAVE;
834 ata_printf(ch, -1, "devices=%02x\n", ch->devices);
838 ata_reinit(struct ata_channel *ch)
840 int devices, misdev, newdev;
842 if (!ch->r_io || !ch->r_altio || !ch->r_irq)
844 ATA_FORCELOCK_CH(ch, ATA_CONTROL);
846 devices = ch->devices;
847 ata_printf(ch, -1, "resetting devices .. ");
850 if ((misdev = devices & ~ch->devices)) {
854 if (misdev & ATA_ATA_MASTER && ch->device[MASTER].driver)
855 ad_detach(&ch->device[MASTER], 0);
856 if (misdev & ATA_ATA_SLAVE && ch->device[SLAVE].driver)
857 ad_detach(&ch->device[SLAVE], 0);
859 #if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST)
860 if (misdev & ATA_ATAPI_MASTER && ch->device[MASTER].driver)
861 atapi_detach(&ch->device[MASTER]);
862 if (misdev & ATA_ATAPI_SLAVE && ch->device[SLAVE].driver)
863 atapi_detach(&ch->device[SLAVE]);
865 if (misdev & ATA_ATA_MASTER || misdev & ATA_ATAPI_MASTER) {
866 if (ch->device[MASTER].param)
867 free(ch->device[MASTER].param, M_ATA);
868 ch->device[MASTER].param = NULL;
870 if (misdev & ATA_ATA_SLAVE || misdev & ATA_ATAPI_SLAVE) {
871 if (ch->device[SLAVE].param)
872 free(ch->device[SLAVE].param, M_ATA);
873 ch->device[SLAVE].param = NULL;
876 if ((newdev = ~devices & ch->devices)) {
877 if (newdev & ATA_ATA_MASTER)
878 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
879 newdev &= ~ATA_ATA_MASTER;
880 if (newdev & ATA_ATA_SLAVE)
881 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
882 newdev &= ~ATA_ATA_SLAVE;
883 if (newdev & ATA_ATAPI_MASTER)
884 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
885 newdev &= ~ATA_ATAPI_MASTER;
886 if (newdev & ATA_ATAPI_SLAVE)
887 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
888 newdev &= ~ATA_ATAPI_SLAVE;
890 if (!misdev && newdev)
893 if (newdev & ATA_ATA_MASTER && !ch->device[MASTER].driver)
894 ad_attach(&ch->device[MASTER]);
895 else if (ch->devices & ATA_ATA_MASTER && ch->device[MASTER].driver) {
896 ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY);
897 ad_reinit(&ch->device[MASTER]);
899 if (newdev & ATA_ATA_SLAVE && !ch->device[SLAVE].driver)
900 ad_attach(&ch->device[SLAVE]);
901 else if (ch->devices & (ATA_ATA_SLAVE) && ch->device[SLAVE].driver) {
902 ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY);
903 ad_reinit(&ch->device[SLAVE]);
906 #if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST)
907 if (newdev & ATA_ATAPI_MASTER && !ch->device[MASTER].driver)
908 atapi_attach(&ch->device[MASTER]);
909 else if (ch->devices & (ATA_ATAPI_MASTER) && ch->device[MASTER].driver) {
910 ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY);
911 atapi_reinit(&ch->device[MASTER]);
913 if (newdev & ATA_ATAPI_SLAVE && !ch->device[SLAVE].driver)
914 atapi_attach(&ch->device[SLAVE]);
915 else if (ch->devices & (ATA_ATAPI_SLAVE) && ch->device[SLAVE].driver) {
916 ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY);
917 atapi_reinit(&ch->device[SLAVE]);
927 ata_service(struct ata_channel *ch)
929 /* do we have a SERVICE request from the drive ? */
930 if ((ch->status & (ATA_S_SERVICE|ATA_S_ERROR|ATA_S_DRQ)) == ATA_S_SERVICE) {
931 ATA_OUTB(ch->r_bmio, ATA_BMSTAT_PORT,
932 ata_dmastatus(ch) | ATA_BMSTAT_INTERRUPT);
934 if ((ATA_INB(ch->r_io, ATA_DRIVE) & ATA_SLAVE) == ATA_MASTER) {
935 if ((ch->devices & ATA_ATA_MASTER) && ch->device[MASTER].driver)
936 return ad_service((struct ad_softc *)
937 ch->device[MASTER].driver, 0);
940 if ((ch->devices & ATA_ATA_SLAVE) && ch->device[SLAVE].driver)
941 return ad_service((struct ad_softc *)
942 ch->device[SLAVE].driver, 0);
946 return ATA_OP_FINISHED;
950 ata_wait(struct ata_device *atadev, u_int8_t mask)
955 while (timeout < 5000000) { /* timeout 5 secs */
956 atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS);
958 /* if drive fails status, reselect the drive just to be sure */
959 if (atadev->channel->status == 0xff) {
960 ata_prtdev(atadev, "no status, reselecting device\n");
961 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM|atadev->unit);
963 atadev->channel->status = ATA_INB(atadev->channel->r_io,ATA_STATUS);
964 if (atadev->channel->status == 0xff)
969 if (!(atadev->channel->status & ATA_S_BUSY))
972 if (timeout > 1000) {
981 if (atadev->channel->status & ATA_S_ERROR)
982 atadev->channel->error = ATA_INB(atadev->channel->r_io, ATA_ERROR);
983 if (timeout >= 5000000)
986 return (atadev->channel->status & ATA_S_ERROR);
988 /* Wait 50 msec for bits wanted. */
991 atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS);
992 if ((atadev->channel->status & mask) == mask) {
993 if (atadev->channel->status & ATA_S_ERROR)
994 atadev->channel->error=ATA_INB(atadev->channel->r_io,ATA_ERROR);
995 return (atadev->channel->status & ATA_S_ERROR);
1003 ata_command(struct ata_device *atadev, u_int8_t command,
1004 u_int64_t lba, u_int16_t count, u_int8_t feature, int flags)
1008 ata_prtdev(atadev, "ata_command: addr=%04lx, cmd=%02x, "
1009 "lba=%lld, count=%d, feature=%d, flags=%02x\n",
1010 rman_get_start(atadev->channel->r_io),
1011 command, lba, count, feature, flags);
1015 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1017 /* disable interrupt from device */
1018 if (atadev->channel->flags & ATA_QUEUED)
1019 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_4BIT);
1021 /* ready to issue command ? */
1022 if (ata_wait(atadev, 0) < 0) {
1023 ata_prtdev(atadev, "timeout sending command=%02x s=%02x e=%02x\n",
1024 command, atadev->channel->status, atadev->channel->error);
1028 /* only use 48bit addressing if needed because of the overhead */
1029 if ((lba > 268435455 || count > 256) && atadev->param &&
1030 atadev->param->support.address48) {
1031 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, (feature>>8) & 0xff);
1032 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, feature);
1033 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, (count>>8) & 0xff);
1034 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, count & 0xff);
1035 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, (lba>>24) & 0xff);
1036 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, lba & 0xff);
1037 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba<<32) & 0xff);
1038 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>8) & 0xff);
1039 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>40) & 0xff);
1040 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>16) & 0xff);
1041 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_LBA | atadev->unit);
1043 /* translate command into 48bit version */
1046 command = ATA_C_READ48; break;
1047 case ATA_C_READ_MUL:
1048 command = ATA_C_READ_MUL48; break;
1049 case ATA_C_READ_DMA:
1050 command = ATA_C_READ_DMA48; break;
1051 case ATA_C_READ_DMA_QUEUED:
1052 command = ATA_C_READ_DMA_QUEUED48; break;
1054 command = ATA_C_WRITE48; break;
1055 case ATA_C_WRITE_MUL:
1056 command = ATA_C_WRITE_MUL48; break;
1057 case ATA_C_WRITE_DMA:
1058 command = ATA_C_WRITE_DMA48; break;
1059 case ATA_C_WRITE_DMA_QUEUED:
1060 command = ATA_C_WRITE_DMA_QUEUED48; break;
1061 case ATA_C_FLUSHCACHE:
1062 command = ATA_C_FLUSHCACHE48; break;
1064 ata_prtdev(atadev, "can't translate cmd to 48bit version\n");
1069 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, feature);
1070 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, count);
1071 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, lba & 0xff);
1072 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>8) & 0xff);
1073 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>16) & 0xff);
1074 if (atadev->flags & ATA_D_USE_CHS)
1075 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE,
1076 ATA_D_IBM | atadev->unit | ((lba>>24) & 0xf));
1078 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE,
1079 ATA_D_IBM | ATA_D_LBA | atadev->unit | ((lba>>24) &0xf));
1082 switch (flags & ATA_WAIT_MASK) {
1084 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1086 /* enable interrupt */
1087 if (atadev->channel->flags & ATA_QUEUED)
1088 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
1092 atadev->channel->active |= ATA_WAIT_INTR;
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);
1099 if (tsleep((caddr_t)atadev->channel, PRIBIO, "atacmd", 10 * hz)) {
1100 ata_prtdev(atadev, "timeout waiting for interrupt\n");
1101 atadev->channel->active &= ~ATA_WAIT_INTR;
1106 case ATA_WAIT_READY:
1107 atadev->channel->active |= ATA_WAIT_READY;
1108 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1109 if (ata_wait(atadev, ATA_S_READY) < 0) {
1110 ata_prtdev(atadev, "timeout waiting for cmd=%02x s=%02x e=%02x\n",
1111 command, atadev->channel->status,atadev->channel->error);
1114 atadev->channel->active &= ~ATA_WAIT_READY;
1121 ata_drawer_start(struct ata_device *atadev)
1123 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1125 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1127 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1129 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1131 ATA_INB(atadev->channel->r_io, ATA_COUNT);
1133 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1138 ata_drawer_end(struct ata_device *atadev)
1140 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1145 ata_chip_start(struct ata_device *atadev)
1147 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0b);
1148 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0a);
1150 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08);
1154 ata_chip_end(struct ata_device *atadev)
1156 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08);
1158 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0a);
1160 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0b);
1165 ata_chip_rdbit(struct ata_device *atadev)
1169 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0);
1171 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x02);
1173 val = ATA_INB(atadev->channel->r_io, ATA_SECTOR) & 0x01;
1179 ata_chip_wrbit(struct ata_device *atadev, u_int8_t data)
1181 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08 | (data & 0x01));
1183 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08 | 0x02 | (data & 0x01));
1188 ata_chip_rw(struct ata_device *atadev, int rw, u_int8_t val)
1193 for (i = 0; i < 8; i++)
1194 ata_chip_wrbit(atadev, (val & (0x80 >> i)) ? 1 : 0);
1197 for (i = 0; i < 8; i++)
1198 val = (val << 1) | ata_chip_rdbit(atadev);
1200 ata_chip_wrbit(atadev, 0);
1205 ata_drawersensor(struct ata_device *atadev, int rw, u_int8_t idx, u_int8_t data)
1207 ata_drawer_start(atadev);
1208 ata_chip_start(atadev);
1209 ata_chip_rw(atadev, 1, 0x5a);
1210 ata_chip_rw(atadev, 1, idx);
1212 ata_chip_rw(atadev, 1, data);
1215 ata_chip_end(atadev);
1216 ata_chip_start(atadev);
1217 ata_chip_rw(atadev, 1, 0x5b);
1218 data = ata_chip_rw(atadev, 0, 0);
1220 ata_chip_end(atadev);
1221 ata_drawer_end(atadev);
1226 ata_drawerleds(struct ata_device *atadev, u_int8_t color)
1228 ata_drawer_start(atadev);
1229 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, color);
1231 ata_drawer_end(atadev);
1235 ata_change_mode(struct ata_device *atadev, int mode)
1237 int umode, wmode, pmode;
1239 umode = ata_umode(atadev->param);
1240 wmode = ata_wmode(atadev->param);
1241 pmode = ata_pmode(atadev->param);
1243 switch (mode & ATA_DMA_MASK) {
1245 if ((mode & ATA_MODE_MASK) < umode)
1246 umode = mode & ATA_MODE_MASK;
1249 if ((mode & ATA_MODE_MASK) < wmode)
1250 wmode = mode & ATA_MODE_MASK;
1254 if (((mode & ATA_MODE_MASK) - ATA_PIO0) < pmode)
1255 pmode = (mode & ATA_MODE_MASK) - ATA_PIO0;
1260 ATA_SLEEPLOCK_CH(atadev->channel, ATA_ACTIVE);
1261 ata_dmainit(atadev->channel, atadev->unit, pmode, wmode, umode);
1262 ATA_UNLOCK_CH(atadev->channel);
1263 ata_start(atadev->channel); /* XXX SOS */
1267 ata_printf(struct ata_channel *ch, int device, const char * fmt, ...)
1273 ret = printf("ata%d: ", device_get_unit(ch->dev));
1275 if (ch->device[ATA_DEV(device)].name)
1276 ret = printf("%s: ", ch->device[ATA_DEV(device)].name);
1278 ret = printf("ata%d-%s: ", device_get_unit(ch->dev),
1279 (device == ATA_MASTER) ? "master" : "slave");
1282 ret += vprintf(fmt, ap);
1288 ata_prtdev(struct ata_device *atadev, const char * fmt, ...)
1294 ret = printf("%s: ", atadev->name);
1296 ret = printf("ata%d-%s: ", device_get_unit(atadev->channel->dev),
1297 (atadev->unit == ATA_MASTER) ? "master" : "slave");
1299 ret += vprintf(fmt, ap);
1305 ata_set_name(struct ata_device *atadev, char *name, int lun)
1307 atadev->name = malloc(strlen(name) + 4, M_ATA, M_NOWAIT);
1309 sprintf(atadev->name, "%s%d", name, lun);
1313 ata_free_name(struct ata_device *atadev)
1316 free(atadev->name, M_ATA);
1317 atadev->name = NULL;
1321 ata_get_lun(u_int32_t *map)
1323 int lun = ffs(~*map) - 1;
1330 ata_test_lun(u_int32_t *map, int lun)
1332 return (*map & (1 << lun));
1336 ata_free_lun(u_int32_t *map, int lun)
1338 *map &= ~(1 << lun);
1342 ata_mode2str(int mode)
1345 case ATA_PIO: return "BIOSPIO";
1346 case ATA_PIO0: return "PIO0";
1347 case ATA_PIO1: return "PIO1";
1348 case ATA_PIO2: return "PIO2";
1349 case ATA_PIO3: return "PIO3";
1350 case ATA_PIO4: return "PIO4";
1351 case ATA_DMA: return "BIOSDMA";
1352 case ATA_WDMA2: return "WDMA2";
1353 case ATA_UDMA2: return "UDMA33";
1354 case ATA_UDMA4: return "UDMA66";
1355 case ATA_UDMA5: return "UDMA100";
1356 case ATA_UDMA6: return "UDMA133";
1357 default: return "???";
1362 ata_pmode(struct ata_params *ap)
1364 if (ap->atavalid & ATA_FLAG_64_70) {
1365 if (ap->apiomodes & 2)
1367 if (ap->apiomodes & 1)
1370 if (ap->retired_piomode == 2)
1372 if (ap->retired_piomode == 1)
1374 if (ap->retired_piomode == 0)
1380 ata_wmode(struct ata_params *ap)
1382 if (ap->mwdmamodes & 0x04)
1384 if (ap->mwdmamodes & 0x02)
1386 if (ap->mwdmamodes & 0x01)
1392 ata_umode(struct ata_params *ap)
1394 if (ap->atavalid & ATA_FLAG_88) {
1395 if (ap->udmamodes & 0x40)
1397 if (ap->udmamodes & 0x20)
1399 if (ap->udmamodes & 0x10)
1401 if (ap->udmamodes & 0x08)
1403 if (ap->udmamodes & 0x04)
1405 if (ap->udmamodes & 0x02)
1407 if (ap->udmamodes & 0x01)
1414 bswap(int8_t *buf, int len)
1416 u_int16_t *ptr = (u_int16_t*)(buf + len);
1418 while (--ptr >= (u_int16_t*)buf)
1423 btrim(int8_t *buf, int len)
1427 for (ptr = buf; ptr < buf+len; ++ptr)
1430 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
1435 bpack(int8_t *src, int8_t *dst, int len)
1439 for (i = j = blank = 0 ; i < len; i++) {
1440 if (blank && src[i] == ' ') continue;
1441 if (blank && src[i] != ' ') {
1446 if (src[i] == ' ') {
1460 /* register controlling device */
1461 make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "ata");
1463 /* register boot attach to be run when interrupts are enabled */
1464 if (!(ata_delayed_attach = (struct intr_config_hook *)
1465 malloc(sizeof(struct intr_config_hook),
1466 M_TEMP, M_NOWAIT | M_ZERO))) {
1467 printf("ata: malloc of delayed attach hook failed\n");
1471 ata_delayed_attach->ich_func = (void*)ata_boot_attach;
1472 if (config_intrhook_establish(ata_delayed_attach) != 0) {
1473 printf("ata: config_intrhook_establish failed\n");
1474 free(ata_delayed_attach, M_TEMP);
1477 SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL)