include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[safe/jmp/linux-2.6] / drivers / scsi / cxgb3i / cxgb3i_offload.c
index a865f1f..a175be9 100644 (file)
@@ -13,6 +13,7 @@
  */
 
 #include <linux/if_vlan.h>
+#include <linux/slab.h>
 #include <linux/version.h>
 
 #include "cxgb3_defs.h"
 #include "cxgb3i_ddp.h"
 
 #ifdef __DEBUG_C3CN_CONN__
-#define c3cn_conn_debug         cxgb3i_log_info
+#define c3cn_conn_debug                cxgb3i_log_debug
 #else
 #define c3cn_conn_debug(fmt...)
 #endif
 
 #ifdef __DEBUG_C3CN_TX__
-#define c3cn_tx_debug         cxgb3i_log_debug
+#define c3cn_tx_debug          cxgb3i_log_debug
 #else
 #define c3cn_tx_debug(fmt...)
 #endif
 
 #ifdef __DEBUG_C3CN_RX__
-#define c3cn_rx_debug         cxgb3i_log_debug
+#define c3cn_rx_debug          cxgb3i_log_debug
 #else
 #define c3cn_rx_debug(fmt...)
 #endif
@@ -47,9 +48,9 @@ static int cxgb3_rcv_win = 256 * 1024;
 module_param(cxgb3_rcv_win, int, 0644);
 MODULE_PARM_DESC(cxgb3_rcv_win, "TCP receive window in bytes (default=256KB)");
 
-static int cxgb3_snd_win = 64 * 1024;
+static int cxgb3_snd_win = 128 * 1024;
 module_param(cxgb3_snd_win, int, 0644);
-MODULE_PARM_DESC(cxgb3_snd_win, "TCP send window in bytes (default=64KB)");
+MODULE_PARM_DESC(cxgb3_snd_win, "TCP send window in bytes (default=128KB)");
 
 static int cxgb3_rx_credit_thres = 10 * 1024;
 module_param(cxgb3_rx_credit_thres, int, 0644);
@@ -94,29 +95,30 @@ static int c3cn_get_port(struct s3_conn *c3cn, struct cxgb3i_sdev_data *cdata)
        if (!cdata)
                goto error_out;
 
-       if (c3cn->saddr.sin_port != 0) {
-               idx = ntohs(c3cn->saddr.sin_port) - cxgb3_sport_base;
-               if (idx < 0 || idx >= cxgb3_max_connect)
-                       return 0;
-               if (!test_and_set_bit(idx, cdata->sport_map))
-                       return -EADDRINUSE;
+       if (c3cn->saddr.sin_port) {
+               cxgb3i_log_error("connect, sin_port NON-ZERO %u.\n",
+                                c3cn->saddr.sin_port);
+               return -EADDRINUSE;
        }
 
-       /* the sport_map_next may not be accurate but that is okay, sport_map
-          should be */
-       start = idx = cdata->sport_map_next;
+       spin_lock_bh(&cdata->lock);
+       start = idx = cdata->sport_next;
        do {
                if (++idx >= cxgb3_max_connect)
                        idx = 0;
-               if (!(test_and_set_bit(idx, cdata->sport_map))) {
+               if (!cdata->sport_conn[idx]) {
                        c3cn->saddr.sin_port = htons(cxgb3_sport_base + idx);
-                       cdata->sport_map_next = idx;
+                       cdata->sport_next = idx;
+                       cdata->sport_conn[idx] = c3cn;
+                       spin_unlock_bh(&cdata->lock);
+
                        c3cn_conn_debug("%s reserve port %u.\n",
                                        cdata->cdev->name,
                                        cxgb3_sport_base + idx);
                        return 0;
                }
        } while (idx != start);
+       spin_unlock_bh(&cdata->lock);
 
 error_out:
        return -EADDRNOTAVAIL;
@@ -124,15 +126,19 @@ error_out:
 
 static void c3cn_put_port(struct s3_conn *c3cn)
 {
-       struct cxgb3i_sdev_data *cdata = CXGB3_SDEV_DATA(c3cn->cdev);
+       if (!c3cn->cdev)
+               return;
 
        if (c3cn->saddr.sin_port) {
+               struct cxgb3i_sdev_data *cdata = CXGB3_SDEV_DATA(c3cn->cdev);
                int idx = ntohs(c3cn->saddr.sin_port) - cxgb3_sport_base;
 
                c3cn->saddr.sin_port = 0;
                if (idx < 0 || idx >= cxgb3_max_connect)
                        return;
-               clear_bit(idx, cdata->sport_map);
+               spin_lock_bh(&cdata->lock);
+               cdata->sport_conn[idx] = NULL;
+               spin_unlock_bh(&cdata->lock);
                c3cn_conn_debug("%s, release port %u.\n",
                                cdata->cdev->name, cxgb3_sport_base + idx);
        }
@@ -286,7 +292,7 @@ static void act_open_req_arp_failure(struct t3cdev *dev, struct sk_buff *skb)
        c3cn_hold(c3cn);
        spin_lock_bh(&c3cn->lock);
        if (c3cn->state == C3CN_STATE_CONNECTING)
-               fail_act_open(c3cn, EHOSTUNREACH);
+               fail_act_open(c3cn, -EHOSTUNREACH);
        spin_unlock_bh(&c3cn->lock);
        c3cn_put(c3cn);
        __kfree_skb(skb);
@@ -301,8 +307,8 @@ static void act_open_req_arp_failure(struct t3cdev *dev, struct sk_buff *skb)
 static void skb_entail(struct s3_conn *c3cn, struct sk_buff *skb,
                       int flags)
 {
-       CXGB3_SKB_CB(skb)->seq = c3cn->write_seq;
-       CXGB3_SKB_CB(skb)->flags = flags;
+       skb_tcp_seq(skb) = c3cn->write_seq;
+       skb_flags(skb) = flags;
        __skb_queue_tail(&c3cn->write_queue, skb);
 }
 
@@ -457,12 +463,9 @@ static unsigned int wrlen __read_mostly;
  * The number of WRs needed for an skb depends on the number of fragments
  * in the skb and whether it has any payload in its main body.  This maps the
  * length of the gather list represented by an skb into the # of necessary WRs.
- *
- * The max. length of an skb is controlled by the max pdu size which is ~16K.
- * Also, assume the min. fragment length is the sector size (512), then add
- * extra fragment counts for iscsi bhs and payload padding.
+ * The extra two fragments are for iscsi bhs and payload padding.
  */
-#define SKB_WR_LIST_SIZE       (16384/512 + 3)
+#define SKB_WR_LIST_SIZE       (MAX_SKB_FRAGS + 2)
 static unsigned int skb_wrs[SKB_WR_LIST_SIZE] __read_mostly;
 
 static void s3_init_wr_tab(unsigned int wr_len)
@@ -485,7 +488,7 @@ static void s3_init_wr_tab(unsigned int wr_len)
 
 static inline void reset_wr_list(struct s3_conn *c3cn)
 {
-       c3cn->wr_pending_head = NULL;
+       c3cn->wr_pending_head = c3cn->wr_pending_tail = NULL;
 }
 
 /*
@@ -496,7 +499,7 @@ static inline void reset_wr_list(struct s3_conn *c3cn)
 static inline void enqueue_wr(struct s3_conn *c3cn,
                              struct sk_buff *skb)
 {
-       skb_wr_data(skb) = NULL;
+       skb_tx_wr_next(skb) = NULL;
 
        /*
         * We want to take an extra reference since both us and the driver
@@ -509,10 +512,22 @@ static inline void enqueue_wr(struct s3_conn *c3cn,
        if (!c3cn->wr_pending_head)
                c3cn->wr_pending_head = skb;
        else
-               skb_wr_data(skb) = skb;
+               skb_tx_wr_next(c3cn->wr_pending_tail) = skb;
        c3cn->wr_pending_tail = skb;
 }
 
+static int count_pending_wrs(struct s3_conn *c3cn)
+{
+       int n = 0;
+       const struct sk_buff *skb = c3cn->wr_pending_head;
+
+       while (skb) {
+               n += skb->csum;
+               skb = skb_tx_wr_next(skb);
+       }
+       return n;
+}
+
 static inline struct sk_buff *peek_wr(const struct s3_conn *c3cn)
 {
        return c3cn->wr_pending_head;
@@ -529,8 +544,8 @@ static inline struct sk_buff *dequeue_wr(struct s3_conn *c3cn)
 
        if (likely(skb)) {
                /* Don't bother clearing the tail */
-               c3cn->wr_pending_head = skb_wr_data(skb);
-               skb_wr_data(skb) = NULL;
+               c3cn->wr_pending_head = skb_tx_wr_next(skb);
+               skb_tx_wr_next(skb) = NULL;
        }
        return skb;
 }
@@ -543,13 +558,14 @@ static void purge_wr_queue(struct s3_conn *c3cn)
 }
 
 static inline void make_tx_data_wr(struct s3_conn *c3cn, struct sk_buff *skb,
-                                  int len)
+                                  int len, int req_completion)
 {
        struct tx_data_wr *req;
 
        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) |
+                       (req_completion ? F_WR_COMPL : 0));
        req->wr_lo = htonl(V_WR_TID(c3cn->tid));
        req->sndseq = htonl(c3cn->snd_nxt);
        /* len includes the length of any HW ULP additions */
@@ -592,7 +608,7 @@ static int c3cn_push_tx_frames(struct s3_conn *c3cn, int req_completion)
 
        if (unlikely(c3cn->state == C3CN_STATE_CONNECTING ||
                     c3cn->state == C3CN_STATE_CLOSE_WAIT_1 ||
-                    c3cn->state == C3CN_STATE_ABORTING)) {
+                    c3cn->state >= C3CN_STATE_ABORTING)) {
                c3cn_tx_debug("c3cn 0x%p, in closing state %u.\n",
                              c3cn, c3cn->state);
                return 0;
@@ -615,7 +631,7 @@ static int c3cn_push_tx_frames(struct s3_conn *c3cn, int req_completion)
                if (c3cn->wr_avail < wrs_needed) {
                        c3cn_tx_debug("c3cn 0x%p, skb len %u/%u, frag %u, "
                                      "wr %d < %u.\n",
-                                     c3cn, skb->len, skb->datalen, frags,
+                                     c3cn, skb->len, skb->data_len, frags,
                                      wrs_needed, c3cn->wr_avail);
                        break;
                }
@@ -627,20 +643,24 @@ static int c3cn_push_tx_frames(struct s3_conn *c3cn, int req_completion)
                c3cn->wr_unacked += wrs_needed;
                enqueue_wr(c3cn, skb);
 
-               if (likely(CXGB3_SKB_CB(skb)->flags & C3CB_FLAG_NEED_HDR)) {
-                       len += ulp_extra_len(skb);
-                       make_tx_data_wr(c3cn, skb, len);
-                       c3cn->snd_nxt += len;
-                       if ((req_completion
-                            && c3cn->wr_unacked == wrs_needed)
-                           || (CXGB3_SKB_CB(skb)->flags & C3CB_FLAG_COMPL)
-                           || c3cn->wr_unacked >= c3cn->wr_max / 2) {
-                               struct work_request_hdr *wr = cplhdr(skb);
+               c3cn_tx_debug("c3cn 0x%p, enqueue, skb len %u/%u, frag %u, "
+                               "wr %d, left %u, unack %u.\n",
+                               c3cn, skb->len, skb->data_len, frags,
+                               wrs_needed, c3cn->wr_avail, c3cn->wr_unacked);
 
-                               wr->wr_hi |= htonl(F_WR_COMPL);
+
+               if (likely(skb_flags(skb) & C3CB_FLAG_NEED_HDR)) {
+                       if ((req_completion &&
+                               c3cn->wr_unacked == wrs_needed) ||
+                           (skb_flags(skb) & C3CB_FLAG_COMPL) ||
+                           c3cn->wr_unacked >= c3cn->wr_max / 2) {
+                               req_completion = 1;
                                c3cn->wr_unacked = 0;
                        }
-                       CXGB3_SKB_CB(skb)->flags &= ~C3CB_FLAG_NEED_HDR;
+                       len += ulp_extra_len(skb);
+                       make_tx_data_wr(c3cn, skb, len, req_completion);
+                       c3cn->snd_nxt += len;
+                       skb_flags(skb) &= ~C3CB_FLAG_NEED_HDR;
                }
 
                total_size += skb->truesize;
@@ -735,8 +755,11 @@ static void process_act_establish(struct s3_conn *c3cn, struct sk_buff *skb)
        if (unlikely(c3cn_flag(c3cn, C3CN_ACTIVE_CLOSE_NEEDED)))
                /* upper layer has requested closing */
                send_abort_req(c3cn);
-       else if (c3cn_push_tx_frames(c3cn, 1))
+       else {
+               if (skb_queue_len(&c3cn->write_queue))
+                       c3cn_push_tx_frames(c3cn, 1);
                cxgb3i_conn_tx_open(c3cn);
+       }
 }
 
 static int do_act_establish(struct t3cdev *cdev, struct sk_buff *skb,
@@ -770,18 +793,18 @@ static int act_open_rpl_status_to_errno(int status)
 {
        switch (status) {
        case CPL_ERR_CONN_RESET:
-               return ECONNREFUSED;
+               return -ECONNREFUSED;
        case CPL_ERR_ARP_MISS:
-               return EHOSTUNREACH;
+               return -EHOSTUNREACH;
        case CPL_ERR_CONN_TIMEDOUT:
-               return ETIMEDOUT;
+               return -ETIMEDOUT;
        case CPL_ERR_TCAM_FULL:
-               return ENOMEM;
+               return -ENOMEM;
        case CPL_ERR_CONN_EXIST:
                cxgb3i_log_error("ACTIVE_OPEN_RPL: 4-tuple in use\n");
-               return EADDRINUSE;
+               return -EADDRINUSE;
        default:
-               return EIO;
+               return -EIO;
        }
 }
 
@@ -795,7 +818,7 @@ static void act_open_retry_timer(unsigned long data)
        spin_lock_bh(&c3cn->lock);
        skb = alloc_skb(sizeof(struct cpl_act_open_req), GFP_ATOMIC);
        if (!skb)
-               fail_act_open(c3cn, ENOMEM);
+               fail_act_open(c3cn, -ENOMEM);
        else {
                skb->sk = (struct sock *)c3cn;
                set_arp_failure_handler(skb, act_open_req_arp_failure);
@@ -944,14 +967,14 @@ static int abort_status_to_errno(struct s3_conn *c3cn, int abort_reason,
        case CPL_ERR_BAD_SYN: /* fall through */
        case CPL_ERR_CONN_RESET:
                return c3cn->state > C3CN_STATE_ESTABLISHED ?
-                       EPIPE : ECONNRESET;
+                       -EPIPE : -ECONNRESET;
        case CPL_ERR_XMIT_TIMEDOUT:
        case CPL_ERR_PERSIST_TIMEDOUT:
        case CPL_ERR_FINWAIT2_TIMEDOUT:
        case CPL_ERR_KEEPALIVE_TIMEDOUT:
-               return ETIMEDOUT;
+               return -ETIMEDOUT;
        default:
-               return EIO;
+               return -EIO;
        }
 }
 
@@ -1082,8 +1105,8 @@ static void process_rx_iscsi_hdr(struct s3_conn *c3cn, struct sk_buff *skb)
                return;
        }
 
-       CXGB3_SKB_CB(skb)->seq = ntohl(hdr_cpl->seq);
-       CXGB3_SKB_CB(skb)->flags = 0;
+       skb_tcp_seq(skb) = ntohl(hdr_cpl->seq);
+       skb_flags(skb) = 0;
 
        skb_reset_transport_header(skb);
        __skb_pull(skb, sizeof(struct cpl_iscsi_hdr));
@@ -1103,12 +1126,12 @@ static void process_rx_iscsi_hdr(struct s3_conn *c3cn, struct sk_buff *skb)
                goto abort_conn;
 
        skb_ulp_mode(skb) = ULP2_FLAG_DATA_READY;
-       skb_ulp_pdulen(skb) = ntohs(ddp_cpl.len);
-       skb_ulp_ddigest(skb) = ntohl(ddp_cpl.ulp_crc);
+       skb_rx_pdulen(skb) = ntohs(ddp_cpl.len);
+       skb_rx_ddigest(skb) = ntohl(ddp_cpl.ulp_crc);
        status = ntohl(ddp_cpl.ddp_status);
 
        c3cn_rx_debug("rx skb 0x%p, len %u, pdulen %u, ddp status 0x%x.\n",
-                     skb, skb->len, skb_ulp_pdulen(skb), status);
+                     skb, skb->len, skb_rx_pdulen(skb), status);
 
        if (status & (1 << RX_DDP_STATUS_HCRC_SHIFT))
                skb_ulp_mode(skb) |= ULP2_FLAG_HCRC_ERROR;
@@ -1126,7 +1149,7 @@ static void process_rx_iscsi_hdr(struct s3_conn *c3cn, struct sk_buff *skb)
        } else if (status & (1 << RX_DDP_STATUS_DDP_SHIFT))
                skb_ulp_mode(skb) |= ULP2_FLAG_DATA_DDPED;
 
-       c3cn->rcv_nxt = ntohl(ddp_cpl.seq) + skb_ulp_pdulen(skb);
+       c3cn->rcv_nxt = ntohl(ddp_cpl.seq) + skb_rx_pdulen(skb);
        __pskb_trim(skb, len);
        __skb_queue_tail(&c3cn->receive_queue, skb);
        cxgb3i_conn_pdu_ready(c3cn);
@@ -1151,12 +1174,27 @@ static int do_iscsi_hdr(struct t3cdev *t3dev, struct sk_buff *skb, void *ctx)
  * Process an acknowledgment of WR completion.  Advance snd_una and send the
  * next batch of work requests from the write queue.
  */
+static void check_wr_invariants(struct s3_conn *c3cn)
+{
+       int pending = count_pending_wrs(c3cn);
+
+       if (unlikely(c3cn->wr_avail + pending != c3cn->wr_max))
+               cxgb3i_log_error("TID %u: credit imbalance: avail %u, "
+                               "pending %u, total should be %u\n",
+                               c3cn->tid, c3cn->wr_avail, pending,
+                               c3cn->wr_max);
+}
+
 static void process_wr_ack(struct s3_conn *c3cn, struct sk_buff *skb)
 {
        struct cpl_wr_ack *hdr = cplhdr(skb);
        unsigned int credits = ntohs(hdr->credits);
        u32 snd_una = ntohl(hdr->snd_una);
 
+       c3cn_tx_debug("%u WR credits, avail %u, unack %u, TID %u, state %u.\n",
+                       credits, c3cn->wr_avail, c3cn->wr_unacked,
+                       c3cn->tid, c3cn->state);
+
        c3cn->wr_avail += credits;
        if (c3cn->wr_unacked > c3cn->wr_max - c3cn->wr_avail)
                c3cn->wr_unacked = c3cn->wr_max - c3cn->wr_avail;
@@ -1171,6 +1209,17 @@ static void process_wr_ack(struct s3_conn *c3cn, struct sk_buff *skb)
                        break;
                }
                if (unlikely(credits < p->csum)) {
+                       struct tx_data_wr *w = cplhdr(p);
+                       cxgb3i_log_error("TID %u got %u WR credits need %u, "
+                                        "len %u, main body %u, frags %u, "
+                                        "seq # %u, ACK una %u, ACK nxt %u, "
+                                        "WR_AVAIL %u, WRs pending %u\n",
+                                        c3cn->tid, credits, p->csum, p->len,
+                                        p->len - p->data_len,
+                                        skb_shinfo(p)->nr_frags,
+                                        ntohl(w->sndseq), snd_una,
+                                        ntohl(hdr->snd_nxt), c3cn->wr_avail,
+                                        count_pending_wrs(c3cn) - credits);
                        p->csum -= credits;
                        break;
                } else {
@@ -1180,15 +1229,24 @@ static void process_wr_ack(struct s3_conn *c3cn, struct sk_buff *skb)
                }
        }
 
-       if (unlikely(before(snd_una, c3cn->snd_una)))
+       check_wr_invariants(c3cn);
+
+       if (unlikely(before(snd_una, c3cn->snd_una))) {
+               cxgb3i_log_error("TID %u, unexpected sequence # %u in WR_ACK "
+                                "snd_una %u\n",
+                                c3cn->tid, snd_una, c3cn->snd_una);
                goto out_free;
+       }
 
        if (c3cn->snd_una != snd_una) {
                c3cn->snd_una = snd_una;
                dst_confirm(c3cn->dst_cache);
        }
 
-       if (skb_queue_len(&c3cn->write_queue) && c3cn_push_tx_frames(c3cn, 0))
+       if (skb_queue_len(&c3cn->write_queue)) {
+               if (c3cn_push_tx_frames(c3cn, 0))
+                       cxgb3i_conn_tx_open(c3cn);
+       } else
                cxgb3i_conn_tx_open(c3cn);
 out_free:
        __kfree_skb(skb);
@@ -1253,11 +1311,7 @@ static void c3cn_release_offload_resources(struct s3_conn *c3cn)
        struct t3cdev *cdev = c3cn->cdev;
        unsigned int tid = c3cn->tid;
 
-       if (!cdev)
-               return;
-
        c3cn->qset = 0;
-
        c3cn_free_cpl_skbs(c3cn);
 
        if (c3cn->wr_avail != c3cn->wr_max) {
@@ -1265,18 +1319,22 @@ static void c3cn_release_offload_resources(struct s3_conn *c3cn)
                reset_wr_list(c3cn);
        }
 
-       if (c3cn->l2t) {
-               l2t_release(L2DATA(cdev), c3cn->l2t);
-               c3cn->l2t = NULL;
-       }
-
-       if (c3cn->state == C3CN_STATE_CONNECTING) /* we have ATID */
-               s3_free_atid(cdev, tid);
-       else {          /* we have TID */
-               cxgb3_remove_tid(cdev, (void *)c3cn, tid);
-               c3cn_put(c3cn);
+       if (cdev) {
+               if (c3cn->l2t) {
+                       l2t_release(L2DATA(cdev), c3cn->l2t);
+                       c3cn->l2t = NULL;
+               }
+               if (c3cn->state == C3CN_STATE_CONNECTING)
+                       /* we have ATID */
+                       s3_free_atid(cdev, tid);
+               else {
+                       /* we have TID */
+                       cxgb3_remove_tid(cdev, (void *)c3cn, tid);
+                       c3cn_put(c3cn);
+               }
        }
 
+       c3cn->dst_cache = NULL;
        c3cn->cdev = NULL;
 }
 
@@ -1373,16 +1431,20 @@ void cxgb3i_c3cn_release(struct s3_conn *c3cn)
 {
        c3cn_conn_debug("c3cn 0x%p, s %u, f 0x%lx.\n",
                        c3cn, c3cn->state, c3cn->flags);
-       if (likely(c3cn->state != C3CN_STATE_CONNECTING))
-               c3cn_active_close(c3cn);
-       else
+       if (unlikely(c3cn->state == C3CN_STATE_CONNECTING))
                c3cn_set_flag(c3cn, C3CN_ACTIVE_CLOSE_NEEDED);
+       else if (likely(c3cn->state != C3CN_STATE_CLOSED))
+               c3cn_active_close(c3cn);
        c3cn_put(c3cn);
 }
 
 static int is_cxgb3_dev(struct net_device *dev)
 {
        struct cxgb3i_sdev_data *cdata;
+       struct net_device *ndev = dev;
+
+       if (dev->priv_flags & IFF_802_1Q_VLAN)
+               ndev = vlan_dev_real_dev(dev);
 
        write_lock(&cdata_rwlock);
        list_for_each_entry(cdata, &cdata_list, list) {
@@ -1390,7 +1452,7 @@ static int is_cxgb3_dev(struct net_device *dev)
                int i;
 
                for (i = 0; i < ports->nports; i++)
-                       if (dev == ports->lldevs[i]) {
+                       if (ndev == ports->lldevs[i]) {
                                write_unlock(&cdata_rwlock);
                                return 1;
                        }
@@ -1422,12 +1484,13 @@ static struct net_device *cxgb3_egress_dev(struct net_device *root_dev,
        return NULL;
 }
 
-static struct rtable *find_route(__be32 saddr, __be32 daddr,
+static struct rtable *find_route(struct net_device *dev,
+                                __be32 saddr, __be32 daddr,
                                 __be16 sport, __be16 dport)
 {
        struct rtable *rt;
        struct flowi fl = {
-               .oif = 0,
+               .oif = dev ? dev->ifindex : 0,
                .nl_u = {
                         .ip4_u = {
                                   .daddr = daddr,
@@ -1452,7 +1515,7 @@ static void init_offload_conn(struct s3_conn *c3cn,
                              struct dst_entry *dst)
 {
        BUG_ON(c3cn->cdev != cdev);
-       c3cn->wr_max = c3cn->wr_avail = T3C_DATA(cdev)->max_wrs;
+       c3cn->wr_max = c3cn->wr_avail = T3C_DATA(cdev)->max_wrs - 1;
        c3cn->wr_unacked = 0;
        c3cn->mss_idx = select_mss(c3cn, dst_mtu(dst));
 
@@ -1505,9 +1568,29 @@ free_tid:
        s3_free_atid(cdev, c3cn->tid);
        c3cn->tid = 0;
 out_err:
-       return -1;
+       return -EINVAL;
 }
 
+/**
+ * cxgb3i_find_dev - find the interface associated with the given address
+ * @ipaddr: ip address
+ */
+static struct net_device *
+cxgb3i_find_dev(struct net_device *dev, __be32 ipaddr)
+{
+       struct flowi fl;
+       int err;
+       struct rtable *rt;
+
+       memset(&fl, 0, sizeof(fl));
+       fl.nl_u.ip4_u.daddr = ipaddr;
+
+       err = ip_route_output_key(dev ? dev_net(dev) : &init_net, &rt, &fl);
+       if (!err)
+               return (&rt->u.dst)->dev;
+
+       return NULL;
+}
 
 /**
  * cxgb3i_c3cn_connect - initiates an iscsi tcp connection to a given address
@@ -1516,36 +1599,48 @@ out_err:
  *
  * return 0 if active open request is sent, < 0 otherwise.
  */
-int cxgb3i_c3cn_connect(struct s3_conn *c3cn, struct sockaddr_in *usin)
+int cxgb3i_c3cn_connect(struct net_device *dev, struct s3_conn *c3cn,
+                       struct sockaddr_in *usin)
 {
        struct rtable *rt;
-       struct net_device *dev;
        struct cxgb3i_sdev_data *cdata;
        struct t3cdev *cdev;
        __be32 sipv4;
+       struct net_device *dstdev;
        int err;
 
+       c3cn_conn_debug("c3cn 0x%p, dev 0x%p.\n", c3cn, dev);
+
        if (usin->sin_family != AF_INET)
                return -EAFNOSUPPORT;
 
        c3cn->daddr.sin_port = usin->sin_port;
        c3cn->daddr.sin_addr.s_addr = usin->sin_addr.s_addr;
 
-       rt = find_route(c3cn->saddr.sin_addr.s_addr,
+       dstdev = cxgb3i_find_dev(dev, usin->sin_addr.s_addr);
+       if (!dstdev || !is_cxgb3_dev(dstdev))
+               return -ENETUNREACH;
+
+       if (dstdev->priv_flags & IFF_802_1Q_VLAN)
+               dev = dstdev;
+
+       rt = find_route(dev, c3cn->saddr.sin_addr.s_addr,
                        c3cn->daddr.sin_addr.s_addr,
                        c3cn->saddr.sin_port,
                        c3cn->daddr.sin_port);
        if (rt == NULL) {
-               c3cn_conn_debug("NO route to 0x%x, port %u.\n",
+               c3cn_conn_debug("NO route to 0x%x, port %u, dev %s.\n",
                                c3cn->daddr.sin_addr.s_addr,
-                               ntohs(c3cn->daddr.sin_port));
+                               ntohs(c3cn->daddr.sin_port),
+                               dev ? dev->name : "any");
                return -ENETUNREACH;
        }
 
        if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
-               c3cn_conn_debug("multi-cast route to 0x%x, port %u.\n",
+               c3cn_conn_debug("multi-cast route to 0x%x, port %u, dev %s.\n",
                                c3cn->daddr.sin_addr.s_addr,
-                               ntohs(c3cn->daddr.sin_port));
+                               ntohs(c3cn->daddr.sin_port),
+                               dev ? dev->name : "any");
                ip_rt_put(rt);
                return -ENETUNREACH;
        }
@@ -1581,10 +1676,11 @@ int cxgb3i_c3cn_connect(struct s3_conn *c3cn, struct sockaddr_in *usin)
        } else
                c3cn->saddr.sin_addr.s_addr = sipv4;
 
-       c3cn_conn_debug("c3cn 0x%p, %u.%u.%u.%u,%u-%u.%u.%u.%u,%u SYN_SENT.\n",
-                       c3cn, NIPQUAD(c3cn->saddr.sin_addr.s_addr),
+       c3cn_conn_debug("c3cn 0x%p, %pI4,%u-%pI4,%u SYN_SENT.\n",
+                       c3cn,
+                       &c3cn->saddr.sin_addr.s_addr,
                        ntohs(c3cn->saddr.sin_port),
-                       NIPQUAD(c3cn->daddr.sin_addr.s_addr),
+                       &c3cn->daddr.sin_addr.s_addr,
                        ntohs(c3cn->daddr.sin_port));
 
        c3cn_set_state(c3cn, C3CN_STATE_CONNECTING);
@@ -1605,7 +1701,6 @@ int cxgb3i_c3cn_connect(struct s3_conn *c3cn, struct sockaddr_in *usin)
        c3cn_set_state(c3cn, C3CN_STATE_CLOSED);
        ip_rt_put(rt);
        c3cn_put_port(c3cn);
-       c3cn->daddr.sin_port = 0;
        return err;
 }
 
@@ -1671,9 +1766,17 @@ int cxgb3i_c3cn_send_pdus(struct s3_conn *c3cn, struct sk_buff *skb)
                goto out_err;
        }
 
-       err = -EPIPE;
        if (c3cn->err) {
                c3cn_tx_debug("c3cn 0x%p, err %d.\n", c3cn, c3cn->err);
+               err = -EPIPE;
+               goto out_err;
+       }
+
+       if (c3cn->write_seq - c3cn->snd_una >= cxgb3_snd_win) {
+               c3cn_tx_debug("c3cn 0x%p, snd %u - %u > %u.\n",
+                               c3cn, c3cn->write_seq, c3cn->snd_una,
+                               cxgb3_snd_win);
+               err = -ENOBUFS;
                goto out_err;
        }
 
@@ -1711,16 +1814,33 @@ done:
 out_err:
        if (copied == 0 && err == -EPIPE)
                copied = c3cn->err ? c3cn->err : -EPIPE;
+       else
+               copied = err;
        goto done;
 }
 
 static void sdev_data_cleanup(struct cxgb3i_sdev_data *cdata)
 {
        struct adap_ports *ports = &cdata->ports;
+       struct s3_conn *c3cn;
        int i;
 
+       for (i = 0; i < cxgb3_max_connect; i++) {
+               if (cdata->sport_conn[i]) {
+                       c3cn = cdata->sport_conn[i];
+                       cdata->sport_conn[i] = NULL;
+
+                       spin_lock_bh(&c3cn->lock);
+                       c3cn->cdev = NULL;
+                       c3cn_set_flag(c3cn, C3CN_OFFLOAD_DOWN);
+                       c3cn_closed(c3cn);
+                       spin_unlock_bh(&c3cn->lock);
+               }
+       }
+
        for (i = 0; i < ports->nports; i++)
                NDEV2CDATA(ports->lldevs[i]) = NULL;
+
        cxgb3i_free_big_mem(cdata);
 }
 
@@ -1762,21 +1882,27 @@ void cxgb3i_sdev_add(struct t3cdev *cdev, struct cxgb3_client *client)
        struct cxgb3i_sdev_data *cdata;
        struct ofld_page_info rx_page_info;
        unsigned int wr_len;
-       int mapsize = DIV_ROUND_UP(cxgb3_max_connect,
-                                  8 * sizeof(unsigned long));
+       int mapsize = cxgb3_max_connect * sizeof(struct s3_conn *);
        int i;
 
        cdata =  cxgb3i_alloc_big_mem(sizeof(*cdata) + mapsize, GFP_KERNEL);
-       if (!cdata)
+       if (!cdata) {
+               cxgb3i_log_warn("t3dev 0x%p, offload up, OOM %d.\n",
+                               cdev, mapsize);
                return;
+       }
 
        if (cdev->ctl(cdev, GET_WR_LEN, &wr_len) < 0 ||
            cdev->ctl(cdev, GET_PORTS, &cdata->ports) < 0 ||
-           cdev->ctl(cdev, GET_RX_PAGE_INFO, &rx_page_info) < 0)
+           cdev->ctl(cdev, GET_RX_PAGE_INFO, &rx_page_info) < 0) {
+               cxgb3i_log_warn("t3dev 0x%p, offload up, ioctl failed.\n",
+                               cdev);
                goto free_cdata;
+       }
 
        s3_init_wr_tab(wr_len);
 
+       spin_lock_init(&cdata->lock);
        INIT_LIST_HEAD(&cdata->list);
        cdata->cdev = cdev;
        cdata->client = client;
@@ -1788,6 +1914,7 @@ void cxgb3i_sdev_add(struct t3cdev *cdev, struct cxgb3_client *client)
        list_add_tail(&cdata->list, &cdata_list);
        write_unlock(&cdata_rwlock);
 
+       cxgb3i_log_info("t3dev 0x%p, offload up, added.\n", cdev);
        return;
 
 free_cdata:
@@ -1802,6 +1929,8 @@ void cxgb3i_sdev_remove(struct t3cdev *cdev)
 {
        struct cxgb3i_sdev_data *cdata = CXGB3_SDEV_DATA(cdev);
 
+       cxgb3i_log_info("t3dev 0x%p, offload down, remove.\n", cdev);
+
        write_lock(&cdata_rwlock);
        list_del(&cdata->list);
        write_unlock(&cdata_rwlock);