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
);
53 host
= kmalloc(sizeof(*host
), GFP_KERNEL
);
57 kref_init(&host
->ref
);
58 memcpy(host
->nqn
, hostnqn
, NVMF_NQN_SIZE
);
59 uuid_le_gen(&host
->id
);
61 list_add_tail(&host
->list
, &nvmf_hosts
);
63 mutex_unlock(&nvmf_hosts_mutex
);
67 static struct nvmf_host
*nvmf_host_default(void)
69 struct nvmf_host
*host
;
71 host
= kmalloc(sizeof(*host
), GFP_KERNEL
);
75 kref_init(&host
->ref
);
76 uuid_le_gen(&host
->id
);
77 snprintf(host
->nqn
, NVMF_NQN_SIZE
,
78 "nqn.2014-08.org.nvmexpress:NVMf:uuid:%pUl", &host
->id
);
80 mutex_lock(&nvmf_hosts_mutex
);
81 list_add_tail(&host
->list
, &nvmf_hosts
);
82 mutex_unlock(&nvmf_hosts_mutex
);
87 static void nvmf_host_destroy(struct kref
*ref
)
89 struct nvmf_host
*host
= container_of(ref
, struct nvmf_host
, ref
);
91 mutex_lock(&nvmf_hosts_mutex
);
92 list_del(&host
->list
);
93 mutex_unlock(&nvmf_hosts_mutex
);
98 static void nvmf_host_put(struct nvmf_host
*host
)
101 kref_put(&host
->ref
, nvmf_host_destroy
);
105 * nvmf_get_address() - Get address/port
106 * @ctrl: Host NVMe controller instance which we got the address
107 * @buf: OUTPUT parameter that will contain the address/port
110 int nvmf_get_address(struct nvme_ctrl
*ctrl
, char *buf
, int size
)
112 return snprintf(buf
, size
, "traddr=%s,trsvcid=%s\n",
113 ctrl
->opts
->traddr
, ctrl
->opts
->trsvcid
);
115 EXPORT_SYMBOL_GPL(nvmf_get_address
);
118 * nvmf_get_subsysnqn() - Get subsystem NQN
119 * @ctrl: Host NVMe controller instance which we got the NQN
121 const char *nvmf_get_subsysnqn(struct nvme_ctrl
*ctrl
)
123 return ctrl
->opts
->subsysnqn
;
125 EXPORT_SYMBOL_GPL(nvmf_get_subsysnqn
);
128 * nvmf_reg_read32() - NVMe Fabrics "Property Get" API function.
129 * @ctrl: Host NVMe controller instance maintaining the admin
130 * queue used to submit the property read command to
131 * the allocated NVMe controller resource on the target system.
132 * @off: Starting offset value of the targeted property
133 * register (see the fabrics section of the NVMe standard).
134 * @val: OUTPUT parameter that will contain the value of
135 * the property after a successful read.
137 * Used by the host system to retrieve a 32-bit capsule property value
138 * from an NVMe controller on the target system.
140 * ("Capsule property" is an "PCIe register concept" applied to the
141 * NVMe fabrics space.)
145 * > 0: NVMe error status code
146 * < 0: Linux errno error code
148 int nvmf_reg_read32(struct nvme_ctrl
*ctrl
, u32 off
, u32
*val
)
150 struct nvme_command cmd
;
151 struct nvme_completion cqe
;
154 memset(&cmd
, 0, sizeof(cmd
));
155 cmd
.prop_get
.opcode
= nvme_fabrics_command
;
156 cmd
.prop_get
.fctype
= nvme_fabrics_type_property_get
;
157 cmd
.prop_get
.offset
= cpu_to_le32(off
);
159 ret
= __nvme_submit_sync_cmd(ctrl
->admin_q
, &cmd
, &cqe
, NULL
, 0, 0,
163 *val
= le64_to_cpu(cqe
.result64
);
164 if (unlikely(ret
!= 0))
165 dev_err(ctrl
->device
,
166 "Property Get error: %d, offset %#x\n",
167 ret
> 0 ? ret
& ~NVME_SC_DNR
: ret
, off
);
171 EXPORT_SYMBOL_GPL(nvmf_reg_read32
);
174 * nvmf_reg_read64() - NVMe Fabrics "Property Get" API function.
175 * @ctrl: Host NVMe controller instance maintaining the admin
176 * queue used to submit the property read command to
177 * the allocated controller resource on the target system.
178 * @off: Starting offset value of the targeted property
179 * register (see the fabrics section of the NVMe standard).
180 * @val: OUTPUT parameter that will contain the value of
181 * the property after a successful read.
183 * Used by the host system to retrieve a 64-bit capsule property value
184 * from an NVMe controller on the target system.
186 * ("Capsule property" is an "PCIe register concept" applied to the
187 * NVMe fabrics space.)
191 * > 0: NVMe error status code
192 * < 0: Linux errno error code
194 int nvmf_reg_read64(struct nvme_ctrl
*ctrl
, u32 off
, u64
*val
)
196 struct nvme_command cmd
;
197 struct nvme_completion cqe
;
200 memset(&cmd
, 0, sizeof(cmd
));
201 cmd
.prop_get
.opcode
= nvme_fabrics_command
;
202 cmd
.prop_get
.fctype
= nvme_fabrics_type_property_get
;
203 cmd
.prop_get
.attrib
= 1;
204 cmd
.prop_get
.offset
= cpu_to_le32(off
);
206 ret
= __nvme_submit_sync_cmd(ctrl
->admin_q
, &cmd
, &cqe
, NULL
, 0, 0,
210 *val
= le64_to_cpu(cqe
.result64
);
211 if (unlikely(ret
!= 0))
212 dev_err(ctrl
->device
,
213 "Property Get error: %d, offset %#x\n",
214 ret
> 0 ? ret
& ~NVME_SC_DNR
: ret
, off
);
217 EXPORT_SYMBOL_GPL(nvmf_reg_read64
);
220 * nvmf_reg_write32() - NVMe Fabrics "Property Write" API function.
221 * @ctrl: Host NVMe controller instance maintaining the admin
222 * queue used to submit the property read command to
223 * the allocated NVMe controller resource on the target system.
224 * @off: Starting offset value of the targeted property
225 * register (see the fabrics section of the NVMe standard).
226 * @val: Input parameter that contains the value to be
227 * written to the property.
229 * Used by the NVMe host system to write a 32-bit capsule property value
230 * to an NVMe controller on the target system.
232 * ("Capsule property" is an "PCIe register concept" applied to the
233 * NVMe fabrics space.)
236 * 0: successful write
237 * > 0: NVMe error status code
238 * < 0: Linux errno error code
240 int nvmf_reg_write32(struct nvme_ctrl
*ctrl
, u32 off
, u32 val
)
242 struct nvme_command cmd
;
245 memset(&cmd
, 0, sizeof(cmd
));
246 cmd
.prop_set
.opcode
= nvme_fabrics_command
;
247 cmd
.prop_set
.fctype
= nvme_fabrics_type_property_set
;
248 cmd
.prop_set
.attrib
= 0;
249 cmd
.prop_set
.offset
= cpu_to_le32(off
);
250 cmd
.prop_set
.value
= cpu_to_le64(val
);
252 ret
= __nvme_submit_sync_cmd(ctrl
->admin_q
, &cmd
, NULL
, NULL
, 0, 0,
255 dev_err(ctrl
->device
,
256 "Property Set error: %d, offset %#x\n",
257 ret
> 0 ? ret
& ~NVME_SC_DNR
: ret
, off
);
260 EXPORT_SYMBOL_GPL(nvmf_reg_write32
);
263 * nvmf_log_connect_error() - Error-parsing-diagnostic print
264 * out function for connect() errors.
266 * @ctrl: the specific /dev/nvmeX device that had the error.
268 * @errval: Error code to be decoded in a more human-friendly
271 * @offset: For use with the NVMe error code NVME_SC_CONNECT_INVALID_PARAM.
273 * @cmd: This is the SQE portion of a submission capsule.
275 * @data: This is the "Data" portion of a submission capsule.
277 static void nvmf_log_connect_error(struct nvme_ctrl
*ctrl
,
278 int errval
, int offset
, struct nvme_command
*cmd
,
279 struct nvmf_connect_data
*data
)
281 int err_sctype
= errval
& (~NVME_SC_DNR
);
283 switch (err_sctype
) {
285 case (NVME_SC_CONNECT_INVALID_PARAM
):
287 char *inv_data
= "Connect Invalid Data Parameter";
289 switch (offset
& 0xffff) {
290 case (offsetof(struct nvmf_connect_data
, cntlid
)):
291 dev_err(ctrl
->device
,
293 inv_data
, data
->cntlid
);
295 case (offsetof(struct nvmf_connect_data
, hostnqn
)):
296 dev_err(ctrl
->device
,
297 "%s, hostnqn \"%s\"\n",
298 inv_data
, data
->hostnqn
);
300 case (offsetof(struct nvmf_connect_data
, subsysnqn
)):
301 dev_err(ctrl
->device
,
302 "%s, subsysnqn \"%s\"\n",
303 inv_data
, data
->subsysnqn
);
306 dev_err(ctrl
->device
,
307 "%s, starting byte offset: %d\n",
308 inv_data
, offset
& 0xffff);
312 char *inv_sqe
= "Connect Invalid SQE Parameter";
315 case (offsetof(struct nvmf_connect_command
, qid
)):
316 dev_err(ctrl
->device
,
318 inv_sqe
, cmd
->connect
.qid
);
321 dev_err(ctrl
->device
,
322 "%s, starting byte offset: %d\n",
328 dev_err(ctrl
->device
,
329 "Connect command failed, error wo/DNR bit: %d\n",
332 } /* switch (err_sctype) */
336 * nvmf_connect_admin_queue() - NVMe Fabrics Admin Queue "Connect"
338 * @ctrl: Host nvme controller instance used to request
339 * a new NVMe controller allocation on the target
340 * system and establish an NVMe Admin connection to
343 * This function enables an NVMe host device to request a new allocation of
344 * an NVMe controller resource on a target system as well establish a
345 * fabrics-protocol connection of the NVMe Admin queue between the
346 * host system device and the allocated NVMe controller on the
347 * target system via a NVMe Fabrics "Connect" command.
351 * > 0: NVMe error status code
352 * < 0: Linux errno error code
355 int nvmf_connect_admin_queue(struct nvme_ctrl
*ctrl
)
357 struct nvme_command cmd
;
358 struct nvme_completion cqe
;
359 struct nvmf_connect_data
*data
;
362 memset(&cmd
, 0, sizeof(cmd
));
363 cmd
.connect
.opcode
= nvme_fabrics_command
;
364 cmd
.connect
.fctype
= nvme_fabrics_type_connect
;
366 cmd
.connect
.sqsize
= cpu_to_le16(ctrl
->sqsize
);
368 * Set keep-alive timeout in seconds granularity (ms * 1000)
369 * and add a grace period for controller kato enforcement
371 cmd
.connect
.kato
= ctrl
->opts
->discovery_nqn
? 0 :
372 cpu_to_le32((ctrl
->kato
+ NVME_KATO_GRACE
) * 1000);
374 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
378 memcpy(&data
->hostid
, &ctrl
->opts
->host
->id
, sizeof(uuid_le
));
379 data
->cntlid
= cpu_to_le16(0xffff);
380 strncpy(data
->subsysnqn
, ctrl
->opts
->subsysnqn
, NVMF_NQN_SIZE
);
381 strncpy(data
->hostnqn
, ctrl
->opts
->host
->nqn
, NVMF_NQN_SIZE
);
383 ret
= __nvme_submit_sync_cmd(ctrl
->admin_q
, &cmd
, &cqe
,
384 data
, sizeof(*data
), 0, NVME_QID_ANY
, 1,
385 BLK_MQ_REQ_RESERVED
| BLK_MQ_REQ_NOWAIT
);
387 nvmf_log_connect_error(ctrl
, ret
, le32_to_cpu(cqe
.result
),
392 ctrl
->cntlid
= le16_to_cpu(cqe
.result16
);
398 EXPORT_SYMBOL_GPL(nvmf_connect_admin_queue
);
401 * nvmf_connect_io_queue() - NVMe Fabrics I/O Queue "Connect"
403 * @ctrl: Host nvme controller instance used to establish an
404 * NVMe I/O queue connection to the already allocated NVMe
405 * controller on the target system.
406 * @qid: NVMe I/O queue number for the new I/O connection between
407 * host and target (note qid == 0 is illegal as this is
408 * the Admin queue, per NVMe standard).
410 * This function issues a fabrics-protocol connection
411 * of a NVMe I/O queue (via NVMe Fabrics "Connect" command)
412 * between the host system device and the allocated NVMe controller
413 * on the target system.
417 * > 0: NVMe error status code
418 * < 0: Linux errno error code
420 int nvmf_connect_io_queue(struct nvme_ctrl
*ctrl
, u16 qid
)
422 struct nvme_command cmd
;
423 struct nvmf_connect_data
*data
;
424 struct nvme_completion cqe
;
427 memset(&cmd
, 0, sizeof(cmd
));
428 cmd
.connect
.opcode
= nvme_fabrics_command
;
429 cmd
.connect
.fctype
= nvme_fabrics_type_connect
;
430 cmd
.connect
.qid
= cpu_to_le16(qid
);
431 cmd
.connect
.sqsize
= cpu_to_le16(ctrl
->sqsize
);
433 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
437 memcpy(&data
->hostid
, &ctrl
->opts
->host
->id
, sizeof(uuid_le
));
438 data
->cntlid
= cpu_to_le16(ctrl
->cntlid
);
439 strncpy(data
->subsysnqn
, ctrl
->opts
->subsysnqn
, NVMF_NQN_SIZE
);
440 strncpy(data
->hostnqn
, ctrl
->opts
->host
->nqn
, NVMF_NQN_SIZE
);
442 ret
= __nvme_submit_sync_cmd(ctrl
->connect_q
, &cmd
, &cqe
,
443 data
, sizeof(*data
), 0, qid
, 1,
444 BLK_MQ_REQ_RESERVED
| BLK_MQ_REQ_NOWAIT
);
446 nvmf_log_connect_error(ctrl
, ret
, le32_to_cpu(cqe
.result
),
452 EXPORT_SYMBOL_GPL(nvmf_connect_io_queue
);
455 * nvmf_register_transport() - NVMe Fabrics Library registration function.
456 * @ops: Transport ops instance to be registered to the
457 * common fabrics library.
459 * API function that registers the type of specific transport fabric
460 * being implemented to the common NVMe fabrics library. Part of
461 * the overall init sequence of starting up a fabrics driver.
463 void nvmf_register_transport(struct nvmf_transport_ops
*ops
)
465 mutex_lock(&nvmf_transports_mutex
);
466 list_add_tail(&ops
->entry
, &nvmf_transports
);
467 mutex_unlock(&nvmf_transports_mutex
);
469 EXPORT_SYMBOL_GPL(nvmf_register_transport
);
472 * nvmf_unregister_transport() - NVMe Fabrics Library unregistration function.
473 * @ops: Transport ops instance to be unregistered from the
474 * common fabrics library.
476 * Fabrics API function that unregisters the type of specific transport
477 * fabric being implemented from the common NVMe fabrics library.
478 * Part of the overall exit sequence of unloading the implemented driver.
480 void nvmf_unregister_transport(struct nvmf_transport_ops
*ops
)
482 mutex_lock(&nvmf_transports_mutex
);
483 list_del(&ops
->entry
);
484 mutex_unlock(&nvmf_transports_mutex
);
486 EXPORT_SYMBOL_GPL(nvmf_unregister_transport
);
488 static struct nvmf_transport_ops
*nvmf_lookup_transport(
489 struct nvmf_ctrl_options
*opts
)
491 struct nvmf_transport_ops
*ops
;
493 lockdep_assert_held(&nvmf_transports_mutex
);
495 list_for_each_entry(ops
, &nvmf_transports
, entry
) {
496 if (strcmp(ops
->name
, opts
->transport
) == 0)
503 static const match_table_t opt_tokens
= {
504 { NVMF_OPT_TRANSPORT
, "transport=%s" },
505 { NVMF_OPT_TRADDR
, "traddr=%s" },
506 { NVMF_OPT_TRSVCID
, "trsvcid=%s" },
507 { NVMF_OPT_NQN
, "nqn=%s" },
508 { NVMF_OPT_QUEUE_SIZE
, "queue_size=%d" },
509 { NVMF_OPT_NR_IO_QUEUES
, "nr_io_queues=%d" },
510 { NVMF_OPT_RECONNECT_DELAY
, "reconnect_delay=%d" },
511 { NVMF_OPT_KATO
, "keep_alive_tmo=%d" },
512 { NVMF_OPT_HOSTNQN
, "hostnqn=%s" },
513 { NVMF_OPT_ERR
, NULL
}
516 static int nvmf_parse_options(struct nvmf_ctrl_options
*opts
,
519 substring_t args
[MAX_OPT_ARGS
];
520 char *options
, *o
, *p
;
525 opts
->queue_size
= NVMF_DEF_QUEUE_SIZE
;
526 opts
->nr_io_queues
= num_online_cpus();
527 opts
->reconnect_delay
= NVMF_DEF_RECONNECT_DELAY
;
529 options
= o
= kstrdup(buf
, GFP_KERNEL
);
533 while ((p
= strsep(&o
, ",\n")) != NULL
) {
537 token
= match_token(p
, opt_tokens
, args
);
540 case NVMF_OPT_TRANSPORT
:
541 p
= match_strdup(args
);
549 p
= match_strdup(args
);
555 nqnlen
= strlen(opts
->subsysnqn
);
556 if (nqnlen
>= NVMF_NQN_SIZE
) {
557 pr_err("%s needs to be < %d bytes\n",
558 opts
->subsysnqn
, NVMF_NQN_SIZE
);
562 opts
->discovery_nqn
=
563 !(strcmp(opts
->subsysnqn
,
564 NVME_DISC_SUBSYS_NAME
));
565 if (opts
->discovery_nqn
)
566 opts
->nr_io_queues
= 0;
568 case NVMF_OPT_TRADDR
:
569 p
= match_strdup(args
);
576 case NVMF_OPT_TRSVCID
:
577 p
= match_strdup(args
);
584 case NVMF_OPT_QUEUE_SIZE
:
585 if (match_int(args
, &token
)) {
589 if (token
< NVMF_MIN_QUEUE_SIZE
||
590 token
> NVMF_MAX_QUEUE_SIZE
) {
591 pr_err("Invalid queue_size %d\n", token
);
595 opts
->queue_size
= token
;
597 case NVMF_OPT_NR_IO_QUEUES
:
598 if (match_int(args
, &token
)) {
603 pr_err("Invalid number of IOQs %d\n", token
);
607 opts
->nr_io_queues
= min_t(unsigned int,
608 num_online_cpus(), token
);
611 if (match_int(args
, &token
)) {
616 if (opts
->discovery_nqn
) {
617 pr_err("Discovery controllers cannot accept keep_alive_tmo != 0\n");
623 pr_err("Invalid keep_alive_tmo %d\n", token
);
626 } else if (token
== 0) {
627 /* Allowed for debug */
628 pr_warn("keep_alive_tmo 0 won't execute keep alives!!!\n");
632 case NVMF_OPT_HOSTNQN
:
634 pr_err("hostnqn already user-assigned: %s\n",
639 p
= match_strdup(args
);
645 if (nqnlen
>= NVMF_NQN_SIZE
) {
646 pr_err("%s needs to be < %d bytes\n",
651 opts
->host
= nvmf_host_add(p
);
657 case NVMF_OPT_RECONNECT_DELAY
:
658 if (match_int(args
, &token
)) {
663 pr_err("Invalid reconnect_delay %d\n", token
);
667 opts
->reconnect_delay
= token
;
670 pr_warn("unknown parameter or missing value '%s' in ctrl creation request\n",
678 kref_get(&nvmf_default_host
->ref
);
679 opts
->host
= nvmf_default_host
;
683 if (!opts
->discovery_nqn
&& !opts
->kato
)
684 opts
->kato
= NVME_DEFAULT_KATO
;
689 static int nvmf_check_required_opts(struct nvmf_ctrl_options
*opts
,
690 unsigned int required_opts
)
692 if ((opts
->mask
& required_opts
) != required_opts
) {
695 for (i
= 0; i
< ARRAY_SIZE(opt_tokens
); i
++) {
696 if ((opt_tokens
[i
].token
& required_opts
) &&
697 !(opt_tokens
[i
].token
& opts
->mask
)) {
698 pr_warn("missing parameter '%s'\n",
699 opt_tokens
[i
].pattern
);
709 static int nvmf_check_allowed_opts(struct nvmf_ctrl_options
*opts
,
710 unsigned int allowed_opts
)
712 if (opts
->mask
& ~allowed_opts
) {
715 for (i
= 0; i
< ARRAY_SIZE(opt_tokens
); i
++) {
716 if (opt_tokens
[i
].token
& ~allowed_opts
) {
717 pr_warn("invalid parameter '%s'\n",
718 opt_tokens
[i
].pattern
);
728 void nvmf_free_options(struct nvmf_ctrl_options
*opts
)
730 nvmf_host_put(opts
->host
);
731 kfree(opts
->transport
);
733 kfree(opts
->trsvcid
);
734 kfree(opts
->subsysnqn
);
737 EXPORT_SYMBOL_GPL(nvmf_free_options
);
739 #define NVMF_REQUIRED_OPTS (NVMF_OPT_TRANSPORT | NVMF_OPT_NQN)
740 #define NVMF_ALLOWED_OPTS (NVMF_OPT_QUEUE_SIZE | NVMF_OPT_NR_IO_QUEUES | \
741 NVMF_OPT_KATO | NVMF_OPT_HOSTNQN)
743 static struct nvme_ctrl
*
744 nvmf_create_ctrl(struct device
*dev
, const char *buf
, size_t count
)
746 struct nvmf_ctrl_options
*opts
;
747 struct nvmf_transport_ops
*ops
;
748 struct nvme_ctrl
*ctrl
;
751 opts
= kzalloc(sizeof(*opts
), GFP_KERNEL
);
753 return ERR_PTR(-ENOMEM
);
755 ret
= nvmf_parse_options(opts
, buf
);
760 * Check the generic options first as we need a valid transport for
761 * the lookup below. Then clear the generic flags so that transport
762 * drivers don't have to care about them.
764 ret
= nvmf_check_required_opts(opts
, NVMF_REQUIRED_OPTS
);
767 opts
->mask
&= ~NVMF_REQUIRED_OPTS
;
769 mutex_lock(&nvmf_transports_mutex
);
770 ops
= nvmf_lookup_transport(opts
);
772 pr_info("no handler found for transport %s.\n",
778 ret
= nvmf_check_required_opts(opts
, ops
->required_opts
);
781 ret
= nvmf_check_allowed_opts(opts
, NVMF_ALLOWED_OPTS
|
782 ops
->allowed_opts
| ops
->required_opts
);
786 ctrl
= ops
->create_ctrl(dev
, opts
);
792 mutex_unlock(&nvmf_transports_mutex
);
796 mutex_unlock(&nvmf_transports_mutex
);
798 nvmf_host_put(opts
->host
);
803 static struct class *nvmf_class
;
804 static struct device
*nvmf_device
;
805 static DEFINE_MUTEX(nvmf_dev_mutex
);
807 static ssize_t
nvmf_dev_write(struct file
*file
, const char __user
*ubuf
,
808 size_t count
, loff_t
*pos
)
810 struct seq_file
*seq_file
= file
->private_data
;
811 struct nvme_ctrl
*ctrl
;
815 if (count
> PAGE_SIZE
)
818 buf
= memdup_user_nul(ubuf
, count
);
822 mutex_lock(&nvmf_dev_mutex
);
823 if (seq_file
->private) {
828 ctrl
= nvmf_create_ctrl(nvmf_device
, buf
, count
);
834 seq_file
->private = ctrl
;
837 mutex_unlock(&nvmf_dev_mutex
);
839 return ret
? ret
: count
;
842 static int nvmf_dev_show(struct seq_file
*seq_file
, void *private)
844 struct nvme_ctrl
*ctrl
;
847 mutex_lock(&nvmf_dev_mutex
);
848 ctrl
= seq_file
->private;
854 seq_printf(seq_file
, "instance=%d,cntlid=%d\n",
855 ctrl
->instance
, ctrl
->cntlid
);
858 mutex_unlock(&nvmf_dev_mutex
);
862 static int nvmf_dev_open(struct inode
*inode
, struct file
*file
)
865 * The miscdevice code initializes file->private_data, but doesn't
866 * make use of it later.
868 file
->private_data
= NULL
;
869 return single_open(file
, nvmf_dev_show
, NULL
);
872 static int nvmf_dev_release(struct inode
*inode
, struct file
*file
)
874 struct seq_file
*seq_file
= file
->private_data
;
875 struct nvme_ctrl
*ctrl
= seq_file
->private;
879 return single_release(inode
, file
);
882 static const struct file_operations nvmf_dev_fops
= {
883 .owner
= THIS_MODULE
,
884 .write
= nvmf_dev_write
,
886 .open
= nvmf_dev_open
,
887 .release
= nvmf_dev_release
,
890 static struct miscdevice nvmf_misc
= {
891 .minor
= MISC_DYNAMIC_MINOR
,
892 .name
= "nvme-fabrics",
893 .fops
= &nvmf_dev_fops
,
896 static int __init
nvmf_init(void)
900 nvmf_default_host
= nvmf_host_default();
901 if (!nvmf_default_host
)
904 nvmf_class
= class_create(THIS_MODULE
, "nvme-fabrics");
905 if (IS_ERR(nvmf_class
)) {
906 pr_err("couldn't register class nvme-fabrics\n");
907 ret
= PTR_ERR(nvmf_class
);
912 device_create(nvmf_class
, NULL
, MKDEV(0, 0), NULL
, "ctl");
913 if (IS_ERR(nvmf_device
)) {
914 pr_err("couldn't create nvme-fabris device!\n");
915 ret
= PTR_ERR(nvmf_device
);
916 goto out_destroy_class
;
919 ret
= misc_register(&nvmf_misc
);
921 pr_err("couldn't register misc device: %d\n", ret
);
922 goto out_destroy_device
;
928 device_destroy(nvmf_class
, MKDEV(0, 0));
930 class_destroy(nvmf_class
);
932 nvmf_host_put(nvmf_default_host
);
936 static void __exit
nvmf_exit(void)
938 misc_deregister(&nvmf_misc
);
939 device_destroy(nvmf_class
, MKDEV(0, 0));
940 class_destroy(nvmf_class
);
941 nvmf_host_put(nvmf_default_host
);
943 BUILD_BUG_ON(sizeof(struct nvmf_connect_command
) != 64);
944 BUILD_BUG_ON(sizeof(struct nvmf_property_get_command
) != 64);
945 BUILD_BUG_ON(sizeof(struct nvmf_property_set_command
) != 64);
946 BUILD_BUG_ON(sizeof(struct nvmf_connect_data
) != 1024);
949 MODULE_LICENSE("GPL v2");
951 module_init(nvmf_init
);
952 module_exit(nvmf_exit
);