hc
2024-05-10 37f49e37ab4cb5d0bc4c60eb5c6d4dd57db767bb
kernel/drivers/nvme/target/fabrics-cmd.c
....@@ -1,15 +1,7 @@
1
+// SPDX-License-Identifier: GPL-2.0
12 /*
23 * NVMe Fabrics command implementation.
34 * 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.
135 */
146 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
157 #include <linux/blkdev.h>
....@@ -17,23 +9,29 @@
179
1810 static void nvmet_execute_prop_set(struct nvmet_req *req)
1911 {
12
+ u64 val = le64_to_cpu(req->cmd->prop_set.value);
2013 u16 status = 0;
2114
22
- if (!(req->cmd->prop_set.attrib & 1)) {
23
- u64 val = le64_to_cpu(req->cmd->prop_set.value);
15
+ if (!nvmet_check_transfer_len(req, 0))
16
+ return;
2417
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
- status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
31
- break;
32
- }
33
- } else {
18
+ if (req->cmd->prop_set.attrib & 1) {
19
+ req->error_loc =
20
+ offsetof(struct nvmf_property_set_command, attrib);
3421 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
22
+ goto out;
3523 }
3624
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_SC_DNR;
33
+ }
34
+out:
3735 nvmet_req_complete(req, status);
3836 }
3937
....@@ -42,6 +40,9 @@
4240 struct nvmet_ctrl *ctrl = req->sq->ctrl;
4341 u16 status = 0;
4442 u64 val = 0;
43
+
44
+ if (!nvmet_check_transfer_len(req, 0))
45
+ return;
4546
4647 if (req->cmd->prop_get.attrib & 1) {
4748 switch (le32_to_cpu(req->cmd->prop_get.offset)) {
....@@ -69,7 +70,15 @@
6970 }
7071 }
7172
72
- req->rsp->result.u64 = cpu_to_le64(val);
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);
7382 nvmet_req_complete(req, status);
7483 }
7584
....@@ -79,16 +88,15 @@
7988
8089 switch (cmd->fabrics.fctype) {
8190 case nvme_fabrics_type_property_set:
82
- req->data_len = 0;
8391 req->execute = nvmet_execute_prop_set;
8492 break;
8593 case nvme_fabrics_type_property_get:
86
- req->data_len = 0;
8794 req->execute = nvmet_execute_prop_get;
8895 break;
8996 default:
9097 pr_err("received unknown capsule type 0x%x\n",
9198 cmd->fabrics.fctype);
99
+ req->error_loc = offsetof(struct nvmf_common_command, fctype);
92100 return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
93101 }
94102
....@@ -101,21 +109,45 @@
101109 u16 qid = le16_to_cpu(c->qid);
102110 u16 sqsize = le16_to_cpu(c->sqsize);
103111 struct nvmet_ctrl *old;
112
+ u16 ret;
104113
105114 old = cmpxchg(&req->sq->ctrl, NULL, ctrl);
106115 if (old) {
107116 pr_warn("queue already connected!\n");
117
+ req->error_loc = offsetof(struct nvmf_connect_command, opcode);
108118 return NVME_SC_CONNECT_CTRL_BUSY | NVME_SC_DNR;
109119 }
110120 if (!sqsize) {
111121 pr_warn("queue size zero!\n");
112
- return NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
122
+ req->error_loc = offsetof(struct nvmf_connect_command, sqsize);
123
+ req->cqe->result.u32 = IPO_IATTR_CONNECT_SQE(sqsize);
124
+ ret = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
125
+ goto err;
113126 }
114127
115128 /* note: convert queue size from 0's-based value to 1's-based value */
116129 nvmet_cq_setup(ctrl, req->cq, qid, sqsize + 1);
117130 nvmet_sq_setup(ctrl, req->sq, qid, sqsize + 1);
131
+
132
+ if (c->cattr & NVME_CONNECT_DISABLE_SQFLOW) {
133
+ req->sq->sqhd_disabled = true;
134
+ req->cqe->sq_head = cpu_to_le16(0xffff);
135
+ }
136
+
137
+ if (ctrl->ops->install_queue) {
138
+ ret = ctrl->ops->install_queue(req->sq);
139
+ if (ret) {
140
+ pr_err("failed to install queue %d cntlid %d ret %x\n",
141
+ qid, ctrl->cntlid, ret);
142
+ goto err;
143
+ }
144
+ }
145
+
118146 return 0;
147
+
148
+err:
149
+ req->sq->ctrl = NULL;
150
+ return ret;
119151 }
120152
121153 static void nvmet_execute_admin_connect(struct nvmet_req *req)
....@@ -124,6 +156,9 @@
124156 struct nvmf_connect_data *d;
125157 struct nvmet_ctrl *ctrl = NULL;
126158 u16 status = 0;
159
+
160
+ if (!nvmet_check_transfer_len(req, sizeof(struct nvmf_connect_data)))
161
+ return;
127162
128163 d = kmalloc(sizeof(*d), GFP_KERNEL);
129164 if (!d) {
....@@ -136,11 +171,12 @@
136171 goto out;
137172
138173 /* zero out initial completion result, assign values as needed */
139
- req->rsp->result.u32 = 0;
174
+ req->cqe->result.u32 = 0;
140175
141176 if (c->recfmt != 0) {
142177 pr_warn("invalid connect version (%d).\n",
143178 le16_to_cpu(c->recfmt));
179
+ req->error_loc = offsetof(struct nvmf_connect_command, recfmt);
144180 status = NVME_SC_CONNECT_FORMAT | NVME_SC_DNR;
145181 goto out;
146182 }
....@@ -149,14 +185,21 @@
149185 pr_warn("connect attempt for invalid controller ID %#x\n",
150186 d->cntlid);
151187 status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
152
- req->rsp->result.u32 = IPO_IATTR_CONNECT_DATA(cntlid);
188
+ req->cqe->result.u32 = IPO_IATTR_CONNECT_DATA(cntlid);
153189 goto out;
154190 }
155191
156192 status = nvmet_alloc_ctrl(d->subsysnqn, d->hostnqn, req,
157193 le32_to_cpu(c->kato), &ctrl);
158
- if (status)
194
+ if (status) {
195
+ if (status == (NVME_SC_INVALID_FIELD | NVME_SC_DNR))
196
+ req->error_loc =
197
+ offsetof(struct nvme_common_command, opcode);
159198 goto out;
199
+ }
200
+
201
+ ctrl->pi_support = ctrl->port->pi_enable && ctrl->subsys->pi_support;
202
+
160203 uuid_copy(&ctrl->hostid, &d->hostid);
161204
162205 status = nvmet_install_queue(ctrl, req);
....@@ -165,9 +208,10 @@
165208 goto out;
166209 }
167210
168
- pr_info("creating controller %d for subsystem %s for NQN %s.\n",
169
- ctrl->cntlid, ctrl->subsys->subsysnqn, ctrl->hostnqn);
170
- req->rsp->result.u16 = cpu_to_le16(ctrl->cntlid);
211
+ pr_info("creating controller %d for subsystem %s for NQN %s%s.\n",
212
+ ctrl->cntlid, ctrl->subsys->subsysnqn, ctrl->hostnqn,
213
+ ctrl->pi_support ? " T10-PI is enabled" : "");
214
+ req->cqe->result.u16 = cpu_to_le16(ctrl->cntlid);
171215
172216 out:
173217 kfree(d);
....@@ -183,6 +227,9 @@
183227 u16 qid = le16_to_cpu(c->qid);
184228 u16 status = 0;
185229
230
+ if (!nvmet_check_transfer_len(req, sizeof(struct nvmf_connect_data)))
231
+ return;
232
+
186233 d = kmalloc(sizeof(*d), GFP_KERNEL);
187234 if (!d) {
188235 status = NVME_SC_INTERNAL;
....@@ -194,7 +241,7 @@
194241 goto out;
195242
196243 /* zero out initial completion result, assign values as needed */
197
- req->rsp->result.u32 = 0;
244
+ req->cqe->result.u32 = 0;
198245
199246 if (c->recfmt != 0) {
200247 pr_warn("invalid connect version (%d).\n",
....@@ -212,16 +259,16 @@
212259 if (unlikely(qid > ctrl->subsys->max_qid)) {
213260 pr_warn("invalid queue id (%d)\n", qid);
214261 status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
215
- req->rsp->result.u32 = IPO_IATTR_CONNECT_SQE(qid);
262
+ req->cqe->result.u32 = IPO_IATTR_CONNECT_SQE(qid);
216263 goto out_ctrl_put;
217264 }
218265
219266 status = nvmet_install_queue(ctrl, req);
220
- if (status) {
221
- /* pass back cntlid that had the issue of installing queue */
222
- req->rsp->result.u16 = cpu_to_le16(ctrl->cntlid);
267
+ if (status)
223268 goto out_ctrl_put;
224
- }
269
+
270
+ /* pass back cntlid for successful completion */
271
+ req->cqe->result.u16 = cpu_to_le16(ctrl->cntlid);
225272
226273 pr_debug("adding queue %d to ctrl %d.\n", qid, ctrl->cntlid);
227274
....@@ -240,18 +287,19 @@
240287 {
241288 struct nvme_command *cmd = req->cmd;
242289
243
- if (cmd->common.opcode != nvme_fabrics_command) {
290
+ if (!nvme_is_fabrics(cmd)) {
244291 pr_err("invalid command 0x%x on unconnected queue.\n",
245292 cmd->fabrics.opcode);
293
+ req->error_loc = offsetof(struct nvme_common_command, opcode);
246294 return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
247295 }
248296 if (cmd->fabrics.fctype != nvme_fabrics_type_connect) {
249297 pr_err("invalid capsule type 0x%x on unconnected queue.\n",
250298 cmd->fabrics.fctype);
299
+ req->error_loc = offsetof(struct nvmf_common_command, fctype);
251300 return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
252301 }
253302
254
- req->data_len = sizeof(struct nvmf_connect_data);
255303 if (cmd->connect.qid == 0)
256304 req->execute = nvmet_execute_admin_connect;
257305 else