2015-09-21 15:52:41 +00:00
|
|
|
/*-
|
|
|
|
* BSD LICENSE
|
|
|
|
*
|
2016-01-26 17:47:22 +00:00
|
|
|
* Copyright (c) Intel Corporation.
|
2015-09-21 15:52:41 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* * Neither the name of Intel Corporation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
2016-02-03 21:36:26 +00:00
|
|
|
|
2015-09-21 15:52:41 +00:00
|
|
|
#include "nvme_internal.h"
|
2016-01-27 07:08:53 +00:00
|
|
|
#include "spdk/pci.h"
|
|
|
|
|
2016-02-10 18:26:12 +00:00
|
|
|
static int nvme_ctrlr_construct_and_submit_aer(struct spdk_nvme_ctrlr *ctrlr,
|
2015-09-21 15:52:41 +00:00
|
|
|
struct nvme_async_event_request *aer);
|
|
|
|
|
2016-03-07 17:36:17 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
spdk_nvme_ctrlr_opts_set_defaults(struct spdk_nvme_ctrlr_opts *opts)
|
|
|
|
{
|
|
|
|
opts->num_io_queues = DEFAULT_MAX_IO_QUEUES;
|
2016-05-05 01:41:16 +00:00
|
|
|
opts->use_cmb_sqs = false;
|
2016-06-14 07:19:38 +00:00
|
|
|
opts->arb_mechanism = SPDK_NVME_CC_AMS_RR;
|
2016-03-07 17:36:17 +00:00
|
|
|
}
|
|
|
|
|
2016-02-29 21:19:02 +00:00
|
|
|
static int
|
|
|
|
spdk_nvme_ctrlr_create_qpair(struct spdk_nvme_ctrlr *ctrlr, struct spdk_nvme_qpair *qpair)
|
|
|
|
{
|
|
|
|
struct nvme_completion_poll_status status;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
status.done = false;
|
|
|
|
rc = nvme_ctrlr_cmd_create_io_cq(ctrlr, qpair, nvme_completion_poll_cb, &status);
|
|
|
|
if (rc != 0) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (status.done == false) {
|
|
|
|
spdk_nvme_qpair_process_completions(&ctrlr->adminq, 0);
|
|
|
|
}
|
|
|
|
if (spdk_nvme_cpl_is_error(&status.cpl)) {
|
|
|
|
nvme_printf(ctrlr, "nvme_create_io_cq failed!\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
status.done = false;
|
|
|
|
rc = nvme_ctrlr_cmd_create_io_sq(qpair->ctrlr, qpair, nvme_completion_poll_cb, &status);
|
|
|
|
if (rc != 0) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (status.done == false) {
|
|
|
|
spdk_nvme_qpair_process_completions(&ctrlr->adminq, 0);
|
|
|
|
}
|
|
|
|
if (spdk_nvme_cpl_is_error(&status.cpl)) {
|
|
|
|
nvme_printf(ctrlr, "nvme_create_io_sq failed!\n");
|
|
|
|
/* Attempt to delete the completion queue */
|
|
|
|
status.done = false;
|
|
|
|
rc = nvme_ctrlr_cmd_delete_io_cq(qpair->ctrlr, qpair, nvme_completion_poll_cb, &status);
|
|
|
|
if (rc != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
while (status.done == false) {
|
|
|
|
spdk_nvme_qpair_process_completions(&ctrlr->adminq, 0);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
nvme_qpair_reset(qpair);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct spdk_nvme_qpair *
|
|
|
|
spdk_nvme_ctrlr_alloc_io_qpair(struct spdk_nvme_ctrlr *ctrlr,
|
|
|
|
enum spdk_nvme_qprio qprio)
|
|
|
|
{
|
|
|
|
struct spdk_nvme_qpair *qpair;
|
2016-06-14 07:19:38 +00:00
|
|
|
union spdk_nvme_cc_register cc;
|
|
|
|
|
|
|
|
cc.raw = nvme_mmio_read_4(ctrlr, cc.raw);
|
2016-02-29 21:19:02 +00:00
|
|
|
|
|
|
|
/* Only the low 2 bits (values 0, 1, 2, 3) of QPRIO are valid. */
|
|
|
|
if ((qprio & 3) != qprio) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-06-14 07:19:38 +00:00
|
|
|
/*
|
|
|
|
* Only value SPDK_NVME_QPRIO_URGENT(0) is valid for the
|
|
|
|
* default round robin arbitration method.
|
|
|
|
*/
|
|
|
|
if ((cc.bits.ams == SPDK_NVME_CC_AMS_RR) && (qprio != SPDK_NVME_QPRIO_URGENT)) {
|
|
|
|
nvme_printf(ctrlr,
|
|
|
|
"invalid queue priority for default round robin arbitration method\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-08-08 17:03:52 +00:00
|
|
|
pthread_mutex_lock(&ctrlr->ctrlr_lock);
|
2016-02-29 21:19:02 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the first available qpair structure.
|
|
|
|
*/
|
|
|
|
qpair = TAILQ_FIRST(&ctrlr->free_io_qpairs);
|
|
|
|
if (qpair == NULL) {
|
|
|
|
/* No free queue IDs */
|
2016-08-08 17:03:52 +00:00
|
|
|
pthread_mutex_unlock(&ctrlr->ctrlr_lock);
|
2016-02-29 21:19:02 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* At this point, qpair contains a preallocated submission and completion queue and a
|
|
|
|
* unique queue ID, but it is not yet created on the controller.
|
|
|
|
*
|
|
|
|
* Fill out the submission queue priority and send out the Create I/O Queue commands.
|
|
|
|
*/
|
|
|
|
qpair->qprio = qprio;
|
|
|
|
if (spdk_nvme_ctrlr_create_qpair(ctrlr, qpair) != 0) {
|
|
|
|
/*
|
|
|
|
* spdk_nvme_ctrlr_create_qpair() failed, so the qpair structure is still unused.
|
|
|
|
* Exit here so we don't insert it into the active_io_qpairs list.
|
|
|
|
*/
|
2016-08-08 17:03:52 +00:00
|
|
|
pthread_mutex_unlock(&ctrlr->ctrlr_lock);
|
2016-02-29 21:19:02 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
TAILQ_REMOVE(&ctrlr->free_io_qpairs, qpair, tailq);
|
|
|
|
TAILQ_INSERT_TAIL(&ctrlr->active_io_qpairs, qpair, tailq);
|
|
|
|
|
2016-08-08 17:03:52 +00:00
|
|
|
pthread_mutex_unlock(&ctrlr->ctrlr_lock);
|
2016-02-29 21:19:02 +00:00
|
|
|
|
|
|
|
return qpair;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
spdk_nvme_ctrlr_free_io_qpair(struct spdk_nvme_qpair *qpair)
|
|
|
|
{
|
|
|
|
struct spdk_nvme_ctrlr *ctrlr;
|
|
|
|
struct nvme_completion_poll_status status;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (qpair == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctrlr = qpair->ctrlr;
|
|
|
|
|
2016-08-08 17:03:52 +00:00
|
|
|
pthread_mutex_lock(&ctrlr->ctrlr_lock);
|
2016-02-29 21:19:02 +00:00
|
|
|
|
|
|
|
/* Delete the I/O submission queue and then the completion queue */
|
|
|
|
|
|
|
|
status.done = false;
|
|
|
|
rc = nvme_ctrlr_cmd_delete_io_sq(ctrlr, qpair, nvme_completion_poll_cb, &status);
|
|
|
|
if (rc != 0) {
|
2016-08-08 17:03:52 +00:00
|
|
|
pthread_mutex_unlock(&ctrlr->ctrlr_lock);
|
2016-02-29 21:19:02 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
while (status.done == false) {
|
|
|
|
spdk_nvme_qpair_process_completions(&ctrlr->adminq, 0);
|
|
|
|
}
|
|
|
|
if (spdk_nvme_cpl_is_error(&status.cpl)) {
|
2016-08-08 17:03:52 +00:00
|
|
|
pthread_mutex_unlock(&ctrlr->ctrlr_lock);
|
2016-02-29 21:19:02 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
status.done = false;
|
|
|
|
rc = nvme_ctrlr_cmd_delete_io_cq(ctrlr, qpair, nvme_completion_poll_cb, &status);
|
|
|
|
if (rc != 0) {
|
2016-08-08 17:03:52 +00:00
|
|
|
pthread_mutex_unlock(&ctrlr->ctrlr_lock);
|
2016-02-29 21:19:02 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
while (status.done == false) {
|
|
|
|
spdk_nvme_qpair_process_completions(&ctrlr->adminq, 0);
|
|
|
|
}
|
|
|
|
if (spdk_nvme_cpl_is_error(&status.cpl)) {
|
2016-08-08 17:03:52 +00:00
|
|
|
pthread_mutex_unlock(&ctrlr->ctrlr_lock);
|
2016-02-29 21:19:02 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
TAILQ_REMOVE(&ctrlr->active_io_qpairs, qpair, tailq);
|
|
|
|
TAILQ_INSERT_HEAD(&ctrlr->free_io_qpairs, qpair, tailq);
|
|
|
|
|
2016-08-08 17:03:52 +00:00
|
|
|
pthread_mutex_unlock(&ctrlr->ctrlr_lock);
|
2016-02-29 21:19:02 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-01-06 05:43:33 +00:00
|
|
|
static void
|
2016-02-10 18:26:12 +00:00
|
|
|
nvme_ctrlr_construct_intel_support_log_page_list(struct spdk_nvme_ctrlr *ctrlr,
|
2016-02-08 23:06:31 +00:00
|
|
|
struct spdk_nvme_intel_log_page_directory *log_page_directory)
|
2016-01-06 05:43:33 +00:00
|
|
|
{
|
2016-02-03 21:36:26 +00:00
|
|
|
struct spdk_pci_device *dev;
|
2016-01-27 07:08:53 +00:00
|
|
|
struct pci_id pci_id;
|
|
|
|
|
2016-02-08 21:08:06 +00:00
|
|
|
if (ctrlr->cdata.vid != SPDK_PCI_VID_INTEL || log_page_directory == NULL)
|
2016-01-06 05:43:33 +00:00
|
|
|
return;
|
|
|
|
|
2016-01-27 07:08:53 +00:00
|
|
|
dev = ctrlr->devhandle;
|
|
|
|
pci_id.vendor_id = spdk_pci_device_get_vendor_id(dev);
|
|
|
|
pci_id.dev_id = spdk_pci_device_get_device_id(dev);
|
|
|
|
pci_id.sub_vendor_id = spdk_pci_device_get_subvendor_id(dev);
|
|
|
|
pci_id.sub_dev_id = spdk_pci_device_get_subdevice_id(dev);
|
|
|
|
|
2016-02-08 23:06:31 +00:00
|
|
|
ctrlr->log_page_supported[SPDK_NVME_INTEL_LOG_PAGE_DIRECTORY] = true;
|
2016-01-06 05:43:33 +00:00
|
|
|
|
2016-01-27 07:08:53 +00:00
|
|
|
if (log_page_directory->read_latency_log_len ||
|
|
|
|
nvme_intel_has_quirk(&pci_id, NVME_INTEL_QUIRK_READ_LATENCY)) {
|
2016-02-08 23:06:31 +00:00
|
|
|
ctrlr->log_page_supported[SPDK_NVME_INTEL_LOG_READ_CMD_LATENCY] = true;
|
2016-01-06 05:43:33 +00:00
|
|
|
}
|
2016-01-27 07:08:53 +00:00
|
|
|
if (log_page_directory->write_latency_log_len ||
|
|
|
|
nvme_intel_has_quirk(&pci_id, NVME_INTEL_QUIRK_WRITE_LATENCY)) {
|
2016-02-08 23:06:31 +00:00
|
|
|
ctrlr->log_page_supported[SPDK_NVME_INTEL_LOG_WRITE_CMD_LATENCY] = true;
|
2016-01-06 05:43:33 +00:00
|
|
|
}
|
|
|
|
if (log_page_directory->temperature_statistics_log_len) {
|
2016-02-08 23:06:31 +00:00
|
|
|
ctrlr->log_page_supported[SPDK_NVME_INTEL_LOG_TEMPERATURE] = true;
|
2016-01-06 05:43:33 +00:00
|
|
|
}
|
|
|
|
if (log_page_directory->smart_log_len) {
|
2016-02-08 23:06:31 +00:00
|
|
|
ctrlr->log_page_supported[SPDK_NVME_INTEL_LOG_SMART] = true;
|
2016-01-06 05:43:33 +00:00
|
|
|
}
|
2016-05-04 03:17:59 +00:00
|
|
|
if (log_page_directory->marketing_description_log_len) {
|
|
|
|
ctrlr->log_page_supported[SPDK_NVME_INTEL_MARKETING_DESCRIPTION] = true;
|
|
|
|
}
|
2016-01-06 05:43:33 +00:00
|
|
|
}
|
|
|
|
|
2016-02-10 18:26:12 +00:00
|
|
|
static int nvme_ctrlr_set_intel_support_log_pages(struct spdk_nvme_ctrlr *ctrlr)
|
2016-01-06 05:43:33 +00:00
|
|
|
{
|
|
|
|
uint64_t phys_addr = 0;
|
|
|
|
struct nvme_completion_poll_status status;
|
2016-02-08 23:06:31 +00:00
|
|
|
struct spdk_nvme_intel_log_page_directory *log_page_directory;
|
2016-01-06 05:43:33 +00:00
|
|
|
|
|
|
|
log_page_directory = nvme_malloc("nvme_log_page_directory",
|
2016-02-08 23:06:31 +00:00
|
|
|
sizeof(struct spdk_nvme_intel_log_page_directory),
|
2016-01-06 05:43:33 +00:00
|
|
|
64, &phys_addr);
|
|
|
|
if (log_page_directory == NULL) {
|
|
|
|
nvme_printf(NULL, "could not allocate log_page_directory\n");
|
2016-06-14 22:17:33 +00:00
|
|
|
return -ENXIO;
|
2016-01-06 05:43:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
status.done = false;
|
2016-02-10 18:26:12 +00:00
|
|
|
spdk_nvme_ctrlr_cmd_get_log_page(ctrlr, SPDK_NVME_INTEL_LOG_PAGE_DIRECTORY, SPDK_NVME_GLOBAL_NS_TAG,
|
|
|
|
log_page_directory, sizeof(struct spdk_nvme_intel_log_page_directory),
|
|
|
|
nvme_completion_poll_cb,
|
|
|
|
&status);
|
2016-01-06 05:43:33 +00:00
|
|
|
while (status.done == false) {
|
2016-02-29 21:56:53 +00:00
|
|
|
spdk_nvme_qpair_process_completions(&ctrlr->adminq, 0);
|
2016-01-06 05:43:33 +00:00
|
|
|
}
|
2016-02-09 18:06:48 +00:00
|
|
|
if (spdk_nvme_cpl_is_error(&status.cpl)) {
|
2016-01-06 05:43:33 +00:00
|
|
|
nvme_free(log_page_directory);
|
|
|
|
nvme_printf(ctrlr, "nvme_ctrlr_cmd_get_log_page failed!\n");
|
2016-06-14 22:17:33 +00:00
|
|
|
return -ENXIO;
|
2016-01-06 05:43:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nvme_ctrlr_construct_intel_support_log_page_list(ctrlr, log_page_directory);
|
|
|
|
nvme_free(log_page_directory);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-02-10 18:26:12 +00:00
|
|
|
nvme_ctrlr_set_supported_log_pages(struct spdk_nvme_ctrlr *ctrlr)
|
2016-01-06 05:43:33 +00:00
|
|
|
{
|
2016-01-15 20:21:18 +00:00
|
|
|
memset(ctrlr->log_page_supported, 0, sizeof(ctrlr->log_page_supported));
|
|
|
|
/* Mandatory pages */
|
2016-02-09 18:06:48 +00:00
|
|
|
ctrlr->log_page_supported[SPDK_NVME_LOG_ERROR] = true;
|
|
|
|
ctrlr->log_page_supported[SPDK_NVME_LOG_HEALTH_INFORMATION] = true;
|
|
|
|
ctrlr->log_page_supported[SPDK_NVME_LOG_FIRMWARE_SLOT] = true;
|
2016-01-06 05:43:33 +00:00
|
|
|
if (ctrlr->cdata.lpa.celp) {
|
2016-02-09 18:06:48 +00:00
|
|
|
ctrlr->log_page_supported[SPDK_NVME_LOG_COMMAND_EFFECTS_LOG] = true;
|
2016-01-06 05:43:33 +00:00
|
|
|
}
|
2016-02-08 21:08:06 +00:00
|
|
|
if (ctrlr->cdata.vid == SPDK_PCI_VID_INTEL) {
|
2016-01-06 05:43:33 +00:00
|
|
|
nvme_ctrlr_set_intel_support_log_pages(ctrlr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-25 05:04:23 +00:00
|
|
|
static void
|
2016-02-10 18:26:12 +00:00
|
|
|
nvme_ctrlr_set_intel_supported_features(struct spdk_nvme_ctrlr *ctrlr)
|
2016-01-25 05:04:23 +00:00
|
|
|
{
|
2016-02-08 23:06:31 +00:00
|
|
|
ctrlr->feature_supported[SPDK_NVME_INTEL_FEAT_MAX_LBA] = true;
|
|
|
|
ctrlr->feature_supported[SPDK_NVME_INTEL_FEAT_NATIVE_MAX_LBA] = true;
|
|
|
|
ctrlr->feature_supported[SPDK_NVME_INTEL_FEAT_POWER_GOVERNOR_SETTING] = true;
|
|
|
|
ctrlr->feature_supported[SPDK_NVME_INTEL_FEAT_SMBUS_ADDRESS] = true;
|
|
|
|
ctrlr->feature_supported[SPDK_NVME_INTEL_FEAT_LED_PATTERN] = true;
|
|
|
|
ctrlr->feature_supported[SPDK_NVME_INTEL_FEAT_RESET_TIMED_WORKLOAD_COUNTERS] = true;
|
|
|
|
ctrlr->feature_supported[SPDK_NVME_INTEL_FEAT_LATENCY_TRACKING] = true;
|
2016-01-25 05:04:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-02-10 18:26:12 +00:00
|
|
|
nvme_ctrlr_set_supported_features(struct spdk_nvme_ctrlr *ctrlr)
|
2016-01-25 05:04:23 +00:00
|
|
|
{
|
|
|
|
memset(ctrlr->feature_supported, 0, sizeof(ctrlr->feature_supported));
|
|
|
|
/* Mandatory features */
|
2016-02-09 18:06:48 +00:00
|
|
|
ctrlr->feature_supported[SPDK_NVME_FEAT_ARBITRATION] = true;
|
|
|
|
ctrlr->feature_supported[SPDK_NVME_FEAT_POWER_MANAGEMENT] = true;
|
|
|
|
ctrlr->feature_supported[SPDK_NVME_FEAT_TEMPERATURE_THRESHOLD] = true;
|
|
|
|
ctrlr->feature_supported[SPDK_NVME_FEAT_ERROR_RECOVERY] = true;
|
|
|
|
ctrlr->feature_supported[SPDK_NVME_FEAT_NUMBER_OF_QUEUES] = true;
|
|
|
|
ctrlr->feature_supported[SPDK_NVME_FEAT_INTERRUPT_COALESCING] = true;
|
|
|
|
ctrlr->feature_supported[SPDK_NVME_FEAT_INTERRUPT_VECTOR_CONFIGURATION] = true;
|
|
|
|
ctrlr->feature_supported[SPDK_NVME_FEAT_WRITE_ATOMICITY] = true;
|
|
|
|
ctrlr->feature_supported[SPDK_NVME_FEAT_ASYNC_EVENT_CONFIGURATION] = true;
|
2016-01-25 05:04:23 +00:00
|
|
|
/* Optional features */
|
|
|
|
if (ctrlr->cdata.vwc.present) {
|
2016-02-09 18:06:48 +00:00
|
|
|
ctrlr->feature_supported[SPDK_NVME_FEAT_VOLATILE_WRITE_CACHE] = true;
|
2016-01-25 05:04:23 +00:00
|
|
|
}
|
|
|
|
if (ctrlr->cdata.apsta.supported) {
|
2016-02-09 18:06:48 +00:00
|
|
|
ctrlr->feature_supported[SPDK_NVME_FEAT_AUTONOMOUS_POWER_STATE_TRANSITION] = true;
|
2016-01-25 05:04:23 +00:00
|
|
|
}
|
|
|
|
if (ctrlr->cdata.hmpre) {
|
2016-02-09 18:06:48 +00:00
|
|
|
ctrlr->feature_supported[SPDK_NVME_FEAT_HOST_MEM_BUFFER] = true;
|
2016-01-25 05:04:23 +00:00
|
|
|
}
|
2016-02-08 21:08:06 +00:00
|
|
|
if (ctrlr->cdata.vid == SPDK_PCI_VID_INTEL) {
|
2016-01-25 05:04:23 +00:00
|
|
|
nvme_ctrlr_set_intel_supported_features(ctrlr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-21 15:52:41 +00:00
|
|
|
static int
|
2016-02-10 18:26:12 +00:00
|
|
|
nvme_ctrlr_construct_admin_qpair(struct spdk_nvme_ctrlr *ctrlr)
|
2015-09-21 15:52:41 +00:00
|
|
|
{
|
2015-09-22 17:12:00 +00:00
|
|
|
return nvme_qpair_construct(&ctrlr->adminq,
|
|
|
|
0, /* qpair ID */
|
|
|
|
NVME_ADMIN_ENTRIES,
|
|
|
|
NVME_ADMIN_TRACKERS,
|
|
|
|
ctrlr);
|
2015-09-21 15:52:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2016-02-10 18:26:12 +00:00
|
|
|
nvme_ctrlr_construct_io_qpairs(struct spdk_nvme_ctrlr *ctrlr)
|
2015-09-21 15:52:41 +00:00
|
|
|
{
|
2016-02-29 17:11:35 +00:00
|
|
|
struct spdk_nvme_qpair *qpair;
|
2016-07-06 22:41:59 +00:00
|
|
|
union spdk_nvme_cap_register cap;
|
2015-11-02 19:58:19 +00:00
|
|
|
uint32_t i, num_entries, num_trackers;
|
|
|
|
int rc;
|
2015-09-21 15:52:41 +00:00
|
|
|
|
|
|
|
if (ctrlr->ioq != NULL) {
|
|
|
|
/*
|
|
|
|
* io_qpairs were already constructed, so just return.
|
|
|
|
* This typically happens when the controller is
|
|
|
|
* initialized a second (or subsequent) time after a
|
|
|
|
* controller reset.
|
|
|
|
*/
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NVMe spec sets a hard limit of 64K max entries, but
|
|
|
|
* devices may specify a smaller limit, so we need to check
|
|
|
|
* the MQES field in the capabilities register.
|
|
|
|
*/
|
2016-07-06 22:41:59 +00:00
|
|
|
cap.raw = nvme_mmio_read_8(ctrlr, cap.raw);
|
|
|
|
num_entries = nvme_min(NVME_IO_ENTRIES, cap.bits.mqes + 1);
|
2015-09-21 15:52:41 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* No need to have more trackers than entries in the submit queue.
|
|
|
|
* Note also that for a queue size of N, we can only have (N-1)
|
|
|
|
* commands outstanding, hence the "-1" here.
|
|
|
|
*/
|
|
|
|
num_trackers = nvme_min(NVME_IO_TRACKERS, (num_entries - 1));
|
|
|
|
|
|
|
|
ctrlr->max_xfer_size = NVME_MAX_XFER_SIZE;
|
|
|
|
|
2016-03-07 17:36:17 +00:00
|
|
|
ctrlr->ioq = calloc(ctrlr->opts.num_io_queues, sizeof(struct spdk_nvme_qpair));
|
2015-09-21 15:52:41 +00:00
|
|
|
|
|
|
|
if (ctrlr->ioq == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2016-03-07 17:36:17 +00:00
|
|
|
for (i = 0; i < ctrlr->opts.num_io_queues; i++) {
|
2015-09-21 15:52:41 +00:00
|
|
|
qpair = &ctrlr->ioq[i];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Admin queue has ID=0. IO queues start at ID=1 -
|
|
|
|
* hence the 'i+1' here.
|
|
|
|
*/
|
|
|
|
rc = nvme_qpair_construct(qpair,
|
|
|
|
i + 1, /* qpair ID */
|
|
|
|
num_entries,
|
|
|
|
num_trackers,
|
|
|
|
ctrlr);
|
|
|
|
if (rc)
|
|
|
|
return -1;
|
2016-02-29 21:19:02 +00:00
|
|
|
|
|
|
|
TAILQ_INSERT_TAIL(&ctrlr->free_io_qpairs, qpair, tailq);
|
2015-09-21 15:52:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-02-10 18:26:12 +00:00
|
|
|
nvme_ctrlr_fail(struct spdk_nvme_ctrlr *ctrlr)
|
2015-09-21 15:52:41 +00:00
|
|
|
{
|
2015-11-02 19:58:19 +00:00
|
|
|
uint32_t i;
|
2015-09-21 15:52:41 +00:00
|
|
|
|
|
|
|
ctrlr->is_failed = true;
|
|
|
|
nvme_qpair_fail(&ctrlr->adminq);
|
2016-03-23 04:38:40 +00:00
|
|
|
if (ctrlr->ioq) {
|
|
|
|
for (i = 0; i < ctrlr->opts.num_io_queues; i++) {
|
|
|
|
nvme_qpair_fail(&ctrlr->ioq[i]);
|
|
|
|
}
|
2015-09-21 15:52:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-02-10 18:26:12 +00:00
|
|
|
nvme_ctrlr_shutdown(struct spdk_nvme_ctrlr *ctrlr)
|
2015-09-21 15:52:41 +00:00
|
|
|
{
|
2016-02-09 18:06:48 +00:00
|
|
|
union spdk_nvme_cc_register cc;
|
|
|
|
union spdk_nvme_csts_register csts;
|
2015-09-21 15:52:41 +00:00
|
|
|
int ms_waited = 0;
|
|
|
|
|
|
|
|
cc.raw = nvme_mmio_read_4(ctrlr, cc.raw);
|
2016-02-09 18:06:48 +00:00
|
|
|
cc.bits.shn = SPDK_NVME_SHN_NORMAL;
|
2015-09-21 15:52:41 +00:00
|
|
|
nvme_mmio_write_4(ctrlr, cc.raw, cc.raw);
|
|
|
|
|
2016-03-01 23:28:29 +00:00
|
|
|
csts.raw = nvme_mmio_read_4(ctrlr, csts.raw);
|
2015-09-21 15:52:41 +00:00
|
|
|
/*
|
|
|
|
* The NVMe spec does not define a timeout period
|
|
|
|
* for shutdown notification, so we just pick
|
|
|
|
* 5 seconds as a reasonable amount of time to
|
|
|
|
* wait before proceeding.
|
|
|
|
*/
|
2016-02-09 18:06:48 +00:00
|
|
|
while (csts.bits.shst != SPDK_NVME_SHST_COMPLETE) {
|
2015-09-21 15:52:41 +00:00
|
|
|
nvme_delay(1000);
|
2016-03-01 23:28:29 +00:00
|
|
|
csts.raw = nvme_mmio_read_4(ctrlr, csts.raw);
|
2015-09-21 15:52:41 +00:00
|
|
|
if (ms_waited++ >= 5000)
|
|
|
|
break;
|
|
|
|
}
|
2016-02-09 18:06:48 +00:00
|
|
|
if (csts.bits.shst != SPDK_NVME_SHST_COMPLETE)
|
2015-09-21 15:52:41 +00:00
|
|
|
nvme_printf(ctrlr, "did not shutdown within 5 seconds\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2016-02-10 18:26:12 +00:00
|
|
|
nvme_ctrlr_enable(struct spdk_nvme_ctrlr *ctrlr)
|
2015-09-21 15:52:41 +00:00
|
|
|
{
|
2016-02-09 18:06:48 +00:00
|
|
|
union spdk_nvme_cc_register cc;
|
|
|
|
union spdk_nvme_aqa_register aqa;
|
2016-07-06 22:41:59 +00:00
|
|
|
union spdk_nvme_cap_register cap;
|
2015-09-21 15:52:41 +00:00
|
|
|
|
|
|
|
cc.raw = nvme_mmio_read_4(ctrlr, cc.raw);
|
|
|
|
|
2016-02-23 23:36:13 +00:00
|
|
|
if (cc.bits.en != 0) {
|
|
|
|
nvme_printf(ctrlr, "%s called with CC.EN = 1\n", __func__);
|
2016-06-14 22:17:33 +00:00
|
|
|
return -EINVAL;
|
2015-09-21 15:52:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nvme_mmio_write_8(ctrlr, asq, ctrlr->adminq.cmd_bus_addr);
|
|
|
|
nvme_mmio_write_8(ctrlr, acq, ctrlr->adminq.cpl_bus_addr);
|
|
|
|
|
|
|
|
aqa.raw = 0;
|
|
|
|
/* acqs and asqs are 0-based. */
|
|
|
|
aqa.bits.acqs = ctrlr->adminq.num_entries - 1;
|
|
|
|
aqa.bits.asqs = ctrlr->adminq.num_entries - 1;
|
|
|
|
nvme_mmio_write_4(ctrlr, aqa.raw, aqa.raw);
|
|
|
|
|
|
|
|
cc.bits.en = 1;
|
|
|
|
cc.bits.css = 0;
|
|
|
|
cc.bits.shn = 0;
|
|
|
|
cc.bits.iosqes = 6; /* SQ entry size == 64 == 2^6 */
|
|
|
|
cc.bits.iocqes = 4; /* CQ entry size == 16 == 2^4 */
|
|
|
|
|
|
|
|
/* Page size is 2 ^ (12 + mps). */
|
|
|
|
cc.bits.mps = nvme_u32log2(PAGE_SIZE) - 12;
|
|
|
|
|
2016-07-06 22:41:59 +00:00
|
|
|
cap.raw = nvme_mmio_read_8(ctrlr, cap.raw);
|
2016-06-14 07:19:38 +00:00
|
|
|
|
|
|
|
switch (ctrlr->opts.arb_mechanism) {
|
|
|
|
case SPDK_NVME_CC_AMS_RR:
|
|
|
|
break;
|
|
|
|
case SPDK_NVME_CC_AMS_WRR:
|
2016-07-06 22:41:59 +00:00
|
|
|
if (SPDK_NVME_CAP_AMS_WRR & cap.bits.ams) {
|
2016-06-14 07:19:38 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return -EINVAL;
|
|
|
|
case SPDK_NVME_CC_AMS_VS:
|
2016-07-06 22:41:59 +00:00
|
|
|
if (SPDK_NVME_CAP_AMS_VS & cap.bits.ams) {
|
2016-06-14 07:19:38 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return -EINVAL;
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
cc.bits.ams = ctrlr->opts.arb_mechanism;
|
|
|
|
|
2015-09-21 15:52:41 +00:00
|
|
|
nvme_mmio_write_4(ctrlr, cc.raw, cc.raw);
|
|
|
|
|
2016-02-23 23:36:13 +00:00
|
|
|
return 0;
|
2015-09-21 15:52:41 +00:00
|
|
|
}
|
|
|
|
|
2016-02-23 23:36:13 +00:00
|
|
|
static void
|
|
|
|
nvme_ctrlr_set_state(struct spdk_nvme_ctrlr *ctrlr, enum nvme_ctrlr_state state,
|
|
|
|
uint64_t timeout_in_ms)
|
2015-09-21 15:52:41 +00:00
|
|
|
{
|
2016-02-23 23:36:13 +00:00
|
|
|
ctrlr->state = state;
|
|
|
|
if (timeout_in_ms == NVME_TIMEOUT_INFINITE) {
|
|
|
|
ctrlr->state_timeout_tsc = NVME_TIMEOUT_INFINITE;
|
2015-09-21 15:52:41 +00:00
|
|
|
} else {
|
2016-02-23 23:36:13 +00:00
|
|
|
ctrlr->state_timeout_tsc = nvme_get_tsc() + (timeout_in_ms * nvme_get_tsc_hz()) / 1000;
|
2015-09-21 15:52:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2016-02-10 18:26:12 +00:00
|
|
|
spdk_nvme_ctrlr_reset(struct spdk_nvme_ctrlr *ctrlr)
|
2015-09-21 15:52:41 +00:00
|
|
|
{
|
2016-02-23 23:36:13 +00:00
|
|
|
int rc = 0;
|
|
|
|
uint32_t i;
|
2016-02-29 21:19:02 +00:00
|
|
|
struct spdk_nvme_qpair *qpair;
|
2015-09-21 15:52:41 +00:00
|
|
|
|
2016-08-08 17:03:52 +00:00
|
|
|
pthread_mutex_lock(&ctrlr->ctrlr_lock);
|
2015-09-21 15:52:41 +00:00
|
|
|
|
|
|
|
if (ctrlr->is_resetting || ctrlr->is_failed) {
|
|
|
|
/*
|
|
|
|
* Controller is already resetting or has failed. Return
|
|
|
|
* immediately since there is no need to kick off another
|
|
|
|
* reset in these cases.
|
|
|
|
*/
|
2016-08-08 17:03:52 +00:00
|
|
|
pthread_mutex_unlock(&ctrlr->ctrlr_lock);
|
2015-09-21 15:52:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-11-03 00:51:32 +00:00
|
|
|
ctrlr->is_resetting = true;
|
2015-09-21 15:52:41 +00:00
|
|
|
|
|
|
|
nvme_printf(ctrlr, "resetting controller\n");
|
2016-02-23 23:36:13 +00:00
|
|
|
|
|
|
|
/* Disable all queues before disabling the controller hardware. */
|
|
|
|
nvme_qpair_disable(&ctrlr->adminq);
|
2016-03-07 17:36:17 +00:00
|
|
|
for (i = 0; i < ctrlr->opts.num_io_queues; i++) {
|
2016-02-23 23:36:13 +00:00
|
|
|
nvme_qpair_disable(&ctrlr->ioq[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the state back to INIT to cause a full hardware reset. */
|
|
|
|
nvme_ctrlr_set_state(ctrlr, NVME_CTRLR_STATE_INIT, NVME_TIMEOUT_INFINITE);
|
|
|
|
|
|
|
|
while (ctrlr->state != NVME_CTRLR_STATE_READY) {
|
|
|
|
if (nvme_ctrlr_process_init(ctrlr) != 0) {
|
|
|
|
nvme_printf(ctrlr, "%s: controller reinitialization failed\n", __func__);
|
|
|
|
nvme_ctrlr_fail(ctrlr);
|
|
|
|
rc = -1;
|
|
|
|
break;
|
|
|
|
}
|
2015-09-21 15:52:41 +00:00
|
|
|
}
|
|
|
|
|
2016-02-29 21:19:02 +00:00
|
|
|
if (!ctrlr->is_failed) {
|
|
|
|
/* Reinitialize qpairs */
|
|
|
|
TAILQ_FOREACH(qpair, &ctrlr->active_io_qpairs, tailq) {
|
|
|
|
if (spdk_nvme_ctrlr_create_qpair(ctrlr, qpair) != 0) {
|
|
|
|
nvme_ctrlr_fail(ctrlr);
|
|
|
|
rc = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-03 00:51:32 +00:00
|
|
|
ctrlr->is_resetting = false;
|
2015-09-21 15:52:41 +00:00
|
|
|
|
2016-08-08 17:03:52 +00:00
|
|
|
pthread_mutex_unlock(&ctrlr->ctrlr_lock);
|
2015-09-21 15:52:41 +00:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2016-02-10 18:26:12 +00:00
|
|
|
nvme_ctrlr_identify(struct spdk_nvme_ctrlr *ctrlr)
|
2015-09-21 15:52:41 +00:00
|
|
|
{
|
|
|
|
struct nvme_completion_poll_status status;
|
2016-02-29 21:33:50 +00:00
|
|
|
int rc;
|
2015-09-21 15:52:41 +00:00
|
|
|
|
|
|
|
status.done = false;
|
2016-02-29 21:33:50 +00:00
|
|
|
rc = nvme_ctrlr_cmd_identify_controller(ctrlr, &ctrlr->cdata,
|
|
|
|
nvme_completion_poll_cb, &status);
|
|
|
|
if (rc != 0) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2015-09-21 15:52:41 +00:00
|
|
|
while (status.done == false) {
|
2016-02-29 21:56:53 +00:00
|
|
|
spdk_nvme_qpair_process_completions(&ctrlr->adminq, 0);
|
2015-09-21 15:52:41 +00:00
|
|
|
}
|
2016-02-09 18:06:48 +00:00
|
|
|
if (spdk_nvme_cpl_is_error(&status.cpl)) {
|
2015-09-21 15:52:41 +00:00
|
|
|
nvme_printf(ctrlr, "nvme_identify_controller failed!\n");
|
2016-06-14 22:17:33 +00:00
|
|
|
return -ENXIO;
|
2015-09-21 15:52:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Use MDTS to ensure our default max_xfer_size doesn't exceed what the
|
|
|
|
* controller supports.
|
|
|
|
*/
|
|
|
|
if (ctrlr->cdata.mdts > 0) {
|
|
|
|
ctrlr->max_xfer_size = nvme_min(ctrlr->max_xfer_size,
|
|
|
|
ctrlr->min_page_size * (1 << (ctrlr->cdata.mdts)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2016-02-10 18:26:12 +00:00
|
|
|
nvme_ctrlr_set_num_qpairs(struct spdk_nvme_ctrlr *ctrlr)
|
2015-09-21 15:52:41 +00:00
|
|
|
{
|
|
|
|
struct nvme_completion_poll_status status;
|
|
|
|
int cq_allocated, sq_allocated;
|
2016-02-29 21:33:50 +00:00
|
|
|
int rc;
|
2015-09-21 15:52:41 +00:00
|
|
|
|
|
|
|
status.done = false;
|
|
|
|
|
2016-03-23 23:33:20 +00:00
|
|
|
if (ctrlr->opts.num_io_queues > SPDK_NVME_MAX_IO_QUEUES) {
|
|
|
|
nvme_printf(ctrlr, "Limiting requested num_io_queues %u to max %d\n",
|
|
|
|
ctrlr->opts.num_io_queues, SPDK_NVME_MAX_IO_QUEUES);
|
|
|
|
ctrlr->opts.num_io_queues = SPDK_NVME_MAX_IO_QUEUES;
|
|
|
|
} else if (ctrlr->opts.num_io_queues < 1) {
|
|
|
|
nvme_printf(ctrlr, "Requested num_io_queues 0, increasing to 1\n");
|
|
|
|
ctrlr->opts.num_io_queues = 1;
|
|
|
|
}
|
|
|
|
|
2016-03-07 17:36:17 +00:00
|
|
|
rc = nvme_ctrlr_cmd_set_num_queues(ctrlr, ctrlr->opts.num_io_queues,
|
2016-02-29 21:33:50 +00:00
|
|
|
nvme_completion_poll_cb, &status);
|
|
|
|
if (rc != 0) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2015-09-21 15:52:41 +00:00
|
|
|
while (status.done == false) {
|
2016-02-29 21:56:53 +00:00
|
|
|
spdk_nvme_qpair_process_completions(&ctrlr->adminq, 0);
|
2015-09-21 15:52:41 +00:00
|
|
|
}
|
2016-02-09 18:06:48 +00:00
|
|
|
if (spdk_nvme_cpl_is_error(&status.cpl)) {
|
2015-09-21 15:52:41 +00:00
|
|
|
nvme_printf(ctrlr, "nvme_set_num_queues failed!\n");
|
2016-06-14 22:17:33 +00:00
|
|
|
return -ENXIO;
|
2015-09-21 15:52:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Data in cdw0 is 0-based.
|
|
|
|
* Lower 16-bits indicate number of submission queues allocated.
|
|
|
|
* Upper 16-bits indicate number of completion queues allocated.
|
|
|
|
*/
|
|
|
|
sq_allocated = (status.cpl.cdw0 & 0xFFFF) + 1;
|
|
|
|
cq_allocated = (status.cpl.cdw0 >> 16) + 1;
|
|
|
|
|
2016-03-07 17:36:17 +00:00
|
|
|
ctrlr->opts.num_io_queues = nvme_min(sq_allocated, cq_allocated);
|
2015-09-21 15:52:41 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-02-10 18:26:12 +00:00
|
|
|
nvme_ctrlr_destruct_namespaces(struct spdk_nvme_ctrlr *ctrlr)
|
2015-09-21 15:52:41 +00:00
|
|
|
{
|
|
|
|
if (ctrlr->ns) {
|
|
|
|
uint32_t i, num_ns = ctrlr->num_ns;
|
|
|
|
|
|
|
|
for (i = 0; i < num_ns; i++) {
|
|
|
|
nvme_ns_destruct(&ctrlr->ns[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(ctrlr->ns);
|
|
|
|
ctrlr->ns = NULL;
|
|
|
|
ctrlr->num_ns = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctrlr->nsdata) {
|
|
|
|
nvme_free(ctrlr->nsdata);
|
|
|
|
ctrlr->nsdata = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2016-02-10 18:26:12 +00:00
|
|
|
nvme_ctrlr_construct_namespaces(struct spdk_nvme_ctrlr *ctrlr)
|
2015-09-21 15:52:41 +00:00
|
|
|
{
|
|
|
|
uint32_t i, nn = ctrlr->cdata.nn;
|
|
|
|
uint64_t phys_addr = 0;
|
|
|
|
|
|
|
|
if (nn == 0) {
|
|
|
|
nvme_printf(ctrlr, "controller has 0 namespaces\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ctrlr->num_ns may be 0 (startup) or a different number of namespaces (reset),
|
|
|
|
* so check if we need to reallocate.
|
|
|
|
*/
|
|
|
|
if (nn != ctrlr->num_ns) {
|
|
|
|
nvme_ctrlr_destruct_namespaces(ctrlr);
|
|
|
|
|
2016-02-10 18:26:12 +00:00
|
|
|
ctrlr->ns = calloc(nn, sizeof(struct spdk_nvme_ns));
|
2015-09-21 15:52:41 +00:00
|
|
|
if (ctrlr->ns == NULL) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctrlr->nsdata = nvme_malloc("nvme_namespaces",
|
2016-02-09 18:06:48 +00:00
|
|
|
nn * sizeof(struct spdk_nvme_ns_data), 64,
|
2015-09-21 15:52:41 +00:00
|
|
|
&phys_addr);
|
|
|
|
if (ctrlr->nsdata == NULL) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctrlr->num_ns = nn;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < nn; i++) {
|
2016-02-10 18:26:12 +00:00
|
|
|
struct spdk_nvme_ns *ns = &ctrlr->ns[i];
|
2015-09-21 15:52:41 +00:00
|
|
|
uint32_t nsid = i + 1;
|
|
|
|
|
|
|
|
if (nvme_ns_construct(ns, nsid, ctrlr) != 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
nvme_ctrlr_destruct_namespaces(ctrlr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-02-09 18:06:48 +00:00
|
|
|
nvme_ctrlr_async_event_cb(void *arg, const struct spdk_nvme_cpl *cpl)
|
2015-09-21 15:52:41 +00:00
|
|
|
{
|
|
|
|
struct nvme_async_event_request *aer = arg;
|
2016-02-10 18:26:12 +00:00
|
|
|
struct spdk_nvme_ctrlr *ctrlr = aer->ctrlr;
|
2015-09-21 15:52:41 +00:00
|
|
|
|
2016-02-09 18:06:48 +00:00
|
|
|
if (cpl->status.sc == SPDK_NVME_SC_ABORTED_SQ_DELETION) {
|
2015-09-21 15:52:41 +00:00
|
|
|
/*
|
|
|
|
* This is simulated when controller is being shut down, to
|
|
|
|
* effectively abort outstanding asynchronous event requests
|
|
|
|
* and make sure all memory is freed. Do not repost the
|
|
|
|
* request in this case.
|
|
|
|
*/
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctrlr->aer_cb_fn != NULL) {
|
|
|
|
ctrlr->aer_cb_fn(ctrlr->aer_cb_arg, cpl);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Repost another asynchronous event request to replace the one
|
|
|
|
* that just completed.
|
|
|
|
*/
|
2016-01-06 20:45:25 +00:00
|
|
|
if (nvme_ctrlr_construct_and_submit_aer(ctrlr, aer)) {
|
|
|
|
/*
|
|
|
|
* We can't do anything to recover from a failure here,
|
|
|
|
* so just print a warning message and leave the AER unsubmitted.
|
|
|
|
*/
|
|
|
|
nvme_printf(ctrlr, "resubmitting AER failed!\n");
|
|
|
|
}
|
2015-09-21 15:52:41 +00:00
|
|
|
}
|
|
|
|
|
2016-01-06 20:45:25 +00:00
|
|
|
static int
|
2016-02-10 18:26:12 +00:00
|
|
|
nvme_ctrlr_construct_and_submit_aer(struct spdk_nvme_ctrlr *ctrlr,
|
2015-09-21 15:52:41 +00:00
|
|
|
struct nvme_async_event_request *aer)
|
|
|
|
{
|
|
|
|
struct nvme_request *req;
|
|
|
|
|
|
|
|
aer->ctrlr = ctrlr;
|
2016-01-22 23:56:20 +00:00
|
|
|
req = nvme_allocate_request_null(nvme_ctrlr_async_event_cb, aer);
|
2015-09-21 15:52:41 +00:00
|
|
|
aer->req = req;
|
2016-01-06 20:45:25 +00:00
|
|
|
if (req == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
2015-09-21 15:52:41 +00:00
|
|
|
|
2016-02-09 18:06:48 +00:00
|
|
|
req->cmd.opc = SPDK_NVME_OPC_ASYNC_EVENT_REQUEST;
|
2016-03-08 22:16:09 +00:00
|
|
|
return nvme_ctrlr_submit_admin_request(ctrlr, req);
|
2015-09-21 15:52:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2016-02-10 18:26:12 +00:00
|
|
|
nvme_ctrlr_configure_aer(struct spdk_nvme_ctrlr *ctrlr)
|
2015-09-21 15:52:41 +00:00
|
|
|
{
|
2016-02-09 18:06:48 +00:00
|
|
|
union spdk_nvme_critical_warning_state state;
|
2015-09-21 15:52:41 +00:00
|
|
|
struct nvme_async_event_request *aer;
|
|
|
|
uint32_t i;
|
|
|
|
struct nvme_completion_poll_status status;
|
2016-02-29 21:33:50 +00:00
|
|
|
int rc;
|
2015-09-21 15:52:41 +00:00
|
|
|
|
|
|
|
status.done = false;
|
|
|
|
|
|
|
|
state.raw = 0xFF;
|
|
|
|
state.bits.reserved = 0;
|
2016-02-29 21:33:50 +00:00
|
|
|
rc = nvme_ctrlr_cmd_set_async_event_config(ctrlr, state, nvme_completion_poll_cb, &status);
|
|
|
|
if (rc != 0) {
|
|
|
|
return rc;
|
|
|
|
}
|
2015-09-21 15:52:41 +00:00
|
|
|
|
|
|
|
while (status.done == false) {
|
2016-02-29 21:56:53 +00:00
|
|
|
spdk_nvme_qpair_process_completions(&ctrlr->adminq, 0);
|
2015-09-21 15:52:41 +00:00
|
|
|
}
|
2016-02-09 18:06:48 +00:00
|
|
|
if (spdk_nvme_cpl_is_error(&status.cpl)) {
|
2015-09-21 15:52:41 +00:00
|
|
|
nvme_printf(ctrlr, "nvme_ctrlr_cmd_set_async_event_config failed!\n");
|
2016-06-14 22:17:33 +00:00
|
|
|
return -ENXIO;
|
2015-09-21 15:52:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* aerl is a zero-based value, so we need to add 1 here. */
|
|
|
|
ctrlr->num_aers = nvme_min(NVME_MAX_ASYNC_EVENTS, (ctrlr->cdata.aerl + 1));
|
|
|
|
|
|
|
|
for (i = 0; i < ctrlr->num_aers; i++) {
|
|
|
|
aer = &ctrlr->aer[i];
|
2016-01-06 20:45:25 +00:00
|
|
|
if (nvme_ctrlr_construct_and_submit_aer(ctrlr, aer)) {
|
|
|
|
nvme_printf(ctrlr, "nvme_ctrlr_construct_and_submit_aer failed!\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2015-09-21 15:52:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-23 23:36:13 +00:00
|
|
|
/**
|
|
|
|
* This function will be called repeatedly during initialization until the controller is ready.
|
|
|
|
*/
|
2015-09-21 15:52:41 +00:00
|
|
|
int
|
2016-02-23 23:36:13 +00:00
|
|
|
nvme_ctrlr_process_init(struct spdk_nvme_ctrlr *ctrlr)
|
2015-09-21 15:52:41 +00:00
|
|
|
{
|
2016-02-23 23:36:13 +00:00
|
|
|
union spdk_nvme_cc_register cc;
|
|
|
|
union spdk_nvme_csts_register csts;
|
2016-07-06 22:41:59 +00:00
|
|
|
union spdk_nvme_cap_register cap;
|
2016-02-23 23:36:13 +00:00
|
|
|
uint32_t ready_timeout_in_ms;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
cc.raw = nvme_mmio_read_4(ctrlr, cc.raw);
|
2016-03-01 23:28:29 +00:00
|
|
|
csts.raw = nvme_mmio_read_4(ctrlr, csts.raw);
|
2016-07-06 22:41:59 +00:00
|
|
|
cap.raw = nvme_mmio_read_8(ctrlr, cap.raw);
|
2016-02-23 23:36:13 +00:00
|
|
|
|
2016-07-06 22:41:59 +00:00
|
|
|
ready_timeout_in_ms = 500 * cap.bits.to;
|
2016-02-23 23:36:13 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if the current initialization step is done or has timed out.
|
|
|
|
*/
|
|
|
|
switch (ctrlr->state) {
|
|
|
|
case NVME_CTRLR_STATE_INIT:
|
|
|
|
/* Begin the hardware initialization by making sure the controller is disabled. */
|
|
|
|
if (cc.bits.en) {
|
|
|
|
/*
|
|
|
|
* Controller is currently enabled. We need to disable it to cause a reset.
|
|
|
|
*
|
|
|
|
* If CC.EN = 1 && CSTS.RDY = 0, the controller is in the process of becoming ready.
|
|
|
|
* Wait for the ready bit to be 1 before disabling the controller.
|
|
|
|
*/
|
|
|
|
if (csts.bits.rdy == 0) {
|
|
|
|
nvme_ctrlr_set_state(ctrlr, NVME_CTRLR_STATE_DISABLE_WAIT_FOR_READY_1, ready_timeout_in_ms);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* CC.EN = 1 && CSTS.RDY == 1, so we can immediately disable the controller. */
|
|
|
|
cc.bits.en = 0;
|
|
|
|
nvme_mmio_write_4(ctrlr, cc.raw, cc.raw);
|
|
|
|
nvme_ctrlr_set_state(ctrlr, NVME_CTRLR_STATE_DISABLE_WAIT_FOR_READY_0, ready_timeout_in_ms);
|
|
|
|
return 0;
|
|
|
|
} else {
|
2016-04-08 16:13:10 +00:00
|
|
|
if (csts.bits.rdy == 1) {
|
|
|
|
/*
|
|
|
|
* Controller is in the process of shutting down.
|
|
|
|
* We need to wait for RDY to become 0.
|
|
|
|
*/
|
|
|
|
nvme_ctrlr_set_state(ctrlr, NVME_CTRLR_STATE_DISABLE_WAIT_FOR_READY_0, ready_timeout_in_ms);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-23 23:36:13 +00:00
|
|
|
/*
|
|
|
|
* Controller is currently disabled. We can jump straight to enabling it.
|
|
|
|
*/
|
2016-06-21 20:37:15 +00:00
|
|
|
rc = nvme_ctrlr_enable(ctrlr);
|
2016-02-23 23:36:13 +00:00
|
|
|
nvme_ctrlr_set_state(ctrlr, NVME_CTRLR_STATE_ENABLE_WAIT_FOR_READY_1, ready_timeout_in_ms);
|
2016-06-21 20:37:15 +00:00
|
|
|
return rc;
|
2016-02-23 23:36:13 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NVME_CTRLR_STATE_DISABLE_WAIT_FOR_READY_1:
|
|
|
|
if (csts.bits.rdy == 1) {
|
|
|
|
/* CC.EN = 1 && CSTS.RDY = 1, so we can set CC.EN = 0 now. */
|
|
|
|
cc.bits.en = 0;
|
|
|
|
nvme_mmio_write_4(ctrlr, cc.raw, cc.raw);
|
|
|
|
nvme_ctrlr_set_state(ctrlr, NVME_CTRLR_STATE_DISABLE_WAIT_FOR_READY_0, ready_timeout_in_ms);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NVME_CTRLR_STATE_DISABLE_WAIT_FOR_READY_0:
|
|
|
|
if (csts.bits.rdy == 0) {
|
|
|
|
/* CC.EN = 0 && CSTS.RDY = 0, so we can enable the controller now. */
|
2016-06-21 20:37:15 +00:00
|
|
|
rc = nvme_ctrlr_enable(ctrlr);
|
2016-02-23 23:36:13 +00:00
|
|
|
nvme_ctrlr_set_state(ctrlr, NVME_CTRLR_STATE_ENABLE_WAIT_FOR_READY_1, ready_timeout_in_ms);
|
2016-06-21 20:37:15 +00:00
|
|
|
return rc;
|
2016-02-23 23:36:13 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NVME_CTRLR_STATE_ENABLE_WAIT_FOR_READY_1:
|
|
|
|
if (csts.bits.rdy == 1) {
|
|
|
|
/*
|
|
|
|
* The controller has been enabled.
|
|
|
|
* Perform the rest of initialization in nvme_ctrlr_start() serially.
|
|
|
|
*/
|
|
|
|
rc = nvme_ctrlr_start(ctrlr);
|
|
|
|
nvme_ctrlr_set_state(ctrlr, NVME_CTRLR_STATE_READY, NVME_TIMEOUT_INFINITE);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
nvme_assert(0, ("unhandled ctrlr state %d\n", ctrlr->state));
|
|
|
|
nvme_ctrlr_fail(ctrlr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctrlr->state_timeout_tsc != NVME_TIMEOUT_INFINITE &&
|
|
|
|
nvme_get_tsc() > ctrlr->state_timeout_tsc) {
|
|
|
|
nvme_printf(ctrlr, "Initialization timed out in state %d\n", ctrlr->state);
|
|
|
|
nvme_ctrlr_fail(ctrlr);
|
2015-09-21 15:52:41 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-02-23 23:36:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
nvme_ctrlr_start(struct spdk_nvme_ctrlr *ctrlr)
|
|
|
|
{
|
2015-09-21 15:52:41 +00:00
|
|
|
nvme_qpair_reset(&ctrlr->adminq);
|
|
|
|
|
|
|
|
nvme_qpair_enable(&ctrlr->adminq);
|
|
|
|
|
|
|
|
if (nvme_ctrlr_identify(ctrlr) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nvme_ctrlr_set_num_qpairs(ctrlr) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-02-29 21:19:02 +00:00
|
|
|
if (nvme_ctrlr_construct_io_qpairs(ctrlr)) {
|
2015-09-21 15:52:41 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nvme_ctrlr_construct_namespaces(ctrlr) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nvme_ctrlr_configure_aer(ctrlr) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-01-06 05:43:33 +00:00
|
|
|
nvme_ctrlr_set_supported_log_pages(ctrlr);
|
2016-01-25 05:04:23 +00:00
|
|
|
nvme_ctrlr_set_supported_features(ctrlr);
|
2016-03-01 02:50:31 +00:00
|
|
|
|
|
|
|
if (ctrlr->cdata.sgls.supported) {
|
|
|
|
ctrlr->flags |= SPDK_NVME_CTRLR_SGL_SUPPORTED;
|
|
|
|
}
|
|
|
|
|
2015-09-21 15:52:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-05 01:41:16 +00:00
|
|
|
static void
|
|
|
|
nvme_ctrlr_map_cmb(struct spdk_nvme_ctrlr *ctrlr)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
void *addr;
|
|
|
|
uint32_t bir;
|
|
|
|
union spdk_nvme_cmbsz_register cmbsz;
|
|
|
|
union spdk_nvme_cmbloc_register cmbloc;
|
|
|
|
uint64_t size, unit_size, offset, bar_size, bar_phys_addr;
|
|
|
|
|
|
|
|
cmbsz.raw = nvme_mmio_read_4(ctrlr, cmbsz.raw);
|
|
|
|
cmbloc.raw = nvme_mmio_read_4(ctrlr, cmbloc.raw);
|
|
|
|
if (!cmbsz.bits.sz)
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
bir = cmbloc.bits.bir;
|
|
|
|
/* Values 0 2 3 4 5 are valid for BAR */
|
|
|
|
if (bir > 5 || bir == 1)
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
/* unit size for 4KB/64KB/1MB/16MB/256MB/4GB/64GB */
|
|
|
|
unit_size = (uint64_t)1 << (12 + 4 * cmbsz.bits.szu);
|
|
|
|
/* controller memory buffer size in Bytes */
|
|
|
|
size = unit_size * cmbsz.bits.sz;
|
|
|
|
/* controller memory buffer offset from BAR in Bytes */
|
|
|
|
offset = unit_size * cmbloc.bits.ofst;
|
|
|
|
|
|
|
|
nvme_pcicfg_get_bar_addr_len(ctrlr->devhandle, bir, &bar_phys_addr, &bar_size);
|
|
|
|
|
|
|
|
if (offset > bar_size)
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
if (size > bar_size - offset)
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
rc = nvme_pcicfg_map_bar_write_combine(ctrlr->devhandle, bir, &addr);
|
2016-06-23 18:06:54 +00:00
|
|
|
if ((rc != 0) || addr == NULL)
|
2016-05-05 01:41:16 +00:00
|
|
|
goto exit;
|
|
|
|
|
|
|
|
ctrlr->cmb_bar_virt_addr = addr;
|
|
|
|
ctrlr->cmb_bar_phys_addr = bar_phys_addr;
|
|
|
|
ctrlr->cmb_size = size;
|
|
|
|
ctrlr->cmb_current_offset = offset;
|
|
|
|
|
2016-05-12 16:40:21 +00:00
|
|
|
if (!cmbsz.bits.sqs) {
|
2016-05-05 01:41:16 +00:00
|
|
|
ctrlr->opts.use_cmb_sqs = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
exit:
|
|
|
|
ctrlr->cmb_bar_virt_addr = NULL;
|
|
|
|
ctrlr->opts.use_cmb_sqs = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
nvme_ctrlr_unmap_cmb(struct spdk_nvme_ctrlr *ctrlr)
|
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
union spdk_nvme_cmbloc_register cmbloc;
|
|
|
|
void *addr = ctrlr->cmb_bar_virt_addr;
|
|
|
|
|
|
|
|
if (addr) {
|
|
|
|
cmbloc.raw = nvme_mmio_read_4(ctrlr, cmbloc.raw);
|
|
|
|
rc = nvme_pcicfg_unmap_bar(ctrlr->devhandle, cmbloc.bits.bir, addr);
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
nvme_ctrlr_alloc_cmb(struct spdk_nvme_ctrlr *ctrlr, uint64_t length, uint64_t aligned,
|
|
|
|
uint64_t *offset)
|
|
|
|
{
|
|
|
|
uint64_t round_offset;
|
|
|
|
|
|
|
|
round_offset = ctrlr->cmb_current_offset;
|
|
|
|
round_offset = (round_offset + (aligned - 1)) & ~(aligned - 1);
|
|
|
|
|
|
|
|
if (round_offset + length > ctrlr->cmb_size)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
*offset = round_offset;
|
|
|
|
ctrlr->cmb_current_offset = round_offset + length;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-09-21 15:52:41 +00:00
|
|
|
static int
|
2016-02-10 18:26:12 +00:00
|
|
|
nvme_ctrlr_allocate_bars(struct spdk_nvme_ctrlr *ctrlr)
|
2015-09-21 15:52:41 +00:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
void *addr;
|
|
|
|
|
|
|
|
rc = nvme_pcicfg_map_bar(ctrlr->devhandle, 0, 0 /* writable */, &addr);
|
2016-02-09 18:06:48 +00:00
|
|
|
ctrlr->regs = (volatile struct spdk_nvme_registers *)addr;
|
2015-09-21 15:52:41 +00:00
|
|
|
if ((ctrlr->regs == NULL) || (rc != 0)) {
|
2015-09-23 23:25:16 +00:00
|
|
|
nvme_printf(ctrlr, "pci_device_map_range failed with error code %d\n", rc);
|
2015-09-21 15:52:41 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-05-05 01:41:16 +00:00
|
|
|
nvme_ctrlr_map_cmb(ctrlr);
|
|
|
|
|
2015-09-21 15:52:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2016-02-10 18:26:12 +00:00
|
|
|
nvme_ctrlr_free_bars(struct spdk_nvme_ctrlr *ctrlr)
|
2015-09-21 15:52:41 +00:00
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
void *addr = (void *)ctrlr->regs;
|
|
|
|
|
2016-05-05 01:41:16 +00:00
|
|
|
rc = nvme_ctrlr_unmap_cmb(ctrlr);
|
|
|
|
if (rc != 0) {
|
|
|
|
nvme_printf(ctrlr, "nvme_ctrlr_unmap_cmb failed with error code %d\n", rc);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-09-21 15:52:41 +00:00
|
|
|
if (addr) {
|
|
|
|
rc = nvme_pcicfg_unmap_bar(ctrlr->devhandle, 0, addr);
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2016-08-08 17:03:52 +00:00
|
|
|
static inline int
|
|
|
|
pthread_mutex_init_recursive(pthread_mutex_t *mtx)
|
|
|
|
{
|
|
|
|
pthread_mutexattr_t attr;
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
if (pthread_mutexattr_init(&attr)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) ||
|
|
|
|
pthread_mutex_init(mtx, &attr)) {
|
|
|
|
rc = -1;
|
|
|
|
}
|
|
|
|
pthread_mutexattr_destroy(&attr);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2015-09-21 15:52:41 +00:00
|
|
|
int
|
2016-02-10 18:26:12 +00:00
|
|
|
nvme_ctrlr_construct(struct spdk_nvme_ctrlr *ctrlr, void *devhandle)
|
2015-09-21 15:52:41 +00:00
|
|
|
{
|
2016-07-06 22:41:59 +00:00
|
|
|
union spdk_nvme_cap_register cap;
|
2015-09-21 15:52:41 +00:00
|
|
|
uint32_t cmd_reg;
|
|
|
|
int status;
|
|
|
|
int rc;
|
|
|
|
|
2016-02-23 23:36:13 +00:00
|
|
|
nvme_ctrlr_set_state(ctrlr, NVME_CTRLR_STATE_INIT, NVME_TIMEOUT_INFINITE);
|
2015-09-21 15:52:41 +00:00
|
|
|
ctrlr->devhandle = devhandle;
|
2016-05-05 01:41:16 +00:00
|
|
|
ctrlr->flags = 0;
|
2015-09-21 15:52:41 +00:00
|
|
|
|
|
|
|
status = nvme_ctrlr_allocate_bars(ctrlr);
|
|
|
|
if (status != 0) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2016-08-17 06:17:17 +00:00
|
|
|
/* Enable PCI busmaster and disable INTx */
|
2015-09-21 15:52:41 +00:00
|
|
|
nvme_pcicfg_read32(devhandle, &cmd_reg, 4);
|
2016-08-17 06:17:17 +00:00
|
|
|
cmd_reg |= 0x0404;
|
2015-09-21 15:52:41 +00:00
|
|
|
nvme_pcicfg_write32(devhandle, cmd_reg, 4);
|
|
|
|
|
2016-07-06 22:41:59 +00:00
|
|
|
cap.raw = nvme_mmio_read_8(ctrlr, cap.raw);
|
2015-09-21 15:52:41 +00:00
|
|
|
|
|
|
|
/* Doorbell stride is 2 ^ (dstrd + 2),
|
|
|
|
* but we want multiples of 4, so drop the + 2 */
|
2016-07-06 22:41:59 +00:00
|
|
|
ctrlr->doorbell_stride_u32 = 1 << cap.bits.dstrd;
|
2015-09-21 15:52:41 +00:00
|
|
|
|
2016-07-06 22:41:59 +00:00
|
|
|
ctrlr->min_page_size = 1 << (12 + cap.bits.mpsmin);
|
2015-09-21 15:52:41 +00:00
|
|
|
|
|
|
|
rc = nvme_ctrlr_construct_admin_qpair(ctrlr);
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
|
2015-11-03 00:51:32 +00:00
|
|
|
ctrlr->is_resetting = false;
|
2015-09-21 15:52:41 +00:00
|
|
|
ctrlr->is_failed = false;
|
|
|
|
|
2016-02-29 21:19:02 +00:00
|
|
|
TAILQ_INIT(&ctrlr->free_io_qpairs);
|
|
|
|
TAILQ_INIT(&ctrlr->active_io_qpairs);
|
|
|
|
|
2016-08-08 17:03:52 +00:00
|
|
|
pthread_mutex_init_recursive(&ctrlr->ctrlr_lock);
|
2015-09-21 15:52:41 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-02-10 18:26:12 +00:00
|
|
|
nvme_ctrlr_destruct(struct spdk_nvme_ctrlr *ctrlr)
|
2015-09-21 15:52:41 +00:00
|
|
|
{
|
2015-11-02 19:58:19 +00:00
|
|
|
uint32_t i;
|
2015-09-21 15:52:41 +00:00
|
|
|
|
2016-04-08 22:22:46 +00:00
|
|
|
while (!TAILQ_EMPTY(&ctrlr->active_io_qpairs)) {
|
|
|
|
struct spdk_nvme_qpair *qpair = TAILQ_FIRST(&ctrlr->active_io_qpairs);
|
|
|
|
|
|
|
|
spdk_nvme_ctrlr_free_io_qpair(qpair);
|
|
|
|
}
|
|
|
|
|
2015-09-21 15:52:41 +00:00
|
|
|
nvme_ctrlr_shutdown(ctrlr);
|
|
|
|
|
|
|
|
nvme_ctrlr_destruct_namespaces(ctrlr);
|
2016-03-23 04:38:40 +00:00
|
|
|
if (ctrlr->ioq) {
|
|
|
|
for (i = 0; i < ctrlr->opts.num_io_queues; i++) {
|
|
|
|
nvme_qpair_destroy(&ctrlr->ioq[i]);
|
|
|
|
}
|
2015-09-21 15:52:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
free(ctrlr->ioq);
|
|
|
|
|
|
|
|
nvme_qpair_destroy(&ctrlr->adminq);
|
|
|
|
|
|
|
|
nvme_ctrlr_free_bars(ctrlr);
|
2016-08-08 17:03:52 +00:00
|
|
|
pthread_mutex_destroy(&ctrlr->ctrlr_lock);
|
2015-09-21 15:52:41 +00:00
|
|
|
}
|
|
|
|
|
2016-03-08 22:16:09 +00:00
|
|
|
int
|
2016-02-10 18:26:12 +00:00
|
|
|
nvme_ctrlr_submit_admin_request(struct spdk_nvme_ctrlr *ctrlr,
|
2015-09-21 15:52:41 +00:00
|
|
|
struct nvme_request *req)
|
|
|
|
{
|
2016-03-08 22:16:09 +00:00
|
|
|
return nvme_qpair_submit_request(&ctrlr->adminq, req);
|
2015-09-21 15:52:41 +00:00
|
|
|
}
|
|
|
|
|
2016-01-11 16:10:02 +00:00
|
|
|
int32_t
|
2016-02-10 18:26:12 +00:00
|
|
|
spdk_nvme_ctrlr_process_admin_completions(struct spdk_nvme_ctrlr *ctrlr)
|
2015-09-21 15:52:41 +00:00
|
|
|
{
|
2016-01-11 16:10:02 +00:00
|
|
|
int32_t num_completions;
|
|
|
|
|
2016-08-08 17:03:52 +00:00
|
|
|
pthread_mutex_lock(&ctrlr->ctrlr_lock);
|
2016-02-29 21:56:53 +00:00
|
|
|
num_completions = spdk_nvme_qpair_process_completions(&ctrlr->adminq, 0);
|
2016-08-08 17:03:52 +00:00
|
|
|
pthread_mutex_unlock(&ctrlr->ctrlr_lock);
|
2016-01-11 16:10:02 +00:00
|
|
|
|
|
|
|
return num_completions;
|
2015-09-21 15:52:41 +00:00
|
|
|
}
|
|
|
|
|
2016-02-09 18:06:48 +00:00
|
|
|
const struct spdk_nvme_ctrlr_data *
|
2016-02-10 18:26:12 +00:00
|
|
|
spdk_nvme_ctrlr_get_data(struct spdk_nvme_ctrlr *ctrlr)
|
2015-09-21 15:52:41 +00:00
|
|
|
{
|
|
|
|
return &ctrlr->cdata;
|
|
|
|
}
|
|
|
|
|
2016-07-06 21:55:28 +00:00
|
|
|
union spdk_nvme_cap_register spdk_nvme_ctrlr_get_regs_cap(struct spdk_nvme_ctrlr *ctrlr)
|
|
|
|
{
|
|
|
|
union spdk_nvme_cap_register cap;
|
|
|
|
|
|
|
|
cap.raw = nvme_mmio_read_8(ctrlr, cap.raw);
|
|
|
|
return cap;
|
|
|
|
}
|
|
|
|
|
|
|
|
union spdk_nvme_vs_register spdk_nvme_ctrlr_get_regs_vs(struct spdk_nvme_ctrlr *ctrlr)
|
|
|
|
{
|
|
|
|
union spdk_nvme_vs_register vs;
|
|
|
|
|
|
|
|
vs.raw = nvme_mmio_read_4(ctrlr, vs.raw);
|
|
|
|
return vs;
|
|
|
|
}
|
|
|
|
|
2015-09-21 15:52:41 +00:00
|
|
|
uint32_t
|
2016-02-10 18:26:12 +00:00
|
|
|
spdk_nvme_ctrlr_get_num_ns(struct spdk_nvme_ctrlr *ctrlr)
|
2015-09-21 15:52:41 +00:00
|
|
|
{
|
|
|
|
return ctrlr->num_ns;
|
|
|
|
}
|
|
|
|
|
2016-02-10 18:26:12 +00:00
|
|
|
struct spdk_nvme_ns *
|
|
|
|
spdk_nvme_ctrlr_get_ns(struct spdk_nvme_ctrlr *ctrlr, uint32_t ns_id)
|
2015-09-21 15:52:41 +00:00
|
|
|
{
|
|
|
|
if (ns_id < 1 || ns_id > ctrlr->num_ns) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ctrlr->ns[ns_id - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-02-10 18:26:12 +00:00
|
|
|
spdk_nvme_ctrlr_register_aer_callback(struct spdk_nvme_ctrlr *ctrlr,
|
|
|
|
spdk_nvme_aer_cb aer_cb_fn,
|
|
|
|
void *aer_cb_arg)
|
2015-09-21 15:52:41 +00:00
|
|
|
{
|
|
|
|
ctrlr->aer_cb_fn = aer_cb_fn;
|
|
|
|
ctrlr->aer_cb_arg = aer_cb_arg;
|
|
|
|
}
|
2016-01-06 05:43:33 +00:00
|
|
|
|
|
|
|
bool
|
2016-02-10 18:26:12 +00:00
|
|
|
spdk_nvme_ctrlr_is_log_page_supported(struct spdk_nvme_ctrlr *ctrlr, uint8_t log_page)
|
2016-01-06 05:43:33 +00:00
|
|
|
{
|
2016-01-15 20:21:18 +00:00
|
|
|
/* No bounds check necessary, since log_page is uint8_t and log_page_supported has 256 entries */
|
|
|
|
SPDK_STATIC_ASSERT(sizeof(ctrlr->log_page_supported) == 256, "log_page_supported size mismatch");
|
|
|
|
return ctrlr->log_page_supported[log_page];
|
2016-01-06 05:43:33 +00:00
|
|
|
}
|
2016-01-25 05:04:23 +00:00
|
|
|
|
|
|
|
bool
|
2016-02-10 18:26:12 +00:00
|
|
|
spdk_nvme_ctrlr_is_feature_supported(struct spdk_nvme_ctrlr *ctrlr, uint8_t feature_code)
|
2016-01-25 05:04:23 +00:00
|
|
|
{
|
|
|
|
/* No bounds check necessary, since feature_code is uint8_t and feature_supported has 256 entries */
|
|
|
|
SPDK_STATIC_ASSERT(sizeof(ctrlr->feature_supported) == 256, "feature_supported size mismatch");
|
|
|
|
return ctrlr->feature_supported[feature_code];
|
|
|
|
}
|
2016-02-25 03:44:44 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
spdk_nvme_ctrlr_attach_ns(struct spdk_nvme_ctrlr *ctrlr, uint32_t nsid,
|
|
|
|
struct spdk_nvme_ctrlr_list *payload)
|
|
|
|
{
|
|
|
|
struct nvme_completion_poll_status status;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
status.done = false;
|
|
|
|
res = nvme_ctrlr_cmd_attach_ns(ctrlr, nsid, payload,
|
|
|
|
nvme_completion_poll_cb, &status);
|
|
|
|
if (res)
|
|
|
|
return res;
|
|
|
|
while (status.done == false) {
|
2016-08-08 17:03:52 +00:00
|
|
|
pthread_mutex_lock(&ctrlr->ctrlr_lock);
|
2016-02-25 03:44:44 +00:00
|
|
|
spdk_nvme_qpair_process_completions(&ctrlr->adminq, 0);
|
2016-08-08 17:03:52 +00:00
|
|
|
pthread_mutex_unlock(&ctrlr->ctrlr_lock);
|
2016-02-25 03:44:44 +00:00
|
|
|
}
|
|
|
|
if (spdk_nvme_cpl_is_error(&status.cpl)) {
|
|
|
|
nvme_printf(ctrlr, "spdk_nvme_ctrlr_attach_ns failed!\n");
|
2016-06-14 22:17:33 +00:00
|
|
|
return -ENXIO;
|
2016-02-25 03:44:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return spdk_nvme_ctrlr_reset(ctrlr);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
spdk_nvme_ctrlr_detach_ns(struct spdk_nvme_ctrlr *ctrlr, uint32_t nsid,
|
|
|
|
struct spdk_nvme_ctrlr_list *payload)
|
|
|
|
{
|
|
|
|
struct nvme_completion_poll_status status;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
status.done = false;
|
|
|
|
res = nvme_ctrlr_cmd_detach_ns(ctrlr, nsid, payload,
|
|
|
|
nvme_completion_poll_cb, &status);
|
|
|
|
if (res)
|
|
|
|
return res;
|
|
|
|
while (status.done == false) {
|
2016-08-08 17:03:52 +00:00
|
|
|
pthread_mutex_lock(&ctrlr->ctrlr_lock);
|
2016-02-25 03:44:44 +00:00
|
|
|
spdk_nvme_qpair_process_completions(&ctrlr->adminq, 0);
|
2016-08-08 17:03:52 +00:00
|
|
|
pthread_mutex_unlock(&ctrlr->ctrlr_lock);
|
2016-02-25 03:44:44 +00:00
|
|
|
}
|
|
|
|
if (spdk_nvme_cpl_is_error(&status.cpl)) {
|
|
|
|
nvme_printf(ctrlr, "spdk_nvme_ctrlr_detach_ns failed!\n");
|
2016-06-14 22:17:33 +00:00
|
|
|
return -ENXIO;
|
2016-02-25 03:44:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return spdk_nvme_ctrlr_reset(ctrlr);
|
|
|
|
}
|
|
|
|
|
2016-05-19 00:05:38 +00:00
|
|
|
uint32_t
|
2016-02-25 03:44:44 +00:00
|
|
|
spdk_nvme_ctrlr_create_ns(struct spdk_nvme_ctrlr *ctrlr, struct spdk_nvme_ns_data *payload)
|
|
|
|
{
|
|
|
|
struct nvme_completion_poll_status status;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
status.done = false;
|
|
|
|
res = nvme_ctrlr_cmd_create_ns(ctrlr, payload, nvme_completion_poll_cb, &status);
|
|
|
|
if (res)
|
2016-05-19 00:05:38 +00:00
|
|
|
return 0;
|
2016-02-25 03:44:44 +00:00
|
|
|
while (status.done == false) {
|
2016-08-08 17:03:52 +00:00
|
|
|
pthread_mutex_lock(&ctrlr->ctrlr_lock);
|
2016-02-25 03:44:44 +00:00
|
|
|
spdk_nvme_qpair_process_completions(&ctrlr->adminq, 0);
|
2016-08-08 17:03:52 +00:00
|
|
|
pthread_mutex_unlock(&ctrlr->ctrlr_lock);
|
2016-02-25 03:44:44 +00:00
|
|
|
}
|
|
|
|
if (spdk_nvme_cpl_is_error(&status.cpl)) {
|
|
|
|
nvme_printf(ctrlr, "spdk_nvme_ctrlr_create_ns failed!\n");
|
2016-05-19 00:05:38 +00:00
|
|
|
return 0;
|
2016-02-25 03:44:44 +00:00
|
|
|
}
|
|
|
|
|
2016-05-19 00:05:38 +00:00
|
|
|
res = spdk_nvme_ctrlr_reset(ctrlr);
|
|
|
|
if (res) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the namespace ID that was created */
|
|
|
|
return status.cpl.cdw0;
|
2016-02-25 03:44:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
spdk_nvme_ctrlr_delete_ns(struct spdk_nvme_ctrlr *ctrlr, uint32_t nsid)
|
|
|
|
{
|
|
|
|
struct nvme_completion_poll_status status;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
status.done = false;
|
|
|
|
res = nvme_ctrlr_cmd_delete_ns(ctrlr, nsid, nvme_completion_poll_cb, &status);
|
|
|
|
if (res)
|
|
|
|
return res;
|
|
|
|
while (status.done == false) {
|
2016-08-08 17:03:52 +00:00
|
|
|
pthread_mutex_lock(&ctrlr->ctrlr_lock);
|
2016-02-25 03:44:44 +00:00
|
|
|
spdk_nvme_qpair_process_completions(&ctrlr->adminq, 0);
|
2016-08-08 17:03:52 +00:00
|
|
|
pthread_mutex_unlock(&ctrlr->ctrlr_lock);
|
2016-02-25 03:44:44 +00:00
|
|
|
}
|
|
|
|
if (spdk_nvme_cpl_is_error(&status.cpl)) {
|
|
|
|
nvme_printf(ctrlr, "spdk_nvme_ctrlr_delete_ns failed!\n");
|
2016-06-14 22:17:33 +00:00
|
|
|
return -ENXIO;
|
2016-02-25 03:44:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return spdk_nvme_ctrlr_reset(ctrlr);
|
|
|
|
}
|
2016-03-07 06:29:50 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
spdk_nvme_ctrlr_format(struct spdk_nvme_ctrlr *ctrlr, uint32_t nsid,
|
|
|
|
struct spdk_nvme_format *format)
|
|
|
|
{
|
|
|
|
struct nvme_completion_poll_status status;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
status.done = false;
|
|
|
|
res = nvme_ctrlr_cmd_format(ctrlr, nsid, format, nvme_completion_poll_cb,
|
|
|
|
&status);
|
|
|
|
if (res)
|
|
|
|
return res;
|
|
|
|
while (status.done == false) {
|
2016-08-08 17:03:52 +00:00
|
|
|
pthread_mutex_lock(&ctrlr->ctrlr_lock);
|
2016-03-07 06:29:50 +00:00
|
|
|
spdk_nvme_qpair_process_completions(&ctrlr->adminq, 0);
|
2016-08-08 17:03:52 +00:00
|
|
|
pthread_mutex_unlock(&ctrlr->ctrlr_lock);
|
2016-03-07 06:29:50 +00:00
|
|
|
}
|
|
|
|
if (spdk_nvme_cpl_is_error(&status.cpl)) {
|
|
|
|
nvme_printf(ctrlr, "spdk_nvme_ctrlr_format failed!\n");
|
2016-06-14 22:17:33 +00:00
|
|
|
return -ENXIO;
|
2016-03-07 06:29:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return spdk_nvme_ctrlr_reset(ctrlr);
|
|
|
|
}
|
2016-05-03 05:18:39 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
spdk_nvme_ctrlr_update_firmware(struct spdk_nvme_ctrlr *ctrlr, void *payload, uint32_t size,
|
|
|
|
int slot)
|
|
|
|
{
|
|
|
|
struct spdk_nvme_fw_commit fw_commit;
|
|
|
|
struct nvme_completion_poll_status status;
|
|
|
|
int res;
|
|
|
|
unsigned int size_remaining;
|
|
|
|
unsigned int offset;
|
|
|
|
unsigned int transfer;
|
|
|
|
void *p;
|
|
|
|
|
|
|
|
if (size % 4) {
|
|
|
|
nvme_printf(ctrlr, "spdk_nvme_ctrlr_update_firmware invalid size!\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Firmware download */
|
|
|
|
size_remaining = size;
|
|
|
|
offset = 0;
|
|
|
|
p = payload;
|
|
|
|
|
|
|
|
while (size_remaining > 0) {
|
|
|
|
transfer = nvme_min(size_remaining, ctrlr->min_page_size);
|
|
|
|
status.done = false;
|
|
|
|
|
|
|
|
res = nvme_ctrlr_cmd_fw_image_download(ctrlr, transfer, offset, p,
|
|
|
|
nvme_completion_poll_cb,
|
|
|
|
&status);
|
|
|
|
if (res)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
while (status.done == false) {
|
2016-08-08 17:03:52 +00:00
|
|
|
pthread_mutex_lock(&ctrlr->ctrlr_lock);
|
2016-05-03 05:18:39 +00:00
|
|
|
spdk_nvme_qpair_process_completions(&ctrlr->adminq, 0);
|
2016-08-08 17:03:52 +00:00
|
|
|
pthread_mutex_unlock(&ctrlr->ctrlr_lock);
|
2016-05-03 05:18:39 +00:00
|
|
|
}
|
|
|
|
if (spdk_nvme_cpl_is_error(&status.cpl)) {
|
|
|
|
nvme_printf(ctrlr, "spdk_nvme_ctrlr_fw_image_download failed!\n");
|
2016-06-14 22:17:33 +00:00
|
|
|
return -ENXIO;
|
2016-05-03 05:18:39 +00:00
|
|
|
}
|
|
|
|
p += transfer;
|
|
|
|
offset += transfer;
|
|
|
|
size_remaining -= transfer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Firmware commit */
|
|
|
|
memset(&fw_commit, 0, sizeof(struct spdk_nvme_fw_commit));
|
|
|
|
fw_commit.fs = slot;
|
|
|
|
fw_commit.ca = SPDK_NVME_FW_COMMIT_REPLACE_IMG;
|
|
|
|
|
|
|
|
status.done = false;
|
|
|
|
|
|
|
|
res = nvme_ctrlr_cmd_fw_commit(ctrlr, &fw_commit, nvme_completion_poll_cb,
|
|
|
|
&status);
|
|
|
|
if (res)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
while (status.done == false) {
|
2016-08-08 17:03:52 +00:00
|
|
|
pthread_mutex_lock(&ctrlr->ctrlr_lock);
|
2016-05-03 05:18:39 +00:00
|
|
|
spdk_nvme_qpair_process_completions(&ctrlr->adminq, 0);
|
2016-08-08 17:03:52 +00:00
|
|
|
pthread_mutex_unlock(&ctrlr->ctrlr_lock);
|
2016-05-03 05:18:39 +00:00
|
|
|
}
|
|
|
|
if (spdk_nvme_cpl_is_error(&status.cpl)) {
|
|
|
|
nvme_printf(ctrlr, "nvme_ctrlr_cmd_fw_commit failed!\n");
|
2016-06-14 22:17:33 +00:00
|
|
|
return -ENXIO;
|
2016-05-03 05:18:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return spdk_nvme_ctrlr_reset(ctrlr);
|
|
|
|
}
|