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