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.38 2007/05/01 17:16:23 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 <sys/thread2.h>
54 #include <machine/stdarg.h>
55 #include <machine/clock.h>
60 #include "atapi-all.h"
64 struct atapi_request atapi;
67 /* device structures */
68 static d_ioctl_t ataioctl;
69 static struct dev_ops ata_ops = {
78 static void ata_boot_attach(void);
80 static void ata_intr(void *);
81 static int ata_getparam(struct ata_device *, u_int8_t);
82 static int ata_service(struct ata_channel *);
83 static void bswap(int8_t *, int);
84 static void btrim(int8_t *, int);
85 static void bpack(int8_t *, int8_t *, int);
86 static void ata_change_mode(struct ata_device *, int);
87 static u_int8_t ata_enclosure_sensor(struct ata_device *, int, u_int8_t, u_int8_t);
88 static int ata_enclosure_status(struct ata_device *, int *, int *, int *, int *);
91 SYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters");
93 int ata_mpipe_size = 4;
94 TUNABLE_INT("hw.ata.mpipe_size", &ata_mpipe_size);
95 SYSCTL_INT(_hw_ata, OID_AUTO, mpipe_size, CTLFLAG_RW, &ata_mpipe_size, 0,
96 "ATA global I/O pipeline max size");
100 devclass_t ata_devclass;
103 static MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer");
106 #define DEV_ATAPIALL NATAPICD > 0 || NATAPIFD > 0 || \
107 NATAPIST > 0 || NATAPICAM > 0
110 ata_probe(device_t dev)
112 struct ata_channel *ch;
115 if (!dev || !(ch = device_get_softc(dev)))
118 if (ch->r_io || ch->r_altio || ch->r_irq)
121 /* initialize the softc basics */
122 ch->active = ATA_IDLE;
125 rid = ATA_IOADDR_RID;
126 ch->r_io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
127 ATA_IOSIZE, RF_ACTIVE);
131 rid = ATA_ALTADDR_RID;
132 ch->r_altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
133 ATA_ALTIOSIZE, RF_ACTIVE);
137 rid = ATA_BMADDR_RID;
138 ch->r_bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
139 ATA_BMIOSIZE, RF_ACTIVE);
141 ata_printf(ch, -1, "iobase=0x%04x altiobase=0x%04x bmaddr=0x%04x\n",
142 (int)rman_get_start(ch->r_io),
143 (int)rman_get_start(ch->r_altio),
144 (ch->r_bmio) ? (int)rman_get_start(ch->r_bmio) : 0);
148 ch->device[MASTER].channel = ch;
149 ch->device[MASTER].unit = ATA_MASTER;
150 ch->device[MASTER].mode = ATA_PIO;
151 ch->device[SLAVE].channel = ch;
152 ch->device[SLAVE].unit = ATA_SLAVE;
153 ch->device[SLAVE].mode = ATA_PIO;
154 TAILQ_INIT(&ch->ata_queue);
155 TAILQ_INIT(&ch->atapi_queue);
157 mpipe_init(&ch->req_mpipe, M_ATA, sizeof(union ata_request),
158 4, ata_mpipe_size, 0, NULL);
159 mpipe_init(&ch->dma_mpipe, M_DEVBUF, PAGE_SIZE,
160 4, ata_mpipe_size, MPF_NOZERO, NULL);
166 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ch->r_io);
168 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ch->r_altio);
170 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, ch->r_bmio);
172 ata_printf(ch, -1, "probe allocation failed\n");
177 ata_attach(device_t dev)
179 struct ata_channel *ch;
182 if (!dev || !(ch = device_get_softc(dev)))
186 ch->r_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
187 RF_SHAREABLE | RF_ACTIVE);
189 ata_printf(ch, -1, "unable to allocate interrupt\n");
194 * Traditional ata registers are sensitive to when they can be accessed
195 * in the face of e.g. ongoing DMA. Do not allow the interrupt to be
198 if ((error = bus_setup_intr(dev, ch->r_irq, INTR_NOPOLL,
199 ata_intr, ch, &ch->ih, NULL))) {
200 ata_printf(ch, -1, "unable to setup interrupt\n");
205 * do not attach devices if we are in early boot, this is done later
206 * when interrupts are enabled by a hook into the boot process.
207 * otherwise attach what the probe has found in ch->devices.
211 if (ch->devices & ATA_ATA_SLAVE)
212 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
213 ch->devices &= ~ATA_ATA_SLAVE;
214 if (ch->devices & ATA_ATAPI_SLAVE)
215 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
216 ch->devices &= ~ATA_ATAPI_SLAVE;
217 if (ch->devices & ATA_ATA_MASTER)
218 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
219 ch->devices &= ~ATA_ATA_MASTER;
220 if (ch->devices & ATA_ATAPI_MASTER)
221 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
222 ch->devices &= ~ATA_ATAPI_MASTER;
224 if (ch->devices & ATA_ATA_MASTER)
225 ad_attach(&ch->device[MASTER], 0);
226 if (ch->devices & ATA_ATA_SLAVE)
227 ad_attach(&ch->device[SLAVE], 0);
230 if (ch->devices & ATA_ATAPI_MASTER)
231 atapi_attach(&ch->device[MASTER], 0);
232 if (ch->devices & ATA_ATAPI_SLAVE)
233 atapi_attach(&ch->device[SLAVE], 0);
236 atapi_cam_attach_bus(ch);
243 ata_detach(device_t dev)
245 struct ata_channel *ch;
247 if (!dev || !(ch = device_get_softc(dev)) ||
248 !ch->r_io || !ch->r_altio || !ch->r_irq)
251 /* make sure channel is not busy */
253 ATA_SLEEPLOCK_CH(ch, ATA_CONTROL);
255 if (ch->devices & ATA_ATA_MASTER && ch->device[MASTER].driver)
256 ad_detach(&ch->device[MASTER], 1);
257 if (ch->devices & ATA_ATA_SLAVE && ch->device[SLAVE].driver)
258 ad_detach(&ch->device[SLAVE], 1);
261 if (ch->devices & ATA_ATAPI_MASTER && ch->device[MASTER].driver)
262 atapi_detach(&ch->device[MASTER]);
263 if (ch->devices & ATA_ATAPI_SLAVE && ch->device[SLAVE].driver)
264 atapi_detach(&ch->device[SLAVE]);
267 atapi_cam_detach_bus(ch);
271 if (ch->device[MASTER].param) {
272 kfree(ch->device[MASTER].param, M_ATA);
273 ch->device[MASTER].param = NULL;
275 if (ch->device[SLAVE].param) {
276 kfree(ch->device[SLAVE].param, M_ATA);
277 ch->device[SLAVE].param = NULL;
279 ch->device[MASTER].driver = NULL;
280 ch->device[SLAVE].driver = NULL;
281 ch->device[MASTER].mode = ATA_PIO;
282 ch->device[SLAVE].mode = ATA_PIO;
286 bus_teardown_intr(dev, ch->r_irq, ch->ih);
287 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
289 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, ch->r_bmio);
290 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ch->r_altio);
291 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ch->r_io);
296 mpipe_done(&ch->req_mpipe);
297 mpipe_done(&ch->dma_mpipe);
304 ata_suspend(device_t dev)
306 struct ata_channel *ch;
308 if (dev == NULL || (ch = device_get_softc(dev)) == NULL)
311 /* wait for the channel to be IDLE or detached before suspending */
314 if (ch->active == ATA_IDLE) {
315 ch->active = ATA_CONTROL;
320 tsleep(ch, 0, "atasusp", hz / 10);
326 ata_resume(device_t dev)
328 return ata_reinit(device_get_softc(dev));
332 ataioctl(struct dev_ioctl_args *ap)
334 struct ata_cmd *iocmd = (struct ata_cmd *)ap->a_data;
335 struct ata_channel *ch;
336 device_t device = devclass_get_device(ata_devclass, iocmd->channel);
339 if (ap->a_cmd != IOCATA)
342 if (iocmd->channel < -1 || iocmd->device < -1 || iocmd->device > SLAVE)
345 switch (iocmd->cmd) {
347 /* should enable channel HW on controller that can SOS XXX */
348 error = ata_probe(device);
350 error = ata_attach(device);
354 error = ata_detach(device);
355 /* should disable channel HW on controller that can SOS XXX */
359 if (!device || !(ch = device_get_softc(device)))
361 crit_enter(); /* interlock non-atomic channel lock */
362 ATA_SLEEPLOCK_CH(ch, ATA_ACTIVE);
363 if ((error = ata_reinit(ch)))
369 if (!device || !(ch = device_get_softc(device)))
372 if ((iocmd->device == MASTER || iocmd->device == -1) &&
373 ch->device[MASTER].driver)
374 iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode;
376 iocmd->u.mode.mode[MASTER] = -1;
378 if ((iocmd->device == SLAVE || iocmd->device == -1) &&
379 ch->device[SLAVE].param)
380 iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode;
382 iocmd->u.mode.mode[SLAVE] = -1;
386 if (!device || !(ch = device_get_softc(device)))
389 if ((iocmd->device == MASTER || iocmd->device == -1) &&
390 iocmd->u.mode.mode[MASTER] >= 0 && ch->device[MASTER].param) {
391 ata_change_mode(&ch->device[MASTER],iocmd->u.mode.mode[MASTER]);
392 iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode;
395 iocmd->u.mode.mode[MASTER] = -1;
397 if ((iocmd->device == SLAVE || iocmd->device == -1) &&
398 iocmd->u.mode.mode[SLAVE] >= 0 && ch->device[SLAVE].param) {
399 ata_change_mode(&ch->device[SLAVE], iocmd->u.mode.mode[SLAVE]);
400 iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode;
403 iocmd->u.mode.mode[SLAVE] = -1;
407 if (!device || !(ch = device_get_softc(device)))
410 iocmd->u.param.type[MASTER] =
411 ch->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER);
412 iocmd->u.param.type[SLAVE] =
413 ch->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE);
415 if (ch->device[MASTER].name)
416 strcpy(iocmd->u.param.name[MASTER], ch->device[MASTER].name);
417 if (ch->device[SLAVE].name)
418 strcpy(iocmd->u.param.name[SLAVE], ch->device[SLAVE].name);
420 if (ch->device[MASTER].param)
421 bcopy(ch->device[MASTER].param, &iocmd->u.param.params[MASTER],
422 sizeof(struct ata_params));
423 if (ch->device[SLAVE].param)
424 bcopy(ch->device[SLAVE].param, &iocmd->u.param.params[SLAVE],
425 sizeof(struct ata_params));
429 struct ata_device *atadev;
431 if (!device || !(ch = device_get_softc(device)))
434 if (iocmd->device == SLAVE)
435 atadev = &ch->device[SLAVE];
437 atadev = &ch->device[MASTER];
439 return ata_enclosure_status(atadev,
440 &iocmd->u.enclosure.fan,
441 &iocmd->u.enclosure.temp,
442 &iocmd->u.enclosure.v05,
443 &iocmd->u.enclosure.v12);
448 return ata_raid_rebuild(iocmd->channel);
451 return ata_raid_create(&iocmd->u.raid_setup);
454 return ata_raid_delete(iocmd->channel);
457 return ata_raid_status(iocmd->channel, &iocmd->u.raid_status);
461 struct ata_device *atadev;
464 if (!device || !(ch = device_get_softc(device)))
467 if (!(atadev = &ch->device[iocmd->device]) ||
468 !(ch->devices & (iocmd->device == MASTER ?
469 ATA_ATAPI_MASTER : ATA_ATAPI_SLAVE)))
472 buf = kmalloc(iocmd->u.atapi.count, M_ATA, M_INTWAIT);
474 if (iocmd->u.atapi.flags & ATAPI_CMD_WRITE) {
475 error = copyin(iocmd->u.atapi.data, buf, iocmd->u.atapi.count);
479 error = atapi_queue_cmd(atadev, iocmd->u.atapi.ccb,
480 buf, iocmd->u.atapi.count,
481 (iocmd->u.atapi.flags == ATAPI_CMD_READ ?
482 ATPR_F_READ : 0) | ATPR_F_QUIET,
483 iocmd->u.atapi.timeout, NULL, NULL);
485 iocmd->u.atapi.error = error;
486 bcopy(&atadev->result, iocmd->u.atapi.sense_data,
487 sizeof(struct atapi_reqsense));
490 else if (iocmd->u.atapi.flags & ATAPI_CMD_READ)
491 error = copyout(buf, iocmd->u.atapi.data, iocmd->u.atapi.count);
504 ata_getparam(struct ata_device *atadev, u_int8_t command)
506 struct ata_params *ata_parm;
509 ata_parm = kmalloc(sizeof(struct ata_params), M_ATA, M_INTWAIT);
511 /* apparently some devices needs this repeated */
513 if (ata_command(atadev, command, 0, 0, 0, ATA_IMMEDIATE)) {
514 ata_prtdev(atadev, "%s identify failed\n",
515 command == ATA_C_ATAPI_IDENTIFY ? "ATAPI" : "ATA");
516 kfree(ata_parm, M_ATA);
520 ata_prtdev(atadev, "%s identify retries exceeded\n",
521 command == ATA_C_ATAPI_IDENTIFY ? "ATAPI" : "ATA");
522 kfree(ata_parm, M_ATA);
525 } while (ata_wait(atadev, ((command == ATA_C_ATAPI_IDENTIFY) ?
526 ATA_S_DRQ : (ATA_S_READY|ATA_S_DSC|ATA_S_DRQ))));
527 ATA_INSW(atadev->channel->r_io, ATA_DATA, (int16_t *)ata_parm,
528 sizeof(struct ata_params)/sizeof(int16_t));
530 if (command == ATA_C_ATA_IDENTIFY ||
531 !((ata_parm->model[0] == 'N' && ata_parm->model[1] == 'E') ||
532 (ata_parm->model[0] == 'F' && ata_parm->model[1] == 'X') ||
533 (ata_parm->model[0] == 'P' && ata_parm->model[1] == 'i')))
534 bswap(ata_parm->model, sizeof(ata_parm->model));
535 btrim(ata_parm->model, sizeof(ata_parm->model));
536 bpack(ata_parm->model, ata_parm->model, sizeof(ata_parm->model));
537 bswap(ata_parm->revision, sizeof(ata_parm->revision));
538 btrim(ata_parm->revision, sizeof(ata_parm->revision));
539 bpack(ata_parm->revision, ata_parm->revision, sizeof(ata_parm->revision));
540 bswap(ata_parm->serial, sizeof(ata_parm->serial));
541 btrim(ata_parm->serial, sizeof(ata_parm->serial));
542 bpack(ata_parm->serial, ata_parm->serial, sizeof(ata_parm->serial));
543 atadev->param = ata_parm;
550 ata_boot_attach(void)
552 struct ata_channel *ch;
558 * run through all ata devices and look for real ATA & ATAPI devices
559 * using the hints we found in the early probe, this avoids some of
560 * the delays probing of non-exsistent devices can cause.
562 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
563 if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
565 if (ch->devices & ATA_ATA_SLAVE)
566 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
567 ch->devices &= ~ATA_ATA_SLAVE;
568 if (ch->devices & ATA_ATAPI_SLAVE)
569 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
570 ch->devices &= ~ATA_ATAPI_SLAVE;
571 if (ch->devices & ATA_ATA_MASTER)
572 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
573 ch->devices &= ~ATA_ATA_MASTER;
574 if (ch->devices & ATA_ATAPI_MASTER)
575 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
576 ch->devices &= ~ATA_ATAPI_MASTER;
580 /* now we know whats there, do the real attach, first the ATA disks */
581 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
582 if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
584 if (ch->devices & ATA_ATA_MASTER)
585 ad_attach(&ch->device[MASTER], 0);
586 if (ch->devices & ATA_ATA_SLAVE)
587 ad_attach(&ch->device[SLAVE], 0);
592 /* then the atapi devices */
593 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
594 if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
596 if (ch->devices & ATA_ATAPI_MASTER)
597 atapi_attach(&ch->device[MASTER], 0);
598 if (ch->devices & ATA_ATAPI_SLAVE)
599 atapi_attach(&ch->device[SLAVE], 0);
601 atapi_cam_attach_bus(ch);
613 struct ata_channel *ch = (struct ata_channel *)data;
615 * on PCI systems we might share an interrupt line with another
616 * device or our twin ATA channel, so call ch->intr_func to figure
617 * out if it is really an interrupt we should process here
619 if (ch->intr_func && ch->intr_func(ch))
622 /* if drive is busy it didn't interrupt */
623 if (ATA_INB(ch->r_altio, ATA_ALTSTAT) & ATA_S_BUSY) {
625 if (!(ATA_INB(ch->r_altio, ATA_ALTSTAT) & ATA_S_DRQ))
629 /* clear interrupt and get status */
630 ch->status = ATA_INB(ch->r_io, ATA_STATUS);
632 if (ch->status & ATA_S_ERROR)
633 ch->error = ATA_INB(ch->r_io, ATA_ERROR);
635 /* find & call the responsible driver to process this interrupt */
636 switch (ch->active) {
639 if (!ch->running || ad_interrupt(ch->running) == ATA_OP_CONTINUES)
644 case ATA_ACTIVE_ATAPI:
645 if (!ch->running || atapi_interrupt(ch->running) == ATA_OP_CONTINUES)
650 case ATA_WAIT_INTR | ATA_CONTROL:
655 case ATA_WAIT_READY | ATA_CONTROL:
659 if (ch->flags & ATA_QUEUED) {
660 ch->active = ATA_ACTIVE;
661 if (ata_service(ch) == ATA_OP_CONTINUES)
669 static int intr_count = 0;
671 if (intr_count++ < 10)
672 ata_printf(ch, -1, "unwanted interrupt #%d active=%02x s=%02x\n",
673 intr_count, ch->active, ch->status);
678 ch->active &= ATA_CONTROL;
679 if (ch->active & ATA_CONTROL)
687 ata_start(struct ata_channel *ch)
690 struct ad_request *ad_request;
693 struct atapi_request *atapi_request;
696 crit_enter(); /* interlock non-atomic channel lock */
697 if (!ATA_LOCK_CH(ch, ATA_ACTIVE)) {
703 /* find & call the responsible driver if anything on the ATA queue */
704 if (TAILQ_EMPTY(&ch->ata_queue)) {
705 if (ch->devices & (ATA_ATA_MASTER) && ch->device[MASTER].driver)
706 ad_start(&ch->device[MASTER]);
707 if (ch->devices & (ATA_ATA_SLAVE) && ch->device[SLAVE].driver)
708 ad_start(&ch->device[SLAVE]);
710 if ((ad_request = TAILQ_FIRST(&ch->ata_queue))) {
711 TAILQ_REMOVE(&ch->ata_queue, ad_request, chain);
712 ch->active = ATA_ACTIVE_ATA;
713 ch->running = ad_request;
716 * The donecount had better be 0 here because the channel may not
717 * have retained the setup for the request (if a retry).
719 KKASSERT(ad_request->donecount == 0);
720 if (ad_transfer(ad_request) == ATA_OP_CONTINUES) {
728 /* find & call the responsible driver if anything on the ATAPI queue */
729 if (TAILQ_EMPTY(&ch->atapi_queue)) {
730 if (ch->devices & (ATA_ATAPI_MASTER) && ch->device[MASTER].driver)
731 atapi_start(&ch->device[MASTER]);
732 if (ch->devices & (ATA_ATAPI_SLAVE) && ch->device[SLAVE].driver)
733 atapi_start(&ch->device[SLAVE]);
735 if ((atapi_request = TAILQ_FIRST(&ch->atapi_queue))) {
736 TAILQ_REMOVE(&ch->atapi_queue, atapi_request, chain);
737 ch->active = ATA_ACTIVE_ATAPI;
738 ch->running = atapi_request;
739 if (atapi_transfer(atapi_request) == ATA_OP_CONTINUES) {
750 ata_reset(struct ata_channel *ch)
752 u_int8_t lsb, msb, ostat0, ostat1;
753 u_int8_t stat0 = 0, stat1 = 0;
754 int mask = 0, timeout;
756 /* do we have any signs of ATA/ATAPI HW being present ? */
757 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
759 ostat0 = ATA_INB(ch->r_io, ATA_STATUS);
760 if ((ostat0 & 0xf8) != 0xf8 && ostat0 != 0xa5) {
764 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
766 ostat1 = ATA_INB(ch->r_io, ATA_STATUS);
767 if ((ostat1 & 0xf8) != 0xf8 && ostat1 != 0xa5) {
776 /* in some setups we dont want to test for a slave */
777 if (ch->flags & ATA_NO_SLAVE) {
783 ata_printf(ch, -1, "mask=%02x ostat0=%02x ostat2=%02x\n",
784 mask, ostat0, ostat1);
787 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
789 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_RESET);
791 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_IDS);
793 ATA_INB(ch->r_io, ATA_ERROR);
795 /* wait for BUSY to go inactive */
796 for (timeout = 0; timeout < 3100; timeout++) {
797 if (stat0 & ATA_S_BUSY) {
798 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
801 /* check for ATAPI signature while its still there */
802 lsb = ATA_INB(ch->r_io, ATA_CYL_LSB);
803 msb = ATA_INB(ch->r_io, ATA_CYL_MSB);
804 stat0 = ATA_INB(ch->r_io, ATA_STATUS);
805 if (!(stat0 & ATA_S_BUSY)) {
807 ata_printf(ch, ATA_MASTER, "ATAPI %02x %02x\n", lsb, msb);
808 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB)
809 ch->devices |= ATA_ATAPI_MASTER;
812 if (stat1 & ATA_S_BUSY) {
813 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
816 /* check for ATAPI signature while its still there */
817 lsb = ATA_INB(ch->r_io, ATA_CYL_LSB);
818 msb = ATA_INB(ch->r_io, ATA_CYL_MSB);
819 stat1 = ATA_INB(ch->r_io, ATA_STATUS);
820 if (!(stat1 & ATA_S_BUSY)) {
822 ata_printf(ch, ATA_SLAVE, "ATAPI %02x %02x\n", lsb, msb);
823 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB)
824 ch->devices |= ATA_ATAPI_SLAVE;
827 if (mask == 0x01) /* wait for master only */
828 if (!(stat0 & ATA_S_BUSY))
830 if (mask == 0x02) /* wait for slave only */
831 if (!(stat1 & ATA_S_BUSY))
833 if (mask == 0x03) /* wait for both master & slave */
834 if (!(stat0 & ATA_S_BUSY) && !(stat1 & ATA_S_BUSY))
839 * some devices release BUSY before they are ready to accept commands.
840 * We must wait at least 50ms before attempting to issue a command after
844 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
846 if (stat0 & ATA_S_BUSY)
848 if (stat1 & ATA_S_BUSY)
851 ata_printf(ch, -1, "mask=%02x stat0=%02x stat1=%02x\n",
856 if (mask & 0x01 && ostat0 != 0x00 && !(ch->devices & ATA_ATAPI_MASTER)) {
857 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
859 ATA_OUTB(ch->r_io, ATA_ERROR, 0x58);
860 ATA_OUTB(ch->r_io, ATA_CYL_LSB, 0xa5);
861 lsb = ATA_INB(ch->r_io, ATA_ERROR);
862 msb = ATA_INB(ch->r_io, ATA_CYL_LSB);
864 ata_printf(ch, ATA_MASTER, "ATA %02x %02x\n", lsb, msb);
865 if (lsb != 0x58 && msb == 0xa5)
866 ch->devices |= ATA_ATA_MASTER;
868 if (mask & 0x02 && ostat1 != 0x00 && !(ch->devices & ATA_ATAPI_SLAVE)) {
869 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
871 ATA_OUTB(ch->r_io, ATA_ERROR, 0x58);
872 ATA_OUTB(ch->r_io, ATA_CYL_LSB, 0xa5);
873 lsb = ATA_INB(ch->r_io, ATA_ERROR);
874 msb = ATA_INB(ch->r_io, ATA_CYL_LSB);
876 ata_printf(ch, ATA_SLAVE, "ATA %02x %02x\n", lsb, msb);
877 if (lsb != 0x58 && msb == 0xa5)
878 ch->devices |= ATA_ATA_SLAVE;
881 ata_printf(ch, -1, "devices=%02x\n", ch->devices);
885 ata_reinit(struct ata_channel *ch)
887 int devices, misdev, newdev;
889 if (!ch->r_io || !ch->r_altio || !ch->r_irq)
892 ATA_FORCELOCK_CH(ch, ATA_CONTROL);
894 devices = ch->devices;
895 ata_printf(ch, -1, "resetting devices .. ");
898 if ((misdev = devices & ~ch->devices)) {
902 if (misdev & ATA_ATA_MASTER && ch->device[MASTER].driver)
903 ad_detach(&ch->device[MASTER], 0);
904 if (misdev & ATA_ATA_SLAVE && ch->device[SLAVE].driver)
905 ad_detach(&ch->device[SLAVE], 0);
908 if (misdev & ATA_ATAPI_MASTER && ch->device[MASTER].driver)
909 atapi_detach(&ch->device[MASTER]);
910 if (misdev & ATA_ATAPI_SLAVE && ch->device[SLAVE].driver)
911 atapi_detach(&ch->device[SLAVE]);
913 if (misdev & ATA_ATA_MASTER || misdev & ATA_ATAPI_MASTER) {
914 if (ch->device[MASTER].param)
915 kfree(ch->device[MASTER].param, M_ATA);
916 ch->device[MASTER].param = NULL;
918 if (misdev & ATA_ATA_SLAVE || misdev & ATA_ATAPI_SLAVE) {
919 if (ch->device[SLAVE].param)
920 kfree(ch->device[SLAVE].param, M_ATA);
921 ch->device[SLAVE].param = NULL;
924 if ((newdev = ~devices & ch->devices)) {
925 if (newdev & ATA_ATA_MASTER)
926 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
927 ch->devices &= ~ATA_ATA_MASTER;
928 if (newdev & ATA_ATA_SLAVE)
929 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
930 ch->devices &= ~ATA_ATA_SLAVE;
931 if (newdev & ATA_ATAPI_MASTER)
932 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
933 ch->devices &= ~ATA_ATAPI_MASTER;
934 if (newdev & ATA_ATAPI_SLAVE)
935 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
936 ch->devices &= ~ATA_ATAPI_SLAVE;
938 newdev = ~devices & ch->devices;
939 if (!misdev && newdev)
942 if (newdev & ATA_ATA_MASTER && !ch->device[MASTER].driver)
943 ad_attach(&ch->device[MASTER], 1);
944 else if (ch->devices & ATA_ATA_MASTER && ch->device[MASTER].driver) {
945 ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY);
946 ad_reinit(&ch->device[MASTER]);
948 if (newdev & ATA_ATA_SLAVE && !ch->device[SLAVE].driver)
949 ad_attach(&ch->device[SLAVE], 1);
950 else if (ch->devices & (ATA_ATA_SLAVE) && ch->device[SLAVE].driver) {
951 ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY);
952 ad_reinit(&ch->device[SLAVE]);
956 if (newdev & ATA_ATAPI_MASTER && !ch->device[MASTER].driver)
957 atapi_attach(&ch->device[MASTER], 1);
958 else if (ch->devices & (ATA_ATAPI_MASTER) && ch->device[MASTER].driver) {
959 ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY);
960 atapi_reinit(&ch->device[MASTER]);
962 if (newdev & ATA_ATAPI_SLAVE && !ch->device[SLAVE].driver)
963 atapi_attach(&ch->device[SLAVE], 1);
964 else if (ch->devices & (ATA_ATAPI_SLAVE) && ch->device[SLAVE].driver) {
965 ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY);
966 atapi_reinit(&ch->device[SLAVE]);
970 if (ch->devices & (ATA_ATAPI_MASTER | ATA_ATAPI_SLAVE))
971 atapi_cam_reinit_bus(ch);
980 ata_service(struct ata_channel *ch)
982 /* do we have a SERVICE request from the drive ? */
983 if ((ch->status & (ATA_S_SERVICE|ATA_S_ERROR|ATA_S_DRQ)) == ATA_S_SERVICE) {
984 ATA_OUTB(ch->r_bmio, ATA_BMSTAT_PORT,
985 ata_dmastatus(ch) | ATA_BMSTAT_INTERRUPT);
987 if ((ATA_INB(ch->r_io, ATA_DRIVE) & ATA_SLAVE) == ATA_MASTER) {
988 if ((ch->devices & ATA_ATA_MASTER) && ch->device[MASTER].driver)
989 return ad_service((struct ad_softc *)
990 ch->device[MASTER].driver, 0);
993 if ((ch->devices & ATA_ATA_SLAVE) && ch->device[SLAVE].driver)
994 return ad_service((struct ad_softc *)
995 ch->device[SLAVE].driver, 0);
999 return ATA_OP_FINISHED;
1003 ata_wait(struct ata_device *atadev, u_int8_t mask)
1008 while (timeout < 5000000) { /* timeout 5 secs */
1009 atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS);
1011 /* if drive fails status, reselect the drive just to be sure */
1012 if (atadev->channel->status == 0xff) {
1013 ata_prtdev(atadev, "no status, reselecting device\n");
1014 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM|atadev->unit);
1016 atadev->channel->status = ATA_INB(atadev->channel->r_io,ATA_STATUS);
1017 if (atadev->channel->status == 0xff)
1022 if (!(atadev->channel->status & ATA_S_BUSY))
1025 if (timeout > 1000) {
1034 if (atadev->channel->status & ATA_S_ERROR)
1035 atadev->channel->error = ATA_INB(atadev->channel->r_io, ATA_ERROR);
1036 if (timeout >= 5000000)
1039 return (atadev->channel->status & ATA_S_ERROR);
1041 /* Wait 50 msec for bits wanted. */
1044 atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS);
1045 if ((atadev->channel->status & mask) == mask) {
1046 if (atadev->channel->status & ATA_S_ERROR)
1047 atadev->channel->error=ATA_INB(atadev->channel->r_io,ATA_ERROR);
1048 return (atadev->channel->status & ATA_S_ERROR);
1056 ata_command(struct ata_device *atadev, u_int8_t command,
1057 u_int64_t lba, u_int16_t count, u_int8_t feature, int flags)
1061 ata_prtdev(atadev, "ata_command: addr=%04lx, cmd=%02x, "
1062 "lba=%lld, count=%d, feature=%d, flags=%02x\n",
1063 rman_get_start(atadev->channel->r_io),
1064 command, lba, count, feature, flags);
1068 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1070 /* disable interrupt from device */
1071 if (atadev->channel->flags & ATA_QUEUED)
1072 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_4BIT);
1074 /* ready to issue command ? */
1075 if (ata_wait(atadev, 0) < 0) {
1076 ata_prtdev(atadev, "timeout sending command=%02x s=%02x e=%02x\n",
1077 command, atadev->channel->status, atadev->channel->error);
1081 /* only use 48bit addressing if needed because of the overhead */
1082 if ((lba >= 268435455 || count > 256) && atadev->param &&
1083 atadev->param->support.address48) {
1084 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, (feature>>8) & 0xff);
1085 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, feature);
1086 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, (count>>8) & 0xff);
1087 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, count & 0xff);
1088 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, (lba>>24) & 0xff);
1089 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, lba & 0xff);
1090 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>32) & 0xff);
1091 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>8) & 0xff);
1092 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>40) & 0xff);
1093 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>16) & 0xff);
1094 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_LBA | atadev->unit);
1096 /* translate command into 48bit version */
1099 command = ATA_C_READ48; break;
1100 case ATA_C_READ_MUL:
1101 command = ATA_C_READ_MUL48; break;
1102 case ATA_C_READ_DMA:
1103 command = ATA_C_READ_DMA48; break;
1104 case ATA_C_READ_DMA_QUEUED:
1105 command = ATA_C_READ_DMA_QUEUED48; break;
1107 command = ATA_C_WRITE48; break;
1108 case ATA_C_WRITE_MUL:
1109 command = ATA_C_WRITE_MUL48; break;
1110 case ATA_C_WRITE_DMA:
1111 command = ATA_C_WRITE_DMA48; break;
1112 case ATA_C_WRITE_DMA_QUEUED:
1113 command = ATA_C_WRITE_DMA_QUEUED48; break;
1114 case ATA_C_FLUSHCACHE:
1115 command = ATA_C_FLUSHCACHE48; break;
1117 ata_prtdev(atadev, "can't translate cmd to 48bit version\n");
1122 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, feature);
1123 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, count);
1124 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, lba & 0xff);
1125 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>8) & 0xff);
1126 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>16) & 0xff);
1127 if (atadev->flags & ATA_D_USE_CHS)
1128 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE,
1129 ATA_D_IBM | atadev->unit | ((lba>>24) & 0xf));
1131 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE,
1132 ATA_D_IBM | ATA_D_LBA | atadev->unit | ((lba>>24) &0xf));
1135 switch (flags & ATA_WAIT_MASK) {
1137 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1139 /* enable interrupt */
1140 if (atadev->channel->flags & ATA_QUEUED)
1141 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
1145 atadev->channel->active |= ATA_WAIT_INTR;
1146 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1148 /* enable interrupt */
1149 if (atadev->channel->flags & ATA_QUEUED)
1150 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
1152 if (tsleep((caddr_t)atadev->channel, 0, "atacmd", 10 * hz)) {
1153 ata_prtdev(atadev, "timeout waiting for interrupt\n");
1154 atadev->channel->active &= ~ATA_WAIT_INTR;
1159 case ATA_WAIT_READY:
1160 atadev->channel->active |= ATA_WAIT_READY;
1161 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1162 if (ata_wait(atadev, ATA_S_READY) < 0) {
1163 ata_prtdev(atadev, "timeout waiting for cmd=%02x s=%02x e=%02x\n",
1164 command, atadev->channel->status,atadev->channel->error);
1167 atadev->channel->active &= ~ATA_WAIT_READY;
1174 ata_enclosure_start(struct ata_device *atadev)
1176 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1178 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1180 ATA_INB(atadev->channel->r_io, ATA_CMD);
1182 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1184 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1186 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1188 ATA_INB(atadev->channel->r_io, ATA_COUNT);
1190 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1195 ata_enclosure_end(struct ata_device *atadev)
1197 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1202 ata_enclosure_chip_start(struct ata_device *atadev)
1204 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0b);
1205 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0a);
1207 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08);
1211 ata_enclosure_chip_end(struct ata_device *atadev)
1213 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08);
1215 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0a);
1217 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0b);
1222 ata_enclosure_chip_rdbit(struct ata_device *atadev)
1226 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0);
1228 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x02);
1230 val = ATA_INB(atadev->channel->r_io, ATA_SECTOR) & 0x01;
1236 ata_enclosure_chip_wrbit(struct ata_device *atadev, u_int8_t data)
1238 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08 | (data & 0x01));
1240 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08 | 0x02 | (data & 0x01));
1245 ata_enclosure_chip_rw(struct ata_device *atadev, int rw, u_int8_t val)
1250 for (i = 0; i < 8; i++)
1251 ata_enclosure_chip_wrbit(atadev, (val & (0x80 >> i)) ? 1 : 0);
1254 for (i = 0; i < 8; i++)
1255 val = (val << 1) | ata_enclosure_chip_rdbit(atadev);
1257 ata_enclosure_chip_wrbit(atadev, 0);
1262 ata_enclosure_sensor(struct ata_device *atadev,
1263 int rw, u_int8_t idx, u_int8_t data)
1265 ata_enclosure_start(atadev);
1266 ata_enclosure_chip_start(atadev);
1267 ata_enclosure_chip_rw(atadev, 1, 0x5a);
1268 ata_enclosure_chip_rw(atadev, 1, idx);
1270 ata_enclosure_chip_rw(atadev, 1, data);
1273 ata_enclosure_chip_end(atadev);
1274 ata_enclosure_chip_start(atadev);
1275 ata_enclosure_chip_rw(atadev, 1, 0x5b);
1276 data = ata_enclosure_chip_rw(atadev, 0, 0);
1278 ata_enclosure_chip_end(atadev);
1279 ata_enclosure_end(atadev);
1284 ata_enclosure_status(struct ata_device *atadev,
1285 int *fan, int *temp, int *v05, int *v12)
1287 u_int8_t id1, id2, cnt, div;
1290 if (atadev->flags & ATA_D_ENC_PRESENT) {
1291 ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL);
1292 ata_enclosure_sensor(atadev, 1, 0x4e, 0);
1293 id1 = ata_enclosure_sensor(atadev, 0, 0x4f, 0);
1294 ata_enclosure_sensor(atadev, 1, 0x4e, 0x80);
1295 id2 = ata_enclosure_sensor(atadev, 0, 0x4f, 0);
1296 if (id1 == 0xa3 && id2 == 0x5c) {
1297 div = 1 << (((ata_enclosure_sensor(atadev, 0, 0x5d, 0)&0x20)>>3)+
1298 ((ata_enclosure_sensor(atadev, 0, 0x47, 0)&0x30)>>4)+1);
1299 cnt = ata_enclosure_sensor(atadev, 0, 0x28, 0);
1303 *fan = 1350000 / cnt / div;
1304 ata_enclosure_sensor(atadev, 1, 0x4e, 0x01);
1305 *temp = (ata_enclosure_sensor(atadev, 0, 0x50, 0) * 10) +
1306 (ata_enclosure_sensor(atadev, 0, 0x50, 0) & 0x80 ? 5 : 0);
1307 *v05 = ata_enclosure_sensor(atadev, 0, 0x23, 0) * 27;
1308 *v12 = ata_enclosure_sensor(atadev, 0, 0x24, 0) * 61;
1311 ATA_UNLOCK_CH(atadev->channel);
1317 ata_enclosure_print(struct ata_device *atadev)
1320 int fan, temp, v05, v12;
1322 ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL);
1323 ata_enclosure_start(atadev);
1324 id = ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1326 st = ATA_INB(atadev->channel->r_io, ATA_COUNT);
1328 ata_enclosure_end(atadev);
1329 ATA_UNLOCK_CH(atadev->channel);
1331 switch (id & 0x93) {
1333 ata_prtdev(atadev, "Universal enclosure");
1336 ata_prtdev(atadev, "FastSwap enclosure");
1340 ata_prtdev(atadev, "SuperSwap enclosure");
1343 atadev->flags &= ~ATA_D_ENC_PRESENT;
1346 atadev->flags |= ATA_D_ENC_PRESENT;
1348 if (ata_enclosure_status(atadev, &fan, &temp, &v05, &v12))
1349 kprintf(" detected\n");
1351 kprintf(" [FAN:%drpm TEMP:%d.%01dC %d.%03dV %d.%03dV]\n",
1352 fan, temp/10, temp%10, v05/1000, v05%1000, v12/1000, v12%1000);
1356 ata_enclosure_leds(struct ata_device *atadev, u_int8_t color)
1358 if (atadev->flags & ATA_D_ENC_PRESENT) {
1361 ata_enclosure_start(atadev);
1362 reg = ATA_INB(atadev->channel->r_io, ATA_COUNT);
1364 ATA_OUTB(atadev->channel->r_io, ATA_COUNT,
1365 (color & ATA_LED_MASK) | (reg & ~ATA_LED_MASK));
1367 ata_enclosure_end(atadev);
1372 ata_change_mode(struct ata_device *atadev, int mode)
1374 int umode, wmode, pmode;
1376 umode = ata_umode(atadev->param);
1377 wmode = ata_wmode(atadev->param);
1378 pmode = ata_pmode(atadev->param);
1380 switch (mode & ATA_DMA_MASK) {
1382 if ((mode & ATA_MODE_MASK) < umode)
1383 umode = mode & ATA_MODE_MASK;
1386 if ((mode & ATA_MODE_MASK) < wmode)
1387 wmode = mode & ATA_MODE_MASK;
1391 if (((mode & ATA_MODE_MASK) - ATA_PIO0) < pmode)
1392 pmode = (mode & ATA_MODE_MASK) - ATA_PIO0;
1397 crit_enter(); /* interlock non-atomic channel lock */
1398 ATA_SLEEPLOCK_CH(atadev->channel, ATA_ACTIVE);
1399 ata_dmainit(atadev, pmode, wmode, umode);
1400 ATA_UNLOCK_CH(atadev->channel);
1402 ata_start(atadev->channel); /* XXX SOS */
1406 ata_printf(struct ata_channel *ch, int device, const char * fmt, ...)
1412 ret = kprintf("ata%d: ", device_get_unit(ch->dev));
1414 if (ch->device[ATA_DEV(device)].name)
1415 ret = kprintf("%s: ", ch->device[ATA_DEV(device)].name);
1417 ret = kprintf("ata%d-%s: ", device_get_unit(ch->dev),
1418 (device == ATA_MASTER) ? "master" : "slave");
1420 __va_start(ap, fmt);
1421 ret += kvprintf(fmt, ap);
1427 ata_prtdev(struct ata_device *atadev, const char * fmt, ...)
1433 ret = kprintf("%s: ", atadev->name);
1435 ret = kprintf("ata%d-%s: ", device_get_unit(atadev->channel->dev),
1436 (atadev->unit == ATA_MASTER) ? "master" : "slave");
1437 __va_start(ap, fmt);
1438 ret += kvprintf(fmt, ap);
1444 ata_set_name(struct ata_device *atadev, char *name, int lun)
1446 atadev->name = kmalloc(strlen(name) + 4, M_ATA, M_INTWAIT);
1447 ksprintf(atadev->name, "%s%d", name, lun);
1451 ata_free_name(struct ata_device *atadev)
1454 kfree(atadev->name, M_ATA);
1455 atadev->name = NULL;
1459 ata_get_lun(u_int32_t *map)
1461 int lun = ffs(~*map) - 1;
1468 ata_test_lun(u_int32_t *map, int lun)
1470 return (*map & (1 << lun));
1474 ata_free_lun(u_int32_t *map, int lun)
1476 *map &= ~(1 << lun);
1480 ata_mode2str(int mode)
1483 case ATA_PIO: return "BIOSPIO";
1484 case ATA_PIO0: return "PIO0";
1485 case ATA_PIO1: return "PIO1";
1486 case ATA_PIO2: return "PIO2";
1487 case ATA_PIO3: return "PIO3";
1488 case ATA_PIO4: return "PIO4";
1489 case ATA_DMA: return "BIOSDMA";
1490 case ATA_WDMA2: return "WDMA2";
1491 case ATA_UDMA2: return "UDMA33";
1492 case ATA_UDMA4: return "UDMA66";
1493 case ATA_UDMA5: return "UDMA100";
1494 case ATA_UDMA6: return "UDMA133";
1495 default: return "???";
1500 ata_pmode(struct ata_params *ap)
1502 if (ap->atavalid & ATA_FLAG_64_70) {
1503 if (ap->apiomodes & 2)
1505 if (ap->apiomodes & 1)
1508 if (ap->retired_piomode == 2)
1510 if (ap->retired_piomode == 1)
1512 if (ap->retired_piomode == 0)
1518 ata_wmode(struct ata_params *ap)
1520 if (ap->mwdmamodes & 0x04)
1522 if (ap->mwdmamodes & 0x02)
1524 if (ap->mwdmamodes & 0x01)
1530 ata_umode(struct ata_params *ap)
1532 if (ap->atavalid & ATA_FLAG_88) {
1533 if (ap->udmamodes & 0x40)
1535 if (ap->udmamodes & 0x20)
1537 if (ap->udmamodes & 0x10)
1539 if (ap->udmamodes & 0x08)
1541 if (ap->udmamodes & 0x04)
1543 if (ap->udmamodes & 0x02)
1545 if (ap->udmamodes & 0x01)
1552 bswap(int8_t *buf, int len)
1554 u_int16_t *ptr = (u_int16_t*)(buf + len);
1556 while (--ptr >= (u_int16_t*)buf)
1561 btrim(int8_t *buf, int len)
1565 for (ptr = buf; ptr < buf+len; ++ptr)
1568 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
1573 bpack(int8_t *src, int8_t *dst, int len)
1577 for (i = j = blank = 0 ; i < len; i++) {
1578 if (blank && src[i] == ' ') continue;
1579 if (blank && src[i] != ' ') {
1584 if (src[i] == ' ') {
1598 make_dev(&ata_ops, 0, UID_ROOT, GID_OPERATOR, 0600, "ata");
1601 SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL)