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); |
Roland Dreier | 489beb9 | 2017-08-29 10:33:44 -0700 | [diff] [blame] | 25 | static DECLARE_RWSEM(nvmf_transports_rwsem); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 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); |
Hannes Reinecke | 1e5f446 | 2018-05-25 11:04:03 +0200 | [diff] [blame] | 60 | strlcpy(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); |
Ewan D. Milne | 6b01823 | 2018-01-05 12:44:06 -0500 | [diff] [blame] | 77 | uuid_gen(&host->id); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 78 | snprintf(host->nqn, NVMF_NQN_SIZE, |
Daniel Verkamp | 40a5fce | 2017-08-30 15:18:19 -0700 | [diff] [blame] | 79 | "nqn.2014-08.org.nvmexpress:uuid:%pUb", &host->id); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 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 | |
Ming Lin | e76debd | 2016-07-01 12:13:32 -0700 | [diff] [blame] | 92 | mutex_lock(&nvmf_hosts_mutex); |
| 93 | list_del(&host->list); |
| 94 | mutex_unlock(&nvmf_hosts_mutex); |
| 95 | |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 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 | { |
James Smart | 0fe51ff | 2016-08-02 10:40:01 +0300 | [diff] [blame] | 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); |
James Smart | 478bcb9 | 2016-08-02 10:42:10 +0300 | [diff] [blame] | 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); |
James Smart | 0fe51ff | 2016-08-02 10:40:01 +0300 | [diff] [blame] | 123 | len += snprintf(buf + len, size - len, "\n"); |
| 124 | |
| 125 | return len; |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 126 | } |
| 127 | EXPORT_SYMBOL_GPL(nvmf_get_address); |
| 128 | |
| 129 | /** |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 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; |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 153 | union nvme_result res; |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 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 | |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 161 | ret = __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, &res, NULL, 0, 0, |
Sagi Grimberg | 6287b51 | 2018-12-14 11:06:07 -0800 | [diff] [blame] | 162 | NVME_QID_ANY, 0, 0, false); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 163 | |
| 164 | if (ret >= 0) |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 165 | *val = le64_to_cpu(res.u64); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 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; |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 199 | union nvme_result res; |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 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 | |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 208 | ret = __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, &res, NULL, 0, 0, |
Sagi Grimberg | 6287b51 | 2018-12-14 11:06:07 -0800 | [diff] [blame] | 209 | NVME_QID_ANY, 0, 0, false); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 210 | |
| 211 | if (ret >= 0) |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 212 | *val = le64_to_cpu(res.u64); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 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, |
Sagi Grimberg | 6287b51 | 2018-12-14 11:06:07 -0800 | [diff] [blame] | 255 | NVME_QID_ANY, 0, 0, false); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 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; |
Guan Junxiong | 97ddc36e | 2017-06-13 10:51:24 +0800 | [diff] [blame] | 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 | |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 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; |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 378 | union nvme_result res; |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 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; |
Sagi Grimberg | 7aa1f42 | 2017-06-18 16:15:59 +0300 | [diff] [blame] | 386 | cmd.connect.sqsize = cpu_to_le16(NVME_AQ_DEPTH - 1); |
Jay Freyensee | f994d9d | 2016-08-17 15:00:26 -0700 | [diff] [blame] | 387 | |
Sagi Grimberg | 038bd4c | 2016-06-13 16:45:28 +0200 | [diff] [blame] | 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); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 394 | |
Sagi Grimberg | 8154ed7 | 2018-11-19 14:11:15 -0800 | [diff] [blame] | 395 | if (ctrl->opts->disable_sqflow) |
| 396 | cmd.connect.cattr |= NVME_CONNECT_DISABLE_SQFLOW; |
| 397 | |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 398 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 399 | if (!data) |
| 400 | return -ENOMEM; |
| 401 | |
Christoph Hellwig | 8e41226 | 2017-05-17 09:54:27 +0200 | [diff] [blame] | 402 | uuid_copy(&data->hostid, &ctrl->opts->host->id); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 403 | data->cntlid = cpu_to_le16(0xffff); |
| 404 | strncpy(data->subsysnqn, ctrl->opts->subsysnqn, NVMF_NQN_SIZE); |
| 405 | strncpy(data->hostnqn, ctrl->opts->host->nqn, NVMF_NQN_SIZE); |
| 406 | |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 407 | ret = __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, &res, |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 408 | data, sizeof(*data), 0, NVME_QID_ANY, 1, |
Sagi Grimberg | 6287b51 | 2018-12-14 11:06:07 -0800 | [diff] [blame] | 409 | BLK_MQ_REQ_RESERVED | BLK_MQ_REQ_NOWAIT, false); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 410 | if (ret) { |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 411 | nvmf_log_connect_error(ctrl, ret, le32_to_cpu(res.u32), |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 412 | &cmd, data); |
| 413 | goto out_free_data; |
| 414 | } |
| 415 | |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 416 | ctrl->cntlid = le16_to_cpu(res.u16); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 417 | |
| 418 | out_free_data: |
| 419 | kfree(data); |
| 420 | return ret; |
| 421 | } |
| 422 | EXPORT_SYMBOL_GPL(nvmf_connect_admin_queue); |
| 423 | |
| 424 | /** |
| 425 | * nvmf_connect_io_queue() - NVMe Fabrics I/O Queue "Connect" |
| 426 | * API function. |
| 427 | * @ctrl: Host nvme controller instance used to establish an |
| 428 | * NVMe I/O queue connection to the already allocated NVMe |
| 429 | * controller on the target system. |
| 430 | * @qid: NVMe I/O queue number for the new I/O connection between |
| 431 | * host and target (note qid == 0 is illegal as this is |
| 432 | * the Admin queue, per NVMe standard). |
| 433 | * |
| 434 | * This function issues a fabrics-protocol connection |
| 435 | * of a NVMe I/O queue (via NVMe Fabrics "Connect" command) |
| 436 | * between the host system device and the allocated NVMe controller |
| 437 | * on the target system. |
| 438 | * |
| 439 | * Return: |
| 440 | * 0: success |
| 441 | * > 0: NVMe error status code |
| 442 | * < 0: Linux errno error code |
| 443 | */ |
Sagi Grimberg | 26c6822 | 2018-12-14 11:06:08 -0800 | [diff] [blame] | 444 | int nvmf_connect_io_queue(struct nvme_ctrl *ctrl, u16 qid, bool poll) |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 445 | { |
| 446 | struct nvme_command cmd; |
| 447 | struct nvmf_connect_data *data; |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 448 | union nvme_result res; |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 449 | int ret; |
| 450 | |
| 451 | memset(&cmd, 0, sizeof(cmd)); |
| 452 | cmd.connect.opcode = nvme_fabrics_command; |
| 453 | cmd.connect.fctype = nvme_fabrics_type_connect; |
| 454 | cmd.connect.qid = cpu_to_le16(qid); |
| 455 | cmd.connect.sqsize = cpu_to_le16(ctrl->sqsize); |
| 456 | |
Sagi Grimberg | 8154ed7 | 2018-11-19 14:11:15 -0800 | [diff] [blame] | 457 | if (ctrl->opts->disable_sqflow) |
| 458 | cmd.connect.cattr |= NVME_CONNECT_DISABLE_SQFLOW; |
| 459 | |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 460 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 461 | if (!data) |
| 462 | return -ENOMEM; |
| 463 | |
Christoph Hellwig | 8e41226 | 2017-05-17 09:54:27 +0200 | [diff] [blame] | 464 | uuid_copy(&data->hostid, &ctrl->opts->host->id); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 465 | data->cntlid = cpu_to_le16(ctrl->cntlid); |
| 466 | strncpy(data->subsysnqn, ctrl->opts->subsysnqn, NVMF_NQN_SIZE); |
| 467 | strncpy(data->hostnqn, ctrl->opts->host->nqn, NVMF_NQN_SIZE); |
| 468 | |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 469 | ret = __nvme_submit_sync_cmd(ctrl->connect_q, &cmd, &res, |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 470 | data, sizeof(*data), 0, qid, 1, |
Sagi Grimberg | 26c6822 | 2018-12-14 11:06:08 -0800 | [diff] [blame] | 471 | BLK_MQ_REQ_RESERVED | BLK_MQ_REQ_NOWAIT, poll); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 472 | if (ret) { |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 473 | nvmf_log_connect_error(ctrl, ret, le32_to_cpu(res.u32), |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 474 | &cmd, data); |
| 475 | } |
| 476 | kfree(data); |
| 477 | return ret; |
| 478 | } |
| 479 | EXPORT_SYMBOL_GPL(nvmf_connect_io_queue); |
| 480 | |
Sagi Grimberg | 42a4527 | 2017-03-18 20:52:36 +0200 | [diff] [blame] | 481 | bool nvmf_should_reconnect(struct nvme_ctrl *ctrl) |
| 482 | { |
Tal Shorer | 66414e8 | 2018-08-07 23:42:39 +0300 | [diff] [blame] | 483 | if (ctrl->opts->max_reconnects == -1 || |
Sagi Grimberg | fdf9dfa | 2017-05-04 13:33:15 +0300 | [diff] [blame] | 484 | ctrl->nr_reconnects < ctrl->opts->max_reconnects) |
Sagi Grimberg | 42a4527 | 2017-03-18 20:52:36 +0200 | [diff] [blame] | 485 | return true; |
| 486 | |
| 487 | return false; |
| 488 | } |
| 489 | EXPORT_SYMBOL_GPL(nvmf_should_reconnect); |
| 490 | |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 491 | /** |
| 492 | * nvmf_register_transport() - NVMe Fabrics Library registration function. |
| 493 | * @ops: Transport ops instance to be registered to the |
| 494 | * common fabrics library. |
| 495 | * |
| 496 | * API function that registers the type of specific transport fabric |
| 497 | * being implemented to the common NVMe fabrics library. Part of |
| 498 | * the overall init sequence of starting up a fabrics driver. |
| 499 | */ |
Johannes Thumshirn | e5a39dd | 2017-01-27 09:03:45 +0100 | [diff] [blame] | 500 | int nvmf_register_transport(struct nvmf_transport_ops *ops) |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 501 | { |
Christoph Hellwig | 5a1e595 | 2018-02-22 07:24:08 -0800 | [diff] [blame] | 502 | if (!ops->create_ctrl) |
Johannes Thumshirn | e5a39dd | 2017-01-27 09:03:45 +0100 | [diff] [blame] | 503 | return -EINVAL; |
| 504 | |
Roland Dreier | 489beb9 | 2017-08-29 10:33:44 -0700 | [diff] [blame] | 505 | down_write(&nvmf_transports_rwsem); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 506 | list_add_tail(&ops->entry, &nvmf_transports); |
Roland Dreier | 489beb9 | 2017-08-29 10:33:44 -0700 | [diff] [blame] | 507 | up_write(&nvmf_transports_rwsem); |
Johannes Thumshirn | e5a39dd | 2017-01-27 09:03:45 +0100 | [diff] [blame] | 508 | |
| 509 | return 0; |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 510 | } |
| 511 | EXPORT_SYMBOL_GPL(nvmf_register_transport); |
| 512 | |
| 513 | /** |
| 514 | * nvmf_unregister_transport() - NVMe Fabrics Library unregistration function. |
| 515 | * @ops: Transport ops instance to be unregistered from the |
| 516 | * common fabrics library. |
| 517 | * |
| 518 | * Fabrics API function that unregisters the type of specific transport |
| 519 | * fabric being implemented from the common NVMe fabrics library. |
| 520 | * Part of the overall exit sequence of unloading the implemented driver. |
| 521 | */ |
| 522 | void nvmf_unregister_transport(struct nvmf_transport_ops *ops) |
| 523 | { |
Roland Dreier | 489beb9 | 2017-08-29 10:33:44 -0700 | [diff] [blame] | 524 | down_write(&nvmf_transports_rwsem); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 525 | list_del(&ops->entry); |
Roland Dreier | 489beb9 | 2017-08-29 10:33:44 -0700 | [diff] [blame] | 526 | up_write(&nvmf_transports_rwsem); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 527 | } |
| 528 | EXPORT_SYMBOL_GPL(nvmf_unregister_transport); |
| 529 | |
| 530 | static struct nvmf_transport_ops *nvmf_lookup_transport( |
| 531 | struct nvmf_ctrl_options *opts) |
| 532 | { |
| 533 | struct nvmf_transport_ops *ops; |
| 534 | |
Roland Dreier | 489beb9 | 2017-08-29 10:33:44 -0700 | [diff] [blame] | 535 | lockdep_assert_held(&nvmf_transports_rwsem); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 536 | |
| 537 | list_for_each_entry(ops, &nvmf_transports, entry) { |
| 538 | if (strcmp(ops->name, opts->transport) == 0) |
| 539 | return ops; |
| 540 | } |
| 541 | |
| 542 | return NULL; |
| 543 | } |
| 544 | |
Christoph Hellwig | 3bc32bb | 2018-06-11 17:34:06 +0200 | [diff] [blame] | 545 | /* |
| 546 | * For something we're not in a state to send to the device the default action |
| 547 | * is to busy it and retry it after the controller state is recovered. However, |
James Smart | 6cdefc6 | 2018-07-20 15:49:48 -0700 | [diff] [blame] | 548 | * if the controller is deleting or if anything is marked for failfast or |
| 549 | * nvme multipath it is immediately failed. |
Christoph Hellwig | 3bc32bb | 2018-06-11 17:34:06 +0200 | [diff] [blame] | 550 | * |
| 551 | * Note: commands used to initialize the controller will be marked for failfast. |
| 552 | * Note: nvme cli/ioctl commands are marked for failfast. |
| 553 | */ |
James Smart | 6cdefc6 | 2018-07-20 15:49:48 -0700 | [diff] [blame] | 554 | blk_status_t nvmf_fail_nonready_command(struct nvme_ctrl *ctrl, |
| 555 | struct request *rq) |
Christoph Hellwig | 3bc32bb | 2018-06-11 17:34:06 +0200 | [diff] [blame] | 556 | { |
James Smart | 6cdefc6 | 2018-07-20 15:49:48 -0700 | [diff] [blame] | 557 | if (ctrl->state != NVME_CTRL_DELETING && |
| 558 | ctrl->state != NVME_CTRL_DEAD && |
| 559 | !blk_noretry_request(rq) && !(rq->cmd_flags & REQ_NVME_MPATH)) |
Christoph Hellwig | 3bc32bb | 2018-06-11 17:34:06 +0200 | [diff] [blame] | 560 | return BLK_STS_RESOURCE; |
James Smart | 783f4a4 | 2018-09-27 16:58:54 -0700 | [diff] [blame] | 561 | |
| 562 | nvme_req(rq)->status = NVME_SC_HOST_PATH_ERROR; |
| 563 | blk_mq_start_request(rq); |
| 564 | nvme_complete_rq(rq); |
| 565 | return BLK_STS_OK; |
Christoph Hellwig | 3bc32bb | 2018-06-11 17:34:06 +0200 | [diff] [blame] | 566 | } |
| 567 | EXPORT_SYMBOL_GPL(nvmf_fail_nonready_command); |
| 568 | |
| 569 | bool __nvmf_check_ready(struct nvme_ctrl *ctrl, struct request *rq, |
| 570 | bool queue_live) |
James Smart | bb06ec31 | 2018-04-12 09:16:15 -0600 | [diff] [blame] | 571 | { |
Christoph Hellwig | 35897b9 | 2018-06-11 17:41:11 +0200 | [diff] [blame] | 572 | struct nvme_request *req = nvme_req(rq); |
James Smart | bb06ec31 | 2018-04-12 09:16:15 -0600 | [diff] [blame] | 573 | |
Christoph Hellwig | 35897b9 | 2018-06-11 17:41:11 +0200 | [diff] [blame] | 574 | /* |
| 575 | * If we are in some state of setup or teardown only allow |
| 576 | * internally generated commands. |
| 577 | */ |
| 578 | if (!blk_rq_is_passthrough(rq) || (req->flags & NVME_REQ_USERCMD)) |
| 579 | return false; |
| 580 | |
| 581 | /* |
| 582 | * Only allow commands on a live queue, except for the connect command, |
| 583 | * which is require to set the queue live in the appropinquate states. |
| 584 | */ |
James Smart | bb06ec31 | 2018-04-12 09:16:15 -0600 | [diff] [blame] | 585 | switch (ctrl->state) { |
James Smart | bb06ec31 | 2018-04-12 09:16:15 -0600 | [diff] [blame] | 586 | case NVME_CTRL_NEW: |
| 587 | case NVME_CTRL_CONNECTING: |
Christoph Hellwig | 35897b9 | 2018-06-11 17:41:11 +0200 | [diff] [blame] | 588 | if (req->cmd->common.opcode == nvme_fabrics_command && |
| 589 | req->cmd->fabrics.fctype == nvme_fabrics_type_connect) |
Christoph Hellwig | 3bc32bb | 2018-06-11 17:34:06 +0200 | [diff] [blame] | 590 | return true; |
Christoph Hellwig | 35897b9 | 2018-06-11 17:41:11 +0200 | [diff] [blame] | 591 | break; |
James Smart | bb06ec31 | 2018-04-12 09:16:15 -0600 | [diff] [blame] | 592 | default: |
Christoph Hellwig | 35897b9 | 2018-06-11 17:41:11 +0200 | [diff] [blame] | 593 | break; |
| 594 | case NVME_CTRL_DEAD: |
Christoph Hellwig | 3bc32bb | 2018-06-11 17:34:06 +0200 | [diff] [blame] | 595 | return false; |
James Smart | bb06ec31 | 2018-04-12 09:16:15 -0600 | [diff] [blame] | 596 | } |
Christoph Hellwig | 35897b9 | 2018-06-11 17:41:11 +0200 | [diff] [blame] | 597 | |
| 598 | return queue_live; |
James Smart | bb06ec31 | 2018-04-12 09:16:15 -0600 | [diff] [blame] | 599 | } |
Christoph Hellwig | 3bc32bb | 2018-06-11 17:34:06 +0200 | [diff] [blame] | 600 | EXPORT_SYMBOL_GPL(__nvmf_check_ready); |
James Smart | bb06ec31 | 2018-04-12 09:16:15 -0600 | [diff] [blame] | 601 | |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 602 | static const match_table_t opt_tokens = { |
| 603 | { NVMF_OPT_TRANSPORT, "transport=%s" }, |
| 604 | { NVMF_OPT_TRADDR, "traddr=%s" }, |
| 605 | { NVMF_OPT_TRSVCID, "trsvcid=%s" }, |
| 606 | { NVMF_OPT_NQN, "nqn=%s" }, |
| 607 | { NVMF_OPT_QUEUE_SIZE, "queue_size=%d" }, |
| 608 | { NVMF_OPT_NR_IO_QUEUES, "nr_io_queues=%d" }, |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 609 | { NVMF_OPT_RECONNECT_DELAY, "reconnect_delay=%d" }, |
Sagi Grimberg | 42a4527 | 2017-03-18 20:52:36 +0200 | [diff] [blame] | 610 | { NVMF_OPT_CTRL_LOSS_TMO, "ctrl_loss_tmo=%d" }, |
Sagi Grimberg | 038bd4c | 2016-06-13 16:45:28 +0200 | [diff] [blame] | 611 | { NVMF_OPT_KATO, "keep_alive_tmo=%d" }, |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 612 | { NVMF_OPT_HOSTNQN, "hostnqn=%s" }, |
James Smart | 478bcb9 | 2016-08-02 10:42:10 +0300 | [diff] [blame] | 613 | { NVMF_OPT_HOST_TRADDR, "host_traddr=%s" }, |
Johannes Thumshirn | 6bfe042 | 2017-06-20 14:23:01 +0200 | [diff] [blame] | 614 | { NVMF_OPT_HOST_ID, "hostid=%s" }, |
James Smart | 3b33876 | 2017-10-20 16:17:06 -0700 | [diff] [blame] | 615 | { NVMF_OPT_DUP_CONNECT, "duplicate_connect" }, |
Sagi Grimberg | 8154ed7 | 2018-11-19 14:11:15 -0800 | [diff] [blame] | 616 | { NVMF_OPT_DISABLE_SQFLOW, "disable_sqflow" }, |
Sagi Grimberg | 3b49fa8 | 2018-12-03 17:52:12 -0800 | [diff] [blame] | 617 | { NVMF_OPT_HDR_DIGEST, "hdr_digest" }, |
Sagi Grimberg | 20d44e8 | 2018-12-03 17:52:13 -0800 | [diff] [blame] | 618 | { NVMF_OPT_DATA_DIGEST, "data_digest" }, |
Sagi Grimberg | 330f6b8 | 2018-12-11 23:38:56 -0800 | [diff] [blame] | 619 | { NVMF_OPT_NR_WRITE_QUEUES, "nr_write_queues=%d" }, |
Sagi Grimberg | 89d4380 | 2018-12-14 11:06:09 -0800 | [diff] [blame] | 620 | { NVMF_OPT_NR_POLL_QUEUES, "nr_poll_queues=%d" }, |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 621 | { NVMF_OPT_ERR, NULL } |
| 622 | }; |
| 623 | |
| 624 | static int nvmf_parse_options(struct nvmf_ctrl_options *opts, |
| 625 | const char *buf) |
| 626 | { |
| 627 | substring_t args[MAX_OPT_ARGS]; |
| 628 | char *options, *o, *p; |
| 629 | int token, ret = 0; |
| 630 | size_t nqnlen = 0; |
Sagi Grimberg | 42a4527 | 2017-03-18 20:52:36 +0200 | [diff] [blame] | 631 | int ctrl_loss_tmo = NVMF_DEF_CTRL_LOSS_TMO; |
Johannes Thumshirn | 6bfe042 | 2017-06-20 14:23:01 +0200 | [diff] [blame] | 632 | uuid_t hostid; |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 633 | |
| 634 | /* Set defaults */ |
| 635 | opts->queue_size = NVMF_DEF_QUEUE_SIZE; |
| 636 | opts->nr_io_queues = num_online_cpus(); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 637 | opts->reconnect_delay = NVMF_DEF_RECONNECT_DELAY; |
Guilherme G. Piccoli | 8edd11c | 2017-09-14 13:59:28 -0300 | [diff] [blame] | 638 | opts->kato = NVME_DEFAULT_KATO; |
James Smart | 3b33876 | 2017-10-20 16:17:06 -0700 | [diff] [blame] | 639 | opts->duplicate_connect = false; |
Sagi Grimberg | 3b49fa8 | 2018-12-03 17:52:12 -0800 | [diff] [blame] | 640 | opts->hdr_digest = false; |
Sagi Grimberg | 20d44e8 | 2018-12-03 17:52:13 -0800 | [diff] [blame] | 641 | opts->data_digest = false; |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 642 | |
| 643 | options = o = kstrdup(buf, GFP_KERNEL); |
| 644 | if (!options) |
| 645 | return -ENOMEM; |
| 646 | |
Johannes Thumshirn | 6bfe042 | 2017-06-20 14:23:01 +0200 | [diff] [blame] | 647 | uuid_gen(&hostid); |
| 648 | |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 649 | while ((p = strsep(&o, ",\n")) != NULL) { |
| 650 | if (!*p) |
| 651 | continue; |
| 652 | |
| 653 | token = match_token(p, opt_tokens, args); |
| 654 | opts->mask |= token; |
| 655 | switch (token) { |
| 656 | case NVMF_OPT_TRANSPORT: |
| 657 | p = match_strdup(args); |
| 658 | if (!p) { |
| 659 | ret = -ENOMEM; |
| 660 | goto out; |
| 661 | } |
Chengguang Xu | 59a2f3f | 2018-04-14 20:06:19 +0800 | [diff] [blame] | 662 | kfree(opts->transport); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 663 | opts->transport = p; |
| 664 | break; |
| 665 | case NVMF_OPT_NQN: |
| 666 | p = match_strdup(args); |
| 667 | if (!p) { |
| 668 | ret = -ENOMEM; |
| 669 | goto out; |
| 670 | } |
Chengguang Xu | 59a2f3f | 2018-04-14 20:06:19 +0800 | [diff] [blame] | 671 | kfree(opts->subsysnqn); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 672 | opts->subsysnqn = p; |
| 673 | nqnlen = strlen(opts->subsysnqn); |
| 674 | if (nqnlen >= NVMF_NQN_SIZE) { |
| 675 | pr_err("%s needs to be < %d bytes\n", |
Bart Van Assche | 6eb7283 | 2016-10-18 13:10:24 -0700 | [diff] [blame] | 676 | opts->subsysnqn, NVMF_NQN_SIZE); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 677 | ret = -EINVAL; |
| 678 | goto out; |
| 679 | } |
| 680 | opts->discovery_nqn = |
| 681 | !(strcmp(opts->subsysnqn, |
| 682 | NVME_DISC_SUBSYS_NAME)); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 683 | break; |
| 684 | case NVMF_OPT_TRADDR: |
| 685 | p = match_strdup(args); |
| 686 | if (!p) { |
| 687 | ret = -ENOMEM; |
| 688 | goto out; |
| 689 | } |
Chengguang Xu | 59a2f3f | 2018-04-14 20:06:19 +0800 | [diff] [blame] | 690 | kfree(opts->traddr); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 691 | opts->traddr = p; |
| 692 | break; |
| 693 | case NVMF_OPT_TRSVCID: |
| 694 | p = match_strdup(args); |
| 695 | if (!p) { |
| 696 | ret = -ENOMEM; |
| 697 | goto out; |
| 698 | } |
Chengguang Xu | 59a2f3f | 2018-04-14 20:06:19 +0800 | [diff] [blame] | 699 | kfree(opts->trsvcid); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 700 | opts->trsvcid = p; |
| 701 | break; |
| 702 | case NVMF_OPT_QUEUE_SIZE: |
| 703 | if (match_int(args, &token)) { |
| 704 | ret = -EINVAL; |
| 705 | goto out; |
| 706 | } |
| 707 | if (token < NVMF_MIN_QUEUE_SIZE || |
| 708 | token > NVMF_MAX_QUEUE_SIZE) { |
| 709 | pr_err("Invalid queue_size %d\n", token); |
| 710 | ret = -EINVAL; |
| 711 | goto out; |
| 712 | } |
| 713 | opts->queue_size = token; |
| 714 | break; |
| 715 | case NVMF_OPT_NR_IO_QUEUES: |
| 716 | if (match_int(args, &token)) { |
| 717 | ret = -EINVAL; |
| 718 | goto out; |
| 719 | } |
| 720 | if (token <= 0) { |
| 721 | pr_err("Invalid number of IOQs %d\n", token); |
| 722 | ret = -EINVAL; |
| 723 | goto out; |
| 724 | } |
Roland Dreier | 0475821 | 2018-03-05 11:59:53 -0800 | [diff] [blame] | 725 | if (opts->discovery_nqn) { |
| 726 | pr_debug("Ignoring nr_io_queues value for discovery controller\n"); |
| 727 | break; |
| 728 | } |
| 729 | |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 730 | opts->nr_io_queues = min_t(unsigned int, |
| 731 | num_online_cpus(), token); |
| 732 | break; |
Sagi Grimberg | 038bd4c | 2016-06-13 16:45:28 +0200 | [diff] [blame] | 733 | case NVMF_OPT_KATO: |
| 734 | if (match_int(args, &token)) { |
| 735 | ret = -EINVAL; |
| 736 | goto out; |
| 737 | } |
| 738 | |
Sagi Grimberg | 038bd4c | 2016-06-13 16:45:28 +0200 | [diff] [blame] | 739 | if (token < 0) { |
| 740 | pr_err("Invalid keep_alive_tmo %d\n", token); |
| 741 | ret = -EINVAL; |
| 742 | goto out; |
Guilherme G. Piccoli | 8edd11c | 2017-09-14 13:59:28 -0300 | [diff] [blame] | 743 | } else if (token == 0 && !opts->discovery_nqn) { |
Sagi Grimberg | 038bd4c | 2016-06-13 16:45:28 +0200 | [diff] [blame] | 744 | /* Allowed for debug */ |
| 745 | pr_warn("keep_alive_tmo 0 won't execute keep alives!!!\n"); |
| 746 | } |
| 747 | opts->kato = token; |
Guilherme G. Piccoli | 8edd11c | 2017-09-14 13:59:28 -0300 | [diff] [blame] | 748 | |
| 749 | if (opts->discovery_nqn && opts->kato) { |
| 750 | pr_err("Discovery controllers cannot accept KATO != 0\n"); |
| 751 | ret = -EINVAL; |
| 752 | goto out; |
| 753 | } |
| 754 | |
Sagi Grimberg | 038bd4c | 2016-06-13 16:45:28 +0200 | [diff] [blame] | 755 | break; |
Sagi Grimberg | 42a4527 | 2017-03-18 20:52:36 +0200 | [diff] [blame] | 756 | case NVMF_OPT_CTRL_LOSS_TMO: |
| 757 | if (match_int(args, &token)) { |
| 758 | ret = -EINVAL; |
| 759 | goto out; |
| 760 | } |
| 761 | |
| 762 | if (token < 0) |
| 763 | pr_warn("ctrl_loss_tmo < 0 will reconnect forever\n"); |
| 764 | ctrl_loss_tmo = token; |
| 765 | break; |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 766 | case NVMF_OPT_HOSTNQN: |
| 767 | if (opts->host) { |
| 768 | pr_err("hostnqn already user-assigned: %s\n", |
| 769 | opts->host->nqn); |
| 770 | ret = -EADDRINUSE; |
| 771 | goto out; |
| 772 | } |
| 773 | p = match_strdup(args); |
| 774 | if (!p) { |
| 775 | ret = -ENOMEM; |
| 776 | goto out; |
| 777 | } |
| 778 | nqnlen = strlen(p); |
| 779 | if (nqnlen >= NVMF_NQN_SIZE) { |
| 780 | pr_err("%s needs to be < %d bytes\n", |
| 781 | p, NVMF_NQN_SIZE); |
Bart Van Assche | 8eadfcb | 2016-10-18 13:10:46 -0700 | [diff] [blame] | 782 | kfree(p); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 783 | ret = -EINVAL; |
| 784 | goto out; |
| 785 | } |
Chengguang Xu | 59a2f3f | 2018-04-14 20:06:19 +0800 | [diff] [blame] | 786 | nvmf_host_put(opts->host); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 787 | opts->host = nvmf_host_add(p); |
Bart Van Assche | 8eadfcb | 2016-10-18 13:10:46 -0700 | [diff] [blame] | 788 | kfree(p); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 789 | if (!opts->host) { |
| 790 | ret = -ENOMEM; |
| 791 | goto out; |
| 792 | } |
| 793 | break; |
| 794 | case NVMF_OPT_RECONNECT_DELAY: |
| 795 | if (match_int(args, &token)) { |
| 796 | ret = -EINVAL; |
| 797 | goto out; |
| 798 | } |
| 799 | if (token <= 0) { |
| 800 | pr_err("Invalid reconnect_delay %d\n", token); |
| 801 | ret = -EINVAL; |
| 802 | goto out; |
| 803 | } |
| 804 | opts->reconnect_delay = token; |
| 805 | break; |
James Smart | 478bcb9 | 2016-08-02 10:42:10 +0300 | [diff] [blame] | 806 | case NVMF_OPT_HOST_TRADDR: |
| 807 | p = match_strdup(args); |
| 808 | if (!p) { |
| 809 | ret = -ENOMEM; |
| 810 | goto out; |
| 811 | } |
Chengguang Xu | 59a2f3f | 2018-04-14 20:06:19 +0800 | [diff] [blame] | 812 | kfree(opts->host_traddr); |
James Smart | 478bcb9 | 2016-08-02 10:42:10 +0300 | [diff] [blame] | 813 | opts->host_traddr = p; |
| 814 | break; |
Johannes Thumshirn | 6bfe042 | 2017-06-20 14:23:01 +0200 | [diff] [blame] | 815 | case NVMF_OPT_HOST_ID: |
| 816 | p = match_strdup(args); |
| 817 | if (!p) { |
| 818 | ret = -ENOMEM; |
| 819 | goto out; |
| 820 | } |
Roland Dreier | df351ef | 2018-01-11 13:38:00 -0800 | [diff] [blame] | 821 | ret = uuid_parse(p, &hostid); |
Roland Dreier | df351ef | 2018-01-11 13:38:00 -0800 | [diff] [blame] | 822 | if (ret) { |
Guan Junxiong | 9b483da | 2017-08-03 21:40:26 +0800 | [diff] [blame] | 823 | pr_err("Invalid hostid %s\n", p); |
Johannes Thumshirn | 6bfe042 | 2017-06-20 14:23:01 +0200 | [diff] [blame] | 824 | ret = -EINVAL; |
Johannes Thumshirn | 6e49412 | 2018-01-25 09:09:25 +0100 | [diff] [blame] | 825 | kfree(p); |
Johannes Thumshirn | 6bfe042 | 2017-06-20 14:23:01 +0200 | [diff] [blame] | 826 | goto out; |
| 827 | } |
Johannes Thumshirn | 6e49412 | 2018-01-25 09:09:25 +0100 | [diff] [blame] | 828 | kfree(p); |
Johannes Thumshirn | 6bfe042 | 2017-06-20 14:23:01 +0200 | [diff] [blame] | 829 | break; |
James Smart | 3b33876 | 2017-10-20 16:17:06 -0700 | [diff] [blame] | 830 | case NVMF_OPT_DUP_CONNECT: |
| 831 | opts->duplicate_connect = true; |
| 832 | break; |
Sagi Grimberg | 8154ed7 | 2018-11-19 14:11:15 -0800 | [diff] [blame] | 833 | case NVMF_OPT_DISABLE_SQFLOW: |
| 834 | opts->disable_sqflow = true; |
| 835 | break; |
Sagi Grimberg | 3b49fa8 | 2018-12-03 17:52:12 -0800 | [diff] [blame] | 836 | case NVMF_OPT_HDR_DIGEST: |
| 837 | opts->hdr_digest = true; |
| 838 | break; |
Sagi Grimberg | 20d44e8 | 2018-12-03 17:52:13 -0800 | [diff] [blame] | 839 | case NVMF_OPT_DATA_DIGEST: |
| 840 | opts->data_digest = true; |
| 841 | break; |
Sagi Grimberg | 330f6b8 | 2018-12-11 23:38:56 -0800 | [diff] [blame] | 842 | case NVMF_OPT_NR_WRITE_QUEUES: |
| 843 | if (match_int(args, &token)) { |
| 844 | ret = -EINVAL; |
| 845 | goto out; |
| 846 | } |
| 847 | if (token <= 0) { |
| 848 | pr_err("Invalid nr_write_queues %d\n", token); |
| 849 | ret = -EINVAL; |
| 850 | goto out; |
| 851 | } |
| 852 | opts->nr_write_queues = token; |
| 853 | break; |
Sagi Grimberg | 89d4380 | 2018-12-14 11:06:09 -0800 | [diff] [blame] | 854 | case NVMF_OPT_NR_POLL_QUEUES: |
| 855 | if (match_int(args, &token)) { |
| 856 | ret = -EINVAL; |
| 857 | goto out; |
| 858 | } |
| 859 | if (token <= 0) { |
| 860 | pr_err("Invalid nr_poll_queues %d\n", token); |
| 861 | ret = -EINVAL; |
| 862 | goto out; |
| 863 | } |
| 864 | opts->nr_poll_queues = token; |
| 865 | break; |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 866 | default: |
| 867 | pr_warn("unknown parameter or missing value '%s' in ctrl creation request\n", |
| 868 | p); |
| 869 | ret = -EINVAL; |
| 870 | goto out; |
| 871 | } |
| 872 | } |
| 873 | |
Hannes Reinecke | 461fbc8 | 2018-05-24 16:18:15 +0200 | [diff] [blame] | 874 | if (opts->discovery_nqn) { |
| 875 | opts->kato = 0; |
| 876 | opts->nr_io_queues = 0; |
Sagi Grimberg | 9846ac0 | 2019-01-07 23:54:23 -0800 | [diff] [blame^] | 877 | opts->nr_write_queues = 0; |
| 878 | opts->nr_poll_queues = 0; |
Hannes Reinecke | 181303d | 2018-05-24 16:18:17 +0200 | [diff] [blame] | 879 | opts->duplicate_connect = true; |
Hannes Reinecke | 461fbc8 | 2018-05-24 16:18:15 +0200 | [diff] [blame] | 880 | } |
Sagi Grimberg | 42a4527 | 2017-03-18 20:52:36 +0200 | [diff] [blame] | 881 | if (ctrl_loss_tmo < 0) |
| 882 | opts->max_reconnects = -1; |
| 883 | else |
| 884 | opts->max_reconnects = DIV_ROUND_UP(ctrl_loss_tmo, |
| 885 | opts->reconnect_delay); |
| 886 | |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 887 | if (!opts->host) { |
| 888 | kref_get(&nvmf_default_host->ref); |
| 889 | opts->host = nvmf_default_host; |
| 890 | } |
| 891 | |
Johannes Thumshirn | 6bfe042 | 2017-06-20 14:23:01 +0200 | [diff] [blame] | 892 | uuid_copy(&opts->host->id, &hostid); |
| 893 | |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 894 | out: |
| 895 | kfree(options); |
| 896 | return ret; |
| 897 | } |
| 898 | |
| 899 | static int nvmf_check_required_opts(struct nvmf_ctrl_options *opts, |
| 900 | unsigned int required_opts) |
| 901 | { |
| 902 | if ((opts->mask & required_opts) != required_opts) { |
| 903 | int i; |
| 904 | |
| 905 | for (i = 0; i < ARRAY_SIZE(opt_tokens); i++) { |
| 906 | if ((opt_tokens[i].token & required_opts) && |
| 907 | !(opt_tokens[i].token & opts->mask)) { |
| 908 | pr_warn("missing parameter '%s'\n", |
| 909 | opt_tokens[i].pattern); |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | return -EINVAL; |
| 914 | } |
| 915 | |
| 916 | return 0; |
| 917 | } |
| 918 | |
Sagi Grimberg | b7c7be6f6 | 2018-10-18 17:40:40 -0700 | [diff] [blame] | 919 | bool nvmf_ip_options_match(struct nvme_ctrl *ctrl, |
| 920 | struct nvmf_ctrl_options *opts) |
| 921 | { |
| 922 | if (!nvmf_ctlr_matches_baseopts(ctrl, opts) || |
| 923 | strcmp(opts->traddr, ctrl->opts->traddr) || |
| 924 | strcmp(opts->trsvcid, ctrl->opts->trsvcid)) |
| 925 | return false; |
| 926 | |
| 927 | /* |
| 928 | * Checking the local address is rough. In most cases, none is specified |
| 929 | * and the host port is selected by the stack. |
| 930 | * |
| 931 | * Assume no match if: |
| 932 | * - local address is specified and address is not the same |
| 933 | * - local address is not specified but remote is, or vice versa |
| 934 | * (admin using specific host_traddr when it matters). |
| 935 | */ |
| 936 | if ((opts->mask & NVMF_OPT_HOST_TRADDR) && |
| 937 | (ctrl->opts->mask & NVMF_OPT_HOST_TRADDR)) { |
| 938 | if (strcmp(opts->host_traddr, ctrl->opts->host_traddr)) |
| 939 | return false; |
| 940 | } else if ((opts->mask & NVMF_OPT_HOST_TRADDR) || |
| 941 | (ctrl->opts->mask & NVMF_OPT_HOST_TRADDR)) { |
| 942 | return false; |
| 943 | } |
| 944 | |
| 945 | return true; |
| 946 | } |
| 947 | EXPORT_SYMBOL_GPL(nvmf_ip_options_match); |
| 948 | |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 949 | static int nvmf_check_allowed_opts(struct nvmf_ctrl_options *opts, |
| 950 | unsigned int allowed_opts) |
| 951 | { |
| 952 | if (opts->mask & ~allowed_opts) { |
| 953 | int i; |
| 954 | |
| 955 | for (i = 0; i < ARRAY_SIZE(opt_tokens); i++) { |
Christoph Hellwig | 81a0b8d | 2017-08-17 13:57:49 +0200 | [diff] [blame] | 956 | if ((opt_tokens[i].token & opts->mask) && |
| 957 | (opt_tokens[i].token & ~allowed_opts)) { |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 958 | pr_warn("invalid parameter '%s'\n", |
| 959 | opt_tokens[i].pattern); |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | return -EINVAL; |
| 964 | } |
| 965 | |
| 966 | return 0; |
| 967 | } |
| 968 | |
| 969 | void nvmf_free_options(struct nvmf_ctrl_options *opts) |
| 970 | { |
| 971 | nvmf_host_put(opts->host); |
| 972 | kfree(opts->transport); |
| 973 | kfree(opts->traddr); |
| 974 | kfree(opts->trsvcid); |
| 975 | kfree(opts->subsysnqn); |
James Smart | 478bcb9 | 2016-08-02 10:42:10 +0300 | [diff] [blame] | 976 | kfree(opts->host_traddr); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 977 | kfree(opts); |
| 978 | } |
| 979 | EXPORT_SYMBOL_GPL(nvmf_free_options); |
| 980 | |
| 981 | #define NVMF_REQUIRED_OPTS (NVMF_OPT_TRANSPORT | NVMF_OPT_NQN) |
| 982 | #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] | 983 | NVMF_OPT_KATO | NVMF_OPT_HOSTNQN | \ |
Sagi Grimberg | 8154ed7 | 2018-11-19 14:11:15 -0800 | [diff] [blame] | 984 | NVMF_OPT_HOST_ID | NVMF_OPT_DUP_CONNECT |\ |
| 985 | NVMF_OPT_DISABLE_SQFLOW) |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 986 | |
| 987 | static struct nvme_ctrl * |
| 988 | nvmf_create_ctrl(struct device *dev, const char *buf, size_t count) |
| 989 | { |
| 990 | struct nvmf_ctrl_options *opts; |
| 991 | struct nvmf_transport_ops *ops; |
| 992 | struct nvme_ctrl *ctrl; |
| 993 | int ret; |
| 994 | |
| 995 | opts = kzalloc(sizeof(*opts), GFP_KERNEL); |
| 996 | if (!opts) |
| 997 | return ERR_PTR(-ENOMEM); |
| 998 | |
| 999 | ret = nvmf_parse_options(opts, buf); |
| 1000 | if (ret) |
| 1001 | goto out_free_opts; |
| 1002 | |
Sagi Grimberg | d1f1071 | 2017-09-24 16:15:55 +0300 | [diff] [blame] | 1003 | |
| 1004 | request_module("nvme-%s", opts->transport); |
| 1005 | |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 1006 | /* |
| 1007 | * Check the generic options first as we need a valid transport for |
| 1008 | * the lookup below. Then clear the generic flags so that transport |
| 1009 | * drivers don't have to care about them. |
| 1010 | */ |
| 1011 | ret = nvmf_check_required_opts(opts, NVMF_REQUIRED_OPTS); |
| 1012 | if (ret) |
| 1013 | goto out_free_opts; |
| 1014 | opts->mask &= ~NVMF_REQUIRED_OPTS; |
| 1015 | |
Roland Dreier | 489beb9 | 2017-08-29 10:33:44 -0700 | [diff] [blame] | 1016 | down_read(&nvmf_transports_rwsem); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 1017 | ops = nvmf_lookup_transport(opts); |
| 1018 | if (!ops) { |
| 1019 | pr_info("no handler found for transport %s.\n", |
| 1020 | opts->transport); |
| 1021 | ret = -EINVAL; |
| 1022 | goto out_unlock; |
| 1023 | } |
| 1024 | |
Roy Shterman | 0de5cd3 | 2017-12-25 14:18:30 +0200 | [diff] [blame] | 1025 | if (!try_module_get(ops->module)) { |
| 1026 | ret = -EBUSY; |
| 1027 | goto out_unlock; |
| 1028 | } |
Johannes Thumshirn | 12a0b66 | 2018-06-01 09:11:20 +0200 | [diff] [blame] | 1029 | up_read(&nvmf_transports_rwsem); |
Roy Shterman | 0de5cd3 | 2017-12-25 14:18:30 +0200 | [diff] [blame] | 1030 | |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 1031 | ret = nvmf_check_required_opts(opts, ops->required_opts); |
| 1032 | if (ret) |
Roy Shterman | 0de5cd3 | 2017-12-25 14:18:30 +0200 | [diff] [blame] | 1033 | goto out_module_put; |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 1034 | ret = nvmf_check_allowed_opts(opts, NVMF_ALLOWED_OPTS | |
| 1035 | ops->allowed_opts | ops->required_opts); |
| 1036 | if (ret) |
Roy Shterman | 0de5cd3 | 2017-12-25 14:18:30 +0200 | [diff] [blame] | 1037 | goto out_module_put; |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 1038 | |
| 1039 | ctrl = ops->create_ctrl(dev, opts); |
| 1040 | if (IS_ERR(ctrl)) { |
| 1041 | ret = PTR_ERR(ctrl); |
Roy Shterman | 0de5cd3 | 2017-12-25 14:18:30 +0200 | [diff] [blame] | 1042 | goto out_module_put; |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 1043 | } |
| 1044 | |
Roy Shterman | 0de5cd3 | 2017-12-25 14:18:30 +0200 | [diff] [blame] | 1045 | module_put(ops->module); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 1046 | return ctrl; |
| 1047 | |
Roy Shterman | 0de5cd3 | 2017-12-25 14:18:30 +0200 | [diff] [blame] | 1048 | out_module_put: |
| 1049 | module_put(ops->module); |
Johannes Thumshirn | 12a0b66 | 2018-06-01 09:11:20 +0200 | [diff] [blame] | 1050 | goto out_free_opts; |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 1051 | out_unlock: |
Roland Dreier | 489beb9 | 2017-08-29 10:33:44 -0700 | [diff] [blame] | 1052 | up_read(&nvmf_transports_rwsem); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 1053 | out_free_opts: |
Bart Van Assche | f3116d8 | 2016-10-18 13:11:03 -0700 | [diff] [blame] | 1054 | nvmf_free_options(opts); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 1055 | return ERR_PTR(ret); |
| 1056 | } |
| 1057 | |
| 1058 | static struct class *nvmf_class; |
| 1059 | static struct device *nvmf_device; |
| 1060 | static DEFINE_MUTEX(nvmf_dev_mutex); |
| 1061 | |
| 1062 | static ssize_t nvmf_dev_write(struct file *file, const char __user *ubuf, |
| 1063 | size_t count, loff_t *pos) |
| 1064 | { |
| 1065 | struct seq_file *seq_file = file->private_data; |
| 1066 | struct nvme_ctrl *ctrl; |
| 1067 | const char *buf; |
| 1068 | int ret = 0; |
| 1069 | |
| 1070 | if (count > PAGE_SIZE) |
| 1071 | return -ENOMEM; |
| 1072 | |
| 1073 | buf = memdup_user_nul(ubuf, count); |
| 1074 | if (IS_ERR(buf)) |
| 1075 | return PTR_ERR(buf); |
| 1076 | |
| 1077 | mutex_lock(&nvmf_dev_mutex); |
| 1078 | if (seq_file->private) { |
| 1079 | ret = -EINVAL; |
| 1080 | goto out_unlock; |
| 1081 | } |
| 1082 | |
| 1083 | ctrl = nvmf_create_ctrl(nvmf_device, buf, count); |
| 1084 | if (IS_ERR(ctrl)) { |
| 1085 | ret = PTR_ERR(ctrl); |
| 1086 | goto out_unlock; |
| 1087 | } |
| 1088 | |
| 1089 | seq_file->private = ctrl; |
| 1090 | |
| 1091 | out_unlock: |
| 1092 | mutex_unlock(&nvmf_dev_mutex); |
| 1093 | kfree(buf); |
| 1094 | return ret ? ret : count; |
| 1095 | } |
| 1096 | |
| 1097 | static int nvmf_dev_show(struct seq_file *seq_file, void *private) |
| 1098 | { |
| 1099 | struct nvme_ctrl *ctrl; |
| 1100 | int ret = 0; |
| 1101 | |
| 1102 | mutex_lock(&nvmf_dev_mutex); |
| 1103 | ctrl = seq_file->private; |
| 1104 | if (!ctrl) { |
| 1105 | ret = -EINVAL; |
| 1106 | goto out_unlock; |
| 1107 | } |
| 1108 | |
| 1109 | seq_printf(seq_file, "instance=%d,cntlid=%d\n", |
| 1110 | ctrl->instance, ctrl->cntlid); |
| 1111 | |
| 1112 | out_unlock: |
| 1113 | mutex_unlock(&nvmf_dev_mutex); |
| 1114 | return ret; |
| 1115 | } |
| 1116 | |
| 1117 | static int nvmf_dev_open(struct inode *inode, struct file *file) |
| 1118 | { |
| 1119 | /* |
| 1120 | * The miscdevice code initializes file->private_data, but doesn't |
| 1121 | * make use of it later. |
| 1122 | */ |
| 1123 | file->private_data = NULL; |
| 1124 | return single_open(file, nvmf_dev_show, NULL); |
| 1125 | } |
| 1126 | |
| 1127 | static int nvmf_dev_release(struct inode *inode, struct file *file) |
| 1128 | { |
| 1129 | struct seq_file *seq_file = file->private_data; |
| 1130 | struct nvme_ctrl *ctrl = seq_file->private; |
| 1131 | |
| 1132 | if (ctrl) |
| 1133 | nvme_put_ctrl(ctrl); |
| 1134 | return single_release(inode, file); |
| 1135 | } |
| 1136 | |
| 1137 | static const struct file_operations nvmf_dev_fops = { |
| 1138 | .owner = THIS_MODULE, |
| 1139 | .write = nvmf_dev_write, |
| 1140 | .read = seq_read, |
| 1141 | .open = nvmf_dev_open, |
| 1142 | .release = nvmf_dev_release, |
| 1143 | }; |
| 1144 | |
| 1145 | static struct miscdevice nvmf_misc = { |
| 1146 | .minor = MISC_DYNAMIC_MINOR, |
| 1147 | .name = "nvme-fabrics", |
| 1148 | .fops = &nvmf_dev_fops, |
| 1149 | }; |
| 1150 | |
| 1151 | static int __init nvmf_init(void) |
| 1152 | { |
| 1153 | int ret; |
| 1154 | |
| 1155 | nvmf_default_host = nvmf_host_default(); |
| 1156 | if (!nvmf_default_host) |
| 1157 | return -ENOMEM; |
| 1158 | |
| 1159 | nvmf_class = class_create(THIS_MODULE, "nvme-fabrics"); |
| 1160 | if (IS_ERR(nvmf_class)) { |
| 1161 | pr_err("couldn't register class nvme-fabrics\n"); |
| 1162 | ret = PTR_ERR(nvmf_class); |
| 1163 | goto out_free_host; |
| 1164 | } |
| 1165 | |
| 1166 | nvmf_device = |
| 1167 | device_create(nvmf_class, NULL, MKDEV(0, 0), NULL, "ctl"); |
| 1168 | if (IS_ERR(nvmf_device)) { |
| 1169 | pr_err("couldn't create nvme-fabris device!\n"); |
| 1170 | ret = PTR_ERR(nvmf_device); |
| 1171 | goto out_destroy_class; |
| 1172 | } |
| 1173 | |
| 1174 | ret = misc_register(&nvmf_misc); |
| 1175 | if (ret) { |
| 1176 | pr_err("couldn't register misc device: %d\n", ret); |
| 1177 | goto out_destroy_device; |
| 1178 | } |
| 1179 | |
| 1180 | return 0; |
| 1181 | |
| 1182 | out_destroy_device: |
| 1183 | device_destroy(nvmf_class, MKDEV(0, 0)); |
| 1184 | out_destroy_class: |
| 1185 | class_destroy(nvmf_class); |
| 1186 | out_free_host: |
| 1187 | nvmf_host_put(nvmf_default_host); |
| 1188 | return ret; |
| 1189 | } |
| 1190 | |
| 1191 | static void __exit nvmf_exit(void) |
| 1192 | { |
| 1193 | misc_deregister(&nvmf_misc); |
| 1194 | device_destroy(nvmf_class, MKDEV(0, 0)); |
| 1195 | class_destroy(nvmf_class); |
| 1196 | nvmf_host_put(nvmf_default_host); |
| 1197 | |
| 1198 | BUILD_BUG_ON(sizeof(struct nvmf_connect_command) != 64); |
| 1199 | BUILD_BUG_ON(sizeof(struct nvmf_property_get_command) != 64); |
| 1200 | BUILD_BUG_ON(sizeof(struct nvmf_property_set_command) != 64); |
| 1201 | BUILD_BUG_ON(sizeof(struct nvmf_connect_data) != 1024); |
| 1202 | } |
| 1203 | |
| 1204 | MODULE_LICENSE("GPL v2"); |
| 1205 | |
| 1206 | module_init(nvmf_init); |
| 1207 | module_exit(nvmf_exit); |