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