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