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_STATUS_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_STATUS_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_STATUS_DNR;
57 switch (le32_to_cpu(req->cmd->prop_get.offset)) {
59 val = ctrl->subsys->ver;
68 val = NVME_CAP_TIMEOUT(ctrl->csts);
71 status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
76 if (status && req->cmd->prop_get.attrib & 1) {
78 offsetof(struct nvmf_property_get_command, offset);
81 offsetof(struct nvmf_property_get_command, attrib);
84 req->cqe->result.u64 = cpu_to_le64(val);
85 nvmet_req_complete(req, status);
88 u32 nvmet_fabrics_admin_cmd_data_len(struct nvmet_req *req)
90 struct nvme_command *cmd = req->cmd;
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);
104 u16 nvmet_parse_fabrics_admin_cmd(struct nvmet_req *req)
106 struct nvme_command *cmd = req->cmd;
108 switch (cmd->fabrics.fctype) {
109 case nvme_fabrics_type_property_set:
110 req->execute = nvmet_execute_prop_set;
112 case nvme_fabrics_type_property_get:
113 req->execute = nvmet_execute_prop_get;
115 #ifdef CONFIG_NVME_TARGET_AUTH
116 case nvme_fabrics_type_auth_send:
117 req->execute = nvmet_execute_auth_send;
119 case nvme_fabrics_type_auth_receive:
120 req->execute = nvmet_execute_auth_receive;
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;
133 u32 nvmet_fabrics_io_cmd_data_len(struct nvmet_req *req)
135 struct nvme_command *cmd = req->cmd;
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);
149 u16 nvmet_parse_fabrics_io_cmd(struct nvmet_req *req)
151 struct nvme_command *cmd = req->cmd;
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;
158 case nvme_fabrics_type_auth_receive:
159 req->execute = nvmet_execute_auth_receive;
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;
172 static u16 nvmet_install_queue(struct nvmet_ctrl *ctrl, struct nvmet_req *req)
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);
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;
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;
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;
204 old = cmpxchg(&req->sq->ctrl, NULL, ctrl);
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;
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);
215 if (c->cattr & NVME_CONNECT_DISABLE_SQFLOW) {
216 req->sq->sqhd_disabled = true;
217 req->cqe->sq_head = cpu_to_le16(0xffff);
220 if (ctrl->ops->install_queue) {
221 ret = ctrl->ops->install_queue(req->sq);
223 pr_err("failed to install queue %d cntlid %d ret %x\n",
224 qid, ctrl->cntlid, ret);
225 ctrl->sqs[qid] = NULL;
233 req->sq->ctrl = NULL;
237 static u32 nvmet_connect_result(struct nvmet_ctrl *ctrl)
239 return (u32)ctrl->cntlid |
240 (nvmet_has_auth(ctrl) ? NVME_CONNECT_AUTHREQ_ATR : 0);
243 static void nvmet_execute_admin_connect(struct nvmet_req *req)
245 struct nvmf_connect_command *c = &req->cmd->connect;
246 struct nvmf_connect_data *d;
247 struct nvmet_ctrl *ctrl = NULL;
248 struct nvmet_alloc_ctrl_args args = {
251 .p2p_client = req->p2p_client,
252 .kato = le32_to_cpu(c->kato),
255 if (!nvmet_check_transfer_len(req, sizeof(struct nvmf_connect_data)))
258 d = kmalloc(sizeof(*d), GFP_KERNEL);
260 args.status = NVME_SC_INTERNAL;
264 args.status = nvmet_copy_from_sgl(req, 0, d, sizeof(*d));
268 if (c->recfmt != 0) {
269 pr_warn("invalid connect version (%d).\n",
270 le16_to_cpu(c->recfmt));
271 args.error_loc = offsetof(struct nvmf_connect_command, recfmt);
272 args.status = NVME_SC_CONNECT_FORMAT | NVME_STATUS_DNR;
276 if (unlikely(d->cntlid != cpu_to_le16(0xffff))) {
277 pr_warn("connect attempt for invalid controller ID %#x\n",
279 args.status = NVME_SC_CONNECT_INVALID_PARAM | NVME_STATUS_DNR;
280 args.result = IPO_IATTR_CONNECT_DATA(cntlid);
284 d->subsysnqn[NVMF_NQN_FIELD_LEN - 1] = '\0';
285 d->hostnqn[NVMF_NQN_FIELD_LEN - 1] = '\0';
287 args.subsysnqn = d->subsysnqn;
288 args.hostnqn = d->hostnqn;
289 args.hostid = &d->hostid;
290 args.kato = le32_to_cpu(c->kato);
292 ctrl = nvmet_alloc_ctrl(&args);
296 args.status = nvmet_install_queue(ctrl, req);
298 nvmet_ctrl_put(ctrl);
302 args.result = cpu_to_le32(nvmet_connect_result(ctrl));
306 req->error_loc = args.error_loc;
307 req->cqe->result.u32 = args.result;
308 nvmet_req_complete(req, args.status);
311 static void nvmet_execute_io_connect(struct nvmet_req *req)
313 struct nvmf_connect_command *c = &req->cmd->connect;
314 struct nvmf_connect_data *d;
315 struct nvmet_ctrl *ctrl;
316 u16 qid = le16_to_cpu(c->qid);
319 if (!nvmet_check_transfer_len(req, sizeof(struct nvmf_connect_data)))
322 d = kmalloc(sizeof(*d), GFP_KERNEL);
324 status = NVME_SC_INTERNAL;
328 status = nvmet_copy_from_sgl(req, 0, d, sizeof(*d));
332 if (c->recfmt != 0) {
333 pr_warn("invalid connect version (%d).\n",
334 le16_to_cpu(c->recfmt));
335 status = NVME_SC_CONNECT_FORMAT | NVME_STATUS_DNR;
339 d->subsysnqn[NVMF_NQN_FIELD_LEN - 1] = '\0';
340 d->hostnqn[NVMF_NQN_FIELD_LEN - 1] = '\0';
341 ctrl = nvmet_ctrl_find_get(d->subsysnqn, d->hostnqn,
342 le16_to_cpu(d->cntlid), req);
344 status = NVME_SC_CONNECT_INVALID_PARAM | NVME_STATUS_DNR;
348 if (unlikely(qid > ctrl->subsys->max_qid)) {
349 pr_warn("invalid queue id (%d)\n", qid);
350 status = NVME_SC_CONNECT_INVALID_PARAM | NVME_STATUS_DNR;
351 req->cqe->result.u32 = IPO_IATTR_CONNECT_SQE(qid);
355 status = nvmet_install_queue(ctrl, req);
359 pr_debug("adding queue %d to ctrl %d.\n", qid, ctrl->cntlid);
360 req->cqe->result.u32 = cpu_to_le32(nvmet_connect_result(ctrl));
364 nvmet_req_complete(req, status);
368 nvmet_ctrl_put(ctrl);
372 u32 nvmet_connect_cmd_data_len(struct nvmet_req *req)
374 struct nvme_command *cmd = req->cmd;
376 if (!nvme_is_fabrics(cmd) ||
377 cmd->fabrics.fctype != nvme_fabrics_type_connect)
380 return sizeof(struct nvmf_connect_data);
383 u16 nvmet_parse_connect_cmd(struct nvmet_req *req)
385 struct nvme_command *cmd = req->cmd;
387 if (!nvme_is_fabrics(cmd)) {
388 pr_debug("invalid command 0x%x on unconnected queue.\n",
389 cmd->fabrics.opcode);
390 req->error_loc = offsetof(struct nvme_common_command, opcode);
391 return NVME_SC_INVALID_OPCODE | NVME_STATUS_DNR;
393 if (cmd->fabrics.fctype != nvme_fabrics_type_connect) {
394 pr_debug("invalid capsule type 0x%x on unconnected queue.\n",
395 cmd->fabrics.fctype);
396 req->error_loc = offsetof(struct nvmf_common_command, fctype);
397 return NVME_SC_INVALID_OPCODE | NVME_STATUS_DNR;
400 if (cmd->connect.qid == 0)
401 req->execute = nvmet_execute_admin_connect;
403 req->execute = nvmet_execute_io_connect;