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