2 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright (c) 2007 David Gwynne <dlg@openbsd.org>
37 * Permission to use, copy, modify, and distribute this software for any
38 * purpose with or without fee is hereby granted, provided that the above
39 * copyright notice and this permission notice appear in all copies.
41 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
42 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
43 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
44 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
45 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
46 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
47 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
49 * $OpenBSD: atascsi.c,v 1.64 2009/02/16 21:19:06 miod Exp $
53 * Implement each SATA port as its own SCSI bus on CAM. This way we can
54 * implement future port multiplier features as individual devices on the
57 * Much of the cdb<->xa conversion code was taken from OpenBSD, the rest
58 * was written natively for DragonFly.
63 const char *ScsiTypeArray[32] = {
98 static void ahci_xpt_action(struct cam_sim *sim, union ccb *ccb);
99 static void ahci_xpt_poll(struct cam_sim *sim);
100 static void ahci_xpt_scsi_disk_io(struct ahci_port *ap,
101 struct ata_port *at, union ccb *ccb);
102 static void ahci_xpt_scsi_atapi_io(struct ahci_port *ap,
103 struct ata_port *at, union ccb *ccb);
105 static void ahci_ata_complete_disk_rw(struct ata_xfer *xa);
106 static void ahci_ata_complete_disk_synchronize_cache(struct ata_xfer *xa);
107 static void ahci_atapi_complete_cmd(struct ata_xfer *xa);
108 static void ahci_ata_dummy_sense(struct scsi_sense_data *sense_data);
109 static void ahci_ata_atapi_sense(struct ata_fis_d2h *rfis,
110 struct scsi_sense_data *sense_data);
112 static int ahci_cam_probe_disk(struct ahci_port *ap, struct ata_port *at);
113 static int ahci_cam_probe_atapi(struct ahci_port *ap, struct ata_port *at);
114 static void ahci_ata_dummy_done(struct ata_xfer *xa);
115 static void ata_fix_identify(struct ata_identify *id);
116 static void ahci_cam_rescan(struct ahci_port *ap);
119 ahci_cam_attach(struct ahci_port *ap)
121 struct cam_devq *devq;
127 * We want at least one ccb to be available for error processing
128 * so don't let CAM use more then ncmds - 1.
130 unit = device_get_unit(ap->ap_sc->sc_dev);
131 if (ap->ap_sc->sc_ncmds > 1)
132 devq = cam_simq_alloc(ap->ap_sc->sc_ncmds - 1);
134 devq = cam_simq_alloc(ap->ap_sc->sc_ncmds);
138 sim = cam_sim_alloc(ahci_xpt_action, ahci_xpt_poll, "ahci",
139 (void *)ap, unit, &sim_mplock, 1, 1, devq);
140 cam_simq_release(devq);
145 ahci_os_unlock_port(ap);
146 error = xpt_bus_register(ap->ap_sim, ap->ap_num);
147 ahci_os_lock_port(ap);
148 if (error != CAM_SUCCESS) {
152 ap->ap_flags |= AP_F_BUS_REGISTERED;
154 if (ap->ap_probe == ATA_PROBE_NEED_IDENT)
155 error = ahci_cam_probe(ap, NULL);
162 ap->ap_flags |= AP_F_CAM_ATTACHED;
168 * The state of the port has changed.
170 * If at is NULL the physical port has changed state.
171 * If at is non-NULL a particular target behind a PM has changed state.
173 * If found is -1 the target state must be queued to a non-interrupt context.
174 * (only works with at == NULL).
176 * If found is 0 the target was removed.
177 * If found is 1 the target was inserted.
180 ahci_cam_changed(struct ahci_port *ap, struct ata_port *atx, int found)
182 struct cam_path *tmppath;
186 target = atx ? atx->at_target : CAM_TARGET_WILDCARD;
188 if (ap->ap_sim == NULL)
190 if (found == CAM_TARGET_WILDCARD) {
191 status = xpt_create_path(&tmppath, NULL,
192 cam_sim_path(ap->ap_sim),
193 target, CAM_LUN_WILDCARD);
194 if (status != CAM_REQ_CMP)
198 status = xpt_create_path(&tmppath, NULL,
199 cam_sim_path(ap->ap_sim),
202 if (status != CAM_REQ_CMP)
209 xpt_async(AC_FOUND_DEVICE, tmppath, NULL);
211 xpt_async(AC_LOST_DEVICE, tmppath, NULL);
214 xpt_free_path(tmppath);
218 ahci_cam_detach(struct ahci_port *ap)
222 if ((ap->ap_flags & AP_F_CAM_ATTACHED) == 0)
226 xpt_freeze_simq(ap->ap_sim, 1);
228 if (ap->ap_flags & AP_F_BUS_REGISTERED) {
229 error = xpt_bus_deregister(cam_sim_path(ap->ap_sim));
230 KKASSERT(error == CAM_REQ_CMP);
231 ap->ap_flags &= ~AP_F_BUS_REGISTERED;
234 cam_sim_free(ap->ap_sim);
238 ap->ap_flags &= ~AP_F_CAM_ATTACHED;
242 * Once the AHCI port has been attached we need to probe for a device or
243 * devices on the port and setup various options.
245 * If at is NULL we are probing the direct-attached device on the port,
246 * which may or may not be a port multiplier.
249 ahci_cam_probe(struct ahci_port *ap, struct ata_port *atx)
254 u_int64_t capacity_bytes;
267 * Delayed CAM attachment for initial probe, sim may be NULL
269 if (ap->ap_sim == NULL)
273 * A NULL atx indicates a probe of the directly connected device.
274 * A non-NULL atx indicates a device connected via a port multiplier.
275 * We need to preserve atx for calls to ahci_ata_get_xfer().
277 * at is always non-NULL. For directly connected devices we supply
278 * an (at) pointing to target 0.
281 at = ap->ap_ata; /* direct attached - device 0 */
282 if (ap->ap_type == ATA_PORT_T_PM) {
283 kprintf("%s: Found Port Multiplier\n",
287 at->at_type = ap->ap_type;
290 if (atx->at_type == ATA_PORT_T_PM) {
291 kprintf("%s: Bogus device, reducing port count to %d\n",
292 ATANAME(ap, atx), atx->at_target);
293 if (ap->ap_pmcount > atx->at_target)
294 ap->ap_pmcount = atx->at_target;
298 if (ap->ap_type == ATA_PORT_T_NONE)
300 if (at->at_type == ATA_PORT_T_NONE)
304 * Issue identify, saving the result
306 xa = ahci_ata_get_xfer(ap, atx);
307 xa->complete = ahci_ata_dummy_done;
308 xa->data = &at->at_identify;
309 xa->datalen = sizeof(at->at_identify);
310 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
312 switch(at->at_type) {
313 case ATA_PORT_T_DISK:
314 xa->fis->command = ATA_C_IDENTIFY;
317 case ATA_PORT_T_ATAPI:
318 xa->fis->command = ATA_C_ATAPI_IDENTIFY;
322 xa->fis->command = ATA_C_ATAPI_IDENTIFY;
323 type = "UNKNOWN(ATAPI?)";
326 xa->fis->features = 0;
328 xa->flags = ATA_F_READ | ATA_F_PIO | ATA_F_POLL;
331 if (ahci_ata_cmd(xa) != ATA_S_COMPLETE) {
332 kprintf("%s: Detected %s device but unable to IDENTIFY\n",
333 ATANAME(ap, atx), type);
334 ahci_ata_put_xfer(xa);
337 ahci_ata_put_xfer(xa);
339 ata_fix_identify(&at->at_identify);
342 * Read capacity using SATA probe info.
344 if (le16toh(at->at_identify.cmdset83) & 0x0400) {
345 /* LBA48 feature set supported */
347 for (i = 3; i >= 0; --i) {
350 le16toh(at->at_identify.addrsecxt[i]);
353 capacity = le16toh(at->at_identify.addrsec[1]);
355 capacity += le16toh(at->at_identify.addrsec[0]);
357 at->at_capacity = capacity;
359 ap->ap_probe = ATA_PROBE_GOOD;
361 capacity_bytes = capacity * 512;
364 * Negotiate NCQ, throw away any ata_xfer's beyond the negotiated
365 * number of slots and limit the number of CAM ccb's to one less
366 * so we always have a slot available for recovery.
368 * NCQ is not used if ap_ncqdepth is 1 or the host controller does
369 * not support it, and in that case the driver can handle extra
372 * NCQ is currently used only with direct-attached disks. It is
373 * not used with port multipliers or direct-attached ATAPI devices.
375 * Remember at least one extra CCB needs to be reserved for the
378 if ((ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) &&
379 ap->ap_type == ATA_PORT_T_DISK &&
380 (le16toh(at->at_identify.satacap) & (1 << 8))) {
381 at->at_ncqdepth = (le16toh(at->at_identify.qdepth) & 0x1F) + 1;
382 devncqdepth = at->at_ncqdepth;
383 if (at->at_ncqdepth > ap->ap_sc->sc_ncmds)
384 at->at_ncqdepth = ap->ap_sc->sc_ncmds;
385 if (at->at_ncqdepth > 1) {
386 for (i = 0; i < ap->ap_sc->sc_ncmds; ++i) {
387 xa = ahci_ata_get_xfer(ap, atx);
388 if (xa->tag < at->at_ncqdepth) {
389 xa->state = ATA_S_COMPLETE;
390 ahci_ata_put_xfer(xa);
393 if (at->at_ncqdepth >= ap->ap_sc->sc_ncmds) {
394 cam_devq_resize(ap->ap_sim->devq,
395 at->at_ncqdepth - 1);
403 * Make the model string a bit more presentable
405 for (model_len = 40; model_len; --model_len) {
406 if (at->at_identify.model[model_len-1] == ' ')
408 if (at->at_identify.model[model_len-1] == 0)
414 * Generate informatiive strings.
416 * NOTE: We do not automatically set write caching, lookahead,
417 * or the security state for ATAPI devices.
419 if (at->at_identify.cmdset82 & ATA_IDENTIFY_WRITECACHE) {
420 if (at->at_identify.features85 & ATA_IDENTIFY_WRITECACHE)
422 else if (at->at_type == ATA_PORT_T_ATAPI)
430 if (at->at_identify.cmdset82 & ATA_IDENTIFY_LOOKAHEAD) {
431 if (at->at_identify.features85 & ATA_IDENTIFY_LOOKAHEAD)
433 else if (at->at_type == ATA_PORT_T_ATAPI)
441 if (at->at_identify.cmdset82 & ATA_IDENTIFY_SECURITY) {
442 if (at->at_identify.securestatus & ATA_SECURE_FROZEN)
444 else if (at->at_type == ATA_PORT_T_ATAPI)
446 else if (AhciNoFeatures & (1 << ap->ap_num))
447 scstr = "<disabled>";
454 kprintf("%s: Found %s \"%*.*s %8.8s\" serial=\"%20.20s\"\n"
455 "%s: tags=%d/%d satacap=%04x satafea=%04x NCQ=%s "
456 "capacity=%lld.%02dMB\n",
460 model_len, model_len,
461 at->at_identify.model,
462 at->at_identify.firmware,
463 at->at_identify.serial,
466 devncqdepth, ap->ap_sc->sc_ncmds,
467 at->at_identify.satacap,
468 at->at_identify.satafsup,
469 (at->at_ncqdepth > 1 ? "YES" : "NO"),
470 (long long)capacity_bytes / (1024 * 1024),
471 (int)(capacity_bytes % (1024 * 1024)) * 100 / (1024 * 1024)
473 kprintf("%s: f85=%04x f86=%04x f87=%04x WC=%s RA=%s SEC=%s\n",
475 at->at_identify.features85,
476 at->at_identify.features86,
477 at->at_identify.features87,
484 * Additional type-specific probing
486 switch(at->at_type) {
487 case ATA_PORT_T_DISK:
488 error = ahci_cam_probe_disk(ap, atx);
490 case ATA_PORT_T_ATAPI:
491 error = ahci_cam_probe_atapi(ap, atx);
499 at->at_probe = ATA_PROBE_FAILED;
501 ap->ap_probe = at->at_probe;
503 at->at_probe = ATA_PROBE_GOOD;
505 ap->ap_probe = at->at_probe;
511 * DISK-specific probe after initial ident
514 ahci_cam_probe_disk(struct ahci_port *ap, struct ata_port *atx)
519 at = atx ? atx : ap->ap_ata;
522 * Enable write cache if supported
524 * NOTE: "WD My Book" external disk devices have a very poor
525 * daughter board between the the ESATA and the HD. Sending
526 * any ATA_C_SET_FEATURES commands will break the hardware port
527 * with a fatal protocol error. However, this device also
528 * indicates that WRITECACHE is already on and READAHEAD is
529 * not supported so we avoid the issue.
531 if ((at->at_identify.cmdset82 & ATA_IDENTIFY_WRITECACHE) &&
532 (at->at_identify.features85 & ATA_IDENTIFY_WRITECACHE) == 0) {
533 xa = ahci_ata_get_xfer(ap, atx);
534 xa->complete = ahci_ata_dummy_done;
535 xa->fis->command = ATA_C_SET_FEATURES;
536 /*xa->fis->features = ATA_SF_WRITECACHE_EN;*/
537 xa->fis->features = ATA_SF_LOOKAHEAD_EN;
538 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
540 xa->flags = ATA_F_READ | ATA_F_PIO | ATA_F_POLL;
543 if (ahci_ata_cmd(xa) == ATA_S_COMPLETE)
544 at->at_features |= ATA_PORT_F_WCACHE;
546 kprintf("%s: Unable to enable write-caching\n",
548 ahci_ata_put_xfer(xa);
552 * Enable readahead if supported
554 if ((at->at_identify.cmdset82 & ATA_IDENTIFY_LOOKAHEAD) &&
555 (at->at_identify.features85 & ATA_IDENTIFY_LOOKAHEAD) == 0) {
556 xa = ahci_ata_get_xfer(ap, atx);
557 xa->complete = ahci_ata_dummy_done;
558 xa->fis->command = ATA_C_SET_FEATURES;
559 xa->fis->features = ATA_SF_LOOKAHEAD_EN;
560 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
562 xa->flags = ATA_F_READ | ATA_F_PIO | ATA_F_POLL;
565 if (ahci_ata_cmd(xa) == ATA_S_COMPLETE)
566 at->at_features |= ATA_PORT_F_RAHEAD;
568 kprintf("%s: Unable to enable read-ahead\n",
570 ahci_ata_put_xfer(xa);
574 * FREEZE LOCK the device so malicious users can't lock it on us.
575 * As there is no harm in issuing this to devices that don't
576 * support the security feature set we just send it, and don't bother
577 * checking if the device sends a command abort to tell us it doesn't
580 if ((at->at_identify.cmdset82 & ATA_IDENTIFY_SECURITY) &&
581 (at->at_identify.securestatus & ATA_SECURE_FROZEN) == 0 &&
582 (AhciNoFeatures & (1 << ap->ap_num)) == 0) {
583 xa = ahci_ata_get_xfer(ap, atx);
584 xa->complete = ahci_ata_dummy_done;
585 xa->fis->command = ATA_C_SEC_FREEZE_LOCK;
586 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
587 xa->flags = ATA_F_READ | ATA_F_PIO | ATA_F_POLL;
590 if (ahci_ata_cmd(xa) == ATA_S_COMPLETE)
591 at->at_features |= ATA_PORT_F_FRZLCK;
593 kprintf("%s: Unable to set security freeze\n",
595 ahci_ata_put_xfer(xa);
602 * ATAPI-specific probe after initial ident
605 ahci_cam_probe_atapi(struct ahci_port *ap, struct ata_port *atx)
611 * Fix byte ordering so buffers can be accessed as
615 ata_fix_identify(struct ata_identify *id)
620 swap = (u_int16_t *)id->serial;
621 for (i = 0; i < sizeof(id->serial) / sizeof(u_int16_t); i++)
622 swap[i] = bswap16(swap[i]);
624 swap = (u_int16_t *)id->firmware;
625 for (i = 0; i < sizeof(id->firmware) / sizeof(u_int16_t); i++)
626 swap[i] = bswap16(swap[i]);
628 swap = (u_int16_t *)id->model;
629 for (i = 0; i < sizeof(id->model) / sizeof(u_int16_t); i++)
630 swap[i] = bswap16(swap[i]);
634 * Dummy done callback for xa.
637 ahci_ata_dummy_done(struct ata_xfer *xa)
642 * Use an engineering request to initiate a target scan for devices
643 * behind a port multiplier.
645 * An asynchronous bus scan is used to avoid reentrancy issues.
648 ahci_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
650 struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
652 if (ccb->ccb_h.func_code == XPT_SCAN_BUS) {
653 ap->ap_flags &= ~AP_F_SCAN_RUNNING;
654 if (ap->ap_flags & AP_F_SCAN_REQUESTED) {
655 ap->ap_flags &= ~AP_F_SCAN_REQUESTED;
658 ap->ap_flags |= AP_F_SCAN_COMPLETED;
659 wakeup(&ap->ap_flags);
665 ahci_cam_rescan(struct ahci_port *ap)
667 struct cam_path *path;
672 if (ap->ap_flags & AP_F_SCAN_RUNNING) {
673 ap->ap_flags |= AP_F_SCAN_REQUESTED;
676 ap->ap_flags |= AP_F_SCAN_RUNNING;
677 for (i = 0; i < AHCI_MAX_PMPORTS; ++i) {
678 ap->ap_ata[i].at_features |= ATA_PORT_F_RESCAN;
681 status = xpt_create_path(&path, xpt_periph, cam_sim_path(ap->ap_sim),
682 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
683 if (status != CAM_REQ_CMP)
686 ccb = xpt_alloc_ccb();
687 xpt_setup_ccb(&ccb->ccb_h, path, 5); /* 5 = low priority */
688 ccb->ccb_h.func_code = XPT_ENG_EXEC;
689 ccb->ccb_h.cbfcnp = ahci_cam_rescan_callback;
690 ccb->ccb_h.sim_priv.entries[0].ptr = ap;
691 ccb->crcn.flags = CAM_FLAG_NONE;
692 xpt_action_async(ccb);
696 ahci_xpt_rescan(struct ahci_port *ap)
698 struct cam_path *path;
702 status = xpt_create_path(&path, xpt_periph, cam_sim_path(ap->ap_sim),
703 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
704 if (status != CAM_REQ_CMP)
707 ccb = xpt_alloc_ccb();
708 xpt_setup_ccb(&ccb->ccb_h, path, 5); /* 5 = low priority */
709 ccb->ccb_h.func_code = XPT_SCAN_BUS;
710 ccb->ccb_h.cbfcnp = ahci_cam_rescan_callback;
711 ccb->ccb_h.sim_priv.entries[0].ptr = ap;
712 ccb->crcn.flags = CAM_FLAG_NONE;
713 xpt_action_async(ccb);
717 * Action function - dispatch command
721 ahci_xpt_action(struct cam_sim *sim, union ccb *ccb)
723 struct ahci_port *ap;
724 struct ata_port *at, *atx;
725 struct ccb_hdr *ccbh;
729 ap = cam_sim_softc(sim);
732 KKASSERT(ap != NULL);
734 unit = cam_sim_unit(sim);
737 * Early failure checks. These checks do not apply to XPT_PATH_INQ,
738 * otherwise the bus rescan will not remove the dead devices when
741 * For non-wildcards we have one target (0) and one lun (0),
742 * unless we have a port multiplier.
744 * A wildcard target indicates only the general bus is being
747 * Calculate at and atx. at is always non-NULL. atx is only
748 * non-NULL for direct-attached devices. It will be NULL for
749 * devices behind a port multiplier.
751 * XXX What do we do with a LUN wildcard?
753 if (ccbh->target_id != CAM_TARGET_WILDCARD &&
754 ccbh->func_code != XPT_PATH_INQ) {
755 if (ap->ap_type == ATA_PORT_T_NONE) {
756 ccbh->status = CAM_DEV_NOT_THERE;
760 if (ccbh->target_id < 0 || ccbh->target_id >= ap->ap_pmcount) {
761 ccbh->status = CAM_DEV_NOT_THERE;
765 at += ccbh->target_id;
766 if (ap->ap_type == ATA_PORT_T_PM)
769 if (ccbh->target_lun != CAM_LUN_WILDCARD && ccbh->target_lun) {
770 ccbh->status = CAM_DEV_NOT_THERE;
777 * Switch on the meta XPT command
779 switch(ccbh->func_code) {
782 * This routine is called after a port multiplier has been
785 ccbh->status = CAM_REQ_CMP;
786 ahci_os_lock_port(ap);
787 ahci_port_state_machine(ap, 0);
788 ahci_os_unlock_port(ap);
794 * This command always succeeds, otherwise the bus scan
795 * will not detach dead devices.
797 ccb->cpi.version_num = 1;
798 ccb->cpi.hba_inquiry = 0;
799 ccb->cpi.target_sprt = 0;
800 ccb->cpi.hba_misc = PIM_SEQSCAN;
801 ccb->cpi.hba_eng_cnt = 0;
802 bzero(ccb->cpi.vuhba_flags, sizeof(ccb->cpi.vuhba_flags));
803 ccb->cpi.max_target = AHCI_MAX_PMPORTS - 1;
804 ccb->cpi.max_lun = 0;
805 ccb->cpi.async_flags = 0;
806 ccb->cpi.hpath_id = 0;
807 ccb->cpi.initiator_id = AHCI_MAX_PMPORTS - 1;
808 ccb->cpi.unit_number = cam_sim_unit(sim);
809 ccb->cpi.bus_id = cam_sim_bus(sim);
810 ccb->cpi.base_transfer_speed = 150000;
811 ccb->cpi.transport = XPORT_SATA;
812 ccb->cpi.transport_version = 1;
813 ccb->cpi.protocol = PROTO_SCSI;
814 ccb->cpi.protocol_version = SCSI_REV_2;
816 ccbh->status = CAM_REQ_CMP;
817 if (ccbh->target_id == CAM_TARGET_WILDCARD) {
818 ahci_os_lock_port(ap);
819 ahci_port_state_machine(ap, 0);
820 ahci_os_unlock_port(ap);
822 switch(ahci_pread(ap, AHCI_PREG_SSTS) &
823 AHCI_PREG_SSTS_SPD) {
824 case AHCI_PREG_SSTS_SPD_GEN1:
825 ccb->cpi.base_transfer_speed = 150000;
827 case AHCI_PREG_SSTS_SPD_GEN2:
828 ccb->cpi.base_transfer_speed = 300000;
832 ccb->cpi.base_transfer_speed = 1000;
836 if (ap->ap_type == ATA_PORT_T_NONE)
837 ccbh->status = CAM_DEV_NOT_THERE;
843 ahci_os_lock_port(ap);
844 if (ap->ap_type == ATA_PORT_T_NONE) {
845 ccbh->status = CAM_DEV_NOT_THERE;
847 ahci_port_reset(ap, atx, 0);
848 ccbh->status = CAM_REQ_CMP;
850 ahci_os_unlock_port(ap);
854 ahci_os_lock_port(ap);
855 ahci_port_reset(ap, NULL, 1);
856 ahci_os_unlock_port(ap);
857 ccbh->status = CAM_REQ_CMP;
860 case XPT_SET_TRAN_SETTINGS:
861 ccbh->status = CAM_FUNC_NOTAVAIL;
864 case XPT_GET_TRAN_SETTINGS:
865 ccb->cts.protocol = PROTO_SCSI;
866 ccb->cts.protocol_version = SCSI_REV_2;
867 ccb->cts.transport = XPORT_SATA;
868 ccb->cts.transport_version = XPORT_VERSION_UNSPECIFIED;
869 ccb->cts.proto_specific.valid = 0;
870 ccb->cts.xport_specific.valid = 0;
871 ccbh->status = CAM_REQ_CMP;
874 case XPT_CALC_GEOMETRY:
875 cam_calc_geometry(&ccb->ccg, 1);
880 * Our parallel startup code might have only probed through
881 * to the IDENT, so do the last step if necessary.
883 if (at->at_probe == ATA_PROBE_NEED_IDENT)
884 ahci_cam_probe(ap, atx);
885 if (at->at_probe != ATA_PROBE_GOOD) {
886 ccbh->status = CAM_DEV_NOT_THERE;
890 switch(at->at_type) {
891 case ATA_PORT_T_DISK:
892 ahci_xpt_scsi_disk_io(ap, atx, ccb);
894 case ATA_PORT_T_ATAPI:
895 ahci_xpt_scsi_atapi_io(ap, atx, ccb);
898 ccbh->status = CAM_REQ_INVALID;
904 ccbh->status = CAM_REQ_INVALID;
913 * Generally this function gets called heavily when interrupts might be
914 * non-operational, during a halt/reboot or panic.
918 ahci_xpt_poll(struct cam_sim *sim)
920 struct ahci_port *ap;
922 ap = cam_sim_softc(sim);
924 ahci_os_lock_port(ap);
925 ahci_port_intr(ap, 1);
926 ahci_os_unlock_port(ap);
931 * Convert the SCSI command in ccb to an ata_xfer command in xa
932 * for ATA_PORT_T_DISK operations. Set the completion function
933 * to convert the response back, then dispatch to the OpenBSD AHCI
936 * AHCI DISK commands only support a limited command set, and we
937 * fake additional commands to make it play nice with the CAM subsystem.
941 ahci_xpt_scsi_disk_io(struct ahci_port *ap, struct ata_port *atx,
944 struct ccb_hdr *ccbh;
945 struct ccb_scsiio *csio;
948 struct ata_fis_h2d *fis;
950 union scsi_data *rdata;
956 ccbh = &ccb->csio.ccb_h;
958 at = atx ? atx : &ap->ap_ata[0];
961 * XXX not passing NULL at for direct attach!
963 xa = ahci_ata_get_xfer(ap, atx);
964 rdata = (void *)csio->data_ptr;
965 rdata_len = csio->dxfer_len;
968 * Build the FIS or process the csio to completion.
970 cdb = (void *)((ccbh->flags & CAM_CDB_POINTER) ?
971 csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes);
973 switch(cdb->generic.opcode) {
976 * Auto-sense everything, so explicit sense requests
979 ccbh->status = CAM_SCSI_STATUS_ERROR;
983 * Inquiry supported features
985 * [opcode, byte2, page_code, length, control]
987 if (cdb->inquiry.byte2 & SI_EVPD) {
988 switch(cdb->inquiry.page_code) {
989 case SVPD_SUPPORTED_PAGE_LIST:
990 /* XXX atascsi_disk_vpd_supported */
991 case SVPD_UNIT_SERIAL_NUMBER:
992 /* XXX atascsi_disk_vpd_serial */
993 case SVPD_UNIT_DEVID:
994 /* XXX atascsi_disk_vpd_ident */
996 ccbh->status = CAM_FUNC_NOTAVAIL;
1000 bzero(rdata, rdata_len);
1001 if (rdata_len < SHORT_INQUIRY_LENGTH) {
1002 ccbh->status = CAM_CCB_LEN_ERR;
1005 if (rdata_len > sizeof(rdata->inquiry_data))
1006 rdata_len = sizeof(rdata->inquiry_data);
1007 rdata->inquiry_data.device = T_DIRECT;
1008 rdata->inquiry_data.version = SCSI_REV_SPC2;
1009 rdata->inquiry_data.response_format = 2;
1010 rdata->inquiry_data.additional_length = 32;
1011 bcopy("SATA ", rdata->inquiry_data.vendor, 8);
1012 bcopy(at->at_identify.model,
1013 rdata->inquiry_data.product,
1014 sizeof(rdata->inquiry_data.product));
1015 bcopy(at->at_identify.firmware,
1016 rdata->inquiry_data.revision,
1017 sizeof(rdata->inquiry_data.revision));
1018 ccbh->status = CAM_REQ_CMP;
1021 case READ_CAPACITY_16:
1022 if (cdb->read_capacity_16.service_action != SRC16_SERVICE_ACTION) {
1023 ccbh->status = CAM_REQ_INVALID;
1026 if (rdata_len < sizeof(rdata->read_capacity_data_16)) {
1027 ccbh->status = CAM_CCB_LEN_ERR;
1032 if (rdata_len < sizeof(rdata->read_capacity_data)) {
1033 ccbh->status = CAM_CCB_LEN_ERR;
1037 capacity = at->at_capacity;
1039 bzero(rdata, rdata_len);
1040 if (cdb->generic.opcode == READ_CAPACITY) {
1041 rdata_len = sizeof(rdata->read_capacity_data);
1042 if (capacity > 0xFFFFFFFFU)
1043 capacity = 0xFFFFFFFFU;
1044 bzero(&rdata->read_capacity_data, rdata_len);
1045 scsi_ulto4b((u_int32_t)capacity - 1,
1046 rdata->read_capacity_data.addr);
1047 scsi_ulto4b(512, rdata->read_capacity_data.length);
1049 rdata_len = sizeof(rdata->read_capacity_data_16);
1050 bzero(&rdata->read_capacity_data_16, rdata_len);
1051 scsi_u64to8b(capacity - 1,
1052 rdata->read_capacity_data_16.addr);
1053 scsi_ulto4b(512, rdata->read_capacity_data_16.length);
1055 ccbh->status = CAM_REQ_CMP;
1057 case SYNCHRONIZE_CACHE:
1059 * Synchronize cache. Specification says this can take
1060 * greater then 30 seconds so give it at least 45.
1063 fis->flags = ATA_H2D_FLAGS_CMD;
1064 fis->command = ATA_C_FLUSH_CACHE;
1066 if (xa->timeout < 45000)
1067 xa->timeout = 45000;
1069 xa->flags = ATA_F_READ;
1070 xa->complete = ahci_ata_complete_disk_synchronize_cache;
1072 case TEST_UNIT_READY:
1073 case START_STOP_UNIT:
1076 * Just silently return success
1078 ccbh->status = CAM_REQ_CMP;
1084 * XXX implement pass-through
1086 ccbh->status = CAM_FUNC_NOTAVAIL;
1089 switch(cdb->generic.opcode) {
1091 lba = scsi_3btoul(cdb->rw_6.addr) & 0x1FFFFF;
1092 count = cdb->rw_6.length ? cdb->rw_6.length : 0x100;
1093 xa->flags = ATA_F_READ;
1096 lba = scsi_4btoul(cdb->rw_10.addr);
1097 count = scsi_2btoul(cdb->rw_10.length);
1098 xa->flags = ATA_F_READ;
1101 lba = scsi_4btoul(cdb->rw_12.addr);
1102 count = scsi_4btoul(cdb->rw_12.length);
1103 xa->flags = ATA_F_READ;
1106 lba = scsi_8btou64(cdb->rw_16.addr);
1107 count = scsi_4btoul(cdb->rw_16.length);
1108 xa->flags = ATA_F_READ;
1111 lba = scsi_3btoul(cdb->rw_6.addr) & 0x1FFFFF;
1112 count = cdb->rw_6.length ? cdb->rw_6.length : 0x100;
1113 xa->flags = ATA_F_WRITE;
1116 lba = scsi_4btoul(cdb->rw_10.addr);
1117 count = scsi_2btoul(cdb->rw_10.length);
1118 xa->flags = ATA_F_WRITE;
1121 lba = scsi_4btoul(cdb->rw_12.addr);
1122 count = scsi_4btoul(cdb->rw_12.length);
1123 xa->flags = ATA_F_WRITE;
1126 lba = scsi_8btou64(cdb->rw_16.addr);
1127 count = scsi_4btoul(cdb->rw_16.length);
1128 xa->flags = ATA_F_WRITE;
1131 ccbh->status = CAM_REQ_INVALID;
1134 if (ccbh->status != CAM_REQ_INPROG)
1138 fis->flags = ATA_H2D_FLAGS_CMD;
1139 fis->lba_low = (u_int8_t)lba;
1140 fis->lba_mid = (u_int8_t)(lba >> 8);
1141 fis->lba_high = (u_int8_t)(lba >> 16);
1142 fis->device = ATA_H2D_DEVICE_LBA;
1145 * NCQ only for direct-attached disks, do not currently
1146 * try to use NCQ with port multipliers.
1148 if (at->at_ncqdepth > 1 &&
1149 ap->ap_type == ATA_PORT_T_DISK &&
1150 (ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) &&
1151 (ccbh->flags & CAM_POLLED) == 0) {
1153 * Use NCQ - always uses 48 bit addressing
1155 xa->flags |= ATA_F_NCQ;
1156 fis->command = (xa->flags & ATA_F_WRITE) ?
1157 ATA_C_WRITE_FPDMA : ATA_C_READ_FPDMA;
1158 fis->lba_low_exp = (u_int8_t)(lba >> 24);
1159 fis->lba_mid_exp = (u_int8_t)(lba >> 32);
1160 fis->lba_high_exp = (u_int8_t)(lba >> 40);
1161 fis->sector_count = xa->tag << 3;
1162 fis->features = (u_int8_t)count;
1163 fis->features_exp = (u_int8_t)(count >> 8);
1164 } else if (count > 0x100 || lba > 0xFFFFFFFFU) {
1168 fis->command = (xa->flags & ATA_F_WRITE) ?
1169 ATA_C_WRITEDMA_EXT : ATA_C_READDMA_EXT;
1170 fis->lba_low_exp = (u_int8_t)(lba >> 24);
1171 fis->lba_mid_exp = (u_int8_t)(lba >> 32);
1172 fis->lba_high_exp = (u_int8_t)(lba >> 40);
1173 fis->sector_count = (u_int8_t)count;
1174 fis->sector_count_exp = (u_int8_t)(count >> 8);
1179 * NOTE: 256 sectors is supported, stored as 0.
1181 fis->command = (xa->flags & ATA_F_WRITE) ?
1182 ATA_C_WRITEDMA : ATA_C_READDMA;
1183 fis->device |= (u_int8_t)(lba >> 24) & 0x0F;
1184 fis->sector_count = (u_int8_t)count;
1187 xa->data = csio->data_ptr;
1188 xa->datalen = csio->dxfer_len;
1189 xa->complete = ahci_ata_complete_disk_rw;
1190 xa->timeout = ccbh->timeout; /* milliseconds */
1191 if (ccbh->flags & CAM_POLLED)
1192 xa->flags |= ATA_F_POLL;
1197 * If the request is still in progress the xa and FIS have
1198 * been set up and must be dispatched. Otherwise the request
1201 if (ccbh->status == CAM_REQ_INPROG) {
1202 KKASSERT(xa->complete != NULL);
1203 xa->atascsi_private = ccb;
1204 ccb->ccb_h.sim_priv.entries[0].ptr = ap;
1205 ahci_os_lock_port(ap);
1206 fis->flags |= at->at_target;
1208 ahci_os_unlock_port(ap);
1210 ahci_ata_put_xfer(xa);
1216 * Convert the SCSI command in ccb to an ata_xfer command in xa
1217 * for ATA_PORT_T_ATAPI operations. Set the completion function
1218 * to convert the response back, then dispatch to the OpenBSD AHCI
1223 ahci_xpt_scsi_atapi_io(struct ahci_port *ap, struct ata_port *atx,
1226 struct ccb_hdr *ccbh;
1227 struct ccb_scsiio *csio;
1228 struct ata_xfer *xa;
1229 struct ata_fis_h2d *fis;
1233 struct ata_port *at;
1235 ccbh = &ccb->csio.ccb_h;
1237 at = atx ? atx : &ap->ap_ata[0];
1239 switch (ccbh->flags & CAM_DIR_MASK) {
1241 flags = ATA_F_PACKET | ATA_F_READ;
1244 flags = ATA_F_PACKET | ATA_F_WRITE;
1247 flags = ATA_F_PACKET;
1250 ccbh->status = CAM_REQ_INVALID;
1257 * The command has to fit in the packet command buffer.
1259 if (csio->cdb_len < 6 || csio->cdb_len > 16) {
1260 ccbh->status = CAM_CCB_LEN_ERR;
1266 * Initialize the XA and FIS.
1268 * XXX not passing NULL at for direct attach!
1270 xa = ahci_ata_get_xfer(ap, atx);
1273 fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
1274 fis->command = ATA_C_PACKET;
1276 fis->sector_count = xa->tag << 3;
1277 fis->features = ATA_H2D_FEATURES_DMA |
1278 ((flags & ATA_F_WRITE) ?
1279 ATA_H2D_FEATURES_DIR_WRITE : ATA_H2D_FEATURES_DIR_READ);
1280 fis->lba_mid = 0x00;
1281 fis->lba_high = 0x20;
1284 xa->data = csio->data_ptr;
1285 xa->datalen = csio->dxfer_len;
1286 xa->timeout = ccbh->timeout; /* milliseconds */
1288 if (ccbh->flags & CAM_POLLED)
1289 xa->flags |= ATA_F_POLL;
1292 * Copy the cdb to the packetcmd buffer in the FIS using a
1293 * convenient pointer in the xa.
1295 cdbs = (void *)((ccbh->flags & CAM_CDB_POINTER) ?
1296 csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes);
1297 bcopy(cdbs, xa->packetcmd, csio->cdb_len);
1300 kprintf("opcode %d cdb_len %d dxfer_len %d\n",
1301 cdbs->generic.opcode,
1302 csio->cdb_len, csio->dxfer_len);
1306 * Some ATAPI commands do not actually follow the SCSI standard.
1308 cdbd = (void *)xa->packetcmd;
1310 switch(cdbd->generic.opcode) {
1313 * Some ATAPI devices can't handle SI_EVPD being set
1314 * for a basic inquiry (page_code == 0).
1316 * Some ATAPI devices can't handle long inquiry lengths,
1317 * don't ask me why. Truncate the inquiry length.
1319 if ((cdbd->inquiry.byte2 & SI_EVPD) &&
1320 cdbd->inquiry.page_code == 0) {
1321 cdbd->inquiry.byte2 &= ~SI_EVPD;
1323 if (cdbd->inquiry.page_code == 0 &&
1324 cdbd->inquiry.length > SHORT_INQUIRY_LENGTH) {
1325 cdbd->inquiry.length = SHORT_INQUIRY_LENGTH;
1331 * Convert *_6 to *_10 commands. Most ATAPI devices
1332 * cannot handle the SCSI READ_6 and WRITE_6 commands.
1334 cdbd->rw_10.opcode |= 0x20;
1335 cdbd->rw_10.byte2 = 0;
1336 cdbd->rw_10.addr[0] = cdbs->rw_6.addr[0] & 0x1F;
1337 cdbd->rw_10.addr[1] = cdbs->rw_6.addr[1];
1338 cdbd->rw_10.addr[2] = cdbs->rw_6.addr[2];
1339 cdbd->rw_10.addr[3] = 0;
1340 cdbd->rw_10.reserved = 0;
1341 cdbd->rw_10.length[0] = 0;
1342 cdbd->rw_10.length[1] = cdbs->rw_6.length;
1343 cdbd->rw_10.control = cdbs->rw_6.control;
1352 xa->complete = ahci_atapi_complete_cmd;
1353 xa->atascsi_private = ccb;
1354 ccb->ccb_h.sim_priv.entries[0].ptr = ap;
1355 ahci_os_lock_port(ap);
1357 ahci_os_unlock_port(ap);
1361 * Completion function for ATA_PORT_T_DISK cache synchronization.
1365 ahci_ata_complete_disk_synchronize_cache(struct ata_xfer *xa)
1367 union ccb *ccb = xa->atascsi_private;
1368 struct ccb_hdr *ccbh = &ccb->ccb_h;
1369 struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
1372 case ATA_S_COMPLETE:
1373 ccbh->status = CAM_REQ_CMP;
1374 ccb->csio.scsi_status = SCSI_STATUS_OK;
1377 kprintf("%s: synchronize_cache: error\n",
1378 ATANAME(ap, xa->at));
1379 ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
1380 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1381 ahci_ata_dummy_sense(&ccb->csio.sense_data);
1384 kprintf("%s: synchronize_cache: timeout\n",
1385 ATANAME(ap, xa->at));
1386 ccbh->status = CAM_CMD_TIMEOUT;
1389 kprintf("%s: synchronize_cache: unknown state %d\n",
1390 ATANAME(ap, xa->at), xa->state);
1391 ccbh->status = CAM_REQ_CMP_ERR;
1394 ahci_ata_put_xfer(xa);
1395 ahci_os_unlock_port(ap);
1397 ahci_os_lock_port(ap);
1401 * Completion function for ATA_PORT_T_DISK I/O
1405 ahci_ata_complete_disk_rw(struct ata_xfer *xa)
1407 union ccb *ccb = xa->atascsi_private;
1408 struct ccb_hdr *ccbh = &ccb->ccb_h;
1409 struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
1412 case ATA_S_COMPLETE:
1413 ccbh->status = CAM_REQ_CMP;
1414 ccb->csio.scsi_status = SCSI_STATUS_OK;
1417 kprintf("%s: disk_rw: error\n", ATANAME(ap, xa->at));
1418 ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
1419 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1420 ahci_ata_dummy_sense(&ccb->csio.sense_data);
1423 kprintf("%s: disk_rw: timeout\n", ATANAME(ap, xa->at));
1424 ccbh->status = CAM_CMD_TIMEOUT;
1425 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1426 ahci_ata_dummy_sense(&ccb->csio.sense_data);
1429 kprintf("%s: disk_rw: unknown state %d\n",
1430 ATANAME(ap, xa->at), xa->state);
1431 ccbh->status = CAM_REQ_CMP_ERR;
1434 ccb->csio.resid = xa->resid;
1435 ahci_ata_put_xfer(xa);
1436 ahci_os_unlock_port(ap);
1438 ahci_os_lock_port(ap);
1442 * Completion function for ATA_PORT_T_ATAPI I/O
1444 * Sense data is returned in the rfis.
1448 ahci_atapi_complete_cmd(struct ata_xfer *xa)
1450 union ccb *ccb = xa->atascsi_private;
1451 struct ccb_hdr *ccbh = &ccb->ccb_h;
1452 struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
1455 cdb = (void *)((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
1456 ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes);
1459 case ATA_S_COMPLETE:
1460 ccbh->status = CAM_REQ_CMP;
1461 ccb->csio.scsi_status = SCSI_STATUS_OK;
1464 ccbh->status = CAM_SCSI_STATUS_ERROR;
1465 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1466 ahci_ata_atapi_sense(&xa->rfis, &ccb->csio.sense_data);
1469 kprintf("%s: cmd %d: timeout\n",
1470 PORTNAME(ap), cdb->generic.opcode);
1471 ccbh->status = CAM_CMD_TIMEOUT;
1472 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1473 ahci_ata_dummy_sense(&ccb->csio.sense_data);
1476 kprintf("%s: cmd %d: unknown state %d\n",
1477 PORTNAME(ap), cdb->generic.opcode, xa->state);
1478 ccbh->status = CAM_REQ_CMP_ERR;
1481 ccb->csio.resid = xa->resid;
1482 ahci_ata_put_xfer(xa);
1483 ahci_os_unlock_port(ap);
1485 ahci_os_lock_port(ap);
1489 * Construct dummy sense data for errors on DISKs
1493 ahci_ata_dummy_sense(struct scsi_sense_data *sense_data)
1495 sense_data->error_code = SSD_ERRCODE_VALID | SSD_CURRENT_ERROR;
1496 sense_data->segment = 0;
1497 sense_data->flags = SSD_KEY_MEDIUM_ERROR;
1498 sense_data->info[0] = 0;
1499 sense_data->info[1] = 0;
1500 sense_data->info[2] = 0;
1501 sense_data->info[3] = 0;
1502 sense_data->extra_len = 0;
1506 * Construct atapi sense data for errors on ATAPI
1508 * The ATAPI sense data is stored in the passed rfis and must be converted
1509 * to SCSI sense data.
1513 ahci_ata_atapi_sense(struct ata_fis_d2h *rfis,
1514 struct scsi_sense_data *sense_data)
1516 sense_data->error_code = SSD_ERRCODE_VALID | SSD_CURRENT_ERROR;
1517 sense_data->segment = 0;
1518 sense_data->flags = (rfis->error & 0xF0) >> 4;
1519 if (rfis->error & 0x04)
1520 sense_data->flags |= SSD_KEY_ILLEGAL_REQUEST;
1521 if (rfis->error & 0x02)
1522 sense_data->flags |= SSD_EOM;
1523 if (rfis->error & 0x01)
1524 sense_data->flags |= SSD_ILI;
1525 sense_data->info[0] = 0;
1526 sense_data->info[1] = 0;
1527 sense_data->info[2] = 0;
1528 sense_data->info[3] = 0;
1529 sense_data->extra_len = 0;