xref: /spdk/module/bdev/nvme/bdev_nvme_rpc.c (revision da2fd6651a9cd4732b0910d30291821e77f4d643)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation. All rights reserved.
5  *   Copyright (c) 2019-2021 Mellanox Technologies LTD. All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "spdk/stdinc.h"
35 
36 #include "bdev_nvme.h"
37 #include "common.h"
38 
39 #include "spdk/config.h"
40 
41 #include "spdk/string.h"
42 #include "spdk/rpc.h"
43 #include "spdk/util.h"
44 
45 #include "spdk/log.h"
46 #include "spdk/bdev_module.h"
47 
48 struct open_descriptors {
49 	void *desc;
50 	struct  spdk_bdev *bdev;
51 	TAILQ_ENTRY(open_descriptors) tqlst;
52 	struct spdk_thread *thread;
53 };
54 typedef TAILQ_HEAD(, open_descriptors) open_descriptors_t;
55 
56 static int
57 rpc_decode_action_on_timeout(const struct spdk_json_val *val, void *out)
58 {
59 	enum spdk_bdev_timeout_action *action = out;
60 
61 	if (spdk_json_strequal(val, "none") == true) {
62 		*action = SPDK_BDEV_NVME_TIMEOUT_ACTION_NONE;
63 	} else if (spdk_json_strequal(val, "abort") == true) {
64 		*action = SPDK_BDEV_NVME_TIMEOUT_ACTION_ABORT;
65 	} else if (spdk_json_strequal(val, "reset") == true) {
66 		*action = SPDK_BDEV_NVME_TIMEOUT_ACTION_RESET;
67 	} else {
68 		SPDK_NOTICELOG("Invalid parameter value: action_on_timeout\n");
69 		return -EINVAL;
70 	}
71 
72 	return 0;
73 }
74 
75 static const struct spdk_json_object_decoder rpc_bdev_nvme_options_decoders[] = {
76 	{"action_on_timeout", offsetof(struct spdk_bdev_nvme_opts, action_on_timeout), rpc_decode_action_on_timeout, true},
77 	{"timeout_us", offsetof(struct spdk_bdev_nvme_opts, timeout_us), spdk_json_decode_uint64, true},
78 	{"timeout_admin_us", offsetof(struct spdk_bdev_nvme_opts, timeout_admin_us), spdk_json_decode_uint64, true},
79 	{"keep_alive_timeout_ms", offsetof(struct spdk_bdev_nvme_opts, keep_alive_timeout_ms), spdk_json_decode_uint32, true},
80 	{"retry_count", offsetof(struct spdk_bdev_nvme_opts, retry_count), spdk_json_decode_uint32, true},
81 	{"arbitration_burst", offsetof(struct spdk_bdev_nvme_opts, arbitration_burst), spdk_json_decode_uint32, true},
82 	{"low_priority_weight", offsetof(struct spdk_bdev_nvme_opts, low_priority_weight), spdk_json_decode_uint32, true},
83 	{"medium_priority_weight", offsetof(struct spdk_bdev_nvme_opts, medium_priority_weight), spdk_json_decode_uint32, true},
84 	{"high_priority_weight", offsetof(struct spdk_bdev_nvme_opts, high_priority_weight), spdk_json_decode_uint32, true},
85 	{"nvme_adminq_poll_period_us", offsetof(struct spdk_bdev_nvme_opts, nvme_adminq_poll_period_us), spdk_json_decode_uint64, true},
86 	{"nvme_ioq_poll_period_us", offsetof(struct spdk_bdev_nvme_opts, nvme_ioq_poll_period_us), spdk_json_decode_uint64, true},
87 	{"io_queue_requests", offsetof(struct spdk_bdev_nvme_opts, io_queue_requests), spdk_json_decode_uint32, true},
88 	{"delay_cmd_submit", offsetof(struct spdk_bdev_nvme_opts, delay_cmd_submit), spdk_json_decode_bool, true},
89 };
90 
91 static void
92 rpc_bdev_nvme_set_options(struct spdk_jsonrpc_request *request,
93 			  const struct spdk_json_val *params)
94 {
95 	struct spdk_bdev_nvme_opts opts;
96 	int rc;
97 
98 	bdev_nvme_get_opts(&opts);
99 	if (params && spdk_json_decode_object(params, rpc_bdev_nvme_options_decoders,
100 					      SPDK_COUNTOF(rpc_bdev_nvme_options_decoders),
101 					      &opts)) {
102 		SPDK_ERRLOG("spdk_json_decode_object failed\n");
103 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
104 						 "spdk_json_decode_object failed");
105 		return;
106 	}
107 
108 	rc = bdev_nvme_set_opts(&opts);
109 	if (rc) {
110 		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
111 		return;
112 	}
113 
114 	spdk_jsonrpc_send_bool_response(request, true);
115 
116 	return;
117 }
118 SPDK_RPC_REGISTER("bdev_nvme_set_options", rpc_bdev_nvme_set_options,
119 		  SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME)
120 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_nvme_set_options, set_bdev_nvme_options)
121 
122 struct rpc_bdev_nvme_hotplug {
123 	bool enabled;
124 	uint64_t period_us;
125 };
126 
127 static const struct spdk_json_object_decoder rpc_bdev_nvme_hotplug_decoders[] = {
128 	{"enable", offsetof(struct rpc_bdev_nvme_hotplug, enabled), spdk_json_decode_bool, false},
129 	{"period_us", offsetof(struct rpc_bdev_nvme_hotplug, period_us), spdk_json_decode_uint64, true},
130 };
131 
132 static void
133 rpc_bdev_nvme_set_hotplug_done(void *ctx)
134 {
135 	struct spdk_jsonrpc_request *request = ctx;
136 
137 	spdk_jsonrpc_send_bool_response(request, true);
138 }
139 
140 static void
141 rpc_bdev_nvme_set_hotplug(struct spdk_jsonrpc_request *request,
142 			  const struct spdk_json_val *params)
143 {
144 	struct rpc_bdev_nvme_hotplug req = {false, 0};
145 	int rc;
146 
147 	if (spdk_json_decode_object(params, rpc_bdev_nvme_hotplug_decoders,
148 				    SPDK_COUNTOF(rpc_bdev_nvme_hotplug_decoders), &req)) {
149 		SPDK_ERRLOG("spdk_json_decode_object failed\n");
150 		rc = -EINVAL;
151 		goto invalid;
152 	}
153 
154 	rc = bdev_nvme_set_hotplug(req.enabled, req.period_us, rpc_bdev_nvme_set_hotplug_done,
155 				   request);
156 	if (rc) {
157 		goto invalid;
158 	}
159 
160 	return;
161 invalid:
162 	spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, spdk_strerror(-rc));
163 }
164 SPDK_RPC_REGISTER("bdev_nvme_set_hotplug", rpc_bdev_nvme_set_hotplug, SPDK_RPC_RUNTIME)
165 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_nvme_set_hotplug, set_bdev_nvme_hotplug)
166 
167 struct rpc_bdev_nvme_attach_controller {
168 	char *name;
169 	char *trtype;
170 	char *adrfam;
171 	char *traddr;
172 	char *trsvcid;
173 	char *priority;
174 	char *subnqn;
175 	char *hostnqn;
176 	char *hostaddr;
177 	char *hostsvcid;
178 	bool prchk_reftag;
179 	bool prchk_guard;
180 	struct spdk_nvme_ctrlr_opts opts;
181 };
182 
183 static void
184 free_rpc_bdev_nvme_attach_controller(struct rpc_bdev_nvme_attach_controller *req)
185 {
186 	free(req->name);
187 	free(req->trtype);
188 	free(req->adrfam);
189 	free(req->traddr);
190 	free(req->trsvcid);
191 	free(req->priority);
192 	free(req->subnqn);
193 	free(req->hostnqn);
194 	free(req->hostaddr);
195 	free(req->hostsvcid);
196 }
197 
198 static const struct spdk_json_object_decoder rpc_bdev_nvme_attach_controller_decoders[] = {
199 	{"name", offsetof(struct rpc_bdev_nvme_attach_controller, name), spdk_json_decode_string},
200 	{"trtype", offsetof(struct rpc_bdev_nvme_attach_controller, trtype), spdk_json_decode_string},
201 	{"traddr", offsetof(struct rpc_bdev_nvme_attach_controller, traddr), spdk_json_decode_string},
202 
203 	{"adrfam", offsetof(struct rpc_bdev_nvme_attach_controller, adrfam), spdk_json_decode_string, true},
204 	{"trsvcid", offsetof(struct rpc_bdev_nvme_attach_controller, trsvcid), spdk_json_decode_string, true},
205 	{"priority", offsetof(struct rpc_bdev_nvme_attach_controller, priority), spdk_json_decode_string, true},
206 	{"subnqn", offsetof(struct rpc_bdev_nvme_attach_controller, subnqn), spdk_json_decode_string, true},
207 	{"hostnqn", offsetof(struct rpc_bdev_nvme_attach_controller, hostnqn), spdk_json_decode_string, true},
208 	{"hostaddr", offsetof(struct rpc_bdev_nvme_attach_controller, hostaddr), spdk_json_decode_string, true},
209 	{"hostsvcid", offsetof(struct rpc_bdev_nvme_attach_controller, hostsvcid), spdk_json_decode_string, true},
210 
211 	{"prchk_reftag", offsetof(struct rpc_bdev_nvme_attach_controller, prchk_reftag), spdk_json_decode_bool, true},
212 	{"prchk_guard", offsetof(struct rpc_bdev_nvme_attach_controller, prchk_guard), spdk_json_decode_bool, true},
213 	{"hdgst", offsetof(struct rpc_bdev_nvme_attach_controller, opts.header_digest), spdk_json_decode_bool, true},
214 	{"ddgst", offsetof(struct rpc_bdev_nvme_attach_controller, opts.data_digest), spdk_json_decode_bool, true}
215 };
216 
217 #define NVME_MAX_BDEVS_PER_RPC 128
218 
219 struct rpc_bdev_nvme_attach_controller_ctx {
220 	struct rpc_bdev_nvme_attach_controller req;
221 	uint32_t count;
222 	size_t bdev_count;
223 	const char *names[NVME_MAX_BDEVS_PER_RPC];
224 	struct spdk_jsonrpc_request *request;
225 };
226 
227 static void
228 rpc_bdev_nvme_attach_controller_examined(void *cb_ctx)
229 {
230 	struct rpc_bdev_nvme_attach_controller_ctx *ctx = cb_ctx;
231 	struct spdk_jsonrpc_request *request = ctx->request;
232 	struct spdk_json_write_ctx *w;
233 	size_t i;
234 
235 	w = spdk_jsonrpc_begin_result(request);
236 	spdk_json_write_array_begin(w);
237 	for (i = 0; i < ctx->bdev_count; i++) {
238 		spdk_json_write_string(w, ctx->names[i]);
239 	}
240 	spdk_json_write_array_end(w);
241 	spdk_jsonrpc_end_result(request, w);
242 
243 	free_rpc_bdev_nvme_attach_controller(&ctx->req);
244 	free(ctx);
245 }
246 
247 static void
248 rpc_bdev_nvme_attach_controller_done(void *cb_ctx, size_t bdev_count, int rc)
249 {
250 	struct rpc_bdev_nvme_attach_controller_ctx *ctx = cb_ctx;
251 	struct spdk_jsonrpc_request *request = ctx->request;
252 
253 	if (rc < 0) {
254 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
255 		free_rpc_bdev_nvme_attach_controller(&ctx->req);
256 		free(ctx);
257 		return;
258 	}
259 
260 	ctx->bdev_count = bdev_count;
261 	spdk_bdev_wait_for_examine(rpc_bdev_nvme_attach_controller_examined, ctx);
262 }
263 
264 static void
265 rpc_bdev_nvme_attach_controller(struct spdk_jsonrpc_request *request,
266 				const struct spdk_json_val *params)
267 {
268 	struct rpc_bdev_nvme_attach_controller_ctx *ctx;
269 	struct spdk_nvme_transport_id trid = {};
270 	struct spdk_nvme_host_id hostid = {};
271 	uint32_t prchk_flags = 0;
272 	struct nvme_ctrlr *ctrlr = NULL;
273 	size_t len, maxlen;
274 	int rc;
275 
276 	ctx = calloc(1, sizeof(*ctx));
277 	if (!ctx) {
278 		spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM));
279 		return;
280 	}
281 
282 	spdk_nvme_ctrlr_get_default_ctrlr_opts(&ctx->req.opts, sizeof(ctx->req.opts));
283 
284 	if (spdk_json_decode_object(params, rpc_bdev_nvme_attach_controller_decoders,
285 				    SPDK_COUNTOF(rpc_bdev_nvme_attach_controller_decoders),
286 				    &ctx->req)) {
287 		SPDK_ERRLOG("spdk_json_decode_object failed\n");
288 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
289 						 "spdk_json_decode_object failed");
290 		goto cleanup;
291 	}
292 
293 	/* Parse trstring */
294 	rc = spdk_nvme_transport_id_populate_trstring(&trid, ctx->req.trtype);
295 	if (rc < 0) {
296 		SPDK_ERRLOG("Failed to parse trtype: %s\n", ctx->req.trtype);
297 		spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse trtype: %s",
298 						     ctx->req.trtype);
299 		goto cleanup;
300 	}
301 
302 	/* Parse trtype */
303 	rc = spdk_nvme_transport_id_parse_trtype(&trid.trtype, ctx->req.trtype);
304 	assert(rc == 0);
305 
306 	ctrlr = nvme_ctrlr_get_by_name(ctx->req.name);
307 
308 	/* Parse traddr */
309 	maxlen = sizeof(trid.traddr);
310 	len = strnlen(ctx->req.traddr, maxlen);
311 	if (len == maxlen) {
312 		spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "traddr too long: %s",
313 						     ctx->req.traddr);
314 		goto cleanup;
315 	}
316 	memcpy(trid.traddr, ctx->req.traddr, len + 1);
317 
318 	/* Parse adrfam */
319 	if (ctx->req.adrfam) {
320 		rc = spdk_nvme_transport_id_parse_adrfam(&trid.adrfam, ctx->req.adrfam);
321 		if (rc < 0) {
322 			SPDK_ERRLOG("Failed to parse adrfam: %s\n", ctx->req.adrfam);
323 			spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse adrfam: %s",
324 							     ctx->req.adrfam);
325 			goto cleanup;
326 		}
327 	}
328 
329 	/* Parse trsvcid */
330 	if (ctx->req.trsvcid) {
331 		maxlen = sizeof(trid.trsvcid);
332 		len = strnlen(ctx->req.trsvcid, maxlen);
333 		if (len == maxlen) {
334 			spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "trsvcid too long: %s",
335 							     ctx->req.trsvcid);
336 			goto cleanup;
337 		}
338 		memcpy(trid.trsvcid, ctx->req.trsvcid, len + 1);
339 	}
340 
341 	/* Parse priority for the NVMe-oF transport connection */
342 	if (ctx->req.priority) {
343 		trid.priority = spdk_strtol(ctx->req.priority, 10);
344 	}
345 
346 	/* Parse subnqn */
347 	if (ctx->req.subnqn) {
348 		maxlen = sizeof(trid.subnqn);
349 		len = strnlen(ctx->req.subnqn, maxlen);
350 		if (len == maxlen) {
351 			spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "subnqn too long: %s",
352 							     ctx->req.subnqn);
353 			goto cleanup;
354 		}
355 		memcpy(trid.subnqn, ctx->req.subnqn, len + 1);
356 	}
357 
358 	if (ctrlr && (ctx->req.hostaddr || ctx->req.hostnqn || ctx->req.hostsvcid || ctx->req.prchk_guard ||
359 		      ctx->req.prchk_reftag)) {
360 		goto conflicting_arguments;
361 	}
362 
363 	if (ctx->req.hostaddr) {
364 		maxlen = sizeof(hostid.hostaddr);
365 		len = strnlen(ctx->req.hostaddr, maxlen);
366 		if (len == maxlen) {
367 			spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "hostaddr too long: %s",
368 							     ctx->req.hostaddr);
369 			goto cleanup;
370 		}
371 		memcpy(hostid.hostaddr, ctx->req.hostaddr, len + 1);
372 	}
373 
374 	if (ctx->req.hostsvcid) {
375 		maxlen = sizeof(hostid.hostsvcid);
376 		len = strnlen(ctx->req.hostsvcid, maxlen);
377 		if (len == maxlen) {
378 			spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "hostsvcid too long: %s",
379 							     ctx->req.hostsvcid);
380 			goto cleanup;
381 		}
382 		memcpy(hostid.hostsvcid, ctx->req.hostsvcid, len + 1);
383 	}
384 
385 	if (ctx->req.prchk_reftag) {
386 		prchk_flags |= SPDK_NVME_IO_FLAGS_PRCHK_REFTAG;
387 	}
388 
389 	if (ctx->req.prchk_guard) {
390 		prchk_flags |= SPDK_NVME_IO_FLAGS_PRCHK_GUARD;
391 	}
392 
393 	ctx->request = request;
394 	ctx->count = NVME_MAX_BDEVS_PER_RPC;
395 	rc = bdev_nvme_create(&trid, &hostid, ctx->req.name, ctx->names, ctx->count, ctx->req.hostnqn,
396 			      prchk_flags, rpc_bdev_nvme_attach_controller_done, ctx, &ctx->req.opts);
397 	if (rc) {
398 		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
399 		goto cleanup;
400 	}
401 
402 	return;
403 
404 conflicting_arguments:
405 	spdk_jsonrpc_send_error_response_fmt(request, -EINVAL,
406 					     "Invalid agrgument list. Existing controller name cannot be combined with host information or PI options.\n");
407 cleanup:
408 	free_rpc_bdev_nvme_attach_controller(&ctx->req);
409 	free(ctx);
410 }
411 SPDK_RPC_REGISTER("bdev_nvme_attach_controller", rpc_bdev_nvme_attach_controller,
412 		  SPDK_RPC_RUNTIME)
413 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_nvme_attach_controller, construct_nvme_bdev)
414 
415 static void
416 rpc_dump_nvme_controller_info(struct nvme_ctrlr *nvme_ctrlr, void *ctx)
417 {
418 	struct spdk_json_write_ctx	*w = ctx;
419 	struct spdk_nvme_transport_id	*trid;
420 
421 	trid = nvme_ctrlr->connected_trid;
422 
423 	spdk_json_write_object_begin(w);
424 	spdk_json_write_named_string(w, "name", nvme_ctrlr->name);
425 
426 #ifdef SPDK_CONFIG_NVME_CUSE
427 	size_t cuse_name_size = 128;
428 	char cuse_name[cuse_name_size];
429 
430 	int rc = spdk_nvme_cuse_get_ctrlr_name(nvme_ctrlr->ctrlr, cuse_name, &cuse_name_size);
431 	if (rc == 0) {
432 		spdk_json_write_named_string(w, "cuse_device", cuse_name);
433 	}
434 #endif
435 
436 	spdk_json_write_named_object_begin(w, "trid");
437 	nvme_bdev_dump_trid_json(trid, w);
438 	spdk_json_write_object_end(w);
439 
440 	spdk_json_write_object_end(w);
441 }
442 
443 struct rpc_bdev_nvme_get_controllers {
444 	char *name;
445 };
446 
447 static void
448 free_rpc_bdev_nvme_get_controllers(struct rpc_bdev_nvme_get_controllers *r)
449 {
450 	free(r->name);
451 }
452 
453 static const struct spdk_json_object_decoder rpc_bdev_nvme_get_controllers_decoders[] = {
454 	{"name", offsetof(struct rpc_bdev_nvme_get_controllers, name), spdk_json_decode_string, true},
455 };
456 
457 static void
458 rpc_bdev_nvme_get_controllers(struct spdk_jsonrpc_request *request,
459 			      const struct spdk_json_val *params)
460 {
461 	struct rpc_bdev_nvme_get_controllers req = {};
462 	struct spdk_json_write_ctx *w;
463 	struct nvme_ctrlr *ctrlr = NULL;
464 
465 	if (params && spdk_json_decode_object(params, rpc_bdev_nvme_get_controllers_decoders,
466 					      SPDK_COUNTOF(rpc_bdev_nvme_get_controllers_decoders),
467 					      &req)) {
468 		SPDK_ERRLOG("spdk_json_decode_object failed\n");
469 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
470 						 "spdk_json_decode_object failed");
471 		goto cleanup;
472 	}
473 
474 	if (req.name) {
475 		ctrlr = nvme_ctrlr_get_by_name(req.name);
476 		if (ctrlr == NULL) {
477 			SPDK_ERRLOG("ctrlr '%s' does not exist\n", req.name);
478 			spdk_jsonrpc_send_error_response_fmt(request, EINVAL, "Controller %s does not exist", req.name);
479 			goto cleanup;
480 		}
481 	}
482 
483 	w = spdk_jsonrpc_begin_result(request);
484 	spdk_json_write_array_begin(w);
485 
486 	if (ctrlr != NULL) {
487 		rpc_dump_nvme_controller_info(ctrlr, w);
488 	} else {
489 		nvme_ctrlr_for_each(rpc_dump_nvme_controller_info, w);
490 	}
491 
492 	spdk_json_write_array_end(w);
493 
494 	spdk_jsonrpc_end_result(request, w);
495 
496 cleanup:
497 	free_rpc_bdev_nvme_get_controllers(&req);
498 }
499 SPDK_RPC_REGISTER("bdev_nvme_get_controllers", rpc_bdev_nvme_get_controllers, SPDK_RPC_RUNTIME)
500 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_nvme_get_controllers, get_nvme_controllers)
501 
502 struct rpc_bdev_nvme_detach_controller {
503 	char *name;
504 	char *trtype;
505 	char *adrfam;
506 	char *traddr;
507 	char *trsvcid;
508 	char *subnqn;
509 };
510 
511 static void
512 free_rpc_bdev_nvme_detach_controller(struct rpc_bdev_nvme_detach_controller *req)
513 {
514 	free(req->name);
515 	free(req->trtype);
516 	free(req->adrfam);
517 	free(req->traddr);
518 	free(req->trsvcid);
519 	free(req->subnqn);
520 }
521 
522 static const struct spdk_json_object_decoder rpc_bdev_nvme_detach_controller_decoders[] = {
523 	{"name", offsetof(struct rpc_bdev_nvme_detach_controller, name), spdk_json_decode_string},
524 	{"trtype", offsetof(struct rpc_bdev_nvme_detach_controller, trtype), spdk_json_decode_string, true},
525 	{"traddr", offsetof(struct rpc_bdev_nvme_detach_controller, traddr), spdk_json_decode_string, true},
526 	{"adrfam", offsetof(struct rpc_bdev_nvme_detach_controller, adrfam), spdk_json_decode_string, true},
527 	{"trsvcid", offsetof(struct rpc_bdev_nvme_detach_controller, trsvcid), spdk_json_decode_string, true},
528 	{"subnqn", offsetof(struct rpc_bdev_nvme_detach_controller, subnqn), spdk_json_decode_string, true},
529 };
530 
531 static void
532 rpc_bdev_nvme_detach_controller(struct spdk_jsonrpc_request *request,
533 				const struct spdk_json_val *params)
534 {
535 	struct rpc_bdev_nvme_detach_controller req = {NULL};
536 	struct spdk_nvme_transport_id trid = {};
537 	size_t len, maxlen;
538 	int rc = 0;
539 	bool all_trid_entries, one_trid_entry;
540 
541 	if (spdk_json_decode_object(params, rpc_bdev_nvme_detach_controller_decoders,
542 				    SPDK_COUNTOF(rpc_bdev_nvme_detach_controller_decoders),
543 				    &req)) {
544 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
545 						 "spdk_json_decode_object failed");
546 		goto cleanup;
547 	}
548 
549 	all_trid_entries = req.trtype && req.traddr && req.adrfam && req.trsvcid && req.subnqn;
550 	one_trid_entry = req.trtype || req.traddr || req.adrfam || req.trsvcid || req.subnqn;
551 
552 	if (all_trid_entries ^ one_trid_entry) {
553 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
554 						 "trtype, traddr, adrfam, trsvcid, subnqn must all be provided together or not at all.");
555 		goto cleanup;
556 	}
557 
558 	if (all_trid_entries) {
559 		/* Parse trtype */
560 		rc = spdk_nvme_transport_id_parse_trtype(&trid.trtype, req.trtype);
561 		if (rc < 0) {
562 			SPDK_ERRLOG("Failed to parse trtype: %s\n", req.trtype);
563 			spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse trtype: %s",
564 							     req.trtype);
565 			goto cleanup;
566 		}
567 
568 		/* Parse traddr */
569 		maxlen = sizeof(trid.traddr);
570 		len = strnlen(req.traddr, maxlen);
571 		if (len == maxlen) {
572 			spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "traddr too long: %s",
573 							     req.traddr);
574 			goto cleanup;
575 		}
576 		memcpy(trid.traddr, req.traddr, len + 1);
577 
578 		rc = spdk_nvme_transport_id_parse_adrfam(&trid.adrfam, req.adrfam);
579 		if (rc < 0) {
580 			SPDK_ERRLOG("Failed to parse adrfam: %s\n", req.adrfam);
581 			spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse adrfam: %s",
582 							     req.adrfam);
583 			goto cleanup;
584 		}
585 
586 		maxlen = sizeof(trid.trsvcid);
587 		len = strnlen(req.trsvcid, maxlen);
588 		if (len == maxlen) {
589 			spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "trsvcid too long: %s",
590 							     req.trsvcid);
591 			goto cleanup;
592 		}
593 		memcpy(trid.trsvcid, req.trsvcid, len + 1);
594 
595 		maxlen = sizeof(trid.subnqn);
596 		len = strnlen(req.subnqn, maxlen);
597 		if (len == maxlen) {
598 			spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "subnqn too long: %s",
599 							     req.subnqn);
600 			goto cleanup;
601 		}
602 		memcpy(trid.subnqn, req.subnqn, len + 1);
603 		rc = bdev_nvme_delete(req.name, &trid);
604 	} else {
605 		rc = bdev_nvme_delete(req.name, NULL);
606 	}
607 
608 	if (rc != 0) {
609 		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
610 		goto cleanup;
611 	}
612 
613 	spdk_jsonrpc_send_bool_response(request, true);
614 
615 cleanup:
616 	free_rpc_bdev_nvme_detach_controller(&req);
617 }
618 SPDK_RPC_REGISTER("bdev_nvme_detach_controller", rpc_bdev_nvme_detach_controller,
619 		  SPDK_RPC_RUNTIME)
620 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_nvme_detach_controller, delete_nvme_controller)
621 
622 struct rpc_apply_firmware {
623 	char *filename;
624 	char *bdev_name;
625 };
626 
627 static void
628 free_rpc_apply_firmware(struct rpc_apply_firmware *req)
629 {
630 	free(req->filename);
631 	free(req->bdev_name);
632 }
633 
634 static const struct spdk_json_object_decoder rpc_apply_firmware_decoders[] = {
635 	{"filename", offsetof(struct rpc_apply_firmware, filename), spdk_json_decode_string},
636 	{"bdev_name", offsetof(struct rpc_apply_firmware, bdev_name), spdk_json_decode_string},
637 };
638 
639 struct firmware_update_info {
640 	void				*fw_image;
641 	void				*p;
642 	unsigned int			size;
643 	unsigned int			size_remaining;
644 	unsigned int			offset;
645 	unsigned int			transfer;
646 
647 	void				*desc;
648 	struct spdk_io_channel		*ch;
649 	struct spdk_jsonrpc_request	*request;
650 	struct spdk_nvme_ctrlr		*ctrlr;
651 	open_descriptors_t		desc_head;
652 	struct rpc_apply_firmware	*req;
653 };
654 
655 static void
656 _apply_firmware_cleanup(void *ctx)
657 {
658 	struct spdk_bdev_desc *desc = ctx;
659 
660 	spdk_bdev_close(desc);
661 }
662 
663 static void
664 apply_firmware_cleanup(void *cb_arg)
665 {
666 	struct open_descriptors			*opt, *tmp;
667 	struct firmware_update_info *firm_ctx = cb_arg;
668 
669 	if (!firm_ctx) {
670 		return;
671 	}
672 
673 	if (firm_ctx->fw_image) {
674 		spdk_free(firm_ctx->fw_image);
675 	}
676 
677 	if (firm_ctx->req) {
678 		free_rpc_apply_firmware(firm_ctx->req);
679 		free(firm_ctx->req);
680 	}
681 
682 	if (firm_ctx->ch) {
683 		spdk_put_io_channel(firm_ctx->ch);
684 	}
685 
686 	TAILQ_FOREACH_SAFE(opt, &firm_ctx->desc_head, tqlst, tmp) {
687 		TAILQ_REMOVE(&firm_ctx->desc_head, opt, tqlst);
688 		/* Close the underlying bdev on its same opened thread. */
689 		if (opt->thread && opt->thread != spdk_get_thread()) {
690 			spdk_thread_send_msg(opt->thread, _apply_firmware_cleanup, opt->desc);
691 		} else {
692 			spdk_bdev_close(opt->desc);
693 		}
694 		free(opt);
695 	}
696 	free(firm_ctx);
697 }
698 
699 static void
700 apply_firmware_complete_reset(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
701 {
702 	struct spdk_json_write_ctx		*w;
703 	struct firmware_update_info *firm_ctx = cb_arg;
704 
705 	spdk_bdev_free_io(bdev_io);
706 
707 	if (!success) {
708 		spdk_jsonrpc_send_error_response(firm_ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
709 						 "firmware commit failed.");
710 		apply_firmware_cleanup(firm_ctx);
711 		return;
712 	}
713 
714 	if (spdk_nvme_ctrlr_reset(firm_ctx->ctrlr) != 0) {
715 		spdk_jsonrpc_send_error_response(firm_ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
716 						 "Controller reset failed.");
717 		apply_firmware_cleanup(firm_ctx);
718 		return;
719 	}
720 
721 	w = spdk_jsonrpc_begin_result(firm_ctx->request);
722 	spdk_json_write_string(w, "firmware commit succeeded. Controller reset in progress.");
723 	spdk_jsonrpc_end_result(firm_ctx->request, w);
724 	apply_firmware_cleanup(firm_ctx);
725 }
726 
727 static void
728 apply_firmware_complete(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
729 {
730 	struct spdk_nvme_cmd			cmd = {};
731 	struct spdk_nvme_fw_commit		fw_commit;
732 	int					slot = 0;
733 	int					rc;
734 	struct firmware_update_info *firm_ctx = cb_arg;
735 	enum spdk_nvme_fw_commit_action commit_action = SPDK_NVME_FW_COMMIT_REPLACE_AND_ENABLE_IMG;
736 
737 	if (!success) {
738 		spdk_jsonrpc_send_error_response(firm_ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
739 						 "firmware download failed .");
740 		spdk_bdev_free_io(bdev_io);
741 		apply_firmware_cleanup(firm_ctx);
742 		return;
743 	}
744 
745 	firm_ctx->p += firm_ctx->transfer;
746 	firm_ctx->offset += firm_ctx->transfer;
747 	firm_ctx->size_remaining -= firm_ctx->transfer;
748 
749 	switch (firm_ctx->size_remaining) {
750 	case 0:
751 		/* firmware download completed. Commit firmware */
752 		memset(&fw_commit, 0, sizeof(struct spdk_nvme_fw_commit));
753 		fw_commit.fs = slot;
754 		fw_commit.ca = commit_action;
755 
756 		cmd.opc = SPDK_NVME_OPC_FIRMWARE_COMMIT;
757 		memcpy(&cmd.cdw10, &fw_commit, sizeof(uint32_t));
758 		rc = spdk_bdev_nvme_admin_passthru(firm_ctx->desc, firm_ctx->ch, &cmd, NULL, 0,
759 						   apply_firmware_complete_reset, firm_ctx);
760 		if (rc) {
761 			spdk_jsonrpc_send_error_response(firm_ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
762 							 "firmware commit failed.");
763 			spdk_bdev_free_io(bdev_io);
764 			apply_firmware_cleanup(firm_ctx);
765 			return;
766 		}
767 		break;
768 	default:
769 		firm_ctx->transfer = spdk_min(firm_ctx->size_remaining, 4096);
770 		cmd.opc = SPDK_NVME_OPC_FIRMWARE_IMAGE_DOWNLOAD;
771 
772 		cmd.cdw10 = spdk_nvme_bytes_to_numd(firm_ctx->transfer);
773 		cmd.cdw11 = firm_ctx->offset >> 2;
774 		rc = spdk_bdev_nvme_admin_passthru(firm_ctx->desc, firm_ctx->ch, &cmd, firm_ctx->p,
775 						   firm_ctx->transfer, apply_firmware_complete, firm_ctx);
776 		if (rc) {
777 			spdk_jsonrpc_send_error_response(firm_ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
778 							 "firmware download failed.");
779 			spdk_bdev_free_io(bdev_io);
780 			apply_firmware_cleanup(firm_ctx);
781 			return;
782 		}
783 		break;
784 	}
785 }
786 
787 static void
788 apply_firmware_open_cb(enum spdk_bdev_event_type type, struct spdk_bdev *bdev, void *event_ctx)
789 {
790 }
791 
792 static void
793 rpc_bdev_nvme_apply_firmware(struct spdk_jsonrpc_request *request,
794 			     const struct spdk_json_val *params)
795 {
796 	int					rc;
797 	int					fd = -1;
798 	struct stat				fw_stat;
799 	struct spdk_nvme_ctrlr			*ctrlr;
800 	char					msg[1024];
801 	struct spdk_bdev			*bdev;
802 	struct spdk_bdev			*bdev2;
803 	struct open_descriptors			*opt;
804 	struct spdk_bdev_desc			*desc;
805 	struct spdk_nvme_cmd			*cmd;
806 	struct firmware_update_info		*firm_ctx;
807 
808 	firm_ctx = calloc(1, sizeof(struct firmware_update_info));
809 	if (!firm_ctx) {
810 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
811 						 "Memory allocation error.");
812 		return;
813 	}
814 	firm_ctx->fw_image = NULL;
815 	TAILQ_INIT(&firm_ctx->desc_head);
816 	firm_ctx->request = request;
817 
818 	firm_ctx->req = calloc(1, sizeof(struct rpc_apply_firmware));
819 	if (!firm_ctx->req) {
820 		snprintf(msg, sizeof(msg), "Memory allocation error.");
821 		goto err;
822 	}
823 
824 	if (spdk_json_decode_object(params, rpc_apply_firmware_decoders,
825 				    SPDK_COUNTOF(rpc_apply_firmware_decoders), firm_ctx->req)) {
826 		snprintf(msg, sizeof(msg), "spdk_json_decode_object failed.");
827 		goto err;
828 	}
829 
830 	if ((bdev = spdk_bdev_get_by_name(firm_ctx->req->bdev_name)) == NULL) {
831 		snprintf(msg, sizeof(msg), "bdev %s were not found", firm_ctx->req->bdev_name);
832 		goto err;
833 	}
834 
835 	if ((ctrlr = bdev_nvme_get_ctrlr(bdev)) == NULL) {
836 		snprintf(msg, sizeof(msg), "Controller information for %s were not found.",
837 			 firm_ctx->req->bdev_name);
838 		goto err;
839 	}
840 	firm_ctx->ctrlr = ctrlr;
841 
842 	for (bdev2 = spdk_bdev_first(); bdev2; bdev2 = spdk_bdev_next(bdev2)) {
843 
844 		if (bdev_nvme_get_ctrlr(bdev2) != ctrlr) {
845 			continue;
846 		}
847 
848 		if (!(opt = malloc(sizeof(struct open_descriptors)))) {
849 			snprintf(msg, sizeof(msg), "Memory allocation error.");
850 			goto err;
851 		}
852 
853 		if (spdk_bdev_open_ext(spdk_bdev_get_name(bdev2), true, apply_firmware_open_cb, NULL, &desc) != 0) {
854 			snprintf(msg, sizeof(msg), "Device %s is in use.", firm_ctx->req->bdev_name);
855 			free(opt);
856 			goto err;
857 		}
858 
859 		/* Save the thread where the base device is opened */
860 		opt->thread = spdk_get_thread();
861 
862 		opt->desc = desc;
863 		opt->bdev = bdev;
864 		TAILQ_INSERT_TAIL(&firm_ctx->desc_head, opt, tqlst);
865 	}
866 
867 	/*
868 	 * find a descriptor associated with our bdev
869 	 */
870 	firm_ctx->desc = NULL;
871 	TAILQ_FOREACH(opt, &firm_ctx->desc_head, tqlst) {
872 		if (opt->bdev == bdev) {
873 			firm_ctx->desc = opt->desc;
874 			break;
875 		}
876 	}
877 
878 	if (!firm_ctx->desc) {
879 		snprintf(msg, sizeof(msg), "No descriptor were found.");
880 		goto err;
881 	}
882 
883 	firm_ctx->ch = spdk_bdev_get_io_channel(firm_ctx->desc);
884 	if (!firm_ctx->ch) {
885 		snprintf(msg, sizeof(msg), "No channels were found.");
886 		goto err;
887 	}
888 
889 	fd = open(firm_ctx->req->filename, O_RDONLY);
890 	if (fd < 0) {
891 		snprintf(msg, sizeof(msg), "open file failed.");
892 		goto err;
893 	}
894 
895 	rc = fstat(fd, &fw_stat);
896 	if (rc < 0) {
897 		close(fd);
898 		snprintf(msg, sizeof(msg), "fstat failed.");
899 		goto err;
900 	}
901 
902 	firm_ctx->size = fw_stat.st_size;
903 	if (fw_stat.st_size % 4) {
904 		close(fd);
905 		snprintf(msg, sizeof(msg), "Firmware image size is not multiple of 4.");
906 		goto err;
907 	}
908 
909 	firm_ctx->fw_image = spdk_zmalloc(firm_ctx->size, 4096, NULL,
910 					  SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA);
911 	if (!firm_ctx->fw_image) {
912 		close(fd);
913 		snprintf(msg, sizeof(msg), "Memory allocation error.");
914 		goto err;
915 	}
916 	firm_ctx->p = firm_ctx->fw_image;
917 
918 	if (read(fd, firm_ctx->p, firm_ctx->size) != ((ssize_t)(firm_ctx->size))) {
919 		close(fd);
920 		snprintf(msg, sizeof(msg), "Read firmware image failed!");
921 		goto err;
922 	}
923 	close(fd);
924 
925 	firm_ctx->offset = 0;
926 	firm_ctx->size_remaining = firm_ctx->size;
927 	firm_ctx->transfer = spdk_min(firm_ctx->size_remaining, 4096);
928 
929 	cmd = malloc(sizeof(struct spdk_nvme_cmd));
930 	if (!cmd) {
931 		snprintf(msg, sizeof(msg), "Memory allocation error.");
932 		goto err;
933 	}
934 	memset(cmd, 0, sizeof(struct spdk_nvme_cmd));
935 	cmd->opc = SPDK_NVME_OPC_FIRMWARE_IMAGE_DOWNLOAD;
936 
937 	cmd->cdw10 = spdk_nvme_bytes_to_numd(firm_ctx->transfer);
938 	cmd->cdw11 = firm_ctx->offset >> 2;
939 
940 	rc = spdk_bdev_nvme_admin_passthru(firm_ctx->desc, firm_ctx->ch, cmd, firm_ctx->p,
941 					   firm_ctx->transfer, apply_firmware_complete, firm_ctx);
942 	if (rc == 0) {
943 		/* normal return here. */
944 		return;
945 	}
946 
947 	free(cmd);
948 	snprintf(msg, sizeof(msg), "Read firmware image failed!");
949 err:
950 	spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, msg);
951 	apply_firmware_cleanup(firm_ctx);
952 }
953 SPDK_RPC_REGISTER("bdev_nvme_apply_firmware", rpc_bdev_nvme_apply_firmware, SPDK_RPC_RUNTIME)
954 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_nvme_apply_firmware, apply_nvme_firmware)
955 
956 struct rpc_bdev_nvme_transport_stat_ctx {
957 	struct spdk_jsonrpc_request *request;
958 	struct spdk_json_write_ctx *w;
959 };
960 
961 static void
962 rpc_bdev_nvme_rdma_stats(struct spdk_json_write_ctx *w,
963 			 struct spdk_nvme_transport_poll_group_stat *stat)
964 {
965 	struct spdk_nvme_rdma_device_stat *device_stats;
966 	uint32_t i;
967 
968 	spdk_json_write_named_array_begin(w, "devices");
969 
970 	for (i = 0; i < stat->rdma.num_devices; i++) {
971 		device_stats = &stat->rdma.device_stats[i];
972 		spdk_json_write_object_begin(w);
973 		spdk_json_write_named_string(w, "dev_name", device_stats->name);
974 		spdk_json_write_named_uint64(w, "polls", device_stats->polls);
975 		spdk_json_write_named_uint64(w, "idle_polls", device_stats->idle_polls);
976 		spdk_json_write_named_uint64(w, "completions", device_stats->completions);
977 		spdk_json_write_named_uint64(w, "queued_requests", device_stats->queued_requests);
978 		spdk_json_write_named_uint64(w, "total_send_wrs", device_stats->total_send_wrs);
979 		spdk_json_write_named_uint64(w, "send_doorbell_updates", device_stats->send_doorbell_updates);
980 		spdk_json_write_named_uint64(w, "total_recv_wrs", device_stats->total_recv_wrs);
981 		spdk_json_write_named_uint64(w, "recv_doorbell_updates", device_stats->recv_doorbell_updates);
982 		spdk_json_write_object_end(w);
983 	}
984 	spdk_json_write_array_end(w);
985 }
986 
987 static void
988 rpc_bdev_nvme_pcie_stats(struct spdk_json_write_ctx *w,
989 			 struct spdk_nvme_transport_poll_group_stat *stat)
990 {
991 	spdk_json_write_named_uint64(w, "polls", stat->pcie.polls);
992 	spdk_json_write_named_uint64(w, "idle_polls", stat->pcie.idle_polls);
993 	spdk_json_write_named_uint64(w, "completions", stat->pcie.completions);
994 	spdk_json_write_named_uint64(w, "cq_doorbell_updates", stat->pcie.cq_doorbell_updates);
995 	spdk_json_write_named_uint64(w, "queued_requests", stat->pcie.queued_requests);
996 	spdk_json_write_named_uint64(w, "submitted_requests", stat->pcie.submitted_requests);
997 	spdk_json_write_named_uint64(w, "sq_doobell_updates", stat->pcie.sq_doobell_updates);
998 }
999 
1000 static void
1001 rpc_bdev_nvme_stats_per_channel(struct spdk_io_channel_iter *i)
1002 {
1003 	struct rpc_bdev_nvme_transport_stat_ctx *ctx;
1004 	struct spdk_io_channel *ch;
1005 	struct nvme_poll_group *group;
1006 	struct spdk_nvme_poll_group_stat *stat;
1007 	struct spdk_nvme_transport_poll_group_stat *tr_stat;
1008 	uint32_t j;
1009 	int rc;
1010 
1011 	ctx = spdk_io_channel_iter_get_ctx(i);
1012 	ch = spdk_io_channel_iter_get_channel(i);
1013 	group = spdk_io_channel_get_ctx(ch);
1014 
1015 	rc = spdk_nvme_poll_group_get_stats(group->group, &stat);
1016 	if (rc) {
1017 		spdk_for_each_channel_continue(i, rc);
1018 		return;
1019 	}
1020 
1021 	spdk_json_write_object_begin(ctx->w);
1022 	spdk_json_write_named_string(ctx->w, "thread", spdk_thread_get_name(spdk_get_thread()));
1023 	spdk_json_write_named_array_begin(ctx->w, "transports");
1024 
1025 	for (j = 0; j < stat->num_transports; j++) {
1026 		tr_stat = stat->transport_stat[j];
1027 		spdk_json_write_object_begin(ctx->w);
1028 		spdk_json_write_named_string(ctx->w, "trname", spdk_nvme_transport_id_trtype_str(tr_stat->trtype));
1029 
1030 		switch (stat->transport_stat[j]->trtype) {
1031 		case SPDK_NVME_TRANSPORT_RDMA:
1032 			rpc_bdev_nvme_rdma_stats(ctx->w, tr_stat);
1033 			break;
1034 		case SPDK_NVME_TRANSPORT_PCIE:
1035 			rpc_bdev_nvme_pcie_stats(ctx->w, tr_stat);
1036 			break;
1037 		default:
1038 			SPDK_WARNLOG("Can't handle trtype %d %s\n", tr_stat->trtype,
1039 				     spdk_nvme_transport_id_trtype_str(tr_stat->trtype));
1040 		}
1041 		spdk_json_write_object_end(ctx->w);
1042 	}
1043 	/* transports array */
1044 	spdk_json_write_array_end(ctx->w);
1045 	spdk_json_write_object_end(ctx->w);
1046 
1047 	spdk_nvme_poll_group_free_stats(group->group, stat);
1048 	spdk_for_each_channel_continue(i, 0);
1049 }
1050 
1051 static void
1052 rpc_bdev_nvme_stats_done(struct spdk_io_channel_iter *i, int status)
1053 {
1054 	struct rpc_bdev_nvme_transport_stat_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
1055 
1056 	spdk_json_write_array_end(ctx->w);
1057 	spdk_json_write_object_end(ctx->w);
1058 	spdk_jsonrpc_end_result(ctx->request, ctx->w);
1059 	free(ctx);
1060 }
1061 
1062 static void
1063 rpc_bdev_nvme_get_transport_statistics(struct spdk_jsonrpc_request *request,
1064 				       const struct spdk_json_val *params)
1065 {
1066 	struct rpc_bdev_nvme_transport_stat_ctx *ctx;
1067 
1068 	if (params) {
1069 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
1070 						 "'bdev_nvme_get_transport_statistics' requires no arguments");
1071 		return;
1072 	}
1073 
1074 	ctx = calloc(1, sizeof(*ctx));
1075 	if (!ctx) {
1076 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1077 						 "Memory allocation error");
1078 		return;
1079 	}
1080 	ctx->request = request;
1081 	ctx->w = spdk_jsonrpc_begin_result(ctx->request);
1082 	spdk_json_write_object_begin(ctx->w);
1083 	spdk_json_write_named_array_begin(ctx->w, "poll_groups");
1084 
1085 	spdk_for_each_channel(&g_nvme_ctrlrs,
1086 			      rpc_bdev_nvme_stats_per_channel,
1087 			      ctx,
1088 			      rpc_bdev_nvme_stats_done);
1089 }
1090 SPDK_RPC_REGISTER("bdev_nvme_get_transport_statistics", rpc_bdev_nvme_get_transport_statistics,
1091 		  SPDK_RPC_RUNTIME)
1092