2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (C) 2012-2016 Intel Corporation
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
39 #include <sys/ioccom.h>
43 #include <sys/endian.h>
45 #include "nvme_private.h"
47 #define B4_CHK_RDY_DELAY_MS 2300 /* work around controller bug */
49 static void nvme_ctrlr_construct_and_submit_aer(struct nvme_controller *ctrlr,
50 struct nvme_async_event_request *aer);
53 nvme_ctrlr_construct_admin_qpair(struct nvme_controller *ctrlr)
55 struct nvme_qpair *qpair;
59 qpair = &ctrlr->adminq;
61 num_entries = NVME_ADMIN_ENTRIES;
62 TUNABLE_INT_FETCH("hw.nvme.admin_entries", &num_entries);
64 * If admin_entries was overridden to an invalid value, revert it
65 * back to our default value.
67 if (num_entries < NVME_MIN_ADMIN_ENTRIES ||
68 num_entries > NVME_MAX_ADMIN_ENTRIES) {
69 nvme_printf(ctrlr, "invalid hw.nvme.admin_entries=%d "
70 "specified\n", num_entries);
71 num_entries = NVME_ADMIN_ENTRIES;
75 * The admin queue's max xfer size is treated differently than the
76 * max I/O xfer size. 16KB is sufficient here - maybe even less?
78 error = nvme_qpair_construct(qpair,
88 nvme_ctrlr_construct_io_qpairs(struct nvme_controller *ctrlr)
90 struct nvme_qpair *qpair;
93 int i, error, num_entries, num_trackers;
95 num_entries = NVME_IO_ENTRIES;
96 TUNABLE_INT_FETCH("hw.nvme.io_entries", &num_entries);
99 * NVMe spec sets a hard limit of 64K max entries, but
100 * devices may specify a smaller limit, so we need to check
101 * the MQES field in the capabilities register.
103 cap_lo = nvme_mmio_read_4(ctrlr, cap_lo);
104 mqes = NVME_CAP_LO_MQES(cap_lo);
105 num_entries = min(num_entries, mqes + 1);
107 num_trackers = NVME_IO_TRACKERS;
108 TUNABLE_INT_FETCH("hw.nvme.io_trackers", &num_trackers);
110 num_trackers = max(num_trackers, NVME_MIN_IO_TRACKERS);
111 num_trackers = min(num_trackers, NVME_MAX_IO_TRACKERS);
113 * No need to have more trackers than entries in the submit queue.
114 * Note also that for a queue size of N, we can only have (N-1)
115 * commands outstanding, hence the "-1" here.
117 num_trackers = min(num_trackers, (num_entries-1));
120 * Our best estimate for the maximum number of I/Os that we should
121 * noramlly have in flight at one time. This should be viewed as a hint,
122 * not a hard limit and will need to be revisitted when the upper layers
123 * of the storage system grows multi-queue support.
125 ctrlr->max_hw_pend_io = num_trackers * ctrlr->num_io_queues * 3 / 4;
128 * This was calculated previously when setting up interrupts, but
129 * a controller could theoretically support fewer I/O queues than
130 * MSI-X vectors. So calculate again here just to be safe.
132 ctrlr->num_cpus_per_ioq = howmany(mp_ncpus, ctrlr->num_io_queues);
134 ctrlr->ioq = malloc(ctrlr->num_io_queues * sizeof(struct nvme_qpair),
135 M_NVME, M_ZERO | M_WAITOK);
137 for (i = 0; i < ctrlr->num_io_queues; i++) {
138 qpair = &ctrlr->ioq[i];
141 * Admin queue has ID=0. IO queues start at ID=1 -
142 * hence the 'i+1' here.
144 * For I/O queues, use the controller-wide max_xfer_size
145 * calculated in nvme_attach().
147 error = nvme_qpair_construct(qpair,
149 ctrlr->msix_enabled ? i+1 : 0, /* vector */
157 * Do not bother binding interrupts if we only have one I/O
158 * interrupt thread for this controller.
160 if (ctrlr->num_io_queues > 1)
161 bus_bind_intr(ctrlr->dev, qpair->res,
162 i * ctrlr->num_cpus_per_ioq);
169 nvme_ctrlr_fail(struct nvme_controller *ctrlr)
173 ctrlr->is_failed = TRUE;
174 nvme_admin_qpair_disable(&ctrlr->adminq);
175 nvme_qpair_fail(&ctrlr->adminq);
176 if (ctrlr->ioq != NULL) {
177 for (i = 0; i < ctrlr->num_io_queues; i++) {
178 nvme_io_qpair_disable(&ctrlr->ioq[i]);
179 nvme_qpair_fail(&ctrlr->ioq[i]);
182 nvme_notify_fail_consumers(ctrlr);
186 nvme_ctrlr_post_failed_request(struct nvme_controller *ctrlr,
187 struct nvme_request *req)
190 mtx_lock(&ctrlr->lock);
191 STAILQ_INSERT_TAIL(&ctrlr->fail_req, req, stailq);
192 mtx_unlock(&ctrlr->lock);
193 taskqueue_enqueue(ctrlr->taskqueue, &ctrlr->fail_req_task);
197 nvme_ctrlr_fail_req_task(void *arg, int pending)
199 struct nvme_controller *ctrlr = arg;
200 struct nvme_request *req;
202 mtx_lock(&ctrlr->lock);
203 while ((req = STAILQ_FIRST(&ctrlr->fail_req)) != NULL) {
204 STAILQ_REMOVE_HEAD(&ctrlr->fail_req, stailq);
205 mtx_unlock(&ctrlr->lock);
206 nvme_qpair_manual_complete_request(req->qpair, req,
207 NVME_SCT_GENERIC, NVME_SC_ABORTED_BY_REQUEST);
208 mtx_lock(&ctrlr->lock);
210 mtx_unlock(&ctrlr->lock);
214 nvme_ctrlr_wait_for_ready(struct nvme_controller *ctrlr, int desired_val)
221 csts = nvme_mmio_read_4(ctrlr, csts);
222 if (csts == 0xffffffff) /* Hot unplug. */
224 if (((csts >> NVME_CSTS_REG_RDY_SHIFT) & NVME_CSTS_REG_RDY_MASK)
227 if (ms_waited++ > ctrlr->ready_timeout_in_ms) {
228 nvme_printf(ctrlr, "controller ready did not become %d "
229 "within %d ms\n", desired_val, ctrlr->ready_timeout_in_ms);
239 nvme_ctrlr_disable(struct nvme_controller *ctrlr)
246 cc = nvme_mmio_read_4(ctrlr, cc);
247 csts = nvme_mmio_read_4(ctrlr, csts);
249 en = (cc >> NVME_CC_REG_EN_SHIFT) & NVME_CC_REG_EN_MASK;
250 rdy = (csts >> NVME_CSTS_REG_RDY_SHIFT) & NVME_CSTS_REG_RDY_MASK;
253 * Per 3.1.5 in NVME 1.3 spec, transitioning CC.EN from 0 to 1
254 * when CSTS.RDY is 1 or transitioning CC.EN from 1 to 0 when
255 * CSTS.RDY is 0 "has undefined results" So make sure that CSTS.RDY
256 * isn't the desired value. Short circuit if we're already disabled.
260 /* EN == 1, wait for RDY == 1 or fail */
261 err = nvme_ctrlr_wait_for_ready(ctrlr, 1);
266 /* EN == 0 already wait for RDY == 0 */
270 return (nvme_ctrlr_wait_for_ready(ctrlr, 0));
273 cc &= ~NVME_CC_REG_EN_MASK;
274 nvme_mmio_write_4(ctrlr, cc, cc);
276 * Some drives have issues with accessing the mmio after we
277 * disable, so delay for a bit after we write the bit to
278 * cope with these issues.
280 if (ctrlr->quirks & QUIRK_DELAY_B4_CHK_RDY)
281 pause("nvmeR", B4_CHK_RDY_DELAY_MS * hz / 1000);
282 return (nvme_ctrlr_wait_for_ready(ctrlr, 0));
286 nvme_ctrlr_enable(struct nvme_controller *ctrlr)
295 cc = nvme_mmio_read_4(ctrlr, cc);
296 csts = nvme_mmio_read_4(ctrlr, csts);
298 en = (cc >> NVME_CC_REG_EN_SHIFT) & NVME_CC_REG_EN_MASK;
299 rdy = (csts >> NVME_CSTS_REG_RDY_SHIFT) & NVME_CSTS_REG_RDY_MASK;
302 * See note in nvme_ctrlr_disable. Short circuit if we're already enabled.
308 return (nvme_ctrlr_wait_for_ready(ctrlr, 1));
310 /* EN == 0 already wait for RDY == 0 or fail */
311 err = nvme_ctrlr_wait_for_ready(ctrlr, 0);
316 nvme_mmio_write_8(ctrlr, asq, ctrlr->adminq.cmd_bus_addr);
318 nvme_mmio_write_8(ctrlr, acq, ctrlr->adminq.cpl_bus_addr);
321 /* acqs and asqs are 0-based. */
322 qsize = ctrlr->adminq.num_entries - 1;
325 aqa = (qsize & NVME_AQA_REG_ACQS_MASK) << NVME_AQA_REG_ACQS_SHIFT;
326 aqa |= (qsize & NVME_AQA_REG_ASQS_MASK) << NVME_AQA_REG_ASQS_SHIFT;
327 nvme_mmio_write_4(ctrlr, aqa, aqa);
330 /* Initialization values for CC */
332 cc |= 1 << NVME_CC_REG_EN_SHIFT;
333 cc |= 0 << NVME_CC_REG_CSS_SHIFT;
334 cc |= 0 << NVME_CC_REG_AMS_SHIFT;
335 cc |= 0 << NVME_CC_REG_SHN_SHIFT;
336 cc |= 6 << NVME_CC_REG_IOSQES_SHIFT; /* SQ entry size == 64 == 2^6 */
337 cc |= 4 << NVME_CC_REG_IOCQES_SHIFT; /* CQ entry size == 16 == 2^4 */
339 /* This evaluates to 0, which is according to spec. */
340 cc |= (PAGE_SIZE >> 13) << NVME_CC_REG_MPS_SHIFT;
342 nvme_mmio_write_4(ctrlr, cc, cc);
344 return (nvme_ctrlr_wait_for_ready(ctrlr, 1));
348 nvme_ctrlr_hw_reset(struct nvme_controller *ctrlr)
352 nvme_admin_qpair_disable(&ctrlr->adminq);
354 * I/O queues are not allocated before the initial HW
355 * reset, so do not try to disable them. Use is_initialized
356 * to determine if this is the initial HW reset.
358 if (ctrlr->is_initialized) {
359 for (i = 0; i < ctrlr->num_io_queues; i++)
360 nvme_io_qpair_disable(&ctrlr->ioq[i]);
365 err = nvme_ctrlr_disable(ctrlr);
368 return (nvme_ctrlr_enable(ctrlr));
372 nvme_ctrlr_reset(struct nvme_controller *ctrlr)
376 cmpset = atomic_cmpset_32(&ctrlr->is_resetting, 0, 1);
378 if (cmpset == 0 || ctrlr->is_failed)
380 * Controller is already resetting or has failed. Return
381 * immediately since there is no need to kick off another
382 * reset in these cases.
386 taskqueue_enqueue(ctrlr->taskqueue, &ctrlr->reset_task);
390 nvme_ctrlr_identify(struct nvme_controller *ctrlr)
392 struct nvme_completion_poll_status status;
395 nvme_ctrlr_cmd_identify_controller(ctrlr, &ctrlr->cdata,
396 nvme_completion_poll_cb, &status);
397 nvme_completion_poll(&status);
398 if (nvme_completion_is_error(&status.cpl)) {
399 nvme_printf(ctrlr, "nvme_identify_controller failed!\n");
403 /* Convert data to host endian */
404 nvme_controller_data_swapbytes(&ctrlr->cdata);
407 * Use MDTS to ensure our default max_xfer_size doesn't exceed what the
408 * controller supports.
410 if (ctrlr->cdata.mdts > 0)
411 ctrlr->max_xfer_size = min(ctrlr->max_xfer_size,
412 ctrlr->min_page_size * (1 << (ctrlr->cdata.mdts)));
418 nvme_ctrlr_set_num_qpairs(struct nvme_controller *ctrlr)
420 struct nvme_completion_poll_status status;
421 int cq_allocated, sq_allocated;
424 nvme_ctrlr_cmd_set_num_queues(ctrlr, ctrlr->num_io_queues,
425 nvme_completion_poll_cb, &status);
426 nvme_completion_poll(&status);
427 if (nvme_completion_is_error(&status.cpl)) {
428 nvme_printf(ctrlr, "nvme_ctrlr_set_num_qpairs failed!\n");
433 * Data in cdw0 is 0-based.
434 * Lower 16-bits indicate number of submission queues allocated.
435 * Upper 16-bits indicate number of completion queues allocated.
437 sq_allocated = (status.cpl.cdw0 & 0xFFFF) + 1;
438 cq_allocated = (status.cpl.cdw0 >> 16) + 1;
441 * Controller may allocate more queues than we requested,
442 * so use the minimum of the number requested and what was
443 * actually allocated.
445 ctrlr->num_io_queues = min(ctrlr->num_io_queues, sq_allocated);
446 ctrlr->num_io_queues = min(ctrlr->num_io_queues, cq_allocated);
452 nvme_ctrlr_create_qpairs(struct nvme_controller *ctrlr)
454 struct nvme_completion_poll_status status;
455 struct nvme_qpair *qpair;
458 for (i = 0; i < ctrlr->num_io_queues; i++) {
459 qpair = &ctrlr->ioq[i];
462 nvme_ctrlr_cmd_create_io_cq(ctrlr, qpair, qpair->vector,
463 nvme_completion_poll_cb, &status);
464 nvme_completion_poll(&status);
465 if (nvme_completion_is_error(&status.cpl)) {
466 nvme_printf(ctrlr, "nvme_create_io_cq failed!\n");
471 nvme_ctrlr_cmd_create_io_sq(qpair->ctrlr, qpair,
472 nvme_completion_poll_cb, &status);
473 nvme_completion_poll(&status);
474 if (nvme_completion_is_error(&status.cpl)) {
475 nvme_printf(ctrlr, "nvme_create_io_sq failed!\n");
484 nvme_ctrlr_destroy_qpairs(struct nvme_controller *ctrlr)
486 struct nvme_completion_poll_status status;
487 struct nvme_qpair *qpair;
489 for (int i = 0; i < ctrlr->num_io_queues; i++) {
490 qpair = &ctrlr->ioq[i];
493 nvme_ctrlr_cmd_delete_io_sq(ctrlr, qpair,
494 nvme_completion_poll_cb, &status);
495 nvme_completion_poll(&status);
496 if (nvme_completion_is_error(&status.cpl)) {
497 nvme_printf(ctrlr, "nvme_destroy_io_sq failed!\n");
502 nvme_ctrlr_cmd_delete_io_cq(ctrlr, qpair,
503 nvme_completion_poll_cb, &status);
504 nvme_completion_poll(&status);
505 if (nvme_completion_is_error(&status.cpl)) {
506 nvme_printf(ctrlr, "nvme_destroy_io_cq failed!\n");
515 nvme_ctrlr_construct_namespaces(struct nvme_controller *ctrlr)
517 struct nvme_namespace *ns;
520 for (i = 0; i < min(ctrlr->cdata.nn, NVME_MAX_NAMESPACES); i++) {
522 nvme_ns_construct(ns, i+1, ctrlr);
529 is_log_page_id_valid(uint8_t page_id)
534 case NVME_LOG_HEALTH_INFORMATION:
535 case NVME_LOG_FIRMWARE_SLOT:
536 case NVME_LOG_CHANGED_NAMESPACE:
537 case NVME_LOG_COMMAND_EFFECT:
538 case NVME_LOG_RES_NOTIFICATION:
539 case NVME_LOG_SANITIZE_STATUS:
547 nvme_ctrlr_get_log_page_size(struct nvme_controller *ctrlr, uint8_t page_id)
549 uint32_t log_page_size;
554 sizeof(struct nvme_error_information_entry) *
555 (ctrlr->cdata.elpe + 1), NVME_MAX_AER_LOG_SIZE);
557 case NVME_LOG_HEALTH_INFORMATION:
558 log_page_size = sizeof(struct nvme_health_information_page);
560 case NVME_LOG_FIRMWARE_SLOT:
561 log_page_size = sizeof(struct nvme_firmware_page);
563 case NVME_LOG_CHANGED_NAMESPACE:
564 log_page_size = sizeof(struct nvme_ns_list);
566 case NVME_LOG_COMMAND_EFFECT:
567 log_page_size = sizeof(struct nvme_command_effects_page);
569 case NVME_LOG_RES_NOTIFICATION:
570 log_page_size = sizeof(struct nvme_res_notification_page);
572 case NVME_LOG_SANITIZE_STATUS:
573 log_page_size = sizeof(struct nvme_sanitize_status_page);
580 return (log_page_size);
584 nvme_ctrlr_log_critical_warnings(struct nvme_controller *ctrlr,
588 if (state & NVME_CRIT_WARN_ST_AVAILABLE_SPARE)
589 nvme_printf(ctrlr, "available spare space below threshold\n");
591 if (state & NVME_CRIT_WARN_ST_TEMPERATURE)
592 nvme_printf(ctrlr, "temperature above threshold\n");
594 if (state & NVME_CRIT_WARN_ST_DEVICE_RELIABILITY)
595 nvme_printf(ctrlr, "device reliability degraded\n");
597 if (state & NVME_CRIT_WARN_ST_READ_ONLY)
598 nvme_printf(ctrlr, "media placed in read only mode\n");
600 if (state & NVME_CRIT_WARN_ST_VOLATILE_MEMORY_BACKUP)
601 nvme_printf(ctrlr, "volatile memory backup device failed\n");
603 if (state & NVME_CRIT_WARN_ST_RESERVED_MASK)
605 "unknown critical warning(s): state = 0x%02x\n", state);
609 nvme_ctrlr_async_event_log_page_cb(void *arg, const struct nvme_completion *cpl)
611 struct nvme_async_event_request *aer = arg;
612 struct nvme_health_information_page *health_info;
613 struct nvme_ns_list *nsl;
614 struct nvme_error_information_entry *err;
618 * If the log page fetch for some reason completed with an error,
619 * don't pass log page data to the consumers. In practice, this case
620 * should never happen.
622 if (nvme_completion_is_error(cpl))
623 nvme_notify_async_consumers(aer->ctrlr, &aer->cpl,
624 aer->log_page_id, NULL, 0);
626 /* Convert data to host endian */
627 switch (aer->log_page_id) {
629 err = (struct nvme_error_information_entry *)aer->log_page_buffer;
630 for (i = 0; i < (aer->ctrlr->cdata.elpe + 1); i++)
631 nvme_error_information_entry_swapbytes(err++);
633 case NVME_LOG_HEALTH_INFORMATION:
634 nvme_health_information_page_swapbytes(
635 (struct nvme_health_information_page *)aer->log_page_buffer);
637 case NVME_LOG_FIRMWARE_SLOT:
638 nvme_firmware_page_swapbytes(
639 (struct nvme_firmware_page *)aer->log_page_buffer);
641 case NVME_LOG_CHANGED_NAMESPACE:
642 nvme_ns_list_swapbytes(
643 (struct nvme_ns_list *)aer->log_page_buffer);
645 case NVME_LOG_COMMAND_EFFECT:
646 nvme_command_effects_page_swapbytes(
647 (struct nvme_command_effects_page *)aer->log_page_buffer);
649 case NVME_LOG_RES_NOTIFICATION:
650 nvme_res_notification_page_swapbytes(
651 (struct nvme_res_notification_page *)aer->log_page_buffer);
653 case NVME_LOG_SANITIZE_STATUS:
654 nvme_sanitize_status_page_swapbytes(
655 (struct nvme_sanitize_status_page *)aer->log_page_buffer);
657 case INTEL_LOG_TEMP_STATS:
658 intel_log_temp_stats_swapbytes(
659 (struct intel_log_temp_stats *)aer->log_page_buffer);
665 if (aer->log_page_id == NVME_LOG_HEALTH_INFORMATION) {
666 health_info = (struct nvme_health_information_page *)
667 aer->log_page_buffer;
668 nvme_ctrlr_log_critical_warnings(aer->ctrlr,
669 health_info->critical_warning);
671 * Critical warnings reported through the
672 * SMART/health log page are persistent, so
673 * clear the associated bits in the async event
674 * config so that we do not receive repeated
675 * notifications for the same event.
677 aer->ctrlr->async_event_config &=
678 ~health_info->critical_warning;
679 nvme_ctrlr_cmd_set_async_event_config(aer->ctrlr,
680 aer->ctrlr->async_event_config, NULL, NULL);
681 } else if (aer->log_page_id == NVME_LOG_CHANGED_NAMESPACE &&
683 nsl = (struct nvme_ns_list *)aer->log_page_buffer;
684 for (i = 0; i < nitems(nsl->ns) && nsl->ns[i] != 0; i++) {
685 if (nsl->ns[i] > NVME_MAX_NAMESPACES)
687 nvme_notify_ns(aer->ctrlr, nsl->ns[i]);
693 * Pass the cpl data from the original async event completion,
694 * not the log page fetch.
696 nvme_notify_async_consumers(aer->ctrlr, &aer->cpl,
697 aer->log_page_id, aer->log_page_buffer, aer->log_page_size);
701 * Repost another asynchronous event request to replace the one
702 * that just completed.
704 nvme_ctrlr_construct_and_submit_aer(aer->ctrlr, aer);
708 nvme_ctrlr_async_event_cb(void *arg, const struct nvme_completion *cpl)
710 struct nvme_async_event_request *aer = arg;
712 if (nvme_completion_is_error(cpl)) {
714 * Do not retry failed async event requests. This avoids
715 * infinite loops where a new async event request is submitted
716 * to replace the one just failed, only to fail again and
717 * perpetuate the loop.
722 /* Associated log page is in bits 23:16 of completion entry dw0. */
723 aer->log_page_id = (cpl->cdw0 & 0xFF0000) >> 16;
725 nvme_printf(aer->ctrlr, "async event occurred (type 0x%x, info 0x%02x,"
726 " page 0x%02x)\n", (cpl->cdw0 & 0x07), (cpl->cdw0 & 0xFF00) >> 8,
729 if (is_log_page_id_valid(aer->log_page_id)) {
730 aer->log_page_size = nvme_ctrlr_get_log_page_size(aer->ctrlr,
732 memcpy(&aer->cpl, cpl, sizeof(*cpl));
733 nvme_ctrlr_cmd_get_log_page(aer->ctrlr, aer->log_page_id,
734 NVME_GLOBAL_NAMESPACE_TAG, aer->log_page_buffer,
735 aer->log_page_size, nvme_ctrlr_async_event_log_page_cb,
737 /* Wait to notify consumers until after log page is fetched. */
739 nvme_notify_async_consumers(aer->ctrlr, cpl, aer->log_page_id,
743 * Repost another asynchronous event request to replace the one
744 * that just completed.
746 nvme_ctrlr_construct_and_submit_aer(aer->ctrlr, aer);
751 nvme_ctrlr_construct_and_submit_aer(struct nvme_controller *ctrlr,
752 struct nvme_async_event_request *aer)
754 struct nvme_request *req;
757 req = nvme_allocate_request_null(nvme_ctrlr_async_event_cb, aer);
761 * Disable timeout here, since asynchronous event requests should by
762 * nature never be timed out.
764 req->timeout = FALSE;
765 req->cmd.opc = NVME_OPC_ASYNC_EVENT_REQUEST;
766 nvme_ctrlr_submit_admin_request(ctrlr, req);
770 nvme_ctrlr_configure_aer(struct nvme_controller *ctrlr)
772 struct nvme_completion_poll_status status;
773 struct nvme_async_event_request *aer;
776 ctrlr->async_event_config = NVME_CRIT_WARN_ST_AVAILABLE_SPARE |
777 NVME_CRIT_WARN_ST_DEVICE_RELIABILITY |
778 NVME_CRIT_WARN_ST_READ_ONLY |
779 NVME_CRIT_WARN_ST_VOLATILE_MEMORY_BACKUP;
780 if (ctrlr->cdata.ver >= NVME_REV(1, 2))
781 ctrlr->async_event_config |= 0x300;
784 nvme_ctrlr_cmd_get_feature(ctrlr, NVME_FEAT_TEMPERATURE_THRESHOLD,
785 0, NULL, 0, nvme_completion_poll_cb, &status);
786 nvme_completion_poll(&status);
787 if (nvme_completion_is_error(&status.cpl) ||
788 (status.cpl.cdw0 & 0xFFFF) == 0xFFFF ||
789 (status.cpl.cdw0 & 0xFFFF) == 0x0000) {
790 nvme_printf(ctrlr, "temperature threshold not supported\n");
792 ctrlr->async_event_config |= NVME_CRIT_WARN_ST_TEMPERATURE;
794 nvme_ctrlr_cmd_set_async_event_config(ctrlr,
795 ctrlr->async_event_config, NULL, NULL);
797 /* aerl is a zero-based value, so we need to add 1 here. */
798 ctrlr->num_aers = min(NVME_MAX_ASYNC_EVENTS, (ctrlr->cdata.aerl+1));
800 for (i = 0; i < ctrlr->num_aers; i++) {
801 aer = &ctrlr->aer[i];
802 nvme_ctrlr_construct_and_submit_aer(ctrlr, aer);
807 nvme_ctrlr_configure_int_coalescing(struct nvme_controller *ctrlr)
810 ctrlr->int_coal_time = 0;
811 TUNABLE_INT_FETCH("hw.nvme.int_coal_time",
812 &ctrlr->int_coal_time);
814 ctrlr->int_coal_threshold = 0;
815 TUNABLE_INT_FETCH("hw.nvme.int_coal_threshold",
816 &ctrlr->int_coal_threshold);
818 nvme_ctrlr_cmd_set_interrupt_coalescing(ctrlr, ctrlr->int_coal_time,
819 ctrlr->int_coal_threshold, NULL, NULL);
823 nvme_ctrlr_start(void *ctrlr_arg)
825 struct nvme_controller *ctrlr = ctrlr_arg;
826 uint32_t old_num_io_queues;
830 * Only reset adminq here when we are restarting the
831 * controller after a reset. During initialization,
832 * we have already submitted admin commands to get
833 * the number of I/O queues supported, so cannot reset
834 * the adminq again here.
836 if (ctrlr->is_resetting)
837 nvme_qpair_reset(&ctrlr->adminq);
839 for (i = 0; i < ctrlr->num_io_queues; i++)
840 nvme_qpair_reset(&ctrlr->ioq[i]);
842 nvme_admin_qpair_enable(&ctrlr->adminq);
844 if (nvme_ctrlr_identify(ctrlr) != 0) {
845 nvme_ctrlr_fail(ctrlr);
850 * The number of qpairs are determined during controller initialization,
851 * including using NVMe SET_FEATURES/NUMBER_OF_QUEUES to determine the
852 * HW limit. We call SET_FEATURES again here so that it gets called
853 * after any reset for controllers that depend on the driver to
854 * explicit specify how many queues it will use. This value should
855 * never change between resets, so panic if somehow that does happen.
857 if (ctrlr->is_resetting) {
858 old_num_io_queues = ctrlr->num_io_queues;
859 if (nvme_ctrlr_set_num_qpairs(ctrlr) != 0) {
860 nvme_ctrlr_fail(ctrlr);
864 if (old_num_io_queues != ctrlr->num_io_queues) {
865 panic("num_io_queues changed from %u to %u",
866 old_num_io_queues, ctrlr->num_io_queues);
870 if (nvme_ctrlr_create_qpairs(ctrlr) != 0) {
871 nvme_ctrlr_fail(ctrlr);
875 if (nvme_ctrlr_construct_namespaces(ctrlr) != 0) {
876 nvme_ctrlr_fail(ctrlr);
880 nvme_ctrlr_configure_aer(ctrlr);
881 nvme_ctrlr_configure_int_coalescing(ctrlr);
883 for (i = 0; i < ctrlr->num_io_queues; i++)
884 nvme_io_qpair_enable(&ctrlr->ioq[i]);
888 nvme_ctrlr_start_config_hook(void *arg)
890 struct nvme_controller *ctrlr = arg;
892 nvme_qpair_reset(&ctrlr->adminq);
893 nvme_admin_qpair_enable(&ctrlr->adminq);
895 if (nvme_ctrlr_set_num_qpairs(ctrlr) == 0 &&
896 nvme_ctrlr_construct_io_qpairs(ctrlr) == 0)
897 nvme_ctrlr_start(ctrlr);
899 nvme_ctrlr_fail(ctrlr);
901 nvme_sysctl_initialize_ctrlr(ctrlr);
902 config_intrhook_disestablish(&ctrlr->config_hook);
904 ctrlr->is_initialized = 1;
905 nvme_notify_new_controller(ctrlr);
909 nvme_ctrlr_reset_task(void *arg, int pending)
911 struct nvme_controller *ctrlr = arg;
914 nvme_printf(ctrlr, "resetting controller\n");
915 status = nvme_ctrlr_hw_reset(ctrlr);
917 * Use pause instead of DELAY, so that we yield to any nvme interrupt
918 * handlers on this CPU that were blocked on a qpair lock. We want
919 * all nvme interrupts completed before proceeding with restarting the
922 * XXX - any way to guarantee the interrupt handlers have quiesced?
924 pause("nvmereset", hz / 10);
926 nvme_ctrlr_start(ctrlr);
928 nvme_ctrlr_fail(ctrlr);
930 atomic_cmpset_32(&ctrlr->is_resetting, 1, 0);
934 * Poll all the queues enabled on the device for completion.
937 nvme_ctrlr_poll(struct nvme_controller *ctrlr)
941 nvme_qpair_process_completions(&ctrlr->adminq);
943 for (i = 0; i < ctrlr->num_io_queues; i++)
944 if (ctrlr->ioq && ctrlr->ioq[i].cpl)
945 nvme_qpair_process_completions(&ctrlr->ioq[i]);
949 * Poll the single-vector intertrupt case: num_io_queues will be 1 and
950 * there's only a single vector. While we're polling, we mask further
951 * interrupts in the controller.
954 nvme_ctrlr_intx_handler(void *arg)
956 struct nvme_controller *ctrlr = arg;
958 nvme_mmio_write_4(ctrlr, intms, 1);
959 nvme_ctrlr_poll(ctrlr);
960 nvme_mmio_write_4(ctrlr, intmc, 1);
964 nvme_pt_done(void *arg, const struct nvme_completion *cpl)
966 struct nvme_pt_command *pt = arg;
967 struct mtx *mtx = pt->driver_lock;
970 bzero(&pt->cpl, sizeof(pt->cpl));
971 pt->cpl.cdw0 = cpl->cdw0;
973 status = cpl->status;
974 status &= ~NVME_STATUS_P_MASK;
975 pt->cpl.status = status;
978 pt->driver_lock = NULL;
984 nvme_ctrlr_passthrough_cmd(struct nvme_controller *ctrlr,
985 struct nvme_pt_command *pt, uint32_t nsid, int is_user_buffer,
988 struct nvme_request *req;
990 struct buf *buf = NULL;
992 vm_offset_t addr, end;
996 * vmapbuf calls vm_fault_quick_hold_pages which only maps full
997 * pages. Ensure this request has fewer than MAXPHYS bytes when
998 * extended to full pages.
1000 addr = (vm_offset_t)pt->buf;
1001 end = round_page(addr + pt->len);
1002 addr = trunc_page(addr);
1003 if (end - addr > MAXPHYS)
1006 if (pt->len > ctrlr->max_xfer_size) {
1007 nvme_printf(ctrlr, "pt->len (%d) "
1008 "exceeds max_xfer_size (%d)\n", pt->len,
1009 ctrlr->max_xfer_size);
1012 if (is_user_buffer) {
1014 * Ensure the user buffer is wired for the duration of
1015 * this passthrough command.
1018 buf = getpbuf(NULL);
1019 buf->b_data = pt->buf;
1020 buf->b_bufsize = pt->len;
1021 buf->b_iocmd = pt->is_read ? BIO_READ : BIO_WRITE;
1022 if (vmapbuf(buf, 1) < 0) {
1026 req = nvme_allocate_request_vaddr(buf->b_data, pt->len,
1029 req = nvme_allocate_request_vaddr(pt->buf, pt->len,
1032 req = nvme_allocate_request_null(nvme_pt_done, pt);
1034 /* Assume userspace already converted to little-endian */
1035 req->cmd.opc = pt->cmd.opc;
1036 req->cmd.fuse = pt->cmd.fuse;
1037 req->cmd.rsvd2 = pt->cmd.rsvd2;
1038 req->cmd.rsvd3 = pt->cmd.rsvd3;
1039 req->cmd.cdw10 = pt->cmd.cdw10;
1040 req->cmd.cdw11 = pt->cmd.cdw11;
1041 req->cmd.cdw12 = pt->cmd.cdw12;
1042 req->cmd.cdw13 = pt->cmd.cdw13;
1043 req->cmd.cdw14 = pt->cmd.cdw14;
1044 req->cmd.cdw15 = pt->cmd.cdw15;
1046 req->cmd.nsid = htole32(nsid);
1048 mtx = mtx_pool_find(mtxpool_sleep, pt);
1049 pt->driver_lock = mtx;
1052 nvme_ctrlr_submit_admin_request(ctrlr, req);
1054 nvme_ctrlr_submit_io_request(ctrlr, req);
1057 while (pt->driver_lock != NULL)
1058 mtx_sleep(pt, mtx, PRIBIO, "nvme_pt", 0);
1071 nvme_ctrlr_ioctl(struct cdev *cdev, u_long cmd, caddr_t arg, int flag,
1074 struct nvme_controller *ctrlr;
1075 struct nvme_pt_command *pt;
1077 ctrlr = cdev->si_drv1;
1080 case NVME_RESET_CONTROLLER:
1081 nvme_ctrlr_reset(ctrlr);
1083 case NVME_PASSTHROUGH_CMD:
1084 pt = (struct nvme_pt_command *)arg;
1085 return (nvme_ctrlr_passthrough_cmd(ctrlr, pt, le32toh(pt->cmd.nsid),
1086 1 /* is_user_buffer */, 1 /* is_admin_cmd */));
1089 struct nvme_get_nsid *gnsid = (struct nvme_get_nsid *)arg;
1090 strncpy(gnsid->cdev, device_get_nameunit(ctrlr->dev),
1091 sizeof(gnsid->cdev));
1102 static struct cdevsw nvme_ctrlr_cdevsw = {
1103 .d_version = D_VERSION,
1105 .d_ioctl = nvme_ctrlr_ioctl
1109 nvme_ctrlr_construct(struct nvme_controller *ctrlr, device_t dev)
1111 struct make_dev_args md_args;
1117 int status, timeout_period;
1121 mtx_init(&ctrlr->lock, "nvme ctrlr lock", NULL, MTX_DEF);
1124 * Software emulators may set the doorbell stride to something
1125 * other than zero, but this driver is not set up to handle that.
1127 cap_hi = nvme_mmio_read_4(ctrlr, cap_hi);
1128 dstrd = NVME_CAP_HI_DSTRD(cap_hi);
1132 mpsmin = NVME_CAP_HI_MPSMIN(cap_hi);
1133 ctrlr->min_page_size = 1 << (12 + mpsmin);
1135 /* Get ready timeout value from controller, in units of 500ms. */
1136 cap_lo = nvme_mmio_read_4(ctrlr, cap_lo);
1137 to = NVME_CAP_LO_TO(cap_lo) + 1;
1138 ctrlr->ready_timeout_in_ms = to * 500;
1140 timeout_period = NVME_DEFAULT_TIMEOUT_PERIOD;
1141 TUNABLE_INT_FETCH("hw.nvme.timeout_period", &timeout_period);
1142 timeout_period = min(timeout_period, NVME_MAX_TIMEOUT_PERIOD);
1143 timeout_period = max(timeout_period, NVME_MIN_TIMEOUT_PERIOD);
1144 ctrlr->timeout_period = timeout_period;
1146 nvme_retry_count = NVME_DEFAULT_RETRY_COUNT;
1147 TUNABLE_INT_FETCH("hw.nvme.retry_count", &nvme_retry_count);
1149 ctrlr->enable_aborts = 0;
1150 TUNABLE_INT_FETCH("hw.nvme.enable_aborts", &ctrlr->enable_aborts);
1152 ctrlr->max_xfer_size = NVME_MAX_XFER_SIZE;
1153 if (nvme_ctrlr_construct_admin_qpair(ctrlr) != 0)
1156 ctrlr->taskqueue = taskqueue_create("nvme_taskq", M_WAITOK,
1157 taskqueue_thread_enqueue, &ctrlr->taskqueue);
1158 taskqueue_start_threads(&ctrlr->taskqueue, 1, PI_DISK, "nvme taskq");
1160 ctrlr->is_resetting = 0;
1161 ctrlr->is_initialized = 0;
1162 ctrlr->notification_sent = 0;
1163 TASK_INIT(&ctrlr->reset_task, 0, nvme_ctrlr_reset_task, ctrlr);
1164 TASK_INIT(&ctrlr->fail_req_task, 0, nvme_ctrlr_fail_req_task, ctrlr);
1165 STAILQ_INIT(&ctrlr->fail_req);
1166 ctrlr->is_failed = FALSE;
1168 make_dev_args_init(&md_args);
1169 md_args.mda_devsw = &nvme_ctrlr_cdevsw;
1170 md_args.mda_uid = UID_ROOT;
1171 md_args.mda_gid = GID_WHEEL;
1172 md_args.mda_mode = 0600;
1173 md_args.mda_unit = device_get_unit(dev);
1174 md_args.mda_si_drv1 = (void *)ctrlr;
1175 status = make_dev_s(&md_args, &ctrlr->cdev, "nvme%d",
1176 device_get_unit(dev));
1184 nvme_ctrlr_destruct(struct nvme_controller *ctrlr, device_t dev)
1188 if (ctrlr->resource == NULL)
1192 * Check whether it is a hot unplug or a clean driver detach.
1193 * If device is not there any more, skip any shutdown commands.
1195 gone = (nvme_mmio_read_4(ctrlr, csts) == 0xffffffff);
1197 nvme_ctrlr_fail(ctrlr);
1199 nvme_notify_fail_consumers(ctrlr);
1201 for (i = 0; i < NVME_MAX_NAMESPACES; i++)
1202 nvme_ns_destruct(&ctrlr->ns[i]);
1205 destroy_dev(ctrlr->cdev);
1207 if (ctrlr->is_initialized) {
1209 nvme_ctrlr_destroy_qpairs(ctrlr);
1210 for (i = 0; i < ctrlr->num_io_queues; i++)
1211 nvme_io_qpair_destroy(&ctrlr->ioq[i]);
1212 free(ctrlr->ioq, M_NVME);
1213 nvme_admin_qpair_destroy(&ctrlr->adminq);
1217 * Notify the controller of a shutdown, even though this is due to
1218 * a driver unload, not a system shutdown (this path is not invoked
1219 * during shutdown). This ensures the controller receives a
1220 * shutdown notification in case the system is shutdown before
1221 * reloading the driver.
1224 nvme_ctrlr_shutdown(ctrlr);
1227 nvme_ctrlr_disable(ctrlr);
1229 if (ctrlr->taskqueue)
1230 taskqueue_free(ctrlr->taskqueue);
1233 bus_teardown_intr(ctrlr->dev, ctrlr->res, ctrlr->tag);
1236 bus_release_resource(ctrlr->dev, SYS_RES_IRQ,
1237 rman_get_rid(ctrlr->res), ctrlr->res);
1239 if (ctrlr->bar4_resource != NULL) {
1240 bus_release_resource(dev, SYS_RES_MEMORY,
1241 ctrlr->bar4_resource_id, ctrlr->bar4_resource);
1244 bus_release_resource(dev, SYS_RES_MEMORY,
1245 ctrlr->resource_id, ctrlr->resource);
1248 mtx_destroy(&ctrlr->lock);
1252 nvme_ctrlr_shutdown(struct nvme_controller *ctrlr)
1258 cc = nvme_mmio_read_4(ctrlr, cc);
1259 cc &= ~(NVME_CC_REG_SHN_MASK << NVME_CC_REG_SHN_SHIFT);
1260 cc |= NVME_SHN_NORMAL << NVME_CC_REG_SHN_SHIFT;
1261 nvme_mmio_write_4(ctrlr, cc, cc);
1264 csts = nvme_mmio_read_4(ctrlr, csts);
1265 if (csts == 0xffffffff) /* Hot unplug. */
1267 if (NVME_CSTS_GET_SHST(csts) == NVME_SHST_COMPLETE)
1269 if (ticks++ > 5*hz) {
1270 nvme_printf(ctrlr, "did not complete shutdown within"
1271 " 5 seconds of notification\n");
1274 pause("nvme shn", 1);
1279 nvme_ctrlr_submit_admin_request(struct nvme_controller *ctrlr,
1280 struct nvme_request *req)
1283 nvme_qpair_submit_request(&ctrlr->adminq, req);
1287 nvme_ctrlr_submit_io_request(struct nvme_controller *ctrlr,
1288 struct nvme_request *req)
1290 struct nvme_qpair *qpair;
1292 qpair = &ctrlr->ioq[curcpu / ctrlr->num_cpus_per_ioq];
1293 nvme_qpair_submit_request(qpair, req);
1297 nvme_ctrlr_get_device(struct nvme_controller *ctrlr)
1300 return (ctrlr->dev);
1303 const struct nvme_controller_data *
1304 nvme_ctrlr_get_data(struct nvme_controller *ctrlr)
1307 return (&ctrlr->cdata);