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