Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / infiniband / ulp / iser / iser_initiator.c
blobdf49c4eb67f7bbe7353379bc2d75b71af3761a5c
1 /*
2 * Copyright (c) 2004, 2005, 2006 Voltaire, Inc. All rights reserved.
3 * Copyright (c) 2013-2014 Mellanox Technologies. All rights reserved.
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:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
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.
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.
33 #include <linux/kernel.h>
34 #include <linux/slab.h>
35 #include <linux/mm.h>
36 #include <linux/scatterlist.h>
37 #include <linux/kfifo.h>
38 #include <scsi/scsi_cmnd.h>
39 #include <scsi/scsi_host.h>
41 #include "iscsi_iser.h"
43 /* Register user buffer memory and initialize passive rdma
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
48 static int iser_prepare_read_cmd(struct iscsi_task *task)
51 struct iscsi_iser_task *iser_task = task->dd_data;
52 struct iser_mem_reg *mem_reg;
53 int err;
54 struct iser_ctrl *hdr = &iser_task->desc.iser_header;
55 struct iser_data_buf *buf_in = &iser_task->data[ISER_DIR_IN];
57 err = iser_dma_map_task_data(iser_task,
58 buf_in,
59 ISER_DIR_IN,
60 DMA_FROM_DEVICE);
61 if (err)
62 return err;
64 if (scsi_prot_sg_count(iser_task->sc)) {
65 struct iser_data_buf *pbuf_in = &iser_task->prot[ISER_DIR_IN];
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;
75 err = iser_reg_rdma_mem(iser_task, ISER_DIR_IN, false);
76 if (err) {
77 iser_err("Failed to set up Data-IN RDMA\n");
78 return err;
80 mem_reg = &iser_task->rdma_reg[ISER_DIR_IN];
82 hdr->flags |= ISER_RSV;
83 hdr->read_stag = cpu_to_be32(mem_reg->rkey);
84 hdr->read_va = cpu_to_be64(mem_reg->sge.addr);
86 iser_dbg("Cmd itt:%d READ tags RKEY:%#.4X VA:%#llX\n",
87 task->itt, mem_reg->rkey,
88 (unsigned long long)mem_reg->sge.addr);
90 return 0;
93 /* Register user buffer memory and initialize passive rdma
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
98 static int
99 iser_prepare_write_cmd(struct iscsi_task *task,
100 unsigned int imm_sz,
101 unsigned int unsol_sz,
102 unsigned int edtl)
104 struct iscsi_iser_task *iser_task = task->dd_data;
105 struct iser_mem_reg *mem_reg;
106 int err;
107 struct iser_ctrl *hdr = &iser_task->desc.iser_header;
108 struct iser_data_buf *buf_out = &iser_task->data[ISER_DIR_OUT];
109 struct ib_sge *tx_dsg = &iser_task->desc.tx_sg[1];
111 err = iser_dma_map_task_data(iser_task,
112 buf_out,
113 ISER_DIR_OUT,
114 DMA_TO_DEVICE);
115 if (err)
116 return err;
118 if (scsi_prot_sg_count(iser_task->sc)) {
119 struct iser_data_buf *pbuf_out = &iser_task->prot[ISER_DIR_OUT];
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;
129 err = iser_reg_rdma_mem(iser_task, ISER_DIR_OUT,
130 buf_out->data_len == imm_sz);
131 if (err != 0) {
132 iser_err("Failed to register write cmd RDMA mem\n");
133 return err;
136 mem_reg = &iser_task->rdma_reg[ISER_DIR_OUT];
138 if (unsol_sz < edtl) {
139 hdr->flags |= ISER_WSV;
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);
145 iser_dbg("Cmd itt:%d, WRITE tags, RKEY:%#.4X VA:%#llX + unsol:%d\n",
146 task->itt, mem_reg->rkey,
147 (unsigned long long)mem_reg->sge.addr, unsol_sz);
150 if (imm_sz > 0) {
151 iser_dbg("Cmd itt:%d, WRITE, adding imm.data sz: %d\n",
152 task->itt, imm_sz);
153 tx_dsg->addr = mem_reg->sge.addr;
154 tx_dsg->length = imm_sz;
155 tx_dsg->lkey = mem_reg->sge.lkey;
156 iser_task->desc.num_sge = 2;
159 return 0;
162 /* creates a new tx descriptor and adds header regd buffer */
163 static void iser_create_send_desc(struct iser_conn *iser_conn,
164 struct iser_tx_desc *tx_desc)
166 struct iser_device *device = iser_conn->ib_conn.device;
168 ib_dma_sync_single_for_cpu(device->ib_device,
169 tx_desc->dma_addr, ISER_HEADERS_LEN, DMA_TO_DEVICE);
171 memset(&tx_desc->iser_header, 0, sizeof(struct iser_ctrl));
172 tx_desc->iser_header.flags = ISER_VER;
173 tx_desc->num_sge = 1;
176 static void iser_free_login_buf(struct iser_conn *iser_conn)
178 struct iser_device *device = iser_conn->ib_conn.device;
179 struct iser_login_desc *desc = &iser_conn->login_desc;
181 if (!desc->req)
182 return;
184 ib_dma_unmap_single(device->ib_device, desc->req_dma,
185 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_TO_DEVICE);
187 ib_dma_unmap_single(device->ib_device, desc->rsp_dma,
188 ISER_RX_LOGIN_SIZE, DMA_FROM_DEVICE);
190 kfree(desc->req);
191 kfree(desc->rsp);
193 /* make sure we never redo any unmapping */
194 desc->req = NULL;
195 desc->rsp = NULL;
198 static int iser_alloc_login_buf(struct iser_conn *iser_conn)
200 struct iser_device *device = iser_conn->ib_conn.device;
201 struct iser_login_desc *desc = &iser_conn->login_desc;
203 desc->req = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
204 if (!desc->req)
205 return -ENOMEM;
207 desc->req_dma = ib_dma_map_single(device->ib_device, desc->req,
208 ISCSI_DEF_MAX_RECV_SEG_LEN,
209 DMA_TO_DEVICE);
210 if (ib_dma_mapping_error(device->ib_device,
211 desc->req_dma))
212 goto free_req;
214 desc->rsp = kmalloc(ISER_RX_LOGIN_SIZE, GFP_KERNEL);
215 if (!desc->rsp)
216 goto unmap_req;
218 desc->rsp_dma = ib_dma_map_single(device->ib_device, desc->rsp,
219 ISER_RX_LOGIN_SIZE,
220 DMA_FROM_DEVICE);
221 if (ib_dma_mapping_error(device->ib_device,
222 desc->rsp_dma))
223 goto free_rsp;
225 return 0;
227 free_rsp:
228 kfree(desc->rsp);
229 unmap_req:
230 ib_dma_unmap_single(device->ib_device, desc->req_dma,
231 ISCSI_DEF_MAX_RECV_SEG_LEN,
232 DMA_TO_DEVICE);
233 free_req:
234 kfree(desc->req);
236 return -ENOMEM;
239 int iser_alloc_rx_descriptors(struct iser_conn *iser_conn,
240 struct iscsi_session *session)
242 int i, j;
243 u64 dma_addr;
244 struct iser_rx_desc *rx_desc;
245 struct ib_sge *rx_sg;
246 struct ib_conn *ib_conn = &iser_conn->ib_conn;
247 struct iser_device *device = ib_conn->device;
249 iser_conn->qp_max_recv_dtos = session->cmds_max;
250 iser_conn->qp_max_recv_dtos_mask = session->cmds_max - 1; /* cmds_max is 2^N */
251 iser_conn->min_posted_rx = iser_conn->qp_max_recv_dtos >> 2;
253 if (device->reg_ops->alloc_reg_res(ib_conn, session->scsi_cmds_max,
254 iser_conn->scsi_sg_tablesize))
255 goto create_rdma_reg_res_failed;
257 if (iser_alloc_login_buf(iser_conn))
258 goto alloc_login_buf_fail;
260 iser_conn->num_rx_descs = session->cmds_max;
261 iser_conn->rx_descs = kmalloc(iser_conn->num_rx_descs *
262 sizeof(struct iser_rx_desc), GFP_KERNEL);
263 if (!iser_conn->rx_descs)
264 goto rx_desc_alloc_fail;
266 rx_desc = iser_conn->rx_descs;
268 for (i = 0; i < iser_conn->qp_max_recv_dtos; i++, rx_desc++) {
269 dma_addr = ib_dma_map_single(device->ib_device, (void *)rx_desc,
270 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
271 if (ib_dma_mapping_error(device->ib_device, dma_addr))
272 goto rx_desc_dma_map_failed;
274 rx_desc->dma_addr = dma_addr;
275 rx_desc->cqe.done = iser_task_rsp;
276 rx_sg = &rx_desc->rx_sg;
277 rx_sg->addr = rx_desc->dma_addr;
278 rx_sg->length = ISER_RX_PAYLOAD_SIZE;
279 rx_sg->lkey = device->pd->local_dma_lkey;
282 iser_conn->rx_desc_head = 0;
283 return 0;
285 rx_desc_dma_map_failed:
286 rx_desc = iser_conn->rx_descs;
287 for (j = 0; j < i; j++, rx_desc++)
288 ib_dma_unmap_single(device->ib_device, rx_desc->dma_addr,
289 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
290 kfree(iser_conn->rx_descs);
291 iser_conn->rx_descs = NULL;
292 rx_desc_alloc_fail:
293 iser_free_login_buf(iser_conn);
294 alloc_login_buf_fail:
295 device->reg_ops->free_reg_res(ib_conn);
296 create_rdma_reg_res_failed:
297 iser_err("failed allocating rx descriptors / data buffers\n");
298 return -ENOMEM;
301 void iser_free_rx_descriptors(struct iser_conn *iser_conn)
303 int i;
304 struct iser_rx_desc *rx_desc;
305 struct ib_conn *ib_conn = &iser_conn->ib_conn;
306 struct iser_device *device = ib_conn->device;
308 if (device->reg_ops->free_reg_res)
309 device->reg_ops->free_reg_res(ib_conn);
311 rx_desc = iser_conn->rx_descs;
312 for (i = 0; i < iser_conn->qp_max_recv_dtos; i++, rx_desc++)
313 ib_dma_unmap_single(device->ib_device, rx_desc->dma_addr,
314 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
315 kfree(iser_conn->rx_descs);
316 /* make sure we never redo any unmapping */
317 iser_conn->rx_descs = NULL;
319 iser_free_login_buf(iser_conn);
322 static int iser_post_rx_bufs(struct iscsi_conn *conn, struct iscsi_hdr *req)
324 struct iser_conn *iser_conn = conn->dd_data;
325 struct ib_conn *ib_conn = &iser_conn->ib_conn;
326 struct iscsi_session *session = conn->session;
328 iser_dbg("req op %x flags %x\n", req->opcode, req->flags);
329 /* check if this is the last login - going to full feature phase */
330 if ((req->flags & ISCSI_FULL_FEATURE_PHASE) != ISCSI_FULL_FEATURE_PHASE)
331 return 0;
334 * Check that there is one posted recv buffer
335 * (for the last login response).
337 WARN_ON(ib_conn->post_recv_buf_count != 1);
339 if (session->discovery_sess) {
340 iser_info("Discovery session, re-using login RX buffer\n");
341 return 0;
342 } else
343 iser_info("Normal session, posting batch of RX %d buffers\n",
344 iser_conn->min_posted_rx);
346 /* Initial post receive buffers */
347 if (iser_post_recvm(iser_conn, iser_conn->min_posted_rx))
348 return -ENOMEM;
350 return 0;
353 static inline bool iser_signal_comp(u8 sig_count)
355 return ((sig_count % ISER_SIGNAL_CMD_COUNT) == 0);
359 * iser_send_command - send command PDU
361 int iser_send_command(struct iscsi_conn *conn,
362 struct iscsi_task *task)
364 struct iser_conn *iser_conn = conn->dd_data;
365 struct iscsi_iser_task *iser_task = task->dd_data;
366 unsigned long edtl;
367 int err;
368 struct iser_data_buf *data_buf, *prot_buf;
369 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)task->hdr;
370 struct scsi_cmnd *sc = task->sc;
371 struct iser_tx_desc *tx_desc = &iser_task->desc;
372 u8 sig_count = ++iser_conn->ib_conn.sig_count;
374 edtl = ntohl(hdr->data_length);
376 /* build the tx desc regd header and add it to the tx desc dto */
377 tx_desc->type = ISCSI_TX_SCSI_COMMAND;
378 tx_desc->cqe.done = iser_cmd_comp;
379 iser_create_send_desc(iser_conn, tx_desc);
381 if (hdr->flags & ISCSI_FLAG_CMD_READ) {
382 data_buf = &iser_task->data[ISER_DIR_IN];
383 prot_buf = &iser_task->prot[ISER_DIR_IN];
384 } else {
385 data_buf = &iser_task->data[ISER_DIR_OUT];
386 prot_buf = &iser_task->prot[ISER_DIR_OUT];
389 if (scsi_sg_count(sc)) { /* using a scatter list */
390 data_buf->sg = scsi_sglist(sc);
391 data_buf->size = scsi_sg_count(sc);
393 data_buf->data_len = scsi_bufflen(sc);
395 if (scsi_prot_sg_count(sc)) {
396 prot_buf->sg = scsi_prot_sglist(sc);
397 prot_buf->size = scsi_prot_sg_count(sc);
398 prot_buf->data_len = (data_buf->data_len >>
399 ilog2(sc->device->sector_size)) * 8;
402 if (hdr->flags & ISCSI_FLAG_CMD_READ) {
403 err = iser_prepare_read_cmd(task);
404 if (err)
405 goto send_command_error;
407 if (hdr->flags & ISCSI_FLAG_CMD_WRITE) {
408 err = iser_prepare_write_cmd(task,
409 task->imm_count,
410 task->imm_count +
411 task->unsol_r2t.data_length,
412 edtl);
413 if (err)
414 goto send_command_error;
417 iser_task->status = ISER_TASK_STATUS_STARTED;
419 err = iser_post_send(&iser_conn->ib_conn, tx_desc,
420 iser_signal_comp(sig_count));
421 if (!err)
422 return 0;
424 send_command_error:
425 iser_err("conn %p failed task->itt %d err %d\n",conn, task->itt, err);
426 return err;
430 * iser_send_data_out - send data out PDU
432 int iser_send_data_out(struct iscsi_conn *conn,
433 struct iscsi_task *task,
434 struct iscsi_data *hdr)
436 struct iser_conn *iser_conn = conn->dd_data;
437 struct iscsi_iser_task *iser_task = task->dd_data;
438 struct iser_tx_desc *tx_desc;
439 struct iser_mem_reg *mem_reg;
440 unsigned long buf_offset;
441 unsigned long data_seg_len;
442 uint32_t itt;
443 int err;
444 struct ib_sge *tx_dsg;
446 itt = (__force uint32_t)hdr->itt;
447 data_seg_len = ntoh24(hdr->dlength);
448 buf_offset = ntohl(hdr->offset);
450 iser_dbg("%s itt %d dseg_len %d offset %d\n",
451 __func__,(int)itt,(int)data_seg_len,(int)buf_offset);
453 tx_desc = kmem_cache_zalloc(ig.desc_cache, GFP_ATOMIC);
454 if (!tx_desc)
455 return -ENOMEM;
457 tx_desc->type = ISCSI_TX_DATAOUT;
458 tx_desc->cqe.done = iser_dataout_comp;
459 tx_desc->iser_header.flags = ISER_VER;
460 memcpy(&tx_desc->iscsi_header, hdr, sizeof(struct iscsi_hdr));
462 /* build the tx desc */
463 err = iser_initialize_task_headers(task, tx_desc);
464 if (err)
465 goto send_data_out_error;
467 mem_reg = &iser_task->rdma_reg[ISER_DIR_OUT];
468 tx_dsg = &tx_desc->tx_sg[1];
469 tx_dsg->addr = mem_reg->sge.addr + buf_offset;
470 tx_dsg->length = data_seg_len;
471 tx_dsg->lkey = mem_reg->sge.lkey;
472 tx_desc->num_sge = 2;
474 if (buf_offset + data_seg_len > iser_task->data[ISER_DIR_OUT].data_len) {
475 iser_err("Offset:%ld & DSL:%ld in Data-Out inconsistent with total len:%ld, itt:%d\n",
476 buf_offset, data_seg_len,
477 iser_task->data[ISER_DIR_OUT].data_len, itt);
478 err = -EINVAL;
479 goto send_data_out_error;
481 iser_dbg("data-out itt: %d, offset: %ld, sz: %ld\n",
482 itt, buf_offset, data_seg_len);
485 err = iser_post_send(&iser_conn->ib_conn, tx_desc, true);
486 if (!err)
487 return 0;
489 send_data_out_error:
490 kmem_cache_free(ig.desc_cache, tx_desc);
491 iser_err("conn %p failed err %d\n", conn, err);
492 return err;
495 int iser_send_control(struct iscsi_conn *conn,
496 struct iscsi_task *task)
498 struct iser_conn *iser_conn = conn->dd_data;
499 struct iscsi_iser_task *iser_task = task->dd_data;
500 struct iser_tx_desc *mdesc = &iser_task->desc;
501 unsigned long data_seg_len;
502 int err = 0;
503 struct iser_device *device;
505 /* build the tx desc regd header and add it to the tx desc dto */
506 mdesc->type = ISCSI_TX_CONTROL;
507 mdesc->cqe.done = iser_ctrl_comp;
508 iser_create_send_desc(iser_conn, mdesc);
510 device = iser_conn->ib_conn.device;
512 data_seg_len = ntoh24(task->hdr->dlength);
514 if (data_seg_len > 0) {
515 struct iser_login_desc *desc = &iser_conn->login_desc;
516 struct ib_sge *tx_dsg = &mdesc->tx_sg[1];
518 if (task != conn->login_task) {
519 iser_err("data present on non login task!!!\n");
520 goto send_control_error;
523 ib_dma_sync_single_for_cpu(device->ib_device, desc->req_dma,
524 task->data_count, DMA_TO_DEVICE);
526 memcpy(desc->req, task->data, task->data_count);
528 ib_dma_sync_single_for_device(device->ib_device, desc->req_dma,
529 task->data_count, DMA_TO_DEVICE);
531 tx_dsg->addr = desc->req_dma;
532 tx_dsg->length = task->data_count;
533 tx_dsg->lkey = device->pd->local_dma_lkey;
534 mdesc->num_sge = 2;
537 if (task == conn->login_task) {
538 iser_dbg("op %x dsl %lx, posting login rx buffer\n",
539 task->hdr->opcode, data_seg_len);
540 err = iser_post_recvl(iser_conn);
541 if (err)
542 goto send_control_error;
543 err = iser_post_rx_bufs(conn, task->hdr);
544 if (err)
545 goto send_control_error;
548 err = iser_post_send(&iser_conn->ib_conn, mdesc, true);
549 if (!err)
550 return 0;
552 send_control_error:
553 iser_err("conn %p failed err %d\n",conn, err);
554 return err;
557 void iser_login_rsp(struct ib_cq *cq, struct ib_wc *wc)
559 struct ib_conn *ib_conn = wc->qp->qp_context;
560 struct iser_conn *iser_conn = to_iser_conn(ib_conn);
561 struct iser_login_desc *desc = iser_login(wc->wr_cqe);
562 struct iscsi_hdr *hdr;
563 char *data;
564 int length;
566 if (unlikely(wc->status != IB_WC_SUCCESS)) {
567 iser_err_comp(wc, "login_rsp");
568 return;
571 ib_dma_sync_single_for_cpu(ib_conn->device->ib_device,
572 desc->rsp_dma, ISER_RX_LOGIN_SIZE,
573 DMA_FROM_DEVICE);
575 hdr = desc->rsp + sizeof(struct iser_ctrl);
576 data = desc->rsp + ISER_HEADERS_LEN;
577 length = wc->byte_len - ISER_HEADERS_LEN;
579 iser_dbg("op 0x%x itt 0x%x dlen %d\n", hdr->opcode,
580 hdr->itt, length);
582 iscsi_iser_recv(iser_conn->iscsi_conn, hdr, data, length);
584 ib_dma_sync_single_for_device(ib_conn->device->ib_device,
585 desc->rsp_dma, ISER_RX_LOGIN_SIZE,
586 DMA_FROM_DEVICE);
588 ib_conn->post_recv_buf_count--;
591 static inline void
592 iser_inv_desc(struct iser_fr_desc *desc, u32 rkey)
594 if (likely(rkey == desc->rsc.mr->rkey))
595 desc->rsc.mr_valid = 0;
596 else if (likely(rkey == desc->pi_ctx->sig_mr->rkey))
597 desc->pi_ctx->sig_mr_valid = 0;
600 static int
601 iser_check_remote_inv(struct iser_conn *iser_conn,
602 struct ib_wc *wc,
603 struct iscsi_hdr *hdr)
605 if (wc->wc_flags & IB_WC_WITH_INVALIDATE) {
606 struct iscsi_task *task;
607 u32 rkey = wc->ex.invalidate_rkey;
609 iser_dbg("conn %p: remote invalidation for rkey %#x\n",
610 iser_conn, rkey);
612 if (unlikely(!iser_conn->snd_w_inv)) {
613 iser_err("conn %p: unexpected remote invalidation, terminating connection\n",
614 iser_conn);
615 return -EPROTO;
618 task = iscsi_itt_to_ctask(iser_conn->iscsi_conn, hdr->itt);
619 if (likely(task)) {
620 struct iscsi_iser_task *iser_task = task->dd_data;
621 struct iser_fr_desc *desc;
623 if (iser_task->dir[ISER_DIR_IN]) {
624 desc = iser_task->rdma_reg[ISER_DIR_IN].mem_h;
625 iser_inv_desc(desc, rkey);
628 if (iser_task->dir[ISER_DIR_OUT]) {
629 desc = iser_task->rdma_reg[ISER_DIR_OUT].mem_h;
630 iser_inv_desc(desc, rkey);
632 } else {
633 iser_err("failed to get task for itt=%d\n", hdr->itt);
634 return -EINVAL;
638 return 0;
642 void iser_task_rsp(struct ib_cq *cq, struct ib_wc *wc)
644 struct ib_conn *ib_conn = wc->qp->qp_context;
645 struct iser_conn *iser_conn = to_iser_conn(ib_conn);
646 struct iser_rx_desc *desc = iser_rx(wc->wr_cqe);
647 struct iscsi_hdr *hdr;
648 int length;
649 int outstanding, count, err;
651 if (unlikely(wc->status != IB_WC_SUCCESS)) {
652 iser_err_comp(wc, "task_rsp");
653 return;
656 ib_dma_sync_single_for_cpu(ib_conn->device->ib_device,
657 desc->dma_addr, ISER_RX_PAYLOAD_SIZE,
658 DMA_FROM_DEVICE);
660 hdr = &desc->iscsi_header;
661 length = wc->byte_len - ISER_HEADERS_LEN;
663 iser_dbg("op 0x%x itt 0x%x dlen %d\n", hdr->opcode,
664 hdr->itt, length);
666 if (iser_check_remote_inv(iser_conn, wc, hdr)) {
667 iscsi_conn_failure(iser_conn->iscsi_conn,
668 ISCSI_ERR_CONN_FAILED);
669 return;
672 iscsi_iser_recv(iser_conn->iscsi_conn, hdr, desc->data, length);
674 ib_dma_sync_single_for_device(ib_conn->device->ib_device,
675 desc->dma_addr, ISER_RX_PAYLOAD_SIZE,
676 DMA_FROM_DEVICE);
678 /* decrementing conn->post_recv_buf_count only --after-- freeing the *
679 * task eliminates the need to worry on tasks which are completed in *
680 * parallel to the execution of iser_conn_term. So the code that waits *
681 * for the posted rx bufs refcount to become zero handles everything */
682 ib_conn->post_recv_buf_count--;
684 outstanding = ib_conn->post_recv_buf_count;
685 if (outstanding + iser_conn->min_posted_rx <= iser_conn->qp_max_recv_dtos) {
686 count = min(iser_conn->qp_max_recv_dtos - outstanding,
687 iser_conn->min_posted_rx);
688 err = iser_post_recvm(iser_conn, count);
689 if (err)
690 iser_err("posting %d rx bufs err %d\n", count, err);
694 void iser_cmd_comp(struct ib_cq *cq, struct ib_wc *wc)
696 if (unlikely(wc->status != IB_WC_SUCCESS))
697 iser_err_comp(wc, "command");
700 void iser_ctrl_comp(struct ib_cq *cq, struct ib_wc *wc)
702 struct iser_tx_desc *desc = iser_tx(wc->wr_cqe);
703 struct iscsi_task *task;
705 if (unlikely(wc->status != IB_WC_SUCCESS)) {
706 iser_err_comp(wc, "control");
707 return;
710 /* this arithmetic is legal by libiscsi dd_data allocation */
711 task = (void *)desc - sizeof(struct iscsi_task);
712 if (task->hdr->itt == RESERVED_ITT)
713 iscsi_put_task(task);
716 void iser_dataout_comp(struct ib_cq *cq, struct ib_wc *wc)
718 struct iser_tx_desc *desc = iser_tx(wc->wr_cqe);
719 struct ib_conn *ib_conn = wc->qp->qp_context;
720 struct iser_device *device = ib_conn->device;
722 if (unlikely(wc->status != IB_WC_SUCCESS))
723 iser_err_comp(wc, "dataout");
725 ib_dma_unmap_single(device->ib_device, desc->dma_addr,
726 ISER_HEADERS_LEN, DMA_TO_DEVICE);
727 kmem_cache_free(ig.desc_cache, desc);
730 void iser_task_rdma_init(struct iscsi_iser_task *iser_task)
733 iser_task->status = ISER_TASK_STATUS_INIT;
735 iser_task->dir[ISER_DIR_IN] = 0;
736 iser_task->dir[ISER_DIR_OUT] = 0;
738 iser_task->data[ISER_DIR_IN].data_len = 0;
739 iser_task->data[ISER_DIR_OUT].data_len = 0;
741 iser_task->prot[ISER_DIR_IN].data_len = 0;
742 iser_task->prot[ISER_DIR_OUT].data_len = 0;
744 memset(&iser_task->rdma_reg[ISER_DIR_IN], 0,
745 sizeof(struct iser_mem_reg));
746 memset(&iser_task->rdma_reg[ISER_DIR_OUT], 0,
747 sizeof(struct iser_mem_reg));
750 void iser_task_rdma_finalize(struct iscsi_iser_task *iser_task)
752 int prot_count = scsi_prot_sg_count(iser_task->sc);
754 if (iser_task->dir[ISER_DIR_IN]) {
755 iser_unreg_rdma_mem(iser_task, ISER_DIR_IN);
756 iser_dma_unmap_task_data(iser_task,
757 &iser_task->data[ISER_DIR_IN],
758 DMA_FROM_DEVICE);
759 if (prot_count)
760 iser_dma_unmap_task_data(iser_task,
761 &iser_task->prot[ISER_DIR_IN],
762 DMA_FROM_DEVICE);
765 if (iser_task->dir[ISER_DIR_OUT]) {
766 iser_unreg_rdma_mem(iser_task, ISER_DIR_OUT);
767 iser_dma_unmap_task_data(iser_task,
768 &iser_task->data[ISER_DIR_OUT],
769 DMA_TO_DEVICE);
770 if (prot_count)
771 iser_dma_unmap_task_data(iser_task,
772 &iser_task->prot[ISER_DIR_OUT],
773 DMA_TO_DEVICE);