2 * Copyright (C) 2012 Intel Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
33 #include <dev/pci/pcivar.h>
35 #include "nvme_private.h"
37 static void _nvme_qpair_submit_request(struct nvme_qpair *qpair,
38 struct nvme_request *req);
40 struct nvme_opcode_string {
46 static struct nvme_opcode_string admin_opcode[] = {
47 { NVME_OPC_DELETE_IO_SQ, "DELETE IO SQ" },
48 { NVME_OPC_CREATE_IO_SQ, "CREATE IO SQ" },
49 { NVME_OPC_GET_LOG_PAGE, "GET LOG PAGE" },
50 { NVME_OPC_DELETE_IO_CQ, "DELETE IO CQ" },
51 { NVME_OPC_CREATE_IO_CQ, "CREATE IO CQ" },
52 { NVME_OPC_IDENTIFY, "IDENTIFY" },
53 { NVME_OPC_ABORT, "ABORT" },
54 { NVME_OPC_SET_FEATURES, "SET FEATURES" },
55 { NVME_OPC_GET_FEATURES, "GET FEATURES" },
56 { NVME_OPC_ASYNC_EVENT_REQUEST, "ASYNC EVENT REQUEST" },
57 { NVME_OPC_FIRMWARE_ACTIVATE, "FIRMWARE ACTIVATE" },
58 { NVME_OPC_FIRMWARE_IMAGE_DOWNLOAD, "FIRMWARE IMAGE DOWNLOAD" },
59 { NVME_OPC_FORMAT_NVM, "FORMAT NVM" },
60 { NVME_OPC_SECURITY_SEND, "SECURITY SEND" },
61 { NVME_OPC_SECURITY_RECEIVE, "SECURITY RECEIVE" },
62 { 0xFFFF, "ADMIN COMMAND" }
65 static struct nvme_opcode_string io_opcode[] = {
66 { NVME_OPC_FLUSH, "FLUSH" },
67 { NVME_OPC_WRITE, "WRITE" },
68 { NVME_OPC_READ, "READ" },
69 { NVME_OPC_WRITE_UNCORRECTABLE, "WRITE UNCORRECTABLE" },
70 { NVME_OPC_COMPARE, "COMPARE" },
71 { NVME_OPC_DATASET_MANAGEMENT, "DATASET MANAGEMENT" },
72 { 0xFFFF, "IO COMMAND" }
76 get_admin_opcode_string(uint16_t opc)
78 struct nvme_opcode_string *entry;
82 while (entry->opc != 0xFFFF) {
83 if (entry->opc == opc)
91 get_io_opcode_string(uint16_t opc)
93 struct nvme_opcode_string *entry;
97 while (entry->opc != 0xFFFF) {
98 if (entry->opc == opc)
107 nvme_admin_qpair_print_command(struct nvme_qpair *qpair,
108 struct nvme_command *cmd)
111 nvme_printf(qpair->ctrlr, "%s (%02x) sqid:%d cid:%d nsid:%x "
112 "cdw10:%08x cdw11:%08x\n",
113 get_admin_opcode_string(cmd->opc), cmd->opc, qpair->id, cmd->cid,
114 cmd->nsid, cmd->cdw10, cmd->cdw11);
118 nvme_io_qpair_print_command(struct nvme_qpair *qpair,
119 struct nvme_command *cmd)
125 case NVME_OPC_WRITE_UNCORRECTABLE:
126 case NVME_OPC_COMPARE:
127 nvme_printf(qpair->ctrlr, "%s sqid:%d cid:%d nsid:%d "
129 get_io_opcode_string(cmd->opc), qpair->id, cmd->cid,
131 ((unsigned long long)cmd->cdw11 << 32) + cmd->cdw10,
132 (cmd->cdw12 & 0xFFFF) + 1);
135 case NVME_OPC_DATASET_MANAGEMENT:
136 nvme_printf(qpair->ctrlr, "%s sqid:%d cid:%d nsid:%d\n",
137 get_io_opcode_string(cmd->opc), qpair->id, cmd->cid,
141 nvme_printf(qpair->ctrlr, "%s (%02x) sqid:%d cid:%d nsid:%d\n",
142 get_io_opcode_string(cmd->opc), cmd->opc, qpair->id,
143 cmd->cid, cmd->nsid);
149 nvme_qpair_print_command(struct nvme_qpair *qpair, struct nvme_command *cmd)
152 nvme_admin_qpair_print_command(qpair, cmd);
154 nvme_io_qpair_print_command(qpair, cmd);
157 struct nvme_status_string {
163 static struct nvme_status_string generic_status[] = {
164 { NVME_SC_SUCCESS, "SUCCESS" },
165 { NVME_SC_INVALID_OPCODE, "INVALID OPCODE" },
166 { NVME_SC_INVALID_FIELD, "INVALID_FIELD" },
167 { NVME_SC_COMMAND_ID_CONFLICT, "COMMAND ID CONFLICT" },
168 { NVME_SC_DATA_TRANSFER_ERROR, "DATA TRANSFER ERROR" },
169 { NVME_SC_ABORTED_POWER_LOSS, "ABORTED - POWER LOSS" },
170 { NVME_SC_INTERNAL_DEVICE_ERROR, "INTERNAL DEVICE ERROR" },
171 { NVME_SC_ABORTED_BY_REQUEST, "ABORTED - BY REQUEST" },
172 { NVME_SC_ABORTED_SQ_DELETION, "ABORTED - SQ DELETION" },
173 { NVME_SC_ABORTED_FAILED_FUSED, "ABORTED - FAILED FUSED" },
174 { NVME_SC_ABORTED_MISSING_FUSED, "ABORTED - MISSING FUSED" },
175 { NVME_SC_INVALID_NAMESPACE_OR_FORMAT, "INVALID NAMESPACE OR FORMAT" },
176 { NVME_SC_COMMAND_SEQUENCE_ERROR, "COMMAND SEQUENCE ERROR" },
177 { NVME_SC_LBA_OUT_OF_RANGE, "LBA OUT OF RANGE" },
178 { NVME_SC_CAPACITY_EXCEEDED, "CAPACITY EXCEEDED" },
179 { NVME_SC_NAMESPACE_NOT_READY, "NAMESPACE NOT READY" },
180 { 0xFFFF, "GENERIC" }
183 static struct nvme_status_string command_specific_status[] = {
184 { NVME_SC_COMPLETION_QUEUE_INVALID, "INVALID COMPLETION QUEUE" },
185 { NVME_SC_INVALID_QUEUE_IDENTIFIER, "INVALID QUEUE IDENTIFIER" },
186 { NVME_SC_MAXIMUM_QUEUE_SIZE_EXCEEDED, "MAX QUEUE SIZE EXCEEDED" },
187 { NVME_SC_ABORT_COMMAND_LIMIT_EXCEEDED, "ABORT CMD LIMIT EXCEEDED" },
188 { NVME_SC_ASYNC_EVENT_REQUEST_LIMIT_EXCEEDED, "ASYNC LIMIT EXCEEDED" },
189 { NVME_SC_INVALID_FIRMWARE_SLOT, "INVALID FIRMWARE SLOT" },
190 { NVME_SC_INVALID_FIRMWARE_IMAGE, "INVALID FIRMWARE IMAGE" },
191 { NVME_SC_INVALID_INTERRUPT_VECTOR, "INVALID INTERRUPT VECTOR" },
192 { NVME_SC_INVALID_LOG_PAGE, "INVALID LOG PAGE" },
193 { NVME_SC_INVALID_FORMAT, "INVALID FORMAT" },
194 { NVME_SC_FIRMWARE_REQUIRES_RESET, "FIRMWARE REQUIRES RESET" },
195 { NVME_SC_CONFLICTING_ATTRIBUTES, "CONFLICTING ATTRIBUTES" },
196 { NVME_SC_INVALID_PROTECTION_INFO, "INVALID PROTECTION INFO" },
197 { NVME_SC_ATTEMPTED_WRITE_TO_RO_PAGE, "WRITE TO RO PAGE" },
198 { 0xFFFF, "COMMAND SPECIFIC" }
201 static struct nvme_status_string media_error_status[] = {
202 { NVME_SC_WRITE_FAULTS, "WRITE FAULTS" },
203 { NVME_SC_UNRECOVERED_READ_ERROR, "UNRECOVERED READ ERROR" },
204 { NVME_SC_GUARD_CHECK_ERROR, "GUARD CHECK ERROR" },
205 { NVME_SC_APPLICATION_TAG_CHECK_ERROR, "APPLICATION TAG CHECK ERROR" },
206 { NVME_SC_REFERENCE_TAG_CHECK_ERROR, "REFERENCE TAG CHECK ERROR" },
207 { NVME_SC_COMPARE_FAILURE, "COMPARE FAILURE" },
208 { NVME_SC_ACCESS_DENIED, "ACCESS DENIED" },
209 { 0xFFFF, "MEDIA ERROR" }
213 get_status_string(uint16_t sct, uint16_t sc)
215 struct nvme_status_string *entry;
218 case NVME_SCT_GENERIC:
219 entry = generic_status;
221 case NVME_SCT_COMMAND_SPECIFIC:
222 entry = command_specific_status;
224 case NVME_SCT_MEDIA_ERROR:
225 entry = media_error_status;
227 case NVME_SCT_VENDOR_SPECIFIC:
228 return ("VENDOR SPECIFIC");
233 while (entry->sc != 0xFFFF) {
242 nvme_qpair_print_completion(struct nvme_qpair *qpair,
243 struct nvme_completion *cpl)
245 nvme_printf(qpair->ctrlr, "%s (%02x/%02x) sqid:%d cid:%d cdw0:%x\n",
246 get_status_string(cpl->status.sct, cpl->status.sc),
247 cpl->status.sct, cpl->status.sc, cpl->sqid, cpl->cid, cpl->cdw0);
251 nvme_completion_is_retry(const struct nvme_completion *cpl)
254 * TODO: spec is not clear how commands that are aborted due
255 * to TLER will be marked. So for now, it seems
256 * NAMESPACE_NOT_READY is the only case where we should
257 * look at the DNR bit.
259 switch (cpl->status.sct) {
260 case NVME_SCT_GENERIC:
261 switch (cpl->status.sc) {
262 case NVME_SC_ABORTED_BY_REQUEST:
263 case NVME_SC_NAMESPACE_NOT_READY:
268 case NVME_SC_INVALID_OPCODE:
269 case NVME_SC_INVALID_FIELD:
270 case NVME_SC_COMMAND_ID_CONFLICT:
271 case NVME_SC_DATA_TRANSFER_ERROR:
272 case NVME_SC_ABORTED_POWER_LOSS:
273 case NVME_SC_INTERNAL_DEVICE_ERROR:
274 case NVME_SC_ABORTED_SQ_DELETION:
275 case NVME_SC_ABORTED_FAILED_FUSED:
276 case NVME_SC_ABORTED_MISSING_FUSED:
277 case NVME_SC_INVALID_NAMESPACE_OR_FORMAT:
278 case NVME_SC_COMMAND_SEQUENCE_ERROR:
279 case NVME_SC_LBA_OUT_OF_RANGE:
280 case NVME_SC_CAPACITY_EXCEEDED:
284 case NVME_SCT_COMMAND_SPECIFIC:
285 case NVME_SCT_MEDIA_ERROR:
286 case NVME_SCT_VENDOR_SPECIFIC:
293 nvme_qpair_construct_tracker(struct nvme_qpair *qpair, struct nvme_tracker *tr,
297 bus_dmamap_create(qpair->dma_tag, 0, &tr->payload_dma_map);
298 bus_dmamap_create(qpair->dma_tag, 0, &tr->prp_dma_map);
300 bus_dmamap_load(qpair->dma_tag, tr->prp_dma_map, tr->prp,
301 sizeof(tr->prp), nvme_single_map, &tr->prp_bus_addr, 0);
303 callout_init(&tr->timer, 1);
309 nvme_qpair_complete_tracker(struct nvme_qpair *qpair, struct nvme_tracker *tr,
310 struct nvme_completion *cpl, boolean_t print_on_error)
312 struct nvme_request *req;
313 boolean_t retry, error;
316 error = nvme_completion_is_error(cpl);
317 retry = error && nvme_completion_is_retry(cpl) &&
318 req->retries < nvme_retry_count;
320 if (error && print_on_error) {
321 nvme_qpair_print_command(qpair, &req->cmd);
322 nvme_qpair_print_completion(qpair, cpl);
325 qpair->act_tr[cpl->cid] = NULL;
327 KASSERT(cpl->cid == req->cmd.cid, ("cpl cid does not match cmd cid\n"));
329 if (req->cb_fn && !retry)
330 req->cb_fn(req->cb_arg, cpl);
332 mtx_lock(&qpair->lock);
333 callout_stop(&tr->timer);
337 nvme_qpair_submit_tracker(qpair, tr);
339 if (req->type != NVME_REQUEST_NULL)
340 bus_dmamap_unload(qpair->dma_tag,
341 tr->payload_dma_map);
343 nvme_free_request(req);
346 TAILQ_REMOVE(&qpair->outstanding_tr, tr, tailq);
347 TAILQ_INSERT_HEAD(&qpair->free_tr, tr, tailq);
350 * If the controller is in the middle of resetting, don't
351 * try to submit queued requests here - let the reset logic
352 * handle that instead.
354 if (!STAILQ_EMPTY(&qpair->queued_req) &&
355 !qpair->ctrlr->is_resetting) {
356 req = STAILQ_FIRST(&qpair->queued_req);
357 STAILQ_REMOVE_HEAD(&qpair->queued_req, stailq);
358 _nvme_qpair_submit_request(qpair, req);
362 mtx_unlock(&qpair->lock);
366 nvme_qpair_manual_complete_tracker(struct nvme_qpair *qpair,
367 struct nvme_tracker *tr, uint32_t sct, uint32_t sc, uint32_t dnr,
368 boolean_t print_on_error)
370 struct nvme_completion cpl;
372 memset(&cpl, 0, sizeof(cpl));
373 cpl.sqid = qpair->id;
375 cpl.status.sct = sct;
377 cpl.status.dnr = dnr;
378 nvme_qpair_complete_tracker(qpair, tr, &cpl, print_on_error);
382 nvme_qpair_manual_complete_request(struct nvme_qpair *qpair,
383 struct nvme_request *req, uint32_t sct, uint32_t sc,
384 boolean_t print_on_error)
386 struct nvme_completion cpl;
389 memset(&cpl, 0, sizeof(cpl));
390 cpl.sqid = qpair->id;
391 cpl.status.sct = sct;
394 error = nvme_completion_is_error(&cpl);
396 if (error && print_on_error) {
397 nvme_qpair_print_command(qpair, &req->cmd);
398 nvme_qpair_print_completion(qpair, &cpl);
402 req->cb_fn(req->cb_arg, &cpl);
404 nvme_free_request(req);
408 nvme_qpair_process_completions(struct nvme_qpair *qpair)
410 struct nvme_tracker *tr;
411 struct nvme_completion *cpl;
413 qpair->num_intr_handler_calls++;
415 if (!qpair->is_enabled)
417 * qpair is not enabled, likely because a controller reset is
418 * is in progress. Ignore the interrupt - any I/O that was
419 * associated with this interrupt will get retried when the
425 cpl = &qpair->cpl[qpair->cq_head];
427 if (cpl->status.p != qpair->phase)
430 tr = qpair->act_tr[cpl->cid];
433 nvme_qpair_complete_tracker(qpair, tr, cpl, TRUE);
434 qpair->sq_head = cpl->sqhd;
436 nvme_printf(qpair->ctrlr,
437 "cpl does not map to outstanding cmd\n");
438 nvme_dump_completion(cpl);
439 KASSERT(0, ("received completion for unknown cmd\n"));
442 if (++qpair->cq_head == qpair->num_entries) {
444 qpair->phase = !qpair->phase;
447 nvme_mmio_write_4(qpair->ctrlr, doorbell[qpair->id].cq_hdbl,
453 nvme_qpair_msix_handler(void *arg)
455 struct nvme_qpair *qpair = arg;
457 nvme_qpair_process_completions(qpair);
461 nvme_qpair_construct(struct nvme_qpair *qpair, uint32_t id,
462 uint16_t vector, uint32_t num_entries, uint32_t num_trackers,
463 uint32_t max_xfer_size, struct nvme_controller *ctrlr)
465 struct nvme_tracker *tr;
469 qpair->vector = vector;
470 qpair->num_entries = num_entries;
473 * Chatham prototype board starts having issues at higher queue
474 * depths. So use a conservative estimate here of no more than 64
475 * outstanding I/O per queue at any one point.
477 if (pci_get_devid(ctrlr->dev) == CHATHAM_PCI_ID)
478 num_trackers = min(num_trackers, 64);
480 qpair->num_trackers = num_trackers;
481 qpair->max_xfer_size = max_xfer_size;
482 qpair->ctrlr = ctrlr;
484 if (ctrlr->msix_enabled) {
487 * MSI-X vector resource IDs start at 1, so we add one to
488 * the queue's vector to get the corresponding rid to use.
490 qpair->rid = vector + 1;
492 qpair->res = bus_alloc_resource_any(ctrlr->dev, SYS_RES_IRQ,
493 &qpair->rid, RF_ACTIVE);
495 bus_setup_intr(ctrlr->dev, qpair->res,
496 INTR_TYPE_MISC | INTR_MPSAFE, NULL,
497 nvme_qpair_msix_handler, qpair, &qpair->tag);
500 mtx_init(&qpair->lock, "nvme qpair lock", NULL, MTX_DEF);
502 bus_dma_tag_create(bus_get_dma_tag(ctrlr->dev),
503 sizeof(uint64_t), PAGE_SIZE, BUS_SPACE_MAXADDR,
504 BUS_SPACE_MAXADDR, NULL, NULL, qpair->max_xfer_size,
505 (qpair->max_xfer_size/PAGE_SIZE)+1, PAGE_SIZE, 0,
506 NULL, NULL, &qpair->dma_tag);
509 qpair->num_intr_handler_calls = 0;
511 qpair->cmd = contigmalloc(qpair->num_entries *
512 sizeof(struct nvme_command), M_NVME, M_ZERO,
513 0, BUS_SPACE_MAXADDR, PAGE_SIZE, 0);
514 qpair->cpl = contigmalloc(qpair->num_entries *
515 sizeof(struct nvme_completion), M_NVME, M_ZERO,
516 0, BUS_SPACE_MAXADDR, PAGE_SIZE, 0);
518 bus_dmamap_create(qpair->dma_tag, 0, &qpair->cmd_dma_map);
519 bus_dmamap_create(qpair->dma_tag, 0, &qpair->cpl_dma_map);
521 bus_dmamap_load(qpair->dma_tag, qpair->cmd_dma_map,
522 qpair->cmd, qpair->num_entries * sizeof(struct nvme_command),
523 nvme_single_map, &qpair->cmd_bus_addr, 0);
524 bus_dmamap_load(qpair->dma_tag, qpair->cpl_dma_map,
525 qpair->cpl, qpair->num_entries * sizeof(struct nvme_completion),
526 nvme_single_map, &qpair->cpl_bus_addr, 0);
528 qpair->sq_tdbl_off = nvme_mmio_offsetof(doorbell[id].sq_tdbl);
529 qpair->cq_hdbl_off = nvme_mmio_offsetof(doorbell[id].cq_hdbl);
531 TAILQ_INIT(&qpair->free_tr);
532 TAILQ_INIT(&qpair->outstanding_tr);
533 STAILQ_INIT(&qpair->queued_req);
535 for (i = 0; i < qpair->num_trackers; i++) {
536 tr = malloc(sizeof(*tr), M_NVME, M_ZERO | M_WAITOK);
537 nvme_qpair_construct_tracker(qpair, tr, i);
538 TAILQ_INSERT_HEAD(&qpair->free_tr, tr, tailq);
541 qpair->act_tr = malloc(sizeof(struct nvme_tracker *) * qpair->num_entries,
542 M_NVME, M_ZERO | M_WAITOK);
546 nvme_qpair_destroy(struct nvme_qpair *qpair)
548 struct nvme_tracker *tr;
551 bus_teardown_intr(qpair->ctrlr->dev, qpair->res, qpair->tag);
554 bus_release_resource(qpair->ctrlr->dev, SYS_RES_IRQ,
555 rman_get_rid(qpair->res), qpair->res);
558 bus_dmamap_unload(qpair->dma_tag, qpair->cmd_dma_map);
559 bus_dmamap_destroy(qpair->dma_tag, qpair->cmd_dma_map);
560 contigfree(qpair->cmd,
561 qpair->num_entries * sizeof(struct nvme_command), M_NVME);
565 bus_dmamap_unload(qpair->dma_tag, qpair->cpl_dma_map);
566 bus_dmamap_destroy(qpair->dma_tag, qpair->cpl_dma_map);
567 contigfree(qpair->cpl,
568 qpair->num_entries * sizeof(struct nvme_completion),
573 bus_dma_tag_destroy(qpair->dma_tag);
576 free(qpair->act_tr, M_NVME);
578 while (!TAILQ_EMPTY(&qpair->free_tr)) {
579 tr = TAILQ_FIRST(&qpair->free_tr);
580 TAILQ_REMOVE(&qpair->free_tr, tr, tailq);
581 bus_dmamap_destroy(qpair->dma_tag, tr->payload_dma_map);
582 bus_dmamap_destroy(qpair->dma_tag, tr->prp_dma_map);
588 nvme_admin_qpair_abort_aers(struct nvme_qpair *qpair)
590 struct nvme_tracker *tr;
592 tr = TAILQ_FIRST(&qpair->outstanding_tr);
594 if (tr->req->cmd.opc == NVME_OPC_ASYNC_EVENT_REQUEST) {
595 nvme_qpair_manual_complete_tracker(qpair, tr,
596 NVME_SCT_GENERIC, NVME_SC_ABORTED_SQ_DELETION, 0,
598 tr = TAILQ_FIRST(&qpair->outstanding_tr);
600 tr = TAILQ_NEXT(tr, tailq);
606 nvme_admin_qpair_destroy(struct nvme_qpair *qpair)
609 nvme_admin_qpair_abort_aers(qpair);
610 nvme_qpair_destroy(qpair);
614 nvme_io_qpair_destroy(struct nvme_qpair *qpair)
617 nvme_qpair_destroy(qpair);
621 nvme_abort_complete(void *arg, const struct nvme_completion *status)
623 struct nvme_tracker *tr = arg;
626 * If cdw0 == 1, the controller was not able to abort the command
627 * we requested. We still need to check the active tracker array,
628 * to cover race where I/O timed out at same time controller was
629 * completing the I/O.
631 if (status->cdw0 == 1 && tr->qpair->act_tr[tr->cid] != NULL) {
633 * An I/O has timed out, and the controller was unable to
634 * abort it for some reason. Construct a fake completion
635 * status, and then complete the I/O's tracker manually.
637 nvme_printf(tr->qpair->ctrlr,
638 "abort command failed, aborting command manually\n");
639 nvme_qpair_manual_complete_tracker(tr->qpair, tr,
640 NVME_SCT_GENERIC, NVME_SC_ABORTED_BY_REQUEST, 0, TRUE);
645 nvme_timeout(void *arg)
647 struct nvme_tracker *tr = arg;
648 struct nvme_qpair *qpair = tr->qpair;
649 struct nvme_controller *ctrlr = qpair->ctrlr;
650 union csts_register csts;
652 /* Read csts to get value of cfs - controller fatal status. */
653 csts.raw = nvme_mmio_read_4(ctrlr, csts);
655 if (ctrlr->enable_aborts && csts.bits.cfs == 0) {
657 * If aborts are enabled, only use them if the controller is
658 * not reporting fatal status.
660 nvme_ctrlr_cmd_abort(ctrlr, tr->cid, qpair->id,
661 nvme_abort_complete, tr);
663 nvme_ctrlr_reset(ctrlr);
667 nvme_qpair_submit_tracker(struct nvme_qpair *qpair, struct nvme_tracker *tr)
669 struct nvme_request *req;
670 struct nvme_controller *ctrlr;
672 mtx_assert(&qpair->lock, MA_OWNED);
675 req->cmd.cid = tr->cid;
676 qpair->act_tr[tr->cid] = tr;
677 ctrlr = qpair->ctrlr;
680 #if __FreeBSD_version >= 800030
681 callout_reset_curcpu(&tr->timer, ctrlr->timeout_period * hz,
684 callout_reset(&tr->timer, ctrlr->timeout_period * hz,
688 /* Copy the command from the tracker to the submission queue. */
689 memcpy(&qpair->cmd[qpair->sq_tail], &req->cmd, sizeof(req->cmd));
691 if (++qpair->sq_tail == qpair->num_entries)
695 nvme_mmio_write_4(qpair->ctrlr, doorbell[qpair->id].sq_tdbl,
702 nvme_payload_map(void *arg, bus_dma_segment_t *seg, int nseg, int error)
704 struct nvme_tracker *tr = arg;
708 * If the mapping operation failed, return immediately. The caller
709 * is responsible for detecting the error status and failing the
716 * Note that we specified PAGE_SIZE for alignment and max
717 * segment size when creating the bus dma tags. So here
718 * we can safely just transfer each segment to its
719 * associated PRP entry.
721 tr->req->cmd.prp1 = seg[0].ds_addr;
724 tr->req->cmd.prp2 = seg[1].ds_addr;
725 } else if (nseg > 2) {
727 tr->req->cmd.prp2 = (uint64_t)tr->prp_bus_addr;
728 while (cur_nseg < nseg) {
729 tr->prp[cur_nseg-1] =
730 (uint64_t)seg[cur_nseg].ds_addr;
735 nvme_qpair_submit_tracker(tr->qpair, tr);
739 nvme_payload_map_uio(void *arg, bus_dma_segment_t *seg, int nseg,
740 bus_size_t mapsize, int error)
743 nvme_payload_map(arg, seg, nseg, error);
747 _nvme_qpair_submit_request(struct nvme_qpair *qpair, struct nvme_request *req)
749 struct nvme_tracker *tr;
752 mtx_assert(&qpair->lock, MA_OWNED);
754 tr = TAILQ_FIRST(&qpair->free_tr);
757 if (tr == NULL || !qpair->is_enabled) {
759 * No tracker is available, or the qpair is disabled due to
760 * an in-progress controller-level reset or controller
764 if (qpair->ctrlr->is_failed) {
766 * The controller has failed. Post the request to a
767 * task where it will be aborted, so that we do not
768 * invoke the request's callback in the context
771 nvme_ctrlr_post_failed_request(qpair->ctrlr, req);
774 * Put the request on the qpair's request queue to be
775 * processed when a tracker frees up via a command
776 * completion or when the controller reset is
779 STAILQ_INSERT_TAIL(&qpair->queued_req, req, stailq);
784 TAILQ_REMOVE(&qpair->free_tr, tr, tailq);
785 TAILQ_INSERT_TAIL(&qpair->outstanding_tr, tr, tailq);
789 case NVME_REQUEST_VADDR:
790 err = bus_dmamap_load(tr->qpair->dma_tag, tr->payload_dma_map,
791 req->u.payload, req->payload_size, nvme_payload_map, tr, 0);
793 nvme_printf(qpair->ctrlr,
794 "bus_dmamap_load returned 0x%x!\n", err);
796 case NVME_REQUEST_NULL:
797 nvme_qpair_submit_tracker(tr->qpair, tr);
799 case NVME_REQUEST_UIO:
800 err = bus_dmamap_load_uio(tr->qpair->dma_tag,
801 tr->payload_dma_map, req->u.uio, nvme_payload_map_uio,
804 nvme_printf(qpair->ctrlr,
805 "bus_dmamap_load_uio returned 0x%x!\n", err);
807 #ifdef NVME_UNMAPPED_BIO_SUPPORT
808 case NVME_REQUEST_BIO:
809 err = bus_dmamap_load_bio(tr->qpair->dma_tag,
810 tr->payload_dma_map, req->u.bio, nvme_payload_map, tr, 0);
812 nvme_printf(qpair->ctrlr,
813 "bus_dmamap_load_bio returned 0x%x!\n", err);
817 panic("unknown nvme request type 0x%x\n", req->type);
823 * The dmamap operation failed, so we manually fail the
824 * tracker here with DATA_TRANSFER_ERROR status.
826 * nvme_qpair_manual_complete_tracker must not be called
827 * with the qpair lock held.
829 mtx_unlock(&qpair->lock);
830 nvme_qpair_manual_complete_tracker(qpair, tr, NVME_SCT_GENERIC,
831 NVME_SC_DATA_TRANSFER_ERROR, 1 /* do not retry */, TRUE);
832 mtx_lock(&qpair->lock);
837 nvme_qpair_submit_request(struct nvme_qpair *qpair, struct nvme_request *req)
840 mtx_lock(&qpair->lock);
841 _nvme_qpair_submit_request(qpair, req);
842 mtx_unlock(&qpair->lock);
846 nvme_qpair_enable(struct nvme_qpair *qpair)
849 qpair->is_enabled = TRUE;
853 nvme_qpair_reset(struct nvme_qpair *qpair)
856 qpair->sq_head = qpair->sq_tail = qpair->cq_head = 0;
859 * First time through the completion queue, HW will set phase
860 * bit on completions to 1. So set this to 1 here, indicating
861 * we're looking for a 1 to know which entries have completed.
862 * we'll toggle the bit each time when the completion queue
867 memset(qpair->cmd, 0,
868 qpair->num_entries * sizeof(struct nvme_command));
869 memset(qpair->cpl, 0,
870 qpair->num_entries * sizeof(struct nvme_completion));
874 nvme_admin_qpair_enable(struct nvme_qpair *qpair)
876 struct nvme_tracker *tr;
877 struct nvme_tracker *tr_temp;
880 * Manually abort each outstanding admin command. Do not retry
881 * admin commands found here, since they will be left over from
882 * a controller reset and its likely the context in which the
883 * command was issued no longer applies.
885 TAILQ_FOREACH_SAFE(tr, &qpair->outstanding_tr, tailq, tr_temp) {
886 nvme_printf(qpair->ctrlr,
887 "aborting outstanding admin command\n");
888 nvme_qpair_manual_complete_tracker(qpair, tr, NVME_SCT_GENERIC,
889 NVME_SC_ABORTED_BY_REQUEST, 1 /* do not retry */, TRUE);
892 nvme_qpair_enable(qpair);
896 nvme_io_qpair_enable(struct nvme_qpair *qpair)
898 STAILQ_HEAD(, nvme_request) temp;
899 struct nvme_tracker *tr;
900 struct nvme_tracker *tr_temp;
901 struct nvme_request *req;
904 * Manually abort each outstanding I/O. This normally results in a
905 * retry, unless the retry count on the associated request has
908 TAILQ_FOREACH_SAFE(tr, &qpair->outstanding_tr, tailq, tr_temp) {
909 nvme_printf(qpair->ctrlr, "aborting outstanding i/o\n");
910 nvme_qpair_manual_complete_tracker(qpair, tr, NVME_SCT_GENERIC,
911 NVME_SC_ABORTED_BY_REQUEST, 0, TRUE);
914 mtx_lock(&qpair->lock);
916 nvme_qpair_enable(qpair);
919 STAILQ_SWAP(&qpair->queued_req, &temp, nvme_request);
921 while (!STAILQ_EMPTY(&temp)) {
922 req = STAILQ_FIRST(&temp);
923 STAILQ_REMOVE_HEAD(&temp, stailq);
924 nvme_printf(qpair->ctrlr, "resubmitting queued i/o\n");
925 nvme_qpair_print_command(qpair, &req->cmd);
926 _nvme_qpair_submit_request(qpair, req);
929 mtx_unlock(&qpair->lock);
933 nvme_qpair_disable(struct nvme_qpair *qpair)
935 struct nvme_tracker *tr;
937 qpair->is_enabled = FALSE;
938 mtx_lock(&qpair->lock);
939 TAILQ_FOREACH(tr, &qpair->outstanding_tr, tailq)
940 callout_stop(&tr->timer);
941 mtx_unlock(&qpair->lock);
945 nvme_admin_qpair_disable(struct nvme_qpair *qpair)
948 nvme_qpair_disable(qpair);
949 nvme_admin_qpair_abort_aers(qpair);
953 nvme_io_qpair_disable(struct nvme_qpair *qpair)
956 nvme_qpair_disable(qpair);
960 nvme_qpair_fail(struct nvme_qpair *qpair)
962 struct nvme_tracker *tr;
963 struct nvme_request *req;
965 mtx_lock(&qpair->lock);
967 while (!STAILQ_EMPTY(&qpair->queued_req)) {
968 req = STAILQ_FIRST(&qpair->queued_req);
969 STAILQ_REMOVE_HEAD(&qpair->queued_req, stailq);
970 nvme_printf(qpair->ctrlr, "failing queued i/o\n");
971 mtx_unlock(&qpair->lock);
972 nvme_qpair_manual_complete_request(qpair, req, NVME_SCT_GENERIC,
973 NVME_SC_ABORTED_BY_REQUEST, TRUE);
974 mtx_lock(&qpair->lock);
977 /* Manually abort each outstanding I/O. */
978 while (!TAILQ_EMPTY(&qpair->outstanding_tr)) {
979 tr = TAILQ_FIRST(&qpair->outstanding_tr);
981 * Do not remove the tracker. The abort_tracker path will
984 nvme_printf(qpair->ctrlr, "failing outstanding i/o\n");
985 mtx_unlock(&qpair->lock);
986 nvme_qpair_manual_complete_tracker(qpair, tr, NVME_SCT_GENERIC,
987 NVME_SC_ABORTED_BY_REQUEST, 1 /* do not retry */, TRUE);
988 mtx_lock(&qpair->lock);
991 mtx_unlock(&qpair->lock);