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