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