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.11 2004/02/18 00:37:08 dillon Exp $
34 #include "use_atadisk.h"
35 #include "use_atapicd.h"
36 #include "use_atapifd.h"
37 #include "use_atapist.h"
38 #include "use_atapicam.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
42 #include <sys/kernel.h>
45 #include <sys/module.h>
48 #include <sys/malloc.h>
49 #include <sys/devicestat.h>
50 #include <sys/sysctl.h>
51 #include <machine/stdarg.h>
52 #include <machine/resource.h>
53 #include <machine/bus.h>
54 #include <machine/clock.h>
57 #include <machine/md_var.h>
62 #include "atapi-all.h"
66 struct atapi_request atapi;
69 /* device structures */
70 static d_ioctl_t ataioctl;
71 static struct cdevsw ata_cdevsw = {
79 /* close */ nullclose,
85 /* strategy */ nostrategy,
91 static void ata_boot_attach(void);
92 static void ata_intr(void *);
93 static int ata_getparam(struct ata_device *, u_int8_t);
94 static int ata_service(struct ata_channel *);
95 static void bswap(int8_t *, int);
96 static void btrim(int8_t *, int);
97 static void bpack(int8_t *, int8_t *, int);
98 static void ata_change_mode(struct ata_device *, int);
99 static u_int8_t ata_enclosure_sensor(struct ata_device *, int, u_int8_t, u_int8_t);
100 static int ata_enclosure_status(struct ata_device *, int *, int *, int *, int *);
103 SYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters");
105 int ata_mpipe_size = 4;
106 TUNABLE_INT("hw.ata.mpipe_size", &ata_mpipe_size);
107 SYSCTL_INT(_hw_ata, OID_AUTO, mpipe_size, CTLFLAG_RW, &ata_mpipe_size, 0,
108 "ATA global I/O pipeline max size");
112 devclass_t ata_devclass;
115 static struct intr_config_hook *ata_delayed_attach = NULL;
116 static MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer");
119 #define DEV_ATAPIALL NATAPICD > 0 || NATAPIFD > 0 || \
120 NATAPIST > 0 || NATAPICAM > 0
123 ata_probe(device_t dev)
125 struct ata_channel *ch;
128 if (!dev || !(ch = device_get_softc(dev)))
131 if (ch->r_io || ch->r_altio || ch->r_irq)
134 /* initialize the softc basics */
135 ch->active = ATA_IDLE;
138 rid = ATA_IOADDR_RID;
139 ch->r_io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
140 ATA_IOSIZE, RF_ACTIVE);
144 rid = ATA_ALTADDR_RID;
145 ch->r_altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
146 ATA_ALTIOSIZE, RF_ACTIVE);
150 rid = ATA_BMADDR_RID;
151 ch->r_bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
152 ATA_BMIOSIZE, RF_ACTIVE);
154 ata_printf(ch, -1, "iobase=0x%04x altiobase=0x%04x bmaddr=0x%04x\n",
155 (int)rman_get_start(ch->r_io),
156 (int)rman_get_start(ch->r_altio),
157 (ch->r_bmio) ? (int)rman_get_start(ch->r_bmio) : 0);
161 ch->device[MASTER].channel = ch;
162 ch->device[MASTER].unit = ATA_MASTER;
163 ch->device[MASTER].mode = ATA_PIO;
164 ch->device[SLAVE].channel = ch;
165 ch->device[SLAVE].unit = ATA_SLAVE;
166 ch->device[SLAVE].mode = ATA_PIO;
167 TAILQ_INIT(&ch->ata_queue);
168 TAILQ_INIT(&ch->atapi_queue);
170 mpipe_init(&ch->req_mpipe, M_ATA, sizeof(union ata_request), 4, ata_mpipe_size);
171 mpipe_init(&ch->dma_mpipe, M_DEVBUF, PAGE_SIZE, 4, ata_mpipe_size);
177 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ch->r_io);
179 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ch->r_altio);
181 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, ch->r_bmio);
183 ata_printf(ch, -1, "probe allocation failed\n");
188 ata_attach(device_t dev)
190 struct ata_channel *ch;
193 if (!dev || !(ch = device_get_softc(dev)))
197 ch->r_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
198 RF_SHAREABLE | RF_ACTIVE);
200 ata_printf(ch, -1, "unable to allocate interrupt\n");
203 if ((error = bus_setup_intr(dev, ch->r_irq, INTR_TYPE_BIO,
204 ata_intr, ch, &ch->ih))) {
205 ata_printf(ch, -1, "unable to setup interrupt\n");
210 * do not attach devices if we are in early boot, this is done later
211 * when interrupts are enabled by a hook into the boot process.
212 * otherwise attach what the probe has found in ch->devices.
214 if (!ata_delayed_attach) {
217 if (ch->devices & ATA_ATA_SLAVE)
218 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
219 ch->devices &= ~ATA_ATA_SLAVE;
220 if (ch->devices & ATA_ATAPI_SLAVE)
221 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
222 ch->devices &= ~ATA_ATAPI_SLAVE;
223 if (ch->devices & ATA_ATA_MASTER)
224 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
225 ch->devices &= ~ATA_ATA_MASTER;
226 if (ch->devices & ATA_ATAPI_MASTER)
227 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
228 ch->devices &= ~ATA_ATAPI_MASTER;
230 if (ch->devices & ATA_ATA_MASTER)
231 ad_attach(&ch->device[MASTER], 0);
232 if (ch->devices & ATA_ATA_SLAVE)
233 ad_attach(&ch->device[SLAVE], 0);
236 if (ch->devices & ATA_ATAPI_MASTER)
237 atapi_attach(&ch->device[MASTER], 0);
238 if (ch->devices & ATA_ATAPI_SLAVE)
239 atapi_attach(&ch->device[SLAVE], 0);
242 atapi_cam_attach_bus(ch);
250 ata_detach(device_t dev)
252 struct ata_channel *ch;
255 if (!dev || !(ch = device_get_softc(dev)) ||
256 !ch->r_io || !ch->r_altio || !ch->r_irq)
259 /* make sure channel is not busy */
260 ATA_SLEEPLOCK_CH(ch, ATA_CONTROL);
264 if (ch->devices & ATA_ATA_MASTER && ch->device[MASTER].driver)
265 ad_detach(&ch->device[MASTER], 1);
266 if (ch->devices & ATA_ATA_SLAVE && ch->device[SLAVE].driver)
267 ad_detach(&ch->device[SLAVE], 1);
270 if (ch->devices & ATA_ATAPI_MASTER && ch->device[MASTER].driver)
271 atapi_detach(&ch->device[MASTER]);
272 if (ch->devices & ATA_ATAPI_SLAVE && ch->device[SLAVE].driver)
273 atapi_detach(&ch->device[SLAVE]);
276 atapi_cam_detach_bus(ch);
280 if (ch->device[MASTER].param) {
281 free(ch->device[MASTER].param, M_ATA);
282 ch->device[MASTER].param = NULL;
284 if (ch->device[SLAVE].param) {
285 free(ch->device[SLAVE].param, M_ATA);
286 ch->device[SLAVE].param = NULL;
288 ch->device[MASTER].driver = NULL;
289 ch->device[SLAVE].driver = NULL;
290 ch->device[MASTER].mode = ATA_PIO;
291 ch->device[SLAVE].mode = ATA_PIO;
294 bus_teardown_intr(dev, ch->r_irq, ch->ih);
295 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
297 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, ch->r_bmio);
298 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ch->r_altio);
299 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ch->r_io);
304 mpipe_done(&ch->req_mpipe);
305 mpipe_done(&ch->dma_mpipe);
312 ata_resume(device_t dev)
314 return ata_reinit(device_get_softc(dev));
318 ataioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, struct thread *td)
320 struct ata_cmd *iocmd = (struct ata_cmd *)addr;
321 struct ata_channel *ch;
322 device_t device = devclass_get_device(ata_devclass, iocmd->channel);
328 if (iocmd->channel < -1 || iocmd->device < -1 || iocmd->device > SLAVE)
331 switch (iocmd->cmd) {
333 /* should enable channel HW on controller that can SOS XXX */
334 error = ata_probe(device);
336 error = ata_attach(device);
340 error = ata_detach(device);
341 /* should disable channel HW on controller that can SOS XXX */
345 if (!device || !(ch = device_get_softc(device)))
347 ATA_SLEEPLOCK_CH(ch, ATA_ACTIVE);
348 if ((error = ata_reinit(ch)))
353 if (!device || !(ch = device_get_softc(device)))
356 if ((iocmd->device == MASTER || iocmd->device == -1) &&
357 ch->device[MASTER].driver)
358 iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode;
360 iocmd->u.mode.mode[MASTER] = -1;
362 if ((iocmd->device == SLAVE || iocmd->device == -1) &&
363 ch->device[SLAVE].param)
364 iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode;
366 iocmd->u.mode.mode[SLAVE] = -1;
370 if (!device || !(ch = device_get_softc(device)))
373 if ((iocmd->device == MASTER || iocmd->device == -1) &&
374 iocmd->u.mode.mode[MASTER] >= 0 && ch->device[MASTER].param) {
375 ata_change_mode(&ch->device[MASTER],iocmd->u.mode.mode[MASTER]);
376 iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode;
379 iocmd->u.mode.mode[MASTER] = -1;
381 if ((iocmd->device == SLAVE || iocmd->device == -1) &&
382 iocmd->u.mode.mode[SLAVE] >= 0 && ch->device[SLAVE].param) {
383 ata_change_mode(&ch->device[SLAVE], iocmd->u.mode.mode[SLAVE]);
384 iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode;
387 iocmd->u.mode.mode[SLAVE] = -1;
391 if (!device || !(ch = device_get_softc(device)))
394 iocmd->u.param.type[MASTER] =
395 ch->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER);
396 iocmd->u.param.type[SLAVE] =
397 ch->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE);
399 if (ch->device[MASTER].name)
400 strcpy(iocmd->u.param.name[MASTER], ch->device[MASTER].name);
401 if (ch->device[SLAVE].name)
402 strcpy(iocmd->u.param.name[SLAVE], ch->device[SLAVE].name);
404 if (ch->device[MASTER].param)
405 bcopy(ch->device[MASTER].param, &iocmd->u.param.params[MASTER],
406 sizeof(struct ata_params));
407 if (ch->device[SLAVE].param)
408 bcopy(ch->device[SLAVE].param, &iocmd->u.param.params[SLAVE],
409 sizeof(struct ata_params));
413 struct ata_device *atadev;
415 if (!device || !(ch = device_get_softc(device)))
418 if (iocmd->device == SLAVE)
419 atadev = &ch->device[SLAVE];
421 atadev = &ch->device[MASTER];
423 return ata_enclosure_status(atadev,
424 &iocmd->u.enclosure.fan,
425 &iocmd->u.enclosure.temp,
426 &iocmd->u.enclosure.v05,
427 &iocmd->u.enclosure.v12);
432 return ata_raid_rebuild(iocmd->channel);
435 return ata_raid_create(&iocmd->u.raid_setup);
438 return ata_raid_delete(iocmd->channel);
441 return ata_raid_status(iocmd->channel, &iocmd->u.raid_status);
445 struct ata_device *atadev;
448 if (!device || !(ch = device_get_softc(device)))
451 if (!(atadev = &ch->device[iocmd->device]) ||
452 !(ch->devices & (iocmd->device == MASTER ?
453 ATA_ATAPI_MASTER : ATA_ATAPI_SLAVE)))
456 if (!(buf = malloc(iocmd->u.atapi.count, M_ATA, M_WAITOK)))
459 if (iocmd->u.atapi.flags & ATAPI_CMD_WRITE) {
460 error = copyin(iocmd->u.atapi.data, buf, iocmd->u.atapi.count);
464 error = atapi_queue_cmd(atadev, iocmd->u.atapi.ccb,
465 buf, iocmd->u.atapi.count,
466 (iocmd->u.atapi.flags == ATAPI_CMD_READ ?
467 ATPR_F_READ : 0) | ATPR_F_QUIET,
468 iocmd->u.atapi.timeout, NULL, NULL);
470 iocmd->u.atapi.error = error;
471 bcopy(&atadev->result, iocmd->u.atapi.sense_data,
472 sizeof(struct atapi_reqsense));
475 else if (iocmd->u.atapi.flags & ATAPI_CMD_READ)
476 error = copyout(buf, iocmd->u.atapi.data, iocmd->u.atapi.count);
489 ata_getparam(struct ata_device *atadev, u_int8_t command)
491 struct ata_params *ata_parm;
494 if (!(ata_parm = malloc(sizeof(struct ata_params), M_ATA, M_WAITOK))) {
495 ata_prtdev(atadev, "malloc for identify data failed\n");
499 /* apparently some devices needs this repeated */
501 if (ata_command(atadev, command, 0, 0, 0, ATA_IMMEDIATE)) {
502 ata_prtdev(atadev, "%s identify failed\n",
503 command == ATA_C_ATAPI_IDENTIFY ? "ATAPI" : "ATA");
504 free(ata_parm, M_ATA);
508 ata_prtdev(atadev, "%s identify retries exceeded\n",
509 command == ATA_C_ATAPI_IDENTIFY ? "ATAPI" : "ATA");
510 free(ata_parm, M_ATA);
513 } while (ata_wait(atadev, ((command == ATA_C_ATAPI_IDENTIFY) ?
514 ATA_S_DRQ : (ATA_S_READY|ATA_S_DSC|ATA_S_DRQ))));
515 ATA_INSW(atadev->channel->r_io, ATA_DATA, (int16_t *)ata_parm,
516 sizeof(struct ata_params)/sizeof(int16_t));
518 if (command == ATA_C_ATA_IDENTIFY ||
519 !((ata_parm->model[0] == 'N' && ata_parm->model[1] == 'E') ||
520 (ata_parm->model[0] == 'F' && ata_parm->model[1] == 'X') ||
521 (ata_parm->model[0] == 'P' && ata_parm->model[1] == 'i')))
522 bswap(ata_parm->model, sizeof(ata_parm->model));
523 btrim(ata_parm->model, sizeof(ata_parm->model));
524 bpack(ata_parm->model, ata_parm->model, sizeof(ata_parm->model));
525 bswap(ata_parm->revision, sizeof(ata_parm->revision));
526 btrim(ata_parm->revision, sizeof(ata_parm->revision));
527 bpack(ata_parm->revision, ata_parm->revision, sizeof(ata_parm->revision));
528 bswap(ata_parm->serial, sizeof(ata_parm->serial));
529 btrim(ata_parm->serial, sizeof(ata_parm->serial));
530 bpack(ata_parm->serial, ata_parm->serial, sizeof(ata_parm->serial));
531 atadev->param = ata_parm;
536 ata_boot_attach(void)
538 struct ata_channel *ch;
541 if (ata_delayed_attach) {
542 config_intrhook_disestablish(ata_delayed_attach);
543 free(ata_delayed_attach, M_TEMP);
544 ata_delayed_attach = NULL;
549 * run through all ata devices and look for real ATA & ATAPI devices
550 * using the hints we found in the early probe, this avoids some of
551 * the delays probing of non-exsistent devices can cause.
553 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
554 if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
556 if (ch->devices & ATA_ATA_SLAVE)
557 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
558 ch->devices &= ~ATA_ATA_SLAVE;
559 if (ch->devices & ATA_ATAPI_SLAVE)
560 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
561 ch->devices &= ~ATA_ATAPI_SLAVE;
562 if (ch->devices & ATA_ATA_MASTER)
563 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
564 ch->devices &= ~ATA_ATA_MASTER;
565 if (ch->devices & ATA_ATAPI_MASTER)
566 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
567 ch->devices &= ~ATA_ATAPI_MASTER;
571 /* now we know whats there, do the real attach, first the ATA disks */
572 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
573 if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
575 if (ch->devices & ATA_ATA_MASTER)
576 ad_attach(&ch->device[MASTER], 0);
577 if (ch->devices & ATA_ATA_SLAVE)
578 ad_attach(&ch->device[SLAVE], 0);
583 /* then the atapi devices */
584 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
585 if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
587 if (ch->devices & ATA_ATAPI_MASTER)
588 atapi_attach(&ch->device[MASTER], 0);
589 if (ch->devices & ATA_ATAPI_SLAVE)
590 atapi_attach(&ch->device[SLAVE], 0);
592 atapi_cam_attach_bus(ch);
602 struct ata_channel *ch = (struct ata_channel *)data;
604 * on PCI systems we might share an interrupt line with another
605 * device or our twin ATA channel, so call ch->intr_func to figure
606 * out if it is really an interrupt we should process here
608 if (ch->intr_func && ch->intr_func(ch))
611 /* if drive is busy it didn't interrupt */
612 if (ATA_INB(ch->r_altio, ATA_ALTSTAT) & ATA_S_BUSY) {
614 if (!(ATA_INB(ch->r_altio, ATA_ALTSTAT) & ATA_S_DRQ))
618 /* clear interrupt and get status */
619 ch->status = ATA_INB(ch->r_io, ATA_STATUS);
621 if (ch->status & ATA_S_ERROR)
622 ch->error = ATA_INB(ch->r_io, ATA_ERROR);
624 /* find & call the responsible driver to process this interrupt */
625 switch (ch->active) {
628 if (!ch->running || ad_interrupt(ch->running) == ATA_OP_CONTINUES)
633 case ATA_ACTIVE_ATAPI:
634 if (!ch->running || atapi_interrupt(ch->running) == ATA_OP_CONTINUES)
639 case ATA_WAIT_INTR | ATA_CONTROL:
644 case ATA_WAIT_READY | ATA_CONTROL:
648 if (ch->flags & ATA_QUEUED) {
649 ch->active = ATA_ACTIVE;
650 if (ata_service(ch) == ATA_OP_CONTINUES)
658 static int intr_count = 0;
660 if (intr_count++ < 10)
661 ata_printf(ch, -1, "unwanted interrupt #%d active=%02x s=%02x\n",
662 intr_count, ch->active, ch->status);
667 ch->active &= ATA_CONTROL;
668 if (ch->active & ATA_CONTROL)
676 ata_start(struct ata_channel *ch)
679 struct ad_request *ad_request;
682 struct atapi_request *atapi_request;
686 if (!ATA_LOCK_CH(ch, ATA_ACTIVE))
691 /* find & call the responsible driver if anything on the ATA queue */
692 if (TAILQ_EMPTY(&ch->ata_queue)) {
693 if (ch->devices & (ATA_ATA_MASTER) && ch->device[MASTER].driver)
694 ad_start(&ch->device[MASTER]);
695 if (ch->devices & (ATA_ATA_SLAVE) && ch->device[SLAVE].driver)
696 ad_start(&ch->device[SLAVE]);
698 if ((ad_request = TAILQ_FIRST(&ch->ata_queue))) {
699 TAILQ_REMOVE(&ch->ata_queue, ad_request, chain);
700 ch->active = ATA_ACTIVE_ATA;
701 ch->running = ad_request;
702 if (ad_transfer(ad_request) == ATA_OP_CONTINUES) {
710 /* find & call the responsible driver if anything on the ATAPI queue */
711 if (TAILQ_EMPTY(&ch->atapi_queue)) {
712 if (ch->devices & (ATA_ATAPI_MASTER) && ch->device[MASTER].driver)
713 atapi_start(&ch->device[MASTER]);
714 if (ch->devices & (ATA_ATAPI_SLAVE) && ch->device[SLAVE].driver)
715 atapi_start(&ch->device[SLAVE]);
717 if ((atapi_request = TAILQ_FIRST(&ch->atapi_queue))) {
718 TAILQ_REMOVE(&ch->atapi_queue, atapi_request, chain);
719 ch->active = ATA_ACTIVE_ATAPI;
720 ch->running = atapi_request;
721 if (atapi_transfer(atapi_request) == ATA_OP_CONTINUES) {
732 ata_reset(struct ata_channel *ch)
734 u_int8_t lsb, msb, ostat0, ostat1;
735 u_int8_t stat0 = 0, stat1 = 0;
736 int mask = 0, timeout;
738 /* do we have any signs of ATA/ATAPI HW being present ? */
739 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
741 ostat0 = ATA_INB(ch->r_io, ATA_STATUS);
742 if ((ostat0 & 0xf8) != 0xf8 && ostat0 != 0xa5) {
746 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
748 ostat1 = ATA_INB(ch->r_io, ATA_STATUS);
749 if ((ostat1 & 0xf8) != 0xf8 && ostat1 != 0xa5) {
758 /* in some setups we dont want to test for a slave */
759 if (ch->flags & ATA_NO_SLAVE) {
765 ata_printf(ch, -1, "mask=%02x ostat0=%02x ostat2=%02x\n",
766 mask, ostat0, ostat1);
769 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
771 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_RESET);
773 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_IDS);
775 ATA_INB(ch->r_io, ATA_ERROR);
777 /* wait for BUSY to go inactive */
778 for (timeout = 0; timeout < 3100; timeout++) {
779 if (stat0 & ATA_S_BUSY) {
780 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
783 /* check for ATAPI signature while its still there */
784 lsb = ATA_INB(ch->r_io, ATA_CYL_LSB);
785 msb = ATA_INB(ch->r_io, ATA_CYL_MSB);
786 stat0 = ATA_INB(ch->r_io, ATA_STATUS);
787 if (!(stat0 & ATA_S_BUSY)) {
789 ata_printf(ch, ATA_MASTER, "ATAPI %02x %02x\n", lsb, msb);
790 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB)
791 ch->devices |= ATA_ATAPI_MASTER;
794 if (stat1 & ATA_S_BUSY) {
795 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
798 /* check for ATAPI signature while its still there */
799 lsb = ATA_INB(ch->r_io, ATA_CYL_LSB);
800 msb = ATA_INB(ch->r_io, ATA_CYL_MSB);
801 stat1 = ATA_INB(ch->r_io, ATA_STATUS);
802 if (!(stat1 & ATA_S_BUSY)) {
804 ata_printf(ch, ATA_SLAVE, "ATAPI %02x %02x\n", lsb, msb);
805 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB)
806 ch->devices |= ATA_ATAPI_SLAVE;
809 if (mask == 0x01) /* wait for master only */
810 if (!(stat0 & ATA_S_BUSY))
812 if (mask == 0x02) /* wait for slave only */
813 if (!(stat1 & ATA_S_BUSY))
815 if (mask == 0x03) /* wait for both master & slave */
816 if (!(stat0 & ATA_S_BUSY) && !(stat1 & ATA_S_BUSY))
821 * some devices release BUSY before they are ready to accept commands.
822 * We must wait at least 50ms before attempting to issue a command after
826 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
828 if (stat0 & ATA_S_BUSY)
830 if (stat1 & ATA_S_BUSY)
833 ata_printf(ch, -1, "mask=%02x stat0=%02x stat1=%02x\n",
838 if (mask & 0x01 && ostat0 != 0x00 && !(ch->devices & ATA_ATAPI_MASTER)) {
839 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
841 ATA_OUTB(ch->r_io, ATA_ERROR, 0x58);
842 ATA_OUTB(ch->r_io, ATA_CYL_LSB, 0xa5);
843 lsb = ATA_INB(ch->r_io, ATA_ERROR);
844 msb = ATA_INB(ch->r_io, ATA_CYL_LSB);
846 ata_printf(ch, ATA_MASTER, "ATA %02x %02x\n", lsb, msb);
847 if (lsb != 0x58 && msb == 0xa5)
848 ch->devices |= ATA_ATA_MASTER;
850 if (mask & 0x02 && ostat1 != 0x00 && !(ch->devices & ATA_ATAPI_SLAVE)) {
851 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
853 ATA_OUTB(ch->r_io, ATA_ERROR, 0x58);
854 ATA_OUTB(ch->r_io, ATA_CYL_LSB, 0xa5);
855 lsb = ATA_INB(ch->r_io, ATA_ERROR);
856 msb = ATA_INB(ch->r_io, ATA_CYL_LSB);
858 ata_printf(ch, ATA_SLAVE, "ATA %02x %02x\n", lsb, msb);
859 if (lsb != 0x58 && msb == 0xa5)
860 ch->devices |= ATA_ATA_SLAVE;
863 ata_printf(ch, -1, "devices=%02x\n", ch->devices);
867 ata_reinit(struct ata_channel *ch)
869 int devices, misdev, newdev;
871 if (!ch->r_io || !ch->r_altio || !ch->r_irq)
874 ATA_FORCELOCK_CH(ch, ATA_CONTROL);
876 devices = ch->devices;
877 ata_printf(ch, -1, "resetting devices .. ");
880 if ((misdev = devices & ~ch->devices)) {
884 if (misdev & ATA_ATA_MASTER && ch->device[MASTER].driver)
885 ad_detach(&ch->device[MASTER], 0);
886 if (misdev & ATA_ATA_SLAVE && ch->device[SLAVE].driver)
887 ad_detach(&ch->device[SLAVE], 0);
890 if (misdev & ATA_ATAPI_MASTER && ch->device[MASTER].driver)
891 atapi_detach(&ch->device[MASTER]);
892 if (misdev & ATA_ATAPI_SLAVE && ch->device[SLAVE].driver)
893 atapi_detach(&ch->device[SLAVE]);
895 if (misdev & ATA_ATA_MASTER || misdev & ATA_ATAPI_MASTER) {
896 if (ch->device[MASTER].param)
897 free(ch->device[MASTER].param, M_ATA);
898 ch->device[MASTER].param = NULL;
900 if (misdev & ATA_ATA_SLAVE || misdev & ATA_ATAPI_SLAVE) {
901 if (ch->device[SLAVE].param)
902 free(ch->device[SLAVE].param, M_ATA);
903 ch->device[SLAVE].param = NULL;
906 if ((newdev = ~devices & ch->devices)) {
907 if (newdev & ATA_ATA_MASTER)
908 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
909 ch->devices &= ~ATA_ATA_MASTER;
910 if (newdev & ATA_ATA_SLAVE)
911 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
912 ch->devices &= ~ATA_ATA_SLAVE;
913 if (newdev & ATA_ATAPI_MASTER)
914 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
915 ch->devices &= ~ATA_ATAPI_MASTER;
916 if (newdev & ATA_ATAPI_SLAVE)
917 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
918 ch->devices &= ~ATA_ATAPI_SLAVE;
920 newdev = ~devices & ch->devices;
921 if (!misdev && newdev)
924 if (newdev & ATA_ATA_MASTER && !ch->device[MASTER].driver)
925 ad_attach(&ch->device[MASTER], 1);
926 else if (ch->devices & ATA_ATA_MASTER && ch->device[MASTER].driver) {
927 ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY);
928 ad_reinit(&ch->device[MASTER]);
930 if (newdev & ATA_ATA_SLAVE && !ch->device[SLAVE].driver)
931 ad_attach(&ch->device[SLAVE], 1);
932 else if (ch->devices & (ATA_ATA_SLAVE) && ch->device[SLAVE].driver) {
933 ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY);
934 ad_reinit(&ch->device[SLAVE]);
938 if (newdev & ATA_ATAPI_MASTER && !ch->device[MASTER].driver)
939 atapi_attach(&ch->device[MASTER], 1);
940 else if (ch->devices & (ATA_ATAPI_MASTER) && ch->device[MASTER].driver) {
941 ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY);
942 atapi_reinit(&ch->device[MASTER]);
944 if (newdev & ATA_ATAPI_SLAVE && !ch->device[SLAVE].driver)
945 atapi_attach(&ch->device[SLAVE], 1);
946 else if (ch->devices & (ATA_ATAPI_SLAVE) && ch->device[SLAVE].driver) {
947 ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY);
948 atapi_reinit(&ch->device[SLAVE]);
952 if (ch->devices & (ATA_ATAPI_MASTER | ATA_ATAPI_SLAVE))
953 atapi_cam_reinit_bus(ch);
962 ata_service(struct ata_channel *ch)
964 /* do we have a SERVICE request from the drive ? */
965 if ((ch->status & (ATA_S_SERVICE|ATA_S_ERROR|ATA_S_DRQ)) == ATA_S_SERVICE) {
966 ATA_OUTB(ch->r_bmio, ATA_BMSTAT_PORT,
967 ata_dmastatus(ch) | ATA_BMSTAT_INTERRUPT);
969 if ((ATA_INB(ch->r_io, ATA_DRIVE) & ATA_SLAVE) == ATA_MASTER) {
970 if ((ch->devices & ATA_ATA_MASTER) && ch->device[MASTER].driver)
971 return ad_service((struct ad_softc *)
972 ch->device[MASTER].driver, 0);
975 if ((ch->devices & ATA_ATA_SLAVE) && ch->device[SLAVE].driver)
976 return ad_service((struct ad_softc *)
977 ch->device[SLAVE].driver, 0);
981 return ATA_OP_FINISHED;
985 ata_wait(struct ata_device *atadev, u_int8_t mask)
990 while (timeout < 5000000) { /* timeout 5 secs */
991 atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS);
993 /* if drive fails status, reselect the drive just to be sure */
994 if (atadev->channel->status == 0xff) {
995 ata_prtdev(atadev, "no status, reselecting device\n");
996 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM|atadev->unit);
998 atadev->channel->status = ATA_INB(atadev->channel->r_io,ATA_STATUS);
999 if (atadev->channel->status == 0xff)
1004 if (!(atadev->channel->status & ATA_S_BUSY))
1007 if (timeout > 1000) {
1016 if (atadev->channel->status & ATA_S_ERROR)
1017 atadev->channel->error = ATA_INB(atadev->channel->r_io, ATA_ERROR);
1018 if (timeout >= 5000000)
1021 return (atadev->channel->status & ATA_S_ERROR);
1023 /* Wait 50 msec for bits wanted. */
1026 atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS);
1027 if ((atadev->channel->status & mask) == mask) {
1028 if (atadev->channel->status & ATA_S_ERROR)
1029 atadev->channel->error=ATA_INB(atadev->channel->r_io,ATA_ERROR);
1030 return (atadev->channel->status & ATA_S_ERROR);
1038 ata_command(struct ata_device *atadev, u_int8_t command,
1039 u_int64_t lba, u_int16_t count, u_int8_t feature, int flags)
1043 ata_prtdev(atadev, "ata_command: addr=%04lx, cmd=%02x, "
1044 "lba=%lld, count=%d, feature=%d, flags=%02x\n",
1045 rman_get_start(atadev->channel->r_io),
1046 command, lba, count, feature, flags);
1050 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1052 /* disable interrupt from device */
1053 if (atadev->channel->flags & ATA_QUEUED)
1054 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_4BIT);
1056 /* ready to issue command ? */
1057 if (ata_wait(atadev, 0) < 0) {
1058 ata_prtdev(atadev, "timeout sending command=%02x s=%02x e=%02x\n",
1059 command, atadev->channel->status, atadev->channel->error);
1063 /* only use 48bit addressing if needed because of the overhead */
1064 if ((lba > 268435455 || count > 256) && atadev->param &&
1065 atadev->param->support.address48) {
1066 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, (feature>>8) & 0xff);
1067 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, feature);
1068 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, (count>>8) & 0xff);
1069 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, count & 0xff);
1070 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, (lba>>24) & 0xff);
1071 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, lba & 0xff);
1072 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>32) & 0xff);
1073 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>8) & 0xff);
1074 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>40) & 0xff);
1075 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>16) & 0xff);
1076 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_LBA | atadev->unit);
1078 /* translate command into 48bit version */
1081 command = ATA_C_READ48; break;
1082 case ATA_C_READ_MUL:
1083 command = ATA_C_READ_MUL48; break;
1084 case ATA_C_READ_DMA:
1085 command = ATA_C_READ_DMA48; break;
1086 case ATA_C_READ_DMA_QUEUED:
1087 command = ATA_C_READ_DMA_QUEUED48; break;
1089 command = ATA_C_WRITE48; break;
1090 case ATA_C_WRITE_MUL:
1091 command = ATA_C_WRITE_MUL48; break;
1092 case ATA_C_WRITE_DMA:
1093 command = ATA_C_WRITE_DMA48; break;
1094 case ATA_C_WRITE_DMA_QUEUED:
1095 command = ATA_C_WRITE_DMA_QUEUED48; break;
1096 case ATA_C_FLUSHCACHE:
1097 command = ATA_C_FLUSHCACHE48; break;
1099 ata_prtdev(atadev, "can't translate cmd to 48bit version\n");
1104 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, feature);
1105 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, count);
1106 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, lba & 0xff);
1107 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>8) & 0xff);
1108 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>16) & 0xff);
1109 if (atadev->flags & ATA_D_USE_CHS)
1110 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE,
1111 ATA_D_IBM | atadev->unit | ((lba>>24) & 0xf));
1113 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE,
1114 ATA_D_IBM | ATA_D_LBA | atadev->unit | ((lba>>24) &0xf));
1117 switch (flags & ATA_WAIT_MASK) {
1119 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1121 /* enable interrupt */
1122 if (atadev->channel->flags & ATA_QUEUED)
1123 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
1127 atadev->channel->active |= ATA_WAIT_INTR;
1128 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1130 /* enable interrupt */
1131 if (atadev->channel->flags & ATA_QUEUED)
1132 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
1134 if (tsleep((caddr_t)atadev->channel, 0, "atacmd", 10 * hz)) {
1135 ata_prtdev(atadev, "timeout waiting for interrupt\n");
1136 atadev->channel->active &= ~ATA_WAIT_INTR;
1141 case ATA_WAIT_READY:
1142 atadev->channel->active |= ATA_WAIT_READY;
1143 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1144 if (ata_wait(atadev, ATA_S_READY) < 0) {
1145 ata_prtdev(atadev, "timeout waiting for cmd=%02x s=%02x e=%02x\n",
1146 command, atadev->channel->status,atadev->channel->error);
1149 atadev->channel->active &= ~ATA_WAIT_READY;
1156 ata_enclosure_start(struct ata_device *atadev)
1158 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1160 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1162 ATA_INB(atadev->channel->r_io, ATA_CMD);
1164 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1166 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1168 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1170 ATA_INB(atadev->channel->r_io, ATA_COUNT);
1172 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1177 ata_enclosure_end(struct ata_device *atadev)
1179 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1184 ata_enclosure_chip_start(struct ata_device *atadev)
1186 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0b);
1187 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0a);
1189 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08);
1193 ata_enclosure_chip_end(struct ata_device *atadev)
1195 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08);
1197 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0a);
1199 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0b);
1204 ata_enclosure_chip_rdbit(struct ata_device *atadev)
1208 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0);
1210 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x02);
1212 val = ATA_INB(atadev->channel->r_io, ATA_SECTOR) & 0x01;
1218 ata_enclosure_chip_wrbit(struct ata_device *atadev, u_int8_t data)
1220 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08 | (data & 0x01));
1222 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08 | 0x02 | (data & 0x01));
1227 ata_enclosure_chip_rw(struct ata_device *atadev, int rw, u_int8_t val)
1232 for (i = 0; i < 8; i++)
1233 ata_enclosure_chip_wrbit(atadev, (val & (0x80 >> i)) ? 1 : 0);
1236 for (i = 0; i < 8; i++)
1237 val = (val << 1) | ata_enclosure_chip_rdbit(atadev);
1239 ata_enclosure_chip_wrbit(atadev, 0);
1244 ata_enclosure_sensor(struct ata_device *atadev,
1245 int rw, u_int8_t idx, u_int8_t data)
1247 ata_enclosure_start(atadev);
1248 ata_enclosure_chip_start(atadev);
1249 ata_enclosure_chip_rw(atadev, 1, 0x5a);
1250 ata_enclosure_chip_rw(atadev, 1, idx);
1252 ata_enclosure_chip_rw(atadev, 1, data);
1255 ata_enclosure_chip_end(atadev);
1256 ata_enclosure_chip_start(atadev);
1257 ata_enclosure_chip_rw(atadev, 1, 0x5b);
1258 data = ata_enclosure_chip_rw(atadev, 0, 0);
1260 ata_enclosure_chip_end(atadev);
1261 ata_enclosure_end(atadev);
1266 ata_enclosure_status(struct ata_device *atadev,
1267 int *fan, int *temp, int *v05, int *v12)
1269 u_int8_t id1, id2, cnt, div;
1272 if (atadev->flags & ATA_D_ENC_PRESENT) {
1273 ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL);
1274 ata_enclosure_sensor(atadev, 1, 0x4e, 0);
1275 id1 = ata_enclosure_sensor(atadev, 0, 0x4f, 0);
1276 ata_enclosure_sensor(atadev, 1, 0x4e, 0x80);
1277 id2 = ata_enclosure_sensor(atadev, 0, 0x4f, 0);
1278 if (id1 == 0xa3 && id2 == 0x5c) {
1279 div = 1 << (((ata_enclosure_sensor(atadev, 0, 0x5d, 0)&0x20)>>3)+
1280 ((ata_enclosure_sensor(atadev, 0, 0x47, 0)&0x30)>>4)+1);
1281 cnt = ata_enclosure_sensor(atadev, 0, 0x28, 0);
1285 *fan = 1350000 / cnt / div;
1286 ata_enclosure_sensor(atadev, 1, 0x4e, 0x01);
1287 *temp = (ata_enclosure_sensor(atadev, 0, 0x50, 0) * 10) +
1288 (ata_enclosure_sensor(atadev, 0, 0x50, 0) & 0x80 ? 5 : 0);
1289 *v05 = ata_enclosure_sensor(atadev, 0, 0x23, 0) * 27;
1290 *v12 = ata_enclosure_sensor(atadev, 0, 0x24, 0) * 61;
1293 ATA_UNLOCK_CH(atadev->channel);
1299 ata_enclosure_print(struct ata_device *atadev)
1302 int fan, temp, v05, v12;
1304 ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL);
1305 ata_enclosure_start(atadev);
1306 id = ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1308 st = ATA_INB(atadev->channel->r_io, ATA_COUNT);
1310 ata_enclosure_end(atadev);
1311 ATA_UNLOCK_CH(atadev->channel);
1313 switch (id & 0x93) {
1315 ata_prtdev(atadev, "Universal enclosure");
1318 ata_prtdev(atadev, "FastSwap enclosure");
1322 ata_prtdev(atadev, "SuperSwap enclosure");
1325 atadev->flags &= ~ATA_D_ENC_PRESENT;
1328 atadev->flags |= ATA_D_ENC_PRESENT;
1330 if (ata_enclosure_status(atadev, &fan, &temp, &v05, &v12))
1331 printf(" detected\n");
1333 printf(" [FAN:%drpm TEMP:%d.%01dC %d.%03dV %d.%03dV]\n",
1334 fan, temp/10, temp%10, v05/1000, v05%1000, v12/1000, v12%1000);
1338 ata_enclosure_leds(struct ata_device *atadev, u_int8_t color)
1340 if (atadev->flags & ATA_D_ENC_PRESENT) {
1343 ata_enclosure_start(atadev);
1344 reg = ATA_INB(atadev->channel->r_io, ATA_COUNT);
1346 ATA_OUTB(atadev->channel->r_io, ATA_COUNT,
1347 (color & ATA_LED_MASK) | (reg & ~ATA_LED_MASK));
1349 ata_enclosure_end(atadev);
1354 ata_change_mode(struct ata_device *atadev, int mode)
1356 int umode, wmode, pmode;
1358 umode = ata_umode(atadev->param);
1359 wmode = ata_wmode(atadev->param);
1360 pmode = ata_pmode(atadev->param);
1362 switch (mode & ATA_DMA_MASK) {
1364 if ((mode & ATA_MODE_MASK) < umode)
1365 umode = mode & ATA_MODE_MASK;
1368 if ((mode & ATA_MODE_MASK) < wmode)
1369 wmode = mode & ATA_MODE_MASK;
1373 if (((mode & ATA_MODE_MASK) - ATA_PIO0) < pmode)
1374 pmode = (mode & ATA_MODE_MASK) - ATA_PIO0;
1379 ATA_SLEEPLOCK_CH(atadev->channel, ATA_ACTIVE);
1380 ata_dmainit(atadev->channel, atadev->unit, pmode, wmode, umode);
1381 ATA_UNLOCK_CH(atadev->channel);
1382 ata_start(atadev->channel); /* XXX SOS */
1386 ata_printf(struct ata_channel *ch, int device, const char * fmt, ...)
1392 ret = printf("ata%d: ", device_get_unit(ch->dev));
1394 if (ch->device[ATA_DEV(device)].name)
1395 ret = printf("%s: ", ch->device[ATA_DEV(device)].name);
1397 ret = printf("ata%d-%s: ", device_get_unit(ch->dev),
1398 (device == ATA_MASTER) ? "master" : "slave");
1400 __va_start(ap, fmt);
1401 ret += vprintf(fmt, ap);
1407 ata_prtdev(struct ata_device *atadev, const char * fmt, ...)
1413 ret = printf("%s: ", atadev->name);
1415 ret = printf("ata%d-%s: ", device_get_unit(atadev->channel->dev),
1416 (atadev->unit == ATA_MASTER) ? "master" : "slave");
1417 __va_start(ap, fmt);
1418 ret += vprintf(fmt, ap);
1424 ata_set_name(struct ata_device *atadev, char *name, int lun)
1426 atadev->name = malloc(strlen(name) + 4, M_ATA, M_WAITOK);
1428 sprintf(atadev->name, "%s%d", name, lun);
1432 ata_free_name(struct ata_device *atadev)
1435 free(atadev->name, M_ATA);
1436 atadev->name = NULL;
1440 ata_get_lun(u_int32_t *map)
1442 int lun = ffs(~*map) - 1;
1449 ata_test_lun(u_int32_t *map, int lun)
1451 return (*map & (1 << lun));
1455 ata_free_lun(u_int32_t *map, int lun)
1457 *map &= ~(1 << lun);
1461 ata_mode2str(int mode)
1464 case ATA_PIO: return "BIOSPIO";
1465 case ATA_PIO0: return "PIO0";
1466 case ATA_PIO1: return "PIO1";
1467 case ATA_PIO2: return "PIO2";
1468 case ATA_PIO3: return "PIO3";
1469 case ATA_PIO4: return "PIO4";
1470 case ATA_DMA: return "BIOSDMA";
1471 case ATA_WDMA2: return "WDMA2";
1472 case ATA_UDMA2: return "UDMA33";
1473 case ATA_UDMA4: return "UDMA66";
1474 case ATA_UDMA5: return "UDMA100";
1475 case ATA_UDMA6: return "UDMA133";
1476 default: return "???";
1481 ata_pmode(struct ata_params *ap)
1483 if (ap->atavalid & ATA_FLAG_64_70) {
1484 if (ap->apiomodes & 2)
1486 if (ap->apiomodes & 1)
1489 if (ap->retired_piomode == 2)
1491 if (ap->retired_piomode == 1)
1493 if (ap->retired_piomode == 0)
1499 ata_wmode(struct ata_params *ap)
1501 if (ap->mwdmamodes & 0x04)
1503 if (ap->mwdmamodes & 0x02)
1505 if (ap->mwdmamodes & 0x01)
1511 ata_umode(struct ata_params *ap)
1513 if (ap->atavalid & ATA_FLAG_88) {
1514 if (ap->udmamodes & 0x40)
1516 if (ap->udmamodes & 0x20)
1518 if (ap->udmamodes & 0x10)
1520 if (ap->udmamodes & 0x08)
1522 if (ap->udmamodes & 0x04)
1524 if (ap->udmamodes & 0x02)
1526 if (ap->udmamodes & 0x01)
1533 bswap(int8_t *buf, int len)
1535 u_int16_t *ptr = (u_int16_t*)(buf + len);
1537 while (--ptr >= (u_int16_t*)buf)
1542 btrim(int8_t *buf, int len)
1546 for (ptr = buf; ptr < buf+len; ++ptr)
1549 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
1554 bpack(int8_t *src, int8_t *dst, int len)
1558 for (i = j = blank = 0 ; i < len; i++) {
1559 if (blank && src[i] == ' ') continue;
1560 if (blank && src[i] != ' ') {
1565 if (src[i] == ' ') {
1579 /* register controlling device */
1580 make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "ata");
1582 /* register boot attach to be run when interrupts are enabled */
1583 if (!(ata_delayed_attach = (struct intr_config_hook *)
1584 malloc(sizeof(struct intr_config_hook),
1585 M_TEMP, M_WAITOK | M_ZERO))) {
1586 printf("ata: malloc of delayed attach hook failed\n");
1590 ata_delayed_attach->ich_func = (void*)ata_boot_attach;
1591 if (config_intrhook_establish(ata_delayed_attach) != 0) {
1592 printf("ata: config_intrhook_establish failed\n");
1593 free(ata_delayed_attach, M_TEMP);
1596 SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL)