RDMA/cxgb4: Keep QP referenced until TID released
[cascardo/linux.git] / drivers / infiniband / hw / cxgb4 / cm.c
1 /*
2  * Copyright (c) 2009-2010 Chelsio, Inc. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32 #include <linux/module.h>
33 #include <linux/list.h>
34 #include <linux/workqueue.h>
35 #include <linux/skbuff.h>
36 #include <linux/timer.h>
37 #include <linux/notifier.h>
38 #include <linux/inetdevice.h>
39 #include <linux/ip.h>
40 #include <linux/tcp.h>
41 #include <linux/if_vlan.h>
42
43 #include <net/neighbour.h>
44 #include <net/netevent.h>
45 #include <net/route.h>
46 #include <net/tcp.h>
47
48 #include "iw_cxgb4.h"
49
50 static char *states[] = {
51         "idle",
52         "listen",
53         "connecting",
54         "mpa_wait_req",
55         "mpa_req_sent",
56         "mpa_req_rcvd",
57         "mpa_rep_sent",
58         "fpdu_mode",
59         "aborting",
60         "closing",
61         "moribund",
62         "dead",
63         NULL,
64 };
65
66 static int nocong;
67 module_param(nocong, int, 0644);
68 MODULE_PARM_DESC(nocong, "Turn of congestion control (default=0)");
69
70 static int enable_ecn;
71 module_param(enable_ecn, int, 0644);
72 MODULE_PARM_DESC(enable_ecn, "Enable ECN (default=0/disabled)");
73
74 static int dack_mode = 1;
75 module_param(dack_mode, int, 0644);
76 MODULE_PARM_DESC(dack_mode, "Delayed ack mode (default=1)");
77
78 int c4iw_max_read_depth = 8;
79 module_param(c4iw_max_read_depth, int, 0644);
80 MODULE_PARM_DESC(c4iw_max_read_depth, "Per-connection max ORD/IRD (default=8)");
81
82 static int enable_tcp_timestamps;
83 module_param(enable_tcp_timestamps, int, 0644);
84 MODULE_PARM_DESC(enable_tcp_timestamps, "Enable tcp timestamps (default=0)");
85
86 static int enable_tcp_sack;
87 module_param(enable_tcp_sack, int, 0644);
88 MODULE_PARM_DESC(enable_tcp_sack, "Enable tcp SACK (default=0)");
89
90 static int enable_tcp_window_scaling = 1;
91 module_param(enable_tcp_window_scaling, int, 0644);
92 MODULE_PARM_DESC(enable_tcp_window_scaling,
93                  "Enable tcp window scaling (default=1)");
94
95 int c4iw_debug;
96 module_param(c4iw_debug, int, 0644);
97 MODULE_PARM_DESC(c4iw_debug, "Enable debug logging (default=0)");
98
99 static int peer2peer;
100 module_param(peer2peer, int, 0644);
101 MODULE_PARM_DESC(peer2peer, "Support peer2peer ULPs (default=0)");
102
103 static int p2p_type = FW_RI_INIT_P2PTYPE_READ_REQ;
104 module_param(p2p_type, int, 0644);
105 MODULE_PARM_DESC(p2p_type, "RDMAP opcode to use for the RTR message: "
106                            "1=RDMA_READ 0=RDMA_WRITE (default 1)");
107
108 static int ep_timeout_secs = 60;
109 module_param(ep_timeout_secs, int, 0644);
110 MODULE_PARM_DESC(ep_timeout_secs, "CM Endpoint operation timeout "
111                                    "in seconds (default=60)");
112
113 static int mpa_rev = 1;
114 module_param(mpa_rev, int, 0644);
115 MODULE_PARM_DESC(mpa_rev, "MPA Revision, 0 supports amso1100, "
116                 "1 is RFC0544 spec compliant, 2 is IETF MPA Peer Connect Draft"
117                 " compliant (default=1)");
118
119 static int markers_enabled;
120 module_param(markers_enabled, int, 0644);
121 MODULE_PARM_DESC(markers_enabled, "Enable MPA MARKERS (default(0)=disabled)");
122
123 static int crc_enabled = 1;
124 module_param(crc_enabled, int, 0644);
125 MODULE_PARM_DESC(crc_enabled, "Enable MPA CRC (default(1)=enabled)");
126
127 static int rcv_win = 256 * 1024;
128 module_param(rcv_win, int, 0644);
129 MODULE_PARM_DESC(rcv_win, "TCP receive window in bytes (default=256KB)");
130
131 static int snd_win = 128 * 1024;
132 module_param(snd_win, int, 0644);
133 MODULE_PARM_DESC(snd_win, "TCP send window in bytes (default=128KB)");
134
135 static struct workqueue_struct *workq;
136
137 static struct sk_buff_head rxq;
138
139 static struct sk_buff *get_skb(struct sk_buff *skb, int len, gfp_t gfp);
140 static void ep_timeout(unsigned long arg);
141 static void connect_reply_upcall(struct c4iw_ep *ep, int status);
142
143 static LIST_HEAD(timeout_list);
144 static spinlock_t timeout_lock;
145
146 static void deref_qp(struct c4iw_ep *ep)
147 {
148         c4iw_qp_rem_ref(&ep->com.qp->ibqp);
149         clear_bit(QP_REFERENCED, &ep->com.flags);
150 }
151
152 static void ref_qp(struct c4iw_ep *ep)
153 {
154         set_bit(QP_REFERENCED, &ep->com.flags);
155         c4iw_qp_add_ref(&ep->com.qp->ibqp);
156 }
157
158 static void start_ep_timer(struct c4iw_ep *ep)
159 {
160         PDBG("%s ep %p\n", __func__, ep);
161         if (timer_pending(&ep->timer)) {
162                 PDBG("%s stopped / restarted timer ep %p\n", __func__, ep);
163                 del_timer_sync(&ep->timer);
164         } else
165                 c4iw_get_ep(&ep->com);
166         ep->timer.expires = jiffies + ep_timeout_secs * HZ;
167         ep->timer.data = (unsigned long)ep;
168         ep->timer.function = ep_timeout;
169         add_timer(&ep->timer);
170 }
171
172 static void stop_ep_timer(struct c4iw_ep *ep)
173 {
174         PDBG("%s ep %p\n", __func__, ep);
175         if (!timer_pending(&ep->timer)) {
176                 WARN(1, "%s timer stopped when its not running! "
177                        "ep %p state %u\n", __func__, ep, ep->com.state);
178                 return;
179         }
180         del_timer_sync(&ep->timer);
181         c4iw_put_ep(&ep->com);
182 }
183
184 static int c4iw_l2t_send(struct c4iw_rdev *rdev, struct sk_buff *skb,
185                   struct l2t_entry *l2e)
186 {
187         int     error = 0;
188
189         if (c4iw_fatal_error(rdev)) {
190                 kfree_skb(skb);
191                 PDBG("%s - device in error state - dropping\n", __func__);
192                 return -EIO;
193         }
194         error = cxgb4_l2t_send(rdev->lldi.ports[0], skb, l2e);
195         if (error < 0)
196                 kfree_skb(skb);
197         return error < 0 ? error : 0;
198 }
199
200 int c4iw_ofld_send(struct c4iw_rdev *rdev, struct sk_buff *skb)
201 {
202         int     error = 0;
203
204         if (c4iw_fatal_error(rdev)) {
205                 kfree_skb(skb);
206                 PDBG("%s - device in error state - dropping\n", __func__);
207                 return -EIO;
208         }
209         error = cxgb4_ofld_send(rdev->lldi.ports[0], skb);
210         if (error < 0)
211                 kfree_skb(skb);
212         return error < 0 ? error : 0;
213 }
214
215 static void release_tid(struct c4iw_rdev *rdev, u32 hwtid, struct sk_buff *skb)
216 {
217         struct cpl_tid_release *req;
218
219         skb = get_skb(skb, sizeof *req, GFP_KERNEL);
220         if (!skb)
221                 return;
222         req = (struct cpl_tid_release *) skb_put(skb, sizeof(*req));
223         INIT_TP_WR(req, hwtid);
224         OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_TID_RELEASE, hwtid));
225         set_wr_txq(skb, CPL_PRIORITY_SETUP, 0);
226         c4iw_ofld_send(rdev, skb);
227         return;
228 }
229
230 static void set_emss(struct c4iw_ep *ep, u16 opt)
231 {
232         ep->emss = ep->com.dev->rdev.lldi.mtus[GET_TCPOPT_MSS(opt)] - 40;
233         ep->mss = ep->emss;
234         if (GET_TCPOPT_TSTAMP(opt))
235                 ep->emss -= 12;
236         if (ep->emss < 128)
237                 ep->emss = 128;
238         PDBG("%s mss_idx %u mss %u emss=%u\n", __func__, GET_TCPOPT_MSS(opt),
239              ep->mss, ep->emss);
240 }
241
242 static enum c4iw_ep_state state_read(struct c4iw_ep_common *epc)
243 {
244         enum c4iw_ep_state state;
245
246         mutex_lock(&epc->mutex);
247         state = epc->state;
248         mutex_unlock(&epc->mutex);
249         return state;
250 }
251
252 static void __state_set(struct c4iw_ep_common *epc, enum c4iw_ep_state new)
253 {
254         epc->state = new;
255 }
256
257 static void state_set(struct c4iw_ep_common *epc, enum c4iw_ep_state new)
258 {
259         mutex_lock(&epc->mutex);
260         PDBG("%s - %s -> %s\n", __func__, states[epc->state], states[new]);
261         __state_set(epc, new);
262         mutex_unlock(&epc->mutex);
263         return;
264 }
265
266 static void *alloc_ep(int size, gfp_t gfp)
267 {
268         struct c4iw_ep_common *epc;
269
270         epc = kzalloc(size, gfp);
271         if (epc) {
272                 kref_init(&epc->kref);
273                 mutex_init(&epc->mutex);
274                 c4iw_init_wr_wait(&epc->wr_wait);
275         }
276         PDBG("%s alloc ep %p\n", __func__, epc);
277         return epc;
278 }
279
280 void _c4iw_free_ep(struct kref *kref)
281 {
282         struct c4iw_ep *ep;
283
284         ep = container_of(kref, struct c4iw_ep, com.kref);
285         PDBG("%s ep %p state %s\n", __func__, ep, states[state_read(&ep->com)]);
286         if (test_bit(QP_REFERENCED, &ep->com.flags))
287                 deref_qp(ep);
288         if (test_bit(RELEASE_RESOURCES, &ep->com.flags)) {
289                 cxgb4_remove_tid(ep->com.dev->rdev.lldi.tids, 0, ep->hwtid);
290                 dst_release(ep->dst);
291                 cxgb4_l2t_release(ep->l2t);
292                 remove_handle(ep->com.dev, &ep->com.dev->hwtid_idr, ep->hwtid);
293         }
294         kfree(ep);
295 }
296
297 static void release_ep_resources(struct c4iw_ep *ep)
298 {
299         set_bit(RELEASE_RESOURCES, &ep->com.flags);
300         c4iw_put_ep(&ep->com);
301 }
302
303 static int status2errno(int status)
304 {
305         switch (status) {
306         case CPL_ERR_NONE:
307                 return 0;
308         case CPL_ERR_CONN_RESET:
309                 return -ECONNRESET;
310         case CPL_ERR_ARP_MISS:
311                 return -EHOSTUNREACH;
312         case CPL_ERR_CONN_TIMEDOUT:
313                 return -ETIMEDOUT;
314         case CPL_ERR_TCAM_FULL:
315                 return -ENOMEM;
316         case CPL_ERR_CONN_EXIST:
317                 return -EADDRINUSE;
318         default:
319                 return -EIO;
320         }
321 }
322
323 /*
324  * Try and reuse skbs already allocated...
325  */
326 static struct sk_buff *get_skb(struct sk_buff *skb, int len, gfp_t gfp)
327 {
328         if (skb && !skb_is_nonlinear(skb) && !skb_cloned(skb)) {
329                 skb_trim(skb, 0);
330                 skb_get(skb);
331                 skb_reset_transport_header(skb);
332         } else {
333                 skb = alloc_skb(len, gfp);
334         }
335         return skb;
336 }
337
338 static struct rtable *find_route(struct c4iw_dev *dev, __be32 local_ip,
339                                  __be32 peer_ip, __be16 local_port,
340                                  __be16 peer_port, u8 tos)
341 {
342         struct rtable *rt;
343         struct flowi4 fl4;
344
345         rt = ip_route_output_ports(&init_net, &fl4, NULL, peer_ip, local_ip,
346                                    peer_port, local_port, IPPROTO_TCP,
347                                    tos, 0);
348         if (IS_ERR(rt))
349                 return NULL;
350         return rt;
351 }
352
353 static void arp_failure_discard(void *handle, struct sk_buff *skb)
354 {
355         PDBG("%s c4iw_dev %p\n", __func__, handle);
356         kfree_skb(skb);
357 }
358
359 /*
360  * Handle an ARP failure for an active open.
361  */
362 static void act_open_req_arp_failure(void *handle, struct sk_buff *skb)
363 {
364         printk(KERN_ERR MOD "ARP failure duing connect\n");
365         kfree_skb(skb);
366 }
367
368 /*
369  * Handle an ARP failure for a CPL_ABORT_REQ.  Change it into a no RST variant
370  * and send it along.
371  */
372 static void abort_arp_failure(void *handle, struct sk_buff *skb)
373 {
374         struct c4iw_rdev *rdev = handle;
375         struct cpl_abort_req *req = cplhdr(skb);
376
377         PDBG("%s rdev %p\n", __func__, rdev);
378         req->cmd = CPL_ABORT_NO_RST;
379         c4iw_ofld_send(rdev, skb);
380 }
381
382 static void send_flowc(struct c4iw_ep *ep, struct sk_buff *skb)
383 {
384         unsigned int flowclen = 80;
385         struct fw_flowc_wr *flowc;
386         int i;
387
388         skb = get_skb(skb, flowclen, GFP_KERNEL);
389         flowc = (struct fw_flowc_wr *)__skb_put(skb, flowclen);
390
391         flowc->op_to_nparams = cpu_to_be32(FW_WR_OP(FW_FLOWC_WR) |
392                                            FW_FLOWC_WR_NPARAMS(8));
393         flowc->flowid_len16 = cpu_to_be32(FW_WR_LEN16(DIV_ROUND_UP(flowclen,
394                                           16)) | FW_WR_FLOWID(ep->hwtid));
395
396         flowc->mnemval[0].mnemonic = FW_FLOWC_MNEM_PFNVFN;
397         flowc->mnemval[0].val = cpu_to_be32(PCI_FUNC(ep->com.dev->rdev.lldi.pdev->devfn) << 8);
398         flowc->mnemval[1].mnemonic = FW_FLOWC_MNEM_CH;
399         flowc->mnemval[1].val = cpu_to_be32(ep->tx_chan);
400         flowc->mnemval[2].mnemonic = FW_FLOWC_MNEM_PORT;
401         flowc->mnemval[2].val = cpu_to_be32(ep->tx_chan);
402         flowc->mnemval[3].mnemonic = FW_FLOWC_MNEM_IQID;
403         flowc->mnemval[3].val = cpu_to_be32(ep->rss_qid);
404         flowc->mnemval[4].mnemonic = FW_FLOWC_MNEM_SNDNXT;
405         flowc->mnemval[4].val = cpu_to_be32(ep->snd_seq);
406         flowc->mnemval[5].mnemonic = FW_FLOWC_MNEM_RCVNXT;
407         flowc->mnemval[5].val = cpu_to_be32(ep->rcv_seq);
408         flowc->mnemval[6].mnemonic = FW_FLOWC_MNEM_SNDBUF;
409         flowc->mnemval[6].val = cpu_to_be32(snd_win);
410         flowc->mnemval[7].mnemonic = FW_FLOWC_MNEM_MSS;
411         flowc->mnemval[7].val = cpu_to_be32(ep->emss);
412         /* Pad WR to 16 byte boundary */
413         flowc->mnemval[8].mnemonic = 0;
414         flowc->mnemval[8].val = 0;
415         for (i = 0; i < 9; i++) {
416                 flowc->mnemval[i].r4[0] = 0;
417                 flowc->mnemval[i].r4[1] = 0;
418                 flowc->mnemval[i].r4[2] = 0;
419         }
420
421         set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
422         c4iw_ofld_send(&ep->com.dev->rdev, skb);
423 }
424
425 static int send_halfclose(struct c4iw_ep *ep, gfp_t gfp)
426 {
427         struct cpl_close_con_req *req;
428         struct sk_buff *skb;
429         int wrlen = roundup(sizeof *req, 16);
430
431         PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
432         skb = get_skb(NULL, wrlen, gfp);
433         if (!skb) {
434                 printk(KERN_ERR MOD "%s - failed to alloc skb\n", __func__);
435                 return -ENOMEM;
436         }
437         set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
438         t4_set_arp_err_handler(skb, NULL, arp_failure_discard);
439         req = (struct cpl_close_con_req *) skb_put(skb, wrlen);
440         memset(req, 0, wrlen);
441         INIT_TP_WR(req, ep->hwtid);
442         OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_CLOSE_CON_REQ,
443                                                     ep->hwtid));
444         return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
445 }
446
447 static int send_abort(struct c4iw_ep *ep, struct sk_buff *skb, gfp_t gfp)
448 {
449         struct cpl_abort_req *req;
450         int wrlen = roundup(sizeof *req, 16);
451
452         PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
453         skb = get_skb(skb, wrlen, gfp);
454         if (!skb) {
455                 printk(KERN_ERR MOD "%s - failed to alloc skb.\n",
456                        __func__);
457                 return -ENOMEM;
458         }
459         set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
460         t4_set_arp_err_handler(skb, &ep->com.dev->rdev, abort_arp_failure);
461         req = (struct cpl_abort_req *) skb_put(skb, wrlen);
462         memset(req, 0, wrlen);
463         INIT_TP_WR(req, ep->hwtid);
464         OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_ABORT_REQ, ep->hwtid));
465         req->cmd = CPL_ABORT_SEND_RST;
466         return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
467 }
468
469 #define VLAN_NONE 0xfff
470 #define FILTER_SEL_VLAN_NONE 0xffff
471 #define FILTER_SEL_WIDTH_P_FC (3+1) /* port uses 3 bits, FCoE one bit */
472 #define FILTER_SEL_WIDTH_VIN_P_FC \
473         (6 + 7 + FILTER_SEL_WIDTH_P_FC) /* 6 bits are unused, VF uses 7 bits*/
474 #define FILTER_SEL_WIDTH_TAG_P_FC \
475         (3 + FILTER_SEL_WIDTH_VIN_P_FC) /* PF uses 3 bits */
476 #define FILTER_SEL_WIDTH_VLD_TAG_P_FC (1 + FILTER_SEL_WIDTH_TAG_P_FC)
477
478 static unsigned int select_ntuple(struct c4iw_dev *dev, struct dst_entry *dst,
479                                   struct l2t_entry *l2t)
480 {
481         unsigned int ntuple = 0;
482         u32 viid;
483
484         switch (dev->rdev.lldi.filt_mode) {
485
486         /* default filter mode */
487         case HW_TPL_FR_MT_PR_IV_P_FC:
488                 if (l2t->vlan == VLAN_NONE)
489                         ntuple |= FILTER_SEL_VLAN_NONE << FILTER_SEL_WIDTH_P_FC;
490                 else {
491                         ntuple |= l2t->vlan << FILTER_SEL_WIDTH_P_FC;
492                         ntuple |= 1 << FILTER_SEL_WIDTH_VLD_TAG_P_FC;
493                 }
494                 ntuple |= l2t->lport << S_PORT | IPPROTO_TCP <<
495                           FILTER_SEL_WIDTH_VLD_TAG_P_FC;
496                 break;
497         case HW_TPL_FR_MT_PR_OV_P_FC: {
498                 viid = cxgb4_port_viid(l2t->neigh->dev);
499
500                 ntuple |= FW_VIID_VIN_GET(viid) << FILTER_SEL_WIDTH_P_FC;
501                 ntuple |= FW_VIID_PFN_GET(viid) << FILTER_SEL_WIDTH_VIN_P_FC;
502                 ntuple |= FW_VIID_VIVLD_GET(viid) << FILTER_SEL_WIDTH_TAG_P_FC;
503                 ntuple |= l2t->lport << S_PORT | IPPROTO_TCP <<
504                           FILTER_SEL_WIDTH_VLD_TAG_P_FC;
505                 break;
506         }
507         default:
508                 break;
509         }
510         return ntuple;
511 }
512
513 static int send_connect(struct c4iw_ep *ep)
514 {
515         struct cpl_act_open_req *req;
516         struct sk_buff *skb;
517         u64 opt0;
518         u32 opt2;
519         unsigned int mtu_idx;
520         int wscale;
521         int wrlen = roundup(sizeof *req, 16);
522
523         PDBG("%s ep %p atid %u\n", __func__, ep, ep->atid);
524
525         skb = get_skb(NULL, wrlen, GFP_KERNEL);
526         if (!skb) {
527                 printk(KERN_ERR MOD "%s - failed to alloc skb.\n",
528                        __func__);
529                 return -ENOMEM;
530         }
531         set_wr_txq(skb, CPL_PRIORITY_SETUP, ep->ctrlq_idx);
532
533         cxgb4_best_mtu(ep->com.dev->rdev.lldi.mtus, ep->mtu, &mtu_idx);
534         wscale = compute_wscale(rcv_win);
535         opt0 = (nocong ? NO_CONG(1) : 0) |
536                KEEP_ALIVE(1) |
537                DELACK(1) |
538                WND_SCALE(wscale) |
539                MSS_IDX(mtu_idx) |
540                L2T_IDX(ep->l2t->idx) |
541                TX_CHAN(ep->tx_chan) |
542                SMAC_SEL(ep->smac_idx) |
543                DSCP(ep->tos) |
544                ULP_MODE(ULP_MODE_TCPDDP) |
545                RCV_BUFSIZ(rcv_win>>10);
546         opt2 = RX_CHANNEL(0) |
547                CCTRL_ECN(enable_ecn) |
548                RSS_QUEUE_VALID | RSS_QUEUE(ep->rss_qid);
549         if (enable_tcp_timestamps)
550                 opt2 |= TSTAMPS_EN(1);
551         if (enable_tcp_sack)
552                 opt2 |= SACK_EN(1);
553         if (wscale && enable_tcp_window_scaling)
554                 opt2 |= WND_SCALE_EN(1);
555         t4_set_arp_err_handler(skb, NULL, act_open_req_arp_failure);
556
557         req = (struct cpl_act_open_req *) skb_put(skb, wrlen);
558         INIT_TP_WR(req, 0);
559         OPCODE_TID(req) = cpu_to_be32(
560                 MK_OPCODE_TID(CPL_ACT_OPEN_REQ, ((ep->rss_qid<<14)|ep->atid)));
561         req->local_port = ep->com.local_addr.sin_port;
562         req->peer_port = ep->com.remote_addr.sin_port;
563         req->local_ip = ep->com.local_addr.sin_addr.s_addr;
564         req->peer_ip = ep->com.remote_addr.sin_addr.s_addr;
565         req->opt0 = cpu_to_be64(opt0);
566         req->params = cpu_to_be32(select_ntuple(ep->com.dev, ep->dst, ep->l2t));
567         req->opt2 = cpu_to_be32(opt2);
568         set_bit(ACT_OPEN_REQ, &ep->com.history);
569         return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
570 }
571
572 static void send_mpa_req(struct c4iw_ep *ep, struct sk_buff *skb,
573                 u8 mpa_rev_to_use)
574 {
575         int mpalen, wrlen;
576         struct fw_ofld_tx_data_wr *req;
577         struct mpa_message *mpa;
578         struct mpa_v2_conn_params mpa_v2_params;
579
580         PDBG("%s ep %p tid %u pd_len %d\n", __func__, ep, ep->hwtid, ep->plen);
581
582         BUG_ON(skb_cloned(skb));
583
584         mpalen = sizeof(*mpa) + ep->plen;
585         if (mpa_rev_to_use == 2)
586                 mpalen += sizeof(struct mpa_v2_conn_params);
587         wrlen = roundup(mpalen + sizeof *req, 16);
588         skb = get_skb(skb, wrlen, GFP_KERNEL);
589         if (!skb) {
590                 connect_reply_upcall(ep, -ENOMEM);
591                 return;
592         }
593         set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
594
595         req = (struct fw_ofld_tx_data_wr *)skb_put(skb, wrlen);
596         memset(req, 0, wrlen);
597         req->op_to_immdlen = cpu_to_be32(
598                 FW_WR_OP(FW_OFLD_TX_DATA_WR) |
599                 FW_WR_COMPL(1) |
600                 FW_WR_IMMDLEN(mpalen));
601         req->flowid_len16 = cpu_to_be32(
602                 FW_WR_FLOWID(ep->hwtid) |
603                 FW_WR_LEN16(wrlen >> 4));
604         req->plen = cpu_to_be32(mpalen);
605         req->tunnel_to_proxy = cpu_to_be32(
606                 FW_OFLD_TX_DATA_WR_FLUSH(1) |
607                 FW_OFLD_TX_DATA_WR_SHOVE(1));
608
609         mpa = (struct mpa_message *)(req + 1);
610         memcpy(mpa->key, MPA_KEY_REQ, sizeof(mpa->key));
611         mpa->flags = (crc_enabled ? MPA_CRC : 0) |
612                      (markers_enabled ? MPA_MARKERS : 0) |
613                      (mpa_rev_to_use == 2 ? MPA_ENHANCED_RDMA_CONN : 0);
614         mpa->private_data_size = htons(ep->plen);
615         mpa->revision = mpa_rev_to_use;
616         if (mpa_rev_to_use == 1) {
617                 ep->tried_with_mpa_v1 = 1;
618                 ep->retry_with_mpa_v1 = 0;
619         }
620
621         if (mpa_rev_to_use == 2) {
622                 mpa->private_data_size = htons(ntohs(mpa->private_data_size) +
623                                                sizeof (struct mpa_v2_conn_params));
624                 mpa_v2_params.ird = htons((u16)ep->ird);
625                 mpa_v2_params.ord = htons((u16)ep->ord);
626
627                 if (peer2peer) {
628                         mpa_v2_params.ird |= htons(MPA_V2_PEER2PEER_MODEL);
629                         if (p2p_type == FW_RI_INIT_P2PTYPE_RDMA_WRITE)
630                                 mpa_v2_params.ord |=
631                                         htons(MPA_V2_RDMA_WRITE_RTR);
632                         else if (p2p_type == FW_RI_INIT_P2PTYPE_READ_REQ)
633                                 mpa_v2_params.ord |=
634                                         htons(MPA_V2_RDMA_READ_RTR);
635                 }
636                 memcpy(mpa->private_data, &mpa_v2_params,
637                        sizeof(struct mpa_v2_conn_params));
638
639                 if (ep->plen)
640                         memcpy(mpa->private_data +
641                                sizeof(struct mpa_v2_conn_params),
642                                ep->mpa_pkt + sizeof(*mpa), ep->plen);
643         } else
644                 if (ep->plen)
645                         memcpy(mpa->private_data,
646                                         ep->mpa_pkt + sizeof(*mpa), ep->plen);
647
648         /*
649          * Reference the mpa skb.  This ensures the data area
650          * will remain in memory until the hw acks the tx.
651          * Function fw4_ack() will deref it.
652          */
653         skb_get(skb);
654         t4_set_arp_err_handler(skb, NULL, arp_failure_discard);
655         BUG_ON(ep->mpa_skb);
656         ep->mpa_skb = skb;
657         c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
658         start_ep_timer(ep);
659         state_set(&ep->com, MPA_REQ_SENT);
660         ep->mpa_attr.initiator = 1;
661         return;
662 }
663
664 static int send_mpa_reject(struct c4iw_ep *ep, const void *pdata, u8 plen)
665 {
666         int mpalen, wrlen;
667         struct fw_ofld_tx_data_wr *req;
668         struct mpa_message *mpa;
669         struct sk_buff *skb;
670         struct mpa_v2_conn_params mpa_v2_params;
671
672         PDBG("%s ep %p tid %u pd_len %d\n", __func__, ep, ep->hwtid, ep->plen);
673
674         mpalen = sizeof(*mpa) + plen;
675         if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn)
676                 mpalen += sizeof(struct mpa_v2_conn_params);
677         wrlen = roundup(mpalen + sizeof *req, 16);
678
679         skb = get_skb(NULL, wrlen, GFP_KERNEL);
680         if (!skb) {
681                 printk(KERN_ERR MOD "%s - cannot alloc skb!\n", __func__);
682                 return -ENOMEM;
683         }
684         set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
685
686         req = (struct fw_ofld_tx_data_wr *)skb_put(skb, wrlen);
687         memset(req, 0, wrlen);
688         req->op_to_immdlen = cpu_to_be32(
689                 FW_WR_OP(FW_OFLD_TX_DATA_WR) |
690                 FW_WR_COMPL(1) |
691                 FW_WR_IMMDLEN(mpalen));
692         req->flowid_len16 = cpu_to_be32(
693                 FW_WR_FLOWID(ep->hwtid) |
694                 FW_WR_LEN16(wrlen >> 4));
695         req->plen = cpu_to_be32(mpalen);
696         req->tunnel_to_proxy = cpu_to_be32(
697                 FW_OFLD_TX_DATA_WR_FLUSH(1) |
698                 FW_OFLD_TX_DATA_WR_SHOVE(1));
699
700         mpa = (struct mpa_message *)(req + 1);
701         memset(mpa, 0, sizeof(*mpa));
702         memcpy(mpa->key, MPA_KEY_REP, sizeof(mpa->key));
703         mpa->flags = MPA_REJECT;
704         mpa->revision = mpa_rev;
705         mpa->private_data_size = htons(plen);
706
707         if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn) {
708                 mpa->flags |= MPA_ENHANCED_RDMA_CONN;
709                 mpa->private_data_size = htons(ntohs(mpa->private_data_size) +
710                                                sizeof (struct mpa_v2_conn_params));
711                 mpa_v2_params.ird = htons(((u16)ep->ird) |
712                                           (peer2peer ? MPA_V2_PEER2PEER_MODEL :
713                                            0));
714                 mpa_v2_params.ord = htons(((u16)ep->ord) | (peer2peer ?
715                                           (p2p_type ==
716                                            FW_RI_INIT_P2PTYPE_RDMA_WRITE ?
717                                            MPA_V2_RDMA_WRITE_RTR : p2p_type ==
718                                            FW_RI_INIT_P2PTYPE_READ_REQ ?
719                                            MPA_V2_RDMA_READ_RTR : 0) : 0));
720                 memcpy(mpa->private_data, &mpa_v2_params,
721                        sizeof(struct mpa_v2_conn_params));
722
723                 if (ep->plen)
724                         memcpy(mpa->private_data +
725                                sizeof(struct mpa_v2_conn_params), pdata, plen);
726         } else
727                 if (plen)
728                         memcpy(mpa->private_data, pdata, plen);
729
730         /*
731          * Reference the mpa skb again.  This ensures the data area
732          * will remain in memory until the hw acks the tx.
733          * Function fw4_ack() will deref it.
734          */
735         skb_get(skb);
736         set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
737         t4_set_arp_err_handler(skb, NULL, arp_failure_discard);
738         BUG_ON(ep->mpa_skb);
739         ep->mpa_skb = skb;
740         return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
741 }
742
743 static int send_mpa_reply(struct c4iw_ep *ep, const void *pdata, u8 plen)
744 {
745         int mpalen, wrlen;
746         struct fw_ofld_tx_data_wr *req;
747         struct mpa_message *mpa;
748         struct sk_buff *skb;
749         struct mpa_v2_conn_params mpa_v2_params;
750
751         PDBG("%s ep %p tid %u pd_len %d\n", __func__, ep, ep->hwtid, ep->plen);
752
753         mpalen = sizeof(*mpa) + plen;
754         if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn)
755                 mpalen += sizeof(struct mpa_v2_conn_params);
756         wrlen = roundup(mpalen + sizeof *req, 16);
757
758         skb = get_skb(NULL, wrlen, GFP_KERNEL);
759         if (!skb) {
760                 printk(KERN_ERR MOD "%s - cannot alloc skb!\n", __func__);
761                 return -ENOMEM;
762         }
763         set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
764
765         req = (struct fw_ofld_tx_data_wr *) skb_put(skb, wrlen);
766         memset(req, 0, wrlen);
767         req->op_to_immdlen = cpu_to_be32(
768                 FW_WR_OP(FW_OFLD_TX_DATA_WR) |
769                 FW_WR_COMPL(1) |
770                 FW_WR_IMMDLEN(mpalen));
771         req->flowid_len16 = cpu_to_be32(
772                 FW_WR_FLOWID(ep->hwtid) |
773                 FW_WR_LEN16(wrlen >> 4));
774         req->plen = cpu_to_be32(mpalen);
775         req->tunnel_to_proxy = cpu_to_be32(
776                 FW_OFLD_TX_DATA_WR_FLUSH(1) |
777                 FW_OFLD_TX_DATA_WR_SHOVE(1));
778
779         mpa = (struct mpa_message *)(req + 1);
780         memset(mpa, 0, sizeof(*mpa));
781         memcpy(mpa->key, MPA_KEY_REP, sizeof(mpa->key));
782         mpa->flags = (ep->mpa_attr.crc_enabled ? MPA_CRC : 0) |
783                      (markers_enabled ? MPA_MARKERS : 0);
784         mpa->revision = ep->mpa_attr.version;
785         mpa->private_data_size = htons(plen);
786
787         if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn) {
788                 mpa->flags |= MPA_ENHANCED_RDMA_CONN;
789                 mpa->private_data_size = htons(ntohs(mpa->private_data_size) +
790                                                sizeof (struct mpa_v2_conn_params));
791                 mpa_v2_params.ird = htons((u16)ep->ird);
792                 mpa_v2_params.ord = htons((u16)ep->ord);
793                 if (peer2peer && (ep->mpa_attr.p2p_type !=
794                                         FW_RI_INIT_P2PTYPE_DISABLED)) {
795                         mpa_v2_params.ird |= htons(MPA_V2_PEER2PEER_MODEL);
796
797                         if (p2p_type == FW_RI_INIT_P2PTYPE_RDMA_WRITE)
798                                 mpa_v2_params.ord |=
799                                         htons(MPA_V2_RDMA_WRITE_RTR);
800                         else if (p2p_type == FW_RI_INIT_P2PTYPE_READ_REQ)
801                                 mpa_v2_params.ord |=
802                                         htons(MPA_V2_RDMA_READ_RTR);
803                 }
804
805                 memcpy(mpa->private_data, &mpa_v2_params,
806                        sizeof(struct mpa_v2_conn_params));
807
808                 if (ep->plen)
809                         memcpy(mpa->private_data +
810                                sizeof(struct mpa_v2_conn_params), pdata, plen);
811         } else
812                 if (plen)
813                         memcpy(mpa->private_data, pdata, plen);
814
815         /*
816          * Reference the mpa skb.  This ensures the data area
817          * will remain in memory until the hw acks the tx.
818          * Function fw4_ack() will deref it.
819          */
820         skb_get(skb);
821         t4_set_arp_err_handler(skb, NULL, arp_failure_discard);
822         ep->mpa_skb = skb;
823         state_set(&ep->com, MPA_REP_SENT);
824         return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
825 }
826
827 static int act_establish(struct c4iw_dev *dev, struct sk_buff *skb)
828 {
829         struct c4iw_ep *ep;
830         struct cpl_act_establish *req = cplhdr(skb);
831         unsigned int tid = GET_TID(req);
832         unsigned int atid = GET_TID_TID(ntohl(req->tos_atid));
833         struct tid_info *t = dev->rdev.lldi.tids;
834
835         ep = lookup_atid(t, atid);
836
837         PDBG("%s ep %p tid %u snd_isn %u rcv_isn %u\n", __func__, ep, tid,
838              be32_to_cpu(req->snd_isn), be32_to_cpu(req->rcv_isn));
839
840         dst_confirm(ep->dst);
841
842         /* setup the hwtid for this connection */
843         ep->hwtid = tid;
844         cxgb4_insert_tid(t, ep, tid);
845         insert_handle(dev, &dev->hwtid_idr, ep, ep->hwtid);
846
847         ep->snd_seq = be32_to_cpu(req->snd_isn);
848         ep->rcv_seq = be32_to_cpu(req->rcv_isn);
849
850         set_emss(ep, ntohs(req->tcp_opt));
851
852         /* dealloc the atid */
853         remove_handle(ep->com.dev, &ep->com.dev->atid_idr, atid);
854         cxgb4_free_atid(t, atid);
855         set_bit(ACT_ESTAB, &ep->com.history);
856
857         /* start MPA negotiation */
858         send_flowc(ep, NULL);
859         if (ep->retry_with_mpa_v1)
860                 send_mpa_req(ep, skb, 1);
861         else
862                 send_mpa_req(ep, skb, mpa_rev);
863
864         return 0;
865 }
866
867 static void close_complete_upcall(struct c4iw_ep *ep)
868 {
869         struct iw_cm_event event;
870
871         PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
872         memset(&event, 0, sizeof(event));
873         event.event = IW_CM_EVENT_CLOSE;
874         if (ep->com.cm_id) {
875                 PDBG("close complete delivered ep %p cm_id %p tid %u\n",
876                      ep, ep->com.cm_id, ep->hwtid);
877                 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
878                 ep->com.cm_id->rem_ref(ep->com.cm_id);
879                 ep->com.cm_id = NULL;
880                 set_bit(CLOSE_UPCALL, &ep->com.history);
881         }
882 }
883
884 static int abort_connection(struct c4iw_ep *ep, struct sk_buff *skb, gfp_t gfp)
885 {
886         PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
887         close_complete_upcall(ep);
888         state_set(&ep->com, ABORTING);
889         set_bit(ABORT_CONN, &ep->com.history);
890         return send_abort(ep, skb, gfp);
891 }
892
893 static void peer_close_upcall(struct c4iw_ep *ep)
894 {
895         struct iw_cm_event event;
896
897         PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
898         memset(&event, 0, sizeof(event));
899         event.event = IW_CM_EVENT_DISCONNECT;
900         if (ep->com.cm_id) {
901                 PDBG("peer close delivered ep %p cm_id %p tid %u\n",
902                      ep, ep->com.cm_id, ep->hwtid);
903                 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
904                 set_bit(DISCONN_UPCALL, &ep->com.history);
905         }
906 }
907
908 static void peer_abort_upcall(struct c4iw_ep *ep)
909 {
910         struct iw_cm_event event;
911
912         PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
913         memset(&event, 0, sizeof(event));
914         event.event = IW_CM_EVENT_CLOSE;
915         event.status = -ECONNRESET;
916         if (ep->com.cm_id) {
917                 PDBG("abort delivered ep %p cm_id %p tid %u\n", ep,
918                      ep->com.cm_id, ep->hwtid);
919                 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
920                 ep->com.cm_id->rem_ref(ep->com.cm_id);
921                 ep->com.cm_id = NULL;
922                 set_bit(ABORT_UPCALL, &ep->com.history);
923         }
924 }
925
926 static void connect_reply_upcall(struct c4iw_ep *ep, int status)
927 {
928         struct iw_cm_event event;
929
930         PDBG("%s ep %p tid %u status %d\n", __func__, ep, ep->hwtid, status);
931         memset(&event, 0, sizeof(event));
932         event.event = IW_CM_EVENT_CONNECT_REPLY;
933         event.status = status;
934         event.local_addr = ep->com.local_addr;
935         event.remote_addr = ep->com.remote_addr;
936
937         if ((status == 0) || (status == -ECONNREFUSED)) {
938                 if (!ep->tried_with_mpa_v1) {
939                         /* this means MPA_v2 is used */
940                         event.private_data_len = ep->plen -
941                                 sizeof(struct mpa_v2_conn_params);
942                         event.private_data = ep->mpa_pkt +
943                                 sizeof(struct mpa_message) +
944                                 sizeof(struct mpa_v2_conn_params);
945                 } else {
946                         /* this means MPA_v1 is used */
947                         event.private_data_len = ep->plen;
948                         event.private_data = ep->mpa_pkt +
949                                 sizeof(struct mpa_message);
950                 }
951         }
952
953         PDBG("%s ep %p tid %u status %d\n", __func__, ep,
954              ep->hwtid, status);
955         set_bit(CONN_RPL_UPCALL, &ep->com.history);
956         ep->com.cm_id->event_handler(ep->com.cm_id, &event);
957
958         if (status < 0) {
959                 ep->com.cm_id->rem_ref(ep->com.cm_id);
960                 ep->com.cm_id = NULL;
961         }
962 }
963
964 static void connect_request_upcall(struct c4iw_ep *ep)
965 {
966         struct iw_cm_event event;
967
968         PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
969         memset(&event, 0, sizeof(event));
970         event.event = IW_CM_EVENT_CONNECT_REQUEST;
971         event.local_addr = ep->com.local_addr;
972         event.remote_addr = ep->com.remote_addr;
973         event.provider_data = ep;
974         if (!ep->tried_with_mpa_v1) {
975                 /* this means MPA_v2 is used */
976                 event.ord = ep->ord;
977                 event.ird = ep->ird;
978                 event.private_data_len = ep->plen -
979                         sizeof(struct mpa_v2_conn_params);
980                 event.private_data = ep->mpa_pkt + sizeof(struct mpa_message) +
981                         sizeof(struct mpa_v2_conn_params);
982         } else {
983                 /* this means MPA_v1 is used. Send max supported */
984                 event.ord = c4iw_max_read_depth;
985                 event.ird = c4iw_max_read_depth;
986                 event.private_data_len = ep->plen;
987                 event.private_data = ep->mpa_pkt + sizeof(struct mpa_message);
988         }
989         if (state_read(&ep->parent_ep->com) != DEAD) {
990                 c4iw_get_ep(&ep->com);
991                 ep->parent_ep->com.cm_id->event_handler(
992                                                 ep->parent_ep->com.cm_id,
993                                                 &event);
994         }
995         set_bit(CONNREQ_UPCALL, &ep->com.history);
996         c4iw_put_ep(&ep->parent_ep->com);
997         ep->parent_ep = NULL;
998 }
999
1000 static void established_upcall(struct c4iw_ep *ep)
1001 {
1002         struct iw_cm_event event;
1003
1004         PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1005         memset(&event, 0, sizeof(event));
1006         event.event = IW_CM_EVENT_ESTABLISHED;
1007         event.ird = ep->ird;
1008         event.ord = ep->ord;
1009         if (ep->com.cm_id) {
1010                 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1011                 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
1012                 set_bit(ESTAB_UPCALL, &ep->com.history);
1013         }
1014 }
1015
1016 static int update_rx_credits(struct c4iw_ep *ep, u32 credits)
1017 {
1018         struct cpl_rx_data_ack *req;
1019         struct sk_buff *skb;
1020         int wrlen = roundup(sizeof *req, 16);
1021
1022         PDBG("%s ep %p tid %u credits %u\n", __func__, ep, ep->hwtid, credits);
1023         skb = get_skb(NULL, wrlen, GFP_KERNEL);
1024         if (!skb) {
1025                 printk(KERN_ERR MOD "update_rx_credits - cannot alloc skb!\n");
1026                 return 0;
1027         }
1028
1029         req = (struct cpl_rx_data_ack *) skb_put(skb, wrlen);
1030         memset(req, 0, wrlen);
1031         INIT_TP_WR(req, ep->hwtid);
1032         OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_RX_DATA_ACK,
1033                                                     ep->hwtid));
1034         req->credit_dack = cpu_to_be32(credits | RX_FORCE_ACK(1) |
1035                                        F_RX_DACK_CHANGE |
1036                                        V_RX_DACK_MODE(dack_mode));
1037         set_wr_txq(skb, CPL_PRIORITY_ACK, ep->ctrlq_idx);
1038         c4iw_ofld_send(&ep->com.dev->rdev, skb);
1039         return credits;
1040 }
1041
1042 static void process_mpa_reply(struct c4iw_ep *ep, struct sk_buff *skb)
1043 {
1044         struct mpa_message *mpa;
1045         struct mpa_v2_conn_params *mpa_v2_params;
1046         u16 plen;
1047         u16 resp_ird, resp_ord;
1048         u8 rtr_mismatch = 0, insuff_ird = 0;
1049         struct c4iw_qp_attributes attrs;
1050         enum c4iw_qp_attr_mask mask;
1051         int err;
1052
1053         PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1054
1055         /*
1056          * Stop mpa timer.  If it expired, then the state has
1057          * changed and we bail since ep_timeout already aborted
1058          * the connection.
1059          */
1060         stop_ep_timer(ep);
1061         if (state_read(&ep->com) != MPA_REQ_SENT)
1062                 return;
1063
1064         /*
1065          * If we get more than the supported amount of private data
1066          * then we must fail this connection.
1067          */
1068         if (ep->mpa_pkt_len + skb->len > sizeof(ep->mpa_pkt)) {
1069                 err = -EINVAL;
1070                 goto err;
1071         }
1072
1073         /*
1074          * copy the new data into our accumulation buffer.
1075          */
1076         skb_copy_from_linear_data(skb, &(ep->mpa_pkt[ep->mpa_pkt_len]),
1077                                   skb->len);
1078         ep->mpa_pkt_len += skb->len;
1079
1080         /*
1081          * if we don't even have the mpa message, then bail.
1082          */
1083         if (ep->mpa_pkt_len < sizeof(*mpa))
1084                 return;
1085         mpa = (struct mpa_message *) ep->mpa_pkt;
1086
1087         /* Validate MPA header. */
1088         if (mpa->revision > mpa_rev) {
1089                 printk(KERN_ERR MOD "%s MPA version mismatch. Local = %d,"
1090                        " Received = %d\n", __func__, mpa_rev, mpa->revision);
1091                 err = -EPROTO;
1092                 goto err;
1093         }
1094         if (memcmp(mpa->key, MPA_KEY_REP, sizeof(mpa->key))) {
1095                 err = -EPROTO;
1096                 goto err;
1097         }
1098
1099         plen = ntohs(mpa->private_data_size);
1100
1101         /*
1102          * Fail if there's too much private data.
1103          */
1104         if (plen > MPA_MAX_PRIVATE_DATA) {
1105                 err = -EPROTO;
1106                 goto err;
1107         }
1108
1109         /*
1110          * If plen does not account for pkt size
1111          */
1112         if (ep->mpa_pkt_len > (sizeof(*mpa) + plen)) {
1113                 err = -EPROTO;
1114                 goto err;
1115         }
1116
1117         ep->plen = (u8) plen;
1118
1119         /*
1120          * If we don't have all the pdata yet, then bail.
1121          * We'll continue process when more data arrives.
1122          */
1123         if (ep->mpa_pkt_len < (sizeof(*mpa) + plen))
1124                 return;
1125
1126         if (mpa->flags & MPA_REJECT) {
1127                 err = -ECONNREFUSED;
1128                 goto err;
1129         }
1130
1131         /*
1132          * If we get here we have accumulated the entire mpa
1133          * start reply message including private data. And
1134          * the MPA header is valid.
1135          */
1136         state_set(&ep->com, FPDU_MODE);
1137         ep->mpa_attr.crc_enabled = (mpa->flags & MPA_CRC) | crc_enabled ? 1 : 0;
1138         ep->mpa_attr.recv_marker_enabled = markers_enabled;
1139         ep->mpa_attr.xmit_marker_enabled = mpa->flags & MPA_MARKERS ? 1 : 0;
1140         ep->mpa_attr.version = mpa->revision;
1141         ep->mpa_attr.p2p_type = FW_RI_INIT_P2PTYPE_DISABLED;
1142
1143         if (mpa->revision == 2) {
1144                 ep->mpa_attr.enhanced_rdma_conn =
1145                         mpa->flags & MPA_ENHANCED_RDMA_CONN ? 1 : 0;
1146                 if (ep->mpa_attr.enhanced_rdma_conn) {
1147                         mpa_v2_params = (struct mpa_v2_conn_params *)
1148                                 (ep->mpa_pkt + sizeof(*mpa));
1149                         resp_ird = ntohs(mpa_v2_params->ird) &
1150                                 MPA_V2_IRD_ORD_MASK;
1151                         resp_ord = ntohs(mpa_v2_params->ord) &
1152                                 MPA_V2_IRD_ORD_MASK;
1153
1154                         /*
1155                          * This is a double-check. Ideally, below checks are
1156                          * not required since ird/ord stuff has been taken
1157                          * care of in c4iw_accept_cr
1158                          */
1159                         if ((ep->ird < resp_ord) || (ep->ord > resp_ird)) {
1160                                 err = -ENOMEM;
1161                                 ep->ird = resp_ord;
1162                                 ep->ord = resp_ird;
1163                                 insuff_ird = 1;
1164                         }
1165
1166                         if (ntohs(mpa_v2_params->ird) &
1167                                         MPA_V2_PEER2PEER_MODEL) {
1168                                 if (ntohs(mpa_v2_params->ord) &
1169                                                 MPA_V2_RDMA_WRITE_RTR)
1170                                         ep->mpa_attr.p2p_type =
1171                                                 FW_RI_INIT_P2PTYPE_RDMA_WRITE;
1172                                 else if (ntohs(mpa_v2_params->ord) &
1173                                                 MPA_V2_RDMA_READ_RTR)
1174                                         ep->mpa_attr.p2p_type =
1175                                                 FW_RI_INIT_P2PTYPE_READ_REQ;
1176                         }
1177                 }
1178         } else if (mpa->revision == 1)
1179                 if (peer2peer)
1180                         ep->mpa_attr.p2p_type = p2p_type;
1181
1182         PDBG("%s - crc_enabled=%d, recv_marker_enabled=%d, "
1183              "xmit_marker_enabled=%d, version=%d p2p_type=%d local-p2p_type = "
1184              "%d\n", __func__, ep->mpa_attr.crc_enabled,
1185              ep->mpa_attr.recv_marker_enabled,
1186              ep->mpa_attr.xmit_marker_enabled, ep->mpa_attr.version,
1187              ep->mpa_attr.p2p_type, p2p_type);
1188
1189         /*
1190          * If responder's RTR does not match with that of initiator, assign
1191          * FW_RI_INIT_P2PTYPE_DISABLED in mpa attributes so that RTR is not
1192          * generated when moving QP to RTS state.
1193          * A TERM message will be sent after QP has moved to RTS state
1194          */
1195         if ((ep->mpa_attr.version == 2) && peer2peer &&
1196                         (ep->mpa_attr.p2p_type != p2p_type)) {
1197                 ep->mpa_attr.p2p_type = FW_RI_INIT_P2PTYPE_DISABLED;
1198                 rtr_mismatch = 1;
1199         }
1200
1201         attrs.mpa_attr = ep->mpa_attr;
1202         attrs.max_ird = ep->ird;
1203         attrs.max_ord = ep->ord;
1204         attrs.llp_stream_handle = ep;
1205         attrs.next_state = C4IW_QP_STATE_RTS;
1206
1207         mask = C4IW_QP_ATTR_NEXT_STATE |
1208             C4IW_QP_ATTR_LLP_STREAM_HANDLE | C4IW_QP_ATTR_MPA_ATTR |
1209             C4IW_QP_ATTR_MAX_IRD | C4IW_QP_ATTR_MAX_ORD;
1210
1211         /* bind QP and TID with INIT_WR */
1212         err = c4iw_modify_qp(ep->com.qp->rhp,
1213                              ep->com.qp, mask, &attrs, 1);
1214         if (err)
1215                 goto err;
1216
1217         /*
1218          * If responder's RTR requirement did not match with what initiator
1219          * supports, generate TERM message
1220          */
1221         if (rtr_mismatch) {
1222                 printk(KERN_ERR "%s: RTR mismatch, sending TERM\n", __func__);
1223                 attrs.layer_etype = LAYER_MPA | DDP_LLP;
1224                 attrs.ecode = MPA_NOMATCH_RTR;
1225                 attrs.next_state = C4IW_QP_STATE_TERMINATE;
1226                 err = c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
1227                                 C4IW_QP_ATTR_NEXT_STATE, &attrs, 0);
1228                 err = -ENOMEM;
1229                 goto out;
1230         }
1231
1232         /*
1233          * Generate TERM if initiator IRD is not sufficient for responder
1234          * provided ORD. Currently, we do the same behaviour even when
1235          * responder provided IRD is also not sufficient as regards to
1236          * initiator ORD.
1237          */
1238         if (insuff_ird) {
1239                 printk(KERN_ERR "%s: Insufficient IRD, sending TERM\n",
1240                                 __func__);
1241                 attrs.layer_etype = LAYER_MPA | DDP_LLP;
1242                 attrs.ecode = MPA_INSUFF_IRD;
1243                 attrs.next_state = C4IW_QP_STATE_TERMINATE;
1244                 err = c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
1245                                 C4IW_QP_ATTR_NEXT_STATE, &attrs, 0);
1246                 err = -ENOMEM;
1247                 goto out;
1248         }
1249         goto out;
1250 err:
1251         state_set(&ep->com, ABORTING);
1252         send_abort(ep, skb, GFP_KERNEL);
1253 out:
1254         connect_reply_upcall(ep, err);
1255         return;
1256 }
1257
1258 static void process_mpa_request(struct c4iw_ep *ep, struct sk_buff *skb)
1259 {
1260         struct mpa_message *mpa;
1261         struct mpa_v2_conn_params *mpa_v2_params;
1262         u16 plen;
1263
1264         PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1265
1266         if (state_read(&ep->com) != MPA_REQ_WAIT)
1267                 return;
1268
1269         /*
1270          * If we get more than the supported amount of private data
1271          * then we must fail this connection.
1272          */
1273         if (ep->mpa_pkt_len + skb->len > sizeof(ep->mpa_pkt)) {
1274                 stop_ep_timer(ep);
1275                 abort_connection(ep, skb, GFP_KERNEL);
1276                 return;
1277         }
1278
1279         PDBG("%s enter (%s line %u)\n", __func__, __FILE__, __LINE__);
1280
1281         /*
1282          * Copy the new data into our accumulation buffer.
1283          */
1284         skb_copy_from_linear_data(skb, &(ep->mpa_pkt[ep->mpa_pkt_len]),
1285                                   skb->len);
1286         ep->mpa_pkt_len += skb->len;
1287
1288         /*
1289          * If we don't even have the mpa message, then bail.
1290          * We'll continue process when more data arrives.
1291          */
1292         if (ep->mpa_pkt_len < sizeof(*mpa))
1293                 return;
1294
1295         PDBG("%s enter (%s line %u)\n", __func__, __FILE__, __LINE__);
1296         stop_ep_timer(ep);
1297         mpa = (struct mpa_message *) ep->mpa_pkt;
1298
1299         /*
1300          * Validate MPA Header.
1301          */
1302         if (mpa->revision > mpa_rev) {
1303                 printk(KERN_ERR MOD "%s MPA version mismatch. Local = %d,"
1304                        " Received = %d\n", __func__, mpa_rev, mpa->revision);
1305                 abort_connection(ep, skb, GFP_KERNEL);
1306                 return;
1307         }
1308
1309         if (memcmp(mpa->key, MPA_KEY_REQ, sizeof(mpa->key))) {
1310                 abort_connection(ep, skb, GFP_KERNEL);
1311                 return;
1312         }
1313
1314         plen = ntohs(mpa->private_data_size);
1315
1316         /*
1317          * Fail if there's too much private data.
1318          */
1319         if (plen > MPA_MAX_PRIVATE_DATA) {
1320                 abort_connection(ep, skb, GFP_KERNEL);
1321                 return;
1322         }
1323
1324         /*
1325          * If plen does not account for pkt size
1326          */
1327         if (ep->mpa_pkt_len > (sizeof(*mpa) + plen)) {
1328                 abort_connection(ep, skb, GFP_KERNEL);
1329                 return;
1330         }
1331         ep->plen = (u8) plen;
1332
1333         /*
1334          * If we don't have all the pdata yet, then bail.
1335          */
1336         if (ep->mpa_pkt_len < (sizeof(*mpa) + plen))
1337                 return;
1338
1339         /*
1340          * If we get here we have accumulated the entire mpa
1341          * start reply message including private data.
1342          */
1343         ep->mpa_attr.initiator = 0;
1344         ep->mpa_attr.crc_enabled = (mpa->flags & MPA_CRC) | crc_enabled ? 1 : 0;
1345         ep->mpa_attr.recv_marker_enabled = markers_enabled;
1346         ep->mpa_attr.xmit_marker_enabled = mpa->flags & MPA_MARKERS ? 1 : 0;
1347         ep->mpa_attr.version = mpa->revision;
1348         if (mpa->revision == 1)
1349                 ep->tried_with_mpa_v1 = 1;
1350         ep->mpa_attr.p2p_type = FW_RI_INIT_P2PTYPE_DISABLED;
1351
1352         if (mpa->revision == 2) {
1353                 ep->mpa_attr.enhanced_rdma_conn =
1354                         mpa->flags & MPA_ENHANCED_RDMA_CONN ? 1 : 0;
1355                 if (ep->mpa_attr.enhanced_rdma_conn) {
1356                         mpa_v2_params = (struct mpa_v2_conn_params *)
1357                                 (ep->mpa_pkt + sizeof(*mpa));
1358                         ep->ird = ntohs(mpa_v2_params->ird) &
1359                                 MPA_V2_IRD_ORD_MASK;
1360                         ep->ord = ntohs(mpa_v2_params->ord) &
1361                                 MPA_V2_IRD_ORD_MASK;
1362                         if (ntohs(mpa_v2_params->ird) & MPA_V2_PEER2PEER_MODEL)
1363                                 if (peer2peer) {
1364                                         if (ntohs(mpa_v2_params->ord) &
1365                                                         MPA_V2_RDMA_WRITE_RTR)
1366                                                 ep->mpa_attr.p2p_type =
1367                                                 FW_RI_INIT_P2PTYPE_RDMA_WRITE;
1368                                         else if (ntohs(mpa_v2_params->ord) &
1369                                                         MPA_V2_RDMA_READ_RTR)
1370                                                 ep->mpa_attr.p2p_type =
1371                                                 FW_RI_INIT_P2PTYPE_READ_REQ;
1372                                 }
1373                 }
1374         } else if (mpa->revision == 1)
1375                 if (peer2peer)
1376                         ep->mpa_attr.p2p_type = p2p_type;
1377
1378         PDBG("%s - crc_enabled=%d, recv_marker_enabled=%d, "
1379              "xmit_marker_enabled=%d, version=%d p2p_type=%d\n", __func__,
1380              ep->mpa_attr.crc_enabled, ep->mpa_attr.recv_marker_enabled,
1381              ep->mpa_attr.xmit_marker_enabled, ep->mpa_attr.version,
1382              ep->mpa_attr.p2p_type);
1383
1384         state_set(&ep->com, MPA_REQ_RCVD);
1385
1386         /* drive upcall */
1387         connect_request_upcall(ep);
1388         return;
1389 }
1390
1391 static int rx_data(struct c4iw_dev *dev, struct sk_buff *skb)
1392 {
1393         struct c4iw_ep *ep;
1394         struct cpl_rx_data *hdr = cplhdr(skb);
1395         unsigned int dlen = ntohs(hdr->len);
1396         unsigned int tid = GET_TID(hdr);
1397         struct tid_info *t = dev->rdev.lldi.tids;
1398         __u8 status = hdr->status;
1399
1400         ep = lookup_tid(t, tid);
1401         PDBG("%s ep %p tid %u dlen %u\n", __func__, ep, ep->hwtid, dlen);
1402         skb_pull(skb, sizeof(*hdr));
1403         skb_trim(skb, dlen);
1404
1405         /* update RX credits */
1406         update_rx_credits(ep, dlen);
1407
1408         switch (state_read(&ep->com)) {
1409         case MPA_REQ_SENT:
1410                 ep->rcv_seq += dlen;
1411                 process_mpa_reply(ep, skb);
1412                 break;
1413         case MPA_REQ_WAIT:
1414                 ep->rcv_seq += dlen;
1415                 process_mpa_request(ep, skb);
1416                 break;
1417         case FPDU_MODE: {
1418                 struct c4iw_qp_attributes attrs;
1419                 BUG_ON(!ep->com.qp);
1420                 if (ep->com.qp->attr.state == C4IW_QP_STATE_RTS)
1421                         pr_err("%s Unexpected streaming data." \
1422                                " ep %p state %d tid %u status %d\n",
1423                                __func__, ep, state_read(&ep->com),
1424                                ep->hwtid, status);
1425                 attrs.next_state = C4IW_QP_STATE_ERROR;
1426                 c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
1427                                C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
1428                 c4iw_ep_disconnect(ep, 1, GFP_KERNEL);
1429                 break;
1430         }
1431         default:
1432                 break;
1433         }
1434         return 0;
1435 }
1436
1437 static int abort_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
1438 {
1439         struct c4iw_ep *ep;
1440         struct cpl_abort_rpl_rss *rpl = cplhdr(skb);
1441         int release = 0;
1442         unsigned int tid = GET_TID(rpl);
1443         struct tid_info *t = dev->rdev.lldi.tids;
1444
1445         ep = lookup_tid(t, tid);
1446         if (!ep) {
1447                 printk(KERN_WARNING MOD "Abort rpl to freed endpoint\n");
1448                 return 0;
1449         }
1450         PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1451         mutex_lock(&ep->com.mutex);
1452         switch (ep->com.state) {
1453         case ABORTING:
1454                 c4iw_wake_up(&ep->com.wr_wait, -ECONNRESET);
1455                 __state_set(&ep->com, DEAD);
1456                 release = 1;
1457                 break;
1458         default:
1459                 printk(KERN_ERR "%s ep %p state %d\n",
1460                      __func__, ep, ep->com.state);
1461                 break;
1462         }
1463         mutex_unlock(&ep->com.mutex);
1464
1465         if (release)
1466                 release_ep_resources(ep);
1467         return 0;
1468 }
1469
1470 static void send_fw_act_open_req(struct c4iw_ep *ep, unsigned int atid)
1471 {
1472         struct sk_buff *skb;
1473         struct fw_ofld_connection_wr *req;
1474         unsigned int mtu_idx;
1475         int wscale;
1476
1477         skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
1478         req = (struct fw_ofld_connection_wr *)__skb_put(skb, sizeof(*req));
1479         memset(req, 0, sizeof(*req));
1480         req->op_compl = htonl(V_WR_OP(FW_OFLD_CONNECTION_WR));
1481         req->len16_pkd = htonl(FW_WR_LEN16(DIV_ROUND_UP(sizeof(*req), 16)));
1482         req->le.filter = cpu_to_be32(select_ntuple(ep->com.dev, ep->dst,
1483                                      ep->l2t));
1484         req->le.lport = ep->com.local_addr.sin_port;
1485         req->le.pport = ep->com.remote_addr.sin_port;
1486         req->le.u.ipv4.lip = ep->com.local_addr.sin_addr.s_addr;
1487         req->le.u.ipv4.pip = ep->com.remote_addr.sin_addr.s_addr;
1488         req->tcb.t_state_to_astid =
1489                         htonl(V_FW_OFLD_CONNECTION_WR_T_STATE(TCP_SYN_SENT) |
1490                         V_FW_OFLD_CONNECTION_WR_ASTID(atid));
1491         req->tcb.cplrxdataack_cplpassacceptrpl =
1492                         htons(F_FW_OFLD_CONNECTION_WR_CPLRXDATAACK);
1493         req->tcb.tx_max = jiffies;
1494         req->tcb.rcv_adv = htons(1);
1495         cxgb4_best_mtu(ep->com.dev->rdev.lldi.mtus, ep->mtu, &mtu_idx);
1496         wscale = compute_wscale(rcv_win);
1497         req->tcb.opt0 = TCAM_BYPASS(1) |
1498                 (nocong ? NO_CONG(1) : 0) |
1499                 KEEP_ALIVE(1) |
1500                 DELACK(1) |
1501                 WND_SCALE(wscale) |
1502                 MSS_IDX(mtu_idx) |
1503                 L2T_IDX(ep->l2t->idx) |
1504                 TX_CHAN(ep->tx_chan) |
1505                 SMAC_SEL(ep->smac_idx) |
1506                 DSCP(ep->tos) |
1507                 ULP_MODE(ULP_MODE_TCPDDP) |
1508                 RCV_BUFSIZ(rcv_win >> 10);
1509         req->tcb.opt2 = PACE(1) |
1510                 TX_QUEUE(ep->com.dev->rdev.lldi.tx_modq[ep->tx_chan]) |
1511                 RX_CHANNEL(0) |
1512                 CCTRL_ECN(enable_ecn) |
1513                 RSS_QUEUE_VALID | RSS_QUEUE(ep->rss_qid);
1514         if (enable_tcp_timestamps)
1515                 req->tcb.opt2 |= TSTAMPS_EN(1);
1516         if (enable_tcp_sack)
1517                 req->tcb.opt2 |= SACK_EN(1);
1518         if (wscale && enable_tcp_window_scaling)
1519                 req->tcb.opt2 |= WND_SCALE_EN(1);
1520         req->tcb.opt0 = cpu_to_be64(req->tcb.opt0);
1521         req->tcb.opt2 = cpu_to_be32(req->tcb.opt2);
1522         set_wr_txq(skb, CPL_PRIORITY_CONTROL, ep->ctrlq_idx);
1523         set_bit(ACT_OFLD_CONN, &ep->com.history);
1524         c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
1525 }
1526
1527 /*
1528  * Return whether a failed active open has allocated a TID
1529  */
1530 static inline int act_open_has_tid(int status)
1531 {
1532         return status != CPL_ERR_TCAM_FULL && status != CPL_ERR_CONN_EXIST &&
1533                status != CPL_ERR_ARP_MISS;
1534 }
1535
1536 #define ACT_OPEN_RETRY_COUNT 2
1537
1538 static int c4iw_reconnect(struct c4iw_ep *ep)
1539 {
1540         int err = 0;
1541         struct rtable *rt;
1542         struct port_info *pi;
1543         struct net_device *pdev;
1544         int step;
1545         struct neighbour *neigh;
1546
1547         PDBG("%s qp %p cm_id %p\n", __func__, ep->com.qp, ep->com.cm_id);
1548         init_timer(&ep->timer);
1549
1550         /*
1551          * Allocate an active TID to initiate a TCP connection.
1552          */
1553         ep->atid = cxgb4_alloc_atid(ep->com.dev->rdev.lldi.tids, ep);
1554         if (ep->atid == -1) {
1555                 pr_err("%s - cannot alloc atid.\n", __func__);
1556                 err = -ENOMEM;
1557                 goto fail2;
1558         }
1559         insert_handle(ep->com.dev, &ep->com.dev->atid_idr, ep, ep->atid);
1560
1561         /* find a route */
1562         rt = find_route(ep->com.dev,
1563                         ep->com.cm_id->local_addr.sin_addr.s_addr,
1564                         ep->com.cm_id->remote_addr.sin_addr.s_addr,
1565                         ep->com.cm_id->local_addr.sin_port,
1566                         ep->com.cm_id->remote_addr.sin_port, 0);
1567         if (!rt) {
1568                 pr_err("%s - cannot find route.\n", __func__);
1569                 err = -EHOSTUNREACH;
1570                 goto fail3;
1571         }
1572         ep->dst = &rt->dst;
1573
1574         neigh = dst_neigh_lookup(ep->dst,
1575                         &ep->com.cm_id->remote_addr.sin_addr.s_addr);
1576         /* get a l2t entry */
1577         if (neigh->dev->flags & IFF_LOOPBACK) {
1578                 PDBG("%s LOOPBACK\n", __func__);
1579                 pdev = ip_dev_find(&init_net,
1580                                 ep->com.cm_id->remote_addr.sin_addr.s_addr);
1581                 ep->l2t = cxgb4_l2t_get(ep->com.dev->rdev.lldi.l2t,
1582                                 neigh, pdev, 0);
1583                 pi = (struct port_info *)netdev_priv(pdev);
1584                 ep->mtu = pdev->mtu;
1585                 ep->tx_chan = cxgb4_port_chan(pdev);
1586                 ep->smac_idx = (cxgb4_port_viid(pdev) & 0x7F) << 1;
1587                 dev_put(pdev);
1588         } else {
1589                 ep->l2t = cxgb4_l2t_get(ep->com.dev->rdev.lldi.l2t,
1590                                 neigh, neigh->dev, 0);
1591                 pi = (struct port_info *)netdev_priv(neigh->dev);
1592                 ep->mtu = dst_mtu(ep->dst);
1593                 ep->tx_chan = cxgb4_port_chan(neigh->dev);
1594                 ep->smac_idx = (cxgb4_port_viid(neigh->dev) &
1595                                 0x7F) << 1;
1596         }
1597
1598         step = ep->com.dev->rdev.lldi.ntxq / ep->com.dev->rdev.lldi.nchan;
1599         ep->txq_idx = pi->port_id * step;
1600         ep->ctrlq_idx = pi->port_id;
1601         step = ep->com.dev->rdev.lldi.nrxq / ep->com.dev->rdev.lldi.nchan;
1602         ep->rss_qid = ep->com.dev->rdev.lldi.rxq_ids[pi->port_id * step];
1603
1604         if (!ep->l2t) {
1605                 pr_err("%s - cannot alloc l2e.\n", __func__);
1606                 err = -ENOMEM;
1607                 goto fail4;
1608         }
1609
1610         PDBG("%s txq_idx %u tx_chan %u smac_idx %u rss_qid %u l2t_idx %u\n",
1611              __func__, ep->txq_idx, ep->tx_chan, ep->smac_idx, ep->rss_qid,
1612              ep->l2t->idx);
1613
1614         state_set(&ep->com, CONNECTING);
1615         ep->tos = 0;
1616
1617         /* send connect request to rnic */
1618         err = send_connect(ep);
1619         if (!err)
1620                 goto out;
1621
1622         cxgb4_l2t_release(ep->l2t);
1623 fail4:
1624         dst_release(ep->dst);
1625 fail3:
1626         remove_handle(ep->com.dev, &ep->com.dev->atid_idr, ep->atid);
1627         cxgb4_free_atid(ep->com.dev->rdev.lldi.tids, ep->atid);
1628 fail2:
1629         /*
1630          * remember to send notification to upper layer.
1631          * We are in here so the upper layer is not aware that this is
1632          * re-connect attempt and so, upper layer is still waiting for
1633          * response of 1st connect request.
1634          */
1635         connect_reply_upcall(ep, -ECONNRESET);
1636         c4iw_put_ep(&ep->com);
1637 out:
1638         return err;
1639 }
1640
1641 static int act_open_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
1642 {
1643         struct c4iw_ep *ep;
1644         struct cpl_act_open_rpl *rpl = cplhdr(skb);
1645         unsigned int atid = GET_TID_TID(GET_AOPEN_ATID(
1646                                         ntohl(rpl->atid_status)));
1647         struct tid_info *t = dev->rdev.lldi.tids;
1648         int status = GET_AOPEN_STATUS(ntohl(rpl->atid_status));
1649
1650         ep = lookup_atid(t, atid);
1651
1652         PDBG("%s ep %p atid %u status %u errno %d\n", __func__, ep, atid,
1653              status, status2errno(status));
1654
1655         if (status == CPL_ERR_RTX_NEG_ADVICE) {
1656                 printk(KERN_WARNING MOD "Connection problems for atid %u\n",
1657                         atid);
1658                 return 0;
1659         }
1660
1661         set_bit(ACT_OPEN_RPL, &ep->com.history);
1662
1663         /*
1664          * Log interesting failures.
1665          */
1666         switch (status) {
1667         case CPL_ERR_CONN_RESET:
1668         case CPL_ERR_CONN_TIMEDOUT:
1669                 break;
1670         case CPL_ERR_TCAM_FULL:
1671                 if (dev->rdev.lldi.enable_fw_ofld_conn) {
1672                         mutex_lock(&dev->rdev.stats.lock);
1673                         dev->rdev.stats.tcam_full++;
1674                         mutex_unlock(&dev->rdev.stats.lock);
1675                         send_fw_act_open_req(ep,
1676                                              GET_TID_TID(GET_AOPEN_ATID(
1677                                              ntohl(rpl->atid_status))));
1678                         return 0;
1679                 }
1680                 break;
1681         case CPL_ERR_CONN_EXIST:
1682                 if (ep->retry_count++ < ACT_OPEN_RETRY_COUNT) {
1683                         set_bit(ACT_RETRY_INUSE, &ep->com.history);
1684                         remove_handle(ep->com.dev, &ep->com.dev->atid_idr,
1685                                         atid);
1686                         cxgb4_free_atid(t, atid);
1687                         dst_release(ep->dst);
1688                         cxgb4_l2t_release(ep->l2t);
1689                         c4iw_reconnect(ep);
1690                         return 0;
1691                 }
1692                 break;
1693         default:
1694                 printk(KERN_INFO MOD "Active open failure - "
1695                        "atid %u status %u errno %d %pI4:%u->%pI4:%u\n",
1696                        atid, status, status2errno(status),
1697                        &ep->com.local_addr.sin_addr.s_addr,
1698                        ntohs(ep->com.local_addr.sin_port),
1699                        &ep->com.remote_addr.sin_addr.s_addr,
1700                        ntohs(ep->com.remote_addr.sin_port));
1701                 break;
1702         }
1703
1704         connect_reply_upcall(ep, status2errno(status));
1705         state_set(&ep->com, DEAD);
1706
1707         if (status && act_open_has_tid(status))
1708                 cxgb4_remove_tid(ep->com.dev->rdev.lldi.tids, 0, GET_TID(rpl));
1709
1710         remove_handle(ep->com.dev, &ep->com.dev->atid_idr, atid);
1711         cxgb4_free_atid(t, atid);
1712         dst_release(ep->dst);
1713         cxgb4_l2t_release(ep->l2t);
1714         c4iw_put_ep(&ep->com);
1715
1716         return 0;
1717 }
1718
1719 static int pass_open_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
1720 {
1721         struct cpl_pass_open_rpl *rpl = cplhdr(skb);
1722         struct tid_info *t = dev->rdev.lldi.tids;
1723         unsigned int stid = GET_TID(rpl);
1724         struct c4iw_listen_ep *ep = lookup_stid(t, stid);
1725
1726         if (!ep) {
1727                 PDBG("%s stid %d lookup failure!\n", __func__, stid);
1728                 goto out;
1729         }
1730         PDBG("%s ep %p status %d error %d\n", __func__, ep,
1731              rpl->status, status2errno(rpl->status));
1732         c4iw_wake_up(&ep->com.wr_wait, status2errno(rpl->status));
1733
1734 out:
1735         return 0;
1736 }
1737
1738 static int listen_stop(struct c4iw_listen_ep *ep)
1739 {
1740         struct sk_buff *skb;
1741         struct cpl_close_listsvr_req *req;
1742
1743         PDBG("%s ep %p\n", __func__, ep);
1744         skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
1745         if (!skb) {
1746                 printk(KERN_ERR MOD "%s - failed to alloc skb\n", __func__);
1747                 return -ENOMEM;
1748         }
1749         req = (struct cpl_close_listsvr_req *) skb_put(skb, sizeof(*req));
1750         INIT_TP_WR(req, 0);
1751         OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_CLOSE_LISTSRV_REQ,
1752                                                     ep->stid));
1753         req->reply_ctrl = cpu_to_be16(
1754                           QUEUENO(ep->com.dev->rdev.lldi.rxq_ids[0]));
1755         set_wr_txq(skb, CPL_PRIORITY_SETUP, 0);
1756         return c4iw_ofld_send(&ep->com.dev->rdev, skb);
1757 }
1758
1759 static int close_listsrv_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
1760 {
1761         struct cpl_close_listsvr_rpl *rpl = cplhdr(skb);
1762         struct tid_info *t = dev->rdev.lldi.tids;
1763         unsigned int stid = GET_TID(rpl);
1764         struct c4iw_listen_ep *ep = lookup_stid(t, stid);
1765
1766         PDBG("%s ep %p\n", __func__, ep);
1767         c4iw_wake_up(&ep->com.wr_wait, status2errno(rpl->status));
1768         return 0;
1769 }
1770
1771 static void accept_cr(struct c4iw_ep *ep, __be32 peer_ip, struct sk_buff *skb,
1772                       struct cpl_pass_accept_req *req)
1773 {
1774         struct cpl_pass_accept_rpl *rpl;
1775         unsigned int mtu_idx;
1776         u64 opt0;
1777         u32 opt2;
1778         int wscale;
1779
1780         PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1781         BUG_ON(skb_cloned(skb));
1782         skb_trim(skb, sizeof(*rpl));
1783         skb_get(skb);
1784         cxgb4_best_mtu(ep->com.dev->rdev.lldi.mtus, ep->mtu, &mtu_idx);
1785         wscale = compute_wscale(rcv_win);
1786         opt0 = (nocong ? NO_CONG(1) : 0) |
1787                KEEP_ALIVE(1) |
1788                DELACK(1) |
1789                WND_SCALE(wscale) |
1790                MSS_IDX(mtu_idx) |
1791                L2T_IDX(ep->l2t->idx) |
1792                TX_CHAN(ep->tx_chan) |
1793                SMAC_SEL(ep->smac_idx) |
1794                DSCP(ep->tos >> 2) |
1795                ULP_MODE(ULP_MODE_TCPDDP) |
1796                RCV_BUFSIZ(rcv_win>>10);
1797         opt2 = RX_CHANNEL(0) |
1798                RSS_QUEUE_VALID | RSS_QUEUE(ep->rss_qid);
1799
1800         if (enable_tcp_timestamps && req->tcpopt.tstamp)
1801                 opt2 |= TSTAMPS_EN(1);
1802         if (enable_tcp_sack && req->tcpopt.sack)
1803                 opt2 |= SACK_EN(1);
1804         if (wscale && enable_tcp_window_scaling)
1805                 opt2 |= WND_SCALE_EN(1);
1806         if (enable_ecn) {
1807                 const struct tcphdr *tcph;
1808                 u32 hlen = ntohl(req->hdr_len);
1809
1810                 tcph = (const void *)(req + 1) + G_ETH_HDR_LEN(hlen) +
1811                         G_IP_HDR_LEN(hlen);
1812                 if (tcph->ece && tcph->cwr)
1813                         opt2 |= CCTRL_ECN(1);
1814         }
1815
1816         rpl = cplhdr(skb);
1817         INIT_TP_WR(rpl, ep->hwtid);
1818         OPCODE_TID(rpl) = cpu_to_be32(MK_OPCODE_TID(CPL_PASS_ACCEPT_RPL,
1819                                       ep->hwtid));
1820         rpl->opt0 = cpu_to_be64(opt0);
1821         rpl->opt2 = cpu_to_be32(opt2);
1822         set_wr_txq(skb, CPL_PRIORITY_SETUP, ep->ctrlq_idx);
1823         c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
1824
1825         return;
1826 }
1827
1828 static void reject_cr(struct c4iw_dev *dev, u32 hwtid, __be32 peer_ip,
1829                       struct sk_buff *skb)
1830 {
1831         PDBG("%s c4iw_dev %p tid %u peer_ip %x\n", __func__, dev, hwtid,
1832              peer_ip);
1833         BUG_ON(skb_cloned(skb));
1834         skb_trim(skb, sizeof(struct cpl_tid_release));
1835         skb_get(skb);
1836         release_tid(&dev->rdev, hwtid, skb);
1837         return;
1838 }
1839
1840 static void get_4tuple(struct cpl_pass_accept_req *req,
1841                        __be32 *local_ip, __be32 *peer_ip,
1842                        __be16 *local_port, __be16 *peer_port)
1843 {
1844         int eth_len = G_ETH_HDR_LEN(be32_to_cpu(req->hdr_len));
1845         int ip_len = G_IP_HDR_LEN(be32_to_cpu(req->hdr_len));
1846         struct iphdr *ip = (struct iphdr *)((u8 *)(req + 1) + eth_len);
1847         struct tcphdr *tcp = (struct tcphdr *)
1848                              ((u8 *)(req + 1) + eth_len + ip_len);
1849
1850         PDBG("%s saddr 0x%x daddr 0x%x sport %u dport %u\n", __func__,
1851              ntohl(ip->saddr), ntohl(ip->daddr), ntohs(tcp->source),
1852              ntohs(tcp->dest));
1853
1854         *peer_ip = ip->saddr;
1855         *local_ip = ip->daddr;
1856         *peer_port = tcp->source;
1857         *local_port = tcp->dest;
1858
1859         return;
1860 }
1861
1862 static int import_ep(struct c4iw_ep *ep, __be32 peer_ip, struct dst_entry *dst,
1863                      struct c4iw_dev *cdev, bool clear_mpa_v1)
1864 {
1865         struct neighbour *n;
1866         int err, step;
1867
1868         n = dst_neigh_lookup(dst, &peer_ip);
1869         if (!n)
1870                 return -ENODEV;
1871
1872         rcu_read_lock();
1873         err = -ENOMEM;
1874         if (n->dev->flags & IFF_LOOPBACK) {
1875                 struct net_device *pdev;
1876
1877                 pdev = ip_dev_find(&init_net, peer_ip);
1878                 if (!pdev) {
1879                         err = -ENODEV;
1880                         goto out;
1881                 }
1882                 ep->l2t = cxgb4_l2t_get(cdev->rdev.lldi.l2t,
1883                                         n, pdev, 0);
1884                 if (!ep->l2t)
1885                         goto out;
1886                 ep->mtu = pdev->mtu;
1887                 ep->tx_chan = cxgb4_port_chan(pdev);
1888                 ep->smac_idx = (cxgb4_port_viid(pdev) & 0x7F) << 1;
1889                 step = cdev->rdev.lldi.ntxq /
1890                         cdev->rdev.lldi.nchan;
1891                 ep->txq_idx = cxgb4_port_idx(pdev) * step;
1892                 step = cdev->rdev.lldi.nrxq /
1893                         cdev->rdev.lldi.nchan;
1894                 ep->ctrlq_idx = cxgb4_port_idx(pdev);
1895                 ep->rss_qid = cdev->rdev.lldi.rxq_ids[
1896                         cxgb4_port_idx(pdev) * step];
1897                 dev_put(pdev);
1898         } else {
1899                 ep->l2t = cxgb4_l2t_get(cdev->rdev.lldi.l2t,
1900                                         n, n->dev, 0);
1901                 if (!ep->l2t)
1902                         goto out;
1903                 ep->mtu = dst_mtu(dst);
1904                 ep->tx_chan = cxgb4_port_chan(n->dev);
1905                 ep->smac_idx = (cxgb4_port_viid(n->dev) & 0x7F) << 1;
1906                 step = cdev->rdev.lldi.ntxq /
1907                         cdev->rdev.lldi.nchan;
1908                 ep->txq_idx = cxgb4_port_idx(n->dev) * step;
1909                 ep->ctrlq_idx = cxgb4_port_idx(n->dev);
1910                 step = cdev->rdev.lldi.nrxq /
1911                         cdev->rdev.lldi.nchan;
1912                 ep->rss_qid = cdev->rdev.lldi.rxq_ids[
1913                         cxgb4_port_idx(n->dev) * step];
1914
1915                 if (clear_mpa_v1) {
1916                         ep->retry_with_mpa_v1 = 0;
1917                         ep->tried_with_mpa_v1 = 0;
1918                 }
1919         }
1920         err = 0;
1921 out:
1922         rcu_read_unlock();
1923
1924         neigh_release(n);
1925
1926         return err;
1927 }
1928
1929 static int pass_accept_req(struct c4iw_dev *dev, struct sk_buff *skb)
1930 {
1931         struct c4iw_ep *child_ep = NULL, *parent_ep;
1932         struct cpl_pass_accept_req *req = cplhdr(skb);
1933         unsigned int stid = GET_POPEN_TID(ntohl(req->tos_stid));
1934         struct tid_info *t = dev->rdev.lldi.tids;
1935         unsigned int hwtid = GET_TID(req);
1936         struct dst_entry *dst;
1937         struct rtable *rt;
1938         __be32 local_ip, peer_ip = 0;
1939         __be16 local_port, peer_port;
1940         int err;
1941         u16 peer_mss = ntohs(req->tcpopt.mss);
1942
1943         parent_ep = lookup_stid(t, stid);
1944         if (!parent_ep) {
1945                 PDBG("%s connect request on invalid stid %d\n", __func__, stid);
1946                 goto reject;
1947         }
1948         get_4tuple(req, &local_ip, &peer_ip, &local_port, &peer_port);
1949
1950         PDBG("%s parent ep %p hwtid %u laddr 0x%x raddr 0x%x lport %d " \
1951              "rport %d peer_mss %d\n", __func__, parent_ep, hwtid,
1952              ntohl(local_ip), ntohl(peer_ip), ntohs(local_port),
1953              ntohs(peer_port), peer_mss);
1954
1955         if (state_read(&parent_ep->com) != LISTEN) {
1956                 printk(KERN_ERR "%s - listening ep not in LISTEN\n",
1957                        __func__);
1958                 goto reject;
1959         }
1960
1961         /* Find output route */
1962         rt = find_route(dev, local_ip, peer_ip, local_port, peer_port,
1963                         GET_POPEN_TOS(ntohl(req->tos_stid)));
1964         if (!rt) {
1965                 printk(KERN_ERR MOD "%s - failed to find dst entry!\n",
1966                        __func__);
1967                 goto reject;
1968         }
1969         dst = &rt->dst;
1970
1971         child_ep = alloc_ep(sizeof(*child_ep), GFP_KERNEL);
1972         if (!child_ep) {
1973                 printk(KERN_ERR MOD "%s - failed to allocate ep entry!\n",
1974                        __func__);
1975                 dst_release(dst);
1976                 goto reject;
1977         }
1978
1979         err = import_ep(child_ep, peer_ip, dst, dev, false);
1980         if (err) {
1981                 printk(KERN_ERR MOD "%s - failed to allocate l2t entry!\n",
1982                        __func__);
1983                 dst_release(dst);
1984                 kfree(child_ep);
1985                 goto reject;
1986         }
1987
1988         if (peer_mss && child_ep->mtu > (peer_mss + 40))
1989                 child_ep->mtu = peer_mss + 40;
1990
1991         state_set(&child_ep->com, CONNECTING);
1992         child_ep->com.dev = dev;
1993         child_ep->com.cm_id = NULL;
1994         child_ep->com.local_addr.sin_family = PF_INET;
1995         child_ep->com.local_addr.sin_port = local_port;
1996         child_ep->com.local_addr.sin_addr.s_addr = local_ip;
1997         child_ep->com.remote_addr.sin_family = PF_INET;
1998         child_ep->com.remote_addr.sin_port = peer_port;
1999         child_ep->com.remote_addr.sin_addr.s_addr = peer_ip;
2000         c4iw_get_ep(&parent_ep->com);
2001         child_ep->parent_ep = parent_ep;
2002         child_ep->tos = GET_POPEN_TOS(ntohl(req->tos_stid));
2003         child_ep->dst = dst;
2004         child_ep->hwtid = hwtid;
2005
2006         PDBG("%s tx_chan %u smac_idx %u rss_qid %u\n", __func__,
2007              child_ep->tx_chan, child_ep->smac_idx, child_ep->rss_qid);
2008
2009         init_timer(&child_ep->timer);
2010         cxgb4_insert_tid(t, child_ep, hwtid);
2011         accept_cr(child_ep, peer_ip, skb, req);
2012         set_bit(PASS_ACCEPT_REQ, &child_ep->com.history);
2013         goto out;
2014 reject:
2015         reject_cr(dev, hwtid, peer_ip, skb);
2016 out:
2017         return 0;
2018 }
2019
2020 static int pass_establish(struct c4iw_dev *dev, struct sk_buff *skb)
2021 {
2022         struct c4iw_ep *ep;
2023         struct cpl_pass_establish *req = cplhdr(skb);
2024         struct tid_info *t = dev->rdev.lldi.tids;
2025         unsigned int tid = GET_TID(req);
2026
2027         ep = lookup_tid(t, tid);
2028         PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
2029         ep->snd_seq = be32_to_cpu(req->snd_isn);
2030         ep->rcv_seq = be32_to_cpu(req->rcv_isn);
2031
2032         PDBG("%s ep %p hwtid %u tcp_opt 0x%02x\n", __func__, ep, tid,
2033              ntohs(req->tcp_opt));
2034
2035         set_emss(ep, ntohs(req->tcp_opt));
2036         insert_handle(dev, &dev->hwtid_idr, ep, ep->hwtid);
2037
2038         dst_confirm(ep->dst);
2039         state_set(&ep->com, MPA_REQ_WAIT);
2040         start_ep_timer(ep);
2041         send_flowc(ep, skb);
2042         set_bit(PASS_ESTAB, &ep->com.history);
2043
2044         return 0;
2045 }
2046
2047 static int peer_close(struct c4iw_dev *dev, struct sk_buff *skb)
2048 {
2049         struct cpl_peer_close *hdr = cplhdr(skb);
2050         struct c4iw_ep *ep;
2051         struct c4iw_qp_attributes attrs;
2052         int disconnect = 1;
2053         int release = 0;
2054         struct tid_info *t = dev->rdev.lldi.tids;
2055         unsigned int tid = GET_TID(hdr);
2056         int ret;
2057
2058         ep = lookup_tid(t, tid);
2059         PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
2060         dst_confirm(ep->dst);
2061
2062         set_bit(PEER_CLOSE, &ep->com.history);
2063         mutex_lock(&ep->com.mutex);
2064         switch (ep->com.state) {
2065         case MPA_REQ_WAIT:
2066                 __state_set(&ep->com, CLOSING);
2067                 break;
2068         case MPA_REQ_SENT:
2069                 __state_set(&ep->com, CLOSING);
2070                 connect_reply_upcall(ep, -ECONNRESET);
2071                 break;
2072         case MPA_REQ_RCVD:
2073
2074                 /*
2075                  * We're gonna mark this puppy DEAD, but keep
2076                  * the reference on it until the ULP accepts or
2077                  * rejects the CR. Also wake up anyone waiting
2078                  * in rdma connection migration (see c4iw_accept_cr()).
2079                  */
2080                 __state_set(&ep->com, CLOSING);
2081                 PDBG("waking up ep %p tid %u\n", ep, ep->hwtid);
2082                 c4iw_wake_up(&ep->com.wr_wait, -ECONNRESET);
2083                 break;
2084         case MPA_REP_SENT:
2085                 __state_set(&ep->com, CLOSING);
2086                 PDBG("waking up ep %p tid %u\n", ep, ep->hwtid);
2087                 c4iw_wake_up(&ep->com.wr_wait, -ECONNRESET);
2088                 break;
2089         case FPDU_MODE:
2090                 start_ep_timer(ep);
2091                 __state_set(&ep->com, CLOSING);
2092                 attrs.next_state = C4IW_QP_STATE_CLOSING;
2093                 ret = c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
2094                                        C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
2095                 if (ret != -ECONNRESET) {
2096                         peer_close_upcall(ep);
2097                         disconnect = 1;
2098                 }
2099                 break;
2100         case ABORTING:
2101                 disconnect = 0;
2102                 break;
2103         case CLOSING:
2104                 __state_set(&ep->com, MORIBUND);
2105                 disconnect = 0;
2106                 break;
2107         case MORIBUND:
2108                 stop_ep_timer(ep);
2109                 if (ep->com.cm_id && ep->com.qp) {
2110                         attrs.next_state = C4IW_QP_STATE_IDLE;
2111                         c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
2112                                        C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
2113                 }
2114                 close_complete_upcall(ep);
2115                 __state_set(&ep->com, DEAD);
2116                 release = 1;
2117                 disconnect = 0;
2118                 break;
2119         case DEAD:
2120                 disconnect = 0;
2121                 break;
2122         default:
2123                 BUG_ON(1);
2124         }
2125         mutex_unlock(&ep->com.mutex);
2126         if (disconnect)
2127                 c4iw_ep_disconnect(ep, 0, GFP_KERNEL);
2128         if (release)
2129                 release_ep_resources(ep);
2130         return 0;
2131 }
2132
2133 /*
2134  * Returns whether an ABORT_REQ_RSS message is a negative advice.
2135  */
2136 static int is_neg_adv_abort(unsigned int status)
2137 {
2138         return status == CPL_ERR_RTX_NEG_ADVICE ||
2139                status == CPL_ERR_PERSIST_NEG_ADVICE;
2140 }
2141
2142 static int peer_abort(struct c4iw_dev *dev, struct sk_buff *skb)
2143 {
2144         struct cpl_abort_req_rss *req = cplhdr(skb);
2145         struct c4iw_ep *ep;
2146         struct cpl_abort_rpl *rpl;
2147         struct sk_buff *rpl_skb;
2148         struct c4iw_qp_attributes attrs;
2149         int ret;
2150         int release = 0;
2151         struct tid_info *t = dev->rdev.lldi.tids;
2152         unsigned int tid = GET_TID(req);
2153
2154         ep = lookup_tid(t, tid);
2155         if (is_neg_adv_abort(req->status)) {
2156                 PDBG("%s neg_adv_abort ep %p tid %u\n", __func__, ep,
2157                      ep->hwtid);
2158                 return 0;
2159         }
2160         PDBG("%s ep %p tid %u state %u\n", __func__, ep, ep->hwtid,
2161              ep->com.state);
2162         set_bit(PEER_ABORT, &ep->com.history);
2163
2164         /*
2165          * Wake up any threads in rdma_init() or rdma_fini().
2166          * However, this is not needed if com state is just
2167          * MPA_REQ_SENT
2168          */
2169         if (ep->com.state != MPA_REQ_SENT)
2170                 c4iw_wake_up(&ep->com.wr_wait, -ECONNRESET);
2171
2172         mutex_lock(&ep->com.mutex);
2173         switch (ep->com.state) {
2174         case CONNECTING:
2175                 break;
2176         case MPA_REQ_WAIT:
2177                 stop_ep_timer(ep);
2178                 break;
2179         case MPA_REQ_SENT:
2180                 stop_ep_timer(ep);
2181                 if (mpa_rev == 2 && ep->tried_with_mpa_v1)
2182                         connect_reply_upcall(ep, -ECONNRESET);
2183                 else {
2184                         /*
2185                          * we just don't send notification upwards because we
2186                          * want to retry with mpa_v1 without upper layers even
2187                          * knowing it.
2188                          *
2189                          * do some housekeeping so as to re-initiate the
2190                          * connection
2191                          */
2192                         PDBG("%s: mpa_rev=%d. Retrying with mpav1\n", __func__,
2193                              mpa_rev);
2194                         ep->retry_with_mpa_v1 = 1;
2195                 }
2196                 break;
2197         case MPA_REP_SENT:
2198                 break;
2199         case MPA_REQ_RCVD:
2200                 break;
2201         case MORIBUND:
2202         case CLOSING:
2203                 stop_ep_timer(ep);
2204                 /*FALLTHROUGH*/
2205         case FPDU_MODE:
2206                 if (ep->com.cm_id && ep->com.qp) {
2207                         attrs.next_state = C4IW_QP_STATE_ERROR;
2208                         ret = c4iw_modify_qp(ep->com.qp->rhp,
2209                                      ep->com.qp, C4IW_QP_ATTR_NEXT_STATE,
2210                                      &attrs, 1);
2211                         if (ret)
2212                                 printk(KERN_ERR MOD
2213                                        "%s - qp <- error failed!\n",
2214                                        __func__);
2215                 }
2216                 peer_abort_upcall(ep);
2217                 break;
2218         case ABORTING:
2219                 break;
2220         case DEAD:
2221                 PDBG("%s PEER_ABORT IN DEAD STATE!!!!\n", __func__);
2222                 mutex_unlock(&ep->com.mutex);
2223                 return 0;
2224         default:
2225                 BUG_ON(1);
2226                 break;
2227         }
2228         dst_confirm(ep->dst);
2229         if (ep->com.state != ABORTING) {
2230                 __state_set(&ep->com, DEAD);
2231                 /* we don't release if we want to retry with mpa_v1 */
2232                 if (!ep->retry_with_mpa_v1)
2233                         release = 1;
2234         }
2235         mutex_unlock(&ep->com.mutex);
2236
2237         rpl_skb = get_skb(skb, sizeof(*rpl), GFP_KERNEL);
2238         if (!rpl_skb) {
2239                 printk(KERN_ERR MOD "%s - cannot allocate skb!\n",
2240                        __func__);
2241                 release = 1;
2242                 goto out;
2243         }
2244         set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
2245         rpl = (struct cpl_abort_rpl *) skb_put(rpl_skb, sizeof(*rpl));
2246         INIT_TP_WR(rpl, ep->hwtid);
2247         OPCODE_TID(rpl) = cpu_to_be32(MK_OPCODE_TID(CPL_ABORT_RPL, ep->hwtid));
2248         rpl->cmd = CPL_ABORT_NO_RST;
2249         c4iw_ofld_send(&ep->com.dev->rdev, rpl_skb);
2250 out:
2251         if (release)
2252                 release_ep_resources(ep);
2253
2254         /* retry with mpa-v1 */
2255         if (ep && ep->retry_with_mpa_v1) {
2256                 cxgb4_remove_tid(ep->com.dev->rdev.lldi.tids, 0, ep->hwtid);
2257                 dst_release(ep->dst);
2258                 cxgb4_l2t_release(ep->l2t);
2259                 c4iw_reconnect(ep);
2260         }
2261
2262         return 0;
2263 }
2264
2265 static int close_con_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
2266 {
2267         struct c4iw_ep *ep;
2268         struct c4iw_qp_attributes attrs;
2269         struct cpl_close_con_rpl *rpl = cplhdr(skb);
2270         int release = 0;
2271         struct tid_info *t = dev->rdev.lldi.tids;
2272         unsigned int tid = GET_TID(rpl);
2273
2274         ep = lookup_tid(t, tid);
2275
2276         PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
2277         BUG_ON(!ep);
2278
2279         /* The cm_id may be null if we failed to connect */
2280         mutex_lock(&ep->com.mutex);
2281         switch (ep->com.state) {
2282         case CLOSING:
2283                 __state_set(&ep->com, MORIBUND);
2284                 break;
2285         case MORIBUND:
2286                 stop_ep_timer(ep);
2287                 if ((ep->com.cm_id) && (ep->com.qp)) {
2288                         attrs.next_state = C4IW_QP_STATE_IDLE;
2289                         c4iw_modify_qp(ep->com.qp->rhp,
2290                                              ep->com.qp,
2291                                              C4IW_QP_ATTR_NEXT_STATE,
2292                                              &attrs, 1);
2293                 }
2294                 close_complete_upcall(ep);
2295                 __state_set(&ep->com, DEAD);
2296                 release = 1;
2297                 break;
2298         case ABORTING:
2299         case DEAD:
2300                 break;
2301         default:
2302                 BUG_ON(1);
2303                 break;
2304         }
2305         mutex_unlock(&ep->com.mutex);
2306         if (release)
2307                 release_ep_resources(ep);
2308         return 0;
2309 }
2310
2311 static int terminate(struct c4iw_dev *dev, struct sk_buff *skb)
2312 {
2313         struct cpl_rdma_terminate *rpl = cplhdr(skb);
2314         struct tid_info *t = dev->rdev.lldi.tids;
2315         unsigned int tid = GET_TID(rpl);
2316         struct c4iw_ep *ep;
2317         struct c4iw_qp_attributes attrs;
2318
2319         ep = lookup_tid(t, tid);
2320         BUG_ON(!ep);
2321
2322         if (ep && ep->com.qp) {
2323                 printk(KERN_WARNING MOD "TERM received tid %u qpid %u\n", tid,
2324                        ep->com.qp->wq.sq.qid);
2325                 attrs.next_state = C4IW_QP_STATE_TERMINATE;
2326                 c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
2327                                C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
2328         } else
2329                 printk(KERN_WARNING MOD "TERM received tid %u no ep/qp\n", tid);
2330
2331         return 0;
2332 }
2333
2334 /*
2335  * Upcall from the adapter indicating data has been transmitted.
2336  * For us its just the single MPA request or reply.  We can now free
2337  * the skb holding the mpa message.
2338  */
2339 static int fw4_ack(struct c4iw_dev *dev, struct sk_buff *skb)
2340 {
2341         struct c4iw_ep *ep;
2342         struct cpl_fw4_ack *hdr = cplhdr(skb);
2343         u8 credits = hdr->credits;
2344         unsigned int tid = GET_TID(hdr);
2345         struct tid_info *t = dev->rdev.lldi.tids;
2346
2347
2348         ep = lookup_tid(t, tid);
2349         PDBG("%s ep %p tid %u credits %u\n", __func__, ep, ep->hwtid, credits);
2350         if (credits == 0) {
2351                 PDBG("%s 0 credit ack ep %p tid %u state %u\n",
2352                      __func__, ep, ep->hwtid, state_read(&ep->com));
2353                 return 0;
2354         }
2355
2356         dst_confirm(ep->dst);
2357         if (ep->mpa_skb) {
2358                 PDBG("%s last streaming msg ack ep %p tid %u state %u "
2359                      "initiator %u freeing skb\n", __func__, ep, ep->hwtid,
2360                      state_read(&ep->com), ep->mpa_attr.initiator ? 1 : 0);
2361                 kfree_skb(ep->mpa_skb);
2362                 ep->mpa_skb = NULL;
2363         }
2364         return 0;
2365 }
2366
2367 int c4iw_reject_cr(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
2368 {
2369         int err;
2370         struct c4iw_ep *ep = to_ep(cm_id);
2371         PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
2372
2373         if (state_read(&ep->com) == DEAD) {
2374                 c4iw_put_ep(&ep->com);
2375                 return -ECONNRESET;
2376         }
2377         set_bit(ULP_REJECT, &ep->com.history);
2378         BUG_ON(state_read(&ep->com) != MPA_REQ_RCVD);
2379         if (mpa_rev == 0)
2380                 abort_connection(ep, NULL, GFP_KERNEL);
2381         else {
2382                 err = send_mpa_reject(ep, pdata, pdata_len);
2383                 err = c4iw_ep_disconnect(ep, 0, GFP_KERNEL);
2384         }
2385         c4iw_put_ep(&ep->com);
2386         return 0;
2387 }
2388
2389 int c4iw_accept_cr(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2390 {
2391         int err;
2392         struct c4iw_qp_attributes attrs;
2393         enum c4iw_qp_attr_mask mask;
2394         struct c4iw_ep *ep = to_ep(cm_id);
2395         struct c4iw_dev *h = to_c4iw_dev(cm_id->device);
2396         struct c4iw_qp *qp = get_qhp(h, conn_param->qpn);
2397
2398         PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
2399         if (state_read(&ep->com) == DEAD) {
2400                 err = -ECONNRESET;
2401                 goto err;
2402         }
2403
2404         BUG_ON(state_read(&ep->com) != MPA_REQ_RCVD);
2405         BUG_ON(!qp);
2406
2407         set_bit(ULP_ACCEPT, &ep->com.history);
2408         if ((conn_param->ord > c4iw_max_read_depth) ||
2409             (conn_param->ird > c4iw_max_read_depth)) {
2410                 abort_connection(ep, NULL, GFP_KERNEL);
2411                 err = -EINVAL;
2412                 goto err;
2413         }
2414
2415         if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn) {
2416                 if (conn_param->ord > ep->ird) {
2417                         ep->ird = conn_param->ird;
2418                         ep->ord = conn_param->ord;
2419                         send_mpa_reject(ep, conn_param->private_data,
2420                                         conn_param->private_data_len);
2421                         abort_connection(ep, NULL, GFP_KERNEL);
2422                         err = -ENOMEM;
2423                         goto err;
2424                 }
2425                 if (conn_param->ird > ep->ord) {
2426                         if (!ep->ord)
2427                                 conn_param->ird = 1;
2428                         else {
2429                                 abort_connection(ep, NULL, GFP_KERNEL);
2430                                 err = -ENOMEM;
2431                                 goto err;
2432                         }
2433                 }
2434
2435         }
2436         ep->ird = conn_param->ird;
2437         ep->ord = conn_param->ord;
2438
2439         if (ep->mpa_attr.version != 2)
2440                 if (peer2peer && ep->ird == 0)
2441                         ep->ird = 1;
2442
2443         PDBG("%s %d ird %d ord %d\n", __func__, __LINE__, ep->ird, ep->ord);
2444
2445         cm_id->add_ref(cm_id);
2446         ep->com.cm_id = cm_id;
2447         ep->com.qp = qp;
2448         ref_qp(ep);
2449
2450         /* bind QP to EP and move to RTS */
2451         attrs.mpa_attr = ep->mpa_attr;
2452         attrs.max_ird = ep->ird;
2453         attrs.max_ord = ep->ord;
2454         attrs.llp_stream_handle = ep;
2455         attrs.next_state = C4IW_QP_STATE_RTS;
2456
2457         /* bind QP and TID with INIT_WR */
2458         mask = C4IW_QP_ATTR_NEXT_STATE |
2459                              C4IW_QP_ATTR_LLP_STREAM_HANDLE |
2460                              C4IW_QP_ATTR_MPA_ATTR |
2461                              C4IW_QP_ATTR_MAX_IRD |
2462                              C4IW_QP_ATTR_MAX_ORD;
2463
2464         err = c4iw_modify_qp(ep->com.qp->rhp,
2465                              ep->com.qp, mask, &attrs, 1);
2466         if (err)
2467                 goto err1;
2468         err = send_mpa_reply(ep, conn_param->private_data,
2469                              conn_param->private_data_len);
2470         if (err)
2471                 goto err1;
2472
2473         state_set(&ep->com, FPDU_MODE);
2474         established_upcall(ep);
2475         c4iw_put_ep(&ep->com);
2476         return 0;
2477 err1:
2478         ep->com.cm_id = NULL;
2479         cm_id->rem_ref(cm_id);
2480 err:
2481         c4iw_put_ep(&ep->com);
2482         return err;
2483 }
2484
2485 int c4iw_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2486 {
2487         struct c4iw_dev *dev = to_c4iw_dev(cm_id->device);
2488         struct c4iw_ep *ep;
2489         struct rtable *rt;
2490         int err = 0;
2491
2492         if ((conn_param->ord > c4iw_max_read_depth) ||
2493             (conn_param->ird > c4iw_max_read_depth)) {
2494                 err = -EINVAL;
2495                 goto out;
2496         }
2497         ep = alloc_ep(sizeof(*ep), GFP_KERNEL);
2498         if (!ep) {
2499                 printk(KERN_ERR MOD "%s - cannot alloc ep.\n", __func__);
2500                 err = -ENOMEM;
2501                 goto out;
2502         }
2503         init_timer(&ep->timer);
2504         ep->plen = conn_param->private_data_len;
2505         if (ep->plen)
2506                 memcpy(ep->mpa_pkt + sizeof(struct mpa_message),
2507                        conn_param->private_data, ep->plen);
2508         ep->ird = conn_param->ird;
2509         ep->ord = conn_param->ord;
2510
2511         if (peer2peer && ep->ord == 0)
2512                 ep->ord = 1;
2513
2514         cm_id->add_ref(cm_id);
2515         ep->com.dev = dev;
2516         ep->com.cm_id = cm_id;
2517         ep->com.qp = get_qhp(dev, conn_param->qpn);
2518         BUG_ON(!ep->com.qp);
2519         ref_qp(ep);
2520         PDBG("%s qpn 0x%x qp %p cm_id %p\n", __func__, conn_param->qpn,
2521              ep->com.qp, cm_id);
2522
2523         /*
2524          * Allocate an active TID to initiate a TCP connection.
2525          */
2526         ep->atid = cxgb4_alloc_atid(dev->rdev.lldi.tids, ep);
2527         if (ep->atid == -1) {
2528                 printk(KERN_ERR MOD "%s - cannot alloc atid.\n", __func__);
2529                 err = -ENOMEM;
2530                 goto fail2;
2531         }
2532         insert_handle(dev, &dev->atid_idr, ep, ep->atid);
2533
2534         PDBG("%s saddr 0x%x sport 0x%x raddr 0x%x rport 0x%x\n", __func__,
2535              ntohl(cm_id->local_addr.sin_addr.s_addr),
2536              ntohs(cm_id->local_addr.sin_port),
2537              ntohl(cm_id->remote_addr.sin_addr.s_addr),
2538              ntohs(cm_id->remote_addr.sin_port));
2539
2540         /* find a route */
2541         rt = find_route(dev,
2542                         cm_id->local_addr.sin_addr.s_addr,
2543                         cm_id->remote_addr.sin_addr.s_addr,
2544                         cm_id->local_addr.sin_port,
2545                         cm_id->remote_addr.sin_port, 0);
2546         if (!rt) {
2547                 printk(KERN_ERR MOD "%s - cannot find route.\n", __func__);
2548                 err = -EHOSTUNREACH;
2549                 goto fail3;
2550         }
2551         ep->dst = &rt->dst;
2552
2553         err = import_ep(ep, cm_id->remote_addr.sin_addr.s_addr,
2554                         ep->dst, ep->com.dev, true);
2555         if (err) {
2556                 printk(KERN_ERR MOD "%s - cannot alloc l2e.\n", __func__);
2557                 goto fail4;
2558         }
2559
2560         PDBG("%s txq_idx %u tx_chan %u smac_idx %u rss_qid %u l2t_idx %u\n",
2561                 __func__, ep->txq_idx, ep->tx_chan, ep->smac_idx, ep->rss_qid,
2562                 ep->l2t->idx);
2563
2564         state_set(&ep->com, CONNECTING);
2565         ep->tos = 0;
2566         ep->com.local_addr = cm_id->local_addr;
2567         ep->com.remote_addr = cm_id->remote_addr;
2568
2569         /* send connect request to rnic */
2570         err = send_connect(ep);
2571         if (!err)
2572                 goto out;
2573
2574         cxgb4_l2t_release(ep->l2t);
2575 fail4:
2576         dst_release(ep->dst);
2577 fail3:
2578         remove_handle(ep->com.dev, &ep->com.dev->atid_idr, ep->atid);
2579         cxgb4_free_atid(ep->com.dev->rdev.lldi.tids, ep->atid);
2580 fail2:
2581         cm_id->rem_ref(cm_id);
2582         c4iw_put_ep(&ep->com);
2583 out:
2584         return err;
2585 }
2586
2587 int c4iw_create_listen(struct iw_cm_id *cm_id, int backlog)
2588 {
2589         int err = 0;
2590         struct c4iw_dev *dev = to_c4iw_dev(cm_id->device);
2591         struct c4iw_listen_ep *ep;
2592
2593         might_sleep();
2594
2595         ep = alloc_ep(sizeof(*ep), GFP_KERNEL);
2596         if (!ep) {
2597                 printk(KERN_ERR MOD "%s - cannot alloc ep.\n", __func__);
2598                 err = -ENOMEM;
2599                 goto fail1;
2600         }
2601         PDBG("%s ep %p\n", __func__, ep);
2602         cm_id->add_ref(cm_id);
2603         ep->com.cm_id = cm_id;
2604         ep->com.dev = dev;
2605         ep->backlog = backlog;
2606         ep->com.local_addr = cm_id->local_addr;
2607
2608         /*
2609          * Allocate a server TID.
2610          */
2611         if (dev->rdev.lldi.enable_fw_ofld_conn)
2612                 ep->stid = cxgb4_alloc_sftid(dev->rdev.lldi.tids, PF_INET, ep);
2613         else
2614                 ep->stid = cxgb4_alloc_stid(dev->rdev.lldi.tids, PF_INET, ep);
2615
2616         if (ep->stid == -1) {
2617                 printk(KERN_ERR MOD "%s - cannot alloc stid.\n", __func__);
2618                 err = -ENOMEM;
2619                 goto fail2;
2620         }
2621         insert_handle(dev, &dev->stid_idr, ep, ep->stid);
2622         state_set(&ep->com, LISTEN);
2623         if (dev->rdev.lldi.enable_fw_ofld_conn) {
2624                 do {
2625                         err = cxgb4_create_server_filter(
2626                                 ep->com.dev->rdev.lldi.ports[0], ep->stid,
2627                                 ep->com.local_addr.sin_addr.s_addr,
2628                                 ep->com.local_addr.sin_port,
2629                                 0,
2630                                 ep->com.dev->rdev.lldi.rxq_ids[0],
2631                                 0,
2632                                 0);
2633                         if (err == -EBUSY) {
2634                                 set_current_state(TASK_UNINTERRUPTIBLE);
2635                                 schedule_timeout(usecs_to_jiffies(100));
2636                         }
2637                 } while (err == -EBUSY);
2638         } else {
2639                 c4iw_init_wr_wait(&ep->com.wr_wait);
2640                 err = cxgb4_create_server(ep->com.dev->rdev.lldi.ports[0],
2641                                 ep->stid, ep->com.local_addr.sin_addr.s_addr,
2642                                 ep->com.local_addr.sin_port,
2643                                 0,
2644                                 ep->com.dev->rdev.lldi.rxq_ids[0]);
2645                 if (!err)
2646                         err = c4iw_wait_for_reply(&ep->com.dev->rdev,
2647                                                   &ep->com.wr_wait,
2648                                                   0, 0, __func__);
2649         }
2650         if (!err) {
2651                 cm_id->provider_data = ep;
2652                 goto out;
2653         }
2654         pr_err("%s cxgb4_create_server/filter failed err %d " \
2655                "stid %d laddr %08x lport %d\n", \
2656                __func__, err, ep->stid,
2657                ntohl(ep->com.local_addr.sin_addr.s_addr),
2658                ntohs(ep->com.local_addr.sin_port));
2659         cxgb4_free_stid(ep->com.dev->rdev.lldi.tids, ep->stid, PF_INET);
2660 fail2:
2661         cm_id->rem_ref(cm_id);
2662         c4iw_put_ep(&ep->com);
2663 fail1:
2664 out:
2665         return err;
2666 }
2667
2668 int c4iw_destroy_listen(struct iw_cm_id *cm_id)
2669 {
2670         int err;
2671         struct c4iw_listen_ep *ep = to_listen_ep(cm_id);
2672
2673         PDBG("%s ep %p\n", __func__, ep);
2674
2675         might_sleep();
2676         state_set(&ep->com, DEAD);
2677         if (ep->com.dev->rdev.lldi.enable_fw_ofld_conn) {
2678                 err = cxgb4_remove_server_filter(
2679                         ep->com.dev->rdev.lldi.ports[0], ep->stid,
2680                         ep->com.dev->rdev.lldi.rxq_ids[0], 0);
2681         } else {
2682                 c4iw_init_wr_wait(&ep->com.wr_wait);
2683                 err = listen_stop(ep);
2684                 if (err)
2685                         goto done;
2686                 err = c4iw_wait_for_reply(&ep->com.dev->rdev, &ep->com.wr_wait,
2687                                           0, 0, __func__);
2688         }
2689         remove_handle(ep->com.dev, &ep->com.dev->stid_idr, ep->stid);
2690         cxgb4_free_stid(ep->com.dev->rdev.lldi.tids, ep->stid, PF_INET);
2691 done:
2692         cm_id->rem_ref(cm_id);
2693         c4iw_put_ep(&ep->com);
2694         return err;
2695 }
2696
2697 int c4iw_ep_disconnect(struct c4iw_ep *ep, int abrupt, gfp_t gfp)
2698 {
2699         int ret = 0;
2700         int close = 0;
2701         int fatal = 0;
2702         struct c4iw_rdev *rdev;
2703
2704         mutex_lock(&ep->com.mutex);
2705
2706         PDBG("%s ep %p state %s, abrupt %d\n", __func__, ep,
2707              states[ep->com.state], abrupt);
2708
2709         rdev = &ep->com.dev->rdev;
2710         if (c4iw_fatal_error(rdev)) {
2711                 fatal = 1;
2712                 close_complete_upcall(ep);
2713                 ep->com.state = DEAD;
2714         }
2715         switch (ep->com.state) {
2716         case MPA_REQ_WAIT:
2717         case MPA_REQ_SENT:
2718         case MPA_REQ_RCVD:
2719         case MPA_REP_SENT:
2720         case FPDU_MODE:
2721                 close = 1;
2722                 if (abrupt)
2723                         ep->com.state = ABORTING;
2724                 else {
2725                         ep->com.state = CLOSING;
2726                         start_ep_timer(ep);
2727                 }
2728                 set_bit(CLOSE_SENT, &ep->com.flags);
2729                 break;
2730         case CLOSING:
2731                 if (!test_and_set_bit(CLOSE_SENT, &ep->com.flags)) {
2732                         close = 1;
2733                         if (abrupt) {
2734                                 stop_ep_timer(ep);
2735                                 ep->com.state = ABORTING;
2736                         } else
2737                                 ep->com.state = MORIBUND;
2738                 }
2739                 break;
2740         case MORIBUND:
2741         case ABORTING:
2742         case DEAD:
2743                 PDBG("%s ignoring disconnect ep %p state %u\n",
2744                      __func__, ep, ep->com.state);
2745                 break;
2746         default:
2747                 BUG();
2748                 break;
2749         }
2750
2751         if (close) {
2752                 if (abrupt) {
2753                         set_bit(EP_DISC_ABORT, &ep->com.history);
2754                         close_complete_upcall(ep);
2755                         ret = send_abort(ep, NULL, gfp);
2756                 } else {
2757                         set_bit(EP_DISC_CLOSE, &ep->com.history);
2758                         ret = send_halfclose(ep, gfp);
2759                 }
2760                 if (ret)
2761                         fatal = 1;
2762         }
2763         mutex_unlock(&ep->com.mutex);
2764         if (fatal)
2765                 release_ep_resources(ep);
2766         return ret;
2767 }
2768
2769 static void active_ofld_conn_reply(struct c4iw_dev *dev, struct sk_buff *skb,
2770                         struct cpl_fw6_msg_ofld_connection_wr_rpl *req)
2771 {
2772         struct c4iw_ep *ep;
2773         int atid = be32_to_cpu(req->tid);
2774
2775         ep = (struct c4iw_ep *)lookup_atid(dev->rdev.lldi.tids, req->tid);
2776         if (!ep)
2777                 return;
2778
2779         switch (req->retval) {
2780         case FW_ENOMEM:
2781                 set_bit(ACT_RETRY_NOMEM, &ep->com.history);
2782                 if (ep->retry_count++ < ACT_OPEN_RETRY_COUNT) {
2783                         send_fw_act_open_req(ep, atid);
2784                         return;
2785                 }
2786         case FW_EADDRINUSE:
2787                 set_bit(ACT_RETRY_INUSE, &ep->com.history);
2788                 if (ep->retry_count++ < ACT_OPEN_RETRY_COUNT) {
2789                         send_fw_act_open_req(ep, atid);
2790                         return;
2791                 }
2792                 break;
2793         default:
2794                 pr_info("%s unexpected ofld conn wr retval %d\n",
2795                        __func__, req->retval);
2796                 break;
2797         }
2798         pr_err("active ofld_connect_wr failure %d atid %d\n",
2799                req->retval, atid);
2800         mutex_lock(&dev->rdev.stats.lock);
2801         dev->rdev.stats.act_ofld_conn_fails++;
2802         mutex_unlock(&dev->rdev.stats.lock);
2803         connect_reply_upcall(ep, status2errno(req->retval));
2804         state_set(&ep->com, DEAD);
2805         remove_handle(dev, &dev->atid_idr, atid);
2806         cxgb4_free_atid(dev->rdev.lldi.tids, atid);
2807         dst_release(ep->dst);
2808         cxgb4_l2t_release(ep->l2t);
2809         c4iw_put_ep(&ep->com);
2810 }
2811
2812 static void passive_ofld_conn_reply(struct c4iw_dev *dev, struct sk_buff *skb,
2813                         struct cpl_fw6_msg_ofld_connection_wr_rpl *req)
2814 {
2815         struct sk_buff *rpl_skb;
2816         struct cpl_pass_accept_req *cpl;
2817         int ret;
2818
2819         rpl_skb = (struct sk_buff *)cpu_to_be64(req->cookie);
2820         BUG_ON(!rpl_skb);
2821         if (req->retval) {
2822                 PDBG("%s passive open failure %d\n", __func__, req->retval);
2823                 mutex_lock(&dev->rdev.stats.lock);
2824                 dev->rdev.stats.pas_ofld_conn_fails++;
2825                 mutex_unlock(&dev->rdev.stats.lock);
2826                 kfree_skb(rpl_skb);
2827         } else {
2828                 cpl = (struct cpl_pass_accept_req *)cplhdr(rpl_skb);
2829                 OPCODE_TID(cpl) = htonl(MK_OPCODE_TID(CPL_PASS_ACCEPT_REQ,
2830                                                       htonl(req->tid)));
2831                 ret = pass_accept_req(dev, rpl_skb);
2832                 if (!ret)
2833                         kfree_skb(rpl_skb);
2834         }
2835         return;
2836 }
2837
2838 static int deferred_fw6_msg(struct c4iw_dev *dev, struct sk_buff *skb)
2839 {
2840         struct cpl_fw6_msg *rpl = cplhdr(skb);
2841         struct cpl_fw6_msg_ofld_connection_wr_rpl *req;
2842
2843         switch (rpl->type) {
2844         case FW6_TYPE_CQE:
2845                 c4iw_ev_dispatch(dev, (struct t4_cqe *)&rpl->data[0]);
2846                 break;
2847         case FW6_TYPE_OFLD_CONNECTION_WR_RPL:
2848                 req = (struct cpl_fw6_msg_ofld_connection_wr_rpl *)rpl->data;
2849                 switch (req->t_state) {
2850                 case TCP_SYN_SENT:
2851                         active_ofld_conn_reply(dev, skb, req);
2852                         break;
2853                 case TCP_SYN_RECV:
2854                         passive_ofld_conn_reply(dev, skb, req);
2855                         break;
2856                 default:
2857                         pr_err("%s unexpected ofld conn wr state %d\n",
2858                                __func__, req->t_state);
2859                         break;
2860                 }
2861                 break;
2862         }
2863         return 0;
2864 }
2865
2866 static void build_cpl_pass_accept_req(struct sk_buff *skb, int stid , u8 tos)
2867 {
2868         u32 l2info;
2869         u16 vlantag, len, hdr_len;
2870         u8 intf;
2871         struct cpl_rx_pkt *cpl = cplhdr(skb);
2872         struct cpl_pass_accept_req *req;
2873         struct tcp_options_received tmp_opt;
2874
2875         /* Store values from cpl_rx_pkt in temporary location. */
2876         vlantag = cpl->vlan;
2877         len = cpl->len;
2878         l2info  = cpl->l2info;
2879         hdr_len = cpl->hdr_len;
2880         intf = cpl->iff;
2881
2882         __skb_pull(skb, sizeof(*req) + sizeof(struct rss_header));
2883
2884         /*
2885          * We need to parse the TCP options from SYN packet.
2886          * to generate cpl_pass_accept_req.
2887          */
2888         memset(&tmp_opt, 0, sizeof(tmp_opt));
2889         tcp_clear_options(&tmp_opt);
2890         tcp_parse_options(skb, &tmp_opt, 0, 0, NULL);
2891
2892         req = (struct cpl_pass_accept_req *)__skb_push(skb, sizeof(*req));
2893         memset(req, 0, sizeof(*req));
2894         req->l2info = cpu_to_be16(V_SYN_INTF(intf) |
2895                          V_SYN_MAC_IDX(G_RX_MACIDX(htonl(l2info))) |
2896                          F_SYN_XACT_MATCH);
2897         req->hdr_len = cpu_to_be32(V_SYN_RX_CHAN(G_RX_CHAN(htonl(l2info))) |
2898                                 V_TCP_HDR_LEN(G_RX_TCPHDR_LEN(htons(hdr_len))) |
2899                                 V_IP_HDR_LEN(G_RX_IPHDR_LEN(htons(hdr_len))) |
2900                                 V_ETH_HDR_LEN(G_RX_ETHHDR_LEN(htonl(l2info))));
2901         req->vlan = vlantag;
2902         req->len = len;
2903         req->tos_stid = cpu_to_be32(PASS_OPEN_TID(stid) |
2904                                     PASS_OPEN_TOS(tos));
2905         req->tcpopt.mss = htons(tmp_opt.mss_clamp);
2906         if (tmp_opt.wscale_ok)
2907                 req->tcpopt.wsf = tmp_opt.snd_wscale;
2908         req->tcpopt.tstamp = tmp_opt.saw_tstamp;
2909         if (tmp_opt.sack_ok)
2910                 req->tcpopt.sack = 1;
2911         OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_ACCEPT_REQ, 0));
2912         return;
2913 }
2914
2915 static void send_fw_pass_open_req(struct c4iw_dev *dev, struct sk_buff *skb,
2916                                   __be32 laddr, __be16 lport,
2917                                   __be32 raddr, __be16 rport,
2918                                   u32 rcv_isn, u32 filter, u16 window,
2919                                   u32 rss_qid, u8 port_id)
2920 {
2921         struct sk_buff *req_skb;
2922         struct fw_ofld_connection_wr *req;
2923         struct cpl_pass_accept_req *cpl = cplhdr(skb);
2924
2925         req_skb = alloc_skb(sizeof(struct fw_ofld_connection_wr), GFP_KERNEL);
2926         req = (struct fw_ofld_connection_wr *)__skb_put(req_skb, sizeof(*req));
2927         memset(req, 0, sizeof(*req));
2928         req->op_compl = htonl(V_WR_OP(FW_OFLD_CONNECTION_WR) | FW_WR_COMPL(1));
2929         req->len16_pkd = htonl(FW_WR_LEN16(DIV_ROUND_UP(sizeof(*req), 16)));
2930         req->le.version_cpl = htonl(F_FW_OFLD_CONNECTION_WR_CPL);
2931         req->le.filter = filter;
2932         req->le.lport = lport;
2933         req->le.pport = rport;
2934         req->le.u.ipv4.lip = laddr;
2935         req->le.u.ipv4.pip = raddr;
2936         req->tcb.rcv_nxt = htonl(rcv_isn + 1);
2937         req->tcb.rcv_adv = htons(window);
2938         req->tcb.t_state_to_astid =
2939                  htonl(V_FW_OFLD_CONNECTION_WR_T_STATE(TCP_SYN_RECV) |
2940                         V_FW_OFLD_CONNECTION_WR_RCV_SCALE(cpl->tcpopt.wsf) |
2941                         V_FW_OFLD_CONNECTION_WR_ASTID(
2942                         GET_PASS_OPEN_TID(ntohl(cpl->tos_stid))));
2943
2944         /*
2945          * We store the qid in opt2 which will be used by the firmware
2946          * to send us the wr response.
2947          */
2948         req->tcb.opt2 = htonl(V_RSS_QUEUE(rss_qid));
2949
2950         /*
2951          * We initialize the MSS index in TCB to 0xF.
2952          * So that when driver sends cpl_pass_accept_rpl
2953          * TCB picks up the correct value. If this was 0
2954          * TP will ignore any value > 0 for MSS index.
2955          */
2956         req->tcb.opt0 = cpu_to_be64(V_MSS_IDX(0xF));
2957         req->cookie = cpu_to_be64((u64)skb);
2958
2959         set_wr_txq(req_skb, CPL_PRIORITY_CONTROL, port_id);
2960         cxgb4_ofld_send(dev->rdev.lldi.ports[0], req_skb);
2961 }
2962
2963 /*
2964  * Handler for CPL_RX_PKT message. Need to handle cpl_rx_pkt
2965  * messages when a filter is being used instead of server to
2966  * redirect a syn packet. When packets hit filter they are redirected
2967  * to the offload queue and driver tries to establish the connection
2968  * using firmware work request.
2969  */
2970 static int rx_pkt(struct c4iw_dev *dev, struct sk_buff *skb)
2971 {
2972         int stid;
2973         unsigned int filter;
2974         struct ethhdr *eh = NULL;
2975         struct vlan_ethhdr *vlan_eh = NULL;
2976         struct iphdr *iph;
2977         struct tcphdr *tcph;
2978         struct rss_header *rss = (void *)skb->data;
2979         struct cpl_rx_pkt *cpl = (void *)skb->data;
2980         struct cpl_pass_accept_req *req = (void *)(rss + 1);
2981         struct l2t_entry *e;
2982         struct dst_entry *dst;
2983         struct rtable *rt;
2984         struct c4iw_ep *lep;
2985         u16 window;
2986         struct port_info *pi;
2987         struct net_device *pdev;
2988         u16 rss_qid;
2989         int step;
2990         u32 tx_chan;
2991         struct neighbour *neigh;
2992
2993         /* Drop all non-SYN packets */
2994         if (!(cpl->l2info & cpu_to_be32(F_RXF_SYN)))
2995                 goto reject;
2996
2997         /*
2998          * Drop all packets which did not hit the filter.
2999          * Unlikely to happen.
3000          */
3001         if (!(rss->filter_hit && rss->filter_tid))
3002                 goto reject;
3003
3004         /*
3005          * Calculate the server tid from filter hit index from cpl_rx_pkt.
3006          */
3007         stid = cpu_to_be32(rss->hash_val) - dev->rdev.lldi.tids->sftid_base
3008                                           + dev->rdev.lldi.tids->nstids;
3009
3010         lep = (struct c4iw_ep *)lookup_stid(dev->rdev.lldi.tids, stid);
3011         if (!lep) {
3012                 PDBG("%s connect request on invalid stid %d\n", __func__, stid);
3013                 goto reject;
3014         }
3015
3016         if (G_RX_ETHHDR_LEN(ntohl(cpl->l2info)) == ETH_HLEN) {
3017                 eh = (struct ethhdr *)(req + 1);
3018                 iph = (struct iphdr *)(eh + 1);
3019         } else {
3020                 vlan_eh = (struct vlan_ethhdr *)(req + 1);
3021                 iph = (struct iphdr *)(vlan_eh + 1);
3022                 skb->vlan_tci = ntohs(cpl->vlan);
3023         }
3024
3025         if (iph->version != 0x4)
3026                 goto reject;
3027
3028         tcph = (struct tcphdr *)(iph + 1);
3029         skb_set_network_header(skb, (void *)iph - (void *)rss);
3030         skb_set_transport_header(skb, (void *)tcph - (void *)rss);
3031         skb_get(skb);
3032
3033         PDBG("%s lip 0x%x lport %u pip 0x%x pport %u tos %d\n", __func__,
3034              ntohl(iph->daddr), ntohs(tcph->dest), ntohl(iph->saddr),
3035              ntohs(tcph->source), iph->tos);
3036
3037         rt = find_route(dev, iph->daddr, iph->saddr, tcph->dest, tcph->source,
3038                         iph->tos);
3039         if (!rt) {
3040                 pr_err("%s - failed to find dst entry!\n",
3041                        __func__);
3042                 goto reject;
3043         }
3044         dst = &rt->dst;
3045         neigh = dst_neigh_lookup_skb(dst, skb);
3046
3047         if (neigh->dev->flags & IFF_LOOPBACK) {
3048                 pdev = ip_dev_find(&init_net, iph->daddr);
3049                 e = cxgb4_l2t_get(dev->rdev.lldi.l2t, neigh,
3050                                     pdev, 0);
3051                 pi = (struct port_info *)netdev_priv(pdev);
3052                 tx_chan = cxgb4_port_chan(pdev);
3053                 dev_put(pdev);
3054         } else {
3055                 e = cxgb4_l2t_get(dev->rdev.lldi.l2t, neigh,
3056                                         neigh->dev, 0);
3057                 pi = (struct port_info *)netdev_priv(neigh->dev);
3058                 tx_chan = cxgb4_port_chan(neigh->dev);
3059         }
3060         if (!e) {
3061                 pr_err("%s - failed to allocate l2t entry!\n",
3062                        __func__);
3063                 goto free_dst;
3064         }
3065
3066         step = dev->rdev.lldi.nrxq / dev->rdev.lldi.nchan;
3067         rss_qid = dev->rdev.lldi.rxq_ids[pi->port_id * step];
3068         window = htons(tcph->window);
3069
3070         /* Calcuate filter portion for LE region. */
3071         filter = cpu_to_be32(select_ntuple(dev, dst, e));
3072
3073         /*
3074          * Synthesize the cpl_pass_accept_req. We have everything except the
3075          * TID. Once firmware sends a reply with TID we update the TID field
3076          * in cpl and pass it through the regular cpl_pass_accept_req path.
3077          */
3078         build_cpl_pass_accept_req(skb, stid, iph->tos);
3079         send_fw_pass_open_req(dev, skb, iph->daddr, tcph->dest, iph->saddr,
3080                               tcph->source, ntohl(tcph->seq), filter, window,
3081                               rss_qid, pi->port_id);
3082         cxgb4_l2t_release(e);
3083 free_dst:
3084         dst_release(dst);
3085 reject:
3086         return 0;
3087 }
3088
3089 /*
3090  * These are the real handlers that are called from a
3091  * work queue.
3092  */
3093 static c4iw_handler_func work_handlers[NUM_CPL_CMDS] = {
3094         [CPL_ACT_ESTABLISH] = act_establish,
3095         [CPL_ACT_OPEN_RPL] = act_open_rpl,
3096         [CPL_RX_DATA] = rx_data,
3097         [CPL_ABORT_RPL_RSS] = abort_rpl,
3098         [CPL_ABORT_RPL] = abort_rpl,
3099         [CPL_PASS_OPEN_RPL] = pass_open_rpl,
3100         [CPL_CLOSE_LISTSRV_RPL] = close_listsrv_rpl,
3101         [CPL_PASS_ACCEPT_REQ] = pass_accept_req,
3102         [CPL_PASS_ESTABLISH] = pass_establish,
3103         [CPL_PEER_CLOSE] = peer_close,
3104         [CPL_ABORT_REQ_RSS] = peer_abort,
3105         [CPL_CLOSE_CON_RPL] = close_con_rpl,
3106         [CPL_RDMA_TERMINATE] = terminate,
3107         [CPL_FW4_ACK] = fw4_ack,
3108         [CPL_FW6_MSG] = deferred_fw6_msg,
3109         [CPL_RX_PKT] = rx_pkt
3110 };
3111
3112 static void process_timeout(struct c4iw_ep *ep)
3113 {
3114         struct c4iw_qp_attributes attrs;
3115         int abort = 1;
3116
3117         mutex_lock(&ep->com.mutex);
3118         PDBG("%s ep %p tid %u state %d\n", __func__, ep, ep->hwtid,
3119              ep->com.state);
3120         set_bit(TIMEDOUT, &ep->com.history);
3121         switch (ep->com.state) {
3122         case MPA_REQ_SENT:
3123                 __state_set(&ep->com, ABORTING);
3124                 connect_reply_upcall(ep, -ETIMEDOUT);
3125                 break;
3126         case MPA_REQ_WAIT:
3127                 __state_set(&ep->com, ABORTING);
3128                 break;
3129         case CLOSING:
3130         case MORIBUND:
3131                 if (ep->com.cm_id && ep->com.qp) {
3132                         attrs.next_state = C4IW_QP_STATE_ERROR;
3133                         c4iw_modify_qp(ep->com.qp->rhp,
3134                                      ep->com.qp, C4IW_QP_ATTR_NEXT_STATE,
3135                                      &attrs, 1);
3136                 }
3137                 __state_set(&ep->com, ABORTING);
3138                 break;
3139         default:
3140                 WARN(1, "%s unexpected state ep %p tid %u state %u\n",
3141                         __func__, ep, ep->hwtid, ep->com.state);
3142                 abort = 0;
3143         }
3144         mutex_unlock(&ep->com.mutex);
3145         if (abort)
3146                 abort_connection(ep, NULL, GFP_KERNEL);
3147         c4iw_put_ep(&ep->com);
3148 }
3149
3150 static void process_timedout_eps(void)
3151 {
3152         struct c4iw_ep *ep;
3153
3154         spin_lock_irq(&timeout_lock);
3155         while (!list_empty(&timeout_list)) {
3156                 struct list_head *tmp;
3157
3158                 tmp = timeout_list.next;
3159                 list_del(tmp);
3160                 spin_unlock_irq(&timeout_lock);
3161                 ep = list_entry(tmp, struct c4iw_ep, entry);
3162                 process_timeout(ep);
3163                 spin_lock_irq(&timeout_lock);
3164         }
3165         spin_unlock_irq(&timeout_lock);
3166 }
3167
3168 static void process_work(struct work_struct *work)
3169 {
3170         struct sk_buff *skb = NULL;
3171         struct c4iw_dev *dev;
3172         struct cpl_act_establish *rpl;
3173         unsigned int opcode;
3174         int ret;
3175
3176         while ((skb = skb_dequeue(&rxq))) {
3177                 rpl = cplhdr(skb);
3178                 dev = *((struct c4iw_dev **) (skb->cb + sizeof(void *)));
3179                 opcode = rpl->ot.opcode;
3180
3181                 BUG_ON(!work_handlers[opcode]);
3182                 ret = work_handlers[opcode](dev, skb);
3183                 if (!ret)
3184                         kfree_skb(skb);
3185         }
3186         process_timedout_eps();
3187 }
3188
3189 static DECLARE_WORK(skb_work, process_work);
3190
3191 static void ep_timeout(unsigned long arg)
3192 {
3193         struct c4iw_ep *ep = (struct c4iw_ep *)arg;
3194
3195         spin_lock(&timeout_lock);
3196         list_add_tail(&ep->entry, &timeout_list);
3197         spin_unlock(&timeout_lock);
3198         queue_work(workq, &skb_work);
3199 }
3200
3201 /*
3202  * All the CM events are handled on a work queue to have a safe context.
3203  */
3204 static int sched(struct c4iw_dev *dev, struct sk_buff *skb)
3205 {
3206
3207         /*
3208          * Save dev in the skb->cb area.
3209          */
3210         *((struct c4iw_dev **) (skb->cb + sizeof(void *))) = dev;
3211
3212         /*
3213          * Queue the skb and schedule the worker thread.
3214          */
3215         skb_queue_tail(&rxq, skb);
3216         queue_work(workq, &skb_work);
3217         return 0;
3218 }
3219
3220 static int set_tcb_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
3221 {
3222         struct cpl_set_tcb_rpl *rpl = cplhdr(skb);
3223
3224         if (rpl->status != CPL_ERR_NONE) {
3225                 printk(KERN_ERR MOD "Unexpected SET_TCB_RPL status %u "
3226                        "for tid %u\n", rpl->status, GET_TID(rpl));
3227         }
3228         kfree_skb(skb);
3229         return 0;
3230 }
3231
3232 static int fw6_msg(struct c4iw_dev *dev, struct sk_buff *skb)
3233 {
3234         struct cpl_fw6_msg *rpl = cplhdr(skb);
3235         struct c4iw_wr_wait *wr_waitp;
3236         int ret;
3237
3238         PDBG("%s type %u\n", __func__, rpl->type);
3239
3240         switch (rpl->type) {
3241         case FW6_TYPE_WR_RPL:
3242                 ret = (int)((be64_to_cpu(rpl->data[0]) >> 8) & 0xff);
3243                 wr_waitp = (struct c4iw_wr_wait *)(__force unsigned long) rpl->data[1];
3244                 PDBG("%s wr_waitp %p ret %u\n", __func__, wr_waitp, ret);
3245                 if (wr_waitp)
3246                         c4iw_wake_up(wr_waitp, ret ? -ret : 0);
3247                 kfree_skb(skb);
3248                 break;
3249         case FW6_TYPE_CQE:
3250         case FW6_TYPE_OFLD_CONNECTION_WR_RPL:
3251                 sched(dev, skb);
3252                 break;
3253         default:
3254                 printk(KERN_ERR MOD "%s unexpected fw6 msg type %u\n", __func__,
3255                        rpl->type);
3256                 kfree_skb(skb);
3257                 break;
3258         }
3259         return 0;
3260 }
3261
3262 static int peer_abort_intr(struct c4iw_dev *dev, struct sk_buff *skb)
3263 {
3264         struct cpl_abort_req_rss *req = cplhdr(skb);
3265         struct c4iw_ep *ep;
3266         struct tid_info *t = dev->rdev.lldi.tids;
3267         unsigned int tid = GET_TID(req);
3268
3269         ep = lookup_tid(t, tid);
3270         if (!ep) {
3271                 printk(KERN_WARNING MOD
3272                        "Abort on non-existent endpoint, tid %d\n", tid);
3273                 kfree_skb(skb);
3274                 return 0;
3275         }
3276         if (is_neg_adv_abort(req->status)) {
3277                 PDBG("%s neg_adv_abort ep %p tid %u\n", __func__, ep,
3278                      ep->hwtid);
3279                 kfree_skb(skb);
3280                 return 0;
3281         }
3282         PDBG("%s ep %p tid %u state %u\n", __func__, ep, ep->hwtid,
3283              ep->com.state);
3284
3285         /*
3286          * Wake up any threads in rdma_init() or rdma_fini().
3287          */
3288         c4iw_wake_up(&ep->com.wr_wait, -ECONNRESET);
3289         sched(dev, skb);
3290         return 0;
3291 }
3292
3293 /*
3294  * Most upcalls from the T4 Core go to sched() to
3295  * schedule the processing on a work queue.
3296  */
3297 c4iw_handler_func c4iw_handlers[NUM_CPL_CMDS] = {
3298         [CPL_ACT_ESTABLISH] = sched,
3299         [CPL_ACT_OPEN_RPL] = sched,
3300         [CPL_RX_DATA] = sched,
3301         [CPL_ABORT_RPL_RSS] = sched,
3302         [CPL_ABORT_RPL] = sched,
3303         [CPL_PASS_OPEN_RPL] = sched,
3304         [CPL_CLOSE_LISTSRV_RPL] = sched,
3305         [CPL_PASS_ACCEPT_REQ] = sched,
3306         [CPL_PASS_ESTABLISH] = sched,
3307         [CPL_PEER_CLOSE] = sched,
3308         [CPL_CLOSE_CON_RPL] = sched,
3309         [CPL_ABORT_REQ_RSS] = peer_abort_intr,
3310         [CPL_RDMA_TERMINATE] = sched,
3311         [CPL_FW4_ACK] = sched,
3312         [CPL_SET_TCB_RPL] = set_tcb_rpl,
3313         [CPL_FW6_MSG] = fw6_msg,
3314         [CPL_RX_PKT] = sched
3315 };
3316
3317 int __init c4iw_cm_init(void)
3318 {
3319         spin_lock_init(&timeout_lock);
3320         skb_queue_head_init(&rxq);
3321
3322         workq = create_singlethread_workqueue("iw_cxgb4");
3323         if (!workq)
3324                 return -ENOMEM;
3325
3326         return 0;
3327 }
3328
3329 void __exit c4iw_cm_term(void)
3330 {
3331         WARN_ON(!list_empty(&timeout_list));
3332         flush_workqueue(workq);
3333         destroy_workqueue(workq);
3334 }