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