1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * NVMe over Fabrics common host code. 4 * Copyright (c) 2015-2016 HGST, a Western Digital Company. 5 */ 6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 7 #include <linux/init.h> 8 #include <linux/miscdevice.h> 9 #include <linux/module.h> 10 #include <linux/mutex.h> 11 #include <linux/parser.h> 12 #include <linux/seq_file.h> 13 #include "nvme.h" 14 #include "fabrics.h" 15 16 static LIST_HEAD(nvmf_transports); 17 static DECLARE_RWSEM(nvmf_transports_rwsem); 18 19 static LIST_HEAD(nvmf_hosts); 20 static DEFINE_MUTEX(nvmf_hosts_mutex); 21 22 static struct nvmf_host *nvmf_default_host; 23 24 static struct nvmf_host *__nvmf_host_find(const char *hostnqn) 25 { 26 struct nvmf_host *host; 27 28 list_for_each_entry(host, &nvmf_hosts, list) { 29 if (!strcmp(host->nqn, hostnqn)) 30 return host; 31 } 32 33 return NULL; 34 } 35 36 static struct nvmf_host *nvmf_host_add(const char *hostnqn) 37 { 38 struct nvmf_host *host; 39 40 mutex_lock(&nvmf_hosts_mutex); 41 host = __nvmf_host_find(hostnqn); 42 if (host) { 43 kref_get(&host->ref); 44 goto out_unlock; 45 } 46 47 host = kmalloc(sizeof(*host), GFP_KERNEL); 48 if (!host) 49 goto out_unlock; 50 51 kref_init(&host->ref); 52 strlcpy(host->nqn, hostnqn, NVMF_NQN_SIZE); 53 54 list_add_tail(&host->list, &nvmf_hosts); 55 out_unlock: 56 mutex_unlock(&nvmf_hosts_mutex); 57 return host; 58 } 59 60 static struct nvmf_host *nvmf_host_default(void) 61 { 62 struct nvmf_host *host; 63 64 host = kmalloc(sizeof(*host), GFP_KERNEL); 65 if (!host) 66 return NULL; 67 68 kref_init(&host->ref); 69 uuid_gen(&host->id); 70 snprintf(host->nqn, NVMF_NQN_SIZE, 71 "nqn.2014-08.org.nvmexpress:uuid:%pUb", &host->id); 72 73 mutex_lock(&nvmf_hosts_mutex); 74 list_add_tail(&host->list, &nvmf_hosts); 75 mutex_unlock(&nvmf_hosts_mutex); 76 77 return host; 78 } 79 80 static void nvmf_host_destroy(struct kref *ref) 81 { 82 struct nvmf_host *host = container_of(ref, struct nvmf_host, ref); 83 84 mutex_lock(&nvmf_hosts_mutex); 85 list_del(&host->list); 86 mutex_unlock(&nvmf_hosts_mutex); 87 88 kfree(host); 89 } 90 91 static void nvmf_host_put(struct nvmf_host *host) 92 { 93 if (host) 94 kref_put(&host->ref, nvmf_host_destroy); 95 } 96 97 /** 98 * nvmf_get_address() - Get address/port 99 * @ctrl: Host NVMe controller instance which we got the address 100 * @buf: OUTPUT parameter that will contain the address/port 101 * @size: buffer size 102 */ 103 int nvmf_get_address(struct nvme_ctrl *ctrl, char *buf, int size) 104 { 105 int len = 0; 106 107 if (ctrl->opts->mask & NVMF_OPT_TRADDR) 108 len += scnprintf(buf, size, "traddr=%s", ctrl->opts->traddr); 109 if (ctrl->opts->mask & NVMF_OPT_TRSVCID) 110 len += scnprintf(buf + len, size - len, "%strsvcid=%s", 111 (len) ? "," : "", ctrl->opts->trsvcid); 112 if (ctrl->opts->mask & NVMF_OPT_HOST_TRADDR) 113 len += scnprintf(buf + len, size - len, "%shost_traddr=%s", 114 (len) ? "," : "", ctrl->opts->host_traddr); 115 if (ctrl->opts->mask & NVMF_OPT_HOST_IFACE) 116 len += scnprintf(buf + len, size - len, "%shost_iface=%s", 117 (len) ? "," : "", ctrl->opts->host_iface); 118 len += scnprintf(buf + len, size - len, "\n"); 119 120 return len; 121 } 122 EXPORT_SYMBOL_GPL(nvmf_get_address); 123 124 /** 125 * nvmf_reg_read32() - NVMe Fabrics "Property Get" API function. 126 * @ctrl: Host NVMe controller instance maintaining the admin 127 * queue used to submit the property read command to 128 * the allocated NVMe controller resource on the target system. 129 * @off: Starting offset value of the targeted property 130 * register (see the fabrics section of the NVMe standard). 131 * @val: OUTPUT parameter that will contain the value of 132 * the property after a successful read. 133 * 134 * Used by the host system to retrieve a 32-bit capsule property value 135 * from an NVMe controller on the target system. 136 * 137 * ("Capsule property" is an "PCIe register concept" applied to the 138 * NVMe fabrics space.) 139 * 140 * Return: 141 * 0: successful read 142 * > 0: NVMe error status code 143 * < 0: Linux errno error code 144 */ 145 int nvmf_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val) 146 { 147 struct nvme_command cmd = { }; 148 union nvme_result res; 149 int ret; 150 151 cmd.prop_get.opcode = nvme_fabrics_command; 152 cmd.prop_get.fctype = nvme_fabrics_type_property_get; 153 cmd.prop_get.offset = cpu_to_le32(off); 154 155 ret = __nvme_submit_sync_cmd(ctrl->fabrics_q, &cmd, &res, NULL, 0, 156 NVME_QID_ANY, 0, 0); 157 158 if (ret >= 0) 159 *val = le64_to_cpu(res.u64); 160 if (unlikely(ret != 0)) 161 dev_err(ctrl->device, 162 "Property Get error: %d, offset %#x\n", 163 ret > 0 ? ret & ~NVME_SC_DNR : ret, off); 164 165 return ret; 166 } 167 EXPORT_SYMBOL_GPL(nvmf_reg_read32); 168 169 /** 170 * nvmf_reg_read64() - NVMe Fabrics "Property Get" API function. 171 * @ctrl: Host NVMe controller instance maintaining the admin 172 * queue used to submit the property read command to 173 * the allocated controller resource on the target system. 174 * @off: Starting offset value of the targeted property 175 * register (see the fabrics section of the NVMe standard). 176 * @val: OUTPUT parameter that will contain the value of 177 * the property after a successful read. 178 * 179 * Used by the host system to retrieve a 64-bit capsule property value 180 * from an NVMe controller on the target system. 181 * 182 * ("Capsule property" is an "PCIe register concept" applied to the 183 * NVMe fabrics space.) 184 * 185 * Return: 186 * 0: successful read 187 * > 0: NVMe error status code 188 * < 0: Linux errno error code 189 */ 190 int nvmf_reg_read64(struct nvme_ctrl *ctrl, u32 off, u64 *val) 191 { 192 struct nvme_command cmd = { }; 193 union nvme_result res; 194 int ret; 195 196 cmd.prop_get.opcode = nvme_fabrics_command; 197 cmd.prop_get.fctype = nvme_fabrics_type_property_get; 198 cmd.prop_get.attrib = 1; 199 cmd.prop_get.offset = cpu_to_le32(off); 200 201 ret = __nvme_submit_sync_cmd(ctrl->fabrics_q, &cmd, &res, NULL, 0, 202 NVME_QID_ANY, 0, 0); 203 204 if (ret >= 0) 205 *val = le64_to_cpu(res.u64); 206 if (unlikely(ret != 0)) 207 dev_err(ctrl->device, 208 "Property Get error: %d, offset %#x\n", 209 ret > 0 ? ret & ~NVME_SC_DNR : ret, off); 210 return ret; 211 } 212 EXPORT_SYMBOL_GPL(nvmf_reg_read64); 213 214 /** 215 * nvmf_reg_write32() - NVMe Fabrics "Property Write" API function. 216 * @ctrl: Host NVMe controller instance maintaining the admin 217 * queue used to submit the property read command to 218 * the allocated NVMe controller resource on the target system. 219 * @off: Starting offset value of the targeted property 220 * register (see the fabrics section of the NVMe standard). 221 * @val: Input parameter that contains the value to be 222 * written to the property. 223 * 224 * Used by the NVMe host system to write a 32-bit capsule property value 225 * to an NVMe controller on the target system. 226 * 227 * ("Capsule property" is an "PCIe register concept" applied to the 228 * NVMe fabrics space.) 229 * 230 * Return: 231 * 0: successful write 232 * > 0: NVMe error status code 233 * < 0: Linux errno error code 234 */ 235 int nvmf_reg_write32(struct nvme_ctrl *ctrl, u32 off, u32 val) 236 { 237 struct nvme_command cmd = { }; 238 int ret; 239 240 cmd.prop_set.opcode = nvme_fabrics_command; 241 cmd.prop_set.fctype = nvme_fabrics_type_property_set; 242 cmd.prop_set.attrib = 0; 243 cmd.prop_set.offset = cpu_to_le32(off); 244 cmd.prop_set.value = cpu_to_le64(val); 245 246 ret = __nvme_submit_sync_cmd(ctrl->fabrics_q, &cmd, NULL, NULL, 0, 247 NVME_QID_ANY, 0, 0); 248 if (unlikely(ret)) 249 dev_err(ctrl->device, 250 "Property Set error: %d, offset %#x\n", 251 ret > 0 ? ret & ~NVME_SC_DNR : ret, off); 252 return ret; 253 } 254 EXPORT_SYMBOL_GPL(nvmf_reg_write32); 255 256 /** 257 * nvmf_log_connect_error() - Error-parsing-diagnostic print out function for 258 * connect() errors. 259 * @ctrl: The specific /dev/nvmeX device that had the error. 260 * @errval: Error code to be decoded in a more human-friendly 261 * printout. 262 * @offset: For use with the NVMe error code 263 * NVME_SC_CONNECT_INVALID_PARAM. 264 * @cmd: This is the SQE portion of a submission capsule. 265 * @data: This is the "Data" portion of a submission capsule. 266 */ 267 static void nvmf_log_connect_error(struct nvme_ctrl *ctrl, 268 int errval, int offset, struct nvme_command *cmd, 269 struct nvmf_connect_data *data) 270 { 271 int err_sctype = errval & ~NVME_SC_DNR; 272 273 switch (err_sctype) { 274 case NVME_SC_CONNECT_INVALID_PARAM: 275 if (offset >> 16) { 276 char *inv_data = "Connect Invalid Data Parameter"; 277 278 switch (offset & 0xffff) { 279 case (offsetof(struct nvmf_connect_data, cntlid)): 280 dev_err(ctrl->device, 281 "%s, cntlid: %d\n", 282 inv_data, data->cntlid); 283 break; 284 case (offsetof(struct nvmf_connect_data, hostnqn)): 285 dev_err(ctrl->device, 286 "%s, hostnqn \"%s\"\n", 287 inv_data, data->hostnqn); 288 break; 289 case (offsetof(struct nvmf_connect_data, subsysnqn)): 290 dev_err(ctrl->device, 291 "%s, subsysnqn \"%s\"\n", 292 inv_data, data->subsysnqn); 293 break; 294 default: 295 dev_err(ctrl->device, 296 "%s, starting byte offset: %d\n", 297 inv_data, offset & 0xffff); 298 break; 299 } 300 } else { 301 char *inv_sqe = "Connect Invalid SQE Parameter"; 302 303 switch (offset) { 304 case (offsetof(struct nvmf_connect_command, qid)): 305 dev_err(ctrl->device, 306 "%s, qid %d\n", 307 inv_sqe, cmd->connect.qid); 308 break; 309 default: 310 dev_err(ctrl->device, 311 "%s, starting byte offset: %d\n", 312 inv_sqe, offset); 313 } 314 } 315 break; 316 case NVME_SC_CONNECT_INVALID_HOST: 317 dev_err(ctrl->device, 318 "Connect for subsystem %s is not allowed, hostnqn: %s\n", 319 data->subsysnqn, data->hostnqn); 320 break; 321 case NVME_SC_CONNECT_CTRL_BUSY: 322 dev_err(ctrl->device, 323 "Connect command failed: controller is busy or not available\n"); 324 break; 325 case NVME_SC_CONNECT_FORMAT: 326 dev_err(ctrl->device, 327 "Connect incompatible format: %d", 328 cmd->connect.recfmt); 329 break; 330 case NVME_SC_HOST_PATH_ERROR: 331 dev_err(ctrl->device, 332 "Connect command failed: host path error\n"); 333 break; 334 case NVME_SC_AUTH_REQUIRED: 335 dev_err(ctrl->device, 336 "Connect command failed: authentication required\n"); 337 break; 338 default: 339 dev_err(ctrl->device, 340 "Connect command failed, error wo/DNR bit: %d\n", 341 err_sctype); 342 break; 343 } 344 } 345 346 /** 347 * nvmf_connect_admin_queue() - NVMe Fabrics Admin Queue "Connect" 348 * API function. 349 * @ctrl: Host nvme controller instance used to request 350 * a new NVMe controller allocation on the target 351 * system and establish an NVMe Admin connection to 352 * that controller. 353 * 354 * This function enables an NVMe host device to request a new allocation of 355 * an NVMe controller resource on a target system as well establish a 356 * fabrics-protocol connection of the NVMe Admin queue between the 357 * host system device and the allocated NVMe controller on the 358 * target system via a NVMe Fabrics "Connect" command. 359 * 360 * Return: 361 * 0: success 362 * > 0: NVMe error status code 363 * < 0: Linux errno error code 364 * 365 */ 366 int nvmf_connect_admin_queue(struct nvme_ctrl *ctrl) 367 { 368 struct nvme_command cmd = { }; 369 union nvme_result res; 370 struct nvmf_connect_data *data; 371 int ret; 372 u32 result; 373 374 cmd.connect.opcode = nvme_fabrics_command; 375 cmd.connect.fctype = nvme_fabrics_type_connect; 376 cmd.connect.qid = 0; 377 cmd.connect.sqsize = cpu_to_le16(NVME_AQ_DEPTH - 1); 378 379 /* 380 * Set keep-alive timeout in seconds granularity (ms * 1000) 381 */ 382 cmd.connect.kato = cpu_to_le32(ctrl->kato * 1000); 383 384 if (ctrl->opts->disable_sqflow) 385 cmd.connect.cattr |= NVME_CONNECT_DISABLE_SQFLOW; 386 387 data = kzalloc(sizeof(*data), GFP_KERNEL); 388 if (!data) 389 return -ENOMEM; 390 391 uuid_copy(&data->hostid, &ctrl->opts->host->id); 392 data->cntlid = cpu_to_le16(0xffff); 393 strncpy(data->subsysnqn, ctrl->opts->subsysnqn, NVMF_NQN_SIZE); 394 strncpy(data->hostnqn, ctrl->opts->host->nqn, NVMF_NQN_SIZE); 395 396 ret = __nvme_submit_sync_cmd(ctrl->fabrics_q, &cmd, &res, 397 data, sizeof(*data), NVME_QID_ANY, 1, 398 BLK_MQ_REQ_RESERVED | BLK_MQ_REQ_NOWAIT); 399 if (ret) { 400 nvmf_log_connect_error(ctrl, ret, le32_to_cpu(res.u32), 401 &cmd, data); 402 goto out_free_data; 403 } 404 405 result = le32_to_cpu(res.u32); 406 ctrl->cntlid = result & 0xFFFF; 407 if ((result >> 16) & 0x3) { 408 /* Authentication required */ 409 ret = nvme_auth_negotiate(ctrl, 0); 410 if (ret) { 411 dev_warn(ctrl->device, 412 "qid 0: authentication setup failed\n"); 413 ret = NVME_SC_AUTH_REQUIRED; 414 goto out_free_data; 415 } 416 ret = nvme_auth_wait(ctrl, 0); 417 if (ret) 418 dev_warn(ctrl->device, 419 "qid 0: authentication failed\n"); 420 else 421 dev_info(ctrl->device, 422 "qid 0: authenticated\n"); 423 } 424 out_free_data: 425 kfree(data); 426 return ret; 427 } 428 EXPORT_SYMBOL_GPL(nvmf_connect_admin_queue); 429 430 /** 431 * nvmf_connect_io_queue() - NVMe Fabrics I/O Queue "Connect" 432 * API function. 433 * @ctrl: Host nvme controller instance used to establish an 434 * NVMe I/O queue connection to the already allocated NVMe 435 * controller on the target system. 436 * @qid: NVMe I/O queue number for the new I/O connection between 437 * host and target (note qid == 0 is illegal as this is 438 * the Admin queue, per NVMe standard). 439 * 440 * This function issues a fabrics-protocol connection 441 * of a NVMe I/O queue (via NVMe Fabrics "Connect" command) 442 * between the host system device and the allocated NVMe controller 443 * on the target system. 444 * 445 * Return: 446 * 0: success 447 * > 0: NVMe error status code 448 * < 0: Linux errno error code 449 */ 450 int nvmf_connect_io_queue(struct nvme_ctrl *ctrl, u16 qid) 451 { 452 struct nvme_command cmd = { }; 453 struct nvmf_connect_data *data; 454 union nvme_result res; 455 int ret; 456 u32 result; 457 458 cmd.connect.opcode = nvme_fabrics_command; 459 cmd.connect.fctype = nvme_fabrics_type_connect; 460 cmd.connect.qid = cpu_to_le16(qid); 461 cmd.connect.sqsize = cpu_to_le16(ctrl->sqsize); 462 463 if (ctrl->opts->disable_sqflow) 464 cmd.connect.cattr |= NVME_CONNECT_DISABLE_SQFLOW; 465 466 data = kzalloc(sizeof(*data), GFP_KERNEL); 467 if (!data) 468 return -ENOMEM; 469 470 uuid_copy(&data->hostid, &ctrl->opts->host->id); 471 data->cntlid = cpu_to_le16(ctrl->cntlid); 472 strncpy(data->subsysnqn, ctrl->opts->subsysnqn, NVMF_NQN_SIZE); 473 strncpy(data->hostnqn, ctrl->opts->host->nqn, NVMF_NQN_SIZE); 474 475 ret = __nvme_submit_sync_cmd(ctrl->connect_q, &cmd, &res, 476 data, sizeof(*data), qid, 1, 477 BLK_MQ_REQ_RESERVED | BLK_MQ_REQ_NOWAIT); 478 if (ret) { 479 nvmf_log_connect_error(ctrl, ret, le32_to_cpu(res.u32), 480 &cmd, data); 481 } 482 result = le32_to_cpu(res.u32); 483 if ((result >> 16) & 2) { 484 /* Authentication required */ 485 ret = nvme_auth_negotiate(ctrl, qid); 486 if (ret) { 487 dev_warn(ctrl->device, 488 "qid %d: authentication setup failed\n", qid); 489 ret = NVME_SC_AUTH_REQUIRED; 490 } else { 491 ret = nvme_auth_wait(ctrl, qid); 492 if (ret) 493 dev_warn(ctrl->device, 494 "qid %u: authentication failed\n", qid); 495 } 496 } 497 kfree(data); 498 return ret; 499 } 500 EXPORT_SYMBOL_GPL(nvmf_connect_io_queue); 501 502 bool nvmf_should_reconnect(struct nvme_ctrl *ctrl) 503 { 504 if (ctrl->opts->max_reconnects == -1 || 505 ctrl->nr_reconnects < ctrl->opts->max_reconnects) 506 return true; 507 508 return false; 509 } 510 EXPORT_SYMBOL_GPL(nvmf_should_reconnect); 511 512 /** 513 * nvmf_register_transport() - NVMe Fabrics Library registration function. 514 * @ops: Transport ops instance to be registered to the 515 * common fabrics library. 516 * 517 * API function that registers the type of specific transport fabric 518 * being implemented to the common NVMe fabrics library. Part of 519 * the overall init sequence of starting up a fabrics driver. 520 */ 521 int nvmf_register_transport(struct nvmf_transport_ops *ops) 522 { 523 if (!ops->create_ctrl) 524 return -EINVAL; 525 526 down_write(&nvmf_transports_rwsem); 527 list_add_tail(&ops->entry, &nvmf_transports); 528 up_write(&nvmf_transports_rwsem); 529 530 return 0; 531 } 532 EXPORT_SYMBOL_GPL(nvmf_register_transport); 533 534 /** 535 * nvmf_unregister_transport() - NVMe Fabrics Library unregistration function. 536 * @ops: Transport ops instance to be unregistered from the 537 * common fabrics library. 538 * 539 * Fabrics API function that unregisters the type of specific transport 540 * fabric being implemented from the common NVMe fabrics library. 541 * Part of the overall exit sequence of unloading the implemented driver. 542 */ 543 void nvmf_unregister_transport(struct nvmf_transport_ops *ops) 544 { 545 down_write(&nvmf_transports_rwsem); 546 list_del(&ops->entry); 547 up_write(&nvmf_transports_rwsem); 548 } 549 EXPORT_SYMBOL_GPL(nvmf_unregister_transport); 550 551 static struct nvmf_transport_ops *nvmf_lookup_transport( 552 struct nvmf_ctrl_options *opts) 553 { 554 struct nvmf_transport_ops *ops; 555 556 lockdep_assert_held(&nvmf_transports_rwsem); 557 558 list_for_each_entry(ops, &nvmf_transports, entry) { 559 if (strcmp(ops->name, opts->transport) == 0) 560 return ops; 561 } 562 563 return NULL; 564 } 565 566 static const match_table_t opt_tokens = { 567 { NVMF_OPT_TRANSPORT, "transport=%s" }, 568 { NVMF_OPT_TRADDR, "traddr=%s" }, 569 { NVMF_OPT_TRSVCID, "trsvcid=%s" }, 570 { NVMF_OPT_NQN, "nqn=%s" }, 571 { NVMF_OPT_QUEUE_SIZE, "queue_size=%d" }, 572 { NVMF_OPT_NR_IO_QUEUES, "nr_io_queues=%d" }, 573 { NVMF_OPT_RECONNECT_DELAY, "reconnect_delay=%d" }, 574 { NVMF_OPT_CTRL_LOSS_TMO, "ctrl_loss_tmo=%d" }, 575 { NVMF_OPT_KATO, "keep_alive_tmo=%d" }, 576 { NVMF_OPT_HOSTNQN, "hostnqn=%s" }, 577 { NVMF_OPT_HOST_TRADDR, "host_traddr=%s" }, 578 { NVMF_OPT_HOST_IFACE, "host_iface=%s" }, 579 { NVMF_OPT_HOST_ID, "hostid=%s" }, 580 { NVMF_OPT_DUP_CONNECT, "duplicate_connect" }, 581 { NVMF_OPT_DISABLE_SQFLOW, "disable_sqflow" }, 582 { NVMF_OPT_HDR_DIGEST, "hdr_digest" }, 583 { NVMF_OPT_DATA_DIGEST, "data_digest" }, 584 { NVMF_OPT_NR_WRITE_QUEUES, "nr_write_queues=%d" }, 585 { NVMF_OPT_NR_POLL_QUEUES, "nr_poll_queues=%d" }, 586 { NVMF_OPT_TOS, "tos=%d" }, 587 { NVMF_OPT_FAIL_FAST_TMO, "fast_io_fail_tmo=%d" }, 588 { NVMF_OPT_DISCOVERY, "discovery" }, 589 { NVMF_OPT_DHCHAP_SECRET, "dhchap_secret=%s" }, 590 { NVMF_OPT_DHCHAP_CTRL_SECRET, "dhchap_ctrl_secret=%s" }, 591 { NVMF_OPT_ERR, NULL } 592 }; 593 594 static int nvmf_parse_options(struct nvmf_ctrl_options *opts, 595 const char *buf) 596 { 597 substring_t args[MAX_OPT_ARGS]; 598 char *options, *o, *p; 599 int token, ret = 0; 600 size_t nqnlen = 0; 601 int ctrl_loss_tmo = NVMF_DEF_CTRL_LOSS_TMO; 602 uuid_t hostid; 603 604 /* Set defaults */ 605 opts->queue_size = NVMF_DEF_QUEUE_SIZE; 606 opts->nr_io_queues = num_online_cpus(); 607 opts->reconnect_delay = NVMF_DEF_RECONNECT_DELAY; 608 opts->kato = 0; 609 opts->duplicate_connect = false; 610 opts->fast_io_fail_tmo = NVMF_DEF_FAIL_FAST_TMO; 611 opts->hdr_digest = false; 612 opts->data_digest = false; 613 opts->tos = -1; /* < 0 == use transport default */ 614 615 options = o = kstrdup(buf, GFP_KERNEL); 616 if (!options) 617 return -ENOMEM; 618 619 uuid_gen(&hostid); 620 621 while ((p = strsep(&o, ",\n")) != NULL) { 622 if (!*p) 623 continue; 624 625 token = match_token(p, opt_tokens, args); 626 opts->mask |= token; 627 switch (token) { 628 case NVMF_OPT_TRANSPORT: 629 p = match_strdup(args); 630 if (!p) { 631 ret = -ENOMEM; 632 goto out; 633 } 634 kfree(opts->transport); 635 opts->transport = p; 636 break; 637 case NVMF_OPT_NQN: 638 p = match_strdup(args); 639 if (!p) { 640 ret = -ENOMEM; 641 goto out; 642 } 643 kfree(opts->subsysnqn); 644 opts->subsysnqn = p; 645 nqnlen = strlen(opts->subsysnqn); 646 if (nqnlen >= NVMF_NQN_SIZE) { 647 pr_err("%s needs to be < %d bytes\n", 648 opts->subsysnqn, NVMF_NQN_SIZE); 649 ret = -EINVAL; 650 goto out; 651 } 652 opts->discovery_nqn = 653 !(strcmp(opts->subsysnqn, 654 NVME_DISC_SUBSYS_NAME)); 655 break; 656 case NVMF_OPT_TRADDR: 657 p = match_strdup(args); 658 if (!p) { 659 ret = -ENOMEM; 660 goto out; 661 } 662 kfree(opts->traddr); 663 opts->traddr = p; 664 break; 665 case NVMF_OPT_TRSVCID: 666 p = match_strdup(args); 667 if (!p) { 668 ret = -ENOMEM; 669 goto out; 670 } 671 kfree(opts->trsvcid); 672 opts->trsvcid = p; 673 break; 674 case NVMF_OPT_QUEUE_SIZE: 675 if (match_int(args, &token)) { 676 ret = -EINVAL; 677 goto out; 678 } 679 if (token < NVMF_MIN_QUEUE_SIZE || 680 token > NVMF_MAX_QUEUE_SIZE) { 681 pr_err("Invalid queue_size %d\n", token); 682 ret = -EINVAL; 683 goto out; 684 } 685 opts->queue_size = token; 686 break; 687 case NVMF_OPT_NR_IO_QUEUES: 688 if (match_int(args, &token)) { 689 ret = -EINVAL; 690 goto out; 691 } 692 if (token <= 0) { 693 pr_err("Invalid number of IOQs %d\n", token); 694 ret = -EINVAL; 695 goto out; 696 } 697 if (opts->discovery_nqn) { 698 pr_debug("Ignoring nr_io_queues value for discovery controller\n"); 699 break; 700 } 701 702 opts->nr_io_queues = min_t(unsigned int, 703 num_online_cpus(), token); 704 break; 705 case NVMF_OPT_KATO: 706 if (match_int(args, &token)) { 707 ret = -EINVAL; 708 goto out; 709 } 710 711 if (token < 0) { 712 pr_err("Invalid keep_alive_tmo %d\n", token); 713 ret = -EINVAL; 714 goto out; 715 } else if (token == 0 && !opts->discovery_nqn) { 716 /* Allowed for debug */ 717 pr_warn("keep_alive_tmo 0 won't execute keep alives!!!\n"); 718 } 719 opts->kato = token; 720 break; 721 case NVMF_OPT_CTRL_LOSS_TMO: 722 if (match_int(args, &token)) { 723 ret = -EINVAL; 724 goto out; 725 } 726 727 if (token < 0) 728 pr_warn("ctrl_loss_tmo < 0 will reconnect forever\n"); 729 ctrl_loss_tmo = token; 730 break; 731 case NVMF_OPT_FAIL_FAST_TMO: 732 if (match_int(args, &token)) { 733 ret = -EINVAL; 734 goto out; 735 } 736 737 if (token >= 0) 738 pr_warn("I/O fail on reconnect controller after %d sec\n", 739 token); 740 else 741 token = -1; 742 743 opts->fast_io_fail_tmo = token; 744 break; 745 case NVMF_OPT_HOSTNQN: 746 if (opts->host) { 747 pr_err("hostnqn already user-assigned: %s\n", 748 opts->host->nqn); 749 ret = -EADDRINUSE; 750 goto out; 751 } 752 p = match_strdup(args); 753 if (!p) { 754 ret = -ENOMEM; 755 goto out; 756 } 757 nqnlen = strlen(p); 758 if (nqnlen >= NVMF_NQN_SIZE) { 759 pr_err("%s needs to be < %d bytes\n", 760 p, NVMF_NQN_SIZE); 761 kfree(p); 762 ret = -EINVAL; 763 goto out; 764 } 765 opts->host = nvmf_host_add(p); 766 kfree(p); 767 if (!opts->host) { 768 ret = -ENOMEM; 769 goto out; 770 } 771 break; 772 case NVMF_OPT_RECONNECT_DELAY: 773 if (match_int(args, &token)) { 774 ret = -EINVAL; 775 goto out; 776 } 777 if (token <= 0) { 778 pr_err("Invalid reconnect_delay %d\n", token); 779 ret = -EINVAL; 780 goto out; 781 } 782 opts->reconnect_delay = token; 783 break; 784 case NVMF_OPT_HOST_TRADDR: 785 p = match_strdup(args); 786 if (!p) { 787 ret = -ENOMEM; 788 goto out; 789 } 790 kfree(opts->host_traddr); 791 opts->host_traddr = p; 792 break; 793 case NVMF_OPT_HOST_IFACE: 794 p = match_strdup(args); 795 if (!p) { 796 ret = -ENOMEM; 797 goto out; 798 } 799 kfree(opts->host_iface); 800 opts->host_iface = p; 801 break; 802 case NVMF_OPT_HOST_ID: 803 p = match_strdup(args); 804 if (!p) { 805 ret = -ENOMEM; 806 goto out; 807 } 808 ret = uuid_parse(p, &hostid); 809 if (ret) { 810 pr_err("Invalid hostid %s\n", p); 811 ret = -EINVAL; 812 kfree(p); 813 goto out; 814 } 815 kfree(p); 816 break; 817 case NVMF_OPT_DUP_CONNECT: 818 opts->duplicate_connect = true; 819 break; 820 case NVMF_OPT_DISABLE_SQFLOW: 821 opts->disable_sqflow = true; 822 break; 823 case NVMF_OPT_HDR_DIGEST: 824 opts->hdr_digest = true; 825 break; 826 case NVMF_OPT_DATA_DIGEST: 827 opts->data_digest = true; 828 break; 829 case NVMF_OPT_NR_WRITE_QUEUES: 830 if (match_int(args, &token)) { 831 ret = -EINVAL; 832 goto out; 833 } 834 if (token <= 0) { 835 pr_err("Invalid nr_write_queues %d\n", token); 836 ret = -EINVAL; 837 goto out; 838 } 839 opts->nr_write_queues = token; 840 break; 841 case NVMF_OPT_NR_POLL_QUEUES: 842 if (match_int(args, &token)) { 843 ret = -EINVAL; 844 goto out; 845 } 846 if (token <= 0) { 847 pr_err("Invalid nr_poll_queues %d\n", token); 848 ret = -EINVAL; 849 goto out; 850 } 851 opts->nr_poll_queues = token; 852 break; 853 case NVMF_OPT_TOS: 854 if (match_int(args, &token)) { 855 ret = -EINVAL; 856 goto out; 857 } 858 if (token < 0) { 859 pr_err("Invalid type of service %d\n", token); 860 ret = -EINVAL; 861 goto out; 862 } 863 if (token > 255) { 864 pr_warn("Clamping type of service to 255\n"); 865 token = 255; 866 } 867 opts->tos = token; 868 break; 869 case NVMF_OPT_DISCOVERY: 870 opts->discovery_nqn = true; 871 break; 872 case NVMF_OPT_DHCHAP_SECRET: 873 p = match_strdup(args); 874 if (!p) { 875 ret = -ENOMEM; 876 goto out; 877 } 878 if (strlen(p) < 11 || strncmp(p, "DHHC-1:", 7)) { 879 pr_err("Invalid DH-CHAP secret %s\n", p); 880 ret = -EINVAL; 881 goto out; 882 } 883 kfree(opts->dhchap_secret); 884 opts->dhchap_secret = p; 885 break; 886 case NVMF_OPT_DHCHAP_CTRL_SECRET: 887 p = match_strdup(args); 888 if (!p) { 889 ret = -ENOMEM; 890 goto out; 891 } 892 if (strlen(p) < 11 || strncmp(p, "DHHC-1:", 7)) { 893 pr_err("Invalid DH-CHAP secret %s\n", p); 894 ret = -EINVAL; 895 goto out; 896 } 897 kfree(opts->dhchap_ctrl_secret); 898 opts->dhchap_ctrl_secret = p; 899 break; 900 default: 901 pr_warn("unknown parameter or missing value '%s' in ctrl creation request\n", 902 p); 903 ret = -EINVAL; 904 goto out; 905 } 906 } 907 908 if (opts->discovery_nqn) { 909 opts->nr_io_queues = 0; 910 opts->nr_write_queues = 0; 911 opts->nr_poll_queues = 0; 912 opts->duplicate_connect = true; 913 } else { 914 if (!opts->kato) 915 opts->kato = NVME_DEFAULT_KATO; 916 } 917 if (ctrl_loss_tmo < 0) { 918 opts->max_reconnects = -1; 919 } else { 920 opts->max_reconnects = DIV_ROUND_UP(ctrl_loss_tmo, 921 opts->reconnect_delay); 922 if (ctrl_loss_tmo < opts->fast_io_fail_tmo) 923 pr_warn("failfast tmo (%d) larger than controller loss tmo (%d)\n", 924 opts->fast_io_fail_tmo, ctrl_loss_tmo); 925 } 926 927 if (!opts->host) { 928 kref_get(&nvmf_default_host->ref); 929 opts->host = nvmf_default_host; 930 } 931 932 uuid_copy(&opts->host->id, &hostid); 933 934 out: 935 kfree(options); 936 return ret; 937 } 938 939 static int nvmf_check_required_opts(struct nvmf_ctrl_options *opts, 940 unsigned int required_opts) 941 { 942 if ((opts->mask & required_opts) != required_opts) { 943 unsigned int i; 944 945 for (i = 0; i < ARRAY_SIZE(opt_tokens); i++) { 946 if ((opt_tokens[i].token & required_opts) && 947 !(opt_tokens[i].token & opts->mask)) { 948 pr_warn("missing parameter '%s'\n", 949 opt_tokens[i].pattern); 950 } 951 } 952 953 return -EINVAL; 954 } 955 956 return 0; 957 } 958 959 bool nvmf_ip_options_match(struct nvme_ctrl *ctrl, 960 struct nvmf_ctrl_options *opts) 961 { 962 if (!nvmf_ctlr_matches_baseopts(ctrl, opts) || 963 strcmp(opts->traddr, ctrl->opts->traddr) || 964 strcmp(opts->trsvcid, ctrl->opts->trsvcid)) 965 return false; 966 967 /* 968 * Checking the local address is rough. In most cases, none is specified 969 * and the host port is selected by the stack. 970 * 971 * Assume no match if: 972 * - local address is specified and address is not the same 973 * - local address is not specified but remote is, or vice versa 974 * (admin using specific host_traddr when it matters). 975 */ 976 if ((opts->mask & NVMF_OPT_HOST_TRADDR) && 977 (ctrl->opts->mask & NVMF_OPT_HOST_TRADDR)) { 978 if (strcmp(opts->host_traddr, ctrl->opts->host_traddr)) 979 return false; 980 } else if ((opts->mask & NVMF_OPT_HOST_TRADDR) || 981 (ctrl->opts->mask & NVMF_OPT_HOST_TRADDR)) { 982 return false; 983 } 984 985 return true; 986 } 987 EXPORT_SYMBOL_GPL(nvmf_ip_options_match); 988 989 static int nvmf_check_allowed_opts(struct nvmf_ctrl_options *opts, 990 unsigned int allowed_opts) 991 { 992 if (opts->mask & ~allowed_opts) { 993 unsigned int i; 994 995 for (i = 0; i < ARRAY_SIZE(opt_tokens); i++) { 996 if ((opt_tokens[i].token & opts->mask) && 997 (opt_tokens[i].token & ~allowed_opts)) { 998 pr_warn("invalid parameter '%s'\n", 999 opt_tokens[i].pattern); 1000 } 1001 } 1002 1003 return -EINVAL; 1004 } 1005 1006 return 0; 1007 } 1008 1009 void nvmf_free_options(struct nvmf_ctrl_options *opts) 1010 { 1011 nvmf_host_put(opts->host); 1012 kfree(opts->transport); 1013 kfree(opts->traddr); 1014 kfree(opts->trsvcid); 1015 kfree(opts->subsysnqn); 1016 kfree(opts->host_traddr); 1017 kfree(opts->host_iface); 1018 kfree(opts->dhchap_secret); 1019 kfree(opts->dhchap_ctrl_secret); 1020 kfree(opts); 1021 } 1022 EXPORT_SYMBOL_GPL(nvmf_free_options); 1023 1024 #define NVMF_REQUIRED_OPTS (NVMF_OPT_TRANSPORT | NVMF_OPT_NQN) 1025 #define NVMF_ALLOWED_OPTS (NVMF_OPT_QUEUE_SIZE | NVMF_OPT_NR_IO_QUEUES | \ 1026 NVMF_OPT_KATO | NVMF_OPT_HOSTNQN | \ 1027 NVMF_OPT_HOST_ID | NVMF_OPT_DUP_CONNECT |\ 1028 NVMF_OPT_DISABLE_SQFLOW | NVMF_OPT_DISCOVERY |\ 1029 NVMF_OPT_FAIL_FAST_TMO | NVMF_OPT_DHCHAP_SECRET |\ 1030 NVMF_OPT_DHCHAP_CTRL_SECRET) 1031 1032 static struct nvme_ctrl * 1033 nvmf_create_ctrl(struct device *dev, const char *buf) 1034 { 1035 struct nvmf_ctrl_options *opts; 1036 struct nvmf_transport_ops *ops; 1037 struct nvme_ctrl *ctrl; 1038 int ret; 1039 1040 opts = kzalloc(sizeof(*opts), GFP_KERNEL); 1041 if (!opts) 1042 return ERR_PTR(-ENOMEM); 1043 1044 ret = nvmf_parse_options(opts, buf); 1045 if (ret) 1046 goto out_free_opts; 1047 1048 1049 request_module("nvme-%s", opts->transport); 1050 1051 /* 1052 * Check the generic options first as we need a valid transport for 1053 * the lookup below. Then clear the generic flags so that transport 1054 * drivers don't have to care about them. 1055 */ 1056 ret = nvmf_check_required_opts(opts, NVMF_REQUIRED_OPTS); 1057 if (ret) 1058 goto out_free_opts; 1059 opts->mask &= ~NVMF_REQUIRED_OPTS; 1060 1061 down_read(&nvmf_transports_rwsem); 1062 ops = nvmf_lookup_transport(opts); 1063 if (!ops) { 1064 pr_info("no handler found for transport %s.\n", 1065 opts->transport); 1066 ret = -EINVAL; 1067 goto out_unlock; 1068 } 1069 1070 if (!try_module_get(ops->module)) { 1071 ret = -EBUSY; 1072 goto out_unlock; 1073 } 1074 up_read(&nvmf_transports_rwsem); 1075 1076 ret = nvmf_check_required_opts(opts, ops->required_opts); 1077 if (ret) 1078 goto out_module_put; 1079 ret = nvmf_check_allowed_opts(opts, NVMF_ALLOWED_OPTS | 1080 ops->allowed_opts | ops->required_opts); 1081 if (ret) 1082 goto out_module_put; 1083 1084 ctrl = ops->create_ctrl(dev, opts); 1085 if (IS_ERR(ctrl)) { 1086 ret = PTR_ERR(ctrl); 1087 goto out_module_put; 1088 } 1089 1090 module_put(ops->module); 1091 return ctrl; 1092 1093 out_module_put: 1094 module_put(ops->module); 1095 goto out_free_opts; 1096 out_unlock: 1097 up_read(&nvmf_transports_rwsem); 1098 out_free_opts: 1099 nvmf_free_options(opts); 1100 return ERR_PTR(ret); 1101 } 1102 1103 static struct class *nvmf_class; 1104 static struct device *nvmf_device; 1105 static DEFINE_MUTEX(nvmf_dev_mutex); 1106 1107 static ssize_t nvmf_dev_write(struct file *file, const char __user *ubuf, 1108 size_t count, loff_t *pos) 1109 { 1110 struct seq_file *seq_file = file->private_data; 1111 struct nvme_ctrl *ctrl; 1112 const char *buf; 1113 int ret = 0; 1114 1115 if (count > PAGE_SIZE) 1116 return -ENOMEM; 1117 1118 buf = memdup_user_nul(ubuf, count); 1119 if (IS_ERR(buf)) 1120 return PTR_ERR(buf); 1121 1122 mutex_lock(&nvmf_dev_mutex); 1123 if (seq_file->private) { 1124 ret = -EINVAL; 1125 goto out_unlock; 1126 } 1127 1128 ctrl = nvmf_create_ctrl(nvmf_device, buf); 1129 if (IS_ERR(ctrl)) { 1130 ret = PTR_ERR(ctrl); 1131 goto out_unlock; 1132 } 1133 1134 seq_file->private = ctrl; 1135 1136 out_unlock: 1137 mutex_unlock(&nvmf_dev_mutex); 1138 kfree(buf); 1139 return ret ? ret : count; 1140 } 1141 1142 static void __nvmf_concat_opt_tokens(struct seq_file *seq_file) 1143 { 1144 const struct match_token *tok; 1145 int idx; 1146 1147 /* 1148 * Add dummy entries for instance and cntlid to 1149 * signal an invalid/non-existing controller 1150 */ 1151 seq_puts(seq_file, "instance=-1,cntlid=-1"); 1152 for (idx = 0; idx < ARRAY_SIZE(opt_tokens); idx++) { 1153 tok = &opt_tokens[idx]; 1154 if (tok->token == NVMF_OPT_ERR) 1155 continue; 1156 seq_puts(seq_file, ","); 1157 seq_puts(seq_file, tok->pattern); 1158 } 1159 seq_puts(seq_file, "\n"); 1160 } 1161 1162 static int nvmf_dev_show(struct seq_file *seq_file, void *private) 1163 { 1164 struct nvme_ctrl *ctrl; 1165 1166 mutex_lock(&nvmf_dev_mutex); 1167 ctrl = seq_file->private; 1168 if (!ctrl) { 1169 __nvmf_concat_opt_tokens(seq_file); 1170 goto out_unlock; 1171 } 1172 1173 seq_printf(seq_file, "instance=%d,cntlid=%d\n", 1174 ctrl->instance, ctrl->cntlid); 1175 1176 out_unlock: 1177 mutex_unlock(&nvmf_dev_mutex); 1178 return 0; 1179 } 1180 1181 static int nvmf_dev_open(struct inode *inode, struct file *file) 1182 { 1183 /* 1184 * The miscdevice code initializes file->private_data, but doesn't 1185 * make use of it later. 1186 */ 1187 file->private_data = NULL; 1188 return single_open(file, nvmf_dev_show, NULL); 1189 } 1190 1191 static int nvmf_dev_release(struct inode *inode, struct file *file) 1192 { 1193 struct seq_file *seq_file = file->private_data; 1194 struct nvme_ctrl *ctrl = seq_file->private; 1195 1196 if (ctrl) 1197 nvme_put_ctrl(ctrl); 1198 return single_release(inode, file); 1199 } 1200 1201 static const struct file_operations nvmf_dev_fops = { 1202 .owner = THIS_MODULE, 1203 .write = nvmf_dev_write, 1204 .read = seq_read, 1205 .open = nvmf_dev_open, 1206 .release = nvmf_dev_release, 1207 }; 1208 1209 static struct miscdevice nvmf_misc = { 1210 .minor = MISC_DYNAMIC_MINOR, 1211 .name = "nvme-fabrics", 1212 .fops = &nvmf_dev_fops, 1213 }; 1214 1215 static int __init nvmf_init(void) 1216 { 1217 int ret; 1218 1219 nvmf_default_host = nvmf_host_default(); 1220 if (!nvmf_default_host) 1221 return -ENOMEM; 1222 1223 nvmf_class = class_create(THIS_MODULE, "nvme-fabrics"); 1224 if (IS_ERR(nvmf_class)) { 1225 pr_err("couldn't register class nvme-fabrics\n"); 1226 ret = PTR_ERR(nvmf_class); 1227 goto out_free_host; 1228 } 1229 1230 nvmf_device = 1231 device_create(nvmf_class, NULL, MKDEV(0, 0), NULL, "ctl"); 1232 if (IS_ERR(nvmf_device)) { 1233 pr_err("couldn't create nvme-fabris device!\n"); 1234 ret = PTR_ERR(nvmf_device); 1235 goto out_destroy_class; 1236 } 1237 1238 ret = misc_register(&nvmf_misc); 1239 if (ret) { 1240 pr_err("couldn't register misc device: %d\n", ret); 1241 goto out_destroy_device; 1242 } 1243 1244 return 0; 1245 1246 out_destroy_device: 1247 device_destroy(nvmf_class, MKDEV(0, 0)); 1248 out_destroy_class: 1249 class_destroy(nvmf_class); 1250 out_free_host: 1251 nvmf_host_put(nvmf_default_host); 1252 return ret; 1253 } 1254 1255 static void __exit nvmf_exit(void) 1256 { 1257 misc_deregister(&nvmf_misc); 1258 device_destroy(nvmf_class, MKDEV(0, 0)); 1259 class_destroy(nvmf_class); 1260 nvmf_host_put(nvmf_default_host); 1261 1262 BUILD_BUG_ON(sizeof(struct nvmf_common_command) != 64); 1263 BUILD_BUG_ON(sizeof(struct nvmf_connect_command) != 64); 1264 BUILD_BUG_ON(sizeof(struct nvmf_property_get_command) != 64); 1265 BUILD_BUG_ON(sizeof(struct nvmf_property_set_command) != 64); 1266 BUILD_BUG_ON(sizeof(struct nvmf_auth_send_command) != 64); 1267 BUILD_BUG_ON(sizeof(struct nvmf_auth_receive_command) != 64); 1268 BUILD_BUG_ON(sizeof(struct nvmf_connect_data) != 1024); 1269 BUILD_BUG_ON(sizeof(struct nvmf_auth_dhchap_negotiate_data) != 8); 1270 BUILD_BUG_ON(sizeof(struct nvmf_auth_dhchap_challenge_data) != 16); 1271 BUILD_BUG_ON(sizeof(struct nvmf_auth_dhchap_reply_data) != 16); 1272 BUILD_BUG_ON(sizeof(struct nvmf_auth_dhchap_success1_data) != 16); 1273 BUILD_BUG_ON(sizeof(struct nvmf_auth_dhchap_success2_data) != 16); 1274 } 1275 1276 MODULE_LICENSE("GPL v2"); 1277 1278 module_init(nvmf_init); 1279 module_exit(nvmf_exit); 1280