Christoph Hellwig | 77141dc | 2019-02-18 11:36:11 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Christoph Hellwig | a07b497 | 2016-06-21 18:04:20 +0200 | [diff] [blame] | 2 | /* |
| 3 | * NVMe Fabrics command implementation. |
| 4 | * Copyright (c) 2015-2016 HGST, a Western Digital Company. |
Christoph Hellwig | a07b497 | 2016-06-21 18:04:20 +0200 | [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 | { |
Chaitanya Kulkarni | 84faf42 | 2018-12-12 15:11:44 -0800 | [diff] [blame] | 12 | u64 val = le64_to_cpu(req->cmd->prop_set.value); |
Christoph Hellwig | a07b497 | 2016-06-21 18:04:20 +0200 | [diff] [blame] | 13 | u16 status = 0; |
| 14 | |
Israel Rukshin | 136cc1f | 2020-05-19 17:05:59 +0300 | [diff] [blame] | 15 | if (!nvmet_check_transfer_len(req, 0)) |
Christoph Hellwig | e9061c3 | 2019-10-23 10:35:44 -0600 | [diff] [blame] | 16 | return; |
| 17 | |
Chaitanya Kulkarni | 84faf42 | 2018-12-12 15:11:44 -0800 | [diff] [blame] | 18 | if (req->cmd->prop_set.attrib & 1) { |
| 19 | req->error_loc = |
| 20 | offsetof(struct nvmf_property_set_command, attrib); |
Christoph Hellwig | a07b497 | 2016-06-21 18:04:20 +0200 | [diff] [blame] | 21 | status = NVME_SC_INVALID_FIELD | NVME_SC_DNR; |
Chaitanya Kulkarni | 84faf42 | 2018-12-12 15:11:44 -0800 | [diff] [blame] | 22 | goto out; |
Christoph Hellwig | a07b497 | 2016-06-21 18:04:20 +0200 | [diff] [blame] | 23 | } |
| 24 | |
Chaitanya Kulkarni | 84faf42 | 2018-12-12 15:11:44 -0800 | [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: |
Christoph Hellwig | a07b497 | 2016-06-21 18:04:20 +0200 | [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 | |
Israel Rukshin | 136cc1f | 2020-05-19 17:05:59 +0300 | [diff] [blame] | 44 | if (!nvmet_check_transfer_len(req, 0)) |
Christoph Hellwig | e9061c3 | 2019-10-23 10:35:44 -0600 | [diff] [blame] | 45 | return; |
| 46 | |
Christoph Hellwig | a07b497 | 2016-06-21 18:04:20 +0200 | [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 | |
Chaitanya Kulkarni | 84faf42 | 2018-12-12 15:11:44 -0800 | [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 | |
Max Gurtovoy | fc6c973 | 2019-04-08 18:39:59 +0300 | [diff] [blame] | 81 | req->cqe->result.u64 = cpu_to_le64(val); |
Christoph Hellwig | a07b497 | 2016-06-21 18:04:20 +0200 | [diff] [blame] | 82 | nvmet_req_complete(req, status); |
| 83 | } |
| 84 | |
Parav Pandit | 64a0ca8 | 2017-02-27 23:21:33 -0600 | [diff] [blame] | 85 | u16 nvmet_parse_fabrics_cmd(struct nvmet_req *req) |
Christoph Hellwig | a07b497 | 2016-06-21 18:04:20 +0200 | [diff] [blame] | 86 | { |
| 87 | struct nvme_command *cmd = req->cmd; |
| 88 | |
Christoph Hellwig | a07b497 | 2016-06-21 18:04:20 +0200 | [diff] [blame] | 89 | switch (cmd->fabrics.fctype) { |
| 90 | case nvme_fabrics_type_property_set: |
Christoph Hellwig | a07b497 | 2016-06-21 18:04:20 +0200 | [diff] [blame] | 91 | req->execute = nvmet_execute_prop_set; |
| 92 | break; |
| 93 | case nvme_fabrics_type_property_get: |
Christoph Hellwig | a07b497 | 2016-06-21 18:04:20 +0200 | [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); |
Chaitanya Kulkarni | 84faf42 | 2018-12-12 15:11:44 -0800 | [diff] [blame] | 99 | req->error_loc = offsetof(struct nvmf_common_command, fctype); |
Christoph Hellwig | a07b497 | 2016-06-21 18:04:20 +0200 | [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; |
Israel Rukshin | 1a3f540 | 2020-02-04 14:38:10 +0200 | [diff] [blame] | 112 | u16 ret; |
Christoph Hellwig | a07b497 | 2016-06-21 18:04:20 +0200 | [diff] [blame] | 113 | |
| 114 | old = cmpxchg(&req->sq->ctrl, NULL, ctrl); |
| 115 | if (old) { |
| 116 | pr_warn("queue already connected!\n"); |
Chaitanya Kulkarni | 84faf42 | 2018-12-12 15:11:44 -0800 | [diff] [blame] | 117 | req->error_loc = offsetof(struct nvmf_connect_command, opcode); |
Christoph Hellwig | a07b497 | 2016-06-21 18:04:20 +0200 | [diff] [blame] | 118 | return NVME_SC_CONNECT_CTRL_BUSY | NVME_SC_DNR; |
| 119 | } |
James Smart | bb1cc74 | 2017-09-18 09:08:29 -0700 | [diff] [blame] | 120 | if (!sqsize) { |
| 121 | pr_warn("queue size zero!\n"); |
Chaitanya Kulkarni | 84faf42 | 2018-12-12 15:11:44 -0800 | [diff] [blame] | 122 | req->error_loc = offsetof(struct nvmf_connect_command, sqsize); |
Israel Rukshin | 1a3f540 | 2020-02-04 14:38:10 +0200 | [diff] [blame] | 123 | ret = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR; |
| 124 | goto err; |
James Smart | bb1cc74 | 2017-09-18 09:08:29 -0700 | [diff] [blame] | 125 | } |
Christoph Hellwig | a07b497 | 2016-06-21 18:04:20 +0200 | [diff] [blame] | 126 | |
James Smart | bb1cc74 | 2017-09-18 09:08:29 -0700 | [diff] [blame] | 127 | /* note: convert queue size from 0's-based value to 1's-based value */ |
| 128 | nvmet_cq_setup(ctrl, req->cq, qid, sqsize + 1); |
| 129 | nvmet_sq_setup(ctrl, req->sq, qid, sqsize + 1); |
Sagi Grimberg | e6a622f | 2018-11-19 14:11:12 -0800 | [diff] [blame] | 130 | |
| 131 | if (c->cattr & NVME_CONNECT_DISABLE_SQFLOW) { |
| 132 | req->sq->sqhd_disabled = true; |
Max Gurtovoy | fc6c973 | 2019-04-08 18:39:59 +0300 | [diff] [blame] | 133 | req->cqe->sq_head = cpu_to_le16(0xffff); |
Sagi Grimberg | e6a622f | 2018-11-19 14:11:12 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Sagi Grimberg | 1672ddb | 2018-12-03 17:52:11 -0800 | [diff] [blame] | 136 | if (ctrl->ops->install_queue) { |
Israel Rukshin | 1a3f540 | 2020-02-04 14:38:10 +0200 | [diff] [blame] | 137 | ret = ctrl->ops->install_queue(req->sq); |
Sagi Grimberg | 1672ddb | 2018-12-03 17:52:11 -0800 | [diff] [blame] | 138 | if (ret) { |
| 139 | pr_err("failed to install queue %d cntlid %d ret %x\n", |
Israel Rukshin | 0b87a2b | 2020-02-04 14:38:09 +0200 | [diff] [blame] | 140 | qid, ctrl->cntlid, ret); |
Israel Rukshin | 1a3f540 | 2020-02-04 14:38:10 +0200 | [diff] [blame] | 141 | goto err; |
Sagi Grimberg | 1672ddb | 2018-12-03 17:52:11 -0800 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
Christoph Hellwig | a07b497 | 2016-06-21 18:04:20 +0200 | [diff] [blame] | 145 | return 0; |
Israel Rukshin | 1a3f540 | 2020-02-04 14:38:10 +0200 | [diff] [blame] | 146 | |
| 147 | err: |
| 148 | req->sq->ctrl = NULL; |
| 149 | return ret; |
Christoph Hellwig | a07b497 | 2016-06-21 18:04:20 +0200 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | static void nvmet_execute_admin_connect(struct nvmet_req *req) |
| 153 | { |
| 154 | struct nvmf_connect_command *c = &req->cmd->connect; |
| 155 | struct nvmf_connect_data *d; |
| 156 | struct nvmet_ctrl *ctrl = NULL; |
| 157 | u16 status = 0; |
| 158 | |
Israel Rukshin | 136cc1f | 2020-05-19 17:05:59 +0300 | [diff] [blame] | 159 | if (!nvmet_check_transfer_len(req, sizeof(struct nvmf_connect_data))) |
Christoph Hellwig | e9061c3 | 2019-10-23 10:35:44 -0600 | [diff] [blame] | 160 | return; |
| 161 | |
Logan Gunthorpe | 1c05cf9 | 2017-04-18 17:32:15 -0600 | [diff] [blame] | 162 | d = kmalloc(sizeof(*d), GFP_KERNEL); |
| 163 | if (!d) { |
| 164 | status = NVME_SC_INTERNAL; |
| 165 | goto complete; |
| 166 | } |
| 167 | |
| 168 | status = nvmet_copy_from_sgl(req, 0, d, sizeof(*d)); |
| 169 | if (status) |
| 170 | goto out; |
Christoph Hellwig | a07b497 | 2016-06-21 18:04:20 +0200 | [diff] [blame] | 171 | |
| 172 | /* zero out initial completion result, assign values as needed */ |
Max Gurtovoy | fc6c973 | 2019-04-08 18:39:59 +0300 | [diff] [blame] | 173 | req->cqe->result.u32 = 0; |
Christoph Hellwig | a07b497 | 2016-06-21 18:04:20 +0200 | [diff] [blame] | 174 | |
| 175 | if (c->recfmt != 0) { |
| 176 | pr_warn("invalid connect version (%d).\n", |
| 177 | le16_to_cpu(c->recfmt)); |
Chaitanya Kulkarni | 84faf42 | 2018-12-12 15:11:44 -0800 | [diff] [blame] | 178 | req->error_loc = offsetof(struct nvmf_connect_command, recfmt); |
Christoph Hellwig | a07b497 | 2016-06-21 18:04:20 +0200 | [diff] [blame] | 179 | status = NVME_SC_CONNECT_FORMAT | NVME_SC_DNR; |
| 180 | goto out; |
| 181 | } |
| 182 | |
| 183 | if (unlikely(d->cntlid != cpu_to_le16(0xffff))) { |
| 184 | pr_warn("connect attempt for invalid controller ID %#x\n", |
| 185 | d->cntlid); |
| 186 | status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR; |
Max Gurtovoy | fc6c973 | 2019-04-08 18:39:59 +0300 | [diff] [blame] | 187 | req->cqe->result.u32 = IPO_IATTR_CONNECT_DATA(cntlid); |
Christoph Hellwig | a07b497 | 2016-06-21 18:04:20 +0200 | [diff] [blame] | 188 | goto out; |
| 189 | } |
| 190 | |
| 191 | status = nvmet_alloc_ctrl(d->subsysnqn, d->hostnqn, req, |
Logan Gunthorpe | 1c05cf9 | 2017-04-18 17:32:15 -0600 | [diff] [blame] | 192 | le32_to_cpu(c->kato), &ctrl); |
Chaitanya Kulkarni | 84faf42 | 2018-12-12 15:11:44 -0800 | [diff] [blame] | 193 | if (status) { |
| 194 | if (status == (NVME_SC_INVALID_FIELD | NVME_SC_DNR)) |
| 195 | req->error_loc = |
| 196 | offsetof(struct nvme_common_command, opcode); |
Christoph Hellwig | a07b497 | 2016-06-21 18:04:20 +0200 | [diff] [blame] | 197 | goto out; |
Chaitanya Kulkarni | 84faf42 | 2018-12-12 15:11:44 -0800 | [diff] [blame] | 198 | } |
| 199 | |
Israel Rukshin | ea52ac1 | 2020-05-19 17:06:01 +0300 | [diff] [blame] | 200 | ctrl->pi_support = ctrl->port->pi_enable && ctrl->subsys->pi_support; |
| 201 | |
Omri Mann | 28dd5cf | 2017-08-30 15:22:59 +0300 | [diff] [blame] | 202 | uuid_copy(&ctrl->hostid, &d->hostid); |
Christoph Hellwig | a07b497 | 2016-06-21 18:04:20 +0200 | [diff] [blame] | 203 | |
| 204 | status = nvmet_install_queue(ctrl, req); |
| 205 | if (status) { |
| 206 | nvmet_ctrl_put(ctrl); |
| 207 | goto out; |
| 208 | } |
| 209 | |
Israel Rukshin | ea52ac1 | 2020-05-19 17:06:01 +0300 | [diff] [blame] | 210 | pr_info("creating controller %d for subsystem %s for NQN %s%s.\n", |
| 211 | ctrl->cntlid, ctrl->subsys->subsysnqn, ctrl->hostnqn, |
| 212 | ctrl->pi_support ? " T10-PI is enabled" : ""); |
Max Gurtovoy | fc6c973 | 2019-04-08 18:39:59 +0300 | [diff] [blame] | 213 | req->cqe->result.u16 = cpu_to_le16(ctrl->cntlid); |
Christoph Hellwig | a07b497 | 2016-06-21 18:04:20 +0200 | [diff] [blame] | 214 | |
| 215 | out: |
Logan Gunthorpe | 1c05cf9 | 2017-04-18 17:32:15 -0600 | [diff] [blame] | 216 | kfree(d); |
| 217 | complete: |
Christoph Hellwig | a07b497 | 2016-06-21 18:04:20 +0200 | [diff] [blame] | 218 | nvmet_req_complete(req, status); |
| 219 | } |
| 220 | |
| 221 | static void nvmet_execute_io_connect(struct nvmet_req *req) |
| 222 | { |
| 223 | struct nvmf_connect_command *c = &req->cmd->connect; |
| 224 | struct nvmf_connect_data *d; |
| 225 | struct nvmet_ctrl *ctrl = NULL; |
| 226 | u16 qid = le16_to_cpu(c->qid); |
| 227 | u16 status = 0; |
| 228 | |
Israel Rukshin | 136cc1f | 2020-05-19 17:05:59 +0300 | [diff] [blame] | 229 | if (!nvmet_check_transfer_len(req, sizeof(struct nvmf_connect_data))) |
Christoph Hellwig | e9061c3 | 2019-10-23 10:35:44 -0600 | [diff] [blame] | 230 | return; |
| 231 | |
Logan Gunthorpe | 1c05cf9 | 2017-04-18 17:32:15 -0600 | [diff] [blame] | 232 | d = kmalloc(sizeof(*d), GFP_KERNEL); |
| 233 | if (!d) { |
| 234 | status = NVME_SC_INTERNAL; |
| 235 | goto complete; |
| 236 | } |
| 237 | |
| 238 | status = nvmet_copy_from_sgl(req, 0, d, sizeof(*d)); |
| 239 | if (status) |
| 240 | goto out; |
Christoph Hellwig | a07b497 | 2016-06-21 18:04:20 +0200 | [diff] [blame] | 241 | |
| 242 | /* zero out initial completion result, assign values as needed */ |
Max Gurtovoy | fc6c973 | 2019-04-08 18:39:59 +0300 | [diff] [blame] | 243 | req->cqe->result.u32 = 0; |
Christoph Hellwig | a07b497 | 2016-06-21 18:04:20 +0200 | [diff] [blame] | 244 | |
| 245 | if (c->recfmt != 0) { |
| 246 | pr_warn("invalid connect version (%d).\n", |
| 247 | le16_to_cpu(c->recfmt)); |
| 248 | status = NVME_SC_CONNECT_FORMAT | NVME_SC_DNR; |
| 249 | goto out; |
| 250 | } |
| 251 | |
| 252 | status = nvmet_ctrl_find_get(d->subsysnqn, d->hostnqn, |
Logan Gunthorpe | 1c05cf9 | 2017-04-18 17:32:15 -0600 | [diff] [blame] | 253 | le16_to_cpu(d->cntlid), |
| 254 | req, &ctrl); |
Christoph Hellwig | a07b497 | 2016-06-21 18:04:20 +0200 | [diff] [blame] | 255 | if (status) |
| 256 | goto out; |
| 257 | |
| 258 | if (unlikely(qid > ctrl->subsys->max_qid)) { |
| 259 | pr_warn("invalid queue id (%d)\n", qid); |
| 260 | status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR; |
Max Gurtovoy | fc6c973 | 2019-04-08 18:39:59 +0300 | [diff] [blame] | 261 | req->cqe->result.u32 = IPO_IATTR_CONNECT_SQE(qid); |
Christoph Hellwig | a07b497 | 2016-06-21 18:04:20 +0200 | [diff] [blame] | 262 | goto out_ctrl_put; |
| 263 | } |
| 264 | |
| 265 | status = nvmet_install_queue(ctrl, req); |
| 266 | if (status) { |
| 267 | /* pass back cntlid that had the issue of installing queue */ |
Max Gurtovoy | fc6c973 | 2019-04-08 18:39:59 +0300 | [diff] [blame] | 268 | req->cqe->result.u16 = cpu_to_le16(ctrl->cntlid); |
Christoph Hellwig | a07b497 | 2016-06-21 18:04:20 +0200 | [diff] [blame] | 269 | goto out_ctrl_put; |
| 270 | } |
| 271 | |
Sagi Grimberg | 6a1c57a | 2017-12-04 10:47:09 +0200 | [diff] [blame] | 272 | pr_debug("adding queue %d to ctrl %d.\n", qid, ctrl->cntlid); |
Christoph Hellwig | a07b497 | 2016-06-21 18:04:20 +0200 | [diff] [blame] | 273 | |
| 274 | out: |
Logan Gunthorpe | 1c05cf9 | 2017-04-18 17:32:15 -0600 | [diff] [blame] | 275 | kfree(d); |
| 276 | complete: |
Christoph Hellwig | a07b497 | 2016-06-21 18:04:20 +0200 | [diff] [blame] | 277 | nvmet_req_complete(req, status); |
| 278 | return; |
| 279 | |
| 280 | out_ctrl_put: |
| 281 | nvmet_ctrl_put(ctrl); |
| 282 | goto out; |
| 283 | } |
| 284 | |
Parav Pandit | 64a0ca8 | 2017-02-27 23:21:33 -0600 | [diff] [blame] | 285 | u16 nvmet_parse_connect_cmd(struct nvmet_req *req) |
Christoph Hellwig | a07b497 | 2016-06-21 18:04:20 +0200 | [diff] [blame] | 286 | { |
| 287 | struct nvme_command *cmd = req->cmd; |
| 288 | |
Minwoo Im | 7a1f46e | 2019-06-06 14:30:14 +0900 | [diff] [blame] | 289 | if (!nvme_is_fabrics(cmd)) { |
Christoph Hellwig | a07b497 | 2016-06-21 18:04:20 +0200 | [diff] [blame] | 290 | pr_err("invalid command 0x%x on unconnected queue.\n", |
| 291 | cmd->fabrics.opcode); |
Chaitanya Kulkarni | 84faf42 | 2018-12-12 15:11:44 -0800 | [diff] [blame] | 292 | req->error_loc = offsetof(struct nvme_common_command, opcode); |
Christoph Hellwig | a07b497 | 2016-06-21 18:04:20 +0200 | [diff] [blame] | 293 | return NVME_SC_INVALID_OPCODE | NVME_SC_DNR; |
| 294 | } |
| 295 | if (cmd->fabrics.fctype != nvme_fabrics_type_connect) { |
| 296 | pr_err("invalid capsule type 0x%x on unconnected queue.\n", |
| 297 | cmd->fabrics.fctype); |
Chaitanya Kulkarni | 84faf42 | 2018-12-12 15:11:44 -0800 | [diff] [blame] | 298 | req->error_loc = offsetof(struct nvmf_common_command, fctype); |
Christoph Hellwig | a07b497 | 2016-06-21 18:04:20 +0200 | [diff] [blame] | 299 | return NVME_SC_INVALID_OPCODE | NVME_SC_DNR; |
| 300 | } |
| 301 | |
Christoph Hellwig | a07b497 | 2016-06-21 18:04:20 +0200 | [diff] [blame] | 302 | if (cmd->connect.qid == 0) |
| 303 | req->execute = nvmet_execute_admin_connect; |
| 304 | else |
| 305 | req->execute = nvmet_execute_io_connect; |
| 306 | return 0; |
| 307 | } |