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