]> bbs.cooldavid.org Git - net-next-2.6.git/blobdiff - drivers/infiniband/ulp/iser/iser_verbs.c
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[net-next-2.6.git] / drivers / infiniband / ulp / iser / iser_verbs.c
index 8579f32ce38e6df739bceb270a0ab28623a8f76f..b89d76b39a131873dc4548bdf57e703c4eb09f5e 100644 (file)
  */
 #include <linux/kernel.h>
 #include <linux/module.h>
+#include <linux/slab.h>
 #include <linux/delay.h>
 
 #include "iscsi_iser.h"
 
 #define ISCSI_ISER_MAX_CONN    8
-#define ISER_MAX_CQ_LEN                ((ISER_QP_MAX_RECV_DTOS + \
-                               ISER_QP_MAX_REQ_DTOS) *   \
-                                ISCSI_ISER_MAX_CONN)
+#define ISER_MAX_RX_CQ_LEN     (ISER_QP_MAX_RECV_DTOS * ISCSI_ISER_MAX_CONN)
+#define ISER_MAX_TX_CQ_LEN     (ISER_QP_MAX_REQ_DTOS  * ISCSI_ISER_MAX_CONN)
 
 static void iser_cq_tasklet_fn(unsigned long data);
 static void iser_cq_callback(struct ib_cq *cq, void *cq_context);
@@ -67,15 +67,23 @@ static int iser_create_device_ib_res(struct iser_device *device)
        if (IS_ERR(device->pd))
                goto pd_err;
 
-       device->cq = ib_create_cq(device->ib_device,
+       device->rx_cq = ib_create_cq(device->ib_device,
                                  iser_cq_callback,
                                  iser_cq_event_callback,
                                  (void *)device,
-                                 ISER_MAX_CQ_LEN, 0);
-       if (IS_ERR(device->cq))
-               goto cq_err;
+                                 ISER_MAX_RX_CQ_LEN, 0);
+       if (IS_ERR(device->rx_cq))
+               goto rx_cq_err;
 
-       if (ib_req_notify_cq(device->cq, IB_CQ_NEXT_COMP))
+       device->tx_cq = ib_create_cq(device->ib_device,
+                                 NULL, iser_cq_event_callback,
+                                 (void *)device,
+                                 ISER_MAX_TX_CQ_LEN, 0);
+
+       if (IS_ERR(device->tx_cq))
+               goto tx_cq_err;
+
+       if (ib_req_notify_cq(device->rx_cq, IB_CQ_NEXT_COMP))
                goto cq_arm_err;
 
        tasklet_init(&device->cq_tasklet,
@@ -93,8 +101,10 @@ static int iser_create_device_ib_res(struct iser_device *device)
 dma_mr_err:
        tasklet_kill(&device->cq_tasklet);
 cq_arm_err:
-       ib_destroy_cq(device->cq);
-cq_err:
+       ib_destroy_cq(device->tx_cq);
+tx_cq_err:
+       ib_destroy_cq(device->rx_cq);
+rx_cq_err:
        ib_dealloc_pd(device->pd);
 pd_err:
        iser_err("failed to allocate an IB resource\n");
@@ -112,11 +122,13 @@ static void iser_free_device_ib_res(struct iser_device *device)
        tasklet_kill(&device->cq_tasklet);
 
        (void)ib_dereg_mr(device->mr);
-       (void)ib_destroy_cq(device->cq);
+       (void)ib_destroy_cq(device->tx_cq);
+       (void)ib_destroy_cq(device->rx_cq);
        (void)ib_dealloc_pd(device->pd);
 
        device->mr = NULL;
-       device->cq = NULL;
+       device->tx_cq = NULL;
+       device->rx_cq = NULL;
        device->pd = NULL;
 }
 
@@ -129,13 +141,23 @@ static int iser_create_ib_conn_res(struct iser_conn *ib_conn)
 {
        struct iser_device      *device;
        struct ib_qp_init_attr  init_attr;
-       int                     ret;
+       int                     ret = -ENOMEM;
        struct ib_fmr_pool_param params;
 
        BUG_ON(ib_conn->device == NULL);
 
        device = ib_conn->device;
 
+       ib_conn->login_buf = kmalloc(ISER_RX_LOGIN_SIZE, GFP_KERNEL);
+       if (!ib_conn->login_buf) {
+               goto alloc_err;
+               ret = -ENOMEM;
+       }
+
+       ib_conn->login_dma = ib_dma_map_single(ib_conn->device->ib_device,
+                               (void *)ib_conn->login_buf, ISER_RX_LOGIN_SIZE,
+                               DMA_FROM_DEVICE);
+
        ib_conn->page_vec = kmalloc(sizeof(struct iser_page_vec) +
                                    (sizeof(u64) * (ISCSI_ISER_SG_TABLESIZE +1)),
                                    GFP_KERNEL);
@@ -169,12 +191,12 @@ static int iser_create_ib_conn_res(struct iser_conn *ib_conn)
 
        init_attr.event_handler = iser_qp_event_callback;
        init_attr.qp_context    = (void *)ib_conn;
-       init_attr.send_cq       = device->cq;
-       init_attr.recv_cq       = device->cq;
+       init_attr.send_cq       = device->tx_cq;
+       init_attr.recv_cq       = device->rx_cq;
        init_attr.cap.max_send_wr  = ISER_QP_MAX_REQ_DTOS;
        init_attr.cap.max_recv_wr  = ISER_QP_MAX_RECV_DTOS;
-       init_attr.cap.max_send_sge = MAX_REGD_BUF_VECTOR_LEN;
-       init_attr.cap.max_recv_sge = 2;
+       init_attr.cap.max_send_sge = 2;
+       init_attr.cap.max_recv_sge = 1;
        init_attr.sq_sig_type   = IB_SIGNAL_REQ_WR;
        init_attr.qp_type       = IB_QPT_RC;
 
@@ -192,6 +214,7 @@ qp_err:
        (void)ib_destroy_fmr_pool(ib_conn->fmr_pool);
 fmr_pool_err:
        kfree(ib_conn->page_vec);
+       kfree(ib_conn->login_buf);
 alloc_err:
        iser_err("unable to alloc mem or create resource, err %d\n", ret);
        return ret;
@@ -278,17 +301,6 @@ static void iser_device_try_release(struct iser_device *device)
        mutex_unlock(&ig.device_list_mutex);
 }
 
-int iser_conn_state_comp(struct iser_conn *ib_conn,
-                       enum iser_ib_conn_state comp)
-{
-       int ret;
-
-       spin_lock_bh(&ib_conn->lock);
-       ret = (ib_conn->state == comp);
-       spin_unlock_bh(&ib_conn->lock);
-       return ret;
-}
-
 static int iser_conn_state_comp_exch(struct iser_conn *ib_conn,
                                     enum iser_ib_conn_state comp,
                                     enum iser_ib_conn_state exch)
@@ -314,7 +326,7 @@ static void iser_conn_release(struct iser_conn *ib_conn)
        mutex_lock(&ig.connlist_mutex);
        list_del(&ib_conn->conn_list);
        mutex_unlock(&ig.connlist_mutex);
-
+       iser_free_rx_descriptors(ib_conn);
        iser_free_ib_conn_res(ib_conn);
        ib_conn->device = NULL;
        /* on EVENT_ADDR_ERROR there's no device yet for this conn */
@@ -442,7 +454,7 @@ static void iser_disconnected_handler(struct rdma_cm_id *cma_id)
                                   ISCSI_ERR_CONN_FAILED);
 
        /* Complete the termination process if no posts are pending */
-       if ((atomic_read(&ib_conn->post_recv_buf_count) == 0) &&
+       if (ib_conn->post_recv_buf_count == 0 &&
            (atomic_read(&ib_conn->post_send_buf_count) == 0)) {
                ib_conn->state = ISER_CONN_DOWN;
                wake_up_interruptible(&ib_conn->wait);
@@ -489,9 +501,8 @@ void iser_conn_init(struct iser_conn *ib_conn)
 {
        ib_conn->state = ISER_CONN_INIT;
        init_waitqueue_head(&ib_conn->wait);
-       atomic_set(&ib_conn->post_recv_buf_count, 0);
+       ib_conn->post_recv_buf_count = 0;
        atomic_set(&ib_conn->post_send_buf_count, 0);
-       atomic_set(&ib_conn->unexpected_pdu_count, 0);
        atomic_set(&ib_conn->refcount, 1);
        INIT_LIST_HEAD(&ib_conn->conn_list);
        spin_lock_init(&ib_conn->lock);
@@ -626,136 +637,97 @@ void iser_unreg_mem(struct iser_mem_reg *reg)
        reg->mem_h = NULL;
 }
 
-/**
- * iser_dto_to_iov - builds IOV from a dto descriptor
- */
-static void iser_dto_to_iov(struct iser_dto *dto, struct ib_sge *iov, int iov_len)
+int iser_post_recvl(struct iser_conn *ib_conn)
 {
-       int                  i;
-       struct ib_sge        *sge;
-       struct iser_regd_buf *regd_buf;
-
-       if (dto->regd_vector_len > iov_len) {
-               iser_err("iov size %d too small for posting dto of len %d\n",
-                        iov_len, dto->regd_vector_len);
-               BUG();
-       }
+       struct ib_recv_wr rx_wr, *rx_wr_failed;
+       struct ib_sge     sge;
+       int ib_ret;
 
-       for (i = 0; i < dto->regd_vector_len; i++) {
-               sge         = &iov[i];
-               regd_buf  = dto->regd[i];
-
-               sge->addr   = regd_buf->reg.va;
-               sge->length = regd_buf->reg.len;
-               sge->lkey   = regd_buf->reg.lkey;
-
-               if (dto->used_sz[i] > 0)  /* Adjust size */
-                       sge->length = dto->used_sz[i];
-
-               /* offset and length should not exceed the regd buf length */
-               if (sge->length + dto->offset[i] > regd_buf->reg.len) {
-                       iser_err("Used len:%ld + offset:%d, exceed reg.buf.len:"
-                                "%ld in dto:0x%p [%d], va:0x%08lX\n",
-                                (unsigned long)sge->length, dto->offset[i],
-                                (unsigned long)regd_buf->reg.len, dto, i,
-                                (unsigned long)sge->addr);
-                       BUG();
-               }
+       sge.addr   = ib_conn->login_dma;
+       sge.length = ISER_RX_LOGIN_SIZE;
+       sge.lkey   = ib_conn->device->mr->lkey;
 
-               sge->addr += dto->offset[i]; /* Adjust offset */
+       rx_wr.wr_id   = (unsigned long)ib_conn->login_buf;
+       rx_wr.sg_list = &sge;
+       rx_wr.num_sge = 1;
+       rx_wr.next    = NULL;
+
+       ib_conn->post_recv_buf_count++;
+       ib_ret  = ib_post_recv(ib_conn->qp, &rx_wr, &rx_wr_failed);
+       if (ib_ret) {
+               iser_err("ib_post_recv failed ret=%d\n", ib_ret);
+               ib_conn->post_recv_buf_count--;
        }
+       return ib_ret;
 }
 
-/**
- * iser_post_recv - Posts a receive buffer.
- *
- * returns 0 on success, -1 on failure
- */
-int iser_post_recv(struct iser_desc *rx_desc)
+int iser_post_recvm(struct iser_conn *ib_conn, int count)
 {
-       int               ib_ret, ret_val = 0;
-       struct ib_recv_wr recv_wr, *recv_wr_failed;
-       struct ib_sge     iov[2];
-       struct iser_conn  *ib_conn;
-       struct iser_dto   *recv_dto = &rx_desc->dto;
-
-       /* Retrieve conn */
-       ib_conn = recv_dto->ib_conn;
-
-       iser_dto_to_iov(recv_dto, iov, 2);
+       struct ib_recv_wr *rx_wr, *rx_wr_failed;
+       int i, ib_ret;
+       unsigned int my_rx_head = ib_conn->rx_desc_head;
+       struct iser_rx_desc *rx_desc;
+
+       for (rx_wr = ib_conn->rx_wr, i = 0; i < count; i++, rx_wr++) {
+               rx_desc         = &ib_conn->rx_descs[my_rx_head];
+               rx_wr->wr_id    = (unsigned long)rx_desc;
+               rx_wr->sg_list  = &rx_desc->rx_sg;
+               rx_wr->num_sge  = 1;
+               rx_wr->next     = rx_wr + 1;
+               my_rx_head = (my_rx_head + 1) & (ISER_QP_MAX_RECV_DTOS - 1);
+       }
 
-       recv_wr.next    = NULL;
-       recv_wr.sg_list = iov;
-       recv_wr.num_sge = recv_dto->regd_vector_len;
-       recv_wr.wr_id   = (unsigned long)rx_desc;
+       rx_wr--;
+       rx_wr->next = NULL; /* mark end of work requests list */
 
-       atomic_inc(&ib_conn->post_recv_buf_count);
-       ib_ret  = ib_post_recv(ib_conn->qp, &recv_wr, &recv_wr_failed);
+       ib_conn->post_recv_buf_count += count;
+       ib_ret  = ib_post_recv(ib_conn->qp, ib_conn->rx_wr, &rx_wr_failed);
        if (ib_ret) {
                iser_err("ib_post_recv failed ret=%d\n", ib_ret);
-               atomic_dec(&ib_conn->post_recv_buf_count);
-               ret_val = -1;
-       }
-
-       return ret_val;
+               ib_conn->post_recv_buf_count -= count;
+       } else
+               ib_conn->rx_desc_head = my_rx_head;
+       return ib_ret;
 }
 
+
 /**
  * iser_start_send - Initiate a Send DTO operation
  *
  * returns 0 on success, -1 on failure
  */
-int iser_post_send(struct iser_desc *tx_desc)
+int iser_post_send(struct iser_conn *ib_conn, struct iser_tx_desc *tx_desc)
 {
-       int               ib_ret, ret_val = 0;
+       int               ib_ret;
        struct ib_send_wr send_wr, *send_wr_failed;
-       struct ib_sge     iov[MAX_REGD_BUF_VECTOR_LEN];
-       struct iser_conn  *ib_conn;
-       struct iser_dto   *dto = &tx_desc->dto;
 
-       ib_conn = dto->ib_conn;
-
-       iser_dto_to_iov(dto, iov, MAX_REGD_BUF_VECTOR_LEN);
+       ib_dma_sync_single_for_device(ib_conn->device->ib_device,
+               tx_desc->dma_addr, ISER_HEADERS_LEN, DMA_TO_DEVICE);
 
        send_wr.next       = NULL;
        send_wr.wr_id      = (unsigned long)tx_desc;
-       send_wr.sg_list    = iov;
-       send_wr.num_sge    = dto->regd_vector_len;
+       send_wr.sg_list    = tx_desc->tx_sg;
+       send_wr.num_sge    = tx_desc->num_sge;
        send_wr.opcode     = IB_WR_SEND;
-       send_wr.send_flags = dto->notify_enable ? IB_SEND_SIGNALED : 0;
+       send_wr.send_flags = IB_SEND_SIGNALED;
 
        atomic_inc(&ib_conn->post_send_buf_count);
 
        ib_ret = ib_post_send(ib_conn->qp, &send_wr, &send_wr_failed);
        if (ib_ret) {
-               iser_err("Failed to start SEND DTO, dto: 0x%p, IOV len: %d\n",
-                        dto, dto->regd_vector_len);
                iser_err("ib_post_send failed, ret:%d\n", ib_ret);
                atomic_dec(&ib_conn->post_send_buf_count);
-               ret_val = -1;
        }
-
-       return ret_val;
+       return ib_ret;
 }
 
-static void iser_handle_comp_error(struct iser_desc *desc)
+static void iser_handle_comp_error(struct iser_tx_desc *desc,
+                               struct iser_conn *ib_conn)
 {
-       struct iser_dto  *dto     = &desc->dto;
-       struct iser_conn *ib_conn = dto->ib_conn;
-
-       iser_dto_buffs_release(dto);
-
-       if (desc->type == ISCSI_RX) {
-               kfree(desc->data);
+       if (desc && desc->type == ISCSI_TX_DATAOUT)
                kmem_cache_free(ig.desc_cache, desc);
-               atomic_dec(&ib_conn->post_recv_buf_count);
-       } else { /* type is TX control/command/dataout */
-               if (desc->type == ISCSI_TX_DATAOUT)
-                       kmem_cache_free(ig.desc_cache, desc);
-               atomic_dec(&ib_conn->post_send_buf_count);
-       }
 
-       if (atomic_read(&ib_conn->post_recv_buf_count) == 0 &&
+       if (ib_conn->post_recv_buf_count == 0 &&
            atomic_read(&ib_conn->post_send_buf_count) == 0) {
                /* getting here when the state is UP means that the conn is *
                 * being terminated asynchronously from the iSCSI layer's   *
@@ -774,32 +746,74 @@ static void iser_handle_comp_error(struct iser_desc *desc)
        }
 }
 
+static int iser_drain_tx_cq(struct iser_device  *device)
+{
+       struct ib_cq  *cq = device->tx_cq;
+       struct ib_wc  wc;
+       struct iser_tx_desc *tx_desc;
+       struct iser_conn *ib_conn;
+       int completed_tx = 0;
+
+       while (ib_poll_cq(cq, 1, &wc) == 1) {
+               tx_desc = (struct iser_tx_desc *) (unsigned long) wc.wr_id;
+               ib_conn = wc.qp->qp_context;
+               if (wc.status == IB_WC_SUCCESS) {
+                       if (wc.opcode == IB_WC_SEND)
+                               iser_snd_completion(tx_desc, ib_conn);
+                       else
+                               iser_err("expected opcode %d got %d\n",
+                                       IB_WC_SEND, wc.opcode);
+               } else {
+                       iser_err("tx id %llx status %d vend_err %x\n",
+                               wc.wr_id, wc.status, wc.vendor_err);
+                       atomic_dec(&ib_conn->post_send_buf_count);
+                       iser_handle_comp_error(tx_desc, ib_conn);
+               }
+               completed_tx++;
+       }
+       return completed_tx;
+}
+
+
 static void iser_cq_tasklet_fn(unsigned long data)
 {
         struct iser_device  *device = (struct iser_device *)data;
-        struct ib_cq        *cq = device->cq;
+        struct ib_cq        *cq = device->rx_cq;
         struct ib_wc        wc;
-        struct iser_desc    *desc;
+        struct iser_rx_desc *desc;
         unsigned long       xfer_len;
+       struct iser_conn *ib_conn;
+       int completed_tx, completed_rx;
+       completed_tx = completed_rx = 0;
 
        while (ib_poll_cq(cq, 1, &wc) == 1) {
-               desc     = (struct iser_desc *) (unsigned long) wc.wr_id;
+               desc     = (struct iser_rx_desc *) (unsigned long) wc.wr_id;
                BUG_ON(desc == NULL);
-
+               ib_conn = wc.qp->qp_context;
                if (wc.status == IB_WC_SUCCESS) {
-                       if (desc->type == ISCSI_RX) {
+                       if (wc.opcode == IB_WC_RECV) {
                                xfer_len = (unsigned long)wc.byte_len;
-                               iser_rcv_completion(desc, xfer_len);
-                       } else /* type == ISCSI_TX_CONTROL/SCSI_CMD/DOUT */
-                               iser_snd_completion(desc);
+                               iser_rcv_completion(desc, xfer_len, ib_conn);
+                       } else
+                               iser_err("expected opcode %d got %d\n",
+                                       IB_WC_RECV, wc.opcode);
                } else {
-                       iser_err("comp w. error op %d status %d\n",desc->type,wc.status);
-                       iser_handle_comp_error(desc);
+                       if (wc.status != IB_WC_WR_FLUSH_ERR)
+                               iser_err("rx id %llx status %d vend_err %x\n",
+                                       wc.wr_id, wc.status, wc.vendor_err);
+                       ib_conn->post_recv_buf_count--;
+                       iser_handle_comp_error(NULL, ib_conn);
                }
+               completed_rx++;
+               if (!(completed_rx & 63))
+                       completed_tx += iser_drain_tx_cq(device);
        }
        /* #warning "it is assumed here that arming CQ only once its empty" *
         * " would not cause interrupts to be missed"                       */
        ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
+
+       completed_tx += iser_drain_tx_cq(device);
+       iser_dbg("got %d rx %d tx completions\n", completed_rx, completed_tx);
 }
 
 static void iser_cq_callback(struct ib_cq *cq, void *cq_context)