1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * NVMe Fabrics command implementation. 4 * Copyright (c) 2015-2016 HGST, a Western Digital Company. 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 { 12 u64 val = le64_to_cpu(req->cmd->prop_set.value); 13 u16 status = 0; 14 15 if (!nvmet_check_transfer_len(req, 0)) 16 return; 17 18 if (req->cmd->prop_set.attrib & 1) { 19 req->error_loc = 20 offsetof(struct nvmf_property_set_command, attrib); 21 status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR; 22 goto out; 23 } 24 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_STATUS_DNR; 33 } 34 out: 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 44 if (!nvmet_check_transfer_len(req, 0)) 45 return; 46 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_STATUS_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 case NVME_REG_CRTO: 68 val = NVME_CAP_TIMEOUT(ctrl->csts); 69 break; 70 default: 71 status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR; 72 break; 73 } 74 } 75 76 if (status && req->cmd->prop_get.attrib & 1) { 77 req->error_loc = 78 offsetof(struct nvmf_property_get_command, offset); 79 } else { 80 req->error_loc = 81 offsetof(struct nvmf_property_get_command, attrib); 82 } 83 84 req->cqe->result.u64 = cpu_to_le64(val); 85 nvmet_req_complete(req, status); 86 } 87 88 u32 nvmet_fabrics_admin_cmd_data_len(struct nvmet_req *req) 89 { 90 struct nvme_command *cmd = req->cmd; 91 92 switch (cmd->fabrics.fctype) { 93 #ifdef CONFIG_NVME_TARGET_AUTH 94 case nvme_fabrics_type_auth_send: 95 return nvmet_auth_send_data_len(req); 96 case nvme_fabrics_type_auth_receive: 97 return nvmet_auth_receive_data_len(req); 98 #endif 99 default: 100 return 0; 101 } 102 } 103 104 u16 nvmet_parse_fabrics_admin_cmd(struct nvmet_req *req) 105 { 106 struct nvme_command *cmd = req->cmd; 107 108 switch (cmd->fabrics.fctype) { 109 case nvme_fabrics_type_property_set: 110 req->execute = nvmet_execute_prop_set; 111 break; 112 case nvme_fabrics_type_property_get: 113 req->execute = nvmet_execute_prop_get; 114 break; 115 #ifdef CONFIG_NVME_TARGET_AUTH 116 case nvme_fabrics_type_auth_send: 117 req->execute = nvmet_execute_auth_send; 118 break; 119 case nvme_fabrics_type_auth_receive: 120 req->execute = nvmet_execute_auth_receive; 121 break; 122 #endif 123 default: 124 pr_debug("received unknown capsule type 0x%x\n", 125 cmd->fabrics.fctype); 126 req->error_loc = offsetof(struct nvmf_common_command, fctype); 127 return NVME_SC_INVALID_OPCODE | NVME_STATUS_DNR; 128 } 129 130 return 0; 131 } 132 133 u32 nvmet_fabrics_io_cmd_data_len(struct nvmet_req *req) 134 { 135 struct nvme_command *cmd = req->cmd; 136 137 switch (cmd->fabrics.fctype) { 138 #ifdef CONFIG_NVME_TARGET_AUTH 139 case nvme_fabrics_type_auth_send: 140 return nvmet_auth_send_data_len(req); 141 case nvme_fabrics_type_auth_receive: 142 return nvmet_auth_receive_data_len(req); 143 #endif 144 default: 145 return 0; 146 } 147 } 148 149 u16 nvmet_parse_fabrics_io_cmd(struct nvmet_req *req) 150 { 151 struct nvme_command *cmd = req->cmd; 152 153 switch (cmd->fabrics.fctype) { 154 #ifdef CONFIG_NVME_TARGET_AUTH 155 case nvme_fabrics_type_auth_send: 156 req->execute = nvmet_execute_auth_send; 157 break; 158 case nvme_fabrics_type_auth_receive: 159 req->execute = nvmet_execute_auth_receive; 160 break; 161 #endif 162 default: 163 pr_debug("received unknown capsule type 0x%x\n", 164 cmd->fabrics.fctype); 165 req->error_loc = offsetof(struct nvmf_common_command, fctype); 166 return NVME_SC_INVALID_OPCODE | NVME_STATUS_DNR; 167 } 168 169 return 0; 170 } 171 172 static u16 nvmet_install_queue(struct nvmet_ctrl *ctrl, struct nvmet_req *req) 173 { 174 struct nvmf_connect_command *c = &req->cmd->connect; 175 u16 qid = le16_to_cpu(c->qid); 176 u16 sqsize = le16_to_cpu(c->sqsize); 177 struct nvmet_ctrl *old; 178 u16 mqes = NVME_CAP_MQES(ctrl->cap); 179 u16 ret; 180 181 if (!sqsize) { 182 pr_warn("queue size zero!\n"); 183 req->error_loc = offsetof(struct nvmf_connect_command, sqsize); 184 req->cqe->result.u32 = IPO_IATTR_CONNECT_SQE(sqsize); 185 ret = NVME_SC_CONNECT_INVALID_PARAM | NVME_STATUS_DNR; 186 goto err; 187 } 188 189 if (ctrl->sqs[qid] != NULL) { 190 pr_warn("qid %u has already been created\n", qid); 191 req->error_loc = offsetof(struct nvmf_connect_command, qid); 192 return NVME_SC_CMD_SEQ_ERROR | NVME_STATUS_DNR; 193 } 194 195 /* for fabrics, this value applies to only the I/O Submission Queues */ 196 if (qid && sqsize > mqes) { 197 pr_warn("sqsize %u is larger than MQES supported %u cntlid %d\n", 198 sqsize, mqes, ctrl->cntlid); 199 req->error_loc = offsetof(struct nvmf_connect_command, sqsize); 200 req->cqe->result.u32 = IPO_IATTR_CONNECT_SQE(sqsize); 201 return NVME_SC_CONNECT_INVALID_PARAM | NVME_STATUS_DNR; 202 } 203 204 old = cmpxchg(&req->sq->ctrl, NULL, ctrl); 205 if (old) { 206 pr_warn("queue already connected!\n"); 207 req->error_loc = offsetof(struct nvmf_connect_command, opcode); 208 return NVME_SC_CONNECT_CTRL_BUSY | NVME_STATUS_DNR; 209 } 210 211 /* note: convert queue size from 0's-based value to 1's-based value */ 212 nvmet_cq_setup(ctrl, req->cq, qid, sqsize + 1); 213 nvmet_sq_setup(ctrl, req->sq, qid, sqsize + 1); 214 215 if (c->cattr & NVME_CONNECT_DISABLE_SQFLOW) { 216 req->sq->sqhd_disabled = true; 217 req->cqe->sq_head = cpu_to_le16(0xffff); 218 } 219 220 if (ctrl->ops->install_queue) { 221 ret = ctrl->ops->install_queue(req->sq); 222 if (ret) { 223 pr_err("failed to install queue %d cntlid %d ret %x\n", 224 qid, ctrl->cntlid, ret); 225 ctrl->sqs[qid] = NULL; 226 goto err; 227 } 228 } 229 230 return 0; 231 232 err: 233 req->sq->ctrl = NULL; 234 return ret; 235 } 236 237 static u32 nvmet_connect_result(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq) 238 { 239 bool needs_auth = nvmet_has_auth(ctrl, sq); 240 key_serial_t keyid = nvmet_queue_tls_keyid(sq); 241 242 /* Do not authenticate I/O queues for secure concatenation */ 243 if (ctrl->concat && sq->qid) 244 needs_auth = false; 245 246 if (keyid) 247 pr_debug("%s: ctrl %d qid %d should %sauthenticate, tls psk %08x\n", 248 __func__, ctrl->cntlid, sq->qid, 249 needs_auth ? "" : "not ", keyid); 250 else 251 pr_debug("%s: ctrl %d qid %d should %sauthenticate%s\n", 252 __func__, ctrl->cntlid, sq->qid, 253 needs_auth ? "" : "not ", 254 ctrl->concat ? ", secure concatenation" : ""); 255 return (u32)ctrl->cntlid | 256 (needs_auth ? NVME_CONNECT_AUTHREQ_ATR : 0); 257 } 258 259 static void nvmet_execute_admin_connect(struct nvmet_req *req) 260 { 261 struct nvmf_connect_command *c = &req->cmd->connect; 262 struct nvmf_connect_data *d; 263 struct nvmet_ctrl *ctrl = NULL; 264 struct nvmet_alloc_ctrl_args args = { 265 .port = req->port, 266 .sq = req->sq, 267 .ops = req->ops, 268 .p2p_client = req->p2p_client, 269 .kato = le32_to_cpu(c->kato), 270 }; 271 272 if (!nvmet_check_transfer_len(req, sizeof(struct nvmf_connect_data))) 273 return; 274 275 d = kmalloc(sizeof(*d), GFP_KERNEL); 276 if (!d) { 277 args.status = NVME_SC_INTERNAL; 278 goto complete; 279 } 280 281 args.status = nvmet_copy_from_sgl(req, 0, d, sizeof(*d)); 282 if (args.status) 283 goto out; 284 285 if (c->recfmt != 0) { 286 pr_warn("invalid connect version (%d).\n", 287 le16_to_cpu(c->recfmt)); 288 args.error_loc = offsetof(struct nvmf_connect_command, recfmt); 289 args.status = NVME_SC_CONNECT_FORMAT | NVME_STATUS_DNR; 290 goto out; 291 } 292 293 if (unlikely(d->cntlid != cpu_to_le16(0xffff))) { 294 pr_warn("connect attempt for invalid controller ID %#x\n", 295 d->cntlid); 296 args.status = NVME_SC_CONNECT_INVALID_PARAM | NVME_STATUS_DNR; 297 args.result = IPO_IATTR_CONNECT_DATA(cntlid); 298 goto out; 299 } 300 301 d->subsysnqn[NVMF_NQN_FIELD_LEN - 1] = '\0'; 302 d->hostnqn[NVMF_NQN_FIELD_LEN - 1] = '\0'; 303 304 args.subsysnqn = d->subsysnqn; 305 args.hostnqn = d->hostnqn; 306 args.hostid = &d->hostid; 307 args.kato = le32_to_cpu(c->kato); 308 309 ctrl = nvmet_alloc_ctrl(&args); 310 if (!ctrl) 311 goto out; 312 313 args.status = nvmet_install_queue(ctrl, req); 314 if (args.status) { 315 nvmet_ctrl_put(ctrl); 316 goto out; 317 } 318 319 args.result = cpu_to_le32(nvmet_connect_result(ctrl, req->sq)); 320 out: 321 kfree(d); 322 complete: 323 req->error_loc = args.error_loc; 324 req->cqe->result.u32 = args.result; 325 nvmet_req_complete(req, args.status); 326 } 327 328 static void nvmet_execute_io_connect(struct nvmet_req *req) 329 { 330 struct nvmf_connect_command *c = &req->cmd->connect; 331 struct nvmf_connect_data *d; 332 struct nvmet_ctrl *ctrl; 333 u16 qid = le16_to_cpu(c->qid); 334 u16 status; 335 336 if (!nvmet_check_transfer_len(req, sizeof(struct nvmf_connect_data))) 337 return; 338 339 d = kmalloc(sizeof(*d), GFP_KERNEL); 340 if (!d) { 341 status = NVME_SC_INTERNAL; 342 goto complete; 343 } 344 345 status = nvmet_copy_from_sgl(req, 0, d, sizeof(*d)); 346 if (status) 347 goto out; 348 349 if (c->recfmt != 0) { 350 pr_warn("invalid connect version (%d).\n", 351 le16_to_cpu(c->recfmt)); 352 status = NVME_SC_CONNECT_FORMAT | NVME_STATUS_DNR; 353 goto out; 354 } 355 356 d->subsysnqn[NVMF_NQN_FIELD_LEN - 1] = '\0'; 357 d->hostnqn[NVMF_NQN_FIELD_LEN - 1] = '\0'; 358 ctrl = nvmet_ctrl_find_get(d->subsysnqn, d->hostnqn, 359 le16_to_cpu(d->cntlid), req); 360 if (!ctrl) { 361 status = NVME_SC_CONNECT_INVALID_PARAM | NVME_STATUS_DNR; 362 goto out; 363 } 364 365 if (unlikely(qid > ctrl->subsys->max_qid)) { 366 pr_warn("invalid queue id (%d)\n", qid); 367 status = NVME_SC_CONNECT_INVALID_PARAM | NVME_STATUS_DNR; 368 req->cqe->result.u32 = IPO_IATTR_CONNECT_SQE(qid); 369 goto out_ctrl_put; 370 } 371 372 status = nvmet_install_queue(ctrl, req); 373 if (status) 374 goto out_ctrl_put; 375 376 pr_debug("adding queue %d to ctrl %d.\n", qid, ctrl->cntlid); 377 req->cqe->result.u32 = cpu_to_le32(nvmet_connect_result(ctrl, req->sq)); 378 out: 379 kfree(d); 380 complete: 381 nvmet_req_complete(req, status); 382 return; 383 384 out_ctrl_put: 385 nvmet_ctrl_put(ctrl); 386 goto out; 387 } 388 389 u32 nvmet_connect_cmd_data_len(struct nvmet_req *req) 390 { 391 struct nvme_command *cmd = req->cmd; 392 393 if (!nvme_is_fabrics(cmd) || 394 cmd->fabrics.fctype != nvme_fabrics_type_connect) 395 return 0; 396 397 return sizeof(struct nvmf_connect_data); 398 } 399 400 u16 nvmet_parse_connect_cmd(struct nvmet_req *req) 401 { 402 struct nvme_command *cmd = req->cmd; 403 404 if (!nvme_is_fabrics(cmd)) { 405 pr_debug("invalid command 0x%x on unconnected queue.\n", 406 cmd->fabrics.opcode); 407 req->error_loc = offsetof(struct nvme_common_command, opcode); 408 return NVME_SC_INVALID_OPCODE | NVME_STATUS_DNR; 409 } 410 if (cmd->fabrics.fctype != nvme_fabrics_type_connect) { 411 pr_debug("invalid capsule type 0x%x on unconnected queue.\n", 412 cmd->fabrics.fctype); 413 req->error_loc = offsetof(struct nvmf_common_command, fctype); 414 return NVME_SC_INVALID_OPCODE | NVME_STATUS_DNR; 415 } 416 417 if (cmd->connect.qid == 0) 418 req->execute = nvmet_execute_admin_connect; 419 else 420 req->execute = nvmet_execute_io_connect; 421 return 0; 422 } 423