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