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