xref: /openbmc/linux/drivers/nvme/host/fabrics.c (revision 80483c3a)
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 	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 	return snprintf(buf, size, "traddr=%s,trsvcid=%s\n",
113 			ctrl->opts->traddr, ctrl->opts->trsvcid);
114 }
115 EXPORT_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  */
121 const char *nvmf_get_subsysnqn(struct nvme_ctrl *ctrl)
122 {
123 	return ctrl->opts->subsysnqn;
124 }
125 EXPORT_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  */
148 int 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 }
171 EXPORT_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  */
194 int 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 }
217 EXPORT_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  */
240 int 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 }
260 EXPORT_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  */
277 static void nvmf_log_connect_error(struct nvme_ctrl *ctrl,
278 		int errval, int offset, struct nvme_command *cmd,
279 		struct nvmf_connect_data *data)
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  */
355 int 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;
366 	cmd.connect.sqsize = cpu_to_le16(ctrl->sqsize);
367 	/*
368 	 * Set keep-alive timeout in seconds granularity (ms * 1000)
369 	 * and add a grace period for controller kato enforcement
370 	 */
371 	cmd.connect.kato = ctrl->opts->discovery_nqn ? 0 :
372 		cpu_to_le32((ctrl->kato + NVME_KATO_GRACE) * 1000);
373 
374 	data = kzalloc(sizeof(*data), GFP_KERNEL);
375 	if (!data)
376 		return -ENOMEM;
377 
378 	memcpy(&data->hostid, &ctrl->opts->host->id, sizeof(uuid_le));
379 	data->cntlid = cpu_to_le16(0xffff);
380 	strncpy(data->subsysnqn, ctrl->opts->subsysnqn, NVMF_NQN_SIZE);
381 	strncpy(data->hostnqn, ctrl->opts->host->nqn, NVMF_NQN_SIZE);
382 
383 	ret = __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, &cqe,
384 			data, sizeof(*data), 0, NVME_QID_ANY, 1,
385 			BLK_MQ_REQ_RESERVED | BLK_MQ_REQ_NOWAIT);
386 	if (ret) {
387 		nvmf_log_connect_error(ctrl, ret, le32_to_cpu(cqe.result),
388 				       &cmd, data);
389 		goto out_free_data;
390 	}
391 
392 	ctrl->cntlid = le16_to_cpu(cqe.result16);
393 
394 out_free_data:
395 	kfree(data);
396 	return ret;
397 }
398 EXPORT_SYMBOL_GPL(nvmf_connect_admin_queue);
399 
400 /**
401  * nvmf_connect_io_queue() - NVMe Fabrics I/O Queue "Connect"
402  *			     API function.
403  * @ctrl:	Host nvme controller instance used to establish an
404  *		NVMe I/O queue connection to the already allocated NVMe
405  *		controller on the target system.
406  * @qid:	NVMe I/O queue number for the new I/O connection between
407  *		host and target (note qid == 0 is illegal as this is
408  *		the Admin queue, per NVMe standard).
409  *
410  * This function issues a fabrics-protocol connection
411  * of a NVMe I/O queue (via NVMe Fabrics "Connect" command)
412  * between the host system device and the allocated NVMe controller
413  * on the target system.
414  *
415  * Return:
416  *	0: success
417  *	> 0: NVMe error status code
418  *	< 0: Linux errno error code
419  */
420 int nvmf_connect_io_queue(struct nvme_ctrl *ctrl, u16 qid)
421 {
422 	struct nvme_command cmd;
423 	struct nvmf_connect_data *data;
424 	struct nvme_completion cqe;
425 	int ret;
426 
427 	memset(&cmd, 0, sizeof(cmd));
428 	cmd.connect.opcode = nvme_fabrics_command;
429 	cmd.connect.fctype = nvme_fabrics_type_connect;
430 	cmd.connect.qid = cpu_to_le16(qid);
431 	cmd.connect.sqsize = cpu_to_le16(ctrl->sqsize);
432 
433 	data = kzalloc(sizeof(*data), GFP_KERNEL);
434 	if (!data)
435 		return -ENOMEM;
436 
437 	memcpy(&data->hostid, &ctrl->opts->host->id, sizeof(uuid_le));
438 	data->cntlid = cpu_to_le16(ctrl->cntlid);
439 	strncpy(data->subsysnqn, ctrl->opts->subsysnqn, NVMF_NQN_SIZE);
440 	strncpy(data->hostnqn, ctrl->opts->host->nqn, NVMF_NQN_SIZE);
441 
442 	ret = __nvme_submit_sync_cmd(ctrl->connect_q, &cmd, &cqe,
443 			data, sizeof(*data), 0, qid, 1,
444 			BLK_MQ_REQ_RESERVED | BLK_MQ_REQ_NOWAIT);
445 	if (ret) {
446 		nvmf_log_connect_error(ctrl, ret, le32_to_cpu(cqe.result),
447 				       &cmd, data);
448 	}
449 	kfree(data);
450 	return ret;
451 }
452 EXPORT_SYMBOL_GPL(nvmf_connect_io_queue);
453 
454 /**
455  * nvmf_register_transport() - NVMe Fabrics Library registration function.
456  * @ops:	Transport ops instance to be registered to the
457  *		common fabrics library.
458  *
459  * API function that registers the type of specific transport fabric
460  * being implemented to the common NVMe fabrics library. Part of
461  * the overall init sequence of starting up a fabrics driver.
462  */
463 void nvmf_register_transport(struct nvmf_transport_ops *ops)
464 {
465 	mutex_lock(&nvmf_transports_mutex);
466 	list_add_tail(&ops->entry, &nvmf_transports);
467 	mutex_unlock(&nvmf_transports_mutex);
468 }
469 EXPORT_SYMBOL_GPL(nvmf_register_transport);
470 
471 /**
472  * nvmf_unregister_transport() - NVMe Fabrics Library unregistration function.
473  * @ops:	Transport ops instance to be unregistered from the
474  *		common fabrics library.
475  *
476  * Fabrics API function that unregisters the type of specific transport
477  * fabric being implemented from the common NVMe fabrics library.
478  * Part of the overall exit sequence of unloading the implemented driver.
479  */
480 void nvmf_unregister_transport(struct nvmf_transport_ops *ops)
481 {
482 	mutex_lock(&nvmf_transports_mutex);
483 	list_del(&ops->entry);
484 	mutex_unlock(&nvmf_transports_mutex);
485 }
486 EXPORT_SYMBOL_GPL(nvmf_unregister_transport);
487 
488 static struct nvmf_transport_ops *nvmf_lookup_transport(
489 		struct nvmf_ctrl_options *opts)
490 {
491 	struct nvmf_transport_ops *ops;
492 
493 	lockdep_assert_held(&nvmf_transports_mutex);
494 
495 	list_for_each_entry(ops, &nvmf_transports, entry) {
496 		if (strcmp(ops->name, opts->transport) == 0)
497 			return ops;
498 	}
499 
500 	return NULL;
501 }
502 
503 static const match_table_t opt_tokens = {
504 	{ NVMF_OPT_TRANSPORT,		"transport=%s"		},
505 	{ NVMF_OPT_TRADDR,		"traddr=%s"		},
506 	{ NVMF_OPT_TRSVCID,		"trsvcid=%s"		},
507 	{ NVMF_OPT_NQN,			"nqn=%s"		},
508 	{ NVMF_OPT_QUEUE_SIZE,		"queue_size=%d"		},
509 	{ NVMF_OPT_NR_IO_QUEUES,	"nr_io_queues=%d"	},
510 	{ NVMF_OPT_RECONNECT_DELAY,	"reconnect_delay=%d"	},
511 	{ NVMF_OPT_KATO,		"keep_alive_tmo=%d"	},
512 	{ NVMF_OPT_HOSTNQN,		"hostnqn=%s"		},
513 	{ NVMF_OPT_ERR,			NULL			}
514 };
515 
516 static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
517 		const char *buf)
518 {
519 	substring_t args[MAX_OPT_ARGS];
520 	char *options, *o, *p;
521 	int token, ret = 0;
522 	size_t nqnlen  = 0;
523 
524 	/* Set defaults */
525 	opts->queue_size = NVMF_DEF_QUEUE_SIZE;
526 	opts->nr_io_queues = num_online_cpus();
527 	opts->reconnect_delay = NVMF_DEF_RECONNECT_DELAY;
528 
529 	options = o = kstrdup(buf, GFP_KERNEL);
530 	if (!options)
531 		return -ENOMEM;
532 
533 	while ((p = strsep(&o, ",\n")) != NULL) {
534 		if (!*p)
535 			continue;
536 
537 		token = match_token(p, opt_tokens, args);
538 		opts->mask |= token;
539 		switch (token) {
540 		case NVMF_OPT_TRANSPORT:
541 			p = match_strdup(args);
542 			if (!p) {
543 				ret = -ENOMEM;
544 				goto out;
545 			}
546 			opts->transport = p;
547 			break;
548 		case NVMF_OPT_NQN:
549 			p = match_strdup(args);
550 			if (!p) {
551 				ret = -ENOMEM;
552 				goto out;
553 			}
554 			opts->subsysnqn = p;
555 			nqnlen = strlen(opts->subsysnqn);
556 			if (nqnlen >= NVMF_NQN_SIZE) {
557 				pr_err("%s needs to be < %d bytes\n",
558 				opts->subsysnqn, NVMF_NQN_SIZE);
559 				ret = -EINVAL;
560 				goto out;
561 			}
562 			opts->discovery_nqn =
563 				!(strcmp(opts->subsysnqn,
564 					 NVME_DISC_SUBSYS_NAME));
565 			if (opts->discovery_nqn)
566 				opts->nr_io_queues = 0;
567 			break;
568 		case NVMF_OPT_TRADDR:
569 			p = match_strdup(args);
570 			if (!p) {
571 				ret = -ENOMEM;
572 				goto out;
573 			}
574 			opts->traddr = p;
575 			break;
576 		case NVMF_OPT_TRSVCID:
577 			p = match_strdup(args);
578 			if (!p) {
579 				ret = -ENOMEM;
580 				goto out;
581 			}
582 			opts->trsvcid = p;
583 			break;
584 		case NVMF_OPT_QUEUE_SIZE:
585 			if (match_int(args, &token)) {
586 				ret = -EINVAL;
587 				goto out;
588 			}
589 			if (token < NVMF_MIN_QUEUE_SIZE ||
590 			    token > NVMF_MAX_QUEUE_SIZE) {
591 				pr_err("Invalid queue_size %d\n", token);
592 				ret = -EINVAL;
593 				goto out;
594 			}
595 			opts->queue_size = token;
596 			break;
597 		case NVMF_OPT_NR_IO_QUEUES:
598 			if (match_int(args, &token)) {
599 				ret = -EINVAL;
600 				goto out;
601 			}
602 			if (token <= 0) {
603 				pr_err("Invalid number of IOQs %d\n", token);
604 				ret = -EINVAL;
605 				goto out;
606 			}
607 			opts->nr_io_queues = min_t(unsigned int,
608 					num_online_cpus(), token);
609 			break;
610 		case NVMF_OPT_KATO:
611 			if (match_int(args, &token)) {
612 				ret = -EINVAL;
613 				goto out;
614 			}
615 
616 			if (opts->discovery_nqn) {
617 				pr_err("Discovery controllers cannot accept keep_alive_tmo != 0\n");
618 				ret = -EINVAL;
619 				goto out;
620 			}
621 
622 			if (token < 0) {
623 				pr_err("Invalid keep_alive_tmo %d\n", token);
624 				ret = -EINVAL;
625 				goto out;
626 			} else if (token == 0) {
627 				/* Allowed for debug */
628 				pr_warn("keep_alive_tmo 0 won't execute keep alives!!!\n");
629 			}
630 			opts->kato = token;
631 			break;
632 		case NVMF_OPT_HOSTNQN:
633 			if (opts->host) {
634 				pr_err("hostnqn already user-assigned: %s\n",
635 				       opts->host->nqn);
636 				ret = -EADDRINUSE;
637 				goto out;
638 			}
639 			p = match_strdup(args);
640 			if (!p) {
641 				ret = -ENOMEM;
642 				goto out;
643 			}
644 			nqnlen = strlen(p);
645 			if (nqnlen >= NVMF_NQN_SIZE) {
646 				pr_err("%s needs to be < %d bytes\n",
647 					p, NVMF_NQN_SIZE);
648 				ret = -EINVAL;
649 				goto out;
650 			}
651 			opts->host = nvmf_host_add(p);
652 			if (!opts->host) {
653 				ret = -ENOMEM;
654 				goto out;
655 			}
656 			break;
657 		case NVMF_OPT_RECONNECT_DELAY:
658 			if (match_int(args, &token)) {
659 				ret = -EINVAL;
660 				goto out;
661 			}
662 			if (token <= 0) {
663 				pr_err("Invalid reconnect_delay %d\n", token);
664 				ret = -EINVAL;
665 				goto out;
666 			}
667 			opts->reconnect_delay = token;
668 			break;
669 		default:
670 			pr_warn("unknown parameter or missing value '%s' in ctrl creation request\n",
671 				p);
672 			ret = -EINVAL;
673 			goto out;
674 		}
675 	}
676 
677 	if (!opts->host) {
678 		kref_get(&nvmf_default_host->ref);
679 		opts->host = nvmf_default_host;
680 	}
681 
682 out:
683 	if (!opts->discovery_nqn && !opts->kato)
684 		opts->kato = NVME_DEFAULT_KATO;
685 	kfree(options);
686 	return ret;
687 }
688 
689 static int nvmf_check_required_opts(struct nvmf_ctrl_options *opts,
690 		unsigned int required_opts)
691 {
692 	if ((opts->mask & required_opts) != required_opts) {
693 		int i;
694 
695 		for (i = 0; i < ARRAY_SIZE(opt_tokens); i++) {
696 			if ((opt_tokens[i].token & required_opts) &&
697 			    !(opt_tokens[i].token & opts->mask)) {
698 				pr_warn("missing parameter '%s'\n",
699 					opt_tokens[i].pattern);
700 			}
701 		}
702 
703 		return -EINVAL;
704 	}
705 
706 	return 0;
707 }
708 
709 static int nvmf_check_allowed_opts(struct nvmf_ctrl_options *opts,
710 		unsigned int allowed_opts)
711 {
712 	if (opts->mask & ~allowed_opts) {
713 		int i;
714 
715 		for (i = 0; i < ARRAY_SIZE(opt_tokens); i++) {
716 			if (opt_tokens[i].token & ~allowed_opts) {
717 				pr_warn("invalid parameter '%s'\n",
718 					opt_tokens[i].pattern);
719 			}
720 		}
721 
722 		return -EINVAL;
723 	}
724 
725 	return 0;
726 }
727 
728 void nvmf_free_options(struct nvmf_ctrl_options *opts)
729 {
730 	nvmf_host_put(opts->host);
731 	kfree(opts->transport);
732 	kfree(opts->traddr);
733 	kfree(opts->trsvcid);
734 	kfree(opts->subsysnqn);
735 	kfree(opts);
736 }
737 EXPORT_SYMBOL_GPL(nvmf_free_options);
738 
739 #define NVMF_REQUIRED_OPTS	(NVMF_OPT_TRANSPORT | NVMF_OPT_NQN)
740 #define NVMF_ALLOWED_OPTS	(NVMF_OPT_QUEUE_SIZE | NVMF_OPT_NR_IO_QUEUES | \
741 				 NVMF_OPT_KATO | NVMF_OPT_HOSTNQN)
742 
743 static struct nvme_ctrl *
744 nvmf_create_ctrl(struct device *dev, const char *buf, size_t count)
745 {
746 	struct nvmf_ctrl_options *opts;
747 	struct nvmf_transport_ops *ops;
748 	struct nvme_ctrl *ctrl;
749 	int ret;
750 
751 	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
752 	if (!opts)
753 		return ERR_PTR(-ENOMEM);
754 
755 	ret = nvmf_parse_options(opts, buf);
756 	if (ret)
757 		goto out_free_opts;
758 
759 	/*
760 	 * Check the generic options first as we need a valid transport for
761 	 * the lookup below.  Then clear the generic flags so that transport
762 	 * drivers don't have to care about them.
763 	 */
764 	ret = nvmf_check_required_opts(opts, NVMF_REQUIRED_OPTS);
765 	if (ret)
766 		goto out_free_opts;
767 	opts->mask &= ~NVMF_REQUIRED_OPTS;
768 
769 	mutex_lock(&nvmf_transports_mutex);
770 	ops = nvmf_lookup_transport(opts);
771 	if (!ops) {
772 		pr_info("no handler found for transport %s.\n",
773 			opts->transport);
774 		ret = -EINVAL;
775 		goto out_unlock;
776 	}
777 
778 	ret = nvmf_check_required_opts(opts, ops->required_opts);
779 	if (ret)
780 		goto out_unlock;
781 	ret = nvmf_check_allowed_opts(opts, NVMF_ALLOWED_OPTS |
782 				ops->allowed_opts | ops->required_opts);
783 	if (ret)
784 		goto out_unlock;
785 
786 	ctrl = ops->create_ctrl(dev, opts);
787 	if (IS_ERR(ctrl)) {
788 		ret = PTR_ERR(ctrl);
789 		goto out_unlock;
790 	}
791 
792 	mutex_unlock(&nvmf_transports_mutex);
793 	return ctrl;
794 
795 out_unlock:
796 	mutex_unlock(&nvmf_transports_mutex);
797 out_free_opts:
798 	nvmf_host_put(opts->host);
799 	kfree(opts);
800 	return ERR_PTR(ret);
801 }
802 
803 static struct class *nvmf_class;
804 static struct device *nvmf_device;
805 static DEFINE_MUTEX(nvmf_dev_mutex);
806 
807 static ssize_t nvmf_dev_write(struct file *file, const char __user *ubuf,
808 		size_t count, loff_t *pos)
809 {
810 	struct seq_file *seq_file = file->private_data;
811 	struct nvme_ctrl *ctrl;
812 	const char *buf;
813 	int ret = 0;
814 
815 	if (count > PAGE_SIZE)
816 		return -ENOMEM;
817 
818 	buf = memdup_user_nul(ubuf, count);
819 	if (IS_ERR(buf))
820 		return PTR_ERR(buf);
821 
822 	mutex_lock(&nvmf_dev_mutex);
823 	if (seq_file->private) {
824 		ret = -EINVAL;
825 		goto out_unlock;
826 	}
827 
828 	ctrl = nvmf_create_ctrl(nvmf_device, buf, count);
829 	if (IS_ERR(ctrl)) {
830 		ret = PTR_ERR(ctrl);
831 		goto out_unlock;
832 	}
833 
834 	seq_file->private = ctrl;
835 
836 out_unlock:
837 	mutex_unlock(&nvmf_dev_mutex);
838 	kfree(buf);
839 	return ret ? ret : count;
840 }
841 
842 static int nvmf_dev_show(struct seq_file *seq_file, void *private)
843 {
844 	struct nvme_ctrl *ctrl;
845 	int ret = 0;
846 
847 	mutex_lock(&nvmf_dev_mutex);
848 	ctrl = seq_file->private;
849 	if (!ctrl) {
850 		ret = -EINVAL;
851 		goto out_unlock;
852 	}
853 
854 	seq_printf(seq_file, "instance=%d,cntlid=%d\n",
855 			ctrl->instance, ctrl->cntlid);
856 
857 out_unlock:
858 	mutex_unlock(&nvmf_dev_mutex);
859 	return ret;
860 }
861 
862 static int nvmf_dev_open(struct inode *inode, struct file *file)
863 {
864 	/*
865 	 * The miscdevice code initializes file->private_data, but doesn't
866 	 * make use of it later.
867 	 */
868 	file->private_data = NULL;
869 	return single_open(file, nvmf_dev_show, NULL);
870 }
871 
872 static int nvmf_dev_release(struct inode *inode, struct file *file)
873 {
874 	struct seq_file *seq_file = file->private_data;
875 	struct nvme_ctrl *ctrl = seq_file->private;
876 
877 	if (ctrl)
878 		nvme_put_ctrl(ctrl);
879 	return single_release(inode, file);
880 }
881 
882 static const struct file_operations nvmf_dev_fops = {
883 	.owner		= THIS_MODULE,
884 	.write		= nvmf_dev_write,
885 	.read		= seq_read,
886 	.open		= nvmf_dev_open,
887 	.release	= nvmf_dev_release,
888 };
889 
890 static struct miscdevice nvmf_misc = {
891 	.minor		= MISC_DYNAMIC_MINOR,
892 	.name           = "nvme-fabrics",
893 	.fops		= &nvmf_dev_fops,
894 };
895 
896 static int __init nvmf_init(void)
897 {
898 	int ret;
899 
900 	nvmf_default_host = nvmf_host_default();
901 	if (!nvmf_default_host)
902 		return -ENOMEM;
903 
904 	nvmf_class = class_create(THIS_MODULE, "nvme-fabrics");
905 	if (IS_ERR(nvmf_class)) {
906 		pr_err("couldn't register class nvme-fabrics\n");
907 		ret = PTR_ERR(nvmf_class);
908 		goto out_free_host;
909 	}
910 
911 	nvmf_device =
912 		device_create(nvmf_class, NULL, MKDEV(0, 0), NULL, "ctl");
913 	if (IS_ERR(nvmf_device)) {
914 		pr_err("couldn't create nvme-fabris device!\n");
915 		ret = PTR_ERR(nvmf_device);
916 		goto out_destroy_class;
917 	}
918 
919 	ret = misc_register(&nvmf_misc);
920 	if (ret) {
921 		pr_err("couldn't register misc device: %d\n", ret);
922 		goto out_destroy_device;
923 	}
924 
925 	return 0;
926 
927 out_destroy_device:
928 	device_destroy(nvmf_class, MKDEV(0, 0));
929 out_destroy_class:
930 	class_destroy(nvmf_class);
931 out_free_host:
932 	nvmf_host_put(nvmf_default_host);
933 	return ret;
934 }
935 
936 static void __exit nvmf_exit(void)
937 {
938 	misc_deregister(&nvmf_misc);
939 	device_destroy(nvmf_class, MKDEV(0, 0));
940 	class_destroy(nvmf_class);
941 	nvmf_host_put(nvmf_default_host);
942 
943 	BUILD_BUG_ON(sizeof(struct nvmf_connect_command) != 64);
944 	BUILD_BUG_ON(sizeof(struct nvmf_property_get_command) != 64);
945 	BUILD_BUG_ON(sizeof(struct nvmf_property_set_command) != 64);
946 	BUILD_BUG_ON(sizeof(struct nvmf_connect_data) != 1024);
947 }
948 
949 MODULE_LICENSE("GPL v2");
950 
951 module_init(nvmf_init);
952 module_exit(nvmf_exit);
953