blob: d4cb826f58ff4138c5b3c68edc094eb3ca1b4179 [file] [log] [blame]
Christoph Hellwig9002c4e2019-02-18 09:31:03 +01001// SPDX-License-Identifier: GPL-2.0
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002/*
3 * NVMe over Fabrics common host code.
4 * Copyright (c) 2015-2016 HGST, a Western Digital Company.
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02005 */
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
16static LIST_HEAD(nvmf_transports);
Roland Dreier489beb92017-08-29 10:33:44 -070017static DECLARE_RWSEM(nvmf_transports_rwsem);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +020018
19static LIST_HEAD(nvmf_hosts);
20static DEFINE_MUTEX(nvmf_hosts_mutex);
21
22static struct nvmf_host *nvmf_default_host;
23
24static 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
36static 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);
Christoph Hellwig98096d82016-08-18 11:16:35 -070042 if (host) {
43 kref_get(&host->ref);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +020044 goto out_unlock;
Christoph Hellwig98096d82016-08-18 11:16:35 -070045 }
Christoph Hellwig07bfcd02016-06-13 16:45:26 +020046
47 host = kmalloc(sizeof(*host), GFP_KERNEL);
48 if (!host)
49 goto out_unlock;
50
51 kref_init(&host->ref);
Hannes Reinecke1e5f4462018-05-25 11:04:03 +020052 strlcpy(host->nqn, hostnqn, NVMF_NQN_SIZE);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +020053
54 list_add_tail(&host->list, &nvmf_hosts);
55out_unlock:
56 mutex_unlock(&nvmf_hosts_mutex);
57 return host;
58}
59
60static 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);
Ewan D. Milne6b018232018-01-05 12:44:06 -050069 uuid_gen(&host->id);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +020070 snprintf(host->nqn, NVMF_NQN_SIZE,
Daniel Verkamp40a5fce2017-08-30 15:18:19 -070071 "nqn.2014-08.org.nvmexpress:uuid:%pUb", &host->id);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +020072
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
80static void nvmf_host_destroy(struct kref *ref)
81{
82 struct nvmf_host *host = container_of(ref, struct nvmf_host, ref);
83
Ming Line76debd2016-07-01 12:13:32 -070084 mutex_lock(&nvmf_hosts_mutex);
85 list_del(&host->list);
86 mutex_unlock(&nvmf_hosts_mutex);
87
Christoph Hellwig07bfcd02016-06-13 16:45:26 +020088 kfree(host);
89}
90
91static 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 */
103int nvmf_get_address(struct nvme_ctrl *ctrl, char *buf, int size)
104{
James Smart0fe51ff2016-08-02 10:40:01 +0300105 int len = 0;
106
107 if (ctrl->opts->mask & NVMF_OPT_TRADDR)
108 len += snprintf(buf, size, "traddr=%s", ctrl->opts->traddr);
109 if (ctrl->opts->mask & NVMF_OPT_TRSVCID)
110 len += snprintf(buf + len, size - len, "%strsvcid=%s",
111 (len) ? "," : "", ctrl->opts->trsvcid);
James Smart478bcb92016-08-02 10:42:10 +0300112 if (ctrl->opts->mask & NVMF_OPT_HOST_TRADDR)
113 len += snprintf(buf + len, size - len, "%shost_traddr=%s",
114 (len) ? "," : "", ctrl->opts->host_traddr);
James Smart0fe51ff2016-08-02 10:40:01 +0300115 len += snprintf(buf + len, size - len, "\n");
116
117 return len;
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200118}
119EXPORT_SYMBOL_GPL(nvmf_get_address);
120
121/**
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200122 * nvmf_reg_read32() - NVMe Fabrics "Property Get" API function.
123 * @ctrl: Host NVMe controller instance maintaining the admin
124 * queue used to submit the property read command to
125 * the allocated NVMe controller resource on the target system.
126 * @off: Starting offset value of the targeted property
127 * register (see the fabrics section of the NVMe standard).
128 * @val: OUTPUT parameter that will contain the value of
129 * the property after a successful read.
130 *
131 * Used by the host system to retrieve a 32-bit capsule property value
132 * from an NVMe controller on the target system.
133 *
134 * ("Capsule property" is an "PCIe register concept" applied to the
135 * NVMe fabrics space.)
136 *
137 * Return:
138 * 0: successful read
139 * > 0: NVMe error status code
140 * < 0: Linux errno error code
141 */
142int nvmf_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val)
143{
144 struct nvme_command cmd;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800145 union nvme_result res;
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200146 int ret;
147
148 memset(&cmd, 0, sizeof(cmd));
149 cmd.prop_get.opcode = nvme_fabrics_command;
150 cmd.prop_get.fctype = nvme_fabrics_type_property_get;
151 cmd.prop_get.offset = cpu_to_le32(off);
152
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800153 ret = __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, &res, NULL, 0, 0,
Sagi Grimberg6287b512018-12-14 11:06:07 -0800154 NVME_QID_ANY, 0, 0, false);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200155
156 if (ret >= 0)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800157 *val = le64_to_cpu(res.u64);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200158 if (unlikely(ret != 0))
159 dev_err(ctrl->device,
160 "Property Get error: %d, offset %#x\n",
161 ret > 0 ? ret & ~NVME_SC_DNR : ret, off);
162
163 return ret;
164}
165EXPORT_SYMBOL_GPL(nvmf_reg_read32);
166
167/**
168 * nvmf_reg_read64() - NVMe Fabrics "Property Get" API function.
169 * @ctrl: Host NVMe controller instance maintaining the admin
170 * queue used to submit the property read command to
171 * the allocated controller resource on the target system.
172 * @off: Starting offset value of the targeted property
173 * register (see the fabrics section of the NVMe standard).
174 * @val: OUTPUT parameter that will contain the value of
175 * the property after a successful read.
176 *
177 * Used by the host system to retrieve a 64-bit capsule property value
178 * from an NVMe controller on the target system.
179 *
180 * ("Capsule property" is an "PCIe register concept" applied to the
181 * NVMe fabrics space.)
182 *
183 * Return:
184 * 0: successful read
185 * > 0: NVMe error status code
186 * < 0: Linux errno error code
187 */
188int nvmf_reg_read64(struct nvme_ctrl *ctrl, u32 off, u64 *val)
189{
190 struct nvme_command cmd;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800191 union nvme_result res;
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200192 int ret;
193
194 memset(&cmd, 0, sizeof(cmd));
195 cmd.prop_get.opcode = nvme_fabrics_command;
196 cmd.prop_get.fctype = nvme_fabrics_type_property_get;
197 cmd.prop_get.attrib = 1;
198 cmd.prop_get.offset = cpu_to_le32(off);
199
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800200 ret = __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, &res, NULL, 0, 0,
Sagi Grimberg6287b512018-12-14 11:06:07 -0800201 NVME_QID_ANY, 0, 0, false);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200202
203 if (ret >= 0)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800204 *val = le64_to_cpu(res.u64);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200205 if (unlikely(ret != 0))
206 dev_err(ctrl->device,
207 "Property Get error: %d, offset %#x\n",
208 ret > 0 ? ret & ~NVME_SC_DNR : ret, off);
209 return ret;
210}
211EXPORT_SYMBOL_GPL(nvmf_reg_read64);
212
213/**
214 * nvmf_reg_write32() - NVMe Fabrics "Property Write" API function.
215 * @ctrl: Host NVMe controller instance maintaining the admin
216 * queue used to submit the property read command to
217 * the allocated NVMe controller resource on the target system.
218 * @off: Starting offset value of the targeted property
219 * register (see the fabrics section of the NVMe standard).
220 * @val: Input parameter that contains the value to be
221 * written to the property.
222 *
223 * Used by the NVMe host system to write a 32-bit capsule property value
224 * to an NVMe controller on the target system.
225 *
226 * ("Capsule property" is an "PCIe register concept" applied to the
227 * NVMe fabrics space.)
228 *
229 * Return:
230 * 0: successful write
231 * > 0: NVMe error status code
232 * < 0: Linux errno error code
233 */
234int nvmf_reg_write32(struct nvme_ctrl *ctrl, u32 off, u32 val)
235{
236 struct nvme_command cmd;
237 int ret;
238
239 memset(&cmd, 0, sizeof(cmd));
240 cmd.prop_set.opcode = nvme_fabrics_command;
241 cmd.prop_set.fctype = nvme_fabrics_type_property_set;
242 cmd.prop_set.attrib = 0;
243 cmd.prop_set.offset = cpu_to_le32(off);
244 cmd.prop_set.value = cpu_to_le64(val);
245
246 ret = __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, NULL, NULL, 0, 0,
Sagi Grimberg6287b512018-12-14 11:06:07 -0800247 NVME_QID_ANY, 0, 0, false);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200248 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}
254EXPORT_SYMBOL_GPL(nvmf_reg_write32);
255
256/**
257 * nvmf_log_connect_error() - Error-parsing-diagnostic print
258 * out function for connect() errors.
259 *
260 * @ctrl: the specific /dev/nvmeX device that had the error.
261 *
262 * @errval: Error code to be decoded in a more human-friendly
263 * printout.
264 *
265 * @offset: For use with the NVMe error code NVME_SC_CONNECT_INVALID_PARAM.
266 *
267 * @cmd: This is the SQE portion of a submission capsule.
268 *
269 * @data: This is the "Data" portion of a submission capsule.
270 */
271static void nvmf_log_connect_error(struct nvme_ctrl *ctrl,
272 int errval, int offset, struct nvme_command *cmd,
273 struct nvmf_connect_data *data)
274{
275 int err_sctype = errval & (~NVME_SC_DNR);
276
277 switch (err_sctype) {
278
279 case (NVME_SC_CONNECT_INVALID_PARAM):
280 if (offset >> 16) {
281 char *inv_data = "Connect Invalid Data Parameter";
282
283 switch (offset & 0xffff) {
284 case (offsetof(struct nvmf_connect_data, cntlid)):
285 dev_err(ctrl->device,
286 "%s, cntlid: %d\n",
287 inv_data, data->cntlid);
288 break;
289 case (offsetof(struct nvmf_connect_data, hostnqn)):
290 dev_err(ctrl->device,
291 "%s, hostnqn \"%s\"\n",
292 inv_data, data->hostnqn);
293 break;
294 case (offsetof(struct nvmf_connect_data, subsysnqn)):
295 dev_err(ctrl->device,
296 "%s, subsysnqn \"%s\"\n",
297 inv_data, data->subsysnqn);
298 break;
299 default:
300 dev_err(ctrl->device,
301 "%s, starting byte offset: %d\n",
302 inv_data, offset & 0xffff);
303 break;
304 }
305 } else {
306 char *inv_sqe = "Connect Invalid SQE Parameter";
307
308 switch (offset) {
309 case (offsetof(struct nvmf_connect_command, qid)):
310 dev_err(ctrl->device,
311 "%s, qid %d\n",
312 inv_sqe, cmd->connect.qid);
313 break;
314 default:
315 dev_err(ctrl->device,
316 "%s, starting byte offset: %d\n",
317 inv_sqe, offset);
318 }
319 }
320 break;
Guan Junxiong97ddc36e2017-06-13 10:51:24 +0800321
322 case NVME_SC_CONNECT_INVALID_HOST:
323 dev_err(ctrl->device,
324 "Connect for subsystem %s is not allowed, hostnqn: %s\n",
325 data->subsysnqn, data->hostnqn);
326 break;
327
328 case NVME_SC_CONNECT_CTRL_BUSY:
329 dev_err(ctrl->device,
330 "Connect command failed: controller is busy or not available\n");
331 break;
332
333 case NVME_SC_CONNECT_FORMAT:
334 dev_err(ctrl->device,
335 "Connect incompatible format: %d",
336 cmd->connect.recfmt);
337 break;
338
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200339 default:
340 dev_err(ctrl->device,
341 "Connect command failed, error wo/DNR bit: %d\n",
342 err_sctype);
343 break;
344 } /* switch (err_sctype) */
345}
346
347/**
348 * nvmf_connect_admin_queue() - NVMe Fabrics Admin Queue "Connect"
349 * API function.
350 * @ctrl: Host nvme controller instance used to request
351 * a new NVMe controller allocation on the target
352 * system and establish an NVMe Admin connection to
353 * that controller.
354 *
355 * This function enables an NVMe host device to request a new allocation of
356 * an NVMe controller resource on a target system as well establish a
357 * fabrics-protocol connection of the NVMe Admin queue between the
358 * host system device and the allocated NVMe controller on the
359 * target system via a NVMe Fabrics "Connect" command.
360 *
361 * Return:
362 * 0: success
363 * > 0: NVMe error status code
364 * < 0: Linux errno error code
365 *
366 */
367int nvmf_connect_admin_queue(struct nvme_ctrl *ctrl)
368{
369 struct nvme_command cmd;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800370 union nvme_result res;
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200371 struct nvmf_connect_data *data;
372 int ret;
373
374 memset(&cmd, 0, sizeof(cmd));
375 cmd.connect.opcode = nvme_fabrics_command;
376 cmd.connect.fctype = nvme_fabrics_type_connect;
377 cmd.connect.qid = 0;
Sagi Grimberg7aa1f422017-06-18 16:15:59 +0300378 cmd.connect.sqsize = cpu_to_le16(NVME_AQ_DEPTH - 1);
Jay Freyenseef994d9d2016-08-17 15:00:26 -0700379
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200380 /*
381 * Set keep-alive timeout in seconds granularity (ms * 1000)
382 * and add a grace period for controller kato enforcement
383 */
384 cmd.connect.kato = ctrl->opts->discovery_nqn ? 0 :
385 cpu_to_le32((ctrl->kato + NVME_KATO_GRACE) * 1000);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200386
Sagi Grimberg8154ed72018-11-19 14:11:15 -0800387 if (ctrl->opts->disable_sqflow)
388 cmd.connect.cattr |= NVME_CONNECT_DISABLE_SQFLOW;
389
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200390 data = kzalloc(sizeof(*data), GFP_KERNEL);
391 if (!data)
392 return -ENOMEM;
393
Christoph Hellwig8e412262017-05-17 09:54:27 +0200394 uuid_copy(&data->hostid, &ctrl->opts->host->id);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200395 data->cntlid = cpu_to_le16(0xffff);
396 strncpy(data->subsysnqn, ctrl->opts->subsysnqn, NVMF_NQN_SIZE);
397 strncpy(data->hostnqn, ctrl->opts->host->nqn, NVMF_NQN_SIZE);
398
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800399 ret = __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, &res,
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200400 data, sizeof(*data), 0, NVME_QID_ANY, 1,
Sagi Grimberg6287b512018-12-14 11:06:07 -0800401 BLK_MQ_REQ_RESERVED | BLK_MQ_REQ_NOWAIT, false);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200402 if (ret) {
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800403 nvmf_log_connect_error(ctrl, ret, le32_to_cpu(res.u32),
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200404 &cmd, data);
405 goto out_free_data;
406 }
407
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800408 ctrl->cntlid = le16_to_cpu(res.u16);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200409
410out_free_data:
411 kfree(data);
412 return ret;
413}
414EXPORT_SYMBOL_GPL(nvmf_connect_admin_queue);
415
416/**
417 * nvmf_connect_io_queue() - NVMe Fabrics I/O Queue "Connect"
418 * API function.
419 * @ctrl: Host nvme controller instance used to establish an
420 * NVMe I/O queue connection to the already allocated NVMe
421 * controller on the target system.
422 * @qid: NVMe I/O queue number for the new I/O connection between
423 * host and target (note qid == 0 is illegal as this is
424 * the Admin queue, per NVMe standard).
Bart Van Asschea467fc52019-02-14 14:50:53 -0800425 * @poll: Whether or not to poll for the completion of the connect cmd.
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200426 *
427 * This function issues a fabrics-protocol connection
428 * of a NVMe I/O queue (via NVMe Fabrics "Connect" command)
429 * between the host system device and the allocated NVMe controller
430 * on the target system.
431 *
432 * Return:
433 * 0: success
434 * > 0: NVMe error status code
435 * < 0: Linux errno error code
436 */
Sagi Grimberg26c68222018-12-14 11:06:08 -0800437int nvmf_connect_io_queue(struct nvme_ctrl *ctrl, u16 qid, bool poll)
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200438{
439 struct nvme_command cmd;
440 struct nvmf_connect_data *data;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800441 union nvme_result res;
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200442 int ret;
443
444 memset(&cmd, 0, sizeof(cmd));
445 cmd.connect.opcode = nvme_fabrics_command;
446 cmd.connect.fctype = nvme_fabrics_type_connect;
447 cmd.connect.qid = cpu_to_le16(qid);
448 cmd.connect.sqsize = cpu_to_le16(ctrl->sqsize);
449
Sagi Grimberg8154ed72018-11-19 14:11:15 -0800450 if (ctrl->opts->disable_sqflow)
451 cmd.connect.cattr |= NVME_CONNECT_DISABLE_SQFLOW;
452
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200453 data = kzalloc(sizeof(*data), GFP_KERNEL);
454 if (!data)
455 return -ENOMEM;
456
Christoph Hellwig8e412262017-05-17 09:54:27 +0200457 uuid_copy(&data->hostid, &ctrl->opts->host->id);
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,
Sagi Grimberg26c68222018-12-14 11:06:08 -0800464 BLK_MQ_REQ_RESERVED | BLK_MQ_REQ_NOWAIT, poll);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200465 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
Sagi Grimberg42a45272017-03-18 20:52:36 +0200474bool nvmf_should_reconnect(struct nvme_ctrl *ctrl)
475{
Tal Shorer66414e82018-08-07 23:42:39 +0300476 if (ctrl->opts->max_reconnects == -1 ||
Sagi Grimbergfdf9dfa2017-05-04 13:33:15 +0300477 ctrl->nr_reconnects < ctrl->opts->max_reconnects)
Sagi Grimberg42a45272017-03-18 20:52:36 +0200478 return true;
479
480 return false;
481}
482EXPORT_SYMBOL_GPL(nvmf_should_reconnect);
483
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200484/**
485 * nvmf_register_transport() - NVMe Fabrics Library registration function.
486 * @ops: Transport ops instance to be registered to the
487 * common fabrics library.
488 *
489 * API function that registers the type of specific transport fabric
490 * being implemented to the common NVMe fabrics library. Part of
491 * the overall init sequence of starting up a fabrics driver.
492 */
Johannes Thumshirne5a39dd2017-01-27 09:03:45 +0100493int nvmf_register_transport(struct nvmf_transport_ops *ops)
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200494{
Christoph Hellwig5a1e5952018-02-22 07:24:08 -0800495 if (!ops->create_ctrl)
Johannes Thumshirne5a39dd2017-01-27 09:03:45 +0100496 return -EINVAL;
497
Roland Dreier489beb92017-08-29 10:33:44 -0700498 down_write(&nvmf_transports_rwsem);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200499 list_add_tail(&ops->entry, &nvmf_transports);
Roland Dreier489beb92017-08-29 10:33:44 -0700500 up_write(&nvmf_transports_rwsem);
Johannes Thumshirne5a39dd2017-01-27 09:03:45 +0100501
502 return 0;
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200503}
504EXPORT_SYMBOL_GPL(nvmf_register_transport);
505
506/**
507 * nvmf_unregister_transport() - NVMe Fabrics Library unregistration function.
508 * @ops: Transport ops instance to be unregistered from the
509 * common fabrics library.
510 *
511 * Fabrics API function that unregisters the type of specific transport
512 * fabric being implemented from the common NVMe fabrics library.
513 * Part of the overall exit sequence of unloading the implemented driver.
514 */
515void nvmf_unregister_transport(struct nvmf_transport_ops *ops)
516{
Roland Dreier489beb92017-08-29 10:33:44 -0700517 down_write(&nvmf_transports_rwsem);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200518 list_del(&ops->entry);
Roland Dreier489beb92017-08-29 10:33:44 -0700519 up_write(&nvmf_transports_rwsem);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200520}
521EXPORT_SYMBOL_GPL(nvmf_unregister_transport);
522
523static struct nvmf_transport_ops *nvmf_lookup_transport(
524 struct nvmf_ctrl_options *opts)
525{
526 struct nvmf_transport_ops *ops;
527
Roland Dreier489beb92017-08-29 10:33:44 -0700528 lockdep_assert_held(&nvmf_transports_rwsem);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200529
530 list_for_each_entry(ops, &nvmf_transports, entry) {
531 if (strcmp(ops->name, opts->transport) == 0)
532 return ops;
533 }
534
535 return NULL;
536}
537
Christoph Hellwig3bc32bb2018-06-11 17:34:06 +0200538/*
539 * For something we're not in a state to send to the device the default action
540 * is to busy it and retry it after the controller state is recovered. However,
James Smart6cdefc62018-07-20 15:49:48 -0700541 * if the controller is deleting or if anything is marked for failfast or
542 * nvme multipath it is immediately failed.
Christoph Hellwig3bc32bb2018-06-11 17:34:06 +0200543 *
544 * Note: commands used to initialize the controller will be marked for failfast.
545 * Note: nvme cli/ioctl commands are marked for failfast.
546 */
James Smart6cdefc62018-07-20 15:49:48 -0700547blk_status_t nvmf_fail_nonready_command(struct nvme_ctrl *ctrl,
548 struct request *rq)
Christoph Hellwig3bc32bb2018-06-11 17:34:06 +0200549{
James Smart6cdefc62018-07-20 15:49:48 -0700550 if (ctrl->state != NVME_CTRL_DELETING &&
551 ctrl->state != NVME_CTRL_DEAD &&
552 !blk_noretry_request(rq) && !(rq->cmd_flags & REQ_NVME_MPATH))
Christoph Hellwig3bc32bb2018-06-11 17:34:06 +0200553 return BLK_STS_RESOURCE;
James Smart783f4a42018-09-27 16:58:54 -0700554
555 nvme_req(rq)->status = NVME_SC_HOST_PATH_ERROR;
556 blk_mq_start_request(rq);
557 nvme_complete_rq(rq);
558 return BLK_STS_OK;
Christoph Hellwig3bc32bb2018-06-11 17:34:06 +0200559}
560EXPORT_SYMBOL_GPL(nvmf_fail_nonready_command);
561
562bool __nvmf_check_ready(struct nvme_ctrl *ctrl, struct request *rq,
563 bool queue_live)
James Smartbb06ec312018-04-12 09:16:15 -0600564{
Christoph Hellwig35897b92018-06-11 17:41:11 +0200565 struct nvme_request *req = nvme_req(rq);
James Smartbb06ec312018-04-12 09:16:15 -0600566
Christoph Hellwig35897b92018-06-11 17:41:11 +0200567 /*
568 * If we are in some state of setup or teardown only allow
569 * internally generated commands.
570 */
571 if (!blk_rq_is_passthrough(rq) || (req->flags & NVME_REQ_USERCMD))
572 return false;
573
574 /*
575 * Only allow commands on a live queue, except for the connect command,
576 * which is require to set the queue live in the appropinquate states.
577 */
James Smartbb06ec312018-04-12 09:16:15 -0600578 switch (ctrl->state) {
James Smartbb06ec312018-04-12 09:16:15 -0600579 case NVME_CTRL_NEW:
580 case NVME_CTRL_CONNECTING:
Christoph Hellwig35897b92018-06-11 17:41:11 +0200581 if (req->cmd->common.opcode == nvme_fabrics_command &&
582 req->cmd->fabrics.fctype == nvme_fabrics_type_connect)
Christoph Hellwig3bc32bb2018-06-11 17:34:06 +0200583 return true;
Christoph Hellwig35897b92018-06-11 17:41:11 +0200584 break;
James Smartbb06ec312018-04-12 09:16:15 -0600585 default:
Christoph Hellwig35897b92018-06-11 17:41:11 +0200586 break;
587 case NVME_CTRL_DEAD:
Christoph Hellwig3bc32bb2018-06-11 17:34:06 +0200588 return false;
James Smartbb06ec312018-04-12 09:16:15 -0600589 }
Christoph Hellwig35897b92018-06-11 17:41:11 +0200590
591 return queue_live;
James Smartbb06ec312018-04-12 09:16:15 -0600592}
Christoph Hellwig3bc32bb2018-06-11 17:34:06 +0200593EXPORT_SYMBOL_GPL(__nvmf_check_ready);
James Smartbb06ec312018-04-12 09:16:15 -0600594
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200595static const match_table_t opt_tokens = {
596 { NVMF_OPT_TRANSPORT, "transport=%s" },
597 { NVMF_OPT_TRADDR, "traddr=%s" },
598 { NVMF_OPT_TRSVCID, "trsvcid=%s" },
599 { NVMF_OPT_NQN, "nqn=%s" },
600 { NVMF_OPT_QUEUE_SIZE, "queue_size=%d" },
601 { NVMF_OPT_NR_IO_QUEUES, "nr_io_queues=%d" },
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200602 { NVMF_OPT_RECONNECT_DELAY, "reconnect_delay=%d" },
Sagi Grimberg42a45272017-03-18 20:52:36 +0200603 { NVMF_OPT_CTRL_LOSS_TMO, "ctrl_loss_tmo=%d" },
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200604 { NVMF_OPT_KATO, "keep_alive_tmo=%d" },
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200605 { NVMF_OPT_HOSTNQN, "hostnqn=%s" },
James Smart478bcb92016-08-02 10:42:10 +0300606 { NVMF_OPT_HOST_TRADDR, "host_traddr=%s" },
Johannes Thumshirn6bfe0422017-06-20 14:23:01 +0200607 { NVMF_OPT_HOST_ID, "hostid=%s" },
James Smart3b338762017-10-20 16:17:06 -0700608 { NVMF_OPT_DUP_CONNECT, "duplicate_connect" },
Sagi Grimberg8154ed72018-11-19 14:11:15 -0800609 { NVMF_OPT_DISABLE_SQFLOW, "disable_sqflow" },
Sagi Grimberg3b49fa82018-12-03 17:52:12 -0800610 { NVMF_OPT_HDR_DIGEST, "hdr_digest" },
Sagi Grimberg20d44e82018-12-03 17:52:13 -0800611 { NVMF_OPT_DATA_DIGEST, "data_digest" },
Sagi Grimberg330f6b82018-12-11 23:38:56 -0800612 { NVMF_OPT_NR_WRITE_QUEUES, "nr_write_queues=%d" },
Sagi Grimberg89d43802018-12-14 11:06:09 -0800613 { NVMF_OPT_NR_POLL_QUEUES, "nr_poll_queues=%d" },
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200614 { NVMF_OPT_ERR, NULL }
615};
616
617static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
618 const char *buf)
619{
620 substring_t args[MAX_OPT_ARGS];
621 char *options, *o, *p;
622 int token, ret = 0;
623 size_t nqnlen = 0;
Sagi Grimberg42a45272017-03-18 20:52:36 +0200624 int ctrl_loss_tmo = NVMF_DEF_CTRL_LOSS_TMO;
Johannes Thumshirn6bfe0422017-06-20 14:23:01 +0200625 uuid_t hostid;
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200626
627 /* Set defaults */
628 opts->queue_size = NVMF_DEF_QUEUE_SIZE;
629 opts->nr_io_queues = num_online_cpus();
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200630 opts->reconnect_delay = NVMF_DEF_RECONNECT_DELAY;
Guilherme G. Piccoli8edd11c2017-09-14 13:59:28 -0300631 opts->kato = NVME_DEFAULT_KATO;
James Smart3b338762017-10-20 16:17:06 -0700632 opts->duplicate_connect = false;
Sagi Grimberg3b49fa82018-12-03 17:52:12 -0800633 opts->hdr_digest = false;
Sagi Grimberg20d44e82018-12-03 17:52:13 -0800634 opts->data_digest = false;
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200635
636 options = o = kstrdup(buf, GFP_KERNEL);
637 if (!options)
638 return -ENOMEM;
639
Johannes Thumshirn6bfe0422017-06-20 14:23:01 +0200640 uuid_gen(&hostid);
641
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200642 while ((p = strsep(&o, ",\n")) != NULL) {
643 if (!*p)
644 continue;
645
646 token = match_token(p, opt_tokens, args);
647 opts->mask |= token;
648 switch (token) {
649 case NVMF_OPT_TRANSPORT:
650 p = match_strdup(args);
651 if (!p) {
652 ret = -ENOMEM;
653 goto out;
654 }
Chengguang Xu59a2f3f2018-04-14 20:06:19 +0800655 kfree(opts->transport);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200656 opts->transport = p;
657 break;
658 case NVMF_OPT_NQN:
659 p = match_strdup(args);
660 if (!p) {
661 ret = -ENOMEM;
662 goto out;
663 }
Chengguang Xu59a2f3f2018-04-14 20:06:19 +0800664 kfree(opts->subsysnqn);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200665 opts->subsysnqn = p;
666 nqnlen = strlen(opts->subsysnqn);
667 if (nqnlen >= NVMF_NQN_SIZE) {
668 pr_err("%s needs to be < %d bytes\n",
Bart Van Assche6eb72832016-10-18 13:10:24 -0700669 opts->subsysnqn, NVMF_NQN_SIZE);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200670 ret = -EINVAL;
671 goto out;
672 }
673 opts->discovery_nqn =
674 !(strcmp(opts->subsysnqn,
675 NVME_DISC_SUBSYS_NAME));
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200676 break;
677 case NVMF_OPT_TRADDR:
678 p = match_strdup(args);
679 if (!p) {
680 ret = -ENOMEM;
681 goto out;
682 }
Chengguang Xu59a2f3f2018-04-14 20:06:19 +0800683 kfree(opts->traddr);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200684 opts->traddr = p;
685 break;
686 case NVMF_OPT_TRSVCID:
687 p = match_strdup(args);
688 if (!p) {
689 ret = -ENOMEM;
690 goto out;
691 }
Chengguang Xu59a2f3f2018-04-14 20:06:19 +0800692 kfree(opts->trsvcid);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200693 opts->trsvcid = p;
694 break;
695 case NVMF_OPT_QUEUE_SIZE:
696 if (match_int(args, &token)) {
697 ret = -EINVAL;
698 goto out;
699 }
700 if (token < NVMF_MIN_QUEUE_SIZE ||
701 token > NVMF_MAX_QUEUE_SIZE) {
702 pr_err("Invalid queue_size %d\n", token);
703 ret = -EINVAL;
704 goto out;
705 }
706 opts->queue_size = token;
707 break;
708 case NVMF_OPT_NR_IO_QUEUES:
709 if (match_int(args, &token)) {
710 ret = -EINVAL;
711 goto out;
712 }
713 if (token <= 0) {
714 pr_err("Invalid number of IOQs %d\n", token);
715 ret = -EINVAL;
716 goto out;
717 }
Roland Dreier04758212018-03-05 11:59:53 -0800718 if (opts->discovery_nqn) {
719 pr_debug("Ignoring nr_io_queues value for discovery controller\n");
720 break;
721 }
722
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200723 opts->nr_io_queues = min_t(unsigned int,
724 num_online_cpus(), token);
725 break;
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200726 case NVMF_OPT_KATO:
727 if (match_int(args, &token)) {
728 ret = -EINVAL;
729 goto out;
730 }
731
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200732 if (token < 0) {
733 pr_err("Invalid keep_alive_tmo %d\n", token);
734 ret = -EINVAL;
735 goto out;
Guilherme G. Piccoli8edd11c2017-09-14 13:59:28 -0300736 } else if (token == 0 && !opts->discovery_nqn) {
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200737 /* Allowed for debug */
738 pr_warn("keep_alive_tmo 0 won't execute keep alives!!!\n");
739 }
740 opts->kato = token;
Guilherme G. Piccoli8edd11c2017-09-14 13:59:28 -0300741
742 if (opts->discovery_nqn && opts->kato) {
743 pr_err("Discovery controllers cannot accept KATO != 0\n");
744 ret = -EINVAL;
745 goto out;
746 }
747
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200748 break;
Sagi Grimberg42a45272017-03-18 20:52:36 +0200749 case NVMF_OPT_CTRL_LOSS_TMO:
750 if (match_int(args, &token)) {
751 ret = -EINVAL;
752 goto out;
753 }
754
755 if (token < 0)
756 pr_warn("ctrl_loss_tmo < 0 will reconnect forever\n");
757 ctrl_loss_tmo = token;
758 break;
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200759 case NVMF_OPT_HOSTNQN:
760 if (opts->host) {
761 pr_err("hostnqn already user-assigned: %s\n",
762 opts->host->nqn);
763 ret = -EADDRINUSE;
764 goto out;
765 }
766 p = match_strdup(args);
767 if (!p) {
768 ret = -ENOMEM;
769 goto out;
770 }
771 nqnlen = strlen(p);
772 if (nqnlen >= NVMF_NQN_SIZE) {
773 pr_err("%s needs to be < %d bytes\n",
774 p, NVMF_NQN_SIZE);
Bart Van Assche8eadfcb2016-10-18 13:10:46 -0700775 kfree(p);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200776 ret = -EINVAL;
777 goto out;
778 }
Chengguang Xu59a2f3f2018-04-14 20:06:19 +0800779 nvmf_host_put(opts->host);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200780 opts->host = nvmf_host_add(p);
Bart Van Assche8eadfcb2016-10-18 13:10:46 -0700781 kfree(p);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200782 if (!opts->host) {
783 ret = -ENOMEM;
784 goto out;
785 }
786 break;
787 case NVMF_OPT_RECONNECT_DELAY:
788 if (match_int(args, &token)) {
789 ret = -EINVAL;
790 goto out;
791 }
792 if (token <= 0) {
793 pr_err("Invalid reconnect_delay %d\n", token);
794 ret = -EINVAL;
795 goto out;
796 }
797 opts->reconnect_delay = token;
798 break;
James Smart478bcb92016-08-02 10:42:10 +0300799 case NVMF_OPT_HOST_TRADDR:
800 p = match_strdup(args);
801 if (!p) {
802 ret = -ENOMEM;
803 goto out;
804 }
Chengguang Xu59a2f3f2018-04-14 20:06:19 +0800805 kfree(opts->host_traddr);
James Smart478bcb92016-08-02 10:42:10 +0300806 opts->host_traddr = p;
807 break;
Johannes Thumshirn6bfe0422017-06-20 14:23:01 +0200808 case NVMF_OPT_HOST_ID:
809 p = match_strdup(args);
810 if (!p) {
811 ret = -ENOMEM;
812 goto out;
813 }
Roland Dreierdf351ef2018-01-11 13:38:00 -0800814 ret = uuid_parse(p, &hostid);
Roland Dreierdf351ef2018-01-11 13:38:00 -0800815 if (ret) {
Guan Junxiong9b483da2017-08-03 21:40:26 +0800816 pr_err("Invalid hostid %s\n", p);
Johannes Thumshirn6bfe0422017-06-20 14:23:01 +0200817 ret = -EINVAL;
Johannes Thumshirn6e494122018-01-25 09:09:25 +0100818 kfree(p);
Johannes Thumshirn6bfe0422017-06-20 14:23:01 +0200819 goto out;
820 }
Johannes Thumshirn6e494122018-01-25 09:09:25 +0100821 kfree(p);
Johannes Thumshirn6bfe0422017-06-20 14:23:01 +0200822 break;
James Smart3b338762017-10-20 16:17:06 -0700823 case NVMF_OPT_DUP_CONNECT:
824 opts->duplicate_connect = true;
825 break;
Sagi Grimberg8154ed72018-11-19 14:11:15 -0800826 case NVMF_OPT_DISABLE_SQFLOW:
827 opts->disable_sqflow = true;
828 break;
Sagi Grimberg3b49fa82018-12-03 17:52:12 -0800829 case NVMF_OPT_HDR_DIGEST:
830 opts->hdr_digest = true;
831 break;
Sagi Grimberg20d44e82018-12-03 17:52:13 -0800832 case NVMF_OPT_DATA_DIGEST:
833 opts->data_digest = true;
834 break;
Sagi Grimberg330f6b82018-12-11 23:38:56 -0800835 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;
Sagi Grimberg89d43802018-12-14 11:06:09 -0800847 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;
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200859 default:
860 pr_warn("unknown parameter or missing value '%s' in ctrl creation request\n",
861 p);
862 ret = -EINVAL;
863 goto out;
864 }
865 }
866
Hannes Reinecke461fbc82018-05-24 16:18:15 +0200867 if (opts->discovery_nqn) {
868 opts->kato = 0;
869 opts->nr_io_queues = 0;
Sagi Grimberg9846ac02019-01-07 23:54:23 -0800870 opts->nr_write_queues = 0;
871 opts->nr_poll_queues = 0;
Hannes Reinecke181303d2018-05-24 16:18:17 +0200872 opts->duplicate_connect = true;
Hannes Reinecke461fbc82018-05-24 16:18:15 +0200873 }
Sagi Grimberg42a45272017-03-18 20:52:36 +0200874 if (ctrl_loss_tmo < 0)
875 opts->max_reconnects = -1;
876 else
877 opts->max_reconnects = DIV_ROUND_UP(ctrl_loss_tmo,
878 opts->reconnect_delay);
879
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200880 if (!opts->host) {
881 kref_get(&nvmf_default_host->ref);
882 opts->host = nvmf_default_host;
883 }
884
Johannes Thumshirn6bfe0422017-06-20 14:23:01 +0200885 uuid_copy(&opts->host->id, &hostid);
886
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200887out:
888 kfree(options);
889 return ret;
890}
891
892static int nvmf_check_required_opts(struct nvmf_ctrl_options *opts,
893 unsigned int required_opts)
894{
895 if ((opts->mask & required_opts) != required_opts) {
896 int i;
897
898 for (i = 0; i < ARRAY_SIZE(opt_tokens); i++) {
899 if ((opt_tokens[i].token & required_opts) &&
900 !(opt_tokens[i].token & opts->mask)) {
901 pr_warn("missing parameter '%s'\n",
902 opt_tokens[i].pattern);
903 }
904 }
905
906 return -EINVAL;
907 }
908
909 return 0;
910}
911
Sagi Grimbergb7c7be6f62018-10-18 17:40:40 -0700912bool nvmf_ip_options_match(struct nvme_ctrl *ctrl,
913 struct nvmf_ctrl_options *opts)
914{
915 if (!nvmf_ctlr_matches_baseopts(ctrl, opts) ||
916 strcmp(opts->traddr, ctrl->opts->traddr) ||
917 strcmp(opts->trsvcid, ctrl->opts->trsvcid))
918 return false;
919
920 /*
921 * Checking the local address is rough. In most cases, none is specified
922 * and the host port is selected by the stack.
923 *
924 * Assume no match if:
925 * - local address is specified and address is not the same
926 * - local address is not specified but remote is, or vice versa
927 * (admin using specific host_traddr when it matters).
928 */
929 if ((opts->mask & NVMF_OPT_HOST_TRADDR) &&
930 (ctrl->opts->mask & NVMF_OPT_HOST_TRADDR)) {
931 if (strcmp(opts->host_traddr, ctrl->opts->host_traddr))
932 return false;
933 } else if ((opts->mask & NVMF_OPT_HOST_TRADDR) ||
934 (ctrl->opts->mask & NVMF_OPT_HOST_TRADDR)) {
935 return false;
936 }
937
938 return true;
939}
940EXPORT_SYMBOL_GPL(nvmf_ip_options_match);
941
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200942static int nvmf_check_allowed_opts(struct nvmf_ctrl_options *opts,
943 unsigned int allowed_opts)
944{
945 if (opts->mask & ~allowed_opts) {
946 int i;
947
948 for (i = 0; i < ARRAY_SIZE(opt_tokens); i++) {
Christoph Hellwig81a0b8d2017-08-17 13:57:49 +0200949 if ((opt_tokens[i].token & opts->mask) &&
950 (opt_tokens[i].token & ~allowed_opts)) {
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200951 pr_warn("invalid parameter '%s'\n",
952 opt_tokens[i].pattern);
953 }
954 }
955
956 return -EINVAL;
957 }
958
959 return 0;
960}
961
962void nvmf_free_options(struct nvmf_ctrl_options *opts)
963{
964 nvmf_host_put(opts->host);
965 kfree(opts->transport);
966 kfree(opts->traddr);
967 kfree(opts->trsvcid);
968 kfree(opts->subsysnqn);
James Smart478bcb92016-08-02 10:42:10 +0300969 kfree(opts->host_traddr);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200970 kfree(opts);
971}
972EXPORT_SYMBOL_GPL(nvmf_free_options);
973
974#define NVMF_REQUIRED_OPTS (NVMF_OPT_TRANSPORT | NVMF_OPT_NQN)
975#define NVMF_ALLOWED_OPTS (NVMF_OPT_QUEUE_SIZE | NVMF_OPT_NR_IO_QUEUES | \
Johannes Thumshirn6bfe0422017-06-20 14:23:01 +0200976 NVMF_OPT_KATO | NVMF_OPT_HOSTNQN | \
Sagi Grimberg8154ed72018-11-19 14:11:15 -0800977 NVMF_OPT_HOST_ID | NVMF_OPT_DUP_CONNECT |\
978 NVMF_OPT_DISABLE_SQFLOW)
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200979
980static struct nvme_ctrl *
981nvmf_create_ctrl(struct device *dev, const char *buf, size_t count)
982{
983 struct nvmf_ctrl_options *opts;
984 struct nvmf_transport_ops *ops;
985 struct nvme_ctrl *ctrl;
986 int ret;
987
988 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
989 if (!opts)
990 return ERR_PTR(-ENOMEM);
991
992 ret = nvmf_parse_options(opts, buf);
993 if (ret)
994 goto out_free_opts;
995
Sagi Grimbergd1f10712017-09-24 16:15:55 +0300996
997 request_module("nvme-%s", opts->transport);
998
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200999 /*
1000 * Check the generic options first as we need a valid transport for
1001 * the lookup below. Then clear the generic flags so that transport
1002 * drivers don't have to care about them.
1003 */
1004 ret = nvmf_check_required_opts(opts, NVMF_REQUIRED_OPTS);
1005 if (ret)
1006 goto out_free_opts;
1007 opts->mask &= ~NVMF_REQUIRED_OPTS;
1008
Roland Dreier489beb92017-08-29 10:33:44 -07001009 down_read(&nvmf_transports_rwsem);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001010 ops = nvmf_lookup_transport(opts);
1011 if (!ops) {
1012 pr_info("no handler found for transport %s.\n",
1013 opts->transport);
1014 ret = -EINVAL;
1015 goto out_unlock;
1016 }
1017
Roy Shterman0de5cd32017-12-25 14:18:30 +02001018 if (!try_module_get(ops->module)) {
1019 ret = -EBUSY;
1020 goto out_unlock;
1021 }
Johannes Thumshirn12a0b662018-06-01 09:11:20 +02001022 up_read(&nvmf_transports_rwsem);
Roy Shterman0de5cd32017-12-25 14:18:30 +02001023
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001024 ret = nvmf_check_required_opts(opts, ops->required_opts);
1025 if (ret)
Roy Shterman0de5cd32017-12-25 14:18:30 +02001026 goto out_module_put;
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001027 ret = nvmf_check_allowed_opts(opts, NVMF_ALLOWED_OPTS |
1028 ops->allowed_opts | ops->required_opts);
1029 if (ret)
Roy Shterman0de5cd32017-12-25 14:18:30 +02001030 goto out_module_put;
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001031
1032 ctrl = ops->create_ctrl(dev, opts);
1033 if (IS_ERR(ctrl)) {
1034 ret = PTR_ERR(ctrl);
Roy Shterman0de5cd32017-12-25 14:18:30 +02001035 goto out_module_put;
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001036 }
1037
Roy Shterman0de5cd32017-12-25 14:18:30 +02001038 module_put(ops->module);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001039 return ctrl;
1040
Roy Shterman0de5cd32017-12-25 14:18:30 +02001041out_module_put:
1042 module_put(ops->module);
Johannes Thumshirn12a0b662018-06-01 09:11:20 +02001043 goto out_free_opts;
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001044out_unlock:
Roland Dreier489beb92017-08-29 10:33:44 -07001045 up_read(&nvmf_transports_rwsem);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001046out_free_opts:
Bart Van Asschef3116d82016-10-18 13:11:03 -07001047 nvmf_free_options(opts);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001048 return ERR_PTR(ret);
1049}
1050
1051static struct class *nvmf_class;
1052static struct device *nvmf_device;
1053static DEFINE_MUTEX(nvmf_dev_mutex);
1054
1055static ssize_t nvmf_dev_write(struct file *file, const char __user *ubuf,
1056 size_t count, loff_t *pos)
1057{
1058 struct seq_file *seq_file = file->private_data;
1059 struct nvme_ctrl *ctrl;
1060 const char *buf;
1061 int ret = 0;
1062
1063 if (count > PAGE_SIZE)
1064 return -ENOMEM;
1065
1066 buf = memdup_user_nul(ubuf, count);
1067 if (IS_ERR(buf))
1068 return PTR_ERR(buf);
1069
1070 mutex_lock(&nvmf_dev_mutex);
1071 if (seq_file->private) {
1072 ret = -EINVAL;
1073 goto out_unlock;
1074 }
1075
1076 ctrl = nvmf_create_ctrl(nvmf_device, buf, count);
1077 if (IS_ERR(ctrl)) {
1078 ret = PTR_ERR(ctrl);
1079 goto out_unlock;
1080 }
1081
1082 seq_file->private = ctrl;
1083
1084out_unlock:
1085 mutex_unlock(&nvmf_dev_mutex);
1086 kfree(buf);
1087 return ret ? ret : count;
1088}
1089
1090static int nvmf_dev_show(struct seq_file *seq_file, void *private)
1091{
1092 struct nvme_ctrl *ctrl;
1093 int ret = 0;
1094
1095 mutex_lock(&nvmf_dev_mutex);
1096 ctrl = seq_file->private;
1097 if (!ctrl) {
1098 ret = -EINVAL;
1099 goto out_unlock;
1100 }
1101
1102 seq_printf(seq_file, "instance=%d,cntlid=%d\n",
1103 ctrl->instance, ctrl->cntlid);
1104
1105out_unlock:
1106 mutex_unlock(&nvmf_dev_mutex);
1107 return ret;
1108}
1109
1110static int nvmf_dev_open(struct inode *inode, struct file *file)
1111{
1112 /*
1113 * The miscdevice code initializes file->private_data, but doesn't
1114 * make use of it later.
1115 */
1116 file->private_data = NULL;
1117 return single_open(file, nvmf_dev_show, NULL);
1118}
1119
1120static int nvmf_dev_release(struct inode *inode, struct file *file)
1121{
1122 struct seq_file *seq_file = file->private_data;
1123 struct nvme_ctrl *ctrl = seq_file->private;
1124
1125 if (ctrl)
1126 nvme_put_ctrl(ctrl);
1127 return single_release(inode, file);
1128}
1129
1130static const struct file_operations nvmf_dev_fops = {
1131 .owner = THIS_MODULE,
1132 .write = nvmf_dev_write,
1133 .read = seq_read,
1134 .open = nvmf_dev_open,
1135 .release = nvmf_dev_release,
1136};
1137
1138static struct miscdevice nvmf_misc = {
1139 .minor = MISC_DYNAMIC_MINOR,
1140 .name = "nvme-fabrics",
1141 .fops = &nvmf_dev_fops,
1142};
1143
1144static int __init nvmf_init(void)
1145{
1146 int ret;
1147
1148 nvmf_default_host = nvmf_host_default();
1149 if (!nvmf_default_host)
1150 return -ENOMEM;
1151
1152 nvmf_class = class_create(THIS_MODULE, "nvme-fabrics");
1153 if (IS_ERR(nvmf_class)) {
1154 pr_err("couldn't register class nvme-fabrics\n");
1155 ret = PTR_ERR(nvmf_class);
1156 goto out_free_host;
1157 }
1158
1159 nvmf_device =
1160 device_create(nvmf_class, NULL, MKDEV(0, 0), NULL, "ctl");
1161 if (IS_ERR(nvmf_device)) {
1162 pr_err("couldn't create nvme-fabris device!\n");
1163 ret = PTR_ERR(nvmf_device);
1164 goto out_destroy_class;
1165 }
1166
1167 ret = misc_register(&nvmf_misc);
1168 if (ret) {
1169 pr_err("couldn't register misc device: %d\n", ret);
1170 goto out_destroy_device;
1171 }
1172
1173 return 0;
1174
1175out_destroy_device:
1176 device_destroy(nvmf_class, MKDEV(0, 0));
1177out_destroy_class:
1178 class_destroy(nvmf_class);
1179out_free_host:
1180 nvmf_host_put(nvmf_default_host);
1181 return ret;
1182}
1183
1184static void __exit nvmf_exit(void)
1185{
1186 misc_deregister(&nvmf_misc);
1187 device_destroy(nvmf_class, MKDEV(0, 0));
1188 class_destroy(nvmf_class);
1189 nvmf_host_put(nvmf_default_host);
1190
1191 BUILD_BUG_ON(sizeof(struct nvmf_connect_command) != 64);
1192 BUILD_BUG_ON(sizeof(struct nvmf_property_get_command) != 64);
1193 BUILD_BUG_ON(sizeof(struct nvmf_property_set_command) != 64);
1194 BUILD_BUG_ON(sizeof(struct nvmf_connect_data) != 1024);
1195}
1196
1197MODULE_LICENSE("GPL v2");
1198
1199module_init(nvmf_init);
1200module_exit(nvmf_exit);