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