xref: /spdk/module/bdev/nvme/bdev_nvme_rpc.c (revision 6f338d4bf3a8a91b7abe377a605a321ea2b05bf7)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (c) Intel Corporation. All rights reserved.
3  *   Copyright (c) 2019-2021 Mellanox Technologies LTD. All rights reserved.
4  *   Copyright (c) 2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
5  */
6 
7 #include "spdk/stdinc.h"
8 
9 #include "bdev_nvme.h"
10 
11 #include "spdk/config.h"
12 
13 #include "spdk/string.h"
14 #include "spdk/rpc.h"
15 #include "spdk/util.h"
16 #include "spdk/env.h"
17 #include "spdk/nvme.h"
18 #include "spdk/nvme_spec.h"
19 
20 #include "spdk/log.h"
21 #include "spdk/bdev_module.h"
22 
23 struct open_descriptors {
24 	void *desc;
25 	struct  spdk_bdev *bdev;
26 	TAILQ_ENTRY(open_descriptors) tqlst;
27 	struct spdk_thread *thread;
28 };
29 typedef TAILQ_HEAD(, open_descriptors) open_descriptors_t;
30 
31 static int
32 rpc_decode_action_on_timeout(const struct spdk_json_val *val, void *out)
33 {
34 	enum spdk_bdev_timeout_action *action = out;
35 
36 	if (spdk_json_strequal(val, "none") == true) {
37 		*action = SPDK_BDEV_NVME_TIMEOUT_ACTION_NONE;
38 	} else if (spdk_json_strequal(val, "abort") == true) {
39 		*action = SPDK_BDEV_NVME_TIMEOUT_ACTION_ABORT;
40 	} else if (spdk_json_strequal(val, "reset") == true) {
41 		*action = SPDK_BDEV_NVME_TIMEOUT_ACTION_RESET;
42 	} else {
43 		SPDK_NOTICELOG("Invalid parameter value: action_on_timeout\n");
44 		return -EINVAL;
45 	}
46 
47 	return 0;
48 }
49 
50 static const struct spdk_json_object_decoder rpc_bdev_nvme_options_decoders[] = {
51 	{"action_on_timeout", offsetof(struct spdk_bdev_nvme_opts, action_on_timeout), rpc_decode_action_on_timeout, true},
52 	{"timeout_us", offsetof(struct spdk_bdev_nvme_opts, timeout_us), spdk_json_decode_uint64, true},
53 	{"timeout_admin_us", offsetof(struct spdk_bdev_nvme_opts, timeout_admin_us), spdk_json_decode_uint64, true},
54 	{"keep_alive_timeout_ms", offsetof(struct spdk_bdev_nvme_opts, keep_alive_timeout_ms), spdk_json_decode_uint32, true},
55 	{"retry_count", offsetof(struct spdk_bdev_nvme_opts, transport_retry_count), spdk_json_decode_uint32, true},
56 	{"arbitration_burst", offsetof(struct spdk_bdev_nvme_opts, arbitration_burst), spdk_json_decode_uint32, true},
57 	{"low_priority_weight", offsetof(struct spdk_bdev_nvme_opts, low_priority_weight), spdk_json_decode_uint32, true},
58 	{"medium_priority_weight", offsetof(struct spdk_bdev_nvme_opts, medium_priority_weight), spdk_json_decode_uint32, true},
59 	{"high_priority_weight", offsetof(struct spdk_bdev_nvme_opts, high_priority_weight), spdk_json_decode_uint32, true},
60 	{"nvme_adminq_poll_period_us", offsetof(struct spdk_bdev_nvme_opts, nvme_adminq_poll_period_us), spdk_json_decode_uint64, true},
61 	{"nvme_ioq_poll_period_us", offsetof(struct spdk_bdev_nvme_opts, nvme_ioq_poll_period_us), spdk_json_decode_uint64, true},
62 	{"io_queue_requests", offsetof(struct spdk_bdev_nvme_opts, io_queue_requests), spdk_json_decode_uint32, true},
63 	{"delay_cmd_submit", offsetof(struct spdk_bdev_nvme_opts, delay_cmd_submit), spdk_json_decode_bool, true},
64 	{"transport_retry_count", offsetof(struct spdk_bdev_nvme_opts, transport_retry_count), spdk_json_decode_uint32, true},
65 	{"bdev_retry_count", offsetof(struct spdk_bdev_nvme_opts, bdev_retry_count), spdk_json_decode_int32, true},
66 	{"transport_ack_timeout", offsetof(struct spdk_bdev_nvme_opts, transport_ack_timeout), spdk_json_decode_uint8, true},
67 	{"ctrlr_loss_timeout_sec", offsetof(struct spdk_bdev_nvme_opts, ctrlr_loss_timeout_sec), spdk_json_decode_int32, true},
68 	{"reconnect_delay_sec", offsetof(struct spdk_bdev_nvme_opts, reconnect_delay_sec), spdk_json_decode_uint32, true},
69 	{"fast_io_fail_timeout_sec", offsetof(struct spdk_bdev_nvme_opts, fast_io_fail_timeout_sec), spdk_json_decode_uint32, true},
70 	{"disable_auto_failback", offsetof(struct spdk_bdev_nvme_opts, disable_auto_failback), spdk_json_decode_bool, true},
71 };
72 
73 static void
74 rpc_bdev_nvme_set_options(struct spdk_jsonrpc_request *request,
75 			  const struct spdk_json_val *params)
76 {
77 	struct spdk_bdev_nvme_opts opts;
78 	int rc;
79 
80 	bdev_nvme_get_opts(&opts);
81 	if (params && spdk_json_decode_object(params, rpc_bdev_nvme_options_decoders,
82 					      SPDK_COUNTOF(rpc_bdev_nvme_options_decoders),
83 					      &opts)) {
84 		SPDK_ERRLOG("spdk_json_decode_object failed\n");
85 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
86 						 "spdk_json_decode_object failed");
87 		return;
88 	}
89 
90 	rc = bdev_nvme_set_opts(&opts);
91 	if (rc) {
92 		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
93 		return;
94 	}
95 
96 	spdk_jsonrpc_send_bool_response(request, true);
97 
98 	return;
99 }
100 SPDK_RPC_REGISTER("bdev_nvme_set_options", rpc_bdev_nvme_set_options,
101 		  SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME)
102 
103 struct rpc_bdev_nvme_hotplug {
104 	bool enabled;
105 	uint64_t period_us;
106 };
107 
108 static const struct spdk_json_object_decoder rpc_bdev_nvme_hotplug_decoders[] = {
109 	{"enable", offsetof(struct rpc_bdev_nvme_hotplug, enabled), spdk_json_decode_bool, false},
110 	{"period_us", offsetof(struct rpc_bdev_nvme_hotplug, period_us), spdk_json_decode_uint64, true},
111 };
112 
113 static void
114 rpc_bdev_nvme_set_hotplug_done(void *ctx)
115 {
116 	struct spdk_jsonrpc_request *request = ctx;
117 
118 	spdk_jsonrpc_send_bool_response(request, true);
119 }
120 
121 static void
122 rpc_bdev_nvme_set_hotplug(struct spdk_jsonrpc_request *request,
123 			  const struct spdk_json_val *params)
124 {
125 	struct rpc_bdev_nvme_hotplug req = {false, 0};
126 	int rc;
127 
128 	if (spdk_json_decode_object(params, rpc_bdev_nvme_hotplug_decoders,
129 				    SPDK_COUNTOF(rpc_bdev_nvme_hotplug_decoders), &req)) {
130 		SPDK_ERRLOG("spdk_json_decode_object failed\n");
131 		rc = -EINVAL;
132 		goto invalid;
133 	}
134 
135 	rc = bdev_nvme_set_hotplug(req.enabled, req.period_us, rpc_bdev_nvme_set_hotplug_done,
136 				   request);
137 	if (rc) {
138 		goto invalid;
139 	}
140 
141 	return;
142 invalid:
143 	spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, spdk_strerror(-rc));
144 }
145 SPDK_RPC_REGISTER("bdev_nvme_set_hotplug", rpc_bdev_nvme_set_hotplug, SPDK_RPC_RUNTIME)
146 
147 struct rpc_bdev_nvme_attach_controller {
148 	char *name;
149 	char *trtype;
150 	char *adrfam;
151 	char *traddr;
152 	char *trsvcid;
153 	char *priority;
154 	char *subnqn;
155 	char *hostnqn;
156 	char *hostaddr;
157 	char *hostsvcid;
158 	char *multipath;
159 	struct nvme_ctrlr_opts bdev_opts;
160 	struct spdk_nvme_ctrlr_opts drv_opts;
161 };
162 
163 static void
164 free_rpc_bdev_nvme_attach_controller(struct rpc_bdev_nvme_attach_controller *req)
165 {
166 	free(req->name);
167 	free(req->trtype);
168 	free(req->adrfam);
169 	free(req->traddr);
170 	free(req->trsvcid);
171 	free(req->priority);
172 	free(req->subnqn);
173 	free(req->hostnqn);
174 	free(req->hostaddr);
175 	free(req->hostsvcid);
176 	free(req->multipath);
177 }
178 
179 static int
180 bdev_nvme_decode_reftag(const struct spdk_json_val *val, void *out)
181 {
182 	uint32_t *flag = out;
183 	bool reftag;
184 	int rc;
185 
186 	rc = spdk_json_decode_bool(val, &reftag);
187 	if (rc == 0 && reftag == true) {
188 		*flag |= SPDK_NVME_IO_FLAGS_PRCHK_REFTAG;
189 	}
190 
191 	return rc;
192 }
193 
194 static int
195 bdev_nvme_decode_guard(const struct spdk_json_val *val, void *out)
196 {
197 	uint32_t *flag = out;
198 	bool guard;
199 	int rc;
200 
201 	rc = spdk_json_decode_bool(val, &guard);
202 	if (rc == 0 && guard == true) {
203 		*flag |= SPDK_NVME_IO_FLAGS_PRCHK_GUARD;
204 	}
205 
206 	return rc;
207 }
208 
209 static const struct spdk_json_object_decoder rpc_bdev_nvme_attach_controller_decoders[] = {
210 	{"name", offsetof(struct rpc_bdev_nvme_attach_controller, name), spdk_json_decode_string},
211 	{"trtype", offsetof(struct rpc_bdev_nvme_attach_controller, trtype), spdk_json_decode_string},
212 	{"traddr", offsetof(struct rpc_bdev_nvme_attach_controller, traddr), spdk_json_decode_string},
213 
214 	{"adrfam", offsetof(struct rpc_bdev_nvme_attach_controller, adrfam), spdk_json_decode_string, true},
215 	{"trsvcid", offsetof(struct rpc_bdev_nvme_attach_controller, trsvcid), spdk_json_decode_string, true},
216 	{"priority", offsetof(struct rpc_bdev_nvme_attach_controller, priority), spdk_json_decode_string, true},
217 	{"subnqn", offsetof(struct rpc_bdev_nvme_attach_controller, subnqn), spdk_json_decode_string, true},
218 	{"hostnqn", offsetof(struct rpc_bdev_nvme_attach_controller, hostnqn), spdk_json_decode_string, true},
219 	{"hostaddr", offsetof(struct rpc_bdev_nvme_attach_controller, hostaddr), spdk_json_decode_string, true},
220 	{"hostsvcid", offsetof(struct rpc_bdev_nvme_attach_controller, hostsvcid), spdk_json_decode_string, true},
221 
222 	{"prchk_reftag", offsetof(struct rpc_bdev_nvme_attach_controller, bdev_opts.prchk_flags), bdev_nvme_decode_reftag, true},
223 	{"prchk_guard", offsetof(struct rpc_bdev_nvme_attach_controller, bdev_opts.prchk_flags), bdev_nvme_decode_guard, true},
224 	{"hdgst", offsetof(struct rpc_bdev_nvme_attach_controller, drv_opts.header_digest), spdk_json_decode_bool, true},
225 	{"ddgst", offsetof(struct rpc_bdev_nvme_attach_controller, drv_opts.data_digest), spdk_json_decode_bool, true},
226 	{"fabrics_connect_timeout_us", offsetof(struct rpc_bdev_nvme_attach_controller, drv_opts.fabrics_connect_timeout_us), spdk_json_decode_uint64, true},
227 	{"multipath", offsetof(struct rpc_bdev_nvme_attach_controller, multipath), spdk_json_decode_string, true},
228 	{"num_io_queues", offsetof(struct rpc_bdev_nvme_attach_controller, drv_opts.num_io_queues), spdk_json_decode_uint32, true},
229 	{"ctrlr_loss_timeout_sec", offsetof(struct rpc_bdev_nvme_attach_controller, bdev_opts.ctrlr_loss_timeout_sec), spdk_json_decode_int32, true},
230 	{"reconnect_delay_sec", offsetof(struct rpc_bdev_nvme_attach_controller, bdev_opts.reconnect_delay_sec), spdk_json_decode_uint32, true},
231 	{"fast_io_fail_timeout_sec", offsetof(struct rpc_bdev_nvme_attach_controller, bdev_opts.fast_io_fail_timeout_sec), spdk_json_decode_uint32, true},
232 };
233 
234 #define NVME_MAX_BDEVS_PER_RPC 128
235 
236 struct rpc_bdev_nvme_attach_controller_ctx {
237 	struct rpc_bdev_nvme_attach_controller req;
238 	uint32_t count;
239 	size_t bdev_count;
240 	const char *names[NVME_MAX_BDEVS_PER_RPC];
241 	struct spdk_jsonrpc_request *request;
242 };
243 
244 static void
245 rpc_bdev_nvme_attach_controller_examined(void *cb_ctx)
246 {
247 	struct rpc_bdev_nvme_attach_controller_ctx *ctx = cb_ctx;
248 	struct spdk_jsonrpc_request *request = ctx->request;
249 	struct spdk_json_write_ctx *w;
250 	size_t i;
251 
252 	w = spdk_jsonrpc_begin_result(request);
253 	spdk_json_write_array_begin(w);
254 	for (i = 0; i < ctx->bdev_count; i++) {
255 		spdk_json_write_string(w, ctx->names[i]);
256 	}
257 	spdk_json_write_array_end(w);
258 	spdk_jsonrpc_end_result(request, w);
259 
260 	free_rpc_bdev_nvme_attach_controller(&ctx->req);
261 	free(ctx);
262 }
263 
264 static void
265 rpc_bdev_nvme_attach_controller_done(void *cb_ctx, size_t bdev_count, int rc)
266 {
267 	struct rpc_bdev_nvme_attach_controller_ctx *ctx = cb_ctx;
268 	struct spdk_jsonrpc_request *request = ctx->request;
269 
270 	if (rc < 0) {
271 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
272 		free_rpc_bdev_nvme_attach_controller(&ctx->req);
273 		free(ctx);
274 		return;
275 	}
276 
277 	ctx->bdev_count = bdev_count;
278 	spdk_bdev_wait_for_examine(rpc_bdev_nvme_attach_controller_examined, ctx);
279 }
280 
281 static void
282 rpc_bdev_nvme_attach_controller(struct spdk_jsonrpc_request *request,
283 				const struct spdk_json_val *params)
284 {
285 	struct rpc_bdev_nvme_attach_controller_ctx *ctx;
286 	struct spdk_nvme_transport_id trid = {};
287 	const struct spdk_nvme_ctrlr_opts *drv_opts;
288 	const struct spdk_nvme_transport_id *ctrlr_trid;
289 	struct nvme_ctrlr *ctrlr = NULL;
290 	size_t len, maxlen;
291 	bool multipath = false;
292 	int rc;
293 
294 	ctx = calloc(1, sizeof(*ctx));
295 	if (!ctx) {
296 		spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM));
297 		return;
298 	}
299 
300 	spdk_nvme_ctrlr_get_default_ctrlr_opts(&ctx->req.drv_opts, sizeof(ctx->req.drv_opts));
301 	bdev_nvme_get_default_ctrlr_opts(&ctx->req.bdev_opts);
302 
303 	if (spdk_json_decode_object(params, rpc_bdev_nvme_attach_controller_decoders,
304 				    SPDK_COUNTOF(rpc_bdev_nvme_attach_controller_decoders),
305 				    &ctx->req)) {
306 		SPDK_ERRLOG("spdk_json_decode_object failed\n");
307 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
308 						 "spdk_json_decode_object failed");
309 		goto cleanup;
310 	}
311 
312 	/* Parse trstring */
313 	rc = spdk_nvme_transport_id_populate_trstring(&trid, ctx->req.trtype);
314 	if (rc < 0) {
315 		SPDK_ERRLOG("Failed to parse trtype: %s\n", ctx->req.trtype);
316 		spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse trtype: %s",
317 						     ctx->req.trtype);
318 		goto cleanup;
319 	}
320 
321 	/* Parse trtype */
322 	rc = spdk_nvme_transport_id_parse_trtype(&trid.trtype, ctx->req.trtype);
323 	assert(rc == 0);
324 
325 	/* Parse traddr */
326 	maxlen = sizeof(trid.traddr);
327 	len = strnlen(ctx->req.traddr, maxlen);
328 	if (len == maxlen) {
329 		spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "traddr too long: %s",
330 						     ctx->req.traddr);
331 		goto cleanup;
332 	}
333 	memcpy(trid.traddr, ctx->req.traddr, len + 1);
334 
335 	/* Parse adrfam */
336 	if (ctx->req.adrfam) {
337 		rc = spdk_nvme_transport_id_parse_adrfam(&trid.adrfam, ctx->req.adrfam);
338 		if (rc < 0) {
339 			SPDK_ERRLOG("Failed to parse adrfam: %s\n", ctx->req.adrfam);
340 			spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse adrfam: %s",
341 							     ctx->req.adrfam);
342 			goto cleanup;
343 		}
344 	}
345 
346 	/* Parse trsvcid */
347 	if (ctx->req.trsvcid) {
348 		maxlen = sizeof(trid.trsvcid);
349 		len = strnlen(ctx->req.trsvcid, maxlen);
350 		if (len == maxlen) {
351 			spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "trsvcid too long: %s",
352 							     ctx->req.trsvcid);
353 			goto cleanup;
354 		}
355 		memcpy(trid.trsvcid, ctx->req.trsvcid, len + 1);
356 	}
357 
358 	/* Parse priority for the NVMe-oF transport connection */
359 	if (ctx->req.priority) {
360 		trid.priority = spdk_strtol(ctx->req.priority, 10);
361 	}
362 
363 	/* Parse subnqn */
364 	if (ctx->req.subnqn) {
365 		maxlen = sizeof(trid.subnqn);
366 		len = strnlen(ctx->req.subnqn, maxlen);
367 		if (len == maxlen) {
368 			spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "subnqn too long: %s",
369 							     ctx->req.subnqn);
370 			goto cleanup;
371 		}
372 		memcpy(trid.subnqn, ctx->req.subnqn, len + 1);
373 	}
374 
375 	if (ctx->req.hostnqn) {
376 		snprintf(ctx->req.drv_opts.hostnqn, sizeof(ctx->req.drv_opts.hostnqn), "%s",
377 			 ctx->req.hostnqn);
378 	}
379 
380 	if (ctx->req.hostaddr) {
381 		maxlen = sizeof(ctx->req.drv_opts.src_addr);
382 		len = strnlen(ctx->req.hostaddr, maxlen);
383 		if (len == maxlen) {
384 			spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "hostaddr too long: %s",
385 							     ctx->req.hostaddr);
386 			goto cleanup;
387 		}
388 		snprintf(ctx->req.drv_opts.src_addr, maxlen, "%s", ctx->req.hostaddr);
389 	}
390 
391 	if (ctx->req.hostsvcid) {
392 		maxlen = sizeof(ctx->req.drv_opts.src_svcid);
393 		len = strnlen(ctx->req.hostsvcid, maxlen);
394 		if (len == maxlen) {
395 			spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "hostsvcid too long: %s",
396 							     ctx->req.hostsvcid);
397 			goto cleanup;
398 		}
399 		snprintf(ctx->req.drv_opts.src_svcid, maxlen, "%s", ctx->req.hostsvcid);
400 	}
401 
402 	ctrlr = nvme_ctrlr_get_by_name(ctx->req.name);
403 
404 	if (ctrlr) {
405 		if (ctx->req.multipath == NULL) {
406 			/* For now, this means add a failover path. This maintains backward compatibility
407 			 * with past behavior. In the future, this behavior will change to "disable". */
408 			SPDK_ERRLOG("The multipath parameter was not specified to bdev_nvme_attach_controller but "
409 				    "it was used to add a failover path. This behavior will default to rejecting "
410 				    "the request in the future. Specify the 'multipath' parameter to control the behavior\n");
411 			ctx->req.multipath = strdup("failover");
412 			if (ctx->req.multipath == NULL) {
413 				SPDK_ERRLOG("cannot allocate multipath failover string\n");
414 				goto cleanup;
415 			}
416 		}
417 
418 		/* This controller already exists. Check what the user wants to do. */
419 		if (strcasecmp(ctx->req.multipath, "disable") == 0) {
420 			/* The user does not want to do any form of multipathing. */
421 			spdk_jsonrpc_send_error_response_fmt(request, -EALREADY,
422 							     "A controller named %s already exists and multipath is disabled\n",
423 							     ctx->req.name);
424 			goto cleanup;
425 
426 		} else if (strcasecmp(ctx->req.multipath, "failover") != 0 &&
427 			   strcasecmp(ctx->req.multipath, "multipath") != 0) {
428 			/* Invalid multipath option */
429 			spdk_jsonrpc_send_error_response_fmt(request, -EINVAL,
430 							     "Invalid multipath parameter: %s\n",
431 							     ctx->req.multipath);
432 			goto cleanup;
433 		}
434 
435 		/* The user wants to add this as a failover path or add this to create multipath. */
436 		drv_opts = spdk_nvme_ctrlr_get_opts(ctrlr->ctrlr);
437 		ctrlr_trid = spdk_nvme_ctrlr_get_transport_id(ctrlr->ctrlr);
438 
439 		if (strncmp(trid.traddr, ctrlr_trid->traddr, sizeof(trid.traddr)) == 0 &&
440 		    strncmp(trid.trsvcid, ctrlr_trid->trsvcid, sizeof(trid.trsvcid)) == 0 &&
441 		    strncmp(ctx->req.drv_opts.src_addr, drv_opts->src_addr, sizeof(drv_opts->src_addr)) == 0 &&
442 		    strncmp(ctx->req.drv_opts.src_svcid, drv_opts->src_svcid, sizeof(drv_opts->src_svcid)) == 0) {
443 			/* Exactly same network path can't be added a second time */
444 			spdk_jsonrpc_send_error_response_fmt(request, -EALREADY,
445 							     "A controller named %s already exists with the specified network path\n",
446 							     ctx->req.name);
447 			goto cleanup;
448 		}
449 
450 		if (strncmp(trid.subnqn,
451 			    ctrlr_trid->subnqn,
452 			    SPDK_NVMF_NQN_MAX_LEN) != 0) {
453 			/* Different SUBNQN is not allowed when specifying the same controller name. */
454 			spdk_jsonrpc_send_error_response_fmt(request, -EINVAL,
455 							     "A controller named %s already exists, but uses a different subnqn (%s)\n",
456 							     ctx->req.name, ctrlr_trid->subnqn);
457 			goto cleanup;
458 		}
459 
460 		if (strncmp(ctx->req.drv_opts.hostnqn, drv_opts->hostnqn, SPDK_NVMF_NQN_MAX_LEN) != 0) {
461 			/* Different HOSTNQN is not allowed when specifying the same controller name. */
462 			spdk_jsonrpc_send_error_response_fmt(request, -EINVAL,
463 							     "A controller named %s already exists, but uses a different hostnqn (%s)\n",
464 							     ctx->req.name, drv_opts->hostnqn);
465 			goto cleanup;
466 		}
467 
468 		if (ctx->req.bdev_opts.prchk_flags) {
469 			spdk_jsonrpc_send_error_response_fmt(request, -EINVAL,
470 							     "A controller named %s already exists. To add a path, do not specify PI options.\n",
471 							     ctx->req.name);
472 			goto cleanup;
473 		}
474 
475 		ctx->req.bdev_opts.prchk_flags = ctrlr->opts.prchk_flags;
476 	}
477 
478 	if (ctx->req.multipath != NULL && strcasecmp(ctx->req.multipath, "multipath") == 0) {
479 		multipath = true;
480 	}
481 
482 	if (ctx->req.drv_opts.num_io_queues == 0 || ctx->req.drv_opts.num_io_queues > UINT16_MAX + 1) {
483 		spdk_jsonrpc_send_error_response_fmt(request, -EINVAL,
484 						     "num_io_queues out of bounds, min: %u max: %u\n",
485 						     1, UINT16_MAX + 1);
486 		goto cleanup;
487 	}
488 
489 	ctx->request = request;
490 	ctx->count = NVME_MAX_BDEVS_PER_RPC;
491 	/* Should already be zero due to the calloc(), but set explicitly for clarity. */
492 	ctx->req.bdev_opts.from_discovery_service = false;
493 	rc = bdev_nvme_create(&trid, ctx->req.name, ctx->names, ctx->count,
494 			      rpc_bdev_nvme_attach_controller_done, ctx, &ctx->req.drv_opts,
495 			      &ctx->req.bdev_opts, multipath);
496 	if (rc) {
497 		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
498 		goto cleanup;
499 	}
500 
501 	return;
502 
503 cleanup:
504 	free_rpc_bdev_nvme_attach_controller(&ctx->req);
505 	free(ctx);
506 }
507 SPDK_RPC_REGISTER("bdev_nvme_attach_controller", rpc_bdev_nvme_attach_controller,
508 		  SPDK_RPC_RUNTIME)
509 
510 static void
511 rpc_dump_nvme_bdev_controller_info(struct nvme_bdev_ctrlr *nbdev_ctrlr, void *ctx)
512 {
513 	struct spdk_json_write_ctx	*w = ctx;
514 	struct nvme_ctrlr		*nvme_ctrlr;
515 
516 	spdk_json_write_object_begin(w);
517 	spdk_json_write_named_string(w, "name", nbdev_ctrlr->name);
518 
519 	spdk_json_write_named_array_begin(w, "ctrlrs");
520 	TAILQ_FOREACH(nvme_ctrlr, &nbdev_ctrlr->ctrlrs, tailq) {
521 		nvme_ctrlr_info_json(w, nvme_ctrlr);
522 	}
523 	spdk_json_write_array_end(w);
524 	spdk_json_write_object_end(w);
525 }
526 
527 struct rpc_bdev_nvme_get_controllers {
528 	char *name;
529 };
530 
531 static void
532 free_rpc_bdev_nvme_get_controllers(struct rpc_bdev_nvme_get_controllers *r)
533 {
534 	free(r->name);
535 }
536 
537 static const struct spdk_json_object_decoder rpc_bdev_nvme_get_controllers_decoders[] = {
538 	{"name", offsetof(struct rpc_bdev_nvme_get_controllers, name), spdk_json_decode_string, true},
539 };
540 
541 static void
542 rpc_bdev_nvme_get_controllers(struct spdk_jsonrpc_request *request,
543 			      const struct spdk_json_val *params)
544 {
545 	struct rpc_bdev_nvme_get_controllers req = {};
546 	struct spdk_json_write_ctx *w;
547 	struct nvme_bdev_ctrlr *nbdev_ctrlr = NULL;
548 
549 	if (params && spdk_json_decode_object(params, rpc_bdev_nvme_get_controllers_decoders,
550 					      SPDK_COUNTOF(rpc_bdev_nvme_get_controllers_decoders),
551 					      &req)) {
552 		SPDK_ERRLOG("spdk_json_decode_object failed\n");
553 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
554 						 "spdk_json_decode_object failed");
555 		goto cleanup;
556 	}
557 
558 	if (req.name) {
559 		nbdev_ctrlr = nvme_bdev_ctrlr_get_by_name(req.name);
560 		if (nbdev_ctrlr == NULL) {
561 			SPDK_ERRLOG("ctrlr '%s' does not exist\n", req.name);
562 			spdk_jsonrpc_send_error_response_fmt(request, EINVAL, "Controller %s does not exist", req.name);
563 			goto cleanup;
564 		}
565 	}
566 
567 	w = spdk_jsonrpc_begin_result(request);
568 	spdk_json_write_array_begin(w);
569 
570 	if (nbdev_ctrlr != NULL) {
571 		rpc_dump_nvme_bdev_controller_info(nbdev_ctrlr, w);
572 	} else {
573 		nvme_bdev_ctrlr_for_each(rpc_dump_nvme_bdev_controller_info, w);
574 	}
575 
576 	spdk_json_write_array_end(w);
577 
578 	spdk_jsonrpc_end_result(request, w);
579 
580 cleanup:
581 	free_rpc_bdev_nvme_get_controllers(&req);
582 }
583 SPDK_RPC_REGISTER("bdev_nvme_get_controllers", rpc_bdev_nvme_get_controllers, SPDK_RPC_RUNTIME)
584 
585 struct rpc_bdev_nvme_detach_controller {
586 	char *name;
587 	char *trtype;
588 	char *adrfam;
589 	char *traddr;
590 	char *trsvcid;
591 	char *subnqn;
592 	char *hostaddr;
593 	char *hostsvcid;
594 };
595 
596 static void
597 free_rpc_bdev_nvme_detach_controller(struct rpc_bdev_nvme_detach_controller *req)
598 {
599 	free(req->name);
600 	free(req->trtype);
601 	free(req->adrfam);
602 	free(req->traddr);
603 	free(req->trsvcid);
604 	free(req->subnqn);
605 	free(req->hostaddr);
606 	free(req->hostsvcid);
607 }
608 
609 static const struct spdk_json_object_decoder rpc_bdev_nvme_detach_controller_decoders[] = {
610 	{"name", offsetof(struct rpc_bdev_nvme_detach_controller, name), spdk_json_decode_string},
611 	{"trtype", offsetof(struct rpc_bdev_nvme_detach_controller, trtype), spdk_json_decode_string, true},
612 	{"traddr", offsetof(struct rpc_bdev_nvme_detach_controller, traddr), spdk_json_decode_string, true},
613 	{"adrfam", offsetof(struct rpc_bdev_nvme_detach_controller, adrfam), spdk_json_decode_string, true},
614 	{"trsvcid", offsetof(struct rpc_bdev_nvme_detach_controller, trsvcid), spdk_json_decode_string, true},
615 	{"subnqn", offsetof(struct rpc_bdev_nvme_detach_controller, subnqn), spdk_json_decode_string, true},
616 	{"hostaddr", offsetof(struct rpc_bdev_nvme_detach_controller, hostaddr), spdk_json_decode_string, true},
617 	{"hostsvcid", offsetof(struct rpc_bdev_nvme_detach_controller, hostsvcid), spdk_json_decode_string, true},
618 };
619 
620 static void
621 rpc_bdev_nvme_detach_controller(struct spdk_jsonrpc_request *request,
622 				const struct spdk_json_val *params)
623 {
624 	struct rpc_bdev_nvme_detach_controller req = {NULL};
625 	struct nvme_path_id path = {};
626 	size_t len, maxlen;
627 	int rc = 0;
628 
629 	if (spdk_json_decode_object(params, rpc_bdev_nvme_detach_controller_decoders,
630 				    SPDK_COUNTOF(rpc_bdev_nvme_detach_controller_decoders),
631 				    &req)) {
632 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
633 						 "spdk_json_decode_object failed");
634 		goto cleanup;
635 	}
636 
637 	if (req.trtype != NULL) {
638 		rc = spdk_nvme_transport_id_populate_trstring(&path.trid, req.trtype);
639 		if (rc < 0) {
640 			SPDK_ERRLOG("Failed to parse trtype: %s\n", req.trtype);
641 			spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse trtype: %s",
642 							     req.trtype);
643 			goto cleanup;
644 		}
645 
646 		rc = spdk_nvme_transport_id_parse_trtype(&path.trid.trtype, req.trtype);
647 		if (rc < 0) {
648 			SPDK_ERRLOG("Failed to parse trtype: %s\n", req.trtype);
649 			spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse trtype: %s",
650 							     req.trtype);
651 			goto cleanup;
652 		}
653 	}
654 
655 	if (req.traddr != NULL) {
656 		maxlen = sizeof(path.trid.traddr);
657 		len = strnlen(req.traddr, maxlen);
658 		if (len == maxlen) {
659 			spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "traddr too long: %s",
660 							     req.traddr);
661 			goto cleanup;
662 		}
663 		memcpy(path.trid.traddr, req.traddr, len + 1);
664 	}
665 
666 	if (req.adrfam != NULL) {
667 		rc = spdk_nvme_transport_id_parse_adrfam(&path.trid.adrfam, req.adrfam);
668 		if (rc < 0) {
669 			SPDK_ERRLOG("Failed to parse adrfam: %s\n", req.adrfam);
670 			spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse adrfam: %s",
671 							     req.adrfam);
672 			goto cleanup;
673 		}
674 	}
675 
676 	if (req.trsvcid != NULL) {
677 		maxlen = sizeof(path.trid.trsvcid);
678 		len = strnlen(req.trsvcid, maxlen);
679 		if (len == maxlen) {
680 			spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "trsvcid too long: %s",
681 							     req.trsvcid);
682 			goto cleanup;
683 		}
684 		memcpy(path.trid.trsvcid, req.trsvcid, len + 1);
685 	}
686 
687 	/* Parse subnqn */
688 	if (req.subnqn != NULL) {
689 		maxlen = sizeof(path.trid.subnqn);
690 		len = strnlen(req.subnqn, maxlen);
691 		if (len == maxlen) {
692 			spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "subnqn too long: %s",
693 							     req.subnqn);
694 			goto cleanup;
695 		}
696 		memcpy(path.trid.subnqn, req.subnqn, len + 1);
697 	}
698 
699 	if (req.hostaddr) {
700 		maxlen = sizeof(path.hostid.hostaddr);
701 		len = strnlen(req.hostaddr, maxlen);
702 		if (len == maxlen) {
703 			spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "hostaddr too long: %s",
704 							     req.hostaddr);
705 			goto cleanup;
706 		}
707 		snprintf(path.hostid.hostaddr, maxlen, "%s", req.hostaddr);
708 	}
709 
710 	if (req.hostsvcid) {
711 		maxlen = sizeof(path.hostid.hostsvcid);
712 		len = strnlen(req.hostsvcid, maxlen);
713 		if (len == maxlen) {
714 			spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "hostsvcid too long: %s",
715 							     req.hostsvcid);
716 			goto cleanup;
717 		}
718 		snprintf(path.hostid.hostsvcid, maxlen, "%s", req.hostsvcid);
719 	}
720 
721 	rc = bdev_nvme_delete(req.name, &path);
722 
723 	if (rc != 0) {
724 		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
725 		goto cleanup;
726 	}
727 
728 	spdk_jsonrpc_send_bool_response(request, true);
729 
730 cleanup:
731 	free_rpc_bdev_nvme_detach_controller(&req);
732 }
733 SPDK_RPC_REGISTER("bdev_nvme_detach_controller", rpc_bdev_nvme_detach_controller,
734 		  SPDK_RPC_RUNTIME)
735 
736 struct rpc_apply_firmware {
737 	char *filename;
738 	char *bdev_name;
739 };
740 
741 static void
742 free_rpc_apply_firmware(struct rpc_apply_firmware *req)
743 {
744 	free(req->filename);
745 	free(req->bdev_name);
746 }
747 
748 static const struct spdk_json_object_decoder rpc_apply_firmware_decoders[] = {
749 	{"filename", offsetof(struct rpc_apply_firmware, filename), spdk_json_decode_string},
750 	{"bdev_name", offsetof(struct rpc_apply_firmware, bdev_name), spdk_json_decode_string},
751 };
752 
753 struct firmware_update_info {
754 	void				*fw_image;
755 	void				*p;
756 	unsigned int			size;
757 	unsigned int			size_remaining;
758 	unsigned int			offset;
759 	unsigned int			transfer;
760 
761 	void				*desc;
762 	struct spdk_io_channel		*ch;
763 	struct spdk_jsonrpc_request	*request;
764 	struct spdk_nvme_ctrlr		*ctrlr;
765 	open_descriptors_t		desc_head;
766 	struct rpc_apply_firmware	*req;
767 };
768 
769 static void
770 _apply_firmware_cleanup(void *ctx)
771 {
772 	struct spdk_bdev_desc *desc = ctx;
773 
774 	spdk_bdev_close(desc);
775 }
776 
777 static void
778 apply_firmware_cleanup(void *cb_arg)
779 {
780 	struct open_descriptors			*opt, *tmp;
781 	struct firmware_update_info *firm_ctx = cb_arg;
782 
783 	if (!firm_ctx) {
784 		return;
785 	}
786 
787 	if (firm_ctx->fw_image) {
788 		spdk_free(firm_ctx->fw_image);
789 	}
790 
791 	if (firm_ctx->req) {
792 		free_rpc_apply_firmware(firm_ctx->req);
793 		free(firm_ctx->req);
794 	}
795 
796 	if (firm_ctx->ch) {
797 		spdk_put_io_channel(firm_ctx->ch);
798 	}
799 
800 	TAILQ_FOREACH_SAFE(opt, &firm_ctx->desc_head, tqlst, tmp) {
801 		TAILQ_REMOVE(&firm_ctx->desc_head, opt, tqlst);
802 		/* Close the underlying bdev on its same opened thread. */
803 		if (opt->thread && opt->thread != spdk_get_thread()) {
804 			spdk_thread_send_msg(opt->thread, _apply_firmware_cleanup, opt->desc);
805 		} else {
806 			spdk_bdev_close(opt->desc);
807 		}
808 		free(opt);
809 	}
810 	free(firm_ctx);
811 }
812 
813 static void
814 apply_firmware_complete_reset(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
815 {
816 	struct spdk_json_write_ctx		*w;
817 	struct firmware_update_info *firm_ctx = cb_arg;
818 
819 	spdk_bdev_free_io(bdev_io);
820 
821 	if (!success) {
822 		spdk_jsonrpc_send_error_response(firm_ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
823 						 "firmware commit failed.");
824 		apply_firmware_cleanup(firm_ctx);
825 		return;
826 	}
827 
828 	if (spdk_nvme_ctrlr_reset(firm_ctx->ctrlr) != 0) {
829 		spdk_jsonrpc_send_error_response(firm_ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
830 						 "Controller reset failed.");
831 		apply_firmware_cleanup(firm_ctx);
832 		return;
833 	}
834 
835 	w = spdk_jsonrpc_begin_result(firm_ctx->request);
836 	spdk_json_write_string(w, "firmware commit succeeded. Controller reset in progress.");
837 	spdk_jsonrpc_end_result(firm_ctx->request, w);
838 	apply_firmware_cleanup(firm_ctx);
839 }
840 
841 static void
842 apply_firmware_complete(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
843 {
844 	struct spdk_nvme_cmd			cmd = {};
845 	struct spdk_nvme_fw_commit		fw_commit;
846 	int					slot = 0;
847 	int					rc;
848 	struct firmware_update_info *firm_ctx = cb_arg;
849 	enum spdk_nvme_fw_commit_action commit_action = SPDK_NVME_FW_COMMIT_REPLACE_AND_ENABLE_IMG;
850 
851 	spdk_bdev_free_io(bdev_io);
852 
853 	if (!success) {
854 		spdk_jsonrpc_send_error_response(firm_ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
855 						 "firmware download failed .");
856 		apply_firmware_cleanup(firm_ctx);
857 		return;
858 	}
859 
860 	firm_ctx->p += firm_ctx->transfer;
861 	firm_ctx->offset += firm_ctx->transfer;
862 	firm_ctx->size_remaining -= firm_ctx->transfer;
863 
864 	switch (firm_ctx->size_remaining) {
865 	case 0:
866 		/* firmware download completed. Commit firmware */
867 		memset(&fw_commit, 0, sizeof(struct spdk_nvme_fw_commit));
868 		fw_commit.fs = slot;
869 		fw_commit.ca = commit_action;
870 
871 		cmd.opc = SPDK_NVME_OPC_FIRMWARE_COMMIT;
872 		memcpy(&cmd.cdw10, &fw_commit, sizeof(uint32_t));
873 		rc = spdk_bdev_nvme_admin_passthru(firm_ctx->desc, firm_ctx->ch, &cmd, NULL, 0,
874 						   apply_firmware_complete_reset, firm_ctx);
875 		if (rc) {
876 			spdk_jsonrpc_send_error_response(firm_ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
877 							 "firmware commit failed.");
878 			apply_firmware_cleanup(firm_ctx);
879 			return;
880 		}
881 		break;
882 	default:
883 		firm_ctx->transfer = spdk_min(firm_ctx->size_remaining, 4096);
884 		cmd.opc = SPDK_NVME_OPC_FIRMWARE_IMAGE_DOWNLOAD;
885 
886 		cmd.cdw10 = spdk_nvme_bytes_to_numd(firm_ctx->transfer);
887 		cmd.cdw11 = firm_ctx->offset >> 2;
888 		rc = spdk_bdev_nvme_admin_passthru(firm_ctx->desc, firm_ctx->ch, &cmd, firm_ctx->p,
889 						   firm_ctx->transfer, apply_firmware_complete, firm_ctx);
890 		if (rc) {
891 			spdk_jsonrpc_send_error_response(firm_ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
892 							 "firmware download failed.");
893 			apply_firmware_cleanup(firm_ctx);
894 			return;
895 		}
896 		break;
897 	}
898 }
899 
900 static void
901 apply_firmware_open_cb(enum spdk_bdev_event_type type, struct spdk_bdev *bdev, void *event_ctx)
902 {
903 }
904 
905 static void
906 rpc_bdev_nvme_apply_firmware(struct spdk_jsonrpc_request *request,
907 			     const struct spdk_json_val *params)
908 {
909 	int					rc;
910 	int					fd = -1;
911 	struct stat				fw_stat;
912 	struct spdk_nvme_ctrlr			*ctrlr;
913 	char					msg[1024];
914 	struct spdk_bdev			*bdev;
915 	struct spdk_bdev			*bdev2;
916 	struct open_descriptors			*opt;
917 	struct spdk_bdev_desc			*desc;
918 	struct spdk_nvme_cmd			*cmd;
919 	struct firmware_update_info		*firm_ctx;
920 
921 	firm_ctx = calloc(1, sizeof(struct firmware_update_info));
922 	if (!firm_ctx) {
923 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
924 						 "Memory allocation error.");
925 		return;
926 	}
927 	firm_ctx->fw_image = NULL;
928 	TAILQ_INIT(&firm_ctx->desc_head);
929 	firm_ctx->request = request;
930 
931 	firm_ctx->req = calloc(1, sizeof(struct rpc_apply_firmware));
932 	if (!firm_ctx->req) {
933 		snprintf(msg, sizeof(msg), "Memory allocation error.");
934 		goto err;
935 	}
936 
937 	if (spdk_json_decode_object(params, rpc_apply_firmware_decoders,
938 				    SPDK_COUNTOF(rpc_apply_firmware_decoders), firm_ctx->req)) {
939 		snprintf(msg, sizeof(msg), "spdk_json_decode_object failed.");
940 		goto err;
941 	}
942 
943 	if ((bdev = spdk_bdev_get_by_name(firm_ctx->req->bdev_name)) == NULL) {
944 		snprintf(msg, sizeof(msg), "bdev %s were not found", firm_ctx->req->bdev_name);
945 		goto err;
946 	}
947 
948 	if ((ctrlr = bdev_nvme_get_ctrlr(bdev)) == NULL) {
949 		snprintf(msg, sizeof(msg), "Controller information for %s were not found.",
950 			 firm_ctx->req->bdev_name);
951 		goto err;
952 	}
953 	firm_ctx->ctrlr = ctrlr;
954 
955 	for (bdev2 = spdk_bdev_first(); bdev2; bdev2 = spdk_bdev_next(bdev2)) {
956 
957 		if (bdev_nvme_get_ctrlr(bdev2) != ctrlr) {
958 			continue;
959 		}
960 
961 		if (!(opt = malloc(sizeof(struct open_descriptors)))) {
962 			snprintf(msg, sizeof(msg), "Memory allocation error.");
963 			goto err;
964 		}
965 
966 		if (spdk_bdev_open_ext(spdk_bdev_get_name(bdev2), true, apply_firmware_open_cb, NULL, &desc) != 0) {
967 			snprintf(msg, sizeof(msg), "Device %s is in use.", firm_ctx->req->bdev_name);
968 			free(opt);
969 			goto err;
970 		}
971 
972 		/* Save the thread where the base device is opened */
973 		opt->thread = spdk_get_thread();
974 
975 		opt->desc = desc;
976 		opt->bdev = bdev;
977 		TAILQ_INSERT_TAIL(&firm_ctx->desc_head, opt, tqlst);
978 	}
979 
980 	/*
981 	 * find a descriptor associated with our bdev
982 	 */
983 	firm_ctx->desc = NULL;
984 	TAILQ_FOREACH(opt, &firm_ctx->desc_head, tqlst) {
985 		if (opt->bdev == bdev) {
986 			firm_ctx->desc = opt->desc;
987 			break;
988 		}
989 	}
990 
991 	if (!firm_ctx->desc) {
992 		snprintf(msg, sizeof(msg), "No descriptor were found.");
993 		goto err;
994 	}
995 
996 	firm_ctx->ch = spdk_bdev_get_io_channel(firm_ctx->desc);
997 	if (!firm_ctx->ch) {
998 		snprintf(msg, sizeof(msg), "No channels were found.");
999 		goto err;
1000 	}
1001 
1002 	fd = open(firm_ctx->req->filename, O_RDONLY);
1003 	if (fd < 0) {
1004 		snprintf(msg, sizeof(msg), "open file failed.");
1005 		goto err;
1006 	}
1007 
1008 	rc = fstat(fd, &fw_stat);
1009 	if (rc < 0) {
1010 		close(fd);
1011 		snprintf(msg, sizeof(msg), "fstat failed.");
1012 		goto err;
1013 	}
1014 
1015 	firm_ctx->size = fw_stat.st_size;
1016 	if (fw_stat.st_size % 4) {
1017 		close(fd);
1018 		snprintf(msg, sizeof(msg), "Firmware image size is not multiple of 4.");
1019 		goto err;
1020 	}
1021 
1022 	firm_ctx->fw_image = spdk_zmalloc(firm_ctx->size, 4096, NULL,
1023 					  SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA);
1024 	if (!firm_ctx->fw_image) {
1025 		close(fd);
1026 		snprintf(msg, sizeof(msg), "Memory allocation error.");
1027 		goto err;
1028 	}
1029 	firm_ctx->p = firm_ctx->fw_image;
1030 
1031 	if (read(fd, firm_ctx->p, firm_ctx->size) != ((ssize_t)(firm_ctx->size))) {
1032 		close(fd);
1033 		snprintf(msg, sizeof(msg), "Read firmware image failed!");
1034 		goto err;
1035 	}
1036 	close(fd);
1037 
1038 	firm_ctx->offset = 0;
1039 	firm_ctx->size_remaining = firm_ctx->size;
1040 	firm_ctx->transfer = spdk_min(firm_ctx->size_remaining, 4096);
1041 
1042 	cmd = malloc(sizeof(struct spdk_nvme_cmd));
1043 	if (!cmd) {
1044 		snprintf(msg, sizeof(msg), "Memory allocation error.");
1045 		goto err;
1046 	}
1047 	memset(cmd, 0, sizeof(struct spdk_nvme_cmd));
1048 	cmd->opc = SPDK_NVME_OPC_FIRMWARE_IMAGE_DOWNLOAD;
1049 
1050 	cmd->cdw10 = spdk_nvme_bytes_to_numd(firm_ctx->transfer);
1051 	cmd->cdw11 = firm_ctx->offset >> 2;
1052 
1053 	rc = spdk_bdev_nvme_admin_passthru(firm_ctx->desc, firm_ctx->ch, cmd, firm_ctx->p,
1054 					   firm_ctx->transfer, apply_firmware_complete, firm_ctx);
1055 	if (rc == 0) {
1056 		/* normal return here. */
1057 		return;
1058 	}
1059 
1060 	free(cmd);
1061 	snprintf(msg, sizeof(msg), "Read firmware image failed!");
1062 err:
1063 	spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, msg);
1064 	apply_firmware_cleanup(firm_ctx);
1065 }
1066 SPDK_RPC_REGISTER("bdev_nvme_apply_firmware", rpc_bdev_nvme_apply_firmware, SPDK_RPC_RUNTIME)
1067 
1068 struct rpc_bdev_nvme_transport_stat_ctx {
1069 	struct spdk_jsonrpc_request *request;
1070 	struct spdk_json_write_ctx *w;
1071 };
1072 
1073 static void
1074 rpc_bdev_nvme_rdma_stats(struct spdk_json_write_ctx *w,
1075 			 struct spdk_nvme_transport_poll_group_stat *stat)
1076 {
1077 	struct spdk_nvme_rdma_device_stat *device_stats;
1078 	uint32_t i;
1079 
1080 	spdk_json_write_named_array_begin(w, "devices");
1081 
1082 	for (i = 0; i < stat->rdma.num_devices; i++) {
1083 		device_stats = &stat->rdma.device_stats[i];
1084 		spdk_json_write_object_begin(w);
1085 		spdk_json_write_named_string(w, "dev_name", device_stats->name);
1086 		spdk_json_write_named_uint64(w, "polls", device_stats->polls);
1087 		spdk_json_write_named_uint64(w, "idle_polls", device_stats->idle_polls);
1088 		spdk_json_write_named_uint64(w, "completions", device_stats->completions);
1089 		spdk_json_write_named_uint64(w, "queued_requests", device_stats->queued_requests);
1090 		spdk_json_write_named_uint64(w, "total_send_wrs", device_stats->total_send_wrs);
1091 		spdk_json_write_named_uint64(w, "send_doorbell_updates", device_stats->send_doorbell_updates);
1092 		spdk_json_write_named_uint64(w, "total_recv_wrs", device_stats->total_recv_wrs);
1093 		spdk_json_write_named_uint64(w, "recv_doorbell_updates", device_stats->recv_doorbell_updates);
1094 		spdk_json_write_object_end(w);
1095 	}
1096 	spdk_json_write_array_end(w);
1097 }
1098 
1099 static void
1100 rpc_bdev_nvme_pcie_stats(struct spdk_json_write_ctx *w,
1101 			 struct spdk_nvme_transport_poll_group_stat *stat)
1102 {
1103 	spdk_json_write_named_uint64(w, "polls", stat->pcie.polls);
1104 	spdk_json_write_named_uint64(w, "idle_polls", stat->pcie.idle_polls);
1105 	spdk_json_write_named_uint64(w, "completions", stat->pcie.completions);
1106 	spdk_json_write_named_uint64(w, "cq_mmio_doorbell_updates", stat->pcie.cq_mmio_doorbell_updates);
1107 	spdk_json_write_named_uint64(w, "cq_shadow_doorbell_updates",
1108 				     stat->pcie.cq_shadow_doorbell_updates);
1109 	spdk_json_write_named_uint64(w, "queued_requests", stat->pcie.queued_requests);
1110 	spdk_json_write_named_uint64(w, "submitted_requests", stat->pcie.submitted_requests);
1111 	spdk_json_write_named_uint64(w, "sq_mmio_doorbell_updates", stat->pcie.sq_mmio_doorbell_updates);
1112 	spdk_json_write_named_uint64(w, "sq_shadow_doorbell_updates",
1113 				     stat->pcie.sq_shadow_doorbell_updates);
1114 }
1115 
1116 static void
1117 rpc_bdev_nvme_tcp_stats(struct spdk_json_write_ctx *w,
1118 			struct spdk_nvme_transport_poll_group_stat *stat)
1119 {
1120 	spdk_json_write_named_uint64(w, "polls", stat->tcp.polls);
1121 	spdk_json_write_named_uint64(w, "idle_polls", stat->tcp.idle_polls);
1122 	spdk_json_write_named_uint64(w, "socket_completions", stat->tcp.socket_completions);
1123 	spdk_json_write_named_uint64(w, "nvme_completions", stat->tcp.nvme_completions);
1124 	spdk_json_write_named_uint64(w, "queued_requests", stat->tcp.queued_requests);
1125 	spdk_json_write_named_uint64(w, "submitted_requests", stat->tcp.submitted_requests);
1126 }
1127 
1128 static void
1129 rpc_bdev_nvme_stats_per_channel(struct spdk_io_channel_iter *i)
1130 {
1131 	struct rpc_bdev_nvme_transport_stat_ctx *ctx;
1132 	struct spdk_io_channel *ch;
1133 	struct nvme_poll_group *group;
1134 	struct spdk_nvme_poll_group_stat *stat;
1135 	struct spdk_nvme_transport_poll_group_stat *tr_stat;
1136 	uint32_t j;
1137 	int rc;
1138 
1139 	ctx = spdk_io_channel_iter_get_ctx(i);
1140 	ch = spdk_io_channel_iter_get_channel(i);
1141 	group = spdk_io_channel_get_ctx(ch);
1142 
1143 	rc = spdk_nvme_poll_group_get_stats(group->group, &stat);
1144 	if (rc) {
1145 		spdk_for_each_channel_continue(i, rc);
1146 		return;
1147 	}
1148 
1149 	spdk_json_write_object_begin(ctx->w);
1150 	spdk_json_write_named_string(ctx->w, "thread", spdk_thread_get_name(spdk_get_thread()));
1151 	spdk_json_write_named_array_begin(ctx->w, "transports");
1152 
1153 	for (j = 0; j < stat->num_transports; j++) {
1154 		tr_stat = stat->transport_stat[j];
1155 		spdk_json_write_object_begin(ctx->w);
1156 		spdk_json_write_named_string(ctx->w, "trname", spdk_nvme_transport_id_trtype_str(tr_stat->trtype));
1157 
1158 		switch (stat->transport_stat[j]->trtype) {
1159 		case SPDK_NVME_TRANSPORT_RDMA:
1160 			rpc_bdev_nvme_rdma_stats(ctx->w, tr_stat);
1161 			break;
1162 		case SPDK_NVME_TRANSPORT_PCIE:
1163 			rpc_bdev_nvme_pcie_stats(ctx->w, tr_stat);
1164 			break;
1165 		case SPDK_NVME_TRANSPORT_TCP:
1166 			rpc_bdev_nvme_tcp_stats(ctx->w, tr_stat);
1167 			break;
1168 		default:
1169 			SPDK_WARNLOG("Can't handle trtype %d %s\n", tr_stat->trtype,
1170 				     spdk_nvme_transport_id_trtype_str(tr_stat->trtype));
1171 		}
1172 		spdk_json_write_object_end(ctx->w);
1173 	}
1174 	/* transports array */
1175 	spdk_json_write_array_end(ctx->w);
1176 	spdk_json_write_object_end(ctx->w);
1177 
1178 	spdk_nvme_poll_group_free_stats(group->group, stat);
1179 	spdk_for_each_channel_continue(i, 0);
1180 }
1181 
1182 static void
1183 rpc_bdev_nvme_stats_done(struct spdk_io_channel_iter *i, int status)
1184 {
1185 	struct rpc_bdev_nvme_transport_stat_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
1186 
1187 	spdk_json_write_array_end(ctx->w);
1188 	spdk_json_write_object_end(ctx->w);
1189 	spdk_jsonrpc_end_result(ctx->request, ctx->w);
1190 	free(ctx);
1191 }
1192 
1193 static void
1194 rpc_bdev_nvme_get_transport_statistics(struct spdk_jsonrpc_request *request,
1195 				       const struct spdk_json_val *params)
1196 {
1197 	struct rpc_bdev_nvme_transport_stat_ctx *ctx;
1198 
1199 	if (params) {
1200 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
1201 						 "'bdev_nvme_get_transport_statistics' requires no arguments");
1202 		return;
1203 	}
1204 
1205 	ctx = calloc(1, sizeof(*ctx));
1206 	if (!ctx) {
1207 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1208 						 "Memory allocation error");
1209 		return;
1210 	}
1211 	ctx->request = request;
1212 	ctx->w = spdk_jsonrpc_begin_result(ctx->request);
1213 	spdk_json_write_object_begin(ctx->w);
1214 	spdk_json_write_named_array_begin(ctx->w, "poll_groups");
1215 
1216 	spdk_for_each_channel(&g_nvme_bdev_ctrlrs,
1217 			      rpc_bdev_nvme_stats_per_channel,
1218 			      ctx,
1219 			      rpc_bdev_nvme_stats_done);
1220 }
1221 SPDK_RPC_REGISTER("bdev_nvme_get_transport_statistics", rpc_bdev_nvme_get_transport_statistics,
1222 		  SPDK_RPC_RUNTIME)
1223 
1224 struct rpc_bdev_nvme_reset_controller_req {
1225 	char *name;
1226 };
1227 
1228 static void
1229 free_rpc_bdev_nvme_reset_controller_req(struct rpc_bdev_nvme_reset_controller_req *r)
1230 {
1231 	free(r->name);
1232 }
1233 
1234 static const struct spdk_json_object_decoder rpc_bdev_nvme_reset_controller_req_decoders[] = {
1235 	{"name", offsetof(struct rpc_bdev_nvme_reset_controller_req, name), spdk_json_decode_string},
1236 };
1237 
1238 struct rpc_bdev_nvme_reset_controller_ctx {
1239 	struct spdk_jsonrpc_request *request;
1240 	bool success;
1241 	struct spdk_thread *orig_thread;
1242 };
1243 
1244 static void
1245 _rpc_bdev_nvme_reset_controller_cb(void *_ctx)
1246 {
1247 	struct rpc_bdev_nvme_reset_controller_ctx *ctx = _ctx;
1248 
1249 	spdk_jsonrpc_send_bool_response(ctx->request, ctx->success);
1250 
1251 	free(ctx);
1252 }
1253 
1254 static void
1255 rpc_bdev_nvme_reset_controller_cb(void *cb_arg, bool success)
1256 {
1257 	struct rpc_bdev_nvme_reset_controller_ctx *ctx = cb_arg;
1258 
1259 	ctx->success = success;
1260 
1261 	spdk_thread_send_msg(ctx->orig_thread, _rpc_bdev_nvme_reset_controller_cb, ctx);
1262 }
1263 
1264 static void
1265 rpc_bdev_nvme_reset_controller(struct spdk_jsonrpc_request *request,
1266 			       const struct spdk_json_val *params)
1267 {
1268 	struct rpc_bdev_nvme_reset_controller_req req = {NULL};
1269 	struct rpc_bdev_nvme_reset_controller_ctx *ctx;
1270 	struct nvme_ctrlr *nvme_ctrlr;
1271 	int rc;
1272 
1273 	ctx = calloc(1, sizeof(*ctx));
1274 	if (ctx == NULL) {
1275 		SPDK_ERRLOG("Memory allocation failed\n");
1276 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1277 						 "Memory allocation failed");
1278 		return;
1279 	}
1280 
1281 	if (spdk_json_decode_object(params, rpc_bdev_nvme_reset_controller_req_decoders,
1282 				    SPDK_COUNTOF(rpc_bdev_nvme_reset_controller_req_decoders),
1283 				    &req)) {
1284 		SPDK_ERRLOG("spdk_json_decode_object failed\n");
1285 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, spdk_strerror(EINVAL));
1286 		goto err;
1287 	}
1288 
1289 	nvme_ctrlr = nvme_ctrlr_get_by_name(req.name);
1290 	if (nvme_ctrlr == NULL) {
1291 		SPDK_ERRLOG("Failed at device lookup\n");
1292 		spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV));
1293 		goto err;
1294 	}
1295 
1296 	ctx->request = request;
1297 	ctx->orig_thread = spdk_get_thread();
1298 
1299 	rc = bdev_nvme_reset_rpc(nvme_ctrlr, rpc_bdev_nvme_reset_controller_cb, ctx);
1300 	if (rc != 0) {
1301 		SPDK_NOTICELOG("Failed at bdev_nvme_reset_rpc\n");
1302 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, spdk_strerror(-rc));
1303 		goto err;
1304 	}
1305 
1306 	free_rpc_bdev_nvme_reset_controller_req(&req);
1307 	return;
1308 
1309 err:
1310 	free_rpc_bdev_nvme_reset_controller_req(&req);
1311 	free(ctx);
1312 }
1313 SPDK_RPC_REGISTER("bdev_nvme_reset_controller", rpc_bdev_nvme_reset_controller, SPDK_RPC_RUNTIME)
1314 
1315 struct rpc_get_controller_health_info {
1316 	char *name;
1317 };
1318 
1319 struct spdk_nvme_health_info_context {
1320 	struct spdk_jsonrpc_request *request;
1321 	struct spdk_nvme_ctrlr *ctrlr;
1322 	struct spdk_nvme_health_information_page health_page;
1323 };
1324 
1325 static void
1326 free_rpc_get_controller_health_info(struct rpc_get_controller_health_info *r)
1327 {
1328 	free(r->name);
1329 }
1330 
1331 static const struct spdk_json_object_decoder rpc_get_controller_health_info_decoders[] = {
1332 	{"name", offsetof(struct rpc_get_controller_health_info, name), spdk_json_decode_string, true},
1333 };
1334 
1335 static void
1336 nvme_health_info_cleanup(struct spdk_nvme_health_info_context *context, bool response)
1337 {
1338 	if (response == true) {
1339 		spdk_jsonrpc_send_error_response(context->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1340 						 "Internal error.");
1341 	}
1342 
1343 	free(context);
1344 }
1345 
1346 static void
1347 get_health_log_page_completion(void *cb_arg, const struct spdk_nvme_cpl *cpl)
1348 {
1349 	int i;
1350 	char buf[128];
1351 	struct spdk_nvme_health_info_context *context = cb_arg;
1352 	struct spdk_jsonrpc_request *request = context->request;
1353 	struct spdk_json_write_ctx *w;
1354 	struct spdk_nvme_ctrlr *ctrlr = context->ctrlr;
1355 	const struct spdk_nvme_transport_id *trid = NULL;
1356 	const struct spdk_nvme_ctrlr_data *cdata = NULL;
1357 	struct spdk_nvme_health_information_page *health_page = NULL;
1358 
1359 	if (spdk_nvme_cpl_is_error(cpl)) {
1360 		nvme_health_info_cleanup(context, true);
1361 		SPDK_ERRLOG("get log page failed\n");
1362 		return;
1363 	}
1364 
1365 	if (ctrlr == NULL) {
1366 		nvme_health_info_cleanup(context, true);
1367 		SPDK_ERRLOG("ctrlr is NULL\n");
1368 		return;
1369 	} else {
1370 		trid = spdk_nvme_ctrlr_get_transport_id(ctrlr);
1371 		cdata = spdk_nvme_ctrlr_get_data(ctrlr);
1372 		health_page = &(context->health_page);
1373 	}
1374 
1375 	w = spdk_jsonrpc_begin_result(request);
1376 
1377 	spdk_json_write_object_begin(w);
1378 	snprintf(buf, sizeof(cdata->mn) + 1, "%s", cdata->mn);
1379 	spdk_str_trim(buf);
1380 	spdk_json_write_named_string(w, "model_number", buf);
1381 	snprintf(buf, sizeof(cdata->sn) + 1, "%s", cdata->sn);
1382 	spdk_str_trim(buf);
1383 	spdk_json_write_named_string(w, "serial_number", buf);
1384 	snprintf(buf, sizeof(cdata->fr) + 1, "%s", cdata->fr);
1385 	spdk_str_trim(buf);
1386 	spdk_json_write_named_string(w, "firmware_revision", buf);
1387 	spdk_json_write_named_string(w, "traddr", trid->traddr);
1388 	spdk_json_write_named_uint64(w, "temperature_celsius", health_page->temperature - 273);
1389 	spdk_json_write_named_uint64(w, "available_spare_percentage", health_page->available_spare);
1390 	spdk_json_write_named_uint64(w, "available_spare_threshold_percentage",
1391 				     health_page->available_spare_threshold);
1392 	spdk_json_write_named_uint64(w, "percentage_used", health_page->percentage_used);
1393 	spdk_json_write_named_uint128(w, "data_units_read",
1394 				      health_page->data_units_read[0], health_page->data_units_read[1]);
1395 	spdk_json_write_named_uint128(w, "data_units_written",
1396 				      health_page->data_units_written[0], health_page->data_units_written[1]);
1397 	spdk_json_write_named_uint128(w, "host_read_commands",
1398 				      health_page->host_read_commands[0], health_page->host_read_commands[1]);
1399 	spdk_json_write_named_uint128(w, "host_write_commands",
1400 				      health_page->host_write_commands[0], health_page->host_write_commands[1]);
1401 	spdk_json_write_named_uint128(w, "controller_busy_time",
1402 				      health_page->controller_busy_time[0], health_page->controller_busy_time[1]);
1403 	spdk_json_write_named_uint128(w, "power_cycles",
1404 				      health_page->power_cycles[0], health_page->power_cycles[1]);
1405 	spdk_json_write_named_uint128(w, "power_on_hours",
1406 				      health_page->power_on_hours[0], health_page->power_on_hours[1]);
1407 	spdk_json_write_named_uint128(w, "unsafe_shutdowns",
1408 				      health_page->unsafe_shutdowns[0], health_page->unsafe_shutdowns[1]);
1409 	spdk_json_write_named_uint128(w, "media_errors",
1410 				      health_page->media_errors[0], health_page->media_errors[1]);
1411 	spdk_json_write_named_uint128(w, "num_err_log_entries",
1412 				      health_page->num_error_info_log_entries[0], health_page->num_error_info_log_entries[1]);
1413 	spdk_json_write_named_uint64(w, "warning_temperature_time_minutes", health_page->warning_temp_time);
1414 	spdk_json_write_named_uint64(w, "critical_composite_temperature_time_minutes",
1415 				     health_page->critical_temp_time);
1416 	for (i = 0; i < 8; i++) {
1417 		if (health_page->temp_sensor[i] != 0) {
1418 			spdk_json_write_named_uint64(w, "temperature_sensor_celsius", health_page->temp_sensor[i] - 273);
1419 		}
1420 	}
1421 	spdk_json_write_object_end(w);
1422 
1423 	spdk_jsonrpc_end_result(request, w);
1424 	nvme_health_info_cleanup(context, false);
1425 }
1426 
1427 static void
1428 get_health_log_page(struct spdk_nvme_health_info_context *context)
1429 {
1430 	struct spdk_nvme_ctrlr *ctrlr = context->ctrlr;
1431 
1432 	if (spdk_nvme_ctrlr_cmd_get_log_page(ctrlr, SPDK_NVME_LOG_HEALTH_INFORMATION,
1433 					     SPDK_NVME_GLOBAL_NS_TAG,
1434 					     &(context->health_page), sizeof(context->health_page), 0,
1435 					     get_health_log_page_completion, context)) {
1436 		nvme_health_info_cleanup(context, true);
1437 		SPDK_ERRLOG("spdk_nvme_ctrlr_cmd_get_log_page() failed\n");
1438 	}
1439 }
1440 
1441 static void
1442 get_temperature_threshold_feature_completion(void *cb_arg, const struct spdk_nvme_cpl *cpl)
1443 {
1444 	struct spdk_nvme_health_info_context *context = cb_arg;
1445 
1446 	if (spdk_nvme_cpl_is_error(cpl)) {
1447 		nvme_health_info_cleanup(context, true);
1448 		SPDK_ERRLOG("feature SPDK_NVME_FEAT_TEMPERATURE_THRESHOLD failed in completion\n");
1449 	} else {
1450 		get_health_log_page(context);
1451 	}
1452 }
1453 
1454 static int
1455 get_temperature_threshold_feature(struct spdk_nvme_health_info_context *context)
1456 {
1457 	struct spdk_nvme_cmd cmd = {};
1458 
1459 	cmd.opc = SPDK_NVME_OPC_GET_FEATURES;
1460 	cmd.cdw10 = SPDK_NVME_FEAT_TEMPERATURE_THRESHOLD;
1461 
1462 	return spdk_nvme_ctrlr_cmd_admin_raw(context->ctrlr, &cmd, NULL, 0,
1463 					     get_temperature_threshold_feature_completion, context);
1464 }
1465 
1466 static void
1467 get_controller_health_info(struct spdk_jsonrpc_request *request, struct spdk_nvme_ctrlr *ctrlr)
1468 {
1469 	struct spdk_nvme_health_info_context *context;
1470 
1471 	context = calloc(1, sizeof(struct spdk_nvme_health_info_context));
1472 	if (!context) {
1473 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1474 						 "Memory allocation error.");
1475 		return;
1476 	}
1477 
1478 	context->request = request;
1479 	context->ctrlr = ctrlr;
1480 
1481 	if (get_temperature_threshold_feature(context)) {
1482 		nvme_health_info_cleanup(context, true);
1483 		SPDK_ERRLOG("feature SPDK_NVME_FEAT_TEMPERATURE_THRESHOLD failed to submit\n");
1484 	}
1485 
1486 	return;
1487 }
1488 
1489 static void
1490 rpc_bdev_nvme_get_controller_health_info(struct spdk_jsonrpc_request *request,
1491 		const struct spdk_json_val *params)
1492 {
1493 	struct rpc_get_controller_health_info req = {};
1494 	struct nvme_ctrlr *nvme_ctrlr = NULL;
1495 
1496 	if (!params) {
1497 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1498 						 "Missing device name");
1499 
1500 		return;
1501 	}
1502 	if (spdk_json_decode_object(params, rpc_get_controller_health_info_decoders,
1503 				    SPDK_COUNTOF(rpc_get_controller_health_info_decoders), &req)) {
1504 		SPDK_ERRLOG("spdk_json_decode_object failed\n");
1505 		free_rpc_get_controller_health_info(&req);
1506 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1507 						 "Invalid parameters");
1508 
1509 		return;
1510 	}
1511 
1512 	nvme_ctrlr = nvme_ctrlr_get_by_name(req.name);
1513 
1514 	if (!nvme_ctrlr) {
1515 		SPDK_ERRLOG("nvme ctrlr name '%s' does not exist\n", req.name);
1516 		free_rpc_get_controller_health_info(&req);
1517 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1518 						 "Device not found");
1519 		return;
1520 	}
1521 
1522 	get_controller_health_info(request, nvme_ctrlr->ctrlr);
1523 	free_rpc_get_controller_health_info(&req);
1524 
1525 	return;
1526 }
1527 SPDK_RPC_REGISTER("bdev_nvme_get_controller_health_info",
1528 		  rpc_bdev_nvme_get_controller_health_info, SPDK_RPC_RUNTIME)
1529 
1530 struct rpc_bdev_nvme_start_discovery {
1531 	char *name;
1532 	char *trtype;
1533 	char *adrfam;
1534 	char *traddr;
1535 	char *trsvcid;
1536 	char *hostnqn;
1537 	bool wait_for_attach;
1538 	uint64_t attach_timeout_ms;
1539 	struct spdk_nvme_ctrlr_opts opts;
1540 	struct nvme_ctrlr_opts bdev_opts;
1541 };
1542 
1543 static void
1544 free_rpc_bdev_nvme_start_discovery(struct rpc_bdev_nvme_start_discovery *req)
1545 {
1546 	free(req->name);
1547 	free(req->trtype);
1548 	free(req->adrfam);
1549 	free(req->traddr);
1550 	free(req->trsvcid);
1551 	free(req->hostnqn);
1552 }
1553 
1554 static const struct spdk_json_object_decoder rpc_bdev_nvme_start_discovery_decoders[] = {
1555 	{"name", offsetof(struct rpc_bdev_nvme_start_discovery, name), spdk_json_decode_string},
1556 	{"trtype", offsetof(struct rpc_bdev_nvme_start_discovery, trtype), spdk_json_decode_string},
1557 	{"traddr", offsetof(struct rpc_bdev_nvme_start_discovery, traddr), spdk_json_decode_string},
1558 	{"adrfam", offsetof(struct rpc_bdev_nvme_start_discovery, adrfam), spdk_json_decode_string, true},
1559 	{"trsvcid", offsetof(struct rpc_bdev_nvme_start_discovery, trsvcid), spdk_json_decode_string, true},
1560 	{"hostnqn", offsetof(struct rpc_bdev_nvme_start_discovery, hostnqn), spdk_json_decode_string, true},
1561 	{"wait_for_attach", offsetof(struct rpc_bdev_nvme_start_discovery, wait_for_attach), spdk_json_decode_bool, true},
1562 	{"attach_timeout_ms", offsetof(struct rpc_bdev_nvme_start_discovery, attach_timeout_ms), spdk_json_decode_uint64, true},
1563 	{"ctrlr_loss_timeout_sec", offsetof(struct rpc_bdev_nvme_start_discovery, bdev_opts.ctrlr_loss_timeout_sec), spdk_json_decode_int32, true},
1564 	{"reconnect_delay_sec", offsetof(struct rpc_bdev_nvme_start_discovery, bdev_opts.reconnect_delay_sec), spdk_json_decode_uint32, true},
1565 	{"fast_io_fail_timeout_sec", offsetof(struct rpc_bdev_nvme_start_discovery, bdev_opts.fast_io_fail_timeout_sec), spdk_json_decode_uint32, true},
1566 };
1567 
1568 struct rpc_bdev_nvme_start_discovery_ctx {
1569 	struct rpc_bdev_nvme_start_discovery req;
1570 	struct spdk_jsonrpc_request *request;
1571 };
1572 
1573 static void
1574 rpc_bdev_nvme_start_discovery_done(void *ctx, int status)
1575 {
1576 	struct spdk_jsonrpc_request *request = ctx;
1577 
1578 	if (status != 0) {
1579 		spdk_jsonrpc_send_error_response(request, status, spdk_strerror(-status));
1580 	} else {
1581 		spdk_jsonrpc_send_bool_response(request, true);
1582 	}
1583 }
1584 
1585 static void
1586 rpc_bdev_nvme_start_discovery(struct spdk_jsonrpc_request *request,
1587 			      const struct spdk_json_val *params)
1588 {
1589 	struct rpc_bdev_nvme_start_discovery_ctx *ctx;
1590 	struct spdk_nvme_transport_id trid = {};
1591 	size_t len, maxlen;
1592 	int rc;
1593 	spdk_bdev_nvme_start_discovery_fn cb_fn;
1594 	void *cb_ctx;
1595 
1596 	ctx = calloc(1, sizeof(*ctx));
1597 	if (!ctx) {
1598 		spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM));
1599 		return;
1600 	}
1601 
1602 	spdk_nvme_ctrlr_get_default_ctrlr_opts(&ctx->req.opts, sizeof(ctx->req.opts));
1603 
1604 	if (spdk_json_decode_object(params, rpc_bdev_nvme_start_discovery_decoders,
1605 				    SPDK_COUNTOF(rpc_bdev_nvme_start_discovery_decoders),
1606 				    &ctx->req)) {
1607 		SPDK_ERRLOG("spdk_json_decode_object failed\n");
1608 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1609 						 "spdk_json_decode_object failed");
1610 		goto cleanup;
1611 	}
1612 
1613 	/* Parse trstring */
1614 	rc = spdk_nvme_transport_id_populate_trstring(&trid, ctx->req.trtype);
1615 	if (rc < 0) {
1616 		SPDK_ERRLOG("Failed to parse trtype: %s\n", ctx->req.trtype);
1617 		spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse trtype: %s",
1618 						     ctx->req.trtype);
1619 		goto cleanup;
1620 	}
1621 
1622 	/* Parse trtype */
1623 	rc = spdk_nvme_transport_id_parse_trtype(&trid.trtype, ctx->req.trtype);
1624 	assert(rc == 0);
1625 
1626 	/* Parse traddr */
1627 	maxlen = sizeof(trid.traddr);
1628 	len = strnlen(ctx->req.traddr, maxlen);
1629 	if (len == maxlen) {
1630 		spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "traddr too long: %s",
1631 						     ctx->req.traddr);
1632 		goto cleanup;
1633 	}
1634 	memcpy(trid.traddr, ctx->req.traddr, len + 1);
1635 
1636 	/* Parse adrfam */
1637 	if (ctx->req.adrfam) {
1638 		rc = spdk_nvme_transport_id_parse_adrfam(&trid.adrfam, ctx->req.adrfam);
1639 		if (rc < 0) {
1640 			SPDK_ERRLOG("Failed to parse adrfam: %s\n", ctx->req.adrfam);
1641 			spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse adrfam: %s",
1642 							     ctx->req.adrfam);
1643 			goto cleanup;
1644 		}
1645 	}
1646 
1647 	/* Parse trsvcid */
1648 	if (ctx->req.trsvcid) {
1649 		maxlen = sizeof(trid.trsvcid);
1650 		len = strnlen(ctx->req.trsvcid, maxlen);
1651 		if (len == maxlen) {
1652 			spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "trsvcid too long: %s",
1653 							     ctx->req.trsvcid);
1654 			goto cleanup;
1655 		}
1656 		memcpy(trid.trsvcid, ctx->req.trsvcid, len + 1);
1657 	}
1658 
1659 	if (ctx->req.hostnqn) {
1660 		snprintf(ctx->req.opts.hostnqn, sizeof(ctx->req.opts.hostnqn), "%s",
1661 			 ctx->req.hostnqn);
1662 	}
1663 
1664 	if (ctx->req.attach_timeout_ms != 0) {
1665 		ctx->req.wait_for_attach = true;
1666 	}
1667 
1668 	ctx->request = request;
1669 	cb_fn = ctx->req.wait_for_attach ? rpc_bdev_nvme_start_discovery_done : NULL;
1670 	cb_ctx = ctx->req.wait_for_attach ? request : NULL;
1671 	rc = bdev_nvme_start_discovery(&trid, ctx->req.name, &ctx->req.opts, &ctx->req.bdev_opts,
1672 				       ctx->req.attach_timeout_ms, cb_fn, cb_ctx);
1673 	if (rc) {
1674 		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
1675 	} else if (!ctx->req.wait_for_attach) {
1676 		rpc_bdev_nvme_start_discovery_done(request, 0);
1677 	}
1678 
1679 cleanup:
1680 	free_rpc_bdev_nvme_start_discovery(&ctx->req);
1681 	free(ctx);
1682 }
1683 SPDK_RPC_REGISTER("bdev_nvme_start_discovery", rpc_bdev_nvme_start_discovery,
1684 		  SPDK_RPC_RUNTIME)
1685 
1686 struct rpc_bdev_nvme_stop_discovery {
1687 	char *name;
1688 };
1689 
1690 static const struct spdk_json_object_decoder rpc_bdev_nvme_stop_discovery_decoders[] = {
1691 	{"name", offsetof(struct rpc_bdev_nvme_stop_discovery, name), spdk_json_decode_string},
1692 };
1693 
1694 struct rpc_bdev_nvme_stop_discovery_ctx {
1695 	struct rpc_bdev_nvme_stop_discovery req;
1696 	struct spdk_jsonrpc_request *request;
1697 };
1698 
1699 static void
1700 rpc_bdev_nvme_stop_discovery_done(void *cb_ctx)
1701 {
1702 	struct rpc_bdev_nvme_stop_discovery_ctx *ctx = cb_ctx;
1703 
1704 	spdk_jsonrpc_send_bool_response(ctx->request, true);
1705 	free(ctx->req.name);
1706 	free(ctx);
1707 }
1708 
1709 static void
1710 rpc_bdev_nvme_stop_discovery(struct spdk_jsonrpc_request *request,
1711 			     const struct spdk_json_val *params)
1712 {
1713 	struct rpc_bdev_nvme_stop_discovery_ctx *ctx;
1714 	int rc;
1715 
1716 	ctx = calloc(1, sizeof(*ctx));
1717 	if (!ctx) {
1718 		spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM));
1719 		return;
1720 	}
1721 
1722 	if (spdk_json_decode_object(params, rpc_bdev_nvme_stop_discovery_decoders,
1723 				    SPDK_COUNTOF(rpc_bdev_nvme_stop_discovery_decoders),
1724 				    &ctx->req)) {
1725 		SPDK_ERRLOG("spdk_json_decode_object failed\n");
1726 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1727 						 "spdk_json_decode_object failed");
1728 		goto cleanup;
1729 	}
1730 
1731 	ctx->request = request;
1732 	rc = bdev_nvme_stop_discovery(ctx->req.name, rpc_bdev_nvme_stop_discovery_done, ctx);
1733 	if (rc) {
1734 		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
1735 		goto cleanup;
1736 	}
1737 
1738 	return;
1739 
1740 cleanup:
1741 	free(ctx->req.name);
1742 	free(ctx);
1743 }
1744 SPDK_RPC_REGISTER("bdev_nvme_stop_discovery", rpc_bdev_nvme_stop_discovery,
1745 		  SPDK_RPC_RUNTIME)
1746 
1747 static void
1748 rpc_bdev_nvme_get_discovery_info(struct spdk_jsonrpc_request *request,
1749 				 const struct spdk_json_val *params)
1750 {
1751 	struct spdk_json_write_ctx *w;
1752 
1753 	w = spdk_jsonrpc_begin_result(request);
1754 	bdev_nvme_get_discovery_info(w);
1755 	spdk_jsonrpc_end_result(request, w);
1756 }
1757 SPDK_RPC_REGISTER("bdev_nvme_get_discovery_info", rpc_bdev_nvme_get_discovery_info,
1758 		  SPDK_RPC_RUNTIME)
1759 
1760 enum error_injection_cmd_type {
1761 	NVME_ADMIN_CMD = 1,
1762 	NVME_IO_CMD,
1763 };
1764 
1765 struct rpc_add_error_injection {
1766 	char *name;
1767 	enum error_injection_cmd_type cmd_type;
1768 	uint8_t opc;
1769 	bool do_not_submit;
1770 	uint64_t timeout_in_us;
1771 	uint32_t err_count;
1772 	uint8_t sct;
1773 	uint8_t sc;
1774 };
1775 
1776 static void
1777 free_rpc_add_error_injection(struct rpc_add_error_injection *req)
1778 {
1779 	free(req->name);
1780 }
1781 
1782 static int
1783 rpc_error_injection_decode_cmd_type(const struct spdk_json_val *val, void *out)
1784 {
1785 	int *cmd_type = out;
1786 
1787 	if (spdk_json_strequal(val, "admin")) {
1788 		*cmd_type = NVME_ADMIN_CMD;
1789 	} else if (spdk_json_strequal(val, "io")) {
1790 		*cmd_type = NVME_IO_CMD;
1791 	} else {
1792 		SPDK_ERRLOG("Invalid parameter value: cmd_type\n");
1793 		return -EINVAL;
1794 	}
1795 
1796 	return 0;
1797 }
1798 
1799 static const struct spdk_json_object_decoder rpc_add_error_injection_decoders[] = {
1800 	{ "name", offsetof(struct rpc_add_error_injection, name), spdk_json_decode_string },
1801 	{ "cmd_type", offsetof(struct rpc_add_error_injection, cmd_type), rpc_error_injection_decode_cmd_type },
1802 	{ "opc", offsetof(struct rpc_add_error_injection, opc), spdk_json_decode_uint8 },
1803 	{ "do_not_submit", offsetof(struct rpc_add_error_injection, do_not_submit), spdk_json_decode_bool, true },
1804 	{ "timeout_in_us", offsetof(struct rpc_add_error_injection, timeout_in_us), spdk_json_decode_uint64, true },
1805 	{ "err_count", offsetof(struct rpc_add_error_injection, err_count), spdk_json_decode_uint32, true },
1806 	{ "sct", offsetof(struct rpc_add_error_injection, sct), spdk_json_decode_uint8, true},
1807 	{ "sc", offsetof(struct rpc_add_error_injection, sc), spdk_json_decode_uint8, true},
1808 };
1809 
1810 struct rpc_add_error_injection_ctx {
1811 	struct spdk_jsonrpc_request *request;
1812 	struct rpc_add_error_injection rpc;
1813 };
1814 
1815 static void
1816 rpc_add_error_injection_done(struct spdk_io_channel_iter *i, int status)
1817 {
1818 	struct rpc_add_error_injection_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
1819 
1820 	if (status) {
1821 		spdk_jsonrpc_send_error_response(ctx->request, status,
1822 						 "Failed to add the error injection.");
1823 	} else {
1824 		spdk_jsonrpc_send_bool_response(ctx->request, true);
1825 	}
1826 
1827 	free_rpc_add_error_injection(&ctx->rpc);
1828 	free(ctx);
1829 }
1830 
1831 static void
1832 rpc_add_error_injection_per_channel(struct spdk_io_channel_iter *i)
1833 {
1834 	struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
1835 	struct rpc_add_error_injection_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
1836 	struct nvme_ctrlr_channel *ctrlr_ch = spdk_io_channel_get_ctx(ch);
1837 	struct spdk_nvme_qpair *qpair = ctrlr_ch->qpair->qpair;
1838 	struct spdk_nvme_ctrlr *ctrlr = ctrlr_ch->qpair->ctrlr->ctrlr;
1839 	int rc = 0;
1840 
1841 	if (qpair != NULL) {
1842 		rc = spdk_nvme_qpair_add_cmd_error_injection(ctrlr, qpair, ctx->rpc.opc,
1843 				ctx->rpc.do_not_submit, ctx->rpc.timeout_in_us, ctx->rpc.err_count,
1844 				ctx->rpc.sct, ctx->rpc.sc);
1845 	}
1846 
1847 	spdk_for_each_channel_continue(i, rc);
1848 }
1849 
1850 static void
1851 rpc_bdev_nvme_add_error_injection(
1852 	struct spdk_jsonrpc_request *request,
1853 	const struct spdk_json_val *params)
1854 {
1855 	struct rpc_add_error_injection_ctx *ctx;
1856 	struct nvme_ctrlr *nvme_ctrlr;
1857 	int rc;
1858 
1859 	ctx = calloc(1, sizeof(*ctx));
1860 	if (!ctx) {
1861 		spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM));
1862 		return;
1863 	}
1864 	ctx->rpc.err_count = 1;
1865 	ctx->request = request;
1866 
1867 	if (spdk_json_decode_object(params,
1868 				    rpc_add_error_injection_decoders,
1869 				    SPDK_COUNTOF(rpc_add_error_injection_decoders),
1870 				    &ctx->rpc)) {
1871 		spdk_jsonrpc_send_error_response(request, -EINVAL,
1872 						 "Failed to parse the request");
1873 		goto cleanup;
1874 	}
1875 
1876 	nvme_ctrlr = nvme_ctrlr_get_by_name(ctx->rpc.name);
1877 	if (nvme_ctrlr == NULL) {
1878 		SPDK_ERRLOG("No controller with specified name was found.\n");
1879 		spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV));
1880 		goto cleanup;
1881 	}
1882 
1883 	if (ctx->rpc.cmd_type == NVME_IO_CMD) {
1884 		spdk_for_each_channel(nvme_ctrlr,
1885 				      rpc_add_error_injection_per_channel,
1886 				      ctx,
1887 				      rpc_add_error_injection_done);
1888 
1889 		return;
1890 	} else {
1891 		rc = spdk_nvme_qpair_add_cmd_error_injection(nvme_ctrlr->ctrlr, NULL, ctx->rpc.opc,
1892 				ctx->rpc.do_not_submit, ctx->rpc.timeout_in_us, ctx->rpc.err_count,
1893 				ctx->rpc.sct, ctx->rpc.sc);
1894 		if (rc) {
1895 			spdk_jsonrpc_send_error_response(request, -rc,
1896 							 "Failed to add the error injection");
1897 		} else {
1898 			spdk_jsonrpc_send_bool_response(ctx->request, true);
1899 		}
1900 	}
1901 
1902 cleanup:
1903 	free_rpc_add_error_injection(&ctx->rpc);
1904 	free(ctx);
1905 }
1906 SPDK_RPC_REGISTER("bdev_nvme_add_error_injection", rpc_bdev_nvme_add_error_injection,
1907 		  SPDK_RPC_RUNTIME)
1908 
1909 struct rpc_remove_error_injection {
1910 	char *name;
1911 	enum error_injection_cmd_type cmd_type;
1912 	uint8_t opc;
1913 };
1914 
1915 static void
1916 free_rpc_remove_error_injection(struct rpc_remove_error_injection *req)
1917 {
1918 	free(req->name);
1919 }
1920 
1921 static const struct spdk_json_object_decoder rpc_remove_error_injection_decoders[] = {
1922 	{ "name", offsetof(struct rpc_remove_error_injection, name), spdk_json_decode_string },
1923 	{ "cmd_type", offsetof(struct rpc_remove_error_injection, cmd_type), rpc_error_injection_decode_cmd_type },
1924 	{ "opc", offsetof(struct rpc_remove_error_injection, opc), spdk_json_decode_uint8 },
1925 };
1926 
1927 struct rpc_remove_error_injection_ctx {
1928 	struct spdk_jsonrpc_request *request;
1929 	struct rpc_remove_error_injection rpc;
1930 };
1931 
1932 static void
1933 rpc_remove_error_injection_done(struct spdk_io_channel_iter *i, int status)
1934 {
1935 	struct rpc_remove_error_injection_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
1936 
1937 	if (status) {
1938 		spdk_jsonrpc_send_error_response(ctx->request, status,
1939 						 "Failed to remove the error injection.");
1940 	} else {
1941 		spdk_jsonrpc_send_bool_response(ctx->request, true);
1942 	}
1943 
1944 	free_rpc_remove_error_injection(&ctx->rpc);
1945 	free(ctx);
1946 }
1947 
1948 static void
1949 rpc_remove_error_injection_per_channel(struct spdk_io_channel_iter *i)
1950 {
1951 	struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
1952 	struct rpc_remove_error_injection_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
1953 	struct nvme_ctrlr_channel *ctrlr_ch = spdk_io_channel_get_ctx(ch);
1954 	struct spdk_nvme_qpair *qpair = ctrlr_ch->qpair->qpair;
1955 	struct spdk_nvme_ctrlr *ctrlr = ctrlr_ch->qpair->ctrlr->ctrlr;
1956 
1957 	if (qpair != NULL) {
1958 		spdk_nvme_qpair_remove_cmd_error_injection(ctrlr, qpair, ctx->rpc.opc);
1959 	}
1960 
1961 	spdk_for_each_channel_continue(i, 0);
1962 }
1963 
1964 static void
1965 rpc_bdev_nvme_remove_error_injection(struct spdk_jsonrpc_request *request,
1966 				     const struct spdk_json_val *params)
1967 {
1968 	struct rpc_remove_error_injection_ctx *ctx;
1969 	struct nvme_ctrlr *nvme_ctrlr;
1970 
1971 	ctx = calloc(1, sizeof(*ctx));
1972 	if (!ctx) {
1973 		spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM));
1974 		return;
1975 	}
1976 	ctx->request = request;
1977 
1978 	if (spdk_json_decode_object(params,
1979 				    rpc_remove_error_injection_decoders,
1980 				    SPDK_COUNTOF(rpc_remove_error_injection_decoders),
1981 				    &ctx->rpc)) {
1982 		spdk_jsonrpc_send_error_response(request, -EINVAL,
1983 						 "Failed to parse the request");
1984 		goto cleanup;
1985 	}
1986 
1987 	nvme_ctrlr = nvme_ctrlr_get_by_name(ctx->rpc.name);
1988 	if (nvme_ctrlr == NULL) {
1989 		SPDK_ERRLOG("No controller with specified name was found.\n");
1990 		spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV));
1991 		goto cleanup;
1992 	}
1993 
1994 	if (ctx->rpc.cmd_type == NVME_IO_CMD) {
1995 		spdk_for_each_channel(nvme_ctrlr,
1996 				      rpc_remove_error_injection_per_channel,
1997 				      ctx,
1998 				      rpc_remove_error_injection_done);
1999 		return;
2000 	} else {
2001 		spdk_nvme_qpair_remove_cmd_error_injection(nvme_ctrlr->ctrlr, NULL, ctx->rpc.opc);
2002 		spdk_jsonrpc_send_bool_response(ctx->request, true);
2003 	}
2004 
2005 cleanup:
2006 	free_rpc_remove_error_injection(&ctx->rpc);
2007 	free(ctx);
2008 }
2009 SPDK_RPC_REGISTER("bdev_nvme_remove_error_injection", rpc_bdev_nvme_remove_error_injection,
2010 		  SPDK_RPC_RUNTIME)
2011 
2012 struct rpc_get_io_paths {
2013 	char *name;
2014 };
2015 
2016 static void
2017 free_rpc_get_io_paths(struct rpc_get_io_paths *r)
2018 {
2019 	free(r->name);
2020 }
2021 
2022 static const struct spdk_json_object_decoder rpc_get_io_paths_decoders[] = {
2023 	{"name", offsetof(struct rpc_get_io_paths, name), spdk_json_decode_string, true},
2024 };
2025 
2026 struct rpc_get_io_paths_ctx {
2027 	struct rpc_get_io_paths req;
2028 	struct spdk_jsonrpc_request *request;
2029 	struct spdk_json_write_ctx *w;
2030 };
2031 
2032 static void
2033 rpc_bdev_nvme_get_io_paths_done(struct spdk_io_channel_iter *i, int status)
2034 {
2035 	struct rpc_get_io_paths_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
2036 
2037 	spdk_json_write_array_end(ctx->w);
2038 
2039 	spdk_json_write_object_end(ctx->w);
2040 
2041 	spdk_jsonrpc_end_result(ctx->request, ctx->w);
2042 
2043 	free_rpc_get_io_paths(&ctx->req);
2044 	free(ctx);
2045 }
2046 
2047 static void
2048 _rpc_bdev_nvme_get_io_paths(struct spdk_io_channel_iter *i)
2049 {
2050 	struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i);
2051 	struct nvme_poll_group *group = spdk_io_channel_get_ctx(_ch);
2052 	struct rpc_get_io_paths_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
2053 	struct nvme_qpair *qpair;
2054 	struct nvme_io_path *io_path;
2055 	struct nvme_bdev *nbdev;
2056 
2057 	spdk_json_write_object_begin(ctx->w);
2058 
2059 	spdk_json_write_named_string(ctx->w, "thread", spdk_thread_get_name(spdk_get_thread()));
2060 
2061 	spdk_json_write_named_array_begin(ctx->w, "io_paths");
2062 
2063 	TAILQ_FOREACH(qpair, &group->qpair_list, tailq) {
2064 		TAILQ_FOREACH(io_path, &qpair->io_path_list, tailq) {
2065 			nbdev = io_path->nvme_ns->bdev;
2066 
2067 			if (ctx->req.name != NULL &&
2068 			    strcmp(ctx->req.name, nbdev->disk.name) != 0) {
2069 				continue;
2070 			}
2071 
2072 			nvme_io_path_info_json(ctx->w, io_path);
2073 		}
2074 	}
2075 
2076 	spdk_json_write_array_end(ctx->w);
2077 
2078 	spdk_json_write_object_end(ctx->w);
2079 
2080 	spdk_for_each_channel_continue(i, 0);
2081 }
2082 
2083 static void
2084 rpc_bdev_nvme_get_io_paths(struct spdk_jsonrpc_request *request,
2085 			   const struct spdk_json_val *params)
2086 {
2087 	struct rpc_get_io_paths_ctx *ctx;
2088 
2089 	ctx = calloc(1, sizeof(*ctx));
2090 	if (ctx == NULL) {
2091 		spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM));
2092 		return;
2093 	}
2094 
2095 	if (params != NULL &&
2096 	    spdk_json_decode_object(params, rpc_get_io_paths_decoders,
2097 				    SPDK_COUNTOF(rpc_get_io_paths_decoders),
2098 				    &ctx->req)) {
2099 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
2100 						 "bdev_nvme_get_io_paths requires no parameters");
2101 
2102 		free_rpc_get_io_paths(&ctx->req);
2103 		free(ctx);
2104 		return;
2105 	}
2106 
2107 	ctx->request = request;
2108 	ctx->w = spdk_jsonrpc_begin_result(request);
2109 
2110 	spdk_json_write_object_begin(ctx->w);
2111 
2112 	spdk_json_write_named_array_begin(ctx->w, "poll_groups");
2113 
2114 	spdk_for_each_channel(&g_nvme_bdev_ctrlrs,
2115 			      _rpc_bdev_nvme_get_io_paths,
2116 			      ctx,
2117 			      rpc_bdev_nvme_get_io_paths_done);
2118 }
2119 SPDK_RPC_REGISTER("bdev_nvme_get_io_paths", rpc_bdev_nvme_get_io_paths, SPDK_RPC_RUNTIME)
2120 
2121 struct rpc_bdev_nvme_set_preferred_path {
2122 	char *name;
2123 	uint16_t cntlid;
2124 };
2125 
2126 static void
2127 free_rpc_bdev_nvme_set_preferred_path(struct rpc_bdev_nvme_set_preferred_path *req)
2128 {
2129 	free(req->name);
2130 }
2131 
2132 static const struct spdk_json_object_decoder rpc_bdev_nvme_set_preferred_path_decoders[] = {
2133 	{"name", offsetof(struct rpc_bdev_nvme_set_preferred_path, name), spdk_json_decode_string},
2134 	{"cntlid", offsetof(struct rpc_bdev_nvme_set_preferred_path, cntlid), spdk_json_decode_uint16},
2135 };
2136 
2137 struct rpc_bdev_nvme_set_preferred_path_ctx {
2138 	struct rpc_bdev_nvme_set_preferred_path req;
2139 	struct spdk_jsonrpc_request *request;
2140 };
2141 
2142 static void
2143 rpc_bdev_nvme_set_preferred_path_done(void *cb_arg, int rc)
2144 {
2145 	struct rpc_bdev_nvme_set_preferred_path_ctx *ctx = cb_arg;
2146 
2147 	if (rc == 0) {
2148 		spdk_jsonrpc_send_bool_response(ctx->request, true);
2149 	} else {
2150 		spdk_jsonrpc_send_error_response(ctx->request, rc, spdk_strerror(-rc));
2151 	}
2152 
2153 	free_rpc_bdev_nvme_set_preferred_path(&ctx->req);
2154 	free(ctx);
2155 }
2156 
2157 static void
2158 rpc_bdev_nvme_set_preferred_path(struct spdk_jsonrpc_request *request,
2159 				 const struct spdk_json_val *params)
2160 {
2161 	struct rpc_bdev_nvme_set_preferred_path_ctx *ctx;
2162 
2163 	ctx = calloc(1, sizeof(*ctx));
2164 	if (ctx == NULL) {
2165 		spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM));
2166 		return;
2167 	}
2168 
2169 	if (spdk_json_decode_object(params, rpc_bdev_nvme_set_preferred_path_decoders,
2170 				    SPDK_COUNTOF(rpc_bdev_nvme_set_preferred_path_decoders),
2171 				    &ctx->req)) {
2172 		SPDK_ERRLOG("spdk_json_decode_object failed\n");
2173 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
2174 						 "spdk_json_decode_object failed");
2175 		goto cleanup;
2176 	}
2177 
2178 	ctx->request = request;
2179 
2180 	bdev_nvme_set_preferred_path(ctx->req.name, ctx->req.cntlid,
2181 				     rpc_bdev_nvme_set_preferred_path_done, ctx);
2182 	return;
2183 
2184 cleanup:
2185 	free_rpc_bdev_nvme_set_preferred_path(&ctx->req);
2186 	free(ctx);
2187 }
2188 SPDK_RPC_REGISTER("bdev_nvme_set_preferred_path", rpc_bdev_nvme_set_preferred_path,
2189 		  SPDK_RPC_RUNTIME)
2190 
2191 struct rpc_set_multipath_policy {
2192 	char *name;
2193 	enum bdev_nvme_multipath_policy policy;
2194 };
2195 
2196 static void
2197 free_rpc_set_multipath_policy(struct rpc_set_multipath_policy *req)
2198 {
2199 	free(req->name);
2200 }
2201 
2202 static int
2203 rpc_decode_mp_policy(const struct spdk_json_val *val, void *out)
2204 {
2205 	enum bdev_nvme_multipath_policy *policy = out;
2206 
2207 	if (spdk_json_strequal(val, "active_passive") == true) {
2208 		*policy = BDEV_NVME_MP_POLICY_ACTIVE_PASSIVE;
2209 	} else if (spdk_json_strequal(val, "active_active") == true) {
2210 		*policy = BDEV_NVME_MP_POLICY_ACTIVE_ACTIVE;
2211 	} else {
2212 		SPDK_NOTICELOG("Invalid parameter value: policy\n");
2213 		return -EINVAL;
2214 	}
2215 
2216 	return 0;
2217 }
2218 
2219 static const struct spdk_json_object_decoder rpc_set_multipath_policy_decoders[] = {
2220 	{"name", offsetof(struct rpc_set_multipath_policy, name), spdk_json_decode_string},
2221 	{"policy", offsetof(struct rpc_set_multipath_policy, policy), rpc_decode_mp_policy},
2222 };
2223 
2224 struct rpc_set_multipath_policy_ctx {
2225 	struct rpc_set_multipath_policy req;
2226 	struct spdk_jsonrpc_request *request;
2227 };
2228 
2229 static void
2230 rpc_bdev_nvme_set_multipath_policy_done(void *cb_arg, int rc)
2231 {
2232 	struct rpc_set_multipath_policy_ctx *ctx = cb_arg;
2233 
2234 	if (rc == 0) {
2235 		spdk_jsonrpc_send_bool_response(ctx->request, true);
2236 	} else {
2237 		spdk_jsonrpc_send_error_response(ctx->request, rc, spdk_strerror(-rc));
2238 	}
2239 
2240 	free_rpc_set_multipath_policy(&ctx->req);
2241 	free(ctx);
2242 }
2243 
2244 static void
2245 rpc_bdev_nvme_set_multipath_policy(struct spdk_jsonrpc_request *request,
2246 				   const struct spdk_json_val *params)
2247 {
2248 	struct rpc_set_multipath_policy_ctx *ctx;
2249 
2250 	ctx = calloc(1, sizeof(*ctx));
2251 	if (ctx == NULL) {
2252 		spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM));
2253 		return;
2254 	}
2255 
2256 	if (spdk_json_decode_object(params, rpc_set_multipath_policy_decoders,
2257 				    SPDK_COUNTOF(rpc_set_multipath_policy_decoders),
2258 				    &ctx->req)) {
2259 		SPDK_ERRLOG("spdk_json_decode_object failed\n");
2260 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
2261 						 "spdk_json_decode_object failed");
2262 		goto cleanup;
2263 	}
2264 
2265 	ctx->request = request;
2266 
2267 	bdev_nvme_set_multipath_policy(ctx->req.name, ctx->req.policy,
2268 				       rpc_bdev_nvme_set_multipath_policy_done, ctx);
2269 	return;
2270 
2271 cleanup:
2272 	free_rpc_set_multipath_policy(&ctx->req);
2273 	free(ctx);
2274 }
2275 SPDK_RPC_REGISTER("bdev_nvme_set_multipath_policy", rpc_bdev_nvme_set_multipath_policy,
2276 		  SPDK_RPC_RUNTIME)
2277