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 (req->cmd->prop_set.attrib & 1) {
17 offsetof(struct nvmf_property_set_command, attrib);
18 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
22 switch (le32_to_cpu(req->cmd->prop_set.offset)) {
24 nvmet_update_cc(req->sq->ctrl, val);
28 offsetof(struct nvmf_property_set_command, offset);
29 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
32 nvmet_req_complete(req, status);
35 static void nvmet_execute_prop_get(struct nvmet_req *req)
37 struct nvmet_ctrl *ctrl = req->sq->ctrl;
41 if (req->cmd->prop_get.attrib & 1) {
42 switch (le32_to_cpu(req->cmd->prop_get.offset)) {
47 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
51 switch (le32_to_cpu(req->cmd->prop_get.offset)) {
53 val = ctrl->subsys->ver;
62 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
67 if (status && req->cmd->prop_get.attrib & 1) {
69 offsetof(struct nvmf_property_get_command, offset);
72 offsetof(struct nvmf_property_get_command, attrib);
75 req->cqe->result.u64 = cpu_to_le64(val);
76 nvmet_req_complete(req, status);
79 u16 nvmet_parse_fabrics_cmd(struct nvmet_req *req)
81 struct nvme_command *cmd = req->cmd;
83 switch (cmd->fabrics.fctype) {
84 case nvme_fabrics_type_property_set:
86 req->execute = nvmet_execute_prop_set;
88 case nvme_fabrics_type_property_get:
90 req->execute = nvmet_execute_prop_get;
93 pr_err("received unknown capsule type 0x%x\n",
95 req->error_loc = offsetof(struct nvmf_common_command, fctype);
96 return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
102 static u16 nvmet_install_queue(struct nvmet_ctrl *ctrl, struct nvmet_req *req)
104 struct nvmf_connect_command *c = &req->cmd->connect;
105 u16 qid = le16_to_cpu(c->qid);
106 u16 sqsize = le16_to_cpu(c->sqsize);
107 struct nvmet_ctrl *old;
109 old = cmpxchg(&req->sq->ctrl, NULL, ctrl);
111 pr_warn("queue already connected!\n");
112 req->error_loc = offsetof(struct nvmf_connect_command, opcode);
113 return NVME_SC_CONNECT_CTRL_BUSY | NVME_SC_DNR;
116 pr_warn("queue size zero!\n");
117 req->error_loc = offsetof(struct nvmf_connect_command, sqsize);
118 return NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
121 /* note: convert queue size from 0's-based value to 1's-based value */
122 nvmet_cq_setup(ctrl, req->cq, qid, sqsize + 1);
123 nvmet_sq_setup(ctrl, req->sq, qid, sqsize + 1);
125 if (c->cattr & NVME_CONNECT_DISABLE_SQFLOW) {
126 req->sq->sqhd_disabled = true;
127 req->cqe->sq_head = cpu_to_le16(0xffff);
130 if (ctrl->ops->install_queue) {
131 u16 ret = ctrl->ops->install_queue(req->sq);
134 pr_err("failed to install queue %d cntlid %d ret %x\n",
135 qid, ret, ctrl->cntlid);
143 static void nvmet_execute_admin_connect(struct nvmet_req *req)
145 struct nvmf_connect_command *c = &req->cmd->connect;
146 struct nvmf_connect_data *d;
147 struct nvmet_ctrl *ctrl = NULL;
150 d = kmalloc(sizeof(*d), GFP_KERNEL);
152 status = NVME_SC_INTERNAL;
156 status = nvmet_copy_from_sgl(req, 0, d, sizeof(*d));
160 /* zero out initial completion result, assign values as needed */
161 req->cqe->result.u32 = 0;
163 if (c->recfmt != 0) {
164 pr_warn("invalid connect version (%d).\n",
165 le16_to_cpu(c->recfmt));
166 req->error_loc = offsetof(struct nvmf_connect_command, recfmt);
167 status = NVME_SC_CONNECT_FORMAT | NVME_SC_DNR;
171 if (unlikely(d->cntlid != cpu_to_le16(0xffff))) {
172 pr_warn("connect attempt for invalid controller ID %#x\n",
174 status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
175 req->cqe->result.u32 = IPO_IATTR_CONNECT_DATA(cntlid);
179 status = nvmet_alloc_ctrl(d->subsysnqn, d->hostnqn, req,
180 le32_to_cpu(c->kato), &ctrl);
182 if (status == (NVME_SC_INVALID_FIELD | NVME_SC_DNR))
184 offsetof(struct nvme_common_command, opcode);
188 uuid_copy(&ctrl->hostid, &d->hostid);
190 status = nvmet_install_queue(ctrl, req);
192 nvmet_ctrl_put(ctrl);
196 pr_info("creating controller %d for subsystem %s for NQN %s.\n",
197 ctrl->cntlid, ctrl->subsys->subsysnqn, ctrl->hostnqn);
198 req->cqe->result.u16 = cpu_to_le16(ctrl->cntlid);
203 nvmet_req_complete(req, status);
206 static void nvmet_execute_io_connect(struct nvmet_req *req)
208 struct nvmf_connect_command *c = &req->cmd->connect;
209 struct nvmf_connect_data *d;
210 struct nvmet_ctrl *ctrl = NULL;
211 u16 qid = le16_to_cpu(c->qid);
214 d = kmalloc(sizeof(*d), GFP_KERNEL);
216 status = NVME_SC_INTERNAL;
220 status = nvmet_copy_from_sgl(req, 0, d, sizeof(*d));
224 /* zero out initial completion result, assign values as needed */
225 req->cqe->result.u32 = 0;
227 if (c->recfmt != 0) {
228 pr_warn("invalid connect version (%d).\n",
229 le16_to_cpu(c->recfmt));
230 status = NVME_SC_CONNECT_FORMAT | NVME_SC_DNR;
234 status = nvmet_ctrl_find_get(d->subsysnqn, d->hostnqn,
235 le16_to_cpu(d->cntlid),
240 if (unlikely(qid > ctrl->subsys->max_qid)) {
241 pr_warn("invalid queue id (%d)\n", qid);
242 status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
243 req->cqe->result.u32 = IPO_IATTR_CONNECT_SQE(qid);
247 status = nvmet_install_queue(ctrl, req);
249 /* pass back cntlid that had the issue of installing queue */
250 req->cqe->result.u16 = cpu_to_le16(ctrl->cntlid);
254 pr_debug("adding queue %d to ctrl %d.\n", qid, ctrl->cntlid);
259 nvmet_req_complete(req, status);
263 nvmet_ctrl_put(ctrl);
267 u16 nvmet_parse_connect_cmd(struct nvmet_req *req)
269 struct nvme_command *cmd = req->cmd;
271 if (!nvme_is_fabrics(cmd)) {
272 pr_err("invalid command 0x%x on unconnected queue.\n",
273 cmd->fabrics.opcode);
274 req->error_loc = offsetof(struct nvme_common_command, opcode);
275 return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
277 if (cmd->fabrics.fctype != nvme_fabrics_type_connect) {
278 pr_err("invalid capsule type 0x%x on unconnected queue.\n",
279 cmd->fabrics.fctype);
280 req->error_loc = offsetof(struct nvmf_common_command, fctype);
281 return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
284 req->data_len = sizeof(struct nvmf_connect_data);
285 if (cmd->connect.qid == 0)
286 req->execute = nvmet_execute_admin_connect;
288 req->execute = nvmet_execute_io_connect;