xref: /spdk/lib/nvmf/ctrlr.c (revision ce6a7cd8b8da0100db347d6e1c507dc4cd3f8383)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "spdk/stdinc.h"
35 
36 #include "nvmf_internal.h"
37 #include "transport.h"
38 
39 #include "spdk/endian.h"
40 #include "spdk/io_channel.h"
41 #include "spdk/trace.h"
42 #include "spdk/nvme_spec.h"
43 #include "spdk/string.h"
44 #include "spdk/util.h"
45 #include "spdk/version.h"
46 
47 #include "spdk_internal/log.h"
48 
49 #define MIN_KEEP_ALIVE_TIMEOUT 10000
50 
51 #define MODEL_NUMBER "SPDK bdev Controller"
52 
53 /*
54  * Report the SPDK version as the firmware revision.
55  * SPDK_VERSION_STRING won't fit into FR (only 8 bytes), so try to fit the most important parts.
56  */
57 #define FW_VERSION SPDK_VERSION_MAJOR_STRING SPDK_VERSION_MINOR_STRING SPDK_VERSION_PATCH_STRING
58 
59 static inline void
60 spdk_nvmf_invalid_connect_response(struct spdk_nvmf_fabric_connect_rsp *rsp,
61 				   uint8_t iattr, uint16_t ipo)
62 {
63 	rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
64 	rsp->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM;
65 	rsp->status_code_specific.invalid.iattr = iattr;
66 	rsp->status_code_specific.invalid.ipo = ipo;
67 }
68 
69 #define SPDK_NVMF_INVALID_CONNECT_CMD(rsp, field)	\
70 	spdk_nvmf_invalid_connect_response(rsp, 0, offsetof(struct spdk_nvmf_fabric_connect_cmd, field))
71 #define SPDK_NVMF_INVALID_CONNECT_DATA(rsp, field)	\
72 	spdk_nvmf_invalid_connect_response(rsp, 1, offsetof(struct spdk_nvmf_fabric_connect_data, field))
73 
74 static void
75 ctrlr_add_qpair_and_update_rsp(struct spdk_nvmf_qpair *qpair,
76 			       struct spdk_nvmf_ctrlr *ctrlr,
77 			       struct spdk_nvmf_fabric_connect_rsp *rsp)
78 {
79 	qpair->ctrlr = ctrlr;
80 	ctrlr->num_qpairs++;
81 	TAILQ_INSERT_HEAD(&ctrlr->qpairs, qpair, link);
82 
83 	rsp->status.sc = SPDK_NVME_SC_SUCCESS;
84 	rsp->status_code_specific.success.cntlid = ctrlr->cntlid;
85 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "connect capsule response: cntlid = 0x%04x\n",
86 		      rsp->status_code_specific.success.cntlid);
87 }
88 
89 static void
90 _spdk_nvmf_request_complete(void *ctx)
91 {
92 	struct spdk_nvmf_request *req = ctx;
93 
94 	spdk_nvmf_request_complete(req);
95 }
96 
97 static void
98 _spdk_nvmf_ctrlr_add_admin_qpair(void *ctx)
99 {
100 	struct spdk_nvmf_request *req = ctx;
101 	struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp;
102 	struct spdk_nvmf_qpair *qpair = req->qpair;
103 	struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr;
104 
105 	ctrlr->admin_qpair = qpair;
106 	ctrlr_add_qpair_and_update_rsp(qpair, ctrlr, rsp);
107 	spdk_nvmf_request_complete(req);
108 }
109 
110 static void
111 _spdk_nvmf_subsystem_add_ctrlr(void *ctx)
112 {
113 	struct spdk_nvmf_request *req = ctx;
114 	struct spdk_nvmf_qpair *qpair = req->qpair;
115 	struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp;
116 	struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr;
117 
118 	if (spdk_nvmf_subsystem_add_ctrlr(ctrlr->subsys, ctrlr)) {
119 		SPDK_ERRLOG("Unable to add controller to subsystem\n");
120 		free(ctrlr);
121 		qpair->ctrlr = NULL;
122 		rsp->status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
123 		spdk_thread_send_msg(qpair->group->thread, _spdk_nvmf_request_complete, req);
124 		return;
125 	}
126 
127 	spdk_thread_send_msg(qpair->group->thread, _spdk_nvmf_ctrlr_add_admin_qpair, req);
128 }
129 
130 static struct spdk_nvmf_ctrlr *
131 spdk_nvmf_ctrlr_create(struct spdk_nvmf_subsystem *subsystem,
132 		       struct spdk_nvmf_request *req,
133 		       struct spdk_nvmf_fabric_connect_cmd *connect_cmd,
134 		       struct spdk_nvmf_fabric_connect_data *connect_data)
135 {
136 	struct spdk_nvmf_ctrlr	*ctrlr;
137 	struct spdk_nvmf_tgt	*tgt;
138 
139 	tgt = subsystem->tgt;
140 
141 	ctrlr = calloc(1, sizeof(*ctrlr));
142 	if (ctrlr == NULL) {
143 		SPDK_ERRLOG("Memory allocation failed\n");
144 		return NULL;
145 	}
146 
147 	req->qpair->ctrlr = ctrlr;
148 	TAILQ_INIT(&ctrlr->qpairs);
149 	ctrlr->num_qpairs = 0;
150 	ctrlr->subsys = subsystem;
151 	ctrlr->max_qpairs_allowed = tgt->opts.max_qpairs_per_ctrlr;
152 
153 	ctrlr->feat.keep_alive_timer.bits.kato = connect_cmd->kato;
154 	ctrlr->feat.async_event_configuration.bits.ns_attr_notice = 1;
155 	ctrlr->feat.volatile_write_cache.bits.wce = 1;
156 
157 	/* Subtract 1 for admin queue, 1 for 0's based */
158 	ctrlr->feat.number_of_queues.bits.ncqr = ctrlr->max_qpairs_allowed - 1 - 1;
159 	ctrlr->feat.number_of_queues.bits.nsqr = ctrlr->max_qpairs_allowed - 1 - 1;
160 
161 	memcpy(ctrlr->hostid, connect_data->hostid, sizeof(ctrlr->hostid));
162 
163 	ctrlr->vcprop.cap.raw = 0;
164 	ctrlr->vcprop.cap.bits.cqr = 1; /* NVMe-oF specification required */
165 	ctrlr->vcprop.cap.bits.mqes = tgt->opts.max_queue_depth - 1; /* max queue depth */
166 	ctrlr->vcprop.cap.bits.ams = 0; /* optional arb mechanisms */
167 	ctrlr->vcprop.cap.bits.to = 1; /* ready timeout - 500 msec units */
168 	ctrlr->vcprop.cap.bits.dstrd = 0; /* fixed to 0 for NVMe-oF */
169 	ctrlr->vcprop.cap.bits.css_nvm = 1; /* NVM command set */
170 	ctrlr->vcprop.cap.bits.mpsmin = 0; /* 2 ^ (12 + mpsmin) == 4k */
171 	ctrlr->vcprop.cap.bits.mpsmax = 0; /* 2 ^ (12 + mpsmax) == 4k */
172 
173 	/* Version Supported: 1.3 */
174 	ctrlr->vcprop.vs.bits.mjr = 1;
175 	ctrlr->vcprop.vs.bits.mnr = 3;
176 	ctrlr->vcprop.vs.bits.ter = 0;
177 
178 	ctrlr->vcprop.cc.raw = 0;
179 	ctrlr->vcprop.cc.bits.en = 0; /* Init controller disabled */
180 
181 	ctrlr->vcprop.csts.raw = 0;
182 	ctrlr->vcprop.csts.bits.rdy = 0; /* Init controller as not ready */
183 
184 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "cap 0x%" PRIx64 "\n", ctrlr->vcprop.cap.raw);
185 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "vs 0x%x\n", ctrlr->vcprop.vs.raw);
186 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "cc 0x%x\n", ctrlr->vcprop.cc.raw);
187 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "csts 0x%x\n", ctrlr->vcprop.csts.raw);
188 
189 	spdk_thread_send_msg(subsystem->thread, _spdk_nvmf_subsystem_add_ctrlr, req);
190 
191 	return ctrlr;
192 }
193 
194 static void ctrlr_destruct(void *ctx)
195 {
196 	struct spdk_nvmf_ctrlr *ctrlr = ctx;
197 
198 	spdk_nvmf_subsystem_remove_ctrlr(ctrlr->subsys, ctrlr);
199 	free(ctrlr);
200 }
201 
202 void
203 spdk_nvmf_ctrlr_destruct(struct spdk_nvmf_ctrlr *ctrlr)
204 {
205 	while (!TAILQ_EMPTY(&ctrlr->qpairs)) {
206 		struct spdk_nvmf_qpair *qpair = TAILQ_FIRST(&ctrlr->qpairs);
207 
208 		TAILQ_REMOVE(&ctrlr->qpairs, qpair, link);
209 		ctrlr->num_qpairs--;
210 		spdk_nvmf_transport_qpair_fini(qpair);
211 	}
212 
213 	ctrlr_destruct(ctrlr);
214 }
215 
216 static void
217 spdk_nvmf_ctrlr_add_io_qpair(void *ctx)
218 {
219 	struct spdk_nvmf_request *req = ctx;
220 	struct spdk_nvmf_fabric_connect_cmd *cmd = &req->cmd->connect_cmd;
221 	struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp;
222 	struct spdk_nvmf_qpair *qpair = req->qpair;
223 	struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr;
224 
225 	/* Unit test will check qpair->ctrlr after calling spdk_nvmf_ctrlr_connect.
226 	  * For error case, the value should be NULL. So set it to NULL at first.
227 	  */
228 	qpair->ctrlr = NULL;
229 
230 	if (ctrlr->subsys->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) {
231 		SPDK_ERRLOG("I/O connect not allowed on discovery controller\n");
232 		SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid);
233 		goto end;
234 	}
235 
236 	if (!ctrlr->vcprop.cc.bits.en) {
237 		SPDK_ERRLOG("Got I/O connect before ctrlr was enabled\n");
238 		SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid);
239 		goto end;
240 	}
241 
242 	if (1u << ctrlr->vcprop.cc.bits.iosqes != sizeof(struct spdk_nvme_cmd)) {
243 		SPDK_ERRLOG("Got I/O connect with invalid IOSQES %u\n",
244 			    ctrlr->vcprop.cc.bits.iosqes);
245 		SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid);
246 		goto end;
247 	}
248 
249 	if (1u << ctrlr->vcprop.cc.bits.iocqes != sizeof(struct spdk_nvme_cpl)) {
250 		SPDK_ERRLOG("Got I/O connect with invalid IOCQES %u\n",
251 			    ctrlr->vcprop.cc.bits.iocqes);
252 		SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid);
253 		goto end;
254 	}
255 
256 	if (spdk_nvmf_ctrlr_get_qpair(ctrlr, cmd->qid)) {
257 		SPDK_ERRLOG("Got I/O connect with duplicate QID %u\n", cmd->qid);
258 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
259 		rsp->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
260 		goto end;
261 	}
262 
263 	/* check if we would exceed ctrlr connection limit */
264 	if (ctrlr->num_qpairs >= ctrlr->max_qpairs_allowed) {
265 		SPDK_ERRLOG("qpair limit %d\n", ctrlr->num_qpairs);
266 		rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
267 		rsp->status.sc = SPDK_NVMF_FABRIC_SC_CONTROLLER_BUSY;
268 		goto end;
269 	}
270 
271 	ctrlr_add_qpair_and_update_rsp(qpair, ctrlr, rsp);
272 
273 end:
274 	spdk_thread_send_msg(qpair->group->thread, _spdk_nvmf_request_complete, req);
275 }
276 
277 static void
278 ctrlr_delete_qpair(void *ctx)
279 {
280 	struct spdk_nvmf_qpair *qpair = ctx;
281 	struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr;
282 
283 	assert(ctrlr != NULL);
284 
285 	ctrlr->num_qpairs--;
286 	TAILQ_REMOVE(&ctrlr->qpairs, qpair, link);
287 	spdk_nvmf_transport_qpair_fini(qpair);
288 
289 	if (ctrlr->num_qpairs == 0) {
290 		assert(ctrlr->subsys != NULL);
291 		assert(ctrlr->subsys->thread != NULL);
292 		spdk_thread_send_msg(ctrlr->subsys->thread, ctrlr_destruct, ctrlr);
293 	}
294 }
295 
296 static void
297 _spdk_nvmf_ctrlr_add_io_qpair(void *ctx)
298 {
299 	struct spdk_nvmf_request *req = ctx;
300 	struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp;
301 	struct spdk_nvmf_fabric_connect_data *data = req->data;
302 	struct spdk_nvmf_ctrlr *ctrlr;
303 	struct spdk_nvmf_qpair *qpair = req->qpair;
304 	struct spdk_nvmf_qpair *admin_qpair;
305 	struct spdk_nvmf_tgt *tgt = qpair->transport->tgt;
306 	struct spdk_nvmf_subsystem *subsystem;
307 
308 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Connect I/O Queue for controller id 0x%x\n", data->cntlid);
309 
310 	subsystem = spdk_nvmf_tgt_find_subsystem(tgt, data->subnqn);
311 	/* We already checked this in spdk_nvmf_ctrlr_connect */
312 	assert(subsystem != NULL);
313 
314 	ctrlr = spdk_nvmf_subsystem_get_ctrlr(subsystem, data->cntlid);
315 	if (ctrlr == NULL) {
316 		SPDK_ERRLOG("Unknown controller ID 0x%x\n", data->cntlid);
317 		SPDK_NVMF_INVALID_CONNECT_DATA(rsp, cntlid);
318 		spdk_thread_send_msg(qpair->group->thread, _spdk_nvmf_request_complete, req);
319 		return;
320 	}
321 
322 	admin_qpair = ctrlr->admin_qpair;
323 	qpair->ctrlr = ctrlr;
324 	spdk_thread_send_msg(admin_qpair->group->thread, spdk_nvmf_ctrlr_add_io_qpair, req);
325 }
326 
327 static int
328 spdk_nvmf_ctrlr_connect(struct spdk_nvmf_request *req)
329 {
330 	struct spdk_nvmf_fabric_connect_data *data = req->data;
331 	struct spdk_nvmf_fabric_connect_cmd *cmd = &req->cmd->connect_cmd;
332 	struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp;
333 	struct spdk_nvmf_qpair *qpair = req->qpair;
334 	struct spdk_nvmf_tgt *tgt = qpair->transport->tgt;
335 	struct spdk_nvmf_ctrlr *ctrlr;
336 	struct spdk_nvmf_subsystem *subsystem;
337 	const char *subnqn, *hostnqn;
338 	void *end;
339 
340 	if (req->length < sizeof(struct spdk_nvmf_fabric_connect_data)) {
341 		SPDK_ERRLOG("Connect command data length 0x%x too small\n", req->length);
342 		rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
343 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
344 	}
345 
346 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "recfmt 0x%x qid %u sqsize %u\n",
347 		      cmd->recfmt, cmd->qid, cmd->sqsize);
348 
349 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Connect data:\n");
350 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "  cntlid:  0x%04x\n", data->cntlid);
351 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "  hostid: %08x-%04x-%04x-%02x%02x-%04x%08x ***\n",
352 		      ntohl(*(uint32_t *)&data->hostid[0]),
353 		      ntohs(*(uint16_t *)&data->hostid[4]),
354 		      ntohs(*(uint16_t *)&data->hostid[6]),
355 		      data->hostid[8],
356 		      data->hostid[9],
357 		      ntohs(*(uint16_t *)&data->hostid[10]),
358 		      ntohl(*(uint32_t *)&data->hostid[12]));
359 
360 	if (cmd->recfmt != 0) {
361 		SPDK_ERRLOG("Connect command unsupported RECFMT %u\n", cmd->recfmt);
362 		rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
363 		rsp->status.sc = SPDK_NVMF_FABRIC_SC_INCOMPATIBLE_FORMAT;
364 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
365 	}
366 
367 	/* Ensure that subnqn is null terminated */
368 	end = memchr(data->subnqn, '\0', SPDK_NVMF_NQN_MAX_LEN + 1);
369 	if (!end) {
370 		SPDK_ERRLOG("Connect SUBNQN is not null terminated\n");
371 		SPDK_NVMF_INVALID_CONNECT_DATA(rsp, subnqn);
372 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
373 	}
374 	subnqn = data->subnqn;
375 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "  subnqn: \"%s\"\n", subnqn);
376 
377 	subsystem = spdk_nvmf_tgt_find_subsystem(tgt, subnqn);
378 	if (subsystem == NULL) {
379 		SPDK_ERRLOG("Could not find subsystem '%s'\n", subnqn);
380 		SPDK_NVMF_INVALID_CONNECT_DATA(rsp, subnqn);
381 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
382 	}
383 
384 	/* Ensure that hostnqn is null terminated */
385 	end = memchr(data->hostnqn, '\0', SPDK_NVMF_NQN_MAX_LEN + 1);
386 	if (!end) {
387 		SPDK_ERRLOG("Connect HOSTNQN is not null terminated\n");
388 		SPDK_NVMF_INVALID_CONNECT_DATA(rsp, hostnqn);
389 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
390 	}
391 	hostnqn = data->hostnqn;
392 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "  hostnqn: \"%s\"\n", hostnqn);
393 
394 	if (!spdk_nvmf_subsystem_host_allowed(subsystem, hostnqn)) {
395 		SPDK_ERRLOG("Subsystem '%s' does not allow host '%s'\n", subnqn, hostnqn);
396 		rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
397 		rsp->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_HOST;
398 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
399 	}
400 
401 	/*
402 	 * SQSIZE is a 0-based value, so it must be at least 1 (minimum queue depth is 2) and
403 	 *  strictly less than max_queue_depth.
404 	 */
405 	if (cmd->sqsize == 0 || cmd->sqsize >= tgt->opts.max_queue_depth) {
406 		SPDK_ERRLOG("Invalid SQSIZE %u (min 1, max %u)\n",
407 			    cmd->sqsize, tgt->opts.max_queue_depth - 1);
408 		SPDK_NVMF_INVALID_CONNECT_CMD(rsp, sqsize);
409 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
410 	}
411 	qpair->sq_head_max = cmd->sqsize;
412 	qpair->qid = cmd->qid;
413 
414 	if (cmd->qid == 0) {
415 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Connect Admin Queue for controller ID 0x%x\n", data->cntlid);
416 
417 		if (data->cntlid != 0xFFFF) {
418 			/* This NVMf target only supports dynamic mode. */
419 			SPDK_ERRLOG("The NVMf target only supports dynamic mode (CNTLID = 0x%x).\n", data->cntlid);
420 			SPDK_NVMF_INVALID_CONNECT_DATA(rsp, cntlid);
421 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
422 		}
423 
424 		/* Establish a new ctrlr */
425 		ctrlr = spdk_nvmf_ctrlr_create(subsystem, req, cmd, data);
426 		if (!ctrlr) {
427 			SPDK_ERRLOG("spdk_nvmf_ctrlr_create() failed\n");
428 			rsp->status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
429 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
430 		} else {
431 			return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
432 		}
433 	} else {
434 		spdk_thread_send_msg(subsystem->thread, _spdk_nvmf_ctrlr_add_io_qpair, req);
435 		return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
436 	}
437 }
438 
439 void
440 spdk_nvmf_ctrlr_disconnect(struct spdk_nvmf_qpair *qpair)
441 {
442 	struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr;
443 	struct spdk_nvmf_qpair *admin_qpair = ctrlr->admin_qpair;
444 
445 	spdk_thread_send_msg(admin_qpair->group->thread, ctrlr_delete_qpair, qpair);
446 }
447 
448 struct spdk_nvmf_qpair *
449 spdk_nvmf_ctrlr_get_qpair(struct spdk_nvmf_ctrlr *ctrlr, uint16_t qid)
450 {
451 	struct spdk_nvmf_qpair *qpair;
452 
453 	TAILQ_FOREACH(qpair, &ctrlr->qpairs, link) {
454 		if (qpair->qid == qid) {
455 			return qpair;
456 		}
457 	}
458 	return NULL;
459 }
460 
461 static struct spdk_nvmf_request *
462 spdk_nvmf_qpair_get_request(struct spdk_nvmf_qpair *qpair, uint16_t cid)
463 {
464 	/* TODO: track list of outstanding requests in qpair? */
465 	return NULL;
466 }
467 
468 static uint64_t
469 nvmf_prop_get_cap(struct spdk_nvmf_ctrlr *ctrlr)
470 {
471 	return ctrlr->vcprop.cap.raw;
472 }
473 
474 static uint64_t
475 nvmf_prop_get_vs(struct spdk_nvmf_ctrlr *ctrlr)
476 {
477 	return ctrlr->vcprop.vs.raw;
478 }
479 
480 static uint64_t
481 nvmf_prop_get_cc(struct spdk_nvmf_ctrlr *ctrlr)
482 {
483 	return ctrlr->vcprop.cc.raw;
484 }
485 
486 static bool
487 nvmf_prop_set_cc(struct spdk_nvmf_ctrlr *ctrlr, uint64_t value)
488 {
489 	union spdk_nvme_cc_register cc, diff;
490 
491 	cc.raw = (uint32_t)value;
492 
493 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "cur CC: 0x%08x\n", ctrlr->vcprop.cc.raw);
494 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "new CC: 0x%08x\n", cc.raw);
495 
496 	/*
497 	 * Calculate which bits changed between the current and new CC.
498 	 * Mark each bit as 0 once it is handled to determine if any unhandled bits were changed.
499 	 */
500 	diff.raw = cc.raw ^ ctrlr->vcprop.cc.raw;
501 
502 	if (diff.bits.en) {
503 		if (cc.bits.en) {
504 			SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Property Set CC Enable!\n");
505 			ctrlr->vcprop.cc.bits.en = 1;
506 			ctrlr->vcprop.csts.bits.rdy = 1;
507 		} else {
508 			SPDK_ERRLOG("CC.EN transition from 1 to 0 (reset) not implemented!\n");
509 
510 		}
511 		diff.bits.en = 0;
512 	}
513 
514 	if (diff.bits.shn) {
515 		if (cc.bits.shn == SPDK_NVME_SHN_NORMAL ||
516 		    cc.bits.shn == SPDK_NVME_SHN_ABRUPT) {
517 			SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Property Set CC Shutdown %u%ub!\n",
518 				      cc.bits.shn >> 1, cc.bits.shn & 1);
519 			ctrlr->vcprop.cc.bits.shn = cc.bits.shn;
520 			ctrlr->vcprop.cc.bits.en = 0;
521 			ctrlr->vcprop.csts.bits.rdy = 0;
522 			ctrlr->vcprop.csts.bits.shst = SPDK_NVME_SHST_COMPLETE;
523 		} else if (cc.bits.shn == 0) {
524 			ctrlr->vcprop.cc.bits.shn = 0;
525 		} else {
526 			SPDK_ERRLOG("Prop Set CC: Invalid SHN value %u%ub\n",
527 				    cc.bits.shn >> 1, cc.bits.shn & 1);
528 			return false;
529 		}
530 		diff.bits.shn = 0;
531 	}
532 
533 	if (diff.bits.iosqes) {
534 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Prop Set IOSQES = %u (%u bytes)\n",
535 			      cc.bits.iosqes, 1u << cc.bits.iosqes);
536 		ctrlr->vcprop.cc.bits.iosqes = cc.bits.iosqes;
537 		diff.bits.iosqes = 0;
538 	}
539 
540 	if (diff.bits.iocqes) {
541 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Prop Set IOCQES = %u (%u bytes)\n",
542 			      cc.bits.iocqes, 1u << cc.bits.iocqes);
543 		ctrlr->vcprop.cc.bits.iocqes = cc.bits.iocqes;
544 		diff.bits.iocqes = 0;
545 	}
546 
547 	if (diff.raw != 0) {
548 		SPDK_ERRLOG("Prop Set CC toggled reserved bits 0x%x!\n", diff.raw);
549 		return false;
550 	}
551 
552 	return true;
553 }
554 
555 static uint64_t
556 nvmf_prop_get_csts(struct spdk_nvmf_ctrlr *ctrlr)
557 {
558 	return ctrlr->vcprop.csts.raw;
559 }
560 
561 struct nvmf_prop {
562 	uint32_t ofst;
563 	uint8_t size;
564 	char name[11];
565 	uint64_t (*get_cb)(struct spdk_nvmf_ctrlr *ctrlr);
566 	bool (*set_cb)(struct spdk_nvmf_ctrlr *ctrlr, uint64_t value);
567 };
568 
569 #define PROP(field, size, get_cb, set_cb) \
570 	{ \
571 		offsetof(struct spdk_nvme_registers, field), \
572 		SPDK_NVMF_PROP_SIZE_##size, \
573 		#field, \
574 		get_cb, set_cb \
575 	}
576 
577 static const struct nvmf_prop nvmf_props[] = {
578 	PROP(cap,  8, nvmf_prop_get_cap,  NULL),
579 	PROP(vs,   4, nvmf_prop_get_vs,   NULL),
580 	PROP(cc,   4, nvmf_prop_get_cc,   nvmf_prop_set_cc),
581 	PROP(csts, 4, nvmf_prop_get_csts, NULL),
582 };
583 
584 static const struct nvmf_prop *
585 find_prop(uint32_t ofst)
586 {
587 	size_t i;
588 
589 	for (i = 0; i < SPDK_COUNTOF(nvmf_props); i++) {
590 		const struct nvmf_prop *prop = &nvmf_props[i];
591 
592 		if (prop->ofst == ofst) {
593 			return prop;
594 		}
595 	}
596 
597 	return NULL;
598 }
599 
600 static int
601 spdk_nvmf_property_get(struct spdk_nvmf_request *req)
602 {
603 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
604 	struct spdk_nvmf_fabric_prop_get_cmd *cmd = &req->cmd->prop_get_cmd;
605 	struct spdk_nvmf_fabric_prop_get_rsp *response = &req->rsp->prop_get_rsp;
606 	const struct nvmf_prop *prop;
607 
608 	response->status.sc = 0;
609 	response->value.u64 = 0;
610 
611 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "size %d, offset 0x%x\n",
612 		      cmd->attrib.size, cmd->ofst);
613 
614 	if (cmd->attrib.size != SPDK_NVMF_PROP_SIZE_4 &&
615 	    cmd->attrib.size != SPDK_NVMF_PROP_SIZE_8) {
616 		SPDK_ERRLOG("Invalid size value %d\n", cmd->attrib.size);
617 		response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM;
618 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
619 	}
620 
621 	prop = find_prop(cmd->ofst);
622 	if (prop == NULL || prop->get_cb == NULL) {
623 		/* Reserved properties return 0 when read */
624 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
625 	}
626 
627 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "name: %s\n", prop->name);
628 	if (cmd->attrib.size != prop->size) {
629 		SPDK_ERRLOG("offset 0x%x size mismatch: cmd %u, prop %u\n",
630 			    cmd->ofst, cmd->attrib.size, prop->size);
631 		response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM;
632 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
633 	}
634 
635 	response->value.u64 = prop->get_cb(ctrlr);
636 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "response value: 0x%" PRIx64 "\n", response->value.u64);
637 
638 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
639 }
640 
641 static int
642 spdk_nvmf_property_set(struct spdk_nvmf_request *req)
643 {
644 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
645 	struct spdk_nvmf_fabric_prop_set_cmd *cmd = &req->cmd->prop_set_cmd;
646 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
647 	const struct nvmf_prop *prop;
648 	uint64_t value;
649 
650 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "size %d, offset 0x%x, value 0x%" PRIx64 "\n",
651 		      cmd->attrib.size, cmd->ofst, cmd->value.u64);
652 
653 	prop = find_prop(cmd->ofst);
654 	if (prop == NULL || prop->set_cb == NULL) {
655 		SPDK_ERRLOG("Invalid offset 0x%x\n", cmd->ofst);
656 		response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM;
657 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
658 	}
659 
660 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "name: %s\n", prop->name);
661 	if (cmd->attrib.size != prop->size) {
662 		SPDK_ERRLOG("offset 0x%x size mismatch: cmd %u, prop %u\n",
663 			    cmd->ofst, cmd->attrib.size, prop->size);
664 		response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM;
665 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
666 	}
667 
668 	value = cmd->value.u64;
669 	if (prop->size == SPDK_NVMF_PROP_SIZE_4) {
670 		value = (uint32_t)value;
671 	}
672 
673 	if (!prop->set_cb(ctrlr, value)) {
674 		SPDK_ERRLOG("prop set_cb failed\n");
675 		response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM;
676 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
677 	}
678 
679 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
680 }
681 
682 static int
683 spdk_nvmf_ctrlr_set_features_arbitration(struct spdk_nvmf_request *req)
684 {
685 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
686 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
687 
688 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Arbitration (cdw11 = 0x%0x)\n", cmd->cdw11);
689 
690 	ctrlr->feat.arbitration.raw = cmd->cdw11;
691 	ctrlr->feat.arbitration.bits.reserved = 0;
692 
693 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
694 }
695 
696 static int
697 spdk_nvmf_ctrlr_set_features_power_management(struct spdk_nvmf_request *req)
698 {
699 	union spdk_nvme_feat_power_management opts;
700 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
701 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
702 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
703 
704 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Power Management (cdw11 = 0x%0x)\n", cmd->cdw11);
705 	opts.raw = cmd->cdw11;
706 
707 	/* Only PS = 0 is allowed, since we report NPSS = 0 */
708 	if (opts.bits.ps != 0) {
709 		SPDK_ERRLOG("Invalid power state %u\n", opts.bits.ps);
710 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
711 		rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
712 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
713 	}
714 
715 	ctrlr->feat.power_management.raw = cmd->cdw11;
716 	ctrlr->feat.power_management.bits.reserved = 0;
717 
718 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
719 }
720 
721 static bool
722 temp_threshold_opts_valid(const union spdk_nvme_feat_temperature_threshold *opts)
723 {
724 	/*
725 	 * Valid TMPSEL values:
726 	 *  0000b - 1000b: temperature sensors
727 	 *  1111b: set all implemented temperature sensors
728 	 */
729 	if (opts->bits.tmpsel >= 9 && opts->bits.tmpsel != 15) {
730 		/* 1001b - 1110b: reserved */
731 		SPDK_ERRLOG("Invalid TMPSEL %u\n", opts->bits.tmpsel);
732 		return false;
733 	}
734 
735 	/*
736 	 * Valid THSEL values:
737 	 *  00b: over temperature threshold
738 	 *  01b: under temperature threshold
739 	 */
740 	if (opts->bits.thsel > 1) {
741 		/* 10b - 11b: reserved */
742 		SPDK_ERRLOG("Invalid THSEL %u\n", opts->bits.thsel);
743 		return false;
744 	}
745 
746 	return true;
747 }
748 
749 static int
750 spdk_nvmf_ctrlr_set_features_temperature_threshold(struct spdk_nvmf_request *req)
751 {
752 	union spdk_nvme_feat_temperature_threshold opts;
753 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
754 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
755 
756 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Temperature Threshold (cdw11 = 0x%0x)\n", cmd->cdw11);
757 	opts.raw = cmd->cdw11;
758 
759 	if (!temp_threshold_opts_valid(&opts)) {
760 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
761 		rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
762 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
763 	}
764 
765 	/* TODO: no sensors implemented - ignore new values */
766 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
767 }
768 
769 static int
770 spdk_nvmf_ctrlr_get_features_temperature_threshold(struct spdk_nvmf_request *req)
771 {
772 	union spdk_nvme_feat_temperature_threshold opts;
773 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
774 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
775 
776 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Get Features - Temperature Threshold (cdw11 = 0x%0x)\n", cmd->cdw11);
777 	opts.raw = cmd->cdw11;
778 
779 	if (!temp_threshold_opts_valid(&opts)) {
780 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
781 		rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
782 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
783 	}
784 
785 	/* TODO: no sensors implemented - return 0 for all thresholds */
786 	rsp->cdw0 = 0;
787 
788 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
789 }
790 
791 static int
792 spdk_nvmf_ctrlr_set_features_error_recovery(struct spdk_nvmf_request *req)
793 {
794 	union spdk_nvme_feat_error_recovery opts;
795 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
796 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
797 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
798 
799 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Error Recovery (cdw11 = 0x%0x)\n", cmd->cdw11);
800 	opts.raw = cmd->cdw11;
801 
802 	if (opts.bits.dulbe) {
803 		/*
804 		 * Host is not allowed to set this bit, since we don't advertise it in
805 		 * Identify Namespace.
806 		 */
807 		SPDK_ERRLOG("Host set unsupported DULBE bit\n");
808 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
809 		rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
810 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
811 	}
812 
813 	ctrlr->feat.error_recovery.raw = cmd->cdw11;
814 	ctrlr->feat.error_recovery.bits.reserved = 0;
815 
816 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
817 }
818 
819 static int
820 spdk_nvmf_ctrlr_set_features_volatile_write_cache(struct spdk_nvmf_request *req)
821 {
822 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
823 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
824 
825 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Volatile Write Cache (cdw11 = 0x%0x)\n", cmd->cdw11);
826 
827 	ctrlr->feat.volatile_write_cache.raw = cmd->cdw11;
828 	ctrlr->feat.volatile_write_cache.bits.reserved = 0;
829 
830 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
831 }
832 
833 static int
834 spdk_nvmf_ctrlr_set_features_write_atomicity(struct spdk_nvmf_request *req)
835 {
836 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
837 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
838 
839 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Write Atomicity (cdw11 = 0x%0x)\n", cmd->cdw11);
840 
841 	ctrlr->feat.write_atomicity.raw = cmd->cdw11;
842 	ctrlr->feat.write_atomicity.bits.reserved = 0;
843 
844 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
845 }
846 
847 static int
848 spdk_nvmf_ctrlr_set_features_host_identifier(struct spdk_nvmf_request *req)
849 {
850 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
851 
852 	SPDK_ERRLOG("Set Features - Host Identifier not allowed\n");
853 	response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
854 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
855 }
856 
857 static int
858 spdk_nvmf_ctrlr_get_features_host_identifier(struct spdk_nvmf_request *req)
859 {
860 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
861 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
862 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
863 	union spdk_nvme_feat_host_identifier opts;
864 
865 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Get Features - Host Identifier\n");
866 
867 	opts.raw = cmd->cdw11;
868 	if (!opts.bits.exhid) {
869 		/* NVMe over Fabrics requires EXHID=1 (128-bit/16-byte host ID) */
870 		SPDK_ERRLOG("Get Features - Host Identifier with EXHID=0 not allowed\n");
871 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
872 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
873 	}
874 
875 	if (req->data == NULL || req->length < sizeof(ctrlr->hostid)) {
876 		SPDK_ERRLOG("Invalid data buffer for Get Features - Host Identifier\n");
877 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
878 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
879 	}
880 
881 	memcpy(req->data, ctrlr->hostid, sizeof(ctrlr->hostid));
882 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
883 }
884 
885 static int
886 spdk_nvmf_ctrlr_set_features_keep_alive_timer(struct spdk_nvmf_request *req)
887 {
888 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
889 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
890 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
891 
892 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Keep Alive Timer (%u ms)\n", cmd->cdw11);
893 
894 	if (cmd->cdw11 == 0) {
895 		rsp->status.sc = SPDK_NVME_SC_KEEP_ALIVE_INVALID;
896 	} else if (cmd->cdw11 < MIN_KEEP_ALIVE_TIMEOUT) {
897 		ctrlr->feat.keep_alive_timer.bits.kato = MIN_KEEP_ALIVE_TIMEOUT;
898 	} else {
899 		ctrlr->feat.keep_alive_timer.bits.kato = cmd->cdw11;
900 	}
901 
902 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Keep Alive Timer set to %u ms\n",
903 		      ctrlr->feat.keep_alive_timer.bits.kato);
904 
905 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
906 }
907 
908 static int
909 spdk_nvmf_ctrlr_set_features_number_of_queues(struct spdk_nvmf_request *req)
910 {
911 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
912 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
913 
914 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Number of Queues, cdw11 0x%x\n",
915 		      req->cmd->nvme_cmd.cdw11);
916 
917 	/* verify that the contoller is ready to process commands */
918 	if (ctrlr->num_qpairs > 1) {
919 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Queue pairs already active!\n");
920 		rsp->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
921 	} else {
922 		/*
923 		 * Ignore the value requested by the host -
924 		 * always return the pre-configured value based on max_qpairs_allowed.
925 		 */
926 		rsp->cdw0 = ctrlr->feat.number_of_queues.raw;
927 	}
928 
929 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
930 }
931 
932 static int
933 spdk_nvmf_ctrlr_set_features_async_event_configuration(struct spdk_nvmf_request *req)
934 {
935 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
936 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
937 
938 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Async Event Configuration, cdw11 0x%08x\n",
939 		      cmd->cdw11);
940 	ctrlr->feat.async_event_configuration.raw = cmd->cdw11;
941 	ctrlr->feat.async_event_configuration.bits.reserved = 0;
942 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
943 }
944 
945 static int
946 spdk_nvmf_ctrlr_async_event_request(struct spdk_nvmf_request *req)
947 {
948 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
949 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
950 
951 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Async Event Request\n");
952 
953 	/* Only one asynchronous event is supported for now */
954 	if (ctrlr->aer_req != NULL) {
955 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "AERL exceeded\n");
956 		rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
957 		rsp->status.sc = SPDK_NVME_SC_ASYNC_EVENT_REQUEST_LIMIT_EXCEEDED;
958 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
959 	}
960 
961 	if (ctrlr->notice_event.bits.async_event_type ==
962 	    SPDK_NVME_ASYNC_EVENT_TYPE_NOTICE) {
963 		rsp->cdw0 = ctrlr->notice_event.raw;
964 		ctrlr->notice_event.raw = 0;
965 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
966 	}
967 
968 	ctrlr->aer_req = req;
969 	return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
970 }
971 
972 static void
973 spdk_nvmf_get_firmware_slot_log_page(void *buffer, uint64_t offset, uint32_t length)
974 {
975 	struct spdk_nvme_firmware_page fw_page;
976 	size_t copy_len;
977 
978 	memset(&fw_page, 0, sizeof(fw_page));
979 	fw_page.afi.active_slot = 1;
980 	fw_page.afi.next_reset_slot = 0;
981 	spdk_strcpy_pad(fw_page.revision[0], FW_VERSION, sizeof(fw_page.revision[0]), ' ');
982 
983 	if (offset < sizeof(fw_page)) {
984 		copy_len = spdk_min(sizeof(fw_page) - offset, length);
985 		if (copy_len > 0) {
986 			memcpy(buffer, (const char *)&fw_page + offset, copy_len);
987 		}
988 	}
989 }
990 
991 void
992 spdk_nvmf_ctrlr_ns_changed(struct spdk_nvmf_ctrlr *ctrlr, uint32_t nsid)
993 {
994 	uint16_t max_changes = SPDK_COUNTOF(ctrlr->changed_ns_list.ns_list);
995 	uint16_t i;
996 	bool found = false;
997 
998 	for (i = 0; i < ctrlr->changed_ns_list_count; i++) {
999 		if (ctrlr->changed_ns_list.ns_list[i] == nsid) {
1000 			/* nsid is already in the list */
1001 			found = true;
1002 			break;
1003 		}
1004 	}
1005 
1006 	if (!found) {
1007 		if (ctrlr->changed_ns_list_count == max_changes) {
1008 			/* Out of space - set first entry to FFFFFFFFh and zero-fill the rest. */
1009 			ctrlr->changed_ns_list.ns_list[0] = 0xFFFFFFFFu;
1010 			for (i = 1; i < max_changes; i++) {
1011 				ctrlr->changed_ns_list.ns_list[i] = 0;
1012 			}
1013 		} else {
1014 			ctrlr->changed_ns_list.ns_list[ctrlr->changed_ns_list_count++] = nsid;
1015 		}
1016 	}
1017 
1018 	spdk_nvmf_ctrlr_async_event_ns_notice(ctrlr);
1019 }
1020 
1021 static void
1022 spdk_nvmf_get_changed_ns_list_log_page(struct spdk_nvmf_ctrlr *ctrlr,
1023 				       void *buffer, uint64_t offset, uint32_t length)
1024 {
1025 	size_t copy_length;
1026 
1027 	if (offset < sizeof(ctrlr->changed_ns_list)) {
1028 		copy_length = spdk_min(length, sizeof(ctrlr->changed_ns_list) - offset);
1029 		if (copy_length) {
1030 			memcpy(buffer, (char *)&ctrlr->changed_ns_list + offset, copy_length);
1031 		}
1032 	}
1033 
1034 	/* Clear log page each time it is read */
1035 	ctrlr->changed_ns_list_count = 0;
1036 	memset(&ctrlr->changed_ns_list, 0, sizeof(ctrlr->changed_ns_list));
1037 }
1038 
1039 /* The structure can be modified if we provide support for other commands in future */
1040 static const struct spdk_nvme_cmds_and_effect_log_page g_cmds_and_effect_log_page = {
1041 	.admin_cmds_supported = {
1042 		/* CSUPP, LBCC, NCC, NIC, CCC, CSE */
1043 		/* Get Log Page */
1044 		[SPDK_NVME_OPC_GET_LOG_PAGE]		= {1, 0, 0, 0, 0, 0, 0, 0},
1045 		/* Identify */
1046 		[SPDK_NVME_OPC_IDENTIFY]		= {1, 0, 0, 0, 0, 0, 0, 0},
1047 		/* Abort */
1048 		[SPDK_NVME_OPC_ABORT]			= {1, 0, 0, 0, 0, 0, 0, 0},
1049 		/* Set Features */
1050 		[SPDK_NVME_OPC_SET_FEATURES]		= {1, 0, 0, 0, 0, 0, 0, 0},
1051 		/* Get Features */
1052 		[SPDK_NVME_OPC_GET_FEATURES]		= {1, 0, 0, 0, 0, 0, 0, 0},
1053 		/* Async Event Request */
1054 		[SPDK_NVME_OPC_ASYNC_EVENT_REQUEST]	= {1, 0, 0, 0, 0, 0, 0, 0},
1055 		/* Keep Alive */
1056 		[SPDK_NVME_OPC_KEEP_ALIVE]		= {1, 0, 0, 0, 0, 0, 0, 0},
1057 	},
1058 	.io_cmds_supported = {
1059 		/* FLUSH */
1060 		[SPDK_NVME_OPC_FLUSH]			= {1, 1, 0, 0, 0, 0, 0, 0},
1061 		/* WRITE */
1062 		[SPDK_NVME_OPC_WRITE]			= {1, 1, 0, 0, 0, 0, 0, 0},
1063 		/* READ */
1064 		[SPDK_NVME_OPC_READ]			= {1, 0, 0, 0, 0, 0, 0, 0},
1065 		/* WRITE ZEROES */
1066 		[SPDK_NVME_OPC_WRITE_ZEROES]		= {1, 1, 0, 0, 0, 0, 0, 0},
1067 		/* DATASET MANAGEMENT */
1068 		[SPDK_NVME_OPC_DATASET_MANAGEMENT]	= {1, 1, 0, 0, 0, 0, 0, 0},
1069 	},
1070 };
1071 
1072 static void
1073 spdk_nvmf_get_cmds_and_effects_log_page(void *buffer,
1074 					uint64_t offset, uint32_t length)
1075 {
1076 	uint32_t page_size = sizeof(struct spdk_nvme_cmds_and_effect_log_page);
1077 	size_t copy_len = 0;
1078 	size_t zero_len = length;
1079 
1080 	if (offset < page_size) {
1081 		copy_len = spdk_min(page_size - offset, length);
1082 		zero_len -= copy_len;
1083 		memcpy(buffer, (char *)(&g_cmds_and_effect_log_page) + offset, copy_len);
1084 	}
1085 
1086 	if (zero_len) {
1087 		memset((char *)buffer + copy_len, 0, zero_len);
1088 	}
1089 }
1090 
1091 static int
1092 spdk_nvmf_ctrlr_get_log_page(struct spdk_nvmf_request *req)
1093 {
1094 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1095 	struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys;
1096 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1097 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
1098 	uint64_t offset, len;
1099 	uint32_t numdl, numdu;
1100 	uint8_t lid;
1101 
1102 	if (req->data == NULL) {
1103 		SPDK_ERRLOG("get log command with no buffer\n");
1104 		response->status.sct = SPDK_NVME_SCT_GENERIC;
1105 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1106 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1107 	}
1108 
1109 	offset = (uint64_t)cmd->cdw12 | ((uint64_t)cmd->cdw13 << 32);
1110 	if (offset & 3) {
1111 		SPDK_ERRLOG("Invalid log page offset 0x%" PRIx64 "\n", offset);
1112 		response->status.sct = SPDK_NVME_SCT_GENERIC;
1113 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1114 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1115 	}
1116 
1117 	numdl = (cmd->cdw10 >> 16) & 0xFFFFu;
1118 	numdu = (cmd->cdw11) & 0xFFFFu;
1119 	len = ((numdu << 16) + numdl + (uint64_t)1) * 4;
1120 	if (len > req->length) {
1121 		SPDK_ERRLOG("Get log page: len (%" PRIu64 ") > buf size (%u)\n",
1122 			    len, req->length);
1123 		response->status.sct = SPDK_NVME_SCT_GENERIC;
1124 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1125 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1126 	}
1127 
1128 	lid = cmd->cdw10 & 0xFF;
1129 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Get log page: LID=0x%02X offset=0x%" PRIx64 " len=0x%" PRIx64 "\n",
1130 		      lid, offset, len);
1131 
1132 	if (subsystem->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) {
1133 		switch (lid) {
1134 		case SPDK_NVME_LOG_DISCOVERY:
1135 			spdk_nvmf_get_discovery_log_page(subsystem->tgt, req->data, offset, len);
1136 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1137 		default:
1138 			goto invalid_log_page;
1139 		}
1140 	} else {
1141 		switch (lid) {
1142 		case SPDK_NVME_LOG_ERROR:
1143 		case SPDK_NVME_LOG_HEALTH_INFORMATION:
1144 			/* TODO: actually fill out log page data */
1145 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1146 		case SPDK_NVME_LOG_FIRMWARE_SLOT:
1147 			spdk_nvmf_get_firmware_slot_log_page(req->data, offset, len);
1148 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1149 		case SPDK_NVME_LOG_COMMAND_EFFECTS_LOG:
1150 			spdk_nvmf_get_cmds_and_effects_log_page(req->data, offset, len);
1151 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1152 		case SPDK_NVME_LOG_CHANGED_NS_LIST:
1153 			spdk_nvmf_get_changed_ns_list_log_page(ctrlr, req->data, offset, len);
1154 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1155 		default:
1156 			goto invalid_log_page;
1157 		}
1158 	}
1159 
1160 invalid_log_page:
1161 	SPDK_ERRLOG("Unsupported Get Log Page 0x%02X\n", lid);
1162 	response->status.sct = SPDK_NVME_SCT_GENERIC;
1163 	response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1164 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1165 }
1166 
1167 static int
1168 spdk_nvmf_ctrlr_identify_ns(struct spdk_nvmf_subsystem *subsystem,
1169 			    struct spdk_nvme_cmd *cmd,
1170 			    struct spdk_nvme_cpl *rsp,
1171 			    struct spdk_nvme_ns_data *nsdata)
1172 {
1173 	struct spdk_nvmf_ns *ns;
1174 
1175 	ns = _spdk_nvmf_subsystem_get_ns(subsystem, cmd->nsid);
1176 	if (ns == NULL || ns->bdev == NULL) {
1177 		SPDK_ERRLOG("Identify Namespace for invalid NSID %u\n", cmd->nsid);
1178 		rsp->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT;
1179 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1180 	}
1181 
1182 	return spdk_nvmf_bdev_ctrlr_identify_ns(ns, nsdata);
1183 }
1184 
1185 static int
1186 spdk_nvmf_ctrlr_identify_ctrlr(struct spdk_nvmf_ctrlr *ctrlr, struct spdk_nvme_ctrlr_data *cdata)
1187 {
1188 	struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys;
1189 	struct spdk_nvmf_tgt *tgt = subsystem->tgt;
1190 
1191 	/*
1192 	 * Common fields for discovery and NVM subsystems
1193 	 */
1194 	spdk_strcpy_pad(cdata->fr, FW_VERSION, sizeof(cdata->fr), ' ');
1195 	assert((tgt->opts.max_io_size % 4096) == 0);
1196 	cdata->mdts = spdk_u32log2(tgt->opts.max_io_size / 4096);
1197 	cdata->cntlid = ctrlr->cntlid;
1198 	cdata->ver = ctrlr->vcprop.vs;
1199 	cdata->lpa.edlp = 1;
1200 	cdata->elpe = 127;
1201 	cdata->maxcmd = tgt->opts.max_queue_depth;
1202 	cdata->sgls.supported = 1;
1203 	cdata->sgls.keyed_sgl = 1;
1204 	cdata->sgls.sgl_offset = 1;
1205 	spdk_strcpy_pad(cdata->subnqn, subsystem->subnqn, sizeof(cdata->subnqn), '\0');
1206 
1207 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ctrlr data: maxcmd 0x%x\n", cdata->maxcmd);
1208 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "sgls data: 0x%x\n", from_le32(&cdata->sgls));
1209 
1210 	/*
1211 	 * NVM subsystem fields (reserved for discovery subsystems)
1212 	 */
1213 	if (subsystem->subtype == SPDK_NVMF_SUBTYPE_NVME) {
1214 		spdk_strcpy_pad(cdata->mn, MODEL_NUMBER, sizeof(cdata->mn), ' ');
1215 		spdk_strcpy_pad(cdata->sn, spdk_nvmf_subsystem_get_sn(subsystem), sizeof(cdata->sn), ' ');
1216 		cdata->kas = 10;
1217 
1218 		cdata->rab = 6;
1219 		cdata->cmic.multi_port = 1;
1220 		cdata->cmic.multi_host = 1;
1221 		cdata->oaes.ns_attribute_notices = 1;
1222 		cdata->ctratt.host_id_exhid_supported = 1;
1223 		cdata->aerl = 0;
1224 		cdata->frmw.slot1_ro = 1;
1225 		cdata->frmw.num_slots = 1;
1226 
1227 		cdata->lpa.celp = 1; /* Command Effects log page supported */
1228 
1229 		cdata->sqes.min = 6;
1230 		cdata->sqes.max = 6;
1231 		cdata->cqes.min = 4;
1232 		cdata->cqes.max = 4;
1233 		cdata->nn = subsystem->max_nsid;
1234 		cdata->vwc.present = 1;
1235 
1236 		cdata->nvmf_specific.ioccsz = sizeof(struct spdk_nvme_cmd) / 16;
1237 		cdata->nvmf_specific.iorcsz = sizeof(struct spdk_nvme_cpl) / 16;
1238 		cdata->nvmf_specific.icdoff = 0; /* offset starts directly after SQE */
1239 		cdata->nvmf_specific.ctrattr.ctrlr_model = SPDK_NVMF_CTRLR_MODEL_DYNAMIC;
1240 		cdata->nvmf_specific.msdbd = 1; /* target supports single SGL in capsule */
1241 
1242 		/* TODO: this should be set by the transport */
1243 		cdata->nvmf_specific.ioccsz += tgt->opts.in_capsule_data_size / 16;
1244 
1245 		cdata->oncs.dsm = spdk_nvmf_ctrlr_dsm_supported(ctrlr);
1246 		cdata->oncs.write_zeroes = spdk_nvmf_ctrlr_write_zeroes_supported(ctrlr);
1247 
1248 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ext ctrlr data: ioccsz 0x%x\n",
1249 			      cdata->nvmf_specific.ioccsz);
1250 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ext ctrlr data: iorcsz 0x%x\n",
1251 			      cdata->nvmf_specific.iorcsz);
1252 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ext ctrlr data: icdoff 0x%x\n",
1253 			      cdata->nvmf_specific.icdoff);
1254 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ext ctrlr data: ctrattr 0x%x\n",
1255 			      *(uint8_t *)&cdata->nvmf_specific.ctrattr);
1256 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ext ctrlr data: msdbd 0x%x\n",
1257 			      cdata->nvmf_specific.msdbd);
1258 	}
1259 
1260 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1261 }
1262 
1263 static int
1264 spdk_nvmf_ctrlr_identify_active_ns_list(struct spdk_nvmf_subsystem *subsystem,
1265 					struct spdk_nvme_cmd *cmd,
1266 					struct spdk_nvme_cpl *rsp,
1267 					struct spdk_nvme_ns_list *ns_list)
1268 {
1269 	struct spdk_nvmf_ns *ns;
1270 	uint32_t count = 0;
1271 
1272 	if (cmd->nsid >= 0xfffffffeUL) {
1273 		SPDK_ERRLOG("Identify Active Namespace List with invalid NSID %u\n", cmd->nsid);
1274 		rsp->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT;
1275 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1276 	}
1277 
1278 	for (ns = spdk_nvmf_subsystem_get_first_ns(subsystem); ns != NULL;
1279 	     ns = spdk_nvmf_subsystem_get_next_ns(subsystem, ns)) {
1280 		if (ns->opts.nsid <= cmd->nsid) {
1281 			continue;
1282 		}
1283 
1284 		ns_list->ns_list[count++] = ns->opts.nsid;
1285 		if (count == SPDK_COUNTOF(ns_list->ns_list)) {
1286 			break;
1287 		}
1288 	}
1289 
1290 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1291 }
1292 
1293 static void
1294 _add_ns_id_desc(void **buf_ptr, size_t *buf_remain,
1295 		enum spdk_nvme_nidt type,
1296 		const void *data, size_t data_size)
1297 {
1298 	struct spdk_nvme_ns_id_desc *desc;
1299 	size_t desc_size = sizeof(*desc) + data_size;
1300 
1301 	/*
1302 	 * These should never fail in practice, since all valid NS ID descriptors
1303 	 * should be defined so that they fit in the available 4096-byte buffer.
1304 	 */
1305 	assert(data_size > 0);
1306 	assert(data_size <= UINT8_MAX);
1307 	assert(desc_size < *buf_remain);
1308 	if (data_size == 0 || data_size > UINT8_MAX || desc_size > *buf_remain) {
1309 		return;
1310 	}
1311 
1312 	desc = *buf_ptr;
1313 	desc->nidt = type;
1314 	desc->nidl = data_size;
1315 	memcpy(desc->nid, data, data_size);
1316 
1317 	*buf_ptr += desc_size;
1318 	*buf_remain -= desc_size;
1319 }
1320 
1321 static int
1322 spdk_nvmf_ctrlr_identify_ns_id_descriptor_list(
1323 	struct spdk_nvmf_subsystem *subsystem,
1324 	struct spdk_nvme_cmd *cmd,
1325 	struct spdk_nvme_cpl *rsp,
1326 	void *id_desc_list, size_t id_desc_list_size)
1327 {
1328 	struct spdk_nvmf_ns *ns;
1329 	size_t buf_remain = id_desc_list_size;
1330 	void *buf_ptr = id_desc_list;
1331 
1332 	ns = _spdk_nvmf_subsystem_get_ns(subsystem, cmd->nsid);
1333 	if (ns == NULL || ns->bdev == NULL) {
1334 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
1335 		rsp->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT;
1336 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1337 	}
1338 
1339 #define ADD_ID_DESC(type, data, size) \
1340 	do { \
1341 		if (!spdk_mem_all_zero(data, size)) { \
1342 			_add_ns_id_desc(&buf_ptr, &buf_remain, type, data, size); \
1343 		} \
1344 	} while (0)
1345 
1346 	ADD_ID_DESC(SPDK_NVME_NIDT_EUI64, ns->opts.eui64, sizeof(ns->opts.eui64));
1347 	ADD_ID_DESC(SPDK_NVME_NIDT_NGUID, ns->opts.nguid, sizeof(ns->opts.nguid));
1348 	ADD_ID_DESC(SPDK_NVME_NIDT_UUID, &ns->opts.uuid, sizeof(ns->opts.uuid));
1349 
1350 	/*
1351 	 * The list is automatically 0-terminated because controller to host buffers in
1352 	 * admin commands always get zeroed in spdk_nvmf_ctrlr_process_admin_cmd().
1353 	 */
1354 
1355 #undef ADD_ID_DESC
1356 
1357 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1358 }
1359 
1360 static int
1361 spdk_nvmf_ctrlr_identify(struct spdk_nvmf_request *req)
1362 {
1363 	uint8_t cns;
1364 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1365 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1366 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
1367 	struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys;
1368 
1369 	if (req->data == NULL || req->length < 4096) {
1370 		SPDK_ERRLOG("identify command with invalid buffer\n");
1371 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
1372 		rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1373 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1374 	}
1375 
1376 	cns = cmd->cdw10 & 0xFF;
1377 
1378 	if (subsystem->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY &&
1379 	    cns != SPDK_NVME_IDENTIFY_CTRLR) {
1380 		/* Discovery controllers only support Identify Controller */
1381 		goto invalid_cns;
1382 	}
1383 
1384 	switch (cns) {
1385 	case SPDK_NVME_IDENTIFY_NS:
1386 		return spdk_nvmf_ctrlr_identify_ns(subsystem, cmd, rsp, req->data);
1387 	case SPDK_NVME_IDENTIFY_CTRLR:
1388 		return spdk_nvmf_ctrlr_identify_ctrlr(ctrlr, req->data);
1389 	case SPDK_NVME_IDENTIFY_ACTIVE_NS_LIST:
1390 		return spdk_nvmf_ctrlr_identify_active_ns_list(subsystem, cmd, rsp, req->data);
1391 	case SPDK_NVME_IDENTIFY_NS_ID_DESCRIPTOR_LIST:
1392 		return spdk_nvmf_ctrlr_identify_ns_id_descriptor_list(subsystem, cmd, rsp, req->data, req->length);
1393 	default:
1394 		goto invalid_cns;
1395 	}
1396 
1397 invalid_cns:
1398 	SPDK_ERRLOG("Identify command with unsupported CNS 0x%02x\n", cns);
1399 	rsp->status.sct = SPDK_NVME_SCT_GENERIC;
1400 	rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1401 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1402 }
1403 
1404 static int
1405 spdk_nvmf_ctrlr_abort(struct spdk_nvmf_request *req)
1406 {
1407 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1408 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
1409 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1410 	uint32_t cdw10 = cmd->cdw10;
1411 	uint16_t cid = cdw10 >> 16;
1412 	uint16_t sqid = cdw10 & 0xFFFFu;
1413 	struct spdk_nvmf_qpair *qpair;
1414 	struct spdk_nvmf_request *req_to_abort;
1415 
1416 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "abort sqid=%u cid=%u\n", sqid, cid);
1417 
1418 	rsp->cdw0 = 1; /* Command not aborted */
1419 
1420 	qpair = spdk_nvmf_ctrlr_get_qpair(ctrlr, sqid);
1421 	if (qpair == NULL) {
1422 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "sqid %u not found\n", sqid);
1423 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
1424 		rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1425 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1426 	}
1427 
1428 	/*
1429 	 * NOTE: This relies on the assumption that all connections for a ctrlr will be handled
1430 	 * on the same thread.  If this assumption becomes untrue, this will need to pass a message
1431 	 * to the thread handling qpair, and the abort will need to be asynchronous.
1432 	 */
1433 	req_to_abort = spdk_nvmf_qpair_get_request(qpair, cid);
1434 	if (req_to_abort == NULL) {
1435 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "cid %u not found\n", cid);
1436 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
1437 		rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1438 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1439 	}
1440 
1441 	if (spdk_nvmf_request_abort(req_to_abort) == 0) {
1442 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "abort ctrlr=%p req=%p sqid=%u cid=%u successful\n",
1443 			      ctrlr, req_to_abort, sqid, cid);
1444 		rsp->cdw0 = 0; /* Command successfully aborted */
1445 	}
1446 	rsp->status.sct = SPDK_NVME_SCT_GENERIC;
1447 	rsp->status.sc = SPDK_NVME_SC_SUCCESS;
1448 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1449 }
1450 
1451 static int
1452 get_features_generic(struct spdk_nvmf_request *req, uint32_t cdw0)
1453 {
1454 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
1455 
1456 	rsp->cdw0 = cdw0;
1457 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1458 }
1459 
1460 static int
1461 spdk_nvmf_ctrlr_get_features(struct spdk_nvmf_request *req)
1462 {
1463 	uint8_t feature;
1464 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1465 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1466 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
1467 
1468 	feature = cmd->cdw10 & 0xff; /* mask out the FID value */
1469 	switch (feature) {
1470 	case SPDK_NVME_FEAT_ARBITRATION:
1471 		return get_features_generic(req, ctrlr->feat.arbitration.raw);
1472 	case SPDK_NVME_FEAT_POWER_MANAGEMENT:
1473 		return get_features_generic(req, ctrlr->feat.power_management.raw);
1474 	case SPDK_NVME_FEAT_TEMPERATURE_THRESHOLD:
1475 		return spdk_nvmf_ctrlr_get_features_temperature_threshold(req);
1476 	case SPDK_NVME_FEAT_ERROR_RECOVERY:
1477 		return get_features_generic(req, ctrlr->feat.error_recovery.raw);
1478 	case SPDK_NVME_FEAT_VOLATILE_WRITE_CACHE:
1479 		return get_features_generic(req, ctrlr->feat.volatile_write_cache.raw);
1480 	case SPDK_NVME_FEAT_NUMBER_OF_QUEUES:
1481 		return get_features_generic(req, ctrlr->feat.number_of_queues.raw);
1482 	case SPDK_NVME_FEAT_WRITE_ATOMICITY:
1483 		return get_features_generic(req, ctrlr->feat.write_atomicity.raw);
1484 	case SPDK_NVME_FEAT_ASYNC_EVENT_CONFIGURATION:
1485 		return get_features_generic(req, ctrlr->feat.async_event_configuration.raw);
1486 	case SPDK_NVME_FEAT_KEEP_ALIVE_TIMER:
1487 		return get_features_generic(req, ctrlr->feat.keep_alive_timer.raw);
1488 	case SPDK_NVME_FEAT_HOST_IDENTIFIER:
1489 		return spdk_nvmf_ctrlr_get_features_host_identifier(req);
1490 	default:
1491 		SPDK_ERRLOG("Get Features command with unsupported feature ID 0x%02x\n", feature);
1492 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1493 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1494 	}
1495 }
1496 
1497 static int
1498 spdk_nvmf_ctrlr_set_features(struct spdk_nvmf_request *req)
1499 {
1500 	uint8_t feature;
1501 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1502 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
1503 
1504 	feature = cmd->cdw10 & 0xff; /* mask out the FID value */
1505 	switch (feature) {
1506 	case SPDK_NVME_FEAT_ARBITRATION:
1507 		return spdk_nvmf_ctrlr_set_features_arbitration(req);
1508 	case SPDK_NVME_FEAT_POWER_MANAGEMENT:
1509 		return spdk_nvmf_ctrlr_set_features_power_management(req);
1510 	case SPDK_NVME_FEAT_TEMPERATURE_THRESHOLD:
1511 		return spdk_nvmf_ctrlr_set_features_temperature_threshold(req);
1512 	case SPDK_NVME_FEAT_ERROR_RECOVERY:
1513 		return spdk_nvmf_ctrlr_set_features_error_recovery(req);
1514 	case SPDK_NVME_FEAT_VOLATILE_WRITE_CACHE:
1515 		return spdk_nvmf_ctrlr_set_features_volatile_write_cache(req);
1516 	case SPDK_NVME_FEAT_NUMBER_OF_QUEUES:
1517 		return spdk_nvmf_ctrlr_set_features_number_of_queues(req);
1518 	case SPDK_NVME_FEAT_WRITE_ATOMICITY:
1519 		return spdk_nvmf_ctrlr_set_features_write_atomicity(req);
1520 	case SPDK_NVME_FEAT_ASYNC_EVENT_CONFIGURATION:
1521 		return spdk_nvmf_ctrlr_set_features_async_event_configuration(req);
1522 	case SPDK_NVME_FEAT_KEEP_ALIVE_TIMER:
1523 		return spdk_nvmf_ctrlr_set_features_keep_alive_timer(req);
1524 	case SPDK_NVME_FEAT_HOST_IDENTIFIER:
1525 		return spdk_nvmf_ctrlr_set_features_host_identifier(req);
1526 	default:
1527 		SPDK_ERRLOG("Set Features command with unsupported feature ID 0x%02x\n", feature);
1528 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1529 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1530 	}
1531 }
1532 
1533 static int
1534 spdk_nvmf_ctrlr_keep_alive(struct spdk_nvmf_request *req)
1535 {
1536 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Keep Alive\n");
1537 	/*
1538 	 * To handle keep alive just clear or reset the
1539 	 * ctrlr based keep alive duration counter.
1540 	 * When added, a separate timer based process
1541 	 * will monitor if the time since last recorded
1542 	 * keep alive has exceeded the max duration and
1543 	 * take appropriate action.
1544 	 */
1545 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1546 }
1547 
1548 int
1549 spdk_nvmf_ctrlr_process_admin_cmd(struct spdk_nvmf_request *req)
1550 {
1551 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1552 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1553 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
1554 
1555 	if (ctrlr == NULL) {
1556 		SPDK_ERRLOG("Admin command sent before CONNECT\n");
1557 		response->status.sct = SPDK_NVME_SCT_GENERIC;
1558 		response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
1559 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1560 	}
1561 
1562 	if (ctrlr->vcprop.cc.bits.en != 1) {
1563 		SPDK_ERRLOG("Admin command sent to disabled controller\n");
1564 		response->status.sct = SPDK_NVME_SCT_GENERIC;
1565 		response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
1566 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1567 	}
1568 
1569 	if (req->data && spdk_nvme_opc_get_data_transfer(cmd->opc) == SPDK_NVME_DATA_CONTROLLER_TO_HOST) {
1570 		memset(req->data, 0, req->length);
1571 	}
1572 
1573 	if (ctrlr->subsys->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) {
1574 		/* Discovery controllers only support Get Log Page and Identify */
1575 		switch (cmd->opc) {
1576 		case SPDK_NVME_OPC_IDENTIFY:
1577 		case SPDK_NVME_OPC_GET_LOG_PAGE:
1578 			break;
1579 		default:
1580 			goto invalid_opcode;
1581 		}
1582 	}
1583 
1584 	switch (cmd->opc) {
1585 	case SPDK_NVME_OPC_GET_LOG_PAGE:
1586 		return spdk_nvmf_ctrlr_get_log_page(req);
1587 	case SPDK_NVME_OPC_IDENTIFY:
1588 		return spdk_nvmf_ctrlr_identify(req);
1589 	case SPDK_NVME_OPC_ABORT:
1590 		return spdk_nvmf_ctrlr_abort(req);
1591 	case SPDK_NVME_OPC_GET_FEATURES:
1592 		return spdk_nvmf_ctrlr_get_features(req);
1593 	case SPDK_NVME_OPC_SET_FEATURES:
1594 		return spdk_nvmf_ctrlr_set_features(req);
1595 	case SPDK_NVME_OPC_ASYNC_EVENT_REQUEST:
1596 		return spdk_nvmf_ctrlr_async_event_request(req);
1597 	case SPDK_NVME_OPC_KEEP_ALIVE:
1598 		return spdk_nvmf_ctrlr_keep_alive(req);
1599 
1600 	case SPDK_NVME_OPC_CREATE_IO_SQ:
1601 	case SPDK_NVME_OPC_CREATE_IO_CQ:
1602 	case SPDK_NVME_OPC_DELETE_IO_SQ:
1603 	case SPDK_NVME_OPC_DELETE_IO_CQ:
1604 		/* Create and Delete I/O CQ/SQ not allowed in NVMe-oF */
1605 		goto invalid_opcode;
1606 
1607 	default:
1608 		goto invalid_opcode;
1609 	}
1610 
1611 invalid_opcode:
1612 	SPDK_ERRLOG("Unsupported admin opcode 0x%x\n", cmd->opc);
1613 	response->status.sct = SPDK_NVME_SCT_GENERIC;
1614 	response->status.sc = SPDK_NVME_SC_INVALID_OPCODE;
1615 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1616 }
1617 
1618 int
1619 spdk_nvmf_ctrlr_process_fabrics_cmd(struct spdk_nvmf_request *req)
1620 {
1621 	struct spdk_nvmf_qpair *qpair = req->qpair;
1622 	struct spdk_nvmf_capsule_cmd *cap_hdr;
1623 
1624 	cap_hdr = &req->cmd->nvmf_cmd;
1625 
1626 	if (qpair->ctrlr == NULL) {
1627 		/* No ctrlr established yet; the only valid command is Connect */
1628 		if (cap_hdr->fctype == SPDK_NVMF_FABRIC_COMMAND_CONNECT) {
1629 			return spdk_nvmf_ctrlr_connect(req);
1630 		} else {
1631 			SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Got fctype 0x%x, expected Connect\n",
1632 				      cap_hdr->fctype);
1633 			req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
1634 			req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
1635 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1636 		}
1637 	} else if (spdk_nvmf_qpair_is_admin_queue(qpair)) {
1638 		/*
1639 		 * Controller session is established, and this is an admin queue.
1640 		 * Disallow Connect and allow other fabrics commands.
1641 		 */
1642 		switch (cap_hdr->fctype) {
1643 		case SPDK_NVMF_FABRIC_COMMAND_PROPERTY_SET:
1644 			return spdk_nvmf_property_set(req);
1645 		case SPDK_NVMF_FABRIC_COMMAND_PROPERTY_GET:
1646 			return spdk_nvmf_property_get(req);
1647 		default:
1648 			SPDK_DEBUGLOG(SPDK_LOG_NVMF, "unknown fctype 0x%02x\n",
1649 				      cap_hdr->fctype);
1650 			req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
1651 			req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_INVALID_OPCODE;
1652 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1653 		}
1654 	} else {
1655 		/* Controller session is established, and this is an I/O queue */
1656 		/* For now, no I/O-specific Fabrics commands are implemented (other than Connect) */
1657 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Unexpected I/O fctype 0x%x\n", cap_hdr->fctype);
1658 		req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
1659 		req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_INVALID_OPCODE;
1660 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1661 	}
1662 }
1663 
1664 int
1665 spdk_nvmf_ctrlr_async_event_ns_notice(struct spdk_nvmf_ctrlr *ctrlr)
1666 {
1667 	struct spdk_nvmf_request *req;
1668 	struct spdk_nvme_cpl *rsp;
1669 	union spdk_nvme_async_event_completion event = {0};
1670 
1671 	/* Users may disable the event notification */
1672 	if (!ctrlr->feat.async_event_configuration.bits.ns_attr_notice) {
1673 		return 0;
1674 	}
1675 
1676 	event.bits.async_event_type = SPDK_NVME_ASYNC_EVENT_TYPE_NOTICE;
1677 	event.bits.async_event_info = SPDK_NVME_ASYNC_EVENT_NS_ATTR_CHANGED;
1678 	event.bits.log_page_identifier = SPDK_NVME_LOG_CHANGED_NS_LIST;
1679 
1680 	/* If there is no outstanding AER request, queue the event.  Then
1681 	 * if an AER is later submitted, this event can be sent as a
1682 	 * response.
1683 	 */
1684 	if (!ctrlr->aer_req) {
1685 		if (ctrlr->notice_event.bits.async_event_type ==
1686 		    SPDK_NVME_ASYNC_EVENT_TYPE_NOTICE) {
1687 			return 0;
1688 		}
1689 
1690 		ctrlr->notice_event.raw = event.raw;
1691 		return 0;
1692 	}
1693 
1694 	req = ctrlr->aer_req;
1695 	rsp = &req->rsp->nvme_cpl;
1696 
1697 	rsp->cdw0 = event.raw;
1698 
1699 	spdk_nvmf_request_complete(req);
1700 	ctrlr->aer_req = NULL;
1701 
1702 	return 0;
1703 }
1704