2 * Copyright (c) 1998 - 2008 Søren Schmidt <sos@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/endian.h>
37 #include <sys/ctype.h>
41 #include <sys/malloc.h>
42 #include <sys/sysctl.h>
44 #include <sys/taskqueue.h>
46 #include <machine/stdarg.h>
47 #include <machine/resource.h>
48 #include <machine/bus.h>
50 #include <dev/ata/ata-all.h>
51 #include <dev/pci/pcivar.h>
56 #include <cam/cam_ccb.h>
57 #include <cam/cam_sim.h>
58 #include <cam/cam_xpt_sim.h>
59 #include <cam/cam_debug.h>
63 /* device structure */
64 static d_ioctl_t ata_ioctl;
65 static struct cdevsw ata_cdevsw = {
66 .d_version = D_VERSION,
67 .d_flags = D_NEEDGIANT, /* we need this as newbus isn't mpsafe */
75 static void ata_boot_attach(void);
76 static device_t ata_add_child(device_t, struct ata_device *, int);
78 static void ataaction(struct cam_sim *sim, union ccb *ccb);
79 static void atapoll(struct cam_sim *sim);
81 static void ata_conn_event(void *, int);
83 static void bswap(int8_t *, int);
84 static void btrim(int8_t *, int);
85 static void bpack(int8_t *, int8_t *, int);
87 static void ata_interrupt_locked(void *data);
89 static void ata_periodic_poll(void *data);
93 MALLOC_DEFINE(M_ATA, "ata_generic", "ATA driver generic layer");
94 int (*ata_raid_ioctl_func)(u_long cmd, caddr_t data) = NULL;
96 struct intr_config_hook *ata_delayed_attach = NULL;
98 devclass_t ata_devclass;
99 uma_zone_t ata_request_zone;
100 uma_zone_t ata_composite_zone;
105 int ata_dma_check_80pin = 1;
109 static int ata_dma = 1;
110 static int atapi_dma = 1;
114 SYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters");
116 TUNABLE_INT("hw.ata.ata_dma", &ata_dma);
117 SYSCTL_INT(_hw_ata, OID_AUTO, ata_dma, CTLFLAG_RDTUN, &ata_dma, 0,
118 "ATA disk DMA mode control");
120 TUNABLE_INT("hw.ata.ata_dma_check_80pin", &ata_dma_check_80pin);
121 SYSCTL_INT(_hw_ata, OID_AUTO, ata_dma_check_80pin,
122 CTLFLAG_RW, &ata_dma_check_80pin, 1,
123 "Check for 80pin cable before setting ATA DMA mode");
125 TUNABLE_INT("hw.ata.atapi_dma", &atapi_dma);
126 SYSCTL_INT(_hw_ata, OID_AUTO, atapi_dma, CTLFLAG_RDTUN, &atapi_dma, 0,
127 "ATAPI device DMA mode control");
128 TUNABLE_INT("hw.ata.wc", &ata_wc);
129 SYSCTL_INT(_hw_ata, OID_AUTO, wc, CTLFLAG_RDTUN, &ata_wc, 0,
130 "ATA disk write caching");
131 TUNABLE_INT("hw.ata.setmax", &ata_setmax);
132 SYSCTL_INT(_hw_ata, OID_AUTO, setmax, CTLFLAG_RDTUN, &ata_setmax, 0,
133 "ATA disk set max native address");
136 FEATURE(ata_cam, "ATA devices are accessed through the cam(4) driver");
140 * newbus device interface related functions
143 ata_probe(device_t dev)
149 ata_attach(device_t dev)
151 struct ata_channel *ch = device_get_softc(dev);
154 struct cam_devq *devq;
160 /* check that we have a virgin channel to attach */
164 /* initialize the softc basics */
166 ch->state = ATA_IDLE;
167 bzero(&ch->state_mtx, sizeof(struct mtx));
168 mtx_init(&ch->state_mtx, "ATA state lock", NULL, MTX_DEF);
169 bzero(&ch->queue_mtx, sizeof(struct mtx));
170 mtx_init(&ch->queue_mtx, "ATA queue lock", NULL, MTX_DEF);
171 TAILQ_INIT(&ch->ata_queue);
172 TASK_INIT(&ch->conntask, 0, ata_conn_event, dev);
174 for (i = 0; i < 16; i++) {
175 ch->user[i].mode = 0;
176 snprintf(buf, sizeof(buf), "dev%d.mode", i);
177 if (resource_string_value(device_get_name(dev),
178 device_get_unit(dev), buf, &res) == 0)
179 mode = ata_str2mode(res);
180 else if (resource_string_value(device_get_name(dev),
181 device_get_unit(dev), "mode", &res) == 0)
182 mode = ata_str2mode(res);
186 ch->user[i].mode = mode;
187 if (ch->flags & ATA_SATA)
188 ch->user[i].bytecount = 8192;
190 ch->user[i].bytecount = MAXPHYS;
191 ch->user[i].caps = 0;
192 ch->curr[i] = ch->user[i];
193 if (ch->pm_level > 0)
194 ch->user[i].caps |= CTS_SATA_CAPS_H_PMREQ;
195 if (ch->pm_level > 1)
196 ch->user[i].caps |= CTS_SATA_CAPS_D_PMREQ;
198 callout_init(&ch->poll_callout, 1);
202 /* reset the controller HW, the channel and device(s) */
203 while (ATA_LOCKING(dev, ATA_LF_LOCK) != ch->unit)
206 ATA_LOCKING(dev, ATA_LF_UNLOCK);
209 /* allocate DMA resources if DMA HW present*/
213 /* setup interrupt delivery */
215 ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
216 RF_SHAREABLE | RF_ACTIVE);
218 device_printf(dev, "unable to allocate interrupt\n");
221 if ((error = bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS, NULL,
222 ata_interrupt, ch, &ch->ih))) {
223 bus_release_resource(dev, SYS_RES_IRQ, rid, ch->r_irq);
224 device_printf(dev, "unable to setup interrupt\n");
229 /* probe and attach devices on this channel unless we are in early boot */
230 if (!ata_delayed_attach)
234 if (ch->flags & ATA_PERIODIC_POLL)
235 callout_reset(&ch->poll_callout, hz, ata_periodic_poll, ch);
236 mtx_lock(&ch->state_mtx);
237 /* Create the device queue for our SIM. */
238 devq = cam_simq_alloc(1);
240 device_printf(dev, "Unable to allocate simq\n");
244 /* Construct SIM entry */
245 ch->sim = cam_sim_alloc(ataaction, atapoll, "ata", ch,
246 device_get_unit(dev), &ch->state_mtx, 1, 0, devq);
247 if (ch->sim == NULL) {
248 device_printf(dev, "unable to allocate sim\n");
253 if (xpt_bus_register(ch->sim, dev, 0) != CAM_SUCCESS) {
254 device_printf(dev, "unable to register xpt bus\n");
258 if (xpt_create_path(&ch->path, /*periph*/NULL, cam_sim_path(ch->sim),
259 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
260 device_printf(dev, "unable to create path\n");
264 mtx_unlock(&ch->state_mtx);
268 xpt_bus_deregister(cam_sim_path(ch->sim));
270 cam_sim_free(ch->sim, /*free_devq*/TRUE);
273 bus_release_resource(dev, SYS_RES_IRQ, rid, ch->r_irq);
274 mtx_unlock(&ch->state_mtx);
275 if (ch->flags & ATA_PERIODIC_POLL)
276 callout_drain(&ch->poll_callout);
282 ata_detach(device_t dev)
284 struct ata_channel *ch = device_get_softc(dev);
290 /* check that we have a valid channel to detach */
294 /* grap the channel lock so no new requests gets launched */
295 mtx_lock(&ch->state_mtx);
296 ch->state |= ATA_STALL_QUEUE;
297 mtx_unlock(&ch->state_mtx);
299 if (ch->flags & ATA_PERIODIC_POLL)
300 callout_drain(&ch->poll_callout);
304 /* detach & delete all children */
305 if (!device_get_children(dev, &children, &nchildren)) {
306 for (i = 0; i < nchildren; i++)
308 device_delete_child(dev, children[i]);
309 free(children, M_TEMP);
312 taskqueue_drain(taskqueue_thread, &ch->conntask);
315 mtx_lock(&ch->state_mtx);
316 xpt_async(AC_LOST_DEVICE, ch->path, NULL);
317 xpt_free_path(ch->path);
318 xpt_bus_deregister(cam_sim_path(ch->sim));
319 cam_sim_free(ch->sim, /*free_devq*/TRUE);
321 mtx_unlock(&ch->state_mtx);
324 /* release resources */
325 bus_teardown_intr(dev, ch->r_irq, ch->ih);
326 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
329 /* free DMA resources if DMA HW present*/
333 mtx_destroy(&ch->state_mtx);
334 mtx_destroy(&ch->queue_mtx);
339 ata_conn_event(void *context, int dummy)
341 device_t dev = (device_t)context;
343 struct ata_channel *ch = device_get_softc(dev);
346 mtx_lock(&ch->state_mtx);
347 if (ch->sim == NULL) {
348 mtx_unlock(&ch->state_mtx);
352 if ((ccb = xpt_alloc_ccb_nowait()) == NULL)
354 if (xpt_create_path(&ccb->ccb_h.path, NULL,
355 cam_sim_path(ch->sim),
356 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
361 mtx_unlock(&ch->state_mtx);
368 ata_reinit(device_t dev)
370 struct ata_channel *ch = device_get_softc(dev);
371 struct ata_request *request;
376 /* check that we have a valid channel to reinit */
377 if (!ch || !ch->r_irq)
381 device_printf(dev, "reiniting channel ..\n");
383 /* poll for locking the channel */
384 while (ATA_LOCKING(dev, ATA_LF_LOCK) != ch->unit)
387 /* catch eventual request in ch->running */
388 mtx_lock(&ch->state_mtx);
389 if (ch->state & ATA_STALL_QUEUE) {
390 /* Recursive reinits and reinits during detach prohobited. */
391 mtx_unlock(&ch->state_mtx);
394 if ((request = ch->running))
395 callout_stop(&request->callout);
398 /* unconditionally grap the channel lock */
399 ch->state |= ATA_STALL_QUEUE;
400 mtx_unlock(&ch->state_mtx);
402 /* reset the controller HW, the channel and device(s) */
405 /* reinit the children and delete any that fails */
406 if (!device_get_children(dev, &children, &nchildren)) {
407 mtx_lock(&Giant); /* newbus suckage it needs Giant */
408 for (i = 0; i < nchildren; i++) {
409 /* did any children go missing ? */
410 if (children[i] && device_is_attached(children[i]) &&
411 ATA_REINIT(children[i])) {
413 * if we had a running request and its device matches
414 * this child we need to inform the request that the
417 if (request && request->dev == children[i]) {
418 request->result = ENXIO;
419 device_printf(request->dev, "FAILURE - device detached\n");
421 /* if not timeout finish request here */
422 if (!(request->flags & ATA_R_TIMEOUT))
426 device_delete_child(dev, children[i]);
429 free(children, M_TEMP);
430 mtx_unlock(&Giant); /* newbus suckage dealt with, release Giant */
433 /* if we still have a good request put it on the queue again */
434 if (request && !(request->flags & ATA_R_TIMEOUT)) {
435 device_printf(request->dev,
436 "WARNING - %s requeued due to channel reset",
437 ata_cmd2str(request));
438 if (!(request->flags & (ATA_R_ATAPI | ATA_R_CONTROL)))
439 printf(" LBA=%ju", request->u.ata.lba);
441 request->flags |= ATA_R_REQUEUE;
442 ata_queue_request(request);
445 /* we're done release the channel for new work */
446 mtx_lock(&ch->state_mtx);
447 ch->state = ATA_IDLE;
448 mtx_unlock(&ch->state_mtx);
449 ATA_LOCKING(dev, ATA_LF_UNLOCK);
451 /* Add new children. */
452 /* ata_identify(dev); */
455 device_printf(dev, "reinit done ..\n");
457 /* kick off requests on the queue */
460 xpt_freeze_simq(ch->sim, 1);
461 if ((request = ch->running)) {
463 if (ch->state == ATA_ACTIVE)
464 ch->state = ATA_IDLE;
465 callout_stop(&request->callout);
467 ch->dma.unload(request);
468 request->result = ERESTART;
469 ata_cam_end_transaction(dev, request);
471 /* reset the controller HW, the channel and device(s) */
473 /* Tell the XPT about the event */
474 xpt_async(AC_BUS_RESET, ch->path, NULL);
475 xpt_release_simq(ch->sim, TRUE);
481 ata_suspend(device_t dev)
483 struct ata_channel *ch;
485 /* check for valid device */
486 if (!dev || !(ch = device_get_softc(dev)))
490 if (ch->flags & ATA_PERIODIC_POLL)
491 callout_drain(&ch->poll_callout);
492 mtx_lock(&ch->state_mtx);
493 xpt_freeze_simq(ch->sim, 1);
494 while (ch->state != ATA_IDLE)
495 msleep(ch, &ch->state_mtx, PRIBIO, "atasusp", hz/100);
496 mtx_unlock(&ch->state_mtx);
498 /* wait for the channel to be IDLE or detached before suspending */
500 mtx_lock(&ch->state_mtx);
501 if (ch->state == ATA_IDLE) {
502 ch->state = ATA_ACTIVE;
503 mtx_unlock(&ch->state_mtx);
506 mtx_unlock(&ch->state_mtx);
507 tsleep(ch, PRIBIO, "atasusp", hz/10);
509 ATA_LOCKING(dev, ATA_LF_UNLOCK);
515 ata_resume(device_t dev)
517 struct ata_channel *ch;
520 /* check for valid device */
521 if (!dev || !(ch = device_get_softc(dev)))
525 mtx_lock(&ch->state_mtx);
526 error = ata_reinit(dev);
527 xpt_release_simq(ch->sim, TRUE);
528 mtx_unlock(&ch->state_mtx);
529 if (ch->flags & ATA_PERIODIC_POLL)
530 callout_reset(&ch->poll_callout, hz, ata_periodic_poll, ch);
532 /* reinit the devices, we dont know what mode/state they are in */
533 error = ata_reinit(dev);
534 /* kick off requests on the queue */
541 ata_interrupt(void *data)
544 struct ata_channel *ch = (struct ata_channel *)data;
546 mtx_lock(&ch->state_mtx);
547 xpt_batch_start(ch->sim);
549 ata_interrupt_locked(data);
551 xpt_batch_done(ch->sim);
552 mtx_unlock(&ch->state_mtx);
557 ata_interrupt_locked(void *data)
559 struct ata_channel *ch = (struct ata_channel *)data;
560 struct ata_request *request;
563 mtx_lock(&ch->state_mtx);
566 /* ignore interrupt if its not for us */
567 if (ch->hw.status && !ch->hw.status(ch->dev))
570 /* do we have a running request */
571 if (!(request = ch->running))
574 ATA_DEBUG_RQ(request, "interrupt");
576 /* safetycheck for the right state */
577 if (ch->state == ATA_IDLE) {
578 device_printf(request->dev, "interrupt on idle channel ignored\n");
583 * we have the HW locks, so end the transaction for this request
584 * if it finishes immediately otherwise wait for next interrupt
586 if (ch->hw.end_transaction(request) == ATA_OP_FINISHED) {
588 if (ch->state == ATA_ACTIVE)
589 ch->state = ATA_IDLE;
591 ata_cam_end_transaction(ch->dev, request);
593 mtx_unlock(&ch->state_mtx);
594 ATA_LOCKING(ch->dev, ATA_LF_UNLOCK);
601 mtx_unlock(&ch->state_mtx);
607 ata_periodic_poll(void *data)
609 struct ata_channel *ch = (struct ata_channel *)data;
611 callout_reset(&ch->poll_callout, hz, ata_periodic_poll, ch);
617 ata_print_cable(device_t dev, u_int8_t *who)
620 "DMA limited to UDMA33, %s found non-ATA66 cable\n", who);
625 ata_check_80pin(device_t dev, int mode)
627 struct ata_device *atadev = device_get_softc(dev);
629 if (!ata_dma_check_80pin) {
631 device_printf(dev, "Skipping 80pin cable check\n");
635 if (mode > ATA_UDMA2 && !(atadev->param.hwres & ATA_CABLE_ID)) {
636 ata_print_cable(dev, "device");
645 ata_setmode(device_t dev)
647 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
648 struct ata_device *atadev = device_get_softc(dev);
649 int error, mode, pmode;
653 pmode = mode = ata_limit_mode(dev, mode, ATA_DMA_MAX);
654 mode = ATA_SETMODE(device_get_parent(dev), atadev->unit, mode);
655 if ((ch->flags & (ATA_CHECKS_CABLE | ATA_SATA)) == 0)
656 mode = ata_check_80pin(dev, mode);
657 } while (pmode != mode); /* Interate till successfull negotiation. */
658 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
660 device_printf(dev, "%ssetting %s\n",
661 (error) ? "FAILURE " : "", ata_mode2str(mode));
667 * device related interfaces
671 ata_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
672 int32_t flag, struct thread *td)
674 device_t device, *children;
675 struct ata_ioc_devices *devices = (struct ata_ioc_devices *)data;
676 int *value = (int *)data;
677 int i, nchildren, error = ENOTTY;
680 case IOCATAGMAXCHANNEL:
681 /* In case we have channel 0..n this will return n+1. */
682 *value = devclass_get_maxunit(ata_devclass);
687 if (*value >= devclass_get_maxunit(ata_devclass) ||
688 !(device = devclass_get_device(ata_devclass, *value)) ||
689 !device_is_attached(device))
691 error = ata_reinit(device);
695 if (*value >= devclass_get_maxunit(ata_devclass) ||
696 !(device = devclass_get_device(ata_devclass, *value)) ||
697 !device_is_attached(device))
699 error = DEVICE_ATTACH(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_DETACH(device);
711 if (devices->channel >= devclass_get_maxunit(ata_devclass) ||
712 !(device = devclass_get_device(ata_devclass, devices->channel)) ||
713 !device_is_attached(device))
715 bzero(devices->name[0], 32);
716 bzero(&devices->params[0], sizeof(struct ata_params));
717 bzero(devices->name[1], 32);
718 bzero(&devices->params[1], sizeof(struct ata_params));
719 if (!device_get_children(device, &children, &nchildren)) {
720 for (i = 0; i < nchildren; i++) {
721 if (children[i] && device_is_attached(children[i])) {
722 struct ata_device *atadev = device_get_softc(children[i]);
724 if (atadev->unit == ATA_MASTER) { /* XXX SOS PM */
725 strncpy(devices->name[0],
726 device_get_nameunit(children[i]), 32);
727 bcopy(&atadev->param, &devices->params[0],
728 sizeof(struct ata_params));
730 if (atadev->unit == ATA_SLAVE) { /* XXX SOS PM */
731 strncpy(devices->name[1],
732 device_get_nameunit(children[i]), 32);
733 bcopy(&atadev->param, &devices->params[1],
734 sizeof(struct ata_params));
738 free(children, M_TEMP);
746 if (ata_raid_ioctl_func)
747 error = ata_raid_ioctl_func(cmd, data);
755 ata_device_ioctl(device_t dev, u_long cmd, caddr_t data)
757 struct ata_device *atadev = device_get_softc(dev);
758 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
759 struct ata_ioc_request *ioc_request = (struct ata_ioc_request *)data;
760 struct ata_params *params = (struct ata_params *)data;
761 int *mode = (int *)data;
762 struct ata_request *request;
768 if (ioc_request->count >
769 (ch->dma.max_iosize ? ch->dma.max_iosize : DFLTPHYS)) {
772 if (!(buf = malloc(ioc_request->count, M_ATA, M_NOWAIT))) {
775 if (!(request = ata_alloc_request())) {
779 request->dev = atadev->dev;
780 if (ioc_request->flags & ATA_CMD_WRITE) {
781 error = copyin(ioc_request->data, buf, ioc_request->count);
784 ata_free_request(request);
788 if (ioc_request->flags & ATA_CMD_ATAPI) {
789 request->flags = ATA_R_ATAPI;
790 bcopy(ioc_request->u.atapi.ccb, request->u.atapi.ccb, 16);
793 request->u.ata.command = ioc_request->u.ata.command;
794 request->u.ata.feature = ioc_request->u.ata.feature;
795 request->u.ata.lba = ioc_request->u.ata.lba;
796 request->u.ata.count = ioc_request->u.ata.count;
798 request->timeout = ioc_request->timeout;
800 request->bytecount = ioc_request->count;
801 request->transfersize = request->bytecount;
802 if (ioc_request->flags & ATA_CMD_CONTROL)
803 request->flags |= ATA_R_CONTROL;
804 if (ioc_request->flags & ATA_CMD_READ)
805 request->flags |= ATA_R_READ;
806 if (ioc_request->flags & ATA_CMD_WRITE)
807 request->flags |= ATA_R_WRITE;
808 ata_queue_request(request);
809 if (request->flags & ATA_R_ATAPI) {
810 bcopy(&request->u.atapi.sense, &ioc_request->u.atapi.sense,
811 sizeof(struct atapi_sense));
814 ioc_request->u.ata.command = request->u.ata.command;
815 ioc_request->u.ata.feature = request->u.ata.feature;
816 ioc_request->u.ata.lba = request->u.ata.lba;
817 ioc_request->u.ata.count = request->u.ata.count;
819 ioc_request->error = request->result;
820 if (ioc_request->flags & ATA_CMD_READ)
821 error = copyout(buf, ioc_request->data, ioc_request->count);
825 ata_free_request(request);
829 ata_getparam(atadev, 0);
830 bcopy(&atadev->param, params, sizeof(struct ata_params));
834 atadev->mode = *mode;
839 *mode = atadev->mode |
840 (ATA_GETREV(device_get_parent(dev), atadev->unit) << 8);
842 case IOCATASSPINDOWN:
843 atadev->spindown = *mode;
845 case IOCATAGSPINDOWN:
846 *mode = atadev->spindown;
856 ata_boot_attach(void)
858 struct ata_channel *ch;
861 mtx_lock(&Giant); /* newbus suckage it needs Giant */
863 /* kick of probe and attach on all channels */
864 for (ctlr = 0; ctlr < devclass_get_maxunit(ata_devclass); ctlr++) {
865 if ((ch = devclass_get_softc(ata_devclass, ctlr))) {
866 ata_identify(ch->dev);
870 /* release the hook that got us here, we are only needed once during boot */
871 if (ata_delayed_attach) {
872 config_intrhook_disestablish(ata_delayed_attach);
873 free(ata_delayed_attach, M_TEMP);
874 ata_delayed_attach = NULL;
877 mtx_unlock(&Giant); /* newbus suckage dealt with, release Giant */
882 * misc support functions
886 ata_add_child(device_t parent, struct ata_device *atadev, int unit)
890 if ((child = device_add_child(parent, (unit < 0) ? NULL : "ad", unit))) {
891 device_set_softc(child, atadev);
894 atadev->max_iosize = DEV_BSIZE;
895 atadev->mode = ATA_PIO_MAX;
903 ata_getparam(struct ata_device *atadev, int init)
905 struct ata_channel *ch = device_get_softc(device_get_parent(atadev->dev));
906 struct ata_request *request;
909 u_int8_t command = 0;
910 int error = ENOMEM, retries = 2, mode = -1;
912 if (ch->devices & (ATA_ATA_MASTER << atadev->unit))
913 command = ATA_ATA_IDENTIFY;
914 if (ch->devices & (ATA_ATAPI_MASTER << atadev->unit))
915 command = ATA_ATAPI_IDENTIFY;
919 while (retries-- > 0 && error) {
920 if (!(request = ata_alloc_request()))
922 request->dev = atadev->dev;
923 request->timeout = 1;
924 request->retries = 0;
925 request->u.ata.command = command;
926 request->flags = (ATA_R_READ|ATA_R_AT_HEAD|ATA_R_DIRECT);
928 request->flags |= ATA_R_QUIET;
929 request->data = (void *)&atadev->param;
930 request->bytecount = sizeof(struct ata_params);
931 request->donecount = 0;
932 request->transfersize = DEV_BSIZE;
933 ata_queue_request(request);
934 error = request->result;
935 ata_free_request(request);
938 if (!error && (isprint(atadev->param.model[0]) ||
939 isprint(atadev->param.model[1]))) {
940 struct ata_params *atacap = &atadev->param;
943 for (ptr = (int16_t *)atacap;
944 ptr < (int16_t *)atacap + sizeof(struct ata_params)/2; ptr++) {
945 *ptr = le16toh(*ptr);
947 if (!(!strncmp(atacap->model, "FX", 2) ||
948 !strncmp(atacap->model, "NEC", 3) ||
949 !strncmp(atacap->model, "Pioneer", 7) ||
950 !strncmp(atacap->model, "SHARP", 5))) {
951 bswap(atacap->model, sizeof(atacap->model));
952 bswap(atacap->revision, sizeof(atacap->revision));
953 bswap(atacap->serial, sizeof(atacap->serial));
955 btrim(atacap->model, sizeof(atacap->model));
956 bpack(atacap->model, atacap->model, sizeof(atacap->model));
957 btrim(atacap->revision, sizeof(atacap->revision));
958 bpack(atacap->revision, atacap->revision, sizeof(atacap->revision));
959 btrim(atacap->serial, sizeof(atacap->serial));
960 bpack(atacap->serial, atacap->serial, sizeof(atacap->serial));
963 printf("ata%d-%s: pio=%s wdma=%s udma=%s cable=%s wire\n",
964 device_get_unit(ch->dev),
965 ata_unit2str(atadev),
966 ata_mode2str(ata_pmode(atacap)),
967 ata_mode2str(ata_wmode(atacap)),
968 ata_mode2str(ata_umode(atacap)),
969 (atacap->hwres & ATA_CABLE_ID) ? "80":"40");
974 sprintf(buffer, "%.40s/%.8s", atacap->model, atacap->revision);
975 device_set_desc_copy(atadev->dev, buffer);
976 if ((atadev->param.config & ATA_PROTO_ATAPI) &&
977 (atadev->param.config != ATA_CFA_MAGIC1) &&
978 (atadev->param.config != ATA_CFA_MAGIC2)) {
980 (atadev->param.config & ATA_DRQ_MASK) != ATA_DRQ_INTR &&
981 ata_umode(&atadev->param) >= ATA_UDMA2)
982 atadev->mode = ATA_DMA_MAX;
986 (ata_umode(&atadev->param) > 0 ||
987 ata_wmode(&atadev->param) > 0))
988 atadev->mode = ATA_DMA_MAX;
990 snprintf(buf, sizeof(buf), "dev%d.mode", atadev->unit);
991 if (resource_string_value(device_get_name(ch->dev),
992 device_get_unit(ch->dev), buf, &res) == 0)
993 mode = ata_str2mode(res);
994 else if (resource_string_value(device_get_name(ch->dev),
995 device_get_unit(ch->dev), "mode", &res) == 0)
996 mode = ata_str2mode(res);
1011 ata_identify(device_t dev)
1013 struct ata_channel *ch = device_get_softc(dev);
1014 struct ata_device *atadev;
1016 device_t child, master = NULL;
1017 int nchildren, i, n = ch->devices;
1020 device_printf(dev, "Identifying devices: %08x\n", ch->devices);
1023 /* Skip existing devices. */
1024 if (!device_get_children(dev, &children, &nchildren)) {
1025 for (i = 0; i < nchildren; i++) {
1026 if (children[i] && (atadev = device_get_softc(children[i])))
1027 n &= ~((ATA_ATA_MASTER | ATA_ATAPI_MASTER) << atadev->unit);
1029 free(children, M_TEMP);
1031 /* Create new devices. */
1033 device_printf(dev, "New devices: %08x\n", n);
1038 for (i = 0; i < ATA_PM; ++i) {
1039 if (n & (((ATA_ATA_MASTER | ATA_ATAPI_MASTER) << i))) {
1042 if (!(atadev = malloc(sizeof(struct ata_device),
1043 M_ATA, M_NOWAIT | M_ZERO))) {
1044 device_printf(dev, "out of memory\n");
1048 #ifdef ATA_STATIC_ID
1049 if (n & (ATA_ATA_MASTER << i))
1050 unit = (device_get_unit(dev) << 1) + i;
1052 if ((child = ata_add_child(dev, atadev, unit))) {
1054 * PATA slave should be identified first, to allow
1055 * device cable detection on master to work properly.
1057 if (i == 0 && (n & ATA_PORTMULTIPLIER) == 0 &&
1058 (n & ((ATA_ATA_MASTER | ATA_ATAPI_MASTER) << 1)) != 0) {
1062 if (ata_getparam(atadev, 1)) {
1063 device_delete_child(dev, child);
1064 free(atadev, M_ATA);
1068 free(atadev, M_ATA);
1072 atadev = device_get_softc(master);
1073 if (ata_getparam(atadev, 1)) {
1074 device_delete_child(dev, master);
1075 free(atadev, M_ATA);
1078 bus_generic_probe(dev);
1079 bus_generic_attach(dev);
1086 ata_default_registers(device_t dev)
1088 struct ata_channel *ch = device_get_softc(dev);
1090 /* fill in the defaults from whats setup already */
1091 ch->r_io[ATA_ERROR].res = ch->r_io[ATA_FEATURE].res;
1092 ch->r_io[ATA_ERROR].offset = ch->r_io[ATA_FEATURE].offset;
1093 ch->r_io[ATA_IREASON].res = ch->r_io[ATA_COUNT].res;
1094 ch->r_io[ATA_IREASON].offset = ch->r_io[ATA_COUNT].offset;
1095 ch->r_io[ATA_STATUS].res = ch->r_io[ATA_COMMAND].res;
1096 ch->r_io[ATA_STATUS].offset = ch->r_io[ATA_COMMAND].offset;
1097 ch->r_io[ATA_ALTSTAT].res = ch->r_io[ATA_CONTROL].res;
1098 ch->r_io[ATA_ALTSTAT].offset = ch->r_io[ATA_CONTROL].offset;
1102 ata_modify_if_48bit(struct ata_request *request)
1104 struct ata_channel *ch = device_get_softc(request->parent);
1105 struct ata_device *atadev = device_get_softc(request->dev);
1107 request->flags &= ~ATA_R_48BIT;
1109 if (((request->u.ata.lba + request->u.ata.count) >= ATA_MAX_28BIT_LBA ||
1110 request->u.ata.count > 256) &&
1111 atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) {
1113 /* translate command into 48bit version */
1114 switch (request->u.ata.command) {
1116 request->u.ata.command = ATA_READ48;
1119 request->u.ata.command = ATA_READ_MUL48;
1122 if (ch->flags & ATA_NO_48BIT_DMA) {
1123 if (request->transfersize > DEV_BSIZE)
1124 request->u.ata.command = ATA_READ_MUL48;
1126 request->u.ata.command = ATA_READ48;
1127 request->flags &= ~ATA_R_DMA;
1130 request->u.ata.command = ATA_READ_DMA48;
1132 case ATA_READ_DMA_QUEUED:
1133 if (ch->flags & ATA_NO_48BIT_DMA) {
1134 if (request->transfersize > DEV_BSIZE)
1135 request->u.ata.command = ATA_READ_MUL48;
1137 request->u.ata.command = ATA_READ48;
1138 request->flags &= ~ATA_R_DMA;
1141 request->u.ata.command = ATA_READ_DMA_QUEUED48;
1144 request->u.ata.command = ATA_WRITE48;
1147 request->u.ata.command = ATA_WRITE_MUL48;
1150 if (ch->flags & ATA_NO_48BIT_DMA) {
1151 if (request->transfersize > DEV_BSIZE)
1152 request->u.ata.command = ATA_WRITE_MUL48;
1154 request->u.ata.command = ATA_WRITE48;
1155 request->flags &= ~ATA_R_DMA;
1158 request->u.ata.command = ATA_WRITE_DMA48;
1160 case ATA_WRITE_DMA_QUEUED:
1161 if (ch->flags & ATA_NO_48BIT_DMA) {
1162 if (request->transfersize > DEV_BSIZE)
1163 request->u.ata.command = ATA_WRITE_MUL48;
1165 request->u.ata.command = ATA_WRITE48;
1166 request->u.ata.command = ATA_WRITE48;
1167 request->flags &= ~ATA_R_DMA;
1170 request->u.ata.command = ATA_WRITE_DMA_QUEUED48;
1172 case ATA_FLUSHCACHE:
1173 request->u.ata.command = ATA_FLUSHCACHE48;
1175 case ATA_SET_MAX_ADDRESS:
1176 request->u.ata.command = ATA_SET_MAX_ADDRESS48;
1181 request->flags |= ATA_R_48BIT;
1183 else if (atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) {
1185 /* translate command into 48bit version */
1186 switch (request->u.ata.command) {
1187 case ATA_FLUSHCACHE:
1188 request->u.ata.command = ATA_FLUSHCACHE48;
1190 case ATA_READ_NATIVE_MAX_ADDRESS:
1191 request->u.ata.command = ATA_READ_NATIVE_MAX_ADDRESS48;
1193 case ATA_SET_MAX_ADDRESS:
1194 request->u.ata.command = ATA_SET_MAX_ADDRESS48;
1199 request->flags |= ATA_R_48BIT;
1204 ata_udelay(int interval)
1206 /* for now just use DELAY, the timer/sleep subsytems are not there yet */
1207 if (1 || interval < (1000000/hz) || ata_delayed_attach)
1210 pause("ataslp", interval/(1000000/hz));
1215 ata_unit2str(struct ata_device *atadev)
1217 struct ata_channel *ch = device_get_softc(device_get_parent(atadev->dev));
1220 if (ch->devices & ATA_PORTMULTIPLIER)
1221 sprintf(str, "port%d", atadev->unit);
1223 sprintf(str, "%s", atadev->unit == ATA_MASTER ? "master" : "slave");
1229 ata_mode2str(int mode)
1232 case -1: return "UNSUPPORTED";
1233 case ATA_PIO0: return "PIO0";
1234 case ATA_PIO1: return "PIO1";
1235 case ATA_PIO2: return "PIO2";
1236 case ATA_PIO3: return "PIO3";
1237 case ATA_PIO4: return "PIO4";
1238 case ATA_WDMA0: return "WDMA0";
1239 case ATA_WDMA1: return "WDMA1";
1240 case ATA_WDMA2: return "WDMA2";
1241 case ATA_UDMA0: return "UDMA16";
1242 case ATA_UDMA1: return "UDMA25";
1243 case ATA_UDMA2: return "UDMA33";
1244 case ATA_UDMA3: return "UDMA40";
1245 case ATA_UDMA4: return "UDMA66";
1246 case ATA_UDMA5: return "UDMA100";
1247 case ATA_UDMA6: return "UDMA133";
1248 case ATA_SA150: return "SATA150";
1249 case ATA_SA300: return "SATA300";
1251 if (mode & ATA_DMA_MASK)
1259 ata_str2mode(const char *str)
1262 if (!strcasecmp(str, "PIO0")) return (ATA_PIO0);
1263 if (!strcasecmp(str, "PIO1")) return (ATA_PIO1);
1264 if (!strcasecmp(str, "PIO2")) return (ATA_PIO2);
1265 if (!strcasecmp(str, "PIO3")) return (ATA_PIO3);
1266 if (!strcasecmp(str, "PIO4")) return (ATA_PIO4);
1267 if (!strcasecmp(str, "WDMA0")) return (ATA_WDMA0);
1268 if (!strcasecmp(str, "WDMA1")) return (ATA_WDMA1);
1269 if (!strcasecmp(str, "WDMA2")) return (ATA_WDMA2);
1270 if (!strcasecmp(str, "UDMA0")) return (ATA_UDMA0);
1271 if (!strcasecmp(str, "UDMA16")) return (ATA_UDMA0);
1272 if (!strcasecmp(str, "UDMA1")) return (ATA_UDMA1);
1273 if (!strcasecmp(str, "UDMA25")) return (ATA_UDMA1);
1274 if (!strcasecmp(str, "UDMA2")) return (ATA_UDMA2);
1275 if (!strcasecmp(str, "UDMA33")) return (ATA_UDMA2);
1276 if (!strcasecmp(str, "UDMA3")) return (ATA_UDMA3);
1277 if (!strcasecmp(str, "UDMA44")) return (ATA_UDMA3);
1278 if (!strcasecmp(str, "UDMA4")) return (ATA_UDMA4);
1279 if (!strcasecmp(str, "UDMA66")) return (ATA_UDMA4);
1280 if (!strcasecmp(str, "UDMA5")) return (ATA_UDMA5);
1281 if (!strcasecmp(str, "UDMA100")) return (ATA_UDMA5);
1282 if (!strcasecmp(str, "UDMA6")) return (ATA_UDMA6);
1283 if (!strcasecmp(str, "UDMA133")) return (ATA_UDMA6);
1289 ata_satarev2str(int rev)
1293 case 1: return "SATA 1.5Gb/s";
1294 case 2: return "SATA 3Gb/s";
1295 case 3: return "SATA 6Gb/s";
1296 case 0xff: return "SATA";
1297 default: return "???";
1303 ata_atapi(device_t dev, int target)
1305 struct ata_channel *ch = device_get_softc(dev);
1307 return (ch->devices & (ATA_ATAPI_MASTER << target));
1312 ata_pmode(struct ata_params *ap)
1314 if (ap->atavalid & ATA_FLAG_64_70) {
1315 if (ap->apiomodes & 0x02)
1317 if (ap->apiomodes & 0x01)
1320 if (ap->mwdmamodes & 0x04)
1322 if (ap->mwdmamodes & 0x02)
1324 if (ap->mwdmamodes & 0x01)
1326 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200)
1328 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100)
1330 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000)
1338 ata_wmode(struct ata_params *ap)
1340 if (ap->mwdmamodes & 0x04)
1342 if (ap->mwdmamodes & 0x02)
1344 if (ap->mwdmamodes & 0x01)
1352 ata_umode(struct ata_params *ap)
1354 if (ap->atavalid & ATA_FLAG_88) {
1355 if (ap->udmamodes & 0x40)
1357 if (ap->udmamodes & 0x20)
1359 if (ap->udmamodes & 0x10)
1361 if (ap->udmamodes & 0x08)
1363 if (ap->udmamodes & 0x04)
1365 if (ap->udmamodes & 0x02)
1367 if (ap->udmamodes & 0x01)
1376 ata_limit_mode(device_t dev, int mode, int maxmode)
1378 struct ata_device *atadev = device_get_softc(dev);
1380 if (maxmode && mode > maxmode)
1383 if (mode >= ATA_UDMA0 && ata_umode(&atadev->param) > 0)
1384 return min(mode, ata_umode(&atadev->param));
1386 if (mode >= ATA_WDMA0 && ata_wmode(&atadev->param) > 0)
1387 return min(mode, ata_wmode(&atadev->param));
1389 if (mode > ata_pmode(&atadev->param))
1390 return min(mode, ata_pmode(&atadev->param));
1398 bswap(int8_t *buf, int len)
1400 u_int16_t *ptr = (u_int16_t*)(buf + len);
1402 while (--ptr >= (u_int16_t*)buf)
1409 btrim(int8_t *buf, int len)
1413 for (ptr = buf; ptr < buf+len; ++ptr)
1414 if (!*ptr || *ptr == '_')
1416 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
1423 bpack(int8_t *src, int8_t *dst, int len)
1427 for (i = j = blank = 0 ; i < len; i++) {
1428 if (blank && src[i] == ' ') continue;
1429 if (blank && src[i] != ' ') {
1434 if (src[i] == ' ') {
1448 ata_cam_begin_transaction(device_t dev, union ccb *ccb)
1450 struct ata_channel *ch = device_get_softc(dev);
1451 struct ata_request *request;
1453 if (!(request = ata_alloc_request())) {
1454 device_printf(dev, "FAILURE - out of memory in start\n");
1455 ccb->ccb_h.status = CAM_REQ_INVALID;
1459 bzero(request, sizeof(*request));
1462 request->dev = NULL;
1463 request->parent = dev;
1464 request->unit = ccb->ccb_h.target_id;
1465 if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1466 request->data = ccb->ataio.data_ptr;
1467 request->bytecount = ccb->ataio.dxfer_len;
1468 request->u.ata.command = ccb->ataio.cmd.command;
1469 request->u.ata.feature = ((uint16_t)ccb->ataio.cmd.features_exp << 8) |
1470 (uint16_t)ccb->ataio.cmd.features;
1471 request->u.ata.count = ((uint16_t)ccb->ataio.cmd.sector_count_exp << 8) |
1472 (uint16_t)ccb->ataio.cmd.sector_count;
1473 if (ccb->ataio.cmd.flags & CAM_ATAIO_48BIT) {
1474 request->flags |= ATA_R_48BIT;
1475 request->u.ata.lba =
1476 ((uint64_t)ccb->ataio.cmd.lba_high_exp << 40) |
1477 ((uint64_t)ccb->ataio.cmd.lba_mid_exp << 32) |
1478 ((uint64_t)ccb->ataio.cmd.lba_low_exp << 24);
1480 request->u.ata.lba =
1481 ((uint64_t)(ccb->ataio.cmd.device & 0x0f) << 24);
1483 request->u.ata.lba |= ((uint64_t)ccb->ataio.cmd.lba_high << 16) |
1484 ((uint64_t)ccb->ataio.cmd.lba_mid << 8) |
1485 (uint64_t)ccb->ataio.cmd.lba_low;
1486 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE &&
1487 ccb->ataio.cmd.flags & CAM_ATAIO_DMA)
1488 request->flags |= ATA_R_DMA;
1489 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1490 request->flags |= ATA_R_READ;
1491 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
1492 request->flags |= ATA_R_WRITE;
1494 request->data = ccb->csio.data_ptr;
1495 request->bytecount = ccb->csio.dxfer_len;
1496 bcopy((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
1497 ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes,
1498 request->u.atapi.ccb, ccb->csio.cdb_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 ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE &&
1503 ch->curr[ccb->ccb_h.target_id].mode >= ATA_DMA)
1504 request->flags |= ATA_R_DMA;
1505 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1506 request->flags |= ATA_R_READ;
1507 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
1508 request->flags |= ATA_R_WRITE;
1510 request->transfersize = min(request->bytecount,
1511 ch->curr[ccb->ccb_h.target_id].bytecount);
1512 request->retries = 0;
1513 request->timeout = (ccb->ccb_h.timeout + 999) / 1000;
1514 callout_init_mtx(&request->callout, &ch->state_mtx, CALLOUT_RETURNUNLOCKED);
1517 ch->running = request;
1518 ch->state = ATA_ACTIVE;
1519 if (ch->hw.begin_transaction(request) == ATA_OP_FINISHED) {
1521 ch->state = ATA_IDLE;
1522 ata_cam_end_transaction(dev, request);
1528 ata_cam_request_sense(device_t dev, struct ata_request *request)
1530 struct ata_channel *ch = device_get_softc(dev);
1531 union ccb *ccb = request->ccb;
1533 ch->requestsense = 1;
1535 bzero(request, sizeof(*request));
1536 request->dev = NULL;
1537 request->parent = dev;
1538 request->unit = ccb->ccb_h.target_id;
1539 request->data = (void *)&ccb->csio.sense_data;
1540 request->bytecount = ccb->csio.sense_len;
1541 request->u.atapi.ccb[0] = ATAPI_REQUEST_SENSE;
1542 request->u.atapi.ccb[4] = ccb->csio.sense_len;
1543 request->flags |= ATA_R_ATAPI;
1544 if (ch->curr[ccb->ccb_h.target_id].atapi == 16)
1545 request->flags |= ATA_R_ATAPI16;
1546 if (ch->curr[ccb->ccb_h.target_id].mode >= ATA_DMA)
1547 request->flags |= ATA_R_DMA;
1548 request->flags |= ATA_R_READ;
1549 request->transfersize = min(request->bytecount,
1550 ch->curr[ccb->ccb_h.target_id].bytecount);
1551 request->retries = 0;
1552 request->timeout = (ccb->ccb_h.timeout + 999) / 1000;
1553 callout_init_mtx(&request->callout, &ch->state_mtx, CALLOUT_RETURNUNLOCKED);
1556 ch->running = request;
1557 ch->state = ATA_ACTIVE;
1558 if (ch->hw.begin_transaction(request) == ATA_OP_FINISHED) {
1560 ch->state = ATA_IDLE;
1561 ata_cam_end_transaction(dev, request);
1567 ata_cam_process_sense(device_t dev, struct ata_request *request)
1569 struct ata_channel *ch = device_get_softc(dev);
1570 union ccb *ccb = request->ccb;
1573 ch->requestsense = 0;
1575 if (request->flags & ATA_R_TIMEOUT)
1577 if ((request->flags & ATA_R_TIMEOUT) == 0 &&
1578 (request->status & ATA_S_ERROR) == 0 &&
1579 request->result == 0) {
1580 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
1582 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1583 ccb->ccb_h.status |= CAM_AUTOSENSE_FAIL;
1586 ata_free_request(request);
1588 /* Do error recovery if needed. */
1594 ata_cam_end_transaction(device_t dev, struct ata_request *request)
1596 struct ata_channel *ch = device_get_softc(dev);
1597 union ccb *ccb = request->ccb;
1600 if (ch->requestsense) {
1601 ata_cam_process_sense(dev, request);
1605 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1606 if (request->flags & ATA_R_TIMEOUT) {
1607 xpt_freeze_simq(ch->sim, 1);
1608 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1609 ccb->ccb_h.status |= CAM_CMD_TIMEOUT | CAM_RELEASE_SIMQ;
1611 } else if (request->status & ATA_S_ERROR) {
1612 if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1613 ccb->ccb_h.status |= CAM_ATA_STATUS_ERROR;
1615 ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1616 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1618 } else if (request->result == ERESTART)
1619 ccb->ccb_h.status |= CAM_REQUEUE_REQ;
1620 else if (request->result != 0)
1621 ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
1623 ccb->ccb_h.status |= CAM_REQ_CMP;
1624 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP &&
1625 !(ccb->ccb_h.status & CAM_DEV_QFRZN)) {
1626 xpt_freeze_devq(ccb->ccb_h.path, 1);
1627 ccb->ccb_h.status |= CAM_DEV_QFRZN;
1629 if (ccb->ccb_h.func_code == XPT_ATA_IO &&
1630 ((request->status & ATA_S_ERROR) ||
1631 (ccb->ataio.cmd.flags & CAM_ATAIO_NEEDRESULT))) {
1632 struct ata_res *res = &ccb->ataio.res;
1633 res->status = request->status;
1634 res->error = request->error;
1635 res->lba_low = request->u.ata.lba;
1636 res->lba_mid = request->u.ata.lba >> 8;
1637 res->lba_high = request->u.ata.lba >> 16;
1638 res->device = request->u.ata.lba >> 24;
1639 res->lba_low_exp = request->u.ata.lba >> 24;
1640 res->lba_mid_exp = request->u.ata.lba >> 32;
1641 res->lba_high_exp = request->u.ata.lba >> 40;
1642 res->sector_count = request->u.ata.count;
1643 res->sector_count_exp = request->u.ata.count >> 8;
1645 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1646 if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1648 ccb->ataio.dxfer_len - request->donecount;
1651 ccb->csio.dxfer_len - request->donecount;
1654 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_SCSI_STATUS_ERROR &&
1655 (ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)
1656 ata_cam_request_sense(dev, request);
1658 ata_free_request(request);
1661 /* Do error recovery if needed. */
1667 ata_check_ids(device_t dev, union ccb *ccb)
1669 struct ata_channel *ch = device_get_softc(dev);
1671 if (ccb->ccb_h.target_id > ((ch->flags & ATA_NO_SLAVE) ? 0 : 1)) {
1672 ccb->ccb_h.status = CAM_TID_INVALID;
1676 if (ccb->ccb_h.target_lun != 0) {
1677 ccb->ccb_h.status = CAM_LUN_INVALID;
1685 ataaction(struct cam_sim *sim, union ccb *ccb)
1687 device_t dev, parent;
1688 struct ata_channel *ch;
1690 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ataaction func_code=%x\n",
1691 ccb->ccb_h.func_code));
1693 ch = (struct ata_channel *)cam_sim_softc(sim);
1695 switch (ccb->ccb_h.func_code) {
1696 /* Common cases first */
1697 case XPT_ATA_IO: /* Execute the requested I/O operation */
1699 if (ata_check_ids(dev, ccb))
1701 if ((ch->devices & ((ATA_ATA_MASTER | ATA_ATAPI_MASTER)
1702 << ccb->ccb_h.target_id)) == 0) {
1703 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
1707 device_printf(dev, "already running!\n");
1708 if (ccb->ccb_h.func_code == XPT_ATA_IO &&
1709 (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) &&
1710 (ccb->ataio.cmd.control & ATA_A_RESET)) {
1711 struct ata_res *res = &ccb->ataio.res;
1713 bzero(res, sizeof(*res));
1714 if (ch->devices & (ATA_ATA_MASTER << ccb->ccb_h.target_id)) {
1718 res->lba_high = 0xeb;
1719 res->lba_mid = 0x14;
1721 ccb->ccb_h.status = CAM_REQ_CMP;
1724 ata_cam_begin_transaction(dev, ccb);
1726 case XPT_EN_LUN: /* Enable LUN as a target */
1727 case XPT_TARGET_IO: /* Execute target I/O request */
1728 case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */
1729 case XPT_CONT_TARGET_IO: /* Continue Host Target I/O Connection*/
1730 case XPT_ABORT: /* Abort the specified CCB */
1732 ccb->ccb_h.status = CAM_REQ_INVALID;
1734 case XPT_SET_TRAN_SETTINGS:
1736 struct ccb_trans_settings *cts = &ccb->cts;
1737 struct ata_cam_device *d;
1739 if (ata_check_ids(dev, ccb))
1741 if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
1742 d = &ch->curr[ccb->ccb_h.target_id];
1744 d = &ch->user[ccb->ccb_h.target_id];
1745 if (ch->flags & ATA_SATA) {
1746 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_REVISION)
1747 d->revision = cts->xport_specific.sata.revision;
1748 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_MODE) {
1749 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
1750 d->mode = ATA_SETMODE(ch->dev,
1751 ccb->ccb_h.target_id,
1752 cts->xport_specific.sata.mode);
1754 d->mode = cts->xport_specific.sata.mode;
1756 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
1757 d->bytecount = min(8192, cts->xport_specific.sata.bytecount);
1758 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_ATAPI)
1759 d->atapi = cts->xport_specific.sata.atapi;
1760 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1761 d->caps = cts->xport_specific.sata.caps;
1763 if (cts->xport_specific.ata.valid & CTS_ATA_VALID_MODE) {
1764 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
1765 d->mode = ATA_SETMODE(ch->dev,
1766 ccb->ccb_h.target_id,
1767 cts->xport_specific.ata.mode);
1769 d->mode = cts->xport_specific.ata.mode;
1771 if (cts->xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
1772 d->bytecount = cts->xport_specific.ata.bytecount;
1773 if (cts->xport_specific.ata.valid & CTS_ATA_VALID_ATAPI)
1774 d->atapi = cts->xport_specific.ata.atapi;
1776 ccb->ccb_h.status = CAM_REQ_CMP;
1779 case XPT_GET_TRAN_SETTINGS:
1781 struct ccb_trans_settings *cts = &ccb->cts;
1782 struct ata_cam_device *d;
1784 if (ata_check_ids(dev, ccb))
1786 if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
1787 d = &ch->curr[ccb->ccb_h.target_id];
1789 d = &ch->user[ccb->ccb_h.target_id];
1790 cts->protocol = PROTO_UNSPECIFIED;
1791 cts->protocol_version = PROTO_VERSION_UNSPECIFIED;
1792 if (ch->flags & ATA_SATA) {
1793 cts->transport = XPORT_SATA;
1794 cts->transport_version = XPORT_VERSION_UNSPECIFIED;
1795 cts->xport_specific.sata.valid = 0;
1796 cts->xport_specific.sata.mode = d->mode;
1797 cts->xport_specific.sata.valid |= CTS_SATA_VALID_MODE;
1798 cts->xport_specific.sata.bytecount = d->bytecount;
1799 cts->xport_specific.sata.valid |= CTS_SATA_VALID_BYTECOUNT;
1800 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
1801 cts->xport_specific.sata.revision =
1802 ATA_GETREV(dev, ccb->ccb_h.target_id);
1803 if (cts->xport_specific.sata.revision != 0xff) {
1804 cts->xport_specific.sata.valid |=
1805 CTS_SATA_VALID_REVISION;
1807 cts->xport_specific.sata.caps =
1808 d->caps & CTS_SATA_CAPS_D;
1810 cts->xport_specific.sata.caps |=
1811 CTS_SATA_CAPS_H_PMREQ;
1813 cts->xport_specific.sata.caps &=
1814 ch->user[ccb->ccb_h.target_id].caps;
1815 cts->xport_specific.sata.valid |=
1816 CTS_SATA_VALID_CAPS;
1818 cts->xport_specific.sata.revision = d->revision;
1819 cts->xport_specific.sata.valid |= CTS_SATA_VALID_REVISION;
1820 cts->xport_specific.sata.caps = d->caps;
1821 cts->xport_specific.sata.valid |= CTS_SATA_VALID_CAPS;
1823 cts->xport_specific.sata.atapi = d->atapi;
1824 cts->xport_specific.sata.valid |= CTS_SATA_VALID_ATAPI;
1826 cts->transport = XPORT_ATA;
1827 cts->transport_version = XPORT_VERSION_UNSPECIFIED;
1828 cts->xport_specific.ata.valid = 0;
1829 cts->xport_specific.ata.mode = d->mode;
1830 cts->xport_specific.ata.valid |= CTS_ATA_VALID_MODE;
1831 cts->xport_specific.ata.bytecount = d->bytecount;
1832 cts->xport_specific.ata.valid |= CTS_ATA_VALID_BYTECOUNT;
1833 cts->xport_specific.ata.atapi = d->atapi;
1834 cts->xport_specific.ata.valid |= CTS_ATA_VALID_ATAPI;
1836 ccb->ccb_h.status = CAM_REQ_CMP;
1839 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
1840 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
1842 ccb->ccb_h.status = CAM_REQ_CMP;
1844 case XPT_TERM_IO: /* Terminate the I/O process */
1846 ccb->ccb_h.status = CAM_REQ_INVALID;
1848 case XPT_PATH_INQ: /* Path routing inquiry */
1850 struct ccb_pathinq *cpi = &ccb->cpi;
1852 parent = device_get_parent(dev);
1853 cpi->version_num = 1; /* XXX??? */
1854 cpi->hba_inquiry = PI_SDTR_ABLE;
1855 cpi->target_sprt = 0;
1856 cpi->hba_misc = PIM_SEQSCAN;
1857 cpi->hba_eng_cnt = 0;
1858 if (ch->flags & ATA_NO_SLAVE)
1859 cpi->max_target = 0;
1861 cpi->max_target = 1;
1863 cpi->initiator_id = 0;
1864 cpi->bus_id = cam_sim_bus(sim);
1865 if (ch->flags & ATA_SATA)
1866 cpi->base_transfer_speed = 150000;
1868 cpi->base_transfer_speed = 3300;
1869 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1870 strncpy(cpi->hba_vid, "ATA", HBA_IDLEN);
1871 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1872 cpi->unit_number = cam_sim_unit(sim);
1873 if (ch->flags & ATA_SATA)
1874 cpi->transport = XPORT_SATA;
1876 cpi->transport = XPORT_ATA;
1877 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
1878 cpi->protocol = PROTO_ATA;
1879 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
1880 cpi->maxio = ch->dma.max_iosize ? ch->dma.max_iosize : DFLTPHYS;
1881 if (device_get_devclass(device_get_parent(parent)) ==
1882 devclass_find("pci")) {
1883 cpi->hba_vendor = pci_get_vendor(parent);
1884 cpi->hba_device = pci_get_device(parent);
1885 cpi->hba_subvendor = pci_get_subvendor(parent);
1886 cpi->hba_subdevice = pci_get_subdevice(parent);
1888 cpi->ccb_h.status = CAM_REQ_CMP;
1892 ccb->ccb_h.status = CAM_REQ_INVALID;
1899 atapoll(struct cam_sim *sim)
1901 struct ata_channel *ch = (struct ata_channel *)cam_sim_softc(sim);
1903 ata_interrupt_locked(ch);
1911 ata_module_event_handler(module_t mod, int what, void *arg)
1914 static struct cdev *atacdev;
1920 /* register controlling device */
1921 atacdev = make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "ata");
1924 /* register boot attach to be run when interrupts are enabled */
1925 if (!(ata_delayed_attach = (struct intr_config_hook *)
1926 malloc(sizeof(struct intr_config_hook),
1927 M_TEMP, M_NOWAIT | M_ZERO))) {
1928 printf("ata: malloc of delayed attach hook failed\n");
1931 ata_delayed_attach->ich_func = (void*)ata_boot_attach;
1932 if (config_intrhook_establish(ata_delayed_attach) != 0) {
1933 printf("ata: config_intrhook_establish failed\n");
1934 free(ata_delayed_attach, M_TEMP);
1942 /* deregister controlling device */
1943 destroy_dev(atacdev);
1952 static moduledata_t ata_moduledata = { "ata", ata_module_event_handler, NULL };
1953 DECLARE_MODULE(ata, ata_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
1954 MODULE_VERSION(ata, 1);
1956 MODULE_DEPEND(ata, cam, 1, 1, 1);
1962 ata_request_zone = uma_zcreate("ata_request", sizeof(struct ata_request),
1963 NULL, NULL, NULL, NULL, 0, 0);
1964 ata_composite_zone = uma_zcreate("ata_composite",
1965 sizeof(struct ata_composite),
1966 NULL, NULL, NULL, NULL, 0, 0);
1968 SYSINIT(ata_register, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL);
1973 uma_zdestroy(ata_composite_zone);
1974 uma_zdestroy(ata_request_zone);
1976 SYSUNINIT(ata_unregister, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_uninit, NULL);