include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[safe/jmp/linux-2.6] / drivers / infiniband / hw / cxgb3 / iwch_cm.c
index 0b515d8..4fef032 100644 (file)
@@ -31,6 +31,7 @@
  */
 #include <linux/module.h>
 #include <linux/list.h>
+#include <linux/slab.h>
 #include <linux/workqueue.h>
 #include <linux/skbuff.h>
 #include <linux/timer.h>
@@ -63,10 +64,14 @@ static char *states[] = {
        NULL,
 };
 
-static int ep_timeout_secs = 10;
+int peer2peer = 0;
+module_param(peer2peer, int, 0644);
+MODULE_PARM_DESC(peer2peer, "Support peer2peer ULPs (default=0)");
+
+static int ep_timeout_secs = 60;
 module_param(ep_timeout_secs, int, 0644);
 MODULE_PARM_DESC(ep_timeout_secs, "CM Endpoint operation timeout "
-                                  "in seconds (default=10)");
+                                  "in seconds (default=60)");
 
 static int mpa_rev = 1;
 module_param(mpa_rev, int, 0644);
@@ -135,6 +140,38 @@ static void stop_ep_timer(struct iwch_ep *ep)
        put_ep(&ep->com);
 }
 
+int iwch_l2t_send(struct t3cdev *tdev, struct sk_buff *skb, struct l2t_entry *l2e)
+{
+       int     error = 0;
+       struct cxio_rdev *rdev;
+
+       rdev = (struct cxio_rdev *)tdev->ulp;
+       if (cxio_fatal_error(rdev)) {
+               kfree_skb(skb);
+               return -EIO;
+       }
+       error = l2t_send(tdev, skb, l2e);
+       if (error)
+               kfree_skb(skb);
+       return error;
+}
+
+int iwch_cxgb3_ofld_send(struct t3cdev *tdev, struct sk_buff *skb)
+{
+       int     error = 0;
+       struct cxio_rdev *rdev;
+
+       rdev = (struct cxio_rdev *)tdev->ulp;
+       if (cxio_fatal_error(rdev)) {
+               kfree_skb(skb);
+               return -EIO;
+       }
+       error = cxgb3_ofld_send(tdev, skb);
+       if (error)
+               kfree_skb(skb);
+       return error;
+}
+
 static void release_tid(struct t3cdev *tdev, u32 hwtid, struct sk_buff *skb)
 {
        struct cpl_tid_release *req;
@@ -146,7 +183,7 @@ static void release_tid(struct t3cdev *tdev, u32 hwtid, struct sk_buff *skb)
        req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
        OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_TID_RELEASE, hwtid));
        skb->priority = CPL_PRIORITY_SETUP;
-       cxgb3_ofld_send(tdev, skb);
+       iwch_cxgb3_ofld_send(tdev, skb);
        return;
 }
 
@@ -168,8 +205,7 @@ int iwch_quiesce_tid(struct iwch_ep *ep)
        req->val = cpu_to_be64(1 << S_TCB_RX_QUIESCE);
 
        skb->priority = CPL_PRIORITY_DATA;
-       cxgb3_ofld_send(ep->com.tdev, skb);
-       return 0;
+       return iwch_cxgb3_ofld_send(ep->com.tdev, skb);
 }
 
 int iwch_resume_tid(struct iwch_ep *ep)
@@ -190,8 +226,7 @@ int iwch_resume_tid(struct iwch_ep *ep)
        req->val = 0;
 
        skb->priority = CPL_PRIORITY_DATA;
-       cxgb3_ofld_send(ep->com.tdev, skb);
-       return 0;
+       return iwch_cxgb3_ofld_send(ep->com.tdev, skb);
 }
 
 static void set_emss(struct iwch_ep *ep, u16 opt)
@@ -248,18 +283,22 @@ static void *alloc_ep(int size, gfp_t gfp)
 
 void __free_ep(struct kref *kref)
 {
-       struct iwch_ep_common *epc;
-       epc = container_of(kref, struct iwch_ep_common, kref);
-       PDBG("%s ep %p state %s\n", __func__, epc, states[state_read(epc)]);
-       kfree(epc);
+       struct iwch_ep *ep;
+       ep = container_of(container_of(kref, struct iwch_ep_common, kref),
+                         struct iwch_ep, com);
+       PDBG("%s ep %p state %s\n", __func__, ep, states[state_read(&ep->com)]);
+       if (test_bit(RELEASE_RESOURCES, &ep->com.flags)) {
+               cxgb3_remove_tid(ep->com.tdev, (void *)ep, ep->hwtid);
+               dst_release(ep->dst);
+               l2t_release(L2DATA(ep->com.tdev), ep->l2t);
+       }
+       kfree(ep);
 }
 
 static void release_ep_resources(struct iwch_ep *ep)
 {
        PDBG("%s ep %p tid %d\n", __func__, ep, ep->hwtid);
-       cxgb3_remove_tid(ep->com.tdev, (void *)ep, ep->hwtid);
-       dst_release(ep->dst);
-       l2t_release(L2DATA(ep->com.tdev), ep->l2t);
+       set_bit(RELEASE_RESOURCES, &ep->com.flags);
        put_ep(&ep->com);
 }
 
@@ -378,7 +417,7 @@ static void abort_arp_failure(struct t3cdev *dev, struct sk_buff *skb)
 
        PDBG("%s t3cdev %p\n", __func__, dev);
        req->cmd = CPL_ABORT_NO_RST;
-       cxgb3_ofld_send(dev, skb);
+       iwch_cxgb3_ofld_send(dev, skb);
 }
 
 static int send_halfclose(struct iwch_ep *ep, gfp_t gfp)
@@ -398,8 +437,7 @@ static int send_halfclose(struct iwch_ep *ep, gfp_t gfp)
        req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_CLOSE_CON));
        req->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
        OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_CON_REQ, ep->hwtid));
-       l2t_send(ep->com.tdev, skb, ep->l2t);
-       return 0;
+       return iwch_l2t_send(ep->com.tdev, skb, ep->l2t);
 }
 
 static int send_abort(struct iwch_ep *ep, struct sk_buff *skb, gfp_t gfp)
@@ -420,8 +458,7 @@ static int send_abort(struct iwch_ep *ep, struct sk_buff *skb, gfp_t gfp)
        req->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
        OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_ABORT_REQ, ep->hwtid));
        req->cmd = CPL_ABORT_SEND_RST;
-       l2t_send(ep->com.tdev, skb, ep->l2t);
-       return 0;
+       return iwch_l2t_send(ep->com.tdev, skb, ep->l2t);
 }
 
 static int send_connect(struct iwch_ep *ep)
@@ -465,8 +502,7 @@ static int send_connect(struct iwch_ep *ep)
        req->opt0l = htonl(opt0l);
        req->params = 0;
        req->opt2 = htonl(opt2);
-       l2t_send(ep->com.tdev, skb, ep->l2t);
-       return 0;
+       return iwch_l2t_send(ep->com.tdev, skb, ep->l2t);
 }
 
 static void send_mpa_req(struct iwch_ep *ep, struct sk_buff *skb)
@@ -514,7 +550,7 @@ static void send_mpa_req(struct iwch_ep *ep, struct sk_buff *skb)
        skb_reset_transport_header(skb);
        len = skb->len;
        req = (struct tx_data_wr *) skb_push(skb, sizeof(*req));
-       req->wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_TX_DATA));
+       req->wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_TX_DATA)|F_WR_COMPL);
        req->wr_lo = htonl(V_WR_TID(ep->hwtid));
        req->len = htonl(len);
        req->param = htonl(V_TX_PORT(ep->l2t->smt_idx) |
@@ -523,7 +559,7 @@ static void send_mpa_req(struct iwch_ep *ep, struct sk_buff *skb)
        req->sndseq = htonl(ep->snd_seq);
        BUG_ON(ep->mpa_skb);
        ep->mpa_skb = skb;
-       l2t_send(ep->com.tdev, skb, ep->l2t);
+       iwch_l2t_send(ep->com.tdev, skb, ep->l2t);
        start_ep_timer(ep);
        state_set(&ep->com, MPA_REQ_SENT);
        return;
@@ -565,7 +601,7 @@ static int send_mpa_reject(struct iwch_ep *ep, const void *pdata, u8 plen)
        set_arp_failure_handler(skb, arp_failure_discard);
        skb_reset_transport_header(skb);
        req = (struct tx_data_wr *) skb_push(skb, sizeof(*req));
-       req->wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_TX_DATA));
+       req->wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_TX_DATA)|F_WR_COMPL);
        req->wr_lo = htonl(V_WR_TID(ep->hwtid));
        req->len = htonl(mpalen);
        req->param = htonl(V_TX_PORT(ep->l2t->smt_idx) |
@@ -574,8 +610,7 @@ static int send_mpa_reject(struct iwch_ep *ep, const void *pdata, u8 plen)
        req->sndseq = htonl(ep->snd_seq);
        BUG_ON(ep->mpa_skb);
        ep->mpa_skb = skb;
-       l2t_send(ep->com.tdev, skb, ep->l2t);
-       return 0;
+       return iwch_l2t_send(ep->com.tdev, skb, ep->l2t);
 }
 
 static int send_mpa_reply(struct iwch_ep *ep, const void *pdata, u8 plen)
@@ -617,7 +652,7 @@ static int send_mpa_reply(struct iwch_ep *ep, const void *pdata, u8 plen)
        skb_reset_transport_header(skb);
        len = skb->len;
        req = (struct tx_data_wr *) skb_push(skb, sizeof(*req));
-       req->wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_TX_DATA));
+       req->wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_TX_DATA)|F_WR_COMPL);
        req->wr_lo = htonl(V_WR_TID(ep->hwtid));
        req->len = htonl(len);
        req->param = htonl(V_TX_PORT(ep->l2t->smt_idx) |
@@ -626,8 +661,7 @@ static int send_mpa_reply(struct iwch_ep *ep, const void *pdata, u8 plen)
        req->sndseq = htonl(ep->snd_seq);
        ep->mpa_skb = skb;
        state_set(&ep->com, MPA_REP_SENT);
-       l2t_send(ep->com.tdev, skb, ep->l2t);
-       return 0;
+       return iwch_l2t_send(ep->com.tdev, skb, ep->l2t);
 }
 
 static int act_establish(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
@@ -753,10 +787,12 @@ static void connect_request_upcall(struct iwch_ep *ep)
        event.private_data_len = ep->plen;
        event.private_data = ep->mpa_pkt + sizeof(struct mpa_message);
        event.provider_data = ep;
-       if (state_read(&ep->parent_ep->com) != DEAD)
+       if (state_read(&ep->parent_ep->com) != DEAD) {
+               get_ep(&ep->com);
                ep->parent_ep->com.cm_id->event_handler(
                                                ep->parent_ep->com.cm_id,
                                                &event);
+       }
        put_ep(&ep->parent_ep->com);
        ep->parent_ep = NULL;
 }
@@ -791,7 +827,7 @@ static int update_rx_credits(struct iwch_ep *ep, u32 credits)
        OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_RX_DATA_ACK, ep->hwtid));
        req->credit_dack = htonl(V_RX_CREDITS(credits) | V_RX_FORCE_ACK(1));
        skb->priority = CPL_PRIORITY_ACK;
-       cxgb3_ofld_send(ep->com.tdev, skb);
+       iwch_cxgb3_ofld_send(ep->com.tdev, skb);
        return credits;
 }
 
@@ -885,6 +921,7 @@ static void process_mpa_reply(struct iwch_ep *ep, struct sk_buff *skb)
         * the MPA header is valid.
         */
        state_set(&ep->com, FPDU_MODE);
+       ep->mpa_attr.initiator = 1;
        ep->mpa_attr.crc_enabled = (mpa->flags & MPA_CRC) | crc_enabled ? 1 : 0;
        ep->mpa_attr.recv_marker_enabled = markers_enabled;
        ep->mpa_attr.xmit_marker_enabled = mpa->flags & MPA_MARKERS ? 1 : 0;
@@ -907,8 +944,14 @@ static void process_mpa_reply(struct iwch_ep *ep, struct sk_buff *skb)
        /* bind QP and TID with INIT_WR */
        err = iwch_modify_qp(ep->com.qp->rhp,
                             ep->com.qp, mask, &attrs, 1);
-       if (!err)
-               goto out;
+       if (err)
+               goto err;
+
+       if (peer2peer && iwch_rqes_posted(ep->com.qp) == 0) {
+               iwch_post_zb_read(ep->com.qp);
+       }
+
+       goto out;
 err:
        abort_connection(ep, skb, GFP_KERNEL);
 out:
@@ -1001,6 +1044,7 @@ static void process_mpa_request(struct iwch_ep *ep, struct sk_buff *skb)
         * If we get here we have accumulated the entire mpa
         * start reply message including private data.
         */
+       ep->mpa_attr.initiator = 0;
        ep->mpa_attr.crc_enabled = (mpa->flags & MPA_CRC) | crc_enabled ? 1 : 0;
        ep->mpa_attr.recv_marker_enabled = markers_enabled;
        ep->mpa_attr.xmit_marker_enabled = mpa->flags & MPA_MARKERS ? 1 : 0;
@@ -1071,17 +1115,33 @@ static int tx_ack(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
 
        PDBG("%s ep %p credits %u\n", __func__, ep, credits);
 
-       if (credits == 0)
+       if (credits == 0) {
+               PDBG(KERN_ERR "%s 0 credit ack  ep %p state %u\n",
+                       __func__, ep, state_read(&ep->com));
                return CPL_RET_BUF_DONE;
+       }
+
        BUG_ON(credits != 1);
-       BUG_ON(ep->mpa_skb == NULL);
-       kfree_skb(ep->mpa_skb);
-       ep->mpa_skb = NULL;
        dst_confirm(ep->dst);
-       if (state_read(&ep->com) == MPA_REP_SENT) {
-               ep->com.rpl_done = 1;
-               PDBG("waking up ep %p\n", ep);
-               wake_up(&ep->com.waitq);
+       if (!ep->mpa_skb) {
+               PDBG("%s rdma_init wr_ack ep %p state %u\n",
+                       __func__, ep, state_read(&ep->com));
+               if (ep->mpa_attr.initiator) {
+                       PDBG("%s initiator ep %p state %u\n",
+                               __func__, ep, state_read(&ep->com));
+                       if (peer2peer)
+                               iwch_post_zb_read(ep->com.qp);
+               } else {
+                       PDBG("%s responder ep %p state %u\n",
+                               __func__, ep, state_read(&ep->com));
+                       ep->com.rpl_done = 1;
+                       wake_up(&ep->com.waitq);
+               }
+       } else {
+               PDBG("%s lsm ack ep %p state %u freeing skb\n",
+                       __func__, ep, state_read(&ep->com));
+               kfree_skb(ep->mpa_skb);
+               ep->mpa_skb = NULL;
        }
        return CPL_RET_BUF_DONE;
 }
@@ -1099,8 +1159,7 @@ static int abort_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
         * We get 2 abort replies from the HW.  The first one must
         * be ignored except for scribbling that we need one more.
         */
-       if (!(ep->flags & ABORT_REQ_IN_PROGRESS)) {
-               ep->flags |= ABORT_REQ_IN_PROGRESS;
+       if (!test_and_set_bit(ABORT_REQ_IN_PROGRESS, &ep->com.flags)) {
                return CPL_RET_BUF_DONE;
        }
 
@@ -1175,8 +1234,7 @@ static int listen_start(struct iwch_listen_ep *ep)
        req->opt1 = htonl(V_CONN_POLICY(CPL_CONN_POLICY_ASK));
 
        skb->priority = 1;
-       cxgb3_ofld_send(ep->com.tdev, skb);
-       return 0;
+       return iwch_cxgb3_ofld_send(ep->com.tdev, skb);
 }
 
 static int pass_open_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
@@ -1209,8 +1267,7 @@ static int listen_stop(struct iwch_listen_ep *ep)
        req->cpu_idx = 0;
        OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_LISTSRV_REQ, ep->stid));
        skb->priority = 1;
-       cxgb3_ofld_send(ep->com.tdev, skb);
-       return 0;
+       return iwch_cxgb3_ofld_send(ep->com.tdev, skb);
 }
 
 static int close_listsrv_rpl(struct t3cdev *tdev, struct sk_buff *skb,
@@ -1258,7 +1315,7 @@ static void accept_cr(struct iwch_ep *ep, __be32 peer_ip, struct sk_buff *skb)
        rpl->opt2 = htonl(opt2);
        rpl->rsvd = rpl->opt2;  /* workaround for HW bug */
        skb->priority = CPL_PRIORITY_SETUP;
-       l2t_send(ep->com.tdev, skb, ep->l2t);
+       iwch_l2t_send(ep->com.tdev, skb, ep->l2t);
 
        return;
 }
@@ -1287,7 +1344,7 @@ static void reject_cr(struct t3cdev *tdev, u32 hwtid, __be32 peer_ip,
                rpl->opt0l_status = htonl(CPL_PASS_OPEN_REJECT);
                rpl->opt2 = 0;
                rpl->rsvd = rpl->opt2;
-               cxgb3_ofld_send(tdev, skb);
+               iwch_cxgb3_ofld_send(tdev, skb);
        }
 }
 
@@ -1315,15 +1372,8 @@ static int pass_accept_req(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
        tim.mac_addr = req->dst_mac;
        tim.vlan_tag = ntohs(req->vlan_tag);
        if (tdev->ctl(tdev, GET_IFF_FROM_MAC, &tim) < 0 || !tim.dev) {
-               printk(KERN_ERR
-                       "%s bad dst mac %02x %02x %02x %02x %02x %02x\n",
-                       __func__,
-                       req->dst_mac[0],
-                       req->dst_mac[1],
-                       req->dst_mac[2],
-                       req->dst_mac[3],
-                       req->dst_mac[4],
-                       req->dst_mac[5]);
+               printk(KERN_ERR "%s bad dst mac %pM\n",
+                       __func__, req->dst_mac);
                goto reject;
        }
 
@@ -1422,10 +1472,14 @@ static int peer_close(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
                /*
                 * We're gonna mark this puppy DEAD, but keep
                 * the reference on it until the ULP accepts or
-                * rejects the CR.
+                * rejects the CR. Also wake up anyone waiting
+                * in rdma connection migration (see iwch_accept_cr()).
                 */
                __state_set(&ep->com, CLOSING);
-               get_ep(&ep->com);
+               ep->com.rpl_done = 1;
+               ep->com.rpl_err = -ECONNRESET;
+               PDBG("waking up ep %p\n", ep);
+               wake_up(&ep->com.waitq);
                break;
        case MPA_REP_SENT:
                __state_set(&ep->com, CLOSING);
@@ -1506,8 +1560,7 @@ static int peer_abort(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
         * We get 2 peer aborts from the HW.  The first one must
         * be ignored except for scribbling that we need one more.
         */
-       if (!(ep->flags & PEER_ABORT_IN_PROGRESS)) {
-               ep->flags |= PEER_ABORT_IN_PROGRESS;
+       if (!test_and_set_bit(PEER_ABORT_IN_PROGRESS, &ep->com.flags)) {
                return CPL_RET_BUF_DONE;
        }
 
@@ -1534,9 +1587,13 @@ static int peer_abort(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
                /*
                 * We're gonna mark this puppy DEAD, but keep
                 * the reference on it until the ULP accepts or
-                * rejects the CR.
+                * rejects the CR. Also wake up anyone waiting
+                * in rdma connection migration (see iwch_accept_cr()).
                 */
-               get_ep(&ep->com);
+               ep->com.rpl_done = 1;
+               ep->com.rpl_err = -ECONNRESET;
+               PDBG("waking up ep %p\n", ep);
+               wake_up(&ep->com.waitq);
                break;
        case MORIBUND:
        case CLOSING:
@@ -1585,7 +1642,7 @@ static int peer_abort(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
        rpl->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
        OPCODE_TID(rpl) = htonl(MK_OPCODE_TID(CPL_ABORT_RPL, ep->hwtid));
        rpl->cmd = CPL_ABORT_NO_RST;
-       cxgb3_ofld_send(ep->com.tdev, rpl_skb);
+       iwch_cxgb3_ofld_send(ep->com.tdev, rpl_skb);
 out:
        if (release)
                release_ep_resources(ep);
@@ -1622,8 +1679,8 @@ static int close_con_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
                release = 1;
                break;
        case ABORTING:
-               break;
        case DEAD:
+               break;
        default:
                BUG_ON(1);
                break;
@@ -1650,6 +1707,9 @@ static int terminate(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
 {
        struct iwch_ep *ep = ctx;
 
+       if (state_read(&ep->com) != FPDU_MODE)
+               return CPL_RET_BUF_DONE;
+
        PDBG("%s ep %p\n", __func__, ep);
        skb_pull(skb, sizeof(struct cpl_rdma_terminate));
        PDBG("%s saving %d bytes of term msg\n", __func__, skb->len);
@@ -1739,6 +1799,7 @@ int iwch_reject_cr(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
                err = send_mpa_reject(ep, pdata, pdata_len);
                err = iwch_ep_disconnect(ep, 0, GFP_KERNEL);
        }
+       put_ep(&ep->com);
        return 0;
 }
 
@@ -1752,8 +1813,10 @@ int iwch_accept_cr(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
        struct iwch_qp *qp = get_qhp(h, conn_param->qpn);
 
        PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
-       if (state_read(&ep->com) == DEAD)
-               return -ECONNRESET;
+       if (state_read(&ep->com) == DEAD) {
+               err = -ECONNRESET;
+               goto err;
+       }
 
        BUG_ON(state_read(&ep->com) != MPA_REQ_RCVD);
        BUG_ON(!qp);
@@ -1761,20 +1824,21 @@ int iwch_accept_cr(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
        if ((conn_param->ord > qp->rhp->attr.max_rdma_read_qp_depth) ||
            (conn_param->ird > qp->rhp->attr.max_rdma_reads_per_qp)) {
                abort_connection(ep, NULL, GFP_KERNEL);
-               return -EINVAL;
+               err = -EINVAL;
+               goto err;
        }
 
        cm_id->add_ref(cm_id);
        ep->com.cm_id = cm_id;
        ep->com.qp = qp;
 
-       ep->com.rpl_done = 0;
-       ep->com.rpl_err = 0;
        ep->ird = conn_param->ird;
        ep->ord = conn_param->ord;
-       PDBG("%s %d ird %d ord %d\n", __func__, __LINE__, ep->ird, ep->ord);
 
-       get_ep(&ep->com);
+       if (peer2peer && ep->ird == 0)
+               ep->ird = 1;
+
+       PDBG("%s %d ird %d ord %d\n", __func__, __LINE__, ep->ird, ep->ord);
 
        /* bind QP to EP and move to RTS */
        attrs.mpa_attr = ep->mpa_attr;
@@ -1793,27 +1857,31 @@ int iwch_accept_cr(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
        err = iwch_modify_qp(ep->com.qp->rhp,
                             ep->com.qp, mask, &attrs, 1);
        if (err)
-               goto err;
+               goto err1;
+
+       /* if needed, wait for wr_ack */
+       if (iwch_rqes_posted(qp)) {
+               wait_event(ep->com.waitq, ep->com.rpl_done);
+               err = ep->com.rpl_err;
+               if (err)
+                       goto err1;
+       }
 
        err = send_mpa_reply(ep, conn_param->private_data,
                             conn_param->private_data_len);
        if (err)
-               goto err;
+               goto err1;
 
-       /* wait for wr_ack */
-       wait_event(ep->com.waitq, ep->com.rpl_done);
-       err = ep->com.rpl_err;
-       if (err)
-               goto err;
 
        state_set(&ep->com, FPDU_MODE);
        established_upcall(ep);
        put_ep(&ep->com);
        return 0;
-err:
+err1:
        ep->com.cm_id = NULL;
        ep->com.qp = NULL;
        cm_id->rem_ref(cm_id);
+err:
        put_ep(&ep->com);
        return err;
 }
@@ -1854,6 +1922,10 @@ int iwch_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
                       conn_param->private_data, ep->plen);
        ep->ird = conn_param->ird;
        ep->ord = conn_param->ord;
+
+       if (peer2peer && ep->ord == 0)
+               ep->ord = 1;
+
        ep->com.tdev = h->rdev.t3cdev_p;
 
        cm_id->add_ref(cm_id);
@@ -1911,6 +1983,7 @@ fail4:
 fail3:
        cxgb3_free_atid(ep->com.tdev, ep->atid);
 fail2:
+       cm_id->rem_ref(cm_id);
        put_ep(&ep->com);
 out:
        return err;
@@ -1982,8 +2055,11 @@ int iwch_destroy_listen(struct iw_cm_id *cm_id)
        ep->com.rpl_done = 0;
        ep->com.rpl_err = 0;
        err = listen_stop(ep);
+       if (err)
+               goto done;
        wait_event(ep->com.waitq, ep->com.rpl_done);
        cxgb3_free_stid(ep->com.tdev, ep->stid);
+done:
        err = ep->com.rpl_err;
        cm_id->rem_ref(cm_id);
        put_ep(&ep->com);
@@ -1995,12 +2071,22 @@ int iwch_ep_disconnect(struct iwch_ep *ep, int abrupt, gfp_t gfp)
        int ret=0;
        unsigned long flags;
        int close = 0;
+       int fatal = 0;
+       struct t3cdev *tdev;
+       struct cxio_rdev *rdev;
 
        spin_lock_irqsave(&ep->com.lock, flags);
 
        PDBG("%s ep %p state %s, abrupt %d\n", __func__, ep,
             states[ep->com.state], abrupt);
 
+       tdev = (struct t3cdev *)ep->com.tdev;
+       rdev = (struct cxio_rdev *)tdev->ulp;
+       if (cxio_fatal_error(rdev)) {
+               fatal = 1;
+               close_complete_upcall(ep);
+               ep->com.state = DEAD;
+       }
        switch (ep->com.state) {
        case MPA_REQ_WAIT:
        case MPA_REQ_SENT:
@@ -2014,14 +2100,17 @@ int iwch_ep_disconnect(struct iwch_ep *ep, int abrupt, gfp_t gfp)
                        ep->com.state = CLOSING;
                        start_ep_timer(ep);
                }
+               set_bit(CLOSE_SENT, &ep->com.flags);
                break;
        case CLOSING:
-               close = 1;
-               if (abrupt) {
-                       stop_ep_timer(ep);
-                       ep->com.state = ABORTING;
-               } else
-                       ep->com.state = MORIBUND;
+               if (!test_and_set_bit(CLOSE_SENT, &ep->com.flags)) {
+                       close = 1;
+                       if (abrupt) {
+                               stop_ep_timer(ep);
+                               ep->com.state = ABORTING;
+                       } else
+                               ep->com.state = MORIBUND;
+               }
                break;
        case MORIBUND:
        case ABORTING:
@@ -2040,7 +2129,11 @@ int iwch_ep_disconnect(struct iwch_ep *ep, int abrupt, gfp_t gfp)
                        ret = send_abort(ep, NULL, gfp);
                else
                        ret = send_halfclose(ep, gfp);
+               if (ret)
+                       fatal = 1;
        }
+       if (fatal)
+               release_ep_resources(ep);
        return ret;
 }