1258223a3SMatthew Dillon /* 2258223a3SMatthew Dillon * Copyright (c) 2009 The DragonFly Project. All rights reserved. 3258223a3SMatthew Dillon * 4258223a3SMatthew Dillon * This code is derived from software contributed to The DragonFly Project 5258223a3SMatthew Dillon * by Matthew Dillon <dillon@backplane.com> 6258223a3SMatthew Dillon * 7258223a3SMatthew Dillon * Redistribution and use in source and binary forms, with or without 8258223a3SMatthew Dillon * modification, are permitted provided that the following conditions 9258223a3SMatthew Dillon * are met: 10258223a3SMatthew Dillon * 11258223a3SMatthew Dillon * 1. Redistributions of source code must retain the above copyright 12258223a3SMatthew Dillon * notice, this list of conditions and the following disclaimer. 13258223a3SMatthew Dillon * 2. Redistributions in binary form must reproduce the above copyright 14258223a3SMatthew Dillon * notice, this list of conditions and the following disclaimer in 15258223a3SMatthew Dillon * the documentation and/or other materials provided with the 16258223a3SMatthew Dillon * distribution. 17258223a3SMatthew Dillon * 3. Neither the name of The DragonFly Project nor the names of its 18258223a3SMatthew Dillon * contributors may be used to endorse or promote products derived 19258223a3SMatthew Dillon * from this software without specific, prior written permission. 20258223a3SMatthew Dillon * 21258223a3SMatthew Dillon * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22258223a3SMatthew Dillon * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23258223a3SMatthew Dillon * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24258223a3SMatthew Dillon * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25258223a3SMatthew Dillon * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26258223a3SMatthew Dillon * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 27258223a3SMatthew Dillon * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28258223a3SMatthew Dillon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29258223a3SMatthew Dillon * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30258223a3SMatthew Dillon * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 31258223a3SMatthew Dillon * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32258223a3SMatthew Dillon * SUCH DAMAGE. 33258223a3SMatthew Dillon * 34258223a3SMatthew Dillon * 35258223a3SMatthew Dillon * Copyright (c) 2007 David Gwynne <dlg@openbsd.org> 36258223a3SMatthew Dillon * 37258223a3SMatthew Dillon * Permission to use, copy, modify, and distribute this software for any 38258223a3SMatthew Dillon * purpose with or without fee is hereby granted, provided that the above 39258223a3SMatthew Dillon * copyright notice and this permission notice appear in all copies. 40258223a3SMatthew Dillon * 41258223a3SMatthew Dillon * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 42258223a3SMatthew Dillon * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 43258223a3SMatthew Dillon * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 44258223a3SMatthew Dillon * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 45258223a3SMatthew Dillon * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 46258223a3SMatthew Dillon * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 47258223a3SMatthew Dillon * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 48258223a3SMatthew Dillon * 49258223a3SMatthew Dillon * $OpenBSD: atascsi.c,v 1.64 2009/02/16 21:19:06 miod Exp $ 50258223a3SMatthew Dillon * $DragonFly$ 51258223a3SMatthew Dillon */ 52258223a3SMatthew Dillon /* 53258223a3SMatthew Dillon * Implement each SATA port as its own SCSI bus on CAM. This way we can 54258223a3SMatthew Dillon * implement future port multiplier features as individual devices on the 55258223a3SMatthew Dillon * bus. 56258223a3SMatthew Dillon * 57258223a3SMatthew Dillon * Much of the cdb<->xa conversion code was taken from OpenBSD, the rest 58258223a3SMatthew Dillon * was written natively for DragonFly. 59258223a3SMatthew Dillon */ 60258223a3SMatthew Dillon 61258223a3SMatthew Dillon #include "ahci.h" 62258223a3SMatthew Dillon 63258223a3SMatthew Dillon static void ahci_xpt_action(struct cam_sim *sim, union ccb *ccb); 64258223a3SMatthew Dillon static void ahci_xpt_poll(struct cam_sim *sim); 651980eff3SMatthew Dillon static void ahci_xpt_scsi_disk_io(struct ahci_port *ap, 661980eff3SMatthew Dillon struct ata_port *at, union ccb *ccb); 671980eff3SMatthew Dillon static void ahci_xpt_scsi_atapi_io(struct ahci_port *ap, 681980eff3SMatthew Dillon struct ata_port *at, union ccb *ccb); 696e0003adSMatthew Dillon static void ahci_xpt_page_inquiry(struct ahci_port *ap, 706e0003adSMatthew Dillon struct ata_port *at, union ccb *ccb); 71258223a3SMatthew Dillon 72258223a3SMatthew Dillon static void ahci_ata_complete_disk_rw(struct ata_xfer *xa); 73258223a3SMatthew Dillon static void ahci_ata_complete_disk_synchronize_cache(struct ata_xfer *xa); 74b4189e5eSMatthew Dillon static void ahci_atapi_complete_cmd(struct ata_xfer *xa); 75b4189e5eSMatthew Dillon static void ahci_ata_dummy_sense(struct scsi_sense_data *sense_data); 767d4fcf34SMatthew Dillon static void ahci_ata_atapi_sense(struct ata_fis_d2h *rfis, 777d4fcf34SMatthew Dillon struct scsi_sense_data *sense_data); 78258223a3SMatthew Dillon 791980eff3SMatthew Dillon static int ahci_cam_probe_disk(struct ahci_port *ap, struct ata_port *at); 801980eff3SMatthew Dillon static int ahci_cam_probe_atapi(struct ahci_port *ap, struct ata_port *at); 81b4189e5eSMatthew Dillon static void ahci_ata_dummy_done(struct ata_xfer *xa); 82258223a3SMatthew Dillon static void ata_fix_identify(struct ata_identify *id); 83258223a3SMatthew Dillon static void ahci_cam_rescan(struct ahci_port *ap); 846e0003adSMatthew Dillon static void ahci_strip_string(const char **basep, int *lenp); 85258223a3SMatthew Dillon 86258223a3SMatthew Dillon int 87258223a3SMatthew Dillon ahci_cam_attach(struct ahci_port *ap) 88258223a3SMatthew Dillon { 89258223a3SMatthew Dillon struct cam_devq *devq; 90258223a3SMatthew Dillon struct cam_sim *sim; 91258223a3SMatthew Dillon int error; 92258223a3SMatthew Dillon int unit; 93258223a3SMatthew Dillon 94cec85a37SMatthew Dillon /* 95cec85a37SMatthew Dillon * We want at least one ccb to be available for error processing 96cec85a37SMatthew Dillon * so don't let CAM use more then ncmds - 1. 97cec85a37SMatthew Dillon */ 98258223a3SMatthew Dillon unit = device_get_unit(ap->ap_sc->sc_dev); 99cec85a37SMatthew Dillon if (ap->ap_sc->sc_ncmds > 1) 100cec85a37SMatthew Dillon devq = cam_simq_alloc(ap->ap_sc->sc_ncmds - 1); 101cec85a37SMatthew Dillon else 102258223a3SMatthew Dillon devq = cam_simq_alloc(ap->ap_sc->sc_ncmds); 103258223a3SMatthew Dillon if (devq == NULL) { 104258223a3SMatthew Dillon return (ENOMEM); 105258223a3SMatthew Dillon } 106258223a3SMatthew Dillon sim = cam_sim_alloc(ahci_xpt_action, ahci_xpt_poll, "ahci", 107258223a3SMatthew Dillon (void *)ap, unit, &sim_mplock, 1, 1, devq); 108258223a3SMatthew Dillon cam_simq_release(devq); 109258223a3SMatthew Dillon if (sim == NULL) { 110258223a3SMatthew Dillon return (ENOMEM); 111258223a3SMatthew Dillon } 112258223a3SMatthew Dillon ap->ap_sim = sim; 113831bc9e3SMatthew Dillon ahci_os_unlock_port(ap); 114258223a3SMatthew Dillon error = xpt_bus_register(ap->ap_sim, ap->ap_num); 115831bc9e3SMatthew Dillon ahci_os_lock_port(ap); 116258223a3SMatthew Dillon if (error != CAM_SUCCESS) { 117258223a3SMatthew Dillon ahci_cam_detach(ap); 118258223a3SMatthew Dillon return (EINVAL); 119258223a3SMatthew Dillon } 120258223a3SMatthew Dillon ap->ap_flags |= AP_F_BUS_REGISTERED; 121258223a3SMatthew Dillon 122c408a8b3SMatthew Dillon if (ap->ap_probe == ATA_PROBE_NEED_IDENT) 1231980eff3SMatthew Dillon error = ahci_cam_probe(ap, NULL); 124c408a8b3SMatthew Dillon else 125c408a8b3SMatthew Dillon error = 0; 126258223a3SMatthew Dillon if (error) { 127258223a3SMatthew Dillon ahci_cam_detach(ap); 128258223a3SMatthew Dillon return (EIO); 129258223a3SMatthew Dillon } 130258223a3SMatthew Dillon ap->ap_flags |= AP_F_CAM_ATTACHED; 131258223a3SMatthew Dillon 132258223a3SMatthew Dillon return(0); 133258223a3SMatthew Dillon } 134258223a3SMatthew Dillon 1351980eff3SMatthew Dillon /* 1363209f581SMatthew Dillon * The state of the port has changed. 1373209f581SMatthew Dillon * 1383209f581SMatthew Dillon * If at is NULL the physical port has changed state. 1393209f581SMatthew Dillon * If at is non-NULL a particular target behind a PM has changed state. 1403209f581SMatthew Dillon * 1413209f581SMatthew Dillon * If found is -1 the target state must be queued to a non-interrupt context. 1423209f581SMatthew Dillon * (only works with at == NULL). 1433209f581SMatthew Dillon * 1443209f581SMatthew Dillon * If found is 0 the target was removed. 1453209f581SMatthew Dillon * If found is 1 the target was inserted. 1461980eff3SMatthew Dillon */ 147258223a3SMatthew Dillon void 1483209f581SMatthew Dillon ahci_cam_changed(struct ahci_port *ap, struct ata_port *atx, int found) 149258223a3SMatthew Dillon { 150fd8bd957SMatthew Dillon struct cam_path *tmppath; 1513209f581SMatthew Dillon int status; 1523209f581SMatthew Dillon int target; 1533209f581SMatthew Dillon 1543209f581SMatthew Dillon target = atx ? atx->at_target : CAM_TARGET_WILDCARD; 155fd8bd957SMatthew Dillon 156258223a3SMatthew Dillon if (ap->ap_sim == NULL) 157258223a3SMatthew Dillon return; 1583209f581SMatthew Dillon if (found == CAM_TARGET_WILDCARD) { 1593209f581SMatthew Dillon status = xpt_create_path(&tmppath, NULL, 1603209f581SMatthew Dillon cam_sim_path(ap->ap_sim), 1613209f581SMatthew Dillon target, CAM_LUN_WILDCARD); 1623209f581SMatthew Dillon if (status != CAM_REQ_CMP) 163fd8bd957SMatthew Dillon return; 164258223a3SMatthew Dillon ahci_cam_rescan(ap); 165fd8bd957SMatthew Dillon } else { 1663209f581SMatthew Dillon status = xpt_create_path(&tmppath, NULL, 1673209f581SMatthew Dillon cam_sim_path(ap->ap_sim), 1683209f581SMatthew Dillon target, 1693209f581SMatthew Dillon CAM_LUN_WILDCARD); 1703209f581SMatthew Dillon if (status != CAM_REQ_CMP) 1713209f581SMatthew Dillon return; 1723209f581SMatthew Dillon #if 0 1733209f581SMatthew Dillon /* 1743209f581SMatthew Dillon * This confuses CAM 1753209f581SMatthew Dillon */ 1763209f581SMatthew Dillon if (found) 1773209f581SMatthew Dillon xpt_async(AC_FOUND_DEVICE, tmppath, NULL); 1783209f581SMatthew Dillon else 179fd8bd957SMatthew Dillon xpt_async(AC_LOST_DEVICE, tmppath, NULL); 1803209f581SMatthew Dillon #endif 181fd8bd957SMatthew Dillon } 182fd8bd957SMatthew Dillon xpt_free_path(tmppath); 183258223a3SMatthew Dillon } 184258223a3SMatthew Dillon 185258223a3SMatthew Dillon void 186258223a3SMatthew Dillon ahci_cam_detach(struct ahci_port *ap) 187258223a3SMatthew Dillon { 188258223a3SMatthew Dillon int error; 189258223a3SMatthew Dillon 190258223a3SMatthew Dillon if ((ap->ap_flags & AP_F_CAM_ATTACHED) == 0) 191258223a3SMatthew Dillon return; 192258223a3SMatthew Dillon get_mplock(); 193258223a3SMatthew Dillon if (ap->ap_sim) { 194258223a3SMatthew Dillon xpt_freeze_simq(ap->ap_sim, 1); 195258223a3SMatthew Dillon } 196258223a3SMatthew Dillon if (ap->ap_flags & AP_F_BUS_REGISTERED) { 197258223a3SMatthew Dillon error = xpt_bus_deregister(cam_sim_path(ap->ap_sim)); 198258223a3SMatthew Dillon KKASSERT(error == CAM_REQ_CMP); 199258223a3SMatthew Dillon ap->ap_flags &= ~AP_F_BUS_REGISTERED; 200258223a3SMatthew Dillon } 201258223a3SMatthew Dillon if (ap->ap_sim) { 202258223a3SMatthew Dillon cam_sim_free(ap->ap_sim); 203258223a3SMatthew Dillon ap->ap_sim = NULL; 204258223a3SMatthew Dillon } 205258223a3SMatthew Dillon rel_mplock(); 206258223a3SMatthew Dillon ap->ap_flags &= ~AP_F_CAM_ATTACHED; 207258223a3SMatthew Dillon } 208258223a3SMatthew Dillon 209258223a3SMatthew Dillon /* 2101980eff3SMatthew Dillon * Once the AHCI port has been attached we need to probe for a device or 211258223a3SMatthew Dillon * devices on the port and setup various options. 2121980eff3SMatthew Dillon * 2131980eff3SMatthew Dillon * If at is NULL we are probing the direct-attached device on the port, 2141980eff3SMatthew Dillon * which may or may not be a port multiplier. 215258223a3SMatthew Dillon */ 2163209f581SMatthew Dillon int 2171980eff3SMatthew Dillon ahci_cam_probe(struct ahci_port *ap, struct ata_port *atx) 218258223a3SMatthew Dillon { 2191980eff3SMatthew Dillon struct ata_port *at; 220258223a3SMatthew Dillon struct ata_xfer *xa; 221258223a3SMatthew Dillon u_int64_t capacity; 222258223a3SMatthew Dillon u_int64_t capacity_bytes; 223258223a3SMatthew Dillon int model_len; 2246e0003adSMatthew Dillon int firmware_len; 2256e0003adSMatthew Dillon int serial_len; 226fd8bd957SMatthew Dillon int error; 227258223a3SMatthew Dillon int devncqdepth; 228258223a3SMatthew Dillon int i; 2296e0003adSMatthew Dillon const char *model_id; 2306e0003adSMatthew Dillon const char *firmware_id; 2316e0003adSMatthew Dillon const char *serial_id; 232669fbbf7SMatthew Dillon const char *wcstr; 233669fbbf7SMatthew Dillon const char *rastr; 234fd8bd957SMatthew Dillon const char *scstr; 235fd8bd957SMatthew Dillon const char *type; 236fd8bd957SMatthew Dillon 2373209f581SMatthew Dillon error = EIO; 2381980eff3SMatthew Dillon 2391980eff3SMatthew Dillon /* 240f4553de1SMatthew Dillon * Delayed CAM attachment for initial probe, sim may be NULL 241f4553de1SMatthew Dillon */ 242f4553de1SMatthew Dillon if (ap->ap_sim == NULL) 243f4553de1SMatthew Dillon return(0); 244f4553de1SMatthew Dillon 245f4553de1SMatthew Dillon /* 2461980eff3SMatthew Dillon * A NULL atx indicates a probe of the directly connected device. 2471980eff3SMatthew Dillon * A non-NULL atx indicates a device connected via a port multiplier. 2481980eff3SMatthew Dillon * We need to preserve atx for calls to ahci_ata_get_xfer(). 2491980eff3SMatthew Dillon * 2501980eff3SMatthew Dillon * at is always non-NULL. For directly connected devices we supply 2511980eff3SMatthew Dillon * an (at) pointing to target 0. 2521980eff3SMatthew Dillon */ 2531980eff3SMatthew Dillon if (atx == NULL) { 254b012a2caSMatthew Dillon at = ap->ap_ata[0]; /* direct attached - device 0 */ 2551980eff3SMatthew Dillon if (ap->ap_type == ATA_PORT_T_PM) { 256831bc9e3SMatthew Dillon kprintf("%s: Found Port Multiplier\n", 257831bc9e3SMatthew Dillon ATANAME(ap, atx)); 2581980eff3SMatthew Dillon return (0); 2591980eff3SMatthew Dillon } 2601980eff3SMatthew Dillon at->at_type = ap->ap_type; 2611980eff3SMatthew Dillon } else { 2623209f581SMatthew Dillon at = atx; 2631980eff3SMatthew Dillon if (atx->at_type == ATA_PORT_T_PM) { 2641980eff3SMatthew Dillon kprintf("%s: Bogus device, reducing port count to %d\n", 2651980eff3SMatthew Dillon ATANAME(ap, atx), atx->at_target); 2661980eff3SMatthew Dillon if (ap->ap_pmcount > atx->at_target) 2671980eff3SMatthew Dillon ap->ap_pmcount = atx->at_target; 2683209f581SMatthew Dillon goto err; 2691980eff3SMatthew Dillon } 2701980eff3SMatthew Dillon } 2713209f581SMatthew Dillon if (ap->ap_type == ATA_PORT_T_NONE) 2723209f581SMatthew Dillon goto err; 2731980eff3SMatthew Dillon if (at->at_type == ATA_PORT_T_NONE) 2743209f581SMatthew Dillon goto err; 275258223a3SMatthew Dillon 276258223a3SMatthew Dillon /* 277258223a3SMatthew Dillon * Issue identify, saving the result 278258223a3SMatthew Dillon */ 2791980eff3SMatthew Dillon xa = ahci_ata_get_xfer(ap, atx); 280258223a3SMatthew Dillon xa->complete = ahci_ata_dummy_done; 2811980eff3SMatthew Dillon xa->data = &at->at_identify; 2821980eff3SMatthew Dillon xa->datalen = sizeof(at->at_identify); 28312feb904SMatthew Dillon xa->flags = ATA_F_READ | ATA_F_PIO | ATA_F_POLL; 2841980eff3SMatthew Dillon xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target; 2851980eff3SMatthew Dillon 2861980eff3SMatthew Dillon switch(at->at_type) { 2871980eff3SMatthew Dillon case ATA_PORT_T_DISK: 288258223a3SMatthew Dillon xa->fis->command = ATA_C_IDENTIFY; 289fd8bd957SMatthew Dillon type = "DISK"; 2901980eff3SMatthew Dillon break; 2911980eff3SMatthew Dillon case ATA_PORT_T_ATAPI: 2921980eff3SMatthew Dillon xa->fis->command = ATA_C_ATAPI_IDENTIFY; 29312feb904SMatthew Dillon xa->flags |= ATA_F_AUTOSENSE; 2941980eff3SMatthew Dillon type = "ATAPI"; 2951980eff3SMatthew Dillon break; 2961980eff3SMatthew Dillon default: 2971980eff3SMatthew Dillon xa->fis->command = ATA_C_ATAPI_IDENTIFY; 2981980eff3SMatthew Dillon type = "UNKNOWN(ATAPI?)"; 2991980eff3SMatthew Dillon break; 300fd8bd957SMatthew Dillon } 301258223a3SMatthew Dillon xa->fis->features = 0; 302258223a3SMatthew Dillon xa->fis->device = 0; 3033209f581SMatthew Dillon xa->timeout = 1000; 304258223a3SMatthew Dillon 305831bc9e3SMatthew Dillon if (ahci_ata_cmd(xa) != ATA_S_COMPLETE) { 306fd8bd957SMatthew Dillon kprintf("%s: Detected %s device but unable to IDENTIFY\n", 3071980eff3SMatthew Dillon ATANAME(ap, atx), type); 308258223a3SMatthew Dillon ahci_ata_put_xfer(xa); 3093209f581SMatthew Dillon goto err; 310258223a3SMatthew Dillon } 311258223a3SMatthew Dillon ahci_ata_put_xfer(xa); 312258223a3SMatthew Dillon 3131980eff3SMatthew Dillon ata_fix_identify(&at->at_identify); 314258223a3SMatthew Dillon 315258223a3SMatthew Dillon /* 316258223a3SMatthew Dillon * Read capacity using SATA probe info. 317258223a3SMatthew Dillon */ 3181980eff3SMatthew Dillon if (le16toh(at->at_identify.cmdset83) & 0x0400) { 319258223a3SMatthew Dillon /* LBA48 feature set supported */ 320258223a3SMatthew Dillon capacity = 0; 321258223a3SMatthew Dillon for (i = 3; i >= 0; --i) { 322258223a3SMatthew Dillon capacity <<= 16; 323258223a3SMatthew Dillon capacity += 3241980eff3SMatthew Dillon le16toh(at->at_identify.addrsecxt[i]); 325258223a3SMatthew Dillon } 326258223a3SMatthew Dillon } else { 3271980eff3SMatthew Dillon capacity = le16toh(at->at_identify.addrsec[1]); 328258223a3SMatthew Dillon capacity <<= 16; 3291980eff3SMatthew Dillon capacity += le16toh(at->at_identify.addrsec[0]); 330258223a3SMatthew Dillon } 33112feb904SMatthew Dillon if (capacity == 0) 33212feb904SMatthew Dillon capacity = 1024 * 1024 / 512; 3331980eff3SMatthew Dillon at->at_capacity = capacity; 3341980eff3SMatthew Dillon if (atx == NULL) 3351980eff3SMatthew Dillon ap->ap_probe = ATA_PROBE_GOOD; 336258223a3SMatthew Dillon 337258223a3SMatthew Dillon capacity_bytes = capacity * 512; 338258223a3SMatthew Dillon 339258223a3SMatthew Dillon /* 340258223a3SMatthew Dillon * Negotiate NCQ, throw away any ata_xfer's beyond the negotiated 341258223a3SMatthew Dillon * number of slots and limit the number of CAM ccb's to one less 342258223a3SMatthew Dillon * so we always have a slot available for recovery. 343258223a3SMatthew Dillon * 344258223a3SMatthew Dillon * NCQ is not used if ap_ncqdepth is 1 or the host controller does 345258223a3SMatthew Dillon * not support it, and in that case the driver can handle extra 346258223a3SMatthew Dillon * ccb's. 347cec85a37SMatthew Dillon * 3481980eff3SMatthew Dillon * NCQ is currently used only with direct-attached disks. It is 3491980eff3SMatthew Dillon * not used with port multipliers or direct-attached ATAPI devices. 3501980eff3SMatthew Dillon * 351cec85a37SMatthew Dillon * Remember at least one extra CCB needs to be reserved for the 352cec85a37SMatthew Dillon * error ccb. 353258223a3SMatthew Dillon */ 354258223a3SMatthew Dillon if ((ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) && 3551980eff3SMatthew Dillon ap->ap_type == ATA_PORT_T_DISK && 3561980eff3SMatthew Dillon (le16toh(at->at_identify.satacap) & (1 << 8))) { 3571980eff3SMatthew Dillon at->at_ncqdepth = (le16toh(at->at_identify.qdepth) & 0x1F) + 1; 3581980eff3SMatthew Dillon devncqdepth = at->at_ncqdepth; 3591980eff3SMatthew Dillon if (at->at_ncqdepth > ap->ap_sc->sc_ncmds) 3601980eff3SMatthew Dillon at->at_ncqdepth = ap->ap_sc->sc_ncmds; 3611980eff3SMatthew Dillon if (at->at_ncqdepth > 1) { 362258223a3SMatthew Dillon for (i = 0; i < ap->ap_sc->sc_ncmds; ++i) { 3631980eff3SMatthew Dillon xa = ahci_ata_get_xfer(ap, atx); 3641980eff3SMatthew Dillon if (xa->tag < at->at_ncqdepth) { 365258223a3SMatthew Dillon xa->state = ATA_S_COMPLETE; 366258223a3SMatthew Dillon ahci_ata_put_xfer(xa); 367258223a3SMatthew Dillon } 368258223a3SMatthew Dillon } 3691980eff3SMatthew Dillon if (at->at_ncqdepth >= ap->ap_sc->sc_ncmds) { 370258223a3SMatthew Dillon cam_devq_resize(ap->ap_sim->devq, 3711980eff3SMatthew Dillon at->at_ncqdepth - 1); 372258223a3SMatthew Dillon } 373cec85a37SMatthew Dillon } 374258223a3SMatthew Dillon } else { 375258223a3SMatthew Dillon devncqdepth = 0; 376258223a3SMatthew Dillon } 377258223a3SMatthew Dillon 3786e0003adSMatthew Dillon model_len = sizeof(at->at_identify.model); 3796e0003adSMatthew Dillon model_id = at->at_identify.model; 3806e0003adSMatthew Dillon ahci_strip_string(&model_id, &model_len); 3816e0003adSMatthew Dillon 3826e0003adSMatthew Dillon firmware_len = sizeof(at->at_identify.firmware); 3836e0003adSMatthew Dillon firmware_id = at->at_identify.firmware; 3846e0003adSMatthew Dillon ahci_strip_string(&firmware_id, &firmware_len); 3856e0003adSMatthew Dillon 3866e0003adSMatthew Dillon serial_len = sizeof(at->at_identify.serial); 3876e0003adSMatthew Dillon serial_id = at->at_identify.serial; 3886e0003adSMatthew Dillon ahci_strip_string(&serial_id, &serial_len); 389669fbbf7SMatthew Dillon 390fd8bd957SMatthew Dillon /* 391fd8bd957SMatthew Dillon * Generate informatiive strings. 392fd8bd957SMatthew Dillon * 393fd8bd957SMatthew Dillon * NOTE: We do not automatically set write caching, lookahead, 394fd8bd957SMatthew Dillon * or the security state for ATAPI devices. 395fd8bd957SMatthew Dillon */ 3961980eff3SMatthew Dillon if (at->at_identify.cmdset82 & ATA_IDENTIFY_WRITECACHE) { 3971980eff3SMatthew Dillon if (at->at_identify.features85 & ATA_IDENTIFY_WRITECACHE) 398669fbbf7SMatthew Dillon wcstr = "enabled"; 3991980eff3SMatthew Dillon else if (at->at_type == ATA_PORT_T_ATAPI) 400fd8bd957SMatthew Dillon wcstr = "disabled"; 401669fbbf7SMatthew Dillon else 402669fbbf7SMatthew Dillon wcstr = "enabling"; 403669fbbf7SMatthew Dillon } else { 404669fbbf7SMatthew Dillon wcstr = "notsupp"; 405669fbbf7SMatthew Dillon } 406669fbbf7SMatthew Dillon 4071980eff3SMatthew Dillon if (at->at_identify.cmdset82 & ATA_IDENTIFY_LOOKAHEAD) { 4081980eff3SMatthew Dillon if (at->at_identify.features85 & ATA_IDENTIFY_LOOKAHEAD) 409669fbbf7SMatthew Dillon rastr = "enabled"; 4101980eff3SMatthew Dillon else if (at->at_type == ATA_PORT_T_ATAPI) 411fd8bd957SMatthew Dillon rastr = "disabled"; 412669fbbf7SMatthew Dillon else 413669fbbf7SMatthew Dillon rastr = "enabling"; 414669fbbf7SMatthew Dillon } else { 415669fbbf7SMatthew Dillon rastr = "notsupp"; 416669fbbf7SMatthew Dillon } 417669fbbf7SMatthew Dillon 4181980eff3SMatthew Dillon if (at->at_identify.cmdset82 & ATA_IDENTIFY_SECURITY) { 4191980eff3SMatthew Dillon if (at->at_identify.securestatus & ATA_SECURE_FROZEN) 420fd8bd957SMatthew Dillon scstr = "frozen"; 4211980eff3SMatthew Dillon else if (at->at_type == ATA_PORT_T_ATAPI) 422fd8bd957SMatthew Dillon scstr = "unfrozen"; 423afa796d2SMatthew Dillon else if (AhciNoFeatures & (1 << ap->ap_num)) 424afa796d2SMatthew Dillon scstr = "<disabled>"; 425fd8bd957SMatthew Dillon else 426fd8bd957SMatthew Dillon scstr = "freezing"; 427fd8bd957SMatthew Dillon } else { 428fd8bd957SMatthew Dillon scstr = "notsupp"; 429fd8bd957SMatthew Dillon } 430fd8bd957SMatthew Dillon 4316e0003adSMatthew Dillon kprintf("%s: Found %s \"%*.*s %*.*s\" serial=\"%*.*s\"\n" 432074579dfSMatthew Dillon "%s: tags=%d/%d satacap=%04x satafea=%04x NCQ=%s " 433074579dfSMatthew Dillon "capacity=%lld.%02dMB\n", 434074579dfSMatthew Dillon 4351980eff3SMatthew Dillon ATANAME(ap, atx), 436fd8bd957SMatthew Dillon type, 4376e0003adSMatthew Dillon model_len, model_len, model_id, 4386e0003adSMatthew Dillon firmware_len, firmware_len, firmware_id, 4396e0003adSMatthew Dillon serial_len, serial_len, serial_id, 440258223a3SMatthew Dillon 4411980eff3SMatthew Dillon ATANAME(ap, atx), 442258223a3SMatthew Dillon devncqdepth, ap->ap_sc->sc_ncmds, 4431980eff3SMatthew Dillon at->at_identify.satacap, 4441980eff3SMatthew Dillon at->at_identify.satafsup, 445074579dfSMatthew Dillon (at->at_ncqdepth > 1 ? "YES" : "NO"), 446258223a3SMatthew Dillon (long long)capacity_bytes / (1024 * 1024), 447074579dfSMatthew Dillon (int)(capacity_bytes % (1024 * 1024)) * 100 / (1024 * 1024) 448074579dfSMatthew Dillon ); 449074579dfSMatthew Dillon kprintf("%s: f85=%04x f86=%04x f87=%04x WC=%s RA=%s SEC=%s\n", 4501980eff3SMatthew Dillon ATANAME(ap, atx), 4511980eff3SMatthew Dillon at->at_identify.features85, 4521980eff3SMatthew Dillon at->at_identify.features86, 4531980eff3SMatthew Dillon at->at_identify.features87, 454669fbbf7SMatthew Dillon wcstr, 455fd8bd957SMatthew Dillon rastr, 456fd8bd957SMatthew Dillon scstr 457258223a3SMatthew Dillon ); 458258223a3SMatthew Dillon 459258223a3SMatthew Dillon /* 460fd8bd957SMatthew Dillon * Additional type-specific probing 461fd8bd957SMatthew Dillon */ 4621980eff3SMatthew Dillon switch(at->at_type) { 463fd8bd957SMatthew Dillon case ATA_PORT_T_DISK: 4641980eff3SMatthew Dillon error = ahci_cam_probe_disk(ap, atx); 4651980eff3SMatthew Dillon break; 4661980eff3SMatthew Dillon case ATA_PORT_T_ATAPI: 4671980eff3SMatthew Dillon error = ahci_cam_probe_atapi(ap, atx); 468fd8bd957SMatthew Dillon break; 469fd8bd957SMatthew Dillon default: 4701980eff3SMatthew Dillon error = EIO; 471fd8bd957SMatthew Dillon break; 472fd8bd957SMatthew Dillon } 4733209f581SMatthew Dillon err: 4743209f581SMatthew Dillon if (error) { 4753209f581SMatthew Dillon at->at_probe = ATA_PROBE_FAILED; 4763209f581SMatthew Dillon if (atx == NULL) 4773209f581SMatthew Dillon ap->ap_probe = at->at_probe; 4783209f581SMatthew Dillon } else { 4793209f581SMatthew Dillon at->at_probe = ATA_PROBE_GOOD; 4803209f581SMatthew Dillon if (atx == NULL) 4813209f581SMatthew Dillon ap->ap_probe = at->at_probe; 4823209f581SMatthew Dillon } 4833209f581SMatthew Dillon return (error); 484fd8bd957SMatthew Dillon } 485fd8bd957SMatthew Dillon 486fd8bd957SMatthew Dillon /* 487fd8bd957SMatthew Dillon * DISK-specific probe after initial ident 488fd8bd957SMatthew Dillon */ 489fd8bd957SMatthew Dillon static int 4901980eff3SMatthew Dillon ahci_cam_probe_disk(struct ahci_port *ap, struct ata_port *atx) 491fd8bd957SMatthew Dillon { 4921980eff3SMatthew Dillon struct ata_port *at; 493fd8bd957SMatthew Dillon struct ata_xfer *xa; 494fd8bd957SMatthew Dillon 495b012a2caSMatthew Dillon at = atx ? atx : ap->ap_ata[0]; 4961980eff3SMatthew Dillon 497fd8bd957SMatthew Dillon /* 498258223a3SMatthew Dillon * Enable write cache if supported 499fd8bd957SMatthew Dillon * 500fd8bd957SMatthew Dillon * NOTE: "WD My Book" external disk devices have a very poor 501fd8bd957SMatthew Dillon * daughter board between the the ESATA and the HD. Sending 502fd8bd957SMatthew Dillon * any ATA_C_SET_FEATURES commands will break the hardware port 503fd8bd957SMatthew Dillon * with a fatal protocol error. However, this device also 504fd8bd957SMatthew Dillon * indicates that WRITECACHE is already on and READAHEAD is 505fd8bd957SMatthew Dillon * not supported so we avoid the issue. 506258223a3SMatthew Dillon */ 5071980eff3SMatthew Dillon if ((at->at_identify.cmdset82 & ATA_IDENTIFY_WRITECACHE) && 5081980eff3SMatthew Dillon (at->at_identify.features85 & ATA_IDENTIFY_WRITECACHE) == 0) { 5091980eff3SMatthew Dillon xa = ahci_ata_get_xfer(ap, atx); 510258223a3SMatthew Dillon xa->complete = ahci_ata_dummy_done; 511258223a3SMatthew Dillon xa->fis->command = ATA_C_SET_FEATURES; 512b012a2caSMatthew Dillon xa->fis->features = ATA_SF_WRITECACHE_EN; 513b012a2caSMatthew Dillon /* xa->fis->features = ATA_SF_LOOKAHEAD_EN; */ 5141980eff3SMatthew Dillon xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target; 515669fbbf7SMatthew Dillon xa->fis->device = 0; 516b012a2caSMatthew Dillon xa->flags = ATA_F_PIO | ATA_F_POLL; 5173209f581SMatthew Dillon xa->timeout = 1000; 518669fbbf7SMatthew Dillon xa->datalen = 0; 519831bc9e3SMatthew Dillon if (ahci_ata_cmd(xa) == ATA_S_COMPLETE) 5201980eff3SMatthew Dillon at->at_features |= ATA_PORT_F_WCACHE; 521afa796d2SMatthew Dillon else 522afa796d2SMatthew Dillon kprintf("%s: Unable to enable write-caching\n", 523afa796d2SMatthew Dillon ATANAME(ap, atx)); 524258223a3SMatthew Dillon ahci_ata_put_xfer(xa); 525258223a3SMatthew Dillon } 526258223a3SMatthew Dillon 527258223a3SMatthew Dillon /* 528258223a3SMatthew Dillon * Enable readahead if supported 529258223a3SMatthew Dillon */ 5301980eff3SMatthew Dillon if ((at->at_identify.cmdset82 & ATA_IDENTIFY_LOOKAHEAD) && 5311980eff3SMatthew Dillon (at->at_identify.features85 & ATA_IDENTIFY_LOOKAHEAD) == 0) { 5321980eff3SMatthew Dillon xa = ahci_ata_get_xfer(ap, atx); 533258223a3SMatthew Dillon xa->complete = ahci_ata_dummy_done; 534258223a3SMatthew Dillon xa->fis->command = ATA_C_SET_FEATURES; 535258223a3SMatthew Dillon xa->fis->features = ATA_SF_LOOKAHEAD_EN; 5361980eff3SMatthew Dillon xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target; 537669fbbf7SMatthew Dillon xa->fis->device = 0; 538b012a2caSMatthew Dillon xa->flags = ATA_F_PIO | ATA_F_POLL; 5393209f581SMatthew Dillon xa->timeout = 1000; 540669fbbf7SMatthew Dillon xa->datalen = 0; 541831bc9e3SMatthew Dillon if (ahci_ata_cmd(xa) == ATA_S_COMPLETE) 5421980eff3SMatthew Dillon at->at_features |= ATA_PORT_F_RAHEAD; 543afa796d2SMatthew Dillon else 544afa796d2SMatthew Dillon kprintf("%s: Unable to enable read-ahead\n", 545afa796d2SMatthew Dillon ATANAME(ap, atx)); 546258223a3SMatthew Dillon ahci_ata_put_xfer(xa); 547258223a3SMatthew Dillon } 548258223a3SMatthew Dillon 549258223a3SMatthew Dillon /* 550258223a3SMatthew Dillon * FREEZE LOCK the device so malicious users can't lock it on us. 551258223a3SMatthew Dillon * As there is no harm in issuing this to devices that don't 552258223a3SMatthew Dillon * support the security feature set we just send it, and don't bother 553258223a3SMatthew Dillon * checking if the device sends a command abort to tell us it doesn't 554258223a3SMatthew Dillon * support it 555258223a3SMatthew Dillon */ 5561980eff3SMatthew Dillon if ((at->at_identify.cmdset82 & ATA_IDENTIFY_SECURITY) && 557afa796d2SMatthew Dillon (at->at_identify.securestatus & ATA_SECURE_FROZEN) == 0 && 558afa796d2SMatthew Dillon (AhciNoFeatures & (1 << ap->ap_num)) == 0) { 5591980eff3SMatthew Dillon xa = ahci_ata_get_xfer(ap, atx); 560258223a3SMatthew Dillon xa->complete = ahci_ata_dummy_done; 561258223a3SMatthew Dillon xa->fis->command = ATA_C_SEC_FREEZE_LOCK; 5621980eff3SMatthew Dillon xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target; 563b012a2caSMatthew Dillon xa->flags = ATA_F_PIO | ATA_F_POLL; 5643209f581SMatthew Dillon xa->timeout = 1000; 565669fbbf7SMatthew Dillon xa->datalen = 0; 566831bc9e3SMatthew Dillon if (ahci_ata_cmd(xa) == ATA_S_COMPLETE) 5671980eff3SMatthew Dillon at->at_features |= ATA_PORT_F_FRZLCK; 568afa796d2SMatthew Dillon else 569afa796d2SMatthew Dillon kprintf("%s: Unable to set security freeze\n", 570afa796d2SMatthew Dillon ATANAME(ap, atx)); 571258223a3SMatthew Dillon ahci_ata_put_xfer(xa); 572669fbbf7SMatthew Dillon } 573258223a3SMatthew Dillon 574b4189e5eSMatthew Dillon return (0); 575b4189e5eSMatthew Dillon } 576b4189e5eSMatthew Dillon 577fd8bd957SMatthew Dillon /* 578fd8bd957SMatthew Dillon * ATAPI-specific probe after initial ident 579fd8bd957SMatthew Dillon */ 580b4189e5eSMatthew Dillon static int 5811980eff3SMatthew Dillon ahci_cam_probe_atapi(struct ahci_port *ap, struct ata_port *atx) 582b4189e5eSMatthew Dillon { 583fd8bd957SMatthew Dillon return(0); 584fd8bd957SMatthew Dillon } 585fd8bd957SMatthew Dillon 586b4189e5eSMatthew Dillon /* 587b4189e5eSMatthew Dillon * Fix byte ordering so buffers can be accessed as 588b4189e5eSMatthew Dillon * strings. 589b4189e5eSMatthew Dillon */ 590258223a3SMatthew Dillon static void 591258223a3SMatthew Dillon ata_fix_identify(struct ata_identify *id) 592258223a3SMatthew Dillon { 593258223a3SMatthew Dillon u_int16_t *swap; 594258223a3SMatthew Dillon int i; 595258223a3SMatthew Dillon 596258223a3SMatthew Dillon swap = (u_int16_t *)id->serial; 597258223a3SMatthew Dillon for (i = 0; i < sizeof(id->serial) / sizeof(u_int16_t); i++) 598258223a3SMatthew Dillon swap[i] = bswap16(swap[i]); 599258223a3SMatthew Dillon 600258223a3SMatthew Dillon swap = (u_int16_t *)id->firmware; 601258223a3SMatthew Dillon for (i = 0; i < sizeof(id->firmware) / sizeof(u_int16_t); i++) 602258223a3SMatthew Dillon swap[i] = bswap16(swap[i]); 603258223a3SMatthew Dillon 604258223a3SMatthew Dillon swap = (u_int16_t *)id->model; 605258223a3SMatthew Dillon for (i = 0; i < sizeof(id->model) / sizeof(u_int16_t); i++) 606258223a3SMatthew Dillon swap[i] = bswap16(swap[i]); 607258223a3SMatthew Dillon } 608258223a3SMatthew Dillon 609258223a3SMatthew Dillon /* 610b4189e5eSMatthew Dillon * Dummy done callback for xa. 611b4189e5eSMatthew Dillon */ 612b4189e5eSMatthew Dillon static void 613b4189e5eSMatthew Dillon ahci_ata_dummy_done(struct ata_xfer *xa) 614b4189e5eSMatthew Dillon { 615b4189e5eSMatthew Dillon } 616b4189e5eSMatthew Dillon 617b4189e5eSMatthew Dillon /* 6183209f581SMatthew Dillon * Use an engineering request to initiate a target scan for devices 6193209f581SMatthew Dillon * behind a port multiplier. 620fd8bd957SMatthew Dillon * 6213209f581SMatthew Dillon * An asynchronous bus scan is used to avoid reentrancy issues. 622258223a3SMatthew Dillon */ 623258223a3SMatthew Dillon static void 624258223a3SMatthew Dillon ahci_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb) 625258223a3SMatthew Dillon { 6263209f581SMatthew Dillon struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr; 6273209f581SMatthew Dillon 628f4553de1SMatthew Dillon if (ccb->ccb_h.func_code == XPT_SCAN_BUS) { 6293209f581SMatthew Dillon ap->ap_flags &= ~AP_F_SCAN_RUNNING; 6303209f581SMatthew Dillon if (ap->ap_flags & AP_F_SCAN_REQUESTED) { 6313209f581SMatthew Dillon ap->ap_flags &= ~AP_F_SCAN_REQUESTED; 6323209f581SMatthew Dillon ahci_cam_rescan(ap); 6333209f581SMatthew Dillon } 634f4553de1SMatthew Dillon ap->ap_flags |= AP_F_SCAN_COMPLETED; 635f4553de1SMatthew Dillon wakeup(&ap->ap_flags); 636f4553de1SMatthew Dillon } 637f4553de1SMatthew Dillon xpt_free_ccb(ccb); 638258223a3SMatthew Dillon } 639258223a3SMatthew Dillon 640258223a3SMatthew Dillon static void 641258223a3SMatthew Dillon ahci_cam_rescan(struct ahci_port *ap) 642258223a3SMatthew Dillon { 643258223a3SMatthew Dillon struct cam_path *path; 644258223a3SMatthew Dillon union ccb *ccb; 645258223a3SMatthew Dillon int status; 6463209f581SMatthew Dillon int i; 6473209f581SMatthew Dillon 6483209f581SMatthew Dillon if (ap->ap_flags & AP_F_SCAN_RUNNING) { 6493209f581SMatthew Dillon ap->ap_flags |= AP_F_SCAN_REQUESTED; 6503209f581SMatthew Dillon return; 6513209f581SMatthew Dillon } 6523209f581SMatthew Dillon ap->ap_flags |= AP_F_SCAN_RUNNING; 6533209f581SMatthew Dillon for (i = 0; i < AHCI_MAX_PMPORTS; ++i) { 654b012a2caSMatthew Dillon ap->ap_ata[i]->at_features |= ATA_PORT_F_RESCAN; 6553209f581SMatthew Dillon } 656258223a3SMatthew Dillon 657258223a3SMatthew Dillon status = xpt_create_path(&path, xpt_periph, cam_sim_path(ap->ap_sim), 658258223a3SMatthew Dillon CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); 659258223a3SMatthew Dillon if (status != CAM_REQ_CMP) 660258223a3SMatthew Dillon return; 661258223a3SMatthew Dillon 662f4553de1SMatthew Dillon ccb = xpt_alloc_ccb(); 663258223a3SMatthew Dillon xpt_setup_ccb(&ccb->ccb_h, path, 5); /* 5 = low priority */ 6642de5e9baSMatthew Dillon ccb->ccb_h.func_code = XPT_ENG_EXEC; 665258223a3SMatthew Dillon ccb->ccb_h.cbfcnp = ahci_cam_rescan_callback; 6663209f581SMatthew Dillon ccb->ccb_h.sim_priv.entries[0].ptr = ap; 667258223a3SMatthew Dillon ccb->crcn.flags = CAM_FLAG_NONE; 6682de5e9baSMatthew Dillon xpt_action_async(ccb); 669258223a3SMatthew Dillon } 670258223a3SMatthew Dillon 6713209f581SMatthew Dillon static void 6723209f581SMatthew Dillon ahci_xpt_rescan(struct ahci_port *ap) 6733209f581SMatthew Dillon { 6743209f581SMatthew Dillon struct cam_path *path; 6753209f581SMatthew Dillon union ccb *ccb; 6763209f581SMatthew Dillon int status; 6773209f581SMatthew Dillon 6783209f581SMatthew Dillon status = xpt_create_path(&path, xpt_periph, cam_sim_path(ap->ap_sim), 6793209f581SMatthew Dillon CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); 6803209f581SMatthew Dillon if (status != CAM_REQ_CMP) 6813209f581SMatthew Dillon return; 682f4553de1SMatthew Dillon 683f4553de1SMatthew Dillon ccb = xpt_alloc_ccb(); 6843209f581SMatthew Dillon xpt_setup_ccb(&ccb->ccb_h, path, 5); /* 5 = low priority */ 6852de5e9baSMatthew Dillon ccb->ccb_h.func_code = XPT_SCAN_BUS; 6863209f581SMatthew Dillon ccb->ccb_h.cbfcnp = ahci_cam_rescan_callback; 6873209f581SMatthew Dillon ccb->ccb_h.sim_priv.entries[0].ptr = ap; 6883209f581SMatthew Dillon ccb->crcn.flags = CAM_FLAG_NONE; 689baef7501SMatthew Dillon xpt_action_async(ccb); 6903209f581SMatthew Dillon } 6913209f581SMatthew Dillon 692258223a3SMatthew Dillon /* 693258223a3SMatthew Dillon * Action function - dispatch command 694258223a3SMatthew Dillon */ 695258223a3SMatthew Dillon static 696258223a3SMatthew Dillon void 697258223a3SMatthew Dillon ahci_xpt_action(struct cam_sim *sim, union ccb *ccb) 698258223a3SMatthew Dillon { 699258223a3SMatthew Dillon struct ahci_port *ap; 7001980eff3SMatthew Dillon struct ata_port *at, *atx; 701258223a3SMatthew Dillon struct ccb_hdr *ccbh; 702258223a3SMatthew Dillon int unit; 703258223a3SMatthew Dillon 704258223a3SMatthew Dillon /* XXX lock */ 705258223a3SMatthew Dillon ap = cam_sim_softc(sim); 7061980eff3SMatthew Dillon atx = NULL; 707258223a3SMatthew Dillon KKASSERT(ap != NULL); 708258223a3SMatthew Dillon ccbh = &ccb->ccb_h; 709258223a3SMatthew Dillon unit = cam_sim_unit(sim); 710258223a3SMatthew Dillon 711258223a3SMatthew Dillon /* 7123209f581SMatthew Dillon * Early failure checks. These checks do not apply to XPT_PATH_INQ, 7133209f581SMatthew Dillon * otherwise the bus rescan will not remove the dead devices when 7143209f581SMatthew Dillon * unplugging a PM. 7153209f581SMatthew Dillon * 7161980eff3SMatthew Dillon * For non-wildcards we have one target (0) and one lun (0), 7171980eff3SMatthew Dillon * unless we have a port multiplier. 7181980eff3SMatthew Dillon * 7191980eff3SMatthew Dillon * A wildcard target indicates only the general bus is being 7201980eff3SMatthew Dillon * probed. 7211980eff3SMatthew Dillon * 7221980eff3SMatthew Dillon * Calculate at and atx. at is always non-NULL. atx is only 7231980eff3SMatthew Dillon * non-NULL for direct-attached devices. It will be NULL for 7241980eff3SMatthew Dillon * devices behind a port multiplier. 725258223a3SMatthew Dillon * 726258223a3SMatthew Dillon * XXX What do we do with a LUN wildcard? 727258223a3SMatthew Dillon */ 7283209f581SMatthew Dillon if (ccbh->target_id != CAM_TARGET_WILDCARD && 7293209f581SMatthew Dillon ccbh->func_code != XPT_PATH_INQ) { 7301980eff3SMatthew Dillon if (ap->ap_type == ATA_PORT_T_NONE) { 7313209f581SMatthew Dillon ccbh->status = CAM_DEV_NOT_THERE; 732258223a3SMatthew Dillon xpt_done(ccb); 733258223a3SMatthew Dillon return; 734258223a3SMatthew Dillon } 7351980eff3SMatthew Dillon if (ccbh->target_id < 0 || ccbh->target_id >= ap->ap_pmcount) { 736258223a3SMatthew Dillon ccbh->status = CAM_DEV_NOT_THERE; 737258223a3SMatthew Dillon xpt_done(ccb); 738258223a3SMatthew Dillon return; 739258223a3SMatthew Dillon } 740b012a2caSMatthew Dillon at = ap->ap_ata[ccbh->target_id]; 7411980eff3SMatthew Dillon if (ap->ap_type == ATA_PORT_T_PM) 7421980eff3SMatthew Dillon atx = at; 7431980eff3SMatthew Dillon 744258223a3SMatthew Dillon if (ccbh->target_lun != CAM_LUN_WILDCARD && ccbh->target_lun) { 745258223a3SMatthew Dillon ccbh->status = CAM_DEV_NOT_THERE; 746258223a3SMatthew Dillon xpt_done(ccb); 747258223a3SMatthew Dillon return; 748258223a3SMatthew Dillon } 749b012a2caSMatthew Dillon } else { 750b012a2caSMatthew Dillon at = ap->ap_ata[0]; 751258223a3SMatthew Dillon } 752258223a3SMatthew Dillon 753258223a3SMatthew Dillon /* 754258223a3SMatthew Dillon * Switch on the meta XPT command 755258223a3SMatthew Dillon */ 756258223a3SMatthew Dillon switch(ccbh->func_code) { 7573209f581SMatthew Dillon case XPT_ENG_EXEC: 7583209f581SMatthew Dillon /* 7593209f581SMatthew Dillon * This routine is called after a port multiplier has been 7603209f581SMatthew Dillon * probed. 7613209f581SMatthew Dillon */ 7623209f581SMatthew Dillon ccbh->status = CAM_REQ_CMP; 763f4553de1SMatthew Dillon ahci_os_lock_port(ap); 764831bc9e3SMatthew Dillon ahci_port_state_machine(ap, 0); 765f4553de1SMatthew Dillon ahci_os_unlock_port(ap); 7663209f581SMatthew Dillon xpt_done(ccb); 7673209f581SMatthew Dillon ahci_xpt_rescan(ap); 7683209f581SMatthew Dillon break; 769258223a3SMatthew Dillon case XPT_PATH_INQ: 7703209f581SMatthew Dillon /* 7713209f581SMatthew Dillon * This command always succeeds, otherwise the bus scan 7723209f581SMatthew Dillon * will not detach dead devices. 7733209f581SMatthew Dillon */ 774258223a3SMatthew Dillon ccb->cpi.version_num = 1; 775258223a3SMatthew Dillon ccb->cpi.hba_inquiry = 0; 776258223a3SMatthew Dillon ccb->cpi.target_sprt = 0; 7771980eff3SMatthew Dillon ccb->cpi.hba_misc = PIM_SEQSCAN; 778258223a3SMatthew Dillon ccb->cpi.hba_eng_cnt = 0; 779258223a3SMatthew Dillon bzero(ccb->cpi.vuhba_flags, sizeof(ccb->cpi.vuhba_flags)); 78076497a9cSMatthew Dillon ccb->cpi.max_target = AHCI_MAX_PMPORTS - 1; 781258223a3SMatthew Dillon ccb->cpi.max_lun = 0; 782258223a3SMatthew Dillon ccb->cpi.async_flags = 0; 783258223a3SMatthew Dillon ccb->cpi.hpath_id = 0; 7841980eff3SMatthew Dillon ccb->cpi.initiator_id = AHCI_MAX_PMPORTS - 1; 785258223a3SMatthew Dillon ccb->cpi.unit_number = cam_sim_unit(sim); 786258223a3SMatthew Dillon ccb->cpi.bus_id = cam_sim_bus(sim); 787258223a3SMatthew Dillon ccb->cpi.base_transfer_speed = 150000; 7882cc2e845SMatthew Dillon ccb->cpi.transport = XPORT_SATA; 789258223a3SMatthew Dillon ccb->cpi.transport_version = 1; 790258223a3SMatthew Dillon ccb->cpi.protocol = PROTO_SCSI; 791258223a3SMatthew Dillon ccb->cpi.protocol_version = SCSI_REV_2; 792258223a3SMatthew Dillon 7933209f581SMatthew Dillon ccbh->status = CAM_REQ_CMP; 794831bc9e3SMatthew Dillon if (ccbh->target_id == CAM_TARGET_WILDCARD) { 795831bc9e3SMatthew Dillon ahci_os_lock_port(ap); 796831bc9e3SMatthew Dillon ahci_port_state_machine(ap, 0); 797831bc9e3SMatthew Dillon ahci_os_unlock_port(ap); 798831bc9e3SMatthew Dillon } else { 799258223a3SMatthew Dillon switch(ahci_pread(ap, AHCI_PREG_SSTS) & 800258223a3SMatthew Dillon AHCI_PREG_SSTS_SPD) { 801258223a3SMatthew Dillon case AHCI_PREG_SSTS_SPD_GEN1: 802258223a3SMatthew Dillon ccb->cpi.base_transfer_speed = 150000; 803258223a3SMatthew Dillon break; 804258223a3SMatthew Dillon case AHCI_PREG_SSTS_SPD_GEN2: 805258223a3SMatthew Dillon ccb->cpi.base_transfer_speed = 300000; 806258223a3SMatthew Dillon break; 807258223a3SMatthew Dillon default: 808258223a3SMatthew Dillon /* unknown */ 809258223a3SMatthew Dillon ccb->cpi.base_transfer_speed = 1000; 810258223a3SMatthew Dillon break; 811258223a3SMatthew Dillon } 8123209f581SMatthew Dillon #if 0 8131980eff3SMatthew Dillon if (ap->ap_type == ATA_PORT_T_NONE) 8141980eff3SMatthew Dillon ccbh->status = CAM_DEV_NOT_THERE; 8153209f581SMatthew Dillon #endif 8161980eff3SMatthew Dillon } 817258223a3SMatthew Dillon xpt_done(ccb); 818258223a3SMatthew Dillon break; 819258223a3SMatthew Dillon case XPT_RESET_DEV: 820f4553de1SMatthew Dillon ahci_os_lock_port(ap); 8211980eff3SMatthew Dillon if (ap->ap_type == ATA_PORT_T_NONE) { 8223209f581SMatthew Dillon ccbh->status = CAM_DEV_NOT_THERE; 8231980eff3SMatthew Dillon } else { 8241980eff3SMatthew Dillon ahci_port_reset(ap, atx, 0); 825fd8bd957SMatthew Dillon ccbh->status = CAM_REQ_CMP; 8261980eff3SMatthew Dillon } 827f4553de1SMatthew Dillon ahci_os_unlock_port(ap); 828258223a3SMatthew Dillon xpt_done(ccb); 829258223a3SMatthew Dillon break; 830258223a3SMatthew Dillon case XPT_RESET_BUS: 831f4553de1SMatthew Dillon ahci_os_lock_port(ap); 8321980eff3SMatthew Dillon ahci_port_reset(ap, NULL, 1); 833f4553de1SMatthew Dillon ahci_os_unlock_port(ap); 834fd8bd957SMatthew Dillon ccbh->status = CAM_REQ_CMP; 835258223a3SMatthew Dillon xpt_done(ccb); 836258223a3SMatthew Dillon break; 837258223a3SMatthew Dillon case XPT_SET_TRAN_SETTINGS: 838258223a3SMatthew Dillon ccbh->status = CAM_FUNC_NOTAVAIL; 839258223a3SMatthew Dillon xpt_done(ccb); 840258223a3SMatthew Dillon break; 841258223a3SMatthew Dillon case XPT_GET_TRAN_SETTINGS: 842258223a3SMatthew Dillon ccb->cts.protocol = PROTO_SCSI; 843258223a3SMatthew Dillon ccb->cts.protocol_version = SCSI_REV_2; 8442cc2e845SMatthew Dillon ccb->cts.transport = XPORT_SATA; 845258223a3SMatthew Dillon ccb->cts.transport_version = XPORT_VERSION_UNSPECIFIED; 846258223a3SMatthew Dillon ccb->cts.proto_specific.valid = 0; 847258223a3SMatthew Dillon ccb->cts.xport_specific.valid = 0; 848258223a3SMatthew Dillon ccbh->status = CAM_REQ_CMP; 849258223a3SMatthew Dillon xpt_done(ccb); 850258223a3SMatthew Dillon break; 851258223a3SMatthew Dillon case XPT_CALC_GEOMETRY: 852258223a3SMatthew Dillon cam_calc_geometry(&ccb->ccg, 1); 853258223a3SMatthew Dillon xpt_done(ccb); 854258223a3SMatthew Dillon break; 855258223a3SMatthew Dillon case XPT_SCSI_IO: 856f4553de1SMatthew Dillon /* 857f4553de1SMatthew Dillon * Our parallel startup code might have only probed through 858f4553de1SMatthew Dillon * to the IDENT, so do the last step if necessary. 859f4553de1SMatthew Dillon */ 860f4553de1SMatthew Dillon if (at->at_probe == ATA_PROBE_NEED_IDENT) 861f4553de1SMatthew Dillon ahci_cam_probe(ap, atx); 862f4553de1SMatthew Dillon if (at->at_probe != ATA_PROBE_GOOD) { 863f4553de1SMatthew Dillon ccbh->status = CAM_DEV_NOT_THERE; 864f4553de1SMatthew Dillon xpt_done(ccb); 865f4553de1SMatthew Dillon break; 866f4553de1SMatthew Dillon } 8671980eff3SMatthew Dillon switch(at->at_type) { 868258223a3SMatthew Dillon case ATA_PORT_T_DISK: 8691980eff3SMatthew Dillon ahci_xpt_scsi_disk_io(ap, atx, ccb); 870258223a3SMatthew Dillon break; 871258223a3SMatthew Dillon case ATA_PORT_T_ATAPI: 8721980eff3SMatthew Dillon ahci_xpt_scsi_atapi_io(ap, atx, ccb); 873258223a3SMatthew Dillon break; 874258223a3SMatthew Dillon default: 875258223a3SMatthew Dillon ccbh->status = CAM_REQ_INVALID; 876258223a3SMatthew Dillon xpt_done(ccb); 877258223a3SMatthew Dillon break; 878258223a3SMatthew Dillon } 879258223a3SMatthew Dillon break; 880258223a3SMatthew Dillon default: 881258223a3SMatthew Dillon ccbh->status = CAM_REQ_INVALID; 882258223a3SMatthew Dillon xpt_done(ccb); 883258223a3SMatthew Dillon break; 884258223a3SMatthew Dillon } 885258223a3SMatthew Dillon } 886258223a3SMatthew Dillon 887258223a3SMatthew Dillon /* 888de68d532SMatthew Dillon * Poll function. 889de68d532SMatthew Dillon * 890de68d532SMatthew Dillon * Generally this function gets called heavily when interrupts might be 891de68d532SMatthew Dillon * non-operational, during a halt/reboot or panic. 892258223a3SMatthew Dillon */ 893258223a3SMatthew Dillon static 894258223a3SMatthew Dillon void 895258223a3SMatthew Dillon ahci_xpt_poll(struct cam_sim *sim) 896258223a3SMatthew Dillon { 897de68d532SMatthew Dillon struct ahci_port *ap; 898258223a3SMatthew Dillon 899de68d532SMatthew Dillon ap = cam_sim_softc(sim); 900de68d532SMatthew Dillon crit_enter(); 901f4553de1SMatthew Dillon ahci_os_lock_port(ap); 902f4553de1SMatthew Dillon ahci_port_intr(ap, 1); 903f4553de1SMatthew Dillon ahci_os_unlock_port(ap); 904de68d532SMatthew Dillon crit_exit(); 905258223a3SMatthew Dillon } 906258223a3SMatthew Dillon 907258223a3SMatthew Dillon /* 908b4189e5eSMatthew Dillon * Convert the SCSI command in ccb to an ata_xfer command in xa 909b4189e5eSMatthew Dillon * for ATA_PORT_T_DISK operations. Set the completion function 910b4189e5eSMatthew Dillon * to convert the response back, then dispatch to the OpenBSD AHCI 911b4189e5eSMatthew Dillon * layer. 912258223a3SMatthew Dillon * 913b4189e5eSMatthew Dillon * AHCI DISK commands only support a limited command set, and we 914b4189e5eSMatthew Dillon * fake additional commands to make it play nice with the CAM subsystem. 915258223a3SMatthew Dillon */ 916258223a3SMatthew Dillon static 917258223a3SMatthew Dillon void 9181980eff3SMatthew Dillon ahci_xpt_scsi_disk_io(struct ahci_port *ap, struct ata_port *atx, 9191980eff3SMatthew Dillon union ccb *ccb) 920258223a3SMatthew Dillon { 921258223a3SMatthew Dillon struct ccb_hdr *ccbh; 922258223a3SMatthew Dillon struct ccb_scsiio *csio; 923258223a3SMatthew Dillon struct ata_xfer *xa; 9241980eff3SMatthew Dillon struct ata_port *at; 925258223a3SMatthew Dillon struct ata_fis_h2d *fis; 926*bedbe7f8SAlex Hornung struct ata_pass_12 *atp12; 927*bedbe7f8SAlex Hornung struct ata_pass_16 *atp16; 928258223a3SMatthew Dillon scsi_cdb_t cdb; 929258223a3SMatthew Dillon union scsi_data *rdata; 930258223a3SMatthew Dillon int rdata_len; 931258223a3SMatthew Dillon u_int64_t capacity; 932258223a3SMatthew Dillon u_int64_t lba; 933258223a3SMatthew Dillon u_int32_t count; 934258223a3SMatthew Dillon 935258223a3SMatthew Dillon ccbh = &ccb->csio.ccb_h; 936258223a3SMatthew Dillon csio = &ccb->csio; 937b012a2caSMatthew Dillon at = atx ? atx : ap->ap_ata[0]; 9381980eff3SMatthew Dillon 9391980eff3SMatthew Dillon /* 9401980eff3SMatthew Dillon * XXX not passing NULL at for direct attach! 9411980eff3SMatthew Dillon */ 9421980eff3SMatthew Dillon xa = ahci_ata_get_xfer(ap, atx); 943258223a3SMatthew Dillon rdata = (void *)csio->data_ptr; 944258223a3SMatthew Dillon rdata_len = csio->dxfer_len; 945258223a3SMatthew Dillon 946258223a3SMatthew Dillon /* 947258223a3SMatthew Dillon * Build the FIS or process the csio to completion. 948258223a3SMatthew Dillon */ 949258223a3SMatthew Dillon cdb = (void *)((ccbh->flags & CAM_CDB_POINTER) ? 950258223a3SMatthew Dillon csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes); 951258223a3SMatthew Dillon 952258223a3SMatthew Dillon switch(cdb->generic.opcode) { 953258223a3SMatthew Dillon case REQUEST_SENSE: 954258223a3SMatthew Dillon /* 955258223a3SMatthew Dillon * Auto-sense everything, so explicit sense requests 956258223a3SMatthew Dillon * return no-sense. 957258223a3SMatthew Dillon */ 958258223a3SMatthew Dillon ccbh->status = CAM_SCSI_STATUS_ERROR; 959258223a3SMatthew Dillon break; 960258223a3SMatthew Dillon case INQUIRY: 961258223a3SMatthew Dillon /* 962258223a3SMatthew Dillon * Inquiry supported features 963258223a3SMatthew Dillon * 964258223a3SMatthew Dillon * [opcode, byte2, page_code, length, control] 965258223a3SMatthew Dillon */ 966258223a3SMatthew Dillon if (cdb->inquiry.byte2 & SI_EVPD) { 9676e0003adSMatthew Dillon ahci_xpt_page_inquiry(ap, at, ccb); 968258223a3SMatthew Dillon } else { 969258223a3SMatthew Dillon bzero(rdata, rdata_len); 970258223a3SMatthew Dillon if (rdata_len < SHORT_INQUIRY_LENGTH) { 971258223a3SMatthew Dillon ccbh->status = CAM_CCB_LEN_ERR; 972258223a3SMatthew Dillon break; 973258223a3SMatthew Dillon } 974258223a3SMatthew Dillon if (rdata_len > sizeof(rdata->inquiry_data)) 975258223a3SMatthew Dillon rdata_len = sizeof(rdata->inquiry_data); 976258223a3SMatthew Dillon rdata->inquiry_data.device = T_DIRECT; 977258223a3SMatthew Dillon rdata->inquiry_data.version = SCSI_REV_SPC2; 978258223a3SMatthew Dillon rdata->inquiry_data.response_format = 2; 979258223a3SMatthew Dillon rdata->inquiry_data.additional_length = 32; 980258223a3SMatthew Dillon bcopy("SATA ", rdata->inquiry_data.vendor, 8); 9811980eff3SMatthew Dillon bcopy(at->at_identify.model, 982258223a3SMatthew Dillon rdata->inquiry_data.product, 983258223a3SMatthew Dillon sizeof(rdata->inquiry_data.product)); 9841980eff3SMatthew Dillon bcopy(at->at_identify.firmware, 985258223a3SMatthew Dillon rdata->inquiry_data.revision, 986258223a3SMatthew Dillon sizeof(rdata->inquiry_data.revision)); 987258223a3SMatthew Dillon ccbh->status = CAM_REQ_CMP; 988258223a3SMatthew Dillon } 989258223a3SMatthew Dillon break; 990258223a3SMatthew Dillon case READ_CAPACITY_16: 991258223a3SMatthew Dillon if (cdb->read_capacity_16.service_action != SRC16_SERVICE_ACTION) { 992258223a3SMatthew Dillon ccbh->status = CAM_REQ_INVALID; 993258223a3SMatthew Dillon break; 994258223a3SMatthew Dillon } 995258223a3SMatthew Dillon if (rdata_len < sizeof(rdata->read_capacity_data_16)) { 996258223a3SMatthew Dillon ccbh->status = CAM_CCB_LEN_ERR; 997258223a3SMatthew Dillon break; 998258223a3SMatthew Dillon } 999258223a3SMatthew Dillon /* fall through */ 1000258223a3SMatthew Dillon case READ_CAPACITY: 1001258223a3SMatthew Dillon if (rdata_len < sizeof(rdata->read_capacity_data)) { 1002258223a3SMatthew Dillon ccbh->status = CAM_CCB_LEN_ERR; 1003258223a3SMatthew Dillon break; 1004258223a3SMatthew Dillon } 1005258223a3SMatthew Dillon 10061980eff3SMatthew Dillon capacity = at->at_capacity; 1007258223a3SMatthew Dillon 1008258223a3SMatthew Dillon bzero(rdata, rdata_len); 1009258223a3SMatthew Dillon if (cdb->generic.opcode == READ_CAPACITY) { 1010258223a3SMatthew Dillon rdata_len = sizeof(rdata->read_capacity_data); 1011258223a3SMatthew Dillon if (capacity > 0xFFFFFFFFU) 1012258223a3SMatthew Dillon capacity = 0xFFFFFFFFU; 1013258223a3SMatthew Dillon bzero(&rdata->read_capacity_data, rdata_len); 1014258223a3SMatthew Dillon scsi_ulto4b((u_int32_t)capacity - 1, 1015258223a3SMatthew Dillon rdata->read_capacity_data.addr); 1016258223a3SMatthew Dillon scsi_ulto4b(512, rdata->read_capacity_data.length); 1017258223a3SMatthew Dillon } else { 1018258223a3SMatthew Dillon rdata_len = sizeof(rdata->read_capacity_data_16); 1019258223a3SMatthew Dillon bzero(&rdata->read_capacity_data_16, rdata_len); 1020258223a3SMatthew Dillon scsi_u64to8b(capacity - 1, 1021258223a3SMatthew Dillon rdata->read_capacity_data_16.addr); 1022258223a3SMatthew Dillon scsi_ulto4b(512, rdata->read_capacity_data_16.length); 1023258223a3SMatthew Dillon } 1024258223a3SMatthew Dillon ccbh->status = CAM_REQ_CMP; 1025258223a3SMatthew Dillon break; 1026258223a3SMatthew Dillon case SYNCHRONIZE_CACHE: 1027258223a3SMatthew Dillon /* 1028258223a3SMatthew Dillon * Synchronize cache. Specification says this can take 1029258223a3SMatthew Dillon * greater then 30 seconds so give it at least 45. 1030258223a3SMatthew Dillon */ 1031258223a3SMatthew Dillon fis = xa->fis; 1032258223a3SMatthew Dillon fis->flags = ATA_H2D_FLAGS_CMD; 1033258223a3SMatthew Dillon fis->command = ATA_C_FLUSH_CACHE; 1034258223a3SMatthew Dillon fis->device = 0; 10353209f581SMatthew Dillon if (xa->timeout < 45000) 10363209f581SMatthew Dillon xa->timeout = 45000; 10371980eff3SMatthew Dillon xa->datalen = 0; 1038b012a2caSMatthew Dillon xa->flags = 0; 10391980eff3SMatthew Dillon xa->complete = ahci_ata_complete_disk_synchronize_cache; 1040258223a3SMatthew Dillon break; 1041258223a3SMatthew Dillon case TEST_UNIT_READY: 1042258223a3SMatthew Dillon case START_STOP_UNIT: 1043258223a3SMatthew Dillon case PREVENT_ALLOW: 1044258223a3SMatthew Dillon /* 1045258223a3SMatthew Dillon * Just silently return success 1046258223a3SMatthew Dillon */ 1047258223a3SMatthew Dillon ccbh->status = CAM_REQ_CMP; 1048258223a3SMatthew Dillon rdata_len = 0; 1049258223a3SMatthew Dillon break; 1050258223a3SMatthew Dillon case ATA_PASS_12: 1051*bedbe7f8SAlex Hornung atp12 = &cdb->ata_pass_12; 1052*bedbe7f8SAlex Hornung fis = xa->fis; 1053258223a3SMatthew Dillon /* 1054*bedbe7f8SAlex Hornung * Figure out the flags to be used, depending on the direction of the 1055*bedbe7f8SAlex Hornung * CAM request. 1056258223a3SMatthew Dillon */ 1057*bedbe7f8SAlex Hornung switch (ccbh->flags & CAM_DIR_MASK) { 1058*bedbe7f8SAlex Hornung case CAM_DIR_IN: 1059*bedbe7f8SAlex Hornung xa->flags = ATA_F_READ; 1060*bedbe7f8SAlex Hornung break; 1061*bedbe7f8SAlex Hornung case CAM_DIR_OUT: 1062*bedbe7f8SAlex Hornung xa->flags = ATA_F_WRITE; 1063*bedbe7f8SAlex Hornung break; 1064*bedbe7f8SAlex Hornung default: 1065*bedbe7f8SAlex Hornung xa->flags = 0; 1066*bedbe7f8SAlex Hornung } 1067*bedbe7f8SAlex Hornung xa->flags |= ATA_F_POLL; 1068*bedbe7f8SAlex Hornung xa->data = csio->data_ptr; 1069*bedbe7f8SAlex Hornung xa->datalen = csio->dxfer_len; 1070*bedbe7f8SAlex Hornung xa->complete = ahci_ata_complete_disk_rw; 1071*bedbe7f8SAlex Hornung xa->timeout = ccbh->timeout; 1072*bedbe7f8SAlex Hornung 1073*bedbe7f8SAlex Hornung /* 1074*bedbe7f8SAlex Hornung * Populate the fis from the information we received through CAM 1075*bedbe7f8SAlex Hornung * ATA passthrough. 1076*bedbe7f8SAlex Hornung */ 1077*bedbe7f8SAlex Hornung fis->flags = ATA_H2D_FLAGS_CMD; /* maybe also atp12->flags ? */ 1078*bedbe7f8SAlex Hornung fis->features = atp12->features; 1079*bedbe7f8SAlex Hornung fis->sector_count = atp12->sector_count; 1080*bedbe7f8SAlex Hornung fis->lba_low = atp12->lba_low; 1081*bedbe7f8SAlex Hornung fis->lba_mid = atp12->lba_mid; 1082*bedbe7f8SAlex Hornung fis->lba_high = atp12->lba_high; 1083*bedbe7f8SAlex Hornung fis->device = atp12->device; /* maybe always 0? */ 1084*bedbe7f8SAlex Hornung fis->command = atp12->command; 1085*bedbe7f8SAlex Hornung fis->control = atp12->control; 1086*bedbe7f8SAlex Hornung 1087*bedbe7f8SAlex Hornung /* 1088*bedbe7f8SAlex Hornung * Mark as in progress so it is sent to the device. 1089*bedbe7f8SAlex Hornung */ 1090*bedbe7f8SAlex Hornung ccbh->status = CAM_REQ_INPROG; 1091*bedbe7f8SAlex Hornung break; 1092*bedbe7f8SAlex Hornung case ATA_PASS_16: 1093*bedbe7f8SAlex Hornung atp16 = &cdb->ata_pass_16; 1094*bedbe7f8SAlex Hornung fis = xa->fis; 1095*bedbe7f8SAlex Hornung /* 1096*bedbe7f8SAlex Hornung * Figure out the flags to be used, depending on the direction of the 1097*bedbe7f8SAlex Hornung * CAM request. 1098*bedbe7f8SAlex Hornung */ 1099*bedbe7f8SAlex Hornung switch (ccbh->flags & CAM_DIR_MASK) { 1100*bedbe7f8SAlex Hornung case CAM_DIR_IN: 1101*bedbe7f8SAlex Hornung xa->flags = ATA_F_READ; 1102*bedbe7f8SAlex Hornung break; 1103*bedbe7f8SAlex Hornung case CAM_DIR_OUT: 1104*bedbe7f8SAlex Hornung xa->flags = ATA_F_WRITE; 1105*bedbe7f8SAlex Hornung break; 1106*bedbe7f8SAlex Hornung default: 1107*bedbe7f8SAlex Hornung xa->flags = 0; 1108*bedbe7f8SAlex Hornung } 1109*bedbe7f8SAlex Hornung xa->flags |= ATA_F_POLL; 1110*bedbe7f8SAlex Hornung xa->data = csio->data_ptr; 1111*bedbe7f8SAlex Hornung xa->datalen = csio->dxfer_len; 1112*bedbe7f8SAlex Hornung xa->complete = ahci_ata_complete_disk_rw; 1113*bedbe7f8SAlex Hornung xa->timeout = ccbh->timeout; 1114*bedbe7f8SAlex Hornung 1115*bedbe7f8SAlex Hornung /* 1116*bedbe7f8SAlex Hornung * Populate the fis from the information we received through CAM 1117*bedbe7f8SAlex Hornung * ATA passthrough. 1118*bedbe7f8SAlex Hornung */ 1119*bedbe7f8SAlex Hornung fis->flags = ATA_H2D_FLAGS_CMD; /* maybe also atp16->flags ? */ 1120*bedbe7f8SAlex Hornung fis->features = atp16->features; 1121*bedbe7f8SAlex Hornung fis->features_exp = atp16->features_ext; 1122*bedbe7f8SAlex Hornung fis->sector_count = atp16->sector_count; 1123*bedbe7f8SAlex Hornung fis->sector_count_exp = atp16->sector_count_ext; 1124*bedbe7f8SAlex Hornung fis->lba_low = atp16->lba_low; 1125*bedbe7f8SAlex Hornung fis->lba_low_exp = atp16->lba_low_ext; 1126*bedbe7f8SAlex Hornung fis->lba_mid = atp16->lba_mid; 1127*bedbe7f8SAlex Hornung fis->lba_mid_exp = atp16->lba_mid_ext; 1128*bedbe7f8SAlex Hornung fis->lba_high = atp16->lba_high; 1129*bedbe7f8SAlex Hornung fis->lba_mid_exp = atp16->lba_mid_ext; 1130*bedbe7f8SAlex Hornung fis->device = atp16->device; /* maybe always 0? */ 1131*bedbe7f8SAlex Hornung fis->command = atp16->command; 1132*bedbe7f8SAlex Hornung 1133*bedbe7f8SAlex Hornung /* 1134*bedbe7f8SAlex Hornung * Mark as in progress so it is sent to the device. 1135*bedbe7f8SAlex Hornung */ 1136*bedbe7f8SAlex Hornung ccbh->status = CAM_REQ_INPROG; 1137258223a3SMatthew Dillon break; 1138258223a3SMatthew Dillon default: 1139258223a3SMatthew Dillon switch(cdb->generic.opcode) { 1140258223a3SMatthew Dillon case READ_6: 1141258223a3SMatthew Dillon lba = scsi_3btoul(cdb->rw_6.addr) & 0x1FFFFF; 1142258223a3SMatthew Dillon count = cdb->rw_6.length ? cdb->rw_6.length : 0x100; 1143258223a3SMatthew Dillon xa->flags = ATA_F_READ; 1144258223a3SMatthew Dillon break; 1145258223a3SMatthew Dillon case READ_10: 1146258223a3SMatthew Dillon lba = scsi_4btoul(cdb->rw_10.addr); 1147258223a3SMatthew Dillon count = scsi_2btoul(cdb->rw_10.length); 1148258223a3SMatthew Dillon xa->flags = ATA_F_READ; 1149258223a3SMatthew Dillon break; 1150258223a3SMatthew Dillon case READ_12: 1151258223a3SMatthew Dillon lba = scsi_4btoul(cdb->rw_12.addr); 1152258223a3SMatthew Dillon count = scsi_4btoul(cdb->rw_12.length); 1153258223a3SMatthew Dillon xa->flags = ATA_F_READ; 1154258223a3SMatthew Dillon break; 1155258223a3SMatthew Dillon case READ_16: 1156258223a3SMatthew Dillon lba = scsi_8btou64(cdb->rw_16.addr); 1157258223a3SMatthew Dillon count = scsi_4btoul(cdb->rw_16.length); 1158258223a3SMatthew Dillon xa->flags = ATA_F_READ; 1159258223a3SMatthew Dillon break; 1160258223a3SMatthew Dillon case WRITE_6: 1161258223a3SMatthew Dillon lba = scsi_3btoul(cdb->rw_6.addr) & 0x1FFFFF; 1162258223a3SMatthew Dillon count = cdb->rw_6.length ? cdb->rw_6.length : 0x100; 1163258223a3SMatthew Dillon xa->flags = ATA_F_WRITE; 1164258223a3SMatthew Dillon break; 1165258223a3SMatthew Dillon case WRITE_10: 1166258223a3SMatthew Dillon lba = scsi_4btoul(cdb->rw_10.addr); 1167258223a3SMatthew Dillon count = scsi_2btoul(cdb->rw_10.length); 1168258223a3SMatthew Dillon xa->flags = ATA_F_WRITE; 1169258223a3SMatthew Dillon break; 1170258223a3SMatthew Dillon case WRITE_12: 1171258223a3SMatthew Dillon lba = scsi_4btoul(cdb->rw_12.addr); 1172258223a3SMatthew Dillon count = scsi_4btoul(cdb->rw_12.length); 1173258223a3SMatthew Dillon xa->flags = ATA_F_WRITE; 1174258223a3SMatthew Dillon break; 1175258223a3SMatthew Dillon case WRITE_16: 1176258223a3SMatthew Dillon lba = scsi_8btou64(cdb->rw_16.addr); 1177258223a3SMatthew Dillon count = scsi_4btoul(cdb->rw_16.length); 1178258223a3SMatthew Dillon xa->flags = ATA_F_WRITE; 1179258223a3SMatthew Dillon break; 1180258223a3SMatthew Dillon default: 1181258223a3SMatthew Dillon ccbh->status = CAM_REQ_INVALID; 1182258223a3SMatthew Dillon break; 1183258223a3SMatthew Dillon } 1184258223a3SMatthew Dillon if (ccbh->status != CAM_REQ_INPROG) 1185258223a3SMatthew Dillon break; 1186258223a3SMatthew Dillon 1187258223a3SMatthew Dillon fis = xa->fis; 1188258223a3SMatthew Dillon fis->flags = ATA_H2D_FLAGS_CMD; 1189258223a3SMatthew Dillon fis->lba_low = (u_int8_t)lba; 1190258223a3SMatthew Dillon fis->lba_mid = (u_int8_t)(lba >> 8); 1191258223a3SMatthew Dillon fis->lba_high = (u_int8_t)(lba >> 16); 1192258223a3SMatthew Dillon fis->device = ATA_H2D_DEVICE_LBA; 1193258223a3SMatthew Dillon 11941980eff3SMatthew Dillon /* 11951980eff3SMatthew Dillon * NCQ only for direct-attached disks, do not currently 11961980eff3SMatthew Dillon * try to use NCQ with port multipliers. 11971980eff3SMatthew Dillon */ 11981980eff3SMatthew Dillon if (at->at_ncqdepth > 1 && 11991980eff3SMatthew Dillon ap->ap_type == ATA_PORT_T_DISK && 1200258223a3SMatthew Dillon (ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) && 1201258223a3SMatthew Dillon (ccbh->flags & CAM_POLLED) == 0) { 1202258223a3SMatthew Dillon /* 1203258223a3SMatthew Dillon * Use NCQ - always uses 48 bit addressing 1204258223a3SMatthew Dillon */ 1205258223a3SMatthew Dillon xa->flags |= ATA_F_NCQ; 1206258223a3SMatthew Dillon fis->command = (xa->flags & ATA_F_WRITE) ? 1207258223a3SMatthew Dillon ATA_C_WRITE_FPDMA : ATA_C_READ_FPDMA; 1208258223a3SMatthew Dillon fis->lba_low_exp = (u_int8_t)(lba >> 24); 1209258223a3SMatthew Dillon fis->lba_mid_exp = (u_int8_t)(lba >> 32); 1210258223a3SMatthew Dillon fis->lba_high_exp = (u_int8_t)(lba >> 40); 1211258223a3SMatthew Dillon fis->sector_count = xa->tag << 3; 1212258223a3SMatthew Dillon fis->features = (u_int8_t)count; 1213258223a3SMatthew Dillon fis->features_exp = (u_int8_t)(count >> 8); 1214b2772aeeSMatthew Dillon } else if (count > 0x100 || lba > 0x0FFFFFFFU) { 1215258223a3SMatthew Dillon /* 1216258223a3SMatthew Dillon * Use LBA48 1217258223a3SMatthew Dillon */ 1218258223a3SMatthew Dillon fis->command = (xa->flags & ATA_F_WRITE) ? 1219258223a3SMatthew Dillon ATA_C_WRITEDMA_EXT : ATA_C_READDMA_EXT; 1220258223a3SMatthew Dillon fis->lba_low_exp = (u_int8_t)(lba >> 24); 1221258223a3SMatthew Dillon fis->lba_mid_exp = (u_int8_t)(lba >> 32); 1222258223a3SMatthew Dillon fis->lba_high_exp = (u_int8_t)(lba >> 40); 1223258223a3SMatthew Dillon fis->sector_count = (u_int8_t)count; 1224258223a3SMatthew Dillon fis->sector_count_exp = (u_int8_t)(count >> 8); 1225258223a3SMatthew Dillon } else { 1226258223a3SMatthew Dillon /* 1227258223a3SMatthew Dillon * Use LBA 1228258223a3SMatthew Dillon * 1229258223a3SMatthew Dillon * NOTE: 256 sectors is supported, stored as 0. 1230258223a3SMatthew Dillon */ 1231258223a3SMatthew Dillon fis->command = (xa->flags & ATA_F_WRITE) ? 1232258223a3SMatthew Dillon ATA_C_WRITEDMA : ATA_C_READDMA; 1233258223a3SMatthew Dillon fis->device |= (u_int8_t)(lba >> 24) & 0x0F; 1234258223a3SMatthew Dillon fis->sector_count = (u_int8_t)count; 1235258223a3SMatthew Dillon } 1236258223a3SMatthew Dillon 1237258223a3SMatthew Dillon xa->data = csio->data_ptr; 1238258223a3SMatthew Dillon xa->datalen = csio->dxfer_len; 1239258223a3SMatthew Dillon xa->complete = ahci_ata_complete_disk_rw; 12403209f581SMatthew Dillon xa->timeout = ccbh->timeout; /* milliseconds */ 124112feb904SMatthew Dillon #if 0 124212feb904SMatthew Dillon if (xa->timeout > 10000) /* XXX - debug */ 124312feb904SMatthew Dillon xa->timeout = 10000; 124412feb904SMatthew Dillon #endif 1245258223a3SMatthew Dillon if (ccbh->flags & CAM_POLLED) 1246258223a3SMatthew Dillon xa->flags |= ATA_F_POLL; 1247258223a3SMatthew Dillon break; 1248258223a3SMatthew Dillon } 1249258223a3SMatthew Dillon 1250258223a3SMatthew Dillon /* 1251258223a3SMatthew Dillon * If the request is still in progress the xa and FIS have 12526e0003adSMatthew Dillon * been set up (except for the PM target), and must be dispatched. 12536e0003adSMatthew Dillon * Otherwise the request was completed. 1254258223a3SMatthew Dillon */ 1255258223a3SMatthew Dillon if (ccbh->status == CAM_REQ_INPROG) { 1256258223a3SMatthew Dillon KKASSERT(xa->complete != NULL); 1257258223a3SMatthew Dillon xa->atascsi_private = ccb; 1258258223a3SMatthew Dillon ccb->ccb_h.sim_priv.entries[0].ptr = ap; 1259f4553de1SMatthew Dillon ahci_os_lock_port(ap); 12606e0003adSMatthew Dillon xa->fis->flags |= at->at_target; 1261258223a3SMatthew Dillon ahci_ata_cmd(xa); 1262f4553de1SMatthew Dillon ahci_os_unlock_port(ap); 1263258223a3SMatthew Dillon } else { 1264258223a3SMatthew Dillon ahci_ata_put_xfer(xa); 1265258223a3SMatthew Dillon xpt_done(ccb); 1266258223a3SMatthew Dillon } 1267258223a3SMatthew Dillon } 1268258223a3SMatthew Dillon 1269b4189e5eSMatthew Dillon /* 1270b4189e5eSMatthew Dillon * Convert the SCSI command in ccb to an ata_xfer command in xa 1271b4189e5eSMatthew Dillon * for ATA_PORT_T_ATAPI operations. Set the completion function 1272b4189e5eSMatthew Dillon * to convert the response back, then dispatch to the OpenBSD AHCI 1273b4189e5eSMatthew Dillon * layer. 1274b4189e5eSMatthew Dillon */ 1275258223a3SMatthew Dillon static 1276258223a3SMatthew Dillon void 12771980eff3SMatthew Dillon ahci_xpt_scsi_atapi_io(struct ahci_port *ap, struct ata_port *atx, 12781980eff3SMatthew Dillon union ccb *ccb) 1279258223a3SMatthew Dillon { 1280258223a3SMatthew Dillon struct ccb_hdr *ccbh; 1281258223a3SMatthew Dillon struct ccb_scsiio *csio; 1282258223a3SMatthew Dillon struct ata_xfer *xa; 1283258223a3SMatthew Dillon struct ata_fis_h2d *fis; 1284b4189e5eSMatthew Dillon scsi_cdb_t cdbs; 1285b4189e5eSMatthew Dillon scsi_cdb_t cdbd; 1286b4189e5eSMatthew Dillon int flags; 12871980eff3SMatthew Dillon struct ata_port *at; 1288258223a3SMatthew Dillon 1289258223a3SMatthew Dillon ccbh = &ccb->csio.ccb_h; 1290258223a3SMatthew Dillon csio = &ccb->csio; 1291b012a2caSMatthew Dillon at = atx ? atx : ap->ap_ata[0]; 1292b4189e5eSMatthew Dillon 1293b4189e5eSMatthew Dillon switch (ccbh->flags & CAM_DIR_MASK) { 1294b4189e5eSMatthew Dillon case CAM_DIR_IN: 1295b4189e5eSMatthew Dillon flags = ATA_F_PACKET | ATA_F_READ; 1296b4189e5eSMatthew Dillon break; 1297b4189e5eSMatthew Dillon case CAM_DIR_OUT: 1298b4189e5eSMatthew Dillon flags = ATA_F_PACKET | ATA_F_WRITE; 1299b4189e5eSMatthew Dillon break; 1300b4189e5eSMatthew Dillon case CAM_DIR_NONE: 1301b4189e5eSMatthew Dillon flags = ATA_F_PACKET; 1302b4189e5eSMatthew Dillon break; 1303b4189e5eSMatthew Dillon default: 1304b4189e5eSMatthew Dillon ccbh->status = CAM_REQ_INVALID; 1305b4189e5eSMatthew Dillon xpt_done(ccb); 1306b4189e5eSMatthew Dillon return; 1307b4189e5eSMatthew Dillon /* NOT REACHED */ 1308b4189e5eSMatthew Dillon } 1309b4189e5eSMatthew Dillon 1310b4189e5eSMatthew Dillon /* 131112feb904SMatthew Dillon * Special handling to get the rfis back into host memory while 1312192ee1d0SMatthew Dillon * still allowing the chip to run commands in parallel to 131312feb904SMatthew Dillon * ATAPI devices behind a PM. 131412feb904SMatthew Dillon */ 131512feb904SMatthew Dillon flags |= ATA_F_AUTOSENSE; 131612feb904SMatthew Dillon 131712feb904SMatthew Dillon /* 1318b4189e5eSMatthew Dillon * The command has to fit in the packet command buffer. 1319b4189e5eSMatthew Dillon */ 1320b4189e5eSMatthew Dillon if (csio->cdb_len < 6 || csio->cdb_len > 16) { 1321b4189e5eSMatthew Dillon ccbh->status = CAM_CCB_LEN_ERR; 1322b4189e5eSMatthew Dillon xpt_done(ccb); 1323b4189e5eSMatthew Dillon return; 1324b4189e5eSMatthew Dillon } 1325b4189e5eSMatthew Dillon 1326b4189e5eSMatthew Dillon /* 1327192ee1d0SMatthew Dillon * Initialize the XA and FIS. It is unclear how much of 1328192ee1d0SMatthew Dillon * this has to mimic the equivalent ATA command. 13291980eff3SMatthew Dillon * 13301980eff3SMatthew Dillon * XXX not passing NULL at for direct attach! 1331b4189e5eSMatthew Dillon */ 13321980eff3SMatthew Dillon xa = ahci_ata_get_xfer(ap, atx); 1333258223a3SMatthew Dillon fis = xa->fis; 1334258223a3SMatthew Dillon 13351980eff3SMatthew Dillon fis->flags = ATA_H2D_FLAGS_CMD | at->at_target; 13361980eff3SMatthew Dillon fis->command = ATA_C_PACKET; 1337192ee1d0SMatthew Dillon fis->device = ATA_H2D_DEVICE_LBA; 13381980eff3SMatthew Dillon fis->sector_count = xa->tag << 3; 1339192ee1d0SMatthew Dillon if (flags & (ATA_F_READ | ATA_F_WRITE)) { 1340192ee1d0SMatthew Dillon if (flags & ATA_F_WRITE) { 13411980eff3SMatthew Dillon fis->features = ATA_H2D_FEATURES_DMA | 1342192ee1d0SMatthew Dillon ATA_H2D_FEATURES_DIR_WRITE; 1343192ee1d0SMatthew Dillon } else { 1344192ee1d0SMatthew Dillon fis->features = ATA_H2D_FEATURES_DMA | 1345192ee1d0SMatthew Dillon ATA_H2D_FEATURES_DIR_READ; 1346192ee1d0SMatthew Dillon } 1347192ee1d0SMatthew Dillon } else { 1348192ee1d0SMatthew Dillon fis->lba_mid = 0; 1349192ee1d0SMatthew Dillon fis->lba_high = 0; 1350192ee1d0SMatthew Dillon } 1351192ee1d0SMatthew Dillon fis->control = ATA_FIS_CONTROL_4BIT; 13521980eff3SMatthew Dillon 1353b4189e5eSMatthew Dillon xa->flags = flags; 1354b4189e5eSMatthew Dillon xa->data = csio->data_ptr; 1355b4189e5eSMatthew Dillon xa->datalen = csio->dxfer_len; 13563209f581SMatthew Dillon xa->timeout = ccbh->timeout; /* milliseconds */ 13571980eff3SMatthew Dillon 1358b4189e5eSMatthew Dillon if (ccbh->flags & CAM_POLLED) 1359b4189e5eSMatthew Dillon xa->flags |= ATA_F_POLL; 1360258223a3SMatthew Dillon 1361258223a3SMatthew Dillon /* 1362b4189e5eSMatthew Dillon * Copy the cdb to the packetcmd buffer in the FIS using a 1363b4189e5eSMatthew Dillon * convenient pointer in the xa. 1364258223a3SMatthew Dillon */ 1365b4189e5eSMatthew Dillon cdbs = (void *)((ccbh->flags & CAM_CDB_POINTER) ? 1366b4189e5eSMatthew Dillon csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes); 1367b4189e5eSMatthew Dillon bcopy(cdbs, xa->packetcmd, csio->cdb_len); 1368b4189e5eSMatthew Dillon 1369669fbbf7SMatthew Dillon #if 0 1370b4189e5eSMatthew Dillon kprintf("opcode %d cdb_len %d dxfer_len %d\n", 1371b4189e5eSMatthew Dillon cdbs->generic.opcode, 1372b4189e5eSMatthew Dillon csio->cdb_len, csio->dxfer_len); 1373669fbbf7SMatthew Dillon #endif 1374b4189e5eSMatthew Dillon 1375b4189e5eSMatthew Dillon /* 1376b4189e5eSMatthew Dillon * Some ATAPI commands do not actually follow the SCSI standard. 1377b4189e5eSMatthew Dillon */ 1378b4189e5eSMatthew Dillon cdbd = (void *)xa->packetcmd; 1379b4189e5eSMatthew Dillon 1380b4189e5eSMatthew Dillon switch(cdbd->generic.opcode) { 1381192ee1d0SMatthew Dillon case REQUEST_SENSE: 1382192ee1d0SMatthew Dillon /* 1383192ee1d0SMatthew Dillon * Force SENSE requests to the ATAPI sense length. 1384192ee1d0SMatthew Dillon * 1385192ee1d0SMatthew Dillon * It is unclear if this is needed or not. 1386192ee1d0SMatthew Dillon */ 1387192ee1d0SMatthew Dillon if (cdbd->sense.length == SSD_FULL_SIZE) { 1388b012a2caSMatthew Dillon if (bootverbose) { 1389192ee1d0SMatthew Dillon kprintf("%s: Shortening sense request\n", 1390192ee1d0SMatthew Dillon PORTNAME(ap)); 1391b012a2caSMatthew Dillon } 1392192ee1d0SMatthew Dillon cdbd->sense.length = offsetof(struct scsi_sense_data, 1393192ee1d0SMatthew Dillon extra_bytes[0]); 1394192ee1d0SMatthew Dillon } 1395192ee1d0SMatthew Dillon break; 1396258223a3SMatthew Dillon case INQUIRY: 1397b4189e5eSMatthew Dillon /* 1398b4189e5eSMatthew Dillon * Some ATAPI devices can't handle long inquiry lengths, 1399b4189e5eSMatthew Dillon * don't ask me why. Truncate the inquiry length. 1400b4189e5eSMatthew Dillon */ 1401b4189e5eSMatthew Dillon if (cdbd->inquiry.page_code == 0 && 1402b4189e5eSMatthew Dillon cdbd->inquiry.length > SHORT_INQUIRY_LENGTH) { 1403b4189e5eSMatthew Dillon cdbd->inquiry.length = SHORT_INQUIRY_LENGTH; 1404b4189e5eSMatthew Dillon } 1405b4189e5eSMatthew Dillon break; 1406258223a3SMatthew Dillon case READ_6: 1407258223a3SMatthew Dillon case WRITE_6: 1408b4189e5eSMatthew Dillon /* 1409b4189e5eSMatthew Dillon * Convert *_6 to *_10 commands. Most ATAPI devices 1410b4189e5eSMatthew Dillon * cannot handle the SCSI READ_6 and WRITE_6 commands. 1411b4189e5eSMatthew Dillon */ 1412b4189e5eSMatthew Dillon cdbd->rw_10.opcode |= 0x20; 1413b4189e5eSMatthew Dillon cdbd->rw_10.byte2 = 0; 1414b4189e5eSMatthew Dillon cdbd->rw_10.addr[0] = cdbs->rw_6.addr[0] & 0x1F; 1415b4189e5eSMatthew Dillon cdbd->rw_10.addr[1] = cdbs->rw_6.addr[1]; 1416b4189e5eSMatthew Dillon cdbd->rw_10.addr[2] = cdbs->rw_6.addr[2]; 1417b4189e5eSMatthew Dillon cdbd->rw_10.addr[3] = 0; 1418b4189e5eSMatthew Dillon cdbd->rw_10.reserved = 0; 1419b4189e5eSMatthew Dillon cdbd->rw_10.length[0] = 0; 1420b4189e5eSMatthew Dillon cdbd->rw_10.length[1] = cdbs->rw_6.length; 1421b4189e5eSMatthew Dillon cdbd->rw_10.control = cdbs->rw_6.control; 1422b4189e5eSMatthew Dillon break; 1423258223a3SMatthew Dillon default: 1424258223a3SMatthew Dillon break; 1425258223a3SMatthew Dillon } 1426258223a3SMatthew Dillon 1427b4189e5eSMatthew Dillon /* 1428b4189e5eSMatthew Dillon * And dispatch 1429b4189e5eSMatthew Dillon */ 1430b4189e5eSMatthew Dillon xa->complete = ahci_atapi_complete_cmd; 1431258223a3SMatthew Dillon xa->atascsi_private = ccb; 1432258223a3SMatthew Dillon ccb->ccb_h.sim_priv.entries[0].ptr = ap; 143376497a9cSMatthew Dillon ahci_os_lock_port(ap); 1434258223a3SMatthew Dillon ahci_ata_cmd(xa); 143576497a9cSMatthew Dillon ahci_os_unlock_port(ap); 1436258223a3SMatthew Dillon } 1437258223a3SMatthew Dillon 1438b4189e5eSMatthew Dillon /* 14396e0003adSMatthew Dillon * Simulate page inquiries for disk attachments. 14406e0003adSMatthew Dillon */ 14416e0003adSMatthew Dillon static 14426e0003adSMatthew Dillon void 14436e0003adSMatthew Dillon ahci_xpt_page_inquiry(struct ahci_port *ap, struct ata_port *at, union ccb *ccb) 14446e0003adSMatthew Dillon { 14456e0003adSMatthew Dillon union { 14466e0003adSMatthew Dillon struct scsi_vpd_supported_page_list list; 14476e0003adSMatthew Dillon struct scsi_vpd_unit_serial_number serno; 14486e0003adSMatthew Dillon struct scsi_vpd_unit_devid devid; 14496e0003adSMatthew Dillon char buf[256]; 14506e0003adSMatthew Dillon } *page; 14516e0003adSMatthew Dillon scsi_cdb_t cdb; 14526e0003adSMatthew Dillon int i; 14536e0003adSMatthew Dillon int j; 14546e0003adSMatthew Dillon int len; 14556e0003adSMatthew Dillon 14566e0003adSMatthew Dillon page = kmalloc(sizeof(*page), M_DEVBUF, M_WAITOK | M_ZERO); 14576e0003adSMatthew Dillon 14586e0003adSMatthew Dillon cdb = (void *)((ccb->ccb_h.flags & CAM_CDB_POINTER) ? 14596e0003adSMatthew Dillon ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes); 14606e0003adSMatthew Dillon 14616e0003adSMatthew Dillon switch(cdb->inquiry.page_code) { 14626e0003adSMatthew Dillon case SVPD_SUPPORTED_PAGE_LIST: 14636e0003adSMatthew Dillon i = 0; 14646e0003adSMatthew Dillon page->list.device = T_DIRECT; 14656e0003adSMatthew Dillon page->list.page_code = SVPD_SUPPORTED_PAGE_LIST; 14666e0003adSMatthew Dillon page->list.list[i++] = SVPD_SUPPORTED_PAGE_LIST; 14676e0003adSMatthew Dillon page->list.list[i++] = SVPD_UNIT_SERIAL_NUMBER; 14686e0003adSMatthew Dillon page->list.list[i++] = SVPD_UNIT_DEVID; 14696e0003adSMatthew Dillon page->list.length = i; 14706e0003adSMatthew Dillon len = offsetof(struct scsi_vpd_supported_page_list, list[3]); 14716e0003adSMatthew Dillon break; 14726e0003adSMatthew Dillon case SVPD_UNIT_SERIAL_NUMBER: 14736e0003adSMatthew Dillon i = 0; 14746e0003adSMatthew Dillon j = sizeof(at->at_identify.serial); 14756e0003adSMatthew Dillon for (i = 0; i < j && at->at_identify.serial[i] == ' '; ++i) 14766e0003adSMatthew Dillon ; 14776e0003adSMatthew Dillon while (j > i && at->at_identify.serial[j-1] == ' ') 14786e0003adSMatthew Dillon --j; 14796e0003adSMatthew Dillon page->serno.device = T_DIRECT; 14806e0003adSMatthew Dillon page->serno.page_code = SVPD_UNIT_SERIAL_NUMBER; 14816e0003adSMatthew Dillon page->serno.length = j - i; 14826e0003adSMatthew Dillon bcopy(at->at_identify.serial + i, 14836e0003adSMatthew Dillon page->serno.serial_num, j - i); 14846e0003adSMatthew Dillon len = offsetof(struct scsi_vpd_unit_serial_number, 14856e0003adSMatthew Dillon serial_num[j-i]); 14866e0003adSMatthew Dillon break; 14876e0003adSMatthew Dillon case SVPD_UNIT_DEVID: 14886e0003adSMatthew Dillon /* fall through for now */ 14896e0003adSMatthew Dillon default: 14906e0003adSMatthew Dillon ccb->ccb_h.status = CAM_FUNC_NOTAVAIL; 14916e0003adSMatthew Dillon len = 0; 14926e0003adSMatthew Dillon break; 14936e0003adSMatthew Dillon } 14946e0003adSMatthew Dillon if (ccb->ccb_h.status == CAM_REQ_INPROG) { 14956e0003adSMatthew Dillon if (len <= ccb->csio.dxfer_len) { 14966e0003adSMatthew Dillon ccb->ccb_h.status = CAM_REQ_CMP; 14976e0003adSMatthew Dillon bzero(ccb->csio.data_ptr, ccb->csio.dxfer_len); 14986e0003adSMatthew Dillon bcopy(page, ccb->csio.data_ptr, len); 14996e0003adSMatthew Dillon ccb->csio.resid = ccb->csio.dxfer_len - len; 15006e0003adSMatthew Dillon } else { 15016e0003adSMatthew Dillon ccb->ccb_h.status = CAM_CCB_LEN_ERR; 15026e0003adSMatthew Dillon } 15036e0003adSMatthew Dillon } 15046e0003adSMatthew Dillon kfree(page, M_DEVBUF); 15056e0003adSMatthew Dillon } 15066e0003adSMatthew Dillon 15076e0003adSMatthew Dillon /* 1508b4189e5eSMatthew Dillon * Completion function for ATA_PORT_T_DISK cache synchronization. 1509b4189e5eSMatthew Dillon */ 1510258223a3SMatthew Dillon static 1511258223a3SMatthew Dillon void 1512258223a3SMatthew Dillon ahci_ata_complete_disk_synchronize_cache(struct ata_xfer *xa) 1513258223a3SMatthew Dillon { 1514258223a3SMatthew Dillon union ccb *ccb = xa->atascsi_private; 1515258223a3SMatthew Dillon struct ccb_hdr *ccbh = &ccb->ccb_h; 1516258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr; 1517258223a3SMatthew Dillon 1518258223a3SMatthew Dillon switch(xa->state) { 1519258223a3SMatthew Dillon case ATA_S_COMPLETE: 1520258223a3SMatthew Dillon ccbh->status = CAM_REQ_CMP; 1521b4189e5eSMatthew Dillon ccb->csio.scsi_status = SCSI_STATUS_OK; 1522258223a3SMatthew Dillon break; 1523258223a3SMatthew Dillon case ATA_S_ERROR: 15241980eff3SMatthew Dillon kprintf("%s: synchronize_cache: error\n", 15251980eff3SMatthew Dillon ATANAME(ap, xa->at)); 1526b4189e5eSMatthew Dillon ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID; 1527b4189e5eSMatthew Dillon ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND; 1528b4189e5eSMatthew Dillon ahci_ata_dummy_sense(&ccb->csio.sense_data); 1529258223a3SMatthew Dillon break; 1530258223a3SMatthew Dillon case ATA_S_TIMEOUT: 15311980eff3SMatthew Dillon kprintf("%s: synchronize_cache: timeout\n", 15321980eff3SMatthew Dillon ATANAME(ap, xa->at)); 1533258223a3SMatthew Dillon ccbh->status = CAM_CMD_TIMEOUT; 1534258223a3SMatthew Dillon break; 1535258223a3SMatthew Dillon default: 1536258223a3SMatthew Dillon kprintf("%s: synchronize_cache: unknown state %d\n", 15371980eff3SMatthew Dillon ATANAME(ap, xa->at), xa->state); 1538258223a3SMatthew Dillon ccbh->status = CAM_REQ_CMP_ERR; 1539258223a3SMatthew Dillon break; 1540258223a3SMatthew Dillon } 1541258223a3SMatthew Dillon ahci_ata_put_xfer(xa); 1542f4553de1SMatthew Dillon ahci_os_unlock_port(ap); 1543258223a3SMatthew Dillon xpt_done(ccb); 1544f4553de1SMatthew Dillon ahci_os_lock_port(ap); 1545258223a3SMatthew Dillon } 1546258223a3SMatthew Dillon 1547b4189e5eSMatthew Dillon /* 1548b4189e5eSMatthew Dillon * Completion function for ATA_PORT_T_DISK I/O 1549b4189e5eSMatthew Dillon */ 1550258223a3SMatthew Dillon static 1551258223a3SMatthew Dillon void 1552258223a3SMatthew Dillon ahci_ata_complete_disk_rw(struct ata_xfer *xa) 1553258223a3SMatthew Dillon { 1554258223a3SMatthew Dillon union ccb *ccb = xa->atascsi_private; 1555258223a3SMatthew Dillon struct ccb_hdr *ccbh = &ccb->ccb_h; 1556258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr; 1557258223a3SMatthew Dillon 1558258223a3SMatthew Dillon switch(xa->state) { 1559258223a3SMatthew Dillon case ATA_S_COMPLETE: 1560258223a3SMatthew Dillon ccbh->status = CAM_REQ_CMP; 1561b4189e5eSMatthew Dillon ccb->csio.scsi_status = SCSI_STATUS_OK; 1562258223a3SMatthew Dillon break; 1563258223a3SMatthew Dillon case ATA_S_ERROR: 15641980eff3SMatthew Dillon kprintf("%s: disk_rw: error\n", ATANAME(ap, xa->at)); 1565b4189e5eSMatthew Dillon ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID; 1566b4189e5eSMatthew Dillon ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND; 1567b4189e5eSMatthew Dillon ahci_ata_dummy_sense(&ccb->csio.sense_data); 1568258223a3SMatthew Dillon break; 1569258223a3SMatthew Dillon case ATA_S_TIMEOUT: 15701980eff3SMatthew Dillon kprintf("%s: disk_rw: timeout\n", ATANAME(ap, xa->at)); 1571258223a3SMatthew Dillon ccbh->status = CAM_CMD_TIMEOUT; 15724c339a5fSMatthew Dillon ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND; 15734c339a5fSMatthew Dillon ahci_ata_dummy_sense(&ccb->csio.sense_data); 1574258223a3SMatthew Dillon break; 1575258223a3SMatthew Dillon default: 1576258223a3SMatthew Dillon kprintf("%s: disk_rw: unknown state %d\n", 15771980eff3SMatthew Dillon ATANAME(ap, xa->at), xa->state); 1578258223a3SMatthew Dillon ccbh->status = CAM_REQ_CMP_ERR; 1579258223a3SMatthew Dillon break; 1580258223a3SMatthew Dillon } 1581258223a3SMatthew Dillon ccb->csio.resid = xa->resid; 1582258223a3SMatthew Dillon ahci_ata_put_xfer(xa); 1583f4553de1SMatthew Dillon ahci_os_unlock_port(ap); 1584258223a3SMatthew Dillon xpt_done(ccb); 1585f4553de1SMatthew Dillon ahci_os_lock_port(ap); 1586258223a3SMatthew Dillon } 1587b4189e5eSMatthew Dillon 15887d4fcf34SMatthew Dillon /* 15897d4fcf34SMatthew Dillon * Completion function for ATA_PORT_T_ATAPI I/O 15907d4fcf34SMatthew Dillon * 15917d4fcf34SMatthew Dillon * Sense data is returned in the rfis. 15927d4fcf34SMatthew Dillon */ 1593b4189e5eSMatthew Dillon static 1594b4189e5eSMatthew Dillon void 1595b4189e5eSMatthew Dillon ahci_atapi_complete_cmd(struct ata_xfer *xa) 1596b4189e5eSMatthew Dillon { 1597b4189e5eSMatthew Dillon union ccb *ccb = xa->atascsi_private; 1598b4189e5eSMatthew Dillon struct ccb_hdr *ccbh = &ccb->ccb_h; 1599b4189e5eSMatthew Dillon struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr; 1600b4189e5eSMatthew Dillon scsi_cdb_t cdb; 1601b4189e5eSMatthew Dillon 1602b4189e5eSMatthew Dillon cdb = (void *)((ccb->ccb_h.flags & CAM_CDB_POINTER) ? 1603b4189e5eSMatthew Dillon ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes); 1604b4189e5eSMatthew Dillon 1605b4189e5eSMatthew Dillon switch(xa->state) { 1606b4189e5eSMatthew Dillon case ATA_S_COMPLETE: 1607b4189e5eSMatthew Dillon ccbh->status = CAM_REQ_CMP; 1608b4189e5eSMatthew Dillon ccb->csio.scsi_status = SCSI_STATUS_OK; 1609b4189e5eSMatthew Dillon break; 1610b4189e5eSMatthew Dillon case ATA_S_ERROR: 1611b4189e5eSMatthew Dillon ccbh->status = CAM_SCSI_STATUS_ERROR; 1612b4189e5eSMatthew Dillon ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND; 16137d4fcf34SMatthew Dillon ahci_ata_atapi_sense(&xa->rfis, &ccb->csio.sense_data); 1614b4189e5eSMatthew Dillon break; 1615b4189e5eSMatthew Dillon case ATA_S_TIMEOUT: 1616b4189e5eSMatthew Dillon kprintf("%s: cmd %d: timeout\n", 1617b4189e5eSMatthew Dillon PORTNAME(ap), cdb->generic.opcode); 1618b4189e5eSMatthew Dillon ccbh->status = CAM_CMD_TIMEOUT; 16194c339a5fSMatthew Dillon ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND; 16204c339a5fSMatthew Dillon ahci_ata_dummy_sense(&ccb->csio.sense_data); 1621b4189e5eSMatthew Dillon break; 1622b4189e5eSMatthew Dillon default: 1623b4189e5eSMatthew Dillon kprintf("%s: cmd %d: unknown state %d\n", 1624b4189e5eSMatthew Dillon PORTNAME(ap), cdb->generic.opcode, xa->state); 1625b4189e5eSMatthew Dillon ccbh->status = CAM_REQ_CMP_ERR; 1626b4189e5eSMatthew Dillon break; 1627b4189e5eSMatthew Dillon } 1628b4189e5eSMatthew Dillon ccb->csio.resid = xa->resid; 1629b4189e5eSMatthew Dillon ahci_ata_put_xfer(xa); 1630f4553de1SMatthew Dillon ahci_os_unlock_port(ap); 1631b4189e5eSMatthew Dillon xpt_done(ccb); 1632f4553de1SMatthew Dillon ahci_os_lock_port(ap); 1633b4189e5eSMatthew Dillon } 1634b4189e5eSMatthew Dillon 16357d4fcf34SMatthew Dillon /* 16367d4fcf34SMatthew Dillon * Construct dummy sense data for errors on DISKs 16377d4fcf34SMatthew Dillon */ 1638b4189e5eSMatthew Dillon static 1639b4189e5eSMatthew Dillon void 1640b4189e5eSMatthew Dillon ahci_ata_dummy_sense(struct scsi_sense_data *sense_data) 1641b4189e5eSMatthew Dillon { 1642b4189e5eSMatthew Dillon sense_data->error_code = SSD_ERRCODE_VALID | SSD_CURRENT_ERROR; 1643b4189e5eSMatthew Dillon sense_data->segment = 0; 1644b4189e5eSMatthew Dillon sense_data->flags = SSD_KEY_MEDIUM_ERROR; 1645b4189e5eSMatthew Dillon sense_data->info[0] = 0; 1646b4189e5eSMatthew Dillon sense_data->info[1] = 0; 1647b4189e5eSMatthew Dillon sense_data->info[2] = 0; 1648b4189e5eSMatthew Dillon sense_data->info[3] = 0; 1649b4189e5eSMatthew Dillon sense_data->extra_len = 0; 1650b4189e5eSMatthew Dillon } 16517d4fcf34SMatthew Dillon 16527d4fcf34SMatthew Dillon /* 16537d4fcf34SMatthew Dillon * Construct atapi sense data for errors on ATAPI 16547d4fcf34SMatthew Dillon * 16557d4fcf34SMatthew Dillon * The ATAPI sense data is stored in the passed rfis and must be converted 16567d4fcf34SMatthew Dillon * to SCSI sense data. 16577d4fcf34SMatthew Dillon */ 16587d4fcf34SMatthew Dillon static 16597d4fcf34SMatthew Dillon void 16607d4fcf34SMatthew Dillon ahci_ata_atapi_sense(struct ata_fis_d2h *rfis, 16617d4fcf34SMatthew Dillon struct scsi_sense_data *sense_data) 16627d4fcf34SMatthew Dillon { 16637d4fcf34SMatthew Dillon sense_data->error_code = SSD_ERRCODE_VALID | SSD_CURRENT_ERROR; 16647d4fcf34SMatthew Dillon sense_data->segment = 0; 16657d4fcf34SMatthew Dillon sense_data->flags = (rfis->error & 0xF0) >> 4; 16667d4fcf34SMatthew Dillon if (rfis->error & 0x04) 16677d4fcf34SMatthew Dillon sense_data->flags |= SSD_KEY_ILLEGAL_REQUEST; 16687d4fcf34SMatthew Dillon if (rfis->error & 0x02) 16697d4fcf34SMatthew Dillon sense_data->flags |= SSD_EOM; 16707d4fcf34SMatthew Dillon if (rfis->error & 0x01) 16717d4fcf34SMatthew Dillon sense_data->flags |= SSD_ILI; 16727d4fcf34SMatthew Dillon sense_data->info[0] = 0; 16737d4fcf34SMatthew Dillon sense_data->info[1] = 0; 16747d4fcf34SMatthew Dillon sense_data->info[2] = 0; 16757d4fcf34SMatthew Dillon sense_data->info[3] = 0; 16767d4fcf34SMatthew Dillon sense_data->extra_len = 0; 16777d4fcf34SMatthew Dillon } 16786e0003adSMatthew Dillon 16796e0003adSMatthew Dillon static 16806e0003adSMatthew Dillon void 16816e0003adSMatthew Dillon ahci_strip_string(const char **basep, int *lenp) 16826e0003adSMatthew Dillon { 16836e0003adSMatthew Dillon const char *base = *basep; 16846e0003adSMatthew Dillon int len = *lenp; 16856e0003adSMatthew Dillon 16866e0003adSMatthew Dillon while (len && (*base == 0 || *base == ' ')) { 16876e0003adSMatthew Dillon --len; 16886e0003adSMatthew Dillon ++base; 16896e0003adSMatthew Dillon } 16906e0003adSMatthew Dillon while (len && (base[len-1] == 0 || base[len-1] == ' ')) 16916e0003adSMatthew Dillon --len; 16926e0003adSMatthew Dillon *basep = base; 16936e0003adSMatthew Dillon *lenp = len; 16946e0003adSMatthew Dillon } 1695