2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (C) 2012-2014 Intel Corporation
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
35 #include <dev/pci/pcivar.h>
37 #include "nvme_private.h"
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);
43 struct nvme_opcode_string {
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" }
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" }
94 get_admin_opcode_string(uint16_t opc)
96 struct nvme_opcode_string *entry;
100 while (entry->opc != 0xFFFF) {
101 if (entry->opc == opc)
109 get_io_opcode_string(uint16_t opc)
111 struct nvme_opcode_string *entry;
115 while (entry->opc != 0xFFFF) {
116 if (entry->opc == opc)
125 nvme_admin_qpair_print_command(struct nvme_qpair *qpair,
126 struct nvme_command *cmd)
130 opc = le16toh(cmd->opc_fuse) & NVME_CMD_OPC_MASK;
131 nvme_printf(qpair->ctrlr, "%s (%02x) sqid:%d cid:%d nsid:%x "
132 "cdw10:%08x cdw11:%08x\n",
133 get_admin_opcode_string(opc), opc, qpair->id, cmd->cid,
134 le32toh(cmd->nsid), le32toh(cmd->cdw10), le32toh(cmd->cdw11));
138 nvme_io_qpair_print_command(struct nvme_qpair *qpair,
139 struct nvme_command *cmd)
143 opc = le16toh(cmd->opc_fuse) & NVME_CMD_OPC_MASK;
147 case NVME_OPC_WRITE_UNCORRECTABLE:
148 case NVME_OPC_COMPARE:
149 case NVME_OPC_WRITE_ZEROES:
150 nvme_printf(qpair->ctrlr, "%s sqid:%d cid:%d nsid:%d "
152 get_io_opcode_string(opc), qpair->id, cmd->cid, le32toh(cmd->nsid),
153 ((unsigned long long)le32toh(cmd->cdw11) << 32) + le32toh(cmd->cdw10),
154 (le32toh(cmd->cdw12) & 0xFFFF) + 1);
157 case NVME_OPC_DATASET_MANAGEMENT:
158 case NVME_OPC_RESERVATION_REGISTER:
159 case NVME_OPC_RESERVATION_REPORT:
160 case NVME_OPC_RESERVATION_ACQUIRE:
161 case NVME_OPC_RESERVATION_RELEASE:
162 nvme_printf(qpair->ctrlr, "%s sqid:%d cid:%d nsid:%d\n",
163 get_io_opcode_string(opc), qpair->id, cmd->cid, le32toh(cmd->nsid));
166 nvme_printf(qpair->ctrlr, "%s (%02x) sqid:%d cid:%d nsid:%d\n",
167 get_io_opcode_string(opc), opc, qpair->id,
168 cmd->cid, le32toh(cmd->nsid));
174 nvme_qpair_print_command(struct nvme_qpair *qpair, struct nvme_command *cmd)
177 nvme_admin_qpair_print_command(qpair, cmd);
179 nvme_io_qpair_print_command(qpair, cmd);
182 struct nvme_status_string {
188 static struct nvme_status_string generic_status[] = {
189 { NVME_SC_SUCCESS, "SUCCESS" },
190 { NVME_SC_INVALID_OPCODE, "INVALID OPCODE" },
191 { NVME_SC_INVALID_FIELD, "INVALID_FIELD" },
192 { NVME_SC_COMMAND_ID_CONFLICT, "COMMAND ID CONFLICT" },
193 { NVME_SC_DATA_TRANSFER_ERROR, "DATA TRANSFER ERROR" },
194 { NVME_SC_ABORTED_POWER_LOSS, "ABORTED - POWER LOSS" },
195 { NVME_SC_INTERNAL_DEVICE_ERROR, "INTERNAL DEVICE ERROR" },
196 { NVME_SC_ABORTED_BY_REQUEST, "ABORTED - BY REQUEST" },
197 { NVME_SC_ABORTED_SQ_DELETION, "ABORTED - SQ DELETION" },
198 { NVME_SC_ABORTED_FAILED_FUSED, "ABORTED - FAILED FUSED" },
199 { NVME_SC_ABORTED_MISSING_FUSED, "ABORTED - MISSING FUSED" },
200 { NVME_SC_INVALID_NAMESPACE_OR_FORMAT, "INVALID NAMESPACE OR FORMAT" },
201 { NVME_SC_COMMAND_SEQUENCE_ERROR, "COMMAND SEQUENCE ERROR" },
202 { NVME_SC_INVALID_SGL_SEGMENT_DESCR, "INVALID SGL SEGMENT DESCRIPTOR" },
203 { NVME_SC_INVALID_NUMBER_OF_SGL_DESCR, "INVALID NUMBER OF SGL DESCRIPTORS" },
204 { NVME_SC_DATA_SGL_LENGTH_INVALID, "DATA SGL LENGTH INVALID" },
205 { NVME_SC_METADATA_SGL_LENGTH_INVALID, "METADATA SGL LENGTH INVALID" },
206 { NVME_SC_SGL_DESCRIPTOR_TYPE_INVALID, "SGL DESCRIPTOR TYPE INVALID" },
207 { NVME_SC_INVALID_USE_OF_CMB, "INVALID USE OF CONTROLLER MEMORY BUFFER" },
208 { NVME_SC_PRP_OFFET_INVALID, "PRP OFFET INVALID" },
209 { NVME_SC_ATOMIC_WRITE_UNIT_EXCEEDED, "ATOMIC WRITE UNIT EXCEEDED" },
210 { NVME_SC_OPERATION_DENIED, "OPERATION DENIED" },
211 { NVME_SC_SGL_OFFSET_INVALID, "SGL OFFSET INVALID" },
212 { NVME_SC_HOST_ID_INCONSISTENT_FORMAT, "HOST IDENTIFIER INCONSISTENT FORMAT" },
213 { NVME_SC_KEEP_ALIVE_TIMEOUT_EXPIRED, "KEEP ALIVE TIMEOUT EXPIRED" },
214 { NVME_SC_KEEP_ALIVE_TIMEOUT_INVALID, "KEEP ALIVE TIMEOUT INVALID" },
215 { NVME_SC_ABORTED_DUE_TO_PREEMPT, "COMMAND ABORTED DUE TO PREEMPT AND ABORT" },
216 { NVME_SC_SANITIZE_FAILED, "SANITIZE FAILED" },
217 { NVME_SC_SANITIZE_IN_PROGRESS, "SANITIZE IN PROGRESS" },
218 { NVME_SC_SGL_DATA_BLOCK_GRAN_INVALID, "SGL_DATA_BLOCK_GRANULARITY_INVALID" },
219 { NVME_SC_NOT_SUPPORTED_IN_CMB, "COMMAND NOT SUPPORTED FOR QUEUE IN CMB" },
221 { NVME_SC_LBA_OUT_OF_RANGE, "LBA OUT OF RANGE" },
222 { NVME_SC_CAPACITY_EXCEEDED, "CAPACITY EXCEEDED" },
223 { NVME_SC_NAMESPACE_NOT_READY, "NAMESPACE NOT READY" },
224 { NVME_SC_RESERVATION_CONFLICT, "RESERVATION CONFLICT" },
225 { NVME_SC_FORMAT_IN_PROGRESS, "FORMAT IN PROGRESS" },
226 { 0xFFFF, "GENERIC" }
229 static struct nvme_status_string command_specific_status[] = {
230 { NVME_SC_COMPLETION_QUEUE_INVALID, "INVALID COMPLETION QUEUE" },
231 { NVME_SC_INVALID_QUEUE_IDENTIFIER, "INVALID QUEUE IDENTIFIER" },
232 { NVME_SC_MAXIMUM_QUEUE_SIZE_EXCEEDED, "MAX QUEUE SIZE EXCEEDED" },
233 { NVME_SC_ABORT_COMMAND_LIMIT_EXCEEDED, "ABORT CMD LIMIT EXCEEDED" },
234 { NVME_SC_ASYNC_EVENT_REQUEST_LIMIT_EXCEEDED, "ASYNC LIMIT EXCEEDED" },
235 { NVME_SC_INVALID_FIRMWARE_SLOT, "INVALID FIRMWARE SLOT" },
236 { NVME_SC_INVALID_FIRMWARE_IMAGE, "INVALID FIRMWARE IMAGE" },
237 { NVME_SC_INVALID_INTERRUPT_VECTOR, "INVALID INTERRUPT VECTOR" },
238 { NVME_SC_INVALID_LOG_PAGE, "INVALID LOG PAGE" },
239 { NVME_SC_INVALID_FORMAT, "INVALID FORMAT" },
240 { NVME_SC_FIRMWARE_REQUIRES_RESET, "FIRMWARE REQUIRES RESET" },
241 { NVME_SC_INVALID_QUEUE_DELETION, "INVALID QUEUE DELETION" },
242 { NVME_SC_FEATURE_NOT_SAVEABLE, "FEATURE IDENTIFIER NOT SAVEABLE" },
243 { NVME_SC_FEATURE_NOT_CHANGEABLE, "FEATURE NOT CHANGEABLE" },
244 { NVME_SC_FEATURE_NOT_NS_SPECIFIC, "FEATURE NOT NAMESPACE SPECIFIC" },
245 { NVME_SC_FW_ACT_REQUIRES_NVMS_RESET, "FIRMWARE ACTIVATION REQUIRES NVM SUBSYSTEM RESET" },
246 { NVME_SC_FW_ACT_REQUIRES_RESET, "FIRMWARE ACTIVATION REQUIRES RESET" },
247 { NVME_SC_FW_ACT_REQUIRES_TIME, "FIRMWARE ACTIVATION REQUIRES MAXIMUM TIME VIOLATION" },
248 { NVME_SC_FW_ACT_PROHIBITED, "FIRMWARE ACTIVATION PROHIBITED" },
249 { NVME_SC_OVERLAPPING_RANGE, "OVERLAPPING RANGE" },
250 { NVME_SC_NS_INSUFFICIENT_CAPACITY, "NAMESPACE INSUFFICIENT CAPACITY" },
251 { NVME_SC_NS_ID_UNAVAILABLE, "NAMESPACE IDENTIFIER UNAVAILABLE" },
252 { NVME_SC_NS_ALREADY_ATTACHED, "NAMESPACE ALREADY ATTACHED" },
253 { NVME_SC_NS_IS_PRIVATE, "NAMESPACE IS PRIVATE" },
254 { NVME_SC_NS_NOT_ATTACHED, "NS NOT ATTACHED" },
255 { NVME_SC_THIN_PROV_NOT_SUPPORTED, "THIN PROVISIONING NOT SUPPORTED" },
256 { NVME_SC_CTRLR_LIST_INVALID, "CONTROLLER LIST INVALID" },
257 { NVME_SC_SELT_TEST_IN_PROGRESS, "DEVICE SELT-TEST IN PROGRESS" },
258 { NVME_SC_BOOT_PART_WRITE_PROHIB, "BOOT PARTITION WRITE PROHIBITED" },
259 { NVME_SC_INVALID_CTRLR_ID, "INVALID CONTROLLER IDENTIFIER" },
260 { NVME_SC_INVALID_SEC_CTRLR_STATE, "INVALID SECONDARY CONTROLLER STATE" },
261 { NVME_SC_INVALID_NUM_OF_CTRLR_RESRC, "INVALID NUMBER OF CONTROLLER RESOURCES" },
262 { NVME_SC_INVALID_RESOURCE_ID, "INVALID RESOURCE IDENTIFIER" },
264 { NVME_SC_CONFLICTING_ATTRIBUTES, "CONFLICTING ATTRIBUTES" },
265 { NVME_SC_INVALID_PROTECTION_INFO, "INVALID PROTECTION INFO" },
266 { NVME_SC_ATTEMPTED_WRITE_TO_RO_PAGE, "WRITE TO RO PAGE" },
267 { 0xFFFF, "COMMAND SPECIFIC" }
270 static struct nvme_status_string media_error_status[] = {
271 { NVME_SC_WRITE_FAULTS, "WRITE FAULTS" },
272 { NVME_SC_UNRECOVERED_READ_ERROR, "UNRECOVERED READ ERROR" },
273 { NVME_SC_GUARD_CHECK_ERROR, "GUARD CHECK ERROR" },
274 { NVME_SC_APPLICATION_TAG_CHECK_ERROR, "APPLICATION TAG CHECK ERROR" },
275 { NVME_SC_REFERENCE_TAG_CHECK_ERROR, "REFERENCE TAG CHECK ERROR" },
276 { NVME_SC_COMPARE_FAILURE, "COMPARE FAILURE" },
277 { NVME_SC_ACCESS_DENIED, "ACCESS DENIED" },
278 { NVME_SC_DEALLOCATED_OR_UNWRITTEN, "DEALLOCATED OR UNWRITTEN LOGICAL BLOCK" },
279 { 0xFFFF, "MEDIA ERROR" }
283 get_status_string(uint16_t sct, uint16_t sc)
285 struct nvme_status_string *entry;
288 case NVME_SCT_GENERIC:
289 entry = generic_status;
291 case NVME_SCT_COMMAND_SPECIFIC:
292 entry = command_specific_status;
294 case NVME_SCT_MEDIA_ERROR:
295 entry = media_error_status;
297 case NVME_SCT_VENDOR_SPECIFIC:
298 return ("VENDOR SPECIFIC");
303 while (entry->sc != 0xFFFF) {
312 nvme_qpair_print_completion(struct nvme_qpair *qpair,
313 struct nvme_completion *cpl)
317 sct = NVME_STATUS_GET_SCT(cpl->status);
318 sc = NVME_STATUS_GET_SC(cpl->status);
320 nvme_printf(qpair->ctrlr, "%s (%02x/%02x) sqid:%d cid:%d cdw0:%x\n",
321 get_status_string(sct, sc), sct, sc, cpl->sqid, cpl->cid,
326 nvme_completion_is_retry(const struct nvme_completion *cpl)
328 uint8_t sct, sc, dnr;
330 sct = NVME_STATUS_GET_SCT(cpl->status);
331 sc = NVME_STATUS_GET_SC(cpl->status);
332 dnr = NVME_STATUS_GET_DNR(cpl->status);
335 * TODO: spec is not clear how commands that are aborted due
336 * to TLER will be marked. So for now, it seems
337 * NAMESPACE_NOT_READY is the only case where we should
338 * look at the DNR bit.
341 case NVME_SCT_GENERIC:
343 case NVME_SC_ABORTED_BY_REQUEST:
344 case NVME_SC_NAMESPACE_NOT_READY:
349 case NVME_SC_INVALID_OPCODE:
350 case NVME_SC_INVALID_FIELD:
351 case NVME_SC_COMMAND_ID_CONFLICT:
352 case NVME_SC_DATA_TRANSFER_ERROR:
353 case NVME_SC_ABORTED_POWER_LOSS:
354 case NVME_SC_INTERNAL_DEVICE_ERROR:
355 case NVME_SC_ABORTED_SQ_DELETION:
356 case NVME_SC_ABORTED_FAILED_FUSED:
357 case NVME_SC_ABORTED_MISSING_FUSED:
358 case NVME_SC_INVALID_NAMESPACE_OR_FORMAT:
359 case NVME_SC_COMMAND_SEQUENCE_ERROR:
360 case NVME_SC_LBA_OUT_OF_RANGE:
361 case NVME_SC_CAPACITY_EXCEEDED:
365 case NVME_SCT_COMMAND_SPECIFIC:
366 case NVME_SCT_MEDIA_ERROR:
367 case NVME_SCT_VENDOR_SPECIFIC:
374 nvme_qpair_complete_tracker(struct nvme_qpair *qpair, struct nvme_tracker *tr,
375 struct nvme_completion *cpl, boolean_t print_on_error)
377 struct nvme_request *req;
378 boolean_t retry, error;
381 error = nvme_completion_is_error(cpl);
382 retry = error && nvme_completion_is_retry(cpl) &&
383 req->retries < nvme_retry_count;
385 if (error && print_on_error) {
386 nvme_qpair_print_command(qpair, &req->cmd);
387 nvme_qpair_print_completion(qpair, cpl);
390 qpair->act_tr[cpl->cid] = NULL;
392 KASSERT(cpl->cid == req->cmd.cid, ("cpl cid does not match cmd cid\n"));
394 if (req->cb_fn && !retry)
395 req->cb_fn(req->cb_arg, cpl);
397 mtx_lock(&qpair->lock);
398 callout_stop(&tr->timer);
402 nvme_qpair_submit_tracker(qpair, tr);
404 if (req->type != NVME_REQUEST_NULL)
405 bus_dmamap_unload(qpair->dma_tag_payload,
406 tr->payload_dma_map);
408 nvme_free_request(req);
411 TAILQ_REMOVE(&qpair->outstanding_tr, tr, tailq);
412 TAILQ_INSERT_HEAD(&qpair->free_tr, tr, tailq);
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.
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);
427 mtx_unlock(&qpair->lock);
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)
435 struct nvme_completion cpl;
437 memset(&cpl, 0, sizeof(cpl));
438 cpl.sqid = qpair->id;
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);
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)
451 struct nvme_completion cpl;
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;
459 error = nvme_completion_is_error(&cpl);
461 if (error && print_on_error) {
462 nvme_qpair_print_command(qpair, &req->cmd);
463 nvme_qpair_print_completion(qpair, &cpl);
467 req->cb_fn(req->cb_arg, &cpl);
469 nvme_free_request(req);
473 nvme_qpair_process_completions(struct nvme_qpair *qpair)
475 struct nvme_tracker *tr;
476 struct nvme_completion cpl;
479 qpair->num_intr_handler_calls++;
481 if (!qpair->is_enabled)
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
491 cpl = qpair->cpl[qpair->cq_head];
493 /* Convert to host endian */
494 nvme_completion_swapbytes(&cpl);
496 if (NVME_STATUS_GET_P(cpl.status) != qpair->phase)
499 tr = qpair->act_tr[cpl.cid];
502 nvme_qpair_complete_tracker(qpair, tr, &cpl, TRUE);
503 qpair->sq_head = cpl.sqhd;
506 nvme_printf(qpair->ctrlr,
507 "cpl does not map to outstanding cmd\n");
508 /* nvme_dump_completion expects device endianess */
509 nvme_dump_completion(&qpair->cpl[qpair->cq_head]);
510 KASSERT(0, ("received completion for unknown cmd\n"));
513 if (++qpair->cq_head == qpair->num_entries) {
515 qpair->phase = !qpair->phase;
518 nvme_mmio_write_4(qpair->ctrlr, doorbell[qpair->id].cq_hdbl,
525 nvme_qpair_msix_handler(void *arg)
527 struct nvme_qpair *qpair = arg;
529 nvme_qpair_process_completions(qpair);
533 nvme_qpair_construct(struct nvme_qpair *qpair, uint32_t id,
534 uint16_t vector, uint32_t num_entries, uint32_t num_trackers,
535 struct nvme_controller *ctrlr)
537 struct nvme_tracker *tr;
538 size_t cmdsz, cplsz, prpsz, allocsz, prpmemsz;
539 uint64_t queuemem_phys, prpmem_phys, list_phys;
540 uint8_t *queuemem, *prpmem, *prp_list;
544 qpair->vector = vector;
545 qpair->num_entries = num_entries;
546 qpair->num_trackers = num_trackers;
547 qpair->ctrlr = ctrlr;
549 if (ctrlr->msix_enabled) {
552 * MSI-X vector resource IDs start at 1, so we add one to
553 * the queue's vector to get the corresponding rid to use.
555 qpair->rid = vector + 1;
557 qpair->res = bus_alloc_resource_any(ctrlr->dev, SYS_RES_IRQ,
558 &qpair->rid, RF_ACTIVE);
559 bus_setup_intr(ctrlr->dev, qpair->res,
560 INTR_TYPE_MISC | INTR_MPSAFE, NULL,
561 nvme_qpair_msix_handler, qpair, &qpair->tag);
564 mtx_init(&qpair->lock, "nvme qpair lock", NULL, MTX_DEF);
566 /* Note: NVMe PRP format is restricted to 4-byte alignment. */
567 err = bus_dma_tag_create(bus_get_dma_tag(ctrlr->dev),
568 4, PAGE_SIZE, BUS_SPACE_MAXADDR,
569 BUS_SPACE_MAXADDR, NULL, NULL, NVME_MAX_XFER_SIZE,
570 (NVME_MAX_XFER_SIZE/PAGE_SIZE)+1, PAGE_SIZE, 0,
571 NULL, NULL, &qpair->dma_tag_payload);
573 nvme_printf(ctrlr, "payload tag create failed %d\n", err);
578 * Each component must be page aligned, and individual PRP lists
579 * cannot cross a page boundary.
581 cmdsz = qpair->num_entries * sizeof(struct nvme_command);
582 cmdsz = roundup2(cmdsz, PAGE_SIZE);
583 cplsz = qpair->num_entries * sizeof(struct nvme_completion);
584 cplsz = roundup2(cplsz, PAGE_SIZE);
585 prpsz = sizeof(uint64_t) * NVME_MAX_PRP_LIST_ENTRIES;;
586 prpmemsz = qpair->num_trackers * prpsz;
587 allocsz = cmdsz + cplsz + prpmemsz;
589 err = bus_dma_tag_create(bus_get_dma_tag(ctrlr->dev),
590 PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
591 allocsz, 1, allocsz, 0, NULL, NULL, &qpair->dma_tag);
593 nvme_printf(ctrlr, "tag create failed %d\n", err);
597 if (bus_dmamem_alloc(qpair->dma_tag, (void **)&queuemem,
598 BUS_DMA_NOWAIT, &qpair->queuemem_map)) {
599 nvme_printf(ctrlr, "failed to alloc qpair memory\n");
603 if (bus_dmamap_load(qpair->dma_tag, qpair->queuemem_map,
604 queuemem, allocsz, nvme_single_map, &queuemem_phys, 0) != 0) {
605 nvme_printf(ctrlr, "failed to load qpair memory\n");
610 qpair->num_intr_handler_calls = 0;
611 qpair->cmd = (struct nvme_command *)queuemem;
612 qpair->cpl = (struct nvme_completion *)(queuemem + cmdsz);
613 prpmem = (uint8_t *)(queuemem + cmdsz + cplsz);
614 qpair->cmd_bus_addr = queuemem_phys;
615 qpair->cpl_bus_addr = queuemem_phys + cmdsz;
616 prpmem_phys = queuemem_phys + cmdsz + cplsz;
618 qpair->sq_tdbl_off = nvme_mmio_offsetof(doorbell[id].sq_tdbl);
619 qpair->cq_hdbl_off = nvme_mmio_offsetof(doorbell[id].cq_hdbl);
621 TAILQ_INIT(&qpair->free_tr);
622 TAILQ_INIT(&qpair->outstanding_tr);
623 STAILQ_INIT(&qpair->queued_req);
625 list_phys = prpmem_phys;
627 for (i = 0; i < qpair->num_trackers; i++) {
629 if (list_phys + prpsz > prpmem_phys + prpmemsz) {
630 qpair->num_trackers = i;
635 * Make sure that the PRP list for this tracker doesn't
636 * overflow to another page.
638 if (trunc_page(list_phys) !=
639 trunc_page(list_phys + prpsz - 1)) {
640 list_phys = roundup2(list_phys, PAGE_SIZE);
642 (uint8_t *)roundup2((uintptr_t)prp_list, PAGE_SIZE);
645 tr = malloc(sizeof(*tr), M_NVME, M_ZERO | M_WAITOK);
646 bus_dmamap_create(qpair->dma_tag_payload, 0,
647 &tr->payload_dma_map);
648 callout_init(&tr->timer, 1);
651 tr->prp = (uint64_t *)prp_list;
652 tr->prp_bus_addr = list_phys;
653 TAILQ_INSERT_HEAD(&qpair->free_tr, tr, tailq);
658 if (qpair->num_trackers == 0) {
659 nvme_printf(ctrlr, "failed to allocate enough trackers\n");
663 qpair->act_tr = malloc(sizeof(struct nvme_tracker *) *
664 qpair->num_entries, M_NVME, M_ZERO | M_WAITOK);
668 nvme_qpair_destroy(qpair);
673 nvme_qpair_destroy(struct nvme_qpair *qpair)
675 struct nvme_tracker *tr;
678 bus_teardown_intr(qpair->ctrlr->dev, qpair->res, qpair->tag);
680 if (mtx_initialized(&qpair->lock))
681 mtx_destroy(&qpair->lock);
684 bus_release_resource(qpair->ctrlr->dev, SYS_RES_IRQ,
685 rman_get_rid(qpair->res), qpair->res);
687 if (qpair->cmd != NULL) {
688 bus_dmamap_unload(qpair->dma_tag, qpair->queuemem_map);
689 bus_dmamem_free(qpair->dma_tag, qpair->cmd,
690 qpair->queuemem_map);
694 bus_dma_tag_destroy(qpair->dma_tag);
696 if (qpair->dma_tag_payload)
697 bus_dma_tag_destroy(qpair->dma_tag_payload);
700 free(qpair->act_tr, M_NVME);
702 while (!TAILQ_EMPTY(&qpair->free_tr)) {
703 tr = TAILQ_FIRST(&qpair->free_tr);
704 TAILQ_REMOVE(&qpair->free_tr, tr, tailq);
705 bus_dmamap_destroy(qpair->dma_tag, tr->payload_dma_map);
711 nvme_admin_qpair_abort_aers(struct nvme_qpair *qpair)
713 struct nvme_tracker *tr;
715 tr = TAILQ_FIRST(&qpair->outstanding_tr);
717 if ((le16toh(tr->req->cmd.opc_fuse) & NVME_CMD_OPC_MASK) == NVME_OPC_ASYNC_EVENT_REQUEST) {
718 nvme_qpair_manual_complete_tracker(qpair, tr,
719 NVME_SCT_GENERIC, NVME_SC_ABORTED_SQ_DELETION, 0,
721 tr = TAILQ_FIRST(&qpair->outstanding_tr);
723 tr = TAILQ_NEXT(tr, tailq);
729 nvme_admin_qpair_destroy(struct nvme_qpair *qpair)
732 nvme_admin_qpair_abort_aers(qpair);
733 nvme_qpair_destroy(qpair);
737 nvme_io_qpair_destroy(struct nvme_qpair *qpair)
740 nvme_qpair_destroy(qpair);
744 nvme_abort_complete(void *arg, const struct nvme_completion *status)
746 struct nvme_tracker *tr = arg;
749 * If cdw0 == 1, the controller was not able to abort the command
750 * we requested. We still need to check the active tracker array,
751 * to cover race where I/O timed out at same time controller was
752 * completing the I/O.
754 if (status->cdw0 == 1 && tr->qpair->act_tr[tr->cid] != NULL) {
756 * An I/O has timed out, and the controller was unable to
757 * abort it for some reason. Construct a fake completion
758 * status, and then complete the I/O's tracker manually.
760 nvme_printf(tr->qpair->ctrlr,
761 "abort command failed, aborting command manually\n");
762 nvme_qpair_manual_complete_tracker(tr->qpair, tr,
763 NVME_SCT_GENERIC, NVME_SC_ABORTED_BY_REQUEST, 0, TRUE);
768 nvme_timeout(void *arg)
770 struct nvme_tracker *tr = arg;
771 struct nvme_qpair *qpair = tr->qpair;
772 struct nvme_controller *ctrlr = qpair->ctrlr;
777 * Read csts to get value of cfs - controller fatal status.
778 * If no fatal status, try to call the completion routine, and
779 * if completes transactions, report a missed interrupt and
780 * return (this may need to be rate limited). Otherwise, if
781 * aborts are enabled and the controller is not reporting
782 * fatal status, abort the command. Otherwise, just reset the
783 * controller and hope for the best.
785 csts = nvme_mmio_read_4(ctrlr, csts);
786 cfs = (csts >> NVME_CSTS_REG_CFS_SHIFT) & NVME_CSTS_REG_CFS_MASK;
787 if (cfs == 0 && nvme_qpair_process_completions(qpair)) {
788 nvme_printf(ctrlr, "Missing interrupt\n");
791 if (ctrlr->enable_aborts && cfs == 0) {
792 nvme_printf(ctrlr, "Aborting command due to a timeout.\n");
793 nvme_ctrlr_cmd_abort(ctrlr, tr->cid, qpair->id,
794 nvme_abort_complete, tr);
796 nvme_printf(ctrlr, "Resetting controller due to a timeout%s.\n",
797 cfs ? " and fatal error status" : "");
798 nvme_ctrlr_reset(ctrlr);
803 nvme_qpair_submit_tracker(struct nvme_qpair *qpair, struct nvme_tracker *tr)
805 struct nvme_request *req;
806 struct nvme_controller *ctrlr;
808 mtx_assert(&qpair->lock, MA_OWNED);
811 req->cmd.cid = tr->cid;
812 qpair->act_tr[tr->cid] = tr;
813 ctrlr = qpair->ctrlr;
816 #if __FreeBSD_version >= 800030
817 callout_reset_curcpu(&tr->timer, ctrlr->timeout_period * hz,
820 callout_reset(&tr->timer, ctrlr->timeout_period * hz,
824 /* Copy the command from the tracker to the submission queue. */
825 memcpy(&qpair->cmd[qpair->sq_tail], &req->cmd, sizeof(req->cmd));
827 if (++qpair->sq_tail == qpair->num_entries)
831 nvme_mmio_write_4(qpair->ctrlr, doorbell[qpair->id].sq_tdbl,
838 nvme_payload_map(void *arg, bus_dma_segment_t *seg, int nseg, int error)
840 struct nvme_tracker *tr = arg;
844 * If the mapping operation failed, return immediately. The caller
845 * is responsible for detecting the error status and failing the
849 nvme_printf(tr->qpair->ctrlr,
850 "nvme_payload_map err %d\n", error);
855 * Note that we specified PAGE_SIZE for alignment and max
856 * segment size when creating the bus dma tags. So here
857 * we can safely just transfer each segment to its
858 * associated PRP entry.
860 tr->req->cmd.prp1 = htole64(seg[0].ds_addr);
863 tr->req->cmd.prp2 = htole64(seg[1].ds_addr);
864 } else if (nseg > 2) {
866 tr->req->cmd.prp2 = htole64((uint64_t)tr->prp_bus_addr);
867 while (cur_nseg < nseg) {
868 tr->prp[cur_nseg-1] =
869 htole64((uint64_t)seg[cur_nseg].ds_addr);
874 * prp2 should not be used by the controller
875 * since there is only one segment, but set
876 * to 0 just to be safe.
878 tr->req->cmd.prp2 = 0;
881 nvme_qpair_submit_tracker(tr->qpair, tr);
885 _nvme_qpair_submit_request(struct nvme_qpair *qpair, struct nvme_request *req)
887 struct nvme_tracker *tr;
890 mtx_assert(&qpair->lock, MA_OWNED);
892 tr = TAILQ_FIRST(&qpair->free_tr);
895 if (tr == NULL || !qpair->is_enabled) {
897 * No tracker is available, or the qpair is disabled due to
898 * an in-progress controller-level reset or controller
902 if (qpair->ctrlr->is_failed) {
904 * The controller has failed. Post the request to a
905 * task where it will be aborted, so that we do not
906 * invoke the request's callback in the context
909 nvme_ctrlr_post_failed_request(qpair->ctrlr, req);
912 * Put the request on the qpair's request queue to be
913 * processed when a tracker frees up via a command
914 * completion or when the controller reset is
917 STAILQ_INSERT_TAIL(&qpair->queued_req, req, stailq);
922 TAILQ_REMOVE(&qpair->free_tr, tr, tailq);
923 TAILQ_INSERT_TAIL(&qpair->outstanding_tr, tr, tailq);
927 case NVME_REQUEST_VADDR:
928 KASSERT(req->payload_size <= qpair->ctrlr->max_xfer_size,
929 ("payload_size (%d) exceeds max_xfer_size (%d)\n",
930 req->payload_size, qpair->ctrlr->max_xfer_size));
931 err = bus_dmamap_load(tr->qpair->dma_tag_payload,
932 tr->payload_dma_map, req->u.payload, req->payload_size,
933 nvme_payload_map, tr, 0);
935 nvme_printf(qpair->ctrlr,
936 "bus_dmamap_load returned 0x%x!\n", err);
938 case NVME_REQUEST_NULL:
939 nvme_qpair_submit_tracker(tr->qpair, tr);
941 #ifdef NVME_UNMAPPED_BIO_SUPPORT
942 case NVME_REQUEST_BIO:
943 KASSERT(req->u.bio->bio_bcount <= qpair->ctrlr->max_xfer_size,
944 ("bio->bio_bcount (%jd) exceeds max_xfer_size (%d)\n",
945 (intmax_t)req->u.bio->bio_bcount,
946 qpair->ctrlr->max_xfer_size));
947 err = bus_dmamap_load_bio(tr->qpair->dma_tag_payload,
948 tr->payload_dma_map, req->u.bio, nvme_payload_map, tr, 0);
950 nvme_printf(qpair->ctrlr,
951 "bus_dmamap_load_bio returned 0x%x!\n", err);
954 case NVME_REQUEST_CCB:
955 err = bus_dmamap_load_ccb(tr->qpair->dma_tag_payload,
956 tr->payload_dma_map, req->u.payload,
957 nvme_payload_map, tr, 0);
959 nvme_printf(qpair->ctrlr,
960 "bus_dmamap_load_ccb returned 0x%x!\n", err);
963 panic("unknown nvme request type 0x%x\n", req->type);
969 * The dmamap operation failed, so we manually fail the
970 * tracker here with DATA_TRANSFER_ERROR status.
972 * nvme_qpair_manual_complete_tracker must not be called
973 * with the qpair lock held.
975 mtx_unlock(&qpair->lock);
976 nvme_qpair_manual_complete_tracker(qpair, tr, NVME_SCT_GENERIC,
977 NVME_SC_DATA_TRANSFER_ERROR, 1 /* do not retry */, TRUE);
978 mtx_lock(&qpair->lock);
983 nvme_qpair_submit_request(struct nvme_qpair *qpair, struct nvme_request *req)
986 mtx_lock(&qpair->lock);
987 _nvme_qpair_submit_request(qpair, req);
988 mtx_unlock(&qpair->lock);
992 nvme_qpair_enable(struct nvme_qpair *qpair)
995 qpair->is_enabled = TRUE;
999 nvme_qpair_reset(struct nvme_qpair *qpair)
1002 qpair->sq_head = qpair->sq_tail = qpair->cq_head = 0;
1005 * First time through the completion queue, HW will set phase
1006 * bit on completions to 1. So set this to 1 here, indicating
1007 * we're looking for a 1 to know which entries have completed.
1008 * we'll toggle the bit each time when the completion queue
1013 memset(qpair->cmd, 0,
1014 qpair->num_entries * sizeof(struct nvme_command));
1015 memset(qpair->cpl, 0,
1016 qpair->num_entries * sizeof(struct nvme_completion));
1020 nvme_admin_qpair_enable(struct nvme_qpair *qpair)
1022 struct nvme_tracker *tr;
1023 struct nvme_tracker *tr_temp;
1026 * Manually abort each outstanding admin command. Do not retry
1027 * admin commands found here, since they will be left over from
1028 * a controller reset and its likely the context in which the
1029 * command was issued no longer applies.
1031 TAILQ_FOREACH_SAFE(tr, &qpair->outstanding_tr, tailq, tr_temp) {
1032 nvme_printf(qpair->ctrlr,
1033 "aborting outstanding admin command\n");
1034 nvme_qpair_manual_complete_tracker(qpair, tr, NVME_SCT_GENERIC,
1035 NVME_SC_ABORTED_BY_REQUEST, 1 /* do not retry */, TRUE);
1038 nvme_qpair_enable(qpair);
1042 nvme_io_qpair_enable(struct nvme_qpair *qpair)
1044 STAILQ_HEAD(, nvme_request) temp;
1045 struct nvme_tracker *tr;
1046 struct nvme_tracker *tr_temp;
1047 struct nvme_request *req;
1050 * Manually abort each outstanding I/O. This normally results in a
1051 * retry, unless the retry count on the associated request has
1052 * reached its limit.
1054 TAILQ_FOREACH_SAFE(tr, &qpair->outstanding_tr, tailq, tr_temp) {
1055 nvme_printf(qpair->ctrlr, "aborting outstanding i/o\n");
1056 nvme_qpair_manual_complete_tracker(qpair, tr, NVME_SCT_GENERIC,
1057 NVME_SC_ABORTED_BY_REQUEST, 0, TRUE);
1060 mtx_lock(&qpair->lock);
1062 nvme_qpair_enable(qpair);
1065 STAILQ_SWAP(&qpair->queued_req, &temp, nvme_request);
1067 while (!STAILQ_EMPTY(&temp)) {
1068 req = STAILQ_FIRST(&temp);
1069 STAILQ_REMOVE_HEAD(&temp, stailq);
1070 nvme_printf(qpair->ctrlr, "resubmitting queued i/o\n");
1071 nvme_qpair_print_command(qpair, &req->cmd);
1072 _nvme_qpair_submit_request(qpair, req);
1075 mtx_unlock(&qpair->lock);
1079 nvme_qpair_disable(struct nvme_qpair *qpair)
1081 struct nvme_tracker *tr;
1083 qpair->is_enabled = FALSE;
1084 mtx_lock(&qpair->lock);
1085 TAILQ_FOREACH(tr, &qpair->outstanding_tr, tailq)
1086 callout_stop(&tr->timer);
1087 mtx_unlock(&qpair->lock);
1091 nvme_admin_qpair_disable(struct nvme_qpair *qpair)
1094 nvme_qpair_disable(qpair);
1095 nvme_admin_qpair_abort_aers(qpair);
1099 nvme_io_qpair_disable(struct nvme_qpair *qpair)
1102 nvme_qpair_disable(qpair);
1106 nvme_qpair_fail(struct nvme_qpair *qpair)
1108 struct nvme_tracker *tr;
1109 struct nvme_request *req;
1111 if (!mtx_initialized(&qpair->lock))
1114 mtx_lock(&qpair->lock);
1116 while (!STAILQ_EMPTY(&qpair->queued_req)) {
1117 req = STAILQ_FIRST(&qpair->queued_req);
1118 STAILQ_REMOVE_HEAD(&qpair->queued_req, stailq);
1119 nvme_printf(qpair->ctrlr, "failing queued i/o\n");
1120 mtx_unlock(&qpair->lock);
1121 nvme_qpair_manual_complete_request(qpair, req, NVME_SCT_GENERIC,
1122 NVME_SC_ABORTED_BY_REQUEST, TRUE);
1123 mtx_lock(&qpair->lock);
1126 /* Manually abort each outstanding I/O. */
1127 while (!TAILQ_EMPTY(&qpair->outstanding_tr)) {
1128 tr = TAILQ_FIRST(&qpair->outstanding_tr);
1130 * Do not remove the tracker. The abort_tracker path will
1133 nvme_printf(qpair->ctrlr, "failing outstanding i/o\n");
1134 mtx_unlock(&qpair->lock);
1135 nvme_qpair_manual_complete_tracker(qpair, tr, NVME_SCT_GENERIC,
1136 NVME_SC_ABORTED_BY_REQUEST, 1 /* do not retry */, TRUE);
1137 mtx_lock(&qpair->lock);
1140 mtx_unlock(&qpair->lock);