Merge branch 'i2c-for-linus' of git://jdelvare.pck.nerim.net/jdelvare-2.6
[linux-2.6] / drivers / infiniband / ulp / iser / iser_initiator.c
1 /*
2  * Copyright (c) 2004, 2005, 2006 Voltaire, Inc. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32 #include <linux/kernel.h>
33 #include <linux/slab.h>
34 #include <linux/mm.h>
35 #include <linux/scatterlist.h>
36 #include <linux/kfifo.h>
37 #include <scsi/scsi_cmnd.h>
38 #include <scsi/scsi_host.h>
39
40 #include "iscsi_iser.h"
41
42 /* Constant PDU lengths calculations */
43 #define ISER_TOTAL_HEADERS_LEN  (sizeof (struct iser_hdr) + \
44                                  sizeof (struct iscsi_hdr))
45
46 /* iser_dto_add_regd_buff - increments the reference count for *
47  * the registered buffer & adds it to the DTO object           */
48 static void iser_dto_add_regd_buff(struct iser_dto *dto,
49                                    struct iser_regd_buf *regd_buf,
50                                    unsigned long use_offset,
51                                    unsigned long use_size)
52 {
53         int add_idx;
54
55         atomic_inc(&regd_buf->ref_count);
56
57         add_idx = dto->regd_vector_len;
58         dto->regd[add_idx] = regd_buf;
59         dto->used_sz[add_idx] = use_size;
60         dto->offset[add_idx] = use_offset;
61
62         dto->regd_vector_len++;
63 }
64
65 /* Register user buffer memory and initialize passive rdma
66  *  dto descriptor. Total data size is stored in
67  *  iser_ctask->data[ISER_DIR_IN].data_len
68  */
69 static int iser_prepare_read_cmd(struct iscsi_cmd_task *ctask,
70                                  unsigned int edtl)
71
72 {
73         struct iscsi_iser_cmd_task *iser_ctask = ctask->dd_data;
74         struct iser_regd_buf *regd_buf;
75         int err;
76         struct iser_hdr *hdr = &iser_ctask->desc.iser_header;
77         struct iser_data_buf *buf_in = &iser_ctask->data[ISER_DIR_IN];
78
79         err = iser_dma_map_task_data(iser_ctask,
80                                      buf_in,
81                                      ISER_DIR_IN,
82                                      DMA_FROM_DEVICE);
83         if (err)
84                 return err;
85
86         if (edtl > iser_ctask->data[ISER_DIR_IN].data_len) {
87                 iser_err("Total data length: %ld, less than EDTL: "
88                          "%d, in READ cmd BHS itt: %d, conn: 0x%p\n",
89                          iser_ctask->data[ISER_DIR_IN].data_len, edtl,
90                          ctask->itt, iser_ctask->iser_conn);
91                 return -EINVAL;
92         }
93
94         err = iser_reg_rdma_mem(iser_ctask,ISER_DIR_IN);
95         if (err) {
96                 iser_err("Failed to set up Data-IN RDMA\n");
97                 return err;
98         }
99         regd_buf = &iser_ctask->rdma_regd[ISER_DIR_IN];
100
101         hdr->flags    |= ISER_RSV;
102         hdr->read_stag = cpu_to_be32(regd_buf->reg.rkey);
103         hdr->read_va   = cpu_to_be64(regd_buf->reg.va);
104
105         iser_dbg("Cmd itt:%d READ tags RKEY:%#.4X VA:%#llX\n",
106                  ctask->itt, regd_buf->reg.rkey,
107                  (unsigned long long)regd_buf->reg.va);
108
109         return 0;
110 }
111
112 /* Register user buffer memory and initialize passive rdma
113  *  dto descriptor. Total data size is stored in
114  *  ctask->data[ISER_DIR_OUT].data_len
115  */
116 static int
117 iser_prepare_write_cmd(struct iscsi_cmd_task *ctask,
118                        unsigned int imm_sz,
119                        unsigned int unsol_sz,
120                        unsigned int edtl)
121 {
122         struct iscsi_iser_cmd_task *iser_ctask = ctask->dd_data;
123         struct iser_regd_buf *regd_buf;
124         int err;
125         struct iser_dto *send_dto = &iser_ctask->desc.dto;
126         struct iser_hdr *hdr = &iser_ctask->desc.iser_header;
127         struct iser_data_buf *buf_out = &iser_ctask->data[ISER_DIR_OUT];
128
129         err = iser_dma_map_task_data(iser_ctask,
130                                      buf_out,
131                                      ISER_DIR_OUT,
132                                      DMA_TO_DEVICE);
133         if (err)
134                 return err;
135
136         if (edtl > iser_ctask->data[ISER_DIR_OUT].data_len) {
137                 iser_err("Total data length: %ld, less than EDTL: %d, "
138                          "in WRITE cmd BHS itt: %d, conn: 0x%p\n",
139                          iser_ctask->data[ISER_DIR_OUT].data_len,
140                          edtl, ctask->itt, ctask->conn);
141                 return -EINVAL;
142         }
143
144         err = iser_reg_rdma_mem(iser_ctask,ISER_DIR_OUT);
145         if (err != 0) {
146                 iser_err("Failed to register write cmd RDMA mem\n");
147                 return err;
148         }
149
150         regd_buf = &iser_ctask->rdma_regd[ISER_DIR_OUT];
151
152         if (unsol_sz < edtl) {
153                 hdr->flags     |= ISER_WSV;
154                 hdr->write_stag = cpu_to_be32(regd_buf->reg.rkey);
155                 hdr->write_va   = cpu_to_be64(regd_buf->reg.va + unsol_sz);
156
157                 iser_dbg("Cmd itt:%d, WRITE tags, RKEY:%#.4X "
158                          "VA:%#llX + unsol:%d\n",
159                          ctask->itt, regd_buf->reg.rkey,
160                          (unsigned long long)regd_buf->reg.va, unsol_sz);
161         }
162
163         if (imm_sz > 0) {
164                 iser_dbg("Cmd itt:%d, WRITE, adding imm.data sz: %d\n",
165                          ctask->itt, imm_sz);
166                 iser_dto_add_regd_buff(send_dto,
167                                        regd_buf,
168                                        0,
169                                        imm_sz);
170         }
171
172         return 0;
173 }
174
175 /**
176  * iser_post_receive_control - allocates, initializes and posts receive DTO.
177  */
178 static int iser_post_receive_control(struct iscsi_conn *conn)
179 {
180         struct iscsi_iser_conn *iser_conn = conn->dd_data;
181         struct iser_desc     *rx_desc;
182         struct iser_regd_buf *regd_hdr;
183         struct iser_regd_buf *regd_data;
184         struct iser_dto      *recv_dto = NULL;
185         struct iser_device  *device = iser_conn->ib_conn->device;
186         int rx_data_size, err = 0;
187
188         rx_desc = kmem_cache_alloc(ig.desc_cache, GFP_NOIO);
189         if (rx_desc == NULL) {
190                 iser_err("Failed to alloc desc for post recv\n");
191                 return -ENOMEM;
192         }
193         rx_desc->type = ISCSI_RX;
194
195         /* for the login sequence we must support rx of upto 8K; login is done
196          * after conn create/bind (connect) and conn stop/bind (reconnect),
197          * what's common for both schemes is that the connection is not started
198          */
199         if (conn->c_stage != ISCSI_CONN_STARTED)
200                 rx_data_size = ISCSI_DEF_MAX_RECV_SEG_LEN;
201         else /* FIXME till user space sets conn->max_recv_dlength correctly */
202                 rx_data_size = 128;
203
204         rx_desc->data = kmalloc(rx_data_size, GFP_NOIO);
205         if (rx_desc->data == NULL) {
206                 iser_err("Failed to alloc data buf for post recv\n");
207                 err = -ENOMEM;
208                 goto post_rx_kmalloc_failure;
209         }
210
211         recv_dto = &rx_desc->dto;
212         recv_dto->ib_conn = iser_conn->ib_conn;
213         recv_dto->regd_vector_len = 0;
214
215         regd_hdr = &rx_desc->hdr_regd_buf;
216         memset(regd_hdr, 0, sizeof(struct iser_regd_buf));
217         regd_hdr->device  = device;
218         regd_hdr->virt_addr  = rx_desc; /* == &rx_desc->iser_header */
219         regd_hdr->data_size  = ISER_TOTAL_HEADERS_LEN;
220
221         iser_reg_single(device, regd_hdr, DMA_FROM_DEVICE);
222
223         iser_dto_add_regd_buff(recv_dto, regd_hdr, 0, 0);
224
225         regd_data = &rx_desc->data_regd_buf;
226         memset(regd_data, 0, sizeof(struct iser_regd_buf));
227         regd_data->device  = device;
228         regd_data->virt_addr  = rx_desc->data;
229         regd_data->data_size  = rx_data_size;
230
231         iser_reg_single(device, regd_data, DMA_FROM_DEVICE);
232
233         iser_dto_add_regd_buff(recv_dto, regd_data, 0, 0);
234
235         err = iser_post_recv(rx_desc);
236         if (!err)
237                 return 0;
238
239         /* iser_post_recv failed */
240         iser_dto_buffs_release(recv_dto);
241         kfree(rx_desc->data);
242 post_rx_kmalloc_failure:
243         kmem_cache_free(ig.desc_cache, rx_desc);
244         return err;
245 }
246
247 /* creates a new tx descriptor and adds header regd buffer */
248 static void iser_create_send_desc(struct iscsi_iser_conn *iser_conn,
249                                   struct iser_desc       *tx_desc)
250 {
251         struct iser_regd_buf *regd_hdr = &tx_desc->hdr_regd_buf;
252         struct iser_dto      *send_dto = &tx_desc->dto;
253
254         memset(regd_hdr, 0, sizeof(struct iser_regd_buf));
255         regd_hdr->device  = iser_conn->ib_conn->device;
256         regd_hdr->virt_addr  = tx_desc; /* == &tx_desc->iser_header */
257         regd_hdr->data_size  = ISER_TOTAL_HEADERS_LEN;
258
259         send_dto->ib_conn         = iser_conn->ib_conn;
260         send_dto->notify_enable   = 1;
261         send_dto->regd_vector_len = 0;
262
263         memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr));
264         tx_desc->iser_header.flags = ISER_VER;
265
266         iser_dto_add_regd_buff(send_dto, regd_hdr, 0, 0);
267 }
268
269 /**
270  *  iser_conn_set_full_featured_mode - (iSER API)
271  */
272 int iser_conn_set_full_featured_mode(struct iscsi_conn *conn)
273 {
274         struct iscsi_iser_conn *iser_conn = conn->dd_data;
275
276         int i;
277         /* no need to keep it in a var, we are after login so if this should
278          * be negotiated, by now the result should be available here */
279         int initial_post_recv_bufs_num = ISER_MAX_RX_MISC_PDUS;
280
281         iser_dbg("Initially post: %d\n", initial_post_recv_bufs_num);
282
283         /* Check that there is no posted recv or send buffers left - */
284         /* they must be consumed during the login phase */
285         BUG_ON(atomic_read(&iser_conn->ib_conn->post_recv_buf_count) != 0);
286         BUG_ON(atomic_read(&iser_conn->ib_conn->post_send_buf_count) != 0);
287
288         /* Initial post receive buffers */
289         for (i = 0; i < initial_post_recv_bufs_num; i++) {
290                 if (iser_post_receive_control(conn) != 0) {
291                         iser_err("Failed to post recv bufs at:%d conn:0x%p\n",
292                                  i, conn);
293                         return -ENOMEM;
294                 }
295         }
296         iser_dbg("Posted %d post recv bufs, conn:0x%p\n", i, conn);
297         return 0;
298 }
299
300 static int
301 iser_check_xmit(struct iscsi_conn *conn, void *task)
302 {
303         struct iscsi_iser_conn *iser_conn = conn->dd_data;
304
305         if (atomic_read(&iser_conn->ib_conn->post_send_buf_count) ==
306             ISER_QP_MAX_REQ_DTOS) {
307                 iser_dbg("%ld can't xmit task %p\n",jiffies,task);
308                 return -ENOBUFS;
309         }
310         return 0;
311 }
312
313
314 /**
315  * iser_send_command - send command PDU
316  */
317 int iser_send_command(struct iscsi_conn     *conn,
318                       struct iscsi_cmd_task *ctask)
319 {
320         struct iscsi_iser_conn *iser_conn = conn->dd_data;
321         struct iscsi_iser_cmd_task *iser_ctask = ctask->dd_data;
322         struct iser_dto *send_dto = NULL;
323         unsigned long edtl;
324         int err = 0;
325         struct iser_data_buf *data_buf;
326
327         struct iscsi_cmd *hdr =  ctask->hdr;
328         struct scsi_cmnd *sc  =  ctask->sc;
329
330         if (!iser_conn_state_comp(iser_conn->ib_conn, ISER_CONN_UP)) {
331                 iser_err("Failed to send, conn: 0x%p is not up\n", iser_conn->ib_conn);
332                 return -EPERM;
333         }
334         if (iser_check_xmit(conn, ctask))
335                 return -ENOBUFS;
336
337         edtl = ntohl(hdr->data_length);
338
339         /* build the tx desc regd header and add it to the tx desc dto */
340         iser_ctask->desc.type = ISCSI_TX_SCSI_COMMAND;
341         send_dto = &iser_ctask->desc.dto;
342         send_dto->ctask = iser_ctask;
343         iser_create_send_desc(iser_conn, &iser_ctask->desc);
344
345         if (hdr->flags & ISCSI_FLAG_CMD_READ)
346                 data_buf = &iser_ctask->data[ISER_DIR_IN];
347         else
348                 data_buf = &iser_ctask->data[ISER_DIR_OUT];
349
350         if (scsi_sg_count(sc)) { /* using a scatter list */
351                 data_buf->buf  = scsi_sglist(sc);
352                 data_buf->size = scsi_sg_count(sc);
353         }
354
355         data_buf->data_len = scsi_bufflen(sc);
356
357         if (hdr->flags & ISCSI_FLAG_CMD_READ) {
358                 err = iser_prepare_read_cmd(ctask, edtl);
359                 if (err)
360                         goto send_command_error;
361         }
362         if (hdr->flags & ISCSI_FLAG_CMD_WRITE) {
363                 err = iser_prepare_write_cmd(ctask,
364                                              ctask->imm_count,
365                                              ctask->imm_count +
366                                              ctask->unsol_count,
367                                              edtl);
368                 if (err)
369                         goto send_command_error;
370         }
371
372         iser_reg_single(iser_conn->ib_conn->device,
373                         send_dto->regd[0], DMA_TO_DEVICE);
374
375         if (iser_post_receive_control(conn) != 0) {
376                 iser_err("post_recv failed!\n");
377                 err = -ENOMEM;
378                 goto send_command_error;
379         }
380
381         iser_ctask->status = ISER_TASK_STATUS_STARTED;
382
383         err = iser_post_send(&iser_ctask->desc);
384         if (!err)
385                 return 0;
386
387 send_command_error:
388         iser_dto_buffs_release(send_dto);
389         iser_err("conn %p failed ctask->itt %d err %d\n",conn, ctask->itt, err);
390         return err;
391 }
392
393 /**
394  * iser_send_data_out - send data out PDU
395  */
396 int iser_send_data_out(struct iscsi_conn     *conn,
397                        struct iscsi_cmd_task *ctask,
398                        struct iscsi_data *hdr)
399 {
400         struct iscsi_iser_conn *iser_conn = conn->dd_data;
401         struct iscsi_iser_cmd_task *iser_ctask = ctask->dd_data;
402         struct iser_desc *tx_desc = NULL;
403         struct iser_dto *send_dto = NULL;
404         unsigned long buf_offset;
405         unsigned long data_seg_len;
406         uint32_t itt;
407         int err = 0;
408
409         if (!iser_conn_state_comp(iser_conn->ib_conn, ISER_CONN_UP)) {
410                 iser_err("Failed to send, conn: 0x%p is not up\n", iser_conn->ib_conn);
411                 return -EPERM;
412         }
413
414         if (iser_check_xmit(conn, ctask))
415                 return -ENOBUFS;
416
417         itt = (__force uint32_t)hdr->itt;
418         data_seg_len = ntoh24(hdr->dlength);
419         buf_offset   = ntohl(hdr->offset);
420
421         iser_dbg("%s itt %d dseg_len %d offset %d\n",
422                  __func__,(int)itt,(int)data_seg_len,(int)buf_offset);
423
424         tx_desc = kmem_cache_alloc(ig.desc_cache, GFP_NOIO);
425         if (tx_desc == NULL) {
426                 iser_err("Failed to alloc desc for post dataout\n");
427                 return -ENOMEM;
428         }
429
430         tx_desc->type = ISCSI_TX_DATAOUT;
431         memcpy(&tx_desc->iscsi_header, hdr, sizeof(struct iscsi_hdr));
432
433         /* build the tx desc regd header and add it to the tx desc dto */
434         send_dto = &tx_desc->dto;
435         send_dto->ctask = iser_ctask;
436         iser_create_send_desc(iser_conn, tx_desc);
437
438         iser_reg_single(iser_conn->ib_conn->device,
439                         send_dto->regd[0], DMA_TO_DEVICE);
440
441         /* all data was registered for RDMA, we can use the lkey */
442         iser_dto_add_regd_buff(send_dto,
443                                &iser_ctask->rdma_regd[ISER_DIR_OUT],
444                                buf_offset,
445                                data_seg_len);
446
447         if (buf_offset + data_seg_len > iser_ctask->data[ISER_DIR_OUT].data_len) {
448                 iser_err("Offset:%ld & DSL:%ld in Data-Out "
449                          "inconsistent with total len:%ld, itt:%d\n",
450                          buf_offset, data_seg_len,
451                          iser_ctask->data[ISER_DIR_OUT].data_len, itt);
452                 err = -EINVAL;
453                 goto send_data_out_error;
454         }
455         iser_dbg("data-out itt: %d, offset: %ld, sz: %ld\n",
456                  itt, buf_offset, data_seg_len);
457
458
459         err = iser_post_send(tx_desc);
460         if (!err)
461                 return 0;
462
463 send_data_out_error:
464         iser_dto_buffs_release(send_dto);
465         kmem_cache_free(ig.desc_cache, tx_desc);
466         iser_err("conn %p failed err %d\n",conn, err);
467         return err;
468 }
469
470 int iser_send_control(struct iscsi_conn *conn,
471                       struct iscsi_mgmt_task *mtask)
472 {
473         struct iscsi_iser_conn *iser_conn = conn->dd_data;
474         struct iser_desc *mdesc = mtask->dd_data;
475         struct iser_dto *send_dto = NULL;
476         unsigned long data_seg_len;
477         int err = 0;
478         struct iser_regd_buf *regd_buf;
479         struct iser_device *device;
480
481         if (!iser_conn_state_comp(iser_conn->ib_conn, ISER_CONN_UP)) {
482                 iser_err("Failed to send, conn: 0x%p is not up\n", iser_conn->ib_conn);
483                 return -EPERM;
484         }
485
486         if (iser_check_xmit(conn,mtask))
487                 return -ENOBUFS;
488
489         /* build the tx desc regd header and add it to the tx desc dto */
490         mdesc->type = ISCSI_TX_CONTROL;
491         send_dto = &mdesc->dto;
492         send_dto->ctask = NULL;
493         iser_create_send_desc(iser_conn, mdesc);
494
495         device = iser_conn->ib_conn->device;
496
497         iser_reg_single(device, send_dto->regd[0], DMA_TO_DEVICE);
498
499         data_seg_len = ntoh24(mtask->hdr->dlength);
500
501         if (data_seg_len > 0) {
502                 regd_buf = &mdesc->data_regd_buf;
503                 memset(regd_buf, 0, sizeof(struct iser_regd_buf));
504                 regd_buf->device = device;
505                 regd_buf->virt_addr = mtask->data;
506                 regd_buf->data_size = mtask->data_count;
507                 iser_reg_single(device, regd_buf,
508                                 DMA_TO_DEVICE);
509                 iser_dto_add_regd_buff(send_dto, regd_buf,
510                                        0,
511                                        data_seg_len);
512         }
513
514         if (iser_post_receive_control(conn) != 0) {
515                 iser_err("post_rcv_buff failed!\n");
516                 err = -ENOMEM;
517                 goto send_control_error;
518         }
519
520         err = iser_post_send(mdesc);
521         if (!err)
522                 return 0;
523
524 send_control_error:
525         iser_dto_buffs_release(send_dto);
526         iser_err("conn %p failed err %d\n",conn, err);
527         return err;
528 }
529
530 /**
531  * iser_rcv_dto_completion - recv DTO completion
532  */
533 void iser_rcv_completion(struct iser_desc *rx_desc,
534                          unsigned long dto_xfer_len)
535 {
536         struct iser_dto        *dto = &rx_desc->dto;
537         struct iscsi_iser_conn *conn = dto->ib_conn->iser_conn;
538         struct iscsi_session *session = conn->iscsi_conn->session;
539         struct iscsi_cmd_task *ctask;
540         struct iscsi_iser_cmd_task *iser_ctask;
541         struct iscsi_hdr *hdr;
542         char   *rx_data = NULL;
543         int     rx_data_len = 0;
544         unsigned int itt;
545         unsigned char opcode;
546
547         hdr = &rx_desc->iscsi_header;
548
549         iser_dbg("op 0x%x itt 0x%x\n", hdr->opcode,hdr->itt);
550
551         if (dto_xfer_len > ISER_TOTAL_HEADERS_LEN) { /* we have data */
552                 rx_data_len = dto_xfer_len - ISER_TOTAL_HEADERS_LEN;
553                 rx_data     = dto->regd[1]->virt_addr;
554                 rx_data    += dto->offset[1];
555         }
556
557         opcode = hdr->opcode & ISCSI_OPCODE_MASK;
558
559         if (opcode == ISCSI_OP_SCSI_CMD_RSP) {
560                 itt = get_itt(hdr->itt); /* mask out cid and age bits */
561                 if (!(itt < session->cmds_max))
562                         iser_err("itt can't be matched to task!!! "
563                                  "conn %p opcode %d cmds_max %d itt %d\n",
564                                  conn->iscsi_conn,opcode,session->cmds_max,itt);
565                 /* use the mapping given with the cmds array indexed by itt */
566                 ctask = (struct iscsi_cmd_task *)session->cmds[itt];
567                 iser_ctask = ctask->dd_data;
568                 iser_dbg("itt %d ctask %p\n",itt,ctask);
569                 iser_ctask->status = ISER_TASK_STATUS_COMPLETED;
570                 iser_ctask_rdma_finalize(iser_ctask);
571         }
572
573         iser_dto_buffs_release(dto);
574
575         iscsi_iser_recv(conn->iscsi_conn, hdr, rx_data, rx_data_len);
576
577         kfree(rx_desc->data);
578         kmem_cache_free(ig.desc_cache, rx_desc);
579
580         /* decrementing conn->post_recv_buf_count only --after-- freeing the   *
581          * task eliminates the need to worry on tasks which are completed in   *
582          * parallel to the execution of iser_conn_term. So the code that waits *
583          * for the posted rx bufs refcount to become zero handles everything   */
584         atomic_dec(&conn->ib_conn->post_recv_buf_count);
585 }
586
587 void iser_snd_completion(struct iser_desc *tx_desc)
588 {
589         struct iser_dto        *dto = &tx_desc->dto;
590         struct iser_conn       *ib_conn = dto->ib_conn;
591         struct iscsi_iser_conn *iser_conn = ib_conn->iser_conn;
592         struct iscsi_conn      *conn = iser_conn->iscsi_conn;
593         struct iscsi_mgmt_task *mtask;
594         int resume_tx = 0;
595
596         iser_dbg("Initiator, Data sent dto=0x%p\n", dto);
597
598         iser_dto_buffs_release(dto);
599
600         if (tx_desc->type == ISCSI_TX_DATAOUT)
601                 kmem_cache_free(ig.desc_cache, tx_desc);
602
603         if (atomic_read(&iser_conn->ib_conn->post_send_buf_count) ==
604             ISER_QP_MAX_REQ_DTOS)
605                 resume_tx = 1;
606
607         atomic_dec(&ib_conn->post_send_buf_count);
608
609         if (resume_tx) {
610                 iser_dbg("%ld resuming tx\n",jiffies);
611                 scsi_queue_work(conn->session->host, &conn->xmitwork);
612         }
613
614         if (tx_desc->type == ISCSI_TX_CONTROL) {
615                 /* this arithmetic is legal by libiscsi dd_data allocation */
616                 mtask = (void *) ((long)(void *)tx_desc -
617                                   sizeof(struct iscsi_mgmt_task));
618                 if (mtask->hdr->itt == RESERVED_ITT) {
619                         struct iscsi_session *session = conn->session;
620
621                         spin_lock(&conn->session->lock);
622                         iscsi_free_mgmt_task(conn, mtask);
623                         spin_unlock(&session->lock);
624                 }
625         }
626 }
627
628 void iser_ctask_rdma_init(struct iscsi_iser_cmd_task *iser_ctask)
629
630 {
631         iser_ctask->status = ISER_TASK_STATUS_INIT;
632
633         iser_ctask->dir[ISER_DIR_IN] = 0;
634         iser_ctask->dir[ISER_DIR_OUT] = 0;
635
636         iser_ctask->data[ISER_DIR_IN].data_len  = 0;
637         iser_ctask->data[ISER_DIR_OUT].data_len = 0;
638
639         memset(&iser_ctask->rdma_regd[ISER_DIR_IN], 0,
640                sizeof(struct iser_regd_buf));
641         memset(&iser_ctask->rdma_regd[ISER_DIR_OUT], 0,
642                sizeof(struct iser_regd_buf));
643 }
644
645 void iser_ctask_rdma_finalize(struct iscsi_iser_cmd_task *iser_ctask)
646 {
647         int deferred;
648         int is_rdma_aligned = 1;
649         struct iser_regd_buf *regd;
650
651         /* if we were reading, copy back to unaligned sglist,
652          * anyway dma_unmap and free the copy
653          */
654         if (iser_ctask->data_copy[ISER_DIR_IN].copy_buf != NULL) {
655                 is_rdma_aligned = 0;
656                 iser_finalize_rdma_unaligned_sg(iser_ctask, ISER_DIR_IN);
657         }
658         if (iser_ctask->data_copy[ISER_DIR_OUT].copy_buf != NULL) {
659                 is_rdma_aligned = 0;
660                 iser_finalize_rdma_unaligned_sg(iser_ctask, ISER_DIR_OUT);
661         }
662
663         if (iser_ctask->dir[ISER_DIR_IN]) {
664                 regd = &iser_ctask->rdma_regd[ISER_DIR_IN];
665                 deferred = iser_regd_buff_release(regd);
666                 if (deferred) {
667                         iser_err("%d references remain for BUF-IN rdma reg\n",
668                                  atomic_read(&regd->ref_count));
669                 }
670         }
671
672         if (iser_ctask->dir[ISER_DIR_OUT]) {
673                 regd = &iser_ctask->rdma_regd[ISER_DIR_OUT];
674                 deferred = iser_regd_buff_release(regd);
675                 if (deferred) {
676                         iser_err("%d references remain for BUF-OUT rdma reg\n",
677                                  atomic_read(&regd->ref_count));
678                 }
679         }
680
681        /* if the data was unaligned, it was already unmapped and then copied */
682        if (is_rdma_aligned)
683                 iser_dma_unmap_task_data(iser_ctask);
684 }
685
686 void iser_dto_buffs_release(struct iser_dto *dto)
687 {
688         int i;
689
690         for (i = 0; i < dto->regd_vector_len; i++)
691                 iser_regd_buff_release(dto->regd[i]);
692 }
693