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