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