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);
81 static void bswap(int8_t *, int);
82 static void btrim(int8_t *, int);
83 static void bpack(int8_t *, int8_t *, int);
84 static void ata_interrupt_locked(void *data);
86 static void ata_periodic_poll(void *data);
90 MALLOC_DEFINE(M_ATA, "ata_generic", "ATA driver generic layer");
91 int (*ata_raid_ioctl_func)(u_long cmd, caddr_t data) = NULL;
92 struct intr_config_hook *ata_delayed_attach = NULL;
93 devclass_t ata_devclass;
94 uma_zone_t ata_request_zone;
95 uma_zone_t ata_composite_zone;
98 int ata_dma_check_80pin = 1;
101 static int ata_dma = 1;
102 static int atapi_dma = 1;
105 SYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters");
106 TUNABLE_INT("hw.ata.ata_dma", &ata_dma);
107 SYSCTL_INT(_hw_ata, OID_AUTO, ata_dma, CTLFLAG_RDTUN, &ata_dma, 0,
108 "ATA disk DMA mode control");
109 TUNABLE_INT("hw.ata.ata_dma_check_80pin", &ata_dma_check_80pin);
110 SYSCTL_INT(_hw_ata, OID_AUTO, ata_dma_check_80pin,
111 CTLFLAG_RW, &ata_dma_check_80pin, 1,
112 "Check for 80pin cable before setting ATA DMA mode");
113 TUNABLE_INT("hw.ata.atapi_dma", &atapi_dma);
114 SYSCTL_INT(_hw_ata, OID_AUTO, atapi_dma, CTLFLAG_RDTUN, &atapi_dma, 0,
115 "ATAPI device DMA mode control");
116 TUNABLE_INT("hw.ata.wc", &ata_wc);
117 SYSCTL_INT(_hw_ata, OID_AUTO, wc, CTLFLAG_RDTUN, &ata_wc, 0,
118 "ATA disk write caching");
119 TUNABLE_INT("hw.ata.setmax", &ata_setmax);
120 SYSCTL_INT(_hw_ata, OID_AUTO, setmax, CTLFLAG_RDTUN, &ata_setmax, 0,
121 "ATA disk set max native address");
124 * newbus device interface related functions
127 ata_probe(device_t dev)
133 ata_attach(device_t dev)
135 struct ata_channel *ch = device_get_softc(dev);
138 struct cam_devq *devq;
144 /* check that we have a virgin channel to attach */
148 /* initialize the softc basics */
150 ch->state = ATA_IDLE;
151 bzero(&ch->state_mtx, sizeof(struct mtx));
152 mtx_init(&ch->state_mtx, "ATA state lock", NULL, MTX_DEF);
153 bzero(&ch->queue_mtx, sizeof(struct mtx));
154 mtx_init(&ch->queue_mtx, "ATA queue lock", NULL, MTX_DEF);
155 TAILQ_INIT(&ch->ata_queue);
156 TASK_INIT(&ch->conntask, 0, ata_conn_event, dev);
158 for (i = 0; i < 16; i++) {
159 ch->user[i].mode = 0;
160 snprintf(buf, sizeof(buf), "dev%d.mode", i);
161 if (resource_string_value(device_get_name(dev),
162 device_get_unit(dev), buf, &res) == 0)
163 mode = ata_str2mode(res);
164 else if (resource_string_value(device_get_name(dev),
165 device_get_unit(dev), "mode", &res) == 0)
166 mode = ata_str2mode(res);
170 ch->user[i].mode = mode;
171 if (ch->flags & ATA_SATA)
172 ch->user[i].bytecount = 8192;
174 ch->user[i].bytecount = MAXPHYS;
175 ch->user[i].caps = 0;
176 ch->curr[i] = ch->user[i];
177 if (ch->pm_level > 0)
178 ch->user[i].caps |= CTS_SATA_CAPS_H_PMREQ;
179 if (ch->pm_level > 1)
180 ch->user[i].caps |= CTS_SATA_CAPS_D_PMREQ;
182 callout_init(&ch->poll_callout, 1);
185 /* reset the controller HW, the channel and device(s) */
186 while (ATA_LOCKING(dev, ATA_LF_LOCK) != ch->unit)
191 ATA_LOCKING(dev, ATA_LF_UNLOCK);
193 /* allocate DMA resources if DMA HW present*/
197 /* setup interrupt delivery */
199 ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
200 RF_SHAREABLE | RF_ACTIVE);
202 device_printf(dev, "unable to allocate interrupt\n");
205 if ((error = bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS, NULL,
206 ata_interrupt, ch, &ch->ih))) {
207 bus_release_resource(dev, SYS_RES_IRQ, rid, ch->r_irq);
208 device_printf(dev, "unable to setup interrupt\n");
213 /* probe and attach devices on this channel unless we are in early boot */
214 if (!ata_delayed_attach)
218 if (ch->flags & ATA_PERIODIC_POLL)
219 callout_reset(&ch->poll_callout, hz, ata_periodic_poll, ch);
220 mtx_lock(&ch->state_mtx);
221 /* Create the device queue for our SIM. */
222 devq = cam_simq_alloc(1);
224 device_printf(dev, "Unable to allocate simq\n");
228 /* Construct SIM entry */
229 ch->sim = cam_sim_alloc(ataaction, atapoll, "ata", ch,
230 device_get_unit(dev), &ch->state_mtx, 1, 0, devq);
231 if (ch->sim == NULL) {
232 device_printf(dev, "unable to allocate sim\n");
237 if (xpt_bus_register(ch->sim, dev, 0) != CAM_SUCCESS) {
238 device_printf(dev, "unable to register xpt bus\n");
242 if (xpt_create_path(&ch->path, /*periph*/NULL, cam_sim_path(ch->sim),
243 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
244 device_printf(dev, "unable to create path\n");
248 mtx_unlock(&ch->state_mtx);
252 xpt_bus_deregister(cam_sim_path(ch->sim));
254 cam_sim_free(ch->sim, /*free_devq*/TRUE);
257 bus_release_resource(dev, SYS_RES_IRQ, rid, ch->r_irq);
258 mtx_unlock(&ch->state_mtx);
259 if (ch->flags & ATA_PERIODIC_POLL)
260 callout_drain(&ch->poll_callout);
266 ata_detach(device_t dev)
268 struct ata_channel *ch = device_get_softc(dev);
274 /* check that we have a valid channel to detach */
278 /* grap the channel lock so no new requests gets launched */
279 mtx_lock(&ch->state_mtx);
280 ch->state |= ATA_STALL_QUEUE;
281 mtx_unlock(&ch->state_mtx);
283 if (ch->flags & ATA_PERIODIC_POLL)
284 callout_drain(&ch->poll_callout);
288 /* detach & delete all children */
289 if (!device_get_children(dev, &children, &nchildren)) {
290 for (i = 0; i < nchildren; i++)
292 device_delete_child(dev, children[i]);
293 free(children, M_TEMP);
296 taskqueue_drain(taskqueue_thread, &ch->conntask);
299 mtx_lock(&ch->state_mtx);
300 xpt_async(AC_LOST_DEVICE, ch->path, NULL);
301 xpt_free_path(ch->path);
302 xpt_bus_deregister(cam_sim_path(ch->sim));
303 cam_sim_free(ch->sim, /*free_devq*/TRUE);
305 mtx_unlock(&ch->state_mtx);
308 /* release resources */
309 bus_teardown_intr(dev, ch->r_irq, ch->ih);
310 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
313 /* free DMA resources if DMA HW present*/
317 mtx_destroy(&ch->state_mtx);
318 mtx_destroy(&ch->queue_mtx);
323 ata_conn_event(void *context, int dummy)
325 device_t dev = (device_t)context;
327 struct ata_channel *ch = device_get_softc(dev);
330 mtx_lock(&ch->state_mtx);
331 if (ch->sim == NULL) {
332 mtx_unlock(&ch->state_mtx);
336 if ((ccb = xpt_alloc_ccb_nowait()) == NULL)
338 if (xpt_create_path(&ccb->ccb_h.path, NULL,
339 cam_sim_path(ch->sim),
340 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
345 mtx_unlock(&ch->state_mtx);
352 ata_reinit(device_t dev)
354 struct ata_channel *ch = device_get_softc(dev);
355 struct ata_request *request;
360 /* check that we have a valid channel to reinit */
361 if (!ch || !ch->r_irq)
365 device_printf(dev, "reiniting channel ..\n");
367 /* poll for locking the channel */
368 while (ATA_LOCKING(dev, ATA_LF_LOCK) != ch->unit)
371 /* catch eventual request in ch->running */
372 mtx_lock(&ch->state_mtx);
373 if (ch->state & ATA_STALL_QUEUE) {
374 /* Recursive reinits and reinits during detach prohobited. */
375 mtx_unlock(&ch->state_mtx);
378 if ((request = ch->running))
379 callout_stop(&request->callout);
382 /* unconditionally grap the channel lock */
383 ch->state |= ATA_STALL_QUEUE;
384 mtx_unlock(&ch->state_mtx);
386 /* reset the controller HW, the channel and device(s) */
389 /* reinit the children and delete any that fails */
390 if (!device_get_children(dev, &children, &nchildren)) {
391 mtx_lock(&Giant); /* newbus suckage it needs Giant */
392 for (i = 0; i < nchildren; i++) {
393 /* did any children go missing ? */
394 if (children[i] && device_is_attached(children[i]) &&
395 ATA_REINIT(children[i])) {
397 * if we had a running request and its device matches
398 * this child we need to inform the request that the
401 if (request && request->dev == children[i]) {
402 request->result = ENXIO;
403 device_printf(request->dev, "FAILURE - device detached\n");
405 /* if not timeout finish request here */
406 if (!(request->flags & ATA_R_TIMEOUT))
410 device_delete_child(dev, children[i]);
413 free(children, M_TEMP);
414 mtx_unlock(&Giant); /* newbus suckage dealt with, release Giant */
417 /* if we still have a good request put it on the queue again */
418 if (request && !(request->flags & ATA_R_TIMEOUT)) {
419 device_printf(request->dev,
420 "WARNING - %s requeued due to channel reset",
421 ata_cmd2str(request));
422 if (!(request->flags & (ATA_R_ATAPI | ATA_R_CONTROL)))
423 printf(" LBA=%ju", request->u.ata.lba);
425 request->flags |= ATA_R_REQUEUE;
426 ata_queue_request(request);
429 /* we're done release the channel for new work */
430 mtx_lock(&ch->state_mtx);
431 ch->state = ATA_IDLE;
432 mtx_unlock(&ch->state_mtx);
433 ATA_LOCKING(dev, ATA_LF_UNLOCK);
435 /* Add new children. */
436 /* ata_identify(dev); */
439 device_printf(dev, "reinit done ..\n");
441 /* kick off requests on the queue */
444 xpt_freeze_simq(ch->sim, 1);
445 if ((request = ch->running)) {
447 if (ch->state == ATA_ACTIVE)
448 ch->state = ATA_IDLE;
449 callout_stop(&request->callout);
451 ch->dma.unload(request);
452 request->result = ERESTART;
453 ata_cam_end_transaction(dev, request);
455 /* reset the controller HW, the channel and device(s) */
457 /* Tell the XPT about the event */
458 xpt_async(AC_BUS_RESET, ch->path, NULL);
459 xpt_release_simq(ch->sim, TRUE);
465 ata_suspend(device_t dev)
467 struct ata_channel *ch;
469 /* check for valid device */
470 if (!dev || !(ch = device_get_softc(dev)))
474 if (ch->flags & ATA_PERIODIC_POLL)
475 callout_drain(&ch->poll_callout);
476 mtx_lock(&ch->state_mtx);
477 xpt_freeze_simq(ch->sim, 1);
478 while (ch->state != ATA_IDLE)
479 msleep(ch, &ch->state_mtx, PRIBIO, "atasusp", hz/100);
480 mtx_unlock(&ch->state_mtx);
482 /* wait for the channel to be IDLE or detached before suspending */
484 mtx_lock(&ch->state_mtx);
485 if (ch->state == ATA_IDLE) {
486 ch->state = ATA_ACTIVE;
487 mtx_unlock(&ch->state_mtx);
490 mtx_unlock(&ch->state_mtx);
491 tsleep(ch, PRIBIO, "atasusp", hz/10);
493 ATA_LOCKING(dev, ATA_LF_UNLOCK);
499 ata_resume(device_t dev)
501 struct ata_channel *ch;
504 /* check for valid device */
505 if (!dev || !(ch = device_get_softc(dev)))
509 mtx_lock(&ch->state_mtx);
510 error = ata_reinit(dev);
511 xpt_release_simq(ch->sim, TRUE);
512 mtx_unlock(&ch->state_mtx);
513 if (ch->flags & ATA_PERIODIC_POLL)
514 callout_reset(&ch->poll_callout, hz, ata_periodic_poll, ch);
516 /* reinit the devices, we dont know what mode/state they are in */
517 error = ata_reinit(dev);
518 /* kick off requests on the queue */
525 ata_interrupt(void *data)
528 struct ata_channel *ch = (struct ata_channel *)data;
530 mtx_lock(&ch->state_mtx);
532 ata_interrupt_locked(data);
534 mtx_unlock(&ch->state_mtx);
539 ata_interrupt_locked(void *data)
541 struct ata_channel *ch = (struct ata_channel *)data;
542 struct ata_request *request;
545 mtx_lock(&ch->state_mtx);
548 /* ignore interrupt if its not for us */
549 if (ch->hw.status && !ch->hw.status(ch->dev))
552 /* do we have a running request */
553 if (!(request = ch->running))
556 ATA_DEBUG_RQ(request, "interrupt");
558 /* safetycheck for the right state */
559 if (ch->state == ATA_IDLE) {
560 device_printf(request->dev, "interrupt on idle channel ignored\n");
565 * we have the HW locks, so end the transaction for this request
566 * if it finishes immediately otherwise wait for next interrupt
568 if (ch->hw.end_transaction(request) == ATA_OP_FINISHED) {
570 if (ch->state == ATA_ACTIVE)
571 ch->state = ATA_IDLE;
573 ata_cam_end_transaction(ch->dev, request);
575 mtx_unlock(&ch->state_mtx);
576 ATA_LOCKING(ch->dev, ATA_LF_UNLOCK);
583 mtx_unlock(&ch->state_mtx);
589 ata_periodic_poll(void *data)
591 struct ata_channel *ch = (struct ata_channel *)data;
593 callout_reset(&ch->poll_callout, hz, ata_periodic_poll, ch);
599 ata_print_cable(device_t dev, u_int8_t *who)
602 "DMA limited to UDMA33, %s found non-ATA66 cable\n", who);
606 ata_check_80pin(device_t dev, int mode)
608 struct ata_device *atadev = device_get_softc(dev);
610 if (!ata_dma_check_80pin) {
612 device_printf(dev, "Skipping 80pin cable check\n");
616 if (mode > ATA_UDMA2 && !(atadev->param.hwres & ATA_CABLE_ID)) {
617 ata_print_cable(dev, "device");
624 ata_setmode(device_t dev)
626 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
627 struct ata_device *atadev = device_get_softc(dev);
628 int error, mode, pmode;
632 pmode = mode = ata_limit_mode(dev, mode, ATA_DMA_MAX);
633 mode = ATA_SETMODE(device_get_parent(dev), atadev->unit, mode);
634 if ((ch->flags & (ATA_CHECKS_CABLE | ATA_SATA)) == 0)
635 mode = ata_check_80pin(dev, mode);
636 } while (pmode != mode); /* Interate till successfull negotiation. */
637 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
639 device_printf(dev, "%ssetting %s\n",
640 (error) ? "FAILURE " : "", ata_mode2str(mode));
645 * device related interfaces
649 ata_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
650 int32_t flag, struct thread *td)
652 device_t device, *children;
653 struct ata_ioc_devices *devices = (struct ata_ioc_devices *)data;
654 int *value = (int *)data;
655 int i, nchildren, error = ENOTTY;
658 case IOCATAGMAXCHANNEL:
659 /* In case we have channel 0..n this will return n+1. */
660 *value = devclass_get_maxunit(ata_devclass);
665 if (*value >= devclass_get_maxunit(ata_devclass) ||
666 !(device = devclass_get_device(ata_devclass, *value)) ||
667 !device_is_attached(device))
669 error = ata_reinit(device);
673 if (*value >= devclass_get_maxunit(ata_devclass) ||
674 !(device = devclass_get_device(ata_devclass, *value)) ||
675 !device_is_attached(device))
677 error = DEVICE_ATTACH(device);
681 if (*value >= devclass_get_maxunit(ata_devclass) ||
682 !(device = devclass_get_device(ata_devclass, *value)) ||
683 !device_is_attached(device))
685 error = DEVICE_DETACH(device);
689 if (devices->channel >= devclass_get_maxunit(ata_devclass) ||
690 !(device = devclass_get_device(ata_devclass, devices->channel)) ||
691 !device_is_attached(device))
693 bzero(devices->name[0], 32);
694 bzero(&devices->params[0], sizeof(struct ata_params));
695 bzero(devices->name[1], 32);
696 bzero(&devices->params[1], sizeof(struct ata_params));
697 if (!device_get_children(device, &children, &nchildren)) {
698 for (i = 0; i < nchildren; i++) {
699 if (children[i] && device_is_attached(children[i])) {
700 struct ata_device *atadev = device_get_softc(children[i]);
702 if (atadev->unit == ATA_MASTER) { /* XXX SOS PM */
703 strncpy(devices->name[0],
704 device_get_nameunit(children[i]), 32);
705 bcopy(&atadev->param, &devices->params[0],
706 sizeof(struct ata_params));
708 if (atadev->unit == ATA_SLAVE) { /* XXX SOS PM */
709 strncpy(devices->name[1],
710 device_get_nameunit(children[i]), 32);
711 bcopy(&atadev->param, &devices->params[1],
712 sizeof(struct ata_params));
716 free(children, M_TEMP);
724 if (ata_raid_ioctl_func)
725 error = ata_raid_ioctl_func(cmd, data);
732 ata_device_ioctl(device_t dev, u_long cmd, caddr_t data)
734 struct ata_device *atadev = device_get_softc(dev);
735 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
736 struct ata_ioc_request *ioc_request = (struct ata_ioc_request *)data;
737 struct ata_params *params = (struct ata_params *)data;
738 int *mode = (int *)data;
739 struct ata_request *request;
745 if (ioc_request->count >
746 (ch->dma.max_iosize ? ch->dma.max_iosize : DFLTPHYS)) {
749 if (!(buf = malloc(ioc_request->count, M_ATA, M_NOWAIT))) {
752 if (!(request = ata_alloc_request())) {
756 request->dev = atadev->dev;
757 if (ioc_request->flags & ATA_CMD_WRITE) {
758 error = copyin(ioc_request->data, buf, ioc_request->count);
761 ata_free_request(request);
765 if (ioc_request->flags & ATA_CMD_ATAPI) {
766 request->flags = ATA_R_ATAPI;
767 bcopy(ioc_request->u.atapi.ccb, request->u.atapi.ccb, 16);
770 request->u.ata.command = ioc_request->u.ata.command;
771 request->u.ata.feature = ioc_request->u.ata.feature;
772 request->u.ata.lba = ioc_request->u.ata.lba;
773 request->u.ata.count = ioc_request->u.ata.count;
775 request->timeout = ioc_request->timeout;
777 request->bytecount = ioc_request->count;
778 request->transfersize = request->bytecount;
779 if (ioc_request->flags & ATA_CMD_CONTROL)
780 request->flags |= ATA_R_CONTROL;
781 if (ioc_request->flags & ATA_CMD_READ)
782 request->flags |= ATA_R_READ;
783 if (ioc_request->flags & ATA_CMD_WRITE)
784 request->flags |= ATA_R_WRITE;
785 ata_queue_request(request);
786 if (request->flags & ATA_R_ATAPI) {
787 bcopy(&request->u.atapi.sense, &ioc_request->u.atapi.sense,
788 sizeof(struct atapi_sense));
791 ioc_request->u.ata.command = request->u.ata.command;
792 ioc_request->u.ata.feature = request->u.ata.feature;
793 ioc_request->u.ata.lba = request->u.ata.lba;
794 ioc_request->u.ata.count = request->u.ata.count;
796 ioc_request->error = request->result;
797 if (ioc_request->flags & ATA_CMD_READ)
798 error = copyout(buf, ioc_request->data, ioc_request->count);
802 ata_free_request(request);
806 ata_getparam(atadev, 0);
807 bcopy(&atadev->param, params, sizeof(struct ata_params));
811 atadev->mode = *mode;
816 *mode = atadev->mode |
817 (ATA_GETREV(device_get_parent(dev), atadev->unit) << 8);
819 case IOCATASSPINDOWN:
820 atadev->spindown = *mode;
822 case IOCATAGSPINDOWN:
823 *mode = atadev->spindown;
832 ata_boot_attach(void)
834 struct ata_channel *ch;
837 mtx_lock(&Giant); /* newbus suckage it needs Giant */
839 /* kick of probe and attach on all channels */
840 for (ctlr = 0; ctlr < devclass_get_maxunit(ata_devclass); ctlr++) {
841 if ((ch = devclass_get_softc(ata_devclass, ctlr))) {
842 ata_identify(ch->dev);
846 /* release the hook that got us here, we are only needed once during boot */
847 if (ata_delayed_attach) {
848 config_intrhook_disestablish(ata_delayed_attach);
849 free(ata_delayed_attach, M_TEMP);
850 ata_delayed_attach = NULL;
853 mtx_unlock(&Giant); /* newbus suckage dealt with, release Giant */
858 * misc support functions
862 ata_add_child(device_t parent, struct ata_device *atadev, int unit)
866 if ((child = device_add_child(parent, NULL, unit))) {
867 device_set_softc(child, atadev);
870 atadev->max_iosize = DEV_BSIZE;
871 atadev->mode = ATA_PIO_MAX;
878 ata_getparam(struct ata_device *atadev, int init)
880 struct ata_channel *ch = device_get_softc(device_get_parent(atadev->dev));
881 struct ata_request *request;
884 u_int8_t command = 0;
885 int error = ENOMEM, retries = 2, mode = -1;
887 if (ch->devices & (ATA_ATA_MASTER << atadev->unit))
888 command = ATA_ATA_IDENTIFY;
889 if (ch->devices & (ATA_ATAPI_MASTER << atadev->unit))
890 command = ATA_ATAPI_IDENTIFY;
894 while (retries-- > 0 && error) {
895 if (!(request = ata_alloc_request()))
897 request->dev = atadev->dev;
898 request->timeout = 1;
899 request->retries = 0;
900 request->u.ata.command = command;
901 request->flags = (ATA_R_READ|ATA_R_AT_HEAD|ATA_R_DIRECT);
903 request->flags |= ATA_R_QUIET;
904 request->data = (void *)&atadev->param;
905 request->bytecount = sizeof(struct ata_params);
906 request->donecount = 0;
907 request->transfersize = DEV_BSIZE;
908 ata_queue_request(request);
909 error = request->result;
910 ata_free_request(request);
913 if (!error && (isprint(atadev->param.model[0]) ||
914 isprint(atadev->param.model[1]))) {
915 struct ata_params *atacap = &atadev->param;
918 for (ptr = (int16_t *)atacap;
919 ptr < (int16_t *)atacap + sizeof(struct ata_params)/2; ptr++) {
920 *ptr = le16toh(*ptr);
922 if (!(!strncmp(atacap->model, "FX", 2) ||
923 !strncmp(atacap->model, "NEC", 3) ||
924 !strncmp(atacap->model, "Pioneer", 7) ||
925 !strncmp(atacap->model, "SHARP", 5))) {
926 bswap(atacap->model, sizeof(atacap->model));
927 bswap(atacap->revision, sizeof(atacap->revision));
928 bswap(atacap->serial, sizeof(atacap->serial));
930 btrim(atacap->model, sizeof(atacap->model));
931 bpack(atacap->model, atacap->model, sizeof(atacap->model));
932 btrim(atacap->revision, sizeof(atacap->revision));
933 bpack(atacap->revision, atacap->revision, sizeof(atacap->revision));
934 btrim(atacap->serial, sizeof(atacap->serial));
935 bpack(atacap->serial, atacap->serial, sizeof(atacap->serial));
938 printf("ata%d-%s: pio=%s wdma=%s udma=%s cable=%s wire\n",
939 device_get_unit(ch->dev),
940 ata_unit2str(atadev),
941 ata_mode2str(ata_pmode(atacap)),
942 ata_mode2str(ata_wmode(atacap)),
943 ata_mode2str(ata_umode(atacap)),
944 (atacap->hwres & ATA_CABLE_ID) ? "80":"40");
949 sprintf(buffer, "%.40s/%.8s", atacap->model, atacap->revision);
950 device_set_desc_copy(atadev->dev, buffer);
951 if ((atadev->param.config & ATA_PROTO_ATAPI) &&
952 (atadev->param.config != ATA_CFA_MAGIC1) &&
953 (atadev->param.config != ATA_CFA_MAGIC2)) {
955 (atadev->param.config & ATA_DRQ_MASK) != ATA_DRQ_INTR &&
956 ata_umode(&atadev->param) >= ATA_UDMA2)
957 atadev->mode = ATA_DMA_MAX;
961 (ata_umode(&atadev->param) > 0 ||
962 ata_wmode(&atadev->param) > 0))
963 atadev->mode = ATA_DMA_MAX;
965 snprintf(buf, sizeof(buf), "dev%d.mode", atadev->unit);
966 if (resource_string_value(device_get_name(ch->dev),
967 device_get_unit(ch->dev), buf, &res) == 0)
968 mode = ata_str2mode(res);
969 else if (resource_string_value(device_get_name(ch->dev),
970 device_get_unit(ch->dev), "mode", &res) == 0)
971 mode = ata_str2mode(res);
985 ata_identify(device_t dev)
987 struct ata_channel *ch = device_get_softc(dev);
988 struct ata_device *atadev;
990 device_t child, master = NULL;
991 int nchildren, i, n = ch->devices;
994 device_printf(dev, "Identifying devices: %08x\n", ch->devices);
997 /* Skip existing devices. */
998 if (!device_get_children(dev, &children, &nchildren)) {
999 for (i = 0; i < nchildren; i++) {
1000 if (children[i] && (atadev = device_get_softc(children[i])))
1001 n &= ~((ATA_ATA_MASTER | ATA_ATAPI_MASTER) << atadev->unit);
1003 free(children, M_TEMP);
1005 /* Create new devices. */
1007 device_printf(dev, "New devices: %08x\n", n);
1012 for (i = 0; i < ATA_PM; ++i) {
1013 if (n & (((ATA_ATA_MASTER | ATA_ATAPI_MASTER) << i))) {
1016 if (!(atadev = malloc(sizeof(struct ata_device),
1017 M_ATA, M_NOWAIT | M_ZERO))) {
1018 device_printf(dev, "out of memory\n");
1022 #ifdef ATA_STATIC_ID
1023 if (n & (ATA_ATA_MASTER << i))
1024 unit = (device_get_unit(dev) << 1) + i;
1026 if ((child = ata_add_child(dev, atadev, unit))) {
1028 * PATA slave should be identified first, to allow
1029 * device cable detection on master to work properly.
1031 if (i == 0 && (n & ATA_PORTMULTIPLIER) == 0 &&
1032 (n & ((ATA_ATA_MASTER | ATA_ATAPI_MASTER) << 1)) != 0) {
1036 if (ata_getparam(atadev, 1)) {
1037 device_delete_child(dev, child);
1038 free(atadev, M_ATA);
1042 free(atadev, M_ATA);
1046 atadev = device_get_softc(master);
1047 if (ata_getparam(atadev, 1)) {
1048 device_delete_child(dev, master);
1049 free(atadev, M_ATA);
1052 bus_generic_probe(dev);
1053 bus_generic_attach(dev);
1060 ata_default_registers(device_t dev)
1062 struct ata_channel *ch = device_get_softc(dev);
1064 /* fill in the defaults from whats setup already */
1065 ch->r_io[ATA_ERROR].res = ch->r_io[ATA_FEATURE].res;
1066 ch->r_io[ATA_ERROR].offset = ch->r_io[ATA_FEATURE].offset;
1067 ch->r_io[ATA_IREASON].res = ch->r_io[ATA_COUNT].res;
1068 ch->r_io[ATA_IREASON].offset = ch->r_io[ATA_COUNT].offset;
1069 ch->r_io[ATA_STATUS].res = ch->r_io[ATA_COMMAND].res;
1070 ch->r_io[ATA_STATUS].offset = ch->r_io[ATA_COMMAND].offset;
1071 ch->r_io[ATA_ALTSTAT].res = ch->r_io[ATA_CONTROL].res;
1072 ch->r_io[ATA_ALTSTAT].offset = ch->r_io[ATA_CONTROL].offset;
1076 ata_modify_if_48bit(struct ata_request *request)
1078 struct ata_channel *ch = device_get_softc(request->parent);
1079 struct ata_device *atadev = device_get_softc(request->dev);
1081 request->flags &= ~ATA_R_48BIT;
1083 if (((request->u.ata.lba + request->u.ata.count) >= ATA_MAX_28BIT_LBA ||
1084 request->u.ata.count > 256) &&
1085 atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) {
1087 /* translate command into 48bit version */
1088 switch (request->u.ata.command) {
1090 request->u.ata.command = ATA_READ48;
1093 request->u.ata.command = ATA_READ_MUL48;
1096 if (ch->flags & ATA_NO_48BIT_DMA) {
1097 if (request->transfersize > DEV_BSIZE)
1098 request->u.ata.command = ATA_READ_MUL48;
1100 request->u.ata.command = ATA_READ48;
1101 request->flags &= ~ATA_R_DMA;
1104 request->u.ata.command = ATA_READ_DMA48;
1106 case ATA_READ_DMA_QUEUED:
1107 if (ch->flags & ATA_NO_48BIT_DMA) {
1108 if (request->transfersize > DEV_BSIZE)
1109 request->u.ata.command = ATA_READ_MUL48;
1111 request->u.ata.command = ATA_READ48;
1112 request->flags &= ~ATA_R_DMA;
1115 request->u.ata.command = ATA_READ_DMA_QUEUED48;
1118 request->u.ata.command = ATA_WRITE48;
1121 request->u.ata.command = ATA_WRITE_MUL48;
1124 if (ch->flags & ATA_NO_48BIT_DMA) {
1125 if (request->transfersize > DEV_BSIZE)
1126 request->u.ata.command = ATA_WRITE_MUL48;
1128 request->u.ata.command = ATA_WRITE48;
1129 request->flags &= ~ATA_R_DMA;
1132 request->u.ata.command = ATA_WRITE_DMA48;
1134 case ATA_WRITE_DMA_QUEUED:
1135 if (ch->flags & ATA_NO_48BIT_DMA) {
1136 if (request->transfersize > DEV_BSIZE)
1137 request->u.ata.command = ATA_WRITE_MUL48;
1139 request->u.ata.command = ATA_WRITE48;
1140 request->u.ata.command = ATA_WRITE48;
1141 request->flags &= ~ATA_R_DMA;
1144 request->u.ata.command = ATA_WRITE_DMA_QUEUED48;
1146 case ATA_FLUSHCACHE:
1147 request->u.ata.command = ATA_FLUSHCACHE48;
1149 case ATA_SET_MAX_ADDRESS:
1150 request->u.ata.command = ATA_SET_MAX_ADDRESS48;
1155 request->flags |= ATA_R_48BIT;
1157 else if (atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) {
1159 /* translate command into 48bit version */
1160 switch (request->u.ata.command) {
1161 case ATA_FLUSHCACHE:
1162 request->u.ata.command = ATA_FLUSHCACHE48;
1164 case ATA_READ_NATIVE_MAX_ADDRESS:
1165 request->u.ata.command = ATA_READ_NATIVE_MAX_ADDRESS48;
1167 case ATA_SET_MAX_ADDRESS:
1168 request->u.ata.command = ATA_SET_MAX_ADDRESS48;
1173 request->flags |= ATA_R_48BIT;
1178 ata_udelay(int interval)
1180 /* for now just use DELAY, the timer/sleep subsytems are not there yet */
1181 if (1 || interval < (1000000/hz) || ata_delayed_attach)
1184 pause("ataslp", interval/(1000000/hz));
1188 ata_unit2str(struct ata_device *atadev)
1190 struct ata_channel *ch = device_get_softc(device_get_parent(atadev->dev));
1193 if (ch->devices & ATA_PORTMULTIPLIER)
1194 sprintf(str, "port%d", atadev->unit);
1196 sprintf(str, "%s", atadev->unit == ATA_MASTER ? "master" : "slave");
1201 ata_mode2str(int mode)
1204 case -1: return "UNSUPPORTED";
1205 case ATA_PIO0: return "PIO0";
1206 case ATA_PIO1: return "PIO1";
1207 case ATA_PIO2: return "PIO2";
1208 case ATA_PIO3: return "PIO3";
1209 case ATA_PIO4: return "PIO4";
1210 case ATA_WDMA0: return "WDMA0";
1211 case ATA_WDMA1: return "WDMA1";
1212 case ATA_WDMA2: return "WDMA2";
1213 case ATA_UDMA0: return "UDMA16";
1214 case ATA_UDMA1: return "UDMA25";
1215 case ATA_UDMA2: return "UDMA33";
1216 case ATA_UDMA3: return "UDMA40";
1217 case ATA_UDMA4: return "UDMA66";
1218 case ATA_UDMA5: return "UDMA100";
1219 case ATA_UDMA6: return "UDMA133";
1220 case ATA_SA150: return "SATA150";
1221 case ATA_SA300: return "SATA300";
1223 if (mode & ATA_DMA_MASK)
1231 ata_str2mode(const char *str)
1234 if (!strcasecmp(str, "PIO0")) return (ATA_PIO0);
1235 if (!strcasecmp(str, "PIO1")) return (ATA_PIO1);
1236 if (!strcasecmp(str, "PIO2")) return (ATA_PIO2);
1237 if (!strcasecmp(str, "PIO3")) return (ATA_PIO3);
1238 if (!strcasecmp(str, "PIO4")) return (ATA_PIO4);
1239 if (!strcasecmp(str, "WDMA0")) return (ATA_WDMA0);
1240 if (!strcasecmp(str, "WDMA1")) return (ATA_WDMA1);
1241 if (!strcasecmp(str, "WDMA2")) return (ATA_WDMA2);
1242 if (!strcasecmp(str, "UDMA0")) return (ATA_UDMA0);
1243 if (!strcasecmp(str, "UDMA16")) return (ATA_UDMA0);
1244 if (!strcasecmp(str, "UDMA1")) return (ATA_UDMA1);
1245 if (!strcasecmp(str, "UDMA25")) return (ATA_UDMA1);
1246 if (!strcasecmp(str, "UDMA2")) return (ATA_UDMA2);
1247 if (!strcasecmp(str, "UDMA33")) return (ATA_UDMA2);
1248 if (!strcasecmp(str, "UDMA3")) return (ATA_UDMA3);
1249 if (!strcasecmp(str, "UDMA44")) return (ATA_UDMA3);
1250 if (!strcasecmp(str, "UDMA4")) return (ATA_UDMA4);
1251 if (!strcasecmp(str, "UDMA66")) return (ATA_UDMA4);
1252 if (!strcasecmp(str, "UDMA5")) return (ATA_UDMA5);
1253 if (!strcasecmp(str, "UDMA100")) return (ATA_UDMA5);
1254 if (!strcasecmp(str, "UDMA6")) return (ATA_UDMA6);
1255 if (!strcasecmp(str, "UDMA133")) return (ATA_UDMA6);
1260 ata_satarev2str(int rev)
1264 case 1: return "SATA 1.5Gb/s";
1265 case 2: return "SATA 3Gb/s";
1266 case 3: return "SATA 6Gb/s";
1267 case 0xff: return "SATA";
1268 default: return "???";
1273 ata_atapi(device_t dev, int target)
1275 struct ata_channel *ch = device_get_softc(dev);
1277 return (ch->devices & (ATA_ATAPI_MASTER << target));
1281 ata_pmode(struct ata_params *ap)
1283 if (ap->atavalid & ATA_FLAG_64_70) {
1284 if (ap->apiomodes & 0x02)
1286 if (ap->apiomodes & 0x01)
1289 if (ap->mwdmamodes & 0x04)
1291 if (ap->mwdmamodes & 0x02)
1293 if (ap->mwdmamodes & 0x01)
1295 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200)
1297 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100)
1299 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000)
1305 ata_wmode(struct ata_params *ap)
1307 if (ap->mwdmamodes & 0x04)
1309 if (ap->mwdmamodes & 0x02)
1311 if (ap->mwdmamodes & 0x01)
1317 ata_umode(struct ata_params *ap)
1319 if (ap->atavalid & ATA_FLAG_88) {
1320 if (ap->udmamodes & 0x40)
1322 if (ap->udmamodes & 0x20)
1324 if (ap->udmamodes & 0x10)
1326 if (ap->udmamodes & 0x08)
1328 if (ap->udmamodes & 0x04)
1330 if (ap->udmamodes & 0x02)
1332 if (ap->udmamodes & 0x01)
1339 ata_limit_mode(device_t dev, int mode, int maxmode)
1341 struct ata_device *atadev = device_get_softc(dev);
1343 if (maxmode && mode > maxmode)
1346 if (mode >= ATA_UDMA0 && ata_umode(&atadev->param) > 0)
1347 return min(mode, ata_umode(&atadev->param));
1349 if (mode >= ATA_WDMA0 && ata_wmode(&atadev->param) > 0)
1350 return min(mode, ata_wmode(&atadev->param));
1352 if (mode > ata_pmode(&atadev->param))
1353 return min(mode, ata_pmode(&atadev->param));
1359 bswap(int8_t *buf, int len)
1361 u_int16_t *ptr = (u_int16_t*)(buf + len);
1363 while (--ptr >= (u_int16_t*)buf)
1368 btrim(int8_t *buf, int len)
1372 for (ptr = buf; ptr < buf+len; ++ptr)
1373 if (!*ptr || *ptr == '_')
1375 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
1380 bpack(int8_t *src, int8_t *dst, int len)
1384 for (i = j = blank = 0 ; i < len; i++) {
1385 if (blank && src[i] == ' ') continue;
1386 if (blank && src[i] != ' ') {
1391 if (src[i] == ' ') {
1404 ata_cam_begin_transaction(device_t dev, union ccb *ccb)
1406 struct ata_channel *ch = device_get_softc(dev);
1407 struct ata_request *request;
1409 if (!(request = ata_alloc_request())) {
1410 device_printf(dev, "FAILURE - out of memory in start\n");
1411 ccb->ccb_h.status = CAM_REQ_INVALID;
1415 bzero(request, sizeof(*request));
1418 request->dev = NULL;
1419 request->parent = dev;
1420 request->unit = ccb->ccb_h.target_id;
1421 if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1422 request->data = ccb->ataio.data_ptr;
1423 request->bytecount = ccb->ataio.dxfer_len;
1424 request->u.ata.command = ccb->ataio.cmd.command;
1425 request->u.ata.feature = ((uint16_t)ccb->ataio.cmd.features_exp << 8) |
1426 (uint16_t)ccb->ataio.cmd.features;
1427 request->u.ata.count = ((uint16_t)ccb->ataio.cmd.sector_count_exp << 8) |
1428 (uint16_t)ccb->ataio.cmd.sector_count;
1429 if (ccb->ataio.cmd.flags & CAM_ATAIO_48BIT) {
1430 request->flags |= ATA_R_48BIT;
1431 request->u.ata.lba =
1432 ((uint64_t)ccb->ataio.cmd.lba_high_exp << 40) |
1433 ((uint64_t)ccb->ataio.cmd.lba_mid_exp << 32) |
1434 ((uint64_t)ccb->ataio.cmd.lba_low_exp << 24);
1436 request->u.ata.lba =
1437 ((uint64_t)(ccb->ataio.cmd.device & 0x0f) << 24);
1439 request->u.ata.lba |= ((uint64_t)ccb->ataio.cmd.lba_high << 16) |
1440 ((uint64_t)ccb->ataio.cmd.lba_mid << 8) |
1441 (uint64_t)ccb->ataio.cmd.lba_low;
1442 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE &&
1443 ccb->ataio.cmd.flags & CAM_ATAIO_DMA)
1444 request->flags |= ATA_R_DMA;
1445 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1446 request->flags |= ATA_R_READ;
1447 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
1448 request->flags |= ATA_R_WRITE;
1450 request->data = ccb->csio.data_ptr;
1451 request->bytecount = ccb->csio.dxfer_len;
1452 bcopy((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
1453 ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes,
1454 request->u.atapi.ccb, ccb->csio.cdb_len);
1455 request->flags |= ATA_R_ATAPI;
1456 if (ch->curr[ccb->ccb_h.target_id].atapi == 16)
1457 request->flags |= ATA_R_ATAPI16;
1458 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE &&
1459 ch->curr[ccb->ccb_h.target_id].mode >= ATA_DMA)
1460 request->flags |= ATA_R_DMA;
1461 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1462 request->flags |= ATA_R_READ;
1463 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
1464 request->flags |= ATA_R_WRITE;
1466 request->transfersize = min(request->bytecount,
1467 ch->curr[ccb->ccb_h.target_id].bytecount);
1468 request->retries = 0;
1469 request->timeout = (ccb->ccb_h.timeout + 999) / 1000;
1470 callout_init_mtx(&request->callout, &ch->state_mtx, CALLOUT_RETURNUNLOCKED);
1473 ch->running = request;
1474 ch->state = ATA_ACTIVE;
1475 if (ch->hw.begin_transaction(request) == ATA_OP_FINISHED) {
1477 ch->state = ATA_IDLE;
1478 ata_cam_end_transaction(dev, request);
1484 ata_cam_request_sense(device_t dev, struct ata_request *request)
1486 struct ata_channel *ch = device_get_softc(dev);
1487 union ccb *ccb = request->ccb;
1489 ch->requestsense = 1;
1491 bzero(request, sizeof(&request));
1492 request->dev = NULL;
1493 request->parent = dev;
1494 request->unit = ccb->ccb_h.target_id;
1495 request->data = (void *)&ccb->csio.sense_data;
1496 request->bytecount = ccb->csio.sense_len;
1497 request->u.atapi.ccb[0] = ATAPI_REQUEST_SENSE;
1498 request->u.atapi.ccb[4] = ccb->csio.sense_len;
1499 request->flags |= ATA_R_ATAPI;
1500 if (ch->curr[ccb->ccb_h.target_id].atapi == 16)
1501 request->flags |= ATA_R_ATAPI16;
1502 if (ch->curr[ccb->ccb_h.target_id].mode >= ATA_DMA)
1503 request->flags |= ATA_R_DMA;
1504 request->flags |= ATA_R_READ;
1505 request->transfersize = min(request->bytecount,
1506 ch->curr[ccb->ccb_h.target_id].bytecount);
1507 request->retries = 0;
1508 request->timeout = (ccb->ccb_h.timeout + 999) / 1000;
1509 callout_init_mtx(&request->callout, &ch->state_mtx, CALLOUT_RETURNUNLOCKED);
1512 ch->running = request;
1513 ch->state = ATA_ACTIVE;
1514 if (ch->hw.begin_transaction(request) == ATA_OP_FINISHED) {
1516 ch->state = ATA_IDLE;
1517 ata_cam_end_transaction(dev, request);
1523 ata_cam_process_sense(device_t dev, struct ata_request *request)
1525 struct ata_channel *ch = device_get_softc(dev);
1526 union ccb *ccb = request->ccb;
1529 ch->requestsense = 0;
1531 if (request->flags & ATA_R_TIMEOUT)
1533 if ((request->flags & ATA_R_TIMEOUT) == 0 &&
1534 (request->status & ATA_S_ERROR) == 0 &&
1535 request->result == 0) {
1536 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
1538 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1539 ccb->ccb_h.status |= CAM_AUTOSENSE_FAIL;
1542 ata_free_request(request);
1544 /* Do error recovery if needed. */
1550 ata_cam_end_transaction(device_t dev, struct ata_request *request)
1552 struct ata_channel *ch = device_get_softc(dev);
1553 union ccb *ccb = request->ccb;
1556 if (ch->requestsense) {
1557 ata_cam_process_sense(dev, request);
1561 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1562 if (request->flags & ATA_R_TIMEOUT) {
1563 xpt_freeze_simq(ch->sim, 1);
1564 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1565 ccb->ccb_h.status |= CAM_CMD_TIMEOUT | CAM_RELEASE_SIMQ;
1567 } else if (request->status & ATA_S_ERROR) {
1568 if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1569 ccb->ccb_h.status |= CAM_ATA_STATUS_ERROR;
1571 ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1572 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1574 } else if (request->result == ERESTART)
1575 ccb->ccb_h.status |= CAM_REQUEUE_REQ;
1576 else if (request->result != 0)
1577 ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
1579 ccb->ccb_h.status |= CAM_REQ_CMP;
1580 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP &&
1581 !(ccb->ccb_h.status & CAM_DEV_QFRZN)) {
1582 xpt_freeze_devq(ccb->ccb_h.path, 1);
1583 ccb->ccb_h.status |= CAM_DEV_QFRZN;
1585 if (ccb->ccb_h.func_code == XPT_ATA_IO &&
1586 ((request->status & ATA_S_ERROR) ||
1587 (ccb->ataio.cmd.flags & CAM_ATAIO_NEEDRESULT))) {
1588 struct ata_res *res = &ccb->ataio.res;
1589 res->status = request->status;
1590 res->error = request->error;
1591 res->lba_low = request->u.ata.lba;
1592 res->lba_mid = request->u.ata.lba >> 8;
1593 res->lba_high = request->u.ata.lba >> 16;
1594 res->device = request->u.ata.lba >> 24;
1595 res->lba_low_exp = request->u.ata.lba >> 24;
1596 res->lba_mid_exp = request->u.ata.lba >> 32;
1597 res->lba_high_exp = request->u.ata.lba >> 40;
1598 res->sector_count = request->u.ata.count;
1599 res->sector_count_exp = request->u.ata.count >> 8;
1601 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1602 if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1604 ccb->ataio.dxfer_len - request->donecount;
1607 ccb->csio.dxfer_len - request->donecount;
1610 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_SCSI_STATUS_ERROR &&
1611 (ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)
1612 ata_cam_request_sense(dev, request);
1614 ata_free_request(request);
1617 /* Do error recovery if needed. */
1623 ata_check_ids(device_t dev, union ccb *ccb)
1625 struct ata_channel *ch = device_get_softc(dev);
1627 if (ccb->ccb_h.target_id > ((ch->flags & ATA_NO_SLAVE) ? 0 : 1)) {
1628 ccb->ccb_h.status = CAM_TID_INVALID;
1632 if (ccb->ccb_h.target_lun != 0) {
1633 ccb->ccb_h.status = CAM_LUN_INVALID;
1641 ataaction(struct cam_sim *sim, union ccb *ccb)
1644 struct ata_channel *ch;
1646 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ataaction func_code=%x\n",
1647 ccb->ccb_h.func_code));
1649 ch = (struct ata_channel *)cam_sim_softc(sim);
1651 switch (ccb->ccb_h.func_code) {
1652 /* Common cases first */
1653 case XPT_ATA_IO: /* Execute the requested I/O operation */
1655 if (ata_check_ids(dev, ccb))
1657 if ((ch->devices & ((ATA_ATA_MASTER | ATA_ATAPI_MASTER)
1658 << ccb->ccb_h.target_id)) == 0) {
1659 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
1663 device_printf(dev, "already running!\n");
1664 if (ccb->ccb_h.func_code == XPT_ATA_IO &&
1665 (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) &&
1666 (ccb->ataio.cmd.control & ATA_A_RESET)) {
1667 struct ata_res *res = &ccb->ataio.res;
1669 bzero(res, sizeof(*res));
1670 if (ch->devices & (ATA_ATA_MASTER << ccb->ccb_h.target_id)) {
1674 res->lba_high = 0xeb;
1675 res->lba_mid = 0x14;
1677 ccb->ccb_h.status = CAM_REQ_CMP;
1680 ata_cam_begin_transaction(dev, ccb);
1682 case XPT_EN_LUN: /* Enable LUN as a target */
1683 case XPT_TARGET_IO: /* Execute target I/O request */
1684 case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */
1685 case XPT_CONT_TARGET_IO: /* Continue Host Target I/O Connection*/
1686 case XPT_ABORT: /* Abort the specified CCB */
1688 ccb->ccb_h.status = CAM_REQ_INVALID;
1690 case XPT_SET_TRAN_SETTINGS:
1692 struct ccb_trans_settings *cts = &ccb->cts;
1693 struct ata_cam_device *d;
1695 if (ata_check_ids(dev, ccb))
1697 if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
1698 d = &ch->curr[ccb->ccb_h.target_id];
1700 d = &ch->user[ccb->ccb_h.target_id];
1701 if (ch->flags & ATA_SATA) {
1702 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_REVISION)
1703 d->revision = cts->xport_specific.sata.revision;
1704 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_MODE) {
1705 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
1706 d->mode = ATA_SETMODE(ch->dev,
1707 ccb->ccb_h.target_id,
1708 cts->xport_specific.sata.mode);
1710 d->mode = cts->xport_specific.sata.mode;
1712 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
1713 d->bytecount = min(8192, cts->xport_specific.sata.bytecount);
1714 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_ATAPI)
1715 d->atapi = cts->xport_specific.sata.atapi;
1716 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1717 d->caps = cts->xport_specific.sata.caps;
1719 if (cts->xport_specific.ata.valid & CTS_ATA_VALID_MODE) {
1720 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
1721 d->mode = ATA_SETMODE(ch->dev,
1722 ccb->ccb_h.target_id,
1723 cts->xport_specific.ata.mode);
1725 d->mode = cts->xport_specific.ata.mode;
1727 if (cts->xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
1728 d->bytecount = cts->xport_specific.ata.bytecount;
1729 if (cts->xport_specific.ata.valid & CTS_ATA_VALID_ATAPI)
1730 d->atapi = cts->xport_specific.ata.atapi;
1732 ccb->ccb_h.status = CAM_REQ_CMP;
1735 case XPT_GET_TRAN_SETTINGS:
1737 struct ccb_trans_settings *cts = &ccb->cts;
1738 struct ata_cam_device *d;
1740 if (ata_check_ids(dev, ccb))
1742 if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
1743 d = &ch->curr[ccb->ccb_h.target_id];
1745 d = &ch->user[ccb->ccb_h.target_id];
1746 cts->protocol = PROTO_ATA;
1747 cts->protocol_version = PROTO_VERSION_UNSPECIFIED;
1748 if (ch->flags & ATA_SATA) {
1749 cts->transport = XPORT_SATA;
1750 cts->transport_version = XPORT_VERSION_UNSPECIFIED;
1751 cts->xport_specific.sata.valid = 0;
1752 cts->xport_specific.sata.mode = d->mode;
1753 cts->xport_specific.sata.valid |= CTS_SATA_VALID_MODE;
1754 cts->xport_specific.sata.bytecount = d->bytecount;
1755 cts->xport_specific.sata.valid |= CTS_SATA_VALID_BYTECOUNT;
1756 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
1757 cts->xport_specific.sata.revision =
1758 ATA_GETREV(dev, ccb->ccb_h.target_id);
1759 if (cts->xport_specific.sata.revision != 0xff) {
1760 cts->xport_specific.sata.valid |=
1761 CTS_SATA_VALID_REVISION;
1763 cts->xport_specific.sata.caps =
1764 d->caps & CTS_SATA_CAPS_D;
1766 cts->xport_specific.sata.caps |=
1767 CTS_SATA_CAPS_H_PMREQ;
1769 cts->xport_specific.sata.caps &=
1770 ch->user[ccb->ccb_h.target_id].caps;
1771 cts->xport_specific.sata.valid |=
1772 CTS_SATA_VALID_CAPS;
1774 cts->xport_specific.sata.revision = d->revision;
1775 cts->xport_specific.sata.valid |= CTS_SATA_VALID_REVISION;
1776 cts->xport_specific.sata.caps = d->caps;
1777 cts->xport_specific.sata.valid |= CTS_SATA_VALID_CAPS;
1779 cts->xport_specific.sata.atapi = d->atapi;
1780 cts->xport_specific.sata.valid |= CTS_SATA_VALID_ATAPI;
1782 cts->transport = XPORT_ATA;
1783 cts->transport_version = XPORT_VERSION_UNSPECIFIED;
1784 cts->xport_specific.ata.valid = 0;
1785 cts->xport_specific.ata.mode = d->mode;
1786 cts->xport_specific.ata.valid |= CTS_ATA_VALID_MODE;
1787 cts->xport_specific.ata.bytecount = d->bytecount;
1788 cts->xport_specific.ata.valid |= CTS_ATA_VALID_BYTECOUNT;
1789 cts->xport_specific.ata.atapi = d->atapi;
1790 cts->xport_specific.ata.valid |= CTS_ATA_VALID_ATAPI;
1792 ccb->ccb_h.status = CAM_REQ_CMP;
1795 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
1796 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
1798 ccb->ccb_h.status = CAM_REQ_CMP;
1800 case XPT_TERM_IO: /* Terminate the I/O process */
1802 ccb->ccb_h.status = CAM_REQ_INVALID;
1804 case XPT_PATH_INQ: /* Path routing inquiry */
1806 struct ccb_pathinq *cpi = &ccb->cpi;
1808 cpi->version_num = 1; /* XXX??? */
1809 cpi->hba_inquiry = PI_SDTR_ABLE;
1810 cpi->target_sprt = 0;
1811 cpi->hba_misc = PIM_SEQSCAN;
1812 cpi->hba_eng_cnt = 0;
1813 if (ch->flags & ATA_NO_SLAVE)
1814 cpi->max_target = 0;
1816 cpi->max_target = 1;
1818 cpi->initiator_id = 0;
1819 cpi->bus_id = cam_sim_bus(sim);
1820 if (ch->flags & ATA_SATA)
1821 cpi->base_transfer_speed = 150000;
1823 cpi->base_transfer_speed = 3300;
1824 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1825 strncpy(cpi->hba_vid, "ATA", HBA_IDLEN);
1826 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1827 cpi->unit_number = cam_sim_unit(sim);
1828 if (ch->flags & ATA_SATA)
1829 cpi->transport = XPORT_SATA;
1831 cpi->transport = XPORT_ATA;
1832 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
1833 cpi->protocol = PROTO_ATA;
1834 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
1835 cpi->maxio = ch->dma.max_iosize ? ch->dma.max_iosize : DFLTPHYS;
1836 cpi->ccb_h.status = CAM_REQ_CMP;
1840 ccb->ccb_h.status = CAM_REQ_INVALID;
1847 atapoll(struct cam_sim *sim)
1849 struct ata_channel *ch = (struct ata_channel *)cam_sim_softc(sim);
1851 ata_interrupt_locked(ch);
1859 ata_module_event_handler(module_t mod, int what, void *arg)
1862 static struct cdev *atacdev;
1868 /* register controlling device */
1869 atacdev = make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "ata");
1872 /* register boot attach to be run when interrupts are enabled */
1873 if (!(ata_delayed_attach = (struct intr_config_hook *)
1874 malloc(sizeof(struct intr_config_hook),
1875 M_TEMP, M_NOWAIT | M_ZERO))) {
1876 printf("ata: malloc of delayed attach hook failed\n");
1879 ata_delayed_attach->ich_func = (void*)ata_boot_attach;
1880 if (config_intrhook_establish(ata_delayed_attach) != 0) {
1881 printf("ata: config_intrhook_establish failed\n");
1882 free(ata_delayed_attach, M_TEMP);
1890 /* deregister controlling device */
1891 destroy_dev(atacdev);
1900 static moduledata_t ata_moduledata = { "ata", ata_module_event_handler, NULL };
1901 DECLARE_MODULE(ata, ata_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
1902 MODULE_VERSION(ata, 1);
1904 MODULE_DEPEND(ata, cam, 1, 1, 1);
1910 ata_request_zone = uma_zcreate("ata_request", sizeof(struct ata_request),
1911 NULL, NULL, NULL, NULL, 0, 0);
1912 ata_composite_zone = uma_zcreate("ata_composite",
1913 sizeof(struct ata_composite),
1914 NULL, NULL, NULL, NULL, 0, 0);
1916 SYSINIT(ata_register, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL);
1921 uma_zdestroy(ata_composite_zone);
1922 uma_zdestroy(ata_request_zone);
1924 SYSUNINIT(ata_unregister, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_uninit, NULL);