GNU Linux-libre 4.19.245-gnu1
[releases.git] / drivers / infiniband / hw / cxgb4 / cm.c
1 /*
2  * Copyright (c) 2009-2014 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 <rdma/ib_addr.h>
51
52 #include <libcxgb_cm.h>
53 #include "iw_cxgb4.h"
54 #include "clip_tbl.h"
55
56 static char *states[] = {
57         "idle",
58         "listen",
59         "connecting",
60         "mpa_wait_req",
61         "mpa_req_sent",
62         "mpa_req_rcvd",
63         "mpa_rep_sent",
64         "fpdu_mode",
65         "aborting",
66         "closing",
67         "moribund",
68         "dead",
69         NULL,
70 };
71
72 static int nocong;
73 module_param(nocong, int, 0644);
74 MODULE_PARM_DESC(nocong, "Turn of congestion control (default=0)");
75
76 static int enable_ecn;
77 module_param(enable_ecn, int, 0644);
78 MODULE_PARM_DESC(enable_ecn, "Enable ECN (default=0/disabled)");
79
80 static int dack_mode = 1;
81 module_param(dack_mode, int, 0644);
82 MODULE_PARM_DESC(dack_mode, "Delayed ack mode (default=1)");
83
84 uint c4iw_max_read_depth = 32;
85 module_param(c4iw_max_read_depth, int, 0644);
86 MODULE_PARM_DESC(c4iw_max_read_depth,
87                  "Per-connection max ORD/IRD (default=32)");
88
89 static int enable_tcp_timestamps;
90 module_param(enable_tcp_timestamps, int, 0644);
91 MODULE_PARM_DESC(enable_tcp_timestamps, "Enable tcp timestamps (default=0)");
92
93 static int enable_tcp_sack;
94 module_param(enable_tcp_sack, int, 0644);
95 MODULE_PARM_DESC(enable_tcp_sack, "Enable tcp SACK (default=0)");
96
97 static int enable_tcp_window_scaling = 1;
98 module_param(enable_tcp_window_scaling, int, 0644);
99 MODULE_PARM_DESC(enable_tcp_window_scaling,
100                  "Enable tcp window scaling (default=1)");
101
102 static int peer2peer = 1;
103 module_param(peer2peer, int, 0644);
104 MODULE_PARM_DESC(peer2peer, "Support peer2peer ULPs (default=1)");
105
106 static int p2p_type = FW_RI_INIT_P2PTYPE_READ_REQ;
107 module_param(p2p_type, int, 0644);
108 MODULE_PARM_DESC(p2p_type, "RDMAP opcode to use for the RTR message: "
109                            "1=RDMA_READ 0=RDMA_WRITE (default 1)");
110
111 static int ep_timeout_secs = 60;
112 module_param(ep_timeout_secs, int, 0644);
113 MODULE_PARM_DESC(ep_timeout_secs, "CM Endpoint operation timeout "
114                                    "in seconds (default=60)");
115
116 static int mpa_rev = 2;
117 module_param(mpa_rev, int, 0644);
118 MODULE_PARM_DESC(mpa_rev, "MPA Revision, 0 supports amso1100, "
119                 "1 is RFC5044 spec compliant, 2 is IETF MPA Peer Connect Draft"
120                 " compliant (default=2)");
121
122 static int markers_enabled;
123 module_param(markers_enabled, int, 0644);
124 MODULE_PARM_DESC(markers_enabled, "Enable MPA MARKERS (default(0)=disabled)");
125
126 static int crc_enabled = 1;
127 module_param(crc_enabled, int, 0644);
128 MODULE_PARM_DESC(crc_enabled, "Enable MPA CRC (default(1)=enabled)");
129
130 static int rcv_win = 256 * 1024;
131 module_param(rcv_win, int, 0644);
132 MODULE_PARM_DESC(rcv_win, "TCP receive window in bytes (default=256KB)");
133
134 static int snd_win = 128 * 1024;
135 module_param(snd_win, int, 0644);
136 MODULE_PARM_DESC(snd_win, "TCP send window in bytes (default=128KB)");
137
138 static struct workqueue_struct *workq;
139
140 static struct sk_buff_head rxq;
141
142 static struct sk_buff *get_skb(struct sk_buff *skb, int len, gfp_t gfp);
143 static void ep_timeout(struct timer_list *t);
144 static void connect_reply_upcall(struct c4iw_ep *ep, int status);
145 static int sched(struct c4iw_dev *dev, struct sk_buff *skb);
146
147 static LIST_HEAD(timeout_list);
148 static spinlock_t timeout_lock;
149
150 static void deref_cm_id(struct c4iw_ep_common *epc)
151 {
152         epc->cm_id->rem_ref(epc->cm_id);
153         epc->cm_id = NULL;
154         set_bit(CM_ID_DEREFED, &epc->history);
155 }
156
157 static void ref_cm_id(struct c4iw_ep_common *epc)
158 {
159         set_bit(CM_ID_REFED, &epc->history);
160         epc->cm_id->add_ref(epc->cm_id);
161 }
162
163 static void deref_qp(struct c4iw_ep *ep)
164 {
165         c4iw_qp_rem_ref(&ep->com.qp->ibqp);
166         clear_bit(QP_REFERENCED, &ep->com.flags);
167         set_bit(QP_DEREFED, &ep->com.history);
168 }
169
170 static void ref_qp(struct c4iw_ep *ep)
171 {
172         set_bit(QP_REFERENCED, &ep->com.flags);
173         set_bit(QP_REFED, &ep->com.history);
174         c4iw_qp_add_ref(&ep->com.qp->ibqp);
175 }
176
177 static void start_ep_timer(struct c4iw_ep *ep)
178 {
179         pr_debug("ep %p\n", ep);
180         if (timer_pending(&ep->timer)) {
181                 pr_err("%s timer already started! ep %p\n",
182                        __func__, ep);
183                 return;
184         }
185         clear_bit(TIMEOUT, &ep->com.flags);
186         c4iw_get_ep(&ep->com);
187         ep->timer.expires = jiffies + ep_timeout_secs * HZ;
188         add_timer(&ep->timer);
189 }
190
191 static int stop_ep_timer(struct c4iw_ep *ep)
192 {
193         pr_debug("ep %p stopping\n", ep);
194         del_timer_sync(&ep->timer);
195         if (!test_and_set_bit(TIMEOUT, &ep->com.flags)) {
196                 c4iw_put_ep(&ep->com);
197                 return 0;
198         }
199         return 1;
200 }
201
202 static int c4iw_l2t_send(struct c4iw_rdev *rdev, struct sk_buff *skb,
203                   struct l2t_entry *l2e)
204 {
205         int     error = 0;
206
207         if (c4iw_fatal_error(rdev)) {
208                 kfree_skb(skb);
209                 pr_err("%s - device in error state - dropping\n", __func__);
210                 return -EIO;
211         }
212         error = cxgb4_l2t_send(rdev->lldi.ports[0], skb, l2e);
213         if (error < 0)
214                 kfree_skb(skb);
215         else if (error == NET_XMIT_DROP)
216                 return -ENOMEM;
217         return error < 0 ? error : 0;
218 }
219
220 int c4iw_ofld_send(struct c4iw_rdev *rdev, struct sk_buff *skb)
221 {
222         int     error = 0;
223
224         if (c4iw_fatal_error(rdev)) {
225                 kfree_skb(skb);
226                 pr_err("%s - device in error state - dropping\n", __func__);
227                 return -EIO;
228         }
229         error = cxgb4_ofld_send(rdev->lldi.ports[0], skb);
230         if (error < 0)
231                 kfree_skb(skb);
232         return error < 0 ? error : 0;
233 }
234
235 static void release_tid(struct c4iw_rdev *rdev, u32 hwtid, struct sk_buff *skb)
236 {
237         u32 len = roundup(sizeof(struct cpl_tid_release), 16);
238
239         skb = get_skb(skb, len, GFP_KERNEL);
240         if (!skb)
241                 return;
242
243         cxgb_mk_tid_release(skb, len, hwtid, 0);
244         c4iw_ofld_send(rdev, skb);
245         return;
246 }
247
248 static void set_emss(struct c4iw_ep *ep, u16 opt)
249 {
250         ep->emss = ep->com.dev->rdev.lldi.mtus[TCPOPT_MSS_G(opt)] -
251                    ((AF_INET == ep->com.remote_addr.ss_family) ?
252                     sizeof(struct iphdr) : sizeof(struct ipv6hdr)) -
253                    sizeof(struct tcphdr);
254         ep->mss = ep->emss;
255         if (TCPOPT_TSTAMP_G(opt))
256                 ep->emss -= round_up(TCPOLEN_TIMESTAMP, 4);
257         if (ep->emss < 128)
258                 ep->emss = 128;
259         if (ep->emss & 7)
260                 pr_debug("Warning: misaligned mtu idx %u mss %u emss=%u\n",
261                          TCPOPT_MSS_G(opt), ep->mss, ep->emss);
262         pr_debug("mss_idx %u mss %u emss=%u\n", TCPOPT_MSS_G(opt), ep->mss,
263                  ep->emss);
264 }
265
266 static enum c4iw_ep_state state_read(struct c4iw_ep_common *epc)
267 {
268         enum c4iw_ep_state state;
269
270         mutex_lock(&epc->mutex);
271         state = epc->state;
272         mutex_unlock(&epc->mutex);
273         return state;
274 }
275
276 static void __state_set(struct c4iw_ep_common *epc, enum c4iw_ep_state new)
277 {
278         epc->state = new;
279 }
280
281 static void state_set(struct c4iw_ep_common *epc, enum c4iw_ep_state new)
282 {
283         mutex_lock(&epc->mutex);
284         pr_debug("%s -> %s\n", states[epc->state], states[new]);
285         __state_set(epc, new);
286         mutex_unlock(&epc->mutex);
287         return;
288 }
289
290 static int alloc_ep_skb_list(struct sk_buff_head *ep_skb_list, int size)
291 {
292         struct sk_buff *skb;
293         unsigned int i;
294         size_t len;
295
296         len = roundup(sizeof(union cpl_wr_size), 16);
297         for (i = 0; i < size; i++) {
298                 skb = alloc_skb(len, GFP_KERNEL);
299                 if (!skb)
300                         goto fail;
301                 skb_queue_tail(ep_skb_list, skb);
302         }
303         return 0;
304 fail:
305         skb_queue_purge(ep_skb_list);
306         return -ENOMEM;
307 }
308
309 static void *alloc_ep(int size, gfp_t gfp)
310 {
311         struct c4iw_ep_common *epc;
312
313         epc = kzalloc(size, gfp);
314         if (epc) {
315                 epc->wr_waitp = c4iw_alloc_wr_wait(gfp);
316                 if (!epc->wr_waitp) {
317                         kfree(epc);
318                         epc = NULL;
319                         goto out;
320                 }
321                 kref_init(&epc->kref);
322                 mutex_init(&epc->mutex);
323                 c4iw_init_wr_wait(epc->wr_waitp);
324         }
325         pr_debug("alloc ep %p\n", epc);
326 out:
327         return epc;
328 }
329
330 static void remove_ep_tid(struct c4iw_ep *ep)
331 {
332         unsigned long flags;
333
334         spin_lock_irqsave(&ep->com.dev->lock, flags);
335         _remove_handle(ep->com.dev, &ep->com.dev->hwtid_idr, ep->hwtid, 0);
336         if (idr_is_empty(&ep->com.dev->hwtid_idr))
337                 wake_up(&ep->com.dev->wait);
338         spin_unlock_irqrestore(&ep->com.dev->lock, flags);
339 }
340
341 static void insert_ep_tid(struct c4iw_ep *ep)
342 {
343         unsigned long flags;
344
345         spin_lock_irqsave(&ep->com.dev->lock, flags);
346         _insert_handle(ep->com.dev, &ep->com.dev->hwtid_idr, ep, ep->hwtid, 0);
347         spin_unlock_irqrestore(&ep->com.dev->lock, flags);
348 }
349
350 /*
351  * Atomically lookup the ep ptr given the tid and grab a reference on the ep.
352  */
353 static struct c4iw_ep *get_ep_from_tid(struct c4iw_dev *dev, unsigned int tid)
354 {
355         struct c4iw_ep *ep;
356         unsigned long flags;
357
358         spin_lock_irqsave(&dev->lock, flags);
359         ep = idr_find(&dev->hwtid_idr, tid);
360         if (ep)
361                 c4iw_get_ep(&ep->com);
362         spin_unlock_irqrestore(&dev->lock, flags);
363         return ep;
364 }
365
366 /*
367  * Atomically lookup the ep ptr given the stid and grab a reference on the ep.
368  */
369 static struct c4iw_listen_ep *get_ep_from_stid(struct c4iw_dev *dev,
370                                                unsigned int stid)
371 {
372         struct c4iw_listen_ep *ep;
373         unsigned long flags;
374
375         spin_lock_irqsave(&dev->lock, flags);
376         ep = idr_find(&dev->stid_idr, stid);
377         if (ep)
378                 c4iw_get_ep(&ep->com);
379         spin_unlock_irqrestore(&dev->lock, flags);
380         return ep;
381 }
382
383 void _c4iw_free_ep(struct kref *kref)
384 {
385         struct c4iw_ep *ep;
386
387         ep = container_of(kref, struct c4iw_ep, com.kref);
388         pr_debug("ep %p state %s\n", ep, states[ep->com.state]);
389         if (test_bit(QP_REFERENCED, &ep->com.flags))
390                 deref_qp(ep);
391         if (test_bit(RELEASE_RESOURCES, &ep->com.flags)) {
392                 if (ep->com.remote_addr.ss_family == AF_INET6) {
393                         struct sockaddr_in6 *sin6 =
394                                         (struct sockaddr_in6 *)
395                                         &ep->com.local_addr;
396
397                         cxgb4_clip_release(
398                                         ep->com.dev->rdev.lldi.ports[0],
399                                         (const u32 *)&sin6->sin6_addr.s6_addr,
400                                         1);
401                 }
402                 cxgb4_remove_tid(ep->com.dev->rdev.lldi.tids, 0, ep->hwtid,
403                                  ep->com.local_addr.ss_family);
404                 dst_release(ep->dst);
405                 cxgb4_l2t_release(ep->l2t);
406                 if (ep->mpa_skb)
407                         kfree_skb(ep->mpa_skb);
408         }
409         if (!skb_queue_empty(&ep->com.ep_skb_list))
410                 skb_queue_purge(&ep->com.ep_skb_list);
411         c4iw_put_wr_wait(ep->com.wr_waitp);
412         kfree(ep);
413 }
414
415 static void release_ep_resources(struct c4iw_ep *ep)
416 {
417         set_bit(RELEASE_RESOURCES, &ep->com.flags);
418
419         /*
420          * If we have a hwtid, then remove it from the idr table
421          * so lookups will no longer find this endpoint.  Otherwise
422          * we have a race where one thread finds the ep ptr just
423          * before the other thread is freeing the ep memory.
424          */
425         if (ep->hwtid != -1)
426                 remove_ep_tid(ep);
427         c4iw_put_ep(&ep->com);
428 }
429
430 static int status2errno(int status)
431 {
432         switch (status) {
433         case CPL_ERR_NONE:
434                 return 0;
435         case CPL_ERR_CONN_RESET:
436                 return -ECONNRESET;
437         case CPL_ERR_ARP_MISS:
438                 return -EHOSTUNREACH;
439         case CPL_ERR_CONN_TIMEDOUT:
440                 return -ETIMEDOUT;
441         case CPL_ERR_TCAM_FULL:
442                 return -ENOMEM;
443         case CPL_ERR_CONN_EXIST:
444                 return -EADDRINUSE;
445         default:
446                 return -EIO;
447         }
448 }
449
450 /*
451  * Try and reuse skbs already allocated...
452  */
453 static struct sk_buff *get_skb(struct sk_buff *skb, int len, gfp_t gfp)
454 {
455         if (skb && !skb_is_nonlinear(skb) && !skb_cloned(skb)) {
456                 skb_trim(skb, 0);
457                 skb_get(skb);
458                 skb_reset_transport_header(skb);
459         } else {
460                 skb = alloc_skb(len, gfp);
461                 if (!skb)
462                         return NULL;
463         }
464         t4_set_arp_err_handler(skb, NULL, NULL);
465         return skb;
466 }
467
468 static struct net_device *get_real_dev(struct net_device *egress_dev)
469 {
470         return rdma_vlan_dev_real_dev(egress_dev) ? : egress_dev;
471 }
472
473 static void arp_failure_discard(void *handle, struct sk_buff *skb)
474 {
475         pr_err("ARP failure\n");
476         kfree_skb(skb);
477 }
478
479 static void mpa_start_arp_failure(void *handle, struct sk_buff *skb)
480 {
481         pr_err("ARP failure during MPA Negotiation - Closing Connection\n");
482 }
483
484 enum {
485         NUM_FAKE_CPLS = 2,
486         FAKE_CPL_PUT_EP_SAFE = NUM_CPL_CMDS + 0,
487         FAKE_CPL_PASS_PUT_EP_SAFE = NUM_CPL_CMDS + 1,
488 };
489
490 static int _put_ep_safe(struct c4iw_dev *dev, struct sk_buff *skb)
491 {
492         struct c4iw_ep *ep;
493
494         ep = *((struct c4iw_ep **)(skb->cb + 2 * sizeof(void *)));
495         release_ep_resources(ep);
496         return 0;
497 }
498
499 static int _put_pass_ep_safe(struct c4iw_dev *dev, struct sk_buff *skb)
500 {
501         struct c4iw_ep *ep;
502
503         ep = *((struct c4iw_ep **)(skb->cb + 2 * sizeof(void *)));
504         c4iw_put_ep(&ep->parent_ep->com);
505         release_ep_resources(ep);
506         return 0;
507 }
508
509 /*
510  * Fake up a special CPL opcode and call sched() so process_work() will call
511  * _put_ep_safe() in a safe context to free the ep resources.  This is needed
512  * because ARP error handlers are called in an ATOMIC context, and
513  * _c4iw_free_ep() needs to block.
514  */
515 static void queue_arp_failure_cpl(struct c4iw_ep *ep, struct sk_buff *skb,
516                                   int cpl)
517 {
518         struct cpl_act_establish *rpl = cplhdr(skb);
519
520         /* Set our special ARP_FAILURE opcode */
521         rpl->ot.opcode = cpl;
522
523         /*
524          * Save ep in the skb->cb area, after where sched() will save the dev
525          * ptr.
526          */
527         *((struct c4iw_ep **)(skb->cb + 2 * sizeof(void *))) = ep;
528         sched(ep->com.dev, skb);
529 }
530
531 /* Handle an ARP failure for an accept */
532 static void pass_accept_rpl_arp_failure(void *handle, struct sk_buff *skb)
533 {
534         struct c4iw_ep *ep = handle;
535
536         pr_err("ARP failure during accept - tid %u - dropping connection\n",
537                ep->hwtid);
538
539         __state_set(&ep->com, DEAD);
540         queue_arp_failure_cpl(ep, skb, FAKE_CPL_PASS_PUT_EP_SAFE);
541 }
542
543 /*
544  * Handle an ARP failure for an active open.
545  */
546 static void act_open_req_arp_failure(void *handle, struct sk_buff *skb)
547 {
548         struct c4iw_ep *ep = handle;
549
550         pr_err("ARP failure during connect\n");
551         connect_reply_upcall(ep, -EHOSTUNREACH);
552         __state_set(&ep->com, DEAD);
553         if (ep->com.remote_addr.ss_family == AF_INET6) {
554                 struct sockaddr_in6 *sin6 =
555                         (struct sockaddr_in6 *)&ep->com.local_addr;
556                 cxgb4_clip_release(ep->com.dev->rdev.lldi.ports[0],
557                                    (const u32 *)&sin6->sin6_addr.s6_addr, 1);
558         }
559         remove_handle(ep->com.dev, &ep->com.dev->atid_idr, ep->atid);
560         cxgb4_free_atid(ep->com.dev->rdev.lldi.tids, ep->atid);
561         queue_arp_failure_cpl(ep, skb, FAKE_CPL_PUT_EP_SAFE);
562 }
563
564 /*
565  * Handle an ARP failure for a CPL_ABORT_REQ.  Change it into a no RST variant
566  * and send it along.
567  */
568 static void abort_arp_failure(void *handle, struct sk_buff *skb)
569 {
570         int ret;
571         struct c4iw_ep *ep = handle;
572         struct c4iw_rdev *rdev = &ep->com.dev->rdev;
573         struct cpl_abort_req *req = cplhdr(skb);
574
575         pr_debug("rdev %p\n", rdev);
576         req->cmd = CPL_ABORT_NO_RST;
577         skb_get(skb);
578         ret = c4iw_ofld_send(rdev, skb);
579         if (ret) {
580                 __state_set(&ep->com, DEAD);
581                 queue_arp_failure_cpl(ep, skb, FAKE_CPL_PUT_EP_SAFE);
582         } else
583                 kfree_skb(skb);
584 }
585
586 static int send_flowc(struct c4iw_ep *ep)
587 {
588         struct fw_flowc_wr *flowc;
589         struct sk_buff *skb = skb_dequeue(&ep->com.ep_skb_list);
590         u16 vlan = ep->l2t->vlan;
591         int nparams;
592         int flowclen, flowclen16;
593
594         if (WARN_ON(!skb))
595                 return -ENOMEM;
596
597         if (vlan == CPL_L2T_VLAN_NONE)
598                 nparams = 9;
599         else
600                 nparams = 10;
601
602         flowclen = offsetof(struct fw_flowc_wr, mnemval[nparams]);
603         flowclen16 = DIV_ROUND_UP(flowclen, 16);
604         flowclen = flowclen16 * 16;
605
606         flowc = __skb_put(skb, flowclen);
607         memset(flowc, 0, flowclen);
608
609         flowc->op_to_nparams = cpu_to_be32(FW_WR_OP_V(FW_FLOWC_WR) |
610                                            FW_FLOWC_WR_NPARAMS_V(nparams));
611         flowc->flowid_len16 = cpu_to_be32(FW_WR_LEN16_V(flowclen16) |
612                                           FW_WR_FLOWID_V(ep->hwtid));
613
614         flowc->mnemval[0].mnemonic = FW_FLOWC_MNEM_PFNVFN;
615         flowc->mnemval[0].val = cpu_to_be32(FW_PFVF_CMD_PFN_V
616                                             (ep->com.dev->rdev.lldi.pf));
617         flowc->mnemval[1].mnemonic = FW_FLOWC_MNEM_CH;
618         flowc->mnemval[1].val = cpu_to_be32(ep->tx_chan);
619         flowc->mnemval[2].mnemonic = FW_FLOWC_MNEM_PORT;
620         flowc->mnemval[2].val = cpu_to_be32(ep->tx_chan);
621         flowc->mnemval[3].mnemonic = FW_FLOWC_MNEM_IQID;
622         flowc->mnemval[3].val = cpu_to_be32(ep->rss_qid);
623         flowc->mnemval[4].mnemonic = FW_FLOWC_MNEM_SNDNXT;
624         flowc->mnemval[4].val = cpu_to_be32(ep->snd_seq);
625         flowc->mnemval[5].mnemonic = FW_FLOWC_MNEM_RCVNXT;
626         flowc->mnemval[5].val = cpu_to_be32(ep->rcv_seq);
627         flowc->mnemval[6].mnemonic = FW_FLOWC_MNEM_SNDBUF;
628         flowc->mnemval[6].val = cpu_to_be32(ep->snd_win);
629         flowc->mnemval[7].mnemonic = FW_FLOWC_MNEM_MSS;
630         flowc->mnemval[7].val = cpu_to_be32(ep->emss);
631         flowc->mnemval[8].mnemonic = FW_FLOWC_MNEM_RCV_SCALE;
632         flowc->mnemval[8].val = cpu_to_be32(ep->snd_wscale);
633         if (nparams == 10) {
634                 u16 pri;
635                 pri = (vlan & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
636                 flowc->mnemval[9].mnemonic = FW_FLOWC_MNEM_SCHEDCLASS;
637                 flowc->mnemval[9].val = cpu_to_be32(pri);
638         }
639
640         set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
641         return c4iw_ofld_send(&ep->com.dev->rdev, skb);
642 }
643
644 static int send_halfclose(struct c4iw_ep *ep)
645 {
646         struct sk_buff *skb = skb_dequeue(&ep->com.ep_skb_list);
647         u32 wrlen = roundup(sizeof(struct cpl_close_con_req), 16);
648
649         pr_debug("ep %p tid %u\n", ep, ep->hwtid);
650         if (WARN_ON(!skb))
651                 return -ENOMEM;
652
653         cxgb_mk_close_con_req(skb, wrlen, ep->hwtid, ep->txq_idx,
654                               NULL, arp_failure_discard);
655
656         return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
657 }
658
659 static int send_abort(struct c4iw_ep *ep)
660 {
661         u32 wrlen = roundup(sizeof(struct cpl_abort_req), 16);
662         struct sk_buff *req_skb = skb_dequeue(&ep->com.ep_skb_list);
663
664         pr_debug("ep %p tid %u\n", ep, ep->hwtid);
665         if (WARN_ON(!req_skb))
666                 return -ENOMEM;
667
668         cxgb_mk_abort_req(req_skb, wrlen, ep->hwtid, ep->txq_idx,
669                           ep, abort_arp_failure);
670
671         return c4iw_l2t_send(&ep->com.dev->rdev, req_skb, ep->l2t);
672 }
673
674 static int send_connect(struct c4iw_ep *ep)
675 {
676         struct cpl_act_open_req *req = NULL;
677         struct cpl_t5_act_open_req *t5req = NULL;
678         struct cpl_t6_act_open_req *t6req = NULL;
679         struct cpl_act_open_req6 *req6 = NULL;
680         struct cpl_t5_act_open_req6 *t5req6 = NULL;
681         struct cpl_t6_act_open_req6 *t6req6 = NULL;
682         struct sk_buff *skb;
683         u64 opt0;
684         u32 opt2;
685         unsigned int mtu_idx;
686         u32 wscale;
687         int win, sizev4, sizev6, wrlen;
688         struct sockaddr_in *la = (struct sockaddr_in *)
689                                  &ep->com.local_addr;
690         struct sockaddr_in *ra = (struct sockaddr_in *)
691                                  &ep->com.remote_addr;
692         struct sockaddr_in6 *la6 = (struct sockaddr_in6 *)
693                                    &ep->com.local_addr;
694         struct sockaddr_in6 *ra6 = (struct sockaddr_in6 *)
695                                    &ep->com.remote_addr;
696         int ret;
697         enum chip_type adapter_type = ep->com.dev->rdev.lldi.adapter_type;
698         u32 isn = (prandom_u32() & ~7UL) - 1;
699         struct net_device *netdev;
700         u64 params;
701
702         netdev = ep->com.dev->rdev.lldi.ports[0];
703
704         switch (CHELSIO_CHIP_VERSION(adapter_type)) {
705         case CHELSIO_T4:
706                 sizev4 = sizeof(struct cpl_act_open_req);
707                 sizev6 = sizeof(struct cpl_act_open_req6);
708                 break;
709         case CHELSIO_T5:
710                 sizev4 = sizeof(struct cpl_t5_act_open_req);
711                 sizev6 = sizeof(struct cpl_t5_act_open_req6);
712                 break;
713         case CHELSIO_T6:
714                 sizev4 = sizeof(struct cpl_t6_act_open_req);
715                 sizev6 = sizeof(struct cpl_t6_act_open_req6);
716                 break;
717         default:
718                 pr_err("T%d Chip is not supported\n",
719                        CHELSIO_CHIP_VERSION(adapter_type));
720                 return -EINVAL;
721         }
722
723         wrlen = (ep->com.remote_addr.ss_family == AF_INET) ?
724                         roundup(sizev4, 16) :
725                         roundup(sizev6, 16);
726
727         pr_debug("ep %p atid %u\n", ep, ep->atid);
728
729         skb = get_skb(NULL, wrlen, GFP_KERNEL);
730         if (!skb) {
731                 pr_err("%s - failed to alloc skb\n", __func__);
732                 return -ENOMEM;
733         }
734         set_wr_txq(skb, CPL_PRIORITY_SETUP, ep->ctrlq_idx);
735
736         cxgb_best_mtu(ep->com.dev->rdev.lldi.mtus, ep->mtu, &mtu_idx,
737                       enable_tcp_timestamps,
738                       (ep->com.remote_addr.ss_family == AF_INET) ? 0 : 1);
739         wscale = cxgb_compute_wscale(rcv_win);
740
741         /*
742          * Specify the largest window that will fit in opt0. The
743          * remainder will be specified in the rx_data_ack.
744          */
745         win = ep->rcv_win >> 10;
746         if (win > RCV_BUFSIZ_M)
747                 win = RCV_BUFSIZ_M;
748
749         opt0 = (nocong ? NO_CONG_F : 0) |
750                KEEP_ALIVE_F |
751                DELACK_F |
752                WND_SCALE_V(wscale) |
753                MSS_IDX_V(mtu_idx) |
754                L2T_IDX_V(ep->l2t->idx) |
755                TX_CHAN_V(ep->tx_chan) |
756                SMAC_SEL_V(ep->smac_idx) |
757                DSCP_V(ep->tos >> 2) |
758                ULP_MODE_V(ULP_MODE_TCPDDP) |
759                RCV_BUFSIZ_V(win);
760         opt2 = RX_CHANNEL_V(0) |
761                CCTRL_ECN_V(enable_ecn) |
762                RSS_QUEUE_VALID_F | RSS_QUEUE_V(ep->rss_qid);
763         if (enable_tcp_timestamps)
764                 opt2 |= TSTAMPS_EN_F;
765         if (enable_tcp_sack)
766                 opt2 |= SACK_EN_F;
767         if (wscale && enable_tcp_window_scaling)
768                 opt2 |= WND_SCALE_EN_F;
769         if (CHELSIO_CHIP_VERSION(adapter_type) > CHELSIO_T4) {
770                 if (peer2peer)
771                         isn += 4;
772
773                 opt2 |= T5_OPT_2_VALID_F;
774                 opt2 |= CONG_CNTRL_V(CONG_ALG_TAHOE);
775                 opt2 |= T5_ISS_F;
776         }
777
778         params = cxgb4_select_ntuple(netdev, ep->l2t);
779
780         if (ep->com.remote_addr.ss_family == AF_INET6)
781                 cxgb4_clip_get(ep->com.dev->rdev.lldi.ports[0],
782                                (const u32 *)&la6->sin6_addr.s6_addr, 1);
783
784         t4_set_arp_err_handler(skb, ep, act_open_req_arp_failure);
785
786         if (ep->com.remote_addr.ss_family == AF_INET) {
787                 switch (CHELSIO_CHIP_VERSION(adapter_type)) {
788                 case CHELSIO_T4:
789                         req = skb_put(skb, wrlen);
790                         INIT_TP_WR(req, 0);
791                         break;
792                 case CHELSIO_T5:
793                         t5req = skb_put(skb, wrlen);
794                         INIT_TP_WR(t5req, 0);
795                         req = (struct cpl_act_open_req *)t5req;
796                         break;
797                 case CHELSIO_T6:
798                         t6req = skb_put(skb, wrlen);
799                         INIT_TP_WR(t6req, 0);
800                         req = (struct cpl_act_open_req *)t6req;
801                         t5req = (struct cpl_t5_act_open_req *)t6req;
802                         break;
803                 default:
804                         pr_err("T%d Chip is not supported\n",
805                                CHELSIO_CHIP_VERSION(adapter_type));
806                         ret = -EINVAL;
807                         goto clip_release;
808                 }
809
810                 OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_ACT_OPEN_REQ,
811                                         ((ep->rss_qid<<14) | ep->atid)));
812                 req->local_port = la->sin_port;
813                 req->peer_port = ra->sin_port;
814                 req->local_ip = la->sin_addr.s_addr;
815                 req->peer_ip = ra->sin_addr.s_addr;
816                 req->opt0 = cpu_to_be64(opt0);
817
818                 if (is_t4(ep->com.dev->rdev.lldi.adapter_type)) {
819                         req->params = cpu_to_be32(params);
820                         req->opt2 = cpu_to_be32(opt2);
821                 } else {
822                         if (is_t5(ep->com.dev->rdev.lldi.adapter_type)) {
823                                 t5req->params =
824                                           cpu_to_be64(FILTER_TUPLE_V(params));
825                                 t5req->rsvd = cpu_to_be32(isn);
826                                 pr_debug("snd_isn %u\n", t5req->rsvd);
827                                 t5req->opt2 = cpu_to_be32(opt2);
828                         } else {
829                                 t6req->params =
830                                           cpu_to_be64(FILTER_TUPLE_V(params));
831                                 t6req->rsvd = cpu_to_be32(isn);
832                                 pr_debug("snd_isn %u\n", t6req->rsvd);
833                                 t6req->opt2 = cpu_to_be32(opt2);
834                         }
835                 }
836         } else {
837                 switch (CHELSIO_CHIP_VERSION(adapter_type)) {
838                 case CHELSIO_T4:
839                         req6 = skb_put(skb, wrlen);
840                         INIT_TP_WR(req6, 0);
841                         break;
842                 case CHELSIO_T5:
843                         t5req6 = skb_put(skb, wrlen);
844                         INIT_TP_WR(t5req6, 0);
845                         req6 = (struct cpl_act_open_req6 *)t5req6;
846                         break;
847                 case CHELSIO_T6:
848                         t6req6 = skb_put(skb, wrlen);
849                         INIT_TP_WR(t6req6, 0);
850                         req6 = (struct cpl_act_open_req6 *)t6req6;
851                         t5req6 = (struct cpl_t5_act_open_req6 *)t6req6;
852                         break;
853                 default:
854                         pr_err("T%d Chip is not supported\n",
855                                CHELSIO_CHIP_VERSION(adapter_type));
856                         ret = -EINVAL;
857                         goto clip_release;
858                 }
859
860                 OPCODE_TID(req6) = cpu_to_be32(MK_OPCODE_TID(CPL_ACT_OPEN_REQ6,
861                                         ((ep->rss_qid<<14)|ep->atid)));
862                 req6->local_port = la6->sin6_port;
863                 req6->peer_port = ra6->sin6_port;
864                 req6->local_ip_hi = *((__be64 *)(la6->sin6_addr.s6_addr));
865                 req6->local_ip_lo = *((__be64 *)(la6->sin6_addr.s6_addr + 8));
866                 req6->peer_ip_hi = *((__be64 *)(ra6->sin6_addr.s6_addr));
867                 req6->peer_ip_lo = *((__be64 *)(ra6->sin6_addr.s6_addr + 8));
868                 req6->opt0 = cpu_to_be64(opt0);
869
870                 if (is_t4(ep->com.dev->rdev.lldi.adapter_type)) {
871                         req6->params = cpu_to_be32(cxgb4_select_ntuple(netdev,
872                                                                       ep->l2t));
873                         req6->opt2 = cpu_to_be32(opt2);
874                 } else {
875                         if (is_t5(ep->com.dev->rdev.lldi.adapter_type)) {
876                                 t5req6->params =
877                                             cpu_to_be64(FILTER_TUPLE_V(params));
878                                 t5req6->rsvd = cpu_to_be32(isn);
879                                 pr_debug("snd_isn %u\n", t5req6->rsvd);
880                                 t5req6->opt2 = cpu_to_be32(opt2);
881                         } else {
882                                 t6req6->params =
883                                             cpu_to_be64(FILTER_TUPLE_V(params));
884                                 t6req6->rsvd = cpu_to_be32(isn);
885                                 pr_debug("snd_isn %u\n", t6req6->rsvd);
886                                 t6req6->opt2 = cpu_to_be32(opt2);
887                         }
888
889                 }
890         }
891
892         set_bit(ACT_OPEN_REQ, &ep->com.history);
893         ret = c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
894 clip_release:
895         if (ret && ep->com.remote_addr.ss_family == AF_INET6)
896                 cxgb4_clip_release(ep->com.dev->rdev.lldi.ports[0],
897                                    (const u32 *)&la6->sin6_addr.s6_addr, 1);
898         return ret;
899 }
900
901 static int send_mpa_req(struct c4iw_ep *ep, struct sk_buff *skb,
902                         u8 mpa_rev_to_use)
903 {
904         int mpalen, wrlen, ret;
905         struct fw_ofld_tx_data_wr *req;
906         struct mpa_message *mpa;
907         struct mpa_v2_conn_params mpa_v2_params;
908
909         pr_debug("ep %p tid %u pd_len %d\n",
910                  ep, ep->hwtid, ep->plen);
911
912         mpalen = sizeof(*mpa) + ep->plen;
913         if (mpa_rev_to_use == 2)
914                 mpalen += sizeof(struct mpa_v2_conn_params);
915         wrlen = roundup(mpalen + sizeof *req, 16);
916         skb = get_skb(skb, wrlen, GFP_KERNEL);
917         if (!skb) {
918                 connect_reply_upcall(ep, -ENOMEM);
919                 return -ENOMEM;
920         }
921         set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
922
923         req = skb_put_zero(skb, wrlen);
924         req->op_to_immdlen = cpu_to_be32(
925                 FW_WR_OP_V(FW_OFLD_TX_DATA_WR) |
926                 FW_WR_COMPL_F |
927                 FW_WR_IMMDLEN_V(mpalen));
928         req->flowid_len16 = cpu_to_be32(
929                 FW_WR_FLOWID_V(ep->hwtid) |
930                 FW_WR_LEN16_V(wrlen >> 4));
931         req->plen = cpu_to_be32(mpalen);
932         req->tunnel_to_proxy = cpu_to_be32(
933                 FW_OFLD_TX_DATA_WR_FLUSH_F |
934                 FW_OFLD_TX_DATA_WR_SHOVE_F);
935
936         mpa = (struct mpa_message *)(req + 1);
937         memcpy(mpa->key, MPA_KEY_REQ, sizeof(mpa->key));
938
939         mpa->flags = 0;
940         if (crc_enabled)
941                 mpa->flags |= MPA_CRC;
942         if (markers_enabled) {
943                 mpa->flags |= MPA_MARKERS;
944                 ep->mpa_attr.recv_marker_enabled = 1;
945         } else {
946                 ep->mpa_attr.recv_marker_enabled = 0;
947         }
948         if (mpa_rev_to_use == 2)
949                 mpa->flags |= MPA_ENHANCED_RDMA_CONN;
950
951         mpa->private_data_size = htons(ep->plen);
952         mpa->revision = mpa_rev_to_use;
953         if (mpa_rev_to_use == 1) {
954                 ep->tried_with_mpa_v1 = 1;
955                 ep->retry_with_mpa_v1 = 0;
956         }
957
958         if (mpa_rev_to_use == 2) {
959                 mpa->private_data_size = htons(ntohs(mpa->private_data_size) +
960                                                sizeof (struct mpa_v2_conn_params));
961                 pr_debug("initiator ird %u ord %u\n", ep->ird,
962                          ep->ord);
963                 mpa_v2_params.ird = htons((u16)ep->ird);
964                 mpa_v2_params.ord = htons((u16)ep->ord);
965
966                 if (peer2peer) {
967                         mpa_v2_params.ird |= htons(MPA_V2_PEER2PEER_MODEL);
968                         if (p2p_type == FW_RI_INIT_P2PTYPE_RDMA_WRITE)
969                                 mpa_v2_params.ord |=
970                                         htons(MPA_V2_RDMA_WRITE_RTR);
971                         else if (p2p_type == FW_RI_INIT_P2PTYPE_READ_REQ)
972                                 mpa_v2_params.ord |=
973                                         htons(MPA_V2_RDMA_READ_RTR);
974                 }
975                 memcpy(mpa->private_data, &mpa_v2_params,
976                        sizeof(struct mpa_v2_conn_params));
977
978                 if (ep->plen)
979                         memcpy(mpa->private_data +
980                                sizeof(struct mpa_v2_conn_params),
981                                ep->mpa_pkt + sizeof(*mpa), ep->plen);
982         } else
983                 if (ep->plen)
984                         memcpy(mpa->private_data,
985                                         ep->mpa_pkt + sizeof(*mpa), ep->plen);
986
987         /*
988          * Reference the mpa skb.  This ensures the data area
989          * will remain in memory until the hw acks the tx.
990          * Function fw4_ack() will deref it.
991          */
992         skb_get(skb);
993         t4_set_arp_err_handler(skb, NULL, arp_failure_discard);
994         ep->mpa_skb = skb;
995         ret = c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
996         if (ret)
997                 return ret;
998         start_ep_timer(ep);
999         __state_set(&ep->com, MPA_REQ_SENT);
1000         ep->mpa_attr.initiator = 1;
1001         ep->snd_seq += mpalen;
1002         return ret;
1003 }
1004
1005 static int send_mpa_reject(struct c4iw_ep *ep, const void *pdata, u8 plen)
1006 {
1007         int mpalen, wrlen;
1008         struct fw_ofld_tx_data_wr *req;
1009         struct mpa_message *mpa;
1010         struct sk_buff *skb;
1011         struct mpa_v2_conn_params mpa_v2_params;
1012
1013         pr_debug("ep %p tid %u pd_len %d\n",
1014                  ep, ep->hwtid, ep->plen);
1015
1016         mpalen = sizeof(*mpa) + plen;
1017         if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn)
1018                 mpalen += sizeof(struct mpa_v2_conn_params);
1019         wrlen = roundup(mpalen + sizeof *req, 16);
1020
1021         skb = get_skb(NULL, wrlen, GFP_KERNEL);
1022         if (!skb) {
1023                 pr_err("%s - cannot alloc skb!\n", __func__);
1024                 return -ENOMEM;
1025         }
1026         set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
1027
1028         req = skb_put_zero(skb, wrlen);
1029         req->op_to_immdlen = cpu_to_be32(
1030                 FW_WR_OP_V(FW_OFLD_TX_DATA_WR) |
1031                 FW_WR_COMPL_F |
1032                 FW_WR_IMMDLEN_V(mpalen));
1033         req->flowid_len16 = cpu_to_be32(
1034                 FW_WR_FLOWID_V(ep->hwtid) |
1035                 FW_WR_LEN16_V(wrlen >> 4));
1036         req->plen = cpu_to_be32(mpalen);
1037         req->tunnel_to_proxy = cpu_to_be32(
1038                 FW_OFLD_TX_DATA_WR_FLUSH_F |
1039                 FW_OFLD_TX_DATA_WR_SHOVE_F);
1040
1041         mpa = (struct mpa_message *)(req + 1);
1042         memset(mpa, 0, sizeof(*mpa));
1043         memcpy(mpa->key, MPA_KEY_REP, sizeof(mpa->key));
1044         mpa->flags = MPA_REJECT;
1045         mpa->revision = ep->mpa_attr.version;
1046         mpa->private_data_size = htons(plen);
1047
1048         if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn) {
1049                 mpa->flags |= MPA_ENHANCED_RDMA_CONN;
1050                 mpa->private_data_size = htons(ntohs(mpa->private_data_size) +
1051                                                sizeof (struct mpa_v2_conn_params));
1052                 mpa_v2_params.ird = htons(((u16)ep->ird) |
1053                                           (peer2peer ? MPA_V2_PEER2PEER_MODEL :
1054                                            0));
1055                 mpa_v2_params.ord = htons(((u16)ep->ord) | (peer2peer ?
1056                                           (p2p_type ==
1057                                            FW_RI_INIT_P2PTYPE_RDMA_WRITE ?
1058                                            MPA_V2_RDMA_WRITE_RTR : p2p_type ==
1059                                            FW_RI_INIT_P2PTYPE_READ_REQ ?
1060                                            MPA_V2_RDMA_READ_RTR : 0) : 0));
1061                 memcpy(mpa->private_data, &mpa_v2_params,
1062                        sizeof(struct mpa_v2_conn_params));
1063
1064                 if (ep->plen)
1065                         memcpy(mpa->private_data +
1066                                sizeof(struct mpa_v2_conn_params), pdata, plen);
1067         } else
1068                 if (plen)
1069                         memcpy(mpa->private_data, pdata, plen);
1070
1071         /*
1072          * Reference the mpa skb again.  This ensures the data area
1073          * will remain in memory until the hw acks the tx.
1074          * Function fw4_ack() will deref it.
1075          */
1076         skb_get(skb);
1077         set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
1078         t4_set_arp_err_handler(skb, NULL, mpa_start_arp_failure);
1079         ep->mpa_skb = skb;
1080         ep->snd_seq += mpalen;
1081         return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
1082 }
1083
1084 static int send_mpa_reply(struct c4iw_ep *ep, const void *pdata, u8 plen)
1085 {
1086         int mpalen, wrlen;
1087         struct fw_ofld_tx_data_wr *req;
1088         struct mpa_message *mpa;
1089         struct sk_buff *skb;
1090         struct mpa_v2_conn_params mpa_v2_params;
1091
1092         pr_debug("ep %p tid %u pd_len %d\n",
1093                  ep, ep->hwtid, ep->plen);
1094
1095         mpalen = sizeof(*mpa) + plen;
1096         if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn)
1097                 mpalen += sizeof(struct mpa_v2_conn_params);
1098         wrlen = roundup(mpalen + sizeof *req, 16);
1099
1100         skb = get_skb(NULL, wrlen, GFP_KERNEL);
1101         if (!skb) {
1102                 pr_err("%s - cannot alloc skb!\n", __func__);
1103                 return -ENOMEM;
1104         }
1105         set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
1106
1107         req = skb_put_zero(skb, wrlen);
1108         req->op_to_immdlen = cpu_to_be32(
1109                 FW_WR_OP_V(FW_OFLD_TX_DATA_WR) |
1110                 FW_WR_COMPL_F |
1111                 FW_WR_IMMDLEN_V(mpalen));
1112         req->flowid_len16 = cpu_to_be32(
1113                 FW_WR_FLOWID_V(ep->hwtid) |
1114                 FW_WR_LEN16_V(wrlen >> 4));
1115         req->plen = cpu_to_be32(mpalen);
1116         req->tunnel_to_proxy = cpu_to_be32(
1117                 FW_OFLD_TX_DATA_WR_FLUSH_F |
1118                 FW_OFLD_TX_DATA_WR_SHOVE_F);
1119
1120         mpa = (struct mpa_message *)(req + 1);
1121         memset(mpa, 0, sizeof(*mpa));
1122         memcpy(mpa->key, MPA_KEY_REP, sizeof(mpa->key));
1123         mpa->flags = 0;
1124         if (ep->mpa_attr.crc_enabled)
1125                 mpa->flags |= MPA_CRC;
1126         if (ep->mpa_attr.recv_marker_enabled)
1127                 mpa->flags |= MPA_MARKERS;
1128         mpa->revision = ep->mpa_attr.version;
1129         mpa->private_data_size = htons(plen);
1130
1131         if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn) {
1132                 mpa->flags |= MPA_ENHANCED_RDMA_CONN;
1133                 mpa->private_data_size = htons(ntohs(mpa->private_data_size) +
1134                                                sizeof (struct mpa_v2_conn_params));
1135                 mpa_v2_params.ird = htons((u16)ep->ird);
1136                 mpa_v2_params.ord = htons((u16)ep->ord);
1137                 if (peer2peer && (ep->mpa_attr.p2p_type !=
1138                                         FW_RI_INIT_P2PTYPE_DISABLED)) {
1139                         mpa_v2_params.ird |= htons(MPA_V2_PEER2PEER_MODEL);
1140
1141                         if (p2p_type == FW_RI_INIT_P2PTYPE_RDMA_WRITE)
1142                                 mpa_v2_params.ord |=
1143                                         htons(MPA_V2_RDMA_WRITE_RTR);
1144                         else if (p2p_type == FW_RI_INIT_P2PTYPE_READ_REQ)
1145                                 mpa_v2_params.ord |=
1146                                         htons(MPA_V2_RDMA_READ_RTR);
1147                 }
1148
1149                 memcpy(mpa->private_data, &mpa_v2_params,
1150                        sizeof(struct mpa_v2_conn_params));
1151
1152                 if (ep->plen)
1153                         memcpy(mpa->private_data +
1154                                sizeof(struct mpa_v2_conn_params), pdata, plen);
1155         } else
1156                 if (plen)
1157                         memcpy(mpa->private_data, pdata, plen);
1158
1159         /*
1160          * Reference the mpa skb.  This ensures the data area
1161          * will remain in memory until the hw acks the tx.
1162          * Function fw4_ack() will deref it.
1163          */
1164         skb_get(skb);
1165         t4_set_arp_err_handler(skb, NULL, mpa_start_arp_failure);
1166         ep->mpa_skb = skb;
1167         __state_set(&ep->com, MPA_REP_SENT);
1168         ep->snd_seq += mpalen;
1169         return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
1170 }
1171
1172 static int act_establish(struct c4iw_dev *dev, struct sk_buff *skb)
1173 {
1174         struct c4iw_ep *ep;
1175         struct cpl_act_establish *req = cplhdr(skb);
1176         unsigned short tcp_opt = ntohs(req->tcp_opt);
1177         unsigned int tid = GET_TID(req);
1178         unsigned int atid = TID_TID_G(ntohl(req->tos_atid));
1179         struct tid_info *t = dev->rdev.lldi.tids;
1180         int ret;
1181
1182         ep = lookup_atid(t, atid);
1183
1184         pr_debug("ep %p tid %u snd_isn %u rcv_isn %u\n", ep, tid,
1185                  be32_to_cpu(req->snd_isn), be32_to_cpu(req->rcv_isn));
1186
1187         mutex_lock(&ep->com.mutex);
1188         dst_confirm(ep->dst);
1189
1190         /* setup the hwtid for this connection */
1191         ep->hwtid = tid;
1192         cxgb4_insert_tid(t, ep, tid, ep->com.local_addr.ss_family);
1193         insert_ep_tid(ep);
1194
1195         ep->snd_seq = be32_to_cpu(req->snd_isn);
1196         ep->rcv_seq = be32_to_cpu(req->rcv_isn);
1197         ep->snd_wscale = TCPOPT_SND_WSCALE_G(tcp_opt);
1198
1199         set_emss(ep, tcp_opt);
1200
1201         /* dealloc the atid */
1202         remove_handle(ep->com.dev, &ep->com.dev->atid_idr, atid);
1203         cxgb4_free_atid(t, atid);
1204         set_bit(ACT_ESTAB, &ep->com.history);
1205
1206         /* start MPA negotiation */
1207         ret = send_flowc(ep);
1208         if (ret)
1209                 goto err;
1210         if (ep->retry_with_mpa_v1)
1211                 ret = send_mpa_req(ep, skb, 1);
1212         else
1213                 ret = send_mpa_req(ep, skb, mpa_rev);
1214         if (ret)
1215                 goto err;
1216         mutex_unlock(&ep->com.mutex);
1217         return 0;
1218 err:
1219         mutex_unlock(&ep->com.mutex);
1220         connect_reply_upcall(ep, -ENOMEM);
1221         c4iw_ep_disconnect(ep, 0, GFP_KERNEL);
1222         return 0;
1223 }
1224
1225 static void close_complete_upcall(struct c4iw_ep *ep, int status)
1226 {
1227         struct iw_cm_event event;
1228
1229         pr_debug("ep %p tid %u\n", ep, ep->hwtid);
1230         memset(&event, 0, sizeof(event));
1231         event.event = IW_CM_EVENT_CLOSE;
1232         event.status = status;
1233         if (ep->com.cm_id) {
1234                 pr_debug("close complete delivered ep %p cm_id %p tid %u\n",
1235                          ep, ep->com.cm_id, ep->hwtid);
1236                 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
1237                 deref_cm_id(&ep->com);
1238                 set_bit(CLOSE_UPCALL, &ep->com.history);
1239         }
1240 }
1241
1242 static void peer_close_upcall(struct c4iw_ep *ep)
1243 {
1244         struct iw_cm_event event;
1245
1246         pr_debug("ep %p tid %u\n", ep, ep->hwtid);
1247         memset(&event, 0, sizeof(event));
1248         event.event = IW_CM_EVENT_DISCONNECT;
1249         if (ep->com.cm_id) {
1250                 pr_debug("peer close delivered ep %p cm_id %p tid %u\n",
1251                          ep, ep->com.cm_id, ep->hwtid);
1252                 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
1253                 set_bit(DISCONN_UPCALL, &ep->com.history);
1254         }
1255 }
1256
1257 static void peer_abort_upcall(struct c4iw_ep *ep)
1258 {
1259         struct iw_cm_event event;
1260
1261         pr_debug("ep %p tid %u\n", ep, ep->hwtid);
1262         memset(&event, 0, sizeof(event));
1263         event.event = IW_CM_EVENT_CLOSE;
1264         event.status = -ECONNRESET;
1265         if (ep->com.cm_id) {
1266                 pr_debug("abort delivered ep %p cm_id %p tid %u\n", ep,
1267                          ep->com.cm_id, ep->hwtid);
1268                 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
1269                 deref_cm_id(&ep->com);
1270                 set_bit(ABORT_UPCALL, &ep->com.history);
1271         }
1272 }
1273
1274 static void connect_reply_upcall(struct c4iw_ep *ep, int status)
1275 {
1276         struct iw_cm_event event;
1277
1278         pr_debug("ep %p tid %u status %d\n",
1279                  ep, ep->hwtid, status);
1280         memset(&event, 0, sizeof(event));
1281         event.event = IW_CM_EVENT_CONNECT_REPLY;
1282         event.status = status;
1283         memcpy(&event.local_addr, &ep->com.local_addr,
1284                sizeof(ep->com.local_addr));
1285         memcpy(&event.remote_addr, &ep->com.remote_addr,
1286                sizeof(ep->com.remote_addr));
1287
1288         if ((status == 0) || (status == -ECONNREFUSED)) {
1289                 if (!ep->tried_with_mpa_v1) {
1290                         /* this means MPA_v2 is used */
1291                         event.ord = ep->ird;
1292                         event.ird = ep->ord;
1293                         event.private_data_len = ep->plen -
1294                                 sizeof(struct mpa_v2_conn_params);
1295                         event.private_data = ep->mpa_pkt +
1296                                 sizeof(struct mpa_message) +
1297                                 sizeof(struct mpa_v2_conn_params);
1298                 } else {
1299                         /* this means MPA_v1 is used */
1300                         event.ord = cur_max_read_depth(ep->com.dev);
1301                         event.ird = cur_max_read_depth(ep->com.dev);
1302                         event.private_data_len = ep->plen;
1303                         event.private_data = ep->mpa_pkt +
1304                                 sizeof(struct mpa_message);
1305                 }
1306         }
1307
1308         pr_debug("ep %p tid %u status %d\n", ep,
1309                  ep->hwtid, status);
1310         set_bit(CONN_RPL_UPCALL, &ep->com.history);
1311         ep->com.cm_id->event_handler(ep->com.cm_id, &event);
1312
1313         if (status < 0)
1314                 deref_cm_id(&ep->com);
1315 }
1316
1317 static int connect_request_upcall(struct c4iw_ep *ep)
1318 {
1319         struct iw_cm_event event;
1320         int ret;
1321
1322         pr_debug("ep %p tid %u\n", ep, ep->hwtid);
1323         memset(&event, 0, sizeof(event));
1324         event.event = IW_CM_EVENT_CONNECT_REQUEST;
1325         memcpy(&event.local_addr, &ep->com.local_addr,
1326                sizeof(ep->com.local_addr));
1327         memcpy(&event.remote_addr, &ep->com.remote_addr,
1328                sizeof(ep->com.remote_addr));
1329         event.provider_data = ep;
1330         if (!ep->tried_with_mpa_v1) {
1331                 /* this means MPA_v2 is used */
1332                 event.ord = ep->ord;
1333                 event.ird = ep->ird;
1334                 event.private_data_len = ep->plen -
1335                         sizeof(struct mpa_v2_conn_params);
1336                 event.private_data = ep->mpa_pkt + sizeof(struct mpa_message) +
1337                         sizeof(struct mpa_v2_conn_params);
1338         } else {
1339                 /* this means MPA_v1 is used. Send max supported */
1340                 event.ord = cur_max_read_depth(ep->com.dev);
1341                 event.ird = cur_max_read_depth(ep->com.dev);
1342                 event.private_data_len = ep->plen;
1343                 event.private_data = ep->mpa_pkt + sizeof(struct mpa_message);
1344         }
1345         c4iw_get_ep(&ep->com);
1346         ret = ep->parent_ep->com.cm_id->event_handler(ep->parent_ep->com.cm_id,
1347                                                       &event);
1348         if (ret)
1349                 c4iw_put_ep(&ep->com);
1350         set_bit(CONNREQ_UPCALL, &ep->com.history);
1351         c4iw_put_ep(&ep->parent_ep->com);
1352         return ret;
1353 }
1354
1355 static void established_upcall(struct c4iw_ep *ep)
1356 {
1357         struct iw_cm_event event;
1358
1359         pr_debug("ep %p tid %u\n", ep, ep->hwtid);
1360         memset(&event, 0, sizeof(event));
1361         event.event = IW_CM_EVENT_ESTABLISHED;
1362         event.ird = ep->ord;
1363         event.ord = ep->ird;
1364         if (ep->com.cm_id) {
1365                 pr_debug("ep %p tid %u\n", ep, ep->hwtid);
1366                 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
1367                 set_bit(ESTAB_UPCALL, &ep->com.history);
1368         }
1369 }
1370
1371 static int update_rx_credits(struct c4iw_ep *ep, u32 credits)
1372 {
1373         struct sk_buff *skb;
1374         u32 wrlen = roundup(sizeof(struct cpl_rx_data_ack), 16);
1375         u32 credit_dack;
1376
1377         pr_debug("ep %p tid %u credits %u\n",
1378                  ep, ep->hwtid, credits);
1379         skb = get_skb(NULL, wrlen, GFP_KERNEL);
1380         if (!skb) {
1381                 pr_err("update_rx_credits - cannot alloc skb!\n");
1382                 return 0;
1383         }
1384
1385         /*
1386          * If we couldn't specify the entire rcv window at connection setup
1387          * due to the limit in the number of bits in the RCV_BUFSIZ field,
1388          * then add the overage in to the credits returned.
1389          */
1390         if (ep->rcv_win > RCV_BUFSIZ_M * 1024)
1391                 credits += ep->rcv_win - RCV_BUFSIZ_M * 1024;
1392
1393         credit_dack = credits | RX_FORCE_ACK_F | RX_DACK_CHANGE_F |
1394                       RX_DACK_MODE_V(dack_mode);
1395
1396         cxgb_mk_rx_data_ack(skb, wrlen, ep->hwtid, ep->ctrlq_idx,
1397                             credit_dack);
1398
1399         c4iw_ofld_send(&ep->com.dev->rdev, skb);
1400         return credits;
1401 }
1402
1403 #define RELAXED_IRD_NEGOTIATION 1
1404
1405 /*
1406  * process_mpa_reply - process streaming mode MPA reply
1407  *
1408  * Returns:
1409  *
1410  * 0 upon success indicating a connect request was delivered to the ULP
1411  * or the mpa request is incomplete but valid so far.
1412  *
1413  * 1 if a failure requires the caller to close the connection.
1414  *
1415  * 2 if a failure requires the caller to abort the connection.
1416  */
1417 static int process_mpa_reply(struct c4iw_ep *ep, struct sk_buff *skb)
1418 {
1419         struct mpa_message *mpa;
1420         struct mpa_v2_conn_params *mpa_v2_params;
1421         u16 plen;
1422         u16 resp_ird, resp_ord;
1423         u8 rtr_mismatch = 0, insuff_ird = 0;
1424         struct c4iw_qp_attributes attrs;
1425         enum c4iw_qp_attr_mask mask;
1426         int err;
1427         int disconnect = 0;
1428
1429         pr_debug("ep %p tid %u\n", ep, ep->hwtid);
1430
1431         /*
1432          * If we get more than the supported amount of private data
1433          * then we must fail this connection.
1434          */
1435         if (ep->mpa_pkt_len + skb->len > sizeof(ep->mpa_pkt)) {
1436                 err = -EINVAL;
1437                 goto err_stop_timer;
1438         }
1439
1440         /*
1441          * copy the new data into our accumulation buffer.
1442          */
1443         skb_copy_from_linear_data(skb, &(ep->mpa_pkt[ep->mpa_pkt_len]),
1444                                   skb->len);
1445         ep->mpa_pkt_len += skb->len;
1446
1447         /*
1448          * if we don't even have the mpa message, then bail.
1449          */
1450         if (ep->mpa_pkt_len < sizeof(*mpa))
1451                 return 0;
1452         mpa = (struct mpa_message *) ep->mpa_pkt;
1453
1454         /* Validate MPA header. */
1455         if (mpa->revision > mpa_rev) {
1456                 pr_err("%s MPA version mismatch. Local = %d, Received = %d\n",
1457                        __func__, mpa_rev, mpa->revision);
1458                 err = -EPROTO;
1459                 goto err_stop_timer;
1460         }
1461         if (memcmp(mpa->key, MPA_KEY_REP, sizeof(mpa->key))) {
1462                 err = -EPROTO;
1463                 goto err_stop_timer;
1464         }
1465
1466         plen = ntohs(mpa->private_data_size);
1467
1468         /*
1469          * Fail if there's too much private data.
1470          */
1471         if (plen > MPA_MAX_PRIVATE_DATA) {
1472                 err = -EPROTO;
1473                 goto err_stop_timer;
1474         }
1475
1476         /*
1477          * If plen does not account for pkt size
1478          */
1479         if (ep->mpa_pkt_len > (sizeof(*mpa) + plen)) {
1480                 err = -EPROTO;
1481                 goto err_stop_timer;
1482         }
1483
1484         ep->plen = (u8) plen;
1485
1486         /*
1487          * If we don't have all the pdata yet, then bail.
1488          * We'll continue process when more data arrives.
1489          */
1490         if (ep->mpa_pkt_len < (sizeof(*mpa) + plen))
1491                 return 0;
1492
1493         if (mpa->flags & MPA_REJECT) {
1494                 err = -ECONNREFUSED;
1495                 goto err_stop_timer;
1496         }
1497
1498         /*
1499          * Stop mpa timer.  If it expired, then
1500          * we ignore the MPA reply.  process_timeout()
1501          * will abort the connection.
1502          */
1503         if (stop_ep_timer(ep))
1504                 return 0;
1505
1506         /*
1507          * If we get here we have accumulated the entire mpa
1508          * start reply message including private data. And
1509          * the MPA header is valid.
1510          */
1511         __state_set(&ep->com, FPDU_MODE);
1512         ep->mpa_attr.crc_enabled = (mpa->flags & MPA_CRC) | crc_enabled ? 1 : 0;
1513         ep->mpa_attr.xmit_marker_enabled = mpa->flags & MPA_MARKERS ? 1 : 0;
1514         ep->mpa_attr.version = mpa->revision;
1515         ep->mpa_attr.p2p_type = FW_RI_INIT_P2PTYPE_DISABLED;
1516
1517         if (mpa->revision == 2) {
1518                 ep->mpa_attr.enhanced_rdma_conn =
1519                         mpa->flags & MPA_ENHANCED_RDMA_CONN ? 1 : 0;
1520                 if (ep->mpa_attr.enhanced_rdma_conn) {
1521                         mpa_v2_params = (struct mpa_v2_conn_params *)
1522                                 (ep->mpa_pkt + sizeof(*mpa));
1523                         resp_ird = ntohs(mpa_v2_params->ird) &
1524                                 MPA_V2_IRD_ORD_MASK;
1525                         resp_ord = ntohs(mpa_v2_params->ord) &
1526                                 MPA_V2_IRD_ORD_MASK;
1527                         pr_debug("responder ird %u ord %u ep ird %u ord %u\n",
1528                                  resp_ird, resp_ord, ep->ird, ep->ord);
1529
1530                         /*
1531                          * This is a double-check. Ideally, below checks are
1532                          * not required since ird/ord stuff has been taken
1533                          * care of in c4iw_accept_cr
1534                          */
1535                         if (ep->ird < resp_ord) {
1536                                 if (RELAXED_IRD_NEGOTIATION && resp_ord <=
1537                                     ep->com.dev->rdev.lldi.max_ordird_qp)
1538                                         ep->ird = resp_ord;
1539                                 else
1540                                         insuff_ird = 1;
1541                         } else if (ep->ird > resp_ord) {
1542                                 ep->ird = resp_ord;
1543                         }
1544                         if (ep->ord > resp_ird) {
1545                                 if (RELAXED_IRD_NEGOTIATION)
1546                                         ep->ord = resp_ird;
1547                                 else
1548                                         insuff_ird = 1;
1549                         }
1550                         if (insuff_ird) {
1551                                 err = -ENOMEM;
1552                                 ep->ird = resp_ord;
1553                                 ep->ord = resp_ird;
1554                         }
1555
1556                         if (ntohs(mpa_v2_params->ird) &
1557                                         MPA_V2_PEER2PEER_MODEL) {
1558                                 if (ntohs(mpa_v2_params->ord) &
1559                                                 MPA_V2_RDMA_WRITE_RTR)
1560                                         ep->mpa_attr.p2p_type =
1561                                                 FW_RI_INIT_P2PTYPE_RDMA_WRITE;
1562                                 else if (ntohs(mpa_v2_params->ord) &
1563                                                 MPA_V2_RDMA_READ_RTR)
1564                                         ep->mpa_attr.p2p_type =
1565                                                 FW_RI_INIT_P2PTYPE_READ_REQ;
1566                         }
1567                 }
1568         } else if (mpa->revision == 1)
1569                 if (peer2peer)
1570                         ep->mpa_attr.p2p_type = p2p_type;
1571
1572         pr_debug("crc_enabled=%d, recv_marker_enabled=%d, xmit_marker_enabled=%d, version=%d p2p_type=%d local-p2p_type = %d\n",
1573                  ep->mpa_attr.crc_enabled,
1574                  ep->mpa_attr.recv_marker_enabled,
1575                  ep->mpa_attr.xmit_marker_enabled, ep->mpa_attr.version,
1576                  ep->mpa_attr.p2p_type, p2p_type);
1577
1578         /*
1579          * If responder's RTR does not match with that of initiator, assign
1580          * FW_RI_INIT_P2PTYPE_DISABLED in mpa attributes so that RTR is not
1581          * generated when moving QP to RTS state.
1582          * A TERM message will be sent after QP has moved to RTS state
1583          */
1584         if ((ep->mpa_attr.version == 2) && peer2peer &&
1585                         (ep->mpa_attr.p2p_type != p2p_type)) {
1586                 ep->mpa_attr.p2p_type = FW_RI_INIT_P2PTYPE_DISABLED;
1587                 rtr_mismatch = 1;
1588         }
1589
1590         attrs.mpa_attr = ep->mpa_attr;
1591         attrs.max_ird = ep->ird;
1592         attrs.max_ord = ep->ord;
1593         attrs.llp_stream_handle = ep;
1594         attrs.next_state = C4IW_QP_STATE_RTS;
1595
1596         mask = C4IW_QP_ATTR_NEXT_STATE |
1597             C4IW_QP_ATTR_LLP_STREAM_HANDLE | C4IW_QP_ATTR_MPA_ATTR |
1598             C4IW_QP_ATTR_MAX_IRD | C4IW_QP_ATTR_MAX_ORD;
1599
1600         /* bind QP and TID with INIT_WR */
1601         err = c4iw_modify_qp(ep->com.qp->rhp,
1602                              ep->com.qp, mask, &attrs, 1);
1603         if (err)
1604                 goto err;
1605
1606         /*
1607          * If responder's RTR requirement did not match with what initiator
1608          * supports, generate TERM message
1609          */
1610         if (rtr_mismatch) {
1611                 pr_err("%s: RTR mismatch, sending TERM\n", __func__);
1612                 attrs.layer_etype = LAYER_MPA | DDP_LLP;
1613                 attrs.ecode = MPA_NOMATCH_RTR;
1614                 attrs.next_state = C4IW_QP_STATE_TERMINATE;
1615                 attrs.send_term = 1;
1616                 err = c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
1617                                 C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
1618                 err = -ENOMEM;
1619                 disconnect = 1;
1620                 goto out;
1621         }
1622
1623         /*
1624          * Generate TERM if initiator IRD is not sufficient for responder
1625          * provided ORD. Currently, we do the same behaviour even when
1626          * responder provided IRD is also not sufficient as regards to
1627          * initiator ORD.
1628          */
1629         if (insuff_ird) {
1630                 pr_err("%s: Insufficient IRD, sending TERM\n", __func__);
1631                 attrs.layer_etype = LAYER_MPA | DDP_LLP;
1632                 attrs.ecode = MPA_INSUFF_IRD;
1633                 attrs.next_state = C4IW_QP_STATE_TERMINATE;
1634                 attrs.send_term = 1;
1635                 err = c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
1636                                 C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
1637                 err = -ENOMEM;
1638                 disconnect = 1;
1639                 goto out;
1640         }
1641         goto out;
1642 err_stop_timer:
1643         stop_ep_timer(ep);
1644 err:
1645         disconnect = 2;
1646 out:
1647         connect_reply_upcall(ep, err);
1648         return disconnect;
1649 }
1650
1651 /*
1652  * process_mpa_request - process streaming mode MPA request
1653  *
1654  * Returns:
1655  *
1656  * 0 upon success indicating a connect request was delivered to the ULP
1657  * or the mpa request is incomplete but valid so far.
1658  *
1659  * 1 if a failure requires the caller to close the connection.
1660  *
1661  * 2 if a failure requires the caller to abort the connection.
1662  */
1663 static int process_mpa_request(struct c4iw_ep *ep, struct sk_buff *skb)
1664 {
1665         struct mpa_message *mpa;
1666         struct mpa_v2_conn_params *mpa_v2_params;
1667         u16 plen;
1668
1669         pr_debug("ep %p tid %u\n", ep, ep->hwtid);
1670
1671         /*
1672          * If we get more than the supported amount of private data
1673          * then we must fail this connection.
1674          */
1675         if (ep->mpa_pkt_len + skb->len > sizeof(ep->mpa_pkt))
1676                 goto err_stop_timer;
1677
1678         pr_debug("enter (%s line %u)\n", __FILE__, __LINE__);
1679
1680         /*
1681          * Copy the new data into our accumulation buffer.
1682          */
1683         skb_copy_from_linear_data(skb, &(ep->mpa_pkt[ep->mpa_pkt_len]),
1684                                   skb->len);
1685         ep->mpa_pkt_len += skb->len;
1686
1687         /*
1688          * If we don't even have the mpa message, then bail.
1689          * We'll continue process when more data arrives.
1690          */
1691         if (ep->mpa_pkt_len < sizeof(*mpa))
1692                 return 0;
1693
1694         pr_debug("enter (%s line %u)\n", __FILE__, __LINE__);
1695         mpa = (struct mpa_message *) ep->mpa_pkt;
1696
1697         /*
1698          * Validate MPA Header.
1699          */
1700         if (mpa->revision > mpa_rev) {
1701                 pr_err("%s MPA version mismatch. Local = %d, Received = %d\n",
1702                        __func__, mpa_rev, mpa->revision);
1703                 goto err_stop_timer;
1704         }
1705
1706         if (memcmp(mpa->key, MPA_KEY_REQ, sizeof(mpa->key)))
1707                 goto err_stop_timer;
1708
1709         plen = ntohs(mpa->private_data_size);
1710
1711         /*
1712          * Fail if there's too much private data.
1713          */
1714         if (plen > MPA_MAX_PRIVATE_DATA)
1715                 goto err_stop_timer;
1716
1717         /*
1718          * If plen does not account for pkt size
1719          */
1720         if (ep->mpa_pkt_len > (sizeof(*mpa) + plen))
1721                 goto err_stop_timer;
1722         ep->plen = (u8) plen;
1723
1724         /*
1725          * If we don't have all the pdata yet, then bail.
1726          */
1727         if (ep->mpa_pkt_len < (sizeof(*mpa) + plen))
1728                 return 0;
1729
1730         /*
1731          * If we get here we have accumulated the entire mpa
1732          * start reply message including private data.
1733          */
1734         ep->mpa_attr.initiator = 0;
1735         ep->mpa_attr.crc_enabled = (mpa->flags & MPA_CRC) | crc_enabled ? 1 : 0;
1736         ep->mpa_attr.recv_marker_enabled = markers_enabled;
1737         ep->mpa_attr.xmit_marker_enabled = mpa->flags & MPA_MARKERS ? 1 : 0;
1738         ep->mpa_attr.version = mpa->revision;
1739         if (mpa->revision == 1)
1740                 ep->tried_with_mpa_v1 = 1;
1741         ep->mpa_attr.p2p_type = FW_RI_INIT_P2PTYPE_DISABLED;
1742
1743         if (mpa->revision == 2) {
1744                 ep->mpa_attr.enhanced_rdma_conn =
1745                         mpa->flags & MPA_ENHANCED_RDMA_CONN ? 1 : 0;
1746                 if (ep->mpa_attr.enhanced_rdma_conn) {
1747                         mpa_v2_params = (struct mpa_v2_conn_params *)
1748                                 (ep->mpa_pkt + sizeof(*mpa));
1749                         ep->ird = ntohs(mpa_v2_params->ird) &
1750                                 MPA_V2_IRD_ORD_MASK;
1751                         ep->ird = min_t(u32, ep->ird,
1752                                         cur_max_read_depth(ep->com.dev));
1753                         ep->ord = ntohs(mpa_v2_params->ord) &
1754                                 MPA_V2_IRD_ORD_MASK;
1755                         ep->ord = min_t(u32, ep->ord,
1756                                         cur_max_read_depth(ep->com.dev));
1757                         pr_debug("initiator ird %u ord %u\n",
1758                                  ep->ird, ep->ord);
1759                         if (ntohs(mpa_v2_params->ird) & MPA_V2_PEER2PEER_MODEL)
1760                                 if (peer2peer) {
1761                                         if (ntohs(mpa_v2_params->ord) &
1762                                                         MPA_V2_RDMA_WRITE_RTR)
1763                                                 ep->mpa_attr.p2p_type =
1764                                                 FW_RI_INIT_P2PTYPE_RDMA_WRITE;
1765                                         else if (ntohs(mpa_v2_params->ord) &
1766                                                         MPA_V2_RDMA_READ_RTR)
1767                                                 ep->mpa_attr.p2p_type =
1768                                                 FW_RI_INIT_P2PTYPE_READ_REQ;
1769                                 }
1770                 }
1771         } else if (mpa->revision == 1)
1772                 if (peer2peer)
1773                         ep->mpa_attr.p2p_type = p2p_type;
1774
1775         pr_debug("crc_enabled=%d, recv_marker_enabled=%d, xmit_marker_enabled=%d, version=%d p2p_type=%d\n",
1776                  ep->mpa_attr.crc_enabled, ep->mpa_attr.recv_marker_enabled,
1777                  ep->mpa_attr.xmit_marker_enabled, ep->mpa_attr.version,
1778                  ep->mpa_attr.p2p_type);
1779
1780         __state_set(&ep->com, MPA_REQ_RCVD);
1781
1782         /* drive upcall */
1783         mutex_lock_nested(&ep->parent_ep->com.mutex, SINGLE_DEPTH_NESTING);
1784         if (ep->parent_ep->com.state != DEAD) {
1785                 if (connect_request_upcall(ep))
1786                         goto err_unlock_parent;
1787         } else {
1788                 goto err_unlock_parent;
1789         }
1790         mutex_unlock(&ep->parent_ep->com.mutex);
1791         return 0;
1792
1793 err_unlock_parent:
1794         mutex_unlock(&ep->parent_ep->com.mutex);
1795         goto err_out;
1796 err_stop_timer:
1797         (void)stop_ep_timer(ep);
1798 err_out:
1799         return 2;
1800 }
1801
1802 static int rx_data(struct c4iw_dev *dev, struct sk_buff *skb)
1803 {
1804         struct c4iw_ep *ep;
1805         struct cpl_rx_data *hdr = cplhdr(skb);
1806         unsigned int dlen = ntohs(hdr->len);
1807         unsigned int tid = GET_TID(hdr);
1808         __u8 status = hdr->status;
1809         int disconnect = 0;
1810
1811         ep = get_ep_from_tid(dev, tid);
1812         if (!ep)
1813                 return 0;
1814         pr_debug("ep %p tid %u dlen %u\n", ep, ep->hwtid, dlen);
1815         skb_pull(skb, sizeof(*hdr));
1816         skb_trim(skb, dlen);
1817         mutex_lock(&ep->com.mutex);
1818
1819         switch (ep->com.state) {
1820         case MPA_REQ_SENT:
1821                 update_rx_credits(ep, dlen);
1822                 ep->rcv_seq += dlen;
1823                 disconnect = process_mpa_reply(ep, skb);
1824                 break;
1825         case MPA_REQ_WAIT:
1826                 update_rx_credits(ep, dlen);
1827                 ep->rcv_seq += dlen;
1828                 disconnect = process_mpa_request(ep, skb);
1829                 break;
1830         case FPDU_MODE: {
1831                 struct c4iw_qp_attributes attrs;
1832
1833                 update_rx_credits(ep, dlen);
1834                 if (status)
1835                         pr_err("%s Unexpected streaming data." \
1836                                " qpid %u ep %p state %d tid %u status %d\n",
1837                                __func__, ep->com.qp->wq.sq.qid, ep,
1838                                ep->com.state, ep->hwtid, status);
1839                 attrs.next_state = C4IW_QP_STATE_TERMINATE;
1840                 c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
1841                                C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
1842                 disconnect = 1;
1843                 break;
1844         }
1845         default:
1846                 break;
1847         }
1848         mutex_unlock(&ep->com.mutex);
1849         if (disconnect)
1850                 c4iw_ep_disconnect(ep, disconnect == 2, GFP_KERNEL);
1851         c4iw_put_ep(&ep->com);
1852         return 0;
1853 }
1854
1855 static void complete_cached_srq_buffers(struct c4iw_ep *ep,
1856                                         __be32 srqidx_status)
1857 {
1858         enum chip_type adapter_type;
1859         u32 srqidx;
1860
1861         adapter_type = ep->com.dev->rdev.lldi.adapter_type;
1862         srqidx = ABORT_RSS_SRQIDX_G(be32_to_cpu(srqidx_status));
1863
1864         /*
1865          * If this TCB had a srq buffer cached, then we must complete
1866          * it. For user mode, that means saving the srqidx in the
1867          * user/kernel status page for this qp.  For kernel mode, just
1868          * synthesize the CQE now.
1869          */
1870         if (CHELSIO_CHIP_VERSION(adapter_type) > CHELSIO_T5 && srqidx) {
1871                 if (ep->com.qp->ibqp.uobject)
1872                         t4_set_wq_in_error(&ep->com.qp->wq, srqidx);
1873                 else
1874                         c4iw_flush_srqidx(ep->com.qp, srqidx);
1875         }
1876 }
1877
1878 static int abort_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
1879 {
1880         struct c4iw_ep *ep;
1881         struct cpl_abort_rpl_rss6 *rpl = cplhdr(skb);
1882         int release = 0;
1883         unsigned int tid = GET_TID(rpl);
1884
1885         ep = get_ep_from_tid(dev, tid);
1886         if (!ep) {
1887                 pr_warn("Abort rpl to freed endpoint\n");
1888                 return 0;
1889         }
1890
1891         complete_cached_srq_buffers(ep, rpl->srqidx_status);
1892
1893         pr_debug("ep %p tid %u\n", ep, ep->hwtid);
1894         mutex_lock(&ep->com.mutex);
1895         switch (ep->com.state) {
1896         case ABORTING:
1897                 c4iw_wake_up_noref(ep->com.wr_waitp, -ECONNRESET);
1898                 __state_set(&ep->com, DEAD);
1899                 release = 1;
1900                 break;
1901         default:
1902                 pr_err("%s ep %p state %d\n", __func__, ep, ep->com.state);
1903                 break;
1904         }
1905         mutex_unlock(&ep->com.mutex);
1906
1907         if (release) {
1908                 close_complete_upcall(ep, -ECONNRESET);
1909                 release_ep_resources(ep);
1910         }
1911         c4iw_put_ep(&ep->com);
1912         return 0;
1913 }
1914
1915 static int send_fw_act_open_req(struct c4iw_ep *ep, unsigned int atid)
1916 {
1917         struct sk_buff *skb;
1918         struct fw_ofld_connection_wr *req;
1919         unsigned int mtu_idx;
1920         u32 wscale;
1921         struct sockaddr_in *sin;
1922         int win;
1923
1924         skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
1925         req = __skb_put_zero(skb, sizeof(*req));
1926         req->op_compl = htonl(WR_OP_V(FW_OFLD_CONNECTION_WR));
1927         req->len16_pkd = htonl(FW_WR_LEN16_V(DIV_ROUND_UP(sizeof(*req), 16)));
1928         req->le.filter = cpu_to_be32(cxgb4_select_ntuple(
1929                                      ep->com.dev->rdev.lldi.ports[0],
1930                                      ep->l2t));
1931         sin = (struct sockaddr_in *)&ep->com.local_addr;
1932         req->le.lport = sin->sin_port;
1933         req->le.u.ipv4.lip = sin->sin_addr.s_addr;
1934         sin = (struct sockaddr_in *)&ep->com.remote_addr;
1935         req->le.pport = sin->sin_port;
1936         req->le.u.ipv4.pip = sin->sin_addr.s_addr;
1937         req->tcb.t_state_to_astid =
1938                         htonl(FW_OFLD_CONNECTION_WR_T_STATE_V(TCP_SYN_SENT) |
1939                         FW_OFLD_CONNECTION_WR_ASTID_V(atid));
1940         req->tcb.cplrxdataack_cplpassacceptrpl =
1941                         htons(FW_OFLD_CONNECTION_WR_CPLRXDATAACK_F);
1942         req->tcb.tx_max = (__force __be32) jiffies;
1943         req->tcb.rcv_adv = htons(1);
1944         cxgb_best_mtu(ep->com.dev->rdev.lldi.mtus, ep->mtu, &mtu_idx,
1945                       enable_tcp_timestamps,
1946                       (ep->com.remote_addr.ss_family == AF_INET) ? 0 : 1);
1947         wscale = cxgb_compute_wscale(rcv_win);
1948
1949         /*
1950          * Specify the largest window that will fit in opt0. The
1951          * remainder will be specified in the rx_data_ack.
1952          */
1953         win = ep->rcv_win >> 10;
1954         if (win > RCV_BUFSIZ_M)
1955                 win = RCV_BUFSIZ_M;
1956
1957         req->tcb.opt0 = (__force __be64) (TCAM_BYPASS_F |
1958                 (nocong ? NO_CONG_F : 0) |
1959                 KEEP_ALIVE_F |
1960                 DELACK_F |
1961                 WND_SCALE_V(wscale) |
1962                 MSS_IDX_V(mtu_idx) |
1963                 L2T_IDX_V(ep->l2t->idx) |
1964                 TX_CHAN_V(ep->tx_chan) |
1965                 SMAC_SEL_V(ep->smac_idx) |
1966                 DSCP_V(ep->tos >> 2) |
1967                 ULP_MODE_V(ULP_MODE_TCPDDP) |
1968                 RCV_BUFSIZ_V(win));
1969         req->tcb.opt2 = (__force __be32) (PACE_V(1) |
1970                 TX_QUEUE_V(ep->com.dev->rdev.lldi.tx_modq[ep->tx_chan]) |
1971                 RX_CHANNEL_V(0) |
1972                 CCTRL_ECN_V(enable_ecn) |
1973                 RSS_QUEUE_VALID_F | RSS_QUEUE_V(ep->rss_qid));
1974         if (enable_tcp_timestamps)
1975                 req->tcb.opt2 |= (__force __be32)TSTAMPS_EN_F;
1976         if (enable_tcp_sack)
1977                 req->tcb.opt2 |= (__force __be32)SACK_EN_F;
1978         if (wscale && enable_tcp_window_scaling)
1979                 req->tcb.opt2 |= (__force __be32)WND_SCALE_EN_F;
1980         req->tcb.opt0 = cpu_to_be64((__force u64)req->tcb.opt0);
1981         req->tcb.opt2 = cpu_to_be32((__force u32)req->tcb.opt2);
1982         set_wr_txq(skb, CPL_PRIORITY_CONTROL, ep->ctrlq_idx);
1983         set_bit(ACT_OFLD_CONN, &ep->com.history);
1984         return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
1985 }
1986
1987 /*
1988  * Some of the error codes above implicitly indicate that there is no TID
1989  * allocated with the result of an ACT_OPEN.  We use this predicate to make
1990  * that explicit.
1991  */
1992 static inline int act_open_has_tid(int status)
1993 {
1994         return (status != CPL_ERR_TCAM_PARITY &&
1995                 status != CPL_ERR_TCAM_MISS &&
1996                 status != CPL_ERR_TCAM_FULL &&
1997                 status != CPL_ERR_CONN_EXIST_SYNRECV &&
1998                 status != CPL_ERR_CONN_EXIST);
1999 }
2000
2001 static char *neg_adv_str(unsigned int status)
2002 {
2003         switch (status) {
2004         case CPL_ERR_RTX_NEG_ADVICE:
2005                 return "Retransmit timeout";
2006         case CPL_ERR_PERSIST_NEG_ADVICE:
2007                 return "Persist timeout";
2008         case CPL_ERR_KEEPALV_NEG_ADVICE:
2009                 return "Keepalive timeout";
2010         default:
2011                 return "Unknown";
2012         }
2013 }
2014
2015 static void set_tcp_window(struct c4iw_ep *ep, struct port_info *pi)
2016 {
2017         ep->snd_win = snd_win;
2018         ep->rcv_win = rcv_win;
2019         pr_debug("snd_win %d rcv_win %d\n",
2020                  ep->snd_win, ep->rcv_win);
2021 }
2022
2023 #define ACT_OPEN_RETRY_COUNT 2
2024
2025 static int import_ep(struct c4iw_ep *ep, int iptype, __u8 *peer_ip,
2026                      struct dst_entry *dst, struct c4iw_dev *cdev,
2027                      bool clear_mpa_v1, enum chip_type adapter_type, u8 tos)
2028 {
2029         struct neighbour *n;
2030         int err, step;
2031         struct net_device *pdev;
2032
2033         n = dst_neigh_lookup(dst, peer_ip);
2034         if (!n)
2035                 return -ENODEV;
2036
2037         rcu_read_lock();
2038         err = -ENOMEM;
2039         if (n->dev->flags & IFF_LOOPBACK) {
2040                 if (iptype == 4)
2041                         pdev = ip_dev_find(&init_net, *(__be32 *)peer_ip);
2042                 else if (IS_ENABLED(CONFIG_IPV6))
2043                         for_each_netdev(&init_net, pdev) {
2044                                 if (ipv6_chk_addr(&init_net,
2045                                                   (struct in6_addr *)peer_ip,
2046                                                   pdev, 1))
2047                                         break;
2048                         }
2049                 else
2050                         pdev = NULL;
2051
2052                 if (!pdev) {
2053                         err = -ENODEV;
2054                         goto out;
2055                 }
2056                 ep->l2t = cxgb4_l2t_get(cdev->rdev.lldi.l2t,
2057                                         n, pdev, rt_tos2priority(tos));
2058                 if (!ep->l2t) {
2059                         dev_put(pdev);
2060                         goto out;
2061                 }
2062                 ep->mtu = pdev->mtu;
2063                 ep->tx_chan = cxgb4_port_chan(pdev);
2064                 ep->smac_idx = cxgb4_tp_smt_idx(adapter_type,
2065                                                 cxgb4_port_viid(pdev));
2066                 step = cdev->rdev.lldi.ntxq /
2067                         cdev->rdev.lldi.nchan;
2068                 ep->txq_idx = cxgb4_port_idx(pdev) * step;
2069                 step = cdev->rdev.lldi.nrxq /
2070                         cdev->rdev.lldi.nchan;
2071                 ep->ctrlq_idx = cxgb4_port_idx(pdev);
2072                 ep->rss_qid = cdev->rdev.lldi.rxq_ids[
2073                         cxgb4_port_idx(pdev) * step];
2074                 set_tcp_window(ep, (struct port_info *)netdev_priv(pdev));
2075                 dev_put(pdev);
2076         } else {
2077                 pdev = get_real_dev(n->dev);
2078                 ep->l2t = cxgb4_l2t_get(cdev->rdev.lldi.l2t,
2079                                         n, pdev, rt_tos2priority(tos));
2080                 if (!ep->l2t)
2081                         goto out;
2082                 ep->mtu = dst_mtu(dst);
2083                 ep->tx_chan = cxgb4_port_chan(pdev);
2084                 ep->smac_idx = cxgb4_tp_smt_idx(adapter_type,
2085                                                 cxgb4_port_viid(pdev));
2086                 step = cdev->rdev.lldi.ntxq /
2087                         cdev->rdev.lldi.nchan;
2088                 ep->txq_idx = cxgb4_port_idx(pdev) * step;
2089                 ep->ctrlq_idx = cxgb4_port_idx(pdev);
2090                 step = cdev->rdev.lldi.nrxq /
2091                         cdev->rdev.lldi.nchan;
2092                 ep->rss_qid = cdev->rdev.lldi.rxq_ids[
2093                         cxgb4_port_idx(pdev) * step];
2094                 set_tcp_window(ep, (struct port_info *)netdev_priv(pdev));
2095
2096                 if (clear_mpa_v1) {
2097                         ep->retry_with_mpa_v1 = 0;
2098                         ep->tried_with_mpa_v1 = 0;
2099                 }
2100         }
2101         err = 0;
2102 out:
2103         rcu_read_unlock();
2104
2105         neigh_release(n);
2106
2107         return err;
2108 }
2109
2110 static int c4iw_reconnect(struct c4iw_ep *ep)
2111 {
2112         int err = 0;
2113         int size = 0;
2114         struct sockaddr_in *laddr = (struct sockaddr_in *)
2115                                     &ep->com.cm_id->m_local_addr;
2116         struct sockaddr_in *raddr = (struct sockaddr_in *)
2117                                     &ep->com.cm_id->m_remote_addr;
2118         struct sockaddr_in6 *laddr6 = (struct sockaddr_in6 *)
2119                                       &ep->com.cm_id->m_local_addr;
2120         struct sockaddr_in6 *raddr6 = (struct sockaddr_in6 *)
2121                                       &ep->com.cm_id->m_remote_addr;
2122         int iptype;
2123         __u8 *ra;
2124
2125         pr_debug("qp %p cm_id %p\n", ep->com.qp, ep->com.cm_id);
2126         c4iw_init_wr_wait(ep->com.wr_waitp);
2127
2128         /* When MPA revision is different on nodes, the node with MPA_rev=2
2129          * tries to reconnect with MPA_rev 1 for the same EP through
2130          * c4iw_reconnect(), where the same EP is assigned with new tid for
2131          * further connection establishment. As we are using the same EP pointer
2132          * for reconnect, few skbs are used during the previous c4iw_connect(),
2133          * which leaves the EP with inadequate skbs for further
2134          * c4iw_reconnect(), Further causing a crash due to an empty
2135          * skb_list() during peer_abort(). Allocate skbs which is already used.
2136          */
2137         size = (CN_MAX_CON_BUF - skb_queue_len(&ep->com.ep_skb_list));
2138         if (alloc_ep_skb_list(&ep->com.ep_skb_list, size)) {
2139                 err = -ENOMEM;
2140                 goto fail1;
2141         }
2142
2143         /*
2144          * Allocate an active TID to initiate a TCP connection.
2145          */
2146         ep->atid = cxgb4_alloc_atid(ep->com.dev->rdev.lldi.tids, ep);
2147         if (ep->atid == -1) {
2148                 pr_err("%s - cannot alloc atid\n", __func__);
2149                 err = -ENOMEM;
2150                 goto fail2;
2151         }
2152         insert_handle(ep->com.dev, &ep->com.dev->atid_idr, ep, ep->atid);
2153
2154         /* find a route */
2155         if (ep->com.cm_id->m_local_addr.ss_family == AF_INET) {
2156                 ep->dst = cxgb_find_route(&ep->com.dev->rdev.lldi, get_real_dev,
2157                                           laddr->sin_addr.s_addr,
2158                                           raddr->sin_addr.s_addr,
2159                                           laddr->sin_port,
2160                                           raddr->sin_port, ep->com.cm_id->tos);
2161                 iptype = 4;
2162                 ra = (__u8 *)&raddr->sin_addr;
2163         } else {
2164                 ep->dst = cxgb_find_route6(&ep->com.dev->rdev.lldi,
2165                                            get_real_dev,
2166                                            laddr6->sin6_addr.s6_addr,
2167                                            raddr6->sin6_addr.s6_addr,
2168                                            laddr6->sin6_port,
2169                                            raddr6->sin6_port,
2170                                            ep->com.cm_id->tos,
2171                                            raddr6->sin6_scope_id);
2172                 iptype = 6;
2173                 ra = (__u8 *)&raddr6->sin6_addr;
2174         }
2175         if (!ep->dst) {
2176                 pr_err("%s - cannot find route\n", __func__);
2177                 err = -EHOSTUNREACH;
2178                 goto fail3;
2179         }
2180         err = import_ep(ep, iptype, ra, ep->dst, ep->com.dev, false,
2181                         ep->com.dev->rdev.lldi.adapter_type,
2182                         ep->com.cm_id->tos);
2183         if (err) {
2184                 pr_err("%s - cannot alloc l2e\n", __func__);
2185                 goto fail4;
2186         }
2187
2188         pr_debug("txq_idx %u tx_chan %u smac_idx %u rss_qid %u l2t_idx %u\n",
2189                  ep->txq_idx, ep->tx_chan, ep->smac_idx, ep->rss_qid,
2190                  ep->l2t->idx);
2191
2192         state_set(&ep->com, CONNECTING);
2193         ep->tos = ep->com.cm_id->tos;
2194
2195         /* send connect request to rnic */
2196         err = send_connect(ep);
2197         if (!err)
2198                 goto out;
2199
2200         cxgb4_l2t_release(ep->l2t);
2201 fail4:
2202         dst_release(ep->dst);
2203 fail3:
2204         remove_handle(ep->com.dev, &ep->com.dev->atid_idr, ep->atid);
2205         cxgb4_free_atid(ep->com.dev->rdev.lldi.tids, ep->atid);
2206 fail2:
2207         /*
2208          * remember to send notification to upper layer.
2209          * We are in here so the upper layer is not aware that this is
2210          * re-connect attempt and so, upper layer is still waiting for
2211          * response of 1st connect request.
2212          */
2213         connect_reply_upcall(ep, -ECONNRESET);
2214 fail1:
2215         c4iw_put_ep(&ep->com);
2216 out:
2217         return err;
2218 }
2219
2220 static int act_open_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
2221 {
2222         struct c4iw_ep *ep;
2223         struct cpl_act_open_rpl *rpl = cplhdr(skb);
2224         unsigned int atid = TID_TID_G(AOPEN_ATID_G(
2225                                       ntohl(rpl->atid_status)));
2226         struct tid_info *t = dev->rdev.lldi.tids;
2227         int status = AOPEN_STATUS_G(ntohl(rpl->atid_status));
2228         struct sockaddr_in *la;
2229         struct sockaddr_in *ra;
2230         struct sockaddr_in6 *la6;
2231         struct sockaddr_in6 *ra6;
2232         int ret = 0;
2233
2234         ep = lookup_atid(t, atid);
2235         la = (struct sockaddr_in *)&ep->com.local_addr;
2236         ra = (struct sockaddr_in *)&ep->com.remote_addr;
2237         la6 = (struct sockaddr_in6 *)&ep->com.local_addr;
2238         ra6 = (struct sockaddr_in6 *)&ep->com.remote_addr;
2239
2240         pr_debug("ep %p atid %u status %u errno %d\n", ep, atid,
2241                  status, status2errno(status));
2242
2243         if (cxgb_is_neg_adv(status)) {
2244                 pr_debug("Connection problems for atid %u status %u (%s)\n",
2245                          atid, status, neg_adv_str(status));
2246                 ep->stats.connect_neg_adv++;
2247                 mutex_lock(&dev->rdev.stats.lock);
2248                 dev->rdev.stats.neg_adv++;
2249                 mutex_unlock(&dev->rdev.stats.lock);
2250                 return 0;
2251         }
2252
2253         set_bit(ACT_OPEN_RPL, &ep->com.history);
2254
2255         /*
2256          * Log interesting failures.
2257          */
2258         switch (status) {
2259         case CPL_ERR_CONN_RESET:
2260         case CPL_ERR_CONN_TIMEDOUT:
2261                 break;
2262         case CPL_ERR_TCAM_FULL:
2263                 mutex_lock(&dev->rdev.stats.lock);
2264                 dev->rdev.stats.tcam_full++;
2265                 mutex_unlock(&dev->rdev.stats.lock);
2266                 if (ep->com.local_addr.ss_family == AF_INET &&
2267                     dev->rdev.lldi.enable_fw_ofld_conn) {
2268                         ret = send_fw_act_open_req(ep, TID_TID_G(AOPEN_ATID_G(
2269                                                    ntohl(rpl->atid_status))));
2270                         if (ret)
2271                                 goto fail;
2272                         return 0;
2273                 }
2274                 break;
2275         case CPL_ERR_CONN_EXIST:
2276                 if (ep->retry_count++ < ACT_OPEN_RETRY_COUNT) {
2277                         set_bit(ACT_RETRY_INUSE, &ep->com.history);
2278                         if (ep->com.remote_addr.ss_family == AF_INET6) {
2279                                 struct sockaddr_in6 *sin6 =
2280                                                 (struct sockaddr_in6 *)
2281                                                 &ep->com.local_addr;
2282                                 cxgb4_clip_release(
2283                                                 ep->com.dev->rdev.lldi.ports[0],
2284                                                 (const u32 *)
2285                                                 &sin6->sin6_addr.s6_addr, 1);
2286                         }
2287                         remove_handle(ep->com.dev, &ep->com.dev->atid_idr,
2288                                         atid);
2289                         cxgb4_free_atid(t, atid);
2290                         dst_release(ep->dst);
2291                         cxgb4_l2t_release(ep->l2t);
2292                         c4iw_reconnect(ep);
2293                         return 0;
2294                 }
2295                 break;
2296         default:
2297                 if (ep->com.local_addr.ss_family == AF_INET) {
2298                         pr_info("Active open failure - atid %u status %u errno %d %pI4:%u->%pI4:%u\n",
2299                                 atid, status, status2errno(status),
2300                                 &la->sin_addr.s_addr, ntohs(la->sin_port),
2301                                 &ra->sin_addr.s_addr, ntohs(ra->sin_port));
2302                 } else {
2303                         pr_info("Active open failure - atid %u status %u errno %d %pI6:%u->%pI6:%u\n",
2304                                 atid, status, status2errno(status),
2305                                 la6->sin6_addr.s6_addr, ntohs(la6->sin6_port),
2306                                 ra6->sin6_addr.s6_addr, ntohs(ra6->sin6_port));
2307                 }
2308                 break;
2309         }
2310
2311 fail:
2312         connect_reply_upcall(ep, status2errno(status));
2313         state_set(&ep->com, DEAD);
2314
2315         if (ep->com.remote_addr.ss_family == AF_INET6) {
2316                 struct sockaddr_in6 *sin6 =
2317                         (struct sockaddr_in6 *)&ep->com.local_addr;
2318                 cxgb4_clip_release(ep->com.dev->rdev.lldi.ports[0],
2319                                    (const u32 *)&sin6->sin6_addr.s6_addr, 1);
2320         }
2321         if (status && act_open_has_tid(status))
2322                 cxgb4_remove_tid(ep->com.dev->rdev.lldi.tids, 0, GET_TID(rpl),
2323                                  ep->com.local_addr.ss_family);
2324
2325         remove_handle(ep->com.dev, &ep->com.dev->atid_idr, atid);
2326         cxgb4_free_atid(t, atid);
2327         dst_release(ep->dst);
2328         cxgb4_l2t_release(ep->l2t);
2329         c4iw_put_ep(&ep->com);
2330
2331         return 0;
2332 }
2333
2334 static int pass_open_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
2335 {
2336         struct cpl_pass_open_rpl *rpl = cplhdr(skb);
2337         unsigned int stid = GET_TID(rpl);
2338         struct c4iw_listen_ep *ep = get_ep_from_stid(dev, stid);
2339
2340         if (!ep) {
2341                 pr_warn("%s stid %d lookup failure!\n", __func__, stid);
2342                 goto out;
2343         }
2344         pr_debug("ep %p status %d error %d\n", ep,
2345                  rpl->status, status2errno(rpl->status));
2346         c4iw_wake_up_noref(ep->com.wr_waitp, status2errno(rpl->status));
2347         c4iw_put_ep(&ep->com);
2348 out:
2349         return 0;
2350 }
2351
2352 static int close_listsrv_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
2353 {
2354         struct cpl_close_listsvr_rpl *rpl = cplhdr(skb);
2355         unsigned int stid = GET_TID(rpl);
2356         struct c4iw_listen_ep *ep = get_ep_from_stid(dev, stid);
2357
2358         if (!ep) {
2359                 pr_warn("%s stid %d lookup failure!\n", __func__, stid);
2360                 goto out;
2361         }
2362         pr_debug("ep %p\n", ep);
2363         c4iw_wake_up_noref(ep->com.wr_waitp, status2errno(rpl->status));
2364         c4iw_put_ep(&ep->com);
2365 out:
2366         return 0;
2367 }
2368
2369 static int accept_cr(struct c4iw_ep *ep, struct sk_buff *skb,
2370                      struct cpl_pass_accept_req *req)
2371 {
2372         struct cpl_pass_accept_rpl *rpl;
2373         unsigned int mtu_idx;
2374         u64 opt0;
2375         u32 opt2;
2376         u32 wscale;
2377         struct cpl_t5_pass_accept_rpl *rpl5 = NULL;
2378         int win;
2379         enum chip_type adapter_type = ep->com.dev->rdev.lldi.adapter_type;
2380
2381         pr_debug("ep %p tid %u\n", ep, ep->hwtid);
2382         cxgb_best_mtu(ep->com.dev->rdev.lldi.mtus, ep->mtu, &mtu_idx,
2383                       enable_tcp_timestamps && req->tcpopt.tstamp,
2384                       (ep->com.remote_addr.ss_family == AF_INET) ? 0 : 1);
2385         wscale = cxgb_compute_wscale(rcv_win);
2386
2387         /*
2388          * Specify the largest window that will fit in opt0. The
2389          * remainder will be specified in the rx_data_ack.
2390          */
2391         win = ep->rcv_win >> 10;
2392         if (win > RCV_BUFSIZ_M)
2393                 win = RCV_BUFSIZ_M;
2394         opt0 = (nocong ? NO_CONG_F : 0) |
2395                KEEP_ALIVE_F |
2396                DELACK_F |
2397                WND_SCALE_V(wscale) |
2398                MSS_IDX_V(mtu_idx) |
2399                L2T_IDX_V(ep->l2t->idx) |
2400                TX_CHAN_V(ep->tx_chan) |
2401                SMAC_SEL_V(ep->smac_idx) |
2402                DSCP_V(ep->tos >> 2) |
2403                ULP_MODE_V(ULP_MODE_TCPDDP) |
2404                RCV_BUFSIZ_V(win);
2405         opt2 = RX_CHANNEL_V(0) |
2406                RSS_QUEUE_VALID_F | RSS_QUEUE_V(ep->rss_qid);
2407
2408         if (enable_tcp_timestamps && req->tcpopt.tstamp)
2409                 opt2 |= TSTAMPS_EN_F;
2410         if (enable_tcp_sack && req->tcpopt.sack)
2411                 opt2 |= SACK_EN_F;
2412         if (wscale && enable_tcp_window_scaling)
2413                 opt2 |= WND_SCALE_EN_F;
2414         if (enable_ecn) {
2415                 const struct tcphdr *tcph;
2416                 u32 hlen = ntohl(req->hdr_len);
2417
2418                 if (CHELSIO_CHIP_VERSION(adapter_type) <= CHELSIO_T5)
2419                         tcph = (const void *)(req + 1) + ETH_HDR_LEN_G(hlen) +
2420                                 IP_HDR_LEN_G(hlen);
2421                 else
2422                         tcph = (const void *)(req + 1) +
2423                                 T6_ETH_HDR_LEN_G(hlen) + T6_IP_HDR_LEN_G(hlen);
2424                 if (tcph->ece && tcph->cwr)
2425                         opt2 |= CCTRL_ECN_V(1);
2426         }
2427
2428         skb_get(skb);
2429         rpl = cplhdr(skb);
2430         if (!is_t4(adapter_type)) {
2431                 skb_trim(skb, roundup(sizeof(*rpl5), 16));
2432                 rpl5 = (void *)rpl;
2433                 INIT_TP_WR(rpl5, ep->hwtid);
2434         } else {
2435                 skb_trim(skb, sizeof(*rpl));
2436                 INIT_TP_WR(rpl, ep->hwtid);
2437         }
2438         OPCODE_TID(rpl) = cpu_to_be32(MK_OPCODE_TID(CPL_PASS_ACCEPT_RPL,
2439                                                     ep->hwtid));
2440
2441         if (CHELSIO_CHIP_VERSION(adapter_type) > CHELSIO_T4) {
2442                 u32 isn = (prandom_u32() & ~7UL) - 1;
2443                 opt2 |= T5_OPT_2_VALID_F;
2444                 opt2 |= CONG_CNTRL_V(CONG_ALG_TAHOE);
2445                 opt2 |= T5_ISS_F;
2446                 rpl5 = (void *)rpl;
2447                 memset(&rpl5->iss, 0, roundup(sizeof(*rpl5)-sizeof(*rpl), 16));
2448                 if (peer2peer)
2449                         isn += 4;
2450                 rpl5->iss = cpu_to_be32(isn);
2451                 pr_debug("iss %u\n", be32_to_cpu(rpl5->iss));
2452         }
2453
2454         rpl->opt0 = cpu_to_be64(opt0);
2455         rpl->opt2 = cpu_to_be32(opt2);
2456         set_wr_txq(skb, CPL_PRIORITY_SETUP, ep->ctrlq_idx);
2457         t4_set_arp_err_handler(skb, ep, pass_accept_rpl_arp_failure);
2458
2459         return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
2460 }
2461
2462 static void reject_cr(struct c4iw_dev *dev, u32 hwtid, struct sk_buff *skb)
2463 {
2464         pr_debug("c4iw_dev %p tid %u\n", dev, hwtid);
2465         skb_trim(skb, sizeof(struct cpl_tid_release));
2466         release_tid(&dev->rdev, hwtid, skb);
2467         return;
2468 }
2469
2470 static int pass_accept_req(struct c4iw_dev *dev, struct sk_buff *skb)
2471 {
2472         struct c4iw_ep *child_ep = NULL, *parent_ep;
2473         struct cpl_pass_accept_req *req = cplhdr(skb);
2474         unsigned int stid = PASS_OPEN_TID_G(ntohl(req->tos_stid));
2475         struct tid_info *t = dev->rdev.lldi.tids;
2476         unsigned int hwtid = GET_TID(req);
2477         struct dst_entry *dst;
2478         __u8 local_ip[16], peer_ip[16];
2479         __be16 local_port, peer_port;
2480         struct sockaddr_in6 *sin6;
2481         int err;
2482         u16 peer_mss = ntohs(req->tcpopt.mss);
2483         int iptype;
2484         unsigned short hdrs;
2485         u8 tos = PASS_OPEN_TOS_G(ntohl(req->tos_stid));
2486
2487         parent_ep = (struct c4iw_ep *)get_ep_from_stid(dev, stid);
2488         if (!parent_ep) {
2489                 pr_err("%s connect request on invalid stid %d\n",
2490                        __func__, stid);
2491                 goto reject;
2492         }
2493
2494         if (state_read(&parent_ep->com) != LISTEN) {
2495                 pr_err("%s - listening ep not in LISTEN\n", __func__);
2496                 goto reject;
2497         }
2498
2499         cxgb_get_4tuple(req, parent_ep->com.dev->rdev.lldi.adapter_type,
2500                         &iptype, local_ip, peer_ip, &local_port, &peer_port);
2501
2502         /* Find output route */
2503         if (iptype == 4)  {
2504                 pr_debug("parent ep %p hwtid %u laddr %pI4 raddr %pI4 lport %d rport %d peer_mss %d\n"
2505                          , parent_ep, hwtid,
2506                          local_ip, peer_ip, ntohs(local_port),
2507                          ntohs(peer_port), peer_mss);
2508                 dst = cxgb_find_route(&dev->rdev.lldi, get_real_dev,
2509                                       *(__be32 *)local_ip, *(__be32 *)peer_ip,
2510                                       local_port, peer_port, tos);
2511         } else {
2512                 pr_debug("parent ep %p hwtid %u laddr %pI6 raddr %pI6 lport %d rport %d peer_mss %d\n"
2513                          , parent_ep, hwtid,
2514                          local_ip, peer_ip, ntohs(local_port),
2515                          ntohs(peer_port), peer_mss);
2516                 dst = cxgb_find_route6(&dev->rdev.lldi, get_real_dev,
2517                                 local_ip, peer_ip, local_port, peer_port,
2518                                 PASS_OPEN_TOS_G(ntohl(req->tos_stid)),
2519                                 ((struct sockaddr_in6 *)
2520                                  &parent_ep->com.local_addr)->sin6_scope_id);
2521         }
2522         if (!dst) {
2523                 pr_err("%s - failed to find dst entry!\n", __func__);
2524                 goto reject;
2525         }
2526
2527         child_ep = alloc_ep(sizeof(*child_ep), GFP_KERNEL);
2528         if (!child_ep) {
2529                 pr_err("%s - failed to allocate ep entry!\n", __func__);
2530                 dst_release(dst);
2531                 goto reject;
2532         }
2533
2534         err = import_ep(child_ep, iptype, peer_ip, dst, dev, false,
2535                         parent_ep->com.dev->rdev.lldi.adapter_type, tos);
2536         if (err) {
2537                 pr_err("%s - failed to allocate l2t entry!\n", __func__);
2538                 dst_release(dst);
2539                 kfree(child_ep);
2540                 goto reject;
2541         }
2542
2543         hdrs = ((iptype == 4) ? sizeof(struct iphdr) : sizeof(struct ipv6hdr)) +
2544                sizeof(struct tcphdr) +
2545                ((enable_tcp_timestamps && req->tcpopt.tstamp) ? 12 : 0);
2546         if (peer_mss && child_ep->mtu > (peer_mss + hdrs))
2547                 child_ep->mtu = peer_mss + hdrs;
2548
2549         skb_queue_head_init(&child_ep->com.ep_skb_list);
2550         if (alloc_ep_skb_list(&child_ep->com.ep_skb_list, CN_MAX_CON_BUF))
2551                 goto fail;
2552
2553         state_set(&child_ep->com, CONNECTING);
2554         child_ep->com.dev = dev;
2555         child_ep->com.cm_id = NULL;
2556
2557         if (iptype == 4) {
2558                 struct sockaddr_in *sin = (struct sockaddr_in *)
2559                         &child_ep->com.local_addr;
2560
2561                 sin->sin_family = AF_INET;
2562                 sin->sin_port = local_port;
2563                 sin->sin_addr.s_addr = *(__be32 *)local_ip;
2564
2565                 sin = (struct sockaddr_in *)&child_ep->com.local_addr;
2566                 sin->sin_family = AF_INET;
2567                 sin->sin_port = ((struct sockaddr_in *)
2568                                  &parent_ep->com.local_addr)->sin_port;
2569                 sin->sin_addr.s_addr = *(__be32 *)local_ip;
2570
2571                 sin = (struct sockaddr_in *)&child_ep->com.remote_addr;
2572                 sin->sin_family = AF_INET;
2573                 sin->sin_port = peer_port;
2574                 sin->sin_addr.s_addr = *(__be32 *)peer_ip;
2575         } else {
2576                 sin6 = (struct sockaddr_in6 *)&child_ep->com.local_addr;
2577                 sin6->sin6_family = PF_INET6;
2578                 sin6->sin6_port = local_port;
2579                 memcpy(sin6->sin6_addr.s6_addr, local_ip, 16);
2580
2581                 sin6 = (struct sockaddr_in6 *)&child_ep->com.local_addr;
2582                 sin6->sin6_family = PF_INET6;
2583                 sin6->sin6_port = ((struct sockaddr_in6 *)
2584                                    &parent_ep->com.local_addr)->sin6_port;
2585                 memcpy(sin6->sin6_addr.s6_addr, local_ip, 16);
2586
2587                 sin6 = (struct sockaddr_in6 *)&child_ep->com.remote_addr;
2588                 sin6->sin6_family = PF_INET6;
2589                 sin6->sin6_port = peer_port;
2590                 memcpy(sin6->sin6_addr.s6_addr, peer_ip, 16);
2591         }
2592
2593         c4iw_get_ep(&parent_ep->com);
2594         child_ep->parent_ep = parent_ep;
2595         child_ep->tos = tos;
2596         child_ep->dst = dst;
2597         child_ep->hwtid = hwtid;
2598
2599         pr_debug("tx_chan %u smac_idx %u rss_qid %u\n",
2600                  child_ep->tx_chan, child_ep->smac_idx, child_ep->rss_qid);
2601
2602         timer_setup(&child_ep->timer, ep_timeout, 0);
2603         cxgb4_insert_tid(t, child_ep, hwtid,
2604                          child_ep->com.local_addr.ss_family);
2605         insert_ep_tid(child_ep);
2606         if (accept_cr(child_ep, skb, req)) {
2607                 c4iw_put_ep(&parent_ep->com);
2608                 release_ep_resources(child_ep);
2609         } else {
2610                 set_bit(PASS_ACCEPT_REQ, &child_ep->com.history);
2611         }
2612         if (iptype == 6) {
2613                 sin6 = (struct sockaddr_in6 *)&child_ep->com.local_addr;
2614                 cxgb4_clip_get(child_ep->com.dev->rdev.lldi.ports[0],
2615                                (const u32 *)&sin6->sin6_addr.s6_addr, 1);
2616         }
2617         goto out;
2618 fail:
2619         c4iw_put_ep(&child_ep->com);
2620 reject:
2621         reject_cr(dev, hwtid, skb);
2622 out:
2623         if (parent_ep)
2624                 c4iw_put_ep(&parent_ep->com);
2625         return 0;
2626 }
2627
2628 static int pass_establish(struct c4iw_dev *dev, struct sk_buff *skb)
2629 {
2630         struct c4iw_ep *ep;
2631         struct cpl_pass_establish *req = cplhdr(skb);
2632         unsigned int tid = GET_TID(req);
2633         int ret;
2634         u16 tcp_opt = ntohs(req->tcp_opt);
2635
2636         ep = get_ep_from_tid(dev, tid);
2637         pr_debug("ep %p tid %u\n", ep, ep->hwtid);
2638         ep->snd_seq = be32_to_cpu(req->snd_isn);
2639         ep->rcv_seq = be32_to_cpu(req->rcv_isn);
2640         ep->snd_wscale = TCPOPT_SND_WSCALE_G(tcp_opt);
2641
2642         pr_debug("ep %p hwtid %u tcp_opt 0x%02x\n", ep, tid, tcp_opt);
2643
2644         set_emss(ep, tcp_opt);
2645
2646         dst_confirm(ep->dst);
2647         mutex_lock(&ep->com.mutex);
2648         ep->com.state = MPA_REQ_WAIT;
2649         start_ep_timer(ep);
2650         set_bit(PASS_ESTAB, &ep->com.history);
2651         ret = send_flowc(ep);
2652         mutex_unlock(&ep->com.mutex);
2653         if (ret)
2654                 c4iw_ep_disconnect(ep, 1, GFP_KERNEL);
2655         c4iw_put_ep(&ep->com);
2656
2657         return 0;
2658 }
2659
2660 static int peer_close(struct c4iw_dev *dev, struct sk_buff *skb)
2661 {
2662         struct cpl_peer_close *hdr = cplhdr(skb);
2663         struct c4iw_ep *ep;
2664         struct c4iw_qp_attributes attrs;
2665         int disconnect = 1;
2666         int release = 0;
2667         unsigned int tid = GET_TID(hdr);
2668         int ret;
2669
2670         ep = get_ep_from_tid(dev, tid);
2671         if (!ep)
2672                 return 0;
2673
2674         pr_debug("ep %p tid %u\n", ep, ep->hwtid);
2675         dst_confirm(ep->dst);
2676
2677         set_bit(PEER_CLOSE, &ep->com.history);
2678         mutex_lock(&ep->com.mutex);
2679         switch (ep->com.state) {
2680         case MPA_REQ_WAIT:
2681                 __state_set(&ep->com, CLOSING);
2682                 break;
2683         case MPA_REQ_SENT:
2684                 __state_set(&ep->com, CLOSING);
2685                 connect_reply_upcall(ep, -ECONNRESET);
2686                 break;
2687         case MPA_REQ_RCVD:
2688
2689                 /*
2690                  * We're gonna mark this puppy DEAD, but keep
2691                  * the reference on it until the ULP accepts or
2692                  * rejects the CR. Also wake up anyone waiting
2693                  * in rdma connection migration (see c4iw_accept_cr()).
2694                  */
2695                 __state_set(&ep->com, CLOSING);
2696                 pr_debug("waking up ep %p tid %u\n", ep, ep->hwtid);
2697                 c4iw_wake_up_noref(ep->com.wr_waitp, -ECONNRESET);
2698                 break;
2699         case MPA_REP_SENT:
2700                 __state_set(&ep->com, CLOSING);
2701                 pr_debug("waking up ep %p tid %u\n", ep, ep->hwtid);
2702                 c4iw_wake_up_noref(ep->com.wr_waitp, -ECONNRESET);
2703                 break;
2704         case FPDU_MODE:
2705                 start_ep_timer(ep);
2706                 __state_set(&ep->com, CLOSING);
2707                 attrs.next_state = C4IW_QP_STATE_CLOSING;
2708                 ret = c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
2709                                        C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
2710                 if (ret != -ECONNRESET) {
2711                         peer_close_upcall(ep);
2712                         disconnect = 1;
2713                 }
2714                 break;
2715         case ABORTING:
2716                 disconnect = 0;
2717                 break;
2718         case CLOSING:
2719                 __state_set(&ep->com, MORIBUND);
2720                 disconnect = 0;
2721                 break;
2722         case MORIBUND:
2723                 (void)stop_ep_timer(ep);
2724                 if (ep->com.cm_id && ep->com.qp) {
2725                         attrs.next_state = C4IW_QP_STATE_IDLE;
2726                         c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
2727                                        C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
2728                 }
2729                 close_complete_upcall(ep, 0);
2730                 __state_set(&ep->com, DEAD);
2731                 release = 1;
2732                 disconnect = 0;
2733                 break;
2734         case DEAD:
2735                 disconnect = 0;
2736                 break;
2737         default:
2738                 WARN_ONCE(1, "Bad endpoint state %u\n", ep->com.state);
2739         }
2740         mutex_unlock(&ep->com.mutex);
2741         if (disconnect)
2742                 c4iw_ep_disconnect(ep, 0, GFP_KERNEL);
2743         if (release)
2744                 release_ep_resources(ep);
2745         c4iw_put_ep(&ep->com);
2746         return 0;
2747 }
2748
2749 static int peer_abort(struct c4iw_dev *dev, struct sk_buff *skb)
2750 {
2751         struct cpl_abort_req_rss6 *req = cplhdr(skb);
2752         struct c4iw_ep *ep;
2753         struct sk_buff *rpl_skb;
2754         struct c4iw_qp_attributes attrs;
2755         int ret;
2756         int release = 0;
2757         unsigned int tid = GET_TID(req);
2758         u8 status;
2759
2760         u32 len = roundup(sizeof(struct cpl_abort_rpl), 16);
2761
2762         ep = get_ep_from_tid(dev, tid);
2763         if (!ep)
2764                 return 0;
2765
2766         status = ABORT_RSS_STATUS_G(be32_to_cpu(req->srqidx_status));
2767
2768         if (cxgb_is_neg_adv(status)) {
2769                 pr_debug("Negative advice on abort- tid %u status %d (%s)\n",
2770                          ep->hwtid, status, neg_adv_str(status));
2771                 ep->stats.abort_neg_adv++;
2772                 mutex_lock(&dev->rdev.stats.lock);
2773                 dev->rdev.stats.neg_adv++;
2774                 mutex_unlock(&dev->rdev.stats.lock);
2775                 goto deref_ep;
2776         }
2777
2778         complete_cached_srq_buffers(ep, req->srqidx_status);
2779
2780         pr_debug("ep %p tid %u state %u\n", ep, ep->hwtid,
2781                  ep->com.state);
2782         set_bit(PEER_ABORT, &ep->com.history);
2783
2784         /*
2785          * Wake up any threads in rdma_init() or rdma_fini().
2786          * However, this is not needed if com state is just
2787          * MPA_REQ_SENT
2788          */
2789         if (ep->com.state != MPA_REQ_SENT)
2790                 c4iw_wake_up_noref(ep->com.wr_waitp, -ECONNRESET);
2791
2792         mutex_lock(&ep->com.mutex);
2793         switch (ep->com.state) {
2794         case CONNECTING:
2795                 c4iw_put_ep(&ep->parent_ep->com);
2796                 break;
2797         case MPA_REQ_WAIT:
2798                 (void)stop_ep_timer(ep);
2799                 break;
2800         case MPA_REQ_SENT:
2801                 (void)stop_ep_timer(ep);
2802                 if (status != CPL_ERR_CONN_RESET || mpa_rev == 1 ||
2803                     (mpa_rev == 2 && ep->tried_with_mpa_v1))
2804                         connect_reply_upcall(ep, -ECONNRESET);
2805                 else {
2806                         /*
2807                          * we just don't send notification upwards because we
2808                          * want to retry with mpa_v1 without upper layers even
2809                          * knowing it.
2810                          *
2811                          * do some housekeeping so as to re-initiate the
2812                          * connection
2813                          */
2814                         pr_info("%s: mpa_rev=%d. Retrying with mpav1\n",
2815                                 __func__, mpa_rev);
2816                         ep->retry_with_mpa_v1 = 1;
2817                 }
2818                 break;
2819         case MPA_REP_SENT:
2820                 break;
2821         case MPA_REQ_RCVD:
2822                 break;
2823         case MORIBUND:
2824         case CLOSING:
2825                 stop_ep_timer(ep);
2826                 /*FALLTHROUGH*/
2827         case FPDU_MODE:
2828                 if (ep->com.cm_id && ep->com.qp) {
2829                         attrs.next_state = C4IW_QP_STATE_ERROR;
2830                         ret = c4iw_modify_qp(ep->com.qp->rhp,
2831                                      ep->com.qp, C4IW_QP_ATTR_NEXT_STATE,
2832                                      &attrs, 1);
2833                         if (ret)
2834                                 pr_err("%s - qp <- error failed!\n", __func__);
2835                 }
2836                 peer_abort_upcall(ep);
2837                 break;
2838         case ABORTING:
2839                 break;
2840         case DEAD:
2841                 pr_warn("%s PEER_ABORT IN DEAD STATE!!!!\n", __func__);
2842                 mutex_unlock(&ep->com.mutex);
2843                 goto deref_ep;
2844         default:
2845                 WARN_ONCE(1, "Bad endpoint state %u\n", ep->com.state);
2846                 break;
2847         }
2848         dst_confirm(ep->dst);
2849         if (ep->com.state != ABORTING) {
2850                 __state_set(&ep->com, DEAD);
2851                 /* we don't release if we want to retry with mpa_v1 */
2852                 if (!ep->retry_with_mpa_v1)
2853                         release = 1;
2854         }
2855         mutex_unlock(&ep->com.mutex);
2856
2857         rpl_skb = skb_dequeue(&ep->com.ep_skb_list);
2858         if (WARN_ON(!rpl_skb)) {
2859                 release = 1;
2860                 goto out;
2861         }
2862
2863         cxgb_mk_abort_rpl(rpl_skb, len, ep->hwtid, ep->txq_idx);
2864
2865         c4iw_ofld_send(&ep->com.dev->rdev, rpl_skb);
2866 out:
2867         if (release)
2868                 release_ep_resources(ep);
2869         else if (ep->retry_with_mpa_v1) {
2870                 if (ep->com.remote_addr.ss_family == AF_INET6) {
2871                         struct sockaddr_in6 *sin6 =
2872                                         (struct sockaddr_in6 *)
2873                                         &ep->com.local_addr;
2874                         cxgb4_clip_release(
2875                                         ep->com.dev->rdev.lldi.ports[0],
2876                                         (const u32 *)&sin6->sin6_addr.s6_addr,
2877                                         1);
2878                 }
2879                 remove_handle(ep->com.dev, &ep->com.dev->hwtid_idr, ep->hwtid);
2880                 cxgb4_remove_tid(ep->com.dev->rdev.lldi.tids, 0, ep->hwtid,
2881                                  ep->com.local_addr.ss_family);
2882                 dst_release(ep->dst);
2883                 cxgb4_l2t_release(ep->l2t);
2884                 c4iw_reconnect(ep);
2885         }
2886
2887 deref_ep:
2888         c4iw_put_ep(&ep->com);
2889         /* Dereferencing ep, referenced in peer_abort_intr() */
2890         c4iw_put_ep(&ep->com);
2891         return 0;
2892 }
2893
2894 static int close_con_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
2895 {
2896         struct c4iw_ep *ep;
2897         struct c4iw_qp_attributes attrs;
2898         struct cpl_close_con_rpl *rpl = cplhdr(skb);
2899         int release = 0;
2900         unsigned int tid = GET_TID(rpl);
2901
2902         ep = get_ep_from_tid(dev, tid);
2903         if (!ep)
2904                 return 0;
2905
2906         pr_debug("ep %p tid %u\n", ep, ep->hwtid);
2907
2908         /* The cm_id may be null if we failed to connect */
2909         mutex_lock(&ep->com.mutex);
2910         set_bit(CLOSE_CON_RPL, &ep->com.history);
2911         switch (ep->com.state) {
2912         case CLOSING:
2913                 __state_set(&ep->com, MORIBUND);
2914                 break;
2915         case MORIBUND:
2916                 (void)stop_ep_timer(ep);
2917                 if ((ep->com.cm_id) && (ep->com.qp)) {
2918                         attrs.next_state = C4IW_QP_STATE_IDLE;
2919                         c4iw_modify_qp(ep->com.qp->rhp,
2920                                              ep->com.qp,
2921                                              C4IW_QP_ATTR_NEXT_STATE,
2922                                              &attrs, 1);
2923                 }
2924                 close_complete_upcall(ep, 0);
2925                 __state_set(&ep->com, DEAD);
2926                 release = 1;
2927                 break;
2928         case ABORTING:
2929         case DEAD:
2930                 break;
2931         default:
2932                 WARN_ONCE(1, "Bad endpoint state %u\n", ep->com.state);
2933                 break;
2934         }
2935         mutex_unlock(&ep->com.mutex);
2936         if (release)
2937                 release_ep_resources(ep);
2938         c4iw_put_ep(&ep->com);
2939         return 0;
2940 }
2941
2942 static int terminate(struct c4iw_dev *dev, struct sk_buff *skb)
2943 {
2944         struct cpl_rdma_terminate *rpl = cplhdr(skb);
2945         unsigned int tid = GET_TID(rpl);
2946         struct c4iw_ep *ep;
2947         struct c4iw_qp_attributes attrs;
2948
2949         ep = get_ep_from_tid(dev, tid);
2950
2951         if (ep) {
2952                 if (ep->com.qp) {
2953                         pr_warn("TERM received tid %u qpid %u\n", tid,
2954                                 ep->com.qp->wq.sq.qid);
2955                         attrs.next_state = C4IW_QP_STATE_TERMINATE;
2956                         c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
2957                                        C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
2958                 }
2959
2960                 c4iw_put_ep(&ep->com);
2961         } else
2962                 pr_warn("TERM received tid %u no ep/qp\n", tid);
2963
2964         return 0;
2965 }
2966
2967 /*
2968  * Upcall from the adapter indicating data has been transmitted.
2969  * For us its just the single MPA request or reply.  We can now free
2970  * the skb holding the mpa message.
2971  */
2972 static int fw4_ack(struct c4iw_dev *dev, struct sk_buff *skb)
2973 {
2974         struct c4iw_ep *ep;
2975         struct cpl_fw4_ack *hdr = cplhdr(skb);
2976         u8 credits = hdr->credits;
2977         unsigned int tid = GET_TID(hdr);
2978
2979
2980         ep = get_ep_from_tid(dev, tid);
2981         if (!ep)
2982                 return 0;
2983         pr_debug("ep %p tid %u credits %u\n",
2984                  ep, ep->hwtid, credits);
2985         if (credits == 0) {
2986                 pr_debug("0 credit ack ep %p tid %u state %u\n",
2987                          ep, ep->hwtid, state_read(&ep->com));
2988                 goto out;
2989         }
2990
2991         dst_confirm(ep->dst);
2992         if (ep->mpa_skb) {
2993                 pr_debug("last streaming msg ack ep %p tid %u state %u initiator %u freeing skb\n",
2994                          ep, ep->hwtid, state_read(&ep->com),
2995                          ep->mpa_attr.initiator ? 1 : 0);
2996                 mutex_lock(&ep->com.mutex);
2997                 kfree_skb(ep->mpa_skb);
2998                 ep->mpa_skb = NULL;
2999                 if (test_bit(STOP_MPA_TIMER, &ep->com.flags))
3000                         stop_ep_timer(ep);
3001                 mutex_unlock(&ep->com.mutex);
3002         }
3003 out:
3004         c4iw_put_ep(&ep->com);
3005         return 0;
3006 }
3007
3008 int c4iw_reject_cr(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
3009 {
3010         int abort;
3011         struct c4iw_ep *ep = to_ep(cm_id);
3012
3013         pr_debug("ep %p tid %u\n", ep, ep->hwtid);
3014
3015         mutex_lock(&ep->com.mutex);
3016         if (ep->com.state != MPA_REQ_RCVD) {
3017                 mutex_unlock(&ep->com.mutex);
3018                 c4iw_put_ep(&ep->com);
3019                 return -ECONNRESET;
3020         }
3021         set_bit(ULP_REJECT, &ep->com.history);
3022         if (mpa_rev == 0)
3023                 abort = 1;
3024         else
3025                 abort = send_mpa_reject(ep, pdata, pdata_len);
3026         mutex_unlock(&ep->com.mutex);
3027
3028         stop_ep_timer(ep);
3029         c4iw_ep_disconnect(ep, abort != 0, GFP_KERNEL);
3030         c4iw_put_ep(&ep->com);
3031         return 0;
3032 }
3033
3034 int c4iw_accept_cr(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
3035 {
3036         int err;
3037         struct c4iw_qp_attributes attrs;
3038         enum c4iw_qp_attr_mask mask;
3039         struct c4iw_ep *ep = to_ep(cm_id);
3040         struct c4iw_dev *h = to_c4iw_dev(cm_id->device);
3041         struct c4iw_qp *qp = get_qhp(h, conn_param->qpn);
3042         int abort = 0;
3043
3044         pr_debug("ep %p tid %u\n", ep, ep->hwtid);
3045
3046         mutex_lock(&ep->com.mutex);
3047         if (ep->com.state != MPA_REQ_RCVD) {
3048                 err = -ECONNRESET;
3049                 goto err_out;
3050         }
3051
3052         if (!qp) {
3053                 err = -EINVAL;
3054                 goto err_out;
3055         }
3056
3057         set_bit(ULP_ACCEPT, &ep->com.history);
3058         if ((conn_param->ord > cur_max_read_depth(ep->com.dev)) ||
3059             (conn_param->ird > cur_max_read_depth(ep->com.dev))) {
3060                 err = -EINVAL;
3061                 goto err_abort;
3062         }
3063
3064         if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn) {
3065                 if (conn_param->ord > ep->ird) {
3066                         if (RELAXED_IRD_NEGOTIATION) {
3067                                 conn_param->ord = ep->ird;
3068                         } else {
3069                                 ep->ird = conn_param->ird;
3070                                 ep->ord = conn_param->ord;
3071                                 send_mpa_reject(ep, conn_param->private_data,
3072                                                 conn_param->private_data_len);
3073                                 err = -ENOMEM;
3074                                 goto err_abort;
3075                         }
3076                 }
3077                 if (conn_param->ird < ep->ord) {
3078                         if (RELAXED_IRD_NEGOTIATION &&
3079                             ep->ord <= h->rdev.lldi.max_ordird_qp) {
3080                                 conn_param->ird = ep->ord;
3081                         } else {
3082                                 err = -ENOMEM;
3083                                 goto err_abort;
3084                         }
3085                 }
3086         }
3087         ep->ird = conn_param->ird;
3088         ep->ord = conn_param->ord;
3089
3090         if (ep->mpa_attr.version == 1) {
3091                 if (peer2peer && ep->ird == 0)
3092                         ep->ird = 1;
3093         } else {
3094                 if (peer2peer &&
3095                     (ep->mpa_attr.p2p_type != FW_RI_INIT_P2PTYPE_DISABLED) &&
3096                     (p2p_type == FW_RI_INIT_P2PTYPE_READ_REQ) && ep->ird == 0)
3097                         ep->ird = 1;
3098         }
3099
3100         pr_debug("ird %d ord %d\n", ep->ird, ep->ord);
3101
3102         ep->com.cm_id = cm_id;
3103         ref_cm_id(&ep->com);
3104         ep->com.qp = qp;
3105         ref_qp(ep);
3106
3107         /* bind QP to EP and move to RTS */
3108         attrs.mpa_attr = ep->mpa_attr;
3109         attrs.max_ird = ep->ird;
3110         attrs.max_ord = ep->ord;
3111         attrs.llp_stream_handle = ep;
3112         attrs.next_state = C4IW_QP_STATE_RTS;
3113
3114         /* bind QP and TID with INIT_WR */
3115         mask = C4IW_QP_ATTR_NEXT_STATE |
3116                              C4IW_QP_ATTR_LLP_STREAM_HANDLE |
3117                              C4IW_QP_ATTR_MPA_ATTR |
3118                              C4IW_QP_ATTR_MAX_IRD |
3119                              C4IW_QP_ATTR_MAX_ORD;
3120
3121         err = c4iw_modify_qp(ep->com.qp->rhp,
3122                              ep->com.qp, mask, &attrs, 1);
3123         if (err)
3124                 goto err_deref_cm_id;
3125
3126         set_bit(STOP_MPA_TIMER, &ep->com.flags);
3127         err = send_mpa_reply(ep, conn_param->private_data,
3128                              conn_param->private_data_len);
3129         if (err)
3130                 goto err_deref_cm_id;
3131
3132         __state_set(&ep->com, FPDU_MODE);
3133         established_upcall(ep);
3134         mutex_unlock(&ep->com.mutex);
3135         c4iw_put_ep(&ep->com);
3136         return 0;
3137 err_deref_cm_id:
3138         deref_cm_id(&ep->com);
3139 err_abort:
3140         abort = 1;
3141 err_out:
3142         mutex_unlock(&ep->com.mutex);
3143         if (abort)
3144                 c4iw_ep_disconnect(ep, 1, GFP_KERNEL);
3145         c4iw_put_ep(&ep->com);
3146         return err;
3147 }
3148
3149 static int pick_local_ipaddrs(struct c4iw_dev *dev, struct iw_cm_id *cm_id)
3150 {
3151         struct in_device *ind;
3152         int found = 0;
3153         struct sockaddr_in *laddr = (struct sockaddr_in *)&cm_id->m_local_addr;
3154         struct sockaddr_in *raddr = (struct sockaddr_in *)&cm_id->m_remote_addr;
3155
3156         ind = in_dev_get(dev->rdev.lldi.ports[0]);
3157         if (!ind)
3158                 return -EADDRNOTAVAIL;
3159         for_primary_ifa(ind) {
3160                 laddr->sin_addr.s_addr = ifa->ifa_address;
3161                 raddr->sin_addr.s_addr = ifa->ifa_address;
3162                 found = 1;
3163                 break;
3164         }
3165         endfor_ifa(ind);
3166         in_dev_put(ind);
3167         return found ? 0 : -EADDRNOTAVAIL;
3168 }
3169
3170 static int get_lladdr(struct net_device *dev, struct in6_addr *addr,
3171                       unsigned char banned_flags)
3172 {
3173         struct inet6_dev *idev;
3174         int err = -EADDRNOTAVAIL;
3175
3176         rcu_read_lock();
3177         idev = __in6_dev_get(dev);
3178         if (idev != NULL) {
3179                 struct inet6_ifaddr *ifp;
3180
3181                 read_lock_bh(&idev->lock);
3182                 list_for_each_entry(ifp, &idev->addr_list, if_list) {
3183                         if (ifp->scope == IFA_LINK &&
3184                             !(ifp->flags & banned_flags)) {
3185                                 memcpy(addr, &ifp->addr, 16);
3186                                 err = 0;
3187                                 break;
3188                         }
3189                 }
3190                 read_unlock_bh(&idev->lock);
3191         }
3192         rcu_read_unlock();
3193         return err;
3194 }
3195
3196 static int pick_local_ip6addrs(struct c4iw_dev *dev, struct iw_cm_id *cm_id)
3197 {
3198         struct in6_addr uninitialized_var(addr);
3199         struct sockaddr_in6 *la6 = (struct sockaddr_in6 *)&cm_id->m_local_addr;
3200         struct sockaddr_in6 *ra6 = (struct sockaddr_in6 *)&cm_id->m_remote_addr;
3201
3202         if (!get_lladdr(dev->rdev.lldi.ports[0], &addr, IFA_F_TENTATIVE)) {
3203                 memcpy(la6->sin6_addr.s6_addr, &addr, 16);
3204                 memcpy(ra6->sin6_addr.s6_addr, &addr, 16);
3205                 return 0;
3206         }
3207         return -EADDRNOTAVAIL;
3208 }
3209
3210 int c4iw_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
3211 {
3212         struct c4iw_dev *dev = to_c4iw_dev(cm_id->device);
3213         struct c4iw_ep *ep;
3214         int err = 0;
3215         struct sockaddr_in *laddr;
3216         struct sockaddr_in *raddr;
3217         struct sockaddr_in6 *laddr6;
3218         struct sockaddr_in6 *raddr6;
3219         __u8 *ra;
3220         int iptype;
3221
3222         if ((conn_param->ord > cur_max_read_depth(dev)) ||
3223             (conn_param->ird > cur_max_read_depth(dev))) {
3224                 err = -EINVAL;
3225                 goto out;
3226         }
3227         ep = alloc_ep(sizeof(*ep), GFP_KERNEL);
3228         if (!ep) {
3229                 pr_err("%s - cannot alloc ep\n", __func__);
3230                 err = -ENOMEM;
3231                 goto out;
3232         }
3233
3234         skb_queue_head_init(&ep->com.ep_skb_list);
3235         if (alloc_ep_skb_list(&ep->com.ep_skb_list, CN_MAX_CON_BUF)) {
3236                 err = -ENOMEM;
3237                 goto fail1;
3238         }
3239
3240         timer_setup(&ep->timer, ep_timeout, 0);
3241         ep->plen = conn_param->private_data_len;
3242         if (ep->plen)
3243                 memcpy(ep->mpa_pkt + sizeof(struct mpa_message),
3244                        conn_param->private_data, ep->plen);
3245         ep->ird = conn_param->ird;
3246         ep->ord = conn_param->ord;
3247
3248         if (peer2peer && ep->ord == 0)
3249                 ep->ord = 1;
3250
3251         ep->com.cm_id = cm_id;
3252         ref_cm_id(&ep->com);
3253         cm_id->provider_data = ep;
3254         ep->com.dev = dev;
3255         ep->com.qp = get_qhp(dev, conn_param->qpn);
3256         if (!ep->com.qp) {
3257                 pr_warn("%s qpn 0x%x not found!\n", __func__, conn_param->qpn);
3258                 err = -EINVAL;
3259                 goto fail2;
3260         }
3261         ref_qp(ep);
3262         pr_debug("qpn 0x%x qp %p cm_id %p\n", conn_param->qpn,
3263                  ep->com.qp, cm_id);
3264
3265         /*
3266          * Allocate an active TID to initiate a TCP connection.
3267          */
3268         ep->atid = cxgb4_alloc_atid(dev->rdev.lldi.tids, ep);
3269         if (ep->atid == -1) {
3270                 pr_err("%s - cannot alloc atid\n", __func__);
3271                 err = -ENOMEM;
3272                 goto fail2;
3273         }
3274         insert_handle(dev, &dev->atid_idr, ep, ep->atid);
3275
3276         memcpy(&ep->com.local_addr, &cm_id->m_local_addr,
3277                sizeof(ep->com.local_addr));
3278         memcpy(&ep->com.remote_addr, &cm_id->m_remote_addr,
3279                sizeof(ep->com.remote_addr));
3280
3281         laddr = (struct sockaddr_in *)&ep->com.local_addr;
3282         raddr = (struct sockaddr_in *)&ep->com.remote_addr;
3283         laddr6 = (struct sockaddr_in6 *)&ep->com.local_addr;
3284         raddr6 = (struct sockaddr_in6 *) &ep->com.remote_addr;
3285
3286         if (cm_id->m_remote_addr.ss_family == AF_INET) {
3287                 iptype = 4;
3288                 ra = (__u8 *)&raddr->sin_addr;
3289
3290                 /*
3291                  * Handle loopback requests to INADDR_ANY.
3292                  */
3293                 if (raddr->sin_addr.s_addr == htonl(INADDR_ANY)) {
3294                         err = pick_local_ipaddrs(dev, cm_id);
3295                         if (err)
3296                                 goto fail3;
3297                 }
3298
3299                 /* find a route */
3300                 pr_debug("saddr %pI4 sport 0x%x raddr %pI4 rport 0x%x\n",
3301                          &laddr->sin_addr, ntohs(laddr->sin_port),
3302                          ra, ntohs(raddr->sin_port));
3303                 ep->dst = cxgb_find_route(&dev->rdev.lldi, get_real_dev,
3304                                           laddr->sin_addr.s_addr,
3305                                           raddr->sin_addr.s_addr,
3306                                           laddr->sin_port,
3307                                           raddr->sin_port, cm_id->tos);
3308         } else {
3309                 iptype = 6;
3310                 ra = (__u8 *)&raddr6->sin6_addr;
3311
3312                 /*
3313                  * Handle loopback requests to INADDR_ANY.
3314                  */
3315                 if (ipv6_addr_type(&raddr6->sin6_addr) == IPV6_ADDR_ANY) {
3316                         err = pick_local_ip6addrs(dev, cm_id);
3317                         if (err)
3318                                 goto fail3;
3319                 }
3320
3321                 /* find a route */
3322                 pr_debug("saddr %pI6 sport 0x%x raddr %pI6 rport 0x%x\n",
3323                          laddr6->sin6_addr.s6_addr,
3324                          ntohs(laddr6->sin6_port),
3325                          raddr6->sin6_addr.s6_addr, ntohs(raddr6->sin6_port));
3326                 ep->dst = cxgb_find_route6(&dev->rdev.lldi, get_real_dev,
3327                                            laddr6->sin6_addr.s6_addr,
3328                                            raddr6->sin6_addr.s6_addr,
3329                                            laddr6->sin6_port,
3330                                            raddr6->sin6_port, cm_id->tos,
3331                                            raddr6->sin6_scope_id);
3332         }
3333         if (!ep->dst) {
3334                 pr_err("%s - cannot find route\n", __func__);
3335                 err = -EHOSTUNREACH;
3336                 goto fail3;
3337         }
3338
3339         err = import_ep(ep, iptype, ra, ep->dst, ep->com.dev, true,
3340                         ep->com.dev->rdev.lldi.adapter_type, cm_id->tos);
3341         if (err) {
3342                 pr_err("%s - cannot alloc l2e\n", __func__);
3343                 goto fail4;
3344         }
3345
3346         pr_debug("txq_idx %u tx_chan %u smac_idx %u rss_qid %u l2t_idx %u\n",
3347                  ep->txq_idx, ep->tx_chan, ep->smac_idx, ep->rss_qid,
3348                  ep->l2t->idx);
3349
3350         state_set(&ep->com, CONNECTING);
3351         ep->tos = cm_id->tos;
3352
3353         /* send connect request to rnic */
3354         err = send_connect(ep);
3355         if (!err)
3356                 goto out;
3357
3358         cxgb4_l2t_release(ep->l2t);
3359 fail4:
3360         dst_release(ep->dst);
3361 fail3:
3362         remove_handle(ep->com.dev, &ep->com.dev->atid_idr, ep->atid);
3363         cxgb4_free_atid(ep->com.dev->rdev.lldi.tids, ep->atid);
3364 fail2:
3365         skb_queue_purge(&ep->com.ep_skb_list);
3366         deref_cm_id(&ep->com);
3367 fail1:
3368         c4iw_put_ep(&ep->com);
3369 out:
3370         return err;
3371 }
3372
3373 static int create_server6(struct c4iw_dev *dev, struct c4iw_listen_ep *ep)
3374 {
3375         int err;
3376         struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)
3377                                     &ep->com.local_addr;
3378
3379         if (ipv6_addr_type(&sin6->sin6_addr) != IPV6_ADDR_ANY) {
3380                 err = cxgb4_clip_get(ep->com.dev->rdev.lldi.ports[0],
3381                                      (const u32 *)&sin6->sin6_addr.s6_addr, 1);
3382                 if (err)
3383                         return err;
3384         }
3385         c4iw_init_wr_wait(ep->com.wr_waitp);
3386         err = cxgb4_create_server6(ep->com.dev->rdev.lldi.ports[0],
3387                                    ep->stid, &sin6->sin6_addr,
3388                                    sin6->sin6_port,
3389                                    ep->com.dev->rdev.lldi.rxq_ids[0]);
3390         if (!err)
3391                 err = c4iw_wait_for_reply(&ep->com.dev->rdev,
3392                                           ep->com.wr_waitp,
3393                                           0, 0, __func__);
3394         else if (err > 0)
3395                 err = net_xmit_errno(err);
3396         if (err) {
3397                 cxgb4_clip_release(ep->com.dev->rdev.lldi.ports[0],
3398                                    (const u32 *)&sin6->sin6_addr.s6_addr, 1);
3399                 pr_err("cxgb4_create_server6/filter failed err %d stid %d laddr %pI6 lport %d\n",
3400                        err, ep->stid,
3401                        sin6->sin6_addr.s6_addr, ntohs(sin6->sin6_port));
3402         }
3403         return err;
3404 }
3405
3406 static int create_server4(struct c4iw_dev *dev, struct c4iw_listen_ep *ep)
3407 {
3408         int err;
3409         struct sockaddr_in *sin = (struct sockaddr_in *)
3410                                   &ep->com.local_addr;
3411
3412         if (dev->rdev.lldi.enable_fw_ofld_conn) {
3413                 do {
3414                         err = cxgb4_create_server_filter(
3415                                 ep->com.dev->rdev.lldi.ports[0], ep->stid,
3416                                 sin->sin_addr.s_addr, sin->sin_port, 0,
3417                                 ep->com.dev->rdev.lldi.rxq_ids[0], 0, 0);
3418                         if (err == -EBUSY) {
3419                                 if (c4iw_fatal_error(&ep->com.dev->rdev)) {
3420                                         err = -EIO;
3421                                         break;
3422                                 }
3423                                 set_current_state(TASK_UNINTERRUPTIBLE);
3424                                 schedule_timeout(usecs_to_jiffies(100));
3425                         }
3426                 } while (err == -EBUSY);
3427         } else {
3428                 c4iw_init_wr_wait(ep->com.wr_waitp);
3429                 err = cxgb4_create_server(ep->com.dev->rdev.lldi.ports[0],
3430                                 ep->stid, sin->sin_addr.s_addr, sin->sin_port,
3431                                 0, ep->com.dev->rdev.lldi.rxq_ids[0]);
3432                 if (!err)
3433                         err = c4iw_wait_for_reply(&ep->com.dev->rdev,
3434                                                   ep->com.wr_waitp,
3435                                                   0, 0, __func__);
3436                 else if (err > 0)
3437                         err = net_xmit_errno(err);
3438         }
3439         if (err)
3440                 pr_err("cxgb4_create_server/filter failed err %d stid %d laddr %pI4 lport %d\n"
3441                        , err, ep->stid,
3442                        &sin->sin_addr, ntohs(sin->sin_port));
3443         return err;
3444 }
3445
3446 int c4iw_create_listen(struct iw_cm_id *cm_id, int backlog)
3447 {
3448         int err = 0;
3449         struct c4iw_dev *dev = to_c4iw_dev(cm_id->device);
3450         struct c4iw_listen_ep *ep;
3451
3452         might_sleep();
3453
3454         ep = alloc_ep(sizeof(*ep), GFP_KERNEL);
3455         if (!ep) {
3456                 pr_err("%s - cannot alloc ep\n", __func__);
3457                 err = -ENOMEM;
3458                 goto fail1;
3459         }
3460         skb_queue_head_init(&ep->com.ep_skb_list);
3461         pr_debug("ep %p\n", ep);
3462         ep->com.cm_id = cm_id;
3463         ref_cm_id(&ep->com);
3464         ep->com.dev = dev;
3465         ep->backlog = backlog;
3466         memcpy(&ep->com.local_addr, &cm_id->m_local_addr,
3467                sizeof(ep->com.local_addr));
3468
3469         /*
3470          * Allocate a server TID.
3471          */
3472         if (dev->rdev.lldi.enable_fw_ofld_conn &&
3473             ep->com.local_addr.ss_family == AF_INET)
3474                 ep->stid = cxgb4_alloc_sftid(dev->rdev.lldi.tids,
3475                                              cm_id->m_local_addr.ss_family, ep);
3476         else
3477                 ep->stid = cxgb4_alloc_stid(dev->rdev.lldi.tids,
3478                                             cm_id->m_local_addr.ss_family, ep);
3479
3480         if (ep->stid == -1) {
3481                 pr_err("%s - cannot alloc stid\n", __func__);
3482                 err = -ENOMEM;
3483                 goto fail2;
3484         }
3485         insert_handle(dev, &dev->stid_idr, ep, ep->stid);
3486
3487         state_set(&ep->com, LISTEN);
3488         if (ep->com.local_addr.ss_family == AF_INET)
3489                 err = create_server4(dev, ep);
3490         else
3491                 err = create_server6(dev, ep);
3492         if (!err) {
3493                 cm_id->provider_data = ep;
3494                 goto out;
3495         }
3496         remove_handle(ep->com.dev, &ep->com.dev->stid_idr, ep->stid);
3497         cxgb4_free_stid(ep->com.dev->rdev.lldi.tids, ep->stid,
3498                         ep->com.local_addr.ss_family);
3499 fail2:
3500         deref_cm_id(&ep->com);
3501         c4iw_put_ep(&ep->com);
3502 fail1:
3503 out:
3504         return err;
3505 }
3506
3507 int c4iw_destroy_listen(struct iw_cm_id *cm_id)
3508 {
3509         int err;
3510         struct c4iw_listen_ep *ep = to_listen_ep(cm_id);
3511
3512         pr_debug("ep %p\n", ep);
3513
3514         might_sleep();
3515         state_set(&ep->com, DEAD);
3516         if (ep->com.dev->rdev.lldi.enable_fw_ofld_conn &&
3517             ep->com.local_addr.ss_family == AF_INET) {
3518                 err = cxgb4_remove_server_filter(
3519                         ep->com.dev->rdev.lldi.ports[0], ep->stid,
3520                         ep->com.dev->rdev.lldi.rxq_ids[0], false);
3521         } else {
3522                 struct sockaddr_in6 *sin6;
3523                 c4iw_init_wr_wait(ep->com.wr_waitp);
3524                 err = cxgb4_remove_server(
3525                                 ep->com.dev->rdev.lldi.ports[0], ep->stid,
3526                                 ep->com.dev->rdev.lldi.rxq_ids[0],
3527                                 ep->com.local_addr.ss_family == AF_INET6);
3528                 if (err)
3529                         goto done;
3530                 err = c4iw_wait_for_reply(&ep->com.dev->rdev, ep->com.wr_waitp,
3531                                           0, 0, __func__);
3532                 sin6 = (struct sockaddr_in6 *)&ep->com.local_addr;
3533                 cxgb4_clip_release(ep->com.dev->rdev.lldi.ports[0],
3534                                    (const u32 *)&sin6->sin6_addr.s6_addr, 1);
3535         }
3536         remove_handle(ep->com.dev, &ep->com.dev->stid_idr, ep->stid);
3537         cxgb4_free_stid(ep->com.dev->rdev.lldi.tids, ep->stid,
3538                         ep->com.local_addr.ss_family);
3539 done:
3540         deref_cm_id(&ep->com);
3541         c4iw_put_ep(&ep->com);
3542         return err;
3543 }
3544
3545 int c4iw_ep_disconnect(struct c4iw_ep *ep, int abrupt, gfp_t gfp)
3546 {
3547         int ret = 0;
3548         int close = 0;
3549         int fatal = 0;
3550         struct c4iw_rdev *rdev;
3551
3552         mutex_lock(&ep->com.mutex);
3553
3554         pr_debug("ep %p state %s, abrupt %d\n", ep,
3555                  states[ep->com.state], abrupt);
3556
3557         /*
3558          * Ref the ep here in case we have fatal errors causing the
3559          * ep to be released and freed.
3560          */
3561         c4iw_get_ep(&ep->com);
3562
3563         rdev = &ep->com.dev->rdev;
3564         if (c4iw_fatal_error(rdev)) {
3565                 fatal = 1;
3566                 close_complete_upcall(ep, -EIO);
3567                 ep->com.state = DEAD;
3568         }
3569         switch (ep->com.state) {
3570         case MPA_REQ_WAIT:
3571         case MPA_REQ_SENT:
3572         case MPA_REQ_RCVD:
3573         case MPA_REP_SENT:
3574         case FPDU_MODE:
3575         case CONNECTING:
3576                 close = 1;
3577                 if (abrupt)
3578                         ep->com.state = ABORTING;
3579                 else {
3580                         ep->com.state = CLOSING;
3581
3582                         /*
3583                          * if we close before we see the fw4_ack() then we fix
3584                          * up the timer state since we're reusing it.
3585                          */
3586                         if (ep->mpa_skb &&
3587                             test_bit(STOP_MPA_TIMER, &ep->com.flags)) {
3588                                 clear_bit(STOP_MPA_TIMER, &ep->com.flags);
3589                                 stop_ep_timer(ep);
3590                         }
3591                         start_ep_timer(ep);
3592                 }
3593                 set_bit(CLOSE_SENT, &ep->com.flags);
3594                 break;
3595         case CLOSING:
3596                 if (!test_and_set_bit(CLOSE_SENT, &ep->com.flags)) {
3597                         close = 1;
3598                         if (abrupt) {
3599                                 (void)stop_ep_timer(ep);
3600                                 ep->com.state = ABORTING;
3601                         } else
3602                                 ep->com.state = MORIBUND;
3603                 }
3604                 break;
3605         case MORIBUND:
3606         case ABORTING:
3607         case DEAD:
3608                 pr_debug("ignoring disconnect ep %p state %u\n",
3609                          ep, ep->com.state);
3610                 break;
3611         default:
3612                 WARN_ONCE(1, "Bad endpoint state %u\n", ep->com.state);
3613                 break;
3614         }
3615
3616         if (close) {
3617                 if (abrupt) {
3618                         set_bit(EP_DISC_ABORT, &ep->com.history);
3619                         ret = send_abort(ep);
3620                 } else {
3621                         set_bit(EP_DISC_CLOSE, &ep->com.history);
3622                         ret = send_halfclose(ep);
3623                 }
3624                 if (ret) {
3625                         set_bit(EP_DISC_FAIL, &ep->com.history);
3626                         if (!abrupt) {
3627                                 stop_ep_timer(ep);
3628                                 close_complete_upcall(ep, -EIO);
3629                         }
3630                         if (ep->com.qp) {
3631                                 struct c4iw_qp_attributes attrs;
3632
3633                                 attrs.next_state = C4IW_QP_STATE_ERROR;
3634                                 ret = c4iw_modify_qp(ep->com.qp->rhp,
3635                                                      ep->com.qp,
3636                                                      C4IW_QP_ATTR_NEXT_STATE,
3637                                                      &attrs, 1);
3638                                 if (ret)
3639                                         pr_err("%s - qp <- error failed!\n",
3640                                                __func__);
3641                         }
3642                         fatal = 1;
3643                 }
3644         }
3645         mutex_unlock(&ep->com.mutex);
3646         c4iw_put_ep(&ep->com);
3647         if (fatal)
3648                 release_ep_resources(ep);
3649         return ret;
3650 }
3651
3652 static void active_ofld_conn_reply(struct c4iw_dev *dev, struct sk_buff *skb,
3653                         struct cpl_fw6_msg_ofld_connection_wr_rpl *req)
3654 {
3655         struct c4iw_ep *ep;
3656         int atid = be32_to_cpu(req->tid);
3657
3658         ep = (struct c4iw_ep *)lookup_atid(dev->rdev.lldi.tids,
3659                                            (__force u32) req->tid);
3660         if (!ep)
3661                 return;
3662
3663         switch (req->retval) {
3664         case FW_ENOMEM:
3665                 set_bit(ACT_RETRY_NOMEM, &ep->com.history);
3666                 if (ep->retry_count++ < ACT_OPEN_RETRY_COUNT) {
3667                         send_fw_act_open_req(ep, atid);
3668                         return;
3669                 }
3670                 /* fall through */
3671         case FW_EADDRINUSE:
3672                 set_bit(ACT_RETRY_INUSE, &ep->com.history);
3673                 if (ep->retry_count++ < ACT_OPEN_RETRY_COUNT) {
3674                         send_fw_act_open_req(ep, atid);
3675                         return;
3676                 }
3677                 break;
3678         default:
3679                 pr_info("%s unexpected ofld conn wr retval %d\n",
3680                        __func__, req->retval);
3681                 break;
3682         }
3683         pr_err("active ofld_connect_wr failure %d atid %d\n",
3684                req->retval, atid);
3685         mutex_lock(&dev->rdev.stats.lock);
3686         dev->rdev.stats.act_ofld_conn_fails++;
3687         mutex_unlock(&dev->rdev.stats.lock);
3688         connect_reply_upcall(ep, status2errno(req->retval));
3689         state_set(&ep->com, DEAD);
3690         if (ep->com.remote_addr.ss_family == AF_INET6) {
3691                 struct sockaddr_in6 *sin6 =
3692                         (struct sockaddr_in6 *)&ep->com.local_addr;
3693                 cxgb4_clip_release(ep->com.dev->rdev.lldi.ports[0],
3694                                    (const u32 *)&sin6->sin6_addr.s6_addr, 1);
3695         }
3696         remove_handle(dev, &dev->atid_idr, atid);
3697         cxgb4_free_atid(dev->rdev.lldi.tids, atid);
3698         dst_release(ep->dst);
3699         cxgb4_l2t_release(ep->l2t);
3700         c4iw_put_ep(&ep->com);
3701 }
3702
3703 static void passive_ofld_conn_reply(struct c4iw_dev *dev, struct sk_buff *skb,
3704                         struct cpl_fw6_msg_ofld_connection_wr_rpl *req)
3705 {
3706         struct sk_buff *rpl_skb;
3707         struct cpl_pass_accept_req *cpl;
3708         int ret;
3709
3710         rpl_skb = (struct sk_buff *)(unsigned long)req->cookie;
3711         if (req->retval) {
3712                 pr_err("%s passive open failure %d\n", __func__, req->retval);
3713                 mutex_lock(&dev->rdev.stats.lock);
3714                 dev->rdev.stats.pas_ofld_conn_fails++;
3715                 mutex_unlock(&dev->rdev.stats.lock);
3716                 kfree_skb(rpl_skb);
3717         } else {
3718                 cpl = (struct cpl_pass_accept_req *)cplhdr(rpl_skb);
3719                 OPCODE_TID(cpl) = htonl(MK_OPCODE_TID(CPL_PASS_ACCEPT_REQ,
3720                                         (__force u32) htonl(
3721                                         (__force u32) req->tid)));
3722                 ret = pass_accept_req(dev, rpl_skb);
3723                 if (!ret)
3724                         kfree_skb(rpl_skb);
3725         }
3726         return;
3727 }
3728
3729 static int deferred_fw6_msg(struct c4iw_dev *dev, struct sk_buff *skb)
3730 {
3731         struct cpl_fw6_msg *rpl = cplhdr(skb);
3732         struct cpl_fw6_msg_ofld_connection_wr_rpl *req;
3733
3734         switch (rpl->type) {
3735         case FW6_TYPE_CQE:
3736                 c4iw_ev_dispatch(dev, (struct t4_cqe *)&rpl->data[0]);
3737                 break;
3738         case FW6_TYPE_OFLD_CONNECTION_WR_RPL:
3739                 req = (struct cpl_fw6_msg_ofld_connection_wr_rpl *)rpl->data;
3740                 switch (req->t_state) {
3741                 case TCP_SYN_SENT:
3742                         active_ofld_conn_reply(dev, skb, req);
3743                         break;
3744                 case TCP_SYN_RECV:
3745                         passive_ofld_conn_reply(dev, skb, req);
3746                         break;
3747                 default:
3748                         pr_err("%s unexpected ofld conn wr state %d\n",
3749                                __func__, req->t_state);
3750                         break;
3751                 }
3752                 break;
3753         }
3754         return 0;
3755 }
3756
3757 static void build_cpl_pass_accept_req(struct sk_buff *skb, int stid , u8 tos)
3758 {
3759         __be32 l2info;
3760         __be16 hdr_len, vlantag, len;
3761         u16 eth_hdr_len;
3762         int tcp_hdr_len, ip_hdr_len;
3763         u8 intf;
3764         struct cpl_rx_pkt *cpl = cplhdr(skb);
3765         struct cpl_pass_accept_req *req;
3766         struct tcp_options_received tmp_opt;
3767         struct c4iw_dev *dev;
3768         enum chip_type type;
3769
3770         dev = *((struct c4iw_dev **) (skb->cb + sizeof(void *)));
3771         /* Store values from cpl_rx_pkt in temporary location. */
3772         vlantag = cpl->vlan;
3773         len = cpl->len;
3774         l2info  = cpl->l2info;
3775         hdr_len = cpl->hdr_len;
3776         intf = cpl->iff;
3777
3778         __skb_pull(skb, sizeof(*req) + sizeof(struct rss_header));
3779
3780         /*
3781          * We need to parse the TCP options from SYN packet.
3782          * to generate cpl_pass_accept_req.
3783          */
3784         memset(&tmp_opt, 0, sizeof(tmp_opt));
3785         tcp_clear_options(&tmp_opt);
3786         tcp_parse_options(&init_net, skb, &tmp_opt, 0, NULL);
3787
3788         req = __skb_push(skb, sizeof(*req));
3789         memset(req, 0, sizeof(*req));
3790         req->l2info = cpu_to_be16(SYN_INTF_V(intf) |
3791                          SYN_MAC_IDX_V(RX_MACIDX_G(
3792                          be32_to_cpu(l2info))) |
3793                          SYN_XACT_MATCH_F);
3794         type = dev->rdev.lldi.adapter_type;
3795         tcp_hdr_len = RX_TCPHDR_LEN_G(be16_to_cpu(hdr_len));
3796         ip_hdr_len = RX_IPHDR_LEN_G(be16_to_cpu(hdr_len));
3797         req->hdr_len =
3798                 cpu_to_be32(SYN_RX_CHAN_V(RX_CHAN_G(be32_to_cpu(l2info))));
3799         if (CHELSIO_CHIP_VERSION(type) <= CHELSIO_T5) {
3800                 eth_hdr_len = is_t4(type) ?
3801                                 RX_ETHHDR_LEN_G(be32_to_cpu(l2info)) :
3802                                 RX_T5_ETHHDR_LEN_G(be32_to_cpu(l2info));
3803                 req->hdr_len |= cpu_to_be32(TCP_HDR_LEN_V(tcp_hdr_len) |
3804                                             IP_HDR_LEN_V(ip_hdr_len) |
3805                                             ETH_HDR_LEN_V(eth_hdr_len));
3806         } else { /* T6 and later */
3807                 eth_hdr_len = RX_T6_ETHHDR_LEN_G(be32_to_cpu(l2info));
3808                 req->hdr_len |= cpu_to_be32(T6_TCP_HDR_LEN_V(tcp_hdr_len) |
3809                                             T6_IP_HDR_LEN_V(ip_hdr_len) |
3810                                             T6_ETH_HDR_LEN_V(eth_hdr_len));
3811         }
3812         req->vlan = vlantag;
3813         req->len = len;
3814         req->tos_stid = cpu_to_be32(PASS_OPEN_TID_V(stid) |
3815                                     PASS_OPEN_TOS_V(tos));
3816         req->tcpopt.mss = htons(tmp_opt.mss_clamp);
3817         if (tmp_opt.wscale_ok)
3818                 req->tcpopt.wsf = tmp_opt.snd_wscale;
3819         req->tcpopt.tstamp = tmp_opt.saw_tstamp;
3820         if (tmp_opt.sack_ok)
3821                 req->tcpopt.sack = 1;
3822         OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_ACCEPT_REQ, 0));
3823         return;
3824 }
3825
3826 static void send_fw_pass_open_req(struct c4iw_dev *dev, struct sk_buff *skb,
3827                                   __be32 laddr, __be16 lport,
3828                                   __be32 raddr, __be16 rport,
3829                                   u32 rcv_isn, u32 filter, u16 window,
3830                                   u32 rss_qid, u8 port_id)
3831 {
3832         struct sk_buff *req_skb;
3833         struct fw_ofld_connection_wr *req;
3834         struct cpl_pass_accept_req *cpl = cplhdr(skb);
3835         int ret;
3836
3837         req_skb = alloc_skb(sizeof(struct fw_ofld_connection_wr), GFP_KERNEL);
3838         if (!req_skb)
3839                 return;
3840         req = __skb_put_zero(req_skb, sizeof(*req));
3841         req->op_compl = htonl(WR_OP_V(FW_OFLD_CONNECTION_WR) | FW_WR_COMPL_F);
3842         req->len16_pkd = htonl(FW_WR_LEN16_V(DIV_ROUND_UP(sizeof(*req), 16)));
3843         req->le.version_cpl = htonl(FW_OFLD_CONNECTION_WR_CPL_F);
3844         req->le.filter = (__force __be32) filter;
3845         req->le.lport = lport;
3846         req->le.pport = rport;
3847         req->le.u.ipv4.lip = laddr;
3848         req->le.u.ipv4.pip = raddr;
3849         req->tcb.rcv_nxt = htonl(rcv_isn + 1);
3850         req->tcb.rcv_adv = htons(window);
3851         req->tcb.t_state_to_astid =
3852                  htonl(FW_OFLD_CONNECTION_WR_T_STATE_V(TCP_SYN_RECV) |
3853                         FW_OFLD_CONNECTION_WR_RCV_SCALE_V(cpl->tcpopt.wsf) |
3854                         FW_OFLD_CONNECTION_WR_ASTID_V(
3855                         PASS_OPEN_TID_G(ntohl(cpl->tos_stid))));
3856
3857         /*
3858          * We store the qid in opt2 which will be used by the firmware
3859          * to send us the wr response.
3860          */
3861         req->tcb.opt2 = htonl(RSS_QUEUE_V(rss_qid));
3862
3863         /*
3864          * We initialize the MSS index in TCB to 0xF.
3865          * So that when driver sends cpl_pass_accept_rpl
3866          * TCB picks up the correct value. If this was 0
3867          * TP will ignore any value > 0 for MSS index.
3868          */
3869         req->tcb.opt0 = cpu_to_be64(MSS_IDX_V(0xF));
3870         req->cookie = (uintptr_t)skb;
3871
3872         set_wr_txq(req_skb, CPL_PRIORITY_CONTROL, port_id);
3873         ret = cxgb4_ofld_send(dev->rdev.lldi.ports[0], req_skb);
3874         if (ret < 0) {
3875                 pr_err("%s - cxgb4_ofld_send error %d - dropping\n", __func__,
3876                        ret);
3877                 kfree_skb(skb);
3878                 kfree_skb(req_skb);
3879         }
3880 }
3881
3882 /*
3883  * Handler for CPL_RX_PKT message. Need to handle cpl_rx_pkt
3884  * messages when a filter is being used instead of server to
3885  * redirect a syn packet. When packets hit filter they are redirected
3886  * to the offload queue and driver tries to establish the connection
3887  * using firmware work request.
3888  */
3889 static int rx_pkt(struct c4iw_dev *dev, struct sk_buff *skb)
3890 {
3891         int stid;
3892         unsigned int filter;
3893         struct ethhdr *eh = NULL;
3894         struct vlan_ethhdr *vlan_eh = NULL;
3895         struct iphdr *iph;
3896         struct tcphdr *tcph;
3897         struct rss_header *rss = (void *)skb->data;
3898         struct cpl_rx_pkt *cpl = (void *)skb->data;
3899         struct cpl_pass_accept_req *req = (void *)(rss + 1);
3900         struct l2t_entry *e;
3901         struct dst_entry *dst;
3902         struct c4iw_ep *lep = NULL;
3903         u16 window;
3904         struct port_info *pi;
3905         struct net_device *pdev;
3906         u16 rss_qid, eth_hdr_len;
3907         int step;
3908         struct neighbour *neigh;
3909
3910         /* Drop all non-SYN packets */
3911         if (!(cpl->l2info & cpu_to_be32(RXF_SYN_F)))
3912                 goto reject;
3913
3914         /*
3915          * Drop all packets which did not hit the filter.
3916          * Unlikely to happen.
3917          */
3918         if (!(rss->filter_hit && rss->filter_tid))
3919                 goto reject;
3920
3921         /*
3922          * Calculate the server tid from filter hit index from cpl_rx_pkt.
3923          */
3924         stid = (__force int) cpu_to_be32((__force u32) rss->hash_val);
3925
3926         lep = (struct c4iw_ep *)get_ep_from_stid(dev, stid);
3927         if (!lep) {
3928                 pr_warn("%s connect request on invalid stid %d\n",
3929                         __func__, stid);
3930                 goto reject;
3931         }
3932
3933         switch (CHELSIO_CHIP_VERSION(dev->rdev.lldi.adapter_type)) {
3934         case CHELSIO_T4:
3935                 eth_hdr_len = RX_ETHHDR_LEN_G(be32_to_cpu(cpl->l2info));
3936                 break;
3937         case CHELSIO_T5:
3938                 eth_hdr_len = RX_T5_ETHHDR_LEN_G(be32_to_cpu(cpl->l2info));
3939                 break;
3940         case CHELSIO_T6:
3941                 eth_hdr_len = RX_T6_ETHHDR_LEN_G(be32_to_cpu(cpl->l2info));
3942                 break;
3943         default:
3944                 pr_err("T%d Chip is not supported\n",
3945                        CHELSIO_CHIP_VERSION(dev->rdev.lldi.adapter_type));
3946                 goto reject;
3947         }
3948
3949         if (eth_hdr_len == ETH_HLEN) {
3950                 eh = (struct ethhdr *)(req + 1);
3951                 iph = (struct iphdr *)(eh + 1);
3952         } else {
3953                 vlan_eh = (struct vlan_ethhdr *)(req + 1);
3954                 iph = (struct iphdr *)(vlan_eh + 1);
3955                 skb->vlan_tci = ntohs(cpl->vlan);
3956         }
3957
3958         if (iph->version != 0x4)
3959                 goto reject;
3960
3961         tcph = (struct tcphdr *)(iph + 1);
3962         skb_set_network_header(skb, (void *)iph - (void *)rss);
3963         skb_set_transport_header(skb, (void *)tcph - (void *)rss);
3964         skb_get(skb);
3965
3966         pr_debug("lip 0x%x lport %u pip 0x%x pport %u tos %d\n",
3967                  ntohl(iph->daddr), ntohs(tcph->dest), ntohl(iph->saddr),
3968                  ntohs(tcph->source), iph->tos);
3969
3970         dst = cxgb_find_route(&dev->rdev.lldi, get_real_dev,
3971                               iph->daddr, iph->saddr, tcph->dest,
3972                               tcph->source, iph->tos);
3973         if (!dst) {
3974                 pr_err("%s - failed to find dst entry!\n", __func__);
3975                 goto reject;
3976         }
3977         neigh = dst_neigh_lookup_skb(dst, skb);
3978
3979         if (!neigh) {
3980                 pr_err("%s - failed to allocate neigh!\n", __func__);
3981                 goto free_dst;
3982         }
3983
3984         if (neigh->dev->flags & IFF_LOOPBACK) {
3985                 pdev = ip_dev_find(&init_net, iph->daddr);
3986                 e = cxgb4_l2t_get(dev->rdev.lldi.l2t, neigh,
3987                                     pdev, 0);
3988                 pi = (struct port_info *)netdev_priv(pdev);
3989                 dev_put(pdev);
3990         } else {
3991                 pdev = get_real_dev(neigh->dev);
3992                 e = cxgb4_l2t_get(dev->rdev.lldi.l2t, neigh,
3993                                         pdev, 0);
3994                 pi = (struct port_info *)netdev_priv(pdev);
3995         }
3996         neigh_release(neigh);
3997         if (!e) {
3998                 pr_err("%s - failed to allocate l2t entry!\n",
3999                        __func__);
4000                 goto free_dst;
4001         }
4002
4003         step = dev->rdev.lldi.nrxq / dev->rdev.lldi.nchan;
4004         rss_qid = dev->rdev.lldi.rxq_ids[pi->port_id * step];
4005         window = (__force u16) htons((__force u16)tcph->window);
4006
4007         /* Calcuate filter portion for LE region. */
4008         filter = (__force unsigned int) cpu_to_be32(cxgb4_select_ntuple(
4009                                                     dev->rdev.lldi.ports[0],
4010                                                     e));
4011
4012         /*
4013          * Synthesize the cpl_pass_accept_req. We have everything except the
4014          * TID. Once firmware sends a reply with TID we update the TID field
4015          * in cpl and pass it through the regular cpl_pass_accept_req path.
4016          */
4017         build_cpl_pass_accept_req(skb, stid, iph->tos);
4018         send_fw_pass_open_req(dev, skb, iph->daddr, tcph->dest, iph->saddr,
4019                               tcph->source, ntohl(tcph->seq), filter, window,
4020                               rss_qid, pi->port_id);
4021         cxgb4_l2t_release(e);
4022 free_dst:
4023         dst_release(dst);
4024 reject:
4025         if (lep)
4026                 c4iw_put_ep(&lep->com);
4027         return 0;
4028 }
4029
4030 /*
4031  * These are the real handlers that are called from a
4032  * work queue.
4033  */
4034 static c4iw_handler_func work_handlers[NUM_CPL_CMDS + NUM_FAKE_CPLS] = {
4035         [CPL_ACT_ESTABLISH] = act_establish,
4036         [CPL_ACT_OPEN_RPL] = act_open_rpl,
4037         [CPL_RX_DATA] = rx_data,
4038         [CPL_ABORT_RPL_RSS] = abort_rpl,
4039         [CPL_ABORT_RPL] = abort_rpl,
4040         [CPL_PASS_OPEN_RPL] = pass_open_rpl,
4041         [CPL_CLOSE_LISTSRV_RPL] = close_listsrv_rpl,
4042         [CPL_PASS_ACCEPT_REQ] = pass_accept_req,
4043         [CPL_PASS_ESTABLISH] = pass_establish,
4044         [CPL_PEER_CLOSE] = peer_close,
4045         [CPL_ABORT_REQ_RSS] = peer_abort,
4046         [CPL_CLOSE_CON_RPL] = close_con_rpl,
4047         [CPL_RDMA_TERMINATE] = terminate,
4048         [CPL_FW4_ACK] = fw4_ack,
4049         [CPL_FW6_MSG] = deferred_fw6_msg,
4050         [CPL_RX_PKT] = rx_pkt,
4051         [FAKE_CPL_PUT_EP_SAFE] = _put_ep_safe,
4052         [FAKE_CPL_PASS_PUT_EP_SAFE] = _put_pass_ep_safe
4053 };
4054
4055 static void process_timeout(struct c4iw_ep *ep)
4056 {
4057         struct c4iw_qp_attributes attrs;
4058         int abort = 1;
4059
4060         mutex_lock(&ep->com.mutex);
4061         pr_debug("ep %p tid %u state %d\n", ep, ep->hwtid, ep->com.state);
4062         set_bit(TIMEDOUT, &ep->com.history);
4063         switch (ep->com.state) {
4064         case MPA_REQ_SENT:
4065                 connect_reply_upcall(ep, -ETIMEDOUT);
4066                 break;
4067         case MPA_REQ_WAIT:
4068         case MPA_REQ_RCVD:
4069         case MPA_REP_SENT:
4070         case FPDU_MODE:
4071                 break;
4072         case CLOSING:
4073         case MORIBUND:
4074                 if (ep->com.cm_id && ep->com.qp) {
4075                         attrs.next_state = C4IW_QP_STATE_ERROR;
4076                         c4iw_modify_qp(ep->com.qp->rhp,
4077                                      ep->com.qp, C4IW_QP_ATTR_NEXT_STATE,
4078                                      &attrs, 1);
4079                 }
4080                 close_complete_upcall(ep, -ETIMEDOUT);
4081                 break;
4082         case ABORTING:
4083         case DEAD:
4084
4085                 /*
4086                  * These states are expected if the ep timed out at the same
4087                  * time as another thread was calling stop_ep_timer().
4088                  * So we silently do nothing for these states.
4089                  */
4090                 abort = 0;
4091                 break;
4092         default:
4093                 WARN(1, "%s unexpected state ep %p tid %u state %u\n",
4094                         __func__, ep, ep->hwtid, ep->com.state);
4095                 abort = 0;
4096         }
4097         mutex_unlock(&ep->com.mutex);
4098         if (abort)
4099                 c4iw_ep_disconnect(ep, 1, GFP_KERNEL);
4100         c4iw_put_ep(&ep->com);
4101 }
4102
4103 static void process_timedout_eps(void)
4104 {
4105         struct c4iw_ep *ep;
4106
4107         spin_lock_irq(&timeout_lock);
4108         while (!list_empty(&timeout_list)) {
4109                 struct list_head *tmp;
4110
4111                 tmp = timeout_list.next;
4112                 list_del(tmp);
4113                 tmp->next = NULL;
4114                 tmp->prev = NULL;
4115                 spin_unlock_irq(&timeout_lock);
4116                 ep = list_entry(tmp, struct c4iw_ep, entry);
4117                 process_timeout(ep);
4118                 spin_lock_irq(&timeout_lock);
4119         }
4120         spin_unlock_irq(&timeout_lock);
4121 }
4122
4123 static void process_work(struct work_struct *work)
4124 {
4125         struct sk_buff *skb = NULL;
4126         struct c4iw_dev *dev;
4127         struct cpl_act_establish *rpl;
4128         unsigned int opcode;
4129         int ret;
4130
4131         process_timedout_eps();
4132         while ((skb = skb_dequeue(&rxq))) {
4133                 rpl = cplhdr(skb);
4134                 dev = *((struct c4iw_dev **) (skb->cb + sizeof(void *)));
4135                 opcode = rpl->ot.opcode;
4136
4137                 if (opcode >= ARRAY_SIZE(work_handlers) ||
4138                     !work_handlers[opcode]) {
4139                         pr_err("No handler for opcode 0x%x.\n", opcode);
4140                         kfree_skb(skb);
4141                 } else {
4142                         ret = work_handlers[opcode](dev, skb);
4143                         if (!ret)
4144                                 kfree_skb(skb);
4145                 }
4146                 process_timedout_eps();
4147         }
4148 }
4149
4150 static DECLARE_WORK(skb_work, process_work);
4151
4152 static void ep_timeout(struct timer_list *t)
4153 {
4154         struct c4iw_ep *ep = from_timer(ep, t, timer);
4155         int kickit = 0;
4156
4157         spin_lock(&timeout_lock);
4158         if (!test_and_set_bit(TIMEOUT, &ep->com.flags)) {
4159                 /*
4160                  * Only insert if it is not already on the list.
4161                  */
4162                 if (!ep->entry.next) {
4163                         list_add_tail(&ep->entry, &timeout_list);
4164                         kickit = 1;
4165                 }
4166         }
4167         spin_unlock(&timeout_lock);
4168         if (kickit)
4169                 queue_work(workq, &skb_work);
4170 }
4171
4172 /*
4173  * All the CM events are handled on a work queue to have a safe context.
4174  */
4175 static int sched(struct c4iw_dev *dev, struct sk_buff *skb)
4176 {
4177
4178         /*
4179          * Save dev in the skb->cb area.
4180          */
4181         *((struct c4iw_dev **) (skb->cb + sizeof(void *))) = dev;
4182
4183         /*
4184          * Queue the skb and schedule the worker thread.
4185          */
4186         skb_queue_tail(&rxq, skb);
4187         queue_work(workq, &skb_work);
4188         return 0;
4189 }
4190
4191 static int set_tcb_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
4192 {
4193         struct cpl_set_tcb_rpl *rpl = cplhdr(skb);
4194
4195         if (rpl->status != CPL_ERR_NONE) {
4196                 pr_err("Unexpected SET_TCB_RPL status %u for tid %u\n",
4197                        rpl->status, GET_TID(rpl));
4198         }
4199         kfree_skb(skb);
4200         return 0;
4201 }
4202
4203 static int fw6_msg(struct c4iw_dev *dev, struct sk_buff *skb)
4204 {
4205         struct cpl_fw6_msg *rpl = cplhdr(skb);
4206         struct c4iw_wr_wait *wr_waitp;
4207         int ret;
4208
4209         pr_debug("type %u\n", rpl->type);
4210
4211         switch (rpl->type) {
4212         case FW6_TYPE_WR_RPL:
4213                 ret = (int)((be64_to_cpu(rpl->data[0]) >> 8) & 0xff);
4214                 wr_waitp = (struct c4iw_wr_wait *)(__force unsigned long) rpl->data[1];
4215                 pr_debug("wr_waitp %p ret %u\n", wr_waitp, ret);
4216                 if (wr_waitp)
4217                         c4iw_wake_up_deref(wr_waitp, ret ? -ret : 0);
4218                 kfree_skb(skb);
4219                 break;
4220         case FW6_TYPE_CQE:
4221         case FW6_TYPE_OFLD_CONNECTION_WR_RPL:
4222                 sched(dev, skb);
4223                 break;
4224         default:
4225                 pr_err("%s unexpected fw6 msg type %u\n",
4226                        __func__, rpl->type);
4227                 kfree_skb(skb);
4228                 break;
4229         }
4230         return 0;
4231 }
4232
4233 static int peer_abort_intr(struct c4iw_dev *dev, struct sk_buff *skb)
4234 {
4235         struct cpl_abort_req_rss *req = cplhdr(skb);
4236         struct c4iw_ep *ep;
4237         unsigned int tid = GET_TID(req);
4238
4239         ep = get_ep_from_tid(dev, tid);
4240         /* This EP will be dereferenced in peer_abort() */
4241         if (!ep) {
4242                 pr_warn("Abort on non-existent endpoint, tid %d\n", tid);
4243                 kfree_skb(skb);
4244                 return 0;
4245         }
4246         if (cxgb_is_neg_adv(req->status)) {
4247                 pr_debug("Negative advice on abort- tid %u status %d (%s)\n",
4248                          ep->hwtid, req->status,
4249                          neg_adv_str(req->status));
4250                 goto out;
4251         }
4252         pr_debug("ep %p tid %u state %u\n", ep, ep->hwtid, ep->com.state);
4253
4254         c4iw_wake_up_noref(ep->com.wr_waitp, -ECONNRESET);
4255 out:
4256         sched(dev, skb);
4257         return 0;
4258 }
4259
4260 /*
4261  * Most upcalls from the T4 Core go to sched() to
4262  * schedule the processing on a work queue.
4263  */
4264 c4iw_handler_func c4iw_handlers[NUM_CPL_CMDS] = {
4265         [CPL_ACT_ESTABLISH] = sched,
4266         [CPL_ACT_OPEN_RPL] = sched,
4267         [CPL_RX_DATA] = sched,
4268         [CPL_ABORT_RPL_RSS] = sched,
4269         [CPL_ABORT_RPL] = sched,
4270         [CPL_PASS_OPEN_RPL] = sched,
4271         [CPL_CLOSE_LISTSRV_RPL] = sched,
4272         [CPL_PASS_ACCEPT_REQ] = sched,
4273         [CPL_PASS_ESTABLISH] = sched,
4274         [CPL_PEER_CLOSE] = sched,
4275         [CPL_CLOSE_CON_RPL] = sched,
4276         [CPL_ABORT_REQ_RSS] = peer_abort_intr,
4277         [CPL_RDMA_TERMINATE] = sched,
4278         [CPL_FW4_ACK] = sched,
4279         [CPL_SET_TCB_RPL] = set_tcb_rpl,
4280         [CPL_FW6_MSG] = fw6_msg,
4281         [CPL_RX_PKT] = sched
4282 };
4283
4284 int __init c4iw_cm_init(void)
4285 {
4286         spin_lock_init(&timeout_lock);
4287         skb_queue_head_init(&rxq);
4288
4289         workq = alloc_ordered_workqueue("iw_cxgb4", WQ_MEM_RECLAIM);
4290         if (!workq)
4291                 return -ENOMEM;
4292
4293         return 0;
4294 }
4295
4296 void c4iw_cm_term(void)
4297 {
4298         WARN_ON(!list_empty(&timeout_list));
4299         flush_workqueue(workq);
4300         destroy_workqueue(workq);
4301 }