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