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