]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/nvme/host/fabrics.c
nvme-fabrics: change NQN UUID to big-endian format
[mirror_ubuntu-artful-kernel.git] / drivers / nvme / host / fabrics.c
CommitLineData
07bfcd09
CH
1/*
2 * NVMe over Fabrics common host code.
3 * Copyright (c) 2015-2016 HGST, a Western Digital Company.
4 *
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.
8 *
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
12 * more details.
13 */
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>
21#include "nvme.h"
22#include "fabrics.h"
23
24static LIST_HEAD(nvmf_transports);
25static DEFINE_MUTEX(nvmf_transports_mutex);
26
27static LIST_HEAD(nvmf_hosts);
28static DEFINE_MUTEX(nvmf_hosts_mutex);
29
30static struct nvmf_host *nvmf_default_host;
31
32static struct nvmf_host *__nvmf_host_find(const char *hostnqn)
33{
34 struct nvmf_host *host;
35
36 list_for_each_entry(host, &nvmf_hosts, list) {
37 if (!strcmp(host->nqn, hostnqn))
38 return host;
39 }
40
41 return NULL;
42}
43
44static struct nvmf_host *nvmf_host_add(const char *hostnqn)
45{
46 struct nvmf_host *host;
47
48 mutex_lock(&nvmf_hosts_mutex);
49 host = __nvmf_host_find(hostnqn);
50 if (host)
51 goto out_unlock;
52
53 host = kmalloc(sizeof(*host), GFP_KERNEL);
54 if (!host)
55 goto out_unlock;
56
57 kref_init(&host->ref);
58 memcpy(host->nqn, hostnqn, NVMF_NQN_SIZE);
7a665d2f 59 uuid_be_gen(&host->id);
07bfcd09
CH
60
61 list_add_tail(&host->list, &nvmf_hosts);
62out_unlock:
63 mutex_unlock(&nvmf_hosts_mutex);
64 return host;
65}
66
67static struct nvmf_host *nvmf_host_default(void)
68{
69 struct nvmf_host *host;
70
71 host = kmalloc(sizeof(*host), GFP_KERNEL);
72 if (!host)
73 return NULL;
74
75 kref_init(&host->ref);
7a665d2f 76 uuid_be_gen(&host->id);
07bfcd09 77 snprintf(host->nqn, NVMF_NQN_SIZE,
7a665d2f 78 "nqn.2014-08.org.nvmexpress:NVMf:uuid:%pUb", &host->id);
07bfcd09
CH
79
80 mutex_lock(&nvmf_hosts_mutex);
81 list_add_tail(&host->list, &nvmf_hosts);
82 mutex_unlock(&nvmf_hosts_mutex);
83
84 return host;
85}
86
87static void nvmf_host_destroy(struct kref *ref)
88{
89 struct nvmf_host *host = container_of(ref, struct nvmf_host, ref);
90
e76debd9
ML
91 mutex_lock(&nvmf_hosts_mutex);
92 list_del(&host->list);
93 mutex_unlock(&nvmf_hosts_mutex);
94
07bfcd09
CH
95 kfree(host);
96}
97
98static void nvmf_host_put(struct nvmf_host *host)
99{
100 if (host)
101 kref_put(&host->ref, nvmf_host_destroy);
102}
103
104/**
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
108 * @size: buffer size
109 */
110int nvmf_get_address(struct nvme_ctrl *ctrl, char *buf, int size)
111{
112 return snprintf(buf, size, "traddr=%s,trsvcid=%s\n",
113 ctrl->opts->traddr, ctrl->opts->trsvcid);
114}
115EXPORT_SYMBOL_GPL(nvmf_get_address);
116
117/**
118 * nvmf_get_subsysnqn() - Get subsystem NQN
119 * @ctrl: Host NVMe controller instance which we got the NQN
120 */
121const char *nvmf_get_subsysnqn(struct nvme_ctrl *ctrl)
122{
123 return ctrl->opts->subsysnqn;
124}
125EXPORT_SYMBOL_GPL(nvmf_get_subsysnqn);
126
127/**
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.
136 *
137 * Used by the host system to retrieve a 32-bit capsule property value
138 * from an NVMe controller on the target system.
139 *
140 * ("Capsule property" is an "PCIe register concept" applied to the
141 * NVMe fabrics space.)
142 *
143 * Return:
144 * 0: successful read
145 * > 0: NVMe error status code
146 * < 0: Linux errno error code
147 */
148int nvmf_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val)
149{
150 struct nvme_command cmd;
151 struct nvme_completion cqe;
152 int ret;
153
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);
158
159 ret = __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, &cqe, NULL, 0, 0,
160 NVME_QID_ANY, 0, 0);
161
162 if (ret >= 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);
168
169 return ret;
170}
171EXPORT_SYMBOL_GPL(nvmf_reg_read32);
172
173/**
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.
182 *
183 * Used by the host system to retrieve a 64-bit capsule property value
184 * from an NVMe controller on the target system.
185 *
186 * ("Capsule property" is an "PCIe register concept" applied to the
187 * NVMe fabrics space.)
188 *
189 * Return:
190 * 0: successful read
191 * > 0: NVMe error status code
192 * < 0: Linux errno error code
193 */
194int nvmf_reg_read64(struct nvme_ctrl *ctrl, u32 off, u64 *val)
195{
196 struct nvme_command cmd;
197 struct nvme_completion cqe;
198 int ret;
199
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);
205
206 ret = __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, &cqe, NULL, 0, 0,
207 NVME_QID_ANY, 0, 0);
208
209 if (ret >= 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);
215 return ret;
216}
217EXPORT_SYMBOL_GPL(nvmf_reg_read64);
218
219/**
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.
228 *
229 * Used by the NVMe host system to write a 32-bit capsule property value
230 * to an NVMe controller on the target system.
231 *
232 * ("Capsule property" is an "PCIe register concept" applied to the
233 * NVMe fabrics space.)
234 *
235 * Return:
236 * 0: successful write
237 * > 0: NVMe error status code
238 * < 0: Linux errno error code
239 */
240int nvmf_reg_write32(struct nvme_ctrl *ctrl, u32 off, u32 val)
241{
242 struct nvme_command cmd;
243 int ret;
244
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);
251
252 ret = __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, NULL, NULL, 0, 0,
253 NVME_QID_ANY, 0, 0);
254 if (unlikely(ret))
255 dev_err(ctrl->device,
256 "Property Set error: %d, offset %#x\n",
257 ret > 0 ? ret & ~NVME_SC_DNR : ret, off);
258 return ret;
259}
260EXPORT_SYMBOL_GPL(nvmf_reg_write32);
261
262/**
263 * nvmf_log_connect_error() - Error-parsing-diagnostic print
264 * out function for connect() errors.
265 *
266 * @ctrl: the specific /dev/nvmeX device that had the error.
267 *
268 * @errval: Error code to be decoded in a more human-friendly
269 * printout.
270 *
271 * @offset: For use with the NVMe error code NVME_SC_CONNECT_INVALID_PARAM.
272 *
273 * @cmd: This is the SQE portion of a submission capsule.
274 *
275 * @data: This is the "Data" portion of a submission capsule.
276 */
277static void nvmf_log_connect_error(struct nvme_ctrl *ctrl,
278 int errval, int offset, struct nvme_command *cmd,
279 struct nvmf_connect_data *data)
280{
281 int err_sctype = errval & (~NVME_SC_DNR);
282
283 switch (err_sctype) {
284
285 case (NVME_SC_CONNECT_INVALID_PARAM):
286 if (offset >> 16) {
287 char *inv_data = "Connect Invalid Data Parameter";
288
289 switch (offset & 0xffff) {
290 case (offsetof(struct nvmf_connect_data, cntlid)):
291 dev_err(ctrl->device,
292 "%s, cntlid: %d\n",
293 inv_data, data->cntlid);
294 break;
295 case (offsetof(struct nvmf_connect_data, hostnqn)):
296 dev_err(ctrl->device,
297 "%s, hostnqn \"%s\"\n",
298 inv_data, data->hostnqn);
299 break;
300 case (offsetof(struct nvmf_connect_data, subsysnqn)):
301 dev_err(ctrl->device,
302 "%s, subsysnqn \"%s\"\n",
303 inv_data, data->subsysnqn);
304 break;
305 default:
306 dev_err(ctrl->device,
307 "%s, starting byte offset: %d\n",
308 inv_data, offset & 0xffff);
309 break;
310 }
311 } else {
312 char *inv_sqe = "Connect Invalid SQE Parameter";
313
314 switch (offset) {
315 case (offsetof(struct nvmf_connect_command, qid)):
316 dev_err(ctrl->device,
317 "%s, qid %d\n",
318 inv_sqe, cmd->connect.qid);
319 break;
320 default:
321 dev_err(ctrl->device,
322 "%s, starting byte offset: %d\n",
323 inv_sqe, offset);
324 }
325 }
326 break;
327 default:
328 dev_err(ctrl->device,
329 "Connect command failed, error wo/DNR bit: %d\n",
330 err_sctype);
331 break;
332 } /* switch (err_sctype) */
333}
334
335/**
336 * nvmf_connect_admin_queue() - NVMe Fabrics Admin Queue "Connect"
337 * API function.
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
341 * that controller.
342 *
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.
348 *
349 * Return:
350 * 0: success
351 * > 0: NVMe error status code
352 * < 0: Linux errno error code
353 *
354 */
355int nvmf_connect_admin_queue(struct nvme_ctrl *ctrl)
356{
357 struct nvme_command cmd;
358 struct nvme_completion cqe;
359 struct nvmf_connect_data *data;
360 int ret;
361
362 memset(&cmd, 0, sizeof(cmd));
363 cmd.connect.opcode = nvme_fabrics_command;
364 cmd.connect.fctype = nvme_fabrics_type_connect;
365 cmd.connect.qid = 0;
f994d9dc
JF
366
367 /*
368 * fabrics spec sets a minimum of depth 32 for admin queue,
369 * so set the queue with this depth always until
370 * justification otherwise.
371 */
372 cmd.connect.sqsize = cpu_to_le16(NVMF_AQ_DEPTH - 1);
373
038bd4cb
SG
374 /*
375 * Set keep-alive timeout in seconds granularity (ms * 1000)
376 * and add a grace period for controller kato enforcement
377 */
378 cmd.connect.kato = ctrl->opts->discovery_nqn ? 0 :
379 cpu_to_le32((ctrl->kato + NVME_KATO_GRACE) * 1000);
07bfcd09
CH
380
381 data = kzalloc(sizeof(*data), GFP_KERNEL);
382 if (!data)
383 return -ENOMEM;
384
7a665d2f 385 memcpy(&data->hostid, &ctrl->opts->host->id, sizeof(uuid_be));
07bfcd09
CH
386 data->cntlid = cpu_to_le16(0xffff);
387 strncpy(data->subsysnqn, ctrl->opts->subsysnqn, NVMF_NQN_SIZE);
388 strncpy(data->hostnqn, ctrl->opts->host->nqn, NVMF_NQN_SIZE);
389
390 ret = __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, &cqe,
391 data, sizeof(*data), 0, NVME_QID_ANY, 1,
392 BLK_MQ_REQ_RESERVED | BLK_MQ_REQ_NOWAIT);
393 if (ret) {
394 nvmf_log_connect_error(ctrl, ret, le32_to_cpu(cqe.result),
395 &cmd, data);
396 goto out_free_data;
397 }
398
399 ctrl->cntlid = le16_to_cpu(cqe.result16);
400
401out_free_data:
402 kfree(data);
403 return ret;
404}
405EXPORT_SYMBOL_GPL(nvmf_connect_admin_queue);
406
407/**
408 * nvmf_connect_io_queue() - NVMe Fabrics I/O Queue "Connect"
409 * API function.
410 * @ctrl: Host nvme controller instance used to establish an
411 * NVMe I/O queue connection to the already allocated NVMe
412 * controller on the target system.
413 * @qid: NVMe I/O queue number for the new I/O connection between
414 * host and target (note qid == 0 is illegal as this is
415 * the Admin queue, per NVMe standard).
416 *
417 * This function issues a fabrics-protocol connection
418 * of a NVMe I/O queue (via NVMe Fabrics "Connect" command)
419 * between the host system device and the allocated NVMe controller
420 * on the target system.
421 *
422 * Return:
423 * 0: success
424 * > 0: NVMe error status code
425 * < 0: Linux errno error code
426 */
427int nvmf_connect_io_queue(struct nvme_ctrl *ctrl, u16 qid)
428{
429 struct nvme_command cmd;
430 struct nvmf_connect_data *data;
431 struct nvme_completion cqe;
432 int ret;
433
434 memset(&cmd, 0, sizeof(cmd));
435 cmd.connect.opcode = nvme_fabrics_command;
436 cmd.connect.fctype = nvme_fabrics_type_connect;
437 cmd.connect.qid = cpu_to_le16(qid);
438 cmd.connect.sqsize = cpu_to_le16(ctrl->sqsize);
439
440 data = kzalloc(sizeof(*data), GFP_KERNEL);
441 if (!data)
442 return -ENOMEM;
443
7a665d2f 444 memcpy(&data->hostid, &ctrl->opts->host->id, sizeof(uuid_be));
07bfcd09
CH
445 data->cntlid = cpu_to_le16(ctrl->cntlid);
446 strncpy(data->subsysnqn, ctrl->opts->subsysnqn, NVMF_NQN_SIZE);
447 strncpy(data->hostnqn, ctrl->opts->host->nqn, NVMF_NQN_SIZE);
448
449 ret = __nvme_submit_sync_cmd(ctrl->connect_q, &cmd, &cqe,
450 data, sizeof(*data), 0, qid, 1,
451 BLK_MQ_REQ_RESERVED | BLK_MQ_REQ_NOWAIT);
452 if (ret) {
453 nvmf_log_connect_error(ctrl, ret, le32_to_cpu(cqe.result),
454 &cmd, data);
455 }
456 kfree(data);
457 return ret;
458}
459EXPORT_SYMBOL_GPL(nvmf_connect_io_queue);
460
461/**
462 * nvmf_register_transport() - NVMe Fabrics Library registration function.
463 * @ops: Transport ops instance to be registered to the
464 * common fabrics library.
465 *
466 * API function that registers the type of specific transport fabric
467 * being implemented to the common NVMe fabrics library. Part of
468 * the overall init sequence of starting up a fabrics driver.
469 */
470void nvmf_register_transport(struct nvmf_transport_ops *ops)
471{
472 mutex_lock(&nvmf_transports_mutex);
473 list_add_tail(&ops->entry, &nvmf_transports);
474 mutex_unlock(&nvmf_transports_mutex);
475}
476EXPORT_SYMBOL_GPL(nvmf_register_transport);
477
478/**
479 * nvmf_unregister_transport() - NVMe Fabrics Library unregistration function.
480 * @ops: Transport ops instance to be unregistered from the
481 * common fabrics library.
482 *
483 * Fabrics API function that unregisters the type of specific transport
484 * fabric being implemented from the common NVMe fabrics library.
485 * Part of the overall exit sequence of unloading the implemented driver.
486 */
487void nvmf_unregister_transport(struct nvmf_transport_ops *ops)
488{
489 mutex_lock(&nvmf_transports_mutex);
490 list_del(&ops->entry);
491 mutex_unlock(&nvmf_transports_mutex);
492}
493EXPORT_SYMBOL_GPL(nvmf_unregister_transport);
494
495static struct nvmf_transport_ops *nvmf_lookup_transport(
496 struct nvmf_ctrl_options *opts)
497{
498 struct nvmf_transport_ops *ops;
499
500 lockdep_assert_held(&nvmf_transports_mutex);
501
502 list_for_each_entry(ops, &nvmf_transports, entry) {
503 if (strcmp(ops->name, opts->transport) == 0)
504 return ops;
505 }
506
507 return NULL;
508}
509
510static const match_table_t opt_tokens = {
511 { NVMF_OPT_TRANSPORT, "transport=%s" },
512 { NVMF_OPT_TRADDR, "traddr=%s" },
513 { NVMF_OPT_TRSVCID, "trsvcid=%s" },
514 { NVMF_OPT_NQN, "nqn=%s" },
515 { NVMF_OPT_QUEUE_SIZE, "queue_size=%d" },
516 { NVMF_OPT_NR_IO_QUEUES, "nr_io_queues=%d" },
07bfcd09 517 { NVMF_OPT_RECONNECT_DELAY, "reconnect_delay=%d" },
038bd4cb 518 { NVMF_OPT_KATO, "keep_alive_tmo=%d" },
07bfcd09
CH
519 { NVMF_OPT_HOSTNQN, "hostnqn=%s" },
520 { NVMF_OPT_ERR, NULL }
521};
522
523static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
524 const char *buf)
525{
526 substring_t args[MAX_OPT_ARGS];
527 char *options, *o, *p;
528 int token, ret = 0;
529 size_t nqnlen = 0;
530
531 /* Set defaults */
532 opts->queue_size = NVMF_DEF_QUEUE_SIZE;
533 opts->nr_io_queues = num_online_cpus();
07bfcd09
CH
534 opts->reconnect_delay = NVMF_DEF_RECONNECT_DELAY;
535
536 options = o = kstrdup(buf, GFP_KERNEL);
537 if (!options)
538 return -ENOMEM;
539
540 while ((p = strsep(&o, ",\n")) != NULL) {
541 if (!*p)
542 continue;
543
544 token = match_token(p, opt_tokens, args);
545 opts->mask |= token;
546 switch (token) {
547 case NVMF_OPT_TRANSPORT:
548 p = match_strdup(args);
549 if (!p) {
550 ret = -ENOMEM;
551 goto out;
552 }
553 opts->transport = p;
554 break;
555 case NVMF_OPT_NQN:
556 p = match_strdup(args);
557 if (!p) {
558 ret = -ENOMEM;
559 goto out;
560 }
561 opts->subsysnqn = p;
562 nqnlen = strlen(opts->subsysnqn);
563 if (nqnlen >= NVMF_NQN_SIZE) {
564 pr_err("%s needs to be < %d bytes\n",
565 opts->subsysnqn, NVMF_NQN_SIZE);
566 ret = -EINVAL;
567 goto out;
568 }
569 opts->discovery_nqn =
570 !(strcmp(opts->subsysnqn,
571 NVME_DISC_SUBSYS_NAME));
572 if (opts->discovery_nqn)
573 opts->nr_io_queues = 0;
574 break;
575 case NVMF_OPT_TRADDR:
576 p = match_strdup(args);
577 if (!p) {
578 ret = -ENOMEM;
579 goto out;
580 }
581 opts->traddr = p;
582 break;
583 case NVMF_OPT_TRSVCID:
584 p = match_strdup(args);
585 if (!p) {
586 ret = -ENOMEM;
587 goto out;
588 }
589 opts->trsvcid = p;
590 break;
591 case NVMF_OPT_QUEUE_SIZE:
592 if (match_int(args, &token)) {
593 ret = -EINVAL;
594 goto out;
595 }
596 if (token < NVMF_MIN_QUEUE_SIZE ||
597 token > NVMF_MAX_QUEUE_SIZE) {
598 pr_err("Invalid queue_size %d\n", token);
599 ret = -EINVAL;
600 goto out;
601 }
602 opts->queue_size = token;
603 break;
604 case NVMF_OPT_NR_IO_QUEUES:
605 if (match_int(args, &token)) {
606 ret = -EINVAL;
607 goto out;
608 }
609 if (token <= 0) {
610 pr_err("Invalid number of IOQs %d\n", token);
611 ret = -EINVAL;
612 goto out;
613 }
614 opts->nr_io_queues = min_t(unsigned int,
615 num_online_cpus(), token);
616 break;
038bd4cb
SG
617 case NVMF_OPT_KATO:
618 if (match_int(args, &token)) {
619 ret = -EINVAL;
620 goto out;
621 }
622
623 if (opts->discovery_nqn) {
624 pr_err("Discovery controllers cannot accept keep_alive_tmo != 0\n");
625 ret = -EINVAL;
626 goto out;
627 }
628
629 if (token < 0) {
630 pr_err("Invalid keep_alive_tmo %d\n", token);
631 ret = -EINVAL;
632 goto out;
633 } else if (token == 0) {
634 /* Allowed for debug */
635 pr_warn("keep_alive_tmo 0 won't execute keep alives!!!\n");
636 }
637 opts->kato = token;
638 break;
07bfcd09
CH
639 case NVMF_OPT_HOSTNQN:
640 if (opts->host) {
641 pr_err("hostnqn already user-assigned: %s\n",
642 opts->host->nqn);
643 ret = -EADDRINUSE;
644 goto out;
645 }
646 p = match_strdup(args);
647 if (!p) {
648 ret = -ENOMEM;
649 goto out;
650 }
651 nqnlen = strlen(p);
652 if (nqnlen >= NVMF_NQN_SIZE) {
653 pr_err("%s needs to be < %d bytes\n",
654 p, NVMF_NQN_SIZE);
655 ret = -EINVAL;
656 goto out;
657 }
658 opts->host = nvmf_host_add(p);
659 if (!opts->host) {
660 ret = -ENOMEM;
661 goto out;
662 }
663 break;
664 case NVMF_OPT_RECONNECT_DELAY:
665 if (match_int(args, &token)) {
666 ret = -EINVAL;
667 goto out;
668 }
669 if (token <= 0) {
670 pr_err("Invalid reconnect_delay %d\n", token);
671 ret = -EINVAL;
672 goto out;
673 }
674 opts->reconnect_delay = token;
675 break;
676 default:
677 pr_warn("unknown parameter or missing value '%s' in ctrl creation request\n",
678 p);
679 ret = -EINVAL;
680 goto out;
681 }
682 }
683
684 if (!opts->host) {
685 kref_get(&nvmf_default_host->ref);
686 opts->host = nvmf_default_host;
687 }
688
689out:
038bd4cb
SG
690 if (!opts->discovery_nqn && !opts->kato)
691 opts->kato = NVME_DEFAULT_KATO;
07bfcd09
CH
692 kfree(options);
693 return ret;
694}
695
696static int nvmf_check_required_opts(struct nvmf_ctrl_options *opts,
697 unsigned int required_opts)
698{
699 if ((opts->mask & required_opts) != required_opts) {
700 int i;
701
702 for (i = 0; i < ARRAY_SIZE(opt_tokens); i++) {
703 if ((opt_tokens[i].token & required_opts) &&
704 !(opt_tokens[i].token & opts->mask)) {
705 pr_warn("missing parameter '%s'\n",
706 opt_tokens[i].pattern);
707 }
708 }
709
710 return -EINVAL;
711 }
712
713 return 0;
714}
715
716static int nvmf_check_allowed_opts(struct nvmf_ctrl_options *opts,
717 unsigned int allowed_opts)
718{
719 if (opts->mask & ~allowed_opts) {
720 int i;
721
722 for (i = 0; i < ARRAY_SIZE(opt_tokens); i++) {
723 if (opt_tokens[i].token & ~allowed_opts) {
724 pr_warn("invalid parameter '%s'\n",
725 opt_tokens[i].pattern);
726 }
727 }
728
729 return -EINVAL;
730 }
731
732 return 0;
733}
734
735void nvmf_free_options(struct nvmf_ctrl_options *opts)
736{
737 nvmf_host_put(opts->host);
738 kfree(opts->transport);
739 kfree(opts->traddr);
740 kfree(opts->trsvcid);
741 kfree(opts->subsysnqn);
742 kfree(opts);
743}
744EXPORT_SYMBOL_GPL(nvmf_free_options);
745
746#define NVMF_REQUIRED_OPTS (NVMF_OPT_TRANSPORT | NVMF_OPT_NQN)
747#define NVMF_ALLOWED_OPTS (NVMF_OPT_QUEUE_SIZE | NVMF_OPT_NR_IO_QUEUES | \
038bd4cb 748 NVMF_OPT_KATO | NVMF_OPT_HOSTNQN)
07bfcd09
CH
749
750static struct nvme_ctrl *
751nvmf_create_ctrl(struct device *dev, const char *buf, size_t count)
752{
753 struct nvmf_ctrl_options *opts;
754 struct nvmf_transport_ops *ops;
755 struct nvme_ctrl *ctrl;
756 int ret;
757
758 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
759 if (!opts)
760 return ERR_PTR(-ENOMEM);
761
762 ret = nvmf_parse_options(opts, buf);
763 if (ret)
764 goto out_free_opts;
765
766 /*
767 * Check the generic options first as we need a valid transport for
768 * the lookup below. Then clear the generic flags so that transport
769 * drivers don't have to care about them.
770 */
771 ret = nvmf_check_required_opts(opts, NVMF_REQUIRED_OPTS);
772 if (ret)
773 goto out_free_opts;
774 opts->mask &= ~NVMF_REQUIRED_OPTS;
775
776 mutex_lock(&nvmf_transports_mutex);
777 ops = nvmf_lookup_transport(opts);
778 if (!ops) {
779 pr_info("no handler found for transport %s.\n",
780 opts->transport);
781 ret = -EINVAL;
782 goto out_unlock;
783 }
784
785 ret = nvmf_check_required_opts(opts, ops->required_opts);
786 if (ret)
787 goto out_unlock;
788 ret = nvmf_check_allowed_opts(opts, NVMF_ALLOWED_OPTS |
789 ops->allowed_opts | ops->required_opts);
790 if (ret)
791 goto out_unlock;
792
793 ctrl = ops->create_ctrl(dev, opts);
794 if (IS_ERR(ctrl)) {
795 ret = PTR_ERR(ctrl);
796 goto out_unlock;
797 }
798
799 mutex_unlock(&nvmf_transports_mutex);
800 return ctrl;
801
802out_unlock:
803 mutex_unlock(&nvmf_transports_mutex);
804out_free_opts:
805 nvmf_host_put(opts->host);
806 kfree(opts);
807 return ERR_PTR(ret);
808}
809
810static struct class *nvmf_class;
811static struct device *nvmf_device;
812static DEFINE_MUTEX(nvmf_dev_mutex);
813
814static ssize_t nvmf_dev_write(struct file *file, const char __user *ubuf,
815 size_t count, loff_t *pos)
816{
817 struct seq_file *seq_file = file->private_data;
818 struct nvme_ctrl *ctrl;
819 const char *buf;
820 int ret = 0;
821
822 if (count > PAGE_SIZE)
823 return -ENOMEM;
824
825 buf = memdup_user_nul(ubuf, count);
826 if (IS_ERR(buf))
827 return PTR_ERR(buf);
828
829 mutex_lock(&nvmf_dev_mutex);
830 if (seq_file->private) {
831 ret = -EINVAL;
832 goto out_unlock;
833 }
834
835 ctrl = nvmf_create_ctrl(nvmf_device, buf, count);
836 if (IS_ERR(ctrl)) {
837 ret = PTR_ERR(ctrl);
838 goto out_unlock;
839 }
840
841 seq_file->private = ctrl;
842
843out_unlock:
844 mutex_unlock(&nvmf_dev_mutex);
845 kfree(buf);
846 return ret ? ret : count;
847}
848
849static int nvmf_dev_show(struct seq_file *seq_file, void *private)
850{
851 struct nvme_ctrl *ctrl;
852 int ret = 0;
853
854 mutex_lock(&nvmf_dev_mutex);
855 ctrl = seq_file->private;
856 if (!ctrl) {
857 ret = -EINVAL;
858 goto out_unlock;
859 }
860
861 seq_printf(seq_file, "instance=%d,cntlid=%d\n",
862 ctrl->instance, ctrl->cntlid);
863
864out_unlock:
865 mutex_unlock(&nvmf_dev_mutex);
866 return ret;
867}
868
869static int nvmf_dev_open(struct inode *inode, struct file *file)
870{
871 /*
872 * The miscdevice code initializes file->private_data, but doesn't
873 * make use of it later.
874 */
875 file->private_data = NULL;
876 return single_open(file, nvmf_dev_show, NULL);
877}
878
879static int nvmf_dev_release(struct inode *inode, struct file *file)
880{
881 struct seq_file *seq_file = file->private_data;
882 struct nvme_ctrl *ctrl = seq_file->private;
883
884 if (ctrl)
885 nvme_put_ctrl(ctrl);
886 return single_release(inode, file);
887}
888
889static const struct file_operations nvmf_dev_fops = {
890 .owner = THIS_MODULE,
891 .write = nvmf_dev_write,
892 .read = seq_read,
893 .open = nvmf_dev_open,
894 .release = nvmf_dev_release,
895};
896
897static struct miscdevice nvmf_misc = {
898 .minor = MISC_DYNAMIC_MINOR,
899 .name = "nvme-fabrics",
900 .fops = &nvmf_dev_fops,
901};
902
903static int __init nvmf_init(void)
904{
905 int ret;
906
907 nvmf_default_host = nvmf_host_default();
908 if (!nvmf_default_host)
909 return -ENOMEM;
910
911 nvmf_class = class_create(THIS_MODULE, "nvme-fabrics");
912 if (IS_ERR(nvmf_class)) {
913 pr_err("couldn't register class nvme-fabrics\n");
914 ret = PTR_ERR(nvmf_class);
915 goto out_free_host;
916 }
917
918 nvmf_device =
919 device_create(nvmf_class, NULL, MKDEV(0, 0), NULL, "ctl");
920 if (IS_ERR(nvmf_device)) {
921 pr_err("couldn't create nvme-fabris device!\n");
922 ret = PTR_ERR(nvmf_device);
923 goto out_destroy_class;
924 }
925
926 ret = misc_register(&nvmf_misc);
927 if (ret) {
928 pr_err("couldn't register misc device: %d\n", ret);
929 goto out_destroy_device;
930 }
931
932 return 0;
933
934out_destroy_device:
935 device_destroy(nvmf_class, MKDEV(0, 0));
936out_destroy_class:
937 class_destroy(nvmf_class);
938out_free_host:
939 nvmf_host_put(nvmf_default_host);
940 return ret;
941}
942
943static void __exit nvmf_exit(void)
944{
945 misc_deregister(&nvmf_misc);
946 device_destroy(nvmf_class, MKDEV(0, 0));
947 class_destroy(nvmf_class);
948 nvmf_host_put(nvmf_default_host);
949
950 BUILD_BUG_ON(sizeof(struct nvmf_connect_command) != 64);
951 BUILD_BUG_ON(sizeof(struct nvmf_property_get_command) != 64);
952 BUILD_BUG_ON(sizeof(struct nvmf_property_set_command) != 64);
953 BUILD_BUG_ON(sizeof(struct nvmf_connect_data) != 1024);
954}
955
956MODULE_LICENSE("GPL v2");
957
958module_init(nvmf_init);
959module_exit(nvmf_exit);