Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright (C) 2021 Intel Corporation. All rights reserved.
3 : : * Copyright (c) 2021 Mellanox Technologies LTD. All rights reserved.
4 : : * Copyright (c) 2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
5 : : */
6 : :
7 : : /*
8 : : * NVMe over PCIe common library
9 : : */
10 : :
11 : : #include "spdk/stdinc.h"
12 : : #include "spdk/likely.h"
13 : : #include "spdk/string.h"
14 : : #include "nvme_internal.h"
15 : : #include "nvme_pcie_internal.h"
16 : : #include "spdk/trace.h"
17 : :
18 : : #include "spdk_internal/trace_defs.h"
19 : :
20 : : __thread struct nvme_pcie_ctrlr *g_thread_mmio_ctrlr = NULL;
21 : :
22 : : static struct spdk_nvme_pcie_stat g_dummy_stat = {};
23 : :
24 : : static void nvme_pcie_fail_request_bad_vtophys(struct spdk_nvme_qpair *qpair,
25 : : struct nvme_tracker *tr);
26 : :
27 : : static inline uint64_t
28 : 181678454 : nvme_pcie_vtophys(struct spdk_nvme_ctrlr *ctrlr, const void *buf, uint64_t *size)
29 : : {
30 [ + + + - : 181678454 : if (spdk_likely(ctrlr->trid.trtype == SPDK_NVME_TRANSPORT_PCIE)) {
+ - + + ]
31 : 180249868 : return spdk_vtophys(buf, size);
32 : : } else {
33 : : /* vfio-user address translation with IOVA=VA mode */
34 : 1428586 : return (uint64_t)(uintptr_t)buf;
35 : : }
36 : 162036 : }
37 : :
38 : : int
39 : 4943 : nvme_pcie_qpair_reset(struct spdk_nvme_qpair *qpair)
40 : : {
41 : 4943 : struct nvme_pcie_qpair *pqpair = nvme_pcie_qpair(qpair);
42 : 6 : uint32_t i;
43 : :
44 : : /* all head/tail vals are set to 0 */
45 [ + - + - : 4943 : pqpair->last_sq_tail = pqpair->sq_tail = pqpair->sq_head = pqpair->cq_head = 0;
+ - + - +
- + - + -
+ - ]
46 : :
47 : : /*
48 : : * First time through the completion queue, HW will set phase
49 : : * bit on completions to 1. So set this to 1 here, indicating
50 : : * we're looking for a 1 to know which entries have completed.
51 : : * we'll toggle the bit each time when the completion queue
52 : : * rolls over.
53 : : */
54 [ + - + - ]: 4943 : pqpair->flags.phase = 1;
55 [ + + + - : 2507707 : for (i = 0; i < pqpair->num_entries; i++) {
+ + ]
56 [ + - + - : 2502764 : pqpair->cpl[i].status.p = 0;
+ - + - +
- + - ]
57 : 10536 : }
58 : :
59 : 4943 : return 0;
60 : 6 : }
61 : :
62 : : static void
63 : 1031409 : nvme_qpair_construct_tracker(struct nvme_tracker *tr, uint16_t cid, uint64_t phys_addr)
64 : : {
65 [ + - + - ]: 1031409 : tr->prp_sgl_bus_addr = phys_addr + offsetof(struct nvme_tracker, u.prp);
66 [ + - + - ]: 1031409 : tr->cid = cid;
67 [ + - + - ]: 1031409 : tr->req = NULL;
68 : 1031409 : }
69 : :
70 : : static void *
71 : 24 : nvme_pcie_ctrlr_alloc_cmb(struct spdk_nvme_ctrlr *ctrlr, uint64_t size, uint64_t alignment,
72 : : uint64_t *phys_addr)
73 : : {
74 : 24 : struct nvme_pcie_ctrlr *pctrlr = nvme_pcie_ctrlr(ctrlr);
75 : 4 : uintptr_t addr;
76 : :
77 [ + + + - : 24 : if (pctrlr->cmb.mem_register_addr != NULL) {
+ - + + ]
78 : : /* BAR is mapped for data */
79 : 6 : return NULL;
80 : : }
81 : :
82 [ + - + - : 18 : addr = (uintptr_t)pctrlr->cmb.bar_va + pctrlr->cmb.current_offset;
+ - + - +
- + - ]
83 : 18 : addr = (addr + (alignment - 1)) & ~(alignment - 1);
84 : :
85 : : /* CMB may only consume part of the BAR, calculate accordingly */
86 [ + + + - : 18 : if (addr + size > ((uintptr_t)pctrlr->cmb.bar_va + pctrlr->cmb.size)) {
+ - + - +
- + - +
+ ]
87 : 6 : SPDK_ERRLOG("Tried to allocate past valid CMB range!\n");
88 : 6 : return NULL;
89 : : }
90 [ + - + - : 12 : *phys_addr = pctrlr->cmb.bar_pa + addr - (uintptr_t)pctrlr->cmb.bar_va;
+ - + - +
- + - +
- ]
91 : :
92 [ + - + - : 12 : pctrlr->cmb.current_offset = (addr + size) - (uintptr_t)pctrlr->cmb.bar_va;
+ - + - +
- + - ]
93 : :
94 : 12 : return (void *)addr;
95 : 4 : }
96 : :
97 : : int
98 : 2420 : nvme_pcie_qpair_construct(struct spdk_nvme_qpair *qpair,
99 : : const struct spdk_nvme_io_qpair_opts *opts)
100 : : {
101 [ + - + - ]: 2420 : struct spdk_nvme_ctrlr *ctrlr = qpair->ctrlr;
102 : 2420 : struct nvme_pcie_ctrlr *pctrlr = nvme_pcie_ctrlr(ctrlr);
103 : 2420 : struct nvme_pcie_qpair *pqpair = nvme_pcie_qpair(qpair);
104 : 4 : struct nvme_tracker *tr;
105 : 4 : uint16_t i;
106 : 4 : uint16_t num_trackers;
107 : 2420 : size_t page_align = sysconf(_SC_PAGESIZE);
108 : 4 : size_t queue_align, queue_len;
109 : 2420 : uint32_t flags = SPDK_MALLOC_DMA;
110 : 2420 : uint64_t sq_paddr = 0;
111 : 2420 : uint64_t cq_paddr = 0;
112 : :
113 [ + + ]: 2420 : if (opts) {
114 [ + - + - : 1716 : pqpair->sq_vaddr = opts->sq.vaddr;
+ - + - +
- ]
115 [ + - + - : 1716 : pqpair->cq_vaddr = opts->cq.vaddr;
+ - + - +
- ]
116 [ + - + - : 1716 : sq_paddr = opts->sq.paddr;
+ - ]
117 [ + - + - : 1716 : cq_paddr = opts->cq.paddr;
+ - ]
118 : 11 : }
119 : :
120 [ + - + - : 2420 : pqpair->retry_count = ctrlr->opts.transport_retry_count;
+ - + - +
- ]
121 : :
122 : : /*
123 : : * Limit the maximum number of completions to return per call to prevent wraparound,
124 : : * and calculate how many trackers can be submitted at once without overflowing the
125 : : * completion queue.
126 : : */
127 [ + - + - : 2420 : pqpair->max_completions_cap = pqpair->num_entries / 4;
+ - + - +
- ]
128 [ + - + - : 2420 : pqpair->max_completions_cap = spdk_max(pqpair->max_completions_cap, NVME_MIN_COMPLETIONS);
+ + + - +
- + - +
- ]
129 [ + - + - : 2420 : pqpair->max_completions_cap = spdk_min(pqpair->max_completions_cap, NVME_MAX_COMPLETIONS);
+ + + - +
- + - +
- ]
130 [ + - + - : 2420 : num_trackers = pqpair->num_entries - pqpair->max_completions_cap;
+ - + - ]
131 : :
132 [ + + + + : 2420 : SPDK_INFOLOG(nvme, "max_completions_cap = %" PRIu16 " num_trackers = %" PRIu16 "\n",
+ - # # #
# ]
133 : : pqpair->max_completions_cap, num_trackers);
134 : :
135 [ + + # # ]: 2420 : assert(num_trackers != 0);
136 : :
137 [ + - + - ]: 2420 : pqpair->sq_in_cmb = false;
138 : :
139 [ + + + + ]: 2420 : if (nvme_qpair_is_admin_queue(&pqpair->qpair)) {
140 : 698 : flags |= SPDK_MALLOC_SHARE;
141 : 10 : }
142 : :
143 : : /* cmd and cpl rings must be aligned on page size boundaries. */
144 [ + + + + : 2420 : if (ctrlr->opts.use_cmb_sqs) {
+ - + - +
+ ]
145 [ + - + - : 6 : pqpair->cmd = nvme_pcie_ctrlr_alloc_cmb(ctrlr, pqpair->num_entries * sizeof(struct spdk_nvme_cmd),
+ - + - ]
146 [ + - ]: 1 : page_align, &pqpair->cmd_bus_addr);
147 [ + - + - : 6 : if (pqpair->cmd != NULL) {
- + ]
148 [ + - + - ]: 6 : pqpair->sq_in_cmb = true;
149 : 1 : }
150 : 1 : }
151 : :
152 [ + + + + : 2420 : if (pqpair->sq_in_cmb == false) {
+ - + + ]
153 [ + + + - : 2414 : if (pqpair->sq_vaddr) {
+ + ]
154 [ + - + - : 6 : pqpair->cmd = pqpair->sq_vaddr;
+ - + - ]
155 : 1 : } else {
156 : : /* To ensure physical address contiguity we make each ring occupy
157 : : * a single hugepage only. See MAX_IO_QUEUE_ENTRIES.
158 : : */
159 [ + - + - ]: 2408 : queue_len = pqpair->num_entries * sizeof(struct spdk_nvme_cmd);
160 [ + + ]: 2408 : queue_align = spdk_max(spdk_align32pow2(queue_len), page_align);
161 [ + - + - ]: 2408 : pqpair->cmd = spdk_zmalloc(queue_len, queue_align, NULL, SPDK_ENV_SOCKET_ID_ANY, flags);
162 [ + + + - : 2408 : if (pqpair->cmd == NULL) {
- + ]
163 : 0 : SPDK_ERRLOG("alloc qpair_cmd failed\n");
164 : 0 : return -ENOMEM;
165 : : }
166 : : }
167 [ + + ]: 2414 : if (sq_paddr) {
168 [ + + + - : 6 : assert(pqpair->sq_vaddr != NULL);
+ - # # ]
169 [ + - + - ]: 6 : pqpair->cmd_bus_addr = sq_paddr;
170 : 1 : } else {
171 [ + - + - : 2408 : pqpair->cmd_bus_addr = nvme_pcie_vtophys(ctrlr, pqpair->cmd, NULL);
+ - + - ]
172 [ + + + - : 2408 : if (pqpair->cmd_bus_addr == SPDK_VTOPHYS_ERROR) {
+ - ]
173 : 0 : SPDK_ERRLOG("spdk_vtophys(pqpair->cmd) failed\n");
174 : 0 : return -EFAULT;
175 : : }
176 : : }
177 : 21 : }
178 : :
179 [ + + + - : 2420 : if (pqpair->cq_vaddr) {
+ + ]
180 [ + - + - : 12 : pqpair->cpl = pqpair->cq_vaddr;
+ - + - ]
181 : 2 : } else {
182 [ + - + - ]: 2408 : queue_len = pqpair->num_entries * sizeof(struct spdk_nvme_cpl);
183 [ + + ]: 2408 : queue_align = spdk_max(spdk_align32pow2(queue_len), page_align);
184 [ + - + - ]: 2408 : pqpair->cpl = spdk_zmalloc(queue_len, queue_align, NULL, SPDK_ENV_SOCKET_ID_ANY, flags);
185 [ + + + - : 2408 : if (pqpair->cpl == NULL) {
+ - ]
186 : 0 : SPDK_ERRLOG("alloc qpair_cpl failed\n");
187 : 0 : return -ENOMEM;
188 : : }
189 : : }
190 [ + + ]: 2420 : if (cq_paddr) {
191 [ + + + - : 12 : assert(pqpair->cq_vaddr != NULL);
+ - # # ]
192 [ + - + - ]: 12 : pqpair->cpl_bus_addr = cq_paddr;
193 : 2 : } else {
194 [ + - + - : 2408 : pqpair->cpl_bus_addr = nvme_pcie_vtophys(ctrlr, pqpair->cpl, NULL);
+ - + - ]
195 [ + + + - : 2408 : if (pqpair->cpl_bus_addr == SPDK_VTOPHYS_ERROR) {
+ - ]
196 : 0 : SPDK_ERRLOG("spdk_vtophys(pqpair->cpl) failed\n");
197 : 0 : return -EFAULT;
198 : : }
199 : : }
200 : :
201 [ + - + - : 2420 : pqpair->sq_tdbl = pctrlr->doorbell_base + (2 * qpair->id + 0) * pctrlr->doorbell_stride_u32;
+ - + - +
- + - + -
+ - + - +
- + - ]
202 [ + - + - : 2420 : pqpair->cq_hdbl = pctrlr->doorbell_base + (2 * qpair->id + 1) * pctrlr->doorbell_stride_u32;
+ - + - +
- + - + -
+ - + - +
- + - ]
203 : :
204 : : /*
205 : : * Reserve space for all of the trackers in a single allocation.
206 : : * struct nvme_tracker must be padded so that its size is already a power of 2.
207 : : * This ensures the PRP list embedded in the nvme_tracker object will not span a
208 : : * 4KB boundary, while allowing access to trackers in tr[] via normal array indexing.
209 : : */
210 [ + - + - ]: 2420 : pqpair->tr = spdk_zmalloc(num_trackers * sizeof(*tr), sizeof(*tr), NULL,
211 : : SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_SHARE);
212 [ + + + - : 2420 : if (pqpair->tr == NULL) {
+ - ]
213 : 0 : SPDK_ERRLOG("nvme_tr failed\n");
214 : 0 : return -ENOMEM;
215 : : }
216 : :
217 [ + - + - : 2420 : TAILQ_INIT(&pqpair->free_tr);
+ - + - +
- + - + -
+ - ]
218 [ + - + - : 2420 : TAILQ_INIT(&pqpair->outstanding_tr);
+ - + - +
- + - + -
+ - ]
219 : :
220 [ + + ]: 1033829 : for (i = 0; i < num_trackers; i++) {
221 [ + - + - : 1031409 : tr = &pqpair->tr[i];
+ - ]
222 : 1031409 : nvme_qpair_construct_tracker(tr, i, nvme_pcie_vtophys(ctrlr, tr, NULL));
223 [ + + + - : 1031409 : TAILQ_INSERT_HEAD(&pqpair->free_tr, tr, tq_list);
+ - + - +
- + - + +
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
224 : 4219 : }
225 : :
226 : 2420 : nvme_pcie_qpair_reset(qpair);
227 : :
228 : 2420 : return 0;
229 : 22 : }
230 : :
231 : : int
232 : 698 : nvme_pcie_ctrlr_construct_admin_qpair(struct spdk_nvme_ctrlr *ctrlr, uint16_t num_entries)
233 : : {
234 : 1 : struct nvme_pcie_qpair *pqpair;
235 : 1 : int rc;
236 : :
237 : 698 : pqpair = spdk_zmalloc(sizeof(*pqpair), 64, NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_SHARE);
238 [ + + ]: 698 : if (pqpair == NULL) {
239 : 0 : return -ENOMEM;
240 : : }
241 : :
242 [ + - + - ]: 698 : pqpair->num_entries = num_entries;
243 [ + - + - ]: 698 : pqpair->flags.delay_cmd_submit = 0;
244 [ + - + - ]: 698 : pqpair->pcie_state = NVME_PCIE_QPAIR_READY;
245 : :
246 [ + - + - : 698 : ctrlr->adminq = &pqpair->qpair;
+ - ]
247 : :
248 [ + - + - ]: 698 : rc = nvme_qpair_init(ctrlr->adminq,
249 : : 0, /* qpair ID */
250 : 10 : ctrlr,
251 : : SPDK_NVME_QPRIO_URGENT,
252 : 10 : num_entries,
253 : : false);
254 [ - + ]: 698 : if (rc != 0) {
255 : 0 : return rc;
256 : : }
257 : :
258 [ + - + - ]: 698 : pqpair->stat = spdk_zmalloc(sizeof(*pqpair->stat), 64, NULL, SPDK_ENV_SOCKET_ID_ANY,
259 : : SPDK_MALLOC_SHARE);
260 [ + + + - : 698 : if (!pqpair->stat) {
+ - ]
261 : 0 : SPDK_ERRLOG("Failed to allocate admin qpair statistics\n");
262 : 0 : return -ENOMEM;
263 : : }
264 : :
265 [ - + - + ]: 698 : return nvme_pcie_qpair_construct(ctrlr->adminq, NULL);
266 : 10 : }
267 : :
268 : : /**
269 : : * Note: the ctrlr_lock must be held when calling this function.
270 : : */
271 : : void
272 : 238 : nvme_pcie_qpair_insert_pending_admin_request(struct spdk_nvme_qpair *qpair,
273 : : struct nvme_request *req, struct spdk_nvme_cpl *cpl)
274 : : {
275 [ # # # # ]: 238 : struct spdk_nvme_ctrlr *ctrlr = qpair->ctrlr;
276 : 238 : struct nvme_request *active_req = req;
277 : 0 : struct spdk_nvme_ctrlr_process *active_proc;
278 : :
279 : : /*
280 : : * The admin request is from another process. Move to the per
281 : : * process list for that process to handle it later.
282 : : */
283 [ - + # # ]: 238 : assert(nvme_qpair_is_admin_queue(qpair));
284 [ - + # # : 238 : assert(active_req->pid != getpid());
# # # # ]
285 : :
286 [ # # # # ]: 238 : active_proc = nvme_ctrlr_get_process(ctrlr, active_req->pid);
287 [ + + ]: 238 : if (active_proc) {
288 : : /* Save the original completion information */
289 [ # # # # : 154 : memcpy(&active_req->cpl, cpl, sizeof(*cpl));
# # ]
290 [ # # # # : 154 : STAILQ_INSERT_TAIL(&active_proc->active_reqs, active_req, stailq);
# # # # #
# # # # #
# # # # #
# # # #
# ]
291 : 0 : } else {
292 [ # # # # ]: 84 : SPDK_ERRLOG("The owning process (pid %d) is not found. Dropping the request.\n",
293 : : active_req->pid);
294 : 84 : nvme_cleanup_user_req(active_req);
295 : 84 : nvme_free_request(active_req);
296 : : }
297 : 238 : }
298 : :
299 : : /**
300 : : * Note: the ctrlr_lock must be held when calling this function.
301 : : */
302 : : void
303 : 50704963 : nvme_pcie_qpair_complete_pending_admin_request(struct spdk_nvme_qpair *qpair)
304 : : {
305 [ + - + - ]: 50704963 : struct spdk_nvme_ctrlr *ctrlr = qpair->ctrlr;
306 : 0 : struct nvme_request *req, *tmp_req;
307 : 50704963 : pid_t pid = getpid();
308 : 0 : struct spdk_nvme_ctrlr_process *proc;
309 : :
310 : : /*
311 : : * Check whether there is any pending admin request from
312 : : * other active processes.
313 : : */
314 [ + + # # ]: 50704963 : assert(nvme_qpair_is_admin_queue(qpair));
315 : :
316 : 50704963 : proc = nvme_ctrlr_get_current_process(ctrlr);
317 [ + + ]: 50704963 : if (!proc) {
318 : 0 : SPDK_ERRLOG("the active process (pid %d) is not found for this controller.\n", pid);
319 [ # # # # ]: 0 : assert(proc);
320 : 0 : return;
321 : : }
322 : :
323 [ + + + - : 50705117 : STAILQ_FOREACH_SAFE(req, &proc->active_reqs, stailq, tmp_req) {
+ - + - #
# # # # #
- + ]
324 [ + - + + : 154 : STAILQ_REMOVE(&proc->active_reqs, req, nvme_request, stailq);
- - - - #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
325 : :
326 [ - + # # : 154 : assert(req->pid == pid);
# # # # ]
327 : :
328 [ # # # # : 154 : nvme_complete_request(req->cb_fn, req->cb_arg, qpair, req, &req->cpl);
# # # # #
# ]
329 : 0 : }
330 [ # # ]: 524335 : }
331 : :
332 : : int
333 : 1775 : nvme_pcie_ctrlr_cmd_create_io_cq(struct spdk_nvme_ctrlr *ctrlr,
334 : : struct spdk_nvme_qpair *io_que, spdk_nvme_cmd_cb cb_fn,
335 : : void *cb_arg)
336 : : {
337 : 1775 : struct nvme_pcie_qpair *pqpair = nvme_pcie_qpair(io_que);
338 : 7 : struct nvme_request *req;
339 : 7 : struct spdk_nvme_cmd *cmd;
340 : :
341 [ + - + - ]: 1775 : req = nvme_allocate_request_null(ctrlr->adminq, cb_fn, cb_arg);
342 [ + + ]: 1775 : if (req == NULL) {
343 : 12 : return -ENOMEM;
344 : : }
345 : :
346 [ + - ]: 1763 : cmd = &req->cmd;
347 [ + - ]: 1763 : cmd->opc = SPDK_NVME_OPC_CREATE_IO_CQ;
348 : :
349 [ + - + - : 1763 : cmd->cdw10_bits.create_io_q.qid = io_que->id;
+ - + - +
- + - ]
350 [ + - + - : 1763 : cmd->cdw10_bits.create_io_q.qsize = pqpair->num_entries - 1;
+ - + - +
- + - +
- ]
351 : :
352 [ + - + - : 1763 : cmd->cdw11_bits.create_io_cq.pc = 1;
+ - + - ]
353 [ + - + - : 1763 : cmd->dptr.prp.prp1 = pqpair->cpl_bus_addr;
+ - + - +
- + - ]
354 : :
355 : 1763 : return nvme_ctrlr_submit_admin_request(ctrlr, req);
356 : 16 : }
357 : :
358 : : int
359 : 1763 : nvme_pcie_ctrlr_cmd_create_io_sq(struct spdk_nvme_ctrlr *ctrlr,
360 : : struct spdk_nvme_qpair *io_que, spdk_nvme_cmd_cb cb_fn, void *cb_arg)
361 : : {
362 : 1763 : struct nvme_pcie_qpair *pqpair = nvme_pcie_qpair(io_que);
363 : 5 : struct nvme_request *req;
364 : 5 : struct spdk_nvme_cmd *cmd;
365 : :
366 [ + - + - ]: 1763 : req = nvme_allocate_request_null(ctrlr->adminq, cb_fn, cb_arg);
367 [ + + ]: 1763 : if (req == NULL) {
368 : 6 : return -ENOMEM;
369 : : }
370 : :
371 [ + - ]: 1757 : cmd = &req->cmd;
372 [ + - ]: 1757 : cmd->opc = SPDK_NVME_OPC_CREATE_IO_SQ;
373 : :
374 [ + - + - : 1757 : cmd->cdw10_bits.create_io_q.qid = io_que->id;
+ - + - +
- + - ]
375 [ + - + - : 1757 : cmd->cdw10_bits.create_io_q.qsize = pqpair->num_entries - 1;
+ - + - +
- + - +
- ]
376 [ + - + - : 1757 : cmd->cdw11_bits.create_io_sq.pc = 1;
+ - + - ]
377 [ + - + - : 1757 : cmd->cdw11_bits.create_io_sq.qprio = io_que->qprio;
+ - + - +
- ]
378 [ + - + - : 1757 : cmd->cdw11_bits.create_io_sq.cqid = io_que->id;
+ - + - +
- + - ]
379 [ + - + - : 1757 : cmd->dptr.prp.prp1 = pqpair->cmd_bus_addr;
+ - + - +
- + - ]
380 : :
381 : 1757 : return nvme_ctrlr_submit_admin_request(ctrlr, req);
382 : 14 : }
383 : :
384 : : int
385 : 1671 : nvme_pcie_ctrlr_cmd_delete_io_cq(struct spdk_nvme_ctrlr *ctrlr, struct spdk_nvme_qpair *qpair,
386 : : spdk_nvme_cmd_cb cb_fn, void *cb_arg)
387 : : {
388 : 3 : struct nvme_request *req;
389 : 3 : struct spdk_nvme_cmd *cmd;
390 : :
391 [ + - + - ]: 1671 : req = nvme_allocate_request_null(ctrlr->adminq, cb_fn, cb_arg);
392 [ + + ]: 1671 : if (req == NULL) {
393 : 6 : return -ENOMEM;
394 : : }
395 : :
396 [ + - ]: 1665 : cmd = &req->cmd;
397 [ + - ]: 1665 : cmd->opc = SPDK_NVME_OPC_DELETE_IO_CQ;
398 [ + - + - : 1665 : cmd->cdw10_bits.delete_io_q.qid = qpair->id;
+ - + - +
- + - ]
399 : :
400 : 1665 : return nvme_ctrlr_submit_admin_request(ctrlr, req);
401 : 12 : }
402 : :
403 : : int
404 : 1668 : nvme_pcie_ctrlr_cmd_delete_io_sq(struct spdk_nvme_ctrlr *ctrlr, struct spdk_nvme_qpair *qpair,
405 : : spdk_nvme_cmd_cb cb_fn, void *cb_arg)
406 : : {
407 : 2 : struct nvme_request *req;
408 : 2 : struct spdk_nvme_cmd *cmd;
409 : :
410 [ + - + - ]: 1668 : req = nvme_allocate_request_null(ctrlr->adminq, cb_fn, cb_arg);
411 [ + + ]: 1668 : if (req == NULL) {
412 : 6 : return -ENOMEM;
413 : : }
414 : :
415 [ + - ]: 1662 : cmd = &req->cmd;
416 [ + - ]: 1662 : cmd->opc = SPDK_NVME_OPC_DELETE_IO_SQ;
417 [ + - + - : 1662 : cmd->cdw10_bits.delete_io_q.qid = qpair->id;
+ - + - +
- + - ]
418 : :
419 : 1662 : return nvme_ctrlr_submit_admin_request(ctrlr, req);
420 : 11 : }
421 : :
422 : : static void
423 : 6 : nvme_completion_sq_error_delete_cq_cb(void *arg, const struct spdk_nvme_cpl *cpl)
424 : : {
425 : 6 : struct spdk_nvme_qpair *qpair = arg;
426 : 6 : struct nvme_pcie_qpair *pqpair = nvme_pcie_qpair(qpair);
427 : :
428 [ + - + + : 6 : if (spdk_nvme_cpl_is_error(cpl)) {
+ - + - +
- + - + -
- + ]
429 : 0 : SPDK_ERRLOG("delete_io_cq failed!\n");
430 : 0 : }
431 : :
432 [ + - + - ]: 6 : pqpair->pcie_state = NVME_PCIE_QPAIR_FAILED;
433 : 6 : }
434 : :
435 : : static void
436 : 1751 : nvme_completion_create_sq_cb(void *arg, const struct spdk_nvme_cpl *cpl)
437 : : {
438 : 1751 : struct spdk_nvme_qpair *qpair = arg;
439 : 1751 : struct nvme_pcie_qpair *pqpair = nvme_pcie_qpair(qpair);
440 [ + - + - ]: 1751 : struct spdk_nvme_ctrlr *ctrlr = qpair->ctrlr;
441 : 1751 : struct nvme_pcie_ctrlr *pctrlr = nvme_pcie_ctrlr(ctrlr);
442 : 3 : int rc;
443 : :
444 [ + + + - : 1751 : if (pqpair->flags.defer_destruction) {
- + ]
445 : : /* This qpair was deleted by the application while the
446 : : * connection was still in progress. We had to wait
447 : : * to free the qpair resources until this outstanding
448 : : * command was completed. Now that we have the completion
449 : : * free it now.
450 : : */
451 : 0 : nvme_pcie_qpair_destroy(qpair);
452 : 0 : return;
453 : : }
454 : :
455 [ + + + + : 1751 : if (spdk_nvme_cpl_is_error(cpl)) {
+ - + + +
- + - + -
- + ]
456 : 6 : SPDK_ERRLOG("nvme_create_io_sq failed, deleting cq!\n");
457 [ + - + - ]: 6 : rc = nvme_pcie_ctrlr_cmd_delete_io_cq(qpair->ctrlr, qpair, nvme_completion_sq_error_delete_cq_cb,
458 : 1 : qpair);
459 [ + + ]: 6 : if (rc != 0) {
460 : 0 : SPDK_ERRLOG("Failed to send request to delete_io_cq with rc=%d\n", rc);
461 [ # # # # ]: 0 : pqpair->pcie_state = NVME_PCIE_QPAIR_FAILED;
462 : 0 : }
463 : 6 : return;
464 : : }
465 [ + - + - ]: 1745 : pqpair->pcie_state = NVME_PCIE_QPAIR_READY;
466 [ + + + - : 1745 : if (ctrlr->shadow_doorbell) {
+ + ]
467 [ + - + - : 2120 : pqpair->shadow_doorbell.sq_tdbl = ctrlr->shadow_doorbell + (2 * qpair->id + 0) *
+ - + - +
- + - + -
+ - + - +
- ]
468 [ + - + - ]: 1232 : pctrlr->doorbell_stride_u32;
469 [ + - + - : 2120 : pqpair->shadow_doorbell.cq_hdbl = ctrlr->shadow_doorbell + (2 * qpair->id + 1) *
+ - + - +
- + - + -
+ - + - +
- ]
470 [ + - + - ]: 1232 : pctrlr->doorbell_stride_u32;
471 [ + - + - : 2120 : pqpair->shadow_doorbell.sq_eventidx = ctrlr->eventidx + (2 * qpair->id + 0) *
+ - + - +
- + - + -
+ - + - +
- ]
472 [ + - + - ]: 1232 : pctrlr->doorbell_stride_u32;
473 [ + - + - : 2120 : pqpair->shadow_doorbell.cq_eventidx = ctrlr->eventidx + (2 * qpair->id + 1) *
+ - + - +
- + - + -
+ - + - +
- ]
474 [ + - + - ]: 1232 : pctrlr->doorbell_stride_u32;
475 [ + - + - ]: 1232 : pqpair->flags.has_shadow_doorbell = 1;
476 : 1 : } else {
477 [ + - + - ]: 513 : pqpair->flags.has_shadow_doorbell = 0;
478 : : }
479 : 1745 : nvme_pcie_qpair_reset(qpair);
480 : :
481 [ - + ]: 12 : }
482 : :
483 : : static void
484 : 1757 : nvme_completion_create_cq_cb(void *arg, const struct spdk_nvme_cpl *cpl)
485 : : {
486 : 1757 : struct spdk_nvme_qpair *qpair = arg;
487 : 1757 : struct nvme_pcie_qpair *pqpair = nvme_pcie_qpair(qpair);
488 : 4 : int rc;
489 : :
490 [ + + + - : 1757 : if (pqpair->flags.defer_destruction) {
- + ]
491 : : /* This qpair was deleted by the application while the
492 : : * connection was still in progress. We had to wait
493 : : * to free the qpair resources until this outstanding
494 : : * command was completed. Now that we have the completion
495 : : * free it now.
496 : : */
497 : 0 : nvme_pcie_qpair_destroy(qpair);
498 : 0 : return;
499 : : }
500 : :
501 [ + + + + : 1757 : if (spdk_nvme_cpl_is_error(cpl)) {
+ - + + +
- + - + -
- + ]
502 [ + - + - ]: 6 : pqpair->pcie_state = NVME_PCIE_QPAIR_FAILED;
503 : 6 : SPDK_ERRLOG("nvme_create_io_cq failed!\n");
504 : 6 : return;
505 : : }
506 : :
507 [ + - + - ]: 1751 : rc = nvme_pcie_ctrlr_cmd_create_io_sq(qpair->ctrlr, qpair, nvme_completion_create_sq_cb, qpair);
508 : :
509 [ + + ]: 1751 : if (rc != 0) {
510 : 0 : SPDK_ERRLOG("Failed to send request to create_io_sq, deleting cq!\n");
511 [ # # # # ]: 0 : rc = nvme_pcie_ctrlr_cmd_delete_io_cq(qpair->ctrlr, qpair, nvme_completion_sq_error_delete_cq_cb,
512 : 0 : qpair);
513 [ # # ]: 0 : if (rc != 0) {
514 : 0 : SPDK_ERRLOG("Failed to send request to delete_io_cq with rc=%d\n", rc);
515 [ # # # # ]: 0 : pqpair->pcie_state = NVME_PCIE_QPAIR_FAILED;
516 : 0 : }
517 : 0 : return;
518 : : }
519 [ + - + - ]: 1751 : pqpair->pcie_state = NVME_PCIE_QPAIR_WAIT_FOR_SQ;
520 [ - + ]: 13 : }
521 : :
522 : : static int
523 : 1763 : _nvme_pcie_ctrlr_create_io_qpair(struct spdk_nvme_ctrlr *ctrlr, struct spdk_nvme_qpair *qpair,
524 : : uint16_t qid)
525 : : {
526 : 1763 : struct nvme_pcie_qpair *pqpair = nvme_pcie_qpair(qpair);
527 : 5 : int rc;
528 : :
529 : : /* Statistics may already be allocated in the case of controller reset */
530 [ + + + - : 1763 : if (qpair->poll_group) {
+ + ]
531 [ + - + - ]: 1326 : struct nvme_pcie_poll_group *group = SPDK_CONTAINEROF(qpair->poll_group,
532 : : struct nvme_pcie_poll_group, group);
533 : :
534 [ + - + - : 1326 : pqpair->stat = &group->stats;
+ - ]
535 [ + - + - ]: 1326 : pqpair->shared_stats = true;
536 : 6 : } else {
537 [ + + + - : 437 : if (pqpair->stat == NULL) {
- + ]
538 [ + - + - ]: 408 : pqpair->stat = calloc(1, sizeof(*pqpair->stat));
539 [ + + + - : 408 : if (!pqpair->stat) {
+ - ]
540 : 0 : SPDK_ERRLOG("Failed to allocate qpair statistics\n");
541 : 0 : nvme_qpair_set_state(qpair, NVME_QPAIR_DISCONNECTED);
542 : 0 : return -ENOMEM;
543 : : }
544 : 8 : }
545 : : }
546 : :
547 : 1763 : rc = nvme_pcie_ctrlr_cmd_create_io_cq(ctrlr, qpair, nvme_completion_create_cq_cb, qpair);
548 : :
549 [ + + ]: 1763 : if (rc != 0) {
550 : 6 : SPDK_ERRLOG("Failed to send request to create_io_cq\n");
551 : 6 : nvme_qpair_set_state(qpair, NVME_QPAIR_DISCONNECTED);
552 : 6 : return rc;
553 : : }
554 [ + - + - ]: 1757 : pqpair->pcie_state = NVME_PCIE_QPAIR_WAIT_FOR_CQ;
555 : 1757 : return 0;
556 : 14 : }
557 : :
558 : : int
559 : 2541 : nvme_pcie_ctrlr_connect_qpair(struct spdk_nvme_ctrlr *ctrlr, struct spdk_nvme_qpair *qpair)
560 : : {
561 : 2541 : int rc = 0;
562 : :
563 [ + + ]: 2541 : if (!nvme_qpair_is_admin_queue(qpair)) {
564 [ + - + - ]: 1763 : rc = _nvme_pcie_ctrlr_create_io_qpair(ctrlr, qpair, qpair->id);
565 : 14 : } else {
566 : 778 : nvme_qpair_set_state(qpair, NVME_QPAIR_CONNECTED);
567 : : }
568 : :
569 : 2546 : return rc;
570 : 5 : }
571 : :
572 : : void
573 : 2514 : nvme_pcie_ctrlr_disconnect_qpair(struct spdk_nvme_ctrlr *ctrlr, struct spdk_nvme_qpair *qpair)
574 : : {
575 [ + + + + : 2514 : if (!nvme_qpair_is_admin_queue(qpair) || !ctrlr->is_disconnecting) {
+ + + - +
- ]
576 : 2428 : nvme_transport_ctrlr_disconnect_qpair_done(qpair);
577 : 18 : } else {
578 : : /* If this function is called for the admin qpair via spdk_nvme_ctrlr_reset()
579 : : * or spdk_nvme_ctrlr_disconnect(), initiate a Controller Level Reset.
580 : : * Then we can abort trackers safely because the Controller Level Reset deletes
581 : : * all I/O SQ/CQs.
582 : : */
583 : 86 : nvme_ctrlr_disable(ctrlr);
584 : : }
585 : 2514 : }
586 : :
587 : : /* Used when dst points to MMIO (i.e. CMB) in a virtual machine - in these cases we must
588 : : * not use wide instructions because QEMU will not emulate such instructions to MMIO space.
589 : : * So this function ensures we only copy 8 bytes at a time.
590 : : */
591 : : static inline void
592 : 0 : nvme_pcie_copy_command_mmio(struct spdk_nvme_cmd *dst, const struct spdk_nvme_cmd *src)
593 : : {
594 : 0 : uint64_t *dst64 = (uint64_t *)dst;
595 : 0 : const uint64_t *src64 = (const uint64_t *)src;
596 : 0 : uint32_t i;
597 : :
598 [ # # ]: 0 : for (i = 0; i < sizeof(*dst) / 8; i++) {
599 [ # # # # : 0 : dst64[i] = src64[i];
# # # # ]
600 : 0 : }
601 : 0 : }
602 : :
603 : : static inline void
604 : 43269708 : nvme_pcie_copy_command(struct spdk_nvme_cmd *dst, const struct spdk_nvme_cmd *src)
605 : : {
606 : : /* dst and src are known to be non-overlapping and 64-byte aligned. */
607 : : #if defined(__SSE2__)
608 : 43269708 : __m128i *d128 = (__m128i *)dst;
609 : 43269708 : const __m128i *s128 = (const __m128i *)src;
610 : :
611 [ + - + - ]: 43269708 : _mm_stream_si128(&d128[0], _mm_load_si128(&s128[0]));
612 [ + - + - ]: 86383612 : _mm_stream_si128(&d128[1], _mm_load_si128(&s128[1]));
613 [ + - + - ]: 86383612 : _mm_stream_si128(&d128[2], _mm_load_si128(&s128[2]));
614 [ + - + - ]: 86383612 : _mm_stream_si128(&d128[3], _mm_load_si128(&s128[3]));
615 : : #else
616 : : *dst = *src;
617 : : #endif
618 : 43269708 : }
619 : :
620 : : void
621 : 43269708 : nvme_pcie_qpair_submit_tracker(struct spdk_nvme_qpair *qpair, struct nvme_tracker *tr)
622 : : {
623 : 0 : struct nvme_request *req;
624 : 43269708 : struct nvme_pcie_qpair *pqpair = nvme_pcie_qpair(qpair);
625 [ + - + - ]: 43269708 : struct spdk_nvme_ctrlr *ctrlr = qpair->ctrlr;
626 : :
627 [ + - + - ]: 43269708 : req = tr->req;
628 [ + + # # ]: 43269708 : assert(req != NULL);
629 : :
630 [ + + + + : 43269708 : spdk_trace_record(TRACE_NVME_PCIE_SUBMIT, qpair->id, 0, (uintptr_t)req, req->cb_arg,
+ - + - +
- + - + -
+ - + - #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ]
631 : : (uint32_t)req->cmd.cid, (uint32_t)req->cmd.opc,
632 : : req->cmd.cdw10, req->cmd.cdw11, req->cmd.cdw12);
633 : :
634 [ + + + - : 43269708 : if (req->cmd.fuse) {
+ - ]
635 : : /*
636 : : * Keep track of the fuse operation sequence so that we ring the doorbell only
637 : : * after the second fuse is submitted.
638 : : */
639 [ # # # # : 2 : qpair->last_fuse = req->cmd.fuse;
# # ]
640 : 0 : }
641 : :
642 : : /* Don't use wide instructions to copy NVMe command, this is limited by QEMU
643 : : * virtual NVMe controller, the maximum access width is 8 Bytes for one time.
644 : : */
645 [ + + + + : 43269708 : if (spdk_unlikely((ctrlr->quirks & NVME_QUIRK_MAXIMUM_PCI_ACCESS_WIDTH) && pqpair->sq_in_cmb)) {
+ + # # #
# # # -
+ ]
646 [ # # # # : 0 : nvme_pcie_copy_command_mmio(&pqpair->cmd[pqpair->sq_tail], &req->cmd);
# # # # #
# # # ]
647 : 0 : } else {
648 : : /* Copy the command from the tracker to the submission queue. */
649 [ + - + - : 43269708 : nvme_pcie_copy_command(&pqpair->cmd[pqpair->sq_tail], &req->cmd);
+ - + - +
- + - ]
650 : : }
651 : :
652 [ + + + - : 43269708 : if (spdk_unlikely(++pqpair->sq_tail == pqpair->num_entries)) {
+ - + + ]
653 [ + - + - ]: 100193 : pqpair->sq_tail = 0;
654 : 1213 : }
655 : :
656 [ + + + - : 43269708 : if (spdk_unlikely(pqpair->sq_tail == pqpair->sq_head)) {
+ - + - +
- ]
657 : 0 : SPDK_ERRLOG("sq_tail is passing sq_head!\n");
658 : 0 : }
659 : :
660 [ + + + - : 43269708 : if (!pqpair->flags.delay_cmd_submit) {
+ + ]
661 : 5345990 : nvme_pcie_qpair_ring_sq_doorbell(qpair);
662 : 155801 : }
663 : 43269708 : }
664 : :
665 : : void
666 : 43269692 : nvme_pcie_qpair_complete_tracker(struct spdk_nvme_qpair *qpair, struct nvme_tracker *tr,
667 : : struct spdk_nvme_cpl *cpl, bool print_on_error)
668 : : {
669 : 43269692 : struct nvme_pcie_qpair *pqpair = nvme_pcie_qpair(qpair);
670 : 0 : struct nvme_request *req;
671 : 0 : bool retry, error;
672 : 0 : bool print_error;
673 : :
674 [ + - + - ]: 43269692 : req = tr->req;
675 : :
676 [ + + + + : 43269692 : spdk_trace_record(TRACE_NVME_PCIE_COMPLETE, qpair->id, 0, (uintptr_t)req, req->cb_arg,
+ - + - +
- + - + -
+ - + - #
# # # # #
# # # # #
# # # # #
# # # # ]
677 : : (uint32_t)req->cmd.cid, (uint32_t)cpl->status_raw);
678 : :
679 [ + + # # ]: 43269692 : assert(req != NULL);
680 : :
681 [ + + + + : 43269692 : error = spdk_nvme_cpl_is_error(cpl);
+ - + + +
- + - +
- ]
682 [ + + + + : 43269692 : retry = error && nvme_completion_is_retry(cpl) &&
+ - ]
683 [ # # # # : 0 : req->retries < pqpair->retry_count;
# # # # ]
684 [ + + + + : 43269692 : print_error = error && print_on_error && !qpair->ctrlr->opts.disable_error_logging;
+ + + - #
# # # # #
# # # # #
# ]
685 : :
686 [ + + + - ]: 43269692 : if (print_error) {
687 [ # # ]: 823846 : spdk_nvme_qpair_print_command(qpair, &req->cmd);
688 : 0 : }
689 : :
690 [ + + + + : 43269692 : if (print_error || SPDK_DEBUGLOG_FLAG_ENABLED("nvme")) {
- + ]
691 : 823886 : spdk_nvme_qpair_print_completion(qpair, cpl);
692 : 0 : }
693 : :
694 [ + + + - : 43269692 : assert(cpl->cid == req->cmd.cid);
+ - + - +
- + - #
# ]
695 : :
696 [ + + - + ]: 43269692 : if (retry) {
697 [ # # ]: 0 : req->retries++;
698 : 0 : nvme_pcie_qpair_submit_tracker(qpair, tr);
699 : 0 : } else {
700 [ + + + - : 43269692 : TAILQ_REMOVE(&pqpair->outstanding_tr, tr, tq_list);
+ - + + +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - ]
701 : :
702 : : /* Only check admin requests from different processes. */
703 [ + + + + : 43269692 : if (nvme_qpair_is_admin_queue(qpair) && req->pid != getpid()) {
+ - + - ]
704 : 238 : nvme_pcie_qpair_insert_pending_admin_request(qpair, req, cpl);
705 : 0 : } else {
706 [ + - + - : 43269454 : nvme_complete_request(tr->cb_fn, tr->cb_arg, qpair, req, cpl);
+ - + - ]
707 : : }
708 : :
709 [ + - + - ]: 43269692 : tr->req = NULL;
710 : :
711 [ + + + - : 43269692 : TAILQ_INSERT_HEAD(&pqpair->free_tr, tr, tq_list);
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- # # # #
# # # # #
# + - + -
+ - + - +
- + - + -
+ - ]
712 : : }
713 : 43269692 : }
714 : :
715 : : void
716 : 3326 : nvme_pcie_qpair_manual_complete_tracker(struct spdk_nvme_qpair *qpair,
717 : : struct nvme_tracker *tr, uint32_t sct, uint32_t sc, uint32_t dnr,
718 : : bool print_on_error)
719 : : {
720 : 1418 : struct spdk_nvme_cpl cpl;
721 : :
722 [ + + ]: 3326 : memset(&cpl, 0, sizeof(cpl));
723 [ + - + - : 3326 : cpl.sqid = qpair->id;
+ - ]
724 [ + - + - : 3326 : cpl.cid = tr->cid;
+ - ]
725 [ + - + - ]: 3326 : cpl.status.sct = sct;
726 [ + - + - ]: 3326 : cpl.status.sc = sc;
727 [ + - + - ]: 3326 : cpl.status.dnr = dnr;
728 [ + - ]: 3326 : nvme_pcie_qpair_complete_tracker(qpair, tr, &cpl, print_on_error);
729 : 3326 : }
730 : :
731 : : void
732 : 2431 : nvme_pcie_qpair_abort_trackers(struct spdk_nvme_qpair *qpair, uint32_t dnr)
733 : : {
734 : 2431 : struct nvme_pcie_qpair *pqpair = nvme_pcie_qpair(qpair);
735 : 0 : struct nvme_tracker *tr, *temp, *last;
736 : :
737 [ + - + - : 2431 : last = TAILQ_LAST(&pqpair->outstanding_tr, nvme_outstanding_tr_head);
+ - + - +
- + - ]
738 : :
739 : : /* Abort previously submitted (outstanding) trs */
740 [ + + + - : 2764 : TAILQ_FOREACH_SAFE(tr, &pqpair->outstanding_tr, tq_list, temp) {
+ - + - #
# # # # #
+ - ]
741 [ + + + - : 384 : if (!qpair->ctrlr->opts.disable_error_logging) {
# # # # #
# # # #
# ]
742 : 384 : SPDK_ERRLOG("aborting outstanding command\n");
743 : 0 : }
744 : 384 : nvme_pcie_qpair_manual_complete_tracker(qpair, tr, SPDK_NVME_SCT_GENERIC,
745 : 0 : SPDK_NVME_SC_ABORTED_BY_REQUEST, dnr, true);
746 : :
747 [ + + ]: 384 : if (tr == last) {
748 : 51 : break;
749 : : }
750 : 0 : }
751 : 2431 : }
752 : :
753 : : void
754 : 1468 : nvme_pcie_admin_qpair_abort_aers(struct spdk_nvme_qpair *qpair)
755 : : {
756 : 1468 : struct nvme_pcie_qpair *pqpair = nvme_pcie_qpair(qpair);
757 : 1 : struct nvme_tracker *tr;
758 : :
759 [ + - + - : 1468 : tr = TAILQ_FIRST(&pqpair->outstanding_tr);
+ - ]
760 [ + + ]: 4410 : while (tr != NULL) {
761 [ + + + - : 2942 : assert(tr->req != NULL);
+ - # # ]
762 [ + - + - : 2942 : if (tr->req->cmd.opc == SPDK_NVME_OPC_ASYNC_EVENT_REQUEST) {
+ - + - +
- ]
763 : 2942 : nvme_pcie_qpair_manual_complete_tracker(qpair, tr,
764 : : SPDK_NVME_SCT_GENERIC, SPDK_NVME_SC_ABORTED_SQ_DELETION, 0,
765 : : false);
766 [ + - + - : 2942 : tr = TAILQ_FIRST(&pqpair->outstanding_tr);
+ - ]
767 : 36 : } else {
768 [ # # # # : 0 : tr = TAILQ_NEXT(tr, tq_list);
# # ]
769 : : }
770 : : }
771 : 1468 : }
772 : :
773 : : void
774 : 694 : nvme_pcie_admin_qpair_destroy(struct spdk_nvme_qpair *qpair)
775 : : {
776 : 694 : nvme_pcie_admin_qpair_abort_aers(qpair);
777 : 694 : }
778 : :
779 : : void
780 : 727 : nvme_pcie_qpair_abort_reqs(struct spdk_nvme_qpair *qpair, uint32_t dnr)
781 : : {
782 : 727 : nvme_pcie_qpair_abort_trackers(qpair, dnr);
783 : 727 : }
784 : :
785 : : static void
786 : 473458 : nvme_pcie_qpair_check_timeout(struct spdk_nvme_qpair *qpair)
787 : : {
788 : 0 : uint64_t t02;
789 : 0 : struct nvme_tracker *tr, *tmp;
790 : 473458 : struct nvme_pcie_qpair *pqpair = nvme_pcie_qpair(qpair);
791 [ # # # # ]: 473458 : struct spdk_nvme_ctrlr *ctrlr = qpair->ctrlr;
792 : 0 : struct spdk_nvme_ctrlr_process *active_proc;
793 : :
794 : : /* Don't check timeouts during controller initialization. */
795 [ - + # # : 473458 : if (ctrlr->state != NVME_CTRLR_STATE_READY) {
# # ]
796 : 0 : return;
797 : : }
798 : :
799 [ + + ]: 473458 : if (nvme_qpair_is_admin_queue(qpair)) {
800 : 386383 : active_proc = nvme_ctrlr_get_current_process(ctrlr);
801 : 0 : } else {
802 [ # # # # ]: 87075 : active_proc = qpair->active_proc;
803 : : }
804 : :
805 : : /* Only check timeouts if the current process has a timeout callback. */
806 [ + - - + : 473458 : if (active_proc == NULL || active_proc->timeout_cb_fn == NULL) {
# # # # ]
807 : 0 : return;
808 : : }
809 : :
810 : 473458 : t02 = spdk_get_ticks();
811 [ + + # # : 475274 : TAILQ_FOREACH_SAFE(tr, &pqpair->outstanding_tr, tq_list, tmp) {
# # # # #
# # # # #
# # ]
812 [ - + # # : 89312 : assert(tr->req != NULL);
# # # # ]
813 : :
814 [ + + # # : 89312 : if (nvme_request_check_timeout(tr->req, tr->cid, active_proc, t02)) {
# # # # #
# ]
815 : : /*
816 : : * The requests are in order, so as soon as one has not timed out,
817 : : * stop iterating.
818 : : */
819 : 87496 : break;
820 : : }
821 : 0 : }
822 [ # # ]: 0 : }
823 : :
824 : : int32_t
825 : 893320768 : nvme_pcie_qpair_process_completions(struct spdk_nvme_qpair *qpair, uint32_t max_completions)
826 : : {
827 : 893320768 : struct nvme_pcie_qpair *pqpair = nvme_pcie_qpair(qpair);
828 : 0 : struct nvme_tracker *tr;
829 : 0 : struct spdk_nvme_cpl *cpl, *next_cpl;
830 : 893320768 : uint32_t num_completions = 0;
831 [ + - + - ]: 893320768 : struct spdk_nvme_ctrlr *ctrlr = qpair->ctrlr;
832 : 0 : uint16_t next_cq_head;
833 : 0 : uint8_t next_phase;
834 : 893320768 : bool next_is_valid = false;
835 : 0 : int rc;
836 : :
837 [ + + + - : 893320768 : if (spdk_unlikely(pqpair->pcie_state == NVME_PCIE_QPAIR_FAILED)) {
- + ]
838 : 0 : return -ENXIO;
839 : : }
840 : :
841 [ + + ]: 893320768 : if (spdk_unlikely(nvme_qpair_get_state(qpair) == NVME_QPAIR_CONNECTING)) {
842 [ + + + - : 343578 : if (pqpair->pcie_state == NVME_PCIE_QPAIR_READY) {
+ + ]
843 : : /* It is possible that another thread set the pcie_state to
844 : : * QPAIR_READY, if it polled the adminq and processed the SQ
845 : : * completion for this qpair. So check for that condition
846 : : * here and then update the qpair's state to CONNECTED, since
847 : : * we can only set the qpair state from the qpair's thread.
848 : : * (Note: this fixed issue #2157.)
849 : : */
850 : 1724 : nvme_qpair_set_state(qpair, NVME_QPAIR_CONNECTED);
851 [ + + + - : 341863 : } else if (pqpair->pcie_state == NVME_PCIE_QPAIR_FAILED) {
- + ]
852 : 0 : nvme_qpair_set_state(qpair, NVME_QPAIR_DISCONNECTED);
853 : 0 : return -ENXIO;
854 : : } else {
855 [ + - + - ]: 341854 : rc = spdk_nvme_qpair_process_completions(ctrlr->adminq, 0);
856 [ + + ]: 341854 : if (rc < 0) {
857 : 0 : return rc;
858 [ + + + - : 341854 : } else if (pqpair->pcie_state == NVME_PCIE_QPAIR_FAILED) {
- + ]
859 : 0 : nvme_qpair_set_state(qpair, NVME_QPAIR_DISCONNECTED);
860 : 0 : return -ENXIO;
861 : : }
862 : : }
863 : 343578 : return 0;
864 : : }
865 : :
866 [ + + ]: 892977190 : if (spdk_unlikely(nvme_qpair_is_admin_queue(qpair))) {
867 : 50704963 : nvme_ctrlr_lock(ctrlr);
868 : 524335 : }
869 : :
870 [ + + + + : 892977190 : if (max_completions == 0 || max_completions > pqpair->max_completions_cap) {
# # # # ]
871 : : /*
872 : : * max_completions == 0 means unlimited, but complete at most
873 : : * max_completions_cap batch of I/O at a time so that the completion
874 : : * queue doorbells don't wrap around.
875 : : */
876 [ + - + - ]: 889773196 : max_completions = pqpair->max_completions_cap;
877 : 835743 : }
878 : :
879 [ + - + - : 892977190 : pqpair->stat->polls++;
+ - ]
880 : :
881 : 991511 : while (1) {
882 [ + - + - : 934844860 : cpl = &pqpair->cpl[pqpair->cq_head];
+ - + - +
- ]
883 : :
884 [ + + + + : 934844860 : if (!next_is_valid && cpl->status.p != pqpair->flags.phase) {
+ - + - +
- + - + -
+ + ]
885 : 891578494 : break;
886 : : }
887 : :
888 [ + + + - : 43266366 : if (spdk_likely(pqpair->cq_head + 1 != pqpair->num_entries)) {
+ - + - +
- + + ]
889 [ + - + - : 43166174 : next_cq_head = pqpair->cq_head + 1;
+ - ]
890 [ + - + - ]: 43166174 : next_phase = pqpair->flags.phase;
891 : 154555 : } else {
892 : 100192 : next_cq_head = 0;
893 [ + - + - ]: 100192 : next_phase = !pqpair->flags.phase;
894 : : }
895 [ + - + - : 43266366 : next_cpl = &pqpair->cpl[next_cq_head];
+ - ]
896 [ + - + - : 43266366 : next_is_valid = (next_cpl->status.p == next_phase);
+ - ]
897 [ + + + + ]: 43266366 : if (next_is_valid) {
898 [ + - + - : 28687257 : __builtin_prefetch(&pqpair->tr[next_cpl->cid]);
+ - + - +
- ]
899 : 1 : }
900 : :
901 : : #if defined(__PPC64__) || defined(__riscv) || defined(__loongarch__)
902 : : /*
903 : : * This memory barrier prevents reordering of:
904 : : * - load after store from/to tr
905 : : * - load after load cpl phase and cpl cid
906 : : */
907 : : spdk_mb();
908 : : #elif defined(__aarch64__)
909 : : __asm volatile("dmb oshld" ::: "memory");
910 : : #endif
911 : :
912 [ + + + - : 43266366 : if (spdk_unlikely(++pqpair->cq_head == pqpair->num_entries)) {
+ - + + ]
913 [ + - + - ]: 100192 : pqpair->cq_head = 0;
914 [ + - + - : 100192 : pqpair->flags.phase = !pqpair->flags.phase;
+ - + - ]
915 : 1213 : }
916 : :
917 [ + - + - : 43266366 : tr = &pqpair->tr[cpl->cid];
+ - + - +
- ]
918 [ + - + - : 43266366 : pqpair->sq_head = cpl->sqhd;
+ - + - ]
919 : :
920 [ + - + - : 43266366 : if (tr->req) {
+ - ]
921 : : /* Prefetch the req's STAILQ_ENTRY since we'll need to access it
922 : : * as part of putting the req back on the qpair's free list.
923 : : */
924 [ + - + - : 43266366 : __builtin_prefetch(&tr->req->stailq);
+ - ]
925 : 43266366 : nvme_pcie_qpair_complete_tracker(qpair, tr, cpl, true);
926 : 155768 : } else {
927 : 0 : SPDK_ERRLOG("cpl does not map to outstanding cmd\n");
928 : 0 : spdk_nvme_qpair_print_completion(qpair, cpl);
929 [ # # ]: 0 : assert(0);
930 : : }
931 : :
932 [ + + ]: 43266366 : if (++num_completions == max_completions) {
933 : 1398696 : break;
934 : : }
935 : : }
936 : :
937 [ + + ]: 892977190 : if (num_completions > 0) {
938 [ + - + - : 14082246 : pqpair->stat->completions += num_completions;
+ - + - ]
939 : 14082246 : nvme_pcie_qpair_ring_cq_doorbell(qpair);
940 : 155766 : } else {
941 [ + - + - : 878894944 : pqpair->stat->idle_polls++;
+ - ]
942 : : }
943 : :
944 [ + + + - : 892977190 : if (pqpair->flags.delay_cmd_submit) {
+ + ]
945 [ + + + - : 819185173 : if (pqpair->last_sq_tail != pqpair->sq_tail) {
+ - + - +
+ ]
946 : 11978871 : nvme_pcie_qpair_ring_sq_doorbell(qpair);
947 [ + - + - : 11978871 : pqpair->last_sq_tail = pqpair->sq_tail;
+ - + - ]
948 : 1 : }
949 : 64 : }
950 : :
951 [ + + + + : 892977190 : if (spdk_unlikely(ctrlr->timeout_enabled)) {
+ - + - ]
952 : : /*
953 : : * User registered for timeout callback
954 : : */
955 : 473458 : nvme_pcie_qpair_check_timeout(qpair);
956 : 0 : }
957 : :
958 : : /* Before returning, complete any pending admin request or
959 : : * process the admin qpair disconnection.
960 : : */
961 [ + + ]: 892977190 : if (spdk_unlikely(nvme_qpair_is_admin_queue(qpair))) {
962 : 50704963 : nvme_pcie_qpair_complete_pending_admin_request(qpair);
963 : :
964 [ + + ]: 50704963 : if (nvme_qpair_get_state(qpair) == NVME_QPAIR_DISCONNECTING) {
965 [ # # # # ]: 1228 : rc = nvme_ctrlr_disable_poll(qpair->ctrlr);
966 [ + + ]: 1228 : if (rc != -EAGAIN) {
967 : 86 : nvme_transport_ctrlr_disconnect_qpair_done(qpair);
968 : 0 : }
969 : 0 : }
970 : :
971 : 50704963 : nvme_ctrlr_unlock(ctrlr);
972 : 524335 : }
973 : :
974 [ + + + - : 892977190 : if (spdk_unlikely(pqpair->flags.has_pending_vtophys_failures)) {
- + ]
975 : 0 : struct nvme_tracker *tr, *tmp;
976 : :
977 [ # # # # : 0 : TAILQ_FOREACH_SAFE(tr, &pqpair->outstanding_tr, tq_list, tmp) {
# # # # #
# # # # #
# # ]
978 [ # # # # ]: 0 : if (tr->bad_vtophys) {
979 [ # # ]: 0 : tr->bad_vtophys = 0;
980 : 0 : nvme_pcie_fail_request_bad_vtophys(qpair, tr);
981 : 0 : }
982 : 0 : }
983 [ # # # # ]: 0 : pqpair->flags.has_pending_vtophys_failures = 0;
984 : 0 : }
985 : :
986 : 892977190 : return num_completions;
987 : 853694 : }
988 : :
989 : : int
990 : 2416 : nvme_pcie_qpair_destroy(struct spdk_nvme_qpair *qpair)
991 : : {
992 : 2416 : struct nvme_pcie_qpair *pqpair = nvme_pcie_qpair(qpair);
993 : :
994 [ + + ]: 2416 : if (nvme_qpair_is_admin_queue(qpair)) {
995 : 694 : nvme_pcie_admin_qpair_destroy(qpair);
996 : 10 : }
997 : : /*
998 : : * We check sq_vaddr and cq_vaddr to see if the user specified the memory
999 : : * buffers when creating the I/O queue.
1000 : : * If the user specified them, we cannot free that memory.
1001 : : * Nor do we free it if it's in the CMB.
1002 : : */
1003 [ + + + - : 2416 : if (!pqpair->sq_vaddr && pqpair->cmd && !pqpair->sq_in_cmb) {
+ + + - +
- + - + -
+ - + - -
+ ]
1004 [ + - + - ]: 2404 : spdk_free(pqpair->cmd);
1005 : 20 : }
1006 [ + + + - : 2416 : if (!pqpair->cq_vaddr && pqpair->cpl) {
+ + + - +
- + - ]
1007 [ + - + - ]: 2404 : spdk_free(pqpair->cpl);
1008 : 20 : }
1009 [ + - + - : 2416 : if (pqpair->tr) {
- + ]
1010 [ + - + - ]: 2416 : spdk_free(pqpair->tr);
1011 : 22 : }
1012 : :
1013 : 2416 : nvme_qpair_deinit(qpair);
1014 : :
1015 [ + + + + : 2824 : if (!pqpair->shared_stats && (!qpair->active_proc ||
+ + + + +
- + - + +
- + ]
1016 [ + - + - : 408 : qpair->active_proc == nvme_ctrlr_get_current_process(qpair->ctrlr))) {
+ - + - ]
1017 [ + + + - : 1120 : if (qpair->id) {
+ + ]
1018 [ + - + - ]: 426 : free(pqpair->stat);
1019 : 11 : } else {
1020 : : /* statistics of admin qpair are allocates from huge pages because
1021 : : * admin qpair is shared for multi-process */
1022 [ + - + - ]: 694 : spdk_free(pqpair->stat);
1023 : : }
1024 : :
1025 : 21 : }
1026 : :
1027 : 2416 : spdk_free(pqpair);
1028 : :
1029 : 2416 : return 0;
1030 : 4 : }
1031 : :
1032 : : struct spdk_nvme_qpair *
1033 : 1704 : nvme_pcie_ctrlr_create_io_qpair(struct spdk_nvme_ctrlr *ctrlr, uint16_t qid,
1034 : : const struct spdk_nvme_io_qpair_opts *opts)
1035 : : {
1036 : 0 : struct nvme_pcie_qpair *pqpair;
1037 : 0 : struct spdk_nvme_qpair *qpair;
1038 : 0 : int rc;
1039 : :
1040 [ + + # # ]: 1704 : assert(ctrlr != NULL);
1041 : :
1042 : 1704 : pqpair = spdk_zmalloc(sizeof(*pqpair), 64, NULL,
1043 : : SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_SHARE);
1044 [ + + ]: 1704 : if (pqpair == NULL) {
1045 : 0 : return NULL;
1046 : : }
1047 : :
1048 [ + - + - : 1704 : pqpair->num_entries = opts->io_queue_size;
+ - + - ]
1049 [ + + + - : 1704 : pqpair->flags.delay_cmd_submit = opts->delay_cmd_submit;
+ - + - +
- + - ]
1050 : :
1051 [ + - ]: 1704 : qpair = &pqpair->qpair;
1052 : :
1053 [ + + + - : 1704 : rc = nvme_qpair_init(qpair, qid, ctrlr, opts->qprio, opts->io_queue_requests, opts->async_mode);
+ - + - +
- + - +
- ]
1054 [ - + ]: 1704 : if (rc != 0) {
1055 : 0 : nvme_pcie_qpair_destroy(qpair);
1056 : 0 : return NULL;
1057 : : }
1058 : :
1059 : 1704 : rc = nvme_pcie_qpair_construct(qpair, opts);
1060 : :
1061 [ - + ]: 1704 : if (rc != 0) {
1062 : 0 : nvme_pcie_qpair_destroy(qpair);
1063 : 0 : return NULL;
1064 : : }
1065 : :
1066 : 1704 : return qpair;
1067 : 9 : }
1068 : :
1069 : : int
1070 : 1704 : nvme_pcie_ctrlr_delete_io_qpair(struct spdk_nvme_ctrlr *ctrlr, struct spdk_nvme_qpair *qpair)
1071 : : {
1072 : 1704 : struct nvme_pcie_qpair *pqpair = nvme_pcie_qpair(qpair);
1073 : 0 : struct nvme_completion_poll_status *status;
1074 : 0 : int rc;
1075 : :
1076 [ + + # # ]: 1704 : assert(ctrlr != NULL);
1077 : :
1078 [ + + + + : 1704 : if (ctrlr->is_removed) {
+ - - + ]
1079 : 12 : goto free;
1080 : : }
1081 : :
1082 [ + + + + : 1692 : if (ctrlr->prepare_for_reset) {
+ - - + ]
1083 [ - + ]: 36 : if (nvme_qpair_get_state(qpair) == NVME_QPAIR_CONNECTING) {
1084 [ # # # # ]: 0 : pqpair->flags.defer_destruction = true;
1085 : 0 : }
1086 : 36 : goto clear_shadow_doorbells;
1087 : : }
1088 : :
1089 : : /* If attempting to delete a qpair that's still being connected, we have to wait until it's
1090 : : * finished, so that we don't free it while it's waiting for the create cq/sq callbacks.
1091 : : */
1092 [ + + + - : 1909 : while (pqpair->pcie_state == NVME_PCIE_QPAIR_WAIT_FOR_CQ ||
- + + - ]
1093 [ + + + - ]: 1900 : pqpair->pcie_state == NVME_PCIE_QPAIR_WAIT_FOR_SQ) {
1094 [ # # # # ]: 244 : rc = spdk_nvme_qpair_process_completions(ctrlr->adminq, 0);
1095 [ - + ]: 244 : if (rc < 0) {
1096 : 0 : break;
1097 : : }
1098 : : }
1099 : :
1100 : 1656 : status = calloc(1, sizeof(*status));
1101 [ + + ]: 1656 : if (!status) {
1102 : 0 : SPDK_ERRLOG("Failed to allocate status tracker\n");
1103 : 0 : goto free;
1104 : : }
1105 : :
1106 : : /* Delete the I/O submission queue */
1107 : 1656 : rc = nvme_pcie_ctrlr_cmd_delete_io_sq(ctrlr, qpair, nvme_completion_poll_cb, status);
1108 [ - + ]: 1656 : if (rc != 0) {
1109 : 0 : SPDK_ERRLOG("Failed to send request to delete_io_sq with rc=%d\n", rc);
1110 : 0 : free(status);
1111 : 0 : goto free;
1112 : : }
1113 [ + + + - : 1656 : if (nvme_wait_for_completion(ctrlr->adminq, status)) {
- + ]
1114 [ - + + - : 3 : if (!status->timed_out) {
# # # # ]
1115 : 3 : free(status);
1116 : 0 : }
1117 : 3 : goto free;
1118 : : }
1119 : :
1120 : : /* Now that the submission queue is deleted, the device is supposed to have
1121 : : * completed any outstanding I/O. Try to complete them. If they don't complete,
1122 : : * they'll be marked as aborted and completed below. */
1123 [ + - + - : 1653 : if (qpair->active_proc == nvme_ctrlr_get_current_process(ctrlr)) {
- + ]
1124 : 1653 : nvme_pcie_qpair_process_completions(qpair, 0);
1125 : 9 : }
1126 : :
1127 [ + + ]: 1653 : memset(status, 0, sizeof(*status));
1128 : : /* Delete the completion queue */
1129 : 1653 : rc = nvme_pcie_ctrlr_cmd_delete_io_cq(ctrlr, qpair, nvme_completion_poll_cb, status);
1130 [ - + ]: 1653 : if (rc != 0) {
1131 : 0 : SPDK_ERRLOG("Failed to send request to delete_io_cq with rc=%d\n", rc);
1132 : 0 : free(status);
1133 : 0 : goto free;
1134 : : }
1135 [ + + + - : 1653 : if (nvme_wait_for_completion(ctrlr->adminq, status)) {
- + ]
1136 [ # # # # : 0 : if (!status->timed_out) {
# # # # ]
1137 : 0 : free(status);
1138 : 0 : }
1139 : 0 : goto free;
1140 : : }
1141 : 1653 : free(status);
1142 : :
1143 : 1680 : clear_shadow_doorbells:
1144 [ + + + + : 1689 : if (pqpair->flags.has_shadow_doorbell && ctrlr->shadow_doorbell) {
- + # # #
# # # ]
1145 [ # # # # : 1172 : *pqpair->shadow_doorbell.sq_tdbl = 0;
# # ]
1146 [ # # # # : 1172 : *pqpair->shadow_doorbell.cq_hdbl = 0;
# # ]
1147 [ # # # # : 1172 : *pqpair->shadow_doorbell.sq_eventidx = 0;
# # ]
1148 [ # # # # : 1172 : *pqpair->shadow_doorbell.cq_eventidx = 0;
# # ]
1149 : 0 : }
1150 : 508 : free:
1151 [ + - - + ]: 1704 : if (qpair->no_deletion_notification_needed == 0) {
1152 : : /* Abort the rest of the I/O */
1153 : 1704 : nvme_pcie_qpair_abort_trackers(qpair, 1);
1154 : 9 : }
1155 : :
1156 [ + - + - : 1704 : if (!pqpair->flags.defer_destruction) {
- + ]
1157 : 1704 : nvme_pcie_qpair_destroy(qpair);
1158 : 9 : }
1159 : 1704 : return 0;
1160 : 0 : }
1161 : :
1162 : : static void
1163 : 18 : nvme_pcie_fail_request_bad_vtophys(struct spdk_nvme_qpair *qpair, struct nvme_tracker *tr)
1164 : : {
1165 [ + - + - ]: 18 : if (!qpair->in_completion_context) {
1166 : 18 : struct nvme_pcie_qpair *pqpair = nvme_pcie_qpair(qpair);
1167 : :
1168 [ + - ]: 18 : tr->bad_vtophys = 1;
1169 [ + - + - ]: 18 : pqpair->flags.has_pending_vtophys_failures = 1;
1170 : 15 : return;
1171 : 3 : }
1172 : :
1173 : : /*
1174 : : * Bad vtophys translation, so abort this request and return
1175 : : * immediately.
1176 : : */
1177 : 0 : SPDK_ERRLOG("vtophys or other payload buffer related error\n");
1178 : 0 : nvme_pcie_qpair_manual_complete_tracker(qpair, tr, SPDK_NVME_SCT_GENERIC,
1179 : : SPDK_NVME_SC_INVALID_FIELD,
1180 : : 1 /* do not retry */, true);
1181 : 3 : }
1182 : :
1183 : : /*
1184 : : * Append PRP list entries to describe a virtually contiguous buffer starting at virt_addr of len bytes.
1185 : : *
1186 : : * *prp_index will be updated to account for the number of PRP entries used.
1187 : : */
1188 : : static inline int
1189 : 28879756 : nvme_pcie_prp_list_append(struct spdk_nvme_ctrlr *ctrlr, struct nvme_tracker *tr,
1190 : : uint32_t *prp_index, void *virt_addr, size_t len,
1191 : : uint32_t page_size)
1192 : : {
1193 [ + - + - : 28879756 : struct spdk_nvme_cmd *cmd = &tr->req->cmd;
+ - ]
1194 : 28879756 : uintptr_t page_mask = page_size - 1;
1195 : 25 : uint64_t phys_addr;
1196 : 25 : uint32_t i;
1197 : :
1198 [ + + + + : 28879756 : SPDK_DEBUGLOG(nvme, "prp_index:%u virt_addr:%p len:%u\n",
+ - # # ]
1199 : : *prp_index, virt_addr, (uint32_t)len);
1200 : :
1201 [ + + ]: 28879756 : if (spdk_unlikely(((uintptr_t)virt_addr & 3) != 0)) {
1202 : 12 : SPDK_ERRLOG("virt_addr %p not dword aligned\n", virt_addr);
1203 : 12 : return -EFAULT;
1204 : : }
1205 : :
1206 [ + - ]: 28879744 : i = *prp_index;
1207 [ + + ]: 185163412 : while (len) {
1208 : 2051 : uint32_t seg_len;
1209 : :
1210 : : /*
1211 : : * prp_index 0 is stored in prp1, and the rest are stored in the prp[] array,
1212 : : * so prp_index == count is valid.
1213 : : */
1214 [ + + ]: 156283692 : if (spdk_unlikely(i > SPDK_COUNTOF(tr->u.prp))) {
1215 : 12 : SPDK_ERRLOG("out of PRP entries\n");
1216 : 12 : return -EFAULT;
1217 : : }
1218 : :
1219 : 156283680 : phys_addr = nvme_pcie_vtophys(ctrlr, virt_addr, NULL);
1220 [ + + ]: 156283680 : if (spdk_unlikely(phys_addr == SPDK_VTOPHYS_ERROR)) {
1221 : 6 : SPDK_ERRLOG("vtophys(%p) failed\n", virt_addr);
1222 : 6 : return -EFAULT;
1223 : : }
1224 : :
1225 [ + + ]: 156283674 : if (i == 0) {
1226 [ + + + + : 20459668 : SPDK_DEBUGLOG(nvme, "prp1 = %p\n", (void *)phys_addr);
+ - ]
1227 [ + - + - : 20459668 : cmd->dptr.prp.prp1 = phys_addr;
+ - + - ]
1228 : 20459668 : seg_len = page_size - ((uintptr_t)virt_addr & page_mask);
1229 : 58 : } else {
1230 [ + + ]: 135824006 : if ((phys_addr & page_mask) != 0) {
1231 : 6 : SPDK_ERRLOG("PRP %u not page aligned (%p)\n", i, virt_addr);
1232 : 6 : return -EFAULT;
1233 : : }
1234 : :
1235 [ + + + + : 135824000 : SPDK_DEBUGLOG(nvme, "prp[%u] = %p\n", i - 1, (void *)phys_addr);
+ - ]
1236 [ + - + - : 135824000 : tr->u.prp[i - 1] = phys_addr;
+ - + - +
- ]
1237 : 135824000 : seg_len = page_size;
1238 : : }
1239 : :
1240 [ + + ]: 156283668 : seg_len = spdk_min(seg_len, len);
1241 [ + - ]: 156283668 : virt_addr = (uint8_t *)virt_addr + seg_len;
1242 : 156283668 : len -= seg_len;
1243 : 156283668 : i++;
1244 [ + + ]: 2051 : }
1245 : :
1246 [ + - ]: 28879720 : cmd->psdt = SPDK_NVME_PSDT_PRP;
1247 [ + + ]: 28879720 : if (i <= 1) {
1248 [ + - + - : 7469399 : cmd->dptr.prp.prp2 = 0;
+ - + - ]
1249 [ + + ]: 21410365 : } else if (i == 2) {
1250 [ + - + - : 9522934 : cmd->dptr.prp.prp2 = tr->u.prp[0];
+ - + - +
- + - + -
+ - + - ]
1251 [ + + + + : 9522934 : SPDK_DEBUGLOG(nvme, "prp2 = %p\n", (void *)cmd->dptr.prp.prp2);
+ - # # #
# # # #
# ]
1252 : 6 : } else {
1253 [ + - + - : 11887387 : cmd->dptr.prp.prp2 = tr->prp_sgl_bus_addr;
+ - + - +
- + - ]
1254 [ + + + + : 11887387 : SPDK_DEBUGLOG(nvme, "prp2 = %p (PRP list)\n", (void *)cmd->dptr.prp.prp2);
+ - # # #
# # # #
# ]
1255 : : }
1256 : :
1257 [ + - ]: 28879720 : *prp_index = i;
1258 : 28879720 : return 0;
1259 : 64 : }
1260 : :
1261 : : static int
1262 : 0 : nvme_pcie_qpair_build_request_invalid(struct spdk_nvme_qpair *qpair,
1263 : : struct nvme_request *req, struct nvme_tracker *tr, bool dword_aligned)
1264 : : {
1265 [ # # ]: 0 : assert(0);
1266 : : nvme_pcie_fail_request_bad_vtophys(qpair, tr);
1267 : : return -EINVAL;
1268 : : }
1269 : :
1270 : : /**
1271 : : * Build PRP list describing physically contiguous payload buffer.
1272 : : */
1273 : : static int
1274 : 16994550 : nvme_pcie_qpair_build_contig_request(struct spdk_nvme_qpair *qpair, struct nvme_request *req,
1275 : : struct nvme_tracker *tr, bool dword_aligned)
1276 : : {
1277 : 16994550 : uint32_t prp_index = 0;
1278 : 4 : int rc;
1279 : :
1280 [ + - + - ]: 33987841 : rc = nvme_pcie_prp_list_append(qpair->ctrlr, tr, &prp_index,
1281 [ + - + - : 16994550 : (uint8_t *)req->payload.contig_or_cb_arg + req->payload_offset,
+ - + - +
- + - ]
1282 [ + - + - : 33987798 : req->payload_size, qpair->ctrlr->page_size);
+ - + - +
- + - ]
1283 [ + + ]: 16994550 : if (rc) {
1284 : 6 : nvme_pcie_fail_request_bad_vtophys(qpair, tr);
1285 : 1 : }
1286 : :
1287 : 16994554 : return rc;
1288 : 4 : }
1289 : :
1290 : : /**
1291 : : * Build an SGL describing a physically contiguous payload buffer.
1292 : : *
1293 : : * This is more efficient than using PRP because large buffers can be
1294 : : * described this way.
1295 : : */
1296 : : static int
1297 : 20449255 : nvme_pcie_qpair_build_contig_hw_sgl_request(struct spdk_nvme_qpair *qpair, struct nvme_request *req,
1298 : : struct nvme_tracker *tr, bool dword_aligned)
1299 : : {
1300 : 3 : uint8_t *virt_addr;
1301 : 7897418 : uint64_t phys_addr, mapping_length;
1302 : 3 : uint32_t length;
1303 : 3 : struct spdk_nvme_sgl_descriptor *sgl;
1304 : 20449255 : uint32_t nseg = 0;
1305 : :
1306 [ + + + - : 20449255 : assert(req->payload_size != 0);
+ - # # ]
1307 [ + + + - : 20449255 : assert(nvme_payload_type(&req->payload) == NVME_PAYLOAD_TYPE_CONTIG);
# # ]
1308 : :
1309 [ + - + - ]: 20449255 : sgl = tr->u.sgl;
1310 [ + - + - ]: 20449255 : req->cmd.psdt = SPDK_NVME_PSDT_SGL_MPTR_CONTIG;
1311 [ + - + - : 20449255 : req->cmd.dptr.sgl1.unkeyed.subtype = 0;
+ - + - +
- + - ]
1312 : :
1313 [ + - + - ]: 20449255 : length = req->payload_size;
1314 : : /* ubsan complains about applying zero offset to null pointer if contig_or_cb_arg is NULL,
1315 : : * so just double cast it to make it go away */
1316 [ + - + - : 20449255 : virt_addr = (uint8_t *)((uintptr_t)req->payload.contig_or_cb_arg + req->payload_offset);
+ - + - +
- ]
1317 : :
1318 [ + + ]: 40922059 : while (length > 0) {
1319 [ - + ]: 20472804 : if (nseg >= NVME_MAX_SGL_DESCRIPTORS) {
1320 : 0 : nvme_pcie_fail_request_bad_vtophys(qpair, tr);
1321 : 0 : return -EFAULT;
1322 : : }
1323 : :
1324 [ + + + + : 20472804 : if (dword_aligned && ((uintptr_t)virt_addr & 3)) {
- + ]
1325 : 0 : SPDK_ERRLOG("virt_addr %p not dword aligned\n", virt_addr);
1326 : 0 : nvme_pcie_fail_request_bad_vtophys(qpair, tr);
1327 : 0 : return -EFAULT;
1328 : : }
1329 : :
1330 : 20472804 : mapping_length = length;
1331 [ + - + - ]: 20472804 : phys_addr = nvme_pcie_vtophys(qpair->ctrlr, virt_addr, &mapping_length);
1332 [ + + ]: 20472804 : if (phys_addr == SPDK_VTOPHYS_ERROR) {
1333 : 0 : nvme_pcie_fail_request_bad_vtophys(qpair, tr);
1334 : 0 : return -EFAULT;
1335 : : }
1336 : :
1337 [ + + ]: 20472804 : mapping_length = spdk_min(length, mapping_length);
1338 : :
1339 : 20472804 : length -= mapping_length;
1340 [ + - ]: 20472804 : virt_addr += mapping_length;
1341 : :
1342 [ + - + - : 20472804 : sgl->unkeyed.type = SPDK_NVME_SGL_TYPE_DATA_BLOCK;
+ - ]
1343 [ + - + - : 20472804 : sgl->unkeyed.length = mapping_length;
+ - + - ]
1344 [ + - + - ]: 20472804 : sgl->address = phys_addr;
1345 [ + - + - : 20472804 : sgl->unkeyed.subtype = 0;
+ - ]
1346 : :
1347 [ + - ]: 20472804 : sgl++;
1348 : 20472804 : nseg++;
1349 : : }
1350 : :
1351 [ + + ]: 20449255 : if (nseg == 1) {
1352 : : /*
1353 : : * The whole transfer can be described by a single SGL descriptor.
1354 : : * Use the special case described by the spec where SGL1's type is Data Block.
1355 : : * This means the SGL in the tracker is not used at all, so copy the first (and only)
1356 : : * SGL element into SGL1.
1357 : : */
1358 [ + - + - : 20425706 : req->cmd.dptr.sgl1.unkeyed.type = SPDK_NVME_SGL_TYPE_DATA_BLOCK;
+ - + - +
- + - ]
1359 [ + - + - : 20425706 : req->cmd.dptr.sgl1.address = tr->u.sgl[0].address;
+ - + - +
- + - + -
+ - + - +
- ]
1360 [ + - + - : 20425706 : req->cmd.dptr.sgl1.unkeyed.length = tr->u.sgl[0].unkeyed.length;
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - ]
1361 : 155670 : } else {
1362 : : /* SPDK NVMe driver supports only 1 SGL segment for now, it is enough because
1363 : : * NVME_MAX_SGL_DESCRIPTORS * 16 is less than one page.
1364 : : */
1365 [ + - + - : 23549 : req->cmd.dptr.sgl1.unkeyed.type = SPDK_NVME_SGL_TYPE_LAST_SEGMENT;
+ - + - +
- + - ]
1366 [ + - + - : 23549 : req->cmd.dptr.sgl1.address = tr->prp_sgl_bus_addr;
+ - + - +
- + - +
- ]
1367 [ + - + - : 23549 : req->cmd.dptr.sgl1.unkeyed.length = nseg * sizeof(struct spdk_nvme_sgl_descriptor);
+ - + - +
- + - +
- ]
1368 : : }
1369 : :
1370 : 20449255 : return 0;
1371 : 155671 : }
1372 : :
1373 : : /**
1374 : : * Build SGL list describing scattered payload buffer.
1375 : : */
1376 : : static int
1377 : 65839 : nvme_pcie_qpair_build_hw_sgl_request(struct spdk_nvme_qpair *qpair, struct nvme_request *req,
1378 : : struct nvme_tracker *tr, bool dword_aligned)
1379 : : {
1380 : 2 : int rc;
1381 : 64517 : void *virt_addr;
1382 : 64517 : uint64_t phys_addr, mapping_length;
1383 : 64517 : uint32_t remaining_transfer_len, remaining_user_sge_len, length;
1384 : 2 : struct spdk_nvme_sgl_descriptor *sgl;
1385 : 65839 : uint32_t nseg = 0;
1386 : :
1387 : : /*
1388 : : * Build scattered payloads.
1389 : : */
1390 [ + + + - : 65839 : assert(req->payload_size != 0);
+ - # # ]
1391 [ + + + - : 65839 : assert(nvme_payload_type(&req->payload) == NVME_PAYLOAD_TYPE_SGL);
# # ]
1392 [ + + + - : 65839 : assert(req->payload.reset_sgl_fn != NULL);
+ - + - #
# ]
1393 [ + + + - : 65839 : assert(req->payload.next_sge_fn != NULL);
+ - + - #
# ]
1394 [ + - + - : 65839 : req->payload.reset_sgl_fn(req->payload.contig_or_cb_arg, req->payload_offset);
+ - - + +
- + - + -
+ - + - +
- ]
1395 : :
1396 [ + - + - ]: 65839 : sgl = tr->u.sgl;
1397 [ + - + - ]: 65839 : req->cmd.psdt = SPDK_NVME_PSDT_SGL_MPTR_CONTIG;
1398 [ + - + - : 65839 : req->cmd.dptr.sgl1.unkeyed.subtype = 0;
+ - + - +
- + - ]
1399 : :
1400 [ + - + - ]: 65839 : remaining_transfer_len = req->payload_size;
1401 : :
1402 [ + + ]: 133793 : while (remaining_transfer_len > 0) {
1403 [ + - + - : 67954 : rc = req->payload.next_sge_fn(req->payload.contig_or_cb_arg,
+ - - + +
- + - + -
+ - ]
1404 : : &virt_addr, &remaining_user_sge_len);
1405 [ - + ]: 67954 : if (rc) {
1406 : 0 : nvme_pcie_fail_request_bad_vtophys(qpair, tr);
1407 : 0 : return -EFAULT;
1408 : : }
1409 : :
1410 : : /* Bit Bucket SGL descriptor */
1411 [ + + ]: 67954 : if ((uint64_t)virt_addr == UINT64_MAX) {
1412 : : /* TODO: enable WRITE and COMPARE when necessary */
1413 [ # # # # : 0 : if (req->cmd.opc != SPDK_NVME_OPC_READ) {
# # ]
1414 : 0 : SPDK_ERRLOG("Only READ command can be supported\n");
1415 : 0 : goto exit;
1416 : : }
1417 [ # # ]: 0 : if (nseg >= NVME_MAX_SGL_DESCRIPTORS) {
1418 : 0 : SPDK_ERRLOG("Too many SGL entries\n");
1419 : 0 : goto exit;
1420 : : }
1421 : :
1422 [ # # # # : 0 : sgl->unkeyed.type = SPDK_NVME_SGL_TYPE_BIT_BUCKET;
# # ]
1423 : : /* If the SGL describes a destination data buffer, the length of data
1424 : : * buffer shall be discarded by controller, and the length is included
1425 : : * in Number of Logical Blocks (NLB) parameter. Otherwise, the length
1426 : : * is not included in the NLB parameter.
1427 : : */
1428 [ # # ]: 0 : remaining_user_sge_len = spdk_min(remaining_user_sge_len, remaining_transfer_len);
1429 : 0 : remaining_transfer_len -= remaining_user_sge_len;
1430 : :
1431 [ # # # # : 0 : sgl->unkeyed.length = remaining_user_sge_len;
# # # # ]
1432 [ # # # # ]: 0 : sgl->address = 0;
1433 [ # # # # : 0 : sgl->unkeyed.subtype = 0;
# # ]
1434 : :
1435 [ # # ]: 0 : sgl++;
1436 : 0 : nseg++;
1437 : :
1438 : 0 : continue;
1439 : : }
1440 : :
1441 [ + + ]: 67954 : remaining_user_sge_len = spdk_min(remaining_user_sge_len, remaining_transfer_len);
1442 : 67954 : remaining_transfer_len -= remaining_user_sge_len;
1443 [ + + ]: 135908 : while (remaining_user_sge_len > 0) {
1444 [ - + ]: 67954 : if (nseg >= NVME_MAX_SGL_DESCRIPTORS) {
1445 : 0 : SPDK_ERRLOG("Too many SGL entries\n");
1446 : 0 : goto exit;
1447 : : }
1448 : :
1449 [ + + + + : 67954 : if (dword_aligned && ((uintptr_t)virt_addr & 3)) {
- + ]
1450 : 0 : SPDK_ERRLOG("virt_addr %p not dword aligned\n", virt_addr);
1451 : 0 : goto exit;
1452 : : }
1453 : :
1454 : 67954 : mapping_length = remaining_user_sge_len;
1455 [ + - + - ]: 67954 : phys_addr = nvme_pcie_vtophys(qpair->ctrlr, virt_addr, &mapping_length);
1456 [ + + ]: 67954 : if (phys_addr == SPDK_VTOPHYS_ERROR) {
1457 : 0 : goto exit;
1458 : : }
1459 : :
1460 [ + + ]: 67954 : length = spdk_min(remaining_user_sge_len, mapping_length);
1461 : 67954 : remaining_user_sge_len -= length;
1462 [ + - ]: 67954 : virt_addr = (uint8_t *)virt_addr + length;
1463 : :
1464 [ + + + - ]: 67956 : if (nseg > 0 && phys_addr ==
1465 [ + + + - : 2115 : (*(sgl - 1)).address + (*(sgl - 1)).unkeyed.length) {
+ - + - +
- + - + -
+ - ]
1466 : : /* extend previous entry */
1467 [ # # # # : 1627 : (*(sgl - 1)).unkeyed.length += length;
# # # # #
# ]
1468 : 1627 : continue;
1469 : : }
1470 : :
1471 [ + - + - : 66327 : sgl->unkeyed.type = SPDK_NVME_SGL_TYPE_DATA_BLOCK;
+ - ]
1472 [ + - + - : 66327 : sgl->unkeyed.length = length;
+ - + - ]
1473 [ + - + - ]: 66327 : sgl->address = phys_addr;
1474 [ + - + - : 66327 : sgl->unkeyed.subtype = 0;
+ - ]
1475 : :
1476 [ + - ]: 66327 : sgl++;
1477 : 66327 : nseg++;
1478 : : }
1479 : : }
1480 : :
1481 [ + + ]: 65839 : if (nseg == 1) {
1482 : : /*
1483 : : * The whole transfer can be described by a single SGL descriptor.
1484 : : * Use the special case described by the spec where SGL1's type is Data Block.
1485 : : * This means the SGL in the tracker is not used at all, so copy the first (and only)
1486 : : * SGL element into SGL1.
1487 : : */
1488 [ + - + - : 65791 : req->cmd.dptr.sgl1.unkeyed.type = SPDK_NVME_SGL_TYPE_DATA_BLOCK;
+ - + - +
- + - ]
1489 [ + - + - : 65791 : req->cmd.dptr.sgl1.address = tr->u.sgl[0].address;
+ - + - +
- + - + -
+ - + - +
- ]
1490 [ + - + - : 65791 : req->cmd.dptr.sgl1.unkeyed.length = tr->u.sgl[0].unkeyed.length;
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - ]
1491 : 1 : } else {
1492 : : /* SPDK NVMe driver supports only 1 SGL segment for now, it is enough because
1493 : : * NVME_MAX_SGL_DESCRIPTORS * 16 is less than one page.
1494 : : */
1495 [ + - + - : 48 : req->cmd.dptr.sgl1.unkeyed.type = SPDK_NVME_SGL_TYPE_LAST_SEGMENT;
+ - + - +
- + - ]
1496 [ + - + - : 48 : req->cmd.dptr.sgl1.address = tr->prp_sgl_bus_addr;
+ - + - +
- + - +
- ]
1497 [ + - + - : 48 : req->cmd.dptr.sgl1.unkeyed.length = nseg * sizeof(struct spdk_nvme_sgl_descriptor);
+ - + - +
- + - +
- ]
1498 : : }
1499 : :
1500 : 65839 : return 0;
1501 : :
1502 : 0 : exit:
1503 : 0 : nvme_pcie_fail_request_bad_vtophys(qpair, tr);
1504 : 0 : return -EFAULT;
1505 : 2 : }
1506 : :
1507 : : /**
1508 : : * Build PRP list describing scattered payload buffer.
1509 : : */
1510 : : static int
1511 : 3465034 : nvme_pcie_qpair_build_prps_sgl_request(struct spdk_nvme_qpair *qpair, struct nvme_request *req,
1512 : : struct nvme_tracker *tr, bool dword_aligned)
1513 : : {
1514 : 1 : int rc;
1515 : 3003136 : void *virt_addr;
1516 : 3003136 : uint32_t remaining_transfer_len, length;
1517 : 3465034 : uint32_t prp_index = 0;
1518 [ + - + - : 3465034 : uint32_t page_size = qpair->ctrlr->page_size;
+ - + - ]
1519 : :
1520 : : /*
1521 : : * Build scattered payloads.
1522 : : */
1523 [ + + + - : 3465034 : assert(nvme_payload_type(&req->payload) == NVME_PAYLOAD_TYPE_SGL);
# # ]
1524 [ + + + - : 3465034 : assert(req->payload.reset_sgl_fn != NULL);
+ - + - #
# ]
1525 [ + - + - : 3465034 : req->payload.reset_sgl_fn(req->payload.contig_or_cb_arg, req->payload_offset);
+ - - + +
- + - + -
+ - + - +
- ]
1526 : :
1527 [ + - + - ]: 3465034 : remaining_transfer_len = req->payload_size;
1528 [ + + ]: 15350120 : while (remaining_transfer_len > 0) {
1529 [ + + + - : 11885086 : assert(req->payload.next_sge_fn != NULL);
+ - + - #
# ]
1530 [ + - + - : 11885086 : rc = req->payload.next_sge_fn(req->payload.contig_or_cb_arg, &virt_addr, &length);
+ - - + +
- + - + -
+ - ]
1531 [ - + ]: 11885086 : if (rc) {
1532 : 0 : nvme_pcie_fail_request_bad_vtophys(qpair, tr);
1533 : 0 : return -EFAULT;
1534 : : }
1535 : :
1536 [ - + ]: 11885086 : length = spdk_min(remaining_transfer_len, length);
1537 : :
1538 : : /*
1539 : : * Any incompatible sges should have been handled up in the splitting routine,
1540 : : * but assert here as an additional check.
1541 : : *
1542 : : * All SGEs except last must end on a page boundary.
1543 : : */
1544 [ + + - + : 11885086 : assert((length == remaining_transfer_len) ||
# # ]
1545 : : _is_page_aligned((uintptr_t)virt_addr + length, page_size));
1546 : :
1547 [ + - + - ]: 11885086 : rc = nvme_pcie_prp_list_append(qpair->ctrlr, tr, &prp_index, virt_addr, length, page_size);
1548 [ - + ]: 11885086 : if (rc) {
1549 : 0 : nvme_pcie_fail_request_bad_vtophys(qpair, tr);
1550 : 0 : return rc;
1551 : : }
1552 : :
1553 : 11885086 : remaining_transfer_len -= length;
1554 : : }
1555 : :
1556 : 3465034 : return 0;
1557 : 1 : }
1558 : :
1559 : : typedef int(*build_req_fn)(struct spdk_nvme_qpair *, struct nvme_request *, struct nvme_tracker *,
1560 : : bool);
1561 : :
1562 : : static build_req_fn const g_nvme_pcie_build_req_table[][2] = {
1563 : : [NVME_PAYLOAD_TYPE_INVALID] = {
1564 : : nvme_pcie_qpair_build_request_invalid, /* PRP */
1565 : : nvme_pcie_qpair_build_request_invalid /* SGL */
1566 : : },
1567 : : [NVME_PAYLOAD_TYPE_CONTIG] = {
1568 : : nvme_pcie_qpair_build_contig_request, /* PRP */
1569 : : nvme_pcie_qpair_build_contig_hw_sgl_request /* SGL */
1570 : : },
1571 : : [NVME_PAYLOAD_TYPE_SGL] = {
1572 : : nvme_pcie_qpair_build_prps_sgl_request, /* PRP */
1573 : : nvme_pcie_qpair_build_hw_sgl_request /* SGL */
1574 : : }
1575 : : };
1576 : :
1577 : : static int
1578 : 40974648 : nvme_pcie_qpair_build_metadata(struct spdk_nvme_qpair *qpair, struct nvme_tracker *tr,
1579 : : bool sgl_supported, bool mptr_sgl_supported, bool dword_aligned)
1580 : : {
1581 : 5 : void *md_payload;
1582 [ + - + - ]: 40974648 : struct nvme_request *req = tr->req;
1583 : 19152572 : uint64_t mapping_length;
1584 : :
1585 [ + + + - : 40974648 : if (req->payload.md) {
+ - + + ]
1586 [ + - + - : 3817791 : md_payload = (uint8_t *)req->payload.md + req->md_offset;
+ - + - +
- + - ]
1587 [ + + + + : 3817791 : if (dword_aligned && ((uintptr_t)md_payload & 3)) {
+ - ]
1588 : 0 : SPDK_ERRLOG("virt_addr %p not dword aligned\n", md_payload);
1589 : 0 : goto exit;
1590 : : }
1591 : :
1592 [ + - + - ]: 3817791 : mapping_length = req->md_size;
1593 [ + + + + : 3817791 : if (sgl_supported && mptr_sgl_supported && dword_aligned) {
+ - + + +
- - + ]
1594 [ + + + - : 12 : assert(req->cmd.psdt == SPDK_NVME_PSDT_SGL_MPTR_CONTIG);
+ - # # ]
1595 [ + - + - ]: 12 : req->cmd.psdt = SPDK_NVME_PSDT_SGL_MPTR_SGL;
1596 : :
1597 [ + - + - : 12 : tr->meta_sgl.address = nvme_pcie_vtophys(qpair->ctrlr, md_payload, &mapping_length);
+ - + - +
- ]
1598 [ + - + + : 12 : if (tr->meta_sgl.address == SPDK_VTOPHYS_ERROR || mapping_length != req->md_size) {
+ - + - +
- + - +
+ ]
1599 : 6 : goto exit;
1600 : : }
1601 [ + - + - : 6 : tr->meta_sgl.unkeyed.type = SPDK_NVME_SGL_TYPE_DATA_BLOCK;
+ - + - ]
1602 [ + - + - : 6 : tr->meta_sgl.unkeyed.length = req->md_size;
+ - + - +
- + - +
- ]
1603 [ + - + - : 6 : tr->meta_sgl.unkeyed.subtype = 0;
+ - + - ]
1604 [ + - + - : 6 : req->cmd.mptr = tr->prp_sgl_bus_addr - sizeof(struct spdk_nvme_sgl_descriptor);
+ - + - +
- ]
1605 : 1 : } else {
1606 [ + - + - : 3817779 : req->cmd.mptr = nvme_pcie_vtophys(qpair->ctrlr, md_payload, &mapping_length);
+ - + - +
- ]
1607 [ + - + + : 3817779 : if (req->cmd.mptr == SPDK_VTOPHYS_ERROR || mapping_length != req->md_size) {
+ - + - +
- + - +
+ ]
1608 : 6 : goto exit;
1609 : : }
1610 : : }
1611 : 3 : }
1612 : :
1613 : 40974636 : return 0;
1614 : :
1615 : 10 : exit:
1616 : 12 : nvme_pcie_fail_request_bad_vtophys(qpair, tr);
1617 : 12 : return -EINVAL;
1618 : 155712 : }
1619 : :
1620 : : int
1621 : 43269939 : nvme_pcie_qpair_submit_request(struct spdk_nvme_qpair *qpair, struct nvme_request *req)
1622 : : {
1623 : 0 : struct nvme_tracker *tr;
1624 : 43269939 : int rc = 0;
1625 [ + - + - ]: 43269939 : struct spdk_nvme_ctrlr *ctrlr = qpair->ctrlr;
1626 : 43269939 : struct nvme_pcie_qpair *pqpair = nvme_pcie_qpair(qpair);
1627 : 0 : enum nvme_payload_type payload_type;
1628 : 0 : bool sgl_supported;
1629 : 0 : bool mptr_sgl_supported;
1630 : 43269939 : bool dword_aligned = true;
1631 : :
1632 [ + + ]: 43269939 : if (spdk_unlikely(nvme_qpair_is_admin_queue(qpair))) {
1633 : 909105 : nvme_ctrlr_lock(ctrlr);
1634 : 133 : }
1635 : :
1636 [ + - + - : 43269939 : tr = TAILQ_FIRST(&pqpair->free_tr);
+ - ]
1637 : :
1638 [ + + ]: 43269939 : if (tr == NULL) {
1639 [ # # # # : 231 : pqpair->stat->queued_requests++;
# # ]
1640 : : /* Inform the upper layer to try again later. */
1641 : 231 : rc = -EAGAIN;
1642 : 231 : goto exit;
1643 : : }
1644 : :
1645 [ + - + - : 43269708 : pqpair->stat->submitted_requests++;
+ - ]
1646 [ + + + - : 43269708 : TAILQ_REMOVE(&pqpair->free_tr, tr, tq_list); /* remove tr from free_tr */
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - # # #
# # # # #
# # # # +
- + - + -
+ - + - +
- + - ]
1647 [ + - + - : 43269708 : TAILQ_INSERT_TAIL(&pqpair->outstanding_tr, tr, tq_list);
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
1648 [ + - + - ]: 43269708 : tr->req = req;
1649 [ + - + - : 43269708 : tr->cb_fn = req->cb_fn;
+ - + - ]
1650 [ + - + - : 43269708 : tr->cb_arg = req->cb_arg;
+ - + - ]
1651 [ + - + - : 43269708 : req->cmd.cid = tr->cid;
+ - + - +
- ]
1652 : :
1653 [ + + + - : 43269708 : if (req->payload_size != 0) {
+ + ]
1654 [ + - ]: 40974618 : payload_type = nvme_payload_type(&req->payload);
1655 : : /* According to the specification, PRPs shall be used for all
1656 : : * Admin commands for NVMe over PCIe implementations.
1657 : : */
1658 [ + + + - : 61493463 : sgl_supported = (ctrlr->flags & SPDK_NVME_CTRLR_SGL_SUPPORTED) != 0 &&
+ + ]
1659 [ + + ]: 20518845 : !nvme_qpair_is_admin_queue(qpair);
1660 [ + + + - : 40974622 : mptr_sgl_supported = (ctrlr->flags & SPDK_NVME_CTRLR_MPTR_SGL_SUPPORTED) != 0 &&
+ - ]
1661 [ - + ]: 4 : !nvme_qpair_is_admin_queue(qpair);
1662 : :
1663 [ + + + + ]: 40974618 : if (sgl_supported) {
1664 : : /* Don't use SGL for DSM command */
1665 [ + + + - : 20515064 : if (spdk_unlikely((ctrlr->quirks & NVME_QUIRK_NO_SGL_FOR_DSM) &&
+ - # # #
# + - ]
1666 : : (req->cmd.opc == SPDK_NVME_OPC_DATASET_MANAGEMENT))) {
1667 : 0 : sgl_supported = false;
1668 : 0 : }
1669 : 155668 : }
1670 : :
1671 [ + + + + : 40974618 : if (sgl_supported && !(ctrlr->flags & SPDK_NVME_CTRLR_SGL_REQUIRES_DWORD_ALIGNMENT)) {
+ - + - +
- ]
1672 : 19101374 : dword_aligned = false;
1673 : 0 : }
1674 : :
1675 : : /* If we fail to build the request or the metadata, do not return the -EFAULT back up
1676 : : * the stack. This ensures that we always fail these types of requests via a
1677 : : * completion callback, and never in the context of the submission.
1678 : : */
1679 [ + - + - : 40974618 : rc = g_nvme_pcie_build_req_table[payload_type][sgl_supported](qpair, req, tr, dword_aligned);
+ - + - +
- + - - +
+ - + - ]
1680 [ + + ]: 40974618 : if (rc < 0) {
1681 [ # # # # ]: 0 : assert(rc == -EFAULT);
1682 : 0 : rc = 0;
1683 : 0 : goto exit;
1684 : : }
1685 : :
1686 [ + - + - : 40974618 : rc = nvme_pcie_qpair_build_metadata(qpair, tr, sgl_supported, mptr_sgl_supported, dword_aligned);
+ - ]
1687 [ + + ]: 40974618 : if (rc < 0) {
1688 [ # # # # ]: 0 : assert(rc == -EFAULT);
1689 : 0 : rc = 0;
1690 : 0 : goto exit;
1691 : : }
1692 : 155707 : }
1693 : :
1694 : 43269708 : nvme_pcie_qpair_submit_tracker(qpair, tr);
1695 : :
1696 : 43114135 : exit:
1697 [ + + ]: 43269939 : if (spdk_unlikely(nvme_qpair_is_admin_queue(qpair))) {
1698 : 909105 : nvme_ctrlr_unlock(ctrlr);
1699 : 133 : }
1700 : :
1701 : 43269939 : return rc;
1702 : 0 : }
1703 : :
1704 : : struct spdk_nvme_transport_poll_group *
1705 : 1186 : nvme_pcie_poll_group_create(void)
1706 : : {
1707 : 1186 : struct nvme_pcie_poll_group *group = calloc(1, sizeof(*group));
1708 : :
1709 [ + + ]: 1186 : if (group == NULL) {
1710 : 0 : SPDK_ERRLOG("Unable to allocate poll group.\n");
1711 : 0 : return NULL;
1712 : : }
1713 : :
1714 [ + - ]: 1186 : return &group->group;
1715 : 2 : }
1716 : :
1717 : : int
1718 : 1296 : nvme_pcie_poll_group_connect_qpair(struct spdk_nvme_qpair *qpair)
1719 : : {
1720 : 1296 : return 0;
1721 : : }
1722 : :
1723 : : int
1724 : 1296 : nvme_pcie_poll_group_disconnect_qpair(struct spdk_nvme_qpair *qpair)
1725 : : {
1726 : 1296 : return 0;
1727 : : }
1728 : :
1729 : : int
1730 : 1296 : nvme_pcie_poll_group_add(struct spdk_nvme_transport_poll_group *tgroup,
1731 : : struct spdk_nvme_qpair *qpair)
1732 : : {
1733 : 1296 : return 0;
1734 : : }
1735 : :
1736 : : int
1737 : 1296 : nvme_pcie_poll_group_remove(struct spdk_nvme_transport_poll_group *tgroup,
1738 : : struct spdk_nvme_qpair *qpair)
1739 : : {
1740 : 1296 : struct nvme_pcie_qpair *pqpair = nvme_pcie_qpair(qpair);
1741 : :
1742 [ + - + - ]: 1296 : pqpair->stat = &g_dummy_stat;
1743 : 1296 : return 0;
1744 : 0 : }
1745 : :
1746 : : int64_t
1747 : 797569570 : nvme_pcie_poll_group_process_completions(struct spdk_nvme_transport_poll_group *tgroup,
1748 : : uint32_t completions_per_qpair, spdk_nvme_disconnected_qpair_cb disconnected_qpair_cb)
1749 : : {
1750 : 0 : struct spdk_nvme_qpair *qpair, *tmp_qpair;
1751 : 797569570 : int32_t local_completions = 0;
1752 : 797569570 : int64_t total_completions = 0;
1753 : :
1754 [ + + + - : 797570761 : STAILQ_FOREACH_SAFE(qpair, &tgroup->disconnected_qpairs, poll_group_stailq, tmp_qpair) {
+ - + + +
- + - + -
+ + ]
1755 [ - + + - : 1191 : disconnected_qpair_cb(qpair, tgroup->group->ctx);
+ - + - +
- + - ]
1756 : 1 : }
1757 : :
1758 [ + + + - : 1613686807 : STAILQ_FOREACH_SAFE(qpair, &tgroup->connected_qpairs, poll_group_stailq, tmp_qpair) {
+ - + + +
- + - + -
+ + ]
1759 : 816117237 : local_completions = spdk_nvme_qpair_process_completions(qpair, completions_per_qpair);
1760 [ - + ]: 816117237 : if (spdk_unlikely(local_completions < 0)) {
1761 [ # # # # : 0 : disconnected_qpair_cb(qpair, tgroup->group->ctx);
# # # # #
# # # ]
1762 : 0 : total_completions = -ENXIO;
1763 [ + + ]: 816117237 : } else if (spdk_likely(total_completions >= 0)) {
1764 [ + - ]: 816117237 : total_completions += local_completions;
1765 : 66 : }
1766 : 66 : }
1767 : :
1768 : 797569570 : return total_completions;
1769 : 0 : }
1770 : :
1771 : : int
1772 : 1186 : nvme_pcie_poll_group_destroy(struct spdk_nvme_transport_poll_group *tgroup)
1773 : : {
1774 [ + - + + : 1186 : if (!STAILQ_EMPTY(&tgroup->connected_qpairs) || !STAILQ_EMPTY(&tgroup->disconnected_qpairs)) {
+ - + - +
- + - + -
- + ]
1775 : 0 : return -EBUSY;
1776 : : }
1777 : :
1778 : 1186 : free(tgroup);
1779 : :
1780 : 1186 : return 0;
1781 : 2 : }
1782 : :
1783 : : int
1784 : 18 : nvme_pcie_poll_group_get_stats(struct spdk_nvme_transport_poll_group *tgroup,
1785 : : struct spdk_nvme_transport_poll_group_stat **_stats)
1786 : : {
1787 : 3 : struct nvme_pcie_poll_group *group;
1788 : 3 : struct spdk_nvme_transport_poll_group_stat *stats;
1789 : :
1790 [ + + + + ]: 18 : if (tgroup == NULL || _stats == NULL) {
1791 : 12 : SPDK_ERRLOG("Invalid stats or group pointer\n");
1792 : 12 : return -EINVAL;
1793 : : }
1794 : :
1795 : 6 : stats = calloc(1, sizeof(*stats));
1796 [ + + ]: 6 : if (!stats) {
1797 : 0 : SPDK_ERRLOG("Can't allocate memory for stats\n");
1798 : 0 : return -ENOMEM;
1799 : : }
1800 [ - + - + ]: 6 : stats->trtype = SPDK_NVME_TRANSPORT_PCIE;
1801 : 6 : group = SPDK_CONTAINEROF(tgroup, struct nvme_pcie_poll_group, group);
1802 [ - + - + : 6 : memcpy(&stats->pcie, &group->stats, sizeof(group->stats));
- + - + -
+ ]
1803 : :
1804 [ - + ]: 6 : *_stats = stats;
1805 : :
1806 : 6 : return 0;
1807 : 3 : }
1808 : :
1809 : : void
1810 : 6 : nvme_pcie_poll_group_free_stats(struct spdk_nvme_transport_poll_group *tgroup,
1811 : : struct spdk_nvme_transport_poll_group_stat *stats)
1812 : : {
1813 : 6 : free(stats);
1814 : 6 : }
1815 : :
1816 : : static void
1817 : 1826 : nvme_pcie_trace(void)
1818 : : {
1819 : 1826 : struct spdk_trace_tpoint_opts opts[] = {
1820 : : {
1821 : : "NVME_PCIE_SUBMIT", TRACE_NVME_PCIE_SUBMIT,
1822 : : OWNER_NVME_PCIE_QP, OBJECT_NVME_PCIE_REQ, 1,
1823 : : { { "ctx", SPDK_TRACE_ARG_TYPE_PTR, 8 },
1824 : : { "cid", SPDK_TRACE_ARG_TYPE_INT, 4 },
1825 : : { "opc", SPDK_TRACE_ARG_TYPE_INT, 4 },
1826 : : { "dw10", SPDK_TRACE_ARG_TYPE_PTR, 4 },
1827 : : { "dw11", SPDK_TRACE_ARG_TYPE_PTR, 4 },
1828 : : { "dw12", SPDK_TRACE_ARG_TYPE_PTR, 4 }
1829 : : }
1830 : : },
1831 : : {
1832 : : "NVME_PCIE_COMPLETE", TRACE_NVME_PCIE_COMPLETE,
1833 : : OWNER_NVME_PCIE_QP, OBJECT_NVME_PCIE_REQ, 0,
1834 : : { { "ctx", SPDK_TRACE_ARG_TYPE_PTR, 8 },
1835 : : { "cid", SPDK_TRACE_ARG_TYPE_INT, 4 },
1836 : : { "cpl", SPDK_TRACE_ARG_TYPE_PTR, 4 }
1837 : : }
1838 : : },
1839 : : };
1840 : :
1841 : 1826 : spdk_trace_register_object(OBJECT_NVME_PCIE_REQ, 'p');
1842 : 1826 : spdk_trace_register_owner(OWNER_NVME_PCIE_QP, 'q');
1843 : 1826 : spdk_trace_register_description_ext(opts, SPDK_COUNTOF(opts));
1844 : 1826 : }
1845 : 2372 : SPDK_TRACE_REGISTER_FN(nvme_pcie_trace, "nvme_pcie", TRACE_GROUP_NVME_PCIE)
|