2016-10-10 03:58:17 +00:00
|
|
|
/*-
|
|
|
|
* BSD LICENSE
|
|
|
|
*
|
2019-11-18 17:11:39 +00:00
|
|
|
* Copyright (c) Intel Corporation. All rights reserved.
|
|
|
|
* Copyright (c) 2019 Mellanox Technologies LTD. All rights reserved.
|
2016-10-10 03:58:17 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2017-05-02 18:18:25 +00:00
|
|
|
#include "spdk/stdinc.h"
|
2017-01-25 23:05:09 +00:00
|
|
|
|
2017-07-13 04:08:53 +00:00
|
|
|
#include "bdev_nvme.h"
|
2019-02-27 10:00:22 +00:00
|
|
|
#include "common.h"
|
2016-12-12 07:42:43 +00:00
|
|
|
|
2019-10-24 18:09:47 +00:00
|
|
|
#include "spdk/config.h"
|
|
|
|
|
2016-12-12 07:42:43 +00:00
|
|
|
#include "spdk/string.h"
|
2016-10-10 03:58:17 +00:00
|
|
|
#include "spdk/rpc.h"
|
2017-03-03 20:44:04 +00:00
|
|
|
#include "spdk/util.h"
|
2016-10-10 03:58:17 +00:00
|
|
|
|
2020-10-06 16:16:26 +00:00
|
|
|
#include "spdk/log.h"
|
2018-05-23 21:01:03 +00:00
|
|
|
#include "spdk/bdev_module.h"
|
2017-07-11 23:32:32 +00:00
|
|
|
|
|
|
|
struct open_descriptors {
|
|
|
|
void *desc;
|
|
|
|
struct spdk_bdev *bdev;
|
|
|
|
TAILQ_ENTRY(open_descriptors) tqlst;
|
2020-02-21 14:58:08 +00:00
|
|
|
struct spdk_thread *thread;
|
2017-07-11 23:32:32 +00:00
|
|
|
};
|
|
|
|
typedef TAILQ_HEAD(, open_descriptors) open_descriptors_t;
|
2016-11-07 22:10:28 +00:00
|
|
|
|
2018-07-09 21:04:33 +00:00
|
|
|
static int
|
|
|
|
rpc_decode_action_on_timeout(const struct spdk_json_val *val, void *out)
|
|
|
|
{
|
|
|
|
enum spdk_bdev_timeout_action *action = out;
|
|
|
|
|
|
|
|
if (spdk_json_strequal(val, "none") == true) {
|
|
|
|
*action = SPDK_BDEV_NVME_TIMEOUT_ACTION_NONE;
|
|
|
|
} else if (spdk_json_strequal(val, "abort") == true) {
|
|
|
|
*action = SPDK_BDEV_NVME_TIMEOUT_ACTION_ABORT;
|
|
|
|
} else if (spdk_json_strequal(val, "reset") == true) {
|
|
|
|
*action = SPDK_BDEV_NVME_TIMEOUT_ACTION_RESET;
|
|
|
|
} else {
|
|
|
|
SPDK_NOTICELOG("Invalid parameter value: action_on_timeout\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct spdk_json_object_decoder rpc_bdev_nvme_options_decoders[] = {
|
|
|
|
{"action_on_timeout", offsetof(struct spdk_bdev_nvme_opts, action_on_timeout), rpc_decode_action_on_timeout, true},
|
|
|
|
{"timeout_us", offsetof(struct spdk_bdev_nvme_opts, timeout_us), spdk_json_decode_uint64, true},
|
2020-11-27 16:10:36 +00:00
|
|
|
{"keep_alive_timeout_ms", offsetof(struct spdk_bdev_nvme_opts, keep_alive_timeout_ms), spdk_json_decode_uint32, true},
|
2018-07-09 21:04:33 +00:00
|
|
|
{"retry_count", offsetof(struct spdk_bdev_nvme_opts, retry_count), spdk_json_decode_uint32, true},
|
2019-09-03 03:48:49 +00:00
|
|
|
{"arbitration_burst", offsetof(struct spdk_bdev_nvme_opts, arbitration_burst), spdk_json_decode_uint32, true},
|
|
|
|
{"low_priority_weight", offsetof(struct spdk_bdev_nvme_opts, low_priority_weight), spdk_json_decode_uint32, true},
|
|
|
|
{"medium_priority_weight", offsetof(struct spdk_bdev_nvme_opts, medium_priority_weight), spdk_json_decode_uint32, true},
|
|
|
|
{"high_priority_weight", offsetof(struct spdk_bdev_nvme_opts, high_priority_weight), spdk_json_decode_uint32, true},
|
2018-07-09 21:04:33 +00:00
|
|
|
{"nvme_adminq_poll_period_us", offsetof(struct spdk_bdev_nvme_opts, nvme_adminq_poll_period_us), spdk_json_decode_uint64, true},
|
2019-03-11 22:26:53 +00:00
|
|
|
{"nvme_ioq_poll_period_us", offsetof(struct spdk_bdev_nvme_opts, nvme_ioq_poll_period_us), spdk_json_decode_uint64, true},
|
2019-07-10 05:13:31 +00:00
|
|
|
{"io_queue_requests", offsetof(struct spdk_bdev_nvme_opts, io_queue_requests), spdk_json_decode_uint32, true},
|
2019-11-18 17:11:39 +00:00
|
|
|
{"delay_cmd_submit", offsetof(struct spdk_bdev_nvme_opts, delay_cmd_submit), spdk_json_decode_bool, true},
|
2018-07-09 21:04:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2020-05-10 07:46:07 +00:00
|
|
|
rpc_bdev_nvme_set_options(struct spdk_jsonrpc_request *request,
|
|
|
|
const struct spdk_json_val *params)
|
2018-07-09 21:04:33 +00:00
|
|
|
{
|
|
|
|
struct spdk_bdev_nvme_opts opts;
|
|
|
|
int rc;
|
|
|
|
|
2020-05-10 07:46:07 +00:00
|
|
|
bdev_nvme_get_opts(&opts);
|
2018-07-09 21:04:33 +00:00
|
|
|
if (params && spdk_json_decode_object(params, rpc_bdev_nvme_options_decoders,
|
|
|
|
SPDK_COUNTOF(rpc_bdev_nvme_options_decoders),
|
|
|
|
&opts)) {
|
|
|
|
SPDK_ERRLOG("spdk_json_decode_object failed\n");
|
2019-07-15 11:07:12 +00:00
|
|
|
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
|
|
|
|
"spdk_json_decode_object failed");
|
|
|
|
return;
|
2018-07-09 21:04:33 +00:00
|
|
|
}
|
|
|
|
|
2020-05-10 07:46:07 +00:00
|
|
|
rc = bdev_nvme_set_opts(&opts);
|
2018-07-09 21:04:33 +00:00
|
|
|
if (rc) {
|
2019-07-15 11:07:12 +00:00
|
|
|
spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
|
|
|
|
return;
|
2018-07-09 21:04:33 +00:00
|
|
|
}
|
|
|
|
|
2020-11-06 16:58:30 +00:00
|
|
|
spdk_jsonrpc_send_bool_response(request, true);
|
2018-07-09 21:04:33 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2020-05-10 07:46:07 +00:00
|
|
|
SPDK_RPC_REGISTER("bdev_nvme_set_options", rpc_bdev_nvme_set_options,
|
2020-01-07 18:53:13 +00:00
|
|
|
SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME)
|
2019-08-30 13:22:53 +00:00
|
|
|
SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_nvme_set_options, set_bdev_nvme_options)
|
2018-07-09 21:04:33 +00:00
|
|
|
|
2018-07-12 12:26:19 +00:00
|
|
|
struct rpc_bdev_nvme_hotplug {
|
|
|
|
bool enabled;
|
|
|
|
uint64_t period_us;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct spdk_json_object_decoder rpc_bdev_nvme_hotplug_decoders[] = {
|
|
|
|
{"enable", offsetof(struct rpc_bdev_nvme_hotplug, enabled), spdk_json_decode_bool, false},
|
|
|
|
{"period_us", offsetof(struct rpc_bdev_nvme_hotplug, period_us), spdk_json_decode_uint64, true},
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2019-08-22 13:04:52 +00:00
|
|
|
rpc_bdev_nvme_set_hotplug_done(void *ctx)
|
2018-07-12 12:26:19 +00:00
|
|
|
{
|
|
|
|
struct spdk_jsonrpc_request *request = ctx;
|
|
|
|
|
2020-11-06 16:58:30 +00:00
|
|
|
spdk_jsonrpc_send_bool_response(request, true);
|
2018-07-12 12:26:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-05-10 07:46:07 +00:00
|
|
|
rpc_bdev_nvme_set_hotplug(struct spdk_jsonrpc_request *request,
|
|
|
|
const struct spdk_json_val *params)
|
2018-07-12 12:26:19 +00:00
|
|
|
{
|
|
|
|
struct rpc_bdev_nvme_hotplug req = {false, 0};
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (spdk_json_decode_object(params, rpc_bdev_nvme_hotplug_decoders,
|
|
|
|
SPDK_COUNTOF(rpc_bdev_nvme_hotplug_decoders), &req)) {
|
|
|
|
SPDK_ERRLOG("spdk_json_decode_object failed\n");
|
|
|
|
rc = -EINVAL;
|
|
|
|
goto invalid;
|
|
|
|
}
|
|
|
|
|
2020-05-10 07:46:07 +00:00
|
|
|
rc = bdev_nvme_set_hotplug(req.enabled, req.period_us, rpc_bdev_nvme_set_hotplug_done,
|
|
|
|
request);
|
2018-07-12 12:26:19 +00:00
|
|
|
if (rc) {
|
|
|
|
goto invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
invalid:
|
|
|
|
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, spdk_strerror(-rc));
|
|
|
|
}
|
2020-05-10 07:46:07 +00:00
|
|
|
SPDK_RPC_REGISTER("bdev_nvme_set_hotplug", rpc_bdev_nvme_set_hotplug, SPDK_RPC_RUNTIME)
|
2019-08-30 13:22:53 +00:00
|
|
|
SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_nvme_set_hotplug, set_bdev_nvme_hotplug)
|
2018-07-12 12:26:19 +00:00
|
|
|
|
2019-08-23 13:50:51 +00:00
|
|
|
struct rpc_bdev_nvme_attach_controller {
|
2017-03-01 20:35:59 +00:00
|
|
|
char *name;
|
2016-12-12 07:42:43 +00:00
|
|
|
char *trtype;
|
|
|
|
char *adrfam;
|
|
|
|
char *traddr;
|
|
|
|
char *trsvcid;
|
2020-02-19 11:18:51 +00:00
|
|
|
char *priority;
|
2016-12-12 07:42:43 +00:00
|
|
|
char *subnqn;
|
2018-06-19 22:00:54 +00:00
|
|
|
char *hostnqn;
|
2018-12-04 23:30:11 +00:00
|
|
|
char *hostaddr;
|
|
|
|
char *hostsvcid;
|
2019-02-08 01:06:45 +00:00
|
|
|
bool prchk_reftag;
|
|
|
|
bool prchk_guard;
|
2016-10-10 03:58:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2019-08-23 13:50:51 +00:00
|
|
|
free_rpc_bdev_nvme_attach_controller(struct rpc_bdev_nvme_attach_controller *req)
|
2016-10-10 03:58:17 +00:00
|
|
|
{
|
2017-03-01 20:35:59 +00:00
|
|
|
free(req->name);
|
2016-12-12 07:42:43 +00:00
|
|
|
free(req->trtype);
|
|
|
|
free(req->adrfam);
|
|
|
|
free(req->traddr);
|
|
|
|
free(req->trsvcid);
|
2020-02-19 11:18:51 +00:00
|
|
|
free(req->priority);
|
2016-12-12 07:42:43 +00:00
|
|
|
free(req->subnqn);
|
2018-06-19 22:00:54 +00:00
|
|
|
free(req->hostnqn);
|
2018-12-04 23:30:11 +00:00
|
|
|
free(req->hostaddr);
|
|
|
|
free(req->hostsvcid);
|
2016-10-10 03:58:17 +00:00
|
|
|
}
|
|
|
|
|
2019-08-23 13:50:51 +00:00
|
|
|
static const struct spdk_json_object_decoder rpc_bdev_nvme_attach_controller_decoders[] = {
|
|
|
|
{"name", offsetof(struct rpc_bdev_nvme_attach_controller, name), spdk_json_decode_string},
|
|
|
|
{"trtype", offsetof(struct rpc_bdev_nvme_attach_controller, trtype), spdk_json_decode_string},
|
|
|
|
{"traddr", offsetof(struct rpc_bdev_nvme_attach_controller, traddr), spdk_json_decode_string},
|
2016-12-12 07:42:43 +00:00
|
|
|
|
2019-08-23 13:50:51 +00:00
|
|
|
{"adrfam", offsetof(struct rpc_bdev_nvme_attach_controller, adrfam), spdk_json_decode_string, true},
|
|
|
|
{"trsvcid", offsetof(struct rpc_bdev_nvme_attach_controller, trsvcid), spdk_json_decode_string, true},
|
2020-02-19 11:18:51 +00:00
|
|
|
{"priority", offsetof(struct rpc_bdev_nvme_attach_controller, priority), spdk_json_decode_string, true},
|
2019-08-23 13:50:51 +00:00
|
|
|
{"subnqn", offsetof(struct rpc_bdev_nvme_attach_controller, subnqn), spdk_json_decode_string, true},
|
|
|
|
{"hostnqn", offsetof(struct rpc_bdev_nvme_attach_controller, hostnqn), spdk_json_decode_string, true},
|
|
|
|
{"hostaddr", offsetof(struct rpc_bdev_nvme_attach_controller, hostaddr), spdk_json_decode_string, true},
|
|
|
|
{"hostsvcid", offsetof(struct rpc_bdev_nvme_attach_controller, hostsvcid), spdk_json_decode_string, true},
|
2018-12-04 23:30:11 +00:00
|
|
|
|
2019-08-23 13:50:51 +00:00
|
|
|
{"prchk_reftag", offsetof(struct rpc_bdev_nvme_attach_controller, prchk_reftag), spdk_json_decode_bool, true},
|
|
|
|
{"prchk_guard", offsetof(struct rpc_bdev_nvme_attach_controller, prchk_guard), spdk_json_decode_bool, true}
|
2016-10-10 03:58:17 +00:00
|
|
|
};
|
|
|
|
|
2018-05-03 17:15:28 +00:00
|
|
|
#define NVME_MAX_BDEVS_PER_RPC 128
|
2017-02-28 18:06:06 +00:00
|
|
|
|
2019-08-23 13:50:51 +00:00
|
|
|
struct rpc_bdev_nvme_attach_controller_ctx {
|
|
|
|
struct rpc_bdev_nvme_attach_controller req;
|
2019-10-02 10:40:06 +00:00
|
|
|
uint32_t count;
|
2019-03-05 06:49:37 +00:00
|
|
|
const char *names[NVME_MAX_BDEVS_PER_RPC];
|
|
|
|
struct spdk_jsonrpc_request *request;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2020-05-10 07:46:07 +00:00
|
|
|
rpc_bdev_nvme_attach_controller_done(void *cb_ctx, size_t bdev_count, int rc)
|
2019-03-05 06:49:37 +00:00
|
|
|
{
|
2019-08-23 13:50:51 +00:00
|
|
|
struct rpc_bdev_nvme_attach_controller_ctx *ctx = cb_ctx;
|
2019-03-05 06:49:37 +00:00
|
|
|
struct spdk_jsonrpc_request *request = ctx->request;
|
|
|
|
struct spdk_json_write_ctx *w;
|
|
|
|
size_t i;
|
|
|
|
|
2019-03-05 07:26:58 +00:00
|
|
|
if (rc < 0) {
|
2019-03-05 06:49:37 +00:00
|
|
|
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2019-08-05 13:21:38 +00:00
|
|
|
w = spdk_jsonrpc_begin_result(request);
|
2019-03-05 06:49:37 +00:00
|
|
|
spdk_json_write_array_begin(w);
|
2019-09-16 08:34:07 +00:00
|
|
|
for (i = 0; i < bdev_count; i++) {
|
2019-03-05 06:49:37 +00:00
|
|
|
spdk_json_write_string(w, ctx->names[i]);
|
|
|
|
}
|
|
|
|
spdk_json_write_array_end(w);
|
|
|
|
spdk_jsonrpc_end_result(request, w);
|
|
|
|
|
|
|
|
exit:
|
2019-08-23 13:50:51 +00:00
|
|
|
free_rpc_bdev_nvme_attach_controller(&ctx->req);
|
2019-03-05 06:49:37 +00:00
|
|
|
free(ctx);
|
|
|
|
}
|
|
|
|
|
2016-10-10 03:58:17 +00:00
|
|
|
static void
|
2020-05-10 07:46:07 +00:00
|
|
|
rpc_bdev_nvme_attach_controller(struct spdk_jsonrpc_request *request,
|
|
|
|
const struct spdk_json_val *params)
|
2016-10-10 03:58:17 +00:00
|
|
|
{
|
2019-08-23 13:50:51 +00:00
|
|
|
struct rpc_bdev_nvme_attach_controller_ctx *ctx;
|
2017-01-25 23:05:09 +00:00
|
|
|
struct spdk_nvme_transport_id trid = {};
|
2018-12-04 23:30:11 +00:00
|
|
|
struct spdk_nvme_host_id hostid = {};
|
2019-02-08 01:06:45 +00:00
|
|
|
uint32_t prchk_flags = 0;
|
2020-06-13 01:06:58 +00:00
|
|
|
struct nvme_bdev_ctrlr *ctrlr = NULL;
|
2020-07-31 00:11:24 +00:00
|
|
|
size_t len, maxlen;
|
2016-12-12 07:42:43 +00:00
|
|
|
int rc;
|
2016-10-10 03:58:17 +00:00
|
|
|
|
2019-03-05 06:49:37 +00:00
|
|
|
ctx = calloc(1, sizeof(*ctx));
|
|
|
|
if (!ctx) {
|
2019-07-15 11:07:12 +00:00
|
|
|
spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM));
|
2019-03-05 06:49:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-23 13:50:51 +00:00
|
|
|
if (spdk_json_decode_object(params, rpc_bdev_nvme_attach_controller_decoders,
|
|
|
|
SPDK_COUNTOF(rpc_bdev_nvme_attach_controller_decoders),
|
2019-03-05 06:49:37 +00:00
|
|
|
&ctx->req)) {
|
2016-12-12 07:42:43 +00:00
|
|
|
SPDK_ERRLOG("spdk_json_decode_object failed\n");
|
2019-07-15 11:07:12 +00:00
|
|
|
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
|
|
|
|
"spdk_json_decode_object failed");
|
|
|
|
goto cleanup;
|
2016-12-12 07:42:43 +00:00
|
|
|
}
|
|
|
|
|
2019-12-23 19:58:27 +00:00
|
|
|
/* Parse trstring */
|
|
|
|
rc = spdk_nvme_transport_id_populate_trstring(&trid, ctx->req.trtype);
|
|
|
|
if (rc < 0) {
|
|
|
|
SPDK_ERRLOG("Failed to parse trtype: %s\n", ctx->req.trtype);
|
|
|
|
spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse trtype: %s",
|
|
|
|
ctx->req.trtype);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2016-12-12 07:42:43 +00:00
|
|
|
/* Parse trtype */
|
2019-03-05 06:49:37 +00:00
|
|
|
rc = spdk_nvme_transport_id_parse_trtype(&trid.trtype, ctx->req.trtype);
|
2020-04-21 02:17:52 +00:00
|
|
|
assert(rc == 0);
|
2016-10-10 03:58:17 +00:00
|
|
|
|
2020-06-13 01:06:58 +00:00
|
|
|
ctrlr = nvme_bdev_ctrlr_get_by_name(ctx->req.name);
|
|
|
|
|
2016-12-12 07:42:43 +00:00
|
|
|
/* Parse traddr */
|
2020-07-31 00:11:24 +00:00
|
|
|
maxlen = sizeof(trid.traddr);
|
|
|
|
len = strnlen(ctx->req.traddr, maxlen);
|
|
|
|
if (len == maxlen) {
|
|
|
|
spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "traddr too long: %s",
|
|
|
|
ctx->req.traddr);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
memcpy(trid.traddr, ctx->req.traddr, len + 1);
|
2016-12-12 07:42:43 +00:00
|
|
|
|
|
|
|
/* Parse adrfam */
|
2019-03-05 06:49:37 +00:00
|
|
|
if (ctx->req.adrfam) {
|
|
|
|
rc = spdk_nvme_transport_id_parse_adrfam(&trid.adrfam, ctx->req.adrfam);
|
2016-12-12 07:42:43 +00:00
|
|
|
if (rc < 0) {
|
2019-03-05 06:49:37 +00:00
|
|
|
SPDK_ERRLOG("Failed to parse adrfam: %s\n", ctx->req.adrfam);
|
2019-07-15 11:07:12 +00:00
|
|
|
spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse adrfam: %s",
|
|
|
|
ctx->req.adrfam);
|
|
|
|
goto cleanup;
|
2016-12-12 07:42:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse trsvcid */
|
2019-03-05 06:49:37 +00:00
|
|
|
if (ctx->req.trsvcid) {
|
2020-07-31 00:11:24 +00:00
|
|
|
maxlen = sizeof(trid.trsvcid);
|
|
|
|
len = strnlen(ctx->req.trsvcid, maxlen);
|
|
|
|
if (len == maxlen) {
|
|
|
|
spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "trsvcid too long: %s",
|
|
|
|
ctx->req.trsvcid);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
memcpy(trid.trsvcid, ctx->req.trsvcid, len + 1);
|
2016-12-12 07:42:43 +00:00
|
|
|
}
|
|
|
|
|
2020-02-19 11:18:51 +00:00
|
|
|
/* Parse priority for the NVMe-oF transport connection */
|
|
|
|
if (ctx->req.priority) {
|
|
|
|
trid.priority = spdk_strtol(ctx->req.priority, 10);
|
|
|
|
}
|
|
|
|
|
2016-12-12 07:42:43 +00:00
|
|
|
/* Parse subnqn */
|
2019-03-05 06:49:37 +00:00
|
|
|
if (ctx->req.subnqn) {
|
2020-07-31 00:11:24 +00:00
|
|
|
maxlen = sizeof(trid.subnqn);
|
|
|
|
len = strnlen(ctx->req.subnqn, maxlen);
|
|
|
|
if (len == maxlen) {
|
|
|
|
spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "subnqn too long: %s",
|
|
|
|
ctx->req.subnqn);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
memcpy(trid.subnqn, ctx->req.subnqn, len + 1);
|
2016-12-12 07:42:43 +00:00
|
|
|
}
|
2016-10-10 03:58:17 +00:00
|
|
|
|
2020-06-13 01:06:58 +00:00
|
|
|
if (ctrlr && (ctx->req.hostaddr || ctx->req.hostnqn || ctx->req.hostsvcid || ctx->req.prchk_guard ||
|
|
|
|
ctx->req.prchk_reftag)) {
|
|
|
|
goto conflicting_arguments;
|
|
|
|
}
|
|
|
|
|
2019-03-05 06:49:37 +00:00
|
|
|
if (ctx->req.hostaddr) {
|
2020-07-31 00:11:24 +00:00
|
|
|
maxlen = sizeof(hostid.hostaddr);
|
|
|
|
len = strnlen(ctx->req.hostaddr, maxlen);
|
|
|
|
if (len == maxlen) {
|
|
|
|
spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "hostaddr too long: %s",
|
|
|
|
ctx->req.hostaddr);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
memcpy(hostid.hostaddr, ctx->req.hostaddr, len + 1);
|
2018-12-04 23:30:11 +00:00
|
|
|
}
|
|
|
|
|
2019-03-05 06:49:37 +00:00
|
|
|
if (ctx->req.hostsvcid) {
|
2020-07-31 00:11:24 +00:00
|
|
|
maxlen = sizeof(hostid.hostsvcid);
|
|
|
|
len = strnlen(ctx->req.hostsvcid, maxlen);
|
|
|
|
if (len == maxlen) {
|
|
|
|
spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "hostsvcid too long: %s",
|
|
|
|
ctx->req.hostsvcid);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
memcpy(hostid.hostsvcid, ctx->req.hostsvcid, len + 1);
|
2018-12-04 23:30:11 +00:00
|
|
|
}
|
|
|
|
|
2019-03-05 06:49:37 +00:00
|
|
|
if (ctx->req.prchk_reftag) {
|
2019-02-08 01:06:45 +00:00
|
|
|
prchk_flags |= SPDK_NVME_IO_FLAGS_PRCHK_REFTAG;
|
|
|
|
}
|
|
|
|
|
2019-03-05 06:49:37 +00:00
|
|
|
if (ctx->req.prchk_guard) {
|
2019-02-08 01:06:45 +00:00
|
|
|
prchk_flags |= SPDK_NVME_IO_FLAGS_PRCHK_GUARD;
|
|
|
|
}
|
|
|
|
|
2019-03-05 06:49:37 +00:00
|
|
|
ctx->request = request;
|
|
|
|
ctx->count = NVME_MAX_BDEVS_PER_RPC;
|
2020-05-10 07:46:07 +00:00
|
|
|
rc = bdev_nvme_create(&trid, &hostid, ctx->req.name, ctx->names, ctx->count, ctx->req.hostnqn,
|
|
|
|
prchk_flags, rpc_bdev_nvme_attach_controller_done, ctx);
|
2019-07-15 11:07:12 +00:00
|
|
|
if (rc) {
|
|
|
|
spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
|
|
|
|
goto cleanup;
|
2016-10-10 03:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
2020-06-13 01:06:58 +00:00
|
|
|
conflicting_arguments:
|
|
|
|
spdk_jsonrpc_send_error_response_fmt(request, -EINVAL,
|
|
|
|
"Invalid agrgument list. Existing controller name cannot be combined with host information or PI options.\n");
|
2019-07-15 11:07:12 +00:00
|
|
|
cleanup:
|
2019-08-23 13:50:51 +00:00
|
|
|
free_rpc_bdev_nvme_attach_controller(&ctx->req);
|
2019-03-05 06:49:37 +00:00
|
|
|
free(ctx);
|
2016-10-10 03:58:17 +00:00
|
|
|
}
|
2020-05-10 07:46:07 +00:00
|
|
|
SPDK_RPC_REGISTER("bdev_nvme_attach_controller", rpc_bdev_nvme_attach_controller,
|
2019-08-23 13:50:51 +00:00
|
|
|
SPDK_RPC_RUNTIME)
|
|
|
|
SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_nvme_attach_controller, construct_nvme_bdev)
|
2017-07-11 23:32:32 +00:00
|
|
|
|
2018-07-12 12:42:44 +00:00
|
|
|
static void
|
2020-05-10 07:46:07 +00:00
|
|
|
rpc_dump_nvme_controller_info(struct spdk_json_write_ctx *w,
|
|
|
|
struct nvme_bdev_ctrlr *nvme_bdev_ctrlr)
|
2018-07-12 12:42:44 +00:00
|
|
|
{
|
|
|
|
struct spdk_nvme_transport_id *trid;
|
|
|
|
|
2020-07-08 22:27:02 +00:00
|
|
|
trid = nvme_bdev_ctrlr->connected_trid;
|
2018-07-12 12:42:44 +00:00
|
|
|
|
|
|
|
spdk_json_write_object_begin(w);
|
2019-02-26 09:49:45 +00:00
|
|
|
spdk_json_write_named_string(w, "name", nvme_bdev_ctrlr->name);
|
2018-07-12 12:42:44 +00:00
|
|
|
|
2019-10-24 18:09:47 +00:00
|
|
|
#ifdef SPDK_CONFIG_NVME_CUSE
|
2020-04-22 11:36:06 +00:00
|
|
|
size_t cuse_name_size = 128;
|
|
|
|
char cuse_name[cuse_name_size];
|
2019-10-24 18:09:47 +00:00
|
|
|
|
2020-04-22 11:36:06 +00:00
|
|
|
int rc = spdk_nvme_cuse_get_ctrlr_name(nvme_bdev_ctrlr->ctrlr, cuse_name, &cuse_name_size);
|
|
|
|
if (rc == 0) {
|
|
|
|
spdk_json_write_named_string(w, "cuse_device", cuse_name);
|
2019-10-24 18:09:47 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-07-12 12:42:44 +00:00
|
|
|
spdk_json_write_named_object_begin(w, "trid");
|
2019-03-05 12:35:29 +00:00
|
|
|
nvme_bdev_dump_trid_json(trid, w);
|
2018-07-12 12:42:44 +00:00
|
|
|
spdk_json_write_object_end(w);
|
|
|
|
|
|
|
|
spdk_json_write_object_end(w);
|
|
|
|
}
|
|
|
|
|
2019-08-27 19:49:10 +00:00
|
|
|
struct rpc_bdev_nvme_get_controllers {
|
2018-07-12 12:42:44 +00:00
|
|
|
char *name;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2019-08-27 19:49:10 +00:00
|
|
|
free_rpc_bdev_nvme_get_controllers(struct rpc_bdev_nvme_get_controllers *r)
|
2018-07-12 12:42:44 +00:00
|
|
|
{
|
|
|
|
free(r->name);
|
|
|
|
}
|
|
|
|
|
2019-08-27 19:49:10 +00:00
|
|
|
static const struct spdk_json_object_decoder rpc_bdev_nvme_get_controllers_decoders[] = {
|
|
|
|
{"name", offsetof(struct rpc_bdev_nvme_get_controllers, name), spdk_json_decode_string, true},
|
2018-07-12 12:42:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2020-05-10 07:46:07 +00:00
|
|
|
rpc_bdev_nvme_get_controllers(struct spdk_jsonrpc_request *request,
|
|
|
|
const struct spdk_json_val *params)
|
2018-07-12 12:42:44 +00:00
|
|
|
{
|
2019-08-27 19:49:10 +00:00
|
|
|
struct rpc_bdev_nvme_get_controllers req = {};
|
2018-07-12 12:42:44 +00:00
|
|
|
struct spdk_json_write_ctx *w;
|
2019-02-26 09:49:45 +00:00
|
|
|
struct nvme_bdev_ctrlr *ctrlr = NULL;
|
2018-07-12 12:42:44 +00:00
|
|
|
|
2019-08-27 19:49:10 +00:00
|
|
|
if (params && spdk_json_decode_object(params, rpc_bdev_nvme_get_controllers_decoders,
|
|
|
|
SPDK_COUNTOF(rpc_bdev_nvme_get_controllers_decoders),
|
2018-07-12 12:42:44 +00:00
|
|
|
&req)) {
|
|
|
|
SPDK_ERRLOG("spdk_json_decode_object failed\n");
|
2019-07-15 11:07:12 +00:00
|
|
|
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
|
|
|
|
"spdk_json_decode_object failed");
|
|
|
|
goto cleanup;
|
2018-07-12 12:42:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (req.name) {
|
2019-02-27 14:21:41 +00:00
|
|
|
ctrlr = nvme_bdev_ctrlr_get_by_name(req.name);
|
2018-07-12 12:42:44 +00:00
|
|
|
if (ctrlr == NULL) {
|
|
|
|
SPDK_ERRLOG("ctrlr '%s' does not exist\n", req.name);
|
2019-07-15 11:07:12 +00:00
|
|
|
spdk_jsonrpc_send_error_response_fmt(request, EINVAL, "Controller %s does not exist", req.name);
|
|
|
|
goto cleanup;
|
2018-07-12 12:42:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w = spdk_jsonrpc_begin_result(request);
|
|
|
|
spdk_json_write_array_begin(w);
|
|
|
|
|
|
|
|
if (ctrlr != NULL) {
|
2020-05-10 07:46:07 +00:00
|
|
|
rpc_dump_nvme_controller_info(w, ctrlr);
|
2018-07-12 12:42:44 +00:00
|
|
|
} else {
|
2019-03-05 12:35:29 +00:00
|
|
|
for (ctrlr = nvme_bdev_first_ctrlr(); ctrlr; ctrlr = nvme_bdev_next_ctrlr(ctrlr)) {
|
2020-05-10 07:46:07 +00:00
|
|
|
rpc_dump_nvme_controller_info(w, ctrlr);
|
2018-07-12 12:42:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
spdk_json_write_array_end(w);
|
|
|
|
|
|
|
|
spdk_jsonrpc_end_result(request, w);
|
|
|
|
|
2019-07-15 11:07:12 +00:00
|
|
|
cleanup:
|
2019-08-27 19:49:10 +00:00
|
|
|
free_rpc_bdev_nvme_get_controllers(&req);
|
2018-07-12 12:42:44 +00:00
|
|
|
}
|
2020-05-10 07:46:07 +00:00
|
|
|
SPDK_RPC_REGISTER("bdev_nvme_get_controllers", rpc_bdev_nvme_get_controllers, SPDK_RPC_RUNTIME)
|
2019-08-27 19:49:10 +00:00
|
|
|
SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_nvme_get_controllers, get_nvme_controllers)
|
2018-07-12 12:42:44 +00:00
|
|
|
|
2019-08-23 15:12:13 +00:00
|
|
|
struct rpc_bdev_nvme_detach_controller {
|
2018-07-26 12:54:20 +00:00
|
|
|
char *name;
|
2020-06-25 19:23:30 +00:00
|
|
|
char *trtype;
|
|
|
|
char *adrfam;
|
|
|
|
char *traddr;
|
|
|
|
char *trsvcid;
|
|
|
|
char *subnqn;
|
2018-07-26 12:54:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2019-08-23 15:12:13 +00:00
|
|
|
free_rpc_bdev_nvme_detach_controller(struct rpc_bdev_nvme_detach_controller *req)
|
2018-07-26 12:54:20 +00:00
|
|
|
{
|
|
|
|
free(req->name);
|
2020-06-25 19:23:30 +00:00
|
|
|
free(req->trtype);
|
|
|
|
free(req->adrfam);
|
|
|
|
free(req->traddr);
|
|
|
|
free(req->trsvcid);
|
|
|
|
free(req->subnqn);
|
2018-07-26 12:54:20 +00:00
|
|
|
}
|
|
|
|
|
2019-08-23 15:12:13 +00:00
|
|
|
static const struct spdk_json_object_decoder rpc_bdev_nvme_detach_controller_decoders[] = {
|
|
|
|
{"name", offsetof(struct rpc_bdev_nvme_detach_controller, name), spdk_json_decode_string},
|
2020-06-25 19:23:30 +00:00
|
|
|
{"trtype", offsetof(struct rpc_bdev_nvme_detach_controller, trtype), spdk_json_decode_string, true},
|
|
|
|
{"traddr", offsetof(struct rpc_bdev_nvme_detach_controller, traddr), spdk_json_decode_string, true},
|
|
|
|
{"adrfam", offsetof(struct rpc_bdev_nvme_detach_controller, adrfam), spdk_json_decode_string, true},
|
|
|
|
{"trsvcid", offsetof(struct rpc_bdev_nvme_detach_controller, trsvcid), spdk_json_decode_string, true},
|
|
|
|
{"subnqn", offsetof(struct rpc_bdev_nvme_detach_controller, subnqn), spdk_json_decode_string, true},
|
2018-07-26 12:54:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2020-05-10 07:46:07 +00:00
|
|
|
rpc_bdev_nvme_detach_controller(struct spdk_jsonrpc_request *request,
|
|
|
|
const struct spdk_json_val *params)
|
2018-07-26 12:54:20 +00:00
|
|
|
{
|
2019-08-23 15:12:13 +00:00
|
|
|
struct rpc_bdev_nvme_detach_controller req = {NULL};
|
2020-06-25 19:23:30 +00:00
|
|
|
struct spdk_nvme_transport_id trid = {};
|
|
|
|
size_t len, maxlen;
|
2018-07-26 12:54:20 +00:00
|
|
|
int rc = 0;
|
2020-06-25 19:23:30 +00:00
|
|
|
bool all_trid_entries, one_trid_entry;
|
2018-07-26 12:54:20 +00:00
|
|
|
|
2019-08-23 15:12:13 +00:00
|
|
|
if (spdk_json_decode_object(params, rpc_bdev_nvme_detach_controller_decoders,
|
|
|
|
SPDK_COUNTOF(rpc_bdev_nvme_detach_controller_decoders),
|
2018-07-26 12:54:20 +00:00
|
|
|
&req)) {
|
2019-07-15 11:07:12 +00:00
|
|
|
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
|
|
|
|
"spdk_json_decode_object failed");
|
|
|
|
goto cleanup;
|
2018-07-26 12:54:20 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 19:23:30 +00:00
|
|
|
all_trid_entries = req.trtype && req.traddr && req.adrfam && req.trsvcid && req.subnqn;
|
|
|
|
one_trid_entry = req.trtype || req.traddr || req.adrfam || req.trsvcid || req.subnqn;
|
|
|
|
|
|
|
|
if (all_trid_entries ^ one_trid_entry) {
|
|
|
|
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
|
|
|
|
"trtype, traddr, adrfam, trsvcid, subnqn must all be provided together or not at all.");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (all_trid_entries) {
|
|
|
|
/* Parse trtype */
|
|
|
|
rc = spdk_nvme_transport_id_parse_trtype(&trid.trtype, req.trtype);
|
|
|
|
if (rc < 0) {
|
|
|
|
SPDK_ERRLOG("Failed to parse trtype: %s\n", req.trtype);
|
|
|
|
spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse trtype: %s",
|
|
|
|
req.trtype);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse traddr */
|
|
|
|
maxlen = sizeof(trid.traddr);
|
|
|
|
len = strnlen(req.traddr, maxlen);
|
|
|
|
if (len == maxlen) {
|
|
|
|
spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "traddr too long: %s",
|
|
|
|
req.traddr);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
memcpy(trid.traddr, req.traddr, len + 1);
|
|
|
|
|
|
|
|
rc = spdk_nvme_transport_id_parse_adrfam(&trid.adrfam, req.adrfam);
|
|
|
|
if (rc < 0) {
|
|
|
|
SPDK_ERRLOG("Failed to parse adrfam: %s\n", req.adrfam);
|
|
|
|
spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse adrfam: %s",
|
|
|
|
req.adrfam);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
maxlen = sizeof(trid.trsvcid);
|
|
|
|
len = strnlen(req.trsvcid, maxlen);
|
|
|
|
if (len == maxlen) {
|
|
|
|
spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "trsvcid too long: %s",
|
|
|
|
req.trsvcid);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
memcpy(trid.trsvcid, req.trsvcid, len + 1);
|
|
|
|
|
|
|
|
maxlen = sizeof(trid.subnqn);
|
|
|
|
len = strnlen(req.subnqn, maxlen);
|
|
|
|
if (len == maxlen) {
|
|
|
|
spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "subnqn too long: %s",
|
|
|
|
req.subnqn);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
memcpy(trid.subnqn, req.subnqn, len + 1);
|
|
|
|
rc = bdev_nvme_remove_trid(req.name, &trid);
|
|
|
|
} else {
|
|
|
|
rc = bdev_nvme_delete(req.name);
|
|
|
|
}
|
|
|
|
|
2018-07-26 12:54:20 +00:00
|
|
|
if (rc != 0) {
|
2019-07-15 11:07:12 +00:00
|
|
|
spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
|
|
|
|
goto cleanup;
|
2018-07-26 12:54:20 +00:00
|
|
|
}
|
|
|
|
|
2020-11-06 16:58:30 +00:00
|
|
|
spdk_jsonrpc_send_bool_response(request, true);
|
2018-07-26 12:54:20 +00:00
|
|
|
|
2019-07-15 11:07:12 +00:00
|
|
|
cleanup:
|
2019-08-23 15:12:13 +00:00
|
|
|
free_rpc_bdev_nvme_detach_controller(&req);
|
2018-07-26 12:54:20 +00:00
|
|
|
}
|
2020-05-10 07:46:07 +00:00
|
|
|
SPDK_RPC_REGISTER("bdev_nvme_detach_controller", rpc_bdev_nvme_detach_controller,
|
2019-08-23 15:12:13 +00:00
|
|
|
SPDK_RPC_RUNTIME)
|
|
|
|
SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_nvme_detach_controller, delete_nvme_controller)
|
2018-07-26 12:54:20 +00:00
|
|
|
|
2017-07-11 23:32:32 +00:00
|
|
|
struct rpc_apply_firmware {
|
|
|
|
char *filename;
|
|
|
|
char *bdev_name;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
free_rpc_apply_firmware(struct rpc_apply_firmware *req)
|
|
|
|
{
|
|
|
|
free(req->filename);
|
|
|
|
free(req->bdev_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct spdk_json_object_decoder rpc_apply_firmware_decoders[] = {
|
|
|
|
{"filename", offsetof(struct rpc_apply_firmware, filename), spdk_json_decode_string},
|
|
|
|
{"bdev_name", offsetof(struct rpc_apply_firmware, bdev_name), spdk_json_decode_string},
|
|
|
|
};
|
|
|
|
|
|
|
|
struct firmware_update_info {
|
|
|
|
void *fw_image;
|
|
|
|
void *p;
|
|
|
|
unsigned int size;
|
2018-03-02 19:49:36 +00:00
|
|
|
unsigned int size_remaining;
|
|
|
|
unsigned int offset;
|
|
|
|
unsigned int transfer;
|
2017-07-11 23:32:32 +00:00
|
|
|
|
|
|
|
void *desc;
|
|
|
|
struct spdk_io_channel *ch;
|
|
|
|
struct spdk_jsonrpc_request *request;
|
|
|
|
struct spdk_nvme_ctrlr *ctrlr;
|
|
|
|
open_descriptors_t desc_head;
|
|
|
|
struct rpc_apply_firmware *req;
|
|
|
|
};
|
|
|
|
|
2020-02-21 14:58:08 +00:00
|
|
|
static void
|
|
|
|
_apply_firmware_cleanup(void *ctx)
|
|
|
|
{
|
|
|
|
struct spdk_bdev_desc *desc = ctx;
|
|
|
|
|
|
|
|
spdk_bdev_close(desc);
|
|
|
|
}
|
|
|
|
|
2017-07-11 23:32:32 +00:00
|
|
|
static void
|
|
|
|
apply_firmware_cleanup(void *cb_arg)
|
|
|
|
{
|
2018-04-09 16:49:26 +00:00
|
|
|
struct open_descriptors *opt, *tmp;
|
2017-07-11 23:32:32 +00:00
|
|
|
struct firmware_update_info *firm_ctx = cb_arg;
|
|
|
|
|
|
|
|
if (!firm_ctx) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (firm_ctx->fw_image) {
|
2019-06-27 04:47:03 +00:00
|
|
|
spdk_free(firm_ctx->fw_image);
|
2017-07-11 23:32:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (firm_ctx->req) {
|
|
|
|
free_rpc_apply_firmware(firm_ctx->req);
|
|
|
|
free(firm_ctx->req);
|
|
|
|
}
|
2020-02-03 12:39:41 +00:00
|
|
|
|
|
|
|
if (firm_ctx->ch) {
|
|
|
|
spdk_put_io_channel(firm_ctx->ch);
|
|
|
|
}
|
|
|
|
|
2018-04-09 16:49:26 +00:00
|
|
|
TAILQ_FOREACH_SAFE(opt, &firm_ctx->desc_head, tqlst, tmp) {
|
|
|
|
TAILQ_REMOVE(&firm_ctx->desc_head, opt, tqlst);
|
2020-02-21 14:58:08 +00:00
|
|
|
/* Close the underlying bdev on its same opened thread. */
|
|
|
|
if (opt->thread && opt->thread != spdk_get_thread()) {
|
|
|
|
spdk_thread_send_msg(opt->thread, _apply_firmware_cleanup, opt->desc);
|
|
|
|
} else {
|
|
|
|
spdk_bdev_close(opt->desc);
|
|
|
|
}
|
2017-07-11 23:32:32 +00:00
|
|
|
free(opt);
|
|
|
|
}
|
|
|
|
free(firm_ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
apply_firmware_complete_reset(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
|
|
|
|
{
|
|
|
|
struct spdk_json_write_ctx *w;
|
|
|
|
struct firmware_update_info *firm_ctx = cb_arg;
|
|
|
|
|
|
|
|
spdk_bdev_free_io(bdev_io);
|
|
|
|
|
|
|
|
if (!success) {
|
|
|
|
spdk_jsonrpc_send_error_response(firm_ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
|
|
|
|
"firmware commit failed.");
|
|
|
|
apply_firmware_cleanup(firm_ctx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-22 16:39:36 +00:00
|
|
|
if (spdk_nvme_ctrlr_reset(firm_ctx->ctrlr) != 0) {
|
2017-07-11 23:32:32 +00:00
|
|
|
spdk_jsonrpc_send_error_response(firm_ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
|
|
|
|
"Controller reset failed.");
|
|
|
|
apply_firmware_cleanup(firm_ctx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-22 09:48:05 +00:00
|
|
|
w = spdk_jsonrpc_begin_result(firm_ctx->request);
|
2017-07-11 23:32:32 +00:00
|
|
|
spdk_json_write_string(w, "firmware commit succeeded. Controller reset in progress.");
|
|
|
|
spdk_jsonrpc_end_result(firm_ctx->request, w);
|
|
|
|
apply_firmware_cleanup(firm_ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
apply_firmware_complete(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
|
|
|
|
{
|
2018-04-06 23:29:02 +00:00
|
|
|
struct spdk_nvme_cmd cmd = {};
|
2017-07-11 23:32:32 +00:00
|
|
|
struct spdk_nvme_fw_commit fw_commit;
|
|
|
|
int slot = 0;
|
|
|
|
int rc;
|
|
|
|
struct firmware_update_info *firm_ctx = cb_arg;
|
|
|
|
enum spdk_nvme_fw_commit_action commit_action = SPDK_NVME_FW_COMMIT_REPLACE_AND_ENABLE_IMG;
|
|
|
|
|
|
|
|
if (!success) {
|
|
|
|
spdk_jsonrpc_send_error_response(firm_ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
|
|
|
|
"firmware download failed .");
|
|
|
|
spdk_bdev_free_io(bdev_io);
|
|
|
|
apply_firmware_cleanup(firm_ctx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
firm_ctx->p += firm_ctx->transfer;
|
|
|
|
firm_ctx->offset += firm_ctx->transfer;
|
|
|
|
firm_ctx->size_remaining -= firm_ctx->transfer;
|
|
|
|
|
|
|
|
switch (firm_ctx->size_remaining) {
|
|
|
|
case 0:
|
|
|
|
/* firmware download completed. Commit firmware */
|
|
|
|
memset(&fw_commit, 0, sizeof(struct spdk_nvme_fw_commit));
|
|
|
|
fw_commit.fs = slot;
|
|
|
|
fw_commit.ca = commit_action;
|
|
|
|
|
2018-04-06 23:29:02 +00:00
|
|
|
cmd.opc = SPDK_NVME_OPC_FIRMWARE_COMMIT;
|
|
|
|
memcpy(&cmd.cdw10, &fw_commit, sizeof(uint32_t));
|
|
|
|
rc = spdk_bdev_nvme_admin_passthru(firm_ctx->desc, firm_ctx->ch, &cmd, NULL, 0,
|
2017-07-11 23:32:32 +00:00
|
|
|
apply_firmware_complete_reset, firm_ctx);
|
|
|
|
if (rc) {
|
|
|
|
spdk_jsonrpc_send_error_response(firm_ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
|
|
|
|
"firmware commit failed.");
|
|
|
|
spdk_bdev_free_io(bdev_io);
|
|
|
|
apply_firmware_cleanup(firm_ctx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
firm_ctx->transfer = spdk_min(firm_ctx->size_remaining, 4096);
|
2018-04-06 23:29:02 +00:00
|
|
|
cmd.opc = SPDK_NVME_OPC_FIRMWARE_IMAGE_DOWNLOAD;
|
2017-07-11 23:32:32 +00:00
|
|
|
|
2018-04-06 23:29:02 +00:00
|
|
|
cmd.cdw10 = (firm_ctx->transfer >> 2) - 1;
|
|
|
|
cmd.cdw11 = firm_ctx->offset >> 2;
|
|
|
|
rc = spdk_bdev_nvme_admin_passthru(firm_ctx->desc, firm_ctx->ch, &cmd, firm_ctx->p,
|
2017-07-11 23:32:32 +00:00
|
|
|
firm_ctx->transfer, apply_firmware_complete, firm_ctx);
|
|
|
|
if (rc) {
|
|
|
|
spdk_jsonrpc_send_error_response(firm_ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
|
|
|
|
"firmware download failed.");
|
|
|
|
spdk_bdev_free_io(bdev_io);
|
|
|
|
apply_firmware_cleanup(firm_ctx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-05-10 07:46:07 +00:00
|
|
|
rpc_bdev_nvme_apply_firmware(struct spdk_jsonrpc_request *request,
|
|
|
|
const struct spdk_json_val *params)
|
2017-07-11 23:32:32 +00:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
int fd = -1;
|
|
|
|
struct stat fw_stat;
|
|
|
|
struct spdk_nvme_ctrlr *ctrlr;
|
|
|
|
char msg[1024];
|
|
|
|
struct spdk_bdev *bdev;
|
|
|
|
struct spdk_bdev *bdev2;
|
|
|
|
struct open_descriptors *opt;
|
|
|
|
struct spdk_bdev_desc *desc;
|
|
|
|
struct spdk_nvme_cmd *cmd;
|
|
|
|
struct firmware_update_info *firm_ctx;
|
|
|
|
|
2020-01-28 13:50:17 +00:00
|
|
|
firm_ctx = calloc(1, sizeof(struct firmware_update_info));
|
2017-07-11 23:32:32 +00:00
|
|
|
if (!firm_ctx) {
|
|
|
|
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
|
|
|
|
"Memory allocation error.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
firm_ctx->fw_image = NULL;
|
|
|
|
TAILQ_INIT(&firm_ctx->desc_head);
|
|
|
|
firm_ctx->request = request;
|
|
|
|
|
2020-01-28 13:50:17 +00:00
|
|
|
firm_ctx->req = calloc(1, sizeof(struct rpc_apply_firmware));
|
2017-07-11 23:32:32 +00:00
|
|
|
if (!firm_ctx->req) {
|
2020-11-09 03:25:31 +00:00
|
|
|
snprintf(msg, sizeof(msg), "Memory allocation error.");
|
|
|
|
goto err;
|
2017-07-11 23:32:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (spdk_json_decode_object(params, rpc_apply_firmware_decoders,
|
|
|
|
SPDK_COUNTOF(rpc_apply_firmware_decoders), firm_ctx->req)) {
|
2020-11-09 03:25:31 +00:00
|
|
|
snprintf(msg, sizeof(msg), "spdk_json_decode_object failed.");
|
|
|
|
goto err;
|
2017-07-11 23:32:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((bdev = spdk_bdev_get_by_name(firm_ctx->req->bdev_name)) == NULL) {
|
|
|
|
snprintf(msg, sizeof(msg), "bdev %s were not found", firm_ctx->req->bdev_name);
|
2020-11-09 03:25:31 +00:00
|
|
|
goto err;
|
2017-07-11 23:32:32 +00:00
|
|
|
}
|
|
|
|
|
2020-05-10 07:46:07 +00:00
|
|
|
if ((ctrlr = bdev_nvme_get_ctrlr(bdev)) == NULL) {
|
2017-07-11 23:32:32 +00:00
|
|
|
snprintf(msg, sizeof(msg), "Controller information for %s were not found.",
|
|
|
|
firm_ctx->req->bdev_name);
|
2020-11-09 03:25:31 +00:00
|
|
|
goto err;
|
2017-07-11 23:32:32 +00:00
|
|
|
}
|
|
|
|
firm_ctx->ctrlr = ctrlr;
|
|
|
|
|
|
|
|
for (bdev2 = spdk_bdev_first(); bdev2; bdev2 = spdk_bdev_next(bdev2)) {
|
|
|
|
|
2020-05-10 07:46:07 +00:00
|
|
|
if (bdev_nvme_get_ctrlr(bdev2) != ctrlr) {
|
2017-07-11 23:32:32 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-04-09 17:57:31 +00:00
|
|
|
if (!(opt = malloc(sizeof(struct open_descriptors)))) {
|
|
|
|
snprintf(msg, sizeof(msg), "Memory allocation error.");
|
2020-11-09 03:25:31 +00:00
|
|
|
goto err;
|
2018-04-09 17:57:31 +00:00
|
|
|
}
|
|
|
|
|
2020-10-22 16:39:36 +00:00
|
|
|
if (spdk_bdev_open(bdev2, true, NULL, NULL, &desc) != 0) {
|
2017-07-11 23:32:32 +00:00
|
|
|
snprintf(msg, sizeof(msg), "Device %s is in use.", firm_ctx->req->bdev_name);
|
2018-04-09 17:57:31 +00:00
|
|
|
free(opt);
|
2020-11-09 03:25:31 +00:00
|
|
|
goto err;
|
2017-07-11 23:32:32 +00:00
|
|
|
}
|
2018-04-09 17:57:31 +00:00
|
|
|
|
2020-02-21 14:58:08 +00:00
|
|
|
/* Save the thread where the base device is opened */
|
|
|
|
opt->thread = spdk_get_thread();
|
|
|
|
|
2018-04-09 17:57:31 +00:00
|
|
|
opt->desc = desc;
|
|
|
|
opt->bdev = bdev;
|
|
|
|
TAILQ_INSERT_TAIL(&firm_ctx->desc_head, opt, tqlst);
|
2017-07-11 23:32:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* find a descriptor associated with our bdev
|
|
|
|
*/
|
|
|
|
firm_ctx->desc = NULL;
|
|
|
|
TAILQ_FOREACH(opt, &firm_ctx->desc_head, tqlst) {
|
|
|
|
if (opt->bdev == bdev) {
|
|
|
|
firm_ctx->desc = opt->desc;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!firm_ctx->desc) {
|
2020-11-09 03:25:31 +00:00
|
|
|
snprintf(msg, sizeof(msg), "No descriptor were found.");
|
|
|
|
goto err;
|
2017-07-11 23:32:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
firm_ctx->ch = spdk_bdev_get_io_channel(firm_ctx->desc);
|
|
|
|
if (!firm_ctx->ch) {
|
2020-11-09 03:25:31 +00:00
|
|
|
snprintf(msg, sizeof(msg), "No channels were found.");
|
|
|
|
goto err;
|
2017-07-11 23:32:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fd = open(firm_ctx->req->filename, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
2020-11-09 03:25:31 +00:00
|
|
|
snprintf(msg, sizeof(msg), "open file failed.");
|
|
|
|
goto err;
|
2017-07-11 23:32:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rc = fstat(fd, &fw_stat);
|
|
|
|
if (rc < 0) {
|
|
|
|
close(fd);
|
2020-11-09 03:25:31 +00:00
|
|
|
snprintf(msg, sizeof(msg), "fstat failed.");
|
|
|
|
goto err;
|
2017-07-11 23:32:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
firm_ctx->size = fw_stat.st_size;
|
|
|
|
if (fw_stat.st_size % 4) {
|
|
|
|
close(fd);
|
2020-11-09 03:25:31 +00:00
|
|
|
snprintf(msg, sizeof(msg), "Firmware image size is not multiple of 4.");
|
|
|
|
goto err;
|
2017-07-11 23:32:32 +00:00
|
|
|
}
|
|
|
|
|
2019-06-27 04:47:03 +00:00
|
|
|
firm_ctx->fw_image = spdk_zmalloc(firm_ctx->size, 4096, NULL,
|
|
|
|
SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA);
|
2017-07-11 23:32:32 +00:00
|
|
|
if (!firm_ctx->fw_image) {
|
|
|
|
close(fd);
|
2020-11-09 03:25:31 +00:00
|
|
|
snprintf(msg, sizeof(msg), "Memory allocation error.");
|
|
|
|
goto err;
|
2017-07-11 23:32:32 +00:00
|
|
|
}
|
|
|
|
firm_ctx->p = firm_ctx->fw_image;
|
|
|
|
|
|
|
|
if (read(fd, firm_ctx->p, firm_ctx->size) != ((ssize_t)(firm_ctx->size))) {
|
|
|
|
close(fd);
|
2020-11-09 03:25:31 +00:00
|
|
|
snprintf(msg, sizeof(msg), "Read firmware image failed!");
|
|
|
|
goto err;
|
2017-07-11 23:32:32 +00:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
firm_ctx->offset = 0;
|
|
|
|
firm_ctx->size_remaining = firm_ctx->size;
|
|
|
|
firm_ctx->transfer = spdk_min(firm_ctx->size_remaining, 4096);
|
|
|
|
|
|
|
|
cmd = malloc(sizeof(struct spdk_nvme_cmd));
|
|
|
|
if (!cmd) {
|
2020-11-09 03:25:31 +00:00
|
|
|
snprintf(msg, sizeof(msg), "Memory allocation error.");
|
|
|
|
goto err;
|
2017-07-11 23:32:32 +00:00
|
|
|
}
|
|
|
|
memset(cmd, 0, sizeof(struct spdk_nvme_cmd));
|
|
|
|
cmd->opc = SPDK_NVME_OPC_FIRMWARE_IMAGE_DOWNLOAD;
|
|
|
|
|
|
|
|
cmd->cdw10 = (firm_ctx->transfer >> 2) - 1;
|
|
|
|
cmd->cdw11 = firm_ctx->offset >> 2;
|
|
|
|
|
|
|
|
rc = spdk_bdev_nvme_admin_passthru(firm_ctx->desc, firm_ctx->ch, cmd, firm_ctx->p,
|
|
|
|
firm_ctx->transfer, apply_firmware_complete, firm_ctx);
|
2020-11-09 03:25:31 +00:00
|
|
|
if (rc == 0) {
|
|
|
|
/* normal return here. */
|
2017-07-11 23:32:32 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-11-09 03:25:31 +00:00
|
|
|
|
|
|
|
free(cmd);
|
|
|
|
snprintf(msg, sizeof(msg), "Read firmware image failed!");
|
|
|
|
err:
|
|
|
|
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, msg);
|
|
|
|
apply_firmware_cleanup(firm_ctx);
|
2017-07-11 23:32:32 +00:00
|
|
|
}
|
2020-05-10 07:46:07 +00:00
|
|
|
SPDK_RPC_REGISTER("bdev_nvme_apply_firmware", rpc_bdev_nvme_apply_firmware, SPDK_RPC_RUNTIME)
|
2019-08-27 20:43:52 +00:00
|
|
|
SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_nvme_apply_firmware, apply_nvme_firmware)
|