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