xref: /linux/drivers/nvme/target/fabrics-cmd.c (revision e814f3fd16acfb7f9966773953de8f740a1e3202)
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 
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_STATUS_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(req->sq->ctrl, val);
28 		break;
29 	default:
30 		req->error_loc =
31 			offsetof(struct nvmf_property_set_command, offset);
32 		status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
33 	}
34 out:
35 	nvmet_req_complete(req, status);
36 }
37 
38 static 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, 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_STATUS_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 		case NVME_REG_CRTO:
68 			val = NVME_CAP_TIMEOUT(ctrl->csts);
69 			break;
70 		default:
71 			status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
72 			break;
73 		}
74 	}
75 
76 	if (status && req->cmd->prop_get.attrib & 1) {
77 		req->error_loc =
78 			offsetof(struct nvmf_property_get_command, offset);
79 	} else {
80 		req->error_loc =
81 			offsetof(struct nvmf_property_get_command, attrib);
82 	}
83 
84 	req->cqe->result.u64 = cpu_to_le64(val);
85 	nvmet_req_complete(req, status);
86 }
87 
88 u32 nvmet_fabrics_admin_cmd_data_len(struct nvmet_req *req)
89 {
90 	struct nvme_command *cmd = req->cmd;
91 
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);
98 #endif
99 	default:
100 		return 0;
101 	}
102 }
103 
104 u16 nvmet_parse_fabrics_admin_cmd(struct nvmet_req *req)
105 {
106 	struct nvme_command *cmd = req->cmd;
107 
108 	switch (cmd->fabrics.fctype) {
109 	case nvme_fabrics_type_property_set:
110 		req->execute = nvmet_execute_prop_set;
111 		break;
112 	case nvme_fabrics_type_property_get:
113 		req->execute = nvmet_execute_prop_get;
114 		break;
115 #ifdef CONFIG_NVME_TARGET_AUTH
116 	case nvme_fabrics_type_auth_send:
117 		req->execute = nvmet_execute_auth_send;
118 		break;
119 	case nvme_fabrics_type_auth_receive:
120 		req->execute = nvmet_execute_auth_receive;
121 		break;
122 #endif
123 	default:
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;
128 	}
129 
130 	return 0;
131 }
132 
133 u32 nvmet_fabrics_io_cmd_data_len(struct nvmet_req *req)
134 {
135 	struct nvme_command *cmd = req->cmd;
136 
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);
143 #endif
144 	default:
145 		return 0;
146 	}
147 }
148 
149 u16 nvmet_parse_fabrics_io_cmd(struct nvmet_req *req)
150 {
151 	struct nvme_command *cmd = req->cmd;
152 
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;
157 		break;
158 	case nvme_fabrics_type_auth_receive:
159 		req->execute = nvmet_execute_auth_receive;
160 		break;
161 #endif
162 	default:
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;
167 	}
168 
169 	return 0;
170 }
171 
172 static u16 nvmet_install_queue(struct nvmet_ctrl *ctrl, struct nvmet_req *req)
173 {
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);
179 	u16 ret;
180 
181 	if (!sqsize) {
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;
186 		goto err;
187 	}
188 
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;
193 	}
194 
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;
202 	}
203 
204 	old = cmpxchg(&req->sq->ctrl, NULL, ctrl);
205 	if (old) {
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;
209 	}
210 
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);
214 
215 	if (c->cattr & NVME_CONNECT_DISABLE_SQFLOW) {
216 		req->sq->sqhd_disabled = true;
217 		req->cqe->sq_head = cpu_to_le16(0xffff);
218 	}
219 
220 	if (ctrl->ops->install_queue) {
221 		ret = ctrl->ops->install_queue(req->sq);
222 		if (ret) {
223 			pr_err("failed to install queue %d cntlid %d ret %x\n",
224 				qid, ctrl->cntlid, ret);
225 			ctrl->sqs[qid] = NULL;
226 			goto err;
227 		}
228 	}
229 
230 	return 0;
231 
232 err:
233 	req->sq->ctrl = NULL;
234 	return ret;
235 }
236 
237 static u32 nvmet_connect_result(struct nvmet_ctrl *ctrl)
238 {
239 	return (u32)ctrl->cntlid |
240 		(nvmet_has_auth(ctrl) ? NVME_CONNECT_AUTHREQ_ATR : 0);
241 }
242 
243 static void nvmet_execute_admin_connect(struct nvmet_req *req)
244 {
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 = {
249 		.port = req->port,
250 		.ops = req->ops,
251 		.p2p_client = req->p2p_client,
252 		.kato = le32_to_cpu(c->kato),
253 	};
254 
255 	if (!nvmet_check_transfer_len(req, sizeof(struct nvmf_connect_data)))
256 		return;
257 
258 	d = kmalloc(sizeof(*d), GFP_KERNEL);
259 	if (!d) {
260 		args.status = NVME_SC_INTERNAL;
261 		goto complete;
262 	}
263 
264 	args.status = nvmet_copy_from_sgl(req, 0, d, sizeof(*d));
265 	if (args.status)
266 		goto out;
267 
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;
273 		goto out;
274 	}
275 
276 	if (unlikely(d->cntlid != cpu_to_le16(0xffff))) {
277 		pr_warn("connect attempt for invalid controller ID %#x\n",
278 			d->cntlid);
279 		args.status = NVME_SC_CONNECT_INVALID_PARAM | NVME_STATUS_DNR;
280 		args.result = IPO_IATTR_CONNECT_DATA(cntlid);
281 		goto out;
282 	}
283 
284 	d->subsysnqn[NVMF_NQN_FIELD_LEN - 1] = '\0';
285 	d->hostnqn[NVMF_NQN_FIELD_LEN - 1] = '\0';
286 
287 	args.subsysnqn = d->subsysnqn;
288 	args.hostnqn = d->hostnqn;
289 	args.hostid = &d->hostid;
290 	args.kato = c->kato;
291 
292 	ctrl = nvmet_alloc_ctrl(&args);
293 	if (!ctrl)
294 		goto out;
295 
296 	args.status = nvmet_install_queue(ctrl, req);
297 	if (args.status) {
298 		nvmet_ctrl_put(ctrl);
299 		goto out;
300 	}
301 
302 	args.result = cpu_to_le32(nvmet_connect_result(ctrl));
303 out:
304 	kfree(d);
305 complete:
306 	req->error_loc = args.error_loc;
307 	req->cqe->result.u32 = args.result;
308 	nvmet_req_complete(req, args.status);
309 }
310 
311 static void nvmet_execute_io_connect(struct nvmet_req *req)
312 {
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);
317 	u16 status;
318 
319 	if (!nvmet_check_transfer_len(req, sizeof(struct nvmf_connect_data)))
320 		return;
321 
322 	d = kmalloc(sizeof(*d), GFP_KERNEL);
323 	if (!d) {
324 		status = NVME_SC_INTERNAL;
325 		goto complete;
326 	}
327 
328 	status = nvmet_copy_from_sgl(req, 0, d, sizeof(*d));
329 	if (status)
330 		goto out;
331 
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;
336 		goto out;
337 	}
338 
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);
343 	if (!ctrl) {
344 		status = NVME_SC_CONNECT_INVALID_PARAM | NVME_STATUS_DNR;
345 		goto out;
346 	}
347 
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);
352 		goto out_ctrl_put;
353 	}
354 
355 	status = nvmet_install_queue(ctrl, req);
356 	if (status)
357 		goto out_ctrl_put;
358 
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));
361 out:
362 	kfree(d);
363 complete:
364 	nvmet_req_complete(req, status);
365 	return;
366 
367 out_ctrl_put:
368 	nvmet_ctrl_put(ctrl);
369 	goto out;
370 }
371 
372 u32 nvmet_connect_cmd_data_len(struct nvmet_req *req)
373 {
374 	struct nvme_command *cmd = req->cmd;
375 
376 	if (!nvme_is_fabrics(cmd) ||
377 	    cmd->fabrics.fctype != nvme_fabrics_type_connect)
378 		return 0;
379 
380 	return sizeof(struct nvmf_connect_data);
381 }
382 
383 u16 nvmet_parse_connect_cmd(struct nvmet_req *req)
384 {
385 	struct nvme_command *cmd = req->cmd;
386 
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;
392 	}
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;
398 	}
399 
400 	if (cmd->connect.qid == 0)
401 		req->execute = nvmet_execute_admin_connect;
402 	else
403 		req->execute = nvmet_execute_io_connect;
404 	return 0;
405 }
406