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