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