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 memcpy(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
);
77 snprintf(host
->nqn
, NVMF_NQN_SIZE
,
78 "nqn.2014-08.org.nvmexpress:uuid:%pUb", &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
)
114 if (ctrl
->opts
->mask
& NVMF_OPT_TRADDR
)
115 len
+= snprintf(buf
, size
, "traddr=%s", ctrl
->opts
->traddr
);
116 if (ctrl
->opts
->mask
& NVMF_OPT_TRSVCID
)
117 len
+= snprintf(buf
+ len
, size
- len
, "%strsvcid=%s",
118 (len
) ? "," : "", ctrl
->opts
->trsvcid
);
119 if (ctrl
->opts
->mask
& NVMF_OPT_HOST_TRADDR
)
120 len
+= snprintf(buf
+ len
, size
- len
, "%shost_traddr=%s",
121 (len
) ? "," : "", ctrl
->opts
->host_traddr
);
122 len
+= snprintf(buf
+ len
, size
- len
, "\n");
126 EXPORT_SYMBOL_GPL(nvmf_get_address
);
129 * nvmf_reg_read32() - NVMe Fabrics "Property Get" API function.
130 * @ctrl: Host NVMe controller instance maintaining the admin
131 * queue used to submit the property read command to
132 * the allocated NVMe controller resource on the target system.
133 * @off: Starting offset value of the targeted property
134 * register (see the fabrics section of the NVMe standard).
135 * @val: OUTPUT parameter that will contain the value of
136 * the property after a successful read.
138 * Used by the host system to retrieve a 32-bit capsule property value
139 * from an NVMe controller on the target system.
141 * ("Capsule property" is an "PCIe register concept" applied to the
142 * NVMe fabrics space.)
146 * > 0: NVMe error status code
147 * < 0: Linux errno error code
149 int nvmf_reg_read32(struct nvme_ctrl
*ctrl
, u32 off
, u32
*val
)
151 struct nvme_command cmd
;
152 union nvme_result res
;
155 memset(&cmd
, 0, sizeof(cmd
));
156 cmd
.prop_get
.opcode
= nvme_fabrics_command
;
157 cmd
.prop_get
.fctype
= nvme_fabrics_type_property_get
;
158 cmd
.prop_get
.offset
= cpu_to_le32(off
);
160 ret
= __nvme_submit_sync_cmd(ctrl
->admin_q
, &cmd
, &res
, NULL
, 0, 0,
164 *val
= le64_to_cpu(res
.u64
);
165 if (unlikely(ret
!= 0))
166 dev_err(ctrl
->device
,
167 "Property Get error: %d, offset %#x\n",
168 ret
> 0 ? ret
& ~NVME_SC_DNR
: ret
, off
);
172 EXPORT_SYMBOL_GPL(nvmf_reg_read32
);
175 * nvmf_reg_read64() - NVMe Fabrics "Property Get" API function.
176 * @ctrl: Host NVMe controller instance maintaining the admin
177 * queue used to submit the property read command to
178 * the allocated controller resource on the target system.
179 * @off: Starting offset value of the targeted property
180 * register (see the fabrics section of the NVMe standard).
181 * @val: OUTPUT parameter that will contain the value of
182 * the property after a successful read.
184 * Used by the host system to retrieve a 64-bit capsule property value
185 * from an NVMe controller on the target system.
187 * ("Capsule property" is an "PCIe register concept" applied to the
188 * NVMe fabrics space.)
192 * > 0: NVMe error status code
193 * < 0: Linux errno error code
195 int nvmf_reg_read64(struct nvme_ctrl
*ctrl
, u32 off
, u64
*val
)
197 struct nvme_command cmd
;
198 union nvme_result res
;
201 memset(&cmd
, 0, sizeof(cmd
));
202 cmd
.prop_get
.opcode
= nvme_fabrics_command
;
203 cmd
.prop_get
.fctype
= nvme_fabrics_type_property_get
;
204 cmd
.prop_get
.attrib
= 1;
205 cmd
.prop_get
.offset
= cpu_to_le32(off
);
207 ret
= __nvme_submit_sync_cmd(ctrl
->admin_q
, &cmd
, &res
, NULL
, 0, 0,
211 *val
= le64_to_cpu(res
.u64
);
212 if (unlikely(ret
!= 0))
213 dev_err(ctrl
->device
,
214 "Property Get error: %d, offset %#x\n",
215 ret
> 0 ? ret
& ~NVME_SC_DNR
: ret
, off
);
218 EXPORT_SYMBOL_GPL(nvmf_reg_read64
);
221 * nvmf_reg_write32() - NVMe Fabrics "Property Write" API function.
222 * @ctrl: Host NVMe controller instance maintaining the admin
223 * queue used to submit the property read command to
224 * the allocated NVMe controller resource on the target system.
225 * @off: Starting offset value of the targeted property
226 * register (see the fabrics section of the NVMe standard).
227 * @val: Input parameter that contains the value to be
228 * written to the property.
230 * Used by the NVMe host system to write a 32-bit capsule property value
231 * to an NVMe controller on the target system.
233 * ("Capsule property" is an "PCIe register concept" applied to the
234 * NVMe fabrics space.)
237 * 0: successful write
238 * > 0: NVMe error status code
239 * < 0: Linux errno error code
241 int nvmf_reg_write32(struct nvme_ctrl
*ctrl
, u32 off
, u32 val
)
243 struct nvme_command cmd
;
246 memset(&cmd
, 0, sizeof(cmd
));
247 cmd
.prop_set
.opcode
= nvme_fabrics_command
;
248 cmd
.prop_set
.fctype
= nvme_fabrics_type_property_set
;
249 cmd
.prop_set
.attrib
= 0;
250 cmd
.prop_set
.offset
= cpu_to_le32(off
);
251 cmd
.prop_set
.value
= cpu_to_le64(val
);
253 ret
= __nvme_submit_sync_cmd(ctrl
->admin_q
, &cmd
, NULL
, NULL
, 0, 0,
256 dev_err(ctrl
->device
,
257 "Property Set error: %d, offset %#x\n",
258 ret
> 0 ? ret
& ~NVME_SC_DNR
: ret
, off
);
261 EXPORT_SYMBOL_GPL(nvmf_reg_write32
);
264 * nvmf_log_connect_error() - Error-parsing-diagnostic print
265 * out function for connect() errors.
267 * @ctrl: the specific /dev/nvmeX device that had the error.
269 * @errval: Error code to be decoded in a more human-friendly
272 * @offset: For use with the NVMe error code NVME_SC_CONNECT_INVALID_PARAM.
274 * @cmd: This is the SQE portion of a submission capsule.
276 * @data: This is the "Data" portion of a submission capsule.
278 static void nvmf_log_connect_error(struct nvme_ctrl
*ctrl
,
279 int errval
, int offset
, struct nvme_command
*cmd
,
280 struct nvmf_connect_data
*data
)
282 int err_sctype
= errval
& (~NVME_SC_DNR
);
284 switch (err_sctype
) {
286 case (NVME_SC_CONNECT_INVALID_PARAM
):
288 char *inv_data
= "Connect Invalid Data Parameter";
290 switch (offset
& 0xffff) {
291 case (offsetof(struct nvmf_connect_data
, cntlid
)):
292 dev_err(ctrl
->device
,
294 inv_data
, data
->cntlid
);
296 case (offsetof(struct nvmf_connect_data
, hostnqn
)):
297 dev_err(ctrl
->device
,
298 "%s, hostnqn \"%s\"\n",
299 inv_data
, data
->hostnqn
);
301 case (offsetof(struct nvmf_connect_data
, subsysnqn
)):
302 dev_err(ctrl
->device
,
303 "%s, subsysnqn \"%s\"\n",
304 inv_data
, data
->subsysnqn
);
307 dev_err(ctrl
->device
,
308 "%s, starting byte offset: %d\n",
309 inv_data
, offset
& 0xffff);
313 char *inv_sqe
= "Connect Invalid SQE Parameter";
316 case (offsetof(struct nvmf_connect_command
, qid
)):
317 dev_err(ctrl
->device
,
319 inv_sqe
, cmd
->connect
.qid
);
322 dev_err(ctrl
->device
,
323 "%s, starting byte offset: %d\n",
329 case NVME_SC_CONNECT_INVALID_HOST
:
330 dev_err(ctrl
->device
,
331 "Connect for subsystem %s is not allowed, hostnqn: %s\n",
332 data
->subsysnqn
, data
->hostnqn
);
335 case NVME_SC_CONNECT_CTRL_BUSY
:
336 dev_err(ctrl
->device
,
337 "Connect command failed: controller is busy or not available\n");
340 case NVME_SC_CONNECT_FORMAT
:
341 dev_err(ctrl
->device
,
342 "Connect incompatible format: %d",
343 cmd
->connect
.recfmt
);
347 dev_err(ctrl
->device
,
348 "Connect command failed, error wo/DNR bit: %d\n",
351 } /* switch (err_sctype) */
355 * nvmf_connect_admin_queue() - NVMe Fabrics Admin Queue "Connect"
357 * @ctrl: Host nvme controller instance used to request
358 * a new NVMe controller allocation on the target
359 * system and establish an NVMe Admin connection to
362 * This function enables an NVMe host device to request a new allocation of
363 * an NVMe controller resource on a target system as well establish a
364 * fabrics-protocol connection of the NVMe Admin queue between the
365 * host system device and the allocated NVMe controller on the
366 * target system via a NVMe Fabrics "Connect" command.
370 * > 0: NVMe error status code
371 * < 0: Linux errno error code
374 int nvmf_connect_admin_queue(struct nvme_ctrl
*ctrl
)
376 struct nvme_command cmd
;
377 union nvme_result res
;
378 struct nvmf_connect_data
*data
;
381 memset(&cmd
, 0, sizeof(cmd
));
382 cmd
.connect
.opcode
= nvme_fabrics_command
;
383 cmd
.connect
.fctype
= nvme_fabrics_type_connect
;
385 cmd
.connect
.sqsize
= cpu_to_le16(NVME_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 uuid_copy(&data
->hostid
, &ctrl
->opts
->host
->id
);
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 uuid_copy(&data
->hostid
, &ctrl
->opts
->host
->id
);
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
);
474 bool nvmf_should_reconnect(struct nvme_ctrl
*ctrl
)
476 if (ctrl
->opts
->max_reconnects
!= -1 &&
477 ctrl
->nr_reconnects
< ctrl
->opts
->max_reconnects
)
482 EXPORT_SYMBOL_GPL(nvmf_should_reconnect
);
485 * nvmf_register_transport() - NVMe Fabrics Library registration function.
486 * @ops: Transport ops instance to be registered to the
487 * common fabrics library.
489 * API function that registers the type of specific transport fabric
490 * being implemented to the common NVMe fabrics library. Part of
491 * the overall init sequence of starting up a fabrics driver.
493 int nvmf_register_transport(struct nvmf_transport_ops
*ops
)
495 if (!ops
->create_ctrl
)
498 down_write(&nvmf_transports_rwsem
);
499 list_add_tail(&ops
->entry
, &nvmf_transports
);
500 up_write(&nvmf_transports_rwsem
);
504 EXPORT_SYMBOL_GPL(nvmf_register_transport
);
507 * nvmf_unregister_transport() - NVMe Fabrics Library unregistration function.
508 * @ops: Transport ops instance to be unregistered from the
509 * common fabrics library.
511 * Fabrics API function that unregisters the type of specific transport
512 * fabric being implemented from the common NVMe fabrics library.
513 * Part of the overall exit sequence of unloading the implemented driver.
515 void nvmf_unregister_transport(struct nvmf_transport_ops
*ops
)
517 down_write(&nvmf_transports_rwsem
);
518 list_del(&ops
->entry
);
519 up_write(&nvmf_transports_rwsem
);
521 EXPORT_SYMBOL_GPL(nvmf_unregister_transport
);
523 static struct nvmf_transport_ops
*nvmf_lookup_transport(
524 struct nvmf_ctrl_options
*opts
)
526 struct nvmf_transport_ops
*ops
;
528 lockdep_assert_held(&nvmf_transports_rwsem
);
530 list_for_each_entry(ops
, &nvmf_transports
, entry
) {
531 if (strcmp(ops
->name
, opts
->transport
) == 0)
538 static const match_table_t opt_tokens
= {
539 { NVMF_OPT_TRANSPORT
, "transport=%s" },
540 { NVMF_OPT_TRADDR
, "traddr=%s" },
541 { NVMF_OPT_TRSVCID
, "trsvcid=%s" },
542 { NVMF_OPT_NQN
, "nqn=%s" },
543 { NVMF_OPT_QUEUE_SIZE
, "queue_size=%d" },
544 { NVMF_OPT_NR_IO_QUEUES
, "nr_io_queues=%d" },
545 { NVMF_OPT_RECONNECT_DELAY
, "reconnect_delay=%d" },
546 { NVMF_OPT_CTRL_LOSS_TMO
, "ctrl_loss_tmo=%d" },
547 { NVMF_OPT_KATO
, "keep_alive_tmo=%d" },
548 { NVMF_OPT_HOSTNQN
, "hostnqn=%s" },
549 { NVMF_OPT_HOST_TRADDR
, "host_traddr=%s" },
550 { NVMF_OPT_HOST_ID
, "hostid=%s" },
551 { NVMF_OPT_DUP_CONNECT
, "duplicate_connect" },
552 { NVMF_OPT_ERR
, NULL
}
555 static int nvmf_parse_options(struct nvmf_ctrl_options
*opts
,
558 substring_t args
[MAX_OPT_ARGS
];
559 char *options
, *o
, *p
;
562 int ctrl_loss_tmo
= NVMF_DEF_CTRL_LOSS_TMO
;
566 opts
->queue_size
= NVMF_DEF_QUEUE_SIZE
;
567 opts
->nr_io_queues
= num_online_cpus();
568 opts
->reconnect_delay
= NVMF_DEF_RECONNECT_DELAY
;
569 opts
->kato
= NVME_DEFAULT_KATO
;
570 opts
->duplicate_connect
= false;
572 options
= o
= kstrdup(buf
, GFP_KERNEL
);
578 while ((p
= strsep(&o
, ",\n")) != NULL
) {
582 token
= match_token(p
, opt_tokens
, args
);
585 case NVMF_OPT_TRANSPORT
:
586 p
= match_strdup(args
);
594 p
= match_strdup(args
);
600 nqnlen
= strlen(opts
->subsysnqn
);
601 if (nqnlen
>= NVMF_NQN_SIZE
) {
602 pr_err("%s needs to be < %d bytes\n",
603 opts
->subsysnqn
, NVMF_NQN_SIZE
);
607 opts
->discovery_nqn
=
608 !(strcmp(opts
->subsysnqn
,
609 NVME_DISC_SUBSYS_NAME
));
610 if (opts
->discovery_nqn
)
611 opts
->nr_io_queues
= 0;
613 case NVMF_OPT_TRADDR
:
614 p
= match_strdup(args
);
621 case NVMF_OPT_TRSVCID
:
622 p
= match_strdup(args
);
629 case NVMF_OPT_QUEUE_SIZE
:
630 if (match_int(args
, &token
)) {
634 if (token
< NVMF_MIN_QUEUE_SIZE
||
635 token
> NVMF_MAX_QUEUE_SIZE
) {
636 pr_err("Invalid queue_size %d\n", token
);
640 opts
->queue_size
= token
;
642 case NVMF_OPT_NR_IO_QUEUES
:
643 if (match_int(args
, &token
)) {
648 pr_err("Invalid number of IOQs %d\n", token
);
652 opts
->nr_io_queues
= min_t(unsigned int,
653 num_online_cpus(), token
);
656 if (match_int(args
, &token
)) {
662 pr_err("Invalid keep_alive_tmo %d\n", token
);
665 } else if (token
== 0 && !opts
->discovery_nqn
) {
666 /* Allowed for debug */
667 pr_warn("keep_alive_tmo 0 won't execute keep alives!!!\n");
671 if (opts
->discovery_nqn
&& opts
->kato
) {
672 pr_err("Discovery controllers cannot accept KATO != 0\n");
678 case NVMF_OPT_CTRL_LOSS_TMO
:
679 if (match_int(args
, &token
)) {
685 pr_warn("ctrl_loss_tmo < 0 will reconnect forever\n");
686 ctrl_loss_tmo
= token
;
688 case NVMF_OPT_HOSTNQN
:
690 pr_err("hostnqn already user-assigned: %s\n",
695 p
= match_strdup(args
);
701 if (nqnlen
>= NVMF_NQN_SIZE
) {
702 pr_err("%s needs to be < %d bytes\n",
708 opts
->host
= nvmf_host_add(p
);
715 case NVMF_OPT_RECONNECT_DELAY
:
716 if (match_int(args
, &token
)) {
721 pr_err("Invalid reconnect_delay %d\n", token
);
725 opts
->reconnect_delay
= token
;
727 case NVMF_OPT_HOST_TRADDR
:
728 p
= match_strdup(args
);
733 opts
->host_traddr
= p
;
735 case NVMF_OPT_HOST_ID
:
736 p
= match_strdup(args
);
741 if (uuid_parse(p
, &hostid
)) {
742 pr_err("Invalid hostid %s\n", p
);
747 case NVMF_OPT_DUP_CONNECT
:
748 opts
->duplicate_connect
= true;
751 pr_warn("unknown parameter or missing value '%s' in ctrl creation request\n",
758 if (ctrl_loss_tmo
< 0)
759 opts
->max_reconnects
= -1;
761 opts
->max_reconnects
= DIV_ROUND_UP(ctrl_loss_tmo
,
762 opts
->reconnect_delay
);
765 kref_get(&nvmf_default_host
->ref
);
766 opts
->host
= nvmf_default_host
;
769 uuid_copy(&opts
->host
->id
, &hostid
);
776 static int nvmf_check_required_opts(struct nvmf_ctrl_options
*opts
,
777 unsigned int required_opts
)
779 if ((opts
->mask
& required_opts
) != required_opts
) {
782 for (i
= 0; i
< ARRAY_SIZE(opt_tokens
); i
++) {
783 if ((opt_tokens
[i
].token
& required_opts
) &&
784 !(opt_tokens
[i
].token
& opts
->mask
)) {
785 pr_warn("missing parameter '%s'\n",
786 opt_tokens
[i
].pattern
);
796 static int nvmf_check_allowed_opts(struct nvmf_ctrl_options
*opts
,
797 unsigned int allowed_opts
)
799 if (opts
->mask
& ~allowed_opts
) {
802 for (i
= 0; i
< ARRAY_SIZE(opt_tokens
); i
++) {
803 if ((opt_tokens
[i
].token
& opts
->mask
) &&
804 (opt_tokens
[i
].token
& ~allowed_opts
)) {
805 pr_warn("invalid parameter '%s'\n",
806 opt_tokens
[i
].pattern
);
816 void nvmf_free_options(struct nvmf_ctrl_options
*opts
)
818 nvmf_host_put(opts
->host
);
819 kfree(opts
->transport
);
821 kfree(opts
->trsvcid
);
822 kfree(opts
->subsysnqn
);
823 kfree(opts
->host_traddr
);
826 EXPORT_SYMBOL_GPL(nvmf_free_options
);
828 #define NVMF_REQUIRED_OPTS (NVMF_OPT_TRANSPORT | NVMF_OPT_NQN)
829 #define NVMF_ALLOWED_OPTS (NVMF_OPT_QUEUE_SIZE | NVMF_OPT_NR_IO_QUEUES | \
830 NVMF_OPT_KATO | NVMF_OPT_HOSTNQN | \
831 NVMF_OPT_HOST_ID | NVMF_OPT_DUP_CONNECT)
833 static struct nvme_ctrl
*
834 nvmf_create_ctrl(struct device
*dev
, const char *buf
, size_t count
)
836 struct nvmf_ctrl_options
*opts
;
837 struct nvmf_transport_ops
*ops
;
838 struct nvme_ctrl
*ctrl
;
841 opts
= kzalloc(sizeof(*opts
), GFP_KERNEL
);
843 return ERR_PTR(-ENOMEM
);
845 ret
= nvmf_parse_options(opts
, buf
);
850 request_module("nvme-%s", opts
->transport
);
853 * Check the generic options first as we need a valid transport for
854 * the lookup below. Then clear the generic flags so that transport
855 * drivers don't have to care about them.
857 ret
= nvmf_check_required_opts(opts
, NVMF_REQUIRED_OPTS
);
860 opts
->mask
&= ~NVMF_REQUIRED_OPTS
;
862 down_read(&nvmf_transports_rwsem
);
863 ops
= nvmf_lookup_transport(opts
);
865 pr_info("no handler found for transport %s.\n",
871 ret
= nvmf_check_required_opts(opts
, ops
->required_opts
);
874 ret
= nvmf_check_allowed_opts(opts
, NVMF_ALLOWED_OPTS
|
875 ops
->allowed_opts
| ops
->required_opts
);
879 ctrl
= ops
->create_ctrl(dev
, opts
);
885 if (strcmp(ctrl
->subsys
->subnqn
, opts
->subsysnqn
)) {
886 dev_warn(ctrl
->device
,
887 "controller returned incorrect NQN: \"%s\".\n",
888 ctrl
->subsys
->subnqn
);
889 up_read(&nvmf_transports_rwsem
);
890 nvme_delete_ctrl_sync(ctrl
);
891 return ERR_PTR(-EINVAL
);
894 up_read(&nvmf_transports_rwsem
);
898 up_read(&nvmf_transports_rwsem
);
900 nvmf_free_options(opts
);
904 static struct class *nvmf_class
;
905 static struct device
*nvmf_device
;
906 static DEFINE_MUTEX(nvmf_dev_mutex
);
908 static ssize_t
nvmf_dev_write(struct file
*file
, const char __user
*ubuf
,
909 size_t count
, loff_t
*pos
)
911 struct seq_file
*seq_file
= file
->private_data
;
912 struct nvme_ctrl
*ctrl
;
916 if (count
> PAGE_SIZE
)
919 buf
= memdup_user_nul(ubuf
, count
);
923 mutex_lock(&nvmf_dev_mutex
);
924 if (seq_file
->private) {
929 ctrl
= nvmf_create_ctrl(nvmf_device
, buf
, count
);
935 seq_file
->private = ctrl
;
938 mutex_unlock(&nvmf_dev_mutex
);
940 return ret
? ret
: count
;
943 static int nvmf_dev_show(struct seq_file
*seq_file
, void *private)
945 struct nvme_ctrl
*ctrl
;
948 mutex_lock(&nvmf_dev_mutex
);
949 ctrl
= seq_file
->private;
955 seq_printf(seq_file
, "instance=%d,cntlid=%d\n",
956 ctrl
->instance
, ctrl
->cntlid
);
959 mutex_unlock(&nvmf_dev_mutex
);
963 static int nvmf_dev_open(struct inode
*inode
, struct file
*file
)
966 * The miscdevice code initializes file->private_data, but doesn't
967 * make use of it later.
969 file
->private_data
= NULL
;
970 return single_open(file
, nvmf_dev_show
, NULL
);
973 static int nvmf_dev_release(struct inode
*inode
, struct file
*file
)
975 struct seq_file
*seq_file
= file
->private_data
;
976 struct nvme_ctrl
*ctrl
= seq_file
->private;
980 return single_release(inode
, file
);
983 static const struct file_operations nvmf_dev_fops
= {
984 .owner
= THIS_MODULE
,
985 .write
= nvmf_dev_write
,
987 .open
= nvmf_dev_open
,
988 .release
= nvmf_dev_release
,
991 static struct miscdevice nvmf_misc
= {
992 .minor
= MISC_DYNAMIC_MINOR
,
993 .name
= "nvme-fabrics",
994 .fops
= &nvmf_dev_fops
,
997 static int __init
nvmf_init(void)
1001 nvmf_default_host
= nvmf_host_default();
1002 if (!nvmf_default_host
)
1005 nvmf_class
= class_create(THIS_MODULE
, "nvme-fabrics");
1006 if (IS_ERR(nvmf_class
)) {
1007 pr_err("couldn't register class nvme-fabrics\n");
1008 ret
= PTR_ERR(nvmf_class
);
1013 device_create(nvmf_class
, NULL
, MKDEV(0, 0), NULL
, "ctl");
1014 if (IS_ERR(nvmf_device
)) {
1015 pr_err("couldn't create nvme-fabris device!\n");
1016 ret
= PTR_ERR(nvmf_device
);
1017 goto out_destroy_class
;
1020 ret
= misc_register(&nvmf_misc
);
1022 pr_err("couldn't register misc device: %d\n", ret
);
1023 goto out_destroy_device
;
1029 device_destroy(nvmf_class
, MKDEV(0, 0));
1031 class_destroy(nvmf_class
);
1033 nvmf_host_put(nvmf_default_host
);
1037 static void __exit
nvmf_exit(void)
1039 misc_deregister(&nvmf_misc
);
1040 device_destroy(nvmf_class
, MKDEV(0, 0));
1041 class_destroy(nvmf_class
);
1042 nvmf_host_put(nvmf_default_host
);
1044 BUILD_BUG_ON(sizeof(struct nvmf_connect_command
) != 64);
1045 BUILD_BUG_ON(sizeof(struct nvmf_property_get_command
) != 64);
1046 BUILD_BUG_ON(sizeof(struct nvmf_property_set_command
) != 64);
1047 BUILD_BUG_ON(sizeof(struct nvmf_connect_data
) != 1024);
1050 MODULE_LICENSE("GPL v2");
1052 module_init(nvmf_init
);
1053 module_exit(nvmf_exit
);