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