xref: /spdk/lib/nvmf/ctrlr.c (revision e05a4871294b304317f41c11011e5ff666ba28c4)
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 		/* The RDMA transport supports up to SPDK_NVMF_MAX_SGL_ENTRIES descriptors. */
1529 		if (transport->ops->type == SPDK_NVME_TRANSPORT_RDMA) {
1530 			cdata->nvmf_specific.msdbd = SPDK_NVMF_MAX_SGL_ENTRIES;
1531 		} else {
1532 			cdata->nvmf_specific.msdbd = 1;
1533 		}
1534 
1535 		/* TODO: this should be set by the transport */
1536 		cdata->nvmf_specific.ioccsz += transport->opts.in_capsule_data_size / 16;
1537 
1538 		cdata->oncs.dsm = spdk_nvmf_ctrlr_dsm_supported(ctrlr);
1539 		cdata->oncs.write_zeroes = spdk_nvmf_ctrlr_write_zeroes_supported(ctrlr);
1540 		cdata->oncs.reservations = 1;
1541 
1542 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ext ctrlr data: ioccsz 0x%x\n",
1543 			      cdata->nvmf_specific.ioccsz);
1544 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ext ctrlr data: iorcsz 0x%x\n",
1545 			      cdata->nvmf_specific.iorcsz);
1546 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ext ctrlr data: icdoff 0x%x\n",
1547 			      cdata->nvmf_specific.icdoff);
1548 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ext ctrlr data: ctrattr 0x%x\n",
1549 			      *(uint8_t *)&cdata->nvmf_specific.ctrattr);
1550 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ext ctrlr data: msdbd 0x%x\n",
1551 			      cdata->nvmf_specific.msdbd);
1552 	}
1553 
1554 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1555 }
1556 
1557 static int
1558 spdk_nvmf_ctrlr_identify_active_ns_list(struct spdk_nvmf_subsystem *subsystem,
1559 					struct spdk_nvme_cmd *cmd,
1560 					struct spdk_nvme_cpl *rsp,
1561 					struct spdk_nvme_ns_list *ns_list)
1562 {
1563 	struct spdk_nvmf_ns *ns;
1564 	uint32_t count = 0;
1565 
1566 	if (cmd->nsid >= 0xfffffffeUL) {
1567 		SPDK_ERRLOG("Identify Active Namespace List with invalid NSID %u\n", cmd->nsid);
1568 		rsp->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT;
1569 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1570 	}
1571 
1572 	for (ns = spdk_nvmf_subsystem_get_first_ns(subsystem); ns != NULL;
1573 	     ns = spdk_nvmf_subsystem_get_next_ns(subsystem, ns)) {
1574 		if (ns->opts.nsid <= cmd->nsid) {
1575 			continue;
1576 		}
1577 
1578 		ns_list->ns_list[count++] = ns->opts.nsid;
1579 		if (count == SPDK_COUNTOF(ns_list->ns_list)) {
1580 			break;
1581 		}
1582 	}
1583 
1584 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1585 }
1586 
1587 static void
1588 _add_ns_id_desc(void **buf_ptr, size_t *buf_remain,
1589 		enum spdk_nvme_nidt type,
1590 		const void *data, size_t data_size)
1591 {
1592 	struct spdk_nvme_ns_id_desc *desc;
1593 	size_t desc_size = sizeof(*desc) + data_size;
1594 
1595 	/*
1596 	 * These should never fail in practice, since all valid NS ID descriptors
1597 	 * should be defined so that they fit in the available 4096-byte buffer.
1598 	 */
1599 	assert(data_size > 0);
1600 	assert(data_size <= UINT8_MAX);
1601 	assert(desc_size < *buf_remain);
1602 	if (data_size == 0 || data_size > UINT8_MAX || desc_size > *buf_remain) {
1603 		return;
1604 	}
1605 
1606 	desc = *buf_ptr;
1607 	desc->nidt = type;
1608 	desc->nidl = data_size;
1609 	memcpy(desc->nid, data, data_size);
1610 
1611 	*buf_ptr += desc_size;
1612 	*buf_remain -= desc_size;
1613 }
1614 
1615 static int
1616 spdk_nvmf_ctrlr_identify_ns_id_descriptor_list(
1617 	struct spdk_nvmf_subsystem *subsystem,
1618 	struct spdk_nvme_cmd *cmd,
1619 	struct spdk_nvme_cpl *rsp,
1620 	void *id_desc_list, size_t id_desc_list_size)
1621 {
1622 	struct spdk_nvmf_ns *ns;
1623 	size_t buf_remain = id_desc_list_size;
1624 	void *buf_ptr = id_desc_list;
1625 
1626 	ns = _spdk_nvmf_subsystem_get_ns(subsystem, cmd->nsid);
1627 	if (ns == NULL || ns->bdev == NULL) {
1628 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
1629 		rsp->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT;
1630 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1631 	}
1632 
1633 #define ADD_ID_DESC(type, data, size) \
1634 	do { \
1635 		if (!spdk_mem_all_zero(data, size)) { \
1636 			_add_ns_id_desc(&buf_ptr, &buf_remain, type, data, size); \
1637 		} \
1638 	} while (0)
1639 
1640 	ADD_ID_DESC(SPDK_NVME_NIDT_EUI64, ns->opts.eui64, sizeof(ns->opts.eui64));
1641 	ADD_ID_DESC(SPDK_NVME_NIDT_NGUID, ns->opts.nguid, sizeof(ns->opts.nguid));
1642 	ADD_ID_DESC(SPDK_NVME_NIDT_UUID, &ns->opts.uuid, sizeof(ns->opts.uuid));
1643 
1644 	/*
1645 	 * The list is automatically 0-terminated because controller to host buffers in
1646 	 * admin commands always get zeroed in spdk_nvmf_ctrlr_process_admin_cmd().
1647 	 */
1648 
1649 #undef ADD_ID_DESC
1650 
1651 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1652 }
1653 
1654 static int
1655 spdk_nvmf_ctrlr_identify(struct spdk_nvmf_request *req)
1656 {
1657 	uint8_t cns;
1658 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1659 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1660 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
1661 	struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys;
1662 
1663 	if (req->data == NULL || req->length < 4096) {
1664 		SPDK_ERRLOG("identify command with invalid buffer\n");
1665 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
1666 		rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1667 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1668 	}
1669 
1670 	cns = cmd->cdw10 & 0xFF;
1671 
1672 	if (subsystem->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY &&
1673 	    cns != SPDK_NVME_IDENTIFY_CTRLR) {
1674 		/* Discovery controllers only support Identify Controller */
1675 		goto invalid_cns;
1676 	}
1677 
1678 	switch (cns) {
1679 	case SPDK_NVME_IDENTIFY_NS:
1680 		return spdk_nvmf_ctrlr_identify_ns(ctrlr, cmd, rsp, req->data);
1681 	case SPDK_NVME_IDENTIFY_CTRLR:
1682 		return spdk_nvmf_ctrlr_identify_ctrlr(ctrlr, req->data);
1683 	case SPDK_NVME_IDENTIFY_ACTIVE_NS_LIST:
1684 		return spdk_nvmf_ctrlr_identify_active_ns_list(subsystem, cmd, rsp, req->data);
1685 	case SPDK_NVME_IDENTIFY_NS_ID_DESCRIPTOR_LIST:
1686 		return spdk_nvmf_ctrlr_identify_ns_id_descriptor_list(subsystem, cmd, rsp, req->data, req->length);
1687 	default:
1688 		goto invalid_cns;
1689 	}
1690 
1691 invalid_cns:
1692 	SPDK_ERRLOG("Identify command with unsupported CNS 0x%02x\n", cns);
1693 	rsp->status.sct = SPDK_NVME_SCT_GENERIC;
1694 	rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1695 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1696 }
1697 
1698 
1699 static struct spdk_nvmf_request *
1700 spdk_nvmf_qpair_abort(struct spdk_nvmf_qpair *qpair, uint16_t cid)
1701 {
1702 	struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr;
1703 	struct spdk_nvmf_request *req;
1704 
1705 	if (spdk_nvmf_qpair_is_admin_queue(qpair)) {
1706 		if (ctrlr->aer_req && ctrlr->aer_req->cmd->nvme_cmd.cid == cid) {
1707 			SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Aborting AER request\n");
1708 			req = ctrlr->aer_req;
1709 			ctrlr->aer_req = NULL;
1710 			return req;
1711 		}
1712 	}
1713 
1714 	/* TODO: track list of outstanding requests in qpair? */
1715 	return NULL;
1716 }
1717 
1718 static void
1719 spdk_nvmf_ctrlr_abort_done(struct spdk_io_channel_iter *i, int status)
1720 {
1721 	struct spdk_nvmf_request *req = spdk_io_channel_iter_get_ctx(i);
1722 
1723 	spdk_nvmf_request_complete(req);
1724 }
1725 
1726 static void
1727 spdk_nvmf_ctrlr_abort_on_pg(struct spdk_io_channel_iter *i)
1728 {
1729 	struct spdk_nvmf_request *req = spdk_io_channel_iter_get_ctx(i);
1730 	struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
1731 	struct spdk_nvmf_poll_group *group = spdk_io_channel_get_ctx(ch);
1732 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
1733 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1734 	uint16_t sqid = cmd->cdw10 & 0xFFFFu;
1735 	struct spdk_nvmf_qpair *qpair;
1736 
1737 	TAILQ_FOREACH(qpair, &group->qpairs, link) {
1738 		if (qpair->ctrlr == req->qpair->ctrlr && qpair->qid == sqid) {
1739 			struct spdk_nvmf_request *req_to_abort;
1740 			uint16_t cid = cmd->cdw10 >> 16;
1741 
1742 			/* Found the qpair */
1743 
1744 			req_to_abort = spdk_nvmf_qpair_abort(qpair, cid);
1745 			if (req_to_abort == NULL) {
1746 				SPDK_DEBUGLOG(SPDK_LOG_NVMF, "cid %u not found\n", cid);
1747 				rsp->status.sct = SPDK_NVME_SCT_GENERIC;
1748 				rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1749 				spdk_for_each_channel_continue(i, -EINVAL);
1750 				return;
1751 			}
1752 
1753 			/* Complete the request with aborted status */
1754 			req_to_abort->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
1755 			req_to_abort->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_ABORTED_BY_REQUEST;
1756 			spdk_nvmf_request_complete(req_to_abort);
1757 
1758 			SPDK_DEBUGLOG(SPDK_LOG_NVMF, "abort ctrlr=%p req=%p sqid=%u cid=%u successful\n",
1759 				      qpair->ctrlr, req_to_abort, sqid, cid);
1760 			rsp->cdw0 = 0; /* Command successfully aborted */
1761 			rsp->status.sct = SPDK_NVME_SCT_GENERIC;
1762 			rsp->status.sc = SPDK_NVME_SC_SUCCESS;
1763 			/* Return -1 for the status so the iteration across threads stops. */
1764 			spdk_for_each_channel_continue(i, -1);
1765 
1766 		}
1767 	}
1768 
1769 	spdk_for_each_channel_continue(i, 0);
1770 }
1771 
1772 static int
1773 spdk_nvmf_ctrlr_abort(struct spdk_nvmf_request *req)
1774 {
1775 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
1776 
1777 	rsp->cdw0 = 1; /* Command not aborted */
1778 	rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
1779 	rsp->status.sc = SPDK_NVME_SC_INVALID_QUEUE_IDENTIFIER;
1780 
1781 	/* Send a message to each poll group, searching for this ctrlr, sqid, and command. */
1782 	spdk_for_each_channel(req->qpair->ctrlr->subsys->tgt,
1783 			      spdk_nvmf_ctrlr_abort_on_pg,
1784 			      req,
1785 			      spdk_nvmf_ctrlr_abort_done
1786 			     );
1787 
1788 	return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
1789 }
1790 
1791 static int
1792 get_features_generic(struct spdk_nvmf_request *req, uint32_t cdw0)
1793 {
1794 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
1795 
1796 	rsp->cdw0 = cdw0;
1797 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1798 }
1799 
1800 static int
1801 spdk_nvmf_ctrlr_get_features(struct spdk_nvmf_request *req)
1802 {
1803 	uint8_t feature;
1804 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1805 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1806 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
1807 
1808 	feature = cmd->cdw10 & 0xff; /* mask out the FID value */
1809 	switch (feature) {
1810 	case SPDK_NVME_FEAT_ARBITRATION:
1811 		return get_features_generic(req, ctrlr->feat.arbitration.raw);
1812 	case SPDK_NVME_FEAT_POWER_MANAGEMENT:
1813 		return get_features_generic(req, ctrlr->feat.power_management.raw);
1814 	case SPDK_NVME_FEAT_TEMPERATURE_THRESHOLD:
1815 		return spdk_nvmf_ctrlr_get_features_temperature_threshold(req);
1816 	case SPDK_NVME_FEAT_ERROR_RECOVERY:
1817 		return get_features_generic(req, ctrlr->feat.error_recovery.raw);
1818 	case SPDK_NVME_FEAT_VOLATILE_WRITE_CACHE:
1819 		return get_features_generic(req, ctrlr->feat.volatile_write_cache.raw);
1820 	case SPDK_NVME_FEAT_NUMBER_OF_QUEUES:
1821 		return get_features_generic(req, ctrlr->feat.number_of_queues.raw);
1822 	case SPDK_NVME_FEAT_WRITE_ATOMICITY:
1823 		return get_features_generic(req, ctrlr->feat.write_atomicity.raw);
1824 	case SPDK_NVME_FEAT_ASYNC_EVENT_CONFIGURATION:
1825 		return get_features_generic(req, ctrlr->feat.async_event_configuration.raw);
1826 	case SPDK_NVME_FEAT_KEEP_ALIVE_TIMER:
1827 		return get_features_generic(req, ctrlr->feat.keep_alive_timer.raw);
1828 	case SPDK_NVME_FEAT_HOST_IDENTIFIER:
1829 		return spdk_nvmf_ctrlr_get_features_host_identifier(req);
1830 	case SPDK_NVME_FEAT_HOST_RESERVE_MASK:
1831 		return spdk_nvmf_ctrlr_get_features_reservation_notification_mask(req);
1832 	case SPDK_NVME_FEAT_HOST_RESERVE_PERSIST:
1833 		return spdk_nvmf_ctrlr_get_features_reservation_persistence(req);
1834 	default:
1835 		SPDK_ERRLOG("Get Features command with unsupported feature ID 0x%02x\n", feature);
1836 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1837 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1838 	}
1839 }
1840 
1841 static int
1842 spdk_nvmf_ctrlr_set_features(struct spdk_nvmf_request *req)
1843 {
1844 	uint8_t feature;
1845 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1846 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
1847 
1848 	feature = cmd->cdw10 & 0xff; /* mask out the FID value */
1849 	switch (feature) {
1850 	case SPDK_NVME_FEAT_ARBITRATION:
1851 		return spdk_nvmf_ctrlr_set_features_arbitration(req);
1852 	case SPDK_NVME_FEAT_POWER_MANAGEMENT:
1853 		return spdk_nvmf_ctrlr_set_features_power_management(req);
1854 	case SPDK_NVME_FEAT_TEMPERATURE_THRESHOLD:
1855 		return spdk_nvmf_ctrlr_set_features_temperature_threshold(req);
1856 	case SPDK_NVME_FEAT_ERROR_RECOVERY:
1857 		return spdk_nvmf_ctrlr_set_features_error_recovery(req);
1858 	case SPDK_NVME_FEAT_VOLATILE_WRITE_CACHE:
1859 		return spdk_nvmf_ctrlr_set_features_volatile_write_cache(req);
1860 	case SPDK_NVME_FEAT_NUMBER_OF_QUEUES:
1861 		return spdk_nvmf_ctrlr_set_features_number_of_queues(req);
1862 	case SPDK_NVME_FEAT_WRITE_ATOMICITY:
1863 		return spdk_nvmf_ctrlr_set_features_write_atomicity(req);
1864 	case SPDK_NVME_FEAT_ASYNC_EVENT_CONFIGURATION:
1865 		return spdk_nvmf_ctrlr_set_features_async_event_configuration(req);
1866 	case SPDK_NVME_FEAT_KEEP_ALIVE_TIMER:
1867 		return spdk_nvmf_ctrlr_set_features_keep_alive_timer(req);
1868 	case SPDK_NVME_FEAT_HOST_IDENTIFIER:
1869 		return spdk_nvmf_ctrlr_set_features_host_identifier(req);
1870 	case SPDK_NVME_FEAT_HOST_RESERVE_MASK:
1871 		return spdk_nvmf_ctrlr_set_features_reservation_notification_mask(req);
1872 	case SPDK_NVME_FEAT_HOST_RESERVE_PERSIST:
1873 		return spdk_nvmf_ctrlr_set_features_reservation_persistence(req);
1874 	default:
1875 		SPDK_ERRLOG("Set Features command with unsupported feature ID 0x%02x\n", feature);
1876 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1877 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1878 	}
1879 }
1880 
1881 static int
1882 spdk_nvmf_ctrlr_keep_alive(struct spdk_nvmf_request *req)
1883 {
1884 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1885 
1886 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Keep Alive\n");
1887 	/*
1888 	 * To handle keep alive just clear or reset the
1889 	 * ctrlr based keep alive duration counter.
1890 	 * When added, a separate timer based process
1891 	 * will monitor if the time since last recorded
1892 	 * keep alive has exceeded the max duration and
1893 	 * take appropriate action.
1894 	 */
1895 	ctrlr->last_keep_alive_tick = spdk_get_ticks();
1896 
1897 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1898 }
1899 
1900 int
1901 spdk_nvmf_ctrlr_process_admin_cmd(struct spdk_nvmf_request *req)
1902 {
1903 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1904 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1905 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
1906 
1907 	if (ctrlr == NULL) {
1908 		SPDK_ERRLOG("Admin command sent before CONNECT\n");
1909 		response->status.sct = SPDK_NVME_SCT_GENERIC;
1910 		response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
1911 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1912 	}
1913 
1914 	if (ctrlr->vcprop.cc.bits.en != 1) {
1915 		SPDK_ERRLOG("Admin command sent to disabled controller\n");
1916 		response->status.sct = SPDK_NVME_SCT_GENERIC;
1917 		response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
1918 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1919 	}
1920 
1921 	if (req->data && spdk_nvme_opc_get_data_transfer(cmd->opc) == SPDK_NVME_DATA_CONTROLLER_TO_HOST) {
1922 		memset(req->data, 0, req->length);
1923 	}
1924 
1925 	if (ctrlr->subsys->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) {
1926 		/* Discovery controllers only support Get Log Page and Identify */
1927 		switch (cmd->opc) {
1928 		case SPDK_NVME_OPC_IDENTIFY:
1929 		case SPDK_NVME_OPC_GET_LOG_PAGE:
1930 			break;
1931 		default:
1932 			goto invalid_opcode;
1933 		}
1934 	}
1935 
1936 	switch (cmd->opc) {
1937 	case SPDK_NVME_OPC_GET_LOG_PAGE:
1938 		return spdk_nvmf_ctrlr_get_log_page(req);
1939 	case SPDK_NVME_OPC_IDENTIFY:
1940 		return spdk_nvmf_ctrlr_identify(req);
1941 	case SPDK_NVME_OPC_ABORT:
1942 		return spdk_nvmf_ctrlr_abort(req);
1943 	case SPDK_NVME_OPC_GET_FEATURES:
1944 		return spdk_nvmf_ctrlr_get_features(req);
1945 	case SPDK_NVME_OPC_SET_FEATURES:
1946 		return spdk_nvmf_ctrlr_set_features(req);
1947 	case SPDK_NVME_OPC_ASYNC_EVENT_REQUEST:
1948 		return spdk_nvmf_ctrlr_async_event_request(req);
1949 	case SPDK_NVME_OPC_KEEP_ALIVE:
1950 		return spdk_nvmf_ctrlr_keep_alive(req);
1951 
1952 	case SPDK_NVME_OPC_CREATE_IO_SQ:
1953 	case SPDK_NVME_OPC_CREATE_IO_CQ:
1954 	case SPDK_NVME_OPC_DELETE_IO_SQ:
1955 	case SPDK_NVME_OPC_DELETE_IO_CQ:
1956 		/* Create and Delete I/O CQ/SQ not allowed in NVMe-oF */
1957 		goto invalid_opcode;
1958 
1959 	default:
1960 		goto invalid_opcode;
1961 	}
1962 
1963 invalid_opcode:
1964 	SPDK_ERRLOG("Unsupported admin opcode 0x%x\n", cmd->opc);
1965 	response->status.sct = SPDK_NVME_SCT_GENERIC;
1966 	response->status.sc = SPDK_NVME_SC_INVALID_OPCODE;
1967 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1968 }
1969 
1970 int
1971 spdk_nvmf_ctrlr_process_fabrics_cmd(struct spdk_nvmf_request *req)
1972 {
1973 	struct spdk_nvmf_qpair *qpair = req->qpair;
1974 	struct spdk_nvmf_capsule_cmd *cap_hdr;
1975 
1976 	cap_hdr = &req->cmd->nvmf_cmd;
1977 
1978 	if (qpair->ctrlr == NULL) {
1979 		/* No ctrlr established yet; the only valid command is Connect */
1980 		if (cap_hdr->fctype == SPDK_NVMF_FABRIC_COMMAND_CONNECT) {
1981 			return spdk_nvmf_ctrlr_connect(req);
1982 		} else {
1983 			SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Got fctype 0x%x, expected Connect\n",
1984 				      cap_hdr->fctype);
1985 			req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
1986 			req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
1987 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1988 		}
1989 	} else if (spdk_nvmf_qpair_is_admin_queue(qpair)) {
1990 		/*
1991 		 * Controller session is established, and this is an admin queue.
1992 		 * Disallow Connect and allow other fabrics commands.
1993 		 */
1994 		switch (cap_hdr->fctype) {
1995 		case SPDK_NVMF_FABRIC_COMMAND_PROPERTY_SET:
1996 			return spdk_nvmf_property_set(req);
1997 		case SPDK_NVMF_FABRIC_COMMAND_PROPERTY_GET:
1998 			return spdk_nvmf_property_get(req);
1999 		default:
2000 			SPDK_DEBUGLOG(SPDK_LOG_NVMF, "unknown fctype 0x%02x\n",
2001 				      cap_hdr->fctype);
2002 			req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
2003 			req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_INVALID_OPCODE;
2004 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2005 		}
2006 	} else {
2007 		/* Controller session is established, and this is an I/O queue */
2008 		/* For now, no I/O-specific Fabrics commands are implemented (other than Connect) */
2009 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Unexpected I/O fctype 0x%x\n", cap_hdr->fctype);
2010 		req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
2011 		req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_INVALID_OPCODE;
2012 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2013 	}
2014 }
2015 
2016 int
2017 spdk_nvmf_ctrlr_async_event_ns_notice(struct spdk_nvmf_ctrlr *ctrlr)
2018 {
2019 	struct spdk_nvmf_request *req;
2020 	struct spdk_nvme_cpl *rsp;
2021 	union spdk_nvme_async_event_completion event = {0};
2022 
2023 	/* Users may disable the event notification */
2024 	if (!ctrlr->feat.async_event_configuration.bits.ns_attr_notice) {
2025 		return 0;
2026 	}
2027 
2028 	event.bits.async_event_type = SPDK_NVME_ASYNC_EVENT_TYPE_NOTICE;
2029 	event.bits.async_event_info = SPDK_NVME_ASYNC_EVENT_NS_ATTR_CHANGED;
2030 	event.bits.log_page_identifier = SPDK_NVME_LOG_CHANGED_NS_LIST;
2031 
2032 	/* If there is no outstanding AER request, queue the event.  Then
2033 	 * if an AER is later submitted, this event can be sent as a
2034 	 * response.
2035 	 */
2036 	if (!ctrlr->aer_req) {
2037 		if (ctrlr->notice_event.bits.async_event_type ==
2038 		    SPDK_NVME_ASYNC_EVENT_TYPE_NOTICE) {
2039 			return 0;
2040 		}
2041 
2042 		ctrlr->notice_event.raw = event.raw;
2043 		return 0;
2044 	}
2045 
2046 	req = ctrlr->aer_req;
2047 	rsp = &req->rsp->nvme_cpl;
2048 
2049 	rsp->cdw0 = event.raw;
2050 
2051 	spdk_nvmf_request_complete(req);
2052 	ctrlr->aer_req = NULL;
2053 
2054 	return 0;
2055 }
2056 
2057 void
2058 spdk_nvmf_qpair_free_aer(struct spdk_nvmf_qpair *qpair)
2059 {
2060 	struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr;
2061 
2062 	if (!spdk_nvmf_qpair_is_admin_queue(qpair)) {
2063 		return;
2064 	}
2065 
2066 	if (ctrlr->aer_req != NULL) {
2067 		spdk_nvmf_request_free(ctrlr->aer_req);
2068 		ctrlr->aer_req = NULL;
2069 	}
2070 }
2071 
2072 void
2073 spdk_nvmf_ctrlr_abort_aer(struct spdk_nvmf_ctrlr *ctrlr)
2074 {
2075 	if (!ctrlr->aer_req) {
2076 		return;
2077 	}
2078 
2079 	spdk_nvmf_request_complete(ctrlr->aer_req);
2080 	ctrlr->aer_req = NULL;
2081 }
2082 
2083 /* Check from subsystem poll group's namespace information data structure */
2084 static bool
2085 nvmf_ns_info_ctrlr_is_registrant(struct spdk_nvmf_subsystem_pg_ns_info *ns_info,
2086 				 struct spdk_nvmf_ctrlr *ctrlr)
2087 {
2088 	uint32_t i;
2089 
2090 	for (i = 0; i < SPDK_NVMF_MAX_NUM_REGISTRANTS; i++) {
2091 		if (!spdk_uuid_compare(&ns_info->reg_hostid[i], &ctrlr->hostid)) {
2092 			return true;
2093 		}
2094 	}
2095 
2096 	return false;
2097 }
2098 
2099 /*
2100  * Check the NVMe command is permitted or not for current controller(Host).
2101  */
2102 static int
2103 nvmf_ns_reservation_request_check(struct spdk_nvmf_subsystem_pg_ns_info *ns_info,
2104 				  struct spdk_nvmf_ctrlr *ctrlr,
2105 				  struct spdk_nvmf_request *req)
2106 {
2107 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
2108 	enum spdk_nvme_reservation_type rtype = ns_info->rtype;
2109 	uint8_t status = SPDK_NVME_SC_SUCCESS;
2110 	uint8_t racqa;
2111 	bool is_registrant;
2112 
2113 	/* No valid reservation */
2114 	if (!rtype) {
2115 		return 0;
2116 	}
2117 
2118 	is_registrant = nvmf_ns_info_ctrlr_is_registrant(ns_info, ctrlr);
2119 	/* All registrants type and current ctrlr is a valid registrant */
2120 	if ((rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_ALL_REGS ||
2121 	     rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_ALL_REGS) && is_registrant) {
2122 		return 0;
2123 	} else if (!spdk_uuid_compare(&ns_info->holder_id, &ctrlr->hostid)) {
2124 		return 0;
2125 	}
2126 
2127 	/* Non-holder for current controller */
2128 	switch (cmd->opc) {
2129 	case SPDK_NVME_OPC_READ:
2130 	case SPDK_NVME_OPC_COMPARE:
2131 		if (rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS) {
2132 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2133 			goto exit;
2134 		}
2135 		if ((rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_REG_ONLY ||
2136 		     rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_ALL_REGS) && !is_registrant) {
2137 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2138 			goto exit;
2139 		}
2140 		break;
2141 	case SPDK_NVME_OPC_FLUSH:
2142 	case SPDK_NVME_OPC_WRITE:
2143 	case SPDK_NVME_OPC_WRITE_UNCORRECTABLE:
2144 	case SPDK_NVME_OPC_WRITE_ZEROES:
2145 	case SPDK_NVME_OPC_DATASET_MANAGEMENT:
2146 		if (rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE ||
2147 		    rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS) {
2148 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2149 			goto exit;
2150 		}
2151 		if (!is_registrant) {
2152 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2153 			goto exit;
2154 		}
2155 		break;
2156 	case SPDK_NVME_OPC_RESERVATION_ACQUIRE:
2157 	case SPDK_NVME_OPC_RESERVATION_RELEASE:
2158 		racqa = cmd->cdw10 & 0x7u;
2159 		if (cmd->opc == SPDK_NVME_OPC_RESERVATION_ACQUIRE &&
2160 		    racqa == SPDK_NVME_RESERVE_ACQUIRE) {
2161 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2162 			goto exit;
2163 		}
2164 		if (!is_registrant) {
2165 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2166 			goto exit;
2167 		}
2168 		break;
2169 	default:
2170 		break;
2171 	}
2172 
2173 exit:
2174 	req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
2175 	req->rsp->nvme_cpl.status.sc = status;
2176 	if (status == SPDK_NVME_SC_RESERVATION_CONFLICT) {
2177 		return -EPERM;
2178 	}
2179 
2180 	return 0;
2181 }
2182 
2183 int
2184 spdk_nvmf_ctrlr_process_io_cmd(struct spdk_nvmf_request *req)
2185 {
2186 	uint32_t nsid;
2187 	struct spdk_nvmf_ns *ns;
2188 	struct spdk_bdev *bdev;
2189 	struct spdk_bdev_desc *desc;
2190 	struct spdk_io_channel *ch;
2191 	struct spdk_nvmf_poll_group *group = req->qpair->group;
2192 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
2193 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
2194 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
2195 	struct spdk_nvmf_subsystem_pg_ns_info *ns_info;
2196 
2197 	/* pre-set response details for this command */
2198 	response->status.sc = SPDK_NVME_SC_SUCCESS;
2199 	nsid = cmd->nsid;
2200 
2201 	if (spdk_unlikely(ctrlr == NULL)) {
2202 		SPDK_ERRLOG("I/O command sent before CONNECT\n");
2203 		response->status.sct = SPDK_NVME_SCT_GENERIC;
2204 		response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
2205 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2206 	}
2207 
2208 	if (spdk_unlikely(ctrlr->vcprop.cc.bits.en != 1)) {
2209 		SPDK_ERRLOG("I/O command sent to disabled controller\n");
2210 		response->status.sct = SPDK_NVME_SCT_GENERIC;
2211 		response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
2212 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2213 	}
2214 
2215 	ns = _spdk_nvmf_subsystem_get_ns(ctrlr->subsys, nsid);
2216 	if (ns == NULL || ns->bdev == NULL) {
2217 		SPDK_ERRLOG("Unsuccessful query for nsid %u\n", cmd->nsid);
2218 		response->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT;
2219 		response->status.dnr = 1;
2220 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2221 	}
2222 
2223 	ns_info = &group->sgroups[ctrlr->subsys->id].ns_info[nsid - 1];
2224 	if (nvmf_ns_reservation_request_check(ns_info, ctrlr, req)) {
2225 		SPDK_NOTICELOG("Reservation Conflict for nsid %u, opcode %u\n", cmd->nsid, cmd->opc);
2226 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
2227 	}
2228 
2229 	bdev = ns->bdev;
2230 	desc = ns->desc;
2231 	ch = ns_info->channel;
2232 	switch (cmd->opc) {
2233 	case SPDK_NVME_OPC_READ:
2234 		return spdk_nvmf_bdev_ctrlr_read_cmd(bdev, desc, ch, req);
2235 	case SPDK_NVME_OPC_WRITE:
2236 		return spdk_nvmf_bdev_ctrlr_write_cmd(bdev, desc, ch, req);
2237 	case SPDK_NVME_OPC_WRITE_ZEROES:
2238 		return spdk_nvmf_bdev_ctrlr_write_zeroes_cmd(bdev, desc, ch, req);
2239 	case SPDK_NVME_OPC_FLUSH:
2240 		return spdk_nvmf_bdev_ctrlr_flush_cmd(bdev, desc, ch, req);
2241 	case SPDK_NVME_OPC_DATASET_MANAGEMENT:
2242 		return spdk_nvmf_bdev_ctrlr_dsm_cmd(bdev, desc, ch, req);
2243 	case SPDK_NVME_OPC_RESERVATION_REGISTER:
2244 	case SPDK_NVME_OPC_RESERVATION_ACQUIRE:
2245 	case SPDK_NVME_OPC_RESERVATION_RELEASE:
2246 	case SPDK_NVME_OPC_RESERVATION_REPORT:
2247 		spdk_thread_send_msg(ctrlr->subsys->thread, spdk_nvmf_ns_reservation_request, req);
2248 		return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
2249 	default:
2250 		return spdk_nvmf_bdev_ctrlr_nvme_passthru_io(bdev, desc, ch, req);
2251 	}
2252 }
2253 
2254 static void
2255 spdk_nvmf_qpair_request_cleanup(struct spdk_nvmf_qpair *qpair)
2256 {
2257 	if (qpair->state == SPDK_NVMF_QPAIR_DEACTIVATING) {
2258 		assert(qpair->state_cb != NULL);
2259 
2260 		if (TAILQ_EMPTY(&qpair->outstanding)) {
2261 			qpair->state_cb(qpair->state_cb_arg, 0);
2262 		}
2263 	} else {
2264 		assert(qpair->state == SPDK_NVMF_QPAIR_ACTIVE);
2265 	}
2266 }
2267 
2268 int
2269 spdk_nvmf_request_free(struct spdk_nvmf_request *req)
2270 {
2271 	struct spdk_nvmf_qpair *qpair = req->qpair;
2272 
2273 	TAILQ_REMOVE(&qpair->outstanding, req, link);
2274 	if (spdk_nvmf_transport_req_free(req)) {
2275 		SPDK_ERRLOG("Unable to free transport level request resources.\n");
2276 	}
2277 
2278 	spdk_nvmf_qpair_request_cleanup(qpair);
2279 
2280 	return 0;
2281 }
2282 
2283 int
2284 spdk_nvmf_request_complete(struct spdk_nvmf_request *req)
2285 {
2286 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
2287 	struct spdk_nvmf_qpair *qpair;
2288 
2289 	rsp->sqid = 0;
2290 	rsp->status.p = 0;
2291 	rsp->cid = req->cmd->nvme_cmd.cid;
2292 
2293 	qpair = req->qpair;
2294 
2295 	SPDK_DEBUGLOG(SPDK_LOG_NVMF,
2296 		      "cpl: cid=%u cdw0=0x%08x rsvd1=%u status=0x%04x\n",
2297 		      rsp->cid, rsp->cdw0, rsp->rsvd1,
2298 		      *(uint16_t *)&rsp->status);
2299 
2300 	TAILQ_REMOVE(&qpair->outstanding, req, link);
2301 	if (spdk_nvmf_transport_req_complete(req)) {
2302 		SPDK_ERRLOG("Transport request completion error!\n");
2303 	}
2304 
2305 	spdk_nvmf_qpair_request_cleanup(qpair);
2306 
2307 	return 0;
2308 }
2309 
2310 static void
2311 nvmf_trace_command(union nvmf_h2c_msg *h2c_msg, bool is_admin_queue)
2312 {
2313 	struct spdk_nvmf_capsule_cmd *cap_hdr = &h2c_msg->nvmf_cmd;
2314 	struct spdk_nvme_cmd *cmd = &h2c_msg->nvme_cmd;
2315 	struct spdk_nvme_sgl_descriptor *sgl = &cmd->dptr.sgl1;
2316 	uint8_t opc;
2317 
2318 	if (cmd->opc == SPDK_NVME_OPC_FABRIC) {
2319 		opc = cap_hdr->fctype;
2320 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "%s Fabrics cmd: fctype 0x%02x cid %u\n",
2321 			      is_admin_queue ? "Admin" : "I/O",
2322 			      cap_hdr->fctype, cap_hdr->cid);
2323 	} else {
2324 		opc = cmd->opc;
2325 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "%s cmd: opc 0x%02x fuse %u cid %u nsid %u cdw10 0x%08x\n",
2326 			      is_admin_queue ? "Admin" : "I/O",
2327 			      cmd->opc, cmd->fuse, cmd->cid, cmd->nsid, cmd->cdw10);
2328 		if (cmd->mptr) {
2329 			SPDK_DEBUGLOG(SPDK_LOG_NVMF, "mptr 0x%" PRIx64 "\n", cmd->mptr);
2330 		}
2331 		if (cmd->psdt != SPDK_NVME_PSDT_SGL_MPTR_CONTIG &&
2332 		    cmd->psdt != SPDK_NVME_PSDT_SGL_MPTR_SGL) {
2333 			SPDK_DEBUGLOG(SPDK_LOG_NVMF, "psdt %u\n", cmd->psdt);
2334 		}
2335 	}
2336 
2337 	if (spdk_nvme_opc_get_data_transfer(opc) != SPDK_NVME_DATA_NONE) {
2338 		if (sgl->generic.type == SPDK_NVME_SGL_TYPE_KEYED_DATA_BLOCK) {
2339 			SPDK_DEBUGLOG(SPDK_LOG_NVMF,
2340 				      "SGL: Keyed%s: addr 0x%" PRIx64 " key 0x%x len 0x%x\n",
2341 				      sgl->generic.subtype == SPDK_NVME_SGL_SUBTYPE_INVALIDATE_KEY ? " (Inv)" : "",
2342 				      sgl->address, sgl->keyed.key, sgl->keyed.length);
2343 		} else if (sgl->generic.type == SPDK_NVME_SGL_TYPE_DATA_BLOCK) {
2344 			SPDK_DEBUGLOG(SPDK_LOG_NVMF, "SGL: Data block: %s 0x%" PRIx64 " len 0x%x\n",
2345 				      sgl->unkeyed.subtype == SPDK_NVME_SGL_SUBTYPE_OFFSET ? "offs" : "addr",
2346 				      sgl->address, sgl->unkeyed.length);
2347 		} else {
2348 			SPDK_DEBUGLOG(SPDK_LOG_NVMF, "SGL type 0x%x subtype 0x%x\n",
2349 				      sgl->generic.type, sgl->generic.subtype);
2350 		}
2351 	}
2352 }
2353 
2354 void
2355 spdk_nvmf_request_exec(struct spdk_nvmf_request *req)
2356 {
2357 	struct spdk_nvmf_qpair *qpair = req->qpair;
2358 	spdk_nvmf_request_exec_status status;
2359 
2360 	nvmf_trace_command(req->cmd, spdk_nvmf_qpair_is_admin_queue(qpair));
2361 
2362 	if (qpair->state != SPDK_NVMF_QPAIR_ACTIVE) {
2363 		req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
2364 		req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
2365 		/* Place the request on the outstanding list so we can keep track of it */
2366 		TAILQ_INSERT_TAIL(&qpair->outstanding, req, link);
2367 		spdk_nvmf_request_complete(req);
2368 		return;
2369 	}
2370 
2371 	/* Check if the subsystem is paused (if there is a subsystem) */
2372 	if (qpair->ctrlr) {
2373 		struct spdk_nvmf_subsystem_poll_group *sgroup = &qpair->group->sgroups[qpair->ctrlr->subsys->id];
2374 		if (sgroup->state != SPDK_NVMF_SUBSYSTEM_ACTIVE) {
2375 			/* The subsystem is not currently active. Queue this request. */
2376 			TAILQ_INSERT_TAIL(&sgroup->queued, req, link);
2377 			return;
2378 		}
2379 
2380 	}
2381 
2382 	/* Place the request on the outstanding list so we can keep track of it */
2383 	TAILQ_INSERT_TAIL(&qpair->outstanding, req, link);
2384 
2385 	if (spdk_unlikely(req->cmd->nvmf_cmd.opcode == SPDK_NVME_OPC_FABRIC)) {
2386 		status = spdk_nvmf_ctrlr_process_fabrics_cmd(req);
2387 	} else if (spdk_unlikely(spdk_nvmf_qpair_is_admin_queue(qpair))) {
2388 		status = spdk_nvmf_ctrlr_process_admin_cmd(req);
2389 	} else {
2390 		status = spdk_nvmf_ctrlr_process_io_cmd(req);
2391 	}
2392 
2393 	if (status == SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE) {
2394 		spdk_nvmf_request_complete(req);
2395 	}
2396 }
2397