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