]>
Commit | Line | Data |
---|---|---|
e85b24b5 OG |
1 | /* |
2 | * Copyright (c) 2004, 2005, 2006 Voltaire, Inc. All rights reserved. | |
3ee07d27 | 3 | * Copyright (c) 2013-2014 Mellanox Technologies. All rights reserved. |
e85b24b5 OG |
4 | * |
5 | * This software is available to you under a choice of one of two | |
6 | * licenses. You may choose to be licensed under the terms of the GNU | |
7 | * General Public License (GPL) Version 2, available from the file | |
8 | * COPYING in the main directory of this source tree, or the | |
9 | * OpenIB.org BSD license below: | |
10 | * | |
11 | * Redistribution and use in source and binary forms, with or | |
12 | * without modification, are permitted provided that the following | |
13 | * conditions are met: | |
14 | * | |
15 | * - Redistributions of source code must retain the above | |
16 | * copyright notice, this list of conditions and the following | |
17 | * disclaimer. | |
18 | * | |
19 | * - Redistributions in binary form must reproduce the above | |
20 | * copyright notice, this list of conditions and the following | |
21 | * disclaimer in the documentation and/or other materials | |
22 | * provided with the distribution. | |
23 | * | |
24 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
25 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
26 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
27 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | |
28 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | |
29 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
30 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
31 | * SOFTWARE. | |
e85b24b5 OG |
32 | */ |
33 | #include <linux/kernel.h> | |
34 | #include <linux/slab.h> | |
35 | #include <linux/mm.h> | |
e85b24b5 OG |
36 | #include <linux/scatterlist.h> |
37 | #include <linux/kfifo.h> | |
38 | #include <scsi/scsi_cmnd.h> | |
39 | #include <scsi/scsi_host.h> | |
40 | ||
41 | #include "iscsi_iser.h" | |
42 | ||
e85b24b5 | 43 | /* Register user buffer memory and initialize passive rdma |
d77e6535 SG |
44 | * dto descriptor. Data size is stored in |
45 | * task->data[ISER_DIR_IN].data_len, Protection size | |
46 | * os stored in task->prot[ISER_DIR_IN].data_len | |
e85b24b5 | 47 | */ |
d77e6535 | 48 | static int iser_prepare_read_cmd(struct iscsi_task *task) |
e85b24b5 OG |
49 | |
50 | { | |
2261ec3d | 51 | struct iscsi_iser_task *iser_task = task->dd_data; |
b130eded | 52 | struct iser_mem_reg *mem_reg; |
e85b24b5 | 53 | int err; |
d3cf81f9 | 54 | struct iser_ctrl *hdr = &iser_task->desc.iser_header; |
2261ec3d | 55 | struct iser_data_buf *buf_in = &iser_task->data[ISER_DIR_IN]; |
e85b24b5 | 56 | |
2261ec3d | 57 | err = iser_dma_map_task_data(iser_task, |
e85b24b5 OG |
58 | buf_in, |
59 | ISER_DIR_IN, | |
60 | DMA_FROM_DEVICE); | |
61 | if (err) | |
62 | return err; | |
63 | ||
177e31bd SG |
64 | if (scsi_prot_sg_count(iser_task->sc)) { |
65 | struct iser_data_buf *pbuf_in = &iser_task->prot[ISER_DIR_IN]; | |
66 | ||
67 | err = iser_dma_map_task_data(iser_task, | |
68 | pbuf_in, | |
69 | ISER_DIR_IN, | |
70 | DMA_FROM_DEVICE); | |
71 | if (err) | |
72 | return err; | |
73 | } | |
74 | ||
b5f04b00 | 75 | err = iser_reg_rdma_mem(iser_task, ISER_DIR_IN, false); |
e85b24b5 OG |
76 | if (err) { |
77 | iser_err("Failed to set up Data-IN RDMA\n"); | |
78 | return err; | |
79 | } | |
b130eded | 80 | mem_reg = &iser_task->rdma_reg[ISER_DIR_IN]; |
e85b24b5 OG |
81 | |
82 | hdr->flags |= ISER_RSV; | |
b130eded | 83 | hdr->read_stag = cpu_to_be32(mem_reg->rkey); |
90a6684c | 84 | hdr->read_va = cpu_to_be64(mem_reg->sge.addr); |
e85b24b5 OG |
85 | |
86 | iser_dbg("Cmd itt:%d READ tags RKEY:%#.4X VA:%#llX\n", | |
b130eded | 87 | task->itt, mem_reg->rkey, |
90a6684c | 88 | (unsigned long long)mem_reg->sge.addr); |
e85b24b5 OG |
89 | |
90 | return 0; | |
91 | } | |
92 | ||
93 | /* Register user buffer memory and initialize passive rdma | |
d77e6535 SG |
94 | * dto descriptor. Data size is stored in |
95 | * task->data[ISER_DIR_OUT].data_len, Protection size | |
96 | * is stored at task->prot[ISER_DIR_OUT].data_len | |
e85b24b5 OG |
97 | */ |
98 | static int | |
2261ec3d | 99 | iser_prepare_write_cmd(struct iscsi_task *task, |
e85b24b5 OG |
100 | unsigned int imm_sz, |
101 | unsigned int unsol_sz, | |
102 | unsigned int edtl) | |
103 | { | |
2261ec3d | 104 | struct iscsi_iser_task *iser_task = task->dd_data; |
b130eded | 105 | struct iser_mem_reg *mem_reg; |
e85b24b5 | 106 | int err; |
d3cf81f9 | 107 | struct iser_ctrl *hdr = &iser_task->desc.iser_header; |
2261ec3d | 108 | struct iser_data_buf *buf_out = &iser_task->data[ISER_DIR_OUT]; |
f19624aa | 109 | struct ib_sge *tx_dsg = &iser_task->desc.tx_sg[1]; |
e85b24b5 | 110 | |
2261ec3d | 111 | err = iser_dma_map_task_data(iser_task, |
e85b24b5 OG |
112 | buf_out, |
113 | ISER_DIR_OUT, | |
114 | DMA_TO_DEVICE); | |
115 | if (err) | |
116 | return err; | |
117 | ||
177e31bd SG |
118 | if (scsi_prot_sg_count(iser_task->sc)) { |
119 | struct iser_data_buf *pbuf_out = &iser_task->prot[ISER_DIR_OUT]; | |
120 | ||
121 | err = iser_dma_map_task_data(iser_task, | |
122 | pbuf_out, | |
123 | ISER_DIR_OUT, | |
124 | DMA_TO_DEVICE); | |
125 | if (err) | |
126 | return err; | |
127 | } | |
128 | ||
b5f04b00 JD |
129 | err = iser_reg_rdma_mem(iser_task, ISER_DIR_OUT, |
130 | buf_out->data_len == imm_sz); | |
e85b24b5 OG |
131 | if (err != 0) { |
132 | iser_err("Failed to register write cmd RDMA mem\n"); | |
133 | return err; | |
134 | } | |
135 | ||
b130eded | 136 | mem_reg = &iser_task->rdma_reg[ISER_DIR_OUT]; |
e85b24b5 OG |
137 | |
138 | if (unsol_sz < edtl) { | |
139 | hdr->flags |= ISER_WSV; | |
e6e52aec SG |
140 | if (buf_out->data_len > imm_sz) { |
141 | hdr->write_stag = cpu_to_be32(mem_reg->rkey); | |
142 | hdr->write_va = cpu_to_be64(mem_reg->sge.addr + unsol_sz); | |
143 | } | |
e85b24b5 OG |
144 | |
145 | iser_dbg("Cmd itt:%d, WRITE tags, RKEY:%#.4X " | |
146 | "VA:%#llX + unsol:%d\n", | |
b130eded | 147 | task->itt, mem_reg->rkey, |
90a6684c | 148 | (unsigned long long)mem_reg->sge.addr, unsol_sz); |
e85b24b5 OG |
149 | } |
150 | ||
151 | if (imm_sz > 0) { | |
152 | iser_dbg("Cmd itt:%d, WRITE, adding imm.data sz: %d\n", | |
2261ec3d | 153 | task->itt, imm_sz); |
90a6684c | 154 | tx_dsg->addr = mem_reg->sge.addr; |
f19624aa | 155 | tx_dsg->length = imm_sz; |
90a6684c | 156 | tx_dsg->lkey = mem_reg->sge.lkey; |
f19624aa | 157 | iser_task->desc.num_sge = 2; |
e85b24b5 OG |
158 | } |
159 | ||
160 | return 0; | |
161 | } | |
162 | ||
e85b24b5 | 163 | /* creates a new tx descriptor and adds header regd buffer */ |
5716af6e | 164 | static void iser_create_send_desc(struct iser_conn *iser_conn, |
f19624aa | 165 | struct iser_tx_desc *tx_desc) |
e85b24b5 | 166 | { |
a4ee3539 | 167 | struct iser_device *device = iser_conn->ib_conn.device; |
e85b24b5 | 168 | |
f19624aa OG |
169 | ib_dma_sync_single_for_cpu(device->ib_device, |
170 | tx_desc->dma_addr, ISER_HEADERS_LEN, DMA_TO_DEVICE); | |
e85b24b5 | 171 | |
d3cf81f9 | 172 | memset(&tx_desc->iser_header, 0, sizeof(struct iser_ctrl)); |
e85b24b5 | 173 | tx_desc->iser_header.flags = ISER_VER; |
f19624aa | 174 | tx_desc->num_sge = 1; |
e85b24b5 OG |
175 | } |
176 | ||
5716af6e | 177 | static void iser_free_login_buf(struct iser_conn *iser_conn) |
986db0d6 | 178 | { |
a4ee3539 | 179 | struct iser_device *device = iser_conn->ib_conn.device; |
0f512b34 | 180 | struct iser_login_desc *desc = &iser_conn->login_desc; |
a4ee3539 | 181 | |
0f512b34 | 182 | if (!desc->req) |
986db0d6 SP |
183 | return; |
184 | ||
0f512b34 SG |
185 | ib_dma_unmap_single(device->ib_device, desc->req_dma, |
186 | ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_TO_DEVICE); | |
986db0d6 | 187 | |
0f512b34 SG |
188 | ib_dma_unmap_single(device->ib_device, desc->rsp_dma, |
189 | ISER_RX_LOGIN_SIZE, DMA_FROM_DEVICE); | |
986db0d6 | 190 | |
0f512b34 SG |
191 | kfree(desc->req); |
192 | kfree(desc->rsp); | |
986db0d6 SP |
193 | |
194 | /* make sure we never redo any unmapping */ | |
0f512b34 SG |
195 | desc->req = NULL; |
196 | desc->rsp = NULL; | |
986db0d6 SP |
197 | } |
198 | ||
5716af6e | 199 | static int iser_alloc_login_buf(struct iser_conn *iser_conn) |
986db0d6 | 200 | { |
a4ee3539 | 201 | struct iser_device *device = iser_conn->ib_conn.device; |
0f512b34 SG |
202 | struct iser_login_desc *desc = &iser_conn->login_desc; |
203 | ||
204 | desc->req = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL); | |
205 | if (!desc->req) | |
206 | return -ENOMEM; | |
207 | ||
208 | desc->req_dma = ib_dma_map_single(device->ib_device, desc->req, | |
209 | ISCSI_DEF_MAX_RECV_SEG_LEN, | |
210 | DMA_TO_DEVICE); | |
211 | if (ib_dma_mapping_error(device->ib_device, | |
212 | desc->req_dma)) | |
213 | goto free_req; | |
214 | ||
215 | desc->rsp = kmalloc(ISER_RX_LOGIN_SIZE, GFP_KERNEL); | |
216 | if (!desc->rsp) | |
217 | goto unmap_req; | |
218 | ||
219 | desc->rsp_dma = ib_dma_map_single(device->ib_device, desc->rsp, | |
220 | ISER_RX_LOGIN_SIZE, | |
221 | DMA_FROM_DEVICE); | |
222 | if (ib_dma_mapping_error(device->ib_device, | |
223 | desc->rsp_dma)) | |
224 | goto free_rsp; | |
225 | ||
986db0d6 SP |
226 | return 0; |
227 | ||
0f512b34 SG |
228 | free_rsp: |
229 | kfree(desc->rsp); | |
230 | unmap_req: | |
231 | ib_dma_unmap_single(device->ib_device, desc->req_dma, | |
232 | ISCSI_DEF_MAX_RECV_SEG_LEN, | |
233 | DMA_TO_DEVICE); | |
234 | free_req: | |
235 | kfree(desc->req); | |
986db0d6 | 236 | |
986db0d6 SP |
237 | return -ENOMEM; |
238 | } | |
f19624aa | 239 | |
5716af6e SG |
240 | int iser_alloc_rx_descriptors(struct iser_conn *iser_conn, |
241 | struct iscsi_session *session) | |
bcc60c38 OG |
242 | { |
243 | int i, j; | |
244 | u64 dma_addr; | |
245 | struct iser_rx_desc *rx_desc; | |
246 | struct ib_sge *rx_sg; | |
a4ee3539 SG |
247 | struct ib_conn *ib_conn = &iser_conn->ib_conn; |
248 | struct iser_device *device = ib_conn->device; | |
bcc60c38 | 249 | |
5716af6e SG |
250 | iser_conn->qp_max_recv_dtos = session->cmds_max; |
251 | iser_conn->qp_max_recv_dtos_mask = session->cmds_max - 1; /* cmds_max is 2^N */ | |
252 | iser_conn->min_posted_rx = iser_conn->qp_max_recv_dtos >> 2; | |
b7f04513 | 253 | |
f8db651d | 254 | if (device->reg_ops->alloc_reg_res(ib_conn, session->scsi_cmds_max, |
df749cdc | 255 | iser_conn->scsi_sg_tablesize)) |
b4e155ff | 256 | goto create_rdma_reg_res_failed; |
986db0d6 | 257 | |
5716af6e | 258 | if (iser_alloc_login_buf(iser_conn)) |
986db0d6 SP |
259 | goto alloc_login_buf_fail; |
260 | ||
6aabfa76 SG |
261 | iser_conn->num_rx_descs = session->cmds_max; |
262 | iser_conn->rx_descs = kmalloc(iser_conn->num_rx_descs * | |
bcc60c38 | 263 | sizeof(struct iser_rx_desc), GFP_KERNEL); |
5716af6e | 264 | if (!iser_conn->rx_descs) |
bcc60c38 OG |
265 | goto rx_desc_alloc_fail; |
266 | ||
5716af6e | 267 | rx_desc = iser_conn->rx_descs; |
bcc60c38 | 268 | |
5716af6e | 269 | for (i = 0; i < iser_conn->qp_max_recv_dtos; i++, rx_desc++) { |
bcc60c38 OG |
270 | dma_addr = ib_dma_map_single(device->ib_device, (void *)rx_desc, |
271 | ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE); | |
272 | if (ib_dma_mapping_error(device->ib_device, dma_addr)) | |
273 | goto rx_desc_dma_map_failed; | |
274 | ||
275 | rx_desc->dma_addr = dma_addr; | |
cfeb91b3 | 276 | rx_desc->cqe.done = iser_task_rsp; |
bcc60c38 | 277 | rx_sg = &rx_desc->rx_sg; |
cfeb91b3 | 278 | rx_sg->addr = rx_desc->dma_addr; |
bcc60c38 | 279 | rx_sg->length = ISER_RX_PAYLOAD_SIZE; |
cfeb91b3 | 280 | rx_sg->lkey = device->pd->local_dma_lkey; |
bcc60c38 OG |
281 | } |
282 | ||
5716af6e | 283 | iser_conn->rx_desc_head = 0; |
bcc60c38 OG |
284 | return 0; |
285 | ||
286 | rx_desc_dma_map_failed: | |
5716af6e | 287 | rx_desc = iser_conn->rx_descs; |
bcc60c38 OG |
288 | for (j = 0; j < i; j++, rx_desc++) |
289 | ib_dma_unmap_single(device->ib_device, rx_desc->dma_addr, | |
986db0d6 | 290 | ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE); |
5716af6e SG |
291 | kfree(iser_conn->rx_descs); |
292 | iser_conn->rx_descs = NULL; | |
bcc60c38 | 293 | rx_desc_alloc_fail: |
5716af6e | 294 | iser_free_login_buf(iser_conn); |
986db0d6 | 295 | alloc_login_buf_fail: |
48afbff6 | 296 | device->reg_ops->free_reg_res(ib_conn); |
b4e155ff | 297 | create_rdma_reg_res_failed: |
bcc60c38 OG |
298 | iser_err("failed allocating rx descriptors / data buffers\n"); |
299 | return -ENOMEM; | |
300 | } | |
301 | ||
5716af6e | 302 | void iser_free_rx_descriptors(struct iser_conn *iser_conn) |
bcc60c38 OG |
303 | { |
304 | int i; | |
305 | struct iser_rx_desc *rx_desc; | |
a4ee3539 SG |
306 | struct ib_conn *ib_conn = &iser_conn->ib_conn; |
307 | struct iser_device *device = ib_conn->device; | |
bcc60c38 | 308 | |
48afbff6 SG |
309 | if (device->reg_ops->free_reg_res) |
310 | device->reg_ops->free_reg_res(ib_conn); | |
bcc60c38 | 311 | |
5716af6e SG |
312 | rx_desc = iser_conn->rx_descs; |
313 | for (i = 0; i < iser_conn->qp_max_recv_dtos; i++, rx_desc++) | |
bcc60c38 | 314 | ib_dma_unmap_single(device->ib_device, rx_desc->dma_addr, |
986db0d6 | 315 | ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE); |
5716af6e | 316 | kfree(iser_conn->rx_descs); |
986db0d6 | 317 | /* make sure we never redo any unmapping */ |
5716af6e | 318 | iser_conn->rx_descs = NULL; |
986db0d6 | 319 | |
5716af6e | 320 | iser_free_login_buf(iser_conn); |
bcc60c38 OG |
321 | } |
322 | ||
89e984e2 | 323 | static int iser_post_rx_bufs(struct iscsi_conn *conn, struct iscsi_hdr *req) |
e85b24b5 | 324 | { |
5716af6e | 325 | struct iser_conn *iser_conn = conn->dd_data; |
a4ee3539 | 326 | struct ib_conn *ib_conn = &iser_conn->ib_conn; |
6a06a4b8 | 327 | struct iscsi_session *session = conn->session; |
e85b24b5 | 328 | |
89e984e2 OG |
329 | iser_dbg("req op %x flags %x\n", req->opcode, req->flags); |
330 | /* check if this is the last login - going to full feature phase */ | |
331 | if ((req->flags & ISCSI_FULL_FEATURE_PHASE) != ISCSI_FULL_FEATURE_PHASE) | |
332 | return 0; | |
e85b24b5 | 333 | |
89e984e2 | 334 | /* |
ff3dd52d SG |
335 | * Check that there is one posted recv buffer |
336 | * (for the last login response). | |
89e984e2 | 337 | */ |
a4ee3539 | 338 | WARN_ON(ib_conn->post_recv_buf_count != 1); |
bcc60c38 | 339 | |
6a06a4b8 OG |
340 | if (session->discovery_sess) { |
341 | iser_info("Discovery session, re-using login RX buffer\n"); | |
342 | return 0; | |
343 | } else | |
344 | iser_info("Normal session, posting batch of RX %d buffers\n", | |
5716af6e | 345 | iser_conn->min_posted_rx); |
6a06a4b8 | 346 | |
e85b24b5 | 347 | /* Initial post receive buffers */ |
5716af6e | 348 | if (iser_post_recvm(iser_conn, iser_conn->min_posted_rx)) |
bcc60c38 OG |
349 | return -ENOMEM; |
350 | ||
e85b24b5 OG |
351 | return 0; |
352 | } | |
353 | ||
6ec9d4d2 | 354 | static inline bool iser_signal_comp(u8 sig_count) |
6df5a128 SG |
355 | { |
356 | return ((sig_count % ISER_SIGNAL_CMD_COUNT) == 0); | |
357 | } | |
358 | ||
e85b24b5 OG |
359 | /** |
360 | * iser_send_command - send command PDU | |
361 | */ | |
2747fdb2 | 362 | int iser_send_command(struct iscsi_conn *conn, |
2261ec3d | 363 | struct iscsi_task *task) |
e85b24b5 | 364 | { |
5716af6e | 365 | struct iser_conn *iser_conn = conn->dd_data; |
2261ec3d | 366 | struct iscsi_iser_task *iser_task = task->dd_data; |
e85b24b5 | 367 | unsigned long edtl; |
bcc60c38 | 368 | int err; |
177e31bd | 369 | struct iser_data_buf *data_buf, *prot_buf; |
12352183 | 370 | struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)task->hdr; |
2261ec3d | 371 | struct scsi_cmnd *sc = task->sc; |
f19624aa | 372 | struct iser_tx_desc *tx_desc = &iser_task->desc; |
6ec9d4d2 | 373 | u8 sig_count = ++iser_conn->ib_conn.sig_count; |
e85b24b5 | 374 | |
e85b24b5 OG |
375 | edtl = ntohl(hdr->data_length); |
376 | ||
377 | /* build the tx desc regd header and add it to the tx desc dto */ | |
f19624aa | 378 | tx_desc->type = ISCSI_TX_SCSI_COMMAND; |
cfeb91b3 | 379 | tx_desc->cqe.done = iser_cmd_comp; |
5716af6e | 380 | iser_create_send_desc(iser_conn, tx_desc); |
e85b24b5 | 381 | |
177e31bd | 382 | if (hdr->flags & ISCSI_FLAG_CMD_READ) { |
2261ec3d | 383 | data_buf = &iser_task->data[ISER_DIR_IN]; |
177e31bd SG |
384 | prot_buf = &iser_task->prot[ISER_DIR_IN]; |
385 | } else { | |
2261ec3d | 386 | data_buf = &iser_task->data[ISER_DIR_OUT]; |
177e31bd SG |
387 | prot_buf = &iser_task->prot[ISER_DIR_OUT]; |
388 | } | |
e85b24b5 | 389 | |
da9c0c77 | 390 | if (scsi_sg_count(sc)) { /* using a scatter list */ |
e3784bd1 | 391 | data_buf->sg = scsi_sglist(sc); |
da9c0c77 | 392 | data_buf->size = scsi_sg_count(sc); |
e85b24b5 | 393 | } |
da9c0c77 | 394 | data_buf->data_len = scsi_bufflen(sc); |
e85b24b5 | 395 | |
177e31bd | 396 | if (scsi_prot_sg_count(sc)) { |
e3784bd1 | 397 | prot_buf->sg = scsi_prot_sglist(sc); |
177e31bd | 398 | prot_buf->size = scsi_prot_sg_count(sc); |
a065fe6a SG |
399 | prot_buf->data_len = (data_buf->data_len >> |
400 | ilog2(sc->device->sector_size)) * 8; | |
177e31bd SG |
401 | } |
402 | ||
e85b24b5 | 403 | if (hdr->flags & ISCSI_FLAG_CMD_READ) { |
d77e6535 | 404 | err = iser_prepare_read_cmd(task); |
e85b24b5 OG |
405 | if (err) |
406 | goto send_command_error; | |
407 | } | |
408 | if (hdr->flags & ISCSI_FLAG_CMD_WRITE) { | |
2261ec3d MC |
409 | err = iser_prepare_write_cmd(task, |
410 | task->imm_count, | |
411 | task->imm_count + | |
0f9c7449 | 412 | task->unsol_r2t.data_length, |
e85b24b5 OG |
413 | edtl); |
414 | if (err) | |
415 | goto send_command_error; | |
416 | } | |
417 | ||
2261ec3d | 418 | iser_task->status = ISER_TASK_STATUS_STARTED; |
e85b24b5 | 419 | |
6df5a128 | 420 | err = iser_post_send(&iser_conn->ib_conn, tx_desc, |
6ec9d4d2 | 421 | iser_signal_comp(sig_count)); |
e85b24b5 OG |
422 | if (!err) |
423 | return 0; | |
424 | ||
425 | send_command_error: | |
2261ec3d | 426 | iser_err("conn %p failed task->itt %d err %d\n",conn, task->itt, err); |
e85b24b5 OG |
427 | return err; |
428 | } | |
429 | ||
430 | /** | |
431 | * iser_send_data_out - send data out PDU | |
432 | */ | |
2747fdb2 | 433 | int iser_send_data_out(struct iscsi_conn *conn, |
2261ec3d | 434 | struct iscsi_task *task, |
e85b24b5 OG |
435 | struct iscsi_data *hdr) |
436 | { | |
5716af6e | 437 | struct iser_conn *iser_conn = conn->dd_data; |
2261ec3d | 438 | struct iscsi_iser_task *iser_task = task->dd_data; |
f19624aa | 439 | struct iser_tx_desc *tx_desc = NULL; |
b130eded | 440 | struct iser_mem_reg *mem_reg; |
e85b24b5 OG |
441 | unsigned long buf_offset; |
442 | unsigned long data_seg_len; | |
0a22ab92 | 443 | uint32_t itt; |
d1673905 | 444 | int err; |
f19624aa OG |
445 | struct ib_sge *tx_dsg; |
446 | ||
0a22ab92 | 447 | itt = (__force uint32_t)hdr->itt; |
e85b24b5 OG |
448 | data_seg_len = ntoh24(hdr->dlength); |
449 | buf_offset = ntohl(hdr->offset); | |
450 | ||
451 | iser_dbg("%s itt %d dseg_len %d offset %d\n", | |
452 | __func__,(int)itt,(int)data_seg_len,(int)buf_offset); | |
453 | ||
528f4e8c | 454 | tx_desc = kmem_cache_zalloc(ig.desc_cache, GFP_ATOMIC); |
e85b24b5 OG |
455 | if (tx_desc == NULL) { |
456 | iser_err("Failed to alloc desc for post dataout\n"); | |
457 | return -ENOMEM; | |
458 | } | |
459 | ||
460 | tx_desc->type = ISCSI_TX_DATAOUT; | |
cfeb91b3 | 461 | tx_desc->cqe.done = iser_dataout_comp; |
f19624aa | 462 | tx_desc->iser_header.flags = ISER_VER; |
e85b24b5 OG |
463 | memcpy(&tx_desc->iscsi_header, hdr, sizeof(struct iscsi_hdr)); |
464 | ||
f19624aa | 465 | /* build the tx desc */ |
d1673905 SG |
466 | err = iser_initialize_task_headers(task, tx_desc); |
467 | if (err) | |
468 | goto send_data_out_error; | |
e85b24b5 | 469 | |
b130eded | 470 | mem_reg = &iser_task->rdma_reg[ISER_DIR_OUT]; |
f19624aa | 471 | tx_dsg = &tx_desc->tx_sg[1]; |
90a6684c SG |
472 | tx_dsg->addr = mem_reg->sge.addr + buf_offset; |
473 | tx_dsg->length = data_seg_len; | |
474 | tx_dsg->lkey = mem_reg->sge.lkey; | |
f19624aa | 475 | tx_desc->num_sge = 2; |
e85b24b5 | 476 | |
2261ec3d | 477 | if (buf_offset + data_seg_len > iser_task->data[ISER_DIR_OUT].data_len) { |
e85b24b5 OG |
478 | iser_err("Offset:%ld & DSL:%ld in Data-Out " |
479 | "inconsistent with total len:%ld, itt:%d\n", | |
480 | buf_offset, data_seg_len, | |
2261ec3d | 481 | iser_task->data[ISER_DIR_OUT].data_len, itt); |
e85b24b5 OG |
482 | err = -EINVAL; |
483 | goto send_data_out_error; | |
484 | } | |
485 | iser_dbg("data-out itt: %d, offset: %ld, sz: %ld\n", | |
486 | itt, buf_offset, data_seg_len); | |
487 | ||
488 | ||
6df5a128 | 489 | err = iser_post_send(&iser_conn->ib_conn, tx_desc, true); |
e85b24b5 OG |
490 | if (!err) |
491 | return 0; | |
492 | ||
493 | send_data_out_error: | |
e85b24b5 | 494 | kmem_cache_free(ig.desc_cache, tx_desc); |
d1673905 | 495 | iser_err("conn %p failed err %d\n", conn, err); |
e85b24b5 OG |
496 | return err; |
497 | } | |
498 | ||
499 | int iser_send_control(struct iscsi_conn *conn, | |
2261ec3d | 500 | struct iscsi_task *task) |
e85b24b5 | 501 | { |
5716af6e | 502 | struct iser_conn *iser_conn = conn->dd_data; |
2261ec3d | 503 | struct iscsi_iser_task *iser_task = task->dd_data; |
f19624aa | 504 | struct iser_tx_desc *mdesc = &iser_task->desc; |
e85b24b5 | 505 | unsigned long data_seg_len; |
f19624aa | 506 | int err = 0; |
e85b24b5 OG |
507 | struct iser_device *device; |
508 | ||
e85b24b5 OG |
509 | /* build the tx desc regd header and add it to the tx desc dto */ |
510 | mdesc->type = ISCSI_TX_CONTROL; | |
cfeb91b3 | 511 | mdesc->cqe.done = iser_ctrl_comp; |
5716af6e | 512 | iser_create_send_desc(iser_conn, mdesc); |
e85b24b5 | 513 | |
a4ee3539 | 514 | device = iser_conn->ib_conn.device; |
e85b24b5 | 515 | |
2261ec3d | 516 | data_seg_len = ntoh24(task->hdr->dlength); |
e85b24b5 OG |
517 | |
518 | if (data_seg_len > 0) { | |
0f512b34 | 519 | struct iser_login_desc *desc = &iser_conn->login_desc; |
f19624aa | 520 | struct ib_sge *tx_dsg = &mdesc->tx_sg[1]; |
0f512b34 | 521 | |
f19624aa OG |
522 | if (task != conn->login_task) { |
523 | iser_err("data present on non login task!!!\n"); | |
524 | goto send_control_error; | |
525 | } | |
2c4ce609 | 526 | |
0f512b34 SG |
527 | ib_dma_sync_single_for_cpu(device->ib_device, desc->req_dma, |
528 | task->data_count, DMA_TO_DEVICE); | |
2c4ce609 | 529 | |
0f512b34 | 530 | memcpy(desc->req, task->data, task->data_count); |
2c4ce609 | 531 | |
0f512b34 SG |
532 | ib_dma_sync_single_for_device(device->ib_device, desc->req_dma, |
533 | task->data_count, DMA_TO_DEVICE); | |
2c4ce609 | 534 | |
0f512b34 SG |
535 | tx_dsg->addr = desc->req_dma; |
536 | tx_dsg->length = task->data_count; | |
537 | tx_dsg->lkey = device->pd->local_dma_lkey; | |
f19624aa | 538 | mdesc->num_sge = 2; |
e85b24b5 OG |
539 | } |
540 | ||
bcc60c38 | 541 | if (task == conn->login_task) { |
6a06a4b8 OG |
542 | iser_dbg("op %x dsl %lx, posting login rx buffer\n", |
543 | task->hdr->opcode, data_seg_len); | |
5716af6e | 544 | err = iser_post_recvl(iser_conn); |
bcc60c38 OG |
545 | if (err) |
546 | goto send_control_error; | |
89e984e2 OG |
547 | err = iser_post_rx_bufs(conn, task->hdr); |
548 | if (err) | |
549 | goto send_control_error; | |
e85b24b5 OG |
550 | } |
551 | ||
6df5a128 | 552 | err = iser_post_send(&iser_conn->ib_conn, mdesc, true); |
e85b24b5 OG |
553 | if (!err) |
554 | return 0; | |
555 | ||
556 | send_control_error: | |
e85b24b5 OG |
557 | iser_err("conn %p failed err %d\n",conn, err); |
558 | return err; | |
559 | } | |
560 | ||
cfeb91b3 | 561 | void iser_login_rsp(struct ib_cq *cq, struct ib_wc *wc) |
e85b24b5 | 562 | { |
cfeb91b3 | 563 | struct ib_conn *ib_conn = wc->qp->qp_context; |
7edc5a99 | 564 | struct iser_conn *iser_conn = to_iser_conn(ib_conn); |
cfeb91b3 | 565 | struct iser_login_desc *desc = iser_login(wc->wr_cqe); |
e85b24b5 | 566 | struct iscsi_hdr *hdr; |
0f512b34 | 567 | char *data; |
cfeb91b3 CH |
568 | int length; |
569 | ||
570 | if (unlikely(wc->status != IB_WC_SUCCESS)) { | |
571 | iser_err_comp(wc, "login_rsp"); | |
572 | return; | |
573 | } | |
574 | ||
575 | ib_dma_sync_single_for_cpu(ib_conn->device->ib_device, | |
576 | desc->rsp_dma, ISER_RX_LOGIN_SIZE, | |
577 | DMA_FROM_DEVICE); | |
578 | ||
d3cf81f9 | 579 | hdr = desc->rsp + sizeof(struct iser_ctrl); |
cfeb91b3 CH |
580 | data = desc->rsp + ISER_HEADERS_LEN; |
581 | length = wc->byte_len - ISER_HEADERS_LEN; | |
582 | ||
583 | iser_dbg("op 0x%x itt 0x%x dlen %d\n", hdr->opcode, | |
584 | hdr->itt, length); | |
585 | ||
586 | iscsi_iser_recv(iser_conn->iscsi_conn, hdr, data, length); | |
587 | ||
588 | ib_dma_sync_single_for_device(ib_conn->device->ib_device, | |
589 | desc->rsp_dma, ISER_RX_LOGIN_SIZE, | |
590 | DMA_FROM_DEVICE); | |
591 | ||
592 | ib_conn->post_recv_buf_count--; | |
593 | } | |
594 | ||
59caaed7 JD |
595 | static inline void |
596 | iser_inv_desc(struct iser_fr_desc *desc, u32 rkey) | |
597 | { | |
598 | if (likely(rkey == desc->rsc.mr->rkey)) | |
599 | desc->rsc.mr_valid = 0; | |
600 | else if (likely(rkey == desc->pi_ctx->sig_mr->rkey)) | |
601 | desc->pi_ctx->sig_mr_valid = 0; | |
602 | } | |
603 | ||
604 | static int | |
605 | iser_check_remote_inv(struct iser_conn *iser_conn, | |
606 | struct ib_wc *wc, | |
607 | struct iscsi_hdr *hdr) | |
608 | { | |
609 | if (wc->wc_flags & IB_WC_WITH_INVALIDATE) { | |
610 | struct iscsi_task *task; | |
611 | u32 rkey = wc->ex.invalidate_rkey; | |
612 | ||
613 | iser_dbg("conn %p: remote invalidation for rkey %#x\n", | |
614 | iser_conn, rkey); | |
615 | ||
616 | if (unlikely(!iser_conn->snd_w_inv)) { | |
05a24b9b | 617 | iser_err("conn %p: unexpected remote invalidation, " |
59caaed7 JD |
618 | "terminating connection\n", iser_conn); |
619 | return -EPROTO; | |
620 | } | |
621 | ||
622 | task = iscsi_itt_to_ctask(iser_conn->iscsi_conn, hdr->itt); | |
623 | if (likely(task)) { | |
624 | struct iscsi_iser_task *iser_task = task->dd_data; | |
625 | struct iser_fr_desc *desc; | |
626 | ||
627 | if (iser_task->dir[ISER_DIR_IN]) { | |
628 | desc = iser_task->rdma_reg[ISER_DIR_IN].mem_h; | |
629 | iser_inv_desc(desc, rkey); | |
630 | } | |
631 | ||
632 | if (iser_task->dir[ISER_DIR_OUT]) { | |
633 | desc = iser_task->rdma_reg[ISER_DIR_OUT].mem_h; | |
634 | iser_inv_desc(desc, rkey); | |
635 | } | |
636 | } else { | |
637 | iser_err("failed to get task for itt=%d\n", hdr->itt); | |
638 | return -EINVAL; | |
639 | } | |
640 | } | |
641 | ||
642 | return 0; | |
643 | } | |
644 | ||
645 | ||
cfeb91b3 CH |
646 | void iser_task_rsp(struct ib_cq *cq, struct ib_wc *wc) |
647 | { | |
648 | struct ib_conn *ib_conn = wc->qp->qp_context; | |
649 | struct iser_conn *iser_conn = to_iser_conn(ib_conn); | |
650 | struct iser_rx_desc *desc = iser_rx(wc->wr_cqe); | |
651 | struct iscsi_hdr *hdr; | |
652 | int length; | |
653 | int outstanding, count, err; | |
654 | ||
655 | if (unlikely(wc->status != IB_WC_SUCCESS)) { | |
656 | iser_err_comp(wc, "task_rsp"); | |
657 | return; | |
bcc60c38 | 658 | } |
e85b24b5 | 659 | |
cfeb91b3 CH |
660 | ib_dma_sync_single_for_cpu(ib_conn->device->ib_device, |
661 | desc->dma_addr, ISER_RX_PAYLOAD_SIZE, | |
662 | DMA_FROM_DEVICE); | |
e85b24b5 | 663 | |
cfeb91b3 CH |
664 | hdr = &desc->iscsi_header; |
665 | length = wc->byte_len - ISER_HEADERS_LEN; | |
e85b24b5 | 666 | |
bcc60c38 | 667 | iser_dbg("op 0x%x itt 0x%x dlen %d\n", hdr->opcode, |
cfeb91b3 | 668 | hdr->itt, length); |
e85b24b5 | 669 | |
59caaed7 JD |
670 | if (iser_check_remote_inv(iser_conn, wc, hdr)) { |
671 | iscsi_conn_failure(iser_conn->iscsi_conn, | |
672 | ISCSI_ERR_CONN_FAILED); | |
673 | return; | |
674 | } | |
675 | ||
cfeb91b3 | 676 | iscsi_iser_recv(iser_conn->iscsi_conn, hdr, desc->data, length); |
e85b24b5 | 677 | |
cfeb91b3 CH |
678 | ib_dma_sync_single_for_device(ib_conn->device->ib_device, |
679 | desc->dma_addr, ISER_RX_PAYLOAD_SIZE, | |
680 | DMA_FROM_DEVICE); | |
e85b24b5 OG |
681 | |
682 | /* decrementing conn->post_recv_buf_count only --after-- freeing the * | |
683 | * task eliminates the need to worry on tasks which are completed in * | |
684 | * parallel to the execution of iser_conn_term. So the code that waits * | |
685 | * for the posted rx bufs refcount to become zero handles everything */ | |
a4ee3539 | 686 | ib_conn->post_recv_buf_count--; |
bcc60c38 | 687 | |
a4ee3539 | 688 | outstanding = ib_conn->post_recv_buf_count; |
5716af6e SG |
689 | if (outstanding + iser_conn->min_posted_rx <= iser_conn->qp_max_recv_dtos) { |
690 | count = min(iser_conn->qp_max_recv_dtos - outstanding, | |
691 | iser_conn->min_posted_rx); | |
692 | err = iser_post_recvm(iser_conn, count); | |
bcc60c38 OG |
693 | if (err) |
694 | iser_err("posting %d rx bufs err %d\n", count, err); | |
695 | } | |
e85b24b5 OG |
696 | } |
697 | ||
cfeb91b3 CH |
698 | void iser_cmd_comp(struct ib_cq *cq, struct ib_wc *wc) |
699 | { | |
700 | if (unlikely(wc->status != IB_WC_SUCCESS)) | |
701 | iser_err_comp(wc, "command"); | |
702 | } | |
703 | ||
704 | void iser_ctrl_comp(struct ib_cq *cq, struct ib_wc *wc) | |
e85b24b5 | 705 | { |
cfeb91b3 | 706 | struct iser_tx_desc *desc = iser_tx(wc->wr_cqe); |
2261ec3d | 707 | struct iscsi_task *task; |
e85b24b5 | 708 | |
cfeb91b3 CH |
709 | if (unlikely(wc->status != IB_WC_SUCCESS)) { |
710 | iser_err_comp(wc, "control"); | |
711 | return; | |
f19624aa | 712 | } |
e85b24b5 | 713 | |
cfeb91b3 CH |
714 | /* this arithmetic is legal by libiscsi dd_data allocation */ |
715 | task = (void *)desc - sizeof(struct iscsi_task); | |
716 | if (task->hdr->itt == RESERVED_ITT) | |
717 | iscsi_put_task(task); | |
718 | } | |
719 | ||
720 | void iser_dataout_comp(struct ib_cq *cq, struct ib_wc *wc) | |
721 | { | |
722 | struct iser_tx_desc *desc = iser_tx(wc->wr_cqe); | |
723 | struct ib_conn *ib_conn = wc->qp->qp_context; | |
724 | struct iser_device *device = ib_conn->device; | |
725 | ||
726 | if (unlikely(wc->status != IB_WC_SUCCESS)) | |
727 | iser_err_comp(wc, "dataout"); | |
728 | ||
729 | ib_dma_unmap_single(device->ib_device, desc->dma_addr, | |
730 | ISER_HEADERS_LEN, DMA_TO_DEVICE); | |
731 | kmem_cache_free(ig.desc_cache, desc); | |
732 | } | |
733 | ||
2261ec3d | 734 | void iser_task_rdma_init(struct iscsi_iser_task *iser_task) |
e85b24b5 OG |
735 | |
736 | { | |
2261ec3d | 737 | iser_task->status = ISER_TASK_STATUS_INIT; |
e85b24b5 | 738 | |
2261ec3d MC |
739 | iser_task->dir[ISER_DIR_IN] = 0; |
740 | iser_task->dir[ISER_DIR_OUT] = 0; | |
e85b24b5 | 741 | |
2261ec3d MC |
742 | iser_task->data[ISER_DIR_IN].data_len = 0; |
743 | iser_task->data[ISER_DIR_OUT].data_len = 0; | |
e85b24b5 | 744 | |
177e31bd SG |
745 | iser_task->prot[ISER_DIR_IN].data_len = 0; |
746 | iser_task->prot[ISER_DIR_OUT].data_len = 0; | |
747 | ||
b130eded SG |
748 | memset(&iser_task->rdma_reg[ISER_DIR_IN], 0, |
749 | sizeof(struct iser_mem_reg)); | |
750 | memset(&iser_task->rdma_reg[ISER_DIR_OUT], 0, | |
751 | sizeof(struct iser_mem_reg)); | |
e85b24b5 OG |
752 | } |
753 | ||
2261ec3d | 754 | void iser_task_rdma_finalize(struct iscsi_iser_task *iser_task) |
e85b24b5 | 755 | { |
177e31bd | 756 | int prot_count = scsi_prot_sg_count(iser_task->sc); |
e85b24b5 | 757 | |
9a8b08fa | 758 | if (iser_task->dir[ISER_DIR_IN]) { |
32467c42 | 759 | iser_unreg_rdma_mem(iser_task, ISER_DIR_IN); |
dd0107a0 SG |
760 | iser_dma_unmap_task_data(iser_task, |
761 | &iser_task->data[ISER_DIR_IN], | |
762 | DMA_FROM_DEVICE); | |
763 | if (prot_count) | |
177e31bd | 764 | iser_dma_unmap_task_data(iser_task, |
c6c95ef4 RD |
765 | &iser_task->prot[ISER_DIR_IN], |
766 | DMA_FROM_DEVICE); | |
9a8b08fa | 767 | } |
e85b24b5 | 768 | |
9a8b08fa | 769 | if (iser_task->dir[ISER_DIR_OUT]) { |
32467c42 | 770 | iser_unreg_rdma_mem(iser_task, ISER_DIR_OUT); |
dd0107a0 SG |
771 | iser_dma_unmap_task_data(iser_task, |
772 | &iser_task->data[ISER_DIR_OUT], | |
773 | DMA_TO_DEVICE); | |
774 | if (prot_count) | |
9a8b08fa | 775 | iser_dma_unmap_task_data(iser_task, |
c6c95ef4 RD |
776 | &iser_task->prot[ISER_DIR_OUT], |
777 | DMA_TO_DEVICE); | |
9a8b08fa | 778 | } |
e85b24b5 | 779 | } |