| Commit | Line | Data |
|---|---|---|
| 258223a3 | 1 | /* |
| fb00c6ed MD |
2 | * (MPSAFE) |
| 3 | * | |
| 258223a3 MD |
4 | * Copyright (c) 2009 The DragonFly Project. All rights reserved. |
| 5 | * | |
| 6 | * This code is derived from software contributed to The DragonFly Project | |
| 7 | * by Matthew Dillon <dillon@backplane.com> | |
| 8 | * | |
| 9 | * Redistribution and use in source and binary forms, with or without | |
| 10 | * modification, are permitted provided that the following conditions | |
| 11 | * are met: | |
| 12 | * | |
| 13 | * 1. Redistributions of source code must retain the above copyright | |
| 14 | * notice, this list of conditions and the following disclaimer. | |
| 15 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 16 | * notice, this list of conditions and the following disclaimer in | |
| 17 | * the documentation and/or other materials provided with the | |
| 18 | * distribution. | |
| 19 | * 3. Neither the name of The DragonFly Project nor the names of its | |
| 20 | * contributors may be used to endorse or promote products derived | |
| 21 | * from this software without specific, prior written permission. | |
| 22 | * | |
| 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 24 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
| 26 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
| 27 | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
| 28 | * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
| 29 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
| 30 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | |
| 31 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
| 32 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |
| 33 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
| 34 | * SUCH DAMAGE. | |
| 35 | * | |
| 36 | * | |
| 37 | * Copyright (c) 2007 David Gwynne <dlg@openbsd.org> | |
| 38 | * | |
| 39 | * Permission to use, copy, modify, and distribute this software for any | |
| 40 | * purpose with or without fee is hereby granted, provided that the above | |
| 41 | * copyright notice and this permission notice appear in all copies. | |
| 42 | * | |
| 43 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
| 44 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
| 45 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
| 46 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
| 47 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
| 48 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
| 49 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
| 50 | * | |
| 51 | * $OpenBSD: atascsi.c,v 1.64 2009/02/16 21:19:06 miod Exp $ | |
| 52 | * $DragonFly$ | |
| 53 | */ | |
| 54 | /* | |
| 55 | * Implement each SATA port as its own SCSI bus on CAM. This way we can | |
| 56 | * implement future port multiplier features as individual devices on the | |
| 57 | * bus. | |
| 58 | * | |
| 59 | * Much of the cdb<->xa conversion code was taken from OpenBSD, the rest | |
| 60 | * was written natively for DragonFly. | |
| 61 | */ | |
| 62 | ||
| 63 | #include "ahci.h" | |
| 64 | ||
| 65 | static void ahci_xpt_action(struct cam_sim *sim, union ccb *ccb); | |
| 66 | static void ahci_xpt_poll(struct cam_sim *sim); | |
| 1980eff3 MD |
67 | static void ahci_xpt_scsi_disk_io(struct ahci_port *ap, |
| 68 | struct ata_port *at, union ccb *ccb); | |
| 69 | static void ahci_xpt_scsi_atapi_io(struct ahci_port *ap, | |
| 70 | struct ata_port *at, union ccb *ccb); | |
| 6e0003ad MD |
71 | static void ahci_xpt_page_inquiry(struct ahci_port *ap, |
| 72 | struct ata_port *at, union ccb *ccb); | |
| 258223a3 MD |
73 | |
| 74 | static void ahci_ata_complete_disk_rw(struct ata_xfer *xa); | |
| 75 | static void ahci_ata_complete_disk_synchronize_cache(struct ata_xfer *xa); | |
| b4189e5e MD |
76 | static void ahci_atapi_complete_cmd(struct ata_xfer *xa); |
| 77 | static void ahci_ata_dummy_sense(struct scsi_sense_data *sense_data); | |
| 7d4fcf34 MD |
78 | static void ahci_ata_atapi_sense(struct ata_fis_d2h *rfis, |
| 79 | struct scsi_sense_data *sense_data); | |
| 258223a3 | 80 | |
| 1980eff3 MD |
81 | static int ahci_cam_probe_disk(struct ahci_port *ap, struct ata_port *at); |
| 82 | static int ahci_cam_probe_atapi(struct ahci_port *ap, struct ata_port *at); | |
| 69b3741e | 83 | static int ahci_set_xfer(struct ahci_port *ap, struct ata_port *atx); |
| b4189e5e | 84 | static void ahci_ata_dummy_done(struct ata_xfer *xa); |
| 258223a3 MD |
85 | static void ata_fix_identify(struct ata_identify *id); |
| 86 | static void ahci_cam_rescan(struct ahci_port *ap); | |
| 6e0003ad | 87 | static void ahci_strip_string(const char **basep, int *lenp); |
| 258223a3 MD |
88 | |
| 89 | int | |
| 90 | ahci_cam_attach(struct ahci_port *ap) | |
| 91 | { | |
| 92 | struct cam_devq *devq; | |
| 93 | struct cam_sim *sim; | |
| 94 | int error; | |
| 95 | int unit; | |
| 96 | ||
| cec85a37 MD |
97 | /* |
| 98 | * We want at least one ccb to be available for error processing | |
| 99 | * so don't let CAM use more then ncmds - 1. | |
| 100 | */ | |
| 258223a3 | 101 | unit = device_get_unit(ap->ap_sc->sc_dev); |
| cec85a37 MD |
102 | if (ap->ap_sc->sc_ncmds > 1) |
| 103 | devq = cam_simq_alloc(ap->ap_sc->sc_ncmds - 1); | |
| 104 | else | |
| 105 | devq = cam_simq_alloc(ap->ap_sc->sc_ncmds); | |
| 258223a3 MD |
106 | if (devq == NULL) { |
| 107 | return (ENOMEM); | |
| 108 | } | |
| 949597c2 MD |
109 | |
| 110 | /* | |
| 111 | * Give the devq enough room to run with 32 max_dev_transactions, | |
| 112 | * but set the overall max tags to 1 until NCQ is negotiated. | |
| 113 | */ | |
| 258223a3 | 114 | sim = cam_sim_alloc(ahci_xpt_action, ahci_xpt_poll, "ahci", |
| 949597c2 MD |
115 | (void *)ap, unit, &ap->ap_sim_lock, |
| 116 | 32, 1, devq); | |
| 258223a3 MD |
117 | cam_simq_release(devq); |
| 118 | if (sim == NULL) { | |
| 119 | return (ENOMEM); | |
| 120 | } | |
| 121 | ap->ap_sim = sim; | |
| 831bc9e3 | 122 | ahci_os_unlock_port(ap); |
| fb00c6ed | 123 | lockmgr(&ap->ap_sim_lock, LK_EXCLUSIVE); |
| 258223a3 | 124 | error = xpt_bus_register(ap->ap_sim, ap->ap_num); |
| fb00c6ed | 125 | lockmgr(&ap->ap_sim_lock, LK_RELEASE); |
| 831bc9e3 | 126 | ahci_os_lock_port(ap); |
| 258223a3 MD |
127 | if (error != CAM_SUCCESS) { |
| 128 | ahci_cam_detach(ap); | |
| 129 | return (EINVAL); | |
| 130 | } | |
| 131 | ap->ap_flags |= AP_F_BUS_REGISTERED; | |
| 258223a3 | 132 | |
| c408a8b3 MD |
133 | if (ap->ap_probe == ATA_PROBE_NEED_IDENT) |
| 134 | error = ahci_cam_probe(ap, NULL); | |
| 135 | else | |
| 136 | error = 0; | |
| 258223a3 MD |
137 | if (error) { |
| 138 | ahci_cam_detach(ap); | |
| 139 | return (EIO); | |
| 140 | } | |
| 141 | ap->ap_flags |= AP_F_CAM_ATTACHED; | |
| 142 | ||
| 258223a3 MD |
143 | return(0); |
| 144 | } | |
| 145 | ||
| 1980eff3 | 146 | /* |
| 3209f581 MD |
147 | * The state of the port has changed. |
| 148 | * | |
| 149 | * If at is NULL the physical port has changed state. | |
| 150 | * If at is non-NULL a particular target behind a PM has changed state. | |
| 151 | * | |
| 152 | * If found is -1 the target state must be queued to a non-interrupt context. | |
| 153 | * (only works with at == NULL). | |
| 154 | * | |
| 155 | * If found is 0 the target was removed. | |
| 156 | * If found is 1 the target was inserted. | |
| 1980eff3 | 157 | */ |
| 258223a3 | 158 | void |
| 3209f581 | 159 | ahci_cam_changed(struct ahci_port *ap, struct ata_port *atx, int found) |
| 258223a3 | 160 | { |
| fd8bd957 | 161 | struct cam_path *tmppath; |
| 3209f581 MD |
162 | int status; |
| 163 | int target; | |
| 164 | ||
| 165 | target = atx ? atx->at_target : CAM_TARGET_WILDCARD; | |
| fd8bd957 | 166 | |
| 258223a3 MD |
167 | if (ap->ap_sim == NULL) |
| 168 | return; | |
| 3209f581 MD |
169 | if (found == CAM_TARGET_WILDCARD) { |
| 170 | status = xpt_create_path(&tmppath, NULL, | |
| 171 | cam_sim_path(ap->ap_sim), | |
| 172 | target, CAM_LUN_WILDCARD); | |
| 173 | if (status != CAM_REQ_CMP) | |
| 174 | return; | |
| fd8bd957 MD |
175 | ahci_cam_rescan(ap); |
| 176 | } else { | |
| 3209f581 MD |
177 | status = xpt_create_path(&tmppath, NULL, |
| 178 | cam_sim_path(ap->ap_sim), | |
| 179 | target, | |
| 180 | CAM_LUN_WILDCARD); | |
| 181 | if (status != CAM_REQ_CMP) | |
| 182 | return; | |
| 183 | #if 0 | |
| 184 | /* | |
| 185 | * This confuses CAM | |
| 186 | */ | |
| 187 | if (found) | |
| 188 | xpt_async(AC_FOUND_DEVICE, tmppath, NULL); | |
| 189 | else | |
| 190 | xpt_async(AC_LOST_DEVICE, tmppath, NULL); | |
| 191 | #endif | |
| fd8bd957 MD |
192 | } |
| 193 | xpt_free_path(tmppath); | |
| 258223a3 MD |
194 | } |
| 195 | ||
| 196 | void | |
| 197 | ahci_cam_detach(struct ahci_port *ap) | |
| 198 | { | |
| 199 | int error; | |
| 200 | ||
| 201 | if ((ap->ap_flags & AP_F_CAM_ATTACHED) == 0) | |
| 202 | return; | |
| fb00c6ed | 203 | lockmgr(&ap->ap_sim_lock, LK_EXCLUSIVE); |
| 258223a3 MD |
204 | if (ap->ap_sim) { |
| 205 | xpt_freeze_simq(ap->ap_sim, 1); | |
| 206 | } | |
| 258223a3 MD |
207 | if (ap->ap_flags & AP_F_BUS_REGISTERED) { |
| 208 | error = xpt_bus_deregister(cam_sim_path(ap->ap_sim)); | |
| 209 | KKASSERT(error == CAM_REQ_CMP); | |
| 210 | ap->ap_flags &= ~AP_F_BUS_REGISTERED; | |
| 211 | } | |
| 212 | if (ap->ap_sim) { | |
| 213 | cam_sim_free(ap->ap_sim); | |
| 214 | ap->ap_sim = NULL; | |
| 215 | } | |
| fb00c6ed | 216 | lockmgr(&ap->ap_sim_lock, LK_RELEASE); |
| 258223a3 MD |
217 | ap->ap_flags &= ~AP_F_CAM_ATTACHED; |
| 218 | } | |
| 219 | ||
| 220 | /* | |
| 1980eff3 | 221 | * Once the AHCI port has been attached we need to probe for a device or |
| 258223a3 | 222 | * devices on the port and setup various options. |
| 1980eff3 MD |
223 | * |
| 224 | * If at is NULL we are probing the direct-attached device on the port, | |
| 225 | * which may or may not be a port multiplier. | |
| 258223a3 | 226 | */ |
| 3209f581 | 227 | int |
| 1980eff3 | 228 | ahci_cam_probe(struct ahci_port *ap, struct ata_port *atx) |
| 258223a3 | 229 | { |
| 1980eff3 | 230 | struct ata_port *at; |
| 258223a3 | 231 | struct ata_xfer *xa; |
| 258223a3 MD |
232 | u_int64_t capacity; |
| 233 | u_int64_t capacity_bytes; | |
| 234 | int model_len; | |
| 6e0003ad MD |
235 | int firmware_len; |
| 236 | int serial_len; | |
| fd8bd957 | 237 | int error; |
| 258223a3 MD |
238 | int devncqdepth; |
| 239 | int i; | |
| 6e0003ad MD |
240 | const char *model_id; |
| 241 | const char *firmware_id; | |
| 242 | const char *serial_id; | |
| 669fbbf7 MD |
243 | const char *wcstr; |
| 244 | const char *rastr; | |
| fd8bd957 MD |
245 | const char *scstr; |
| 246 | const char *type; | |
| 247 | ||
| 3209f581 | 248 | error = EIO; |
| 1980eff3 MD |
249 | |
| 250 | /* | |
| f4553de1 MD |
251 | * Delayed CAM attachment for initial probe, sim may be NULL |
| 252 | */ | |
| 253 | if (ap->ap_sim == NULL) | |
| 254 | return(0); | |
| 255 | ||
| 256 | /* | |
| 1980eff3 MD |
257 | * A NULL atx indicates a probe of the directly connected device. |
| 258 | * A non-NULL atx indicates a device connected via a port multiplier. | |
| 259 | * We need to preserve atx for calls to ahci_ata_get_xfer(). | |
| 260 | * | |
| 261 | * at is always non-NULL. For directly connected devices we supply | |
| 262 | * an (at) pointing to target 0. | |
| 263 | */ | |
| 264 | if (atx == NULL) { | |
| b012a2ca | 265 | at = ap->ap_ata[0]; /* direct attached - device 0 */ |
| 1980eff3 | 266 | if (ap->ap_type == ATA_PORT_T_PM) { |
| 831bc9e3 MD |
267 | kprintf("%s: Found Port Multiplier\n", |
| 268 | ATANAME(ap, atx)); | |
| 1980eff3 MD |
269 | return (0); |
| 270 | } | |
| 1980eff3 MD |
271 | at->at_type = ap->ap_type; |
| 272 | } else { | |
| 3209f581 | 273 | at = atx; |
| 1980eff3 MD |
274 | if (atx->at_type == ATA_PORT_T_PM) { |
| 275 | kprintf("%s: Bogus device, reducing port count to %d\n", | |
| 276 | ATANAME(ap, atx), atx->at_target); | |
| 277 | if (ap->ap_pmcount > atx->at_target) | |
| 278 | ap->ap_pmcount = atx->at_target; | |
| 3209f581 | 279 | goto err; |
| 1980eff3 | 280 | } |
| 1980eff3 | 281 | } |
| 3209f581 MD |
282 | if (ap->ap_type == ATA_PORT_T_NONE) |
| 283 | goto err; | |
| 1980eff3 | 284 | if (at->at_type == ATA_PORT_T_NONE) |
| 3209f581 | 285 | goto err; |
| 258223a3 | 286 | |
| 258223a3 MD |
287 | /* |
| 288 | * Issue identify, saving the result | |
| 289 | */ | |
| 1980eff3 | 290 | xa = ahci_ata_get_xfer(ap, atx); |
| 258223a3 | 291 | xa->complete = ahci_ata_dummy_done; |
| 1980eff3 MD |
292 | xa->data = &at->at_identify; |
| 293 | xa->datalen = sizeof(at->at_identify); | |
| 12feb904 | 294 | xa->flags = ATA_F_READ | ATA_F_PIO | ATA_F_POLL; |
| 1980eff3 MD |
295 | xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target; |
| 296 | ||
| 297 | switch(at->at_type) { | |
| 298 | case ATA_PORT_T_DISK: | |
| fd8bd957 MD |
299 | xa->fis->command = ATA_C_IDENTIFY; |
| 300 | type = "DISK"; | |
| 1980eff3 MD |
301 | break; |
| 302 | case ATA_PORT_T_ATAPI: | |
| 303 | xa->fis->command = ATA_C_ATAPI_IDENTIFY; | |
| 12feb904 | 304 | xa->flags |= ATA_F_AUTOSENSE; |
| 1980eff3 MD |
305 | type = "ATAPI"; |
| 306 | break; | |
| 307 | default: | |
| 308 | xa->fis->command = ATA_C_ATAPI_IDENTIFY; | |
| 309 | type = "UNKNOWN(ATAPI?)"; | |
| 310 | break; | |
| fd8bd957 | 311 | } |
| 258223a3 MD |
312 | xa->fis->features = 0; |
| 313 | xa->fis->device = 0; | |
| 3209f581 | 314 | xa->timeout = 1000; |
| 258223a3 | 315 | |
| 831bc9e3 | 316 | if (ahci_ata_cmd(xa) != ATA_S_COMPLETE) { |
| fd8bd957 | 317 | kprintf("%s: Detected %s device but unable to IDENTIFY\n", |
| 1980eff3 | 318 | ATANAME(ap, atx), type); |
| 258223a3 | 319 | ahci_ata_put_xfer(xa); |
| 3209f581 | 320 | goto err; |
| 258223a3 | 321 | } |
| 258223a3 MD |
322 | ahci_ata_put_xfer(xa); |
| 323 | ||
| 1980eff3 | 324 | ata_fix_identify(&at->at_identify); |
| 258223a3 MD |
325 | |
| 326 | /* | |
| 327 | * Read capacity using SATA probe info. | |
| 328 | */ | |
| 1980eff3 | 329 | if (le16toh(at->at_identify.cmdset83) & 0x0400) { |
| 258223a3 MD |
330 | /* LBA48 feature set supported */ |
| 331 | capacity = 0; | |
| 332 | for (i = 3; i >= 0; --i) { | |
| 333 | capacity <<= 16; | |
| 334 | capacity += | |
| 1980eff3 | 335 | le16toh(at->at_identify.addrsecxt[i]); |
| 258223a3 MD |
336 | } |
| 337 | } else { | |
| 1980eff3 | 338 | capacity = le16toh(at->at_identify.addrsec[1]); |
| 258223a3 | 339 | capacity <<= 16; |
| 1980eff3 | 340 | capacity += le16toh(at->at_identify.addrsec[0]); |
| 258223a3 | 341 | } |
| 12feb904 MD |
342 | if (capacity == 0) |
| 343 | capacity = 1024 * 1024 / 512; | |
| 1980eff3 MD |
344 | at->at_capacity = capacity; |
| 345 | if (atx == NULL) | |
| 346 | ap->ap_probe = ATA_PROBE_GOOD; | |
| 258223a3 MD |
347 | |
| 348 | capacity_bytes = capacity * 512; | |
| 349 | ||
| 350 | /* | |
| 351 | * Negotiate NCQ, throw away any ata_xfer's beyond the negotiated | |
| 352 | * number of slots and limit the number of CAM ccb's to one less | |
| 353 | * so we always have a slot available for recovery. | |
| 354 | * | |
| 355 | * NCQ is not used if ap_ncqdepth is 1 or the host controller does | |
| 356 | * not support it, and in that case the driver can handle extra | |
| 357 | * ccb's. | |
| cec85a37 | 358 | * |
| 1980eff3 MD |
359 | * NCQ is currently used only with direct-attached disks. It is |
| 360 | * not used with port multipliers or direct-attached ATAPI devices. | |
| 361 | * | |
| cec85a37 MD |
362 | * Remember at least one extra CCB needs to be reserved for the |
| 363 | * error ccb. | |
| 258223a3 MD |
364 | */ |
| 365 | if ((ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) && | |
| 1980eff3 MD |
366 | ap->ap_type == ATA_PORT_T_DISK && |
| 367 | (le16toh(at->at_identify.satacap) & (1 << 8))) { | |
| 368 | at->at_ncqdepth = (le16toh(at->at_identify.qdepth) & 0x1F) + 1; | |
| 369 | devncqdepth = at->at_ncqdepth; | |
| 370 | if (at->at_ncqdepth > ap->ap_sc->sc_ncmds) | |
| 371 | at->at_ncqdepth = ap->ap_sc->sc_ncmds; | |
| 372 | if (at->at_ncqdepth > 1) { | |
| cec85a37 | 373 | for (i = 0; i < ap->ap_sc->sc_ncmds; ++i) { |
| 1980eff3 MD |
374 | xa = ahci_ata_get_xfer(ap, atx); |
| 375 | if (xa->tag < at->at_ncqdepth) { | |
| cec85a37 MD |
376 | xa->state = ATA_S_COMPLETE; |
| 377 | ahci_ata_put_xfer(xa); | |
| 378 | } | |
| 379 | } | |
| 1980eff3 | 380 | if (at->at_ncqdepth >= ap->ap_sc->sc_ncmds) { |
| 949597c2 MD |
381 | cam_sim_set_max_tags(ap->ap_sim, |
| 382 | at->at_ncqdepth - 1); | |
| 258223a3 | 383 | } |
| 258223a3 MD |
384 | } |
| 385 | } else { | |
| 386 | devncqdepth = 0; | |
| 387 | } | |
| 388 | ||
| 6e0003ad MD |
389 | model_len = sizeof(at->at_identify.model); |
| 390 | model_id = at->at_identify.model; | |
| 391 | ahci_strip_string(&model_id, &model_len); | |
| 392 | ||
| 393 | firmware_len = sizeof(at->at_identify.firmware); | |
| 394 | firmware_id = at->at_identify.firmware; | |
| 395 | ahci_strip_string(&firmware_id, &firmware_len); | |
| 396 | ||
| 397 | serial_len = sizeof(at->at_identify.serial); | |
| 398 | serial_id = at->at_identify.serial; | |
| 399 | ahci_strip_string(&serial_id, &serial_len); | |
| 669fbbf7 | 400 | |
| fd8bd957 MD |
401 | /* |
| 402 | * Generate informatiive strings. | |
| 403 | * | |
| 404 | * NOTE: We do not automatically set write caching, lookahead, | |
| 405 | * or the security state for ATAPI devices. | |
| 406 | */ | |
| 1980eff3 MD |
407 | if (at->at_identify.cmdset82 & ATA_IDENTIFY_WRITECACHE) { |
| 408 | if (at->at_identify.features85 & ATA_IDENTIFY_WRITECACHE) | |
| 669fbbf7 | 409 | wcstr = "enabled"; |
| 1980eff3 | 410 | else if (at->at_type == ATA_PORT_T_ATAPI) |
| fd8bd957 | 411 | wcstr = "disabled"; |
| 669fbbf7 MD |
412 | else |
| 413 | wcstr = "enabling"; | |
| 414 | } else { | |
| 415 | wcstr = "notsupp"; | |
| 416 | } | |
| 417 | ||
| 1980eff3 MD |
418 | if (at->at_identify.cmdset82 & ATA_IDENTIFY_LOOKAHEAD) { |
| 419 | if (at->at_identify.features85 & ATA_IDENTIFY_LOOKAHEAD) | |
| 669fbbf7 | 420 | rastr = "enabled"; |
| 1980eff3 | 421 | else if (at->at_type == ATA_PORT_T_ATAPI) |
| fd8bd957 | 422 | rastr = "disabled"; |
| 669fbbf7 MD |
423 | else |
| 424 | rastr = "enabling"; | |
| 425 | } else { | |
| 426 | rastr = "notsupp"; | |
| 427 | } | |
| 428 | ||
| 1980eff3 MD |
429 | if (at->at_identify.cmdset82 & ATA_IDENTIFY_SECURITY) { |
| 430 | if (at->at_identify.securestatus & ATA_SECURE_FROZEN) | |
| fd8bd957 | 431 | scstr = "frozen"; |
| 1980eff3 | 432 | else if (at->at_type == ATA_PORT_T_ATAPI) |
| fd8bd957 | 433 | scstr = "unfrozen"; |
| afa796d2 MD |
434 | else if (AhciNoFeatures & (1 << ap->ap_num)) |
| 435 | scstr = "<disabled>"; | |
| fd8bd957 MD |
436 | else |
| 437 | scstr = "freezing"; | |
| 438 | } else { | |
| 439 | scstr = "notsupp"; | |
| 440 | } | |
| 441 | ||
| 6e0003ad | 442 | kprintf("%s: Found %s \"%*.*s %*.*s\" serial=\"%*.*s\"\n" |
| 074579df MD |
443 | "%s: tags=%d/%d satacap=%04x satafea=%04x NCQ=%s " |
| 444 | "capacity=%lld.%02dMB\n", | |
| 445 | ||
| 1980eff3 | 446 | ATANAME(ap, atx), |
| fd8bd957 | 447 | type, |
| 6e0003ad MD |
448 | model_len, model_len, model_id, |
| 449 | firmware_len, firmware_len, firmware_id, | |
| 450 | serial_len, serial_len, serial_id, | |
| 258223a3 | 451 | |
| 1980eff3 | 452 | ATANAME(ap, atx), |
| 258223a3 | 453 | devncqdepth, ap->ap_sc->sc_ncmds, |
| 1980eff3 MD |
454 | at->at_identify.satacap, |
| 455 | at->at_identify.satafsup, | |
| 074579df | 456 | (at->at_ncqdepth > 1 ? "YES" : "NO"), |
| 258223a3 | 457 | (long long)capacity_bytes / (1024 * 1024), |
| 074579df MD |
458 | (int)(capacity_bytes % (1024 * 1024)) * 100 / (1024 * 1024) |
| 459 | ); | |
| 074579df | 460 | kprintf("%s: f85=%04x f86=%04x f87=%04x WC=%s RA=%s SEC=%s\n", |
| 1980eff3 MD |
461 | ATANAME(ap, atx), |
| 462 | at->at_identify.features85, | |
| 463 | at->at_identify.features86, | |
| 464 | at->at_identify.features87, | |
| 669fbbf7 | 465 | wcstr, |
| fd8bd957 MD |
466 | rastr, |
| 467 | scstr | |
| 258223a3 MD |
468 | ); |
| 469 | ||
| 470 | /* | |
| fd8bd957 MD |
471 | * Additional type-specific probing |
| 472 | */ | |
| 1980eff3 | 473 | switch(at->at_type) { |
| fd8bd957 | 474 | case ATA_PORT_T_DISK: |
| 1980eff3 MD |
475 | error = ahci_cam_probe_disk(ap, atx); |
| 476 | break; | |
| 477 | case ATA_PORT_T_ATAPI: | |
| 478 | error = ahci_cam_probe_atapi(ap, atx); | |
| fd8bd957 MD |
479 | break; |
| 480 | default: | |
| 1980eff3 | 481 | error = EIO; |
| fd8bd957 MD |
482 | break; |
| 483 | } | |
| 3209f581 MD |
484 | err: |
| 485 | if (error) { | |
| 486 | at->at_probe = ATA_PROBE_FAILED; | |
| 487 | if (atx == NULL) | |
| 488 | ap->ap_probe = at->at_probe; | |
| 489 | } else { | |
| 490 | at->at_probe = ATA_PROBE_GOOD; | |
| 491 | if (atx == NULL) | |
| 492 | ap->ap_probe = at->at_probe; | |
| 493 | } | |
| 494 | return (error); | |
| fd8bd957 MD |
495 | } |
| 496 | ||
| 497 | /* | |
| 498 | * DISK-specific probe after initial ident | |
| 499 | */ | |
| 500 | static int | |
| 1980eff3 | 501 | ahci_cam_probe_disk(struct ahci_port *ap, struct ata_port *atx) |
| fd8bd957 | 502 | { |
| 1980eff3 | 503 | struct ata_port *at; |
| fd8bd957 | 504 | struct ata_xfer *xa; |
| fd8bd957 | 505 | |
| b012a2ca | 506 | at = atx ? atx : ap->ap_ata[0]; |
| 1980eff3 | 507 | |
| fd8bd957 | 508 | /* |
| 69b3741e MD |
509 | * Set dummy xfer mode |
| 510 | */ | |
| 511 | ahci_set_xfer(ap, atx); | |
| 512 | ||
| 513 | /* | |
| 258223a3 | 514 | * Enable write cache if supported |
| fd8bd957 MD |
515 | * |
| 516 | * NOTE: "WD My Book" external disk devices have a very poor | |
| 517 | * daughter board between the the ESATA and the HD. Sending | |
| 518 | * any ATA_C_SET_FEATURES commands will break the hardware port | |
| 519 | * with a fatal protocol error. However, this device also | |
| 520 | * indicates that WRITECACHE is already on and READAHEAD is | |
| 521 | * not supported so we avoid the issue. | |
| 258223a3 | 522 | */ |
| 1980eff3 MD |
523 | if ((at->at_identify.cmdset82 & ATA_IDENTIFY_WRITECACHE) && |
| 524 | (at->at_identify.features85 & ATA_IDENTIFY_WRITECACHE) == 0) { | |
| 525 | xa = ahci_ata_get_xfer(ap, atx); | |
| 258223a3 MD |
526 | xa->complete = ahci_ata_dummy_done; |
| 527 | xa->fis->command = ATA_C_SET_FEATURES; | |
| b012a2ca MD |
528 | xa->fis->features = ATA_SF_WRITECACHE_EN; |
| 529 | /* xa->fis->features = ATA_SF_LOOKAHEAD_EN; */ | |
| 1980eff3 | 530 | xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target; |
| 669fbbf7 | 531 | xa->fis->device = 0; |
| b012a2ca | 532 | xa->flags = ATA_F_PIO | ATA_F_POLL; |
| 3209f581 | 533 | xa->timeout = 1000; |
| 669fbbf7 | 534 | xa->datalen = 0; |
| 831bc9e3 | 535 | if (ahci_ata_cmd(xa) == ATA_S_COMPLETE) |
| 1980eff3 | 536 | at->at_features |= ATA_PORT_F_WCACHE; |
| afa796d2 MD |
537 | else |
| 538 | kprintf("%s: Unable to enable write-caching\n", | |
| 539 | ATANAME(ap, atx)); | |
| 258223a3 MD |
540 | ahci_ata_put_xfer(xa); |
| 541 | } | |
| 542 | ||
| 543 | /* | |
| 544 | * Enable readahead if supported | |
| 545 | */ | |
| 1980eff3 MD |
546 | if ((at->at_identify.cmdset82 & ATA_IDENTIFY_LOOKAHEAD) && |
| 547 | (at->at_identify.features85 & ATA_IDENTIFY_LOOKAHEAD) == 0) { | |
| 548 | xa = ahci_ata_get_xfer(ap, atx); | |
| 258223a3 MD |
549 | xa->complete = ahci_ata_dummy_done; |
| 550 | xa->fis->command = ATA_C_SET_FEATURES; | |
| 551 | xa->fis->features = ATA_SF_LOOKAHEAD_EN; | |
| 1980eff3 | 552 | xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target; |
| 669fbbf7 | 553 | xa->fis->device = 0; |
| b012a2ca | 554 | xa->flags = ATA_F_PIO | ATA_F_POLL; |
| 3209f581 | 555 | xa->timeout = 1000; |
| 669fbbf7 | 556 | xa->datalen = 0; |
| 831bc9e3 | 557 | if (ahci_ata_cmd(xa) == ATA_S_COMPLETE) |
| 1980eff3 | 558 | at->at_features |= ATA_PORT_F_RAHEAD; |
| afa796d2 MD |
559 | else |
| 560 | kprintf("%s: Unable to enable read-ahead\n", | |
| 561 | ATANAME(ap, atx)); | |
| 258223a3 MD |
562 | ahci_ata_put_xfer(xa); |
| 563 | } | |
| 564 | ||
| 565 | /* | |
| 566 | * FREEZE LOCK the device so malicious users can't lock it on us. | |
| 567 | * As there is no harm in issuing this to devices that don't | |
| 568 | * support the security feature set we just send it, and don't bother | |
| 569 | * checking if the device sends a command abort to tell us it doesn't | |
| 570 | * support it | |
| 571 | */ | |
| 1980eff3 | 572 | if ((at->at_identify.cmdset82 & ATA_IDENTIFY_SECURITY) && |
| afa796d2 MD |
573 | (at->at_identify.securestatus & ATA_SECURE_FROZEN) == 0 && |
| 574 | (AhciNoFeatures & (1 << ap->ap_num)) == 0) { | |
| 1980eff3 | 575 | xa = ahci_ata_get_xfer(ap, atx); |
| 669fbbf7 MD |
576 | xa->complete = ahci_ata_dummy_done; |
| 577 | xa->fis->command = ATA_C_SEC_FREEZE_LOCK; | |
| 1980eff3 | 578 | xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target; |
| b012a2ca | 579 | xa->flags = ATA_F_PIO | ATA_F_POLL; |
| 3209f581 | 580 | xa->timeout = 1000; |
| 669fbbf7 | 581 | xa->datalen = 0; |
| 831bc9e3 | 582 | if (ahci_ata_cmd(xa) == ATA_S_COMPLETE) |
| 1980eff3 | 583 | at->at_features |= ATA_PORT_F_FRZLCK; |
| afa796d2 MD |
584 | else |
| 585 | kprintf("%s: Unable to set security freeze\n", | |
| 586 | ATANAME(ap, atx)); | |
| 669fbbf7 MD |
587 | ahci_ata_put_xfer(xa); |
| 588 | } | |
| 258223a3 | 589 | |
| b4189e5e MD |
590 | return (0); |
| 591 | } | |
| 592 | ||
| fd8bd957 MD |
593 | /* |
| 594 | * ATAPI-specific probe after initial ident | |
| 595 | */ | |
| b4189e5e | 596 | static int |
| 1980eff3 | 597 | ahci_cam_probe_atapi(struct ahci_port *ap, struct ata_port *atx) |
| b4189e5e | 598 | { |
| 69b3741e MD |
599 | ahci_set_xfer(ap, atx); |
| 600 | return(0); | |
| 601 | } | |
| 602 | ||
| 603 | /* | |
| 604 | * Setting the transfer mode is irrelevant for the SATA transport | |
| 605 | * but some (atapi) devices seem to need it anyway. In addition | |
| 606 | * if we are running through a SATA->PATA converter for some reason | |
| 607 | * beyond my comprehension we might have to set the mode. | |
| d15a49f2 MD |
608 | * |
| 609 | * We only support DMA modes for SATA attached devices, so don't bother | |
| 610 | * with legacy modes. | |
| 69b3741e MD |
611 | */ |
| 612 | static int | |
| 613 | ahci_set_xfer(struct ahci_port *ap, struct ata_port *atx) | |
| 614 | { | |
| 615 | struct ata_port *at; | |
| 616 | struct ata_xfer *xa; | |
| 617 | u_int16_t mode; | |
| d15a49f2 | 618 | u_int16_t mask; |
| 69b3741e MD |
619 | |
| 620 | at = atx ? atx : ap->ap_ata[0]; | |
| 621 | ||
| 622 | /* | |
| d15a49f2 | 623 | * Figure out the supported UDMA mode. Ignore other legacy modes. |
| 69b3741e | 624 | */ |
| d15a49f2 MD |
625 | mask = le16toh(at->at_identify.ultradma); |
| 626 | if ((mask & 0xFF) == 0 || mask == 0xFFFF) | |
| 69b3741e | 627 | return(0); |
| d15a49f2 MD |
628 | mask &= 0xFF; |
| 629 | mode = 0x4F; | |
| 630 | while ((mask & 0x8000) == 0) { | |
| 631 | mask <<= 1; | |
| 632 | --mode; | |
| 633 | } | |
| 69b3741e MD |
634 | |
| 635 | /* | |
| 636 | * SATA atapi devices often still report a dma mode, even though | |
| 637 | * it is irrelevant for SATA transport. It is also possible that | |
| 638 | * we are running through a SATA->PATA converter and seeing the | |
| 639 | * PATA dma mode. | |
| 640 | * | |
| 641 | * In this case the device may require a (dummy) SETXFER to be | |
| 642 | * sent before it will work properly. | |
| 643 | */ | |
| 644 | xa = ahci_ata_get_xfer(ap, atx); | |
| 645 | xa->complete = ahci_ata_dummy_done; | |
| 646 | xa->fis->command = ATA_C_SET_FEATURES; | |
| 647 | xa->fis->features = ATA_SF_SETXFER; | |
| 648 | xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target; | |
| d15a49f2 | 649 | xa->fis->sector_count = mode; |
| 69b3741e MD |
650 | xa->flags = ATA_F_PIO | ATA_F_POLL; |
| 651 | xa->timeout = 1000; | |
| 652 | xa->datalen = 0; | |
| 653 | if (ahci_ata_cmd(xa) != ATA_S_COMPLETE) { | |
| 654 | kprintf("%s: Unable to set dummy xfer mode \n", | |
| 655 | ATANAME(ap, atx)); | |
| 656 | } else if (bootverbose) { | |
| 657 | kprintf("%s: Set dummy xfer mode to %02x\n", | |
| d15a49f2 | 658 | ATANAME(ap, atx), mode); |
| 69b3741e MD |
659 | } |
| 660 | ahci_ata_put_xfer(xa); | |
| fd8bd957 MD |
661 | return(0); |
| 662 | } | |
| 663 | ||
| b4189e5e MD |
664 | /* |
| 665 | * Fix byte ordering so buffers can be accessed as | |
| 666 | * strings. | |
| 667 | */ | |
| 258223a3 MD |
668 | static void |
| 669 | ata_fix_identify(struct ata_identify *id) | |
| 670 | { | |
| 671 | u_int16_t *swap; | |
| 672 | int i; | |
| 673 | ||
| 674 | swap = (u_int16_t *)id->serial; | |
| 675 | for (i = 0; i < sizeof(id->serial) / sizeof(u_int16_t); i++) | |
| 676 | swap[i] = bswap16(swap[i]); | |
| 677 | ||
| 678 | swap = (u_int16_t *)id->firmware; | |
| 679 | for (i = 0; i < sizeof(id->firmware) / sizeof(u_int16_t); i++) | |
| 680 | swap[i] = bswap16(swap[i]); | |
| 681 | ||
| 682 | swap = (u_int16_t *)id->model; | |
| 683 | for (i = 0; i < sizeof(id->model) / sizeof(u_int16_t); i++) | |
| 684 | swap[i] = bswap16(swap[i]); | |
| 685 | } | |
| 686 | ||
| 687 | /* | |
| b4189e5e MD |
688 | * Dummy done callback for xa. |
| 689 | */ | |
| 690 | static void | |
| 691 | ahci_ata_dummy_done(struct ata_xfer *xa) | |
| 692 | { | |
| 693 | } | |
| 694 | ||
| 695 | /* | |
| 3209f581 MD |
696 | * Use an engineering request to initiate a target scan for devices |
| 697 | * behind a port multiplier. | |
| fd8bd957 | 698 | * |
| 3209f581 | 699 | * An asynchronous bus scan is used to avoid reentrancy issues. |
| 258223a3 MD |
700 | */ |
| 701 | static void | |
| 702 | ahci_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb) | |
| 703 | { | |
| 3209f581 MD |
704 | struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr; |
| 705 | ||
| f4553de1 MD |
706 | if (ccb->ccb_h.func_code == XPT_SCAN_BUS) { |
| 707 | ap->ap_flags &= ~AP_F_SCAN_RUNNING; | |
| 708 | if (ap->ap_flags & AP_F_SCAN_REQUESTED) { | |
| 709 | ap->ap_flags &= ~AP_F_SCAN_REQUESTED; | |
| 710 | ahci_cam_rescan(ap); | |
| 711 | } | |
| 712 | ap->ap_flags |= AP_F_SCAN_COMPLETED; | |
| 713 | wakeup(&ap->ap_flags); | |
| 3209f581 | 714 | } |
| f4553de1 | 715 | xpt_free_ccb(ccb); |
| 258223a3 MD |
716 | } |
| 717 | ||
| 718 | static void | |
| 719 | ahci_cam_rescan(struct ahci_port *ap) | |
| 720 | { | |
| 721 | struct cam_path *path; | |
| 722 | union ccb *ccb; | |
| 723 | int status; | |
| 3209f581 MD |
724 | int i; |
| 725 | ||
| 726 | if (ap->ap_flags & AP_F_SCAN_RUNNING) { | |
| 727 | ap->ap_flags |= AP_F_SCAN_REQUESTED; | |
| 728 | return; | |
| 729 | } | |
| 730 | ap->ap_flags |= AP_F_SCAN_RUNNING; | |
| 731 | for (i = 0; i < AHCI_MAX_PMPORTS; ++i) { | |
| b012a2ca | 732 | ap->ap_ata[i]->at_features |= ATA_PORT_F_RESCAN; |
| 3209f581 | 733 | } |
| 258223a3 | 734 | |
| 258223a3 MD |
735 | status = xpt_create_path(&path, xpt_periph, cam_sim_path(ap->ap_sim), |
| 736 | CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); | |
| 737 | if (status != CAM_REQ_CMP) | |
| 738 | return; | |
| 739 | ||
| f4553de1 | 740 | ccb = xpt_alloc_ccb(); |
| 258223a3 | 741 | xpt_setup_ccb(&ccb->ccb_h, path, 5); /* 5 = low priority */ |
| 2de5e9ba | 742 | ccb->ccb_h.func_code = XPT_ENG_EXEC; |
| 258223a3 | 743 | ccb->ccb_h.cbfcnp = ahci_cam_rescan_callback; |
| 3209f581 | 744 | ccb->ccb_h.sim_priv.entries[0].ptr = ap; |
| 258223a3 | 745 | ccb->crcn.flags = CAM_FLAG_NONE; |
| 2de5e9ba | 746 | xpt_action_async(ccb); |
| 258223a3 MD |
747 | } |
| 748 | ||
| 3209f581 MD |
749 | static void |
| 750 | ahci_xpt_rescan(struct ahci_port *ap) | |
| 751 | { | |
| 752 | struct cam_path *path; | |
| 753 | union ccb *ccb; | |
| 754 | int status; | |
| 755 | ||
| 756 | status = xpt_create_path(&path, xpt_periph, cam_sim_path(ap->ap_sim), | |
| 757 | CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); | |
| 758 | if (status != CAM_REQ_CMP) | |
| 759 | return; | |
| f4553de1 MD |
760 | |
| 761 | ccb = xpt_alloc_ccb(); | |
| 3209f581 | 762 | xpt_setup_ccb(&ccb->ccb_h, path, 5); /* 5 = low priority */ |
| 2de5e9ba | 763 | ccb->ccb_h.func_code = XPT_SCAN_BUS; |
| 3209f581 MD |
764 | ccb->ccb_h.cbfcnp = ahci_cam_rescan_callback; |
| 765 | ccb->ccb_h.sim_priv.entries[0].ptr = ap; | |
| 766 | ccb->crcn.flags = CAM_FLAG_NONE; | |
| baef7501 | 767 | xpt_action_async(ccb); |
| 3209f581 MD |
768 | } |
| 769 | ||
| 258223a3 MD |
770 | /* |
| 771 | * Action function - dispatch command | |
| 772 | */ | |
| 773 | static | |
| 774 | void | |
| 775 | ahci_xpt_action(struct cam_sim *sim, union ccb *ccb) | |
| 776 | { | |
| 777 | struct ahci_port *ap; | |
| 1980eff3 | 778 | struct ata_port *at, *atx; |
| 258223a3 MD |
779 | struct ccb_hdr *ccbh; |
| 780 | int unit; | |
| 781 | ||
| 782 | /* XXX lock */ | |
| 783 | ap = cam_sim_softc(sim); | |
| 1980eff3 | 784 | atx = NULL; |
| 258223a3 MD |
785 | KKASSERT(ap != NULL); |
| 786 | ccbh = &ccb->ccb_h; | |
| 787 | unit = cam_sim_unit(sim); | |
| 788 | ||
| 789 | /* | |
| 3209f581 MD |
790 | * Early failure checks. These checks do not apply to XPT_PATH_INQ, |
| 791 | * otherwise the bus rescan will not remove the dead devices when | |
| 792 | * unplugging a PM. | |
| 793 | * | |
| 1980eff3 MD |
794 | * For non-wildcards we have one target (0) and one lun (0), |
| 795 | * unless we have a port multiplier. | |
| 796 | * | |
| 797 | * A wildcard target indicates only the general bus is being | |
| 798 | * probed. | |
| 799 | * | |
| 800 | * Calculate at and atx. at is always non-NULL. atx is only | |
| 801 | * non-NULL for direct-attached devices. It will be NULL for | |
| 802 | * devices behind a port multiplier. | |
| 258223a3 MD |
803 | * |
| 804 | * XXX What do we do with a LUN wildcard? | |
| 805 | */ | |
| 3209f581 MD |
806 | if (ccbh->target_id != CAM_TARGET_WILDCARD && |
| 807 | ccbh->func_code != XPT_PATH_INQ) { | |
| 1980eff3 | 808 | if (ap->ap_type == ATA_PORT_T_NONE) { |
| 3209f581 | 809 | ccbh->status = CAM_DEV_NOT_THERE; |
| 258223a3 MD |
810 | xpt_done(ccb); |
| 811 | return; | |
| 812 | } | |
| 1980eff3 | 813 | if (ccbh->target_id < 0 || ccbh->target_id >= ap->ap_pmcount) { |
| 258223a3 MD |
814 | ccbh->status = CAM_DEV_NOT_THERE; |
| 815 | xpt_done(ccb); | |
| 816 | return; | |
| 817 | } | |
| b012a2ca | 818 | at = ap->ap_ata[ccbh->target_id]; |
| 1980eff3 MD |
819 | if (ap->ap_type == ATA_PORT_T_PM) |
| 820 | atx = at; | |
| 821 | ||
| 258223a3 MD |
822 | if (ccbh->target_lun != CAM_LUN_WILDCARD && ccbh->target_lun) { |
| 823 | ccbh->status = CAM_DEV_NOT_THERE; | |
| 824 | xpt_done(ccb); | |
| 825 | return; | |
| 826 | } | |
| b012a2ca MD |
827 | } else { |
| 828 | at = ap->ap_ata[0]; | |
| 258223a3 MD |
829 | } |
| 830 | ||
| 831 | /* | |
| 832 | * Switch on the meta XPT command | |
| 833 | */ | |
| 834 | switch(ccbh->func_code) { | |
| 3209f581 MD |
835 | case XPT_ENG_EXEC: |
| 836 | /* | |
| 837 | * This routine is called after a port multiplier has been | |
| 838 | * probed. | |
| 839 | */ | |
| 840 | ccbh->status = CAM_REQ_CMP; | |
| f4553de1 | 841 | ahci_os_lock_port(ap); |
| 831bc9e3 | 842 | ahci_port_state_machine(ap, 0); |
| f4553de1 | 843 | ahci_os_unlock_port(ap); |
| 3209f581 | 844 | xpt_done(ccb); |
| 3209f581 MD |
845 | ahci_xpt_rescan(ap); |
| 846 | break; | |
| 258223a3 | 847 | case XPT_PATH_INQ: |
| 3209f581 MD |
848 | /* |
| 849 | * This command always succeeds, otherwise the bus scan | |
| 850 | * will not detach dead devices. | |
| 851 | */ | |
| 258223a3 MD |
852 | ccb->cpi.version_num = 1; |
| 853 | ccb->cpi.hba_inquiry = 0; | |
| 854 | ccb->cpi.target_sprt = 0; | |
| 1980eff3 | 855 | ccb->cpi.hba_misc = PIM_SEQSCAN; |
| 258223a3 MD |
856 | ccb->cpi.hba_eng_cnt = 0; |
| 857 | bzero(ccb->cpi.vuhba_flags, sizeof(ccb->cpi.vuhba_flags)); | |
| 76497a9c | 858 | ccb->cpi.max_target = AHCI_MAX_PMPORTS - 1; |
| 258223a3 MD |
859 | ccb->cpi.max_lun = 0; |
| 860 | ccb->cpi.async_flags = 0; | |
| 861 | ccb->cpi.hpath_id = 0; | |
| 1980eff3 | 862 | ccb->cpi.initiator_id = AHCI_MAX_PMPORTS - 1; |
| 258223a3 MD |
863 | ccb->cpi.unit_number = cam_sim_unit(sim); |
| 864 | ccb->cpi.bus_id = cam_sim_bus(sim); | |
| 865 | ccb->cpi.base_transfer_speed = 150000; | |
| 2cc2e845 | 866 | ccb->cpi.transport = XPORT_SATA; |
| 258223a3 MD |
867 | ccb->cpi.transport_version = 1; |
| 868 | ccb->cpi.protocol = PROTO_SCSI; | |
| 869 | ccb->cpi.protocol_version = SCSI_REV_2; | |
| 870 | ||
| 3209f581 | 871 | ccbh->status = CAM_REQ_CMP; |
| 831bc9e3 MD |
872 | if (ccbh->target_id == CAM_TARGET_WILDCARD) { |
| 873 | ahci_os_lock_port(ap); | |
| 874 | ahci_port_state_machine(ap, 0); | |
| 875 | ahci_os_unlock_port(ap); | |
| 876 | } else { | |
| 258223a3 MD |
877 | switch(ahci_pread(ap, AHCI_PREG_SSTS) & |
| 878 | AHCI_PREG_SSTS_SPD) { | |
| 879 | case AHCI_PREG_SSTS_SPD_GEN1: | |
| 880 | ccb->cpi.base_transfer_speed = 150000; | |
| 881 | break; | |
| 882 | case AHCI_PREG_SSTS_SPD_GEN2: | |
| 883 | ccb->cpi.base_transfer_speed = 300000; | |
| 884 | break; | |
| 8986d351 MD |
885 | case AHCI_PREG_SSTS_SPD_GEN3: |
| 886 | ccb->cpi.base_transfer_speed = 600000; | |
| 887 | break; | |
| 258223a3 MD |
888 | default: |
| 889 | /* unknown */ | |
| 890 | ccb->cpi.base_transfer_speed = 1000; | |
| 891 | break; | |
| 892 | } | |
| 3209f581 MD |
893 | #if 0 |
| 894 | if (ap->ap_type == ATA_PORT_T_NONE) | |
| 895 | ccbh->status = CAM_DEV_NOT_THERE; | |
| 896 | #endif | |
| 258223a3 | 897 | } |
| 258223a3 MD |
898 | xpt_done(ccb); |
| 899 | break; | |
| 900 | case XPT_RESET_DEV: | |
| f4553de1 | 901 | ahci_os_lock_port(ap); |
| 1980eff3 | 902 | if (ap->ap_type == ATA_PORT_T_NONE) { |
| 3209f581 | 903 | ccbh->status = CAM_DEV_NOT_THERE; |
| 1980eff3 MD |
904 | } else { |
| 905 | ahci_port_reset(ap, atx, 0); | |
| 906 | ccbh->status = CAM_REQ_CMP; | |
| 907 | } | |
| f4553de1 | 908 | ahci_os_unlock_port(ap); |
| 258223a3 MD |
909 | xpt_done(ccb); |
| 910 | break; | |
| 911 | case XPT_RESET_BUS: | |
| f4553de1 | 912 | ahci_os_lock_port(ap); |
| 1980eff3 | 913 | ahci_port_reset(ap, NULL, 1); |
| f4553de1 | 914 | ahci_os_unlock_port(ap); |
| fd8bd957 | 915 | ccbh->status = CAM_REQ_CMP; |
| 258223a3 MD |
916 | xpt_done(ccb); |
| 917 | break; | |
| 918 | case XPT_SET_TRAN_SETTINGS: | |
| 919 | ccbh->status = CAM_FUNC_NOTAVAIL; | |
| 920 | xpt_done(ccb); | |
| 921 | break; | |
| 922 | case XPT_GET_TRAN_SETTINGS: | |
| 923 | ccb->cts.protocol = PROTO_SCSI; | |
| 924 | ccb->cts.protocol_version = SCSI_REV_2; | |
| 2cc2e845 | 925 | ccb->cts.transport = XPORT_SATA; |
| 258223a3 MD |
926 | ccb->cts.transport_version = XPORT_VERSION_UNSPECIFIED; |
| 927 | ccb->cts.proto_specific.valid = 0; | |
| 928 | ccb->cts.xport_specific.valid = 0; | |
| 929 | ccbh->status = CAM_REQ_CMP; | |
| 930 | xpt_done(ccb); | |
| 931 | break; | |
| 932 | case XPT_CALC_GEOMETRY: | |
| 933 | cam_calc_geometry(&ccb->ccg, 1); | |
| 934 | xpt_done(ccb); | |
| 935 | break; | |
| 936 | case XPT_SCSI_IO: | |
| f4553de1 MD |
937 | /* |
| 938 | * Our parallel startup code might have only probed through | |
| 939 | * to the IDENT, so do the last step if necessary. | |
| 940 | */ | |
| 941 | if (at->at_probe == ATA_PROBE_NEED_IDENT) | |
| 942 | ahci_cam_probe(ap, atx); | |
| 943 | if (at->at_probe != ATA_PROBE_GOOD) { | |
| 944 | ccbh->status = CAM_DEV_NOT_THERE; | |
| 945 | xpt_done(ccb); | |
| 946 | break; | |
| 947 | } | |
| 1980eff3 | 948 | switch(at->at_type) { |
| 258223a3 | 949 | case ATA_PORT_T_DISK: |
| 1980eff3 | 950 | ahci_xpt_scsi_disk_io(ap, atx, ccb); |
| 258223a3 MD |
951 | break; |
| 952 | case ATA_PORT_T_ATAPI: | |
| 1980eff3 | 953 | ahci_xpt_scsi_atapi_io(ap, atx, ccb); |
| 258223a3 MD |
954 | break; |
| 955 | default: | |
| 956 | ccbh->status = CAM_REQ_INVALID; | |
| 957 | xpt_done(ccb); | |
| 958 | break; | |
| 959 | } | |
| 960 | break; | |
| e0fb398b T |
961 | case XPT_TRIM: |
| 962 | { | |
| 963 | scsi_cdb_t cdb; | |
| 964 | struct ccb_scsiio *csio; | |
| 965 | csio = &ccb->csio; | |
| 966 | cdb = (void *)((ccbh->flags & CAM_CDB_POINTER) ? | |
| 967 | csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes); | |
| 968 | cdb->generic.opcode = TRIM; | |
| 969 | ahci_xpt_scsi_disk_io(ap, atx, ccb); | |
| 970 | break; | |
| 971 | } | |
| 258223a3 | 972 | default: |
| 258223a3 MD |
973 | ccbh->status = CAM_REQ_INVALID; |
| 974 | xpt_done(ccb); | |
| 975 | break; | |
| 976 | } | |
| 977 | } | |
| 978 | ||
| 979 | /* | |
| de68d532 MD |
980 | * Poll function. |
| 981 | * | |
| 982 | * Generally this function gets called heavily when interrupts might be | |
| 983 | * non-operational, during a halt/reboot or panic. | |
| 258223a3 MD |
984 | */ |
| 985 | static | |
| 986 | void | |
| 987 | ahci_xpt_poll(struct cam_sim *sim) | |
| 988 | { | |
| de68d532 | 989 | struct ahci_port *ap; |
| 258223a3 | 990 | |
| de68d532 MD |
991 | ap = cam_sim_softc(sim); |
| 992 | crit_enter(); | |
| f4553de1 MD |
993 | ahci_os_lock_port(ap); |
| 994 | ahci_port_intr(ap, 1); | |
| 995 | ahci_os_unlock_port(ap); | |
| de68d532 | 996 | crit_exit(); |
| 258223a3 MD |
997 | } |
| 998 | ||
| 999 | /* | |
| b4189e5e MD |
1000 | * Convert the SCSI command in ccb to an ata_xfer command in xa |
| 1001 | * for ATA_PORT_T_DISK operations. Set the completion function | |
| 1002 | * to convert the response back, then dispatch to the OpenBSD AHCI | |
| 1003 | * layer. | |
| 258223a3 | 1004 | * |
| b4189e5e MD |
1005 | * AHCI DISK commands only support a limited command set, and we |
| 1006 | * fake additional commands to make it play nice with the CAM subsystem. | |
| 258223a3 MD |
1007 | */ |
| 1008 | static | |
| 1009 | void | |
| 1980eff3 MD |
1010 | ahci_xpt_scsi_disk_io(struct ahci_port *ap, struct ata_port *atx, |
| 1011 | union ccb *ccb) | |
| 258223a3 | 1012 | { |
| 258223a3 MD |
1013 | struct ccb_hdr *ccbh; |
| 1014 | struct ccb_scsiio *csio; | |
| 1015 | struct ata_xfer *xa; | |
| 1980eff3 | 1016 | struct ata_port *at; |
| 258223a3 | 1017 | struct ata_fis_h2d *fis; |
| bedbe7f8 AH |
1018 | struct ata_pass_12 *atp12; |
| 1019 | struct ata_pass_16 *atp16; | |
| 258223a3 MD |
1020 | scsi_cdb_t cdb; |
| 1021 | union scsi_data *rdata; | |
| 1022 | int rdata_len; | |
| 1023 | u_int64_t capacity; | |
| 1024 | u_int64_t lba; | |
| 1025 | u_int32_t count; | |
| 1026 | ||
| 258223a3 MD |
1027 | ccbh = &ccb->csio.ccb_h; |
| 1028 | csio = &ccb->csio; | |
| b012a2ca | 1029 | at = atx ? atx : ap->ap_ata[0]; |
| 1980eff3 MD |
1030 | |
| 1031 | /* | |
| 1032 | * XXX not passing NULL at for direct attach! | |
| 1033 | */ | |
| 1034 | xa = ahci_ata_get_xfer(ap, atx); | |
| 258223a3 MD |
1035 | rdata = (void *)csio->data_ptr; |
| 1036 | rdata_len = csio->dxfer_len; | |
| 1037 | ||
| 1038 | /* | |
| 1039 | * Build the FIS or process the csio to completion. | |
| 1040 | */ | |
| 1041 | cdb = (void *)((ccbh->flags & CAM_CDB_POINTER) ? | |
| 1042 | csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes); | |
| 1043 | ||
| 1044 | switch(cdb->generic.opcode) { | |
| 1045 | case REQUEST_SENSE: | |
| 1046 | /* | |
| 1047 | * Auto-sense everything, so explicit sense requests | |
| 1048 | * return no-sense. | |
| 1049 | */ | |
| 1050 | ccbh->status = CAM_SCSI_STATUS_ERROR; | |
| 1051 | break; | |
| 1052 | case INQUIRY: | |
| 1053 | /* | |
| 1054 | * Inquiry supported features | |
| 1055 | * | |
| 1056 | * [opcode, byte2, page_code, length, control] | |
| 1057 | */ | |
| 1058 | if (cdb->inquiry.byte2 & SI_EVPD) { | |
| 6e0003ad | 1059 | ahci_xpt_page_inquiry(ap, at, ccb); |
| 258223a3 MD |
1060 | } else { |
| 1061 | bzero(rdata, rdata_len); | |
| 1062 | if (rdata_len < SHORT_INQUIRY_LENGTH) { | |
| 1063 | ccbh->status = CAM_CCB_LEN_ERR; | |
| 1064 | break; | |
| 1065 | } | |
| 1066 | if (rdata_len > sizeof(rdata->inquiry_data)) | |
| 1067 | rdata_len = sizeof(rdata->inquiry_data); | |
| 1068 | rdata->inquiry_data.device = T_DIRECT; | |
| 1069 | rdata->inquiry_data.version = SCSI_REV_SPC2; | |
| 1070 | rdata->inquiry_data.response_format = 2; | |
| 1071 | rdata->inquiry_data.additional_length = 32; | |
| 1072 | bcopy("SATA ", rdata->inquiry_data.vendor, 8); | |
| 1980eff3 | 1073 | bcopy(at->at_identify.model, |
| 258223a3 MD |
1074 | rdata->inquiry_data.product, |
| 1075 | sizeof(rdata->inquiry_data.product)); | |
| 1980eff3 | 1076 | bcopy(at->at_identify.firmware, |
| 258223a3 MD |
1077 | rdata->inquiry_data.revision, |
| 1078 | sizeof(rdata->inquiry_data.revision)); | |
| 1079 | ccbh->status = CAM_REQ_CMP; | |
| 1080 | } | |
| e0fb398b T |
1081 | |
| 1082 | /* | |
| 1083 | * Use the vendor specific area to set the TRIM status | |
| 1084 | * for scsi_da | |
| 1085 | */ | |
| 1086 | if (at->at_identify.support_dsm) { | |
| 1087 | rdata->inquiry_data.vendor_specific1[0] = | |
| 1088 | at->at_identify.support_dsm &ATA_SUPPORT_DSM_TRIM; | |
| 1089 | rdata->inquiry_data.vendor_specific1[1] = | |
| 1090 | at->at_identify.max_dsm_blocks; | |
| 1091 | } | |
| 258223a3 MD |
1092 | break; |
| 1093 | case READ_CAPACITY_16: | |
| 1094 | if (cdb->read_capacity_16.service_action != SRC16_SERVICE_ACTION) { | |
| 1095 | ccbh->status = CAM_REQ_INVALID; | |
| 1096 | break; | |
| 1097 | } | |
| 1098 | if (rdata_len < sizeof(rdata->read_capacity_data_16)) { | |
| 1099 | ccbh->status = CAM_CCB_LEN_ERR; | |
| 1100 | break; | |
| 1101 | } | |
| 1102 | /* fall through */ | |
| 1103 | case READ_CAPACITY: | |
| 1104 | if (rdata_len < sizeof(rdata->read_capacity_data)) { | |
| 1105 | ccbh->status = CAM_CCB_LEN_ERR; | |
| 1106 | break; | |
| 1107 | } | |
| 1108 | ||
| 1980eff3 | 1109 | capacity = at->at_capacity; |
| 258223a3 MD |
1110 | |
| 1111 | bzero(rdata, rdata_len); | |
| 1112 | if (cdb->generic.opcode == READ_CAPACITY) { | |
| 1113 | rdata_len = sizeof(rdata->read_capacity_data); | |
| 1114 | if (capacity > 0xFFFFFFFFU) | |
| 1115 | capacity = 0xFFFFFFFFU; | |
| 1116 | bzero(&rdata->read_capacity_data, rdata_len); | |
| 1117 | scsi_ulto4b((u_int32_t)capacity - 1, | |
| 1118 | rdata->read_capacity_data.addr); | |
| 1119 | scsi_ulto4b(512, rdata->read_capacity_data.length); | |
| 1120 | } else { | |
| 1121 | rdata_len = sizeof(rdata->read_capacity_data_16); | |
| 1122 | bzero(&rdata->read_capacity_data_16, rdata_len); | |
| 1123 | scsi_u64to8b(capacity - 1, | |
| 1124 | rdata->read_capacity_data_16.addr); | |
| 1125 | scsi_ulto4b(512, rdata->read_capacity_data_16.length); | |
| 1126 | } | |
| 1127 | ccbh->status = CAM_REQ_CMP; | |
| 1128 | break; | |
| 1129 | case SYNCHRONIZE_CACHE: | |
| 1130 | /* | |
| 1131 | * Synchronize cache. Specification says this can take | |
| 1132 | * greater then 30 seconds so give it at least 45. | |
| 1133 | */ | |
| 1134 | fis = xa->fis; | |
| 258223a3 MD |
1135 | fis->flags = ATA_H2D_FLAGS_CMD; |
| 1136 | fis->command = ATA_C_FLUSH_CACHE; | |
| 1137 | fis->device = 0; | |
| 3209f581 MD |
1138 | if (xa->timeout < 45000) |
| 1139 | xa->timeout = 45000; | |
| 1980eff3 | 1140 | xa->datalen = 0; |
| b012a2ca | 1141 | xa->flags = 0; |
| 1980eff3 | 1142 | xa->complete = ahci_ata_complete_disk_synchronize_cache; |
| 258223a3 | 1143 | break; |
| e0fb398b T |
1144 | case TRIM: |
| 1145 | fis = xa->fis; | |
| 1146 | fis->command = ATA_C_DATA_SET_MANAGEMENT; | |
| 1147 | fis->features = (u_int8_t)ATA_SF_DSM_TRIM; | |
| 1148 | fis->features_exp = (u_int8_t)(ATA_SF_DSM_TRIM>> 8); | |
| 1149 | ||
| 1150 | xa->flags = ATA_F_WRITE; | |
| 1151 | fis->flags = ATA_H2D_FLAGS_CMD; | |
| 1152 | ||
| 1153 | xa->data = csio->data_ptr; | |
| 1154 | xa->datalen = csio->dxfer_len; | |
| 1155 | xa->timeout = ccbh->timeout*50; /* milliseconds */ | |
| 1156 | ||
| 1157 | fis->sector_count =(u_int8_t)(xa->datalen/512); | |
| 1158 | fis->sector_count_exp =(u_int8_t)((xa->datalen/512)>>8); | |
| 1159 | ||
| 1160 | lba = 0; | |
| 1161 | fis->lba_low = (u_int8_t)lba; | |
| 1162 | fis->lba_mid = (u_int8_t)(lba >> 8); | |
| 1163 | fis->lba_high = (u_int8_t)(lba >> 16); | |
| 1164 | fis->lba_low_exp = (u_int8_t)(lba >> 24); | |
| 1165 | fis->lba_mid_exp = (u_int8_t)(lba >> 32); | |
| 1166 | fis->lba_high_exp = (u_int8_t)(lba >> 40); | |
| 1167 | ||
| 1168 | fis->device = ATA_H2D_DEVICE_LBA; | |
| 1169 | xa->data = csio->data_ptr; | |
| 1170 | ||
| 1171 | xa->complete = ahci_ata_complete_disk_rw; | |
| 1172 | ccbh->status = CAM_REQ_INPROG; | |
| 1173 | break; | |
| 258223a3 MD |
1174 | case TEST_UNIT_READY: |
| 1175 | case START_STOP_UNIT: | |
| 1176 | case PREVENT_ALLOW: | |
| 1177 | /* | |
| 1178 | * Just silently return success | |
| 1179 | */ | |
| 1180 | ccbh->status = CAM_REQ_CMP; | |
| 1181 | rdata_len = 0; | |
| 1182 | break; | |
| 1183 | case ATA_PASS_12: | |
| bedbe7f8 AH |
1184 | atp12 = &cdb->ata_pass_12; |
| 1185 | fis = xa->fis; | |
| 1186 | /* | |
| 1187 | * Figure out the flags to be used, depending on the direction of the | |
| 1188 | * CAM request. | |
| 1189 | */ | |
| 1190 | switch (ccbh->flags & CAM_DIR_MASK) { | |
| 1191 | case CAM_DIR_IN: | |
| 1192 | xa->flags = ATA_F_READ; | |
| 1193 | break; | |
| 1194 | case CAM_DIR_OUT: | |
| 1195 | xa->flags = ATA_F_WRITE; | |
| 1196 | break; | |
| 1197 | default: | |
| 1198 | xa->flags = 0; | |
| 1199 | } | |
| 22726f69 | 1200 | xa->flags |= ATA_F_POLL | ATA_F_EXCLUSIVE; |
| bedbe7f8 AH |
1201 | xa->data = csio->data_ptr; |
| 1202 | xa->datalen = csio->dxfer_len; | |
| 1203 | xa->complete = ahci_ata_complete_disk_rw; | |
| 1204 | xa->timeout = ccbh->timeout; | |
| 1205 | ||
| 1206 | /* | |
| 1207 | * Populate the fis from the information we received through CAM | |
| 1208 | * ATA passthrough. | |
| 1209 | */ | |
| 1210 | fis->flags = ATA_H2D_FLAGS_CMD; /* maybe also atp12->flags ? */ | |
| 1211 | fis->features = atp12->features; | |
| 1212 | fis->sector_count = atp12->sector_count; | |
| 1213 | fis->lba_low = atp12->lba_low; | |
| 1214 | fis->lba_mid = atp12->lba_mid; | |
| 1215 | fis->lba_high = atp12->lba_high; | |
| 1216 | fis->device = atp12->device; /* maybe always 0? */ | |
| 1217 | fis->command = atp12->command; | |
| 1218 | fis->control = atp12->control; | |
| 1219 | ||
| 1220 | /* | |
| 1221 | * Mark as in progress so it is sent to the device. | |
| 1222 | */ | |
| 1223 | ccbh->status = CAM_REQ_INPROG; | |
| 1224 | break; | |
| 258223a3 | 1225 | case ATA_PASS_16: |
| bedbe7f8 AH |
1226 | atp16 = &cdb->ata_pass_16; |
| 1227 | fis = xa->fis; | |
| 258223a3 | 1228 | /* |
| bedbe7f8 AH |
1229 | * Figure out the flags to be used, depending on the direction of the |
| 1230 | * CAM request. | |
| 258223a3 | 1231 | */ |
| bedbe7f8 AH |
1232 | switch (ccbh->flags & CAM_DIR_MASK) { |
| 1233 | case CAM_DIR_IN: | |
| 1234 | xa->flags = ATA_F_READ; | |
| 1235 | break; | |
| 1236 | case CAM_DIR_OUT: | |
| 1237 | xa->flags = ATA_F_WRITE; | |
| 1238 | break; | |
| 1239 | default: | |
| 1240 | xa->flags = 0; | |
| 1241 | } | |
| 22726f69 | 1242 | xa->flags |= ATA_F_POLL | ATA_F_EXCLUSIVE; |
| bedbe7f8 AH |
1243 | xa->data = csio->data_ptr; |
| 1244 | xa->datalen = csio->dxfer_len; | |
| 1245 | xa->complete = ahci_ata_complete_disk_rw; | |
| 1246 | xa->timeout = ccbh->timeout; | |
| 1247 | ||
| 1248 | /* | |
| 1249 | * Populate the fis from the information we received through CAM | |
| 1250 | * ATA passthrough. | |
| 1251 | */ | |
| 1252 | fis->flags = ATA_H2D_FLAGS_CMD; /* maybe also atp16->flags ? */ | |
| 1253 | fis->features = atp16->features; | |
| 1254 | fis->features_exp = atp16->features_ext; | |
| 1255 | fis->sector_count = atp16->sector_count; | |
| 1256 | fis->sector_count_exp = atp16->sector_count_ext; | |
| 1257 | fis->lba_low = atp16->lba_low; | |
| 1258 | fis->lba_low_exp = atp16->lba_low_ext; | |
| 1259 | fis->lba_mid = atp16->lba_mid; | |
| 1260 | fis->lba_mid_exp = atp16->lba_mid_ext; | |
| 1261 | fis->lba_high = atp16->lba_high; | |
| 1262 | fis->lba_mid_exp = atp16->lba_mid_ext; | |
| 1263 | fis->device = atp16->device; /* maybe always 0? */ | |
| 1264 | fis->command = atp16->command; | |
| 1265 | ||
| 1266 | /* | |
| 1267 | * Mark as in progress so it is sent to the device. | |
| 1268 | */ | |
| 1269 | ccbh->status = CAM_REQ_INPROG; | |
| 258223a3 MD |
1270 | break; |
| 1271 | default: | |
| 1272 | switch(cdb->generic.opcode) { | |
| 1273 | case READ_6: | |
| 1274 | lba = scsi_3btoul(cdb->rw_6.addr) & 0x1FFFFF; | |
| 1275 | count = cdb->rw_6.length ? cdb->rw_6.length : 0x100; | |
| 1276 | xa->flags = ATA_F_READ; | |
| 1277 | break; | |
| 1278 | case READ_10: | |
| 1279 | lba = scsi_4btoul(cdb->rw_10.addr); | |
| 1280 | count = scsi_2btoul(cdb->rw_10.length); | |
| 1281 | xa->flags = ATA_F_READ; | |
| 1282 | break; | |
| 1283 | case READ_12: | |
| 1284 | lba = scsi_4btoul(cdb->rw_12.addr); | |
| 1285 | count = scsi_4btoul(cdb->rw_12.length); | |
| 1286 | xa->flags = ATA_F_READ; | |
| 1287 | break; | |
| 1288 | case READ_16: | |
| 1289 | lba = scsi_8btou64(cdb->rw_16.addr); | |
| 1290 | count = scsi_4btoul(cdb->rw_16.length); | |
| 1291 | xa->flags = ATA_F_READ; | |
| 1292 | break; | |
| 1293 | case WRITE_6: | |
| 1294 | lba = scsi_3btoul(cdb->rw_6.addr) & 0x1FFFFF; | |
| 1295 | count = cdb->rw_6.length ? cdb->rw_6.length : 0x100; | |
| 1296 | xa->flags = ATA_F_WRITE; | |
| 1297 | break; | |
| 1298 | case WRITE_10: | |
| 1299 | lba = scsi_4btoul(cdb->rw_10.addr); | |
| 1300 | count = scsi_2btoul(cdb->rw_10.length); | |
| 1301 | xa->flags = ATA_F_WRITE; | |
| 1302 | break; | |
| 1303 | case WRITE_12: | |
| 1304 | lba = scsi_4btoul(cdb->rw_12.addr); | |
| 1305 | count = scsi_4btoul(cdb->rw_12.length); | |
| 1306 | xa->flags = ATA_F_WRITE; | |
| 1307 | break; | |
| 1308 | case WRITE_16: | |
| 1309 | lba = scsi_8btou64(cdb->rw_16.addr); | |
| 1310 | count = scsi_4btoul(cdb->rw_16.length); | |
| 1311 | xa->flags = ATA_F_WRITE; | |
| 1312 | break; | |
| 1313 | default: | |
| 1314 | ccbh->status = CAM_REQ_INVALID; | |
| 1315 | break; | |
| 1316 | } | |
| 1317 | if (ccbh->status != CAM_REQ_INPROG) | |
| 1318 | break; | |
| 1319 | ||
| 1320 | fis = xa->fis; | |
| 1321 | fis->flags = ATA_H2D_FLAGS_CMD; | |
| 1322 | fis->lba_low = (u_int8_t)lba; | |
| 1323 | fis->lba_mid = (u_int8_t)(lba >> 8); | |
| 1324 | fis->lba_high = (u_int8_t)(lba >> 16); | |
| 1325 | fis->device = ATA_H2D_DEVICE_LBA; | |
| 1326 | ||
| 1980eff3 MD |
1327 | /* |
| 1328 | * NCQ only for direct-attached disks, do not currently | |
| 1329 | * try to use NCQ with port multipliers. | |
| 1330 | */ | |
| 1331 | if (at->at_ncqdepth > 1 && | |
| 1332 | ap->ap_type == ATA_PORT_T_DISK && | |
| 258223a3 MD |
1333 | (ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) && |
| 1334 | (ccbh->flags & CAM_POLLED) == 0) { | |
| 1335 | /* | |
| 1336 | * Use NCQ - always uses 48 bit addressing | |
| 1337 | */ | |
| 1338 | xa->flags |= ATA_F_NCQ; | |
| 1339 | fis->command = (xa->flags & ATA_F_WRITE) ? | |
| 1340 | ATA_C_WRITE_FPDMA : ATA_C_READ_FPDMA; | |
| 1341 | fis->lba_low_exp = (u_int8_t)(lba >> 24); | |
| 1342 | fis->lba_mid_exp = (u_int8_t)(lba >> 32); | |
| 1343 | fis->lba_high_exp = (u_int8_t)(lba >> 40); | |
| 1344 | fis->sector_count = xa->tag << 3; | |
| 1345 | fis->features = (u_int8_t)count; | |
| 1346 | fis->features_exp = (u_int8_t)(count >> 8); | |
| b2772aee | 1347 | } else if (count > 0x100 || lba > 0x0FFFFFFFU) { |
| 258223a3 MD |
1348 | /* |
| 1349 | * Use LBA48 | |
| 1350 | */ | |
| 1351 | fis->command = (xa->flags & ATA_F_WRITE) ? | |
| 1352 | ATA_C_WRITEDMA_EXT : ATA_C_READDMA_EXT; | |
| 1353 | fis->lba_low_exp = (u_int8_t)(lba >> 24); | |
| 1354 | fis->lba_mid_exp = (u_int8_t)(lba >> 32); | |
| 1355 | fis->lba_high_exp = (u_int8_t)(lba >> 40); | |
| 1356 | fis->sector_count = (u_int8_t)count; | |
| 1357 | fis->sector_count_exp = (u_int8_t)(count >> 8); | |
| 1358 | } else { | |
| 1359 | /* | |
| 1360 | * Use LBA | |
| 1361 | * | |
| 1362 | * NOTE: 256 sectors is supported, stored as 0. | |
| 1363 | */ | |
| 1364 | fis->command = (xa->flags & ATA_F_WRITE) ? | |
| 1365 | ATA_C_WRITEDMA : ATA_C_READDMA; | |
| 1366 | fis->device |= (u_int8_t)(lba >> 24) & 0x0F; | |
| 1367 | fis->sector_count = (u_int8_t)count; | |
| 1368 | } | |
| 1369 | ||
| 1370 | xa->data = csio->data_ptr; | |
| 1371 | xa->datalen = csio->dxfer_len; | |
| 1372 | xa->complete = ahci_ata_complete_disk_rw; | |
| 3209f581 | 1373 | xa->timeout = ccbh->timeout; /* milliseconds */ |
| 12feb904 MD |
1374 | #if 0 |
| 1375 | if (xa->timeout > 10000) /* XXX - debug */ | |
| 1376 | xa->timeout = 10000; | |
| 1377 | #endif | |
| 258223a3 MD |
1378 | if (ccbh->flags & CAM_POLLED) |
| 1379 | xa->flags |= ATA_F_POLL; | |
| 1380 | break; | |
| 1381 | } | |
| 1382 | ||
| 1383 | /* | |
| 1384 | * If the request is still in progress the xa and FIS have | |
| 6e0003ad MD |
1385 | * been set up (except for the PM target), and must be dispatched. |
| 1386 | * Otherwise the request was completed. | |
| 258223a3 MD |
1387 | */ |
| 1388 | if (ccbh->status == CAM_REQ_INPROG) { | |
| 1389 | KKASSERT(xa->complete != NULL); | |
| 1390 | xa->atascsi_private = ccb; | |
| 1391 | ccb->ccb_h.sim_priv.entries[0].ptr = ap; | |
| f4553de1 | 1392 | ahci_os_lock_port(ap); |
| 6e0003ad | 1393 | xa->fis->flags |= at->at_target; |
| 258223a3 | 1394 | ahci_ata_cmd(xa); |
| f4553de1 | 1395 | ahci_os_unlock_port(ap); |
| 258223a3 MD |
1396 | } else { |
| 1397 | ahci_ata_put_xfer(xa); | |
| 1398 | xpt_done(ccb); | |
| 1399 | } | |
| 1400 | } | |
| 1401 | ||
| b4189e5e MD |
1402 | /* |
| 1403 | * Convert the SCSI command in ccb to an ata_xfer command in xa | |
| 1404 | * for ATA_PORT_T_ATAPI operations. Set the completion function | |
| 1405 | * to convert the response back, then dispatch to the OpenBSD AHCI | |
| 1406 | * layer. | |
| 1407 | */ | |
| 258223a3 MD |
1408 | static |
| 1409 | void | |
| 1980eff3 MD |
1410 | ahci_xpt_scsi_atapi_io(struct ahci_port *ap, struct ata_port *atx, |
| 1411 | union ccb *ccb) | |
| 258223a3 | 1412 | { |
| 258223a3 MD |
1413 | struct ccb_hdr *ccbh; |
| 1414 | struct ccb_scsiio *csio; | |
| 1415 | struct ata_xfer *xa; | |
| 1416 | struct ata_fis_h2d *fis; | |
| b4189e5e MD |
1417 | scsi_cdb_t cdbs; |
| 1418 | scsi_cdb_t cdbd; | |
| 1419 | int flags; | |
| 1980eff3 | 1420 | struct ata_port *at; |
| 258223a3 | 1421 | |
| 258223a3 MD |
1422 | ccbh = &ccb->csio.ccb_h; |
| 1423 | csio = &ccb->csio; | |
| b012a2ca | 1424 | at = atx ? atx : ap->ap_ata[0]; |
| b4189e5e MD |
1425 | |
| 1426 | switch (ccbh->flags & CAM_DIR_MASK) { | |
| 1427 | case CAM_DIR_IN: | |
| 1428 | flags = ATA_F_PACKET | ATA_F_READ; | |
| 1429 | break; | |
| 1430 | case CAM_DIR_OUT: | |
| 1431 | flags = ATA_F_PACKET | ATA_F_WRITE; | |
| 1432 | break; | |
| 1433 | case CAM_DIR_NONE: | |
| 1434 | flags = ATA_F_PACKET; | |
| 1435 | break; | |
| 1436 | default: | |
| 1437 | ccbh->status = CAM_REQ_INVALID; | |
| 1438 | xpt_done(ccb); | |
| 1439 | return; | |
| 1440 | /* NOT REACHED */ | |
| 1441 | } | |
| 1442 | ||
| 1443 | /* | |
| 12feb904 | 1444 | * Special handling to get the rfis back into host memory while |
| 192ee1d0 | 1445 | * still allowing the chip to run commands in parallel to |
| 12feb904 MD |
1446 | * ATAPI devices behind a PM. |
| 1447 | */ | |
| 1448 | flags |= ATA_F_AUTOSENSE; | |
| 1449 | ||
| 1450 | /* | |
| b4189e5e MD |
1451 | * The command has to fit in the packet command buffer. |
| 1452 | */ | |
| 1453 | if (csio->cdb_len < 6 || csio->cdb_len > 16) { | |
| 1454 | ccbh->status = CAM_CCB_LEN_ERR; | |
| 1455 | xpt_done(ccb); | |
| 1456 | return; | |
| 1457 | } | |
| 1458 | ||
| 1459 | /* | |
| 192ee1d0 MD |
1460 | * Initialize the XA and FIS. It is unclear how much of |
| 1461 | * this has to mimic the equivalent ATA command. | |
| 1980eff3 MD |
1462 | * |
| 1463 | * XXX not passing NULL at for direct attach! | |
| b4189e5e | 1464 | */ |
| 1980eff3 | 1465 | xa = ahci_ata_get_xfer(ap, atx); |
| 258223a3 MD |
1466 | fis = xa->fis; |
| 1467 | ||
| 1980eff3 | 1468 | fis->flags = ATA_H2D_FLAGS_CMD | at->at_target; |
| b4189e5e | 1469 | fis->command = ATA_C_PACKET; |
| 192ee1d0 | 1470 | fis->device = ATA_H2D_DEVICE_LBA; |
| b4189e5e | 1471 | fis->sector_count = xa->tag << 3; |
| 192ee1d0 MD |
1472 | if (flags & (ATA_F_READ | ATA_F_WRITE)) { |
| 1473 | if (flags & ATA_F_WRITE) { | |
| 1474 | fis->features = ATA_H2D_FEATURES_DMA | | |
| 1475 | ATA_H2D_FEATURES_DIR_WRITE; | |
| 1476 | } else { | |
| 1477 | fis->features = ATA_H2D_FEATURES_DMA | | |
| 1478 | ATA_H2D_FEATURES_DIR_READ; | |
| 1479 | } | |
| 1480 | } else { | |
| 1481 | fis->lba_mid = 0; | |
| 1482 | fis->lba_high = 0; | |
| 1483 | } | |
| 1484 | fis->control = ATA_FIS_CONTROL_4BIT; | |
| b4189e5e | 1485 | |
| 1980eff3 MD |
1486 | xa->flags = flags; |
| 1487 | xa->data = csio->data_ptr; | |
| 1488 | xa->datalen = csio->dxfer_len; | |
| 3209f581 | 1489 | xa->timeout = ccbh->timeout; /* milliseconds */ |
| 1980eff3 MD |
1490 | |
| 1491 | if (ccbh->flags & CAM_POLLED) | |
| 1492 | xa->flags |= ATA_F_POLL; | |
| 1493 | ||
| b4189e5e MD |
1494 | /* |
| 1495 | * Copy the cdb to the packetcmd buffer in the FIS using a | |
| 1496 | * convenient pointer in the xa. | |
| a7d90c87 MD |
1497 | * |
| 1498 | * Zero-out any trailing bytes in case the ATAPI device cares. | |
| b4189e5e MD |
1499 | */ |
| 1500 | cdbs = (void *)((ccbh->flags & CAM_CDB_POINTER) ? | |
| 258223a3 | 1501 | csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes); |
| b4189e5e | 1502 | bcopy(cdbs, xa->packetcmd, csio->cdb_len); |
| a7d90c87 MD |
1503 | if (csio->cdb_len < 16) |
| 1504 | bzero(xa->packetcmd + csio->cdb_len, 16 - csio->cdb_len); | |
| 258223a3 | 1505 | |
| 669fbbf7 | 1506 | #if 0 |
| b4189e5e MD |
1507 | kprintf("opcode %d cdb_len %d dxfer_len %d\n", |
| 1508 | cdbs->generic.opcode, | |
| 1509 | csio->cdb_len, csio->dxfer_len); | |
| 669fbbf7 | 1510 | #endif |
| b4189e5e MD |
1511 | |
| 1512 | /* | |
| 1513 | * Some ATAPI commands do not actually follow the SCSI standard. | |
| 1514 | */ | |
| 1515 | cdbd = (void *)xa->packetcmd; | |
| 1516 | ||
| 1517 | switch(cdbd->generic.opcode) { | |
| 192ee1d0 MD |
1518 | case REQUEST_SENSE: |
| 1519 | /* | |
| 1520 | * Force SENSE requests to the ATAPI sense length. | |
| 1521 | * | |
| 1522 | * It is unclear if this is needed or not. | |
| 1523 | */ | |
| 1524 | if (cdbd->sense.length == SSD_FULL_SIZE) { | |
| b012a2ca MD |
1525 | if (bootverbose) { |
| 1526 | kprintf("%s: Shortening sense request\n", | |
| 1527 | PORTNAME(ap)); | |
| 1528 | } | |
| 192ee1d0 MD |
1529 | cdbd->sense.length = offsetof(struct scsi_sense_data, |
| 1530 | extra_bytes[0]); | |
| 1531 | } | |
| 1532 | break; | |
| b4189e5e | 1533 | case INQUIRY: |
| 258223a3 | 1534 | /* |
| b4189e5e MD |
1535 | * Some ATAPI devices can't handle long inquiry lengths, |
| 1536 | * don't ask me why. Truncate the inquiry length. | |
| 258223a3 | 1537 | */ |
| b4189e5e MD |
1538 | if (cdbd->inquiry.page_code == 0 && |
| 1539 | cdbd->inquiry.length > SHORT_INQUIRY_LENGTH) { | |
| 1540 | cdbd->inquiry.length = SHORT_INQUIRY_LENGTH; | |
| 1541 | } | |
| 258223a3 | 1542 | break; |
| 258223a3 | 1543 | case READ_6: |
| 258223a3 | 1544 | case WRITE_6: |
| b4189e5e MD |
1545 | /* |
| 1546 | * Convert *_6 to *_10 commands. Most ATAPI devices | |
| 1547 | * cannot handle the SCSI READ_6 and WRITE_6 commands. | |
| 1548 | */ | |
| 1549 | cdbd->rw_10.opcode |= 0x20; | |
| 1550 | cdbd->rw_10.byte2 = 0; | |
| 1551 | cdbd->rw_10.addr[0] = cdbs->rw_6.addr[0] & 0x1F; | |
| 1552 | cdbd->rw_10.addr[1] = cdbs->rw_6.addr[1]; | |
| 1553 | cdbd->rw_10.addr[2] = cdbs->rw_6.addr[2]; | |
| 1554 | cdbd->rw_10.addr[3] = 0; | |
| 1555 | cdbd->rw_10.reserved = 0; | |
| 1556 | cdbd->rw_10.length[0] = 0; | |
| 1557 | cdbd->rw_10.length[1] = cdbs->rw_6.length; | |
| 1558 | cdbd->rw_10.control = cdbs->rw_6.control; | |
| 1559 | break; | |
| 258223a3 | 1560 | default: |
| 258223a3 MD |
1561 | break; |
| 1562 | } | |
| 1563 | ||
| b4189e5e MD |
1564 | /* |
| 1565 | * And dispatch | |
| 1566 | */ | |
| 1567 | xa->complete = ahci_atapi_complete_cmd; | |
| 1568 | xa->atascsi_private = ccb; | |
| 1569 | ccb->ccb_h.sim_priv.entries[0].ptr = ap; | |
| 76497a9c | 1570 | ahci_os_lock_port(ap); |
| b4189e5e | 1571 | ahci_ata_cmd(xa); |
| 76497a9c | 1572 | ahci_os_unlock_port(ap); |
| 258223a3 MD |
1573 | } |
| 1574 | ||
| b4189e5e | 1575 | /* |
| 6e0003ad MD |
1576 | * Simulate page inquiries for disk attachments. |
| 1577 | */ | |
| 1578 | static | |
| 1579 | void | |
| 1580 | ahci_xpt_page_inquiry(struct ahci_port *ap, struct ata_port *at, union ccb *ccb) | |
| 1581 | { | |
| 1582 | union { | |
| 1583 | struct scsi_vpd_supported_page_list list; | |
| 1584 | struct scsi_vpd_unit_serial_number serno; | |
| 1585 | struct scsi_vpd_unit_devid devid; | |
| 1586 | char buf[256]; | |
| 1587 | } *page; | |
| 1588 | scsi_cdb_t cdb; | |
| 1589 | int i; | |
| 1590 | int j; | |
| 1591 | int len; | |
| 1592 | ||
| 1593 | page = kmalloc(sizeof(*page), M_DEVBUF, M_WAITOK | M_ZERO); | |
| 1594 | ||
| 1595 | cdb = (void *)((ccb->ccb_h.flags & CAM_CDB_POINTER) ? | |
| 1596 | ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes); | |
| 1597 | ||
| 1598 | switch(cdb->inquiry.page_code) { | |
| 1599 | case SVPD_SUPPORTED_PAGE_LIST: | |
| 1600 | i = 0; | |
| 1601 | page->list.device = T_DIRECT; | |
| 1602 | page->list.page_code = SVPD_SUPPORTED_PAGE_LIST; | |
| 1603 | page->list.list[i++] = SVPD_SUPPORTED_PAGE_LIST; | |
| 1604 | page->list.list[i++] = SVPD_UNIT_SERIAL_NUMBER; | |
| 1605 | page->list.list[i++] = SVPD_UNIT_DEVID; | |
| 1606 | page->list.length = i; | |
| 1607 | len = offsetof(struct scsi_vpd_supported_page_list, list[3]); | |
| 1608 | break; | |
| 1609 | case SVPD_UNIT_SERIAL_NUMBER: | |
| 1610 | i = 0; | |
| 1611 | j = sizeof(at->at_identify.serial); | |
| 1612 | for (i = 0; i < j && at->at_identify.serial[i] == ' '; ++i) | |
| 1613 | ; | |
| 1614 | while (j > i && at->at_identify.serial[j-1] == ' ') | |
| 1615 | --j; | |
| 1616 | page->serno.device = T_DIRECT; | |
| 1617 | page->serno.page_code = SVPD_UNIT_SERIAL_NUMBER; | |
| 1618 | page->serno.length = j - i; | |
| 1619 | bcopy(at->at_identify.serial + i, | |
| 1620 | page->serno.serial_num, j - i); | |
| 1621 | len = offsetof(struct scsi_vpd_unit_serial_number, | |
| 1622 | serial_num[j-i]); | |
| 1623 | break; | |
| 1624 | case SVPD_UNIT_DEVID: | |
| 1625 | /* fall through for now */ | |
| 1626 | default: | |
| 1627 | ccb->ccb_h.status = CAM_FUNC_NOTAVAIL; | |
| 1628 | len = 0; | |
| 1629 | break; | |
| 1630 | } | |
| 1631 | if (ccb->ccb_h.status == CAM_REQ_INPROG) { | |
| 1632 | if (len <= ccb->csio.dxfer_len) { | |
| 1633 | ccb->ccb_h.status = CAM_REQ_CMP; | |
| 1634 | bzero(ccb->csio.data_ptr, ccb->csio.dxfer_len); | |
| 1635 | bcopy(page, ccb->csio.data_ptr, len); | |
| 1636 | ccb->csio.resid = ccb->csio.dxfer_len - len; | |
| 1637 | } else { | |
| 1638 | ccb->ccb_h.status = CAM_CCB_LEN_ERR; | |
| 1639 | } | |
| 1640 | } | |
| 1641 | kfree(page, M_DEVBUF); | |
| 1642 | } | |
| 1643 | ||
| 1644 | /* | |
| b4189e5e MD |
1645 | * Completion function for ATA_PORT_T_DISK cache synchronization. |
| 1646 | */ | |
| 258223a3 MD |
1647 | static |
| 1648 | void | |
| 1649 | ahci_ata_complete_disk_synchronize_cache(struct ata_xfer *xa) | |
| 1650 | { | |
| 1651 | union ccb *ccb = xa->atascsi_private; | |
| 1652 | struct ccb_hdr *ccbh = &ccb->ccb_h; | |
| 1653 | struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr; | |
| 1654 | ||
| 1655 | switch(xa->state) { | |
| 1656 | case ATA_S_COMPLETE: | |
| 1657 | ccbh->status = CAM_REQ_CMP; | |
| b4189e5e | 1658 | ccb->csio.scsi_status = SCSI_STATUS_OK; |
| 258223a3 MD |
1659 | break; |
| 1660 | case ATA_S_ERROR: | |
| 1980eff3 MD |
1661 | kprintf("%s: synchronize_cache: error\n", |
| 1662 | ATANAME(ap, xa->at)); | |
| b4189e5e MD |
1663 | ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID; |
| 1664 | ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND; | |
| 1665 | ahci_ata_dummy_sense(&ccb->csio.sense_data); | |
| 258223a3 MD |
1666 | break; |
| 1667 | case ATA_S_TIMEOUT: | |
| 1980eff3 MD |
1668 | kprintf("%s: synchronize_cache: timeout\n", |
| 1669 | ATANAME(ap, xa->at)); | |
| 258223a3 MD |
1670 | ccbh->status = CAM_CMD_TIMEOUT; |
| 1671 | break; | |
| 1672 | default: | |
| 1673 | kprintf("%s: synchronize_cache: unknown state %d\n", | |
| 1980eff3 | 1674 | ATANAME(ap, xa->at), xa->state); |
| 258223a3 MD |
1675 | ccbh->status = CAM_REQ_CMP_ERR; |
| 1676 | break; | |
| 1677 | } | |
| 1678 | ahci_ata_put_xfer(xa); | |
| f4553de1 | 1679 | ahci_os_unlock_port(ap); |
| 258223a3 | 1680 | xpt_done(ccb); |
| f4553de1 | 1681 | ahci_os_lock_port(ap); |
| 258223a3 MD |
1682 | } |
| 1683 | ||
| b4189e5e MD |
1684 | /* |
| 1685 | * Completion function for ATA_PORT_T_DISK I/O | |
| 1686 | */ | |
| 258223a3 MD |
1687 | static |
| 1688 | void | |
| 1689 | ahci_ata_complete_disk_rw(struct ata_xfer *xa) | |
| 1690 | { | |
| 1691 | union ccb *ccb = xa->atascsi_private; | |
| 1692 | struct ccb_hdr *ccbh = &ccb->ccb_h; | |
| 1693 | struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr; | |
| 1694 | ||
| 1695 | switch(xa->state) { | |
| 1696 | case ATA_S_COMPLETE: | |
| 1697 | ccbh->status = CAM_REQ_CMP; | |
| b4189e5e | 1698 | ccb->csio.scsi_status = SCSI_STATUS_OK; |
| 258223a3 MD |
1699 | break; |
| 1700 | case ATA_S_ERROR: | |
| 1980eff3 | 1701 | kprintf("%s: disk_rw: error\n", ATANAME(ap, xa->at)); |
| b4189e5e MD |
1702 | ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID; |
| 1703 | ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND; | |
| 1704 | ahci_ata_dummy_sense(&ccb->csio.sense_data); | |
| 258223a3 MD |
1705 | break; |
| 1706 | case ATA_S_TIMEOUT: | |
| 1980eff3 | 1707 | kprintf("%s: disk_rw: timeout\n", ATANAME(ap, xa->at)); |
| 258223a3 | 1708 | ccbh->status = CAM_CMD_TIMEOUT; |
| 4c339a5f MD |
1709 | ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND; |
| 1710 | ahci_ata_dummy_sense(&ccb->csio.sense_data); | |
| 258223a3 MD |
1711 | break; |
| 1712 | default: | |
| 1713 | kprintf("%s: disk_rw: unknown state %d\n", | |
| 1980eff3 | 1714 | ATANAME(ap, xa->at), xa->state); |
| 258223a3 MD |
1715 | ccbh->status = CAM_REQ_CMP_ERR; |
| 1716 | break; | |
| 1717 | } | |
| 1718 | ccb->csio.resid = xa->resid; | |
| 1719 | ahci_ata_put_xfer(xa); | |
| f4553de1 | 1720 | ahci_os_unlock_port(ap); |
| 258223a3 | 1721 | xpt_done(ccb); |
| f4553de1 | 1722 | ahci_os_lock_port(ap); |
| 258223a3 | 1723 | } |
| b4189e5e | 1724 | |
| 7d4fcf34 MD |
1725 | /* |
| 1726 | * Completion function for ATA_PORT_T_ATAPI I/O | |
| 1727 | * | |
| 1728 | * Sense data is returned in the rfis. | |
| 1729 | */ | |
| b4189e5e MD |
1730 | static |
| 1731 | void | |
| 1732 | ahci_atapi_complete_cmd(struct ata_xfer *xa) | |
| 1733 | { | |
| 1734 | union ccb *ccb = xa->atascsi_private; | |
| 1735 | struct ccb_hdr *ccbh = &ccb->ccb_h; | |
| 1736 | struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr; | |
| 1737 | scsi_cdb_t cdb; | |
| 1738 | ||
| 1739 | cdb = (void *)((ccb->ccb_h.flags & CAM_CDB_POINTER) ? | |
| 1740 | ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes); | |
| 1741 | ||
| 1742 | switch(xa->state) { | |
| 1743 | case ATA_S_COMPLETE: | |
| 1744 | ccbh->status = CAM_REQ_CMP; | |
| 1745 | ccb->csio.scsi_status = SCSI_STATUS_OK; | |
| 1746 | break; | |
| 1747 | case ATA_S_ERROR: | |
| b4189e5e MD |
1748 | ccbh->status = CAM_SCSI_STATUS_ERROR; |
| 1749 | ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND; | |
| 7d4fcf34 | 1750 | ahci_ata_atapi_sense(&xa->rfis, &ccb->csio.sense_data); |
| b4189e5e MD |
1751 | break; |
| 1752 | case ATA_S_TIMEOUT: | |
| 1753 | kprintf("%s: cmd %d: timeout\n", | |
| 1754 | PORTNAME(ap), cdb->generic.opcode); | |
| 1755 | ccbh->status = CAM_CMD_TIMEOUT; | |
| 4c339a5f MD |
1756 | ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND; |
| 1757 | ahci_ata_dummy_sense(&ccb->csio.sense_data); | |
| b4189e5e MD |
1758 | break; |
| 1759 | default: | |
| 1760 | kprintf("%s: cmd %d: unknown state %d\n", | |
| 1761 | PORTNAME(ap), cdb->generic.opcode, xa->state); | |
| 1762 | ccbh->status = CAM_REQ_CMP_ERR; | |
| 1763 | break; | |
| 1764 | } | |
| 1765 | ccb->csio.resid = xa->resid; | |
| 1766 | ahci_ata_put_xfer(xa); | |
| f4553de1 | 1767 | ahci_os_unlock_port(ap); |
| b4189e5e | 1768 | xpt_done(ccb); |
| f4553de1 | 1769 | ahci_os_lock_port(ap); |
| b4189e5e MD |
1770 | } |
| 1771 | ||
| 7d4fcf34 MD |
1772 | /* |
| 1773 | * Construct dummy sense data for errors on DISKs | |
| 1774 | */ | |
| b4189e5e MD |
1775 | static |
| 1776 | void | |
| 1777 | ahci_ata_dummy_sense(struct scsi_sense_data *sense_data) | |
| 1778 | { | |
| 1779 | sense_data->error_code = SSD_ERRCODE_VALID | SSD_CURRENT_ERROR; | |
| 1780 | sense_data->segment = 0; | |
| 1781 | sense_data->flags = SSD_KEY_MEDIUM_ERROR; | |
| 1782 | sense_data->info[0] = 0; | |
| 1783 | sense_data->info[1] = 0; | |
| 1784 | sense_data->info[2] = 0; | |
| 1785 | sense_data->info[3] = 0; | |
| 1786 | sense_data->extra_len = 0; | |
| 1787 | } | |
| 7d4fcf34 MD |
1788 | |
| 1789 | /* | |
| 1790 | * Construct atapi sense data for errors on ATAPI | |
| 1791 | * | |
| 1792 | * The ATAPI sense data is stored in the passed rfis and must be converted | |
| 1793 | * to SCSI sense data. | |
| 1794 | */ | |
| 1795 | static | |
| 1796 | void | |
| 1797 | ahci_ata_atapi_sense(struct ata_fis_d2h *rfis, | |
| 1798 | struct scsi_sense_data *sense_data) | |
| 1799 | { | |
| 1800 | sense_data->error_code = SSD_ERRCODE_VALID | SSD_CURRENT_ERROR; | |
| 1801 | sense_data->segment = 0; | |
| 1802 | sense_data->flags = (rfis->error & 0xF0) >> 4; | |
| 1803 | if (rfis->error & 0x04) | |
| 1804 | sense_data->flags |= SSD_KEY_ILLEGAL_REQUEST; | |
| 1805 | if (rfis->error & 0x02) | |
| 1806 | sense_data->flags |= SSD_EOM; | |
| 1807 | if (rfis->error & 0x01) | |
| 1808 | sense_data->flags |= SSD_ILI; | |
| 1809 | sense_data->info[0] = 0; | |
| 1810 | sense_data->info[1] = 0; | |
| 1811 | sense_data->info[2] = 0; | |
| 1812 | sense_data->info[3] = 0; | |
| 1813 | sense_data->extra_len = 0; | |
| 1814 | } | |
| 6e0003ad MD |
1815 | |
| 1816 | static | |
| 1817 | void | |
| 1818 | ahci_strip_string(const char **basep, int *lenp) | |
| 1819 | { | |
| 1820 | const char *base = *basep; | |
| 1821 | int len = *lenp; | |
| 1822 | ||
| 1823 | while (len && (*base == 0 || *base == ' ')) { | |
| 1824 | --len; | |
| 1825 | ++base; | |
| 1826 | } | |
| 1827 | while (len && (base[len-1] == 0 || base[len-1] == ' ')) | |
| 1828 | --len; | |
| 1829 | *basep = base; | |
| 1830 | *lenp = len; | |
| 1831 | } |