Christoph Hellwig | 9002c4e | 2019-02-18 09:31:03 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 2 | /* |
| 3 | * NVMe over Fabrics common host code. |
| 4 | * Copyright (c) 2015-2016 HGST, a Western Digital Company. |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 5 | */ |
| 6 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 7 | #include <linux/init.h> |
| 8 | #include <linux/miscdevice.h> |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/mutex.h> |
| 11 | #include <linux/parser.h> |
| 12 | #include <linux/seq_file.h> |
| 13 | #include "nvme.h" |
| 14 | #include "fabrics.h" |
| 15 | |
| 16 | static LIST_HEAD(nvmf_transports); |
Roland Dreier | 489beb9 | 2017-08-29 10:33:44 -0700 | [diff] [blame] | 17 | static DECLARE_RWSEM(nvmf_transports_rwsem); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 18 | |
| 19 | static LIST_HEAD(nvmf_hosts); |
| 20 | static DEFINE_MUTEX(nvmf_hosts_mutex); |
| 21 | |
| 22 | static struct nvmf_host *nvmf_default_host; |
| 23 | |
| 24 | static struct nvmf_host *__nvmf_host_find(const char *hostnqn) |
| 25 | { |
| 26 | struct nvmf_host *host; |
| 27 | |
| 28 | list_for_each_entry(host, &nvmf_hosts, list) { |
| 29 | if (!strcmp(host->nqn, hostnqn)) |
| 30 | return host; |
| 31 | } |
| 32 | |
| 33 | return NULL; |
| 34 | } |
| 35 | |
| 36 | static struct nvmf_host *nvmf_host_add(const char *hostnqn) |
| 37 | { |
| 38 | struct nvmf_host *host; |
| 39 | |
| 40 | mutex_lock(&nvmf_hosts_mutex); |
| 41 | host = __nvmf_host_find(hostnqn); |
Christoph Hellwig | 98096d8 | 2016-08-18 11:16:35 -0700 | [diff] [blame] | 42 | if (host) { |
| 43 | kref_get(&host->ref); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 44 | goto out_unlock; |
Christoph Hellwig | 98096d8 | 2016-08-18 11:16:35 -0700 | [diff] [blame] | 45 | } |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 46 | |
| 47 | host = kmalloc(sizeof(*host), GFP_KERNEL); |
| 48 | if (!host) |
| 49 | goto out_unlock; |
| 50 | |
| 51 | kref_init(&host->ref); |
Hannes Reinecke | 1e5f446 | 2018-05-25 11:04:03 +0200 | [diff] [blame] | 52 | strlcpy(host->nqn, hostnqn, NVMF_NQN_SIZE); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 53 | |
| 54 | list_add_tail(&host->list, &nvmf_hosts); |
| 55 | out_unlock: |
| 56 | mutex_unlock(&nvmf_hosts_mutex); |
| 57 | return host; |
| 58 | } |
| 59 | |
| 60 | static struct nvmf_host *nvmf_host_default(void) |
| 61 | { |
| 62 | struct nvmf_host *host; |
| 63 | |
| 64 | host = kmalloc(sizeof(*host), GFP_KERNEL); |
| 65 | if (!host) |
| 66 | return NULL; |
| 67 | |
| 68 | kref_init(&host->ref); |
Ewan D. Milne | 6b01823 | 2018-01-05 12:44:06 -0500 | [diff] [blame] | 69 | uuid_gen(&host->id); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 70 | snprintf(host->nqn, NVMF_NQN_SIZE, |
Daniel Verkamp | 40a5fce | 2017-08-30 15:18:19 -0700 | [diff] [blame] | 71 | "nqn.2014-08.org.nvmexpress:uuid:%pUb", &host->id); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 72 | |
| 73 | mutex_lock(&nvmf_hosts_mutex); |
| 74 | list_add_tail(&host->list, &nvmf_hosts); |
| 75 | mutex_unlock(&nvmf_hosts_mutex); |
| 76 | |
| 77 | return host; |
| 78 | } |
| 79 | |
| 80 | static void nvmf_host_destroy(struct kref *ref) |
| 81 | { |
| 82 | struct nvmf_host *host = container_of(ref, struct nvmf_host, ref); |
| 83 | |
Ming Lin | e76debd | 2016-07-01 12:13:32 -0700 | [diff] [blame] | 84 | mutex_lock(&nvmf_hosts_mutex); |
| 85 | list_del(&host->list); |
| 86 | mutex_unlock(&nvmf_hosts_mutex); |
| 87 | |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 88 | kfree(host); |
| 89 | } |
| 90 | |
| 91 | static void nvmf_host_put(struct nvmf_host *host) |
| 92 | { |
| 93 | if (host) |
| 94 | kref_put(&host->ref, nvmf_host_destroy); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * nvmf_get_address() - Get address/port |
| 99 | * @ctrl: Host NVMe controller instance which we got the address |
| 100 | * @buf: OUTPUT parameter that will contain the address/port |
| 101 | * @size: buffer size |
| 102 | */ |
| 103 | int nvmf_get_address(struct nvme_ctrl *ctrl, char *buf, int size) |
| 104 | { |
James Smart | 0fe51ff | 2016-08-02 10:40:01 +0300 | [diff] [blame] | 105 | int len = 0; |
| 106 | |
| 107 | if (ctrl->opts->mask & NVMF_OPT_TRADDR) |
Takashi Iwai | 8d8a50e | 2020-03-11 09:50:37 +0100 | [diff] [blame] | 108 | len += scnprintf(buf, size, "traddr=%s", ctrl->opts->traddr); |
James Smart | 0fe51ff | 2016-08-02 10:40:01 +0300 | [diff] [blame] | 109 | if (ctrl->opts->mask & NVMF_OPT_TRSVCID) |
Takashi Iwai | 8d8a50e | 2020-03-11 09:50:37 +0100 | [diff] [blame] | 110 | len += scnprintf(buf + len, size - len, "%strsvcid=%s", |
James Smart | 0fe51ff | 2016-08-02 10:40:01 +0300 | [diff] [blame] | 111 | (len) ? "," : "", ctrl->opts->trsvcid); |
James Smart | 478bcb9 | 2016-08-02 10:42:10 +0300 | [diff] [blame] | 112 | if (ctrl->opts->mask & NVMF_OPT_HOST_TRADDR) |
Takashi Iwai | 8d8a50e | 2020-03-11 09:50:37 +0100 | [diff] [blame] | 113 | len += scnprintf(buf + len, size - len, "%shost_traddr=%s", |
James Smart | 478bcb9 | 2016-08-02 10:42:10 +0300 | [diff] [blame] | 114 | (len) ? "," : "", ctrl->opts->host_traddr); |
Takashi Iwai | 8d8a50e | 2020-03-11 09:50:37 +0100 | [diff] [blame] | 115 | len += scnprintf(buf + len, size - len, "\n"); |
James Smart | 0fe51ff | 2016-08-02 10:40:01 +0300 | [diff] [blame] | 116 | |
| 117 | return len; |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 118 | } |
| 119 | EXPORT_SYMBOL_GPL(nvmf_get_address); |
| 120 | |
| 121 | /** |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 122 | * nvmf_reg_read32() - NVMe Fabrics "Property Get" API function. |
| 123 | * @ctrl: Host NVMe controller instance maintaining the admin |
| 124 | * queue used to submit the property read command to |
| 125 | * the allocated NVMe controller resource on the target system. |
| 126 | * @off: Starting offset value of the targeted property |
| 127 | * register (see the fabrics section of the NVMe standard). |
| 128 | * @val: OUTPUT parameter that will contain the value of |
| 129 | * the property after a successful read. |
| 130 | * |
| 131 | * Used by the host system to retrieve a 32-bit capsule property value |
| 132 | * from an NVMe controller on the target system. |
| 133 | * |
| 134 | * ("Capsule property" is an "PCIe register concept" applied to the |
| 135 | * NVMe fabrics space.) |
| 136 | * |
| 137 | * Return: |
| 138 | * 0: successful read |
| 139 | * > 0: NVMe error status code |
| 140 | * < 0: Linux errno error code |
| 141 | */ |
| 142 | int nvmf_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val) |
| 143 | { |
| 144 | struct nvme_command cmd; |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 145 | union nvme_result res; |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 146 | int ret; |
| 147 | |
| 148 | memset(&cmd, 0, sizeof(cmd)); |
| 149 | cmd.prop_get.opcode = nvme_fabrics_command; |
| 150 | cmd.prop_get.fctype = nvme_fabrics_type_property_get; |
| 151 | cmd.prop_get.offset = cpu_to_le32(off); |
| 152 | |
Sagi Grimberg | e7832cb | 2019-08-02 19:33:59 -0700 | [diff] [blame] | 153 | ret = __nvme_submit_sync_cmd(ctrl->fabrics_q, &cmd, &res, NULL, 0, 0, |
Sagi Grimberg | 6287b51 | 2018-12-14 11:06:07 -0800 | [diff] [blame] | 154 | NVME_QID_ANY, 0, 0, false); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 155 | |
| 156 | if (ret >= 0) |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 157 | *val = le64_to_cpu(res.u64); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 158 | if (unlikely(ret != 0)) |
| 159 | dev_err(ctrl->device, |
| 160 | "Property Get error: %d, offset %#x\n", |
| 161 | ret > 0 ? ret & ~NVME_SC_DNR : ret, off); |
| 162 | |
| 163 | return ret; |
| 164 | } |
| 165 | EXPORT_SYMBOL_GPL(nvmf_reg_read32); |
| 166 | |
| 167 | /** |
| 168 | * nvmf_reg_read64() - NVMe Fabrics "Property Get" API function. |
| 169 | * @ctrl: Host NVMe controller instance maintaining the admin |
| 170 | * queue used to submit the property read command to |
| 171 | * the allocated controller resource on the target system. |
| 172 | * @off: Starting offset value of the targeted property |
| 173 | * register (see the fabrics section of the NVMe standard). |
| 174 | * @val: OUTPUT parameter that will contain the value of |
| 175 | * the property after a successful read. |
| 176 | * |
| 177 | * Used by the host system to retrieve a 64-bit capsule property value |
| 178 | * from an NVMe controller on the target system. |
| 179 | * |
| 180 | * ("Capsule property" is an "PCIe register concept" applied to the |
| 181 | * NVMe fabrics space.) |
| 182 | * |
| 183 | * Return: |
| 184 | * 0: successful read |
| 185 | * > 0: NVMe error status code |
| 186 | * < 0: Linux errno error code |
| 187 | */ |
| 188 | int nvmf_reg_read64(struct nvme_ctrl *ctrl, u32 off, u64 *val) |
| 189 | { |
| 190 | struct nvme_command cmd; |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 191 | union nvme_result res; |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 192 | int ret; |
| 193 | |
| 194 | memset(&cmd, 0, sizeof(cmd)); |
| 195 | cmd.prop_get.opcode = nvme_fabrics_command; |
| 196 | cmd.prop_get.fctype = nvme_fabrics_type_property_get; |
| 197 | cmd.prop_get.attrib = 1; |
| 198 | cmd.prop_get.offset = cpu_to_le32(off); |
| 199 | |
Sagi Grimberg | e7832cb | 2019-08-02 19:33:59 -0700 | [diff] [blame] | 200 | ret = __nvme_submit_sync_cmd(ctrl->fabrics_q, &cmd, &res, NULL, 0, 0, |
Sagi Grimberg | 6287b51 | 2018-12-14 11:06:07 -0800 | [diff] [blame] | 201 | NVME_QID_ANY, 0, 0, false); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 202 | |
| 203 | if (ret >= 0) |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 204 | *val = le64_to_cpu(res.u64); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 205 | if (unlikely(ret != 0)) |
| 206 | dev_err(ctrl->device, |
| 207 | "Property Get error: %d, offset %#x\n", |
| 208 | ret > 0 ? ret & ~NVME_SC_DNR : ret, off); |
| 209 | return ret; |
| 210 | } |
| 211 | EXPORT_SYMBOL_GPL(nvmf_reg_read64); |
| 212 | |
| 213 | /** |
| 214 | * nvmf_reg_write32() - NVMe Fabrics "Property Write" API function. |
| 215 | * @ctrl: Host NVMe controller instance maintaining the admin |
| 216 | * queue used to submit the property read command to |
| 217 | * the allocated NVMe controller resource on the target system. |
| 218 | * @off: Starting offset value of the targeted property |
| 219 | * register (see the fabrics section of the NVMe standard). |
| 220 | * @val: Input parameter that contains the value to be |
| 221 | * written to the property. |
| 222 | * |
| 223 | * Used by the NVMe host system to write a 32-bit capsule property value |
| 224 | * to an NVMe controller on the target system. |
| 225 | * |
| 226 | * ("Capsule property" is an "PCIe register concept" applied to the |
| 227 | * NVMe fabrics space.) |
| 228 | * |
| 229 | * Return: |
| 230 | * 0: successful write |
| 231 | * > 0: NVMe error status code |
| 232 | * < 0: Linux errno error code |
| 233 | */ |
| 234 | int nvmf_reg_write32(struct nvme_ctrl *ctrl, u32 off, u32 val) |
| 235 | { |
| 236 | struct nvme_command cmd; |
| 237 | int ret; |
| 238 | |
| 239 | memset(&cmd, 0, sizeof(cmd)); |
| 240 | cmd.prop_set.opcode = nvme_fabrics_command; |
| 241 | cmd.prop_set.fctype = nvme_fabrics_type_property_set; |
| 242 | cmd.prop_set.attrib = 0; |
| 243 | cmd.prop_set.offset = cpu_to_le32(off); |
| 244 | cmd.prop_set.value = cpu_to_le64(val); |
| 245 | |
Sagi Grimberg | e7832cb | 2019-08-02 19:33:59 -0700 | [diff] [blame] | 246 | ret = __nvme_submit_sync_cmd(ctrl->fabrics_q, &cmd, NULL, NULL, 0, 0, |
Sagi Grimberg | 6287b51 | 2018-12-14 11:06:07 -0800 | [diff] [blame] | 247 | NVME_QID_ANY, 0, 0, false); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 248 | if (unlikely(ret)) |
| 249 | dev_err(ctrl->device, |
| 250 | "Property Set error: %d, offset %#x\n", |
| 251 | ret > 0 ? ret & ~NVME_SC_DNR : ret, off); |
| 252 | return ret; |
| 253 | } |
| 254 | EXPORT_SYMBOL_GPL(nvmf_reg_write32); |
| 255 | |
| 256 | /** |
| 257 | * nvmf_log_connect_error() - Error-parsing-diagnostic print |
| 258 | * out function for connect() errors. |
| 259 | * |
| 260 | * @ctrl: the specific /dev/nvmeX device that had the error. |
| 261 | * |
| 262 | * @errval: Error code to be decoded in a more human-friendly |
| 263 | * printout. |
| 264 | * |
| 265 | * @offset: For use with the NVMe error code NVME_SC_CONNECT_INVALID_PARAM. |
| 266 | * |
| 267 | * @cmd: This is the SQE portion of a submission capsule. |
| 268 | * |
| 269 | * @data: This is the "Data" portion of a submission capsule. |
| 270 | */ |
| 271 | static void nvmf_log_connect_error(struct nvme_ctrl *ctrl, |
| 272 | int errval, int offset, struct nvme_command *cmd, |
| 273 | struct nvmf_connect_data *data) |
| 274 | { |
| 275 | int err_sctype = errval & (~NVME_SC_DNR); |
| 276 | |
| 277 | switch (err_sctype) { |
| 278 | |
| 279 | case (NVME_SC_CONNECT_INVALID_PARAM): |
| 280 | if (offset >> 16) { |
| 281 | char *inv_data = "Connect Invalid Data Parameter"; |
| 282 | |
| 283 | switch (offset & 0xffff) { |
| 284 | case (offsetof(struct nvmf_connect_data, cntlid)): |
| 285 | dev_err(ctrl->device, |
| 286 | "%s, cntlid: %d\n", |
| 287 | inv_data, data->cntlid); |
| 288 | break; |
| 289 | case (offsetof(struct nvmf_connect_data, hostnqn)): |
| 290 | dev_err(ctrl->device, |
| 291 | "%s, hostnqn \"%s\"\n", |
| 292 | inv_data, data->hostnqn); |
| 293 | break; |
| 294 | case (offsetof(struct nvmf_connect_data, subsysnqn)): |
| 295 | dev_err(ctrl->device, |
| 296 | "%s, subsysnqn \"%s\"\n", |
| 297 | inv_data, data->subsysnqn); |
| 298 | break; |
| 299 | default: |
| 300 | dev_err(ctrl->device, |
| 301 | "%s, starting byte offset: %d\n", |
| 302 | inv_data, offset & 0xffff); |
| 303 | break; |
| 304 | } |
| 305 | } else { |
| 306 | char *inv_sqe = "Connect Invalid SQE Parameter"; |
| 307 | |
| 308 | switch (offset) { |
| 309 | case (offsetof(struct nvmf_connect_command, qid)): |
| 310 | dev_err(ctrl->device, |
| 311 | "%s, qid %d\n", |
| 312 | inv_sqe, cmd->connect.qid); |
| 313 | break; |
| 314 | default: |
| 315 | dev_err(ctrl->device, |
| 316 | "%s, starting byte offset: %d\n", |
| 317 | inv_sqe, offset); |
| 318 | } |
| 319 | } |
| 320 | break; |
Guan Junxiong | 97ddc36e | 2017-06-13 10:51:24 +0800 | [diff] [blame] | 321 | |
| 322 | case NVME_SC_CONNECT_INVALID_HOST: |
| 323 | dev_err(ctrl->device, |
| 324 | "Connect for subsystem %s is not allowed, hostnqn: %s\n", |
| 325 | data->subsysnqn, data->hostnqn); |
| 326 | break; |
| 327 | |
| 328 | case NVME_SC_CONNECT_CTRL_BUSY: |
| 329 | dev_err(ctrl->device, |
| 330 | "Connect command failed: controller is busy or not available\n"); |
| 331 | break; |
| 332 | |
| 333 | case NVME_SC_CONNECT_FORMAT: |
| 334 | dev_err(ctrl->device, |
| 335 | "Connect incompatible format: %d", |
| 336 | cmd->connect.recfmt); |
| 337 | break; |
| 338 | |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 339 | default: |
| 340 | dev_err(ctrl->device, |
| 341 | "Connect command failed, error wo/DNR bit: %d\n", |
| 342 | err_sctype); |
| 343 | break; |
| 344 | } /* switch (err_sctype) */ |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * nvmf_connect_admin_queue() - NVMe Fabrics Admin Queue "Connect" |
| 349 | * API function. |
| 350 | * @ctrl: Host nvme controller instance used to request |
| 351 | * a new NVMe controller allocation on the target |
| 352 | * system and establish an NVMe Admin connection to |
| 353 | * that controller. |
| 354 | * |
| 355 | * This function enables an NVMe host device to request a new allocation of |
| 356 | * an NVMe controller resource on a target system as well establish a |
| 357 | * fabrics-protocol connection of the NVMe Admin queue between the |
| 358 | * host system device and the allocated NVMe controller on the |
| 359 | * target system via a NVMe Fabrics "Connect" command. |
| 360 | * |
| 361 | * Return: |
| 362 | * 0: success |
| 363 | * > 0: NVMe error status code |
| 364 | * < 0: Linux errno error code |
| 365 | * |
| 366 | */ |
| 367 | int nvmf_connect_admin_queue(struct nvme_ctrl *ctrl) |
| 368 | { |
| 369 | struct nvme_command cmd; |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 370 | union nvme_result res; |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 371 | struct nvmf_connect_data *data; |
| 372 | int ret; |
| 373 | |
| 374 | memset(&cmd, 0, sizeof(cmd)); |
| 375 | cmd.connect.opcode = nvme_fabrics_command; |
| 376 | cmd.connect.fctype = nvme_fabrics_type_connect; |
| 377 | cmd.connect.qid = 0; |
Sagi Grimberg | 7aa1f42 | 2017-06-18 16:15:59 +0300 | [diff] [blame] | 378 | cmd.connect.sqsize = cpu_to_le16(NVME_AQ_DEPTH - 1); |
Jay Freyensee | f994d9d | 2016-08-17 15:00:26 -0700 | [diff] [blame] | 379 | |
Sagi Grimberg | 038bd4c | 2016-06-13 16:45:28 +0200 | [diff] [blame] | 380 | /* |
| 381 | * Set keep-alive timeout in seconds granularity (ms * 1000) |
| 382 | * and add a grace period for controller kato enforcement |
| 383 | */ |
Sagi Grimberg | 2d352df | 2019-07-12 11:02:09 -0700 | [diff] [blame] | 384 | cmd.connect.kato = ctrl->kato ? |
| 385 | cpu_to_le32((ctrl->kato + NVME_KATO_GRACE) * 1000) : 0; |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 386 | |
Sagi Grimberg | 8154ed7 | 2018-11-19 14:11:15 -0800 | [diff] [blame] | 387 | if (ctrl->opts->disable_sqflow) |
| 388 | cmd.connect.cattr |= NVME_CONNECT_DISABLE_SQFLOW; |
| 389 | |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 390 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 391 | if (!data) |
| 392 | return -ENOMEM; |
| 393 | |
Christoph Hellwig | 8e41226 | 2017-05-17 09:54:27 +0200 | [diff] [blame] | 394 | uuid_copy(&data->hostid, &ctrl->opts->host->id); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 395 | data->cntlid = cpu_to_le16(0xffff); |
| 396 | strncpy(data->subsysnqn, ctrl->opts->subsysnqn, NVMF_NQN_SIZE); |
| 397 | strncpy(data->hostnqn, ctrl->opts->host->nqn, NVMF_NQN_SIZE); |
| 398 | |
Sagi Grimberg | e7832cb | 2019-08-02 19:33:59 -0700 | [diff] [blame] | 399 | ret = __nvme_submit_sync_cmd(ctrl->fabrics_q, &cmd, &res, |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 400 | data, sizeof(*data), 0, NVME_QID_ANY, 1, |
Sagi Grimberg | 6287b51 | 2018-12-14 11:06:07 -0800 | [diff] [blame] | 401 | BLK_MQ_REQ_RESERVED | BLK_MQ_REQ_NOWAIT, false); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 402 | if (ret) { |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 403 | nvmf_log_connect_error(ctrl, ret, le32_to_cpu(res.u32), |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 404 | &cmd, data); |
| 405 | goto out_free_data; |
| 406 | } |
| 407 | |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 408 | ctrl->cntlid = le16_to_cpu(res.u16); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 409 | |
| 410 | out_free_data: |
| 411 | kfree(data); |
| 412 | return ret; |
| 413 | } |
| 414 | EXPORT_SYMBOL_GPL(nvmf_connect_admin_queue); |
| 415 | |
| 416 | /** |
| 417 | * nvmf_connect_io_queue() - NVMe Fabrics I/O Queue "Connect" |
| 418 | * API function. |
| 419 | * @ctrl: Host nvme controller instance used to establish an |
| 420 | * NVMe I/O queue connection to the already allocated NVMe |
| 421 | * controller on the target system. |
| 422 | * @qid: NVMe I/O queue number for the new I/O connection between |
| 423 | * host and target (note qid == 0 is illegal as this is |
| 424 | * the Admin queue, per NVMe standard). |
Bart Van Assche | a467fc5 | 2019-02-14 14:50:53 -0800 | [diff] [blame] | 425 | * @poll: Whether or not to poll for the completion of the connect cmd. |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 426 | * |
| 427 | * This function issues a fabrics-protocol connection |
| 428 | * of a NVMe I/O queue (via NVMe Fabrics "Connect" command) |
| 429 | * between the host system device and the allocated NVMe controller |
| 430 | * on the target system. |
| 431 | * |
| 432 | * Return: |
| 433 | * 0: success |
| 434 | * > 0: NVMe error status code |
| 435 | * < 0: Linux errno error code |
| 436 | */ |
Sagi Grimberg | 26c6822 | 2018-12-14 11:06:08 -0800 | [diff] [blame] | 437 | 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] | 438 | { |
| 439 | struct nvme_command cmd; |
| 440 | struct nvmf_connect_data *data; |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 441 | union nvme_result res; |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 442 | int ret; |
| 443 | |
| 444 | memset(&cmd, 0, sizeof(cmd)); |
| 445 | cmd.connect.opcode = nvme_fabrics_command; |
| 446 | cmd.connect.fctype = nvme_fabrics_type_connect; |
| 447 | cmd.connect.qid = cpu_to_le16(qid); |
| 448 | cmd.connect.sqsize = cpu_to_le16(ctrl->sqsize); |
| 449 | |
Sagi Grimberg | 8154ed7 | 2018-11-19 14:11:15 -0800 | [diff] [blame] | 450 | if (ctrl->opts->disable_sqflow) |
| 451 | cmd.connect.cattr |= NVME_CONNECT_DISABLE_SQFLOW; |
| 452 | |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 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, |
Sagi Grimberg | 26c6822 | 2018-12-14 11:06:08 -0800 | [diff] [blame] | 464 | BLK_MQ_REQ_RESERVED | BLK_MQ_REQ_NOWAIT, poll); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 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 | { |
Tal Shorer | 66414e8 | 2018-08-07 23:42:39 +0300 | [diff] [blame] | 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 | { |
Christoph Hellwig | 5a1e595 | 2018-02-22 07:24:08 -0800 | [diff] [blame] | 495 | if (!ops->create_ctrl) |
Johannes Thumshirn | e5a39dd | 2017-01-27 09:03:45 +0100 | [diff] [blame] | 496 | return -EINVAL; |
| 497 | |
Roland Dreier | 489beb9 | 2017-08-29 10:33:44 -0700 | [diff] [blame] | 498 | down_write(&nvmf_transports_rwsem); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 499 | list_add_tail(&ops->entry, &nvmf_transports); |
Roland Dreier | 489beb9 | 2017-08-29 10:33:44 -0700 | [diff] [blame] | 500 | up_write(&nvmf_transports_rwsem); |
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 | { |
Roland Dreier | 489beb9 | 2017-08-29 10:33:44 -0700 | [diff] [blame] | 517 | down_write(&nvmf_transports_rwsem); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 518 | list_del(&ops->entry); |
Roland Dreier | 489beb9 | 2017-08-29 10:33:44 -0700 | [diff] [blame] | 519 | up_write(&nvmf_transports_rwsem); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 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 | |
Roland Dreier | 489beb9 | 2017-08-29 10:33:44 -0700 | [diff] [blame] | 528 | lockdep_assert_held(&nvmf_transports_rwsem); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 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 | |
Christoph Hellwig | 3bc32bb | 2018-06-11 17:34:06 +0200 | [diff] [blame] | 538 | /* |
| 539 | * For something we're not in a state to send to the device the default action |
| 540 | * 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] | 541 | * if the controller is deleting or if anything is marked for failfast or |
| 542 | * nvme multipath it is immediately failed. |
Christoph Hellwig | 3bc32bb | 2018-06-11 17:34:06 +0200 | [diff] [blame] | 543 | * |
| 544 | * Note: commands used to initialize the controller will be marked for failfast. |
| 545 | * Note: nvme cli/ioctl commands are marked for failfast. |
| 546 | */ |
James Smart | 6cdefc6 | 2018-07-20 15:49:48 -0700 | [diff] [blame] | 547 | blk_status_t nvmf_fail_nonready_command(struct nvme_ctrl *ctrl, |
| 548 | struct request *rq) |
Christoph Hellwig | 3bc32bb | 2018-06-11 17:34:06 +0200 | [diff] [blame] | 549 | { |
Sagi Grimberg | ecca390e | 2020-07-22 16:32:19 -0700 | [diff] [blame] | 550 | if (ctrl->state != NVME_CTRL_DELETING_NOIO && |
James Smart | 6cdefc6 | 2018-07-20 15:49:48 -0700 | [diff] [blame] | 551 | ctrl->state != NVME_CTRL_DEAD && |
Victor Gladkov | 8c4dfea | 2020-11-24 18:34:59 +0000 | [diff] [blame^] | 552 | !test_bit(NVME_CTRL_FAILFAST_EXPIRED, &ctrl->flags) && |
James Smart | 6cdefc6 | 2018-07-20 15:49:48 -0700 | [diff] [blame] | 553 | !blk_noretry_request(rq) && !(rq->cmd_flags & REQ_NVME_MPATH)) |
Christoph Hellwig | 3bc32bb | 2018-06-11 17:34:06 +0200 | [diff] [blame] | 554 | return BLK_STS_RESOURCE; |
James Smart | 783f4a4 | 2018-09-27 16:58:54 -0700 | [diff] [blame] | 555 | |
| 556 | nvme_req(rq)->status = NVME_SC_HOST_PATH_ERROR; |
| 557 | blk_mq_start_request(rq); |
| 558 | nvme_complete_rq(rq); |
| 559 | return BLK_STS_OK; |
Christoph Hellwig | 3bc32bb | 2018-06-11 17:34:06 +0200 | [diff] [blame] | 560 | } |
| 561 | EXPORT_SYMBOL_GPL(nvmf_fail_nonready_command); |
| 562 | |
| 563 | bool __nvmf_check_ready(struct nvme_ctrl *ctrl, struct request *rq, |
| 564 | bool queue_live) |
James Smart | bb06ec31 | 2018-04-12 09:16:15 -0600 | [diff] [blame] | 565 | { |
Christoph Hellwig | 35897b9 | 2018-06-11 17:41:11 +0200 | [diff] [blame] | 566 | struct nvme_request *req = nvme_req(rq); |
James Smart | bb06ec31 | 2018-04-12 09:16:15 -0600 | [diff] [blame] | 567 | |
Christoph Hellwig | 35897b9 | 2018-06-11 17:41:11 +0200 | [diff] [blame] | 568 | /* |
Sagi Grimberg | 73a5379 | 2020-09-08 12:56:08 -0700 | [diff] [blame] | 569 | * currently we have a problem sending passthru commands |
| 570 | * on the admin_q if the controller is not LIVE because we can't |
| 571 | * make sure that they are going out after the admin connect, |
| 572 | * controller enable and/or other commands in the initialization |
| 573 | * sequence. until the controller will be LIVE, fail with |
| 574 | * BLK_STS_RESOURCE so that they will be rescheduled. |
Christoph Hellwig | 35897b9 | 2018-06-11 17:41:11 +0200 | [diff] [blame] | 575 | */ |
Sagi Grimberg | 73a5379 | 2020-09-08 12:56:08 -0700 | [diff] [blame] | 576 | if (rq->q == ctrl->admin_q && (req->flags & NVME_REQ_USERCMD)) |
Christoph Hellwig | 35897b9 | 2018-06-11 17:41:11 +0200 | [diff] [blame] | 577 | return false; |
| 578 | |
| 579 | /* |
| 580 | * Only allow commands on a live queue, except for the connect command, |
| 581 | * which is require to set the queue live in the appropinquate states. |
| 582 | */ |
James Smart | bb06ec31 | 2018-04-12 09:16:15 -0600 | [diff] [blame] | 583 | switch (ctrl->state) { |
James Smart | bb06ec31 | 2018-04-12 09:16:15 -0600 | [diff] [blame] | 584 | case NVME_CTRL_CONNECTING: |
Sagi Grimberg | 73a5379 | 2020-09-08 12:56:08 -0700 | [diff] [blame] | 585 | if (blk_rq_is_passthrough(rq) && nvme_is_fabrics(req->cmd) && |
Christoph Hellwig | 35897b9 | 2018-06-11 17:41:11 +0200 | [diff] [blame] | 586 | req->cmd->fabrics.fctype == nvme_fabrics_type_connect) |
Christoph Hellwig | 3bc32bb | 2018-06-11 17:34:06 +0200 | [diff] [blame] | 587 | return true; |
Christoph Hellwig | 35897b9 | 2018-06-11 17:41:11 +0200 | [diff] [blame] | 588 | break; |
James Smart | bb06ec31 | 2018-04-12 09:16:15 -0600 | [diff] [blame] | 589 | default: |
Christoph Hellwig | 35897b9 | 2018-06-11 17:41:11 +0200 | [diff] [blame] | 590 | break; |
| 591 | case NVME_CTRL_DEAD: |
Christoph Hellwig | 3bc32bb | 2018-06-11 17:34:06 +0200 | [diff] [blame] | 592 | return false; |
James Smart | bb06ec31 | 2018-04-12 09:16:15 -0600 | [diff] [blame] | 593 | } |
Christoph Hellwig | 35897b9 | 2018-06-11 17:41:11 +0200 | [diff] [blame] | 594 | |
| 595 | return queue_live; |
James Smart | bb06ec31 | 2018-04-12 09:16:15 -0600 | [diff] [blame] | 596 | } |
Christoph Hellwig | 3bc32bb | 2018-06-11 17:34:06 +0200 | [diff] [blame] | 597 | EXPORT_SYMBOL_GPL(__nvmf_check_ready); |
James Smart | bb06ec31 | 2018-04-12 09:16:15 -0600 | [diff] [blame] | 598 | |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 599 | static const match_table_t opt_tokens = { |
| 600 | { NVMF_OPT_TRANSPORT, "transport=%s" }, |
| 601 | { NVMF_OPT_TRADDR, "traddr=%s" }, |
| 602 | { NVMF_OPT_TRSVCID, "trsvcid=%s" }, |
| 603 | { NVMF_OPT_NQN, "nqn=%s" }, |
| 604 | { NVMF_OPT_QUEUE_SIZE, "queue_size=%d" }, |
| 605 | { NVMF_OPT_NR_IO_QUEUES, "nr_io_queues=%d" }, |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 606 | { NVMF_OPT_RECONNECT_DELAY, "reconnect_delay=%d" }, |
Sagi Grimberg | 42a4527 | 2017-03-18 20:52:36 +0200 | [diff] [blame] | 607 | { NVMF_OPT_CTRL_LOSS_TMO, "ctrl_loss_tmo=%d" }, |
Sagi Grimberg | 038bd4c | 2016-06-13 16:45:28 +0200 | [diff] [blame] | 608 | { NVMF_OPT_KATO, "keep_alive_tmo=%d" }, |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 609 | { NVMF_OPT_HOSTNQN, "hostnqn=%s" }, |
James Smart | 478bcb9 | 2016-08-02 10:42:10 +0300 | [diff] [blame] | 610 | { NVMF_OPT_HOST_TRADDR, "host_traddr=%s" }, |
Johannes Thumshirn | 6bfe042 | 2017-06-20 14:23:01 +0200 | [diff] [blame] | 611 | { NVMF_OPT_HOST_ID, "hostid=%s" }, |
James Smart | 3b33876 | 2017-10-20 16:17:06 -0700 | [diff] [blame] | 612 | { NVMF_OPT_DUP_CONNECT, "duplicate_connect" }, |
Sagi Grimberg | 8154ed7 | 2018-11-19 14:11:15 -0800 | [diff] [blame] | 613 | { NVMF_OPT_DISABLE_SQFLOW, "disable_sqflow" }, |
Sagi Grimberg | 3b49fa8 | 2018-12-03 17:52:12 -0800 | [diff] [blame] | 614 | { NVMF_OPT_HDR_DIGEST, "hdr_digest" }, |
Sagi Grimberg | 20d44e8 | 2018-12-03 17:52:13 -0800 | [diff] [blame] | 615 | { NVMF_OPT_DATA_DIGEST, "data_digest" }, |
Sagi Grimberg | 330f6b8 | 2018-12-11 23:38:56 -0800 | [diff] [blame] | 616 | { NVMF_OPT_NR_WRITE_QUEUES, "nr_write_queues=%d" }, |
Sagi Grimberg | 89d4380 | 2018-12-14 11:06:09 -0800 | [diff] [blame] | 617 | { NVMF_OPT_NR_POLL_QUEUES, "nr_poll_queues=%d" }, |
Israel Rukshin | 52b4451 | 2019-08-18 12:08:51 +0300 | [diff] [blame] | 618 | { NVMF_OPT_TOS, "tos=%d" }, |
Victor Gladkov | 8c4dfea | 2020-11-24 18:34:59 +0000 | [diff] [blame^] | 619 | { NVMF_OPT_FAIL_FAST_TMO, "fast_io_fail_tmo=%d" }, |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 620 | { NVMF_OPT_ERR, NULL } |
| 621 | }; |
| 622 | |
| 623 | static int nvmf_parse_options(struct nvmf_ctrl_options *opts, |
| 624 | const char *buf) |
| 625 | { |
| 626 | substring_t args[MAX_OPT_ARGS]; |
| 627 | char *options, *o, *p; |
| 628 | int token, ret = 0; |
| 629 | size_t nqnlen = 0; |
Sagi Grimberg | 42a4527 | 2017-03-18 20:52:36 +0200 | [diff] [blame] | 630 | int ctrl_loss_tmo = NVMF_DEF_CTRL_LOSS_TMO; |
Johannes Thumshirn | 6bfe042 | 2017-06-20 14:23:01 +0200 | [diff] [blame] | 631 | uuid_t hostid; |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 632 | |
| 633 | /* Set defaults */ |
| 634 | opts->queue_size = NVMF_DEF_QUEUE_SIZE; |
| 635 | opts->nr_io_queues = num_online_cpus(); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 636 | opts->reconnect_delay = NVMF_DEF_RECONNECT_DELAY; |
Guilherme G. Piccoli | 8edd11c | 2017-09-14 13:59:28 -0300 | [diff] [blame] | 637 | opts->kato = NVME_DEFAULT_KATO; |
James Smart | 3b33876 | 2017-10-20 16:17:06 -0700 | [diff] [blame] | 638 | opts->duplicate_connect = false; |
Victor Gladkov | 8c4dfea | 2020-11-24 18:34:59 +0000 | [diff] [blame^] | 639 | opts->fast_io_fail_tmo = NVMF_DEF_FAIL_FAST_TMO; |
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; |
Israel Rukshin | 52b4451 | 2019-08-18 12:08:51 +0300 | [diff] [blame] | 642 | opts->tos = -1; /* < 0 == use transport default */ |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 643 | |
| 644 | options = o = kstrdup(buf, GFP_KERNEL); |
| 645 | if (!options) |
| 646 | return -ENOMEM; |
| 647 | |
Johannes Thumshirn | 6bfe042 | 2017-06-20 14:23:01 +0200 | [diff] [blame] | 648 | uuid_gen(&hostid); |
| 649 | |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 650 | while ((p = strsep(&o, ",\n")) != NULL) { |
| 651 | if (!*p) |
| 652 | continue; |
| 653 | |
| 654 | token = match_token(p, opt_tokens, args); |
| 655 | opts->mask |= token; |
| 656 | switch (token) { |
| 657 | case NVMF_OPT_TRANSPORT: |
| 658 | p = match_strdup(args); |
| 659 | if (!p) { |
| 660 | ret = -ENOMEM; |
| 661 | goto out; |
| 662 | } |
Chengguang Xu | 59a2f3f | 2018-04-14 20:06:19 +0800 | [diff] [blame] | 663 | kfree(opts->transport); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 664 | opts->transport = p; |
| 665 | break; |
| 666 | case NVMF_OPT_NQN: |
| 667 | p = match_strdup(args); |
| 668 | if (!p) { |
| 669 | ret = -ENOMEM; |
| 670 | goto out; |
| 671 | } |
Chengguang Xu | 59a2f3f | 2018-04-14 20:06:19 +0800 | [diff] [blame] | 672 | kfree(opts->subsysnqn); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 673 | opts->subsysnqn = p; |
| 674 | nqnlen = strlen(opts->subsysnqn); |
| 675 | if (nqnlen >= NVMF_NQN_SIZE) { |
| 676 | pr_err("%s needs to be < %d bytes\n", |
Bart Van Assche | 6eb7283 | 2016-10-18 13:10:24 -0700 | [diff] [blame] | 677 | opts->subsysnqn, NVMF_NQN_SIZE); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 678 | ret = -EINVAL; |
| 679 | goto out; |
| 680 | } |
| 681 | opts->discovery_nqn = |
| 682 | !(strcmp(opts->subsysnqn, |
| 683 | NVME_DISC_SUBSYS_NAME)); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 684 | break; |
| 685 | case NVMF_OPT_TRADDR: |
| 686 | p = match_strdup(args); |
| 687 | if (!p) { |
| 688 | ret = -ENOMEM; |
| 689 | goto out; |
| 690 | } |
Chengguang Xu | 59a2f3f | 2018-04-14 20:06:19 +0800 | [diff] [blame] | 691 | kfree(opts->traddr); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 692 | opts->traddr = p; |
| 693 | break; |
| 694 | case NVMF_OPT_TRSVCID: |
| 695 | p = match_strdup(args); |
| 696 | if (!p) { |
| 697 | ret = -ENOMEM; |
| 698 | goto out; |
| 699 | } |
Chengguang Xu | 59a2f3f | 2018-04-14 20:06:19 +0800 | [diff] [blame] | 700 | kfree(opts->trsvcid); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 701 | opts->trsvcid = p; |
| 702 | break; |
| 703 | case NVMF_OPT_QUEUE_SIZE: |
| 704 | if (match_int(args, &token)) { |
| 705 | ret = -EINVAL; |
| 706 | goto out; |
| 707 | } |
| 708 | if (token < NVMF_MIN_QUEUE_SIZE || |
| 709 | token > NVMF_MAX_QUEUE_SIZE) { |
| 710 | pr_err("Invalid queue_size %d\n", token); |
| 711 | ret = -EINVAL; |
| 712 | goto out; |
| 713 | } |
| 714 | opts->queue_size = token; |
| 715 | break; |
| 716 | case NVMF_OPT_NR_IO_QUEUES: |
| 717 | if (match_int(args, &token)) { |
| 718 | ret = -EINVAL; |
| 719 | goto out; |
| 720 | } |
| 721 | if (token <= 0) { |
| 722 | pr_err("Invalid number of IOQs %d\n", token); |
| 723 | ret = -EINVAL; |
| 724 | goto out; |
| 725 | } |
Roland Dreier | 0475821 | 2018-03-05 11:59:53 -0800 | [diff] [blame] | 726 | if (opts->discovery_nqn) { |
| 727 | pr_debug("Ignoring nr_io_queues value for discovery controller\n"); |
| 728 | break; |
| 729 | } |
| 730 | |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 731 | opts->nr_io_queues = min_t(unsigned int, |
| 732 | num_online_cpus(), token); |
| 733 | break; |
Sagi Grimberg | 038bd4c | 2016-06-13 16:45:28 +0200 | [diff] [blame] | 734 | case NVMF_OPT_KATO: |
| 735 | if (match_int(args, &token)) { |
| 736 | ret = -EINVAL; |
| 737 | goto out; |
| 738 | } |
| 739 | |
Sagi Grimberg | 038bd4c | 2016-06-13 16:45:28 +0200 | [diff] [blame] | 740 | if (token < 0) { |
| 741 | pr_err("Invalid keep_alive_tmo %d\n", token); |
| 742 | ret = -EINVAL; |
| 743 | goto out; |
Guilherme G. Piccoli | 8edd11c | 2017-09-14 13:59:28 -0300 | [diff] [blame] | 744 | } else if (token == 0 && !opts->discovery_nqn) { |
Sagi Grimberg | 038bd4c | 2016-06-13 16:45:28 +0200 | [diff] [blame] | 745 | /* Allowed for debug */ |
| 746 | pr_warn("keep_alive_tmo 0 won't execute keep alives!!!\n"); |
| 747 | } |
| 748 | opts->kato = token; |
| 749 | break; |
Sagi Grimberg | 42a4527 | 2017-03-18 20:52:36 +0200 | [diff] [blame] | 750 | case NVMF_OPT_CTRL_LOSS_TMO: |
| 751 | if (match_int(args, &token)) { |
| 752 | ret = -EINVAL; |
| 753 | goto out; |
| 754 | } |
| 755 | |
| 756 | if (token < 0) |
| 757 | pr_warn("ctrl_loss_tmo < 0 will reconnect forever\n"); |
| 758 | ctrl_loss_tmo = token; |
| 759 | break; |
Victor Gladkov | 8c4dfea | 2020-11-24 18:34:59 +0000 | [diff] [blame^] | 760 | case NVMF_OPT_FAIL_FAST_TMO: |
| 761 | if (match_int(args, &token)) { |
| 762 | ret = -EINVAL; |
| 763 | goto out; |
| 764 | } |
| 765 | |
| 766 | if (token >= 0) |
| 767 | pr_warn("I/O fail on reconnect controller after %d sec\n", |
| 768 | token); |
| 769 | opts->fast_io_fail_tmo = token; |
| 770 | break; |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 771 | case NVMF_OPT_HOSTNQN: |
| 772 | if (opts->host) { |
| 773 | pr_err("hostnqn already user-assigned: %s\n", |
| 774 | opts->host->nqn); |
| 775 | ret = -EADDRINUSE; |
| 776 | goto out; |
| 777 | } |
| 778 | p = match_strdup(args); |
| 779 | if (!p) { |
| 780 | ret = -ENOMEM; |
| 781 | goto out; |
| 782 | } |
| 783 | nqnlen = strlen(p); |
| 784 | if (nqnlen >= NVMF_NQN_SIZE) { |
| 785 | pr_err("%s needs to be < %d bytes\n", |
| 786 | p, NVMF_NQN_SIZE); |
Bart Van Assche | 8eadfcb | 2016-10-18 13:10:46 -0700 | [diff] [blame] | 787 | kfree(p); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 788 | ret = -EINVAL; |
| 789 | goto out; |
| 790 | } |
Chengguang Xu | 59a2f3f | 2018-04-14 20:06:19 +0800 | [diff] [blame] | 791 | nvmf_host_put(opts->host); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 792 | opts->host = nvmf_host_add(p); |
Bart Van Assche | 8eadfcb | 2016-10-18 13:10:46 -0700 | [diff] [blame] | 793 | kfree(p); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 794 | if (!opts->host) { |
| 795 | ret = -ENOMEM; |
| 796 | goto out; |
| 797 | } |
| 798 | break; |
| 799 | case NVMF_OPT_RECONNECT_DELAY: |
| 800 | if (match_int(args, &token)) { |
| 801 | ret = -EINVAL; |
| 802 | goto out; |
| 803 | } |
| 804 | if (token <= 0) { |
| 805 | pr_err("Invalid reconnect_delay %d\n", token); |
| 806 | ret = -EINVAL; |
| 807 | goto out; |
| 808 | } |
| 809 | opts->reconnect_delay = token; |
| 810 | break; |
James Smart | 478bcb9 | 2016-08-02 10:42:10 +0300 | [diff] [blame] | 811 | case NVMF_OPT_HOST_TRADDR: |
| 812 | p = match_strdup(args); |
| 813 | if (!p) { |
| 814 | ret = -ENOMEM; |
| 815 | goto out; |
| 816 | } |
Chengguang Xu | 59a2f3f | 2018-04-14 20:06:19 +0800 | [diff] [blame] | 817 | kfree(opts->host_traddr); |
James Smart | 478bcb9 | 2016-08-02 10:42:10 +0300 | [diff] [blame] | 818 | opts->host_traddr = p; |
| 819 | break; |
Johannes Thumshirn | 6bfe042 | 2017-06-20 14:23:01 +0200 | [diff] [blame] | 820 | case NVMF_OPT_HOST_ID: |
| 821 | p = match_strdup(args); |
| 822 | if (!p) { |
| 823 | ret = -ENOMEM; |
| 824 | goto out; |
| 825 | } |
Roland Dreier | df351ef | 2018-01-11 13:38:00 -0800 | [diff] [blame] | 826 | ret = uuid_parse(p, &hostid); |
Roland Dreier | df351ef | 2018-01-11 13:38:00 -0800 | [diff] [blame] | 827 | if (ret) { |
Guan Junxiong | 9b483da | 2017-08-03 21:40:26 +0800 | [diff] [blame] | 828 | pr_err("Invalid hostid %s\n", p); |
Johannes Thumshirn | 6bfe042 | 2017-06-20 14:23:01 +0200 | [diff] [blame] | 829 | ret = -EINVAL; |
Johannes Thumshirn | 6e49412 | 2018-01-25 09:09:25 +0100 | [diff] [blame] | 830 | kfree(p); |
Johannes Thumshirn | 6bfe042 | 2017-06-20 14:23:01 +0200 | [diff] [blame] | 831 | goto out; |
| 832 | } |
Johannes Thumshirn | 6e49412 | 2018-01-25 09:09:25 +0100 | [diff] [blame] | 833 | kfree(p); |
Johannes Thumshirn | 6bfe042 | 2017-06-20 14:23:01 +0200 | [diff] [blame] | 834 | break; |
James Smart | 3b33876 | 2017-10-20 16:17:06 -0700 | [diff] [blame] | 835 | case NVMF_OPT_DUP_CONNECT: |
| 836 | opts->duplicate_connect = true; |
| 837 | break; |
Sagi Grimberg | 8154ed7 | 2018-11-19 14:11:15 -0800 | [diff] [blame] | 838 | case NVMF_OPT_DISABLE_SQFLOW: |
| 839 | opts->disable_sqflow = true; |
| 840 | break; |
Sagi Grimberg | 3b49fa8 | 2018-12-03 17:52:12 -0800 | [diff] [blame] | 841 | case NVMF_OPT_HDR_DIGEST: |
| 842 | opts->hdr_digest = true; |
| 843 | break; |
Sagi Grimberg | 20d44e8 | 2018-12-03 17:52:13 -0800 | [diff] [blame] | 844 | case NVMF_OPT_DATA_DIGEST: |
| 845 | opts->data_digest = true; |
| 846 | break; |
Sagi Grimberg | 330f6b8 | 2018-12-11 23:38:56 -0800 | [diff] [blame] | 847 | case NVMF_OPT_NR_WRITE_QUEUES: |
| 848 | if (match_int(args, &token)) { |
| 849 | ret = -EINVAL; |
| 850 | goto out; |
| 851 | } |
| 852 | if (token <= 0) { |
| 853 | pr_err("Invalid nr_write_queues %d\n", token); |
| 854 | ret = -EINVAL; |
| 855 | goto out; |
| 856 | } |
| 857 | opts->nr_write_queues = token; |
| 858 | break; |
Sagi Grimberg | 89d4380 | 2018-12-14 11:06:09 -0800 | [diff] [blame] | 859 | case NVMF_OPT_NR_POLL_QUEUES: |
| 860 | if (match_int(args, &token)) { |
| 861 | ret = -EINVAL; |
| 862 | goto out; |
| 863 | } |
| 864 | if (token <= 0) { |
| 865 | pr_err("Invalid nr_poll_queues %d\n", token); |
| 866 | ret = -EINVAL; |
| 867 | goto out; |
| 868 | } |
| 869 | opts->nr_poll_queues = token; |
| 870 | break; |
Israel Rukshin | 52b4451 | 2019-08-18 12:08:51 +0300 | [diff] [blame] | 871 | case NVMF_OPT_TOS: |
| 872 | if (match_int(args, &token)) { |
| 873 | ret = -EINVAL; |
| 874 | goto out; |
| 875 | } |
| 876 | if (token < 0) { |
| 877 | pr_err("Invalid type of service %d\n", token); |
| 878 | ret = -EINVAL; |
| 879 | goto out; |
| 880 | } |
| 881 | if (token > 255) { |
| 882 | pr_warn("Clamping type of service to 255\n"); |
| 883 | token = 255; |
| 884 | } |
| 885 | opts->tos = token; |
| 886 | break; |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 887 | default: |
| 888 | pr_warn("unknown parameter or missing value '%s' in ctrl creation request\n", |
| 889 | p); |
| 890 | ret = -EINVAL; |
| 891 | goto out; |
| 892 | } |
| 893 | } |
| 894 | |
Hannes Reinecke | 461fbc8 | 2018-05-24 16:18:15 +0200 | [diff] [blame] | 895 | if (opts->discovery_nqn) { |
Hannes Reinecke | 461fbc8 | 2018-05-24 16:18:15 +0200 | [diff] [blame] | 896 | opts->nr_io_queues = 0; |
Sagi Grimberg | 9846ac0 | 2019-01-07 23:54:23 -0800 | [diff] [blame] | 897 | opts->nr_write_queues = 0; |
| 898 | opts->nr_poll_queues = 0; |
Hannes Reinecke | 181303d | 2018-05-24 16:18:17 +0200 | [diff] [blame] | 899 | opts->duplicate_connect = true; |
Hannes Reinecke | 461fbc8 | 2018-05-24 16:18:15 +0200 | [diff] [blame] | 900 | } |
Victor Gladkov | 8c4dfea | 2020-11-24 18:34:59 +0000 | [diff] [blame^] | 901 | if (ctrl_loss_tmo < 0) { |
Sagi Grimberg | 42a4527 | 2017-03-18 20:52:36 +0200 | [diff] [blame] | 902 | opts->max_reconnects = -1; |
Victor Gladkov | 8c4dfea | 2020-11-24 18:34:59 +0000 | [diff] [blame^] | 903 | } else { |
Sagi Grimberg | 42a4527 | 2017-03-18 20:52:36 +0200 | [diff] [blame] | 904 | opts->max_reconnects = DIV_ROUND_UP(ctrl_loss_tmo, |
| 905 | opts->reconnect_delay); |
Victor Gladkov | 8c4dfea | 2020-11-24 18:34:59 +0000 | [diff] [blame^] | 906 | if (ctrl_loss_tmo < opts->fast_io_fail_tmo) |
| 907 | pr_warn("failfast tmo (%d) larger than controller loss tmo (%d)\n", |
| 908 | opts->fast_io_fail_tmo, ctrl_loss_tmo); |
| 909 | } |
Sagi Grimberg | 42a4527 | 2017-03-18 20:52:36 +0200 | [diff] [blame] | 910 | |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 911 | if (!opts->host) { |
| 912 | kref_get(&nvmf_default_host->ref); |
| 913 | opts->host = nvmf_default_host; |
| 914 | } |
| 915 | |
Johannes Thumshirn | 6bfe042 | 2017-06-20 14:23:01 +0200 | [diff] [blame] | 916 | uuid_copy(&opts->host->id, &hostid); |
| 917 | |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 918 | out: |
| 919 | kfree(options); |
| 920 | return ret; |
| 921 | } |
| 922 | |
| 923 | static int nvmf_check_required_opts(struct nvmf_ctrl_options *opts, |
| 924 | unsigned int required_opts) |
| 925 | { |
| 926 | if ((opts->mask & required_opts) != required_opts) { |
| 927 | int i; |
| 928 | |
| 929 | for (i = 0; i < ARRAY_SIZE(opt_tokens); i++) { |
| 930 | if ((opt_tokens[i].token & required_opts) && |
| 931 | !(opt_tokens[i].token & opts->mask)) { |
| 932 | pr_warn("missing parameter '%s'\n", |
| 933 | opt_tokens[i].pattern); |
| 934 | } |
| 935 | } |
| 936 | |
| 937 | return -EINVAL; |
| 938 | } |
| 939 | |
| 940 | return 0; |
| 941 | } |
| 942 | |
Sagi Grimberg | b7c7be6f6 | 2018-10-18 17:40:40 -0700 | [diff] [blame] | 943 | bool nvmf_ip_options_match(struct nvme_ctrl *ctrl, |
| 944 | struct nvmf_ctrl_options *opts) |
| 945 | { |
| 946 | if (!nvmf_ctlr_matches_baseopts(ctrl, opts) || |
| 947 | strcmp(opts->traddr, ctrl->opts->traddr) || |
| 948 | strcmp(opts->trsvcid, ctrl->opts->trsvcid)) |
| 949 | return false; |
| 950 | |
| 951 | /* |
| 952 | * Checking the local address is rough. In most cases, none is specified |
| 953 | * and the host port is selected by the stack. |
| 954 | * |
| 955 | * Assume no match if: |
| 956 | * - local address is specified and address is not the same |
| 957 | * - local address is not specified but remote is, or vice versa |
| 958 | * (admin using specific host_traddr when it matters). |
| 959 | */ |
| 960 | if ((opts->mask & NVMF_OPT_HOST_TRADDR) && |
| 961 | (ctrl->opts->mask & NVMF_OPT_HOST_TRADDR)) { |
| 962 | if (strcmp(opts->host_traddr, ctrl->opts->host_traddr)) |
| 963 | return false; |
| 964 | } else if ((opts->mask & NVMF_OPT_HOST_TRADDR) || |
| 965 | (ctrl->opts->mask & NVMF_OPT_HOST_TRADDR)) { |
| 966 | return false; |
| 967 | } |
| 968 | |
| 969 | return true; |
| 970 | } |
| 971 | EXPORT_SYMBOL_GPL(nvmf_ip_options_match); |
| 972 | |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 973 | static int nvmf_check_allowed_opts(struct nvmf_ctrl_options *opts, |
| 974 | unsigned int allowed_opts) |
| 975 | { |
| 976 | if (opts->mask & ~allowed_opts) { |
| 977 | int i; |
| 978 | |
| 979 | for (i = 0; i < ARRAY_SIZE(opt_tokens); i++) { |
Christoph Hellwig | 81a0b8d | 2017-08-17 13:57:49 +0200 | [diff] [blame] | 980 | if ((opt_tokens[i].token & opts->mask) && |
| 981 | (opt_tokens[i].token & ~allowed_opts)) { |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 982 | pr_warn("invalid parameter '%s'\n", |
| 983 | opt_tokens[i].pattern); |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | return -EINVAL; |
| 988 | } |
| 989 | |
| 990 | return 0; |
| 991 | } |
| 992 | |
| 993 | void nvmf_free_options(struct nvmf_ctrl_options *opts) |
| 994 | { |
| 995 | nvmf_host_put(opts->host); |
| 996 | kfree(opts->transport); |
| 997 | kfree(opts->traddr); |
| 998 | kfree(opts->trsvcid); |
| 999 | kfree(opts->subsysnqn); |
James Smart | 478bcb9 | 2016-08-02 10:42:10 +0300 | [diff] [blame] | 1000 | kfree(opts->host_traddr); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 1001 | kfree(opts); |
| 1002 | } |
| 1003 | EXPORT_SYMBOL_GPL(nvmf_free_options); |
| 1004 | |
| 1005 | #define NVMF_REQUIRED_OPTS (NVMF_OPT_TRANSPORT | NVMF_OPT_NQN) |
| 1006 | #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] | 1007 | NVMF_OPT_KATO | NVMF_OPT_HOSTNQN | \ |
Sagi Grimberg | 8154ed7 | 2018-11-19 14:11:15 -0800 | [diff] [blame] | 1008 | NVMF_OPT_HOST_ID | NVMF_OPT_DUP_CONNECT |\ |
Victor Gladkov | 8c4dfea | 2020-11-24 18:34:59 +0000 | [diff] [blame^] | 1009 | NVMF_OPT_DISABLE_SQFLOW |\ |
| 1010 | NVMF_OPT_FAIL_FAST_TMO) |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 1011 | |
| 1012 | static struct nvme_ctrl * |
Minwoo Im | 94e970b | 2019-05-11 22:42:55 +0900 | [diff] [blame] | 1013 | nvmf_create_ctrl(struct device *dev, const char *buf) |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 1014 | { |
| 1015 | struct nvmf_ctrl_options *opts; |
| 1016 | struct nvmf_transport_ops *ops; |
| 1017 | struct nvme_ctrl *ctrl; |
| 1018 | int ret; |
| 1019 | |
| 1020 | opts = kzalloc(sizeof(*opts), GFP_KERNEL); |
| 1021 | if (!opts) |
| 1022 | return ERR_PTR(-ENOMEM); |
| 1023 | |
| 1024 | ret = nvmf_parse_options(opts, buf); |
| 1025 | if (ret) |
| 1026 | goto out_free_opts; |
| 1027 | |
Sagi Grimberg | d1f1071 | 2017-09-24 16:15:55 +0300 | [diff] [blame] | 1028 | |
| 1029 | request_module("nvme-%s", opts->transport); |
| 1030 | |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 1031 | /* |
| 1032 | * Check the generic options first as we need a valid transport for |
| 1033 | * the lookup below. Then clear the generic flags so that transport |
| 1034 | * drivers don't have to care about them. |
| 1035 | */ |
| 1036 | ret = nvmf_check_required_opts(opts, NVMF_REQUIRED_OPTS); |
| 1037 | if (ret) |
| 1038 | goto out_free_opts; |
| 1039 | opts->mask &= ~NVMF_REQUIRED_OPTS; |
| 1040 | |
Roland Dreier | 489beb9 | 2017-08-29 10:33:44 -0700 | [diff] [blame] | 1041 | down_read(&nvmf_transports_rwsem); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 1042 | ops = nvmf_lookup_transport(opts); |
| 1043 | if (!ops) { |
| 1044 | pr_info("no handler found for transport %s.\n", |
| 1045 | opts->transport); |
| 1046 | ret = -EINVAL; |
| 1047 | goto out_unlock; |
| 1048 | } |
| 1049 | |
Roy Shterman | 0de5cd3 | 2017-12-25 14:18:30 +0200 | [diff] [blame] | 1050 | if (!try_module_get(ops->module)) { |
| 1051 | ret = -EBUSY; |
| 1052 | goto out_unlock; |
| 1053 | } |
Johannes Thumshirn | 12a0b66 | 2018-06-01 09:11:20 +0200 | [diff] [blame] | 1054 | up_read(&nvmf_transports_rwsem); |
Roy Shterman | 0de5cd3 | 2017-12-25 14:18:30 +0200 | [diff] [blame] | 1055 | |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 1056 | ret = nvmf_check_required_opts(opts, ops->required_opts); |
| 1057 | if (ret) |
Roy Shterman | 0de5cd3 | 2017-12-25 14:18:30 +0200 | [diff] [blame] | 1058 | goto out_module_put; |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 1059 | ret = nvmf_check_allowed_opts(opts, NVMF_ALLOWED_OPTS | |
| 1060 | ops->allowed_opts | ops->required_opts); |
| 1061 | if (ret) |
Roy Shterman | 0de5cd3 | 2017-12-25 14:18:30 +0200 | [diff] [blame] | 1062 | goto out_module_put; |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 1063 | |
| 1064 | ctrl = ops->create_ctrl(dev, opts); |
| 1065 | if (IS_ERR(ctrl)) { |
| 1066 | ret = PTR_ERR(ctrl); |
Roy Shterman | 0de5cd3 | 2017-12-25 14:18:30 +0200 | [diff] [blame] | 1067 | goto out_module_put; |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 1068 | } |
| 1069 | |
Roy Shterman | 0de5cd3 | 2017-12-25 14:18:30 +0200 | [diff] [blame] | 1070 | module_put(ops->module); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 1071 | return ctrl; |
| 1072 | |
Roy Shterman | 0de5cd3 | 2017-12-25 14:18:30 +0200 | [diff] [blame] | 1073 | out_module_put: |
| 1074 | module_put(ops->module); |
Johannes Thumshirn | 12a0b66 | 2018-06-01 09:11:20 +0200 | [diff] [blame] | 1075 | goto out_free_opts; |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 1076 | out_unlock: |
Roland Dreier | 489beb9 | 2017-08-29 10:33:44 -0700 | [diff] [blame] | 1077 | up_read(&nvmf_transports_rwsem); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 1078 | out_free_opts: |
Bart Van Assche | f3116d8 | 2016-10-18 13:11:03 -0700 | [diff] [blame] | 1079 | nvmf_free_options(opts); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 1080 | return ERR_PTR(ret); |
| 1081 | } |
| 1082 | |
| 1083 | static struct class *nvmf_class; |
| 1084 | static struct device *nvmf_device; |
| 1085 | static DEFINE_MUTEX(nvmf_dev_mutex); |
| 1086 | |
| 1087 | static ssize_t nvmf_dev_write(struct file *file, const char __user *ubuf, |
| 1088 | size_t count, loff_t *pos) |
| 1089 | { |
| 1090 | struct seq_file *seq_file = file->private_data; |
| 1091 | struct nvme_ctrl *ctrl; |
| 1092 | const char *buf; |
| 1093 | int ret = 0; |
| 1094 | |
| 1095 | if (count > PAGE_SIZE) |
| 1096 | return -ENOMEM; |
| 1097 | |
| 1098 | buf = memdup_user_nul(ubuf, count); |
| 1099 | if (IS_ERR(buf)) |
| 1100 | return PTR_ERR(buf); |
| 1101 | |
| 1102 | mutex_lock(&nvmf_dev_mutex); |
| 1103 | if (seq_file->private) { |
| 1104 | ret = -EINVAL; |
| 1105 | goto out_unlock; |
| 1106 | } |
| 1107 | |
Minwoo Im | 94e970b | 2019-05-11 22:42:55 +0900 | [diff] [blame] | 1108 | ctrl = nvmf_create_ctrl(nvmf_device, buf); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 1109 | if (IS_ERR(ctrl)) { |
| 1110 | ret = PTR_ERR(ctrl); |
| 1111 | goto out_unlock; |
| 1112 | } |
| 1113 | |
| 1114 | seq_file->private = ctrl; |
| 1115 | |
| 1116 | out_unlock: |
| 1117 | mutex_unlock(&nvmf_dev_mutex); |
| 1118 | kfree(buf); |
| 1119 | return ret ? ret : count; |
| 1120 | } |
| 1121 | |
| 1122 | static int nvmf_dev_show(struct seq_file *seq_file, void *private) |
| 1123 | { |
| 1124 | struct nvme_ctrl *ctrl; |
| 1125 | int ret = 0; |
| 1126 | |
| 1127 | mutex_lock(&nvmf_dev_mutex); |
| 1128 | ctrl = seq_file->private; |
| 1129 | if (!ctrl) { |
| 1130 | ret = -EINVAL; |
| 1131 | goto out_unlock; |
| 1132 | } |
| 1133 | |
| 1134 | seq_printf(seq_file, "instance=%d,cntlid=%d\n", |
| 1135 | ctrl->instance, ctrl->cntlid); |
| 1136 | |
| 1137 | out_unlock: |
| 1138 | mutex_unlock(&nvmf_dev_mutex); |
| 1139 | return ret; |
| 1140 | } |
| 1141 | |
| 1142 | static int nvmf_dev_open(struct inode *inode, struct file *file) |
| 1143 | { |
| 1144 | /* |
| 1145 | * The miscdevice code initializes file->private_data, but doesn't |
| 1146 | * make use of it later. |
| 1147 | */ |
| 1148 | file->private_data = NULL; |
| 1149 | return single_open(file, nvmf_dev_show, NULL); |
| 1150 | } |
| 1151 | |
| 1152 | static int nvmf_dev_release(struct inode *inode, struct file *file) |
| 1153 | { |
| 1154 | struct seq_file *seq_file = file->private_data; |
| 1155 | struct nvme_ctrl *ctrl = seq_file->private; |
| 1156 | |
| 1157 | if (ctrl) |
| 1158 | nvme_put_ctrl(ctrl); |
| 1159 | return single_release(inode, file); |
| 1160 | } |
| 1161 | |
| 1162 | static const struct file_operations nvmf_dev_fops = { |
| 1163 | .owner = THIS_MODULE, |
| 1164 | .write = nvmf_dev_write, |
| 1165 | .read = seq_read, |
| 1166 | .open = nvmf_dev_open, |
| 1167 | .release = nvmf_dev_release, |
| 1168 | }; |
| 1169 | |
| 1170 | static struct miscdevice nvmf_misc = { |
| 1171 | .minor = MISC_DYNAMIC_MINOR, |
| 1172 | .name = "nvme-fabrics", |
| 1173 | .fops = &nvmf_dev_fops, |
| 1174 | }; |
| 1175 | |
| 1176 | static int __init nvmf_init(void) |
| 1177 | { |
| 1178 | int ret; |
| 1179 | |
| 1180 | nvmf_default_host = nvmf_host_default(); |
| 1181 | if (!nvmf_default_host) |
| 1182 | return -ENOMEM; |
| 1183 | |
| 1184 | nvmf_class = class_create(THIS_MODULE, "nvme-fabrics"); |
| 1185 | if (IS_ERR(nvmf_class)) { |
| 1186 | pr_err("couldn't register class nvme-fabrics\n"); |
| 1187 | ret = PTR_ERR(nvmf_class); |
| 1188 | goto out_free_host; |
| 1189 | } |
| 1190 | |
| 1191 | nvmf_device = |
| 1192 | device_create(nvmf_class, NULL, MKDEV(0, 0), NULL, "ctl"); |
| 1193 | if (IS_ERR(nvmf_device)) { |
| 1194 | pr_err("couldn't create nvme-fabris device!\n"); |
| 1195 | ret = PTR_ERR(nvmf_device); |
| 1196 | goto out_destroy_class; |
| 1197 | } |
| 1198 | |
| 1199 | ret = misc_register(&nvmf_misc); |
| 1200 | if (ret) { |
| 1201 | pr_err("couldn't register misc device: %d\n", ret); |
| 1202 | goto out_destroy_device; |
| 1203 | } |
| 1204 | |
| 1205 | return 0; |
| 1206 | |
| 1207 | out_destroy_device: |
| 1208 | device_destroy(nvmf_class, MKDEV(0, 0)); |
| 1209 | out_destroy_class: |
| 1210 | class_destroy(nvmf_class); |
| 1211 | out_free_host: |
| 1212 | nvmf_host_put(nvmf_default_host); |
| 1213 | return ret; |
| 1214 | } |
| 1215 | |
| 1216 | static void __exit nvmf_exit(void) |
| 1217 | { |
| 1218 | misc_deregister(&nvmf_misc); |
| 1219 | device_destroy(nvmf_class, MKDEV(0, 0)); |
| 1220 | class_destroy(nvmf_class); |
| 1221 | nvmf_host_put(nvmf_default_host); |
| 1222 | |
Minwoo Im | a2faf94 | 2019-04-12 00:18:31 +0900 | [diff] [blame] | 1223 | BUILD_BUG_ON(sizeof(struct nvmf_common_command) != 64); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 1224 | BUILD_BUG_ON(sizeof(struct nvmf_connect_command) != 64); |
| 1225 | BUILD_BUG_ON(sizeof(struct nvmf_property_get_command) != 64); |
| 1226 | BUILD_BUG_ON(sizeof(struct nvmf_property_set_command) != 64); |
| 1227 | BUILD_BUG_ON(sizeof(struct nvmf_connect_data) != 1024); |
| 1228 | } |
| 1229 | |
| 1230 | MODULE_LICENSE("GPL v2"); |
| 1231 | |
| 1232 | module_init(nvmf_init); |
| 1233 | module_exit(nvmf_exit); |