1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (c) Intel Corporation. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * * Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * * Neither the name of Intel Corporation nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include "spdk/stdinc.h" 35 36 #include "nvmf_internal.h" 37 #include "transport.h" 38 39 #include "spdk/bit_array.h" 40 #include "spdk/endian.h" 41 #include "spdk/thread.h" 42 #include "spdk/trace.h" 43 #include "spdk/nvme_spec.h" 44 #include "spdk/string.h" 45 #include "spdk/util.h" 46 #include "spdk/version.h" 47 48 #include "spdk_internal/log.h" 49 50 #define MIN_KEEP_ALIVE_TIMEOUT_IN_MS 10000 51 #define NVMF_DISC_KATO_IN_MS 120000 52 #define KAS_TIME_UNIT_IN_MS 100 53 #define KAS_DEFAULT_VALUE (MIN_KEEP_ALIVE_TIMEOUT_IN_MS / KAS_TIME_UNIT_IN_MS) 54 55 /* 56 * Report the SPDK version as the firmware revision. 57 * SPDK_VERSION_STRING won't fit into FR (only 8 bytes), so try to fit the most important parts. 58 */ 59 #define FW_VERSION SPDK_VERSION_MAJOR_STRING SPDK_VERSION_MINOR_STRING SPDK_VERSION_PATCH_STRING 60 61 static inline void 62 spdk_nvmf_invalid_connect_response(struct spdk_nvmf_fabric_connect_rsp *rsp, 63 uint8_t iattr, uint16_t ipo) 64 { 65 rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 66 rsp->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM; 67 rsp->status_code_specific.invalid.iattr = iattr; 68 rsp->status_code_specific.invalid.ipo = ipo; 69 } 70 71 #define SPDK_NVMF_INVALID_CONNECT_CMD(rsp, field) \ 72 spdk_nvmf_invalid_connect_response(rsp, 0, offsetof(struct spdk_nvmf_fabric_connect_cmd, field)) 73 #define SPDK_NVMF_INVALID_CONNECT_DATA(rsp, field) \ 74 spdk_nvmf_invalid_connect_response(rsp, 1, offsetof(struct spdk_nvmf_fabric_connect_data, field)) 75 76 static void 77 spdk_nvmf_ctrlr_stop_keep_alive_timer(struct spdk_nvmf_ctrlr *ctrlr) 78 { 79 if (!ctrlr) { 80 SPDK_ERRLOG("Controller is NULL\n"); 81 return; 82 } 83 84 if (ctrlr->keep_alive_poller == NULL) { 85 return; 86 } 87 88 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Stop keep alive poller\n"); 89 spdk_poller_unregister(&ctrlr->keep_alive_poller); 90 } 91 92 static void 93 spdk_nvmf_ctrlr_disconnect_qpairs_done(struct spdk_io_channel_iter *i, int status) 94 { 95 if (status == 0) { 96 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ctrlr disconnect qpairs complete successfully\n"); 97 } else { 98 SPDK_ERRLOG("Fail to disconnect ctrlr qpairs\n"); 99 } 100 } 101 102 static void 103 spdk_nvmf_ctrlr_disconnect_qpairs_on_pg(struct spdk_io_channel_iter *i) 104 { 105 int rc = 0; 106 struct spdk_nvmf_ctrlr *ctrlr; 107 struct spdk_nvmf_qpair *qpair, *temp_qpair; 108 struct spdk_io_channel *ch; 109 struct spdk_nvmf_poll_group *group; 110 111 ctrlr = spdk_io_channel_iter_get_ctx(i); 112 ch = spdk_io_channel_iter_get_channel(i); 113 group = spdk_io_channel_get_ctx(ch); 114 115 TAILQ_FOREACH_SAFE(qpair, &group->qpairs, link, temp_qpair) { 116 if (qpair->ctrlr == ctrlr) { 117 rc = spdk_nvmf_qpair_disconnect(qpair, NULL, NULL); 118 if (rc) { 119 SPDK_ERRLOG("Qpair disconnect failed\n"); 120 goto next_channel; 121 } 122 } 123 } 124 125 next_channel: 126 spdk_for_each_channel_continue(i, rc); 127 } 128 129 static int 130 spdk_nvmf_ctrlr_keep_alive_poll(void *ctx) 131 { 132 uint64_t keep_alive_timeout_tick; 133 uint64_t now = spdk_get_ticks(); 134 struct spdk_nvmf_ctrlr *ctrlr = ctx; 135 136 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Polling ctrlr keep alive timeout\n"); 137 138 /* If the Keep alive feature is in use and the timer expires */ 139 keep_alive_timeout_tick = ctrlr->last_keep_alive_tick + 140 ctrlr->feat.keep_alive_timer.bits.kato * spdk_get_ticks_hz() / UINT64_C(1000); 141 if (now > keep_alive_timeout_tick) { 142 SPDK_NOTICELOG("Disconnecting host from subsystem %s due to keep alive timeout.\n", 143 ctrlr->subsys->subnqn); 144 /* set the Controller Fatal Status bit to '1' */ 145 if (ctrlr->vcprop.csts.bits.cfs == 0) { 146 ctrlr->vcprop.csts.bits.cfs = 1; 147 148 /* 149 * disconnect qpairs, terminate Transport connection 150 * destroy ctrlr, break the host to controller association 151 * disconnect qpairs with qpair->ctrlr == ctrlr 152 */ 153 spdk_for_each_channel(ctrlr->subsys->tgt, 154 spdk_nvmf_ctrlr_disconnect_qpairs_on_pg, 155 ctrlr, 156 spdk_nvmf_ctrlr_disconnect_qpairs_done); 157 } 158 } 159 160 return 1; 161 } 162 163 static void 164 spdk_nvmf_ctrlr_start_keep_alive_timer(struct spdk_nvmf_ctrlr *ctrlr) 165 { 166 if (!ctrlr) { 167 SPDK_ERRLOG("Controller is NULL\n"); 168 return; 169 } 170 171 /* if cleared to 0 then the Keep Alive Timer is disabled */ 172 if (ctrlr->feat.keep_alive_timer.bits.kato != 0) { 173 174 ctrlr->last_keep_alive_tick = spdk_get_ticks(); 175 176 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Ctrlr add keep alive poller\n"); 177 ctrlr->keep_alive_poller = spdk_poller_register(spdk_nvmf_ctrlr_keep_alive_poll, ctrlr, 178 ctrlr->feat.keep_alive_timer.bits.kato * 1000); 179 } 180 } 181 182 static void 183 ctrlr_add_qpair_and_update_rsp(struct spdk_nvmf_qpair *qpair, 184 struct spdk_nvmf_ctrlr *ctrlr, 185 struct spdk_nvmf_fabric_connect_rsp *rsp) 186 { 187 assert(ctrlr->admin_qpair->group->thread == spdk_get_thread()); 188 189 /* check if we would exceed ctrlr connection limit */ 190 if (qpair->qid >= spdk_bit_array_capacity(ctrlr->qpair_mask)) { 191 SPDK_ERRLOG("Requested QID %u but Max QID is %u\n", 192 qpair->qid, spdk_bit_array_capacity(ctrlr->qpair_mask) - 1); 193 rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 194 rsp->status.sc = SPDK_NVME_SC_INVALID_QUEUE_IDENTIFIER; 195 return; 196 } 197 198 if (spdk_bit_array_get(ctrlr->qpair_mask, qpair->qid)) { 199 SPDK_ERRLOG("Got I/O connect with duplicate QID %u\n", qpair->qid); 200 rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 201 rsp->status.sc = SPDK_NVME_SC_INVALID_QUEUE_IDENTIFIER; 202 return; 203 } 204 205 qpair->ctrlr = ctrlr; 206 spdk_bit_array_set(ctrlr->qpair_mask, qpair->qid); 207 208 rsp->status.sc = SPDK_NVME_SC_SUCCESS; 209 rsp->status_code_specific.success.cntlid = ctrlr->cntlid; 210 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "connect capsule response: cntlid = 0x%04x\n", 211 rsp->status_code_specific.success.cntlid); 212 } 213 214 static void 215 _spdk_nvmf_request_complete(void *ctx) 216 { 217 struct spdk_nvmf_request *req = ctx; 218 219 spdk_nvmf_request_complete(req); 220 } 221 222 static void 223 _spdk_nvmf_ctrlr_add_admin_qpair(void *ctx) 224 { 225 struct spdk_nvmf_request *req = ctx; 226 struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp; 227 struct spdk_nvmf_qpair *qpair = req->qpair; 228 struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr; 229 230 ctrlr->admin_qpair = qpair; 231 spdk_nvmf_ctrlr_start_keep_alive_timer(ctrlr); 232 ctrlr_add_qpair_and_update_rsp(qpair, ctrlr, rsp); 233 spdk_nvmf_request_complete(req); 234 } 235 236 static void 237 _spdk_nvmf_subsystem_add_ctrlr(void *ctx) 238 { 239 struct spdk_nvmf_request *req = ctx; 240 struct spdk_nvmf_qpair *qpair = req->qpair; 241 struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp; 242 struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr; 243 244 if (spdk_nvmf_subsystem_add_ctrlr(ctrlr->subsys, ctrlr)) { 245 SPDK_ERRLOG("Unable to add controller to subsystem\n"); 246 spdk_bit_array_free(&ctrlr->qpair_mask); 247 free(ctrlr); 248 qpair->ctrlr = NULL; 249 rsp->status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR; 250 spdk_thread_send_msg(qpair->group->thread, _spdk_nvmf_request_complete, req); 251 return; 252 } 253 254 spdk_thread_send_msg(ctrlr->thread, _spdk_nvmf_ctrlr_add_admin_qpair, req); 255 } 256 257 static struct spdk_nvmf_ctrlr * 258 spdk_nvmf_ctrlr_create(struct spdk_nvmf_subsystem *subsystem, 259 struct spdk_nvmf_request *req, 260 struct spdk_nvmf_fabric_connect_cmd *connect_cmd, 261 struct spdk_nvmf_fabric_connect_data *connect_data) 262 { 263 struct spdk_nvmf_ctrlr *ctrlr; 264 struct spdk_nvmf_transport *transport; 265 266 ctrlr = calloc(1, sizeof(*ctrlr)); 267 if (ctrlr == NULL) { 268 SPDK_ERRLOG("Memory allocation failed\n"); 269 return NULL; 270 } 271 272 TAILQ_INIT(&ctrlr->log_head); 273 ctrlr->subsys = subsystem; 274 ctrlr->thread = req->qpair->group->thread; 275 276 transport = req->qpair->transport; 277 ctrlr->qpair_mask = spdk_bit_array_create(transport->opts.max_qpairs_per_ctrlr); 278 if (!ctrlr->qpair_mask) { 279 SPDK_ERRLOG("Failed to allocate controller qpair mask\n"); 280 free(ctrlr); 281 return NULL; 282 } 283 284 /* 285 * KAS: this field indicates the granularity of the Keep Alive Timer in 100ms units 286 * keep-alive timeout in milliseconds 287 */ 288 ctrlr->feat.keep_alive_timer.bits.kato = spdk_divide_round_up(connect_cmd->kato, 289 KAS_DEFAULT_VALUE * KAS_TIME_UNIT_IN_MS) * 290 KAS_DEFAULT_VALUE * KAS_TIME_UNIT_IN_MS; 291 ctrlr->feat.async_event_configuration.bits.ns_attr_notice = 1; 292 ctrlr->feat.volatile_write_cache.bits.wce = 1; 293 294 if (ctrlr->subsys->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) { 295 /* Don't accept keep-alive timeout for discovery controllers */ 296 if (ctrlr->feat.keep_alive_timer.bits.kato != 0) { 297 SPDK_ERRLOG("Discovery controller don't accept keep-alive timeout\n"); 298 spdk_bit_array_free(&ctrlr->qpair_mask); 299 free(ctrlr); 300 return NULL; 301 } 302 303 /* 304 * Discovery controllers use some arbitrary high value in order 305 * to cleanup stale discovery sessions 306 * 307 * From the 1.0a nvme-of spec: 308 * "The Keep Alive command is reserved for 309 * Discovery controllers. A transport may specify a 310 * fixed Discovery controller activity timeout value 311 * (e.g., 2 minutes). If no commands are received 312 * by a Discovery controller within that time 313 * period, the controller may perform the 314 * actions for Keep Alive Timer expiration". 315 * kato is in millisecond. 316 */ 317 ctrlr->feat.keep_alive_timer.bits.kato = NVMF_DISC_KATO_IN_MS; 318 } 319 320 /* Subtract 1 for admin queue, 1 for 0's based */ 321 ctrlr->feat.number_of_queues.bits.ncqr = transport->opts.max_qpairs_per_ctrlr - 1 - 322 1; 323 ctrlr->feat.number_of_queues.bits.nsqr = transport->opts.max_qpairs_per_ctrlr - 1 - 324 1; 325 326 spdk_uuid_copy(&ctrlr->hostid, (struct spdk_uuid *)connect_data->hostid); 327 328 ctrlr->vcprop.cap.raw = 0; 329 ctrlr->vcprop.cap.bits.cqr = 1; /* NVMe-oF specification required */ 330 ctrlr->vcprop.cap.bits.mqes = transport->opts.max_queue_depth - 331 1; /* max queue depth */ 332 ctrlr->vcprop.cap.bits.ams = 0; /* optional arb mechanisms */ 333 ctrlr->vcprop.cap.bits.to = 1; /* ready timeout - 500 msec units */ 334 ctrlr->vcprop.cap.bits.dstrd = 0; /* fixed to 0 for NVMe-oF */ 335 ctrlr->vcprop.cap.bits.css = SPDK_NVME_CAP_CSS_NVM; /* NVM command set */ 336 ctrlr->vcprop.cap.bits.mpsmin = 0; /* 2 ^ (12 + mpsmin) == 4k */ 337 ctrlr->vcprop.cap.bits.mpsmax = 0; /* 2 ^ (12 + mpsmax) == 4k */ 338 339 /* Version Supported: 1.3 */ 340 ctrlr->vcprop.vs.bits.mjr = 1; 341 ctrlr->vcprop.vs.bits.mnr = 3; 342 ctrlr->vcprop.vs.bits.ter = 0; 343 344 ctrlr->vcprop.cc.raw = 0; 345 ctrlr->vcprop.cc.bits.en = 0; /* Init controller disabled */ 346 347 ctrlr->vcprop.csts.raw = 0; 348 ctrlr->vcprop.csts.bits.rdy = 0; /* Init controller as not ready */ 349 350 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "cap 0x%" PRIx64 "\n", ctrlr->vcprop.cap.raw); 351 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "vs 0x%x\n", ctrlr->vcprop.vs.raw); 352 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "cc 0x%x\n", ctrlr->vcprop.cc.raw); 353 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "csts 0x%x\n", ctrlr->vcprop.csts.raw); 354 355 req->qpair->ctrlr = ctrlr; 356 spdk_thread_send_msg(subsystem->thread, _spdk_nvmf_subsystem_add_ctrlr, req); 357 358 return ctrlr; 359 } 360 361 static void 362 _spdk_nvmf_ctrlr_destruct(void *ctx) 363 { 364 struct spdk_nvmf_ctrlr *ctrlr = ctx; 365 struct spdk_nvmf_reservation_log *log, *log_tmp; 366 367 spdk_nvmf_ctrlr_stop_keep_alive_timer(ctrlr); 368 369 TAILQ_FOREACH_SAFE(log, &ctrlr->log_head, link, log_tmp) { 370 TAILQ_REMOVE(&ctrlr->log_head, log, link); 371 free(log); 372 } 373 free(ctrlr); 374 } 375 376 void 377 spdk_nvmf_ctrlr_destruct(struct spdk_nvmf_ctrlr *ctrlr) 378 { 379 spdk_nvmf_subsystem_remove_ctrlr(ctrlr->subsys, ctrlr); 380 381 spdk_thread_send_msg(ctrlr->thread, _spdk_nvmf_ctrlr_destruct, ctrlr); 382 } 383 384 static void 385 spdk_nvmf_ctrlr_add_io_qpair(void *ctx) 386 { 387 struct spdk_nvmf_request *req = ctx; 388 struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp; 389 struct spdk_nvmf_qpair *qpair = req->qpair; 390 struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr; 391 392 /* Unit test will check qpair->ctrlr after calling spdk_nvmf_ctrlr_connect. 393 * For error case, the value should be NULL. So set it to NULL at first. 394 */ 395 qpair->ctrlr = NULL; 396 397 if (ctrlr->subsys->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) { 398 SPDK_ERRLOG("I/O connect not allowed on discovery controller\n"); 399 SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid); 400 goto end; 401 } 402 403 if (!ctrlr->vcprop.cc.bits.en) { 404 SPDK_ERRLOG("Got I/O connect before ctrlr was enabled\n"); 405 SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid); 406 goto end; 407 } 408 409 if (1u << ctrlr->vcprop.cc.bits.iosqes != sizeof(struct spdk_nvme_cmd)) { 410 SPDK_ERRLOG("Got I/O connect with invalid IOSQES %u\n", 411 ctrlr->vcprop.cc.bits.iosqes); 412 SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid); 413 goto end; 414 } 415 416 if (1u << ctrlr->vcprop.cc.bits.iocqes != sizeof(struct spdk_nvme_cpl)) { 417 SPDK_ERRLOG("Got I/O connect with invalid IOCQES %u\n", 418 ctrlr->vcprop.cc.bits.iocqes); 419 SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid); 420 goto end; 421 } 422 423 ctrlr_add_qpair_and_update_rsp(qpair, ctrlr, rsp); 424 end: 425 spdk_thread_send_msg(qpair->group->thread, _spdk_nvmf_request_complete, req); 426 } 427 428 static void 429 _spdk_nvmf_ctrlr_add_io_qpair(void *ctx) 430 { 431 struct spdk_nvmf_request *req = ctx; 432 struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp; 433 struct spdk_nvmf_fabric_connect_data *data = req->data; 434 struct spdk_nvmf_ctrlr *ctrlr; 435 struct spdk_nvmf_qpair *qpair = req->qpair; 436 struct spdk_nvmf_qpair *admin_qpair; 437 struct spdk_nvmf_tgt *tgt = qpair->transport->tgt; 438 struct spdk_nvmf_subsystem *subsystem; 439 440 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Connect I/O Queue for controller id 0x%x\n", data->cntlid); 441 442 subsystem = spdk_nvmf_tgt_find_subsystem(tgt, data->subnqn); 443 /* We already checked this in spdk_nvmf_ctrlr_connect */ 444 assert(subsystem != NULL); 445 446 ctrlr = spdk_nvmf_subsystem_get_ctrlr(subsystem, data->cntlid); 447 if (ctrlr == NULL) { 448 SPDK_ERRLOG("Unknown controller ID 0x%x\n", data->cntlid); 449 SPDK_NVMF_INVALID_CONNECT_DATA(rsp, cntlid); 450 spdk_thread_send_msg(qpair->group->thread, _spdk_nvmf_request_complete, req); 451 return; 452 } 453 454 admin_qpair = ctrlr->admin_qpair; 455 qpair->ctrlr = ctrlr; 456 spdk_thread_send_msg(admin_qpair->group->thread, spdk_nvmf_ctrlr_add_io_qpair, req); 457 } 458 459 static int 460 spdk_nvmf_ctrlr_connect(struct spdk_nvmf_request *req) 461 { 462 struct spdk_nvmf_fabric_connect_data *data = req->data; 463 struct spdk_nvmf_fabric_connect_cmd *cmd = &req->cmd->connect_cmd; 464 struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp; 465 struct spdk_nvmf_qpair *qpair = req->qpair; 466 struct spdk_nvmf_transport *transport = qpair->transport; 467 struct spdk_nvmf_tgt *tgt = transport->tgt; 468 struct spdk_nvmf_ctrlr *ctrlr; 469 struct spdk_nvmf_subsystem *subsystem; 470 const char *subnqn, *hostnqn; 471 struct spdk_nvme_transport_id listen_trid = {}; 472 void *end; 473 474 if (req->length < sizeof(struct spdk_nvmf_fabric_connect_data)) { 475 SPDK_ERRLOG("Connect command data length 0x%x too small\n", req->length); 476 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 477 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 478 } 479 480 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "recfmt 0x%x qid %u sqsize %u\n", 481 cmd->recfmt, cmd->qid, cmd->sqsize); 482 483 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Connect data:\n"); 484 SPDK_DEBUGLOG(SPDK_LOG_NVMF, " cntlid: 0x%04x\n", data->cntlid); 485 SPDK_DEBUGLOG(SPDK_LOG_NVMF, " hostid: %08x-%04x-%04x-%02x%02x-%04x%08x ***\n", 486 ntohl(*(uint32_t *)&data->hostid[0]), 487 ntohs(*(uint16_t *)&data->hostid[4]), 488 ntohs(*(uint16_t *)&data->hostid[6]), 489 data->hostid[8], 490 data->hostid[9], 491 ntohs(*(uint16_t *)&data->hostid[10]), 492 ntohl(*(uint32_t *)&data->hostid[12])); 493 494 if (cmd->recfmt != 0) { 495 SPDK_ERRLOG("Connect command unsupported RECFMT %u\n", cmd->recfmt); 496 rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 497 rsp->status.sc = SPDK_NVMF_FABRIC_SC_INCOMPATIBLE_FORMAT; 498 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 499 } 500 501 /* Ensure that subnqn is null terminated */ 502 end = memchr(data->subnqn, '\0', SPDK_NVMF_NQN_MAX_LEN + 1); 503 if (!end) { 504 SPDK_ERRLOG("Connect SUBNQN is not null terminated\n"); 505 SPDK_NVMF_INVALID_CONNECT_DATA(rsp, subnqn); 506 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 507 } 508 subnqn = data->subnqn; 509 SPDK_DEBUGLOG(SPDK_LOG_NVMF, " subnqn: \"%s\"\n", subnqn); 510 511 subsystem = spdk_nvmf_tgt_find_subsystem(tgt, subnqn); 512 if (subsystem == NULL) { 513 SPDK_ERRLOG("Could not find subsystem '%s'\n", subnqn); 514 SPDK_NVMF_INVALID_CONNECT_DATA(rsp, subnqn); 515 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 516 } 517 518 if ((subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE) || 519 (subsystem->state == SPDK_NVMF_SUBSYSTEM_DEACTIVATING)) { 520 SPDK_ERRLOG("Subsystem '%s' is not ready\n", subnqn); 521 rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 522 rsp->status.sc = SPDK_NVMF_FABRIC_SC_CONTROLLER_BUSY; 523 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 524 } 525 526 /* Ensure that hostnqn is null terminated */ 527 end = memchr(data->hostnqn, '\0', SPDK_NVMF_NQN_MAX_LEN + 1); 528 if (!end) { 529 SPDK_ERRLOG("Connect HOSTNQN is not null terminated\n"); 530 SPDK_NVMF_INVALID_CONNECT_DATA(rsp, hostnqn); 531 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 532 } 533 hostnqn = data->hostnqn; 534 SPDK_DEBUGLOG(SPDK_LOG_NVMF, " hostnqn: \"%s\"\n", hostnqn); 535 536 if (!spdk_nvmf_subsystem_host_allowed(subsystem, hostnqn)) { 537 SPDK_ERRLOG("Subsystem '%s' does not allow host '%s'\n", subnqn, hostnqn); 538 rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 539 rsp->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_HOST; 540 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 541 } 542 543 if (spdk_nvmf_qpair_get_listen_trid(qpair, &listen_trid)) { 544 SPDK_ERRLOG("Subsystem '%s' is unable to enforce access control due to an internal error.\n", 545 subnqn); 546 rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 547 rsp->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_HOST; 548 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 549 } 550 551 if (!spdk_nvmf_subsystem_listener_allowed(subsystem, &listen_trid)) { 552 SPDK_ERRLOG("Subsystem '%s' does not allow host '%s' to connect at this address.\n", subnqn, 553 hostnqn); 554 rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 555 rsp->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_HOST; 556 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 557 } 558 559 /* 560 * SQSIZE is a 0-based value, so it must be at least 1 (minimum queue depth is 2) and 561 * strictly less than max_aq_depth (admin queues) or max_queue_depth (io queues). 562 */ 563 if (cmd->sqsize == 0) { 564 SPDK_ERRLOG("Invalid SQSIZE = 0\n"); 565 SPDK_NVMF_INVALID_CONNECT_CMD(rsp, sqsize); 566 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 567 } 568 569 if (spdk_nvmf_qpair_is_admin_queue(qpair)) { 570 if (cmd->sqsize >= transport->opts.max_aq_depth) { 571 SPDK_ERRLOG("Invalid SQSIZE for admin queue %u (min 1, max %u)\n", 572 cmd->sqsize, transport->opts.max_aq_depth - 1); 573 SPDK_NVMF_INVALID_CONNECT_CMD(rsp, sqsize); 574 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 575 } 576 } else if (cmd->sqsize >= transport->opts.max_queue_depth) { 577 SPDK_ERRLOG("Invalid SQSIZE %u (min 1, max %u)\n", 578 cmd->sqsize, transport->opts.max_queue_depth - 1); 579 SPDK_NVMF_INVALID_CONNECT_CMD(rsp, sqsize); 580 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 581 } 582 583 qpair->sq_head_max = cmd->sqsize; 584 qpair->qid = cmd->qid; 585 586 if (spdk_nvmf_transport_qpair_set_sqsize(qpair)) { 587 SPDK_ERRLOG("Can not create SQSIZE %u for qpair=%p\n", cmd->sqsize, qpair); 588 rsp->status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR; 589 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 590 } 591 592 if (cmd->qid == 0) { 593 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Connect Admin Queue for controller ID 0x%x\n", data->cntlid); 594 595 if (data->cntlid != 0xFFFF) { 596 /* This NVMf target only supports dynamic mode. */ 597 SPDK_ERRLOG("The NVMf target only supports dynamic mode (CNTLID = 0x%x).\n", data->cntlid); 598 SPDK_NVMF_INVALID_CONNECT_DATA(rsp, cntlid); 599 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 600 } 601 602 /* Establish a new ctrlr */ 603 ctrlr = spdk_nvmf_ctrlr_create(subsystem, req, cmd, data); 604 if (!ctrlr) { 605 SPDK_ERRLOG("spdk_nvmf_ctrlr_create() failed\n"); 606 rsp->status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR; 607 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 608 } else { 609 return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS; 610 } 611 } else { 612 spdk_thread_send_msg(subsystem->thread, _spdk_nvmf_ctrlr_add_io_qpair, req); 613 return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS; 614 } 615 } 616 617 static uint64_t 618 nvmf_prop_get_cap(struct spdk_nvmf_ctrlr *ctrlr) 619 { 620 return ctrlr->vcprop.cap.raw; 621 } 622 623 static uint64_t 624 nvmf_prop_get_vs(struct spdk_nvmf_ctrlr *ctrlr) 625 { 626 return ctrlr->vcprop.vs.raw; 627 } 628 629 static uint64_t 630 nvmf_prop_get_cc(struct spdk_nvmf_ctrlr *ctrlr) 631 { 632 return ctrlr->vcprop.cc.raw; 633 } 634 635 static bool 636 nvmf_prop_set_cc(struct spdk_nvmf_ctrlr *ctrlr, uint64_t value) 637 { 638 union spdk_nvme_cc_register cc, diff; 639 640 cc.raw = (uint32_t)value; 641 642 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "cur CC: 0x%08x\n", ctrlr->vcprop.cc.raw); 643 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "new CC: 0x%08x\n", cc.raw); 644 645 /* 646 * Calculate which bits changed between the current and new CC. 647 * Mark each bit as 0 once it is handled to determine if any unhandled bits were changed. 648 */ 649 diff.raw = cc.raw ^ ctrlr->vcprop.cc.raw; 650 651 if (diff.bits.en) { 652 if (cc.bits.en) { 653 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Property Set CC Enable!\n"); 654 ctrlr->vcprop.cc.bits.en = 1; 655 ctrlr->vcprop.csts.bits.rdy = 1; 656 } else { 657 SPDK_ERRLOG("CC.EN transition from 1 to 0 (reset) not implemented!\n"); 658 659 } 660 diff.bits.en = 0; 661 } 662 663 if (diff.bits.shn) { 664 if (cc.bits.shn == SPDK_NVME_SHN_NORMAL || 665 cc.bits.shn == SPDK_NVME_SHN_ABRUPT) { 666 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Property Set CC Shutdown %u%ub!\n", 667 cc.bits.shn >> 1, cc.bits.shn & 1); 668 ctrlr->vcprop.cc.bits.shn = cc.bits.shn; 669 ctrlr->vcprop.cc.bits.en = 0; 670 ctrlr->vcprop.csts.bits.rdy = 0; 671 ctrlr->vcprop.csts.bits.shst = SPDK_NVME_SHST_COMPLETE; 672 } else if (cc.bits.shn == 0) { 673 ctrlr->vcprop.cc.bits.shn = 0; 674 } else { 675 SPDK_ERRLOG("Prop Set CC: Invalid SHN value %u%ub\n", 676 cc.bits.shn >> 1, cc.bits.shn & 1); 677 return false; 678 } 679 diff.bits.shn = 0; 680 } 681 682 if (diff.bits.iosqes) { 683 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Prop Set IOSQES = %u (%u bytes)\n", 684 cc.bits.iosqes, 1u << cc.bits.iosqes); 685 ctrlr->vcprop.cc.bits.iosqes = cc.bits.iosqes; 686 diff.bits.iosqes = 0; 687 } 688 689 if (diff.bits.iocqes) { 690 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Prop Set IOCQES = %u (%u bytes)\n", 691 cc.bits.iocqes, 1u << cc.bits.iocqes); 692 ctrlr->vcprop.cc.bits.iocqes = cc.bits.iocqes; 693 diff.bits.iocqes = 0; 694 } 695 696 if (diff.raw != 0) { 697 SPDK_ERRLOG("Prop Set CC toggled reserved bits 0x%x!\n", diff.raw); 698 return false; 699 } 700 701 return true; 702 } 703 704 static uint64_t 705 nvmf_prop_get_csts(struct spdk_nvmf_ctrlr *ctrlr) 706 { 707 return ctrlr->vcprop.csts.raw; 708 } 709 710 struct nvmf_prop { 711 uint32_t ofst; 712 uint8_t size; 713 char name[11]; 714 uint64_t (*get_cb)(struct spdk_nvmf_ctrlr *ctrlr); 715 bool (*set_cb)(struct spdk_nvmf_ctrlr *ctrlr, uint64_t value); 716 }; 717 718 #define PROP(field, size, get_cb, set_cb) \ 719 { \ 720 offsetof(struct spdk_nvme_registers, field), \ 721 SPDK_NVMF_PROP_SIZE_##size, \ 722 #field, \ 723 get_cb, set_cb \ 724 } 725 726 static const struct nvmf_prop nvmf_props[] = { 727 PROP(cap, 8, nvmf_prop_get_cap, NULL), 728 PROP(vs, 4, nvmf_prop_get_vs, NULL), 729 PROP(cc, 4, nvmf_prop_get_cc, nvmf_prop_set_cc), 730 PROP(csts, 4, nvmf_prop_get_csts, NULL), 731 }; 732 733 static const struct nvmf_prop * 734 find_prop(uint32_t ofst) 735 { 736 size_t i; 737 738 for (i = 0; i < SPDK_COUNTOF(nvmf_props); i++) { 739 const struct nvmf_prop *prop = &nvmf_props[i]; 740 741 if (prop->ofst == ofst) { 742 return prop; 743 } 744 } 745 746 return NULL; 747 } 748 749 static int 750 spdk_nvmf_property_get(struct spdk_nvmf_request *req) 751 { 752 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 753 struct spdk_nvmf_fabric_prop_get_cmd *cmd = &req->cmd->prop_get_cmd; 754 struct spdk_nvmf_fabric_prop_get_rsp *response = &req->rsp->prop_get_rsp; 755 const struct nvmf_prop *prop; 756 757 response->status.sc = 0; 758 response->value.u64 = 0; 759 760 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "size %d, offset 0x%x\n", 761 cmd->attrib.size, cmd->ofst); 762 763 if (cmd->attrib.size != SPDK_NVMF_PROP_SIZE_4 && 764 cmd->attrib.size != SPDK_NVMF_PROP_SIZE_8) { 765 SPDK_ERRLOG("Invalid size value %d\n", cmd->attrib.size); 766 response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 767 response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM; 768 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 769 } 770 771 prop = find_prop(cmd->ofst); 772 if (prop == NULL || prop->get_cb == NULL) { 773 /* Reserved properties return 0 when read */ 774 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 775 } 776 777 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "name: %s\n", prop->name); 778 if (cmd->attrib.size != prop->size) { 779 SPDK_ERRLOG("offset 0x%x size mismatch: cmd %u, prop %u\n", 780 cmd->ofst, cmd->attrib.size, prop->size); 781 response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 782 response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM; 783 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 784 } 785 786 response->value.u64 = prop->get_cb(ctrlr); 787 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "response value: 0x%" PRIx64 "\n", response->value.u64); 788 789 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 790 } 791 792 static int 793 spdk_nvmf_property_set(struct spdk_nvmf_request *req) 794 { 795 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 796 struct spdk_nvmf_fabric_prop_set_cmd *cmd = &req->cmd->prop_set_cmd; 797 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 798 const struct nvmf_prop *prop; 799 uint64_t value; 800 801 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "size %d, offset 0x%x, value 0x%" PRIx64 "\n", 802 cmd->attrib.size, cmd->ofst, cmd->value.u64); 803 804 prop = find_prop(cmd->ofst); 805 if (prop == NULL || prop->set_cb == NULL) { 806 SPDK_ERRLOG("Invalid offset 0x%x\n", cmd->ofst); 807 response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 808 response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM; 809 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 810 } 811 812 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "name: %s\n", prop->name); 813 if (cmd->attrib.size != prop->size) { 814 SPDK_ERRLOG("offset 0x%x size mismatch: cmd %u, prop %u\n", 815 cmd->ofst, cmd->attrib.size, prop->size); 816 response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 817 response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM; 818 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 819 } 820 821 value = cmd->value.u64; 822 if (prop->size == SPDK_NVMF_PROP_SIZE_4) { 823 value = (uint32_t)value; 824 } 825 826 if (!prop->set_cb(ctrlr, value)) { 827 SPDK_ERRLOG("prop set_cb failed\n"); 828 response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 829 response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM; 830 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 831 } 832 833 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 834 } 835 836 static int 837 spdk_nvmf_ctrlr_set_features_arbitration(struct spdk_nvmf_request *req) 838 { 839 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 840 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 841 842 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Arbitration (cdw11 = 0x%0x)\n", cmd->cdw11); 843 844 ctrlr->feat.arbitration.raw = cmd->cdw11; 845 ctrlr->feat.arbitration.bits.reserved = 0; 846 847 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 848 } 849 850 static int 851 spdk_nvmf_ctrlr_set_features_power_management(struct spdk_nvmf_request *req) 852 { 853 union spdk_nvme_feat_power_management opts; 854 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 855 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 856 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 857 858 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Power Management (cdw11 = 0x%0x)\n", cmd->cdw11); 859 opts.raw = cmd->cdw11; 860 861 /* Only PS = 0 is allowed, since we report NPSS = 0 */ 862 if (opts.bits.ps != 0) { 863 SPDK_ERRLOG("Invalid power state %u\n", opts.bits.ps); 864 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 865 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 866 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 867 } 868 869 ctrlr->feat.power_management.raw = cmd->cdw11; 870 ctrlr->feat.power_management.bits.reserved = 0; 871 872 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 873 } 874 875 static bool 876 temp_threshold_opts_valid(const union spdk_nvme_feat_temperature_threshold *opts) 877 { 878 /* 879 * Valid TMPSEL values: 880 * 0000b - 1000b: temperature sensors 881 * 1111b: set all implemented temperature sensors 882 */ 883 if (opts->bits.tmpsel >= 9 && opts->bits.tmpsel != 15) { 884 /* 1001b - 1110b: reserved */ 885 SPDK_ERRLOG("Invalid TMPSEL %u\n", opts->bits.tmpsel); 886 return false; 887 } 888 889 /* 890 * Valid THSEL values: 891 * 00b: over temperature threshold 892 * 01b: under temperature threshold 893 */ 894 if (opts->bits.thsel > 1) { 895 /* 10b - 11b: reserved */ 896 SPDK_ERRLOG("Invalid THSEL %u\n", opts->bits.thsel); 897 return false; 898 } 899 900 return true; 901 } 902 903 static int 904 spdk_nvmf_ctrlr_set_features_temperature_threshold(struct spdk_nvmf_request *req) 905 { 906 union spdk_nvme_feat_temperature_threshold opts; 907 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 908 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 909 910 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Temperature Threshold (cdw11 = 0x%0x)\n", cmd->cdw11); 911 opts.raw = cmd->cdw11; 912 913 if (!temp_threshold_opts_valid(&opts)) { 914 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 915 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 916 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 917 } 918 919 /* TODO: no sensors implemented - ignore new values */ 920 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 921 } 922 923 static int 924 spdk_nvmf_ctrlr_get_features_temperature_threshold(struct spdk_nvmf_request *req) 925 { 926 union spdk_nvme_feat_temperature_threshold opts; 927 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 928 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 929 930 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Get Features - Temperature Threshold (cdw11 = 0x%0x)\n", cmd->cdw11); 931 opts.raw = cmd->cdw11; 932 933 if (!temp_threshold_opts_valid(&opts)) { 934 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 935 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 936 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 937 } 938 939 /* TODO: no sensors implemented - return 0 for all thresholds */ 940 rsp->cdw0 = 0; 941 942 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 943 } 944 945 static int 946 spdk_nvmf_ctrlr_set_features_error_recovery(struct spdk_nvmf_request *req) 947 { 948 union spdk_nvme_feat_error_recovery opts; 949 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 950 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 951 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 952 953 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Error Recovery (cdw11 = 0x%0x)\n", cmd->cdw11); 954 opts.raw = cmd->cdw11; 955 956 if (opts.bits.dulbe) { 957 /* 958 * Host is not allowed to set this bit, since we don't advertise it in 959 * Identify Namespace. 960 */ 961 SPDK_ERRLOG("Host set unsupported DULBE bit\n"); 962 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 963 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 964 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 965 } 966 967 ctrlr->feat.error_recovery.raw = cmd->cdw11; 968 ctrlr->feat.error_recovery.bits.reserved = 0; 969 970 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 971 } 972 973 static int 974 spdk_nvmf_ctrlr_set_features_volatile_write_cache(struct spdk_nvmf_request *req) 975 { 976 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 977 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 978 979 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Volatile Write Cache (cdw11 = 0x%0x)\n", cmd->cdw11); 980 981 ctrlr->feat.volatile_write_cache.raw = cmd->cdw11; 982 ctrlr->feat.volatile_write_cache.bits.reserved = 0; 983 984 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Volatile Write Cache %s\n", 985 ctrlr->feat.volatile_write_cache.bits.wce ? "Enabled" : "Disabled"); 986 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 987 } 988 989 static int 990 spdk_nvmf_ctrlr_set_features_write_atomicity(struct spdk_nvmf_request *req) 991 { 992 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 993 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 994 995 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Write Atomicity (cdw11 = 0x%0x)\n", cmd->cdw11); 996 997 ctrlr->feat.write_atomicity.raw = cmd->cdw11; 998 ctrlr->feat.write_atomicity.bits.reserved = 0; 999 1000 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1001 } 1002 1003 static int 1004 spdk_nvmf_ctrlr_set_features_host_identifier(struct spdk_nvmf_request *req) 1005 { 1006 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 1007 1008 SPDK_ERRLOG("Set Features - Host Identifier not allowed\n"); 1009 response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR; 1010 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1011 } 1012 1013 static int 1014 spdk_nvmf_ctrlr_get_features_host_identifier(struct spdk_nvmf_request *req) 1015 { 1016 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1017 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1018 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 1019 union spdk_nvme_feat_host_identifier opts; 1020 1021 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Get Features - Host Identifier\n"); 1022 1023 opts.raw = cmd->cdw11; 1024 if (!opts.bits.exhid) { 1025 /* NVMe over Fabrics requires EXHID=1 (128-bit/16-byte host ID) */ 1026 SPDK_ERRLOG("Get Features - Host Identifier with EXHID=0 not allowed\n"); 1027 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1028 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1029 } 1030 1031 if (req->data == NULL || req->length < sizeof(ctrlr->hostid)) { 1032 SPDK_ERRLOG("Invalid data buffer for Get Features - Host Identifier\n"); 1033 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1034 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1035 } 1036 1037 spdk_uuid_copy((struct spdk_uuid *)req->data, &ctrlr->hostid); 1038 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1039 } 1040 1041 static int 1042 spdk_nvmf_ctrlr_get_features_reservation_notification_mask(struct spdk_nvmf_request *req) 1043 { 1044 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1045 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1046 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 1047 struct spdk_nvmf_ns *ns; 1048 1049 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "get Features - Reservation Notificaton Mask\n"); 1050 1051 if (cmd->nsid == 0xffffffffu) { 1052 SPDK_ERRLOG("get Features - Invalid Namespace ID\n"); 1053 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1054 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1055 } 1056 1057 ns = _spdk_nvmf_subsystem_get_ns(ctrlr->subsys, cmd->nsid); 1058 if (ns == NULL) { 1059 SPDK_ERRLOG("Set Features - Invalid Namespace ID\n"); 1060 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1061 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1062 } 1063 rsp->cdw0 = ns->mask; 1064 1065 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1066 } 1067 1068 static int 1069 spdk_nvmf_ctrlr_set_features_reservation_notification_mask(struct spdk_nvmf_request *req) 1070 { 1071 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1072 struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys; 1073 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1074 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 1075 struct spdk_nvmf_ns *ns; 1076 1077 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Reservation Notificaton Mask\n"); 1078 1079 if (cmd->nsid == 0xffffffffu) { 1080 for (ns = spdk_nvmf_subsystem_get_first_ns(subsystem); ns != NULL; 1081 ns = spdk_nvmf_subsystem_get_next_ns(subsystem, ns)) { 1082 ns->mask = cmd->cdw11; 1083 } 1084 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1085 } 1086 1087 ns = _spdk_nvmf_subsystem_get_ns(ctrlr->subsys, cmd->nsid); 1088 if (ns == NULL) { 1089 SPDK_ERRLOG("Set Features - Invalid Namespace ID\n"); 1090 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1091 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1092 } 1093 ns->mask = cmd->cdw11; 1094 1095 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1096 } 1097 1098 static int 1099 spdk_nvmf_ctrlr_get_features_reservation_persistence(struct spdk_nvmf_request *req) 1100 { 1101 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1102 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1103 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 1104 struct spdk_nvmf_ns *ns; 1105 1106 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Get Features - Reservation Persistence\n"); 1107 1108 ns = _spdk_nvmf_subsystem_get_ns(ctrlr->subsys, cmd->nsid); 1109 /* NSID with 0xffffffffu also included */ 1110 if (ns == NULL) { 1111 SPDK_ERRLOG("Get Features - Invalid Namespace ID\n"); 1112 response->status.sct = SPDK_NVME_SCT_GENERIC; 1113 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1114 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1115 } 1116 1117 /* TODO: Persistence feature can't support for now */ 1118 response->cdw0 = 0; 1119 1120 response->status.sct = SPDK_NVME_SCT_GENERIC; 1121 response->status.sc = SPDK_NVME_SC_SUCCESS; 1122 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1123 } 1124 1125 static int 1126 spdk_nvmf_ctrlr_set_features_reservation_persistence(struct spdk_nvmf_request *req) 1127 { 1128 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1129 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1130 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 1131 struct spdk_nvmf_ns *ns; 1132 1133 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Reservation Persistence\n"); 1134 1135 ns = _spdk_nvmf_subsystem_get_ns(ctrlr->subsys, cmd->nsid); 1136 if (cmd->nsid != 0xffffffffu && ns == NULL) { 1137 SPDK_ERRLOG("Set Features - Invalid Namespace ID\n"); 1138 response->status.sct = SPDK_NVME_SCT_GENERIC; 1139 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1140 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1141 } 1142 1143 /* TODO: Feature not changeable for now */ 1144 response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 1145 response->status.sc = SPDK_NVME_SC_FEATURE_NOT_CHANGEABLE; 1146 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1147 } 1148 1149 static int 1150 spdk_nvmf_ctrlr_set_features_keep_alive_timer(struct spdk_nvmf_request *req) 1151 { 1152 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1153 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1154 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 1155 1156 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Keep Alive Timer (%u ms)\n", cmd->cdw11); 1157 1158 /* 1159 * if attempts to disable keep alive by setting kato to 0h 1160 * a status value of keep alive invalid shall be returned 1161 */ 1162 if (cmd->cdw11 == 0) { 1163 rsp->status.sc = SPDK_NVME_SC_KEEP_ALIVE_INVALID; 1164 } else if (cmd->cdw11 < MIN_KEEP_ALIVE_TIMEOUT_IN_MS) { 1165 ctrlr->feat.keep_alive_timer.bits.kato = MIN_KEEP_ALIVE_TIMEOUT_IN_MS; 1166 } else { 1167 /* round up to milliseconds */ 1168 ctrlr->feat.keep_alive_timer.bits.kato = spdk_divide_round_up(cmd->cdw11, 1169 KAS_DEFAULT_VALUE * KAS_TIME_UNIT_IN_MS) * 1170 KAS_DEFAULT_VALUE * KAS_TIME_UNIT_IN_MS; 1171 } 1172 1173 /* 1174 * if change the keep alive timeout value successfully 1175 * update the keep alive poller. 1176 */ 1177 if (cmd->cdw11 != 0) { 1178 if (ctrlr->keep_alive_poller != NULL) { 1179 spdk_poller_unregister(&ctrlr->keep_alive_poller); 1180 } 1181 ctrlr->keep_alive_poller = spdk_poller_register(spdk_nvmf_ctrlr_keep_alive_poll, ctrlr, 1182 ctrlr->feat.keep_alive_timer.bits.kato * 1000); 1183 } 1184 1185 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Keep Alive Timer set to %u ms\n", 1186 ctrlr->feat.keep_alive_timer.bits.kato); 1187 1188 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1189 } 1190 1191 static int 1192 spdk_nvmf_ctrlr_set_features_number_of_queues(struct spdk_nvmf_request *req) 1193 { 1194 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1195 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 1196 uint32_t count; 1197 1198 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Number of Queues, cdw11 0x%x\n", 1199 req->cmd->nvme_cmd.cdw11); 1200 1201 count = spdk_bit_array_count_set(ctrlr->qpair_mask); 1202 /* verify that the controller is ready to process commands */ 1203 if (count > 1) { 1204 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Queue pairs already active!\n"); 1205 rsp->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR; 1206 } else { 1207 /* 1208 * Ignore the value requested by the host - 1209 * always return the pre-configured value based on max_qpairs_allowed. 1210 */ 1211 rsp->cdw0 = ctrlr->feat.number_of_queues.raw; 1212 } 1213 1214 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1215 } 1216 1217 static int 1218 spdk_nvmf_ctrlr_set_features_async_event_configuration(struct spdk_nvmf_request *req) 1219 { 1220 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1221 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1222 1223 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Async Event Configuration, cdw11 0x%08x\n", 1224 cmd->cdw11); 1225 ctrlr->feat.async_event_configuration.raw = cmd->cdw11; 1226 ctrlr->feat.async_event_configuration.bits.reserved = 0; 1227 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1228 } 1229 1230 static int 1231 spdk_nvmf_ctrlr_async_event_request(struct spdk_nvmf_request *req) 1232 { 1233 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1234 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 1235 1236 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Async Event Request\n"); 1237 1238 /* Only one asynchronous event is supported for now */ 1239 if (ctrlr->aer_req != NULL) { 1240 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "AERL exceeded\n"); 1241 rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 1242 rsp->status.sc = SPDK_NVME_SC_ASYNC_EVENT_REQUEST_LIMIT_EXCEEDED; 1243 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1244 } 1245 1246 if (ctrlr->notice_event.bits.async_event_type == 1247 SPDK_NVME_ASYNC_EVENT_TYPE_NOTICE) { 1248 rsp->cdw0 = ctrlr->notice_event.raw; 1249 ctrlr->notice_event.raw = 0; 1250 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1251 } 1252 1253 if (ctrlr->reservation_event.bits.async_event_type == 1254 SPDK_NVME_ASYNC_EVENT_TYPE_IO) { 1255 rsp->cdw0 = ctrlr->reservation_event.raw; 1256 ctrlr->reservation_event.raw = 0; 1257 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1258 } 1259 1260 ctrlr->aer_req = req; 1261 return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS; 1262 } 1263 1264 static void 1265 spdk_nvmf_get_firmware_slot_log_page(void *buffer, uint64_t offset, uint32_t length) 1266 { 1267 struct spdk_nvme_firmware_page fw_page; 1268 size_t copy_len; 1269 1270 memset(&fw_page, 0, sizeof(fw_page)); 1271 fw_page.afi.active_slot = 1; 1272 fw_page.afi.next_reset_slot = 0; 1273 spdk_strcpy_pad(fw_page.revision[0], FW_VERSION, sizeof(fw_page.revision[0]), ' '); 1274 1275 if (offset < sizeof(fw_page)) { 1276 copy_len = spdk_min(sizeof(fw_page) - offset, length); 1277 if (copy_len > 0) { 1278 memcpy(buffer, (const char *)&fw_page + offset, copy_len); 1279 } 1280 } 1281 } 1282 1283 void 1284 spdk_nvmf_ctrlr_ns_changed(struct spdk_nvmf_ctrlr *ctrlr, uint32_t nsid) 1285 { 1286 uint16_t max_changes = SPDK_COUNTOF(ctrlr->changed_ns_list.ns_list); 1287 uint16_t i; 1288 bool found = false; 1289 1290 for (i = 0; i < ctrlr->changed_ns_list_count; i++) { 1291 if (ctrlr->changed_ns_list.ns_list[i] == nsid) { 1292 /* nsid is already in the list */ 1293 found = true; 1294 break; 1295 } 1296 } 1297 1298 if (!found) { 1299 if (ctrlr->changed_ns_list_count == max_changes) { 1300 /* Out of space - set first entry to FFFFFFFFh and zero-fill the rest. */ 1301 ctrlr->changed_ns_list.ns_list[0] = 0xFFFFFFFFu; 1302 for (i = 1; i < max_changes; i++) { 1303 ctrlr->changed_ns_list.ns_list[i] = 0; 1304 } 1305 } else { 1306 ctrlr->changed_ns_list.ns_list[ctrlr->changed_ns_list_count++] = nsid; 1307 } 1308 } 1309 1310 spdk_nvmf_ctrlr_async_event_ns_notice(ctrlr); 1311 } 1312 1313 static void 1314 spdk_nvmf_get_changed_ns_list_log_page(struct spdk_nvmf_ctrlr *ctrlr, 1315 void *buffer, uint64_t offset, uint32_t length) 1316 { 1317 size_t copy_length; 1318 1319 if (offset < sizeof(ctrlr->changed_ns_list)) { 1320 copy_length = spdk_min(length, sizeof(ctrlr->changed_ns_list) - offset); 1321 if (copy_length) { 1322 memcpy(buffer, (char *)&ctrlr->changed_ns_list + offset, copy_length); 1323 } 1324 } 1325 1326 /* Clear log page each time it is read */ 1327 ctrlr->changed_ns_list_count = 0; 1328 memset(&ctrlr->changed_ns_list, 0, sizeof(ctrlr->changed_ns_list)); 1329 } 1330 1331 /* The structure can be modified if we provide support for other commands in future */ 1332 static const struct spdk_nvme_cmds_and_effect_log_page g_cmds_and_effect_log_page = { 1333 .admin_cmds_supported = { 1334 /* CSUPP, LBCC, NCC, NIC, CCC, CSE */ 1335 /* Get Log Page */ 1336 [SPDK_NVME_OPC_GET_LOG_PAGE] = {1, 0, 0, 0, 0, 0, 0, 0}, 1337 /* Identify */ 1338 [SPDK_NVME_OPC_IDENTIFY] = {1, 0, 0, 0, 0, 0, 0, 0}, 1339 /* Abort */ 1340 [SPDK_NVME_OPC_ABORT] = {1, 0, 0, 0, 0, 0, 0, 0}, 1341 /* Set Features */ 1342 [SPDK_NVME_OPC_SET_FEATURES] = {1, 0, 0, 0, 0, 0, 0, 0}, 1343 /* Get Features */ 1344 [SPDK_NVME_OPC_GET_FEATURES] = {1, 0, 0, 0, 0, 0, 0, 0}, 1345 /* Async Event Request */ 1346 [SPDK_NVME_OPC_ASYNC_EVENT_REQUEST] = {1, 0, 0, 0, 0, 0, 0, 0}, 1347 /* Keep Alive */ 1348 [SPDK_NVME_OPC_KEEP_ALIVE] = {1, 0, 0, 0, 0, 0, 0, 0}, 1349 }, 1350 .io_cmds_supported = { 1351 /* FLUSH */ 1352 [SPDK_NVME_OPC_FLUSH] = {1, 1, 0, 0, 0, 0, 0, 0}, 1353 /* WRITE */ 1354 [SPDK_NVME_OPC_WRITE] = {1, 1, 0, 0, 0, 0, 0, 0}, 1355 /* READ */ 1356 [SPDK_NVME_OPC_READ] = {1, 0, 0, 0, 0, 0, 0, 0}, 1357 /* WRITE ZEROES */ 1358 [SPDK_NVME_OPC_WRITE_ZEROES] = {1, 1, 0, 0, 0, 0, 0, 0}, 1359 /* DATASET MANAGEMENT */ 1360 [SPDK_NVME_OPC_DATASET_MANAGEMENT] = {1, 1, 0, 0, 0, 0, 0, 0}, 1361 }, 1362 }; 1363 1364 static void 1365 spdk_nvmf_get_cmds_and_effects_log_page(void *buffer, 1366 uint64_t offset, uint32_t length) 1367 { 1368 uint32_t page_size = sizeof(struct spdk_nvme_cmds_and_effect_log_page); 1369 size_t copy_len = 0; 1370 size_t zero_len = length; 1371 1372 if (offset < page_size) { 1373 copy_len = spdk_min(page_size - offset, length); 1374 zero_len -= copy_len; 1375 memcpy(buffer, (char *)(&g_cmds_and_effect_log_page) + offset, copy_len); 1376 } 1377 1378 if (zero_len) { 1379 memset((char *)buffer + copy_len, 0, zero_len); 1380 } 1381 } 1382 1383 static void 1384 spdk_nvmf_get_reservation_notification_log_page(struct spdk_nvmf_ctrlr *ctrlr, 1385 void *data, uint64_t offset, uint32_t length) 1386 { 1387 uint32_t unit_log_len, avail_log_len, next_pos, copy_len; 1388 struct spdk_nvmf_reservation_log *log, *log_tmp; 1389 uint8_t *buf = data; 1390 1391 unit_log_len = sizeof(struct spdk_nvme_reservation_notification_log); 1392 /* No available log, return 1 zeroed log page */ 1393 if (!ctrlr->num_avail_log_pages) { 1394 memset(buf, 0, spdk_min(length, unit_log_len)); 1395 return; 1396 } 1397 1398 avail_log_len = ctrlr->num_avail_log_pages * unit_log_len; 1399 if (offset >= avail_log_len) { 1400 return; 1401 } 1402 1403 next_pos = copy_len = 0; 1404 TAILQ_FOREACH_SAFE(log, &ctrlr->log_head, link, log_tmp) { 1405 TAILQ_REMOVE(&ctrlr->log_head, log, link); 1406 ctrlr->num_avail_log_pages--; 1407 1408 next_pos += unit_log_len; 1409 if (next_pos > offset) { 1410 copy_len = spdk_min(next_pos - offset, length); 1411 memcpy(buf, &log->log, copy_len); 1412 length -= copy_len; 1413 offset += copy_len; 1414 buf += copy_len; 1415 } 1416 free(log); 1417 1418 if (length == 0) { 1419 break; 1420 } 1421 } 1422 return; 1423 } 1424 1425 static int 1426 spdk_nvmf_ctrlr_get_log_page(struct spdk_nvmf_request *req) 1427 { 1428 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1429 struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys; 1430 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1431 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 1432 uint64_t offset, len; 1433 uint32_t numdl, numdu; 1434 uint8_t lid; 1435 1436 if (req->data == NULL) { 1437 SPDK_ERRLOG("get log command with no buffer\n"); 1438 response->status.sct = SPDK_NVME_SCT_GENERIC; 1439 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1440 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1441 } 1442 1443 offset = (uint64_t)cmd->cdw12 | ((uint64_t)cmd->cdw13 << 32); 1444 if (offset & 3) { 1445 SPDK_ERRLOG("Invalid log page offset 0x%" PRIx64 "\n", offset); 1446 response->status.sct = SPDK_NVME_SCT_GENERIC; 1447 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1448 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1449 } 1450 1451 numdl = (cmd->cdw10 >> 16) & 0xFFFFu; 1452 numdu = (cmd->cdw11) & 0xFFFFu; 1453 len = ((numdu << 16) + numdl + (uint64_t)1) * 4; 1454 if (len > req->length) { 1455 SPDK_ERRLOG("Get log page: len (%" PRIu64 ") > buf size (%u)\n", 1456 len, req->length); 1457 response->status.sct = SPDK_NVME_SCT_GENERIC; 1458 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1459 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1460 } 1461 1462 lid = cmd->cdw10 & 0xFF; 1463 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Get log page: LID=0x%02X offset=0x%" PRIx64 " len=0x%" PRIx64 "\n", 1464 lid, offset, len); 1465 1466 if (subsystem->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) { 1467 switch (lid) { 1468 case SPDK_NVME_LOG_DISCOVERY: 1469 spdk_nvmf_get_discovery_log_page(subsystem->tgt, req->iov, req->iovcnt, offset, len); 1470 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1471 default: 1472 goto invalid_log_page; 1473 } 1474 } else { 1475 switch (lid) { 1476 case SPDK_NVME_LOG_ERROR: 1477 case SPDK_NVME_LOG_HEALTH_INFORMATION: 1478 /* TODO: actually fill out log page data */ 1479 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1480 case SPDK_NVME_LOG_FIRMWARE_SLOT: 1481 spdk_nvmf_get_firmware_slot_log_page(req->data, offset, len); 1482 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1483 case SPDK_NVME_LOG_COMMAND_EFFECTS_LOG: 1484 spdk_nvmf_get_cmds_and_effects_log_page(req->data, offset, len); 1485 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1486 case SPDK_NVME_LOG_CHANGED_NS_LIST: 1487 spdk_nvmf_get_changed_ns_list_log_page(ctrlr, req->data, offset, len); 1488 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1489 case SPDK_NVME_LOG_RESERVATION_NOTIFICATION: 1490 spdk_nvmf_get_reservation_notification_log_page(ctrlr, req->data, offset, len); 1491 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1492 default: 1493 goto invalid_log_page; 1494 } 1495 } 1496 1497 invalid_log_page: 1498 SPDK_ERRLOG("Unsupported Get Log Page 0x%02X\n", lid); 1499 response->status.sct = SPDK_NVME_SCT_GENERIC; 1500 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1501 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1502 } 1503 1504 static int 1505 spdk_nvmf_ctrlr_identify_ns(struct spdk_nvmf_ctrlr *ctrlr, 1506 struct spdk_nvme_cmd *cmd, 1507 struct spdk_nvme_cpl *rsp, 1508 struct spdk_nvme_ns_data *nsdata) 1509 { 1510 struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys; 1511 struct spdk_nvmf_ns *ns; 1512 uint32_t max_num_blocks; 1513 1514 if (cmd->nsid == 0 || cmd->nsid > subsystem->max_nsid) { 1515 SPDK_ERRLOG("Identify Namespace for invalid NSID %u\n", cmd->nsid); 1516 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 1517 rsp->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT; 1518 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1519 } 1520 1521 ns = _spdk_nvmf_subsystem_get_ns(subsystem, cmd->nsid); 1522 if (ns == NULL || ns->bdev == NULL) { 1523 /* 1524 * Inactive namespaces should return a zero filled data structure. 1525 * The data buffer is already zeroed by spdk_nvmf_ctrlr_process_admin_cmd(), 1526 * so we can just return early here. 1527 */ 1528 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Identify Namespace for inactive NSID %u\n", cmd->nsid); 1529 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 1530 rsp->status.sc = SPDK_NVME_SC_SUCCESS; 1531 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1532 } 1533 1534 spdk_nvmf_bdev_ctrlr_identify_ns(ns, nsdata); 1535 1536 /* Due to bug in the Linux kernel NVMe driver we have to set noiob no larger than mdts */ 1537 max_num_blocks = ctrlr->admin_qpair->transport->opts.max_io_size / 1538 (1U << nsdata->lbaf[nsdata->flbas.format].lbads); 1539 if (nsdata->noiob > max_num_blocks) { 1540 nsdata->noiob = max_num_blocks; 1541 } 1542 1543 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1544 } 1545 1546 static int 1547 spdk_nvmf_ctrlr_identify_ctrlr(struct spdk_nvmf_ctrlr *ctrlr, struct spdk_nvme_ctrlr_data *cdata) 1548 { 1549 struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys; 1550 struct spdk_nvmf_transport *transport = ctrlr->admin_qpair->transport; 1551 1552 /* 1553 * Common fields for discovery and NVM subsystems 1554 */ 1555 spdk_strcpy_pad(cdata->fr, FW_VERSION, sizeof(cdata->fr), ' '); 1556 assert((transport->opts.max_io_size % 4096) == 0); 1557 cdata->mdts = spdk_u32log2(transport->opts.max_io_size / 4096); 1558 cdata->cntlid = ctrlr->cntlid; 1559 cdata->ver = ctrlr->vcprop.vs; 1560 cdata->lpa.edlp = 1; 1561 cdata->elpe = 127; 1562 cdata->maxcmd = transport->opts.max_queue_depth; 1563 cdata->sgls.supported = 1; 1564 cdata->sgls.keyed_sgl = 1; 1565 cdata->sgls.sgl_offset = 1; 1566 spdk_strcpy_pad(cdata->subnqn, subsystem->subnqn, sizeof(cdata->subnqn), '\0'); 1567 1568 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ctrlr data: maxcmd 0x%x\n", cdata->maxcmd); 1569 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "sgls data: 0x%x\n", from_le32(&cdata->sgls)); 1570 1571 /* 1572 * NVM subsystem fields (reserved for discovery subsystems) 1573 */ 1574 if (subsystem->subtype == SPDK_NVMF_SUBTYPE_NVME) { 1575 spdk_strcpy_pad(cdata->mn, spdk_nvmf_subsystem_get_mn(subsystem), sizeof(cdata->mn), ' '); 1576 spdk_strcpy_pad(cdata->sn, spdk_nvmf_subsystem_get_sn(subsystem), sizeof(cdata->sn), ' '); 1577 cdata->kas = KAS_DEFAULT_VALUE; 1578 1579 cdata->rab = 6; 1580 cdata->cmic.multi_port = 1; 1581 cdata->cmic.multi_host = 1; 1582 cdata->oaes.ns_attribute_notices = 1; 1583 cdata->ctratt.host_id_exhid_supported = 1; 1584 cdata->aerl = 0; 1585 cdata->frmw.slot1_ro = 1; 1586 cdata->frmw.num_slots = 1; 1587 1588 cdata->lpa.celp = 1; /* Command Effects log page supported */ 1589 1590 cdata->sqes.min = 6; 1591 cdata->sqes.max = 6; 1592 cdata->cqes.min = 4; 1593 cdata->cqes.max = 4; 1594 cdata->nn = subsystem->max_nsid; 1595 cdata->vwc.present = 1; 1596 cdata->vwc.flush_broadcast = SPDK_NVME_FLUSH_BROADCAST_NOT_SUPPORTED; 1597 1598 cdata->nvmf_specific.ioccsz = sizeof(struct spdk_nvme_cmd) / 16; 1599 cdata->nvmf_specific.iorcsz = sizeof(struct spdk_nvme_cpl) / 16; 1600 cdata->nvmf_specific.icdoff = 0; /* offset starts directly after SQE */ 1601 cdata->nvmf_specific.ctrattr.ctrlr_model = SPDK_NVMF_CTRLR_MODEL_DYNAMIC; 1602 /* The RDMA transport supports up to SPDK_NVMF_MAX_SGL_ENTRIES descriptors. */ 1603 if (transport->ops->type == SPDK_NVME_TRANSPORT_RDMA) { 1604 cdata->nvmf_specific.msdbd = SPDK_NVMF_MAX_SGL_ENTRIES; 1605 } else { 1606 cdata->nvmf_specific.msdbd = 1; 1607 } 1608 1609 /* TODO: this should be set by the transport */ 1610 cdata->nvmf_specific.ioccsz += transport->opts.in_capsule_data_size / 16; 1611 1612 cdata->oncs.dsm = spdk_nvmf_ctrlr_dsm_supported(ctrlr); 1613 cdata->oncs.write_zeroes = spdk_nvmf_ctrlr_write_zeroes_supported(ctrlr); 1614 cdata->oncs.reservations = 1; 1615 1616 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ext ctrlr data: ioccsz 0x%x\n", 1617 cdata->nvmf_specific.ioccsz); 1618 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ext ctrlr data: iorcsz 0x%x\n", 1619 cdata->nvmf_specific.iorcsz); 1620 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ext ctrlr data: icdoff 0x%x\n", 1621 cdata->nvmf_specific.icdoff); 1622 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ext ctrlr data: ctrattr 0x%x\n", 1623 *(uint8_t *)&cdata->nvmf_specific.ctrattr); 1624 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ext ctrlr data: msdbd 0x%x\n", 1625 cdata->nvmf_specific.msdbd); 1626 } 1627 1628 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1629 } 1630 1631 static int 1632 spdk_nvmf_ctrlr_identify_active_ns_list(struct spdk_nvmf_subsystem *subsystem, 1633 struct spdk_nvme_cmd *cmd, 1634 struct spdk_nvme_cpl *rsp, 1635 struct spdk_nvme_ns_list *ns_list) 1636 { 1637 struct spdk_nvmf_ns *ns; 1638 uint32_t count = 0; 1639 1640 if (cmd->nsid >= 0xfffffffeUL) { 1641 SPDK_ERRLOG("Identify Active Namespace List with invalid NSID %u\n", cmd->nsid); 1642 rsp->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT; 1643 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1644 } 1645 1646 for (ns = spdk_nvmf_subsystem_get_first_ns(subsystem); ns != NULL; 1647 ns = spdk_nvmf_subsystem_get_next_ns(subsystem, ns)) { 1648 if (ns->opts.nsid <= cmd->nsid) { 1649 continue; 1650 } 1651 1652 ns_list->ns_list[count++] = ns->opts.nsid; 1653 if (count == SPDK_COUNTOF(ns_list->ns_list)) { 1654 break; 1655 } 1656 } 1657 1658 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1659 } 1660 1661 static void 1662 _add_ns_id_desc(void **buf_ptr, size_t *buf_remain, 1663 enum spdk_nvme_nidt type, 1664 const void *data, size_t data_size) 1665 { 1666 struct spdk_nvme_ns_id_desc *desc; 1667 size_t desc_size = sizeof(*desc) + data_size; 1668 1669 /* 1670 * These should never fail in practice, since all valid NS ID descriptors 1671 * should be defined so that they fit in the available 4096-byte buffer. 1672 */ 1673 assert(data_size > 0); 1674 assert(data_size <= UINT8_MAX); 1675 assert(desc_size < *buf_remain); 1676 if (data_size == 0 || data_size > UINT8_MAX || desc_size > *buf_remain) { 1677 return; 1678 } 1679 1680 desc = *buf_ptr; 1681 desc->nidt = type; 1682 desc->nidl = data_size; 1683 memcpy(desc->nid, data, data_size); 1684 1685 *buf_ptr += desc_size; 1686 *buf_remain -= desc_size; 1687 } 1688 1689 static int 1690 spdk_nvmf_ctrlr_identify_ns_id_descriptor_list( 1691 struct spdk_nvmf_subsystem *subsystem, 1692 struct spdk_nvme_cmd *cmd, 1693 struct spdk_nvme_cpl *rsp, 1694 void *id_desc_list, size_t id_desc_list_size) 1695 { 1696 struct spdk_nvmf_ns *ns; 1697 size_t buf_remain = id_desc_list_size; 1698 void *buf_ptr = id_desc_list; 1699 1700 ns = _spdk_nvmf_subsystem_get_ns(subsystem, cmd->nsid); 1701 if (ns == NULL || ns->bdev == NULL) { 1702 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 1703 rsp->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT; 1704 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1705 } 1706 1707 #define ADD_ID_DESC(type, data, size) \ 1708 do { \ 1709 if (!spdk_mem_all_zero(data, size)) { \ 1710 _add_ns_id_desc(&buf_ptr, &buf_remain, type, data, size); \ 1711 } \ 1712 } while (0) 1713 1714 ADD_ID_DESC(SPDK_NVME_NIDT_EUI64, ns->opts.eui64, sizeof(ns->opts.eui64)); 1715 ADD_ID_DESC(SPDK_NVME_NIDT_NGUID, ns->opts.nguid, sizeof(ns->opts.nguid)); 1716 ADD_ID_DESC(SPDK_NVME_NIDT_UUID, &ns->opts.uuid, sizeof(ns->opts.uuid)); 1717 1718 /* 1719 * The list is automatically 0-terminated because controller to host buffers in 1720 * admin commands always get zeroed in spdk_nvmf_ctrlr_process_admin_cmd(). 1721 */ 1722 1723 #undef ADD_ID_DESC 1724 1725 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1726 } 1727 1728 static int 1729 spdk_nvmf_ctrlr_identify(struct spdk_nvmf_request *req) 1730 { 1731 uint8_t cns; 1732 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1733 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1734 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 1735 struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys; 1736 1737 if (req->data == NULL || req->length < 4096) { 1738 SPDK_ERRLOG("identify command with invalid buffer\n"); 1739 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 1740 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1741 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1742 } 1743 1744 cns = cmd->cdw10 & 0xFF; 1745 1746 if (subsystem->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY && 1747 cns != SPDK_NVME_IDENTIFY_CTRLR) { 1748 /* Discovery controllers only support Identify Controller */ 1749 goto invalid_cns; 1750 } 1751 1752 switch (cns) { 1753 case SPDK_NVME_IDENTIFY_NS: 1754 return spdk_nvmf_ctrlr_identify_ns(ctrlr, cmd, rsp, req->data); 1755 case SPDK_NVME_IDENTIFY_CTRLR: 1756 return spdk_nvmf_ctrlr_identify_ctrlr(ctrlr, req->data); 1757 case SPDK_NVME_IDENTIFY_ACTIVE_NS_LIST: 1758 return spdk_nvmf_ctrlr_identify_active_ns_list(subsystem, cmd, rsp, req->data); 1759 case SPDK_NVME_IDENTIFY_NS_ID_DESCRIPTOR_LIST: 1760 return spdk_nvmf_ctrlr_identify_ns_id_descriptor_list(subsystem, cmd, rsp, req->data, req->length); 1761 default: 1762 goto invalid_cns; 1763 } 1764 1765 invalid_cns: 1766 SPDK_ERRLOG("Identify command with unsupported CNS 0x%02x\n", cns); 1767 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 1768 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1769 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1770 } 1771 1772 1773 static struct spdk_nvmf_request * 1774 spdk_nvmf_qpair_abort(struct spdk_nvmf_qpair *qpair, uint16_t cid) 1775 { 1776 struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr; 1777 struct spdk_nvmf_request *req; 1778 1779 if (spdk_nvmf_qpair_is_admin_queue(qpair)) { 1780 if (ctrlr->aer_req && ctrlr->aer_req->cmd->nvme_cmd.cid == cid) { 1781 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Aborting AER request\n"); 1782 req = ctrlr->aer_req; 1783 ctrlr->aer_req = NULL; 1784 return req; 1785 } 1786 } 1787 1788 /* TODO: track list of outstanding requests in qpair? */ 1789 return NULL; 1790 } 1791 1792 static void 1793 spdk_nvmf_ctrlr_abort_done(struct spdk_io_channel_iter *i, int status) 1794 { 1795 struct spdk_nvmf_request *req = spdk_io_channel_iter_get_ctx(i); 1796 1797 spdk_nvmf_request_complete(req); 1798 } 1799 1800 static void 1801 spdk_nvmf_ctrlr_abort_on_pg(struct spdk_io_channel_iter *i) 1802 { 1803 struct spdk_nvmf_request *req = spdk_io_channel_iter_get_ctx(i); 1804 struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i); 1805 struct spdk_nvmf_poll_group *group = spdk_io_channel_get_ctx(ch); 1806 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 1807 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1808 uint16_t sqid = cmd->cdw10 & 0xFFFFu; 1809 struct spdk_nvmf_qpair *qpair; 1810 1811 TAILQ_FOREACH(qpair, &group->qpairs, link) { 1812 if (qpair->ctrlr == req->qpair->ctrlr && qpair->qid == sqid) { 1813 struct spdk_nvmf_request *req_to_abort; 1814 uint16_t cid = cmd->cdw10 >> 16; 1815 1816 /* Found the qpair */ 1817 1818 req_to_abort = spdk_nvmf_qpair_abort(qpair, cid); 1819 if (req_to_abort == NULL) { 1820 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "cid %u not found\n", cid); 1821 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 1822 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1823 spdk_for_each_channel_continue(i, -EINVAL); 1824 return; 1825 } 1826 1827 /* Complete the request with aborted status */ 1828 req_to_abort->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC; 1829 req_to_abort->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_ABORTED_BY_REQUEST; 1830 spdk_nvmf_request_complete(req_to_abort); 1831 1832 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "abort ctrlr=%p req=%p sqid=%u cid=%u successful\n", 1833 qpair->ctrlr, req_to_abort, sqid, cid); 1834 rsp->cdw0 = 0; /* Command successfully aborted */ 1835 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 1836 rsp->status.sc = SPDK_NVME_SC_SUCCESS; 1837 /* Return -1 for the status so the iteration across threads stops. */ 1838 spdk_for_each_channel_continue(i, -1); 1839 1840 } 1841 } 1842 1843 spdk_for_each_channel_continue(i, 0); 1844 } 1845 1846 static int 1847 spdk_nvmf_ctrlr_abort(struct spdk_nvmf_request *req) 1848 { 1849 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 1850 1851 rsp->cdw0 = 1; /* Command not aborted */ 1852 rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 1853 rsp->status.sc = SPDK_NVME_SC_INVALID_QUEUE_IDENTIFIER; 1854 1855 /* Send a message to each poll group, searching for this ctrlr, sqid, and command. */ 1856 spdk_for_each_channel(req->qpair->ctrlr->subsys->tgt, 1857 spdk_nvmf_ctrlr_abort_on_pg, 1858 req, 1859 spdk_nvmf_ctrlr_abort_done 1860 ); 1861 1862 return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS; 1863 } 1864 1865 static int 1866 get_features_generic(struct spdk_nvmf_request *req, uint32_t cdw0) 1867 { 1868 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 1869 1870 rsp->cdw0 = cdw0; 1871 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1872 } 1873 1874 static int 1875 spdk_nvmf_ctrlr_get_features(struct spdk_nvmf_request *req) 1876 { 1877 uint8_t feature; 1878 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1879 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1880 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 1881 1882 feature = cmd->cdw10 & 0xff; /* mask out the FID value */ 1883 switch (feature) { 1884 case SPDK_NVME_FEAT_ARBITRATION: 1885 return get_features_generic(req, ctrlr->feat.arbitration.raw); 1886 case SPDK_NVME_FEAT_POWER_MANAGEMENT: 1887 return get_features_generic(req, ctrlr->feat.power_management.raw); 1888 case SPDK_NVME_FEAT_TEMPERATURE_THRESHOLD: 1889 return spdk_nvmf_ctrlr_get_features_temperature_threshold(req); 1890 case SPDK_NVME_FEAT_ERROR_RECOVERY: 1891 return get_features_generic(req, ctrlr->feat.error_recovery.raw); 1892 case SPDK_NVME_FEAT_VOLATILE_WRITE_CACHE: 1893 return get_features_generic(req, ctrlr->feat.volatile_write_cache.raw); 1894 case SPDK_NVME_FEAT_NUMBER_OF_QUEUES: 1895 return get_features_generic(req, ctrlr->feat.number_of_queues.raw); 1896 case SPDK_NVME_FEAT_WRITE_ATOMICITY: 1897 return get_features_generic(req, ctrlr->feat.write_atomicity.raw); 1898 case SPDK_NVME_FEAT_ASYNC_EVENT_CONFIGURATION: 1899 return get_features_generic(req, ctrlr->feat.async_event_configuration.raw); 1900 case SPDK_NVME_FEAT_KEEP_ALIVE_TIMER: 1901 return get_features_generic(req, ctrlr->feat.keep_alive_timer.raw); 1902 case SPDK_NVME_FEAT_HOST_IDENTIFIER: 1903 return spdk_nvmf_ctrlr_get_features_host_identifier(req); 1904 case SPDK_NVME_FEAT_HOST_RESERVE_MASK: 1905 return spdk_nvmf_ctrlr_get_features_reservation_notification_mask(req); 1906 case SPDK_NVME_FEAT_HOST_RESERVE_PERSIST: 1907 return spdk_nvmf_ctrlr_get_features_reservation_persistence(req); 1908 default: 1909 SPDK_ERRLOG("Get Features command with unsupported feature ID 0x%02x\n", feature); 1910 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1911 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1912 } 1913 } 1914 1915 static int 1916 spdk_nvmf_ctrlr_set_features(struct spdk_nvmf_request *req) 1917 { 1918 uint8_t feature; 1919 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1920 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 1921 1922 feature = cmd->cdw10 & 0xff; /* mask out the FID value */ 1923 switch (feature) { 1924 case SPDK_NVME_FEAT_ARBITRATION: 1925 return spdk_nvmf_ctrlr_set_features_arbitration(req); 1926 case SPDK_NVME_FEAT_POWER_MANAGEMENT: 1927 return spdk_nvmf_ctrlr_set_features_power_management(req); 1928 case SPDK_NVME_FEAT_TEMPERATURE_THRESHOLD: 1929 return spdk_nvmf_ctrlr_set_features_temperature_threshold(req); 1930 case SPDK_NVME_FEAT_ERROR_RECOVERY: 1931 return spdk_nvmf_ctrlr_set_features_error_recovery(req); 1932 case SPDK_NVME_FEAT_VOLATILE_WRITE_CACHE: 1933 return spdk_nvmf_ctrlr_set_features_volatile_write_cache(req); 1934 case SPDK_NVME_FEAT_NUMBER_OF_QUEUES: 1935 return spdk_nvmf_ctrlr_set_features_number_of_queues(req); 1936 case SPDK_NVME_FEAT_WRITE_ATOMICITY: 1937 return spdk_nvmf_ctrlr_set_features_write_atomicity(req); 1938 case SPDK_NVME_FEAT_ASYNC_EVENT_CONFIGURATION: 1939 return spdk_nvmf_ctrlr_set_features_async_event_configuration(req); 1940 case SPDK_NVME_FEAT_KEEP_ALIVE_TIMER: 1941 return spdk_nvmf_ctrlr_set_features_keep_alive_timer(req); 1942 case SPDK_NVME_FEAT_HOST_IDENTIFIER: 1943 return spdk_nvmf_ctrlr_set_features_host_identifier(req); 1944 case SPDK_NVME_FEAT_HOST_RESERVE_MASK: 1945 return spdk_nvmf_ctrlr_set_features_reservation_notification_mask(req); 1946 case SPDK_NVME_FEAT_HOST_RESERVE_PERSIST: 1947 return spdk_nvmf_ctrlr_set_features_reservation_persistence(req); 1948 default: 1949 SPDK_ERRLOG("Set Features command with unsupported feature ID 0x%02x\n", feature); 1950 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1951 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1952 } 1953 } 1954 1955 static int 1956 spdk_nvmf_ctrlr_keep_alive(struct spdk_nvmf_request *req) 1957 { 1958 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1959 1960 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Keep Alive\n"); 1961 /* 1962 * To handle keep alive just clear or reset the 1963 * ctrlr based keep alive duration counter. 1964 * When added, a separate timer based process 1965 * will monitor if the time since last recorded 1966 * keep alive has exceeded the max duration and 1967 * take appropriate action. 1968 */ 1969 ctrlr->last_keep_alive_tick = spdk_get_ticks(); 1970 1971 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1972 } 1973 1974 int 1975 spdk_nvmf_ctrlr_process_admin_cmd(struct spdk_nvmf_request *req) 1976 { 1977 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1978 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1979 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 1980 1981 if (ctrlr == NULL) { 1982 SPDK_ERRLOG("Admin command sent before CONNECT\n"); 1983 response->status.sct = SPDK_NVME_SCT_GENERIC; 1984 response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR; 1985 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1986 } 1987 1988 if (ctrlr->vcprop.cc.bits.en != 1) { 1989 SPDK_ERRLOG("Admin command sent to disabled controller\n"); 1990 response->status.sct = SPDK_NVME_SCT_GENERIC; 1991 response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR; 1992 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1993 } 1994 1995 if (req->data && spdk_nvme_opc_get_data_transfer(cmd->opc) == SPDK_NVME_DATA_CONTROLLER_TO_HOST) { 1996 memset(req->data, 0, req->length); 1997 } 1998 1999 if (ctrlr->subsys->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) { 2000 /* Discovery controllers only support Get Log Page and Identify */ 2001 switch (cmd->opc) { 2002 case SPDK_NVME_OPC_IDENTIFY: 2003 case SPDK_NVME_OPC_GET_LOG_PAGE: 2004 break; 2005 default: 2006 goto invalid_opcode; 2007 } 2008 } 2009 2010 switch (cmd->opc) { 2011 case SPDK_NVME_OPC_GET_LOG_PAGE: 2012 return spdk_nvmf_ctrlr_get_log_page(req); 2013 case SPDK_NVME_OPC_IDENTIFY: 2014 return spdk_nvmf_ctrlr_identify(req); 2015 case SPDK_NVME_OPC_ABORT: 2016 return spdk_nvmf_ctrlr_abort(req); 2017 case SPDK_NVME_OPC_GET_FEATURES: 2018 return spdk_nvmf_ctrlr_get_features(req); 2019 case SPDK_NVME_OPC_SET_FEATURES: 2020 return spdk_nvmf_ctrlr_set_features(req); 2021 case SPDK_NVME_OPC_ASYNC_EVENT_REQUEST: 2022 return spdk_nvmf_ctrlr_async_event_request(req); 2023 case SPDK_NVME_OPC_KEEP_ALIVE: 2024 return spdk_nvmf_ctrlr_keep_alive(req); 2025 2026 case SPDK_NVME_OPC_CREATE_IO_SQ: 2027 case SPDK_NVME_OPC_CREATE_IO_CQ: 2028 case SPDK_NVME_OPC_DELETE_IO_SQ: 2029 case SPDK_NVME_OPC_DELETE_IO_CQ: 2030 /* Create and Delete I/O CQ/SQ not allowed in NVMe-oF */ 2031 goto invalid_opcode; 2032 2033 default: 2034 goto invalid_opcode; 2035 } 2036 2037 invalid_opcode: 2038 SPDK_ERRLOG("Unsupported admin opcode 0x%x\n", cmd->opc); 2039 response->status.sct = SPDK_NVME_SCT_GENERIC; 2040 response->status.sc = SPDK_NVME_SC_INVALID_OPCODE; 2041 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2042 } 2043 2044 int 2045 spdk_nvmf_ctrlr_process_fabrics_cmd(struct spdk_nvmf_request *req) 2046 { 2047 struct spdk_nvmf_qpair *qpair = req->qpair; 2048 struct spdk_nvmf_capsule_cmd *cap_hdr; 2049 2050 cap_hdr = &req->cmd->nvmf_cmd; 2051 2052 if (qpair->ctrlr == NULL) { 2053 /* No ctrlr established yet; the only valid command is Connect */ 2054 if (cap_hdr->fctype == SPDK_NVMF_FABRIC_COMMAND_CONNECT) { 2055 return spdk_nvmf_ctrlr_connect(req); 2056 } else { 2057 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Got fctype 0x%x, expected Connect\n", 2058 cap_hdr->fctype); 2059 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC; 2060 req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR; 2061 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2062 } 2063 } else if (spdk_nvmf_qpair_is_admin_queue(qpair)) { 2064 /* 2065 * Controller session is established, and this is an admin queue. 2066 * Disallow Connect and allow other fabrics commands. 2067 */ 2068 switch (cap_hdr->fctype) { 2069 case SPDK_NVMF_FABRIC_COMMAND_PROPERTY_SET: 2070 return spdk_nvmf_property_set(req); 2071 case SPDK_NVMF_FABRIC_COMMAND_PROPERTY_GET: 2072 return spdk_nvmf_property_get(req); 2073 default: 2074 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "unknown fctype 0x%02x\n", 2075 cap_hdr->fctype); 2076 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC; 2077 req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_INVALID_OPCODE; 2078 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2079 } 2080 } else { 2081 /* Controller session is established, and this is an I/O queue */ 2082 /* For now, no I/O-specific Fabrics commands are implemented (other than Connect) */ 2083 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Unexpected I/O fctype 0x%x\n", cap_hdr->fctype); 2084 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC; 2085 req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_INVALID_OPCODE; 2086 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2087 } 2088 } 2089 2090 int 2091 spdk_nvmf_ctrlr_async_event_ns_notice(struct spdk_nvmf_ctrlr *ctrlr) 2092 { 2093 struct spdk_nvmf_request *req; 2094 struct spdk_nvme_cpl *rsp; 2095 union spdk_nvme_async_event_completion event = {0}; 2096 2097 /* Users may disable the event notification */ 2098 if (!ctrlr->feat.async_event_configuration.bits.ns_attr_notice) { 2099 return 0; 2100 } 2101 2102 event.bits.async_event_type = SPDK_NVME_ASYNC_EVENT_TYPE_NOTICE; 2103 event.bits.async_event_info = SPDK_NVME_ASYNC_EVENT_NS_ATTR_CHANGED; 2104 event.bits.log_page_identifier = SPDK_NVME_LOG_CHANGED_NS_LIST; 2105 2106 /* If there is no outstanding AER request, queue the event. Then 2107 * if an AER is later submitted, this event can be sent as a 2108 * response. 2109 */ 2110 if (!ctrlr->aer_req) { 2111 if (ctrlr->notice_event.bits.async_event_type == 2112 SPDK_NVME_ASYNC_EVENT_TYPE_NOTICE) { 2113 return 0; 2114 } 2115 2116 ctrlr->notice_event.raw = event.raw; 2117 return 0; 2118 } 2119 2120 req = ctrlr->aer_req; 2121 rsp = &req->rsp->nvme_cpl; 2122 2123 rsp->cdw0 = event.raw; 2124 2125 spdk_nvmf_request_complete(req); 2126 ctrlr->aer_req = NULL; 2127 2128 return 0; 2129 } 2130 2131 void 2132 spdk_nvmf_ctrlr_async_event_reservation_notification(struct spdk_nvmf_ctrlr *ctrlr) 2133 { 2134 struct spdk_nvmf_request *req; 2135 struct spdk_nvme_cpl *rsp; 2136 union spdk_nvme_async_event_completion event = {0}; 2137 2138 if (!ctrlr->num_avail_log_pages) { 2139 return; 2140 } 2141 event.bits.async_event_type = SPDK_NVME_ASYNC_EVENT_TYPE_IO; 2142 event.bits.async_event_info = SPDK_NVME_ASYNC_EVENT_RESERVATION_LOG_AVAIL; 2143 event.bits.log_page_identifier = SPDK_NVME_LOG_RESERVATION_NOTIFICATION; 2144 2145 /* If there is no outstanding AER request, queue the event. Then 2146 * if an AER is later submitted, this event can be sent as a 2147 * response. 2148 */ 2149 if (!ctrlr->aer_req) { 2150 if (ctrlr->reservation_event.bits.async_event_type == 2151 SPDK_NVME_ASYNC_EVENT_TYPE_IO) { 2152 return; 2153 } 2154 2155 ctrlr->reservation_event.raw = event.raw; 2156 return; 2157 } 2158 2159 req = ctrlr->aer_req; 2160 rsp = &req->rsp->nvme_cpl; 2161 2162 rsp->cdw0 = event.raw; 2163 2164 spdk_nvmf_request_complete(req); 2165 ctrlr->aer_req = NULL; 2166 } 2167 2168 void 2169 spdk_nvmf_qpair_free_aer(struct spdk_nvmf_qpair *qpair) 2170 { 2171 struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr; 2172 2173 if (!spdk_nvmf_qpair_is_admin_queue(qpair)) { 2174 return; 2175 } 2176 2177 if (ctrlr->aer_req != NULL) { 2178 spdk_nvmf_request_free(ctrlr->aer_req); 2179 ctrlr->aer_req = NULL; 2180 } 2181 } 2182 2183 void 2184 spdk_nvmf_ctrlr_abort_aer(struct spdk_nvmf_ctrlr *ctrlr) 2185 { 2186 if (!ctrlr->aer_req) { 2187 return; 2188 } 2189 2190 spdk_nvmf_request_complete(ctrlr->aer_req); 2191 ctrlr->aer_req = NULL; 2192 } 2193 2194 static void 2195 _nvmf_ctrlr_add_reservation_log(void *ctx) 2196 { 2197 struct spdk_nvmf_reservation_log *log = (struct spdk_nvmf_reservation_log *)ctx; 2198 struct spdk_nvmf_ctrlr *ctrlr = log->ctrlr; 2199 2200 ctrlr->log_page_count++; 2201 2202 /* Maximum number of queued log pages is 255 */ 2203 if (ctrlr->num_avail_log_pages == 0xff) { 2204 struct spdk_nvmf_reservation_log *entry; 2205 entry = TAILQ_LAST(&ctrlr->log_head, log_page_head); 2206 entry->log.log_page_count = ctrlr->log_page_count; 2207 free(log); 2208 return; 2209 } 2210 2211 log->log.log_page_count = ctrlr->log_page_count; 2212 log->log.num_avail_log_pages = ctrlr->num_avail_log_pages++; 2213 TAILQ_INSERT_TAIL(&ctrlr->log_head, log, link); 2214 2215 spdk_nvmf_ctrlr_async_event_reservation_notification(ctrlr); 2216 } 2217 2218 void 2219 spdk_nvmf_ctrlr_reservation_notice_log(struct spdk_nvmf_ctrlr *ctrlr, 2220 struct spdk_nvmf_ns *ns, 2221 enum spdk_nvme_reservation_notification_log_page_type type) 2222 { 2223 struct spdk_nvmf_reservation_log *log; 2224 2225 switch (type) { 2226 case SPDK_NVME_RESERVATION_LOG_PAGE_EMPTY: 2227 return; 2228 case SPDK_NVME_REGISTRATION_PREEMPTED: 2229 if (ns->mask & SPDK_NVME_REGISTRATION_PREEMPTED_MASK) { 2230 return; 2231 } 2232 break; 2233 case SPDK_NVME_RESERVATION_RELEASED: 2234 if (ns->mask & SPDK_NVME_RESERVATION_RELEASED_MASK) { 2235 return; 2236 } 2237 break; 2238 case SPDK_NVME_RESERVATION_PREEMPTED: 2239 if (ns->mask & SPDK_NVME_RESERVATION_PREEMPTED_MASK) { 2240 return; 2241 } 2242 break; 2243 default: 2244 return; 2245 } 2246 2247 log = calloc(1, sizeof(*log)); 2248 if (!log) { 2249 SPDK_ERRLOG("Alloc log page failed, ignore the log\n"); 2250 return; 2251 } 2252 log->ctrlr = ctrlr; 2253 log->log.type = type; 2254 log->log.nsid = ns->nsid; 2255 2256 spdk_thread_send_msg(ctrlr->thread, _nvmf_ctrlr_add_reservation_log, log); 2257 } 2258 2259 /* Check from subsystem poll group's namespace information data structure */ 2260 static bool 2261 nvmf_ns_info_ctrlr_is_registrant(struct spdk_nvmf_subsystem_pg_ns_info *ns_info, 2262 struct spdk_nvmf_ctrlr *ctrlr) 2263 { 2264 uint32_t i; 2265 2266 for (i = 0; i < SPDK_NVMF_MAX_NUM_REGISTRANTS; i++) { 2267 if (!spdk_uuid_compare(&ns_info->reg_hostid[i], &ctrlr->hostid)) { 2268 return true; 2269 } 2270 } 2271 2272 return false; 2273 } 2274 2275 /* 2276 * Check the NVMe command is permitted or not for current controller(Host). 2277 */ 2278 static int 2279 nvmf_ns_reservation_request_check(struct spdk_nvmf_subsystem_pg_ns_info *ns_info, 2280 struct spdk_nvmf_ctrlr *ctrlr, 2281 struct spdk_nvmf_request *req) 2282 { 2283 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 2284 enum spdk_nvme_reservation_type rtype = ns_info->rtype; 2285 uint8_t status = SPDK_NVME_SC_SUCCESS; 2286 uint8_t racqa; 2287 bool is_registrant; 2288 2289 /* No valid reservation */ 2290 if (!rtype) { 2291 return 0; 2292 } 2293 2294 is_registrant = nvmf_ns_info_ctrlr_is_registrant(ns_info, ctrlr); 2295 /* All registrants type and current ctrlr is a valid registrant */ 2296 if ((rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_ALL_REGS || 2297 rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_ALL_REGS) && is_registrant) { 2298 return 0; 2299 } else if (!spdk_uuid_compare(&ns_info->holder_id, &ctrlr->hostid)) { 2300 return 0; 2301 } 2302 2303 /* Non-holder for current controller */ 2304 switch (cmd->opc) { 2305 case SPDK_NVME_OPC_READ: 2306 case SPDK_NVME_OPC_COMPARE: 2307 if (rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS) { 2308 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 2309 goto exit; 2310 } 2311 if ((rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_REG_ONLY || 2312 rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_ALL_REGS) && !is_registrant) { 2313 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 2314 goto exit; 2315 } 2316 break; 2317 case SPDK_NVME_OPC_FLUSH: 2318 case SPDK_NVME_OPC_WRITE: 2319 case SPDK_NVME_OPC_WRITE_UNCORRECTABLE: 2320 case SPDK_NVME_OPC_WRITE_ZEROES: 2321 case SPDK_NVME_OPC_DATASET_MANAGEMENT: 2322 if (rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE || 2323 rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS) { 2324 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 2325 goto exit; 2326 } 2327 if (!is_registrant) { 2328 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 2329 goto exit; 2330 } 2331 break; 2332 case SPDK_NVME_OPC_RESERVATION_ACQUIRE: 2333 case SPDK_NVME_OPC_RESERVATION_RELEASE: 2334 racqa = cmd->cdw10 & 0x7u; 2335 if (cmd->opc == SPDK_NVME_OPC_RESERVATION_ACQUIRE && 2336 racqa == SPDK_NVME_RESERVE_ACQUIRE) { 2337 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 2338 goto exit; 2339 } 2340 if (!is_registrant) { 2341 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 2342 goto exit; 2343 } 2344 break; 2345 default: 2346 break; 2347 } 2348 2349 exit: 2350 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC; 2351 req->rsp->nvme_cpl.status.sc = status; 2352 if (status == SPDK_NVME_SC_RESERVATION_CONFLICT) { 2353 return -EPERM; 2354 } 2355 2356 return 0; 2357 } 2358 2359 int 2360 spdk_nvmf_ctrlr_process_io_cmd(struct spdk_nvmf_request *req) 2361 { 2362 uint32_t nsid; 2363 struct spdk_nvmf_ns *ns; 2364 struct spdk_bdev *bdev; 2365 struct spdk_bdev_desc *desc; 2366 struct spdk_io_channel *ch; 2367 struct spdk_nvmf_poll_group *group = req->qpair->group; 2368 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 2369 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 2370 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 2371 struct spdk_nvmf_subsystem_pg_ns_info *ns_info; 2372 2373 /* pre-set response details for this command */ 2374 response->status.sc = SPDK_NVME_SC_SUCCESS; 2375 nsid = cmd->nsid; 2376 2377 if (spdk_unlikely(ctrlr == NULL)) { 2378 SPDK_ERRLOG("I/O command sent before CONNECT\n"); 2379 response->status.sct = SPDK_NVME_SCT_GENERIC; 2380 response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR; 2381 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2382 } 2383 2384 if (spdk_unlikely(ctrlr->vcprop.cc.bits.en != 1)) { 2385 SPDK_ERRLOG("I/O command sent to disabled controller\n"); 2386 response->status.sct = SPDK_NVME_SCT_GENERIC; 2387 response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR; 2388 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2389 } 2390 2391 ns = _spdk_nvmf_subsystem_get_ns(ctrlr->subsys, nsid); 2392 if (ns == NULL || ns->bdev == NULL) { 2393 SPDK_ERRLOG("Unsuccessful query for nsid %u\n", cmd->nsid); 2394 response->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT; 2395 response->status.dnr = 1; 2396 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2397 } 2398 2399 ns_info = &group->sgroups[ctrlr->subsys->id].ns_info[nsid - 1]; 2400 if (nvmf_ns_reservation_request_check(ns_info, ctrlr, req)) { 2401 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Reservation Conflict for nsid %u, opcode %u\n", 2402 cmd->nsid, cmd->opc); 2403 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2404 } 2405 2406 bdev = ns->bdev; 2407 desc = ns->desc; 2408 ch = ns_info->channel; 2409 switch (cmd->opc) { 2410 case SPDK_NVME_OPC_READ: 2411 return spdk_nvmf_bdev_ctrlr_read_cmd(bdev, desc, ch, req); 2412 case SPDK_NVME_OPC_WRITE: 2413 return spdk_nvmf_bdev_ctrlr_write_cmd(bdev, desc, ch, req); 2414 case SPDK_NVME_OPC_WRITE_ZEROES: 2415 return spdk_nvmf_bdev_ctrlr_write_zeroes_cmd(bdev, desc, ch, req); 2416 case SPDK_NVME_OPC_FLUSH: 2417 return spdk_nvmf_bdev_ctrlr_flush_cmd(bdev, desc, ch, req); 2418 case SPDK_NVME_OPC_DATASET_MANAGEMENT: 2419 return spdk_nvmf_bdev_ctrlr_dsm_cmd(bdev, desc, ch, req); 2420 case SPDK_NVME_OPC_RESERVATION_REGISTER: 2421 case SPDK_NVME_OPC_RESERVATION_ACQUIRE: 2422 case SPDK_NVME_OPC_RESERVATION_RELEASE: 2423 case SPDK_NVME_OPC_RESERVATION_REPORT: 2424 spdk_thread_send_msg(ctrlr->subsys->thread, spdk_nvmf_ns_reservation_request, req); 2425 return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS; 2426 default: 2427 return spdk_nvmf_bdev_ctrlr_nvme_passthru_io(bdev, desc, ch, req); 2428 } 2429 } 2430 2431 static void 2432 spdk_nvmf_qpair_request_cleanup(struct spdk_nvmf_qpair *qpair) 2433 { 2434 if (qpair->state == SPDK_NVMF_QPAIR_DEACTIVATING) { 2435 assert(qpair->state_cb != NULL); 2436 2437 if (TAILQ_EMPTY(&qpair->outstanding)) { 2438 qpair->state_cb(qpair->state_cb_arg, 0); 2439 } 2440 } else { 2441 assert(qpair->state == SPDK_NVMF_QPAIR_ACTIVE); 2442 } 2443 } 2444 2445 int 2446 spdk_nvmf_request_free(struct spdk_nvmf_request *req) 2447 { 2448 struct spdk_nvmf_qpair *qpair = req->qpair; 2449 2450 TAILQ_REMOVE(&qpair->outstanding, req, link); 2451 if (spdk_nvmf_transport_req_free(req)) { 2452 SPDK_ERRLOG("Unable to free transport level request resources.\n"); 2453 } 2454 2455 spdk_nvmf_qpair_request_cleanup(qpair); 2456 2457 return 0; 2458 } 2459 2460 int 2461 spdk_nvmf_request_complete(struct spdk_nvmf_request *req) 2462 { 2463 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 2464 struct spdk_nvmf_qpair *qpair; 2465 2466 rsp->sqid = 0; 2467 rsp->status.p = 0; 2468 rsp->cid = req->cmd->nvme_cmd.cid; 2469 2470 qpair = req->qpair; 2471 2472 SPDK_DEBUGLOG(SPDK_LOG_NVMF, 2473 "cpl: cid=%u cdw0=0x%08x rsvd1=%u status=0x%04x\n", 2474 rsp->cid, rsp->cdw0, rsp->rsvd1, 2475 *(uint16_t *)&rsp->status); 2476 2477 TAILQ_REMOVE(&qpair->outstanding, req, link); 2478 if (spdk_nvmf_transport_req_complete(req)) { 2479 SPDK_ERRLOG("Transport request completion error!\n"); 2480 } 2481 2482 spdk_nvmf_qpair_request_cleanup(qpair); 2483 2484 return 0; 2485 } 2486 2487 static void 2488 nvmf_trace_command(union nvmf_h2c_msg *h2c_msg, bool is_admin_queue) 2489 { 2490 struct spdk_nvmf_capsule_cmd *cap_hdr = &h2c_msg->nvmf_cmd; 2491 struct spdk_nvme_cmd *cmd = &h2c_msg->nvme_cmd; 2492 struct spdk_nvme_sgl_descriptor *sgl = &cmd->dptr.sgl1; 2493 uint8_t opc; 2494 2495 if (cmd->opc == SPDK_NVME_OPC_FABRIC) { 2496 opc = cap_hdr->fctype; 2497 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "%s Fabrics cmd: fctype 0x%02x cid %u\n", 2498 is_admin_queue ? "Admin" : "I/O", 2499 cap_hdr->fctype, cap_hdr->cid); 2500 } else { 2501 opc = cmd->opc; 2502 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "%s cmd: opc 0x%02x fuse %u cid %u nsid %u cdw10 0x%08x\n", 2503 is_admin_queue ? "Admin" : "I/O", 2504 cmd->opc, cmd->fuse, cmd->cid, cmd->nsid, cmd->cdw10); 2505 if (cmd->mptr) { 2506 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "mptr 0x%" PRIx64 "\n", cmd->mptr); 2507 } 2508 if (cmd->psdt != SPDK_NVME_PSDT_SGL_MPTR_CONTIG && 2509 cmd->psdt != SPDK_NVME_PSDT_SGL_MPTR_SGL) { 2510 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "psdt %u\n", cmd->psdt); 2511 } 2512 } 2513 2514 if (spdk_nvme_opc_get_data_transfer(opc) != SPDK_NVME_DATA_NONE) { 2515 if (sgl->generic.type == SPDK_NVME_SGL_TYPE_KEYED_DATA_BLOCK) { 2516 SPDK_DEBUGLOG(SPDK_LOG_NVMF, 2517 "SGL: Keyed%s: addr 0x%" PRIx64 " key 0x%x len 0x%x\n", 2518 sgl->generic.subtype == SPDK_NVME_SGL_SUBTYPE_INVALIDATE_KEY ? " (Inv)" : "", 2519 sgl->address, sgl->keyed.key, sgl->keyed.length); 2520 } else if (sgl->generic.type == SPDK_NVME_SGL_TYPE_DATA_BLOCK) { 2521 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "SGL: Data block: %s 0x%" PRIx64 " len 0x%x\n", 2522 sgl->unkeyed.subtype == SPDK_NVME_SGL_SUBTYPE_OFFSET ? "offs" : "addr", 2523 sgl->address, sgl->unkeyed.length); 2524 } else { 2525 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "SGL type 0x%x subtype 0x%x\n", 2526 sgl->generic.type, sgl->generic.subtype); 2527 } 2528 } 2529 } 2530 2531 void 2532 spdk_nvmf_request_exec(struct spdk_nvmf_request *req) 2533 { 2534 struct spdk_nvmf_qpair *qpair = req->qpair; 2535 spdk_nvmf_request_exec_status status; 2536 2537 nvmf_trace_command(req->cmd, spdk_nvmf_qpair_is_admin_queue(qpair)); 2538 2539 if (qpair->state != SPDK_NVMF_QPAIR_ACTIVE) { 2540 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC; 2541 req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR; 2542 /* Place the request on the outstanding list so we can keep track of it */ 2543 TAILQ_INSERT_TAIL(&qpair->outstanding, req, link); 2544 spdk_nvmf_request_complete(req); 2545 return; 2546 } 2547 2548 /* Check if the subsystem is paused (if there is a subsystem) */ 2549 if (qpair->ctrlr) { 2550 struct spdk_nvmf_subsystem_poll_group *sgroup = &qpair->group->sgroups[qpair->ctrlr->subsys->id]; 2551 if (sgroup->state != SPDK_NVMF_SUBSYSTEM_ACTIVE) { 2552 /* The subsystem is not currently active. Queue this request. */ 2553 TAILQ_INSERT_TAIL(&sgroup->queued, req, link); 2554 return; 2555 } 2556 2557 } 2558 2559 /* Place the request on the outstanding list so we can keep track of it */ 2560 TAILQ_INSERT_TAIL(&qpair->outstanding, req, link); 2561 2562 if (spdk_unlikely(req->cmd->nvmf_cmd.opcode == SPDK_NVME_OPC_FABRIC)) { 2563 status = spdk_nvmf_ctrlr_process_fabrics_cmd(req); 2564 } else if (spdk_unlikely(spdk_nvmf_qpair_is_admin_queue(qpair))) { 2565 status = spdk_nvmf_ctrlr_process_admin_cmd(req); 2566 } else { 2567 status = spdk_nvmf_ctrlr_process_io_cmd(req); 2568 } 2569 2570 if (status == SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE) { 2571 spdk_nvmf_request_complete(req); 2572 } 2573 } 2574