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,
62 static void ata_boot_attach(void);
63 static device_t ata_add_child(device_t, struct ata_device *, int);
64 static void ata_conn_event(void *, 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;
78 int ata_dma_check_80pin = 1;
81 static int ata_dma = 1;
82 static int atapi_dma = 1;
85 SYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters");
86 TUNABLE_INT("hw.ata.ata_dma", &ata_dma);
87 SYSCTL_INT(_hw_ata, OID_AUTO, ata_dma, CTLFLAG_RDTUN, &ata_dma, 0,
88 "ATA disk DMA mode control");
89 TUNABLE_INT("hw.ata.ata_dma_check_80pin", &ata_dma_check_80pin);
90 SYSCTL_INT(_hw_ata, OID_AUTO, ata_dma_check_80pin,
91 CTLFLAG_RDTUN, &ata_dma_check_80pin, 1,
92 "Check for 80pin cable before setting ATA DMA mode");
93 TUNABLE_INT("hw.ata.atapi_dma", &atapi_dma);
94 SYSCTL_INT(_hw_ata, OID_AUTO, atapi_dma, CTLFLAG_RDTUN, &atapi_dma, 0,
95 "ATAPI device DMA mode control");
96 TUNABLE_INT("hw.ata.wc", &ata_wc);
97 SYSCTL_INT(_hw_ata, OID_AUTO, wc, CTLFLAG_RDTUN, &ata_wc, 0,
98 "ATA disk write caching");
99 TUNABLE_INT("hw.ata.setmax", &ata_setmax);
100 SYSCTL_INT(_hw_ata, OID_AUTO, setmax, CTLFLAG_RDTUN, &ata_setmax, 0,
101 "ATA disk set max native address");
104 * newbus device interface related functions
107 ata_probe(device_t dev)
113 ata_attach(device_t dev)
115 struct ata_channel *ch = device_get_softc(dev);
118 /* check that we have a virgin channel to attach */
122 /* initialize the softc basics */
124 ch->state = ATA_IDLE;
125 bzero(&ch->state_mtx, sizeof(struct mtx));
126 mtx_init(&ch->state_mtx, "ATA state lock", NULL, MTX_DEF);
127 bzero(&ch->queue_mtx, sizeof(struct mtx));
128 mtx_init(&ch->queue_mtx, "ATA queue lock", NULL, MTX_DEF);
129 TAILQ_INIT(&ch->ata_queue);
130 TASK_INIT(&ch->conntask, 0, ata_conn_event, dev);
132 /* reset the controller HW, the channel and device(s) */
133 while (ATA_LOCKING(dev, ATA_LF_LOCK) != ch->unit)
136 ATA_LOCKING(dev, ATA_LF_UNLOCK);
138 /* allocate DMA resources if DMA HW present*/
142 /* setup interrupt delivery */
144 ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
145 RF_SHAREABLE | RF_ACTIVE);
147 device_printf(dev, "unable to allocate interrupt\n");
150 if ((error = bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS, NULL,
151 ata_interrupt, ch, &ch->ih))) {
152 device_printf(dev, "unable to setup interrupt\n");
156 /* probe and attach devices on this channel unless we are in early boot */
157 if (!ata_delayed_attach)
163 ata_detach(device_t dev)
165 struct ata_channel *ch = device_get_softc(dev);
169 /* check that we have a valid channel to detach */
173 /* grap the channel lock so no new requests gets launched */
174 mtx_lock(&ch->state_mtx);
175 ch->state |= ATA_STALL_QUEUE;
176 mtx_unlock(&ch->state_mtx);
178 /* detach & delete all children */
179 if (!device_get_children(dev, &children, &nchildren)) {
180 for (i = 0; i < nchildren; i++)
182 device_delete_child(dev, children[i]);
183 free(children, M_TEMP);
185 taskqueue_drain(taskqueue_thread, &ch->conntask);
187 /* release resources */
188 bus_teardown_intr(dev, ch->r_irq, ch->ih);
189 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
192 /* free DMA resources if DMA HW present*/
196 mtx_destroy(&ch->state_mtx);
197 mtx_destroy(&ch->queue_mtx);
202 ata_conn_event(void *context, int dummy)
204 device_t dev = (device_t)context;
212 ata_reinit(device_t dev)
214 struct ata_channel *ch = device_get_softc(dev);
215 struct ata_request *request;
219 /* check that we have a valid channel to reinit */
220 if (!ch || !ch->r_irq)
224 device_printf(dev, "reiniting channel ..\n");
226 /* poll for locking the channel */
227 while (ATA_LOCKING(dev, ATA_LF_LOCK) != ch->unit)
230 /* catch eventual request in ch->running */
231 mtx_lock(&ch->state_mtx);
232 if (ch->state & ATA_STALL_QUEUE) {
233 /* Recursive reinits and reinits during detach prohobited. */
234 mtx_unlock(&ch->state_mtx);
237 if ((request = ch->running))
238 callout_stop(&request->callout);
241 /* unconditionally grap the channel lock */
242 ch->state |= ATA_STALL_QUEUE;
243 mtx_unlock(&ch->state_mtx);
245 /* reset the controller HW, the channel and device(s) */
248 /* reinit the children and delete any that fails */
249 if (!device_get_children(dev, &children, &nchildren)) {
250 for (i = 0; i < nchildren; i++) {
251 /* did any children go missing ? */
252 if (children[i] && device_is_attached(children[i]) &&
253 ATA_REINIT(children[i])) {
255 * if we had a running request and its device matches
256 * this child we need to inform the request that the
259 if (request && request->dev == children[i]) {
260 request->result = ENXIO;
261 device_printf(request->dev, "FAILURE - device detached\n");
263 /* if not timeout finish request here */
264 if (!(request->flags & ATA_R_TIMEOUT))
268 device_delete_child(dev, children[i]);
271 free(children, M_TEMP);
274 /* if we still have a good request put it on the queue again */
275 if (request && !(request->flags & ATA_R_TIMEOUT)) {
276 device_printf(request->dev,
277 "WARNING - %s requeued due to channel reset",
278 ata_cmd2str(request));
279 if (!(request->flags & (ATA_R_ATAPI | ATA_R_CONTROL)))
280 printf(" LBA=%ju", request->u.ata.lba);
282 request->flags |= ATA_R_REQUEUE;
283 ata_queue_request(request);
286 /* we're done release the channel for new work */
287 mtx_lock(&ch->state_mtx);
288 ch->state = ATA_IDLE;
289 mtx_unlock(&ch->state_mtx);
290 ATA_LOCKING(dev, ATA_LF_UNLOCK);
292 /* Add new children. */
293 /* ata_identify(dev); */
296 device_printf(dev, "reinit done ..\n");
298 /* kick off requests on the queue */
304 ata_suspend(device_t dev)
306 struct ata_channel *ch;
308 /* check for valid device */
309 if (!dev || !(ch = device_get_softc(dev)))
312 /* wait for the channel to be IDLE or detached before suspending */
314 mtx_lock(&ch->state_mtx);
315 if (ch->state == ATA_IDLE) {
316 ch->state = ATA_ACTIVE;
317 mtx_unlock(&ch->state_mtx);
320 mtx_unlock(&ch->state_mtx);
321 tsleep(ch, PRIBIO, "atasusp", hz/10);
323 ATA_LOCKING(dev, ATA_LF_UNLOCK);
328 ata_resume(device_t dev)
332 /* check for valid device */
333 if (!dev || !device_get_softc(dev))
336 /* reinit the devices, we dont know what mode/state they are in */
337 error = ata_reinit(dev);
339 /* kick off requests on the queue */
345 ata_interrupt(void *data)
347 struct ata_channel *ch = (struct ata_channel *)data;
348 struct ata_request *request;
350 mtx_lock(&ch->state_mtx);
352 /* ignore interrupt if its not for us */
353 if (ch->hw.status && !ch->hw.status(ch->dev))
356 /* do we have a running request */
357 if (!(request = ch->running))
360 ATA_DEBUG_RQ(request, "interrupt");
362 /* safetycheck for the right state */
363 if (ch->state == ATA_IDLE) {
364 device_printf(request->dev, "interrupt on idle channel ignored\n");
369 * we have the HW locks, so end the transaction for this request
370 * if it finishes immediately otherwise wait for next interrupt
372 if (ch->hw.end_transaction(request) == ATA_OP_FINISHED) {
374 if (ch->state == ATA_ACTIVE)
375 ch->state = ATA_IDLE;
376 mtx_unlock(&ch->state_mtx);
377 ATA_LOCKING(ch->dev, ATA_LF_UNLOCK);
382 mtx_unlock(&ch->state_mtx);
386 * device related interfaces
389 ata_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
390 int32_t flag, struct thread *td)
392 device_t device, *children;
393 struct ata_ioc_devices *devices = (struct ata_ioc_devices *)data;
394 int *value = (int *)data;
395 int i, nchildren, error = ENOTTY;
399 case IOCATAGMAXCHANNEL:
400 /* In case we have channel 0..n this will return n+1. */
401 *value = devclass_get_maxunit(ata_devclass);
406 if (*value >= devclass_get_maxunit(ata_devclass) ||
407 !(device = devclass_get_device(ata_devclass, *value)) ||
408 !device_is_attached(device)) {
412 error = ata_reinit(device);
416 if (*value >= devclass_get_maxunit(ata_devclass) ||
417 !(device = devclass_get_device(ata_devclass, *value)) ||
418 !device_is_attached(device)) {
422 error = DEVICE_ATTACH(device);
426 if (*value >= devclass_get_maxunit(ata_devclass) ||
427 !(device = devclass_get_device(ata_devclass, *value)) ||
428 !device_is_attached(device)) {
432 error = DEVICE_DETACH(device);
436 if (devices->channel >= devclass_get_maxunit(ata_devclass) ||
437 !(device = devclass_get_device(ata_devclass, devices->channel)) ||
438 !device_is_attached(device)) {
442 bzero(devices->name[0], 32);
443 bzero(&devices->params[0], sizeof(struct ata_params));
444 bzero(devices->name[1], 32);
445 bzero(&devices->params[1], sizeof(struct ata_params));
446 if (!device_get_children(device, &children, &nchildren)) {
447 for (i = 0; i < nchildren; i++) {
448 if (children[i] && device_is_attached(children[i])) {
449 struct ata_device *atadev = device_get_softc(children[i]);
451 if (atadev->unit == ATA_MASTER) { /* XXX SOS PM */
452 strncpy(devices->name[0],
453 device_get_nameunit(children[i]), 32);
454 bcopy(&atadev->param, &devices->params[0],
455 sizeof(struct ata_params));
457 if (atadev->unit == ATA_SLAVE) { /* XXX SOS PM */
458 strncpy(devices->name[1],
459 device_get_nameunit(children[i]), 32);
460 bcopy(&atadev->param, &devices->params[1],
461 sizeof(struct ata_params));
465 free(children, M_TEMP);
473 if (ata_raid_ioctl_func)
474 error = ata_raid_ioctl_func(cmd, data);
481 ata_device_ioctl(device_t dev, u_long cmd, caddr_t data)
483 struct ata_device *atadev = device_get_softc(dev);
484 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
485 struct ata_ioc_request *ioc_request = (struct ata_ioc_request *)data;
486 struct ata_params *params = (struct ata_params *)data;
487 int *mode = (int *)data;
488 struct ata_request *request;
494 if (ioc_request->count >
495 (ch->dma.max_iosize ? ch->dma.max_iosize : DFLTPHYS)) {
498 if (!(buf = malloc(ioc_request->count, M_ATA, M_NOWAIT))) {
501 if (!(request = ata_alloc_request())) {
505 request->dev = atadev->dev;
506 if (ioc_request->flags & ATA_CMD_WRITE) {
507 error = copyin(ioc_request->data, buf, ioc_request->count);
510 ata_free_request(request);
514 if (ioc_request->flags & ATA_CMD_ATAPI) {
515 request->flags = ATA_R_ATAPI;
516 bcopy(ioc_request->u.atapi.ccb, request->u.atapi.ccb, 16);
519 request->u.ata.command = ioc_request->u.ata.command;
520 request->u.ata.feature = ioc_request->u.ata.feature;
521 request->u.ata.lba = ioc_request->u.ata.lba;
522 request->u.ata.count = ioc_request->u.ata.count;
524 request->timeout = ioc_request->timeout;
526 request->bytecount = ioc_request->count;
527 request->transfersize = request->bytecount;
528 if (ioc_request->flags & ATA_CMD_CONTROL)
529 request->flags |= ATA_R_CONTROL;
530 if (ioc_request->flags & ATA_CMD_READ)
531 request->flags |= ATA_R_READ;
532 if (ioc_request->flags & ATA_CMD_WRITE)
533 request->flags |= ATA_R_WRITE;
534 ata_queue_request(request);
535 if (request->flags & ATA_R_ATAPI) {
536 bcopy(&request->u.atapi.sense, &ioc_request->u.atapi.sense,
537 sizeof(struct atapi_sense));
540 ioc_request->u.ata.command = request->u.ata.command;
541 ioc_request->u.ata.feature = request->u.ata.feature;
542 ioc_request->u.ata.lba = request->u.ata.lba;
543 ioc_request->u.ata.count = request->u.ata.count;
545 ioc_request->error = request->result;
546 if (ioc_request->flags & ATA_CMD_READ)
547 error = copyout(buf, ioc_request->data, ioc_request->count);
551 ata_free_request(request);
555 ata_getparam(atadev, 0);
556 bcopy(&atadev->param, params, sizeof(struct ata_params));
560 atadev->mode = *mode;
561 ATA_SETMODE(device_get_parent(dev), dev);
565 *mode = atadev->mode;
567 case IOCATASSPINDOWN:
568 atadev->spindown = *mode;
570 case IOCATAGSPINDOWN:
571 *mode = atadev->spindown;
579 ata_boot_attach(void)
581 struct ata_channel *ch;
586 /* kick of probe and attach on all channels */
587 for (ctlr = 0; ctlr < devclass_get_maxunit(ata_devclass); ctlr++) {
588 if ((ch = devclass_get_softc(ata_devclass, ctlr))) {
589 ata_identify(ch->dev);
593 /* release the hook that got us here, we are only needed once during boot */
594 if (ata_delayed_attach) {
595 config_intrhook_disestablish(ata_delayed_attach);
596 free(ata_delayed_attach, M_TEMP);
597 ata_delayed_attach = NULL;
604 * misc support functions
607 ata_add_child(device_t parent, struct ata_device *atadev, int unit)
611 if ((child = device_add_child(parent, NULL, unit))) {
612 device_set_softc(child, atadev);
615 atadev->max_iosize = DEV_BSIZE;
616 atadev->mode = ATA_PIO_MAX;
622 ata_getparam(struct ata_device *atadev, int init)
624 struct ata_channel *ch = device_get_softc(device_get_parent(atadev->dev));
625 struct ata_request *request;
626 u_int8_t command = 0;
627 int error = ENOMEM, retries = 2;
629 if (ch->devices & (ATA_ATA_MASTER << atadev->unit))
630 command = ATA_ATA_IDENTIFY;
631 if (ch->devices & (ATA_ATAPI_MASTER << atadev->unit))
632 command = ATA_ATAPI_IDENTIFY;
636 while (retries-- > 0 && error) {
637 if (!(request = ata_alloc_request()))
639 request->dev = atadev->dev;
640 request->timeout = 1;
641 request->retries = 0;
642 request->u.ata.command = command;
643 request->flags = (ATA_R_READ|ATA_R_AT_HEAD|ATA_R_DIRECT);
645 request->flags |= ATA_R_QUIET;
646 request->data = (void *)&atadev->param;
647 request->bytecount = sizeof(struct ata_params);
648 request->donecount = 0;
649 request->transfersize = DEV_BSIZE;
650 ata_queue_request(request);
651 error = request->result;
652 ata_free_request(request);
655 if (!error && (isprint(atadev->param.model[0]) ||
656 isprint(atadev->param.model[1]))) {
657 struct ata_params *atacap = &atadev->param;
660 for (ptr = (int16_t *)atacap;
661 ptr < (int16_t *)atacap + sizeof(struct ata_params)/2; ptr++) {
662 *ptr = le16toh(*ptr);
664 if (!(!strncmp(atacap->model, "FX", 2) ||
665 !strncmp(atacap->model, "NEC", 3) ||
666 !strncmp(atacap->model, "Pioneer", 7) ||
667 !strncmp(atacap->model, "SHARP", 5))) {
668 bswap(atacap->model, sizeof(atacap->model));
669 bswap(atacap->revision, sizeof(atacap->revision));
670 bswap(atacap->serial, sizeof(atacap->serial));
672 btrim(atacap->model, sizeof(atacap->model));
673 bpack(atacap->model, atacap->model, sizeof(atacap->model));
674 btrim(atacap->revision, sizeof(atacap->revision));
675 bpack(atacap->revision, atacap->revision, sizeof(atacap->revision));
676 btrim(atacap->serial, sizeof(atacap->serial));
677 bpack(atacap->serial, atacap->serial, sizeof(atacap->serial));
680 printf("ata%d-%s: pio=%s wdma=%s udma=%s cable=%s wire\n",
681 device_get_unit(ch->dev),
682 ata_unit2str(atadev),
683 ata_mode2str(ata_pmode(atacap)),
684 ata_mode2str(ata_wmode(atacap)),
685 ata_mode2str(ata_umode(atacap)),
686 (atacap->hwres & ATA_CABLE_ID) ? "80":"40");
691 sprintf(buffer, "%.40s/%.8s", atacap->model, atacap->revision);
692 device_set_desc_copy(atadev->dev, buffer);
693 if ((atadev->param.config & ATA_PROTO_ATAPI) &&
694 (atadev->param.config != ATA_CFA_MAGIC1) &&
695 (atadev->param.config != ATA_CFA_MAGIC2)) {
697 (atadev->param.config & ATA_DRQ_MASK) != ATA_DRQ_INTR &&
698 ata_umode(&atadev->param) >= ATA_UDMA2)
699 atadev->mode = ATA_DMA_MAX;
703 (ata_umode(&atadev->param) > 0 ||
704 ata_wmode(&atadev->param) > 0))
705 atadev->mode = ATA_DMA_MAX;
717 ata_identify(device_t dev)
719 struct ata_channel *ch = device_get_softc(dev);
720 struct ata_device *atadev;
722 device_t child, master = NULL;
723 int nchildren, i, n = ch->devices;
726 device_printf(dev, "Identifying devices: %08x\n", ch->devices);
728 /* Skip existing devices. */
729 if (!device_get_children(dev, &children, &nchildren)) {
730 for (i = 0; i < nchildren; i++) {
731 if (children[i] && (atadev = device_get_softc(children[i])))
732 n &= ~((ATA_ATA_MASTER | ATA_ATAPI_MASTER) << atadev->unit);
734 free(children, M_TEMP);
736 /* Create new devices. */
738 device_printf(dev, "New devices: %08x\n", n);
741 for (i = 0; i < ATA_PM; ++i) {
742 if (n & (((ATA_ATA_MASTER | ATA_ATAPI_MASTER) << i))) {
745 if (!(atadev = malloc(sizeof(struct ata_device),
746 M_ATA, M_NOWAIT | M_ZERO))) {
747 device_printf(dev, "out of memory\n");
752 if (n & (ATA_ATA_MASTER << i))
753 unit = (device_get_unit(dev) << 1) + i;
755 if ((child = ata_add_child(dev, atadev, unit))) {
757 * PATA slave should be identified first, to allow
758 * device cable detection on master to work properly.
760 if (i == 0 && (n & ATA_PORTMULTIPLIER) == 0 &&
761 (n & ((ATA_ATA_MASTER | ATA_ATAPI_MASTER) << 1)) != 0) {
765 if (ata_getparam(atadev, 1)) {
766 device_delete_child(dev, child);
775 atadev = device_get_softc(master);
776 if (ata_getparam(atadev, 1)) {
777 device_delete_child(dev, master);
781 bus_generic_probe(dev);
782 bus_generic_attach(dev);
787 ata_default_registers(device_t dev)
789 struct ata_channel *ch = device_get_softc(dev);
791 /* fill in the defaults from whats setup already */
792 ch->r_io[ATA_ERROR].res = ch->r_io[ATA_FEATURE].res;
793 ch->r_io[ATA_ERROR].offset = ch->r_io[ATA_FEATURE].offset;
794 ch->r_io[ATA_IREASON].res = ch->r_io[ATA_COUNT].res;
795 ch->r_io[ATA_IREASON].offset = ch->r_io[ATA_COUNT].offset;
796 ch->r_io[ATA_STATUS].res = ch->r_io[ATA_COMMAND].res;
797 ch->r_io[ATA_STATUS].offset = ch->r_io[ATA_COMMAND].offset;
798 ch->r_io[ATA_ALTSTAT].res = ch->r_io[ATA_CONTROL].res;
799 ch->r_io[ATA_ALTSTAT].offset = ch->r_io[ATA_CONTROL].offset;
803 ata_modify_if_48bit(struct ata_request *request)
805 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
806 struct ata_device *atadev = device_get_softc(request->dev);
808 atadev->flags &= ~ATA_D_48BIT_ACTIVE;
810 if (((request->u.ata.lba + request->u.ata.count) >= ATA_MAX_28BIT_LBA ||
811 request->u.ata.count > 256) &&
812 atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) {
814 /* translate command into 48bit version */
815 switch (request->u.ata.command) {
817 request->u.ata.command = ATA_READ48;
820 request->u.ata.command = ATA_READ_MUL48;
823 if (ch->flags & ATA_NO_48BIT_DMA) {
824 if (request->transfersize > DEV_BSIZE)
825 request->u.ata.command = ATA_READ_MUL48;
827 request->u.ata.command = ATA_READ48;
828 request->flags &= ~ATA_R_DMA;
831 request->u.ata.command = ATA_READ_DMA48;
833 case ATA_READ_DMA_QUEUED:
834 if (ch->flags & ATA_NO_48BIT_DMA) {
835 if (request->transfersize > DEV_BSIZE)
836 request->u.ata.command = ATA_READ_MUL48;
838 request->u.ata.command = ATA_READ48;
839 request->flags &= ~ATA_R_DMA;
842 request->u.ata.command = ATA_READ_DMA_QUEUED48;
845 request->u.ata.command = ATA_WRITE48;
848 request->u.ata.command = ATA_WRITE_MUL48;
851 if (ch->flags & ATA_NO_48BIT_DMA) {
852 if (request->transfersize > DEV_BSIZE)
853 request->u.ata.command = ATA_WRITE_MUL48;
855 request->u.ata.command = ATA_WRITE48;
856 request->flags &= ~ATA_R_DMA;
859 request->u.ata.command = ATA_WRITE_DMA48;
861 case ATA_WRITE_DMA_QUEUED:
862 if (ch->flags & ATA_NO_48BIT_DMA) {
863 if (request->transfersize > DEV_BSIZE)
864 request->u.ata.command = ATA_WRITE_MUL48;
866 request->u.ata.command = ATA_WRITE48;
867 request->u.ata.command = ATA_WRITE48;
868 request->flags &= ~ATA_R_DMA;
871 request->u.ata.command = ATA_WRITE_DMA_QUEUED48;
874 request->u.ata.command = ATA_FLUSHCACHE48;
876 case ATA_SET_MAX_ADDRESS:
877 request->u.ata.command = ATA_SET_MAX_ADDRESS48;
882 atadev->flags |= ATA_D_48BIT_ACTIVE;
884 else if (atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) {
886 /* translate command into 48bit version */
887 switch (request->u.ata.command) {
889 request->u.ata.command = ATA_FLUSHCACHE48;
891 case ATA_READ_NATIVE_MAX_ADDRESS:
892 request->u.ata.command = ATA_READ_NATIVE_MAX_ADDRESS48;
894 case ATA_SET_MAX_ADDRESS:
895 request->u.ata.command = ATA_SET_MAX_ADDRESS48;
900 atadev->flags |= ATA_D_48BIT_ACTIVE;
905 ata_udelay(int interval)
907 /* for now just use DELAY, the timer/sleep subsytems are not there yet */
908 if (1 || interval < (1000000/hz) || ata_delayed_attach)
911 pause("ataslp", interval/(1000000/hz));
915 ata_unit2str(struct ata_device *atadev)
917 struct ata_channel *ch = device_get_softc(device_get_parent(atadev->dev));
920 if (ch->devices & ATA_PORTMULTIPLIER)
921 sprintf(str, "port%d", atadev->unit);
923 sprintf(str, "%s", atadev->unit == ATA_MASTER ? "master" : "slave");
928 ata_mode2str(int mode)
931 case -1: return "UNSUPPORTED";
932 case ATA_PIO0: return "PIO0";
933 case ATA_PIO1: return "PIO1";
934 case ATA_PIO2: return "PIO2";
935 case ATA_PIO3: return "PIO3";
936 case ATA_PIO4: return "PIO4";
937 case ATA_WDMA0: return "WDMA0";
938 case ATA_WDMA1: return "WDMA1";
939 case ATA_WDMA2: return "WDMA2";
940 case ATA_UDMA0: return "UDMA16";
941 case ATA_UDMA1: return "UDMA25";
942 case ATA_UDMA2: return "UDMA33";
943 case ATA_UDMA3: return "UDMA40";
944 case ATA_UDMA4: return "UDMA66";
945 case ATA_UDMA5: return "UDMA100";
946 case ATA_UDMA6: return "UDMA133";
947 case ATA_SA150: return "SATA150";
948 case ATA_SA300: return "SATA300";
949 case ATA_USB: return "USB";
950 case ATA_USB1: return "USB1";
951 case ATA_USB2: return "USB2";
953 if (mode & ATA_DMA_MASK)
961 ata_atapi(device_t dev)
963 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
964 struct ata_device *atadev = device_get_softc(dev);
966 return (ch->devices & (ATA_ATAPI_MASTER << atadev->unit));
970 ata_pmode(struct ata_params *ap)
972 if (ap->atavalid & ATA_FLAG_64_70) {
973 if (ap->apiomodes & 0x02)
975 if (ap->apiomodes & 0x01)
978 if (ap->mwdmamodes & 0x04)
980 if (ap->mwdmamodes & 0x02)
982 if (ap->mwdmamodes & 0x01)
984 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200)
986 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100)
988 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000)
994 ata_wmode(struct ata_params *ap)
996 if (ap->mwdmamodes & 0x04)
998 if (ap->mwdmamodes & 0x02)
1000 if (ap->mwdmamodes & 0x01)
1006 ata_umode(struct ata_params *ap)
1008 if (ap->atavalid & ATA_FLAG_88) {
1009 if (ap->udmamodes & 0x40)
1011 if (ap->udmamodes & 0x20)
1013 if (ap->udmamodes & 0x10)
1015 if (ap->udmamodes & 0x08)
1017 if (ap->udmamodes & 0x04)
1019 if (ap->udmamodes & 0x02)
1021 if (ap->udmamodes & 0x01)
1028 ata_limit_mode(device_t dev, int mode, int maxmode)
1030 struct ata_device *atadev = device_get_softc(dev);
1032 if (maxmode && mode > maxmode)
1035 if (mode >= ATA_UDMA0 && ata_umode(&atadev->param) > 0)
1036 return min(mode, ata_umode(&atadev->param));
1038 if (mode >= ATA_WDMA0 && ata_wmode(&atadev->param) > 0)
1039 return min(mode, ata_wmode(&atadev->param));
1041 if (mode > ata_pmode(&atadev->param))
1042 return min(mode, ata_pmode(&atadev->param));
1048 bswap(int8_t *buf, int len)
1050 u_int16_t *ptr = (u_int16_t*)(buf + len);
1052 while (--ptr >= (u_int16_t*)buf)
1057 btrim(int8_t *buf, int len)
1061 for (ptr = buf; ptr < buf+len; ++ptr)
1062 if (!*ptr || *ptr == '_')
1064 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
1069 bpack(int8_t *src, int8_t *dst, int len)
1073 for (i = j = blank = 0 ; i < len; i++) {
1074 if (blank && src[i] == ' ') continue;
1075 if (blank && src[i] != ' ') {
1080 if (src[i] == ' ') {
1096 ata_module_event_handler(module_t mod, int what, void *arg)
1098 static struct cdev *atacdev;
1102 /* register controlling device */
1103 atacdev = make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "ata");
1106 /* register boot attach to be run when interrupts are enabled */
1107 if (!(ata_delayed_attach = (struct intr_config_hook *)
1108 malloc(sizeof(struct intr_config_hook),
1109 M_TEMP, M_NOWAIT | M_ZERO))) {
1110 printf("ata: malloc of delayed attach hook failed\n");
1113 ata_delayed_attach->ich_func = (void*)ata_boot_attach;
1114 if (config_intrhook_establish(ata_delayed_attach) != 0) {
1115 printf("ata: config_intrhook_establish failed\n");
1116 free(ata_delayed_attach, M_TEMP);
1122 /* deregister controlling device */
1123 destroy_dev(atacdev);
1131 static moduledata_t ata_moduledata = { "ata", ata_module_event_handler, NULL };
1132 DECLARE_MODULE(ata, ata_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
1133 MODULE_VERSION(ata, 1);
1138 ata_request_zone = uma_zcreate("ata_request", sizeof(struct ata_request),
1139 NULL, NULL, NULL, NULL, 0, 0);
1140 ata_composite_zone = uma_zcreate("ata_composite",
1141 sizeof(struct ata_composite),
1142 NULL, NULL, NULL, NULL, 0, 0);
1144 SYSINIT(ata_register, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL);
1149 uma_zdestroy(ata_composite_zone);
1150 uma_zdestroy(ata_request_zone);
1152 SYSUNINIT(ata_unregister, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_uninit, NULL);