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>
55 #include <cam/cam_ccb.h>
56 #include <cam/cam_sim.h>
57 #include <cam/cam_xpt_sim.h>
58 #include <cam/cam_debug.h>
62 /* device structure */
63 static d_ioctl_t ata_ioctl;
64 static struct cdevsw ata_cdevsw = {
65 .d_version = D_VERSION,
66 .d_flags = D_NEEDGIANT, /* we need this as newbus isn't mpsafe */
74 static void ata_boot_attach(void);
75 static device_t ata_add_child(device_t, struct ata_device *, int);
77 static void ataaction(struct cam_sim *sim, union ccb *ccb);
78 static void atapoll(struct cam_sim *sim);
80 static void ata_conn_event(void *, int);
82 static void bswap(int8_t *, int);
83 static void btrim(int8_t *, int);
84 static void bpack(int8_t *, int8_t *, int);
86 static void ata_interrupt_locked(void *data);
88 static void ata_periodic_poll(void *data);
92 MALLOC_DEFINE(M_ATA, "ata_generic", "ATA driver generic layer");
93 int (*ata_raid_ioctl_func)(u_long cmd, caddr_t data) = NULL;
95 struct intr_config_hook *ata_delayed_attach = NULL;
97 devclass_t ata_devclass;
98 uma_zone_t ata_request_zone;
99 uma_zone_t ata_composite_zone;
104 int ata_dma_check_80pin = 1;
108 static int ata_dma = 1;
109 static int atapi_dma = 1;
113 SYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters");
115 TUNABLE_INT("hw.ata.ata_dma", &ata_dma);
116 SYSCTL_INT(_hw_ata, OID_AUTO, ata_dma, CTLFLAG_RDTUN, &ata_dma, 0,
117 "ATA disk DMA mode control");
119 TUNABLE_INT("hw.ata.ata_dma_check_80pin", &ata_dma_check_80pin);
120 SYSCTL_INT(_hw_ata, OID_AUTO, ata_dma_check_80pin,
121 CTLFLAG_RW, &ata_dma_check_80pin, 1,
122 "Check for 80pin cable before setting ATA DMA mode");
124 TUNABLE_INT("hw.ata.atapi_dma", &atapi_dma);
125 SYSCTL_INT(_hw_ata, OID_AUTO, atapi_dma, CTLFLAG_RDTUN, &atapi_dma, 0,
126 "ATAPI device DMA mode control");
127 TUNABLE_INT("hw.ata.wc", &ata_wc);
128 SYSCTL_INT(_hw_ata, OID_AUTO, wc, CTLFLAG_RDTUN, &ata_wc, 0,
129 "ATA disk write caching");
130 TUNABLE_INT("hw.ata.setmax", &ata_setmax);
131 SYSCTL_INT(_hw_ata, OID_AUTO, setmax, CTLFLAG_RDTUN, &ata_setmax, 0,
132 "ATA disk set max native address");
135 FEATURE(ata_cam, "ATA devices are accessed through the cam(4) driver");
139 * newbus device interface related functions
142 ata_probe(device_t dev)
148 ata_attach(device_t dev)
150 struct ata_channel *ch = device_get_softc(dev);
153 struct cam_devq *devq;
159 /* check that we have a virgin channel to attach */
163 /* initialize the softc basics */
165 ch->state = ATA_IDLE;
166 bzero(&ch->state_mtx, sizeof(struct mtx));
167 mtx_init(&ch->state_mtx, "ATA state lock", NULL, MTX_DEF);
168 bzero(&ch->queue_mtx, sizeof(struct mtx));
169 mtx_init(&ch->queue_mtx, "ATA queue lock", NULL, MTX_DEF);
170 TAILQ_INIT(&ch->ata_queue);
171 TASK_INIT(&ch->conntask, 0, ata_conn_event, dev);
173 for (i = 0; i < 16; i++) {
174 ch->user[i].revision = 0;
175 snprintf(buf, sizeof(buf), "dev%d.sata_rev", i);
176 if (resource_int_value(device_get_name(dev),
177 device_get_unit(dev), buf, &mode) != 0 &&
178 resource_int_value(device_get_name(dev),
179 device_get_unit(dev), "sata_rev", &mode) != 0)
182 ch->user[i].revision = mode;
183 ch->user[i].mode = 0;
184 snprintf(buf, sizeof(buf), "dev%d.mode", i);
185 if (resource_string_value(device_get_name(dev),
186 device_get_unit(dev), buf, &res) == 0)
187 mode = ata_str2mode(res);
188 else if (resource_string_value(device_get_name(dev),
189 device_get_unit(dev), "mode", &res) == 0)
190 mode = ata_str2mode(res);
194 ch->user[i].mode = mode;
195 if (ch->flags & ATA_SATA)
196 ch->user[i].bytecount = 8192;
198 ch->user[i].bytecount = MAXPHYS;
199 ch->user[i].caps = 0;
200 ch->curr[i] = ch->user[i];
201 if (ch->pm_level > 0)
202 ch->user[i].caps |= CTS_SATA_CAPS_H_PMREQ;
203 if (ch->pm_level > 1)
204 ch->user[i].caps |= CTS_SATA_CAPS_D_PMREQ;
206 callout_init(&ch->poll_callout, 1);
210 /* reset the controller HW, the channel and device(s) */
211 while (ATA_LOCKING(dev, ATA_LF_LOCK) != ch->unit)
214 ATA_LOCKING(dev, ATA_LF_UNLOCK);
217 /* allocate DMA resources if DMA HW present*/
221 /* setup interrupt delivery */
223 ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
224 RF_SHAREABLE | RF_ACTIVE);
226 device_printf(dev, "unable to allocate interrupt\n");
229 if ((error = bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS, NULL,
230 ata_interrupt, ch, &ch->ih))) {
231 bus_release_resource(dev, SYS_RES_IRQ, rid, ch->r_irq);
232 device_printf(dev, "unable to setup interrupt\n");
237 /* probe and attach devices on this channel unless we are in early boot */
238 if (!ata_delayed_attach)
242 if (ch->flags & ATA_PERIODIC_POLL)
243 callout_reset(&ch->poll_callout, hz, ata_periodic_poll, ch);
244 mtx_lock(&ch->state_mtx);
245 /* Create the device queue for our SIM. */
246 devq = cam_simq_alloc(1);
248 device_printf(dev, "Unable to allocate simq\n");
252 /* Construct SIM entry */
253 ch->sim = cam_sim_alloc(ataaction, atapoll, "ata", ch,
254 device_get_unit(dev), &ch->state_mtx, 1, 0, devq);
255 if (ch->sim == NULL) {
256 device_printf(dev, "unable to allocate sim\n");
261 if (xpt_bus_register(ch->sim, dev, 0) != CAM_SUCCESS) {
262 device_printf(dev, "unable to register xpt bus\n");
266 if (xpt_create_path(&ch->path, /*periph*/NULL, cam_sim_path(ch->sim),
267 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
268 device_printf(dev, "unable to create path\n");
272 mtx_unlock(&ch->state_mtx);
276 xpt_bus_deregister(cam_sim_path(ch->sim));
278 cam_sim_free(ch->sim, /*free_devq*/TRUE);
281 bus_release_resource(dev, SYS_RES_IRQ, rid, ch->r_irq);
282 mtx_unlock(&ch->state_mtx);
283 if (ch->flags & ATA_PERIODIC_POLL)
284 callout_drain(&ch->poll_callout);
290 ata_detach(device_t dev)
292 struct ata_channel *ch = device_get_softc(dev);
298 /* check that we have a valid channel to detach */
302 /* grap the channel lock so no new requests gets launched */
303 mtx_lock(&ch->state_mtx);
304 ch->state |= ATA_STALL_QUEUE;
305 mtx_unlock(&ch->state_mtx);
307 if (ch->flags & ATA_PERIODIC_POLL)
308 callout_drain(&ch->poll_callout);
312 /* detach & delete all children */
313 if (!device_get_children(dev, &children, &nchildren)) {
314 for (i = 0; i < nchildren; i++)
316 device_delete_child(dev, children[i]);
317 free(children, M_TEMP);
320 taskqueue_drain(taskqueue_thread, &ch->conntask);
323 mtx_lock(&ch->state_mtx);
324 xpt_async(AC_LOST_DEVICE, ch->path, NULL);
325 xpt_free_path(ch->path);
326 xpt_bus_deregister(cam_sim_path(ch->sim));
327 cam_sim_free(ch->sim, /*free_devq*/TRUE);
329 mtx_unlock(&ch->state_mtx);
332 /* release resources */
333 bus_teardown_intr(dev, ch->r_irq, ch->ih);
334 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
337 /* free DMA resources if DMA HW present*/
341 mtx_destroy(&ch->state_mtx);
342 mtx_destroy(&ch->queue_mtx);
347 ata_conn_event(void *context, int dummy)
349 device_t dev = (device_t)context;
351 struct ata_channel *ch = device_get_softc(dev);
354 mtx_lock(&ch->state_mtx);
355 if (ch->sim == NULL) {
356 mtx_unlock(&ch->state_mtx);
360 if ((ccb = xpt_alloc_ccb_nowait()) == NULL)
362 if (xpt_create_path(&ccb->ccb_h.path, NULL,
363 cam_sim_path(ch->sim),
364 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
369 mtx_unlock(&ch->state_mtx);
376 ata_reinit(device_t dev)
378 struct ata_channel *ch = device_get_softc(dev);
379 struct ata_request *request;
384 /* check that we have a valid channel to reinit */
385 if (!ch || !ch->r_irq)
389 device_printf(dev, "reiniting channel ..\n");
391 /* poll for locking the channel */
392 while (ATA_LOCKING(dev, ATA_LF_LOCK) != ch->unit)
395 /* catch eventual request in ch->running */
396 mtx_lock(&ch->state_mtx);
397 if (ch->state & ATA_STALL_QUEUE) {
398 /* Recursive reinits and reinits during detach prohobited. */
399 mtx_unlock(&ch->state_mtx);
402 if ((request = ch->running))
403 callout_stop(&request->callout);
406 /* unconditionally grap the channel lock */
407 ch->state |= ATA_STALL_QUEUE;
408 mtx_unlock(&ch->state_mtx);
410 /* reset the controller HW, the channel and device(s) */
413 /* reinit the children and delete any that fails */
414 if (!device_get_children(dev, &children, &nchildren)) {
415 mtx_lock(&Giant); /* newbus suckage it needs Giant */
416 for (i = 0; i < nchildren; i++) {
417 /* did any children go missing ? */
418 if (children[i] && device_is_attached(children[i]) &&
419 ATA_REINIT(children[i])) {
421 * if we had a running request and its device matches
422 * this child we need to inform the request that the
425 if (request && request->dev == children[i]) {
426 request->result = ENXIO;
427 device_printf(request->dev, "FAILURE - device detached\n");
429 /* if not timeout finish request here */
430 if (!(request->flags & ATA_R_TIMEOUT))
434 device_delete_child(dev, children[i]);
437 free(children, M_TEMP);
438 mtx_unlock(&Giant); /* newbus suckage dealt with, release Giant */
441 /* if we still have a good request put it on the queue again */
442 if (request && !(request->flags & ATA_R_TIMEOUT)) {
443 device_printf(request->dev,
444 "WARNING - %s requeued due to channel reset",
445 ata_cmd2str(request));
446 if (!(request->flags & (ATA_R_ATAPI | ATA_R_CONTROL)))
447 printf(" LBA=%ju", request->u.ata.lba);
449 request->flags |= ATA_R_REQUEUE;
450 ata_queue_request(request);
453 /* we're done release the channel for new work */
454 mtx_lock(&ch->state_mtx);
455 ch->state = ATA_IDLE;
456 mtx_unlock(&ch->state_mtx);
457 ATA_LOCKING(dev, ATA_LF_UNLOCK);
459 /* Add new children. */
460 /* ata_identify(dev); */
463 device_printf(dev, "reinit done ..\n");
465 /* kick off requests on the queue */
468 xpt_freeze_simq(ch->sim, 1);
469 if ((request = ch->running)) {
471 if (ch->state == ATA_ACTIVE)
472 ch->state = ATA_IDLE;
473 callout_stop(&request->callout);
475 ch->dma.unload(request);
476 request->result = ERESTART;
477 ata_cam_end_transaction(dev, request);
479 /* reset the controller HW, the channel and device(s) */
481 /* Tell the XPT about the event */
482 xpt_async(AC_BUS_RESET, ch->path, NULL);
483 xpt_release_simq(ch->sim, TRUE);
489 ata_suspend(device_t dev)
491 struct ata_channel *ch;
493 /* check for valid device */
494 if (!dev || !(ch = device_get_softc(dev)))
498 if (ch->flags & ATA_PERIODIC_POLL)
499 callout_drain(&ch->poll_callout);
500 mtx_lock(&ch->state_mtx);
501 xpt_freeze_simq(ch->sim, 1);
502 while (ch->state != ATA_IDLE)
503 msleep(ch, &ch->state_mtx, PRIBIO, "atasusp", hz/100);
504 mtx_unlock(&ch->state_mtx);
506 /* wait for the channel to be IDLE or detached before suspending */
508 mtx_lock(&ch->state_mtx);
509 if (ch->state == ATA_IDLE) {
510 ch->state = ATA_ACTIVE;
511 mtx_unlock(&ch->state_mtx);
514 mtx_unlock(&ch->state_mtx);
515 tsleep(ch, PRIBIO, "atasusp", hz/10);
517 ATA_LOCKING(dev, ATA_LF_UNLOCK);
523 ata_resume(device_t dev)
525 struct ata_channel *ch;
528 /* check for valid device */
529 if (!dev || !(ch = device_get_softc(dev)))
533 mtx_lock(&ch->state_mtx);
534 error = ata_reinit(dev);
535 xpt_release_simq(ch->sim, TRUE);
536 mtx_unlock(&ch->state_mtx);
537 if (ch->flags & ATA_PERIODIC_POLL)
538 callout_reset(&ch->poll_callout, hz, ata_periodic_poll, ch);
540 /* reinit the devices, we dont know what mode/state they are in */
541 error = ata_reinit(dev);
542 /* kick off requests on the queue */
549 ata_interrupt(void *data)
552 struct ata_channel *ch = (struct ata_channel *)data;
554 mtx_lock(&ch->state_mtx);
555 xpt_batch_start(ch->sim);
557 ata_interrupt_locked(data);
559 xpt_batch_done(ch->sim);
560 mtx_unlock(&ch->state_mtx);
565 ata_interrupt_locked(void *data)
567 struct ata_channel *ch = (struct ata_channel *)data;
568 struct ata_request *request;
571 mtx_lock(&ch->state_mtx);
574 /* ignore interrupt if its not for us */
575 if (ch->hw.status && !ch->hw.status(ch->dev))
578 /* do we have a running request */
579 if (!(request = ch->running))
582 ATA_DEBUG_RQ(request, "interrupt");
584 /* safetycheck for the right state */
585 if (ch->state == ATA_IDLE) {
586 device_printf(request->dev, "interrupt on idle channel ignored\n");
591 * we have the HW locks, so end the transaction for this request
592 * if it finishes immediately otherwise wait for next interrupt
594 if (ch->hw.end_transaction(request) == ATA_OP_FINISHED) {
596 if (ch->state == ATA_ACTIVE)
597 ch->state = ATA_IDLE;
599 ata_cam_end_transaction(ch->dev, request);
601 mtx_unlock(&ch->state_mtx);
602 ATA_LOCKING(ch->dev, ATA_LF_UNLOCK);
609 mtx_unlock(&ch->state_mtx);
615 ata_periodic_poll(void *data)
617 struct ata_channel *ch = (struct ata_channel *)data;
619 callout_reset(&ch->poll_callout, hz, ata_periodic_poll, ch);
625 ata_print_cable(device_t dev, u_int8_t *who)
628 "DMA limited to UDMA33, %s found non-ATA66 cable\n", who);
633 ata_check_80pin(device_t dev, int mode)
635 struct ata_device *atadev = device_get_softc(dev);
637 if (!ata_dma_check_80pin) {
639 device_printf(dev, "Skipping 80pin cable check\n");
643 if (mode > ATA_UDMA2 && !(atadev->param.hwres & ATA_CABLE_ID)) {
644 ata_print_cable(dev, "device");
653 ata_setmode(device_t dev)
655 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
656 struct ata_device *atadev = device_get_softc(dev);
657 int error, mode, pmode;
661 pmode = mode = ata_limit_mode(dev, mode, ATA_DMA_MAX);
662 mode = ATA_SETMODE(device_get_parent(dev), atadev->unit, mode);
663 if ((ch->flags & (ATA_CHECKS_CABLE | ATA_SATA)) == 0)
664 mode = ata_check_80pin(dev, mode);
665 } while (pmode != mode); /* Interate till successfull negotiation. */
666 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
668 device_printf(dev, "%ssetting %s\n",
669 (error) ? "FAILURE " : "", ata_mode2str(mode));
675 * device related interfaces
679 ata_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
680 int32_t flag, struct thread *td)
682 device_t device, *children;
683 struct ata_ioc_devices *devices = (struct ata_ioc_devices *)data;
684 int *value = (int *)data;
685 int i, nchildren, error = ENOTTY;
688 case IOCATAGMAXCHANNEL:
689 /* In case we have channel 0..n this will return n+1. */
690 *value = devclass_get_maxunit(ata_devclass);
695 if (*value >= devclass_get_maxunit(ata_devclass) ||
696 !(device = devclass_get_device(ata_devclass, *value)) ||
697 !device_is_attached(device))
699 error = ata_reinit(device);
703 if (*value >= devclass_get_maxunit(ata_devclass) ||
704 !(device = devclass_get_device(ata_devclass, *value)) ||
705 !device_is_attached(device))
707 error = DEVICE_ATTACH(device);
711 if (*value >= devclass_get_maxunit(ata_devclass) ||
712 !(device = devclass_get_device(ata_devclass, *value)) ||
713 !device_is_attached(device))
715 error = DEVICE_DETACH(device);
719 if (devices->channel >= devclass_get_maxunit(ata_devclass) ||
720 !(device = devclass_get_device(ata_devclass, devices->channel)) ||
721 !device_is_attached(device))
723 bzero(devices->name[0], 32);
724 bzero(&devices->params[0], sizeof(struct ata_params));
725 bzero(devices->name[1], 32);
726 bzero(&devices->params[1], sizeof(struct ata_params));
727 if (!device_get_children(device, &children, &nchildren)) {
728 for (i = 0; i < nchildren; i++) {
729 if (children[i] && device_is_attached(children[i])) {
730 struct ata_device *atadev = device_get_softc(children[i]);
732 if (atadev->unit == ATA_MASTER) { /* XXX SOS PM */
733 strncpy(devices->name[0],
734 device_get_nameunit(children[i]), 32);
735 bcopy(&atadev->param, &devices->params[0],
736 sizeof(struct ata_params));
738 if (atadev->unit == ATA_SLAVE) { /* XXX SOS PM */
739 strncpy(devices->name[1],
740 device_get_nameunit(children[i]), 32);
741 bcopy(&atadev->param, &devices->params[1],
742 sizeof(struct ata_params));
746 free(children, M_TEMP);
754 if (ata_raid_ioctl_func)
755 error = ata_raid_ioctl_func(cmd, data);
763 ata_device_ioctl(device_t dev, u_long cmd, caddr_t data)
765 struct ata_device *atadev = device_get_softc(dev);
766 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
767 struct ata_ioc_request *ioc_request = (struct ata_ioc_request *)data;
768 struct ata_params *params = (struct ata_params *)data;
769 int *mode = (int *)data;
770 struct ata_request *request;
776 if (ioc_request->count >
777 (ch->dma.max_iosize ? ch->dma.max_iosize : DFLTPHYS)) {
780 if (!(buf = malloc(ioc_request->count, M_ATA, M_NOWAIT))) {
783 if (!(request = ata_alloc_request())) {
787 request->dev = atadev->dev;
788 if (ioc_request->flags & ATA_CMD_WRITE) {
789 error = copyin(ioc_request->data, buf, ioc_request->count);
792 ata_free_request(request);
796 if (ioc_request->flags & ATA_CMD_ATAPI) {
797 request->flags = ATA_R_ATAPI;
798 bcopy(ioc_request->u.atapi.ccb, request->u.atapi.ccb, 16);
801 request->u.ata.command = ioc_request->u.ata.command;
802 request->u.ata.feature = ioc_request->u.ata.feature;
803 request->u.ata.lba = ioc_request->u.ata.lba;
804 request->u.ata.count = ioc_request->u.ata.count;
806 request->timeout = ioc_request->timeout;
808 request->bytecount = ioc_request->count;
809 request->transfersize = request->bytecount;
810 if (ioc_request->flags & ATA_CMD_CONTROL)
811 request->flags |= ATA_R_CONTROL;
812 if (ioc_request->flags & ATA_CMD_READ)
813 request->flags |= ATA_R_READ;
814 if (ioc_request->flags & ATA_CMD_WRITE)
815 request->flags |= ATA_R_WRITE;
816 ata_queue_request(request);
817 if (request->flags & ATA_R_ATAPI) {
818 bcopy(&request->u.atapi.sense, &ioc_request->u.atapi.sense,
819 sizeof(struct atapi_sense));
822 ioc_request->u.ata.command = request->u.ata.command;
823 ioc_request->u.ata.feature = request->u.ata.feature;
824 ioc_request->u.ata.lba = request->u.ata.lba;
825 ioc_request->u.ata.count = request->u.ata.count;
827 ioc_request->error = request->result;
828 if (ioc_request->flags & ATA_CMD_READ)
829 error = copyout(buf, ioc_request->data, ioc_request->count);
833 ata_free_request(request);
837 ata_getparam(atadev, 0);
838 bcopy(&atadev->param, params, sizeof(struct ata_params));
842 atadev->mode = *mode;
847 *mode = atadev->mode |
848 (ATA_GETREV(device_get_parent(dev), atadev->unit) << 8);
850 case IOCATASSPINDOWN:
851 atadev->spindown = *mode;
853 case IOCATAGSPINDOWN:
854 *mode = atadev->spindown;
864 ata_boot_attach(void)
866 struct ata_channel *ch;
869 mtx_lock(&Giant); /* newbus suckage it needs Giant */
871 /* kick off probe and attach on all channels */
872 for (ctlr = 0; ctlr < devclass_get_maxunit(ata_devclass); ctlr++) {
873 if ((ch = devclass_get_softc(ata_devclass, ctlr))) {
874 ata_identify(ch->dev);
878 /* release the hook that got us here, we are only needed once during boot */
879 if (ata_delayed_attach) {
880 config_intrhook_disestablish(ata_delayed_attach);
881 free(ata_delayed_attach, M_TEMP);
882 ata_delayed_attach = NULL;
885 mtx_unlock(&Giant); /* newbus suckage dealt with, release Giant */
890 * misc support functions
894 ata_add_child(device_t parent, struct ata_device *atadev, int unit)
898 if ((child = device_add_child(parent, (unit < 0) ? NULL : "ad", unit))) {
899 device_set_softc(child, atadev);
902 atadev->max_iosize = DEV_BSIZE;
903 atadev->mode = ATA_PIO_MAX;
911 ata_getparam(struct ata_device *atadev, int init)
913 struct ata_channel *ch = device_get_softc(device_get_parent(atadev->dev));
914 struct ata_request *request;
917 u_int8_t command = 0;
918 int error = ENOMEM, retries = 2, mode = -1;
920 if (ch->devices & (ATA_ATA_MASTER << atadev->unit))
921 command = ATA_ATA_IDENTIFY;
922 if (ch->devices & (ATA_ATAPI_MASTER << atadev->unit))
923 command = ATA_ATAPI_IDENTIFY;
927 while (retries-- > 0 && error) {
928 if (!(request = ata_alloc_request()))
930 request->dev = atadev->dev;
931 request->timeout = 1;
932 request->retries = 0;
933 request->u.ata.command = command;
934 request->flags = (ATA_R_READ|ATA_R_AT_HEAD|ATA_R_DIRECT);
936 request->flags |= ATA_R_QUIET;
937 request->data = (void *)&atadev->param;
938 request->bytecount = sizeof(struct ata_params);
939 request->donecount = 0;
940 request->transfersize = DEV_BSIZE;
941 ata_queue_request(request);
942 error = request->result;
943 ata_free_request(request);
946 if (!error && (isprint(atadev->param.model[0]) ||
947 isprint(atadev->param.model[1]))) {
948 struct ata_params *atacap = &atadev->param;
951 for (ptr = (int16_t *)atacap;
952 ptr < (int16_t *)atacap + sizeof(struct ata_params)/2; ptr++) {
953 *ptr = le16toh(*ptr);
955 if (!(!strncmp(atacap->model, "FX", 2) ||
956 !strncmp(atacap->model, "NEC", 3) ||
957 !strncmp(atacap->model, "Pioneer", 7) ||
958 !strncmp(atacap->model, "SHARP", 5))) {
959 bswap(atacap->model, sizeof(atacap->model));
960 bswap(atacap->revision, sizeof(atacap->revision));
961 bswap(atacap->serial, sizeof(atacap->serial));
963 btrim(atacap->model, sizeof(atacap->model));
964 bpack(atacap->model, atacap->model, sizeof(atacap->model));
965 btrim(atacap->revision, sizeof(atacap->revision));
966 bpack(atacap->revision, atacap->revision, sizeof(atacap->revision));
967 btrim(atacap->serial, sizeof(atacap->serial));
968 bpack(atacap->serial, atacap->serial, sizeof(atacap->serial));
971 printf("ata%d-%s: pio=%s wdma=%s udma=%s cable=%s wire\n",
972 device_get_unit(ch->dev),
973 ata_unit2str(atadev),
974 ata_mode2str(ata_pmode(atacap)),
975 ata_mode2str(ata_wmode(atacap)),
976 ata_mode2str(ata_umode(atacap)),
977 (atacap->hwres & ATA_CABLE_ID) ? "80":"40");
982 sprintf(buffer, "%.40s/%.8s", atacap->model, atacap->revision);
983 device_set_desc_copy(atadev->dev, buffer);
984 if ((atadev->param.config & ATA_PROTO_ATAPI) &&
985 (atadev->param.config != ATA_CFA_MAGIC1) &&
986 (atadev->param.config != ATA_CFA_MAGIC2)) {
988 (atadev->param.config & ATA_DRQ_MASK) != ATA_DRQ_INTR &&
989 ata_umode(&atadev->param) >= ATA_UDMA2)
990 atadev->mode = ATA_DMA_MAX;
994 (ata_umode(&atadev->param) > 0 ||
995 ata_wmode(&atadev->param) > 0))
996 atadev->mode = ATA_DMA_MAX;
998 snprintf(buf, sizeof(buf), "dev%d.mode", atadev->unit);
999 if (resource_string_value(device_get_name(ch->dev),
1000 device_get_unit(ch->dev), buf, &res) == 0)
1001 mode = ata_str2mode(res);
1002 else if (resource_string_value(device_get_name(ch->dev),
1003 device_get_unit(ch->dev), "mode", &res) == 0)
1004 mode = ata_str2mode(res);
1006 atadev->mode = mode;
1019 ata_identify(device_t dev)
1021 struct ata_channel *ch = device_get_softc(dev);
1022 struct ata_device *atadev;
1024 device_t child, master = NULL;
1025 int nchildren, i, n = ch->devices;
1028 device_printf(dev, "Identifying devices: %08x\n", ch->devices);
1031 /* Skip existing devices. */
1032 if (!device_get_children(dev, &children, &nchildren)) {
1033 for (i = 0; i < nchildren; i++) {
1034 if (children[i] && (atadev = device_get_softc(children[i])))
1035 n &= ~((ATA_ATA_MASTER | ATA_ATAPI_MASTER) << atadev->unit);
1037 free(children, M_TEMP);
1039 /* Create new devices. */
1041 device_printf(dev, "New devices: %08x\n", n);
1046 for (i = 0; i < ATA_PM; ++i) {
1047 if (n & (((ATA_ATA_MASTER | ATA_ATAPI_MASTER) << i))) {
1050 if (!(atadev = malloc(sizeof(struct ata_device),
1051 M_ATA, M_NOWAIT | M_ZERO))) {
1052 device_printf(dev, "out of memory\n");
1056 #ifdef ATA_STATIC_ID
1057 if (n & (ATA_ATA_MASTER << i))
1058 unit = (device_get_unit(dev) << 1) + i;
1060 if ((child = ata_add_child(dev, atadev, unit))) {
1062 * PATA slave should be identified first, to allow
1063 * device cable detection on master to work properly.
1065 if (i == 0 && (n & ATA_PORTMULTIPLIER) == 0 &&
1066 (n & ((ATA_ATA_MASTER | ATA_ATAPI_MASTER) << 1)) != 0) {
1070 if (ata_getparam(atadev, 1)) {
1071 device_delete_child(dev, child);
1072 free(atadev, M_ATA);
1076 free(atadev, M_ATA);
1080 atadev = device_get_softc(master);
1081 if (ata_getparam(atadev, 1)) {
1082 device_delete_child(dev, master);
1083 free(atadev, M_ATA);
1086 bus_generic_probe(dev);
1087 bus_generic_attach(dev);
1094 ata_default_registers(device_t dev)
1096 struct ata_channel *ch = device_get_softc(dev);
1098 /* fill in the defaults from whats setup already */
1099 ch->r_io[ATA_ERROR].res = ch->r_io[ATA_FEATURE].res;
1100 ch->r_io[ATA_ERROR].offset = ch->r_io[ATA_FEATURE].offset;
1101 ch->r_io[ATA_IREASON].res = ch->r_io[ATA_COUNT].res;
1102 ch->r_io[ATA_IREASON].offset = ch->r_io[ATA_COUNT].offset;
1103 ch->r_io[ATA_STATUS].res = ch->r_io[ATA_COMMAND].res;
1104 ch->r_io[ATA_STATUS].offset = ch->r_io[ATA_COMMAND].offset;
1105 ch->r_io[ATA_ALTSTAT].res = ch->r_io[ATA_CONTROL].res;
1106 ch->r_io[ATA_ALTSTAT].offset = ch->r_io[ATA_CONTROL].offset;
1110 ata_modify_if_48bit(struct ata_request *request)
1112 struct ata_channel *ch = device_get_softc(request->parent);
1113 struct ata_device *atadev = device_get_softc(request->dev);
1115 request->flags &= ~ATA_R_48BIT;
1117 if (((request->u.ata.lba + request->u.ata.count) >= ATA_MAX_28BIT_LBA ||
1118 request->u.ata.count > 256) &&
1119 atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) {
1121 /* translate command into 48bit version */
1122 switch (request->u.ata.command) {
1124 request->u.ata.command = ATA_READ48;
1127 request->u.ata.command = ATA_READ_MUL48;
1130 if (ch->flags & ATA_NO_48BIT_DMA) {
1131 if (request->transfersize > DEV_BSIZE)
1132 request->u.ata.command = ATA_READ_MUL48;
1134 request->u.ata.command = ATA_READ48;
1135 request->flags &= ~ATA_R_DMA;
1138 request->u.ata.command = ATA_READ_DMA48;
1140 case ATA_READ_DMA_QUEUED:
1141 if (ch->flags & ATA_NO_48BIT_DMA) {
1142 if (request->transfersize > DEV_BSIZE)
1143 request->u.ata.command = ATA_READ_MUL48;
1145 request->u.ata.command = ATA_READ48;
1146 request->flags &= ~ATA_R_DMA;
1149 request->u.ata.command = ATA_READ_DMA_QUEUED48;
1152 request->u.ata.command = ATA_WRITE48;
1155 request->u.ata.command = ATA_WRITE_MUL48;
1158 if (ch->flags & ATA_NO_48BIT_DMA) {
1159 if (request->transfersize > DEV_BSIZE)
1160 request->u.ata.command = ATA_WRITE_MUL48;
1162 request->u.ata.command = ATA_WRITE48;
1163 request->flags &= ~ATA_R_DMA;
1166 request->u.ata.command = ATA_WRITE_DMA48;
1168 case ATA_WRITE_DMA_QUEUED:
1169 if (ch->flags & ATA_NO_48BIT_DMA) {
1170 if (request->transfersize > DEV_BSIZE)
1171 request->u.ata.command = ATA_WRITE_MUL48;
1173 request->u.ata.command = ATA_WRITE48;
1174 request->u.ata.command = ATA_WRITE48;
1175 request->flags &= ~ATA_R_DMA;
1178 request->u.ata.command = ATA_WRITE_DMA_QUEUED48;
1180 case ATA_FLUSHCACHE:
1181 request->u.ata.command = ATA_FLUSHCACHE48;
1183 case ATA_SET_MAX_ADDRESS:
1184 request->u.ata.command = ATA_SET_MAX_ADDRESS48;
1189 request->flags |= ATA_R_48BIT;
1191 else if (atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) {
1193 /* translate command into 48bit version */
1194 switch (request->u.ata.command) {
1195 case ATA_FLUSHCACHE:
1196 request->u.ata.command = ATA_FLUSHCACHE48;
1198 case ATA_READ_NATIVE_MAX_ADDRESS:
1199 request->u.ata.command = ATA_READ_NATIVE_MAX_ADDRESS48;
1201 case ATA_SET_MAX_ADDRESS:
1202 request->u.ata.command = ATA_SET_MAX_ADDRESS48;
1207 request->flags |= ATA_R_48BIT;
1212 ata_udelay(int interval)
1214 /* for now just use DELAY, the timer/sleep subsytems are not there yet */
1215 if (1 || interval < (1000000/hz) || ata_delayed_attach)
1218 pause("ataslp", interval/(1000000/hz));
1223 ata_unit2str(struct ata_device *atadev)
1225 struct ata_channel *ch = device_get_softc(device_get_parent(atadev->dev));
1228 if (ch->devices & ATA_PORTMULTIPLIER)
1229 sprintf(str, "port%d", atadev->unit);
1231 sprintf(str, "%s", atadev->unit == ATA_MASTER ? "master" : "slave");
1237 ata_mode2str(int mode)
1240 case -1: return "UNSUPPORTED";
1241 case ATA_PIO0: return "PIO0";
1242 case ATA_PIO1: return "PIO1";
1243 case ATA_PIO2: return "PIO2";
1244 case ATA_PIO3: return "PIO3";
1245 case ATA_PIO4: return "PIO4";
1246 case ATA_WDMA0: return "WDMA0";
1247 case ATA_WDMA1: return "WDMA1";
1248 case ATA_WDMA2: return "WDMA2";
1249 case ATA_UDMA0: return "UDMA16";
1250 case ATA_UDMA1: return "UDMA25";
1251 case ATA_UDMA2: return "UDMA33";
1252 case ATA_UDMA3: return "UDMA40";
1253 case ATA_UDMA4: return "UDMA66";
1254 case ATA_UDMA5: return "UDMA100";
1255 case ATA_UDMA6: return "UDMA133";
1256 case ATA_SA150: return "SATA150";
1257 case ATA_SA300: return "SATA300";
1259 if (mode & ATA_DMA_MASK)
1267 ata_str2mode(const char *str)
1270 if (!strcasecmp(str, "PIO0")) return (ATA_PIO0);
1271 if (!strcasecmp(str, "PIO1")) return (ATA_PIO1);
1272 if (!strcasecmp(str, "PIO2")) return (ATA_PIO2);
1273 if (!strcasecmp(str, "PIO3")) return (ATA_PIO3);
1274 if (!strcasecmp(str, "PIO4")) return (ATA_PIO4);
1275 if (!strcasecmp(str, "WDMA0")) return (ATA_WDMA0);
1276 if (!strcasecmp(str, "WDMA1")) return (ATA_WDMA1);
1277 if (!strcasecmp(str, "WDMA2")) return (ATA_WDMA2);
1278 if (!strcasecmp(str, "UDMA0")) return (ATA_UDMA0);
1279 if (!strcasecmp(str, "UDMA16")) return (ATA_UDMA0);
1280 if (!strcasecmp(str, "UDMA1")) return (ATA_UDMA1);
1281 if (!strcasecmp(str, "UDMA25")) return (ATA_UDMA1);
1282 if (!strcasecmp(str, "UDMA2")) return (ATA_UDMA2);
1283 if (!strcasecmp(str, "UDMA33")) return (ATA_UDMA2);
1284 if (!strcasecmp(str, "UDMA3")) return (ATA_UDMA3);
1285 if (!strcasecmp(str, "UDMA44")) return (ATA_UDMA3);
1286 if (!strcasecmp(str, "UDMA4")) return (ATA_UDMA4);
1287 if (!strcasecmp(str, "UDMA66")) return (ATA_UDMA4);
1288 if (!strcasecmp(str, "UDMA5")) return (ATA_UDMA5);
1289 if (!strcasecmp(str, "UDMA100")) return (ATA_UDMA5);
1290 if (!strcasecmp(str, "UDMA6")) return (ATA_UDMA6);
1291 if (!strcasecmp(str, "UDMA133")) return (ATA_UDMA6);
1297 ata_satarev2str(int rev)
1301 case 1: return "SATA 1.5Gb/s";
1302 case 2: return "SATA 3Gb/s";
1303 case 3: return "SATA 6Gb/s";
1304 case 0xff: return "SATA";
1305 default: return "???";
1311 ata_atapi(device_t dev, int target)
1313 struct ata_channel *ch = device_get_softc(dev);
1315 return (ch->devices & (ATA_ATAPI_MASTER << target));
1320 ata_pmode(struct ata_params *ap)
1322 if (ap->atavalid & ATA_FLAG_64_70) {
1323 if (ap->apiomodes & 0x02)
1325 if (ap->apiomodes & 0x01)
1328 if (ap->mwdmamodes & 0x04)
1330 if (ap->mwdmamodes & 0x02)
1332 if (ap->mwdmamodes & 0x01)
1334 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200)
1336 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100)
1338 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000)
1346 ata_wmode(struct ata_params *ap)
1348 if (ap->mwdmamodes & 0x04)
1350 if (ap->mwdmamodes & 0x02)
1352 if (ap->mwdmamodes & 0x01)
1360 ata_umode(struct ata_params *ap)
1362 if (ap->atavalid & ATA_FLAG_88) {
1363 if (ap->udmamodes & 0x40)
1365 if (ap->udmamodes & 0x20)
1367 if (ap->udmamodes & 0x10)
1369 if (ap->udmamodes & 0x08)
1371 if (ap->udmamodes & 0x04)
1373 if (ap->udmamodes & 0x02)
1375 if (ap->udmamodes & 0x01)
1384 ata_limit_mode(device_t dev, int mode, int maxmode)
1386 struct ata_device *atadev = device_get_softc(dev);
1388 if (maxmode && mode > maxmode)
1391 if (mode >= ATA_UDMA0 && ata_umode(&atadev->param) > 0)
1392 return min(mode, ata_umode(&atadev->param));
1394 if (mode >= ATA_WDMA0 && ata_wmode(&atadev->param) > 0)
1395 return min(mode, ata_wmode(&atadev->param));
1397 if (mode > ata_pmode(&atadev->param))
1398 return min(mode, ata_pmode(&atadev->param));
1406 bswap(int8_t *buf, int len)
1408 u_int16_t *ptr = (u_int16_t*)(buf + len);
1410 while (--ptr >= (u_int16_t*)buf)
1417 btrim(int8_t *buf, int len)
1421 for (ptr = buf; ptr < buf+len; ++ptr)
1422 if (!*ptr || *ptr == '_')
1424 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
1431 bpack(int8_t *src, int8_t *dst, int len)
1435 for (i = j = blank = 0 ; i < len; i++) {
1436 if (blank && src[i] == ' ') continue;
1437 if (blank && src[i] != ' ') {
1442 if (src[i] == ' ') {
1456 ata_cam_begin_transaction(device_t dev, union ccb *ccb)
1458 struct ata_channel *ch = device_get_softc(dev);
1459 struct ata_request *request;
1461 if (!(request = ata_alloc_request())) {
1462 device_printf(dev, "FAILURE - out of memory in start\n");
1463 ccb->ccb_h.status = CAM_REQ_INVALID;
1467 bzero(request, sizeof(*request));
1470 request->dev = NULL;
1471 request->parent = dev;
1472 request->unit = ccb->ccb_h.target_id;
1473 if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1474 request->data = ccb->ataio.data_ptr;
1475 request->bytecount = ccb->ataio.dxfer_len;
1476 request->u.ata.command = ccb->ataio.cmd.command;
1477 request->u.ata.feature = ((uint16_t)ccb->ataio.cmd.features_exp << 8) |
1478 (uint16_t)ccb->ataio.cmd.features;
1479 request->u.ata.count = ((uint16_t)ccb->ataio.cmd.sector_count_exp << 8) |
1480 (uint16_t)ccb->ataio.cmd.sector_count;
1481 if (ccb->ataio.cmd.flags & CAM_ATAIO_48BIT) {
1482 request->flags |= ATA_R_48BIT;
1483 request->u.ata.lba =
1484 ((uint64_t)ccb->ataio.cmd.lba_high_exp << 40) |
1485 ((uint64_t)ccb->ataio.cmd.lba_mid_exp << 32) |
1486 ((uint64_t)ccb->ataio.cmd.lba_low_exp << 24);
1488 request->u.ata.lba =
1489 ((uint64_t)(ccb->ataio.cmd.device & 0x0f) << 24);
1491 request->u.ata.lba |= ((uint64_t)ccb->ataio.cmd.lba_high << 16) |
1492 ((uint64_t)ccb->ataio.cmd.lba_mid << 8) |
1493 (uint64_t)ccb->ataio.cmd.lba_low;
1494 if (ccb->ataio.cmd.flags & CAM_ATAIO_NEEDRESULT)
1495 request->flags |= ATA_R_NEEDRESULT;
1496 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE &&
1497 ccb->ataio.cmd.flags & CAM_ATAIO_DMA)
1498 request->flags |= ATA_R_DMA;
1499 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1500 request->flags |= ATA_R_READ;
1501 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
1502 request->flags |= ATA_R_WRITE;
1503 if (ccb->ataio.cmd.command == ATA_READ_MUL ||
1504 ccb->ataio.cmd.command == ATA_READ_MUL48 ||
1505 ccb->ataio.cmd.command == ATA_WRITE_MUL ||
1506 ccb->ataio.cmd.command == ATA_WRITE_MUL48) {
1507 request->transfersize = min(request->bytecount,
1508 ch->curr[ccb->ccb_h.target_id].bytecount);
1510 request->transfersize = min(request->bytecount, 512);
1512 request->data = ccb->csio.data_ptr;
1513 request->bytecount = ccb->csio.dxfer_len;
1514 bcopy((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
1515 ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes,
1516 request->u.atapi.ccb, ccb->csio.cdb_len);
1517 request->flags |= ATA_R_ATAPI;
1518 if (ch->curr[ccb->ccb_h.target_id].atapi == 16)
1519 request->flags |= ATA_R_ATAPI16;
1520 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE &&
1521 ch->curr[ccb->ccb_h.target_id].mode >= ATA_DMA)
1522 request->flags |= ATA_R_DMA;
1523 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1524 request->flags |= ATA_R_READ;
1525 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
1526 request->flags |= ATA_R_WRITE;
1527 request->transfersize = min(request->bytecount,
1528 ch->curr[ccb->ccb_h.target_id].bytecount);
1530 request->retries = 0;
1531 request->timeout = (ccb->ccb_h.timeout + 999) / 1000;
1532 callout_init_mtx(&request->callout, &ch->state_mtx, CALLOUT_RETURNUNLOCKED);
1535 ch->running = request;
1536 ch->state = ATA_ACTIVE;
1537 if (ch->hw.begin_transaction(request) == ATA_OP_FINISHED) {
1539 ch->state = ATA_IDLE;
1540 ata_cam_end_transaction(dev, request);
1546 ata_cam_request_sense(device_t dev, struct ata_request *request)
1548 struct ata_channel *ch = device_get_softc(dev);
1549 union ccb *ccb = request->ccb;
1551 ch->requestsense = 1;
1553 bzero(request, sizeof(*request));
1554 request->dev = NULL;
1555 request->parent = dev;
1556 request->unit = ccb->ccb_h.target_id;
1557 request->data = (void *)&ccb->csio.sense_data;
1558 request->bytecount = ccb->csio.sense_len;
1559 request->u.atapi.ccb[0] = ATAPI_REQUEST_SENSE;
1560 request->u.atapi.ccb[4] = ccb->csio.sense_len;
1561 request->flags |= ATA_R_ATAPI;
1562 if (ch->curr[ccb->ccb_h.target_id].atapi == 16)
1563 request->flags |= ATA_R_ATAPI16;
1564 if (ch->curr[ccb->ccb_h.target_id].mode >= ATA_DMA)
1565 request->flags |= ATA_R_DMA;
1566 request->flags |= ATA_R_READ;
1567 request->transfersize = min(request->bytecount,
1568 ch->curr[ccb->ccb_h.target_id].bytecount);
1569 request->retries = 0;
1570 request->timeout = (ccb->ccb_h.timeout + 999) / 1000;
1571 callout_init_mtx(&request->callout, &ch->state_mtx, CALLOUT_RETURNUNLOCKED);
1574 ch->running = request;
1575 ch->state = ATA_ACTIVE;
1576 if (ch->hw.begin_transaction(request) == ATA_OP_FINISHED) {
1578 ch->state = ATA_IDLE;
1579 ata_cam_end_transaction(dev, request);
1585 ata_cam_process_sense(device_t dev, struct ata_request *request)
1587 struct ata_channel *ch = device_get_softc(dev);
1588 union ccb *ccb = request->ccb;
1591 ch->requestsense = 0;
1593 if (request->flags & ATA_R_TIMEOUT)
1595 if ((request->flags & ATA_R_TIMEOUT) == 0 &&
1596 (request->status & ATA_S_ERROR) == 0 &&
1597 request->result == 0) {
1598 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
1600 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1601 ccb->ccb_h.status |= CAM_AUTOSENSE_FAIL;
1604 ata_free_request(request);
1606 /* Do error recovery if needed. */
1612 ata_cam_end_transaction(device_t dev, struct ata_request *request)
1614 struct ata_channel *ch = device_get_softc(dev);
1615 union ccb *ccb = request->ccb;
1618 if (ch->requestsense) {
1619 ata_cam_process_sense(dev, request);
1623 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1624 if (request->flags & ATA_R_TIMEOUT) {
1625 xpt_freeze_simq(ch->sim, 1);
1626 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1627 ccb->ccb_h.status |= CAM_CMD_TIMEOUT | CAM_RELEASE_SIMQ;
1629 } else if (request->status & ATA_S_ERROR) {
1630 if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1631 ccb->ccb_h.status |= CAM_ATA_STATUS_ERROR;
1633 ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1634 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1636 } else if (request->result == ERESTART)
1637 ccb->ccb_h.status |= CAM_REQUEUE_REQ;
1638 else if (request->result != 0)
1639 ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
1641 ccb->ccb_h.status |= CAM_REQ_CMP;
1642 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP &&
1643 !(ccb->ccb_h.status & CAM_DEV_QFRZN)) {
1644 xpt_freeze_devq(ccb->ccb_h.path, 1);
1645 ccb->ccb_h.status |= CAM_DEV_QFRZN;
1647 if (ccb->ccb_h.func_code == XPT_ATA_IO &&
1648 ((request->status & ATA_S_ERROR) ||
1649 (ccb->ataio.cmd.flags & CAM_ATAIO_NEEDRESULT))) {
1650 struct ata_res *res = &ccb->ataio.res;
1651 res->status = request->status;
1652 res->error = request->error;
1653 res->lba_low = request->u.ata.lba;
1654 res->lba_mid = request->u.ata.lba >> 8;
1655 res->lba_high = request->u.ata.lba >> 16;
1656 res->device = request->u.ata.lba >> 24;
1657 res->lba_low_exp = request->u.ata.lba >> 24;
1658 res->lba_mid_exp = request->u.ata.lba >> 32;
1659 res->lba_high_exp = request->u.ata.lba >> 40;
1660 res->sector_count = request->u.ata.count;
1661 res->sector_count_exp = request->u.ata.count >> 8;
1663 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1664 if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1666 ccb->ataio.dxfer_len - request->donecount;
1669 ccb->csio.dxfer_len - request->donecount;
1672 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_SCSI_STATUS_ERROR &&
1673 (ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)
1674 ata_cam_request_sense(dev, request);
1676 ata_free_request(request);
1679 /* Do error recovery if needed. */
1685 ata_check_ids(device_t dev, union ccb *ccb)
1687 struct ata_channel *ch = device_get_softc(dev);
1689 if (ccb->ccb_h.target_id > ((ch->flags & ATA_NO_SLAVE) ? 0 : 1)) {
1690 ccb->ccb_h.status = CAM_TID_INVALID;
1694 if (ccb->ccb_h.target_lun != 0) {
1695 ccb->ccb_h.status = CAM_LUN_INVALID;
1703 ataaction(struct cam_sim *sim, union ccb *ccb)
1706 struct ata_channel *ch;
1708 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ataaction func_code=%x\n",
1709 ccb->ccb_h.func_code));
1711 ch = (struct ata_channel *)cam_sim_softc(sim);
1713 switch (ccb->ccb_h.func_code) {
1714 /* Common cases first */
1715 case XPT_ATA_IO: /* Execute the requested I/O operation */
1717 if (ata_check_ids(dev, ccb))
1719 if ((ch->devices & ((ATA_ATA_MASTER | ATA_ATAPI_MASTER)
1720 << ccb->ccb_h.target_id)) == 0) {
1721 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
1725 device_printf(dev, "already running!\n");
1726 if (ccb->ccb_h.func_code == XPT_ATA_IO &&
1727 (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) &&
1728 (ccb->ataio.cmd.control & ATA_A_RESET)) {
1729 struct ata_res *res = &ccb->ataio.res;
1731 bzero(res, sizeof(*res));
1732 if (ch->devices & (ATA_ATA_MASTER << ccb->ccb_h.target_id)) {
1736 res->lba_high = 0xeb;
1737 res->lba_mid = 0x14;
1739 ccb->ccb_h.status = CAM_REQ_CMP;
1742 ata_cam_begin_transaction(dev, ccb);
1744 case XPT_EN_LUN: /* Enable LUN as a target */
1745 case XPT_TARGET_IO: /* Execute target I/O request */
1746 case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */
1747 case XPT_CONT_TARGET_IO: /* Continue Host Target I/O Connection*/
1748 case XPT_ABORT: /* Abort the specified CCB */
1750 ccb->ccb_h.status = CAM_REQ_INVALID;
1752 case XPT_SET_TRAN_SETTINGS:
1754 struct ccb_trans_settings *cts = &ccb->cts;
1755 struct ata_cam_device *d;
1757 if (ata_check_ids(dev, ccb))
1759 if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
1760 d = &ch->curr[ccb->ccb_h.target_id];
1762 d = &ch->user[ccb->ccb_h.target_id];
1763 if (ch->flags & ATA_SATA) {
1764 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_REVISION)
1765 d->revision = cts->xport_specific.sata.revision;
1766 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_MODE) {
1767 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
1768 d->mode = ATA_SETMODE(ch->dev,
1769 ccb->ccb_h.target_id,
1770 cts->xport_specific.sata.mode);
1772 d->mode = cts->xport_specific.sata.mode;
1774 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
1775 d->bytecount = min(8192, cts->xport_specific.sata.bytecount);
1776 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_ATAPI)
1777 d->atapi = cts->xport_specific.sata.atapi;
1778 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1779 d->caps = cts->xport_specific.sata.caps;
1781 if (cts->xport_specific.ata.valid & CTS_ATA_VALID_MODE) {
1782 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
1783 d->mode = ATA_SETMODE(ch->dev,
1784 ccb->ccb_h.target_id,
1785 cts->xport_specific.ata.mode);
1787 d->mode = cts->xport_specific.ata.mode;
1789 if (cts->xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
1790 d->bytecount = cts->xport_specific.ata.bytecount;
1791 if (cts->xport_specific.ata.valid & CTS_ATA_VALID_ATAPI)
1792 d->atapi = cts->xport_specific.ata.atapi;
1794 ccb->ccb_h.status = CAM_REQ_CMP;
1797 case XPT_GET_TRAN_SETTINGS:
1799 struct ccb_trans_settings *cts = &ccb->cts;
1800 struct ata_cam_device *d;
1802 if (ata_check_ids(dev, ccb))
1804 if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
1805 d = &ch->curr[ccb->ccb_h.target_id];
1807 d = &ch->user[ccb->ccb_h.target_id];
1808 cts->protocol = PROTO_UNSPECIFIED;
1809 cts->protocol_version = PROTO_VERSION_UNSPECIFIED;
1810 if (ch->flags & ATA_SATA) {
1811 cts->transport = XPORT_SATA;
1812 cts->transport_version = XPORT_VERSION_UNSPECIFIED;
1813 cts->xport_specific.sata.valid = 0;
1814 cts->xport_specific.sata.mode = d->mode;
1815 cts->xport_specific.sata.valid |= CTS_SATA_VALID_MODE;
1816 cts->xport_specific.sata.bytecount = d->bytecount;
1817 cts->xport_specific.sata.valid |= CTS_SATA_VALID_BYTECOUNT;
1818 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
1819 cts->xport_specific.sata.revision =
1820 ATA_GETREV(dev, ccb->ccb_h.target_id);
1821 if (cts->xport_specific.sata.revision != 0xff) {
1822 cts->xport_specific.sata.valid |=
1823 CTS_SATA_VALID_REVISION;
1825 cts->xport_specific.sata.caps =
1826 d->caps & CTS_SATA_CAPS_D;
1828 cts->xport_specific.sata.caps |=
1829 CTS_SATA_CAPS_H_PMREQ;
1831 cts->xport_specific.sata.caps &=
1832 ch->user[ccb->ccb_h.target_id].caps;
1833 cts->xport_specific.sata.valid |=
1834 CTS_SATA_VALID_CAPS;
1836 cts->xport_specific.sata.revision = d->revision;
1837 cts->xport_specific.sata.valid |= CTS_SATA_VALID_REVISION;
1838 cts->xport_specific.sata.caps = d->caps;
1839 cts->xport_specific.sata.valid |= CTS_SATA_VALID_CAPS;
1841 cts->xport_specific.sata.atapi = d->atapi;
1842 cts->xport_specific.sata.valid |= CTS_SATA_VALID_ATAPI;
1844 cts->transport = XPORT_ATA;
1845 cts->transport_version = XPORT_VERSION_UNSPECIFIED;
1846 cts->xport_specific.ata.valid = 0;
1847 cts->xport_specific.ata.mode = d->mode;
1848 cts->xport_specific.ata.valid |= CTS_ATA_VALID_MODE;
1849 cts->xport_specific.ata.bytecount = d->bytecount;
1850 cts->xport_specific.ata.valid |= CTS_ATA_VALID_BYTECOUNT;
1851 cts->xport_specific.ata.atapi = d->atapi;
1852 cts->xport_specific.ata.valid |= CTS_ATA_VALID_ATAPI;
1854 ccb->ccb_h.status = CAM_REQ_CMP;
1857 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
1858 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
1860 ccb->ccb_h.status = CAM_REQ_CMP;
1862 case XPT_TERM_IO: /* Terminate the I/O process */
1864 ccb->ccb_h.status = CAM_REQ_INVALID;
1866 case XPT_PATH_INQ: /* Path routing inquiry */
1868 struct ccb_pathinq *cpi = &ccb->cpi;
1870 cpi->version_num = 1; /* XXX??? */
1871 cpi->hba_inquiry = PI_SDTR_ABLE;
1872 cpi->target_sprt = 0;
1873 cpi->hba_misc = PIM_SEQSCAN;
1874 cpi->hba_eng_cnt = 0;
1875 if (ch->flags & ATA_NO_SLAVE)
1876 cpi->max_target = 0;
1878 cpi->max_target = 1;
1880 cpi->initiator_id = 0;
1881 cpi->bus_id = cam_sim_bus(sim);
1882 if (ch->flags & ATA_SATA)
1883 cpi->base_transfer_speed = 150000;
1885 cpi->base_transfer_speed = 3300;
1886 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1887 strncpy(cpi->hba_vid, "ATA", HBA_IDLEN);
1888 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1889 cpi->unit_number = cam_sim_unit(sim);
1890 if (ch->flags & ATA_SATA)
1891 cpi->transport = XPORT_SATA;
1893 cpi->transport = XPORT_ATA;
1894 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
1895 cpi->protocol = PROTO_ATA;
1896 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
1897 cpi->maxio = ch->dma.max_iosize ? ch->dma.max_iosize : DFLTPHYS;
1898 cpi->ccb_h.status = CAM_REQ_CMP;
1902 ccb->ccb_h.status = CAM_REQ_INVALID;
1909 atapoll(struct cam_sim *sim)
1911 struct ata_channel *ch = (struct ata_channel *)cam_sim_softc(sim);
1913 ata_interrupt_locked(ch);
1921 ata_module_event_handler(module_t mod, int what, void *arg)
1924 static struct cdev *atacdev;
1930 /* register controlling device */
1931 atacdev = make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "ata");
1934 /* register boot attach to be run when interrupts are enabled */
1935 if (!(ata_delayed_attach = (struct intr_config_hook *)
1936 malloc(sizeof(struct intr_config_hook),
1937 M_TEMP, M_NOWAIT | M_ZERO))) {
1938 printf("ata: malloc of delayed attach hook failed\n");
1941 ata_delayed_attach->ich_func = (void*)ata_boot_attach;
1942 if (config_intrhook_establish(ata_delayed_attach) != 0) {
1943 printf("ata: config_intrhook_establish failed\n");
1944 free(ata_delayed_attach, M_TEMP);
1952 /* deregister controlling device */
1953 destroy_dev(atacdev);
1962 static moduledata_t ata_moduledata = { "ata", ata_module_event_handler, NULL };
1963 DECLARE_MODULE(ata, ata_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
1964 MODULE_VERSION(ata, 1);
1966 MODULE_DEPEND(ata, cam, 1, 1, 1);
1972 ata_request_zone = uma_zcreate("ata_request", sizeof(struct ata_request),
1973 NULL, NULL, NULL, NULL, 0, 0);
1974 ata_composite_zone = uma_zcreate("ata_composite",
1975 sizeof(struct ata_composite),
1976 NULL, NULL, NULL, NULL, 0, 0);
1978 SYSINIT(ata_register, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL);
1983 uma_zdestroy(ata_composite_zone);
1984 uma_zdestroy(ata_request_zone);
1986 SYSUNINIT(ata_unregister, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_uninit, NULL);