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 DEFINE_MUTEX(nvmf_transports_mutex);
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 memcpy(host->nqn, hostnqn, NVMF_NQN_SIZE);
61 uuid_be_gen(&host->id);
63 list_add_tail(&host->list, &nvmf_hosts);
65 mutex_unlock(&nvmf_hosts_mutex);
69 static struct nvmf_host *nvmf_host_default(void)
71 struct nvmf_host *host;
73 host = kmalloc(sizeof(*host), GFP_KERNEL);
77 kref_init(&host->ref);
78 uuid_be_gen(&host->id);
79 snprintf(host->nqn, NVMF_NQN_SIZE,
80 "nqn.2014-08.org.nvmexpress:NVMf:uuid:%pUb", &host->id);
82 mutex_lock(&nvmf_hosts_mutex);
83 list_add_tail(&host->list, &nvmf_hosts);
84 mutex_unlock(&nvmf_hosts_mutex);
89 static void nvmf_host_destroy(struct kref *ref)
91 struct nvmf_host *host = container_of(ref, struct nvmf_host, ref);
93 mutex_lock(&nvmf_hosts_mutex);
94 list_del(&host->list);
95 mutex_unlock(&nvmf_hosts_mutex);
100 static void nvmf_host_put(struct nvmf_host *host)
103 kref_put(&host->ref, nvmf_host_destroy);
107 * nvmf_get_address() - Get address/port
108 * @ctrl: Host NVMe controller instance which we got the address
109 * @buf: OUTPUT parameter that will contain the address/port
112 int nvmf_get_address(struct nvme_ctrl *ctrl, char *buf, int size)
116 if (ctrl->opts->mask & NVMF_OPT_TRADDR)
117 len += snprintf(buf, size, "traddr=%s", ctrl->opts->traddr);
118 if (ctrl->opts->mask & NVMF_OPT_TRSVCID)
119 len += snprintf(buf + len, size - len, "%strsvcid=%s",
120 (len) ? "," : "", ctrl->opts->trsvcid);
121 if (ctrl->opts->mask & NVMF_OPT_HOST_TRADDR)
122 len += snprintf(buf + len, size - len, "%shost_traddr=%s",
123 (len) ? "," : "", ctrl->opts->host_traddr);
124 len += snprintf(buf + len, size - len, "\n");
128 EXPORT_SYMBOL_GPL(nvmf_get_address);
131 * nvmf_get_subsysnqn() - Get subsystem NQN
132 * @ctrl: Host NVMe controller instance which we got the NQN
134 const char *nvmf_get_subsysnqn(struct nvme_ctrl *ctrl)
136 return ctrl->opts->subsysnqn;
138 EXPORT_SYMBOL_GPL(nvmf_get_subsysnqn);
141 * nvmf_reg_read32() - NVMe Fabrics "Property Get" API function.
142 * @ctrl: Host NVMe controller instance maintaining the admin
143 * queue used to submit the property read command to
144 * the allocated NVMe controller resource on the target system.
145 * @off: Starting offset value of the targeted property
146 * register (see the fabrics section of the NVMe standard).
147 * @val: OUTPUT parameter that will contain the value of
148 * the property after a successful read.
150 * Used by the host system to retrieve a 32-bit capsule property value
151 * from an NVMe controller on the target system.
153 * ("Capsule property" is an "PCIe register concept" applied to the
154 * NVMe fabrics space.)
158 * > 0: NVMe error status code
159 * < 0: Linux errno error code
161 int nvmf_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val)
163 struct nvme_command cmd;
164 union nvme_result res;
167 memset(&cmd, 0, sizeof(cmd));
168 cmd.prop_get.opcode = nvme_fabrics_command;
169 cmd.prop_get.fctype = nvme_fabrics_type_property_get;
170 cmd.prop_get.offset = cpu_to_le32(off);
172 ret = __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, &res, NULL, 0, 0,
176 *val = le64_to_cpu(res.u64);
177 if (unlikely(ret != 0))
178 dev_err(ctrl->device,
179 "Property Get error: %d, offset %#x\n",
180 ret > 0 ? ret & ~NVME_SC_DNR : ret, off);
184 EXPORT_SYMBOL_GPL(nvmf_reg_read32);
187 * nvmf_reg_read64() - NVMe Fabrics "Property Get" API function.
188 * @ctrl: Host NVMe controller instance maintaining the admin
189 * queue used to submit the property read command to
190 * the allocated controller resource on the target system.
191 * @off: Starting offset value of the targeted property
192 * register (see the fabrics section of the NVMe standard).
193 * @val: OUTPUT parameter that will contain the value of
194 * the property after a successful read.
196 * Used by the host system to retrieve a 64-bit capsule property value
197 * from an NVMe controller on the target system.
199 * ("Capsule property" is an "PCIe register concept" applied to the
200 * NVMe fabrics space.)
204 * > 0: NVMe error status code
205 * < 0: Linux errno error code
207 int nvmf_reg_read64(struct nvme_ctrl *ctrl, u32 off, u64 *val)
209 struct nvme_command cmd;
210 union nvme_result res;
213 memset(&cmd, 0, sizeof(cmd));
214 cmd.prop_get.opcode = nvme_fabrics_command;
215 cmd.prop_get.fctype = nvme_fabrics_type_property_get;
216 cmd.prop_get.attrib = 1;
217 cmd.prop_get.offset = cpu_to_le32(off);
219 ret = __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, &res, NULL, 0, 0,
223 *val = le64_to_cpu(res.u64);
224 if (unlikely(ret != 0))
225 dev_err(ctrl->device,
226 "Property Get error: %d, offset %#x\n",
227 ret > 0 ? ret & ~NVME_SC_DNR : ret, off);
230 EXPORT_SYMBOL_GPL(nvmf_reg_read64);
233 * nvmf_reg_write32() - NVMe Fabrics "Property Write" API function.
234 * @ctrl: Host NVMe controller instance maintaining the admin
235 * queue used to submit the property read command to
236 * the allocated NVMe controller resource on the target system.
237 * @off: Starting offset value of the targeted property
238 * register (see the fabrics section of the NVMe standard).
239 * @val: Input parameter that contains the value to be
240 * written to the property.
242 * Used by the NVMe host system to write a 32-bit capsule property value
243 * to an NVMe controller on the target system.
245 * ("Capsule property" is an "PCIe register concept" applied to the
246 * NVMe fabrics space.)
249 * 0: successful write
250 * > 0: NVMe error status code
251 * < 0: Linux errno error code
253 int nvmf_reg_write32(struct nvme_ctrl *ctrl, u32 off, u32 val)
255 struct nvme_command cmd;
258 memset(&cmd, 0, sizeof(cmd));
259 cmd.prop_set.opcode = nvme_fabrics_command;
260 cmd.prop_set.fctype = nvme_fabrics_type_property_set;
261 cmd.prop_set.attrib = 0;
262 cmd.prop_set.offset = cpu_to_le32(off);
263 cmd.prop_set.value = cpu_to_le64(val);
265 ret = __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, NULL, NULL, 0, 0,
268 dev_err(ctrl->device,
269 "Property Set error: %d, offset %#x\n",
270 ret > 0 ? ret & ~NVME_SC_DNR : ret, off);
273 EXPORT_SYMBOL_GPL(nvmf_reg_write32);
276 * nvmf_log_connect_error() - Error-parsing-diagnostic print
277 * out function for connect() errors.
279 * @ctrl: the specific /dev/nvmeX device that had the error.
281 * @errval: Error code to be decoded in a more human-friendly
284 * @offset: For use with the NVMe error code NVME_SC_CONNECT_INVALID_PARAM.
286 * @cmd: This is the SQE portion of a submission capsule.
288 * @data: This is the "Data" portion of a submission capsule.
290 static void nvmf_log_connect_error(struct nvme_ctrl *ctrl,
291 int errval, int offset, struct nvme_command *cmd,
292 struct nvmf_connect_data *data)
294 int err_sctype = errval & (~NVME_SC_DNR);
296 switch (err_sctype) {
298 case (NVME_SC_CONNECT_INVALID_PARAM):
300 char *inv_data = "Connect Invalid Data Parameter";
302 switch (offset & 0xffff) {
303 case (offsetof(struct nvmf_connect_data, cntlid)):
304 dev_err(ctrl->device,
306 inv_data, data->cntlid);
308 case (offsetof(struct nvmf_connect_data, hostnqn)):
309 dev_err(ctrl->device,
310 "%s, hostnqn \"%s\"\n",
311 inv_data, data->hostnqn);
313 case (offsetof(struct nvmf_connect_data, subsysnqn)):
314 dev_err(ctrl->device,
315 "%s, subsysnqn \"%s\"\n",
316 inv_data, data->subsysnqn);
319 dev_err(ctrl->device,
320 "%s, starting byte offset: %d\n",
321 inv_data, offset & 0xffff);
325 char *inv_sqe = "Connect Invalid SQE Parameter";
328 case (offsetof(struct nvmf_connect_command, qid)):
329 dev_err(ctrl->device,
331 inv_sqe, cmd->connect.qid);
334 dev_err(ctrl->device,
335 "%s, starting byte offset: %d\n",
341 dev_err(ctrl->device,
342 "Connect command failed, error wo/DNR bit: %d\n",
345 } /* switch (err_sctype) */
349 * nvmf_connect_admin_queue() - NVMe Fabrics Admin Queue "Connect"
351 * @ctrl: Host nvme controller instance used to request
352 * a new NVMe controller allocation on the target
353 * system and establish an NVMe Admin connection to
356 * This function enables an NVMe host device to request a new allocation of
357 * an NVMe controller resource on a target system as well establish a
358 * fabrics-protocol connection of the NVMe Admin queue between the
359 * host system device and the allocated NVMe controller on the
360 * target system via a NVMe Fabrics "Connect" command.
364 * > 0: NVMe error status code
365 * < 0: Linux errno error code
368 int nvmf_connect_admin_queue(struct nvme_ctrl *ctrl)
370 struct nvme_command cmd;
371 union nvme_result res;
372 struct nvmf_connect_data *data;
375 memset(&cmd, 0, sizeof(cmd));
376 cmd.connect.opcode = nvme_fabrics_command;
377 cmd.connect.fctype = nvme_fabrics_type_connect;
381 * fabrics spec sets a minimum of depth 32 for admin queue,
382 * so set the queue with this depth always until
383 * justification otherwise.
385 cmd.connect.sqsize = cpu_to_le16(NVMF_AQ_DEPTH - 1);
388 * Set keep-alive timeout in seconds granularity (ms * 1000)
389 * and add a grace period for controller kato enforcement
391 cmd.connect.kato = ctrl->opts->discovery_nqn ? 0 :
392 cpu_to_le32((ctrl->kato + NVME_KATO_GRACE) * 1000);
394 data = kzalloc(sizeof(*data), GFP_KERNEL);
398 memcpy(&data->hostid, &ctrl->opts->host->id, sizeof(uuid_be));
399 data->cntlid = cpu_to_le16(0xffff);
400 strncpy(data->subsysnqn, ctrl->opts->subsysnqn, NVMF_NQN_SIZE);
401 strncpy(data->hostnqn, ctrl->opts->host->nqn, NVMF_NQN_SIZE);
403 ret = __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, &res,
404 data, sizeof(*data), 0, NVME_QID_ANY, 1,
405 BLK_MQ_REQ_RESERVED | BLK_MQ_REQ_NOWAIT);
407 nvmf_log_connect_error(ctrl, ret, le32_to_cpu(res.u32),
412 ctrl->cntlid = le16_to_cpu(res.u16);
418 EXPORT_SYMBOL_GPL(nvmf_connect_admin_queue);
421 * nvmf_connect_io_queue() - NVMe Fabrics I/O Queue "Connect"
423 * @ctrl: Host nvme controller instance used to establish an
424 * NVMe I/O queue connection to the already allocated NVMe
425 * controller on the target system.
426 * @qid: NVMe I/O queue number for the new I/O connection between
427 * host and target (note qid == 0 is illegal as this is
428 * the Admin queue, per NVMe standard).
430 * This function issues a fabrics-protocol connection
431 * of a NVMe I/O queue (via NVMe Fabrics "Connect" command)
432 * between the host system device and the allocated NVMe controller
433 * on the target system.
437 * > 0: NVMe error status code
438 * < 0: Linux errno error code
440 int nvmf_connect_io_queue(struct nvme_ctrl *ctrl, u16 qid)
442 struct nvme_command cmd;
443 struct nvmf_connect_data *data;
444 union nvme_result res;
447 memset(&cmd, 0, sizeof(cmd));
448 cmd.connect.opcode = nvme_fabrics_command;
449 cmd.connect.fctype = nvme_fabrics_type_connect;
450 cmd.connect.qid = cpu_to_le16(qid);
451 cmd.connect.sqsize = cpu_to_le16(ctrl->sqsize);
453 data = kzalloc(sizeof(*data), GFP_KERNEL);
457 memcpy(&data->hostid, &ctrl->opts->host->id, sizeof(uuid_be));
458 data->cntlid = cpu_to_le16(ctrl->cntlid);
459 strncpy(data->subsysnqn, ctrl->opts->subsysnqn, NVMF_NQN_SIZE);
460 strncpy(data->hostnqn, ctrl->opts->host->nqn, NVMF_NQN_SIZE);
462 ret = __nvme_submit_sync_cmd(ctrl->connect_q, &cmd, &res,
463 data, sizeof(*data), 0, qid, 1,
464 BLK_MQ_REQ_RESERVED | BLK_MQ_REQ_NOWAIT);
466 nvmf_log_connect_error(ctrl, ret, le32_to_cpu(res.u32),
472 EXPORT_SYMBOL_GPL(nvmf_connect_io_queue);
475 * nvmf_register_transport() - NVMe Fabrics Library registration function.
476 * @ops: Transport ops instance to be registered to the
477 * common fabrics library.
479 * API function that registers the type of specific transport fabric
480 * being implemented to the common NVMe fabrics library. Part of
481 * the overall init sequence of starting up a fabrics driver.
483 int nvmf_register_transport(struct nvmf_transport_ops *ops)
485 if (!ops->create_ctrl)
488 mutex_lock(&nvmf_transports_mutex);
489 list_add_tail(&ops->entry, &nvmf_transports);
490 mutex_unlock(&nvmf_transports_mutex);
494 EXPORT_SYMBOL_GPL(nvmf_register_transport);
497 * nvmf_unregister_transport() - NVMe Fabrics Library unregistration function.
498 * @ops: Transport ops instance to be unregistered from the
499 * common fabrics library.
501 * Fabrics API function that unregisters the type of specific transport
502 * fabric being implemented from the common NVMe fabrics library.
503 * Part of the overall exit sequence of unloading the implemented driver.
505 void nvmf_unregister_transport(struct nvmf_transport_ops *ops)
507 mutex_lock(&nvmf_transports_mutex);
508 list_del(&ops->entry);
509 mutex_unlock(&nvmf_transports_mutex);
511 EXPORT_SYMBOL_GPL(nvmf_unregister_transport);
513 static struct nvmf_transport_ops *nvmf_lookup_transport(
514 struct nvmf_ctrl_options *opts)
516 struct nvmf_transport_ops *ops;
518 lockdep_assert_held(&nvmf_transports_mutex);
520 list_for_each_entry(ops, &nvmf_transports, entry) {
521 if (strcmp(ops->name, opts->transport) == 0)
528 static const match_table_t opt_tokens = {
529 { NVMF_OPT_TRANSPORT, "transport=%s" },
530 { NVMF_OPT_TRADDR, "traddr=%s" },
531 { NVMF_OPT_TRSVCID, "trsvcid=%s" },
532 { NVMF_OPT_NQN, "nqn=%s" },
533 { NVMF_OPT_QUEUE_SIZE, "queue_size=%d" },
534 { NVMF_OPT_NR_IO_QUEUES, "nr_io_queues=%d" },
535 { NVMF_OPT_RECONNECT_DELAY, "reconnect_delay=%d" },
536 { NVMF_OPT_KATO, "keep_alive_tmo=%d" },
537 { NVMF_OPT_HOSTNQN, "hostnqn=%s" },
538 { NVMF_OPT_HOST_TRADDR, "host_traddr=%s" },
539 { NVMF_OPT_ERR, NULL }
542 static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
545 substring_t args[MAX_OPT_ARGS];
546 char *options, *o, *p;
551 opts->queue_size = NVMF_DEF_QUEUE_SIZE;
552 opts->nr_io_queues = num_online_cpus();
553 opts->reconnect_delay = NVMF_DEF_RECONNECT_DELAY;
555 options = o = kstrdup(buf, GFP_KERNEL);
559 while ((p = strsep(&o, ",\n")) != NULL) {
563 token = match_token(p, opt_tokens, args);
566 case NVMF_OPT_TRANSPORT:
567 p = match_strdup(args);
575 p = match_strdup(args);
581 nqnlen = strlen(opts->subsysnqn);
582 if (nqnlen >= NVMF_NQN_SIZE) {
583 pr_err("%s needs to be < %d bytes\n",
584 opts->subsysnqn, NVMF_NQN_SIZE);
588 opts->discovery_nqn =
589 !(strcmp(opts->subsysnqn,
590 NVME_DISC_SUBSYS_NAME));
591 if (opts->discovery_nqn)
592 opts->nr_io_queues = 0;
594 case NVMF_OPT_TRADDR:
595 p = match_strdup(args);
602 case NVMF_OPT_TRSVCID:
603 p = match_strdup(args);
610 case NVMF_OPT_QUEUE_SIZE:
611 if (match_int(args, &token)) {
615 if (token < NVMF_MIN_QUEUE_SIZE ||
616 token > NVMF_MAX_QUEUE_SIZE) {
617 pr_err("Invalid queue_size %d\n", token);
621 opts->queue_size = token;
623 case NVMF_OPT_NR_IO_QUEUES:
624 if (match_int(args, &token)) {
629 pr_err("Invalid number of IOQs %d\n", token);
633 opts->nr_io_queues = min_t(unsigned int,
634 num_online_cpus(), token);
637 if (match_int(args, &token)) {
642 if (opts->discovery_nqn) {
643 pr_err("Discovery controllers cannot accept keep_alive_tmo != 0\n");
649 pr_err("Invalid keep_alive_tmo %d\n", token);
652 } else if (token == 0) {
653 /* Allowed for debug */
654 pr_warn("keep_alive_tmo 0 won't execute keep alives!!!\n");
658 case NVMF_OPT_HOSTNQN:
660 pr_err("hostnqn already user-assigned: %s\n",
665 p = match_strdup(args);
671 if (nqnlen >= NVMF_NQN_SIZE) {
672 pr_err("%s needs to be < %d bytes\n",
678 opts->host = nvmf_host_add(p);
685 case NVMF_OPT_RECONNECT_DELAY:
686 if (match_int(args, &token)) {
691 pr_err("Invalid reconnect_delay %d\n", token);
695 opts->reconnect_delay = token;
697 case NVMF_OPT_HOST_TRADDR:
698 p = match_strdup(args);
703 opts->host_traddr = p;
706 pr_warn("unknown parameter or missing value '%s' in ctrl creation request\n",
714 kref_get(&nvmf_default_host->ref);
715 opts->host = nvmf_default_host;
719 if (!opts->discovery_nqn && !opts->kato)
720 opts->kato = NVME_DEFAULT_KATO;
725 static int nvmf_check_required_opts(struct nvmf_ctrl_options *opts,
726 unsigned int required_opts)
728 if ((opts->mask & required_opts) != required_opts) {
731 for (i = 0; i < ARRAY_SIZE(opt_tokens); i++) {
732 if ((opt_tokens[i].token & required_opts) &&
733 !(opt_tokens[i].token & opts->mask)) {
734 pr_warn("missing parameter '%s'\n",
735 opt_tokens[i].pattern);
745 static int nvmf_check_allowed_opts(struct nvmf_ctrl_options *opts,
746 unsigned int allowed_opts)
748 if (opts->mask & ~allowed_opts) {
751 for (i = 0; i < ARRAY_SIZE(opt_tokens); i++) {
752 if (opt_tokens[i].token & ~allowed_opts) {
753 pr_warn("invalid parameter '%s'\n",
754 opt_tokens[i].pattern);
764 void nvmf_free_options(struct nvmf_ctrl_options *opts)
766 nvmf_host_put(opts->host);
767 kfree(opts->transport);
769 kfree(opts->trsvcid);
770 kfree(opts->subsysnqn);
771 kfree(opts->host_traddr);
774 EXPORT_SYMBOL_GPL(nvmf_free_options);
776 #define NVMF_REQUIRED_OPTS (NVMF_OPT_TRANSPORT | NVMF_OPT_NQN)
777 #define NVMF_ALLOWED_OPTS (NVMF_OPT_QUEUE_SIZE | NVMF_OPT_NR_IO_QUEUES | \
778 NVMF_OPT_KATO | NVMF_OPT_HOSTNQN)
780 static struct nvme_ctrl *
781 nvmf_create_ctrl(struct device *dev, const char *buf, size_t count)
783 struct nvmf_ctrl_options *opts;
784 struct nvmf_transport_ops *ops;
785 struct nvme_ctrl *ctrl;
788 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
790 return ERR_PTR(-ENOMEM);
792 ret = nvmf_parse_options(opts, buf);
797 * Check the generic options first as we need a valid transport for
798 * the lookup below. Then clear the generic flags so that transport
799 * drivers don't have to care about them.
801 ret = nvmf_check_required_opts(opts, NVMF_REQUIRED_OPTS);
804 opts->mask &= ~NVMF_REQUIRED_OPTS;
806 mutex_lock(&nvmf_transports_mutex);
807 ops = nvmf_lookup_transport(opts);
809 pr_info("no handler found for transport %s.\n",
815 ret = nvmf_check_required_opts(opts, ops->required_opts);
818 ret = nvmf_check_allowed_opts(opts, NVMF_ALLOWED_OPTS |
819 ops->allowed_opts | ops->required_opts);
823 ctrl = ops->create_ctrl(dev, opts);
829 mutex_unlock(&nvmf_transports_mutex);
833 mutex_unlock(&nvmf_transports_mutex);
835 nvmf_free_options(opts);
839 static struct class *nvmf_class;
840 static struct device *nvmf_device;
841 static DEFINE_MUTEX(nvmf_dev_mutex);
843 static ssize_t nvmf_dev_write(struct file *file, const char __user *ubuf,
844 size_t count, loff_t *pos)
846 struct seq_file *seq_file = file->private_data;
847 struct nvme_ctrl *ctrl;
851 if (count > PAGE_SIZE)
854 buf = memdup_user_nul(ubuf, count);
858 mutex_lock(&nvmf_dev_mutex);
859 if (seq_file->private) {
864 ctrl = nvmf_create_ctrl(nvmf_device, buf, count);
870 seq_file->private = ctrl;
873 mutex_unlock(&nvmf_dev_mutex);
875 return ret ? ret : count;
878 static int nvmf_dev_show(struct seq_file *seq_file, void *private)
880 struct nvme_ctrl *ctrl;
883 mutex_lock(&nvmf_dev_mutex);
884 ctrl = seq_file->private;
890 seq_printf(seq_file, "instance=%d,cntlid=%d\n",
891 ctrl->instance, ctrl->cntlid);
894 mutex_unlock(&nvmf_dev_mutex);
898 static int nvmf_dev_open(struct inode *inode, struct file *file)
901 * The miscdevice code initializes file->private_data, but doesn't
902 * make use of it later.
904 file->private_data = NULL;
905 return single_open(file, nvmf_dev_show, NULL);
908 static int nvmf_dev_release(struct inode *inode, struct file *file)
910 struct seq_file *seq_file = file->private_data;
911 struct nvme_ctrl *ctrl = seq_file->private;
915 return single_release(inode, file);
918 static const struct file_operations nvmf_dev_fops = {
919 .owner = THIS_MODULE,
920 .write = nvmf_dev_write,
922 .open = nvmf_dev_open,
923 .release = nvmf_dev_release,
926 static struct miscdevice nvmf_misc = {
927 .minor = MISC_DYNAMIC_MINOR,
928 .name = "nvme-fabrics",
929 .fops = &nvmf_dev_fops,
932 static int __init nvmf_init(void)
936 nvmf_default_host = nvmf_host_default();
937 if (!nvmf_default_host)
940 nvmf_class = class_create(THIS_MODULE, "nvme-fabrics");
941 if (IS_ERR(nvmf_class)) {
942 pr_err("couldn't register class nvme-fabrics\n");
943 ret = PTR_ERR(nvmf_class);
948 device_create(nvmf_class, NULL, MKDEV(0, 0), NULL, "ctl");
949 if (IS_ERR(nvmf_device)) {
950 pr_err("couldn't create nvme-fabris device!\n");
951 ret = PTR_ERR(nvmf_device);
952 goto out_destroy_class;
955 ret = misc_register(&nvmf_misc);
957 pr_err("couldn't register misc device: %d\n", ret);
958 goto out_destroy_device;
964 device_destroy(nvmf_class, MKDEV(0, 0));
966 class_destroy(nvmf_class);
968 nvmf_host_put(nvmf_default_host);
972 static void __exit nvmf_exit(void)
974 misc_deregister(&nvmf_misc);
975 device_destroy(nvmf_class, MKDEV(0, 0));
976 class_destroy(nvmf_class);
977 nvmf_host_put(nvmf_default_host);
979 BUILD_BUG_ON(sizeof(struct nvmf_connect_command) != 64);
980 BUILD_BUG_ON(sizeof(struct nvmf_property_get_command) != 64);
981 BUILD_BUG_ON(sizeof(struct nvmf_property_set_command) != 64);
982 BUILD_BUG_ON(sizeof(struct nvmf_connect_data) != 1024);
985 MODULE_LICENSE("GPL v2");
987 module_init(nvmf_init);
988 module_exit(nvmf_exit);