1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (C) 2012-2014 Intel Corporation 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/bus.h> 34 35 #include <dev/pci/pcivar.h> 36 37 #include "nvme_private.h" 38 39 static void _nvme_qpair_submit_request(struct nvme_qpair *qpair, 40 struct nvme_request *req); 41 static void nvme_qpair_destroy(struct nvme_qpair *qpair); 42 43 struct nvme_opcode_string { 44 45 uint16_t opc; 46 const char * str; 47 }; 48 49 static struct nvme_opcode_string admin_opcode[] = { 50 { NVME_OPC_DELETE_IO_SQ, "DELETE IO SQ" }, 51 { NVME_OPC_CREATE_IO_SQ, "CREATE IO SQ" }, 52 { NVME_OPC_GET_LOG_PAGE, "GET LOG PAGE" }, 53 { NVME_OPC_DELETE_IO_CQ, "DELETE IO CQ" }, 54 { NVME_OPC_CREATE_IO_CQ, "CREATE IO CQ" }, 55 { NVME_OPC_IDENTIFY, "IDENTIFY" }, 56 { NVME_OPC_ABORT, "ABORT" }, 57 { NVME_OPC_SET_FEATURES, "SET FEATURES" }, 58 { NVME_OPC_GET_FEATURES, "GET FEATURES" }, 59 { NVME_OPC_ASYNC_EVENT_REQUEST, "ASYNC EVENT REQUEST" }, 60 { NVME_OPC_FIRMWARE_ACTIVATE, "FIRMWARE ACTIVATE" }, 61 { NVME_OPC_FIRMWARE_IMAGE_DOWNLOAD, "FIRMWARE IMAGE DOWNLOAD" }, 62 { NVME_OPC_DEVICE_SELF_TEST, "DEVICE SELF-TEST" }, 63 { NVME_OPC_NAMESPACE_ATTACHMENT, "NAMESPACE ATTACHMENT" }, 64 { NVME_OPC_KEEP_ALIVE, "KEEP ALIVE" }, 65 { NVME_OPC_DIRECTIVE_SEND, "DIRECTIVE SEND" }, 66 { NVME_OPC_DIRECTIVE_RECEIVE, "DIRECTIVE RECEIVE" }, 67 { NVME_OPC_VIRTUALIZATION_MANAGEMENT, "VIRTUALIZATION MANAGEMENT" }, 68 { NVME_OPC_NVME_MI_SEND, "NVME-MI SEND" }, 69 { NVME_OPC_NVME_MI_RECEIVE, "NVME-MI RECEIVE" }, 70 { NVME_OPC_DOORBELL_BUFFER_CONFIG, "DOORBELL BUFFER CONFIG" }, 71 { NVME_OPC_FORMAT_NVM, "FORMAT NVM" }, 72 { NVME_OPC_SECURITY_SEND, "SECURITY SEND" }, 73 { NVME_OPC_SECURITY_RECEIVE, "SECURITY RECEIVE" }, 74 { NVME_OPC_SANITIZE, "SANITIZE" }, 75 { 0xFFFF, "ADMIN COMMAND" } 76 }; 77 78 static struct nvme_opcode_string io_opcode[] = { 79 { NVME_OPC_FLUSH, "FLUSH" }, 80 { NVME_OPC_WRITE, "WRITE" }, 81 { NVME_OPC_READ, "READ" }, 82 { NVME_OPC_WRITE_UNCORRECTABLE, "WRITE UNCORRECTABLE" }, 83 { NVME_OPC_COMPARE, "COMPARE" }, 84 { NVME_OPC_WRITE_ZEROES, "WRITE ZEROES" }, 85 { NVME_OPC_DATASET_MANAGEMENT, "DATASET MANAGEMENT" }, 86 { NVME_OPC_RESERVATION_REGISTER, "RESERVATION REGISTER" }, 87 { NVME_OPC_RESERVATION_REPORT, "RESERVATION REPORT" }, 88 { NVME_OPC_RESERVATION_ACQUIRE, "RESERVATION ACQUIRE" }, 89 { NVME_OPC_RESERVATION_RELEASE, "RESERVATION RELEASE" }, 90 { 0xFFFF, "IO COMMAND" } 91 }; 92 93 static const char * 94 get_admin_opcode_string(uint16_t opc) 95 { 96 struct nvme_opcode_string *entry; 97 98 entry = admin_opcode; 99 100 while (entry->opc != 0xFFFF) { 101 if (entry->opc == opc) 102 return (entry->str); 103 entry++; 104 } 105 return (entry->str); 106 } 107 108 static const char * 109 get_io_opcode_string(uint16_t opc) 110 { 111 struct nvme_opcode_string *entry; 112 113 entry = io_opcode; 114 115 while (entry->opc != 0xFFFF) { 116 if (entry->opc == opc) 117 return (entry->str); 118 entry++; 119 } 120 return (entry->str); 121 } 122 123 124 static void 125 nvme_admin_qpair_print_command(struct nvme_qpair *qpair, 126 struct nvme_command *cmd) 127 { 128 129 nvme_printf(qpair->ctrlr, "%s (%02x) sqid:%d cid:%d nsid:%x " 130 "cdw10:%08x cdw11:%08x\n", 131 get_admin_opcode_string(cmd->opc), cmd->opc, qpair->id, cmd->cid, 132 le32toh(cmd->nsid), le32toh(cmd->cdw10), le32toh(cmd->cdw11)); 133 } 134 135 static void 136 nvme_io_qpair_print_command(struct nvme_qpair *qpair, 137 struct nvme_command *cmd) 138 { 139 140 switch (cmd->opc) { 141 case NVME_OPC_WRITE: 142 case NVME_OPC_READ: 143 case NVME_OPC_WRITE_UNCORRECTABLE: 144 case NVME_OPC_COMPARE: 145 case NVME_OPC_WRITE_ZEROES: 146 nvme_printf(qpair->ctrlr, "%s sqid:%d cid:%d nsid:%d " 147 "lba:%llu len:%d\n", 148 get_io_opcode_string(cmd->opc), qpair->id, cmd->cid, le32toh(cmd->nsid), 149 ((unsigned long long)le32toh(cmd->cdw11) << 32) + le32toh(cmd->cdw10), 150 (le32toh(cmd->cdw12) & 0xFFFF) + 1); 151 break; 152 case NVME_OPC_FLUSH: 153 case NVME_OPC_DATASET_MANAGEMENT: 154 case NVME_OPC_RESERVATION_REGISTER: 155 case NVME_OPC_RESERVATION_REPORT: 156 case NVME_OPC_RESERVATION_ACQUIRE: 157 case NVME_OPC_RESERVATION_RELEASE: 158 nvme_printf(qpair->ctrlr, "%s sqid:%d cid:%d nsid:%d\n", 159 get_io_opcode_string(cmd->opc), qpair->id, cmd->cid, le32toh(cmd->nsid)); 160 break; 161 default: 162 nvme_printf(qpair->ctrlr, "%s (%02x) sqid:%d cid:%d nsid:%d\n", 163 get_io_opcode_string(cmd->opc), cmd->opc, qpair->id, 164 cmd->cid, le32toh(cmd->nsid)); 165 break; 166 } 167 } 168 169 static void 170 nvme_qpair_print_command(struct nvme_qpair *qpair, struct nvme_command *cmd) 171 { 172 if (qpair->id == 0) 173 nvme_admin_qpair_print_command(qpair, cmd); 174 else 175 nvme_io_qpair_print_command(qpair, cmd); 176 } 177 178 struct nvme_status_string { 179 180 uint16_t sc; 181 const char * str; 182 }; 183 184 static struct nvme_status_string generic_status[] = { 185 { NVME_SC_SUCCESS, "SUCCESS" }, 186 { NVME_SC_INVALID_OPCODE, "INVALID OPCODE" }, 187 { NVME_SC_INVALID_FIELD, "INVALID_FIELD" }, 188 { NVME_SC_COMMAND_ID_CONFLICT, "COMMAND ID CONFLICT" }, 189 { NVME_SC_DATA_TRANSFER_ERROR, "DATA TRANSFER ERROR" }, 190 { NVME_SC_ABORTED_POWER_LOSS, "ABORTED - POWER LOSS" }, 191 { NVME_SC_INTERNAL_DEVICE_ERROR, "INTERNAL DEVICE ERROR" }, 192 { NVME_SC_ABORTED_BY_REQUEST, "ABORTED - BY REQUEST" }, 193 { NVME_SC_ABORTED_SQ_DELETION, "ABORTED - SQ DELETION" }, 194 { NVME_SC_ABORTED_FAILED_FUSED, "ABORTED - FAILED FUSED" }, 195 { NVME_SC_ABORTED_MISSING_FUSED, "ABORTED - MISSING FUSED" }, 196 { NVME_SC_INVALID_NAMESPACE_OR_FORMAT, "INVALID NAMESPACE OR FORMAT" }, 197 { NVME_SC_COMMAND_SEQUENCE_ERROR, "COMMAND SEQUENCE ERROR" }, 198 { NVME_SC_INVALID_SGL_SEGMENT_DESCR, "INVALID SGL SEGMENT DESCRIPTOR" }, 199 { NVME_SC_INVALID_NUMBER_OF_SGL_DESCR, "INVALID NUMBER OF SGL DESCRIPTORS" }, 200 { NVME_SC_DATA_SGL_LENGTH_INVALID, "DATA SGL LENGTH INVALID" }, 201 { NVME_SC_METADATA_SGL_LENGTH_INVALID, "METADATA SGL LENGTH INVALID" }, 202 { NVME_SC_SGL_DESCRIPTOR_TYPE_INVALID, "SGL DESCRIPTOR TYPE INVALID" }, 203 { NVME_SC_INVALID_USE_OF_CMB, "INVALID USE OF CONTROLLER MEMORY BUFFER" }, 204 { NVME_SC_PRP_OFFET_INVALID, "PRP OFFET INVALID" }, 205 { NVME_SC_ATOMIC_WRITE_UNIT_EXCEEDED, "ATOMIC WRITE UNIT EXCEEDED" }, 206 { NVME_SC_OPERATION_DENIED, "OPERATION DENIED" }, 207 { NVME_SC_SGL_OFFSET_INVALID, "SGL OFFSET INVALID" }, 208 { NVME_SC_HOST_ID_INCONSISTENT_FORMAT, "HOST IDENTIFIER INCONSISTENT FORMAT" }, 209 { NVME_SC_KEEP_ALIVE_TIMEOUT_EXPIRED, "KEEP ALIVE TIMEOUT EXPIRED" }, 210 { NVME_SC_KEEP_ALIVE_TIMEOUT_INVALID, "KEEP ALIVE TIMEOUT INVALID" }, 211 { NVME_SC_ABORTED_DUE_TO_PREEMPT, "COMMAND ABORTED DUE TO PREEMPT AND ABORT" }, 212 { NVME_SC_SANITIZE_FAILED, "SANITIZE FAILED" }, 213 { NVME_SC_SANITIZE_IN_PROGRESS, "SANITIZE IN PROGRESS" }, 214 { NVME_SC_SGL_DATA_BLOCK_GRAN_INVALID, "SGL_DATA_BLOCK_GRANULARITY_INVALID" }, 215 { NVME_SC_NOT_SUPPORTED_IN_CMB, "COMMAND NOT SUPPORTED FOR QUEUE IN CMB" }, 216 217 { NVME_SC_LBA_OUT_OF_RANGE, "LBA OUT OF RANGE" }, 218 { NVME_SC_CAPACITY_EXCEEDED, "CAPACITY EXCEEDED" }, 219 { NVME_SC_NAMESPACE_NOT_READY, "NAMESPACE NOT READY" }, 220 { NVME_SC_RESERVATION_CONFLICT, "RESERVATION CONFLICT" }, 221 { NVME_SC_FORMAT_IN_PROGRESS, "FORMAT IN PROGRESS" }, 222 { 0xFFFF, "GENERIC" } 223 }; 224 225 static struct nvme_status_string command_specific_status[] = { 226 { NVME_SC_COMPLETION_QUEUE_INVALID, "INVALID COMPLETION QUEUE" }, 227 { NVME_SC_INVALID_QUEUE_IDENTIFIER, "INVALID QUEUE IDENTIFIER" }, 228 { NVME_SC_MAXIMUM_QUEUE_SIZE_EXCEEDED, "MAX QUEUE SIZE EXCEEDED" }, 229 { NVME_SC_ABORT_COMMAND_LIMIT_EXCEEDED, "ABORT CMD LIMIT EXCEEDED" }, 230 { NVME_SC_ASYNC_EVENT_REQUEST_LIMIT_EXCEEDED, "ASYNC LIMIT EXCEEDED" }, 231 { NVME_SC_INVALID_FIRMWARE_SLOT, "INVALID FIRMWARE SLOT" }, 232 { NVME_SC_INVALID_FIRMWARE_IMAGE, "INVALID FIRMWARE IMAGE" }, 233 { NVME_SC_INVALID_INTERRUPT_VECTOR, "INVALID INTERRUPT VECTOR" }, 234 { NVME_SC_INVALID_LOG_PAGE, "INVALID LOG PAGE" }, 235 { NVME_SC_INVALID_FORMAT, "INVALID FORMAT" }, 236 { NVME_SC_FIRMWARE_REQUIRES_RESET, "FIRMWARE REQUIRES RESET" }, 237 { NVME_SC_INVALID_QUEUE_DELETION, "INVALID QUEUE DELETION" }, 238 { NVME_SC_FEATURE_NOT_SAVEABLE, "FEATURE IDENTIFIER NOT SAVEABLE" }, 239 { NVME_SC_FEATURE_NOT_CHANGEABLE, "FEATURE NOT CHANGEABLE" }, 240 { NVME_SC_FEATURE_NOT_NS_SPECIFIC, "FEATURE NOT NAMESPACE SPECIFIC" }, 241 { NVME_SC_FW_ACT_REQUIRES_NVMS_RESET, "FIRMWARE ACTIVATION REQUIRES NVM SUBSYSTEM RESET" }, 242 { NVME_SC_FW_ACT_REQUIRES_RESET, "FIRMWARE ACTIVATION REQUIRES RESET" }, 243 { NVME_SC_FW_ACT_REQUIRES_TIME, "FIRMWARE ACTIVATION REQUIRES MAXIMUM TIME VIOLATION" }, 244 { NVME_SC_FW_ACT_PROHIBITED, "FIRMWARE ACTIVATION PROHIBITED" }, 245 { NVME_SC_OVERLAPPING_RANGE, "OVERLAPPING RANGE" }, 246 { NVME_SC_NS_INSUFFICIENT_CAPACITY, "NAMESPACE INSUFFICIENT CAPACITY" }, 247 { NVME_SC_NS_ID_UNAVAILABLE, "NAMESPACE IDENTIFIER UNAVAILABLE" }, 248 { NVME_SC_NS_ALREADY_ATTACHED, "NAMESPACE ALREADY ATTACHED" }, 249 { NVME_SC_NS_IS_PRIVATE, "NAMESPACE IS PRIVATE" }, 250 { NVME_SC_NS_NOT_ATTACHED, "NS NOT ATTACHED" }, 251 { NVME_SC_THIN_PROV_NOT_SUPPORTED, "THIN PROVISIONING NOT SUPPORTED" }, 252 { NVME_SC_CTRLR_LIST_INVALID, "CONTROLLER LIST INVALID" }, 253 { NVME_SC_SELT_TEST_IN_PROGRESS, "DEVICE SELT-TEST IN PROGRESS" }, 254 { NVME_SC_BOOT_PART_WRITE_PROHIB, "BOOT PARTITION WRITE PROHIBITED" }, 255 { NVME_SC_INVALID_CTRLR_ID, "INVALID CONTROLLER IDENTIFIER" }, 256 { NVME_SC_INVALID_SEC_CTRLR_STATE, "INVALID SECONDARY CONTROLLER STATE" }, 257 { NVME_SC_INVALID_NUM_OF_CTRLR_RESRC, "INVALID NUMBER OF CONTROLLER RESOURCES" }, 258 { NVME_SC_INVALID_RESOURCE_ID, "INVALID RESOURCE IDENTIFIER" }, 259 260 { NVME_SC_CONFLICTING_ATTRIBUTES, "CONFLICTING ATTRIBUTES" }, 261 { NVME_SC_INVALID_PROTECTION_INFO, "INVALID PROTECTION INFO" }, 262 { NVME_SC_ATTEMPTED_WRITE_TO_RO_PAGE, "WRITE TO RO PAGE" }, 263 { 0xFFFF, "COMMAND SPECIFIC" } 264 }; 265 266 static struct nvme_status_string media_error_status[] = { 267 { NVME_SC_WRITE_FAULTS, "WRITE FAULTS" }, 268 { NVME_SC_UNRECOVERED_READ_ERROR, "UNRECOVERED READ ERROR" }, 269 { NVME_SC_GUARD_CHECK_ERROR, "GUARD CHECK ERROR" }, 270 { NVME_SC_APPLICATION_TAG_CHECK_ERROR, "APPLICATION TAG CHECK ERROR" }, 271 { NVME_SC_REFERENCE_TAG_CHECK_ERROR, "REFERENCE TAG CHECK ERROR" }, 272 { NVME_SC_COMPARE_FAILURE, "COMPARE FAILURE" }, 273 { NVME_SC_ACCESS_DENIED, "ACCESS DENIED" }, 274 { NVME_SC_DEALLOCATED_OR_UNWRITTEN, "DEALLOCATED OR UNWRITTEN LOGICAL BLOCK" }, 275 { 0xFFFF, "MEDIA ERROR" } 276 }; 277 278 static const char * 279 get_status_string(uint16_t sct, uint16_t sc) 280 { 281 struct nvme_status_string *entry; 282 283 switch (sct) { 284 case NVME_SCT_GENERIC: 285 entry = generic_status; 286 break; 287 case NVME_SCT_COMMAND_SPECIFIC: 288 entry = command_specific_status; 289 break; 290 case NVME_SCT_MEDIA_ERROR: 291 entry = media_error_status; 292 break; 293 case NVME_SCT_VENDOR_SPECIFIC: 294 return ("VENDOR SPECIFIC"); 295 default: 296 return ("RESERVED"); 297 } 298 299 while (entry->sc != 0xFFFF) { 300 if (entry->sc == sc) 301 return (entry->str); 302 entry++; 303 } 304 return (entry->str); 305 } 306 307 static void 308 nvme_qpair_print_completion(struct nvme_qpair *qpair, 309 struct nvme_completion *cpl) 310 { 311 uint16_t sct, sc; 312 313 sct = NVME_STATUS_GET_SCT(cpl->status); 314 sc = NVME_STATUS_GET_SC(cpl->status); 315 316 nvme_printf(qpair->ctrlr, "%s (%02x/%02x) sqid:%d cid:%d cdw0:%x\n", 317 get_status_string(sct, sc), sct, sc, cpl->sqid, cpl->cid, 318 cpl->cdw0); 319 } 320 321 static boolean_t 322 nvme_completion_is_retry(const struct nvme_completion *cpl) 323 { 324 uint8_t sct, sc, dnr; 325 326 sct = NVME_STATUS_GET_SCT(cpl->status); 327 sc = NVME_STATUS_GET_SC(cpl->status); 328 dnr = NVME_STATUS_GET_DNR(cpl->status); 329 330 /* 331 * TODO: spec is not clear how commands that are aborted due 332 * to TLER will be marked. So for now, it seems 333 * NAMESPACE_NOT_READY is the only case where we should 334 * look at the DNR bit. 335 */ 336 switch (sct) { 337 case NVME_SCT_GENERIC: 338 switch (sc) { 339 case NVME_SC_ABORTED_BY_REQUEST: 340 case NVME_SC_NAMESPACE_NOT_READY: 341 if (dnr) 342 return (0); 343 else 344 return (1); 345 case NVME_SC_INVALID_OPCODE: 346 case NVME_SC_INVALID_FIELD: 347 case NVME_SC_COMMAND_ID_CONFLICT: 348 case NVME_SC_DATA_TRANSFER_ERROR: 349 case NVME_SC_ABORTED_POWER_LOSS: 350 case NVME_SC_INTERNAL_DEVICE_ERROR: 351 case NVME_SC_ABORTED_SQ_DELETION: 352 case NVME_SC_ABORTED_FAILED_FUSED: 353 case NVME_SC_ABORTED_MISSING_FUSED: 354 case NVME_SC_INVALID_NAMESPACE_OR_FORMAT: 355 case NVME_SC_COMMAND_SEQUENCE_ERROR: 356 case NVME_SC_LBA_OUT_OF_RANGE: 357 case NVME_SC_CAPACITY_EXCEEDED: 358 default: 359 return (0); 360 } 361 case NVME_SCT_COMMAND_SPECIFIC: 362 case NVME_SCT_MEDIA_ERROR: 363 case NVME_SCT_VENDOR_SPECIFIC: 364 default: 365 return (0); 366 } 367 } 368 369 static void 370 nvme_qpair_complete_tracker(struct nvme_qpair *qpair, struct nvme_tracker *tr, 371 struct nvme_completion *cpl, boolean_t print_on_error) 372 { 373 struct nvme_request *req; 374 boolean_t retry, error; 375 376 req = tr->req; 377 error = nvme_completion_is_error(cpl); 378 retry = error && nvme_completion_is_retry(cpl) && 379 req->retries < nvme_retry_count; 380 381 if (error && print_on_error) { 382 nvme_qpair_print_command(qpair, &req->cmd); 383 nvme_qpair_print_completion(qpair, cpl); 384 } 385 386 qpair->act_tr[cpl->cid] = NULL; 387 388 KASSERT(cpl->cid == req->cmd.cid, ("cpl cid does not match cmd cid\n")); 389 390 if (req->cb_fn && !retry) 391 req->cb_fn(req->cb_arg, cpl); 392 393 mtx_lock(&qpair->lock); 394 callout_stop(&tr->timer); 395 396 if (retry) { 397 req->retries++; 398 nvme_qpair_submit_tracker(qpair, tr); 399 } else { 400 if (req->type != NVME_REQUEST_NULL) { 401 bus_dmamap_sync(qpair->dma_tag_payload, 402 tr->payload_dma_map, 403 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 404 bus_dmamap_unload(qpair->dma_tag_payload, 405 tr->payload_dma_map); 406 } 407 408 nvme_free_request(req); 409 tr->req = NULL; 410 411 TAILQ_REMOVE(&qpair->outstanding_tr, tr, tailq); 412 TAILQ_INSERT_HEAD(&qpair->free_tr, tr, tailq); 413 414 /* 415 * If the controller is in the middle of resetting, don't 416 * try to submit queued requests here - let the reset logic 417 * handle that instead. 418 */ 419 if (!STAILQ_EMPTY(&qpair->queued_req) && 420 !qpair->ctrlr->is_resetting) { 421 req = STAILQ_FIRST(&qpair->queued_req); 422 STAILQ_REMOVE_HEAD(&qpair->queued_req, stailq); 423 _nvme_qpair_submit_request(qpair, req); 424 } 425 } 426 427 mtx_unlock(&qpair->lock); 428 } 429 430 static void 431 nvme_qpair_manual_complete_tracker(struct nvme_qpair *qpair, 432 struct nvme_tracker *tr, uint32_t sct, uint32_t sc, uint32_t dnr, 433 boolean_t print_on_error) 434 { 435 struct nvme_completion cpl; 436 437 memset(&cpl, 0, sizeof(cpl)); 438 cpl.sqid = qpair->id; 439 cpl.cid = tr->cid; 440 cpl.status |= (sct & NVME_STATUS_SCT_MASK) << NVME_STATUS_SCT_SHIFT; 441 cpl.status |= (sc & NVME_STATUS_SC_MASK) << NVME_STATUS_SC_SHIFT; 442 cpl.status |= (dnr & NVME_STATUS_DNR_MASK) << NVME_STATUS_DNR_SHIFT; 443 nvme_qpair_complete_tracker(qpair, tr, &cpl, print_on_error); 444 } 445 446 void 447 nvme_qpair_manual_complete_request(struct nvme_qpair *qpair, 448 struct nvme_request *req, uint32_t sct, uint32_t sc, 449 boolean_t print_on_error) 450 { 451 struct nvme_completion cpl; 452 boolean_t error; 453 454 memset(&cpl, 0, sizeof(cpl)); 455 cpl.sqid = qpair->id; 456 cpl.status |= (sct & NVME_STATUS_SCT_MASK) << NVME_STATUS_SCT_SHIFT; 457 cpl.status |= (sc & NVME_STATUS_SC_MASK) << NVME_STATUS_SC_SHIFT; 458 459 error = nvme_completion_is_error(&cpl); 460 461 if (error && print_on_error) { 462 nvme_qpair_print_command(qpair, &req->cmd); 463 nvme_qpair_print_completion(qpair, &cpl); 464 } 465 466 if (req->cb_fn) 467 req->cb_fn(req->cb_arg, &cpl); 468 469 nvme_free_request(req); 470 } 471 472 bool 473 nvme_qpair_process_completions(struct nvme_qpair *qpair) 474 { 475 struct nvme_tracker *tr; 476 struct nvme_completion cpl; 477 int done = 0; 478 479 qpair->num_intr_handler_calls++; 480 481 if (!qpair->is_enabled) 482 /* 483 * qpair is not enabled, likely because a controller reset is 484 * is in progress. Ignore the interrupt - any I/O that was 485 * associated with this interrupt will get retried when the 486 * reset is complete. 487 */ 488 return (false); 489 490 bus_dmamap_sync(qpair->dma_tag, qpair->queuemem_map, 491 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 492 while (1) { 493 cpl = qpair->cpl[qpair->cq_head]; 494 495 /* Convert to host endian */ 496 nvme_completion_swapbytes(&cpl); 497 498 if (NVME_STATUS_GET_P(cpl.status) != qpair->phase) 499 break; 500 501 tr = qpair->act_tr[cpl.cid]; 502 503 if (tr != NULL) { 504 nvme_qpair_complete_tracker(qpair, tr, &cpl, TRUE); 505 qpair->sq_head = cpl.sqhd; 506 done++; 507 } else { 508 nvme_printf(qpair->ctrlr, 509 "cpl does not map to outstanding cmd\n"); 510 /* nvme_dump_completion expects device endianess */ 511 nvme_dump_completion(&qpair->cpl[qpair->cq_head]); 512 KASSERT(0, ("received completion for unknown cmd\n")); 513 } 514 515 if (++qpair->cq_head == qpair->num_entries) { 516 qpair->cq_head = 0; 517 qpair->phase = !qpair->phase; 518 } 519 520 nvme_mmio_write_4(qpair->ctrlr, doorbell[qpair->id].cq_hdbl, 521 qpair->cq_head); 522 } 523 return (done != 0); 524 } 525 526 static void 527 nvme_qpair_msix_handler(void *arg) 528 { 529 struct nvme_qpair *qpair = arg; 530 531 nvme_qpair_process_completions(qpair); 532 } 533 534 int 535 nvme_qpair_construct(struct nvme_qpair *qpair, uint32_t id, 536 uint16_t vector, uint32_t num_entries, uint32_t num_trackers, 537 struct nvme_controller *ctrlr) 538 { 539 struct nvme_tracker *tr; 540 size_t cmdsz, cplsz, prpsz, allocsz, prpmemsz; 541 uint64_t queuemem_phys, prpmem_phys, list_phys; 542 uint8_t *queuemem, *prpmem, *prp_list; 543 int i, err; 544 545 qpair->id = id; 546 qpair->vector = vector; 547 qpair->num_entries = num_entries; 548 qpair->num_trackers = num_trackers; 549 qpair->ctrlr = ctrlr; 550 551 if (ctrlr->msix_enabled) { 552 553 /* 554 * MSI-X vector resource IDs start at 1, so we add one to 555 * the queue's vector to get the corresponding rid to use. 556 */ 557 qpair->rid = vector + 1; 558 559 qpair->res = bus_alloc_resource_any(ctrlr->dev, SYS_RES_IRQ, 560 &qpair->rid, RF_ACTIVE); 561 bus_setup_intr(ctrlr->dev, qpair->res, 562 INTR_TYPE_MISC | INTR_MPSAFE, NULL, 563 nvme_qpair_msix_handler, qpair, &qpair->tag); 564 } 565 566 mtx_init(&qpair->lock, "nvme qpair lock", NULL, MTX_DEF); 567 568 /* Note: NVMe PRP format is restricted to 4-byte alignment. */ 569 err = bus_dma_tag_create(bus_get_dma_tag(ctrlr->dev), 570 4, PAGE_SIZE, BUS_SPACE_MAXADDR, 571 BUS_SPACE_MAXADDR, NULL, NULL, NVME_MAX_XFER_SIZE, 572 (NVME_MAX_XFER_SIZE/PAGE_SIZE)+1, PAGE_SIZE, 0, 573 NULL, NULL, &qpair->dma_tag_payload); 574 if (err != 0) { 575 nvme_printf(ctrlr, "payload tag create failed %d\n", err); 576 goto out; 577 } 578 579 /* 580 * Each component must be page aligned, and individual PRP lists 581 * cannot cross a page boundary. 582 */ 583 cmdsz = qpair->num_entries * sizeof(struct nvme_command); 584 cmdsz = roundup2(cmdsz, PAGE_SIZE); 585 cplsz = qpair->num_entries * sizeof(struct nvme_completion); 586 cplsz = roundup2(cplsz, PAGE_SIZE); 587 prpsz = sizeof(uint64_t) * NVME_MAX_PRP_LIST_ENTRIES;; 588 prpmemsz = qpair->num_trackers * prpsz; 589 allocsz = cmdsz + cplsz + prpmemsz; 590 591 err = bus_dma_tag_create(bus_get_dma_tag(ctrlr->dev), 592 PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, 593 allocsz, 1, allocsz, 0, NULL, NULL, &qpair->dma_tag); 594 if (err != 0) { 595 nvme_printf(ctrlr, "tag create failed %d\n", err); 596 goto out; 597 } 598 599 if (bus_dmamem_alloc(qpair->dma_tag, (void **)&queuemem, 600 BUS_DMA_NOWAIT, &qpair->queuemem_map)) { 601 nvme_printf(ctrlr, "failed to alloc qpair memory\n"); 602 goto out; 603 } 604 605 if (bus_dmamap_load(qpair->dma_tag, qpair->queuemem_map, 606 queuemem, allocsz, nvme_single_map, &queuemem_phys, 0) != 0) { 607 nvme_printf(ctrlr, "failed to load qpair memory\n"); 608 goto out; 609 } 610 611 qpair->num_cmds = 0; 612 qpair->num_intr_handler_calls = 0; 613 qpair->cmd = (struct nvme_command *)queuemem; 614 qpair->cpl = (struct nvme_completion *)(queuemem + cmdsz); 615 prpmem = (uint8_t *)(queuemem + cmdsz + cplsz); 616 qpair->cmd_bus_addr = queuemem_phys; 617 qpair->cpl_bus_addr = queuemem_phys + cmdsz; 618 prpmem_phys = queuemem_phys + cmdsz + cplsz; 619 620 qpair->sq_tdbl_off = nvme_mmio_offsetof(doorbell[id].sq_tdbl); 621 qpair->cq_hdbl_off = nvme_mmio_offsetof(doorbell[id].cq_hdbl); 622 623 TAILQ_INIT(&qpair->free_tr); 624 TAILQ_INIT(&qpair->outstanding_tr); 625 STAILQ_INIT(&qpair->queued_req); 626 627 list_phys = prpmem_phys; 628 prp_list = prpmem; 629 for (i = 0; i < qpair->num_trackers; i++) { 630 631 if (list_phys + prpsz > prpmem_phys + prpmemsz) { 632 qpair->num_trackers = i; 633 break; 634 } 635 636 /* 637 * Make sure that the PRP list for this tracker doesn't 638 * overflow to another page. 639 */ 640 if (trunc_page(list_phys) != 641 trunc_page(list_phys + prpsz - 1)) { 642 list_phys = roundup2(list_phys, PAGE_SIZE); 643 prp_list = 644 (uint8_t *)roundup2((uintptr_t)prp_list, PAGE_SIZE); 645 } 646 647 tr = malloc(sizeof(*tr), M_NVME, M_ZERO | M_WAITOK); 648 bus_dmamap_create(qpair->dma_tag_payload, 0, 649 &tr->payload_dma_map); 650 callout_init(&tr->timer, 1); 651 tr->cid = i; 652 tr->qpair = qpair; 653 tr->prp = (uint64_t *)prp_list; 654 tr->prp_bus_addr = list_phys; 655 TAILQ_INSERT_HEAD(&qpair->free_tr, tr, tailq); 656 list_phys += prpsz; 657 prp_list += prpsz; 658 } 659 660 if (qpair->num_trackers == 0) { 661 nvme_printf(ctrlr, "failed to allocate enough trackers\n"); 662 goto out; 663 } 664 665 qpair->act_tr = malloc(sizeof(struct nvme_tracker *) * 666 qpair->num_entries, M_NVME, M_ZERO | M_WAITOK); 667 return (0); 668 669 out: 670 nvme_qpair_destroy(qpair); 671 return (ENOMEM); 672 } 673 674 static void 675 nvme_qpair_destroy(struct nvme_qpair *qpair) 676 { 677 struct nvme_tracker *tr; 678 679 if (qpair->tag) 680 bus_teardown_intr(qpair->ctrlr->dev, qpair->res, qpair->tag); 681 682 if (mtx_initialized(&qpair->lock)) 683 mtx_destroy(&qpair->lock); 684 685 if (qpair->res) 686 bus_release_resource(qpair->ctrlr->dev, SYS_RES_IRQ, 687 rman_get_rid(qpair->res), qpair->res); 688 689 if (qpair->cmd != NULL) { 690 bus_dmamap_unload(qpair->dma_tag, qpair->queuemem_map); 691 bus_dmamem_free(qpair->dma_tag, qpair->cmd, 692 qpair->queuemem_map); 693 } 694 695 if (qpair->act_tr) 696 free(qpair->act_tr, M_NVME); 697 698 while (!TAILQ_EMPTY(&qpair->free_tr)) { 699 tr = TAILQ_FIRST(&qpair->free_tr); 700 TAILQ_REMOVE(&qpair->free_tr, tr, tailq); 701 bus_dmamap_destroy(qpair->dma_tag_payload, 702 tr->payload_dma_map); 703 free(tr, M_NVME); 704 } 705 706 if (qpair->dma_tag) 707 bus_dma_tag_destroy(qpair->dma_tag); 708 709 if (qpair->dma_tag_payload) 710 bus_dma_tag_destroy(qpair->dma_tag_payload); 711 } 712 713 static void 714 nvme_admin_qpair_abort_aers(struct nvme_qpair *qpair) 715 { 716 struct nvme_tracker *tr; 717 718 tr = TAILQ_FIRST(&qpair->outstanding_tr); 719 while (tr != NULL) { 720 if (tr->req->cmd.opc == NVME_OPC_ASYNC_EVENT_REQUEST) { 721 nvme_qpair_manual_complete_tracker(qpair, tr, 722 NVME_SCT_GENERIC, NVME_SC_ABORTED_SQ_DELETION, 0, 723 FALSE); 724 tr = TAILQ_FIRST(&qpair->outstanding_tr); 725 } else { 726 tr = TAILQ_NEXT(tr, tailq); 727 } 728 } 729 } 730 731 void 732 nvme_admin_qpair_destroy(struct nvme_qpair *qpair) 733 { 734 735 nvme_admin_qpair_abort_aers(qpair); 736 nvme_qpair_destroy(qpair); 737 } 738 739 void 740 nvme_io_qpair_destroy(struct nvme_qpair *qpair) 741 { 742 743 nvme_qpair_destroy(qpair); 744 } 745 746 static void 747 nvme_abort_complete(void *arg, const struct nvme_completion *status) 748 { 749 struct nvme_tracker *tr = arg; 750 751 /* 752 * If cdw0 == 1, the controller was not able to abort the command 753 * we requested. We still need to check the active tracker array, 754 * to cover race where I/O timed out at same time controller was 755 * completing the I/O. 756 */ 757 if (status->cdw0 == 1 && tr->qpair->act_tr[tr->cid] != NULL) { 758 /* 759 * An I/O has timed out, and the controller was unable to 760 * abort it for some reason. Construct a fake completion 761 * status, and then complete the I/O's tracker manually. 762 */ 763 nvme_printf(tr->qpair->ctrlr, 764 "abort command failed, aborting command manually\n"); 765 nvme_qpair_manual_complete_tracker(tr->qpair, tr, 766 NVME_SCT_GENERIC, NVME_SC_ABORTED_BY_REQUEST, 0, TRUE); 767 } 768 } 769 770 static void 771 nvme_timeout(void *arg) 772 { 773 struct nvme_tracker *tr = arg; 774 struct nvme_qpair *qpair = tr->qpair; 775 struct nvme_controller *ctrlr = qpair->ctrlr; 776 uint32_t csts; 777 uint8_t cfs; 778 779 /* 780 * Read csts to get value of cfs - controller fatal status. 781 * If no fatal status, try to call the completion routine, and 782 * if completes transactions, report a missed interrupt and 783 * return (this may need to be rate limited). Otherwise, if 784 * aborts are enabled and the controller is not reporting 785 * fatal status, abort the command. Otherwise, just reset the 786 * controller and hope for the best. 787 */ 788 csts = nvme_mmio_read_4(ctrlr, csts); 789 cfs = (csts >> NVME_CSTS_REG_CFS_SHIFT) & NVME_CSTS_REG_CFS_MASK; 790 if (cfs == 0 && nvme_qpair_process_completions(qpair)) { 791 nvme_printf(ctrlr, "Missing interrupt\n"); 792 return; 793 } 794 if (ctrlr->enable_aborts && cfs == 0) { 795 nvme_printf(ctrlr, "Aborting command due to a timeout.\n"); 796 nvme_ctrlr_cmd_abort(ctrlr, tr->cid, qpair->id, 797 nvme_abort_complete, tr); 798 } else { 799 nvme_printf(ctrlr, "Resetting controller due to a timeout%s.\n", 800 cfs ? " and fatal error status" : ""); 801 nvme_ctrlr_reset(ctrlr); 802 } 803 } 804 805 void 806 nvme_qpair_submit_tracker(struct nvme_qpair *qpair, struct nvme_tracker *tr) 807 { 808 struct nvme_request *req; 809 struct nvme_controller *ctrlr; 810 811 mtx_assert(&qpair->lock, MA_OWNED); 812 813 req = tr->req; 814 req->cmd.cid = tr->cid; 815 qpair->act_tr[tr->cid] = tr; 816 ctrlr = qpair->ctrlr; 817 818 if (req->timeout) 819 #if __FreeBSD_version >= 800030 820 callout_reset_curcpu(&tr->timer, ctrlr->timeout_period * hz, 821 nvme_timeout, tr); 822 #else 823 callout_reset(&tr->timer, ctrlr->timeout_period * hz, 824 nvme_timeout, tr); 825 #endif 826 827 /* Copy the command from the tracker to the submission queue. */ 828 memcpy(&qpair->cmd[qpair->sq_tail], &req->cmd, sizeof(req->cmd)); 829 830 if (++qpair->sq_tail == qpair->num_entries) 831 qpair->sq_tail = 0; 832 833 bus_dmamap_sync(qpair->dma_tag, qpair->queuemem_map, 834 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 835 #ifndef __powerpc__ 836 /* 837 * powerpc's bus_dmamap_sync() already includes a heavyweight sync, but 838 * no other archs do. 839 */ 840 wmb(); 841 #endif 842 843 nvme_mmio_write_4(qpair->ctrlr, doorbell[qpair->id].sq_tdbl, 844 qpair->sq_tail); 845 846 qpair->num_cmds++; 847 } 848 849 static void 850 nvme_payload_map(void *arg, bus_dma_segment_t *seg, int nseg, int error) 851 { 852 struct nvme_tracker *tr = arg; 853 uint32_t cur_nseg; 854 855 /* 856 * If the mapping operation failed, return immediately. The caller 857 * is responsible for detecting the error status and failing the 858 * tracker manually. 859 */ 860 if (error != 0) { 861 nvme_printf(tr->qpair->ctrlr, 862 "nvme_payload_map err %d\n", error); 863 return; 864 } 865 866 /* 867 * Note that we specified PAGE_SIZE for alignment and max 868 * segment size when creating the bus dma tags. So here 869 * we can safely just transfer each segment to its 870 * associated PRP entry. 871 */ 872 tr->req->cmd.prp1 = htole64(seg[0].ds_addr); 873 874 if (nseg == 2) { 875 tr->req->cmd.prp2 = htole64(seg[1].ds_addr); 876 } else if (nseg > 2) { 877 cur_nseg = 1; 878 tr->req->cmd.prp2 = htole64((uint64_t)tr->prp_bus_addr); 879 while (cur_nseg < nseg) { 880 tr->prp[cur_nseg-1] = 881 htole64((uint64_t)seg[cur_nseg].ds_addr); 882 cur_nseg++; 883 } 884 } else { 885 /* 886 * prp2 should not be used by the controller 887 * since there is only one segment, but set 888 * to 0 just to be safe. 889 */ 890 tr->req->cmd.prp2 = 0; 891 } 892 893 bus_dmamap_sync(tr->qpair->dma_tag_payload, tr->payload_dma_map, 894 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 895 nvme_qpair_submit_tracker(tr->qpair, tr); 896 } 897 898 static void 899 _nvme_qpair_submit_request(struct nvme_qpair *qpair, struct nvme_request *req) 900 { 901 struct nvme_tracker *tr; 902 int err = 0; 903 904 mtx_assert(&qpair->lock, MA_OWNED); 905 906 tr = TAILQ_FIRST(&qpair->free_tr); 907 req->qpair = qpair; 908 909 if (tr == NULL || !qpair->is_enabled) { 910 /* 911 * No tracker is available, or the qpair is disabled due to 912 * an in-progress controller-level reset or controller 913 * failure. 914 */ 915 916 if (qpair->ctrlr->is_failed) { 917 /* 918 * The controller has failed. Post the request to a 919 * task where it will be aborted, so that we do not 920 * invoke the request's callback in the context 921 * of the submission. 922 */ 923 nvme_ctrlr_post_failed_request(qpair->ctrlr, req); 924 } else { 925 /* 926 * Put the request on the qpair's request queue to be 927 * processed when a tracker frees up via a command 928 * completion or when the controller reset is 929 * completed. 930 */ 931 STAILQ_INSERT_TAIL(&qpair->queued_req, req, stailq); 932 } 933 return; 934 } 935 936 TAILQ_REMOVE(&qpair->free_tr, tr, tailq); 937 TAILQ_INSERT_TAIL(&qpair->outstanding_tr, tr, tailq); 938 tr->req = req; 939 940 switch (req->type) { 941 case NVME_REQUEST_VADDR: 942 KASSERT(req->payload_size <= qpair->ctrlr->max_xfer_size, 943 ("payload_size (%d) exceeds max_xfer_size (%d)\n", 944 req->payload_size, qpair->ctrlr->max_xfer_size)); 945 err = bus_dmamap_load(tr->qpair->dma_tag_payload, 946 tr->payload_dma_map, req->u.payload, req->payload_size, 947 nvme_payload_map, tr, 0); 948 if (err != 0) 949 nvme_printf(qpair->ctrlr, 950 "bus_dmamap_load returned 0x%x!\n", err); 951 break; 952 case NVME_REQUEST_NULL: 953 nvme_qpair_submit_tracker(tr->qpair, tr); 954 break; 955 #ifdef NVME_UNMAPPED_BIO_SUPPORT 956 case NVME_REQUEST_BIO: 957 KASSERT(req->u.bio->bio_bcount <= qpair->ctrlr->max_xfer_size, 958 ("bio->bio_bcount (%jd) exceeds max_xfer_size (%d)\n", 959 (intmax_t)req->u.bio->bio_bcount, 960 qpair->ctrlr->max_xfer_size)); 961 err = bus_dmamap_load_bio(tr->qpair->dma_tag_payload, 962 tr->payload_dma_map, req->u.bio, nvme_payload_map, tr, 0); 963 if (err != 0) 964 nvme_printf(qpair->ctrlr, 965 "bus_dmamap_load_bio returned 0x%x!\n", err); 966 break; 967 #endif 968 case NVME_REQUEST_CCB: 969 err = bus_dmamap_load_ccb(tr->qpair->dma_tag_payload, 970 tr->payload_dma_map, req->u.payload, 971 nvme_payload_map, tr, 0); 972 if (err != 0) 973 nvme_printf(qpair->ctrlr, 974 "bus_dmamap_load_ccb returned 0x%x!\n", err); 975 break; 976 default: 977 panic("unknown nvme request type 0x%x\n", req->type); 978 break; 979 } 980 981 if (err != 0) { 982 /* 983 * The dmamap operation failed, so we manually fail the 984 * tracker here with DATA_TRANSFER_ERROR status. 985 * 986 * nvme_qpair_manual_complete_tracker must not be called 987 * with the qpair lock held. 988 */ 989 mtx_unlock(&qpair->lock); 990 nvme_qpair_manual_complete_tracker(qpair, tr, NVME_SCT_GENERIC, 991 NVME_SC_DATA_TRANSFER_ERROR, 1 /* do not retry */, TRUE); 992 mtx_lock(&qpair->lock); 993 } 994 } 995 996 void 997 nvme_qpair_submit_request(struct nvme_qpair *qpair, struct nvme_request *req) 998 { 999 1000 mtx_lock(&qpair->lock); 1001 _nvme_qpair_submit_request(qpair, req); 1002 mtx_unlock(&qpair->lock); 1003 } 1004 1005 static void 1006 nvme_qpair_enable(struct nvme_qpair *qpair) 1007 { 1008 1009 qpair->is_enabled = TRUE; 1010 } 1011 1012 void 1013 nvme_qpair_reset(struct nvme_qpair *qpair) 1014 { 1015 1016 qpair->sq_head = qpair->sq_tail = qpair->cq_head = 0; 1017 1018 /* 1019 * First time through the completion queue, HW will set phase 1020 * bit on completions to 1. So set this to 1 here, indicating 1021 * we're looking for a 1 to know which entries have completed. 1022 * we'll toggle the bit each time when the completion queue 1023 * rolls over. 1024 */ 1025 qpair->phase = 1; 1026 1027 memset(qpair->cmd, 0, 1028 qpair->num_entries * sizeof(struct nvme_command)); 1029 memset(qpair->cpl, 0, 1030 qpair->num_entries * sizeof(struct nvme_completion)); 1031 } 1032 1033 void 1034 nvme_admin_qpair_enable(struct nvme_qpair *qpair) 1035 { 1036 struct nvme_tracker *tr; 1037 struct nvme_tracker *tr_temp; 1038 1039 /* 1040 * Manually abort each outstanding admin command. Do not retry 1041 * admin commands found here, since they will be left over from 1042 * a controller reset and its likely the context in which the 1043 * command was issued no longer applies. 1044 */ 1045 TAILQ_FOREACH_SAFE(tr, &qpair->outstanding_tr, tailq, tr_temp) { 1046 nvme_printf(qpair->ctrlr, 1047 "aborting outstanding admin command\n"); 1048 nvme_qpair_manual_complete_tracker(qpair, tr, NVME_SCT_GENERIC, 1049 NVME_SC_ABORTED_BY_REQUEST, 1 /* do not retry */, TRUE); 1050 } 1051 1052 nvme_qpair_enable(qpair); 1053 } 1054 1055 void 1056 nvme_io_qpair_enable(struct nvme_qpair *qpair) 1057 { 1058 STAILQ_HEAD(, nvme_request) temp; 1059 struct nvme_tracker *tr; 1060 struct nvme_tracker *tr_temp; 1061 struct nvme_request *req; 1062 1063 /* 1064 * Manually abort each outstanding I/O. This normally results in a 1065 * retry, unless the retry count on the associated request has 1066 * reached its limit. 1067 */ 1068 TAILQ_FOREACH_SAFE(tr, &qpair->outstanding_tr, tailq, tr_temp) { 1069 nvme_printf(qpair->ctrlr, "aborting outstanding i/o\n"); 1070 nvme_qpair_manual_complete_tracker(qpair, tr, NVME_SCT_GENERIC, 1071 NVME_SC_ABORTED_BY_REQUEST, 0, TRUE); 1072 } 1073 1074 mtx_lock(&qpair->lock); 1075 1076 nvme_qpair_enable(qpair); 1077 1078 STAILQ_INIT(&temp); 1079 STAILQ_SWAP(&qpair->queued_req, &temp, nvme_request); 1080 1081 while (!STAILQ_EMPTY(&temp)) { 1082 req = STAILQ_FIRST(&temp); 1083 STAILQ_REMOVE_HEAD(&temp, stailq); 1084 nvme_printf(qpair->ctrlr, "resubmitting queued i/o\n"); 1085 nvme_qpair_print_command(qpair, &req->cmd); 1086 _nvme_qpair_submit_request(qpair, req); 1087 } 1088 1089 mtx_unlock(&qpair->lock); 1090 } 1091 1092 static void 1093 nvme_qpair_disable(struct nvme_qpair *qpair) 1094 { 1095 struct nvme_tracker *tr; 1096 1097 qpair->is_enabled = FALSE; 1098 mtx_lock(&qpair->lock); 1099 TAILQ_FOREACH(tr, &qpair->outstanding_tr, tailq) 1100 callout_stop(&tr->timer); 1101 mtx_unlock(&qpair->lock); 1102 } 1103 1104 void 1105 nvme_admin_qpair_disable(struct nvme_qpair *qpair) 1106 { 1107 1108 nvme_qpair_disable(qpair); 1109 nvme_admin_qpair_abort_aers(qpair); 1110 } 1111 1112 void 1113 nvme_io_qpair_disable(struct nvme_qpair *qpair) 1114 { 1115 1116 nvme_qpair_disable(qpair); 1117 } 1118 1119 void 1120 nvme_qpair_fail(struct nvme_qpair *qpair) 1121 { 1122 struct nvme_tracker *tr; 1123 struct nvme_request *req; 1124 1125 if (!mtx_initialized(&qpair->lock)) 1126 return; 1127 1128 mtx_lock(&qpair->lock); 1129 1130 while (!STAILQ_EMPTY(&qpair->queued_req)) { 1131 req = STAILQ_FIRST(&qpair->queued_req); 1132 STAILQ_REMOVE_HEAD(&qpair->queued_req, stailq); 1133 nvme_printf(qpair->ctrlr, "failing queued i/o\n"); 1134 mtx_unlock(&qpair->lock); 1135 nvme_qpair_manual_complete_request(qpair, req, NVME_SCT_GENERIC, 1136 NVME_SC_ABORTED_BY_REQUEST, TRUE); 1137 mtx_lock(&qpair->lock); 1138 } 1139 1140 /* Manually abort each outstanding I/O. */ 1141 while (!TAILQ_EMPTY(&qpair->outstanding_tr)) { 1142 tr = TAILQ_FIRST(&qpair->outstanding_tr); 1143 /* 1144 * Do not remove the tracker. The abort_tracker path will 1145 * do that for us. 1146 */ 1147 nvme_printf(qpair->ctrlr, "failing outstanding i/o\n"); 1148 mtx_unlock(&qpair->lock); 1149 nvme_qpair_manual_complete_tracker(qpair, tr, NVME_SCT_GENERIC, 1150 NVME_SC_ABORTED_BY_REQUEST, 1 /* do not retry */, TRUE); 1151 mtx_lock(&qpair->lock); 1152 } 1153 1154 mtx_unlock(&qpair->lock); 1155 } 1156 1157