xref: /spdk/lib/nvmf/ctrlr.c (revision 7ea4a778a3e3fd19de06c382a42a3d577b344bec)
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: %zd\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 	int iov_idx = 0;
1761 	struct iovec *iov;
1762 
1763 	copy_ctx->iovs = iovs;
1764 	copy_ctx->iovcnt = iovcnt;
1765 	copy_ctx->cur_iov_idx = 0;
1766 	copy_ctx->cur_iov_offset = 0;
1767 
1768 	while (iov_idx < copy_ctx->iovcnt) {
1769 		iov = &copy_ctx->iovs[iov_idx];
1770 		memset(iov->iov_base, 0, iov->iov_len);
1771 		iov_idx++;
1772 	}
1773 }
1774 
1775 static size_t
1776 _copy_buf_to_iovs(struct copy_iovs_ctx *copy_ctx, const void *buf, size_t buf_len)
1777 {
1778 	size_t len, iov_remain_len, copied_len = 0;
1779 	struct iovec *iov;
1780 
1781 	if (buf_len == 0) {
1782 		return 0;
1783 	}
1784 
1785 	while (copy_ctx->cur_iov_idx < copy_ctx->iovcnt) {
1786 		iov = &copy_ctx->iovs[copy_ctx->cur_iov_idx];
1787 		iov_remain_len = iov->iov_len - copy_ctx->cur_iov_offset;
1788 		if (iov_remain_len == 0) {
1789 			copy_ctx->cur_iov_idx++;
1790 			copy_ctx->cur_iov_offset = 0;
1791 			continue;
1792 		}
1793 
1794 		len = spdk_min(iov_remain_len, buf_len - copied_len);
1795 		memcpy((char *)iov->iov_base + copy_ctx->cur_iov_offset,
1796 		       (const char *)buf + copied_len,
1797 		       len);
1798 		copied_len += len;
1799 		copy_ctx->cur_iov_offset += len;
1800 
1801 		if (buf_len == copied_len) {
1802 			return copied_len;
1803 		}
1804 	}
1805 
1806 	return copied_len;
1807 }
1808 
1809 static void
1810 nvmf_get_firmware_slot_log_page(struct iovec *iovs, int iovcnt, uint64_t offset, uint32_t length)
1811 {
1812 	struct spdk_nvme_firmware_page fw_page;
1813 	size_t copy_len;
1814 	struct copy_iovs_ctx copy_ctx;
1815 
1816 	_init_copy_iovs_ctx(&copy_ctx, iovs, iovcnt);
1817 
1818 	memset(&fw_page, 0, sizeof(fw_page));
1819 	fw_page.afi.active_slot = 1;
1820 	fw_page.afi.next_reset_slot = 0;
1821 	spdk_strcpy_pad(fw_page.revision[0], FW_VERSION, sizeof(fw_page.revision[0]), ' ');
1822 
1823 	if (offset < sizeof(fw_page)) {
1824 		copy_len = spdk_min(sizeof(fw_page) - offset, length);
1825 		if (copy_len > 0) {
1826 			_copy_buf_to_iovs(&copy_ctx, (const char *)&fw_page + offset, copy_len);
1827 		}
1828 	}
1829 }
1830 
1831 /*
1832  * Asynchronous Event Mask Bit
1833  */
1834 enum spdk_nvme_async_event_mask_bit {
1835 	/* Mask Namespace Change Notificaton */
1836 	SPDK_NVME_ASYNC_EVENT_NS_ATTR_CHANGE_MASK_BIT		= 0,
1837 	/* Mask Asymmetric Namespace Access Change Notification */
1838 	SPDK_NVME_ASYNC_EVENT_ANA_CHANGE_MASK_BIT		= 1,
1839 	/* Mask Discovery Log Change Notification */
1840 	SPDK_NVME_ASYNC_EVENT_DISCOVERY_LOG_CHANGE_MASK_BIT	= 2,
1841 	/* Mask Reservation Log Page Available Notification */
1842 	SPDK_NVME_ASYNC_EVENT_RESERVATION_LOG_AVAIL_MASK_BIT	= 3,
1843 
1844 	/* 4 - 63 Reserved */
1845 };
1846 
1847 static inline void
1848 nvmf_ctrlr_unmask_aen(struct spdk_nvmf_ctrlr *ctrlr,
1849 		      enum spdk_nvme_async_event_mask_bit mask)
1850 {
1851 	ctrlr->notice_aen_mask &= ~(1 << mask);
1852 }
1853 
1854 static inline bool
1855 nvmf_ctrlr_mask_aen(struct spdk_nvmf_ctrlr *ctrlr,
1856 		    enum spdk_nvme_async_event_mask_bit mask)
1857 {
1858 	if (ctrlr->notice_aen_mask & (1 << mask)) {
1859 		return false;
1860 	} else {
1861 		ctrlr->notice_aen_mask |= (1 << mask);
1862 		return true;
1863 	}
1864 }
1865 
1866 #define SPDK_NVMF_ANA_DESC_SIZE	(sizeof(struct spdk_nvme_ana_group_descriptor) +	\
1867 				 sizeof(uint32_t))
1868 static void
1869 nvmf_get_ana_log_page(struct spdk_nvmf_ctrlr *ctrlr, struct iovec *iovs, int iovcnt,
1870 		      uint64_t offset, uint32_t length, uint32_t rae)
1871 {
1872 	struct spdk_nvme_ana_page ana_hdr;
1873 	char _ana_desc[SPDK_NVMF_ANA_DESC_SIZE];
1874 	struct spdk_nvme_ana_group_descriptor *ana_desc;
1875 	size_t copy_len, copied_len;
1876 	uint32_t num_ns = 0;
1877 	struct spdk_nvmf_ns *ns;
1878 	struct copy_iovs_ctx copy_ctx;
1879 
1880 	_init_copy_iovs_ctx(&copy_ctx, iovs, iovcnt);
1881 
1882 	if (length == 0) {
1883 		return;
1884 	}
1885 
1886 	if (offset >= sizeof(ana_hdr)) {
1887 		offset -= sizeof(ana_hdr);
1888 	} else {
1889 		for (ns = spdk_nvmf_subsystem_get_first_ns(ctrlr->subsys); ns != NULL;
1890 		     ns = spdk_nvmf_subsystem_get_next_ns(ctrlr->subsys, ns)) {
1891 			num_ns++;
1892 		}
1893 
1894 		memset(&ana_hdr, 0, sizeof(ana_hdr));
1895 
1896 		ana_hdr.num_ana_group_desc = num_ns;
1897 		/* TODO: Support Change Count. */
1898 		ana_hdr.change_count = 0;
1899 
1900 		copy_len = spdk_min(sizeof(ana_hdr) - offset, length);
1901 		copied_len = _copy_buf_to_iovs(&copy_ctx, (const char *)&ana_hdr + offset, copy_len);
1902 		assert(copied_len == copy_len);
1903 		length -= copied_len;
1904 		offset = 0;
1905 	}
1906 
1907 	if (length == 0) {
1908 		return;
1909 	}
1910 
1911 	ana_desc = (void *)_ana_desc;
1912 
1913 	for (ns = spdk_nvmf_subsystem_get_first_ns(ctrlr->subsys); ns != NULL;
1914 	     ns = spdk_nvmf_subsystem_get_next_ns(ctrlr->subsys, ns)) {
1915 		if (offset >= SPDK_NVMF_ANA_DESC_SIZE) {
1916 			offset -= SPDK_NVMF_ANA_DESC_SIZE;
1917 			continue;
1918 		}
1919 
1920 		memset(ana_desc, 0, SPDK_NVMF_ANA_DESC_SIZE);
1921 
1922 		ana_desc->ana_group_id = ns->nsid;
1923 		ana_desc->num_of_nsid = 1;
1924 		ana_desc->ana_state = ctrlr->listener->ana_state;
1925 		ana_desc->nsid[0] = ns->nsid;
1926 		/* TODO: Support Change Count. */
1927 		ana_desc->change_count = 0;
1928 
1929 		copy_len = spdk_min(SPDK_NVMF_ANA_DESC_SIZE - offset, length);
1930 		copied_len = _copy_buf_to_iovs(&copy_ctx, (const char *)ana_desc + offset, copy_len);
1931 		assert(copied_len == copy_len);
1932 		length -= copied_len;
1933 		offset = 0;
1934 
1935 		if (length == 0) {
1936 			goto done;
1937 		}
1938 	}
1939 
1940 done:
1941 	if (!rae) {
1942 		nvmf_ctrlr_unmask_aen(ctrlr, SPDK_NVME_ASYNC_EVENT_ANA_CHANGE_MASK_BIT);
1943 	}
1944 }
1945 
1946 void
1947 nvmf_ctrlr_ns_changed(struct spdk_nvmf_ctrlr *ctrlr, uint32_t nsid)
1948 {
1949 	uint16_t max_changes = SPDK_COUNTOF(ctrlr->changed_ns_list.ns_list);
1950 	uint16_t i;
1951 	bool found = false;
1952 
1953 	for (i = 0; i < ctrlr->changed_ns_list_count; i++) {
1954 		if (ctrlr->changed_ns_list.ns_list[i] == nsid) {
1955 			/* nsid is already in the list */
1956 			found = true;
1957 			break;
1958 		}
1959 	}
1960 
1961 	if (!found) {
1962 		if (ctrlr->changed_ns_list_count == max_changes) {
1963 			/* Out of space - set first entry to FFFFFFFFh and zero-fill the rest. */
1964 			ctrlr->changed_ns_list.ns_list[0] = 0xFFFFFFFFu;
1965 			for (i = 1; i < max_changes; i++) {
1966 				ctrlr->changed_ns_list.ns_list[i] = 0;
1967 			}
1968 		} else {
1969 			ctrlr->changed_ns_list.ns_list[ctrlr->changed_ns_list_count++] = nsid;
1970 		}
1971 	}
1972 }
1973 
1974 static void
1975 nvmf_get_changed_ns_list_log_page(struct spdk_nvmf_ctrlr *ctrlr,
1976 				  struct iovec *iovs, int iovcnt, uint64_t offset, uint32_t length, uint32_t rae)
1977 {
1978 	size_t copy_length;
1979 	struct copy_iovs_ctx copy_ctx;
1980 
1981 	_init_copy_iovs_ctx(&copy_ctx, iovs, iovcnt);
1982 
1983 	if (offset < sizeof(ctrlr->changed_ns_list)) {
1984 		copy_length = spdk_min(length, sizeof(ctrlr->changed_ns_list) - offset);
1985 		if (copy_length) {
1986 			_copy_buf_to_iovs(&copy_ctx, (char *)&ctrlr->changed_ns_list + offset, copy_length);
1987 		}
1988 	}
1989 
1990 	/* Clear log page each time it is read */
1991 	ctrlr->changed_ns_list_count = 0;
1992 	memset(&ctrlr->changed_ns_list, 0, sizeof(ctrlr->changed_ns_list));
1993 
1994 	if (!rae) {
1995 		nvmf_ctrlr_unmask_aen(ctrlr, SPDK_NVME_ASYNC_EVENT_NS_ATTR_CHANGE_MASK_BIT);
1996 	}
1997 }
1998 
1999 /* The structure can be modified if we provide support for other commands in future */
2000 static const struct spdk_nvme_cmds_and_effect_log_page g_cmds_and_effect_log_page = {
2001 	.admin_cmds_supported = {
2002 		/* CSUPP, LBCC, NCC, NIC, CCC, CSE */
2003 		/* Get Log Page */
2004 		[SPDK_NVME_OPC_GET_LOG_PAGE]		= {1, 0, 0, 0, 0, 0, 0, 0},
2005 		/* Identify */
2006 		[SPDK_NVME_OPC_IDENTIFY]		= {1, 0, 0, 0, 0, 0, 0, 0},
2007 		/* Abort */
2008 		[SPDK_NVME_OPC_ABORT]			= {1, 0, 0, 0, 0, 0, 0, 0},
2009 		/* Set Features */
2010 		[SPDK_NVME_OPC_SET_FEATURES]		= {1, 0, 0, 0, 0, 0, 0, 0},
2011 		/* Get Features */
2012 		[SPDK_NVME_OPC_GET_FEATURES]		= {1, 0, 0, 0, 0, 0, 0, 0},
2013 		/* Async Event Request */
2014 		[SPDK_NVME_OPC_ASYNC_EVENT_REQUEST]	= {1, 0, 0, 0, 0, 0, 0, 0},
2015 		/* Keep Alive */
2016 		[SPDK_NVME_OPC_KEEP_ALIVE]		= {1, 0, 0, 0, 0, 0, 0, 0},
2017 	},
2018 	.io_cmds_supported = {
2019 		/* FLUSH */
2020 		[SPDK_NVME_OPC_FLUSH]			= {1, 1, 0, 0, 0, 0, 0, 0},
2021 		/* WRITE */
2022 		[SPDK_NVME_OPC_WRITE]			= {1, 1, 0, 0, 0, 0, 0, 0},
2023 		/* READ */
2024 		[SPDK_NVME_OPC_READ]			= {1, 0, 0, 0, 0, 0, 0, 0},
2025 		/* WRITE ZEROES */
2026 		[SPDK_NVME_OPC_WRITE_ZEROES]		= {1, 1, 0, 0, 0, 0, 0, 0},
2027 		/* DATASET MANAGEMENT */
2028 		[SPDK_NVME_OPC_DATASET_MANAGEMENT]	= {1, 1, 0, 0, 0, 0, 0, 0},
2029 		/* COMPARE */
2030 		[SPDK_NVME_OPC_COMPARE]			= {1, 0, 0, 0, 0, 0, 0, 0},
2031 	},
2032 };
2033 
2034 static void
2035 nvmf_get_cmds_and_effects_log_page(struct iovec *iovs, int iovcnt,
2036 				   uint64_t offset, uint32_t length)
2037 {
2038 	uint32_t page_size = sizeof(struct spdk_nvme_cmds_and_effect_log_page);
2039 	size_t copy_len = 0;
2040 	struct copy_iovs_ctx copy_ctx;
2041 
2042 	_init_copy_iovs_ctx(&copy_ctx, iovs, iovcnt);
2043 
2044 	if (offset < page_size) {
2045 		copy_len = spdk_min(page_size - offset, length);
2046 		_copy_buf_to_iovs(&copy_ctx, (char *)(&g_cmds_and_effect_log_page) + offset, copy_len);
2047 	}
2048 }
2049 
2050 static void
2051 nvmf_get_reservation_notification_log_page(struct spdk_nvmf_ctrlr *ctrlr,
2052 		struct iovec *iovs, int iovcnt, uint64_t offset, uint32_t length, uint32_t rae)
2053 {
2054 	uint32_t unit_log_len, avail_log_len, next_pos, copy_len;
2055 	struct spdk_nvmf_reservation_log *log, *log_tmp;
2056 	struct copy_iovs_ctx copy_ctx;
2057 
2058 	_init_copy_iovs_ctx(&copy_ctx, iovs, iovcnt);
2059 
2060 	unit_log_len = sizeof(struct spdk_nvme_reservation_notification_log);
2061 	/* No available log, return zeroed log pages */
2062 	if (!ctrlr->num_avail_log_pages) {
2063 		return;
2064 	}
2065 
2066 	avail_log_len = ctrlr->num_avail_log_pages * unit_log_len;
2067 	if (offset >= avail_log_len) {
2068 		return;
2069 	}
2070 
2071 	next_pos = 0;
2072 	TAILQ_FOREACH_SAFE(log, &ctrlr->log_head, link, log_tmp) {
2073 		TAILQ_REMOVE(&ctrlr->log_head, log, link);
2074 		ctrlr->num_avail_log_pages--;
2075 
2076 		next_pos += unit_log_len;
2077 		if (next_pos > offset) {
2078 			copy_len = spdk_min(next_pos - offset, length);
2079 			_copy_buf_to_iovs(&copy_ctx, &log->log, copy_len);
2080 			length -= copy_len;
2081 			offset += copy_len;
2082 		}
2083 		free(log);
2084 
2085 		if (length == 0) {
2086 			break;
2087 		}
2088 	}
2089 
2090 	if (!rae) {
2091 		nvmf_ctrlr_unmask_aen(ctrlr, SPDK_NVME_ASYNC_EVENT_RESERVATION_LOG_AVAIL_MASK_BIT);
2092 	}
2093 	return;
2094 }
2095 
2096 static int
2097 nvmf_ctrlr_get_log_page(struct spdk_nvmf_request *req)
2098 {
2099 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
2100 	struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys;
2101 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
2102 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
2103 	uint64_t offset, len;
2104 	uint32_t rae, numdl, numdu;
2105 	uint8_t lid;
2106 
2107 	if (req->data == NULL) {
2108 		SPDK_ERRLOG("get log command with no buffer\n");
2109 		response->status.sct = SPDK_NVME_SCT_GENERIC;
2110 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
2111 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2112 	}
2113 
2114 	offset = (uint64_t)cmd->cdw12 | ((uint64_t)cmd->cdw13 << 32);
2115 	if (offset & 3) {
2116 		SPDK_ERRLOG("Invalid log page offset 0x%" PRIx64 "\n", offset);
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 	rae = cmd->cdw10_bits.get_log_page.rae;
2123 	numdl = cmd->cdw10_bits.get_log_page.numdl;
2124 	numdu = cmd->cdw11_bits.get_log_page.numdu;
2125 	len = ((numdu << 16) + numdl + (uint64_t)1) * 4;
2126 	if (len > req->length) {
2127 		SPDK_ERRLOG("Get log page: len (%" PRIu64 ") > buf size (%u)\n",
2128 			    len, req->length);
2129 		response->status.sct = SPDK_NVME_SCT_GENERIC;
2130 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
2131 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2132 	}
2133 
2134 	lid = cmd->cdw10_bits.get_log_page.lid;
2135 	SPDK_DEBUGLOG(nvmf, "Get log page: LID=0x%02X offset=0x%" PRIx64 " len=0x%" PRIx64 " rae=%u\n",
2136 		      lid, offset, len, rae);
2137 
2138 	if (subsystem->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) {
2139 		switch (lid) {
2140 		case SPDK_NVME_LOG_DISCOVERY:
2141 			nvmf_get_discovery_log_page(subsystem->tgt, ctrlr->hostnqn, req->iov, req->iovcnt, offset,
2142 						    len);
2143 			if (!rae) {
2144 				nvmf_ctrlr_unmask_aen(ctrlr, SPDK_NVME_ASYNC_EVENT_DISCOVERY_LOG_CHANGE_MASK_BIT);
2145 			}
2146 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2147 		default:
2148 			goto invalid_log_page;
2149 		}
2150 	} else {
2151 		switch (lid) {
2152 		case SPDK_NVME_LOG_ERROR:
2153 		case SPDK_NVME_LOG_HEALTH_INFORMATION:
2154 			/* TODO: actually fill out log page data */
2155 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2156 		case SPDK_NVME_LOG_FIRMWARE_SLOT:
2157 			nvmf_get_firmware_slot_log_page(req->iov, req->iovcnt, offset, len);
2158 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2159 		case SPDK_NVME_LOG_ASYMMETRIC_NAMESPACE_ACCESS:
2160 			if (subsystem->flags.ana_reporting) {
2161 				nvmf_get_ana_log_page(ctrlr, req->iov, req->iovcnt, offset, len, rae);
2162 				return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2163 			} else {
2164 				goto invalid_log_page;
2165 			}
2166 		case SPDK_NVME_LOG_COMMAND_EFFECTS_LOG:
2167 			nvmf_get_cmds_and_effects_log_page(req->iov, req->iovcnt, offset, len);
2168 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2169 		case SPDK_NVME_LOG_CHANGED_NS_LIST:
2170 			nvmf_get_changed_ns_list_log_page(ctrlr, req->iov, req->iovcnt, offset, len, rae);
2171 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2172 		case SPDK_NVME_LOG_RESERVATION_NOTIFICATION:
2173 			nvmf_get_reservation_notification_log_page(ctrlr, req->iov, req->iovcnt, offset, len, rae);
2174 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2175 		default:
2176 			goto invalid_log_page;
2177 		}
2178 	}
2179 
2180 invalid_log_page:
2181 	SPDK_ERRLOG("Unsupported Get Log Page 0x%02X\n", lid);
2182 	response->status.sct = SPDK_NVME_SCT_GENERIC;
2183 	response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
2184 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2185 }
2186 
2187 int
2188 spdk_nvmf_ctrlr_identify_ns(struct spdk_nvmf_ctrlr *ctrlr,
2189 			    struct spdk_nvme_cmd *cmd,
2190 			    struct spdk_nvme_cpl *rsp,
2191 			    struct spdk_nvme_ns_data *nsdata)
2192 {
2193 	struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys;
2194 	struct spdk_nvmf_ns *ns;
2195 	uint32_t max_num_blocks;
2196 
2197 	if (cmd->nsid == 0 || cmd->nsid > subsystem->max_nsid) {
2198 		SPDK_ERRLOG("Identify Namespace for invalid NSID %u\n", cmd->nsid);
2199 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
2200 		rsp->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT;
2201 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2202 	}
2203 
2204 	ns = _nvmf_subsystem_get_ns(subsystem, cmd->nsid);
2205 	if (ns == NULL || ns->bdev == NULL) {
2206 		/*
2207 		 * Inactive namespaces should return a zero filled data structure.
2208 		 * The data buffer is already zeroed by nvmf_ctrlr_process_admin_cmd(),
2209 		 * so we can just return early here.
2210 		 */
2211 		SPDK_DEBUGLOG(nvmf, "Identify Namespace for inactive NSID %u\n", cmd->nsid);
2212 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
2213 		rsp->status.sc = SPDK_NVME_SC_SUCCESS;
2214 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2215 	}
2216 
2217 	nvmf_bdev_ctrlr_identify_ns(ns, nsdata, ctrlr->dif_insert_or_strip);
2218 
2219 	/* Due to bug in the Linux kernel NVMe driver we have to set noiob no larger than mdts */
2220 	max_num_blocks = ctrlr->admin_qpair->transport->opts.max_io_size /
2221 			 (1U << nsdata->lbaf[nsdata->flbas.format].lbads);
2222 	if (nsdata->noiob > max_num_blocks) {
2223 		nsdata->noiob = max_num_blocks;
2224 	}
2225 
2226 	if (subsystem->flags.ana_reporting) {
2227 		/* ANA group ID matches NSID. */
2228 		nsdata->anagrpid = ns->nsid;
2229 
2230 		if (ctrlr->listener->ana_state == SPDK_NVME_ANA_INACCESSIBLE_STATE ||
2231 		    ctrlr->listener->ana_state == SPDK_NVME_ANA_PERSISTENT_LOSS_STATE) {
2232 			nsdata->nuse = 0;
2233 		}
2234 	}
2235 
2236 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2237 }
2238 
2239 static void
2240 nvmf_ctrlr_populate_oacs(struct spdk_nvmf_ctrlr *ctrlr,
2241 			 struct spdk_nvme_ctrlr_data *cdata)
2242 {
2243 	cdata->oacs.virtualization_management =
2244 		g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_VIRTUALIZATION_MANAGEMENT].hdlr != NULL;
2245 	cdata->oacs.nvme_mi = g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_NVME_MI_SEND].hdlr != NULL
2246 			      && g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_NVME_MI_RECEIVE].hdlr != NULL;
2247 	cdata->oacs.directives = g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_DIRECTIVE_SEND].hdlr != NULL
2248 				 && g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_DIRECTIVE_RECEIVE].hdlr != NULL;
2249 	cdata->oacs.device_self_test =
2250 		g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_DEVICE_SELF_TEST].hdlr != NULL;
2251 	cdata->oacs.ns_manage = g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_NS_MANAGEMENT].hdlr != NULL
2252 				&& g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_NS_ATTACHMENT].hdlr != NULL;
2253 	cdata->oacs.firmware = g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_FIRMWARE_IMAGE_DOWNLOAD].hdlr !=
2254 			       NULL
2255 			       && g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_FIRMWARE_COMMIT].hdlr != NULL;
2256 	cdata->oacs.format =
2257 		g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_FORMAT_NVM].hdlr != NULL;
2258 	cdata->oacs.security = g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_SECURITY_SEND].hdlr != NULL
2259 			       && g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_SECURITY_RECEIVE].hdlr != NULL;
2260 	cdata->oacs.get_lba_status = g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_GET_LBA_STATUS].hdlr !=
2261 				     NULL;
2262 }
2263 
2264 int
2265 spdk_nvmf_ctrlr_identify_ctrlr(struct spdk_nvmf_ctrlr *ctrlr, struct spdk_nvme_ctrlr_data *cdata)
2266 {
2267 	struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys;
2268 	struct spdk_nvmf_transport *transport = ctrlr->admin_qpair->transport;
2269 
2270 	/*
2271 	 * Common fields for discovery and NVM subsystems
2272 	 */
2273 	spdk_strcpy_pad(cdata->fr, FW_VERSION, sizeof(cdata->fr), ' ');
2274 	assert((transport->opts.max_io_size % 4096) == 0);
2275 	cdata->mdts = spdk_u32log2(transport->opts.max_io_size / 4096);
2276 	cdata->cntlid = ctrlr->cntlid;
2277 	cdata->ver = ctrlr->vcprop.vs;
2278 	cdata->aerl = NVMF_MAX_ASYNC_EVENTS - 1;
2279 	cdata->lpa.edlp = 1;
2280 	cdata->elpe = 127;
2281 	cdata->maxcmd = transport->opts.max_queue_depth;
2282 	cdata->sgls = ctrlr->cdata.sgls;
2283 	cdata->fuses.compare_and_write = 1;
2284 	cdata->acwu = 1;
2285 	if (subsystem->flags.ana_reporting) {
2286 		cdata->mnan = subsystem->max_nsid;
2287 	}
2288 	spdk_strcpy_pad(cdata->subnqn, subsystem->subnqn, sizeof(cdata->subnqn), '\0');
2289 
2290 	SPDK_DEBUGLOG(nvmf, "ctrlr data: maxcmd 0x%x\n", cdata->maxcmd);
2291 	SPDK_DEBUGLOG(nvmf, "sgls data: 0x%x\n", from_le32(&cdata->sgls));
2292 
2293 
2294 	if (subsystem->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) {
2295 		/*
2296 		 * NVM Discovery subsystem fields
2297 		 */
2298 		cdata->oaes.discovery_log_change_notices = 1;
2299 	} else {
2300 		/*
2301 		 * NVM subsystem fields (reserved for discovery subsystems)
2302 		 */
2303 		spdk_strcpy_pad(cdata->mn, spdk_nvmf_subsystem_get_mn(subsystem), sizeof(cdata->mn), ' ');
2304 		spdk_strcpy_pad(cdata->sn, spdk_nvmf_subsystem_get_sn(subsystem), sizeof(cdata->sn), ' ');
2305 		cdata->kas = ctrlr->cdata.kas;
2306 
2307 		cdata->rab = 6;
2308 		cdata->cmic.multi_port = 1;
2309 		cdata->cmic.multi_host = 1;
2310 		if (subsystem->flags.ana_reporting) {
2311 			/* Asymmetric Namespace Access Reporting is supported. */
2312 			cdata->cmic.ana_reporting = 1;
2313 		}
2314 		cdata->oaes.ns_attribute_notices = 1;
2315 		if (subsystem->flags.ana_reporting) {
2316 			cdata->oaes.ana_change_notices = 1;
2317 		}
2318 		cdata->ctratt.host_id_exhid_supported = 1;
2319 		/* TODO: Concurrent execution of multiple abort commands. */
2320 		cdata->acl = 0;
2321 		cdata->aerl = 0;
2322 		cdata->frmw.slot1_ro = 1;
2323 		cdata->frmw.num_slots = 1;
2324 
2325 		cdata->lpa.celp = 1; /* Command Effects log page supported */
2326 
2327 		cdata->sqes.min = 6;
2328 		cdata->sqes.max = 6;
2329 		cdata->cqes.min = 4;
2330 		cdata->cqes.max = 4;
2331 		cdata->nn = subsystem->max_nsid;
2332 		cdata->vwc.present = 1;
2333 		cdata->vwc.flush_broadcast = SPDK_NVME_FLUSH_BROADCAST_NOT_SUPPORTED;
2334 
2335 		cdata->nvmf_specific = ctrlr->cdata.nvmf_specific;
2336 
2337 		cdata->oncs.dsm = nvmf_ctrlr_dsm_supported(ctrlr);
2338 		cdata->oncs.write_zeroes = nvmf_ctrlr_write_zeroes_supported(ctrlr);
2339 		cdata->oncs.reservations = 1;
2340 		if (subsystem->flags.ana_reporting) {
2341 			cdata->anatt = ANA_TRANSITION_TIME_IN_SEC;
2342 			/* ANA Change state is not used, and ANA Persistent Loss state
2343 			 * is not supported for now.
2344 			 */
2345 			cdata->anacap.ana_optimized_state = 1;
2346 			cdata->anacap.ana_non_optimized_state = 1;
2347 			cdata->anacap.ana_inaccessible_state = 1;
2348 			/* ANAGRPID does not change while namespace is attached to controller */
2349 			cdata->anacap.no_change_anagrpid = 1;
2350 			cdata->anagrpmax = subsystem->max_nsid;
2351 			cdata->nanagrpid = subsystem->max_nsid;
2352 		}
2353 
2354 		nvmf_ctrlr_populate_oacs(ctrlr, cdata);
2355 
2356 		assert(subsystem->tgt != NULL);
2357 		cdata->crdt[0] = subsystem->tgt->crdt[0];
2358 		cdata->crdt[1] = subsystem->tgt->crdt[1];
2359 		cdata->crdt[2] = subsystem->tgt->crdt[2];
2360 
2361 		SPDK_DEBUGLOG(nvmf, "ext ctrlr data: ioccsz 0x%x\n",
2362 			      cdata->nvmf_specific.ioccsz);
2363 		SPDK_DEBUGLOG(nvmf, "ext ctrlr data: iorcsz 0x%x\n",
2364 			      cdata->nvmf_specific.iorcsz);
2365 		SPDK_DEBUGLOG(nvmf, "ext ctrlr data: icdoff 0x%x\n",
2366 			      cdata->nvmf_specific.icdoff);
2367 		SPDK_DEBUGLOG(nvmf, "ext ctrlr data: ctrattr 0x%x\n",
2368 			      *(uint8_t *)&cdata->nvmf_specific.ctrattr);
2369 		SPDK_DEBUGLOG(nvmf, "ext ctrlr data: msdbd 0x%x\n",
2370 			      cdata->nvmf_specific.msdbd);
2371 	}
2372 
2373 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2374 }
2375 
2376 static int
2377 nvmf_ctrlr_identify_active_ns_list(struct spdk_nvmf_subsystem *subsystem,
2378 				   struct spdk_nvme_cmd *cmd,
2379 				   struct spdk_nvme_cpl *rsp,
2380 				   struct spdk_nvme_ns_list *ns_list)
2381 {
2382 	struct spdk_nvmf_ns *ns;
2383 	uint32_t count = 0;
2384 
2385 	if (cmd->nsid >= 0xfffffffeUL) {
2386 		SPDK_ERRLOG("Identify Active Namespace List with invalid NSID %u\n", cmd->nsid);
2387 		rsp->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT;
2388 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2389 	}
2390 
2391 	for (ns = spdk_nvmf_subsystem_get_first_ns(subsystem); ns != NULL;
2392 	     ns = spdk_nvmf_subsystem_get_next_ns(subsystem, ns)) {
2393 		if (ns->opts.nsid <= cmd->nsid) {
2394 			continue;
2395 		}
2396 
2397 		ns_list->ns_list[count++] = ns->opts.nsid;
2398 		if (count == SPDK_COUNTOF(ns_list->ns_list)) {
2399 			break;
2400 		}
2401 	}
2402 
2403 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2404 }
2405 
2406 static void
2407 _add_ns_id_desc(void **buf_ptr, size_t *buf_remain,
2408 		enum spdk_nvme_nidt type,
2409 		const void *data, size_t data_size)
2410 {
2411 	struct spdk_nvme_ns_id_desc *desc;
2412 	size_t desc_size = sizeof(*desc) + data_size;
2413 
2414 	/*
2415 	 * These should never fail in practice, since all valid NS ID descriptors
2416 	 * should be defined so that they fit in the available 4096-byte buffer.
2417 	 */
2418 	assert(data_size > 0);
2419 	assert(data_size <= UINT8_MAX);
2420 	assert(desc_size < *buf_remain);
2421 	if (data_size == 0 || data_size > UINT8_MAX || desc_size > *buf_remain) {
2422 		return;
2423 	}
2424 
2425 	desc = *buf_ptr;
2426 	desc->nidt = type;
2427 	desc->nidl = data_size;
2428 	memcpy(desc->nid, data, data_size);
2429 
2430 	*buf_ptr += desc_size;
2431 	*buf_remain -= desc_size;
2432 }
2433 
2434 static int
2435 nvmf_ctrlr_identify_ns_id_descriptor_list(
2436 	struct spdk_nvmf_subsystem *subsystem,
2437 	struct spdk_nvme_cmd *cmd,
2438 	struct spdk_nvme_cpl *rsp,
2439 	void *id_desc_list, size_t id_desc_list_size)
2440 {
2441 	struct spdk_nvmf_ns *ns;
2442 	size_t buf_remain = id_desc_list_size;
2443 	void *buf_ptr = id_desc_list;
2444 
2445 	ns = _nvmf_subsystem_get_ns(subsystem, cmd->nsid);
2446 	if (ns == NULL || ns->bdev == NULL) {
2447 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
2448 		rsp->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT;
2449 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2450 	}
2451 
2452 #define ADD_ID_DESC(type, data, size) \
2453 	do { \
2454 		if (!spdk_mem_all_zero(data, size)) { \
2455 			_add_ns_id_desc(&buf_ptr, &buf_remain, type, data, size); \
2456 		} \
2457 	} while (0)
2458 
2459 	ADD_ID_DESC(SPDK_NVME_NIDT_EUI64, ns->opts.eui64, sizeof(ns->opts.eui64));
2460 	ADD_ID_DESC(SPDK_NVME_NIDT_NGUID, ns->opts.nguid, sizeof(ns->opts.nguid));
2461 	ADD_ID_DESC(SPDK_NVME_NIDT_UUID, &ns->opts.uuid, sizeof(ns->opts.uuid));
2462 
2463 	/*
2464 	 * The list is automatically 0-terminated because controller to host buffers in
2465 	 * admin commands always get zeroed in nvmf_ctrlr_process_admin_cmd().
2466 	 */
2467 
2468 #undef ADD_ID_DESC
2469 
2470 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2471 }
2472 
2473 static int
2474 nvmf_ctrlr_identify(struct spdk_nvmf_request *req)
2475 {
2476 	uint8_t cns;
2477 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
2478 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
2479 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
2480 	struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys;
2481 
2482 	if (req->data == NULL || req->length < 4096) {
2483 		SPDK_ERRLOG("identify command with invalid buffer\n");
2484 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
2485 		rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
2486 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2487 	}
2488 
2489 	cns = cmd->cdw10_bits.identify.cns;
2490 
2491 	if (subsystem->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY &&
2492 	    cns != SPDK_NVME_IDENTIFY_CTRLR) {
2493 		/* Discovery controllers only support Identify Controller */
2494 		goto invalid_cns;
2495 	}
2496 
2497 	switch (cns) {
2498 	case SPDK_NVME_IDENTIFY_NS:
2499 		return spdk_nvmf_ctrlr_identify_ns(ctrlr, cmd, rsp, req->data);
2500 	case SPDK_NVME_IDENTIFY_CTRLR:
2501 		return spdk_nvmf_ctrlr_identify_ctrlr(ctrlr, req->data);
2502 	case SPDK_NVME_IDENTIFY_ACTIVE_NS_LIST:
2503 		return nvmf_ctrlr_identify_active_ns_list(subsystem, cmd, rsp, req->data);
2504 	case SPDK_NVME_IDENTIFY_NS_ID_DESCRIPTOR_LIST:
2505 		return nvmf_ctrlr_identify_ns_id_descriptor_list(subsystem, cmd, rsp, req->data, req->length);
2506 	default:
2507 		goto invalid_cns;
2508 	}
2509 
2510 invalid_cns:
2511 	SPDK_ERRLOG("Identify command with unsupported CNS 0x%02x\n", cns);
2512 	rsp->status.sct = SPDK_NVME_SCT_GENERIC;
2513 	rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
2514 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2515 }
2516 
2517 static bool
2518 nvmf_qpair_abort_aer(struct spdk_nvmf_qpair *qpair, uint16_t cid)
2519 {
2520 	struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr;
2521 	struct spdk_nvmf_request *req;
2522 	int i;
2523 
2524 	if (!nvmf_qpair_is_admin_queue(qpair)) {
2525 		return false;
2526 	}
2527 
2528 	for (i = 0; i < ctrlr->nr_aer_reqs; i++) {
2529 		if (ctrlr->aer_req[i]->cmd->nvme_cmd.cid == cid) {
2530 			SPDK_DEBUGLOG(nvmf, "Aborting AER request\n");
2531 			req = ctrlr->aer_req[i];
2532 			ctrlr->aer_req[i] = NULL;
2533 			ctrlr->nr_aer_reqs--;
2534 
2535 			/* Move the last req to the aborting position for making aer_reqs
2536 			 * in continuous
2537 			 */
2538 			if (i < ctrlr->nr_aer_reqs) {
2539 				ctrlr->aer_req[i] = ctrlr->aer_req[ctrlr->nr_aer_reqs];
2540 				ctrlr->aer_req[ctrlr->nr_aer_reqs] = NULL;
2541 			}
2542 
2543 			req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
2544 			req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_ABORTED_BY_REQUEST;
2545 			_nvmf_request_complete(req);
2546 			return true;
2547 		}
2548 	}
2549 
2550 	return false;
2551 }
2552 
2553 static void
2554 nvmf_qpair_abort_request(struct spdk_nvmf_qpair *qpair, struct spdk_nvmf_request *req)
2555 {
2556 	uint16_t cid = req->cmd->nvme_cmd.cdw10_bits.abort.cid;
2557 
2558 	if (nvmf_qpair_abort_aer(qpair, cid)) {
2559 		SPDK_DEBUGLOG(nvmf, "abort ctrlr=%p sqid=%u cid=%u successful\n",
2560 			      qpair->ctrlr, qpair->qid, cid);
2561 		req->rsp->nvme_cpl.cdw0 &= ~1U; /* Command successfully aborted */
2562 
2563 		spdk_nvmf_request_complete(req);
2564 		return;
2565 	}
2566 
2567 	nvmf_transport_qpair_abort_request(qpair, req);
2568 }
2569 
2570 static void
2571 nvmf_ctrlr_abort_done(struct spdk_io_channel_iter *i, int status)
2572 {
2573 	struct spdk_nvmf_request *req = spdk_io_channel_iter_get_ctx(i);
2574 
2575 	if (status == 0) {
2576 		/* There was no qpair whose ID matches SQID of the abort command.
2577 		 * Hence call _nvmf_request_complete() here.
2578 		 */
2579 		_nvmf_request_complete(req);
2580 	}
2581 }
2582 
2583 static void
2584 nvmf_ctrlr_abort_on_pg(struct spdk_io_channel_iter *i)
2585 {
2586 	struct spdk_nvmf_request *req = spdk_io_channel_iter_get_ctx(i);
2587 	struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
2588 	struct spdk_nvmf_poll_group *group = spdk_io_channel_get_ctx(ch);
2589 	uint16_t sqid = req->cmd->nvme_cmd.cdw10_bits.abort.sqid;
2590 	struct spdk_nvmf_qpair *qpair;
2591 
2592 	TAILQ_FOREACH(qpair, &group->qpairs, link) {
2593 		if (qpair->ctrlr == req->qpair->ctrlr && qpair->qid == sqid) {
2594 			/* Found the qpair */
2595 
2596 			nvmf_qpair_abort_request(qpair, req);
2597 
2598 			/* Return -1 for the status so the iteration across threads stops. */
2599 			spdk_for_each_channel_continue(i, -1);
2600 			return;
2601 		}
2602 	}
2603 
2604 	spdk_for_each_channel_continue(i, 0);
2605 }
2606 
2607 static int
2608 nvmf_ctrlr_abort(struct spdk_nvmf_request *req)
2609 {
2610 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
2611 
2612 	rsp->cdw0 = 1U; /* Command not aborted */
2613 	rsp->status.sct = SPDK_NVME_SCT_GENERIC;
2614 	rsp->status.sc = SPDK_NVME_SC_SUCCESS;
2615 
2616 	/* Send a message to each poll group, searching for this ctrlr, sqid, and command. */
2617 	spdk_for_each_channel(req->qpair->ctrlr->subsys->tgt,
2618 			      nvmf_ctrlr_abort_on_pg,
2619 			      req,
2620 			      nvmf_ctrlr_abort_done
2621 			     );
2622 
2623 	return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
2624 }
2625 
2626 int
2627 nvmf_ctrlr_abort_request(struct spdk_nvmf_request *req)
2628 {
2629 	struct spdk_nvmf_request *req_to_abort = req->req_to_abort;
2630 	struct spdk_bdev *bdev;
2631 	struct spdk_bdev_desc *desc;
2632 	struct spdk_io_channel *ch;
2633 	int rc;
2634 
2635 	assert(req_to_abort != NULL);
2636 
2637 	if (g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_ABORT].hdlr &&
2638 	    nvmf_qpair_is_admin_queue(req_to_abort->qpair)) {
2639 		return g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_ABORT].hdlr(req);
2640 	}
2641 
2642 	rc = spdk_nvmf_request_get_bdev(req_to_abort->cmd->nvme_cmd.nsid, req_to_abort,
2643 					&bdev, &desc, &ch);
2644 	if (rc != 0) {
2645 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2646 	}
2647 
2648 	return spdk_nvmf_bdev_ctrlr_abort_cmd(bdev, desc, ch, req, req_to_abort);
2649 }
2650 
2651 static int
2652 get_features_generic(struct spdk_nvmf_request *req, uint32_t cdw0)
2653 {
2654 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
2655 
2656 	rsp->cdw0 = cdw0;
2657 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2658 }
2659 
2660 /* we have to use the typedef in the function declaration to appease astyle. */
2661 typedef enum spdk_nvme_path_status_code spdk_nvme_path_status_code_t;
2662 
2663 static spdk_nvme_path_status_code_t
2664 _nvme_ana_state_to_path_status(enum spdk_nvme_ana_state ana_state)
2665 {
2666 	switch (ana_state) {
2667 	case SPDK_NVME_ANA_INACCESSIBLE_STATE:
2668 		return SPDK_NVME_SC_ASYMMETRIC_ACCESS_INACCESSIBLE;
2669 	case SPDK_NVME_ANA_PERSISTENT_LOSS_STATE:
2670 		return SPDK_NVME_SC_ASYMMETRIC_ACCESS_PERSISTENT_LOSS;
2671 	case SPDK_NVME_ANA_CHANGE_STATE:
2672 		return SPDK_NVME_SC_ASYMMETRIC_ACCESS_TRANSITION;
2673 	default:
2674 		return SPDK_NVME_SC_INTERNAL_PATH_ERROR;
2675 	}
2676 }
2677 
2678 static int
2679 nvmf_ctrlr_get_features(struct spdk_nvmf_request *req)
2680 {
2681 	uint8_t feature;
2682 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
2683 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
2684 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
2685 	enum spdk_nvme_ana_state ana_state;
2686 
2687 	feature = cmd->cdw10_bits.get_features.fid;
2688 
2689 	if (ctrlr->subsys->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) {
2690 		/*
2691 		 * Features supported by Discovery controller
2692 		 */
2693 		switch (feature) {
2694 		case SPDK_NVME_FEAT_KEEP_ALIVE_TIMER:
2695 			return get_features_generic(req, ctrlr->feat.keep_alive_timer.raw);
2696 		case SPDK_NVME_FEAT_ASYNC_EVENT_CONFIGURATION:
2697 			return get_features_generic(req, ctrlr->feat.async_event_configuration.raw);
2698 		default:
2699 			SPDK_ERRLOG("Get Features command with unsupported feature ID 0x%02x\n", feature);
2700 			response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
2701 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2702 		}
2703 	}
2704 	/*
2705 	 * Process Get Features command for non-discovery controller
2706 	 */
2707 	ana_state = ctrlr->listener->ana_state;
2708 	switch (ana_state) {
2709 	case SPDK_NVME_ANA_INACCESSIBLE_STATE:
2710 	case SPDK_NVME_ANA_PERSISTENT_LOSS_STATE:
2711 	case SPDK_NVME_ANA_CHANGE_STATE:
2712 		switch (feature) {
2713 		case SPDK_NVME_FEAT_ERROR_RECOVERY:
2714 		case SPDK_NVME_FEAT_WRITE_ATOMICITY:
2715 		case SPDK_NVME_FEAT_HOST_RESERVE_MASK:
2716 		case SPDK_NVME_FEAT_HOST_RESERVE_PERSIST:
2717 			response->status.sct = SPDK_NVME_SCT_PATH;
2718 			response->status.sc = _nvme_ana_state_to_path_status(ana_state);
2719 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2720 		default:
2721 			break;
2722 		}
2723 		break;
2724 	default:
2725 		break;
2726 	}
2727 
2728 	switch (feature) {
2729 	case SPDK_NVME_FEAT_ARBITRATION:
2730 		return get_features_generic(req, ctrlr->feat.arbitration.raw);
2731 	case SPDK_NVME_FEAT_POWER_MANAGEMENT:
2732 		return get_features_generic(req, ctrlr->feat.power_management.raw);
2733 	case SPDK_NVME_FEAT_TEMPERATURE_THRESHOLD:
2734 		return nvmf_ctrlr_get_features_temperature_threshold(req);
2735 	case SPDK_NVME_FEAT_ERROR_RECOVERY:
2736 		return get_features_generic(req, ctrlr->feat.error_recovery.raw);
2737 	case SPDK_NVME_FEAT_VOLATILE_WRITE_CACHE:
2738 		return get_features_generic(req, ctrlr->feat.volatile_write_cache.raw);
2739 	case SPDK_NVME_FEAT_NUMBER_OF_QUEUES:
2740 		return get_features_generic(req, ctrlr->feat.number_of_queues.raw);
2741 	case SPDK_NVME_FEAT_WRITE_ATOMICITY:
2742 		return get_features_generic(req, ctrlr->feat.write_atomicity.raw);
2743 	case SPDK_NVME_FEAT_ASYNC_EVENT_CONFIGURATION:
2744 		return get_features_generic(req, ctrlr->feat.async_event_configuration.raw);
2745 	case SPDK_NVME_FEAT_KEEP_ALIVE_TIMER:
2746 		return get_features_generic(req, ctrlr->feat.keep_alive_timer.raw);
2747 	case SPDK_NVME_FEAT_HOST_IDENTIFIER:
2748 		return nvmf_ctrlr_get_features_host_identifier(req);
2749 	case SPDK_NVME_FEAT_HOST_RESERVE_MASK:
2750 		return nvmf_ctrlr_get_features_reservation_notification_mask(req);
2751 	case SPDK_NVME_FEAT_HOST_RESERVE_PERSIST:
2752 		return nvmf_ctrlr_get_features_reservation_persistence(req);
2753 	default:
2754 		SPDK_ERRLOG("Get Features command with unsupported feature ID 0x%02x\n", feature);
2755 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
2756 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2757 	}
2758 }
2759 
2760 static int
2761 nvmf_ctrlr_set_features(struct spdk_nvmf_request *req)
2762 {
2763 	uint8_t feature, save;
2764 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
2765 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
2766 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
2767 	enum spdk_nvme_ana_state ana_state;
2768 	/*
2769 	 * Features are not saveable by the controller as indicated by
2770 	 * ONCS field of the Identify Controller data.
2771 	 * */
2772 	save = cmd->cdw10_bits.set_features.sv;
2773 	if (save) {
2774 		response->status.sc = SPDK_NVME_SC_FEATURE_ID_NOT_SAVEABLE;
2775 		response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
2776 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2777 	}
2778 
2779 	feature = cmd->cdw10_bits.set_features.fid;
2780 
2781 	if (ctrlr->subsys->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) {
2782 		/*
2783 		 * Features supported by Discovery controller
2784 		 */
2785 		switch (feature) {
2786 		case SPDK_NVME_FEAT_KEEP_ALIVE_TIMER:
2787 			return nvmf_ctrlr_set_features_keep_alive_timer(req);
2788 		case SPDK_NVME_FEAT_ASYNC_EVENT_CONFIGURATION:
2789 			return nvmf_ctrlr_set_features_async_event_configuration(req);
2790 		default:
2791 			SPDK_ERRLOG("Set Features command with unsupported feature ID 0x%02x\n", feature);
2792 			response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
2793 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2794 		}
2795 	}
2796 	/*
2797 	 * Process Set Features command for non-discovery controller
2798 	 */
2799 	ana_state = ctrlr->listener->ana_state;
2800 	switch (ana_state) {
2801 	case SPDK_NVME_ANA_INACCESSIBLE_STATE:
2802 	case SPDK_NVME_ANA_CHANGE_STATE:
2803 		if (cmd->nsid == SPDK_NVME_GLOBAL_NS_TAG) {
2804 			response->status.sct = SPDK_NVME_SCT_PATH;
2805 			response->status.sc = _nvme_ana_state_to_path_status(ana_state);
2806 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2807 		} else {
2808 			switch (feature) {
2809 			case SPDK_NVME_FEAT_ERROR_RECOVERY:
2810 			case SPDK_NVME_FEAT_WRITE_ATOMICITY:
2811 			case SPDK_NVME_FEAT_HOST_RESERVE_MASK:
2812 			case SPDK_NVME_FEAT_HOST_RESERVE_PERSIST:
2813 				response->status.sct = SPDK_NVME_SCT_PATH;
2814 				response->status.sc = _nvme_ana_state_to_path_status(ana_state);
2815 				return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2816 			default:
2817 				break;
2818 			}
2819 		}
2820 		break;
2821 	case SPDK_NVME_ANA_PERSISTENT_LOSS_STATE:
2822 		response->status.sct = SPDK_NVME_SCT_PATH;
2823 		response->status.sc = SPDK_NVME_SC_ASYMMETRIC_ACCESS_PERSISTENT_LOSS;
2824 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2825 	default:
2826 		break;
2827 	}
2828 
2829 	switch (feature) {
2830 	case SPDK_NVME_FEAT_ARBITRATION:
2831 		return nvmf_ctrlr_set_features_arbitration(req);
2832 	case SPDK_NVME_FEAT_POWER_MANAGEMENT:
2833 		return nvmf_ctrlr_set_features_power_management(req);
2834 	case SPDK_NVME_FEAT_TEMPERATURE_THRESHOLD:
2835 		return nvmf_ctrlr_set_features_temperature_threshold(req);
2836 	case SPDK_NVME_FEAT_ERROR_RECOVERY:
2837 		return nvmf_ctrlr_set_features_error_recovery(req);
2838 	case SPDK_NVME_FEAT_VOLATILE_WRITE_CACHE:
2839 		return nvmf_ctrlr_set_features_volatile_write_cache(req);
2840 	case SPDK_NVME_FEAT_NUMBER_OF_QUEUES:
2841 		return nvmf_ctrlr_set_features_number_of_queues(req);
2842 	case SPDK_NVME_FEAT_WRITE_ATOMICITY:
2843 		return nvmf_ctrlr_set_features_write_atomicity(req);
2844 	case SPDK_NVME_FEAT_ASYNC_EVENT_CONFIGURATION:
2845 		return nvmf_ctrlr_set_features_async_event_configuration(req);
2846 	case SPDK_NVME_FEAT_KEEP_ALIVE_TIMER:
2847 		return nvmf_ctrlr_set_features_keep_alive_timer(req);
2848 	case SPDK_NVME_FEAT_HOST_IDENTIFIER:
2849 		return nvmf_ctrlr_set_features_host_identifier(req);
2850 	case SPDK_NVME_FEAT_HOST_RESERVE_MASK:
2851 		return nvmf_ctrlr_set_features_reservation_notification_mask(req);
2852 	case SPDK_NVME_FEAT_HOST_RESERVE_PERSIST:
2853 		return nvmf_ctrlr_set_features_reservation_persistence(req);
2854 	case SPDK_NVME_FEAT_HOST_BEHAVIOR_SUPPORT:
2855 		return nvmf_ctrlr_set_features_host_behavior_support(req);
2856 	default:
2857 		SPDK_ERRLOG("Set Features command with unsupported feature ID 0x%02x\n", feature);
2858 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
2859 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2860 	}
2861 }
2862 
2863 static int
2864 nvmf_ctrlr_keep_alive(struct spdk_nvmf_request *req)
2865 {
2866 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
2867 
2868 	SPDK_DEBUGLOG(nvmf, "Keep Alive\n");
2869 	/*
2870 	 * To handle keep alive just clear or reset the
2871 	 * ctrlr based keep alive duration counter.
2872 	 * When added, a separate timer based process
2873 	 * will monitor if the time since last recorded
2874 	 * keep alive has exceeded the max duration and
2875 	 * take appropriate action.
2876 	 */
2877 	ctrlr->last_keep_alive_tick = spdk_get_ticks();
2878 
2879 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2880 }
2881 
2882 int
2883 nvmf_ctrlr_process_admin_cmd(struct spdk_nvmf_request *req)
2884 {
2885 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
2886 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
2887 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
2888 	int rc;
2889 
2890 	if (ctrlr == NULL) {
2891 		SPDK_ERRLOG("Admin command sent before CONNECT\n");
2892 		response->status.sct = SPDK_NVME_SCT_GENERIC;
2893 		response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
2894 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2895 	}
2896 
2897 	if (ctrlr->vcprop.cc.bits.en != 1) {
2898 		SPDK_ERRLOG("Admin command sent to disabled controller\n");
2899 		response->status.sct = SPDK_NVME_SCT_GENERIC;
2900 		response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
2901 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2902 	}
2903 
2904 	if (req->data && spdk_nvme_opc_get_data_transfer(cmd->opc) == SPDK_NVME_DATA_CONTROLLER_TO_HOST) {
2905 		memset(req->data, 0, req->length);
2906 	}
2907 
2908 	if (ctrlr->subsys->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) {
2909 		/* Discovery controllers only support these admin OPS. */
2910 		switch (cmd->opc) {
2911 		case SPDK_NVME_OPC_IDENTIFY:
2912 		case SPDK_NVME_OPC_GET_LOG_PAGE:
2913 		case SPDK_NVME_OPC_KEEP_ALIVE:
2914 		case SPDK_NVME_OPC_SET_FEATURES:
2915 		case SPDK_NVME_OPC_GET_FEATURES:
2916 		case SPDK_NVME_OPC_ASYNC_EVENT_REQUEST:
2917 			break;
2918 		default:
2919 			goto invalid_opcode;
2920 		}
2921 	}
2922 
2923 	/* Call a custom adm cmd handler if set. Aborts are handled in a different path (see nvmf_passthru_admin_cmd) */
2924 	if (g_nvmf_custom_admin_cmd_hdlrs[cmd->opc].hdlr && cmd->opc != SPDK_NVME_OPC_ABORT) {
2925 		rc = g_nvmf_custom_admin_cmd_hdlrs[cmd->opc].hdlr(req);
2926 		if (rc >= SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE) {
2927 			/* The handler took care of this commmand */
2928 			return rc;
2929 		}
2930 	}
2931 
2932 	switch (cmd->opc) {
2933 	case SPDK_NVME_OPC_GET_LOG_PAGE:
2934 		return nvmf_ctrlr_get_log_page(req);
2935 	case SPDK_NVME_OPC_IDENTIFY:
2936 		return nvmf_ctrlr_identify(req);
2937 	case SPDK_NVME_OPC_ABORT:
2938 		return nvmf_ctrlr_abort(req);
2939 	case SPDK_NVME_OPC_GET_FEATURES:
2940 		return nvmf_ctrlr_get_features(req);
2941 	case SPDK_NVME_OPC_SET_FEATURES:
2942 		return nvmf_ctrlr_set_features(req);
2943 	case SPDK_NVME_OPC_ASYNC_EVENT_REQUEST:
2944 		return nvmf_ctrlr_async_event_request(req);
2945 	case SPDK_NVME_OPC_KEEP_ALIVE:
2946 		return nvmf_ctrlr_keep_alive(req);
2947 
2948 	case SPDK_NVME_OPC_CREATE_IO_SQ:
2949 	case SPDK_NVME_OPC_CREATE_IO_CQ:
2950 	case SPDK_NVME_OPC_DELETE_IO_SQ:
2951 	case SPDK_NVME_OPC_DELETE_IO_CQ:
2952 		/* Create and Delete I/O CQ/SQ not allowed in NVMe-oF */
2953 		goto invalid_opcode;
2954 
2955 	default:
2956 		goto invalid_opcode;
2957 	}
2958 
2959 invalid_opcode:
2960 	SPDK_ERRLOG("Unsupported admin opcode 0x%x\n", cmd->opc);
2961 	response->status.sct = SPDK_NVME_SCT_GENERIC;
2962 	response->status.sc = SPDK_NVME_SC_INVALID_OPCODE;
2963 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2964 }
2965 
2966 static int
2967 nvmf_ctrlr_process_fabrics_cmd(struct spdk_nvmf_request *req)
2968 {
2969 	struct spdk_nvmf_qpair *qpair = req->qpair;
2970 	struct spdk_nvmf_capsule_cmd *cap_hdr;
2971 
2972 	cap_hdr = &req->cmd->nvmf_cmd;
2973 
2974 	if (qpair->ctrlr == NULL) {
2975 		/* No ctrlr established yet; the only valid command is Connect */
2976 		if (cap_hdr->fctype == SPDK_NVMF_FABRIC_COMMAND_CONNECT) {
2977 			return nvmf_ctrlr_cmd_connect(req);
2978 		} else {
2979 			SPDK_DEBUGLOG(nvmf, "Got fctype 0x%x, expected Connect\n",
2980 				      cap_hdr->fctype);
2981 			req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
2982 			req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
2983 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2984 		}
2985 	} else if (nvmf_qpair_is_admin_queue(qpair)) {
2986 		/*
2987 		 * Controller session is established, and this is an admin queue.
2988 		 * Disallow Connect and allow other fabrics commands.
2989 		 */
2990 		switch (cap_hdr->fctype) {
2991 		case SPDK_NVMF_FABRIC_COMMAND_PROPERTY_SET:
2992 			return nvmf_property_set(req);
2993 		case SPDK_NVMF_FABRIC_COMMAND_PROPERTY_GET:
2994 			return nvmf_property_get(req);
2995 		default:
2996 			SPDK_DEBUGLOG(nvmf, "unknown fctype 0x%02x\n",
2997 				      cap_hdr->fctype);
2998 			req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
2999 			req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_INVALID_OPCODE;
3000 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
3001 		}
3002 	} else {
3003 		/* Controller session is established, and this is an I/O queue */
3004 		/* For now, no I/O-specific Fabrics commands are implemented (other than Connect) */
3005 		SPDK_DEBUGLOG(nvmf, "Unexpected I/O fctype 0x%x\n", cap_hdr->fctype);
3006 		req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
3007 		req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_INVALID_OPCODE;
3008 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
3009 	}
3010 }
3011 
3012 static inline int
3013 nvmf_ctrlr_async_event_notification(struct spdk_nvmf_ctrlr *ctrlr,
3014 				    union spdk_nvme_async_event_completion *event)
3015 {
3016 	struct spdk_nvmf_request *req;
3017 	struct spdk_nvme_cpl *rsp;
3018 
3019 	assert(ctrlr->nr_aer_reqs > 0);
3020 
3021 	req = ctrlr->aer_req[--ctrlr->nr_aer_reqs];
3022 	rsp = &req->rsp->nvme_cpl;
3023 
3024 	rsp->cdw0 = event->raw;
3025 
3026 	_nvmf_request_complete(req);
3027 	ctrlr->aer_req[ctrlr->nr_aer_reqs] = NULL;
3028 
3029 	return 0;
3030 }
3031 
3032 static inline void
3033 nvmf_ctrlr_queue_pending_async_event(struct spdk_nvmf_ctrlr *ctrlr,
3034 				     union spdk_nvme_async_event_completion *event)
3035 {
3036 	struct spdk_nvmf_async_event_completion *nvmf_event;
3037 
3038 	nvmf_event = calloc(1, sizeof(*nvmf_event));
3039 	if (!nvmf_event) {
3040 		SPDK_ERRLOG("Alloc nvmf event failed, ignore the event\n");
3041 		return;
3042 	}
3043 	nvmf_event->event.raw = event->raw;
3044 	STAILQ_INSERT_TAIL(&ctrlr->async_events, nvmf_event, link);
3045 }
3046 
3047 int
3048 nvmf_ctrlr_async_event_ns_notice(struct spdk_nvmf_ctrlr *ctrlr)
3049 {
3050 	union spdk_nvme_async_event_completion event = {0};
3051 
3052 	/* Users may disable the event notification */
3053 	if (!ctrlr->feat.async_event_configuration.bits.ns_attr_notice) {
3054 		return 0;
3055 	}
3056 
3057 	if (!nvmf_ctrlr_mask_aen(ctrlr, SPDK_NVME_ASYNC_EVENT_NS_ATTR_CHANGE_MASK_BIT)) {
3058 		return 0;
3059 	}
3060 
3061 	event.bits.async_event_type = SPDK_NVME_ASYNC_EVENT_TYPE_NOTICE;
3062 	event.bits.async_event_info = SPDK_NVME_ASYNC_EVENT_NS_ATTR_CHANGED;
3063 	event.bits.log_page_identifier = SPDK_NVME_LOG_CHANGED_NS_LIST;
3064 
3065 	/* If there is no outstanding AER request, queue the event.  Then
3066 	 * if an AER is later submitted, this event can be sent as a
3067 	 * response.
3068 	 */
3069 	if (ctrlr->nr_aer_reqs == 0) {
3070 		nvmf_ctrlr_queue_pending_async_event(ctrlr, &event);
3071 		return 0;
3072 	}
3073 
3074 	return nvmf_ctrlr_async_event_notification(ctrlr, &event);
3075 }
3076 
3077 int
3078 nvmf_ctrlr_async_event_ana_change_notice(struct spdk_nvmf_ctrlr *ctrlr)
3079 {
3080 	union spdk_nvme_async_event_completion event = {0};
3081 
3082 	/* Users may disable the event notification */
3083 	if (!ctrlr->feat.async_event_configuration.bits.ana_change_notice) {
3084 		return 0;
3085 	}
3086 
3087 	if (!nvmf_ctrlr_mask_aen(ctrlr, SPDK_NVME_ASYNC_EVENT_ANA_CHANGE_MASK_BIT)) {
3088 		return 0;
3089 	}
3090 
3091 	event.bits.async_event_type = SPDK_NVME_ASYNC_EVENT_TYPE_NOTICE;
3092 	event.bits.async_event_info = SPDK_NVME_ASYNC_EVENT_ANA_CHANGE;
3093 	event.bits.log_page_identifier = SPDK_NVME_LOG_ASYMMETRIC_NAMESPACE_ACCESS;
3094 
3095 	/* If there is no outstanding AER request, queue the event.  Then
3096 	 * if an AER is later submitted, this event can be sent as a
3097 	 * response.
3098 	 */
3099 	if (ctrlr->nr_aer_reqs == 0) {
3100 		nvmf_ctrlr_queue_pending_async_event(ctrlr, &event);
3101 		return 0;
3102 	}
3103 
3104 	return nvmf_ctrlr_async_event_notification(ctrlr, &event);
3105 }
3106 
3107 void
3108 nvmf_ctrlr_async_event_reservation_notification(struct spdk_nvmf_ctrlr *ctrlr)
3109 {
3110 	union spdk_nvme_async_event_completion event = {0};
3111 
3112 	if (!ctrlr->num_avail_log_pages) {
3113 		return;
3114 	}
3115 
3116 	if (!nvmf_ctrlr_mask_aen(ctrlr, SPDK_NVME_ASYNC_EVENT_RESERVATION_LOG_AVAIL_MASK_BIT)) {
3117 		return;
3118 	}
3119 
3120 	event.bits.async_event_type = SPDK_NVME_ASYNC_EVENT_TYPE_IO;
3121 	event.bits.async_event_info = SPDK_NVME_ASYNC_EVENT_RESERVATION_LOG_AVAIL;
3122 	event.bits.log_page_identifier = SPDK_NVME_LOG_RESERVATION_NOTIFICATION;
3123 
3124 	/* If there is no outstanding AER request, queue the event.  Then
3125 	 * if an AER is later submitted, this event can be sent as a
3126 	 * response.
3127 	 */
3128 	if (ctrlr->nr_aer_reqs == 0) {
3129 		nvmf_ctrlr_queue_pending_async_event(ctrlr, &event);
3130 		return;
3131 	}
3132 
3133 	nvmf_ctrlr_async_event_notification(ctrlr, &event);
3134 }
3135 
3136 int
3137 nvmf_ctrlr_async_event_discovery_log_change_notice(struct spdk_nvmf_ctrlr *ctrlr)
3138 {
3139 	union spdk_nvme_async_event_completion event = {0};
3140 
3141 	/* Users may disable the event notification manually or
3142 	 * it may not be enabled due to keep alive timeout
3143 	 * not being set in connect command to discovery controller.
3144 	 */
3145 	if (!ctrlr->feat.async_event_configuration.bits.discovery_log_change_notice) {
3146 		return 0;
3147 	}
3148 
3149 	if (!nvmf_ctrlr_mask_aen(ctrlr, SPDK_NVME_ASYNC_EVENT_DISCOVERY_LOG_CHANGE_MASK_BIT)) {
3150 		return 0;
3151 	}
3152 
3153 	event.bits.async_event_type = SPDK_NVME_ASYNC_EVENT_TYPE_NOTICE;
3154 	event.bits.async_event_info = SPDK_NVME_ASYNC_EVENT_DISCOVERY_LOG_CHANGE;
3155 	event.bits.log_page_identifier = SPDK_NVME_LOG_DISCOVERY;
3156 
3157 	/* If there is no outstanding AER request, queue the event.  Then
3158 	 * if an AER is later submitted, this event can be sent as a
3159 	 * response.
3160 	 */
3161 	if (ctrlr->nr_aer_reqs == 0) {
3162 		nvmf_ctrlr_queue_pending_async_event(ctrlr, &event);
3163 		return 0;
3164 	}
3165 
3166 	return nvmf_ctrlr_async_event_notification(ctrlr, &event);
3167 }
3168 
3169 void
3170 nvmf_qpair_free_aer(struct spdk_nvmf_qpair *qpair)
3171 {
3172 	struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr;
3173 	int i;
3174 
3175 	if (!nvmf_qpair_is_admin_queue(qpair)) {
3176 		return;
3177 	}
3178 
3179 	for (i = 0; i < ctrlr->nr_aer_reqs; i++) {
3180 		spdk_nvmf_request_free(ctrlr->aer_req[i]);
3181 		ctrlr->aer_req[i] = NULL;
3182 	}
3183 
3184 	ctrlr->nr_aer_reqs = 0;
3185 }
3186 
3187 void
3188 nvmf_ctrlr_abort_aer(struct spdk_nvmf_ctrlr *ctrlr)
3189 {
3190 	struct spdk_nvmf_request *req;
3191 	int i;
3192 
3193 	for (i = 0; i < ctrlr->nr_aer_reqs; i++) {
3194 		req = ctrlr->aer_req[i];
3195 
3196 		req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
3197 		req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_ABORTED_BY_REQUEST;
3198 		_nvmf_request_complete(req);
3199 
3200 		ctrlr->aer_req[i] = NULL;
3201 	}
3202 
3203 	ctrlr->nr_aer_reqs = 0;
3204 }
3205 
3206 static void
3207 _nvmf_ctrlr_add_reservation_log(void *ctx)
3208 {
3209 	struct spdk_nvmf_reservation_log *log = (struct spdk_nvmf_reservation_log *)ctx;
3210 	struct spdk_nvmf_ctrlr *ctrlr = log->ctrlr;
3211 
3212 	ctrlr->log_page_count++;
3213 
3214 	/* Maximum number of queued log pages is 255 */
3215 	if (ctrlr->num_avail_log_pages == 0xff) {
3216 		struct spdk_nvmf_reservation_log *entry;
3217 		entry = TAILQ_LAST(&ctrlr->log_head, log_page_head);
3218 		entry->log.log_page_count = ctrlr->log_page_count;
3219 		free(log);
3220 		return;
3221 	}
3222 
3223 	log->log.log_page_count = ctrlr->log_page_count;
3224 	log->log.num_avail_log_pages = ctrlr->num_avail_log_pages++;
3225 	TAILQ_INSERT_TAIL(&ctrlr->log_head, log, link);
3226 
3227 	nvmf_ctrlr_async_event_reservation_notification(ctrlr);
3228 }
3229 
3230 void
3231 nvmf_ctrlr_reservation_notice_log(struct spdk_nvmf_ctrlr *ctrlr,
3232 				  struct spdk_nvmf_ns *ns,
3233 				  enum spdk_nvme_reservation_notification_log_page_type type)
3234 {
3235 	struct spdk_nvmf_reservation_log *log;
3236 
3237 	switch (type) {
3238 	case SPDK_NVME_RESERVATION_LOG_PAGE_EMPTY:
3239 		return;
3240 	case SPDK_NVME_REGISTRATION_PREEMPTED:
3241 		if (ns->mask & SPDK_NVME_REGISTRATION_PREEMPTED_MASK) {
3242 			return;
3243 		}
3244 		break;
3245 	case SPDK_NVME_RESERVATION_RELEASED:
3246 		if (ns->mask & SPDK_NVME_RESERVATION_RELEASED_MASK) {
3247 			return;
3248 		}
3249 		break;
3250 	case SPDK_NVME_RESERVATION_PREEMPTED:
3251 		if (ns->mask & SPDK_NVME_RESERVATION_PREEMPTED_MASK) {
3252 			return;
3253 		}
3254 		break;
3255 	default:
3256 		return;
3257 	}
3258 
3259 	log = calloc(1, sizeof(*log));
3260 	if (!log) {
3261 		SPDK_ERRLOG("Alloc log page failed, ignore the log\n");
3262 		return;
3263 	}
3264 	log->ctrlr = ctrlr;
3265 	log->log.type = type;
3266 	log->log.nsid = ns->nsid;
3267 
3268 	spdk_thread_send_msg(ctrlr->thread, _nvmf_ctrlr_add_reservation_log, log);
3269 }
3270 
3271 /* Check from subsystem poll group's namespace information data structure */
3272 static bool
3273 nvmf_ns_info_ctrlr_is_registrant(struct spdk_nvmf_subsystem_pg_ns_info *ns_info,
3274 				 struct spdk_nvmf_ctrlr *ctrlr)
3275 {
3276 	uint32_t i;
3277 
3278 	for (i = 0; i < SPDK_NVMF_MAX_NUM_REGISTRANTS; i++) {
3279 		if (!spdk_uuid_compare(&ns_info->reg_hostid[i], &ctrlr->hostid)) {
3280 			return true;
3281 		}
3282 	}
3283 
3284 	return false;
3285 }
3286 
3287 /*
3288  * Check the NVMe command is permitted or not for current controller(Host).
3289  */
3290 static int
3291 nvmf_ns_reservation_request_check(struct spdk_nvmf_subsystem_pg_ns_info *ns_info,
3292 				  struct spdk_nvmf_ctrlr *ctrlr,
3293 				  struct spdk_nvmf_request *req)
3294 {
3295 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
3296 	enum spdk_nvme_reservation_type rtype = ns_info->rtype;
3297 	uint8_t status = SPDK_NVME_SC_SUCCESS;
3298 	uint8_t racqa;
3299 	bool is_registrant;
3300 
3301 	/* No valid reservation */
3302 	if (!rtype) {
3303 		return 0;
3304 	}
3305 
3306 	is_registrant = nvmf_ns_info_ctrlr_is_registrant(ns_info, ctrlr);
3307 	/* All registrants type and current ctrlr is a valid registrant */
3308 	if ((rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_ALL_REGS ||
3309 	     rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_ALL_REGS) && is_registrant) {
3310 		return 0;
3311 	} else if (!spdk_uuid_compare(&ns_info->holder_id, &ctrlr->hostid)) {
3312 		return 0;
3313 	}
3314 
3315 	/* Non-holder for current controller */
3316 	switch (cmd->opc) {
3317 	case SPDK_NVME_OPC_READ:
3318 	case SPDK_NVME_OPC_COMPARE:
3319 		if (rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS) {
3320 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
3321 			goto exit;
3322 		}
3323 		if ((rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_REG_ONLY ||
3324 		     rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_ALL_REGS) && !is_registrant) {
3325 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
3326 		}
3327 		break;
3328 	case SPDK_NVME_OPC_FLUSH:
3329 	case SPDK_NVME_OPC_WRITE:
3330 	case SPDK_NVME_OPC_WRITE_UNCORRECTABLE:
3331 	case SPDK_NVME_OPC_WRITE_ZEROES:
3332 	case SPDK_NVME_OPC_DATASET_MANAGEMENT:
3333 		if (rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE ||
3334 		    rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS) {
3335 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
3336 			goto exit;
3337 		}
3338 		if (!is_registrant) {
3339 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
3340 		}
3341 		break;
3342 	case SPDK_NVME_OPC_RESERVATION_ACQUIRE:
3343 		racqa = cmd->cdw10_bits.resv_acquire.racqa;
3344 		if (racqa == SPDK_NVME_RESERVE_ACQUIRE) {
3345 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
3346 			goto exit;
3347 		}
3348 		if (!is_registrant) {
3349 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
3350 		}
3351 		break;
3352 	case SPDK_NVME_OPC_RESERVATION_RELEASE:
3353 		if (!is_registrant) {
3354 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
3355 		}
3356 		break;
3357 	default:
3358 		break;
3359 	}
3360 
3361 exit:
3362 	req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
3363 	req->rsp->nvme_cpl.status.sc = status;
3364 	if (status == SPDK_NVME_SC_RESERVATION_CONFLICT) {
3365 		return -EPERM;
3366 	}
3367 
3368 	return 0;
3369 }
3370 
3371 static int
3372 nvmf_ctrlr_process_io_fused_cmd(struct spdk_nvmf_request *req, struct spdk_bdev *bdev,
3373 				struct spdk_bdev_desc *desc, struct spdk_io_channel *ch)
3374 {
3375 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
3376 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
3377 	struct spdk_nvmf_request *first_fused_req = req->qpair->first_fused_req;
3378 	int rc;
3379 
3380 	if (cmd->fuse == SPDK_NVME_CMD_FUSE_FIRST) {
3381 		/* first fused operation (should be compare) */
3382 		if (first_fused_req != NULL) {
3383 			struct spdk_nvme_cpl *fused_response = &first_fused_req->rsp->nvme_cpl;
3384 
3385 			SPDK_ERRLOG("Wrong sequence of fused operations\n");
3386 
3387 			/* abort req->qpair->first_fused_request and continue with new fused command */
3388 			fused_response->status.sc = SPDK_NVME_SC_ABORTED_MISSING_FUSED;
3389 			fused_response->status.sct = SPDK_NVME_SCT_GENERIC;
3390 			_nvmf_request_complete(first_fused_req);
3391 		} else if (cmd->opc != SPDK_NVME_OPC_COMPARE) {
3392 			SPDK_ERRLOG("Wrong op code of fused operations\n");
3393 			rsp->status.sct = SPDK_NVME_SCT_GENERIC;
3394 			rsp->status.sc = SPDK_NVME_SC_INVALID_OPCODE;
3395 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
3396 		}
3397 
3398 		req->qpair->first_fused_req = req;
3399 		return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
3400 	} else if (cmd->fuse == SPDK_NVME_CMD_FUSE_SECOND) {
3401 		/* second fused operation (should be write) */
3402 		if (first_fused_req == NULL) {
3403 			SPDK_ERRLOG("Wrong sequence of fused operations\n");
3404 			rsp->status.sct = SPDK_NVME_SCT_GENERIC;
3405 			rsp->status.sc = SPDK_NVME_SC_ABORTED_MISSING_FUSED;
3406 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
3407 		} else if (cmd->opc != SPDK_NVME_OPC_WRITE) {
3408 			struct spdk_nvme_cpl *fused_response = &first_fused_req->rsp->nvme_cpl;
3409 
3410 			SPDK_ERRLOG("Wrong op code of fused operations\n");
3411 
3412 			/* abort req->qpair->first_fused_request and fail current command */
3413 			fused_response->status.sc = SPDK_NVME_SC_ABORTED_MISSING_FUSED;
3414 			fused_response->status.sct = SPDK_NVME_SCT_GENERIC;
3415 			_nvmf_request_complete(first_fused_req);
3416 
3417 			rsp->status.sct = SPDK_NVME_SCT_GENERIC;
3418 			rsp->status.sc = SPDK_NVME_SC_INVALID_OPCODE;
3419 			req->qpair->first_fused_req = NULL;
3420 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
3421 		}
3422 
3423 		/* save request of first command to generate response later */
3424 		req->first_fused_req = first_fused_req;
3425 		req->qpair->first_fused_req = NULL;
3426 	} else {
3427 		SPDK_ERRLOG("Invalid fused command fuse field.\n");
3428 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
3429 		rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
3430 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
3431 	}
3432 
3433 	rc = nvmf_bdev_ctrlr_compare_and_write_cmd(bdev, desc, ch, req->first_fused_req, req);
3434 
3435 	if (rc == SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE) {
3436 		if (spdk_nvme_cpl_is_error(rsp)) {
3437 			struct spdk_nvme_cpl *fused_response = &first_fused_req->rsp->nvme_cpl;
3438 
3439 			fused_response->status = rsp->status;
3440 			rsp->status.sct = SPDK_NVME_SCT_GENERIC;
3441 			rsp->status.sc = SPDK_NVME_SC_ABORTED_FAILED_FUSED;
3442 			/* Complete first of fused commands. Second will be completed by upper layer */
3443 			_nvmf_request_complete(first_fused_req);
3444 			req->first_fused_req = NULL;
3445 		}
3446 	}
3447 
3448 	return rc;
3449 }
3450 
3451 int
3452 nvmf_ctrlr_process_io_cmd(struct spdk_nvmf_request *req)
3453 {
3454 	uint32_t nsid;
3455 	struct spdk_nvmf_ns *ns;
3456 	struct spdk_bdev *bdev;
3457 	struct spdk_bdev_desc *desc;
3458 	struct spdk_io_channel *ch;
3459 	struct spdk_nvmf_poll_group *group = req->qpair->group;
3460 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
3461 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
3462 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
3463 	struct spdk_nvmf_subsystem_pg_ns_info *ns_info;
3464 	enum spdk_nvme_ana_state ana_state;
3465 
3466 	/* pre-set response details for this command */
3467 	response->status.sc = SPDK_NVME_SC_SUCCESS;
3468 	nsid = cmd->nsid;
3469 
3470 	if (spdk_unlikely(ctrlr == NULL)) {
3471 		SPDK_ERRLOG("I/O command sent before CONNECT\n");
3472 		response->status.sct = SPDK_NVME_SCT_GENERIC;
3473 		response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
3474 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
3475 	}
3476 
3477 	if (spdk_unlikely(ctrlr->vcprop.cc.bits.en != 1)) {
3478 		SPDK_ERRLOG("I/O command sent to disabled controller\n");
3479 		response->status.sct = SPDK_NVME_SCT_GENERIC;
3480 		response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
3481 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
3482 	}
3483 
3484 	/* It will be lower overhead to check if ANA state is optimized or
3485 	 * non-optimized.
3486 	 */
3487 	ana_state = ctrlr->listener->ana_state;
3488 	if (spdk_unlikely(ana_state != SPDK_NVME_ANA_OPTIMIZED_STATE &&
3489 			  ana_state != SPDK_NVME_ANA_NON_OPTIMIZED_STATE)) {
3490 		SPDK_DEBUGLOG(nvmf, "Fail I/O command due to ANA state %d\n",
3491 			      ana_state);
3492 		response->status.sct = SPDK_NVME_SCT_PATH;
3493 		response->status.sc = _nvme_ana_state_to_path_status(ana_state);
3494 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
3495 	}
3496 
3497 	ns = _nvmf_subsystem_get_ns(ctrlr->subsys, nsid);
3498 	if (ns == NULL || ns->bdev == NULL) {
3499 		SPDK_DEBUGLOG(nvmf, "Unsuccessful query for nsid %u\n", cmd->nsid);
3500 		response->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT;
3501 		response->status.dnr = 1;
3502 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
3503 	}
3504 
3505 	/* scan-build falsely reporting dereference of null pointer */
3506 	assert(group != NULL && group->sgroups != NULL);
3507 	ns_info = &group->sgroups[ctrlr->subsys->id].ns_info[nsid - 1];
3508 	if (nvmf_ns_reservation_request_check(ns_info, ctrlr, req)) {
3509 		SPDK_DEBUGLOG(nvmf, "Reservation Conflict for nsid %u, opcode %u\n",
3510 			      cmd->nsid, cmd->opc);
3511 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
3512 	}
3513 
3514 	bdev = ns->bdev;
3515 	desc = ns->desc;
3516 	ch = ns_info->channel;
3517 
3518 	if (spdk_unlikely(cmd->fuse & SPDK_NVME_CMD_FUSE_MASK)) {
3519 		return nvmf_ctrlr_process_io_fused_cmd(req, bdev, desc, ch);
3520 	} else if (spdk_unlikely(req->qpair->first_fused_req != NULL)) {
3521 		struct spdk_nvme_cpl *fused_response = &req->qpair->first_fused_req->rsp->nvme_cpl;
3522 
3523 		SPDK_ERRLOG("Expected second of fused commands - failing first of fused commands\n");
3524 
3525 		/* abort req->qpair->first_fused_request and continue with new command */
3526 		fused_response->status.sc = SPDK_NVME_SC_ABORTED_MISSING_FUSED;
3527 		fused_response->status.sct = SPDK_NVME_SCT_GENERIC;
3528 		_nvmf_request_complete(req->qpair->first_fused_req);
3529 		req->qpair->first_fused_req = NULL;
3530 	}
3531 
3532 	switch (cmd->opc) {
3533 	case SPDK_NVME_OPC_READ:
3534 		return nvmf_bdev_ctrlr_read_cmd(bdev, desc, ch, req);
3535 	case SPDK_NVME_OPC_WRITE:
3536 		return nvmf_bdev_ctrlr_write_cmd(bdev, desc, ch, req);
3537 	case SPDK_NVME_OPC_COMPARE:
3538 		return nvmf_bdev_ctrlr_compare_cmd(bdev, desc, ch, req);
3539 	case SPDK_NVME_OPC_WRITE_ZEROES:
3540 		return nvmf_bdev_ctrlr_write_zeroes_cmd(bdev, desc, ch, req);
3541 	case SPDK_NVME_OPC_FLUSH:
3542 		return nvmf_bdev_ctrlr_flush_cmd(bdev, desc, ch, req);
3543 	case SPDK_NVME_OPC_DATASET_MANAGEMENT:
3544 		return nvmf_bdev_ctrlr_dsm_cmd(bdev, desc, ch, req);
3545 	case SPDK_NVME_OPC_RESERVATION_REGISTER:
3546 	case SPDK_NVME_OPC_RESERVATION_ACQUIRE:
3547 	case SPDK_NVME_OPC_RESERVATION_RELEASE:
3548 	case SPDK_NVME_OPC_RESERVATION_REPORT:
3549 		spdk_thread_send_msg(ctrlr->subsys->thread, nvmf_ns_reservation_request, req);
3550 		return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
3551 	default:
3552 		return nvmf_bdev_ctrlr_nvme_passthru_io(bdev, desc, ch, req);
3553 	}
3554 }
3555 
3556 static void
3557 nvmf_qpair_request_cleanup(struct spdk_nvmf_qpair *qpair)
3558 {
3559 	if (qpair->state == SPDK_NVMF_QPAIR_DEACTIVATING) {
3560 		assert(qpair->state_cb != NULL);
3561 
3562 		if (TAILQ_EMPTY(&qpair->outstanding)) {
3563 			qpair->state_cb(qpair->state_cb_arg, 0);
3564 		}
3565 	}
3566 }
3567 
3568 int
3569 spdk_nvmf_request_free(struct spdk_nvmf_request *req)
3570 {
3571 	struct spdk_nvmf_qpair *qpair = req->qpair;
3572 
3573 	TAILQ_REMOVE(&qpair->outstanding, req, link);
3574 	if (nvmf_transport_req_free(req)) {
3575 		SPDK_ERRLOG("Unable to free transport level request resources.\n");
3576 	}
3577 
3578 	nvmf_qpair_request_cleanup(qpair);
3579 
3580 	return 0;
3581 }
3582 
3583 static void
3584 _nvmf_request_complete(void *ctx)
3585 {
3586 	struct spdk_nvmf_request *req = ctx;
3587 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
3588 	struct spdk_nvmf_qpair *qpair;
3589 	struct spdk_nvmf_subsystem_poll_group *sgroup = NULL;
3590 	struct spdk_nvmf_subsystem_pg_ns_info *ns_info;
3591 	bool is_aer = false;
3592 	uint32_t nsid;
3593 	bool paused;
3594 	uint8_t opcode;
3595 
3596 	rsp->sqid = 0;
3597 	rsp->status.p = 0;
3598 	rsp->cid = req->cmd->nvme_cmd.cid;
3599 	nsid = req->cmd->nvme_cmd.nsid;
3600 	opcode = req->cmd->nvmf_cmd.opcode;
3601 
3602 	qpair = req->qpair;
3603 	if (qpair->ctrlr) {
3604 		sgroup = &qpair->group->sgroups[qpair->ctrlr->subsys->id];
3605 		assert(sgroup != NULL);
3606 		is_aer = req->cmd->nvme_cmd.opc == SPDK_NVME_OPC_ASYNC_EVENT_REQUEST;
3607 
3608 		/*
3609 		 * Set the crd value.
3610 		 * If the the IO has any error, and dnr (DoNotRetry) is not 1,
3611 		 * and ACRE is enabled, we will set the crd to 1 to select the first CRDT.
3612 		 */
3613 		if (spdk_nvme_cpl_is_error(rsp) &&
3614 		    rsp->status.dnr == 0 &&
3615 		    qpair->ctrlr->acre_enabled) {
3616 			rsp->status.crd = 1;
3617 		}
3618 	} else if (spdk_unlikely(nvmf_request_is_fabric_connect(req))) {
3619 		sgroup = nvmf_subsystem_pg_from_connect_cmd(req);
3620 	}
3621 
3622 	if (SPDK_DEBUGLOG_FLAG_ENABLED("nvmf")) {
3623 		spdk_nvme_print_completion(qpair->qid, rsp);
3624 	}
3625 
3626 	TAILQ_REMOVE(&qpair->outstanding, req, link);
3627 	if (nvmf_transport_req_complete(req)) {
3628 		SPDK_ERRLOG("Transport request completion error!\n");
3629 	}
3630 
3631 	/* AER cmd is an exception */
3632 	if (sgroup && !is_aer) {
3633 		if (spdk_unlikely(opcode == SPDK_NVME_OPC_FABRIC ||
3634 				  nvmf_qpair_is_admin_queue(qpair))) {
3635 			assert(sgroup->mgmt_io_outstanding > 0);
3636 			sgroup->mgmt_io_outstanding--;
3637 		} else {
3638 			/* NOTE: This implicitly also checks for 0, since 0 - 1 wraps around to UINT32_MAX. */
3639 			if (spdk_likely(nsid - 1 < sgroup->num_ns)) {
3640 				sgroup->ns_info[nsid - 1].io_outstanding--;
3641 			}
3642 		}
3643 
3644 		if (spdk_unlikely(sgroup->state == SPDK_NVMF_SUBSYSTEM_PAUSING &&
3645 				  sgroup->mgmt_io_outstanding == 0)) {
3646 			paused = true;
3647 			for (nsid = 0; nsid < sgroup->num_ns; nsid++) {
3648 				ns_info = &sgroup->ns_info[nsid];
3649 
3650 				if (ns_info->state == SPDK_NVMF_SUBSYSTEM_PAUSING &&
3651 				    ns_info->io_outstanding > 0) {
3652 					paused = false;
3653 					break;
3654 				}
3655 			}
3656 
3657 			if (paused) {
3658 				sgroup->state = SPDK_NVMF_SUBSYSTEM_PAUSED;
3659 				sgroup->cb_fn(sgroup->cb_arg, 0);
3660 				sgroup->cb_fn = NULL;
3661 				sgroup->cb_arg = NULL;
3662 			}
3663 		}
3664 
3665 	}
3666 
3667 	nvmf_qpair_request_cleanup(qpair);
3668 }
3669 
3670 int
3671 spdk_nvmf_request_complete(struct spdk_nvmf_request *req)
3672 {
3673 	struct spdk_nvmf_qpair *qpair = req->qpair;
3674 
3675 	if (spdk_likely(qpair->group->thread == spdk_get_thread())) {
3676 		_nvmf_request_complete(req);
3677 	} else {
3678 		spdk_thread_send_msg(qpair->group->thread,
3679 				     _nvmf_request_complete, req);
3680 	}
3681 
3682 	return 0;
3683 }
3684 
3685 void
3686 spdk_nvmf_request_exec_fabrics(struct spdk_nvmf_request *req)
3687 {
3688 	struct spdk_nvmf_qpair *qpair = req->qpair;
3689 	struct spdk_nvmf_subsystem_poll_group *sgroup = NULL;
3690 	enum spdk_nvmf_request_exec_status status;
3691 
3692 	if (qpair->ctrlr) {
3693 		sgroup = &qpair->group->sgroups[qpair->ctrlr->subsys->id];
3694 	} else if (spdk_unlikely(nvmf_request_is_fabric_connect(req))) {
3695 		sgroup = nvmf_subsystem_pg_from_connect_cmd(req);
3696 	}
3697 
3698 	assert(sgroup != NULL);
3699 	sgroup->mgmt_io_outstanding++;
3700 
3701 	/* Place the request on the outstanding list so we can keep track of it */
3702 	TAILQ_INSERT_TAIL(&qpair->outstanding, req, link);
3703 
3704 	assert(req->cmd->nvmf_cmd.opcode == SPDK_NVME_OPC_FABRIC);
3705 	status = nvmf_ctrlr_process_fabrics_cmd(req);
3706 
3707 	if (status == SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE) {
3708 		_nvmf_request_complete(req);
3709 	}
3710 }
3711 
3712 void
3713 spdk_nvmf_request_exec(struct spdk_nvmf_request *req)
3714 {
3715 	struct spdk_nvmf_qpair *qpair = req->qpair;
3716 	struct spdk_nvmf_subsystem_poll_group *sgroup = NULL;
3717 	struct spdk_nvmf_subsystem_pg_ns_info *ns_info;
3718 	enum spdk_nvmf_request_exec_status status;
3719 	uint32_t nsid;
3720 
3721 	if (qpair->ctrlr) {
3722 		sgroup = &qpair->group->sgroups[qpair->ctrlr->subsys->id];
3723 		assert(sgroup != NULL);
3724 	} else if (spdk_unlikely(nvmf_request_is_fabric_connect(req))) {
3725 		sgroup = nvmf_subsystem_pg_from_connect_cmd(req);
3726 	}
3727 
3728 	/* Check if the subsystem is paused (if there is a subsystem) */
3729 	if (sgroup != NULL) {
3730 		if (spdk_unlikely(req->cmd->nvmf_cmd.opcode == SPDK_NVME_OPC_FABRIC ||
3731 				  nvmf_qpair_is_admin_queue(qpair))) {
3732 			if (sgroup->state != SPDK_NVMF_SUBSYSTEM_ACTIVE) {
3733 				/* The subsystem is not currently active. Queue this request. */
3734 				TAILQ_INSERT_TAIL(&sgroup->queued, req, link);
3735 				return;
3736 			}
3737 			sgroup->mgmt_io_outstanding++;
3738 		} else {
3739 			nsid = req->cmd->nvme_cmd.nsid;
3740 
3741 			/* NOTE: This implicitly also checks for 0, since 0 - 1 wraps around to UINT32_MAX. */
3742 			if (spdk_unlikely(nsid - 1 >= sgroup->num_ns)) {
3743 				req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
3744 				req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT;
3745 				req->rsp->nvme_cpl.status.dnr = 1;
3746 				TAILQ_INSERT_TAIL(&qpair->outstanding, req, link);
3747 				_nvmf_request_complete(req);
3748 				return;
3749 			}
3750 
3751 			ns_info = &sgroup->ns_info[nsid - 1];
3752 			if (ns_info->channel == NULL) {
3753 				/* This can can happen if host sends I/O to a namespace that is
3754 				 * in the process of being added, but before the full addition
3755 				 * process is complete.  Report invalid namespace in that case.
3756 				 */
3757 				req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
3758 				req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT;
3759 				req->rsp->nvme_cpl.status.dnr = 1;
3760 				TAILQ_INSERT_TAIL(&qpair->outstanding, req, link);
3761 				ns_info->io_outstanding++;
3762 				_nvmf_request_complete(req);
3763 				return;
3764 			}
3765 
3766 			if (ns_info->state != SPDK_NVMF_SUBSYSTEM_ACTIVE) {
3767 				/* The namespace is not currently active. Queue this request. */
3768 				TAILQ_INSERT_TAIL(&sgroup->queued, req, link);
3769 				return;
3770 			}
3771 			ns_info->io_outstanding++;
3772 		}
3773 	}
3774 
3775 	if (qpair->state != SPDK_NVMF_QPAIR_ACTIVE) {
3776 		req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
3777 		req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
3778 		TAILQ_INSERT_TAIL(&qpair->outstanding, req, link);
3779 		_nvmf_request_complete(req);
3780 		return;
3781 	}
3782 
3783 	if (SPDK_DEBUGLOG_FLAG_ENABLED("nvmf")) {
3784 		spdk_nvme_print_command(qpair->qid, &req->cmd->nvme_cmd);
3785 	}
3786 
3787 	/* Place the request on the outstanding list so we can keep track of it */
3788 	TAILQ_INSERT_TAIL(&qpair->outstanding, req, link);
3789 
3790 	if (spdk_unlikely(req->cmd->nvmf_cmd.opcode == SPDK_NVME_OPC_FABRIC)) {
3791 		status = nvmf_ctrlr_process_fabrics_cmd(req);
3792 	} else if (spdk_unlikely(nvmf_qpair_is_admin_queue(qpair))) {
3793 		status = nvmf_ctrlr_process_admin_cmd(req);
3794 	} else {
3795 		status = nvmf_ctrlr_process_io_cmd(req);
3796 	}
3797 
3798 	if (status == SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE) {
3799 		_nvmf_request_complete(req);
3800 	}
3801 }
3802 
3803 static bool
3804 nvmf_ctrlr_get_dif_ctx(struct spdk_nvmf_ctrlr *ctrlr, struct spdk_nvme_cmd *cmd,
3805 		       struct spdk_dif_ctx *dif_ctx)
3806 {
3807 	struct spdk_nvmf_ns *ns;
3808 	struct spdk_bdev *bdev;
3809 
3810 	if (ctrlr == NULL || cmd == NULL) {
3811 		return false;
3812 	}
3813 
3814 	ns = _nvmf_subsystem_get_ns(ctrlr->subsys, cmd->nsid);
3815 	if (ns == NULL || ns->bdev == NULL) {
3816 		return false;
3817 	}
3818 
3819 	bdev = ns->bdev;
3820 
3821 	switch (cmd->opc) {
3822 	case SPDK_NVME_OPC_READ:
3823 	case SPDK_NVME_OPC_WRITE:
3824 	case SPDK_NVME_OPC_COMPARE:
3825 		return nvmf_bdev_ctrlr_get_dif_ctx(bdev, cmd, dif_ctx);
3826 	default:
3827 		break;
3828 	}
3829 
3830 	return false;
3831 }
3832 
3833 bool
3834 spdk_nvmf_request_get_dif_ctx(struct spdk_nvmf_request *req, struct spdk_dif_ctx *dif_ctx)
3835 {
3836 	struct spdk_nvmf_qpair *qpair = req->qpair;
3837 	struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr;
3838 
3839 	if (spdk_likely(ctrlr == NULL || !ctrlr->dif_insert_or_strip)) {
3840 		return false;
3841 	}
3842 
3843 	if (spdk_unlikely(qpair->state != SPDK_NVMF_QPAIR_ACTIVE)) {
3844 		return false;
3845 	}
3846 
3847 	if (spdk_unlikely(req->cmd->nvmf_cmd.opcode == SPDK_NVME_OPC_FABRIC)) {
3848 		return false;
3849 	}
3850 
3851 	if (spdk_unlikely(nvmf_qpair_is_admin_queue(qpair))) {
3852 		return false;
3853 	}
3854 
3855 	return nvmf_ctrlr_get_dif_ctx(ctrlr, &req->cmd->nvme_cmd, dif_ctx);
3856 }
3857 
3858 void
3859 spdk_nvmf_set_custom_admin_cmd_hdlr(uint8_t opc, spdk_nvmf_custom_cmd_hdlr hdlr)
3860 {
3861 	g_nvmf_custom_admin_cmd_hdlrs[opc].hdlr = hdlr;
3862 }
3863 
3864 static int
3865 nvmf_passthru_admin_cmd(struct spdk_nvmf_request *req)
3866 {
3867 	struct spdk_bdev *bdev;
3868 	struct spdk_bdev_desc *desc;
3869 	struct spdk_io_channel *ch;
3870 	struct spdk_nvme_cmd *cmd = spdk_nvmf_request_get_cmd(req);
3871 	struct spdk_nvme_cpl *response = spdk_nvmf_request_get_response(req);
3872 	uint32_t bdev_nsid;
3873 	int rc;
3874 
3875 	if (g_nvmf_custom_admin_cmd_hdlrs[cmd->opc].nsid == 0) {
3876 		bdev_nsid = cmd->nsid;
3877 	} else {
3878 		bdev_nsid = g_nvmf_custom_admin_cmd_hdlrs[cmd->opc].nsid;
3879 	}
3880 
3881 	rc = spdk_nvmf_request_get_bdev(bdev_nsid, req, &bdev, &desc, &ch);
3882 	if (rc) {
3883 		response->status.sct = SPDK_NVME_SCT_GENERIC;
3884 		response->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT;
3885 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
3886 	}
3887 	return spdk_nvmf_bdev_ctrlr_nvme_passthru_admin(bdev, desc, ch, req, NULL);
3888 }
3889 
3890 void
3891 spdk_nvmf_set_passthru_admin_cmd(uint8_t opc, uint32_t forward_nsid)
3892 {
3893 	g_nvmf_custom_admin_cmd_hdlrs[opc].hdlr = nvmf_passthru_admin_cmd;
3894 	g_nvmf_custom_admin_cmd_hdlrs[opc].nsid = forward_nsid;
3895 }
3896 
3897 int
3898 spdk_nvmf_request_get_bdev(uint32_t nsid, struct spdk_nvmf_request *req,
3899 			   struct spdk_bdev **bdev, struct spdk_bdev_desc **desc, struct spdk_io_channel **ch)
3900 {
3901 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
3902 	struct spdk_nvmf_ns *ns;
3903 	struct spdk_nvmf_poll_group *group = req->qpair->group;
3904 	struct spdk_nvmf_subsystem_pg_ns_info *ns_info;
3905 
3906 	*bdev = NULL;
3907 	*desc = NULL;
3908 	*ch = NULL;
3909 
3910 	ns = _nvmf_subsystem_get_ns(ctrlr->subsys, nsid);
3911 	if (ns == NULL || ns->bdev == NULL) {
3912 		return -EINVAL;
3913 	}
3914 
3915 	assert(group != NULL && group->sgroups != NULL);
3916 	ns_info = &group->sgroups[ctrlr->subsys->id].ns_info[nsid - 1];
3917 	*bdev = ns->bdev;
3918 	*desc = ns->desc;
3919 	*ch = ns_info->channel;
3920 
3921 	return 0;
3922 }
3923 
3924 struct spdk_nvmf_ctrlr *spdk_nvmf_request_get_ctrlr(struct spdk_nvmf_request *req)
3925 {
3926 	return req->qpair->ctrlr;
3927 }
3928 
3929 struct spdk_nvme_cmd *spdk_nvmf_request_get_cmd(struct spdk_nvmf_request *req)
3930 {
3931 	return &req->cmd->nvme_cmd;
3932 }
3933 
3934 struct spdk_nvme_cpl *spdk_nvmf_request_get_response(struct spdk_nvmf_request *req)
3935 {
3936 	return &req->rsp->nvme_cpl;
3937 }
3938 
3939 struct spdk_nvmf_subsystem *spdk_nvmf_request_get_subsystem(struct spdk_nvmf_request *req)
3940 {
3941 	return req->qpair->ctrlr->subsys;
3942 }
3943 
3944 void spdk_nvmf_request_get_data(struct spdk_nvmf_request *req, void **data, uint32_t *length)
3945 {
3946 	*data = req->data;
3947 	*length = req->length;
3948 }
3949 
3950 struct spdk_nvmf_subsystem *spdk_nvmf_ctrlr_get_subsystem(struct spdk_nvmf_ctrlr *ctrlr)
3951 {
3952 	return ctrlr->subsys;
3953 }
3954 
3955 uint16_t spdk_nvmf_ctrlr_get_id(struct spdk_nvmf_ctrlr *ctrlr)
3956 {
3957 	return ctrlr->cntlid;
3958 }
3959 
3960 struct spdk_nvmf_request *spdk_nvmf_request_get_req_to_abort(struct spdk_nvmf_request *req)
3961 {
3962 	return req->req_to_abort;
3963 }
3964