2 * Copyright (c) 1998 - 2006 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.
26 * $FreeBSD: src/sys/dev/ata/ata-all.c,v 1.273 2006/05/12 05:04:40 jhb Exp $
27 * $DragonFly: src/sys/dev/disk/nata/ata-all.c,v 1.10 2007/06/01 00:31:14 dillon Exp $
32 #include <sys/param.h>
34 #include <sys/callout.h>
36 #include <sys/ctype.h>
37 #include <sys/device.h>
38 #include <sys/endian.h>
39 #include <sys/kernel.h>
40 #include <sys/libkern.h>
41 #include <sys/lock.h> /* for {get,rel}_mplock() */
42 #include <sys/malloc.h>
43 #include <sys/module.h>
45 #include <sys/objcache.h>
46 #include <sys/queue.h>
47 #include <sys/spinlock2.h>
48 #include <sys/sysctl.h>
49 #include <sys/systm.h>
54 /* device structure */
55 static d_ioctl_t ata_ioctl;
56 static struct dev_ops ata_ops = {
64 static void ata_boot_attach(void);
65 static device_t ata_add_child(device_t, struct ata_device *, int);
66 static int ata_getparam(struct ata_device *, int);
67 static void bswap(int8_t *, int);
68 static void btrim(int8_t *, int);
69 static void bpack(int8_t *, int8_t *, int);
72 MALLOC_DEFINE(M_ATA, "ata_generic", "ATA driver generic layer");
73 int (*ata_raid_ioctl_func)(u_long cmd, caddr_t data) = NULL;
74 devclass_t ata_devclass;
75 struct objcache *ata_request_cache;
76 struct objcache *ata_composite_cache;
77 struct objcache_malloc_args ata_request_malloc_args = {
78 sizeof(struct ata_request), M_ATA };
79 struct objcache_malloc_args ata_composite_malloc_args = {
80 sizeof(struct ata_composite), M_ATA };
84 static int ata_dma = 1;
85 static int atapi_dma = 1;
88 SYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters");
89 TUNABLE_INT("hw.ata.ata_dma", &ata_dma);
90 SYSCTL_INT(_hw_ata, OID_AUTO, ata_dma, CTLFLAG_RW, &ata_dma, 0,
91 "ATA disk DMA mode control");
92 TUNABLE_INT("hw.ata.atapi_dma", &atapi_dma);
93 SYSCTL_INT(_hw_ata, OID_AUTO, atapi_dma, CTLFLAG_RW, &atapi_dma, 0,
94 "ATAPI device DMA mode control");
95 TUNABLE_INT("hw.ata.wc", &ata_wc);
96 SYSCTL_INT(_hw_ata, OID_AUTO, ata_wc, CTLFLAG_RW, &ata_wc, 0,
97 "ATA disk write caching");
100 * newbus device interface related functions
103 ata_probe(device_t dev)
109 ata_attach(device_t dev)
111 struct ata_channel *ch = device_get_softc(dev);
114 /* check that we have a virgin channel to attach */
118 /* initialize the softc basics */
120 ch->state = ATA_IDLE;
121 spin_init(&ch->state_mtx);
122 spin_init(&ch->queue_mtx);
123 TAILQ_INIT(&ch->ata_queue);
125 /* reset the controller HW, the channel and device(s) */
126 while (ATA_LOCKING(dev, ATA_LF_LOCK) != ch->unit)
127 tsleep(&error, 0, "ataatch", 1);
129 ATA_LOCKING(dev, ATA_LF_UNLOCK);
131 /* setup interrupt delivery */
133 ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
134 RF_SHAREABLE | RF_ACTIVE);
136 device_printf(dev, "unable to allocate interrupt\n");
139 if ((error = bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS,
140 (driver_intr_t *)ata_interrupt, ch, &ch->ih,
142 device_printf(dev, "unable to setup interrupt\n");
146 /* probe and attach devices on this channel unless we are in early boot */
152 ata_detach(device_t dev)
154 struct ata_channel *ch = device_get_softc(dev);
158 /* check that we have a valid channel to detach */
162 /* grap the channel lock so no new requests gets launched */
163 spin_lock_wr(&ch->state_mtx);
164 ch->state |= ATA_STALL_QUEUE;
165 spin_unlock_wr(&ch->state_mtx);
167 /* detach & delete all children */
168 if (!device_get_children(dev, &children, &nchildren)) {
169 for (i = 0; i < nchildren; i++)
171 device_delete_child(dev, children[i]);
172 kfree(children, M_TEMP);
175 /* release resources */
176 bus_teardown_intr(dev, ch->r_irq, ch->ih);
177 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
179 spin_uninit(&ch->state_mtx);
180 spin_uninit(&ch->queue_mtx);
185 ata_reinit(device_t dev)
187 struct ata_channel *ch = device_get_softc(dev);
188 struct ata_request *request;
192 /* check that we have a valid channel to reinit */
193 if (!ch || !ch->r_irq)
197 device_printf(dev, "reiniting channel ..\n");
199 /* poll for locking the channel */
200 while (ATA_LOCKING(dev, ATA_LF_LOCK) != ch->unit)
201 tsleep(&dev, 0, "atarini", 1);
203 /* catch eventual request in ch->running */
204 spin_lock_wr(&ch->state_mtx);
205 if ((request = ch->running))
206 callout_stop(&request->callout);
209 /* unconditionally grap the channel lock */
210 ch->state |= ATA_STALL_QUEUE;
211 spin_unlock_wr(&ch->state_mtx);
213 /* reset the controller HW, the channel and device(s) */
216 /* reinit the children and delete any that fails */
217 if (!device_get_children(dev, &children, &nchildren)) {
219 for (i = 0; i < nchildren; i++) {
220 /* did any children go missing ? */
221 if (children[i] && device_is_attached(children[i]) &&
222 ATA_REINIT(children[i])) {
224 * if we had a running request and its device matches
225 * this child we need to inform the request that the
228 if (request && request->dev == children[i]) {
229 request->result = ENXIO;
230 device_printf(request->dev, "FAILURE - device detached\n");
232 /* if not timeout finish request here */
233 if (!(request->flags & ATA_R_TIMEOUT))
237 device_delete_child(dev, children[i]);
240 kfree(children, M_TEMP);
244 /* if we still have a good request put it on the queue again */
245 if (request && !(request->flags & ATA_R_TIMEOUT)) {
246 device_printf(request->dev,
247 "WARNING - %s requeued due to channel reset",
248 ata_cmd2str(request));
249 if (!(request->flags & (ATA_R_ATAPI | ATA_R_CONTROL)))
250 kprintf(" LBA=%ju", request->u.ata.lba);
252 request->flags |= ATA_R_REQUEUE;
253 ata_queue_request(request);
256 /* we're done release the channel for new work */
257 spin_lock_wr(&ch->state_mtx);
258 ch->state = ATA_IDLE;
259 spin_unlock_wr(&ch->state_mtx);
260 ATA_LOCKING(dev, ATA_LF_UNLOCK);
263 device_printf(dev, "reinit done ..\n");
265 /* kick off requests on the queue */
271 ata_suspend(device_t dev)
273 struct ata_channel *ch;
275 /* check for valid device */
276 if (!dev || !(ch = device_get_softc(dev)))
279 /* wait for the channel to be IDLE or detached before suspending */
281 spin_lock_wr(&ch->state_mtx);
282 if (ch->state == ATA_IDLE) {
283 ch->state = ATA_ACTIVE;
284 spin_unlock_wr(&ch->state_mtx);
287 spin_unlock_wr(&ch->state_mtx);
288 tsleep(ch, 0, "atasusp", hz/10);
290 ATA_LOCKING(dev, ATA_LF_UNLOCK);
295 ata_resume(device_t dev)
297 struct ata_channel *ch;
300 /* check for valid device */
301 if (!dev || !(ch = device_get_softc(dev)))
304 /* reinit the devices, we dont know what mode/state they are in */
305 error = ata_reinit(dev);
307 /* kick off requests on the queue */
313 ata_interrupt(void *data)
315 struct ata_channel *ch = (struct ata_channel *)data;
316 struct ata_request *request;
318 spin_lock_wr(&ch->state_mtx);
320 /* ignore interrupt if its not for us */
321 if (ch->hw.status && !ch->hw.status(ch->dev))
324 /* do we have a running request */
325 if (!(request = ch->running))
328 /* XXX TGEN Ignore weird ATAPI+DMA interrupts on SMP */
329 if (ch->dma && (request->flags & ATA_R_ATAPI)) {
330 int status = ATA_IDX_INB(ch, ATA_STATUS);
331 int error = ATA_IDX_INB(ch, ATA_ERROR);
332 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
333 if (((status & (ATA_S_DWF|ATA_S_DRQ)) == (ATA_S_DWF|ATA_S_DRQ)) &&
334 ((error & ATA_E_ILI) == ATA_E_ILI) &&
335 !(bmstat & ATA_BMSTAT_ERROR)) {
337 device_printf(request->dev, "ignoring weird interrupt\n");
342 ATA_DEBUG_RQ(request, "interrupt");
344 /* safetycheck for the right state */
345 if (ch->state == ATA_IDLE) {
346 device_printf(request->dev, "interrupt on idle channel ignored\n");
351 * we have the HW locks, so end the transaction for this request
352 * if it finishes immediately otherwise wait for next interrupt
354 if (ch->hw.end_transaction(request) == ATA_OP_FINISHED) {
356 if (ch->state == ATA_ACTIVE)
357 ch->state = ATA_IDLE;
358 spin_unlock_wr(&ch->state_mtx);
359 ATA_LOCKING(ch->dev, ATA_LF_UNLOCK);
364 spin_unlock_wr(&ch->state_mtx);
369 * device related interfaces
372 ata_ioctl(struct dev_ioctl_args *ap)
374 device_t device, *children;
375 struct ata_ioc_devices *devices = (struct ata_ioc_devices *)ap->a_data;
376 int *value = (int *)ap->a_data;
377 int i, nchildren, error = ENOTTY;
380 case IOCATAGMAXCHANNEL:
381 *value = devclass_get_maxunit(ata_devclass);
386 if (*value > devclass_get_maxunit(ata_devclass) ||
387 !(device = devclass_get_device(ata_devclass, *value)))
389 error = ata_reinit(device);
394 if (*value > devclass_get_maxunit(ata_devclass) ||
395 !(device = devclass_get_device(ata_devclass, *value)))
397 /* XXX SOS should enable channel HW on controller */
398 error = ata_attach(device);
402 if (*value > devclass_get_maxunit(ata_devclass) ||
403 !(device = devclass_get_device(ata_devclass, *value)))
405 error = ata_detach(device);
406 /* XXX SOS should disable channel HW on controller */
410 if (devices->channel > devclass_get_maxunit(ata_devclass) ||
411 !(device = devclass_get_device(ata_devclass, devices->channel)))
413 bzero(devices->name[0], 32);
414 bzero(&devices->params[0], sizeof(struct ata_params));
415 bzero(devices->name[1], 32);
416 bzero(&devices->params[1], sizeof(struct ata_params));
417 if (!device_get_children(device, &children, &nchildren)) {
418 for (i = 0; i < nchildren; i++) {
419 if (children[i] && device_is_attached(children[i])) {
420 struct ata_device *atadev = device_get_softc(children[i]);
422 if (atadev->unit == ATA_MASTER) {
423 strncpy(devices->name[0],
424 device_get_nameunit(children[i]), 32);
425 bcopy(&atadev->param, &devices->params[0],
426 sizeof(struct ata_params));
428 if (atadev->unit == ATA_SLAVE) {
429 strncpy(devices->name[1],
430 device_get_nameunit(children[i]), 32);
431 bcopy(&atadev->param, &devices->params[1],
432 sizeof(struct ata_params));
436 kfree(children, M_TEMP);
444 if (ata_raid_ioctl_func)
445 error = ata_raid_ioctl_func(ap->a_cmd, ap->a_data);
451 ata_device_ioctl(device_t dev, u_long cmd, caddr_t data)
453 struct ata_device *atadev = device_get_softc(dev);
454 struct ata_ioc_request *ioc_request = (struct ata_ioc_request *)data;
455 struct ata_params *params = (struct ata_params *)data;
456 int *mode = (int *)data;
457 struct ata_request *request;
463 if (!(buf = kmalloc(ioc_request->count, M_ATA, M_WAITOK | M_NULLOK))) {
466 if (!(request = ata_alloc_request())) {
470 if (ioc_request->flags & ATA_CMD_WRITE) {
471 error = copyin(ioc_request->data, buf, ioc_request->count);
474 ata_free_request(request);
479 if (ioc_request->flags & ATA_CMD_ATAPI) {
480 request->flags = ATA_R_ATAPI;
481 bcopy(ioc_request->u.atapi.ccb, request->u.atapi.ccb, 16);
484 request->u.ata.command = ioc_request->u.ata.command;
485 request->u.ata.feature = ioc_request->u.ata.feature;
486 request->u.ata.lba = ioc_request->u.ata.lba;
487 request->u.ata.count = ioc_request->u.ata.count;
489 request->timeout = ioc_request->timeout;
491 request->bytecount = ioc_request->count;
492 request->transfersize = request->bytecount;
493 if (ioc_request->flags & ATA_CMD_CONTROL)
494 request->flags |= ATA_R_CONTROL;
495 if (ioc_request->flags & ATA_CMD_READ)
496 request->flags |= ATA_R_READ;
497 if (ioc_request->flags & ATA_CMD_WRITE)
498 request->flags |= ATA_R_WRITE;
499 ata_queue_request(request);
500 if (request->flags & ATA_R_ATAPI) {
501 bcopy(&request->u.atapi.sense, &ioc_request->u.atapi.sense,
502 sizeof(struct atapi_sense));
505 ioc_request->u.ata.command = request->u.ata.command;
506 ioc_request->u.ata.feature = request->u.ata.feature;
507 ioc_request->u.ata.lba = request->u.ata.lba;
508 ioc_request->u.ata.count = request->u.ata.count;
510 ioc_request->error = request->result;
511 if (ioc_request->flags & ATA_CMD_READ)
512 error = copyout(buf, ioc_request->data, ioc_request->count);
516 ata_free_request(request);
520 ata_getparam(atadev, 0);
521 bcopy(&atadev->param, params, sizeof(struct ata_params));
525 atadev->mode = *mode;
526 ATA_SETMODE(device_get_parent(dev), dev);
530 *mode = atadev->mode;
538 ata_boot_attach(void)
540 struct ata_channel *ch;
545 /* kick of probe and attach on all channels */
546 for (ctlr = 0; ctlr < devclass_get_maxunit(ata_devclass); ctlr++) {
547 if ((ch = devclass_get_softc(ata_devclass, ctlr))) {
548 ata_identify(ch->dev);
557 * misc support functions
560 ata_add_child(device_t parent, struct ata_device *atadev, int unit)
564 if ((child = device_add_child(parent, NULL, unit))) {
565 device_set_softc(child, atadev);
568 atadev->max_iosize = DEV_BSIZE;
569 atadev->mode = ATA_PIO_MAX;
575 ata_getparam(struct ata_device *atadev, int init)
577 struct ata_channel *ch = device_get_softc(device_get_parent(atadev->dev));
578 struct ata_request *request;
579 u_int8_t command = 0;
580 int error = ENOMEM, retries = 2;
583 (atadev->unit == ATA_MASTER ? ATA_ATA_MASTER : ATA_ATA_SLAVE))
584 command = ATA_ATA_IDENTIFY;
586 (atadev->unit == ATA_MASTER ? ATA_ATAPI_MASTER : ATA_ATAPI_SLAVE))
587 command = ATA_ATAPI_IDENTIFY;
591 while (retries-- > 0 && error) {
592 if (!(request = ata_alloc_request()))
594 request->dev = atadev->dev;
595 request->timeout = 1;
596 request->retries = 0;
597 request->u.ata.command = command;
598 request->flags = (ATA_R_READ|ATA_R_AT_HEAD|ATA_R_DIRECT|ATA_R_QUIET);
599 request->data = (void *)&atadev->param;
600 request->bytecount = sizeof(struct ata_params);
601 request->donecount = 0;
602 request->transfersize = DEV_BSIZE;
603 ata_queue_request(request);
604 error = request->result;
605 ata_free_request(request);
608 if (!error && (isprint(atadev->param.model[0]) ||
609 isprint(atadev->param.model[1]))) {
610 struct ata_params *atacap = &atadev->param;
614 for (ptr = (int16_t *)atacap;
615 ptr < (int16_t *)atacap + sizeof(struct ata_params)/2; ptr++) {
616 *ptr = le16toh(*ptr);
618 if (!(!strncmp(atacap->model, "FX", 2) ||
619 !strncmp(atacap->model, "NEC", 3) ||
620 !strncmp(atacap->model, "Pioneer", 7) ||
621 !strncmp(atacap->model, "SHARP", 5))) {
622 bswap(atacap->model, sizeof(atacap->model));
623 bswap(atacap->revision, sizeof(atacap->revision));
624 bswap(atacap->serial, sizeof(atacap->serial));
626 btrim(atacap->model, sizeof(atacap->model));
627 bpack(atacap->model, atacap->model, sizeof(atacap->model));
628 btrim(atacap->revision, sizeof(atacap->revision));
629 bpack(atacap->revision, atacap->revision, sizeof(atacap->revision));
630 btrim(atacap->serial, sizeof(atacap->serial));
631 bpack(atacap->serial, atacap->serial, sizeof(atacap->serial));
634 kprintf("ata%d-%s: pio=%s wdma=%s udma=%s cable=%s wire\n",
635 device_get_unit(ch->dev),
636 atadev->unit == ATA_MASTER ? "master" : "slave",
637 ata_mode2str(ata_pmode(atacap)),
638 ata_mode2str(ata_wmode(atacap)),
639 ata_mode2str(ata_umode(atacap)),
640 (atacap->hwres & ATA_CABLE_ID) ? "80":"40");
643 ksprintf(buffer, "%.40s/%.8s", atacap->model, atacap->revision);
644 device_set_desc_copy(atadev->dev, buffer);
645 if ((atadev->param.config & ATA_PROTO_ATAPI) &&
646 (atadev->param.config != ATA_CFA_MAGIC1) &&
647 (atadev->param.config != ATA_CFA_MAGIC2)) {
648 if (atapi_dma && ch->dma &&
649 (atadev->param.config & ATA_DRQ_MASK) != ATA_DRQ_INTR &&
650 ata_umode(&atadev->param) >= ATA_UDMA2)
651 atadev->mode = ATA_DMA_MAX;
654 if (ata_dma && ch->dma &&
655 (ata_umode(&atadev->param) > 0 ||
656 ata_wmode(&atadev->param) > 0))
657 atadev->mode = ATA_DMA_MAX;
669 ata_identify(device_t dev)
671 struct ata_channel *ch = device_get_softc(dev);
672 struct ata_device *master = NULL, *slave = NULL;
673 device_t master_child = NULL, slave_child = NULL;
674 int master_unit = -1, slave_unit = -1;
676 if (ch->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER)) {
677 if (!(master = kmalloc(sizeof(struct ata_device),
678 M_ATA, M_INTWAIT | M_ZERO))) {
679 device_printf(dev, "out of memory\n");
682 master->unit = ATA_MASTER;
684 if (ch->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE)) {
685 if (!(slave = kmalloc(sizeof(struct ata_device),
686 M_ATA, M_INTWAIT | M_ZERO))) {
687 kfree(master, M_ATA);
688 device_printf(dev, "out of memory\n");
691 slave->unit = ATA_SLAVE;
695 if (ch->devices & ATA_ATA_MASTER)
696 master_unit = (device_get_unit(dev) << 1);
698 if (master && !(master_child = ata_add_child(dev, master, master_unit))) {
699 kfree(master, M_ATA);
703 if (ch->devices & ATA_ATA_SLAVE)
704 slave_unit = (device_get_unit(dev) << 1) + 1;
706 if (slave && !(slave_child = ata_add_child(dev, slave, slave_unit))) {
711 if (slave && ata_getparam(slave, 1)) {
712 device_delete_child(dev, slave_child);
715 if (master && ata_getparam(master, 1)) {
716 device_delete_child(dev, master_child);
717 kfree(master, M_ATA);
720 bus_generic_probe(dev);
721 bus_generic_attach(dev);
726 ata_default_registers(device_t dev)
728 struct ata_channel *ch = device_get_softc(dev);
730 /* fill in the defaults from whats setup already */
731 ch->r_io[ATA_ERROR].res = ch->r_io[ATA_FEATURE].res;
732 ch->r_io[ATA_ERROR].offset = ch->r_io[ATA_FEATURE].offset;
733 ch->r_io[ATA_IREASON].res = ch->r_io[ATA_COUNT].res;
734 ch->r_io[ATA_IREASON].offset = ch->r_io[ATA_COUNT].offset;
735 ch->r_io[ATA_STATUS].res = ch->r_io[ATA_COMMAND].res;
736 ch->r_io[ATA_STATUS].offset = ch->r_io[ATA_COMMAND].offset;
737 ch->r_io[ATA_ALTSTAT].res = ch->r_io[ATA_CONTROL].res;
738 ch->r_io[ATA_ALTSTAT].offset = ch->r_io[ATA_CONTROL].offset;
742 ata_modify_if_48bit(struct ata_request *request)
744 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
745 struct ata_device *atadev = device_get_softc(request->dev);
747 atadev->flags &= ~ATA_D_48BIT_ACTIVE;
749 if ((request->u.ata.lba >= ATA_MAX_28BIT_LBA ||
750 request->u.ata.count > 256) &&
751 atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) {
753 /* translate command into 48bit version */
754 switch (request->u.ata.command) {
756 request->u.ata.command = ATA_READ48;
759 request->u.ata.command = ATA_READ_MUL48;
762 if (ch->flags & ATA_NO_48BIT_DMA) {
763 if (request->transfersize > DEV_BSIZE)
764 request->u.ata.command = ATA_READ_MUL48;
766 request->u.ata.command = ATA_READ48;
767 request->flags &= ~ATA_R_DMA;
770 request->u.ata.command = ATA_READ_DMA48;
772 case ATA_READ_DMA_QUEUED:
773 if (ch->flags & ATA_NO_48BIT_DMA) {
774 if (request->transfersize > DEV_BSIZE)
775 request->u.ata.command = ATA_READ_MUL48;
777 request->u.ata.command = ATA_READ48;
778 request->flags &= ~ATA_R_DMA;
781 request->u.ata.command = ATA_READ_DMA_QUEUED48;
784 request->u.ata.command = ATA_WRITE48;
787 request->u.ata.command = ATA_WRITE_MUL48;
790 if (ch->flags & ATA_NO_48BIT_DMA) {
791 if (request->transfersize > DEV_BSIZE)
792 request->u.ata.command = ATA_WRITE_MUL48;
794 request->u.ata.command = ATA_WRITE48;
795 request->flags &= ~ATA_R_DMA;
798 request->u.ata.command = ATA_WRITE_DMA48;
800 case ATA_WRITE_DMA_QUEUED:
801 if (ch->flags & ATA_NO_48BIT_DMA) {
802 if (request->transfersize > DEV_BSIZE)
803 request->u.ata.command = ATA_WRITE_MUL48;
805 request->u.ata.command = ATA_WRITE48;
806 request->u.ata.command = ATA_WRITE48;
807 request->flags &= ~ATA_R_DMA;
810 request->u.ata.command = ATA_WRITE_DMA_QUEUED48;
813 request->u.ata.command = ATA_FLUSHCACHE48;
815 case ATA_READ_NATIVE_MAX_ADDDRESS:
816 request->u.ata.command = ATA_READ_NATIVE_MAX_ADDDRESS48;
818 case ATA_SET_MAX_ADDRESS:
819 request->u.ata.command = ATA_SET_MAX_ADDRESS48;
824 atadev->flags |= ATA_D_48BIT_ACTIVE;
829 ata_udelay(int interval)
831 /* for now just use DELAY, the timer/sleep subsytems are not there yet */
832 /* XXX use DRIVERSLEEP if possible */
833 if (1 || interval < (1000000/hz))
836 tsleep(&interval, 0, "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";
861 case ATA_USB: return "USB";
862 case ATA_USB1: return "USB1";
863 case ATA_USB2: return "USB2";
865 if (mode & ATA_DMA_MASK)
873 ata_pmode(struct ata_params *ap)
875 if (ap->atavalid & ATA_FLAG_64_70) {
876 if (ap->apiomodes & 0x02)
878 if (ap->apiomodes & 0x01)
881 if (ap->mwdmamodes & 0x04)
883 if (ap->mwdmamodes & 0x02)
885 if (ap->mwdmamodes & 0x01)
887 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200)
889 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100)
891 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000)
897 ata_wmode(struct ata_params *ap)
899 if (ap->mwdmamodes & 0x04)
901 if (ap->mwdmamodes & 0x02)
903 if (ap->mwdmamodes & 0x01)
909 ata_umode(struct ata_params *ap)
911 if (ap->atavalid & ATA_FLAG_88) {
912 if (ap->udmamodes & 0x40)
914 if (ap->udmamodes & 0x20)
916 if (ap->udmamodes & 0x10)
918 if (ap->udmamodes & 0x08)
920 if (ap->udmamodes & 0x04)
922 if (ap->udmamodes & 0x02)
924 if (ap->udmamodes & 0x01)
931 ata_limit_mode(device_t dev, int mode, int maxmode)
933 struct ata_device *atadev = device_get_softc(dev);
935 if (maxmode && mode > maxmode)
938 if (mode >= ATA_UDMA0 && ata_umode(&atadev->param) > 0)
939 return min(mode, ata_umode(&atadev->param));
941 if (mode >= ATA_WDMA0 && ata_wmode(&atadev->param) > 0)
942 return min(mode, ata_wmode(&atadev->param));
944 if (mode > ata_pmode(&atadev->param))
945 return min(mode, ata_pmode(&atadev->param));
951 bswap(int8_t *buf, int len)
953 u_int16_t *ptr = (u_int16_t*)(buf + len);
955 while (--ptr >= (u_int16_t*)buf)
960 btrim(int8_t *buf, int len)
964 for (ptr = buf; ptr < buf+len; ++ptr)
965 if (!*ptr || *ptr == '_')
967 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
972 bpack(int8_t *src, int8_t *dst, int len)
976 for (i = j = blank = 0 ; i < len; i++) {
977 if (blank && src[i] == ' ') continue;
978 if (blank && src[i] != ' ') {
999 ata_module_event_handler(module_t mod, int what, void *arg)
1001 /* static because we need the reference at destruction time */
1002 static cdev_t atacdev;
1006 /* register controlling device */
1007 dev_ops_add(&ata_ops, 0, 0);
1008 atacdev = make_dev(&ata_ops, 0, UID_ROOT, GID_OPERATOR, 0600, "ata");
1009 reference_dev(atacdev);
1013 /* deregister controlling device */
1014 destroy_dev(atacdev);
1015 dev_ops_remove(&ata_ops, 0, 0);
1023 static moduledata_t ata_moduledata = { "ata", ata_module_event_handler, NULL };
1024 DECLARE_MODULE(ata, ata_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
1025 MODULE_VERSION(ata, 1);
1028 * Construct a completely zero'ed ata_request. On objcache_put(), an
1029 * ata_request object is also zero'ed, so objcache_get() is guaranteed to give
1030 * completely zero'ed objects without spending too much time.
1033 ata_request_cache_ctor(void *obj, void *private, int ocflags)
1035 struct ata_request *arp = obj;
1037 bzero(arp, sizeof(struct ata_request));
1042 * Construct a completely zero'ed ata_composite. On objcache_put(), an
1043 * ata_composite object is also zero'ed, so objcache_get() is guaranteed to give
1044 * completely zero'ed objects without spending too much time.
1047 ata_composite_cache_ctor(void *obj, void *private, int ocflags)
1049 struct ata_composite *acp = obj;
1051 bzero(acp, sizeof(struct ata_composite));
1058 ata_request_cache = objcache_create("ata_request", 0, 0,
1059 ata_request_cache_ctor, NULL, NULL,
1060 objcache_malloc_alloc,
1061 objcache_malloc_free,
1062 &ata_request_malloc_args);
1063 ata_composite_cache = objcache_create("ata_composite", 0, 0,
1064 ata_composite_cache_ctor, NULL, NULL,
1065 objcache_malloc_alloc,
1066 objcache_malloc_free,
1067 &ata_composite_malloc_args);
1069 SYSINIT(ata_register, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL);
1074 objcache_destroy(ata_composite_cache);
1075 objcache_destroy(ata_request_cache);
1077 SYSUNINIT(ata_unregister, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_uninit, NULL);