David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2 | /* |
| 3 | * NVMe Fabrics command implementation. |
| 4 | * Copyright (c) 2015-2016 HGST, a Western Digital Company. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5 | */ |
| 6 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 7 | #include <linux/blkdev.h> |
| 8 | #include "nvmet.h" |
| 9 | |
| 10 | static void nvmet_execute_prop_set(struct nvmet_req *req) |
| 11 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 12 | u64 val = le64_to_cpu(req->cmd->prop_set.value); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 13 | u16 status = 0; |
| 14 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 15 | if (!nvmet_check_transfer_len(req, 0)) |
| 16 | return; |
| 17 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 18 | if (req->cmd->prop_set.attrib & 1) { |
| 19 | req->error_loc = |
| 20 | offsetof(struct nvmf_property_set_command, attrib); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 21 | status = NVME_SC_INVALID_FIELD | NVME_SC_DNR; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 22 | goto out; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 23 | } |
| 24 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 25 | switch (le32_to_cpu(req->cmd->prop_set.offset)) { |
| 26 | case NVME_REG_CC: |
| 27 | nvmet_update_cc(req->sq->ctrl, val); |
| 28 | break; |
| 29 | default: |
| 30 | req->error_loc = |
| 31 | offsetof(struct nvmf_property_set_command, offset); |
| 32 | status = NVME_SC_INVALID_FIELD | NVME_SC_DNR; |
| 33 | } |
| 34 | out: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 35 | nvmet_req_complete(req, status); |
| 36 | } |
| 37 | |
| 38 | static void nvmet_execute_prop_get(struct nvmet_req *req) |
| 39 | { |
| 40 | struct nvmet_ctrl *ctrl = req->sq->ctrl; |
| 41 | u16 status = 0; |
| 42 | u64 val = 0; |
| 43 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 44 | if (!nvmet_check_transfer_len(req, 0)) |
| 45 | return; |
| 46 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 47 | if (req->cmd->prop_get.attrib & 1) { |
| 48 | switch (le32_to_cpu(req->cmd->prop_get.offset)) { |
| 49 | case NVME_REG_CAP: |
| 50 | val = ctrl->cap; |
| 51 | break; |
| 52 | default: |
| 53 | status = NVME_SC_INVALID_FIELD | NVME_SC_DNR; |
| 54 | break; |
| 55 | } |
| 56 | } else { |
| 57 | switch (le32_to_cpu(req->cmd->prop_get.offset)) { |
| 58 | case NVME_REG_VS: |
| 59 | val = ctrl->subsys->ver; |
| 60 | break; |
| 61 | case NVME_REG_CC: |
| 62 | val = ctrl->cc; |
| 63 | break; |
| 64 | case NVME_REG_CSTS: |
| 65 | val = ctrl->csts; |
| 66 | break; |
| 67 | default: |
| 68 | status = NVME_SC_INVALID_FIELD | NVME_SC_DNR; |
| 69 | break; |
| 70 | } |
| 71 | } |
| 72 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 73 | if (status && req->cmd->prop_get.attrib & 1) { |
| 74 | req->error_loc = |
| 75 | offsetof(struct nvmf_property_get_command, offset); |
| 76 | } else { |
| 77 | req->error_loc = |
| 78 | offsetof(struct nvmf_property_get_command, attrib); |
| 79 | } |
| 80 | |
| 81 | req->cqe->result.u64 = cpu_to_le64(val); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 82 | nvmet_req_complete(req, status); |
| 83 | } |
| 84 | |
| 85 | u16 nvmet_parse_fabrics_cmd(struct nvmet_req *req) |
| 86 | { |
| 87 | struct nvme_command *cmd = req->cmd; |
| 88 | |
| 89 | switch (cmd->fabrics.fctype) { |
| 90 | case nvme_fabrics_type_property_set: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 91 | req->execute = nvmet_execute_prop_set; |
| 92 | break; |
| 93 | case nvme_fabrics_type_property_get: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 94 | req->execute = nvmet_execute_prop_get; |
| 95 | break; |
| 96 | default: |
| 97 | pr_err("received unknown capsule type 0x%x\n", |
| 98 | cmd->fabrics.fctype); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 99 | req->error_loc = offsetof(struct nvmf_common_command, fctype); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 100 | return NVME_SC_INVALID_OPCODE | NVME_SC_DNR; |
| 101 | } |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | static u16 nvmet_install_queue(struct nvmet_ctrl *ctrl, struct nvmet_req *req) |
| 107 | { |
| 108 | struct nvmf_connect_command *c = &req->cmd->connect; |
| 109 | u16 qid = le16_to_cpu(c->qid); |
| 110 | u16 sqsize = le16_to_cpu(c->sqsize); |
| 111 | struct nvmet_ctrl *old; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 112 | u16 ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 113 | |
| 114 | old = cmpxchg(&req->sq->ctrl, NULL, ctrl); |
| 115 | if (old) { |
| 116 | pr_warn("queue already connected!\n"); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 117 | req->error_loc = offsetof(struct nvmf_connect_command, opcode); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 118 | return NVME_SC_CONNECT_CTRL_BUSY | NVME_SC_DNR; |
| 119 | } |
| 120 | if (!sqsize) { |
| 121 | pr_warn("queue size zero!\n"); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 122 | req->error_loc = offsetof(struct nvmf_connect_command, sqsize); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 123 | req->cqe->result.u32 = IPO_IATTR_CONNECT_SQE(sqsize); |
| 124 | ret = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR; |
| 125 | goto err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /* note: convert queue size from 0's-based value to 1's-based value */ |
| 129 | nvmet_cq_setup(ctrl, req->cq, qid, sqsize + 1); |
| 130 | nvmet_sq_setup(ctrl, req->sq, qid, sqsize + 1); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 131 | |
| 132 | if (c->cattr & NVME_CONNECT_DISABLE_SQFLOW) { |
| 133 | req->sq->sqhd_disabled = true; |
| 134 | req->cqe->sq_head = cpu_to_le16(0xffff); |
| 135 | } |
| 136 | |
| 137 | if (ctrl->ops->install_queue) { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 138 | ret = ctrl->ops->install_queue(req->sq); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 139 | if (ret) { |
| 140 | pr_err("failed to install queue %d cntlid %d ret %x\n", |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 141 | qid, ctrl->cntlid, ret); |
| 142 | goto err; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 146 | return 0; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 147 | |
| 148 | err: |
| 149 | req->sq->ctrl = NULL; |
| 150 | return ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | static void nvmet_execute_admin_connect(struct nvmet_req *req) |
| 154 | { |
| 155 | struct nvmf_connect_command *c = &req->cmd->connect; |
| 156 | struct nvmf_connect_data *d; |
| 157 | struct nvmet_ctrl *ctrl = NULL; |
| 158 | u16 status = 0; |
| 159 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 160 | if (!nvmet_check_transfer_len(req, sizeof(struct nvmf_connect_data))) |
| 161 | return; |
| 162 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 163 | d = kmalloc(sizeof(*d), GFP_KERNEL); |
| 164 | if (!d) { |
| 165 | status = NVME_SC_INTERNAL; |
| 166 | goto complete; |
| 167 | } |
| 168 | |
| 169 | status = nvmet_copy_from_sgl(req, 0, d, sizeof(*d)); |
| 170 | if (status) |
| 171 | goto out; |
| 172 | |
| 173 | /* zero out initial completion result, assign values as needed */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 174 | req->cqe->result.u32 = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 175 | |
| 176 | if (c->recfmt != 0) { |
| 177 | pr_warn("invalid connect version (%d).\n", |
| 178 | le16_to_cpu(c->recfmt)); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 179 | req->error_loc = offsetof(struct nvmf_connect_command, recfmt); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 180 | status = NVME_SC_CONNECT_FORMAT | NVME_SC_DNR; |
| 181 | goto out; |
| 182 | } |
| 183 | |
| 184 | if (unlikely(d->cntlid != cpu_to_le16(0xffff))) { |
| 185 | pr_warn("connect attempt for invalid controller ID %#x\n", |
| 186 | d->cntlid); |
| 187 | status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 188 | req->cqe->result.u32 = IPO_IATTR_CONNECT_DATA(cntlid); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 189 | goto out; |
| 190 | } |
| 191 | |
| 192 | status = nvmet_alloc_ctrl(d->subsysnqn, d->hostnqn, req, |
| 193 | le32_to_cpu(c->kato), &ctrl); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 194 | if (status) { |
| 195 | if (status == (NVME_SC_INVALID_FIELD | NVME_SC_DNR)) |
| 196 | req->error_loc = |
| 197 | offsetof(struct nvme_common_command, opcode); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 198 | goto out; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 201 | ctrl->pi_support = ctrl->port->pi_enable && ctrl->subsys->pi_support; |
| 202 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 203 | uuid_copy(&ctrl->hostid, &d->hostid); |
| 204 | |
| 205 | status = nvmet_install_queue(ctrl, req); |
| 206 | if (status) { |
| 207 | nvmet_ctrl_put(ctrl); |
| 208 | goto out; |
| 209 | } |
| 210 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 211 | pr_info("creating controller %d for subsystem %s for NQN %s%s.\n", |
| 212 | ctrl->cntlid, ctrl->subsys->subsysnqn, ctrl->hostnqn, |
| 213 | ctrl->pi_support ? " T10-PI is enabled" : ""); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 214 | req->cqe->result.u16 = cpu_to_le16(ctrl->cntlid); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 215 | |
| 216 | out: |
| 217 | kfree(d); |
| 218 | complete: |
| 219 | nvmet_req_complete(req, status); |
| 220 | } |
| 221 | |
| 222 | static void nvmet_execute_io_connect(struct nvmet_req *req) |
| 223 | { |
| 224 | struct nvmf_connect_command *c = &req->cmd->connect; |
| 225 | struct nvmf_connect_data *d; |
| 226 | struct nvmet_ctrl *ctrl = NULL; |
| 227 | u16 qid = le16_to_cpu(c->qid); |
| 228 | u16 status = 0; |
| 229 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 230 | if (!nvmet_check_transfer_len(req, sizeof(struct nvmf_connect_data))) |
| 231 | return; |
| 232 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 233 | d = kmalloc(sizeof(*d), GFP_KERNEL); |
| 234 | if (!d) { |
| 235 | status = NVME_SC_INTERNAL; |
| 236 | goto complete; |
| 237 | } |
| 238 | |
| 239 | status = nvmet_copy_from_sgl(req, 0, d, sizeof(*d)); |
| 240 | if (status) |
| 241 | goto out; |
| 242 | |
| 243 | /* zero out initial completion result, assign values as needed */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 244 | req->cqe->result.u32 = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 245 | |
| 246 | if (c->recfmt != 0) { |
| 247 | pr_warn("invalid connect version (%d).\n", |
| 248 | le16_to_cpu(c->recfmt)); |
| 249 | status = NVME_SC_CONNECT_FORMAT | NVME_SC_DNR; |
| 250 | goto out; |
| 251 | } |
| 252 | |
| 253 | status = nvmet_ctrl_find_get(d->subsysnqn, d->hostnqn, |
| 254 | le16_to_cpu(d->cntlid), |
| 255 | req, &ctrl); |
| 256 | if (status) |
| 257 | goto out; |
| 258 | |
| 259 | if (unlikely(qid > ctrl->subsys->max_qid)) { |
| 260 | pr_warn("invalid queue id (%d)\n", qid); |
| 261 | status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 262 | req->cqe->result.u32 = IPO_IATTR_CONNECT_SQE(qid); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 263 | goto out_ctrl_put; |
| 264 | } |
| 265 | |
| 266 | status = nvmet_install_queue(ctrl, req); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 267 | if (status) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 268 | goto out_ctrl_put; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 269 | |
| 270 | /* pass back cntlid for successful completion */ |
| 271 | req->cqe->result.u16 = cpu_to_le16(ctrl->cntlid); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 272 | |
| 273 | pr_debug("adding queue %d to ctrl %d.\n", qid, ctrl->cntlid); |
| 274 | |
| 275 | out: |
| 276 | kfree(d); |
| 277 | complete: |
| 278 | nvmet_req_complete(req, status); |
| 279 | return; |
| 280 | |
| 281 | out_ctrl_put: |
| 282 | nvmet_ctrl_put(ctrl); |
| 283 | goto out; |
| 284 | } |
| 285 | |
| 286 | u16 nvmet_parse_connect_cmd(struct nvmet_req *req) |
| 287 | { |
| 288 | struct nvme_command *cmd = req->cmd; |
| 289 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 290 | if (!nvme_is_fabrics(cmd)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 291 | pr_err("invalid command 0x%x on unconnected queue.\n", |
| 292 | cmd->fabrics.opcode); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 293 | req->error_loc = offsetof(struct nvme_common_command, opcode); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 294 | return NVME_SC_INVALID_OPCODE | NVME_SC_DNR; |
| 295 | } |
| 296 | if (cmd->fabrics.fctype != nvme_fabrics_type_connect) { |
| 297 | pr_err("invalid capsule type 0x%x on unconnected queue.\n", |
| 298 | cmd->fabrics.fctype); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 299 | req->error_loc = offsetof(struct nvmf_common_command, fctype); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 300 | return NVME_SC_INVALID_OPCODE | NVME_SC_DNR; |
| 301 | } |
| 302 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 303 | if (cmd->connect.qid == 0) |
| 304 | req->execute = nvmet_execute_admin_connect; |
| 305 | else |
| 306 | req->execute = nvmet_execute_io_connect; |
| 307 | return 0; |
| 308 | } |