2 * Copyright (c) 1998 - 2005 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.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/endian.h>
39 #include <sys/ctype.h>
43 #include <sys/malloc.h>
44 #include <sys/sysctl.h>
46 #include <sys/taskqueue.h>
48 #include <machine/stdarg.h>
49 #include <machine/resource.h>
50 #include <machine/bus.h>
53 #include <machine/md_var.h>
55 #include <dev/ata/ata-all.h>
58 /* device structure */
59 static d_ioctl_t ata_ioctl;
60 static struct cdevsw ata_cdevsw = {
61 .d_version = D_VERSION,
62 .d_flags = D_NEEDGIANT, /* we need this as newbus isn't mpsafe */
68 static void ata_interrupt(void *);
69 static void ata_boot_attach(void);
70 static device_t ata_add_child(device_t, struct ata_device *, int);
71 static int ata_getparam(struct ata_device *, int);
72 static void bswap(int8_t *, int);
73 static void btrim(int8_t *, int);
74 static void bpack(int8_t *, int8_t *, int);
77 MALLOC_DEFINE(M_ATA, "ata_generic", "ATA driver generic layer");
78 int (*ata_raid_ioctl_func)(u_long cmd, caddr_t data) = NULL;
79 devclass_t ata_devclass;
80 uma_zone_t ata_request_zone;
81 uma_zone_t ata_composite_zone;
85 static struct intr_config_hook *ata_delayed_attach = NULL;
86 static int ata_dma = 1;
87 static int atapi_dma = 1;
90 SYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters");
91 TUNABLE_INT("hw.ata.ata_dma", &ata_dma);
92 SYSCTL_INT(_hw_ata, OID_AUTO, ata_dma, CTLFLAG_RDTUN, &ata_dma, 0,
93 "ATA disk DMA mode control");
94 TUNABLE_INT("hw.ata.atapi_dma", &atapi_dma);
95 SYSCTL_INT(_hw_ata, OID_AUTO, atapi_dma, CTLFLAG_RDTUN, &atapi_dma, 0,
96 "ATAPI device DMA mode control");
97 TUNABLE_INT("hw.ata.wc", &ata_wc);
98 SYSCTL_INT(_hw_ata, OID_AUTO, wc, CTLFLAG_RDTUN, &ata_wc, 0,
99 "ATA disk write caching");
102 * newbus device interface related functions
105 ata_probe(device_t dev)
111 ata_attach(device_t dev)
113 struct ata_channel *ch = device_get_softc(dev);
116 /* check that we have a virgin channel to attach */
120 /* initialize the softc basics */
122 ch->state = ATA_IDLE;
123 bzero(&ch->state_mtx, sizeof(struct mtx));
124 mtx_init(&ch->state_mtx, "ATA state lock", NULL, MTX_DEF);
125 bzero(&ch->queue_mtx, sizeof(struct mtx));
126 mtx_init(&ch->queue_mtx, "ATA queue lock", NULL, MTX_DEF);
127 TAILQ_INIT(&ch->ata_queue);
129 /* reset the controller HW, the channel and device(s) */
130 while (ATA_LOCKING(dev, ATA_LF_LOCK) != ch->unit)
131 tsleep(&error, PRIBIO, "ataatch", 1);
133 ATA_LOCKING(dev, ATA_LF_UNLOCK);
135 /* setup interrupt delivery */
137 ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
138 RF_SHAREABLE | RF_ACTIVE);
140 device_printf(dev, "unable to allocate interrupt\n");
143 if ((error = bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS,
144 ata_interrupt, ch, &ch->ih))) {
145 device_printf(dev, "unable to setup interrupt\n");
149 /* probe and attach devices on this channel unless we are in early boot */
150 if (!ata_delayed_attach)
156 ata_detach(device_t dev)
158 struct ata_channel *ch = device_get_softc(dev);
162 /* check that we have a valid channel to detach */
166 /* detach & delete all children */
167 if (!device_get_children(dev, &children, &nchildren)) {
168 for (i = 0; i < nchildren; i++)
170 device_delete_child(dev, children[i]);
171 free(children, M_TEMP);
174 /* release resources */
175 bus_teardown_intr(dev, ch->r_irq, ch->ih);
176 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
178 mtx_destroy(&ch->state_mtx);
179 mtx_destroy(&ch->queue_mtx);
184 ata_reinit(device_t dev)
186 struct ata_channel *ch = device_get_softc(dev);
187 struct ata_request *request;
191 /* check that we have a valid channel to reinit */
192 if (!ch || !ch->r_irq)
196 device_printf(dev, "reiniting channel ..\n");
198 /* poll for locking the channel */
199 while (ATA_LOCKING(dev, ATA_LF_LOCK) != ch->unit)
200 tsleep(&dev, PRIBIO, "atarini", 1);
202 /* unconditionally grap the channel lock */
203 mtx_lock(&ch->state_mtx);
204 ch->state = ATA_STALL_QUEUE;
205 mtx_unlock(&ch->state_mtx);
207 /* reset the controller HW, the channel and device(s) */
210 /* reinit the children and delete any that fails */
211 if (!device_get_children(dev, &children, &nchildren)) {
212 mtx_lock(&Giant); /* newbus suckage it needs Giant */
213 for (i = 0; i < nchildren; i++) {
214 if (children[i] && device_is_attached(children[i]))
215 if (ATA_REINIT(children[i])) {
217 * if we have a running request and its device matches
218 * this child we need to inform the request that the
219 * device is gone and remove it from ch->running
221 mtx_lock(&ch->state_mtx);
222 if (ch->running && ch->running->dev == children[i]) {
223 callout_stop(&ch->running->callout);
224 request = ch->running;
229 mtx_unlock(&ch->state_mtx);
232 request->result = ENXIO;
233 device_printf(request->dev,
234 "FAILURE - device detached\n");
236 /* if not timeout finish request here */
237 if (!(request->flags & ATA_R_TIMEOUT))
240 device_delete_child(dev, children[i]);
243 free(children, M_TEMP);
244 mtx_unlock(&Giant); /* newbus suckage dealt with, release Giant */
247 /* catch request in ch->running if we havn't already */
248 mtx_lock(&ch->state_mtx);
249 if ((request = ch->running))
250 callout_stop(&request->callout);
252 mtx_unlock(&ch->state_mtx);
254 /* if we got one put it on the queue again */
256 device_printf(request->dev,
257 "WARNING - %s requeued due to channel reset",
258 ata_cmd2str(request));
259 if (!(request->flags & (ATA_R_ATAPI | ATA_R_CONTROL)))
260 printf(" LBA=%llu", (unsigned long long)request->u.ata.lba);
262 request->flags |= ATA_R_REQUEUE;
263 ata_queue_request(request);
266 /* we're done release the channel for new work */
267 mtx_lock(&ch->state_mtx);
268 ch->state = ATA_IDLE;
269 mtx_unlock(&ch->state_mtx);
270 ATA_LOCKING(dev, ATA_LF_UNLOCK);
273 device_printf(dev, "reinit done ..\n");
275 /* kick off requests on the queue */
281 ata_suspend(device_t dev)
283 struct ata_channel *ch;
285 /* check for valid device */
286 if (!dev || !(ch = device_get_softc(dev)))
289 /* wait for the channel to be IDLE before entering suspend mode */
291 mtx_lock(&ch->state_mtx);
292 if (ch->state == ATA_IDLE) {
293 ch->state = ATA_ACTIVE;
294 mtx_unlock(&ch->state_mtx);
297 mtx_unlock(&ch->state_mtx);
298 tsleep(ch, PRIBIO, "atasusp", hz/10);
300 ATA_LOCKING(dev, ATA_LF_UNLOCK);
305 ata_resume(device_t dev)
307 struct ata_channel *ch;
310 /* check for valid device */
311 if (!dev || !(ch = device_get_softc(dev)))
314 /* reinit the devices, we dont know what mode/state they are in */
315 error = ata_reinit(dev);
317 /* kick off requests on the queue */
323 ata_interrupt(void *data)
325 struct ata_channel *ch = (struct ata_channel *)data;
326 struct ata_request *request;
328 mtx_lock(&ch->state_mtx);
330 /* do we have a running request */
331 if (!(request = ch->running))
334 ATA_DEBUG_RQ(request, "interrupt");
336 /* ignore interrupt if device is busy */
337 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) {
339 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY)
343 /* check for the right state */
344 if (ch->state != ATA_ACTIVE && ch->state != ATA_STALL_QUEUE) {
345 device_printf(request->dev, "interrupt on idle channel ignored\n");
350 * we have the HW locks, so end the tranaction for this request
351 * if it finishes immediately otherwise wait for next interrupt
353 if (ch->hw.end_transaction(request) == ATA_OP_FINISHED) {
355 if (ch->state == ATA_ACTIVE)
356 ch->state = ATA_IDLE;
357 mtx_unlock(&ch->state_mtx);
358 ATA_LOCKING(ch->dev, ATA_LF_UNLOCK);
363 mtx_unlock(&ch->state_mtx);
367 * device related interfaces
370 ata_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
371 int32_t flag, struct thread *td)
373 device_t device, *children;
374 struct ata_ioc_devices *devices = (struct ata_ioc_devices *)data;
375 int *value = (int *)data;
376 int i, nchildren, error = ENOTTY;
379 case IOCATAGMAXCHANNEL:
380 *value = devclass_get_maxunit(ata_devclass);
385 if (*value > devclass_get_maxunit(ata_devclass) ||
386 !(device = devclass_get_device(ata_devclass, *value)))
388 error = ata_reinit(device);
393 if (*value > devclass_get_maxunit(ata_devclass) ||
394 !(device = devclass_get_device(ata_devclass, *value)))
396 /* XXX SOS should enable channel HW on controller */
397 error = ata_attach(device);
401 if (*value > devclass_get_maxunit(ata_devclass) ||
402 !(device = devclass_get_device(ata_devclass, *value)))
404 error = ata_detach(device);
405 /* XXX SOS should disable channel HW on controller */
409 if (devices->channel > devclass_get_maxunit(ata_devclass) ||
410 !(device = devclass_get_device(ata_devclass, devices->channel)))
412 bzero(devices->name[0], 32);
413 bzero(&devices->params[0], sizeof(struct ata_params));
414 bzero(devices->name[1], 32);
415 bzero(&devices->params[1], sizeof(struct ata_params));
416 if (!device_get_children(device, &children, &nchildren)) {
417 for (i = 0; i < nchildren; i++) {
418 if (children[i] && device_is_attached(children[i])) {
419 struct ata_device *atadev = device_get_softc(children[i]);
421 if (atadev->unit == ATA_MASTER) {
422 strncpy(devices->name[0],
423 device_get_nameunit(children[i]), 32);
424 bcopy(&atadev->param, &devices->params[0],
425 sizeof(struct ata_params));
427 if (atadev->unit == ATA_SLAVE) {
428 strncpy(devices->name[1],
429 device_get_nameunit(children[i]), 32);
430 bcopy(&atadev->param, &devices->params[1],
431 sizeof(struct ata_params));
435 free(children, M_TEMP);
443 if (ata_raid_ioctl_func)
444 error = ata_raid_ioctl_func(cmd, data);
450 ata_device_ioctl(device_t dev, u_long cmd, caddr_t data)
452 struct ata_device *atadev = device_get_softc(dev);
453 struct ata_ioc_request *ioc_request = (struct ata_ioc_request *)data;
454 struct ata_params *params = (struct ata_params *)data;
455 int *mode = (int *)data;
456 struct ata_request *request;
462 if (!(buf = malloc(ioc_request->count, M_ATA, M_NOWAIT))) {
465 if (!(request = ata_alloc_request())) {
469 if (ioc_request->flags & ATA_CMD_WRITE) {
470 error = copyin(ioc_request->data, buf, ioc_request->count);
473 ata_free_request(request);
478 if (ioc_request->flags & ATA_CMD_ATAPI) {
479 request->flags = ATA_R_ATAPI;
480 bcopy(ioc_request->u.atapi.ccb, request->u.atapi.ccb, 16);
483 request->u.ata.command = ioc_request->u.ata.command;
484 request->u.ata.feature = ioc_request->u.ata.feature;
485 request->u.ata.lba = ioc_request->u.ata.lba;
486 request->u.ata.count = ioc_request->u.ata.count;
488 request->timeout = ioc_request->timeout;
490 request->bytecount = ioc_request->count;
491 request->transfersize = request->bytecount;
492 if (ioc_request->flags & ATA_CMD_CONTROL)
493 request->flags |= ATA_R_CONTROL;
494 if (ioc_request->flags & ATA_CMD_READ)
495 request->flags |= ATA_R_READ;
496 if (ioc_request->flags & ATA_CMD_WRITE)
497 request->flags |= ATA_R_WRITE;
498 ata_queue_request(request);
499 if (!(request->flags & ATA_R_ATAPI)) {
500 ioc_request->u.ata.command = request->u.ata.command;
501 ioc_request->u.ata.feature = request->u.ata.feature;
502 ioc_request->u.ata.lba = request->u.ata.lba;
503 ioc_request->u.ata.count = request->u.ata.count;
505 ioc_request->error = request->result;
506 if (ioc_request->flags & ATA_CMD_READ)
507 error = copyout(buf, ioc_request->data, ioc_request->count);
511 ata_free_request(request);
515 ata_getparam(atadev, 0);
516 bcopy(&atadev->param, params, sizeof(struct ata_params));
520 atadev->mode = *mode;
521 ATA_SETMODE(device_get_parent(dev), dev);
525 *mode = atadev->mode;
533 ata_boot_attach(void)
535 struct ata_channel *ch;
538 mtx_lock(&Giant); /* newbus suckage it needs Giant */
540 /* kick of probe and attach on all channels */
541 for (ctlr = 0; ctlr < devclass_get_maxunit(ata_devclass); ctlr++) {
542 if ((ch = devclass_get_softc(ata_devclass, ctlr))) {
543 ata_identify(ch->dev);
547 /* release the hook that got us here, we are only needed once during boot */
548 if (ata_delayed_attach) {
549 config_intrhook_disestablish(ata_delayed_attach);
550 ata_delayed_attach = NULL;
551 free(ata_delayed_attach, M_TEMP);
554 mtx_unlock(&Giant); /* newbus suckage dealt with, release Giant */
559 * misc support functions
562 ata_add_child(device_t parent, struct ata_device *atadev, int unit)
566 if ((child = device_add_child(parent, NULL, unit))) {
567 device_set_softc(child, atadev);
570 atadev->max_iosize = DEV_BSIZE;
571 atadev->mode = ATA_PIO_MAX;
577 ata_getparam(struct ata_device *atadev, int init)
579 struct ata_channel *ch = device_get_softc(device_get_parent(atadev->dev));
580 struct ata_request *request;
581 u_int8_t command = 0;
582 int error = ENOMEM, retries = 2;
585 (atadev->unit == ATA_MASTER ? ATA_ATA_MASTER : ATA_ATA_SLAVE))
586 command = ATA_ATA_IDENTIFY;
588 (atadev->unit == ATA_MASTER ? ATA_ATAPI_MASTER : ATA_ATAPI_SLAVE))
589 command = ATA_ATAPI_IDENTIFY;
593 while (retries-- > 0 && error) {
594 if (!(request = ata_alloc_request()))
596 request->dev = atadev->dev;
597 request->timeout = 1;
598 request->retries = 0;
599 request->u.ata.command = command;
600 request->flags = (ATA_R_READ|ATA_R_AT_HEAD|ATA_R_DIRECT|ATA_R_QUIET);
601 request->data = (void *)&atadev->param;
602 request->bytecount = sizeof(struct ata_params);
603 request->donecount = 0;
604 request->transfersize = DEV_BSIZE;
605 ata_queue_request(request);
606 error = request->result;
607 ata_free_request(request);
610 if (!error && (isprint(atadev->param.model[0]) ||
611 isprint(atadev->param.model[1]))) {
612 struct ata_params *atacap = &atadev->param;
614 #if BYTE_ORDER == BIG_ENDIAN
617 for (ptr = (int16_t *)atacap;
618 ptr < (int16_t *)atacap + sizeof(struct ata_params)/2; ptr++) {
619 *ptr = bswap16(*ptr);
622 if (!(!strncmp(atacap->model, "FX", 2) ||
623 !strncmp(atacap->model, "NEC", 3) ||
624 !strncmp(atacap->model, "Pioneer", 7) ||
625 !strncmp(atacap->model, "SHARP", 5))) {
626 bswap(atacap->model, sizeof(atacap->model));
627 bswap(atacap->revision, sizeof(atacap->revision));
628 bswap(atacap->serial, sizeof(atacap->serial));
630 btrim(atacap->model, sizeof(atacap->model));
631 bpack(atacap->model, atacap->model, sizeof(atacap->model));
632 btrim(atacap->revision, sizeof(atacap->revision));
633 bpack(atacap->revision, atacap->revision, sizeof(atacap->revision));
634 btrim(atacap->serial, sizeof(atacap->serial));
635 bpack(atacap->serial, atacap->serial, sizeof(atacap->serial));
638 printf("ata%d-%s: pio=%s wdma=%s udma=%s cable=%s wire\n",
639 ch->unit, atadev->unit == ATA_MASTER ? "master":"slave",
640 ata_mode2str(ata_pmode(atacap)),
641 ata_mode2str(ata_wmode(atacap)),
642 ata_mode2str(ata_umode(atacap)),
643 (atacap->hwres & ATA_CABLE_ID) ? "80":"40");
646 sprintf(buffer, "%.40s/%.8s", atacap->model, atacap->revision);
647 device_set_desc_copy(atadev->dev, buffer);
648 if (atadev->param.config & ATA_PROTO_ATAPI) {
649 if (atapi_dma && ch->dma &&
650 (atadev->param.config & ATA_DRQ_MASK) != ATA_DRQ_INTR &&
651 ata_umode(&atadev->param) >= ATA_UDMA2)
652 atadev->mode = ATA_DMA_MAX;
655 if (ata_dma && ch->dma &&
656 (ata_umode(&atadev->param) > 0 ||
657 ata_wmode(&atadev->param) > 0))
658 atadev->mode = ATA_DMA_MAX;
670 ata_identify(device_t dev)
672 struct ata_channel *ch = device_get_softc(dev);
673 struct ata_device *master = NULL, *slave = NULL;
674 device_t master_child = NULL, slave_child = NULL;
675 int master_unit = -1, slave_unit = -1;
677 if (ch->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER)) {
678 if (!(master = malloc(sizeof(struct ata_device),
679 M_ATA, M_NOWAIT | M_ZERO))) {
680 device_printf(dev, "out of memory\n");
683 master->unit = ATA_MASTER;
685 if (ch->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE)) {
686 if (!(slave = malloc(sizeof(struct ata_device),
687 M_ATA, M_NOWAIT | M_ZERO))) {
689 device_printf(dev, "out of memory\n");
692 slave->unit = ATA_SLAVE;
696 if (ch->devices & ATA_ATA_MASTER)
697 master_unit = (device_get_unit(dev) << 1);
699 if (master && !(master_child = ata_add_child(dev, master, master_unit))) {
704 if (ch->devices & ATA_ATA_SLAVE)
705 slave_unit = (device_get_unit(dev) << 1) + 1;
707 if (slave && !(slave_child = ata_add_child(dev, slave, slave_unit))) {
712 if (slave && ata_getparam(slave, 1)) {
713 device_delete_child(dev, slave_child);
716 if (master && ata_getparam(master, 1)) {
717 device_delete_child(dev, master_child);
721 bus_generic_probe(dev);
722 bus_generic_attach(dev);
727 ata_default_registers(device_t dev)
729 struct ata_channel *ch = device_get_softc(dev);
731 /* fill in the defaults from whats setup already */
732 ch->r_io[ATA_ERROR].res = ch->r_io[ATA_FEATURE].res;
733 ch->r_io[ATA_ERROR].offset = ch->r_io[ATA_FEATURE].offset;
734 ch->r_io[ATA_IREASON].res = ch->r_io[ATA_COUNT].res;
735 ch->r_io[ATA_IREASON].offset = ch->r_io[ATA_COUNT].offset;
736 ch->r_io[ATA_STATUS].res = ch->r_io[ATA_COMMAND].res;
737 ch->r_io[ATA_STATUS].offset = ch->r_io[ATA_COMMAND].offset;
738 ch->r_io[ATA_ALTSTAT].res = ch->r_io[ATA_CONTROL].res;
739 ch->r_io[ATA_ALTSTAT].offset = ch->r_io[ATA_CONTROL].offset;
743 ata_modify_if_48bit(struct ata_request *request)
745 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
746 struct ata_device *atadev = device_get_softc(request->dev);
748 atadev->flags &= ~ATA_D_48BIT_ACTIVE;
750 if ((request->u.ata.lba >= ATA_MAX_28BIT_LBA ||
751 request->u.ata.count > 256) &&
752 atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) {
754 /* translate command into 48bit version */
755 switch (request->u.ata.command) {
757 request->u.ata.command = ATA_READ48;
760 request->u.ata.command = ATA_READ_MUL48;
763 if (ch->flags & ATA_NO_48BIT_DMA) {
764 if (request->transfersize > DEV_BSIZE)
765 request->u.ata.command = ATA_READ_MUL48;
767 request->u.ata.command = ATA_READ48;
768 request->flags &= ~ATA_R_DMA;
771 request->u.ata.command = ATA_READ_DMA48;
773 case ATA_READ_DMA_QUEUED:
774 if (ch->flags & ATA_NO_48BIT_DMA) {
775 if (request->transfersize > DEV_BSIZE)
776 request->u.ata.command = ATA_READ_MUL48;
778 request->u.ata.command = ATA_READ48;
779 request->flags &= ~ATA_R_DMA;
782 request->u.ata.command = ATA_READ_DMA_QUEUED48;
785 request->u.ata.command = ATA_WRITE48;
788 request->u.ata.command = ATA_WRITE_MUL48;
791 if (ch->flags & ATA_NO_48BIT_DMA) {
792 if (request->transfersize > DEV_BSIZE)
793 request->u.ata.command = ATA_WRITE_MUL48;
795 request->u.ata.command = ATA_WRITE48;
796 request->flags &= ~ATA_R_DMA;
799 request->u.ata.command = ATA_WRITE_DMA48;
801 case ATA_WRITE_DMA_QUEUED:
802 if (ch->flags & ATA_NO_48BIT_DMA) {
803 if (request->transfersize > DEV_BSIZE)
804 request->u.ata.command = ATA_WRITE_MUL48;
806 request->u.ata.command = ATA_WRITE48;
807 request->u.ata.command = ATA_WRITE48;
808 request->flags &= ~ATA_R_DMA;
811 request->u.ata.command = ATA_WRITE_DMA_QUEUED48;
814 request->u.ata.command = ATA_FLUSHCACHE48;
816 case ATA_READ_NATIVE_MAX_ADDDRESS:
817 request->u.ata.command = ATA_READ_NATIVE_MAX_ADDDRESS48;
819 case ATA_SET_MAX_ADDRESS:
820 request->u.ata.command = ATA_SET_MAX_ADDRESS48;
825 atadev->flags |= ATA_D_48BIT_ACTIVE;
830 ata_udelay(int interval)
832 /* for now just use DELAY, the timer/sleep subsytems are not there yet */
833 if (1 || interval < (1000000/hz) || ata_delayed_attach)
836 tsleep(&interval, PRIBIO, "ataslp", interval/(1000000/hz));
840 ata_mode2str(int mode)
843 case -1: return "UNSUPPORTED";
844 case ATA_PIO0: return "PIO0";
845 case ATA_PIO1: return "PIO1";
846 case ATA_PIO2: return "PIO2";
847 case ATA_PIO3: return "PIO3";
848 case ATA_PIO4: return "PIO4";
849 case ATA_WDMA0: return "WDMA0";
850 case ATA_WDMA1: return "WDMA1";
851 case ATA_WDMA2: return "WDMA2";
852 case ATA_UDMA0: return "UDMA16";
853 case ATA_UDMA1: return "UDMA25";
854 case ATA_UDMA2: return "UDMA33";
855 case ATA_UDMA3: return "UDMA40";
856 case ATA_UDMA4: return "UDMA66";
857 case ATA_UDMA5: return "UDMA100";
858 case ATA_UDMA6: return "UDMA133";
859 case ATA_SA150: return "SATA150";
860 case ATA_SA300: return "SATA300";
862 if (mode & ATA_DMA_MASK)
870 ata_pmode(struct ata_params *ap)
872 if (ap->atavalid & ATA_FLAG_64_70) {
873 if (ap->apiomodes & 0x02)
875 if (ap->apiomodes & 0x01)
878 if (ap->mwdmamodes & 0x04)
880 if (ap->mwdmamodes & 0x02)
882 if (ap->mwdmamodes & 0x01)
884 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200)
886 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100)
888 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000)
894 ata_wmode(struct ata_params *ap)
896 if (ap->mwdmamodes & 0x04)
898 if (ap->mwdmamodes & 0x02)
900 if (ap->mwdmamodes & 0x01)
906 ata_umode(struct ata_params *ap)
908 if (ap->atavalid & ATA_FLAG_88) {
909 if (ap->udmamodes & 0x40)
911 if (ap->udmamodes & 0x20)
913 if (ap->udmamodes & 0x10)
915 if (ap->udmamodes & 0x08)
917 if (ap->udmamodes & 0x04)
919 if (ap->udmamodes & 0x02)
921 if (ap->udmamodes & 0x01)
928 ata_limit_mode(device_t dev, int mode, int maxmode)
930 struct ata_device *atadev = device_get_softc(dev);
932 if (maxmode && mode > maxmode)
935 if (mode >= ATA_UDMA0 && ata_umode(&atadev->param) > 0)
936 return min(mode, ata_umode(&atadev->param));
938 if (mode >= ATA_WDMA0 && ata_wmode(&atadev->param) > 0)
939 return min(mode, ata_wmode(&atadev->param));
941 if (mode > ata_pmode(&atadev->param))
942 return min(mode, ata_pmode(&atadev->param));
948 bswap(int8_t *buf, int len)
950 u_int16_t *ptr = (u_int16_t*)(buf + len);
952 while (--ptr >= (u_int16_t*)buf)
957 btrim(int8_t *buf, int len)
961 for (ptr = buf; ptr < buf+len; ++ptr)
962 if (!*ptr || *ptr == '_')
964 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
969 bpack(int8_t *src, int8_t *dst, int len)
973 for (i = j = blank = 0 ; i < len; i++) {
974 if (blank && src[i] == ' ') continue;
975 if (blank && src[i] != ' ') {
996 ata_module_event_handler(module_t mod, int what, void *arg)
998 static struct cdev *atacdev;
1002 /* register controlling device */
1003 atacdev = make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "ata");
1006 /* register boot attach to be run when interrupts are enabled */
1007 if (!(ata_delayed_attach = (struct intr_config_hook *)
1008 malloc(sizeof(struct intr_config_hook),
1009 M_TEMP, M_NOWAIT | M_ZERO))) {
1010 printf("ata: malloc of delayed attach hook failed\n");
1013 ata_delayed_attach->ich_func = (void*)ata_boot_attach;
1014 if (config_intrhook_establish(ata_delayed_attach) != 0) {
1015 printf("ata: config_intrhook_establish failed\n");
1016 free(ata_delayed_attach, M_TEMP);
1022 /* deregister controlling device */
1023 destroy_dev(atacdev);
1031 static moduledata_t ata_moduledata = { "ata", ata_module_event_handler, NULL };
1032 DECLARE_MODULE(ata, ata_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
1033 MODULE_VERSION(ata, 1);
1038 ata_request_zone = uma_zcreate("ata_request", sizeof(struct ata_request),
1039 NULL, NULL, NULL, NULL, 0, 0);
1040 ata_composite_zone = uma_zcreate("ata_composite",
1041 sizeof(struct ata_composite),
1042 NULL, NULL, NULL, NULL, 0, 0);
1044 SYSINIT(ata_register, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL);
1049 uma_zdestroy(ata_composite_zone);
1050 uma_zdestroy(ata_request_zone);
1052 SYSUNINIT(ata_unregister, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_uninit, NULL);