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