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