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