xref: /spdk/lib/nvmf/ctrlr.c (revision 34cbbf3bf06841b4eea3fcafec3f062ff6cc585a)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation. All rights reserved.
5  *   Copyright (c) 2019, 2020 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 "nvmf_internal.h"
37 #include "transport.h"
38 
39 #include "spdk/bit_array.h"
40 #include "spdk/endian.h"
41 #include "spdk/thread.h"
42 #include "spdk/trace.h"
43 #include "spdk/nvme_spec.h"
44 #include "spdk/nvmf_cmd.h"
45 #include "spdk/string.h"
46 #include "spdk/util.h"
47 #include "spdk/version.h"
48 
49 #include "spdk_internal/log.h"
50 
51 #define MIN_KEEP_ALIVE_TIMEOUT_IN_MS 10000
52 #define NVMF_DISC_KATO_IN_MS 120000
53 #define KAS_TIME_UNIT_IN_MS 100
54 #define KAS_DEFAULT_VALUE (MIN_KEEP_ALIVE_TIMEOUT_IN_MS / KAS_TIME_UNIT_IN_MS)
55 
56 /*
57  * Report the SPDK version as the firmware revision.
58  * SPDK_VERSION_STRING won't fit into FR (only 8 bytes), so try to fit the most important parts.
59  */
60 #define FW_VERSION SPDK_VERSION_MAJOR_STRING SPDK_VERSION_MINOR_STRING SPDK_VERSION_PATCH_STRING
61 
62 /*
63  * Support for custom admin command handlers
64  */
65 struct spdk_nvmf_custom_admin_cmd {
66 	spdk_nvmf_custom_cmd_hdlr hdlr;
67 	uint32_t nsid; /* nsid to forward */
68 };
69 
70 static struct spdk_nvmf_custom_admin_cmd g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_MAX_OPC + 1];
71 
72 static void _nvmf_request_complete(void *ctx);
73 
74 static inline void
75 nvmf_invalid_connect_response(struct spdk_nvmf_fabric_connect_rsp *rsp,
76 			      uint8_t iattr, uint16_t ipo)
77 {
78 	rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
79 	rsp->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM;
80 	rsp->status_code_specific.invalid.iattr = iattr;
81 	rsp->status_code_specific.invalid.ipo = ipo;
82 }
83 
84 #define SPDK_NVMF_INVALID_CONNECT_CMD(rsp, field)	\
85 	nvmf_invalid_connect_response(rsp, 0, offsetof(struct spdk_nvmf_fabric_connect_cmd, field))
86 #define SPDK_NVMF_INVALID_CONNECT_DATA(rsp, field)	\
87 	nvmf_invalid_connect_response(rsp, 1, offsetof(struct spdk_nvmf_fabric_connect_data, field))
88 
89 static void
90 nvmf_ctrlr_stop_keep_alive_timer(struct spdk_nvmf_ctrlr *ctrlr)
91 {
92 	if (!ctrlr) {
93 		SPDK_ERRLOG("Controller is NULL\n");
94 		return;
95 	}
96 
97 	if (ctrlr->keep_alive_poller == NULL) {
98 		return;
99 	}
100 
101 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Stop keep alive poller\n");
102 	spdk_poller_unregister(&ctrlr->keep_alive_poller);
103 }
104 
105 static void
106 nvmf_ctrlr_stop_association_timer(struct spdk_nvmf_ctrlr *ctrlr)
107 {
108 	if (!ctrlr) {
109 		SPDK_ERRLOG("Controller is NULL\n");
110 		assert(false);
111 		return;
112 	}
113 
114 	if (ctrlr->association_timer == NULL) {
115 		return;
116 	}
117 
118 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Stop association timer\n");
119 	spdk_poller_unregister(&ctrlr->association_timer);
120 }
121 
122 static void
123 nvmf_ctrlr_disconnect_qpairs_done(struct spdk_io_channel_iter *i, int status)
124 {
125 	if (status == 0) {
126 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ctrlr disconnect qpairs complete successfully\n");
127 	} else {
128 		SPDK_ERRLOG("Fail to disconnect ctrlr qpairs\n");
129 	}
130 }
131 
132 static int
133 _nvmf_ctrlr_disconnect_qpairs_on_pg(struct spdk_io_channel_iter *i, bool include_admin)
134 {
135 	int rc = 0;
136 	struct spdk_nvmf_ctrlr *ctrlr;
137 	struct spdk_nvmf_qpair *qpair, *temp_qpair;
138 	struct spdk_io_channel *ch;
139 	struct spdk_nvmf_poll_group *group;
140 
141 	ctrlr = spdk_io_channel_iter_get_ctx(i);
142 	ch = spdk_io_channel_iter_get_channel(i);
143 	group = spdk_io_channel_get_ctx(ch);
144 
145 	TAILQ_FOREACH_SAFE(qpair, &group->qpairs, link, temp_qpair) {
146 		if (qpair->ctrlr == ctrlr && (include_admin || !nvmf_qpair_is_admin_queue(qpair))) {
147 			rc = spdk_nvmf_qpair_disconnect(qpair, NULL, NULL);
148 			if (rc) {
149 				SPDK_ERRLOG("Qpair disconnect failed\n");
150 				return rc;
151 			}
152 		}
153 	}
154 
155 	return rc;
156 }
157 
158 static void
159 nvmf_ctrlr_disconnect_qpairs_on_pg(struct spdk_io_channel_iter *i)
160 {
161 	spdk_for_each_channel_continue(i, _nvmf_ctrlr_disconnect_qpairs_on_pg(i, true));
162 }
163 
164 static void
165 nvmf_ctrlr_disconnect_io_qpairs_on_pg(struct spdk_io_channel_iter *i)
166 {
167 	spdk_for_each_channel_continue(i, _nvmf_ctrlr_disconnect_qpairs_on_pg(i, false));
168 }
169 
170 static int
171 nvmf_ctrlr_keep_alive_poll(void *ctx)
172 {
173 	uint64_t keep_alive_timeout_tick;
174 	uint64_t now = spdk_get_ticks();
175 	struct spdk_nvmf_ctrlr *ctrlr = ctx;
176 
177 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Polling ctrlr keep alive timeout\n");
178 
179 	/* If the Keep alive feature is in use and the timer expires */
180 	keep_alive_timeout_tick = ctrlr->last_keep_alive_tick +
181 				  ctrlr->feat.keep_alive_timer.bits.kato * spdk_get_ticks_hz() / UINT64_C(1000);
182 	if (now > keep_alive_timeout_tick) {
183 		SPDK_NOTICELOG("Disconnecting host from subsystem %s due to keep alive timeout.\n",
184 			       ctrlr->subsys->subnqn);
185 		/* set the Controller Fatal Status bit to '1' */
186 		if (ctrlr->vcprop.csts.bits.cfs == 0) {
187 			ctrlr->vcprop.csts.bits.cfs = 1;
188 
189 			/*
190 			 * disconnect qpairs, terminate Transport connection
191 			 * destroy ctrlr, break the host to controller association
192 			 * disconnect qpairs with qpair->ctrlr == ctrlr
193 			 */
194 			spdk_for_each_channel(ctrlr->subsys->tgt,
195 					      nvmf_ctrlr_disconnect_qpairs_on_pg,
196 					      ctrlr,
197 					      nvmf_ctrlr_disconnect_qpairs_done);
198 		}
199 	}
200 
201 	return SPDK_POLLER_BUSY;
202 }
203 
204 static void
205 nvmf_ctrlr_start_keep_alive_timer(struct spdk_nvmf_ctrlr *ctrlr)
206 {
207 	if (!ctrlr) {
208 		SPDK_ERRLOG("Controller is NULL\n");
209 		return;
210 	}
211 
212 	/* if cleared to 0 then the Keep Alive Timer is disabled */
213 	if (ctrlr->feat.keep_alive_timer.bits.kato != 0) {
214 
215 		ctrlr->last_keep_alive_tick = spdk_get_ticks();
216 
217 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Ctrlr add keep alive poller\n");
218 		ctrlr->keep_alive_poller = SPDK_POLLER_REGISTER(nvmf_ctrlr_keep_alive_poll, ctrlr,
219 					   ctrlr->feat.keep_alive_timer.bits.kato * 1000);
220 	}
221 }
222 
223 static void
224 ctrlr_add_qpair_and_update_rsp(struct spdk_nvmf_qpair *qpair,
225 			       struct spdk_nvmf_ctrlr *ctrlr,
226 			       struct spdk_nvmf_fabric_connect_rsp *rsp)
227 {
228 	assert(ctrlr->admin_qpair->group->thread == spdk_get_thread());
229 
230 	/* check if we would exceed ctrlr connection limit */
231 	if (qpair->qid >= spdk_bit_array_capacity(ctrlr->qpair_mask)) {
232 		SPDK_ERRLOG("Requested QID %u but Max QID is %u\n",
233 			    qpair->qid, spdk_bit_array_capacity(ctrlr->qpair_mask) - 1);
234 		rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
235 		rsp->status.sc = SPDK_NVME_SC_INVALID_QUEUE_IDENTIFIER;
236 		return;
237 	}
238 
239 	if (spdk_bit_array_get(ctrlr->qpair_mask, qpair->qid)) {
240 		SPDK_ERRLOG("Got I/O connect with duplicate QID %u\n", qpair->qid);
241 		rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
242 		rsp->status.sc = SPDK_NVME_SC_INVALID_QUEUE_IDENTIFIER;
243 		return;
244 	}
245 
246 	qpair->ctrlr = ctrlr;
247 	spdk_bit_array_set(ctrlr->qpair_mask, qpair->qid);
248 
249 	rsp->status.sc = SPDK_NVME_SC_SUCCESS;
250 	rsp->status_code_specific.success.cntlid = ctrlr->cntlid;
251 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "connect capsule response: cntlid = 0x%04x\n",
252 		      rsp->status_code_specific.success.cntlid);
253 }
254 
255 static void
256 _nvmf_ctrlr_add_admin_qpair(void *ctx)
257 {
258 	struct spdk_nvmf_request *req = ctx;
259 	struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp;
260 	struct spdk_nvmf_qpair *qpair = req->qpair;
261 	struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr;
262 
263 	ctrlr->admin_qpair = qpair;
264 	nvmf_ctrlr_start_keep_alive_timer(ctrlr);
265 	ctrlr_add_qpair_and_update_rsp(qpair, ctrlr, rsp);
266 	_nvmf_request_complete(req);
267 }
268 
269 static void
270 _nvmf_subsystem_add_ctrlr(void *ctx)
271 {
272 	struct spdk_nvmf_request *req = ctx;
273 	struct spdk_nvmf_qpair *qpair = req->qpair;
274 	struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp;
275 	struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr;
276 
277 	if (nvmf_subsystem_add_ctrlr(ctrlr->subsys, ctrlr)) {
278 		SPDK_ERRLOG("Unable to add controller to subsystem\n");
279 		spdk_bit_array_free(&ctrlr->qpair_mask);
280 		free(ctrlr);
281 		qpair->ctrlr = NULL;
282 		rsp->status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
283 		spdk_nvmf_request_complete(req);
284 		return;
285 	}
286 
287 	spdk_thread_send_msg(ctrlr->thread, _nvmf_ctrlr_add_admin_qpair, req);
288 }
289 
290 static void
291 nvmf_ctrlr_cdata_init(struct spdk_nvmf_transport *transport, struct spdk_nvmf_subsystem *subsystem,
292 		      struct spdk_nvmf_ctrlr_data *cdata)
293 {
294 	cdata->kas = KAS_DEFAULT_VALUE;
295 	cdata->sgls.supported = 1;
296 	cdata->sgls.keyed_sgl = 1;
297 	cdata->sgls.sgl_offset = 1;
298 	cdata->nvmf_specific.ioccsz = sizeof(struct spdk_nvme_cmd) / 16;
299 	cdata->nvmf_specific.ioccsz += transport->opts.in_capsule_data_size / 16;
300 	cdata->nvmf_specific.iorcsz = sizeof(struct spdk_nvme_cpl) / 16;
301 	cdata->nvmf_specific.icdoff = 0; /* offset starts directly after SQE */
302 	cdata->nvmf_specific.ctrattr.ctrlr_model = SPDK_NVMF_CTRLR_MODEL_DYNAMIC;
303 	cdata->nvmf_specific.msdbd = 1;
304 
305 	if (transport->ops->cdata_init) {
306 		transport->ops->cdata_init(transport, subsystem, cdata);
307 	}
308 }
309 
310 static struct spdk_nvmf_ctrlr *
311 nvmf_ctrlr_create(struct spdk_nvmf_subsystem *subsystem,
312 		  struct spdk_nvmf_request *req,
313 		  struct spdk_nvmf_fabric_connect_cmd *connect_cmd,
314 		  struct spdk_nvmf_fabric_connect_data *connect_data)
315 {
316 	struct spdk_nvmf_ctrlr	*ctrlr;
317 	struct spdk_nvmf_transport *transport;
318 
319 	ctrlr = calloc(1, sizeof(*ctrlr));
320 	if (ctrlr == NULL) {
321 		SPDK_ERRLOG("Memory allocation failed\n");
322 		return NULL;
323 	}
324 
325 	TAILQ_INIT(&ctrlr->log_head);
326 	ctrlr->subsys = subsystem;
327 	ctrlr->thread = req->qpair->group->thread;
328 
329 	transport = req->qpair->transport;
330 	ctrlr->qpair_mask = spdk_bit_array_create(transport->opts.max_qpairs_per_ctrlr);
331 	if (!ctrlr->qpair_mask) {
332 		SPDK_ERRLOG("Failed to allocate controller qpair mask\n");
333 		free(ctrlr);
334 		return NULL;
335 	}
336 
337 	nvmf_ctrlr_cdata_init(transport, subsystem, &ctrlr->cdata);
338 
339 	/*
340 	 * KAS: This field indicates the granularity of the Keep Alive Timer in 100ms units.
341 	 * If this field is cleared to 0h, then Keep Alive is not supported.
342 	 */
343 	if (ctrlr->cdata.kas) {
344 		ctrlr->feat.keep_alive_timer.bits.kato = spdk_divide_round_up(connect_cmd->kato,
345 				KAS_DEFAULT_VALUE * KAS_TIME_UNIT_IN_MS) *
346 				KAS_DEFAULT_VALUE * KAS_TIME_UNIT_IN_MS;
347 	}
348 
349 	ctrlr->feat.async_event_configuration.bits.ns_attr_notice = 1;
350 	ctrlr->feat.volatile_write_cache.bits.wce = 1;
351 
352 	if (ctrlr->subsys->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) {
353 		/*
354 		 * If keep-alive timeout is not set, discovery controllers use some
355 		 * arbitrary high value in order to cleanup stale discovery sessions
356 		 *
357 		 * From the 1.0a nvme-of spec:
358 		 * "The Keep Alive command is reserved for
359 		 * Discovery controllers. A transport may specify a
360 		 * fixed Discovery controller activity timeout value
361 		 * (e.g., 2 minutes).  If no commands are received
362 		 * by a Discovery controller within that time
363 		 * period, the controller may perform the
364 		 * actions for Keep Alive Timer expiration".
365 		 * kato is in millisecond.
366 		 */
367 		if (ctrlr->feat.keep_alive_timer.bits.kato == 0) {
368 			ctrlr->feat.keep_alive_timer.bits.kato = NVMF_DISC_KATO_IN_MS;
369 		}
370 	}
371 
372 	/* Subtract 1 for admin queue, 1 for 0's based */
373 	ctrlr->feat.number_of_queues.bits.ncqr = transport->opts.max_qpairs_per_ctrlr - 1 -
374 			1;
375 	ctrlr->feat.number_of_queues.bits.nsqr = transport->opts.max_qpairs_per_ctrlr - 1 -
376 			1;
377 
378 	spdk_uuid_copy(&ctrlr->hostid, (struct spdk_uuid *)connect_data->hostid);
379 	memcpy(ctrlr->hostnqn, connect_data->hostnqn, sizeof(ctrlr->hostnqn));
380 
381 	ctrlr->vcprop.cap.raw = 0;
382 	ctrlr->vcprop.cap.bits.cqr = 1; /* NVMe-oF specification required */
383 	ctrlr->vcprop.cap.bits.mqes = transport->opts.max_queue_depth -
384 				      1; /* max queue depth */
385 	ctrlr->vcprop.cap.bits.ams = 0; /* optional arb mechanisms */
386 	ctrlr->vcprop.cap.bits.to = 1; /* ready timeout - 500 msec units */
387 	ctrlr->vcprop.cap.bits.dstrd = 0; /* fixed to 0 for NVMe-oF */
388 	ctrlr->vcprop.cap.bits.css = SPDK_NVME_CAP_CSS_NVM; /* NVM command set */
389 	ctrlr->vcprop.cap.bits.mpsmin = 0; /* 2 ^ (12 + mpsmin) == 4k */
390 	ctrlr->vcprop.cap.bits.mpsmax = 0; /* 2 ^ (12 + mpsmax) == 4k */
391 
392 	/* Version Supported: 1.3 */
393 	ctrlr->vcprop.vs.bits.mjr = 1;
394 	ctrlr->vcprop.vs.bits.mnr = 3;
395 	ctrlr->vcprop.vs.bits.ter = 0;
396 
397 	ctrlr->vcprop.cc.raw = 0;
398 	ctrlr->vcprop.cc.bits.en = 0; /* Init controller disabled */
399 
400 	ctrlr->vcprop.csts.raw = 0;
401 	ctrlr->vcprop.csts.bits.rdy = 0; /* Init controller as not ready */
402 
403 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "cap 0x%" PRIx64 "\n", ctrlr->vcprop.cap.raw);
404 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "vs 0x%x\n", ctrlr->vcprop.vs.raw);
405 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "cc 0x%x\n", ctrlr->vcprop.cc.raw);
406 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "csts 0x%x\n", ctrlr->vcprop.csts.raw);
407 
408 	ctrlr->dif_insert_or_strip = transport->opts.dif_insert_or_strip;
409 
410 	req->qpair->ctrlr = ctrlr;
411 	spdk_thread_send_msg(subsystem->thread, _nvmf_subsystem_add_ctrlr, req);
412 
413 	return ctrlr;
414 }
415 
416 static void
417 _nvmf_ctrlr_destruct(void *ctx)
418 {
419 	struct spdk_nvmf_ctrlr *ctrlr = ctx;
420 	struct spdk_nvmf_reservation_log *log, *log_tmp;
421 
422 	nvmf_ctrlr_stop_keep_alive_timer(ctrlr);
423 	nvmf_ctrlr_stop_association_timer(ctrlr);
424 	spdk_bit_array_free(&ctrlr->qpair_mask);
425 
426 	TAILQ_FOREACH_SAFE(log, &ctrlr->log_head, link, log_tmp) {
427 		TAILQ_REMOVE(&ctrlr->log_head, log, link);
428 		free(log);
429 	}
430 	free(ctrlr);
431 }
432 
433 void
434 nvmf_ctrlr_destruct(struct spdk_nvmf_ctrlr *ctrlr)
435 {
436 	nvmf_subsystem_remove_ctrlr(ctrlr->subsys, ctrlr);
437 
438 	spdk_thread_send_msg(ctrlr->thread, _nvmf_ctrlr_destruct, ctrlr);
439 }
440 
441 static void
442 nvmf_ctrlr_add_io_qpair(void *ctx)
443 {
444 	struct spdk_nvmf_request *req = ctx;
445 	struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp;
446 	struct spdk_nvmf_qpair *qpair = req->qpair;
447 	struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr;
448 
449 	/* Unit test will check qpair->ctrlr after calling spdk_nvmf_ctrlr_connect.
450 	  * For error case, the value should be NULL. So set it to NULL at first.
451 	  */
452 	qpair->ctrlr = NULL;
453 
454 	/* Make sure the controller is not being destroyed. */
455 	if (ctrlr->in_destruct) {
456 		SPDK_ERRLOG("Got I/O connect while ctrlr was being destroyed.\n");
457 		SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid);
458 		goto end;
459 	}
460 
461 	if (ctrlr->subsys->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) {
462 		SPDK_ERRLOG("I/O connect not allowed on discovery controller\n");
463 		SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid);
464 		goto end;
465 	}
466 
467 	if (!ctrlr->vcprop.cc.bits.en) {
468 		SPDK_ERRLOG("Got I/O connect before ctrlr was enabled\n");
469 		SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid);
470 		goto end;
471 	}
472 
473 	if (1u << ctrlr->vcprop.cc.bits.iosqes != sizeof(struct spdk_nvme_cmd)) {
474 		SPDK_ERRLOG("Got I/O connect with invalid IOSQES %u\n",
475 			    ctrlr->vcprop.cc.bits.iosqes);
476 		SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid);
477 		goto end;
478 	}
479 
480 	if (1u << ctrlr->vcprop.cc.bits.iocqes != sizeof(struct spdk_nvme_cpl)) {
481 		SPDK_ERRLOG("Got I/O connect with invalid IOCQES %u\n",
482 			    ctrlr->vcprop.cc.bits.iocqes);
483 		SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid);
484 		goto end;
485 	}
486 
487 	ctrlr_add_qpair_and_update_rsp(qpair, ctrlr, rsp);
488 end:
489 	spdk_nvmf_request_complete(req);
490 }
491 
492 static void
493 _nvmf_ctrlr_add_io_qpair(void *ctx)
494 {
495 	struct spdk_nvmf_request *req = ctx;
496 	struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp;
497 	struct spdk_nvmf_fabric_connect_data *data = req->data;
498 	struct spdk_nvmf_ctrlr *ctrlr;
499 	struct spdk_nvmf_qpair *qpair = req->qpair;
500 	struct spdk_nvmf_qpair *admin_qpair;
501 	struct spdk_nvmf_tgt *tgt = qpair->transport->tgt;
502 	struct spdk_nvmf_subsystem *subsystem;
503 
504 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Connect I/O Queue for controller id 0x%x\n", data->cntlid);
505 
506 	subsystem = spdk_nvmf_tgt_find_subsystem(tgt, data->subnqn);
507 	/* We already checked this in spdk_nvmf_ctrlr_connect */
508 	assert(subsystem != NULL);
509 
510 	ctrlr = nvmf_subsystem_get_ctrlr(subsystem, data->cntlid);
511 	if (ctrlr == NULL) {
512 		SPDK_ERRLOG("Unknown controller ID 0x%x\n", data->cntlid);
513 		SPDK_NVMF_INVALID_CONNECT_DATA(rsp, cntlid);
514 		spdk_nvmf_request_complete(req);
515 		return;
516 	}
517 
518 	/* fail before passing a message to the controller thread. */
519 	if (ctrlr->in_destruct) {
520 		SPDK_ERRLOG("Got I/O connect while ctrlr was being destroyed.\n");
521 		SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid);
522 		spdk_nvmf_request_complete(req);
523 		return;
524 	}
525 
526 	admin_qpair = ctrlr->admin_qpair;
527 	qpair->ctrlr = ctrlr;
528 	spdk_thread_send_msg(admin_qpair->group->thread, nvmf_ctrlr_add_io_qpair, req);
529 }
530 
531 static bool
532 nvmf_qpair_access_allowed(struct spdk_nvmf_qpair *qpair, struct spdk_nvmf_subsystem *subsystem,
533 			  const char *hostnqn)
534 {
535 	struct spdk_nvme_transport_id listen_trid = {};
536 
537 	if (!spdk_nvmf_subsystem_host_allowed(subsystem, hostnqn)) {
538 		SPDK_ERRLOG("Subsystem '%s' does not allow host '%s'\n", subsystem->subnqn, hostnqn);
539 		return false;
540 	}
541 
542 	if (spdk_nvmf_qpair_get_listen_trid(qpair, &listen_trid)) {
543 		SPDK_ERRLOG("Subsystem '%s' is unable to enforce access control due to an internal error.\n",
544 			    subsystem->subnqn);
545 		return false;
546 	}
547 
548 	if (!spdk_nvmf_subsystem_listener_allowed(subsystem, &listen_trid)) {
549 		SPDK_ERRLOG("Subsystem '%s' does not allow host '%s' to connect at this address.\n",
550 			    subsystem->subnqn, hostnqn);
551 		return false;
552 	}
553 
554 	return true;
555 }
556 
557 static int
558 _nvmf_ctrlr_connect(struct spdk_nvmf_request *req)
559 {
560 	struct spdk_nvmf_fabric_connect_data *data = req->data;
561 	struct spdk_nvmf_fabric_connect_cmd *cmd = &req->cmd->connect_cmd;
562 	struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp;
563 	struct spdk_nvmf_qpair *qpair = req->qpair;
564 	struct spdk_nvmf_transport *transport = qpair->transport;
565 	struct spdk_nvmf_ctrlr *ctrlr;
566 	struct spdk_nvmf_subsystem *subsystem;
567 
568 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "recfmt 0x%x qid %u sqsize %u\n",
569 		      cmd->recfmt, cmd->qid, cmd->sqsize);
570 
571 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Connect data:\n");
572 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "  cntlid:  0x%04x\n", data->cntlid);
573 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "  hostid: %08x-%04x-%04x-%02x%02x-%04x%08x ***\n",
574 		      ntohl(*(uint32_t *)&data->hostid[0]),
575 		      ntohs(*(uint16_t *)&data->hostid[4]),
576 		      ntohs(*(uint16_t *)&data->hostid[6]),
577 		      data->hostid[8],
578 		      data->hostid[9],
579 		      ntohs(*(uint16_t *)&data->hostid[10]),
580 		      ntohl(*(uint32_t *)&data->hostid[12]));
581 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "  subnqn: \"%s\"\n", data->subnqn);
582 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "  hostnqn: \"%s\"\n", data->hostnqn);
583 
584 	subsystem = spdk_nvmf_tgt_find_subsystem(transport->tgt, data->subnqn);
585 	if (!subsystem) {
586 		SPDK_NVMF_INVALID_CONNECT_DATA(rsp, subnqn);
587 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
588 	}
589 
590 	if (cmd->recfmt != 0) {
591 		SPDK_ERRLOG("Connect command unsupported RECFMT %u\n", cmd->recfmt);
592 		rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
593 		rsp->status.sc = SPDK_NVMF_FABRIC_SC_INCOMPATIBLE_FORMAT;
594 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
595 	}
596 
597 	/*
598 	 * SQSIZE is a 0-based value, so it must be at least 1 (minimum queue depth is 2) and
599 	 * strictly less than max_aq_depth (admin queues) or max_queue_depth (io queues).
600 	 */
601 	if (cmd->sqsize == 0) {
602 		SPDK_ERRLOG("Invalid SQSIZE = 0\n");
603 		SPDK_NVMF_INVALID_CONNECT_CMD(rsp, sqsize);
604 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
605 	}
606 
607 	if (cmd->qid == 0) {
608 		if (cmd->sqsize >= transport->opts.max_aq_depth) {
609 			SPDK_ERRLOG("Invalid SQSIZE for admin queue %u (min 1, max %u)\n",
610 				    cmd->sqsize, transport->opts.max_aq_depth - 1);
611 			SPDK_NVMF_INVALID_CONNECT_CMD(rsp, sqsize);
612 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
613 		}
614 	} else if (cmd->sqsize >= transport->opts.max_queue_depth) {
615 		SPDK_ERRLOG("Invalid SQSIZE %u (min 1, max %u)\n",
616 			    cmd->sqsize, transport->opts.max_queue_depth - 1);
617 		SPDK_NVMF_INVALID_CONNECT_CMD(rsp, sqsize);
618 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
619 	}
620 
621 	qpair->sq_head_max = cmd->sqsize;
622 	qpair->qid = cmd->qid;
623 
624 	if (0 == qpair->qid) {
625 		qpair->group->stat.admin_qpairs++;
626 	} else {
627 		qpair->group->stat.io_qpairs++;
628 	}
629 
630 	if (cmd->qid == 0) {
631 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Connect Admin Queue for controller ID 0x%x\n", data->cntlid);
632 
633 		if (data->cntlid != 0xFFFF) {
634 			/* This NVMf target only supports dynamic mode. */
635 			SPDK_ERRLOG("The NVMf target only supports dynamic mode (CNTLID = 0x%x).\n", data->cntlid);
636 			SPDK_NVMF_INVALID_CONNECT_DATA(rsp, cntlid);
637 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
638 		}
639 
640 		/* Establish a new ctrlr */
641 		ctrlr = nvmf_ctrlr_create(subsystem, req, cmd, data);
642 		if (!ctrlr) {
643 			SPDK_ERRLOG("nvmf_ctrlr_create() failed\n");
644 			rsp->status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
645 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
646 		} else {
647 			return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
648 		}
649 	} else {
650 		spdk_thread_send_msg(subsystem->thread, _nvmf_ctrlr_add_io_qpair, req);
651 		return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
652 	}
653 }
654 
655 static inline bool
656 nvmf_request_is_fabric_connect(struct spdk_nvmf_request *req)
657 {
658 	return req->cmd->nvmf_cmd.opcode == SPDK_NVME_OPC_FABRIC &&
659 	       req->cmd->nvmf_cmd.fctype == SPDK_NVMF_FABRIC_COMMAND_CONNECT;
660 }
661 
662 static struct spdk_nvmf_subsystem_poll_group *
663 nvmf_subsystem_pg_from_connect_cmd(struct spdk_nvmf_request *req)
664 {
665 	struct spdk_nvmf_fabric_connect_data *data;
666 	struct spdk_nvmf_subsystem *subsystem;
667 	struct spdk_nvmf_tgt *tgt;
668 
669 	assert(nvmf_request_is_fabric_connect(req));
670 	assert(req->qpair->ctrlr == NULL);
671 
672 	data = req->data;
673 	tgt = req->qpair->transport->tgt;
674 
675 	subsystem = spdk_nvmf_tgt_find_subsystem(tgt, data->subnqn);
676 	if (subsystem == NULL) {
677 		return NULL;
678 	}
679 
680 	return &req->qpair->group->sgroups[subsystem->id];
681 }
682 
683 int
684 spdk_nvmf_ctrlr_connect(struct spdk_nvmf_request *req)
685 {
686 	struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp;
687 	struct spdk_nvmf_qpair *qpair = req->qpair;
688 	struct spdk_nvmf_subsystem_poll_group *sgroup;
689 	enum spdk_nvmf_request_exec_status status;
690 
691 	sgroup = nvmf_subsystem_pg_from_connect_cmd(req);
692 	if (!sgroup) {
693 		SPDK_NVMF_INVALID_CONNECT_DATA(rsp, subnqn);
694 		status = SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
695 		goto out;
696 	}
697 
698 	sgroup->io_outstanding++;
699 	TAILQ_INSERT_TAIL(&qpair->outstanding, req, link);
700 
701 	status = _nvmf_ctrlr_connect(req);
702 
703 out:
704 	if (status == SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE) {
705 		_nvmf_request_complete(req);
706 	}
707 
708 	return status;
709 }
710 
711 static int
712 nvmf_ctrlr_cmd_connect(struct spdk_nvmf_request *req)
713 {
714 	struct spdk_nvmf_fabric_connect_data *data = req->data;
715 	struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp;
716 	struct spdk_nvmf_transport *transport = req->qpair->transport;
717 	struct spdk_nvmf_subsystem *subsystem;
718 
719 	if (req->length < sizeof(struct spdk_nvmf_fabric_connect_data)) {
720 		SPDK_ERRLOG("Connect command data length 0x%x too small\n", req->length);
721 		rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
722 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
723 	}
724 
725 	subsystem = spdk_nvmf_tgt_find_subsystem(transport->tgt, data->subnqn);
726 	if (!subsystem) {
727 		SPDK_NVMF_INVALID_CONNECT_DATA(rsp, subnqn);
728 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
729 	}
730 
731 	if ((subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE) ||
732 	    (subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSING) ||
733 	    (subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED) ||
734 	    (subsystem->state == SPDK_NVMF_SUBSYSTEM_DEACTIVATING)) {
735 		SPDK_ERRLOG("Subsystem '%s' is not ready\n", subsystem->subnqn);
736 		rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
737 		rsp->status.sc = SPDK_NVMF_FABRIC_SC_CONTROLLER_BUSY;
738 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
739 	}
740 
741 	/* Ensure that hostnqn is null terminated */
742 	if (!memchr(data->hostnqn, '\0', SPDK_NVMF_NQN_MAX_LEN + 1)) {
743 		SPDK_ERRLOG("Connect HOSTNQN is not null terminated\n");
744 		SPDK_NVMF_INVALID_CONNECT_DATA(rsp, hostnqn);
745 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
746 	}
747 
748 	if (!nvmf_qpair_access_allowed(req->qpair, subsystem, data->hostnqn)) {
749 		rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
750 		rsp->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_HOST;
751 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
752 	}
753 
754 	return _nvmf_ctrlr_connect(req);
755 }
756 
757 static int
758 nvmf_ctrlr_association_remove(void *ctx)
759 {
760 	struct spdk_nvmf_ctrlr *ctrlr = ctx;
761 	int rc;
762 
763 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Disconnecting host from subsystem %s due to association timeout.\n",
764 		      ctrlr->subsys->subnqn);
765 
766 	rc = spdk_nvmf_qpair_disconnect(ctrlr->admin_qpair, NULL, NULL);
767 	if (rc < 0) {
768 		SPDK_ERRLOG("Fail to disconnect admin ctrlr qpair\n");
769 		assert(false);
770 	}
771 
772 	nvmf_ctrlr_stop_association_timer(ctrlr);
773 	return 1;
774 }
775 
776 static void
777 nvmf_ctrlr_cc_shn_done(struct spdk_io_channel_iter *i, int status)
778 {
779 	struct spdk_nvmf_ctrlr *ctrlr = spdk_io_channel_iter_get_ctx(i);
780 
781 	if (status < 0) {
782 		SPDK_ERRLOG("Fail to disconnect io ctrlr qpairs\n");
783 		assert(false);
784 	}
785 
786 	ctrlr->vcprop.csts.bits.shst = SPDK_NVME_SHST_COMPLETE;
787 
788 	/* After CC.EN transitions to 0 (due to shutdown or reset), the association
789 	 * between the host and controller shall be preserved for at least 2 minutes */
790 	ctrlr->association_timer = SPDK_POLLER_REGISTER(nvmf_ctrlr_association_remove, ctrlr,
791 				   ctrlr->admin_qpair->transport->opts.association_timeout * 1000);
792 }
793 
794 static void
795 nvmf_ctrlr_cc_reset_done(struct spdk_io_channel_iter *i, int status)
796 {
797 	struct spdk_nvmf_ctrlr *ctrlr = spdk_io_channel_iter_get_ctx(i);
798 
799 	if (status < 0) {
800 		SPDK_ERRLOG("Fail to disconnect io ctrlr qpairs\n");
801 		assert(false);
802 	}
803 
804 	/* Only a subset of the registers are cleared out on a reset */
805 	ctrlr->vcprop.cc.raw = 0;
806 	ctrlr->vcprop.csts.raw = 0;
807 
808 	/* After CC.EN transitions to 0 (due to shutdown or reset), the association
809 	 * between the host and controller shall be preserved for at least 2 minutes */
810 	ctrlr->association_timer = SPDK_POLLER_REGISTER(nvmf_ctrlr_association_remove, ctrlr,
811 				   ctrlr->admin_qpair->transport->opts.association_timeout * 1000);
812 }
813 
814 const struct spdk_nvmf_registers *
815 spdk_nvmf_ctrlr_get_regs(struct spdk_nvmf_ctrlr *ctrlr)
816 {
817 	return &ctrlr->vcprop;
818 }
819 
820 static uint64_t
821 nvmf_prop_get_cap(struct spdk_nvmf_ctrlr *ctrlr)
822 {
823 	return ctrlr->vcprop.cap.raw;
824 }
825 
826 static uint64_t
827 nvmf_prop_get_vs(struct spdk_nvmf_ctrlr *ctrlr)
828 {
829 	return ctrlr->vcprop.vs.raw;
830 }
831 
832 static uint64_t
833 nvmf_prop_get_cc(struct spdk_nvmf_ctrlr *ctrlr)
834 {
835 	return ctrlr->vcprop.cc.raw;
836 }
837 
838 static bool
839 nvmf_prop_set_cc(struct spdk_nvmf_ctrlr *ctrlr, uint32_t value)
840 {
841 	union spdk_nvme_cc_register cc, diff;
842 
843 	cc.raw = value;
844 
845 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "cur CC: 0x%08x\n", ctrlr->vcprop.cc.raw);
846 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "new CC: 0x%08x\n", cc.raw);
847 
848 	/*
849 	 * Calculate which bits changed between the current and new CC.
850 	 * Mark each bit as 0 once it is handled to determine if any unhandled bits were changed.
851 	 */
852 	diff.raw = cc.raw ^ ctrlr->vcprop.cc.raw;
853 
854 	if (diff.bits.en) {
855 		if (cc.bits.en) {
856 			SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Property Set CC Enable!\n");
857 			nvmf_ctrlr_stop_association_timer(ctrlr);
858 
859 			ctrlr->vcprop.cc.bits.en = 1;
860 			ctrlr->vcprop.csts.bits.rdy = 1;
861 		} else {
862 			SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Property Set CC Disable!\n");
863 			ctrlr->vcprop.cc.bits.en = 0;
864 			spdk_for_each_channel(ctrlr->subsys->tgt,
865 					      nvmf_ctrlr_disconnect_io_qpairs_on_pg,
866 					      ctrlr,
867 					      nvmf_ctrlr_cc_reset_done);
868 		}
869 		diff.bits.en = 0;
870 	}
871 
872 	if (diff.bits.shn) {
873 		if (cc.bits.shn == SPDK_NVME_SHN_NORMAL ||
874 		    cc.bits.shn == SPDK_NVME_SHN_ABRUPT) {
875 			SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Property Set CC Shutdown %u%ub!\n",
876 				      cc.bits.shn >> 1, cc.bits.shn & 1);
877 			ctrlr->vcprop.cc.bits.shn = cc.bits.shn;
878 			spdk_for_each_channel(ctrlr->subsys->tgt,
879 					      nvmf_ctrlr_disconnect_io_qpairs_on_pg,
880 					      ctrlr,
881 					      nvmf_ctrlr_cc_shn_done);
882 
883 			/* From the time a shutdown is initiated the controller shall disable
884 			 * Keep Alive timer */
885 			nvmf_ctrlr_stop_keep_alive_timer(ctrlr);
886 		} else if (cc.bits.shn == 0) {
887 			ctrlr->vcprop.cc.bits.shn = 0;
888 		} else {
889 			SPDK_ERRLOG("Prop Set CC: Invalid SHN value %u%ub\n",
890 				    cc.bits.shn >> 1, cc.bits.shn & 1);
891 			return false;
892 		}
893 		diff.bits.shn = 0;
894 	}
895 
896 	if (diff.bits.iosqes) {
897 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Prop Set IOSQES = %u (%u bytes)\n",
898 			      cc.bits.iosqes, 1u << cc.bits.iosqes);
899 		ctrlr->vcprop.cc.bits.iosqes = cc.bits.iosqes;
900 		diff.bits.iosqes = 0;
901 	}
902 
903 	if (diff.bits.iocqes) {
904 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Prop Set IOCQES = %u (%u bytes)\n",
905 			      cc.bits.iocqes, 1u << cc.bits.iocqes);
906 		ctrlr->vcprop.cc.bits.iocqes = cc.bits.iocqes;
907 		diff.bits.iocqes = 0;
908 	}
909 
910 	if (diff.bits.ams) {
911 		SPDK_ERRLOG("Arbitration Mechanism Selected (AMS) 0x%x not supported!\n", cc.bits.ams);
912 		return false;
913 	}
914 
915 	if (diff.bits.mps) {
916 		SPDK_ERRLOG("Memory Page Size (MPS) %u KiB not supported!\n", (1 << (2 + cc.bits.mps)));
917 		return false;
918 	}
919 
920 	if (diff.bits.css) {
921 		SPDK_ERRLOG("I/O Command Set Selected (CSS) 0x%x not supported!\n", cc.bits.css);
922 		return false;
923 	}
924 
925 	if (diff.raw != 0) {
926 		SPDK_ERRLOG("Prop Set CC toggled reserved bits 0x%x!\n", diff.raw);
927 		return false;
928 	}
929 
930 	return true;
931 }
932 
933 static uint64_t
934 nvmf_prop_get_csts(struct spdk_nvmf_ctrlr *ctrlr)
935 {
936 	return ctrlr->vcprop.csts.raw;
937 }
938 
939 static uint64_t
940 nvmf_prop_get_aqa(struct spdk_nvmf_ctrlr *ctrlr)
941 {
942 	return ctrlr->vcprop.aqa.raw;
943 }
944 
945 static bool
946 nvmf_prop_set_aqa(struct spdk_nvmf_ctrlr *ctrlr, uint32_t value)
947 {
948 	union spdk_nvme_aqa_register aqa;
949 
950 	aqa.raw = value;
951 
952 	if (aqa.bits.asqs < SPDK_NVME_ADMIN_QUEUE_MIN_ENTRIES - 1 ||
953 	    aqa.bits.acqs < SPDK_NVME_ADMIN_QUEUE_MIN_ENTRIES - 1 ||
954 	    aqa.bits.reserved1 != 0 || aqa.bits.reserved2 != 0) {
955 		return false;
956 	}
957 
958 	ctrlr->vcprop.aqa.raw = value;
959 
960 	return true;
961 }
962 
963 static uint64_t
964 nvmf_prop_get_asq(struct spdk_nvmf_ctrlr *ctrlr)
965 {
966 	return ctrlr->vcprop.asq;
967 }
968 
969 static bool
970 nvmf_prop_set_asq_lower(struct spdk_nvmf_ctrlr *ctrlr, uint32_t value)
971 {
972 	ctrlr->vcprop.asq = (ctrlr->vcprop.asq & (0xFFFFFFFFULL << 32ULL)) | value;
973 
974 	return true;
975 }
976 
977 static bool
978 nvmf_prop_set_asq_upper(struct spdk_nvmf_ctrlr *ctrlr, uint32_t value)
979 {
980 	ctrlr->vcprop.asq = (ctrlr->vcprop.asq & 0xFFFFFFFFULL) | ((uint64_t)value << 32ULL);
981 
982 	return true;
983 }
984 
985 static uint64_t
986 nvmf_prop_get_acq(struct spdk_nvmf_ctrlr *ctrlr)
987 {
988 	return ctrlr->vcprop.acq;
989 }
990 
991 static bool
992 nvmf_prop_set_acq_lower(struct spdk_nvmf_ctrlr *ctrlr, uint32_t value)
993 {
994 	ctrlr->vcprop.acq = (ctrlr->vcprop.acq & (0xFFFFFFFFULL << 32ULL)) | value;
995 
996 	return true;
997 }
998 
999 static bool
1000 nvmf_prop_set_acq_upper(struct spdk_nvmf_ctrlr *ctrlr, uint32_t value)
1001 {
1002 	ctrlr->vcprop.acq = (ctrlr->vcprop.acq & 0xFFFFFFFFULL) | ((uint64_t)value << 32ULL);
1003 
1004 	return true;
1005 }
1006 
1007 struct nvmf_prop {
1008 	uint32_t ofst;
1009 	uint8_t size;
1010 	char name[11];
1011 	uint64_t (*get_cb)(struct spdk_nvmf_ctrlr *ctrlr);
1012 	bool (*set_cb)(struct spdk_nvmf_ctrlr *ctrlr, uint32_t value);
1013 	bool (*set_upper_cb)(struct spdk_nvmf_ctrlr *ctrlr, uint32_t value);
1014 };
1015 
1016 #define PROP(field, size, get_cb, set_cb, set_upper_cb) \
1017 	{ \
1018 		offsetof(struct spdk_nvme_registers, field), \
1019 		size, \
1020 		#field, \
1021 		get_cb, set_cb, set_upper_cb \
1022 	}
1023 
1024 static const struct nvmf_prop nvmf_props[] = {
1025 	PROP(cap,  8, nvmf_prop_get_cap,  NULL,                    NULL),
1026 	PROP(vs,   4, nvmf_prop_get_vs,   NULL,                    NULL),
1027 	PROP(cc,   4, nvmf_prop_get_cc,   nvmf_prop_set_cc,        NULL),
1028 	PROP(csts, 4, nvmf_prop_get_csts, NULL,                    NULL),
1029 	PROP(aqa,  4, nvmf_prop_get_aqa,  nvmf_prop_set_aqa,       NULL),
1030 	PROP(asq,  8, nvmf_prop_get_asq,  nvmf_prop_set_asq_lower, nvmf_prop_set_asq_upper),
1031 	PROP(acq,  8, nvmf_prop_get_acq,  nvmf_prop_set_acq_lower, nvmf_prop_set_acq_upper),
1032 };
1033 
1034 static const struct nvmf_prop *
1035 find_prop(uint32_t ofst, uint8_t size)
1036 {
1037 	size_t i;
1038 
1039 	for (i = 0; i < SPDK_COUNTOF(nvmf_props); i++) {
1040 		const struct nvmf_prop *prop = &nvmf_props[i];
1041 
1042 		if ((ofst >= prop->ofst) && (ofst + size <= prop->ofst + prop->size)) {
1043 			return prop;
1044 		}
1045 	}
1046 
1047 	return NULL;
1048 }
1049 
1050 static int
1051 nvmf_property_get(struct spdk_nvmf_request *req)
1052 {
1053 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1054 	struct spdk_nvmf_fabric_prop_get_cmd *cmd = &req->cmd->prop_get_cmd;
1055 	struct spdk_nvmf_fabric_prop_get_rsp *response = &req->rsp->prop_get_rsp;
1056 	const struct nvmf_prop *prop;
1057 	uint8_t size;
1058 
1059 	response->status.sc = 0;
1060 	response->value.u64 = 0;
1061 
1062 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "size %d, offset 0x%x\n",
1063 		      cmd->attrib.size, cmd->ofst);
1064 
1065 	switch (cmd->attrib.size) {
1066 	case SPDK_NVMF_PROP_SIZE_4:
1067 		size = 4;
1068 		break;
1069 	case SPDK_NVMF_PROP_SIZE_8:
1070 		size = 8;
1071 		break;
1072 	default:
1073 		SPDK_ERRLOG("Invalid size value %d\n", cmd->attrib.size);
1074 		response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
1075 		response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM;
1076 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1077 	}
1078 
1079 	prop = find_prop(cmd->ofst, size);
1080 	if (prop == NULL || prop->get_cb == NULL) {
1081 		response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
1082 		response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM;
1083 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1084 	}
1085 
1086 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "name: %s\n", prop->name);
1087 
1088 	response->value.u64 = prop->get_cb(ctrlr);
1089 
1090 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "response value: 0x%" PRIx64 "\n", response->value.u64);
1091 
1092 	if (size != prop->size) {
1093 		/* The size must be 4 and the prop->size is 8. Figure out which part of the property to read. */
1094 		assert(size == 4);
1095 		assert(prop->size == 8);
1096 
1097 		if (cmd->ofst == prop->ofst) {
1098 			/* Keep bottom 4 bytes only */
1099 			response->value.u64 &= 0xFFFFFFFF;
1100 		} else {
1101 			/* Keep top 4 bytes only */
1102 			response->value.u64 >>= 32;
1103 		}
1104 	}
1105 
1106 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1107 }
1108 
1109 static int
1110 nvmf_property_set(struct spdk_nvmf_request *req)
1111 {
1112 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1113 	struct spdk_nvmf_fabric_prop_set_cmd *cmd = &req->cmd->prop_set_cmd;
1114 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
1115 	const struct nvmf_prop *prop;
1116 	uint64_t value;
1117 	uint8_t size;
1118 	bool ret;
1119 
1120 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "size %d, offset 0x%x, value 0x%" PRIx64 "\n",
1121 		      cmd->attrib.size, cmd->ofst, cmd->value.u64);
1122 
1123 	switch (cmd->attrib.size) {
1124 	case SPDK_NVMF_PROP_SIZE_4:
1125 		size = 4;
1126 		break;
1127 	case SPDK_NVMF_PROP_SIZE_8:
1128 		size = 8;
1129 		break;
1130 	default:
1131 		SPDK_ERRLOG("Invalid size value %d\n", cmd->attrib.size);
1132 		response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
1133 		response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM;
1134 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1135 	}
1136 
1137 	prop = find_prop(cmd->ofst, size);
1138 	if (prop == NULL || prop->set_cb == NULL) {
1139 		SPDK_ERRLOG("Invalid offset 0x%x\n", cmd->ofst);
1140 		response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
1141 		response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM;
1142 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1143 	}
1144 
1145 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "name: %s\n", prop->name);
1146 
1147 	value = cmd->value.u64;
1148 
1149 	if (prop->size == 4) {
1150 		ret = prop->set_cb(ctrlr, (uint32_t)value);
1151 	} else if (size != prop->size) {
1152 		/* The size must be 4 and the prop->size is 8. Figure out which part of the property to write. */
1153 		assert(size == 4);
1154 		assert(prop->size == 8);
1155 
1156 		if (cmd->ofst == prop->ofst) {
1157 			ret = prop->set_cb(ctrlr, (uint32_t)value);
1158 		} else {
1159 			ret = prop->set_upper_cb(ctrlr, (uint32_t)value);
1160 		}
1161 	} else {
1162 		ret = prop->set_cb(ctrlr, (uint32_t)value);
1163 		if (ret) {
1164 			ret = prop->set_upper_cb(ctrlr, (uint32_t)(value >> 32));
1165 		}
1166 	}
1167 
1168 	if (!ret) {
1169 		SPDK_ERRLOG("prop set_cb failed\n");
1170 		response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
1171 		response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM;
1172 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1173 	}
1174 
1175 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1176 }
1177 
1178 static int
1179 nvmf_ctrlr_set_features_arbitration(struct spdk_nvmf_request *req)
1180 {
1181 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1182 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1183 
1184 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Arbitration (cdw11 = 0x%0x)\n", cmd->cdw11);
1185 
1186 	ctrlr->feat.arbitration.raw = cmd->cdw11;
1187 	ctrlr->feat.arbitration.bits.reserved = 0;
1188 
1189 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1190 }
1191 
1192 static int
1193 nvmf_ctrlr_set_features_power_management(struct spdk_nvmf_request *req)
1194 {
1195 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1196 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1197 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
1198 
1199 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Power Management (cdw11 = 0x%0x)\n", cmd->cdw11);
1200 
1201 	/* Only PS = 0 is allowed, since we report NPSS = 0 */
1202 	if (cmd->cdw11_bits.feat_power_management.bits.ps != 0) {
1203 		SPDK_ERRLOG("Invalid power state %u\n", cmd->cdw11_bits.feat_power_management.bits.ps);
1204 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
1205 		rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1206 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1207 	}
1208 
1209 	ctrlr->feat.power_management.raw = cmd->cdw11;
1210 	ctrlr->feat.power_management.bits.reserved = 0;
1211 
1212 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1213 }
1214 
1215 static bool
1216 temp_threshold_opts_valid(const union spdk_nvme_feat_temperature_threshold *opts)
1217 {
1218 	/*
1219 	 * Valid TMPSEL values:
1220 	 *  0000b - 1000b: temperature sensors
1221 	 *  1111b: set all implemented temperature sensors
1222 	 */
1223 	if (opts->bits.tmpsel >= 9 && opts->bits.tmpsel != 15) {
1224 		/* 1001b - 1110b: reserved */
1225 		SPDK_ERRLOG("Invalid TMPSEL %u\n", opts->bits.tmpsel);
1226 		return false;
1227 	}
1228 
1229 	/*
1230 	 * Valid THSEL values:
1231 	 *  00b: over temperature threshold
1232 	 *  01b: under temperature threshold
1233 	 */
1234 	if (opts->bits.thsel > 1) {
1235 		/* 10b - 11b: reserved */
1236 		SPDK_ERRLOG("Invalid THSEL %u\n", opts->bits.thsel);
1237 		return false;
1238 	}
1239 
1240 	return true;
1241 }
1242 
1243 static int
1244 nvmf_ctrlr_set_features_temperature_threshold(struct spdk_nvmf_request *req)
1245 {
1246 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1247 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
1248 
1249 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Temperature Threshold (cdw11 = 0x%0x)\n", cmd->cdw11);
1250 
1251 	if (!temp_threshold_opts_valid(&cmd->cdw11_bits.feat_temp_threshold)) {
1252 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
1253 		rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1254 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1255 	}
1256 
1257 	/* TODO: no sensors implemented - ignore new values */
1258 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1259 }
1260 
1261 static int
1262 nvmf_ctrlr_get_features_temperature_threshold(struct spdk_nvmf_request *req)
1263 {
1264 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1265 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
1266 
1267 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Get Features - Temperature Threshold (cdw11 = 0x%0x)\n", cmd->cdw11);
1268 
1269 	if (!temp_threshold_opts_valid(&cmd->cdw11_bits.feat_temp_threshold)) {
1270 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
1271 		rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1272 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1273 	}
1274 
1275 	/* TODO: no sensors implemented - return 0 for all thresholds */
1276 	rsp->cdw0 = 0;
1277 
1278 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1279 }
1280 
1281 static int
1282 nvmf_ctrlr_set_features_error_recovery(struct spdk_nvmf_request *req)
1283 {
1284 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1285 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1286 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
1287 
1288 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Error Recovery (cdw11 = 0x%0x)\n", cmd->cdw11);
1289 
1290 	if (cmd->cdw11_bits.feat_error_recovery.bits.dulbe) {
1291 		/*
1292 		 * Host is not allowed to set this bit, since we don't advertise it in
1293 		 * Identify Namespace.
1294 		 */
1295 		SPDK_ERRLOG("Host set unsupported DULBE bit\n");
1296 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
1297 		rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1298 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1299 	}
1300 
1301 	ctrlr->feat.error_recovery.raw = cmd->cdw11;
1302 	ctrlr->feat.error_recovery.bits.reserved = 0;
1303 
1304 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1305 }
1306 
1307 static int
1308 nvmf_ctrlr_set_features_volatile_write_cache(struct spdk_nvmf_request *req)
1309 {
1310 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1311 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1312 
1313 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Volatile Write Cache (cdw11 = 0x%0x)\n", cmd->cdw11);
1314 
1315 	ctrlr->feat.volatile_write_cache.raw = cmd->cdw11;
1316 	ctrlr->feat.volatile_write_cache.bits.reserved = 0;
1317 
1318 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Volatile Write Cache %s\n",
1319 		      ctrlr->feat.volatile_write_cache.bits.wce ? "Enabled" : "Disabled");
1320 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1321 }
1322 
1323 static int
1324 nvmf_ctrlr_set_features_write_atomicity(struct spdk_nvmf_request *req)
1325 {
1326 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1327 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1328 
1329 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Write Atomicity (cdw11 = 0x%0x)\n", cmd->cdw11);
1330 
1331 	ctrlr->feat.write_atomicity.raw = cmd->cdw11;
1332 	ctrlr->feat.write_atomicity.bits.reserved = 0;
1333 
1334 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1335 }
1336 
1337 static int
1338 nvmf_ctrlr_set_features_host_identifier(struct spdk_nvmf_request *req)
1339 {
1340 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
1341 
1342 	SPDK_ERRLOG("Set Features - Host Identifier not allowed\n");
1343 	response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
1344 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1345 }
1346 
1347 static int
1348 nvmf_ctrlr_get_features_host_identifier(struct spdk_nvmf_request *req)
1349 {
1350 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1351 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1352 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
1353 
1354 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Get Features - Host Identifier\n");
1355 
1356 	if (!cmd->cdw11_bits.feat_host_identifier.bits.exhid) {
1357 		/* NVMe over Fabrics requires EXHID=1 (128-bit/16-byte host ID) */
1358 		SPDK_ERRLOG("Get Features - Host Identifier with EXHID=0 not allowed\n");
1359 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1360 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1361 	}
1362 
1363 	if (req->data == NULL || req->length < sizeof(ctrlr->hostid)) {
1364 		SPDK_ERRLOG("Invalid data buffer for Get Features - Host Identifier\n");
1365 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1366 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1367 	}
1368 
1369 	spdk_uuid_copy((struct spdk_uuid *)req->data, &ctrlr->hostid);
1370 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1371 }
1372 
1373 static int
1374 nvmf_ctrlr_get_features_reservation_notification_mask(struct spdk_nvmf_request *req)
1375 {
1376 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1377 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1378 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
1379 	struct spdk_nvmf_ns *ns;
1380 
1381 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "get Features - Reservation Notificaton Mask\n");
1382 
1383 	if (cmd->nsid == 0xffffffffu) {
1384 		SPDK_ERRLOG("get Features - Invalid Namespace ID\n");
1385 		rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1386 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1387 	}
1388 
1389 	ns = _nvmf_subsystem_get_ns(ctrlr->subsys, cmd->nsid);
1390 	if (ns == NULL) {
1391 		SPDK_ERRLOG("Set Features - Invalid Namespace ID\n");
1392 		rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1393 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1394 	}
1395 	rsp->cdw0 = ns->mask;
1396 
1397 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1398 }
1399 
1400 static int
1401 nvmf_ctrlr_set_features_reservation_notification_mask(struct spdk_nvmf_request *req)
1402 {
1403 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1404 	struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys;
1405 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1406 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
1407 	struct spdk_nvmf_ns *ns;
1408 
1409 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Reservation Notificaton Mask\n");
1410 
1411 	if (cmd->nsid == 0xffffffffu) {
1412 		for (ns = spdk_nvmf_subsystem_get_first_ns(subsystem); ns != NULL;
1413 		     ns = spdk_nvmf_subsystem_get_next_ns(subsystem, ns)) {
1414 			ns->mask = cmd->cdw11;
1415 		}
1416 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1417 	}
1418 
1419 	ns = _nvmf_subsystem_get_ns(ctrlr->subsys, cmd->nsid);
1420 	if (ns == NULL) {
1421 		SPDK_ERRLOG("Set Features - Invalid Namespace ID\n");
1422 		rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1423 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1424 	}
1425 	ns->mask = cmd->cdw11;
1426 
1427 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1428 }
1429 
1430 static int
1431 nvmf_ctrlr_get_features_reservation_persistence(struct spdk_nvmf_request *req)
1432 {
1433 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1434 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1435 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
1436 	struct spdk_nvmf_ns *ns;
1437 
1438 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Get Features - Reservation Persistence\n");
1439 
1440 	ns = _nvmf_subsystem_get_ns(ctrlr->subsys, cmd->nsid);
1441 	/* NSID with 0xffffffffu also included */
1442 	if (ns == NULL) {
1443 		SPDK_ERRLOG("Get Features - Invalid Namespace ID\n");
1444 		response->status.sct = SPDK_NVME_SCT_GENERIC;
1445 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1446 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1447 	}
1448 
1449 	response->cdw0 = ns->ptpl_activated;
1450 
1451 	response->status.sct = SPDK_NVME_SCT_GENERIC;
1452 	response->status.sc = SPDK_NVME_SC_SUCCESS;
1453 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1454 }
1455 
1456 static int
1457 nvmf_ctrlr_set_features_reservation_persistence(struct spdk_nvmf_request *req)
1458 {
1459 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1460 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1461 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
1462 	struct spdk_nvmf_ns *ns;
1463 	bool ptpl;
1464 
1465 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Reservation Persistence\n");
1466 
1467 	ns = _nvmf_subsystem_get_ns(ctrlr->subsys, cmd->nsid);
1468 	ptpl = cmd->cdw11_bits.feat_rsv_persistence.bits.ptpl;
1469 
1470 	if (cmd->nsid != 0xffffffffu && ns && ns->ptpl_file) {
1471 		ns->ptpl_activated = ptpl;
1472 	} else if (cmd->nsid == 0xffffffffu) {
1473 		for (ns = spdk_nvmf_subsystem_get_first_ns(ctrlr->subsys); ns && ns->ptpl_file;
1474 		     ns = spdk_nvmf_subsystem_get_next_ns(ctrlr->subsys, ns)) {
1475 			ns->ptpl_activated = ptpl;
1476 		}
1477 	} else {
1478 		SPDK_ERRLOG("Set Features - Invalid Namespace ID or Reservation Configuration\n");
1479 		response->status.sct = SPDK_NVME_SCT_GENERIC;
1480 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1481 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1482 	}
1483 
1484 	/* TODO: Feature not changeable for now */
1485 	response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
1486 	response->status.sc = SPDK_NVME_SC_FEATURE_ID_NOT_SAVEABLE;
1487 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1488 }
1489 
1490 static int
1491 nvmf_ctrlr_set_features_keep_alive_timer(struct spdk_nvmf_request *req)
1492 {
1493 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1494 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1495 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
1496 
1497 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Keep Alive Timer (%u ms)\n", cmd->cdw11);
1498 
1499 	/*
1500 	 * if attempts to disable keep alive by setting kato to 0h
1501 	 * a status value of keep alive invalid shall be returned
1502 	 */
1503 	if (cmd->cdw11_bits.feat_keep_alive_timer.bits.kato == 0) {
1504 		rsp->status.sc = SPDK_NVME_SC_KEEP_ALIVE_INVALID;
1505 	} else if (cmd->cdw11_bits.feat_keep_alive_timer.bits.kato < MIN_KEEP_ALIVE_TIMEOUT_IN_MS) {
1506 		ctrlr->feat.keep_alive_timer.bits.kato = MIN_KEEP_ALIVE_TIMEOUT_IN_MS;
1507 	} else {
1508 		/* round up to milliseconds */
1509 		ctrlr->feat.keep_alive_timer.bits.kato = spdk_divide_round_up(
1510 					cmd->cdw11_bits.feat_keep_alive_timer.bits.kato,
1511 					KAS_DEFAULT_VALUE * KAS_TIME_UNIT_IN_MS) *
1512 				KAS_DEFAULT_VALUE * KAS_TIME_UNIT_IN_MS;
1513 	}
1514 
1515 	/*
1516 	 * if change the keep alive timeout value successfully
1517 	 * update the keep alive poller.
1518 	 */
1519 	if (cmd->cdw11_bits.feat_keep_alive_timer.bits.kato != 0) {
1520 		if (ctrlr->keep_alive_poller != NULL) {
1521 			spdk_poller_unregister(&ctrlr->keep_alive_poller);
1522 		}
1523 		ctrlr->keep_alive_poller = SPDK_POLLER_REGISTER(nvmf_ctrlr_keep_alive_poll, ctrlr,
1524 					   ctrlr->feat.keep_alive_timer.bits.kato * 1000);
1525 	}
1526 
1527 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Keep Alive Timer set to %u ms\n",
1528 		      ctrlr->feat.keep_alive_timer.bits.kato);
1529 
1530 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1531 }
1532 
1533 static int
1534 nvmf_ctrlr_set_features_number_of_queues(struct spdk_nvmf_request *req)
1535 {
1536 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1537 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
1538 	uint32_t count;
1539 
1540 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Number of Queues, cdw11 0x%x\n",
1541 		      req->cmd->nvme_cmd.cdw11);
1542 
1543 	count = spdk_bit_array_count_set(ctrlr->qpair_mask);
1544 	/* verify that the controller is ready to process commands */
1545 	if (count > 1) {
1546 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Queue pairs already active!\n");
1547 		rsp->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
1548 	} else {
1549 		/*
1550 		 * Ignore the value requested by the host -
1551 		 * always return the pre-configured value based on max_qpairs_allowed.
1552 		 */
1553 		rsp->cdw0 = ctrlr->feat.number_of_queues.raw;
1554 	}
1555 
1556 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1557 }
1558 
1559 static int
1560 nvmf_ctrlr_set_features_async_event_configuration(struct spdk_nvmf_request *req)
1561 {
1562 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1563 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1564 
1565 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Async Event Configuration, cdw11 0x%08x\n",
1566 		      cmd->cdw11);
1567 	ctrlr->feat.async_event_configuration.raw = cmd->cdw11;
1568 	ctrlr->feat.async_event_configuration.bits.reserved = 0;
1569 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1570 }
1571 
1572 static int
1573 nvmf_ctrlr_async_event_request(struct spdk_nvmf_request *req)
1574 {
1575 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1576 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
1577 	struct spdk_nvmf_subsystem_poll_group *sgroup;
1578 
1579 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Async Event Request\n");
1580 
1581 	/* AER cmd is an exception */
1582 	sgroup = &req->qpair->group->sgroups[ctrlr->subsys->id];
1583 	assert(sgroup != NULL);
1584 	sgroup->io_outstanding--;
1585 
1586 	/* Four asynchronous events are supported for now */
1587 	if (ctrlr->nr_aer_reqs >= NVMF_MAX_ASYNC_EVENTS) {
1588 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "AERL exceeded\n");
1589 		rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
1590 		rsp->status.sc = SPDK_NVME_SC_ASYNC_EVENT_REQUEST_LIMIT_EXCEEDED;
1591 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1592 	}
1593 
1594 	if (ctrlr->notice_event.bits.async_event_type ==
1595 	    SPDK_NVME_ASYNC_EVENT_TYPE_NOTICE) {
1596 		rsp->cdw0 = ctrlr->notice_event.raw;
1597 		ctrlr->notice_event.raw = 0;
1598 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1599 	}
1600 
1601 	if (ctrlr->reservation_event.bits.async_event_type ==
1602 	    SPDK_NVME_ASYNC_EVENT_TYPE_IO) {
1603 		rsp->cdw0 = ctrlr->reservation_event.raw;
1604 		ctrlr->reservation_event.raw = 0;
1605 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1606 	}
1607 
1608 	ctrlr->aer_req[ctrlr->nr_aer_reqs++] = req;
1609 	return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
1610 }
1611 
1612 static void
1613 nvmf_get_firmware_slot_log_page(void *buffer, uint64_t offset, uint32_t length)
1614 {
1615 	struct spdk_nvme_firmware_page fw_page;
1616 	size_t copy_len;
1617 
1618 	memset(&fw_page, 0, sizeof(fw_page));
1619 	fw_page.afi.active_slot = 1;
1620 	fw_page.afi.next_reset_slot = 0;
1621 	spdk_strcpy_pad(fw_page.revision[0], FW_VERSION, sizeof(fw_page.revision[0]), ' ');
1622 
1623 	if (offset < sizeof(fw_page)) {
1624 		copy_len = spdk_min(sizeof(fw_page) - offset, length);
1625 		if (copy_len > 0) {
1626 			memcpy(buffer, (const char *)&fw_page + offset, copy_len);
1627 		}
1628 	}
1629 }
1630 
1631 static void
1632 nvmf_get_ana_log_page(struct spdk_nvmf_ctrlr *ctrlr, void *data,
1633 		      uint64_t offset, uint32_t length)
1634 {
1635 	char *buf = data;
1636 	struct spdk_nvme_ana_page ana_hdr;
1637 	const size_t ana_desc_size = sizeof(struct spdk_nvme_ana_group_descriptor) +
1638 				     sizeof(uint32_t);
1639 	char _ana_desc[ana_desc_size];
1640 	struct spdk_nvme_ana_group_descriptor *ana_desc;
1641 	size_t copy_len;
1642 	uint32_t num_ns = 0;
1643 	struct spdk_nvmf_ns *ns;
1644 
1645 	if (length == 0) {
1646 		return;
1647 	}
1648 
1649 	if (offset >= sizeof(ana_hdr)) {
1650 		offset -= sizeof(ana_hdr);
1651 	} else {
1652 		for (ns = spdk_nvmf_subsystem_get_first_ns(ctrlr->subsys); ns != NULL;
1653 		     ns = spdk_nvmf_subsystem_get_next_ns(ctrlr->subsys, ns)) {
1654 			num_ns++;
1655 		}
1656 
1657 		memset(&ana_hdr, 0, sizeof(ana_hdr));
1658 
1659 		ana_hdr.num_ana_group_desc = num_ns;
1660 		/* TODO: Support Change Count. */
1661 		ana_hdr.change_count = 0;
1662 
1663 		copy_len = spdk_min(sizeof(ana_hdr) - offset, length);
1664 		memcpy(buf, (const char *)&ana_hdr + offset, copy_len);
1665 		length -= copy_len;
1666 		buf += copy_len;
1667 		offset = 0;
1668 	}
1669 
1670 	if (length == 0) {
1671 		return;
1672 	}
1673 
1674 	ana_desc = (void *)_ana_desc;
1675 
1676 	for (ns = spdk_nvmf_subsystem_get_first_ns(ctrlr->subsys); ns != NULL;
1677 	     ns = spdk_nvmf_subsystem_get_next_ns(ctrlr->subsys, ns)) {
1678 		if (offset >= ana_desc_size) {
1679 			offset -= ana_desc_size;
1680 			continue;
1681 		}
1682 
1683 		memset(ana_desc, 0, ana_desc_size);
1684 
1685 		ana_desc->ana_group_id = ns->nsid;
1686 		ana_desc->num_of_nsid = 1;
1687 		ana_desc->ana_state = SPDK_NVME_ANA_OPTIMIZED_STATE;
1688 		ana_desc->nsid[0] = ns->nsid;
1689 		/* TODO: Support Change Count. */
1690 		ana_desc->change_count = 0;
1691 
1692 		copy_len = spdk_min(ana_desc_size - offset, length);
1693 		memcpy(buf, (const char *)ana_desc + offset, copy_len);
1694 		length -= copy_len;
1695 		buf += copy_len;
1696 		offset = 0;
1697 
1698 		if (length == 0) {
1699 			return;
1700 		}
1701 	}
1702 }
1703 
1704 void
1705 nvmf_ctrlr_ns_changed(struct spdk_nvmf_ctrlr *ctrlr, uint32_t nsid)
1706 {
1707 	uint16_t max_changes = SPDK_COUNTOF(ctrlr->changed_ns_list.ns_list);
1708 	uint16_t i;
1709 	bool found = false;
1710 
1711 	for (i = 0; i < ctrlr->changed_ns_list_count; i++) {
1712 		if (ctrlr->changed_ns_list.ns_list[i] == nsid) {
1713 			/* nsid is already in the list */
1714 			found = true;
1715 			break;
1716 		}
1717 	}
1718 
1719 	if (!found) {
1720 		if (ctrlr->changed_ns_list_count == max_changes) {
1721 			/* Out of space - set first entry to FFFFFFFFh and zero-fill the rest. */
1722 			ctrlr->changed_ns_list.ns_list[0] = 0xFFFFFFFFu;
1723 			for (i = 1; i < max_changes; i++) {
1724 				ctrlr->changed_ns_list.ns_list[i] = 0;
1725 			}
1726 		} else {
1727 			ctrlr->changed_ns_list.ns_list[ctrlr->changed_ns_list_count++] = nsid;
1728 		}
1729 	}
1730 }
1731 
1732 static void
1733 nvmf_get_changed_ns_list_log_page(struct spdk_nvmf_ctrlr *ctrlr,
1734 				  void *buffer, uint64_t offset, uint32_t length)
1735 {
1736 	size_t copy_length;
1737 
1738 	if (offset < sizeof(ctrlr->changed_ns_list)) {
1739 		copy_length = spdk_min(length, sizeof(ctrlr->changed_ns_list) - offset);
1740 		if (copy_length) {
1741 			memcpy(buffer, (char *)&ctrlr->changed_ns_list + offset, copy_length);
1742 		}
1743 	}
1744 
1745 	/* Clear log page each time it is read */
1746 	ctrlr->changed_ns_list_count = 0;
1747 	memset(&ctrlr->changed_ns_list, 0, sizeof(ctrlr->changed_ns_list));
1748 }
1749 
1750 /* The structure can be modified if we provide support for other commands in future */
1751 static const struct spdk_nvme_cmds_and_effect_log_page g_cmds_and_effect_log_page = {
1752 	.admin_cmds_supported = {
1753 		/* CSUPP, LBCC, NCC, NIC, CCC, CSE */
1754 		/* Get Log Page */
1755 		[SPDK_NVME_OPC_GET_LOG_PAGE]		= {1, 0, 0, 0, 0, 0, 0, 0},
1756 		/* Identify */
1757 		[SPDK_NVME_OPC_IDENTIFY]		= {1, 0, 0, 0, 0, 0, 0, 0},
1758 		/* Abort */
1759 		[SPDK_NVME_OPC_ABORT]			= {1, 0, 0, 0, 0, 0, 0, 0},
1760 		/* Set Features */
1761 		[SPDK_NVME_OPC_SET_FEATURES]		= {1, 0, 0, 0, 0, 0, 0, 0},
1762 		/* Get Features */
1763 		[SPDK_NVME_OPC_GET_FEATURES]		= {1, 0, 0, 0, 0, 0, 0, 0},
1764 		/* Async Event Request */
1765 		[SPDK_NVME_OPC_ASYNC_EVENT_REQUEST]	= {1, 0, 0, 0, 0, 0, 0, 0},
1766 		/* Keep Alive */
1767 		[SPDK_NVME_OPC_KEEP_ALIVE]		= {1, 0, 0, 0, 0, 0, 0, 0},
1768 	},
1769 	.io_cmds_supported = {
1770 		/* FLUSH */
1771 		[SPDK_NVME_OPC_FLUSH]			= {1, 1, 0, 0, 0, 0, 0, 0},
1772 		/* WRITE */
1773 		[SPDK_NVME_OPC_WRITE]			= {1, 1, 0, 0, 0, 0, 0, 0},
1774 		/* READ */
1775 		[SPDK_NVME_OPC_READ]			= {1, 0, 0, 0, 0, 0, 0, 0},
1776 		/* WRITE ZEROES */
1777 		[SPDK_NVME_OPC_WRITE_ZEROES]		= {1, 1, 0, 0, 0, 0, 0, 0},
1778 		/* DATASET MANAGEMENT */
1779 		[SPDK_NVME_OPC_DATASET_MANAGEMENT]	= {1, 1, 0, 0, 0, 0, 0, 0},
1780 		/* COMPARE */
1781 		[SPDK_NVME_OPC_COMPARE]			= {1, 0, 0, 0, 0, 0, 0, 0},
1782 	},
1783 };
1784 
1785 static void
1786 nvmf_get_cmds_and_effects_log_page(void *buffer,
1787 				   uint64_t offset, uint32_t length)
1788 {
1789 	uint32_t page_size = sizeof(struct spdk_nvme_cmds_and_effect_log_page);
1790 	size_t copy_len = 0;
1791 	size_t zero_len = length;
1792 
1793 	if (offset < page_size) {
1794 		copy_len = spdk_min(page_size - offset, length);
1795 		zero_len -= copy_len;
1796 		memcpy(buffer, (char *)(&g_cmds_and_effect_log_page) + offset, copy_len);
1797 	}
1798 
1799 	if (zero_len) {
1800 		memset((char *)buffer + copy_len, 0, zero_len);
1801 	}
1802 }
1803 
1804 static void
1805 nvmf_get_reservation_notification_log_page(struct spdk_nvmf_ctrlr *ctrlr,
1806 		void *data, uint64_t offset, uint32_t length)
1807 {
1808 	uint32_t unit_log_len, avail_log_len, next_pos, copy_len;
1809 	struct spdk_nvmf_reservation_log *log, *log_tmp;
1810 	uint8_t *buf = data;
1811 
1812 	unit_log_len = sizeof(struct spdk_nvme_reservation_notification_log);
1813 	/* No available log, return 1 zeroed log page */
1814 	if (!ctrlr->num_avail_log_pages) {
1815 		memset(buf, 0, spdk_min(length, unit_log_len));
1816 		return;
1817 	}
1818 
1819 	avail_log_len = ctrlr->num_avail_log_pages * unit_log_len;
1820 	if (offset >= avail_log_len) {
1821 		return;
1822 	}
1823 
1824 	next_pos = copy_len = 0;
1825 	TAILQ_FOREACH_SAFE(log, &ctrlr->log_head, link, log_tmp) {
1826 		TAILQ_REMOVE(&ctrlr->log_head, log, link);
1827 		ctrlr->num_avail_log_pages--;
1828 
1829 		next_pos += unit_log_len;
1830 		if (next_pos > offset) {
1831 			copy_len = spdk_min(next_pos - offset, length);
1832 			memcpy(buf, &log->log, copy_len);
1833 			length -= copy_len;
1834 			offset += copy_len;
1835 			buf += copy_len;
1836 		}
1837 		free(log);
1838 
1839 		if (length == 0) {
1840 			break;
1841 		}
1842 	}
1843 	return;
1844 }
1845 
1846 static int
1847 nvmf_ctrlr_get_log_page(struct spdk_nvmf_request *req)
1848 {
1849 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1850 	struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys;
1851 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1852 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
1853 	uint64_t offset, len;
1854 	uint32_t numdl, numdu;
1855 	uint8_t lid;
1856 
1857 	if (req->data == NULL) {
1858 		SPDK_ERRLOG("get log command with no buffer\n");
1859 		response->status.sct = SPDK_NVME_SCT_GENERIC;
1860 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1861 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1862 	}
1863 
1864 	offset = (uint64_t)cmd->cdw12 | ((uint64_t)cmd->cdw13 << 32);
1865 	if (offset & 3) {
1866 		SPDK_ERRLOG("Invalid log page offset 0x%" PRIx64 "\n", offset);
1867 		response->status.sct = SPDK_NVME_SCT_GENERIC;
1868 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1869 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1870 	}
1871 
1872 	numdl = cmd->cdw10_bits.get_log_page.numdl;
1873 	numdu = cmd->cdw11_bits.get_log_page.numdu;
1874 	len = ((numdu << 16) + numdl + (uint64_t)1) * 4;
1875 	if (len > req->length) {
1876 		SPDK_ERRLOG("Get log page: len (%" PRIu64 ") > buf size (%u)\n",
1877 			    len, req->length);
1878 		response->status.sct = SPDK_NVME_SCT_GENERIC;
1879 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1880 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1881 	}
1882 
1883 	lid = cmd->cdw10_bits.get_log_page.lid;
1884 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Get log page: LID=0x%02X offset=0x%" PRIx64 " len=0x%" PRIx64 "\n",
1885 		      lid, offset, len);
1886 
1887 	if (subsystem->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) {
1888 		switch (lid) {
1889 		case SPDK_NVME_LOG_DISCOVERY:
1890 			nvmf_get_discovery_log_page(subsystem->tgt, ctrlr->hostnqn, req->iov, req->iovcnt, offset,
1891 						    len);
1892 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1893 		default:
1894 			goto invalid_log_page;
1895 		}
1896 	} else {
1897 		switch (lid) {
1898 		case SPDK_NVME_LOG_ERROR:
1899 		case SPDK_NVME_LOG_HEALTH_INFORMATION:
1900 			/* TODO: actually fill out log page data */
1901 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1902 		case SPDK_NVME_LOG_FIRMWARE_SLOT:
1903 			nvmf_get_firmware_slot_log_page(req->data, offset, len);
1904 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1905 		case SPDK_NVME_LOG_ASYMMETRIC_NAMESPACE_ACCESS:
1906 			if (subsystem->ana_reporting) {
1907 				nvmf_get_ana_log_page(ctrlr, req->data, offset, len);
1908 				return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1909 			} else {
1910 				goto invalid_log_page;
1911 			}
1912 		case SPDK_NVME_LOG_COMMAND_EFFECTS_LOG:
1913 			nvmf_get_cmds_and_effects_log_page(req->data, offset, len);
1914 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1915 		case SPDK_NVME_LOG_CHANGED_NS_LIST:
1916 			nvmf_get_changed_ns_list_log_page(ctrlr, req->data, offset, len);
1917 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1918 		case SPDK_NVME_LOG_RESERVATION_NOTIFICATION:
1919 			nvmf_get_reservation_notification_log_page(ctrlr, req->data, offset, len);
1920 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1921 		default:
1922 			goto invalid_log_page;
1923 		}
1924 	}
1925 
1926 invalid_log_page:
1927 	SPDK_ERRLOG("Unsupported Get Log Page 0x%02X\n", lid);
1928 	response->status.sct = SPDK_NVME_SCT_GENERIC;
1929 	response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1930 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1931 }
1932 
1933 int
1934 spdk_nvmf_ctrlr_identify_ns(struct spdk_nvmf_ctrlr *ctrlr,
1935 			    struct spdk_nvme_cmd *cmd,
1936 			    struct spdk_nvme_cpl *rsp,
1937 			    struct spdk_nvme_ns_data *nsdata)
1938 {
1939 	struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys;
1940 	struct spdk_nvmf_ns *ns;
1941 	uint32_t max_num_blocks;
1942 
1943 	if (cmd->nsid == 0 || cmd->nsid > subsystem->max_nsid) {
1944 		SPDK_ERRLOG("Identify Namespace for invalid NSID %u\n", cmd->nsid);
1945 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
1946 		rsp->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT;
1947 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1948 	}
1949 
1950 	ns = _nvmf_subsystem_get_ns(subsystem, cmd->nsid);
1951 	if (ns == NULL || ns->bdev == NULL) {
1952 		/*
1953 		 * Inactive namespaces should return a zero filled data structure.
1954 		 * The data buffer is already zeroed by nvmf_ctrlr_process_admin_cmd(),
1955 		 * so we can just return early here.
1956 		 */
1957 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Identify Namespace for inactive NSID %u\n", cmd->nsid);
1958 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
1959 		rsp->status.sc = SPDK_NVME_SC_SUCCESS;
1960 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1961 	}
1962 
1963 	nvmf_bdev_ctrlr_identify_ns(ns, nsdata, ctrlr->dif_insert_or_strip);
1964 
1965 	/* Due to bug in the Linux kernel NVMe driver we have to set noiob no larger than mdts */
1966 	max_num_blocks = ctrlr->admin_qpair->transport->opts.max_io_size /
1967 			 (1U << nsdata->lbaf[nsdata->flbas.format].lbads);
1968 	if (nsdata->noiob > max_num_blocks) {
1969 		nsdata->noiob = max_num_blocks;
1970 	}
1971 
1972 	if (subsystem->ana_reporting) {
1973 		/* ANA group ID matches NSID. */
1974 		nsdata->anagrpid = ns->nsid;
1975 	}
1976 
1977 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1978 }
1979 
1980 static void
1981 nvmf_ctrlr_populate_oacs(struct spdk_nvmf_ctrlr *ctrlr,
1982 			 struct spdk_nvme_ctrlr_data *cdata)
1983 {
1984 	cdata->oacs.virtualization_management =
1985 		g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_VIRTUALIZATION_MANAGEMENT].hdlr != NULL;
1986 	cdata->oacs.nvme_mi = g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_NVME_MI_SEND].hdlr != NULL
1987 			      && g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_NVME_MI_RECEIVE].hdlr != NULL;
1988 	cdata->oacs.directives = g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_DIRECTIVE_SEND].hdlr != NULL
1989 				 && g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_DIRECTIVE_RECEIVE].hdlr != NULL;
1990 	cdata->oacs.device_self_test =
1991 		g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_DEVICE_SELF_TEST].hdlr != NULL;
1992 	cdata->oacs.ns_manage = g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_NS_MANAGEMENT].hdlr != NULL
1993 				&& g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_NS_ATTACHMENT].hdlr != NULL;
1994 	cdata->oacs.firmware = g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_FIRMWARE_IMAGE_DOWNLOAD].hdlr !=
1995 			       NULL
1996 			       && g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_FIRMWARE_COMMIT].hdlr != NULL;
1997 	cdata->oacs.format =
1998 		g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_FORMAT_NVM].hdlr != NULL;
1999 	cdata->oacs.security = g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_SECURITY_SEND].hdlr != NULL
2000 			       && g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_SECURITY_RECEIVE].hdlr != NULL;
2001 	cdata->oacs.get_lba_status = g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_GET_LBA_STATUS].hdlr !=
2002 				     NULL;
2003 }
2004 
2005 int
2006 spdk_nvmf_ctrlr_identify_ctrlr(struct spdk_nvmf_ctrlr *ctrlr, struct spdk_nvme_ctrlr_data *cdata)
2007 {
2008 	struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys;
2009 	struct spdk_nvmf_transport *transport = ctrlr->admin_qpair->transport;
2010 
2011 	/*
2012 	 * Common fields for discovery and NVM subsystems
2013 	 */
2014 	spdk_strcpy_pad(cdata->fr, FW_VERSION, sizeof(cdata->fr), ' ');
2015 	assert((transport->opts.max_io_size % 4096) == 0);
2016 	cdata->mdts = spdk_u32log2(transport->opts.max_io_size / 4096);
2017 	cdata->cntlid = ctrlr->cntlid;
2018 	cdata->ver = ctrlr->vcprop.vs;
2019 	cdata->aerl = NVMF_MAX_ASYNC_EVENTS - 1;
2020 	cdata->lpa.edlp = 1;
2021 	cdata->elpe = 127;
2022 	cdata->maxcmd = transport->opts.max_queue_depth;
2023 	cdata->sgls = ctrlr->cdata.sgls;
2024 	cdata->fuses.compare_and_write = 1;
2025 	cdata->acwu = 1;
2026 	if (subsystem->ana_reporting) {
2027 		cdata->mnan = subsystem->max_nsid;
2028 	}
2029 	spdk_strcpy_pad(cdata->subnqn, subsystem->subnqn, sizeof(cdata->subnqn), '\0');
2030 
2031 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ctrlr data: maxcmd 0x%x\n", cdata->maxcmd);
2032 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "sgls data: 0x%x\n", from_le32(&cdata->sgls));
2033 
2034 	/*
2035 	 * NVM subsystem fields (reserved for discovery subsystems)
2036 	 */
2037 	if (subsystem->subtype == SPDK_NVMF_SUBTYPE_NVME) {
2038 		spdk_strcpy_pad(cdata->mn, spdk_nvmf_subsystem_get_mn(subsystem), sizeof(cdata->mn), ' ');
2039 		spdk_strcpy_pad(cdata->sn, spdk_nvmf_subsystem_get_sn(subsystem), sizeof(cdata->sn), ' ');
2040 		cdata->kas = ctrlr->cdata.kas;
2041 
2042 		cdata->rab = 6;
2043 		cdata->cmic.multi_port = 1;
2044 		cdata->cmic.multi_host = 1;
2045 		if (subsystem->ana_reporting) {
2046 			/* Asymmetric Namespace Access Reporting is supported. */
2047 			cdata->cmic.ana_reporting = 1;
2048 		}
2049 		cdata->oaes.ns_attribute_notices = 1;
2050 		cdata->ctratt.host_id_exhid_supported = 1;
2051 		/* TODO: Concurrent execution of multiple abort commands. */
2052 		cdata->acl = 0;
2053 		cdata->aerl = 0;
2054 		cdata->frmw.slot1_ro = 1;
2055 		cdata->frmw.num_slots = 1;
2056 
2057 		cdata->lpa.celp = 1; /* Command Effects log page supported */
2058 
2059 		cdata->sqes.min = 6;
2060 		cdata->sqes.max = 6;
2061 		cdata->cqes.min = 4;
2062 		cdata->cqes.max = 4;
2063 		cdata->nn = subsystem->max_nsid;
2064 		cdata->vwc.present = 1;
2065 		cdata->vwc.flush_broadcast = SPDK_NVME_FLUSH_BROADCAST_NOT_SUPPORTED;
2066 
2067 		cdata->nvmf_specific = ctrlr->cdata.nvmf_specific;
2068 
2069 		cdata->oncs.dsm = nvmf_ctrlr_dsm_supported(ctrlr);
2070 		cdata->oncs.write_zeroes = nvmf_ctrlr_write_zeroes_supported(ctrlr);
2071 		cdata->oncs.reservations = 1;
2072 		if (subsystem->ana_reporting) {
2073 			cdata->anacap.ana_optimized_state = 1;
2074 			/* ANAGRPID does not change while namespace is attached to controller */
2075 			cdata->anacap.no_change_anagrpid = 1;
2076 			cdata->anagrpmax = subsystem->max_nsid;
2077 			cdata->nanagrpid = subsystem->max_nsid;
2078 		}
2079 
2080 		nvmf_ctrlr_populate_oacs(ctrlr, cdata);
2081 
2082 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ext ctrlr data: ioccsz 0x%x\n",
2083 			      cdata->nvmf_specific.ioccsz);
2084 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ext ctrlr data: iorcsz 0x%x\n",
2085 			      cdata->nvmf_specific.iorcsz);
2086 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ext ctrlr data: icdoff 0x%x\n",
2087 			      cdata->nvmf_specific.icdoff);
2088 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ext ctrlr data: ctrattr 0x%x\n",
2089 			      *(uint8_t *)&cdata->nvmf_specific.ctrattr);
2090 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ext ctrlr data: msdbd 0x%x\n",
2091 			      cdata->nvmf_specific.msdbd);
2092 	}
2093 
2094 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2095 }
2096 
2097 static int
2098 nvmf_ctrlr_identify_active_ns_list(struct spdk_nvmf_subsystem *subsystem,
2099 				   struct spdk_nvme_cmd *cmd,
2100 				   struct spdk_nvme_cpl *rsp,
2101 				   struct spdk_nvme_ns_list *ns_list)
2102 {
2103 	struct spdk_nvmf_ns *ns;
2104 	uint32_t count = 0;
2105 
2106 	if (cmd->nsid >= 0xfffffffeUL) {
2107 		SPDK_ERRLOG("Identify Active Namespace List with invalid NSID %u\n", cmd->nsid);
2108 		rsp->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT;
2109 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2110 	}
2111 
2112 	for (ns = spdk_nvmf_subsystem_get_first_ns(subsystem); ns != NULL;
2113 	     ns = spdk_nvmf_subsystem_get_next_ns(subsystem, ns)) {
2114 		if (ns->opts.nsid <= cmd->nsid) {
2115 			continue;
2116 		}
2117 
2118 		ns_list->ns_list[count++] = ns->opts.nsid;
2119 		if (count == SPDK_COUNTOF(ns_list->ns_list)) {
2120 			break;
2121 		}
2122 	}
2123 
2124 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2125 }
2126 
2127 static void
2128 _add_ns_id_desc(void **buf_ptr, size_t *buf_remain,
2129 		enum spdk_nvme_nidt type,
2130 		const void *data, size_t data_size)
2131 {
2132 	struct spdk_nvme_ns_id_desc *desc;
2133 	size_t desc_size = sizeof(*desc) + data_size;
2134 
2135 	/*
2136 	 * These should never fail in practice, since all valid NS ID descriptors
2137 	 * should be defined so that they fit in the available 4096-byte buffer.
2138 	 */
2139 	assert(data_size > 0);
2140 	assert(data_size <= UINT8_MAX);
2141 	assert(desc_size < *buf_remain);
2142 	if (data_size == 0 || data_size > UINT8_MAX || desc_size > *buf_remain) {
2143 		return;
2144 	}
2145 
2146 	desc = *buf_ptr;
2147 	desc->nidt = type;
2148 	desc->nidl = data_size;
2149 	memcpy(desc->nid, data, data_size);
2150 
2151 	*buf_ptr += desc_size;
2152 	*buf_remain -= desc_size;
2153 }
2154 
2155 static int
2156 nvmf_ctrlr_identify_ns_id_descriptor_list(
2157 	struct spdk_nvmf_subsystem *subsystem,
2158 	struct spdk_nvme_cmd *cmd,
2159 	struct spdk_nvme_cpl *rsp,
2160 	void *id_desc_list, size_t id_desc_list_size)
2161 {
2162 	struct spdk_nvmf_ns *ns;
2163 	size_t buf_remain = id_desc_list_size;
2164 	void *buf_ptr = id_desc_list;
2165 
2166 	ns = _nvmf_subsystem_get_ns(subsystem, cmd->nsid);
2167 	if (ns == NULL || ns->bdev == NULL) {
2168 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
2169 		rsp->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT;
2170 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2171 	}
2172 
2173 #define ADD_ID_DESC(type, data, size) \
2174 	do { \
2175 		if (!spdk_mem_all_zero(data, size)) { \
2176 			_add_ns_id_desc(&buf_ptr, &buf_remain, type, data, size); \
2177 		} \
2178 	} while (0)
2179 
2180 	ADD_ID_DESC(SPDK_NVME_NIDT_EUI64, ns->opts.eui64, sizeof(ns->opts.eui64));
2181 	ADD_ID_DESC(SPDK_NVME_NIDT_NGUID, ns->opts.nguid, sizeof(ns->opts.nguid));
2182 	ADD_ID_DESC(SPDK_NVME_NIDT_UUID, &ns->opts.uuid, sizeof(ns->opts.uuid));
2183 
2184 	/*
2185 	 * The list is automatically 0-terminated because controller to host buffers in
2186 	 * admin commands always get zeroed in nvmf_ctrlr_process_admin_cmd().
2187 	 */
2188 
2189 #undef ADD_ID_DESC
2190 
2191 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2192 }
2193 
2194 static int
2195 nvmf_ctrlr_identify(struct spdk_nvmf_request *req)
2196 {
2197 	uint8_t cns;
2198 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
2199 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
2200 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
2201 	struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys;
2202 
2203 	if (req->data == NULL || req->length < 4096) {
2204 		SPDK_ERRLOG("identify command with invalid buffer\n");
2205 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
2206 		rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
2207 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2208 	}
2209 
2210 	cns = cmd->cdw10_bits.identify.cns;
2211 
2212 	if (subsystem->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY &&
2213 	    cns != SPDK_NVME_IDENTIFY_CTRLR) {
2214 		/* Discovery controllers only support Identify Controller */
2215 		goto invalid_cns;
2216 	}
2217 
2218 	switch (cns) {
2219 	case SPDK_NVME_IDENTIFY_NS:
2220 		return spdk_nvmf_ctrlr_identify_ns(ctrlr, cmd, rsp, req->data);
2221 	case SPDK_NVME_IDENTIFY_CTRLR:
2222 		return spdk_nvmf_ctrlr_identify_ctrlr(ctrlr, req->data);
2223 	case SPDK_NVME_IDENTIFY_ACTIVE_NS_LIST:
2224 		return nvmf_ctrlr_identify_active_ns_list(subsystem, cmd, rsp, req->data);
2225 	case SPDK_NVME_IDENTIFY_NS_ID_DESCRIPTOR_LIST:
2226 		return nvmf_ctrlr_identify_ns_id_descriptor_list(subsystem, cmd, rsp, req->data, req->length);
2227 	default:
2228 		goto invalid_cns;
2229 	}
2230 
2231 invalid_cns:
2232 	SPDK_ERRLOG("Identify command with unsupported CNS 0x%02x\n", cns);
2233 	rsp->status.sct = SPDK_NVME_SCT_GENERIC;
2234 	rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
2235 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2236 }
2237 
2238 static bool
2239 nvmf_qpair_abort_aer(struct spdk_nvmf_qpair *qpair, uint16_t cid)
2240 {
2241 	struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr;
2242 	struct spdk_nvmf_request *req;
2243 	int i;
2244 
2245 	if (!nvmf_qpair_is_admin_queue(qpair)) {
2246 		return false;
2247 	}
2248 
2249 	for (i = 0; i < ctrlr->nr_aer_reqs; i++) {
2250 		if (ctrlr->aer_req[i]->cmd->nvme_cmd.cid == cid) {
2251 			SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Aborting AER request\n");
2252 			req = ctrlr->aer_req[i];
2253 			ctrlr->aer_req[i] = NULL;
2254 			ctrlr->nr_aer_reqs--;
2255 
2256 			/* Move the last req to the aborting position for making aer_reqs
2257 			 * in continuous
2258 			 */
2259 			if (i < ctrlr->nr_aer_reqs) {
2260 				ctrlr->aer_req[i] = ctrlr->aer_req[ctrlr->nr_aer_reqs];
2261 				ctrlr->aer_req[ctrlr->nr_aer_reqs] = NULL;
2262 			}
2263 
2264 			req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
2265 			req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_ABORTED_BY_REQUEST;
2266 			_nvmf_request_complete(req);
2267 			return true;
2268 		}
2269 	}
2270 
2271 	return false;
2272 }
2273 
2274 static void
2275 nvmf_qpair_abort_request(struct spdk_nvmf_qpair *qpair, struct spdk_nvmf_request *req)
2276 {
2277 	uint16_t cid = req->cmd->nvme_cmd.cdw10_bits.abort.cid;
2278 
2279 	if (nvmf_qpair_abort_aer(qpair, cid)) {
2280 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "abort ctrlr=%p sqid=%u cid=%u successful\n",
2281 			      qpair->ctrlr, qpair->qid, cid);
2282 		req->rsp->nvme_cpl.cdw0 &= ~1U; /* Command successfully aborted */
2283 
2284 		spdk_nvmf_request_complete(req);
2285 		return;
2286 	}
2287 
2288 	nvmf_transport_qpair_abort_request(qpair, req);
2289 }
2290 
2291 static void
2292 nvmf_ctrlr_abort_done(struct spdk_io_channel_iter *i, int status)
2293 {
2294 	struct spdk_nvmf_request *req = spdk_io_channel_iter_get_ctx(i);
2295 
2296 	if (status == 0) {
2297 		/* There was no qpair whose ID matches SQID of the abort command.
2298 		 * Hence call _nvmf_request_complete() here.
2299 		 */
2300 		_nvmf_request_complete(req);
2301 	}
2302 }
2303 
2304 static void
2305 nvmf_ctrlr_abort_on_pg(struct spdk_io_channel_iter *i)
2306 {
2307 	struct spdk_nvmf_request *req = spdk_io_channel_iter_get_ctx(i);
2308 	struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
2309 	struct spdk_nvmf_poll_group *group = spdk_io_channel_get_ctx(ch);
2310 	uint16_t sqid = req->cmd->nvme_cmd.cdw10_bits.abort.sqid;
2311 	struct spdk_nvmf_qpair *qpair;
2312 
2313 	TAILQ_FOREACH(qpair, &group->qpairs, link) {
2314 		if (qpair->ctrlr == req->qpair->ctrlr && qpair->qid == sqid) {
2315 			/* Found the qpair */
2316 
2317 			nvmf_qpair_abort_request(qpair, req);
2318 
2319 			/* Return -1 for the status so the iteration across threads stops. */
2320 			spdk_for_each_channel_continue(i, -1);
2321 			return;
2322 		}
2323 	}
2324 
2325 	spdk_for_each_channel_continue(i, 0);
2326 }
2327 
2328 static int
2329 nvmf_ctrlr_abort(struct spdk_nvmf_request *req)
2330 {
2331 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
2332 
2333 	rsp->cdw0 = 1U; /* Command not aborted */
2334 	rsp->status.sct = SPDK_NVME_SCT_GENERIC;
2335 	rsp->status.sc = SPDK_NVME_SC_SUCCESS;
2336 
2337 	/* Send a message to each poll group, searching for this ctrlr, sqid, and command. */
2338 	spdk_for_each_channel(req->qpair->ctrlr->subsys->tgt,
2339 			      nvmf_ctrlr_abort_on_pg,
2340 			      req,
2341 			      nvmf_ctrlr_abort_done
2342 			     );
2343 
2344 	return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
2345 }
2346 
2347 int
2348 nvmf_ctrlr_abort_request(struct spdk_nvmf_request *req)
2349 {
2350 	struct spdk_nvmf_request *req_to_abort = req->req_to_abort;
2351 	struct spdk_bdev *bdev;
2352 	struct spdk_bdev_desc *desc;
2353 	struct spdk_io_channel *ch;
2354 	int rc;
2355 
2356 	assert(req_to_abort != NULL);
2357 
2358 	if (g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_ABORT].hdlr &&
2359 	    nvmf_qpair_is_admin_queue(req_to_abort->qpair)) {
2360 		return g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_ABORT].hdlr(req);
2361 	}
2362 
2363 	rc = spdk_nvmf_request_get_bdev(req_to_abort->cmd->nvme_cmd.nsid, req_to_abort,
2364 					&bdev, &desc, &ch);
2365 	if (rc != 0) {
2366 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2367 	}
2368 
2369 	return spdk_nvmf_bdev_ctrlr_abort_cmd(bdev, desc, ch, req, req_to_abort);
2370 }
2371 
2372 static int
2373 get_features_generic(struct spdk_nvmf_request *req, uint32_t cdw0)
2374 {
2375 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
2376 
2377 	rsp->cdw0 = cdw0;
2378 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2379 }
2380 
2381 static int
2382 nvmf_ctrlr_get_features(struct spdk_nvmf_request *req)
2383 {
2384 	uint8_t feature;
2385 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
2386 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
2387 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
2388 
2389 	feature = cmd->cdw10_bits.get_features.fid;
2390 	switch (feature) {
2391 	case SPDK_NVME_FEAT_ARBITRATION:
2392 		return get_features_generic(req, ctrlr->feat.arbitration.raw);
2393 	case SPDK_NVME_FEAT_POWER_MANAGEMENT:
2394 		return get_features_generic(req, ctrlr->feat.power_management.raw);
2395 	case SPDK_NVME_FEAT_TEMPERATURE_THRESHOLD:
2396 		return nvmf_ctrlr_get_features_temperature_threshold(req);
2397 	case SPDK_NVME_FEAT_ERROR_RECOVERY:
2398 		return get_features_generic(req, ctrlr->feat.error_recovery.raw);
2399 	case SPDK_NVME_FEAT_VOLATILE_WRITE_CACHE:
2400 		return get_features_generic(req, ctrlr->feat.volatile_write_cache.raw);
2401 	case SPDK_NVME_FEAT_NUMBER_OF_QUEUES:
2402 		return get_features_generic(req, ctrlr->feat.number_of_queues.raw);
2403 	case SPDK_NVME_FEAT_WRITE_ATOMICITY:
2404 		return get_features_generic(req, ctrlr->feat.write_atomicity.raw);
2405 	case SPDK_NVME_FEAT_ASYNC_EVENT_CONFIGURATION:
2406 		return get_features_generic(req, ctrlr->feat.async_event_configuration.raw);
2407 	case SPDK_NVME_FEAT_KEEP_ALIVE_TIMER:
2408 		return get_features_generic(req, ctrlr->feat.keep_alive_timer.raw);
2409 	case SPDK_NVME_FEAT_HOST_IDENTIFIER:
2410 		return nvmf_ctrlr_get_features_host_identifier(req);
2411 	case SPDK_NVME_FEAT_HOST_RESERVE_MASK:
2412 		return nvmf_ctrlr_get_features_reservation_notification_mask(req);
2413 	case SPDK_NVME_FEAT_HOST_RESERVE_PERSIST:
2414 		return nvmf_ctrlr_get_features_reservation_persistence(req);
2415 	default:
2416 		SPDK_ERRLOG("Get Features command with unsupported feature ID 0x%02x\n", feature);
2417 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
2418 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2419 	}
2420 }
2421 
2422 static int
2423 nvmf_ctrlr_set_features(struct spdk_nvmf_request *req)
2424 {
2425 	uint8_t feature, save;
2426 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
2427 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
2428 
2429 	/*
2430 	 * Features are not saveable by the controller as indicated by
2431 	 * ONCS field of the Identify Controller data.
2432 	 * */
2433 	save = cmd->cdw10_bits.set_features.sv;
2434 	if (save) {
2435 		response->status.sc = SPDK_NVME_SC_FEATURE_ID_NOT_SAVEABLE;
2436 		response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
2437 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2438 	}
2439 
2440 	feature = cmd->cdw10_bits.set_features.fid;
2441 	switch (feature) {
2442 	case SPDK_NVME_FEAT_ARBITRATION:
2443 		return nvmf_ctrlr_set_features_arbitration(req);
2444 	case SPDK_NVME_FEAT_POWER_MANAGEMENT:
2445 		return nvmf_ctrlr_set_features_power_management(req);
2446 	case SPDK_NVME_FEAT_TEMPERATURE_THRESHOLD:
2447 		return nvmf_ctrlr_set_features_temperature_threshold(req);
2448 	case SPDK_NVME_FEAT_ERROR_RECOVERY:
2449 		return nvmf_ctrlr_set_features_error_recovery(req);
2450 	case SPDK_NVME_FEAT_VOLATILE_WRITE_CACHE:
2451 		return nvmf_ctrlr_set_features_volatile_write_cache(req);
2452 	case SPDK_NVME_FEAT_NUMBER_OF_QUEUES:
2453 		return nvmf_ctrlr_set_features_number_of_queues(req);
2454 	case SPDK_NVME_FEAT_WRITE_ATOMICITY:
2455 		return nvmf_ctrlr_set_features_write_atomicity(req);
2456 	case SPDK_NVME_FEAT_ASYNC_EVENT_CONFIGURATION:
2457 		return nvmf_ctrlr_set_features_async_event_configuration(req);
2458 	case SPDK_NVME_FEAT_KEEP_ALIVE_TIMER:
2459 		return nvmf_ctrlr_set_features_keep_alive_timer(req);
2460 	case SPDK_NVME_FEAT_HOST_IDENTIFIER:
2461 		return nvmf_ctrlr_set_features_host_identifier(req);
2462 	case SPDK_NVME_FEAT_HOST_RESERVE_MASK:
2463 		return nvmf_ctrlr_set_features_reservation_notification_mask(req);
2464 	case SPDK_NVME_FEAT_HOST_RESERVE_PERSIST:
2465 		return nvmf_ctrlr_set_features_reservation_persistence(req);
2466 	default:
2467 		SPDK_ERRLOG("Set Features command with unsupported feature ID 0x%02x\n", feature);
2468 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
2469 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2470 	}
2471 }
2472 
2473 static int
2474 nvmf_ctrlr_keep_alive(struct spdk_nvmf_request *req)
2475 {
2476 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
2477 
2478 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Keep Alive\n");
2479 	/*
2480 	 * To handle keep alive just clear or reset the
2481 	 * ctrlr based keep alive duration counter.
2482 	 * When added, a separate timer based process
2483 	 * will monitor if the time since last recorded
2484 	 * keep alive has exceeded the max duration and
2485 	 * take appropriate action.
2486 	 */
2487 	ctrlr->last_keep_alive_tick = spdk_get_ticks();
2488 
2489 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2490 }
2491 
2492 int
2493 nvmf_ctrlr_process_admin_cmd(struct spdk_nvmf_request *req)
2494 {
2495 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
2496 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
2497 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
2498 	int rc;
2499 
2500 	if (ctrlr == NULL) {
2501 		SPDK_ERRLOG("Admin command sent before CONNECT\n");
2502 		response->status.sct = SPDK_NVME_SCT_GENERIC;
2503 		response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
2504 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2505 	}
2506 
2507 	if (ctrlr->vcprop.cc.bits.en != 1) {
2508 		SPDK_ERRLOG("Admin command sent to disabled controller\n");
2509 		response->status.sct = SPDK_NVME_SCT_GENERIC;
2510 		response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
2511 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2512 	}
2513 
2514 	if (req->data && spdk_nvme_opc_get_data_transfer(cmd->opc) == SPDK_NVME_DATA_CONTROLLER_TO_HOST) {
2515 		memset(req->data, 0, req->length);
2516 	}
2517 
2518 	if (ctrlr->subsys->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) {
2519 		/* Discovery controllers only support Get Log Page, Identify and Keep Alive. */
2520 		switch (cmd->opc) {
2521 		case SPDK_NVME_OPC_IDENTIFY:
2522 		case SPDK_NVME_OPC_GET_LOG_PAGE:
2523 		case SPDK_NVME_OPC_KEEP_ALIVE:
2524 			break;
2525 		default:
2526 			goto invalid_opcode;
2527 		}
2528 	}
2529 
2530 	/* Call a custom adm cmd handler if set. Aborts are handled in a different path (see nvmf_passthru_admin_cmd) */
2531 	if (g_nvmf_custom_admin_cmd_hdlrs[cmd->opc].hdlr && cmd->opc != SPDK_NVME_OPC_ABORT) {
2532 		rc = g_nvmf_custom_admin_cmd_hdlrs[cmd->opc].hdlr(req);
2533 		if (rc >= SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE) {
2534 			/* The handler took care of this commmand */
2535 			return rc;
2536 		}
2537 	}
2538 
2539 	switch (cmd->opc) {
2540 	case SPDK_NVME_OPC_GET_LOG_PAGE:
2541 		return nvmf_ctrlr_get_log_page(req);
2542 	case SPDK_NVME_OPC_IDENTIFY:
2543 		return nvmf_ctrlr_identify(req);
2544 	case SPDK_NVME_OPC_ABORT:
2545 		return nvmf_ctrlr_abort(req);
2546 	case SPDK_NVME_OPC_GET_FEATURES:
2547 		return nvmf_ctrlr_get_features(req);
2548 	case SPDK_NVME_OPC_SET_FEATURES:
2549 		return nvmf_ctrlr_set_features(req);
2550 	case SPDK_NVME_OPC_ASYNC_EVENT_REQUEST:
2551 		return nvmf_ctrlr_async_event_request(req);
2552 	case SPDK_NVME_OPC_KEEP_ALIVE:
2553 		return nvmf_ctrlr_keep_alive(req);
2554 
2555 	case SPDK_NVME_OPC_CREATE_IO_SQ:
2556 	case SPDK_NVME_OPC_CREATE_IO_CQ:
2557 	case SPDK_NVME_OPC_DELETE_IO_SQ:
2558 	case SPDK_NVME_OPC_DELETE_IO_CQ:
2559 		/* Create and Delete I/O CQ/SQ not allowed in NVMe-oF */
2560 		goto invalid_opcode;
2561 
2562 	default:
2563 		goto invalid_opcode;
2564 	}
2565 
2566 invalid_opcode:
2567 	SPDK_ERRLOG("Unsupported admin opcode 0x%x\n", cmd->opc);
2568 	response->status.sct = SPDK_NVME_SCT_GENERIC;
2569 	response->status.sc = SPDK_NVME_SC_INVALID_OPCODE;
2570 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2571 }
2572 
2573 int
2574 nvmf_ctrlr_process_fabrics_cmd(struct spdk_nvmf_request *req)
2575 {
2576 	struct spdk_nvmf_qpair *qpair = req->qpair;
2577 	struct spdk_nvmf_capsule_cmd *cap_hdr;
2578 
2579 	cap_hdr = &req->cmd->nvmf_cmd;
2580 
2581 	if (qpair->ctrlr == NULL) {
2582 		/* No ctrlr established yet; the only valid command is Connect */
2583 		if (cap_hdr->fctype == SPDK_NVMF_FABRIC_COMMAND_CONNECT) {
2584 			return nvmf_ctrlr_cmd_connect(req);
2585 		} else {
2586 			SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Got fctype 0x%x, expected Connect\n",
2587 				      cap_hdr->fctype);
2588 			req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
2589 			req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
2590 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2591 		}
2592 	} else if (nvmf_qpair_is_admin_queue(qpair)) {
2593 		/*
2594 		 * Controller session is established, and this is an admin queue.
2595 		 * Disallow Connect and allow other fabrics commands.
2596 		 */
2597 		switch (cap_hdr->fctype) {
2598 		case SPDK_NVMF_FABRIC_COMMAND_PROPERTY_SET:
2599 			return nvmf_property_set(req);
2600 		case SPDK_NVMF_FABRIC_COMMAND_PROPERTY_GET:
2601 			return nvmf_property_get(req);
2602 		default:
2603 			SPDK_DEBUGLOG(SPDK_LOG_NVMF, "unknown fctype 0x%02x\n",
2604 				      cap_hdr->fctype);
2605 			req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
2606 			req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_INVALID_OPCODE;
2607 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2608 		}
2609 	} else {
2610 		/* Controller session is established, and this is an I/O queue */
2611 		/* For now, no I/O-specific Fabrics commands are implemented (other than Connect) */
2612 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Unexpected I/O fctype 0x%x\n", cap_hdr->fctype);
2613 		req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
2614 		req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_INVALID_OPCODE;
2615 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2616 	}
2617 }
2618 
2619 static inline int
2620 nvmf_ctrlr_async_event_notification(struct spdk_nvmf_ctrlr *ctrlr,
2621 				    union spdk_nvme_async_event_completion *event)
2622 {
2623 	struct spdk_nvmf_request *req;
2624 	struct spdk_nvme_cpl *rsp;
2625 
2626 	assert(ctrlr->nr_aer_reqs > 0);
2627 
2628 	req = ctrlr->aer_req[--ctrlr->nr_aer_reqs];
2629 	rsp = &req->rsp->nvme_cpl;
2630 
2631 	rsp->cdw0 = event->raw;
2632 
2633 	_nvmf_request_complete(req);
2634 	ctrlr->aer_req[ctrlr->nr_aer_reqs] = NULL;
2635 
2636 	return 0;
2637 }
2638 
2639 int
2640 nvmf_ctrlr_async_event_ns_notice(struct spdk_nvmf_ctrlr *ctrlr)
2641 {
2642 	union spdk_nvme_async_event_completion event = {0};
2643 
2644 	/* Users may disable the event notification */
2645 	if (!ctrlr->feat.async_event_configuration.bits.ns_attr_notice) {
2646 		return 0;
2647 	}
2648 
2649 	event.bits.async_event_type = SPDK_NVME_ASYNC_EVENT_TYPE_NOTICE;
2650 	event.bits.async_event_info = SPDK_NVME_ASYNC_EVENT_NS_ATTR_CHANGED;
2651 	event.bits.log_page_identifier = SPDK_NVME_LOG_CHANGED_NS_LIST;
2652 
2653 	/* If there is no outstanding AER request, queue the event.  Then
2654 	 * if an AER is later submitted, this event can be sent as a
2655 	 * response.
2656 	 */
2657 	if (ctrlr->nr_aer_reqs == 0) {
2658 		if (ctrlr->notice_event.bits.async_event_type ==
2659 		    SPDK_NVME_ASYNC_EVENT_TYPE_NOTICE) {
2660 			return 0;
2661 		}
2662 
2663 		ctrlr->notice_event.raw = event.raw;
2664 		return 0;
2665 	}
2666 
2667 	return nvmf_ctrlr_async_event_notification(ctrlr, &event);
2668 }
2669 
2670 void
2671 nvmf_ctrlr_async_event_reservation_notification(struct spdk_nvmf_ctrlr *ctrlr)
2672 {
2673 	union spdk_nvme_async_event_completion event = {0};
2674 
2675 	if (!ctrlr->num_avail_log_pages) {
2676 		return;
2677 	}
2678 	event.bits.async_event_type = SPDK_NVME_ASYNC_EVENT_TYPE_IO;
2679 	event.bits.async_event_info = SPDK_NVME_ASYNC_EVENT_RESERVATION_LOG_AVAIL;
2680 	event.bits.log_page_identifier = SPDK_NVME_LOG_RESERVATION_NOTIFICATION;
2681 
2682 	/* If there is no outstanding AER request, queue the event.  Then
2683 	 * if an AER is later submitted, this event can be sent as a
2684 	 * response.
2685 	 */
2686 	if (ctrlr->nr_aer_reqs == 0) {
2687 		if (ctrlr->reservation_event.bits.async_event_type ==
2688 		    SPDK_NVME_ASYNC_EVENT_TYPE_IO) {
2689 			return;
2690 		}
2691 
2692 		ctrlr->reservation_event.raw = event.raw;
2693 		return;
2694 	}
2695 
2696 	nvmf_ctrlr_async_event_notification(ctrlr, &event);
2697 }
2698 
2699 void
2700 nvmf_qpair_free_aer(struct spdk_nvmf_qpair *qpair)
2701 {
2702 	struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr;
2703 	int i;
2704 
2705 	if (!nvmf_qpair_is_admin_queue(qpair)) {
2706 		return;
2707 	}
2708 
2709 	for (i = 0; i < ctrlr->nr_aer_reqs; i++) {
2710 		spdk_nvmf_request_free(ctrlr->aer_req[i]);
2711 		ctrlr->aer_req[i] = NULL;
2712 	}
2713 
2714 	ctrlr->nr_aer_reqs = 0;
2715 }
2716 
2717 void
2718 nvmf_ctrlr_abort_aer(struct spdk_nvmf_ctrlr *ctrlr)
2719 {
2720 	struct spdk_nvmf_request *req;
2721 	int i;
2722 
2723 	for (i = 0; i < ctrlr->nr_aer_reqs; i++) {
2724 		req = ctrlr->aer_req[i];
2725 
2726 		req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
2727 		req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_ABORTED_BY_REQUEST;
2728 		_nvmf_request_complete(req);
2729 
2730 		ctrlr->aer_req[i] = NULL;
2731 	}
2732 
2733 	ctrlr->nr_aer_reqs = 0;
2734 }
2735 
2736 static void
2737 _nvmf_ctrlr_add_reservation_log(void *ctx)
2738 {
2739 	struct spdk_nvmf_reservation_log *log = (struct spdk_nvmf_reservation_log *)ctx;
2740 	struct spdk_nvmf_ctrlr *ctrlr = log->ctrlr;
2741 
2742 	ctrlr->log_page_count++;
2743 
2744 	/* Maximum number of queued log pages is 255 */
2745 	if (ctrlr->num_avail_log_pages == 0xff) {
2746 		struct spdk_nvmf_reservation_log *entry;
2747 		entry = TAILQ_LAST(&ctrlr->log_head, log_page_head);
2748 		entry->log.log_page_count = ctrlr->log_page_count;
2749 		free(log);
2750 		return;
2751 	}
2752 
2753 	log->log.log_page_count = ctrlr->log_page_count;
2754 	log->log.num_avail_log_pages = ctrlr->num_avail_log_pages++;
2755 	TAILQ_INSERT_TAIL(&ctrlr->log_head, log, link);
2756 
2757 	nvmf_ctrlr_async_event_reservation_notification(ctrlr);
2758 }
2759 
2760 void
2761 nvmf_ctrlr_reservation_notice_log(struct spdk_nvmf_ctrlr *ctrlr,
2762 				  struct spdk_nvmf_ns *ns,
2763 				  enum spdk_nvme_reservation_notification_log_page_type type)
2764 {
2765 	struct spdk_nvmf_reservation_log *log;
2766 
2767 	switch (type) {
2768 	case SPDK_NVME_RESERVATION_LOG_PAGE_EMPTY:
2769 		return;
2770 	case SPDK_NVME_REGISTRATION_PREEMPTED:
2771 		if (ns->mask & SPDK_NVME_REGISTRATION_PREEMPTED_MASK) {
2772 			return;
2773 		}
2774 		break;
2775 	case SPDK_NVME_RESERVATION_RELEASED:
2776 		if (ns->mask & SPDK_NVME_RESERVATION_RELEASED_MASK) {
2777 			return;
2778 		}
2779 		break;
2780 	case SPDK_NVME_RESERVATION_PREEMPTED:
2781 		if (ns->mask & SPDK_NVME_RESERVATION_PREEMPTED_MASK) {
2782 			return;
2783 		}
2784 		break;
2785 	default:
2786 		return;
2787 	}
2788 
2789 	log = calloc(1, sizeof(*log));
2790 	if (!log) {
2791 		SPDK_ERRLOG("Alloc log page failed, ignore the log\n");
2792 		return;
2793 	}
2794 	log->ctrlr = ctrlr;
2795 	log->log.type = type;
2796 	log->log.nsid = ns->nsid;
2797 
2798 	spdk_thread_send_msg(ctrlr->thread, _nvmf_ctrlr_add_reservation_log, log);
2799 }
2800 
2801 /* Check from subsystem poll group's namespace information data structure */
2802 static bool
2803 nvmf_ns_info_ctrlr_is_registrant(struct spdk_nvmf_subsystem_pg_ns_info *ns_info,
2804 				 struct spdk_nvmf_ctrlr *ctrlr)
2805 {
2806 	uint32_t i;
2807 
2808 	for (i = 0; i < SPDK_NVMF_MAX_NUM_REGISTRANTS; i++) {
2809 		if (!spdk_uuid_compare(&ns_info->reg_hostid[i], &ctrlr->hostid)) {
2810 			return true;
2811 		}
2812 	}
2813 
2814 	return false;
2815 }
2816 
2817 /*
2818  * Check the NVMe command is permitted or not for current controller(Host).
2819  */
2820 static int
2821 nvmf_ns_reservation_request_check(struct spdk_nvmf_subsystem_pg_ns_info *ns_info,
2822 				  struct spdk_nvmf_ctrlr *ctrlr,
2823 				  struct spdk_nvmf_request *req)
2824 {
2825 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
2826 	enum spdk_nvme_reservation_type rtype = ns_info->rtype;
2827 	uint8_t status = SPDK_NVME_SC_SUCCESS;
2828 	uint8_t racqa;
2829 	bool is_registrant;
2830 
2831 	/* No valid reservation */
2832 	if (!rtype) {
2833 		return 0;
2834 	}
2835 
2836 	is_registrant = nvmf_ns_info_ctrlr_is_registrant(ns_info, ctrlr);
2837 	/* All registrants type and current ctrlr is a valid registrant */
2838 	if ((rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_ALL_REGS ||
2839 	     rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_ALL_REGS) && is_registrant) {
2840 		return 0;
2841 	} else if (!spdk_uuid_compare(&ns_info->holder_id, &ctrlr->hostid)) {
2842 		return 0;
2843 	}
2844 
2845 	/* Non-holder for current controller */
2846 	switch (cmd->opc) {
2847 	case SPDK_NVME_OPC_READ:
2848 	case SPDK_NVME_OPC_COMPARE:
2849 		if (rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS) {
2850 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2851 			goto exit;
2852 		}
2853 		if ((rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_REG_ONLY ||
2854 		     rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_ALL_REGS) && !is_registrant) {
2855 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2856 		}
2857 		break;
2858 	case SPDK_NVME_OPC_FLUSH:
2859 	case SPDK_NVME_OPC_WRITE:
2860 	case SPDK_NVME_OPC_WRITE_UNCORRECTABLE:
2861 	case SPDK_NVME_OPC_WRITE_ZEROES:
2862 	case SPDK_NVME_OPC_DATASET_MANAGEMENT:
2863 		if (rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE ||
2864 		    rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS) {
2865 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2866 			goto exit;
2867 		}
2868 		if (!is_registrant) {
2869 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2870 		}
2871 		break;
2872 	case SPDK_NVME_OPC_RESERVATION_ACQUIRE:
2873 		racqa = cmd->cdw10_bits.resv_acquire.racqa;
2874 		if (racqa == SPDK_NVME_RESERVE_ACQUIRE) {
2875 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2876 			goto exit;
2877 		}
2878 		if (!is_registrant) {
2879 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2880 		}
2881 		break;
2882 	case SPDK_NVME_OPC_RESERVATION_RELEASE:
2883 		if (!is_registrant) {
2884 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2885 		}
2886 		break;
2887 	default:
2888 		break;
2889 	}
2890 
2891 exit:
2892 	req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
2893 	req->rsp->nvme_cpl.status.sc = status;
2894 	if (status == SPDK_NVME_SC_RESERVATION_CONFLICT) {
2895 		return -EPERM;
2896 	}
2897 
2898 	return 0;
2899 }
2900 
2901 static int
2902 nvmf_ctrlr_process_io_fused_cmd(struct spdk_nvmf_request *req, struct spdk_bdev *bdev,
2903 				struct spdk_bdev_desc *desc, struct spdk_io_channel *ch)
2904 {
2905 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
2906 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
2907 	struct spdk_nvmf_request *first_fused_req = req->qpair->first_fused_req;
2908 	int rc;
2909 
2910 	if (cmd->fuse == SPDK_NVME_CMD_FUSE_FIRST) {
2911 		/* first fused operation (should be compare) */
2912 		if (first_fused_req != NULL) {
2913 			struct spdk_nvme_cpl *fused_response = &first_fused_req->rsp->nvme_cpl;
2914 
2915 			SPDK_ERRLOG("Wrong sequence of fused operations\n");
2916 
2917 			/* abort req->qpair->first_fused_request and continue with new fused command */
2918 			fused_response->status.sc = SPDK_NVME_SC_ABORTED_MISSING_FUSED;
2919 			fused_response->status.sct = SPDK_NVME_SCT_GENERIC;
2920 			_nvmf_request_complete(first_fused_req);
2921 		} else if (cmd->opc != SPDK_NVME_OPC_COMPARE) {
2922 			SPDK_ERRLOG("Wrong op code of fused operations\n");
2923 			rsp->status.sct = SPDK_NVME_SCT_GENERIC;
2924 			rsp->status.sc = SPDK_NVME_SC_INVALID_OPCODE;
2925 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2926 		}
2927 
2928 		req->qpair->first_fused_req = req;
2929 		return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
2930 	} else if (cmd->fuse == SPDK_NVME_CMD_FUSE_SECOND) {
2931 		/* second fused operation (should be write) */
2932 		if (first_fused_req == NULL) {
2933 			SPDK_ERRLOG("Wrong sequence of fused operations\n");
2934 			rsp->status.sct = SPDK_NVME_SCT_GENERIC;
2935 			rsp->status.sc = SPDK_NVME_SC_ABORTED_MISSING_FUSED;
2936 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2937 		} else if (cmd->opc != SPDK_NVME_OPC_WRITE) {
2938 			struct spdk_nvme_cpl *fused_response = &first_fused_req->rsp->nvme_cpl;
2939 
2940 			SPDK_ERRLOG("Wrong op code of fused operations\n");
2941 
2942 			/* abort req->qpair->first_fused_request and fail current command */
2943 			fused_response->status.sc = SPDK_NVME_SC_ABORTED_MISSING_FUSED;
2944 			fused_response->status.sct = SPDK_NVME_SCT_GENERIC;
2945 			_nvmf_request_complete(first_fused_req);
2946 
2947 			rsp->status.sct = SPDK_NVME_SCT_GENERIC;
2948 			rsp->status.sc = SPDK_NVME_SC_INVALID_OPCODE;
2949 			req->qpair->first_fused_req = NULL;
2950 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2951 		}
2952 
2953 		/* save request of first command to generate response later */
2954 		req->first_fused_req = first_fused_req;
2955 		req->qpair->first_fused_req = NULL;
2956 	} else {
2957 		SPDK_ERRLOG("Invalid fused command fuse field.\n");
2958 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
2959 		rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
2960 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2961 	}
2962 
2963 	rc = nvmf_bdev_ctrlr_compare_and_write_cmd(bdev, desc, ch, req->first_fused_req, req);
2964 
2965 	if (rc == SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE) {
2966 		if (spdk_nvme_cpl_is_error(rsp)) {
2967 			struct spdk_nvme_cpl *fused_response = &first_fused_req->rsp->nvme_cpl;
2968 
2969 			fused_response->status = rsp->status;
2970 			rsp->status.sct = SPDK_NVME_SCT_GENERIC;
2971 			rsp->status.sc = SPDK_NVME_SC_ABORTED_FAILED_FUSED;
2972 			/* Complete first of fused commands. Second will be completed by upper layer */
2973 			_nvmf_request_complete(first_fused_req);
2974 			req->first_fused_req = NULL;
2975 		}
2976 	}
2977 
2978 	return rc;
2979 }
2980 
2981 int
2982 nvmf_ctrlr_process_io_cmd(struct spdk_nvmf_request *req)
2983 {
2984 	uint32_t nsid;
2985 	struct spdk_nvmf_ns *ns;
2986 	struct spdk_bdev *bdev;
2987 	struct spdk_bdev_desc *desc;
2988 	struct spdk_io_channel *ch;
2989 	struct spdk_nvmf_poll_group *group = req->qpair->group;
2990 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
2991 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
2992 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
2993 	struct spdk_nvmf_subsystem_pg_ns_info *ns_info;
2994 
2995 	/* pre-set response details for this command */
2996 	response->status.sc = SPDK_NVME_SC_SUCCESS;
2997 	nsid = cmd->nsid;
2998 
2999 	if (spdk_unlikely(ctrlr == NULL)) {
3000 		SPDK_ERRLOG("I/O command sent before CONNECT\n");
3001 		response->status.sct = SPDK_NVME_SCT_GENERIC;
3002 		response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
3003 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
3004 	}
3005 
3006 	if (spdk_unlikely(ctrlr->vcprop.cc.bits.en != 1)) {
3007 		SPDK_ERRLOG("I/O command sent to disabled controller\n");
3008 		response->status.sct = SPDK_NVME_SCT_GENERIC;
3009 		response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
3010 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
3011 	}
3012 
3013 	ns = _nvmf_subsystem_get_ns(ctrlr->subsys, nsid);
3014 	if (ns == NULL || ns->bdev == NULL) {
3015 		SPDK_ERRLOG("Unsuccessful query for nsid %u\n", cmd->nsid);
3016 		response->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT;
3017 		response->status.dnr = 1;
3018 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
3019 	}
3020 
3021 	/* scan-build falsely reporting dereference of null pointer */
3022 	assert(group != NULL && group->sgroups != NULL);
3023 	ns_info = &group->sgroups[ctrlr->subsys->id].ns_info[nsid - 1];
3024 	if (nvmf_ns_reservation_request_check(ns_info, ctrlr, req)) {
3025 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Reservation Conflict for nsid %u, opcode %u\n",
3026 			      cmd->nsid, cmd->opc);
3027 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
3028 	}
3029 
3030 	bdev = ns->bdev;
3031 	desc = ns->desc;
3032 	ch = ns_info->channel;
3033 
3034 	if (spdk_unlikely(cmd->fuse & SPDK_NVME_CMD_FUSE_MASK)) {
3035 		return nvmf_ctrlr_process_io_fused_cmd(req, bdev, desc, ch);
3036 	} else if (spdk_unlikely(req->qpair->first_fused_req != NULL)) {
3037 		struct spdk_nvme_cpl *fused_response = &req->qpair->first_fused_req->rsp->nvme_cpl;
3038 
3039 		SPDK_ERRLOG("Expected second of fused commands - failing first of fused commands\n");
3040 
3041 		/* abort req->qpair->first_fused_request and continue with new command */
3042 		fused_response->status.sc = SPDK_NVME_SC_ABORTED_MISSING_FUSED;
3043 		fused_response->status.sct = SPDK_NVME_SCT_GENERIC;
3044 		_nvmf_request_complete(req->qpair->first_fused_req);
3045 		req->qpair->first_fused_req = NULL;
3046 	}
3047 
3048 	switch (cmd->opc) {
3049 	case SPDK_NVME_OPC_READ:
3050 		return nvmf_bdev_ctrlr_read_cmd(bdev, desc, ch, req);
3051 	case SPDK_NVME_OPC_WRITE:
3052 		return nvmf_bdev_ctrlr_write_cmd(bdev, desc, ch, req);
3053 	case SPDK_NVME_OPC_COMPARE:
3054 		return nvmf_bdev_ctrlr_compare_cmd(bdev, desc, ch, req);
3055 	case SPDK_NVME_OPC_WRITE_ZEROES:
3056 		return nvmf_bdev_ctrlr_write_zeroes_cmd(bdev, desc, ch, req);
3057 	case SPDK_NVME_OPC_FLUSH:
3058 		return nvmf_bdev_ctrlr_flush_cmd(bdev, desc, ch, req);
3059 	case SPDK_NVME_OPC_DATASET_MANAGEMENT:
3060 		return nvmf_bdev_ctrlr_dsm_cmd(bdev, desc, ch, req);
3061 	case SPDK_NVME_OPC_RESERVATION_REGISTER:
3062 	case SPDK_NVME_OPC_RESERVATION_ACQUIRE:
3063 	case SPDK_NVME_OPC_RESERVATION_RELEASE:
3064 	case SPDK_NVME_OPC_RESERVATION_REPORT:
3065 		spdk_thread_send_msg(ctrlr->subsys->thread, nvmf_ns_reservation_request, req);
3066 		return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
3067 	default:
3068 		return nvmf_bdev_ctrlr_nvme_passthru_io(bdev, desc, ch, req);
3069 	}
3070 }
3071 
3072 static void
3073 nvmf_qpair_request_cleanup(struct spdk_nvmf_qpair *qpair)
3074 {
3075 	if (qpair->state == SPDK_NVMF_QPAIR_DEACTIVATING) {
3076 		assert(qpair->state_cb != NULL);
3077 
3078 		if (TAILQ_EMPTY(&qpair->outstanding)) {
3079 			qpair->state_cb(qpair->state_cb_arg, 0);
3080 		}
3081 	}
3082 }
3083 
3084 int
3085 spdk_nvmf_request_free(struct spdk_nvmf_request *req)
3086 {
3087 	struct spdk_nvmf_qpair *qpair = req->qpair;
3088 
3089 	TAILQ_REMOVE(&qpair->outstanding, req, link);
3090 	if (nvmf_transport_req_free(req)) {
3091 		SPDK_ERRLOG("Unable to free transport level request resources.\n");
3092 	}
3093 
3094 	nvmf_qpair_request_cleanup(qpair);
3095 
3096 	return 0;
3097 }
3098 
3099 static void
3100 _nvmf_request_complete(void *ctx)
3101 {
3102 	struct spdk_nvmf_request *req = ctx;
3103 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
3104 	struct spdk_nvmf_qpair *qpair;
3105 	struct spdk_nvmf_subsystem_poll_group *sgroup = NULL;
3106 	bool is_aer = false;
3107 
3108 	rsp->sqid = 0;
3109 	rsp->status.p = 0;
3110 	rsp->cid = req->cmd->nvme_cmd.cid;
3111 
3112 	qpair = req->qpair;
3113 	if (qpair->ctrlr) {
3114 		sgroup = &qpair->group->sgroups[qpair->ctrlr->subsys->id];
3115 		assert(sgroup != NULL);
3116 		is_aer = req->cmd->nvme_cmd.opc == SPDK_NVME_OPC_ASYNC_EVENT_REQUEST;
3117 	} else if (spdk_unlikely(nvmf_request_is_fabric_connect(req))) {
3118 		sgroup = nvmf_subsystem_pg_from_connect_cmd(req);
3119 	}
3120 
3121 	if (SPDK_DEBUGLOG_FLAG_ENABLED("nvmf")) {
3122 		spdk_nvme_print_completion(qpair->qid, rsp);
3123 	}
3124 
3125 	TAILQ_REMOVE(&qpair->outstanding, req, link);
3126 	if (nvmf_transport_req_complete(req)) {
3127 		SPDK_ERRLOG("Transport request completion error!\n");
3128 	}
3129 
3130 	/* AER cmd is an exception */
3131 	if (sgroup && !is_aer) {
3132 		assert(sgroup->io_outstanding > 0);
3133 		sgroup->io_outstanding--;
3134 		if (sgroup->state == SPDK_NVMF_SUBSYSTEM_PAUSING &&
3135 		    sgroup->io_outstanding == 0) {
3136 			sgroup->state = SPDK_NVMF_SUBSYSTEM_PAUSED;
3137 			sgroup->cb_fn(sgroup->cb_arg, 0);
3138 		}
3139 	}
3140 
3141 	nvmf_qpair_request_cleanup(qpair);
3142 }
3143 
3144 int
3145 spdk_nvmf_request_complete(struct spdk_nvmf_request *req)
3146 {
3147 	struct spdk_nvmf_qpair *qpair = req->qpair;
3148 
3149 	if (spdk_likely(qpair->group->thread == spdk_get_thread())) {
3150 		_nvmf_request_complete(req);
3151 	} else {
3152 		spdk_thread_send_msg(qpair->group->thread,
3153 				     _nvmf_request_complete, req);
3154 	}
3155 
3156 	return 0;
3157 }
3158 
3159 static void
3160 _nvmf_request_exec(struct spdk_nvmf_request *req,
3161 		   struct spdk_nvmf_subsystem_poll_group *sgroup)
3162 {
3163 	struct spdk_nvmf_qpair *qpair = req->qpair;
3164 	enum spdk_nvmf_request_exec_status status;
3165 
3166 	if (SPDK_DEBUGLOG_FLAG_ENABLED("nvmf")) {
3167 		spdk_nvme_print_command(qpair->qid, &req->cmd->nvme_cmd);
3168 	}
3169 
3170 	if (sgroup) {
3171 		sgroup->io_outstanding++;
3172 	}
3173 
3174 	/* Place the request on the outstanding list so we can keep track of it */
3175 	TAILQ_INSERT_TAIL(&qpair->outstanding, req, link);
3176 
3177 	if (spdk_unlikely(req->cmd->nvmf_cmd.opcode == SPDK_NVME_OPC_FABRIC)) {
3178 		status = nvmf_ctrlr_process_fabrics_cmd(req);
3179 	} else if (spdk_unlikely(nvmf_qpair_is_admin_queue(qpair))) {
3180 		status = nvmf_ctrlr_process_admin_cmd(req);
3181 	} else {
3182 		status = nvmf_ctrlr_process_io_cmd(req);
3183 	}
3184 
3185 	if (status == SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE) {
3186 		_nvmf_request_complete(req);
3187 	}
3188 }
3189 
3190 void
3191 spdk_nvmf_request_exec_fabrics(struct spdk_nvmf_request *req)
3192 {
3193 	struct spdk_nvmf_qpair *qpair = req->qpair;
3194 	struct spdk_nvmf_subsystem_poll_group *sgroup = NULL;
3195 
3196 	assert(req->cmd->nvmf_cmd.opcode == SPDK_NVME_OPC_FABRIC);
3197 
3198 	if (qpair->ctrlr) {
3199 		sgroup = &qpair->group->sgroups[qpair->ctrlr->subsys->id];
3200 		assert(sgroup != NULL);
3201 	} else {
3202 		sgroup = nvmf_subsystem_pg_from_connect_cmd(req);
3203 	}
3204 
3205 	_nvmf_request_exec(req, sgroup);
3206 }
3207 
3208 void
3209 spdk_nvmf_request_exec(struct spdk_nvmf_request *req)
3210 {
3211 	struct spdk_nvmf_qpair *qpair = req->qpair;
3212 	struct spdk_nvmf_subsystem_poll_group *sgroup = NULL;
3213 
3214 	if (qpair->ctrlr) {
3215 		sgroup = &qpair->group->sgroups[qpair->ctrlr->subsys->id];
3216 		assert(sgroup != NULL);
3217 	} else if (spdk_unlikely(nvmf_request_is_fabric_connect(req))) {
3218 		sgroup = nvmf_subsystem_pg_from_connect_cmd(req);
3219 	}
3220 
3221 	if (qpair->state != SPDK_NVMF_QPAIR_ACTIVE) {
3222 		req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
3223 		req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
3224 		/* Place the request on the outstanding list so we can keep track of it */
3225 		TAILQ_INSERT_TAIL(&qpair->outstanding, req, link);
3226 		/* Still increment io_outstanding because request_complete decrements it */
3227 		if (sgroup != NULL) {
3228 			sgroup->io_outstanding++;
3229 		}
3230 		_nvmf_request_complete(req);
3231 		return;
3232 	}
3233 
3234 	/* Check if the subsystem is paused (if there is a subsystem) */
3235 	if (sgroup != NULL) {
3236 		if (sgroup->state != SPDK_NVMF_SUBSYSTEM_ACTIVE) {
3237 			/* The subsystem is not currently active. Queue this request. */
3238 			TAILQ_INSERT_TAIL(&sgroup->queued, req, link);
3239 			return;
3240 		}
3241 	}
3242 
3243 	_nvmf_request_exec(req, sgroup);
3244 }
3245 
3246 static bool
3247 nvmf_ctrlr_get_dif_ctx(struct spdk_nvmf_ctrlr *ctrlr, struct spdk_nvme_cmd *cmd,
3248 		       struct spdk_dif_ctx *dif_ctx)
3249 {
3250 	struct spdk_nvmf_ns *ns;
3251 	struct spdk_bdev *bdev;
3252 
3253 	if (ctrlr == NULL || cmd == NULL) {
3254 		return false;
3255 	}
3256 
3257 	ns = _nvmf_subsystem_get_ns(ctrlr->subsys, cmd->nsid);
3258 	if (ns == NULL || ns->bdev == NULL) {
3259 		return false;
3260 	}
3261 
3262 	bdev = ns->bdev;
3263 
3264 	switch (cmd->opc) {
3265 	case SPDK_NVME_OPC_READ:
3266 	case SPDK_NVME_OPC_WRITE:
3267 	case SPDK_NVME_OPC_COMPARE:
3268 		return nvmf_bdev_ctrlr_get_dif_ctx(bdev, cmd, dif_ctx);
3269 	default:
3270 		break;
3271 	}
3272 
3273 	return false;
3274 }
3275 
3276 bool
3277 spdk_nvmf_request_get_dif_ctx(struct spdk_nvmf_request *req, struct spdk_dif_ctx *dif_ctx)
3278 {
3279 	struct spdk_nvmf_qpair *qpair = req->qpair;
3280 	struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr;
3281 
3282 	if (spdk_likely(ctrlr == NULL || !ctrlr->dif_insert_or_strip)) {
3283 		return false;
3284 	}
3285 
3286 	if (spdk_unlikely(qpair->state != SPDK_NVMF_QPAIR_ACTIVE)) {
3287 		return false;
3288 	}
3289 
3290 	if (spdk_unlikely(req->cmd->nvmf_cmd.opcode == SPDK_NVME_OPC_FABRIC)) {
3291 		return false;
3292 	}
3293 
3294 	if (spdk_unlikely(nvmf_qpair_is_admin_queue(qpair))) {
3295 		return false;
3296 	}
3297 
3298 	return nvmf_ctrlr_get_dif_ctx(ctrlr, &req->cmd->nvme_cmd, dif_ctx);
3299 }
3300 
3301 void
3302 spdk_nvmf_set_custom_admin_cmd_hdlr(uint8_t opc, spdk_nvmf_custom_cmd_hdlr hdlr)
3303 {
3304 	g_nvmf_custom_admin_cmd_hdlrs[opc].hdlr = hdlr;
3305 }
3306 
3307 static int
3308 nvmf_passthru_admin_cmd(struct spdk_nvmf_request *req)
3309 {
3310 	struct spdk_bdev *bdev;
3311 	struct spdk_bdev_desc *desc;
3312 	struct spdk_io_channel *ch;
3313 	struct spdk_nvme_cmd *cmd = spdk_nvmf_request_get_cmd(req);
3314 	struct spdk_nvme_cpl *response = spdk_nvmf_request_get_response(req);
3315 	uint32_t bdev_nsid;
3316 	int rc;
3317 
3318 	if (g_nvmf_custom_admin_cmd_hdlrs[cmd->opc].nsid == 0) {
3319 		bdev_nsid = cmd->nsid;
3320 	} else {
3321 		bdev_nsid = g_nvmf_custom_admin_cmd_hdlrs[cmd->opc].nsid;
3322 	}
3323 
3324 	rc = spdk_nvmf_request_get_bdev(bdev_nsid, req, &bdev, &desc, &ch);
3325 	if (rc) {
3326 		response->status.sct = SPDK_NVME_SCT_GENERIC;
3327 		response->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT;
3328 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
3329 	}
3330 	return spdk_nvmf_bdev_ctrlr_nvme_passthru_admin(bdev, desc, ch, req, NULL);
3331 }
3332 
3333 void
3334 spdk_nvmf_set_passthru_admin_cmd(uint8_t opc, uint32_t forward_nsid)
3335 {
3336 	g_nvmf_custom_admin_cmd_hdlrs[opc].hdlr = nvmf_passthru_admin_cmd;
3337 	g_nvmf_custom_admin_cmd_hdlrs[opc].nsid = forward_nsid;
3338 }
3339 
3340 int
3341 spdk_nvmf_request_get_bdev(uint32_t nsid, struct spdk_nvmf_request *req,
3342 			   struct spdk_bdev **bdev, struct spdk_bdev_desc **desc, struct spdk_io_channel **ch)
3343 {
3344 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
3345 	struct spdk_nvmf_ns *ns;
3346 	struct spdk_nvmf_poll_group *group = req->qpair->group;
3347 	struct spdk_nvmf_subsystem_pg_ns_info *ns_info;
3348 
3349 	*bdev = NULL;
3350 	*desc = NULL;
3351 	*ch = NULL;
3352 
3353 	ns = _nvmf_subsystem_get_ns(ctrlr->subsys, nsid);
3354 	if (ns == NULL || ns->bdev == NULL) {
3355 		return -EINVAL;
3356 	}
3357 
3358 	assert(group != NULL && group->sgroups != NULL);
3359 	ns_info = &group->sgroups[ctrlr->subsys->id].ns_info[nsid - 1];
3360 	*bdev = ns->bdev;
3361 	*desc = ns->desc;
3362 	*ch = ns_info->channel;
3363 
3364 	return 0;
3365 }
3366 
3367 struct spdk_nvmf_ctrlr *spdk_nvmf_request_get_ctrlr(struct spdk_nvmf_request *req)
3368 {
3369 	return req->qpair->ctrlr;
3370 }
3371 
3372 struct spdk_nvme_cmd *spdk_nvmf_request_get_cmd(struct spdk_nvmf_request *req)
3373 {
3374 	return &req->cmd->nvme_cmd;
3375 }
3376 
3377 struct spdk_nvme_cpl *spdk_nvmf_request_get_response(struct spdk_nvmf_request *req)
3378 {
3379 	return &req->rsp->nvme_cpl;
3380 }
3381 
3382 struct spdk_nvmf_subsystem *spdk_nvmf_request_get_subsystem(struct spdk_nvmf_request *req)
3383 {
3384 	return req->qpair->ctrlr->subsys;
3385 }
3386 
3387 void spdk_nvmf_request_get_data(struct spdk_nvmf_request *req, void **data, uint32_t *length)
3388 {
3389 	*data = req->data;
3390 	*length = req->length;
3391 }
3392 
3393 struct spdk_nvmf_subsystem *spdk_nvmf_ctrlr_get_subsystem(struct spdk_nvmf_ctrlr *ctrlr)
3394 {
3395 	return ctrlr->subsys;
3396 }
3397 
3398 uint16_t spdk_nvmf_ctrlr_get_id(struct spdk_nvmf_ctrlr *ctrlr)
3399 {
3400 	return ctrlr->cntlid;
3401 }
3402 
3403 struct spdk_nvmf_request *spdk_nvmf_request_get_req_to_abort(struct spdk_nvmf_request *req)
3404 {
3405 	return req->req_to_abort;
3406 }
3407