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 error = xpt_bus_register(ap->ap_sim, ap->ap_num);
146 if (error != CAM_SUCCESS) {
150 ap->ap_flags |= AP_F_BUS_REGISTERED;
152 error = ahci_cam_probe(ap, NULL);
157 ap->ap_flags |= AP_F_CAM_ATTACHED;
163 * The state of the port has changed.
165 * If at is NULL the physical port has changed state.
166 * If at is non-NULL a particular target behind a PM has changed state.
168 * If found is -1 the target state must be queued to a non-interrupt context.
169 * (only works with at == NULL).
171 * If found is 0 the target was removed.
172 * If found is 1 the target was inserted.
175 ahci_cam_changed(struct ahci_port *ap, struct ata_port *atx, int found)
177 struct cam_path *tmppath;
181 target = atx ? atx->at_target : CAM_TARGET_WILDCARD;
183 if (ap->ap_sim == NULL)
185 if (found == CAM_TARGET_WILDCARD) {
186 status = xpt_create_path(&tmppath, NULL,
187 cam_sim_path(ap->ap_sim),
188 target, CAM_LUN_WILDCARD);
189 if (status != CAM_REQ_CMP)
193 status = xpt_create_path(&tmppath, NULL,
194 cam_sim_path(ap->ap_sim),
197 if (status != CAM_REQ_CMP)
204 xpt_async(AC_FOUND_DEVICE, tmppath, NULL);
206 xpt_async(AC_LOST_DEVICE, tmppath, NULL);
209 xpt_free_path(tmppath);
213 ahci_cam_detach(struct ahci_port *ap)
217 if ((ap->ap_flags & AP_F_CAM_ATTACHED) == 0)
221 xpt_freeze_simq(ap->ap_sim, 1);
223 if (ap->ap_flags & AP_F_BUS_REGISTERED) {
224 error = xpt_bus_deregister(cam_sim_path(ap->ap_sim));
225 KKASSERT(error == CAM_REQ_CMP);
226 ap->ap_flags &= ~AP_F_BUS_REGISTERED;
229 cam_sim_free(ap->ap_sim);
233 ap->ap_flags &= ~AP_F_CAM_ATTACHED;
237 * Once the AHCI port has been attached we need to probe for a device or
238 * devices on the port and setup various options.
240 * If at is NULL we are probing the direct-attached device on the port,
241 * which may or may not be a port multiplier.
244 ahci_cam_probe(struct ahci_port *ap, struct ata_port *atx)
249 u_int64_t capacity_bytes;
263 * Delayed CAM attachment for initial probe, sim may be NULL
265 if (ap->ap_sim == NULL)
269 * A NULL atx indicates a probe of the directly connected device.
270 * A non-NULL atx indicates a device connected via a port multiplier.
271 * We need to preserve atx for calls to ahci_ata_get_xfer().
273 * at is always non-NULL. For directly connected devices we supply
274 * an (at) pointing to target 0.
277 at = ap->ap_ata; /* direct attached - device 0 */
278 if (ap->ap_type == ATA_PORT_T_PM) {
279 kprintf("%s: Found Port Multiplier %d\n",
280 ATANAME(ap, atx), ap->ap_probe);
283 at->at_type = ap->ap_type;
286 if (atx->at_type == ATA_PORT_T_PM) {
287 kprintf("%s: Bogus device, reducing port count to %d\n",
288 ATANAME(ap, atx), atx->at_target);
289 if (ap->ap_pmcount > atx->at_target)
290 ap->ap_pmcount = atx->at_target;
294 if (ap->ap_type == ATA_PORT_T_NONE)
296 if (at->at_type == ATA_PORT_T_NONE)
300 * Issue identify, saving the result
302 xa = ahci_ata_get_xfer(ap, atx);
303 xa->complete = ahci_ata_dummy_done;
304 xa->data = &at->at_identify;
305 xa->datalen = sizeof(at->at_identify);
306 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
308 switch(at->at_type) {
309 case ATA_PORT_T_DISK:
310 xa->fis->command = ATA_C_IDENTIFY;
313 case ATA_PORT_T_ATAPI:
314 xa->fis->command = ATA_C_ATAPI_IDENTIFY;
318 xa->fis->command = ATA_C_ATAPI_IDENTIFY;
319 type = "UNKNOWN(ATAPI?)";
322 xa->fis->features = 0;
324 xa->flags = ATA_F_READ | ATA_F_PIO | ATA_F_POLL;
327 status = ahci_ata_cmd(xa);
328 if (status != ATA_COMPLETE) {
329 kprintf("%s: Detected %s device but unable to IDENTIFY\n",
330 ATANAME(ap, atx), type);
331 ahci_ata_put_xfer(xa);
334 if (xa->state != ATA_S_COMPLETE) {
335 kprintf("%s: Detected %s device but unable to IDENTIFY "
337 ATANAME(ap, atx), type, xa->state);
338 ahci_ata_put_xfer(xa);
341 ahci_ata_put_xfer(xa);
343 ata_fix_identify(&at->at_identify);
346 * Read capacity using SATA probe info.
348 if (le16toh(at->at_identify.cmdset83) & 0x0400) {
349 /* LBA48 feature set supported */
351 for (i = 3; i >= 0; --i) {
354 le16toh(at->at_identify.addrsecxt[i]);
357 capacity = le16toh(at->at_identify.addrsec[1]);
359 capacity += le16toh(at->at_identify.addrsec[0]);
361 at->at_capacity = capacity;
363 ap->ap_probe = ATA_PROBE_GOOD;
365 capacity_bytes = capacity * 512;
368 * Negotiate NCQ, throw away any ata_xfer's beyond the negotiated
369 * number of slots and limit the number of CAM ccb's to one less
370 * so we always have a slot available for recovery.
372 * NCQ is not used if ap_ncqdepth is 1 or the host controller does
373 * not support it, and in that case the driver can handle extra
376 * NCQ is currently used only with direct-attached disks. It is
377 * not used with port multipliers or direct-attached ATAPI devices.
379 * Remember at least one extra CCB needs to be reserved for the
382 if ((ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) &&
383 ap->ap_type == ATA_PORT_T_DISK &&
384 (le16toh(at->at_identify.satacap) & (1 << 8))) {
385 at->at_ncqdepth = (le16toh(at->at_identify.qdepth) & 0x1F) + 1;
386 devncqdepth = at->at_ncqdepth;
387 if (at->at_ncqdepth > ap->ap_sc->sc_ncmds)
388 at->at_ncqdepth = ap->ap_sc->sc_ncmds;
389 if (at->at_ncqdepth > 1) {
390 for (i = 0; i < ap->ap_sc->sc_ncmds; ++i) {
391 xa = ahci_ata_get_xfer(ap, atx);
392 if (xa->tag < at->at_ncqdepth) {
393 xa->state = ATA_S_COMPLETE;
394 ahci_ata_put_xfer(xa);
397 if (at->at_ncqdepth >= ap->ap_sc->sc_ncmds) {
398 cam_devq_resize(ap->ap_sim->devq,
399 at->at_ncqdepth - 1);
407 * Make the model string a bit more presentable
409 for (model_len = 40; model_len; --model_len) {
410 if (at->at_identify.model[model_len-1] == ' ')
412 if (at->at_identify.model[model_len-1] == 0)
418 * Generate informatiive strings.
420 * NOTE: We do not automatically set write caching, lookahead,
421 * or the security state for ATAPI devices.
423 if (at->at_identify.cmdset82 & ATA_IDENTIFY_WRITECACHE) {
424 if (at->at_identify.features85 & ATA_IDENTIFY_WRITECACHE)
426 else if (at->at_type == ATA_PORT_T_ATAPI)
434 if (at->at_identify.cmdset82 & ATA_IDENTIFY_LOOKAHEAD) {
435 if (at->at_identify.features85 & ATA_IDENTIFY_LOOKAHEAD)
437 else if (at->at_type == ATA_PORT_T_ATAPI)
445 if (at->at_identify.cmdset82 & ATA_IDENTIFY_SECURITY) {
446 if (at->at_identify.securestatus & ATA_SECURE_FROZEN)
448 else if (at->at_type == ATA_PORT_T_ATAPI)
456 kprintf("%s: Found %s \"%*.*s %8.8s\" serial=\"%20.20s\"\n"
457 "%s: tags=%d/%d satacaps=%04x satafeat=%04x "
458 "capacity=%lld.%02dMB\n"
459 "%s: f85=%04x f86=%04x f87=%04x WC=%s RA=%s SEC=%s\n",
462 model_len, model_len,
463 at->at_identify.model,
464 at->at_identify.firmware,
465 at->at_identify.serial,
468 devncqdepth, ap->ap_sc->sc_ncmds,
469 at->at_identify.satacap,
470 at->at_identify.satafsup,
471 (long long)capacity_bytes / (1024 * 1024),
472 (int)(capacity_bytes % (1024 * 1024)) * 100 / (1024 * 1024),
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)
520 at = atx ? atx : ap->ap_ata;
523 * Enable write cache if supported
525 * NOTE: "WD My Book" external disk devices have a very poor
526 * daughter board between the the ESATA and the HD. Sending
527 * any ATA_C_SET_FEATURES commands will break the hardware port
528 * with a fatal protocol error. However, this device also
529 * indicates that WRITECACHE is already on and READAHEAD is
530 * not supported so we avoid the issue.
532 if ((at->at_identify.cmdset82 & ATA_IDENTIFY_WRITECACHE) &&
533 (at->at_identify.features85 & ATA_IDENTIFY_WRITECACHE) == 0) {
534 xa = ahci_ata_get_xfer(ap, atx);
535 xa->complete = ahci_ata_dummy_done;
536 xa->fis->command = ATA_C_SET_FEATURES;
537 /*xa->fis->features = ATA_SF_WRITECACHE_EN;*/
538 xa->fis->features = ATA_SF_LOOKAHEAD_EN;
539 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
541 xa->flags = ATA_F_READ | ATA_F_PIO | ATA_F_POLL;
544 status = ahci_ata_cmd(xa);
545 if (status == ATA_COMPLETE)
546 at->at_features |= ATA_PORT_F_WCACHE;
547 ahci_ata_put_xfer(xa);
551 * Enable readahead if supported
553 if ((at->at_identify.cmdset82 & ATA_IDENTIFY_LOOKAHEAD) &&
554 (at->at_identify.features85 & ATA_IDENTIFY_LOOKAHEAD) == 0) {
555 xa = ahci_ata_get_xfer(ap, atx);
556 xa->complete = ahci_ata_dummy_done;
557 xa->fis->command = ATA_C_SET_FEATURES;
558 xa->fis->features = ATA_SF_LOOKAHEAD_EN;
559 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
561 xa->flags = ATA_F_READ | ATA_F_PIO | ATA_F_POLL;
564 status = ahci_ata_cmd(xa);
565 if (status == ATA_COMPLETE)
566 at->at_features |= ATA_PORT_F_RAHEAD;
567 ahci_ata_put_xfer(xa);
571 * FREEZE LOCK the device so malicious users can't lock it on us.
572 * As there is no harm in issuing this to devices that don't
573 * support the security feature set we just send it, and don't bother
574 * checking if the device sends a command abort to tell us it doesn't
577 if ((at->at_identify.cmdset82 & ATA_IDENTIFY_SECURITY) &&
578 (at->at_identify.securestatus & ATA_SECURE_FROZEN) == 0) {
579 xa = ahci_ata_get_xfer(ap, atx);
580 xa->complete = ahci_ata_dummy_done;
581 xa->fis->command = ATA_C_SEC_FREEZE_LOCK;
582 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
583 xa->flags = ATA_F_READ | ATA_F_PIO | ATA_F_POLL;
586 status = ahci_ata_cmd(xa);
587 if (status == ATA_COMPLETE)
588 at->at_features |= ATA_PORT_F_FRZLCK;
589 ahci_ata_put_xfer(xa);
596 * ATAPI-specific probe after initial ident
599 ahci_cam_probe_atapi(struct ahci_port *ap, struct ata_port *atx)
605 * Fix byte ordering so buffers can be accessed as
609 ata_fix_identify(struct ata_identify *id)
614 swap = (u_int16_t *)id->serial;
615 for (i = 0; i < sizeof(id->serial) / sizeof(u_int16_t); i++)
616 swap[i] = bswap16(swap[i]);
618 swap = (u_int16_t *)id->firmware;
619 for (i = 0; i < sizeof(id->firmware) / sizeof(u_int16_t); i++)
620 swap[i] = bswap16(swap[i]);
622 swap = (u_int16_t *)id->model;
623 for (i = 0; i < sizeof(id->model) / sizeof(u_int16_t); i++)
624 swap[i] = bswap16(swap[i]);
628 * Dummy done callback for xa.
631 ahci_ata_dummy_done(struct ata_xfer *xa)
636 * Use an engineering request to initiate a target scan for devices
637 * behind a port multiplier.
639 * An asynchronous bus scan is used to avoid reentrancy issues.
642 ahci_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
644 struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
646 if (ccb->ccb_h.func_code == XPT_SCAN_BUS) {
647 ap->ap_flags &= ~AP_F_SCAN_RUNNING;
648 if (ap->ap_flags & AP_F_SCAN_REQUESTED) {
649 ap->ap_flags &= ~AP_F_SCAN_REQUESTED;
652 ap->ap_flags |= AP_F_SCAN_COMPLETED;
653 wakeup(&ap->ap_flags);
659 ahci_cam_rescan(struct ahci_port *ap)
661 struct cam_path *path;
666 if (ap->ap_flags & AP_F_SCAN_RUNNING) {
667 ap->ap_flags |= AP_F_SCAN_REQUESTED;
670 ap->ap_flags |= AP_F_SCAN_RUNNING;
671 for (i = 0; i < AHCI_MAX_PMPORTS; ++i) {
672 ap->ap_ata[i].at_features |= ATA_PORT_F_RESCAN;
675 status = xpt_create_path(&path, xpt_periph, cam_sim_path(ap->ap_sim),
676 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
677 if (status != CAM_REQ_CMP)
680 ccb = xpt_alloc_ccb();
681 xpt_setup_ccb(&ccb->ccb_h, path, 5); /* 5 = low priority */
682 ccb->ccb_h.func_code = XPT_ENG_EXEC;
683 ccb->ccb_h.cbfcnp = ahci_cam_rescan_callback;
684 ccb->ccb_h.sim_priv.entries[0].ptr = ap;
685 ccb->crcn.flags = CAM_FLAG_NONE;
686 xpt_action_async(ccb);
690 ahci_xpt_rescan(struct ahci_port *ap)
692 struct cam_path *path;
696 status = xpt_create_path(&path, xpt_periph, cam_sim_path(ap->ap_sim),
697 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
698 if (status != CAM_REQ_CMP)
701 ccb = xpt_alloc_ccb();
702 xpt_setup_ccb(&ccb->ccb_h, path, 5); /* 5 = low priority */
703 ccb->ccb_h.func_code = XPT_SCAN_BUS;
704 ccb->ccb_h.cbfcnp = ahci_cam_rescan_callback;
705 ccb->ccb_h.sim_priv.entries[0].ptr = ap;
706 ccb->crcn.flags = CAM_FLAG_NONE;
711 * Action function - dispatch command
715 ahci_xpt_action(struct cam_sim *sim, union ccb *ccb)
717 struct ahci_port *ap;
718 struct ata_port *at, *atx;
719 struct ccb_hdr *ccbh;
723 ap = cam_sim_softc(sim);
726 KKASSERT(ap != NULL);
728 unit = cam_sim_unit(sim);
731 * Early failure checks. These checks do not apply to XPT_PATH_INQ,
732 * otherwise the bus rescan will not remove the dead devices when
735 * For non-wildcards we have one target (0) and one lun (0),
736 * unless we have a port multiplier.
738 * A wildcard target indicates only the general bus is being
741 * Calculate at and atx. at is always non-NULL. atx is only
742 * non-NULL for direct-attached devices. It will be NULL for
743 * devices behind a port multiplier.
745 * XXX What do we do with a LUN wildcard?
747 if (ccbh->target_id != CAM_TARGET_WILDCARD &&
748 ccbh->func_code != XPT_PATH_INQ) {
749 if (ap->ap_type == ATA_PORT_T_NONE) {
750 ccbh->status = CAM_DEV_NOT_THERE;
754 if (ccbh->target_id < 0 || ccbh->target_id >= ap->ap_pmcount) {
755 ccbh->status = CAM_DEV_NOT_THERE;
759 at += ccbh->target_id;
760 if (ap->ap_type == ATA_PORT_T_PM)
763 if (ccbh->target_lun != CAM_LUN_WILDCARD && ccbh->target_lun) {
764 ccbh->status = CAM_DEV_NOT_THERE;
771 * Switch on the meta XPT command
773 switch(ccbh->func_code) {
776 * This routine is called after a port multiplier has been
779 ccbh->status = CAM_REQ_CMP;
780 ahci_os_lock_port(ap);
781 ahci_port_state_machine(ap);
782 ahci_os_unlock_port(ap);
788 * This command always succeeds, otherwise the bus scan
789 * will not detach dead devices.
791 ccb->cpi.version_num = 1;
792 ccb->cpi.hba_inquiry = 0;
793 ccb->cpi.target_sprt = 0;
794 ccb->cpi.hba_misc = PIM_SEQSCAN;
795 ccb->cpi.hba_eng_cnt = 0;
796 bzero(ccb->cpi.vuhba_flags, sizeof(ccb->cpi.vuhba_flags));
797 ccb->cpi.max_target = AHCI_MAX_PMPORTS;
798 ccb->cpi.max_lun = 0;
799 ccb->cpi.async_flags = 0;
800 ccb->cpi.hpath_id = 0;
801 ccb->cpi.initiator_id = AHCI_MAX_PMPORTS - 1;
802 ccb->cpi.unit_number = cam_sim_unit(sim);
803 ccb->cpi.bus_id = cam_sim_bus(sim);
804 ccb->cpi.base_transfer_speed = 150000;
805 ccb->cpi.transport = XPORT_AHCI;
806 ccb->cpi.transport_version = 1;
807 ccb->cpi.protocol = PROTO_SCSI;
808 ccb->cpi.protocol_version = SCSI_REV_2;
810 ccbh->status = CAM_REQ_CMP;
811 if (ccbh->target_id != CAM_TARGET_WILDCARD) {
812 ahci_port_state_machine(ap);
814 switch(ahci_pread(ap, AHCI_PREG_SSTS) &
815 AHCI_PREG_SSTS_SPD) {
816 case AHCI_PREG_SSTS_SPD_GEN1:
817 ccb->cpi.base_transfer_speed = 150000;
819 case AHCI_PREG_SSTS_SPD_GEN2:
820 ccb->cpi.base_transfer_speed = 300000;
824 ccb->cpi.base_transfer_speed = 1000;
828 if (ap->ap_type == ATA_PORT_T_NONE)
829 ccbh->status = CAM_DEV_NOT_THERE;
835 ahci_os_lock_port(ap);
836 if (ap->ap_type == ATA_PORT_T_NONE) {
837 ccbh->status = CAM_DEV_NOT_THERE;
839 ahci_port_reset(ap, atx, 0);
840 ccbh->status = CAM_REQ_CMP;
842 ahci_os_unlock_port(ap);
846 ahci_os_lock_port(ap);
847 ahci_port_reset(ap, NULL, 1);
848 ahci_os_unlock_port(ap);
849 ccbh->status = CAM_REQ_CMP;
852 case XPT_SET_TRAN_SETTINGS:
853 ccbh->status = CAM_FUNC_NOTAVAIL;
856 case XPT_GET_TRAN_SETTINGS:
857 ccb->cts.protocol = PROTO_SCSI;
858 ccb->cts.protocol_version = SCSI_REV_2;
859 ccb->cts.transport = XPORT_AHCI;
860 ccb->cts.transport_version = XPORT_VERSION_UNSPECIFIED;
861 ccb->cts.proto_specific.valid = 0;
862 ccb->cts.xport_specific.valid = 0;
863 ccbh->status = CAM_REQ_CMP;
866 case XPT_CALC_GEOMETRY:
867 cam_calc_geometry(&ccb->ccg, 1);
872 * Our parallel startup code might have only probed through
873 * to the IDENT, so do the last step if necessary.
875 if (at->at_probe == ATA_PROBE_NEED_IDENT)
876 ahci_cam_probe(ap, atx);
877 if (at->at_probe != ATA_PROBE_GOOD) {
878 ccbh->status = CAM_DEV_NOT_THERE;
882 switch(at->at_type) {
883 case ATA_PORT_T_DISK:
884 ahci_xpt_scsi_disk_io(ap, atx, ccb);
886 case ATA_PORT_T_ATAPI:
887 ahci_xpt_scsi_atapi_io(ap, atx, ccb);
890 ccbh->status = CAM_REQ_INVALID;
896 ccbh->status = CAM_REQ_INVALID;
905 * Generally this function gets called heavily when interrupts might be
906 * non-operational, during a halt/reboot or panic.
910 ahci_xpt_poll(struct cam_sim *sim)
912 struct ahci_port *ap;
914 ap = cam_sim_softc(sim);
916 ahci_os_lock_port(ap);
917 ahci_port_intr(ap, 1);
918 ahci_os_unlock_port(ap);
923 * Convert the SCSI command in ccb to an ata_xfer command in xa
924 * for ATA_PORT_T_DISK operations. Set the completion function
925 * to convert the response back, then dispatch to the OpenBSD AHCI
928 * AHCI DISK commands only support a limited command set, and we
929 * fake additional commands to make it play nice with the CAM subsystem.
933 ahci_xpt_scsi_disk_io(struct ahci_port *ap, struct ata_port *atx,
936 struct ccb_hdr *ccbh;
937 struct ccb_scsiio *csio;
940 struct ata_fis_h2d *fis;
942 union scsi_data *rdata;
948 ccbh = &ccb->csio.ccb_h;
950 at = atx ? atx : &ap->ap_ata[0];
953 * XXX not passing NULL at for direct attach!
955 xa = ahci_ata_get_xfer(ap, atx);
956 rdata = (void *)csio->data_ptr;
957 rdata_len = csio->dxfer_len;
960 * Build the FIS or process the csio to completion.
962 cdb = (void *)((ccbh->flags & CAM_CDB_POINTER) ?
963 csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes);
965 switch(cdb->generic.opcode) {
968 * Auto-sense everything, so explicit sense requests
971 ccbh->status = CAM_SCSI_STATUS_ERROR;
975 * Inquiry supported features
977 * [opcode, byte2, page_code, length, control]
979 if (cdb->inquiry.byte2 & SI_EVPD) {
980 switch(cdb->inquiry.page_code) {
981 case SVPD_SUPPORTED_PAGE_LIST:
982 /* XXX atascsi_disk_vpd_supported */
983 case SVPD_UNIT_SERIAL_NUMBER:
984 /* XXX atascsi_disk_vpd_serial */
985 case SVPD_UNIT_DEVID:
986 /* XXX atascsi_disk_vpd_ident */
988 ccbh->status = CAM_FUNC_NOTAVAIL;
992 bzero(rdata, rdata_len);
993 if (rdata_len < SHORT_INQUIRY_LENGTH) {
994 ccbh->status = CAM_CCB_LEN_ERR;
997 if (rdata_len > sizeof(rdata->inquiry_data))
998 rdata_len = sizeof(rdata->inquiry_data);
999 rdata->inquiry_data.device = T_DIRECT;
1000 rdata->inquiry_data.version = SCSI_REV_SPC2;
1001 rdata->inquiry_data.response_format = 2;
1002 rdata->inquiry_data.additional_length = 32;
1003 bcopy("SATA ", rdata->inquiry_data.vendor, 8);
1004 bcopy(at->at_identify.model,
1005 rdata->inquiry_data.product,
1006 sizeof(rdata->inquiry_data.product));
1007 bcopy(at->at_identify.firmware,
1008 rdata->inquiry_data.revision,
1009 sizeof(rdata->inquiry_data.revision));
1010 ccbh->status = CAM_REQ_CMP;
1013 case READ_CAPACITY_16:
1014 if (cdb->read_capacity_16.service_action != SRC16_SERVICE_ACTION) {
1015 ccbh->status = CAM_REQ_INVALID;
1018 if (rdata_len < sizeof(rdata->read_capacity_data_16)) {
1019 ccbh->status = CAM_CCB_LEN_ERR;
1024 if (rdata_len < sizeof(rdata->read_capacity_data)) {
1025 ccbh->status = CAM_CCB_LEN_ERR;
1029 capacity = at->at_capacity;
1031 bzero(rdata, rdata_len);
1032 if (cdb->generic.opcode == READ_CAPACITY) {
1033 rdata_len = sizeof(rdata->read_capacity_data);
1034 if (capacity > 0xFFFFFFFFU)
1035 capacity = 0xFFFFFFFFU;
1036 bzero(&rdata->read_capacity_data, rdata_len);
1037 scsi_ulto4b((u_int32_t)capacity - 1,
1038 rdata->read_capacity_data.addr);
1039 scsi_ulto4b(512, rdata->read_capacity_data.length);
1041 rdata_len = sizeof(rdata->read_capacity_data_16);
1042 bzero(&rdata->read_capacity_data_16, rdata_len);
1043 scsi_u64to8b(capacity - 1,
1044 rdata->read_capacity_data_16.addr);
1045 scsi_ulto4b(512, rdata->read_capacity_data_16.length);
1047 ccbh->status = CAM_REQ_CMP;
1049 case SYNCHRONIZE_CACHE:
1051 * Synchronize cache. Specification says this can take
1052 * greater then 30 seconds so give it at least 45.
1055 fis->flags = ATA_H2D_FLAGS_CMD;
1056 fis->command = ATA_C_FLUSH_CACHE;
1058 if (xa->timeout < 45000)
1059 xa->timeout = 45000;
1061 xa->flags = ATA_F_READ;
1062 xa->complete = ahci_ata_complete_disk_synchronize_cache;
1064 case TEST_UNIT_READY:
1065 case START_STOP_UNIT:
1068 * Just silently return success
1070 ccbh->status = CAM_REQ_CMP;
1076 * XXX implement pass-through
1078 ccbh->status = CAM_FUNC_NOTAVAIL;
1081 switch(cdb->generic.opcode) {
1083 lba = scsi_3btoul(cdb->rw_6.addr) & 0x1FFFFF;
1084 count = cdb->rw_6.length ? cdb->rw_6.length : 0x100;
1085 xa->flags = ATA_F_READ;
1088 lba = scsi_4btoul(cdb->rw_10.addr);
1089 count = scsi_2btoul(cdb->rw_10.length);
1090 xa->flags = ATA_F_READ;
1093 lba = scsi_4btoul(cdb->rw_12.addr);
1094 count = scsi_4btoul(cdb->rw_12.length);
1095 xa->flags = ATA_F_READ;
1098 lba = scsi_8btou64(cdb->rw_16.addr);
1099 count = scsi_4btoul(cdb->rw_16.length);
1100 xa->flags = ATA_F_READ;
1103 lba = scsi_3btoul(cdb->rw_6.addr) & 0x1FFFFF;
1104 count = cdb->rw_6.length ? cdb->rw_6.length : 0x100;
1105 xa->flags = ATA_F_WRITE;
1108 lba = scsi_4btoul(cdb->rw_10.addr);
1109 count = scsi_2btoul(cdb->rw_10.length);
1110 xa->flags = ATA_F_WRITE;
1113 lba = scsi_4btoul(cdb->rw_12.addr);
1114 count = scsi_4btoul(cdb->rw_12.length);
1115 xa->flags = ATA_F_WRITE;
1118 lba = scsi_8btou64(cdb->rw_16.addr);
1119 count = scsi_4btoul(cdb->rw_16.length);
1120 xa->flags = ATA_F_WRITE;
1123 ccbh->status = CAM_REQ_INVALID;
1126 if (ccbh->status != CAM_REQ_INPROG)
1130 fis->flags = ATA_H2D_FLAGS_CMD;
1131 fis->lba_low = (u_int8_t)lba;
1132 fis->lba_mid = (u_int8_t)(lba >> 8);
1133 fis->lba_high = (u_int8_t)(lba >> 16);
1134 fis->device = ATA_H2D_DEVICE_LBA;
1137 * NCQ only for direct-attached disks, do not currently
1138 * try to use NCQ with port multipliers.
1140 if (at->at_ncqdepth > 1 &&
1141 ap->ap_type == ATA_PORT_T_DISK &&
1142 (ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) &&
1143 (ccbh->flags & CAM_POLLED) == 0) {
1145 * Use NCQ - always uses 48 bit addressing
1147 xa->flags |= ATA_F_NCQ;
1148 fis->command = (xa->flags & ATA_F_WRITE) ?
1149 ATA_C_WRITE_FPDMA : ATA_C_READ_FPDMA;
1150 fis->lba_low_exp = (u_int8_t)(lba >> 24);
1151 fis->lba_mid_exp = (u_int8_t)(lba >> 32);
1152 fis->lba_high_exp = (u_int8_t)(lba >> 40);
1153 fis->sector_count = xa->tag << 3;
1154 fis->features = (u_int8_t)count;
1155 fis->features_exp = (u_int8_t)(count >> 8);
1156 } else if (count > 0x100 || lba > 0xFFFFFFFFU) {
1160 fis->command = (xa->flags & ATA_F_WRITE) ?
1161 ATA_C_WRITEDMA_EXT : ATA_C_READDMA_EXT;
1162 fis->lba_low_exp = (u_int8_t)(lba >> 24);
1163 fis->lba_mid_exp = (u_int8_t)(lba >> 32);
1164 fis->lba_high_exp = (u_int8_t)(lba >> 40);
1165 fis->sector_count = (u_int8_t)count;
1166 fis->sector_count_exp = (u_int8_t)(count >> 8);
1171 * NOTE: 256 sectors is supported, stored as 0.
1173 fis->command = (xa->flags & ATA_F_WRITE) ?
1174 ATA_C_WRITEDMA : ATA_C_READDMA;
1175 fis->device |= (u_int8_t)(lba >> 24) & 0x0F;
1176 fis->sector_count = (u_int8_t)count;
1179 xa->data = csio->data_ptr;
1180 xa->datalen = csio->dxfer_len;
1181 xa->complete = ahci_ata_complete_disk_rw;
1182 xa->timeout = ccbh->timeout; /* milliseconds */
1183 if (ccbh->flags & CAM_POLLED)
1184 xa->flags |= ATA_F_POLL;
1189 * If the request is still in progress the xa and FIS have
1190 * been set up and must be dispatched. Otherwise the request
1193 if (ccbh->status == CAM_REQ_INPROG) {
1194 KKASSERT(xa->complete != NULL);
1195 xa->atascsi_private = ccb;
1196 ccb->ccb_h.sim_priv.entries[0].ptr = ap;
1197 ahci_os_lock_port(ap);
1198 fis->flags |= at->at_target;
1200 ahci_os_unlock_port(ap);
1202 ahci_ata_put_xfer(xa);
1208 * Convert the SCSI command in ccb to an ata_xfer command in xa
1209 * for ATA_PORT_T_ATAPI operations. Set the completion function
1210 * to convert the response back, then dispatch to the OpenBSD AHCI
1215 ahci_xpt_scsi_atapi_io(struct ahci_port *ap, struct ata_port *atx,
1218 struct ccb_hdr *ccbh;
1219 struct ccb_scsiio *csio;
1220 struct ata_xfer *xa;
1221 struct ata_fis_h2d *fis;
1225 struct ata_port *at;
1227 ccbh = &ccb->csio.ccb_h;
1229 at = atx ? atx : &ap->ap_ata[0];
1231 switch (ccbh->flags & CAM_DIR_MASK) {
1233 flags = ATA_F_PACKET | ATA_F_READ;
1236 flags = ATA_F_PACKET | ATA_F_WRITE;
1239 flags = ATA_F_PACKET;
1242 ccbh->status = CAM_REQ_INVALID;
1249 * The command has to fit in the packet command buffer.
1251 if (csio->cdb_len < 6 || csio->cdb_len > 16) {
1252 ccbh->status = CAM_CCB_LEN_ERR;
1258 * Initialize the XA and FIS.
1260 * XXX not passing NULL at for direct attach!
1262 xa = ahci_ata_get_xfer(ap, atx);
1265 fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
1266 fis->command = ATA_C_PACKET;
1268 fis->sector_count = xa->tag << 3;
1269 fis->features = ATA_H2D_FEATURES_DMA |
1270 ((flags & ATA_F_WRITE) ?
1271 ATA_H2D_FEATURES_DIR_WRITE : ATA_H2D_FEATURES_DIR_READ);
1272 fis->lba_mid = 0x00;
1273 fis->lba_high = 0x20;
1276 xa->data = csio->data_ptr;
1277 xa->datalen = csio->dxfer_len;
1278 xa->timeout = ccbh->timeout; /* milliseconds */
1280 if (ccbh->flags & CAM_POLLED)
1281 xa->flags |= ATA_F_POLL;
1284 * Copy the cdb to the packetcmd buffer in the FIS using a
1285 * convenient pointer in the xa.
1287 cdbs = (void *)((ccbh->flags & CAM_CDB_POINTER) ?
1288 csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes);
1289 bcopy(cdbs, xa->packetcmd, csio->cdb_len);
1292 kprintf("opcode %d cdb_len %d dxfer_len %d\n",
1293 cdbs->generic.opcode,
1294 csio->cdb_len, csio->dxfer_len);
1298 * Some ATAPI commands do not actually follow the SCSI standard.
1300 cdbd = (void *)xa->packetcmd;
1302 switch(cdbd->generic.opcode) {
1305 * Some ATAPI devices can't handle SI_EVPD being set
1306 * for a basic inquiry (page_code == 0).
1308 * Some ATAPI devices can't handle long inquiry lengths,
1309 * don't ask me why. Truncate the inquiry length.
1311 if ((cdbd->inquiry.byte2 & SI_EVPD) &&
1312 cdbd->inquiry.page_code == 0) {
1313 cdbd->inquiry.byte2 &= ~SI_EVPD;
1315 if (cdbd->inquiry.page_code == 0 &&
1316 cdbd->inquiry.length > SHORT_INQUIRY_LENGTH) {
1317 cdbd->inquiry.length = SHORT_INQUIRY_LENGTH;
1323 * Convert *_6 to *_10 commands. Most ATAPI devices
1324 * cannot handle the SCSI READ_6 and WRITE_6 commands.
1326 cdbd->rw_10.opcode |= 0x20;
1327 cdbd->rw_10.byte2 = 0;
1328 cdbd->rw_10.addr[0] = cdbs->rw_6.addr[0] & 0x1F;
1329 cdbd->rw_10.addr[1] = cdbs->rw_6.addr[1];
1330 cdbd->rw_10.addr[2] = cdbs->rw_6.addr[2];
1331 cdbd->rw_10.addr[3] = 0;
1332 cdbd->rw_10.reserved = 0;
1333 cdbd->rw_10.length[0] = 0;
1334 cdbd->rw_10.length[1] = cdbs->rw_6.length;
1335 cdbd->rw_10.control = cdbs->rw_6.control;
1344 xa->complete = ahci_atapi_complete_cmd;
1345 xa->atascsi_private = ccb;
1346 ccb->ccb_h.sim_priv.entries[0].ptr = ap;
1351 * Completion function for ATA_PORT_T_DISK cache synchronization.
1355 ahci_ata_complete_disk_synchronize_cache(struct ata_xfer *xa)
1357 union ccb *ccb = xa->atascsi_private;
1358 struct ccb_hdr *ccbh = &ccb->ccb_h;
1359 struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
1362 case ATA_S_COMPLETE:
1363 ccbh->status = CAM_REQ_CMP;
1364 ccb->csio.scsi_status = SCSI_STATUS_OK;
1367 kprintf("%s: synchronize_cache: error\n",
1368 ATANAME(ap, xa->at));
1369 ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
1370 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1371 ahci_ata_dummy_sense(&ccb->csio.sense_data);
1374 kprintf("%s: synchronize_cache: timeout\n",
1375 ATANAME(ap, xa->at));
1376 ccbh->status = CAM_CMD_TIMEOUT;
1379 kprintf("%s: synchronize_cache: unknown state %d\n",
1380 ATANAME(ap, xa->at), xa->state);
1381 ccbh->status = CAM_REQ_CMP_ERR;
1384 ahci_ata_put_xfer(xa);
1385 ahci_os_unlock_port(ap);
1387 ahci_os_lock_port(ap);
1391 * Completion function for ATA_PORT_T_DISK I/O
1395 ahci_ata_complete_disk_rw(struct ata_xfer *xa)
1397 union ccb *ccb = xa->atascsi_private;
1398 struct ccb_hdr *ccbh = &ccb->ccb_h;
1399 struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
1402 case ATA_S_COMPLETE:
1403 ccbh->status = CAM_REQ_CMP;
1404 ccb->csio.scsi_status = SCSI_STATUS_OK;
1407 kprintf("%s: disk_rw: error\n", ATANAME(ap, xa->at));
1408 ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
1409 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1410 ahci_ata_dummy_sense(&ccb->csio.sense_data);
1413 kprintf("%s: disk_rw: timeout\n", ATANAME(ap, xa->at));
1414 ccbh->status = CAM_CMD_TIMEOUT;
1417 kprintf("%s: disk_rw: unknown state %d\n",
1418 ATANAME(ap, xa->at), xa->state);
1419 ccbh->status = CAM_REQ_CMP_ERR;
1422 ccb->csio.resid = xa->resid;
1423 ahci_ata_put_xfer(xa);
1424 ahci_os_unlock_port(ap);
1426 ahci_os_lock_port(ap);
1430 * Completion function for ATA_PORT_T_ATAPI I/O
1432 * Sense data is returned in the rfis.
1436 ahci_atapi_complete_cmd(struct ata_xfer *xa)
1438 union ccb *ccb = xa->atascsi_private;
1439 struct ccb_hdr *ccbh = &ccb->ccb_h;
1440 struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
1443 cdb = (void *)((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
1444 ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes);
1447 case ATA_S_COMPLETE:
1448 ccbh->status = CAM_REQ_CMP;
1449 ccb->csio.scsi_status = SCSI_STATUS_OK;
1452 kprintf("%s: cmd %d: error\n",
1453 PORTNAME(ap), cdb->generic.opcode);
1454 ccbh->status = CAM_SCSI_STATUS_ERROR;
1455 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1456 ahci_ata_atapi_sense(&xa->rfis, &ccb->csio.sense_data);
1459 kprintf("%s: cmd %d: timeout\n",
1460 PORTNAME(ap), cdb->generic.opcode);
1461 ccbh->status = CAM_CMD_TIMEOUT;
1464 kprintf("%s: cmd %d: unknown state %d\n",
1465 PORTNAME(ap), cdb->generic.opcode, xa->state);
1466 ccbh->status = CAM_REQ_CMP_ERR;
1469 ccb->csio.resid = xa->resid;
1470 ahci_ata_put_xfer(xa);
1471 ahci_os_unlock_port(ap);
1473 ahci_os_lock_port(ap);
1477 * Construct dummy sense data for errors on DISKs
1481 ahci_ata_dummy_sense(struct scsi_sense_data *sense_data)
1483 sense_data->error_code = SSD_ERRCODE_VALID | SSD_CURRENT_ERROR;
1484 sense_data->segment = 0;
1485 sense_data->flags = SSD_KEY_MEDIUM_ERROR;
1486 sense_data->info[0] = 0;
1487 sense_data->info[1] = 0;
1488 sense_data->info[2] = 0;
1489 sense_data->info[3] = 0;
1490 sense_data->extra_len = 0;
1494 * Construct atapi sense data for errors on ATAPI
1496 * The ATAPI sense data is stored in the passed rfis and must be converted
1497 * to SCSI sense data.
1501 ahci_ata_atapi_sense(struct ata_fis_d2h *rfis,
1502 struct scsi_sense_data *sense_data)
1504 sense_data->error_code = SSD_ERRCODE_VALID | SSD_CURRENT_ERROR;
1505 sense_data->segment = 0;
1506 sense_data->flags = (rfis->error & 0xF0) >> 4;
1507 if (rfis->error & 0x04)
1508 sense_data->flags |= SSD_KEY_ILLEGAL_REQUEST;
1509 if (rfis->error & 0x02)
1510 sense_data->flags |= SSD_EOM;
1511 if (rfis->error & 0x01)
1512 sense_data->flags |= SSD_ILI;
1513 sense_data->info[0] = 0;
1514 sense_data->info[1] = 0;
1515 sense_data->info[2] = 0;
1516 sense_data->info[3] = 0;
1517 sense_data->extra_len = 0;