xref: /spdk/lib/nvmf/ctrlr.c (revision de5f53220cb1b96b8f1bb4112bc184037c8d116d)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   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/string.h"
45 #include "spdk/util.h"
46 #include "spdk/version.h"
47 
48 #include "spdk_internal/log.h"
49 
50 #define MIN_KEEP_ALIVE_TIMEOUT_IN_MS 10000
51 #define NVMF_DISC_KATO_IN_MS 120000
52 #define KAS_TIME_UNIT_IN_MS 100
53 #define KAS_DEFAULT_VALUE (MIN_KEEP_ALIVE_TIMEOUT_IN_MS / KAS_TIME_UNIT_IN_MS)
54 
55 #define MODEL_NUMBER "SPDK bdev Controller"
56 
57 /*
58  * Report the SPDK version as the firmware revision.
59  * SPDK_VERSION_STRING won't fit into FR (only 8 bytes), so try to fit the most important parts.
60  */
61 #define FW_VERSION SPDK_VERSION_MAJOR_STRING SPDK_VERSION_MINOR_STRING SPDK_VERSION_PATCH_STRING
62 
63 static inline void
64 spdk_nvmf_invalid_connect_response(struct spdk_nvmf_fabric_connect_rsp *rsp,
65 				   uint8_t iattr, uint16_t ipo)
66 {
67 	rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
68 	rsp->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM;
69 	rsp->status_code_specific.invalid.iattr = iattr;
70 	rsp->status_code_specific.invalid.ipo = ipo;
71 }
72 
73 #define SPDK_NVMF_INVALID_CONNECT_CMD(rsp, field)	\
74 	spdk_nvmf_invalid_connect_response(rsp, 0, offsetof(struct spdk_nvmf_fabric_connect_cmd, field))
75 #define SPDK_NVMF_INVALID_CONNECT_DATA(rsp, field)	\
76 	spdk_nvmf_invalid_connect_response(rsp, 1, offsetof(struct spdk_nvmf_fabric_connect_data, field))
77 
78 static void
79 spdk_nvmf_ctrlr_stop_keep_alive_timer(struct spdk_nvmf_ctrlr *ctrlr)
80 {
81 	if (!ctrlr) {
82 		SPDK_ERRLOG("Controller is NULL\n");
83 		return;
84 	}
85 
86 	if (ctrlr->keep_alive_poller == NULL) {
87 		return;
88 	}
89 
90 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Stop keep alive poller\n");
91 	spdk_poller_unregister(&ctrlr->keep_alive_poller);
92 }
93 
94 static void
95 spdk_nvmf_ctrlr_disconnect_qpairs_done(struct spdk_io_channel_iter *i, int status)
96 {
97 	if (status == 0) {
98 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ctrlr disconnect qpairs complete successfully\n");
99 	} else {
100 		SPDK_ERRLOG("Fail to disconnect ctrlr qpairs\n");
101 	}
102 }
103 
104 static void
105 spdk_nvmf_ctrlr_disconnect_qpairs_on_pg(struct spdk_io_channel_iter *i)
106 {
107 	int rc = 0;
108 	struct spdk_nvmf_ctrlr *ctrlr;
109 	struct spdk_nvmf_qpair *qpair, *temp_qpair;
110 	struct spdk_io_channel *ch;
111 	struct spdk_nvmf_poll_group *group;
112 
113 	ctrlr = spdk_io_channel_iter_get_ctx(i);
114 	ch = spdk_io_channel_iter_get_channel(i);
115 	group = spdk_io_channel_get_ctx(ch);
116 
117 	TAILQ_FOREACH_SAFE(qpair, &group->qpairs, link, temp_qpair) {
118 		if (qpair->ctrlr == ctrlr) {
119 			rc = spdk_nvmf_qpair_disconnect(qpair, NULL, NULL);
120 			if (rc) {
121 				SPDK_ERRLOG("Qpair disconnect failed\n");
122 				goto next_channel;
123 			}
124 		}
125 	}
126 
127 next_channel:
128 	spdk_for_each_channel_continue(i, rc);
129 }
130 
131 static int
132 spdk_nvmf_ctrlr_keep_alive_poll(void *ctx)
133 {
134 	uint64_t keep_alive_timeout_tick;
135 	uint64_t now = spdk_get_ticks();
136 	struct spdk_nvmf_ctrlr *ctrlr = ctx;
137 
138 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Polling ctrlr keep alive timeout\n");
139 
140 	/* If the Keep alive feature is in use and the timer expires */
141 	keep_alive_timeout_tick = ctrlr->last_keep_alive_tick +
142 				  ctrlr->feat.keep_alive_timer.bits.kato * spdk_get_ticks_hz() / UINT64_C(1000);
143 	if (now > keep_alive_timeout_tick) {
144 		/* set the Controller Fatal Status bit to '1' */
145 		if (ctrlr->vcprop.csts.bits.cfs == 0) {
146 			ctrlr->vcprop.csts.bits.cfs = 1;
147 
148 			/*
149 			 * disconnect qpairs, terminate Transport connection
150 			 * destroy ctrlr, break the host to controller association
151 			 * disconnect qpairs with qpair->ctrlr == ctrlr
152 			 */
153 			spdk_for_each_channel(ctrlr->subsys->tgt,
154 					      spdk_nvmf_ctrlr_disconnect_qpairs_on_pg,
155 					      ctrlr,
156 					      spdk_nvmf_ctrlr_disconnect_qpairs_done);
157 		}
158 	}
159 
160 	return 1;
161 }
162 
163 static void
164 spdk_nvmf_ctrlr_start_keep_alive_timer(struct spdk_nvmf_ctrlr *ctrlr)
165 {
166 	if (!ctrlr) {
167 		SPDK_ERRLOG("Controller is NULL\n");
168 		return;
169 	}
170 
171 	/* if cleared to 0 then the Keep Alive Timer is disabled */
172 	if (ctrlr->feat.keep_alive_timer.bits.kato != 0) {
173 
174 		ctrlr->last_keep_alive_tick = spdk_get_ticks();
175 
176 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Ctrlr add keep alive poller\n");
177 		ctrlr->keep_alive_poller = spdk_poller_register(spdk_nvmf_ctrlr_keep_alive_poll, ctrlr,
178 					   ctrlr->feat.keep_alive_timer.bits.kato * 1000);
179 	}
180 }
181 
182 static void
183 ctrlr_add_qpair_and_update_rsp(struct spdk_nvmf_qpair *qpair,
184 			       struct spdk_nvmf_ctrlr *ctrlr,
185 			       struct spdk_nvmf_fabric_connect_rsp *rsp)
186 {
187 	assert(ctrlr->admin_qpair->group->thread == spdk_get_thread());
188 
189 	/* check if we would exceed ctrlr connection limit */
190 	if (qpair->qid >= spdk_bit_array_capacity(ctrlr->qpair_mask)) {
191 		SPDK_ERRLOG("Requested QID %u but Max QID is %u\n",
192 			    qpair->qid, spdk_bit_array_capacity(ctrlr->qpair_mask) - 1);
193 		rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
194 		rsp->status.sc = SPDK_NVME_SC_INVALID_QUEUE_IDENTIFIER;
195 		return;
196 	}
197 
198 	if (spdk_bit_array_get(ctrlr->qpair_mask, qpair->qid)) {
199 		SPDK_ERRLOG("Got I/O connect with duplicate QID %u\n", qpair->qid);
200 		rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
201 		rsp->status.sc = SPDK_NVME_SC_INVALID_QUEUE_IDENTIFIER;
202 		return;
203 	}
204 
205 	qpair->ctrlr = ctrlr;
206 	spdk_bit_array_set(ctrlr->qpair_mask, qpair->qid);
207 
208 	rsp->status.sc = SPDK_NVME_SC_SUCCESS;
209 	rsp->status_code_specific.success.cntlid = ctrlr->cntlid;
210 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "connect capsule response: cntlid = 0x%04x\n",
211 		      rsp->status_code_specific.success.cntlid);
212 }
213 
214 static void
215 _spdk_nvmf_request_complete(void *ctx)
216 {
217 	struct spdk_nvmf_request *req = ctx;
218 
219 	spdk_nvmf_request_complete(req);
220 }
221 
222 static void
223 _spdk_nvmf_ctrlr_add_admin_qpair(void *ctx)
224 {
225 	struct spdk_nvmf_request *req = ctx;
226 	struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp;
227 	struct spdk_nvmf_qpair *qpair = req->qpair;
228 	struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr;
229 
230 	ctrlr->admin_qpair = qpair;
231 	spdk_nvmf_ctrlr_start_keep_alive_timer(ctrlr);
232 	ctrlr_add_qpair_and_update_rsp(qpair, ctrlr, rsp);
233 	spdk_nvmf_request_complete(req);
234 }
235 
236 static void
237 _spdk_nvmf_subsystem_add_ctrlr(void *ctx)
238 {
239 	struct spdk_nvmf_request *req = ctx;
240 	struct spdk_nvmf_qpair *qpair = req->qpair;
241 	struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp;
242 	struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr;
243 
244 	if (spdk_nvmf_subsystem_add_ctrlr(ctrlr->subsys, ctrlr)) {
245 		SPDK_ERRLOG("Unable to add controller to subsystem\n");
246 		spdk_bit_array_free(&ctrlr->qpair_mask);
247 		free(ctrlr);
248 		qpair->ctrlr = NULL;
249 		rsp->status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
250 		spdk_thread_send_msg(qpair->group->thread, _spdk_nvmf_request_complete, req);
251 		return;
252 	}
253 
254 	spdk_thread_send_msg(ctrlr->thread, _spdk_nvmf_ctrlr_add_admin_qpair, req);
255 }
256 
257 static struct spdk_nvmf_ctrlr *
258 spdk_nvmf_ctrlr_create(struct spdk_nvmf_subsystem *subsystem,
259 		       struct spdk_nvmf_request *req,
260 		       struct spdk_nvmf_fabric_connect_cmd *connect_cmd,
261 		       struct spdk_nvmf_fabric_connect_data *connect_data)
262 {
263 	struct spdk_nvmf_ctrlr	*ctrlr;
264 	struct spdk_nvmf_transport *transport;
265 
266 	ctrlr = calloc(1, sizeof(*ctrlr));
267 	if (ctrlr == NULL) {
268 		SPDK_ERRLOG("Memory allocation failed\n");
269 		return NULL;
270 	}
271 
272 	ctrlr->subsys = subsystem;
273 	ctrlr->thread = req->qpair->group->thread;
274 
275 	transport = req->qpair->transport;
276 	ctrlr->qpair_mask = spdk_bit_array_create(transport->opts.max_qpairs_per_ctrlr);
277 	if (!ctrlr->qpair_mask) {
278 		SPDK_ERRLOG("Failed to allocate controller qpair mask\n");
279 		free(ctrlr);
280 		return NULL;
281 	}
282 
283 	/*
284 	 * KAS: this field indicates the granularity of the Keep Alive Timer in 100ms units
285 	 * keep-alive timeout in milliseconds
286 	 */
287 	ctrlr->feat.keep_alive_timer.bits.kato = spdk_divide_round_up(connect_cmd->kato,
288 			KAS_DEFAULT_VALUE * KAS_TIME_UNIT_IN_MS) *
289 			KAS_DEFAULT_VALUE * KAS_TIME_UNIT_IN_MS;
290 	ctrlr->feat.async_event_configuration.bits.ns_attr_notice = 1;
291 	ctrlr->feat.volatile_write_cache.bits.wce = 1;
292 
293 	if (ctrlr->subsys->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) {
294 		/* Don't accept keep-alive timeout for discovery controllers */
295 		if (ctrlr->feat.keep_alive_timer.bits.kato != 0) {
296 			SPDK_ERRLOG("Discovery controller don't accept keep-alive timeout\n");
297 			spdk_bit_array_free(&ctrlr->qpair_mask);
298 			free(ctrlr);
299 			return NULL;
300 		}
301 
302 		/*
303 		 * Discovery controllers use some arbitrary high value in order
304 		 * to cleanup stale discovery sessions
305 		 *
306 		 * From the 1.0a nvme-of spec:
307 		 * "The Keep Alive command is reserved for
308 		 * Discovery controllers. A transport may specify a
309 		 * fixed Discovery controller activity timeout value
310 		 * (e.g., 2 minutes).  If no commands are received
311 		 * by a Discovery controller within that time
312 		 * period, the controller may perform the
313 		 * actions for Keep Alive Timer expiration".
314 		 * kato is in millisecond.
315 		 */
316 		ctrlr->feat.keep_alive_timer.bits.kato = NVMF_DISC_KATO_IN_MS;
317 	}
318 
319 	/* Subtract 1 for admin queue, 1 for 0's based */
320 	ctrlr->feat.number_of_queues.bits.ncqr = transport->opts.max_qpairs_per_ctrlr - 1 -
321 			1;
322 	ctrlr->feat.number_of_queues.bits.nsqr = transport->opts.max_qpairs_per_ctrlr - 1 -
323 			1;
324 
325 	spdk_uuid_copy(&ctrlr->hostid, (struct spdk_uuid *)connect_data->hostid);
326 
327 	ctrlr->vcprop.cap.raw = 0;
328 	ctrlr->vcprop.cap.bits.cqr = 1; /* NVMe-oF specification required */
329 	ctrlr->vcprop.cap.bits.mqes = transport->opts.max_queue_depth -
330 				      1; /* max queue depth */
331 	ctrlr->vcprop.cap.bits.ams = 0; /* optional arb mechanisms */
332 	ctrlr->vcprop.cap.bits.to = 1; /* ready timeout - 500 msec units */
333 	ctrlr->vcprop.cap.bits.dstrd = 0; /* fixed to 0 for NVMe-oF */
334 	ctrlr->vcprop.cap.bits.css = SPDK_NVME_CAP_CSS_NVM; /* NVM command set */
335 	ctrlr->vcprop.cap.bits.mpsmin = 0; /* 2 ^ (12 + mpsmin) == 4k */
336 	ctrlr->vcprop.cap.bits.mpsmax = 0; /* 2 ^ (12 + mpsmax) == 4k */
337 
338 	/* Version Supported: 1.3 */
339 	ctrlr->vcprop.vs.bits.mjr = 1;
340 	ctrlr->vcprop.vs.bits.mnr = 3;
341 	ctrlr->vcprop.vs.bits.ter = 0;
342 
343 	ctrlr->vcprop.cc.raw = 0;
344 	ctrlr->vcprop.cc.bits.en = 0; /* Init controller disabled */
345 
346 	ctrlr->vcprop.csts.raw = 0;
347 	ctrlr->vcprop.csts.bits.rdy = 0; /* Init controller as not ready */
348 
349 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "cap 0x%" PRIx64 "\n", ctrlr->vcprop.cap.raw);
350 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "vs 0x%x\n", ctrlr->vcprop.vs.raw);
351 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "cc 0x%x\n", ctrlr->vcprop.cc.raw);
352 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "csts 0x%x\n", ctrlr->vcprop.csts.raw);
353 
354 	req->qpair->ctrlr = ctrlr;
355 	spdk_thread_send_msg(subsystem->thread, _spdk_nvmf_subsystem_add_ctrlr, req);
356 
357 	return ctrlr;
358 }
359 
360 static void
361 _spdk_nvmf_ctrlr_destruct(void *ctx)
362 {
363 	struct spdk_nvmf_ctrlr *ctrlr = ctx;
364 
365 	spdk_nvmf_ctrlr_stop_keep_alive_timer(ctrlr);
366 	free(ctrlr);
367 }
368 
369 void
370 spdk_nvmf_ctrlr_destruct(struct spdk_nvmf_ctrlr *ctrlr)
371 {
372 	spdk_nvmf_subsystem_remove_ctrlr(ctrlr->subsys, ctrlr);
373 
374 	spdk_thread_send_msg(ctrlr->thread, _spdk_nvmf_ctrlr_destruct, ctrlr);
375 }
376 
377 static void
378 spdk_nvmf_ctrlr_add_io_qpair(void *ctx)
379 {
380 	struct spdk_nvmf_request *req = ctx;
381 	struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp;
382 	struct spdk_nvmf_qpair *qpair = req->qpair;
383 	struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr;
384 
385 	/* Unit test will check qpair->ctrlr after calling spdk_nvmf_ctrlr_connect.
386 	  * For error case, the value should be NULL. So set it to NULL at first.
387 	  */
388 	qpair->ctrlr = NULL;
389 
390 	if (ctrlr->subsys->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) {
391 		SPDK_ERRLOG("I/O connect not allowed on discovery controller\n");
392 		SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid);
393 		goto end;
394 	}
395 
396 	if (!ctrlr->vcprop.cc.bits.en) {
397 		SPDK_ERRLOG("Got I/O connect before ctrlr was enabled\n");
398 		SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid);
399 		goto end;
400 	}
401 
402 	if (1u << ctrlr->vcprop.cc.bits.iosqes != sizeof(struct spdk_nvme_cmd)) {
403 		SPDK_ERRLOG("Got I/O connect with invalid IOSQES %u\n",
404 			    ctrlr->vcprop.cc.bits.iosqes);
405 		SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid);
406 		goto end;
407 	}
408 
409 	if (1u << ctrlr->vcprop.cc.bits.iocqes != sizeof(struct spdk_nvme_cpl)) {
410 		SPDK_ERRLOG("Got I/O connect with invalid IOCQES %u\n",
411 			    ctrlr->vcprop.cc.bits.iocqes);
412 		SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid);
413 		goto end;
414 	}
415 
416 	ctrlr_add_qpair_and_update_rsp(qpair, ctrlr, rsp);
417 end:
418 	spdk_thread_send_msg(qpair->group->thread, _spdk_nvmf_request_complete, req);
419 }
420 
421 static void
422 _spdk_nvmf_ctrlr_add_io_qpair(void *ctx)
423 {
424 	struct spdk_nvmf_request *req = ctx;
425 	struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp;
426 	struct spdk_nvmf_fabric_connect_data *data = req->data;
427 	struct spdk_nvmf_ctrlr *ctrlr;
428 	struct spdk_nvmf_qpair *qpair = req->qpair;
429 	struct spdk_nvmf_qpair *admin_qpair;
430 	struct spdk_nvmf_tgt *tgt = qpair->transport->tgt;
431 	struct spdk_nvmf_subsystem *subsystem;
432 
433 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Connect I/O Queue for controller id 0x%x\n", data->cntlid);
434 
435 	subsystem = spdk_nvmf_tgt_find_subsystem(tgt, data->subnqn);
436 	/* We already checked this in spdk_nvmf_ctrlr_connect */
437 	assert(subsystem != NULL);
438 
439 	ctrlr = spdk_nvmf_subsystem_get_ctrlr(subsystem, data->cntlid);
440 	if (ctrlr == NULL) {
441 		SPDK_ERRLOG("Unknown controller ID 0x%x\n", data->cntlid);
442 		SPDK_NVMF_INVALID_CONNECT_DATA(rsp, cntlid);
443 		spdk_thread_send_msg(qpair->group->thread, _spdk_nvmf_request_complete, req);
444 		return;
445 	}
446 
447 	admin_qpair = ctrlr->admin_qpair;
448 	qpair->ctrlr = ctrlr;
449 	spdk_thread_send_msg(admin_qpair->group->thread, spdk_nvmf_ctrlr_add_io_qpair, req);
450 }
451 
452 static int
453 spdk_nvmf_ctrlr_connect(struct spdk_nvmf_request *req)
454 {
455 	struct spdk_nvmf_fabric_connect_data *data = req->data;
456 	struct spdk_nvmf_fabric_connect_cmd *cmd = &req->cmd->connect_cmd;
457 	struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp;
458 	struct spdk_nvmf_qpair *qpair = req->qpair;
459 	struct spdk_nvmf_tgt *tgt = qpair->transport->tgt;
460 	struct spdk_nvmf_ctrlr *ctrlr;
461 	struct spdk_nvmf_subsystem *subsystem;
462 	const char *subnqn, *hostnqn;
463 	struct spdk_nvme_transport_id listen_trid = {};
464 	void *end;
465 
466 	if (req->length < sizeof(struct spdk_nvmf_fabric_connect_data)) {
467 		SPDK_ERRLOG("Connect command data length 0x%x too small\n", req->length);
468 		rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
469 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
470 	}
471 
472 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "recfmt 0x%x qid %u sqsize %u\n",
473 		      cmd->recfmt, cmd->qid, cmd->sqsize);
474 
475 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Connect data:\n");
476 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "  cntlid:  0x%04x\n", data->cntlid);
477 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "  hostid: %08x-%04x-%04x-%02x%02x-%04x%08x ***\n",
478 		      ntohl(*(uint32_t *)&data->hostid[0]),
479 		      ntohs(*(uint16_t *)&data->hostid[4]),
480 		      ntohs(*(uint16_t *)&data->hostid[6]),
481 		      data->hostid[8],
482 		      data->hostid[9],
483 		      ntohs(*(uint16_t *)&data->hostid[10]),
484 		      ntohl(*(uint32_t *)&data->hostid[12]));
485 
486 	if (cmd->recfmt != 0) {
487 		SPDK_ERRLOG("Connect command unsupported RECFMT %u\n", cmd->recfmt);
488 		rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
489 		rsp->status.sc = SPDK_NVMF_FABRIC_SC_INCOMPATIBLE_FORMAT;
490 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
491 	}
492 
493 	/* Ensure that subnqn is null terminated */
494 	end = memchr(data->subnqn, '\0', SPDK_NVMF_NQN_MAX_LEN + 1);
495 	if (!end) {
496 		SPDK_ERRLOG("Connect SUBNQN is not null terminated\n");
497 		SPDK_NVMF_INVALID_CONNECT_DATA(rsp, subnqn);
498 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
499 	}
500 	subnqn = data->subnqn;
501 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "  subnqn: \"%s\"\n", subnqn);
502 
503 	subsystem = spdk_nvmf_tgt_find_subsystem(tgt, subnqn);
504 	if (subsystem == NULL) {
505 		SPDK_ERRLOG("Could not find subsystem '%s'\n", subnqn);
506 		SPDK_NVMF_INVALID_CONNECT_DATA(rsp, subnqn);
507 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
508 	}
509 
510 	if ((subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE) ||
511 	    (subsystem->state == SPDK_NVMF_SUBSYSTEM_DEACTIVATING)) {
512 		SPDK_ERRLOG("Subsystem '%s' is not ready\n", subnqn);
513 		rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
514 		rsp->status.sc = SPDK_NVMF_FABRIC_SC_CONTROLLER_BUSY;
515 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
516 	}
517 
518 	/* Ensure that hostnqn is null terminated */
519 	end = memchr(data->hostnqn, '\0', SPDK_NVMF_NQN_MAX_LEN + 1);
520 	if (!end) {
521 		SPDK_ERRLOG("Connect HOSTNQN is not null terminated\n");
522 		SPDK_NVMF_INVALID_CONNECT_DATA(rsp, hostnqn);
523 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
524 	}
525 	hostnqn = data->hostnqn;
526 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "  hostnqn: \"%s\"\n", hostnqn);
527 
528 	if (!spdk_nvmf_subsystem_host_allowed(subsystem, hostnqn)) {
529 		SPDK_ERRLOG("Subsystem '%s' does not allow host '%s'\n", subnqn, hostnqn);
530 		rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
531 		rsp->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_HOST;
532 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
533 	}
534 
535 	if (spdk_nvmf_qpair_get_listen_trid(qpair, &listen_trid)) {
536 		SPDK_ERRLOG("Subsystem '%s' is unable to enforce access control due to an internal error.\n",
537 			    subnqn);
538 		rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
539 		rsp->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_HOST;
540 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
541 	}
542 
543 	if (!spdk_nvmf_subsystem_listener_allowed(subsystem, &listen_trid)) {
544 		SPDK_ERRLOG("Subsystem '%s' does not allow host '%s' to connect at this address.\n", subnqn,
545 			    hostnqn);
546 		rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
547 		rsp->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_HOST;
548 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
549 	}
550 
551 	/*
552 	 * SQSIZE is a 0-based value, so it must be at least 1 (minimum queue depth is 2) and
553 	 *  strictly less than max_queue_depth.
554 	 */
555 	if (cmd->sqsize == 0 || cmd->sqsize >= qpair->transport->opts.max_queue_depth) {
556 		SPDK_ERRLOG("Invalid SQSIZE %u (min 1, max %u)\n",
557 			    cmd->sqsize, qpair->transport->opts.max_queue_depth - 1);
558 		SPDK_NVMF_INVALID_CONNECT_CMD(rsp, sqsize);
559 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
560 	}
561 	qpair->sq_head_max = cmd->sqsize;
562 	qpair->qid = cmd->qid;
563 
564 	if (spdk_nvmf_transport_qpair_set_sqsize(qpair)) {
565 		SPDK_ERRLOG("Can not create SQSIZE %u for qpair=%p\n", cmd->sqsize, qpair);
566 		rsp->status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
567 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
568 	}
569 
570 	if (cmd->qid == 0) {
571 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Connect Admin Queue for controller ID 0x%x\n", data->cntlid);
572 
573 		if (data->cntlid != 0xFFFF) {
574 			/* This NVMf target only supports dynamic mode. */
575 			SPDK_ERRLOG("The NVMf target only supports dynamic mode (CNTLID = 0x%x).\n", data->cntlid);
576 			SPDK_NVMF_INVALID_CONNECT_DATA(rsp, cntlid);
577 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
578 		}
579 
580 		/* Establish a new ctrlr */
581 		ctrlr = spdk_nvmf_ctrlr_create(subsystem, req, cmd, data);
582 		if (!ctrlr) {
583 			SPDK_ERRLOG("spdk_nvmf_ctrlr_create() failed\n");
584 			rsp->status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
585 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
586 		} else {
587 			return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
588 		}
589 	} else {
590 		spdk_thread_send_msg(subsystem->thread, _spdk_nvmf_ctrlr_add_io_qpair, req);
591 		return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
592 	}
593 }
594 
595 static uint64_t
596 nvmf_prop_get_cap(struct spdk_nvmf_ctrlr *ctrlr)
597 {
598 	return ctrlr->vcprop.cap.raw;
599 }
600 
601 static uint64_t
602 nvmf_prop_get_vs(struct spdk_nvmf_ctrlr *ctrlr)
603 {
604 	return ctrlr->vcprop.vs.raw;
605 }
606 
607 static uint64_t
608 nvmf_prop_get_cc(struct spdk_nvmf_ctrlr *ctrlr)
609 {
610 	return ctrlr->vcprop.cc.raw;
611 }
612 
613 static bool
614 nvmf_prop_set_cc(struct spdk_nvmf_ctrlr *ctrlr, uint64_t value)
615 {
616 	union spdk_nvme_cc_register cc, diff;
617 
618 	cc.raw = (uint32_t)value;
619 
620 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "cur CC: 0x%08x\n", ctrlr->vcprop.cc.raw);
621 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "new CC: 0x%08x\n", cc.raw);
622 
623 	/*
624 	 * Calculate which bits changed between the current and new CC.
625 	 * Mark each bit as 0 once it is handled to determine if any unhandled bits were changed.
626 	 */
627 	diff.raw = cc.raw ^ ctrlr->vcprop.cc.raw;
628 
629 	if (diff.bits.en) {
630 		if (cc.bits.en) {
631 			SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Property Set CC Enable!\n");
632 			ctrlr->vcprop.cc.bits.en = 1;
633 			ctrlr->vcprop.csts.bits.rdy = 1;
634 		} else {
635 			SPDK_ERRLOG("CC.EN transition from 1 to 0 (reset) not implemented!\n");
636 
637 		}
638 		diff.bits.en = 0;
639 	}
640 
641 	if (diff.bits.shn) {
642 		if (cc.bits.shn == SPDK_NVME_SHN_NORMAL ||
643 		    cc.bits.shn == SPDK_NVME_SHN_ABRUPT) {
644 			SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Property Set CC Shutdown %u%ub!\n",
645 				      cc.bits.shn >> 1, cc.bits.shn & 1);
646 			ctrlr->vcprop.cc.bits.shn = cc.bits.shn;
647 			ctrlr->vcprop.cc.bits.en = 0;
648 			ctrlr->vcprop.csts.bits.rdy = 0;
649 			ctrlr->vcprop.csts.bits.shst = SPDK_NVME_SHST_COMPLETE;
650 		} else if (cc.bits.shn == 0) {
651 			ctrlr->vcprop.cc.bits.shn = 0;
652 		} else {
653 			SPDK_ERRLOG("Prop Set CC: Invalid SHN value %u%ub\n",
654 				    cc.bits.shn >> 1, cc.bits.shn & 1);
655 			return false;
656 		}
657 		diff.bits.shn = 0;
658 	}
659 
660 	if (diff.bits.iosqes) {
661 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Prop Set IOSQES = %u (%u bytes)\n",
662 			      cc.bits.iosqes, 1u << cc.bits.iosqes);
663 		ctrlr->vcprop.cc.bits.iosqes = cc.bits.iosqes;
664 		diff.bits.iosqes = 0;
665 	}
666 
667 	if (diff.bits.iocqes) {
668 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Prop Set IOCQES = %u (%u bytes)\n",
669 			      cc.bits.iocqes, 1u << cc.bits.iocqes);
670 		ctrlr->vcprop.cc.bits.iocqes = cc.bits.iocqes;
671 		diff.bits.iocqes = 0;
672 	}
673 
674 	if (diff.raw != 0) {
675 		SPDK_ERRLOG("Prop Set CC toggled reserved bits 0x%x!\n", diff.raw);
676 		return false;
677 	}
678 
679 	return true;
680 }
681 
682 static uint64_t
683 nvmf_prop_get_csts(struct spdk_nvmf_ctrlr *ctrlr)
684 {
685 	return ctrlr->vcprop.csts.raw;
686 }
687 
688 struct nvmf_prop {
689 	uint32_t ofst;
690 	uint8_t size;
691 	char name[11];
692 	uint64_t (*get_cb)(struct spdk_nvmf_ctrlr *ctrlr);
693 	bool (*set_cb)(struct spdk_nvmf_ctrlr *ctrlr, uint64_t value);
694 };
695 
696 #define PROP(field, size, get_cb, set_cb) \
697 	{ \
698 		offsetof(struct spdk_nvme_registers, field), \
699 		SPDK_NVMF_PROP_SIZE_##size, \
700 		#field, \
701 		get_cb, set_cb \
702 	}
703 
704 static const struct nvmf_prop nvmf_props[] = {
705 	PROP(cap,  8, nvmf_prop_get_cap,  NULL),
706 	PROP(vs,   4, nvmf_prop_get_vs,   NULL),
707 	PROP(cc,   4, nvmf_prop_get_cc,   nvmf_prop_set_cc),
708 	PROP(csts, 4, nvmf_prop_get_csts, NULL),
709 };
710 
711 static const struct nvmf_prop *
712 find_prop(uint32_t ofst)
713 {
714 	size_t i;
715 
716 	for (i = 0; i < SPDK_COUNTOF(nvmf_props); i++) {
717 		const struct nvmf_prop *prop = &nvmf_props[i];
718 
719 		if (prop->ofst == ofst) {
720 			return prop;
721 		}
722 	}
723 
724 	return NULL;
725 }
726 
727 static int
728 spdk_nvmf_property_get(struct spdk_nvmf_request *req)
729 {
730 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
731 	struct spdk_nvmf_fabric_prop_get_cmd *cmd = &req->cmd->prop_get_cmd;
732 	struct spdk_nvmf_fabric_prop_get_rsp *response = &req->rsp->prop_get_rsp;
733 	const struct nvmf_prop *prop;
734 
735 	response->status.sc = 0;
736 	response->value.u64 = 0;
737 
738 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "size %d, offset 0x%x\n",
739 		      cmd->attrib.size, cmd->ofst);
740 
741 	if (cmd->attrib.size != SPDK_NVMF_PROP_SIZE_4 &&
742 	    cmd->attrib.size != SPDK_NVMF_PROP_SIZE_8) {
743 		SPDK_ERRLOG("Invalid size value %d\n", cmd->attrib.size);
744 		response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
745 		response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM;
746 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
747 	}
748 
749 	prop = find_prop(cmd->ofst);
750 	if (prop == NULL || prop->get_cb == NULL) {
751 		/* Reserved properties return 0 when read */
752 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
753 	}
754 
755 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "name: %s\n", prop->name);
756 	if (cmd->attrib.size != prop->size) {
757 		SPDK_ERRLOG("offset 0x%x size mismatch: cmd %u, prop %u\n",
758 			    cmd->ofst, cmd->attrib.size, prop->size);
759 		response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
760 		response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM;
761 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
762 	}
763 
764 	response->value.u64 = prop->get_cb(ctrlr);
765 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "response value: 0x%" PRIx64 "\n", response->value.u64);
766 
767 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
768 }
769 
770 static int
771 spdk_nvmf_property_set(struct spdk_nvmf_request *req)
772 {
773 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
774 	struct spdk_nvmf_fabric_prop_set_cmd *cmd = &req->cmd->prop_set_cmd;
775 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
776 	const struct nvmf_prop *prop;
777 	uint64_t value;
778 
779 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "size %d, offset 0x%x, value 0x%" PRIx64 "\n",
780 		      cmd->attrib.size, cmd->ofst, cmd->value.u64);
781 
782 	prop = find_prop(cmd->ofst);
783 	if (prop == NULL || prop->set_cb == NULL) {
784 		SPDK_ERRLOG("Invalid offset 0x%x\n", cmd->ofst);
785 		response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
786 		response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM;
787 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
788 	}
789 
790 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "name: %s\n", prop->name);
791 	if (cmd->attrib.size != prop->size) {
792 		SPDK_ERRLOG("offset 0x%x size mismatch: cmd %u, prop %u\n",
793 			    cmd->ofst, cmd->attrib.size, prop->size);
794 		response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
795 		response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM;
796 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
797 	}
798 
799 	value = cmd->value.u64;
800 	if (prop->size == SPDK_NVMF_PROP_SIZE_4) {
801 		value = (uint32_t)value;
802 	}
803 
804 	if (!prop->set_cb(ctrlr, value)) {
805 		SPDK_ERRLOG("prop set_cb failed\n");
806 		response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
807 		response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM;
808 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
809 	}
810 
811 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
812 }
813 
814 static int
815 spdk_nvmf_ctrlr_set_features_arbitration(struct spdk_nvmf_request *req)
816 {
817 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
818 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
819 
820 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Arbitration (cdw11 = 0x%0x)\n", cmd->cdw11);
821 
822 	ctrlr->feat.arbitration.raw = cmd->cdw11;
823 	ctrlr->feat.arbitration.bits.reserved = 0;
824 
825 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
826 }
827 
828 static int
829 spdk_nvmf_ctrlr_set_features_power_management(struct spdk_nvmf_request *req)
830 {
831 	union spdk_nvme_feat_power_management opts;
832 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
833 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
834 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
835 
836 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Power Management (cdw11 = 0x%0x)\n", cmd->cdw11);
837 	opts.raw = cmd->cdw11;
838 
839 	/* Only PS = 0 is allowed, since we report NPSS = 0 */
840 	if (opts.bits.ps != 0) {
841 		SPDK_ERRLOG("Invalid power state %u\n", opts.bits.ps);
842 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
843 		rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
844 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
845 	}
846 
847 	ctrlr->feat.power_management.raw = cmd->cdw11;
848 	ctrlr->feat.power_management.bits.reserved = 0;
849 
850 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
851 }
852 
853 static bool
854 temp_threshold_opts_valid(const union spdk_nvme_feat_temperature_threshold *opts)
855 {
856 	/*
857 	 * Valid TMPSEL values:
858 	 *  0000b - 1000b: temperature sensors
859 	 *  1111b: set all implemented temperature sensors
860 	 */
861 	if (opts->bits.tmpsel >= 9 && opts->bits.tmpsel != 15) {
862 		/* 1001b - 1110b: reserved */
863 		SPDK_ERRLOG("Invalid TMPSEL %u\n", opts->bits.tmpsel);
864 		return false;
865 	}
866 
867 	/*
868 	 * Valid THSEL values:
869 	 *  00b: over temperature threshold
870 	 *  01b: under temperature threshold
871 	 */
872 	if (opts->bits.thsel > 1) {
873 		/* 10b - 11b: reserved */
874 		SPDK_ERRLOG("Invalid THSEL %u\n", opts->bits.thsel);
875 		return false;
876 	}
877 
878 	return true;
879 }
880 
881 static int
882 spdk_nvmf_ctrlr_set_features_temperature_threshold(struct spdk_nvmf_request *req)
883 {
884 	union spdk_nvme_feat_temperature_threshold opts;
885 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
886 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
887 
888 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Temperature Threshold (cdw11 = 0x%0x)\n", cmd->cdw11);
889 	opts.raw = cmd->cdw11;
890 
891 	if (!temp_threshold_opts_valid(&opts)) {
892 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
893 		rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
894 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
895 	}
896 
897 	/* TODO: no sensors implemented - ignore new values */
898 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
899 }
900 
901 static int
902 spdk_nvmf_ctrlr_get_features_temperature_threshold(struct spdk_nvmf_request *req)
903 {
904 	union spdk_nvme_feat_temperature_threshold opts;
905 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
906 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
907 
908 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Get Features - Temperature Threshold (cdw11 = 0x%0x)\n", cmd->cdw11);
909 	opts.raw = cmd->cdw11;
910 
911 	if (!temp_threshold_opts_valid(&opts)) {
912 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
913 		rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
914 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
915 	}
916 
917 	/* TODO: no sensors implemented - return 0 for all thresholds */
918 	rsp->cdw0 = 0;
919 
920 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
921 }
922 
923 static int
924 spdk_nvmf_ctrlr_set_features_error_recovery(struct spdk_nvmf_request *req)
925 {
926 	union spdk_nvme_feat_error_recovery opts;
927 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
928 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
929 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
930 
931 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Error Recovery (cdw11 = 0x%0x)\n", cmd->cdw11);
932 	opts.raw = cmd->cdw11;
933 
934 	if (opts.bits.dulbe) {
935 		/*
936 		 * Host is not allowed to set this bit, since we don't advertise it in
937 		 * Identify Namespace.
938 		 */
939 		SPDK_ERRLOG("Host set unsupported DULBE bit\n");
940 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
941 		rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
942 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
943 	}
944 
945 	ctrlr->feat.error_recovery.raw = cmd->cdw11;
946 	ctrlr->feat.error_recovery.bits.reserved = 0;
947 
948 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
949 }
950 
951 static int
952 spdk_nvmf_ctrlr_set_features_volatile_write_cache(struct spdk_nvmf_request *req)
953 {
954 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
955 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
956 
957 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Volatile Write Cache (cdw11 = 0x%0x)\n", cmd->cdw11);
958 
959 	ctrlr->feat.volatile_write_cache.raw = cmd->cdw11;
960 	ctrlr->feat.volatile_write_cache.bits.reserved = 0;
961 
962 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Volatile Write Cache %s\n",
963 		      ctrlr->feat.volatile_write_cache.bits.wce ? "Enabled" : "Disabled");
964 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
965 }
966 
967 static int
968 spdk_nvmf_ctrlr_set_features_write_atomicity(struct spdk_nvmf_request *req)
969 {
970 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
971 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
972 
973 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Write Atomicity (cdw11 = 0x%0x)\n", cmd->cdw11);
974 
975 	ctrlr->feat.write_atomicity.raw = cmd->cdw11;
976 	ctrlr->feat.write_atomicity.bits.reserved = 0;
977 
978 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
979 }
980 
981 static int
982 spdk_nvmf_ctrlr_set_features_host_identifier(struct spdk_nvmf_request *req)
983 {
984 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
985 
986 	SPDK_ERRLOG("Set Features - Host Identifier not allowed\n");
987 	response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
988 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
989 }
990 
991 static int
992 spdk_nvmf_ctrlr_get_features_host_identifier(struct spdk_nvmf_request *req)
993 {
994 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
995 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
996 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
997 	union spdk_nvme_feat_host_identifier opts;
998 
999 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Get Features - Host Identifier\n");
1000 
1001 	opts.raw = cmd->cdw11;
1002 	if (!opts.bits.exhid) {
1003 		/* NVMe over Fabrics requires EXHID=1 (128-bit/16-byte host ID) */
1004 		SPDK_ERRLOG("Get Features - Host Identifier with EXHID=0 not allowed\n");
1005 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1006 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1007 	}
1008 
1009 	if (req->data == NULL || req->length < sizeof(ctrlr->hostid)) {
1010 		SPDK_ERRLOG("Invalid data buffer for Get Features - Host Identifier\n");
1011 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1012 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1013 	}
1014 
1015 	spdk_uuid_copy((struct spdk_uuid *)req->data, &ctrlr->hostid);
1016 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1017 }
1018 
1019 static int
1020 spdk_nvmf_ctrlr_get_features_reservation_notification_mask(struct spdk_nvmf_request *req)
1021 {
1022 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1023 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1024 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
1025 	struct spdk_nvmf_ns *ns;
1026 
1027 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "get Features - Reservation Notificaton Mask\n");
1028 
1029 	if (cmd->nsid == 0xffffffffu) {
1030 		SPDK_ERRLOG("get Features - Invalid Namespace ID\n");
1031 		rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1032 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1033 	}
1034 
1035 	ns = _spdk_nvmf_subsystem_get_ns(ctrlr->subsys, cmd->nsid);
1036 	if (ns == NULL) {
1037 		SPDK_ERRLOG("Set Features - Invalid Namespace ID\n");
1038 		rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1039 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1040 	}
1041 	rsp->cdw0 = ns->mask;
1042 
1043 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1044 }
1045 
1046 static int
1047 spdk_nvmf_ctrlr_set_features_reservation_notification_mask(struct spdk_nvmf_request *req)
1048 {
1049 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1050 	struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys;
1051 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1052 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
1053 	struct spdk_nvmf_ns *ns;
1054 
1055 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Reservation Notificaton Mask\n");
1056 
1057 	if (cmd->nsid == 0xffffffffu) {
1058 		for (ns = spdk_nvmf_subsystem_get_first_ns(subsystem); ns != NULL;
1059 		     ns = spdk_nvmf_subsystem_get_next_ns(subsystem, ns)) {
1060 			ns->mask = cmd->cdw11;
1061 		}
1062 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1063 	}
1064 
1065 	ns = _spdk_nvmf_subsystem_get_ns(ctrlr->subsys, cmd->nsid);
1066 	if (ns == NULL) {
1067 		SPDK_ERRLOG("Set Features - Invalid Namespace ID\n");
1068 		rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1069 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1070 	}
1071 	ns->mask = cmd->cdw11;
1072 
1073 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1074 }
1075 
1076 static int
1077 spdk_nvmf_ctrlr_get_features_reservation_persistence(struct spdk_nvmf_request *req)
1078 {
1079 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1080 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1081 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
1082 	struct spdk_nvmf_ns *ns;
1083 
1084 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Get Features - Reservation Persistence\n");
1085 
1086 	ns = _spdk_nvmf_subsystem_get_ns(ctrlr->subsys, cmd->nsid);
1087 	/* NSID with 0xffffffffu also included */
1088 	if (ns == NULL) {
1089 		SPDK_ERRLOG("Get Features - Invalid Namespace ID\n");
1090 		response->status.sct = SPDK_NVME_SCT_GENERIC;
1091 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1092 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1093 	}
1094 
1095 	/* TODO: Persistence feature can't support for now */
1096 	response->cdw0 = 0;
1097 
1098 	response->status.sct = SPDK_NVME_SCT_GENERIC;
1099 	response->status.sc = SPDK_NVME_SC_SUCCESS;
1100 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1101 }
1102 
1103 static int
1104 spdk_nvmf_ctrlr_set_features_reservation_persistence(struct spdk_nvmf_request *req)
1105 {
1106 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1107 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1108 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
1109 	struct spdk_nvmf_ns *ns;
1110 
1111 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Reservation Persistence\n");
1112 
1113 	ns = _spdk_nvmf_subsystem_get_ns(ctrlr->subsys, cmd->nsid);
1114 	if (cmd->nsid != 0xffffffffu && ns == NULL) {
1115 		SPDK_ERRLOG("Set Features - Invalid Namespace ID\n");
1116 		response->status.sct = SPDK_NVME_SCT_GENERIC;
1117 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1118 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1119 	}
1120 
1121 	/* TODO: Feature not changeable for now */
1122 	response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
1123 	response->status.sc = SPDK_NVME_SC_FEATURE_NOT_CHANGEABLE;
1124 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1125 }
1126 
1127 static int
1128 spdk_nvmf_ctrlr_set_features_keep_alive_timer(struct spdk_nvmf_request *req)
1129 {
1130 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1131 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1132 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
1133 
1134 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Keep Alive Timer (%u ms)\n", cmd->cdw11);
1135 
1136 	/*
1137 	 * if attempts to disable keep alive by setting kato to 0h
1138 	 * a status value of keep alive invalid shall be returned
1139 	 */
1140 	if (cmd->cdw11 == 0) {
1141 		rsp->status.sc = SPDK_NVME_SC_KEEP_ALIVE_INVALID;
1142 	} else if (cmd->cdw11 < MIN_KEEP_ALIVE_TIMEOUT_IN_MS) {
1143 		ctrlr->feat.keep_alive_timer.bits.kato = MIN_KEEP_ALIVE_TIMEOUT_IN_MS;
1144 	} else {
1145 		/* round up to milliseconds */
1146 		ctrlr->feat.keep_alive_timer.bits.kato = spdk_divide_round_up(cmd->cdw11,
1147 				KAS_DEFAULT_VALUE * KAS_TIME_UNIT_IN_MS) *
1148 				KAS_DEFAULT_VALUE * KAS_TIME_UNIT_IN_MS;
1149 	}
1150 
1151 	/*
1152 	 * if change the keep alive timeout value successfully
1153 	 * update the keep alive poller.
1154 	 */
1155 	if (cmd->cdw11 != 0) {
1156 		if (ctrlr->keep_alive_poller != NULL) {
1157 			spdk_poller_unregister(&ctrlr->keep_alive_poller);
1158 		}
1159 		ctrlr->keep_alive_poller = spdk_poller_register(spdk_nvmf_ctrlr_keep_alive_poll, ctrlr,
1160 					   ctrlr->feat.keep_alive_timer.bits.kato * 1000);
1161 	}
1162 
1163 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Keep Alive Timer set to %u ms\n",
1164 		      ctrlr->feat.keep_alive_timer.bits.kato);
1165 
1166 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1167 }
1168 
1169 static int
1170 spdk_nvmf_ctrlr_set_features_number_of_queues(struct spdk_nvmf_request *req)
1171 {
1172 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1173 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
1174 	uint32_t count;
1175 
1176 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Number of Queues, cdw11 0x%x\n",
1177 		      req->cmd->nvme_cmd.cdw11);
1178 
1179 	count = spdk_bit_array_count_set(ctrlr->qpair_mask);
1180 	/* verify that the controller is ready to process commands */
1181 	if (count > 1) {
1182 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Queue pairs already active!\n");
1183 		rsp->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
1184 	} else {
1185 		/*
1186 		 * Ignore the value requested by the host -
1187 		 * always return the pre-configured value based on max_qpairs_allowed.
1188 		 */
1189 		rsp->cdw0 = ctrlr->feat.number_of_queues.raw;
1190 	}
1191 
1192 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1193 }
1194 
1195 static int
1196 spdk_nvmf_ctrlr_set_features_async_event_configuration(struct spdk_nvmf_request *req)
1197 {
1198 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1199 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1200 
1201 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Async Event Configuration, cdw11 0x%08x\n",
1202 		      cmd->cdw11);
1203 	ctrlr->feat.async_event_configuration.raw = cmd->cdw11;
1204 	ctrlr->feat.async_event_configuration.bits.reserved = 0;
1205 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1206 }
1207 
1208 static int
1209 spdk_nvmf_ctrlr_async_event_request(struct spdk_nvmf_request *req)
1210 {
1211 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1212 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
1213 
1214 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Async Event Request\n");
1215 
1216 	/* Only one asynchronous event is supported for now */
1217 	if (ctrlr->aer_req != NULL) {
1218 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "AERL exceeded\n");
1219 		rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
1220 		rsp->status.sc = SPDK_NVME_SC_ASYNC_EVENT_REQUEST_LIMIT_EXCEEDED;
1221 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1222 	}
1223 
1224 	if (ctrlr->notice_event.bits.async_event_type ==
1225 	    SPDK_NVME_ASYNC_EVENT_TYPE_NOTICE) {
1226 		rsp->cdw0 = ctrlr->notice_event.raw;
1227 		ctrlr->notice_event.raw = 0;
1228 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1229 	}
1230 
1231 	ctrlr->aer_req = req;
1232 	return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
1233 }
1234 
1235 static void
1236 spdk_nvmf_get_firmware_slot_log_page(void *buffer, uint64_t offset, uint32_t length)
1237 {
1238 	struct spdk_nvme_firmware_page fw_page;
1239 	size_t copy_len;
1240 
1241 	memset(&fw_page, 0, sizeof(fw_page));
1242 	fw_page.afi.active_slot = 1;
1243 	fw_page.afi.next_reset_slot = 0;
1244 	spdk_strcpy_pad(fw_page.revision[0], FW_VERSION, sizeof(fw_page.revision[0]), ' ');
1245 
1246 	if (offset < sizeof(fw_page)) {
1247 		copy_len = spdk_min(sizeof(fw_page) - offset, length);
1248 		if (copy_len > 0) {
1249 			memcpy(buffer, (const char *)&fw_page + offset, copy_len);
1250 		}
1251 	}
1252 }
1253 
1254 void
1255 spdk_nvmf_ctrlr_ns_changed(struct spdk_nvmf_ctrlr *ctrlr, uint32_t nsid)
1256 {
1257 	uint16_t max_changes = SPDK_COUNTOF(ctrlr->changed_ns_list.ns_list);
1258 	uint16_t i;
1259 	bool found = false;
1260 
1261 	for (i = 0; i < ctrlr->changed_ns_list_count; i++) {
1262 		if (ctrlr->changed_ns_list.ns_list[i] == nsid) {
1263 			/* nsid is already in the list */
1264 			found = true;
1265 			break;
1266 		}
1267 	}
1268 
1269 	if (!found) {
1270 		if (ctrlr->changed_ns_list_count == max_changes) {
1271 			/* Out of space - set first entry to FFFFFFFFh and zero-fill the rest. */
1272 			ctrlr->changed_ns_list.ns_list[0] = 0xFFFFFFFFu;
1273 			for (i = 1; i < max_changes; i++) {
1274 				ctrlr->changed_ns_list.ns_list[i] = 0;
1275 			}
1276 		} else {
1277 			ctrlr->changed_ns_list.ns_list[ctrlr->changed_ns_list_count++] = nsid;
1278 		}
1279 	}
1280 
1281 	spdk_nvmf_ctrlr_async_event_ns_notice(ctrlr);
1282 }
1283 
1284 static void
1285 spdk_nvmf_get_changed_ns_list_log_page(struct spdk_nvmf_ctrlr *ctrlr,
1286 				       void *buffer, uint64_t offset, uint32_t length)
1287 {
1288 	size_t copy_length;
1289 
1290 	if (offset < sizeof(ctrlr->changed_ns_list)) {
1291 		copy_length = spdk_min(length, sizeof(ctrlr->changed_ns_list) - offset);
1292 		if (copy_length) {
1293 			memcpy(buffer, (char *)&ctrlr->changed_ns_list + offset, copy_length);
1294 		}
1295 	}
1296 
1297 	/* Clear log page each time it is read */
1298 	ctrlr->changed_ns_list_count = 0;
1299 	memset(&ctrlr->changed_ns_list, 0, sizeof(ctrlr->changed_ns_list));
1300 }
1301 
1302 /* The structure can be modified if we provide support for other commands in future */
1303 static const struct spdk_nvme_cmds_and_effect_log_page g_cmds_and_effect_log_page = {
1304 	.admin_cmds_supported = {
1305 		/* CSUPP, LBCC, NCC, NIC, CCC, CSE */
1306 		/* Get Log Page */
1307 		[SPDK_NVME_OPC_GET_LOG_PAGE]		= {1, 0, 0, 0, 0, 0, 0, 0},
1308 		/* Identify */
1309 		[SPDK_NVME_OPC_IDENTIFY]		= {1, 0, 0, 0, 0, 0, 0, 0},
1310 		/* Abort */
1311 		[SPDK_NVME_OPC_ABORT]			= {1, 0, 0, 0, 0, 0, 0, 0},
1312 		/* Set Features */
1313 		[SPDK_NVME_OPC_SET_FEATURES]		= {1, 0, 0, 0, 0, 0, 0, 0},
1314 		/* Get Features */
1315 		[SPDK_NVME_OPC_GET_FEATURES]		= {1, 0, 0, 0, 0, 0, 0, 0},
1316 		/* Async Event Request */
1317 		[SPDK_NVME_OPC_ASYNC_EVENT_REQUEST]	= {1, 0, 0, 0, 0, 0, 0, 0},
1318 		/* Keep Alive */
1319 		[SPDK_NVME_OPC_KEEP_ALIVE]		= {1, 0, 0, 0, 0, 0, 0, 0},
1320 	},
1321 	.io_cmds_supported = {
1322 		/* FLUSH */
1323 		[SPDK_NVME_OPC_FLUSH]			= {1, 1, 0, 0, 0, 0, 0, 0},
1324 		/* WRITE */
1325 		[SPDK_NVME_OPC_WRITE]			= {1, 1, 0, 0, 0, 0, 0, 0},
1326 		/* READ */
1327 		[SPDK_NVME_OPC_READ]			= {1, 0, 0, 0, 0, 0, 0, 0},
1328 		/* WRITE ZEROES */
1329 		[SPDK_NVME_OPC_WRITE_ZEROES]		= {1, 1, 0, 0, 0, 0, 0, 0},
1330 		/* DATASET MANAGEMENT */
1331 		[SPDK_NVME_OPC_DATASET_MANAGEMENT]	= {1, 1, 0, 0, 0, 0, 0, 0},
1332 	},
1333 };
1334 
1335 static void
1336 spdk_nvmf_get_cmds_and_effects_log_page(void *buffer,
1337 					uint64_t offset, uint32_t length)
1338 {
1339 	uint32_t page_size = sizeof(struct spdk_nvme_cmds_and_effect_log_page);
1340 	size_t copy_len = 0;
1341 	size_t zero_len = length;
1342 
1343 	if (offset < page_size) {
1344 		copy_len = spdk_min(page_size - offset, length);
1345 		zero_len -= copy_len;
1346 		memcpy(buffer, (char *)(&g_cmds_and_effect_log_page) + offset, copy_len);
1347 	}
1348 
1349 	if (zero_len) {
1350 		memset((char *)buffer + copy_len, 0, zero_len);
1351 	}
1352 }
1353 
1354 static int
1355 spdk_nvmf_ctrlr_get_log_page(struct spdk_nvmf_request *req)
1356 {
1357 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1358 	struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys;
1359 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1360 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
1361 	uint64_t offset, len;
1362 	uint32_t numdl, numdu;
1363 	uint8_t lid;
1364 
1365 	if (req->data == NULL) {
1366 		SPDK_ERRLOG("get log command with no buffer\n");
1367 		response->status.sct = SPDK_NVME_SCT_GENERIC;
1368 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1369 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1370 	}
1371 
1372 	offset = (uint64_t)cmd->cdw12 | ((uint64_t)cmd->cdw13 << 32);
1373 	if (offset & 3) {
1374 		SPDK_ERRLOG("Invalid log page offset 0x%" PRIx64 "\n", offset);
1375 		response->status.sct = SPDK_NVME_SCT_GENERIC;
1376 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1377 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1378 	}
1379 
1380 	numdl = (cmd->cdw10 >> 16) & 0xFFFFu;
1381 	numdu = (cmd->cdw11) & 0xFFFFu;
1382 	len = ((numdu << 16) + numdl + (uint64_t)1) * 4;
1383 	if (len > req->length) {
1384 		SPDK_ERRLOG("Get log page: len (%" PRIu64 ") > buf size (%u)\n",
1385 			    len, req->length);
1386 		response->status.sct = SPDK_NVME_SCT_GENERIC;
1387 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1388 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1389 	}
1390 
1391 	lid = cmd->cdw10 & 0xFF;
1392 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Get log page: LID=0x%02X offset=0x%" PRIx64 " len=0x%" PRIx64 "\n",
1393 		      lid, offset, len);
1394 
1395 	if (subsystem->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) {
1396 		switch (lid) {
1397 		case SPDK_NVME_LOG_DISCOVERY:
1398 			spdk_nvmf_get_discovery_log_page(subsystem->tgt, req->iov, req->iovcnt, offset, len);
1399 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1400 		default:
1401 			goto invalid_log_page;
1402 		}
1403 	} else {
1404 		switch (lid) {
1405 		case SPDK_NVME_LOG_ERROR:
1406 		case SPDK_NVME_LOG_HEALTH_INFORMATION:
1407 			/* TODO: actually fill out log page data */
1408 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1409 		case SPDK_NVME_LOG_FIRMWARE_SLOT:
1410 			spdk_nvmf_get_firmware_slot_log_page(req->data, offset, len);
1411 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1412 		case SPDK_NVME_LOG_COMMAND_EFFECTS_LOG:
1413 			spdk_nvmf_get_cmds_and_effects_log_page(req->data, offset, len);
1414 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1415 		case SPDK_NVME_LOG_CHANGED_NS_LIST:
1416 			spdk_nvmf_get_changed_ns_list_log_page(ctrlr, req->data, offset, len);
1417 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1418 		default:
1419 			goto invalid_log_page;
1420 		}
1421 	}
1422 
1423 invalid_log_page:
1424 	SPDK_ERRLOG("Unsupported Get Log Page 0x%02X\n", lid);
1425 	response->status.sct = SPDK_NVME_SCT_GENERIC;
1426 	response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1427 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1428 }
1429 
1430 static int
1431 spdk_nvmf_ctrlr_identify_ns(struct spdk_nvmf_ctrlr *ctrlr,
1432 			    struct spdk_nvme_cmd *cmd,
1433 			    struct spdk_nvme_cpl *rsp,
1434 			    struct spdk_nvme_ns_data *nsdata)
1435 {
1436 	struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys;
1437 	struct spdk_nvmf_ns *ns;
1438 	uint32_t max_num_blocks;
1439 
1440 	if (cmd->nsid == 0 || cmd->nsid > subsystem->max_nsid) {
1441 		SPDK_ERRLOG("Identify Namespace for invalid NSID %u\n", cmd->nsid);
1442 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
1443 		rsp->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT;
1444 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1445 	}
1446 
1447 	ns = _spdk_nvmf_subsystem_get_ns(subsystem, cmd->nsid);
1448 	if (ns == NULL || ns->bdev == NULL) {
1449 		/*
1450 		 * Inactive namespaces should return a zero filled data structure.
1451 		 * The data buffer is already zeroed by spdk_nvmf_ctrlr_process_admin_cmd(),
1452 		 * so we can just return early here.
1453 		 */
1454 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Identify Namespace for inactive NSID %u\n", cmd->nsid);
1455 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
1456 		rsp->status.sc = SPDK_NVME_SC_SUCCESS;
1457 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1458 	}
1459 
1460 	spdk_nvmf_bdev_ctrlr_identify_ns(ns, nsdata);
1461 
1462 	/* Due to bug in the Linux kernel NVMe driver we have to set noiob no larger than mdts */
1463 	max_num_blocks = ctrlr->admin_qpair->transport->opts.max_io_size /
1464 			 (1U << nsdata->lbaf[nsdata->flbas.format].lbads);
1465 	if (nsdata->noiob > max_num_blocks) {
1466 		nsdata->noiob = max_num_blocks;
1467 	}
1468 
1469 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1470 }
1471 
1472 static int
1473 spdk_nvmf_ctrlr_identify_ctrlr(struct spdk_nvmf_ctrlr *ctrlr, struct spdk_nvme_ctrlr_data *cdata)
1474 {
1475 	struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys;
1476 	struct spdk_nvmf_transport *transport = ctrlr->admin_qpair->transport;
1477 
1478 	/*
1479 	 * Common fields for discovery and NVM subsystems
1480 	 */
1481 	spdk_strcpy_pad(cdata->fr, FW_VERSION, sizeof(cdata->fr), ' ');
1482 	assert((transport->opts.max_io_size % 4096) == 0);
1483 	cdata->mdts = spdk_u32log2(transport->opts.max_io_size / 4096);
1484 	cdata->cntlid = ctrlr->cntlid;
1485 	cdata->ver = ctrlr->vcprop.vs;
1486 	cdata->lpa.edlp = 1;
1487 	cdata->elpe = 127;
1488 	cdata->maxcmd = transport->opts.max_queue_depth;
1489 	cdata->sgls.supported = 1;
1490 	cdata->sgls.keyed_sgl = 1;
1491 	cdata->sgls.sgl_offset = 1;
1492 	spdk_strcpy_pad(cdata->subnqn, subsystem->subnqn, sizeof(cdata->subnqn), '\0');
1493 
1494 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ctrlr data: maxcmd 0x%x\n", cdata->maxcmd);
1495 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "sgls data: 0x%x\n", from_le32(&cdata->sgls));
1496 
1497 	/*
1498 	 * NVM subsystem fields (reserved for discovery subsystems)
1499 	 */
1500 	if (subsystem->subtype == SPDK_NVMF_SUBTYPE_NVME) {
1501 		spdk_strcpy_pad(cdata->mn, MODEL_NUMBER, sizeof(cdata->mn), ' ');
1502 		spdk_strcpy_pad(cdata->sn, spdk_nvmf_subsystem_get_sn(subsystem), sizeof(cdata->sn), ' ');
1503 		cdata->kas = KAS_DEFAULT_VALUE;
1504 
1505 		cdata->rab = 6;
1506 		cdata->cmic.multi_port = 1;
1507 		cdata->cmic.multi_host = 1;
1508 		cdata->oaes.ns_attribute_notices = 1;
1509 		cdata->ctratt.host_id_exhid_supported = 1;
1510 		cdata->aerl = 0;
1511 		cdata->frmw.slot1_ro = 1;
1512 		cdata->frmw.num_slots = 1;
1513 
1514 		cdata->lpa.celp = 1; /* Command Effects log page supported */
1515 
1516 		cdata->sqes.min = 6;
1517 		cdata->sqes.max = 6;
1518 		cdata->cqes.min = 4;
1519 		cdata->cqes.max = 4;
1520 		cdata->nn = subsystem->max_nsid;
1521 		cdata->vwc.present = 1;
1522 		cdata->vwc.flush_broadcast = SPDK_NVME_FLUSH_BROADCAST_NOT_SUPPORTED;
1523 
1524 		cdata->nvmf_specific.ioccsz = sizeof(struct spdk_nvme_cmd) / 16;
1525 		cdata->nvmf_specific.iorcsz = sizeof(struct spdk_nvme_cpl) / 16;
1526 		cdata->nvmf_specific.icdoff = 0; /* offset starts directly after SQE */
1527 		cdata->nvmf_specific.ctrattr.ctrlr_model = SPDK_NVMF_CTRLR_MODEL_DYNAMIC;
1528 		cdata->nvmf_specific.msdbd = 1; /* target supports single SGL in capsule */
1529 
1530 		/* TODO: this should be set by the transport */
1531 		cdata->nvmf_specific.ioccsz += transport->opts.in_capsule_data_size / 16;
1532 
1533 		cdata->oncs.dsm = spdk_nvmf_ctrlr_dsm_supported(ctrlr);
1534 		cdata->oncs.write_zeroes = spdk_nvmf_ctrlr_write_zeroes_supported(ctrlr);
1535 		cdata->oncs.reservations = 1;
1536 
1537 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ext ctrlr data: ioccsz 0x%x\n",
1538 			      cdata->nvmf_specific.ioccsz);
1539 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ext ctrlr data: iorcsz 0x%x\n",
1540 			      cdata->nvmf_specific.iorcsz);
1541 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ext ctrlr data: icdoff 0x%x\n",
1542 			      cdata->nvmf_specific.icdoff);
1543 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ext ctrlr data: ctrattr 0x%x\n",
1544 			      *(uint8_t *)&cdata->nvmf_specific.ctrattr);
1545 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ext ctrlr data: msdbd 0x%x\n",
1546 			      cdata->nvmf_specific.msdbd);
1547 	}
1548 
1549 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1550 }
1551 
1552 static int
1553 spdk_nvmf_ctrlr_identify_active_ns_list(struct spdk_nvmf_subsystem *subsystem,
1554 					struct spdk_nvme_cmd *cmd,
1555 					struct spdk_nvme_cpl *rsp,
1556 					struct spdk_nvme_ns_list *ns_list)
1557 {
1558 	struct spdk_nvmf_ns *ns;
1559 	uint32_t count = 0;
1560 
1561 	if (cmd->nsid >= 0xfffffffeUL) {
1562 		SPDK_ERRLOG("Identify Active Namespace List with invalid NSID %u\n", cmd->nsid);
1563 		rsp->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT;
1564 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1565 	}
1566 
1567 	for (ns = spdk_nvmf_subsystem_get_first_ns(subsystem); ns != NULL;
1568 	     ns = spdk_nvmf_subsystem_get_next_ns(subsystem, ns)) {
1569 		if (ns->opts.nsid <= cmd->nsid) {
1570 			continue;
1571 		}
1572 
1573 		ns_list->ns_list[count++] = ns->opts.nsid;
1574 		if (count == SPDK_COUNTOF(ns_list->ns_list)) {
1575 			break;
1576 		}
1577 	}
1578 
1579 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1580 }
1581 
1582 static void
1583 _add_ns_id_desc(void **buf_ptr, size_t *buf_remain,
1584 		enum spdk_nvme_nidt type,
1585 		const void *data, size_t data_size)
1586 {
1587 	struct spdk_nvme_ns_id_desc *desc;
1588 	size_t desc_size = sizeof(*desc) + data_size;
1589 
1590 	/*
1591 	 * These should never fail in practice, since all valid NS ID descriptors
1592 	 * should be defined so that they fit in the available 4096-byte buffer.
1593 	 */
1594 	assert(data_size > 0);
1595 	assert(data_size <= UINT8_MAX);
1596 	assert(desc_size < *buf_remain);
1597 	if (data_size == 0 || data_size > UINT8_MAX || desc_size > *buf_remain) {
1598 		return;
1599 	}
1600 
1601 	desc = *buf_ptr;
1602 	desc->nidt = type;
1603 	desc->nidl = data_size;
1604 	memcpy(desc->nid, data, data_size);
1605 
1606 	*buf_ptr += desc_size;
1607 	*buf_remain -= desc_size;
1608 }
1609 
1610 static int
1611 spdk_nvmf_ctrlr_identify_ns_id_descriptor_list(
1612 	struct spdk_nvmf_subsystem *subsystem,
1613 	struct spdk_nvme_cmd *cmd,
1614 	struct spdk_nvme_cpl *rsp,
1615 	void *id_desc_list, size_t id_desc_list_size)
1616 {
1617 	struct spdk_nvmf_ns *ns;
1618 	size_t buf_remain = id_desc_list_size;
1619 	void *buf_ptr = id_desc_list;
1620 
1621 	ns = _spdk_nvmf_subsystem_get_ns(subsystem, cmd->nsid);
1622 	if (ns == NULL || ns->bdev == NULL) {
1623 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
1624 		rsp->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT;
1625 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1626 	}
1627 
1628 #define ADD_ID_DESC(type, data, size) \
1629 	do { \
1630 		if (!spdk_mem_all_zero(data, size)) { \
1631 			_add_ns_id_desc(&buf_ptr, &buf_remain, type, data, size); \
1632 		} \
1633 	} while (0)
1634 
1635 	ADD_ID_DESC(SPDK_NVME_NIDT_EUI64, ns->opts.eui64, sizeof(ns->opts.eui64));
1636 	ADD_ID_DESC(SPDK_NVME_NIDT_NGUID, ns->opts.nguid, sizeof(ns->opts.nguid));
1637 	ADD_ID_DESC(SPDK_NVME_NIDT_UUID, &ns->opts.uuid, sizeof(ns->opts.uuid));
1638 
1639 	/*
1640 	 * The list is automatically 0-terminated because controller to host buffers in
1641 	 * admin commands always get zeroed in spdk_nvmf_ctrlr_process_admin_cmd().
1642 	 */
1643 
1644 #undef ADD_ID_DESC
1645 
1646 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1647 }
1648 
1649 static int
1650 spdk_nvmf_ctrlr_identify(struct spdk_nvmf_request *req)
1651 {
1652 	uint8_t cns;
1653 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1654 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1655 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
1656 	struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys;
1657 
1658 	if (req->data == NULL || req->length < 4096) {
1659 		SPDK_ERRLOG("identify command with invalid buffer\n");
1660 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
1661 		rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1662 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1663 	}
1664 
1665 	cns = cmd->cdw10 & 0xFF;
1666 
1667 	if (subsystem->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY &&
1668 	    cns != SPDK_NVME_IDENTIFY_CTRLR) {
1669 		/* Discovery controllers only support Identify Controller */
1670 		goto invalid_cns;
1671 	}
1672 
1673 	switch (cns) {
1674 	case SPDK_NVME_IDENTIFY_NS:
1675 		return spdk_nvmf_ctrlr_identify_ns(ctrlr, cmd, rsp, req->data);
1676 	case SPDK_NVME_IDENTIFY_CTRLR:
1677 		return spdk_nvmf_ctrlr_identify_ctrlr(ctrlr, req->data);
1678 	case SPDK_NVME_IDENTIFY_ACTIVE_NS_LIST:
1679 		return spdk_nvmf_ctrlr_identify_active_ns_list(subsystem, cmd, rsp, req->data);
1680 	case SPDK_NVME_IDENTIFY_NS_ID_DESCRIPTOR_LIST:
1681 		return spdk_nvmf_ctrlr_identify_ns_id_descriptor_list(subsystem, cmd, rsp, req->data, req->length);
1682 	default:
1683 		goto invalid_cns;
1684 	}
1685 
1686 invalid_cns:
1687 	SPDK_ERRLOG("Identify command with unsupported CNS 0x%02x\n", cns);
1688 	rsp->status.sct = SPDK_NVME_SCT_GENERIC;
1689 	rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1690 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1691 }
1692 
1693 
1694 static struct spdk_nvmf_request *
1695 spdk_nvmf_qpair_abort(struct spdk_nvmf_qpair *qpair, uint16_t cid)
1696 {
1697 	struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr;
1698 	struct spdk_nvmf_request *req;
1699 
1700 	if (spdk_nvmf_qpair_is_admin_queue(qpair)) {
1701 		if (ctrlr->aer_req && ctrlr->aer_req->cmd->nvme_cmd.cid == cid) {
1702 			SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Aborting AER request\n");
1703 			req = ctrlr->aer_req;
1704 			ctrlr->aer_req = NULL;
1705 			return req;
1706 		}
1707 	}
1708 
1709 	/* TODO: track list of outstanding requests in qpair? */
1710 	return NULL;
1711 }
1712 
1713 static void
1714 spdk_nvmf_ctrlr_abort_done(struct spdk_io_channel_iter *i, int status)
1715 {
1716 	struct spdk_nvmf_request *req = spdk_io_channel_iter_get_ctx(i);
1717 
1718 	spdk_nvmf_request_complete(req);
1719 }
1720 
1721 static void
1722 spdk_nvmf_ctrlr_abort_on_pg(struct spdk_io_channel_iter *i)
1723 {
1724 	struct spdk_nvmf_request *req = spdk_io_channel_iter_get_ctx(i);
1725 	struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
1726 	struct spdk_nvmf_poll_group *group = spdk_io_channel_get_ctx(ch);
1727 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
1728 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1729 	uint16_t sqid = cmd->cdw10 & 0xFFFFu;
1730 	struct spdk_nvmf_qpair *qpair;
1731 
1732 	TAILQ_FOREACH(qpair, &group->qpairs, link) {
1733 		if (qpair->ctrlr == req->qpair->ctrlr && qpair->qid == sqid) {
1734 			struct spdk_nvmf_request *req_to_abort;
1735 			uint16_t cid = cmd->cdw10 >> 16;
1736 
1737 			/* Found the qpair */
1738 
1739 			req_to_abort = spdk_nvmf_qpair_abort(qpair, cid);
1740 			if (req_to_abort == NULL) {
1741 				SPDK_DEBUGLOG(SPDK_LOG_NVMF, "cid %u not found\n", cid);
1742 				rsp->status.sct = SPDK_NVME_SCT_GENERIC;
1743 				rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1744 				spdk_for_each_channel_continue(i, -EINVAL);
1745 				return;
1746 			}
1747 
1748 			/* Complete the request with aborted status */
1749 			req_to_abort->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
1750 			req_to_abort->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_ABORTED_BY_REQUEST;
1751 			spdk_nvmf_request_complete(req_to_abort);
1752 
1753 			SPDK_DEBUGLOG(SPDK_LOG_NVMF, "abort ctrlr=%p req=%p sqid=%u cid=%u successful\n",
1754 				      qpair->ctrlr, req_to_abort, sqid, cid);
1755 			rsp->cdw0 = 0; /* Command successfully aborted */
1756 			rsp->status.sct = SPDK_NVME_SCT_GENERIC;
1757 			rsp->status.sc = SPDK_NVME_SC_SUCCESS;
1758 			/* Return -1 for the status so the iteration across threads stops. */
1759 			spdk_for_each_channel_continue(i, -1);
1760 
1761 		}
1762 	}
1763 
1764 	spdk_for_each_channel_continue(i, 0);
1765 }
1766 
1767 static int
1768 spdk_nvmf_ctrlr_abort(struct spdk_nvmf_request *req)
1769 {
1770 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
1771 
1772 	rsp->cdw0 = 1; /* Command not aborted */
1773 	rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
1774 	rsp->status.sc = SPDK_NVME_SC_INVALID_QUEUE_IDENTIFIER;
1775 
1776 	/* Send a message to each poll group, searching for this ctrlr, sqid, and command. */
1777 	spdk_for_each_channel(req->qpair->ctrlr->subsys->tgt,
1778 			      spdk_nvmf_ctrlr_abort_on_pg,
1779 			      req,
1780 			      spdk_nvmf_ctrlr_abort_done
1781 			     );
1782 
1783 	return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
1784 }
1785 
1786 static int
1787 get_features_generic(struct spdk_nvmf_request *req, uint32_t cdw0)
1788 {
1789 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
1790 
1791 	rsp->cdw0 = cdw0;
1792 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1793 }
1794 
1795 static int
1796 spdk_nvmf_ctrlr_get_features(struct spdk_nvmf_request *req)
1797 {
1798 	uint8_t feature;
1799 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1800 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1801 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
1802 
1803 	feature = cmd->cdw10 & 0xff; /* mask out the FID value */
1804 	switch (feature) {
1805 	case SPDK_NVME_FEAT_ARBITRATION:
1806 		return get_features_generic(req, ctrlr->feat.arbitration.raw);
1807 	case SPDK_NVME_FEAT_POWER_MANAGEMENT:
1808 		return get_features_generic(req, ctrlr->feat.power_management.raw);
1809 	case SPDK_NVME_FEAT_TEMPERATURE_THRESHOLD:
1810 		return spdk_nvmf_ctrlr_get_features_temperature_threshold(req);
1811 	case SPDK_NVME_FEAT_ERROR_RECOVERY:
1812 		return get_features_generic(req, ctrlr->feat.error_recovery.raw);
1813 	case SPDK_NVME_FEAT_VOLATILE_WRITE_CACHE:
1814 		return get_features_generic(req, ctrlr->feat.volatile_write_cache.raw);
1815 	case SPDK_NVME_FEAT_NUMBER_OF_QUEUES:
1816 		return get_features_generic(req, ctrlr->feat.number_of_queues.raw);
1817 	case SPDK_NVME_FEAT_WRITE_ATOMICITY:
1818 		return get_features_generic(req, ctrlr->feat.write_atomicity.raw);
1819 	case SPDK_NVME_FEAT_ASYNC_EVENT_CONFIGURATION:
1820 		return get_features_generic(req, ctrlr->feat.async_event_configuration.raw);
1821 	case SPDK_NVME_FEAT_KEEP_ALIVE_TIMER:
1822 		return get_features_generic(req, ctrlr->feat.keep_alive_timer.raw);
1823 	case SPDK_NVME_FEAT_HOST_IDENTIFIER:
1824 		return spdk_nvmf_ctrlr_get_features_host_identifier(req);
1825 	case SPDK_NVME_FEAT_HOST_RESERVE_MASK:
1826 		return spdk_nvmf_ctrlr_get_features_reservation_notification_mask(req);
1827 	case SPDK_NVME_FEAT_HOST_RESERVE_PERSIST:
1828 		return spdk_nvmf_ctrlr_get_features_reservation_persistence(req);
1829 	default:
1830 		SPDK_ERRLOG("Get Features command with unsupported feature ID 0x%02x\n", feature);
1831 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1832 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1833 	}
1834 }
1835 
1836 static int
1837 spdk_nvmf_ctrlr_set_features(struct spdk_nvmf_request *req)
1838 {
1839 	uint8_t feature;
1840 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1841 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
1842 
1843 	feature = cmd->cdw10 & 0xff; /* mask out the FID value */
1844 	switch (feature) {
1845 	case SPDK_NVME_FEAT_ARBITRATION:
1846 		return spdk_nvmf_ctrlr_set_features_arbitration(req);
1847 	case SPDK_NVME_FEAT_POWER_MANAGEMENT:
1848 		return spdk_nvmf_ctrlr_set_features_power_management(req);
1849 	case SPDK_NVME_FEAT_TEMPERATURE_THRESHOLD:
1850 		return spdk_nvmf_ctrlr_set_features_temperature_threshold(req);
1851 	case SPDK_NVME_FEAT_ERROR_RECOVERY:
1852 		return spdk_nvmf_ctrlr_set_features_error_recovery(req);
1853 	case SPDK_NVME_FEAT_VOLATILE_WRITE_CACHE:
1854 		return spdk_nvmf_ctrlr_set_features_volatile_write_cache(req);
1855 	case SPDK_NVME_FEAT_NUMBER_OF_QUEUES:
1856 		return spdk_nvmf_ctrlr_set_features_number_of_queues(req);
1857 	case SPDK_NVME_FEAT_WRITE_ATOMICITY:
1858 		return spdk_nvmf_ctrlr_set_features_write_atomicity(req);
1859 	case SPDK_NVME_FEAT_ASYNC_EVENT_CONFIGURATION:
1860 		return spdk_nvmf_ctrlr_set_features_async_event_configuration(req);
1861 	case SPDK_NVME_FEAT_KEEP_ALIVE_TIMER:
1862 		return spdk_nvmf_ctrlr_set_features_keep_alive_timer(req);
1863 	case SPDK_NVME_FEAT_HOST_IDENTIFIER:
1864 		return spdk_nvmf_ctrlr_set_features_host_identifier(req);
1865 	case SPDK_NVME_FEAT_HOST_RESERVE_MASK:
1866 		return spdk_nvmf_ctrlr_set_features_reservation_notification_mask(req);
1867 	case SPDK_NVME_FEAT_HOST_RESERVE_PERSIST:
1868 		return spdk_nvmf_ctrlr_set_features_reservation_persistence(req);
1869 	default:
1870 		SPDK_ERRLOG("Set Features command with unsupported feature ID 0x%02x\n", feature);
1871 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1872 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1873 	}
1874 }
1875 
1876 static int
1877 spdk_nvmf_ctrlr_keep_alive(struct spdk_nvmf_request *req)
1878 {
1879 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1880 
1881 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Keep Alive\n");
1882 	/*
1883 	 * To handle keep alive just clear or reset the
1884 	 * ctrlr based keep alive duration counter.
1885 	 * When added, a separate timer based process
1886 	 * will monitor if the time since last recorded
1887 	 * keep alive has exceeded the max duration and
1888 	 * take appropriate action.
1889 	 */
1890 	ctrlr->last_keep_alive_tick = spdk_get_ticks();
1891 
1892 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1893 }
1894 
1895 int
1896 spdk_nvmf_ctrlr_process_admin_cmd(struct spdk_nvmf_request *req)
1897 {
1898 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1899 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1900 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
1901 
1902 	if (ctrlr == NULL) {
1903 		SPDK_ERRLOG("Admin command sent before CONNECT\n");
1904 		response->status.sct = SPDK_NVME_SCT_GENERIC;
1905 		response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
1906 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1907 	}
1908 
1909 	if (ctrlr->vcprop.cc.bits.en != 1) {
1910 		SPDK_ERRLOG("Admin command sent to disabled controller\n");
1911 		response->status.sct = SPDK_NVME_SCT_GENERIC;
1912 		response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
1913 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1914 	}
1915 
1916 	if (req->data && spdk_nvme_opc_get_data_transfer(cmd->opc) == SPDK_NVME_DATA_CONTROLLER_TO_HOST) {
1917 		memset(req->data, 0, req->length);
1918 	}
1919 
1920 	if (ctrlr->subsys->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) {
1921 		/* Discovery controllers only support Get Log Page and Identify */
1922 		switch (cmd->opc) {
1923 		case SPDK_NVME_OPC_IDENTIFY:
1924 		case SPDK_NVME_OPC_GET_LOG_PAGE:
1925 			break;
1926 		default:
1927 			goto invalid_opcode;
1928 		}
1929 	}
1930 
1931 	switch (cmd->opc) {
1932 	case SPDK_NVME_OPC_GET_LOG_PAGE:
1933 		return spdk_nvmf_ctrlr_get_log_page(req);
1934 	case SPDK_NVME_OPC_IDENTIFY:
1935 		return spdk_nvmf_ctrlr_identify(req);
1936 	case SPDK_NVME_OPC_ABORT:
1937 		return spdk_nvmf_ctrlr_abort(req);
1938 	case SPDK_NVME_OPC_GET_FEATURES:
1939 		return spdk_nvmf_ctrlr_get_features(req);
1940 	case SPDK_NVME_OPC_SET_FEATURES:
1941 		return spdk_nvmf_ctrlr_set_features(req);
1942 	case SPDK_NVME_OPC_ASYNC_EVENT_REQUEST:
1943 		return spdk_nvmf_ctrlr_async_event_request(req);
1944 	case SPDK_NVME_OPC_KEEP_ALIVE:
1945 		return spdk_nvmf_ctrlr_keep_alive(req);
1946 
1947 	case SPDK_NVME_OPC_CREATE_IO_SQ:
1948 	case SPDK_NVME_OPC_CREATE_IO_CQ:
1949 	case SPDK_NVME_OPC_DELETE_IO_SQ:
1950 	case SPDK_NVME_OPC_DELETE_IO_CQ:
1951 		/* Create and Delete I/O CQ/SQ not allowed in NVMe-oF */
1952 		goto invalid_opcode;
1953 
1954 	default:
1955 		goto invalid_opcode;
1956 	}
1957 
1958 invalid_opcode:
1959 	SPDK_ERRLOG("Unsupported admin opcode 0x%x\n", cmd->opc);
1960 	response->status.sct = SPDK_NVME_SCT_GENERIC;
1961 	response->status.sc = SPDK_NVME_SC_INVALID_OPCODE;
1962 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1963 }
1964 
1965 int
1966 spdk_nvmf_ctrlr_process_fabrics_cmd(struct spdk_nvmf_request *req)
1967 {
1968 	struct spdk_nvmf_qpair *qpair = req->qpair;
1969 	struct spdk_nvmf_capsule_cmd *cap_hdr;
1970 
1971 	cap_hdr = &req->cmd->nvmf_cmd;
1972 
1973 	if (qpair->ctrlr == NULL) {
1974 		/* No ctrlr established yet; the only valid command is Connect */
1975 		if (cap_hdr->fctype == SPDK_NVMF_FABRIC_COMMAND_CONNECT) {
1976 			return spdk_nvmf_ctrlr_connect(req);
1977 		} else {
1978 			SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Got fctype 0x%x, expected Connect\n",
1979 				      cap_hdr->fctype);
1980 			req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
1981 			req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
1982 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1983 		}
1984 	} else if (spdk_nvmf_qpair_is_admin_queue(qpair)) {
1985 		/*
1986 		 * Controller session is established, and this is an admin queue.
1987 		 * Disallow Connect and allow other fabrics commands.
1988 		 */
1989 		switch (cap_hdr->fctype) {
1990 		case SPDK_NVMF_FABRIC_COMMAND_PROPERTY_SET:
1991 			return spdk_nvmf_property_set(req);
1992 		case SPDK_NVMF_FABRIC_COMMAND_PROPERTY_GET:
1993 			return spdk_nvmf_property_get(req);
1994 		default:
1995 			SPDK_DEBUGLOG(SPDK_LOG_NVMF, "unknown fctype 0x%02x\n",
1996 				      cap_hdr->fctype);
1997 			req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
1998 			req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_INVALID_OPCODE;
1999 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2000 		}
2001 	} else {
2002 		/* Controller session is established, and this is an I/O queue */
2003 		/* For now, no I/O-specific Fabrics commands are implemented (other than Connect) */
2004 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Unexpected I/O fctype 0x%x\n", cap_hdr->fctype);
2005 		req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
2006 		req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_INVALID_OPCODE;
2007 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2008 	}
2009 }
2010 
2011 int
2012 spdk_nvmf_ctrlr_async_event_ns_notice(struct spdk_nvmf_ctrlr *ctrlr)
2013 {
2014 	struct spdk_nvmf_request *req;
2015 	struct spdk_nvme_cpl *rsp;
2016 	union spdk_nvme_async_event_completion event = {0};
2017 
2018 	/* Users may disable the event notification */
2019 	if (!ctrlr->feat.async_event_configuration.bits.ns_attr_notice) {
2020 		return 0;
2021 	}
2022 
2023 	event.bits.async_event_type = SPDK_NVME_ASYNC_EVENT_TYPE_NOTICE;
2024 	event.bits.async_event_info = SPDK_NVME_ASYNC_EVENT_NS_ATTR_CHANGED;
2025 	event.bits.log_page_identifier = SPDK_NVME_LOG_CHANGED_NS_LIST;
2026 
2027 	/* If there is no outstanding AER request, queue the event.  Then
2028 	 * if an AER is later submitted, this event can be sent as a
2029 	 * response.
2030 	 */
2031 	if (!ctrlr->aer_req) {
2032 		if (ctrlr->notice_event.bits.async_event_type ==
2033 		    SPDK_NVME_ASYNC_EVENT_TYPE_NOTICE) {
2034 			return 0;
2035 		}
2036 
2037 		ctrlr->notice_event.raw = event.raw;
2038 		return 0;
2039 	}
2040 
2041 	req = ctrlr->aer_req;
2042 	rsp = &req->rsp->nvme_cpl;
2043 
2044 	rsp->cdw0 = event.raw;
2045 
2046 	spdk_nvmf_request_complete(req);
2047 	ctrlr->aer_req = NULL;
2048 
2049 	return 0;
2050 }
2051 
2052 void
2053 spdk_nvmf_qpair_free_aer(struct spdk_nvmf_qpair *qpair)
2054 {
2055 	struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr;
2056 
2057 	if (!spdk_nvmf_qpair_is_admin_queue(qpair)) {
2058 		return;
2059 	}
2060 
2061 	if (ctrlr->aer_req != NULL) {
2062 		spdk_nvmf_request_free(ctrlr->aer_req);
2063 		ctrlr->aer_req = NULL;
2064 	}
2065 }
2066 
2067 void
2068 spdk_nvmf_ctrlr_abort_aer(struct spdk_nvmf_ctrlr *ctrlr)
2069 {
2070 	if (!ctrlr->aer_req) {
2071 		return;
2072 	}
2073 
2074 	spdk_nvmf_request_complete(ctrlr->aer_req);
2075 	ctrlr->aer_req = NULL;
2076 }
2077 
2078 /* Check from subsystem poll group's namespace information data structure */
2079 static bool
2080 nvmf_ns_info_ctrlr_is_registrant(struct spdk_nvmf_subsystem_pg_ns_info *ns_info,
2081 				 struct spdk_nvmf_ctrlr *ctrlr)
2082 {
2083 	uint32_t i;
2084 
2085 	for (i = 0; i < SPDK_NVMF_MAX_NUM_REGISTRANTS; i++) {
2086 		if (!spdk_uuid_compare(&ns_info->reg_hostid[i], &ctrlr->hostid)) {
2087 			return true;
2088 		}
2089 	}
2090 
2091 	return false;
2092 }
2093 
2094 /*
2095  * Check the NVMe command is permitted or not for current controller(Host).
2096  */
2097 static int
2098 nvmf_ns_reservation_request_check(struct spdk_nvmf_subsystem_pg_ns_info *ns_info,
2099 				  struct spdk_nvmf_ctrlr *ctrlr,
2100 				  struct spdk_nvmf_request *req)
2101 {
2102 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
2103 	enum spdk_nvme_reservation_type rtype = ns_info->rtype;
2104 	uint8_t status = SPDK_NVME_SC_SUCCESS;
2105 	uint8_t racqa;
2106 	bool is_registrant;
2107 
2108 	/* No valid reservation */
2109 	if (!rtype) {
2110 		return 0;
2111 	}
2112 
2113 	is_registrant = nvmf_ns_info_ctrlr_is_registrant(ns_info, ctrlr);
2114 	/* All registrants type and current ctrlr is a valid registrant */
2115 	if ((rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_ALL_REGS ||
2116 	     rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_ALL_REGS) && is_registrant) {
2117 		return 0;
2118 	} else if (!spdk_uuid_compare(&ns_info->holder_id, &ctrlr->hostid)) {
2119 		return 0;
2120 	}
2121 
2122 	/* Non-holder for current controller */
2123 	switch (cmd->opc) {
2124 	case SPDK_NVME_OPC_READ:
2125 	case SPDK_NVME_OPC_COMPARE:
2126 		if (rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS) {
2127 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2128 			goto exit;
2129 		}
2130 		if ((rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_REG_ONLY ||
2131 		     rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_ALL_REGS) && !is_registrant) {
2132 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2133 			goto exit;
2134 		}
2135 		break;
2136 	case SPDK_NVME_OPC_FLUSH:
2137 	case SPDK_NVME_OPC_WRITE:
2138 	case SPDK_NVME_OPC_WRITE_UNCORRECTABLE:
2139 	case SPDK_NVME_OPC_WRITE_ZEROES:
2140 	case SPDK_NVME_OPC_DATASET_MANAGEMENT:
2141 		if (rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE ||
2142 		    rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS) {
2143 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2144 			goto exit;
2145 		}
2146 		if (!is_registrant) {
2147 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2148 			goto exit;
2149 		}
2150 		break;
2151 	case SPDK_NVME_OPC_RESERVATION_ACQUIRE:
2152 	case SPDK_NVME_OPC_RESERVATION_RELEASE:
2153 		racqa = cmd->cdw10 & 0x7u;
2154 		if (cmd->opc == SPDK_NVME_OPC_RESERVATION_ACQUIRE &&
2155 		    racqa == SPDK_NVME_RESERVE_ACQUIRE) {
2156 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2157 			goto exit;
2158 		}
2159 		if (!is_registrant) {
2160 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2161 			goto exit;
2162 		}
2163 		break;
2164 	default:
2165 		break;
2166 	}
2167 
2168 exit:
2169 	req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
2170 	req->rsp->nvme_cpl.status.sc = status;
2171 	if (status == SPDK_NVME_SC_RESERVATION_CONFLICT) {
2172 		return -EPERM;
2173 	}
2174 
2175 	return 0;
2176 }
2177 
2178 int
2179 spdk_nvmf_ctrlr_process_io_cmd(struct spdk_nvmf_request *req)
2180 {
2181 	uint32_t nsid;
2182 	struct spdk_nvmf_ns *ns;
2183 	struct spdk_bdev *bdev;
2184 	struct spdk_bdev_desc *desc;
2185 	struct spdk_io_channel *ch;
2186 	struct spdk_nvmf_poll_group *group = req->qpair->group;
2187 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
2188 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
2189 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
2190 	struct spdk_nvmf_subsystem_pg_ns_info *ns_info;
2191 
2192 	/* pre-set response details for this command */
2193 	response->status.sc = SPDK_NVME_SC_SUCCESS;
2194 	nsid = cmd->nsid;
2195 
2196 	if (spdk_unlikely(ctrlr == NULL)) {
2197 		SPDK_ERRLOG("I/O command sent before CONNECT\n");
2198 		response->status.sct = SPDK_NVME_SCT_GENERIC;
2199 		response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
2200 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2201 	}
2202 
2203 	if (spdk_unlikely(ctrlr->vcprop.cc.bits.en != 1)) {
2204 		SPDK_ERRLOG("I/O command sent to disabled controller\n");
2205 		response->status.sct = SPDK_NVME_SCT_GENERIC;
2206 		response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
2207 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2208 	}
2209 
2210 	ns = _spdk_nvmf_subsystem_get_ns(ctrlr->subsys, nsid);
2211 	if (ns == NULL || ns->bdev == NULL) {
2212 		SPDK_ERRLOG("Unsuccessful query for nsid %u\n", cmd->nsid);
2213 		response->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT;
2214 		response->status.dnr = 1;
2215 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2216 	}
2217 
2218 	ns_info = &group->sgroups[ctrlr->subsys->id].ns_info[nsid - 1];
2219 	if (nvmf_ns_reservation_request_check(ns_info, ctrlr, req)) {
2220 		SPDK_NOTICELOG("Reservation Conflict for nsid %u, opcode %u\n", cmd->nsid, cmd->opc);
2221 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2222 	}
2223 
2224 	bdev = ns->bdev;
2225 	desc = ns->desc;
2226 	ch = ns_info->channel;
2227 	switch (cmd->opc) {
2228 	case SPDK_NVME_OPC_READ:
2229 		return spdk_nvmf_bdev_ctrlr_read_cmd(bdev, desc, ch, req);
2230 	case SPDK_NVME_OPC_WRITE:
2231 		return spdk_nvmf_bdev_ctrlr_write_cmd(bdev, desc, ch, req);
2232 	case SPDK_NVME_OPC_WRITE_ZEROES:
2233 		return spdk_nvmf_bdev_ctrlr_write_zeroes_cmd(bdev, desc, ch, req);
2234 	case SPDK_NVME_OPC_FLUSH:
2235 		return spdk_nvmf_bdev_ctrlr_flush_cmd(bdev, desc, ch, req);
2236 	case SPDK_NVME_OPC_DATASET_MANAGEMENT:
2237 		return spdk_nvmf_bdev_ctrlr_dsm_cmd(bdev, desc, ch, req);
2238 	case SPDK_NVME_OPC_RESERVATION_REGISTER:
2239 	case SPDK_NVME_OPC_RESERVATION_ACQUIRE:
2240 	case SPDK_NVME_OPC_RESERVATION_RELEASE:
2241 	case SPDK_NVME_OPC_RESERVATION_REPORT:
2242 		spdk_thread_send_msg(ctrlr->subsys->thread, spdk_nvmf_ns_reservation_request, req);
2243 		return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
2244 	default:
2245 		return spdk_nvmf_bdev_ctrlr_nvme_passthru_io(bdev, desc, ch, req);
2246 	}
2247 }
2248 
2249 static void
2250 spdk_nvmf_qpair_request_cleanup(struct spdk_nvmf_qpair *qpair)
2251 {
2252 	if (qpair->state == SPDK_NVMF_QPAIR_DEACTIVATING) {
2253 		assert(qpair->state_cb != NULL);
2254 
2255 		if (TAILQ_EMPTY(&qpair->outstanding)) {
2256 			qpair->state_cb(qpair->state_cb_arg, 0);
2257 		}
2258 	} else {
2259 		assert(qpair->state == SPDK_NVMF_QPAIR_ACTIVE);
2260 	}
2261 }
2262 
2263 int
2264 spdk_nvmf_request_free(struct spdk_nvmf_request *req)
2265 {
2266 	struct spdk_nvmf_qpair *qpair = req->qpair;
2267 
2268 	TAILQ_REMOVE(&qpair->outstanding, req, link);
2269 	if (spdk_nvmf_transport_req_free(req)) {
2270 		SPDK_ERRLOG("Unable to free transport level request resources.\n");
2271 	}
2272 
2273 	spdk_nvmf_qpair_request_cleanup(qpair);
2274 
2275 	return 0;
2276 }
2277 
2278 int
2279 spdk_nvmf_request_complete(struct spdk_nvmf_request *req)
2280 {
2281 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
2282 	struct spdk_nvmf_qpair *qpair;
2283 
2284 	rsp->sqid = 0;
2285 	rsp->status.p = 0;
2286 	rsp->cid = req->cmd->nvme_cmd.cid;
2287 
2288 	qpair = req->qpair;
2289 
2290 	SPDK_DEBUGLOG(SPDK_LOG_NVMF,
2291 		      "cpl: cid=%u cdw0=0x%08x rsvd1=%u status=0x%04x\n",
2292 		      rsp->cid, rsp->cdw0, rsp->rsvd1,
2293 		      *(uint16_t *)&rsp->status);
2294 
2295 	TAILQ_REMOVE(&qpair->outstanding, req, link);
2296 	if (spdk_nvmf_transport_req_complete(req)) {
2297 		SPDK_ERRLOG("Transport request completion error!\n");
2298 	}
2299 
2300 	spdk_nvmf_qpair_request_cleanup(qpair);
2301 
2302 	return 0;
2303 }
2304 
2305 static void
2306 nvmf_trace_command(union nvmf_h2c_msg *h2c_msg, bool is_admin_queue)
2307 {
2308 	struct spdk_nvmf_capsule_cmd *cap_hdr = &h2c_msg->nvmf_cmd;
2309 	struct spdk_nvme_cmd *cmd = &h2c_msg->nvme_cmd;
2310 	struct spdk_nvme_sgl_descriptor *sgl = &cmd->dptr.sgl1;
2311 	uint8_t opc;
2312 
2313 	if (cmd->opc == SPDK_NVME_OPC_FABRIC) {
2314 		opc = cap_hdr->fctype;
2315 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "%s Fabrics cmd: fctype 0x%02x cid %u\n",
2316 			      is_admin_queue ? "Admin" : "I/O",
2317 			      cap_hdr->fctype, cap_hdr->cid);
2318 	} else {
2319 		opc = cmd->opc;
2320 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "%s cmd: opc 0x%02x fuse %u cid %u nsid %u cdw10 0x%08x\n",
2321 			      is_admin_queue ? "Admin" : "I/O",
2322 			      cmd->opc, cmd->fuse, cmd->cid, cmd->nsid, cmd->cdw10);
2323 		if (cmd->mptr) {
2324 			SPDK_DEBUGLOG(SPDK_LOG_NVMF, "mptr 0x%" PRIx64 "\n", cmd->mptr);
2325 		}
2326 		if (cmd->psdt != SPDK_NVME_PSDT_SGL_MPTR_CONTIG &&
2327 		    cmd->psdt != SPDK_NVME_PSDT_SGL_MPTR_SGL) {
2328 			SPDK_DEBUGLOG(SPDK_LOG_NVMF, "psdt %u\n", cmd->psdt);
2329 		}
2330 	}
2331 
2332 	if (spdk_nvme_opc_get_data_transfer(opc) != SPDK_NVME_DATA_NONE) {
2333 		if (sgl->generic.type == SPDK_NVME_SGL_TYPE_KEYED_DATA_BLOCK) {
2334 			SPDK_DEBUGLOG(SPDK_LOG_NVMF,
2335 				      "SGL: Keyed%s: addr 0x%" PRIx64 " key 0x%x len 0x%x\n",
2336 				      sgl->generic.subtype == SPDK_NVME_SGL_SUBTYPE_INVALIDATE_KEY ? " (Inv)" : "",
2337 				      sgl->address, sgl->keyed.key, sgl->keyed.length);
2338 		} else if (sgl->generic.type == SPDK_NVME_SGL_TYPE_DATA_BLOCK) {
2339 			SPDK_DEBUGLOG(SPDK_LOG_NVMF, "SGL: Data block: %s 0x%" PRIx64 " len 0x%x\n",
2340 				      sgl->unkeyed.subtype == SPDK_NVME_SGL_SUBTYPE_OFFSET ? "offs" : "addr",
2341 				      sgl->address, sgl->unkeyed.length);
2342 		} else {
2343 			SPDK_DEBUGLOG(SPDK_LOG_NVMF, "SGL type 0x%x subtype 0x%x\n",
2344 				      sgl->generic.type, sgl->generic.subtype);
2345 		}
2346 	}
2347 }
2348 
2349 void
2350 spdk_nvmf_request_exec(struct spdk_nvmf_request *req)
2351 {
2352 	struct spdk_nvmf_qpair *qpair = req->qpair;
2353 	spdk_nvmf_request_exec_status status;
2354 
2355 	nvmf_trace_command(req->cmd, spdk_nvmf_qpair_is_admin_queue(qpair));
2356 
2357 	if (qpair->state != SPDK_NVMF_QPAIR_ACTIVE) {
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 		/* Place the request on the outstanding list so we can keep track of it */
2361 		TAILQ_INSERT_TAIL(&qpair->outstanding, req, link);
2362 		spdk_nvmf_request_complete(req);
2363 		return;
2364 	}
2365 
2366 	/* Check if the subsystem is paused (if there is a subsystem) */
2367 	if (qpair->ctrlr) {
2368 		struct spdk_nvmf_subsystem_poll_group *sgroup = &qpair->group->sgroups[qpair->ctrlr->subsys->id];
2369 		if (sgroup->state != SPDK_NVMF_SUBSYSTEM_ACTIVE) {
2370 			/* The subsystem is not currently active. Queue this request. */
2371 			TAILQ_INSERT_TAIL(&sgroup->queued, req, link);
2372 			return;
2373 		}
2374 
2375 	}
2376 
2377 	/* Place the request on the outstanding list so we can keep track of it */
2378 	TAILQ_INSERT_TAIL(&qpair->outstanding, req, link);
2379 
2380 	if (spdk_unlikely(req->cmd->nvmf_cmd.opcode == SPDK_NVME_OPC_FABRIC)) {
2381 		status = spdk_nvmf_ctrlr_process_fabrics_cmd(req);
2382 	} else if (spdk_unlikely(spdk_nvmf_qpair_is_admin_queue(qpair))) {
2383 		status = spdk_nvmf_ctrlr_process_admin_cmd(req);
2384 	} else {
2385 		status = spdk_nvmf_ctrlr_process_io_cmd(req);
2386 	}
2387 
2388 	if (status == SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE) {
2389 		spdk_nvmf_request_complete(req);
2390 	}
2391 }
2392