]>
Commit | Line | Data |
---|---|---|
07bfcd09 CH |
1 | /* |
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 | ||
24 | static LIST_HEAD(nvmf_transports); | |
489beb91 | 25 | static DECLARE_RWSEM(nvmf_transports_rwsem); |
07bfcd09 CH |
26 | |
27 | static LIST_HEAD(nvmf_hosts); | |
28 | static DEFINE_MUTEX(nvmf_hosts_mutex); | |
29 | ||
30 | static struct nvmf_host *nvmf_default_host; | |
31 | ||
32 | static 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 | ||
44 | static 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); | |
98096d8a CH |
50 | if (host) { |
51 | kref_get(&host->ref); | |
07bfcd09 | 52 | goto out_unlock; |
98096d8a | 53 | } |
07bfcd09 CH |
54 | |
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); | |
07bfcd09 CH |
61 | |
62 | list_add_tail(&host->list, &nvmf_hosts); | |
63 | out_unlock: | |
64 | mutex_unlock(&nvmf_hosts_mutex); | |
65 | return host; | |
66 | } | |
67 | ||
68 | static 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); | |
6b018235 | 77 | uuid_gen(&host->id); |
07bfcd09 | 78 | snprintf(host->nqn, NVMF_NQN_SIZE, |
40a5fce4 | 79 | "nqn.2014-08.org.nvmexpress:uuid:%pUb", &host->id); |
07bfcd09 CH |
80 | |
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 | ||
88 | static void nvmf_host_destroy(struct kref *ref) | |
89 | { | |
90 | struct nvmf_host *host = container_of(ref, struct nvmf_host, ref); | |
91 | ||
e76debd9 ML |
92 | mutex_lock(&nvmf_hosts_mutex); |
93 | list_del(&host->list); | |
94 | mutex_unlock(&nvmf_hosts_mutex); | |
95 | ||
07bfcd09 CH |
96 | kfree(host); |
97 | } | |
98 | ||
99 | static 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 | */ | |
111 | int nvmf_get_address(struct nvme_ctrl *ctrl, char *buf, int size) | |
112 | { | |
0fe51ff2 JS |
113 | 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); | |
478bcb93 JS |
120 | if (ctrl->opts->mask & NVMF_OPT_HOST_TRADDR) |
121 | len += snprintf(buf + len, size - len, "%shost_traddr=%s", | |
122 | (len) ? "," : "", ctrl->opts->host_traddr); | |
0fe51ff2 JS |
123 | len += snprintf(buf + len, size - len, "\n"); |
124 | ||
125 | return len; | |
07bfcd09 CH |
126 | } |
127 | EXPORT_SYMBOL_GPL(nvmf_get_address); | |
128 | ||
07bfcd09 CH |
129 | /** |
130 | * 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 | */ | |
150 | int nvmf_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val) | |
151 | { | |
152 | struct nvme_command cmd; | |
d49187e9 | 153 | union nvme_result res; |
07bfcd09 CH |
154 | 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 | ||
d49187e9 | 161 | ret = __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, &res, NULL, 0, 0, |
07bfcd09 CH |
162 | NVME_QID_ANY, 0, 0); |
163 | ||
164 | if (ret >= 0) | |
d49187e9 | 165 | *val = le64_to_cpu(res.u64); |
07bfcd09 CH |
166 | 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 | } | |
173 | EXPORT_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 | */ | |
196 | int nvmf_reg_read64(struct nvme_ctrl *ctrl, u32 off, u64 *val) | |
197 | { | |
198 | struct nvme_command cmd; | |
d49187e9 | 199 | union nvme_result res; |
07bfcd09 CH |
200 | 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 | ||
d49187e9 | 208 | ret = __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, &res, NULL, 0, 0, |
07bfcd09 CH |
209 | NVME_QID_ANY, 0, 0); |
210 | ||
211 | if (ret >= 0) | |
d49187e9 | 212 | *val = le64_to_cpu(res.u64); |
07bfcd09 CH |
213 | 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 | } | |
219 | EXPORT_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 | */ | |
242 | int 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 | } | |
262 | EXPORT_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 | */ | |
279 | static 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; | |
97ddc36e GJ |
329 | |
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 | ||
07bfcd09 CH |
347 | 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 | */ | |
375 | int nvmf_connect_admin_queue(struct nvme_ctrl *ctrl) | |
376 | { | |
377 | struct nvme_command cmd; | |
d49187e9 | 378 | union nvme_result res; |
07bfcd09 CH |
379 | 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; | |
7aa1f427 | 386 | cmd.connect.sqsize = cpu_to_le16(NVME_AQ_DEPTH - 1); |
f994d9dc | 387 | |
038bd4cb SG |
388 | /* |
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); | |
07bfcd09 CH |
394 | |
395 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
396 | if (!data) | |
397 | return -ENOMEM; | |
398 | ||
8e412263 | 399 | uuid_copy(&data->hostid, &ctrl->opts->host->id); |
07bfcd09 CH |
400 | 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 | ||
d49187e9 | 404 | ret = __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, &res, |
07bfcd09 CH |
405 | data, sizeof(*data), 0, NVME_QID_ANY, 1, |
406 | BLK_MQ_REQ_RESERVED | BLK_MQ_REQ_NOWAIT); | |
407 | if (ret) { | |
d49187e9 | 408 | nvmf_log_connect_error(ctrl, ret, le32_to_cpu(res.u32), |
07bfcd09 CH |
409 | &cmd, data); |
410 | goto out_free_data; | |
411 | } | |
412 | ||
d49187e9 | 413 | ctrl->cntlid = le16_to_cpu(res.u16); |
07bfcd09 CH |
414 | |
415 | out_free_data: | |
416 | kfree(data); | |
417 | return ret; | |
418 | } | |
419 | EXPORT_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 | */ | |
441 | int nvmf_connect_io_queue(struct nvme_ctrl *ctrl, u16 qid) | |
442 | { | |
443 | struct nvme_command cmd; | |
444 | struct nvmf_connect_data *data; | |
d49187e9 | 445 | union nvme_result res; |
07bfcd09 CH |
446 | 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 | ||
8e412263 | 458 | uuid_copy(&data->hostid, &ctrl->opts->host->id); |
07bfcd09 CH |
459 | 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 | ||
d49187e9 | 463 | ret = __nvme_submit_sync_cmd(ctrl->connect_q, &cmd, &res, |
07bfcd09 CH |
464 | data, sizeof(*data), 0, qid, 1, |
465 | BLK_MQ_REQ_RESERVED | BLK_MQ_REQ_NOWAIT); | |
466 | if (ret) { | |
d49187e9 | 467 | nvmf_log_connect_error(ctrl, ret, le32_to_cpu(res.u32), |
07bfcd09 CH |
468 | &cmd, data); |
469 | } | |
470 | kfree(data); | |
471 | return ret; | |
472 | } | |
473 | EXPORT_SYMBOL_GPL(nvmf_connect_io_queue); | |
474 | ||
42a45274 SG |
475 | bool nvmf_should_reconnect(struct nvme_ctrl *ctrl) |
476 | { | |
477 | if (ctrl->opts->max_reconnects != -1 && | |
fdf9dfa8 | 478 | ctrl->nr_reconnects < ctrl->opts->max_reconnects) |
42a45274 SG |
479 | return true; |
480 | ||
481 | return false; | |
482 | } | |
483 | EXPORT_SYMBOL_GPL(nvmf_should_reconnect); | |
484 | ||
07bfcd09 CH |
485 | /** |
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 | */ | |
e5a39dd8 | 494 | int nvmf_register_transport(struct nvmf_transport_ops *ops) |
07bfcd09 | 495 | { |
e5a39dd8 JT |
496 | if (!ops->create_ctrl) |
497 | return -EINVAL; | |
498 | ||
489beb91 | 499 | down_write(&nvmf_transports_rwsem); |
07bfcd09 | 500 | list_add_tail(&ops->entry, &nvmf_transports); |
489beb91 | 501 | up_write(&nvmf_transports_rwsem); |
e5a39dd8 JT |
502 | |
503 | return 0; | |
07bfcd09 CH |
504 | } |
505 | EXPORT_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 | */ | |
516 | void nvmf_unregister_transport(struct nvmf_transport_ops *ops) | |
517 | { | |
489beb91 | 518 | down_write(&nvmf_transports_rwsem); |
07bfcd09 | 519 | list_del(&ops->entry); |
489beb91 | 520 | up_write(&nvmf_transports_rwsem); |
07bfcd09 CH |
521 | } |
522 | EXPORT_SYMBOL_GPL(nvmf_unregister_transport); | |
523 | ||
524 | static struct nvmf_transport_ops *nvmf_lookup_transport( | |
525 | struct nvmf_ctrl_options *opts) | |
526 | { | |
527 | struct nvmf_transport_ops *ops; | |
528 | ||
489beb91 | 529 | lockdep_assert_held(&nvmf_transports_rwsem); |
07bfcd09 CH |
530 | |
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 | ||
539 | static const match_table_t opt_tokens = { | |
540 | { NVMF_OPT_TRANSPORT, "transport=%s" }, | |
541 | { NVMF_OPT_TRADDR, "traddr=%s" }, | |
542 | { NVMF_OPT_TRSVCID, "trsvcid=%s" }, | |
543 | { NVMF_OPT_NQN, "nqn=%s" }, | |
544 | { NVMF_OPT_QUEUE_SIZE, "queue_size=%d" }, | |
545 | { NVMF_OPT_NR_IO_QUEUES, "nr_io_queues=%d" }, | |
07bfcd09 | 546 | { NVMF_OPT_RECONNECT_DELAY, "reconnect_delay=%d" }, |
42a45274 | 547 | { NVMF_OPT_CTRL_LOSS_TMO, "ctrl_loss_tmo=%d" }, |
038bd4cb | 548 | { NVMF_OPT_KATO, "keep_alive_tmo=%d" }, |
07bfcd09 | 549 | { NVMF_OPT_HOSTNQN, "hostnqn=%s" }, |
478bcb93 | 550 | { NVMF_OPT_HOST_TRADDR, "host_traddr=%s" }, |
6bfe0425 | 551 | { NVMF_OPT_HOST_ID, "hostid=%s" }, |
3b338762 | 552 | { NVMF_OPT_DUP_CONNECT, "duplicate_connect" }, |
07bfcd09 CH |
553 | { NVMF_OPT_ERR, NULL } |
554 | }; | |
555 | ||
556 | static int nvmf_parse_options(struct nvmf_ctrl_options *opts, | |
557 | const char *buf) | |
558 | { | |
559 | substring_t args[MAX_OPT_ARGS]; | |
560 | char *options, *o, *p; | |
561 | int token, ret = 0; | |
562 | size_t nqnlen = 0; | |
42a45274 | 563 | int ctrl_loss_tmo = NVMF_DEF_CTRL_LOSS_TMO; |
6bfe0425 | 564 | uuid_t hostid; |
07bfcd09 CH |
565 | |
566 | /* Set defaults */ | |
567 | opts->queue_size = NVMF_DEF_QUEUE_SIZE; | |
568 | opts->nr_io_queues = num_online_cpus(); | |
07bfcd09 | 569 | opts->reconnect_delay = NVMF_DEF_RECONNECT_DELAY; |
8edd11c9 | 570 | opts->kato = NVME_DEFAULT_KATO; |
3b338762 | 571 | opts->duplicate_connect = false; |
07bfcd09 CH |
572 | |
573 | options = o = kstrdup(buf, GFP_KERNEL); | |
574 | if (!options) | |
575 | return -ENOMEM; | |
576 | ||
6bfe0425 JT |
577 | uuid_gen(&hostid); |
578 | ||
07bfcd09 CH |
579 | while ((p = strsep(&o, ",\n")) != NULL) { |
580 | if (!*p) | |
581 | continue; | |
582 | ||
583 | token = match_token(p, opt_tokens, args); | |
584 | opts->mask |= token; | |
585 | switch (token) { | |
586 | case NVMF_OPT_TRANSPORT: | |
587 | p = match_strdup(args); | |
588 | if (!p) { | |
589 | ret = -ENOMEM; | |
590 | goto out; | |
591 | } | |
592 | opts->transport = p; | |
593 | break; | |
594 | case NVMF_OPT_NQN: | |
595 | p = match_strdup(args); | |
596 | if (!p) { | |
597 | ret = -ENOMEM; | |
598 | goto out; | |
599 | } | |
600 | opts->subsysnqn = p; | |
601 | nqnlen = strlen(opts->subsysnqn); | |
602 | if (nqnlen >= NVMF_NQN_SIZE) { | |
603 | pr_err("%s needs to be < %d bytes\n", | |
6eb72830 | 604 | opts->subsysnqn, NVMF_NQN_SIZE); |
07bfcd09 CH |
605 | ret = -EINVAL; |
606 | goto out; | |
607 | } | |
608 | opts->discovery_nqn = | |
609 | !(strcmp(opts->subsysnqn, | |
610 | NVME_DISC_SUBSYS_NAME)); | |
611 | if (opts->discovery_nqn) | |
612 | opts->nr_io_queues = 0; | |
613 | break; | |
614 | case NVMF_OPT_TRADDR: | |
615 | p = match_strdup(args); | |
616 | if (!p) { | |
617 | ret = -ENOMEM; | |
618 | goto out; | |
619 | } | |
620 | opts->traddr = p; | |
621 | break; | |
622 | case NVMF_OPT_TRSVCID: | |
623 | p = match_strdup(args); | |
624 | if (!p) { | |
625 | ret = -ENOMEM; | |
626 | goto out; | |
627 | } | |
628 | opts->trsvcid = p; | |
629 | break; | |
630 | case NVMF_OPT_QUEUE_SIZE: | |
631 | if (match_int(args, &token)) { | |
632 | ret = -EINVAL; | |
633 | goto out; | |
634 | } | |
635 | if (token < NVMF_MIN_QUEUE_SIZE || | |
636 | token > NVMF_MAX_QUEUE_SIZE) { | |
637 | pr_err("Invalid queue_size %d\n", token); | |
638 | ret = -EINVAL; | |
639 | goto out; | |
640 | } | |
641 | opts->queue_size = token; | |
642 | break; | |
643 | case NVMF_OPT_NR_IO_QUEUES: | |
644 | if (match_int(args, &token)) { | |
645 | ret = -EINVAL; | |
646 | goto out; | |
647 | } | |
648 | if (token <= 0) { | |
649 | pr_err("Invalid number of IOQs %d\n", token); | |
650 | ret = -EINVAL; | |
651 | goto out; | |
652 | } | |
653 | opts->nr_io_queues = min_t(unsigned int, | |
654 | num_online_cpus(), token); | |
655 | break; | |
038bd4cb SG |
656 | case NVMF_OPT_KATO: |
657 | if (match_int(args, &token)) { | |
658 | ret = -EINVAL; | |
659 | goto out; | |
660 | } | |
661 | ||
038bd4cb SG |
662 | if (token < 0) { |
663 | pr_err("Invalid keep_alive_tmo %d\n", token); | |
664 | ret = -EINVAL; | |
665 | goto out; | |
8edd11c9 | 666 | } else if (token == 0 && !opts->discovery_nqn) { |
038bd4cb SG |
667 | /* Allowed for debug */ |
668 | pr_warn("keep_alive_tmo 0 won't execute keep alives!!!\n"); | |
669 | } | |
670 | opts->kato = token; | |
8edd11c9 GP |
671 | |
672 | if (opts->discovery_nqn && opts->kato) { | |
673 | pr_err("Discovery controllers cannot accept KATO != 0\n"); | |
674 | ret = -EINVAL; | |
675 | goto out; | |
676 | } | |
677 | ||
038bd4cb | 678 | break; |
42a45274 SG |
679 | case NVMF_OPT_CTRL_LOSS_TMO: |
680 | if (match_int(args, &token)) { | |
681 | ret = -EINVAL; | |
682 | goto out; | |
683 | } | |
684 | ||
685 | if (token < 0) | |
686 | pr_warn("ctrl_loss_tmo < 0 will reconnect forever\n"); | |
687 | ctrl_loss_tmo = token; | |
688 | break; | |
07bfcd09 CH |
689 | case NVMF_OPT_HOSTNQN: |
690 | if (opts->host) { | |
691 | pr_err("hostnqn already user-assigned: %s\n", | |
692 | opts->host->nqn); | |
693 | ret = -EADDRINUSE; | |
694 | goto out; | |
695 | } | |
696 | p = match_strdup(args); | |
697 | if (!p) { | |
698 | ret = -ENOMEM; | |
699 | goto out; | |
700 | } | |
701 | nqnlen = strlen(p); | |
702 | if (nqnlen >= NVMF_NQN_SIZE) { | |
703 | pr_err("%s needs to be < %d bytes\n", | |
704 | p, NVMF_NQN_SIZE); | |
8eadfcb1 | 705 | kfree(p); |
07bfcd09 CH |
706 | ret = -EINVAL; |
707 | goto out; | |
708 | } | |
709 | opts->host = nvmf_host_add(p); | |
8eadfcb1 | 710 | kfree(p); |
07bfcd09 CH |
711 | if (!opts->host) { |
712 | ret = -ENOMEM; | |
713 | goto out; | |
714 | } | |
715 | break; | |
716 | case NVMF_OPT_RECONNECT_DELAY: | |
717 | if (match_int(args, &token)) { | |
718 | ret = -EINVAL; | |
719 | goto out; | |
720 | } | |
721 | if (token <= 0) { | |
722 | pr_err("Invalid reconnect_delay %d\n", token); | |
723 | ret = -EINVAL; | |
724 | goto out; | |
725 | } | |
726 | opts->reconnect_delay = token; | |
727 | break; | |
478bcb93 JS |
728 | case NVMF_OPT_HOST_TRADDR: |
729 | p = match_strdup(args); | |
730 | if (!p) { | |
731 | ret = -ENOMEM; | |
732 | goto out; | |
733 | } | |
734 | opts->host_traddr = p; | |
735 | break; | |
6bfe0425 JT |
736 | case NVMF_OPT_HOST_ID: |
737 | p = match_strdup(args); | |
738 | if (!p) { | |
739 | ret = -ENOMEM; | |
740 | goto out; | |
741 | } | |
742 | if (uuid_parse(p, &hostid)) { | |
9b483da1 | 743 | pr_err("Invalid hostid %s\n", p); |
6bfe0425 JT |
744 | ret = -EINVAL; |
745 | goto out; | |
746 | } | |
747 | break; | |
3b338762 JS |
748 | case NVMF_OPT_DUP_CONNECT: |
749 | opts->duplicate_connect = true; | |
750 | break; | |
07bfcd09 CH |
751 | default: |
752 | pr_warn("unknown parameter or missing value '%s' in ctrl creation request\n", | |
753 | p); | |
754 | ret = -EINVAL; | |
755 | goto out; | |
756 | } | |
757 | } | |
758 | ||
42a45274 SG |
759 | if (ctrl_loss_tmo < 0) |
760 | opts->max_reconnects = -1; | |
761 | else | |
762 | opts->max_reconnects = DIV_ROUND_UP(ctrl_loss_tmo, | |
763 | opts->reconnect_delay); | |
764 | ||
07bfcd09 CH |
765 | if (!opts->host) { |
766 | kref_get(&nvmf_default_host->ref); | |
767 | opts->host = nvmf_default_host; | |
768 | } | |
769 | ||
6bfe0425 JT |
770 | uuid_copy(&opts->host->id, &hostid); |
771 | ||
07bfcd09 CH |
772 | out: |
773 | kfree(options); | |
774 | return ret; | |
775 | } | |
776 | ||
777 | static int nvmf_check_required_opts(struct nvmf_ctrl_options *opts, | |
778 | unsigned int required_opts) | |
779 | { | |
780 | if ((opts->mask & required_opts) != required_opts) { | |
781 | int i; | |
782 | ||
783 | for (i = 0; i < ARRAY_SIZE(opt_tokens); i++) { | |
784 | if ((opt_tokens[i].token & required_opts) && | |
785 | !(opt_tokens[i].token & opts->mask)) { | |
786 | pr_warn("missing parameter '%s'\n", | |
787 | opt_tokens[i].pattern); | |
788 | } | |
789 | } | |
790 | ||
791 | return -EINVAL; | |
792 | } | |
793 | ||
794 | return 0; | |
795 | } | |
796 | ||
797 | static int nvmf_check_allowed_opts(struct nvmf_ctrl_options *opts, | |
798 | unsigned int allowed_opts) | |
799 | { | |
800 | if (opts->mask & ~allowed_opts) { | |
801 | int i; | |
802 | ||
803 | for (i = 0; i < ARRAY_SIZE(opt_tokens); i++) { | |
81a0b8d7 CH |
804 | if ((opt_tokens[i].token & opts->mask) && |
805 | (opt_tokens[i].token & ~allowed_opts)) { | |
07bfcd09 CH |
806 | pr_warn("invalid parameter '%s'\n", |
807 | opt_tokens[i].pattern); | |
808 | } | |
809 | } | |
810 | ||
811 | return -EINVAL; | |
812 | } | |
813 | ||
814 | return 0; | |
815 | } | |
816 | ||
817 | void nvmf_free_options(struct nvmf_ctrl_options *opts) | |
818 | { | |
819 | nvmf_host_put(opts->host); | |
820 | kfree(opts->transport); | |
821 | kfree(opts->traddr); | |
822 | kfree(opts->trsvcid); | |
823 | kfree(opts->subsysnqn); | |
478bcb93 | 824 | kfree(opts->host_traddr); |
07bfcd09 CH |
825 | kfree(opts); |
826 | } | |
827 | EXPORT_SYMBOL_GPL(nvmf_free_options); | |
828 | ||
829 | #define NVMF_REQUIRED_OPTS (NVMF_OPT_TRANSPORT | NVMF_OPT_NQN) | |
830 | #define NVMF_ALLOWED_OPTS (NVMF_OPT_QUEUE_SIZE | NVMF_OPT_NR_IO_QUEUES | \ | |
6bfe0425 | 831 | NVMF_OPT_KATO | NVMF_OPT_HOSTNQN | \ |
3b338762 | 832 | NVMF_OPT_HOST_ID | NVMF_OPT_DUP_CONNECT) |
07bfcd09 CH |
833 | |
834 | static struct nvme_ctrl * | |
835 | nvmf_create_ctrl(struct device *dev, const char *buf, size_t count) | |
836 | { | |
837 | struct nvmf_ctrl_options *opts; | |
838 | struct nvmf_transport_ops *ops; | |
839 | struct nvme_ctrl *ctrl; | |
840 | int ret; | |
841 | ||
842 | opts = kzalloc(sizeof(*opts), GFP_KERNEL); | |
843 | if (!opts) | |
844 | return ERR_PTR(-ENOMEM); | |
845 | ||
846 | ret = nvmf_parse_options(opts, buf); | |
847 | if (ret) | |
848 | goto out_free_opts; | |
849 | ||
d1f1071f SG |
850 | |
851 | request_module("nvme-%s", opts->transport); | |
852 | ||
07bfcd09 CH |
853 | /* |
854 | * Check the generic options first as we need a valid transport for | |
855 | * the lookup below. Then clear the generic flags so that transport | |
856 | * drivers don't have to care about them. | |
857 | */ | |
858 | ret = nvmf_check_required_opts(opts, NVMF_REQUIRED_OPTS); | |
859 | if (ret) | |
860 | goto out_free_opts; | |
861 | opts->mask &= ~NVMF_REQUIRED_OPTS; | |
862 | ||
489beb91 | 863 | down_read(&nvmf_transports_rwsem); |
07bfcd09 CH |
864 | ops = nvmf_lookup_transport(opts); |
865 | if (!ops) { | |
866 | pr_info("no handler found for transport %s.\n", | |
867 | opts->transport); | |
868 | ret = -EINVAL; | |
869 | goto out_unlock; | |
870 | } | |
871 | ||
872 | ret = nvmf_check_required_opts(opts, ops->required_opts); | |
873 | if (ret) | |
874 | goto out_unlock; | |
875 | ret = nvmf_check_allowed_opts(opts, NVMF_ALLOWED_OPTS | | |
876 | ops->allowed_opts | ops->required_opts); | |
877 | if (ret) | |
878 | goto out_unlock; | |
879 | ||
880 | ctrl = ops->create_ctrl(dev, opts); | |
881 | if (IS_ERR(ctrl)) { | |
882 | ret = PTR_ERR(ctrl); | |
883 | goto out_unlock; | |
884 | } | |
885 | ||
ab9e00cc | 886 | if (strcmp(ctrl->subsys->subnqn, opts->subsysnqn)) { |
b1465c63 CH |
887 | dev_warn(ctrl->device, |
888 | "controller returned incorrect NQN: \"%s\".\n", | |
ab9e00cc | 889 | ctrl->subsys->subnqn); |
489beb91 | 890 | up_read(&nvmf_transports_rwsem); |
c5017e85 | 891 | nvme_delete_ctrl_sync(ctrl); |
b1465c63 CH |
892 | return ERR_PTR(-EINVAL); |
893 | } | |
894 | ||
489beb91 | 895 | up_read(&nvmf_transports_rwsem); |
07bfcd09 CH |
896 | return ctrl; |
897 | ||
898 | out_unlock: | |
489beb91 | 899 | up_read(&nvmf_transports_rwsem); |
07bfcd09 | 900 | out_free_opts: |
f3116d8f | 901 | nvmf_free_options(opts); |
07bfcd09 CH |
902 | return ERR_PTR(ret); |
903 | } | |
904 | ||
905 | static struct class *nvmf_class; | |
906 | static struct device *nvmf_device; | |
907 | static DEFINE_MUTEX(nvmf_dev_mutex); | |
908 | ||
909 | static ssize_t nvmf_dev_write(struct file *file, const char __user *ubuf, | |
910 | size_t count, loff_t *pos) | |
911 | { | |
912 | struct seq_file *seq_file = file->private_data; | |
913 | struct nvme_ctrl *ctrl; | |
914 | const char *buf; | |
915 | int ret = 0; | |
916 | ||
917 | if (count > PAGE_SIZE) | |
918 | return -ENOMEM; | |
919 | ||
920 | buf = memdup_user_nul(ubuf, count); | |
921 | if (IS_ERR(buf)) | |
922 | return PTR_ERR(buf); | |
923 | ||
924 | mutex_lock(&nvmf_dev_mutex); | |
925 | if (seq_file->private) { | |
926 | ret = -EINVAL; | |
927 | goto out_unlock; | |
928 | } | |
929 | ||
930 | ctrl = nvmf_create_ctrl(nvmf_device, buf, count); | |
931 | if (IS_ERR(ctrl)) { | |
932 | ret = PTR_ERR(ctrl); | |
933 | goto out_unlock; | |
934 | } | |
935 | ||
936 | seq_file->private = ctrl; | |
937 | ||
938 | out_unlock: | |
939 | mutex_unlock(&nvmf_dev_mutex); | |
940 | kfree(buf); | |
941 | return ret ? ret : count; | |
942 | } | |
943 | ||
944 | static int nvmf_dev_show(struct seq_file *seq_file, void *private) | |
945 | { | |
946 | struct nvme_ctrl *ctrl; | |
947 | int ret = 0; | |
948 | ||
949 | mutex_lock(&nvmf_dev_mutex); | |
950 | ctrl = seq_file->private; | |
951 | if (!ctrl) { | |
952 | ret = -EINVAL; | |
953 | goto out_unlock; | |
954 | } | |
955 | ||
956 | seq_printf(seq_file, "instance=%d,cntlid=%d\n", | |
957 | ctrl->instance, ctrl->cntlid); | |
958 | ||
959 | out_unlock: | |
960 | mutex_unlock(&nvmf_dev_mutex); | |
961 | return ret; | |
962 | } | |
963 | ||
964 | static int nvmf_dev_open(struct inode *inode, struct file *file) | |
965 | { | |
966 | /* | |
967 | * The miscdevice code initializes file->private_data, but doesn't | |
968 | * make use of it later. | |
969 | */ | |
970 | file->private_data = NULL; | |
971 | return single_open(file, nvmf_dev_show, NULL); | |
972 | } | |
973 | ||
974 | static int nvmf_dev_release(struct inode *inode, struct file *file) | |
975 | { | |
976 | struct seq_file *seq_file = file->private_data; | |
977 | struct nvme_ctrl *ctrl = seq_file->private; | |
978 | ||
979 | if (ctrl) | |
980 | nvme_put_ctrl(ctrl); | |
981 | return single_release(inode, file); | |
982 | } | |
983 | ||
984 | static const struct file_operations nvmf_dev_fops = { | |
985 | .owner = THIS_MODULE, | |
986 | .write = nvmf_dev_write, | |
987 | .read = seq_read, | |
988 | .open = nvmf_dev_open, | |
989 | .release = nvmf_dev_release, | |
990 | }; | |
991 | ||
992 | static struct miscdevice nvmf_misc = { | |
993 | .minor = MISC_DYNAMIC_MINOR, | |
994 | .name = "nvme-fabrics", | |
995 | .fops = &nvmf_dev_fops, | |
996 | }; | |
997 | ||
998 | static int __init nvmf_init(void) | |
999 | { | |
1000 | int ret; | |
1001 | ||
1002 | nvmf_default_host = nvmf_host_default(); | |
1003 | if (!nvmf_default_host) | |
1004 | return -ENOMEM; | |
1005 | ||
1006 | nvmf_class = class_create(THIS_MODULE, "nvme-fabrics"); | |
1007 | if (IS_ERR(nvmf_class)) { | |
1008 | pr_err("couldn't register class nvme-fabrics\n"); | |
1009 | ret = PTR_ERR(nvmf_class); | |
1010 | goto out_free_host; | |
1011 | } | |
1012 | ||
1013 | nvmf_device = | |
1014 | device_create(nvmf_class, NULL, MKDEV(0, 0), NULL, "ctl"); | |
1015 | if (IS_ERR(nvmf_device)) { | |
1016 | pr_err("couldn't create nvme-fabris device!\n"); | |
1017 | ret = PTR_ERR(nvmf_device); | |
1018 | goto out_destroy_class; | |
1019 | } | |
1020 | ||
1021 | ret = misc_register(&nvmf_misc); | |
1022 | if (ret) { | |
1023 | pr_err("couldn't register misc device: %d\n", ret); | |
1024 | goto out_destroy_device; | |
1025 | } | |
1026 | ||
1027 | return 0; | |
1028 | ||
1029 | out_destroy_device: | |
1030 | device_destroy(nvmf_class, MKDEV(0, 0)); | |
1031 | out_destroy_class: | |
1032 | class_destroy(nvmf_class); | |
1033 | out_free_host: | |
1034 | nvmf_host_put(nvmf_default_host); | |
1035 | return ret; | |
1036 | } | |
1037 | ||
1038 | static void __exit nvmf_exit(void) | |
1039 | { | |
1040 | misc_deregister(&nvmf_misc); | |
1041 | device_destroy(nvmf_class, MKDEV(0, 0)); | |
1042 | class_destroy(nvmf_class); | |
1043 | nvmf_host_put(nvmf_default_host); | |
1044 | ||
1045 | BUILD_BUG_ON(sizeof(struct nvmf_connect_command) != 64); | |
1046 | BUILD_BUG_ON(sizeof(struct nvmf_property_get_command) != 64); | |
1047 | BUILD_BUG_ON(sizeof(struct nvmf_property_set_command) != 64); | |
1048 | BUILD_BUG_ON(sizeof(struct nvmf_connect_data) != 1024); | |
1049 | } | |
1050 | ||
1051 | MODULE_LICENSE("GPL v2"); | |
1052 | ||
1053 | module_init(nvmf_init); | |
1054 | module_exit(nvmf_exit); |