2 * NVMe over Fabrics common host code.
3 * Copyright (c) 2015-2016 HGST, a Western Digital Company.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/init.h>
16 #include <linux/miscdevice.h>
17 #include <linux/module.h>
18 #include <linux/mutex.h>
19 #include <linux/parser.h>
20 #include <linux/seq_file.h>
24 static LIST_HEAD(nvmf_transports);
25 static DECLARE_RWSEM(nvmf_transports_rwsem);
27 static LIST_HEAD(nvmf_hosts);
28 static DEFINE_MUTEX(nvmf_hosts_mutex);
30 static struct nvmf_host *nvmf_default_host;
32 static struct nvmf_host *__nvmf_host_find(const char *hostnqn)
34 struct nvmf_host *host;
36 list_for_each_entry(host, &nvmf_hosts, list) {
37 if (!strcmp(host->nqn, hostnqn))
44 static struct nvmf_host *nvmf_host_add(const char *hostnqn)
46 struct nvmf_host *host;
48 mutex_lock(&nvmf_hosts_mutex);
49 host = __nvmf_host_find(hostnqn);
55 host = kmalloc(sizeof(*host), GFP_KERNEL);
59 kref_init(&host->ref);
60 strlcpy(host->nqn, hostnqn, NVMF_NQN_SIZE);
62 list_add_tail(&host->list, &nvmf_hosts);
64 mutex_unlock(&nvmf_hosts_mutex);
68 static struct nvmf_host *nvmf_host_default(void)
70 struct nvmf_host *host;
72 host = kmalloc(sizeof(*host), GFP_KERNEL);
76 kref_init(&host->ref);
78 snprintf(host->nqn, NVMF_NQN_SIZE,
79 "nqn.2014-08.org.nvmexpress:uuid:%pUb", &host->id);
81 mutex_lock(&nvmf_hosts_mutex);
82 list_add_tail(&host->list, &nvmf_hosts);
83 mutex_unlock(&nvmf_hosts_mutex);
88 static void nvmf_host_destroy(struct kref *ref)
90 struct nvmf_host *host = container_of(ref, struct nvmf_host, ref);
92 mutex_lock(&nvmf_hosts_mutex);
93 list_del(&host->list);
94 mutex_unlock(&nvmf_hosts_mutex);
99 static void nvmf_host_put(struct nvmf_host *host)
102 kref_put(&host->ref, nvmf_host_destroy);
106 * nvmf_get_address() - Get address/port
107 * @ctrl: Host NVMe controller instance which we got the address
108 * @buf: OUTPUT parameter that will contain the address/port
111 int nvmf_get_address(struct nvme_ctrl *ctrl, char *buf, int size)
115 if (ctrl->opts->mask & NVMF_OPT_TRADDR)
116 len += snprintf(buf, size, "traddr=%s", ctrl->opts->traddr);
117 if (ctrl->opts->mask & NVMF_OPT_TRSVCID)
118 len += snprintf(buf + len, size - len, "%strsvcid=%s",
119 (len) ? "," : "", ctrl->opts->trsvcid);
120 if (ctrl->opts->mask & NVMF_OPT_HOST_TRADDR)
121 len += snprintf(buf + len, size - len, "%shost_traddr=%s",
122 (len) ? "," : "", ctrl->opts->host_traddr);
123 len += snprintf(buf + len, size - len, "\n");
127 EXPORT_SYMBOL_GPL(nvmf_get_address);
130 * nvmf_reg_read32() - NVMe Fabrics "Property Get" API function.
131 * @ctrl: Host NVMe controller instance maintaining the admin
132 * queue used to submit the property read command to
133 * the allocated NVMe controller resource on the target system.
134 * @off: Starting offset value of the targeted property
135 * register (see the fabrics section of the NVMe standard).
136 * @val: OUTPUT parameter that will contain the value of
137 * the property after a successful read.
139 * Used by the host system to retrieve a 32-bit capsule property value
140 * from an NVMe controller on the target system.
142 * ("Capsule property" is an "PCIe register concept" applied to the
143 * NVMe fabrics space.)
147 * > 0: NVMe error status code
148 * < 0: Linux errno error code
150 int nvmf_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val)
152 struct nvme_command cmd;
153 union nvme_result res;
156 memset(&cmd, 0, sizeof(cmd));
157 cmd.prop_get.opcode = nvme_fabrics_command;
158 cmd.prop_get.fctype = nvme_fabrics_type_property_get;
159 cmd.prop_get.offset = cpu_to_le32(off);
161 ret = __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, &res, NULL, 0, 0,
165 *val = le64_to_cpu(res.u64);
166 if (unlikely(ret != 0))
167 dev_err(ctrl->device,
168 "Property Get error: %d, offset %#x\n",
169 ret > 0 ? ret & ~NVME_SC_DNR : ret, off);
173 EXPORT_SYMBOL_GPL(nvmf_reg_read32);
176 * nvmf_reg_read64() - NVMe Fabrics "Property Get" API function.
177 * @ctrl: Host NVMe controller instance maintaining the admin
178 * queue used to submit the property read command to
179 * the allocated controller resource on the target system.
180 * @off: Starting offset value of the targeted property
181 * register (see the fabrics section of the NVMe standard).
182 * @val: OUTPUT parameter that will contain the value of
183 * the property after a successful read.
185 * Used by the host system to retrieve a 64-bit capsule property value
186 * from an NVMe controller on the target system.
188 * ("Capsule property" is an "PCIe register concept" applied to the
189 * NVMe fabrics space.)
193 * > 0: NVMe error status code
194 * < 0: Linux errno error code
196 int nvmf_reg_read64(struct nvme_ctrl *ctrl, u32 off, u64 *val)
198 struct nvme_command cmd;
199 union nvme_result res;
202 memset(&cmd, 0, sizeof(cmd));
203 cmd.prop_get.opcode = nvme_fabrics_command;
204 cmd.prop_get.fctype = nvme_fabrics_type_property_get;
205 cmd.prop_get.attrib = 1;
206 cmd.prop_get.offset = cpu_to_le32(off);
208 ret = __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, &res, NULL, 0, 0,
212 *val = le64_to_cpu(res.u64);
213 if (unlikely(ret != 0))
214 dev_err(ctrl->device,
215 "Property Get error: %d, offset %#x\n",
216 ret > 0 ? ret & ~NVME_SC_DNR : ret, off);
219 EXPORT_SYMBOL_GPL(nvmf_reg_read64);
222 * nvmf_reg_write32() - NVMe Fabrics "Property Write" API function.
223 * @ctrl: Host NVMe controller instance maintaining the admin
224 * queue used to submit the property read command to
225 * the allocated NVMe controller resource on the target system.
226 * @off: Starting offset value of the targeted property
227 * register (see the fabrics section of the NVMe standard).
228 * @val: Input parameter that contains the value to be
229 * written to the property.
231 * Used by the NVMe host system to write a 32-bit capsule property value
232 * to an NVMe controller on the target system.
234 * ("Capsule property" is an "PCIe register concept" applied to the
235 * NVMe fabrics space.)
238 * 0: successful write
239 * > 0: NVMe error status code
240 * < 0: Linux errno error code
242 int nvmf_reg_write32(struct nvme_ctrl *ctrl, u32 off, u32 val)
244 struct nvme_command cmd;
247 memset(&cmd, 0, sizeof(cmd));
248 cmd.prop_set.opcode = nvme_fabrics_command;
249 cmd.prop_set.fctype = nvme_fabrics_type_property_set;
250 cmd.prop_set.attrib = 0;
251 cmd.prop_set.offset = cpu_to_le32(off);
252 cmd.prop_set.value = cpu_to_le64(val);
254 ret = __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, NULL, NULL, 0, 0,
257 dev_err(ctrl->device,
258 "Property Set error: %d, offset %#x\n",
259 ret > 0 ? ret & ~NVME_SC_DNR : ret, off);
262 EXPORT_SYMBOL_GPL(nvmf_reg_write32);
265 * nvmf_log_connect_error() - Error-parsing-diagnostic print
266 * out function for connect() errors.
268 * @ctrl: the specific /dev/nvmeX device that had the error.
270 * @errval: Error code to be decoded in a more human-friendly
273 * @offset: For use with the NVMe error code NVME_SC_CONNECT_INVALID_PARAM.
275 * @cmd: This is the SQE portion of a submission capsule.
277 * @data: This is the "Data" portion of a submission capsule.
279 static void nvmf_log_connect_error(struct nvme_ctrl *ctrl,
280 int errval, int offset, struct nvme_command *cmd,
281 struct nvmf_connect_data *data)
283 int err_sctype = errval & (~NVME_SC_DNR);
285 switch (err_sctype) {
287 case (NVME_SC_CONNECT_INVALID_PARAM):
289 char *inv_data = "Connect Invalid Data Parameter";
291 switch (offset & 0xffff) {
292 case (offsetof(struct nvmf_connect_data, cntlid)):
293 dev_err(ctrl->device,
295 inv_data, data->cntlid);
297 case (offsetof(struct nvmf_connect_data, hostnqn)):
298 dev_err(ctrl->device,
299 "%s, hostnqn \"%s\"\n",
300 inv_data, data->hostnqn);
302 case (offsetof(struct nvmf_connect_data, subsysnqn)):
303 dev_err(ctrl->device,
304 "%s, subsysnqn \"%s\"\n",
305 inv_data, data->subsysnqn);
308 dev_err(ctrl->device,
309 "%s, starting byte offset: %d\n",
310 inv_data, offset & 0xffff);
314 char *inv_sqe = "Connect Invalid SQE Parameter";
317 case (offsetof(struct nvmf_connect_command, qid)):
318 dev_err(ctrl->device,
320 inv_sqe, cmd->connect.qid);
323 dev_err(ctrl->device,
324 "%s, starting byte offset: %d\n",
330 case NVME_SC_CONNECT_INVALID_HOST:
331 dev_err(ctrl->device,
332 "Connect for subsystem %s is not allowed, hostnqn: %s\n",
333 data->subsysnqn, data->hostnqn);
336 case NVME_SC_CONNECT_CTRL_BUSY:
337 dev_err(ctrl->device,
338 "Connect command failed: controller is busy or not available\n");
341 case NVME_SC_CONNECT_FORMAT:
342 dev_err(ctrl->device,
343 "Connect incompatible format: %d",
344 cmd->connect.recfmt);
348 dev_err(ctrl->device,
349 "Connect command failed, error wo/DNR bit: %d\n",
352 } /* switch (err_sctype) */
356 * nvmf_connect_admin_queue() - NVMe Fabrics Admin Queue "Connect"
358 * @ctrl: Host nvme controller instance used to request
359 * a new NVMe controller allocation on the target
360 * system and establish an NVMe Admin connection to
363 * This function enables an NVMe host device to request a new allocation of
364 * an NVMe controller resource on a target system as well establish a
365 * fabrics-protocol connection of the NVMe Admin queue between the
366 * host system device and the allocated NVMe controller on the
367 * target system via a NVMe Fabrics "Connect" command.
371 * > 0: NVMe error status code
372 * < 0: Linux errno error code
375 int nvmf_connect_admin_queue(struct nvme_ctrl *ctrl)
377 struct nvme_command cmd;
378 union nvme_result res;
379 struct nvmf_connect_data *data;
382 memset(&cmd, 0, sizeof(cmd));
383 cmd.connect.opcode = nvme_fabrics_command;
384 cmd.connect.fctype = nvme_fabrics_type_connect;
386 cmd.connect.sqsize = cpu_to_le16(NVME_AQ_DEPTH - 1);
389 * Set keep-alive timeout in seconds granularity (ms * 1000)
390 * and add a grace period for controller kato enforcement
392 cmd.connect.kato = ctrl->opts->discovery_nqn ? 0 :
393 cpu_to_le32((ctrl->kato + NVME_KATO_GRACE) * 1000);
395 if (ctrl->opts->disable_sqflow)
396 cmd.connect.cattr |= NVME_CONNECT_DISABLE_SQFLOW;
398 data = kzalloc(sizeof(*data), GFP_KERNEL);
402 uuid_copy(&data->hostid, &ctrl->opts->host->id);
403 data->cntlid = cpu_to_le16(0xffff);
404 strncpy(data->subsysnqn, ctrl->opts->subsysnqn, NVMF_NQN_SIZE);
405 strncpy(data->hostnqn, ctrl->opts->host->nqn, NVMF_NQN_SIZE);
407 ret = __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, &res,
408 data, sizeof(*data), 0, NVME_QID_ANY, 1,
409 BLK_MQ_REQ_RESERVED | BLK_MQ_REQ_NOWAIT);
411 nvmf_log_connect_error(ctrl, ret, le32_to_cpu(res.u32),
416 ctrl->cntlid = le16_to_cpu(res.u16);
422 EXPORT_SYMBOL_GPL(nvmf_connect_admin_queue);
425 * nvmf_connect_io_queue() - NVMe Fabrics I/O Queue "Connect"
427 * @ctrl: Host nvme controller instance used to establish an
428 * NVMe I/O queue connection to the already allocated NVMe
429 * controller on the target system.
430 * @qid: NVMe I/O queue number for the new I/O connection between
431 * host and target (note qid == 0 is illegal as this is
432 * the Admin queue, per NVMe standard).
434 * This function issues a fabrics-protocol connection
435 * of a NVMe I/O queue (via NVMe Fabrics "Connect" command)
436 * between the host system device and the allocated NVMe controller
437 * on the target system.
441 * > 0: NVMe error status code
442 * < 0: Linux errno error code
444 int nvmf_connect_io_queue(struct nvme_ctrl *ctrl, u16 qid)
446 struct nvme_command cmd;
447 struct nvmf_connect_data *data;
448 union nvme_result res;
451 memset(&cmd, 0, sizeof(cmd));
452 cmd.connect.opcode = nvme_fabrics_command;
453 cmd.connect.fctype = nvme_fabrics_type_connect;
454 cmd.connect.qid = cpu_to_le16(qid);
455 cmd.connect.sqsize = cpu_to_le16(ctrl->sqsize);
457 if (ctrl->opts->disable_sqflow)
458 cmd.connect.cattr |= NVME_CONNECT_DISABLE_SQFLOW;
460 data = kzalloc(sizeof(*data), GFP_KERNEL);
464 uuid_copy(&data->hostid, &ctrl->opts->host->id);
465 data->cntlid = cpu_to_le16(ctrl->cntlid);
466 strncpy(data->subsysnqn, ctrl->opts->subsysnqn, NVMF_NQN_SIZE);
467 strncpy(data->hostnqn, ctrl->opts->host->nqn, NVMF_NQN_SIZE);
469 ret = __nvme_submit_sync_cmd(ctrl->connect_q, &cmd, &res,
470 data, sizeof(*data), 0, qid, 1,
471 BLK_MQ_REQ_RESERVED | BLK_MQ_REQ_NOWAIT);
473 nvmf_log_connect_error(ctrl, ret, le32_to_cpu(res.u32),
479 EXPORT_SYMBOL_GPL(nvmf_connect_io_queue);
481 bool nvmf_should_reconnect(struct nvme_ctrl *ctrl)
483 if (ctrl->opts->max_reconnects == -1 ||
484 ctrl->nr_reconnects < ctrl->opts->max_reconnects)
489 EXPORT_SYMBOL_GPL(nvmf_should_reconnect);
492 * nvmf_register_transport() - NVMe Fabrics Library registration function.
493 * @ops: Transport ops instance to be registered to the
494 * common fabrics library.
496 * API function that registers the type of specific transport fabric
497 * being implemented to the common NVMe fabrics library. Part of
498 * the overall init sequence of starting up a fabrics driver.
500 int nvmf_register_transport(struct nvmf_transport_ops *ops)
502 if (!ops->create_ctrl)
505 down_write(&nvmf_transports_rwsem);
506 list_add_tail(&ops->entry, &nvmf_transports);
507 up_write(&nvmf_transports_rwsem);
511 EXPORT_SYMBOL_GPL(nvmf_register_transport);
514 * nvmf_unregister_transport() - NVMe Fabrics Library unregistration function.
515 * @ops: Transport ops instance to be unregistered from the
516 * common fabrics library.
518 * Fabrics API function that unregisters the type of specific transport
519 * fabric being implemented from the common NVMe fabrics library.
520 * Part of the overall exit sequence of unloading the implemented driver.
522 void nvmf_unregister_transport(struct nvmf_transport_ops *ops)
524 down_write(&nvmf_transports_rwsem);
525 list_del(&ops->entry);
526 up_write(&nvmf_transports_rwsem);
528 EXPORT_SYMBOL_GPL(nvmf_unregister_transport);
530 static struct nvmf_transport_ops *nvmf_lookup_transport(
531 struct nvmf_ctrl_options *opts)
533 struct nvmf_transport_ops *ops;
535 lockdep_assert_held(&nvmf_transports_rwsem);
537 list_for_each_entry(ops, &nvmf_transports, entry) {
538 if (strcmp(ops->name, opts->transport) == 0)
546 * For something we're not in a state to send to the device the default action
547 * is to busy it and retry it after the controller state is recovered. However,
548 * if the controller is deleting or if anything is marked for failfast or
549 * nvme multipath it is immediately failed.
551 * Note: commands used to initialize the controller will be marked for failfast.
552 * Note: nvme cli/ioctl commands are marked for failfast.
554 blk_status_t nvmf_fail_nonready_command(struct nvme_ctrl *ctrl,
557 if (ctrl->state != NVME_CTRL_DELETING &&
558 ctrl->state != NVME_CTRL_DEAD &&
559 !blk_noretry_request(rq) && !(rq->cmd_flags & REQ_NVME_MPATH))
560 return BLK_STS_RESOURCE;
562 nvme_req(rq)->status = NVME_SC_HOST_PATH_ERROR;
563 blk_mq_start_request(rq);
564 nvme_complete_rq(rq);
567 EXPORT_SYMBOL_GPL(nvmf_fail_nonready_command);
569 bool __nvmf_check_ready(struct nvme_ctrl *ctrl, struct request *rq,
572 struct nvme_request *req = nvme_req(rq);
575 * If we are in some state of setup or teardown only allow
576 * internally generated commands.
578 if (!blk_rq_is_passthrough(rq) || (req->flags & NVME_REQ_USERCMD))
582 * Only allow commands on a live queue, except for the connect command,
583 * which is require to set the queue live in the appropinquate states.
585 switch (ctrl->state) {
587 case NVME_CTRL_CONNECTING:
588 if (req->cmd->common.opcode == nvme_fabrics_command &&
589 req->cmd->fabrics.fctype == nvme_fabrics_type_connect)
600 EXPORT_SYMBOL_GPL(__nvmf_check_ready);
602 static const match_table_t opt_tokens = {
603 { NVMF_OPT_TRANSPORT, "transport=%s" },
604 { NVMF_OPT_TRADDR, "traddr=%s" },
605 { NVMF_OPT_TRSVCID, "trsvcid=%s" },
606 { NVMF_OPT_NQN, "nqn=%s" },
607 { NVMF_OPT_QUEUE_SIZE, "queue_size=%d" },
608 { NVMF_OPT_NR_IO_QUEUES, "nr_io_queues=%d" },
609 { NVMF_OPT_RECONNECT_DELAY, "reconnect_delay=%d" },
610 { NVMF_OPT_CTRL_LOSS_TMO, "ctrl_loss_tmo=%d" },
611 { NVMF_OPT_KATO, "keep_alive_tmo=%d" },
612 { NVMF_OPT_HOSTNQN, "hostnqn=%s" },
613 { NVMF_OPT_HOST_TRADDR, "host_traddr=%s" },
614 { NVMF_OPT_HOST_ID, "hostid=%s" },
615 { NVMF_OPT_DUP_CONNECT, "duplicate_connect" },
616 { NVMF_OPT_DISABLE_SQFLOW, "disable_sqflow" },
617 { NVMF_OPT_HDR_DIGEST, "hdr_digest" },
618 { NVMF_OPT_DATA_DIGEST, "data_digest" },
619 { NVMF_OPT_ERR, NULL }
622 static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
625 substring_t args[MAX_OPT_ARGS];
626 char *options, *o, *p;
629 int ctrl_loss_tmo = NVMF_DEF_CTRL_LOSS_TMO;
633 opts->queue_size = NVMF_DEF_QUEUE_SIZE;
634 opts->nr_io_queues = num_online_cpus();
635 opts->reconnect_delay = NVMF_DEF_RECONNECT_DELAY;
636 opts->kato = NVME_DEFAULT_KATO;
637 opts->duplicate_connect = false;
638 opts->hdr_digest = false;
639 opts->data_digest = false;
641 options = o = kstrdup(buf, GFP_KERNEL);
647 while ((p = strsep(&o, ",\n")) != NULL) {
651 token = match_token(p, opt_tokens, args);
654 case NVMF_OPT_TRANSPORT:
655 p = match_strdup(args);
660 kfree(opts->transport);
664 p = match_strdup(args);
669 kfree(opts->subsysnqn);
671 nqnlen = strlen(opts->subsysnqn);
672 if (nqnlen >= NVMF_NQN_SIZE) {
673 pr_err("%s needs to be < %d bytes\n",
674 opts->subsysnqn, NVMF_NQN_SIZE);
678 opts->discovery_nqn =
679 !(strcmp(opts->subsysnqn,
680 NVME_DISC_SUBSYS_NAME));
682 case NVMF_OPT_TRADDR:
683 p = match_strdup(args);
691 case NVMF_OPT_TRSVCID:
692 p = match_strdup(args);
697 kfree(opts->trsvcid);
700 case NVMF_OPT_QUEUE_SIZE:
701 if (match_int(args, &token)) {
705 if (token < NVMF_MIN_QUEUE_SIZE ||
706 token > NVMF_MAX_QUEUE_SIZE) {
707 pr_err("Invalid queue_size %d\n", token);
711 opts->queue_size = token;
713 case NVMF_OPT_NR_IO_QUEUES:
714 if (match_int(args, &token)) {
719 pr_err("Invalid number of IOQs %d\n", token);
723 if (opts->discovery_nqn) {
724 pr_debug("Ignoring nr_io_queues value for discovery controller\n");
728 opts->nr_io_queues = min_t(unsigned int,
729 num_online_cpus(), token);
732 if (match_int(args, &token)) {
738 pr_err("Invalid keep_alive_tmo %d\n", token);
741 } else if (token == 0 && !opts->discovery_nqn) {
742 /* Allowed for debug */
743 pr_warn("keep_alive_tmo 0 won't execute keep alives!!!\n");
747 if (opts->discovery_nqn && opts->kato) {
748 pr_err("Discovery controllers cannot accept KATO != 0\n");
754 case NVMF_OPT_CTRL_LOSS_TMO:
755 if (match_int(args, &token)) {
761 pr_warn("ctrl_loss_tmo < 0 will reconnect forever\n");
762 ctrl_loss_tmo = token;
764 case NVMF_OPT_HOSTNQN:
766 pr_err("hostnqn already user-assigned: %s\n",
771 p = match_strdup(args);
777 if (nqnlen >= NVMF_NQN_SIZE) {
778 pr_err("%s needs to be < %d bytes\n",
784 nvmf_host_put(opts->host);
785 opts->host = nvmf_host_add(p);
792 case NVMF_OPT_RECONNECT_DELAY:
793 if (match_int(args, &token)) {
798 pr_err("Invalid reconnect_delay %d\n", token);
802 opts->reconnect_delay = token;
804 case NVMF_OPT_HOST_TRADDR:
805 p = match_strdup(args);
810 kfree(opts->host_traddr);
811 opts->host_traddr = p;
813 case NVMF_OPT_HOST_ID:
814 p = match_strdup(args);
819 ret = uuid_parse(p, &hostid);
821 pr_err("Invalid hostid %s\n", p);
828 case NVMF_OPT_DUP_CONNECT:
829 opts->duplicate_connect = true;
831 case NVMF_OPT_DISABLE_SQFLOW:
832 opts->disable_sqflow = true;
834 case NVMF_OPT_HDR_DIGEST:
835 opts->hdr_digest = true;
837 case NVMF_OPT_DATA_DIGEST:
838 opts->data_digest = true;
841 pr_warn("unknown parameter or missing value '%s' in ctrl creation request\n",
848 if (opts->discovery_nqn) {
850 opts->nr_io_queues = 0;
851 opts->duplicate_connect = true;
853 if (ctrl_loss_tmo < 0)
854 opts->max_reconnects = -1;
856 opts->max_reconnects = DIV_ROUND_UP(ctrl_loss_tmo,
857 opts->reconnect_delay);
860 kref_get(&nvmf_default_host->ref);
861 opts->host = nvmf_default_host;
864 uuid_copy(&opts->host->id, &hostid);
871 static int nvmf_check_required_opts(struct nvmf_ctrl_options *opts,
872 unsigned int required_opts)
874 if ((opts->mask & required_opts) != required_opts) {
877 for (i = 0; i < ARRAY_SIZE(opt_tokens); i++) {
878 if ((opt_tokens[i].token & required_opts) &&
879 !(opt_tokens[i].token & opts->mask)) {
880 pr_warn("missing parameter '%s'\n",
881 opt_tokens[i].pattern);
891 bool nvmf_ip_options_match(struct nvme_ctrl *ctrl,
892 struct nvmf_ctrl_options *opts)
894 if (!nvmf_ctlr_matches_baseopts(ctrl, opts) ||
895 strcmp(opts->traddr, ctrl->opts->traddr) ||
896 strcmp(opts->trsvcid, ctrl->opts->trsvcid))
900 * Checking the local address is rough. In most cases, none is specified
901 * and the host port is selected by the stack.
903 * Assume no match if:
904 * - local address is specified and address is not the same
905 * - local address is not specified but remote is, or vice versa
906 * (admin using specific host_traddr when it matters).
908 if ((opts->mask & NVMF_OPT_HOST_TRADDR) &&
909 (ctrl->opts->mask & NVMF_OPT_HOST_TRADDR)) {
910 if (strcmp(opts->host_traddr, ctrl->opts->host_traddr))
912 } else if ((opts->mask & NVMF_OPT_HOST_TRADDR) ||
913 (ctrl->opts->mask & NVMF_OPT_HOST_TRADDR)) {
919 EXPORT_SYMBOL_GPL(nvmf_ip_options_match);
921 static int nvmf_check_allowed_opts(struct nvmf_ctrl_options *opts,
922 unsigned int allowed_opts)
924 if (opts->mask & ~allowed_opts) {
927 for (i = 0; i < ARRAY_SIZE(opt_tokens); i++) {
928 if ((opt_tokens[i].token & opts->mask) &&
929 (opt_tokens[i].token & ~allowed_opts)) {
930 pr_warn("invalid parameter '%s'\n",
931 opt_tokens[i].pattern);
941 void nvmf_free_options(struct nvmf_ctrl_options *opts)
943 nvmf_host_put(opts->host);
944 kfree(opts->transport);
946 kfree(opts->trsvcid);
947 kfree(opts->subsysnqn);
948 kfree(opts->host_traddr);
951 EXPORT_SYMBOL_GPL(nvmf_free_options);
953 #define NVMF_REQUIRED_OPTS (NVMF_OPT_TRANSPORT | NVMF_OPT_NQN)
954 #define NVMF_ALLOWED_OPTS (NVMF_OPT_QUEUE_SIZE | NVMF_OPT_NR_IO_QUEUES | \
955 NVMF_OPT_KATO | NVMF_OPT_HOSTNQN | \
956 NVMF_OPT_HOST_ID | NVMF_OPT_DUP_CONNECT |\
957 NVMF_OPT_DISABLE_SQFLOW)
959 static struct nvme_ctrl *
960 nvmf_create_ctrl(struct device *dev, const char *buf, size_t count)
962 struct nvmf_ctrl_options *opts;
963 struct nvmf_transport_ops *ops;
964 struct nvme_ctrl *ctrl;
967 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
969 return ERR_PTR(-ENOMEM);
971 ret = nvmf_parse_options(opts, buf);
976 request_module("nvme-%s", opts->transport);
979 * Check the generic options first as we need a valid transport for
980 * the lookup below. Then clear the generic flags so that transport
981 * drivers don't have to care about them.
983 ret = nvmf_check_required_opts(opts, NVMF_REQUIRED_OPTS);
986 opts->mask &= ~NVMF_REQUIRED_OPTS;
988 down_read(&nvmf_transports_rwsem);
989 ops = nvmf_lookup_transport(opts);
991 pr_info("no handler found for transport %s.\n",
997 if (!try_module_get(ops->module)) {
1001 up_read(&nvmf_transports_rwsem);
1003 ret = nvmf_check_required_opts(opts, ops->required_opts);
1005 goto out_module_put;
1006 ret = nvmf_check_allowed_opts(opts, NVMF_ALLOWED_OPTS |
1007 ops->allowed_opts | ops->required_opts);
1009 goto out_module_put;
1011 ctrl = ops->create_ctrl(dev, opts);
1013 ret = PTR_ERR(ctrl);
1014 goto out_module_put;
1017 module_put(ops->module);
1021 module_put(ops->module);
1024 up_read(&nvmf_transports_rwsem);
1026 nvmf_free_options(opts);
1027 return ERR_PTR(ret);
1030 static struct class *nvmf_class;
1031 static struct device *nvmf_device;
1032 static DEFINE_MUTEX(nvmf_dev_mutex);
1034 static ssize_t nvmf_dev_write(struct file *file, const char __user *ubuf,
1035 size_t count, loff_t *pos)
1037 struct seq_file *seq_file = file->private_data;
1038 struct nvme_ctrl *ctrl;
1042 if (count > PAGE_SIZE)
1045 buf = memdup_user_nul(ubuf, count);
1047 return PTR_ERR(buf);
1049 mutex_lock(&nvmf_dev_mutex);
1050 if (seq_file->private) {
1055 ctrl = nvmf_create_ctrl(nvmf_device, buf, count);
1057 ret = PTR_ERR(ctrl);
1061 seq_file->private = ctrl;
1064 mutex_unlock(&nvmf_dev_mutex);
1066 return ret ? ret : count;
1069 static int nvmf_dev_show(struct seq_file *seq_file, void *private)
1071 struct nvme_ctrl *ctrl;
1074 mutex_lock(&nvmf_dev_mutex);
1075 ctrl = seq_file->private;
1081 seq_printf(seq_file, "instance=%d,cntlid=%d\n",
1082 ctrl->instance, ctrl->cntlid);
1085 mutex_unlock(&nvmf_dev_mutex);
1089 static int nvmf_dev_open(struct inode *inode, struct file *file)
1092 * The miscdevice code initializes file->private_data, but doesn't
1093 * make use of it later.
1095 file->private_data = NULL;
1096 return single_open(file, nvmf_dev_show, NULL);
1099 static int nvmf_dev_release(struct inode *inode, struct file *file)
1101 struct seq_file *seq_file = file->private_data;
1102 struct nvme_ctrl *ctrl = seq_file->private;
1105 nvme_put_ctrl(ctrl);
1106 return single_release(inode, file);
1109 static const struct file_operations nvmf_dev_fops = {
1110 .owner = THIS_MODULE,
1111 .write = nvmf_dev_write,
1113 .open = nvmf_dev_open,
1114 .release = nvmf_dev_release,
1117 static struct miscdevice nvmf_misc = {
1118 .minor = MISC_DYNAMIC_MINOR,
1119 .name = "nvme-fabrics",
1120 .fops = &nvmf_dev_fops,
1123 static int __init nvmf_init(void)
1127 nvmf_default_host = nvmf_host_default();
1128 if (!nvmf_default_host)
1131 nvmf_class = class_create(THIS_MODULE, "nvme-fabrics");
1132 if (IS_ERR(nvmf_class)) {
1133 pr_err("couldn't register class nvme-fabrics\n");
1134 ret = PTR_ERR(nvmf_class);
1139 device_create(nvmf_class, NULL, MKDEV(0, 0), NULL, "ctl");
1140 if (IS_ERR(nvmf_device)) {
1141 pr_err("couldn't create nvme-fabris device!\n");
1142 ret = PTR_ERR(nvmf_device);
1143 goto out_destroy_class;
1146 ret = misc_register(&nvmf_misc);
1148 pr_err("couldn't register misc device: %d\n", ret);
1149 goto out_destroy_device;
1155 device_destroy(nvmf_class, MKDEV(0, 0));
1157 class_destroy(nvmf_class);
1159 nvmf_host_put(nvmf_default_host);
1163 static void __exit nvmf_exit(void)
1165 misc_deregister(&nvmf_misc);
1166 device_destroy(nvmf_class, MKDEV(0, 0));
1167 class_destroy(nvmf_class);
1168 nvmf_host_put(nvmf_default_host);
1170 BUILD_BUG_ON(sizeof(struct nvmf_connect_command) != 64);
1171 BUILD_BUG_ON(sizeof(struct nvmf_property_get_command) != 64);
1172 BUILD_BUG_ON(sizeof(struct nvmf_property_set_command) != 64);
1173 BUILD_BUG_ON(sizeof(struct nvmf_connect_data) != 1024);
1176 MODULE_LICENSE("GPL v2");
1178 module_init(nvmf_init);
1179 module_exit(nvmf_exit);