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