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