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 $
32 #include "use_atadisk.h"
33 #include "use_atapicd.h"
34 #include "use_atapifd.h"
35 #include "use_atapist.h"
36 #include "use_atapicam.h"
37 #include <sys/param.h>
38 #include <sys/systm.h>
40 #include <sys/kernel.h>
43 #include <sys/module.h>
46 #include <sys/malloc.h>
47 #include <sys/devicestat.h>
48 #include <sys/sysctl.h>
49 #include <sys/thread2.h>
52 #include <machine/stdarg.h>
53 #include <machine/clock.h>
58 #include "atapi-all.h"
62 struct atapi_request atapi;
65 /* device structures */
66 static d_ioctl_t ataioctl;
67 static struct dev_ops ata_ops = {
76 static void ata_boot_attach(void);
78 static void ata_intr(void *);
79 static int ata_getparam(struct ata_device *, u_int8_t);
80 static int ata_service(struct ata_channel *);
81 static void bswap(int8_t *, int);
82 static void btrim(int8_t *, int);
83 static void bpack(int8_t *, int8_t *, int);
84 static void ata_change_mode(struct ata_device *, int);
85 static u_int8_t ata_enclosure_sensor(struct ata_device *, int, u_int8_t, u_int8_t);
86 static int ata_enclosure_status(struct ata_device *, int *, int *, int *, int *);
89 SYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters");
91 int ata_mpipe_size = 4;
92 TUNABLE_INT("hw.ata.mpipe_size", &ata_mpipe_size);
93 SYSCTL_INT(_hw_ata, OID_AUTO, mpipe_size, CTLFLAG_RW, &ata_mpipe_size, 0,
94 "ATA global I/O pipeline max size");
98 devclass_t ata_devclass;
101 static MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer");
104 #define DEV_ATAPIALL NATAPICD > 0 || NATAPIFD > 0 || \
105 NATAPIST > 0 || NATAPICAM > 0
108 ata_probe(device_t dev)
110 struct ata_channel *ch;
113 if (!dev || !(ch = device_get_softc(dev)))
116 if (ch->r_io || ch->r_altio || ch->r_irq)
119 /* initialize the softc basics */
120 ch->active = ATA_IDLE;
123 rid = ATA_IOADDR_RID;
124 ch->r_io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
125 ATA_IOSIZE, RF_ACTIVE);
129 rid = ATA_ALTADDR_RID;
130 ch->r_altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
131 ATA_ALTIOSIZE, RF_ACTIVE);
135 rid = ATA_BMADDR_RID;
136 ch->r_bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
137 ATA_BMIOSIZE, RF_ACTIVE);
139 ata_printf(ch, -1, "iobase=0x%04x altiobase=0x%04x bmaddr=0x%04x\n",
140 (int)rman_get_start(ch->r_io),
141 (int)rman_get_start(ch->r_altio),
142 (ch->r_bmio) ? (int)rman_get_start(ch->r_bmio) : 0);
146 ch->device[MASTER].channel = ch;
147 ch->device[MASTER].unit = ATA_MASTER;
148 ch->device[MASTER].mode = ATA_PIO;
149 ch->device[SLAVE].channel = ch;
150 ch->device[SLAVE].unit = ATA_SLAVE;
151 ch->device[SLAVE].mode = ATA_PIO;
152 TAILQ_INIT(&ch->ata_queue);
153 TAILQ_INIT(&ch->atapi_queue);
155 mpipe_init(&ch->req_mpipe, M_ATA, sizeof(union ata_request),
156 4, ata_mpipe_size, 0, NULL, NULL, NULL);
157 mpipe_init(&ch->dma_mpipe, M_DEVBUF, PAGE_SIZE,
158 4, ata_mpipe_size, MPF_NOZERO, NULL, NULL, NULL);
164 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ch->r_io);
166 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ch->r_altio);
168 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, ch->r_bmio);
170 ata_printf(ch, -1, "probe allocation failed\n");
175 ata_attach(device_t dev)
177 struct ata_channel *ch;
180 if (!dev || !(ch = device_get_softc(dev)))
184 ch->r_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
185 RF_SHAREABLE | RF_ACTIVE);
187 ata_printf(ch, -1, "unable to allocate interrupt\n");
192 * Traditional ata registers are sensitive to when they can be accessed
193 * in the face of e.g. ongoing DMA. Do not allow the interrupt to be
196 if ((error = bus_setup_intr(dev, ch->r_irq, INTR_NOPOLL,
197 ata_intr, ch, &ch->ih, NULL))) {
198 ata_printf(ch, -1, "unable to setup interrupt\n");
203 * do not attach devices if we are in early boot, this is done later
204 * when interrupts are enabled by a hook into the boot process.
205 * otherwise attach what the probe has found in ch->devices.
209 if (ch->devices & ATA_ATA_SLAVE)
210 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
211 ch->devices &= ~ATA_ATA_SLAVE;
212 if (ch->devices & ATA_ATAPI_SLAVE)
213 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
214 ch->devices &= ~ATA_ATAPI_SLAVE;
215 if (ch->devices & ATA_ATA_MASTER)
216 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
217 ch->devices &= ~ATA_ATA_MASTER;
218 if (ch->devices & ATA_ATAPI_MASTER)
219 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
220 ch->devices &= ~ATA_ATAPI_MASTER;
222 if (ch->devices & ATA_ATA_MASTER)
223 ad_attach(&ch->device[MASTER], 0);
224 if (ch->devices & ATA_ATA_SLAVE)
225 ad_attach(&ch->device[SLAVE], 0);
228 if (ch->devices & ATA_ATAPI_MASTER)
229 atapi_attach(&ch->device[MASTER], 0);
230 if (ch->devices & ATA_ATAPI_SLAVE)
231 atapi_attach(&ch->device[SLAVE], 0);
234 atapi_cam_attach_bus(ch);
241 ata_detach(device_t dev)
243 struct ata_channel *ch;
245 if (!dev || !(ch = device_get_softc(dev)) ||
246 !ch->r_io || !ch->r_altio || !ch->r_irq)
249 /* make sure channel is not busy */
251 ATA_SLEEPLOCK_CH(ch, ATA_CONTROL);
253 if (ch->devices & ATA_ATA_MASTER && ch->device[MASTER].driver)
254 ad_detach(&ch->device[MASTER], 1);
255 if (ch->devices & ATA_ATA_SLAVE && ch->device[SLAVE].driver)
256 ad_detach(&ch->device[SLAVE], 1);
259 if (ch->devices & ATA_ATAPI_MASTER && ch->device[MASTER].driver)
260 atapi_detach(&ch->device[MASTER]);
261 if (ch->devices & ATA_ATAPI_SLAVE && ch->device[SLAVE].driver)
262 atapi_detach(&ch->device[SLAVE]);
265 atapi_cam_detach_bus(ch);
269 if (ch->device[MASTER].param) {
270 kfree(ch->device[MASTER].param, M_ATA);
271 ch->device[MASTER].param = NULL;
273 if (ch->device[SLAVE].param) {
274 kfree(ch->device[SLAVE].param, M_ATA);
275 ch->device[SLAVE].param = NULL;
277 ch->device[MASTER].driver = NULL;
278 ch->device[SLAVE].driver = NULL;
279 ch->device[MASTER].mode = ATA_PIO;
280 ch->device[SLAVE].mode = ATA_PIO;
284 bus_teardown_intr(dev, ch->r_irq, ch->ih);
285 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
287 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, ch->r_bmio);
288 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ch->r_altio);
289 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ch->r_io);
294 mpipe_done(&ch->req_mpipe);
295 mpipe_done(&ch->dma_mpipe);
302 ata_suspend(device_t dev)
304 struct ata_channel *ch;
306 if (dev == NULL || (ch = device_get_softc(dev)) == NULL)
309 /* wait for the channel to be IDLE or detached before suspending */
312 if (ch->active == ATA_IDLE) {
313 ch->active = ATA_CONTROL;
318 tsleep(ch, 0, "atasusp", hz / 10);
324 ata_resume(device_t dev)
326 return ata_reinit(device_get_softc(dev));
330 ataioctl(struct dev_ioctl_args *ap)
332 struct ata_cmd *iocmd = (struct ata_cmd *)ap->a_data;
333 struct ata_channel *ch;
334 device_t device = devclass_get_device(ata_devclass, iocmd->channel);
337 if (ap->a_cmd != IOCATA)
340 if (iocmd->channel < -1 || iocmd->device < -1 || iocmd->device > SLAVE)
343 switch (iocmd->cmd) {
345 /* should enable channel HW on controller that can SOS XXX */
346 error = ata_probe(device);
348 error = ata_attach(device);
352 error = ata_detach(device);
353 /* should disable channel HW on controller that can SOS XXX */
357 if (!device || !(ch = device_get_softc(device)))
359 crit_enter(); /* interlock non-atomic channel lock */
360 ATA_SLEEPLOCK_CH(ch, ATA_ACTIVE);
361 if ((error = ata_reinit(ch)))
367 if (!device || !(ch = device_get_softc(device)))
370 if ((iocmd->device == MASTER || iocmd->device == -1) &&
371 ch->device[MASTER].driver)
372 iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode;
374 iocmd->u.mode.mode[MASTER] = -1;
376 if ((iocmd->device == SLAVE || iocmd->device == -1) &&
377 ch->device[SLAVE].param)
378 iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode;
380 iocmd->u.mode.mode[SLAVE] = -1;
384 if (!device || !(ch = device_get_softc(device)))
387 if ((iocmd->device == MASTER || iocmd->device == -1) &&
388 iocmd->u.mode.mode[MASTER] >= 0 && ch->device[MASTER].param) {
389 ata_change_mode(&ch->device[MASTER],iocmd->u.mode.mode[MASTER]);
390 iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode;
393 iocmd->u.mode.mode[MASTER] = -1;
395 if ((iocmd->device == SLAVE || iocmd->device == -1) &&
396 iocmd->u.mode.mode[SLAVE] >= 0 && ch->device[SLAVE].param) {
397 ata_change_mode(&ch->device[SLAVE], iocmd->u.mode.mode[SLAVE]);
398 iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode;
401 iocmd->u.mode.mode[SLAVE] = -1;
405 if (!device || !(ch = device_get_softc(device)))
408 iocmd->u.param.type[MASTER] =
409 ch->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER);
410 iocmd->u.param.type[SLAVE] =
411 ch->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE);
413 if (ch->device[MASTER].name)
414 strcpy(iocmd->u.param.name[MASTER], ch->device[MASTER].name);
415 if (ch->device[SLAVE].name)
416 strcpy(iocmd->u.param.name[SLAVE], ch->device[SLAVE].name);
418 if (ch->device[MASTER].param)
419 bcopy(ch->device[MASTER].param, &iocmd->u.param.params[MASTER],
420 sizeof(struct ata_params));
421 if (ch->device[SLAVE].param)
422 bcopy(ch->device[SLAVE].param, &iocmd->u.param.params[SLAVE],
423 sizeof(struct ata_params));
427 struct ata_device *atadev;
429 if (!device || !(ch = device_get_softc(device)))
432 if (iocmd->device == SLAVE)
433 atadev = &ch->device[SLAVE];
435 atadev = &ch->device[MASTER];
437 return ata_enclosure_status(atadev,
438 &iocmd->u.enclosure.fan,
439 &iocmd->u.enclosure.temp,
440 &iocmd->u.enclosure.v05,
441 &iocmd->u.enclosure.v12);
446 return ata_raid_rebuild(iocmd->channel);
449 return ata_raid_create(&iocmd->u.raid_setup);
452 return ata_raid_delete(iocmd->channel);
455 return ata_raid_status(iocmd->channel, &iocmd->u.raid_status);
459 struct ata_device *atadev;
462 if (!device || !(ch = device_get_softc(device)))
465 if (!(atadev = &ch->device[iocmd->device]) ||
466 !(ch->devices & (iocmd->device == MASTER ?
467 ATA_ATAPI_MASTER : ATA_ATAPI_SLAVE)))
470 buf = kmalloc(iocmd->u.atapi.count, M_ATA, M_INTWAIT);
472 if (iocmd->u.atapi.flags & ATAPI_CMD_WRITE) {
473 error = copyin(iocmd->u.atapi.data, buf, iocmd->u.atapi.count);
477 error = atapi_queue_cmd(atadev, iocmd->u.atapi.ccb,
478 buf, iocmd->u.atapi.count,
479 (iocmd->u.atapi.flags == ATAPI_CMD_READ ?
480 ATPR_F_READ : 0) | ATPR_F_QUIET,
481 iocmd->u.atapi.timeout, NULL, NULL);
483 iocmd->u.atapi.error = error;
484 bcopy(&atadev->result, iocmd->u.atapi.sense_data,
485 sizeof(struct atapi_reqsense));
488 else if (iocmd->u.atapi.flags & ATAPI_CMD_READ)
489 error = copyout(buf, iocmd->u.atapi.data, iocmd->u.atapi.count);
502 ata_getparam(struct ata_device *atadev, u_int8_t command)
504 struct ata_params *ata_parm;
507 ata_parm = kmalloc(sizeof(struct ata_params), M_ATA, M_INTWAIT);
509 /* apparently some devices needs this repeated */
511 if (ata_command(atadev, command, 0, 0, 0, ATA_IMMEDIATE)) {
512 ata_prtdev(atadev, "%s identify failed\n",
513 command == ATA_C_ATAPI_IDENTIFY ? "ATAPI" : "ATA");
514 kfree(ata_parm, M_ATA);
518 ata_prtdev(atadev, "%s identify retries exceeded\n",
519 command == ATA_C_ATAPI_IDENTIFY ? "ATAPI" : "ATA");
520 kfree(ata_parm, M_ATA);
523 } while (ata_wait(atadev, ((command == ATA_C_ATAPI_IDENTIFY) ?
524 ATA_S_DRQ : (ATA_S_READY|ATA_S_DSC|ATA_S_DRQ))));
525 ATA_INSW(atadev->channel->r_io, ATA_DATA, (int16_t *)ata_parm,
526 sizeof(struct ata_params)/sizeof(int16_t));
528 if (command == ATA_C_ATA_IDENTIFY ||
529 !((ata_parm->model[0] == 'N' && ata_parm->model[1] == 'E') ||
530 (ata_parm->model[0] == 'F' && ata_parm->model[1] == 'X') ||
531 (ata_parm->model[0] == 'P' && ata_parm->model[1] == 'i')))
532 bswap(ata_parm->model, sizeof(ata_parm->model));
533 btrim(ata_parm->model, sizeof(ata_parm->model));
534 bpack(ata_parm->model, ata_parm->model, sizeof(ata_parm->model));
535 bswap(ata_parm->revision, sizeof(ata_parm->revision));
536 btrim(ata_parm->revision, sizeof(ata_parm->revision));
537 bpack(ata_parm->revision, ata_parm->revision, sizeof(ata_parm->revision));
538 bswap(ata_parm->serial, sizeof(ata_parm->serial));
539 btrim(ata_parm->serial, sizeof(ata_parm->serial));
540 bpack(ata_parm->serial, ata_parm->serial, sizeof(ata_parm->serial));
541 atadev->param = ata_parm;
548 ata_boot_attach(void)
550 struct ata_channel *ch;
556 * run through all ata devices and look for real ATA & ATAPI devices
557 * using the hints we found in the early probe, this avoids some of
558 * the delays probing of non-exsistent devices can cause.
560 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
561 if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
563 if (ch->devices & ATA_ATA_SLAVE)
564 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
565 ch->devices &= ~ATA_ATA_SLAVE;
566 if (ch->devices & ATA_ATAPI_SLAVE)
567 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
568 ch->devices &= ~ATA_ATAPI_SLAVE;
569 if (ch->devices & ATA_ATA_MASTER)
570 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
571 ch->devices &= ~ATA_ATA_MASTER;
572 if (ch->devices & ATA_ATAPI_MASTER)
573 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
574 ch->devices &= ~ATA_ATAPI_MASTER;
578 /* now we know whats there, do the real attach, first the ATA disks */
579 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
580 if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
582 if (ch->devices & ATA_ATA_MASTER)
583 ad_attach(&ch->device[MASTER], 0);
584 if (ch->devices & ATA_ATA_SLAVE)
585 ad_attach(&ch->device[SLAVE], 0);
590 /* then the atapi devices */
591 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
592 if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
594 if (ch->devices & ATA_ATAPI_MASTER)
595 atapi_attach(&ch->device[MASTER], 0);
596 if (ch->devices & ATA_ATAPI_SLAVE)
597 atapi_attach(&ch->device[SLAVE], 0);
599 atapi_cam_attach_bus(ch);
611 struct ata_channel *ch = (struct ata_channel *)data;
613 * on PCI systems we might share an interrupt line with another
614 * device or our twin ATA channel, so call ch->intr_func to figure
615 * out if it is really an interrupt we should process here
617 if (ch->intr_func && ch->intr_func(ch))
620 /* if drive is busy it didn't interrupt */
621 if (ATA_INB(ch->r_altio, ATA_ALTSTAT) & ATA_S_BUSY) {
623 if (!(ATA_INB(ch->r_altio, ATA_ALTSTAT) & ATA_S_DRQ))
627 /* clear interrupt and get status */
628 ch->status = ATA_INB(ch->r_io, ATA_STATUS);
630 if (ch->status & ATA_S_ERROR)
631 ch->error = ATA_INB(ch->r_io, ATA_ERROR);
633 /* find & call the responsible driver to process this interrupt */
634 switch (ch->active) {
637 if (!ch->running || ad_interrupt(ch->running) == ATA_OP_CONTINUES)
642 case ATA_ACTIVE_ATAPI:
643 if (!ch->running || atapi_interrupt(ch->running) == ATA_OP_CONTINUES)
648 case ATA_WAIT_INTR | ATA_CONTROL:
653 case ATA_WAIT_READY | ATA_CONTROL:
657 if (ch->flags & ATA_QUEUED) {
658 ch->active = ATA_ACTIVE;
659 if (ata_service(ch) == ATA_OP_CONTINUES)
667 static int intr_count = 0;
669 if (intr_count++ < 10)
670 ata_printf(ch, -1, "unwanted interrupt #%d active=%02x s=%02x\n",
671 intr_count, ch->active, ch->status);
676 ch->active &= ATA_CONTROL;
677 if (ch->active & ATA_CONTROL)
685 ata_start(struct ata_channel *ch)
688 struct ad_request *ad_request;
691 struct atapi_request *atapi_request;
694 crit_enter(); /* interlock non-atomic channel lock */
695 if (!ATA_LOCK_CH(ch, ATA_ACTIVE)) {
701 /* find & call the responsible driver if anything on the ATA queue */
702 if (TAILQ_EMPTY(&ch->ata_queue)) {
703 if (ch->devices & (ATA_ATA_MASTER) && ch->device[MASTER].driver)
704 ad_start(&ch->device[MASTER]);
705 if (ch->devices & (ATA_ATA_SLAVE) && ch->device[SLAVE].driver)
706 ad_start(&ch->device[SLAVE]);
708 if ((ad_request = TAILQ_FIRST(&ch->ata_queue))) {
709 TAILQ_REMOVE(&ch->ata_queue, ad_request, chain);
710 ch->active = ATA_ACTIVE_ATA;
711 ch->running = ad_request;
714 * The donecount had better be 0 here because the channel may not
715 * have retained the setup for the request (if a retry).
717 KKASSERT(ad_request->donecount == 0);
718 if (ad_transfer(ad_request) == ATA_OP_CONTINUES) {
726 /* find & call the responsible driver if anything on the ATAPI queue */
727 if (TAILQ_EMPTY(&ch->atapi_queue)) {
728 if (ch->devices & (ATA_ATAPI_MASTER) && ch->device[MASTER].driver)
729 atapi_start(&ch->device[MASTER]);
730 if (ch->devices & (ATA_ATAPI_SLAVE) && ch->device[SLAVE].driver)
731 atapi_start(&ch->device[SLAVE]);
733 if ((atapi_request = TAILQ_FIRST(&ch->atapi_queue))) {
734 TAILQ_REMOVE(&ch->atapi_queue, atapi_request, chain);
735 ch->active = ATA_ACTIVE_ATAPI;
736 ch->running = atapi_request;
737 if (atapi_transfer(atapi_request) == ATA_OP_CONTINUES) {
748 ata_reset(struct ata_channel *ch)
750 u_int8_t lsb, msb, ostat0, ostat1;
751 u_int8_t stat0 = 0, stat1 = 0;
752 int mask = 0, timeout;
754 /* do we have any signs of ATA/ATAPI HW being present ? */
755 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
757 ostat0 = ATA_INB(ch->r_io, ATA_STATUS);
758 if ((ostat0 & 0xf8) != 0xf8 && ostat0 != 0xa5) {
762 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
764 ostat1 = ATA_INB(ch->r_io, ATA_STATUS);
765 if ((ostat1 & 0xf8) != 0xf8 && ostat1 != 0xa5) {
774 /* in some setups we dont want to test for a slave */
775 if (ch->flags & ATA_NO_SLAVE) {
781 ata_printf(ch, -1, "mask=%02x ostat0=%02x ostat2=%02x\n",
782 mask, ostat0, ostat1);
785 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
787 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_RESET);
789 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_IDS);
791 ATA_INB(ch->r_io, ATA_ERROR);
793 /* wait for BUSY to go inactive */
794 for (timeout = 0; timeout < 3100; timeout++) {
795 if (stat0 & ATA_S_BUSY) {
796 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
799 /* check for ATAPI signature while its still there */
800 lsb = ATA_INB(ch->r_io, ATA_CYL_LSB);
801 msb = ATA_INB(ch->r_io, ATA_CYL_MSB);
802 stat0 = ATA_INB(ch->r_io, ATA_STATUS);
803 if (!(stat0 & ATA_S_BUSY)) {
805 ata_printf(ch, ATA_MASTER, "ATAPI %02x %02x\n", lsb, msb);
806 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB)
807 ch->devices |= ATA_ATAPI_MASTER;
810 if (stat1 & ATA_S_BUSY) {
811 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
814 /* check for ATAPI signature while its still there */
815 lsb = ATA_INB(ch->r_io, ATA_CYL_LSB);
816 msb = ATA_INB(ch->r_io, ATA_CYL_MSB);
817 stat1 = ATA_INB(ch->r_io, ATA_STATUS);
818 if (!(stat1 & ATA_S_BUSY)) {
820 ata_printf(ch, ATA_SLAVE, "ATAPI %02x %02x\n", lsb, msb);
821 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB)
822 ch->devices |= ATA_ATAPI_SLAVE;
825 if (mask == 0x01) /* wait for master only */
826 if (!(stat0 & ATA_S_BUSY))
828 if (mask == 0x02) /* wait for slave only */
829 if (!(stat1 & ATA_S_BUSY))
831 if (mask == 0x03) /* wait for both master & slave */
832 if (!(stat0 & ATA_S_BUSY) && !(stat1 & ATA_S_BUSY))
837 * some devices release BUSY before they are ready to accept commands.
838 * We must wait at least 50ms before attempting to issue a command after
842 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
844 if (stat0 & ATA_S_BUSY)
846 if (stat1 & ATA_S_BUSY)
849 ata_printf(ch, -1, "mask=%02x stat0=%02x stat1=%02x\n",
854 if (mask & 0x01 && ostat0 != 0x00 && !(ch->devices & ATA_ATAPI_MASTER)) {
855 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
857 ATA_OUTB(ch->r_io, ATA_ERROR, 0x58);
858 ATA_OUTB(ch->r_io, ATA_CYL_LSB, 0xa5);
859 lsb = ATA_INB(ch->r_io, ATA_ERROR);
860 msb = ATA_INB(ch->r_io, ATA_CYL_LSB);
862 ata_printf(ch, ATA_MASTER, "ATA %02x %02x\n", lsb, msb);
863 if (lsb != 0x58 && msb == 0xa5)
864 ch->devices |= ATA_ATA_MASTER;
866 if (mask & 0x02 && ostat1 != 0x00 && !(ch->devices & ATA_ATAPI_SLAVE)) {
867 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
869 ATA_OUTB(ch->r_io, ATA_ERROR, 0x58);
870 ATA_OUTB(ch->r_io, ATA_CYL_LSB, 0xa5);
871 lsb = ATA_INB(ch->r_io, ATA_ERROR);
872 msb = ATA_INB(ch->r_io, ATA_CYL_LSB);
874 ata_printf(ch, ATA_SLAVE, "ATA %02x %02x\n", lsb, msb);
875 if (lsb != 0x58 && msb == 0xa5)
876 ch->devices |= ATA_ATA_SLAVE;
879 ata_printf(ch, -1, "devices=%02x\n", ch->devices);
883 ata_reinit(struct ata_channel *ch)
885 int devices, misdev, newdev;
887 if (!ch->r_io || !ch->r_altio || !ch->r_irq)
890 ATA_FORCELOCK_CH(ch, ATA_CONTROL);
892 devices = ch->devices;
893 ata_printf(ch, -1, "resetting devices .. ");
896 if ((misdev = devices & ~ch->devices)) {
900 if (misdev & ATA_ATA_MASTER && ch->device[MASTER].driver)
901 ad_detach(&ch->device[MASTER], 0);
902 if (misdev & ATA_ATA_SLAVE && ch->device[SLAVE].driver)
903 ad_detach(&ch->device[SLAVE], 0);
906 if (misdev & ATA_ATAPI_MASTER && ch->device[MASTER].driver)
907 atapi_detach(&ch->device[MASTER]);
908 if (misdev & ATA_ATAPI_SLAVE && ch->device[SLAVE].driver)
909 atapi_detach(&ch->device[SLAVE]);
911 if (misdev & ATA_ATA_MASTER || misdev & ATA_ATAPI_MASTER) {
912 if (ch->device[MASTER].param)
913 kfree(ch->device[MASTER].param, M_ATA);
914 ch->device[MASTER].param = NULL;
916 if (misdev & ATA_ATA_SLAVE || misdev & ATA_ATAPI_SLAVE) {
917 if (ch->device[SLAVE].param)
918 kfree(ch->device[SLAVE].param, M_ATA);
919 ch->device[SLAVE].param = NULL;
922 if ((newdev = ~devices & ch->devices)) {
923 if (newdev & ATA_ATA_MASTER)
924 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
925 ch->devices &= ~ATA_ATA_MASTER;
926 if (newdev & ATA_ATA_SLAVE)
927 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
928 ch->devices &= ~ATA_ATA_SLAVE;
929 if (newdev & ATA_ATAPI_MASTER)
930 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
931 ch->devices &= ~ATA_ATAPI_MASTER;
932 if (newdev & ATA_ATAPI_SLAVE)
933 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
934 ch->devices &= ~ATA_ATAPI_SLAVE;
936 newdev = ~devices & ch->devices;
937 if (!misdev && newdev)
940 if (newdev & ATA_ATA_MASTER && !ch->device[MASTER].driver)
941 ad_attach(&ch->device[MASTER], 1);
942 else if (ch->devices & ATA_ATA_MASTER && ch->device[MASTER].driver) {
943 ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY);
944 ad_reinit(&ch->device[MASTER]);
946 if (newdev & ATA_ATA_SLAVE && !ch->device[SLAVE].driver)
947 ad_attach(&ch->device[SLAVE], 1);
948 else if (ch->devices & (ATA_ATA_SLAVE) && ch->device[SLAVE].driver) {
949 ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY);
950 ad_reinit(&ch->device[SLAVE]);
954 if (newdev & ATA_ATAPI_MASTER && !ch->device[MASTER].driver)
955 atapi_attach(&ch->device[MASTER], 1);
956 else if (ch->devices & (ATA_ATAPI_MASTER) && ch->device[MASTER].driver) {
957 ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY);
958 atapi_reinit(&ch->device[MASTER]);
960 if (newdev & ATA_ATAPI_SLAVE && !ch->device[SLAVE].driver)
961 atapi_attach(&ch->device[SLAVE], 1);
962 else if (ch->devices & (ATA_ATAPI_SLAVE) && ch->device[SLAVE].driver) {
963 ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY);
964 atapi_reinit(&ch->device[SLAVE]);
968 if (ch->devices & (ATA_ATAPI_MASTER | ATA_ATAPI_SLAVE))
969 atapi_cam_reinit_bus(ch);
978 ata_service(struct ata_channel *ch)
980 /* do we have a SERVICE request from the drive ? */
981 if ((ch->status & (ATA_S_SERVICE|ATA_S_ERROR|ATA_S_DRQ)) == ATA_S_SERVICE) {
982 ATA_OUTB(ch->r_bmio, ATA_BMSTAT_PORT,
983 ata_dmastatus(ch) | ATA_BMSTAT_INTERRUPT);
985 if ((ATA_INB(ch->r_io, ATA_DRIVE) & ATA_SLAVE) == ATA_MASTER) {
986 if ((ch->devices & ATA_ATA_MASTER) && ch->device[MASTER].driver)
987 return ad_service((struct ad_softc *)
988 ch->device[MASTER].driver, 0);
991 if ((ch->devices & ATA_ATA_SLAVE) && ch->device[SLAVE].driver)
992 return ad_service((struct ad_softc *)
993 ch->device[SLAVE].driver, 0);
997 return ATA_OP_FINISHED;
1001 ata_wait(struct ata_device *atadev, u_int8_t mask)
1006 while (timeout < 5000000) { /* timeout 5 secs */
1007 atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS);
1009 /* if drive fails status, reselect the drive just to be sure */
1010 if (atadev->channel->status == 0xff) {
1011 ata_prtdev(atadev, "no status, reselecting device\n");
1012 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM|atadev->unit);
1014 atadev->channel->status = ATA_INB(atadev->channel->r_io,ATA_STATUS);
1015 if (atadev->channel->status == 0xff)
1020 if (!(atadev->channel->status & ATA_S_BUSY))
1023 if (timeout > 1000) {
1032 if (atadev->channel->status & ATA_S_ERROR)
1033 atadev->channel->error = ATA_INB(atadev->channel->r_io, ATA_ERROR);
1034 if (timeout >= 5000000)
1037 return (atadev->channel->status & ATA_S_ERROR);
1039 /* Wait 50 msec for bits wanted. */
1042 atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS);
1043 if ((atadev->channel->status & mask) == mask) {
1044 if (atadev->channel->status & ATA_S_ERROR)
1045 atadev->channel->error=ATA_INB(atadev->channel->r_io,ATA_ERROR);
1046 return (atadev->channel->status & ATA_S_ERROR);
1054 ata_command(struct ata_device *atadev, u_int8_t command,
1055 u_int64_t lba, u_int16_t count, u_int8_t feature, int flags)
1059 ata_prtdev(atadev, "ata_command: addr=%04lx, cmd=%02x, "
1060 "lba=%lld, count=%d, feature=%d, flags=%02x\n",
1061 rman_get_start(atadev->channel->r_io),
1062 command, lba, count, feature, flags);
1066 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1068 /* disable interrupt from device */
1069 if (atadev->channel->flags & ATA_QUEUED)
1070 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_4BIT);
1072 /* ready to issue command ? */
1073 if (ata_wait(atadev, 0) < 0) {
1074 ata_prtdev(atadev, "timeout sending command=%02x s=%02x e=%02x\n",
1075 command, atadev->channel->status, atadev->channel->error);
1079 /* only use 48bit addressing if needed because of the overhead */
1080 if ((lba >= 268435455 || count > 256) && atadev->param &&
1081 atadev->param->support.address48) {
1082 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, (feature>>8) & 0xff);
1083 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, feature);
1084 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, (count>>8) & 0xff);
1085 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, count & 0xff);
1086 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, (lba>>24) & 0xff);
1087 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, lba & 0xff);
1088 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>32) & 0xff);
1089 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>8) & 0xff);
1090 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>40) & 0xff);
1091 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>16) & 0xff);
1092 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_LBA | atadev->unit);
1094 /* translate command into 48bit version */
1097 command = ATA_C_READ48; break;
1098 case ATA_C_READ_MUL:
1099 command = ATA_C_READ_MUL48; break;
1100 case ATA_C_READ_DMA:
1101 command = ATA_C_READ_DMA48; break;
1102 case ATA_C_READ_DMA_QUEUED:
1103 command = ATA_C_READ_DMA_QUEUED48; break;
1105 command = ATA_C_WRITE48; break;
1106 case ATA_C_WRITE_MUL:
1107 command = ATA_C_WRITE_MUL48; break;
1108 case ATA_C_WRITE_DMA:
1109 command = ATA_C_WRITE_DMA48; break;
1110 case ATA_C_WRITE_DMA_QUEUED:
1111 command = ATA_C_WRITE_DMA_QUEUED48; break;
1112 case ATA_C_FLUSHCACHE:
1113 command = ATA_C_FLUSHCACHE48; break;
1115 ata_prtdev(atadev, "can't translate cmd to 48bit version\n");
1120 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, feature);
1121 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, count);
1122 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, lba & 0xff);
1123 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>8) & 0xff);
1124 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>16) & 0xff);
1125 if (atadev->flags & ATA_D_USE_CHS)
1126 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE,
1127 ATA_D_IBM | atadev->unit | ((lba>>24) & 0xf));
1129 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE,
1130 ATA_D_IBM | ATA_D_LBA | atadev->unit | ((lba>>24) &0xf));
1133 switch (flags & ATA_WAIT_MASK) {
1135 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1137 /* enable interrupt */
1138 if (atadev->channel->flags & ATA_QUEUED)
1139 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
1143 atadev->channel->active |= ATA_WAIT_INTR;
1144 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1146 /* enable interrupt */
1147 if (atadev->channel->flags & ATA_QUEUED)
1148 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
1150 if (tsleep((caddr_t)atadev->channel, 0, "atacmd", 10 * hz)) {
1151 ata_prtdev(atadev, "timeout waiting for interrupt\n");
1152 atadev->channel->active &= ~ATA_WAIT_INTR;
1157 case ATA_WAIT_READY:
1158 atadev->channel->active |= ATA_WAIT_READY;
1159 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1160 if (ata_wait(atadev, ATA_S_READY) < 0) {
1161 ata_prtdev(atadev, "timeout waiting for cmd=%02x s=%02x e=%02x\n",
1162 command, atadev->channel->status,atadev->channel->error);
1165 atadev->channel->active &= ~ATA_WAIT_READY;
1172 ata_enclosure_start(struct ata_device *atadev)
1174 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1176 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1178 ATA_INB(atadev->channel->r_io, ATA_CMD);
1180 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
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_INB(atadev->channel->r_io, ATA_COUNT);
1188 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1193 ata_enclosure_end(struct ata_device *atadev)
1195 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1200 ata_enclosure_chip_start(struct ata_device *atadev)
1202 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0b);
1203 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0a);
1205 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08);
1209 ata_enclosure_chip_end(struct ata_device *atadev)
1211 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08);
1213 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0a);
1215 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0b);
1220 ata_enclosure_chip_rdbit(struct ata_device *atadev)
1224 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0);
1226 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x02);
1228 val = ATA_INB(atadev->channel->r_io, ATA_SECTOR) & 0x01;
1234 ata_enclosure_chip_wrbit(struct ata_device *atadev, u_int8_t data)
1236 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08 | (data & 0x01));
1238 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08 | 0x02 | (data & 0x01));
1243 ata_enclosure_chip_rw(struct ata_device *atadev, int rw, u_int8_t val)
1248 for (i = 0; i < 8; i++)
1249 ata_enclosure_chip_wrbit(atadev, (val & (0x80 >> i)) ? 1 : 0);
1252 for (i = 0; i < 8; i++)
1253 val = (val << 1) | ata_enclosure_chip_rdbit(atadev);
1255 ata_enclosure_chip_wrbit(atadev, 0);
1260 ata_enclosure_sensor(struct ata_device *atadev,
1261 int rw, u_int8_t idx, u_int8_t data)
1263 ata_enclosure_start(atadev);
1264 ata_enclosure_chip_start(atadev);
1265 ata_enclosure_chip_rw(atadev, 1, 0x5a);
1266 ata_enclosure_chip_rw(atadev, 1, idx);
1268 ata_enclosure_chip_rw(atadev, 1, data);
1271 ata_enclosure_chip_end(atadev);
1272 ata_enclosure_chip_start(atadev);
1273 ata_enclosure_chip_rw(atadev, 1, 0x5b);
1274 data = ata_enclosure_chip_rw(atadev, 0, 0);
1276 ata_enclosure_chip_end(atadev);
1277 ata_enclosure_end(atadev);
1282 ata_enclosure_status(struct ata_device *atadev,
1283 int *fan, int *temp, int *v05, int *v12)
1285 u_int8_t id1, id2, cnt, div;
1288 if (atadev->flags & ATA_D_ENC_PRESENT) {
1289 ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL);
1290 ata_enclosure_sensor(atadev, 1, 0x4e, 0);
1291 id1 = ata_enclosure_sensor(atadev, 0, 0x4f, 0);
1292 ata_enclosure_sensor(atadev, 1, 0x4e, 0x80);
1293 id2 = ata_enclosure_sensor(atadev, 0, 0x4f, 0);
1294 if (id1 == 0xa3 && id2 == 0x5c) {
1295 div = 1 << (((ata_enclosure_sensor(atadev, 0, 0x5d, 0)&0x20)>>3)+
1296 ((ata_enclosure_sensor(atadev, 0, 0x47, 0)&0x30)>>4)+1);
1297 cnt = ata_enclosure_sensor(atadev, 0, 0x28, 0);
1301 *fan = 1350000 / cnt / div;
1302 ata_enclosure_sensor(atadev, 1, 0x4e, 0x01);
1303 *temp = (ata_enclosure_sensor(atadev, 0, 0x50, 0) * 10) +
1304 (ata_enclosure_sensor(atadev, 0, 0x50, 0) & 0x80 ? 5 : 0);
1305 *v05 = ata_enclosure_sensor(atadev, 0, 0x23, 0) * 27;
1306 *v12 = ata_enclosure_sensor(atadev, 0, 0x24, 0) * 61;
1309 ATA_UNLOCK_CH(atadev->channel);
1315 ata_enclosure_print(struct ata_device *atadev)
1318 int fan, temp, v05, v12;
1320 ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL);
1321 ata_enclosure_start(atadev);
1322 id = ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1324 st = ATA_INB(atadev->channel->r_io, ATA_COUNT);
1326 ata_enclosure_end(atadev);
1327 ATA_UNLOCK_CH(atadev->channel);
1329 switch (id & 0x93) {
1331 ata_prtdev(atadev, "Universal enclosure");
1334 ata_prtdev(atadev, "FastSwap enclosure");
1338 ata_prtdev(atadev, "SuperSwap enclosure");
1341 atadev->flags &= ~ATA_D_ENC_PRESENT;
1344 atadev->flags |= ATA_D_ENC_PRESENT;
1346 if (ata_enclosure_status(atadev, &fan, &temp, &v05, &v12))
1347 kprintf(" detected\n");
1349 kprintf(" [FAN:%drpm TEMP:%d.%01dC %d.%03dV %d.%03dV]\n",
1350 fan, temp/10, temp%10, v05/1000, v05%1000, v12/1000, v12%1000);
1354 ata_enclosure_leds(struct ata_device *atadev, u_int8_t color)
1356 if (atadev->flags & ATA_D_ENC_PRESENT) {
1359 ata_enclosure_start(atadev);
1360 reg = ATA_INB(atadev->channel->r_io, ATA_COUNT);
1362 ATA_OUTB(atadev->channel->r_io, ATA_COUNT,
1363 (color & ATA_LED_MASK) | (reg & ~ATA_LED_MASK));
1365 ata_enclosure_end(atadev);
1370 ata_change_mode(struct ata_device *atadev, int mode)
1372 int umode, wmode, pmode;
1374 umode = ata_umode(atadev->param);
1375 wmode = ata_wmode(atadev->param);
1376 pmode = ata_pmode(atadev->param);
1378 switch (mode & ATA_DMA_MASK) {
1380 if ((mode & ATA_MODE_MASK) < umode)
1381 umode = mode & ATA_MODE_MASK;
1384 if ((mode & ATA_MODE_MASK) < wmode)
1385 wmode = mode & ATA_MODE_MASK;
1389 if (((mode & ATA_MODE_MASK) - ATA_PIO0) < pmode)
1390 pmode = (mode & ATA_MODE_MASK) - ATA_PIO0;
1395 crit_enter(); /* interlock non-atomic channel lock */
1396 ATA_SLEEPLOCK_CH(atadev->channel, ATA_ACTIVE);
1397 ata_dmainit(atadev, pmode, wmode, umode);
1398 ATA_UNLOCK_CH(atadev->channel);
1400 ata_start(atadev->channel); /* XXX SOS */
1404 ata_printf(struct ata_channel *ch, int device, const char * fmt, ...)
1410 ret = kprintf("ata%d: ", device_get_unit(ch->dev));
1412 if (ch->device[ATA_DEV(device)].name)
1413 ret = kprintf("%s: ", ch->device[ATA_DEV(device)].name);
1415 ret = kprintf("ata%d-%s: ", device_get_unit(ch->dev),
1416 (device == ATA_MASTER) ? "master" : "slave");
1418 __va_start(ap, fmt);
1419 ret += kvprintf(fmt, ap);
1425 ata_prtdev(struct ata_device *atadev, const char * fmt, ...)
1431 ret = kprintf("%s: ", atadev->name);
1433 ret = kprintf("ata%d-%s: ", device_get_unit(atadev->channel->dev),
1434 (atadev->unit == ATA_MASTER) ? "master" : "slave");
1435 __va_start(ap, fmt);
1436 ret += kvprintf(fmt, ap);
1442 ata_set_name(struct ata_device *atadev, char *name, int lun)
1444 atadev->name = kmalloc(strlen(name) + 4, M_ATA, M_INTWAIT);
1445 ksprintf(atadev->name, "%s%d", name, lun);
1449 ata_free_name(struct ata_device *atadev)
1452 kfree(atadev->name, M_ATA);
1453 atadev->name = NULL;
1457 ata_get_lun(u_int32_t *map)
1459 int lun = ffs(~*map) - 1;
1466 ata_test_lun(u_int32_t *map, int lun)
1468 return (*map & (1 << lun));
1472 ata_free_lun(u_int32_t *map, int lun)
1474 *map &= ~(1 << lun);
1478 ata_mode2str(int mode)
1481 case ATA_PIO: return "BIOSPIO";
1482 case ATA_PIO0: return "PIO0";
1483 case ATA_PIO1: return "PIO1";
1484 case ATA_PIO2: return "PIO2";
1485 case ATA_PIO3: return "PIO3";
1486 case ATA_PIO4: return "PIO4";
1487 case ATA_DMA: return "BIOSDMA";
1488 case ATA_WDMA2: return "WDMA2";
1489 case ATA_UDMA2: return "UDMA33";
1490 case ATA_UDMA4: return "UDMA66";
1491 case ATA_UDMA5: return "UDMA100";
1492 case ATA_UDMA6: return "UDMA133";
1493 default: return "???";
1498 ata_pmode(struct ata_params *ap)
1500 if (ap->atavalid & ATA_FLAG_64_70) {
1501 if (ap->apiomodes & 2)
1503 if (ap->apiomodes & 1)
1506 if (ap->retired_piomode == 2)
1508 if (ap->retired_piomode == 1)
1510 if (ap->retired_piomode == 0)
1516 ata_wmode(struct ata_params *ap)
1518 if (ap->mwdmamodes & 0x04)
1520 if (ap->mwdmamodes & 0x02)
1522 if (ap->mwdmamodes & 0x01)
1528 ata_umode(struct ata_params *ap)
1530 if (ap->atavalid & ATA_FLAG_88) {
1531 if (ap->udmamodes & 0x40)
1533 if (ap->udmamodes & 0x20)
1535 if (ap->udmamodes & 0x10)
1537 if (ap->udmamodes & 0x08)
1539 if (ap->udmamodes & 0x04)
1541 if (ap->udmamodes & 0x02)
1543 if (ap->udmamodes & 0x01)
1550 bswap(int8_t *buf, int len)
1552 u_int16_t *ptr = (u_int16_t*)(buf + len);
1554 while (--ptr >= (u_int16_t*)buf)
1559 btrim(int8_t *buf, int len)
1563 for (ptr = buf; ptr < buf+len; ++ptr)
1566 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
1571 bpack(int8_t *src, int8_t *dst, int len)
1575 for (i = j = blank = 0 ; i < len; i++) {
1576 if (blank && src[i] == ' ') continue;
1577 if (blank && src[i] != ' ') {
1582 if (src[i] == ' ') {
1596 make_dev(&ata_ops, 0, UID_ROOT, GID_OPERATOR, 0600, "ata");
1599 SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL)