]> git.proxmox.com Git - mirror_ubuntu-kernels.git/blob - drivers/nvme/target/fabrics-cmd.c
HID: logitech-dj: fix spelling in printk
[mirror_ubuntu-kernels.git] / drivers / nvme / target / fabrics-cmd.c
1 /*
2 * NVMe Fabrics command implementation.
3 * Copyright (c) 2015-2016 HGST, a Western Digital Company.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 */
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/blkdev.h>
16 #include "nvmet.h"
17
18 static void nvmet_execute_prop_set(struct nvmet_req *req)
19 {
20 u64 val = le64_to_cpu(req->cmd->prop_set.value);
21 u16 status = 0;
22
23 if (req->cmd->prop_set.attrib & 1) {
24 req->error_loc =
25 offsetof(struct nvmf_property_set_command, attrib);
26 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
27 goto out;
28 }
29
30 switch (le32_to_cpu(req->cmd->prop_set.offset)) {
31 case NVME_REG_CC:
32 nvmet_update_cc(req->sq->ctrl, val);
33 break;
34 default:
35 req->error_loc =
36 offsetof(struct nvmf_property_set_command, offset);
37 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
38 }
39 out:
40 nvmet_req_complete(req, status);
41 }
42
43 static void nvmet_execute_prop_get(struct nvmet_req *req)
44 {
45 struct nvmet_ctrl *ctrl = req->sq->ctrl;
46 u16 status = 0;
47 u64 val = 0;
48
49 if (req->cmd->prop_get.attrib & 1) {
50 switch (le32_to_cpu(req->cmd->prop_get.offset)) {
51 case NVME_REG_CAP:
52 val = ctrl->cap;
53 break;
54 default:
55 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
56 break;
57 }
58 } else {
59 switch (le32_to_cpu(req->cmd->prop_get.offset)) {
60 case NVME_REG_VS:
61 val = ctrl->subsys->ver;
62 break;
63 case NVME_REG_CC:
64 val = ctrl->cc;
65 break;
66 case NVME_REG_CSTS:
67 val = ctrl->csts;
68 break;
69 default:
70 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
71 break;
72 }
73 }
74
75 if (status && req->cmd->prop_get.attrib & 1) {
76 req->error_loc =
77 offsetof(struct nvmf_property_get_command, offset);
78 } else {
79 req->error_loc =
80 offsetof(struct nvmf_property_get_command, attrib);
81 }
82
83 req->rsp->result.u64 = cpu_to_le64(val);
84 nvmet_req_complete(req, status);
85 }
86
87 u16 nvmet_parse_fabrics_cmd(struct nvmet_req *req)
88 {
89 struct nvme_command *cmd = req->cmd;
90
91 switch (cmd->fabrics.fctype) {
92 case nvme_fabrics_type_property_set:
93 req->data_len = 0;
94 req->execute = nvmet_execute_prop_set;
95 break;
96 case nvme_fabrics_type_property_get:
97 req->data_len = 0;
98 req->execute = nvmet_execute_prop_get;
99 break;
100 default:
101 pr_err("received unknown capsule type 0x%x\n",
102 cmd->fabrics.fctype);
103 req->error_loc = offsetof(struct nvmf_common_command, fctype);
104 return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
105 }
106
107 return 0;
108 }
109
110 static u16 nvmet_install_queue(struct nvmet_ctrl *ctrl, struct nvmet_req *req)
111 {
112 struct nvmf_connect_command *c = &req->cmd->connect;
113 u16 qid = le16_to_cpu(c->qid);
114 u16 sqsize = le16_to_cpu(c->sqsize);
115 struct nvmet_ctrl *old;
116
117 old = cmpxchg(&req->sq->ctrl, NULL, ctrl);
118 if (old) {
119 pr_warn("queue already connected!\n");
120 req->error_loc = offsetof(struct nvmf_connect_command, opcode);
121 return NVME_SC_CONNECT_CTRL_BUSY | NVME_SC_DNR;
122 }
123 if (!sqsize) {
124 pr_warn("queue size zero!\n");
125 req->error_loc = offsetof(struct nvmf_connect_command, sqsize);
126 return NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
127 }
128
129 /* note: convert queue size from 0's-based value to 1's-based value */
130 nvmet_cq_setup(ctrl, req->cq, qid, sqsize + 1);
131 nvmet_sq_setup(ctrl, req->sq, qid, sqsize + 1);
132
133 if (c->cattr & NVME_CONNECT_DISABLE_SQFLOW) {
134 req->sq->sqhd_disabled = true;
135 req->rsp->sq_head = cpu_to_le16(0xffff);
136 }
137
138 if (ctrl->ops->install_queue) {
139 u16 ret = ctrl->ops->install_queue(req->sq);
140
141 if (ret) {
142 pr_err("failed to install queue %d cntlid %d ret %x\n",
143 qid, ret, ctrl->cntlid);
144 return ret;
145 }
146 }
147
148 return 0;
149 }
150
151 static void nvmet_execute_admin_connect(struct nvmet_req *req)
152 {
153 struct nvmf_connect_command *c = &req->cmd->connect;
154 struct nvmf_connect_data *d;
155 struct nvmet_ctrl *ctrl = NULL;
156 u16 status = 0;
157
158 d = kmalloc(sizeof(*d), GFP_KERNEL);
159 if (!d) {
160 status = NVME_SC_INTERNAL;
161 goto complete;
162 }
163
164 status = nvmet_copy_from_sgl(req, 0, d, sizeof(*d));
165 if (status)
166 goto out;
167
168 /* zero out initial completion result, assign values as needed */
169 req->rsp->result.u32 = 0;
170
171 if (c->recfmt != 0) {
172 pr_warn("invalid connect version (%d).\n",
173 le16_to_cpu(c->recfmt));
174 req->error_loc = offsetof(struct nvmf_connect_command, recfmt);
175 status = NVME_SC_CONNECT_FORMAT | NVME_SC_DNR;
176 goto out;
177 }
178
179 if (unlikely(d->cntlid != cpu_to_le16(0xffff))) {
180 pr_warn("connect attempt for invalid controller ID %#x\n",
181 d->cntlid);
182 status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
183 req->rsp->result.u32 = IPO_IATTR_CONNECT_DATA(cntlid);
184 goto out;
185 }
186
187 status = nvmet_alloc_ctrl(d->subsysnqn, d->hostnqn, req,
188 le32_to_cpu(c->kato), &ctrl);
189 if (status) {
190 if (status == (NVME_SC_INVALID_FIELD | NVME_SC_DNR))
191 req->error_loc =
192 offsetof(struct nvme_common_command, opcode);
193 goto out;
194 }
195
196 uuid_copy(&ctrl->hostid, &d->hostid);
197
198 status = nvmet_install_queue(ctrl, req);
199 if (status) {
200 nvmet_ctrl_put(ctrl);
201 goto out;
202 }
203
204 pr_info("creating controller %d for subsystem %s for NQN %s.\n",
205 ctrl->cntlid, ctrl->subsys->subsysnqn, ctrl->hostnqn);
206 req->rsp->result.u16 = cpu_to_le16(ctrl->cntlid);
207
208 out:
209 kfree(d);
210 complete:
211 nvmet_req_complete(req, status);
212 }
213
214 static void nvmet_execute_io_connect(struct nvmet_req *req)
215 {
216 struct nvmf_connect_command *c = &req->cmd->connect;
217 struct nvmf_connect_data *d;
218 struct nvmet_ctrl *ctrl = NULL;
219 u16 qid = le16_to_cpu(c->qid);
220 u16 status = 0;
221
222 d = kmalloc(sizeof(*d), GFP_KERNEL);
223 if (!d) {
224 status = NVME_SC_INTERNAL;
225 goto complete;
226 }
227
228 status = nvmet_copy_from_sgl(req, 0, d, sizeof(*d));
229 if (status)
230 goto out;
231
232 /* zero out initial completion result, assign values as needed */
233 req->rsp->result.u32 = 0;
234
235 if (c->recfmt != 0) {
236 pr_warn("invalid connect version (%d).\n",
237 le16_to_cpu(c->recfmt));
238 status = NVME_SC_CONNECT_FORMAT | NVME_SC_DNR;
239 goto out;
240 }
241
242 status = nvmet_ctrl_find_get(d->subsysnqn, d->hostnqn,
243 le16_to_cpu(d->cntlid),
244 req, &ctrl);
245 if (status)
246 goto out;
247
248 if (unlikely(qid > ctrl->subsys->max_qid)) {
249 pr_warn("invalid queue id (%d)\n", qid);
250 status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
251 req->rsp->result.u32 = IPO_IATTR_CONNECT_SQE(qid);
252 goto out_ctrl_put;
253 }
254
255 status = nvmet_install_queue(ctrl, req);
256 if (status) {
257 /* pass back cntlid that had the issue of installing queue */
258 req->rsp->result.u16 = cpu_to_le16(ctrl->cntlid);
259 goto out_ctrl_put;
260 }
261
262 pr_debug("adding queue %d to ctrl %d.\n", qid, ctrl->cntlid);
263
264 out:
265 kfree(d);
266 complete:
267 nvmet_req_complete(req, status);
268 return;
269
270 out_ctrl_put:
271 nvmet_ctrl_put(ctrl);
272 goto out;
273 }
274
275 u16 nvmet_parse_connect_cmd(struct nvmet_req *req)
276 {
277 struct nvme_command *cmd = req->cmd;
278
279 if (cmd->common.opcode != nvme_fabrics_command) {
280 pr_err("invalid command 0x%x on unconnected queue.\n",
281 cmd->fabrics.opcode);
282 req->error_loc = offsetof(struct nvme_common_command, opcode);
283 return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
284 }
285 if (cmd->fabrics.fctype != nvme_fabrics_type_connect) {
286 pr_err("invalid capsule type 0x%x on unconnected queue.\n",
287 cmd->fabrics.fctype);
288 req->error_loc = offsetof(struct nvmf_common_command, fctype);
289 return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
290 }
291
292 req->data_len = sizeof(struct nvmf_connect_data);
293 if (cmd->connect.qid == 0)
294 req->execute = nvmet_execute_admin_connect;
295 else
296 req->execute = nvmet_execute_io_connect;
297 return 0;
298 }