blob: fa32c1216409a349ed502902317e443471bbd747 [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);
Roland Dreier489beb92017-08-29 10:33:44 -070025static DECLARE_RWSEM(nvmf_transports_rwsem);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +020026
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);
Hannes Reinecke1e5f4462018-05-25 11:04:03 +020060 strlcpy(host->nqn, hostnqn, NVMF_NQN_SIZE);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +020061
62 list_add_tail(&host->list, &nvmf_hosts);
63out_unlock:
64 mutex_unlock(&nvmf_hosts_mutex);
65 return host;
66}
67
68static struct nvmf_host *nvmf_host_default(void)
69{
70 struct nvmf_host *host;
71
72 host = kmalloc(sizeof(*host), GFP_KERNEL);
73 if (!host)
74 return NULL;
75
76 kref_init(&host->ref);
Ewan D. Milne6b018232018-01-05 12:44:06 -050077 uuid_gen(&host->id);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +020078 snprintf(host->nqn, NVMF_NQN_SIZE,
Daniel Verkamp40a5fce2017-08-30 15:18:19 -070079 "nqn.2014-08.org.nvmexpress:uuid:%pUb", &host->id);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +020080
81 mutex_lock(&nvmf_hosts_mutex);
82 list_add_tail(&host->list, &nvmf_hosts);
83 mutex_unlock(&nvmf_hosts_mutex);
84
85 return host;
86}
87
88static void nvmf_host_destroy(struct kref *ref)
89{
90 struct nvmf_host *host = container_of(ref, struct nvmf_host, ref);
91
Ming Line76debd2016-07-01 12:13:32 -070092 mutex_lock(&nvmf_hosts_mutex);
93 list_del(&host->list);
94 mutex_unlock(&nvmf_hosts_mutex);
95
Christoph Hellwig07bfcd02016-06-13 16:45:26 +020096 kfree(host);
97}
98
99static void nvmf_host_put(struct nvmf_host *host)
100{
101 if (host)
102 kref_put(&host->ref, nvmf_host_destroy);
103}
104
105/**
106 * nvmf_get_address() - Get address/port
107 * @ctrl: Host NVMe controller instance which we got the address
108 * @buf: OUTPUT parameter that will contain the address/port
109 * @size: buffer size
110 */
111int nvmf_get_address(struct nvme_ctrl *ctrl, char *buf, int size)
112{
James Smart0fe51ff2016-08-02 10:40:01 +0300113 int len = 0;
114
115 if (ctrl->opts->mask & NVMF_OPT_TRADDR)
116 len += snprintf(buf, size, "traddr=%s", ctrl->opts->traddr);
117 if (ctrl->opts->mask & NVMF_OPT_TRSVCID)
118 len += snprintf(buf + len, size - len, "%strsvcid=%s",
119 (len) ? "," : "", ctrl->opts->trsvcid);
James Smart478bcb92016-08-02 10:42:10 +0300120 if (ctrl->opts->mask & NVMF_OPT_HOST_TRADDR)
121 len += snprintf(buf + len, size - len, "%shost_traddr=%s",
122 (len) ? "," : "", ctrl->opts->host_traddr);
James Smart0fe51ff2016-08-02 10:40:01 +0300123 len += snprintf(buf + len, size - len, "\n");
124
125 return len;
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200126}
127EXPORT_SYMBOL_GPL(nvmf_get_address);
128
129/**
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200130 * nvmf_reg_read32() - NVMe Fabrics "Property Get" API function.
131 * @ctrl: Host NVMe controller instance maintaining the admin
132 * queue used to submit the property read command to
133 * the allocated NVMe controller resource on the target system.
134 * @off: Starting offset value of the targeted property
135 * register (see the fabrics section of the NVMe standard).
136 * @val: OUTPUT parameter that will contain the value of
137 * the property after a successful read.
138 *
139 * Used by the host system to retrieve a 32-bit capsule property value
140 * from an NVMe controller on the target system.
141 *
142 * ("Capsule property" is an "PCIe register concept" applied to the
143 * NVMe fabrics space.)
144 *
145 * Return:
146 * 0: successful read
147 * > 0: NVMe error status code
148 * < 0: Linux errno error code
149 */
150int nvmf_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val)
151{
152 struct nvme_command cmd;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800153 union nvme_result res;
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200154 int ret;
155
156 memset(&cmd, 0, sizeof(cmd));
157 cmd.prop_get.opcode = nvme_fabrics_command;
158 cmd.prop_get.fctype = nvme_fabrics_type_property_get;
159 cmd.prop_get.offset = cpu_to_le32(off);
160
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800161 ret = __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, &res, NULL, 0, 0,
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200162 NVME_QID_ANY, 0, 0);
163
164 if (ret >= 0)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800165 *val = le64_to_cpu(res.u64);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200166 if (unlikely(ret != 0))
167 dev_err(ctrl->device,
168 "Property Get error: %d, offset %#x\n",
169 ret > 0 ? ret & ~NVME_SC_DNR : ret, off);
170
171 return ret;
172}
173EXPORT_SYMBOL_GPL(nvmf_reg_read32);
174
175/**
176 * nvmf_reg_read64() - NVMe Fabrics "Property Get" API function.
177 * @ctrl: Host NVMe controller instance maintaining the admin
178 * queue used to submit the property read command to
179 * the allocated controller resource on the target system.
180 * @off: Starting offset value of the targeted property
181 * register (see the fabrics section of the NVMe standard).
182 * @val: OUTPUT parameter that will contain the value of
183 * the property after a successful read.
184 *
185 * Used by the host system to retrieve a 64-bit capsule property value
186 * from an NVMe controller on the target system.
187 *
188 * ("Capsule property" is an "PCIe register concept" applied to the
189 * NVMe fabrics space.)
190 *
191 * Return:
192 * 0: successful read
193 * > 0: NVMe error status code
194 * < 0: Linux errno error code
195 */
196int nvmf_reg_read64(struct nvme_ctrl *ctrl, u32 off, u64 *val)
197{
198 struct nvme_command cmd;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800199 union nvme_result res;
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200200 int ret;
201
202 memset(&cmd, 0, sizeof(cmd));
203 cmd.prop_get.opcode = nvme_fabrics_command;
204 cmd.prop_get.fctype = nvme_fabrics_type_property_get;
205 cmd.prop_get.attrib = 1;
206 cmd.prop_get.offset = cpu_to_le32(off);
207
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800208 ret = __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, &res, NULL, 0, 0,
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200209 NVME_QID_ANY, 0, 0);
210
211 if (ret >= 0)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800212 *val = le64_to_cpu(res.u64);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200213 if (unlikely(ret != 0))
214 dev_err(ctrl->device,
215 "Property Get error: %d, offset %#x\n",
216 ret > 0 ? ret & ~NVME_SC_DNR : ret, off);
217 return ret;
218}
219EXPORT_SYMBOL_GPL(nvmf_reg_read64);
220
221/**
222 * nvmf_reg_write32() - NVMe Fabrics "Property Write" API function.
223 * @ctrl: Host NVMe controller instance maintaining the admin
224 * queue used to submit the property read command to
225 * the allocated NVMe controller resource on the target system.
226 * @off: Starting offset value of the targeted property
227 * register (see the fabrics section of the NVMe standard).
228 * @val: Input parameter that contains the value to be
229 * written to the property.
230 *
231 * Used by the NVMe host system to write a 32-bit capsule property value
232 * to an NVMe controller on the target system.
233 *
234 * ("Capsule property" is an "PCIe register concept" applied to the
235 * NVMe fabrics space.)
236 *
237 * Return:
238 * 0: successful write
239 * > 0: NVMe error status code
240 * < 0: Linux errno error code
241 */
242int nvmf_reg_write32(struct nvme_ctrl *ctrl, u32 off, u32 val)
243{
244 struct nvme_command cmd;
245 int ret;
246
247 memset(&cmd, 0, sizeof(cmd));
248 cmd.prop_set.opcode = nvme_fabrics_command;
249 cmd.prop_set.fctype = nvme_fabrics_type_property_set;
250 cmd.prop_set.attrib = 0;
251 cmd.prop_set.offset = cpu_to_le32(off);
252 cmd.prop_set.value = cpu_to_le64(val);
253
254 ret = __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, NULL, NULL, 0, 0,
255 NVME_QID_ANY, 0, 0);
256 if (unlikely(ret))
257 dev_err(ctrl->device,
258 "Property Set error: %d, offset %#x\n",
259 ret > 0 ? ret & ~NVME_SC_DNR : ret, off);
260 return ret;
261}
262EXPORT_SYMBOL_GPL(nvmf_reg_write32);
263
264/**
265 * nvmf_log_connect_error() - Error-parsing-diagnostic print
266 * out function for connect() errors.
267 *
268 * @ctrl: the specific /dev/nvmeX device that had the error.
269 *
270 * @errval: Error code to be decoded in a more human-friendly
271 * printout.
272 *
273 * @offset: For use with the NVMe error code NVME_SC_CONNECT_INVALID_PARAM.
274 *
275 * @cmd: This is the SQE portion of a submission capsule.
276 *
277 * @data: This is the "Data" portion of a submission capsule.
278 */
279static void nvmf_log_connect_error(struct nvme_ctrl *ctrl,
280 int errval, int offset, struct nvme_command *cmd,
281 struct nvmf_connect_data *data)
282{
283 int err_sctype = errval & (~NVME_SC_DNR);
284
285 switch (err_sctype) {
286
287 case (NVME_SC_CONNECT_INVALID_PARAM):
288 if (offset >> 16) {
289 char *inv_data = "Connect Invalid Data Parameter";
290
291 switch (offset & 0xffff) {
292 case (offsetof(struct nvmf_connect_data, cntlid)):
293 dev_err(ctrl->device,
294 "%s, cntlid: %d\n",
295 inv_data, data->cntlid);
296 break;
297 case (offsetof(struct nvmf_connect_data, hostnqn)):
298 dev_err(ctrl->device,
299 "%s, hostnqn \"%s\"\n",
300 inv_data, data->hostnqn);
301 break;
302 case (offsetof(struct nvmf_connect_data, subsysnqn)):
303 dev_err(ctrl->device,
304 "%s, subsysnqn \"%s\"\n",
305 inv_data, data->subsysnqn);
306 break;
307 default:
308 dev_err(ctrl->device,
309 "%s, starting byte offset: %d\n",
310 inv_data, offset & 0xffff);
311 break;
312 }
313 } else {
314 char *inv_sqe = "Connect Invalid SQE Parameter";
315
316 switch (offset) {
317 case (offsetof(struct nvmf_connect_command, qid)):
318 dev_err(ctrl->device,
319 "%s, qid %d\n",
320 inv_sqe, cmd->connect.qid);
321 break;
322 default:
323 dev_err(ctrl->device,
324 "%s, starting byte offset: %d\n",
325 inv_sqe, offset);
326 }
327 }
328 break;
Guan Junxiong97ddc36e2017-06-13 10:51:24 +0800329
330 case NVME_SC_CONNECT_INVALID_HOST:
331 dev_err(ctrl->device,
332 "Connect for subsystem %s is not allowed, hostnqn: %s\n",
333 data->subsysnqn, data->hostnqn);
334 break;
335
336 case NVME_SC_CONNECT_CTRL_BUSY:
337 dev_err(ctrl->device,
338 "Connect command failed: controller is busy or not available\n");
339 break;
340
341 case NVME_SC_CONNECT_FORMAT:
342 dev_err(ctrl->device,
343 "Connect incompatible format: %d",
344 cmd->connect.recfmt);
345 break;
346
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200347 default:
348 dev_err(ctrl->device,
349 "Connect command failed, error wo/DNR bit: %d\n",
350 err_sctype);
351 break;
352 } /* switch (err_sctype) */
353}
354
355/**
356 * nvmf_connect_admin_queue() - NVMe Fabrics Admin Queue "Connect"
357 * API function.
358 * @ctrl: Host nvme controller instance used to request
359 * a new NVMe controller allocation on the target
360 * system and establish an NVMe Admin connection to
361 * that controller.
362 *
363 * This function enables an NVMe host device to request a new allocation of
364 * an NVMe controller resource on a target system as well establish a
365 * fabrics-protocol connection of the NVMe Admin queue between the
366 * host system device and the allocated NVMe controller on the
367 * target system via a NVMe Fabrics "Connect" command.
368 *
369 * Return:
370 * 0: success
371 * > 0: NVMe error status code
372 * < 0: Linux errno error code
373 *
374 */
375int nvmf_connect_admin_queue(struct nvme_ctrl *ctrl)
376{
377 struct nvme_command cmd;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800378 union nvme_result res;
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200379 struct nvmf_connect_data *data;
380 int ret;
381
382 memset(&cmd, 0, sizeof(cmd));
383 cmd.connect.opcode = nvme_fabrics_command;
384 cmd.connect.fctype = nvme_fabrics_type_connect;
385 cmd.connect.qid = 0;
Sagi Grimberg7aa1f422017-06-18 16:15:59 +0300386 cmd.connect.sqsize = cpu_to_le16(NVME_AQ_DEPTH - 1);
Jay Freyenseef994d9d2016-08-17 15:00:26 -0700387
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200388 /*
389 * Set keep-alive timeout in seconds granularity (ms * 1000)
390 * and add a grace period for controller kato enforcement
391 */
392 cmd.connect.kato = ctrl->opts->discovery_nqn ? 0 :
393 cpu_to_le32((ctrl->kato + NVME_KATO_GRACE) * 1000);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200394
395 data = kzalloc(sizeof(*data), GFP_KERNEL);
396 if (!data)
397 return -ENOMEM;
398
Christoph Hellwig8e412262017-05-17 09:54:27 +0200399 uuid_copy(&data->hostid, &ctrl->opts->host->id);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200400 data->cntlid = cpu_to_le16(0xffff);
401 strncpy(data->subsysnqn, ctrl->opts->subsysnqn, NVMF_NQN_SIZE);
402 strncpy(data->hostnqn, ctrl->opts->host->nqn, NVMF_NQN_SIZE);
403
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800404 ret = __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, &res,
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200405 data, sizeof(*data), 0, NVME_QID_ANY, 1,
406 BLK_MQ_REQ_RESERVED | BLK_MQ_REQ_NOWAIT);
407 if (ret) {
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800408 nvmf_log_connect_error(ctrl, ret, le32_to_cpu(res.u32),
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200409 &cmd, data);
410 goto out_free_data;
411 }
412
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800413 ctrl->cntlid = le16_to_cpu(res.u16);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200414
415out_free_data:
416 kfree(data);
417 return ret;
418}
419EXPORT_SYMBOL_GPL(nvmf_connect_admin_queue);
420
421/**
422 * nvmf_connect_io_queue() - NVMe Fabrics I/O Queue "Connect"
423 * API function.
424 * @ctrl: Host nvme controller instance used to establish an
425 * NVMe I/O queue connection to the already allocated NVMe
426 * controller on the target system.
427 * @qid: NVMe I/O queue number for the new I/O connection between
428 * host and target (note qid == 0 is illegal as this is
429 * the Admin queue, per NVMe standard).
430 *
431 * This function issues a fabrics-protocol connection
432 * of a NVMe I/O queue (via NVMe Fabrics "Connect" command)
433 * between the host system device and the allocated NVMe controller
434 * on the target system.
435 *
436 * Return:
437 * 0: success
438 * > 0: NVMe error status code
439 * < 0: Linux errno error code
440 */
441int nvmf_connect_io_queue(struct nvme_ctrl *ctrl, u16 qid)
442{
443 struct nvme_command cmd;
444 struct nvmf_connect_data *data;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800445 union nvme_result res;
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200446 int ret;
447
448 memset(&cmd, 0, sizeof(cmd));
449 cmd.connect.opcode = nvme_fabrics_command;
450 cmd.connect.fctype = nvme_fabrics_type_connect;
451 cmd.connect.qid = cpu_to_le16(qid);
452 cmd.connect.sqsize = cpu_to_le16(ctrl->sqsize);
453
454 data = kzalloc(sizeof(*data), GFP_KERNEL);
455 if (!data)
456 return -ENOMEM;
457
Christoph Hellwig8e412262017-05-17 09:54:27 +0200458 uuid_copy(&data->hostid, &ctrl->opts->host->id);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200459 data->cntlid = cpu_to_le16(ctrl->cntlid);
460 strncpy(data->subsysnqn, ctrl->opts->subsysnqn, NVMF_NQN_SIZE);
461 strncpy(data->hostnqn, ctrl->opts->host->nqn, NVMF_NQN_SIZE);
462
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800463 ret = __nvme_submit_sync_cmd(ctrl->connect_q, &cmd, &res,
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200464 data, sizeof(*data), 0, qid, 1,
465 BLK_MQ_REQ_RESERVED | BLK_MQ_REQ_NOWAIT);
466 if (ret) {
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800467 nvmf_log_connect_error(ctrl, ret, le32_to_cpu(res.u32),
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200468 &cmd, data);
469 }
470 kfree(data);
471 return ret;
472}
473EXPORT_SYMBOL_GPL(nvmf_connect_io_queue);
474
Sagi Grimberg42a45272017-03-18 20:52:36 +0200475bool nvmf_should_reconnect(struct nvme_ctrl *ctrl)
476{
477 if (ctrl->opts->max_reconnects != -1 &&
Sagi Grimbergfdf9dfa2017-05-04 13:33:15 +0300478 ctrl->nr_reconnects < ctrl->opts->max_reconnects)
Sagi Grimberg42a45272017-03-18 20:52:36 +0200479 return true;
480
481 return false;
482}
483EXPORT_SYMBOL_GPL(nvmf_should_reconnect);
484
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200485/**
486 * nvmf_register_transport() - NVMe Fabrics Library registration function.
487 * @ops: Transport ops instance to be registered to the
488 * common fabrics library.
489 *
490 * API function that registers the type of specific transport fabric
491 * being implemented to the common NVMe fabrics library. Part of
492 * the overall init sequence of starting up a fabrics driver.
493 */
Johannes Thumshirne5a39dd2017-01-27 09:03:45 +0100494int nvmf_register_transport(struct nvmf_transport_ops *ops)
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200495{
Christoph Hellwig5a1e5952018-02-22 07:24:08 -0800496 if (!ops->create_ctrl)
Johannes Thumshirne5a39dd2017-01-27 09:03:45 +0100497 return -EINVAL;
498
Roland Dreier489beb92017-08-29 10:33:44 -0700499 down_write(&nvmf_transports_rwsem);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200500 list_add_tail(&ops->entry, &nvmf_transports);
Roland Dreier489beb92017-08-29 10:33:44 -0700501 up_write(&nvmf_transports_rwsem);
Johannes Thumshirne5a39dd2017-01-27 09:03:45 +0100502
503 return 0;
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200504}
505EXPORT_SYMBOL_GPL(nvmf_register_transport);
506
507/**
508 * nvmf_unregister_transport() - NVMe Fabrics Library unregistration function.
509 * @ops: Transport ops instance to be unregistered from the
510 * common fabrics library.
511 *
512 * Fabrics API function that unregisters the type of specific transport
513 * fabric being implemented from the common NVMe fabrics library.
514 * Part of the overall exit sequence of unloading the implemented driver.
515 */
516void nvmf_unregister_transport(struct nvmf_transport_ops *ops)
517{
Roland Dreier489beb92017-08-29 10:33:44 -0700518 down_write(&nvmf_transports_rwsem);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200519 list_del(&ops->entry);
Roland Dreier489beb92017-08-29 10:33:44 -0700520 up_write(&nvmf_transports_rwsem);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200521}
522EXPORT_SYMBOL_GPL(nvmf_unregister_transport);
523
524static struct nvmf_transport_ops *nvmf_lookup_transport(
525 struct nvmf_ctrl_options *opts)
526{
527 struct nvmf_transport_ops *ops;
528
Roland Dreier489beb92017-08-29 10:33:44 -0700529 lockdep_assert_held(&nvmf_transports_rwsem);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200530
531 list_for_each_entry(ops, &nvmf_transports, entry) {
532 if (strcmp(ops->name, opts->transport) == 0)
533 return ops;
534 }
535
536 return NULL;
537}
538
James Smartbb06ec312018-04-12 09:16:15 -0600539blk_status_t nvmf_check_if_ready(struct nvme_ctrl *ctrl, struct request *rq,
540 bool queue_live, bool is_connected)
541{
542 struct nvme_command *cmd = nvme_req(rq)->cmd;
543
544 if (likely(ctrl->state == NVME_CTRL_LIVE && is_connected))
545 return BLK_STS_OK;
546
547 switch (ctrl->state) {
James Smartbb06ec312018-04-12 09:16:15 -0600548 case NVME_CTRL_NEW:
549 case NVME_CTRL_CONNECTING:
Christoph Hellwigcc456b62018-05-25 15:41:54 +0200550 case NVME_CTRL_DELETING:
551 /*
552 * This is the case of starting a new or deleting an association
553 * but connectivity was lost before it was fully created or torn
554 * down. We need to error the commands used to initialize the
555 * controller so the reconnect can go into a retry attempt. The
556 * commands should all be marked REQ_FAILFAST_DRIVER, which will
557 * hit the reject path below. Anything else will be queued while
558 * the state settles.
559 */
James Smartbb06ec312018-04-12 09:16:15 -0600560 if (!is_connected)
Christoph Hellwigcc456b62018-05-25 15:41:54 +0200561 break;
James Smartbb06ec312018-04-12 09:16:15 -0600562
Christoph Hellwigcc456b62018-05-25 15:41:54 +0200563 /*
564 * If queue is live, allow only commands that are internally
565 * generated pass through. These are commands on the admin
566 * queue to initialize the controller. This will reject any
567 * ioctl admin cmds received while initializing.
568 */
569 if (queue_live && !(nvme_req(rq)->flags & NVME_REQ_USERCMD))
James Smartbb06ec312018-04-12 09:16:15 -0600570 return BLK_STS_OK;
571
572 /*
Christoph Hellwigcc456b62018-05-25 15:41:54 +0200573 * If the queue is not live, allow only a connect command. This
574 * will reject any ioctl admin cmd as well as initialization
575 * commands if the controller reverted the queue to non-live.
James Smartbb06ec312018-04-12 09:16:15 -0600576 */
Christoph Hellwigcc456b62018-05-25 15:41:54 +0200577 if (!queue_live && blk_rq_is_passthrough(rq) &&
578 cmd->common.opcode == nvme_fabrics_command &&
579 cmd->fabrics.fctype == nvme_fabrics_type_connect)
580 return BLK_STS_OK;
James Smartbb06ec312018-04-12 09:16:15 -0600581 break;
James Smartbb06ec312018-04-12 09:16:15 -0600582 default:
583 break;
584 }
585
James Smartbb06ec312018-04-12 09:16:15 -0600586 /*
Christoph Hellwigcc456b62018-05-25 15:41:54 +0200587 * Any other new io is something we're not in a state to send to the
588 * device. Default action is to busy it and retry it after the
589 * controller state is recovered. However, anything marked for failfast
590 * or nvme multipath is immediately failed. Note: commands used to
591 * initialize the controller will be marked for failfast.
James Smartbb06ec312018-04-12 09:16:15 -0600592 * Note: nvme cli/ioctl commands are marked for failfast.
593 */
594 if (!blk_noretry_request(rq) && !(rq->cmd_flags & REQ_NVME_MPATH))
595 return BLK_STS_RESOURCE;
James Smartbb06ec312018-04-12 09:16:15 -0600596 nvme_req(rq)->status = NVME_SC_ABORT_REQ;
597 return BLK_STS_IOERR;
598}
599EXPORT_SYMBOL_GPL(nvmf_check_if_ready);
600
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200601static const match_table_t opt_tokens = {
602 { NVMF_OPT_TRANSPORT, "transport=%s" },
603 { NVMF_OPT_TRADDR, "traddr=%s" },
604 { NVMF_OPT_TRSVCID, "trsvcid=%s" },
605 { NVMF_OPT_NQN, "nqn=%s" },
606 { NVMF_OPT_QUEUE_SIZE, "queue_size=%d" },
607 { NVMF_OPT_NR_IO_QUEUES, "nr_io_queues=%d" },
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200608 { NVMF_OPT_RECONNECT_DELAY, "reconnect_delay=%d" },
Sagi Grimberg42a45272017-03-18 20:52:36 +0200609 { NVMF_OPT_CTRL_LOSS_TMO, "ctrl_loss_tmo=%d" },
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200610 { NVMF_OPT_KATO, "keep_alive_tmo=%d" },
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200611 { NVMF_OPT_HOSTNQN, "hostnqn=%s" },
James Smart478bcb92016-08-02 10:42:10 +0300612 { NVMF_OPT_HOST_TRADDR, "host_traddr=%s" },
Johannes Thumshirn6bfe0422017-06-20 14:23:01 +0200613 { NVMF_OPT_HOST_ID, "hostid=%s" },
James Smart3b338762017-10-20 16:17:06 -0700614 { NVMF_OPT_DUP_CONNECT, "duplicate_connect" },
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200615 { NVMF_OPT_ERR, NULL }
616};
617
618static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
619 const char *buf)
620{
621 substring_t args[MAX_OPT_ARGS];
622 char *options, *o, *p;
623 int token, ret = 0;
624 size_t nqnlen = 0;
Sagi Grimberg42a45272017-03-18 20:52:36 +0200625 int ctrl_loss_tmo = NVMF_DEF_CTRL_LOSS_TMO;
Johannes Thumshirn6bfe0422017-06-20 14:23:01 +0200626 uuid_t hostid;
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200627
628 /* Set defaults */
629 opts->queue_size = NVMF_DEF_QUEUE_SIZE;
630 opts->nr_io_queues = num_online_cpus();
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200631 opts->reconnect_delay = NVMF_DEF_RECONNECT_DELAY;
Guilherme G. Piccoli8edd11c2017-09-14 13:59:28 -0300632 opts->kato = NVME_DEFAULT_KATO;
James Smart3b338762017-10-20 16:17:06 -0700633 opts->duplicate_connect = false;
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200634
635 options = o = kstrdup(buf, GFP_KERNEL);
636 if (!options)
637 return -ENOMEM;
638
Johannes Thumshirn6bfe0422017-06-20 14:23:01 +0200639 uuid_gen(&hostid);
640
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200641 while ((p = strsep(&o, ",\n")) != NULL) {
642 if (!*p)
643 continue;
644
645 token = match_token(p, opt_tokens, args);
646 opts->mask |= token;
647 switch (token) {
648 case NVMF_OPT_TRANSPORT:
649 p = match_strdup(args);
650 if (!p) {
651 ret = -ENOMEM;
652 goto out;
653 }
Chengguang Xu59a2f3f2018-04-14 20:06:19 +0800654 kfree(opts->transport);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200655 opts->transport = p;
656 break;
657 case NVMF_OPT_NQN:
658 p = match_strdup(args);
659 if (!p) {
660 ret = -ENOMEM;
661 goto out;
662 }
Chengguang Xu59a2f3f2018-04-14 20:06:19 +0800663 kfree(opts->subsysnqn);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200664 opts->subsysnqn = p;
665 nqnlen = strlen(opts->subsysnqn);
666 if (nqnlen >= NVMF_NQN_SIZE) {
667 pr_err("%s needs to be < %d bytes\n",
Bart Van Assche6eb72832016-10-18 13:10:24 -0700668 opts->subsysnqn, NVMF_NQN_SIZE);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200669 ret = -EINVAL;
670 goto out;
671 }
672 opts->discovery_nqn =
673 !(strcmp(opts->subsysnqn,
674 NVME_DISC_SUBSYS_NAME));
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200675 break;
676 case NVMF_OPT_TRADDR:
677 p = match_strdup(args);
678 if (!p) {
679 ret = -ENOMEM;
680 goto out;
681 }
Chengguang Xu59a2f3f2018-04-14 20:06:19 +0800682 kfree(opts->traddr);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200683 opts->traddr = p;
684 break;
685 case NVMF_OPT_TRSVCID:
686 p = match_strdup(args);
687 if (!p) {
688 ret = -ENOMEM;
689 goto out;
690 }
Chengguang Xu59a2f3f2018-04-14 20:06:19 +0800691 kfree(opts->trsvcid);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200692 opts->trsvcid = p;
693 break;
694 case NVMF_OPT_QUEUE_SIZE:
695 if (match_int(args, &token)) {
696 ret = -EINVAL;
697 goto out;
698 }
699 if (token < NVMF_MIN_QUEUE_SIZE ||
700 token > NVMF_MAX_QUEUE_SIZE) {
701 pr_err("Invalid queue_size %d\n", token);
702 ret = -EINVAL;
703 goto out;
704 }
705 opts->queue_size = token;
706 break;
707 case NVMF_OPT_NR_IO_QUEUES:
708 if (match_int(args, &token)) {
709 ret = -EINVAL;
710 goto out;
711 }
712 if (token <= 0) {
713 pr_err("Invalid number of IOQs %d\n", token);
714 ret = -EINVAL;
715 goto out;
716 }
Roland Dreier04758212018-03-05 11:59:53 -0800717 if (opts->discovery_nqn) {
718 pr_debug("Ignoring nr_io_queues value for discovery controller\n");
719 break;
720 }
721
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200722 opts->nr_io_queues = min_t(unsigned int,
723 num_online_cpus(), token);
724 break;
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200725 case NVMF_OPT_KATO:
726 if (match_int(args, &token)) {
727 ret = -EINVAL;
728 goto out;
729 }
730
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200731 if (token < 0) {
732 pr_err("Invalid keep_alive_tmo %d\n", token);
733 ret = -EINVAL;
734 goto out;
Guilherme G. Piccoli8edd11c2017-09-14 13:59:28 -0300735 } else if (token == 0 && !opts->discovery_nqn) {
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200736 /* Allowed for debug */
737 pr_warn("keep_alive_tmo 0 won't execute keep alives!!!\n");
738 }
739 opts->kato = token;
Guilherme G. Piccoli8edd11c2017-09-14 13:59:28 -0300740
741 if (opts->discovery_nqn && opts->kato) {
742 pr_err("Discovery controllers cannot accept KATO != 0\n");
743 ret = -EINVAL;
744 goto out;
745 }
746
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200747 break;
Sagi Grimberg42a45272017-03-18 20:52:36 +0200748 case NVMF_OPT_CTRL_LOSS_TMO:
749 if (match_int(args, &token)) {
750 ret = -EINVAL;
751 goto out;
752 }
753
754 if (token < 0)
755 pr_warn("ctrl_loss_tmo < 0 will reconnect forever\n");
756 ctrl_loss_tmo = token;
757 break;
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200758 case NVMF_OPT_HOSTNQN:
759 if (opts->host) {
760 pr_err("hostnqn already user-assigned: %s\n",
761 opts->host->nqn);
762 ret = -EADDRINUSE;
763 goto out;
764 }
765 p = match_strdup(args);
766 if (!p) {
767 ret = -ENOMEM;
768 goto out;
769 }
770 nqnlen = strlen(p);
771 if (nqnlen >= NVMF_NQN_SIZE) {
772 pr_err("%s needs to be < %d bytes\n",
773 p, NVMF_NQN_SIZE);
Bart Van Assche8eadfcb2016-10-18 13:10:46 -0700774 kfree(p);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200775 ret = -EINVAL;
776 goto out;
777 }
Chengguang Xu59a2f3f2018-04-14 20:06:19 +0800778 nvmf_host_put(opts->host);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200779 opts->host = nvmf_host_add(p);
Bart Van Assche8eadfcb2016-10-18 13:10:46 -0700780 kfree(p);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200781 if (!opts->host) {
782 ret = -ENOMEM;
783 goto out;
784 }
785 break;
786 case NVMF_OPT_RECONNECT_DELAY:
787 if (match_int(args, &token)) {
788 ret = -EINVAL;
789 goto out;
790 }
791 if (token <= 0) {
792 pr_err("Invalid reconnect_delay %d\n", token);
793 ret = -EINVAL;
794 goto out;
795 }
796 opts->reconnect_delay = token;
797 break;
James Smart478bcb92016-08-02 10:42:10 +0300798 case NVMF_OPT_HOST_TRADDR:
799 p = match_strdup(args);
800 if (!p) {
801 ret = -ENOMEM;
802 goto out;
803 }
Chengguang Xu59a2f3f2018-04-14 20:06:19 +0800804 kfree(opts->host_traddr);
James Smart478bcb92016-08-02 10:42:10 +0300805 opts->host_traddr = p;
806 break;
Johannes Thumshirn6bfe0422017-06-20 14:23:01 +0200807 case NVMF_OPT_HOST_ID:
808 p = match_strdup(args);
809 if (!p) {
810 ret = -ENOMEM;
811 goto out;
812 }
Roland Dreierdf351ef2018-01-11 13:38:00 -0800813 ret = uuid_parse(p, &hostid);
Roland Dreierdf351ef2018-01-11 13:38:00 -0800814 if (ret) {
Guan Junxiong9b483da2017-08-03 21:40:26 +0800815 pr_err("Invalid hostid %s\n", p);
Johannes Thumshirn6bfe0422017-06-20 14:23:01 +0200816 ret = -EINVAL;
Johannes Thumshirn6e494122018-01-25 09:09:25 +0100817 kfree(p);
Johannes Thumshirn6bfe0422017-06-20 14:23:01 +0200818 goto out;
819 }
Johannes Thumshirn6e494122018-01-25 09:09:25 +0100820 kfree(p);
Johannes Thumshirn6bfe0422017-06-20 14:23:01 +0200821 break;
James Smart3b338762017-10-20 16:17:06 -0700822 case NVMF_OPT_DUP_CONNECT:
823 opts->duplicate_connect = true;
824 break;
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200825 default:
826 pr_warn("unknown parameter or missing value '%s' in ctrl creation request\n",
827 p);
828 ret = -EINVAL;
829 goto out;
830 }
831 }
832
Hannes Reinecke461fbc82018-05-24 16:18:15 +0200833 if (opts->discovery_nqn) {
834 opts->kato = 0;
835 opts->nr_io_queues = 0;
Hannes Reinecke181303d2018-05-24 16:18:17 +0200836 opts->duplicate_connect = true;
Hannes Reinecke461fbc82018-05-24 16:18:15 +0200837 }
Sagi Grimberg42a45272017-03-18 20:52:36 +0200838 if (ctrl_loss_tmo < 0)
839 opts->max_reconnects = -1;
840 else
841 opts->max_reconnects = DIV_ROUND_UP(ctrl_loss_tmo,
842 opts->reconnect_delay);
843
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200844 if (!opts->host) {
845 kref_get(&nvmf_default_host->ref);
846 opts->host = nvmf_default_host;
847 }
848
Johannes Thumshirn6bfe0422017-06-20 14:23:01 +0200849 uuid_copy(&opts->host->id, &hostid);
850
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200851out:
852 kfree(options);
853 return ret;
854}
855
856static int nvmf_check_required_opts(struct nvmf_ctrl_options *opts,
857 unsigned int required_opts)
858{
859 if ((opts->mask & required_opts) != required_opts) {
860 int i;
861
862 for (i = 0; i < ARRAY_SIZE(opt_tokens); i++) {
863 if ((opt_tokens[i].token & required_opts) &&
864 !(opt_tokens[i].token & opts->mask)) {
865 pr_warn("missing parameter '%s'\n",
866 opt_tokens[i].pattern);
867 }
868 }
869
870 return -EINVAL;
871 }
872
873 return 0;
874}
875
876static int nvmf_check_allowed_opts(struct nvmf_ctrl_options *opts,
877 unsigned int allowed_opts)
878{
879 if (opts->mask & ~allowed_opts) {
880 int i;
881
882 for (i = 0; i < ARRAY_SIZE(opt_tokens); i++) {
Christoph Hellwig81a0b8d2017-08-17 13:57:49 +0200883 if ((opt_tokens[i].token & opts->mask) &&
884 (opt_tokens[i].token & ~allowed_opts)) {
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200885 pr_warn("invalid parameter '%s'\n",
886 opt_tokens[i].pattern);
887 }
888 }
889
890 return -EINVAL;
891 }
892
893 return 0;
894}
895
896void nvmf_free_options(struct nvmf_ctrl_options *opts)
897{
898 nvmf_host_put(opts->host);
899 kfree(opts->transport);
900 kfree(opts->traddr);
901 kfree(opts->trsvcid);
902 kfree(opts->subsysnqn);
James Smart478bcb92016-08-02 10:42:10 +0300903 kfree(opts->host_traddr);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200904 kfree(opts);
905}
906EXPORT_SYMBOL_GPL(nvmf_free_options);
907
908#define NVMF_REQUIRED_OPTS (NVMF_OPT_TRANSPORT | NVMF_OPT_NQN)
909#define NVMF_ALLOWED_OPTS (NVMF_OPT_QUEUE_SIZE | NVMF_OPT_NR_IO_QUEUES | \
Johannes Thumshirn6bfe0422017-06-20 14:23:01 +0200910 NVMF_OPT_KATO | NVMF_OPT_HOSTNQN | \
James Smart3b338762017-10-20 16:17:06 -0700911 NVMF_OPT_HOST_ID | NVMF_OPT_DUP_CONNECT)
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200912
913static struct nvme_ctrl *
914nvmf_create_ctrl(struct device *dev, const char *buf, size_t count)
915{
916 struct nvmf_ctrl_options *opts;
917 struct nvmf_transport_ops *ops;
918 struct nvme_ctrl *ctrl;
919 int ret;
920
921 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
922 if (!opts)
923 return ERR_PTR(-ENOMEM);
924
925 ret = nvmf_parse_options(opts, buf);
926 if (ret)
927 goto out_free_opts;
928
Sagi Grimbergd1f10712017-09-24 16:15:55 +0300929
930 request_module("nvme-%s", opts->transport);
931
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200932 /*
933 * Check the generic options first as we need a valid transport for
934 * the lookup below. Then clear the generic flags so that transport
935 * drivers don't have to care about them.
936 */
937 ret = nvmf_check_required_opts(opts, NVMF_REQUIRED_OPTS);
938 if (ret)
939 goto out_free_opts;
940 opts->mask &= ~NVMF_REQUIRED_OPTS;
941
Roland Dreier489beb92017-08-29 10:33:44 -0700942 down_read(&nvmf_transports_rwsem);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200943 ops = nvmf_lookup_transport(opts);
944 if (!ops) {
945 pr_info("no handler found for transport %s.\n",
946 opts->transport);
947 ret = -EINVAL;
948 goto out_unlock;
949 }
950
Roy Shterman0de5cd32017-12-25 14:18:30 +0200951 if (!try_module_get(ops->module)) {
952 ret = -EBUSY;
953 goto out_unlock;
954 }
Johannes Thumshirn12a0b662018-06-01 09:11:20 +0200955 up_read(&nvmf_transports_rwsem);
Roy Shterman0de5cd32017-12-25 14:18:30 +0200956
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200957 ret = nvmf_check_required_opts(opts, ops->required_opts);
958 if (ret)
Roy Shterman0de5cd32017-12-25 14:18:30 +0200959 goto out_module_put;
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200960 ret = nvmf_check_allowed_opts(opts, NVMF_ALLOWED_OPTS |
961 ops->allowed_opts | ops->required_opts);
962 if (ret)
Roy Shterman0de5cd32017-12-25 14:18:30 +0200963 goto out_module_put;
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200964
965 ctrl = ops->create_ctrl(dev, opts);
966 if (IS_ERR(ctrl)) {
967 ret = PTR_ERR(ctrl);
Roy Shterman0de5cd32017-12-25 14:18:30 +0200968 goto out_module_put;
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200969 }
970
Roy Shterman0de5cd32017-12-25 14:18:30 +0200971 module_put(ops->module);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200972 return ctrl;
973
Roy Shterman0de5cd32017-12-25 14:18:30 +0200974out_module_put:
975 module_put(ops->module);
Johannes Thumshirn12a0b662018-06-01 09:11:20 +0200976 goto out_free_opts;
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200977out_unlock:
Roland Dreier489beb92017-08-29 10:33:44 -0700978 up_read(&nvmf_transports_rwsem);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200979out_free_opts:
Bart Van Asschef3116d82016-10-18 13:11:03 -0700980 nvmf_free_options(opts);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200981 return ERR_PTR(ret);
982}
983
984static struct class *nvmf_class;
985static struct device *nvmf_device;
986static DEFINE_MUTEX(nvmf_dev_mutex);
987
988static ssize_t nvmf_dev_write(struct file *file, const char __user *ubuf,
989 size_t count, loff_t *pos)
990{
991 struct seq_file *seq_file = file->private_data;
992 struct nvme_ctrl *ctrl;
993 const char *buf;
994 int ret = 0;
995
996 if (count > PAGE_SIZE)
997 return -ENOMEM;
998
999 buf = memdup_user_nul(ubuf, count);
1000 if (IS_ERR(buf))
1001 return PTR_ERR(buf);
1002
1003 mutex_lock(&nvmf_dev_mutex);
1004 if (seq_file->private) {
1005 ret = -EINVAL;
1006 goto out_unlock;
1007 }
1008
1009 ctrl = nvmf_create_ctrl(nvmf_device, buf, count);
1010 if (IS_ERR(ctrl)) {
1011 ret = PTR_ERR(ctrl);
1012 goto out_unlock;
1013 }
1014
1015 seq_file->private = ctrl;
1016
1017out_unlock:
1018 mutex_unlock(&nvmf_dev_mutex);
1019 kfree(buf);
1020 return ret ? ret : count;
1021}
1022
1023static int nvmf_dev_show(struct seq_file *seq_file, void *private)
1024{
1025 struct nvme_ctrl *ctrl;
1026 int ret = 0;
1027
1028 mutex_lock(&nvmf_dev_mutex);
1029 ctrl = seq_file->private;
1030 if (!ctrl) {
1031 ret = -EINVAL;
1032 goto out_unlock;
1033 }
1034
1035 seq_printf(seq_file, "instance=%d,cntlid=%d\n",
1036 ctrl->instance, ctrl->cntlid);
1037
1038out_unlock:
1039 mutex_unlock(&nvmf_dev_mutex);
1040 return ret;
1041}
1042
1043static int nvmf_dev_open(struct inode *inode, struct file *file)
1044{
1045 /*
1046 * The miscdevice code initializes file->private_data, but doesn't
1047 * make use of it later.
1048 */
1049 file->private_data = NULL;
1050 return single_open(file, nvmf_dev_show, NULL);
1051}
1052
1053static int nvmf_dev_release(struct inode *inode, struct file *file)
1054{
1055 struct seq_file *seq_file = file->private_data;
1056 struct nvme_ctrl *ctrl = seq_file->private;
1057
1058 if (ctrl)
1059 nvme_put_ctrl(ctrl);
1060 return single_release(inode, file);
1061}
1062
1063static const struct file_operations nvmf_dev_fops = {
1064 .owner = THIS_MODULE,
1065 .write = nvmf_dev_write,
1066 .read = seq_read,
1067 .open = nvmf_dev_open,
1068 .release = nvmf_dev_release,
1069};
1070
1071static struct miscdevice nvmf_misc = {
1072 .minor = MISC_DYNAMIC_MINOR,
1073 .name = "nvme-fabrics",
1074 .fops = &nvmf_dev_fops,
1075};
1076
1077static int __init nvmf_init(void)
1078{
1079 int ret;
1080
1081 nvmf_default_host = nvmf_host_default();
1082 if (!nvmf_default_host)
1083 return -ENOMEM;
1084
1085 nvmf_class = class_create(THIS_MODULE, "nvme-fabrics");
1086 if (IS_ERR(nvmf_class)) {
1087 pr_err("couldn't register class nvme-fabrics\n");
1088 ret = PTR_ERR(nvmf_class);
1089 goto out_free_host;
1090 }
1091
1092 nvmf_device =
1093 device_create(nvmf_class, NULL, MKDEV(0, 0), NULL, "ctl");
1094 if (IS_ERR(nvmf_device)) {
1095 pr_err("couldn't create nvme-fabris device!\n");
1096 ret = PTR_ERR(nvmf_device);
1097 goto out_destroy_class;
1098 }
1099
1100 ret = misc_register(&nvmf_misc);
1101 if (ret) {
1102 pr_err("couldn't register misc device: %d\n", ret);
1103 goto out_destroy_device;
1104 }
1105
1106 return 0;
1107
1108out_destroy_device:
1109 device_destroy(nvmf_class, MKDEV(0, 0));
1110out_destroy_class:
1111 class_destroy(nvmf_class);
1112out_free_host:
1113 nvmf_host_put(nvmf_default_host);
1114 return ret;
1115}
1116
1117static void __exit nvmf_exit(void)
1118{
1119 misc_deregister(&nvmf_misc);
1120 device_destroy(nvmf_class, MKDEV(0, 0));
1121 class_destroy(nvmf_class);
1122 nvmf_host_put(nvmf_default_host);
1123
1124 BUILD_BUG_ON(sizeof(struct nvmf_connect_command) != 64);
1125 BUILD_BUG_ON(sizeof(struct nvmf_property_get_command) != 64);
1126 BUILD_BUG_ON(sizeof(struct nvmf_property_set_command) != 64);
1127 BUILD_BUG_ON(sizeof(struct nvmf_connect_data) != 1024);
1128}
1129
1130MODULE_LICENSE("GPL v2");
1131
1132module_init(nvmf_init);
1133module_exit(nvmf_exit);