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 ata_conn_event(void *, int);
66 static void bswap(int8_t *, int);
67 static void btrim(int8_t *, int);
68 static void bpack(int8_t *, int8_t *, int);
71 MALLOC_DEFINE(M_ATA, "ata_generic", "ATA driver generic layer");
72 int (*ata_raid_ioctl_func)(u_long cmd, caddr_t data) = NULL;
73 struct intr_config_hook *ata_delayed_attach = NULL;
74 devclass_t ata_devclass;
75 uma_zone_t ata_request_zone;
76 uma_zone_t ata_composite_zone;
79 int ata_dma_check_80pin = 1;
82 static int ata_dma = 1;
83 static int atapi_dma = 1;
86 SYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters");
87 TUNABLE_INT("hw.ata.ata_dma", &ata_dma);
88 SYSCTL_INT(_hw_ata, OID_AUTO, ata_dma, CTLFLAG_RDTUN, &ata_dma, 0,
89 "ATA disk DMA mode control");
90 TUNABLE_INT("hw.ata.ata_dma_check_80pin", &ata_dma_check_80pin);
91 SYSCTL_INT(_hw_ata, OID_AUTO, ata_dma_check_80pin,
92 CTLFLAG_RDTUN, &ata_dma_check_80pin, 1,
93 "Check for 80pin cable before setting ATA DMA mode");
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");
100 TUNABLE_INT("hw.ata.setmax", &ata_setmax);
101 SYSCTL_INT(_hw_ata, OID_AUTO, setmax, CTLFLAG_RDTUN, &ata_setmax, 0,
102 "ATA disk set max native address");
105 * newbus device interface related functions
108 ata_probe(device_t dev)
114 ata_attach(device_t dev)
116 struct ata_channel *ch = device_get_softc(dev);
119 /* check that we have a virgin channel to attach */
123 /* initialize the softc basics */
125 ch->state = ATA_IDLE;
126 bzero(&ch->state_mtx, sizeof(struct mtx));
127 mtx_init(&ch->state_mtx, "ATA state lock", NULL, MTX_DEF);
128 bzero(&ch->queue_mtx, sizeof(struct mtx));
129 mtx_init(&ch->queue_mtx, "ATA queue lock", NULL, MTX_DEF);
130 TAILQ_INIT(&ch->ata_queue);
131 TASK_INIT(&ch->conntask, 0, ata_conn_event, dev);
133 /* reset the controller HW, the channel and device(s) */
134 while (ATA_LOCKING(dev, ATA_LF_LOCK) != ch->unit)
137 ATA_LOCKING(dev, ATA_LF_UNLOCK);
139 /* allocate DMA resources if DMA HW present*/
143 /* setup interrupt delivery */
145 ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
146 RF_SHAREABLE | RF_ACTIVE);
148 device_printf(dev, "unable to allocate interrupt\n");
151 if ((error = bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS, NULL,
152 ata_interrupt, ch, &ch->ih))) {
153 device_printf(dev, "unable to setup interrupt\n");
157 /* probe and attach devices on this channel unless we are in early boot */
158 if (!ata_delayed_attach)
164 ata_detach(device_t dev)
166 struct ata_channel *ch = device_get_softc(dev);
170 /* check that we have a valid channel to detach */
174 /* grap the channel lock so no new requests gets launched */
175 mtx_lock(&ch->state_mtx);
176 ch->state |= ATA_STALL_QUEUE;
177 mtx_unlock(&ch->state_mtx);
179 /* detach & delete all children */
180 if (!device_get_children(dev, &children, &nchildren)) {
181 for (i = 0; i < nchildren; i++)
183 device_delete_child(dev, children[i]);
184 free(children, M_TEMP);
186 taskqueue_drain(taskqueue_thread, &ch->conntask);
188 /* release resources */
189 bus_teardown_intr(dev, ch->r_irq, ch->ih);
190 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
193 /* free DMA resources if DMA HW present*/
197 mtx_destroy(&ch->state_mtx);
198 mtx_destroy(&ch->queue_mtx);
203 ata_conn_event(void *context, int dummy)
205 device_t dev = (device_t)context;
211 ata_reinit(device_t dev)
213 struct ata_channel *ch = device_get_softc(dev);
214 struct ata_request *request;
218 /* check that we have a valid channel to reinit */
219 if (!ch || !ch->r_irq)
223 device_printf(dev, "reiniting channel ..\n");
225 /* poll for locking the channel */
226 while (ATA_LOCKING(dev, ATA_LF_LOCK) != ch->unit)
229 /* catch eventual request in ch->running */
230 mtx_lock(&ch->state_mtx);
231 if (ch->state & ATA_STALL_QUEUE) {
232 /* Recursive reinits and reinits during detach prohobited. */
233 mtx_unlock(&ch->state_mtx);
236 if ((request = ch->running))
237 callout_stop(&request->callout);
240 /* unconditionally grap the channel lock */
241 ch->state |= ATA_STALL_QUEUE;
242 mtx_unlock(&ch->state_mtx);
244 /* reset the controller HW, the channel and device(s) */
247 /* reinit the children and delete any that fails */
248 if (!device_get_children(dev, &children, &nchildren)) {
249 mtx_lock(&Giant); /* newbus suckage it needs Giant */
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);
272 mtx_unlock(&Giant); /* newbus suckage dealt with, release Giant */
275 /* if we still have a good request put it on the queue again */
276 if (request && !(request->flags & ATA_R_TIMEOUT)) {
277 device_printf(request->dev,
278 "WARNING - %s requeued due to channel reset",
279 ata_cmd2str(request));
280 if (!(request->flags & (ATA_R_ATAPI | ATA_R_CONTROL)))
281 printf(" LBA=%ju", request->u.ata.lba);
283 request->flags |= ATA_R_REQUEUE;
284 ata_queue_request(request);
287 /* we're done release the channel for new work */
288 mtx_lock(&ch->state_mtx);
289 ch->state = ATA_IDLE;
290 mtx_unlock(&ch->state_mtx);
291 ATA_LOCKING(dev, ATA_LF_UNLOCK);
293 /* Add new children. */
294 /* ata_identify(dev); */
297 device_printf(dev, "reinit done ..\n");
299 /* kick off requests on the queue */
305 ata_suspend(device_t dev)
307 struct ata_channel *ch;
309 /* check for valid device */
310 if (!dev || !(ch = device_get_softc(dev)))
313 /* wait for the channel to be IDLE or detached before suspending */
315 mtx_lock(&ch->state_mtx);
316 if (ch->state == ATA_IDLE) {
317 ch->state = ATA_ACTIVE;
318 mtx_unlock(&ch->state_mtx);
321 mtx_unlock(&ch->state_mtx);
322 tsleep(ch, PRIBIO, "atasusp", hz/10);
324 ATA_LOCKING(dev, ATA_LF_UNLOCK);
329 ata_resume(device_t dev)
333 /* check for valid device */
334 if (!dev || !device_get_softc(dev))
337 /* reinit the devices, we dont know what mode/state they are in */
338 error = ata_reinit(dev);
340 /* kick off requests on the queue */
346 ata_interrupt(void *data)
348 struct ata_channel *ch = (struct ata_channel *)data;
349 struct ata_request *request;
351 mtx_lock(&ch->state_mtx);
353 /* ignore interrupt if its not for us */
354 if (ch->hw.status && !ch->hw.status(ch->dev))
357 /* do we have a running request */
358 if (!(request = ch->running))
361 ATA_DEBUG_RQ(request, "interrupt");
363 /* safetycheck for the right state */
364 if (ch->state == ATA_IDLE) {
365 device_printf(request->dev, "interrupt on idle channel ignored\n");
370 * we have the HW locks, so end the transaction for this request
371 * if it finishes immediately otherwise wait for next interrupt
373 if (ch->hw.end_transaction(request) == ATA_OP_FINISHED) {
375 if (ch->state == ATA_ACTIVE)
376 ch->state = ATA_IDLE;
377 mtx_unlock(&ch->state_mtx);
378 ATA_LOCKING(ch->dev, ATA_LF_UNLOCK);
383 mtx_unlock(&ch->state_mtx);
387 * device related interfaces
390 ata_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
391 int32_t flag, struct thread *td)
393 device_t device, *children;
394 struct ata_ioc_devices *devices = (struct ata_ioc_devices *)data;
395 int *value = (int *)data;
396 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))
410 error = ata_reinit(device);
414 if (*value >= devclass_get_maxunit(ata_devclass) ||
415 !(device = devclass_get_device(ata_devclass, *value)) ||
416 !device_is_attached(device))
418 error = DEVICE_ATTACH(device);
422 if (*value >= devclass_get_maxunit(ata_devclass) ||
423 !(device = devclass_get_device(ata_devclass, *value)) ||
424 !device_is_attached(device))
426 error = DEVICE_DETACH(device);
430 if (devices->channel >= devclass_get_maxunit(ata_devclass) ||
431 !(device = devclass_get_device(ata_devclass, devices->channel)) ||
432 !device_is_attached(device))
434 bzero(devices->name[0], 32);
435 bzero(&devices->params[0], sizeof(struct ata_params));
436 bzero(devices->name[1], 32);
437 bzero(&devices->params[1], sizeof(struct ata_params));
438 if (!device_get_children(device, &children, &nchildren)) {
439 for (i = 0; i < nchildren; i++) {
440 if (children[i] && device_is_attached(children[i])) {
441 struct ata_device *atadev = device_get_softc(children[i]);
443 if (atadev->unit == ATA_MASTER) { /* XXX SOS PM */
444 strncpy(devices->name[0],
445 device_get_nameunit(children[i]), 32);
446 bcopy(&atadev->param, &devices->params[0],
447 sizeof(struct ata_params));
449 if (atadev->unit == ATA_SLAVE) { /* XXX SOS PM */
450 strncpy(devices->name[1],
451 device_get_nameunit(children[i]), 32);
452 bcopy(&atadev->param, &devices->params[1],
453 sizeof(struct ata_params));
457 free(children, M_TEMP);
465 if (ata_raid_ioctl_func)
466 error = ata_raid_ioctl_func(cmd, data);
472 ata_device_ioctl(device_t dev, u_long cmd, caddr_t data)
474 struct ata_device *atadev = device_get_softc(dev);
475 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
476 struct ata_ioc_request *ioc_request = (struct ata_ioc_request *)data;
477 struct ata_params *params = (struct ata_params *)data;
478 int *mode = (int *)data;
479 struct ata_request *request;
485 if (ioc_request->count >
486 (ch->dma.max_iosize ? ch->dma.max_iosize : DFLTPHYS)) {
489 if (!(buf = malloc(ioc_request->count, M_ATA, M_NOWAIT))) {
492 if (!(request = ata_alloc_request())) {
496 request->dev = atadev->dev;
497 if (ioc_request->flags & ATA_CMD_WRITE) {
498 error = copyin(ioc_request->data, buf, ioc_request->count);
501 ata_free_request(request);
505 if (ioc_request->flags & ATA_CMD_ATAPI) {
506 request->flags = ATA_R_ATAPI;
507 bcopy(ioc_request->u.atapi.ccb, request->u.atapi.ccb, 16);
510 request->u.ata.command = ioc_request->u.ata.command;
511 request->u.ata.feature = ioc_request->u.ata.feature;
512 request->u.ata.lba = ioc_request->u.ata.lba;
513 request->u.ata.count = ioc_request->u.ata.count;
515 request->timeout = ioc_request->timeout;
517 request->bytecount = ioc_request->count;
518 request->transfersize = request->bytecount;
519 if (ioc_request->flags & ATA_CMD_CONTROL)
520 request->flags |= ATA_R_CONTROL;
521 if (ioc_request->flags & ATA_CMD_READ)
522 request->flags |= ATA_R_READ;
523 if (ioc_request->flags & ATA_CMD_WRITE)
524 request->flags |= ATA_R_WRITE;
525 ata_queue_request(request);
526 if (request->flags & ATA_R_ATAPI) {
527 bcopy(&request->u.atapi.sense, &ioc_request->u.atapi.sense,
528 sizeof(struct atapi_sense));
531 ioc_request->u.ata.command = request->u.ata.command;
532 ioc_request->u.ata.feature = request->u.ata.feature;
533 ioc_request->u.ata.lba = request->u.ata.lba;
534 ioc_request->u.ata.count = request->u.ata.count;
536 ioc_request->error = request->result;
537 if (ioc_request->flags & ATA_CMD_READ)
538 error = copyout(buf, ioc_request->data, ioc_request->count);
542 ata_free_request(request);
546 ata_getparam(atadev, 0);
547 bcopy(&atadev->param, params, sizeof(struct ata_params));
551 atadev->mode = *mode;
552 ATA_SETMODE(device_get_parent(dev), dev);
556 *mode = atadev->mode;
558 case IOCATASSPINDOWN:
559 atadev->spindown = *mode;
561 case IOCATAGSPINDOWN:
562 *mode = atadev->spindown;
570 ata_boot_attach(void)
572 struct ata_channel *ch;
575 mtx_lock(&Giant); /* newbus suckage it needs Giant */
577 /* kick of probe and attach on all channels */
578 for (ctlr = 0; ctlr < devclass_get_maxunit(ata_devclass); ctlr++) {
579 if ((ch = devclass_get_softc(ata_devclass, ctlr))) {
580 ata_identify(ch->dev);
584 /* release the hook that got us here, we are only needed once during boot */
585 if (ata_delayed_attach) {
586 config_intrhook_disestablish(ata_delayed_attach);
587 free(ata_delayed_attach, M_TEMP);
588 ata_delayed_attach = NULL;
591 mtx_unlock(&Giant); /* newbus suckage dealt with, release Giant */
596 * misc support functions
599 ata_add_child(device_t parent, struct ata_device *atadev, int unit)
603 if ((child = device_add_child(parent, NULL, unit))) {
604 device_set_softc(child, atadev);
607 atadev->max_iosize = DEV_BSIZE;
608 atadev->mode = ATA_PIO_MAX;
614 ata_getparam(struct ata_device *atadev, int init)
616 struct ata_channel *ch = device_get_softc(device_get_parent(atadev->dev));
617 struct ata_request *request;
618 u_int8_t command = 0;
619 int error = ENOMEM, retries = 2;
621 if (ch->devices & (ATA_ATA_MASTER << atadev->unit))
622 command = ATA_ATA_IDENTIFY;
623 if (ch->devices & (ATA_ATAPI_MASTER << atadev->unit))
624 command = ATA_ATAPI_IDENTIFY;
628 while (retries-- > 0 && error) {
629 if (!(request = ata_alloc_request()))
631 request->dev = atadev->dev;
632 request->timeout = 1;
633 request->retries = 0;
634 request->u.ata.command = command;
635 request->flags = (ATA_R_READ|ATA_R_AT_HEAD|ATA_R_DIRECT);
637 request->flags |= ATA_R_QUIET;
638 request->data = (void *)&atadev->param;
639 request->bytecount = sizeof(struct ata_params);
640 request->donecount = 0;
641 request->transfersize = DEV_BSIZE;
642 ata_queue_request(request);
643 error = request->result;
644 ata_free_request(request);
647 if (!error && (isprint(atadev->param.model[0]) ||
648 isprint(atadev->param.model[1]))) {
649 struct ata_params *atacap = &atadev->param;
652 for (ptr = (int16_t *)atacap;
653 ptr < (int16_t *)atacap + sizeof(struct ata_params)/2; ptr++) {
654 *ptr = le16toh(*ptr);
656 if (!(!strncmp(atacap->model, "FX", 2) ||
657 !strncmp(atacap->model, "NEC", 3) ||
658 !strncmp(atacap->model, "Pioneer", 7) ||
659 !strncmp(atacap->model, "SHARP", 5))) {
660 bswap(atacap->model, sizeof(atacap->model));
661 bswap(atacap->revision, sizeof(atacap->revision));
662 bswap(atacap->serial, sizeof(atacap->serial));
664 btrim(atacap->model, sizeof(atacap->model));
665 bpack(atacap->model, atacap->model, sizeof(atacap->model));
666 btrim(atacap->revision, sizeof(atacap->revision));
667 bpack(atacap->revision, atacap->revision, sizeof(atacap->revision));
668 btrim(atacap->serial, sizeof(atacap->serial));
669 bpack(atacap->serial, atacap->serial, sizeof(atacap->serial));
672 printf("ata%d-%s: pio=%s wdma=%s udma=%s cable=%s wire\n",
673 device_get_unit(ch->dev),
674 ata_unit2str(atadev),
675 ata_mode2str(ata_pmode(atacap)),
676 ata_mode2str(ata_wmode(atacap)),
677 ata_mode2str(ata_umode(atacap)),
678 (atacap->hwres & ATA_CABLE_ID) ? "80":"40");
683 sprintf(buffer, "%.40s/%.8s", atacap->model, atacap->revision);
684 device_set_desc_copy(atadev->dev, buffer);
685 if ((atadev->param.config & ATA_PROTO_ATAPI) &&
686 (atadev->param.config != ATA_CFA_MAGIC1) &&
687 (atadev->param.config != ATA_CFA_MAGIC2)) {
689 (atadev->param.config & ATA_DRQ_MASK) != ATA_DRQ_INTR &&
690 ata_umode(&atadev->param) >= ATA_UDMA2)
691 atadev->mode = ATA_DMA_MAX;
695 (ata_umode(&atadev->param) > 0 ||
696 ata_wmode(&atadev->param) > 0))
697 atadev->mode = ATA_DMA_MAX;
709 ata_identify(device_t dev)
711 struct ata_channel *ch = device_get_softc(dev);
712 struct ata_device *atadev;
714 device_t child, master = NULL;
715 int nchildren, i, n = ch->devices;
718 device_printf(dev, "Identifying devices: %08x\n", ch->devices);
721 /* Skip existing devices. */
722 if (!device_get_children(dev, &children, &nchildren)) {
723 for (i = 0; i < nchildren; i++) {
724 if (children[i] && (atadev = device_get_softc(children[i])))
725 n &= ~((ATA_ATA_MASTER | ATA_ATAPI_MASTER) << atadev->unit);
727 free(children, M_TEMP);
729 /* Create new devices. */
731 device_printf(dev, "New devices: %08x\n", n);
736 for (i = 0; i < ATA_PM; ++i) {
737 if (n & (((ATA_ATA_MASTER | ATA_ATAPI_MASTER) << i))) {
740 if (!(atadev = malloc(sizeof(struct ata_device),
741 M_ATA, M_NOWAIT | M_ZERO))) {
742 device_printf(dev, "out of memory\n");
747 if (n & (ATA_ATA_MASTER << i))
748 unit = (device_get_unit(dev) << 1) + i;
750 if ((child = ata_add_child(dev, atadev, unit))) {
752 * PATA slave should be identified first, to allow
753 * device cable detection on master to work properly.
755 if (i == 0 && (n & ATA_PORTMULTIPLIER) == 0 &&
756 (n & ((ATA_ATA_MASTER | ATA_ATAPI_MASTER) << 1)) != 0) {
760 if (ata_getparam(atadev, 1)) {
761 device_delete_child(dev, child);
770 atadev = device_get_softc(master);
771 if (ata_getparam(atadev, 1)) {
772 device_delete_child(dev, master);
776 bus_generic_probe(dev);
777 bus_generic_attach(dev);
783 ata_default_registers(device_t dev)
785 struct ata_channel *ch = device_get_softc(dev);
787 /* fill in the defaults from whats setup already */
788 ch->r_io[ATA_ERROR].res = ch->r_io[ATA_FEATURE].res;
789 ch->r_io[ATA_ERROR].offset = ch->r_io[ATA_FEATURE].offset;
790 ch->r_io[ATA_IREASON].res = ch->r_io[ATA_COUNT].res;
791 ch->r_io[ATA_IREASON].offset = ch->r_io[ATA_COUNT].offset;
792 ch->r_io[ATA_STATUS].res = ch->r_io[ATA_COMMAND].res;
793 ch->r_io[ATA_STATUS].offset = ch->r_io[ATA_COMMAND].offset;
794 ch->r_io[ATA_ALTSTAT].res = ch->r_io[ATA_CONTROL].res;
795 ch->r_io[ATA_ALTSTAT].offset = ch->r_io[ATA_CONTROL].offset;
799 ata_modify_if_48bit(struct ata_request *request)
801 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
802 struct ata_device *atadev = device_get_softc(request->dev);
804 atadev->flags &= ~ATA_D_48BIT_ACTIVE;
806 if (((request->u.ata.lba + request->u.ata.count) >= ATA_MAX_28BIT_LBA ||
807 request->u.ata.count > 256) &&
808 atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) {
810 /* translate command into 48bit version */
811 switch (request->u.ata.command) {
813 request->u.ata.command = ATA_READ48;
816 request->u.ata.command = ATA_READ_MUL48;
819 if (ch->flags & ATA_NO_48BIT_DMA) {
820 if (request->transfersize > DEV_BSIZE)
821 request->u.ata.command = ATA_READ_MUL48;
823 request->u.ata.command = ATA_READ48;
824 request->flags &= ~ATA_R_DMA;
827 request->u.ata.command = ATA_READ_DMA48;
829 case ATA_READ_DMA_QUEUED:
830 if (ch->flags & ATA_NO_48BIT_DMA) {
831 if (request->transfersize > DEV_BSIZE)
832 request->u.ata.command = ATA_READ_MUL48;
834 request->u.ata.command = ATA_READ48;
835 request->flags &= ~ATA_R_DMA;
838 request->u.ata.command = ATA_READ_DMA_QUEUED48;
841 request->u.ata.command = ATA_WRITE48;
844 request->u.ata.command = ATA_WRITE_MUL48;
847 if (ch->flags & ATA_NO_48BIT_DMA) {
848 if (request->transfersize > DEV_BSIZE)
849 request->u.ata.command = ATA_WRITE_MUL48;
851 request->u.ata.command = ATA_WRITE48;
852 request->flags &= ~ATA_R_DMA;
855 request->u.ata.command = ATA_WRITE_DMA48;
857 case ATA_WRITE_DMA_QUEUED:
858 if (ch->flags & ATA_NO_48BIT_DMA) {
859 if (request->transfersize > DEV_BSIZE)
860 request->u.ata.command = ATA_WRITE_MUL48;
862 request->u.ata.command = ATA_WRITE48;
863 request->u.ata.command = ATA_WRITE48;
864 request->flags &= ~ATA_R_DMA;
867 request->u.ata.command = ATA_WRITE_DMA_QUEUED48;
870 request->u.ata.command = ATA_FLUSHCACHE48;
872 case ATA_SET_MAX_ADDRESS:
873 request->u.ata.command = ATA_SET_MAX_ADDRESS48;
878 atadev->flags |= ATA_D_48BIT_ACTIVE;
880 else if (atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) {
882 /* translate command into 48bit version */
883 switch (request->u.ata.command) {
885 request->u.ata.command = ATA_FLUSHCACHE48;
887 case ATA_READ_NATIVE_MAX_ADDRESS:
888 request->u.ata.command = ATA_READ_NATIVE_MAX_ADDRESS48;
890 case ATA_SET_MAX_ADDRESS:
891 request->u.ata.command = ATA_SET_MAX_ADDRESS48;
896 atadev->flags |= ATA_D_48BIT_ACTIVE;
901 ata_udelay(int interval)
903 /* for now just use DELAY, the timer/sleep subsytems are not there yet */
904 if (1 || interval < (1000000/hz) || ata_delayed_attach)
907 pause("ataslp", interval/(1000000/hz));
911 ata_unit2str(struct ata_device *atadev)
913 struct ata_channel *ch = device_get_softc(device_get_parent(atadev->dev));
916 if (ch->devices & ATA_PORTMULTIPLIER)
917 sprintf(str, "port%d", atadev->unit);
919 sprintf(str, "%s", atadev->unit == ATA_MASTER ? "master" : "slave");
924 ata_mode2str(int mode)
927 case -1: return "UNSUPPORTED";
928 case ATA_PIO0: return "PIO0";
929 case ATA_PIO1: return "PIO1";
930 case ATA_PIO2: return "PIO2";
931 case ATA_PIO3: return "PIO3";
932 case ATA_PIO4: return "PIO4";
933 case ATA_WDMA0: return "WDMA0";
934 case ATA_WDMA1: return "WDMA1";
935 case ATA_WDMA2: return "WDMA2";
936 case ATA_UDMA0: return "UDMA16";
937 case ATA_UDMA1: return "UDMA25";
938 case ATA_UDMA2: return "UDMA33";
939 case ATA_UDMA3: return "UDMA40";
940 case ATA_UDMA4: return "UDMA66";
941 case ATA_UDMA5: return "UDMA100";
942 case ATA_UDMA6: return "UDMA133";
943 case ATA_SA150: return "SATA150";
944 case ATA_SA300: return "SATA300";
945 case ATA_USB: return "USB";
946 case ATA_USB1: return "USB1";
947 case ATA_USB2: return "USB2";
949 if (mode & ATA_DMA_MASK)
957 ata_atapi(device_t dev)
959 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
960 struct ata_device *atadev = device_get_softc(dev);
962 return (ch->devices & (ATA_ATAPI_MASTER << atadev->unit));
966 ata_pmode(struct ata_params *ap)
968 if (ap->atavalid & ATA_FLAG_64_70) {
969 if (ap->apiomodes & 0x02)
971 if (ap->apiomodes & 0x01)
974 if (ap->mwdmamodes & 0x04)
976 if (ap->mwdmamodes & 0x02)
978 if (ap->mwdmamodes & 0x01)
980 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200)
982 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100)
984 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000)
990 ata_wmode(struct ata_params *ap)
992 if (ap->mwdmamodes & 0x04)
994 if (ap->mwdmamodes & 0x02)
996 if (ap->mwdmamodes & 0x01)
1002 ata_umode(struct ata_params *ap)
1004 if (ap->atavalid & ATA_FLAG_88) {
1005 if (ap->udmamodes & 0x40)
1007 if (ap->udmamodes & 0x20)
1009 if (ap->udmamodes & 0x10)
1011 if (ap->udmamodes & 0x08)
1013 if (ap->udmamodes & 0x04)
1015 if (ap->udmamodes & 0x02)
1017 if (ap->udmamodes & 0x01)
1024 ata_limit_mode(device_t dev, int mode, int maxmode)
1026 struct ata_device *atadev = device_get_softc(dev);
1028 if (maxmode && mode > maxmode)
1031 if (mode >= ATA_UDMA0 && ata_umode(&atadev->param) > 0)
1032 return min(mode, ata_umode(&atadev->param));
1034 if (mode >= ATA_WDMA0 && ata_wmode(&atadev->param) > 0)
1035 return min(mode, ata_wmode(&atadev->param));
1037 if (mode > ata_pmode(&atadev->param))
1038 return min(mode, ata_pmode(&atadev->param));
1044 bswap(int8_t *buf, int len)
1046 u_int16_t *ptr = (u_int16_t*)(buf + len);
1048 while (--ptr >= (u_int16_t*)buf)
1053 btrim(int8_t *buf, int len)
1057 for (ptr = buf; ptr < buf+len; ++ptr)
1058 if (!*ptr || *ptr == '_')
1060 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
1065 bpack(int8_t *src, int8_t *dst, int len)
1069 for (i = j = blank = 0 ; i < len; i++) {
1070 if (blank && src[i] == ' ') continue;
1071 if (blank && src[i] != ' ') {
1076 if (src[i] == ' ') {
1092 ata_module_event_handler(module_t mod, int what, void *arg)
1094 static struct cdev *atacdev;
1098 /* register controlling device */
1099 atacdev = make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "ata");
1102 /* register boot attach to be run when interrupts are enabled */
1103 if (!(ata_delayed_attach = (struct intr_config_hook *)
1104 malloc(sizeof(struct intr_config_hook),
1105 M_TEMP, M_NOWAIT | M_ZERO))) {
1106 printf("ata: malloc of delayed attach hook failed\n");
1109 ata_delayed_attach->ich_func = (void*)ata_boot_attach;
1110 if (config_intrhook_establish(ata_delayed_attach) != 0) {
1111 printf("ata: config_intrhook_establish failed\n");
1112 free(ata_delayed_attach, M_TEMP);
1118 /* deregister controlling device */
1119 destroy_dev(atacdev);
1127 static moduledata_t ata_moduledata = { "ata", ata_module_event_handler, NULL };
1128 DECLARE_MODULE(ata, ata_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
1129 MODULE_VERSION(ata, 1);
1134 ata_request_zone = uma_zcreate("ata_request", sizeof(struct ata_request),
1135 NULL, NULL, NULL, NULL, 0, 0);
1136 ata_composite_zone = uma_zcreate("ata_composite",
1137 sizeof(struct ata_composite),
1138 NULL, NULL, NULL, NULL, 0, 0);
1140 SYSINIT(ata_register, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL);
1145 uma_zdestroy(ata_composite_zone);
1146 uma_zdestroy(ata_request_zone);
1148 SYSUNINIT(ata_unregister, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_uninit, NULL);