xref: /spdk/lib/nvmf/ctrlr.c (revision 97cb1713ffd23594904e38b2636aa491b7d9ab51)
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 struct spdk_nvmf_ctrlr *
60 spdk_nvmf_ctrlr_create(struct spdk_nvmf_subsystem *subsystem,
61 		       struct spdk_nvmf_qpair *admin_qpair,
62 		       struct spdk_nvmf_fabric_connect_cmd *connect_cmd,
63 		       struct spdk_nvmf_fabric_connect_data *connect_data)
64 {
65 	struct spdk_nvmf_ctrlr	*ctrlr;
66 	struct spdk_nvmf_tgt	*tgt;
67 
68 	tgt = subsystem->tgt;
69 
70 	ctrlr = calloc(1, sizeof(*ctrlr));
71 	if (ctrlr == NULL) {
72 		SPDK_ERRLOG("Memory allocation failed\n");
73 		return NULL;
74 	}
75 
76 	TAILQ_INIT(&ctrlr->qpairs);
77 	ctrlr->kato = connect_cmd->kato;
78 	ctrlr->async_event_config.raw = 0;
79 	ctrlr->num_qpairs = 0;
80 	ctrlr->subsys = subsystem;
81 	ctrlr->max_qpairs_allowed = tgt->opts.max_qpairs_per_ctrlr;
82 
83 	memcpy(ctrlr->hostid, connect_data->hostid, sizeof(ctrlr->hostid));
84 
85 	ctrlr->vcprop.cap.raw = 0;
86 	ctrlr->vcprop.cap.bits.cqr = 1; /* NVMe-oF specification required */
87 	ctrlr->vcprop.cap.bits.mqes = tgt->opts.max_queue_depth - 1; /* max queue depth */
88 	ctrlr->vcprop.cap.bits.ams = 0; /* optional arb mechanisms */
89 	ctrlr->vcprop.cap.bits.to = 1; /* ready timeout - 500 msec units */
90 	ctrlr->vcprop.cap.bits.dstrd = 0; /* fixed to 0 for NVMe-oF */
91 	ctrlr->vcprop.cap.bits.css_nvm = 1; /* NVM command set */
92 	ctrlr->vcprop.cap.bits.mpsmin = 0; /* 2 ^ (12 + mpsmin) == 4k */
93 	ctrlr->vcprop.cap.bits.mpsmax = 0; /* 2 ^ (12 + mpsmax) == 4k */
94 
95 	/* Version Supported: 1.2.1 */
96 	ctrlr->vcprop.vs.bits.mjr = 1;
97 	ctrlr->vcprop.vs.bits.mnr = 2;
98 	ctrlr->vcprop.vs.bits.ter = 1;
99 
100 	ctrlr->vcprop.cc.raw = 0;
101 	ctrlr->vcprop.cc.bits.en = 0; /* Init controller disabled */
102 
103 	ctrlr->vcprop.csts.raw = 0;
104 	ctrlr->vcprop.csts.bits.rdy = 0; /* Init controller as not ready */
105 
106 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "cap 0x%" PRIx64 "\n", ctrlr->vcprop.cap.raw);
107 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "vs 0x%x\n", ctrlr->vcprop.vs.raw);
108 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "cc 0x%x\n", ctrlr->vcprop.cc.raw);
109 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "csts 0x%x\n", ctrlr->vcprop.csts.raw);
110 
111 	if (spdk_nvmf_subsystem_add_ctrlr(subsystem, ctrlr)) {
112 		SPDK_ERRLOG("Unable to add controller to subsystem\n");
113 		free(ctrlr);
114 		return NULL;
115 	}
116 
117 	return ctrlr;
118 }
119 
120 static void ctrlr_destruct(struct spdk_nvmf_ctrlr *ctrlr)
121 {
122 	spdk_nvmf_subsystem_remove_ctrlr(ctrlr->subsys, ctrlr);
123 	free(ctrlr);
124 }
125 
126 void
127 spdk_nvmf_ctrlr_destruct(struct spdk_nvmf_ctrlr *ctrlr)
128 {
129 	while (!TAILQ_EMPTY(&ctrlr->qpairs)) {
130 		struct spdk_nvmf_qpair *qpair = TAILQ_FIRST(&ctrlr->qpairs);
131 
132 		TAILQ_REMOVE(&ctrlr->qpairs, qpair, link);
133 		ctrlr->num_qpairs--;
134 		spdk_nvmf_transport_qpair_fini(qpair);
135 	}
136 
137 	ctrlr_destruct(ctrlr);
138 }
139 
140 static inline void
141 spdk_nvmf_invalid_connect_response(struct spdk_nvmf_fabric_connect_rsp *rsp,
142 				   uint8_t iattr, uint16_t ipo)
143 {
144 	rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
145 	rsp->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM;
146 	rsp->status_code_specific.invalid.iattr = iattr;
147 	rsp->status_code_specific.invalid.ipo = ipo;
148 }
149 
150 #define SPDK_NVMF_INVALID_CONNECT_CMD(rsp, field)	\
151 	spdk_nvmf_invalid_connect_response(rsp, 0, offsetof(struct spdk_nvmf_fabric_connect_cmd, field))
152 #define SPDK_NVMF_INVALID_CONNECT_DATA(rsp, field)	\
153 	spdk_nvmf_invalid_connect_response(rsp, 1, offsetof(struct spdk_nvmf_fabric_connect_data, field))
154 
155 static void
156 ctrlr_add_qpair_and_update_rsp(struct spdk_nvmf_qpair *qpair,
157 			       struct spdk_nvmf_ctrlr *ctrlr,
158 			       struct spdk_nvmf_fabric_connect_rsp *rsp)
159 {
160 	qpair->ctrlr = ctrlr;
161 	ctrlr->num_qpairs++;
162 	TAILQ_INSERT_HEAD(&ctrlr->qpairs, qpair, link);
163 
164 	rsp->status.sc = SPDK_NVME_SC_SUCCESS;
165 	rsp->status_code_specific.success.cntlid = ctrlr->cntlid;
166 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "connect capsule response: cntlid = 0x%04x\n",
167 		      rsp->status_code_specific.success.cntlid);
168 }
169 
170 static void
171 _spdk_nvmf_request_complete(void *ctx)
172 {
173 	struct spdk_nvmf_request *req = ctx;
174 
175 	spdk_nvmf_request_complete(req);
176 }
177 
178 static void
179 spdk_nvmf_ctrlr_add_io_qpair(void *ctx)
180 {
181 	struct spdk_nvmf_request *req = ctx;
182 	struct spdk_nvmf_fabric_connect_cmd *cmd = &req->cmd->connect_cmd;
183 	struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp;
184 	struct spdk_nvmf_qpair *qpair = req->qpair;
185 	struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr;
186 
187 	/* Unit test will check qpair->ctrlr after calling spdk_nvmf_ctrlr_connect.
188 	  * For error case, the value should be NULL. So set it to NULL at first.
189 	  */
190 	qpair->ctrlr = NULL;
191 
192 	if (ctrlr->subsys->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) {
193 		SPDK_ERRLOG("I/O connect not allowed on discovery controller\n");
194 		SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid);
195 		goto end;
196 	}
197 
198 	if (!ctrlr->vcprop.cc.bits.en) {
199 		SPDK_ERRLOG("Got I/O connect before ctrlr was enabled\n");
200 		SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid);
201 		goto end;
202 	}
203 
204 	if (1u << ctrlr->vcprop.cc.bits.iosqes != sizeof(struct spdk_nvme_cmd)) {
205 		SPDK_ERRLOG("Got I/O connect with invalid IOSQES %u\n",
206 			    ctrlr->vcprop.cc.bits.iosqes);
207 		SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid);
208 		goto end;
209 	}
210 
211 	if (1u << ctrlr->vcprop.cc.bits.iocqes != sizeof(struct spdk_nvme_cpl)) {
212 		SPDK_ERRLOG("Got I/O connect with invalid IOCQES %u\n",
213 			    ctrlr->vcprop.cc.bits.iocqes);
214 		SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid);
215 		goto end;
216 	}
217 
218 	if (spdk_nvmf_ctrlr_get_qpair(ctrlr, cmd->qid)) {
219 		SPDK_ERRLOG("Got I/O connect with duplicate QID %u\n", cmd->qid);
220 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
221 		rsp->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
222 		goto end;
223 	}
224 
225 	/* check if we would exceed ctrlr connection limit */
226 	if (ctrlr->num_qpairs >= ctrlr->max_qpairs_allowed) {
227 		SPDK_ERRLOG("qpair limit %d\n", ctrlr->num_qpairs);
228 		rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
229 		rsp->status.sc = SPDK_NVMF_FABRIC_SC_CONTROLLER_BUSY;
230 		goto end;
231 	}
232 
233 	ctrlr_add_qpair_and_update_rsp(qpair, ctrlr, rsp);
234 
235 end:
236 	spdk_thread_send_msg(qpair->group->thread, _spdk_nvmf_request_complete, req);
237 }
238 
239 static void
240 ctrlr_delete_qpair(void *ctx)
241 {
242 	struct spdk_nvmf_qpair *qpair = ctx;
243 	struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr;
244 
245 	assert(ctrlr != NULL);
246 
247 	ctrlr->num_qpairs--;
248 	TAILQ_REMOVE(&ctrlr->qpairs, qpair, link);
249 	spdk_nvmf_transport_qpair_fini(qpair);
250 
251 	if (ctrlr->num_qpairs == 0) {
252 		ctrlr_destruct(ctrlr);
253 	}
254 }
255 
256 static int
257 spdk_nvmf_ctrlr_connect(struct spdk_nvmf_request *req)
258 {
259 	struct spdk_nvmf_fabric_connect_data *data = req->data;
260 	struct spdk_nvmf_fabric_connect_cmd *cmd = &req->cmd->connect_cmd;
261 	struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp;
262 	struct spdk_nvmf_qpair *qpair = req->qpair;
263 	struct spdk_nvmf_tgt *tgt = qpair->transport->tgt;
264 	struct spdk_nvmf_ctrlr *ctrlr;
265 	struct spdk_nvmf_qpair *admin_qpair = NULL;
266 	struct spdk_nvmf_subsystem *subsystem;
267 	const char *subnqn, *hostnqn;
268 	void *end;
269 
270 	if (req->length < sizeof(struct spdk_nvmf_fabric_connect_data)) {
271 		SPDK_ERRLOG("Connect command data length 0x%x too small\n", req->length);
272 		rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
273 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
274 	}
275 
276 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "recfmt 0x%x qid %u sqsize %u\n",
277 		      cmd->recfmt, cmd->qid, cmd->sqsize);
278 
279 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Connect data:\n");
280 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "  cntlid:  0x%04x\n", data->cntlid);
281 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "  hostid: %08x-%04x-%04x-%02x%02x-%04x%08x ***\n",
282 		      ntohl(*(uint32_t *)&data->hostid[0]),
283 		      ntohs(*(uint16_t *)&data->hostid[4]),
284 		      ntohs(*(uint16_t *)&data->hostid[6]),
285 		      data->hostid[8],
286 		      data->hostid[9],
287 		      ntohs(*(uint16_t *)&data->hostid[10]),
288 		      ntohl(*(uint32_t *)&data->hostid[12]));
289 
290 	if (cmd->recfmt != 0) {
291 		SPDK_ERRLOG("Connect command unsupported RECFMT %u\n", cmd->recfmt);
292 		rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
293 		rsp->status.sc = SPDK_NVMF_FABRIC_SC_INCOMPATIBLE_FORMAT;
294 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
295 	}
296 
297 	/* Ensure that subnqn is null terminated */
298 	end = memchr(data->subnqn, '\0', SPDK_NVMF_NQN_MAX_LEN + 1);
299 	if (!end) {
300 		SPDK_ERRLOG("Connect SUBNQN is not null terminated\n");
301 		SPDK_NVMF_INVALID_CONNECT_DATA(rsp, subnqn);
302 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
303 	}
304 	subnqn = data->subnqn;
305 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "  subnqn: \"%s\"\n", subnqn);
306 
307 	subsystem = spdk_nvmf_tgt_find_subsystem(tgt, subnqn);
308 	if (subsystem == NULL) {
309 		SPDK_ERRLOG("Could not find subsystem '%s'\n", subnqn);
310 		SPDK_NVMF_INVALID_CONNECT_DATA(rsp, subnqn);
311 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
312 	}
313 
314 	/* Ensure that hostnqn is null terminated */
315 	end = memchr(data->hostnqn, '\0', SPDK_NVMF_NQN_MAX_LEN + 1);
316 	if (!end) {
317 		SPDK_ERRLOG("Connect HOSTNQN is not null terminated\n");
318 		SPDK_NVMF_INVALID_CONNECT_DATA(rsp, hostnqn);
319 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
320 	}
321 	hostnqn = data->hostnqn;
322 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "  hostnqn: \"%s\"\n", hostnqn);
323 
324 	if (!spdk_nvmf_subsystem_host_allowed(subsystem, hostnqn)) {
325 		SPDK_ERRLOG("Subsystem '%s' does not allow host '%s'\n", subnqn, hostnqn);
326 		rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
327 		rsp->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_HOST;
328 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
329 	}
330 
331 	/*
332 	 * SQSIZE is a 0-based value, so it must be at least 1 (minimum queue depth is 2) and
333 	 *  strictly less than max_queue_depth.
334 	 */
335 	if (cmd->sqsize == 0 || cmd->sqsize >= tgt->opts.max_queue_depth) {
336 		SPDK_ERRLOG("Invalid SQSIZE %u (min 1, max %u)\n",
337 			    cmd->sqsize, tgt->opts.max_queue_depth - 1);
338 		SPDK_NVMF_INVALID_CONNECT_CMD(rsp, sqsize);
339 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
340 	}
341 	qpair->sq_head_max = cmd->sqsize;
342 	qpair->qid = cmd->qid;
343 
344 	if (cmd->qid == 0) {
345 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Connect Admin Queue for controller ID 0x%x\n", data->cntlid);
346 
347 		if (data->cntlid != 0xFFFF) {
348 			/* This NVMf target only supports dynamic mode. */
349 			SPDK_ERRLOG("The NVMf target only supports dynamic mode (CNTLID = 0x%x).\n", data->cntlid);
350 			SPDK_NVMF_INVALID_CONNECT_DATA(rsp, cntlid);
351 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
352 		}
353 
354 		/* Establish a new ctrlr */
355 		ctrlr = spdk_nvmf_ctrlr_create(subsystem, qpair, cmd, data);
356 		if (!ctrlr) {
357 			SPDK_ERRLOG("spdk_nvmf_ctrlr_create() failed\n");
358 			rsp->status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
359 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
360 		}
361 
362 		ctrlr->admin_qpair = qpair;
363 		ctrlr_add_qpair_and_update_rsp(qpair, ctrlr, rsp);
364 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
365 	} else {
366 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Connect I/O Queue for controller id 0x%x\n", data->cntlid);
367 
368 		ctrlr = spdk_nvmf_subsystem_get_ctrlr(subsystem, data->cntlid);
369 		if (ctrlr == NULL) {
370 			SPDK_ERRLOG("Unknown controller ID 0x%x\n", data->cntlid);
371 			SPDK_NVMF_INVALID_CONNECT_DATA(rsp, cntlid);
372 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
373 		}
374 
375 		admin_qpair = ctrlr->admin_qpair;
376 		qpair->ctrlr = ctrlr;
377 		spdk_thread_send_msg(admin_qpair->group->thread, spdk_nvmf_ctrlr_add_io_qpair, req);
378 		return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
379 	}
380 }
381 
382 void
383 spdk_nvmf_ctrlr_disconnect(struct spdk_nvmf_qpair *qpair)
384 {
385 	struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr;
386 	struct spdk_nvmf_qpair *admin_qpair = ctrlr->admin_qpair;
387 
388 	spdk_thread_send_msg(admin_qpair->group->thread, ctrlr_delete_qpair, qpair);
389 }
390 
391 struct spdk_nvmf_qpair *
392 spdk_nvmf_ctrlr_get_qpair(struct spdk_nvmf_ctrlr *ctrlr, uint16_t qid)
393 {
394 	struct spdk_nvmf_qpair *qpair;
395 
396 	TAILQ_FOREACH(qpair, &ctrlr->qpairs, link) {
397 		if (qpair->qid == qid) {
398 			return qpair;
399 		}
400 	}
401 	return NULL;
402 }
403 
404 static struct spdk_nvmf_request *
405 spdk_nvmf_qpair_get_request(struct spdk_nvmf_qpair *qpair, uint16_t cid)
406 {
407 	/* TODO: track list of outstanding requests in qpair? */
408 	return NULL;
409 }
410 
411 static uint64_t
412 nvmf_prop_get_cap(struct spdk_nvmf_ctrlr *ctrlr)
413 {
414 	return ctrlr->vcprop.cap.raw;
415 }
416 
417 static uint64_t
418 nvmf_prop_get_vs(struct spdk_nvmf_ctrlr *ctrlr)
419 {
420 	return ctrlr->vcprop.vs.raw;
421 }
422 
423 static uint64_t
424 nvmf_prop_get_cc(struct spdk_nvmf_ctrlr *ctrlr)
425 {
426 	return ctrlr->vcprop.cc.raw;
427 }
428 
429 static bool
430 nvmf_prop_set_cc(struct spdk_nvmf_ctrlr *ctrlr, uint64_t value)
431 {
432 	union spdk_nvme_cc_register cc, diff;
433 
434 	cc.raw = (uint32_t)value;
435 
436 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "cur CC: 0x%08x\n", ctrlr->vcprop.cc.raw);
437 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "new CC: 0x%08x\n", cc.raw);
438 
439 	/*
440 	 * Calculate which bits changed between the current and new CC.
441 	 * Mark each bit as 0 once it is handled to determine if any unhandled bits were changed.
442 	 */
443 	diff.raw = cc.raw ^ ctrlr->vcprop.cc.raw;
444 
445 	if (diff.bits.en) {
446 		if (cc.bits.en) {
447 			SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Property Set CC Enable!\n");
448 			ctrlr->vcprop.cc.bits.en = 1;
449 			ctrlr->vcprop.csts.bits.rdy = 1;
450 		} else {
451 			SPDK_ERRLOG("CC.EN transition from 1 to 0 (reset) not implemented!\n");
452 
453 		}
454 		diff.bits.en = 0;
455 	}
456 
457 	if (diff.bits.shn) {
458 		if (cc.bits.shn == SPDK_NVME_SHN_NORMAL ||
459 		    cc.bits.shn == SPDK_NVME_SHN_ABRUPT) {
460 			SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Property Set CC Shutdown %u%ub!\n",
461 				      cc.bits.shn >> 1, cc.bits.shn & 1);
462 			ctrlr->vcprop.cc.bits.shn = cc.bits.shn;
463 			ctrlr->vcprop.cc.bits.en = 0;
464 			ctrlr->vcprop.csts.bits.rdy = 0;
465 			ctrlr->vcprop.csts.bits.shst = SPDK_NVME_SHST_COMPLETE;
466 		} else if (cc.bits.shn == 0) {
467 			ctrlr->vcprop.cc.bits.shn = 0;
468 		} else {
469 			SPDK_ERRLOG("Prop Set CC: Invalid SHN value %u%ub\n",
470 				    cc.bits.shn >> 1, cc.bits.shn & 1);
471 			return false;
472 		}
473 		diff.bits.shn = 0;
474 	}
475 
476 	if (diff.bits.iosqes) {
477 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Prop Set IOSQES = %u (%u bytes)\n",
478 			      cc.bits.iosqes, 1u << cc.bits.iosqes);
479 		ctrlr->vcprop.cc.bits.iosqes = cc.bits.iosqes;
480 		diff.bits.iosqes = 0;
481 	}
482 
483 	if (diff.bits.iocqes) {
484 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Prop Set IOCQES = %u (%u bytes)\n",
485 			      cc.bits.iocqes, 1u << cc.bits.iocqes);
486 		ctrlr->vcprop.cc.bits.iocqes = cc.bits.iocqes;
487 		diff.bits.iocqes = 0;
488 	}
489 
490 	if (diff.raw != 0) {
491 		SPDK_ERRLOG("Prop Set CC toggled reserved bits 0x%x!\n", diff.raw);
492 		return false;
493 	}
494 
495 	return true;
496 }
497 
498 static uint64_t
499 nvmf_prop_get_csts(struct spdk_nvmf_ctrlr *ctrlr)
500 {
501 	return ctrlr->vcprop.csts.raw;
502 }
503 
504 struct nvmf_prop {
505 	uint32_t ofst;
506 	uint8_t size;
507 	char name[11];
508 	uint64_t (*get_cb)(struct spdk_nvmf_ctrlr *ctrlr);
509 	bool (*set_cb)(struct spdk_nvmf_ctrlr *ctrlr, uint64_t value);
510 };
511 
512 #define PROP(field, size, get_cb, set_cb) \
513 	{ \
514 		offsetof(struct spdk_nvme_registers, field), \
515 		SPDK_NVMF_PROP_SIZE_##size, \
516 		#field, \
517 		get_cb, set_cb \
518 	}
519 
520 static const struct nvmf_prop nvmf_props[] = {
521 	PROP(cap,  8, nvmf_prop_get_cap,  NULL),
522 	PROP(vs,   4, nvmf_prop_get_vs,   NULL),
523 	PROP(cc,   4, nvmf_prop_get_cc,   nvmf_prop_set_cc),
524 	PROP(csts, 4, nvmf_prop_get_csts, NULL),
525 };
526 
527 static const struct nvmf_prop *
528 find_prop(uint32_t ofst)
529 {
530 	size_t i;
531 
532 	for (i = 0; i < SPDK_COUNTOF(nvmf_props); i++) {
533 		const struct nvmf_prop *prop = &nvmf_props[i];
534 
535 		if (prop->ofst == ofst) {
536 			return prop;
537 		}
538 	}
539 
540 	return NULL;
541 }
542 
543 static int
544 spdk_nvmf_property_get(struct spdk_nvmf_request *req)
545 {
546 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
547 	struct spdk_nvmf_fabric_prop_get_cmd *cmd = &req->cmd->prop_get_cmd;
548 	struct spdk_nvmf_fabric_prop_get_rsp *response = &req->rsp->prop_get_rsp;
549 	const struct nvmf_prop *prop;
550 
551 	response->status.sc = 0;
552 	response->value.u64 = 0;
553 
554 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "size %d, offset 0x%x\n",
555 		      cmd->attrib.size, cmd->ofst);
556 
557 	if (cmd->attrib.size != SPDK_NVMF_PROP_SIZE_4 &&
558 	    cmd->attrib.size != SPDK_NVMF_PROP_SIZE_8) {
559 		SPDK_ERRLOG("Invalid size value %d\n", cmd->attrib.size);
560 		response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM;
561 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
562 	}
563 
564 	prop = find_prop(cmd->ofst);
565 	if (prop == NULL || prop->get_cb == NULL) {
566 		/* Reserved properties return 0 when read */
567 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
568 	}
569 
570 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "name: %s\n", prop->name);
571 	if (cmd->attrib.size != prop->size) {
572 		SPDK_ERRLOG("offset 0x%x size mismatch: cmd %u, prop %u\n",
573 			    cmd->ofst, cmd->attrib.size, prop->size);
574 		response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM;
575 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
576 	}
577 
578 	response->value.u64 = prop->get_cb(ctrlr);
579 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "response value: 0x%" PRIx64 "\n", response->value.u64);
580 
581 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
582 }
583 
584 static int
585 spdk_nvmf_property_set(struct spdk_nvmf_request *req)
586 {
587 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
588 	struct spdk_nvmf_fabric_prop_set_cmd *cmd = &req->cmd->prop_set_cmd;
589 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
590 	const struct nvmf_prop *prop;
591 	uint64_t value;
592 
593 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "size %d, offset 0x%x, value 0x%" PRIx64 "\n",
594 		      cmd->attrib.size, cmd->ofst, cmd->value.u64);
595 
596 	prop = find_prop(cmd->ofst);
597 	if (prop == NULL || prop->set_cb == NULL) {
598 		SPDK_ERRLOG("Invalid offset 0x%x\n", cmd->ofst);
599 		response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM;
600 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
601 	}
602 
603 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "name: %s\n", prop->name);
604 	if (cmd->attrib.size != prop->size) {
605 		SPDK_ERRLOG("offset 0x%x size mismatch: cmd %u, prop %u\n",
606 			    cmd->ofst, cmd->attrib.size, prop->size);
607 		response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM;
608 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
609 	}
610 
611 	value = cmd->value.u64;
612 	if (prop->size == SPDK_NVMF_PROP_SIZE_4) {
613 		value = (uint32_t)value;
614 	}
615 
616 	if (!prop->set_cb(ctrlr, value)) {
617 		SPDK_ERRLOG("prop set_cb failed\n");
618 		response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM;
619 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
620 	}
621 
622 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
623 }
624 
625 static int
626 spdk_nvmf_ctrlr_set_features_host_identifier(struct spdk_nvmf_request *req)
627 {
628 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
629 
630 	SPDK_ERRLOG("Set Features - Host Identifier not allowed\n");
631 	response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
632 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
633 }
634 
635 static int
636 spdk_nvmf_ctrlr_get_features_host_identifier(struct spdk_nvmf_request *req)
637 {
638 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
639 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
640 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
641 
642 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Get Features - Host Identifier\n");
643 	if (!(cmd->cdw11 & 1)) {
644 		/* NVMe over Fabrics requires EXHID=1 (128-bit/16-byte host ID) */
645 		SPDK_ERRLOG("Get Features - Host Identifier with EXHID=0 not allowed\n");
646 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
647 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
648 	}
649 
650 	if (req->data == NULL || req->length < sizeof(ctrlr->hostid)) {
651 		SPDK_ERRLOG("Invalid data buffer for Get Features - Host Identifier\n");
652 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
653 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
654 	}
655 
656 	memcpy(req->data, ctrlr->hostid, sizeof(ctrlr->hostid));
657 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
658 }
659 
660 static int
661 spdk_nvmf_ctrlr_set_features_keep_alive_timer(struct spdk_nvmf_request *req)
662 {
663 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
664 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
665 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
666 
667 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Keep Alive Timer (%u ms)\n", cmd->cdw11);
668 
669 	if (cmd->cdw11 == 0) {
670 		rsp->status.sc = SPDK_NVME_SC_KEEP_ALIVE_INVALID;
671 	} else if (cmd->cdw11 < MIN_KEEP_ALIVE_TIMEOUT) {
672 		ctrlr->kato = MIN_KEEP_ALIVE_TIMEOUT;
673 	} else {
674 		ctrlr->kato = cmd->cdw11;
675 	}
676 
677 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Keep Alive Timer set to %u ms\n", ctrlr->kato);
678 
679 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
680 }
681 
682 static int
683 spdk_nvmf_ctrlr_get_features_keep_alive_timer(struct spdk_nvmf_request *req)
684 {
685 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
686 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
687 
688 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Get Features - Keep Alive Timer\n");
689 	rsp->cdw0 = ctrlr->kato;
690 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
691 }
692 
693 static int
694 spdk_nvmf_ctrlr_set_features_number_of_queues(struct spdk_nvmf_request *req)
695 {
696 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
697 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
698 	uint32_t nr_io_queues;
699 
700 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Number of Queues, cdw11 0x%x\n",
701 		      req->cmd->nvme_cmd.cdw11);
702 
703 	/* Extra 1 connection for Admin queue */
704 	nr_io_queues = ctrlr->max_qpairs_allowed - 1;
705 
706 	/* verify that the contoller is ready to process commands */
707 	if (ctrlr->num_qpairs > 1) {
708 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Queue pairs already active!\n");
709 		rsp->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
710 	} else {
711 		/* Number of IO queues has a zero based value */
712 		rsp->cdw0 = ((nr_io_queues - 1) << 16) |
713 			    (nr_io_queues - 1);
714 	}
715 
716 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
717 }
718 
719 static int
720 spdk_nvmf_ctrlr_get_features_number_of_queues(struct spdk_nvmf_request *req)
721 {
722 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
723 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
724 	uint32_t nr_io_queues;
725 
726 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Get Features - Number of Queues\n");
727 
728 	nr_io_queues = ctrlr->max_qpairs_allowed - 1;
729 
730 	/* Number of IO queues has a zero based value */
731 	rsp->cdw0 = ((nr_io_queues - 1) << 16) |
732 		    (nr_io_queues - 1);
733 
734 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
735 }
736 
737 static int
738 spdk_nvmf_ctrlr_get_features_write_cache(struct spdk_nvmf_request *req)
739 {
740 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
741 
742 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Get Features - Write Cache\n");
743 	rsp->cdw0 = 1;
744 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
745 }
746 
747 static int
748 spdk_nvmf_ctrlr_set_features_async_event_configuration(struct spdk_nvmf_request *req)
749 {
750 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
751 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
752 
753 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Async Event Configuration, cdw11 0x%08x\n",
754 		      cmd->cdw11);
755 	ctrlr->async_event_config.raw = cmd->cdw11;
756 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
757 }
758 
759 static int
760 spdk_nvmf_ctrlr_get_features_async_event_configuration(struct spdk_nvmf_request *req)
761 {
762 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
763 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
764 
765 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Get Features - Async Event Configuration\n");
766 	rsp->cdw0 = ctrlr->async_event_config.raw;
767 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
768 }
769 
770 static int
771 spdk_nvmf_ctrlr_async_event_request(struct spdk_nvmf_request *req)
772 {
773 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
774 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
775 
776 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Async Event Request\n");
777 
778 	/* Only one asynchronous event is supported for now */
779 	if (ctrlr->aer_req != NULL) {
780 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "AERL exceeded\n");
781 		rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
782 		rsp->status.sc = SPDK_NVME_SC_ASYNC_EVENT_REQUEST_LIMIT_EXCEEDED;
783 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
784 	}
785 
786 	ctrlr->aer_req = req;
787 	return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
788 }
789 
790 static int
791 spdk_nvmf_ctrlr_get_log_page(struct spdk_nvmf_request *req)
792 {
793 	struct spdk_nvmf_subsystem *subsystem = req->qpair->ctrlr->subsys;
794 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
795 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
796 	uint64_t offset, len;
797 	uint32_t numdl, numdu;
798 	uint8_t lid;
799 
800 	if (req->data == NULL) {
801 		SPDK_ERRLOG("get log command with no buffer\n");
802 		response->status.sct = SPDK_NVME_SCT_GENERIC;
803 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
804 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
805 	}
806 
807 	offset = (uint64_t)cmd->cdw12 | ((uint64_t)cmd->cdw13 << 32);
808 	if (offset & 3) {
809 		SPDK_ERRLOG("Invalid log page offset 0x%" PRIx64 "\n", offset);
810 		response->status.sct = SPDK_NVME_SCT_GENERIC;
811 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
812 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
813 	}
814 
815 	numdl = (cmd->cdw10 >> 16) & 0xFFFFu;
816 	numdu = (cmd->cdw11) & 0xFFFFu;
817 	len = ((numdu << 16) + numdl + (uint64_t)1) * 4;
818 	if (len > req->length) {
819 		SPDK_ERRLOG("Get log page: len (%" PRIu64 ") > buf size (%u)\n",
820 			    len, req->length);
821 		response->status.sct = SPDK_NVME_SCT_GENERIC;
822 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
823 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
824 	}
825 
826 	lid = cmd->cdw10 & 0xFF;
827 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Get log page: LID=0x%02X offset=0x%" PRIx64 " len=0x%" PRIx64 "\n",
828 		      lid, offset, len);
829 
830 	if (subsystem->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) {
831 		switch (lid) {
832 		case SPDK_NVME_LOG_DISCOVERY:
833 			spdk_nvmf_get_discovery_log_page(subsystem->tgt, req->data, offset, len);
834 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
835 		default:
836 			goto invalid_log_page;
837 		}
838 	} else {
839 		switch (lid) {
840 		case SPDK_NVME_LOG_ERROR:
841 		case SPDK_NVME_LOG_HEALTH_INFORMATION:
842 		case SPDK_NVME_LOG_FIRMWARE_SLOT:
843 			/* TODO: actually fill out log page data */
844 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
845 		default:
846 			goto invalid_log_page;
847 		}
848 	}
849 
850 invalid_log_page:
851 	SPDK_ERRLOG("Unsupported Get Log Page 0x%02X\n", lid);
852 	response->status.sct = SPDK_NVME_SCT_GENERIC;
853 	response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
854 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
855 }
856 
857 static int
858 spdk_nvmf_ctrlr_identify_ns(struct spdk_nvmf_subsystem *subsystem,
859 			    struct spdk_nvme_cmd *cmd,
860 			    struct spdk_nvme_cpl *rsp,
861 			    struct spdk_nvme_ns_data *nsdata)
862 {
863 	struct spdk_nvmf_ns *ns;
864 
865 	ns = _spdk_nvmf_subsystem_get_ns(subsystem, cmd->nsid);
866 	if (ns == NULL || ns->bdev == NULL) {
867 		SPDK_ERRLOG("Identify Namespace for invalid NSID %u\n", cmd->nsid);
868 		rsp->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT;
869 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
870 	}
871 
872 	return spdk_nvmf_bdev_ctrlr_identify_ns(ns, nsdata);
873 }
874 
875 static int
876 spdk_nvmf_ctrlr_identify_ctrlr(struct spdk_nvmf_ctrlr *ctrlr, struct spdk_nvme_ctrlr_data *cdata)
877 {
878 	struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys;
879 	struct spdk_nvmf_tgt *tgt = subsystem->tgt;
880 
881 	/*
882 	 * Common fields for discovery and NVM subsystems
883 	 */
884 	spdk_strcpy_pad(cdata->fr, FW_VERSION, sizeof(cdata->fr), ' ');
885 	assert((tgt->opts.max_io_size % 4096) == 0);
886 	cdata->mdts = spdk_u32log2(tgt->opts.max_io_size / 4096);
887 	cdata->cntlid = ctrlr->cntlid;
888 	cdata->ver = ctrlr->vcprop.vs;
889 	cdata->lpa.edlp = 1;
890 	cdata->elpe = 127;
891 	cdata->maxcmd = tgt->opts.max_queue_depth;
892 	cdata->sgls.supported = 1;
893 	cdata->sgls.keyed_sgl = 1;
894 	cdata->sgls.sgl_offset = 1;
895 	spdk_strcpy_pad(cdata->subnqn, subsystem->subnqn, sizeof(cdata->subnqn), '\0');
896 
897 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ctrlr data: maxcmd 0x%x\n", cdata->maxcmd);
898 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "sgls data: 0x%x\n", from_le32(&cdata->sgls));
899 
900 	/*
901 	 * NVM subsystem fields (reserved for discovery subsystems)
902 	 */
903 	if (subsystem->subtype == SPDK_NVMF_SUBTYPE_NVME) {
904 		spdk_strcpy_pad(cdata->mn, MODEL_NUMBER, sizeof(cdata->mn), ' ');
905 		spdk_strcpy_pad(cdata->sn, spdk_nvmf_subsystem_get_sn(subsystem), sizeof(cdata->sn), ' ');
906 		cdata->aerl = 0;
907 		cdata->kas = 10;
908 
909 		cdata->rab = 6;
910 		cdata->ctratt.host_id_exhid_supported = 1;
911 		cdata->aerl = 0;
912 		cdata->frmw.slot1_ro = 1;
913 		cdata->frmw.num_slots = 1;
914 
915 		cdata->sqes.min = 6;
916 		cdata->sqes.max = 6;
917 		cdata->cqes.min = 4;
918 		cdata->cqes.max = 4;
919 		cdata->nn = subsystem->max_nsid;
920 		cdata->vwc.present = 1;
921 
922 		cdata->nvmf_specific.ioccsz = sizeof(struct spdk_nvme_cmd) / 16;
923 		cdata->nvmf_specific.iorcsz = sizeof(struct spdk_nvme_cpl) / 16;
924 		cdata->nvmf_specific.icdoff = 0; /* offset starts directly after SQE */
925 		cdata->nvmf_specific.ctrattr.ctrlr_model = SPDK_NVMF_CTRLR_MODEL_DYNAMIC;
926 		cdata->nvmf_specific.msdbd = 1; /* target supports single SGL in capsule */
927 
928 		/* TODO: this should be set by the transport */
929 		cdata->nvmf_specific.ioccsz += tgt->opts.in_capsule_data_size / 16;
930 
931 		cdata->oncs.dsm = spdk_nvmf_ctrlr_dsm_supported(ctrlr);
932 		cdata->oncs.write_zeroes = spdk_nvmf_ctrlr_write_zeroes_supported(ctrlr);
933 
934 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ext ctrlr data: ioccsz 0x%x\n",
935 			      cdata->nvmf_specific.ioccsz);
936 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ext ctrlr data: iorcsz 0x%x\n",
937 			      cdata->nvmf_specific.iorcsz);
938 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ext ctrlr data: icdoff 0x%x\n",
939 			      cdata->nvmf_specific.icdoff);
940 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ext ctrlr data: ctrattr 0x%x\n",
941 			      *(uint8_t *)&cdata->nvmf_specific.ctrattr);
942 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ext ctrlr data: msdbd 0x%x\n",
943 			      cdata->nvmf_specific.msdbd);
944 	}
945 
946 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
947 }
948 
949 static int
950 spdk_nvmf_ctrlr_identify_active_ns_list(struct spdk_nvmf_subsystem *subsystem,
951 					struct spdk_nvme_cmd *cmd,
952 					struct spdk_nvme_cpl *rsp,
953 					struct spdk_nvme_ns_list *ns_list)
954 {
955 	struct spdk_nvmf_ns *ns;
956 	uint32_t count = 0;
957 
958 	if (cmd->nsid >= 0xfffffffeUL) {
959 		SPDK_ERRLOG("Identify Active Namespace List with invalid NSID %u\n", cmd->nsid);
960 		rsp->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT;
961 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
962 	}
963 
964 	for (ns = spdk_nvmf_subsystem_get_first_ns(subsystem); ns != NULL;
965 	     ns = spdk_nvmf_subsystem_get_next_ns(subsystem, ns)) {
966 		if (ns->opts.nsid <= cmd->nsid) {
967 			continue;
968 		}
969 
970 		ns_list->ns_list[count++] = ns->opts.nsid;
971 		if (count == SPDK_COUNTOF(ns_list->ns_list)) {
972 			break;
973 		}
974 	}
975 
976 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
977 }
978 
979 static int
980 spdk_nvmf_ctrlr_identify(struct spdk_nvmf_request *req)
981 {
982 	uint8_t cns;
983 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
984 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
985 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
986 	struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys;
987 
988 	if (req->data == NULL || req->length < 4096) {
989 		SPDK_ERRLOG("identify command with invalid buffer\n");
990 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
991 		rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
992 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
993 	}
994 
995 	cns = cmd->cdw10 & 0xFF;
996 
997 	if (subsystem->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY &&
998 	    cns != SPDK_NVME_IDENTIFY_CTRLR) {
999 		/* Discovery controllers only support Identify Controller */
1000 		goto invalid_cns;
1001 	}
1002 
1003 	switch (cns) {
1004 	case SPDK_NVME_IDENTIFY_NS:
1005 		return spdk_nvmf_ctrlr_identify_ns(subsystem, cmd, rsp, req->data);
1006 	case SPDK_NVME_IDENTIFY_CTRLR:
1007 		return spdk_nvmf_ctrlr_identify_ctrlr(ctrlr, req->data);
1008 	case SPDK_NVME_IDENTIFY_ACTIVE_NS_LIST:
1009 		return spdk_nvmf_ctrlr_identify_active_ns_list(subsystem, cmd, rsp, req->data);
1010 	default:
1011 		goto invalid_cns;
1012 	}
1013 
1014 invalid_cns:
1015 	SPDK_ERRLOG("Identify command with unsupported CNS 0x%02x\n", cns);
1016 	rsp->status.sct = SPDK_NVME_SCT_GENERIC;
1017 	rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1018 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1019 }
1020 
1021 static int
1022 spdk_nvmf_ctrlr_abort(struct spdk_nvmf_request *req)
1023 {
1024 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1025 	struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
1026 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1027 	uint32_t cdw10 = cmd->cdw10;
1028 	uint16_t cid = cdw10 >> 16;
1029 	uint16_t sqid = cdw10 & 0xFFFFu;
1030 	struct spdk_nvmf_qpair *qpair;
1031 	struct spdk_nvmf_request *req_to_abort;
1032 
1033 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "abort sqid=%u cid=%u\n", sqid, cid);
1034 
1035 	rsp->cdw0 = 1; /* Command not aborted */
1036 
1037 	qpair = spdk_nvmf_ctrlr_get_qpair(ctrlr, sqid);
1038 	if (qpair == NULL) {
1039 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "sqid %u not found\n", sqid);
1040 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
1041 		rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1042 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1043 	}
1044 
1045 	/*
1046 	 * NOTE: This relies on the assumption that all connections for a ctrlr will be handled
1047 	 * on the same thread.  If this assumption becomes untrue, this will need to pass a message
1048 	 * to the thread handling qpair, and the abort will need to be asynchronous.
1049 	 */
1050 	req_to_abort = spdk_nvmf_qpair_get_request(qpair, cid);
1051 	if (req_to_abort == NULL) {
1052 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "cid %u not found\n", cid);
1053 		rsp->status.sct = SPDK_NVME_SCT_GENERIC;
1054 		rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1055 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1056 	}
1057 
1058 	if (spdk_nvmf_request_abort(req_to_abort) == 0) {
1059 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "abort ctrlr=%p req=%p sqid=%u cid=%u successful\n",
1060 			      ctrlr, req_to_abort, sqid, cid);
1061 		rsp->cdw0 = 0; /* Command successfully aborted */
1062 	}
1063 	rsp->status.sct = SPDK_NVME_SCT_GENERIC;
1064 	rsp->status.sc = SPDK_NVME_SC_SUCCESS;
1065 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1066 }
1067 
1068 static int
1069 spdk_nvmf_ctrlr_get_features(struct spdk_nvmf_request *req)
1070 {
1071 	uint8_t feature;
1072 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1073 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
1074 
1075 	feature = cmd->cdw10 & 0xff; /* mask out the FID value */
1076 	switch (feature) {
1077 	case SPDK_NVME_FEAT_NUMBER_OF_QUEUES:
1078 		return spdk_nvmf_ctrlr_get_features_number_of_queues(req);
1079 	case SPDK_NVME_FEAT_VOLATILE_WRITE_CACHE:
1080 		return spdk_nvmf_ctrlr_get_features_write_cache(req);
1081 	case SPDK_NVME_FEAT_KEEP_ALIVE_TIMER:
1082 		return spdk_nvmf_ctrlr_get_features_keep_alive_timer(req);
1083 	case SPDK_NVME_FEAT_ASYNC_EVENT_CONFIGURATION:
1084 		return spdk_nvmf_ctrlr_get_features_async_event_configuration(req);
1085 	case SPDK_NVME_FEAT_HOST_IDENTIFIER:
1086 		return spdk_nvmf_ctrlr_get_features_host_identifier(req);
1087 	default:
1088 		SPDK_ERRLOG("Get Features command with unsupported feature ID 0x%02x\n", feature);
1089 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1090 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1091 	}
1092 }
1093 
1094 static int
1095 spdk_nvmf_ctrlr_set_features(struct spdk_nvmf_request *req)
1096 {
1097 	uint8_t feature;
1098 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1099 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
1100 
1101 	feature = cmd->cdw10 & 0xff; /* mask out the FID value */
1102 	switch (feature) {
1103 	case SPDK_NVME_FEAT_NUMBER_OF_QUEUES:
1104 		return spdk_nvmf_ctrlr_set_features_number_of_queues(req);
1105 	case SPDK_NVME_FEAT_KEEP_ALIVE_TIMER:
1106 		return spdk_nvmf_ctrlr_set_features_keep_alive_timer(req);
1107 	case SPDK_NVME_FEAT_ASYNC_EVENT_CONFIGURATION:
1108 		return spdk_nvmf_ctrlr_set_features_async_event_configuration(req);
1109 	case SPDK_NVME_FEAT_HOST_IDENTIFIER:
1110 		return spdk_nvmf_ctrlr_set_features_host_identifier(req);
1111 	default:
1112 		SPDK_ERRLOG("Set Features command with unsupported feature ID 0x%02x\n", feature);
1113 		response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
1114 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1115 	}
1116 }
1117 
1118 static int
1119 spdk_nvmf_ctrlr_keep_alive(struct spdk_nvmf_request *req)
1120 {
1121 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Keep Alive\n");
1122 	/*
1123 	 * To handle keep alive just clear or reset the
1124 	 * ctrlr based keep alive duration counter.
1125 	 * When added, a separate timer based process
1126 	 * will monitor if the time since last recorded
1127 	 * keep alive has exceeded the max duration and
1128 	 * take appropriate action.
1129 	 */
1130 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1131 }
1132 
1133 int
1134 spdk_nvmf_ctrlr_process_admin_cmd(struct spdk_nvmf_request *req)
1135 {
1136 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
1137 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1138 	struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
1139 
1140 	if (ctrlr == NULL) {
1141 		SPDK_ERRLOG("Admin command sent before CONNECT\n");
1142 		response->status.sct = SPDK_NVME_SCT_GENERIC;
1143 		response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
1144 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1145 	}
1146 
1147 	if (ctrlr->vcprop.cc.bits.en != 1) {
1148 		SPDK_ERRLOG("Admin command sent to disabled controller\n");
1149 		response->status.sct = SPDK_NVME_SCT_GENERIC;
1150 		response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
1151 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1152 	}
1153 
1154 	if (req->data && spdk_nvme_opc_get_data_transfer(cmd->opc) == SPDK_NVME_DATA_CONTROLLER_TO_HOST) {
1155 		memset(req->data, 0, req->length);
1156 	}
1157 
1158 	if (ctrlr->subsys->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) {
1159 		/* Discovery controllers only support Get Log Page and Identify */
1160 		switch (cmd->opc) {
1161 		case SPDK_NVME_OPC_IDENTIFY:
1162 		case SPDK_NVME_OPC_GET_LOG_PAGE:
1163 			break;
1164 		default:
1165 			goto invalid_opcode;
1166 		}
1167 	}
1168 
1169 	switch (cmd->opc) {
1170 	case SPDK_NVME_OPC_GET_LOG_PAGE:
1171 		return spdk_nvmf_ctrlr_get_log_page(req);
1172 	case SPDK_NVME_OPC_IDENTIFY:
1173 		return spdk_nvmf_ctrlr_identify(req);
1174 	case SPDK_NVME_OPC_ABORT:
1175 		return spdk_nvmf_ctrlr_abort(req);
1176 	case SPDK_NVME_OPC_GET_FEATURES:
1177 		return spdk_nvmf_ctrlr_get_features(req);
1178 	case SPDK_NVME_OPC_SET_FEATURES:
1179 		return spdk_nvmf_ctrlr_set_features(req);
1180 	case SPDK_NVME_OPC_ASYNC_EVENT_REQUEST:
1181 		return spdk_nvmf_ctrlr_async_event_request(req);
1182 	case SPDK_NVME_OPC_KEEP_ALIVE:
1183 		return spdk_nvmf_ctrlr_keep_alive(req);
1184 
1185 	case SPDK_NVME_OPC_CREATE_IO_SQ:
1186 	case SPDK_NVME_OPC_CREATE_IO_CQ:
1187 	case SPDK_NVME_OPC_DELETE_IO_SQ:
1188 	case SPDK_NVME_OPC_DELETE_IO_CQ:
1189 		/* Create and Delete I/O CQ/SQ not allowed in NVMe-oF */
1190 		goto invalid_opcode;
1191 
1192 	default:
1193 		goto invalid_opcode;
1194 	}
1195 
1196 invalid_opcode:
1197 	SPDK_ERRLOG("Unsupported admin opcode 0x%x\n", cmd->opc);
1198 	response->status.sct = SPDK_NVME_SCT_GENERIC;
1199 	response->status.sc = SPDK_NVME_SC_INVALID_OPCODE;
1200 	return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1201 }
1202 
1203 int
1204 spdk_nvmf_ctrlr_process_fabrics_cmd(struct spdk_nvmf_request *req)
1205 {
1206 	struct spdk_nvmf_qpair *qpair = req->qpair;
1207 	struct spdk_nvmf_capsule_cmd *cap_hdr;
1208 
1209 	cap_hdr = &req->cmd->nvmf_cmd;
1210 
1211 	if (qpair->ctrlr == NULL) {
1212 		/* No ctrlr established yet; the only valid command is Connect */
1213 		if (cap_hdr->fctype == SPDK_NVMF_FABRIC_COMMAND_CONNECT) {
1214 			return spdk_nvmf_ctrlr_connect(req);
1215 		} else {
1216 			SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Got fctype 0x%x, expected Connect\n",
1217 				      cap_hdr->fctype);
1218 			req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
1219 			req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR;
1220 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1221 		}
1222 	} else if (spdk_nvmf_qpair_is_admin_queue(qpair)) {
1223 		/*
1224 		 * Controller session is established, and this is an admin queue.
1225 		 * Disallow Connect and allow other fabrics commands.
1226 		 */
1227 		switch (cap_hdr->fctype) {
1228 		case SPDK_NVMF_FABRIC_COMMAND_PROPERTY_SET:
1229 			return spdk_nvmf_property_set(req);
1230 		case SPDK_NVMF_FABRIC_COMMAND_PROPERTY_GET:
1231 			return spdk_nvmf_property_get(req);
1232 		default:
1233 			SPDK_DEBUGLOG(SPDK_LOG_NVMF, "unknown fctype 0x%02x\n",
1234 				      cap_hdr->fctype);
1235 			req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
1236 			req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_INVALID_OPCODE;
1237 			return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1238 		}
1239 	} else {
1240 		/* Controller session is established, and this is an I/O queue */
1241 		/* For now, no I/O-specific Fabrics commands are implemented (other than Connect) */
1242 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Unexpected I/O fctype 0x%x\n", cap_hdr->fctype);
1243 		req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
1244 		req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_INVALID_OPCODE;
1245 		return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
1246 	}
1247 }
1248