IB/iser: Fix wrong calculation of protection buffer length
[deliverable/linux.git] / drivers / infiniband / ulp / iser / iser_initiator.c
CommitLineData
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 48static int iser_prepare_read_cmd(struct iscsi_task *task)
e85b24b5
OG
49
50{
2261ec3d 51 struct iscsi_iser_task *iser_task = task->dd_data;
a4ee3539 52 struct iser_device *device = iser_task->iser_conn->ib_conn.device;
e85b24b5
OG
53 struct iser_regd_buf *regd_buf;
54 int err;
2261ec3d
MC
55 struct iser_hdr *hdr = &iser_task->desc.iser_header;
56 struct iser_data_buf *buf_in = &iser_task->data[ISER_DIR_IN];
e85b24b5 57
2261ec3d 58 err = iser_dma_map_task_data(iser_task,
e85b24b5
OG
59 buf_in,
60 ISER_DIR_IN,
61 DMA_FROM_DEVICE);
62 if (err)
63 return err;
64
177e31bd
SG
65 if (scsi_prot_sg_count(iser_task->sc)) {
66 struct iser_data_buf *pbuf_in = &iser_task->prot[ISER_DIR_IN];
67
68 err = iser_dma_map_task_data(iser_task,
69 pbuf_in,
70 ISER_DIR_IN,
71 DMA_FROM_DEVICE);
72 if (err)
73 return err;
74 }
75
b4e155ff 76 err = device->iser_reg_rdma_mem(iser_task, ISER_DIR_IN);
e85b24b5
OG
77 if (err) {
78 iser_err("Failed to set up Data-IN RDMA\n");
79 return err;
80 }
2261ec3d 81 regd_buf = &iser_task->rdma_regd[ISER_DIR_IN];
e85b24b5
OG
82
83 hdr->flags |= ISER_RSV;
84 hdr->read_stag = cpu_to_be32(regd_buf->reg.rkey);
85 hdr->read_va = cpu_to_be64(regd_buf->reg.va);
86
87 iser_dbg("Cmd itt:%d READ tags RKEY:%#.4X VA:%#llX\n",
2261ec3d 88 task->itt, regd_buf->reg.rkey,
e85b24b5
OG
89 (unsigned long long)regd_buf->reg.va);
90
91 return 0;
92}
93
94/* Register user buffer memory and initialize passive rdma
d77e6535
SG
95 * dto descriptor. Data size is stored in
96 * task->data[ISER_DIR_OUT].data_len, Protection size
97 * is stored at task->prot[ISER_DIR_OUT].data_len
e85b24b5
OG
98 */
99static int
2261ec3d 100iser_prepare_write_cmd(struct iscsi_task *task,
e85b24b5
OG
101 unsigned int imm_sz,
102 unsigned int unsol_sz,
103 unsigned int edtl)
104{
2261ec3d 105 struct iscsi_iser_task *iser_task = task->dd_data;
a4ee3539 106 struct iser_device *device = iser_task->iser_conn->ib_conn.device;
e85b24b5
OG
107 struct iser_regd_buf *regd_buf;
108 int err;
2261ec3d
MC
109 struct iser_hdr *hdr = &iser_task->desc.iser_header;
110 struct iser_data_buf *buf_out = &iser_task->data[ISER_DIR_OUT];
f19624aa 111 struct ib_sge *tx_dsg = &iser_task->desc.tx_sg[1];
e85b24b5 112
2261ec3d 113 err = iser_dma_map_task_data(iser_task,
e85b24b5
OG
114 buf_out,
115 ISER_DIR_OUT,
116 DMA_TO_DEVICE);
117 if (err)
118 return err;
119
177e31bd
SG
120 if (scsi_prot_sg_count(iser_task->sc)) {
121 struct iser_data_buf *pbuf_out = &iser_task->prot[ISER_DIR_OUT];
122
123 err = iser_dma_map_task_data(iser_task,
124 pbuf_out,
125 ISER_DIR_OUT,
126 DMA_TO_DEVICE);
127 if (err)
128 return err;
129 }
130
b4e155ff 131 err = device->iser_reg_rdma_mem(iser_task, ISER_DIR_OUT);
e85b24b5
OG
132 if (err != 0) {
133 iser_err("Failed to register write cmd RDMA mem\n");
134 return err;
135 }
136
2261ec3d 137 regd_buf = &iser_task->rdma_regd[ISER_DIR_OUT];
e85b24b5
OG
138
139 if (unsol_sz < edtl) {
140 hdr->flags |= ISER_WSV;
141 hdr->write_stag = cpu_to_be32(regd_buf->reg.rkey);
142 hdr->write_va = cpu_to_be64(regd_buf->reg.va + unsol_sz);
143
144 iser_dbg("Cmd itt:%d, WRITE tags, RKEY:%#.4X "
145 "VA:%#llX + unsol:%d\n",
2261ec3d 146 task->itt, regd_buf->reg.rkey,
e85b24b5
OG
147 (unsigned long long)regd_buf->reg.va, unsol_sz);
148 }
149
150 if (imm_sz > 0) {
151 iser_dbg("Cmd itt:%d, WRITE, adding imm.data sz: %d\n",
2261ec3d 152 task->itt, imm_sz);
f19624aa
OG
153 tx_dsg->addr = regd_buf->reg.va;
154 tx_dsg->length = imm_sz;
155 tx_dsg->lkey = regd_buf->reg.lkey;
156 iser_task->desc.num_sge = 2;
e85b24b5
OG
157 }
158
159 return 0;
160}
161
e85b24b5 162/* creates a new tx descriptor and adds header regd buffer */
5716af6e 163static void iser_create_send_desc(struct iser_conn *iser_conn,
f19624aa 164 struct iser_tx_desc *tx_desc)
e85b24b5 165{
a4ee3539 166 struct iser_device *device = iser_conn->ib_conn.device;
e85b24b5 167
f19624aa
OG
168 ib_dma_sync_single_for_cpu(device->ib_device,
169 tx_desc->dma_addr, ISER_HEADERS_LEN, DMA_TO_DEVICE);
e85b24b5
OG
170
171 memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr));
172 tx_desc->iser_header.flags = ISER_VER;
173
f19624aa
OG
174 tx_desc->num_sge = 1;
175
176 if (tx_desc->tx_sg[0].lkey != device->mr->lkey) {
177 tx_desc->tx_sg[0].lkey = device->mr->lkey;
178 iser_dbg("sdesc %p lkey mismatch, fixing\n", tx_desc);
179 }
e85b24b5
OG
180}
181
5716af6e 182static void iser_free_login_buf(struct iser_conn *iser_conn)
986db0d6 183{
a4ee3539
SG
184 struct iser_device *device = iser_conn->ib_conn.device;
185
5716af6e 186 if (!iser_conn->login_buf)
986db0d6
SP
187 return;
188
5716af6e 189 if (iser_conn->login_req_dma)
a4ee3539 190 ib_dma_unmap_single(device->ib_device,
5716af6e 191 iser_conn->login_req_dma,
986db0d6
SP
192 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_TO_DEVICE);
193
5716af6e 194 if (iser_conn->login_resp_dma)
a4ee3539 195 ib_dma_unmap_single(device->ib_device,
5716af6e 196 iser_conn->login_resp_dma,
986db0d6
SP
197 ISER_RX_LOGIN_SIZE, DMA_FROM_DEVICE);
198
5716af6e 199 kfree(iser_conn->login_buf);
986db0d6
SP
200
201 /* make sure we never redo any unmapping */
5716af6e
SG
202 iser_conn->login_req_dma = 0;
203 iser_conn->login_resp_dma = 0;
204 iser_conn->login_buf = NULL;
986db0d6
SP
205}
206
5716af6e 207static int iser_alloc_login_buf(struct iser_conn *iser_conn)
986db0d6 208{
a4ee3539 209 struct iser_device *device = iser_conn->ib_conn.device;
986db0d6
SP
210 int req_err, resp_err;
211
a4ee3539 212 BUG_ON(device == NULL);
986db0d6 213
5716af6e 214 iser_conn->login_buf = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
986db0d6 215 ISER_RX_LOGIN_SIZE, GFP_KERNEL);
5716af6e 216 if (!iser_conn->login_buf)
986db0d6
SP
217 goto out_err;
218
5716af6e
SG
219 iser_conn->login_req_buf = iser_conn->login_buf;
220 iser_conn->login_resp_buf = iser_conn->login_buf +
986db0d6
SP
221 ISCSI_DEF_MAX_RECV_SEG_LEN;
222
5716af6e
SG
223 iser_conn->login_req_dma = ib_dma_map_single(device->ib_device,
224 iser_conn->login_req_buf,
225 ISCSI_DEF_MAX_RECV_SEG_LEN,
226 DMA_TO_DEVICE);
986db0d6 227
5716af6e
SG
228 iser_conn->login_resp_dma = ib_dma_map_single(device->ib_device,
229 iser_conn->login_resp_buf,
230 ISER_RX_LOGIN_SIZE,
231 DMA_FROM_DEVICE);
986db0d6
SP
232
233 req_err = ib_dma_mapping_error(device->ib_device,
5716af6e 234 iser_conn->login_req_dma);
986db0d6 235 resp_err = ib_dma_mapping_error(device->ib_device,
5716af6e 236 iser_conn->login_resp_dma);
f19624aa 237
986db0d6
SP
238 if (req_err || resp_err) {
239 if (req_err)
5716af6e 240 iser_conn->login_req_dma = 0;
986db0d6 241 if (resp_err)
5716af6e 242 iser_conn->login_resp_dma = 0;
986db0d6
SP
243 goto free_login_buf;
244 }
245 return 0;
246
247free_login_buf:
5716af6e 248 iser_free_login_buf(iser_conn);
986db0d6
SP
249
250out_err:
251 iser_err("unable to alloc or map login buf\n");
252 return -ENOMEM;
253}
f19624aa 254
5716af6e
SG
255int iser_alloc_rx_descriptors(struct iser_conn *iser_conn,
256 struct iscsi_session *session)
bcc60c38
OG
257{
258 int i, j;
259 u64 dma_addr;
260 struct iser_rx_desc *rx_desc;
261 struct ib_sge *rx_sg;
a4ee3539
SG
262 struct ib_conn *ib_conn = &iser_conn->ib_conn;
263 struct iser_device *device = ib_conn->device;
bcc60c38 264
5716af6e
SG
265 iser_conn->qp_max_recv_dtos = session->cmds_max;
266 iser_conn->qp_max_recv_dtos_mask = session->cmds_max - 1; /* cmds_max is 2^N */
267 iser_conn->min_posted_rx = iser_conn->qp_max_recv_dtos >> 2;
b7f04513 268
a4ee3539 269 if (device->iser_alloc_rdma_reg_res(ib_conn, session->scsi_cmds_max))
b4e155ff 270 goto create_rdma_reg_res_failed;
986db0d6 271
5716af6e 272 if (iser_alloc_login_buf(iser_conn))
986db0d6
SP
273 goto alloc_login_buf_fail;
274
6aabfa76
SG
275 iser_conn->num_rx_descs = session->cmds_max;
276 iser_conn->rx_descs = kmalloc(iser_conn->num_rx_descs *
bcc60c38 277 sizeof(struct iser_rx_desc), GFP_KERNEL);
5716af6e 278 if (!iser_conn->rx_descs)
bcc60c38
OG
279 goto rx_desc_alloc_fail;
280
5716af6e 281 rx_desc = iser_conn->rx_descs;
bcc60c38 282
5716af6e 283 for (i = 0; i < iser_conn->qp_max_recv_dtos; i++, rx_desc++) {
bcc60c38
OG
284 dma_addr = ib_dma_map_single(device->ib_device, (void *)rx_desc,
285 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
286 if (ib_dma_mapping_error(device->ib_device, dma_addr))
287 goto rx_desc_dma_map_failed;
288
289 rx_desc->dma_addr = dma_addr;
290
291 rx_sg = &rx_desc->rx_sg;
292 rx_sg->addr = rx_desc->dma_addr;
293 rx_sg->length = ISER_RX_PAYLOAD_SIZE;
294 rx_sg->lkey = device->mr->lkey;
295 }
296
5716af6e 297 iser_conn->rx_desc_head = 0;
bcc60c38
OG
298 return 0;
299
300rx_desc_dma_map_failed:
5716af6e 301 rx_desc = iser_conn->rx_descs;
bcc60c38
OG
302 for (j = 0; j < i; j++, rx_desc++)
303 ib_dma_unmap_single(device->ib_device, rx_desc->dma_addr,
986db0d6 304 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
5716af6e
SG
305 kfree(iser_conn->rx_descs);
306 iser_conn->rx_descs = NULL;
bcc60c38 307rx_desc_alloc_fail:
5716af6e 308 iser_free_login_buf(iser_conn);
986db0d6 309alloc_login_buf_fail:
a4ee3539 310 device->iser_free_rdma_reg_res(ib_conn);
b4e155ff 311create_rdma_reg_res_failed:
bcc60c38
OG
312 iser_err("failed allocating rx descriptors / data buffers\n");
313 return -ENOMEM;
314}
315
5716af6e 316void iser_free_rx_descriptors(struct iser_conn *iser_conn)
bcc60c38
OG
317{
318 int i;
319 struct iser_rx_desc *rx_desc;
a4ee3539
SG
320 struct ib_conn *ib_conn = &iser_conn->ib_conn;
321 struct iser_device *device = ib_conn->device;
bcc60c38 322
2e02d653 323 if (device->iser_free_rdma_reg_res)
a4ee3539 324 device->iser_free_rdma_reg_res(ib_conn);
bcc60c38 325
5716af6e
SG
326 rx_desc = iser_conn->rx_descs;
327 for (i = 0; i < iser_conn->qp_max_recv_dtos; i++, rx_desc++)
bcc60c38 328 ib_dma_unmap_single(device->ib_device, rx_desc->dma_addr,
986db0d6 329 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
5716af6e 330 kfree(iser_conn->rx_descs);
986db0d6 331 /* make sure we never redo any unmapping */
5716af6e 332 iser_conn->rx_descs = NULL;
986db0d6 333
5716af6e 334 iser_free_login_buf(iser_conn);
bcc60c38
OG
335}
336
89e984e2 337static int iser_post_rx_bufs(struct iscsi_conn *conn, struct iscsi_hdr *req)
e85b24b5 338{
5716af6e 339 struct iser_conn *iser_conn = conn->dd_data;
a4ee3539 340 struct ib_conn *ib_conn = &iser_conn->ib_conn;
6a06a4b8 341 struct iscsi_session *session = conn->session;
e85b24b5 342
89e984e2
OG
343 iser_dbg("req op %x flags %x\n", req->opcode, req->flags);
344 /* check if this is the last login - going to full feature phase */
345 if ((req->flags & ISCSI_FULL_FEATURE_PHASE) != ISCSI_FULL_FEATURE_PHASE)
346 return 0;
e85b24b5 347
89e984e2 348 /*
ff3dd52d
SG
349 * Check that there is one posted recv buffer
350 * (for the last login response).
89e984e2 351 */
a4ee3539 352 WARN_ON(ib_conn->post_recv_buf_count != 1);
bcc60c38 353
6a06a4b8
OG
354 if (session->discovery_sess) {
355 iser_info("Discovery session, re-using login RX buffer\n");
356 return 0;
357 } else
358 iser_info("Normal session, posting batch of RX %d buffers\n",
5716af6e 359 iser_conn->min_posted_rx);
6a06a4b8 360
e85b24b5 361 /* Initial post receive buffers */
5716af6e 362 if (iser_post_recvm(iser_conn, iser_conn->min_posted_rx))
bcc60c38
OG
363 return -ENOMEM;
364
e85b24b5
OG
365 return 0;
366}
367
6ec9d4d2 368static inline bool iser_signal_comp(u8 sig_count)
6df5a128
SG
369{
370 return ((sig_count % ISER_SIGNAL_CMD_COUNT) == 0);
371}
372
e85b24b5
OG
373/**
374 * iser_send_command - send command PDU
375 */
2747fdb2 376int iser_send_command(struct iscsi_conn *conn,
2261ec3d 377 struct iscsi_task *task)
e85b24b5 378{
5716af6e 379 struct iser_conn *iser_conn = conn->dd_data;
2261ec3d 380 struct iscsi_iser_task *iser_task = task->dd_data;
e85b24b5 381 unsigned long edtl;
bcc60c38 382 int err;
177e31bd 383 struct iser_data_buf *data_buf, *prot_buf;
12352183 384 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)task->hdr;
2261ec3d 385 struct scsi_cmnd *sc = task->sc;
f19624aa 386 struct iser_tx_desc *tx_desc = &iser_task->desc;
6ec9d4d2 387 u8 sig_count = ++iser_conn->ib_conn.sig_count;
e85b24b5 388
e85b24b5
OG
389 edtl = ntohl(hdr->data_length);
390
391 /* build the tx desc regd header and add it to the tx desc dto */
f19624aa 392 tx_desc->type = ISCSI_TX_SCSI_COMMAND;
5716af6e 393 iser_create_send_desc(iser_conn, tx_desc);
e85b24b5 394
177e31bd 395 if (hdr->flags & ISCSI_FLAG_CMD_READ) {
2261ec3d 396 data_buf = &iser_task->data[ISER_DIR_IN];
177e31bd
SG
397 prot_buf = &iser_task->prot[ISER_DIR_IN];
398 } else {
2261ec3d 399 data_buf = &iser_task->data[ISER_DIR_OUT];
177e31bd
SG
400 prot_buf = &iser_task->prot[ISER_DIR_OUT];
401 }
e85b24b5 402
da9c0c77
FT
403 if (scsi_sg_count(sc)) { /* using a scatter list */
404 data_buf->buf = scsi_sglist(sc);
405 data_buf->size = scsi_sg_count(sc);
e85b24b5 406 }
da9c0c77 407 data_buf->data_len = scsi_bufflen(sc);
e85b24b5 408
177e31bd
SG
409 if (scsi_prot_sg_count(sc)) {
410 prot_buf->buf = scsi_prot_sglist(sc);
411 prot_buf->size = scsi_prot_sg_count(sc);
a065fe6a
SG
412 prot_buf->data_len = (data_buf->data_len >>
413 ilog2(sc->device->sector_size)) * 8;
177e31bd
SG
414 }
415
e85b24b5 416 if (hdr->flags & ISCSI_FLAG_CMD_READ) {
d77e6535 417 err = iser_prepare_read_cmd(task);
e85b24b5
OG
418 if (err)
419 goto send_command_error;
420 }
421 if (hdr->flags & ISCSI_FLAG_CMD_WRITE) {
2261ec3d
MC
422 err = iser_prepare_write_cmd(task,
423 task->imm_count,
424 task->imm_count +
0f9c7449 425 task->unsol_r2t.data_length,
e85b24b5
OG
426 edtl);
427 if (err)
428 goto send_command_error;
429 }
430
2261ec3d 431 iser_task->status = ISER_TASK_STATUS_STARTED;
e85b24b5 432
6df5a128 433 err = iser_post_send(&iser_conn->ib_conn, tx_desc,
6ec9d4d2 434 iser_signal_comp(sig_count));
e85b24b5
OG
435 if (!err)
436 return 0;
437
438send_command_error:
2261ec3d 439 iser_err("conn %p failed task->itt %d err %d\n",conn, task->itt, err);
e85b24b5
OG
440 return err;
441}
442
443/**
444 * iser_send_data_out - send data out PDU
445 */
2747fdb2 446int iser_send_data_out(struct iscsi_conn *conn,
2261ec3d 447 struct iscsi_task *task,
e85b24b5
OG
448 struct iscsi_data *hdr)
449{
5716af6e 450 struct iser_conn *iser_conn = conn->dd_data;
2261ec3d 451 struct iscsi_iser_task *iser_task = task->dd_data;
f19624aa
OG
452 struct iser_tx_desc *tx_desc = NULL;
453 struct iser_regd_buf *regd_buf;
e85b24b5
OG
454 unsigned long buf_offset;
455 unsigned long data_seg_len;
0a22ab92 456 uint32_t itt;
e85b24b5 457 int err = 0;
f19624aa
OG
458 struct ib_sge *tx_dsg;
459
0a22ab92 460 itt = (__force uint32_t)hdr->itt;
e85b24b5
OG
461 data_seg_len = ntoh24(hdr->dlength);
462 buf_offset = ntohl(hdr->offset);
463
464 iser_dbg("%s itt %d dseg_len %d offset %d\n",
465 __func__,(int)itt,(int)data_seg_len,(int)buf_offset);
466
528f4e8c 467 tx_desc = kmem_cache_zalloc(ig.desc_cache, GFP_ATOMIC);
e85b24b5
OG
468 if (tx_desc == NULL) {
469 iser_err("Failed to alloc desc for post dataout\n");
470 return -ENOMEM;
471 }
472
473 tx_desc->type = ISCSI_TX_DATAOUT;
f19624aa 474 tx_desc->iser_header.flags = ISER_VER;
e85b24b5
OG
475 memcpy(&tx_desc->iscsi_header, hdr, sizeof(struct iscsi_hdr));
476
f19624aa
OG
477 /* build the tx desc */
478 iser_initialize_task_headers(task, tx_desc);
e85b24b5 479
f19624aa
OG
480 regd_buf = &iser_task->rdma_regd[ISER_DIR_OUT];
481 tx_dsg = &tx_desc->tx_sg[1];
482 tx_dsg->addr = regd_buf->reg.va + buf_offset;
483 tx_dsg->length = data_seg_len;
484 tx_dsg->lkey = regd_buf->reg.lkey;
485 tx_desc->num_sge = 2;
e85b24b5 486
2261ec3d 487 if (buf_offset + data_seg_len > iser_task->data[ISER_DIR_OUT].data_len) {
e85b24b5
OG
488 iser_err("Offset:%ld & DSL:%ld in Data-Out "
489 "inconsistent with total len:%ld, itt:%d\n",
490 buf_offset, data_seg_len,
2261ec3d 491 iser_task->data[ISER_DIR_OUT].data_len, itt);
e85b24b5
OG
492 err = -EINVAL;
493 goto send_data_out_error;
494 }
495 iser_dbg("data-out itt: %d, offset: %ld, sz: %ld\n",
496 itt, buf_offset, data_seg_len);
497
498
6df5a128 499 err = iser_post_send(&iser_conn->ib_conn, tx_desc, true);
e85b24b5
OG
500 if (!err)
501 return 0;
502
503send_data_out_error:
e85b24b5
OG
504 kmem_cache_free(ig.desc_cache, tx_desc);
505 iser_err("conn %p failed err %d\n",conn, err);
506 return err;
507}
508
509int iser_send_control(struct iscsi_conn *conn,
2261ec3d 510 struct iscsi_task *task)
e85b24b5 511{
5716af6e 512 struct iser_conn *iser_conn = conn->dd_data;
2261ec3d 513 struct iscsi_iser_task *iser_task = task->dd_data;
f19624aa 514 struct iser_tx_desc *mdesc = &iser_task->desc;
e85b24b5 515 unsigned long data_seg_len;
f19624aa 516 int err = 0;
e85b24b5
OG
517 struct iser_device *device;
518
e85b24b5
OG
519 /* build the tx desc regd header and add it to the tx desc dto */
520 mdesc->type = ISCSI_TX_CONTROL;
5716af6e 521 iser_create_send_desc(iser_conn, mdesc);
e85b24b5 522
a4ee3539 523 device = iser_conn->ib_conn.device;
e85b24b5 524
2261ec3d 525 data_seg_len = ntoh24(task->hdr->dlength);
e85b24b5
OG
526
527 if (data_seg_len > 0) {
f19624aa
OG
528 struct ib_sge *tx_dsg = &mdesc->tx_sg[1];
529 if (task != conn->login_task) {
530 iser_err("data present on non login task!!!\n");
531 goto send_control_error;
532 }
2c4ce609
OG
533
534 ib_dma_sync_single_for_cpu(device->ib_device,
5716af6e 535 iser_conn->login_req_dma, task->data_count,
2c4ce609
OG
536 DMA_TO_DEVICE);
537
5716af6e 538 memcpy(iser_conn->login_req_buf, task->data, task->data_count);
2c4ce609
OG
539
540 ib_dma_sync_single_for_device(device->ib_device,
5716af6e 541 iser_conn->login_req_dma, task->data_count,
2c4ce609
OG
542 DMA_TO_DEVICE);
543
5716af6e 544 tx_dsg->addr = iser_conn->login_req_dma;
200ae1a0 545 tx_dsg->length = task->data_count;
f19624aa
OG
546 tx_dsg->lkey = device->mr->lkey;
547 mdesc->num_sge = 2;
e85b24b5
OG
548 }
549
bcc60c38 550 if (task == conn->login_task) {
6a06a4b8
OG
551 iser_dbg("op %x dsl %lx, posting login rx buffer\n",
552 task->hdr->opcode, data_seg_len);
5716af6e 553 err = iser_post_recvl(iser_conn);
bcc60c38
OG
554 if (err)
555 goto send_control_error;
89e984e2
OG
556 err = iser_post_rx_bufs(conn, task->hdr);
557 if (err)
558 goto send_control_error;
e85b24b5
OG
559 }
560
6df5a128 561 err = iser_post_send(&iser_conn->ib_conn, mdesc, true);
e85b24b5
OG
562 if (!err)
563 return 0;
564
565send_control_error:
e85b24b5
OG
566 iser_err("conn %p failed err %d\n",conn, err);
567 return err;
568}
569
570/**
571 * iser_rcv_dto_completion - recv DTO completion
572 */
bcc60c38
OG
573void iser_rcv_completion(struct iser_rx_desc *rx_desc,
574 unsigned long rx_xfer_len,
a4ee3539 575 struct ib_conn *ib_conn)
e85b24b5 576{
a4ee3539
SG
577 struct iser_conn *iser_conn = container_of(ib_conn, struct iser_conn,
578 ib_conn);
e85b24b5 579 struct iscsi_hdr *hdr;
bcc60c38
OG
580 u64 rx_dma;
581 int rx_buflen, outstanding, count, err;
582
583 /* differentiate between login to all other PDUs */
5716af6e
SG
584 if ((char *)rx_desc == iser_conn->login_resp_buf) {
585 rx_dma = iser_conn->login_resp_dma;
bcc60c38
OG
586 rx_buflen = ISER_RX_LOGIN_SIZE;
587 } else {
588 rx_dma = rx_desc->dma_addr;
589 rx_buflen = ISER_RX_PAYLOAD_SIZE;
590 }
e85b24b5 591
a4ee3539 592 ib_dma_sync_single_for_cpu(ib_conn->device->ib_device, rx_dma,
5716af6e 593 rx_buflen, DMA_FROM_DEVICE);
e85b24b5 594
bcc60c38 595 hdr = &rx_desc->iscsi_header;
e85b24b5 596
bcc60c38
OG
597 iser_dbg("op 0x%x itt 0x%x dlen %d\n", hdr->opcode,
598 hdr->itt, (int)(rx_xfer_len - ISER_HEADERS_LEN));
e85b24b5 599
5716af6e 600 iscsi_iser_recv(iser_conn->iscsi_conn, hdr, rx_desc->data,
4667f5df 601 rx_xfer_len - ISER_HEADERS_LEN);
e85b24b5 602
a4ee3539 603 ib_dma_sync_single_for_device(ib_conn->device->ib_device, rx_dma,
4667f5df 604 rx_buflen, DMA_FROM_DEVICE);
e85b24b5
OG
605
606 /* decrementing conn->post_recv_buf_count only --after-- freeing the *
607 * task eliminates the need to worry on tasks which are completed in *
608 * parallel to the execution of iser_conn_term. So the code that waits *
609 * for the posted rx bufs refcount to become zero handles everything */
a4ee3539 610 ib_conn->post_recv_buf_count--;
bcc60c38 611
5716af6e 612 if (rx_dma == iser_conn->login_resp_dma)
bcc60c38
OG
613 return;
614
a4ee3539 615 outstanding = ib_conn->post_recv_buf_count;
5716af6e
SG
616 if (outstanding + iser_conn->min_posted_rx <= iser_conn->qp_max_recv_dtos) {
617 count = min(iser_conn->qp_max_recv_dtos - outstanding,
618 iser_conn->min_posted_rx);
619 err = iser_post_recvm(iser_conn, count);
bcc60c38
OG
620 if (err)
621 iser_err("posting %d rx bufs err %d\n", count, err);
622 }
e85b24b5
OG
623}
624
f19624aa 625void iser_snd_completion(struct iser_tx_desc *tx_desc,
a4ee3539 626 struct ib_conn *ib_conn)
e85b24b5 627{
2261ec3d 628 struct iscsi_task *task;
a4ee3539 629 struct iser_device *device = ib_conn->device;
e85b24b5 630
f19624aa
OG
631 if (tx_desc->type == ISCSI_TX_DATAOUT) {
632 ib_dma_unmap_single(device->ib_device, tx_desc->dma_addr,
633 ISER_HEADERS_LEN, DMA_TO_DEVICE);
e85b24b5 634 kmem_cache_free(ig.desc_cache, tx_desc);
fd8b48b2 635 tx_desc = NULL;
f19624aa 636 }
e85b24b5 637
fd8b48b2 638 if (tx_desc && tx_desc->type == ISCSI_TX_CONTROL) {
e85b24b5 639 /* this arithmetic is legal by libiscsi dd_data allocation */
2261ec3d
MC
640 task = (void *) ((long)(void *)tx_desc -
641 sizeof(struct iscsi_task));
642 if (task->hdr->itt == RESERVED_ITT)
643 iscsi_put_task(task);
e85b24b5
OG
644 }
645}
646
2261ec3d 647void iser_task_rdma_init(struct iscsi_iser_task *iser_task)
e85b24b5
OG
648
649{
2261ec3d 650 iser_task->status = ISER_TASK_STATUS_INIT;
e85b24b5 651
2261ec3d
MC
652 iser_task->dir[ISER_DIR_IN] = 0;
653 iser_task->dir[ISER_DIR_OUT] = 0;
e85b24b5 654
2261ec3d
MC
655 iser_task->data[ISER_DIR_IN].data_len = 0;
656 iser_task->data[ISER_DIR_OUT].data_len = 0;
e85b24b5 657
177e31bd
SG
658 iser_task->prot[ISER_DIR_IN].data_len = 0;
659 iser_task->prot[ISER_DIR_OUT].data_len = 0;
660
2261ec3d 661 memset(&iser_task->rdma_regd[ISER_DIR_IN], 0,
e85b24b5 662 sizeof(struct iser_regd_buf));
2261ec3d 663 memset(&iser_task->rdma_regd[ISER_DIR_OUT], 0,
e85b24b5
OG
664 sizeof(struct iser_regd_buf));
665}
666
2261ec3d 667void iser_task_rdma_finalize(struct iscsi_iser_task *iser_task)
e85b24b5 668{
a4ee3539 669 struct iser_device *device = iser_task->iser_conn->ib_conn.device;
9a8b08fa 670 int is_rdma_data_aligned = 1;
177e31bd
SG
671 int is_rdma_prot_aligned = 1;
672 int prot_count = scsi_prot_sg_count(iser_task->sc);
e85b24b5
OG
673
674 /* if we were reading, copy back to unaligned sglist,
675 * anyway dma_unmap and free the copy
676 */
2261ec3d 677 if (iser_task->data_copy[ISER_DIR_IN].copy_buf != NULL) {
9a8b08fa
SG
678 is_rdma_data_aligned = 0;
679 iser_finalize_rdma_unaligned_sg(iser_task,
680 &iser_task->data[ISER_DIR_IN],
681 &iser_task->data_copy[ISER_DIR_IN],
682 ISER_DIR_IN);
74a20780 683 }
9a8b08fa 684
2261ec3d 685 if (iser_task->data_copy[ISER_DIR_OUT].copy_buf != NULL) {
9a8b08fa
SG
686 is_rdma_data_aligned = 0;
687 iser_finalize_rdma_unaligned_sg(iser_task,
688 &iser_task->data[ISER_DIR_OUT],
689 &iser_task->data_copy[ISER_DIR_OUT],
690 ISER_DIR_OUT);
74a20780 691 }
e85b24b5 692
177e31bd
SG
693 if (iser_task->prot_copy[ISER_DIR_IN].copy_buf != NULL) {
694 is_rdma_prot_aligned = 0;
695 iser_finalize_rdma_unaligned_sg(iser_task,
696 &iser_task->prot[ISER_DIR_IN],
697 &iser_task->prot_copy[ISER_DIR_IN],
698 ISER_DIR_IN);
699 }
700
701 if (iser_task->prot_copy[ISER_DIR_OUT].copy_buf != NULL) {
702 is_rdma_prot_aligned = 0;
703 iser_finalize_rdma_unaligned_sg(iser_task,
704 &iser_task->prot[ISER_DIR_OUT],
705 &iser_task->prot_copy[ISER_DIR_OUT],
706 ISER_DIR_OUT);
707 }
708
9a8b08fa 709 if (iser_task->dir[ISER_DIR_IN]) {
b4e155ff 710 device->iser_unreg_rdma_mem(iser_task, ISER_DIR_IN);
9a8b08fa
SG
711 if (is_rdma_data_aligned)
712 iser_dma_unmap_task_data(iser_task,
c6c95ef4
RD
713 &iser_task->data[ISER_DIR_IN],
714 DMA_FROM_DEVICE);
177e31bd
SG
715 if (prot_count && is_rdma_prot_aligned)
716 iser_dma_unmap_task_data(iser_task,
c6c95ef4
RD
717 &iser_task->prot[ISER_DIR_IN],
718 DMA_FROM_DEVICE);
9a8b08fa 719 }
e85b24b5 720
9a8b08fa
SG
721 if (iser_task->dir[ISER_DIR_OUT]) {
722 device->iser_unreg_rdma_mem(iser_task, ISER_DIR_OUT);
723 if (is_rdma_data_aligned)
724 iser_dma_unmap_task_data(iser_task,
c6c95ef4
RD
725 &iser_task->data[ISER_DIR_OUT],
726 DMA_TO_DEVICE);
9a8b08fa
SG
727 if (prot_count && is_rdma_prot_aligned)
728 iser_dma_unmap_task_data(iser_task,
c6c95ef4
RD
729 &iser_task->prot[ISER_DIR_OUT],
730 DMA_TO_DEVICE);
9a8b08fa 731 }
e85b24b5 732}
This page took 0.711383 seconds and 5 git commands to generate.