1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (c) Intel Corporation. All rights reserved. 5 * Copyright (c) 2019-2021 Mellanox Technologies LTD. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * * Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * * Neither the name of Intel Corporation nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include "spdk/stdinc.h" 35 36 #include "bdev_nvme.h" 37 38 #include "spdk/config.h" 39 40 #include "spdk/string.h" 41 #include "spdk/rpc.h" 42 #include "spdk/util.h" 43 #include "spdk/env.h" 44 #include "spdk/nvme.h" 45 #include "spdk/nvme_spec.h" 46 47 #include "spdk/log.h" 48 #include "spdk/bdev_module.h" 49 50 struct open_descriptors { 51 void *desc; 52 struct spdk_bdev *bdev; 53 TAILQ_ENTRY(open_descriptors) tqlst; 54 struct spdk_thread *thread; 55 }; 56 typedef TAILQ_HEAD(, open_descriptors) open_descriptors_t; 57 58 static int 59 rpc_decode_action_on_timeout(const struct spdk_json_val *val, void *out) 60 { 61 enum spdk_bdev_timeout_action *action = out; 62 63 if (spdk_json_strequal(val, "none") == true) { 64 *action = SPDK_BDEV_NVME_TIMEOUT_ACTION_NONE; 65 } else if (spdk_json_strequal(val, "abort") == true) { 66 *action = SPDK_BDEV_NVME_TIMEOUT_ACTION_ABORT; 67 } else if (spdk_json_strequal(val, "reset") == true) { 68 *action = SPDK_BDEV_NVME_TIMEOUT_ACTION_RESET; 69 } else { 70 SPDK_NOTICELOG("Invalid parameter value: action_on_timeout\n"); 71 return -EINVAL; 72 } 73 74 return 0; 75 } 76 77 static const struct spdk_json_object_decoder rpc_bdev_nvme_options_decoders[] = { 78 {"action_on_timeout", offsetof(struct spdk_bdev_nvme_opts, action_on_timeout), rpc_decode_action_on_timeout, true}, 79 {"timeout_us", offsetof(struct spdk_bdev_nvme_opts, timeout_us), spdk_json_decode_uint64, true}, 80 {"timeout_admin_us", offsetof(struct spdk_bdev_nvme_opts, timeout_admin_us), spdk_json_decode_uint64, true}, 81 {"keep_alive_timeout_ms", offsetof(struct spdk_bdev_nvme_opts, keep_alive_timeout_ms), spdk_json_decode_uint32, true}, 82 {"retry_count", offsetof(struct spdk_bdev_nvme_opts, transport_retry_count), spdk_json_decode_uint32, true}, 83 {"arbitration_burst", offsetof(struct spdk_bdev_nvme_opts, arbitration_burst), spdk_json_decode_uint32, true}, 84 {"low_priority_weight", offsetof(struct spdk_bdev_nvme_opts, low_priority_weight), spdk_json_decode_uint32, true}, 85 {"medium_priority_weight", offsetof(struct spdk_bdev_nvme_opts, medium_priority_weight), spdk_json_decode_uint32, true}, 86 {"high_priority_weight", offsetof(struct spdk_bdev_nvme_opts, high_priority_weight), spdk_json_decode_uint32, true}, 87 {"nvme_adminq_poll_period_us", offsetof(struct spdk_bdev_nvme_opts, nvme_adminq_poll_period_us), spdk_json_decode_uint64, true}, 88 {"nvme_ioq_poll_period_us", offsetof(struct spdk_bdev_nvme_opts, nvme_ioq_poll_period_us), spdk_json_decode_uint64, true}, 89 {"io_queue_requests", offsetof(struct spdk_bdev_nvme_opts, io_queue_requests), spdk_json_decode_uint32, true}, 90 {"delay_cmd_submit", offsetof(struct spdk_bdev_nvme_opts, delay_cmd_submit), spdk_json_decode_bool, true}, 91 {"transport_retry_count", offsetof(struct spdk_bdev_nvme_opts, transport_retry_count), spdk_json_decode_uint32, true}, 92 {"bdev_retry_count", offsetof(struct spdk_bdev_nvme_opts, bdev_retry_count), spdk_json_decode_int32, true}, 93 }; 94 95 static void 96 rpc_bdev_nvme_set_options(struct spdk_jsonrpc_request *request, 97 const struct spdk_json_val *params) 98 { 99 struct spdk_bdev_nvme_opts opts; 100 int rc; 101 102 bdev_nvme_get_opts(&opts); 103 if (params && spdk_json_decode_object(params, rpc_bdev_nvme_options_decoders, 104 SPDK_COUNTOF(rpc_bdev_nvme_options_decoders), 105 &opts)) { 106 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 107 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 108 "spdk_json_decode_object failed"); 109 return; 110 } 111 112 rc = bdev_nvme_set_opts(&opts); 113 if (rc) { 114 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc)); 115 return; 116 } 117 118 spdk_jsonrpc_send_bool_response(request, true); 119 120 return; 121 } 122 SPDK_RPC_REGISTER("bdev_nvme_set_options", rpc_bdev_nvme_set_options, 123 SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME) 124 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_nvme_set_options, set_bdev_nvme_options) 125 126 struct rpc_bdev_nvme_hotplug { 127 bool enabled; 128 uint64_t period_us; 129 }; 130 131 static const struct spdk_json_object_decoder rpc_bdev_nvme_hotplug_decoders[] = { 132 {"enable", offsetof(struct rpc_bdev_nvme_hotplug, enabled), spdk_json_decode_bool, false}, 133 {"period_us", offsetof(struct rpc_bdev_nvme_hotplug, period_us), spdk_json_decode_uint64, true}, 134 }; 135 136 static void 137 rpc_bdev_nvme_set_hotplug_done(void *ctx) 138 { 139 struct spdk_jsonrpc_request *request = ctx; 140 141 spdk_jsonrpc_send_bool_response(request, true); 142 } 143 144 static void 145 rpc_bdev_nvme_set_hotplug(struct spdk_jsonrpc_request *request, 146 const struct spdk_json_val *params) 147 { 148 struct rpc_bdev_nvme_hotplug req = {false, 0}; 149 int rc; 150 151 if (spdk_json_decode_object(params, rpc_bdev_nvme_hotplug_decoders, 152 SPDK_COUNTOF(rpc_bdev_nvme_hotplug_decoders), &req)) { 153 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 154 rc = -EINVAL; 155 goto invalid; 156 } 157 158 rc = bdev_nvme_set_hotplug(req.enabled, req.period_us, rpc_bdev_nvme_set_hotplug_done, 159 request); 160 if (rc) { 161 goto invalid; 162 } 163 164 return; 165 invalid: 166 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, spdk_strerror(-rc)); 167 } 168 SPDK_RPC_REGISTER("bdev_nvme_set_hotplug", rpc_bdev_nvme_set_hotplug, SPDK_RPC_RUNTIME) 169 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_nvme_set_hotplug, set_bdev_nvme_hotplug) 170 171 struct rpc_bdev_nvme_attach_controller { 172 char *name; 173 char *trtype; 174 char *adrfam; 175 char *traddr; 176 char *trsvcid; 177 char *priority; 178 char *subnqn; 179 char *hostnqn; 180 char *hostaddr; 181 char *hostsvcid; 182 bool prchk_reftag; 183 bool prchk_guard; 184 uint64_t fabrics_connect_timeout_us; 185 char *multipath; 186 struct spdk_nvme_ctrlr_opts opts; 187 }; 188 189 static void 190 free_rpc_bdev_nvme_attach_controller(struct rpc_bdev_nvme_attach_controller *req) 191 { 192 free(req->name); 193 free(req->trtype); 194 free(req->adrfam); 195 free(req->traddr); 196 free(req->trsvcid); 197 free(req->priority); 198 free(req->subnqn); 199 free(req->hostnqn); 200 free(req->hostaddr); 201 free(req->hostsvcid); 202 free(req->multipath); 203 } 204 205 static const struct spdk_json_object_decoder rpc_bdev_nvme_attach_controller_decoders[] = { 206 {"name", offsetof(struct rpc_bdev_nvme_attach_controller, name), spdk_json_decode_string}, 207 {"trtype", offsetof(struct rpc_bdev_nvme_attach_controller, trtype), spdk_json_decode_string}, 208 {"traddr", offsetof(struct rpc_bdev_nvme_attach_controller, traddr), spdk_json_decode_string}, 209 210 {"adrfam", offsetof(struct rpc_bdev_nvme_attach_controller, adrfam), spdk_json_decode_string, true}, 211 {"trsvcid", offsetof(struct rpc_bdev_nvme_attach_controller, trsvcid), spdk_json_decode_string, true}, 212 {"priority", offsetof(struct rpc_bdev_nvme_attach_controller, priority), spdk_json_decode_string, true}, 213 {"subnqn", offsetof(struct rpc_bdev_nvme_attach_controller, subnqn), spdk_json_decode_string, true}, 214 {"hostnqn", offsetof(struct rpc_bdev_nvme_attach_controller, hostnqn), spdk_json_decode_string, true}, 215 {"hostaddr", offsetof(struct rpc_bdev_nvme_attach_controller, hostaddr), spdk_json_decode_string, true}, 216 {"hostsvcid", offsetof(struct rpc_bdev_nvme_attach_controller, hostsvcid), spdk_json_decode_string, true}, 217 218 {"prchk_reftag", offsetof(struct rpc_bdev_nvme_attach_controller, prchk_reftag), spdk_json_decode_bool, true}, 219 {"prchk_guard", offsetof(struct rpc_bdev_nvme_attach_controller, prchk_guard), spdk_json_decode_bool, true}, 220 {"hdgst", offsetof(struct rpc_bdev_nvme_attach_controller, opts.header_digest), spdk_json_decode_bool, true}, 221 {"ddgst", offsetof(struct rpc_bdev_nvme_attach_controller, opts.data_digest), spdk_json_decode_bool, true}, 222 {"fabrics_connect_timeout_us", offsetof(struct rpc_bdev_nvme_attach_controller, opts.fabrics_connect_timeout_us), spdk_json_decode_uint64, true}, 223 {"multipath", offsetof(struct rpc_bdev_nvme_attach_controller, multipath), spdk_json_decode_string, true}, 224 }; 225 226 #define NVME_MAX_BDEVS_PER_RPC 128 227 228 struct rpc_bdev_nvme_attach_controller_ctx { 229 struct rpc_bdev_nvme_attach_controller req; 230 uint32_t count; 231 size_t bdev_count; 232 const char *names[NVME_MAX_BDEVS_PER_RPC]; 233 struct spdk_jsonrpc_request *request; 234 }; 235 236 static void 237 rpc_bdev_nvme_attach_controller_examined(void *cb_ctx) 238 { 239 struct rpc_bdev_nvme_attach_controller_ctx *ctx = cb_ctx; 240 struct spdk_jsonrpc_request *request = ctx->request; 241 struct spdk_json_write_ctx *w; 242 size_t i; 243 244 w = spdk_jsonrpc_begin_result(request); 245 spdk_json_write_array_begin(w); 246 for (i = 0; i < ctx->bdev_count; i++) { 247 spdk_json_write_string(w, ctx->names[i]); 248 } 249 spdk_json_write_array_end(w); 250 spdk_jsonrpc_end_result(request, w); 251 252 free_rpc_bdev_nvme_attach_controller(&ctx->req); 253 free(ctx); 254 } 255 256 static void 257 rpc_bdev_nvme_attach_controller_done(void *cb_ctx, size_t bdev_count, int rc) 258 { 259 struct rpc_bdev_nvme_attach_controller_ctx *ctx = cb_ctx; 260 struct spdk_jsonrpc_request *request = ctx->request; 261 262 if (rc < 0) { 263 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters"); 264 free_rpc_bdev_nvme_attach_controller(&ctx->req); 265 free(ctx); 266 return; 267 } 268 269 ctx->bdev_count = bdev_count; 270 spdk_bdev_wait_for_examine(rpc_bdev_nvme_attach_controller_examined, ctx); 271 } 272 273 static void 274 rpc_bdev_nvme_attach_controller(struct spdk_jsonrpc_request *request, 275 const struct spdk_json_val *params) 276 { 277 struct rpc_bdev_nvme_attach_controller_ctx *ctx; 278 struct spdk_nvme_transport_id trid = {}; 279 const struct spdk_nvme_ctrlr_opts *opts; 280 const struct spdk_nvme_transport_id *ctrlr_trid; 281 uint32_t prchk_flags = 0; 282 struct nvme_ctrlr *ctrlr = NULL; 283 size_t len, maxlen; 284 bool multipath = false; 285 int rc; 286 287 ctx = calloc(1, sizeof(*ctx)); 288 if (!ctx) { 289 spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM)); 290 return; 291 } 292 293 spdk_nvme_ctrlr_get_default_ctrlr_opts(&ctx->req.opts, sizeof(ctx->req.opts)); 294 295 if (spdk_json_decode_object(params, rpc_bdev_nvme_attach_controller_decoders, 296 SPDK_COUNTOF(rpc_bdev_nvme_attach_controller_decoders), 297 &ctx->req)) { 298 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 299 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 300 "spdk_json_decode_object failed"); 301 goto cleanup; 302 } 303 304 /* Parse trstring */ 305 rc = spdk_nvme_transport_id_populate_trstring(&trid, ctx->req.trtype); 306 if (rc < 0) { 307 SPDK_ERRLOG("Failed to parse trtype: %s\n", ctx->req.trtype); 308 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse trtype: %s", 309 ctx->req.trtype); 310 goto cleanup; 311 } 312 313 /* Parse trtype */ 314 rc = spdk_nvme_transport_id_parse_trtype(&trid.trtype, ctx->req.trtype); 315 assert(rc == 0); 316 317 /* Parse traddr */ 318 maxlen = sizeof(trid.traddr); 319 len = strnlen(ctx->req.traddr, maxlen); 320 if (len == maxlen) { 321 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "traddr too long: %s", 322 ctx->req.traddr); 323 goto cleanup; 324 } 325 memcpy(trid.traddr, ctx->req.traddr, len + 1); 326 327 /* Parse adrfam */ 328 if (ctx->req.adrfam) { 329 rc = spdk_nvme_transport_id_parse_adrfam(&trid.adrfam, ctx->req.adrfam); 330 if (rc < 0) { 331 SPDK_ERRLOG("Failed to parse adrfam: %s\n", ctx->req.adrfam); 332 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse adrfam: %s", 333 ctx->req.adrfam); 334 goto cleanup; 335 } 336 } 337 338 /* Parse trsvcid */ 339 if (ctx->req.trsvcid) { 340 maxlen = sizeof(trid.trsvcid); 341 len = strnlen(ctx->req.trsvcid, maxlen); 342 if (len == maxlen) { 343 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "trsvcid too long: %s", 344 ctx->req.trsvcid); 345 goto cleanup; 346 } 347 memcpy(trid.trsvcid, ctx->req.trsvcid, len + 1); 348 } 349 350 /* Parse priority for the NVMe-oF transport connection */ 351 if (ctx->req.priority) { 352 trid.priority = spdk_strtol(ctx->req.priority, 10); 353 } 354 355 /* Parse subnqn */ 356 if (ctx->req.subnqn) { 357 maxlen = sizeof(trid.subnqn); 358 len = strnlen(ctx->req.subnqn, maxlen); 359 if (len == maxlen) { 360 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "subnqn too long: %s", 361 ctx->req.subnqn); 362 goto cleanup; 363 } 364 memcpy(trid.subnqn, ctx->req.subnqn, len + 1); 365 } 366 367 if (ctx->req.hostnqn) { 368 snprintf(ctx->req.opts.hostnqn, sizeof(ctx->req.opts.hostnqn), "%s", 369 ctx->req.hostnqn); 370 } 371 372 if (ctx->req.hostaddr) { 373 maxlen = sizeof(ctx->req.opts.src_addr); 374 len = strnlen(ctx->req.hostaddr, maxlen); 375 if (len == maxlen) { 376 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "hostaddr too long: %s", 377 ctx->req.hostaddr); 378 goto cleanup; 379 } 380 snprintf(ctx->req.opts.src_addr, maxlen, "%s", ctx->req.hostaddr); 381 } 382 383 if (ctx->req.hostsvcid) { 384 maxlen = sizeof(ctx->req.opts.src_svcid); 385 len = strnlen(ctx->req.hostsvcid, maxlen); 386 if (len == maxlen) { 387 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "hostsvcid too long: %s", 388 ctx->req.hostsvcid); 389 goto cleanup; 390 } 391 snprintf(ctx->req.opts.src_svcid, maxlen, "%s", ctx->req.hostsvcid); 392 } 393 394 ctrlr = nvme_ctrlr_get_by_name(ctx->req.name); 395 396 if (ctrlr) { 397 if (ctx->req.multipath == NULL) { 398 /* For now, this means add a failover path. This maintains backward compatibility 399 * with past behavior. In the future, this behavior will change to "disable". */ 400 SPDK_ERRLOG("The multipath parameter was not specified to bdev_nvme_attach_controller but " 401 "it was used to add a failover path. This behavior will default to rejecting " 402 "the request in the future. Specify the 'multipath' parameter to control the behavior"); 403 ctx->req.multipath = strdup("failover"); 404 if (ctx->req.multipath == NULL) { 405 SPDK_ERRLOG("cannot allocate multipath failover string\n"); 406 goto cleanup; 407 } 408 } 409 410 opts = spdk_nvme_ctrlr_get_opts(ctrlr->ctrlr); 411 ctrlr_trid = spdk_nvme_ctrlr_get_transport_id(ctrlr->ctrlr); 412 413 /* This controller already exists. Check what the user wants to do. */ 414 if (strcasecmp(ctx->req.multipath, "disable") == 0) { 415 /* The user does not want to do any form of multipathing. */ 416 spdk_jsonrpc_send_error_response_fmt(request, -EALREADY, 417 "A controller named %s already exists and multipath is disabled\n", 418 ctx->req.name); 419 goto cleanup; 420 } else if (strcasecmp(ctx->req.multipath, "failover") == 0 || 421 strcasecmp(ctx->req.multipath, "multipath") == 0) { 422 /* The user wants to add this as a failover path or add this to create multipath. */ 423 424 if (strncmp(trid.traddr, ctrlr_trid->traddr, sizeof(trid.traddr)) == 0 && 425 strncmp(trid.trsvcid, ctrlr_trid->trsvcid, sizeof(trid.trsvcid)) == 0 && 426 strncmp(ctx->req.opts.src_addr, opts->src_addr, sizeof(opts->src_addr)) == 0 && 427 strncmp(ctx->req.opts.src_svcid, opts->src_svcid, sizeof(opts->src_svcid)) == 0) { 428 /* Exactly same network path can't be added a second time */ 429 spdk_jsonrpc_send_error_response_fmt(request, -EALREADY, 430 "A controller named %s already exists with the specified network path\n", 431 ctx->req.name); 432 goto cleanup; 433 } 434 } else { 435 /* Invalid multipath option */ 436 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, 437 "Invalid multipath parameter: %s\n", 438 ctx->req.multipath); 439 goto cleanup; 440 } 441 442 if (strncmp(trid.subnqn, 443 ctrlr_trid->subnqn, 444 SPDK_NVMF_NQN_MAX_LEN) != 0) { 445 /* Different SUBNQN is not allowed when specifying the same controller name. */ 446 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, 447 "A controller named %s already exists, but uses a different subnqn (%s)\n", 448 ctx->req.name, ctrlr_trid->subnqn); 449 goto cleanup; 450 } 451 452 453 454 if (strncmp(ctx->req.opts.hostnqn, opts->hostnqn, SPDK_NVMF_NQN_MAX_LEN) != 0) { 455 /* Different HOSTNQN is not allowed when specifying the same controller name. */ 456 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, 457 "A controller named %s already exists, but uses a different hostnqn (%s)\n", 458 ctx->req.name, opts->hostnqn); 459 goto cleanup; 460 } 461 462 if (ctx->req.prchk_guard || ctx->req.prchk_reftag) { 463 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, 464 "A controller named %s already exists. To add a path, do not specify PI options.\n", 465 ctx->req.name); 466 goto cleanup; 467 } 468 } 469 470 if (ctx->req.prchk_reftag) { 471 prchk_flags |= SPDK_NVME_IO_FLAGS_PRCHK_REFTAG; 472 } 473 474 if (ctx->req.prchk_guard) { 475 prchk_flags |= SPDK_NVME_IO_FLAGS_PRCHK_GUARD; 476 } 477 478 if (ctx->req.multipath != NULL && strcasecmp(ctx->req.multipath, "multipath") == 0) { 479 multipath = true; 480 } 481 482 ctx->request = request; 483 ctx->count = NVME_MAX_BDEVS_PER_RPC; 484 rc = bdev_nvme_create(&trid, ctx->req.name, ctx->names, ctx->count, prchk_flags, 485 rpc_bdev_nvme_attach_controller_done, ctx, &ctx->req.opts, 486 multipath); 487 if (rc) { 488 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc)); 489 goto cleanup; 490 } 491 492 return; 493 494 cleanup: 495 free_rpc_bdev_nvme_attach_controller(&ctx->req); 496 free(ctx); 497 } 498 SPDK_RPC_REGISTER("bdev_nvme_attach_controller", rpc_bdev_nvme_attach_controller, 499 SPDK_RPC_RUNTIME) 500 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_nvme_attach_controller, construct_nvme_bdev) 501 502 static void 503 rpc_dump_nvme_bdev_controller_info(struct nvme_bdev_ctrlr *nbdev_ctrlr, void *ctx) 504 { 505 struct spdk_json_write_ctx *w = ctx; 506 struct spdk_nvme_transport_id *trid; 507 struct nvme_ctrlr *nvme_ctrlr; 508 const struct spdk_nvme_ctrlr_opts *opts; 509 510 spdk_json_write_object_begin(w); 511 spdk_json_write_named_string(w, "name", nbdev_ctrlr->name); 512 513 spdk_json_write_named_array_begin(w, "ctrlrs"); 514 TAILQ_FOREACH(nvme_ctrlr, &nbdev_ctrlr->ctrlrs, tailq) { 515 spdk_json_write_object_begin(w); 516 #ifdef SPDK_CONFIG_NVME_CUSE 517 size_t cuse_name_size = 128; 518 char cuse_name[cuse_name_size]; 519 520 int rc = spdk_nvme_cuse_get_ctrlr_name(nvme_ctrlr->ctrlr, cuse_name, &cuse_name_size); 521 if (rc == 0) { 522 spdk_json_write_named_string(w, "cuse_device", cuse_name); 523 } 524 #endif 525 trid = &nvme_ctrlr->active_path_id->trid; 526 spdk_json_write_named_object_begin(w, "trid"); 527 nvme_bdev_dump_trid_json(trid, w); 528 spdk_json_write_object_end(w); 529 530 opts = spdk_nvme_ctrlr_get_opts(nvme_ctrlr->ctrlr); 531 spdk_json_write_named_object_begin(w, "host"); 532 spdk_json_write_named_string(w, "nqn", opts->hostnqn); 533 spdk_json_write_named_string(w, "addr", opts->src_addr); 534 spdk_json_write_named_string(w, "svcid", opts->src_svcid); 535 spdk_json_write_object_end(w); 536 spdk_json_write_object_end(w); 537 } 538 spdk_json_write_array_end(w); 539 spdk_json_write_object_end(w); 540 } 541 542 struct rpc_bdev_nvme_get_controllers { 543 char *name; 544 }; 545 546 static void 547 free_rpc_bdev_nvme_get_controllers(struct rpc_bdev_nvme_get_controllers *r) 548 { 549 free(r->name); 550 } 551 552 static const struct spdk_json_object_decoder rpc_bdev_nvme_get_controllers_decoders[] = { 553 {"name", offsetof(struct rpc_bdev_nvme_get_controllers, name), spdk_json_decode_string, true}, 554 }; 555 556 static void 557 rpc_bdev_nvme_get_controllers(struct spdk_jsonrpc_request *request, 558 const struct spdk_json_val *params) 559 { 560 struct rpc_bdev_nvme_get_controllers req = {}; 561 struct spdk_json_write_ctx *w; 562 struct nvme_bdev_ctrlr *nbdev_ctrlr = NULL; 563 564 if (params && spdk_json_decode_object(params, rpc_bdev_nvme_get_controllers_decoders, 565 SPDK_COUNTOF(rpc_bdev_nvme_get_controllers_decoders), 566 &req)) { 567 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 568 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 569 "spdk_json_decode_object failed"); 570 goto cleanup; 571 } 572 573 if (req.name) { 574 nbdev_ctrlr = nvme_bdev_ctrlr_get_by_name(req.name); 575 if (nbdev_ctrlr == NULL) { 576 SPDK_ERRLOG("ctrlr '%s' does not exist\n", req.name); 577 spdk_jsonrpc_send_error_response_fmt(request, EINVAL, "Controller %s does not exist", req.name); 578 goto cleanup; 579 } 580 } 581 582 w = spdk_jsonrpc_begin_result(request); 583 spdk_json_write_array_begin(w); 584 585 if (nbdev_ctrlr != NULL) { 586 rpc_dump_nvme_bdev_controller_info(nbdev_ctrlr, w); 587 } else { 588 nvme_bdev_ctrlr_for_each(rpc_dump_nvme_bdev_controller_info, w); 589 } 590 591 spdk_json_write_array_end(w); 592 593 spdk_jsonrpc_end_result(request, w); 594 595 cleanup: 596 free_rpc_bdev_nvme_get_controllers(&req); 597 } 598 SPDK_RPC_REGISTER("bdev_nvme_get_controllers", rpc_bdev_nvme_get_controllers, SPDK_RPC_RUNTIME) 599 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_nvme_get_controllers, get_nvme_controllers) 600 601 struct rpc_bdev_nvme_detach_controller { 602 char *name; 603 char *trtype; 604 char *adrfam; 605 char *traddr; 606 char *trsvcid; 607 char *subnqn; 608 char *hostaddr; 609 char *hostsvcid; 610 }; 611 612 static void 613 free_rpc_bdev_nvme_detach_controller(struct rpc_bdev_nvme_detach_controller *req) 614 { 615 free(req->name); 616 free(req->trtype); 617 free(req->adrfam); 618 free(req->traddr); 619 free(req->trsvcid); 620 free(req->subnqn); 621 free(req->hostaddr); 622 free(req->hostsvcid); 623 } 624 625 static const struct spdk_json_object_decoder rpc_bdev_nvme_detach_controller_decoders[] = { 626 {"name", offsetof(struct rpc_bdev_nvme_detach_controller, name), spdk_json_decode_string}, 627 {"trtype", offsetof(struct rpc_bdev_nvme_detach_controller, trtype), spdk_json_decode_string, true}, 628 {"traddr", offsetof(struct rpc_bdev_nvme_detach_controller, traddr), spdk_json_decode_string, true}, 629 {"adrfam", offsetof(struct rpc_bdev_nvme_detach_controller, adrfam), spdk_json_decode_string, true}, 630 {"trsvcid", offsetof(struct rpc_bdev_nvme_detach_controller, trsvcid), spdk_json_decode_string, true}, 631 {"subnqn", offsetof(struct rpc_bdev_nvme_detach_controller, subnqn), spdk_json_decode_string, true}, 632 {"hostaddr", offsetof(struct rpc_bdev_nvme_detach_controller, hostaddr), spdk_json_decode_string, true}, 633 {"hostsvcid", offsetof(struct rpc_bdev_nvme_detach_controller, hostsvcid), spdk_json_decode_string, true}, 634 }; 635 636 static void 637 rpc_bdev_nvme_detach_controller(struct spdk_jsonrpc_request *request, 638 const struct spdk_json_val *params) 639 { 640 struct rpc_bdev_nvme_detach_controller req = {NULL}; 641 struct nvme_path_id path = {}; 642 size_t len, maxlen; 643 int rc = 0; 644 645 if (spdk_json_decode_object(params, rpc_bdev_nvme_detach_controller_decoders, 646 SPDK_COUNTOF(rpc_bdev_nvme_detach_controller_decoders), 647 &req)) { 648 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 649 "spdk_json_decode_object failed"); 650 goto cleanup; 651 } 652 653 if (req.trtype != NULL) { 654 rc = spdk_nvme_transport_id_populate_trstring(&path.trid, req.trtype); 655 if (rc < 0) { 656 SPDK_ERRLOG("Failed to parse trtype: %s\n", req.trtype); 657 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse trtype: %s", 658 req.trtype); 659 goto cleanup; 660 } 661 662 rc = spdk_nvme_transport_id_parse_trtype(&path.trid.trtype, req.trtype); 663 if (rc < 0) { 664 SPDK_ERRLOG("Failed to parse trtype: %s\n", req.trtype); 665 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse trtype: %s", 666 req.trtype); 667 goto cleanup; 668 } 669 } 670 671 if (req.traddr != NULL) { 672 maxlen = sizeof(path.trid.traddr); 673 len = strnlen(req.traddr, maxlen); 674 if (len == maxlen) { 675 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "traddr too long: %s", 676 req.traddr); 677 goto cleanup; 678 } 679 memcpy(path.trid.traddr, req.traddr, len + 1); 680 } 681 682 if (req.adrfam != NULL) { 683 rc = spdk_nvme_transport_id_parse_adrfam(&path.trid.adrfam, req.adrfam); 684 if (rc < 0) { 685 SPDK_ERRLOG("Failed to parse adrfam: %s\n", req.adrfam); 686 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse adrfam: %s", 687 req.adrfam); 688 goto cleanup; 689 } 690 } 691 692 if (req.trsvcid != NULL) { 693 maxlen = sizeof(path.trid.trsvcid); 694 len = strnlen(req.trsvcid, maxlen); 695 if (len == maxlen) { 696 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "trsvcid too long: %s", 697 req.trsvcid); 698 goto cleanup; 699 } 700 memcpy(path.trid.trsvcid, req.trsvcid, len + 1); 701 } 702 703 /* Parse subnqn */ 704 if (req.subnqn != NULL) { 705 maxlen = sizeof(path.trid.subnqn); 706 len = strnlen(req.subnqn, maxlen); 707 if (len == maxlen) { 708 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "subnqn too long: %s", 709 req.subnqn); 710 goto cleanup; 711 } 712 memcpy(path.trid.subnqn, req.subnqn, len + 1); 713 } 714 715 if (req.hostaddr) { 716 maxlen = sizeof(path.hostid.hostaddr); 717 len = strnlen(req.hostaddr, maxlen); 718 if (len == maxlen) { 719 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "hostaddr too long: %s", 720 req.hostaddr); 721 goto cleanup; 722 } 723 snprintf(path.hostid.hostaddr, maxlen, "%s", req.hostaddr); 724 } 725 726 if (req.hostsvcid) { 727 maxlen = sizeof(path.hostid.hostsvcid); 728 len = strnlen(req.hostsvcid, maxlen); 729 if (len == maxlen) { 730 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "hostsvcid too long: %s", 731 req.hostsvcid); 732 goto cleanup; 733 } 734 snprintf(path.hostid.hostsvcid, maxlen, "%s", req.hostsvcid); 735 } 736 737 rc = bdev_nvme_delete(req.name, &path); 738 739 if (rc != 0) { 740 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc)); 741 goto cleanup; 742 } 743 744 spdk_jsonrpc_send_bool_response(request, true); 745 746 cleanup: 747 free_rpc_bdev_nvme_detach_controller(&req); 748 } 749 SPDK_RPC_REGISTER("bdev_nvme_detach_controller", rpc_bdev_nvme_detach_controller, 750 SPDK_RPC_RUNTIME) 751 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_nvme_detach_controller, delete_nvme_controller) 752 753 struct rpc_apply_firmware { 754 char *filename; 755 char *bdev_name; 756 }; 757 758 static void 759 free_rpc_apply_firmware(struct rpc_apply_firmware *req) 760 { 761 free(req->filename); 762 free(req->bdev_name); 763 } 764 765 static const struct spdk_json_object_decoder rpc_apply_firmware_decoders[] = { 766 {"filename", offsetof(struct rpc_apply_firmware, filename), spdk_json_decode_string}, 767 {"bdev_name", offsetof(struct rpc_apply_firmware, bdev_name), spdk_json_decode_string}, 768 }; 769 770 struct firmware_update_info { 771 void *fw_image; 772 void *p; 773 unsigned int size; 774 unsigned int size_remaining; 775 unsigned int offset; 776 unsigned int transfer; 777 778 void *desc; 779 struct spdk_io_channel *ch; 780 struct spdk_jsonrpc_request *request; 781 struct spdk_nvme_ctrlr *ctrlr; 782 open_descriptors_t desc_head; 783 struct rpc_apply_firmware *req; 784 }; 785 786 static void 787 _apply_firmware_cleanup(void *ctx) 788 { 789 struct spdk_bdev_desc *desc = ctx; 790 791 spdk_bdev_close(desc); 792 } 793 794 static void 795 apply_firmware_cleanup(void *cb_arg) 796 { 797 struct open_descriptors *opt, *tmp; 798 struct firmware_update_info *firm_ctx = cb_arg; 799 800 if (!firm_ctx) { 801 return; 802 } 803 804 if (firm_ctx->fw_image) { 805 spdk_free(firm_ctx->fw_image); 806 } 807 808 if (firm_ctx->req) { 809 free_rpc_apply_firmware(firm_ctx->req); 810 free(firm_ctx->req); 811 } 812 813 if (firm_ctx->ch) { 814 spdk_put_io_channel(firm_ctx->ch); 815 } 816 817 TAILQ_FOREACH_SAFE(opt, &firm_ctx->desc_head, tqlst, tmp) { 818 TAILQ_REMOVE(&firm_ctx->desc_head, opt, tqlst); 819 /* Close the underlying bdev on its same opened thread. */ 820 if (opt->thread && opt->thread != spdk_get_thread()) { 821 spdk_thread_send_msg(opt->thread, _apply_firmware_cleanup, opt->desc); 822 } else { 823 spdk_bdev_close(opt->desc); 824 } 825 free(opt); 826 } 827 free(firm_ctx); 828 } 829 830 static void 831 apply_firmware_complete_reset(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) 832 { 833 struct spdk_json_write_ctx *w; 834 struct firmware_update_info *firm_ctx = cb_arg; 835 836 spdk_bdev_free_io(bdev_io); 837 838 if (!success) { 839 spdk_jsonrpc_send_error_response(firm_ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 840 "firmware commit failed."); 841 apply_firmware_cleanup(firm_ctx); 842 return; 843 } 844 845 if (spdk_nvme_ctrlr_reset(firm_ctx->ctrlr) != 0) { 846 spdk_jsonrpc_send_error_response(firm_ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 847 "Controller reset failed."); 848 apply_firmware_cleanup(firm_ctx); 849 return; 850 } 851 852 w = spdk_jsonrpc_begin_result(firm_ctx->request); 853 spdk_json_write_string(w, "firmware commit succeeded. Controller reset in progress."); 854 spdk_jsonrpc_end_result(firm_ctx->request, w); 855 apply_firmware_cleanup(firm_ctx); 856 } 857 858 static void 859 apply_firmware_complete(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) 860 { 861 struct spdk_nvme_cmd cmd = {}; 862 struct spdk_nvme_fw_commit fw_commit; 863 int slot = 0; 864 int rc; 865 struct firmware_update_info *firm_ctx = cb_arg; 866 enum spdk_nvme_fw_commit_action commit_action = SPDK_NVME_FW_COMMIT_REPLACE_AND_ENABLE_IMG; 867 868 if (!success) { 869 spdk_jsonrpc_send_error_response(firm_ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 870 "firmware download failed ."); 871 spdk_bdev_free_io(bdev_io); 872 apply_firmware_cleanup(firm_ctx); 873 return; 874 } 875 876 firm_ctx->p += firm_ctx->transfer; 877 firm_ctx->offset += firm_ctx->transfer; 878 firm_ctx->size_remaining -= firm_ctx->transfer; 879 880 switch (firm_ctx->size_remaining) { 881 case 0: 882 /* firmware download completed. Commit firmware */ 883 memset(&fw_commit, 0, sizeof(struct spdk_nvme_fw_commit)); 884 fw_commit.fs = slot; 885 fw_commit.ca = commit_action; 886 887 cmd.opc = SPDK_NVME_OPC_FIRMWARE_COMMIT; 888 memcpy(&cmd.cdw10, &fw_commit, sizeof(uint32_t)); 889 rc = spdk_bdev_nvme_admin_passthru(firm_ctx->desc, firm_ctx->ch, &cmd, NULL, 0, 890 apply_firmware_complete_reset, firm_ctx); 891 if (rc) { 892 spdk_jsonrpc_send_error_response(firm_ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 893 "firmware commit failed."); 894 spdk_bdev_free_io(bdev_io); 895 apply_firmware_cleanup(firm_ctx); 896 return; 897 } 898 break; 899 default: 900 firm_ctx->transfer = spdk_min(firm_ctx->size_remaining, 4096); 901 cmd.opc = SPDK_NVME_OPC_FIRMWARE_IMAGE_DOWNLOAD; 902 903 cmd.cdw10 = spdk_nvme_bytes_to_numd(firm_ctx->transfer); 904 cmd.cdw11 = firm_ctx->offset >> 2; 905 rc = spdk_bdev_nvme_admin_passthru(firm_ctx->desc, firm_ctx->ch, &cmd, firm_ctx->p, 906 firm_ctx->transfer, apply_firmware_complete, firm_ctx); 907 if (rc) { 908 spdk_jsonrpc_send_error_response(firm_ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 909 "firmware download failed."); 910 spdk_bdev_free_io(bdev_io); 911 apply_firmware_cleanup(firm_ctx); 912 return; 913 } 914 break; 915 } 916 } 917 918 static void 919 apply_firmware_open_cb(enum spdk_bdev_event_type type, struct spdk_bdev *bdev, void *event_ctx) 920 { 921 } 922 923 static void 924 rpc_bdev_nvme_apply_firmware(struct spdk_jsonrpc_request *request, 925 const struct spdk_json_val *params) 926 { 927 int rc; 928 int fd = -1; 929 struct stat fw_stat; 930 struct spdk_nvme_ctrlr *ctrlr; 931 char msg[1024]; 932 struct spdk_bdev *bdev; 933 struct spdk_bdev *bdev2; 934 struct open_descriptors *opt; 935 struct spdk_bdev_desc *desc; 936 struct spdk_nvme_cmd *cmd; 937 struct firmware_update_info *firm_ctx; 938 939 firm_ctx = calloc(1, sizeof(struct firmware_update_info)); 940 if (!firm_ctx) { 941 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 942 "Memory allocation error."); 943 return; 944 } 945 firm_ctx->fw_image = NULL; 946 TAILQ_INIT(&firm_ctx->desc_head); 947 firm_ctx->request = request; 948 949 firm_ctx->req = calloc(1, sizeof(struct rpc_apply_firmware)); 950 if (!firm_ctx->req) { 951 snprintf(msg, sizeof(msg), "Memory allocation error."); 952 goto err; 953 } 954 955 if (spdk_json_decode_object(params, rpc_apply_firmware_decoders, 956 SPDK_COUNTOF(rpc_apply_firmware_decoders), firm_ctx->req)) { 957 snprintf(msg, sizeof(msg), "spdk_json_decode_object failed."); 958 goto err; 959 } 960 961 if ((bdev = spdk_bdev_get_by_name(firm_ctx->req->bdev_name)) == NULL) { 962 snprintf(msg, sizeof(msg), "bdev %s were not found", firm_ctx->req->bdev_name); 963 goto err; 964 } 965 966 if ((ctrlr = bdev_nvme_get_ctrlr(bdev)) == NULL) { 967 snprintf(msg, sizeof(msg), "Controller information for %s were not found.", 968 firm_ctx->req->bdev_name); 969 goto err; 970 } 971 firm_ctx->ctrlr = ctrlr; 972 973 for (bdev2 = spdk_bdev_first(); bdev2; bdev2 = spdk_bdev_next(bdev2)) { 974 975 if (bdev_nvme_get_ctrlr(bdev2) != ctrlr) { 976 continue; 977 } 978 979 if (!(opt = malloc(sizeof(struct open_descriptors)))) { 980 snprintf(msg, sizeof(msg), "Memory allocation error."); 981 goto err; 982 } 983 984 if (spdk_bdev_open_ext(spdk_bdev_get_name(bdev2), true, apply_firmware_open_cb, NULL, &desc) != 0) { 985 snprintf(msg, sizeof(msg), "Device %s is in use.", firm_ctx->req->bdev_name); 986 free(opt); 987 goto err; 988 } 989 990 /* Save the thread where the base device is opened */ 991 opt->thread = spdk_get_thread(); 992 993 opt->desc = desc; 994 opt->bdev = bdev; 995 TAILQ_INSERT_TAIL(&firm_ctx->desc_head, opt, tqlst); 996 } 997 998 /* 999 * find a descriptor associated with our bdev 1000 */ 1001 firm_ctx->desc = NULL; 1002 TAILQ_FOREACH(opt, &firm_ctx->desc_head, tqlst) { 1003 if (opt->bdev == bdev) { 1004 firm_ctx->desc = opt->desc; 1005 break; 1006 } 1007 } 1008 1009 if (!firm_ctx->desc) { 1010 snprintf(msg, sizeof(msg), "No descriptor were found."); 1011 goto err; 1012 } 1013 1014 firm_ctx->ch = spdk_bdev_get_io_channel(firm_ctx->desc); 1015 if (!firm_ctx->ch) { 1016 snprintf(msg, sizeof(msg), "No channels were found."); 1017 goto err; 1018 } 1019 1020 fd = open(firm_ctx->req->filename, O_RDONLY); 1021 if (fd < 0) { 1022 snprintf(msg, sizeof(msg), "open file failed."); 1023 goto err; 1024 } 1025 1026 rc = fstat(fd, &fw_stat); 1027 if (rc < 0) { 1028 close(fd); 1029 snprintf(msg, sizeof(msg), "fstat failed."); 1030 goto err; 1031 } 1032 1033 firm_ctx->size = fw_stat.st_size; 1034 if (fw_stat.st_size % 4) { 1035 close(fd); 1036 snprintf(msg, sizeof(msg), "Firmware image size is not multiple of 4."); 1037 goto err; 1038 } 1039 1040 firm_ctx->fw_image = spdk_zmalloc(firm_ctx->size, 4096, NULL, 1041 SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA); 1042 if (!firm_ctx->fw_image) { 1043 close(fd); 1044 snprintf(msg, sizeof(msg), "Memory allocation error."); 1045 goto err; 1046 } 1047 firm_ctx->p = firm_ctx->fw_image; 1048 1049 if (read(fd, firm_ctx->p, firm_ctx->size) != ((ssize_t)(firm_ctx->size))) { 1050 close(fd); 1051 snprintf(msg, sizeof(msg), "Read firmware image failed!"); 1052 goto err; 1053 } 1054 close(fd); 1055 1056 firm_ctx->offset = 0; 1057 firm_ctx->size_remaining = firm_ctx->size; 1058 firm_ctx->transfer = spdk_min(firm_ctx->size_remaining, 4096); 1059 1060 cmd = malloc(sizeof(struct spdk_nvme_cmd)); 1061 if (!cmd) { 1062 snprintf(msg, sizeof(msg), "Memory allocation error."); 1063 goto err; 1064 } 1065 memset(cmd, 0, sizeof(struct spdk_nvme_cmd)); 1066 cmd->opc = SPDK_NVME_OPC_FIRMWARE_IMAGE_DOWNLOAD; 1067 1068 cmd->cdw10 = spdk_nvme_bytes_to_numd(firm_ctx->transfer); 1069 cmd->cdw11 = firm_ctx->offset >> 2; 1070 1071 rc = spdk_bdev_nvme_admin_passthru(firm_ctx->desc, firm_ctx->ch, cmd, firm_ctx->p, 1072 firm_ctx->transfer, apply_firmware_complete, firm_ctx); 1073 if (rc == 0) { 1074 /* normal return here. */ 1075 return; 1076 } 1077 1078 free(cmd); 1079 snprintf(msg, sizeof(msg), "Read firmware image failed!"); 1080 err: 1081 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, msg); 1082 apply_firmware_cleanup(firm_ctx); 1083 } 1084 SPDK_RPC_REGISTER("bdev_nvme_apply_firmware", rpc_bdev_nvme_apply_firmware, SPDK_RPC_RUNTIME) 1085 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_nvme_apply_firmware, apply_nvme_firmware) 1086 1087 struct rpc_bdev_nvme_transport_stat_ctx { 1088 struct spdk_jsonrpc_request *request; 1089 struct spdk_json_write_ctx *w; 1090 }; 1091 1092 static void 1093 rpc_bdev_nvme_rdma_stats(struct spdk_json_write_ctx *w, 1094 struct spdk_nvme_transport_poll_group_stat *stat) 1095 { 1096 struct spdk_nvme_rdma_device_stat *device_stats; 1097 uint32_t i; 1098 1099 spdk_json_write_named_array_begin(w, "devices"); 1100 1101 for (i = 0; i < stat->rdma.num_devices; i++) { 1102 device_stats = &stat->rdma.device_stats[i]; 1103 spdk_json_write_object_begin(w); 1104 spdk_json_write_named_string(w, "dev_name", device_stats->name); 1105 spdk_json_write_named_uint64(w, "polls", device_stats->polls); 1106 spdk_json_write_named_uint64(w, "idle_polls", device_stats->idle_polls); 1107 spdk_json_write_named_uint64(w, "completions", device_stats->completions); 1108 spdk_json_write_named_uint64(w, "queued_requests", device_stats->queued_requests); 1109 spdk_json_write_named_uint64(w, "total_send_wrs", device_stats->total_send_wrs); 1110 spdk_json_write_named_uint64(w, "send_doorbell_updates", device_stats->send_doorbell_updates); 1111 spdk_json_write_named_uint64(w, "total_recv_wrs", device_stats->total_recv_wrs); 1112 spdk_json_write_named_uint64(w, "recv_doorbell_updates", device_stats->recv_doorbell_updates); 1113 spdk_json_write_object_end(w); 1114 } 1115 spdk_json_write_array_end(w); 1116 } 1117 1118 static void 1119 rpc_bdev_nvme_pcie_stats(struct spdk_json_write_ctx *w, 1120 struct spdk_nvme_transport_poll_group_stat *stat) 1121 { 1122 spdk_json_write_named_uint64(w, "polls", stat->pcie.polls); 1123 spdk_json_write_named_uint64(w, "idle_polls", stat->pcie.idle_polls); 1124 spdk_json_write_named_uint64(w, "completions", stat->pcie.completions); 1125 spdk_json_write_named_uint64(w, "cq_doorbell_updates", stat->pcie.cq_doorbell_updates); 1126 spdk_json_write_named_uint64(w, "queued_requests", stat->pcie.queued_requests); 1127 spdk_json_write_named_uint64(w, "submitted_requests", stat->pcie.submitted_requests); 1128 spdk_json_write_named_uint64(w, "sq_doobell_updates", stat->pcie.sq_doobell_updates); 1129 } 1130 1131 static void 1132 rpc_bdev_nvme_tcp_stats(struct spdk_json_write_ctx *w, 1133 struct spdk_nvme_transport_poll_group_stat *stat) 1134 { 1135 spdk_json_write_named_uint64(w, "polls", stat->tcp.polls); 1136 spdk_json_write_named_uint64(w, "idle_polls", stat->tcp.idle_polls); 1137 spdk_json_write_named_uint64(w, "socket_completions", stat->tcp.socket_completions); 1138 spdk_json_write_named_uint64(w, "nvme_completions", stat->tcp.nvme_completions); 1139 spdk_json_write_named_uint64(w, "queued_requests", stat->tcp.queued_requests); 1140 spdk_json_write_named_uint64(w, "submitted_requests", stat->tcp.submitted_requests); 1141 } 1142 1143 static void 1144 rpc_bdev_nvme_stats_per_channel(struct spdk_io_channel_iter *i) 1145 { 1146 struct rpc_bdev_nvme_transport_stat_ctx *ctx; 1147 struct spdk_io_channel *ch; 1148 struct nvme_poll_group *group; 1149 struct spdk_nvme_poll_group_stat *stat; 1150 struct spdk_nvme_transport_poll_group_stat *tr_stat; 1151 uint32_t j; 1152 int rc; 1153 1154 ctx = spdk_io_channel_iter_get_ctx(i); 1155 ch = spdk_io_channel_iter_get_channel(i); 1156 group = spdk_io_channel_get_ctx(ch); 1157 1158 rc = spdk_nvme_poll_group_get_stats(group->group, &stat); 1159 if (rc) { 1160 spdk_for_each_channel_continue(i, rc); 1161 return; 1162 } 1163 1164 spdk_json_write_object_begin(ctx->w); 1165 spdk_json_write_named_string(ctx->w, "thread", spdk_thread_get_name(spdk_get_thread())); 1166 spdk_json_write_named_array_begin(ctx->w, "transports"); 1167 1168 for (j = 0; j < stat->num_transports; j++) { 1169 tr_stat = stat->transport_stat[j]; 1170 spdk_json_write_object_begin(ctx->w); 1171 spdk_json_write_named_string(ctx->w, "trname", spdk_nvme_transport_id_trtype_str(tr_stat->trtype)); 1172 1173 switch (stat->transport_stat[j]->trtype) { 1174 case SPDK_NVME_TRANSPORT_RDMA: 1175 rpc_bdev_nvme_rdma_stats(ctx->w, tr_stat); 1176 break; 1177 case SPDK_NVME_TRANSPORT_PCIE: 1178 rpc_bdev_nvme_pcie_stats(ctx->w, tr_stat); 1179 break; 1180 case SPDK_NVME_TRANSPORT_TCP: 1181 rpc_bdev_nvme_tcp_stats(ctx->w, tr_stat); 1182 break; 1183 default: 1184 SPDK_WARNLOG("Can't handle trtype %d %s\n", tr_stat->trtype, 1185 spdk_nvme_transport_id_trtype_str(tr_stat->trtype)); 1186 } 1187 spdk_json_write_object_end(ctx->w); 1188 } 1189 /* transports array */ 1190 spdk_json_write_array_end(ctx->w); 1191 spdk_json_write_object_end(ctx->w); 1192 1193 spdk_nvme_poll_group_free_stats(group->group, stat); 1194 spdk_for_each_channel_continue(i, 0); 1195 } 1196 1197 static void 1198 rpc_bdev_nvme_stats_done(struct spdk_io_channel_iter *i, int status) 1199 { 1200 struct rpc_bdev_nvme_transport_stat_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 1201 1202 spdk_json_write_array_end(ctx->w); 1203 spdk_json_write_object_end(ctx->w); 1204 spdk_jsonrpc_end_result(ctx->request, ctx->w); 1205 free(ctx); 1206 } 1207 1208 static void 1209 rpc_bdev_nvme_get_transport_statistics(struct spdk_jsonrpc_request *request, 1210 const struct spdk_json_val *params) 1211 { 1212 struct rpc_bdev_nvme_transport_stat_ctx *ctx; 1213 1214 if (params) { 1215 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, 1216 "'bdev_nvme_get_transport_statistics' requires no arguments"); 1217 return; 1218 } 1219 1220 ctx = calloc(1, sizeof(*ctx)); 1221 if (!ctx) { 1222 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1223 "Memory allocation error"); 1224 return; 1225 } 1226 ctx->request = request; 1227 ctx->w = spdk_jsonrpc_begin_result(ctx->request); 1228 spdk_json_write_object_begin(ctx->w); 1229 spdk_json_write_named_array_begin(ctx->w, "poll_groups"); 1230 1231 spdk_for_each_channel(&g_nvme_bdev_ctrlrs, 1232 rpc_bdev_nvme_stats_per_channel, 1233 ctx, 1234 rpc_bdev_nvme_stats_done); 1235 } 1236 SPDK_RPC_REGISTER("bdev_nvme_get_transport_statistics", rpc_bdev_nvme_get_transport_statistics, 1237 SPDK_RPC_RUNTIME) 1238 1239 struct rpc_bdev_nvme_reset_controller_req { 1240 char *name; 1241 }; 1242 1243 static void 1244 free_rpc_bdev_nvme_reset_controller_req(struct rpc_bdev_nvme_reset_controller_req *r) 1245 { 1246 free(r->name); 1247 } 1248 1249 static const struct spdk_json_object_decoder rpc_bdev_nvme_reset_controller_req_decoders[] = { 1250 {"name", offsetof(struct rpc_bdev_nvme_reset_controller_req, name), spdk_json_decode_string}, 1251 }; 1252 1253 struct rpc_bdev_nvme_reset_controller_ctx { 1254 struct spdk_jsonrpc_request *request; 1255 bool success; 1256 struct spdk_thread *orig_thread; 1257 }; 1258 1259 static void 1260 _rpc_bdev_nvme_reset_controller_cb(void *_ctx) 1261 { 1262 struct rpc_bdev_nvme_reset_controller_ctx *ctx = _ctx; 1263 1264 spdk_jsonrpc_send_bool_response(ctx->request, ctx->success); 1265 1266 free(ctx); 1267 } 1268 1269 static void 1270 rpc_bdev_nvme_reset_controller_cb(void *cb_arg, bool success) 1271 { 1272 struct rpc_bdev_nvme_reset_controller_ctx *ctx = cb_arg; 1273 1274 ctx->success = success; 1275 1276 spdk_thread_send_msg(ctx->orig_thread, _rpc_bdev_nvme_reset_controller_cb, ctx); 1277 } 1278 1279 static void 1280 rpc_bdev_nvme_reset_controller(struct spdk_jsonrpc_request *request, 1281 const struct spdk_json_val *params) 1282 { 1283 struct rpc_bdev_nvme_reset_controller_req req = {NULL}; 1284 struct rpc_bdev_nvme_reset_controller_ctx *ctx; 1285 struct nvme_ctrlr *nvme_ctrlr; 1286 int rc; 1287 1288 ctx = calloc(1, sizeof(*ctx)); 1289 if (ctx == NULL) { 1290 SPDK_ERRLOG("Memory allocation failed\n"); 1291 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1292 "Memory allocation failed"); 1293 return; 1294 } 1295 1296 if (spdk_json_decode_object(params, rpc_bdev_nvme_reset_controller_req_decoders, 1297 SPDK_COUNTOF(rpc_bdev_nvme_reset_controller_req_decoders), 1298 &req)) { 1299 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 1300 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, spdk_strerror(EINVAL)); 1301 goto err; 1302 } 1303 1304 nvme_ctrlr = nvme_ctrlr_get_by_name(req.name); 1305 if (nvme_ctrlr == NULL) { 1306 SPDK_ERRLOG("Failed at device lookup\n"); 1307 spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV)); 1308 goto err; 1309 } 1310 1311 ctx->request = request; 1312 ctx->orig_thread = spdk_get_thread(); 1313 1314 rc = bdev_nvme_reset_rpc(nvme_ctrlr, rpc_bdev_nvme_reset_controller_cb, ctx); 1315 if (rc != 0) { 1316 SPDK_NOTICELOG("Failed at bdev_nvme_reset_rpc\n"); 1317 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, spdk_strerror(-rc)); 1318 goto err; 1319 } 1320 1321 free_rpc_bdev_nvme_reset_controller_req(&req); 1322 return; 1323 1324 err: 1325 free_rpc_bdev_nvme_reset_controller_req(&req); 1326 free(ctx); 1327 } 1328 SPDK_RPC_REGISTER("bdev_nvme_reset_controller", rpc_bdev_nvme_reset_controller, SPDK_RPC_RUNTIME) 1329 1330 struct rpc_get_controller_health_info { 1331 char *name; 1332 }; 1333 1334 struct spdk_nvme_health_info_context { 1335 struct spdk_jsonrpc_request *request; 1336 struct spdk_nvme_ctrlr *ctrlr; 1337 struct spdk_nvme_health_information_page health_page; 1338 }; 1339 1340 static void 1341 free_rpc_get_controller_health_info(struct rpc_get_controller_health_info *r) 1342 { 1343 free(r->name); 1344 } 1345 1346 static const struct spdk_json_object_decoder rpc_get_controller_health_info_decoders[] = { 1347 {"name", offsetof(struct rpc_get_controller_health_info, name), spdk_json_decode_string, true}, 1348 }; 1349 1350 static void nvme_health_info_cleanup(struct spdk_nvme_health_info_context *context, bool response) 1351 { 1352 if (response == true) { 1353 spdk_jsonrpc_send_error_response(context->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1354 "Internal error."); 1355 } 1356 1357 free(context); 1358 } 1359 1360 static void 1361 get_health_log_page_completion(void *cb_arg, const struct spdk_nvme_cpl *cpl) 1362 { 1363 int i; 1364 char buf[128]; 1365 struct spdk_nvme_health_info_context *context = cb_arg; 1366 struct spdk_jsonrpc_request *request = context->request; 1367 struct spdk_json_write_ctx *w; 1368 struct spdk_nvme_ctrlr *ctrlr = context->ctrlr; 1369 const struct spdk_nvme_transport_id *trid = NULL; 1370 const struct spdk_nvme_ctrlr_data *cdata = NULL; 1371 struct spdk_nvme_health_information_page *health_page = NULL; 1372 1373 if (spdk_nvme_cpl_is_error(cpl)) { 1374 nvme_health_info_cleanup(context, true); 1375 SPDK_ERRLOG("get log page failed\n"); 1376 return; 1377 } 1378 1379 if (ctrlr == NULL) { 1380 nvme_health_info_cleanup(context, true); 1381 SPDK_ERRLOG("ctrlr is NULL\n"); 1382 return; 1383 } else { 1384 trid = spdk_nvme_ctrlr_get_transport_id(ctrlr); 1385 cdata = spdk_nvme_ctrlr_get_data(ctrlr); 1386 health_page = &(context->health_page); 1387 } 1388 1389 w = spdk_jsonrpc_begin_result(request); 1390 1391 spdk_json_write_object_begin(w); 1392 snprintf(buf, sizeof(cdata->mn) + 1, "%s", cdata->mn); 1393 spdk_str_trim(buf); 1394 spdk_json_write_named_string(w, "model_number", buf); 1395 snprintf(buf, sizeof(cdata->sn) + 1, "%s", cdata->sn); 1396 spdk_str_trim(buf); 1397 spdk_json_write_named_string(w, "serial_number", buf); 1398 snprintf(buf, sizeof(cdata->fr) + 1, "%s", cdata->fr); 1399 spdk_str_trim(buf); 1400 spdk_json_write_named_string(w, "firmware_revision", buf); 1401 spdk_json_write_named_string(w, "traddr", trid->traddr); 1402 spdk_json_write_named_uint64(w, "temperature_celsius", health_page->temperature - 273); 1403 spdk_json_write_named_uint64(w, "available_spare_percentage", health_page->available_spare); 1404 spdk_json_write_named_uint64(w, "available_spare_threshold_percentage", 1405 health_page->available_spare_threshold); 1406 spdk_json_write_named_uint64(w, "percentage_used", health_page->percentage_used); 1407 spdk_json_write_named_uint128(w, "data_units_read", 1408 health_page->data_units_read[0], health_page->data_units_read[1]); 1409 spdk_json_write_named_uint128(w, "data_units_written", 1410 health_page->data_units_written[0], health_page->data_units_written[1]); 1411 spdk_json_write_named_uint128(w, "host_read_commands", 1412 health_page->host_read_commands[0], health_page->host_read_commands[1]); 1413 spdk_json_write_named_uint128(w, "host_write_commands", 1414 health_page->host_write_commands[0], health_page->host_write_commands[1]); 1415 spdk_json_write_named_uint128(w, "controller_busy_time", 1416 health_page->controller_busy_time[0], health_page->controller_busy_time[1]); 1417 spdk_json_write_named_uint128(w, "power_cycles", 1418 health_page->power_cycles[0], health_page->power_cycles[1]); 1419 spdk_json_write_named_uint128(w, "power_on_hours", 1420 health_page->power_on_hours[0], health_page->power_on_hours[1]); 1421 spdk_json_write_named_uint128(w, "unsafe_shutdowns", 1422 health_page->unsafe_shutdowns[0], health_page->unsafe_shutdowns[1]); 1423 spdk_json_write_named_uint128(w, "media_errors", 1424 health_page->media_errors[0], health_page->media_errors[1]); 1425 spdk_json_write_named_uint128(w, "num_err_log_entries", 1426 health_page->num_error_info_log_entries[0], health_page->num_error_info_log_entries[1]); 1427 spdk_json_write_named_uint64(w, "warning_temperature_time_minutes", health_page->warning_temp_time); 1428 spdk_json_write_named_uint64(w, "critical_composite_temperature_time_minutes", 1429 health_page->critical_temp_time); 1430 for (i = 0; i < 8; i++) { 1431 if (health_page->temp_sensor[i] != 0) { 1432 spdk_json_write_named_uint64(w, "temperature_sensor_celsius", health_page->temp_sensor[i] - 273); 1433 } 1434 } 1435 spdk_json_write_object_end(w); 1436 1437 spdk_jsonrpc_end_result(request, w); 1438 nvme_health_info_cleanup(context, false); 1439 } 1440 1441 static void 1442 get_health_log_page(struct spdk_nvme_health_info_context *context) 1443 { 1444 struct spdk_nvme_ctrlr *ctrlr = context->ctrlr; 1445 1446 if (spdk_nvme_ctrlr_cmd_get_log_page(ctrlr, SPDK_NVME_LOG_HEALTH_INFORMATION, 1447 SPDK_NVME_GLOBAL_NS_TAG, 1448 &(context->health_page), sizeof(context->health_page), 0, 1449 get_health_log_page_completion, context)) { 1450 nvme_health_info_cleanup(context, true); 1451 SPDK_ERRLOG("spdk_nvme_ctrlr_cmd_get_log_page() failed\n"); 1452 } 1453 } 1454 1455 static void 1456 get_temperature_threshold_feature_completion(void *cb_arg, const struct spdk_nvme_cpl *cpl) 1457 { 1458 struct spdk_nvme_health_info_context *context = cb_arg; 1459 1460 if (spdk_nvme_cpl_is_error(cpl)) { 1461 nvme_health_info_cleanup(context, true); 1462 SPDK_ERRLOG("feature SPDK_NVME_FEAT_TEMPERATURE_THRESHOLD failed in completion\n"); 1463 } else { 1464 get_health_log_page(context); 1465 } 1466 } 1467 1468 static int 1469 get_temperature_threshold_feature(struct spdk_nvme_health_info_context *context) 1470 { 1471 struct spdk_nvme_cmd cmd = {}; 1472 1473 cmd.opc = SPDK_NVME_OPC_GET_FEATURES; 1474 cmd.cdw10 = SPDK_NVME_FEAT_TEMPERATURE_THRESHOLD; 1475 1476 return spdk_nvme_ctrlr_cmd_admin_raw(context->ctrlr, &cmd, NULL, 0, 1477 get_temperature_threshold_feature_completion, context); 1478 } 1479 1480 static void 1481 get_controller_health_info(struct spdk_jsonrpc_request *request, struct spdk_nvme_ctrlr *ctrlr) 1482 { 1483 struct spdk_nvme_health_info_context *context; 1484 1485 context = calloc(1, sizeof(struct spdk_nvme_health_info_context)); 1486 if (!context) { 1487 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1488 "Memory allocation error."); 1489 return; 1490 } 1491 1492 context->request = request; 1493 context->ctrlr = ctrlr; 1494 1495 if (get_temperature_threshold_feature(context)) { 1496 nvme_health_info_cleanup(context, true); 1497 SPDK_ERRLOG("feature SPDK_NVME_FEAT_TEMPERATURE_THRESHOLD failed to submit\n"); 1498 } 1499 1500 return; 1501 } 1502 1503 static void 1504 rpc_bdev_nvme_get_controller_health_info(struct spdk_jsonrpc_request *request, 1505 const struct spdk_json_val *params) 1506 { 1507 struct rpc_get_controller_health_info req = {}; 1508 struct nvme_ctrlr *nvme_ctrlr = NULL; 1509 1510 if (!params) { 1511 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1512 "Missing device name"); 1513 1514 return; 1515 } 1516 if (spdk_json_decode_object(params, rpc_get_controller_health_info_decoders, 1517 SPDK_COUNTOF(rpc_get_controller_health_info_decoders), &req)) { 1518 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 1519 free_rpc_get_controller_health_info(&req); 1520 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1521 "Invalid parameters"); 1522 1523 return; 1524 } 1525 1526 nvme_ctrlr = nvme_ctrlr_get_by_name(req.name); 1527 1528 if (!nvme_ctrlr) { 1529 SPDK_ERRLOG("nvme ctrlr name '%s' does not exist\n", req.name); 1530 free_rpc_get_controller_health_info(&req); 1531 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1532 "Device not found"); 1533 return; 1534 } 1535 1536 get_controller_health_info(request, nvme_ctrlr->ctrlr); 1537 free_rpc_get_controller_health_info(&req); 1538 1539 return; 1540 } 1541 SPDK_RPC_REGISTER("bdev_nvme_get_controller_health_info", 1542 rpc_bdev_nvme_get_controller_health_info, SPDK_RPC_RUNTIME) 1543