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