1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (C) 2012-2016 Intel Corporation
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD: stable/12/sys/dev/nvme/nvme_ctrlr.c 372125 2022-06-10 12:47:16Z gbe $");
31
32 #include "opt_cam.h"
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/buf.h>
37 #include <sys/bus.h>
38 #include <sys/conf.h>
39 #include <sys/ioccom.h>
40 #include <sys/proc.h>
41 #include <sys/smp.h>
42 #include <sys/uio.h>
43 #include <sys/sbuf.h>
44 #include <sys/endian.h>
45 #include <machine/stdarg.h>
46 #include <vm/vm.h>
47
48 #include "nvme_private.h"
49
50 #define B4_CHK_RDY_DELAY_MS 2300 /* work around controller bug */
51
52 static void nvme_ctrlr_construct_and_submit_aer(struct nvme_controller *ctrlr,
53 struct nvme_async_event_request *aer);
54
55 static void
nvme_ctrlr_devctl_log(struct nvme_controller * ctrlr,const char * type,const char * msg,...)56 nvme_ctrlr_devctl_log(struct nvme_controller *ctrlr, const char *type, const char *msg, ...)
57 {
58 struct sbuf sb;
59 va_list ap;
60 int error;
61
62 if (sbuf_new(&sb, NULL, 0, SBUF_AUTOEXTEND | SBUF_NOWAIT) == NULL)
63 return;
64 sbuf_printf(&sb, "%s: ", device_get_nameunit(ctrlr->dev));
65 va_start(ap, msg);
66 sbuf_vprintf(&sb, msg, ap);
67 va_end(ap);
68 error = sbuf_finish(&sb);
69 if (error == 0)
70 printf("%s\n", sbuf_data(&sb));
71
72 sbuf_clear(&sb);
73 sbuf_printf(&sb, "name=\"%s\" reason=\"", device_get_nameunit(ctrlr->dev));
74 va_start(ap, msg);
75 sbuf_vprintf(&sb, msg, ap);
76 va_end(ap);
77 sbuf_printf(&sb, "\"");
78 error = sbuf_finish(&sb);
79 if (error == 0)
80 devctl_notify("nvme", "controller", type, sbuf_data(&sb));
81 sbuf_delete(&sb);
82 }
83
84 static int
nvme_ctrlr_construct_admin_qpair(struct nvme_controller * ctrlr)85 nvme_ctrlr_construct_admin_qpair(struct nvme_controller *ctrlr)
86 {
87 struct nvme_qpair *qpair;
88 uint32_t num_entries;
89 int error;
90
91 qpair = &ctrlr->adminq;
92 qpair->id = 0;
93 qpair->cpu = CPU_FFS(&cpuset_domain[ctrlr->domain]) - 1;
94 qpair->domain = ctrlr->domain;
95
96 num_entries = NVME_ADMIN_ENTRIES;
97 TUNABLE_INT_FETCH("hw.nvme.admin_entries", &num_entries);
98 /*
99 * If admin_entries was overridden to an invalid value, revert it
100 * back to our default value.
101 */
102 if (num_entries < NVME_MIN_ADMIN_ENTRIES ||
103 num_entries > NVME_MAX_ADMIN_ENTRIES) {
104 nvme_printf(ctrlr, "invalid hw.nvme.admin_entries=%d "
105 "specified\n", num_entries);
106 num_entries = NVME_ADMIN_ENTRIES;
107 }
108
109 /*
110 * The admin queue's max xfer size is treated differently than the
111 * max I/O xfer size. 16KB is sufficient here - maybe even less?
112 */
113 error = nvme_qpair_construct(qpair, num_entries, NVME_ADMIN_TRACKERS,
114 ctrlr);
115 return (error);
116 }
117
118 #define QP(ctrlr, c) ((c) * (ctrlr)->num_io_queues / mp_ncpus)
119
120 static int
nvme_ctrlr_construct_io_qpairs(struct nvme_controller * ctrlr)121 nvme_ctrlr_construct_io_qpairs(struct nvme_controller *ctrlr)
122 {
123 struct nvme_qpair *qpair;
124 uint32_t cap_lo;
125 uint16_t mqes;
126 int c, error, i, n;
127 int num_entries, num_trackers, max_entries;
128
129 /*
130 * NVMe spec sets a hard limit of 64K max entries, but devices may
131 * specify a smaller limit, so we need to check the MQES field in the
132 * capabilities register. We have to cap the number of entries to the
133 * current stride allows for in BAR 0/1, otherwise the remainder entries
134 * are inaccessible. MQES should reflect this, and this is just a
135 * fail-safe.
136 */
137 max_entries =
138 (rman_get_size(ctrlr->resource) - nvme_mmio_offsetof(doorbell[0])) /
139 (1 << (ctrlr->dstrd + 1));
140 num_entries = NVME_IO_ENTRIES;
141 TUNABLE_INT_FETCH("hw.nvme.io_entries", &num_entries);
142 cap_lo = nvme_mmio_read_4(ctrlr, cap_lo);
143 mqes = NVME_CAP_LO_MQES(cap_lo);
144 num_entries = min(num_entries, mqes + 1);
145 num_entries = min(num_entries, max_entries);
146
147 num_trackers = NVME_IO_TRACKERS;
148 TUNABLE_INT_FETCH("hw.nvme.io_trackers", &num_trackers);
149
150 num_trackers = max(num_trackers, NVME_MIN_IO_TRACKERS);
151 num_trackers = min(num_trackers, NVME_MAX_IO_TRACKERS);
152 /*
153 * No need to have more trackers than entries in the submit queue. Note
154 * also that for a queue size of N, we can only have (N-1) commands
155 * outstanding, hence the "-1" here.
156 */
157 num_trackers = min(num_trackers, (num_entries-1));
158
159 /*
160 * Our best estimate for the maximum number of I/Os that we should
161 * normally have in flight at one time. This should be viewed as a hint,
162 * not a hard limit and will need to be revisited when the upper layers
163 * of the storage system grows multi-queue support.
164 */
165 ctrlr->max_hw_pend_io = num_trackers * ctrlr->num_io_queues * 3 / 4;
166
167 ctrlr->ioq = malloc(ctrlr->num_io_queues * sizeof(struct nvme_qpair),
168 M_NVME, M_ZERO | M_WAITOK);
169
170 for (i = c = n = 0; i < ctrlr->num_io_queues; i++, c += n) {
171 qpair = &ctrlr->ioq[i];
172
173 /*
174 * Admin queue has ID=0. IO queues start at ID=1 -
175 * hence the 'i+1' here.
176 */
177 qpair->id = i + 1;
178 if (ctrlr->num_io_queues > 1) {
179 /* Find number of CPUs served by this queue. */
180 for (n = 1; QP(ctrlr, c + n) == i; n++)
181 ;
182 /* Shuffle multiple NVMe devices between CPUs. */
183 qpair->cpu = c + (device_get_unit(ctrlr->dev)+n/2) % n;
184 qpair->domain = pcpu_find(qpair->cpu)->pc_domain;
185 } else {
186 qpair->cpu = CPU_FFS(&cpuset_domain[ctrlr->domain]) - 1;
187 qpair->domain = ctrlr->domain;
188 }
189
190 /*
191 * For I/O queues, use the controller-wide max_xfer_size
192 * calculated in nvme_attach().
193 */
194 error = nvme_qpair_construct(qpair, num_entries, num_trackers,
195 ctrlr);
196 if (error)
197 return (error);
198
199 /*
200 * Do not bother binding interrupts if we only have one I/O
201 * interrupt thread for this controller.
202 */
203 if (ctrlr->num_io_queues > 1)
204 bus_bind_intr(ctrlr->dev, qpair->res, qpair->cpu);
205 }
206
207 return (0);
208 }
209
210 static void
nvme_ctrlr_fail(struct nvme_controller * ctrlr)211 nvme_ctrlr_fail(struct nvme_controller *ctrlr)
212 {
213 int i;
214
215 ctrlr->is_failed = true;
216 nvme_admin_qpair_disable(&ctrlr->adminq);
217 nvme_qpair_fail(&ctrlr->adminq);
218 if (ctrlr->ioq != NULL) {
219 for (i = 0; i < ctrlr->num_io_queues; i++) {
220 nvme_io_qpair_disable(&ctrlr->ioq[i]);
221 nvme_qpair_fail(&ctrlr->ioq[i]);
222 }
223 }
224 nvme_notify_fail_consumers(ctrlr);
225 }
226
227 void
nvme_ctrlr_post_failed_request(struct nvme_controller * ctrlr,struct nvme_request * req)228 nvme_ctrlr_post_failed_request(struct nvme_controller *ctrlr,
229 struct nvme_request *req)
230 {
231
232 mtx_lock(&ctrlr->lock);
233 STAILQ_INSERT_TAIL(&ctrlr->fail_req, req, stailq);
234 mtx_unlock(&ctrlr->lock);
235 taskqueue_enqueue(ctrlr->taskqueue, &ctrlr->fail_req_task);
236 }
237
238 static void
nvme_ctrlr_fail_req_task(void * arg,int pending)239 nvme_ctrlr_fail_req_task(void *arg, int pending)
240 {
241 struct nvme_controller *ctrlr = arg;
242 struct nvme_request *req;
243
244 mtx_lock(&ctrlr->lock);
245 while ((req = STAILQ_FIRST(&ctrlr->fail_req)) != NULL) {
246 STAILQ_REMOVE_HEAD(&ctrlr->fail_req, stailq);
247 mtx_unlock(&ctrlr->lock);
248 nvme_qpair_manual_complete_request(req->qpair, req,
249 NVME_SCT_GENERIC, NVME_SC_ABORTED_BY_REQUEST);
250 mtx_lock(&ctrlr->lock);
251 }
252 mtx_unlock(&ctrlr->lock);
253 }
254
255 static int
nvme_ctrlr_wait_for_ready(struct nvme_controller * ctrlr,int desired_val)256 nvme_ctrlr_wait_for_ready(struct nvme_controller *ctrlr, int desired_val)
257 {
258 int timeout = ticks + (uint64_t)ctrlr->ready_timeout_in_ms * hz / 1000;
259 uint32_t csts;
260
261 while (1) {
262 csts = nvme_mmio_read_4(ctrlr, csts);
263 if (csts == NVME_GONE) /* Hot unplug. */
264 return (ENXIO);
265 if (((csts >> NVME_CSTS_REG_RDY_SHIFT) & NVME_CSTS_REG_RDY_MASK)
266 == desired_val)
267 break;
268 if (timeout - ticks < 0) {
269 nvme_printf(ctrlr, "controller ready did not become %d "
270 "within %d ms\n", desired_val, ctrlr->ready_timeout_in_ms);
271 return (ENXIO);
272 }
273 pause("nvmerdy", 1);
274 }
275
276 return (0);
277 }
278
279 static int
nvme_ctrlr_disable(struct nvme_controller * ctrlr)280 nvme_ctrlr_disable(struct nvme_controller *ctrlr)
281 {
282 uint32_t cc;
283 uint32_t csts;
284 uint8_t en, rdy;
285 int err;
286
287 cc = nvme_mmio_read_4(ctrlr, cc);
288 csts = nvme_mmio_read_4(ctrlr, csts);
289
290 en = (cc >> NVME_CC_REG_EN_SHIFT) & NVME_CC_REG_EN_MASK;
291 rdy = (csts >> NVME_CSTS_REG_RDY_SHIFT) & NVME_CSTS_REG_RDY_MASK;
292
293 /*
294 * Per 3.1.5 in NVME 1.3 spec, transitioning CC.EN from 0 to 1
295 * when CSTS.RDY is 1 or transitioning CC.EN from 1 to 0 when
296 * CSTS.RDY is 0 "has undefined results" So make sure that CSTS.RDY
297 * isn't the desired value. Short circuit if we're already disabled.
298 */
299 if (en == 1) {
300 if (rdy == 0) {
301 /* EN == 1, wait for RDY == 1 or fail */
302 err = nvme_ctrlr_wait_for_ready(ctrlr, 1);
303 if (err != 0)
304 return (err);
305 }
306 } else {
307 /* EN == 0 already wait for RDY == 0 */
308 if (rdy == 0)
309 return (0);
310 else
311 return (nvme_ctrlr_wait_for_ready(ctrlr, 0));
312 }
313
314 cc &= ~NVME_CC_REG_EN_MASK;
315 nvme_mmio_write_4(ctrlr, cc, cc);
316 /*
317 * Some drives have issues with accessing the mmio after we
318 * disable, so delay for a bit after we write the bit to
319 * cope with these issues.
320 */
321 if (ctrlr->quirks & QUIRK_DELAY_B4_CHK_RDY)
322 pause("nvmeR", B4_CHK_RDY_DELAY_MS * hz / 1000);
323 return (nvme_ctrlr_wait_for_ready(ctrlr, 0));
324 }
325
326 static int
nvme_ctrlr_enable(struct nvme_controller * ctrlr)327 nvme_ctrlr_enable(struct nvme_controller *ctrlr)
328 {
329 uint32_t cc;
330 uint32_t csts;
331 uint32_t aqa;
332 uint32_t qsize;
333 uint8_t en, rdy;
334 int err;
335
336 cc = nvme_mmio_read_4(ctrlr, cc);
337 csts = nvme_mmio_read_4(ctrlr, csts);
338
339 en = (cc >> NVME_CC_REG_EN_SHIFT) & NVME_CC_REG_EN_MASK;
340 rdy = (csts >> NVME_CSTS_REG_RDY_SHIFT) & NVME_CSTS_REG_RDY_MASK;
341
342 /*
343 * See note in nvme_ctrlr_disable. Short circuit if we're already enabled.
344 */
345 if (en == 1) {
346 if (rdy == 1)
347 return (0);
348 else
349 return (nvme_ctrlr_wait_for_ready(ctrlr, 1));
350 } else {
351 /* EN == 0 already wait for RDY == 0 or fail */
352 err = nvme_ctrlr_wait_for_ready(ctrlr, 0);
353 if (err != 0)
354 return (err);
355 }
356
357 nvme_mmio_write_8(ctrlr, asq, ctrlr->adminq.cmd_bus_addr);
358 DELAY(5000);
359 nvme_mmio_write_8(ctrlr, acq, ctrlr->adminq.cpl_bus_addr);
360 DELAY(5000);
361
362 /* acqs and asqs are 0-based. */
363 qsize = ctrlr->adminq.num_entries - 1;
364
365 aqa = 0;
366 aqa = (qsize & NVME_AQA_REG_ACQS_MASK) << NVME_AQA_REG_ACQS_SHIFT;
367 aqa |= (qsize & NVME_AQA_REG_ASQS_MASK) << NVME_AQA_REG_ASQS_SHIFT;
368 nvme_mmio_write_4(ctrlr, aqa, aqa);
369 DELAY(5000);
370
371 /* Initialization values for CC */
372 cc = 0;
373 cc |= 1 << NVME_CC_REG_EN_SHIFT;
374 cc |= 0 << NVME_CC_REG_CSS_SHIFT;
375 cc |= 0 << NVME_CC_REG_AMS_SHIFT;
376 cc |= 0 << NVME_CC_REG_SHN_SHIFT;
377 cc |= 6 << NVME_CC_REG_IOSQES_SHIFT; /* SQ entry size == 64 == 2^6 */
378 cc |= 4 << NVME_CC_REG_IOCQES_SHIFT; /* CQ entry size == 16 == 2^4 */
379
380 /* This evaluates to 0, which is according to spec. */
381 cc |= (PAGE_SIZE >> 13) << NVME_CC_REG_MPS_SHIFT;
382
383 nvme_mmio_write_4(ctrlr, cc, cc);
384
385 return (nvme_ctrlr_wait_for_ready(ctrlr, 1));
386 }
387
388 static void
nvme_ctrlr_disable_qpairs(struct nvme_controller * ctrlr)389 nvme_ctrlr_disable_qpairs(struct nvme_controller *ctrlr)
390 {
391 int i;
392
393 nvme_admin_qpair_disable(&ctrlr->adminq);
394 /*
395 * I/O queues are not allocated before the initial HW
396 * reset, so do not try to disable them. Use is_initialized
397 * to determine if this is the initial HW reset.
398 */
399 if (ctrlr->is_initialized) {
400 for (i = 0; i < ctrlr->num_io_queues; i++)
401 nvme_io_qpair_disable(&ctrlr->ioq[i]);
402 }
403 }
404
405 static int
nvme_ctrlr_hw_reset(struct nvme_controller * ctrlr)406 nvme_ctrlr_hw_reset(struct nvme_controller *ctrlr)
407 {
408 int err;
409
410 nvme_ctrlr_disable_qpairs(ctrlr);
411
412 pause("nvmehwreset", hz / 10);
413
414 err = nvme_ctrlr_disable(ctrlr);
415 if (err != 0)
416 return err;
417 return (nvme_ctrlr_enable(ctrlr));
418 }
419
420 void
nvme_ctrlr_reset(struct nvme_controller * ctrlr)421 nvme_ctrlr_reset(struct nvme_controller *ctrlr)
422 {
423 int cmpset;
424
425 cmpset = atomic_cmpset_32(&ctrlr->is_resetting, 0, 1);
426
427 if (cmpset == 0 || ctrlr->is_failed)
428 /*
429 * Controller is already resetting or has failed. Return
430 * immediately since there is no need to kick off another
431 * reset in these cases.
432 */
433 return;
434
435 taskqueue_enqueue(ctrlr->taskqueue, &ctrlr->reset_task);
436 }
437
438 static int
nvme_ctrlr_identify(struct nvme_controller * ctrlr)439 nvme_ctrlr_identify(struct nvme_controller *ctrlr)
440 {
441 struct nvme_completion_poll_status status;
442
443 status.done = 0;
444 nvme_ctrlr_cmd_identify_controller(ctrlr, &ctrlr->cdata,
445 nvme_completion_poll_cb, &status);
446 nvme_completion_poll(&status);
447 if (nvme_completion_is_error(&status.cpl)) {
448 nvme_printf(ctrlr, "nvme_identify_controller failed!\n");
449 return (ENXIO);
450 }
451
452 /* Convert data to host endian */
453 nvme_controller_data_swapbytes(&ctrlr->cdata);
454
455 /*
456 * Use MDTS to ensure our default max_xfer_size doesn't exceed what the
457 * controller supports.
458 */
459 if (ctrlr->cdata.mdts > 0)
460 ctrlr->max_xfer_size = min(ctrlr->max_xfer_size,
461 ctrlr->min_page_size * (1 << (ctrlr->cdata.mdts)));
462
463 return (0);
464 }
465
466 static int
nvme_ctrlr_set_num_qpairs(struct nvme_controller * ctrlr)467 nvme_ctrlr_set_num_qpairs(struct nvme_controller *ctrlr)
468 {
469 struct nvme_completion_poll_status status;
470 int cq_allocated, sq_allocated;
471
472 status.done = 0;
473 nvme_ctrlr_cmd_set_num_queues(ctrlr, ctrlr->num_io_queues,
474 nvme_completion_poll_cb, &status);
475 nvme_completion_poll(&status);
476 if (nvme_completion_is_error(&status.cpl)) {
477 nvme_printf(ctrlr, "nvme_ctrlr_set_num_qpairs failed!\n");
478 return (ENXIO);
479 }
480
481 /*
482 * Data in cdw0 is 0-based.
483 * Lower 16-bits indicate number of submission queues allocated.
484 * Upper 16-bits indicate number of completion queues allocated.
485 */
486 sq_allocated = (status.cpl.cdw0 & 0xFFFF) + 1;
487 cq_allocated = (status.cpl.cdw0 >> 16) + 1;
488
489 /*
490 * Controller may allocate more queues than we requested,
491 * so use the minimum of the number requested and what was
492 * actually allocated.
493 */
494 ctrlr->num_io_queues = min(ctrlr->num_io_queues, sq_allocated);
495 ctrlr->num_io_queues = min(ctrlr->num_io_queues, cq_allocated);
496 if (ctrlr->num_io_queues > vm_ndomains)
497 ctrlr->num_io_queues -= ctrlr->num_io_queues % vm_ndomains;
498
499 return (0);
500 }
501
502 static int
nvme_ctrlr_create_qpairs(struct nvme_controller * ctrlr)503 nvme_ctrlr_create_qpairs(struct nvme_controller *ctrlr)
504 {
505 struct nvme_completion_poll_status status;
506 struct nvme_qpair *qpair;
507 int i;
508
509 for (i = 0; i < ctrlr->num_io_queues; i++) {
510 qpair = &ctrlr->ioq[i];
511
512 status.done = 0;
513 nvme_ctrlr_cmd_create_io_cq(ctrlr, qpair,
514 nvme_completion_poll_cb, &status);
515 nvme_completion_poll(&status);
516 if (nvme_completion_is_error(&status.cpl)) {
517 nvme_printf(ctrlr, "nvme_create_io_cq failed!\n");
518 return (ENXIO);
519 }
520
521 status.done = 0;
522 nvme_ctrlr_cmd_create_io_sq(ctrlr, qpair,
523 nvme_completion_poll_cb, &status);
524 nvme_completion_poll(&status);
525 if (nvme_completion_is_error(&status.cpl)) {
526 nvme_printf(ctrlr, "nvme_create_io_sq failed!\n");
527 return (ENXIO);
528 }
529 }
530
531 return (0);
532 }
533
534 static int
nvme_ctrlr_delete_qpairs(struct nvme_controller * ctrlr)535 nvme_ctrlr_delete_qpairs(struct nvme_controller *ctrlr)
536 {
537 struct nvme_completion_poll_status status;
538 struct nvme_qpair *qpair;
539
540 for (int i = 0; i < ctrlr->num_io_queues; i++) {
541 qpair = &ctrlr->ioq[i];
542
543 status.done = 0;
544 nvme_ctrlr_cmd_delete_io_sq(ctrlr, qpair,
545 nvme_completion_poll_cb, &status);
546 nvme_completion_poll(&status);
547 if (nvme_completion_is_error(&status.cpl)) {
548 nvme_printf(ctrlr, "nvme_destroy_io_sq failed!\n");
549 return (ENXIO);
550 }
551
552 status.done = 0;
553 nvme_ctrlr_cmd_delete_io_cq(ctrlr, qpair,
554 nvme_completion_poll_cb, &status);
555 nvme_completion_poll(&status);
556 if (nvme_completion_is_error(&status.cpl)) {
557 nvme_printf(ctrlr, "nvme_destroy_io_cq failed!\n");
558 return (ENXIO);
559 }
560 }
561
562 return (0);
563 }
564
565 static int
nvme_ctrlr_construct_namespaces(struct nvme_controller * ctrlr)566 nvme_ctrlr_construct_namespaces(struct nvme_controller *ctrlr)
567 {
568 struct nvme_namespace *ns;
569 uint32_t i;
570
571 for (i = 0; i < min(ctrlr->cdata.nn, NVME_MAX_NAMESPACES); i++) {
572 ns = &ctrlr->ns[i];
573 nvme_ns_construct(ns, i+1, ctrlr);
574 }
575
576 return (0);
577 }
578
579 static bool
is_log_page_id_valid(uint8_t page_id)580 is_log_page_id_valid(uint8_t page_id)
581 {
582
583 switch (page_id) {
584 case NVME_LOG_ERROR:
585 case NVME_LOG_HEALTH_INFORMATION:
586 case NVME_LOG_FIRMWARE_SLOT:
587 case NVME_LOG_CHANGED_NAMESPACE:
588 case NVME_LOG_COMMAND_EFFECT:
589 case NVME_LOG_RES_NOTIFICATION:
590 case NVME_LOG_SANITIZE_STATUS:
591 return (true);
592 }
593
594 return (false);
595 }
596
597 static uint32_t
nvme_ctrlr_get_log_page_size(struct nvme_controller * ctrlr,uint8_t page_id)598 nvme_ctrlr_get_log_page_size(struct nvme_controller *ctrlr, uint8_t page_id)
599 {
600 uint32_t log_page_size;
601
602 switch (page_id) {
603 case NVME_LOG_ERROR:
604 log_page_size = min(
605 sizeof(struct nvme_error_information_entry) *
606 (ctrlr->cdata.elpe + 1), NVME_MAX_AER_LOG_SIZE);
607 break;
608 case NVME_LOG_HEALTH_INFORMATION:
609 log_page_size = sizeof(struct nvme_health_information_page);
610 break;
611 case NVME_LOG_FIRMWARE_SLOT:
612 log_page_size = sizeof(struct nvme_firmware_page);
613 break;
614 case NVME_LOG_CHANGED_NAMESPACE:
615 log_page_size = sizeof(struct nvme_ns_list);
616 break;
617 case NVME_LOG_COMMAND_EFFECT:
618 log_page_size = sizeof(struct nvme_command_effects_page);
619 break;
620 case NVME_LOG_RES_NOTIFICATION:
621 log_page_size = sizeof(struct nvme_res_notification_page);
622 break;
623 case NVME_LOG_SANITIZE_STATUS:
624 log_page_size = sizeof(struct nvme_sanitize_status_page);
625 break;
626 default:
627 log_page_size = 0;
628 break;
629 }
630
631 return (log_page_size);
632 }
633
634 static void
nvme_ctrlr_log_critical_warnings(struct nvme_controller * ctrlr,uint8_t state)635 nvme_ctrlr_log_critical_warnings(struct nvme_controller *ctrlr,
636 uint8_t state)
637 {
638
639 if (state & NVME_CRIT_WARN_ST_AVAILABLE_SPARE)
640 nvme_ctrlr_devctl_log(ctrlr, "critical",
641 "available spare space below threshold");
642
643 if (state & NVME_CRIT_WARN_ST_TEMPERATURE)
644 nvme_ctrlr_devctl_log(ctrlr, "critical",
645 "temperature above threshold");
646
647 if (state & NVME_CRIT_WARN_ST_DEVICE_RELIABILITY)
648 nvme_ctrlr_devctl_log(ctrlr, "critical",
649 "device reliability degraded");
650
651 if (state & NVME_CRIT_WARN_ST_READ_ONLY)
652 nvme_ctrlr_devctl_log(ctrlr, "critical",
653 "media placed in read only mode");
654
655 if (state & NVME_CRIT_WARN_ST_VOLATILE_MEMORY_BACKUP)
656 nvme_ctrlr_devctl_log(ctrlr, "critical",
657 "volatile memory backup device failed");
658
659 if (state & NVME_CRIT_WARN_ST_RESERVED_MASK)
660 nvme_ctrlr_devctl_log(ctrlr, "critical",
661 "unknown critical warning(s): state = 0x%02x", state);
662 }
663
664 static void
nvme_ctrlr_async_event_log_page_cb(void * arg,const struct nvme_completion * cpl)665 nvme_ctrlr_async_event_log_page_cb(void *arg, const struct nvme_completion *cpl)
666 {
667 struct nvme_async_event_request *aer = arg;
668 struct nvme_health_information_page *health_info;
669 struct nvme_ns_list *nsl;
670 struct nvme_error_information_entry *err;
671 int i;
672
673 /*
674 * If the log page fetch for some reason completed with an error,
675 * don't pass log page data to the consumers. In practice, this case
676 * should never happen.
677 */
678 if (nvme_completion_is_error(cpl))
679 nvme_notify_async_consumers(aer->ctrlr, &aer->cpl,
680 aer->log_page_id, NULL, 0);
681 else {
682 /* Convert data to host endian */
683 switch (aer->log_page_id) {
684 case NVME_LOG_ERROR:
685 err = (struct nvme_error_information_entry *)aer->log_page_buffer;
686 for (i = 0; i < (aer->ctrlr->cdata.elpe + 1); i++)
687 nvme_error_information_entry_swapbytes(err++);
688 break;
689 case NVME_LOG_HEALTH_INFORMATION:
690 nvme_health_information_page_swapbytes(
691 (struct nvme_health_information_page *)aer->log_page_buffer);
692 break;
693 case NVME_LOG_FIRMWARE_SLOT:
694 nvme_firmware_page_swapbytes(
695 (struct nvme_firmware_page *)aer->log_page_buffer);
696 break;
697 case NVME_LOG_CHANGED_NAMESPACE:
698 nvme_ns_list_swapbytes(
699 (struct nvme_ns_list *)aer->log_page_buffer);
700 break;
701 case NVME_LOG_COMMAND_EFFECT:
702 nvme_command_effects_page_swapbytes(
703 (struct nvme_command_effects_page *)aer->log_page_buffer);
704 break;
705 case NVME_LOG_RES_NOTIFICATION:
706 nvme_res_notification_page_swapbytes(
707 (struct nvme_res_notification_page *)aer->log_page_buffer);
708 break;
709 case NVME_LOG_SANITIZE_STATUS:
710 nvme_sanitize_status_page_swapbytes(
711 (struct nvme_sanitize_status_page *)aer->log_page_buffer);
712 break;
713 case INTEL_LOG_TEMP_STATS:
714 intel_log_temp_stats_swapbytes(
715 (struct intel_log_temp_stats *)aer->log_page_buffer);
716 break;
717 default:
718 break;
719 }
720
721 if (aer->log_page_id == NVME_LOG_HEALTH_INFORMATION) {
722 health_info = (struct nvme_health_information_page *)
723 aer->log_page_buffer;
724 nvme_ctrlr_log_critical_warnings(aer->ctrlr,
725 health_info->critical_warning);
726 /*
727 * Critical warnings reported through the
728 * SMART/health log page are persistent, so
729 * clear the associated bits in the async event
730 * config so that we do not receive repeated
731 * notifications for the same event.
732 */
733 aer->ctrlr->async_event_config &=
734 ~health_info->critical_warning;
735 nvme_ctrlr_cmd_set_async_event_config(aer->ctrlr,
736 aer->ctrlr->async_event_config, NULL, NULL);
737 } else if (aer->log_page_id == NVME_LOG_CHANGED_NAMESPACE &&
738 !nvme_use_nvd) {
739 nsl = (struct nvme_ns_list *)aer->log_page_buffer;
740 for (i = 0; i < nitems(nsl->ns) && nsl->ns[i] != 0; i++) {
741 if (nsl->ns[i] > NVME_MAX_NAMESPACES)
742 break;
743 nvme_notify_ns(aer->ctrlr, nsl->ns[i]);
744 }
745 }
746
747
748 /*
749 * Pass the cpl data from the original async event completion,
750 * not the log page fetch.
751 */
752 nvme_notify_async_consumers(aer->ctrlr, &aer->cpl,
753 aer->log_page_id, aer->log_page_buffer, aer->log_page_size);
754 }
755
756 /*
757 * Repost another asynchronous event request to replace the one
758 * that just completed.
759 */
760 nvme_ctrlr_construct_and_submit_aer(aer->ctrlr, aer);
761 }
762
763 static void
nvme_ctrlr_async_event_cb(void * arg,const struct nvme_completion * cpl)764 nvme_ctrlr_async_event_cb(void *arg, const struct nvme_completion *cpl)
765 {
766 struct nvme_async_event_request *aer = arg;
767
768 if (nvme_completion_is_error(cpl)) {
769 /*
770 * Do not retry failed async event requests. This avoids
771 * infinite loops where a new async event request is submitted
772 * to replace the one just failed, only to fail again and
773 * perpetuate the loop.
774 */
775 return;
776 }
777
778 /* Associated log page is in bits 23:16 of completion entry dw0. */
779 aer->log_page_id = (cpl->cdw0 & 0xFF0000) >> 16;
780
781 nvme_printf(aer->ctrlr, "async event occurred (type 0x%x, info 0x%02x,"
782 " page 0x%02x)\n", (cpl->cdw0 & 0x07), (cpl->cdw0 & 0xFF00) >> 8,
783 aer->log_page_id);
784
785 if (is_log_page_id_valid(aer->log_page_id)) {
786 aer->log_page_size = nvme_ctrlr_get_log_page_size(aer->ctrlr,
787 aer->log_page_id);
788 memcpy(&aer->cpl, cpl, sizeof(*cpl));
789 nvme_ctrlr_cmd_get_log_page(aer->ctrlr, aer->log_page_id,
790 NVME_GLOBAL_NAMESPACE_TAG, aer->log_page_buffer,
791 aer->log_page_size, nvme_ctrlr_async_event_log_page_cb,
792 aer);
793 /* Wait to notify consumers until after log page is fetched. */
794 } else {
795 nvme_notify_async_consumers(aer->ctrlr, cpl, aer->log_page_id,
796 NULL, 0);
797
798 /*
799 * Repost another asynchronous event request to replace the one
800 * that just completed.
801 */
802 nvme_ctrlr_construct_and_submit_aer(aer->ctrlr, aer);
803 }
804 }
805
806 static void
nvme_ctrlr_construct_and_submit_aer(struct nvme_controller * ctrlr,struct nvme_async_event_request * aer)807 nvme_ctrlr_construct_and_submit_aer(struct nvme_controller *ctrlr,
808 struct nvme_async_event_request *aer)
809 {
810 struct nvme_request *req;
811
812 aer->ctrlr = ctrlr;
813 req = nvme_allocate_request_null(nvme_ctrlr_async_event_cb, aer);
814 aer->req = req;
815
816 /*
817 * Disable timeout here, since asynchronous event requests should by
818 * nature never be timed out.
819 */
820 req->timeout = false;
821 req->cmd.opc = NVME_OPC_ASYNC_EVENT_REQUEST;
822 nvme_ctrlr_submit_admin_request(ctrlr, req);
823 }
824
825 static void
nvme_ctrlr_configure_aer(struct nvme_controller * ctrlr)826 nvme_ctrlr_configure_aer(struct nvme_controller *ctrlr)
827 {
828 struct nvme_completion_poll_status status;
829 struct nvme_async_event_request *aer;
830 uint32_t i;
831
832 ctrlr->async_event_config = NVME_CRIT_WARN_ST_AVAILABLE_SPARE |
833 NVME_CRIT_WARN_ST_DEVICE_RELIABILITY |
834 NVME_CRIT_WARN_ST_READ_ONLY |
835 NVME_CRIT_WARN_ST_VOLATILE_MEMORY_BACKUP;
836 if (ctrlr->cdata.ver >= NVME_REV(1, 2))
837 ctrlr->async_event_config |= NVME_ASYNC_EVENT_NS_ATTRIBUTE |
838 NVME_ASYNC_EVENT_FW_ACTIVATE;
839
840 status.done = 0;
841 nvme_ctrlr_cmd_get_feature(ctrlr, NVME_FEAT_TEMPERATURE_THRESHOLD,
842 0, NULL, 0, nvme_completion_poll_cb, &status);
843 nvme_completion_poll(&status);
844 if (nvme_completion_is_error(&status.cpl) ||
845 (status.cpl.cdw0 & 0xFFFF) == 0xFFFF ||
846 (status.cpl.cdw0 & 0xFFFF) == 0x0000) {
847 nvme_printf(ctrlr, "temperature threshold not supported\n");
848 } else
849 ctrlr->async_event_config |= NVME_CRIT_WARN_ST_TEMPERATURE;
850
851 nvme_ctrlr_cmd_set_async_event_config(ctrlr,
852 ctrlr->async_event_config, NULL, NULL);
853
854 /* aerl is a zero-based value, so we need to add 1 here. */
855 ctrlr->num_aers = min(NVME_MAX_ASYNC_EVENTS, (ctrlr->cdata.aerl+1));
856
857 for (i = 0; i < ctrlr->num_aers; i++) {
858 aer = &ctrlr->aer[i];
859 nvme_ctrlr_construct_and_submit_aer(ctrlr, aer);
860 }
861 }
862
863 static void
nvme_ctrlr_configure_int_coalescing(struct nvme_controller * ctrlr)864 nvme_ctrlr_configure_int_coalescing(struct nvme_controller *ctrlr)
865 {
866
867 ctrlr->int_coal_time = 0;
868 TUNABLE_INT_FETCH("hw.nvme.int_coal_time",
869 &ctrlr->int_coal_time);
870
871 ctrlr->int_coal_threshold = 0;
872 TUNABLE_INT_FETCH("hw.nvme.int_coal_threshold",
873 &ctrlr->int_coal_threshold);
874
875 nvme_ctrlr_cmd_set_interrupt_coalescing(ctrlr, ctrlr->int_coal_time,
876 ctrlr->int_coal_threshold, NULL, NULL);
877 }
878
879 static void
nvme_ctrlr_hmb_free(struct nvme_controller * ctrlr)880 nvme_ctrlr_hmb_free(struct nvme_controller *ctrlr)
881 {
882 struct nvme_hmb_chunk *hmbc;
883 int i;
884
885 if (ctrlr->hmb_desc_paddr) {
886 bus_dmamap_unload(ctrlr->hmb_desc_tag, ctrlr->hmb_desc_map);
887 bus_dmamem_free(ctrlr->hmb_desc_tag, ctrlr->hmb_desc_vaddr,
888 ctrlr->hmb_desc_map);
889 ctrlr->hmb_desc_paddr = 0;
890 }
891 if (ctrlr->hmb_desc_tag) {
892 bus_dma_tag_destroy(ctrlr->hmb_desc_tag);
893 ctrlr->hmb_desc_tag = NULL;
894 }
895 for (i = 0; i < ctrlr->hmb_nchunks; i++) {
896 hmbc = &ctrlr->hmb_chunks[i];
897 bus_dmamap_unload(ctrlr->hmb_tag, hmbc->hmbc_map);
898 bus_dmamem_free(ctrlr->hmb_tag, hmbc->hmbc_vaddr,
899 hmbc->hmbc_map);
900 }
901 ctrlr->hmb_nchunks = 0;
902 if (ctrlr->hmb_tag) {
903 bus_dma_tag_destroy(ctrlr->hmb_tag);
904 ctrlr->hmb_tag = NULL;
905 }
906 if (ctrlr->hmb_chunks) {
907 free(ctrlr->hmb_chunks, M_NVME);
908 ctrlr->hmb_chunks = NULL;
909 }
910 }
911
912 static void
nvme_ctrlr_hmb_alloc(struct nvme_controller * ctrlr)913 nvme_ctrlr_hmb_alloc(struct nvme_controller *ctrlr)
914 {
915 struct nvme_hmb_chunk *hmbc;
916 size_t pref, min, minc, size;
917 int err, i;
918 uint64_t max;
919
920 /* Limit HMB to 5% of RAM size per device by default. */
921 max = (uint64_t)physmem * PAGE_SIZE / 20;
922 TUNABLE_UINT64_FETCH("hw.nvme.hmb_max", &max);
923
924 min = (long long unsigned)ctrlr->cdata.hmmin * 4096;
925 if (max == 0 || max < min)
926 return;
927 pref = MIN((long long unsigned)ctrlr->cdata.hmpre * 4096, max);
928 minc = MAX(ctrlr->cdata.hmminds * 4096, PAGE_SIZE);
929 if (min > 0 && ctrlr->cdata.hmmaxd > 0)
930 minc = MAX(minc, min / ctrlr->cdata.hmmaxd);
931 ctrlr->hmb_chunk = pref;
932
933 again:
934 ctrlr->hmb_chunk = roundup2(ctrlr->hmb_chunk, PAGE_SIZE);
935 ctrlr->hmb_nchunks = howmany(pref, ctrlr->hmb_chunk);
936 if (ctrlr->cdata.hmmaxd > 0 && ctrlr->hmb_nchunks > ctrlr->cdata.hmmaxd)
937 ctrlr->hmb_nchunks = ctrlr->cdata.hmmaxd;
938 ctrlr->hmb_chunks = malloc(sizeof(struct nvme_hmb_chunk) *
939 ctrlr->hmb_nchunks, M_NVME, M_WAITOK);
940 err = bus_dma_tag_create(bus_get_dma_tag(ctrlr->dev),
941 PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
942 ctrlr->hmb_chunk, 1, ctrlr->hmb_chunk, 0, NULL, NULL, &ctrlr->hmb_tag);
943 if (err != 0) {
944 nvme_printf(ctrlr, "HMB tag create failed %d\n", err);
945 nvme_ctrlr_hmb_free(ctrlr);
946 return;
947 }
948
949 for (i = 0; i < ctrlr->hmb_nchunks; i++) {
950 hmbc = &ctrlr->hmb_chunks[i];
951 if (bus_dmamem_alloc(ctrlr->hmb_tag,
952 (void **)&hmbc->hmbc_vaddr, BUS_DMA_NOWAIT,
953 &hmbc->hmbc_map)) {
954 nvme_printf(ctrlr, "failed to alloc HMB\n");
955 break;
956 }
957 if (bus_dmamap_load(ctrlr->hmb_tag, hmbc->hmbc_map,
958 hmbc->hmbc_vaddr, ctrlr->hmb_chunk, nvme_single_map,
959 &hmbc->hmbc_paddr, BUS_DMA_NOWAIT) != 0) {
960 bus_dmamem_free(ctrlr->hmb_tag, hmbc->hmbc_vaddr,
961 hmbc->hmbc_map);
962 nvme_printf(ctrlr, "failed to load HMB\n");
963 break;
964 }
965 bus_dmamap_sync(ctrlr->hmb_tag, hmbc->hmbc_map,
966 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
967 }
968
969 if (i < ctrlr->hmb_nchunks && i * ctrlr->hmb_chunk < min &&
970 ctrlr->hmb_chunk / 2 >= minc) {
971 ctrlr->hmb_nchunks = i;
972 nvme_ctrlr_hmb_free(ctrlr);
973 ctrlr->hmb_chunk /= 2;
974 goto again;
975 }
976 ctrlr->hmb_nchunks = i;
977 if (ctrlr->hmb_nchunks * ctrlr->hmb_chunk < min) {
978 nvme_ctrlr_hmb_free(ctrlr);
979 return;
980 }
981
982 size = sizeof(struct nvme_hmb_desc) * ctrlr->hmb_nchunks;
983 err = bus_dma_tag_create(bus_get_dma_tag(ctrlr->dev),
984 16, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
985 size, 1, size, 0, NULL, NULL, &ctrlr->hmb_desc_tag);
986 if (err != 0) {
987 nvme_printf(ctrlr, "HMB desc tag create failed %d\n", err);
988 nvme_ctrlr_hmb_free(ctrlr);
989 return;
990 }
991 if (bus_dmamem_alloc(ctrlr->hmb_desc_tag,
992 (void **)&ctrlr->hmb_desc_vaddr, BUS_DMA_WAITOK,
993 &ctrlr->hmb_desc_map)) {
994 nvme_printf(ctrlr, "failed to alloc HMB desc\n");
995 nvme_ctrlr_hmb_free(ctrlr);
996 return;
997 }
998 if (bus_dmamap_load(ctrlr->hmb_desc_tag, ctrlr->hmb_desc_map,
999 ctrlr->hmb_desc_vaddr, size, nvme_single_map,
1000 &ctrlr->hmb_desc_paddr, BUS_DMA_NOWAIT) != 0) {
1001 bus_dmamem_free(ctrlr->hmb_desc_tag, ctrlr->hmb_desc_vaddr,
1002 ctrlr->hmb_desc_map);
1003 nvme_printf(ctrlr, "failed to load HMB desc\n");
1004 nvme_ctrlr_hmb_free(ctrlr);
1005 return;
1006 }
1007
1008 for (i = 0; i < ctrlr->hmb_nchunks; i++) {
1009 ctrlr->hmb_desc_vaddr[i].addr =
1010 htole64(ctrlr->hmb_chunks[i].hmbc_paddr);
1011 ctrlr->hmb_desc_vaddr[i].size = htole32(ctrlr->hmb_chunk / 4096);
1012 }
1013 bus_dmamap_sync(ctrlr->hmb_desc_tag, ctrlr->hmb_desc_map,
1014 BUS_DMASYNC_PREWRITE);
1015
1016 nvme_printf(ctrlr, "Allocated %lluMB host memory buffer\n",
1017 (long long unsigned)ctrlr->hmb_nchunks * ctrlr->hmb_chunk
1018 / 1024 / 1024);
1019 }
1020
1021 static void
nvme_ctrlr_hmb_enable(struct nvme_controller * ctrlr,bool enable,bool memret)1022 nvme_ctrlr_hmb_enable(struct nvme_controller *ctrlr, bool enable, bool memret)
1023 {
1024 struct nvme_completion_poll_status status;
1025 uint32_t cdw11;
1026
1027 cdw11 = 0;
1028 if (enable)
1029 cdw11 |= 1;
1030 if (memret)
1031 cdw11 |= 2;
1032 status.done = 0;
1033 nvme_ctrlr_cmd_set_feature(ctrlr, NVME_FEAT_HOST_MEMORY_BUFFER, cdw11,
1034 ctrlr->hmb_nchunks * ctrlr->hmb_chunk / 4096, ctrlr->hmb_desc_paddr,
1035 ctrlr->hmb_desc_paddr >> 32, ctrlr->hmb_nchunks, NULL, 0,
1036 nvme_completion_poll_cb, &status);
1037 nvme_completion_poll(&status);
1038 if (nvme_completion_is_error(&status.cpl))
1039 nvme_printf(ctrlr, "nvme_ctrlr_hmb_enable failed!\n");
1040 }
1041
1042 static void
nvme_ctrlr_start(void * ctrlr_arg,bool resetting)1043 nvme_ctrlr_start(void *ctrlr_arg, bool resetting)
1044 {
1045 struct nvme_controller *ctrlr = ctrlr_arg;
1046 uint32_t old_num_io_queues;
1047 int i;
1048
1049 /*
1050 * Only reset adminq here when we are restarting the
1051 * controller after a reset. During initialization,
1052 * we have already submitted admin commands to get
1053 * the number of I/O queues supported, so cannot reset
1054 * the adminq again here.
1055 */
1056 if (resetting) {
1057 nvme_qpair_reset(&ctrlr->adminq);
1058 nvme_admin_qpair_enable(&ctrlr->adminq);
1059 }
1060
1061 if (ctrlr->ioq != NULL) {
1062 for (i = 0; i < ctrlr->num_io_queues; i++)
1063 nvme_qpair_reset(&ctrlr->ioq[i]);
1064 }
1065
1066 /*
1067 * If it was a reset on initialization command timeout, just
1068 * return here, letting initialization code fail gracefully.
1069 */
1070 if (resetting && !ctrlr->is_initialized)
1071 return;
1072
1073 if (resetting && nvme_ctrlr_identify(ctrlr) != 0) {
1074 nvme_ctrlr_fail(ctrlr);
1075 return;
1076 }
1077
1078 /*
1079 * The number of qpairs are determined during controller initialization,
1080 * including using NVMe SET_FEATURES/NUMBER_OF_QUEUES to determine the
1081 * HW limit. We call SET_FEATURES again here so that it gets called
1082 * after any reset for controllers that depend on the driver to
1083 * explicit specify how many queues it will use. This value should
1084 * never change between resets, so panic if somehow that does happen.
1085 */
1086 if (resetting) {
1087 old_num_io_queues = ctrlr->num_io_queues;
1088 if (nvme_ctrlr_set_num_qpairs(ctrlr) != 0) {
1089 nvme_ctrlr_fail(ctrlr);
1090 return;
1091 }
1092
1093 if (old_num_io_queues != ctrlr->num_io_queues) {
1094 panic("num_io_queues changed from %u to %u",
1095 old_num_io_queues, ctrlr->num_io_queues);
1096 }
1097 }
1098
1099 if (ctrlr->cdata.hmpre > 0 && ctrlr->hmb_nchunks == 0) {
1100 nvme_ctrlr_hmb_alloc(ctrlr);
1101 if (ctrlr->hmb_nchunks > 0)
1102 nvme_ctrlr_hmb_enable(ctrlr, true, false);
1103 } else if (ctrlr->hmb_nchunks > 0)
1104 nvme_ctrlr_hmb_enable(ctrlr, true, true);
1105
1106 if (nvme_ctrlr_create_qpairs(ctrlr) != 0) {
1107 nvme_ctrlr_fail(ctrlr);
1108 return;
1109 }
1110
1111 if (nvme_ctrlr_construct_namespaces(ctrlr) != 0) {
1112 nvme_ctrlr_fail(ctrlr);
1113 return;
1114 }
1115
1116 nvme_ctrlr_configure_aer(ctrlr);
1117 nvme_ctrlr_configure_int_coalescing(ctrlr);
1118
1119 for (i = 0; i < ctrlr->num_io_queues; i++)
1120 nvme_io_qpair_enable(&ctrlr->ioq[i]);
1121 }
1122
1123 void
nvme_ctrlr_start_config_hook(void * arg)1124 nvme_ctrlr_start_config_hook(void *arg)
1125 {
1126 struct nvme_controller *ctrlr = arg;
1127
1128 /*
1129 * Reset controller twice to ensure we do a transition from cc.en==1 to
1130 * cc.en==0. This is because we don't really know what status the
1131 * controller was left in when boot handed off to OS. Linux doesn't do
1132 * this, however. If we adopt that policy, see also nvme_ctrlr_resume().
1133 */
1134 if (nvme_ctrlr_hw_reset(ctrlr) != 0) {
1135 fail:
1136 nvme_ctrlr_fail(ctrlr);
1137 config_intrhook_disestablish(&ctrlr->config_hook);
1138 return;
1139 }
1140
1141 if (nvme_ctrlr_hw_reset(ctrlr) != 0)
1142 goto fail;
1143
1144 nvme_qpair_reset(&ctrlr->adminq);
1145 nvme_admin_qpair_enable(&ctrlr->adminq);
1146
1147 if (nvme_ctrlr_identify(ctrlr) == 0 &&
1148 nvme_ctrlr_set_num_qpairs(ctrlr) == 0 &&
1149 nvme_ctrlr_construct_io_qpairs(ctrlr) == 0)
1150 nvme_ctrlr_start(ctrlr, false);
1151 else
1152 goto fail;
1153
1154 nvme_sysctl_initialize_ctrlr(ctrlr);
1155 config_intrhook_disestablish(&ctrlr->config_hook);
1156
1157 ctrlr->is_initialized = 1;
1158 nvme_notify_new_controller(ctrlr);
1159 }
1160
1161 static void
nvme_ctrlr_reset_task(void * arg,int pending)1162 nvme_ctrlr_reset_task(void *arg, int pending)
1163 {
1164 struct nvme_controller *ctrlr = arg;
1165 int status;
1166
1167 nvme_ctrlr_devctl_log(ctrlr, "RESET", "resetting controller");
1168 status = nvme_ctrlr_hw_reset(ctrlr);
1169 /*
1170 * Use pause instead of DELAY, so that we yield to any nvme interrupt
1171 * handlers on this CPU that were blocked on a qpair lock. We want
1172 * all nvme interrupts completed before proceeding with restarting the
1173 * controller.
1174 *
1175 * XXX - any way to guarantee the interrupt handlers have quiesced?
1176 */
1177 pause("nvmereset", hz / 10);
1178 if (status == 0)
1179 nvme_ctrlr_start(ctrlr, true);
1180 else
1181 nvme_ctrlr_fail(ctrlr);
1182
1183 atomic_cmpset_32(&ctrlr->is_resetting, 1, 0);
1184 }
1185
1186 /*
1187 * Poll all the queues enabled on the device for completion.
1188 */
1189 void
nvme_ctrlr_poll(struct nvme_controller * ctrlr)1190 nvme_ctrlr_poll(struct nvme_controller *ctrlr)
1191 {
1192 int i;
1193
1194 nvme_qpair_process_completions(&ctrlr->adminq);
1195
1196 for (i = 0; i < ctrlr->num_io_queues; i++)
1197 if (ctrlr->ioq && ctrlr->ioq[i].cpl)
1198 nvme_qpair_process_completions(&ctrlr->ioq[i]);
1199 }
1200
1201 /*
1202 * Poll the single-vector interrupt case: num_io_queues will be 1 and
1203 * there's only a single vector. While we're polling, we mask further
1204 * interrupts in the controller.
1205 */
1206 void
nvme_ctrlr_shared_handler(void * arg)1207 nvme_ctrlr_shared_handler(void *arg)
1208 {
1209 struct nvme_controller *ctrlr = arg;
1210
1211 nvme_mmio_write_4(ctrlr, intms, 1);
1212 nvme_ctrlr_poll(ctrlr);
1213 nvme_mmio_write_4(ctrlr, intmc, 1);
1214 }
1215
1216 static void
nvme_pt_done(void * arg,const struct nvme_completion * cpl)1217 nvme_pt_done(void *arg, const struct nvme_completion *cpl)
1218 {
1219 struct nvme_pt_command *pt = arg;
1220 struct mtx *mtx = pt->driver_lock;
1221 uint16_t status;
1222
1223 bzero(&pt->cpl, sizeof(pt->cpl));
1224 pt->cpl.cdw0 = cpl->cdw0;
1225
1226 status = cpl->status;
1227 status &= ~NVME_STATUS_P_MASK;
1228 pt->cpl.status = status;
1229
1230 mtx_lock(mtx);
1231 pt->driver_lock = NULL;
1232 wakeup(pt);
1233 mtx_unlock(mtx);
1234 }
1235
1236 int
nvme_ctrlr_passthrough_cmd(struct nvme_controller * ctrlr,struct nvme_pt_command * pt,uint32_t nsid,int is_user_buffer,int is_admin_cmd)1237 nvme_ctrlr_passthrough_cmd(struct nvme_controller *ctrlr,
1238 struct nvme_pt_command *pt, uint32_t nsid, int is_user_buffer,
1239 int is_admin_cmd)
1240 {
1241 struct nvme_request *req;
1242 struct mtx *mtx;
1243 struct buf *buf = NULL;
1244 int ret = 0;
1245 vm_offset_t addr, end;
1246
1247 if (pt->len > 0) {
1248 /*
1249 * vmapbuf calls vm_fault_quick_hold_pages which only maps full
1250 * pages. Ensure this request has fewer than MAXPHYS bytes when
1251 * extended to full pages.
1252 */
1253 addr = (vm_offset_t)pt->buf;
1254 end = round_page(addr + pt->len);
1255 addr = trunc_page(addr);
1256 if (end - addr > MAXPHYS)
1257 return EIO;
1258
1259 if (pt->len > ctrlr->max_xfer_size) {
1260 nvme_printf(ctrlr, "pt->len (%d) "
1261 "exceeds max_xfer_size (%d)\n", pt->len,
1262 ctrlr->max_xfer_size);
1263 return EIO;
1264 }
1265 if (is_user_buffer) {
1266 /*
1267 * Ensure the user buffer is wired for the duration of
1268 * this pass-through command.
1269 */
1270 PHOLD(curproc);
1271 buf = getpbuf(NULL);
1272 buf->b_iocmd = pt->is_read ? BIO_READ : BIO_WRITE;
1273 if (vmapbuf(buf, pt->buf, pt->len, 1) < 0) {
1274 ret = EFAULT;
1275 goto err;
1276 }
1277 req = nvme_allocate_request_vaddr(buf->b_data, pt->len,
1278 nvme_pt_done, pt);
1279 } else
1280 req = nvme_allocate_request_vaddr(pt->buf, pt->len,
1281 nvme_pt_done, pt);
1282 } else
1283 req = nvme_allocate_request_null(nvme_pt_done, pt);
1284
1285 /* Assume user space already converted to little-endian */
1286 req->cmd.opc = pt->cmd.opc;
1287 req->cmd.fuse = pt->cmd.fuse;
1288 req->cmd.rsvd2 = pt->cmd.rsvd2;
1289 req->cmd.rsvd3 = pt->cmd.rsvd3;
1290 req->cmd.cdw10 = pt->cmd.cdw10;
1291 req->cmd.cdw11 = pt->cmd.cdw11;
1292 req->cmd.cdw12 = pt->cmd.cdw12;
1293 req->cmd.cdw13 = pt->cmd.cdw13;
1294 req->cmd.cdw14 = pt->cmd.cdw14;
1295 req->cmd.cdw15 = pt->cmd.cdw15;
1296
1297 req->cmd.nsid = htole32(nsid);
1298
1299 mtx = mtx_pool_find(mtxpool_sleep, pt);
1300 pt->driver_lock = mtx;
1301
1302 if (is_admin_cmd)
1303 nvme_ctrlr_submit_admin_request(ctrlr, req);
1304 else
1305 nvme_ctrlr_submit_io_request(ctrlr, req);
1306
1307 mtx_lock(mtx);
1308 while (pt->driver_lock != NULL)
1309 mtx_sleep(pt, mtx, PRIBIO, "nvme_pt", 0);
1310 mtx_unlock(mtx);
1311
1312 err:
1313 if (buf != NULL) {
1314 relpbuf(buf, NULL);
1315 PRELE(curproc);
1316 }
1317
1318 return (ret);
1319 }
1320
1321 static int
nvme_ctrlr_ioctl(struct cdev * cdev,u_long cmd,caddr_t arg,int flag,struct thread * td)1322 nvme_ctrlr_ioctl(struct cdev *cdev, u_long cmd, caddr_t arg, int flag,
1323 struct thread *td)
1324 {
1325 struct nvme_controller *ctrlr;
1326 struct nvme_pt_command *pt;
1327
1328 ctrlr = cdev->si_drv1;
1329
1330 switch (cmd) {
1331 case NVME_RESET_CONTROLLER:
1332 nvme_ctrlr_reset(ctrlr);
1333 break;
1334 case NVME_PASSTHROUGH_CMD:
1335 pt = (struct nvme_pt_command *)arg;
1336 return (nvme_ctrlr_passthrough_cmd(ctrlr, pt, le32toh(pt->cmd.nsid),
1337 1 /* is_user_buffer */, 1 /* is_admin_cmd */));
1338 case NVME_GET_NSID:
1339 {
1340 struct nvme_get_nsid *gnsid = (struct nvme_get_nsid *)arg;
1341 strncpy(gnsid->cdev, device_get_nameunit(ctrlr->dev),
1342 sizeof(gnsid->cdev));
1343 gnsid->cdev[sizeof(gnsid->cdev) - 1] = '\0';
1344 gnsid->nsid = 0;
1345 break;
1346 }
1347 case NVME_GET_MAX_XFER_SIZE:
1348 *(uint64_t *)arg = ctrlr->max_xfer_size;
1349 break;
1350 default:
1351 return (ENOTTY);
1352 }
1353
1354 return (0);
1355 }
1356
1357 static struct cdevsw nvme_ctrlr_cdevsw = {
1358 .d_version = D_VERSION,
1359 .d_flags = 0,
1360 .d_ioctl = nvme_ctrlr_ioctl
1361 };
1362
1363 int
nvme_ctrlr_construct(struct nvme_controller * ctrlr,device_t dev)1364 nvme_ctrlr_construct(struct nvme_controller *ctrlr, device_t dev)
1365 {
1366 struct make_dev_args md_args;
1367 uint32_t cap_lo;
1368 uint32_t cap_hi;
1369 uint32_t to, vs, pmrcap;
1370 uint8_t mpsmin;
1371 int status, timeout_period;
1372
1373 ctrlr->dev = dev;
1374
1375 mtx_init(&ctrlr->lock, "nvme ctrlr lock", NULL, MTX_DEF);
1376 if (bus_get_domain(dev, &ctrlr->domain) != 0)
1377 ctrlr->domain = 0;
1378
1379 cap_lo = nvme_mmio_read_4(ctrlr, cap_lo);
1380 if (bootverbose) {
1381 device_printf(dev, "CapLo: 0x%08x: MQES %u%s%s%s%s, TO %u\n",
1382 cap_lo, NVME_CAP_LO_MQES(cap_lo),
1383 NVME_CAP_LO_CQR(cap_lo) ? ", CQR" : "",
1384 NVME_CAP_LO_AMS(cap_lo) ? ", AMS" : "",
1385 (NVME_CAP_LO_AMS(cap_lo) & 0x1) ? " WRRwUPC" : "",
1386 (NVME_CAP_LO_AMS(cap_lo) & 0x2) ? " VS" : "",
1387 NVME_CAP_LO_TO(cap_lo));
1388 }
1389 cap_hi = nvme_mmio_read_4(ctrlr, cap_hi);
1390 if (bootverbose) {
1391 device_printf(dev, "CapHi: 0x%08x: DSTRD %u%s, CSS %x%s, "
1392 "MPSMIN %u, MPSMAX %u%s%s\n", cap_hi,
1393 NVME_CAP_HI_DSTRD(cap_hi),
1394 NVME_CAP_HI_NSSRS(cap_hi) ? ", NSSRS" : "",
1395 NVME_CAP_HI_CSS(cap_hi),
1396 NVME_CAP_HI_BPS(cap_hi) ? ", BPS" : "",
1397 NVME_CAP_HI_MPSMIN(cap_hi),
1398 NVME_CAP_HI_MPSMAX(cap_hi),
1399 NVME_CAP_HI_PMRS(cap_hi) ? ", PMRS" : "",
1400 NVME_CAP_HI_CMBS(cap_hi) ? ", CMBS" : "");
1401 }
1402 if (bootverbose) {
1403 vs = nvme_mmio_read_4(ctrlr, vs);
1404 device_printf(dev, "Version: 0x%08x: %d.%d\n", vs,
1405 NVME_MAJOR(vs), NVME_MINOR(vs));
1406 }
1407 if (bootverbose && NVME_CAP_HI_PMRS(cap_hi)) {
1408 pmrcap = nvme_mmio_read_4(ctrlr, pmrcap);
1409 device_printf(dev, "PMRCap: 0x%08x: BIR %u%s%s, PMRTU %u, "
1410 "PMRWBM %x, PMRTO %u%s\n", pmrcap,
1411 NVME_PMRCAP_BIR(pmrcap),
1412 NVME_PMRCAP_RDS(pmrcap) ? ", RDS" : "",
1413 NVME_PMRCAP_WDS(pmrcap) ? ", WDS" : "",
1414 NVME_PMRCAP_PMRTU(pmrcap),
1415 NVME_PMRCAP_PMRWBM(pmrcap),
1416 NVME_PMRCAP_PMRTO(pmrcap),
1417 NVME_PMRCAP_CMSS(pmrcap) ? ", CMSS" : "");
1418 }
1419
1420 ctrlr->dstrd = NVME_CAP_HI_DSTRD(cap_hi) + 2;
1421
1422 mpsmin = NVME_CAP_HI_MPSMIN(cap_hi);
1423 ctrlr->min_page_size = 1 << (12 + mpsmin);
1424
1425 /* Get ready timeout value from controller, in units of 500ms. */
1426 to = NVME_CAP_LO_TO(cap_lo) + 1;
1427 ctrlr->ready_timeout_in_ms = to * 500;
1428
1429 timeout_period = NVME_DEFAULT_TIMEOUT_PERIOD;
1430 TUNABLE_INT_FETCH("hw.nvme.timeout_period", &timeout_period);
1431 timeout_period = min(timeout_period, NVME_MAX_TIMEOUT_PERIOD);
1432 timeout_period = max(timeout_period, NVME_MIN_TIMEOUT_PERIOD);
1433 ctrlr->timeout_period = timeout_period;
1434
1435 nvme_retry_count = NVME_DEFAULT_RETRY_COUNT;
1436 TUNABLE_INT_FETCH("hw.nvme.retry_count", &nvme_retry_count);
1437
1438 ctrlr->enable_aborts = 0;
1439 TUNABLE_INT_FETCH("hw.nvme.enable_aborts", &ctrlr->enable_aborts);
1440
1441 ctrlr->max_xfer_size = NVME_MAX_XFER_SIZE;
1442 if (nvme_ctrlr_construct_admin_qpair(ctrlr) != 0)
1443 return (ENXIO);
1444
1445 /*
1446 * Create 2 threads for the taskqueue. The reset thread will block when
1447 * it detects that the controller has failed until all I/O has been
1448 * failed up the stack. The fail_req task needs to be able to run in
1449 * this case to finish the request failure for some cases.
1450 *
1451 * We could partially solve this race by draining the failed requeust
1452 * queue before proceding to free the sim, though nothing would stop
1453 * new I/O from coming in after we do that drain, but before we reach
1454 * cam_sim_free, so this big hammer is used instead.
1455 */
1456 ctrlr->taskqueue = taskqueue_create("nvme_taskq", M_WAITOK,
1457 taskqueue_thread_enqueue, &ctrlr->taskqueue);
1458 taskqueue_start_threads(&ctrlr->taskqueue, 2, PI_DISK, "nvme taskq");
1459
1460 ctrlr->is_resetting = 0;
1461 ctrlr->is_initialized = 0;
1462 ctrlr->notification_sent = 0;
1463 TASK_INIT(&ctrlr->reset_task, 0, nvme_ctrlr_reset_task, ctrlr);
1464 TASK_INIT(&ctrlr->fail_req_task, 0, nvme_ctrlr_fail_req_task, ctrlr);
1465 STAILQ_INIT(&ctrlr->fail_req);
1466 ctrlr->is_failed = false;
1467
1468 make_dev_args_init(&md_args);
1469 md_args.mda_devsw = &nvme_ctrlr_cdevsw;
1470 md_args.mda_uid = UID_ROOT;
1471 md_args.mda_gid = GID_WHEEL;
1472 md_args.mda_mode = 0600;
1473 md_args.mda_unit = device_get_unit(dev);
1474 md_args.mda_si_drv1 = (void *)ctrlr;
1475 status = make_dev_s(&md_args, &ctrlr->cdev, "nvme%d",
1476 device_get_unit(dev));
1477 if (status != 0)
1478 return (ENXIO);
1479
1480 return (0);
1481 }
1482
1483 void
nvme_ctrlr_destruct(struct nvme_controller * ctrlr,device_t dev)1484 nvme_ctrlr_destruct(struct nvme_controller *ctrlr, device_t dev)
1485 {
1486 int gone, i;
1487
1488 if (ctrlr->resource == NULL)
1489 goto nores;
1490 if (!mtx_initialized(&ctrlr->adminq.lock))
1491 goto noadminq;
1492
1493 /*
1494 * Check whether it is a hot unplug or a clean driver detach.
1495 * If device is not there any more, skip any shutdown commands.
1496 */
1497 gone = (nvme_mmio_read_4(ctrlr, csts) == NVME_GONE);
1498 if (gone)
1499 nvme_ctrlr_fail(ctrlr);
1500 else
1501 nvme_notify_fail_consumers(ctrlr);
1502
1503 for (i = 0; i < NVME_MAX_NAMESPACES; i++)
1504 nvme_ns_destruct(&ctrlr->ns[i]);
1505
1506 if (ctrlr->cdev)
1507 destroy_dev(ctrlr->cdev);
1508
1509 if (ctrlr->is_initialized) {
1510 if (!gone) {
1511 if (ctrlr->hmb_nchunks > 0)
1512 nvme_ctrlr_hmb_enable(ctrlr, false, false);
1513 nvme_ctrlr_delete_qpairs(ctrlr);
1514 }
1515 nvme_ctrlr_hmb_free(ctrlr);
1516 }
1517 if (ctrlr->ioq != NULL) {
1518 for (i = 0; i < ctrlr->num_io_queues; i++)
1519 nvme_io_qpair_destroy(&ctrlr->ioq[i]);
1520 free(ctrlr->ioq, M_NVME);
1521 }
1522 nvme_admin_qpair_destroy(&ctrlr->adminq);
1523
1524 /*
1525 * Notify the controller of a shutdown, even though this is due to
1526 * a driver unload, not a system shutdown (this path is not invoked
1527 * during shutdown). This ensures the controller receives a
1528 * shutdown notification in case the system is shutdown before
1529 * reloading the driver.
1530 */
1531 if (!gone)
1532 nvme_ctrlr_shutdown(ctrlr);
1533
1534 if (!gone)
1535 nvme_ctrlr_disable(ctrlr);
1536
1537 noadminq:
1538 if (ctrlr->taskqueue)
1539 taskqueue_free(ctrlr->taskqueue);
1540
1541 if (ctrlr->tag)
1542 bus_teardown_intr(ctrlr->dev, ctrlr->res, ctrlr->tag);
1543
1544 if (ctrlr->res)
1545 bus_release_resource(ctrlr->dev, SYS_RES_IRQ,
1546 rman_get_rid(ctrlr->res), ctrlr->res);
1547
1548 if (ctrlr->bar4_resource != NULL) {
1549 bus_release_resource(dev, SYS_RES_MEMORY,
1550 ctrlr->bar4_resource_id, ctrlr->bar4_resource);
1551 }
1552
1553 bus_release_resource(dev, SYS_RES_MEMORY,
1554 ctrlr->resource_id, ctrlr->resource);
1555
1556 nores:
1557 mtx_destroy(&ctrlr->lock);
1558 }
1559
1560 void
nvme_ctrlr_shutdown(struct nvme_controller * ctrlr)1561 nvme_ctrlr_shutdown(struct nvme_controller *ctrlr)
1562 {
1563 uint32_t cc;
1564 uint32_t csts;
1565 int timeout;
1566
1567 cc = nvme_mmio_read_4(ctrlr, cc);
1568 cc &= ~(NVME_CC_REG_SHN_MASK << NVME_CC_REG_SHN_SHIFT);
1569 cc |= NVME_SHN_NORMAL << NVME_CC_REG_SHN_SHIFT;
1570 nvme_mmio_write_4(ctrlr, cc, cc);
1571
1572 timeout = ticks + (ctrlr->cdata.rtd3e == 0 ? 5 * hz :
1573 ((uint64_t)ctrlr->cdata.rtd3e * hz + 999999) / 1000000);
1574 while (1) {
1575 csts = nvme_mmio_read_4(ctrlr, csts);
1576 if (csts == NVME_GONE) /* Hot unplug. */
1577 break;
1578 if (NVME_CSTS_GET_SHST(csts) == NVME_SHST_COMPLETE)
1579 break;
1580 if (timeout - ticks < 0) {
1581 nvme_printf(ctrlr, "shutdown timeout\n");
1582 break;
1583 }
1584 pause("nvmeshut", 1);
1585 }
1586 }
1587
1588 void
nvme_ctrlr_submit_admin_request(struct nvme_controller * ctrlr,struct nvme_request * req)1589 nvme_ctrlr_submit_admin_request(struct nvme_controller *ctrlr,
1590 struct nvme_request *req)
1591 {
1592
1593 nvme_qpair_submit_request(&ctrlr->adminq, req);
1594 }
1595
1596 void
nvme_ctrlr_submit_io_request(struct nvme_controller * ctrlr,struct nvme_request * req)1597 nvme_ctrlr_submit_io_request(struct nvme_controller *ctrlr,
1598 struct nvme_request *req)
1599 {
1600 struct nvme_qpair *qpair;
1601
1602 qpair = &ctrlr->ioq[QP(ctrlr, curcpu)];
1603 nvme_qpair_submit_request(qpair, req);
1604 }
1605
1606 device_t
nvme_ctrlr_get_device(struct nvme_controller * ctrlr)1607 nvme_ctrlr_get_device(struct nvme_controller *ctrlr)
1608 {
1609
1610 return (ctrlr->dev);
1611 }
1612
1613 const struct nvme_controller_data *
nvme_ctrlr_get_data(struct nvme_controller * ctrlr)1614 nvme_ctrlr_get_data(struct nvme_controller *ctrlr)
1615 {
1616
1617 return (&ctrlr->cdata);
1618 }
1619
1620 int
nvme_ctrlr_suspend(struct nvme_controller * ctrlr)1621 nvme_ctrlr_suspend(struct nvme_controller *ctrlr)
1622 {
1623 int to = hz;
1624
1625 /*
1626 * Can't touch failed controllers, so it's already suspended.
1627 */
1628 if (ctrlr->is_failed)
1629 return (0);
1630
1631 /*
1632 * We don't want the reset taskqueue running, since it does similar
1633 * things, so prevent it from running after we start. Wait for any reset
1634 * that may have been started to complete. The reset process we follow
1635 * will ensure that any new I/O will queue and be given to the hardware
1636 * after we resume (though there should be none).
1637 */
1638 while (atomic_cmpset_32(&ctrlr->is_resetting, 0, 1) == 0 && to-- > 0)
1639 pause("nvmesusp", 1);
1640 if (to <= 0) {
1641 nvme_printf(ctrlr,
1642 "Competing reset task didn't finish. Try again later.\n");
1643 return (EWOULDBLOCK);
1644 }
1645
1646 if (ctrlr->hmb_nchunks > 0)
1647 nvme_ctrlr_hmb_enable(ctrlr, false, false);
1648
1649 /*
1650 * Per Section 7.6.2 of NVMe spec 1.4, to properly suspend, we need to
1651 * delete the hardware I/O queues, and then shutdown. This properly
1652 * flushes any metadata the drive may have stored so it can survive
1653 * having its power removed and prevents the unsafe shutdown count from
1654 * incriminating. Once we delete the qpairs, we have to disable them
1655 * before shutting down. The delay is out of paranoia in
1656 * nvme_ctrlr_hw_reset, and is repeated here (though we should have no
1657 * pending I/O that the delay copes with).
1658 */
1659 nvme_ctrlr_delete_qpairs(ctrlr);
1660 nvme_ctrlr_disable_qpairs(ctrlr);
1661 pause("nvmesusp", hz / 10);
1662 nvme_ctrlr_shutdown(ctrlr);
1663
1664 return (0);
1665 }
1666
1667 int
nvme_ctrlr_resume(struct nvme_controller * ctrlr)1668 nvme_ctrlr_resume(struct nvme_controller *ctrlr)
1669 {
1670
1671 /*
1672 * Can't touch failed controllers, so nothing to do to resume.
1673 */
1674 if (ctrlr->is_failed)
1675 return (0);
1676
1677 /*
1678 * Have to reset the hardware twice, just like we do on attach. See
1679 * nmve_attach() for why.
1680 */
1681 if (nvme_ctrlr_hw_reset(ctrlr) != 0)
1682 goto fail;
1683 if (nvme_ctrlr_hw_reset(ctrlr) != 0)
1684 goto fail;
1685
1686 /*
1687 * Now that we've reset the hardware, we can restart the controller. Any
1688 * I/O that was pending is requeued. Any admin commands are aborted with
1689 * an error. Once we've restarted, take the controller out of reset.
1690 */
1691 nvme_ctrlr_start(ctrlr, true);
1692 (void)atomic_cmpset_32(&ctrlr->is_resetting, 1, 0);
1693
1694 return (0);
1695 fail:
1696 /*
1697 * Since we can't bring the controller out of reset, announce and fail
1698 * the controller. However, we have to return success for the resume
1699 * itself, due to questionable APIs.
1700 */
1701 nvme_printf(ctrlr, "Failed to reset on resume, failing.\n");
1702 nvme_ctrlr_fail(ctrlr);
1703 (void)atomic_cmpset_32(&ctrlr->is_resetting, 1, 0);
1704 return (0);
1705 }
1706