2 * Copyright (C) 2012 Intel Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
33 #include <sys/ioccom.h>
36 #include <dev/pci/pcireg.h>
37 #include <dev/pci/pcivar.h>
39 #include "nvme_private.h"
41 static void nvme_ctrlr_construct_and_submit_aer(struct nvme_controller *ctrlr,
42 struct nvme_async_event_request *aer);
45 nvme_ctrlr_allocate_bar(struct nvme_controller *ctrlr)
48 /* Chatham puts the NVMe MMRs behind BAR 2/3, not BAR 0/1. */
49 if (pci_get_devid(ctrlr->dev) == CHATHAM_PCI_ID)
50 ctrlr->resource_id = PCIR_BAR(2);
52 ctrlr->resource_id = PCIR_BAR(0);
54 ctrlr->resource = bus_alloc_resource(ctrlr->dev, SYS_RES_MEMORY,
55 &ctrlr->resource_id, 0, ~0, 1, RF_ACTIVE);
57 if(ctrlr->resource == NULL) {
58 nvme_printf(ctrlr, "unable to allocate pci resource\n");
62 ctrlr->bus_tag = rman_get_bustag(ctrlr->resource);
63 ctrlr->bus_handle = rman_get_bushandle(ctrlr->resource);
64 ctrlr->regs = (struct nvme_registers *)ctrlr->bus_handle;
67 * The NVMe spec allows for the MSI-X table to be placed behind
68 * BAR 4/5, separate from the control/doorbell registers. Always
69 * try to map this bar, because it must be mapped prior to calling
70 * pci_alloc_msix(). If the table isn't behind BAR 4/5,
71 * bus_alloc_resource() will just return NULL which is OK.
73 ctrlr->bar4_resource_id = PCIR_BAR(4);
74 ctrlr->bar4_resource = bus_alloc_resource(ctrlr->dev, SYS_RES_MEMORY,
75 &ctrlr->bar4_resource_id, 0, ~0, 1, RF_ACTIVE);
82 nvme_ctrlr_allocate_chatham_bar(struct nvme_controller *ctrlr)
85 ctrlr->chatham_resource_id = PCIR_BAR(CHATHAM_CONTROL_BAR);
86 ctrlr->chatham_resource = bus_alloc_resource(ctrlr->dev,
87 SYS_RES_MEMORY, &ctrlr->chatham_resource_id, 0, ~0, 1,
90 if(ctrlr->chatham_resource == NULL) {
91 nvme_printf(ctrlr, "unable to alloc pci resource\n");
95 ctrlr->chatham_bus_tag = rman_get_bustag(ctrlr->chatham_resource);
96 ctrlr->chatham_bus_handle =
97 rman_get_bushandle(ctrlr->chatham_resource);
103 nvme_ctrlr_setup_chatham(struct nvme_controller *ctrlr)
105 uint64_t reg1, reg2, reg3;
106 uint64_t temp1, temp2;
108 uint32_t use_flash_timings = 0;
112 temp3 = chatham_read_4(ctrlr, 0x8080);
114 device_printf(ctrlr->dev, "Chatham version: 0x%x\n", temp3);
116 ctrlr->chatham_lbas = chatham_read_4(ctrlr, 0x8068) - 0x110;
117 ctrlr->chatham_size = ctrlr->chatham_lbas * 512;
119 device_printf(ctrlr->dev, "Chatham size: %jd\n",
120 (intmax_t)ctrlr->chatham_size);
122 reg1 = reg2 = reg3 = ctrlr->chatham_size - 1;
124 TUNABLE_INT_FETCH("hw.nvme.use_flash_timings", &use_flash_timings);
125 if (use_flash_timings) {
126 device_printf(ctrlr->dev, "Chatham: using flash timings\n");
127 temp1 = 0x00001b58000007d0LL;
128 temp2 = 0x000000cb00000131LL;
130 device_printf(ctrlr->dev, "Chatham: using DDR timings\n");
131 temp1 = temp2 = 0x0LL;
134 chatham_write_8(ctrlr, 0x8000, reg1);
135 chatham_write_8(ctrlr, 0x8008, reg2);
136 chatham_write_8(ctrlr, 0x8010, reg3);
138 chatham_write_8(ctrlr, 0x8020, temp1);
139 temp3 = chatham_read_4(ctrlr, 0x8020);
141 chatham_write_8(ctrlr, 0x8028, temp2);
142 temp3 = chatham_read_4(ctrlr, 0x8028);
144 chatham_write_8(ctrlr, 0x8030, temp1);
145 chatham_write_8(ctrlr, 0x8038, temp2);
146 chatham_write_8(ctrlr, 0x8040, temp1);
147 chatham_write_8(ctrlr, 0x8048, temp2);
148 chatham_write_8(ctrlr, 0x8050, temp1);
149 chatham_write_8(ctrlr, 0x8058, temp2);
155 nvme_chatham_populate_cdata(struct nvme_controller *ctrlr)
157 struct nvme_controller_data *cdata;
159 cdata = &ctrlr->cdata;
162 cdata->ssvid = 0x2011;
165 * Chatham2 puts garbage data in these fields when we
166 * invoke IDENTIFY_CONTROLLER, so we need to re-zero
167 * the fields before calling bcopy().
169 memset(cdata->sn, 0, sizeof(cdata->sn));
170 memcpy(cdata->sn, "2012", strlen("2012"));
171 memset(cdata->mn, 0, sizeof(cdata->mn));
172 memcpy(cdata->mn, "CHATHAM2", strlen("CHATHAM2"));
173 memset(cdata->fr, 0, sizeof(cdata->fr));
174 memcpy(cdata->fr, "0", strlen("0"));
177 cdata->lpa.ns_smart = 1;
184 /* Chatham2 doesn't support DSM command */
187 cdata->vwc.present = 1;
189 #endif /* CHATHAM2 */
192 nvme_ctrlr_construct_admin_qpair(struct nvme_controller *ctrlr)
194 struct nvme_qpair *qpair;
195 uint32_t num_entries;
197 qpair = &ctrlr->adminq;
199 num_entries = NVME_ADMIN_ENTRIES;
200 TUNABLE_INT_FETCH("hw.nvme.admin_entries", &num_entries);
202 * If admin_entries was overridden to an invalid value, revert it
203 * back to our default value.
205 if (num_entries < NVME_MIN_ADMIN_ENTRIES ||
206 num_entries > NVME_MAX_ADMIN_ENTRIES) {
207 nvme_printf(ctrlr, "invalid hw.nvme.admin_entries=%d "
208 "specified\n", num_entries);
209 num_entries = NVME_ADMIN_ENTRIES;
213 * The admin queue's max xfer size is treated differently than the
214 * max I/O xfer size. 16KB is sufficient here - maybe even less?
216 nvme_qpair_construct(qpair,
221 16*1024, /* max xfer size */
226 nvme_ctrlr_construct_io_qpairs(struct nvme_controller *ctrlr)
228 struct nvme_qpair *qpair;
229 union cap_lo_register cap_lo;
230 int i, num_entries, num_trackers;
232 num_entries = NVME_IO_ENTRIES;
233 TUNABLE_INT_FETCH("hw.nvme.io_entries", &num_entries);
236 * NVMe spec sets a hard limit of 64K max entries, but
237 * devices may specify a smaller limit, so we need to check
238 * the MQES field in the capabilities register.
240 cap_lo.raw = nvme_mmio_read_4(ctrlr, cap_lo);
241 num_entries = min(num_entries, cap_lo.bits.mqes+1);
243 num_trackers = NVME_IO_TRACKERS;
244 TUNABLE_INT_FETCH("hw.nvme.io_trackers", &num_trackers);
246 num_trackers = max(num_trackers, NVME_MIN_IO_TRACKERS);
247 num_trackers = min(num_trackers, NVME_MAX_IO_TRACKERS);
249 * No need to have more trackers than entries in the submit queue.
250 * Note also that for a queue size of N, we can only have (N-1)
251 * commands outstanding, hence the "-1" here.
253 num_trackers = min(num_trackers, (num_entries-1));
255 ctrlr->max_xfer_size = NVME_MAX_XFER_SIZE;
256 TUNABLE_INT_FETCH("hw.nvme.max_xfer_size", &ctrlr->max_xfer_size);
258 * Check that tunable doesn't specify a size greater than what our
259 * driver supports, and is an even PAGE_SIZE multiple.
261 if (ctrlr->max_xfer_size > NVME_MAX_XFER_SIZE ||
262 ctrlr->max_xfer_size % PAGE_SIZE)
263 ctrlr->max_xfer_size = NVME_MAX_XFER_SIZE;
265 ctrlr->ioq = malloc(ctrlr->num_io_queues * sizeof(struct nvme_qpair),
266 M_NVME, M_ZERO | M_WAITOK);
268 for (i = 0; i < ctrlr->num_io_queues; i++) {
269 qpair = &ctrlr->ioq[i];
272 * Admin queue has ID=0. IO queues start at ID=1 -
273 * hence the 'i+1' here.
275 * For I/O queues, use the controller-wide max_xfer_size
276 * calculated in nvme_attach().
278 nvme_qpair_construct(qpair,
280 ctrlr->msix_enabled ? i+1 : 0, /* vector */
283 ctrlr->max_xfer_size,
286 if (ctrlr->per_cpu_io_queues)
287 bus_bind_intr(ctrlr->dev, qpair->res, i);
294 nvme_ctrlr_fail(struct nvme_controller *ctrlr)
298 ctrlr->is_failed = TRUE;
299 nvme_qpair_fail(&ctrlr->adminq);
300 for (i = 0; i < ctrlr->num_io_queues; i++)
301 nvme_qpair_fail(&ctrlr->ioq[i]);
302 nvme_notify_fail_consumers(ctrlr);
306 nvme_ctrlr_post_failed_request(struct nvme_controller *ctrlr,
307 struct nvme_request *req)
310 mtx_lock(&ctrlr->fail_req_lock);
311 STAILQ_INSERT_TAIL(&ctrlr->fail_req, req, stailq);
312 mtx_unlock(&ctrlr->fail_req_lock);
313 taskqueue_enqueue(ctrlr->taskqueue, &ctrlr->fail_req_task);
317 nvme_ctrlr_fail_req_task(void *arg, int pending)
319 struct nvme_controller *ctrlr = arg;
320 struct nvme_request *req;
322 mtx_lock(&ctrlr->fail_req_lock);
323 while (!STAILQ_EMPTY(&ctrlr->fail_req)) {
324 req = STAILQ_FIRST(&ctrlr->fail_req);
325 STAILQ_REMOVE_HEAD(&ctrlr->fail_req, stailq);
326 nvme_qpair_manual_complete_request(req->qpair, req,
327 NVME_SCT_GENERIC, NVME_SC_ABORTED_BY_REQUEST, TRUE);
329 mtx_unlock(&ctrlr->fail_req_lock);
333 nvme_ctrlr_wait_for_ready(struct nvme_controller *ctrlr)
336 union cc_register cc;
337 union csts_register csts;
339 cc.raw = nvme_mmio_read_4(ctrlr, cc);
340 csts.raw = nvme_mmio_read_4(ctrlr, csts);
343 nvme_printf(ctrlr, "%s called with cc.en = 0\n", __func__);
349 while (!csts.bits.rdy) {
351 if (ms_waited++ > ctrlr->ready_timeout_in_ms) {
352 nvme_printf(ctrlr, "controller did not become ready "
353 "within %d ms\n", ctrlr->ready_timeout_in_ms);
356 csts.raw = nvme_mmio_read_4(ctrlr, csts);
363 nvme_ctrlr_disable(struct nvme_controller *ctrlr)
365 union cc_register cc;
366 union csts_register csts;
368 cc.raw = nvme_mmio_read_4(ctrlr, cc);
369 csts.raw = nvme_mmio_read_4(ctrlr, csts);
371 if (cc.bits.en == 1 && csts.bits.rdy == 0)
372 nvme_ctrlr_wait_for_ready(ctrlr);
375 nvme_mmio_write_4(ctrlr, cc, cc.raw);
380 nvme_ctrlr_enable(struct nvme_controller *ctrlr)
382 union cc_register cc;
383 union csts_register csts;
384 union aqa_register aqa;
386 cc.raw = nvme_mmio_read_4(ctrlr, cc);
387 csts.raw = nvme_mmio_read_4(ctrlr, csts);
389 if (cc.bits.en == 1) {
390 if (csts.bits.rdy == 1)
393 return (nvme_ctrlr_wait_for_ready(ctrlr));
396 nvme_mmio_write_8(ctrlr, asq, ctrlr->adminq.cmd_bus_addr);
398 nvme_mmio_write_8(ctrlr, acq, ctrlr->adminq.cpl_bus_addr);
402 /* acqs and asqs are 0-based. */
403 aqa.bits.acqs = ctrlr->adminq.num_entries-1;
404 aqa.bits.asqs = ctrlr->adminq.num_entries-1;
405 nvme_mmio_write_4(ctrlr, aqa, aqa.raw);
412 cc.bits.iosqes = 6; /* SQ entry size == 64 == 2^6 */
413 cc.bits.iocqes = 4; /* CQ entry size == 16 == 2^4 */
415 /* This evaluates to 0, which is according to spec. */
416 cc.bits.mps = (PAGE_SIZE >> 13);
418 nvme_mmio_write_4(ctrlr, cc, cc.raw);
421 return (nvme_ctrlr_wait_for_ready(ctrlr));
425 nvme_ctrlr_hw_reset(struct nvme_controller *ctrlr)
429 nvme_admin_qpair_disable(&ctrlr->adminq);
430 for (i = 0; i < ctrlr->num_io_queues; i++)
431 nvme_io_qpair_disable(&ctrlr->ioq[i]);
435 nvme_ctrlr_disable(ctrlr);
436 return (nvme_ctrlr_enable(ctrlr));
440 nvme_ctrlr_reset(struct nvme_controller *ctrlr)
444 cmpset = atomic_cmpset_32(&ctrlr->is_resetting, 0, 1);
446 if (cmpset == 0 || ctrlr->is_failed)
448 * Controller is already resetting or has failed. Return
449 * immediately since there is no need to kick off another
450 * reset in these cases.
454 taskqueue_enqueue(ctrlr->taskqueue, &ctrlr->reset_task);
458 nvme_ctrlr_identify(struct nvme_controller *ctrlr)
460 struct nvme_completion_poll_status status;
463 nvme_ctrlr_cmd_identify_controller(ctrlr, &ctrlr->cdata,
464 nvme_completion_poll_cb, &status);
465 while (status.done == FALSE)
467 if (nvme_completion_is_error(&status.cpl)) {
468 nvme_printf(ctrlr, "nvme_identify_controller failed!\n");
473 if (pci_get_devid(ctrlr->dev) == CHATHAM_PCI_ID)
474 nvme_chatham_populate_cdata(ctrlr);
478 * Use MDTS to ensure our default max_xfer_size doesn't exceed what the
479 * controller supports.
481 if (ctrlr->cdata.mdts > 0)
482 ctrlr->max_xfer_size = min(ctrlr->max_xfer_size,
483 ctrlr->min_page_size * (1 << (ctrlr->cdata.mdts)));
489 nvme_ctrlr_set_num_qpairs(struct nvme_controller *ctrlr)
491 struct nvme_completion_poll_status status;
492 int cq_allocated, i, sq_allocated;
495 nvme_ctrlr_cmd_set_num_queues(ctrlr, ctrlr->num_io_queues,
496 nvme_completion_poll_cb, &status);
497 while (status.done == FALSE)
499 if (nvme_completion_is_error(&status.cpl)) {
500 nvme_printf(ctrlr, "nvme_set_num_queues failed!\n");
505 * Data in cdw0 is 0-based.
506 * Lower 16-bits indicate number of submission queues allocated.
507 * Upper 16-bits indicate number of completion queues allocated.
509 sq_allocated = (status.cpl.cdw0 & 0xFFFF) + 1;
510 cq_allocated = (status.cpl.cdw0 >> 16) + 1;
513 * Check that the controller was able to allocate the number of
514 * queues we requested. If not, revert to one IO queue pair.
516 if (sq_allocated < ctrlr->num_io_queues ||
517 cq_allocated < ctrlr->num_io_queues) {
520 * Destroy extra IO queue pairs that were created at
521 * controller construction time but are no longer
522 * needed. This will only happen when a controller
523 * supports fewer queues than MSI-X vectors. This
524 * is not the normal case, but does occur with the
525 * Chatham prototype board.
527 for (i = 1; i < ctrlr->num_io_queues; i++)
528 nvme_io_qpair_destroy(&ctrlr->ioq[i]);
530 ctrlr->num_io_queues = 1;
531 ctrlr->per_cpu_io_queues = 0;
538 nvme_ctrlr_create_qpairs(struct nvme_controller *ctrlr)
540 struct nvme_completion_poll_status status;
541 struct nvme_qpair *qpair;
544 for (i = 0; i < ctrlr->num_io_queues; i++) {
545 qpair = &ctrlr->ioq[i];
548 nvme_ctrlr_cmd_create_io_cq(ctrlr, qpair, qpair->vector,
549 nvme_completion_poll_cb, &status);
550 while (status.done == FALSE)
552 if (nvme_completion_is_error(&status.cpl)) {
553 nvme_printf(ctrlr, "nvme_create_io_cq failed!\n");
558 nvme_ctrlr_cmd_create_io_sq(qpair->ctrlr, qpair,
559 nvme_completion_poll_cb, &status);
560 while (status.done == FALSE)
562 if (nvme_completion_is_error(&status.cpl)) {
563 nvme_printf(ctrlr, "nvme_create_io_sq failed!\n");
572 nvme_ctrlr_construct_namespaces(struct nvme_controller *ctrlr)
574 struct nvme_namespace *ns;
577 for (i = 0; i < ctrlr->cdata.nn; i++) {
579 status = nvme_ns_construct(ns, i+1, ctrlr);
588 is_log_page_id_valid(uint8_t page_id)
593 case NVME_LOG_HEALTH_INFORMATION:
594 case NVME_LOG_FIRMWARE_SLOT:
602 nvme_ctrlr_get_log_page_size(struct nvme_controller *ctrlr, uint8_t page_id)
604 uint32_t log_page_size;
609 sizeof(struct nvme_error_information_entry) *
611 NVME_MAX_AER_LOG_SIZE);
613 case NVME_LOG_HEALTH_INFORMATION:
614 log_page_size = sizeof(struct nvme_health_information_page);
616 case NVME_LOG_FIRMWARE_SLOT:
617 log_page_size = sizeof(struct nvme_firmware_page);
624 return (log_page_size);
628 nvme_ctrlr_async_event_log_page_cb(void *arg, const struct nvme_completion *cpl)
630 struct nvme_async_event_request *aer = arg;
633 * If the log page fetch for some reason completed with an error,
634 * don't pass log page data to the consumers. In practice, this case
635 * should never happen.
637 if (nvme_completion_is_error(cpl))
638 nvme_notify_async_consumers(aer->ctrlr, &aer->cpl,
639 aer->log_page_id, NULL, 0);
642 * Pass the cpl data from the original async event completion,
643 * not the log page fetch.
645 nvme_notify_async_consumers(aer->ctrlr, &aer->cpl,
646 aer->log_page_id, aer->log_page_buffer, aer->log_page_size);
649 * Repost another asynchronous event request to replace the one
650 * that just completed.
652 nvme_ctrlr_construct_and_submit_aer(aer->ctrlr, aer);
656 nvme_ctrlr_async_event_cb(void *arg, const struct nvme_completion *cpl)
658 struct nvme_async_event_request *aer = arg;
660 if (cpl->status.sc == NVME_SC_ABORTED_SQ_DELETION) {
662 * This is simulated when controller is being shut down, to
663 * effectively abort outstanding asynchronous event requests
664 * and make sure all memory is freed. Do not repost the
665 * request in this case.
670 /* Associated log page is in bits 23:16 of completion entry dw0. */
671 aer->log_page_id = (cpl->cdw0 & 0xFF0000) >> 16;
673 nvme_printf(aer->ctrlr, "async event occurred (log page id=0x%x)\n",
676 if (is_log_page_id_valid(aer->log_page_id)) {
677 aer->log_page_size = nvme_ctrlr_get_log_page_size(aer->ctrlr,
679 memcpy(&aer->cpl, cpl, sizeof(*cpl));
680 nvme_ctrlr_cmd_get_log_page(aer->ctrlr, aer->log_page_id,
681 NVME_GLOBAL_NAMESPACE_TAG, aer->log_page_buffer,
682 aer->log_page_size, nvme_ctrlr_async_event_log_page_cb,
684 /* Wait to notify consumers until after log page is fetched. */
686 nvme_notify_async_consumers(aer->ctrlr, cpl, aer->log_page_id,
690 * Repost another asynchronous event request to replace the one
691 * that just completed.
693 nvme_ctrlr_construct_and_submit_aer(aer->ctrlr, aer);
698 nvme_ctrlr_construct_and_submit_aer(struct nvme_controller *ctrlr,
699 struct nvme_async_event_request *aer)
701 struct nvme_request *req;
704 req = nvme_allocate_request_null(nvme_ctrlr_async_event_cb, aer);
708 * Disable timeout here, since asynchronous event requests should by
709 * nature never be timed out.
711 req->timeout = FALSE;
712 req->cmd.opc = NVME_OPC_ASYNC_EVENT_REQUEST;
713 nvme_ctrlr_submit_admin_request(ctrlr, req);
717 nvme_ctrlr_configure_aer(struct nvme_controller *ctrlr)
719 union nvme_critical_warning_state state;
720 struct nvme_async_event_request *aer;
724 state.bits.reserved = 0;
725 nvme_ctrlr_cmd_set_async_event_config(ctrlr, state, NULL, NULL);
727 /* aerl is a zero-based value, so we need to add 1 here. */
728 ctrlr->num_aers = min(NVME_MAX_ASYNC_EVENTS, (ctrlr->cdata.aerl+1));
730 /* Chatham doesn't support AERs. */
731 if (pci_get_devid(ctrlr->dev) == CHATHAM_PCI_ID)
734 for (i = 0; i < ctrlr->num_aers; i++) {
735 aer = &ctrlr->aer[i];
736 nvme_ctrlr_construct_and_submit_aer(ctrlr, aer);
741 nvme_ctrlr_configure_int_coalescing(struct nvme_controller *ctrlr)
744 ctrlr->int_coal_time = 0;
745 TUNABLE_INT_FETCH("hw.nvme.int_coal_time",
746 &ctrlr->int_coal_time);
748 ctrlr->int_coal_threshold = 0;
749 TUNABLE_INT_FETCH("hw.nvme.int_coal_threshold",
750 &ctrlr->int_coal_threshold);
752 nvme_ctrlr_cmd_set_interrupt_coalescing(ctrlr, ctrlr->int_coal_time,
753 ctrlr->int_coal_threshold, NULL, NULL);
757 nvme_ctrlr_start(void *ctrlr_arg)
759 struct nvme_controller *ctrlr = ctrlr_arg;
762 nvme_qpair_reset(&ctrlr->adminq);
763 for (i = 0; i < ctrlr->num_io_queues; i++)
764 nvme_qpair_reset(&ctrlr->ioq[i]);
766 nvme_admin_qpair_enable(&ctrlr->adminq);
768 if (nvme_ctrlr_identify(ctrlr) != 0) {
769 nvme_ctrlr_fail(ctrlr);
773 if (nvme_ctrlr_set_num_qpairs(ctrlr) != 0) {
774 nvme_ctrlr_fail(ctrlr);
778 if (nvme_ctrlr_create_qpairs(ctrlr) != 0) {
779 nvme_ctrlr_fail(ctrlr);
783 if (nvme_ctrlr_construct_namespaces(ctrlr) != 0) {
784 nvme_ctrlr_fail(ctrlr);
788 nvme_ctrlr_configure_aer(ctrlr);
789 nvme_ctrlr_configure_int_coalescing(ctrlr);
791 for (i = 0; i < ctrlr->num_io_queues; i++)
792 nvme_io_qpair_enable(&ctrlr->ioq[i]);
795 * Clear software progress marker to 0, to indicate to pre-boot
796 * software that OS driver load was successful.
798 * Chatham does not support this feature.
800 if (pci_get_devid(ctrlr->dev) != CHATHAM_PCI_ID)
801 nvme_ctrlr_cmd_set_feature(ctrlr,
802 NVME_FEAT_SOFTWARE_PROGRESS_MARKER, 0, NULL, 0, NULL, NULL);
806 nvme_ctrlr_start_config_hook(void *arg)
808 struct nvme_controller *ctrlr = arg;
810 nvme_ctrlr_start(ctrlr);
811 config_intrhook_disestablish(&ctrlr->config_hook);
815 nvme_ctrlr_reset_task(void *arg, int pending)
817 struct nvme_controller *ctrlr = arg;
820 nvme_printf(ctrlr, "resetting controller\n");
821 status = nvme_ctrlr_hw_reset(ctrlr);
823 * Use pause instead of DELAY, so that we yield to any nvme interrupt
824 * handlers on this CPU that were blocked on a qpair lock. We want
825 * all nvme interrupts completed before proceeding with restarting the
828 * XXX - any way to guarantee the interrupt handlers have quiesced?
830 pause("nvmereset", hz / 10);
832 nvme_ctrlr_start(ctrlr);
834 nvme_ctrlr_fail(ctrlr);
836 atomic_cmpset_32(&ctrlr->is_resetting, 1, 0);
840 nvme_ctrlr_intx_handler(void *arg)
842 struct nvme_controller *ctrlr = arg;
844 nvme_mmio_write_4(ctrlr, intms, 1);
846 nvme_qpair_process_completions(&ctrlr->adminq);
848 if (ctrlr->ioq[0].cpl)
849 nvme_qpair_process_completions(&ctrlr->ioq[0]);
851 nvme_mmio_write_4(ctrlr, intmc, 1);
855 nvme_ctrlr_configure_intx(struct nvme_controller *ctrlr)
858 ctrlr->num_io_queues = 1;
859 ctrlr->per_cpu_io_queues = 0;
861 ctrlr->res = bus_alloc_resource_any(ctrlr->dev, SYS_RES_IRQ,
862 &ctrlr->rid, RF_SHAREABLE | RF_ACTIVE);
864 if (ctrlr->res == NULL) {
865 nvme_printf(ctrlr, "unable to allocate shared IRQ\n");
869 bus_setup_intr(ctrlr->dev, ctrlr->res,
870 INTR_TYPE_MISC | INTR_MPSAFE, NULL, nvme_ctrlr_intx_handler,
873 if (ctrlr->tag == NULL) {
874 nvme_printf(ctrlr, "unable to setup intx handler\n");
882 nvme_ctrlr_ioctl(struct cdev *cdev, u_long cmd, caddr_t arg, int flag,
885 struct nvme_completion_poll_status status;
886 struct nvme_controller *ctrlr;
888 ctrlr = cdev->si_drv1;
891 case NVME_IDENTIFY_CONTROLLER:
894 * Don't refresh data on Chatham, since Chatham returns
895 * garbage on IDENTIFY anyways.
897 if (pci_get_devid(ctrlr->dev) == CHATHAM_PCI_ID) {
898 memcpy(arg, &ctrlr->cdata, sizeof(ctrlr->cdata));
902 /* Refresh data before returning to user. */
904 nvme_ctrlr_cmd_identify_controller(ctrlr, &ctrlr->cdata,
905 nvme_completion_poll_cb, &status);
906 while (status.done == FALSE)
908 if (nvme_completion_is_error(&status.cpl))
910 memcpy(arg, &ctrlr->cdata, sizeof(ctrlr->cdata));
912 case NVME_RESET_CONTROLLER:
913 nvme_ctrlr_reset(ctrlr);
922 static struct cdevsw nvme_ctrlr_cdevsw = {
923 .d_version = D_VERSION,
925 .d_ioctl = nvme_ctrlr_ioctl
929 nvme_ctrlr_construct(struct nvme_controller *ctrlr, device_t dev)
931 union cap_lo_register cap_lo;
932 union cap_hi_register cap_hi;
933 int num_vectors, per_cpu_io_queues, status = 0;
938 status = nvme_ctrlr_allocate_bar(ctrlr);
944 if (pci_get_devid(dev) == CHATHAM_PCI_ID) {
945 status = nvme_ctrlr_allocate_chatham_bar(ctrlr);
948 nvme_ctrlr_setup_chatham(ctrlr);
953 * Software emulators may set the doorbell stride to something
954 * other than zero, but this driver is not set up to handle that.
956 cap_hi.raw = nvme_mmio_read_4(ctrlr, cap_hi);
957 if (cap_hi.bits.dstrd != 0)
960 ctrlr->min_page_size = 1 << (12 + cap_hi.bits.mpsmin);
962 /* Get ready timeout value from controller, in units of 500ms. */
963 cap_lo.raw = nvme_mmio_read_4(ctrlr, cap_lo);
964 ctrlr->ready_timeout_in_ms = cap_lo.bits.to * 500;
966 timeout_period = NVME_DEFAULT_TIMEOUT_PERIOD;
967 TUNABLE_INT_FETCH("hw.nvme.timeout_period", &timeout_period);
968 timeout_period = min(timeout_period, NVME_MAX_TIMEOUT_PERIOD);
969 timeout_period = max(timeout_period, NVME_MIN_TIMEOUT_PERIOD);
970 ctrlr->timeout_period = timeout_period;
972 nvme_retry_count = NVME_DEFAULT_RETRY_COUNT;
973 TUNABLE_INT_FETCH("hw.nvme.retry_count", &nvme_retry_count);
975 per_cpu_io_queues = 1;
976 TUNABLE_INT_FETCH("hw.nvme.per_cpu_io_queues", &per_cpu_io_queues);
977 ctrlr->per_cpu_io_queues = per_cpu_io_queues ? TRUE : FALSE;
979 if (ctrlr->per_cpu_io_queues)
980 ctrlr->num_io_queues = mp_ncpus;
982 ctrlr->num_io_queues = 1;
984 ctrlr->force_intx = 0;
985 TUNABLE_INT_FETCH("hw.nvme.force_intx", &ctrlr->force_intx);
987 ctrlr->enable_aborts = 0;
988 TUNABLE_INT_FETCH("hw.nvme.enable_aborts", &ctrlr->enable_aborts);
990 ctrlr->msix_enabled = 1;
992 if (ctrlr->force_intx) {
993 ctrlr->msix_enabled = 0;
997 /* One vector per IO queue, plus one vector for admin queue. */
998 num_vectors = ctrlr->num_io_queues + 1;
1000 if (pci_msix_count(dev) < num_vectors) {
1001 ctrlr->msix_enabled = 0;
1005 if (pci_alloc_msix(dev, &num_vectors) != 0)
1006 ctrlr->msix_enabled = 0;
1010 if (!ctrlr->msix_enabled)
1011 nvme_ctrlr_configure_intx(ctrlr);
1013 nvme_ctrlr_construct_admin_qpair(ctrlr);
1015 status = nvme_ctrlr_construct_io_qpairs(ctrlr);
1020 ctrlr->cdev = make_dev(&nvme_ctrlr_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
1021 "nvme%d", device_get_unit(dev));
1023 if (ctrlr->cdev == NULL)
1026 ctrlr->cdev->si_drv1 = (void *)ctrlr;
1028 ctrlr->taskqueue = taskqueue_create("nvme_taskq", M_WAITOK,
1029 taskqueue_thread_enqueue, &ctrlr->taskqueue);
1030 taskqueue_start_threads(&ctrlr->taskqueue, 1, PI_DISK, "nvme taskq");
1032 ctrlr->is_resetting = 0;
1033 TASK_INIT(&ctrlr->reset_task, 0, nvme_ctrlr_reset_task, ctrlr);
1035 TASK_INIT(&ctrlr->fail_req_task, 0, nvme_ctrlr_fail_req_task, ctrlr);
1036 mtx_init(&ctrlr->fail_req_lock, "nvme ctrlr fail req lock", NULL,
1038 STAILQ_INIT(&ctrlr->fail_req);
1039 ctrlr->is_failed = FALSE;
1045 nvme_ctrlr_destruct(struct nvme_controller *ctrlr, device_t dev)
1049 nvme_ctrlr_disable(ctrlr);
1050 taskqueue_free(ctrlr->taskqueue);
1052 for (i = 0; i < NVME_MAX_NAMESPACES; i++)
1053 nvme_ns_destruct(&ctrlr->ns[i]);
1056 destroy_dev(ctrlr->cdev);
1058 for (i = 0; i < ctrlr->num_io_queues; i++) {
1059 nvme_io_qpair_destroy(&ctrlr->ioq[i]);
1062 free(ctrlr->ioq, M_NVME);
1064 nvme_admin_qpair_destroy(&ctrlr->adminq);
1066 if (ctrlr->resource != NULL) {
1067 bus_release_resource(dev, SYS_RES_MEMORY,
1068 ctrlr->resource_id, ctrlr->resource);
1071 if (ctrlr->bar4_resource != NULL) {
1072 bus_release_resource(dev, SYS_RES_MEMORY,
1073 ctrlr->bar4_resource_id, ctrlr->bar4_resource);
1077 if (ctrlr->chatham_resource != NULL) {
1078 bus_release_resource(dev, SYS_RES_MEMORY,
1079 ctrlr->chatham_resource_id, ctrlr->chatham_resource);
1084 bus_teardown_intr(ctrlr->dev, ctrlr->res, ctrlr->tag);
1087 bus_release_resource(ctrlr->dev, SYS_RES_IRQ,
1088 rman_get_rid(ctrlr->res), ctrlr->res);
1090 if (ctrlr->msix_enabled)
1091 pci_release_msi(dev);
1095 nvme_ctrlr_submit_admin_request(struct nvme_controller *ctrlr,
1096 struct nvme_request *req)
1099 nvme_qpair_submit_request(&ctrlr->adminq, req);
1103 nvme_ctrlr_submit_io_request(struct nvme_controller *ctrlr,
1104 struct nvme_request *req)
1106 struct nvme_qpair *qpair;
1108 if (ctrlr->per_cpu_io_queues)
1109 qpair = &ctrlr->ioq[curcpu];
1111 qpair = &ctrlr->ioq[0];
1113 nvme_qpair_submit_request(qpair, req);
1117 nvme_ctrlr_get_device(struct nvme_controller *ctrlr)
1120 return (ctrlr->dev);
1123 const struct nvme_controller_data *
1124 nvme_ctrlr_get_data(struct nvme_controller *ctrlr)
1127 return (&ctrlr->cdata);