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