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->flags = ATA_F_READ | ATA_F_PIO | ATA_F_POLL;
311 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
313 switch(at->at_type) {
314 case ATA_PORT_T_DISK:
315 xa->fis->command = ATA_C_IDENTIFY;
318 case ATA_PORT_T_ATAPI:
319 xa->fis->command = ATA_C_ATAPI_IDENTIFY;
320 xa->flags |= ATA_F_AUTOSENSE;
324 xa->fis->command = ATA_C_ATAPI_IDENTIFY;
325 type = "UNKNOWN(ATAPI?)";
328 xa->fis->features = 0;
332 if (ahci_ata_cmd(xa) != ATA_S_COMPLETE) {
333 kprintf("%s: Detected %s device but unable to IDENTIFY\n",
334 ATANAME(ap, atx), type);
335 ahci_ata_put_xfer(xa);
338 ahci_ata_put_xfer(xa);
340 ata_fix_identify(&at->at_identify);
343 * Read capacity using SATA probe info.
345 if (le16toh(at->at_identify.cmdset83) & 0x0400) {
346 /* LBA48 feature set supported */
348 for (i = 3; i >= 0; --i) {
351 le16toh(at->at_identify.addrsecxt[i]);
354 capacity = le16toh(at->at_identify.addrsec[1]);
356 capacity += le16toh(at->at_identify.addrsec[0]);
359 capacity = 1024 * 1024 / 512;
360 at->at_capacity = capacity;
362 ap->ap_probe = ATA_PROBE_GOOD;
364 capacity_bytes = capacity * 512;
367 * Negotiate NCQ, throw away any ata_xfer's beyond the negotiated
368 * number of slots and limit the number of CAM ccb's to one less
369 * so we always have a slot available for recovery.
371 * NCQ is not used if ap_ncqdepth is 1 or the host controller does
372 * not support it, and in that case the driver can handle extra
375 * NCQ is currently used only with direct-attached disks. It is
376 * not used with port multipliers or direct-attached ATAPI devices.
378 * Remember at least one extra CCB needs to be reserved for the
381 if ((ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) &&
382 ap->ap_type == ATA_PORT_T_DISK &&
383 (le16toh(at->at_identify.satacap) & (1 << 8))) {
384 at->at_ncqdepth = (le16toh(at->at_identify.qdepth) & 0x1F) + 1;
385 devncqdepth = at->at_ncqdepth;
386 if (at->at_ncqdepth > ap->ap_sc->sc_ncmds)
387 at->at_ncqdepth = ap->ap_sc->sc_ncmds;
388 if (at->at_ncqdepth > 1) {
389 for (i = 0; i < ap->ap_sc->sc_ncmds; ++i) {
390 xa = ahci_ata_get_xfer(ap, atx);
391 if (xa->tag < at->at_ncqdepth) {
392 xa->state = ATA_S_COMPLETE;
393 ahci_ata_put_xfer(xa);
396 if (at->at_ncqdepth >= ap->ap_sc->sc_ncmds) {
397 cam_devq_resize(ap->ap_sim->devq,
398 at->at_ncqdepth - 1);
406 * Make the model string a bit more presentable
408 for (model_len = 40; model_len; --model_len) {
409 if (at->at_identify.model[model_len-1] == ' ')
411 if (at->at_identify.model[model_len-1] == 0)
417 * Generate informatiive strings.
419 * NOTE: We do not automatically set write caching, lookahead,
420 * or the security state for ATAPI devices.
422 if (at->at_identify.cmdset82 & ATA_IDENTIFY_WRITECACHE) {
423 if (at->at_identify.features85 & ATA_IDENTIFY_WRITECACHE)
425 else if (at->at_type == ATA_PORT_T_ATAPI)
433 if (at->at_identify.cmdset82 & ATA_IDENTIFY_LOOKAHEAD) {
434 if (at->at_identify.features85 & ATA_IDENTIFY_LOOKAHEAD)
436 else if (at->at_type == ATA_PORT_T_ATAPI)
444 if (at->at_identify.cmdset82 & ATA_IDENTIFY_SECURITY) {
445 if (at->at_identify.securestatus & ATA_SECURE_FROZEN)
447 else if (at->at_type == ATA_PORT_T_ATAPI)
449 else if (AhciNoFeatures & (1 << ap->ap_num))
450 scstr = "<disabled>";
457 kprintf("%s: Found %s \"%*.*s %8.8s\" serial=\"%20.20s\"\n"
458 "%s: tags=%d/%d satacap=%04x satafea=%04x NCQ=%s "
459 "capacity=%lld.%02dMB\n",
463 model_len, model_len,
464 at->at_identify.model,
465 at->at_identify.firmware,
466 at->at_identify.serial,
469 devncqdepth, ap->ap_sc->sc_ncmds,
470 at->at_identify.satacap,
471 at->at_identify.satafsup,
472 (at->at_ncqdepth > 1 ? "YES" : "NO"),
473 (long long)capacity_bytes / (1024 * 1024),
474 (int)(capacity_bytes % (1024 * 1024)) * 100 / (1024 * 1024)
476 kprintf("%s: f85=%04x f86=%04x f87=%04x WC=%s RA=%s SEC=%s\n",
478 at->at_identify.features85,
479 at->at_identify.features86,
480 at->at_identify.features87,
487 * Additional type-specific probing
489 switch(at->at_type) {
490 case ATA_PORT_T_DISK:
491 error = ahci_cam_probe_disk(ap, atx);
493 case ATA_PORT_T_ATAPI:
494 error = ahci_cam_probe_atapi(ap, atx);
502 at->at_probe = ATA_PROBE_FAILED;
504 ap->ap_probe = at->at_probe;
506 at->at_probe = ATA_PROBE_GOOD;
508 ap->ap_probe = at->at_probe;
514 * DISK-specific probe after initial ident
517 ahci_cam_probe_disk(struct ahci_port *ap, struct ata_port *atx)
522 at = atx ? atx : ap->ap_ata;
525 * Enable write cache if supported
527 * NOTE: "WD My Book" external disk devices have a very poor
528 * daughter board between the the ESATA and the HD. Sending
529 * any ATA_C_SET_FEATURES commands will break the hardware port
530 * with a fatal protocol error. However, this device also
531 * indicates that WRITECACHE is already on and READAHEAD is
532 * not supported so we avoid the issue.
534 if ((at->at_identify.cmdset82 & ATA_IDENTIFY_WRITECACHE) &&
535 (at->at_identify.features85 & ATA_IDENTIFY_WRITECACHE) == 0) {
536 xa = ahci_ata_get_xfer(ap, atx);
537 xa->complete = ahci_ata_dummy_done;
538 xa->fis->command = ATA_C_SET_FEATURES;
539 /*xa->fis->features = ATA_SF_WRITECACHE_EN;*/
540 xa->fis->features = ATA_SF_LOOKAHEAD_EN;
541 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
543 xa->flags = ATA_F_READ | ATA_F_PIO | ATA_F_POLL;
546 if (ahci_ata_cmd(xa) == ATA_S_COMPLETE)
547 at->at_features |= ATA_PORT_F_WCACHE;
549 kprintf("%s: Unable to enable write-caching\n",
551 ahci_ata_put_xfer(xa);
555 * Enable readahead if supported
557 if ((at->at_identify.cmdset82 & ATA_IDENTIFY_LOOKAHEAD) &&
558 (at->at_identify.features85 & ATA_IDENTIFY_LOOKAHEAD) == 0) {
559 xa = ahci_ata_get_xfer(ap, atx);
560 xa->complete = ahci_ata_dummy_done;
561 xa->fis->command = ATA_C_SET_FEATURES;
562 xa->fis->features = ATA_SF_LOOKAHEAD_EN;
563 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
565 xa->flags = ATA_F_READ | ATA_F_PIO | ATA_F_POLL;
568 if (ahci_ata_cmd(xa) == ATA_S_COMPLETE)
569 at->at_features |= ATA_PORT_F_RAHEAD;
571 kprintf("%s: Unable to enable read-ahead\n",
573 ahci_ata_put_xfer(xa);
577 * FREEZE LOCK the device so malicious users can't lock it on us.
578 * As there is no harm in issuing this to devices that don't
579 * support the security feature set we just send it, and don't bother
580 * checking if the device sends a command abort to tell us it doesn't
583 if ((at->at_identify.cmdset82 & ATA_IDENTIFY_SECURITY) &&
584 (at->at_identify.securestatus & ATA_SECURE_FROZEN) == 0 &&
585 (AhciNoFeatures & (1 << ap->ap_num)) == 0) {
586 xa = ahci_ata_get_xfer(ap, atx);
587 xa->complete = ahci_ata_dummy_done;
588 xa->fis->command = ATA_C_SEC_FREEZE_LOCK;
589 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
590 xa->flags = ATA_F_READ | ATA_F_PIO | ATA_F_POLL;
593 if (ahci_ata_cmd(xa) == ATA_S_COMPLETE)
594 at->at_features |= ATA_PORT_F_FRZLCK;
596 kprintf("%s: Unable to set security freeze\n",
598 ahci_ata_put_xfer(xa);
605 * ATAPI-specific probe after initial ident
608 ahci_cam_probe_atapi(struct ahci_port *ap, struct ata_port *atx)
614 * Fix byte ordering so buffers can be accessed as
618 ata_fix_identify(struct ata_identify *id)
623 swap = (u_int16_t *)id->serial;
624 for (i = 0; i < sizeof(id->serial) / sizeof(u_int16_t); i++)
625 swap[i] = bswap16(swap[i]);
627 swap = (u_int16_t *)id->firmware;
628 for (i = 0; i < sizeof(id->firmware) / sizeof(u_int16_t); i++)
629 swap[i] = bswap16(swap[i]);
631 swap = (u_int16_t *)id->model;
632 for (i = 0; i < sizeof(id->model) / sizeof(u_int16_t); i++)
633 swap[i] = bswap16(swap[i]);
637 * Dummy done callback for xa.
640 ahci_ata_dummy_done(struct ata_xfer *xa)
645 * Use an engineering request to initiate a target scan for devices
646 * behind a port multiplier.
648 * An asynchronous bus scan is used to avoid reentrancy issues.
651 ahci_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
653 struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
655 if (ccb->ccb_h.func_code == XPT_SCAN_BUS) {
656 ap->ap_flags &= ~AP_F_SCAN_RUNNING;
657 if (ap->ap_flags & AP_F_SCAN_REQUESTED) {
658 ap->ap_flags &= ~AP_F_SCAN_REQUESTED;
661 ap->ap_flags |= AP_F_SCAN_COMPLETED;
662 wakeup(&ap->ap_flags);
668 ahci_cam_rescan(struct ahci_port *ap)
670 struct cam_path *path;
675 if (ap->ap_flags & AP_F_SCAN_RUNNING) {
676 ap->ap_flags |= AP_F_SCAN_REQUESTED;
679 ap->ap_flags |= AP_F_SCAN_RUNNING;
680 for (i = 0; i < AHCI_MAX_PMPORTS; ++i) {
681 ap->ap_ata[i].at_features |= ATA_PORT_F_RESCAN;
684 status = xpt_create_path(&path, xpt_periph, cam_sim_path(ap->ap_sim),
685 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
686 if (status != CAM_REQ_CMP)
689 ccb = xpt_alloc_ccb();
690 xpt_setup_ccb(&ccb->ccb_h, path, 5); /* 5 = low priority */
691 ccb->ccb_h.func_code = XPT_ENG_EXEC;
692 ccb->ccb_h.cbfcnp = ahci_cam_rescan_callback;
693 ccb->ccb_h.sim_priv.entries[0].ptr = ap;
694 ccb->crcn.flags = CAM_FLAG_NONE;
695 xpt_action_async(ccb);
699 ahci_xpt_rescan(struct ahci_port *ap)
701 struct cam_path *path;
705 status = xpt_create_path(&path, xpt_periph, cam_sim_path(ap->ap_sim),
706 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
707 if (status != CAM_REQ_CMP)
710 ccb = xpt_alloc_ccb();
711 xpt_setup_ccb(&ccb->ccb_h, path, 5); /* 5 = low priority */
712 ccb->ccb_h.func_code = XPT_SCAN_BUS;
713 ccb->ccb_h.cbfcnp = ahci_cam_rescan_callback;
714 ccb->ccb_h.sim_priv.entries[0].ptr = ap;
715 ccb->crcn.flags = CAM_FLAG_NONE;
716 xpt_action_async(ccb);
720 * Action function - dispatch command
724 ahci_xpt_action(struct cam_sim *sim, union ccb *ccb)
726 struct ahci_port *ap;
727 struct ata_port *at, *atx;
728 struct ccb_hdr *ccbh;
732 ap = cam_sim_softc(sim);
735 KKASSERT(ap != NULL);
737 unit = cam_sim_unit(sim);
740 * Early failure checks. These checks do not apply to XPT_PATH_INQ,
741 * otherwise the bus rescan will not remove the dead devices when
744 * For non-wildcards we have one target (0) and one lun (0),
745 * unless we have a port multiplier.
747 * A wildcard target indicates only the general bus is being
750 * Calculate at and atx. at is always non-NULL. atx is only
751 * non-NULL for direct-attached devices. It will be NULL for
752 * devices behind a port multiplier.
754 * XXX What do we do with a LUN wildcard?
756 if (ccbh->target_id != CAM_TARGET_WILDCARD &&
757 ccbh->func_code != XPT_PATH_INQ) {
758 if (ap->ap_type == ATA_PORT_T_NONE) {
759 ccbh->status = CAM_DEV_NOT_THERE;
763 if (ccbh->target_id < 0 || ccbh->target_id >= ap->ap_pmcount) {
764 ccbh->status = CAM_DEV_NOT_THERE;
768 at += ccbh->target_id;
769 if (ap->ap_type == ATA_PORT_T_PM)
772 if (ccbh->target_lun != CAM_LUN_WILDCARD && ccbh->target_lun) {
773 ccbh->status = CAM_DEV_NOT_THERE;
780 * Switch on the meta XPT command
782 switch(ccbh->func_code) {
785 * This routine is called after a port multiplier has been
788 ccbh->status = CAM_REQ_CMP;
789 ahci_os_lock_port(ap);
790 ahci_port_state_machine(ap, 0);
791 ahci_os_unlock_port(ap);
797 * This command always succeeds, otherwise the bus scan
798 * will not detach dead devices.
800 ccb->cpi.version_num = 1;
801 ccb->cpi.hba_inquiry = 0;
802 ccb->cpi.target_sprt = 0;
803 ccb->cpi.hba_misc = PIM_SEQSCAN;
804 ccb->cpi.hba_eng_cnt = 0;
805 bzero(ccb->cpi.vuhba_flags, sizeof(ccb->cpi.vuhba_flags));
806 ccb->cpi.max_target = AHCI_MAX_PMPORTS - 1;
807 ccb->cpi.max_lun = 0;
808 ccb->cpi.async_flags = 0;
809 ccb->cpi.hpath_id = 0;
810 ccb->cpi.initiator_id = AHCI_MAX_PMPORTS - 1;
811 ccb->cpi.unit_number = cam_sim_unit(sim);
812 ccb->cpi.bus_id = cam_sim_bus(sim);
813 ccb->cpi.base_transfer_speed = 150000;
814 ccb->cpi.transport = XPORT_SATA;
815 ccb->cpi.transport_version = 1;
816 ccb->cpi.protocol = PROTO_SCSI;
817 ccb->cpi.protocol_version = SCSI_REV_2;
819 ccbh->status = CAM_REQ_CMP;
820 if (ccbh->target_id == CAM_TARGET_WILDCARD) {
821 ahci_os_lock_port(ap);
822 ahci_port_state_machine(ap, 0);
823 ahci_os_unlock_port(ap);
825 switch(ahci_pread(ap, AHCI_PREG_SSTS) &
826 AHCI_PREG_SSTS_SPD) {
827 case AHCI_PREG_SSTS_SPD_GEN1:
828 ccb->cpi.base_transfer_speed = 150000;
830 case AHCI_PREG_SSTS_SPD_GEN2:
831 ccb->cpi.base_transfer_speed = 300000;
835 ccb->cpi.base_transfer_speed = 1000;
839 if (ap->ap_type == ATA_PORT_T_NONE)
840 ccbh->status = CAM_DEV_NOT_THERE;
846 ahci_os_lock_port(ap);
847 if (ap->ap_type == ATA_PORT_T_NONE) {
848 ccbh->status = CAM_DEV_NOT_THERE;
850 ahci_port_reset(ap, atx, 0);
851 ccbh->status = CAM_REQ_CMP;
853 ahci_os_unlock_port(ap);
857 ahci_os_lock_port(ap);
858 ahci_port_reset(ap, NULL, 1);
859 ahci_os_unlock_port(ap);
860 ccbh->status = CAM_REQ_CMP;
863 case XPT_SET_TRAN_SETTINGS:
864 ccbh->status = CAM_FUNC_NOTAVAIL;
867 case XPT_GET_TRAN_SETTINGS:
868 ccb->cts.protocol = PROTO_SCSI;
869 ccb->cts.protocol_version = SCSI_REV_2;
870 ccb->cts.transport = XPORT_SATA;
871 ccb->cts.transport_version = XPORT_VERSION_UNSPECIFIED;
872 ccb->cts.proto_specific.valid = 0;
873 ccb->cts.xport_specific.valid = 0;
874 ccbh->status = CAM_REQ_CMP;
877 case XPT_CALC_GEOMETRY:
878 cam_calc_geometry(&ccb->ccg, 1);
883 * Our parallel startup code might have only probed through
884 * to the IDENT, so do the last step if necessary.
886 if (at->at_probe == ATA_PROBE_NEED_IDENT)
887 ahci_cam_probe(ap, atx);
888 if (at->at_probe != ATA_PROBE_GOOD) {
889 ccbh->status = CAM_DEV_NOT_THERE;
893 switch(at->at_type) {
894 case ATA_PORT_T_DISK:
895 ahci_xpt_scsi_disk_io(ap, atx, ccb);
897 case ATA_PORT_T_ATAPI:
898 ahci_xpt_scsi_atapi_io(ap, atx, ccb);
901 ccbh->status = CAM_REQ_INVALID;
907 ccbh->status = CAM_REQ_INVALID;
916 * Generally this function gets called heavily when interrupts might be
917 * non-operational, during a halt/reboot or panic.
921 ahci_xpt_poll(struct cam_sim *sim)
923 struct ahci_port *ap;
925 ap = cam_sim_softc(sim);
927 ahci_os_lock_port(ap);
928 ahci_port_intr(ap, 1);
929 ahci_os_unlock_port(ap);
934 * Convert the SCSI command in ccb to an ata_xfer command in xa
935 * for ATA_PORT_T_DISK operations. Set the completion function
936 * to convert the response back, then dispatch to the OpenBSD AHCI
939 * AHCI DISK commands only support a limited command set, and we
940 * fake additional commands to make it play nice with the CAM subsystem.
944 ahci_xpt_scsi_disk_io(struct ahci_port *ap, struct ata_port *atx,
947 struct ccb_hdr *ccbh;
948 struct ccb_scsiio *csio;
951 struct ata_fis_h2d *fis;
953 union scsi_data *rdata;
959 ccbh = &ccb->csio.ccb_h;
961 at = atx ? atx : &ap->ap_ata[0];
964 * XXX not passing NULL at for direct attach!
966 xa = ahci_ata_get_xfer(ap, atx);
967 rdata = (void *)csio->data_ptr;
968 rdata_len = csio->dxfer_len;
971 * Build the FIS or process the csio to completion.
973 cdb = (void *)((ccbh->flags & CAM_CDB_POINTER) ?
974 csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes);
976 switch(cdb->generic.opcode) {
979 * Auto-sense everything, so explicit sense requests
982 ccbh->status = CAM_SCSI_STATUS_ERROR;
986 * Inquiry supported features
988 * [opcode, byte2, page_code, length, control]
990 if (cdb->inquiry.byte2 & SI_EVPD) {
991 switch(cdb->inquiry.page_code) {
992 case SVPD_SUPPORTED_PAGE_LIST:
993 /* XXX atascsi_disk_vpd_supported */
994 case SVPD_UNIT_SERIAL_NUMBER:
995 /* XXX atascsi_disk_vpd_serial */
996 case SVPD_UNIT_DEVID:
997 /* XXX atascsi_disk_vpd_ident */
999 ccbh->status = CAM_FUNC_NOTAVAIL;
1003 bzero(rdata, rdata_len);
1004 if (rdata_len < SHORT_INQUIRY_LENGTH) {
1005 ccbh->status = CAM_CCB_LEN_ERR;
1008 if (rdata_len > sizeof(rdata->inquiry_data))
1009 rdata_len = sizeof(rdata->inquiry_data);
1010 rdata->inquiry_data.device = T_DIRECT;
1011 rdata->inquiry_data.version = SCSI_REV_SPC2;
1012 rdata->inquiry_data.response_format = 2;
1013 rdata->inquiry_data.additional_length = 32;
1014 bcopy("SATA ", rdata->inquiry_data.vendor, 8);
1015 bcopy(at->at_identify.model,
1016 rdata->inquiry_data.product,
1017 sizeof(rdata->inquiry_data.product));
1018 bcopy(at->at_identify.firmware,
1019 rdata->inquiry_data.revision,
1020 sizeof(rdata->inquiry_data.revision));
1021 ccbh->status = CAM_REQ_CMP;
1024 case READ_CAPACITY_16:
1025 if (cdb->read_capacity_16.service_action != SRC16_SERVICE_ACTION) {
1026 ccbh->status = CAM_REQ_INVALID;
1029 if (rdata_len < sizeof(rdata->read_capacity_data_16)) {
1030 ccbh->status = CAM_CCB_LEN_ERR;
1035 if (rdata_len < sizeof(rdata->read_capacity_data)) {
1036 ccbh->status = CAM_CCB_LEN_ERR;
1040 capacity = at->at_capacity;
1042 bzero(rdata, rdata_len);
1043 if (cdb->generic.opcode == READ_CAPACITY) {
1044 rdata_len = sizeof(rdata->read_capacity_data);
1045 if (capacity > 0xFFFFFFFFU)
1046 capacity = 0xFFFFFFFFU;
1047 bzero(&rdata->read_capacity_data, rdata_len);
1048 scsi_ulto4b((u_int32_t)capacity - 1,
1049 rdata->read_capacity_data.addr);
1050 scsi_ulto4b(512, rdata->read_capacity_data.length);
1052 rdata_len = sizeof(rdata->read_capacity_data_16);
1053 bzero(&rdata->read_capacity_data_16, rdata_len);
1054 scsi_u64to8b(capacity - 1,
1055 rdata->read_capacity_data_16.addr);
1056 scsi_ulto4b(512, rdata->read_capacity_data_16.length);
1058 ccbh->status = CAM_REQ_CMP;
1060 case SYNCHRONIZE_CACHE:
1062 * Synchronize cache. Specification says this can take
1063 * greater then 30 seconds so give it at least 45.
1066 fis->flags = ATA_H2D_FLAGS_CMD;
1067 fis->command = ATA_C_FLUSH_CACHE;
1069 if (xa->timeout < 45000)
1070 xa->timeout = 45000;
1072 xa->flags = ATA_F_READ;
1073 xa->complete = ahci_ata_complete_disk_synchronize_cache;
1075 case TEST_UNIT_READY:
1076 case START_STOP_UNIT:
1079 * Just silently return success
1081 ccbh->status = CAM_REQ_CMP;
1087 * XXX implement pass-through
1089 ccbh->status = CAM_FUNC_NOTAVAIL;
1092 switch(cdb->generic.opcode) {
1094 lba = scsi_3btoul(cdb->rw_6.addr) & 0x1FFFFF;
1095 count = cdb->rw_6.length ? cdb->rw_6.length : 0x100;
1096 xa->flags = ATA_F_READ;
1099 lba = scsi_4btoul(cdb->rw_10.addr);
1100 count = scsi_2btoul(cdb->rw_10.length);
1101 xa->flags = ATA_F_READ;
1104 lba = scsi_4btoul(cdb->rw_12.addr);
1105 count = scsi_4btoul(cdb->rw_12.length);
1106 xa->flags = ATA_F_READ;
1109 lba = scsi_8btou64(cdb->rw_16.addr);
1110 count = scsi_4btoul(cdb->rw_16.length);
1111 xa->flags = ATA_F_READ;
1114 lba = scsi_3btoul(cdb->rw_6.addr) & 0x1FFFFF;
1115 count = cdb->rw_6.length ? cdb->rw_6.length : 0x100;
1116 xa->flags = ATA_F_WRITE;
1119 lba = scsi_4btoul(cdb->rw_10.addr);
1120 count = scsi_2btoul(cdb->rw_10.length);
1121 xa->flags = ATA_F_WRITE;
1124 lba = scsi_4btoul(cdb->rw_12.addr);
1125 count = scsi_4btoul(cdb->rw_12.length);
1126 xa->flags = ATA_F_WRITE;
1129 lba = scsi_8btou64(cdb->rw_16.addr);
1130 count = scsi_4btoul(cdb->rw_16.length);
1131 xa->flags = ATA_F_WRITE;
1134 ccbh->status = CAM_REQ_INVALID;
1137 if (ccbh->status != CAM_REQ_INPROG)
1141 fis->flags = ATA_H2D_FLAGS_CMD;
1142 fis->lba_low = (u_int8_t)lba;
1143 fis->lba_mid = (u_int8_t)(lba >> 8);
1144 fis->lba_high = (u_int8_t)(lba >> 16);
1145 fis->device = ATA_H2D_DEVICE_LBA;
1148 * NCQ only for direct-attached disks, do not currently
1149 * try to use NCQ with port multipliers.
1151 if (at->at_ncqdepth > 1 &&
1152 ap->ap_type == ATA_PORT_T_DISK &&
1153 (ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) &&
1154 (ccbh->flags & CAM_POLLED) == 0) {
1156 * Use NCQ - always uses 48 bit addressing
1158 xa->flags |= ATA_F_NCQ;
1159 fis->command = (xa->flags & ATA_F_WRITE) ?
1160 ATA_C_WRITE_FPDMA : ATA_C_READ_FPDMA;
1161 fis->lba_low_exp = (u_int8_t)(lba >> 24);
1162 fis->lba_mid_exp = (u_int8_t)(lba >> 32);
1163 fis->lba_high_exp = (u_int8_t)(lba >> 40);
1164 fis->sector_count = xa->tag << 3;
1165 fis->features = (u_int8_t)count;
1166 fis->features_exp = (u_int8_t)(count >> 8);
1167 } else if (count > 0x100 || lba > 0xFFFFFFFFU) {
1171 fis->command = (xa->flags & ATA_F_WRITE) ?
1172 ATA_C_WRITEDMA_EXT : ATA_C_READDMA_EXT;
1173 fis->lba_low_exp = (u_int8_t)(lba >> 24);
1174 fis->lba_mid_exp = (u_int8_t)(lba >> 32);
1175 fis->lba_high_exp = (u_int8_t)(lba >> 40);
1176 fis->sector_count = (u_int8_t)count;
1177 fis->sector_count_exp = (u_int8_t)(count >> 8);
1182 * NOTE: 256 sectors is supported, stored as 0.
1184 fis->command = (xa->flags & ATA_F_WRITE) ?
1185 ATA_C_WRITEDMA : ATA_C_READDMA;
1186 fis->device |= (u_int8_t)(lba >> 24) & 0x0F;
1187 fis->sector_count = (u_int8_t)count;
1190 xa->data = csio->data_ptr;
1191 xa->datalen = csio->dxfer_len;
1192 xa->complete = ahci_ata_complete_disk_rw;
1193 xa->timeout = ccbh->timeout; /* milliseconds */
1195 if (xa->timeout > 10000) /* XXX - debug */
1196 xa->timeout = 10000;
1198 if (ccbh->flags & CAM_POLLED)
1199 xa->flags |= ATA_F_POLL;
1204 * If the request is still in progress the xa and FIS have
1205 * been set up and must be dispatched. Otherwise the request
1208 if (ccbh->status == CAM_REQ_INPROG) {
1209 KKASSERT(xa->complete != NULL);
1210 xa->atascsi_private = ccb;
1211 ccb->ccb_h.sim_priv.entries[0].ptr = ap;
1212 ahci_os_lock_port(ap);
1213 fis->flags |= at->at_target;
1215 ahci_os_unlock_port(ap);
1217 ahci_ata_put_xfer(xa);
1223 * Convert the SCSI command in ccb to an ata_xfer command in xa
1224 * for ATA_PORT_T_ATAPI operations. Set the completion function
1225 * to convert the response back, then dispatch to the OpenBSD AHCI
1230 ahci_xpt_scsi_atapi_io(struct ahci_port *ap, struct ata_port *atx,
1233 struct ccb_hdr *ccbh;
1234 struct ccb_scsiio *csio;
1235 struct ata_xfer *xa;
1236 struct ata_fis_h2d *fis;
1240 struct ata_port *at;
1242 ccbh = &ccb->csio.ccb_h;
1244 at = atx ? atx : &ap->ap_ata[0];
1246 switch (ccbh->flags & CAM_DIR_MASK) {
1248 flags = ATA_F_PACKET | ATA_F_READ;
1251 flags = ATA_F_PACKET | ATA_F_WRITE;
1254 flags = ATA_F_PACKET;
1257 ccbh->status = CAM_REQ_INVALID;
1264 * Special handling to get the rfis back into host memory while
1265 * still allowing the Sili chip to run commands in parallel to
1266 * ATAPI devices behind a PM.
1268 flags |= ATA_F_AUTOSENSE;
1271 * The command has to fit in the packet command buffer.
1273 if (csio->cdb_len < 6 || csio->cdb_len > 16) {
1274 ccbh->status = CAM_CCB_LEN_ERR;
1280 * Initialize the XA and FIS.
1282 * XXX not passing NULL at for direct attach!
1284 xa = ahci_ata_get_xfer(ap, atx);
1287 fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
1288 fis->command = ATA_C_PACKET;
1290 fis->sector_count = xa->tag << 3;
1291 fis->features = ATA_H2D_FEATURES_DMA |
1292 ((flags & ATA_F_WRITE) ?
1293 ATA_H2D_FEATURES_DIR_WRITE : ATA_H2D_FEATURES_DIR_READ);
1294 fis->lba_mid = 0x00;
1295 fis->lba_high = 0x20;
1298 xa->data = csio->data_ptr;
1299 xa->datalen = csio->dxfer_len;
1300 xa->timeout = ccbh->timeout; /* milliseconds */
1302 if (ccbh->flags & CAM_POLLED)
1303 xa->flags |= ATA_F_POLL;
1306 * Copy the cdb to the packetcmd buffer in the FIS using a
1307 * convenient pointer in the xa.
1309 cdbs = (void *)((ccbh->flags & CAM_CDB_POINTER) ?
1310 csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes);
1311 bcopy(cdbs, xa->packetcmd, csio->cdb_len);
1314 kprintf("opcode %d cdb_len %d dxfer_len %d\n",
1315 cdbs->generic.opcode,
1316 csio->cdb_len, csio->dxfer_len);
1320 * Some ATAPI commands do not actually follow the SCSI standard.
1322 cdbd = (void *)xa->packetcmd;
1324 switch(cdbd->generic.opcode) {
1327 * Some ATAPI devices can't handle SI_EVPD being set
1328 * for a basic inquiry (page_code == 0).
1330 * Some ATAPI devices can't handle long inquiry lengths,
1331 * don't ask me why. Truncate the inquiry length.
1333 if ((cdbd->inquiry.byte2 & SI_EVPD) &&
1334 cdbd->inquiry.page_code == 0) {
1335 cdbd->inquiry.byte2 &= ~SI_EVPD;
1337 if (cdbd->inquiry.page_code == 0 &&
1338 cdbd->inquiry.length > SHORT_INQUIRY_LENGTH) {
1339 cdbd->inquiry.length = SHORT_INQUIRY_LENGTH;
1345 * Convert *_6 to *_10 commands. Most ATAPI devices
1346 * cannot handle the SCSI READ_6 and WRITE_6 commands.
1348 cdbd->rw_10.opcode |= 0x20;
1349 cdbd->rw_10.byte2 = 0;
1350 cdbd->rw_10.addr[0] = cdbs->rw_6.addr[0] & 0x1F;
1351 cdbd->rw_10.addr[1] = cdbs->rw_6.addr[1];
1352 cdbd->rw_10.addr[2] = cdbs->rw_6.addr[2];
1353 cdbd->rw_10.addr[3] = 0;
1354 cdbd->rw_10.reserved = 0;
1355 cdbd->rw_10.length[0] = 0;
1356 cdbd->rw_10.length[1] = cdbs->rw_6.length;
1357 cdbd->rw_10.control = cdbs->rw_6.control;
1366 xa->complete = ahci_atapi_complete_cmd;
1367 xa->atascsi_private = ccb;
1368 ccb->ccb_h.sim_priv.entries[0].ptr = ap;
1369 ahci_os_lock_port(ap);
1371 ahci_os_unlock_port(ap);
1375 * Completion function for ATA_PORT_T_DISK cache synchronization.
1379 ahci_ata_complete_disk_synchronize_cache(struct ata_xfer *xa)
1381 union ccb *ccb = xa->atascsi_private;
1382 struct ccb_hdr *ccbh = &ccb->ccb_h;
1383 struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
1386 case ATA_S_COMPLETE:
1387 ccbh->status = CAM_REQ_CMP;
1388 ccb->csio.scsi_status = SCSI_STATUS_OK;
1391 kprintf("%s: synchronize_cache: error\n",
1392 ATANAME(ap, xa->at));
1393 ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
1394 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1395 ahci_ata_dummy_sense(&ccb->csio.sense_data);
1398 kprintf("%s: synchronize_cache: timeout\n",
1399 ATANAME(ap, xa->at));
1400 ccbh->status = CAM_CMD_TIMEOUT;
1403 kprintf("%s: synchronize_cache: unknown state %d\n",
1404 ATANAME(ap, xa->at), xa->state);
1405 ccbh->status = CAM_REQ_CMP_ERR;
1408 ahci_ata_put_xfer(xa);
1409 ahci_os_unlock_port(ap);
1411 ahci_os_lock_port(ap);
1415 * Completion function for ATA_PORT_T_DISK I/O
1419 ahci_ata_complete_disk_rw(struct ata_xfer *xa)
1421 union ccb *ccb = xa->atascsi_private;
1422 struct ccb_hdr *ccbh = &ccb->ccb_h;
1423 struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
1426 case ATA_S_COMPLETE:
1427 ccbh->status = CAM_REQ_CMP;
1428 ccb->csio.scsi_status = SCSI_STATUS_OK;
1431 kprintf("%s: disk_rw: error\n", ATANAME(ap, xa->at));
1432 ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
1433 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1434 ahci_ata_dummy_sense(&ccb->csio.sense_data);
1437 kprintf("%s: disk_rw: timeout\n", ATANAME(ap, xa->at));
1438 ccbh->status = CAM_CMD_TIMEOUT;
1439 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1440 ahci_ata_dummy_sense(&ccb->csio.sense_data);
1443 kprintf("%s: disk_rw: unknown state %d\n",
1444 ATANAME(ap, xa->at), xa->state);
1445 ccbh->status = CAM_REQ_CMP_ERR;
1448 ccb->csio.resid = xa->resid;
1449 ahci_ata_put_xfer(xa);
1450 ahci_os_unlock_port(ap);
1452 ahci_os_lock_port(ap);
1456 * Completion function for ATA_PORT_T_ATAPI I/O
1458 * Sense data is returned in the rfis.
1462 ahci_atapi_complete_cmd(struct ata_xfer *xa)
1464 union ccb *ccb = xa->atascsi_private;
1465 struct ccb_hdr *ccbh = &ccb->ccb_h;
1466 struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
1469 cdb = (void *)((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
1470 ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes);
1473 case ATA_S_COMPLETE:
1474 ccbh->status = CAM_REQ_CMP;
1475 ccb->csio.scsi_status = SCSI_STATUS_OK;
1478 ccbh->status = CAM_SCSI_STATUS_ERROR;
1479 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1480 ahci_ata_atapi_sense(&xa->rfis, &ccb->csio.sense_data);
1483 kprintf("%s: cmd %d: timeout\n",
1484 PORTNAME(ap), cdb->generic.opcode);
1485 ccbh->status = CAM_CMD_TIMEOUT;
1486 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1487 ahci_ata_dummy_sense(&ccb->csio.sense_data);
1490 kprintf("%s: cmd %d: unknown state %d\n",
1491 PORTNAME(ap), cdb->generic.opcode, xa->state);
1492 ccbh->status = CAM_REQ_CMP_ERR;
1495 ccb->csio.resid = xa->resid;
1496 ahci_ata_put_xfer(xa);
1497 ahci_os_unlock_port(ap);
1499 ahci_os_lock_port(ap);
1503 * Construct dummy sense data for errors on DISKs
1507 ahci_ata_dummy_sense(struct scsi_sense_data *sense_data)
1509 sense_data->error_code = SSD_ERRCODE_VALID | SSD_CURRENT_ERROR;
1510 sense_data->segment = 0;
1511 sense_data->flags = SSD_KEY_MEDIUM_ERROR;
1512 sense_data->info[0] = 0;
1513 sense_data->info[1] = 0;
1514 sense_data->info[2] = 0;
1515 sense_data->info[3] = 0;
1516 sense_data->extra_len = 0;
1520 * Construct atapi sense data for errors on ATAPI
1522 * The ATAPI sense data is stored in the passed rfis and must be converted
1523 * to SCSI sense data.
1527 ahci_ata_atapi_sense(struct ata_fis_d2h *rfis,
1528 struct scsi_sense_data *sense_data)
1530 sense_data->error_code = SSD_ERRCODE_VALID | SSD_CURRENT_ERROR;
1531 sense_data->segment = 0;
1532 sense_data->flags = (rfis->error & 0xF0) >> 4;
1533 if (rfis->error & 0x04)
1534 sense_data->flags |= SSD_KEY_ILLEGAL_REQUEST;
1535 if (rfis->error & 0x02)
1536 sense_data->flags |= SSD_EOM;
1537 if (rfis->error & 0x01)
1538 sense_data->flags |= SSD_ILI;
1539 sense_data->info[0] = 0;
1540 sense_data->info[1] = 0;
1541 sense_data->info[2] = 0;
1542 sense_data->info[3] = 0;
1543 sense_data->extra_len = 0;