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 
__nvmf_host_find(const char * hostnqn)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 
nvmf_host_add(const char * hostnqn)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 	strscpy(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 
nvmf_host_default(void)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 
nvmf_host_destroy(struct kref * ref)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 
nvmf_host_put(struct nvmf_host * host)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  */
nvmf_get_address(struct nvme_ctrl * ctrl,char * buf,int size)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 	if (ctrl->opts->mask & NVMF_OPT_HOST_IFACE)
116 		len += scnprintf(buf + len, size - len, "%shost_iface=%s",
117 				(len) ? "," : "", ctrl->opts->host_iface);
118 	len += scnprintf(buf + len, size - len, "\n");
119 
120 	return len;
121 }
122 EXPORT_SYMBOL_GPL(nvmf_get_address);
123 
124 /**
125  * nvmf_reg_read32() -  NVMe Fabrics "Property Get" API function.
126  * @ctrl:	Host NVMe controller instance maintaining the admin
127  *		queue used to submit the property read command to
128  *		the allocated NVMe controller resource on the target system.
129  * @off:	Starting offset value of the targeted property
130  *		register (see the fabrics section of the NVMe standard).
131  * @val:	OUTPUT parameter that will contain the value of
132  *		the property after a successful read.
133  *
134  * Used by the host system to retrieve a 32-bit capsule property value
135  * from an NVMe controller on the target system.
136  *
137  * ("Capsule property" is an "PCIe register concept" applied to the
138  * NVMe fabrics space.)
139  *
140  * Return:
141  *	0: successful read
142  *	> 0: NVMe error status code
143  *	< 0: Linux errno error code
144  */
nvmf_reg_read32(struct nvme_ctrl * ctrl,u32 off,u32 * val)145 int nvmf_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val)
146 {
147 	struct nvme_command cmd = { };
148 	union nvme_result res;
149 	int ret;
150 
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->fabrics_q, &cmd, &res, NULL, 0,
156 			NVME_QID_ANY, 0, 0);
157 
158 	if (ret >= 0)
159 		*val = le64_to_cpu(res.u64);
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  */
nvmf_reg_read64(struct nvme_ctrl * ctrl,u32 off,u64 * val)190 int nvmf_reg_read64(struct nvme_ctrl *ctrl, u32 off, u64 *val)
191 {
192 	struct nvme_command cmd = { };
193 	union nvme_result res;
194 	int ret;
195 
196 	cmd.prop_get.opcode = nvme_fabrics_command;
197 	cmd.prop_get.fctype = nvme_fabrics_type_property_get;
198 	cmd.prop_get.attrib = 1;
199 	cmd.prop_get.offset = cpu_to_le32(off);
200 
201 	ret = __nvme_submit_sync_cmd(ctrl->fabrics_q, &cmd, &res, NULL, 0,
202 			NVME_QID_ANY, 0, 0);
203 
204 	if (ret >= 0)
205 		*val = le64_to_cpu(res.u64);
206 	if (unlikely(ret != 0))
207 		dev_err(ctrl->device,
208 			"Property Get error: %d, offset %#x\n",
209 			ret > 0 ? ret & ~NVME_SC_DNR : ret, off);
210 	return ret;
211 }
212 EXPORT_SYMBOL_GPL(nvmf_reg_read64);
213 
214 /**
215  * nvmf_reg_write32() -  NVMe Fabrics "Property Write" API function.
216  * @ctrl:	Host NVMe controller instance maintaining the admin
217  *		queue used to submit the property read command to
218  *		the allocated NVMe controller resource on the target system.
219  * @off:	Starting offset value of the targeted property
220  *		register (see the fabrics section of the NVMe standard).
221  * @val:	Input parameter that contains the value to be
222  *		written to the property.
223  *
224  * Used by the NVMe host system to write a 32-bit capsule property value
225  * to an NVMe controller on the target system.
226  *
227  * ("Capsule property" is an "PCIe register concept" applied to the
228  * NVMe fabrics space.)
229  *
230  * Return:
231  *	0: successful write
232  *	> 0: NVMe error status code
233  *	< 0: Linux errno error code
234  */
nvmf_reg_write32(struct nvme_ctrl * ctrl,u32 off,u32 val)235 int nvmf_reg_write32(struct nvme_ctrl *ctrl, u32 off, u32 val)
236 {
237 	struct nvme_command cmd = { };
238 	int ret;
239 
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,
247 			NVME_QID_ANY, 0, 0);
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 out function for
258  * 				connect() errors.
259  * @ctrl:	The specific /dev/nvmeX device that had the error.
260  * @errval:	Error code to be decoded in a more human-friendly
261  * 		printout.
262  * @offset:	For use with the NVMe error code
263  * 		NVME_SC_CONNECT_INVALID_PARAM.
264  * @cmd:	This is the SQE portion of a submission capsule.
265  * @data:	This is the "Data" portion of a submission capsule.
266  */
nvmf_log_connect_error(struct nvme_ctrl * ctrl,int errval,int offset,struct nvme_command * cmd,struct nvmf_connect_data * data)267 static void nvmf_log_connect_error(struct nvme_ctrl *ctrl,
268 		int errval, int offset, struct nvme_command *cmd,
269 		struct nvmf_connect_data *data)
270 {
271 	int err_sctype = errval & ~NVME_SC_DNR;
272 
273 	if (errval < 0) {
274 		dev_err(ctrl->device,
275 			"Connect command failed, errno: %d\n", errval);
276 		return;
277 	}
278 
279 	switch (err_sctype) {
280 	case NVME_SC_CONNECT_INVALID_PARAM:
281 		if (offset >> 16) {
282 			char *inv_data = "Connect Invalid Data Parameter";
283 
284 			switch (offset & 0xffff) {
285 			case (offsetof(struct nvmf_connect_data, cntlid)):
286 				dev_err(ctrl->device,
287 					"%s, cntlid: %d\n",
288 					inv_data, data->cntlid);
289 				break;
290 			case (offsetof(struct nvmf_connect_data, hostnqn)):
291 				dev_err(ctrl->device,
292 					"%s, hostnqn \"%s\"\n",
293 					inv_data, data->hostnqn);
294 				break;
295 			case (offsetof(struct nvmf_connect_data, subsysnqn)):
296 				dev_err(ctrl->device,
297 					"%s, subsysnqn \"%s\"\n",
298 					inv_data, data->subsysnqn);
299 				break;
300 			default:
301 				dev_err(ctrl->device,
302 					"%s, starting byte offset: %d\n",
303 				       inv_data, offset & 0xffff);
304 				break;
305 			}
306 		} else {
307 			char *inv_sqe = "Connect Invalid SQE Parameter";
308 
309 			switch (offset) {
310 			case (offsetof(struct nvmf_connect_command, qid)):
311 				dev_err(ctrl->device,
312 				       "%s, qid %d\n",
313 					inv_sqe, cmd->connect.qid);
314 				break;
315 			default:
316 				dev_err(ctrl->device,
317 					"%s, starting byte offset: %d\n",
318 					inv_sqe, offset);
319 			}
320 		}
321 		break;
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 	case NVME_SC_CONNECT_CTRL_BUSY:
328 		dev_err(ctrl->device,
329 			"Connect command failed: controller is busy or not available\n");
330 		break;
331 	case NVME_SC_CONNECT_FORMAT:
332 		dev_err(ctrl->device,
333 			"Connect incompatible format: %d",
334 			cmd->connect.recfmt);
335 		break;
336 	case NVME_SC_HOST_PATH_ERROR:
337 		dev_err(ctrl->device,
338 			"Connect command failed: host path error\n");
339 		break;
340 	case NVME_SC_AUTH_REQUIRED:
341 		dev_err(ctrl->device,
342 			"Connect command failed: authentication required\n");
343 		break;
344 	default:
345 		dev_err(ctrl->device,
346 			"Connect command failed, error wo/DNR bit: %d\n",
347 			err_sctype);
348 		break;
349 	}
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  */
nvmf_connect_admin_queue(struct nvme_ctrl * ctrl)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 	u32 result;
379 
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), NVME_QID_ANY, 1,
404 			BLK_MQ_REQ_RESERVED | BLK_MQ_REQ_NOWAIT);
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 	result = le32_to_cpu(res.u32);
412 	ctrl->cntlid = result & 0xFFFF;
413 	if ((result >> 16) & 0x3) {
414 		/* Authentication required */
415 		ret = nvme_auth_negotiate(ctrl, 0);
416 		if (ret) {
417 			dev_warn(ctrl->device,
418 				 "qid 0: authentication setup failed\n");
419 			ret = NVME_SC_AUTH_REQUIRED;
420 			goto out_free_data;
421 		}
422 		ret = nvme_auth_wait(ctrl, 0);
423 		if (ret)
424 			dev_warn(ctrl->device,
425 				 "qid 0: authentication failed\n");
426 		else
427 			dev_info(ctrl->device,
428 				 "qid 0: authenticated\n");
429 	}
430 out_free_data:
431 	kfree(data);
432 	return ret;
433 }
434 EXPORT_SYMBOL_GPL(nvmf_connect_admin_queue);
435 
436 /**
437  * nvmf_connect_io_queue() - NVMe Fabrics I/O Queue "Connect"
438  *			     API function.
439  * @ctrl:	Host nvme controller instance used to establish an
440  *		NVMe I/O queue connection to the already allocated NVMe
441  *		controller on the target system.
442  * @qid:	NVMe I/O queue number for the new I/O connection between
443  *		host and target (note qid == 0 is illegal as this is
444  *		the Admin queue, per NVMe standard).
445  *
446  * This function issues a fabrics-protocol connection
447  * of a NVMe I/O queue (via NVMe Fabrics "Connect" command)
448  * between the host system device and the allocated NVMe controller
449  * on the target system.
450  *
451  * Return:
452  *	0: success
453  *	> 0: NVMe error status code
454  *	< 0: Linux errno error code
455  */
nvmf_connect_io_queue(struct nvme_ctrl * ctrl,u16 qid)456 int nvmf_connect_io_queue(struct nvme_ctrl *ctrl, u16 qid)
457 {
458 	struct nvme_command cmd = { };
459 	struct nvmf_connect_data *data;
460 	union nvme_result res;
461 	int ret;
462 	u32 result;
463 
464 	cmd.connect.opcode = nvme_fabrics_command;
465 	cmd.connect.fctype = nvme_fabrics_type_connect;
466 	cmd.connect.qid = cpu_to_le16(qid);
467 	cmd.connect.sqsize = cpu_to_le16(ctrl->sqsize);
468 
469 	if (ctrl->opts->disable_sqflow)
470 		cmd.connect.cattr |= NVME_CONNECT_DISABLE_SQFLOW;
471 
472 	data = kzalloc(sizeof(*data), GFP_KERNEL);
473 	if (!data)
474 		return -ENOMEM;
475 
476 	uuid_copy(&data->hostid, &ctrl->opts->host->id);
477 	data->cntlid = cpu_to_le16(ctrl->cntlid);
478 	strncpy(data->subsysnqn, ctrl->opts->subsysnqn, NVMF_NQN_SIZE);
479 	strncpy(data->hostnqn, ctrl->opts->host->nqn, NVMF_NQN_SIZE);
480 
481 	ret = __nvme_submit_sync_cmd(ctrl->connect_q, &cmd, &res,
482 			data, sizeof(*data), qid, 1,
483 			BLK_MQ_REQ_RESERVED | BLK_MQ_REQ_NOWAIT);
484 	if (ret) {
485 		nvmf_log_connect_error(ctrl, ret, le32_to_cpu(res.u32),
486 				       &cmd, data);
487 	}
488 	result = le32_to_cpu(res.u32);
489 	if ((result >> 16) & 2) {
490 		/* Authentication required */
491 		ret = nvme_auth_negotiate(ctrl, qid);
492 		if (ret) {
493 			dev_warn(ctrl->device,
494 				 "qid %d: authentication setup failed\n", qid);
495 			ret = NVME_SC_AUTH_REQUIRED;
496 		} else {
497 			ret = nvme_auth_wait(ctrl, qid);
498 			if (ret)
499 				dev_warn(ctrl->device,
500 					 "qid %u: authentication failed\n", qid);
501 		}
502 	}
503 	kfree(data);
504 	return ret;
505 }
506 EXPORT_SYMBOL_GPL(nvmf_connect_io_queue);
507 
nvmf_should_reconnect(struct nvme_ctrl * ctrl)508 bool nvmf_should_reconnect(struct nvme_ctrl *ctrl)
509 {
510 	if (ctrl->opts->max_reconnects == -1 ||
511 	    ctrl->nr_reconnects < ctrl->opts->max_reconnects)
512 		return true;
513 
514 	return false;
515 }
516 EXPORT_SYMBOL_GPL(nvmf_should_reconnect);
517 
518 /**
519  * nvmf_register_transport() - NVMe Fabrics Library registration function.
520  * @ops:	Transport ops instance to be registered to the
521  *		common fabrics library.
522  *
523  * API function that registers the type of specific transport fabric
524  * being implemented to the common NVMe fabrics library. Part of
525  * the overall init sequence of starting up a fabrics driver.
526  */
nvmf_register_transport(struct nvmf_transport_ops * ops)527 int nvmf_register_transport(struct nvmf_transport_ops *ops)
528 {
529 	if (!ops->create_ctrl)
530 		return -EINVAL;
531 
532 	down_write(&nvmf_transports_rwsem);
533 	list_add_tail(&ops->entry, &nvmf_transports);
534 	up_write(&nvmf_transports_rwsem);
535 
536 	return 0;
537 }
538 EXPORT_SYMBOL_GPL(nvmf_register_transport);
539 
540 /**
541  * nvmf_unregister_transport() - NVMe Fabrics Library unregistration function.
542  * @ops:	Transport ops instance to be unregistered from the
543  *		common fabrics library.
544  *
545  * Fabrics API function that unregisters the type of specific transport
546  * fabric being implemented from the common NVMe fabrics library.
547  * Part of the overall exit sequence of unloading the implemented driver.
548  */
nvmf_unregister_transport(struct nvmf_transport_ops * ops)549 void nvmf_unregister_transport(struct nvmf_transport_ops *ops)
550 {
551 	down_write(&nvmf_transports_rwsem);
552 	list_del(&ops->entry);
553 	up_write(&nvmf_transports_rwsem);
554 }
555 EXPORT_SYMBOL_GPL(nvmf_unregister_transport);
556 
nvmf_lookup_transport(struct nvmf_ctrl_options * opts)557 static struct nvmf_transport_ops *nvmf_lookup_transport(
558 		struct nvmf_ctrl_options *opts)
559 {
560 	struct nvmf_transport_ops *ops;
561 
562 	lockdep_assert_held(&nvmf_transports_rwsem);
563 
564 	list_for_each_entry(ops, &nvmf_transports, entry) {
565 		if (strcmp(ops->name, opts->transport) == 0)
566 			return ops;
567 	}
568 
569 	return NULL;
570 }
571 
572 static const match_table_t opt_tokens = {
573 	{ NVMF_OPT_TRANSPORT,		"transport=%s"		},
574 	{ NVMF_OPT_TRADDR,		"traddr=%s"		},
575 	{ NVMF_OPT_TRSVCID,		"trsvcid=%s"		},
576 	{ NVMF_OPT_NQN,			"nqn=%s"		},
577 	{ NVMF_OPT_QUEUE_SIZE,		"queue_size=%d"		},
578 	{ NVMF_OPT_NR_IO_QUEUES,	"nr_io_queues=%d"	},
579 	{ NVMF_OPT_RECONNECT_DELAY,	"reconnect_delay=%d"	},
580 	{ NVMF_OPT_CTRL_LOSS_TMO,	"ctrl_loss_tmo=%d"	},
581 	{ NVMF_OPT_KATO,		"keep_alive_tmo=%d"	},
582 	{ NVMF_OPT_HOSTNQN,		"hostnqn=%s"		},
583 	{ NVMF_OPT_HOST_TRADDR,		"host_traddr=%s"	},
584 	{ NVMF_OPT_HOST_IFACE,		"host_iface=%s"		},
585 	{ NVMF_OPT_HOST_ID,		"hostid=%s"		},
586 	{ NVMF_OPT_DUP_CONNECT,		"duplicate_connect"	},
587 	{ NVMF_OPT_DISABLE_SQFLOW,	"disable_sqflow"	},
588 	{ NVMF_OPT_HDR_DIGEST,		"hdr_digest"		},
589 	{ NVMF_OPT_DATA_DIGEST,		"data_digest"		},
590 	{ NVMF_OPT_NR_WRITE_QUEUES,	"nr_write_queues=%d"	},
591 	{ NVMF_OPT_NR_POLL_QUEUES,	"nr_poll_queues=%d"	},
592 	{ NVMF_OPT_TOS,			"tos=%d"		},
593 	{ NVMF_OPT_FAIL_FAST_TMO,	"fast_io_fail_tmo=%d"	},
594 	{ NVMF_OPT_DISCOVERY,		"discovery"		},
595 	{ NVMF_OPT_DHCHAP_SECRET,	"dhchap_secret=%s"	},
596 	{ NVMF_OPT_DHCHAP_CTRL_SECRET,	"dhchap_ctrl_secret=%s"	},
597 	{ NVMF_OPT_ERR,			NULL			}
598 };
599 
nvmf_parse_options(struct nvmf_ctrl_options * opts,const char * buf)600 static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
601 		const char *buf)
602 {
603 	substring_t args[MAX_OPT_ARGS];
604 	char *options, *o, *p;
605 	int token, ret = 0;
606 	size_t nqnlen  = 0;
607 	int ctrl_loss_tmo = NVMF_DEF_CTRL_LOSS_TMO;
608 	uuid_t hostid;
609 
610 	/* Set defaults */
611 	opts->queue_size = NVMF_DEF_QUEUE_SIZE;
612 	opts->nr_io_queues = num_online_cpus();
613 	opts->reconnect_delay = NVMF_DEF_RECONNECT_DELAY;
614 	opts->kato = 0;
615 	opts->duplicate_connect = false;
616 	opts->fast_io_fail_tmo = NVMF_DEF_FAIL_FAST_TMO;
617 	opts->hdr_digest = false;
618 	opts->data_digest = false;
619 	opts->tos = -1; /* < 0 == use transport default */
620 
621 	options = o = kstrdup(buf, GFP_KERNEL);
622 	if (!options)
623 		return -ENOMEM;
624 
625 	uuid_gen(&hostid);
626 
627 	while ((p = strsep(&o, ",\n")) != NULL) {
628 		if (!*p)
629 			continue;
630 
631 		token = match_token(p, opt_tokens, args);
632 		opts->mask |= token;
633 		switch (token) {
634 		case NVMF_OPT_TRANSPORT:
635 			p = match_strdup(args);
636 			if (!p) {
637 				ret = -ENOMEM;
638 				goto out;
639 			}
640 			kfree(opts->transport);
641 			opts->transport = p;
642 			break;
643 		case NVMF_OPT_NQN:
644 			p = match_strdup(args);
645 			if (!p) {
646 				ret = -ENOMEM;
647 				goto out;
648 			}
649 			kfree(opts->subsysnqn);
650 			opts->subsysnqn = p;
651 			nqnlen = strlen(opts->subsysnqn);
652 			if (nqnlen >= NVMF_NQN_SIZE) {
653 				pr_err("%s needs to be < %d bytes\n",
654 					opts->subsysnqn, NVMF_NQN_SIZE);
655 				ret = -EINVAL;
656 				goto out;
657 			}
658 			opts->discovery_nqn =
659 				!(strcmp(opts->subsysnqn,
660 					 NVME_DISC_SUBSYS_NAME));
661 			break;
662 		case NVMF_OPT_TRADDR:
663 			p = match_strdup(args);
664 			if (!p) {
665 				ret = -ENOMEM;
666 				goto out;
667 			}
668 			kfree(opts->traddr);
669 			opts->traddr = p;
670 			break;
671 		case NVMF_OPT_TRSVCID:
672 			p = match_strdup(args);
673 			if (!p) {
674 				ret = -ENOMEM;
675 				goto out;
676 			}
677 			kfree(opts->trsvcid);
678 			opts->trsvcid = p;
679 			break;
680 		case NVMF_OPT_QUEUE_SIZE:
681 			if (match_int(args, &token)) {
682 				ret = -EINVAL;
683 				goto out;
684 			}
685 			if (token < NVMF_MIN_QUEUE_SIZE ||
686 			    token > NVMF_MAX_QUEUE_SIZE) {
687 				pr_err("Invalid queue_size %d\n", token);
688 				ret = -EINVAL;
689 				goto out;
690 			}
691 			opts->queue_size = token;
692 			break;
693 		case NVMF_OPT_NR_IO_QUEUES:
694 			if (match_int(args, &token)) {
695 				ret = -EINVAL;
696 				goto out;
697 			}
698 			if (token <= 0) {
699 				pr_err("Invalid number of IOQs %d\n", token);
700 				ret = -EINVAL;
701 				goto out;
702 			}
703 			if (opts->discovery_nqn) {
704 				pr_debug("Ignoring nr_io_queues value for discovery controller\n");
705 				break;
706 			}
707 
708 			opts->nr_io_queues = min_t(unsigned int,
709 					num_online_cpus(), token);
710 			break;
711 		case NVMF_OPT_KATO:
712 			if (match_int(args, &token)) {
713 				ret = -EINVAL;
714 				goto out;
715 			}
716 
717 			if (token < 0) {
718 				pr_err("Invalid keep_alive_tmo %d\n", token);
719 				ret = -EINVAL;
720 				goto out;
721 			} else if (token == 0 && !opts->discovery_nqn) {
722 				/* Allowed for debug */
723 				pr_warn("keep_alive_tmo 0 won't execute keep alives!!!\n");
724 			}
725 			opts->kato = token;
726 			break;
727 		case NVMF_OPT_CTRL_LOSS_TMO:
728 			if (match_int(args, &token)) {
729 				ret = -EINVAL;
730 				goto out;
731 			}
732 
733 			if (token < 0)
734 				pr_warn("ctrl_loss_tmo < 0 will reconnect forever\n");
735 			ctrl_loss_tmo = token;
736 			break;
737 		case NVMF_OPT_FAIL_FAST_TMO:
738 			if (match_int(args, &token)) {
739 				ret = -EINVAL;
740 				goto out;
741 			}
742 
743 			if (token >= 0)
744 				pr_warn("I/O fail on reconnect controller after %d sec\n",
745 					token);
746 			else
747 				token = -1;
748 
749 			opts->fast_io_fail_tmo = token;
750 			break;
751 		case NVMF_OPT_HOSTNQN:
752 			if (opts->host) {
753 				pr_err("hostnqn already user-assigned: %s\n",
754 				       opts->host->nqn);
755 				ret = -EADDRINUSE;
756 				goto out;
757 			}
758 			p = match_strdup(args);
759 			if (!p) {
760 				ret = -ENOMEM;
761 				goto out;
762 			}
763 			nqnlen = strlen(p);
764 			if (nqnlen >= NVMF_NQN_SIZE) {
765 				pr_err("%s needs to be < %d bytes\n",
766 					p, NVMF_NQN_SIZE);
767 				kfree(p);
768 				ret = -EINVAL;
769 				goto out;
770 			}
771 			opts->host = nvmf_host_add(p);
772 			kfree(p);
773 			if (!opts->host) {
774 				ret = -ENOMEM;
775 				goto out;
776 			}
777 			break;
778 		case NVMF_OPT_RECONNECT_DELAY:
779 			if (match_int(args, &token)) {
780 				ret = -EINVAL;
781 				goto out;
782 			}
783 			if (token <= 0) {
784 				pr_err("Invalid reconnect_delay %d\n", token);
785 				ret = -EINVAL;
786 				goto out;
787 			}
788 			opts->reconnect_delay = token;
789 			break;
790 		case NVMF_OPT_HOST_TRADDR:
791 			p = match_strdup(args);
792 			if (!p) {
793 				ret = -ENOMEM;
794 				goto out;
795 			}
796 			kfree(opts->host_traddr);
797 			opts->host_traddr = p;
798 			break;
799 		case NVMF_OPT_HOST_IFACE:
800 			p = match_strdup(args);
801 			if (!p) {
802 				ret = -ENOMEM;
803 				goto out;
804 			}
805 			kfree(opts->host_iface);
806 			opts->host_iface = p;
807 			break;
808 		case NVMF_OPT_HOST_ID:
809 			p = match_strdup(args);
810 			if (!p) {
811 				ret = -ENOMEM;
812 				goto out;
813 			}
814 			ret = uuid_parse(p, &hostid);
815 			if (ret) {
816 				pr_err("Invalid hostid %s\n", p);
817 				ret = -EINVAL;
818 				kfree(p);
819 				goto out;
820 			}
821 			kfree(p);
822 			break;
823 		case NVMF_OPT_DUP_CONNECT:
824 			opts->duplicate_connect = true;
825 			break;
826 		case NVMF_OPT_DISABLE_SQFLOW:
827 			opts->disable_sqflow = true;
828 			break;
829 		case NVMF_OPT_HDR_DIGEST:
830 			opts->hdr_digest = true;
831 			break;
832 		case NVMF_OPT_DATA_DIGEST:
833 			opts->data_digest = true;
834 			break;
835 		case NVMF_OPT_NR_WRITE_QUEUES:
836 			if (match_int(args, &token)) {
837 				ret = -EINVAL;
838 				goto out;
839 			}
840 			if (token <= 0) {
841 				pr_err("Invalid nr_write_queues %d\n", token);
842 				ret = -EINVAL;
843 				goto out;
844 			}
845 			opts->nr_write_queues = token;
846 			break;
847 		case NVMF_OPT_NR_POLL_QUEUES:
848 			if (match_int(args, &token)) {
849 				ret = -EINVAL;
850 				goto out;
851 			}
852 			if (token <= 0) {
853 				pr_err("Invalid nr_poll_queues %d\n", token);
854 				ret = -EINVAL;
855 				goto out;
856 			}
857 			opts->nr_poll_queues = token;
858 			break;
859 		case NVMF_OPT_TOS:
860 			if (match_int(args, &token)) {
861 				ret = -EINVAL;
862 				goto out;
863 			}
864 			if (token < 0) {
865 				pr_err("Invalid type of service %d\n", token);
866 				ret = -EINVAL;
867 				goto out;
868 			}
869 			if (token > 255) {
870 				pr_warn("Clamping type of service to 255\n");
871 				token = 255;
872 			}
873 			opts->tos = token;
874 			break;
875 		case NVMF_OPT_DISCOVERY:
876 			opts->discovery_nqn = true;
877 			break;
878 		case NVMF_OPT_DHCHAP_SECRET:
879 			p = match_strdup(args);
880 			if (!p) {
881 				ret = -ENOMEM;
882 				goto out;
883 			}
884 			if (strlen(p) < 11 || strncmp(p, "DHHC-1:", 7)) {
885 				pr_err("Invalid DH-CHAP secret %s\n", p);
886 				ret = -EINVAL;
887 				goto out;
888 			}
889 			kfree(opts->dhchap_secret);
890 			opts->dhchap_secret = p;
891 			break;
892 		case NVMF_OPT_DHCHAP_CTRL_SECRET:
893 			p = match_strdup(args);
894 			if (!p) {
895 				ret = -ENOMEM;
896 				goto out;
897 			}
898 			if (strlen(p) < 11 || strncmp(p, "DHHC-1:", 7)) {
899 				pr_err("Invalid DH-CHAP secret %s\n", p);
900 				ret = -EINVAL;
901 				goto out;
902 			}
903 			kfree(opts->dhchap_ctrl_secret);
904 			opts->dhchap_ctrl_secret = p;
905 			break;
906 		default:
907 			pr_warn("unknown parameter or missing value '%s' in ctrl creation request\n",
908 				p);
909 			ret = -EINVAL;
910 			goto out;
911 		}
912 	}
913 
914 	if (opts->discovery_nqn) {
915 		opts->nr_io_queues = 0;
916 		opts->nr_write_queues = 0;
917 		opts->nr_poll_queues = 0;
918 		opts->duplicate_connect = true;
919 	} else {
920 		if (!opts->kato)
921 			opts->kato = NVME_DEFAULT_KATO;
922 	}
923 	if (ctrl_loss_tmo < 0) {
924 		opts->max_reconnects = -1;
925 	} else {
926 		opts->max_reconnects = DIV_ROUND_UP(ctrl_loss_tmo,
927 						opts->reconnect_delay);
928 		if (ctrl_loss_tmo < opts->fast_io_fail_tmo)
929 			pr_warn("failfast tmo (%d) larger than controller loss tmo (%d)\n",
930 				opts->fast_io_fail_tmo, ctrl_loss_tmo);
931 	}
932 
933 	if (!opts->host) {
934 		kref_get(&nvmf_default_host->ref);
935 		opts->host = nvmf_default_host;
936 	}
937 
938 	uuid_copy(&opts->host->id, &hostid);
939 
940 out:
941 	kfree(options);
942 	return ret;
943 }
944 
nvmf_check_required_opts(struct nvmf_ctrl_options * opts,unsigned int required_opts)945 static int nvmf_check_required_opts(struct nvmf_ctrl_options *opts,
946 		unsigned int required_opts)
947 {
948 	if ((opts->mask & required_opts) != required_opts) {
949 		unsigned int i;
950 
951 		for (i = 0; i < ARRAY_SIZE(opt_tokens); i++) {
952 			if ((opt_tokens[i].token & required_opts) &&
953 			    !(opt_tokens[i].token & opts->mask)) {
954 				pr_warn("missing parameter '%s'\n",
955 					opt_tokens[i].pattern);
956 			}
957 		}
958 
959 		return -EINVAL;
960 	}
961 
962 	return 0;
963 }
964 
nvmf_ip_options_match(struct nvme_ctrl * ctrl,struct nvmf_ctrl_options * opts)965 bool nvmf_ip_options_match(struct nvme_ctrl *ctrl,
966 		struct nvmf_ctrl_options *opts)
967 {
968 	if (!nvmf_ctlr_matches_baseopts(ctrl, opts) ||
969 	    strcmp(opts->traddr, ctrl->opts->traddr) ||
970 	    strcmp(opts->trsvcid, ctrl->opts->trsvcid))
971 		return false;
972 
973 	/*
974 	 * Checking the local address or host interfaces is rough.
975 	 *
976 	 * In most cases, none is specified and the host port or
977 	 * host interface is selected by the stack.
978 	 *
979 	 * Assume no match if:
980 	 * -  local address or host interface is specified and address
981 	 *    or host interface is not the same
982 	 * -  local address or host interface is not specified but
983 	 *    remote is, or vice versa (admin using specific
984 	 *    host_traddr/host_iface when it matters).
985 	 */
986 	if ((opts->mask & NVMF_OPT_HOST_TRADDR) &&
987 	    (ctrl->opts->mask & NVMF_OPT_HOST_TRADDR)) {
988 		if (strcmp(opts->host_traddr, ctrl->opts->host_traddr))
989 			return false;
990 	} else if ((opts->mask & NVMF_OPT_HOST_TRADDR) ||
991 		   (ctrl->opts->mask & NVMF_OPT_HOST_TRADDR)) {
992 		return false;
993 	}
994 
995 	if ((opts->mask & NVMF_OPT_HOST_IFACE) &&
996 	    (ctrl->opts->mask & NVMF_OPT_HOST_IFACE)) {
997 		if (strcmp(opts->host_iface, ctrl->opts->host_iface))
998 			return false;
999 	} else if ((opts->mask & NVMF_OPT_HOST_IFACE) ||
1000 		   (ctrl->opts->mask & NVMF_OPT_HOST_IFACE)) {
1001 		return false;
1002 	}
1003 
1004 	return true;
1005 }
1006 EXPORT_SYMBOL_GPL(nvmf_ip_options_match);
1007 
nvmf_check_allowed_opts(struct nvmf_ctrl_options * opts,unsigned int allowed_opts)1008 static int nvmf_check_allowed_opts(struct nvmf_ctrl_options *opts,
1009 		unsigned int allowed_opts)
1010 {
1011 	if (opts->mask & ~allowed_opts) {
1012 		unsigned int i;
1013 
1014 		for (i = 0; i < ARRAY_SIZE(opt_tokens); i++) {
1015 			if ((opt_tokens[i].token & opts->mask) &&
1016 			    (opt_tokens[i].token & ~allowed_opts)) {
1017 				pr_warn("invalid parameter '%s'\n",
1018 					opt_tokens[i].pattern);
1019 			}
1020 		}
1021 
1022 		return -EINVAL;
1023 	}
1024 
1025 	return 0;
1026 }
1027 
nvmf_free_options(struct nvmf_ctrl_options * opts)1028 void nvmf_free_options(struct nvmf_ctrl_options *opts)
1029 {
1030 	nvmf_host_put(opts->host);
1031 	kfree(opts->transport);
1032 	kfree(opts->traddr);
1033 	kfree(opts->trsvcid);
1034 	kfree(opts->subsysnqn);
1035 	kfree(opts->host_traddr);
1036 	kfree(opts->host_iface);
1037 	kfree(opts->dhchap_secret);
1038 	kfree(opts->dhchap_ctrl_secret);
1039 	kfree(opts);
1040 }
1041 EXPORT_SYMBOL_GPL(nvmf_free_options);
1042 
1043 #define NVMF_REQUIRED_OPTS	(NVMF_OPT_TRANSPORT | NVMF_OPT_NQN)
1044 #define NVMF_ALLOWED_OPTS	(NVMF_OPT_QUEUE_SIZE | NVMF_OPT_NR_IO_QUEUES | \
1045 				 NVMF_OPT_KATO | NVMF_OPT_HOSTNQN | \
1046 				 NVMF_OPT_HOST_ID | NVMF_OPT_DUP_CONNECT |\
1047 				 NVMF_OPT_DISABLE_SQFLOW | NVMF_OPT_DISCOVERY |\
1048 				 NVMF_OPT_FAIL_FAST_TMO | NVMF_OPT_DHCHAP_SECRET |\
1049 				 NVMF_OPT_DHCHAP_CTRL_SECRET)
1050 
1051 static struct nvme_ctrl *
nvmf_create_ctrl(struct device * dev,const char * buf)1052 nvmf_create_ctrl(struct device *dev, const char *buf)
1053 {
1054 	struct nvmf_ctrl_options *opts;
1055 	struct nvmf_transport_ops *ops;
1056 	struct nvme_ctrl *ctrl;
1057 	int ret;
1058 
1059 	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1060 	if (!opts)
1061 		return ERR_PTR(-ENOMEM);
1062 
1063 	ret = nvmf_parse_options(opts, buf);
1064 	if (ret)
1065 		goto out_free_opts;
1066 
1067 
1068 	request_module("nvme-%s", opts->transport);
1069 
1070 	/*
1071 	 * Check the generic options first as we need a valid transport for
1072 	 * the lookup below.  Then clear the generic flags so that transport
1073 	 * drivers don't have to care about them.
1074 	 */
1075 	ret = nvmf_check_required_opts(opts, NVMF_REQUIRED_OPTS);
1076 	if (ret)
1077 		goto out_free_opts;
1078 	opts->mask &= ~NVMF_REQUIRED_OPTS;
1079 
1080 	down_read(&nvmf_transports_rwsem);
1081 	ops = nvmf_lookup_transport(opts);
1082 	if (!ops) {
1083 		pr_info("no handler found for transport %s.\n",
1084 			opts->transport);
1085 		ret = -EINVAL;
1086 		goto out_unlock;
1087 	}
1088 
1089 	if (!try_module_get(ops->module)) {
1090 		ret = -EBUSY;
1091 		goto out_unlock;
1092 	}
1093 	up_read(&nvmf_transports_rwsem);
1094 
1095 	ret = nvmf_check_required_opts(opts, ops->required_opts);
1096 	if (ret)
1097 		goto out_module_put;
1098 	ret = nvmf_check_allowed_opts(opts, NVMF_ALLOWED_OPTS |
1099 				ops->allowed_opts | ops->required_opts);
1100 	if (ret)
1101 		goto out_module_put;
1102 
1103 	ctrl = ops->create_ctrl(dev, opts);
1104 	if (IS_ERR(ctrl)) {
1105 		ret = PTR_ERR(ctrl);
1106 		goto out_module_put;
1107 	}
1108 
1109 	module_put(ops->module);
1110 	return ctrl;
1111 
1112 out_module_put:
1113 	module_put(ops->module);
1114 	goto out_free_opts;
1115 out_unlock:
1116 	up_read(&nvmf_transports_rwsem);
1117 out_free_opts:
1118 	nvmf_free_options(opts);
1119 	return ERR_PTR(ret);
1120 }
1121 
1122 static struct class *nvmf_class;
1123 static struct device *nvmf_device;
1124 static DEFINE_MUTEX(nvmf_dev_mutex);
1125 
nvmf_dev_write(struct file * file,const char __user * ubuf,size_t count,loff_t * pos)1126 static ssize_t nvmf_dev_write(struct file *file, const char __user *ubuf,
1127 		size_t count, loff_t *pos)
1128 {
1129 	struct seq_file *seq_file = file->private_data;
1130 	struct nvme_ctrl *ctrl;
1131 	const char *buf;
1132 	int ret = 0;
1133 
1134 	if (count > PAGE_SIZE)
1135 		return -ENOMEM;
1136 
1137 	buf = memdup_user_nul(ubuf, count);
1138 	if (IS_ERR(buf))
1139 		return PTR_ERR(buf);
1140 
1141 	mutex_lock(&nvmf_dev_mutex);
1142 	if (seq_file->private) {
1143 		ret = -EINVAL;
1144 		goto out_unlock;
1145 	}
1146 
1147 	ctrl = nvmf_create_ctrl(nvmf_device, buf);
1148 	if (IS_ERR(ctrl)) {
1149 		ret = PTR_ERR(ctrl);
1150 		goto out_unlock;
1151 	}
1152 
1153 	seq_file->private = ctrl;
1154 
1155 out_unlock:
1156 	mutex_unlock(&nvmf_dev_mutex);
1157 	kfree(buf);
1158 	return ret ? ret : count;
1159 }
1160 
__nvmf_concat_opt_tokens(struct seq_file * seq_file)1161 static void __nvmf_concat_opt_tokens(struct seq_file *seq_file)
1162 {
1163 	const struct match_token *tok;
1164 	int idx;
1165 
1166 	/*
1167 	 * Add dummy entries for instance and cntlid to
1168 	 * signal an invalid/non-existing controller
1169 	 */
1170 	seq_puts(seq_file, "instance=-1,cntlid=-1");
1171 	for (idx = 0; idx < ARRAY_SIZE(opt_tokens); idx++) {
1172 		tok = &opt_tokens[idx];
1173 		if (tok->token == NVMF_OPT_ERR)
1174 			continue;
1175 		seq_puts(seq_file, ",");
1176 		seq_puts(seq_file, tok->pattern);
1177 	}
1178 	seq_puts(seq_file, "\n");
1179 }
1180 
nvmf_dev_show(struct seq_file * seq_file,void * private)1181 static int nvmf_dev_show(struct seq_file *seq_file, void *private)
1182 {
1183 	struct nvme_ctrl *ctrl;
1184 
1185 	mutex_lock(&nvmf_dev_mutex);
1186 	ctrl = seq_file->private;
1187 	if (!ctrl) {
1188 		__nvmf_concat_opt_tokens(seq_file);
1189 		goto out_unlock;
1190 	}
1191 
1192 	seq_printf(seq_file, "instance=%d,cntlid=%d\n",
1193 			ctrl->instance, ctrl->cntlid);
1194 
1195 out_unlock:
1196 	mutex_unlock(&nvmf_dev_mutex);
1197 	return 0;
1198 }
1199 
nvmf_dev_open(struct inode * inode,struct file * file)1200 static int nvmf_dev_open(struct inode *inode, struct file *file)
1201 {
1202 	/*
1203 	 * The miscdevice code initializes file->private_data, but doesn't
1204 	 * make use of it later.
1205 	 */
1206 	file->private_data = NULL;
1207 	return single_open(file, nvmf_dev_show, NULL);
1208 }
1209 
nvmf_dev_release(struct inode * inode,struct file * file)1210 static int nvmf_dev_release(struct inode *inode, struct file *file)
1211 {
1212 	struct seq_file *seq_file = file->private_data;
1213 	struct nvme_ctrl *ctrl = seq_file->private;
1214 
1215 	if (ctrl)
1216 		nvme_put_ctrl(ctrl);
1217 	return single_release(inode, file);
1218 }
1219 
1220 static const struct file_operations nvmf_dev_fops = {
1221 	.owner		= THIS_MODULE,
1222 	.write		= nvmf_dev_write,
1223 	.read		= seq_read,
1224 	.open		= nvmf_dev_open,
1225 	.release	= nvmf_dev_release,
1226 };
1227 
1228 static struct miscdevice nvmf_misc = {
1229 	.minor		= MISC_DYNAMIC_MINOR,
1230 	.name           = "nvme-fabrics",
1231 	.fops		= &nvmf_dev_fops,
1232 };
1233 
nvmf_init(void)1234 static int __init nvmf_init(void)
1235 {
1236 	int ret;
1237 
1238 	nvmf_default_host = nvmf_host_default();
1239 	if (!nvmf_default_host)
1240 		return -ENOMEM;
1241 
1242 	nvmf_class = class_create(THIS_MODULE, "nvme-fabrics");
1243 	if (IS_ERR(nvmf_class)) {
1244 		pr_err("couldn't register class nvme-fabrics\n");
1245 		ret = PTR_ERR(nvmf_class);
1246 		goto out_free_host;
1247 	}
1248 
1249 	nvmf_device =
1250 		device_create(nvmf_class, NULL, MKDEV(0, 0), NULL, "ctl");
1251 	if (IS_ERR(nvmf_device)) {
1252 		pr_err("couldn't create nvme-fabrics device!\n");
1253 		ret = PTR_ERR(nvmf_device);
1254 		goto out_destroy_class;
1255 	}
1256 
1257 	ret = misc_register(&nvmf_misc);
1258 	if (ret) {
1259 		pr_err("couldn't register misc device: %d\n", ret);
1260 		goto out_destroy_device;
1261 	}
1262 
1263 	return 0;
1264 
1265 out_destroy_device:
1266 	device_destroy(nvmf_class, MKDEV(0, 0));
1267 out_destroy_class:
1268 	class_destroy(nvmf_class);
1269 out_free_host:
1270 	nvmf_host_put(nvmf_default_host);
1271 	return ret;
1272 }
1273 
nvmf_exit(void)1274 static void __exit nvmf_exit(void)
1275 {
1276 	misc_deregister(&nvmf_misc);
1277 	device_destroy(nvmf_class, MKDEV(0, 0));
1278 	class_destroy(nvmf_class);
1279 	nvmf_host_put(nvmf_default_host);
1280 
1281 	BUILD_BUG_ON(sizeof(struct nvmf_common_command) != 64);
1282 	BUILD_BUG_ON(sizeof(struct nvmf_connect_command) != 64);
1283 	BUILD_BUG_ON(sizeof(struct nvmf_property_get_command) != 64);
1284 	BUILD_BUG_ON(sizeof(struct nvmf_property_set_command) != 64);
1285 	BUILD_BUG_ON(sizeof(struct nvmf_auth_send_command) != 64);
1286 	BUILD_BUG_ON(sizeof(struct nvmf_auth_receive_command) != 64);
1287 	BUILD_BUG_ON(sizeof(struct nvmf_connect_data) != 1024);
1288 	BUILD_BUG_ON(sizeof(struct nvmf_auth_dhchap_negotiate_data) != 8);
1289 	BUILD_BUG_ON(sizeof(struct nvmf_auth_dhchap_challenge_data) != 16);
1290 	BUILD_BUG_ON(sizeof(struct nvmf_auth_dhchap_reply_data) != 16);
1291 	BUILD_BUG_ON(sizeof(struct nvmf_auth_dhchap_success1_data) != 16);
1292 	BUILD_BUG_ON(sizeof(struct nvmf_auth_dhchap_success2_data) != 16);
1293 }
1294 
1295 MODULE_LICENSE("GPL v2");
1296 
1297 module_init(nvmf_init);
1298 module_exit(nvmf_exit);
1299