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