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