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