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