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