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>
51 #include <dev/pci/pcivar.h>
56 #include <cam/cam_ccb.h>
57 #include <cam/cam_sim.h>
58 #include <cam/cam_xpt_sim.h>
59 #include <cam/cam_debug.h>
63 /* device structure */
64 static d_ioctl_t ata_ioctl;
65 static struct cdevsw ata_cdevsw = {
66 .d_version = D_VERSION,
67 .d_flags = D_NEEDGIANT, /* we need this as newbus isn't mpsafe */
75 static void bswap(int8_t *, int);
76 static void btrim(int8_t *, int);
77 static void bpack(int8_t *, int8_t *, int);
78 static void ata_boot_attach(void);
79 static device_t ata_add_child(device_t, struct ata_device *, int);
81 static void ataaction(struct cam_sim *sim, union ccb *ccb);
82 static void atapoll(struct cam_sim *sim);
83 static void ata_cam_begin_transaction(device_t dev, union ccb *ccb);
84 static void ata_cam_end_transaction(device_t dev, struct ata_request *request);
85 static void ata_cam_request_sense(device_t dev, struct ata_request *request);
86 static int ata_check_ids(device_t dev, union ccb *ccb);
87 static void ata_periodic_poll(void *data);
89 static void ata_conn_event(void *, int);
90 static void ata_init(void);
91 static void ata_interrupt_locked(void *data);
92 static int ata_module_event_handler(module_t mod, int what, void *arg);
93 static int ata_str2mode(const char *str);
94 static void ata_uninit(void);
97 MALLOC_DEFINE(M_ATA, "ata_generic", "ATA driver generic layer");
98 int (*ata_raid_ioctl_func)(u_long cmd, caddr_t data) = NULL;
100 struct intr_config_hook *ata_delayed_attach = NULL;
102 devclass_t ata_devclass;
103 uma_zone_t ata_request_zone;
104 uma_zone_t ata_composite_zone;
109 int ata_dma_check_80pin = 1;
113 static int ata_dma = 1;
114 static int atapi_dma = 1;
118 static SYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters");
120 TUNABLE_INT("hw.ata.ata_dma", &ata_dma);
121 SYSCTL_INT(_hw_ata, OID_AUTO, ata_dma, CTLFLAG_RDTUN, &ata_dma, 0,
122 "ATA disk DMA mode control");
124 TUNABLE_INT("hw.ata.ata_dma_check_80pin", &ata_dma_check_80pin);
125 SYSCTL_INT(_hw_ata, OID_AUTO, ata_dma_check_80pin,
126 CTLFLAG_RW, &ata_dma_check_80pin, 1,
127 "Check for 80pin cable before setting ATA DMA mode");
129 TUNABLE_INT("hw.ata.atapi_dma", &atapi_dma);
130 SYSCTL_INT(_hw_ata, OID_AUTO, atapi_dma, CTLFLAG_RDTUN, &atapi_dma, 0,
131 "ATAPI device DMA mode control");
132 TUNABLE_INT("hw.ata.wc", &ata_wc);
133 SYSCTL_INT(_hw_ata, OID_AUTO, wc, CTLFLAG_RDTUN, &ata_wc, 0,
134 "ATA disk write caching");
135 TUNABLE_INT("hw.ata.setmax", &ata_setmax);
136 SYSCTL_INT(_hw_ata, OID_AUTO, setmax, CTLFLAG_RDTUN, &ata_setmax, 0,
137 "ATA disk set max native address");
140 FEATURE(ata_cam, "ATA devices are accessed through the cam(4) driver");
144 * newbus device interface related functions
147 ata_probe(device_t dev)
153 ata_attach(device_t dev)
155 struct ata_channel *ch = device_get_softc(dev);
158 struct cam_devq *devq;
164 /* check that we have a virgin channel to attach */
168 /* initialize the softc basics */
170 ch->state = ATA_IDLE;
171 bzero(&ch->state_mtx, sizeof(struct mtx));
172 mtx_init(&ch->state_mtx, "ATA state lock", NULL, MTX_DEF);
174 bzero(&ch->queue_mtx, sizeof(struct mtx));
175 mtx_init(&ch->queue_mtx, "ATA queue lock", NULL, MTX_DEF);
176 TAILQ_INIT(&ch->ata_queue);
178 TASK_INIT(&ch->conntask, 0, ata_conn_event, dev);
180 for (i = 0; i < 16; i++) {
181 ch->user[i].revision = 0;
182 snprintf(buf, sizeof(buf), "dev%d.sata_rev", i);
183 if (resource_int_value(device_get_name(dev),
184 device_get_unit(dev), buf, &mode) != 0 &&
185 resource_int_value(device_get_name(dev),
186 device_get_unit(dev), "sata_rev", &mode) != 0)
189 ch->user[i].revision = mode;
190 ch->user[i].mode = 0;
191 snprintf(buf, sizeof(buf), "dev%d.mode", i);
192 if (resource_string_value(device_get_name(dev),
193 device_get_unit(dev), buf, &res) == 0)
194 mode = ata_str2mode(res);
195 else if (resource_string_value(device_get_name(dev),
196 device_get_unit(dev), "mode", &res) == 0)
197 mode = ata_str2mode(res);
201 ch->user[i].mode = mode;
202 if (ch->flags & ATA_SATA)
203 ch->user[i].bytecount = 8192;
205 ch->user[i].bytecount = MAXPHYS;
206 ch->user[i].caps = 0;
207 ch->curr[i] = ch->user[i];
208 if (ch->flags & ATA_SATA) {
209 if (ch->pm_level > 0)
210 ch->user[i].caps |= CTS_SATA_CAPS_H_PMREQ;
211 if (ch->pm_level > 1)
212 ch->user[i].caps |= CTS_SATA_CAPS_D_PMREQ;
214 if (!(ch->flags & ATA_NO_48BIT_DMA))
215 ch->user[i].caps |= CTS_ATA_CAPS_H_DMA48;
218 callout_init(&ch->poll_callout, 1);
222 /* reset the controller HW, the channel and device(s) */
223 while (ATA_LOCKING(dev, ATA_LF_LOCK) != ch->unit)
226 ATA_LOCKING(dev, ATA_LF_UNLOCK);
229 /* allocate DMA resources if DMA HW present*/
233 /* setup interrupt delivery */
235 ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
236 RF_SHAREABLE | RF_ACTIVE);
238 device_printf(dev, "unable to allocate interrupt\n");
241 if ((error = bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS, NULL,
242 ata_interrupt, ch, &ch->ih))) {
243 bus_release_resource(dev, SYS_RES_IRQ, rid, ch->r_irq);
244 device_printf(dev, "unable to setup interrupt\n");
249 /* probe and attach devices on this channel unless we are in early boot */
250 if (!ata_delayed_attach)
254 if (ch->flags & ATA_PERIODIC_POLL)
255 callout_reset(&ch->poll_callout, hz, ata_periodic_poll, ch);
256 mtx_lock(&ch->state_mtx);
257 /* Create the device queue for our SIM. */
258 devq = cam_simq_alloc(1);
260 device_printf(dev, "Unable to allocate simq\n");
264 /* Construct SIM entry */
265 ch->sim = cam_sim_alloc(ataaction, atapoll, "ata", ch,
266 device_get_unit(dev), &ch->state_mtx, 1, 0, devq);
267 if (ch->sim == NULL) {
268 device_printf(dev, "unable to allocate sim\n");
273 if (xpt_bus_register(ch->sim, dev, 0) != CAM_SUCCESS) {
274 device_printf(dev, "unable to register xpt bus\n");
278 if (xpt_create_path(&ch->path, /*periph*/NULL, cam_sim_path(ch->sim),
279 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
280 device_printf(dev, "unable to create path\n");
284 mtx_unlock(&ch->state_mtx);
288 xpt_bus_deregister(cam_sim_path(ch->sim));
290 cam_sim_free(ch->sim, /*free_devq*/TRUE);
293 bus_release_resource(dev, SYS_RES_IRQ, rid, ch->r_irq);
294 mtx_unlock(&ch->state_mtx);
295 if (ch->flags & ATA_PERIODIC_POLL)
296 callout_drain(&ch->poll_callout);
302 ata_detach(device_t dev)
304 struct ata_channel *ch = device_get_softc(dev);
310 /* check that we have a valid channel to detach */
314 /* grap the channel lock so no new requests gets launched */
315 mtx_lock(&ch->state_mtx);
316 ch->state |= ATA_STALL_QUEUE;
317 mtx_unlock(&ch->state_mtx);
319 if (ch->flags & ATA_PERIODIC_POLL)
320 callout_drain(&ch->poll_callout);
324 /* detach & delete all children */
325 if (!device_get_children(dev, &children, &nchildren)) {
326 for (i = 0; i < nchildren; i++)
328 device_delete_child(dev, children[i]);
329 free(children, M_TEMP);
332 taskqueue_drain(taskqueue_thread, &ch->conntask);
335 mtx_lock(&ch->state_mtx);
336 xpt_async(AC_LOST_DEVICE, ch->path, NULL);
337 xpt_free_path(ch->path);
338 xpt_bus_deregister(cam_sim_path(ch->sim));
339 cam_sim_free(ch->sim, /*free_devq*/TRUE);
341 mtx_unlock(&ch->state_mtx);
344 /* release resources */
345 bus_teardown_intr(dev, ch->r_irq, ch->ih);
346 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
349 /* free DMA resources if DMA HW present*/
353 mtx_destroy(&ch->state_mtx);
355 mtx_destroy(&ch->queue_mtx);
361 ata_conn_event(void *context, int dummy)
363 device_t dev = (device_t)context;
365 struct ata_channel *ch = device_get_softc(dev);
368 mtx_lock(&ch->state_mtx);
369 if (ch->sim == NULL) {
370 mtx_unlock(&ch->state_mtx);
374 if ((ccb = xpt_alloc_ccb_nowait()) == NULL)
376 if (xpt_create_path(&ccb->ccb_h.path, NULL,
377 cam_sim_path(ch->sim),
378 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
383 mtx_unlock(&ch->state_mtx);
390 ata_reinit(device_t dev)
392 struct ata_channel *ch = device_get_softc(dev);
393 struct ata_request *request;
398 /* check that we have a valid channel to reinit */
399 if (!ch || !ch->r_irq)
403 device_printf(dev, "reiniting channel ..\n");
405 /* poll for locking the channel */
406 while (ATA_LOCKING(dev, ATA_LF_LOCK) != ch->unit)
409 /* catch eventual request in ch->running */
410 mtx_lock(&ch->state_mtx);
411 if (ch->state & ATA_STALL_QUEUE) {
412 /* Recursive reinits and reinits during detach prohobited. */
413 mtx_unlock(&ch->state_mtx);
416 if ((request = ch->running))
417 callout_stop(&request->callout);
420 /* unconditionally grap the channel lock */
421 ch->state |= ATA_STALL_QUEUE;
422 mtx_unlock(&ch->state_mtx);
424 /* reset the controller HW, the channel and device(s) */
427 /* reinit the children and delete any that fails */
428 if (!device_get_children(dev, &children, &nchildren)) {
429 mtx_lock(&Giant); /* newbus suckage it needs Giant */
430 for (i = 0; i < nchildren; i++) {
431 /* did any children go missing ? */
432 if (children[i] && device_is_attached(children[i]) &&
433 ATA_REINIT(children[i])) {
435 * if we had a running request and its device matches
436 * this child we need to inform the request that the
439 if (request && request->dev == children[i]) {
440 request->result = ENXIO;
441 device_printf(request->dev, "FAILURE - device detached\n");
443 /* if not timeout finish request here */
444 if (!(request->flags & ATA_R_TIMEOUT))
448 device_delete_child(dev, children[i]);
451 free(children, M_TEMP);
452 mtx_unlock(&Giant); /* newbus suckage dealt with, release Giant */
455 /* if we still have a good request put it on the queue again */
456 if (request && !(request->flags & ATA_R_TIMEOUT)) {
457 device_printf(request->dev,
458 "WARNING - %s requeued due to channel reset",
459 ata_cmd2str(request));
460 if (!(request->flags & (ATA_R_ATAPI | ATA_R_CONTROL)))
461 printf(" LBA=%ju", request->u.ata.lba);
463 request->flags |= ATA_R_REQUEUE;
464 ata_queue_request(request);
467 /* we're done release the channel for new work */
468 mtx_lock(&ch->state_mtx);
469 ch->state = ATA_IDLE;
470 mtx_unlock(&ch->state_mtx);
471 ATA_LOCKING(dev, ATA_LF_UNLOCK);
473 /* Add new children. */
474 /* ata_identify(dev); */
477 device_printf(dev, "reinit done ..\n");
479 /* kick off requests on the queue */
482 xpt_freeze_simq(ch->sim, 1);
483 if ((request = ch->running)) {
485 if (ch->state == ATA_ACTIVE)
486 ch->state = ATA_IDLE;
487 callout_stop(&request->callout);
489 ch->dma.unload(request);
490 request->result = ERESTART;
491 ata_cam_end_transaction(dev, request);
493 /* reset the controller HW, the channel and device(s) */
495 /* Tell the XPT about the event */
496 xpt_async(AC_BUS_RESET, ch->path, NULL);
497 xpt_release_simq(ch->sim, TRUE);
503 ata_suspend(device_t dev)
505 struct ata_channel *ch;
507 /* check for valid device */
508 if (!dev || !(ch = device_get_softc(dev)))
512 if (ch->flags & ATA_PERIODIC_POLL)
513 callout_drain(&ch->poll_callout);
514 mtx_lock(&ch->state_mtx);
515 xpt_freeze_simq(ch->sim, 1);
516 while (ch->state != ATA_IDLE)
517 msleep(ch, &ch->state_mtx, PRIBIO, "atasusp", hz/100);
518 mtx_unlock(&ch->state_mtx);
520 /* wait for the channel to be IDLE or detached before suspending */
522 mtx_lock(&ch->state_mtx);
523 if (ch->state == ATA_IDLE) {
524 ch->state = ATA_ACTIVE;
525 mtx_unlock(&ch->state_mtx);
528 mtx_unlock(&ch->state_mtx);
529 tsleep(ch, PRIBIO, "atasusp", hz/10);
531 ATA_LOCKING(dev, ATA_LF_UNLOCK);
537 ata_resume(device_t dev)
539 struct ata_channel *ch;
542 /* check for valid device */
543 if (!dev || !(ch = device_get_softc(dev)))
547 mtx_lock(&ch->state_mtx);
548 error = ata_reinit(dev);
549 xpt_release_simq(ch->sim, TRUE);
550 mtx_unlock(&ch->state_mtx);
551 if (ch->flags & ATA_PERIODIC_POLL)
552 callout_reset(&ch->poll_callout, hz, ata_periodic_poll, ch);
554 /* reinit the devices, we dont know what mode/state they are in */
555 error = ata_reinit(dev);
556 /* kick off requests on the queue */
563 ata_interrupt(void *data)
566 struct ata_channel *ch = (struct ata_channel *)data;
568 mtx_lock(&ch->state_mtx);
569 xpt_batch_start(ch->sim);
571 ata_interrupt_locked(data);
573 xpt_batch_done(ch->sim);
574 mtx_unlock(&ch->state_mtx);
579 ata_interrupt_locked(void *data)
581 struct ata_channel *ch = (struct ata_channel *)data;
582 struct ata_request *request;
585 mtx_lock(&ch->state_mtx);
588 /* ignore interrupt if its not for us */
589 if (ch->hw.status && !ch->hw.status(ch->dev))
592 /* do we have a running request */
593 if (!(request = ch->running))
596 ATA_DEBUG_RQ(request, "interrupt");
598 /* safetycheck for the right state */
599 if (ch->state == ATA_IDLE) {
600 device_printf(request->dev, "interrupt on idle channel ignored\n");
605 * we have the HW locks, so end the transaction for this request
606 * if it finishes immediately otherwise wait for next interrupt
608 if (ch->hw.end_transaction(request) == ATA_OP_FINISHED) {
610 if (ch->state == ATA_ACTIVE)
611 ch->state = ATA_IDLE;
613 ata_cam_end_transaction(ch->dev, request);
615 mtx_unlock(&ch->state_mtx);
616 ATA_LOCKING(ch->dev, ATA_LF_UNLOCK);
623 mtx_unlock(&ch->state_mtx);
629 ata_periodic_poll(void *data)
631 struct ata_channel *ch = (struct ata_channel *)data;
633 callout_reset(&ch->poll_callout, hz, ata_periodic_poll, ch);
639 ata_print_cable(device_t dev, u_int8_t *who)
642 "DMA limited to UDMA33, %s found non-ATA66 cable\n", who);
647 ata_check_80pin(device_t dev, int mode)
649 struct ata_device *atadev = device_get_softc(dev);
651 if (!ata_dma_check_80pin) {
653 device_printf(dev, "Skipping 80pin cable check\n");
657 if (mode > ATA_UDMA2 && !(atadev->param.hwres & ATA_CABLE_ID)) {
658 ata_print_cable(dev, "device");
667 ata_setmode(device_t dev)
669 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
670 struct ata_device *atadev = device_get_softc(dev);
671 int error, mode, pmode;
675 pmode = mode = ata_limit_mode(dev, mode, ATA_DMA_MAX);
676 mode = ATA_SETMODE(device_get_parent(dev), atadev->unit, mode);
677 if ((ch->flags & (ATA_CHECKS_CABLE | ATA_SATA)) == 0)
678 mode = ata_check_80pin(dev, mode);
679 } while (pmode != mode); /* Interate till successfull negotiation. */
680 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
682 device_printf(dev, "%ssetting %s\n",
683 (error) ? "FAILURE " : "", ata_mode2str(mode));
689 * device related interfaces
693 ata_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
694 int32_t flag, struct thread *td)
696 device_t device, *children;
697 struct ata_ioc_devices *devices = (struct ata_ioc_devices *)data;
698 int *value = (int *)data;
699 int i, nchildren, error = ENOTTY;
702 case IOCATAGMAXCHANNEL:
703 /* In case we have channel 0..n this will return n+1. */
704 *value = devclass_get_maxunit(ata_devclass);
709 if (*value >= devclass_get_maxunit(ata_devclass) ||
710 !(device = devclass_get_device(ata_devclass, *value)) ||
711 !device_is_attached(device))
713 error = ata_reinit(device);
717 if (*value >= devclass_get_maxunit(ata_devclass) ||
718 !(device = devclass_get_device(ata_devclass, *value)) ||
719 !device_is_attached(device))
721 error = DEVICE_ATTACH(device);
725 if (*value >= devclass_get_maxunit(ata_devclass) ||
726 !(device = devclass_get_device(ata_devclass, *value)) ||
727 !device_is_attached(device))
729 error = DEVICE_DETACH(device);
733 if (devices->channel >= devclass_get_maxunit(ata_devclass) ||
734 !(device = devclass_get_device(ata_devclass, devices->channel)) ||
735 !device_is_attached(device))
737 bzero(devices->name[0], 32);
738 bzero(&devices->params[0], sizeof(struct ata_params));
739 bzero(devices->name[1], 32);
740 bzero(&devices->params[1], sizeof(struct ata_params));
741 if (!device_get_children(device, &children, &nchildren)) {
742 for (i = 0; i < nchildren; i++) {
743 if (children[i] && device_is_attached(children[i])) {
744 struct ata_device *atadev = device_get_softc(children[i]);
746 if (atadev->unit == ATA_MASTER) { /* XXX SOS PM */
747 strncpy(devices->name[0],
748 device_get_nameunit(children[i]), 32);
749 bcopy(&atadev->param, &devices->params[0],
750 sizeof(struct ata_params));
752 if (atadev->unit == ATA_SLAVE) { /* XXX SOS PM */
753 strncpy(devices->name[1],
754 device_get_nameunit(children[i]), 32);
755 bcopy(&atadev->param, &devices->params[1],
756 sizeof(struct ata_params));
760 free(children, M_TEMP);
768 if (ata_raid_ioctl_func)
769 error = ata_raid_ioctl_func(cmd, data);
777 ata_device_ioctl(device_t dev, u_long cmd, caddr_t data)
779 struct ata_device *atadev = device_get_softc(dev);
780 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
781 struct ata_ioc_request *ioc_request = (struct ata_ioc_request *)data;
782 struct ata_params *params = (struct ata_params *)data;
783 int *mode = (int *)data;
784 struct ata_request *request;
790 if (ioc_request->count >
791 (ch->dma.max_iosize ? ch->dma.max_iosize : DFLTPHYS)) {
794 if (!(buf = malloc(ioc_request->count, M_ATA, M_NOWAIT))) {
797 if (!(request = ata_alloc_request())) {
801 request->dev = atadev->dev;
802 if (ioc_request->flags & ATA_CMD_WRITE) {
803 error = copyin(ioc_request->data, buf, ioc_request->count);
806 ata_free_request(request);
810 if (ioc_request->flags & ATA_CMD_ATAPI) {
811 request->flags = ATA_R_ATAPI;
812 bcopy(ioc_request->u.atapi.ccb, request->u.atapi.ccb, 16);
815 request->u.ata.command = ioc_request->u.ata.command;
816 request->u.ata.feature = ioc_request->u.ata.feature;
817 request->u.ata.lba = ioc_request->u.ata.lba;
818 request->u.ata.count = ioc_request->u.ata.count;
820 request->timeout = ioc_request->timeout;
822 request->bytecount = ioc_request->count;
823 request->transfersize = request->bytecount;
824 if (ioc_request->flags & ATA_CMD_CONTROL)
825 request->flags |= ATA_R_CONTROL;
826 if (ioc_request->flags & ATA_CMD_READ)
827 request->flags |= ATA_R_READ;
828 if (ioc_request->flags & ATA_CMD_WRITE)
829 request->flags |= ATA_R_WRITE;
830 ata_queue_request(request);
831 if (request->flags & ATA_R_ATAPI) {
832 bcopy(&request->u.atapi.sense, &ioc_request->u.atapi.sense,
833 sizeof(struct atapi_sense));
836 ioc_request->u.ata.command = request->u.ata.command;
837 ioc_request->u.ata.feature = request->u.ata.feature;
838 ioc_request->u.ata.lba = request->u.ata.lba;
839 ioc_request->u.ata.count = request->u.ata.count;
841 ioc_request->error = request->result;
842 if (ioc_request->flags & ATA_CMD_READ)
843 error = copyout(buf, ioc_request->data, ioc_request->count);
847 ata_free_request(request);
851 ata_getparam(atadev, 0);
852 bcopy(&atadev->param, params, sizeof(struct ata_params));
856 atadev->mode = *mode;
861 *mode = atadev->mode |
862 (ATA_GETREV(device_get_parent(dev), atadev->unit) << 8);
864 case IOCATASSPINDOWN:
865 atadev->spindown = *mode;
867 case IOCATAGSPINDOWN:
868 *mode = atadev->spindown;
878 ata_boot_attach(void)
880 struct ata_channel *ch;
883 mtx_lock(&Giant); /* newbus suckage it needs Giant */
885 /* kick off probe and attach on all channels */
886 for (ctlr = 0; ctlr < devclass_get_maxunit(ata_devclass); ctlr++) {
887 if ((ch = devclass_get_softc(ata_devclass, ctlr))) {
888 ata_identify(ch->dev);
892 /* release the hook that got us here, we are only needed once during boot */
893 if (ata_delayed_attach) {
894 config_intrhook_disestablish(ata_delayed_attach);
895 free(ata_delayed_attach, M_TEMP);
896 ata_delayed_attach = NULL;
899 mtx_unlock(&Giant); /* newbus suckage dealt with, release Giant */
904 * misc support functions
908 ata_add_child(device_t parent, struct ata_device *atadev, int unit)
912 if ((child = device_add_child(parent, (unit < 0) ? NULL : "ad", unit))) {
913 device_set_softc(child, atadev);
916 atadev->max_iosize = DEV_BSIZE;
917 atadev->mode = ATA_PIO_MAX;
925 ata_getparam(struct ata_device *atadev, int init)
927 struct ata_channel *ch = device_get_softc(device_get_parent(atadev->dev));
928 struct ata_request *request;
931 u_int8_t command = 0;
932 int error = ENOMEM, retries = 2, mode = -1;
934 if (ch->devices & (ATA_ATA_MASTER << atadev->unit))
935 command = ATA_ATA_IDENTIFY;
936 if (ch->devices & (ATA_ATAPI_MASTER << atadev->unit))
937 command = ATA_ATAPI_IDENTIFY;
941 while (retries-- > 0 && error) {
942 if (!(request = ata_alloc_request()))
944 request->dev = atadev->dev;
945 request->timeout = 1;
946 request->retries = 0;
947 request->u.ata.command = command;
948 request->flags = (ATA_R_READ|ATA_R_AT_HEAD|ATA_R_DIRECT);
950 request->flags |= ATA_R_QUIET;
951 request->data = (void *)&atadev->param;
952 request->bytecount = sizeof(struct ata_params);
953 request->donecount = 0;
954 request->transfersize = DEV_BSIZE;
955 ata_queue_request(request);
956 error = request->result;
957 ata_free_request(request);
960 if (!error && (isprint(atadev->param.model[0]) ||
961 isprint(atadev->param.model[1]))) {
962 struct ata_params *atacap = &atadev->param;
965 for (ptr = (int16_t *)atacap;
966 ptr < (int16_t *)atacap + sizeof(struct ata_params)/2; ptr++) {
967 *ptr = le16toh(*ptr);
969 if (!(!strncmp(atacap->model, "FX", 2) ||
970 !strncmp(atacap->model, "NEC", 3) ||
971 !strncmp(atacap->model, "Pioneer", 7) ||
972 !strncmp(atacap->model, "SHARP", 5))) {
973 bswap(atacap->model, sizeof(atacap->model));
974 bswap(atacap->revision, sizeof(atacap->revision));
975 bswap(atacap->serial, sizeof(atacap->serial));
977 btrim(atacap->model, sizeof(atacap->model));
978 bpack(atacap->model, atacap->model, sizeof(atacap->model));
979 btrim(atacap->revision, sizeof(atacap->revision));
980 bpack(atacap->revision, atacap->revision, sizeof(atacap->revision));
981 btrim(atacap->serial, sizeof(atacap->serial));
982 bpack(atacap->serial, atacap->serial, sizeof(atacap->serial));
985 printf("ata%d-%s: pio=%s wdma=%s udma=%s cable=%s wire\n",
986 device_get_unit(ch->dev),
987 ata_unit2str(atadev),
988 ata_mode2str(ata_pmode(atacap)),
989 ata_mode2str(ata_wmode(atacap)),
990 ata_mode2str(ata_umode(atacap)),
991 (atacap->hwres & ATA_CABLE_ID) ? "80":"40");
996 sprintf(buffer, "%.40s/%.8s", atacap->model, atacap->revision);
997 device_set_desc_copy(atadev->dev, buffer);
998 if ((atadev->param.config & ATA_PROTO_ATAPI) &&
999 (atadev->param.config != ATA_CFA_MAGIC1) &&
1000 (atadev->param.config != ATA_CFA_MAGIC2)) {
1002 (atadev->param.config & ATA_DRQ_MASK) != ATA_DRQ_INTR &&
1003 ata_umode(&atadev->param) >= ATA_UDMA2)
1004 atadev->mode = ATA_DMA_MAX;
1008 (ata_umode(&atadev->param) > 0 ||
1009 ata_wmode(&atadev->param) > 0))
1010 atadev->mode = ATA_DMA_MAX;
1012 snprintf(buf, sizeof(buf), "dev%d.mode", atadev->unit);
1013 if (resource_string_value(device_get_name(ch->dev),
1014 device_get_unit(ch->dev), buf, &res) == 0)
1015 mode = ata_str2mode(res);
1016 else if (resource_string_value(device_get_name(ch->dev),
1017 device_get_unit(ch->dev), "mode", &res) == 0)
1018 mode = ata_str2mode(res);
1020 atadev->mode = mode;
1033 ata_identify(device_t dev)
1035 struct ata_channel *ch = device_get_softc(dev);
1036 struct ata_device *atadev;
1038 device_t child, master = NULL;
1039 int nchildren, i, n = ch->devices;
1042 device_printf(dev, "Identifying devices: %08x\n", ch->devices);
1045 /* Skip existing devices. */
1046 if (!device_get_children(dev, &children, &nchildren)) {
1047 for (i = 0; i < nchildren; i++) {
1048 if (children[i] && (atadev = device_get_softc(children[i])))
1049 n &= ~((ATA_ATA_MASTER | ATA_ATAPI_MASTER) << atadev->unit);
1051 free(children, M_TEMP);
1053 /* Create new devices. */
1055 device_printf(dev, "New devices: %08x\n", n);
1060 for (i = 0; i < ATA_PM; ++i) {
1061 if (n & (((ATA_ATA_MASTER | ATA_ATAPI_MASTER) << i))) {
1064 if (!(atadev = malloc(sizeof(struct ata_device),
1065 M_ATA, M_NOWAIT | M_ZERO))) {
1066 device_printf(dev, "out of memory\n");
1070 #ifdef ATA_STATIC_ID
1071 if (n & (ATA_ATA_MASTER << i))
1072 unit = (device_get_unit(dev) << 1) + i;
1074 if ((child = ata_add_child(dev, atadev, unit))) {
1076 * PATA slave should be identified first, to allow
1077 * device cable detection on master to work properly.
1079 if (i == 0 && (n & ATA_PORTMULTIPLIER) == 0 &&
1080 (n & ((ATA_ATA_MASTER | ATA_ATAPI_MASTER) << 1)) != 0) {
1084 if (ata_getparam(atadev, 1)) {
1085 device_delete_child(dev, child);
1086 free(atadev, M_ATA);
1090 free(atadev, M_ATA);
1094 atadev = device_get_softc(master);
1095 if (ata_getparam(atadev, 1)) {
1096 device_delete_child(dev, master);
1097 free(atadev, M_ATA);
1100 bus_generic_probe(dev);
1101 bus_generic_attach(dev);
1108 ata_default_registers(device_t dev)
1110 struct ata_channel *ch = device_get_softc(dev);
1112 /* fill in the defaults from whats setup already */
1113 ch->r_io[ATA_ERROR].res = ch->r_io[ATA_FEATURE].res;
1114 ch->r_io[ATA_ERROR].offset = ch->r_io[ATA_FEATURE].offset;
1115 ch->r_io[ATA_IREASON].res = ch->r_io[ATA_COUNT].res;
1116 ch->r_io[ATA_IREASON].offset = ch->r_io[ATA_COUNT].offset;
1117 ch->r_io[ATA_STATUS].res = ch->r_io[ATA_COMMAND].res;
1118 ch->r_io[ATA_STATUS].offset = ch->r_io[ATA_COMMAND].offset;
1119 ch->r_io[ATA_ALTSTAT].res = ch->r_io[ATA_CONTROL].res;
1120 ch->r_io[ATA_ALTSTAT].offset = ch->r_io[ATA_CONTROL].offset;
1125 ata_modify_if_48bit(struct ata_request *request)
1127 struct ata_channel *ch = device_get_softc(request->parent);
1128 struct ata_device *atadev = device_get_softc(request->dev);
1130 request->flags &= ~ATA_R_48BIT;
1132 if (((request->u.ata.lba + request->u.ata.count) >= ATA_MAX_28BIT_LBA ||
1133 request->u.ata.count > 256) &&
1134 atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) {
1136 /* translate command into 48bit version */
1137 switch (request->u.ata.command) {
1139 request->u.ata.command = ATA_READ48;
1142 request->u.ata.command = ATA_READ_MUL48;
1145 if (ch->flags & ATA_NO_48BIT_DMA) {
1146 if (request->transfersize > DEV_BSIZE)
1147 request->u.ata.command = ATA_READ_MUL48;
1149 request->u.ata.command = ATA_READ48;
1150 request->flags &= ~ATA_R_DMA;
1153 request->u.ata.command = ATA_READ_DMA48;
1155 case ATA_READ_DMA_QUEUED:
1156 if (ch->flags & ATA_NO_48BIT_DMA) {
1157 if (request->transfersize > DEV_BSIZE)
1158 request->u.ata.command = ATA_READ_MUL48;
1160 request->u.ata.command = ATA_READ48;
1161 request->flags &= ~ATA_R_DMA;
1164 request->u.ata.command = ATA_READ_DMA_QUEUED48;
1167 request->u.ata.command = ATA_WRITE48;
1170 request->u.ata.command = ATA_WRITE_MUL48;
1173 if (ch->flags & ATA_NO_48BIT_DMA) {
1174 if (request->transfersize > DEV_BSIZE)
1175 request->u.ata.command = ATA_WRITE_MUL48;
1177 request->u.ata.command = ATA_WRITE48;
1178 request->flags &= ~ATA_R_DMA;
1181 request->u.ata.command = ATA_WRITE_DMA48;
1183 case ATA_WRITE_DMA_QUEUED:
1184 if (ch->flags & ATA_NO_48BIT_DMA) {
1185 if (request->transfersize > DEV_BSIZE)
1186 request->u.ata.command = ATA_WRITE_MUL48;
1188 request->u.ata.command = ATA_WRITE48;
1189 request->u.ata.command = ATA_WRITE48;
1190 request->flags &= ~ATA_R_DMA;
1193 request->u.ata.command = ATA_WRITE_DMA_QUEUED48;
1195 case ATA_FLUSHCACHE:
1196 request->u.ata.command = ATA_FLUSHCACHE48;
1198 case ATA_SET_MAX_ADDRESS:
1199 request->u.ata.command = ATA_SET_MAX_ADDRESS48;
1204 request->flags |= ATA_R_48BIT;
1206 else if (atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) {
1208 /* translate command into 48bit version */
1209 switch (request->u.ata.command) {
1210 case ATA_FLUSHCACHE:
1211 request->u.ata.command = ATA_FLUSHCACHE48;
1213 case ATA_READ_NATIVE_MAX_ADDRESS:
1214 request->u.ata.command = ATA_READ_NATIVE_MAX_ADDRESS48;
1216 case ATA_SET_MAX_ADDRESS:
1217 request->u.ata.command = ATA_SET_MAX_ADDRESS48;
1222 request->flags |= ATA_R_48BIT;
1228 ata_udelay(int interval)
1230 /* for now just use DELAY, the timer/sleep subsytems are not there yet */
1231 if (1 || interval < (1000000/hz) || ata_delayed_attach)
1234 pause("ataslp", interval/(1000000/hz));
1239 ata_unit2str(struct ata_device *atadev)
1241 struct ata_channel *ch = device_get_softc(device_get_parent(atadev->dev));
1244 if (ch->devices & ATA_PORTMULTIPLIER)
1245 sprintf(str, "port%d", atadev->unit);
1247 sprintf(str, "%s", atadev->unit == ATA_MASTER ? "master" : "slave");
1253 ata_cmd2str(struct ata_request *request)
1255 static char buffer[20];
1257 if (request->flags & ATA_R_ATAPI) {
1258 switch (request->u.atapi.sense.key ?
1259 request->u.atapi.saved_cmd : request->u.atapi.ccb[0]) {
1260 case 0x00: return ("TEST_UNIT_READY");
1261 case 0x01: return ("REZERO");
1262 case 0x03: return ("REQUEST_SENSE");
1263 case 0x04: return ("FORMAT");
1264 case 0x08: return ("READ");
1265 case 0x0a: return ("WRITE");
1266 case 0x10: return ("WEOF");
1267 case 0x11: return ("SPACE");
1268 case 0x12: return ("INQUIRY");
1269 case 0x15: return ("MODE_SELECT");
1270 case 0x19: return ("ERASE");
1271 case 0x1a: return ("MODE_SENSE");
1272 case 0x1b: return ("START_STOP");
1273 case 0x1e: return ("PREVENT_ALLOW");
1274 case 0x23: return ("ATAPI_READ_FORMAT_CAPACITIES");
1275 case 0x25: return ("READ_CAPACITY");
1276 case 0x28: return ("READ_BIG");
1277 case 0x2a: return ("WRITE_BIG");
1278 case 0x2b: return ("LOCATE");
1279 case 0x34: return ("READ_POSITION");
1280 case 0x35: return ("SYNCHRONIZE_CACHE");
1281 case 0x3b: return ("WRITE_BUFFER");
1282 case 0x3c: return ("READ_BUFFER");
1283 case 0x42: return ("READ_SUBCHANNEL");
1284 case 0x43: return ("READ_TOC");
1285 case 0x45: return ("PLAY_10");
1286 case 0x47: return ("PLAY_MSF");
1287 case 0x48: return ("PLAY_TRACK");
1288 case 0x4b: return ("PAUSE");
1289 case 0x51: return ("READ_DISK_INFO");
1290 case 0x52: return ("READ_TRACK_INFO");
1291 case 0x53: return ("RESERVE_TRACK");
1292 case 0x54: return ("SEND_OPC_INFO");
1293 case 0x55: return ("MODE_SELECT_BIG");
1294 case 0x58: return ("REPAIR_TRACK");
1295 case 0x59: return ("READ_MASTER_CUE");
1296 case 0x5a: return ("MODE_SENSE_BIG");
1297 case 0x5b: return ("CLOSE_TRACK/SESSION");
1298 case 0x5c: return ("READ_BUFFER_CAPACITY");
1299 case 0x5d: return ("SEND_CUE_SHEET");
1300 case 0x96: return ("SERVICE_ACTION_IN");
1301 case 0xa1: return ("BLANK_CMD");
1302 case 0xa3: return ("SEND_KEY");
1303 case 0xa4: return ("REPORT_KEY");
1304 case 0xa5: return ("PLAY_12");
1305 case 0xa6: return ("LOAD_UNLOAD");
1306 case 0xad: return ("READ_DVD_STRUCTURE");
1307 case 0xb4: return ("PLAY_CD");
1308 case 0xbb: return ("SET_SPEED");
1309 case 0xbd: return ("MECH_STATUS");
1310 case 0xbe: return ("READ_CD");
1311 case 0xff: return ("POLL_DSC");
1314 switch (request->u.ata.command) {
1315 case 0x00: return ("NOP");
1316 case 0x08: return ("DEVICE_RESET");
1317 case 0x20: return ("READ");
1318 case 0x24: return ("READ48");
1319 case 0x25: return ("READ_DMA48");
1320 case 0x26: return ("READ_DMA_QUEUED48");
1321 case 0x27: return ("READ_NATIVE_MAX_ADDRESS48");
1322 case 0x29: return ("READ_MUL48");
1323 case 0x30: return ("WRITE");
1324 case 0x34: return ("WRITE48");
1325 case 0x35: return ("WRITE_DMA48");
1326 case 0x36: return ("WRITE_DMA_QUEUED48");
1327 case 0x37: return ("SET_MAX_ADDRESS48");
1328 case 0x39: return ("WRITE_MUL48");
1329 case 0x70: return ("SEEK");
1330 case 0xa0: return ("PACKET_CMD");
1331 case 0xa1: return ("ATAPI_IDENTIFY");
1332 case 0xa2: return ("SERVICE");
1333 case 0xb0: return ("SMART");
1334 case 0xc0: return ("CFA ERASE");
1335 case 0xc4: return ("READ_MUL");
1336 case 0xc5: return ("WRITE_MUL");
1337 case 0xc6: return ("SET_MULTI");
1338 case 0xc7: return ("READ_DMA_QUEUED");
1339 case 0xc8: return ("READ_DMA");
1340 case 0xca: return ("WRITE_DMA");
1341 case 0xcc: return ("WRITE_DMA_QUEUED");
1342 case 0xe6: return ("SLEEP");
1343 case 0xe7: return ("FLUSHCACHE");
1344 case 0xea: return ("FLUSHCACHE48");
1345 case 0xec: return ("ATA_IDENTIFY");
1347 switch (request->u.ata.feature) {
1348 case 0x03: return ("SETFEATURES SET TRANSFER MODE");
1349 case 0x02: return ("SETFEATURES ENABLE WCACHE");
1350 case 0x82: return ("SETFEATURES DISABLE WCACHE");
1351 case 0xaa: return ("SETFEATURES ENABLE RCACHE");
1352 case 0x55: return ("SETFEATURES DISABLE RCACHE");
1354 sprintf(buffer, "SETFEATURES 0x%02x",
1355 request->u.ata.feature);
1357 case 0xf5: return ("SECURITY_FREE_LOCK");
1358 case 0xf8: return ("READ_NATIVE_MAX_ADDRESS");
1359 case 0xf9: return ("SET_MAX_ADDRESS");
1362 sprintf(buffer, "unknown CMD (0x%02x)", request->u.ata.command);
1367 ata_mode2str(int mode)
1370 case -1: return "UNSUPPORTED";
1371 case ATA_PIO0: return "PIO0";
1372 case ATA_PIO1: return "PIO1";
1373 case ATA_PIO2: return "PIO2";
1374 case ATA_PIO3: return "PIO3";
1375 case ATA_PIO4: return "PIO4";
1376 case ATA_WDMA0: return "WDMA0";
1377 case ATA_WDMA1: return "WDMA1";
1378 case ATA_WDMA2: return "WDMA2";
1379 case ATA_UDMA0: return "UDMA16";
1380 case ATA_UDMA1: return "UDMA25";
1381 case ATA_UDMA2: return "UDMA33";
1382 case ATA_UDMA3: return "UDMA40";
1383 case ATA_UDMA4: return "UDMA66";
1384 case ATA_UDMA5: return "UDMA100";
1385 case ATA_UDMA6: return "UDMA133";
1386 case ATA_SA150: return "SATA150";
1387 case ATA_SA300: return "SATA300";
1389 if (mode & ATA_DMA_MASK)
1397 ata_str2mode(const char *str)
1400 if (!strcasecmp(str, "PIO0")) return (ATA_PIO0);
1401 if (!strcasecmp(str, "PIO1")) return (ATA_PIO1);
1402 if (!strcasecmp(str, "PIO2")) return (ATA_PIO2);
1403 if (!strcasecmp(str, "PIO3")) return (ATA_PIO3);
1404 if (!strcasecmp(str, "PIO4")) return (ATA_PIO4);
1405 if (!strcasecmp(str, "WDMA0")) return (ATA_WDMA0);
1406 if (!strcasecmp(str, "WDMA1")) return (ATA_WDMA1);
1407 if (!strcasecmp(str, "WDMA2")) return (ATA_WDMA2);
1408 if (!strcasecmp(str, "UDMA0")) return (ATA_UDMA0);
1409 if (!strcasecmp(str, "UDMA16")) return (ATA_UDMA0);
1410 if (!strcasecmp(str, "UDMA1")) return (ATA_UDMA1);
1411 if (!strcasecmp(str, "UDMA25")) return (ATA_UDMA1);
1412 if (!strcasecmp(str, "UDMA2")) return (ATA_UDMA2);
1413 if (!strcasecmp(str, "UDMA33")) return (ATA_UDMA2);
1414 if (!strcasecmp(str, "UDMA3")) return (ATA_UDMA3);
1415 if (!strcasecmp(str, "UDMA44")) return (ATA_UDMA3);
1416 if (!strcasecmp(str, "UDMA4")) return (ATA_UDMA4);
1417 if (!strcasecmp(str, "UDMA66")) return (ATA_UDMA4);
1418 if (!strcasecmp(str, "UDMA5")) return (ATA_UDMA5);
1419 if (!strcasecmp(str, "UDMA100")) return (ATA_UDMA5);
1420 if (!strcasecmp(str, "UDMA6")) return (ATA_UDMA6);
1421 if (!strcasecmp(str, "UDMA133")) return (ATA_UDMA6);
1427 ata_satarev2str(int rev)
1431 case 1: return "SATA 1.5Gb/s";
1432 case 2: return "SATA 3Gb/s";
1433 case 3: return "SATA 6Gb/s";
1434 case 0xff: return "SATA";
1435 default: return "???";
1441 ata_atapi(device_t dev, int target)
1443 struct ata_channel *ch = device_get_softc(dev);
1445 return (ch->devices & (ATA_ATAPI_MASTER << target));
1450 ata_pmode(struct ata_params *ap)
1452 if (ap->atavalid & ATA_FLAG_64_70) {
1453 if (ap->apiomodes & 0x02)
1455 if (ap->apiomodes & 0x01)
1458 if (ap->mwdmamodes & 0x04)
1460 if (ap->mwdmamodes & 0x02)
1462 if (ap->mwdmamodes & 0x01)
1464 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200)
1466 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100)
1468 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000)
1476 ata_wmode(struct ata_params *ap)
1478 if (ap->mwdmamodes & 0x04)
1480 if (ap->mwdmamodes & 0x02)
1482 if (ap->mwdmamodes & 0x01)
1490 ata_umode(struct ata_params *ap)
1492 if (ap->atavalid & ATA_FLAG_88) {
1493 if (ap->udmamodes & 0x40)
1495 if (ap->udmamodes & 0x20)
1497 if (ap->udmamodes & 0x10)
1499 if (ap->udmamodes & 0x08)
1501 if (ap->udmamodes & 0x04)
1503 if (ap->udmamodes & 0x02)
1505 if (ap->udmamodes & 0x01)
1514 ata_limit_mode(device_t dev, int mode, int maxmode)
1516 struct ata_device *atadev = device_get_softc(dev);
1518 if (maxmode && mode > maxmode)
1521 if (mode >= ATA_UDMA0 && ata_umode(&atadev->param) > 0)
1522 return min(mode, ata_umode(&atadev->param));
1524 if (mode >= ATA_WDMA0 && ata_wmode(&atadev->param) > 0)
1525 return min(mode, ata_wmode(&atadev->param));
1527 if (mode > ata_pmode(&atadev->param))
1528 return min(mode, ata_pmode(&atadev->param));
1536 bswap(int8_t *buf, int len)
1538 u_int16_t *ptr = (u_int16_t*)(buf + len);
1540 while (--ptr >= (u_int16_t*)buf)
1547 btrim(int8_t *buf, int len)
1551 for (ptr = buf; ptr < buf+len; ++ptr)
1552 if (!*ptr || *ptr == '_')
1554 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
1561 bpack(int8_t *src, int8_t *dst, int len)
1565 for (i = j = blank = 0 ; i < len; i++) {
1566 if (blank && src[i] == ' ') continue;
1567 if (blank && src[i] != ' ') {
1572 if (src[i] == ' ') {
1585 ata_timeout(struct ata_request *request)
1587 struct ata_channel *ch;
1589 ch = device_get_softc(request->parent);
1590 //request->flags |= ATA_R_DEBUG;
1591 ATA_DEBUG_RQ(request, "timeout");
1594 * If we have an ATA_ACTIVE request running, we flag the request
1595 * ATA_R_TIMEOUT so ata_cam_end_transaction()/ata_finish() will handle
1597 * Also, NULL out the running request so we wont loose the race with
1598 * an eventual interrupt arriving late.
1600 if (ch->state == ATA_ACTIVE) {
1601 request->flags |= ATA_R_TIMEOUT;
1603 ch->dma.unload(request);
1605 ch->state = ATA_IDLE;
1607 ata_cam_end_transaction(ch->dev, request);
1609 mtx_unlock(&ch->state_mtx);
1611 ATA_LOCKING(ch->dev, ATA_LF_UNLOCK);
1612 ata_finish(request);
1615 mtx_unlock(&ch->state_mtx);
1620 ata_cam_begin_transaction(device_t dev, union ccb *ccb)
1622 struct ata_channel *ch = device_get_softc(dev);
1623 struct ata_request *request;
1625 if (!(request = ata_alloc_request())) {
1626 device_printf(dev, "FAILURE - out of memory in start\n");
1627 ccb->ccb_h.status = CAM_REQ_INVALID;
1631 bzero(request, sizeof(*request));
1634 request->dev = NULL;
1635 request->parent = dev;
1636 request->unit = ccb->ccb_h.target_id;
1637 if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1638 request->data = ccb->ataio.data_ptr;
1639 request->bytecount = ccb->ataio.dxfer_len;
1640 request->u.ata.command = ccb->ataio.cmd.command;
1641 request->u.ata.feature = ((uint16_t)ccb->ataio.cmd.features_exp << 8) |
1642 (uint16_t)ccb->ataio.cmd.features;
1643 request->u.ata.count = ((uint16_t)ccb->ataio.cmd.sector_count_exp << 8) |
1644 (uint16_t)ccb->ataio.cmd.sector_count;
1645 if (ccb->ataio.cmd.flags & CAM_ATAIO_48BIT) {
1646 request->flags |= ATA_R_48BIT;
1647 request->u.ata.lba =
1648 ((uint64_t)ccb->ataio.cmd.lba_high_exp << 40) |
1649 ((uint64_t)ccb->ataio.cmd.lba_mid_exp << 32) |
1650 ((uint64_t)ccb->ataio.cmd.lba_low_exp << 24);
1652 request->u.ata.lba =
1653 ((uint64_t)(ccb->ataio.cmd.device & 0x0f) << 24);
1655 request->u.ata.lba |= ((uint64_t)ccb->ataio.cmd.lba_high << 16) |
1656 ((uint64_t)ccb->ataio.cmd.lba_mid << 8) |
1657 (uint64_t)ccb->ataio.cmd.lba_low;
1658 if (ccb->ataio.cmd.flags & CAM_ATAIO_NEEDRESULT)
1659 request->flags |= ATA_R_NEEDRESULT;
1660 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE &&
1661 ccb->ataio.cmd.flags & CAM_ATAIO_DMA)
1662 request->flags |= ATA_R_DMA;
1663 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1664 request->flags |= ATA_R_READ;
1665 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
1666 request->flags |= ATA_R_WRITE;
1667 if (ccb->ataio.cmd.command == ATA_READ_MUL ||
1668 ccb->ataio.cmd.command == ATA_READ_MUL48 ||
1669 ccb->ataio.cmd.command == ATA_WRITE_MUL ||
1670 ccb->ataio.cmd.command == ATA_WRITE_MUL48) {
1671 request->transfersize = min(request->bytecount,
1672 ch->curr[ccb->ccb_h.target_id].bytecount);
1674 request->transfersize = min(request->bytecount, 512);
1676 request->data = ccb->csio.data_ptr;
1677 request->bytecount = ccb->csio.dxfer_len;
1678 bcopy((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
1679 ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes,
1680 request->u.atapi.ccb, ccb->csio.cdb_len);
1681 request->flags |= ATA_R_ATAPI;
1682 if (ch->curr[ccb->ccb_h.target_id].atapi == 16)
1683 request->flags |= ATA_R_ATAPI16;
1684 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE &&
1685 ch->curr[ccb->ccb_h.target_id].mode >= ATA_DMA)
1686 request->flags |= ATA_R_DMA;
1687 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1688 request->flags |= ATA_R_READ;
1689 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
1690 request->flags |= ATA_R_WRITE;
1691 request->transfersize = min(request->bytecount,
1692 ch->curr[ccb->ccb_h.target_id].bytecount);
1694 request->retries = 0;
1695 request->timeout = (ccb->ccb_h.timeout + 999) / 1000;
1696 callout_init_mtx(&request->callout, &ch->state_mtx, CALLOUT_RETURNUNLOCKED);
1698 request->flags |= ATA_R_DATA_IN_CCB;
1700 ch->running = request;
1701 ch->state = ATA_ACTIVE;
1702 if (ch->hw.begin_transaction(request) == ATA_OP_FINISHED) {
1704 ch->state = ATA_IDLE;
1705 ata_cam_end_transaction(dev, request);
1711 ata_cam_request_sense(device_t dev, struct ata_request *request)
1713 struct ata_channel *ch = device_get_softc(dev);
1714 union ccb *ccb = request->ccb;
1716 ch->requestsense = 1;
1718 bzero(request, sizeof(*request));
1719 request->dev = NULL;
1720 request->parent = dev;
1721 request->unit = ccb->ccb_h.target_id;
1722 request->data = (void *)&ccb->csio.sense_data;
1723 request->bytecount = ccb->csio.sense_len;
1724 request->u.atapi.ccb[0] = ATAPI_REQUEST_SENSE;
1725 request->u.atapi.ccb[4] = ccb->csio.sense_len;
1726 request->flags |= ATA_R_ATAPI;
1727 if (ch->curr[ccb->ccb_h.target_id].atapi == 16)
1728 request->flags |= ATA_R_ATAPI16;
1729 if (ch->curr[ccb->ccb_h.target_id].mode >= ATA_DMA)
1730 request->flags |= ATA_R_DMA;
1731 request->flags |= ATA_R_READ;
1732 request->transfersize = min(request->bytecount,
1733 ch->curr[ccb->ccb_h.target_id].bytecount);
1734 request->retries = 0;
1735 request->timeout = (ccb->ccb_h.timeout + 999) / 1000;
1736 callout_init_mtx(&request->callout, &ch->state_mtx, CALLOUT_RETURNUNLOCKED);
1739 ch->running = request;
1740 ch->state = ATA_ACTIVE;
1741 if (ch->hw.begin_transaction(request) == ATA_OP_FINISHED) {
1743 ch->state = ATA_IDLE;
1744 ata_cam_end_transaction(dev, request);
1750 ata_cam_process_sense(device_t dev, struct ata_request *request)
1752 struct ata_channel *ch = device_get_softc(dev);
1753 union ccb *ccb = request->ccb;
1756 ch->requestsense = 0;
1758 if (request->flags & ATA_R_TIMEOUT)
1760 if ((request->flags & ATA_R_TIMEOUT) == 0 &&
1761 (request->status & ATA_S_ERROR) == 0 &&
1762 request->result == 0) {
1763 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
1765 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1766 ccb->ccb_h.status |= CAM_AUTOSENSE_FAIL;
1769 ata_free_request(request);
1771 /* Do error recovery if needed. */
1777 ata_cam_end_transaction(device_t dev, struct ata_request *request)
1779 struct ata_channel *ch = device_get_softc(dev);
1780 union ccb *ccb = request->ccb;
1783 if (ch->requestsense) {
1784 ata_cam_process_sense(dev, request);
1788 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1789 if (request->flags & ATA_R_TIMEOUT) {
1790 xpt_freeze_simq(ch->sim, 1);
1791 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1792 ccb->ccb_h.status |= CAM_CMD_TIMEOUT | CAM_RELEASE_SIMQ;
1794 } else if (request->status & ATA_S_ERROR) {
1795 if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1796 ccb->ccb_h.status |= CAM_ATA_STATUS_ERROR;
1798 ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1799 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1801 } else if (request->result == ERESTART)
1802 ccb->ccb_h.status |= CAM_REQUEUE_REQ;
1803 else if (request->result != 0)
1804 ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
1806 ccb->ccb_h.status |= CAM_REQ_CMP;
1807 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP &&
1808 !(ccb->ccb_h.status & CAM_DEV_QFRZN)) {
1809 xpt_freeze_devq(ccb->ccb_h.path, 1);
1810 ccb->ccb_h.status |= CAM_DEV_QFRZN;
1812 if (ccb->ccb_h.func_code == XPT_ATA_IO &&
1813 ((request->status & ATA_S_ERROR) ||
1814 (ccb->ataio.cmd.flags & CAM_ATAIO_NEEDRESULT))) {
1815 struct ata_res *res = &ccb->ataio.res;
1816 res->status = request->status;
1817 res->error = request->error;
1818 res->lba_low = request->u.ata.lba;
1819 res->lba_mid = request->u.ata.lba >> 8;
1820 res->lba_high = request->u.ata.lba >> 16;
1821 res->device = request->u.ata.lba >> 24;
1822 res->lba_low_exp = request->u.ata.lba >> 24;
1823 res->lba_mid_exp = request->u.ata.lba >> 32;
1824 res->lba_high_exp = request->u.ata.lba >> 40;
1825 res->sector_count = request->u.ata.count;
1826 res->sector_count_exp = request->u.ata.count >> 8;
1828 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1829 if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1831 ccb->ataio.dxfer_len - request->donecount;
1834 ccb->csio.dxfer_len - request->donecount;
1837 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_SCSI_STATUS_ERROR &&
1838 (ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)
1839 ata_cam_request_sense(dev, request);
1841 ata_free_request(request);
1844 /* Do error recovery if needed. */
1850 ata_check_ids(device_t dev, union ccb *ccb)
1852 struct ata_channel *ch = device_get_softc(dev);
1854 if (ccb->ccb_h.target_id > ((ch->flags & ATA_NO_SLAVE) ? 0 : 1)) {
1855 ccb->ccb_h.status = CAM_TID_INVALID;
1859 if (ccb->ccb_h.target_lun != 0) {
1860 ccb->ccb_h.status = CAM_LUN_INVALID;
1868 ataaction(struct cam_sim *sim, union ccb *ccb)
1870 device_t dev, parent;
1871 struct ata_channel *ch;
1873 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ataaction func_code=%x\n",
1874 ccb->ccb_h.func_code));
1876 ch = (struct ata_channel *)cam_sim_softc(sim);
1878 switch (ccb->ccb_h.func_code) {
1879 /* Common cases first */
1880 case XPT_ATA_IO: /* Execute the requested I/O operation */
1882 if (ata_check_ids(dev, ccb))
1884 if ((ch->devices & ((ATA_ATA_MASTER | ATA_ATAPI_MASTER)
1885 << ccb->ccb_h.target_id)) == 0) {
1886 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
1890 device_printf(dev, "already running!\n");
1891 if (ccb->ccb_h.func_code == XPT_ATA_IO &&
1892 (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) &&
1893 (ccb->ataio.cmd.control & ATA_A_RESET)) {
1894 struct ata_res *res = &ccb->ataio.res;
1896 bzero(res, sizeof(*res));
1897 if (ch->devices & (ATA_ATA_MASTER << ccb->ccb_h.target_id)) {
1901 res->lba_high = 0xeb;
1902 res->lba_mid = 0x14;
1904 ccb->ccb_h.status = CAM_REQ_CMP;
1907 ata_cam_begin_transaction(dev, ccb);
1909 case XPT_EN_LUN: /* Enable LUN as a target */
1910 case XPT_TARGET_IO: /* Execute target I/O request */
1911 case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */
1912 case XPT_CONT_TARGET_IO: /* Continue Host Target I/O Connection*/
1913 case XPT_ABORT: /* Abort the specified CCB */
1915 ccb->ccb_h.status = CAM_REQ_INVALID;
1917 case XPT_SET_TRAN_SETTINGS:
1919 struct ccb_trans_settings *cts = &ccb->cts;
1920 struct ata_cam_device *d;
1922 if (ata_check_ids(dev, ccb))
1924 if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
1925 d = &ch->curr[ccb->ccb_h.target_id];
1927 d = &ch->user[ccb->ccb_h.target_id];
1928 if (ch->flags & ATA_SATA) {
1929 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_REVISION)
1930 d->revision = cts->xport_specific.sata.revision;
1931 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_MODE) {
1932 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
1933 d->mode = ATA_SETMODE(ch->dev,
1934 ccb->ccb_h.target_id,
1935 cts->xport_specific.sata.mode);
1937 d->mode = cts->xport_specific.sata.mode;
1939 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
1940 d->bytecount = min(8192, cts->xport_specific.sata.bytecount);
1941 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_ATAPI)
1942 d->atapi = cts->xport_specific.sata.atapi;
1943 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1944 d->caps = cts->xport_specific.sata.caps;
1946 if (cts->xport_specific.ata.valid & CTS_ATA_VALID_MODE) {
1947 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
1948 d->mode = ATA_SETMODE(ch->dev,
1949 ccb->ccb_h.target_id,
1950 cts->xport_specific.ata.mode);
1952 d->mode = cts->xport_specific.ata.mode;
1954 if (cts->xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
1955 d->bytecount = cts->xport_specific.ata.bytecount;
1956 if (cts->xport_specific.ata.valid & CTS_ATA_VALID_ATAPI)
1957 d->atapi = cts->xport_specific.ata.atapi;
1958 if (cts->xport_specific.ata.valid & CTS_ATA_VALID_CAPS)
1959 d->caps = cts->xport_specific.ata.caps;
1961 ccb->ccb_h.status = CAM_REQ_CMP;
1964 case XPT_GET_TRAN_SETTINGS:
1966 struct ccb_trans_settings *cts = &ccb->cts;
1967 struct ata_cam_device *d;
1969 if (ata_check_ids(dev, ccb))
1971 if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
1972 d = &ch->curr[ccb->ccb_h.target_id];
1974 d = &ch->user[ccb->ccb_h.target_id];
1975 cts->protocol = PROTO_UNSPECIFIED;
1976 cts->protocol_version = PROTO_VERSION_UNSPECIFIED;
1977 if (ch->flags & ATA_SATA) {
1978 cts->transport = XPORT_SATA;
1979 cts->transport_version = XPORT_VERSION_UNSPECIFIED;
1980 cts->xport_specific.sata.valid = 0;
1981 cts->xport_specific.sata.mode = d->mode;
1982 cts->xport_specific.sata.valid |= CTS_SATA_VALID_MODE;
1983 cts->xport_specific.sata.bytecount = d->bytecount;
1984 cts->xport_specific.sata.valid |= CTS_SATA_VALID_BYTECOUNT;
1985 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
1986 cts->xport_specific.sata.revision =
1987 ATA_GETREV(dev, ccb->ccb_h.target_id);
1988 if (cts->xport_specific.sata.revision != 0xff) {
1989 cts->xport_specific.sata.valid |=
1990 CTS_SATA_VALID_REVISION;
1992 cts->xport_specific.sata.caps =
1993 d->caps & CTS_SATA_CAPS_D;
1995 cts->xport_specific.sata.caps |=
1996 CTS_SATA_CAPS_H_PMREQ;
1998 cts->xport_specific.sata.caps &=
1999 ch->user[ccb->ccb_h.target_id].caps;
2001 cts->xport_specific.sata.revision = d->revision;
2002 cts->xport_specific.sata.valid |= CTS_SATA_VALID_REVISION;
2003 cts->xport_specific.sata.caps = d->caps;
2005 cts->xport_specific.sata.valid |= CTS_SATA_VALID_CAPS;
2006 cts->xport_specific.sata.atapi = d->atapi;
2007 cts->xport_specific.sata.valid |= CTS_SATA_VALID_ATAPI;
2009 cts->transport = XPORT_ATA;
2010 cts->transport_version = XPORT_VERSION_UNSPECIFIED;
2011 cts->xport_specific.ata.valid = 0;
2012 cts->xport_specific.ata.mode = d->mode;
2013 cts->xport_specific.ata.valid |= CTS_ATA_VALID_MODE;
2014 cts->xport_specific.ata.bytecount = d->bytecount;
2015 cts->xport_specific.ata.valid |= CTS_ATA_VALID_BYTECOUNT;
2016 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
2017 cts->xport_specific.ata.caps =
2018 d->caps & CTS_ATA_CAPS_D;
2019 if (!(ch->flags & ATA_NO_48BIT_DMA))
2020 cts->xport_specific.ata.caps |=
2021 CTS_ATA_CAPS_H_DMA48;
2022 cts->xport_specific.ata.caps &=
2023 ch->user[ccb->ccb_h.target_id].caps;
2025 cts->xport_specific.ata.caps = d->caps;
2026 cts->xport_specific.ata.valid |= CTS_ATA_VALID_CAPS;
2027 cts->xport_specific.ata.atapi = d->atapi;
2028 cts->xport_specific.ata.valid |= CTS_ATA_VALID_ATAPI;
2030 ccb->ccb_h.status = CAM_REQ_CMP;
2033 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
2034 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
2036 ccb->ccb_h.status = CAM_REQ_CMP;
2038 case XPT_TERM_IO: /* Terminate the I/O process */
2040 ccb->ccb_h.status = CAM_REQ_INVALID;
2042 case XPT_PATH_INQ: /* Path routing inquiry */
2044 struct ccb_pathinq *cpi = &ccb->cpi;
2046 parent = device_get_parent(dev);
2047 cpi->version_num = 1; /* XXX??? */
2048 cpi->hba_inquiry = PI_SDTR_ABLE;
2049 cpi->target_sprt = 0;
2050 cpi->hba_misc = PIM_SEQSCAN;
2051 cpi->hba_eng_cnt = 0;
2052 if (ch->flags & ATA_NO_SLAVE)
2053 cpi->max_target = 0;
2055 cpi->max_target = 1;
2057 cpi->initiator_id = 0;
2058 cpi->bus_id = cam_sim_bus(sim);
2059 if (ch->flags & ATA_SATA)
2060 cpi->base_transfer_speed = 150000;
2062 cpi->base_transfer_speed = 3300;
2063 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2064 strncpy(cpi->hba_vid, "ATA", HBA_IDLEN);
2065 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2066 cpi->unit_number = cam_sim_unit(sim);
2067 if (ch->flags & ATA_SATA)
2068 cpi->transport = XPORT_SATA;
2070 cpi->transport = XPORT_ATA;
2071 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
2072 cpi->protocol = PROTO_ATA;
2073 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
2074 cpi->maxio = ch->dma.max_iosize ? ch->dma.max_iosize : DFLTPHYS;
2075 if (device_get_devclass(device_get_parent(parent)) ==
2076 devclass_find("pci")) {
2077 cpi->hba_vendor = pci_get_vendor(parent);
2078 cpi->hba_device = pci_get_device(parent);
2079 cpi->hba_subvendor = pci_get_subvendor(parent);
2080 cpi->hba_subdevice = pci_get_subdevice(parent);
2082 cpi->ccb_h.status = CAM_REQ_CMP;
2086 ccb->ccb_h.status = CAM_REQ_INVALID;
2093 atapoll(struct cam_sim *sim)
2095 struct ata_channel *ch = (struct ata_channel *)cam_sim_softc(sim);
2097 ata_interrupt_locked(ch);
2105 ata_module_event_handler(module_t mod, int what, void *arg)
2108 static struct cdev *atacdev;
2114 /* register controlling device */
2115 atacdev = make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "ata");
2118 /* register boot attach to be run when interrupts are enabled */
2119 if (!(ata_delayed_attach = (struct intr_config_hook *)
2120 malloc(sizeof(struct intr_config_hook),
2121 M_TEMP, M_NOWAIT | M_ZERO))) {
2122 printf("ata: malloc of delayed attach hook failed\n");
2125 ata_delayed_attach->ich_func = (void*)ata_boot_attach;
2126 if (config_intrhook_establish(ata_delayed_attach) != 0) {
2127 printf("ata: config_intrhook_establish failed\n");
2128 free(ata_delayed_attach, M_TEMP);
2136 /* deregister controlling device */
2137 destroy_dev(atacdev);
2146 static moduledata_t ata_moduledata = { "ata", ata_module_event_handler, NULL };
2147 DECLARE_MODULE(ata, ata_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
2148 MODULE_VERSION(ata, 1);
2150 MODULE_DEPEND(ata, cam, 1, 1, 1);
2156 ata_request_zone = uma_zcreate("ata_request", sizeof(struct ata_request),
2157 NULL, NULL, NULL, NULL, 0, 0);
2158 ata_composite_zone = uma_zcreate("ata_composite",
2159 sizeof(struct ata_composite),
2160 NULL, NULL, NULL, NULL, 0, 0);
2162 SYSINIT(ata_register, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL);
2167 uma_zdestroy(ata_composite_zone);
2168 uma_zdestroy(ata_request_zone);
2170 SYSUNINIT(ata_unregister, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_uninit, NULL);