1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (C) 2012-2014 Intel Corporation
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/param.h>
30 #include <sys/bus.h>
31 #include <sys/conf.h>
32 #include <sys/domainset.h>
33 #include <sys/proc.h>
34 #include <sys/sbuf.h>
35
36 #include <dev/pci/pcivar.h>
37
38 #include "nvme_private.h"
39
40 typedef enum error_print { ERROR_PRINT_NONE, ERROR_PRINT_NO_RETRY, ERROR_PRINT_ALL } error_print_t;
41 #define DO_NOT_RETRY 1
42
43 static void _nvme_qpair_submit_request(struct nvme_qpair *qpair,
44 struct nvme_request *req);
45 static void nvme_qpair_destroy(struct nvme_qpair *qpair);
46
47 static const char *
get_opcode_string(bool admin,uint8_t opc,char * buf,size_t len)48 get_opcode_string(bool admin, uint8_t opc, char *buf, size_t len)
49 {
50 struct sbuf sb;
51
52 sbuf_new(&sb, buf, len, SBUF_FIXEDLEN);
53 nvme_opcode_sbuf(admin, opc, &sb);
54 if (sbuf_finish(&sb) != 0)
55 return ("");
56 return (buf);
57 }
58
59 static void
nvme_admin_qpair_print_command(struct nvme_qpair * qpair,struct nvme_command * cmd)60 nvme_admin_qpair_print_command(struct nvme_qpair *qpair,
61 struct nvme_command *cmd)
62 {
63 char buf[64];
64
65 nvme_printf(qpair->ctrlr, "%s sqid:%d cid:%d nsid:%x "
66 "cdw10:%08x cdw11:%08x\n",
67 get_opcode_string(true, cmd->opc, buf, sizeof(buf)), qpair->id,
68 cmd->cid, le32toh(cmd->nsid), le32toh(cmd->cdw10),
69 le32toh(cmd->cdw11));
70 }
71
72 static void
nvme_io_qpair_print_command(struct nvme_qpair * qpair,struct nvme_command * cmd)73 nvme_io_qpair_print_command(struct nvme_qpair *qpair,
74 struct nvme_command *cmd)
75 {
76 char buf[64];
77
78 switch (cmd->opc) {
79 case NVME_OPC_WRITE:
80 case NVME_OPC_READ:
81 case NVME_OPC_WRITE_UNCORRECTABLE:
82 case NVME_OPC_COMPARE:
83 case NVME_OPC_WRITE_ZEROES:
84 case NVME_OPC_VERIFY:
85 nvme_printf(qpair->ctrlr, "%s sqid:%d cid:%d nsid:%d "
86 "lba:%llu len:%d\n",
87 get_opcode_string(false, cmd->opc, buf, sizeof(buf)),
88 qpair->id, cmd->cid, le32toh(cmd->nsid),
89 ((unsigned long long)le32toh(cmd->cdw11) << 32) + le32toh(cmd->cdw10),
90 (le32toh(cmd->cdw12) & 0xFFFF) + 1);
91 break;
92 default:
93 nvme_printf(qpair->ctrlr, "%s sqid:%d cid:%d nsid:%d\n",
94 get_opcode_string(false, cmd->opc, buf, sizeof(buf)),
95 qpair->id, cmd->cid, le32toh(cmd->nsid));
96 break;
97 }
98 }
99
100 void
nvme_qpair_print_command(struct nvme_qpair * qpair,struct nvme_command * cmd)101 nvme_qpair_print_command(struct nvme_qpair *qpair, struct nvme_command *cmd)
102 {
103 if (qpair->id == 0)
104 nvme_admin_qpair_print_command(qpair, cmd);
105 else
106 nvme_io_qpair_print_command(qpair, cmd);
107 if (nvme_verbose_cmd_dump) {
108 nvme_printf(qpair->ctrlr,
109 "nsid:%#x rsvd2:%#x rsvd3:%#x mptr:%#jx prp1:%#jx prp2:%#jx\n",
110 cmd->nsid, cmd->rsvd2, cmd->rsvd3, (uintmax_t)cmd->mptr,
111 (uintmax_t)cmd->prp1, (uintmax_t)cmd->prp2);
112 nvme_printf(qpair->ctrlr,
113 "cdw10: %#x cdw11:%#x cdw12:%#x cdw13:%#x cdw14:%#x cdw15:%#x\n",
114 cmd->cdw10, cmd->cdw11, cmd->cdw12, cmd->cdw13, cmd->cdw14,
115 cmd->cdw15);
116 }
117 }
118
119 static const char *
get_status_string(const struct nvme_completion * cpl,char * buf,size_t len)120 get_status_string(const struct nvme_completion *cpl, char *buf, size_t len)
121 {
122 struct sbuf sb;
123
124 sbuf_new(&sb, buf, len, SBUF_FIXEDLEN);
125 nvme_sc_sbuf(cpl, &sb);
126 if (sbuf_finish(&sb) != 0)
127 return ("");
128 return (buf);
129 }
130
131 void
nvme_qpair_print_completion(struct nvme_qpair * qpair,struct nvme_completion * cpl)132 nvme_qpair_print_completion(struct nvme_qpair *qpair,
133 struct nvme_completion *cpl)
134 {
135 char buf[64];
136 uint8_t crd, m, dnr, p;
137
138 crd = NVME_STATUS_GET_CRD(cpl->status);
139 m = NVME_STATUS_GET_M(cpl->status);
140 dnr = NVME_STATUS_GET_DNR(cpl->status);
141 p = NVME_STATUS_GET_P(cpl->status);
142
143 nvme_printf(qpair->ctrlr, "%s crd:%x m:%x dnr:%x p:%d "
144 "sqid:%d cid:%d cdw0:%x\n",
145 get_status_string(cpl, buf, sizeof(buf)), crd, m, dnr, p,
146 cpl->sqid, cpl->cid, cpl->cdw0);
147 }
148
149 static bool
nvme_completion_is_retry(const struct nvme_completion * cpl)150 nvme_completion_is_retry(const struct nvme_completion *cpl)
151 {
152 uint8_t sct, sc, dnr;
153
154 sct = NVME_STATUS_GET_SCT(cpl->status);
155 sc = NVME_STATUS_GET_SC(cpl->status);
156 dnr = NVME_STATUS_GET_DNR(cpl->status); /* Do Not Retry Bit */
157
158 /*
159 * TODO: spec is not clear how commands that are aborted due
160 * to TLER will be marked. So for now, it seems
161 * NAMESPACE_NOT_READY is the only case where we should
162 * look at the DNR bit. Requests failed with ABORTED_BY_REQUEST
163 * set the DNR bit correctly since the driver controls that.
164 */
165 switch (sct) {
166 case NVME_SCT_GENERIC:
167 switch (sc) {
168 case NVME_SC_ABORTED_BY_REQUEST:
169 case NVME_SC_NAMESPACE_NOT_READY:
170 if (dnr)
171 return (0);
172 else
173 return (1);
174 case NVME_SC_INVALID_OPCODE:
175 case NVME_SC_INVALID_FIELD:
176 case NVME_SC_COMMAND_ID_CONFLICT:
177 case NVME_SC_DATA_TRANSFER_ERROR:
178 case NVME_SC_ABORTED_POWER_LOSS:
179 case NVME_SC_INTERNAL_DEVICE_ERROR:
180 case NVME_SC_ABORTED_SQ_DELETION:
181 case NVME_SC_ABORTED_FAILED_FUSED:
182 case NVME_SC_ABORTED_MISSING_FUSED:
183 case NVME_SC_INVALID_NAMESPACE_OR_FORMAT:
184 case NVME_SC_COMMAND_SEQUENCE_ERROR:
185 case NVME_SC_LBA_OUT_OF_RANGE:
186 case NVME_SC_CAPACITY_EXCEEDED:
187 default:
188 return (0);
189 }
190 case NVME_SCT_COMMAND_SPECIFIC:
191 case NVME_SCT_MEDIA_ERROR:
192 return (0);
193 case NVME_SCT_PATH_RELATED:
194 switch (sc) {
195 case NVME_SC_INTERNAL_PATH_ERROR:
196 if (dnr)
197 return (0);
198 else
199 return (1);
200 default:
201 return (0);
202 }
203 case NVME_SCT_VENDOR_SPECIFIC:
204 default:
205 return (0);
206 }
207 }
208
209 static void
nvme_qpair_complete_tracker(struct nvme_tracker * tr,struct nvme_completion * cpl,error_print_t print_on_error)210 nvme_qpair_complete_tracker(struct nvme_tracker *tr,
211 struct nvme_completion *cpl, error_print_t print_on_error)
212 {
213 struct nvme_qpair *qpair = tr->qpair;
214 struct nvme_request *req;
215 bool retry, error, retriable;
216
217 mtx_assert(&qpair->lock, MA_NOTOWNED);
218
219 req = tr->req;
220 error = nvme_completion_is_error(cpl);
221 retriable = nvme_completion_is_retry(cpl);
222 retry = error && retriable && req->retries < nvme_retry_count;
223 if (retry)
224 qpair->num_retries++;
225 if (error && req->retries >= nvme_retry_count && retriable)
226 qpair->num_failures++;
227
228 if (error && (print_on_error == ERROR_PRINT_ALL ||
229 (!retry && print_on_error == ERROR_PRINT_NO_RETRY))) {
230 nvme_qpair_print_command(qpair, &req->cmd);
231 nvme_qpair_print_completion(qpair, cpl);
232 }
233
234 qpair->act_tr[cpl->cid - qpair->cid_base] = NULL;
235
236 KASSERT(cpl->cid == req->cmd.cid, ("cpl cid does not match cmd cid\n"));
237
238 if (!retry) {
239 if (req->payload_valid) {
240 bus_dmamap_sync(qpair->dma_tag_payload,
241 tr->payload_dma_map,
242 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
243 }
244 if (req->cb_fn)
245 req->cb_fn(req->cb_arg, cpl);
246 }
247
248 mtx_lock(&qpair->lock);
249
250 if (retry) {
251 req->retries++;
252 nvme_qpair_submit_tracker(qpair, tr);
253 } else {
254 if (req->payload_valid) {
255 bus_dmamap_unload(qpair->dma_tag_payload,
256 tr->payload_dma_map);
257 }
258
259 nvme_free_request(req);
260 tr->req = NULL;
261
262 TAILQ_REMOVE(&qpair->outstanding_tr, tr, tailq);
263 TAILQ_INSERT_HEAD(&qpair->free_tr, tr, tailq);
264
265 /*
266 * If the controller is in the middle of resetting, don't
267 * try to submit queued requests here - let the reset logic
268 * handle that instead.
269 */
270 if (!STAILQ_EMPTY(&qpair->queued_req) &&
271 !qpair->ctrlr->is_resetting) {
272 req = STAILQ_FIRST(&qpair->queued_req);
273 STAILQ_REMOVE_HEAD(&qpair->queued_req, stailq);
274 _nvme_qpair_submit_request(qpair, req);
275 }
276 }
277
278 mtx_unlock(&qpair->lock);
279 }
280
281 static uint32_t
nvme_qpair_make_status(uint32_t sct,uint32_t sc,uint32_t dnr)282 nvme_qpair_make_status(uint32_t sct, uint32_t sc, uint32_t dnr)
283 {
284 uint32_t status = 0;
285
286 status |= NVMEF(NVME_STATUS_SCT, sct);
287 status |= NVMEF(NVME_STATUS_SC, sc);
288 status |= NVMEF(NVME_STATUS_DNR, dnr);
289 /* M=0 : this is artificial so no data in error log page */
290 /* CRD=0 : this is artificial and no delayed retry support anyway */
291 /* P=0 : phase not checked */
292 return (status);
293 }
294
295 static void
nvme_qpair_manual_complete_tracker(struct nvme_tracker * tr,uint32_t sct,uint32_t sc,uint32_t dnr,error_print_t print_on_error)296 nvme_qpair_manual_complete_tracker(
297 struct nvme_tracker *tr, uint32_t sct, uint32_t sc, uint32_t dnr,
298 error_print_t print_on_error)
299 {
300 struct nvme_completion cpl;
301 struct nvme_qpair * qpair = tr->qpair;
302
303 mtx_assert(&qpair->lock, MA_NOTOWNED);
304
305 memset(&cpl, 0, sizeof(cpl));
306
307 cpl.sqid = qpair->id;
308 cpl.cid = qpair->cid_base + tr->cid;
309 cpl.status = nvme_qpair_make_status(sct, sc, dnr);
310 nvme_qpair_complete_tracker(tr, &cpl, print_on_error);
311 }
312
313 static void
nvme_qpair_manual_complete_request(struct nvme_qpair * qpair,struct nvme_request * req,uint32_t sct,uint32_t sc,uint32_t dnr,error_print_t print_on_error)314 nvme_qpair_manual_complete_request(struct nvme_qpair *qpair,
315 struct nvme_request *req, uint32_t sct, uint32_t sc, uint32_t dnr,
316 error_print_t print_on_error)
317 {
318 struct nvme_completion cpl;
319 bool error;
320
321 memset(&cpl, 0, sizeof(cpl));
322 cpl.sqid = qpair->id;
323 cpl.status = nvme_qpair_make_status(sct, sc, dnr);
324 error = nvme_completion_is_error(&cpl);
325
326 if (error && print_on_error == ERROR_PRINT_ALL) {
327 nvme_qpair_print_command(qpair, &req->cmd);
328 nvme_qpair_print_completion(qpair, &cpl);
329 }
330
331 if (req->cb_fn)
332 req->cb_fn(req->cb_arg, &cpl);
333
334 nvme_free_request(req);
335 }
336
337 /* Locked version of completion processor */
338 static bool
_nvme_qpair_process_completions(struct nvme_qpair * qpair)339 _nvme_qpair_process_completions(struct nvme_qpair *qpair)
340 {
341 struct nvme_tracker *tr;
342 struct nvme_completion cpl;
343 bool done = false;
344 bool in_panic = dumping || SCHEDULER_STOPPED();
345
346 mtx_assert(&qpair->recovery, MA_OWNED);
347
348 /*
349 * qpair is not enabled, likely because a controller reset is in
350 * progress. Ignore the interrupt - any I/O that was associated with
351 * this interrupt will get retried when the reset is complete. Any
352 * pending completions for when we're in startup will be completed
353 * as soon as initialization is complete and we start sending commands
354 * to the device.
355 */
356 if (qpair->recovery_state != RECOVERY_NONE) {
357 qpair->num_ignored++;
358 return (false);
359 }
360
361 /*
362 * Sanity check initialization. After we reset the hardware, the phase
363 * is defined to be 1. So if we get here with zero prior calls and the
364 * phase is 0, it means that we've lost a race between the
365 * initialization and the ISR running. With the phase wrong, we'll
366 * process a bunch of completions that aren't really completions leading
367 * to a KASSERT below.
368 */
369 KASSERT(!(qpair->num_intr_handler_calls == 0 && qpair->phase == 0),
370 ("%s: Phase wrong for first interrupt call.",
371 device_get_nameunit(qpair->ctrlr->dev)));
372
373 qpair->num_intr_handler_calls++;
374
375 bus_dmamap_sync(qpair->dma_tag, qpair->queuemem_map,
376 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
377 /*
378 * A panic can stop the CPU this routine is running on at any point. If
379 * we're called during a panic, complete the sq_head wrap protocol for
380 * the case where we are interrupted just after the increment at 1
381 * below, but before we can reset cq_head to zero at 2. Also cope with
382 * the case where we do the zero at 2, but may or may not have done the
383 * phase adjustment at step 3. The panic machinery flushes all pending
384 * memory writes, so we can make these strong ordering assumptions
385 * that would otherwise be unwise if we were racing in real time.
386 */
387 if (__predict_false(in_panic)) {
388 if (qpair->cq_head == qpair->num_entries) {
389 /*
390 * Here we know that we need to zero cq_head and then negate
391 * the phase, which hasn't been assigned if cq_head isn't
392 * zero due to the atomic_store_rel.
393 */
394 qpair->cq_head = 0;
395 qpair->phase = !qpair->phase;
396 } else if (qpair->cq_head == 0) {
397 /*
398 * In this case, we know that the assignment at 2
399 * happened below, but we don't know if it 3 happened or
400 * not. To do this, we look at the last completion
401 * entry and set the phase to the opposite phase
402 * that it has. This gets us back in sync
403 */
404 cpl = qpair->cpl[qpair->num_entries - 1];
405 nvme_completion_swapbytes(&cpl);
406 qpair->phase = !NVME_STATUS_GET_P(cpl.status);
407 }
408 }
409
410 while (1) {
411 uint16_t status;
412
413 /*
414 * We need to do this dance to avoid a race between the host and
415 * the device where the device overtakes the host while the host
416 * is reading this record, leaving the status field 'new' and
417 * the sqhd and cid fields potentially stale. If the phase
418 * doesn't match, that means status hasn't yet been updated and
419 * we'll get any pending changes next time. It also means that
420 * the phase must be the same the second time. We have to sync
421 * before reading to ensure any bouncing completes.
422 */
423 status = le16toh(qpair->cpl[qpair->cq_head].status);
424 if (NVME_STATUS_GET_P(status) != qpair->phase)
425 break;
426
427 bus_dmamap_sync(qpair->dma_tag, qpair->queuemem_map,
428 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
429 cpl = qpair->cpl[qpair->cq_head];
430 nvme_completion_swapbytes(&cpl);
431
432 KASSERT(
433 NVME_STATUS_GET_P(status) == NVME_STATUS_GET_P(cpl.status),
434 ("Phase unexpectedly inconsistent"));
435
436 if (cpl.cid >= qpair->cid_base &&
437 cpl.cid < qpair->cid_base + qpair->num_trackers)
438 tr = qpair->act_tr[cpl.cid - qpair->cid_base];
439 else
440 tr = NULL;
441
442 done = true;
443 if (tr != NULL) {
444 nvme_qpair_complete_tracker(tr, &cpl, ERROR_PRINT_ALL);
445 qpair->sq_head = cpl.sqhd;
446 } else if (!in_panic) {
447 /*
448 * A missing tracker is normally an error. However, a
449 * panic can stop the CPU this routine is running on
450 * after completing an I/O but before updating
451 * qpair->cq_head at 1 below. Later, we re-enter this
452 * routine to poll I/O associated with the kernel
453 * dump. We find that the tr has been set to null before
454 * calling the completion routine. If it hasn't
455 * completed (or it triggers a panic), then '1' below
456 * won't have updated cq_head. Rather than panic again,
457 * ignore this condition because it's not unexpected.
458 */
459 nvme_printf(qpair->ctrlr,
460 "cpl (cid = %u) does not map to outstanding cmd\n",
461 cpl.cid);
462 nvme_qpair_print_completion(qpair,
463 &qpair->cpl[qpair->cq_head]);
464 KASSERT(0, ("received completion for unknown cmd"));
465 }
466
467 /*
468 * There's a number of races with the following (see above) when
469 * the system panics. We compensate for each one of them by
470 * using the atomic store to force strong ordering (at least when
471 * viewed in the aftermath of a panic).
472 */
473 if (++qpair->cq_head == qpair->num_entries) { /* 1 */
474 atomic_store_rel_int(&qpair->cq_head, 0); /* 2 */
475 qpair->phase = !qpair->phase; /* 3 */
476 }
477 }
478
479 if (done) {
480 bus_write_4(qpair->ctrlr->resource, qpair->cq_hdbl_off,
481 qpair->cq_head);
482 }
483
484 return (done);
485 }
486
487 bool
nvme_qpair_process_completions(struct nvme_qpair * qpair)488 nvme_qpair_process_completions(struct nvme_qpair *qpair)
489 {
490 bool done = false;
491
492 /*
493 * Interlock with reset / recovery code. This is an usually uncontended
494 * to make sure that we drain out of the ISRs before we reset the card
495 * and to prevent races with the recovery process called from a timeout
496 * context.
497 */
498 mtx_lock(&qpair->recovery);
499
500 if (__predict_true(qpair->recovery_state == RECOVERY_NONE))
501 done = _nvme_qpair_process_completions(qpair);
502 else
503 qpair->num_recovery_nolock++; // XXX likely need to rename
504
505 mtx_unlock(&qpair->recovery);
506
507 return (done);
508 }
509
510 static void
nvme_qpair_msi_handler(void * arg)511 nvme_qpair_msi_handler(void *arg)
512 {
513 struct nvme_qpair *qpair = arg;
514
515 nvme_qpair_process_completions(qpair);
516 }
517
518 int
nvme_qpair_construct(struct nvme_qpair * qpair,uint32_t num_entries,uint32_t num_trackers,struct nvme_controller * ctrlr)519 nvme_qpair_construct(struct nvme_qpair *qpair,
520 uint32_t num_entries, uint32_t num_trackers,
521 struct nvme_controller *ctrlr)
522 {
523 struct nvme_tracker *tr;
524 size_t cmdsz, cplsz, prpsz, allocsz, prpmemsz;
525 uint64_t queuemem_phys, prpmem_phys, list_phys;
526 uint8_t *queuemem, *prpmem, *prp_list;
527 int i, err;
528
529 qpair->vector = ctrlr->msi_count > 1 ? qpair->id : 0;
530 qpair->num_entries = num_entries;
531 qpair->num_trackers = num_trackers;
532 qpair->ctrlr = ctrlr;
533
534 KASSERT(ctrlr->io_sqes == NVME_IOSQES_64 ||
535 ctrlr->io_sqes == NVME_IOSQES_128,
536 ("invalid CC.IOSQES value %u", ctrlr->io_sqes));
537 /* Admin SQEs are always 64 bytes. */
538 qpair->sqe_shift = qpair->id == 0 ? 0 :
539 ctrlr->io_sqes - NVME_IOSQES_64;
540 if ((ctrlr->quirks & QUIRK_APPLE_SHARED_CID_SPACE) && qpair->id != 0)
541 qpair->cid_base = ctrlr->adminq.num_trackers;
542 else
543 qpair->cid_base = 0;
544
545 mtx_init(&qpair->lock, "nvme qpair lock", NULL, MTX_DEF);
546 mtx_init(&qpair->recovery, "nvme qpair recovery", NULL, MTX_DEF);
547
548 callout_init_mtx(&qpair->timer, &qpair->recovery, 0);
549 qpair->timer_armed = false;
550 qpair->recovery_state = RECOVERY_WAITING;
551
552 /* Note: NVMe PRP format is restricted to 4-byte alignment. */
553 err = bus_dma_tag_create(bus_get_dma_tag(ctrlr->dev),
554 4, ctrlr->page_size, BUS_SPACE_MAXADDR,
555 BUS_SPACE_MAXADDR, NULL, NULL, ctrlr->max_xfer_size,
556 howmany(ctrlr->max_xfer_size, ctrlr->page_size) + 1,
557 ctrlr->page_size, 0,
558 NULL, NULL, &qpair->dma_tag_payload);
559 if (err != 0) {
560 nvme_printf(ctrlr, "payload tag create failed %d\n", err);
561 goto out;
562 }
563
564 /*
565 * Each component must be page aligned, and individual PRP lists
566 * cannot cross a page boundary.
567 */
568 cmdsz = qpair->num_entries * sizeof(struct nvme_command) << qpair->sqe_shift;
569 cmdsz = roundup2(cmdsz, ctrlr->page_size);
570 cplsz = qpair->num_entries * sizeof(struct nvme_completion);
571 cplsz = roundup2(cplsz, ctrlr->page_size);
572 /*
573 * For commands requiring more than 2 PRP entries, one PRP will be
574 * embedded in the command (prp1), and the rest of the PRP entries
575 * will be in a list pointed to by the command (prp2).
576 */
577 prpsz = sizeof(uint64_t) *
578 howmany(ctrlr->max_xfer_size, ctrlr->page_size);
579 prpmemsz = qpair->num_trackers * prpsz;
580 allocsz = cmdsz + cplsz + prpmemsz;
581
582 err = bus_dma_tag_create(bus_get_dma_tag(ctrlr->dev),
583 ctrlr->page_size, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
584 allocsz, 1, allocsz, 0, NULL, NULL, &qpair->dma_tag);
585 if (err != 0) {
586 nvme_printf(ctrlr, "tag create failed %d\n", err);
587 goto out;
588 }
589 bus_dma_tag_set_domain(qpair->dma_tag, qpair->domain);
590
591 if (bus_dmamem_alloc(qpair->dma_tag, (void **)&queuemem,
592 BUS_DMA_COHERENT | BUS_DMA_NOWAIT, &qpair->queuemem_map)) {
593 nvme_printf(ctrlr, "failed to alloc qpair memory\n");
594 goto out;
595 }
596
597 if (bus_dmamap_load(qpair->dma_tag, qpair->queuemem_map,
598 queuemem, allocsz, nvme_single_map, &queuemem_phys, 0) != 0) {
599 nvme_printf(ctrlr, "failed to load qpair memory\n");
600 bus_dmamem_free(qpair->dma_tag, qpair->cmd,
601 qpair->queuemem_map);
602 goto out;
603 }
604
605 qpair->num_cmds = 0;
606 qpair->num_intr_handler_calls = 0;
607 qpair->num_retries = 0;
608 qpair->num_failures = 0;
609 qpair->num_ignored = 0;
610 qpair->cmd = (struct nvme_command *)queuemem;
611 qpair->cpl = (struct nvme_completion *)(queuemem + cmdsz);
612 prpmem = (uint8_t *)(queuemem + cmdsz + cplsz);
613 qpair->cmd_bus_addr = queuemem_phys;
614 qpair->cpl_bus_addr = queuemem_phys + cmdsz;
615 prpmem_phys = queuemem_phys + cmdsz + cplsz;
616
617 /*
618 * Calcuate the stride of the doorbell register. Many emulators set this
619 * value to correspond to a cache line. However, some hardware has set
620 * it to various small values.
621 */
622 qpair->sq_tdbl_off = nvme_mmio_offsetof(doorbell[0]) +
623 (qpair->id << (ctrlr->dstrd + 1));
624 qpair->cq_hdbl_off = nvme_mmio_offsetof(doorbell[0]) +
625 (qpair->id << (ctrlr->dstrd + 1)) + (1 << ctrlr->dstrd);
626
627 TAILQ_INIT(&qpair->free_tr);
628 TAILQ_INIT(&qpair->outstanding_tr);
629 STAILQ_INIT(&qpair->queued_req);
630
631 list_phys = prpmem_phys;
632 prp_list = prpmem;
633 for (i = 0; i < qpair->num_trackers; i++) {
634 if (list_phys + prpsz > prpmem_phys + prpmemsz) {
635 qpair->num_trackers = i;
636 break;
637 }
638
639 /*
640 * Make sure that the PRP list for this tracker doesn't
641 * overflow to another nvme page.
642 */
643 if (trunc_page(list_phys) !=
644 trunc_page(list_phys + prpsz - 1)) {
645 list_phys = roundup2(list_phys, ctrlr->page_size);
646 prp_list =
647 (uint8_t *)roundup2((uintptr_t)prp_list, ctrlr->page_size);
648 }
649
650 tr = malloc_domainset(sizeof(*tr), M_NVME,
651 DOMAINSET_PREF(qpair->domain), M_ZERO | M_WAITOK);
652 bus_dmamap_create(qpair->dma_tag_payload, 0,
653 &tr->payload_dma_map);
654 tr->cid = i;
655 tr->qpair = qpair;
656 tr->prp = (uint64_t *)prp_list;
657 tr->prp_bus_addr = list_phys;
658 TAILQ_INSERT_HEAD(&qpair->free_tr, tr, tailq);
659 list_phys += prpsz;
660 prp_list += prpsz;
661 }
662
663 if (qpair->num_trackers == 0) {
664 nvme_printf(ctrlr, "failed to allocate enough trackers\n");
665 goto out;
666 }
667
668 qpair->act_tr = malloc_domainset(sizeof(struct nvme_tracker *) *
669 qpair->num_entries, M_NVME, DOMAINSET_PREF(qpair->domain),
670 M_ZERO | M_WAITOK);
671
672 if (ctrlr->msi_count > 1) {
673 /*
674 * MSI-X vector resource IDs start at 1, so we add one to
675 * the queue's vector to get the corresponding rid to use.
676 */
677 qpair->rid = qpair->vector + 1;
678
679 qpair->res = bus_alloc_resource_any(ctrlr->dev, SYS_RES_IRQ,
680 &qpair->rid, RF_ACTIVE);
681 if (qpair->res == NULL) {
682 nvme_printf(ctrlr, "unable to allocate MSI\n");
683 goto out;
684 }
685 if (bus_setup_intr(ctrlr->dev, qpair->res,
686 INTR_TYPE_MISC | INTR_MPSAFE, NULL,
687 nvme_qpair_msi_handler, qpair, &qpair->tag) != 0) {
688 nvme_printf(ctrlr, "unable to setup MSI\n");
689 goto out;
690 }
691 if (qpair->id == 0) {
692 bus_describe_intr(ctrlr->dev, qpair->res, qpair->tag,
693 "admin");
694 } else {
695 bus_describe_intr(ctrlr->dev, qpair->res, qpair->tag,
696 "io%d", qpair->id - 1);
697 }
698 }
699
700 return (0);
701
702 out:
703 nvme_qpair_destroy(qpair);
704 return (ENOMEM);
705 }
706
707 static void
nvme_qpair_destroy(struct nvme_qpair * qpair)708 nvme_qpair_destroy(struct nvme_qpair *qpair)
709 {
710 struct nvme_tracker *tr;
711
712 mtx_lock(&qpair->recovery);
713 qpair->timer_armed = false;
714 mtx_unlock(&qpair->recovery);
715 callout_drain(&qpair->timer);
716
717 if (qpair->tag) {
718 bus_teardown_intr(qpair->ctrlr->dev, qpair->res, qpair->tag);
719 qpair->tag = NULL;
720 }
721
722 if (qpair->act_tr) {
723 free(qpair->act_tr, M_NVME);
724 qpair->act_tr = NULL;
725 }
726
727 while (!TAILQ_EMPTY(&qpair->free_tr)) {
728 tr = TAILQ_FIRST(&qpair->free_tr);
729 TAILQ_REMOVE(&qpair->free_tr, tr, tailq);
730 bus_dmamap_destroy(qpair->dma_tag_payload,
731 tr->payload_dma_map);
732 free(tr, M_NVME);
733 }
734
735 if (qpair->cmd != NULL) {
736 bus_dmamap_unload(qpair->dma_tag, qpair->queuemem_map);
737 bus_dmamem_free(qpair->dma_tag, qpair->cmd,
738 qpair->queuemem_map);
739 qpair->cmd = NULL;
740 }
741
742 if (qpair->dma_tag) {
743 bus_dma_tag_destroy(qpair->dma_tag);
744 qpair->dma_tag = NULL;
745 }
746
747 if (qpair->dma_tag_payload) {
748 bus_dma_tag_destroy(qpair->dma_tag_payload);
749 qpair->dma_tag_payload = NULL;
750 }
751
752 if (mtx_initialized(&qpair->lock))
753 mtx_destroy(&qpair->lock);
754 if (mtx_initialized(&qpair->recovery))
755 mtx_destroy(&qpair->recovery);
756
757 if (qpair->res) {
758 bus_release_resource(qpair->ctrlr->dev, SYS_RES_IRQ,
759 rman_get_rid(qpair->res), qpair->res);
760 qpair->res = NULL;
761 }
762 }
763
764 static void
nvme_admin_qpair_abort_aers(struct nvme_qpair * qpair)765 nvme_admin_qpair_abort_aers(struct nvme_qpair *qpair)
766 {
767 struct nvme_tracker *tr;
768
769 /*
770 * nvme_complete_tracker must be called without the qpair lock held. It
771 * takes the lock to adjust outstanding_tr list, so make sure we don't
772 * have it yet. We need the lock to make the list traverse safe, but
773 * have to drop the lock to complete any AER. We restart the list scan
774 * when we do this to make this safe. There's interlock with the ISR so
775 * we know this tracker won't be completed twice.
776 */
777 mtx_assert(&qpair->lock, MA_NOTOWNED);
778
779 mtx_lock(&qpair->lock);
780 tr = TAILQ_FIRST(&qpair->outstanding_tr);
781 while (tr != NULL) {
782 if (tr->req->cmd.opc != NVME_OPC_ASYNC_EVENT_REQUEST) {
783 tr = TAILQ_NEXT(tr, tailq);
784 continue;
785 }
786 mtx_unlock(&qpair->lock);
787 nvme_qpair_manual_complete_tracker(tr,
788 NVME_SCT_GENERIC, NVME_SC_ABORTED_SQ_DELETION, 0,
789 ERROR_PRINT_NONE);
790 mtx_lock(&qpair->lock);
791 tr = TAILQ_FIRST(&qpair->outstanding_tr);
792 }
793 mtx_unlock(&qpair->lock);
794 }
795
796 void
nvme_admin_qpair_destroy(struct nvme_qpair * qpair)797 nvme_admin_qpair_destroy(struct nvme_qpair *qpair)
798 {
799 mtx_assert(&qpair->lock, MA_NOTOWNED);
800
801 nvme_admin_qpair_abort_aers(qpair);
802 nvme_qpair_destroy(qpair);
803 }
804
805 void
nvme_io_qpair_destroy(struct nvme_qpair * qpair)806 nvme_io_qpair_destroy(struct nvme_qpair *qpair)
807 {
808 nvme_qpair_destroy(qpair);
809 }
810
811 static void
nvme_abort_complete(void * arg,const struct nvme_completion * status)812 nvme_abort_complete(void *arg, const struct nvme_completion *status)
813 {
814 struct nvme_tracker *tr = arg;
815
816 /*
817 * If cdw0 bit 0 == 1, the controller was not able to abort the command
818 * we requested. We still need to check the active tracker array, to
819 * cover race where I/O timed out at same time controller was completing
820 * the I/O. An abort command always is on the admin queue, but affects
821 * either an admin or an I/O queue, so take the appropriate qpair lock
822 * for the original command's queue, since we'll need it to avoid races
823 * with the completion code and to complete the command manually.
824 */
825 mtx_lock(&tr->qpair->lock);
826 if ((status->cdw0 & 1) == 1 && tr->qpair->act_tr[tr->cid] != NULL) {
827 /*
828 * An I/O has timed out, and the controller was unable to abort
829 * it for some reason. And we've not processed a completion for
830 * it yet. Construct a fake completion status, and then complete
831 * the I/O's tracker manually.
832 */
833 nvme_printf(tr->qpair->ctrlr,
834 "abort command failed, aborting command manually\n");
835 nvme_qpair_manual_complete_tracker(tr,
836 NVME_SCT_GENERIC, NVME_SC_ABORTED_BY_REQUEST, 0, ERROR_PRINT_ALL);
837 }
838 /*
839 * XXX We don't check status for the possible 'Could not abort because
840 * excess aborts were submitted to the controller'. We don't prevent
841 * that, either. Document for the future here, since the standard is
842 * squishy and only says 'may generate' but implies anything is possible
843 * including hangs if you exceed the ACL.
844 */
845 mtx_unlock(&tr->qpair->lock);
846 }
847
848 static void
nvme_qpair_timeout(void * arg)849 nvme_qpair_timeout(void *arg)
850 {
851 struct nvme_qpair *qpair = arg;
852 struct nvme_controller *ctrlr = qpair->ctrlr;
853 struct nvme_tracker *tr;
854 sbintime_t now;
855 bool idle = true;
856 bool is_admin = qpair == &ctrlr->adminq;
857 bool fast;
858 uint32_t csts;
859 uint8_t cfs;
860
861 mtx_assert(&qpair->recovery, MA_OWNED);
862
863 /*
864 * If the controller is failed, then stop polling. This ensures that any
865 * failure processing that races with the qpair timeout will fail
866 * safely.
867 */
868 if (is_admin ? qpair->ctrlr->is_failed_admin : qpair->ctrlr->is_failed) {
869 nvme_printf(qpair->ctrlr,
870 "%sFailed controller, stopping watchdog timeout.\n",
871 is_admin ? "Complete " : "");
872 qpair->timer_armed = false;
873 return;
874 }
875
876 /*
877 * Shutdown condition: We set qpair->timer_armed to false in
878 * nvme_qpair_destroy before calling callout_drain. When we call that,
879 * this routine might get called one last time. Exit w/o setting a
880 * timeout. None of the watchdog stuff needs to be done since we're
881 * destroying the qpair.
882 */
883 if (!qpair->timer_armed) {
884 nvme_printf(qpair->ctrlr,
885 "Timeout fired during nvme_qpair_destroy\n");
886 return;
887 }
888
889 switch (qpair->recovery_state) {
890 case RECOVERY_NONE:
891 /*
892 * Read csts to get value of cfs - controller fatal status. If
893 * we are in the hot-plug or controller failed status proceed
894 * directly to reset. We also bail early if the status reads all
895 * 1's or the control fatal status bit is now 1. The latter is
896 * always true when the former is true, but not vice versa. The
897 * intent of the code is that if the card is gone (all 1's) or
898 * we've failed, then try to do a reset (which someitmes
899 * unwedges a card reading all 1's that's not gone away, but
900 * usually doesn't).
901 */
902 csts = nvme_mmio_read_4(ctrlr, csts);
903 cfs = NVMEV(NVME_CSTS_REG_CFS, csts);
904 if (csts == NVME_GONE || cfs == 1) {
905 /*
906 * We've had a command timeout that we weren't able to
907 * abort or we have aborts disabled and any command
908 * timed out.
909 *
910 * If we get here due to a possible surprise hot-unplug
911 * event, then we let nvme_ctrlr_reset confirm and fail
912 * the controller.
913 */
914 do_reset:
915 nvme_printf(ctrlr, "Resetting controller due to a timeout%s.\n",
916 (csts == 0xffffffff) ? " and possible hot unplug" :
917 (cfs ? " and fatal error status" : ""));
918 qpair->recovery_state = RECOVERY_WAITING;
919 nvme_ctrlr_reset(ctrlr);
920 idle = false;
921 break;
922 }
923
924
925 /*
926 * See if there's any recovery needed. First, do a fast check to
927 * see if anything could have timed out. If not, then skip
928 * everything else.
929 */
930 fast = false;
931 mtx_lock(&qpair->lock);
932 now = getsbinuptime();
933 TAILQ_FOREACH(tr, &qpair->outstanding_tr, tailq) {
934 /*
935 * Skip async commands, they are posted to the card for
936 * an indefinite amount of time and have no deadline.
937 */
938 if (tr->deadline == SBT_MAX)
939 continue;
940
941 /*
942 * If the first real transaction is not in timeout, then
943 * we're done. Otherwise, we try recovery.
944 */
945 idle = false;
946 if (now <= tr->deadline)
947 fast = true;
948 break;
949 }
950 mtx_unlock(&qpair->lock);
951 if (idle || fast)
952 break;
953
954 /*
955 * There's a stale transaction at the start of the queue whose
956 * deadline has passed. Poll the competions as a last-ditch
957 * effort in case an interrupt has been missed. Warn the user if
958 * transactions were found of possible interrupt issues, but
959 * just once per controller.
960 */
961 if (_nvme_qpair_process_completions(qpair) && !ctrlr->isr_warned) {
962 nvme_printf(ctrlr, "System interrupt issues?\n");
963 ctrlr->isr_warned = true;
964 }
965
966 /*
967 * Now that we've run the ISR, re-rheck to see if there's any
968 * timed out commands and abort them or reset the card if so.
969 */
970 mtx_lock(&qpair->lock);
971 idle = true;
972 TAILQ_FOREACH(tr, &qpair->outstanding_tr, tailq) {
973 /*
974 * Skip async commands, they are posted to the card for
975 * an indefinite amount of time and have no deadline.
976 */
977 if (tr->deadline == SBT_MAX)
978 continue;
979
980 /*
981 * If we know this tracker hasn't timed out, we also
982 * know all subsequent ones haven't timed out. The tr
983 * queue is in submission order and all normal commands
984 * in a queue have the same timeout (or the timeout was
985 * changed by the user, but we eventually timeout then).
986 */
987 idle = false;
988 if (now <= tr->deadline)
989 break;
990
991 /*
992 * Timeout expired, abort it or reset controller.
993 */
994 if (ctrlr->enable_aborts &&
995 tr->req->cb_fn != nvme_abort_complete) {
996 /*
997 * This isn't an abort command, ask for a
998 * hardware abort. This goes to the admin
999 * queue which will reset the card if it
1000 * times out.
1001 */
1002 nvme_ctrlr_cmd_abort(ctrlr,
1003 qpair->cid_base + tr->cid, qpair->id,
1004 nvme_abort_complete, tr);
1005 } else {
1006 /*
1007 * We have a live command in the card (either
1008 * one we couldn't abort, or aborts weren't
1009 * enabled). We can only reset.
1010 */
1011 mtx_unlock(&qpair->lock);
1012 goto do_reset;
1013 }
1014 }
1015 mtx_unlock(&qpair->lock);
1016 break;
1017
1018 case RECOVERY_WAITING:
1019 /*
1020 * These messages aren't interesting while we're suspended. We
1021 * put the queues into waiting state while
1022 * suspending. Suspending takes a while, so we'll see these
1023 * during that time and they aren't diagnostic. At other times,
1024 * they indicate a problem that's worth complaining about.
1025 */
1026 if (!device_is_suspended(ctrlr->dev))
1027 nvme_printf(ctrlr, "Waiting for reset to complete\n");
1028 idle = false; /* We want to keep polling */
1029 break;
1030 }
1031
1032 /*
1033 * Rearm the timeout.
1034 */
1035 if (!idle) {
1036 callout_schedule_sbt(&qpair->timer, SBT_1S / 2, SBT_1S / 2, 0);
1037 } else {
1038 qpair->timer_armed = false;
1039 }
1040 }
1041
1042 /*
1043 * Submit the tracker to the hardware. Must already be in the
1044 * outstanding queue when called.
1045 */
1046 void
nvme_qpair_submit_tracker(struct nvme_qpair * qpair,struct nvme_tracker * tr)1047 nvme_qpair_submit_tracker(struct nvme_qpair *qpair, struct nvme_tracker *tr)
1048 {
1049 struct nvme_request *req;
1050 struct nvme_controller *ctrlr;
1051 int timeout;
1052
1053 mtx_assert(&qpair->lock, MA_OWNED);
1054
1055 req = tr->req;
1056 qpair->act_tr[tr->cid] = tr;
1057 ctrlr = qpair->ctrlr;
1058
1059 if (req->timeout) {
1060 if (req->cb_fn == nvme_completion_poll_cb)
1061 timeout = 1;
1062 else if (qpair->id == 0)
1063 timeout = ctrlr->admin_timeout_period;
1064 else
1065 timeout = ctrlr->timeout_period;
1066 tr->deadline = getsbinuptime() + timeout * SBT_1S;
1067 if (!qpair->timer_armed) {
1068 qpair->timer_armed = true;
1069 callout_reset_sbt_on(&qpair->timer, SBT_1S / 2, SBT_1S / 2,
1070 nvme_qpair_timeout, qpair, qpair->cpu, 0);
1071 }
1072 } else
1073 tr->deadline = SBT_MAX;
1074
1075 /* Copy the command from the tracker to the submission queue. */
1076 memcpy(NVME_SQE(qpair, qpair->sq_tail), &req->cmd, sizeof(req->cmd));
1077
1078 if (++qpair->sq_tail == qpair->num_entries)
1079 qpair->sq_tail = 0;
1080
1081 bus_dmamap_sync(qpair->dma_tag, qpair->queuemem_map,
1082 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1083 bus_write_4(ctrlr->resource, qpair->sq_tdbl_off, qpair->sq_tail);
1084 qpair->num_cmds++;
1085 }
1086
1087 static void
nvme_payload_map(void * arg,bus_dma_segment_t * seg,int nseg,int error)1088 nvme_payload_map(void *arg, bus_dma_segment_t *seg, int nseg, int error)
1089 {
1090 struct nvme_tracker *tr = arg;
1091 struct nvme_qpair *qpair = tr->qpair;
1092 uint32_t cur_nseg;
1093
1094 if (error != 0) {
1095 nvme_printf(qpair->ctrlr,
1096 "payload DMA mapping failed with error %d\n", error);
1097 mtx_unlock(&qpair->lock);
1098 nvme_qpair_manual_complete_tracker(tr, NVME_SCT_GENERIC,
1099 NVME_SC_DATA_TRANSFER_ERROR, DO_NOT_RETRY, ERROR_PRINT_ALL);
1100 mtx_lock(&qpair->lock);
1101 return;
1102 }
1103
1104 /*
1105 * Note that we specified ctrlr->page_size for alignment and max
1106 * segment size when creating the bus dma tags. So here we can safely
1107 * just transfer each segment to its associated PRP entry.
1108 */
1109 tr->req->cmd.prp1 = htole64(seg[0].ds_addr);
1110
1111 if (nseg == 2) {
1112 tr->req->cmd.prp2 = htole64(seg[1].ds_addr);
1113 } else if (nseg > 2) {
1114 cur_nseg = 1;
1115 tr->req->cmd.prp2 = htole64((uint64_t)tr->prp_bus_addr);
1116 while (cur_nseg < nseg) {
1117 tr->prp[cur_nseg-1] =
1118 htole64((uint64_t)seg[cur_nseg].ds_addr);
1119 cur_nseg++;
1120 }
1121 } else {
1122 /*
1123 * prp2 should not be used by the controller
1124 * since there is only one segment, but set
1125 * to 0 just to be safe.
1126 */
1127 tr->req->cmd.prp2 = 0;
1128 }
1129
1130 bus_dmamap_sync(tr->qpair->dma_tag_payload, tr->payload_dma_map,
1131 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1132 nvme_qpair_submit_tracker(tr->qpair, tr);
1133 }
1134
1135 static void
_nvme_qpair_submit_request(struct nvme_qpair * qpair,struct nvme_request * req)1136 _nvme_qpair_submit_request(struct nvme_qpair *qpair, struct nvme_request *req)
1137 {
1138 struct nvme_tracker *tr;
1139 bool is_admin = qpair == &qpair->ctrlr->adminq;
1140
1141 mtx_assert(&qpair->lock, MA_OWNED);
1142
1143 tr = TAILQ_FIRST(&qpair->free_tr);
1144 req->qpair = qpair;
1145
1146 /*
1147 * The controller has failed, so fail the request. Note, that this races
1148 * the recovery / timeout code. Since we hold the qpair lock, we know
1149 * it's safe to fail directly. is_failed is set when we fail the
1150 * controller. It is only ever reset in the ioctl reset controller
1151 * path, which is safe to race (for failed controllers, we make no
1152 * guarantees about bringing it out of failed state relative to other
1153 * commands). We try hard to allow admin commands when the entire
1154 * controller hasn't failed, only something related to I/O queues.
1155 */
1156 if (is_admin ? qpair->ctrlr->is_failed_admin : qpair->ctrlr->is_failed) {
1157 nvme_qpair_manual_complete_request(qpair, req,
1158 NVME_SCT_GENERIC, NVME_SC_ABORTED_BY_REQUEST, 1,
1159 ERROR_PRINT_NONE);
1160 return;
1161 }
1162
1163 /*
1164 * No tracker is available, or the qpair is disabled due to an
1165 * in-progress controller-level reset. If we lose the race with
1166 * recovery_state, then we may add an extra request to the queue which
1167 * will be resubmitted later. We only set recovery_state to NONE with
1168 * qpair->lock also held, so if we observe that the state is not NONE,
1169 * we know it won't transition back to NONE without retrying queued
1170 * request.
1171 */
1172 if (tr == NULL || qpair->recovery_state != RECOVERY_NONE) {
1173 STAILQ_INSERT_TAIL(&qpair->queued_req, req, stailq);
1174 return;
1175 }
1176
1177 TAILQ_REMOVE(&qpair->free_tr, tr, tailq);
1178 TAILQ_INSERT_TAIL(&qpair->outstanding_tr, tr, tailq);
1179 tr->deadline = SBT_MAX;
1180 tr->req = req;
1181 req->cmd.cid = qpair->cid_base + tr->cid;
1182
1183 if (!req->payload_valid) {
1184 nvme_qpair_submit_tracker(tr->qpair, tr);
1185 return;
1186 }
1187
1188 /*
1189 * tr->deadline updating when nvme_payload_map calls
1190 * nvme_qpair_submit_tracker (we call it above directly
1191 * when there's no map to load).
1192 */
1193 (void)bus_dmamap_load_mem(tr->qpair->dma_tag_payload,
1194 tr->payload_dma_map, &req->payload, nvme_payload_map, tr,
1195 BUS_DMA_NOWAIT);
1196 }
1197
1198 void
nvme_qpair_submit_request(struct nvme_qpair * qpair,struct nvme_request * req)1199 nvme_qpair_submit_request(struct nvme_qpair *qpair, struct nvme_request *req)
1200 {
1201 mtx_lock(&qpair->lock);
1202 _nvme_qpair_submit_request(qpair, req);
1203 mtx_unlock(&qpair->lock);
1204 }
1205
1206 static void
nvme_qpair_enable(struct nvme_qpair * qpair)1207 nvme_qpair_enable(struct nvme_qpair *qpair)
1208 {
1209 bool is_admin __diagused = qpair == &qpair->ctrlr->adminq;
1210
1211 if (mtx_initialized(&qpair->recovery))
1212 mtx_assert(&qpair->recovery, MA_OWNED);
1213 if (mtx_initialized(&qpair->lock))
1214 mtx_assert(&qpair->lock, MA_OWNED);
1215 KASSERT(!(is_admin ? qpair->ctrlr->is_failed_admin : qpair->ctrlr->is_failed),
1216 ("Enabling a failed qpair\n"));
1217
1218 qpair->recovery_state = RECOVERY_NONE;
1219 }
1220
1221 void
nvme_qpair_reset(struct nvme_qpair * qpair)1222 nvme_qpair_reset(struct nvme_qpair *qpair)
1223 {
1224 qpair->sq_head = qpair->sq_tail = qpair->cq_head = 0;
1225
1226 /*
1227 * First time through the completion queue, HW will set phase
1228 * bit on completions to 1. So set this to 1 here, indicating
1229 * we're looking for a 1 to know which entries have completed.
1230 * we'll toggle the bit each time when the completion queue
1231 * rolls over.
1232 */
1233 qpair->phase = 1;
1234
1235 memset(qpair->cmd, 0,
1236 qpair->num_entries * sizeof(struct nvme_command) << qpair->sqe_shift);
1237 memset(qpair->cpl, 0,
1238 qpair->num_entries * sizeof(struct nvme_completion));
1239 }
1240
1241 void
nvme_admin_qpair_enable(struct nvme_qpair * qpair)1242 nvme_admin_qpair_enable(struct nvme_qpair *qpair)
1243 {
1244 struct nvme_tracker *tr;
1245 struct nvme_tracker *tr_temp;
1246 bool rpt;
1247
1248 /*
1249 * Manually abort each outstanding admin command. Do not retry
1250 * admin commands found here, since they will be left over from
1251 * a controller reset and its likely the context in which the
1252 * command was issued no longer applies.
1253 */
1254 rpt = !TAILQ_EMPTY(&qpair->outstanding_tr);
1255 if (rpt)
1256 nvme_printf(qpair->ctrlr,
1257 "aborting outstanding admin command\n");
1258 TAILQ_FOREACH_SAFE(tr, &qpair->outstanding_tr, tailq, tr_temp) {
1259 nvme_qpair_manual_complete_tracker(tr, NVME_SCT_GENERIC,
1260 NVME_SC_ABORTED_BY_REQUEST, DO_NOT_RETRY, ERROR_PRINT_ALL);
1261 }
1262 if (rpt)
1263 nvme_printf(qpair->ctrlr,
1264 "done aborting outstanding admin\n");
1265
1266 mtx_lock(&qpair->recovery);
1267 mtx_lock(&qpair->lock);
1268 nvme_qpair_enable(qpair);
1269 mtx_unlock(&qpair->lock);
1270 mtx_unlock(&qpair->recovery);
1271 }
1272
1273 void
nvme_io_qpair_enable(struct nvme_qpair * qpair)1274 nvme_io_qpair_enable(struct nvme_qpair *qpair)
1275 {
1276 STAILQ_HEAD(, nvme_request) temp;
1277 struct nvme_tracker *tr;
1278 struct nvme_tracker *tr_temp;
1279 struct nvme_request *req;
1280 bool report;
1281
1282 /*
1283 * Manually abort each outstanding I/O. This normally results in a
1284 * retry, unless the retry count on the associated request has
1285 * reached its limit.
1286 */
1287 report = !TAILQ_EMPTY(&qpair->outstanding_tr);
1288 if (report)
1289 nvme_printf(qpair->ctrlr, "aborting outstanding i/o\n");
1290 TAILQ_FOREACH_SAFE(tr, &qpair->outstanding_tr, tailq, tr_temp) {
1291 nvme_qpair_manual_complete_tracker(tr, NVME_SCT_GENERIC,
1292 NVME_SC_ABORTED_BY_REQUEST, 0, ERROR_PRINT_NO_RETRY);
1293 }
1294 if (report)
1295 nvme_printf(qpair->ctrlr, "done aborting outstanding i/o\n");
1296
1297 mtx_lock(&qpair->recovery);
1298 mtx_lock(&qpair->lock);
1299 nvme_qpair_enable(qpair);
1300
1301 STAILQ_INIT(&temp);
1302 STAILQ_SWAP(&qpair->queued_req, &temp, nvme_request);
1303
1304 report = !STAILQ_EMPTY(&temp);
1305 if (report)
1306 nvme_printf(qpair->ctrlr, "resubmitting queued i/o\n");
1307 while (!STAILQ_EMPTY(&temp)) {
1308 req = STAILQ_FIRST(&temp);
1309 STAILQ_REMOVE_HEAD(&temp, stailq);
1310 nvme_qpair_print_command(qpair, &req->cmd);
1311 _nvme_qpair_submit_request(qpair, req);
1312 }
1313 if (report)
1314 nvme_printf(qpair->ctrlr, "done resubmitting i/o\n");
1315
1316 mtx_unlock(&qpair->lock);
1317 mtx_unlock(&qpair->recovery);
1318 }
1319
1320 static void
nvme_qpair_disable(struct nvme_qpair * qpair)1321 nvme_qpair_disable(struct nvme_qpair *qpair)
1322 {
1323 struct nvme_tracker *tr, *tr_temp;
1324
1325 if (mtx_initialized(&qpair->recovery))
1326 mtx_assert(&qpair->recovery, MA_OWNED);
1327 if (mtx_initialized(&qpair->lock))
1328 mtx_assert(&qpair->lock, MA_OWNED);
1329
1330 qpair->recovery_state = RECOVERY_WAITING;
1331 TAILQ_FOREACH_SAFE(tr, &qpair->outstanding_tr, tailq, tr_temp) {
1332 tr->deadline = SBT_MAX;
1333 }
1334 }
1335
1336 void
nvme_admin_qpair_disable(struct nvme_qpair * qpair)1337 nvme_admin_qpair_disable(struct nvme_qpair *qpair)
1338 {
1339 mtx_lock(&qpair->recovery);
1340
1341 mtx_lock(&qpair->lock);
1342 nvme_qpair_disable(qpair);
1343 mtx_unlock(&qpair->lock);
1344
1345 nvme_admin_qpair_abort_aers(qpair);
1346
1347 mtx_unlock(&qpair->recovery);
1348 }
1349
1350 void
nvme_io_qpair_disable(struct nvme_qpair * qpair)1351 nvme_io_qpair_disable(struct nvme_qpair *qpair)
1352 {
1353 mtx_lock(&qpair->recovery);
1354 mtx_lock(&qpair->lock);
1355
1356 nvme_qpair_disable(qpair);
1357
1358 mtx_unlock(&qpair->lock);
1359 mtx_unlock(&qpair->recovery);
1360 }
1361
1362 void
nvme_qpair_fail(struct nvme_qpair * qpair)1363 nvme_qpair_fail(struct nvme_qpair *qpair)
1364 {
1365 struct nvme_tracker *tr;
1366 struct nvme_request *req;
1367
1368 if (!mtx_initialized(&qpair->lock))
1369 return;
1370
1371 mtx_lock(&qpair->lock);
1372
1373 if (!STAILQ_EMPTY(&qpair->queued_req)) {
1374 nvme_printf(qpair->ctrlr, "failing queued i/o\n");
1375 }
1376 while (!STAILQ_EMPTY(&qpair->queued_req)) {
1377 req = STAILQ_FIRST(&qpair->queued_req);
1378 STAILQ_REMOVE_HEAD(&qpair->queued_req, stailq);
1379 mtx_unlock(&qpair->lock);
1380 nvme_qpair_manual_complete_request(qpair, req, NVME_SCT_GENERIC,
1381 NVME_SC_ABORTED_BY_REQUEST, 1, ERROR_PRINT_ALL);
1382 mtx_lock(&qpair->lock);
1383 }
1384
1385 if (!TAILQ_EMPTY(&qpair->outstanding_tr)) {
1386 nvme_printf(qpair->ctrlr, "failing outstanding i/o\n");
1387 }
1388 /* Manually abort each outstanding I/O. */
1389 while (!TAILQ_EMPTY(&qpair->outstanding_tr)) {
1390 tr = TAILQ_FIRST(&qpair->outstanding_tr);
1391 /*
1392 * Do not remove the tracker. The abort_tracker path will
1393 * do that for us.
1394 */
1395 mtx_unlock(&qpair->lock);
1396 nvme_qpair_manual_complete_tracker(tr, NVME_SCT_GENERIC,
1397 NVME_SC_ABORTED_BY_REQUEST, DO_NOT_RETRY, ERROR_PRINT_ALL);
1398 mtx_lock(&qpair->lock);
1399 }
1400
1401 mtx_unlock(&qpair->lock);
1402 }
1403