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