blob: 6e6864516ce60f6482c651ecdcc1f949e283cd1a [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);
Christoph Hellwig8e412262017-05-17 09:54:27 +020061 uuid_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);
Christoph Hellwig8e412262017-05-17 09:54:27 +020078 uuid_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;
Guan Junxiong97ddc36e2017-06-13 10:51:24 +0800340
341 case NVME_SC_CONNECT_INVALID_HOST:
342 dev_err(ctrl->device,
343 "Connect for subsystem %s is not allowed, hostnqn: %s\n",
344 data->subsysnqn, data->hostnqn);
345 break;
346
347 case NVME_SC_CONNECT_CTRL_BUSY:
348 dev_err(ctrl->device,
349 "Connect command failed: controller is busy or not available\n");
350 break;
351
352 case NVME_SC_CONNECT_FORMAT:
353 dev_err(ctrl->device,
354 "Connect incompatible format: %d",
355 cmd->connect.recfmt);
356 break;
357
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200358 default:
359 dev_err(ctrl->device,
360 "Connect command failed, error wo/DNR bit: %d\n",
361 err_sctype);
362 break;
363 } /* switch (err_sctype) */
364}
365
366/**
367 * nvmf_connect_admin_queue() - NVMe Fabrics Admin Queue "Connect"
368 * API function.
369 * @ctrl: Host nvme controller instance used to request
370 * a new NVMe controller allocation on the target
371 * system and establish an NVMe Admin connection to
372 * that controller.
373 *
374 * This function enables an NVMe host device to request a new allocation of
375 * an NVMe controller resource on a target system as well establish a
376 * fabrics-protocol connection of the NVMe Admin queue between the
377 * host system device and the allocated NVMe controller on the
378 * target system via a NVMe Fabrics "Connect" command.
379 *
380 * Return:
381 * 0: success
382 * > 0: NVMe error status code
383 * < 0: Linux errno error code
384 *
385 */
386int nvmf_connect_admin_queue(struct nvme_ctrl *ctrl)
387{
388 struct nvme_command cmd;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800389 union nvme_result res;
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200390 struct nvmf_connect_data *data;
391 int ret;
392
393 memset(&cmd, 0, sizeof(cmd));
394 cmd.connect.opcode = nvme_fabrics_command;
395 cmd.connect.fctype = nvme_fabrics_type_connect;
396 cmd.connect.qid = 0;
Jay Freyenseef994d9d2016-08-17 15:00:26 -0700397
398 /*
399 * fabrics spec sets a minimum of depth 32 for admin queue,
400 * so set the queue with this depth always until
401 * justification otherwise.
402 */
403 cmd.connect.sqsize = cpu_to_le16(NVMF_AQ_DEPTH - 1);
404
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200405 /*
406 * Set keep-alive timeout in seconds granularity (ms * 1000)
407 * and add a grace period for controller kato enforcement
408 */
409 cmd.connect.kato = ctrl->opts->discovery_nqn ? 0 :
410 cpu_to_le32((ctrl->kato + NVME_KATO_GRACE) * 1000);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200411
412 data = kzalloc(sizeof(*data), GFP_KERNEL);
413 if (!data)
414 return -ENOMEM;
415
Christoph Hellwig8e412262017-05-17 09:54:27 +0200416 uuid_copy(&data->hostid, &ctrl->opts->host->id);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200417 data->cntlid = cpu_to_le16(0xffff);
418 strncpy(data->subsysnqn, ctrl->opts->subsysnqn, NVMF_NQN_SIZE);
419 strncpy(data->hostnqn, ctrl->opts->host->nqn, NVMF_NQN_SIZE);
420
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800421 ret = __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, &res,
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200422 data, sizeof(*data), 0, NVME_QID_ANY, 1,
423 BLK_MQ_REQ_RESERVED | BLK_MQ_REQ_NOWAIT);
424 if (ret) {
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800425 nvmf_log_connect_error(ctrl, ret, le32_to_cpu(res.u32),
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200426 &cmd, data);
427 goto out_free_data;
428 }
429
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800430 ctrl->cntlid = le16_to_cpu(res.u16);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200431
432out_free_data:
433 kfree(data);
434 return ret;
435}
436EXPORT_SYMBOL_GPL(nvmf_connect_admin_queue);
437
438/**
439 * nvmf_connect_io_queue() - NVMe Fabrics I/O Queue "Connect"
440 * API function.
441 * @ctrl: Host nvme controller instance used to establish an
442 * NVMe I/O queue connection to the already allocated NVMe
443 * controller on the target system.
444 * @qid: NVMe I/O queue number for the new I/O connection between
445 * host and target (note qid == 0 is illegal as this is
446 * the Admin queue, per NVMe standard).
447 *
448 * This function issues a fabrics-protocol connection
449 * of a NVMe I/O queue (via NVMe Fabrics "Connect" command)
450 * between the host system device and the allocated NVMe controller
451 * on the target system.
452 *
453 * Return:
454 * 0: success
455 * > 0: NVMe error status code
456 * < 0: Linux errno error code
457 */
458int nvmf_connect_io_queue(struct nvme_ctrl *ctrl, u16 qid)
459{
460 struct nvme_command cmd;
461 struct nvmf_connect_data *data;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800462 union nvme_result res;
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200463 int ret;
464
465 memset(&cmd, 0, sizeof(cmd));
466 cmd.connect.opcode = nvme_fabrics_command;
467 cmd.connect.fctype = nvme_fabrics_type_connect;
468 cmd.connect.qid = cpu_to_le16(qid);
469 cmd.connect.sqsize = cpu_to_le16(ctrl->sqsize);
470
471 data = kzalloc(sizeof(*data), GFP_KERNEL);
472 if (!data)
473 return -ENOMEM;
474
Christoph Hellwig8e412262017-05-17 09:54:27 +0200475 uuid_copy(&data->hostid, &ctrl->opts->host->id);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200476 data->cntlid = cpu_to_le16(ctrl->cntlid);
477 strncpy(data->subsysnqn, ctrl->opts->subsysnqn, NVMF_NQN_SIZE);
478 strncpy(data->hostnqn, ctrl->opts->host->nqn, NVMF_NQN_SIZE);
479
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800480 ret = __nvme_submit_sync_cmd(ctrl->connect_q, &cmd, &res,
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200481 data, sizeof(*data), 0, qid, 1,
482 BLK_MQ_REQ_RESERVED | BLK_MQ_REQ_NOWAIT);
483 if (ret) {
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800484 nvmf_log_connect_error(ctrl, ret, le32_to_cpu(res.u32),
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200485 &cmd, data);
486 }
487 kfree(data);
488 return ret;
489}
490EXPORT_SYMBOL_GPL(nvmf_connect_io_queue);
491
Sagi Grimberg42a45272017-03-18 20:52:36 +0200492bool nvmf_should_reconnect(struct nvme_ctrl *ctrl)
493{
494 if (ctrl->opts->max_reconnects != -1 &&
Sagi Grimbergfdf9dfa2017-05-04 13:33:15 +0300495 ctrl->nr_reconnects < ctrl->opts->max_reconnects)
Sagi Grimberg42a45272017-03-18 20:52:36 +0200496 return true;
497
498 return false;
499}
500EXPORT_SYMBOL_GPL(nvmf_should_reconnect);
501
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200502/**
503 * nvmf_register_transport() - NVMe Fabrics Library registration function.
504 * @ops: Transport ops instance to be registered to the
505 * common fabrics library.
506 *
507 * API function that registers the type of specific transport fabric
508 * being implemented to the common NVMe fabrics library. Part of
509 * the overall init sequence of starting up a fabrics driver.
510 */
Johannes Thumshirne5a39dd2017-01-27 09:03:45 +0100511int nvmf_register_transport(struct nvmf_transport_ops *ops)
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200512{
Johannes Thumshirne5a39dd2017-01-27 09:03:45 +0100513 if (!ops->create_ctrl)
514 return -EINVAL;
515
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200516 mutex_lock(&nvmf_transports_mutex);
517 list_add_tail(&ops->entry, &nvmf_transports);
518 mutex_unlock(&nvmf_transports_mutex);
Johannes Thumshirne5a39dd2017-01-27 09:03:45 +0100519
520 return 0;
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200521}
522EXPORT_SYMBOL_GPL(nvmf_register_transport);
523
524/**
525 * nvmf_unregister_transport() - NVMe Fabrics Library unregistration function.
526 * @ops: Transport ops instance to be unregistered from the
527 * common fabrics library.
528 *
529 * Fabrics API function that unregisters the type of specific transport
530 * fabric being implemented from the common NVMe fabrics library.
531 * Part of the overall exit sequence of unloading the implemented driver.
532 */
533void nvmf_unregister_transport(struct nvmf_transport_ops *ops)
534{
535 mutex_lock(&nvmf_transports_mutex);
536 list_del(&ops->entry);
537 mutex_unlock(&nvmf_transports_mutex);
538}
539EXPORT_SYMBOL_GPL(nvmf_unregister_transport);
540
541static struct nvmf_transport_ops *nvmf_lookup_transport(
542 struct nvmf_ctrl_options *opts)
543{
544 struct nvmf_transport_ops *ops;
545
546 lockdep_assert_held(&nvmf_transports_mutex);
547
548 list_for_each_entry(ops, &nvmf_transports, entry) {
549 if (strcmp(ops->name, opts->transport) == 0)
550 return ops;
551 }
552
553 return NULL;
554}
555
556static const match_table_t opt_tokens = {
557 { NVMF_OPT_TRANSPORT, "transport=%s" },
558 { NVMF_OPT_TRADDR, "traddr=%s" },
559 { NVMF_OPT_TRSVCID, "trsvcid=%s" },
560 { NVMF_OPT_NQN, "nqn=%s" },
561 { NVMF_OPT_QUEUE_SIZE, "queue_size=%d" },
562 { NVMF_OPT_NR_IO_QUEUES, "nr_io_queues=%d" },
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200563 { NVMF_OPT_RECONNECT_DELAY, "reconnect_delay=%d" },
Sagi Grimberg42a45272017-03-18 20:52:36 +0200564 { NVMF_OPT_CTRL_LOSS_TMO, "ctrl_loss_tmo=%d" },
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200565 { NVMF_OPT_KATO, "keep_alive_tmo=%d" },
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200566 { NVMF_OPT_HOSTNQN, "hostnqn=%s" },
James Smart478bcb92016-08-02 10:42:10 +0300567 { NVMF_OPT_HOST_TRADDR, "host_traddr=%s" },
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200568 { NVMF_OPT_ERR, NULL }
569};
570
571static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
572 const char *buf)
573{
574 substring_t args[MAX_OPT_ARGS];
575 char *options, *o, *p;
576 int token, ret = 0;
577 size_t nqnlen = 0;
Sagi Grimberg42a45272017-03-18 20:52:36 +0200578 int ctrl_loss_tmo = NVMF_DEF_CTRL_LOSS_TMO;
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200579
580 /* Set defaults */
581 opts->queue_size = NVMF_DEF_QUEUE_SIZE;
582 opts->nr_io_queues = num_online_cpus();
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200583 opts->reconnect_delay = NVMF_DEF_RECONNECT_DELAY;
584
585 options = o = kstrdup(buf, GFP_KERNEL);
586 if (!options)
587 return -ENOMEM;
588
589 while ((p = strsep(&o, ",\n")) != NULL) {
590 if (!*p)
591 continue;
592
593 token = match_token(p, opt_tokens, args);
594 opts->mask |= token;
595 switch (token) {
596 case NVMF_OPT_TRANSPORT:
597 p = match_strdup(args);
598 if (!p) {
599 ret = -ENOMEM;
600 goto out;
601 }
602 opts->transport = p;
603 break;
604 case NVMF_OPT_NQN:
605 p = match_strdup(args);
606 if (!p) {
607 ret = -ENOMEM;
608 goto out;
609 }
610 opts->subsysnqn = p;
611 nqnlen = strlen(opts->subsysnqn);
612 if (nqnlen >= NVMF_NQN_SIZE) {
613 pr_err("%s needs to be < %d bytes\n",
Bart Van Assche6eb72832016-10-18 13:10:24 -0700614 opts->subsysnqn, NVMF_NQN_SIZE);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200615 ret = -EINVAL;
616 goto out;
617 }
618 opts->discovery_nqn =
619 !(strcmp(opts->subsysnqn,
620 NVME_DISC_SUBSYS_NAME));
621 if (opts->discovery_nqn)
622 opts->nr_io_queues = 0;
623 break;
624 case NVMF_OPT_TRADDR:
625 p = match_strdup(args);
626 if (!p) {
627 ret = -ENOMEM;
628 goto out;
629 }
630 opts->traddr = p;
631 break;
632 case NVMF_OPT_TRSVCID:
633 p = match_strdup(args);
634 if (!p) {
635 ret = -ENOMEM;
636 goto out;
637 }
638 opts->trsvcid = p;
639 break;
640 case NVMF_OPT_QUEUE_SIZE:
641 if (match_int(args, &token)) {
642 ret = -EINVAL;
643 goto out;
644 }
645 if (token < NVMF_MIN_QUEUE_SIZE ||
646 token > NVMF_MAX_QUEUE_SIZE) {
647 pr_err("Invalid queue_size %d\n", token);
648 ret = -EINVAL;
649 goto out;
650 }
651 opts->queue_size = token;
652 break;
653 case NVMF_OPT_NR_IO_QUEUES:
654 if (match_int(args, &token)) {
655 ret = -EINVAL;
656 goto out;
657 }
658 if (token <= 0) {
659 pr_err("Invalid number of IOQs %d\n", token);
660 ret = -EINVAL;
661 goto out;
662 }
663 opts->nr_io_queues = min_t(unsigned int,
664 num_online_cpus(), token);
665 break;
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200666 case NVMF_OPT_KATO:
667 if (match_int(args, &token)) {
668 ret = -EINVAL;
669 goto out;
670 }
671
672 if (opts->discovery_nqn) {
673 pr_err("Discovery controllers cannot accept keep_alive_tmo != 0\n");
674 ret = -EINVAL;
675 goto out;
676 }
677
678 if (token < 0) {
679 pr_err("Invalid keep_alive_tmo %d\n", token);
680 ret = -EINVAL;
681 goto out;
682 } else if (token == 0) {
683 /* Allowed for debug */
684 pr_warn("keep_alive_tmo 0 won't execute keep alives!!!\n");
685 }
686 opts->kato = token;
687 break;
Sagi Grimberg42a45272017-03-18 20:52:36 +0200688 case NVMF_OPT_CTRL_LOSS_TMO:
689 if (match_int(args, &token)) {
690 ret = -EINVAL;
691 goto out;
692 }
693
694 if (token < 0)
695 pr_warn("ctrl_loss_tmo < 0 will reconnect forever\n");
696 ctrl_loss_tmo = token;
697 break;
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200698 case NVMF_OPT_HOSTNQN:
699 if (opts->host) {
700 pr_err("hostnqn already user-assigned: %s\n",
701 opts->host->nqn);
702 ret = -EADDRINUSE;
703 goto out;
704 }
705 p = match_strdup(args);
706 if (!p) {
707 ret = -ENOMEM;
708 goto out;
709 }
710 nqnlen = strlen(p);
711 if (nqnlen >= NVMF_NQN_SIZE) {
712 pr_err("%s needs to be < %d bytes\n",
713 p, NVMF_NQN_SIZE);
Bart Van Assche8eadfcb2016-10-18 13:10:46 -0700714 kfree(p);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200715 ret = -EINVAL;
716 goto out;
717 }
718 opts->host = nvmf_host_add(p);
Bart Van Assche8eadfcb2016-10-18 13:10:46 -0700719 kfree(p);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200720 if (!opts->host) {
721 ret = -ENOMEM;
722 goto out;
723 }
724 break;
725 case NVMF_OPT_RECONNECT_DELAY:
726 if (match_int(args, &token)) {
727 ret = -EINVAL;
728 goto out;
729 }
730 if (token <= 0) {
731 pr_err("Invalid reconnect_delay %d\n", token);
732 ret = -EINVAL;
733 goto out;
734 }
735 opts->reconnect_delay = token;
736 break;
James Smart478bcb92016-08-02 10:42:10 +0300737 case NVMF_OPT_HOST_TRADDR:
738 p = match_strdup(args);
739 if (!p) {
740 ret = -ENOMEM;
741 goto out;
742 }
743 opts->host_traddr = p;
744 break;
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200745 default:
746 pr_warn("unknown parameter or missing value '%s' in ctrl creation request\n",
747 p);
748 ret = -EINVAL;
749 goto out;
750 }
751 }
752
Sagi Grimberg42a45272017-03-18 20:52:36 +0200753 if (ctrl_loss_tmo < 0)
754 opts->max_reconnects = -1;
755 else
756 opts->max_reconnects = DIV_ROUND_UP(ctrl_loss_tmo,
757 opts->reconnect_delay);
758
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200759 if (!opts->host) {
760 kref_get(&nvmf_default_host->ref);
761 opts->host = nvmf_default_host;
762 }
763
764out:
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200765 if (!opts->discovery_nqn && !opts->kato)
766 opts->kato = NVME_DEFAULT_KATO;
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200767 kfree(options);
768 return ret;
769}
770
771static int nvmf_check_required_opts(struct nvmf_ctrl_options *opts,
772 unsigned int required_opts)
773{
774 if ((opts->mask & required_opts) != required_opts) {
775 int i;
776
777 for (i = 0; i < ARRAY_SIZE(opt_tokens); i++) {
778 if ((opt_tokens[i].token & required_opts) &&
779 !(opt_tokens[i].token & opts->mask)) {
780 pr_warn("missing parameter '%s'\n",
781 opt_tokens[i].pattern);
782 }
783 }
784
785 return -EINVAL;
786 }
787
788 return 0;
789}
790
791static int nvmf_check_allowed_opts(struct nvmf_ctrl_options *opts,
792 unsigned int allowed_opts)
793{
794 if (opts->mask & ~allowed_opts) {
795 int i;
796
797 for (i = 0; i < ARRAY_SIZE(opt_tokens); i++) {
798 if (opt_tokens[i].token & ~allowed_opts) {
799 pr_warn("invalid parameter '%s'\n",
800 opt_tokens[i].pattern);
801 }
802 }
803
804 return -EINVAL;
805 }
806
807 return 0;
808}
809
810void nvmf_free_options(struct nvmf_ctrl_options *opts)
811{
812 nvmf_host_put(opts->host);
813 kfree(opts->transport);
814 kfree(opts->traddr);
815 kfree(opts->trsvcid);
816 kfree(opts->subsysnqn);
James Smart478bcb92016-08-02 10:42:10 +0300817 kfree(opts->host_traddr);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200818 kfree(opts);
819}
820EXPORT_SYMBOL_GPL(nvmf_free_options);
821
822#define NVMF_REQUIRED_OPTS (NVMF_OPT_TRANSPORT | NVMF_OPT_NQN)
823#define NVMF_ALLOWED_OPTS (NVMF_OPT_QUEUE_SIZE | NVMF_OPT_NR_IO_QUEUES | \
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200824 NVMF_OPT_KATO | NVMF_OPT_HOSTNQN)
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200825
826static struct nvme_ctrl *
827nvmf_create_ctrl(struct device *dev, const char *buf, size_t count)
828{
829 struct nvmf_ctrl_options *opts;
830 struct nvmf_transport_ops *ops;
831 struct nvme_ctrl *ctrl;
832 int ret;
833
834 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
835 if (!opts)
836 return ERR_PTR(-ENOMEM);
837
838 ret = nvmf_parse_options(opts, buf);
839 if (ret)
840 goto out_free_opts;
841
842 /*
843 * Check the generic options first as we need a valid transport for
844 * the lookup below. Then clear the generic flags so that transport
845 * drivers don't have to care about them.
846 */
847 ret = nvmf_check_required_opts(opts, NVMF_REQUIRED_OPTS);
848 if (ret)
849 goto out_free_opts;
850 opts->mask &= ~NVMF_REQUIRED_OPTS;
851
852 mutex_lock(&nvmf_transports_mutex);
853 ops = nvmf_lookup_transport(opts);
854 if (!ops) {
855 pr_info("no handler found for transport %s.\n",
856 opts->transport);
857 ret = -EINVAL;
858 goto out_unlock;
859 }
860
861 ret = nvmf_check_required_opts(opts, ops->required_opts);
862 if (ret)
863 goto out_unlock;
864 ret = nvmf_check_allowed_opts(opts, NVMF_ALLOWED_OPTS |
865 ops->allowed_opts | ops->required_opts);
866 if (ret)
867 goto out_unlock;
868
869 ctrl = ops->create_ctrl(dev, opts);
870 if (IS_ERR(ctrl)) {
871 ret = PTR_ERR(ctrl);
872 goto out_unlock;
873 }
874
875 mutex_unlock(&nvmf_transports_mutex);
876 return ctrl;
877
878out_unlock:
879 mutex_unlock(&nvmf_transports_mutex);
880out_free_opts:
Bart Van Asschef3116d82016-10-18 13:11:03 -0700881 nvmf_free_options(opts);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200882 return ERR_PTR(ret);
883}
884
885static struct class *nvmf_class;
886static struct device *nvmf_device;
887static DEFINE_MUTEX(nvmf_dev_mutex);
888
889static ssize_t nvmf_dev_write(struct file *file, const char __user *ubuf,
890 size_t count, loff_t *pos)
891{
892 struct seq_file *seq_file = file->private_data;
893 struct nvme_ctrl *ctrl;
894 const char *buf;
895 int ret = 0;
896
897 if (count > PAGE_SIZE)
898 return -ENOMEM;
899
900 buf = memdup_user_nul(ubuf, count);
901 if (IS_ERR(buf))
902 return PTR_ERR(buf);
903
904 mutex_lock(&nvmf_dev_mutex);
905 if (seq_file->private) {
906 ret = -EINVAL;
907 goto out_unlock;
908 }
909
910 ctrl = nvmf_create_ctrl(nvmf_device, buf, count);
911 if (IS_ERR(ctrl)) {
912 ret = PTR_ERR(ctrl);
913 goto out_unlock;
914 }
915
916 seq_file->private = ctrl;
917
918out_unlock:
919 mutex_unlock(&nvmf_dev_mutex);
920 kfree(buf);
921 return ret ? ret : count;
922}
923
924static int nvmf_dev_show(struct seq_file *seq_file, void *private)
925{
926 struct nvme_ctrl *ctrl;
927 int ret = 0;
928
929 mutex_lock(&nvmf_dev_mutex);
930 ctrl = seq_file->private;
931 if (!ctrl) {
932 ret = -EINVAL;
933 goto out_unlock;
934 }
935
936 seq_printf(seq_file, "instance=%d,cntlid=%d\n",
937 ctrl->instance, ctrl->cntlid);
938
939out_unlock:
940 mutex_unlock(&nvmf_dev_mutex);
941 return ret;
942}
943
944static int nvmf_dev_open(struct inode *inode, struct file *file)
945{
946 /*
947 * The miscdevice code initializes file->private_data, but doesn't
948 * make use of it later.
949 */
950 file->private_data = NULL;
951 return single_open(file, nvmf_dev_show, NULL);
952}
953
954static int nvmf_dev_release(struct inode *inode, struct file *file)
955{
956 struct seq_file *seq_file = file->private_data;
957 struct nvme_ctrl *ctrl = seq_file->private;
958
959 if (ctrl)
960 nvme_put_ctrl(ctrl);
961 return single_release(inode, file);
962}
963
964static const struct file_operations nvmf_dev_fops = {
965 .owner = THIS_MODULE,
966 .write = nvmf_dev_write,
967 .read = seq_read,
968 .open = nvmf_dev_open,
969 .release = nvmf_dev_release,
970};
971
972static struct miscdevice nvmf_misc = {
973 .minor = MISC_DYNAMIC_MINOR,
974 .name = "nvme-fabrics",
975 .fops = &nvmf_dev_fops,
976};
977
978static int __init nvmf_init(void)
979{
980 int ret;
981
982 nvmf_default_host = nvmf_host_default();
983 if (!nvmf_default_host)
984 return -ENOMEM;
985
986 nvmf_class = class_create(THIS_MODULE, "nvme-fabrics");
987 if (IS_ERR(nvmf_class)) {
988 pr_err("couldn't register class nvme-fabrics\n");
989 ret = PTR_ERR(nvmf_class);
990 goto out_free_host;
991 }
992
993 nvmf_device =
994 device_create(nvmf_class, NULL, MKDEV(0, 0), NULL, "ctl");
995 if (IS_ERR(nvmf_device)) {
996 pr_err("couldn't create nvme-fabris device!\n");
997 ret = PTR_ERR(nvmf_device);
998 goto out_destroy_class;
999 }
1000
1001 ret = misc_register(&nvmf_misc);
1002 if (ret) {
1003 pr_err("couldn't register misc device: %d\n", ret);
1004 goto out_destroy_device;
1005 }
1006
1007 return 0;
1008
1009out_destroy_device:
1010 device_destroy(nvmf_class, MKDEV(0, 0));
1011out_destroy_class:
1012 class_destroy(nvmf_class);
1013out_free_host:
1014 nvmf_host_put(nvmf_default_host);
1015 return ret;
1016}
1017
1018static void __exit nvmf_exit(void)
1019{
1020 misc_deregister(&nvmf_misc);
1021 device_destroy(nvmf_class, MKDEV(0, 0));
1022 class_destroy(nvmf_class);
1023 nvmf_host_put(nvmf_default_host);
1024
1025 BUILD_BUG_ON(sizeof(struct nvmf_connect_command) != 64);
1026 BUILD_BUG_ON(sizeof(struct nvmf_property_get_command) != 64);
1027 BUILD_BUG_ON(sizeof(struct nvmf_property_set_command) != 64);
1028 BUILD_BUG_ON(sizeof(struct nvmf_connect_data) != 1024);
1029}
1030
1031MODULE_LICENSE("GPL v2");
1032
1033module_init(nvmf_init);
1034module_exit(nvmf_exit);