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