1 // SPDX-License-Identifier: GPL-2.0
3 * NVMe over Fabrics common host code.
4 * Copyright (c) 2015-2016 HGST, a Western Digital Company.
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>
16 static LIST_HEAD(nvmf_transports);
17 static DECLARE_RWSEM(nvmf_transports_rwsem);
19 static LIST_HEAD(nvmf_hosts);
20 static DEFINE_MUTEX(nvmf_hosts_mutex);
22 static struct nvmf_host *nvmf_default_host;
24 static struct nvmf_host *__nvmf_host_find(const char *hostnqn)
26 struct nvmf_host *host;
28 list_for_each_entry(host, &nvmf_hosts, list) {
29 if (!strcmp(host->nqn, hostnqn))
36 static struct nvmf_host *nvmf_host_add(const char *hostnqn)
38 struct nvmf_host *host;
40 mutex_lock(&nvmf_hosts_mutex);
41 host = __nvmf_host_find(hostnqn);
47 host = kmalloc(sizeof(*host), GFP_KERNEL);
51 kref_init(&host->ref);
52 strlcpy(host->nqn, hostnqn, NVMF_NQN_SIZE);
54 list_add_tail(&host->list, &nvmf_hosts);
56 mutex_unlock(&nvmf_hosts_mutex);
60 static struct nvmf_host *nvmf_host_default(void)
62 struct nvmf_host *host;
64 host = kmalloc(sizeof(*host), GFP_KERNEL);
68 kref_init(&host->ref);
70 snprintf(host->nqn, NVMF_NQN_SIZE,
71 "nqn.2014-08.org.nvmexpress:uuid:%pUb", &host->id);
73 mutex_lock(&nvmf_hosts_mutex);
74 list_add_tail(&host->list, &nvmf_hosts);
75 mutex_unlock(&nvmf_hosts_mutex);
80 static void nvmf_host_destroy(struct kref *ref)
82 struct nvmf_host *host = container_of(ref, struct nvmf_host, ref);
84 mutex_lock(&nvmf_hosts_mutex);
85 list_del(&host->list);
86 mutex_unlock(&nvmf_hosts_mutex);
91 static void nvmf_host_put(struct nvmf_host *host)
94 kref_put(&host->ref, nvmf_host_destroy);
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
103 int nvmf_get_address(struct nvme_ctrl *ctrl, char *buf, int size)
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 len += scnprintf(buf + len, size - len, "\n");
119 EXPORT_SYMBOL_GPL(nvmf_get_address);
122 * nvmf_reg_read32() - NVMe Fabrics "Property Get" API function.
123 * @ctrl: Host NVMe controller instance maintaining the admin
124 * queue used to submit the property read command to
125 * the allocated NVMe controller resource on the target system.
126 * @off: Starting offset value of the targeted property
127 * register (see the fabrics section of the NVMe standard).
128 * @val: OUTPUT parameter that will contain the value of
129 * the property after a successful read.
131 * Used by the host system to retrieve a 32-bit capsule property value
132 * from an NVMe controller on the target system.
134 * ("Capsule property" is an "PCIe register concept" applied to the
135 * NVMe fabrics space.)
139 * > 0: NVMe error status code
140 * < 0: Linux errno error code
142 int nvmf_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val)
144 struct nvme_command cmd;
145 union nvme_result res;
148 memset(&cmd, 0, sizeof(cmd));
149 cmd.prop_get.opcode = nvme_fabrics_command;
150 cmd.prop_get.fctype = nvme_fabrics_type_property_get;
151 cmd.prop_get.offset = cpu_to_le32(off);
153 ret = __nvme_submit_sync_cmd(ctrl->fabrics_q, &cmd, &res, NULL, 0, 0,
154 NVME_QID_ANY, 0, 0, false);
157 *val = le64_to_cpu(res.u64);
158 if (unlikely(ret != 0))
159 dev_err(ctrl->device,
160 "Property Get error: %d, offset %#x\n",
161 ret > 0 ? ret & ~NVME_SC_DNR : ret, off);
165 EXPORT_SYMBOL_GPL(nvmf_reg_read32);
168 * nvmf_reg_read64() - NVMe Fabrics "Property Get" API function.
169 * @ctrl: Host NVMe controller instance maintaining the admin
170 * queue used to submit the property read command to
171 * the allocated controller resource on the target system.
172 * @off: Starting offset value of the targeted property
173 * register (see the fabrics section of the NVMe standard).
174 * @val: OUTPUT parameter that will contain the value of
175 * the property after a successful read.
177 * Used by the host system to retrieve a 64-bit capsule property value
178 * from an NVMe controller on the target system.
180 * ("Capsule property" is an "PCIe register concept" applied to the
181 * NVMe fabrics space.)
185 * > 0: NVMe error status code
186 * < 0: Linux errno error code
188 int nvmf_reg_read64(struct nvme_ctrl *ctrl, u32 off, u64 *val)
190 struct nvme_command cmd;
191 union nvme_result res;
194 memset(&cmd, 0, sizeof(cmd));
195 cmd.prop_get.opcode = nvme_fabrics_command;
196 cmd.prop_get.fctype = nvme_fabrics_type_property_get;
197 cmd.prop_get.attrib = 1;
198 cmd.prop_get.offset = cpu_to_le32(off);
200 ret = __nvme_submit_sync_cmd(ctrl->fabrics_q, &cmd, &res, NULL, 0, 0,
201 NVME_QID_ANY, 0, 0, false);
204 *val = le64_to_cpu(res.u64);
205 if (unlikely(ret != 0))
206 dev_err(ctrl->device,
207 "Property Get error: %d, offset %#x\n",
208 ret > 0 ? ret & ~NVME_SC_DNR : ret, off);
211 EXPORT_SYMBOL_GPL(nvmf_reg_read64);
214 * nvmf_reg_write32() - NVMe Fabrics "Property Write" API function.
215 * @ctrl: Host NVMe controller instance maintaining the admin
216 * queue used to submit the property read command to
217 * the allocated NVMe controller resource on the target system.
218 * @off: Starting offset value of the targeted property
219 * register (see the fabrics section of the NVMe standard).
220 * @val: Input parameter that contains the value to be
221 * written to the property.
223 * Used by the NVMe host system to write a 32-bit capsule property value
224 * to an NVMe controller on the target system.
226 * ("Capsule property" is an "PCIe register concept" applied to the
227 * NVMe fabrics space.)
230 * 0: successful write
231 * > 0: NVMe error status code
232 * < 0: Linux errno error code
234 int nvmf_reg_write32(struct nvme_ctrl *ctrl, u32 off, u32 val)
236 struct nvme_command cmd;
239 memset(&cmd, 0, sizeof(cmd));
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);
246 ret = __nvme_submit_sync_cmd(ctrl->fabrics_q, &cmd, NULL, NULL, 0, 0,
247 NVME_QID_ANY, 0, 0, false);
249 dev_err(ctrl->device,
250 "Property Set error: %d, offset %#x\n",
251 ret > 0 ? ret & ~NVME_SC_DNR : ret, off);
254 EXPORT_SYMBOL_GPL(nvmf_reg_write32);
257 * nvmf_log_connect_error() - Error-parsing-diagnostic print
258 * out function for connect() errors.
260 * @ctrl: the specific /dev/nvmeX device that had the error.
262 * @errval: Error code to be decoded in a more human-friendly
265 * @offset: For use with the NVMe error code NVME_SC_CONNECT_INVALID_PARAM.
267 * @cmd: This is the SQE portion of a submission capsule.
269 * @data: This is the "Data" portion of a submission capsule.
271 static void nvmf_log_connect_error(struct nvme_ctrl *ctrl,
272 int errval, int offset, struct nvme_command *cmd,
273 struct nvmf_connect_data *data)
275 int err_sctype = errval & (~NVME_SC_DNR);
277 switch (err_sctype) {
279 case (NVME_SC_CONNECT_INVALID_PARAM):
281 char *inv_data = "Connect Invalid Data Parameter";
283 switch (offset & 0xffff) {
284 case (offsetof(struct nvmf_connect_data, cntlid)):
285 dev_err(ctrl->device,
287 inv_data, data->cntlid);
289 case (offsetof(struct nvmf_connect_data, hostnqn)):
290 dev_err(ctrl->device,
291 "%s, hostnqn \"%s\"\n",
292 inv_data, data->hostnqn);
294 case (offsetof(struct nvmf_connect_data, subsysnqn)):
295 dev_err(ctrl->device,
296 "%s, subsysnqn \"%s\"\n",
297 inv_data, data->subsysnqn);
300 dev_err(ctrl->device,
301 "%s, starting byte offset: %d\n",
302 inv_data, offset & 0xffff);
306 char *inv_sqe = "Connect Invalid SQE Parameter";
309 case (offsetof(struct nvmf_connect_command, qid)):
310 dev_err(ctrl->device,
312 inv_sqe, cmd->connect.qid);
315 dev_err(ctrl->device,
316 "%s, starting byte offset: %d\n",
322 case NVME_SC_CONNECT_INVALID_HOST:
323 dev_err(ctrl->device,
324 "Connect for subsystem %s is not allowed, hostnqn: %s\n",
325 data->subsysnqn, data->hostnqn);
328 case NVME_SC_CONNECT_CTRL_BUSY:
329 dev_err(ctrl->device,
330 "Connect command failed: controller is busy or not available\n");
333 case NVME_SC_CONNECT_FORMAT:
334 dev_err(ctrl->device,
335 "Connect incompatible format: %d",
336 cmd->connect.recfmt);
340 dev_err(ctrl->device,
341 "Connect command failed, error wo/DNR bit: %d\n",
344 } /* switch (err_sctype) */
348 * nvmf_connect_admin_queue() - NVMe Fabrics Admin Queue "Connect"
350 * @ctrl: Host nvme controller instance used to request
351 * a new NVMe controller allocation on the target
352 * system and establish an NVMe Admin connection to
355 * This function enables an NVMe host device to request a new allocation of
356 * an NVMe controller resource on a target system as well establish a
357 * fabrics-protocol connection of the NVMe Admin queue between the
358 * host system device and the allocated NVMe controller on the
359 * target system via a NVMe Fabrics "Connect" command.
363 * > 0: NVMe error status code
364 * < 0: Linux errno error code
367 int nvmf_connect_admin_queue(struct nvme_ctrl *ctrl)
369 struct nvme_command cmd;
370 union nvme_result res;
371 struct nvmf_connect_data *data;
374 memset(&cmd, 0, sizeof(cmd));
375 cmd.connect.opcode = nvme_fabrics_command;
376 cmd.connect.fctype = nvme_fabrics_type_connect;
378 cmd.connect.sqsize = cpu_to_le16(NVME_AQ_DEPTH - 1);
381 * Set keep-alive timeout in seconds granularity (ms * 1000)
383 cmd.connect.kato = cpu_to_le32(ctrl->kato * 1000);
385 if (ctrl->opts->disable_sqflow)
386 cmd.connect.cattr |= NVME_CONNECT_DISABLE_SQFLOW;
388 data = kzalloc(sizeof(*data), GFP_KERNEL);
392 uuid_copy(&data->hostid, &ctrl->opts->host->id);
393 data->cntlid = cpu_to_le16(0xffff);
394 strncpy(data->subsysnqn, ctrl->opts->subsysnqn, NVMF_NQN_SIZE);
395 strncpy(data->hostnqn, ctrl->opts->host->nqn, NVMF_NQN_SIZE);
397 ret = __nvme_submit_sync_cmd(ctrl->fabrics_q, &cmd, &res,
398 data, sizeof(*data), 0, NVME_QID_ANY, 1,
399 BLK_MQ_REQ_RESERVED | BLK_MQ_REQ_NOWAIT, false);
401 nvmf_log_connect_error(ctrl, ret, le32_to_cpu(res.u32),
406 ctrl->cntlid = le16_to_cpu(res.u16);
412 EXPORT_SYMBOL_GPL(nvmf_connect_admin_queue);
415 * nvmf_connect_io_queue() - NVMe Fabrics I/O Queue "Connect"
417 * @ctrl: Host nvme controller instance used to establish an
418 * NVMe I/O queue connection to the already allocated NVMe
419 * controller on the target system.
420 * @qid: NVMe I/O queue number for the new I/O connection between
421 * host and target (note qid == 0 is illegal as this is
422 * the Admin queue, per NVMe standard).
423 * @poll: Whether or not to poll for the completion of the connect cmd.
425 * This function issues a fabrics-protocol connection
426 * of a NVMe I/O queue (via NVMe Fabrics "Connect" command)
427 * between the host system device and the allocated NVMe controller
428 * on the target system.
432 * > 0: NVMe error status code
433 * < 0: Linux errno error code
435 int nvmf_connect_io_queue(struct nvme_ctrl *ctrl, u16 qid, bool poll)
437 struct nvme_command cmd;
438 struct nvmf_connect_data *data;
439 union nvme_result res;
442 memset(&cmd, 0, sizeof(cmd));
443 cmd.connect.opcode = nvme_fabrics_command;
444 cmd.connect.fctype = nvme_fabrics_type_connect;
445 cmd.connect.qid = cpu_to_le16(qid);
446 cmd.connect.sqsize = cpu_to_le16(ctrl->sqsize);
448 if (ctrl->opts->disable_sqflow)
449 cmd.connect.cattr |= NVME_CONNECT_DISABLE_SQFLOW;
451 data = kzalloc(sizeof(*data), GFP_KERNEL);
455 uuid_copy(&data->hostid, &ctrl->opts->host->id);
456 data->cntlid = cpu_to_le16(ctrl->cntlid);
457 strncpy(data->subsysnqn, ctrl->opts->subsysnqn, NVMF_NQN_SIZE);
458 strncpy(data->hostnqn, ctrl->opts->host->nqn, NVMF_NQN_SIZE);
460 ret = __nvme_submit_sync_cmd(ctrl->connect_q, &cmd, &res,
461 data, sizeof(*data), 0, qid, 1,
462 BLK_MQ_REQ_RESERVED | BLK_MQ_REQ_NOWAIT, poll);
464 nvmf_log_connect_error(ctrl, ret, le32_to_cpu(res.u32),
470 EXPORT_SYMBOL_GPL(nvmf_connect_io_queue);
472 bool nvmf_should_reconnect(struct nvme_ctrl *ctrl)
474 if (ctrl->opts->max_reconnects == -1 ||
475 ctrl->nr_reconnects < ctrl->opts->max_reconnects)
480 EXPORT_SYMBOL_GPL(nvmf_should_reconnect);
483 * nvmf_register_transport() - NVMe Fabrics Library registration function.
484 * @ops: Transport ops instance to be registered to the
485 * common fabrics library.
487 * API function that registers the type of specific transport fabric
488 * being implemented to the common NVMe fabrics library. Part of
489 * the overall init sequence of starting up a fabrics driver.
491 int nvmf_register_transport(struct nvmf_transport_ops *ops)
493 if (!ops->create_ctrl)
496 down_write(&nvmf_transports_rwsem);
497 list_add_tail(&ops->entry, &nvmf_transports);
498 up_write(&nvmf_transports_rwsem);
502 EXPORT_SYMBOL_GPL(nvmf_register_transport);
505 * nvmf_unregister_transport() - NVMe Fabrics Library unregistration function.
506 * @ops: Transport ops instance to be unregistered from the
507 * common fabrics library.
509 * Fabrics API function that unregisters the type of specific transport
510 * fabric being implemented from the common NVMe fabrics library.
511 * Part of the overall exit sequence of unloading the implemented driver.
513 void nvmf_unregister_transport(struct nvmf_transport_ops *ops)
515 down_write(&nvmf_transports_rwsem);
516 list_del(&ops->entry);
517 up_write(&nvmf_transports_rwsem);
519 EXPORT_SYMBOL_GPL(nvmf_unregister_transport);
521 static struct nvmf_transport_ops *nvmf_lookup_transport(
522 struct nvmf_ctrl_options *opts)
524 struct nvmf_transport_ops *ops;
526 lockdep_assert_held(&nvmf_transports_rwsem);
528 list_for_each_entry(ops, &nvmf_transports, entry) {
529 if (strcmp(ops->name, opts->transport) == 0)
537 * For something we're not in a state to send to the device the default action
538 * is to busy it and retry it after the controller state is recovered. However,
539 * if the controller is deleting or if anything is marked for failfast or
540 * nvme multipath it is immediately failed.
542 * Note: commands used to initialize the controller will be marked for failfast.
543 * Note: nvme cli/ioctl commands are marked for failfast.
545 blk_status_t nvmf_fail_nonready_command(struct nvme_ctrl *ctrl,
548 if (ctrl->state != NVME_CTRL_DELETING_NOIO &&
549 ctrl->state != NVME_CTRL_DEAD &&
550 !test_bit(NVME_CTRL_FAILFAST_EXPIRED, &ctrl->flags) &&
551 !blk_noretry_request(rq) && !(rq->cmd_flags & REQ_NVME_MPATH))
552 return BLK_STS_RESOURCE;
553 return nvme_host_path_error(rq);
555 EXPORT_SYMBOL_GPL(nvmf_fail_nonready_command);
557 bool __nvmf_check_ready(struct nvme_ctrl *ctrl, struct request *rq,
560 struct nvme_request *req = nvme_req(rq);
563 * currently we have a problem sending passthru commands
564 * on the admin_q if the controller is not LIVE because we can't
565 * make sure that they are going out after the admin connect,
566 * controller enable and/or other commands in the initialization
567 * sequence. until the controller will be LIVE, fail with
568 * BLK_STS_RESOURCE so that they will be rescheduled.
570 if (rq->q == ctrl->admin_q && (req->flags & NVME_REQ_USERCMD))
574 * Only allow commands on a live queue, except for the connect command,
575 * which is require to set the queue live in the appropinquate states.
577 switch (ctrl->state) {
578 case NVME_CTRL_CONNECTING:
579 if (blk_rq_is_passthrough(rq) && nvme_is_fabrics(req->cmd) &&
580 req->cmd->fabrics.fctype == nvme_fabrics_type_connect)
591 EXPORT_SYMBOL_GPL(__nvmf_check_ready);
593 static const match_table_t opt_tokens = {
594 { NVMF_OPT_TRANSPORT, "transport=%s" },
595 { NVMF_OPT_TRADDR, "traddr=%s" },
596 { NVMF_OPT_TRSVCID, "trsvcid=%s" },
597 { NVMF_OPT_NQN, "nqn=%s" },
598 { NVMF_OPT_QUEUE_SIZE, "queue_size=%d" },
599 { NVMF_OPT_NR_IO_QUEUES, "nr_io_queues=%d" },
600 { NVMF_OPT_RECONNECT_DELAY, "reconnect_delay=%d" },
601 { NVMF_OPT_CTRL_LOSS_TMO, "ctrl_loss_tmo=%d" },
602 { NVMF_OPT_KATO, "keep_alive_tmo=%d" },
603 { NVMF_OPT_HOSTNQN, "hostnqn=%s" },
604 { NVMF_OPT_HOST_TRADDR, "host_traddr=%s" },
605 { NVMF_OPT_HOST_ID, "hostid=%s" },
606 { NVMF_OPT_DUP_CONNECT, "duplicate_connect" },
607 { NVMF_OPT_DISABLE_SQFLOW, "disable_sqflow" },
608 { NVMF_OPT_HDR_DIGEST, "hdr_digest" },
609 { NVMF_OPT_DATA_DIGEST, "data_digest" },
610 { NVMF_OPT_NR_WRITE_QUEUES, "nr_write_queues=%d" },
611 { NVMF_OPT_NR_POLL_QUEUES, "nr_poll_queues=%d" },
612 { NVMF_OPT_TOS, "tos=%d" },
613 { NVMF_OPT_FAIL_FAST_TMO, "fast_io_fail_tmo=%d" },
614 { NVMF_OPT_ERR, NULL }
617 static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
620 substring_t args[MAX_OPT_ARGS];
621 char *options, *o, *p;
624 int ctrl_loss_tmo = NVMF_DEF_CTRL_LOSS_TMO;
628 opts->queue_size = NVMF_DEF_QUEUE_SIZE;
629 opts->nr_io_queues = num_online_cpus();
630 opts->reconnect_delay = NVMF_DEF_RECONNECT_DELAY;
632 opts->duplicate_connect = false;
633 opts->fast_io_fail_tmo = NVMF_DEF_FAIL_FAST_TMO;
634 opts->hdr_digest = false;
635 opts->data_digest = false;
636 opts->tos = -1; /* < 0 == use transport default */
638 options = o = kstrdup(buf, GFP_KERNEL);
644 while ((p = strsep(&o, ",\n")) != NULL) {
648 token = match_token(p, opt_tokens, args);
651 case NVMF_OPT_TRANSPORT:
652 p = match_strdup(args);
657 kfree(opts->transport);
661 p = match_strdup(args);
666 kfree(opts->subsysnqn);
668 nqnlen = strlen(opts->subsysnqn);
669 if (nqnlen >= NVMF_NQN_SIZE) {
670 pr_err("%s needs to be < %d bytes\n",
671 opts->subsysnqn, NVMF_NQN_SIZE);
675 opts->discovery_nqn =
676 !(strcmp(opts->subsysnqn,
677 NVME_DISC_SUBSYS_NAME));
679 case NVMF_OPT_TRADDR:
680 p = match_strdup(args);
688 case NVMF_OPT_TRSVCID:
689 p = match_strdup(args);
694 kfree(opts->trsvcid);
697 case NVMF_OPT_QUEUE_SIZE:
698 if (match_int(args, &token)) {
702 if (token < NVMF_MIN_QUEUE_SIZE ||
703 token > NVMF_MAX_QUEUE_SIZE) {
704 pr_err("Invalid queue_size %d\n", token);
708 opts->queue_size = token;
710 case NVMF_OPT_NR_IO_QUEUES:
711 if (match_int(args, &token)) {
716 pr_err("Invalid number of IOQs %d\n", token);
720 if (opts->discovery_nqn) {
721 pr_debug("Ignoring nr_io_queues value for discovery controller\n");
725 opts->nr_io_queues = min_t(unsigned int,
726 num_online_cpus(), token);
729 if (match_int(args, &token)) {
735 pr_err("Invalid keep_alive_tmo %d\n", token);
738 } else if (token == 0 && !opts->discovery_nqn) {
739 /* Allowed for debug */
740 pr_warn("keep_alive_tmo 0 won't execute keep alives!!!\n");
744 case NVMF_OPT_CTRL_LOSS_TMO:
745 if (match_int(args, &token)) {
751 pr_warn("ctrl_loss_tmo < 0 will reconnect forever\n");
752 ctrl_loss_tmo = token;
754 case NVMF_OPT_FAIL_FAST_TMO:
755 if (match_int(args, &token)) {
761 pr_warn("I/O fail on reconnect controller after %d sec\n",
763 opts->fast_io_fail_tmo = token;
765 case NVMF_OPT_HOSTNQN:
767 pr_err("hostnqn already user-assigned: %s\n",
772 p = match_strdup(args);
778 if (nqnlen >= NVMF_NQN_SIZE) {
779 pr_err("%s needs to be < %d bytes\n",
785 nvmf_host_put(opts->host);
786 opts->host = nvmf_host_add(p);
793 case NVMF_OPT_RECONNECT_DELAY:
794 if (match_int(args, &token)) {
799 pr_err("Invalid reconnect_delay %d\n", token);
803 opts->reconnect_delay = token;
805 case NVMF_OPT_HOST_TRADDR:
806 p = match_strdup(args);
811 kfree(opts->host_traddr);
812 opts->host_traddr = p;
814 case NVMF_OPT_HOST_ID:
815 p = match_strdup(args);
820 ret = uuid_parse(p, &hostid);
822 pr_err("Invalid hostid %s\n", p);
829 case NVMF_OPT_DUP_CONNECT:
830 opts->duplicate_connect = true;
832 case NVMF_OPT_DISABLE_SQFLOW:
833 opts->disable_sqflow = true;
835 case NVMF_OPT_HDR_DIGEST:
836 opts->hdr_digest = true;
838 case NVMF_OPT_DATA_DIGEST:
839 opts->data_digest = true;
841 case NVMF_OPT_NR_WRITE_QUEUES:
842 if (match_int(args, &token)) {
847 pr_err("Invalid nr_write_queues %d\n", token);
851 opts->nr_write_queues = token;
853 case NVMF_OPT_NR_POLL_QUEUES:
854 if (match_int(args, &token)) {
859 pr_err("Invalid nr_poll_queues %d\n", token);
863 opts->nr_poll_queues = token;
866 if (match_int(args, &token)) {
871 pr_err("Invalid type of service %d\n", token);
876 pr_warn("Clamping type of service to 255\n");
882 pr_warn("unknown parameter or missing value '%s' in ctrl creation request\n",
889 if (opts->discovery_nqn) {
890 opts->nr_io_queues = 0;
891 opts->nr_write_queues = 0;
892 opts->nr_poll_queues = 0;
893 opts->duplicate_connect = true;
896 opts->kato = NVME_DEFAULT_KATO;
898 if (ctrl_loss_tmo < 0) {
899 opts->max_reconnects = -1;
901 opts->max_reconnects = DIV_ROUND_UP(ctrl_loss_tmo,
902 opts->reconnect_delay);
903 if (ctrl_loss_tmo < opts->fast_io_fail_tmo)
904 pr_warn("failfast tmo (%d) larger than controller loss tmo (%d)\n",
905 opts->fast_io_fail_tmo, ctrl_loss_tmo);
909 kref_get(&nvmf_default_host->ref);
910 opts->host = nvmf_default_host;
913 uuid_copy(&opts->host->id, &hostid);
920 static int nvmf_check_required_opts(struct nvmf_ctrl_options *opts,
921 unsigned int required_opts)
923 if ((opts->mask & required_opts) != required_opts) {
926 for (i = 0; i < ARRAY_SIZE(opt_tokens); i++) {
927 if ((opt_tokens[i].token & required_opts) &&
928 !(opt_tokens[i].token & opts->mask)) {
929 pr_warn("missing parameter '%s'\n",
930 opt_tokens[i].pattern);
940 bool nvmf_ip_options_match(struct nvme_ctrl *ctrl,
941 struct nvmf_ctrl_options *opts)
943 if (!nvmf_ctlr_matches_baseopts(ctrl, opts) ||
944 strcmp(opts->traddr, ctrl->opts->traddr) ||
945 strcmp(opts->trsvcid, ctrl->opts->trsvcid))
949 * Checking the local address is rough. In most cases, none is specified
950 * and the host port is selected by the stack.
952 * Assume no match if:
953 * - local address is specified and address is not the same
954 * - local address is not specified but remote is, or vice versa
955 * (admin using specific host_traddr when it matters).
957 if ((opts->mask & NVMF_OPT_HOST_TRADDR) &&
958 (ctrl->opts->mask & NVMF_OPT_HOST_TRADDR)) {
959 if (strcmp(opts->host_traddr, ctrl->opts->host_traddr))
961 } else if ((opts->mask & NVMF_OPT_HOST_TRADDR) ||
962 (ctrl->opts->mask & NVMF_OPT_HOST_TRADDR)) {
968 EXPORT_SYMBOL_GPL(nvmf_ip_options_match);
970 static int nvmf_check_allowed_opts(struct nvmf_ctrl_options *opts,
971 unsigned int allowed_opts)
973 if (opts->mask & ~allowed_opts) {
976 for (i = 0; i < ARRAY_SIZE(opt_tokens); i++) {
977 if ((opt_tokens[i].token & opts->mask) &&
978 (opt_tokens[i].token & ~allowed_opts)) {
979 pr_warn("invalid parameter '%s'\n",
980 opt_tokens[i].pattern);
990 void nvmf_free_options(struct nvmf_ctrl_options *opts)
992 nvmf_host_put(opts->host);
993 kfree(opts->transport);
995 kfree(opts->trsvcid);
996 kfree(opts->subsysnqn);
997 kfree(opts->host_traddr);
1000 EXPORT_SYMBOL_GPL(nvmf_free_options);
1002 #define NVMF_REQUIRED_OPTS (NVMF_OPT_TRANSPORT | NVMF_OPT_NQN)
1003 #define NVMF_ALLOWED_OPTS (NVMF_OPT_QUEUE_SIZE | NVMF_OPT_NR_IO_QUEUES | \
1004 NVMF_OPT_KATO | NVMF_OPT_HOSTNQN | \
1005 NVMF_OPT_HOST_ID | NVMF_OPT_DUP_CONNECT |\
1006 NVMF_OPT_DISABLE_SQFLOW |\
1007 NVMF_OPT_FAIL_FAST_TMO)
1009 static struct nvme_ctrl *
1010 nvmf_create_ctrl(struct device *dev, const char *buf)
1012 struct nvmf_ctrl_options *opts;
1013 struct nvmf_transport_ops *ops;
1014 struct nvme_ctrl *ctrl;
1017 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1019 return ERR_PTR(-ENOMEM);
1021 ret = nvmf_parse_options(opts, buf);
1026 request_module("nvme-%s", opts->transport);
1029 * Check the generic options first as we need a valid transport for
1030 * the lookup below. Then clear the generic flags so that transport
1031 * drivers don't have to care about them.
1033 ret = nvmf_check_required_opts(opts, NVMF_REQUIRED_OPTS);
1036 opts->mask &= ~NVMF_REQUIRED_OPTS;
1038 down_read(&nvmf_transports_rwsem);
1039 ops = nvmf_lookup_transport(opts);
1041 pr_info("no handler found for transport %s.\n",
1047 if (!try_module_get(ops->module)) {
1051 up_read(&nvmf_transports_rwsem);
1053 ret = nvmf_check_required_opts(opts, ops->required_opts);
1055 goto out_module_put;
1056 ret = nvmf_check_allowed_opts(opts, NVMF_ALLOWED_OPTS |
1057 ops->allowed_opts | ops->required_opts);
1059 goto out_module_put;
1061 ctrl = ops->create_ctrl(dev, opts);
1063 ret = PTR_ERR(ctrl);
1064 goto out_module_put;
1067 module_put(ops->module);
1071 module_put(ops->module);
1074 up_read(&nvmf_transports_rwsem);
1076 nvmf_free_options(opts);
1077 return ERR_PTR(ret);
1080 static struct class *nvmf_class;
1081 static struct device *nvmf_device;
1082 static DEFINE_MUTEX(nvmf_dev_mutex);
1084 static ssize_t nvmf_dev_write(struct file *file, const char __user *ubuf,
1085 size_t count, loff_t *pos)
1087 struct seq_file *seq_file = file->private_data;
1088 struct nvme_ctrl *ctrl;
1092 if (count > PAGE_SIZE)
1095 buf = memdup_user_nul(ubuf, count);
1097 return PTR_ERR(buf);
1099 mutex_lock(&nvmf_dev_mutex);
1100 if (seq_file->private) {
1105 ctrl = nvmf_create_ctrl(nvmf_device, buf);
1107 ret = PTR_ERR(ctrl);
1111 seq_file->private = ctrl;
1114 mutex_unlock(&nvmf_dev_mutex);
1116 return ret ? ret : count;
1119 static int nvmf_dev_show(struct seq_file *seq_file, void *private)
1121 struct nvme_ctrl *ctrl;
1124 mutex_lock(&nvmf_dev_mutex);
1125 ctrl = seq_file->private;
1131 seq_printf(seq_file, "instance=%d,cntlid=%d\n",
1132 ctrl->instance, ctrl->cntlid);
1135 mutex_unlock(&nvmf_dev_mutex);
1139 static int nvmf_dev_open(struct inode *inode, struct file *file)
1142 * The miscdevice code initializes file->private_data, but doesn't
1143 * make use of it later.
1145 file->private_data = NULL;
1146 return single_open(file, nvmf_dev_show, NULL);
1149 static int nvmf_dev_release(struct inode *inode, struct file *file)
1151 struct seq_file *seq_file = file->private_data;
1152 struct nvme_ctrl *ctrl = seq_file->private;
1155 nvme_put_ctrl(ctrl);
1156 return single_release(inode, file);
1159 static const struct file_operations nvmf_dev_fops = {
1160 .owner = THIS_MODULE,
1161 .write = nvmf_dev_write,
1163 .open = nvmf_dev_open,
1164 .release = nvmf_dev_release,
1167 static struct miscdevice nvmf_misc = {
1168 .minor = MISC_DYNAMIC_MINOR,
1169 .name = "nvme-fabrics",
1170 .fops = &nvmf_dev_fops,
1173 static int __init nvmf_init(void)
1177 nvmf_default_host = nvmf_host_default();
1178 if (!nvmf_default_host)
1181 nvmf_class = class_create(THIS_MODULE, "nvme-fabrics");
1182 if (IS_ERR(nvmf_class)) {
1183 pr_err("couldn't register class nvme-fabrics\n");
1184 ret = PTR_ERR(nvmf_class);
1189 device_create(nvmf_class, NULL, MKDEV(0, 0), NULL, "ctl");
1190 if (IS_ERR(nvmf_device)) {
1191 pr_err("couldn't create nvme-fabris device!\n");
1192 ret = PTR_ERR(nvmf_device);
1193 goto out_destroy_class;
1196 ret = misc_register(&nvmf_misc);
1198 pr_err("couldn't register misc device: %d\n", ret);
1199 goto out_destroy_device;
1205 device_destroy(nvmf_class, MKDEV(0, 0));
1207 class_destroy(nvmf_class);
1209 nvmf_host_put(nvmf_default_host);
1213 static void __exit nvmf_exit(void)
1215 misc_deregister(&nvmf_misc);
1216 device_destroy(nvmf_class, MKDEV(0, 0));
1217 class_destroy(nvmf_class);
1218 nvmf_host_put(nvmf_default_host);
1220 BUILD_BUG_ON(sizeof(struct nvmf_common_command) != 64);
1221 BUILD_BUG_ON(sizeof(struct nvmf_connect_command) != 64);
1222 BUILD_BUG_ON(sizeof(struct nvmf_property_get_command) != 64);
1223 BUILD_BUG_ON(sizeof(struct nvmf_property_set_command) != 64);
1224 BUILD_BUG_ON(sizeof(struct nvmf_connect_data) != 1024);
1227 MODULE_LICENSE("GPL v2");
1229 module_init(nvmf_init);
1230 module_exit(nvmf_exit);