xref: /spdk/lib/nvmf/ctrlr.c (revision 30e3f4d9006346a1ea182c80fad4a3a847fd3b0b)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation. All rights reserved.
5  *   Copyright (c) 2019, 2020 Mellanox Technologies LTD. All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "spdk/stdinc.h"
35 
36 #include "nvmf_internal.h"
37 #include "transport.h"
38 
39 #include "spdk/bit_array.h"
40 #include "spdk/endian.h"
41 #include "spdk/thread.h"
42 #include "spdk/trace.h"
43 #include "spdk/nvme_spec.h"
44 #include "spdk/nvmf_cmd.h"
45 #include "spdk/string.h"
46 #include "spdk/util.h"
47 #include "spdk/version.h"
48 
49 #include "spdk_internal/log.h"
50 
51 #define MIN_KEEP_ALIVE_TIMEOUT_IN_MS 10000
52 #define NVMF_DISC_KATO_IN_MS 120000
53 #define KAS_TIME_UNIT_IN_MS 100
54 #define KAS_DEFAULT_VALUE (MIN_KEEP_ALIVE_TIMEOUT_IN_MS / KAS_TIME_UNIT_IN_MS)
55 
56 /*
57  * Report the SPDK version as the firmware revision.
58  * SPDK_VERSION_STRING won't fit into FR (only 8 bytes), so try to fit the most important parts.
59  */
60 #define FW_VERSION SPDK_VERSION_MAJOR_STRING SPDK_VERSION_MINOR_STRING SPDK_VERSION_PATCH_STRING
61 
62 /*
63  * Support for custom admin command handlers
64  */
65 struct spdk_nvmf_custom_admin_cmd {
66 	spdk_nvmf_custom_cmd_hdlr hdlr;
67 	uint32_t nsid; /* nsid to forward */
68 };
69 
70 static struct spdk_nvmf_custom_admin_cmd g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_MAX_OPC + 1];
71 
72 static inline void
73 nvmf_invalid_connect_response(struct spdk_nvmf_fabric_connect_rsp *rsp,
74 			      uint8_t iattr, uint16_t ipo)
75 {
76 	rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
77 	rsp->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM;
78 	rsp->status_code_specific.invalid.iattr = iattr;
79 	rsp->status_code_specific.invalid.ipo = ipo;
80 }
81 
82 #define SPDK_NVMF_INVALID_CONNECT_CMD(rsp, field)	\
83 	nvmf_invalid_connect_response(rsp, 0, offsetof(struct spdk_nvmf_fabric_connect_cmd, field))
84 #define SPDK_NVMF_INVALID_CONNECT_DATA(rsp, field)	\
85 	nvmf_invalid_connect_response(rsp, 1, offsetof(struct spdk_nvmf_fabric_connect_data, field))
86 
87 static void
88 nvmf_ctrlr_stop_keep_alive_timer(struct spdk_nvmf_ctrlr *ctrlr)
89 {
90 	if (!ctrlr) {
91 		SPDK_ERRLOG("Controller is NULL\n");
92 		return;
93 	}
94 
95 	if (ctrlr->keep_alive_poller == NULL) {
96 		return;
97 	}
98 
99 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Stop keep alive poller\n");
100 	spdk_poller_unregister(&ctrlr->keep_alive_poller);
101 }
102 
103 static void
104 nvmf_ctrlr_disconnect_qpairs_done(struct spdk_io_channel_iter *i, int status)
105 {
106 	if (status == 0) {
107 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ctrlr disconnect qpairs complete successfully\n");
108 	} else {
109 		SPDK_ERRLOG("Fail to disconnect ctrlr qpairs\n");
110 	}
111 }
112 
113 static int
114 _nvmf_ctrlr_disconnect_qpairs_on_pg(struct spdk_io_channel_iter *i, bool include_admin)
115 {
116 	int rc = 0;
117 	struct spdk_nvmf_ctrlr *ctrlr;
118 	struct spdk_nvmf_qpair *qpair, *temp_qpair;
119 	struct spdk_io_channel *ch;
120 	struct spdk_nvmf_poll_group *group;
121 
122 	ctrlr = spdk_io_channel_iter_get_ctx(i);
123 	ch = spdk_io_channel_iter_get_channel(i);
124 	group = spdk_io_channel_get_ctx(ch);
125 
126 	TAILQ_FOREACH_SAFE(qpair, &group->qpairs, link, temp_qpair) {
127 		if (qpair->ctrlr == ctrlr && (include_admin || !nvmf_qpair_is_admin_queue(qpair))) {
128 			rc = spdk_nvmf_qpair_disconnect(qpair, NULL, NULL);
129 			if (rc) {
130 				SPDK_ERRLOG("Qpair disconnect failed\n");
131 				return rc;
132 			}
133 		}
134 	}
135 
136 	return rc;
137 }
138 
139 static void
140 nvmf_ctrlr_disconnect_qpairs_on_pg(struct spdk_io_channel_iter *i)
141 {
142 	spdk_for_each_channel_continue(i, _nvmf_ctrlr_disconnect_qpairs_on_pg(i, true));
143 }
144 
145 static void
146 nvmf_ctrlr_disconnect_io_qpairs_on_pg(struct spdk_io_channel_iter *i)
147 {
148 	spdk_for_each_channel_continue(i, _nvmf_ctrlr_disconnect_qpairs_on_pg(i, false));
149 }
150 
151 static int
152 nvmf_ctrlr_keep_alive_poll(void *ctx)
153 {
154 	uint64_t keep_alive_timeout_tick;
155 	uint64_t now = spdk_get_ticks();
156 	struct spdk_nvmf_ctrlr *ctrlr = ctx;
157 
158 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Polling ctrlr keep alive timeout\n");
159 
160 	/* If the Keep alive feature is in use and the timer expires */
161 	keep_alive_timeout_tick = ctrlr->last_keep_alive_tick +
162 				  ctrlr->feat.keep_alive_timer.bits.kato * spdk_get_ticks_hz() / UINT64_C(1000);
163 	if (now > keep_alive_timeout_tick) {
164 		SPDK_NOTICELOG("Disconnecting host from subsystem %s due to keep alive timeout.\n",
165 			       ctrlr->subsys->subnqn);
166 		/* set the Controller Fatal Status bit to '1' */
167 		if (ctrlr->vcprop.csts.bits.cfs == 0) {
168 			ctrlr->vcprop.csts.bits.cfs = 1;
169 
170 			/*
171 			 * disconnect qpairs, terminate Transport connection
172 			 * destroy ctrlr, break the host to controller association
173 			 * disconnect qpairs with qpair->ctrlr == ctrlr
174 			 */
175 			spdk_for_each_channel(ctrlr->subsys->tgt,
176 					      nvmf_ctrlr_disconnect_qpairs_on_pg,
177 					      ctrlr,
178 					      nvmf_ctrlr_disconnect_qpairs_done);
179 		}
180 	}
181 
182 	return 1;
183 }
184 
185 static void
186 nvmf_ctrlr_start_keep_alive_timer(struct spdk_nvmf_ctrlr *ctrlr)
187 {
188 	if (!ctrlr) {
189 		SPDK_ERRLOG("Controller is NULL\n");
190 		return;
191 	}
192 
193 	/* if cleared to 0 then the Keep Alive Timer is disabled */
194 	if (ctrlr->feat.keep_alive_timer.bits.kato != 0) {
195 
196 		ctrlr->last_keep_alive_tick = spdk_get_ticks();
197 
198 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Ctrlr add keep alive poller\n");
199 		ctrlr->keep_alive_poller = SPDK_POLLER_REGISTER(nvmf_ctrlr_keep_alive_poll, ctrlr,
200 					   ctrlr->feat.keep_alive_timer.bits.kato * 1000);
201 	}
202 }
203 
204 static void
205 ctrlr_add_qpair_and_update_rsp(struct spdk_nvmf_qpair *qpair,
206 			       struct spdk_nvmf_ctrlr *ctrlr,
207 			       struct spdk_nvmf_fabric_connect_rsp *rsp)
208 {
209 	assert(ctrlr->admin_qpair->group->thread == spdk_get_thread());
210 
211 	/* check if we would exceed ctrlr connection limit */
212 	if (qpair->qid >= spdk_bit_array_capacity(ctrlr->qpair_mask)) {
213 		SPDK_ERRLOG("Requested QID %u but Max QID is %u\n",
214 			    qpair->qid, spdk_bit_array_capacity(ctrlr->qpair_mask) - 1);
215 		rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
216 		rsp->status.sc = SPDK_NVME_SC_INVALID_QUEUE_IDENTIFIER;
217 		return;
218 	}
219 
220 	if (spdk_bit_array_get(ctrlr->qpair_mask, qpair->qid)) {
221 		SPDK_ERRLOG("Got I/O connect with duplicate QID %u\n", qpair->qid);
222 		rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
223 		rsp->status.sc = SPDK_NVME_SC_INVALID_QUEUE_IDENTIFIER;
224 		return;
225 	}
226 
227 	qpair->ctrlr = ctrlr;
228 	spdk_bit_array_set(ctrlr->qpair_mask, qpair->qid);
229 
230 	rsp->status.sc = SPDK_NVME_SC_SUCCESS;
231 	rsp->status_code_specific.success.cntlid = ctrlr->cntlid;
232 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "connect capsule response: cntlid = 0x%04x\n",
233 		      rsp->status_code_specific.success.cntlid);
234 }
235 
236 static void
237 _nvmf_request_complete(void *ctx)
238 {
239 	struct spdk_nvmf_request *req = ctx;
240 
241 	spdk_nvmf_request_complete(req);
242 }
243 
244 static void
245 _nvmf_ctrlr_add_admin_qpair(void *ctx)
246 {
247 	struct spdk_nvmf_request *req = ctx;
248 	struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp;
249 	struct spdk_nvmf_qpair *qpair = req->qpair;
250 	struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr;
251 
252 	ctrlr->admin_qpair = qpair;
253 	nvmf_ctrlr_start_keep_alive_timer(ctrlr);
254 	ctrlr_add_qpair_and_update_rsp(qpair, ctrlr, rsp);
255 	spdk_nvmf_request_complete(req);
256 }
257 
258 static void
259 _nvmf_subsystem_add_ctrlr(void *ctx)
260 {
261 	struct spdk_nvmf_request *req = ctx;
262 	struct spdk_nvmf_qpair *qpair = req->qpair;
263 	struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp;
264 	struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr;
265 
266 	if (nvmf_subsystem_add_ctrlr(ctrlr->subsys, ctrlr)) {
267 		SPDK_ERRLOG("Unable to add controller to subsystem\n");
268 		spdk_bit_array_free(&ctrlr->qpair_mask);
269 		free(ctrlr);
270 		qpair->ctrlr = NULL;
271 		rsp->status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
272 		spdk_thread_send_msg(qpair->group->thread, _nvmf_request_complete, req);
273 		return;
274 	}
275 
276 	spdk_thread_send_msg(ctrlr->thread, _nvmf_ctrlr_add_admin_qpair, req);
277 }
278 
279 static struct spdk_nvmf_ctrlr *
280 nvmf_ctrlr_create(struct spdk_nvmf_subsystem *subsystem,
281 		  struct spdk_nvmf_request *req,
282 		  struct spdk_nvmf_fabric_connect_cmd *connect_cmd,
283 		  struct spdk_nvmf_fabric_connect_data *connect_data)
284 {
285 	struct spdk_nvmf_ctrlr	*ctrlr;
286 	struct spdk_nvmf_transport *transport;
287 
288 	ctrlr = calloc(1, sizeof(*ctrlr));
289 	if (ctrlr == NULL) {
290 		SPDK_ERRLOG("Memory allocation failed\n");
291 		return NULL;
292 	}
293 
294 	TAILQ_INIT(&ctrlr->log_head);
295 	ctrlr->subsys = subsystem;
296 	ctrlr->thread = req->qpair->group->thread;
297 
298 	transport = req->qpair->transport;
299 	ctrlr->qpair_mask = spdk_bit_array_create(transport->opts.max_qpairs_per_ctrlr);
300 	if (!ctrlr->qpair_mask) {
301 		SPDK_ERRLOG("Failed to allocate controller qpair mask\n");
302 		free(ctrlr);
303 		return NULL;
304 	}
305 
306 	/*
307 	 * KAS: This field indicates the granularity of the Keep Alive Timer in 100ms units.
308 	 * If this field is cleared to 0h, then Keep Alive is not supported.
309 	 */
310 	if (transport->cdata.kas) {
311 		ctrlr->feat.keep_alive_timer.bits.kato = spdk_divide_round_up(connect_cmd->kato,
312 				KAS_DEFAULT_VALUE * KAS_TIME_UNIT_IN_MS) *
313 				KAS_DEFAULT_VALUE * KAS_TIME_UNIT_IN_MS;
314 	}
315 
316 	ctrlr->feat.async_event_configuration.bits.ns_attr_notice = 1;
317 	ctrlr->feat.volatile_write_cache.bits.wce = 1;
318 
319 	if (ctrlr->subsys->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) {
320 		/*
321 		 * If keep-alive timeout is not set, discovery controllers use some
322 		 * arbitrary high value in order to cleanup stale discovery sessions
323 		 *
324 		 * From the 1.0a nvme-of spec:
325 		 * "The Keep Alive command is reserved for
326 		 * Discovery controllers. A transport may specify a
327 		 * fixed Discovery controller activity timeout value
328 		 * (e.g., 2 minutes).  If no commands are received
329 		 * by a Discovery controller within that time
330 		 * period, the controller may perform the
331 		 * actions for Keep Alive Timer expiration".
332 		 * kato is in millisecond.
333 		 */
334 		if (ctrlr->feat.keep_alive_timer.bits.kato == 0) {
335 			ctrlr->feat.keep_alive_timer.bits.kato = NVMF_DISC_KATO_IN_MS;
336 		}
337 	}
338 
339 	/* Subtract 1 for admin queue, 1 for 0's based */
340 	ctrlr->feat.number_of_queues.bits.ncqr = transport->opts.max_qpairs_per_ctrlr - 1 -
341 			1;
342 	ctrlr->feat.number_of_queues.bits.nsqr = transport->opts.max_qpairs_per_ctrlr - 1 -
343 			1;
344 
345 	spdk_uuid_copy(&ctrlr->hostid, (struct spdk_uuid *)connect_data->hostid);
346 	memcpy(ctrlr->hostnqn, connect_data->hostnqn, sizeof(ctrlr->hostnqn));
347 
348 	ctrlr->vcprop.cap.raw = 0;
349 	ctrlr->vcprop.cap.bits.cqr = 1; /* NVMe-oF specification required */
350 	ctrlr->vcprop.cap.bits.mqes = transport->opts.max_queue_depth -
351 				      1; /* max queue depth */
352 	ctrlr->vcprop.cap.bits.ams = 0; /* optional arb mechanisms */
353 	ctrlr->vcprop.cap.bits.to = 1; /* ready timeout - 500 msec units */
354 	ctrlr->vcprop.cap.bits.dstrd = 0; /* fixed to 0 for NVMe-oF */
355 	ctrlr->vcprop.cap.bits.css = SPDK_NVME_CAP_CSS_NVM; /* NVM command set */
356 	ctrlr->vcprop.cap.bits.mpsmin = 0; /* 2 ^ (12 + mpsmin) == 4k */
357 	ctrlr->vcprop.cap.bits.mpsmax = 0; /* 2 ^ (12 + mpsmax) == 4k */
358 
359 	/* Version Supported: 1.3 */
360 	ctrlr->vcprop.vs.bits.mjr = 1;
361 	ctrlr->vcprop.vs.bits.mnr = 3;
362 	ctrlr->vcprop.vs.bits.ter = 0;
363 
364 	ctrlr->vcprop.cc.raw = 0;
365 	ctrlr->vcprop.cc.bits.en = 0; /* Init controller disabled */
366 
367 	ctrlr->vcprop.csts.raw = 0;
368 	ctrlr->vcprop.csts.bits.rdy = 0; /* Init controller as not ready */
369 
370 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "cap 0x%" PRIx64 "\n", ctrlr->vcprop.cap.raw);
371 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "vs 0x%x\n", ctrlr->vcprop.vs.raw);
372 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "cc 0x%x\n", ctrlr->vcprop.cc.raw);
373 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "csts 0x%x\n", ctrlr->vcprop.csts.raw);
374 
375 	ctrlr->dif_insert_or_strip = transport->opts.dif_insert_or_strip;
376 
377 	req->qpair->ctrlr = ctrlr;
378 	spdk_thread_send_msg(subsystem->thread, _nvmf_subsystem_add_ctrlr, req);
379 
380 	return ctrlr;
381 }
382 
383 static void
384 _nvmf_ctrlr_destruct(void *ctx)
385 {
386 	struct spdk_nvmf_ctrlr *ctrlr = ctx;
387 	struct spdk_nvmf_reservation_log *log, *log_tmp;
388 
389 	nvmf_ctrlr_stop_keep_alive_timer(ctrlr);
390 
391 	TAILQ_FOREACH_SAFE(log, &ctrlr->log_head, link, log_tmp) {
392 		TAILQ_REMOVE(&ctrlr->log_head, log, link);
393 		free(log);
394 	}
395 	free(ctrlr);
396 }
397 
398 void
399 nvmf_ctrlr_destruct(struct spdk_nvmf_ctrlr *ctrlr)
400 {
401 	nvmf_subsystem_remove_ctrlr(ctrlr->subsys, ctrlr);
402 
403 	spdk_thread_send_msg(ctrlr->thread, _nvmf_ctrlr_destruct, ctrlr);
404 }
405 
406 static void
407 nvmf_ctrlr_add_io_qpair(void *ctx)
408 {
409 	struct spdk_nvmf_request *req = ctx;
410 	struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp;
411 	struct spdk_nvmf_qpair *qpair = req->qpair;
412 	struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr;
413 
414 	/* Unit test will check qpair->ctrlr after calling spdk_nvmf_ctrlr_connect.
415 	  * For error case, the value should be NULL. So set it to NULL at first.
416 	  */
417 	qpair->ctrlr = NULL;
418 
419 	if (ctrlr->subsys->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) {
420 		SPDK_ERRLOG("I/O connect not allowed on discovery controller\n");
421 		SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid);
422 		goto end;
423 	}
424 
425 	if (!ctrlr->vcprop.cc.bits.en) {
426 		SPDK_ERRLOG("Got I/O connect before ctrlr was enabled\n");
427 		SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid);
428 		goto end;
429 	}
430 
431 	if (1u << ctrlr->vcprop.cc.bits.iosqes != sizeof(struct spdk_nvme_cmd)) {
432 		SPDK_ERRLOG("Got I/O connect with invalid IOSQES %u\n",
433 			    ctrlr->vcprop.cc.bits.iosqes);
434 		SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid);
435 		goto end;
436 	}
437 
438 	if (1u << ctrlr->vcprop.cc.bits.iocqes != sizeof(struct spdk_nvme_cpl)) {
439 		SPDK_ERRLOG("Got I/O connect with invalid IOCQES %u\n",
440 			    ctrlr->vcprop.cc.bits.iocqes);
441 		SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid);
442 		goto end;
443 	}
444 
445 	ctrlr_add_qpair_and_update_rsp(qpair, ctrlr, rsp);
446 end:
447 	spdk_thread_send_msg(qpair->group->thread, _nvmf_request_complete, req);
448 }
449 
450 static void
451 _nvmf_ctrlr_add_io_qpair(void *ctx)
452 {
453 	struct spdk_nvmf_request *req = ctx;
454 	struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp;
455 	struct spdk_nvmf_fabric_connect_data *data = req->data;
456 	struct spdk_nvmf_ctrlr *ctrlr;
457 	struct spdk_nvmf_qpair *qpair = req->qpair;
458 	struct spdk_nvmf_qpair *admin_qpair;
459 	struct spdk_nvmf_tgt *tgt = qpair->transport->tgt;
460 	struct spdk_nvmf_subsystem *subsystem;
461 
462 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Connect I/O Queue for controller id 0x%x\n", data->cntlid);
463 
464 	subsystem = spdk_nvmf_tgt_find_subsystem(tgt, data->subnqn);
465 	/* We already checked this in spdk_nvmf_ctrlr_connect */
466 	assert(subsystem != NULL);
467 
468 	ctrlr = nvmf_subsystem_get_ctrlr(subsystem, data->cntlid);
469 	if (ctrlr == NULL) {
470 		SPDK_ERRLOG("Unknown controller ID 0x%x\n", data->cntlid);
471 		SPDK_NVMF_INVALID_CONNECT_DATA(rsp, cntlid);
472 		spdk_thread_send_msg(qpair->group->thread, _nvmf_request_complete, req);
473 		return;
474 	}
475 
476 	admin_qpair = ctrlr->admin_qpair;
477 	qpair->ctrlr = ctrlr;
478 	spdk_thread_send_msg(admin_qpair->group->thread, nvmf_ctrlr_add_io_qpair, req);
479 }
480 
481 static bool
482 nvmf_qpair_access_allowed(struct spdk_nvmf_qpair *qpair, struct spdk_nvmf_subsystem *subsystem,
483 			  const char *hostnqn)
484 {
485 	struct spdk_nvme_transport_id listen_trid = {};
486 
487 	if (!spdk_nvmf_subsystem_host_allowed(subsystem, hostnqn)) {
488 		SPDK_ERRLOG("Subsystem '%s' does not allow host '%s'\n", subsystem->subnqn, hostnqn);
489 		return false;
490 	}
491 
492 	if (spdk_nvmf_qpair_get_listen_trid(qpair, &listen_trid)) {
493 		SPDK_ERRLOG("Subsystem '%s' is unable to enforce access control due to an internal error.\n",
494 			    subsystem->subnqn);
495 		return false;
496 	}
497 
498 	if (!spdk_nvmf_subsystem_listener_allowed(subsystem, &listen_trid)) {
499 		SPDK_ERRLOG("Subsystem '%s' does not allow host '%s' to connect at this address.\n",
500 			    subsystem->subnqn, hostnqn);
501 		return false;
502 	}
503 
504 	return true;
505 }
506 
507 static int
508 _nvmf_ctrlr_connect(struct spdk_nvmf_request *req)
509 {
510 	struct spdk_nvmf_fabric_connect_data *data = req->data;
511 	struct spdk_nvmf_fabric_connect_cmd *cmd = &req->cmd->connect_cmd;
512 	struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp;
513 	struct spdk_nvmf_qpair *qpair = req->qpair;
514 	struct spdk_nvmf_transport *transport = qpair->transport;
515 	struct spdk_nvmf_ctrlr *ctrlr;
516 	struct spdk_nvmf_subsystem *subsystem;
517 
518 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "recfmt 0x%x qid %u sqsize %u\n",
519 		      cmd->recfmt, cmd->qid, cmd->sqsize);
520 
521 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Connect data:\n");
522 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "  cntlid:  0x%04x\n", data->cntlid);
523 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "  hostid: %08x-%04x-%04x-%02x%02x-%04x%08x ***\n",
524 		      ntohl(*(uint32_t *)&data->hostid[0]),
525 		      ntohs(*(uint16_t *)&data->hostid[4]),
526 		      ntohs(*(uint16_t *)&data->hostid[6]),
527 		      data->hostid[8],
528 		      data->hostid[9],
529 		      ntohs(*(uint16_t *)&data->hostid[10]),
530 		      ntohl(*(uint32_t *)&data->hostid[12]));
531 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "  subnqn: \"%s\"\n", data->subnqn);
532 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "  hostnqn: \"%s\"\n", data->hostnqn);
533 
534 	subsystem = spdk_nvmf_tgt_find_subsystem(transport->tgt, data->subnqn);
535 	if (!subsystem) {
536 		SPDK_NVMF_INVALID_CONNECT_DATA(rsp, subnqn);
537 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
538 	}
539 
540 	if (cmd->recfmt != 0) {
541 		SPDK_ERRLOG("Connect command unsupported RECFMT %u\n", cmd->recfmt);
542 		rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
543 		rsp->status.sc = SPDK_NVMF_FABRIC_SC_INCOMPATIBLE_FORMAT;
544 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
545 	}
546 
547 	/*
548 	 * SQSIZE is a 0-based value, so it must be at least 1 (minimum queue depth is 2) and
549 	 * strictly less than max_aq_depth (admin queues) or max_queue_depth (io queues).
550 	 */
551 	if (cmd->sqsize == 0) {
552 		SPDK_ERRLOG("Invalid SQSIZE = 0\n");
553 		SPDK_NVMF_INVALID_CONNECT_CMD(rsp, sqsize);
554 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
555 	}
556 
557 	if (cmd->qid == 0) {
558 		if (cmd->sqsize >= transport->opts.max_aq_depth) {
559 			SPDK_ERRLOG("Invalid SQSIZE for admin queue %u (min 1, max %u)\n",
560 				    cmd->sqsize, transport->opts.max_aq_depth - 1);
561 			SPDK_NVMF_INVALID_CONNECT_CMD(rsp, sqsize);
562 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
563 		}
564 	} else if (cmd->sqsize >= transport->opts.max_queue_depth) {
565 		SPDK_ERRLOG("Invalid SQSIZE %u (min 1, max %u)\n",
566 			    cmd->sqsize, transport->opts.max_queue_depth - 1);
567 		SPDK_NVMF_INVALID_CONNECT_CMD(rsp, sqsize);
568 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
569 	}
570 
571 	qpair->sq_head_max = cmd->sqsize;
572 	qpair->qid = cmd->qid;
573 
574 	if (0 == qpair->qid) {
575 		qpair->group->stat.admin_qpairs++;
576 	} else {
577 		qpair->group->stat.io_qpairs++;
578 	}
579 
580 	if (cmd->qid == 0) {
581 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Connect Admin Queue for controller ID 0x%x\n", data->cntlid);
582 
583 		if (data->cntlid != 0xFFFF) {
584 			/* This NVMf target only supports dynamic mode. */
585 			SPDK_ERRLOG("The NVMf target only supports dynamic mode (CNTLID = 0x%x).\n", data->cntlid);
586 			SPDK_NVMF_INVALID_CONNECT_DATA(rsp, cntlid);
587 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
588 		}
589 
590 		/* Establish a new ctrlr */
591 		ctrlr = nvmf_ctrlr_create(subsystem, req, cmd, data);
592 		if (!ctrlr) {
593 			SPDK_ERRLOG("nvmf_ctrlr_create() failed\n");
594 			rsp->status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
595 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
596 		} else {
597 			return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
598 		}
599 	} else {
600 		spdk_thread_send_msg(subsystem->thread, _nvmf_ctrlr_add_io_qpair, req);
601 		return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
602 	}
603 }
604 
605 static inline bool
606 nvmf_request_is_fabric_connect(struct spdk_nvmf_request *req)
607 {
608 	return req->cmd->nvmf_cmd.opcode == SPDK_NVME_OPC_FABRIC &&
609 	       req->cmd->nvmf_cmd.fctype == SPDK_NVMF_FABRIC_COMMAND_CONNECT;
610 }
611 
612 static struct spdk_nvmf_subsystem_poll_group *
613 nvmf_subsystem_pg_from_connect_cmd(struct spdk_nvmf_request *req)
614 {
615 	struct spdk_nvmf_fabric_connect_data *data;
616 	struct spdk_nvmf_subsystem *subsystem;
617 	struct spdk_nvmf_tgt *tgt;
618 
619 	assert(nvmf_request_is_fabric_connect(req));
620 	assert(req->qpair->ctrlr == NULL);
621 
622 	data = req->data;
623 	tgt = req->qpair->transport->tgt;
624 
625 	subsystem = spdk_nvmf_tgt_find_subsystem(tgt, data->subnqn);
626 	if (subsystem == NULL) {
627 		return NULL;
628 	}
629 
630 	return &req->qpair->group->sgroups[subsystem->id];
631 }
632 
633 int
634 spdk_nvmf_ctrlr_connect(struct spdk_nvmf_request *req)
635 {
636 	struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp;
637 	struct spdk_nvmf_qpair *qpair = req->qpair;
638 	struct spdk_nvmf_subsystem_poll_group *sgroup;
639 	enum spdk_nvmf_request_exec_status status;
640 
641 	sgroup = nvmf_subsystem_pg_from_connect_cmd(req);
642 	if (!sgroup) {
643 		SPDK_NVMF_INVALID_CONNECT_DATA(rsp, subnqn);
644 		status = SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
645 		goto out;
646 	}
647 
648 	sgroup->io_outstanding++;
649 	TAILQ_INSERT_TAIL(&qpair->outstanding, req, link);
650 
651 	status = _nvmf_ctrlr_connect(req);
652 
653 out:
654 	if (status == SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE) {
655 		spdk_nvmf_request_complete(req);
656 	}
657 
658 	return status;
659 }
660 
661 static int
662 nvmf_ctrlr_cmd_connect(struct spdk_nvmf_request *req)
663 {
664 	struct spdk_nvmf_fabric_connect_data *data = req->data;
665 	struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp;
666 	struct spdk_nvmf_transport *transport = req->qpair->transport;
667 	struct spdk_nvmf_subsystem *subsystem;
668 
669 	if (req->length < sizeof(struct spdk_nvmf_fabric_connect_data)) {
670 		SPDK_ERRLOG("Connect command data length 0x%x too small\n", req->length);
671 		rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
672 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
673 	}
674 
675 	subsystem = spdk_nvmf_tgt_find_subsystem(transport->tgt, data->subnqn);
676 	if (!subsystem) {
677 		SPDK_NVMF_INVALID_CONNECT_DATA(rsp, subnqn);
678 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
679 	}
680 
681 	if ((subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE) ||
682 	    (subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSING) ||
683 	    (subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED) ||
684 	    (subsystem->state == SPDK_NVMF_SUBSYSTEM_DEACTIVATING)) {
685 		SPDK_ERRLOG("Subsystem '%s' is not ready\n", subsystem->subnqn);
686 		rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
687 		rsp->status.sc = SPDK_NVMF_FABRIC_SC_CONTROLLER_BUSY;
688 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
689 	}
690 
691 	/* Ensure that hostnqn is null terminated */
692 	if (!memchr(data->hostnqn, '\0', SPDK_NVMF_NQN_MAX_LEN + 1)) {
693 		SPDK_ERRLOG("Connect HOSTNQN is not null terminated\n");
694 		SPDK_NVMF_INVALID_CONNECT_DATA(rsp, hostnqn);
695 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
696 	}
697 
698 	if (!nvmf_qpair_access_allowed(req->qpair, subsystem, data->hostnqn)) {
699 		rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
700 		rsp->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_HOST;
701 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
702 	}
703 
704 	return _nvmf_ctrlr_connect(req);
705 }
706 
707 static void
708 nvmf_ctrlr_cc_reset_done(struct spdk_io_channel_iter *i, int status)
709 {
710 	struct spdk_nvmf_ctrlr *ctrlr = spdk_io_channel_iter_get_ctx(i);
711 
712 	if (status < 0) {
713 		SPDK_ERRLOG("Fail to disconnect io ctrlr qpairs\n");
714 		assert(false);
715 	}
716 
717 	/* Only a subset of the registers are cleared out on a reset */
718 	ctrlr->vcprop.cc.raw = 0;
719 	ctrlr->vcprop.csts.raw = 0;
720 
721 }
722 
723 const struct spdk_nvmf_registers *
724 spdk_nvmf_ctrlr_get_regs(struct spdk_nvmf_ctrlr *ctrlr)
725 {
726 	return &ctrlr->vcprop;
727 }
728 
729 static uint64_t
730 nvmf_prop_get_cap(struct spdk_nvmf_ctrlr *ctrlr)
731 {
732 	return ctrlr->vcprop.cap.raw;
733 }
734 
735 static uint64_t
736 nvmf_prop_get_vs(struct spdk_nvmf_ctrlr *ctrlr)
737 {
738 	return ctrlr->vcprop.vs.raw;
739 }
740 
741 static uint64_t
742 nvmf_prop_get_cc(struct spdk_nvmf_ctrlr *ctrlr)
743 {
744 	return ctrlr->vcprop.cc.raw;
745 }
746 
747 static bool
748 nvmf_prop_set_cc(struct spdk_nvmf_ctrlr *ctrlr, uint32_t value)
749 {
750 	union spdk_nvme_cc_register cc, diff;
751 
752 	cc.raw = value;
753 
754 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "cur CC: 0x%08x\n", ctrlr->vcprop.cc.raw);
755 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "new CC: 0x%08x\n", cc.raw);
756 
757 	/*
758 	 * Calculate which bits changed between the current and new CC.
759 	 * Mark each bit as 0 once it is handled to determine if any unhandled bits were changed.
760 	 */
761 	diff.raw = cc.raw ^ ctrlr->vcprop.cc.raw;
762 
763 	if (diff.bits.en) {
764 		if (cc.bits.en) {
765 			SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Property Set CC Enable!\n");
766 			ctrlr->vcprop.cc.bits.en = 1;
767 			ctrlr->vcprop.csts.bits.rdy = 1;
768 		} else {
769 			SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Property Set CC Disable!\n");
770 			ctrlr->vcprop.cc.bits.en = 0;
771 			spdk_for_each_channel(ctrlr->subsys->tgt,
772 					      nvmf_ctrlr_disconnect_io_qpairs_on_pg,
773 					      ctrlr,
774 					      nvmf_ctrlr_cc_reset_done);
775 		}
776 		diff.bits.en = 0;
777 	}
778 
779 	if (diff.bits.shn) {
780 		if (cc.bits.shn == SPDK_NVME_SHN_NORMAL ||
781 		    cc.bits.shn == SPDK_NVME_SHN_ABRUPT) {
782 			SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Property Set CC Shutdown %u%ub!\n",
783 				      cc.bits.shn >> 1, cc.bits.shn & 1);
784 			ctrlr->vcprop.cc.bits.shn = cc.bits.shn;
785 			ctrlr->vcprop.cc.bits.en = 0;
786 			ctrlr->vcprop.csts.bits.rdy = 0;
787 			ctrlr->vcprop.csts.bits.shst = SPDK_NVME_SHST_COMPLETE;
788 		} else if (cc.bits.shn == 0) {
789 			ctrlr->vcprop.cc.bits.shn = 0;
790 		} else {
791 			SPDK_ERRLOG("Prop Set CC: Invalid SHN value %u%ub\n",
792 				    cc.bits.shn >> 1, cc.bits.shn & 1);
793 			return false;
794 		}
795 		diff.bits.shn = 0;
796 	}
797 
798 	if (diff.bits.iosqes) {
799 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Prop Set IOSQES = %u (%u bytes)\n",
800 			      cc.bits.iosqes, 1u << cc.bits.iosqes);
801 		ctrlr->vcprop.cc.bits.iosqes = cc.bits.iosqes;
802 		diff.bits.iosqes = 0;
803 	}
804 
805 	if (diff.bits.iocqes) {
806 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Prop Set IOCQES = %u (%u bytes)\n",
807 			      cc.bits.iocqes, 1u << cc.bits.iocqes);
808 		ctrlr->vcprop.cc.bits.iocqes = cc.bits.iocqes;
809 		diff.bits.iocqes = 0;
810 	}
811 
812 	if (diff.bits.ams) {
813 		SPDK_ERRLOG("Arbitration Mechanism Selected (AMS) 0x%x not supported!\n", cc.bits.ams);
814 		return false;
815 	}
816 
817 	if (diff.bits.mps) {
818 		SPDK_ERRLOG("Memory Page Size (MPS) %u KiB not supported!\n", (1 << (2 + cc.bits.mps)));
819 		return false;
820 	}
821 
822 	if (diff.bits.css) {
823 		SPDK_ERRLOG("I/O Command Set Selected (CSS) 0x%x not supported!\n", cc.bits.css);
824 		return false;
825 	}
826 
827 	if (diff.raw != 0) {
828 		SPDK_ERRLOG("Prop Set CC toggled reserved bits 0x%x!\n", diff.raw);
829 		return false;
830 	}
831 
832 	return true;
833 }
834 
835 static uint64_t
836 nvmf_prop_get_csts(struct spdk_nvmf_ctrlr *ctrlr)
837 {
838 	return ctrlr->vcprop.csts.raw;
839 }
840 
841 static uint64_t
842 nvmf_prop_get_aqa(struct spdk_nvmf_ctrlr *ctrlr)
843 {
844 	return ctrlr->vcprop.aqa.raw;
845 }
846 
847 static bool
848 nvmf_prop_set_aqa(struct spdk_nvmf_ctrlr *ctrlr, uint32_t value)
849 {
850 	union spdk_nvme_aqa_register aqa;
851 
852 	aqa.raw = value;
853 
854 	if (aqa.bits.asqs > ctrlr->vcprop.cap.bits.mqes ||
855 	    aqa.bits.acqs > ctrlr->vcprop.cap.bits.mqes) {
856 		return false;
857 	}
858 
859 	ctrlr->vcprop.aqa.raw = value;
860 
861 	return true;
862 }
863 
864 static uint64_t
865 nvmf_prop_get_asq(struct spdk_nvmf_ctrlr *ctrlr)
866 {
867 	return ctrlr->vcprop.asq;
868 }
869 
870 static bool
871 nvmf_prop_set_asq_lower(struct spdk_nvmf_ctrlr *ctrlr, uint32_t value)
872 {
873 	ctrlr->vcprop.asq = (ctrlr->vcprop.asq & (0xFFFFFFFFULL << 32ULL)) | value;
874 
875 	return true;
876 }
877 
878 static bool
879 nvmf_prop_set_asq_upper(struct spdk_nvmf_ctrlr *ctrlr, uint32_t value)
880 {
881 	ctrlr->vcprop.asq = (ctrlr->vcprop.asq & 0xFFFFFFFFULL) | ((uint64_t)value << 32ULL);
882 
883 	return true;
884 }
885 
886 static uint64_t
887 nvmf_prop_get_acq(struct spdk_nvmf_ctrlr *ctrlr)
888 {
889 	return ctrlr->vcprop.acq;
890 }
891 
892 static bool
893 nvmf_prop_set_acq_lower(struct spdk_nvmf_ctrlr *ctrlr, uint32_t value)
894 {
895 	ctrlr->vcprop.acq = (ctrlr->vcprop.acq & (0xFFFFFFFFULL << 32ULL)) | value;
896 
897 	return true;
898 }
899 
900 static bool
901 nvmf_prop_set_acq_upper(struct spdk_nvmf_ctrlr *ctrlr, uint32_t value)
902 {
903 	ctrlr->vcprop.acq = (ctrlr->vcprop.acq & 0xFFFFFFFFULL) | ((uint64_t)value << 32ULL);
904 
905 	return true;
906 }
907 
908 struct nvmf_prop {
909 	uint32_t ofst;
910 	uint8_t size;
911 	char name[11];
912 	uint64_t (*get_cb)(struct spdk_nvmf_ctrlr *ctrlr);
913 	bool (*set_cb)(struct spdk_nvmf_ctrlr *ctrlr, uint32_t value);
914 	bool (*set_upper_cb)(struct spdk_nvmf_ctrlr *ctrlr, uint32_t value);
915 };
916 
917 #define PROP(field, size, get_cb, set_cb, set_upper_cb) \
918 	{ \
919 		offsetof(struct spdk_nvme_registers, field), \
920 		size, \
921 		#field, \
922 		get_cb, set_cb, set_upper_cb \
923 	}
924 
925 static const struct nvmf_prop nvmf_props[] = {
926 	PROP(cap,  8, nvmf_prop_get_cap,  NULL,                    NULL),
927 	PROP(vs,   4, nvmf_prop_get_vs,   NULL,                    NULL),
928 	PROP(cc,   4, nvmf_prop_get_cc,   nvmf_prop_set_cc,        NULL),
929 	PROP(csts, 4, nvmf_prop_get_csts, NULL,                    NULL),
930 	PROP(aqa,  4, nvmf_prop_get_aqa,  nvmf_prop_set_aqa,       NULL),
931 	PROP(asq,  8, nvmf_prop_get_asq,  nvmf_prop_set_asq_lower, nvmf_prop_set_asq_upper),
932 	PROP(acq,  8, nvmf_prop_get_acq,  nvmf_prop_set_acq_lower, nvmf_prop_set_acq_upper),
933 };
934 
935 static const struct nvmf_prop *
936 find_prop(uint32_t ofst, uint8_t size)
937 {
938 	size_t i;
939 
940 	for (i = 0; i < SPDK_COUNTOF(nvmf_props); i++) {
941 		const struct nvmf_prop *prop = &nvmf_props[i];
942 
943 		if ((ofst >= prop->ofst) && (ofst + size <= prop->ofst + prop->size)) {
944 			return prop;
945 		}
946 	}
947 
948 	return NULL;
949 }
950 
951 static int
952 nvmf_property_get(struct spdk_nvmf_request *req)
953 {
954 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
955 	struct spdk_nvmf_fabric_prop_get_cmd *cmd = &req->cmd->prop_get_cmd;
956 	struct spdk_nvmf_fabric_prop_get_rsp *response = &req->rsp->prop_get_rsp;
957 	const struct nvmf_prop *prop;
958 	uint8_t size;
959 
960 	response->status.sc = 0;
961 	response->value.u64 = 0;
962 
963 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "size %d, offset 0x%x\n",
964 		      cmd->attrib.size, cmd->ofst);
965 
966 	switch (cmd->attrib.size) {
967 	case SPDK_NVMF_PROP_SIZE_4:
968 		size = 4;
969 		break;
970 	case SPDK_NVMF_PROP_SIZE_8:
971 		size = 8;
972 		break;
973 	default:
974 		SPDK_ERRLOG("Invalid size value %d\n", cmd->attrib.size);
975 		response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
976 		response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM;
977 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
978 	}
979 
980 	prop = find_prop(cmd->ofst, size);
981 	if (prop == NULL || prop->get_cb == NULL) {
982 		response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
983 		response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM;
984 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
985 	}
986 
987 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "name: %s\n", prop->name);
988 
989 	response->value.u64 = prop->get_cb(ctrlr);
990 
991 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "response value: 0x%" PRIx64 "\n", response->value.u64);
992 
993 	if (size != prop->size) {
994 		/* The size must be 4 and the prop->size is 8. Figure out which part of the property to read. */
995 		assert(size == 4);
996 		assert(prop->size == 8);
997 
998 		if (cmd->ofst == prop->ofst) {
999 			/* Keep bottom 4 bytes only */
1000 			response->value.u64 &= 0xFFFFFFFF;
1001 		} else {
1002 			/* Keep top 4 bytes only */
1003 			response->value.u64 >>= 32;
1004 		}
1005 	}
1006 
1007 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1008 }
1009 
1010 static int
1011 nvmf_property_set(struct spdk_nvmf_request *req)
1012 {
1013 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1014 	struct spdk_nvmf_fabric_prop_set_cmd *cmd = &req->cmd->prop_set_cmd;
1015 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
1016 	const struct nvmf_prop *prop;
1017 	uint64_t value;
1018 	uint8_t size;
1019 	bool ret;
1020 
1021 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "size %d, offset 0x%x, value 0x%" PRIx64 "\n",
1022 		      cmd->attrib.size, cmd->ofst, cmd->value.u64);
1023 
1024 	switch (cmd->attrib.size) {
1025 	case SPDK_NVMF_PROP_SIZE_4:
1026 		size = 4;
1027 		break;
1028 	case SPDK_NVMF_PROP_SIZE_8:
1029 		size = 8;
1030 		break;
1031 	default:
1032 		SPDK_ERRLOG("Invalid size value %d\n", cmd->attrib.size);
1033 		response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
1034 		response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM;
1035 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1036 	}
1037 
1038 	prop = find_prop(cmd->ofst, size);
1039 	if (prop == NULL || prop->set_cb == NULL) {
1040 		SPDK_ERRLOG("Invalid offset 0x%x\n", cmd->ofst);
1041 		response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
1042 		response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM;
1043 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1044 	}
1045 
1046 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "name: %s\n", prop->name);
1047 
1048 	value = cmd->value.u64;
1049 
1050 	if (prop->size == 4) {
1051 		ret = prop->set_cb(ctrlr, (uint32_t)value);
1052 	} else if (size != prop->size) {
1053 		/* The size must be 4 and the prop->size is 8. Figure out which part of the property to write. */
1054 		assert(size == 4);
1055 		assert(prop->size == 8);
1056 
1057 		if (cmd->ofst == prop->ofst) {
1058 			ret = prop->set_cb(ctrlr, (uint32_t)value);
1059 		} else {
1060 			ret = prop->set_upper_cb(ctrlr, (uint32_t)value);
1061 		}
1062 	} else {
1063 		ret = prop->set_cb(ctrlr, (uint32_t)value);
1064 		if (ret) {
1065 			ret = prop->set_upper_cb(ctrlr, (uint32_t)(value >> 32));
1066 		}
1067 	}
1068 
1069 	if (!ret) {
1070 		SPDK_ERRLOG("prop set_cb failed\n");
1071 		response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
1072 		response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM;
1073 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1074 	}
1075 
1076 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1077 }
1078 
1079 static int
1080 nvmf_ctrlr_set_features_arbitration(struct spdk_nvmf_request *req)
1081 {
1082 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1083 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1084 
1085 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Arbitration (cdw11 = 0x%0x)\n", cmd->cdw11);
1086 
1087 	ctrlr->feat.arbitration.raw = cmd->cdw11;
1088 	ctrlr->feat.arbitration.bits.reserved = 0;
1089 
1090 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1091 }
1092 
1093 static int
1094 nvmf_ctrlr_set_features_power_management(struct spdk_nvmf_request *req)
1095 {
1096 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1097 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1098 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
1099 
1100 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Power Management (cdw11 = 0x%0x)\n", cmd->cdw11);
1101 
1102 	/* Only PS = 0 is allowed, since we report NPSS = 0 */
1103 	if (cmd->cdw11_bits.feat_power_management.bits.ps != 0) {
1104 		SPDK_ERRLOG("Invalid power state %u\n", cmd->cdw11_bits.feat_power_management.bits.ps);
1105 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
1106 		rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1107 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1108 	}
1109 
1110 	ctrlr->feat.power_management.raw = cmd->cdw11;
1111 	ctrlr->feat.power_management.bits.reserved = 0;
1112 
1113 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1114 }
1115 
1116 static bool
1117 temp_threshold_opts_valid(const union spdk_nvme_feat_temperature_threshold *opts)
1118 {
1119 	/*
1120 	 * Valid TMPSEL values:
1121 	 *  0000b - 1000b: temperature sensors
1122 	 *  1111b: set all implemented temperature sensors
1123 	 */
1124 	if (opts->bits.tmpsel >= 9 && opts->bits.tmpsel != 15) {
1125 		/* 1001b - 1110b: reserved */
1126 		SPDK_ERRLOG("Invalid TMPSEL %u\n", opts->bits.tmpsel);
1127 		return false;
1128 	}
1129 
1130 	/*
1131 	 * Valid THSEL values:
1132 	 *  00b: over temperature threshold
1133 	 *  01b: under temperature threshold
1134 	 */
1135 	if (opts->bits.thsel > 1) {
1136 		/* 10b - 11b: reserved */
1137 		SPDK_ERRLOG("Invalid THSEL %u\n", opts->bits.thsel);
1138 		return false;
1139 	}
1140 
1141 	return true;
1142 }
1143 
1144 static int
1145 nvmf_ctrlr_set_features_temperature_threshold(struct spdk_nvmf_request *req)
1146 {
1147 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1148 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
1149 
1150 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Temperature Threshold (cdw11 = 0x%0x)\n", cmd->cdw11);
1151 
1152 	if (!temp_threshold_opts_valid(&cmd->cdw11_bits.feat_temp_threshold)) {
1153 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
1154 		rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1155 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1156 	}
1157 
1158 	/* TODO: no sensors implemented - ignore new values */
1159 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1160 }
1161 
1162 static int
1163 nvmf_ctrlr_get_features_temperature_threshold(struct spdk_nvmf_request *req)
1164 {
1165 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1166 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
1167 
1168 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Get Features - Temperature Threshold (cdw11 = 0x%0x)\n", cmd->cdw11);
1169 
1170 	if (!temp_threshold_opts_valid(&cmd->cdw11_bits.feat_temp_threshold)) {
1171 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
1172 		rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1173 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1174 	}
1175 
1176 	/* TODO: no sensors implemented - return 0 for all thresholds */
1177 	rsp->cdw0 = 0;
1178 
1179 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1180 }
1181 
1182 static int
1183 nvmf_ctrlr_set_features_error_recovery(struct spdk_nvmf_request *req)
1184 {
1185 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1186 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1187 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
1188 
1189 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Error Recovery (cdw11 = 0x%0x)\n", cmd->cdw11);
1190 
1191 	if (cmd->cdw11_bits.feat_error_recovery.bits.dulbe) {
1192 		/*
1193 		 * Host is not allowed to set this bit, since we don't advertise it in
1194 		 * Identify Namespace.
1195 		 */
1196 		SPDK_ERRLOG("Host set unsupported DULBE bit\n");
1197 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
1198 		rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1199 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1200 	}
1201 
1202 	ctrlr->feat.error_recovery.raw = cmd->cdw11;
1203 	ctrlr->feat.error_recovery.bits.reserved = 0;
1204 
1205 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1206 }
1207 
1208 static int
1209 nvmf_ctrlr_set_features_volatile_write_cache(struct spdk_nvmf_request *req)
1210 {
1211 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1212 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1213 
1214 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Volatile Write Cache (cdw11 = 0x%0x)\n", cmd->cdw11);
1215 
1216 	ctrlr->feat.volatile_write_cache.raw = cmd->cdw11;
1217 	ctrlr->feat.volatile_write_cache.bits.reserved = 0;
1218 
1219 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Volatile Write Cache %s\n",
1220 		      ctrlr->feat.volatile_write_cache.bits.wce ? "Enabled" : "Disabled");
1221 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1222 }
1223 
1224 static int
1225 nvmf_ctrlr_set_features_write_atomicity(struct spdk_nvmf_request *req)
1226 {
1227 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1228 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1229 
1230 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Write Atomicity (cdw11 = 0x%0x)\n", cmd->cdw11);
1231 
1232 	ctrlr->feat.write_atomicity.raw = cmd->cdw11;
1233 	ctrlr->feat.write_atomicity.bits.reserved = 0;
1234 
1235 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1236 }
1237 
1238 static int
1239 nvmf_ctrlr_set_features_host_identifier(struct spdk_nvmf_request *req)
1240 {
1241 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
1242 
1243 	SPDK_ERRLOG("Set Features - Host Identifier not allowed\n");
1244 	response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
1245 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1246 }
1247 
1248 static int
1249 nvmf_ctrlr_get_features_host_identifier(struct spdk_nvmf_request *req)
1250 {
1251 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1252 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1253 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
1254 
1255 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Get Features - Host Identifier\n");
1256 
1257 	if (!cmd->cdw11_bits.feat_host_identifier.bits.exhid) {
1258 		/* NVMe over Fabrics requires EXHID=1 (128-bit/16-byte host ID) */
1259 		SPDK_ERRLOG("Get Features - Host Identifier with EXHID=0 not allowed\n");
1260 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1261 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1262 	}
1263 
1264 	if (req->data == NULL || req->length < sizeof(ctrlr->hostid)) {
1265 		SPDK_ERRLOG("Invalid data buffer for Get Features - Host Identifier\n");
1266 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1267 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1268 	}
1269 
1270 	spdk_uuid_copy((struct spdk_uuid *)req->data, &ctrlr->hostid);
1271 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1272 }
1273 
1274 static int
1275 nvmf_ctrlr_get_features_reservation_notification_mask(struct spdk_nvmf_request *req)
1276 {
1277 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1278 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1279 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
1280 	struct spdk_nvmf_ns *ns;
1281 
1282 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "get Features - Reservation Notificaton Mask\n");
1283 
1284 	if (cmd->nsid == 0xffffffffu) {
1285 		SPDK_ERRLOG("get Features - Invalid Namespace ID\n");
1286 		rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1287 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1288 	}
1289 
1290 	ns = _nvmf_subsystem_get_ns(ctrlr->subsys, cmd->nsid);
1291 	if (ns == NULL) {
1292 		SPDK_ERRLOG("Set Features - Invalid Namespace ID\n");
1293 		rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1294 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1295 	}
1296 	rsp->cdw0 = ns->mask;
1297 
1298 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1299 }
1300 
1301 static int
1302 nvmf_ctrlr_set_features_reservation_notification_mask(struct spdk_nvmf_request *req)
1303 {
1304 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1305 	struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys;
1306 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1307 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
1308 	struct spdk_nvmf_ns *ns;
1309 
1310 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Reservation Notificaton Mask\n");
1311 
1312 	if (cmd->nsid == 0xffffffffu) {
1313 		for (ns = spdk_nvmf_subsystem_get_first_ns(subsystem); ns != NULL;
1314 		     ns = spdk_nvmf_subsystem_get_next_ns(subsystem, ns)) {
1315 			ns->mask = cmd->cdw11;
1316 		}
1317 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1318 	}
1319 
1320 	ns = _nvmf_subsystem_get_ns(ctrlr->subsys, cmd->nsid);
1321 	if (ns == NULL) {
1322 		SPDK_ERRLOG("Set Features - Invalid Namespace ID\n");
1323 		rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1324 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1325 	}
1326 	ns->mask = cmd->cdw11;
1327 
1328 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1329 }
1330 
1331 static int
1332 nvmf_ctrlr_get_features_reservation_persistence(struct spdk_nvmf_request *req)
1333 {
1334 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1335 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1336 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
1337 	struct spdk_nvmf_ns *ns;
1338 
1339 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Get Features - Reservation Persistence\n");
1340 
1341 	ns = _nvmf_subsystem_get_ns(ctrlr->subsys, cmd->nsid);
1342 	/* NSID with 0xffffffffu also included */
1343 	if (ns == NULL) {
1344 		SPDK_ERRLOG("Get Features - Invalid Namespace ID\n");
1345 		response->status.sct = SPDK_NVME_SCT_GENERIC;
1346 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1347 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1348 	}
1349 
1350 	response->cdw0 = ns->ptpl_activated;
1351 
1352 	response->status.sct = SPDK_NVME_SCT_GENERIC;
1353 	response->status.sc = SPDK_NVME_SC_SUCCESS;
1354 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1355 }
1356 
1357 static int
1358 nvmf_ctrlr_set_features_reservation_persistence(struct spdk_nvmf_request *req)
1359 {
1360 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1361 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1362 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
1363 	struct spdk_nvmf_ns *ns;
1364 	bool ptpl;
1365 
1366 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Reservation Persistence\n");
1367 
1368 	ns = _nvmf_subsystem_get_ns(ctrlr->subsys, cmd->nsid);
1369 	ptpl = cmd->cdw11_bits.feat_rsv_persistence.bits.ptpl;
1370 
1371 	if (cmd->nsid != 0xffffffffu && ns && ns->ptpl_file) {
1372 		ns->ptpl_activated = ptpl;
1373 	} else if (cmd->nsid == 0xffffffffu) {
1374 		for (ns = spdk_nvmf_subsystem_get_first_ns(ctrlr->subsys); ns && ns->ptpl_file;
1375 		     ns = spdk_nvmf_subsystem_get_next_ns(ctrlr->subsys, ns)) {
1376 			ns->ptpl_activated = ptpl;
1377 		}
1378 	} else {
1379 		SPDK_ERRLOG("Set Features - Invalid Namespace ID or Reservation Configuration\n");
1380 		response->status.sct = SPDK_NVME_SCT_GENERIC;
1381 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1382 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1383 	}
1384 
1385 	/* TODO: Feature not changeable for now */
1386 	response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
1387 	response->status.sc = SPDK_NVME_SC_FEATURE_ID_NOT_SAVEABLE;
1388 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1389 }
1390 
1391 static int
1392 nvmf_ctrlr_set_features_keep_alive_timer(struct spdk_nvmf_request *req)
1393 {
1394 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1395 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1396 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
1397 
1398 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Keep Alive Timer (%u ms)\n", cmd->cdw11);
1399 
1400 	/*
1401 	 * if attempts to disable keep alive by setting kato to 0h
1402 	 * a status value of keep alive invalid shall be returned
1403 	 */
1404 	if (cmd->cdw11_bits.feat_keep_alive_timer.bits.kato == 0) {
1405 		rsp->status.sc = SPDK_NVME_SC_KEEP_ALIVE_INVALID;
1406 	} else if (cmd->cdw11_bits.feat_keep_alive_timer.bits.kato < MIN_KEEP_ALIVE_TIMEOUT_IN_MS) {
1407 		ctrlr->feat.keep_alive_timer.bits.kato = MIN_KEEP_ALIVE_TIMEOUT_IN_MS;
1408 	} else {
1409 		/* round up to milliseconds */
1410 		ctrlr->feat.keep_alive_timer.bits.kato = spdk_divide_round_up(
1411 					cmd->cdw11_bits.feat_keep_alive_timer.bits.kato,
1412 					KAS_DEFAULT_VALUE * KAS_TIME_UNIT_IN_MS) *
1413 				KAS_DEFAULT_VALUE * KAS_TIME_UNIT_IN_MS;
1414 	}
1415 
1416 	/*
1417 	 * if change the keep alive timeout value successfully
1418 	 * update the keep alive poller.
1419 	 */
1420 	if (cmd->cdw11_bits.feat_keep_alive_timer.bits.kato != 0) {
1421 		if (ctrlr->keep_alive_poller != NULL) {
1422 			spdk_poller_unregister(&ctrlr->keep_alive_poller);
1423 		}
1424 		ctrlr->keep_alive_poller = SPDK_POLLER_REGISTER(nvmf_ctrlr_keep_alive_poll, ctrlr,
1425 					   ctrlr->feat.keep_alive_timer.bits.kato * 1000);
1426 	}
1427 
1428 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Keep Alive Timer set to %u ms\n",
1429 		      ctrlr->feat.keep_alive_timer.bits.kato);
1430 
1431 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1432 }
1433 
1434 static int
1435 nvmf_ctrlr_set_features_number_of_queues(struct spdk_nvmf_request *req)
1436 {
1437 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1438 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
1439 	uint32_t count;
1440 
1441 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Number of Queues, cdw11 0x%x\n",
1442 		      req->cmd->nvme_cmd.cdw11);
1443 
1444 	count = spdk_bit_array_count_set(ctrlr->qpair_mask);
1445 	/* verify that the controller is ready to process commands */
1446 	if (count > 1) {
1447 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Queue pairs already active!\n");
1448 		rsp->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
1449 	} else {
1450 		/*
1451 		 * Ignore the value requested by the host -
1452 		 * always return the pre-configured value based on max_qpairs_allowed.
1453 		 */
1454 		rsp->cdw0 = ctrlr->feat.number_of_queues.raw;
1455 	}
1456 
1457 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1458 }
1459 
1460 static int
1461 nvmf_ctrlr_set_features_async_event_configuration(struct spdk_nvmf_request *req)
1462 {
1463 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1464 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1465 
1466 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Async Event Configuration, cdw11 0x%08x\n",
1467 		      cmd->cdw11);
1468 	ctrlr->feat.async_event_configuration.raw = cmd->cdw11;
1469 	ctrlr->feat.async_event_configuration.bits.reserved = 0;
1470 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1471 }
1472 
1473 static int
1474 nvmf_ctrlr_async_event_request(struct spdk_nvmf_request *req)
1475 {
1476 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1477 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
1478 	struct spdk_nvmf_subsystem_poll_group *sgroup;
1479 
1480 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Async Event Request\n");
1481 
1482 	/* Only one asynchronous event is supported for now */
1483 	if (ctrlr->aer_req != NULL) {
1484 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "AERL exceeded\n");
1485 		rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
1486 		rsp->status.sc = SPDK_NVME_SC_ASYNC_EVENT_REQUEST_LIMIT_EXCEEDED;
1487 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1488 	}
1489 
1490 	if (ctrlr->notice_event.bits.async_event_type ==
1491 	    SPDK_NVME_ASYNC_EVENT_TYPE_NOTICE) {
1492 		rsp->cdw0 = ctrlr->notice_event.raw;
1493 		ctrlr->notice_event.raw = 0;
1494 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1495 	}
1496 
1497 	if (ctrlr->reservation_event.bits.async_event_type ==
1498 	    SPDK_NVME_ASYNC_EVENT_TYPE_IO) {
1499 		rsp->cdw0 = ctrlr->reservation_event.raw;
1500 		ctrlr->reservation_event.raw = 0;
1501 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1502 	}
1503 
1504 	/* AER cmd is an exception */
1505 	sgroup = &req->qpair->group->sgroups[ctrlr->subsys->id];
1506 	sgroup->io_outstanding--;
1507 
1508 	ctrlr->aer_req = req;
1509 	return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
1510 }
1511 
1512 static void
1513 nvmf_get_firmware_slot_log_page(void *buffer, uint64_t offset, uint32_t length)
1514 {
1515 	struct spdk_nvme_firmware_page fw_page;
1516 	size_t copy_len;
1517 
1518 	memset(&fw_page, 0, sizeof(fw_page));
1519 	fw_page.afi.active_slot = 1;
1520 	fw_page.afi.next_reset_slot = 0;
1521 	spdk_strcpy_pad(fw_page.revision[0], FW_VERSION, sizeof(fw_page.revision[0]), ' ');
1522 
1523 	if (offset < sizeof(fw_page)) {
1524 		copy_len = spdk_min(sizeof(fw_page) - offset, length);
1525 		if (copy_len > 0) {
1526 			memcpy(buffer, (const char *)&fw_page + offset, copy_len);
1527 		}
1528 	}
1529 }
1530 
1531 void
1532 nvmf_ctrlr_ns_changed(struct spdk_nvmf_ctrlr *ctrlr, uint32_t nsid)
1533 {
1534 	uint16_t max_changes = SPDK_COUNTOF(ctrlr->changed_ns_list.ns_list);
1535 	uint16_t i;
1536 	bool found = false;
1537 
1538 	for (i = 0; i < ctrlr->changed_ns_list_count; i++) {
1539 		if (ctrlr->changed_ns_list.ns_list[i] == nsid) {
1540 			/* nsid is already in the list */
1541 			found = true;
1542 			break;
1543 		}
1544 	}
1545 
1546 	if (!found) {
1547 		if (ctrlr->changed_ns_list_count == max_changes) {
1548 			/* Out of space - set first entry to FFFFFFFFh and zero-fill the rest. */
1549 			ctrlr->changed_ns_list.ns_list[0] = 0xFFFFFFFFu;
1550 			for (i = 1; i < max_changes; i++) {
1551 				ctrlr->changed_ns_list.ns_list[i] = 0;
1552 			}
1553 		} else {
1554 			ctrlr->changed_ns_list.ns_list[ctrlr->changed_ns_list_count++] = nsid;
1555 		}
1556 	}
1557 }
1558 
1559 static void
1560 nvmf_get_changed_ns_list_log_page(struct spdk_nvmf_ctrlr *ctrlr,
1561 				  void *buffer, uint64_t offset, uint32_t length)
1562 {
1563 	size_t copy_length;
1564 
1565 	if (offset < sizeof(ctrlr->changed_ns_list)) {
1566 		copy_length = spdk_min(length, sizeof(ctrlr->changed_ns_list) - offset);
1567 		if (copy_length) {
1568 			memcpy(buffer, (char *)&ctrlr->changed_ns_list + offset, copy_length);
1569 		}
1570 	}
1571 
1572 	/* Clear log page each time it is read */
1573 	ctrlr->changed_ns_list_count = 0;
1574 	memset(&ctrlr->changed_ns_list, 0, sizeof(ctrlr->changed_ns_list));
1575 }
1576 
1577 /* The structure can be modified if we provide support for other commands in future */
1578 static const struct spdk_nvme_cmds_and_effect_log_page g_cmds_and_effect_log_page = {
1579 	.admin_cmds_supported = {
1580 		/* CSUPP, LBCC, NCC, NIC, CCC, CSE */
1581 		/* Get Log Page */
1582 		[SPDK_NVME_OPC_GET_LOG_PAGE]		= {1, 0, 0, 0, 0, 0, 0, 0},
1583 		/* Identify */
1584 		[SPDK_NVME_OPC_IDENTIFY]		= {1, 0, 0, 0, 0, 0, 0, 0},
1585 		/* Abort */
1586 		[SPDK_NVME_OPC_ABORT]			= {1, 0, 0, 0, 0, 0, 0, 0},
1587 		/* Set Features */
1588 		[SPDK_NVME_OPC_SET_FEATURES]		= {1, 0, 0, 0, 0, 0, 0, 0},
1589 		/* Get Features */
1590 		[SPDK_NVME_OPC_GET_FEATURES]		= {1, 0, 0, 0, 0, 0, 0, 0},
1591 		/* Async Event Request */
1592 		[SPDK_NVME_OPC_ASYNC_EVENT_REQUEST]	= {1, 0, 0, 0, 0, 0, 0, 0},
1593 		/* Keep Alive */
1594 		[SPDK_NVME_OPC_KEEP_ALIVE]		= {1, 0, 0, 0, 0, 0, 0, 0},
1595 	},
1596 	.io_cmds_supported = {
1597 		/* FLUSH */
1598 		[SPDK_NVME_OPC_FLUSH]			= {1, 1, 0, 0, 0, 0, 0, 0},
1599 		/* WRITE */
1600 		[SPDK_NVME_OPC_WRITE]			= {1, 1, 0, 0, 0, 0, 0, 0},
1601 		/* READ */
1602 		[SPDK_NVME_OPC_READ]			= {1, 0, 0, 0, 0, 0, 0, 0},
1603 		/* WRITE ZEROES */
1604 		[SPDK_NVME_OPC_WRITE_ZEROES]		= {1, 1, 0, 0, 0, 0, 0, 0},
1605 		/* DATASET MANAGEMENT */
1606 		[SPDK_NVME_OPC_DATASET_MANAGEMENT]	= {1, 1, 0, 0, 0, 0, 0, 0},
1607 		/* COMPARE */
1608 		[SPDK_NVME_OPC_COMPARE]			= {1, 0, 0, 0, 0, 0, 0, 0},
1609 	},
1610 };
1611 
1612 static void
1613 nvmf_get_cmds_and_effects_log_page(void *buffer,
1614 				   uint64_t offset, uint32_t length)
1615 {
1616 	uint32_t page_size = sizeof(struct spdk_nvme_cmds_and_effect_log_page);
1617 	size_t copy_len = 0;
1618 	size_t zero_len = length;
1619 
1620 	if (offset < page_size) {
1621 		copy_len = spdk_min(page_size - offset, length);
1622 		zero_len -= copy_len;
1623 		memcpy(buffer, (char *)(&g_cmds_and_effect_log_page) + offset, copy_len);
1624 	}
1625 
1626 	if (zero_len) {
1627 		memset((char *)buffer + copy_len, 0, zero_len);
1628 	}
1629 }
1630 
1631 static void
1632 nvmf_get_reservation_notification_log_page(struct spdk_nvmf_ctrlr *ctrlr,
1633 		void *data, uint64_t offset, uint32_t length)
1634 {
1635 	uint32_t unit_log_len, avail_log_len, next_pos, copy_len;
1636 	struct spdk_nvmf_reservation_log *log, *log_tmp;
1637 	uint8_t *buf = data;
1638 
1639 	unit_log_len = sizeof(struct spdk_nvme_reservation_notification_log);
1640 	/* No available log, return 1 zeroed log page */
1641 	if (!ctrlr->num_avail_log_pages) {
1642 		memset(buf, 0, spdk_min(length, unit_log_len));
1643 		return;
1644 	}
1645 
1646 	avail_log_len = ctrlr->num_avail_log_pages * unit_log_len;
1647 	if (offset >= avail_log_len) {
1648 		return;
1649 	}
1650 
1651 	next_pos = copy_len = 0;
1652 	TAILQ_FOREACH_SAFE(log, &ctrlr->log_head, link, log_tmp) {
1653 		TAILQ_REMOVE(&ctrlr->log_head, log, link);
1654 		ctrlr->num_avail_log_pages--;
1655 
1656 		next_pos += unit_log_len;
1657 		if (next_pos > offset) {
1658 			copy_len = spdk_min(next_pos - offset, length);
1659 			memcpy(buf, &log->log, copy_len);
1660 			length -= copy_len;
1661 			offset += copy_len;
1662 			buf += copy_len;
1663 		}
1664 		free(log);
1665 
1666 		if (length == 0) {
1667 			break;
1668 		}
1669 	}
1670 	return;
1671 }
1672 
1673 static int
1674 nvmf_ctrlr_get_log_page(struct spdk_nvmf_request *req)
1675 {
1676 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1677 	struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys;
1678 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1679 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
1680 	uint64_t offset, len;
1681 	uint32_t numdl, numdu;
1682 	uint8_t lid;
1683 
1684 	if (req->data == NULL) {
1685 		SPDK_ERRLOG("get log command with no buffer\n");
1686 		response->status.sct = SPDK_NVME_SCT_GENERIC;
1687 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1688 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1689 	}
1690 
1691 	offset = (uint64_t)cmd->cdw12 | ((uint64_t)cmd->cdw13 << 32);
1692 	if (offset & 3) {
1693 		SPDK_ERRLOG("Invalid log page offset 0x%" PRIx64 "\n", offset);
1694 		response->status.sct = SPDK_NVME_SCT_GENERIC;
1695 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1696 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1697 	}
1698 
1699 	numdl = cmd->cdw10_bits.get_log_page.numdl;
1700 	numdu = cmd->cdw11_bits.get_log_page.numdu;
1701 	len = ((numdu << 16) + numdl + (uint64_t)1) * 4;
1702 	if (len > req->length) {
1703 		SPDK_ERRLOG("Get log page: len (%" PRIu64 ") > buf size (%u)\n",
1704 			    len, req->length);
1705 		response->status.sct = SPDK_NVME_SCT_GENERIC;
1706 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1707 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1708 	}
1709 
1710 	lid = cmd->cdw10_bits.get_log_page.lid;
1711 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Get log page: LID=0x%02X offset=0x%" PRIx64 " len=0x%" PRIx64 "\n",
1712 		      lid, offset, len);
1713 
1714 	if (subsystem->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) {
1715 		switch (lid) {
1716 		case SPDK_NVME_LOG_DISCOVERY:
1717 			nvmf_get_discovery_log_page(subsystem->tgt, ctrlr->hostnqn, req->iov, req->iovcnt, offset,
1718 						    len);
1719 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1720 		default:
1721 			goto invalid_log_page;
1722 		}
1723 	} else {
1724 		switch (lid) {
1725 		case SPDK_NVME_LOG_ERROR:
1726 		case SPDK_NVME_LOG_HEALTH_INFORMATION:
1727 			/* TODO: actually fill out log page data */
1728 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1729 		case SPDK_NVME_LOG_FIRMWARE_SLOT:
1730 			nvmf_get_firmware_slot_log_page(req->data, offset, len);
1731 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1732 		case SPDK_NVME_LOG_COMMAND_EFFECTS_LOG:
1733 			nvmf_get_cmds_and_effects_log_page(req->data, offset, len);
1734 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1735 		case SPDK_NVME_LOG_CHANGED_NS_LIST:
1736 			nvmf_get_changed_ns_list_log_page(ctrlr, req->data, offset, len);
1737 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1738 		case SPDK_NVME_LOG_RESERVATION_NOTIFICATION:
1739 			nvmf_get_reservation_notification_log_page(ctrlr, req->data, offset, len);
1740 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1741 		default:
1742 			goto invalid_log_page;
1743 		}
1744 	}
1745 
1746 invalid_log_page:
1747 	SPDK_ERRLOG("Unsupported Get Log Page 0x%02X\n", lid);
1748 	response->status.sct = SPDK_NVME_SCT_GENERIC;
1749 	response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1750 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1751 }
1752 
1753 int
1754 spdk_nvmf_ctrlr_identify_ns(struct spdk_nvmf_ctrlr *ctrlr,
1755 			    struct spdk_nvme_cmd *cmd,
1756 			    struct spdk_nvme_cpl *rsp,
1757 			    struct spdk_nvme_ns_data *nsdata)
1758 {
1759 	struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys;
1760 	struct spdk_nvmf_ns *ns;
1761 	uint32_t max_num_blocks;
1762 
1763 	if (cmd->nsid == 0 || cmd->nsid > subsystem->max_nsid) {
1764 		SPDK_ERRLOG("Identify Namespace for invalid NSID %u\n", cmd->nsid);
1765 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
1766 		rsp->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT;
1767 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1768 	}
1769 
1770 	ns = _nvmf_subsystem_get_ns(subsystem, cmd->nsid);
1771 	if (ns == NULL || ns->bdev == NULL) {
1772 		/*
1773 		 * Inactive namespaces should return a zero filled data structure.
1774 		 * The data buffer is already zeroed by nvmf_ctrlr_process_admin_cmd(),
1775 		 * so we can just return early here.
1776 		 */
1777 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Identify Namespace for inactive NSID %u\n", cmd->nsid);
1778 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
1779 		rsp->status.sc = SPDK_NVME_SC_SUCCESS;
1780 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1781 	}
1782 
1783 	nvmf_bdev_ctrlr_identify_ns(ns, nsdata, ctrlr->dif_insert_or_strip);
1784 
1785 	/* Due to bug in the Linux kernel NVMe driver we have to set noiob no larger than mdts */
1786 	max_num_blocks = ctrlr->admin_qpair->transport->opts.max_io_size /
1787 			 (1U << nsdata->lbaf[nsdata->flbas.format].lbads);
1788 	if (nsdata->noiob > max_num_blocks) {
1789 		nsdata->noiob = max_num_blocks;
1790 	}
1791 
1792 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1793 }
1794 
1795 static void
1796 nvmf_ctrlr_populate_oacs(struct spdk_nvmf_ctrlr *ctrlr,
1797 			 struct spdk_nvme_ctrlr_data *cdata)
1798 {
1799 	cdata->oacs.virtualization_management =
1800 		g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_VIRTUALIZATION_MANAGEMENT].hdlr != NULL;
1801 	cdata->oacs.nvme_mi = g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_NVME_MI_SEND].hdlr != NULL
1802 			      && g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_NVME_MI_RECEIVE].hdlr != NULL;
1803 	cdata->oacs.directives = g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_DIRECTIVE_SEND].hdlr != NULL
1804 				 && g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_DIRECTIVE_RECEIVE].hdlr != NULL;
1805 	cdata->oacs.device_self_test =
1806 		g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_DEVICE_SELF_TEST].hdlr != NULL;
1807 	cdata->oacs.ns_manage = g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_NS_MANAGEMENT].hdlr != NULL
1808 				&& g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_NS_ATTACHMENT].hdlr != NULL;
1809 	cdata->oacs.firmware = g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_FIRMWARE_IMAGE_DOWNLOAD].hdlr !=
1810 			       NULL
1811 			       && g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_FIRMWARE_COMMIT].hdlr != NULL;
1812 	cdata->oacs.format =
1813 		g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_FORMAT_NVM].hdlr != NULL;
1814 	cdata->oacs.security = g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_SECURITY_SEND].hdlr != NULL
1815 			       && g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_SECURITY_RECEIVE].hdlr != NULL;
1816 	cdata->oacs.get_lba_status = g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_GET_LBA_STATUS].hdlr !=
1817 				     NULL;
1818 }
1819 
1820 void
1821 spdk_nvmf_ctrlr_data_init(struct spdk_nvmf_transport_opts *opts, struct spdk_nvmf_ctrlr_data *cdata)
1822 {
1823 	cdata->kas = KAS_DEFAULT_VALUE;
1824 	cdata->nvmf_specific.ioccsz = sizeof(struct spdk_nvme_cmd) / 16;
1825 	cdata->nvmf_specific.ioccsz += opts->in_capsule_data_size / 16;
1826 	cdata->nvmf_specific.iorcsz = sizeof(struct spdk_nvme_cpl) / 16;
1827 	cdata->nvmf_specific.icdoff = 0; /* offset starts directly after SQE */
1828 	cdata->nvmf_specific.ctrattr.ctrlr_model = SPDK_NVMF_CTRLR_MODEL_DYNAMIC;
1829 	cdata->nvmf_specific.msdbd = 1;
1830 }
1831 
1832 int
1833 spdk_nvmf_ctrlr_identify_ctrlr(struct spdk_nvmf_ctrlr *ctrlr, struct spdk_nvme_ctrlr_data *cdata)
1834 {
1835 	struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys;
1836 	struct spdk_nvmf_transport *transport = ctrlr->admin_qpair->transport;
1837 
1838 	/*
1839 	 * Common fields for discovery and NVM subsystems
1840 	 */
1841 	spdk_strcpy_pad(cdata->fr, FW_VERSION, sizeof(cdata->fr), ' ');
1842 	assert((transport->opts.max_io_size % 4096) == 0);
1843 	cdata->mdts = spdk_u32log2(transport->opts.max_io_size / 4096);
1844 	cdata->cntlid = ctrlr->cntlid;
1845 	cdata->ver = ctrlr->vcprop.vs;
1846 	cdata->lpa.edlp = 1;
1847 	cdata->elpe = 127;
1848 	cdata->maxcmd = transport->opts.max_queue_depth;
1849 	cdata->sgls.supported = 1;
1850 	cdata->sgls.keyed_sgl = 1;
1851 	cdata->sgls.sgl_offset = 1;
1852 	cdata->fuses.compare_and_write = 1;
1853 	cdata->acwu = 1;
1854 	spdk_strcpy_pad(cdata->subnqn, subsystem->subnqn, sizeof(cdata->subnqn), '\0');
1855 
1856 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ctrlr data: maxcmd 0x%x\n", cdata->maxcmd);
1857 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "sgls data: 0x%x\n", from_le32(&cdata->sgls));
1858 
1859 	/*
1860 	 * NVM subsystem fields (reserved for discovery subsystems)
1861 	 */
1862 	if (subsystem->subtype == SPDK_NVMF_SUBTYPE_NVME) {
1863 		spdk_strcpy_pad(cdata->mn, spdk_nvmf_subsystem_get_mn(subsystem), sizeof(cdata->mn), ' ');
1864 		spdk_strcpy_pad(cdata->sn, spdk_nvmf_subsystem_get_sn(subsystem), sizeof(cdata->sn), ' ');
1865 		cdata->kas = transport->cdata.kas;
1866 
1867 		cdata->rab = 6;
1868 		cdata->cmic.multi_port = 1;
1869 		cdata->cmic.multi_host = 1;
1870 		cdata->oaes.ns_attribute_notices = 1;
1871 		cdata->ctratt.host_id_exhid_supported = 1;
1872 		cdata->aerl = 0;
1873 		cdata->frmw.slot1_ro = 1;
1874 		cdata->frmw.num_slots = 1;
1875 
1876 		cdata->lpa.celp = 1; /* Command Effects log page supported */
1877 
1878 		cdata->sqes.min = 6;
1879 		cdata->sqes.max = 6;
1880 		cdata->cqes.min = 4;
1881 		cdata->cqes.max = 4;
1882 		cdata->nn = subsystem->max_nsid;
1883 		cdata->vwc.present = 1;
1884 		cdata->vwc.flush_broadcast = SPDK_NVME_FLUSH_BROADCAST_NOT_SUPPORTED;
1885 
1886 		cdata->nvmf_specific = transport->cdata.nvmf_specific;
1887 
1888 		cdata->oncs.dsm = nvmf_ctrlr_dsm_supported(ctrlr);
1889 		cdata->oncs.write_zeroes = nvmf_ctrlr_write_zeroes_supported(ctrlr);
1890 		cdata->oncs.reservations = 1;
1891 
1892 		nvmf_ctrlr_populate_oacs(ctrlr, cdata);
1893 
1894 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ext ctrlr data: ioccsz 0x%x\n",
1895 			      cdata->nvmf_specific.ioccsz);
1896 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ext ctrlr data: iorcsz 0x%x\n",
1897 			      cdata->nvmf_specific.iorcsz);
1898 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ext ctrlr data: icdoff 0x%x\n",
1899 			      cdata->nvmf_specific.icdoff);
1900 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ext ctrlr data: ctrattr 0x%x\n",
1901 			      *(uint8_t *)&cdata->nvmf_specific.ctrattr);
1902 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ext ctrlr data: msdbd 0x%x\n",
1903 			      cdata->nvmf_specific.msdbd);
1904 	}
1905 
1906 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1907 }
1908 
1909 static int
1910 nvmf_ctrlr_identify_active_ns_list(struct spdk_nvmf_subsystem *subsystem,
1911 				   struct spdk_nvme_cmd *cmd,
1912 				   struct spdk_nvme_cpl *rsp,
1913 				   struct spdk_nvme_ns_list *ns_list)
1914 {
1915 	struct spdk_nvmf_ns *ns;
1916 	uint32_t count = 0;
1917 
1918 	if (cmd->nsid >= 0xfffffffeUL) {
1919 		SPDK_ERRLOG("Identify Active Namespace List with invalid NSID %u\n", cmd->nsid);
1920 		rsp->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT;
1921 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1922 	}
1923 
1924 	for (ns = spdk_nvmf_subsystem_get_first_ns(subsystem); ns != NULL;
1925 	     ns = spdk_nvmf_subsystem_get_next_ns(subsystem, ns)) {
1926 		if (ns->opts.nsid <= cmd->nsid) {
1927 			continue;
1928 		}
1929 
1930 		ns_list->ns_list[count++] = ns->opts.nsid;
1931 		if (count == SPDK_COUNTOF(ns_list->ns_list)) {
1932 			break;
1933 		}
1934 	}
1935 
1936 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1937 }
1938 
1939 static void
1940 _add_ns_id_desc(void **buf_ptr, size_t *buf_remain,
1941 		enum spdk_nvme_nidt type,
1942 		const void *data, size_t data_size)
1943 {
1944 	struct spdk_nvme_ns_id_desc *desc;
1945 	size_t desc_size = sizeof(*desc) + data_size;
1946 
1947 	/*
1948 	 * These should never fail in practice, since all valid NS ID descriptors
1949 	 * should be defined so that they fit in the available 4096-byte buffer.
1950 	 */
1951 	assert(data_size > 0);
1952 	assert(data_size <= UINT8_MAX);
1953 	assert(desc_size < *buf_remain);
1954 	if (data_size == 0 || data_size > UINT8_MAX || desc_size > *buf_remain) {
1955 		return;
1956 	}
1957 
1958 	desc = *buf_ptr;
1959 	desc->nidt = type;
1960 	desc->nidl = data_size;
1961 	memcpy(desc->nid, data, data_size);
1962 
1963 	*buf_ptr += desc_size;
1964 	*buf_remain -= desc_size;
1965 }
1966 
1967 static int
1968 nvmf_ctrlr_identify_ns_id_descriptor_list(
1969 	struct spdk_nvmf_subsystem *subsystem,
1970 	struct spdk_nvme_cmd *cmd,
1971 	struct spdk_nvme_cpl *rsp,
1972 	void *id_desc_list, size_t id_desc_list_size)
1973 {
1974 	struct spdk_nvmf_ns *ns;
1975 	size_t buf_remain = id_desc_list_size;
1976 	void *buf_ptr = id_desc_list;
1977 
1978 	ns = _nvmf_subsystem_get_ns(subsystem, cmd->nsid);
1979 	if (ns == NULL || ns->bdev == NULL) {
1980 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
1981 		rsp->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT;
1982 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1983 	}
1984 
1985 #define ADD_ID_DESC(type, data, size) \
1986 	do { \
1987 		if (!spdk_mem_all_zero(data, size)) { \
1988 			_add_ns_id_desc(&buf_ptr, &buf_remain, type, data, size); \
1989 		} \
1990 	} while (0)
1991 
1992 	ADD_ID_DESC(SPDK_NVME_NIDT_EUI64, ns->opts.eui64, sizeof(ns->opts.eui64));
1993 	ADD_ID_DESC(SPDK_NVME_NIDT_NGUID, ns->opts.nguid, sizeof(ns->opts.nguid));
1994 	ADD_ID_DESC(SPDK_NVME_NIDT_UUID, &ns->opts.uuid, sizeof(ns->opts.uuid));
1995 
1996 	/*
1997 	 * The list is automatically 0-terminated because controller to host buffers in
1998 	 * admin commands always get zeroed in nvmf_ctrlr_process_admin_cmd().
1999 	 */
2000 
2001 #undef ADD_ID_DESC
2002 
2003 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2004 }
2005 
2006 static int
2007 nvmf_ctrlr_identify(struct spdk_nvmf_request *req)
2008 {
2009 	uint8_t cns;
2010 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
2011 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
2012 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
2013 	struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys;
2014 
2015 	if (req->data == NULL || req->length < 4096) {
2016 		SPDK_ERRLOG("identify command with invalid buffer\n");
2017 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
2018 		rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
2019 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2020 	}
2021 
2022 	cns = cmd->cdw10_bits.identify.cns;
2023 
2024 	if (subsystem->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY &&
2025 	    cns != SPDK_NVME_IDENTIFY_CTRLR) {
2026 		/* Discovery controllers only support Identify Controller */
2027 		goto invalid_cns;
2028 	}
2029 
2030 	switch (cns) {
2031 	case SPDK_NVME_IDENTIFY_NS:
2032 		return spdk_nvmf_ctrlr_identify_ns(ctrlr, cmd, rsp, req->data);
2033 	case SPDK_NVME_IDENTIFY_CTRLR:
2034 		return spdk_nvmf_ctrlr_identify_ctrlr(ctrlr, req->data);
2035 	case SPDK_NVME_IDENTIFY_ACTIVE_NS_LIST:
2036 		return nvmf_ctrlr_identify_active_ns_list(subsystem, cmd, rsp, req->data);
2037 	case SPDK_NVME_IDENTIFY_NS_ID_DESCRIPTOR_LIST:
2038 		return nvmf_ctrlr_identify_ns_id_descriptor_list(subsystem, cmd, rsp, req->data, req->length);
2039 	default:
2040 		goto invalid_cns;
2041 	}
2042 
2043 invalid_cns:
2044 	SPDK_ERRLOG("Identify command with unsupported CNS 0x%02x\n", cns);
2045 	rsp->status.sct = SPDK_NVME_SCT_GENERIC;
2046 	rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
2047 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2048 }
2049 
2050 
2051 static struct spdk_nvmf_request *
2052 nvmf_qpair_abort(struct spdk_nvmf_qpair *qpair, uint16_t cid)
2053 {
2054 	struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr;
2055 	struct spdk_nvmf_request *req;
2056 
2057 	if (nvmf_qpair_is_admin_queue(qpair)) {
2058 		if (ctrlr->aer_req && ctrlr->aer_req->cmd->nvme_cmd.cid == cid) {
2059 			SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Aborting AER request\n");
2060 			req = ctrlr->aer_req;
2061 			ctrlr->aer_req = NULL;
2062 			return req;
2063 		}
2064 	}
2065 
2066 	/* TODO: track list of outstanding requests in qpair? */
2067 	return NULL;
2068 }
2069 
2070 static void
2071 nvmf_ctrlr_abort_done(struct spdk_io_channel_iter *i, int status)
2072 {
2073 	struct spdk_nvmf_request *req = spdk_io_channel_iter_get_ctx(i);
2074 
2075 	spdk_nvmf_request_complete(req);
2076 }
2077 
2078 static void
2079 nvmf_ctrlr_abort_on_pg(struct spdk_io_channel_iter *i)
2080 {
2081 	struct spdk_nvmf_request *req = spdk_io_channel_iter_get_ctx(i);
2082 	struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
2083 	struct spdk_nvmf_poll_group *group = spdk_io_channel_get_ctx(ch);
2084 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
2085 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
2086 	uint16_t sqid = cmd->cdw10_bits.abort.sqid;
2087 	struct spdk_nvmf_qpair *qpair;
2088 
2089 	TAILQ_FOREACH(qpair, &group->qpairs, link) {
2090 		if (qpair->ctrlr == req->qpair->ctrlr && qpair->qid == sqid) {
2091 			struct spdk_nvmf_request *req_to_abort;
2092 			uint16_t cid = cmd->cdw10_bits.abort.cid;
2093 
2094 			/* Found the qpair */
2095 
2096 			req_to_abort = nvmf_qpair_abort(qpair, cid);
2097 			if (req_to_abort == NULL) {
2098 				SPDK_DEBUGLOG(SPDK_LOG_NVMF, "cid %u not found\n", cid);
2099 				rsp->status.sct = SPDK_NVME_SCT_GENERIC;
2100 				rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
2101 				spdk_for_each_channel_continue(i, -EINVAL);
2102 				return;
2103 			}
2104 
2105 			/* Complete the request with aborted status */
2106 			req_to_abort->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
2107 			req_to_abort->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_ABORTED_BY_REQUEST;
2108 			spdk_nvmf_request_complete(req_to_abort);
2109 
2110 			SPDK_DEBUGLOG(SPDK_LOG_NVMF, "abort ctrlr=%p req=%p sqid=%u cid=%u successful\n",
2111 				      qpair->ctrlr, req_to_abort, sqid, cid);
2112 			rsp->cdw0 = 0; /* Command successfully aborted */
2113 			rsp->status.sct = SPDK_NVME_SCT_GENERIC;
2114 			rsp->status.sc = SPDK_NVME_SC_SUCCESS;
2115 			/* Return -1 for the status so the iteration across threads stops. */
2116 			spdk_for_each_channel_continue(i, -1);
2117 
2118 		}
2119 	}
2120 
2121 	spdk_for_each_channel_continue(i, 0);
2122 }
2123 
2124 static int
2125 nvmf_ctrlr_abort(struct spdk_nvmf_request *req)
2126 {
2127 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
2128 
2129 	rsp->cdw0 = 1; /* Command not aborted */
2130 	rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
2131 	rsp->status.sc = SPDK_NVME_SC_INVALID_QUEUE_IDENTIFIER;
2132 
2133 	/* Send a message to each poll group, searching for this ctrlr, sqid, and command. */
2134 	spdk_for_each_channel(req->qpair->ctrlr->subsys->tgt,
2135 			      nvmf_ctrlr_abort_on_pg,
2136 			      req,
2137 			      nvmf_ctrlr_abort_done
2138 			     );
2139 
2140 	return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
2141 }
2142 
2143 static int
2144 get_features_generic(struct spdk_nvmf_request *req, uint32_t cdw0)
2145 {
2146 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
2147 
2148 	rsp->cdw0 = cdw0;
2149 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2150 }
2151 
2152 static int
2153 nvmf_ctrlr_get_features(struct spdk_nvmf_request *req)
2154 {
2155 	uint8_t feature;
2156 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
2157 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
2158 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
2159 
2160 	feature = cmd->cdw10_bits.get_features.fid;
2161 	switch (feature) {
2162 	case SPDK_NVME_FEAT_ARBITRATION:
2163 		return get_features_generic(req, ctrlr->feat.arbitration.raw);
2164 	case SPDK_NVME_FEAT_POWER_MANAGEMENT:
2165 		return get_features_generic(req, ctrlr->feat.power_management.raw);
2166 	case SPDK_NVME_FEAT_TEMPERATURE_THRESHOLD:
2167 		return nvmf_ctrlr_get_features_temperature_threshold(req);
2168 	case SPDK_NVME_FEAT_ERROR_RECOVERY:
2169 		return get_features_generic(req, ctrlr->feat.error_recovery.raw);
2170 	case SPDK_NVME_FEAT_VOLATILE_WRITE_CACHE:
2171 		return get_features_generic(req, ctrlr->feat.volatile_write_cache.raw);
2172 	case SPDK_NVME_FEAT_NUMBER_OF_QUEUES:
2173 		return get_features_generic(req, ctrlr->feat.number_of_queues.raw);
2174 	case SPDK_NVME_FEAT_WRITE_ATOMICITY:
2175 		return get_features_generic(req, ctrlr->feat.write_atomicity.raw);
2176 	case SPDK_NVME_FEAT_ASYNC_EVENT_CONFIGURATION:
2177 		return get_features_generic(req, ctrlr->feat.async_event_configuration.raw);
2178 	case SPDK_NVME_FEAT_KEEP_ALIVE_TIMER:
2179 		return get_features_generic(req, ctrlr->feat.keep_alive_timer.raw);
2180 	case SPDK_NVME_FEAT_HOST_IDENTIFIER:
2181 		return nvmf_ctrlr_get_features_host_identifier(req);
2182 	case SPDK_NVME_FEAT_HOST_RESERVE_MASK:
2183 		return nvmf_ctrlr_get_features_reservation_notification_mask(req);
2184 	case SPDK_NVME_FEAT_HOST_RESERVE_PERSIST:
2185 		return nvmf_ctrlr_get_features_reservation_persistence(req);
2186 	default:
2187 		SPDK_ERRLOG("Get Features command with unsupported feature ID 0x%02x\n", feature);
2188 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
2189 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2190 	}
2191 }
2192 
2193 static int
2194 nvmf_ctrlr_set_features(struct spdk_nvmf_request *req)
2195 {
2196 	uint8_t feature, save;
2197 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
2198 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
2199 
2200 	/*
2201 	 * Features are not saveable by the controller as indicated by
2202 	 * ONCS field of the Identify Controller data.
2203 	 * */
2204 	save = cmd->cdw10_bits.set_features.sv;
2205 	if (save) {
2206 		response->status.sc = SPDK_NVME_SC_FEATURE_ID_NOT_SAVEABLE;
2207 		response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
2208 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2209 	}
2210 
2211 	feature = cmd->cdw10_bits.set_features.fid;
2212 	switch (feature) {
2213 	case SPDK_NVME_FEAT_ARBITRATION:
2214 		return nvmf_ctrlr_set_features_arbitration(req);
2215 	case SPDK_NVME_FEAT_POWER_MANAGEMENT:
2216 		return nvmf_ctrlr_set_features_power_management(req);
2217 	case SPDK_NVME_FEAT_TEMPERATURE_THRESHOLD:
2218 		return nvmf_ctrlr_set_features_temperature_threshold(req);
2219 	case SPDK_NVME_FEAT_ERROR_RECOVERY:
2220 		return nvmf_ctrlr_set_features_error_recovery(req);
2221 	case SPDK_NVME_FEAT_VOLATILE_WRITE_CACHE:
2222 		return nvmf_ctrlr_set_features_volatile_write_cache(req);
2223 	case SPDK_NVME_FEAT_NUMBER_OF_QUEUES:
2224 		return nvmf_ctrlr_set_features_number_of_queues(req);
2225 	case SPDK_NVME_FEAT_WRITE_ATOMICITY:
2226 		return nvmf_ctrlr_set_features_write_atomicity(req);
2227 	case SPDK_NVME_FEAT_ASYNC_EVENT_CONFIGURATION:
2228 		return nvmf_ctrlr_set_features_async_event_configuration(req);
2229 	case SPDK_NVME_FEAT_KEEP_ALIVE_TIMER:
2230 		return nvmf_ctrlr_set_features_keep_alive_timer(req);
2231 	case SPDK_NVME_FEAT_HOST_IDENTIFIER:
2232 		return nvmf_ctrlr_set_features_host_identifier(req);
2233 	case SPDK_NVME_FEAT_HOST_RESERVE_MASK:
2234 		return nvmf_ctrlr_set_features_reservation_notification_mask(req);
2235 	case SPDK_NVME_FEAT_HOST_RESERVE_PERSIST:
2236 		return nvmf_ctrlr_set_features_reservation_persistence(req);
2237 	default:
2238 		SPDK_ERRLOG("Set Features command with unsupported feature ID 0x%02x\n", feature);
2239 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
2240 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2241 	}
2242 }
2243 
2244 static int
2245 nvmf_ctrlr_keep_alive(struct spdk_nvmf_request *req)
2246 {
2247 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
2248 
2249 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Keep Alive\n");
2250 	/*
2251 	 * To handle keep alive just clear or reset the
2252 	 * ctrlr based keep alive duration counter.
2253 	 * When added, a separate timer based process
2254 	 * will monitor if the time since last recorded
2255 	 * keep alive has exceeded the max duration and
2256 	 * take appropriate action.
2257 	 */
2258 	ctrlr->last_keep_alive_tick = spdk_get_ticks();
2259 
2260 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2261 }
2262 
2263 int
2264 nvmf_ctrlr_process_admin_cmd(struct spdk_nvmf_request *req)
2265 {
2266 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
2267 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
2268 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
2269 	int rc;
2270 
2271 	if (ctrlr == NULL) {
2272 		SPDK_ERRLOG("Admin command sent before CONNECT\n");
2273 		response->status.sct = SPDK_NVME_SCT_GENERIC;
2274 		response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
2275 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2276 	}
2277 
2278 	if (ctrlr->vcprop.cc.bits.en != 1) {
2279 		SPDK_ERRLOG("Admin command sent to disabled controller\n");
2280 		response->status.sct = SPDK_NVME_SCT_GENERIC;
2281 		response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
2282 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2283 	}
2284 
2285 	if (req->data && spdk_nvme_opc_get_data_transfer(cmd->opc) == SPDK_NVME_DATA_CONTROLLER_TO_HOST) {
2286 		memset(req->data, 0, req->length);
2287 	}
2288 
2289 	if (ctrlr->subsys->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) {
2290 		/* Discovery controllers only support Get Log Page, Identify and Keep Alive. */
2291 		switch (cmd->opc) {
2292 		case SPDK_NVME_OPC_IDENTIFY:
2293 		case SPDK_NVME_OPC_GET_LOG_PAGE:
2294 		case SPDK_NVME_OPC_KEEP_ALIVE:
2295 			break;
2296 		default:
2297 			goto invalid_opcode;
2298 		}
2299 	}
2300 
2301 	if (g_nvmf_custom_admin_cmd_hdlrs[cmd->opc].hdlr) {
2302 		rc = g_nvmf_custom_admin_cmd_hdlrs[cmd->opc].hdlr(req);
2303 		if (rc >= SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE) {
2304 			/* The handler took care of this commmand */
2305 			return rc;
2306 		}
2307 	}
2308 
2309 	switch (cmd->opc) {
2310 	case SPDK_NVME_OPC_GET_LOG_PAGE:
2311 		return nvmf_ctrlr_get_log_page(req);
2312 	case SPDK_NVME_OPC_IDENTIFY:
2313 		return nvmf_ctrlr_identify(req);
2314 	case SPDK_NVME_OPC_ABORT:
2315 		return nvmf_ctrlr_abort(req);
2316 	case SPDK_NVME_OPC_GET_FEATURES:
2317 		return nvmf_ctrlr_get_features(req);
2318 	case SPDK_NVME_OPC_SET_FEATURES:
2319 		return nvmf_ctrlr_set_features(req);
2320 	case SPDK_NVME_OPC_ASYNC_EVENT_REQUEST:
2321 		return nvmf_ctrlr_async_event_request(req);
2322 	case SPDK_NVME_OPC_KEEP_ALIVE:
2323 		return nvmf_ctrlr_keep_alive(req);
2324 
2325 	case SPDK_NVME_OPC_CREATE_IO_SQ:
2326 	case SPDK_NVME_OPC_CREATE_IO_CQ:
2327 	case SPDK_NVME_OPC_DELETE_IO_SQ:
2328 	case SPDK_NVME_OPC_DELETE_IO_CQ:
2329 		/* Create and Delete I/O CQ/SQ not allowed in NVMe-oF */
2330 		goto invalid_opcode;
2331 
2332 	default:
2333 		goto invalid_opcode;
2334 	}
2335 
2336 invalid_opcode:
2337 	SPDK_ERRLOG("Unsupported admin opcode 0x%x\n", cmd->opc);
2338 	response->status.sct = SPDK_NVME_SCT_GENERIC;
2339 	response->status.sc = SPDK_NVME_SC_INVALID_OPCODE;
2340 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2341 }
2342 
2343 int
2344 nvmf_ctrlr_process_fabrics_cmd(struct spdk_nvmf_request *req)
2345 {
2346 	struct spdk_nvmf_qpair *qpair = req->qpair;
2347 	struct spdk_nvmf_capsule_cmd *cap_hdr;
2348 
2349 	cap_hdr = &req->cmd->nvmf_cmd;
2350 
2351 	if (qpair->ctrlr == NULL) {
2352 		/* No ctrlr established yet; the only valid command is Connect */
2353 		if (cap_hdr->fctype == SPDK_NVMF_FABRIC_COMMAND_CONNECT) {
2354 			return nvmf_ctrlr_cmd_connect(req);
2355 		} else {
2356 			SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Got fctype 0x%x, expected Connect\n",
2357 				      cap_hdr->fctype);
2358 			req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
2359 			req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
2360 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2361 		}
2362 	} else if (nvmf_qpair_is_admin_queue(qpair)) {
2363 		/*
2364 		 * Controller session is established, and this is an admin queue.
2365 		 * Disallow Connect and allow other fabrics commands.
2366 		 */
2367 		switch (cap_hdr->fctype) {
2368 		case SPDK_NVMF_FABRIC_COMMAND_PROPERTY_SET:
2369 			return nvmf_property_set(req);
2370 		case SPDK_NVMF_FABRIC_COMMAND_PROPERTY_GET:
2371 			return nvmf_property_get(req);
2372 		default:
2373 			SPDK_DEBUGLOG(SPDK_LOG_NVMF, "unknown fctype 0x%02x\n",
2374 				      cap_hdr->fctype);
2375 			req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
2376 			req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_INVALID_OPCODE;
2377 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2378 		}
2379 	} else {
2380 		/* Controller session is established, and this is an I/O queue */
2381 		/* For now, no I/O-specific Fabrics commands are implemented (other than Connect) */
2382 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Unexpected I/O fctype 0x%x\n", cap_hdr->fctype);
2383 		req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
2384 		req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_INVALID_OPCODE;
2385 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2386 	}
2387 }
2388 
2389 int
2390 nvmf_ctrlr_async_event_ns_notice(struct spdk_nvmf_ctrlr *ctrlr)
2391 {
2392 	struct spdk_nvmf_request *req;
2393 	struct spdk_nvme_cpl *rsp;
2394 	union spdk_nvme_async_event_completion event = {0};
2395 
2396 	/* Users may disable the event notification */
2397 	if (!ctrlr->feat.async_event_configuration.bits.ns_attr_notice) {
2398 		return 0;
2399 	}
2400 
2401 	event.bits.async_event_type = SPDK_NVME_ASYNC_EVENT_TYPE_NOTICE;
2402 	event.bits.async_event_info = SPDK_NVME_ASYNC_EVENT_NS_ATTR_CHANGED;
2403 	event.bits.log_page_identifier = SPDK_NVME_LOG_CHANGED_NS_LIST;
2404 
2405 	/* If there is no outstanding AER request, queue the event.  Then
2406 	 * if an AER is later submitted, this event can be sent as a
2407 	 * response.
2408 	 */
2409 	if (!ctrlr->aer_req) {
2410 		if (ctrlr->notice_event.bits.async_event_type ==
2411 		    SPDK_NVME_ASYNC_EVENT_TYPE_NOTICE) {
2412 			return 0;
2413 		}
2414 
2415 		ctrlr->notice_event.raw = event.raw;
2416 		return 0;
2417 	}
2418 
2419 	req = ctrlr->aer_req;
2420 	rsp = &req->rsp->nvme_cpl;
2421 
2422 	rsp->cdw0 = event.raw;
2423 
2424 	spdk_nvmf_request_complete(req);
2425 	ctrlr->aer_req = NULL;
2426 
2427 	return 0;
2428 }
2429 
2430 void
2431 nvmf_ctrlr_async_event_reservation_notification(struct spdk_nvmf_ctrlr *ctrlr)
2432 {
2433 	struct spdk_nvmf_request *req;
2434 	struct spdk_nvme_cpl *rsp;
2435 	union spdk_nvme_async_event_completion event = {0};
2436 
2437 	if (!ctrlr->num_avail_log_pages) {
2438 		return;
2439 	}
2440 	event.bits.async_event_type = SPDK_NVME_ASYNC_EVENT_TYPE_IO;
2441 	event.bits.async_event_info = SPDK_NVME_ASYNC_EVENT_RESERVATION_LOG_AVAIL;
2442 	event.bits.log_page_identifier = SPDK_NVME_LOG_RESERVATION_NOTIFICATION;
2443 
2444 	/* If there is no outstanding AER request, queue the event.  Then
2445 	 * if an AER is later submitted, this event can be sent as a
2446 	 * response.
2447 	 */
2448 	if (!ctrlr->aer_req) {
2449 		if (ctrlr->reservation_event.bits.async_event_type ==
2450 		    SPDK_NVME_ASYNC_EVENT_TYPE_IO) {
2451 			return;
2452 		}
2453 
2454 		ctrlr->reservation_event.raw = event.raw;
2455 		return;
2456 	}
2457 
2458 	req = ctrlr->aer_req;
2459 	rsp = &req->rsp->nvme_cpl;
2460 
2461 	rsp->cdw0 = event.raw;
2462 
2463 	spdk_nvmf_request_complete(req);
2464 	ctrlr->aer_req = NULL;
2465 }
2466 
2467 void
2468 nvmf_qpair_free_aer(struct spdk_nvmf_qpair *qpair)
2469 {
2470 	struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr;
2471 
2472 	if (!nvmf_qpair_is_admin_queue(qpair)) {
2473 		return;
2474 	}
2475 
2476 	if (ctrlr->aer_req != NULL) {
2477 		spdk_nvmf_request_free(ctrlr->aer_req);
2478 		ctrlr->aer_req = NULL;
2479 	}
2480 }
2481 
2482 void
2483 nvmf_ctrlr_abort_aer(struct spdk_nvmf_ctrlr *ctrlr)
2484 {
2485 	if (!ctrlr->aer_req) {
2486 		return;
2487 	}
2488 
2489 	spdk_nvmf_request_complete(ctrlr->aer_req);
2490 	ctrlr->aer_req = NULL;
2491 }
2492 
2493 static void
2494 _nvmf_ctrlr_add_reservation_log(void *ctx)
2495 {
2496 	struct spdk_nvmf_reservation_log *log = (struct spdk_nvmf_reservation_log *)ctx;
2497 	struct spdk_nvmf_ctrlr *ctrlr = log->ctrlr;
2498 
2499 	ctrlr->log_page_count++;
2500 
2501 	/* Maximum number of queued log pages is 255 */
2502 	if (ctrlr->num_avail_log_pages == 0xff) {
2503 		struct spdk_nvmf_reservation_log *entry;
2504 		entry = TAILQ_LAST(&ctrlr->log_head, log_page_head);
2505 		entry->log.log_page_count = ctrlr->log_page_count;
2506 		free(log);
2507 		return;
2508 	}
2509 
2510 	log->log.log_page_count = ctrlr->log_page_count;
2511 	log->log.num_avail_log_pages = ctrlr->num_avail_log_pages++;
2512 	TAILQ_INSERT_TAIL(&ctrlr->log_head, log, link);
2513 
2514 	nvmf_ctrlr_async_event_reservation_notification(ctrlr);
2515 }
2516 
2517 void
2518 nvmf_ctrlr_reservation_notice_log(struct spdk_nvmf_ctrlr *ctrlr,
2519 				  struct spdk_nvmf_ns *ns,
2520 				  enum spdk_nvme_reservation_notification_log_page_type type)
2521 {
2522 	struct spdk_nvmf_reservation_log *log;
2523 
2524 	switch (type) {
2525 	case SPDK_NVME_RESERVATION_LOG_PAGE_EMPTY:
2526 		return;
2527 	case SPDK_NVME_REGISTRATION_PREEMPTED:
2528 		if (ns->mask & SPDK_NVME_REGISTRATION_PREEMPTED_MASK) {
2529 			return;
2530 		}
2531 		break;
2532 	case SPDK_NVME_RESERVATION_RELEASED:
2533 		if (ns->mask & SPDK_NVME_RESERVATION_RELEASED_MASK) {
2534 			return;
2535 		}
2536 		break;
2537 	case SPDK_NVME_RESERVATION_PREEMPTED:
2538 		if (ns->mask & SPDK_NVME_RESERVATION_PREEMPTED_MASK) {
2539 			return;
2540 		}
2541 		break;
2542 	default:
2543 		return;
2544 	}
2545 
2546 	log = calloc(1, sizeof(*log));
2547 	if (!log) {
2548 		SPDK_ERRLOG("Alloc log page failed, ignore the log\n");
2549 		return;
2550 	}
2551 	log->ctrlr = ctrlr;
2552 	log->log.type = type;
2553 	log->log.nsid = ns->nsid;
2554 
2555 	spdk_thread_send_msg(ctrlr->thread, _nvmf_ctrlr_add_reservation_log, log);
2556 }
2557 
2558 /* Check from subsystem poll group's namespace information data structure */
2559 static bool
2560 nvmf_ns_info_ctrlr_is_registrant(struct spdk_nvmf_subsystem_pg_ns_info *ns_info,
2561 				 struct spdk_nvmf_ctrlr *ctrlr)
2562 {
2563 	uint32_t i;
2564 
2565 	for (i = 0; i < SPDK_NVMF_MAX_NUM_REGISTRANTS; i++) {
2566 		if (!spdk_uuid_compare(&ns_info->reg_hostid[i], &ctrlr->hostid)) {
2567 			return true;
2568 		}
2569 	}
2570 
2571 	return false;
2572 }
2573 
2574 /*
2575  * Check the NVMe command is permitted or not for current controller(Host).
2576  */
2577 static int
2578 nvmf_ns_reservation_request_check(struct spdk_nvmf_subsystem_pg_ns_info *ns_info,
2579 				  struct spdk_nvmf_ctrlr *ctrlr,
2580 				  struct spdk_nvmf_request *req)
2581 {
2582 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
2583 	enum spdk_nvme_reservation_type rtype = ns_info->rtype;
2584 	uint8_t status = SPDK_NVME_SC_SUCCESS;
2585 	uint8_t racqa;
2586 	bool is_registrant;
2587 
2588 	/* No valid reservation */
2589 	if (!rtype) {
2590 		return 0;
2591 	}
2592 
2593 	is_registrant = nvmf_ns_info_ctrlr_is_registrant(ns_info, ctrlr);
2594 	/* All registrants type and current ctrlr is a valid registrant */
2595 	if ((rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_ALL_REGS ||
2596 	     rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_ALL_REGS) && is_registrant) {
2597 		return 0;
2598 	} else if (!spdk_uuid_compare(&ns_info->holder_id, &ctrlr->hostid)) {
2599 		return 0;
2600 	}
2601 
2602 	/* Non-holder for current controller */
2603 	switch (cmd->opc) {
2604 	case SPDK_NVME_OPC_READ:
2605 	case SPDK_NVME_OPC_COMPARE:
2606 		if (rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS) {
2607 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2608 			goto exit;
2609 		}
2610 		if ((rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_REG_ONLY ||
2611 		     rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_ALL_REGS) && !is_registrant) {
2612 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2613 		}
2614 		break;
2615 	case SPDK_NVME_OPC_FLUSH:
2616 	case SPDK_NVME_OPC_WRITE:
2617 	case SPDK_NVME_OPC_WRITE_UNCORRECTABLE:
2618 	case SPDK_NVME_OPC_WRITE_ZEROES:
2619 	case SPDK_NVME_OPC_DATASET_MANAGEMENT:
2620 		if (rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE ||
2621 		    rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS) {
2622 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2623 			goto exit;
2624 		}
2625 		if (!is_registrant) {
2626 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2627 		}
2628 		break;
2629 	case SPDK_NVME_OPC_RESERVATION_ACQUIRE:
2630 		racqa = cmd->cdw10_bits.resv_acquire.racqa;
2631 		if (racqa == SPDK_NVME_RESERVE_ACQUIRE) {
2632 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2633 			goto exit;
2634 		}
2635 		if (!is_registrant) {
2636 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2637 		}
2638 		break;
2639 	case SPDK_NVME_OPC_RESERVATION_RELEASE:
2640 		if (!is_registrant) {
2641 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2642 		}
2643 		break;
2644 	default:
2645 		break;
2646 	}
2647 
2648 exit:
2649 	req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
2650 	req->rsp->nvme_cpl.status.sc = status;
2651 	if (status == SPDK_NVME_SC_RESERVATION_CONFLICT) {
2652 		return -EPERM;
2653 	}
2654 
2655 	return 0;
2656 }
2657 
2658 static int
2659 nvmf_ctrlr_process_io_fused_cmd(struct spdk_nvmf_request *req, struct spdk_bdev *bdev,
2660 				struct spdk_bdev_desc *desc, struct spdk_io_channel *ch)
2661 {
2662 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
2663 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
2664 	struct spdk_nvmf_request *first_fused_req = req->qpair->first_fused_req;
2665 	int rc;
2666 
2667 	if (cmd->fuse == SPDK_NVME_CMD_FUSE_FIRST) {
2668 		/* first fused operation (should be compare) */
2669 		if (first_fused_req != NULL) {
2670 			struct spdk_nvme_cpl *fused_response = &first_fused_req->rsp->nvme_cpl;
2671 
2672 			SPDK_ERRLOG("Wrong sequence of fused operations\n");
2673 
2674 			/* abort req->qpair->first_fused_request and continue with new fused command */
2675 			fused_response->status.sc = SPDK_NVME_SC_ABORTED_MISSING_FUSED;
2676 			fused_response->status.sct = SPDK_NVME_SCT_GENERIC;
2677 			spdk_nvmf_request_complete(first_fused_req);
2678 		} else if (cmd->opc != SPDK_NVME_OPC_COMPARE) {
2679 			SPDK_ERRLOG("Wrong op code of fused operations\n");
2680 			rsp->status.sct = SPDK_NVME_SCT_GENERIC;
2681 			rsp->status.sc = SPDK_NVME_SC_INVALID_OPCODE;
2682 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2683 		}
2684 
2685 		req->qpair->first_fused_req = req;
2686 		return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
2687 	} else if (cmd->fuse == SPDK_NVME_CMD_FUSE_SECOND) {
2688 		/* second fused operation (should be write) */
2689 		if (first_fused_req == NULL) {
2690 			SPDK_ERRLOG("Wrong sequence of fused operations\n");
2691 			rsp->status.sct = SPDK_NVME_SCT_GENERIC;
2692 			rsp->status.sc = SPDK_NVME_SC_ABORTED_MISSING_FUSED;
2693 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2694 		} else if (cmd->opc != SPDK_NVME_OPC_WRITE) {
2695 			struct spdk_nvme_cpl *fused_response = &first_fused_req->rsp->nvme_cpl;
2696 
2697 			SPDK_ERRLOG("Wrong op code of fused operations\n");
2698 
2699 			/* abort req->qpair->first_fused_request and fail current command */
2700 			fused_response->status.sc = SPDK_NVME_SC_ABORTED_MISSING_FUSED;
2701 			fused_response->status.sct = SPDK_NVME_SCT_GENERIC;
2702 			spdk_nvmf_request_complete(first_fused_req);
2703 
2704 			rsp->status.sct = SPDK_NVME_SCT_GENERIC;
2705 			rsp->status.sc = SPDK_NVME_SC_INVALID_OPCODE;
2706 			req->qpair->first_fused_req = NULL;
2707 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2708 		}
2709 
2710 		/* save request of first command to generate response later */
2711 		req->first_fused_req = first_fused_req;
2712 		req->qpair->first_fused_req = NULL;
2713 	} else {
2714 		SPDK_ERRLOG("Invalid fused command fuse field.\n");
2715 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
2716 		rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
2717 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2718 	}
2719 
2720 	rc = nvmf_bdev_ctrlr_compare_and_write_cmd(bdev, desc, ch, req->first_fused_req, req);
2721 
2722 	if (rc == SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE) {
2723 		if (spdk_nvme_cpl_is_error(rsp)) {
2724 			struct spdk_nvme_cpl *fused_response = &first_fused_req->rsp->nvme_cpl;
2725 
2726 			fused_response->status = rsp->status;
2727 			rsp->status.sct = SPDK_NVME_SCT_GENERIC;
2728 			rsp->status.sc = SPDK_NVME_SC_ABORTED_FAILED_FUSED;
2729 			/* Complete first of fused commands. Second will be completed by upper layer */
2730 			spdk_nvmf_request_complete(first_fused_req);
2731 			req->first_fused_req = NULL;
2732 		}
2733 	}
2734 
2735 	return rc;
2736 }
2737 
2738 int
2739 nvmf_ctrlr_process_io_cmd(struct spdk_nvmf_request *req)
2740 {
2741 	uint32_t nsid;
2742 	struct spdk_nvmf_ns *ns;
2743 	struct spdk_bdev *bdev;
2744 	struct spdk_bdev_desc *desc;
2745 	struct spdk_io_channel *ch;
2746 	struct spdk_nvmf_poll_group *group = req->qpair->group;
2747 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
2748 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
2749 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
2750 	struct spdk_nvmf_subsystem_pg_ns_info *ns_info;
2751 
2752 	/* pre-set response details for this command */
2753 	response->status.sc = SPDK_NVME_SC_SUCCESS;
2754 	nsid = cmd->nsid;
2755 
2756 	if (spdk_unlikely(ctrlr == NULL)) {
2757 		SPDK_ERRLOG("I/O command sent before CONNECT\n");
2758 		response->status.sct = SPDK_NVME_SCT_GENERIC;
2759 		response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
2760 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2761 	}
2762 
2763 	if (spdk_unlikely(ctrlr->vcprop.cc.bits.en != 1)) {
2764 		SPDK_ERRLOG("I/O command sent to disabled controller\n");
2765 		response->status.sct = SPDK_NVME_SCT_GENERIC;
2766 		response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
2767 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2768 	}
2769 
2770 	ns = _nvmf_subsystem_get_ns(ctrlr->subsys, nsid);
2771 	if (ns == NULL || ns->bdev == NULL) {
2772 		SPDK_ERRLOG("Unsuccessful query for nsid %u\n", cmd->nsid);
2773 		response->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT;
2774 		response->status.dnr = 1;
2775 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2776 	}
2777 
2778 	/* scan-build falsely reporting dereference of null pointer */
2779 	assert(group != NULL && group->sgroups != NULL);
2780 	ns_info = &group->sgroups[ctrlr->subsys->id].ns_info[nsid - 1];
2781 	if (nvmf_ns_reservation_request_check(ns_info, ctrlr, req)) {
2782 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Reservation Conflict for nsid %u, opcode %u\n",
2783 			      cmd->nsid, cmd->opc);
2784 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2785 	}
2786 
2787 	bdev = ns->bdev;
2788 	desc = ns->desc;
2789 	ch = ns_info->channel;
2790 
2791 	if (spdk_unlikely(cmd->fuse & SPDK_NVME_CMD_FUSE_MASK)) {
2792 		return nvmf_ctrlr_process_io_fused_cmd(req, bdev, desc, ch);
2793 	} else if (spdk_unlikely(req->qpair->first_fused_req != NULL)) {
2794 		struct spdk_nvme_cpl *fused_response = &req->qpair->first_fused_req->rsp->nvme_cpl;
2795 
2796 		SPDK_ERRLOG("Expected second of fused commands - failing first of fused commands\n");
2797 
2798 		/* abort req->qpair->first_fused_request and continue with new command */
2799 		fused_response->status.sc = SPDK_NVME_SC_ABORTED_MISSING_FUSED;
2800 		fused_response->status.sct = SPDK_NVME_SCT_GENERIC;
2801 		spdk_nvmf_request_complete(req->qpair->first_fused_req);
2802 		req->qpair->first_fused_req = NULL;
2803 	}
2804 
2805 	switch (cmd->opc) {
2806 	case SPDK_NVME_OPC_READ:
2807 		return nvmf_bdev_ctrlr_read_cmd(bdev, desc, ch, req);
2808 	case SPDK_NVME_OPC_WRITE:
2809 		return nvmf_bdev_ctrlr_write_cmd(bdev, desc, ch, req);
2810 	case SPDK_NVME_OPC_COMPARE:
2811 		return nvmf_bdev_ctrlr_compare_cmd(bdev, desc, ch, req);
2812 	case SPDK_NVME_OPC_WRITE_ZEROES:
2813 		return nvmf_bdev_ctrlr_write_zeroes_cmd(bdev, desc, ch, req);
2814 	case SPDK_NVME_OPC_FLUSH:
2815 		return nvmf_bdev_ctrlr_flush_cmd(bdev, desc, ch, req);
2816 	case SPDK_NVME_OPC_DATASET_MANAGEMENT:
2817 		return nvmf_bdev_ctrlr_dsm_cmd(bdev, desc, ch, req);
2818 	case SPDK_NVME_OPC_RESERVATION_REGISTER:
2819 	case SPDK_NVME_OPC_RESERVATION_ACQUIRE:
2820 	case SPDK_NVME_OPC_RESERVATION_RELEASE:
2821 	case SPDK_NVME_OPC_RESERVATION_REPORT:
2822 		spdk_thread_send_msg(ctrlr->subsys->thread, nvmf_ns_reservation_request, req);
2823 		return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
2824 	default:
2825 		return nvmf_bdev_ctrlr_nvme_passthru_io(bdev, desc, ch, req);
2826 	}
2827 }
2828 
2829 static void
2830 nvmf_qpair_request_cleanup(struct spdk_nvmf_qpair *qpair)
2831 {
2832 	if (qpair->state == SPDK_NVMF_QPAIR_DEACTIVATING) {
2833 		assert(qpair->state_cb != NULL);
2834 
2835 		if (TAILQ_EMPTY(&qpair->outstanding)) {
2836 			qpair->state_cb(qpair->state_cb_arg, 0);
2837 		}
2838 	}
2839 }
2840 
2841 int
2842 spdk_nvmf_request_free(struct spdk_nvmf_request *req)
2843 {
2844 	struct spdk_nvmf_qpair *qpair = req->qpair;
2845 
2846 	TAILQ_REMOVE(&qpair->outstanding, req, link);
2847 	if (spdk_nvmf_transport_req_free(req)) {
2848 		SPDK_ERRLOG("Unable to free transport level request resources.\n");
2849 	}
2850 
2851 	nvmf_qpair_request_cleanup(qpair);
2852 
2853 	return 0;
2854 }
2855 
2856 int
2857 spdk_nvmf_request_complete(struct spdk_nvmf_request *req)
2858 {
2859 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
2860 	struct spdk_nvmf_qpair *qpair;
2861 	struct spdk_nvmf_subsystem_poll_group *sgroup = NULL;
2862 	bool is_aer = false;
2863 
2864 	rsp->sqid = 0;
2865 	rsp->status.p = 0;
2866 	rsp->cid = req->cmd->nvme_cmd.cid;
2867 
2868 	qpair = req->qpair;
2869 	if (qpair->ctrlr) {
2870 		sgroup = &qpair->group->sgroups[qpair->ctrlr->subsys->id];
2871 		is_aer = qpair->ctrlr->aer_req == req;
2872 	} else if (spdk_unlikely(nvmf_request_is_fabric_connect(req))) {
2873 		sgroup = nvmf_subsystem_pg_from_connect_cmd(req);
2874 	}
2875 
2876 	SPDK_DEBUGLOG(SPDK_LOG_NVMF,
2877 		      "cpl: cdw0=0x%08x sct=0x%01x sc=0x%02x cid=0x%04x\n",
2878 		      rsp->cdw0, rsp->status.sct, rsp->status.sc, rsp->cid);
2879 
2880 	TAILQ_REMOVE(&qpair->outstanding, req, link);
2881 	if (spdk_nvmf_transport_req_complete(req)) {
2882 		SPDK_ERRLOG("Transport request completion error!\n");
2883 	}
2884 
2885 	/* AER cmd is an exception */
2886 	if (sgroup && !is_aer) {
2887 		assert(sgroup->io_outstanding > 0);
2888 		sgroup->io_outstanding--;
2889 		if (sgroup->state == SPDK_NVMF_SUBSYSTEM_PAUSING &&
2890 		    sgroup->io_outstanding == 0) {
2891 			sgroup->state = SPDK_NVMF_SUBSYSTEM_PAUSED;
2892 			sgroup->cb_fn(sgroup->cb_arg, 0);
2893 		}
2894 	}
2895 
2896 	nvmf_qpair_request_cleanup(qpair);
2897 
2898 	return 0;
2899 }
2900 
2901 static void
2902 nvmf_trace_command(union nvmf_h2c_msg *h2c_msg, bool is_admin_queue)
2903 {
2904 	struct spdk_nvmf_capsule_cmd *cap_hdr = &h2c_msg->nvmf_cmd;
2905 	struct spdk_nvme_cmd *cmd = &h2c_msg->nvme_cmd;
2906 	struct spdk_nvme_sgl_descriptor *sgl = &cmd->dptr.sgl1;
2907 	uint8_t opc;
2908 
2909 	if (cmd->opc == SPDK_NVME_OPC_FABRIC) {
2910 		opc = cap_hdr->fctype;
2911 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "%s Fabrics cmd: fctype 0x%02x cid %u\n",
2912 			      is_admin_queue ? "Admin" : "I/O",
2913 			      cap_hdr->fctype, cap_hdr->cid);
2914 	} else {
2915 		opc = cmd->opc;
2916 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "%s cmd: opc 0x%02x fuse %u cid %u nsid %u cdw10 0x%08x\n",
2917 			      is_admin_queue ? "Admin" : "I/O",
2918 			      cmd->opc, cmd->fuse, cmd->cid, cmd->nsid, cmd->cdw10);
2919 		if (cmd->mptr) {
2920 			SPDK_DEBUGLOG(SPDK_LOG_NVMF, "mptr 0x%" PRIx64 "\n", cmd->mptr);
2921 		}
2922 		if (cmd->psdt != SPDK_NVME_PSDT_SGL_MPTR_CONTIG &&
2923 		    cmd->psdt != SPDK_NVME_PSDT_SGL_MPTR_SGL) {
2924 			SPDK_DEBUGLOG(SPDK_LOG_NVMF, "psdt %u\n", cmd->psdt);
2925 		}
2926 	}
2927 
2928 	if (spdk_nvme_opc_get_data_transfer(opc) != SPDK_NVME_DATA_NONE) {
2929 		if (sgl->generic.type == SPDK_NVME_SGL_TYPE_KEYED_DATA_BLOCK) {
2930 			SPDK_DEBUGLOG(SPDK_LOG_NVMF,
2931 				      "SGL: Keyed%s: addr 0x%" PRIx64 " key 0x%x len 0x%x\n",
2932 				      sgl->generic.subtype == SPDK_NVME_SGL_SUBTYPE_INVALIDATE_KEY ? " (Inv)" : "",
2933 				      sgl->address, sgl->keyed.key, sgl->keyed.length);
2934 		} else if (sgl->generic.type == SPDK_NVME_SGL_TYPE_DATA_BLOCK) {
2935 			SPDK_DEBUGLOG(SPDK_LOG_NVMF, "SGL: Data block: %s 0x%" PRIx64 " len 0x%x\n",
2936 				      sgl->unkeyed.subtype == SPDK_NVME_SGL_SUBTYPE_OFFSET ? "offs" : "addr",
2937 				      sgl->address, sgl->unkeyed.length);
2938 		} else {
2939 			SPDK_DEBUGLOG(SPDK_LOG_NVMF, "SGL type 0x%x subtype 0x%x\n",
2940 				      sgl->generic.type, sgl->generic.subtype);
2941 		}
2942 	}
2943 }
2944 
2945 static void
2946 _nvmf_request_exec(struct spdk_nvmf_request *req,
2947 		   struct spdk_nvmf_subsystem_poll_group *sgroup)
2948 {
2949 	struct spdk_nvmf_qpair *qpair = req->qpair;
2950 	enum spdk_nvmf_request_exec_status status;
2951 
2952 	nvmf_trace_command(req->cmd, nvmf_qpair_is_admin_queue(qpair));
2953 
2954 	if (sgroup) {
2955 		sgroup->io_outstanding++;
2956 	}
2957 
2958 	/* Place the request on the outstanding list so we can keep track of it */
2959 	TAILQ_INSERT_TAIL(&qpair->outstanding, req, link);
2960 
2961 	if (spdk_unlikely(req->cmd->nvmf_cmd.opcode == SPDK_NVME_OPC_FABRIC)) {
2962 		status = nvmf_ctrlr_process_fabrics_cmd(req);
2963 	} else if (spdk_unlikely(nvmf_qpair_is_admin_queue(qpair))) {
2964 		status = nvmf_ctrlr_process_admin_cmd(req);
2965 	} else {
2966 		status = nvmf_ctrlr_process_io_cmd(req);
2967 	}
2968 
2969 	if (status == SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE) {
2970 		spdk_nvmf_request_complete(req);
2971 	}
2972 }
2973 
2974 void
2975 spdk_nvmf_request_exec_fabrics(struct spdk_nvmf_request *req)
2976 {
2977 	struct spdk_nvmf_qpair *qpair = req->qpair;
2978 	struct spdk_nvmf_subsystem_poll_group *sgroup = NULL;
2979 
2980 	assert(req->cmd->nvmf_cmd.opcode == SPDK_NVME_OPC_FABRIC);
2981 
2982 	if (qpair->ctrlr) {
2983 		sgroup = &qpair->group->sgroups[qpair->ctrlr->subsys->id];
2984 	} else {
2985 		sgroup = nvmf_subsystem_pg_from_connect_cmd(req);
2986 	}
2987 
2988 	_nvmf_request_exec(req, sgroup);
2989 }
2990 
2991 void
2992 spdk_nvmf_request_exec(struct spdk_nvmf_request *req)
2993 {
2994 	struct spdk_nvmf_qpair *qpair = req->qpair;
2995 	struct spdk_nvmf_subsystem_poll_group *sgroup = NULL;
2996 
2997 	if (qpair->ctrlr) {
2998 		sgroup = &qpair->group->sgroups[qpair->ctrlr->subsys->id];
2999 	} else if (spdk_unlikely(nvmf_request_is_fabric_connect(req))) {
3000 		sgroup = nvmf_subsystem_pg_from_connect_cmd(req);
3001 	}
3002 
3003 	if (qpair->state != SPDK_NVMF_QPAIR_ACTIVE) {
3004 		req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
3005 		req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
3006 		/* Place the request on the outstanding list so we can keep track of it */
3007 		TAILQ_INSERT_TAIL(&qpair->outstanding, req, link);
3008 		/* Still increment io_outstanding because request_complete decrements it */
3009 		if (sgroup != NULL) {
3010 			sgroup->io_outstanding++;
3011 		}
3012 		spdk_nvmf_request_complete(req);
3013 		return;
3014 	}
3015 
3016 	/* Check if the subsystem is paused (if there is a subsystem) */
3017 	if (sgroup != NULL) {
3018 		if (sgroup->state != SPDK_NVMF_SUBSYSTEM_ACTIVE) {
3019 			/* The subsystem is not currently active. Queue this request. */
3020 			TAILQ_INSERT_TAIL(&sgroup->queued, req, link);
3021 			return;
3022 		}
3023 	}
3024 
3025 	_nvmf_request_exec(req, sgroup);
3026 }
3027 
3028 static bool
3029 nvmf_ctrlr_get_dif_ctx(struct spdk_nvmf_ctrlr *ctrlr, struct spdk_nvme_cmd *cmd,
3030 		       struct spdk_dif_ctx *dif_ctx)
3031 {
3032 	struct spdk_nvmf_ns *ns;
3033 	struct spdk_bdev *bdev;
3034 
3035 	if (ctrlr == NULL || cmd == NULL) {
3036 		return false;
3037 	}
3038 
3039 	ns = _nvmf_subsystem_get_ns(ctrlr->subsys, cmd->nsid);
3040 	if (ns == NULL || ns->bdev == NULL) {
3041 		return false;
3042 	}
3043 
3044 	bdev = ns->bdev;
3045 
3046 	switch (cmd->opc) {
3047 	case SPDK_NVME_OPC_READ:
3048 	case SPDK_NVME_OPC_WRITE:
3049 	case SPDK_NVME_OPC_COMPARE:
3050 		return nvmf_bdev_ctrlr_get_dif_ctx(bdev, cmd, dif_ctx);
3051 	default:
3052 		break;
3053 	}
3054 
3055 	return false;
3056 }
3057 
3058 bool
3059 spdk_nvmf_request_get_dif_ctx(struct spdk_nvmf_request *req, struct spdk_dif_ctx *dif_ctx)
3060 {
3061 	struct spdk_nvmf_qpair *qpair = req->qpair;
3062 	struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr;
3063 
3064 	if (spdk_likely(ctrlr == NULL || !ctrlr->dif_insert_or_strip)) {
3065 		return false;
3066 	}
3067 
3068 	if (spdk_unlikely(qpair->state != SPDK_NVMF_QPAIR_ACTIVE)) {
3069 		return false;
3070 	}
3071 
3072 	if (spdk_unlikely(req->cmd->nvmf_cmd.opcode == SPDK_NVME_OPC_FABRIC)) {
3073 		return false;
3074 	}
3075 
3076 	if (spdk_unlikely(nvmf_qpair_is_admin_queue(qpair))) {
3077 		return false;
3078 	}
3079 
3080 	return nvmf_ctrlr_get_dif_ctx(ctrlr, &req->cmd->nvme_cmd, dif_ctx);
3081 }
3082 
3083 void
3084 spdk_nvmf_set_custom_admin_cmd_hdlr(uint8_t opc, spdk_nvmf_custom_cmd_hdlr hdlr)
3085 {
3086 	g_nvmf_custom_admin_cmd_hdlrs[opc].hdlr = hdlr;
3087 }
3088 
3089 static int
3090 nvmf_passthru_admin_cmd(struct spdk_nvmf_request *req)
3091 {
3092 	struct spdk_bdev *bdev;
3093 	struct spdk_bdev_desc *desc;
3094 	struct spdk_io_channel *ch;
3095 	struct spdk_nvme_cmd *cmd = spdk_nvmf_request_get_cmd(req);
3096 	struct spdk_nvme_cpl *response = spdk_nvmf_request_get_response(req);
3097 	uint32_t bdev_nsid;
3098 	int rc;
3099 
3100 	if (g_nvmf_custom_admin_cmd_hdlrs[cmd->opc].nsid == 0) {
3101 		bdev_nsid = cmd->nsid;
3102 	} else {
3103 		bdev_nsid = g_nvmf_custom_admin_cmd_hdlrs[cmd->opc].nsid;
3104 	}
3105 
3106 	rc = spdk_nvmf_request_get_bdev(bdev_nsid, req, &bdev, &desc, &ch);
3107 	if (rc) {
3108 		response->status.sct = SPDK_NVME_SCT_GENERIC;
3109 		response->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT;
3110 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
3111 	}
3112 	return spdk_nvmf_bdev_ctrlr_nvme_passthru_admin(bdev, desc, ch, req, NULL);
3113 }
3114 
3115 void
3116 spdk_nvmf_set_passthru_admin_cmd(uint8_t opc, uint32_t forward_nsid)
3117 {
3118 	g_nvmf_custom_admin_cmd_hdlrs[opc].hdlr = nvmf_passthru_admin_cmd;
3119 	g_nvmf_custom_admin_cmd_hdlrs[opc].nsid = forward_nsid;
3120 }
3121 
3122 int
3123 spdk_nvmf_request_get_bdev(uint32_t nsid, struct spdk_nvmf_request *req,
3124 			   struct spdk_bdev **bdev, struct spdk_bdev_desc **desc, struct spdk_io_channel **ch)
3125 {
3126 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
3127 	struct spdk_nvmf_ns *ns;
3128 	struct spdk_nvmf_poll_group *group = req->qpair->group;
3129 	struct spdk_nvmf_subsystem_pg_ns_info *ns_info;
3130 
3131 	*bdev = NULL;
3132 	*desc = NULL;
3133 	*ch = NULL;
3134 
3135 	ns = _nvmf_subsystem_get_ns(ctrlr->subsys, nsid);
3136 	if (ns == NULL || ns->bdev == NULL) {
3137 		return -EINVAL;
3138 	}
3139 
3140 	assert(group != NULL && group->sgroups != NULL);
3141 	ns_info = &group->sgroups[ctrlr->subsys->id].ns_info[nsid - 1];
3142 	*bdev = ns->bdev;
3143 	*desc = ns->desc;
3144 	*ch = ns_info->channel;
3145 
3146 	return 0;
3147 }
3148 
3149 struct spdk_nvmf_ctrlr *spdk_nvmf_request_get_ctrlr(struct spdk_nvmf_request *req)
3150 {
3151 	return req->qpair->ctrlr;
3152 }
3153 
3154 struct spdk_nvme_cmd *spdk_nvmf_request_get_cmd(struct spdk_nvmf_request *req)
3155 {
3156 	return &req->cmd->nvme_cmd;
3157 }
3158 
3159 struct spdk_nvme_cpl *spdk_nvmf_request_get_response(struct spdk_nvmf_request *req)
3160 {
3161 	return &req->rsp->nvme_cpl;
3162 }
3163 
3164 struct spdk_nvmf_subsystem *spdk_nvmf_request_get_subsystem(struct spdk_nvmf_request *req)
3165 {
3166 	return req->qpair->ctrlr->subsys;
3167 }
3168 
3169 void spdk_nvmf_request_get_data(struct spdk_nvmf_request *req, void **data, uint32_t *length)
3170 {
3171 	*data = req->data;
3172 	*length = req->length;
3173 }
3174 
3175 struct spdk_nvmf_subsystem *spdk_nvmf_ctrlr_get_subsystem(struct spdk_nvmf_ctrlr *ctrlr)
3176 {
3177 	return ctrlr->subsys;
3178 }
3179