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
10static 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, len: 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_SC_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(ctrl: req->sq->ctrl, new: val);
28 break;
29 default:
30 req->error_loc =
31 offsetof(struct nvmf_property_set_command, offset);
32 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
33 }
34out:
35 nvmet_req_complete(req, status);
36}
37
38static 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, len: 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_SC_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 default:
68 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
69 break;
70 }
71 }
72
73 if (status && req->cmd->prop_get.attrib & 1) {
74 req->error_loc =
75 offsetof(struct nvmf_property_get_command, offset);
76 } else {
77 req->error_loc =
78 offsetof(struct nvmf_property_get_command, attrib);
79 }
80
81 req->cqe->result.u64 = cpu_to_le64(val);
82 nvmet_req_complete(req, status);
83}
84
85u16 nvmet_parse_fabrics_admin_cmd(struct nvmet_req *req)
86{
87 struct nvme_command *cmd = req->cmd;
88
89 switch (cmd->fabrics.fctype) {
90 case nvme_fabrics_type_property_set:
91 req->execute = nvmet_execute_prop_set;
92 break;
93 case nvme_fabrics_type_property_get:
94 req->execute = nvmet_execute_prop_get;
95 break;
96#ifdef CONFIG_NVME_TARGET_AUTH
97 case nvme_fabrics_type_auth_send:
98 req->execute = nvmet_execute_auth_send;
99 break;
100 case nvme_fabrics_type_auth_receive:
101 req->execute = nvmet_execute_auth_receive;
102 break;
103#endif
104 default:
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;
109 }
110
111 return 0;
112}
113
114u16 nvmet_parse_fabrics_io_cmd(struct nvmet_req *req)
115{
116 struct nvme_command *cmd = req->cmd;
117
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;
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_SC_DNR;
132 }
133
134 return 0;
135}
136
137static u16 nvmet_install_queue(struct nvmet_ctrl *ctrl, struct nvmet_req *req)
138{
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);
144 u16 ret;
145
146 if (!sqsize) {
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;
151 goto err;
152 }
153
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;
158 }
159
160 /* for fabrics, this value applies to only the I/O Submission Queues */
161 if (qid && sqsize > mqes) {
162 pr_warn("sqsize %u is larger than MQES supported %u cntlid %d\n",
163 sqsize, mqes, ctrl->cntlid);
164 req->error_loc = offsetof(struct nvmf_connect_command, sqsize);
165 req->cqe->result.u32 = IPO_IATTR_CONNECT_SQE(sqsize);
166 return NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
167 }
168
169 old = cmpxchg(&req->sq->ctrl, NULL, ctrl);
170 if (old) {
171 pr_warn("queue already connected!\n");
172 req->error_loc = offsetof(struct nvmf_connect_command, opcode);
173 return NVME_SC_CONNECT_CTRL_BUSY | NVME_SC_DNR;
174 }
175
176 /* note: convert queue size from 0's-based value to 1's-based value */
177 nvmet_cq_setup(ctrl, cq: req->cq, qid, size: sqsize + 1);
178 nvmet_sq_setup(ctrl, sq: req->sq, qid, size: sqsize + 1);
179
180 if (c->cattr & NVME_CONNECT_DISABLE_SQFLOW) {
181 req->sq->sqhd_disabled = true;
182 req->cqe->sq_head = cpu_to_le16(0xffff);
183 }
184
185 if (ctrl->ops->install_queue) {
186 ret = ctrl->ops->install_queue(req->sq);
187 if (ret) {
188 pr_err("failed to install queue %d cntlid %d ret %x\n",
189 qid, ctrl->cntlid, ret);
190 ctrl->sqs[qid] = NULL;
191 goto err;
192 }
193 }
194
195 return 0;
196
197err:
198 req->sq->ctrl = NULL;
199 return ret;
200}
201
202static u32 nvmet_connect_result(struct nvmet_ctrl *ctrl)
203{
204 return (u32)ctrl->cntlid |
205 (nvmet_has_auth(ctrl) ? NVME_CONNECT_AUTHREQ_ATR : 0);
206}
207
208static void nvmet_execute_admin_connect(struct nvmet_req *req)
209{
210 struct nvmf_connect_command *c = &req->cmd->connect;
211 struct nvmf_connect_data *d;
212 struct nvmet_ctrl *ctrl = NULL;
213 u16 status;
214 int ret;
215
216 if (!nvmet_check_transfer_len(req, len: sizeof(struct nvmf_connect_data)))
217 return;
218
219 d = kmalloc(size: sizeof(*d), GFP_KERNEL);
220 if (!d) {
221 status = NVME_SC_INTERNAL;
222 goto complete;
223 }
224
225 status = nvmet_copy_from_sgl(req, off: 0, buf: d, len: sizeof(*d));
226 if (status)
227 goto out;
228
229 /* zero out initial completion result, assign values as needed */
230 req->cqe->result.u32 = 0;
231
232 if (c->recfmt != 0) {
233 pr_warn("invalid connect version (%d).\n",
234 le16_to_cpu(c->recfmt));
235 req->error_loc = offsetof(struct nvmf_connect_command, recfmt);
236 status = NVME_SC_CONNECT_FORMAT | NVME_SC_DNR;
237 goto out;
238 }
239
240 if (unlikely(d->cntlid != cpu_to_le16(0xffff))) {
241 pr_warn("connect attempt for invalid controller ID %#x\n",
242 d->cntlid);
243 status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
244 req->cqe->result.u32 = IPO_IATTR_CONNECT_DATA(cntlid);
245 goto out;
246 }
247
248 d->subsysnqn[NVMF_NQN_FIELD_LEN - 1] = '\0';
249 d->hostnqn[NVMF_NQN_FIELD_LEN - 1] = '\0';
250 status = nvmet_alloc_ctrl(subsysnqn: d->subsysnqn, hostnqn: d->hostnqn, req,
251 le32_to_cpu(c->kato), ctrlp: &ctrl);
252 if (status)
253 goto out;
254
255 uuid_copy(dst: &ctrl->hostid, src: &d->hostid);
256
257 ret = nvmet_setup_auth(ctrl);
258 if (ret < 0) {
259 pr_err("Failed to setup authentication, error %d\n", ret);
260 nvmet_ctrl_put(ctrl);
261 if (ret == -EPERM)
262 status = (NVME_SC_CONNECT_INVALID_HOST | NVME_SC_DNR);
263 else
264 status = NVME_SC_INTERNAL;
265 goto out;
266 }
267
268 status = nvmet_install_queue(ctrl, req);
269 if (status) {
270 nvmet_ctrl_put(ctrl);
271 goto out;
272 }
273
274 pr_info("creating %s controller %d for subsystem %s for NQN %s%s%s.\n",
275 nvmet_is_disc_subsys(ctrl->subsys) ? "discovery" : "nvm",
276 ctrl->cntlid, ctrl->subsys->subsysnqn, ctrl->hostnqn,
277 ctrl->pi_support ? " T10-PI is enabled" : "",
278 nvmet_has_auth(ctrl) ? " with DH-HMAC-CHAP" : "");
279 req->cqe->result.u32 = cpu_to_le32(nvmet_connect_result(ctrl));
280out:
281 kfree(objp: d);
282complete:
283 nvmet_req_complete(req, status);
284}
285
286static void nvmet_execute_io_connect(struct nvmet_req *req)
287{
288 struct nvmf_connect_command *c = &req->cmd->connect;
289 struct nvmf_connect_data *d;
290 struct nvmet_ctrl *ctrl;
291 u16 qid = le16_to_cpu(c->qid);
292 u16 status;
293
294 if (!nvmet_check_transfer_len(req, len: sizeof(struct nvmf_connect_data)))
295 return;
296
297 d = kmalloc(size: sizeof(*d), GFP_KERNEL);
298 if (!d) {
299 status = NVME_SC_INTERNAL;
300 goto complete;
301 }
302
303 status = nvmet_copy_from_sgl(req, off: 0, buf: d, len: sizeof(*d));
304 if (status)
305 goto out;
306
307 /* zero out initial completion result, assign values as needed */
308 req->cqe->result.u32 = 0;
309
310 if (c->recfmt != 0) {
311 pr_warn("invalid connect version (%d).\n",
312 le16_to_cpu(c->recfmt));
313 status = NVME_SC_CONNECT_FORMAT | NVME_SC_DNR;
314 goto out;
315 }
316
317 d->subsysnqn[NVMF_NQN_FIELD_LEN - 1] = '\0';
318 d->hostnqn[NVMF_NQN_FIELD_LEN - 1] = '\0';
319 ctrl = nvmet_ctrl_find_get(subsysnqn: d->subsysnqn, hostnqn: d->hostnqn,
320 le16_to_cpu(d->cntlid), req);
321 if (!ctrl) {
322 status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
323 goto out;
324 }
325
326 if (unlikely(qid > ctrl->subsys->max_qid)) {
327 pr_warn("invalid queue id (%d)\n", qid);
328 status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
329 req->cqe->result.u32 = IPO_IATTR_CONNECT_SQE(qid);
330 goto out_ctrl_put;
331 }
332
333 status = nvmet_install_queue(ctrl, req);
334 if (status)
335 goto out_ctrl_put;
336
337 pr_debug("adding queue %d to ctrl %d.\n", qid, ctrl->cntlid);
338 req->cqe->result.u32 = cpu_to_le32(nvmet_connect_result(ctrl));
339out:
340 kfree(objp: d);
341complete:
342 nvmet_req_complete(req, status);
343 return;
344
345out_ctrl_put:
346 nvmet_ctrl_put(ctrl);
347 goto out;
348}
349
350u16 nvmet_parse_connect_cmd(struct nvmet_req *req)
351{
352 struct nvme_command *cmd = req->cmd;
353
354 if (!nvme_is_fabrics(cmd)) {
355 pr_debug("invalid command 0x%x on unconnected queue.\n",
356 cmd->fabrics.opcode);
357 req->error_loc = offsetof(struct nvme_common_command, opcode);
358 return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
359 }
360 if (cmd->fabrics.fctype != nvme_fabrics_type_connect) {
361 pr_debug("invalid capsule type 0x%x on unconnected queue.\n",
362 cmd->fabrics.fctype);
363 req->error_loc = offsetof(struct nvmf_common_command, fctype);
364 return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
365 }
366
367 if (cmd->connect.qid == 0)
368 req->execute = nvmet_execute_admin_connect;
369 else
370 req->execute = nvmet_execute_io_connect;
371 return 0;
372}
373

source code of linux/drivers/nvme/target/fabrics-cmd.c