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