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