2 * Copyright (c) 1998 - 2005 Søren Schmidt <sos@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/endian.h>
39 #include <sys/ctype.h>
43 #include <sys/malloc.h>
44 #include <sys/sysctl.h>
46 #include <sys/taskqueue.h>
48 #include <machine/stdarg.h>
49 #include <machine/resource.h>
50 #include <machine/bus.h>
53 #include <machine/md_var.h>
55 #include <dev/ata/ata-all.h>
58 /* device structure */
59 static d_ioctl_t ata_ioctl;
60 static struct cdevsw ata_cdevsw = {
61 .d_version = D_VERSION,
62 .d_flags = D_NEEDGIANT, /* we need this as newbus isn't mpsafe */
68 static void ata_interrupt(void *);
69 static void ata_boot_attach(void);
70 static device_t ata_add_child(device_t, struct ata_device *, int);
71 static void bswap(int8_t *, int);
72 static void btrim(int8_t *, int);
73 static void bpack(int8_t *, int8_t *, int);
76 MALLOC_DEFINE(M_ATA, "ata_generic", "ATA driver generic layer");
77 int (*ata_raid_ioctl_func)(u_long cmd, caddr_t data) = NULL;
78 devclass_t ata_devclass;
79 uma_zone_t ata_request_zone;
80 uma_zone_t ata_composite_zone;
84 static struct intr_config_hook *ata_delayed_attach = NULL;
85 static int ata_dma = 1;
86 static int atapi_dma = 1;
89 SYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters");
90 TUNABLE_INT("hw.ata.ata_dma", &ata_dma);
91 SYSCTL_INT(_hw_ata, OID_AUTO, ata_dma, CTLFLAG_RDTUN, &ata_dma, 0,
92 "ATA disk DMA mode control");
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");
101 * newbus device interface related functions
104 ata_probe(device_t dev)
110 ata_attach(device_t dev)
112 struct ata_channel *ch = device_get_softc(dev);
115 /* check that we have a virgin channel to attach */
119 /* initialize the softc basics */
121 ch->state = ATA_IDLE;
122 bzero(&ch->state_mtx, sizeof(struct mtx));
123 mtx_init(&ch->state_mtx, "ATA state lock", NULL, MTX_DEF);
124 bzero(&ch->queue_mtx, sizeof(struct mtx));
125 mtx_init(&ch->queue_mtx, "ATA queue lock", NULL, MTX_DEF);
126 TAILQ_INIT(&ch->ata_queue);
128 /* reset the controller HW, the channel and device(s) */
129 while (ATA_LOCKING(dev, ATA_LF_LOCK) != ch->unit)
130 tsleep(&error, PRIBIO, "ataatch", 1);
132 ATA_LOCKING(dev, ATA_LF_UNLOCK);
134 /* setup interrupt delivery */
136 ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
137 RF_SHAREABLE | RF_ACTIVE);
139 device_printf(dev, "unable to allocate interrupt\n");
142 if ((error = bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS,
143 ata_interrupt, ch, &ch->ih))) {
144 device_printf(dev, "unable to setup interrupt\n");
148 /* probe and attach devices on this channel unless we are in early boot */
149 if (!ata_delayed_attach)
155 ata_detach(device_t dev)
157 struct ata_channel *ch = device_get_softc(dev);
161 /* check that we have a valid channel to detach */
165 /* detach & delete all children */
166 if (!device_get_children(dev, &children, &nchildren)) {
167 for (i = 0; i < nchildren; i++)
169 device_delete_child(dev, children[i]);
170 free(children, M_TEMP);
173 /* release resources */
174 bus_teardown_intr(dev, ch->r_irq, ch->ih);
175 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
177 mtx_destroy(&ch->state_mtx);
178 mtx_destroy(&ch->queue_mtx);
183 ata_reinit(device_t dev)
185 struct ata_channel *ch = device_get_softc(dev);
186 struct ata_request *request;
190 /* check that we have a valid channel to reinit */
191 if (!ch || !ch->r_irq)
195 device_printf(dev, "reiniting channel ..\n");
197 /* poll for locking the channel */
198 while (ATA_LOCKING(dev, ATA_LF_LOCK) != ch->unit)
199 tsleep(&dev, PRIBIO, "atarini", 1);
201 /* unconditionally grap the channel lock */
202 mtx_lock(&ch->state_mtx);
203 ch->state = ATA_STALL_QUEUE;
204 mtx_unlock(&ch->state_mtx);
206 /* reset the controller HW, the channel and device(s) */
209 /* reinit the children and delete any that fails */
210 if (!device_get_children(dev, &children, &nchildren)) {
211 mtx_lock(&Giant); /* newbus suckage it needs Giant */
212 for (i = 0; i < nchildren; i++) {
213 if (children[i] && device_is_attached(children[i]))
214 if (ATA_REINIT(children[i])) {
216 * if we have a running request and its device matches
217 * this child we need to inform the request that the
218 * device is gone and remove it from ch->running
220 mtx_lock(&ch->state_mtx);
221 if (ch->running && ch->running->dev == children[i]) {
222 callout_stop(&ch->running->callout);
223 request = ch->running;
228 mtx_unlock(&ch->state_mtx);
231 request->result = ENXIO;
232 device_printf(request->dev,
233 "FAILURE - device detached\n");
235 /* if not timeout finish request here */
236 if (!(request->flags & ATA_R_TIMEOUT))
239 device_delete_child(dev, children[i]);
242 free(children, M_TEMP);
243 mtx_unlock(&Giant); /* newbus suckage dealt with, release Giant */
246 /* catch request in ch->running if we havn't already */
247 mtx_lock(&ch->state_mtx);
248 if ((request = ch->running))
249 callout_stop(&request->callout);
251 mtx_unlock(&ch->state_mtx);
253 /* if we got one put it on the queue again */
255 device_printf(request->dev,
256 "WARNING - %s requeued due to channel reset",
257 ata_cmd2str(request));
258 if (!(request->flags & (ATA_R_ATAPI | ATA_R_CONTROL)))
259 printf(" LBA=%llu", (unsigned long long)request->u.ata.lba);
261 request->flags |= ATA_R_REQUEUE;
262 ata_queue_request(request);
265 /* we're done release the channel for new work */
266 mtx_lock(&ch->state_mtx);
267 ch->state = ATA_IDLE;
268 mtx_unlock(&ch->state_mtx);
269 ATA_LOCKING(dev, ATA_LF_UNLOCK);
272 device_printf(dev, "reinit done ..\n");
274 /* kick off requests on the queue */
280 ata_suspend(device_t dev)
282 struct ata_channel *ch;
284 /* check for valid device */
285 if (!dev || !(ch = device_get_softc(dev)))
288 /* wait for the channel to be IDLE before entering suspend mode */
290 mtx_lock(&ch->state_mtx);
291 if (ch->state == ATA_IDLE) {
292 ch->state = ATA_ACTIVE;
293 mtx_unlock(&ch->state_mtx);
296 mtx_unlock(&ch->state_mtx);
297 tsleep(ch, PRIBIO, "atasusp", hz/10);
299 ATA_LOCKING(dev, ATA_LF_UNLOCK);
304 ata_resume(device_t dev)
306 struct ata_channel *ch;
309 /* check for valid device */
310 if (!dev || !(ch = device_get_softc(dev)))
313 /* reinit the devices, we dont know what mode/state they are in */
314 error = ata_reinit(dev);
316 /* kick off requests on the queue */
322 ata_interrupt(void *data)
324 struct ata_channel *ch = (struct ata_channel *)data;
325 struct ata_request *request;
327 mtx_lock(&ch->state_mtx);
329 /* do we have a running request */
330 if (!(request = ch->running))
333 ATA_DEBUG_RQ(request, "interrupt");
335 /* ignore interrupt if device is busy */
336 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) {
338 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY)
342 /* check for the right state */
343 if (ch->state != ATA_ACTIVE && ch->state != ATA_STALL_QUEUE) {
344 device_printf(request->dev, "interrupt on idle channel ignored\n");
349 * we have the HW locks, so end the tranaction for this request
350 * if it finishes immediately otherwise wait for next interrupt
352 if (ch->hw.end_transaction(request) == ATA_OP_FINISHED) {
354 if (ch->state == ATA_ACTIVE)
355 ch->state = ATA_IDLE;
356 mtx_unlock(&ch->state_mtx);
357 ATA_LOCKING(ch->dev, ATA_LF_UNLOCK);
362 mtx_unlock(&ch->state_mtx);
366 * device related interfaces
369 ata_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
370 int32_t flag, struct thread *td)
372 device_t device, *children;
373 struct ata_ioc_devices *devices = (struct ata_ioc_devices *)data;
374 int *value = (int *)data;
375 int i, nchildren, error = ENOTTY;
378 case IOCATAGMAXCHANNEL:
379 *value = devclass_get_maxunit(ata_devclass);
384 if (*value > devclass_get_maxunit(ata_devclass) ||
385 !(device = devclass_get_device(ata_devclass, *value)))
387 error = ata_reinit(device);
392 if (*value > devclass_get_maxunit(ata_devclass) ||
393 !(device = devclass_get_device(ata_devclass, *value)))
395 /* XXX SOS should enable channel HW on controller */
396 error = ata_attach(device);
400 if (*value > devclass_get_maxunit(ata_devclass) ||
401 !(device = devclass_get_device(ata_devclass, *value)))
403 error = ata_detach(device);
404 /* XXX SOS should disable channel HW on controller */
408 if (devices->channel > devclass_get_maxunit(ata_devclass) ||
409 !(device = devclass_get_device(ata_devclass, devices->channel)))
411 bzero(devices->name[0], 32);
412 bzero(&devices->params[0], sizeof(struct ata_params));
413 bzero(devices->name[1], 32);
414 bzero(&devices->params[1], sizeof(struct ata_params));
415 if (!device_get_children(device, &children, &nchildren)) {
416 for (i = 0; i < nchildren; i++) {
417 if (children[i] && device_is_attached(children[i])) {
418 struct ata_device *atadev = device_get_softc(children[i]);
420 if (atadev->unit == ATA_MASTER) {
421 strncpy(devices->name[0],
422 device_get_nameunit(children[i]), 32);
423 bcopy(&atadev->param, &devices->params[0],
424 sizeof(struct ata_params));
426 if (atadev->unit == ATA_SLAVE) {
427 strncpy(devices->name[1],
428 device_get_nameunit(children[i]), 32);
429 bcopy(&atadev->param, &devices->params[1],
430 sizeof(struct ata_params));
434 free(children, M_TEMP);
442 if (ata_raid_ioctl_func)
443 error = ata_raid_ioctl_func(cmd, data);
449 ata_device_ioctl(device_t dev, u_long cmd, caddr_t data)
451 struct ata_device *atadev = device_get_softc(dev);
452 struct ata_ioc_request *ioc_request = (struct ata_ioc_request *)data;
453 struct ata_params *params = (struct ata_params *)data;
454 int *mode = (int *)data;
455 struct ata_request *request;
461 if (!(buf = malloc(ioc_request->count, M_ATA, M_NOWAIT))) {
464 if (!(request = ata_alloc_request())) {
468 if (ioc_request->flags & ATA_CMD_WRITE) {
469 error = copyin(ioc_request->data, buf, ioc_request->count);
472 ata_free_request(request);
477 if (ioc_request->flags & ATA_CMD_ATAPI) {
478 request->flags = ATA_R_ATAPI;
479 bcopy(ioc_request->u.atapi.ccb, request->u.atapi.ccb, 16);
482 request->u.ata.command = ioc_request->u.ata.command;
483 request->u.ata.feature = ioc_request->u.ata.feature;
484 request->u.ata.lba = ioc_request->u.ata.lba;
485 request->u.ata.count = ioc_request->u.ata.count;
487 request->timeout = ioc_request->timeout;
489 request->bytecount = ioc_request->count;
490 request->transfersize = request->bytecount;
491 if (ioc_request->flags & ATA_CMD_CONTROL)
492 request->flags |= ATA_R_CONTROL;
493 if (ioc_request->flags & ATA_CMD_READ)
494 request->flags |= ATA_R_READ;
495 if (ioc_request->flags & ATA_CMD_WRITE)
496 request->flags |= ATA_R_WRITE;
497 ata_queue_request(request);
498 if (!(request->flags & ATA_R_ATAPI)) {
499 ioc_request->u.ata.command = request->u.ata.command;
500 ioc_request->u.ata.feature = request->u.ata.feature;
501 ioc_request->u.ata.lba = request->u.ata.lba;
502 ioc_request->u.ata.count = request->u.ata.count;
504 ioc_request->error = request->result;
505 if (ioc_request->flags & ATA_CMD_READ)
506 error = copyout(buf, ioc_request->data, ioc_request->count);
510 ata_free_request(request);
514 bcopy(&atadev->param, params, sizeof(struct ata_params));
518 atadev->mode = *mode;
519 ATA_SETMODE(device_get_parent(dev), dev);
523 *mode = atadev->mode;
531 ata_boot_attach(void)
533 struct ata_channel *ch;
536 mtx_lock(&Giant); /* newbus suckage it needs Giant */
538 /* kick of probe and attach on all channels */
539 for (ctlr = 0; ctlr < devclass_get_maxunit(ata_devclass); ctlr++) {
540 if ((ch = devclass_get_softc(ata_devclass, ctlr))) {
541 ata_identify(ch->dev);
545 /* release the hook that got us here, we are only needed once during boot */
546 if (ata_delayed_attach) {
547 config_intrhook_disestablish(ata_delayed_attach);
548 ata_delayed_attach = NULL;
549 free(ata_delayed_attach, M_TEMP);
552 mtx_unlock(&Giant); /* newbus suckage dealt with, release Giant */
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(device_t parent, struct ata_device *atadev)
577 struct ata_channel *ch = device_get_softc(parent);
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;
612 #if BYTE_ORDER == BIG_ENDIAN
615 for (ptr = (int16_t *)atacap;
616 ptr < (int16_t *)atacap + sizeof(struct ata_params)/2; ptr++) {
617 *ptr = bswap16(*ptr);
620 if (!(!strncmp(atacap->model, "FX", 2) ||
621 !strncmp(atacap->model, "NEC", 3) ||
622 !strncmp(atacap->model, "Pioneer", 7) ||
623 !strncmp(atacap->model, "SHARP", 5))) {
624 bswap(atacap->model, sizeof(atacap->model));
625 bswap(atacap->revision, sizeof(atacap->revision));
626 bswap(atacap->serial, sizeof(atacap->serial));
628 btrim(atacap->model, sizeof(atacap->model));
629 bpack(atacap->model, atacap->model, sizeof(atacap->model));
630 btrim(atacap->revision, sizeof(atacap->revision));
631 bpack(atacap->revision, atacap->revision, sizeof(atacap->revision));
632 btrim(atacap->serial, sizeof(atacap->serial));
633 bpack(atacap->serial, atacap->serial, sizeof(atacap->serial));
634 sprintf(buffer, "%.40s/%.8s", atacap->model, atacap->revision);
635 device_set_desc_copy(atadev->dev, buffer);
637 printf("ata%d-%s: pio=%s wdma=%s udma=%s cable=%s wire\n",
638 ch->unit, atadev->unit == ATA_MASTER ? "master":"slave",
639 ata_mode2str(ata_pmode(atacap)),
640 ata_mode2str(ata_wmode(atacap)),
641 ata_mode2str(ata_umode(atacap)),
642 (atacap->hwres & ATA_CABLE_ID) ? "80":"40");
644 if (atadev->param.config & ATA_PROTO_ATAPI) {
645 if (atapi_dma && ch->dma &&
646 (atadev->param.config & ATA_DRQ_MASK) != ATA_DRQ_INTR &&
647 ata_umode(&atadev->param) >= ATA_UDMA2)
648 atadev->mode = ATA_DMA_MAX;
651 if (ata_dma && ch->dma &&
652 (ata_umode(&atadev->param) > 0 ||
653 ata_wmode(&atadev->param) > 0))
654 atadev->mode = ATA_DMA_MAX;
665 ata_identify(device_t dev)
667 struct ata_channel *ch = device_get_softc(dev);
668 struct ata_device *master = NULL, *slave = NULL;
669 device_t master_child = NULL, slave_child = NULL;
670 int master_unit = -1, slave_unit = -1;
672 if (ch->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER)) {
673 if (!(master = malloc(sizeof(struct ata_device),
674 M_ATA, M_NOWAIT | M_ZERO))) {
675 device_printf(dev, "out of memory\n");
678 master->unit = ATA_MASTER;
680 if (ch->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE)) {
681 if (!(slave = malloc(sizeof(struct ata_device),
682 M_ATA, M_NOWAIT | M_ZERO))) {
684 device_printf(dev, "out of memory\n");
687 slave->unit = ATA_SLAVE;
691 if (ch->devices & ATA_ATA_MASTER)
692 master_unit = (device_get_unit(dev) << 1);
694 if (master && !(master_child = ata_add_child(dev, master, master_unit))) {
699 if (ch->devices & ATA_ATA_SLAVE)
700 slave_unit = (device_get_unit(dev) << 1) + 1;
702 if (slave && !(slave_child = ata_add_child(dev, slave, slave_unit))) {
707 if (slave && ata_getparam(dev, slave)) {
708 device_delete_child(dev, slave_child);
711 if (master && ata_getparam(dev, master)) {
712 device_delete_child(dev, master_child);
716 bus_generic_probe(dev);
717 bus_generic_attach(dev);
722 ata_default_registers(device_t dev)
724 struct ata_channel *ch = device_get_softc(dev);
726 /* fill in the defaults from whats setup already */
727 ch->r_io[ATA_ERROR].res = ch->r_io[ATA_FEATURE].res;
728 ch->r_io[ATA_ERROR].offset = ch->r_io[ATA_FEATURE].offset;
729 ch->r_io[ATA_IREASON].res = ch->r_io[ATA_COUNT].res;
730 ch->r_io[ATA_IREASON].offset = ch->r_io[ATA_COUNT].offset;
731 ch->r_io[ATA_STATUS].res = ch->r_io[ATA_COMMAND].res;
732 ch->r_io[ATA_STATUS].offset = ch->r_io[ATA_COMMAND].offset;
733 ch->r_io[ATA_ALTSTAT].res = ch->r_io[ATA_CONTROL].res;
734 ch->r_io[ATA_ALTSTAT].offset = ch->r_io[ATA_CONTROL].offset;
738 ata_modify_if_48bit(struct ata_request *request)
740 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
741 struct ata_device *atadev = device_get_softc(request->dev);
743 atadev->flags &= ~ATA_D_48BIT_ACTIVE;
745 if ((request->u.ata.lba >= ATA_MAX_28BIT_LBA ||
746 request->u.ata.count > 256) &&
747 atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) {
749 /* translate command into 48bit version */
750 switch (request->u.ata.command) {
752 request->u.ata.command = ATA_READ48;
755 request->u.ata.command = ATA_READ_MUL48;
758 if (ch->flags & ATA_NO_48BIT_DMA) {
759 if (request->transfersize > DEV_BSIZE)
760 request->u.ata.command = ATA_READ_MUL48;
762 request->u.ata.command = ATA_READ48;
763 request->flags &= ~ATA_R_DMA;
766 request->u.ata.command = ATA_READ_DMA48;
768 case ATA_READ_DMA_QUEUED:
769 if (ch->flags & ATA_NO_48BIT_DMA) {
770 if (request->transfersize > DEV_BSIZE)
771 request->u.ata.command = ATA_READ_MUL48;
773 request->u.ata.command = ATA_READ48;
774 request->flags &= ~ATA_R_DMA;
777 request->u.ata.command = ATA_READ_DMA_QUEUED48;
780 request->u.ata.command = ATA_WRITE48;
783 request->u.ata.command = ATA_WRITE_MUL48;
786 if (ch->flags & ATA_NO_48BIT_DMA) {
787 if (request->transfersize > DEV_BSIZE)
788 request->u.ata.command = ATA_WRITE_MUL48;
790 request->u.ata.command = ATA_WRITE48;
791 request->flags &= ~ATA_R_DMA;
794 request->u.ata.command = ATA_WRITE_DMA48;
796 case ATA_WRITE_DMA_QUEUED:
797 if (ch->flags & ATA_NO_48BIT_DMA) {
798 if (request->transfersize > DEV_BSIZE)
799 request->u.ata.command = ATA_WRITE_MUL48;
801 request->u.ata.command = ATA_WRITE48;
802 request->u.ata.command = ATA_WRITE48;
803 request->flags &= ~ATA_R_DMA;
806 request->u.ata.command = ATA_WRITE_DMA_QUEUED48;
809 request->u.ata.command = ATA_FLUSHCACHE48;
814 atadev->flags |= ATA_D_48BIT_ACTIVE;
819 ata_udelay(int interval)
821 /* for now just use DELAY, the timer/sleep subsytems are not there yet */
822 if (1 || interval < (1000000/hz) || ata_delayed_attach)
825 tsleep(&interval, PRIBIO, "ataslp", interval/(1000000/hz));
829 ata_mode2str(int mode)
832 case -1: return "UNSUPPORTED";
833 case ATA_PIO0: return "PIO0";
834 case ATA_PIO1: return "PIO1";
835 case ATA_PIO2: return "PIO2";
836 case ATA_PIO3: return "PIO3";
837 case ATA_PIO4: return "PIO4";
838 case ATA_WDMA0: return "WDMA0";
839 case ATA_WDMA1: return "WDMA1";
840 case ATA_WDMA2: return "WDMA2";
841 case ATA_UDMA0: return "UDMA16";
842 case ATA_UDMA1: return "UDMA25";
843 case ATA_UDMA2: return "UDMA33";
844 case ATA_UDMA3: return "UDMA40";
845 case ATA_UDMA4: return "UDMA66";
846 case ATA_UDMA5: return "UDMA100";
847 case ATA_UDMA6: return "UDMA133";
848 case ATA_SA150: return "SATA150";
849 case ATA_SA300: return "SATA300";
851 if (mode & ATA_DMA_MASK)
859 ata_pmode(struct ata_params *ap)
861 if (ap->atavalid & ATA_FLAG_64_70) {
862 if (ap->apiomodes & 0x02)
864 if (ap->apiomodes & 0x01)
867 if (ap->mwdmamodes & 0x04)
869 if (ap->mwdmamodes & 0x02)
871 if (ap->mwdmamodes & 0x01)
873 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200)
875 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100)
877 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000)
883 ata_wmode(struct ata_params *ap)
885 if (ap->mwdmamodes & 0x04)
887 if (ap->mwdmamodes & 0x02)
889 if (ap->mwdmamodes & 0x01)
895 ata_umode(struct ata_params *ap)
897 if (ap->atavalid & ATA_FLAG_88) {
898 if (ap->udmamodes & 0x40)
900 if (ap->udmamodes & 0x20)
902 if (ap->udmamodes & 0x10)
904 if (ap->udmamodes & 0x08)
906 if (ap->udmamodes & 0x04)
908 if (ap->udmamodes & 0x02)
910 if (ap->udmamodes & 0x01)
917 ata_limit_mode(device_t dev, int mode, int maxmode)
919 struct ata_device *atadev = device_get_softc(dev);
921 if (maxmode && mode > maxmode)
924 if (mode >= ATA_UDMA0 && ata_umode(&atadev->param) > 0)
925 return min(mode, ata_umode(&atadev->param));
927 if (mode >= ATA_WDMA0 && ata_wmode(&atadev->param) > 0)
928 return min(mode, ata_wmode(&atadev->param));
930 if (mode > ata_pmode(&atadev->param))
931 return min(mode, ata_pmode(&atadev->param));
937 bswap(int8_t *buf, int len)
939 u_int16_t *ptr = (u_int16_t*)(buf + len);
941 while (--ptr >= (u_int16_t*)buf)
946 btrim(int8_t *buf, int len)
950 for (ptr = buf; ptr < buf+len; ++ptr)
951 if (!*ptr || *ptr == '_')
953 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
958 bpack(int8_t *src, int8_t *dst, int len)
962 for (i = j = blank = 0 ; i < len; i++) {
963 if (blank && src[i] == ' ') continue;
964 if (blank && src[i] != ' ') {
985 ata_module_event_handler(module_t mod, int what, void *arg)
987 static struct cdev *atacdev;
991 /* register controlling device */
992 atacdev = make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "ata");
995 /* register boot attach to be run when interrupts are enabled */
996 if (!(ata_delayed_attach = (struct intr_config_hook *)
997 malloc(sizeof(struct intr_config_hook),
998 M_TEMP, M_NOWAIT | M_ZERO))) {
999 printf("ata: malloc of delayed attach hook failed\n");
1002 ata_delayed_attach->ich_func = (void*)ata_boot_attach;
1003 if (config_intrhook_establish(ata_delayed_attach) != 0) {
1004 printf("ata: config_intrhook_establish failed\n");
1005 free(ata_delayed_attach, M_TEMP);
1011 /* deregister controlling device */
1012 destroy_dev(atacdev);
1020 static moduledata_t ata_moduledata = { "ata", ata_module_event_handler, NULL };
1021 DECLARE_MODULE(ata, ata_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
1022 MODULE_VERSION(ata, 1);
1027 ata_request_zone = uma_zcreate("ata_request", sizeof(struct ata_request),
1028 NULL, NULL, NULL, NULL, 0, 0);
1029 ata_composite_zone = uma_zcreate("ata_composite",
1030 sizeof(struct ata_composite),
1031 NULL, NULL, NULL, NULL, 0, 0);
1033 SYSINIT(ata_register, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL);
1038 uma_zdestroy(ata_composite_zone);
1039 uma_zdestroy(ata_request_zone);
1041 SYSUNINIT(ata_unregister, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_uninit, NULL);