]> Git Repo - J-linux.git/blob - drivers/nvme/target/fabrics-cmd.c
Merge tag 'vfs-6.13-rc7.fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs
[J-linux.git] / drivers / nvme / target / fabrics-cmd.c
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 u16 nvmet_parse_fabrics_admin_cmd(struct nvmet_req *req)
89 {
90         struct nvme_command *cmd = req->cmd;
91
92         switch (cmd->fabrics.fctype) {
93         case nvme_fabrics_type_property_set:
94                 req->execute = nvmet_execute_prop_set;
95                 break;
96         case nvme_fabrics_type_property_get:
97                 req->execute = nvmet_execute_prop_get;
98                 break;
99 #ifdef CONFIG_NVME_TARGET_AUTH
100         case nvme_fabrics_type_auth_send:
101                 req->execute = nvmet_execute_auth_send;
102                 break;
103         case nvme_fabrics_type_auth_receive:
104                 req->execute = nvmet_execute_auth_receive;
105                 break;
106 #endif
107         default:
108                 pr_debug("received unknown capsule type 0x%x\n",
109                         cmd->fabrics.fctype);
110                 req->error_loc = offsetof(struct nvmf_common_command, fctype);
111                 return NVME_SC_INVALID_OPCODE | NVME_STATUS_DNR;
112         }
113
114         return 0;
115 }
116
117 u16 nvmet_parse_fabrics_io_cmd(struct nvmet_req *req)
118 {
119         struct nvme_command *cmd = req->cmd;
120
121         switch (cmd->fabrics.fctype) {
122 #ifdef CONFIG_NVME_TARGET_AUTH
123         case nvme_fabrics_type_auth_send:
124                 req->execute = nvmet_execute_auth_send;
125                 break;
126         case nvme_fabrics_type_auth_receive:
127                 req->execute = nvmet_execute_auth_receive;
128                 break;
129 #endif
130         default:
131                 pr_debug("received unknown capsule type 0x%x\n",
132                         cmd->fabrics.fctype);
133                 req->error_loc = offsetof(struct nvmf_common_command, fctype);
134                 return NVME_SC_INVALID_OPCODE | NVME_STATUS_DNR;
135         }
136
137         return 0;
138 }
139
140 static u16 nvmet_install_queue(struct nvmet_ctrl *ctrl, struct nvmet_req *req)
141 {
142         struct nvmf_connect_command *c = &req->cmd->connect;
143         u16 qid = le16_to_cpu(c->qid);
144         u16 sqsize = le16_to_cpu(c->sqsize);
145         struct nvmet_ctrl *old;
146         u16 mqes = NVME_CAP_MQES(ctrl->cap);
147         u16 ret;
148
149         if (!sqsize) {
150                 pr_warn("queue size zero!\n");
151                 req->error_loc = offsetof(struct nvmf_connect_command, sqsize);
152                 req->cqe->result.u32 = IPO_IATTR_CONNECT_SQE(sqsize);
153                 ret = NVME_SC_CONNECT_INVALID_PARAM | NVME_STATUS_DNR;
154                 goto err;
155         }
156
157         if (ctrl->sqs[qid] != NULL) {
158                 pr_warn("qid %u has already been created\n", qid);
159                 req->error_loc = offsetof(struct nvmf_connect_command, qid);
160                 return NVME_SC_CMD_SEQ_ERROR | NVME_STATUS_DNR;
161         }
162
163         /* for fabrics, this value applies to only the I/O Submission Queues */
164         if (qid && sqsize > mqes) {
165                 pr_warn("sqsize %u is larger than MQES supported %u cntlid %d\n",
166                                 sqsize, mqes, ctrl->cntlid);
167                 req->error_loc = offsetof(struct nvmf_connect_command, sqsize);
168                 req->cqe->result.u32 = IPO_IATTR_CONNECT_SQE(sqsize);
169                 return NVME_SC_CONNECT_INVALID_PARAM | NVME_STATUS_DNR;
170         }
171
172         old = cmpxchg(&req->sq->ctrl, NULL, ctrl);
173         if (old) {
174                 pr_warn("queue already connected!\n");
175                 req->error_loc = offsetof(struct nvmf_connect_command, opcode);
176                 return NVME_SC_CONNECT_CTRL_BUSY | NVME_STATUS_DNR;
177         }
178
179         /* note: convert queue size from 0's-based value to 1's-based value */
180         nvmet_cq_setup(ctrl, req->cq, qid, sqsize + 1);
181         nvmet_sq_setup(ctrl, req->sq, qid, sqsize + 1);
182
183         if (c->cattr & NVME_CONNECT_DISABLE_SQFLOW) {
184                 req->sq->sqhd_disabled = true;
185                 req->cqe->sq_head = cpu_to_le16(0xffff);
186         }
187
188         if (ctrl->ops->install_queue) {
189                 ret = ctrl->ops->install_queue(req->sq);
190                 if (ret) {
191                         pr_err("failed to install queue %d cntlid %d ret %x\n",
192                                 qid, ctrl->cntlid, ret);
193                         ctrl->sqs[qid] = NULL;
194                         goto err;
195                 }
196         }
197
198         return 0;
199
200 err:
201         req->sq->ctrl = NULL;
202         return ret;
203 }
204
205 static u32 nvmet_connect_result(struct nvmet_ctrl *ctrl)
206 {
207         return (u32)ctrl->cntlid |
208                 (nvmet_has_auth(ctrl) ? NVME_CONNECT_AUTHREQ_ATR : 0);
209 }
210
211 static void nvmet_execute_admin_connect(struct nvmet_req *req)
212 {
213         struct nvmf_connect_command *c = &req->cmd->connect;
214         struct nvmf_connect_data *d;
215         struct nvmet_ctrl *ctrl = NULL;
216         u16 status;
217         u8 dhchap_status;
218
219         if (!nvmet_check_transfer_len(req, sizeof(struct nvmf_connect_data)))
220                 return;
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         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_STATUS_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_STATUS_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(d->subsysnqn, d->hostnqn, req,
251                                   le32_to_cpu(c->kato), &ctrl, &d->hostid);
252         if (status)
253                 goto out;
254
255         dhchap_status = nvmet_setup_auth(ctrl);
256         if (dhchap_status) {
257                 pr_err("Failed to setup authentication, dhchap status %u\n",
258                        dhchap_status);
259                 nvmet_ctrl_put(ctrl);
260                 if (dhchap_status == NVME_AUTH_DHCHAP_FAILURE_FAILED)
261                         status = (NVME_SC_CONNECT_INVALID_HOST | NVME_STATUS_DNR);
262                 else
263                         status = NVME_SC_INTERNAL;
264                 goto out;
265         }
266
267         status = nvmet_install_queue(ctrl, req);
268         if (status) {
269                 nvmet_ctrl_put(ctrl);
270                 goto out;
271         }
272
273         pr_info("creating %s controller %d for subsystem %s for NQN %s%s%s.\n",
274                 nvmet_is_disc_subsys(ctrl->subsys) ? "discovery" : "nvm",
275                 ctrl->cntlid, ctrl->subsys->subsysnqn, ctrl->hostnqn,
276                 ctrl->pi_support ? " T10-PI is enabled" : "",
277                 nvmet_has_auth(ctrl) ? " with DH-HMAC-CHAP" : "");
278         req->cqe->result.u32 = cpu_to_le32(nvmet_connect_result(ctrl));
279 out:
280         kfree(d);
281 complete:
282         nvmet_req_complete(req, status);
283 }
284
285 static void nvmet_execute_io_connect(struct nvmet_req *req)
286 {
287         struct nvmf_connect_command *c = &req->cmd->connect;
288         struct nvmf_connect_data *d;
289         struct nvmet_ctrl *ctrl;
290         u16 qid = le16_to_cpu(c->qid);
291         u16 status;
292
293         if (!nvmet_check_transfer_len(req, sizeof(struct nvmf_connect_data)))
294                 return;
295
296         d = kmalloc(sizeof(*d), GFP_KERNEL);
297         if (!d) {
298                 status = NVME_SC_INTERNAL;
299                 goto complete;
300         }
301
302         status = nvmet_copy_from_sgl(req, 0, d, sizeof(*d));
303         if (status)
304                 goto out;
305
306         if (c->recfmt != 0) {
307                 pr_warn("invalid connect version (%d).\n",
308                         le16_to_cpu(c->recfmt));
309                 status = NVME_SC_CONNECT_FORMAT | NVME_STATUS_DNR;
310                 goto out;
311         }
312
313         d->subsysnqn[NVMF_NQN_FIELD_LEN - 1] = '\0';
314         d->hostnqn[NVMF_NQN_FIELD_LEN - 1] = '\0';
315         ctrl = nvmet_ctrl_find_get(d->subsysnqn, d->hostnqn,
316                                    le16_to_cpu(d->cntlid), req);
317         if (!ctrl) {
318                 status = NVME_SC_CONNECT_INVALID_PARAM | NVME_STATUS_DNR;
319                 goto out;
320         }
321
322         if (unlikely(qid > ctrl->subsys->max_qid)) {
323                 pr_warn("invalid queue id (%d)\n", qid);
324                 status = NVME_SC_CONNECT_INVALID_PARAM | NVME_STATUS_DNR;
325                 req->cqe->result.u32 = IPO_IATTR_CONNECT_SQE(qid);
326                 goto out_ctrl_put;
327         }
328
329         status = nvmet_install_queue(ctrl, req);
330         if (status)
331                 goto out_ctrl_put;
332
333         pr_debug("adding queue %d to ctrl %d.\n", qid, ctrl->cntlid);
334         req->cqe->result.u32 = cpu_to_le32(nvmet_connect_result(ctrl));
335 out:
336         kfree(d);
337 complete:
338         nvmet_req_complete(req, status);
339         return;
340
341 out_ctrl_put:
342         nvmet_ctrl_put(ctrl);
343         goto out;
344 }
345
346 u16 nvmet_parse_connect_cmd(struct nvmet_req *req)
347 {
348         struct nvme_command *cmd = req->cmd;
349
350         if (!nvme_is_fabrics(cmd)) {
351                 pr_debug("invalid command 0x%x on unconnected queue.\n",
352                         cmd->fabrics.opcode);
353                 req->error_loc = offsetof(struct nvme_common_command, opcode);
354                 return NVME_SC_INVALID_OPCODE | NVME_STATUS_DNR;
355         }
356         if (cmd->fabrics.fctype != nvme_fabrics_type_connect) {
357                 pr_debug("invalid capsule type 0x%x on unconnected queue.\n",
358                         cmd->fabrics.fctype);
359                 req->error_loc = offsetof(struct nvmf_common_command, fctype);
360                 return NVME_SC_INVALID_OPCODE | NVME_STATUS_DNR;
361         }
362
363         if (cmd->connect.qid == 0)
364                 req->execute = nvmet_execute_admin_connect;
365         else
366                 req->execute = nvmet_execute_io_connect;
367         return 0;
368 }
This page took 0.04608 seconds and 4 git commands to generate.