3216bcad7e820088f3cf3d27b7feb14b62630c7e
[firefly-linux-kernel-4.4.55.git] / drivers / infiniband / hw / cxgb3 / iwch_cm.c
1 /*
2  * Copyright (c) 2006 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/slab.h>
35 #include <linux/workqueue.h>
36 #include <linux/skbuff.h>
37 #include <linux/timer.h>
38 #include <linux/notifier.h>
39 #include <linux/inetdevice.h>
40
41 #include <net/neighbour.h>
42 #include <net/netevent.h>
43 #include <net/route.h>
44
45 #include "tcb.h"
46 #include "cxgb3_offload.h"
47 #include "iwch.h"
48 #include "iwch_provider.h"
49 #include "iwch_cm.h"
50
51 static char *states[] = {
52         "idle",
53         "listen",
54         "connecting",
55         "mpa_wait_req",
56         "mpa_req_sent",
57         "mpa_req_rcvd",
58         "mpa_rep_sent",
59         "fpdu_mode",
60         "aborting",
61         "closing",
62         "moribund",
63         "dead",
64         NULL,
65 };
66
67 int peer2peer = 0;
68 module_param(peer2peer, int, 0644);
69 MODULE_PARM_DESC(peer2peer, "Support peer2peer ULPs (default=0)");
70
71 static int ep_timeout_secs = 60;
72 module_param(ep_timeout_secs, int, 0644);
73 MODULE_PARM_DESC(ep_timeout_secs, "CM Endpoint operation timeout "
74                                    "in seconds (default=60)");
75
76 static int mpa_rev = 1;
77 module_param(mpa_rev, int, 0644);
78 MODULE_PARM_DESC(mpa_rev, "MPA Revision, 0 supports amso1100, "
79                  "1 is spec compliant. (default=1)");
80
81 static int markers_enabled = 0;
82 module_param(markers_enabled, int, 0644);
83 MODULE_PARM_DESC(markers_enabled, "Enable MPA MARKERS (default(0)=disabled)");
84
85 static int crc_enabled = 1;
86 module_param(crc_enabled, int, 0644);
87 MODULE_PARM_DESC(crc_enabled, "Enable MPA CRC (default(1)=enabled)");
88
89 static int rcv_win = 256 * 1024;
90 module_param(rcv_win, int, 0644);
91 MODULE_PARM_DESC(rcv_win, "TCP receive window in bytes (default=256)");
92
93 static int snd_win = 32 * 1024;
94 module_param(snd_win, int, 0644);
95 MODULE_PARM_DESC(snd_win, "TCP send window in bytes (default=32KB)");
96
97 static unsigned int nocong = 0;
98 module_param(nocong, uint, 0644);
99 MODULE_PARM_DESC(nocong, "Turn off congestion control (default=0)");
100
101 static unsigned int cong_flavor = 1;
102 module_param(cong_flavor, uint, 0644);
103 MODULE_PARM_DESC(cong_flavor, "TCP Congestion control flavor (default=1)");
104
105 static struct workqueue_struct *workq;
106
107 static struct sk_buff_head rxq;
108
109 static struct sk_buff *get_skb(struct sk_buff *skb, int len, gfp_t gfp);
110 static void ep_timeout(unsigned long arg);
111 static void connect_reply_upcall(struct iwch_ep *ep, int status);
112
113 static void start_ep_timer(struct iwch_ep *ep)
114 {
115         PDBG("%s ep %p\n", __func__, ep);
116         if (timer_pending(&ep->timer)) {
117                 PDBG("%s stopped / restarted timer ep %p\n", __func__, ep);
118                 del_timer_sync(&ep->timer);
119         } else
120                 get_ep(&ep->com);
121         ep->timer.expires = jiffies + ep_timeout_secs * HZ;
122         ep->timer.data = (unsigned long)ep;
123         ep->timer.function = ep_timeout;
124         add_timer(&ep->timer);
125 }
126
127 static void stop_ep_timer(struct iwch_ep *ep)
128 {
129         PDBG("%s ep %p\n", __func__, ep);
130         if (!timer_pending(&ep->timer)) {
131                 printk(KERN_ERR "%s timer stopped when its not running!  ep %p state %u\n",
132                         __func__, ep, ep->com.state);
133                 WARN_ON(1);
134                 return;
135         }
136         del_timer_sync(&ep->timer);
137         put_ep(&ep->com);
138 }
139
140 static int iwch_l2t_send(struct t3cdev *tdev, struct sk_buff *skb, struct l2t_entry *l2e)
141 {
142         int     error = 0;
143         struct cxio_rdev *rdev;
144
145         rdev = (struct cxio_rdev *)tdev->ulp;
146         if (cxio_fatal_error(rdev)) {
147                 kfree_skb(skb);
148                 return -EIO;
149         }
150         error = l2t_send(tdev, skb, l2e);
151         if (error < 0)
152                 kfree_skb(skb);
153         return error;
154 }
155
156 int iwch_cxgb3_ofld_send(struct t3cdev *tdev, struct sk_buff *skb)
157 {
158         int     error = 0;
159         struct cxio_rdev *rdev;
160
161         rdev = (struct cxio_rdev *)tdev->ulp;
162         if (cxio_fatal_error(rdev)) {
163                 kfree_skb(skb);
164                 return -EIO;
165         }
166         error = cxgb3_ofld_send(tdev, skb);
167         if (error < 0)
168                 kfree_skb(skb);
169         return error;
170 }
171
172 static void release_tid(struct t3cdev *tdev, u32 hwtid, struct sk_buff *skb)
173 {
174         struct cpl_tid_release *req;
175
176         skb = get_skb(skb, sizeof *req, GFP_KERNEL);
177         if (!skb)
178                 return;
179         req = (struct cpl_tid_release *) skb_put(skb, sizeof(*req));
180         req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
181         OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_TID_RELEASE, hwtid));
182         skb->priority = CPL_PRIORITY_SETUP;
183         iwch_cxgb3_ofld_send(tdev, skb);
184         return;
185 }
186
187 int iwch_quiesce_tid(struct iwch_ep *ep)
188 {
189         struct cpl_set_tcb_field *req;
190         struct sk_buff *skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
191
192         if (!skb)
193                 return -ENOMEM;
194         req = (struct cpl_set_tcb_field *) skb_put(skb, sizeof(*req));
195         req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
196         req->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
197         OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, ep->hwtid));
198         req->reply = 0;
199         req->cpu_idx = 0;
200         req->word = htons(W_TCB_RX_QUIESCE);
201         req->mask = cpu_to_be64(1ULL << S_TCB_RX_QUIESCE);
202         req->val = cpu_to_be64(1 << S_TCB_RX_QUIESCE);
203
204         skb->priority = CPL_PRIORITY_DATA;
205         return iwch_cxgb3_ofld_send(ep->com.tdev, skb);
206 }
207
208 int iwch_resume_tid(struct iwch_ep *ep)
209 {
210         struct cpl_set_tcb_field *req;
211         struct sk_buff *skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
212
213         if (!skb)
214                 return -ENOMEM;
215         req = (struct cpl_set_tcb_field *) skb_put(skb, sizeof(*req));
216         req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
217         req->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
218         OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, ep->hwtid));
219         req->reply = 0;
220         req->cpu_idx = 0;
221         req->word = htons(W_TCB_RX_QUIESCE);
222         req->mask = cpu_to_be64(1ULL << S_TCB_RX_QUIESCE);
223         req->val = 0;
224
225         skb->priority = CPL_PRIORITY_DATA;
226         return iwch_cxgb3_ofld_send(ep->com.tdev, skb);
227 }
228
229 static void set_emss(struct iwch_ep *ep, u16 opt)
230 {
231         PDBG("%s ep %p opt %u\n", __func__, ep, opt);
232         ep->emss = T3C_DATA(ep->com.tdev)->mtus[G_TCPOPT_MSS(opt)] - 40;
233         if (G_TCPOPT_TSTAMP(opt))
234                 ep->emss -= 12;
235         if (ep->emss < 128)
236                 ep->emss = 128;
237         PDBG("emss=%d\n", ep->emss);
238 }
239
240 static enum iwch_ep_state state_read(struct iwch_ep_common *epc)
241 {
242         unsigned long flags;
243         enum iwch_ep_state state;
244
245         spin_lock_irqsave(&epc->lock, flags);
246         state = epc->state;
247         spin_unlock_irqrestore(&epc->lock, flags);
248         return state;
249 }
250
251 static void __state_set(struct iwch_ep_common *epc, enum iwch_ep_state new)
252 {
253         epc->state = new;
254 }
255
256 static void state_set(struct iwch_ep_common *epc, enum iwch_ep_state new)
257 {
258         unsigned long flags;
259
260         spin_lock_irqsave(&epc->lock, flags);
261         PDBG("%s - %s -> %s\n", __func__, states[epc->state], states[new]);
262         __state_set(epc, new);
263         spin_unlock_irqrestore(&epc->lock, flags);
264         return;
265 }
266
267 static void *alloc_ep(int size, gfp_t gfp)
268 {
269         struct iwch_ep_common *epc;
270
271         epc = kzalloc(size, gfp);
272         if (epc) {
273                 kref_init(&epc->kref);
274                 spin_lock_init(&epc->lock);
275                 init_waitqueue_head(&epc->waitq);
276         }
277         PDBG("%s alloc ep %p\n", __func__, epc);
278         return epc;
279 }
280
281 void __free_ep(struct kref *kref)
282 {
283         struct iwch_ep *ep;
284         ep = container_of(container_of(kref, struct iwch_ep_common, kref),
285                           struct iwch_ep, com);
286         PDBG("%s ep %p state %s\n", __func__, ep, states[state_read(&ep->com)]);
287         if (test_bit(RELEASE_RESOURCES, &ep->com.flags)) {
288                 cxgb3_remove_tid(ep->com.tdev, (void *)ep, ep->hwtid);
289                 dst_release(ep->dst);
290                 l2t_release(L2DATA(ep->com.tdev), ep->l2t);
291         }
292         kfree(ep);
293 }
294
295 static void release_ep_resources(struct iwch_ep *ep)
296 {
297         PDBG("%s ep %p tid %d\n", __func__, ep, ep->hwtid);
298         set_bit(RELEASE_RESOURCES, &ep->com.flags);
299         put_ep(&ep->com);
300 }
301
302 static int status2errno(int status)
303 {
304         switch (status) {
305         case CPL_ERR_NONE:
306                 return 0;
307         case CPL_ERR_CONN_RESET:
308                 return -ECONNRESET;
309         case CPL_ERR_ARP_MISS:
310                 return -EHOSTUNREACH;
311         case CPL_ERR_CONN_TIMEDOUT:
312                 return -ETIMEDOUT;
313         case CPL_ERR_TCAM_FULL:
314                 return -ENOMEM;
315         case CPL_ERR_CONN_EXIST:
316                 return -EADDRINUSE;
317         default:
318                 return -EIO;
319         }
320 }
321
322 /*
323  * Try and reuse skbs already allocated...
324  */
325 static struct sk_buff *get_skb(struct sk_buff *skb, int len, gfp_t gfp)
326 {
327         if (skb && !skb_is_nonlinear(skb) && !skb_cloned(skb)) {
328                 skb_trim(skb, 0);
329                 skb_get(skb);
330         } else {
331                 skb = alloc_skb(len, gfp);
332         }
333         return skb;
334 }
335
336 static struct rtable *find_route(struct t3cdev *dev, __be32 local_ip,
337                                  __be32 peer_ip, __be16 local_port,
338                                  __be16 peer_port, u8 tos)
339 {
340         struct rtable *rt;
341
342         rt = ip_route_output_ports(&init_net, NULL, peer_ip, local_ip,
343                                    peer_port, local_port, IPPROTO_TCP,
344                                    tos, 0);
345         if (IS_ERR(rt))
346                 return NULL;
347         return rt;
348 }
349
350 static unsigned int find_best_mtu(const struct t3c_data *d, unsigned short mtu)
351 {
352         int i = 0;
353
354         while (i < d->nmtus - 1 && d->mtus[i + 1] <= mtu)
355                 ++i;
356         return i;
357 }
358
359 static void arp_failure_discard(struct t3cdev *dev, struct sk_buff *skb)
360 {
361         PDBG("%s t3cdev %p\n", __func__, dev);
362         kfree_skb(skb);
363 }
364
365 /*
366  * Handle an ARP failure for an active open.
367  */
368 static void act_open_req_arp_failure(struct t3cdev *dev, struct sk_buff *skb)
369 {
370         printk(KERN_ERR MOD "ARP failure duing connect\n");
371         kfree_skb(skb);
372 }
373
374 /*
375  * Handle an ARP failure for a CPL_ABORT_REQ.  Change it into a no RST variant
376  * and send it along.
377  */
378 static void abort_arp_failure(struct t3cdev *dev, struct sk_buff *skb)
379 {
380         struct cpl_abort_req *req = cplhdr(skb);
381
382         PDBG("%s t3cdev %p\n", __func__, dev);
383         req->cmd = CPL_ABORT_NO_RST;
384         iwch_cxgb3_ofld_send(dev, skb);
385 }
386
387 static int send_halfclose(struct iwch_ep *ep, gfp_t gfp)
388 {
389         struct cpl_close_con_req *req;
390         struct sk_buff *skb;
391
392         PDBG("%s ep %p\n", __func__, ep);
393         skb = get_skb(NULL, sizeof(*req), gfp);
394         if (!skb) {
395                 printk(KERN_ERR MOD "%s - failed to alloc skb\n", __func__);
396                 return -ENOMEM;
397         }
398         skb->priority = CPL_PRIORITY_DATA;
399         set_arp_failure_handler(skb, arp_failure_discard);
400         req = (struct cpl_close_con_req *) skb_put(skb, sizeof(*req));
401         req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_CLOSE_CON));
402         req->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
403         OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_CON_REQ, ep->hwtid));
404         return iwch_l2t_send(ep->com.tdev, skb, ep->l2t);
405 }
406
407 static int send_abort(struct iwch_ep *ep, struct sk_buff *skb, gfp_t gfp)
408 {
409         struct cpl_abort_req *req;
410
411         PDBG("%s ep %p\n", __func__, ep);
412         skb = get_skb(skb, sizeof(*req), gfp);
413         if (!skb) {
414                 printk(KERN_ERR MOD "%s - failed to alloc skb.\n",
415                        __func__);
416                 return -ENOMEM;
417         }
418         skb->priority = CPL_PRIORITY_DATA;
419         set_arp_failure_handler(skb, abort_arp_failure);
420         req = (struct cpl_abort_req *) skb_put(skb, sizeof(*req));
421         req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_HOST_ABORT_CON_REQ));
422         req->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
423         OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_ABORT_REQ, ep->hwtid));
424         req->cmd = CPL_ABORT_SEND_RST;
425         return iwch_l2t_send(ep->com.tdev, skb, ep->l2t);
426 }
427
428 static int send_connect(struct iwch_ep *ep)
429 {
430         struct cpl_act_open_req *req;
431         struct sk_buff *skb;
432         u32 opt0h, opt0l, opt2;
433         unsigned int mtu_idx;
434         int wscale;
435
436         PDBG("%s ep %p\n", __func__, ep);
437
438         skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
439         if (!skb) {
440                 printk(KERN_ERR MOD "%s - failed to alloc skb.\n",
441                        __func__);
442                 return -ENOMEM;
443         }
444         mtu_idx = find_best_mtu(T3C_DATA(ep->com.tdev), dst_mtu(ep->dst));
445         wscale = compute_wscale(rcv_win);
446         opt0h = V_NAGLE(0) |
447             V_NO_CONG(nocong) |
448             V_KEEP_ALIVE(1) |
449             F_TCAM_BYPASS |
450             V_WND_SCALE(wscale) |
451             V_MSS_IDX(mtu_idx) |
452             V_L2T_IDX(ep->l2t->idx) | V_TX_CHANNEL(ep->l2t->smt_idx);
453         opt0l = V_TOS((ep->tos >> 2) & M_TOS) | V_RCV_BUFSIZ(rcv_win>>10);
454         opt2 = F_RX_COALESCE_VALID | V_RX_COALESCE(0) | V_FLAVORS_VALID(1) |
455                V_CONG_CONTROL_FLAVOR(cong_flavor);
456         skb->priority = CPL_PRIORITY_SETUP;
457         set_arp_failure_handler(skb, act_open_req_arp_failure);
458
459         req = (struct cpl_act_open_req *) skb_put(skb, sizeof(*req));
460         req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
461         OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_ACT_OPEN_REQ, ep->atid));
462         req->local_port = ep->com.local_addr.sin_port;
463         req->peer_port = ep->com.remote_addr.sin_port;
464         req->local_ip = ep->com.local_addr.sin_addr.s_addr;
465         req->peer_ip = ep->com.remote_addr.sin_addr.s_addr;
466         req->opt0h = htonl(opt0h);
467         req->opt0l = htonl(opt0l);
468         req->params = 0;
469         req->opt2 = htonl(opt2);
470         return iwch_l2t_send(ep->com.tdev, skb, ep->l2t);
471 }
472
473 static void send_mpa_req(struct iwch_ep *ep, struct sk_buff *skb)
474 {
475         int mpalen;
476         struct tx_data_wr *req;
477         struct mpa_message *mpa;
478         int len;
479
480         PDBG("%s ep %p pd_len %d\n", __func__, ep, ep->plen);
481
482         BUG_ON(skb_cloned(skb));
483
484         mpalen = sizeof(*mpa) + ep->plen;
485         if (skb->data + mpalen + sizeof(*req) > skb_end_pointer(skb)) {
486                 kfree_skb(skb);
487                 skb=alloc_skb(mpalen + sizeof(*req), GFP_KERNEL);
488                 if (!skb) {
489                         connect_reply_upcall(ep, -ENOMEM);
490                         return;
491                 }
492         }
493         skb_trim(skb, 0);
494         skb_reserve(skb, sizeof(*req));
495         skb_put(skb, mpalen);
496         skb->priority = CPL_PRIORITY_DATA;
497         mpa = (struct mpa_message *) skb->data;
498         memset(mpa, 0, sizeof(*mpa));
499         memcpy(mpa->key, MPA_KEY_REQ, sizeof(mpa->key));
500         mpa->flags = (crc_enabled ? MPA_CRC : 0) |
501                      (markers_enabled ? MPA_MARKERS : 0);
502         mpa->private_data_size = htons(ep->plen);
503         mpa->revision = mpa_rev;
504
505         if (ep->plen)
506                 memcpy(mpa->private_data, ep->mpa_pkt + sizeof(*mpa), ep->plen);
507
508         /*
509          * Reference the mpa skb.  This ensures the data area
510          * will remain in memory until the hw acks the tx.
511          * Function tx_ack() will deref it.
512          */
513         skb_get(skb);
514         set_arp_failure_handler(skb, arp_failure_discard);
515         skb_reset_transport_header(skb);
516         len = skb->len;
517         req = (struct tx_data_wr *) skb_push(skb, sizeof(*req));
518         req->wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_TX_DATA)|F_WR_COMPL);
519         req->wr_lo = htonl(V_WR_TID(ep->hwtid));
520         req->len = htonl(len);
521         req->param = htonl(V_TX_PORT(ep->l2t->smt_idx) |
522                            V_TX_SNDBUF(snd_win>>15));
523         req->flags = htonl(F_TX_INIT);
524         req->sndseq = htonl(ep->snd_seq);
525         BUG_ON(ep->mpa_skb);
526         ep->mpa_skb = skb;
527         iwch_l2t_send(ep->com.tdev, skb, ep->l2t);
528         start_ep_timer(ep);
529         state_set(&ep->com, MPA_REQ_SENT);
530         return;
531 }
532
533 static int send_mpa_reject(struct iwch_ep *ep, const void *pdata, u8 plen)
534 {
535         int mpalen;
536         struct tx_data_wr *req;
537         struct mpa_message *mpa;
538         struct sk_buff *skb;
539
540         PDBG("%s ep %p plen %d\n", __func__, ep, plen);
541
542         mpalen = sizeof(*mpa) + plen;
543
544         skb = get_skb(NULL, mpalen + sizeof(*req), GFP_KERNEL);
545         if (!skb) {
546                 printk(KERN_ERR MOD "%s - cannot alloc skb!\n", __func__);
547                 return -ENOMEM;
548         }
549         skb_reserve(skb, sizeof(*req));
550         mpa = (struct mpa_message *) skb_put(skb, mpalen);
551         memset(mpa, 0, sizeof(*mpa));
552         memcpy(mpa->key, MPA_KEY_REP, sizeof(mpa->key));
553         mpa->flags = MPA_REJECT;
554         mpa->revision = mpa_rev;
555         mpa->private_data_size = htons(plen);
556         if (plen)
557                 memcpy(mpa->private_data, pdata, plen);
558
559         /*
560          * Reference the mpa skb again.  This ensures the data area
561          * will remain in memory until the hw acks the tx.
562          * Function tx_ack() will deref it.
563          */
564         skb_get(skb);
565         skb->priority = CPL_PRIORITY_DATA;
566         set_arp_failure_handler(skb, arp_failure_discard);
567         skb_reset_transport_header(skb);
568         req = (struct tx_data_wr *) skb_push(skb, sizeof(*req));
569         req->wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_TX_DATA)|F_WR_COMPL);
570         req->wr_lo = htonl(V_WR_TID(ep->hwtid));
571         req->len = htonl(mpalen);
572         req->param = htonl(V_TX_PORT(ep->l2t->smt_idx) |
573                            V_TX_SNDBUF(snd_win>>15));
574         req->flags = htonl(F_TX_INIT);
575         req->sndseq = htonl(ep->snd_seq);
576         BUG_ON(ep->mpa_skb);
577         ep->mpa_skb = skb;
578         return iwch_l2t_send(ep->com.tdev, skb, ep->l2t);
579 }
580
581 static int send_mpa_reply(struct iwch_ep *ep, const void *pdata, u8 plen)
582 {
583         int mpalen;
584         struct tx_data_wr *req;
585         struct mpa_message *mpa;
586         int len;
587         struct sk_buff *skb;
588
589         PDBG("%s ep %p plen %d\n", __func__, ep, plen);
590
591         mpalen = sizeof(*mpa) + plen;
592
593         skb = get_skb(NULL, mpalen + sizeof(*req), GFP_KERNEL);
594         if (!skb) {
595                 printk(KERN_ERR MOD "%s - cannot alloc skb!\n", __func__);
596                 return -ENOMEM;
597         }
598         skb->priority = CPL_PRIORITY_DATA;
599         skb_reserve(skb, sizeof(*req));
600         mpa = (struct mpa_message *) skb_put(skb, mpalen);
601         memset(mpa, 0, sizeof(*mpa));
602         memcpy(mpa->key, MPA_KEY_REP, sizeof(mpa->key));
603         mpa->flags = (ep->mpa_attr.crc_enabled ? MPA_CRC : 0) |
604                      (markers_enabled ? MPA_MARKERS : 0);
605         mpa->revision = mpa_rev;
606         mpa->private_data_size = htons(plen);
607         if (plen)
608                 memcpy(mpa->private_data, pdata, plen);
609
610         /*
611          * Reference the mpa skb.  This ensures the data area
612          * will remain in memory until the hw acks the tx.
613          * Function tx_ack() will deref it.
614          */
615         skb_get(skb);
616         set_arp_failure_handler(skb, arp_failure_discard);
617         skb_reset_transport_header(skb);
618         len = skb->len;
619         req = (struct tx_data_wr *) skb_push(skb, sizeof(*req));
620         req->wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_TX_DATA)|F_WR_COMPL);
621         req->wr_lo = htonl(V_WR_TID(ep->hwtid));
622         req->len = htonl(len);
623         req->param = htonl(V_TX_PORT(ep->l2t->smt_idx) |
624                            V_TX_SNDBUF(snd_win>>15));
625         req->flags = htonl(F_TX_INIT);
626         req->sndseq = htonl(ep->snd_seq);
627         ep->mpa_skb = skb;
628         state_set(&ep->com, MPA_REP_SENT);
629         return iwch_l2t_send(ep->com.tdev, skb, ep->l2t);
630 }
631
632 static int act_establish(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
633 {
634         struct iwch_ep *ep = ctx;
635         struct cpl_act_establish *req = cplhdr(skb);
636         unsigned int tid = GET_TID(req);
637
638         PDBG("%s ep %p tid %d\n", __func__, ep, tid);
639
640         dst_confirm(ep->dst);
641
642         /* setup the hwtid for this connection */
643         ep->hwtid = tid;
644         cxgb3_insert_tid(ep->com.tdev, &t3c_client, ep, tid);
645
646         ep->snd_seq = ntohl(req->snd_isn);
647         ep->rcv_seq = ntohl(req->rcv_isn);
648
649         set_emss(ep, ntohs(req->tcp_opt));
650
651         /* dealloc the atid */
652         cxgb3_free_atid(ep->com.tdev, ep->atid);
653
654         /* start MPA negotiation */
655         send_mpa_req(ep, skb);
656
657         return 0;
658 }
659
660 static void abort_connection(struct iwch_ep *ep, struct sk_buff *skb, gfp_t gfp)
661 {
662         PDBG("%s ep %p\n", __FILE__, ep);
663         state_set(&ep->com, ABORTING);
664         send_abort(ep, skb, gfp);
665 }
666
667 static void close_complete_upcall(struct iwch_ep *ep)
668 {
669         struct iw_cm_event event;
670
671         PDBG("%s ep %p\n", __func__, ep);
672         memset(&event, 0, sizeof(event));
673         event.event = IW_CM_EVENT_CLOSE;
674         if (ep->com.cm_id) {
675                 PDBG("close complete delivered ep %p cm_id %p tid %d\n",
676                      ep, ep->com.cm_id, ep->hwtid);
677                 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
678                 ep->com.cm_id->rem_ref(ep->com.cm_id);
679                 ep->com.cm_id = NULL;
680                 ep->com.qp = NULL;
681         }
682 }
683
684 static void peer_close_upcall(struct iwch_ep *ep)
685 {
686         struct iw_cm_event event;
687
688         PDBG("%s ep %p\n", __func__, ep);
689         memset(&event, 0, sizeof(event));
690         event.event = IW_CM_EVENT_DISCONNECT;
691         if (ep->com.cm_id) {
692                 PDBG("peer close delivered ep %p cm_id %p tid %d\n",
693                      ep, ep->com.cm_id, ep->hwtid);
694                 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
695         }
696 }
697
698 static void peer_abort_upcall(struct iwch_ep *ep)
699 {
700         struct iw_cm_event event;
701
702         PDBG("%s ep %p\n", __func__, ep);
703         memset(&event, 0, sizeof(event));
704         event.event = IW_CM_EVENT_CLOSE;
705         event.status = -ECONNRESET;
706         if (ep->com.cm_id) {
707                 PDBG("abort delivered ep %p cm_id %p tid %d\n", ep,
708                      ep->com.cm_id, ep->hwtid);
709                 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
710                 ep->com.cm_id->rem_ref(ep->com.cm_id);
711                 ep->com.cm_id = NULL;
712                 ep->com.qp = NULL;
713         }
714 }
715
716 static void connect_reply_upcall(struct iwch_ep *ep, int status)
717 {
718         struct iw_cm_event event;
719
720         PDBG("%s ep %p status %d\n", __func__, ep, status);
721         memset(&event, 0, sizeof(event));
722         event.event = IW_CM_EVENT_CONNECT_REPLY;
723         event.status = status;
724         event.local_addr = ep->com.local_addr;
725         event.remote_addr = ep->com.remote_addr;
726
727         if ((status == 0) || (status == -ECONNREFUSED)) {
728                 event.private_data_len = ep->plen;
729                 event.private_data = ep->mpa_pkt + sizeof(struct mpa_message);
730         }
731         if (ep->com.cm_id) {
732                 PDBG("%s ep %p tid %d status %d\n", __func__, ep,
733                      ep->hwtid, status);
734                 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
735         }
736         if (status < 0) {
737                 ep->com.cm_id->rem_ref(ep->com.cm_id);
738                 ep->com.cm_id = NULL;
739                 ep->com.qp = NULL;
740         }
741 }
742
743 static void connect_request_upcall(struct iwch_ep *ep)
744 {
745         struct iw_cm_event event;
746
747         PDBG("%s ep %p tid %d\n", __func__, ep, ep->hwtid);
748         memset(&event, 0, sizeof(event));
749         event.event = IW_CM_EVENT_CONNECT_REQUEST;
750         event.local_addr = ep->com.local_addr;
751         event.remote_addr = ep->com.remote_addr;
752         event.private_data_len = ep->plen;
753         event.private_data = ep->mpa_pkt + sizeof(struct mpa_message);
754         event.provider_data = ep;
755         if (state_read(&ep->parent_ep->com) != DEAD) {
756                 get_ep(&ep->com);
757                 ep->parent_ep->com.cm_id->event_handler(
758                                                 ep->parent_ep->com.cm_id,
759                                                 &event);
760         }
761         put_ep(&ep->parent_ep->com);
762         ep->parent_ep = NULL;
763 }
764
765 static void established_upcall(struct iwch_ep *ep)
766 {
767         struct iw_cm_event event;
768
769         PDBG("%s ep %p\n", __func__, ep);
770         memset(&event, 0, sizeof(event));
771         event.event = IW_CM_EVENT_ESTABLISHED;
772         if (ep->com.cm_id) {
773                 PDBG("%s ep %p tid %d\n", __func__, ep, ep->hwtid);
774                 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
775         }
776 }
777
778 static int update_rx_credits(struct iwch_ep *ep, u32 credits)
779 {
780         struct cpl_rx_data_ack *req;
781         struct sk_buff *skb;
782
783         PDBG("%s ep %p credits %u\n", __func__, ep, credits);
784         skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
785         if (!skb) {
786                 printk(KERN_ERR MOD "update_rx_credits - cannot alloc skb!\n");
787                 return 0;
788         }
789
790         req = (struct cpl_rx_data_ack *) skb_put(skb, sizeof(*req));
791         req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
792         OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_RX_DATA_ACK, ep->hwtid));
793         req->credit_dack = htonl(V_RX_CREDITS(credits) | V_RX_FORCE_ACK(1));
794         skb->priority = CPL_PRIORITY_ACK;
795         iwch_cxgb3_ofld_send(ep->com.tdev, skb);
796         return credits;
797 }
798
799 static void process_mpa_reply(struct iwch_ep *ep, struct sk_buff *skb)
800 {
801         struct mpa_message *mpa;
802         u16 plen;
803         struct iwch_qp_attributes attrs;
804         enum iwch_qp_attr_mask mask;
805         int err;
806
807         PDBG("%s ep %p\n", __func__, ep);
808
809         /*
810          * Stop mpa timer.  If it expired, then the state has
811          * changed and we bail since ep_timeout already aborted
812          * the connection.
813          */
814         stop_ep_timer(ep);
815         if (state_read(&ep->com) != MPA_REQ_SENT)
816                 return;
817
818         /*
819          * If we get more than the supported amount of private data
820          * then we must fail this connection.
821          */
822         if (ep->mpa_pkt_len + skb->len > sizeof(ep->mpa_pkt)) {
823                 err = -EINVAL;
824                 goto err;
825         }
826
827         /*
828          * copy the new data into our accumulation buffer.
829          */
830         skb_copy_from_linear_data(skb, &(ep->mpa_pkt[ep->mpa_pkt_len]),
831                                   skb->len);
832         ep->mpa_pkt_len += skb->len;
833
834         /*
835          * if we don't even have the mpa message, then bail.
836          */
837         if (ep->mpa_pkt_len < sizeof(*mpa))
838                 return;
839         mpa = (struct mpa_message *) ep->mpa_pkt;
840
841         /* Validate MPA header. */
842         if (mpa->revision != mpa_rev) {
843                 err = -EPROTO;
844                 goto err;
845         }
846         if (memcmp(mpa->key, MPA_KEY_REP, sizeof(mpa->key))) {
847                 err = -EPROTO;
848                 goto err;
849         }
850
851         plen = ntohs(mpa->private_data_size);
852
853         /*
854          * Fail if there's too much private data.
855          */
856         if (plen > MPA_MAX_PRIVATE_DATA) {
857                 err = -EPROTO;
858                 goto err;
859         }
860
861         /*
862          * If plen does not account for pkt size
863          */
864         if (ep->mpa_pkt_len > (sizeof(*mpa) + plen)) {
865                 err = -EPROTO;
866                 goto err;
867         }
868
869         ep->plen = (u8) plen;
870
871         /*
872          * If we don't have all the pdata yet, then bail.
873          * We'll continue process when more data arrives.
874          */
875         if (ep->mpa_pkt_len < (sizeof(*mpa) + plen))
876                 return;
877
878         if (mpa->flags & MPA_REJECT) {
879                 err = -ECONNREFUSED;
880                 goto err;
881         }
882
883         /*
884          * If we get here we have accumulated the entire mpa
885          * start reply message including private data. And
886          * the MPA header is valid.
887          */
888         state_set(&ep->com, FPDU_MODE);
889         ep->mpa_attr.initiator = 1;
890         ep->mpa_attr.crc_enabled = (mpa->flags & MPA_CRC) | crc_enabled ? 1 : 0;
891         ep->mpa_attr.recv_marker_enabled = markers_enabled;
892         ep->mpa_attr.xmit_marker_enabled = mpa->flags & MPA_MARKERS ? 1 : 0;
893         ep->mpa_attr.version = mpa_rev;
894         PDBG("%s - crc_enabled=%d, recv_marker_enabled=%d, "
895              "xmit_marker_enabled=%d, version=%d\n", __func__,
896              ep->mpa_attr.crc_enabled, ep->mpa_attr.recv_marker_enabled,
897              ep->mpa_attr.xmit_marker_enabled, ep->mpa_attr.version);
898
899         attrs.mpa_attr = ep->mpa_attr;
900         attrs.max_ird = ep->ird;
901         attrs.max_ord = ep->ord;
902         attrs.llp_stream_handle = ep;
903         attrs.next_state = IWCH_QP_STATE_RTS;
904
905         mask = IWCH_QP_ATTR_NEXT_STATE |
906             IWCH_QP_ATTR_LLP_STREAM_HANDLE | IWCH_QP_ATTR_MPA_ATTR |
907             IWCH_QP_ATTR_MAX_IRD | IWCH_QP_ATTR_MAX_ORD;
908
909         /* bind QP and TID with INIT_WR */
910         err = iwch_modify_qp(ep->com.qp->rhp,
911                              ep->com.qp, mask, &attrs, 1);
912         if (err)
913                 goto err;
914
915         if (peer2peer && iwch_rqes_posted(ep->com.qp) == 0) {
916                 iwch_post_zb_read(ep->com.qp);
917         }
918
919         goto out;
920 err:
921         abort_connection(ep, skb, GFP_KERNEL);
922 out:
923         connect_reply_upcall(ep, err);
924         return;
925 }
926
927 static void process_mpa_request(struct iwch_ep *ep, struct sk_buff *skb)
928 {
929         struct mpa_message *mpa;
930         u16 plen;
931
932         PDBG("%s ep %p\n", __func__, ep);
933
934         /*
935          * Stop mpa timer.  If it expired, then the state has
936          * changed and we bail since ep_timeout already aborted
937          * the connection.
938          */
939         stop_ep_timer(ep);
940         if (state_read(&ep->com) != MPA_REQ_WAIT)
941                 return;
942
943         /*
944          * If we get more than the supported amount of private data
945          * then we must fail this connection.
946          */
947         if (ep->mpa_pkt_len + skb->len > sizeof(ep->mpa_pkt)) {
948                 abort_connection(ep, skb, GFP_KERNEL);
949                 return;
950         }
951
952         PDBG("%s enter (%s line %u)\n", __func__, __FILE__, __LINE__);
953
954         /*
955          * Copy the new data into our accumulation buffer.
956          */
957         skb_copy_from_linear_data(skb, &(ep->mpa_pkt[ep->mpa_pkt_len]),
958                                   skb->len);
959         ep->mpa_pkt_len += skb->len;
960
961         /*
962          * If we don't even have the mpa message, then bail.
963          * We'll continue process when more data arrives.
964          */
965         if (ep->mpa_pkt_len < sizeof(*mpa))
966                 return;
967         PDBG("%s enter (%s line %u)\n", __func__, __FILE__, __LINE__);
968         mpa = (struct mpa_message *) ep->mpa_pkt;
969
970         /*
971          * Validate MPA Header.
972          */
973         if (mpa->revision != mpa_rev) {
974                 abort_connection(ep, skb, GFP_KERNEL);
975                 return;
976         }
977
978         if (memcmp(mpa->key, MPA_KEY_REQ, sizeof(mpa->key))) {
979                 abort_connection(ep, skb, GFP_KERNEL);
980                 return;
981         }
982
983         plen = ntohs(mpa->private_data_size);
984
985         /*
986          * Fail if there's too much private data.
987          */
988         if (plen > MPA_MAX_PRIVATE_DATA) {
989                 abort_connection(ep, skb, GFP_KERNEL);
990                 return;
991         }
992
993         /*
994          * If plen does not account for pkt size
995          */
996         if (ep->mpa_pkt_len > (sizeof(*mpa) + plen)) {
997                 abort_connection(ep, skb, GFP_KERNEL);
998                 return;
999         }
1000         ep->plen = (u8) plen;
1001
1002         /*
1003          * If we don't have all the pdata yet, then bail.
1004          */
1005         if (ep->mpa_pkt_len < (sizeof(*mpa) + plen))
1006                 return;
1007
1008         /*
1009          * If we get here we have accumulated the entire mpa
1010          * start reply message including private data.
1011          */
1012         ep->mpa_attr.initiator = 0;
1013         ep->mpa_attr.crc_enabled = (mpa->flags & MPA_CRC) | crc_enabled ? 1 : 0;
1014         ep->mpa_attr.recv_marker_enabled = markers_enabled;
1015         ep->mpa_attr.xmit_marker_enabled = mpa->flags & MPA_MARKERS ? 1 : 0;
1016         ep->mpa_attr.version = mpa_rev;
1017         PDBG("%s - crc_enabled=%d, recv_marker_enabled=%d, "
1018              "xmit_marker_enabled=%d, version=%d\n", __func__,
1019              ep->mpa_attr.crc_enabled, ep->mpa_attr.recv_marker_enabled,
1020              ep->mpa_attr.xmit_marker_enabled, ep->mpa_attr.version);
1021
1022         state_set(&ep->com, MPA_REQ_RCVD);
1023
1024         /* drive upcall */
1025         connect_request_upcall(ep);
1026         return;
1027 }
1028
1029 static int rx_data(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1030 {
1031         struct iwch_ep *ep = ctx;
1032         struct cpl_rx_data *hdr = cplhdr(skb);
1033         unsigned int dlen = ntohs(hdr->len);
1034
1035         PDBG("%s ep %p dlen %u\n", __func__, ep, dlen);
1036
1037         skb_pull(skb, sizeof(*hdr));
1038         skb_trim(skb, dlen);
1039
1040         ep->rcv_seq += dlen;
1041         BUG_ON(ep->rcv_seq != (ntohl(hdr->seq) + dlen));
1042
1043         switch (state_read(&ep->com)) {
1044         case MPA_REQ_SENT:
1045                 process_mpa_reply(ep, skb);
1046                 break;
1047         case MPA_REQ_WAIT:
1048                 process_mpa_request(ep, skb);
1049                 break;
1050         case MPA_REP_SENT:
1051                 break;
1052         default:
1053                 printk(KERN_ERR MOD "%s Unexpected streaming data."
1054                        " ep %p state %d tid %d\n",
1055                        __func__, ep, state_read(&ep->com), ep->hwtid);
1056
1057                 /*
1058                  * The ep will timeout and inform the ULP of the failure.
1059                  * See ep_timeout().
1060                  */
1061                 break;
1062         }
1063
1064         /* update RX credits */
1065         update_rx_credits(ep, dlen);
1066
1067         return CPL_RET_BUF_DONE;
1068 }
1069
1070 /*
1071  * Upcall from the adapter indicating data has been transmitted.
1072  * For us its just the single MPA request or reply.  We can now free
1073  * the skb holding the mpa message.
1074  */
1075 static int tx_ack(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1076 {
1077         struct iwch_ep *ep = ctx;
1078         struct cpl_wr_ack *hdr = cplhdr(skb);
1079         unsigned int credits = ntohs(hdr->credits);
1080
1081         PDBG("%s ep %p credits %u\n", __func__, ep, credits);
1082
1083         if (credits == 0) {
1084                 PDBG("%s 0 credit ack  ep %p state %u\n",
1085                      __func__, ep, state_read(&ep->com));
1086                 return CPL_RET_BUF_DONE;
1087         }
1088
1089         BUG_ON(credits != 1);
1090         dst_confirm(ep->dst);
1091         if (!ep->mpa_skb) {
1092                 PDBG("%s rdma_init wr_ack ep %p state %u\n",
1093                         __func__, ep, state_read(&ep->com));
1094                 if (ep->mpa_attr.initiator) {
1095                         PDBG("%s initiator ep %p state %u\n",
1096                                 __func__, ep, state_read(&ep->com));
1097                         if (peer2peer)
1098                                 iwch_post_zb_read(ep->com.qp);
1099                 } else {
1100                         PDBG("%s responder ep %p state %u\n",
1101                                 __func__, ep, state_read(&ep->com));
1102                         ep->com.rpl_done = 1;
1103                         wake_up(&ep->com.waitq);
1104                 }
1105         } else {
1106                 PDBG("%s lsm ack ep %p state %u freeing skb\n",
1107                         __func__, ep, state_read(&ep->com));
1108                 kfree_skb(ep->mpa_skb);
1109                 ep->mpa_skb = NULL;
1110         }
1111         return CPL_RET_BUF_DONE;
1112 }
1113
1114 static int abort_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1115 {
1116         struct iwch_ep *ep = ctx;
1117         unsigned long flags;
1118         int release = 0;
1119
1120         PDBG("%s ep %p\n", __func__, ep);
1121         BUG_ON(!ep);
1122
1123         /*
1124          * We get 2 abort replies from the HW.  The first one must
1125          * be ignored except for scribbling that we need one more.
1126          */
1127         if (!test_and_set_bit(ABORT_REQ_IN_PROGRESS, &ep->com.flags)) {
1128                 return CPL_RET_BUF_DONE;
1129         }
1130
1131         spin_lock_irqsave(&ep->com.lock, flags);
1132         switch (ep->com.state) {
1133         case ABORTING:
1134                 close_complete_upcall(ep);
1135                 __state_set(&ep->com, DEAD);
1136                 release = 1;
1137                 break;
1138         default:
1139                 printk(KERN_ERR "%s ep %p state %d\n",
1140                      __func__, ep, ep->com.state);
1141                 break;
1142         }
1143         spin_unlock_irqrestore(&ep->com.lock, flags);
1144
1145         if (release)
1146                 release_ep_resources(ep);
1147         return CPL_RET_BUF_DONE;
1148 }
1149
1150 /*
1151  * Return whether a failed active open has allocated a TID
1152  */
1153 static inline int act_open_has_tid(int status)
1154 {
1155         return status != CPL_ERR_TCAM_FULL && status != CPL_ERR_CONN_EXIST &&
1156                status != CPL_ERR_ARP_MISS;
1157 }
1158
1159 static int act_open_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1160 {
1161         struct iwch_ep *ep = ctx;
1162         struct cpl_act_open_rpl *rpl = cplhdr(skb);
1163
1164         PDBG("%s ep %p status %u errno %d\n", __func__, ep, rpl->status,
1165              status2errno(rpl->status));
1166         connect_reply_upcall(ep, status2errno(rpl->status));
1167         state_set(&ep->com, DEAD);
1168         if (ep->com.tdev->type != T3A && act_open_has_tid(rpl->status))
1169                 release_tid(ep->com.tdev, GET_TID(rpl), NULL);
1170         cxgb3_free_atid(ep->com.tdev, ep->atid);
1171         dst_release(ep->dst);
1172         l2t_release(L2DATA(ep->com.tdev), ep->l2t);
1173         put_ep(&ep->com);
1174         return CPL_RET_BUF_DONE;
1175 }
1176
1177 static int listen_start(struct iwch_listen_ep *ep)
1178 {
1179         struct sk_buff *skb;
1180         struct cpl_pass_open_req *req;
1181
1182         PDBG("%s ep %p\n", __func__, ep);
1183         skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
1184         if (!skb) {
1185                 printk(KERN_ERR MOD "t3c_listen_start failed to alloc skb!\n");
1186                 return -ENOMEM;
1187         }
1188
1189         req = (struct cpl_pass_open_req *) skb_put(skb, sizeof(*req));
1190         req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
1191         OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ, ep->stid));
1192         req->local_port = ep->com.local_addr.sin_port;
1193         req->local_ip = ep->com.local_addr.sin_addr.s_addr;
1194         req->peer_port = 0;
1195         req->peer_ip = 0;
1196         req->peer_netmask = 0;
1197         req->opt0h = htonl(F_DELACK | F_TCAM_BYPASS);
1198         req->opt0l = htonl(V_RCV_BUFSIZ(rcv_win>>10));
1199         req->opt1 = htonl(V_CONN_POLICY(CPL_CONN_POLICY_ASK));
1200
1201         skb->priority = 1;
1202         return iwch_cxgb3_ofld_send(ep->com.tdev, skb);
1203 }
1204
1205 static int pass_open_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1206 {
1207         struct iwch_listen_ep *ep = ctx;
1208         struct cpl_pass_open_rpl *rpl = cplhdr(skb);
1209
1210         PDBG("%s ep %p status %d error %d\n", __func__, ep,
1211              rpl->status, status2errno(rpl->status));
1212         ep->com.rpl_err = status2errno(rpl->status);
1213         ep->com.rpl_done = 1;
1214         wake_up(&ep->com.waitq);
1215
1216         return CPL_RET_BUF_DONE;
1217 }
1218
1219 static int listen_stop(struct iwch_listen_ep *ep)
1220 {
1221         struct sk_buff *skb;
1222         struct cpl_close_listserv_req *req;
1223
1224         PDBG("%s ep %p\n", __func__, ep);
1225         skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
1226         if (!skb) {
1227                 printk(KERN_ERR MOD "%s - failed to alloc skb\n", __func__);
1228                 return -ENOMEM;
1229         }
1230         req = (struct cpl_close_listserv_req *) skb_put(skb, sizeof(*req));
1231         req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
1232         req->cpu_idx = 0;
1233         OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_LISTSRV_REQ, ep->stid));
1234         skb->priority = 1;
1235         return iwch_cxgb3_ofld_send(ep->com.tdev, skb);
1236 }
1237
1238 static int close_listsrv_rpl(struct t3cdev *tdev, struct sk_buff *skb,
1239                              void *ctx)
1240 {
1241         struct iwch_listen_ep *ep = ctx;
1242         struct cpl_close_listserv_rpl *rpl = cplhdr(skb);
1243
1244         PDBG("%s ep %p\n", __func__, ep);
1245         ep->com.rpl_err = status2errno(rpl->status);
1246         ep->com.rpl_done = 1;
1247         wake_up(&ep->com.waitq);
1248         return CPL_RET_BUF_DONE;
1249 }
1250
1251 static void accept_cr(struct iwch_ep *ep, __be32 peer_ip, struct sk_buff *skb)
1252 {
1253         struct cpl_pass_accept_rpl *rpl;
1254         unsigned int mtu_idx;
1255         u32 opt0h, opt0l, opt2;
1256         int wscale;
1257
1258         PDBG("%s ep %p\n", __func__, ep);
1259         BUG_ON(skb_cloned(skb));
1260         skb_trim(skb, sizeof(*rpl));
1261         skb_get(skb);
1262         mtu_idx = find_best_mtu(T3C_DATA(ep->com.tdev), dst_mtu(ep->dst));
1263         wscale = compute_wscale(rcv_win);
1264         opt0h = V_NAGLE(0) |
1265             V_NO_CONG(nocong) |
1266             V_KEEP_ALIVE(1) |
1267             F_TCAM_BYPASS |
1268             V_WND_SCALE(wscale) |
1269             V_MSS_IDX(mtu_idx) |
1270             V_L2T_IDX(ep->l2t->idx) | V_TX_CHANNEL(ep->l2t->smt_idx);
1271         opt0l = V_TOS((ep->tos >> 2) & M_TOS) | V_RCV_BUFSIZ(rcv_win>>10);
1272         opt2 = F_RX_COALESCE_VALID | V_RX_COALESCE(0) | V_FLAVORS_VALID(1) |
1273                V_CONG_CONTROL_FLAVOR(cong_flavor);
1274
1275         rpl = cplhdr(skb);
1276         rpl->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
1277         OPCODE_TID(rpl) = htonl(MK_OPCODE_TID(CPL_PASS_ACCEPT_RPL, ep->hwtid));
1278         rpl->peer_ip = peer_ip;
1279         rpl->opt0h = htonl(opt0h);
1280         rpl->opt0l_status = htonl(opt0l | CPL_PASS_OPEN_ACCEPT);
1281         rpl->opt2 = htonl(opt2);
1282         rpl->rsvd = rpl->opt2;  /* workaround for HW bug */
1283         skb->priority = CPL_PRIORITY_SETUP;
1284         iwch_l2t_send(ep->com.tdev, skb, ep->l2t);
1285
1286         return;
1287 }
1288
1289 static void reject_cr(struct t3cdev *tdev, u32 hwtid, __be32 peer_ip,
1290                       struct sk_buff *skb)
1291 {
1292         PDBG("%s t3cdev %p tid %u peer_ip %x\n", __func__, tdev, hwtid,
1293              peer_ip);
1294         BUG_ON(skb_cloned(skb));
1295         skb_trim(skb, sizeof(struct cpl_tid_release));
1296         skb_get(skb);
1297
1298         if (tdev->type != T3A)
1299                 release_tid(tdev, hwtid, skb);
1300         else {
1301                 struct cpl_pass_accept_rpl *rpl;
1302
1303                 rpl = cplhdr(skb);
1304                 skb->priority = CPL_PRIORITY_SETUP;
1305                 rpl->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
1306                 OPCODE_TID(rpl) = htonl(MK_OPCODE_TID(CPL_PASS_ACCEPT_RPL,
1307                                                       hwtid));
1308                 rpl->peer_ip = peer_ip;
1309                 rpl->opt0h = htonl(F_TCAM_BYPASS);
1310                 rpl->opt0l_status = htonl(CPL_PASS_OPEN_REJECT);
1311                 rpl->opt2 = 0;
1312                 rpl->rsvd = rpl->opt2;
1313                 iwch_cxgb3_ofld_send(tdev, skb);
1314         }
1315 }
1316
1317 static int pass_accept_req(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1318 {
1319         struct iwch_ep *child_ep, *parent_ep = ctx;
1320         struct cpl_pass_accept_req *req = cplhdr(skb);
1321         unsigned int hwtid = GET_TID(req);
1322         struct dst_entry *dst;
1323         struct l2t_entry *l2t;
1324         struct rtable *rt;
1325         struct iff_mac tim;
1326
1327         PDBG("%s parent ep %p tid %u\n", __func__, parent_ep, hwtid);
1328
1329         if (state_read(&parent_ep->com) != LISTEN) {
1330                 printk(KERN_ERR "%s - listening ep not in LISTEN\n",
1331                        __func__);
1332                 goto reject;
1333         }
1334
1335         /*
1336          * Find the netdev for this connection request.
1337          */
1338         tim.mac_addr = req->dst_mac;
1339         tim.vlan_tag = ntohs(req->vlan_tag);
1340         if (tdev->ctl(tdev, GET_IFF_FROM_MAC, &tim) < 0 || !tim.dev) {
1341                 printk(KERN_ERR "%s bad dst mac %pM\n",
1342                         __func__, req->dst_mac);
1343                 goto reject;
1344         }
1345
1346         /* Find output route */
1347         rt = find_route(tdev,
1348                         req->local_ip,
1349                         req->peer_ip,
1350                         req->local_port,
1351                         req->peer_port, G_PASS_OPEN_TOS(ntohl(req->tos_tid)));
1352         if (!rt) {
1353                 printk(KERN_ERR MOD "%s - failed to find dst entry!\n",
1354                        __func__);
1355                 goto reject;
1356         }
1357         dst = &rt->dst;
1358         l2t = t3_l2t_get(tdev, dst->neighbour, dst->neighbour->dev);
1359         if (!l2t) {
1360                 printk(KERN_ERR MOD "%s - failed to allocate l2t entry!\n",
1361                        __func__);
1362                 dst_release(dst);
1363                 goto reject;
1364         }
1365         child_ep = alloc_ep(sizeof(*child_ep), GFP_KERNEL);
1366         if (!child_ep) {
1367                 printk(KERN_ERR MOD "%s - failed to allocate ep entry!\n",
1368                        __func__);
1369                 l2t_release(L2DATA(tdev), l2t);
1370                 dst_release(dst);
1371                 goto reject;
1372         }
1373         state_set(&child_ep->com, CONNECTING);
1374         child_ep->com.tdev = tdev;
1375         child_ep->com.cm_id = NULL;
1376         child_ep->com.local_addr.sin_family = PF_INET;
1377         child_ep->com.local_addr.sin_port = req->local_port;
1378         child_ep->com.local_addr.sin_addr.s_addr = req->local_ip;
1379         child_ep->com.remote_addr.sin_family = PF_INET;
1380         child_ep->com.remote_addr.sin_port = req->peer_port;
1381         child_ep->com.remote_addr.sin_addr.s_addr = req->peer_ip;
1382         get_ep(&parent_ep->com);
1383         child_ep->parent_ep = parent_ep;
1384         child_ep->tos = G_PASS_OPEN_TOS(ntohl(req->tos_tid));
1385         child_ep->l2t = l2t;
1386         child_ep->dst = dst;
1387         child_ep->hwtid = hwtid;
1388         init_timer(&child_ep->timer);
1389         cxgb3_insert_tid(tdev, &t3c_client, child_ep, hwtid);
1390         accept_cr(child_ep, req->peer_ip, skb);
1391         goto out;
1392 reject:
1393         reject_cr(tdev, hwtid, req->peer_ip, skb);
1394 out:
1395         return CPL_RET_BUF_DONE;
1396 }
1397
1398 static int pass_establish(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1399 {
1400         struct iwch_ep *ep = ctx;
1401         struct cpl_pass_establish *req = cplhdr(skb);
1402
1403         PDBG("%s ep %p\n", __func__, ep);
1404         ep->snd_seq = ntohl(req->snd_isn);
1405         ep->rcv_seq = ntohl(req->rcv_isn);
1406
1407         set_emss(ep, ntohs(req->tcp_opt));
1408
1409         dst_confirm(ep->dst);
1410         state_set(&ep->com, MPA_REQ_WAIT);
1411         start_ep_timer(ep);
1412
1413         return CPL_RET_BUF_DONE;
1414 }
1415
1416 static int peer_close(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1417 {
1418         struct iwch_ep *ep = ctx;
1419         struct iwch_qp_attributes attrs;
1420         unsigned long flags;
1421         int disconnect = 1;
1422         int release = 0;
1423
1424         PDBG("%s ep %p\n", __func__, ep);
1425         dst_confirm(ep->dst);
1426
1427         spin_lock_irqsave(&ep->com.lock, flags);
1428         switch (ep->com.state) {
1429         case MPA_REQ_WAIT:
1430                 __state_set(&ep->com, CLOSING);
1431                 break;
1432         case MPA_REQ_SENT:
1433                 __state_set(&ep->com, CLOSING);
1434                 connect_reply_upcall(ep, -ECONNRESET);
1435                 break;
1436         case MPA_REQ_RCVD:
1437
1438                 /*
1439                  * We're gonna mark this puppy DEAD, but keep
1440                  * the reference on it until the ULP accepts or
1441                  * rejects the CR. Also wake up anyone waiting
1442                  * in rdma connection migration (see iwch_accept_cr()).
1443                  */
1444                 __state_set(&ep->com, CLOSING);
1445                 ep->com.rpl_done = 1;
1446                 ep->com.rpl_err = -ECONNRESET;
1447                 PDBG("waking up ep %p\n", ep);
1448                 wake_up(&ep->com.waitq);
1449                 break;
1450         case MPA_REP_SENT:
1451                 __state_set(&ep->com, CLOSING);
1452                 ep->com.rpl_done = 1;
1453                 ep->com.rpl_err = -ECONNRESET;
1454                 PDBG("waking up ep %p\n", ep);
1455                 wake_up(&ep->com.waitq);
1456                 break;
1457         case FPDU_MODE:
1458                 start_ep_timer(ep);
1459                 __state_set(&ep->com, CLOSING);
1460                 attrs.next_state = IWCH_QP_STATE_CLOSING;
1461                 iwch_modify_qp(ep->com.qp->rhp, ep->com.qp,
1462                                IWCH_QP_ATTR_NEXT_STATE, &attrs, 1);
1463                 peer_close_upcall(ep);
1464                 break;
1465         case ABORTING:
1466                 disconnect = 0;
1467                 break;
1468         case CLOSING:
1469                 __state_set(&ep->com, MORIBUND);
1470                 disconnect = 0;
1471                 break;
1472         case MORIBUND:
1473                 stop_ep_timer(ep);
1474                 if (ep->com.cm_id && ep->com.qp) {
1475                         attrs.next_state = IWCH_QP_STATE_IDLE;
1476                         iwch_modify_qp(ep->com.qp->rhp, ep->com.qp,
1477                                        IWCH_QP_ATTR_NEXT_STATE, &attrs, 1);
1478                 }
1479                 close_complete_upcall(ep);
1480                 __state_set(&ep->com, DEAD);
1481                 release = 1;
1482                 disconnect = 0;
1483                 break;
1484         case DEAD:
1485                 disconnect = 0;
1486                 break;
1487         default:
1488                 BUG_ON(1);
1489         }
1490         spin_unlock_irqrestore(&ep->com.lock, flags);
1491         if (disconnect)
1492                 iwch_ep_disconnect(ep, 0, GFP_KERNEL);
1493         if (release)
1494                 release_ep_resources(ep);
1495         return CPL_RET_BUF_DONE;
1496 }
1497
1498 /*
1499  * Returns whether an ABORT_REQ_RSS message is a negative advice.
1500  */
1501 static int is_neg_adv_abort(unsigned int status)
1502 {
1503         return status == CPL_ERR_RTX_NEG_ADVICE ||
1504                status == CPL_ERR_PERSIST_NEG_ADVICE;
1505 }
1506
1507 static int peer_abort(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1508 {
1509         struct cpl_abort_req_rss *req = cplhdr(skb);
1510         struct iwch_ep *ep = ctx;
1511         struct cpl_abort_rpl *rpl;
1512         struct sk_buff *rpl_skb;
1513         struct iwch_qp_attributes attrs;
1514         int ret;
1515         int release = 0;
1516         unsigned long flags;
1517
1518         if (is_neg_adv_abort(req->status)) {
1519                 PDBG("%s neg_adv_abort ep %p tid %d\n", __func__, ep,
1520                      ep->hwtid);
1521                 t3_l2t_send_event(ep->com.tdev, ep->l2t);
1522                 return CPL_RET_BUF_DONE;
1523         }
1524
1525         /*
1526          * We get 2 peer aborts from the HW.  The first one must
1527          * be ignored except for scribbling that we need one more.
1528          */
1529         if (!test_and_set_bit(PEER_ABORT_IN_PROGRESS, &ep->com.flags)) {
1530                 return CPL_RET_BUF_DONE;
1531         }
1532
1533         spin_lock_irqsave(&ep->com.lock, flags);
1534         PDBG("%s ep %p state %u\n", __func__, ep, ep->com.state);
1535         switch (ep->com.state) {
1536         case CONNECTING:
1537                 break;
1538         case MPA_REQ_WAIT:
1539                 stop_ep_timer(ep);
1540                 break;
1541         case MPA_REQ_SENT:
1542                 stop_ep_timer(ep);
1543                 connect_reply_upcall(ep, -ECONNRESET);
1544                 break;
1545         case MPA_REP_SENT:
1546                 ep->com.rpl_done = 1;
1547                 ep->com.rpl_err = -ECONNRESET;
1548                 PDBG("waking up ep %p\n", ep);
1549                 wake_up(&ep->com.waitq);
1550                 break;
1551         case MPA_REQ_RCVD:
1552
1553                 /*
1554                  * We're gonna mark this puppy DEAD, but keep
1555                  * the reference on it until the ULP accepts or
1556                  * rejects the CR. Also wake up anyone waiting
1557                  * in rdma connection migration (see iwch_accept_cr()).
1558                  */
1559                 ep->com.rpl_done = 1;
1560                 ep->com.rpl_err = -ECONNRESET;
1561                 PDBG("waking up ep %p\n", ep);
1562                 wake_up(&ep->com.waitq);
1563                 break;
1564         case MORIBUND:
1565         case CLOSING:
1566                 stop_ep_timer(ep);
1567                 /*FALLTHROUGH*/
1568         case FPDU_MODE:
1569                 if (ep->com.cm_id && ep->com.qp) {
1570                         attrs.next_state = IWCH_QP_STATE_ERROR;
1571                         ret = iwch_modify_qp(ep->com.qp->rhp,
1572                                      ep->com.qp, IWCH_QP_ATTR_NEXT_STATE,
1573                                      &attrs, 1);
1574                         if (ret)
1575                                 printk(KERN_ERR MOD
1576                                        "%s - qp <- error failed!\n",
1577                                        __func__);
1578                 }
1579                 peer_abort_upcall(ep);
1580                 break;
1581         case ABORTING:
1582                 break;
1583         case DEAD:
1584                 PDBG("%s PEER_ABORT IN DEAD STATE!!!!\n", __func__);
1585                 spin_unlock_irqrestore(&ep->com.lock, flags);
1586                 return CPL_RET_BUF_DONE;
1587         default:
1588                 BUG_ON(1);
1589                 break;
1590         }
1591         dst_confirm(ep->dst);
1592         if (ep->com.state != ABORTING) {
1593                 __state_set(&ep->com, DEAD);
1594                 release = 1;
1595         }
1596         spin_unlock_irqrestore(&ep->com.lock, flags);
1597
1598         rpl_skb = get_skb(skb, sizeof(*rpl), GFP_KERNEL);
1599         if (!rpl_skb) {
1600                 printk(KERN_ERR MOD "%s - cannot allocate skb!\n",
1601                        __func__);
1602                 release = 1;
1603                 goto out;
1604         }
1605         rpl_skb->priority = CPL_PRIORITY_DATA;
1606         rpl = (struct cpl_abort_rpl *) skb_put(rpl_skb, sizeof(*rpl));
1607         rpl->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_HOST_ABORT_CON_RPL));
1608         rpl->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
1609         OPCODE_TID(rpl) = htonl(MK_OPCODE_TID(CPL_ABORT_RPL, ep->hwtid));
1610         rpl->cmd = CPL_ABORT_NO_RST;
1611         iwch_cxgb3_ofld_send(ep->com.tdev, rpl_skb);
1612 out:
1613         if (release)
1614                 release_ep_resources(ep);
1615         return CPL_RET_BUF_DONE;
1616 }
1617
1618 static int close_con_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1619 {
1620         struct iwch_ep *ep = ctx;
1621         struct iwch_qp_attributes attrs;
1622         unsigned long flags;
1623         int release = 0;
1624
1625         PDBG("%s ep %p\n", __func__, ep);
1626         BUG_ON(!ep);
1627
1628         /* The cm_id may be null if we failed to connect */
1629         spin_lock_irqsave(&ep->com.lock, flags);
1630         switch (ep->com.state) {
1631         case CLOSING:
1632                 __state_set(&ep->com, MORIBUND);
1633                 break;
1634         case MORIBUND:
1635                 stop_ep_timer(ep);
1636                 if ((ep->com.cm_id) && (ep->com.qp)) {
1637                         attrs.next_state = IWCH_QP_STATE_IDLE;
1638                         iwch_modify_qp(ep->com.qp->rhp,
1639                                              ep->com.qp,
1640                                              IWCH_QP_ATTR_NEXT_STATE,
1641                                              &attrs, 1);
1642                 }
1643                 close_complete_upcall(ep);
1644                 __state_set(&ep->com, DEAD);
1645                 release = 1;
1646                 break;
1647         case ABORTING:
1648         case DEAD:
1649                 break;
1650         default:
1651                 BUG_ON(1);
1652                 break;
1653         }
1654         spin_unlock_irqrestore(&ep->com.lock, flags);
1655         if (release)
1656                 release_ep_resources(ep);
1657         return CPL_RET_BUF_DONE;
1658 }
1659
1660 /*
1661  * T3A does 3 things when a TERM is received:
1662  * 1) send up a CPL_RDMA_TERMINATE message with the TERM packet
1663  * 2) generate an async event on the QP with the TERMINATE opcode
1664  * 3) post a TERMINATE opcde cqe into the associated CQ.
1665  *
1666  * For (1), we save the message in the qp for later consumer consumption.
1667  * For (2), we move the QP into TERMINATE, post a QP event and disconnect.
1668  * For (3), we toss the CQE in cxio_poll_cq().
1669  *
1670  * terminate() handles case (1)...
1671  */
1672 static int terminate(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1673 {
1674         struct iwch_ep *ep = ctx;
1675
1676         if (state_read(&ep->com) != FPDU_MODE)
1677                 return CPL_RET_BUF_DONE;
1678
1679         PDBG("%s ep %p\n", __func__, ep);
1680         skb_pull(skb, sizeof(struct cpl_rdma_terminate));
1681         PDBG("%s saving %d bytes of term msg\n", __func__, skb->len);
1682         skb_copy_from_linear_data(skb, ep->com.qp->attr.terminate_buffer,
1683                                   skb->len);
1684         ep->com.qp->attr.terminate_msg_len = skb->len;
1685         ep->com.qp->attr.is_terminate_local = 0;
1686         return CPL_RET_BUF_DONE;
1687 }
1688
1689 static int ec_status(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1690 {
1691         struct cpl_rdma_ec_status *rep = cplhdr(skb);
1692         struct iwch_ep *ep = ctx;
1693
1694         PDBG("%s ep %p tid %u status %d\n", __func__, ep, ep->hwtid,
1695              rep->status);
1696         if (rep->status) {
1697                 struct iwch_qp_attributes attrs;
1698
1699                 printk(KERN_ERR MOD "%s BAD CLOSE - Aborting tid %u\n",
1700                        __func__, ep->hwtid);
1701                 stop_ep_timer(ep);
1702                 attrs.next_state = IWCH_QP_STATE_ERROR;
1703                 iwch_modify_qp(ep->com.qp->rhp,
1704                                ep->com.qp, IWCH_QP_ATTR_NEXT_STATE,
1705                                &attrs, 1);
1706                 abort_connection(ep, NULL, GFP_KERNEL);
1707         }
1708         return CPL_RET_BUF_DONE;
1709 }
1710
1711 static void ep_timeout(unsigned long arg)
1712 {
1713         struct iwch_ep *ep = (struct iwch_ep *)arg;
1714         struct iwch_qp_attributes attrs;
1715         unsigned long flags;
1716         int abort = 1;
1717
1718         spin_lock_irqsave(&ep->com.lock, flags);
1719         PDBG("%s ep %p tid %u state %d\n", __func__, ep, ep->hwtid,
1720              ep->com.state);
1721         switch (ep->com.state) {
1722         case MPA_REQ_SENT:
1723                 __state_set(&ep->com, ABORTING);
1724                 connect_reply_upcall(ep, -ETIMEDOUT);
1725                 break;
1726         case MPA_REQ_WAIT:
1727                 __state_set(&ep->com, ABORTING);
1728                 break;
1729         case CLOSING:
1730         case MORIBUND:
1731                 if (ep->com.cm_id && ep->com.qp) {
1732                         attrs.next_state = IWCH_QP_STATE_ERROR;
1733                         iwch_modify_qp(ep->com.qp->rhp,
1734                                      ep->com.qp, IWCH_QP_ATTR_NEXT_STATE,
1735                                      &attrs, 1);
1736                 }
1737                 __state_set(&ep->com, ABORTING);
1738                 break;
1739         default:
1740                 printk(KERN_ERR "%s unexpected state ep %p state %u\n",
1741                         __func__, ep, ep->com.state);
1742                 WARN_ON(1);
1743                 abort = 0;
1744         }
1745         spin_unlock_irqrestore(&ep->com.lock, flags);
1746         if (abort)
1747                 abort_connection(ep, NULL, GFP_ATOMIC);
1748         put_ep(&ep->com);
1749 }
1750
1751 int iwch_reject_cr(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
1752 {
1753         int err;
1754         struct iwch_ep *ep = to_ep(cm_id);
1755         PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1756
1757         if (state_read(&ep->com) == DEAD) {
1758                 put_ep(&ep->com);
1759                 return -ECONNRESET;
1760         }
1761         BUG_ON(state_read(&ep->com) != MPA_REQ_RCVD);
1762         if (mpa_rev == 0)
1763                 abort_connection(ep, NULL, GFP_KERNEL);
1764         else {
1765                 err = send_mpa_reject(ep, pdata, pdata_len);
1766                 err = iwch_ep_disconnect(ep, 0, GFP_KERNEL);
1767         }
1768         put_ep(&ep->com);
1769         return 0;
1770 }
1771
1772 int iwch_accept_cr(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
1773 {
1774         int err;
1775         struct iwch_qp_attributes attrs;
1776         enum iwch_qp_attr_mask mask;
1777         struct iwch_ep *ep = to_ep(cm_id);
1778         struct iwch_dev *h = to_iwch_dev(cm_id->device);
1779         struct iwch_qp *qp = get_qhp(h, conn_param->qpn);
1780
1781         PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1782         if (state_read(&ep->com) == DEAD) {
1783                 err = -ECONNRESET;
1784                 goto err;
1785         }
1786
1787         BUG_ON(state_read(&ep->com) != MPA_REQ_RCVD);
1788         BUG_ON(!qp);
1789
1790         if ((conn_param->ord > qp->rhp->attr.max_rdma_read_qp_depth) ||
1791             (conn_param->ird > qp->rhp->attr.max_rdma_reads_per_qp)) {
1792                 abort_connection(ep, NULL, GFP_KERNEL);
1793                 err = -EINVAL;
1794                 goto err;
1795         }
1796
1797         cm_id->add_ref(cm_id);
1798         ep->com.cm_id = cm_id;
1799         ep->com.qp = qp;
1800
1801         ep->ird = conn_param->ird;
1802         ep->ord = conn_param->ord;
1803
1804         if (peer2peer && ep->ird == 0)
1805                 ep->ird = 1;
1806
1807         PDBG("%s %d ird %d ord %d\n", __func__, __LINE__, ep->ird, ep->ord);
1808
1809         /* bind QP to EP and move to RTS */
1810         attrs.mpa_attr = ep->mpa_attr;
1811         attrs.max_ird = ep->ird;
1812         attrs.max_ord = ep->ord;
1813         attrs.llp_stream_handle = ep;
1814         attrs.next_state = IWCH_QP_STATE_RTS;
1815
1816         /* bind QP and TID with INIT_WR */
1817         mask = IWCH_QP_ATTR_NEXT_STATE |
1818                              IWCH_QP_ATTR_LLP_STREAM_HANDLE |
1819                              IWCH_QP_ATTR_MPA_ATTR |
1820                              IWCH_QP_ATTR_MAX_IRD |
1821                              IWCH_QP_ATTR_MAX_ORD;
1822
1823         err = iwch_modify_qp(ep->com.qp->rhp,
1824                              ep->com.qp, mask, &attrs, 1);
1825         if (err)
1826                 goto err1;
1827
1828         /* if needed, wait for wr_ack */
1829         if (iwch_rqes_posted(qp)) {
1830                 wait_event(ep->com.waitq, ep->com.rpl_done);
1831                 err = ep->com.rpl_err;
1832                 if (err)
1833                         goto err1;
1834         }
1835
1836         err = send_mpa_reply(ep, conn_param->private_data,
1837                              conn_param->private_data_len);
1838         if (err)
1839                 goto err1;
1840
1841
1842         state_set(&ep->com, FPDU_MODE);
1843         established_upcall(ep);
1844         put_ep(&ep->com);
1845         return 0;
1846 err1:
1847         ep->com.cm_id = NULL;
1848         ep->com.qp = NULL;
1849         cm_id->rem_ref(cm_id);
1850 err:
1851         put_ep(&ep->com);
1852         return err;
1853 }
1854
1855 static int is_loopback_dst(struct iw_cm_id *cm_id)
1856 {
1857         struct net_device *dev;
1858
1859         dev = ip_dev_find(&init_net, cm_id->remote_addr.sin_addr.s_addr);
1860         if (!dev)
1861                 return 0;
1862         dev_put(dev);
1863         return 1;
1864 }
1865
1866 int iwch_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
1867 {
1868         int err = 0;
1869         struct iwch_dev *h = to_iwch_dev(cm_id->device);
1870         struct iwch_ep *ep;
1871         struct rtable *rt;
1872
1873         if (is_loopback_dst(cm_id)) {
1874                 err = -ENOSYS;
1875                 goto out;
1876         }
1877
1878         ep = alloc_ep(sizeof(*ep), GFP_KERNEL);
1879         if (!ep) {
1880                 printk(KERN_ERR MOD "%s - cannot alloc ep.\n", __func__);
1881                 err = -ENOMEM;
1882                 goto out;
1883         }
1884         init_timer(&ep->timer);
1885         ep->plen = conn_param->private_data_len;
1886         if (ep->plen)
1887                 memcpy(ep->mpa_pkt + sizeof(struct mpa_message),
1888                        conn_param->private_data, ep->plen);
1889         ep->ird = conn_param->ird;
1890         ep->ord = conn_param->ord;
1891
1892         if (peer2peer && ep->ord == 0)
1893                 ep->ord = 1;
1894
1895         ep->com.tdev = h->rdev.t3cdev_p;
1896
1897         cm_id->add_ref(cm_id);
1898         ep->com.cm_id = cm_id;
1899         ep->com.qp = get_qhp(h, conn_param->qpn);
1900         BUG_ON(!ep->com.qp);
1901         PDBG("%s qpn 0x%x qp %p cm_id %p\n", __func__, conn_param->qpn,
1902              ep->com.qp, cm_id);
1903
1904         /*
1905          * Allocate an active TID to initiate a TCP connection.
1906          */
1907         ep->atid = cxgb3_alloc_atid(h->rdev.t3cdev_p, &t3c_client, ep);
1908         if (ep->atid == -1) {
1909                 printk(KERN_ERR MOD "%s - cannot alloc atid.\n", __func__);
1910                 err = -ENOMEM;
1911                 goto fail2;
1912         }
1913
1914         /* find a route */
1915         rt = find_route(h->rdev.t3cdev_p,
1916                         cm_id->local_addr.sin_addr.s_addr,
1917                         cm_id->remote_addr.sin_addr.s_addr,
1918                         cm_id->local_addr.sin_port,
1919                         cm_id->remote_addr.sin_port, IPTOS_LOWDELAY);
1920         if (!rt) {
1921                 printk(KERN_ERR MOD "%s - cannot find route.\n", __func__);
1922                 err = -EHOSTUNREACH;
1923                 goto fail3;
1924         }
1925         ep->dst = &rt->dst;
1926
1927         /* get a l2t entry */
1928         ep->l2t = t3_l2t_get(ep->com.tdev, ep->dst->neighbour,
1929                              ep->dst->neighbour->dev);
1930         if (!ep->l2t) {
1931                 printk(KERN_ERR MOD "%s - cannot alloc l2e.\n", __func__);
1932                 err = -ENOMEM;
1933                 goto fail4;
1934         }
1935
1936         state_set(&ep->com, CONNECTING);
1937         ep->tos = IPTOS_LOWDELAY;
1938         ep->com.local_addr = cm_id->local_addr;
1939         ep->com.remote_addr = cm_id->remote_addr;
1940
1941         /* send connect request to rnic */
1942         err = send_connect(ep);
1943         if (!err)
1944                 goto out;
1945
1946         l2t_release(L2DATA(h->rdev.t3cdev_p), ep->l2t);
1947 fail4:
1948         dst_release(ep->dst);
1949 fail3:
1950         cxgb3_free_atid(ep->com.tdev, ep->atid);
1951 fail2:
1952         cm_id->rem_ref(cm_id);
1953         put_ep(&ep->com);
1954 out:
1955         return err;
1956 }
1957
1958 int iwch_create_listen(struct iw_cm_id *cm_id, int backlog)
1959 {
1960         int err = 0;
1961         struct iwch_dev *h = to_iwch_dev(cm_id->device);
1962         struct iwch_listen_ep *ep;
1963
1964
1965         might_sleep();
1966
1967         ep = alloc_ep(sizeof(*ep), GFP_KERNEL);
1968         if (!ep) {
1969                 printk(KERN_ERR MOD "%s - cannot alloc ep.\n", __func__);
1970                 err = -ENOMEM;
1971                 goto fail1;
1972         }
1973         PDBG("%s ep %p\n", __func__, ep);
1974         ep->com.tdev = h->rdev.t3cdev_p;
1975         cm_id->add_ref(cm_id);
1976         ep->com.cm_id = cm_id;
1977         ep->backlog = backlog;
1978         ep->com.local_addr = cm_id->local_addr;
1979
1980         /*
1981          * Allocate a server TID.
1982          */
1983         ep->stid = cxgb3_alloc_stid(h->rdev.t3cdev_p, &t3c_client, ep);
1984         if (ep->stid == -1) {
1985                 printk(KERN_ERR MOD "%s - cannot alloc atid.\n", __func__);
1986                 err = -ENOMEM;
1987                 goto fail2;
1988         }
1989
1990         state_set(&ep->com, LISTEN);
1991         err = listen_start(ep);
1992         if (err)
1993                 goto fail3;
1994
1995         /* wait for pass_open_rpl */
1996         wait_event(ep->com.waitq, ep->com.rpl_done);
1997         err = ep->com.rpl_err;
1998         if (!err) {
1999                 cm_id->provider_data = ep;
2000                 goto out;
2001         }
2002 fail3:
2003         cxgb3_free_stid(ep->com.tdev, ep->stid);
2004 fail2:
2005         cm_id->rem_ref(cm_id);
2006         put_ep(&ep->com);
2007 fail1:
2008 out:
2009         return err;
2010 }
2011
2012 int iwch_destroy_listen(struct iw_cm_id *cm_id)
2013 {
2014         int err;
2015         struct iwch_listen_ep *ep = to_listen_ep(cm_id);
2016
2017         PDBG("%s ep %p\n", __func__, ep);
2018
2019         might_sleep();
2020         state_set(&ep->com, DEAD);
2021         ep->com.rpl_done = 0;
2022         ep->com.rpl_err = 0;
2023         err = listen_stop(ep);
2024         if (err)
2025                 goto done;
2026         wait_event(ep->com.waitq, ep->com.rpl_done);
2027         cxgb3_free_stid(ep->com.tdev, ep->stid);
2028 done:
2029         err = ep->com.rpl_err;
2030         cm_id->rem_ref(cm_id);
2031         put_ep(&ep->com);
2032         return err;
2033 }
2034
2035 int iwch_ep_disconnect(struct iwch_ep *ep, int abrupt, gfp_t gfp)
2036 {
2037         int ret=0;
2038         unsigned long flags;
2039         int close = 0;
2040         int fatal = 0;
2041         struct t3cdev *tdev;
2042         struct cxio_rdev *rdev;
2043
2044         spin_lock_irqsave(&ep->com.lock, flags);
2045
2046         PDBG("%s ep %p state %s, abrupt %d\n", __func__, ep,
2047              states[ep->com.state], abrupt);
2048
2049         tdev = (struct t3cdev *)ep->com.tdev;
2050         rdev = (struct cxio_rdev *)tdev->ulp;
2051         if (cxio_fatal_error(rdev)) {
2052                 fatal = 1;
2053                 close_complete_upcall(ep);
2054                 ep->com.state = DEAD;
2055         }
2056         switch (ep->com.state) {
2057         case MPA_REQ_WAIT:
2058         case MPA_REQ_SENT:
2059         case MPA_REQ_RCVD:
2060         case MPA_REP_SENT:
2061         case FPDU_MODE:
2062                 close = 1;
2063                 if (abrupt)
2064                         ep->com.state = ABORTING;
2065                 else {
2066                         ep->com.state = CLOSING;
2067                         start_ep_timer(ep);
2068                 }
2069                 set_bit(CLOSE_SENT, &ep->com.flags);
2070                 break;
2071         case CLOSING:
2072                 if (!test_and_set_bit(CLOSE_SENT, &ep->com.flags)) {
2073                         close = 1;
2074                         if (abrupt) {
2075                                 stop_ep_timer(ep);
2076                                 ep->com.state = ABORTING;
2077                         } else
2078                                 ep->com.state = MORIBUND;
2079                 }
2080                 break;
2081         case MORIBUND:
2082         case ABORTING:
2083         case DEAD:
2084                 PDBG("%s ignoring disconnect ep %p state %u\n",
2085                      __func__, ep, ep->com.state);
2086                 break;
2087         default:
2088                 BUG();
2089                 break;
2090         }
2091
2092         spin_unlock_irqrestore(&ep->com.lock, flags);
2093         if (close) {
2094                 if (abrupt)
2095                         ret = send_abort(ep, NULL, gfp);
2096                 else
2097                         ret = send_halfclose(ep, gfp);
2098                 if (ret)
2099                         fatal = 1;
2100         }
2101         if (fatal)
2102                 release_ep_resources(ep);
2103         return ret;
2104 }
2105
2106 int iwch_ep_redirect(void *ctx, struct dst_entry *old, struct dst_entry *new,
2107                      struct l2t_entry *l2t)
2108 {
2109         struct iwch_ep *ep = ctx;
2110
2111         if (ep->dst != old)
2112                 return 0;
2113
2114         PDBG("%s ep %p redirect to dst %p l2t %p\n", __func__, ep, new,
2115              l2t);
2116         dst_hold(new);
2117         l2t_release(L2DATA(ep->com.tdev), ep->l2t);
2118         ep->l2t = l2t;
2119         dst_release(old);
2120         ep->dst = new;
2121         return 1;
2122 }
2123
2124 /*
2125  * All the CM events are handled on a work queue to have a safe context.
2126  * These are the real handlers that are called from the work queue.
2127  */
2128 static const cxgb3_cpl_handler_func work_handlers[NUM_CPL_CMDS] = {
2129         [CPL_ACT_ESTABLISH]     = act_establish,
2130         [CPL_ACT_OPEN_RPL]      = act_open_rpl,
2131         [CPL_RX_DATA]           = rx_data,
2132         [CPL_TX_DMA_ACK]        = tx_ack,
2133         [CPL_ABORT_RPL_RSS]     = abort_rpl,
2134         [CPL_ABORT_RPL]         = abort_rpl,
2135         [CPL_PASS_OPEN_RPL]     = pass_open_rpl,
2136         [CPL_CLOSE_LISTSRV_RPL] = close_listsrv_rpl,
2137         [CPL_PASS_ACCEPT_REQ]   = pass_accept_req,
2138         [CPL_PASS_ESTABLISH]    = pass_establish,
2139         [CPL_PEER_CLOSE]        = peer_close,
2140         [CPL_ABORT_REQ_RSS]     = peer_abort,
2141         [CPL_CLOSE_CON_RPL]     = close_con_rpl,
2142         [CPL_RDMA_TERMINATE]    = terminate,
2143         [CPL_RDMA_EC_STATUS]    = ec_status,
2144 };
2145
2146 static void process_work(struct work_struct *work)
2147 {
2148         struct sk_buff *skb = NULL;
2149         void *ep;
2150         struct t3cdev *tdev;
2151         int ret;
2152
2153         while ((skb = skb_dequeue(&rxq))) {
2154                 ep = *((void **) (skb->cb));
2155                 tdev = *((struct t3cdev **) (skb->cb + sizeof(void *)));
2156                 ret = work_handlers[G_OPCODE(ntohl((__force __be32)skb->csum))](tdev, skb, ep);
2157                 if (ret & CPL_RET_BUF_DONE)
2158                         kfree_skb(skb);
2159
2160                 /*
2161                  * ep was referenced in sched(), and is freed here.
2162                  */
2163                 put_ep((struct iwch_ep_common *)ep);
2164         }
2165 }
2166
2167 static DECLARE_WORK(skb_work, process_work);
2168
2169 static int sched(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
2170 {
2171         struct iwch_ep_common *epc = ctx;
2172
2173         get_ep(epc);
2174
2175         /*
2176          * Save ctx and tdev in the skb->cb area.
2177          */
2178         *((void **) skb->cb) = ctx;
2179         *((struct t3cdev **) (skb->cb + sizeof(void *))) = tdev;
2180
2181         /*
2182          * Queue the skb and schedule the worker thread.
2183          */
2184         skb_queue_tail(&rxq, skb);
2185         queue_work(workq, &skb_work);
2186         return 0;
2187 }
2188
2189 static int set_tcb_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
2190 {
2191         struct cpl_set_tcb_rpl *rpl = cplhdr(skb);
2192
2193         if (rpl->status != CPL_ERR_NONE) {
2194                 printk(KERN_ERR MOD "Unexpected SET_TCB_RPL status %u "
2195                        "for tid %u\n", rpl->status, GET_TID(rpl));
2196         }
2197         return CPL_RET_BUF_DONE;
2198 }
2199
2200 /*
2201  * All upcalls from the T3 Core go to sched() to schedule the
2202  * processing on a work queue.
2203  */
2204 cxgb3_cpl_handler_func t3c_handlers[NUM_CPL_CMDS] = {
2205         [CPL_ACT_ESTABLISH]     = sched,
2206         [CPL_ACT_OPEN_RPL]      = sched,
2207         [CPL_RX_DATA]           = sched,
2208         [CPL_TX_DMA_ACK]        = sched,
2209         [CPL_ABORT_RPL_RSS]     = sched,
2210         [CPL_ABORT_RPL]         = sched,
2211         [CPL_PASS_OPEN_RPL]     = sched,
2212         [CPL_CLOSE_LISTSRV_RPL] = sched,
2213         [CPL_PASS_ACCEPT_REQ]   = sched,
2214         [CPL_PASS_ESTABLISH]    = sched,
2215         [CPL_PEER_CLOSE]        = sched,
2216         [CPL_CLOSE_CON_RPL]     = sched,
2217         [CPL_ABORT_REQ_RSS]     = sched,
2218         [CPL_RDMA_TERMINATE]    = sched,
2219         [CPL_RDMA_EC_STATUS]    = sched,
2220         [CPL_SET_TCB_RPL]       = set_tcb_rpl,
2221 };
2222
2223 int __init iwch_cm_init(void)
2224 {
2225         skb_queue_head_init(&rxq);
2226
2227         workq = create_singlethread_workqueue("iw_cxgb3");
2228         if (!workq)
2229                 return -ENOMEM;
2230
2231         return 0;
2232 }
2233
2234 void __exit iwch_cm_term(void)
2235 {
2236         flush_workqueue(workq);
2237         destroy_workqueue(workq);
2238 }