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