2 * Copyright (c) 1998 - 2008 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.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/endian.h>
37 #include <sys/ctype.h>
41 #include <sys/malloc.h>
42 #include <sys/sysctl.h>
44 #include <sys/taskqueue.h>
46 #include <machine/stdarg.h>
47 #include <machine/resource.h>
48 #include <machine/bus.h>
50 #include <dev/ata/ata-all.h>
53 /* device structure */
54 static d_ioctl_t ata_ioctl;
55 static struct cdevsw ata_cdevsw = {
56 .d_version = D_VERSION,
57 .d_flags = D_NEEDGIANT, /* we need this as newbus isn't mpsafe */
63 static void ata_boot_attach(void);
64 static device_t ata_add_child(device_t, struct ata_device *, int);
65 static void bswap(int8_t *, int);
66 static void btrim(int8_t *, int);
67 static void bpack(int8_t *, int8_t *, int);
70 MALLOC_DEFINE(M_ATA, "ata_generic", "ATA driver generic layer");
71 int (*ata_raid_ioctl_func)(u_long cmd, caddr_t data) = NULL;
72 struct intr_config_hook *ata_delayed_attach = NULL;
73 devclass_t ata_devclass;
74 uma_zone_t ata_request_zone;
75 uma_zone_t ata_composite_zone;
80 static int ata_dma = 1;
81 static int atapi_dma = 1;
84 SYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters");
85 TUNABLE_INT("hw.ata.ata_dma", &ata_dma);
86 SYSCTL_INT(_hw_ata, OID_AUTO, ata_dma, CTLFLAG_RDTUN, &ata_dma, 0,
87 "ATA disk DMA mode control");
88 TUNABLE_INT("hw.ata.atapi_dma", &atapi_dma);
89 SYSCTL_INT(_hw_ata, OID_AUTO, atapi_dma, CTLFLAG_RDTUN, &atapi_dma, 0,
90 "ATAPI device DMA mode control");
91 TUNABLE_INT("hw.ata.wc", &ata_wc);
92 SYSCTL_INT(_hw_ata, OID_AUTO, wc, CTLFLAG_RDTUN, &ata_wc, 0,
93 "ATA disk write caching");
94 TUNABLE_INT("hw.ata.setmax", &ata_setmax);
95 SYSCTL_INT(_hw_ata, OID_AUTO, setmax, CTLFLAG_RDTUN, &ata_setmax, 0,
96 "ATA disk set max native address");
99 * newbus device interface related functions
102 ata_probe(device_t dev)
108 ata_attach(device_t dev)
110 struct ata_channel *ch = device_get_softc(dev);
113 /* check that we have a virgin channel to attach */
117 /* initialize the softc basics */
119 ch->state = ATA_IDLE;
120 bzero(&ch->state_mtx, sizeof(struct mtx));
121 mtx_init(&ch->state_mtx, "ATA state lock", NULL, MTX_DEF);
122 bzero(&ch->queue_mtx, sizeof(struct mtx));
123 mtx_init(&ch->queue_mtx, "ATA queue lock", NULL, MTX_DEF);
124 TAILQ_INIT(&ch->ata_queue);
126 /* reset the controller HW, the channel and device(s) */
127 while (ATA_LOCKING(dev, ATA_LF_LOCK) != ch->unit)
130 ATA_LOCKING(dev, ATA_LF_UNLOCK);
132 /* allocate DMA resources if DMA HW present*/
136 /* setup interrupt delivery */
138 ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
139 RF_SHAREABLE | RF_ACTIVE);
141 device_printf(dev, "unable to allocate interrupt\n");
144 if ((error = bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS, NULL,
145 (driver_intr_t *)ata_interrupt, ch, &ch->ih))) {
146 device_printf(dev, "unable to setup interrupt\n");
150 /* probe and attach devices on this channel unless we are in early boot */
151 if (!ata_delayed_attach)
157 ata_detach(device_t dev)
159 struct ata_channel *ch = device_get_softc(dev);
163 /* check that we have a valid channel to detach */
167 /* grap the channel lock so no new requests gets launched */
168 mtx_lock(&ch->state_mtx);
169 ch->state |= ATA_STALL_QUEUE;
170 mtx_unlock(&ch->state_mtx);
172 /* detach & delete all children */
173 if (!device_get_children(dev, &children, &nchildren)) {
174 for (i = 0; i < nchildren; i++)
176 device_delete_child(dev, children[i]);
177 free(children, M_TEMP);
180 /* release resources */
181 bus_teardown_intr(dev, ch->r_irq, ch->ih);
182 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
184 mtx_destroy(&ch->state_mtx);
185 mtx_destroy(&ch->queue_mtx);
190 ata_reinit(device_t dev)
192 struct ata_channel *ch = device_get_softc(dev);
193 struct ata_request *request;
197 /* check that we have a valid channel to reinit */
198 if (!ch || !ch->r_irq)
202 device_printf(dev, "reiniting channel ..\n");
204 /* poll for locking the channel */
205 while (ATA_LOCKING(dev, ATA_LF_LOCK) != ch->unit)
208 /* catch eventual request in ch->running */
209 mtx_lock(&ch->state_mtx);
210 if ((request = ch->running))
211 callout_stop(&request->callout);
214 /* unconditionally grap the channel lock */
215 ch->state |= ATA_STALL_QUEUE;
216 mtx_unlock(&ch->state_mtx);
218 /* reset the controller HW, the channel and device(s) */
221 /* reinit the children and delete any that fails */
222 if (!device_get_children(dev, &children, &nchildren)) {
223 mtx_lock(&Giant); /* newbus suckage it needs Giant */
224 for (i = 0; i < nchildren; i++) {
225 /* did any children go missing ? */
226 if (children[i] && device_is_attached(children[i]) &&
227 ATA_REINIT(children[i])) {
229 * if we had a running request and its device matches
230 * this child we need to inform the request that the
233 if (request && request->dev == children[i]) {
234 request->result = ENXIO;
235 device_printf(request->dev, "FAILURE - device detached\n");
237 /* if not timeout finish request here */
238 if (!(request->flags & ATA_R_TIMEOUT))
242 device_delete_child(dev, children[i]);
245 free(children, M_TEMP);
246 mtx_unlock(&Giant); /* newbus suckage dealt with, release Giant */
249 /* if we still have a good request put it on the queue again */
250 if (request && !(request->flags & ATA_R_TIMEOUT)) {
251 device_printf(request->dev,
252 "WARNING - %s requeued due to channel reset",
253 ata_cmd2str(request));
254 if (!(request->flags & (ATA_R_ATAPI | ATA_R_CONTROL)))
255 printf(" LBA=%ju", request->u.ata.lba);
257 request->flags |= ATA_R_REQUEUE;
258 ata_queue_request(request);
261 /* we're done release the channel for new work */
262 mtx_lock(&ch->state_mtx);
263 ch->state = ATA_IDLE;
264 mtx_unlock(&ch->state_mtx);
265 ATA_LOCKING(dev, ATA_LF_UNLOCK);
268 device_printf(dev, "reinit done ..\n");
270 /* kick off requests on the queue */
276 ata_suspend(device_t dev)
278 struct ata_channel *ch;
280 /* check for valid device */
281 if (!dev || !(ch = device_get_softc(dev)))
284 /* wait for the channel to be IDLE or detached before suspending */
286 mtx_lock(&ch->state_mtx);
287 if (ch->state == ATA_IDLE) {
288 ch->state = ATA_ACTIVE;
289 mtx_unlock(&ch->state_mtx);
292 mtx_unlock(&ch->state_mtx);
293 tsleep(ch, PRIBIO, "atasusp", hz/10);
295 ATA_LOCKING(dev, ATA_LF_UNLOCK);
300 ata_resume(device_t dev)
302 struct ata_channel *ch;
305 /* check for valid device */
306 if (!dev || !(ch = device_get_softc(dev)))
309 /* reinit the devices, we dont know what mode/state they are in */
310 error = ata_reinit(dev);
312 /* kick off requests on the queue */
318 ata_interrupt(void *data)
320 struct ata_channel *ch = (struct ata_channel *)data;
321 struct ata_request *request;
323 mtx_lock(&ch->state_mtx);
325 /* ignore interrupt if its not for us */
326 if (ch->hw.status && !ch->hw.status(ch->dev))
329 /* do we have a running request */
330 if (!(request = ch->running))
333 ATA_DEBUG_RQ(request, "interrupt");
335 /* safetycheck for the right state */
336 if (ch->state == ATA_IDLE) {
337 device_printf(request->dev, "interrupt on idle channel ignored\n");
342 * we have the HW locks, so end the transaction for this request
343 * if it finishes immediately otherwise wait for next interrupt
345 if (ch->hw.end_transaction(request) == ATA_OP_FINISHED) {
347 if (ch->state == ATA_ACTIVE)
348 ch->state = ATA_IDLE;
349 mtx_unlock(&ch->state_mtx);
350 ATA_LOCKING(ch->dev, ATA_LF_UNLOCK);
355 mtx_unlock(&ch->state_mtx);
360 * device related interfaces
363 ata_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
364 int32_t flag, struct thread *td)
366 device_t device, *children;
367 struct ata_ioc_devices *devices = (struct ata_ioc_devices *)data;
368 int *value = (int *)data;
369 int i, nchildren, error = ENOTTY;
372 case IOCATAGMAXCHANNEL:
373 /* In case we have channel 0..n this will return n+1. */
374 *value = devclass_get_maxunit(ata_devclass);
379 if (*value >= devclass_get_maxunit(ata_devclass) ||
380 !(device = devclass_get_device(ata_devclass, *value)))
382 error = ata_reinit(device);
386 if (*value >= devclass_get_maxunit(ata_devclass) ||
387 !(device = devclass_get_device(ata_devclass, *value)))
389 /* XXX SOS should enable channel HW on controller */
390 error = ata_attach(device);
394 if (*value >= devclass_get_maxunit(ata_devclass) ||
395 !(device = devclass_get_device(ata_devclass, *value)))
397 error = ata_detach(device);
398 /* XXX SOS should disable channel HW on controller */
402 if (devices->channel >= devclass_get_maxunit(ata_devclass) ||
403 !(device = devclass_get_device(ata_devclass, devices->channel)))
405 bzero(devices->name[0], 32);
406 bzero(&devices->params[0], sizeof(struct ata_params));
407 bzero(devices->name[1], 32);
408 bzero(&devices->params[1], sizeof(struct ata_params));
409 if (!device_get_children(device, &children, &nchildren)) {
410 for (i = 0; i < nchildren; i++) {
411 if (children[i] && device_is_attached(children[i])) {
412 struct ata_device *atadev = device_get_softc(children[i]);
414 if (atadev->unit == ATA_MASTER) { /* XXX SOS PM */
415 strncpy(devices->name[0],
416 device_get_nameunit(children[i]), 32);
417 bcopy(&atadev->param, &devices->params[0],
418 sizeof(struct ata_params));
420 if (atadev->unit == ATA_SLAVE) { /* XXX SOS PM */
421 strncpy(devices->name[1],
422 device_get_nameunit(children[i]), 32);
423 bcopy(&atadev->param, &devices->params[1],
424 sizeof(struct ata_params));
428 free(children, M_TEMP);
436 if (ata_raid_ioctl_func)
437 error = ata_raid_ioctl_func(cmd, data);
443 ata_device_ioctl(device_t dev, u_long cmd, caddr_t data)
445 struct ata_device *atadev = device_get_softc(dev);
446 struct ata_ioc_request *ioc_request = (struct ata_ioc_request *)data;
447 struct ata_params *params = (struct ata_params *)data;
448 int *mode = (int *)data;
449 struct ata_request *request;
455 if (!(buf = malloc(ioc_request->count, M_ATA, M_NOWAIT))) {
458 if (!(request = ata_alloc_request())) {
462 request->dev = atadev->dev;
463 if (ioc_request->flags & ATA_CMD_WRITE) {
464 error = copyin(ioc_request->data, buf, ioc_request->count);
467 ata_free_request(request);
471 if (ioc_request->flags & ATA_CMD_ATAPI) {
472 request->flags = ATA_R_ATAPI;
473 bcopy(ioc_request->u.atapi.ccb, request->u.atapi.ccb, 16);
476 request->u.ata.command = ioc_request->u.ata.command;
477 request->u.ata.feature = ioc_request->u.ata.feature;
478 request->u.ata.lba = ioc_request->u.ata.lba;
479 request->u.ata.count = ioc_request->u.ata.count;
481 request->timeout = ioc_request->timeout;
483 request->bytecount = ioc_request->count;
484 request->transfersize = request->bytecount;
485 if (ioc_request->flags & ATA_CMD_CONTROL)
486 request->flags |= ATA_R_CONTROL;
487 if (ioc_request->flags & ATA_CMD_READ)
488 request->flags |= ATA_R_READ;
489 if (ioc_request->flags & ATA_CMD_WRITE)
490 request->flags |= ATA_R_WRITE;
491 ata_queue_request(request);
492 if (request->flags & ATA_R_ATAPI) {
493 bcopy(&request->u.atapi.sense, &ioc_request->u.atapi.sense,
494 sizeof(struct atapi_sense));
497 ioc_request->u.ata.command = request->u.ata.command;
498 ioc_request->u.ata.feature = request->u.ata.feature;
499 ioc_request->u.ata.lba = request->u.ata.lba;
500 ioc_request->u.ata.count = request->u.ata.count;
502 ioc_request->error = request->result;
503 if (ioc_request->flags & ATA_CMD_READ)
504 error = copyout(buf, ioc_request->data, ioc_request->count);
508 ata_free_request(request);
512 ata_getparam(atadev, 0);
513 bcopy(&atadev->param, params, sizeof(struct ata_params));
517 atadev->mode = *mode;
518 ATA_SETMODE(device_get_parent(dev), dev);
522 *mode = atadev->mode;
524 case IOCATASSPINDOWN:
525 atadev->spindown = *mode;
527 case IOCATAGSPINDOWN:
528 *mode = atadev->spindown;
536 ata_boot_attach(void)
538 struct ata_channel *ch;
541 mtx_lock(&Giant); /* newbus suckage it needs Giant */
543 /* kick of probe and attach on all channels */
544 for (ctlr = 0; ctlr < devclass_get_maxunit(ata_devclass); ctlr++) {
545 if ((ch = devclass_get_softc(ata_devclass, ctlr))) {
546 ata_identify(ch->dev);
550 /* release the hook that got us here, we are only needed once during boot */
551 if (ata_delayed_attach) {
552 config_intrhook_disestablish(ata_delayed_attach);
553 free(ata_delayed_attach, M_TEMP);
554 ata_delayed_attach = NULL;
557 mtx_unlock(&Giant); /* newbus suckage dealt with, release Giant */
562 * misc support functions
565 ata_add_child(device_t parent, struct ata_device *atadev, int unit)
569 if ((child = device_add_child(parent, NULL, unit))) {
570 device_set_softc(child, atadev);
573 atadev->max_iosize = DEV_BSIZE;
574 atadev->mode = ATA_PIO_MAX;
580 ata_getparam(struct ata_device *atadev, int init)
582 struct ata_channel *ch = device_get_softc(device_get_parent(atadev->dev));
583 struct ata_request *request;
584 u_int8_t command = 0;
585 int error = ENOMEM, retries = 2;
587 if (ch->devices & (ATA_ATA_MASTER << atadev->unit))
588 command = ATA_ATA_IDENTIFY;
589 if (ch->devices & (ATA_ATAPI_MASTER << atadev->unit))
590 command = ATA_ATAPI_IDENTIFY;
594 while (retries-- > 0 && error) {
595 if (!(request = ata_alloc_request()))
597 request->dev = atadev->dev;
598 request->timeout = 1;
599 request->retries = 0;
600 request->u.ata.command = command;
601 request->flags = (ATA_R_READ|ATA_R_AT_HEAD|ATA_R_DIRECT|ATA_R_QUIET);
602 request->data = (void *)&atadev->param;
603 request->bytecount = sizeof(struct ata_params);
604 request->donecount = 0;
605 request->transfersize = DEV_BSIZE;
606 ata_queue_request(request);
607 error = request->result;
608 ata_free_request(request);
611 if (!error && (isprint(atadev->param.model[0]) ||
612 isprint(atadev->param.model[1]))) {
613 struct ata_params *atacap = &atadev->param;
617 for (ptr = (int16_t *)atacap;
618 ptr < (int16_t *)atacap + sizeof(struct ata_params)/2; ptr++) {
619 *ptr = le16toh(*ptr);
621 if (!(!strncmp(atacap->model, "FX", 2) ||
622 !strncmp(atacap->model, "NEC", 3) ||
623 !strncmp(atacap->model, "Pioneer", 7) ||
624 !strncmp(atacap->model, "SHARP", 5))) {
625 bswap(atacap->model, sizeof(atacap->model));
626 bswap(atacap->revision, sizeof(atacap->revision));
627 bswap(atacap->serial, sizeof(atacap->serial));
629 btrim(atacap->model, sizeof(atacap->model));
630 bpack(atacap->model, atacap->model, sizeof(atacap->model));
631 btrim(atacap->revision, sizeof(atacap->revision));
632 bpack(atacap->revision, atacap->revision, sizeof(atacap->revision));
633 btrim(atacap->serial, sizeof(atacap->serial));
634 bpack(atacap->serial, atacap->serial, sizeof(atacap->serial));
637 printf("ata%d-%s: pio=%s wdma=%s udma=%s cable=%s wire\n",
638 device_get_unit(ch->dev),
639 ata_unit2str(atadev),
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 (atadev->param.config != ATA_CFA_MAGIC1) &&
650 (atadev->param.config != ATA_CFA_MAGIC2)) {
652 (atadev->param.config & ATA_DRQ_MASK) != ATA_DRQ_INTR &&
653 ata_umode(&atadev->param) >= ATA_UDMA2)
654 atadev->mode = ATA_DMA_MAX;
658 (ata_umode(&atadev->param) > 0 ||
659 ata_wmode(&atadev->param) > 0))
660 atadev->mode = ATA_DMA_MAX;
672 ata_identify(device_t dev)
674 struct ata_channel *ch = device_get_softc(dev);
675 struct ata_device *devices[ATA_PM];
676 device_t childdevs[ATA_PM];
680 device_printf(dev, "identify ch->devices=%08x\n", ch->devices);
682 for (i = 0; i < ATA_PM; ++i) {
683 if (ch->devices & (((ATA_ATA_MASTER | ATA_ATAPI_MASTER) << i))) {
686 if (!(devices[i] = malloc(sizeof(struct ata_device),
687 M_ATA, M_NOWAIT | M_ZERO))) {
688 device_printf(dev, "out of memory\n");
691 devices[i]->unit = i;
693 if (ch->devices & ((ATA_ATA_MASTER << i)))
694 unit = (device_get_unit(dev) << 1) + i;
696 if (!(childdevs[i] = ata_add_child(dev, devices[i], unit))) {
697 free(devices[i], M_ATA);
701 if (ata_getparam(devices[i], 1)) {
702 device_delete_child(dev, childdevs[i]);
703 free(devices[i], M_ATA);
713 bus_generic_probe(dev);
714 bus_generic_attach(dev);
719 ata_default_registers(device_t dev)
721 struct ata_channel *ch = device_get_softc(dev);
723 /* fill in the defaults from whats setup already */
724 ch->r_io[ATA_ERROR].res = ch->r_io[ATA_FEATURE].res;
725 ch->r_io[ATA_ERROR].offset = ch->r_io[ATA_FEATURE].offset;
726 ch->r_io[ATA_IREASON].res = ch->r_io[ATA_COUNT].res;
727 ch->r_io[ATA_IREASON].offset = ch->r_io[ATA_COUNT].offset;
728 ch->r_io[ATA_STATUS].res = ch->r_io[ATA_COMMAND].res;
729 ch->r_io[ATA_STATUS].offset = ch->r_io[ATA_COMMAND].offset;
730 ch->r_io[ATA_ALTSTAT].res = ch->r_io[ATA_CONTROL].res;
731 ch->r_io[ATA_ALTSTAT].offset = ch->r_io[ATA_CONTROL].offset;
735 ata_modify_if_48bit(struct ata_request *request)
737 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
738 struct ata_device *atadev = device_get_softc(request->dev);
740 atadev->flags &= ~ATA_D_48BIT_ACTIVE;
742 if (((request->u.ata.lba + request->u.ata.count) >= ATA_MAX_28BIT_LBA ||
743 request->u.ata.count > 256) &&
744 atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) {
746 /* translate command into 48bit version */
747 switch (request->u.ata.command) {
749 request->u.ata.command = ATA_READ48;
752 request->u.ata.command = ATA_READ_MUL48;
755 if (ch->flags & ATA_NO_48BIT_DMA) {
756 if (request->transfersize > DEV_BSIZE)
757 request->u.ata.command = ATA_READ_MUL48;
759 request->u.ata.command = ATA_READ48;
760 request->flags &= ~ATA_R_DMA;
763 request->u.ata.command = ATA_READ_DMA48;
765 case ATA_READ_DMA_QUEUED:
766 if (ch->flags & ATA_NO_48BIT_DMA) {
767 if (request->transfersize > DEV_BSIZE)
768 request->u.ata.command = ATA_READ_MUL48;
770 request->u.ata.command = ATA_READ48;
771 request->flags &= ~ATA_R_DMA;
774 request->u.ata.command = ATA_READ_DMA_QUEUED48;
777 request->u.ata.command = ATA_WRITE48;
780 request->u.ata.command = ATA_WRITE_MUL48;
783 if (ch->flags & ATA_NO_48BIT_DMA) {
784 if (request->transfersize > DEV_BSIZE)
785 request->u.ata.command = ATA_WRITE_MUL48;
787 request->u.ata.command = ATA_WRITE48;
788 request->flags &= ~ATA_R_DMA;
791 request->u.ata.command = ATA_WRITE_DMA48;
793 case ATA_WRITE_DMA_QUEUED:
794 if (ch->flags & ATA_NO_48BIT_DMA) {
795 if (request->transfersize > DEV_BSIZE)
796 request->u.ata.command = ATA_WRITE_MUL48;
798 request->u.ata.command = ATA_WRITE48;
799 request->u.ata.command = ATA_WRITE48;
800 request->flags &= ~ATA_R_DMA;
803 request->u.ata.command = ATA_WRITE_DMA_QUEUED48;
806 request->u.ata.command = ATA_FLUSHCACHE48;
808 case ATA_SET_MAX_ADDRESS:
809 request->u.ata.command = ATA_SET_MAX_ADDRESS48;
814 atadev->flags |= ATA_D_48BIT_ACTIVE;
816 else if (atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) {
818 /* translate command into 48bit version */
819 switch (request->u.ata.command) {
821 request->u.ata.command = ATA_FLUSHCACHE48;
823 case ATA_READ_NATIVE_MAX_ADDRESS:
824 request->u.ata.command = ATA_READ_NATIVE_MAX_ADDRESS48;
826 case ATA_SET_MAX_ADDRESS:
827 request->u.ata.command = ATA_SET_MAX_ADDRESS48;
832 atadev->flags |= ATA_D_48BIT_ACTIVE;
837 ata_udelay(int interval)
839 /* for now just use DELAY, the timer/sleep subsytems are not there yet */
840 if (1 || interval < (1000000/hz) || ata_delayed_attach)
843 pause("ataslp", interval/(1000000/hz));
847 ata_unit2str(struct ata_device *atadev)
849 struct ata_channel *ch = device_get_softc(device_get_parent(atadev->dev));
852 if (ch->devices & ATA_PORTMULTIPLIER)
853 sprintf(str, "port%d", atadev->unit);
855 sprintf(str, "%s", atadev->unit == ATA_MASTER ? "master" : "slave");
860 ata_mode2str(int mode)
863 case -1: return "UNSUPPORTED";
864 case ATA_PIO0: return "PIO0";
865 case ATA_PIO1: return "PIO1";
866 case ATA_PIO2: return "PIO2";
867 case ATA_PIO3: return "PIO3";
868 case ATA_PIO4: return "PIO4";
869 case ATA_WDMA0: return "WDMA0";
870 case ATA_WDMA1: return "WDMA1";
871 case ATA_WDMA2: return "WDMA2";
872 case ATA_UDMA0: return "UDMA16";
873 case ATA_UDMA1: return "UDMA25";
874 case ATA_UDMA2: return "UDMA33";
875 case ATA_UDMA3: return "UDMA40";
876 case ATA_UDMA4: return "UDMA66";
877 case ATA_UDMA5: return "UDMA100";
878 case ATA_UDMA6: return "UDMA133";
879 case ATA_SA150: return "SATA150";
880 case ATA_SA300: return "SATA300";
881 case ATA_USB: return "USB";
882 case ATA_USB1: return "USB1";
883 case ATA_USB2: return "USB2";
885 if (mode & ATA_DMA_MASK)
893 ata_pmode(struct ata_params *ap)
895 if (ap->atavalid & ATA_FLAG_64_70) {
896 if (ap->apiomodes & 0x02)
898 if (ap->apiomodes & 0x01)
901 if (ap->mwdmamodes & 0x04)
903 if (ap->mwdmamodes & 0x02)
905 if (ap->mwdmamodes & 0x01)
907 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200)
909 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100)
911 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000)
917 ata_wmode(struct ata_params *ap)
919 if (ap->mwdmamodes & 0x04)
921 if (ap->mwdmamodes & 0x02)
923 if (ap->mwdmamodes & 0x01)
929 ata_umode(struct ata_params *ap)
931 if (ap->atavalid & ATA_FLAG_88) {
932 if (ap->udmamodes & 0x40)
934 if (ap->udmamodes & 0x20)
936 if (ap->udmamodes & 0x10)
938 if (ap->udmamodes & 0x08)
940 if (ap->udmamodes & 0x04)
942 if (ap->udmamodes & 0x02)
944 if (ap->udmamodes & 0x01)
951 ata_limit_mode(device_t dev, int mode, int maxmode)
953 struct ata_device *atadev = device_get_softc(dev);
955 if (maxmode && mode > maxmode)
958 if (mode >= ATA_UDMA0 && ata_umode(&atadev->param) > 0)
959 return min(mode, ata_umode(&atadev->param));
961 if (mode >= ATA_WDMA0 && ata_wmode(&atadev->param) > 0)
962 return min(mode, ata_wmode(&atadev->param));
964 if (mode > ata_pmode(&atadev->param))
965 return min(mode, ata_pmode(&atadev->param));
971 bswap(int8_t *buf, int len)
973 u_int16_t *ptr = (u_int16_t*)(buf + len);
975 while (--ptr >= (u_int16_t*)buf)
980 btrim(int8_t *buf, int len)
984 for (ptr = buf; ptr < buf+len; ++ptr)
985 if (!*ptr || *ptr == '_')
987 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
992 bpack(int8_t *src, int8_t *dst, int len)
996 for (i = j = blank = 0 ; i < len; i++) {
997 if (blank && src[i] == ' ') continue;
998 if (blank && src[i] != ' ') {
1003 if (src[i] == ' ') {
1019 ata_module_event_handler(module_t mod, int what, void *arg)
1021 static struct cdev *atacdev;
1025 /* register controlling device */
1026 atacdev = make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "ata");
1029 /* register boot attach to be run when interrupts are enabled */
1030 if (!(ata_delayed_attach = (struct intr_config_hook *)
1031 malloc(sizeof(struct intr_config_hook),
1032 M_TEMP, M_NOWAIT | M_ZERO))) {
1033 printf("ata: malloc of delayed attach hook failed\n");
1036 ata_delayed_attach->ich_func = (void*)ata_boot_attach;
1037 if (config_intrhook_establish(ata_delayed_attach) != 0) {
1038 printf("ata: config_intrhook_establish failed\n");
1039 free(ata_delayed_attach, M_TEMP);
1045 /* deregister controlling device */
1046 destroy_dev(atacdev);
1054 static moduledata_t ata_moduledata = { "ata", ata_module_event_handler, NULL };
1055 DECLARE_MODULE(ata, ata_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
1056 MODULE_VERSION(ata, 1);
1061 ata_request_zone = uma_zcreate("ata_request", sizeof(struct ata_request),
1062 NULL, NULL, NULL, NULL, 0, 0);
1063 ata_composite_zone = uma_zcreate("ata_composite",
1064 sizeof(struct ata_composite),
1065 NULL, NULL, NULL, NULL, 0, 0);
1067 SYSINIT(ata_register, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL);
1072 uma_zdestroy(ata_composite_zone);
1073 uma_zdestroy(ata_request_zone);
1075 SYSUNINIT(ata_unregister, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_uninit, NULL);