1 // SPDX-License-Identifier: GPL-2.0
3 * NVMe Fabrics command implementation.
4 * Copyright (c) 2015-2016 HGST, a Western Digital Company.
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 #include <linux/blkdev.h>
10 static void nvmet_execute_prop_set(struct nvmet_req *req)
12 u64 val = le64_to_cpu(req->cmd->prop_set.value);
15 if (!nvmet_check_transfer_len(req, 0))
18 if (req->cmd->prop_set.attrib & 1) {
20 offsetof(struct nvmf_property_set_command, attrib);
21 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
25 switch (le32_to_cpu(req->cmd->prop_set.offset)) {
27 nvmet_update_cc(req->sq->ctrl, val);
31 offsetof(struct nvmf_property_set_command, offset);
32 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
35 nvmet_req_complete(req, status);
38 static void nvmet_execute_prop_get(struct nvmet_req *req)
40 struct nvmet_ctrl *ctrl = req->sq->ctrl;
44 if (!nvmet_check_transfer_len(req, 0))
47 if (req->cmd->prop_get.attrib & 1) {
48 switch (le32_to_cpu(req->cmd->prop_get.offset)) {
53 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
57 switch (le32_to_cpu(req->cmd->prop_get.offset)) {
59 val = ctrl->subsys->ver;
68 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
73 if (status && req->cmd->prop_get.attrib & 1) {
75 offsetof(struct nvmf_property_get_command, offset);
78 offsetof(struct nvmf_property_get_command, attrib);
81 req->cqe->result.u64 = cpu_to_le64(val);
82 nvmet_req_complete(req, status);
85 u16 nvmet_parse_fabrics_admin_cmd(struct nvmet_req *req)
87 struct nvme_command *cmd = req->cmd;
89 switch (cmd->fabrics.fctype) {
90 case nvme_fabrics_type_property_set:
91 req->execute = nvmet_execute_prop_set;
93 case nvme_fabrics_type_property_get:
94 req->execute = nvmet_execute_prop_get;
96 #ifdef CONFIG_NVME_TARGET_AUTH
97 case nvme_fabrics_type_auth_send:
98 req->execute = nvmet_execute_auth_send;
100 case nvme_fabrics_type_auth_receive:
101 req->execute = nvmet_execute_auth_receive;
105 pr_debug("received unknown capsule type 0x%x\n",
106 cmd->fabrics.fctype);
107 req->error_loc = offsetof(struct nvmf_common_command, fctype);
108 return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
114 u16 nvmet_parse_fabrics_io_cmd(struct nvmet_req *req)
116 struct nvme_command *cmd = req->cmd;
118 switch (cmd->fabrics.fctype) {
119 #ifdef CONFIG_NVME_TARGET_AUTH
120 case nvme_fabrics_type_auth_send:
121 req->execute = nvmet_execute_auth_send;
123 case nvme_fabrics_type_auth_receive:
124 req->execute = nvmet_execute_auth_receive;
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_SC_DNR;
137 static u16 nvmet_install_queue(struct nvmet_ctrl *ctrl, struct nvmet_req *req)
139 struct nvmf_connect_command *c = &req->cmd->connect;
140 u16 qid = le16_to_cpu(c->qid);
141 u16 sqsize = le16_to_cpu(c->sqsize);
142 struct nvmet_ctrl *old;
143 u16 mqes = NVME_CAP_MQES(ctrl->cap);
147 pr_warn("queue size zero!\n");
148 req->error_loc = offsetof(struct nvmf_connect_command, sqsize);
149 req->cqe->result.u32 = IPO_IATTR_CONNECT_SQE(sqsize);
150 ret = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
154 if (ctrl->sqs[qid] != NULL) {
155 pr_warn("qid %u has already been created\n", qid);
156 req->error_loc = offsetof(struct nvmf_connect_command, qid);
157 return NVME_SC_CMD_SEQ_ERROR | NVME_SC_DNR;
161 pr_warn("sqsize %u is larger than MQES supported %u cntlid %d\n",
162 sqsize, mqes, ctrl->cntlid);
163 req->error_loc = offsetof(struct nvmf_connect_command, sqsize);
164 req->cqe->result.u32 = IPO_IATTR_CONNECT_SQE(sqsize);
165 return NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
168 old = cmpxchg(&req->sq->ctrl, NULL, ctrl);
170 pr_warn("queue already connected!\n");
171 req->error_loc = offsetof(struct nvmf_connect_command, opcode);
172 return NVME_SC_CONNECT_CTRL_BUSY | NVME_SC_DNR;
175 /* note: convert queue size from 0's-based value to 1's-based value */
176 nvmet_cq_setup(ctrl, req->cq, qid, sqsize + 1);
177 nvmet_sq_setup(ctrl, req->sq, qid, sqsize + 1);
179 if (c->cattr & NVME_CONNECT_DISABLE_SQFLOW) {
180 req->sq->sqhd_disabled = true;
181 req->cqe->sq_head = cpu_to_le16(0xffff);
184 if (ctrl->ops->install_queue) {
185 ret = ctrl->ops->install_queue(req->sq);
187 pr_err("failed to install queue %d cntlid %d ret %x\n",
188 qid, ctrl->cntlid, ret);
189 ctrl->sqs[qid] = NULL;
197 req->sq->ctrl = NULL;
201 static u32 nvmet_connect_result(struct nvmet_ctrl *ctrl)
203 return (u32)ctrl->cntlid |
204 (nvmet_has_auth(ctrl) ? NVME_CONNECT_AUTHREQ_ATR : 0);
207 static void nvmet_execute_admin_connect(struct nvmet_req *req)
209 struct nvmf_connect_command *c = &req->cmd->connect;
210 struct nvmf_connect_data *d;
211 struct nvmet_ctrl *ctrl = NULL;
215 if (!nvmet_check_transfer_len(req, sizeof(struct nvmf_connect_data)))
218 d = kmalloc(sizeof(*d), GFP_KERNEL);
220 status = NVME_SC_INTERNAL;
224 status = nvmet_copy_from_sgl(req, 0, d, sizeof(*d));
228 /* zero out initial completion result, assign values as needed */
229 req->cqe->result.u32 = 0;
231 if (c->recfmt != 0) {
232 pr_warn("invalid connect version (%d).\n",
233 le16_to_cpu(c->recfmt));
234 req->error_loc = offsetof(struct nvmf_connect_command, recfmt);
235 status = NVME_SC_CONNECT_FORMAT | NVME_SC_DNR;
239 if (unlikely(d->cntlid != cpu_to_le16(0xffff))) {
240 pr_warn("connect attempt for invalid controller ID %#x\n",
242 status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
243 req->cqe->result.u32 = IPO_IATTR_CONNECT_DATA(cntlid);
247 d->subsysnqn[NVMF_NQN_FIELD_LEN - 1] = '\0';
248 d->hostnqn[NVMF_NQN_FIELD_LEN - 1] = '\0';
249 status = nvmet_alloc_ctrl(d->subsysnqn, d->hostnqn, req,
250 le32_to_cpu(c->kato), &ctrl);
254 ctrl->pi_support = ctrl->port->pi_enable && ctrl->subsys->pi_support;
256 uuid_copy(&ctrl->hostid, &d->hostid);
258 ret = nvmet_setup_auth(ctrl);
260 pr_err("Failed to setup authentication, error %d\n", ret);
261 nvmet_ctrl_put(ctrl);
263 status = (NVME_SC_CONNECT_INVALID_HOST | NVME_SC_DNR);
265 status = NVME_SC_INTERNAL;
269 status = nvmet_install_queue(ctrl, req);
271 nvmet_ctrl_put(ctrl);
275 pr_info("creating %s controller %d for subsystem %s for NQN %s%s%s.\n",
276 nvmet_is_disc_subsys(ctrl->subsys) ? "discovery" : "nvm",
277 ctrl->cntlid, ctrl->subsys->subsysnqn, ctrl->hostnqn,
278 ctrl->pi_support ? " T10-PI is enabled" : "",
279 nvmet_has_auth(ctrl) ? " with DH-HMAC-CHAP" : "");
280 req->cqe->result.u32 = cpu_to_le32(nvmet_connect_result(ctrl));
284 nvmet_req_complete(req, status);
287 static void nvmet_execute_io_connect(struct nvmet_req *req)
289 struct nvmf_connect_command *c = &req->cmd->connect;
290 struct nvmf_connect_data *d;
291 struct nvmet_ctrl *ctrl;
292 u16 qid = le16_to_cpu(c->qid);
295 if (!nvmet_check_transfer_len(req, sizeof(struct nvmf_connect_data)))
298 d = kmalloc(sizeof(*d), GFP_KERNEL);
300 status = NVME_SC_INTERNAL;
304 status = nvmet_copy_from_sgl(req, 0, d, sizeof(*d));
308 /* zero out initial completion result, assign values as needed */
309 req->cqe->result.u32 = 0;
311 if (c->recfmt != 0) {
312 pr_warn("invalid connect version (%d).\n",
313 le16_to_cpu(c->recfmt));
314 status = NVME_SC_CONNECT_FORMAT | NVME_SC_DNR;
318 d->subsysnqn[NVMF_NQN_FIELD_LEN - 1] = '\0';
319 d->hostnqn[NVMF_NQN_FIELD_LEN - 1] = '\0';
320 ctrl = nvmet_ctrl_find_get(d->subsysnqn, d->hostnqn,
321 le16_to_cpu(d->cntlid), req);
323 status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
327 if (unlikely(qid > ctrl->subsys->max_qid)) {
328 pr_warn("invalid queue id (%d)\n", qid);
329 status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
330 req->cqe->result.u32 = IPO_IATTR_CONNECT_SQE(qid);
334 status = nvmet_install_queue(ctrl, req);
338 pr_debug("adding queue %d to ctrl %d.\n", qid, ctrl->cntlid);
339 req->cqe->result.u32 = cpu_to_le32(nvmet_connect_result(ctrl));
343 nvmet_req_complete(req, status);
347 nvmet_ctrl_put(ctrl);
351 u16 nvmet_parse_connect_cmd(struct nvmet_req *req)
353 struct nvme_command *cmd = req->cmd;
355 if (!nvme_is_fabrics(cmd)) {
356 pr_debug("invalid command 0x%x on unconnected queue.\n",
357 cmd->fabrics.opcode);
358 req->error_loc = offsetof(struct nvme_common_command, opcode);
359 return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
361 if (cmd->fabrics.fctype != nvme_fabrics_type_connect) {
362 pr_debug("invalid capsule type 0x%x on unconnected queue.\n",
363 cmd->fabrics.fctype);
364 req->error_loc = offsetof(struct nvmf_common_command, fctype);
365 return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
368 if (cmd->connect.qid == 0)
369 req->execute = nvmet_execute_admin_connect;
371 req->execute = nvmet_execute_io_connect;