228721f23ce44663324f64c3adcac004defbf65f
[safe/jmp/linux-2.6] / 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
39 #include <net/neighbour.h>
40 #include <net/netevent.h>
41 #include <net/route.h>
42
43 #include "tcb.h"
44 #include "cxgb3_offload.h"
45 #include "iwch.h"
46 #include "iwch_provider.h"
47 #include "iwch_cm.h"
48
49 static char *states[] = {
50         "idle",
51         "listen",
52         "connecting",
53         "mpa_wait_req",
54         "mpa_req_sent",
55         "mpa_req_rcvd",
56         "mpa_rep_sent",
57         "fpdu_mode",
58         "aborting",
59         "closing",
60         "moribund",
61         "dead",
62         NULL,
63 };
64
65 static int ep_timeout_secs = 10;
66 module_param(ep_timeout_secs, int, 0444);
67 MODULE_PARM_DESC(ep_timeout_secs, "CM Endpoint operation timeout "
68                                    "in seconds (default=10)");
69
70 static int mpa_rev = 1;
71 module_param(mpa_rev, int, 0444);
72 MODULE_PARM_DESC(mpa_rev, "MPA Revision, 0 supports amso1100, "
73                  "1 is spec compliant. (default=1)");
74
75 static int markers_enabled = 0;
76 module_param(markers_enabled, int, 0444);
77 MODULE_PARM_DESC(markers_enabled, "Enable MPA MARKERS (default(0)=disabled)");
78
79 static int crc_enabled = 1;
80 module_param(crc_enabled, int, 0444);
81 MODULE_PARM_DESC(crc_enabled, "Enable MPA CRC (default(1)=enabled)");
82
83 static int rcv_win = 256 * 1024;
84 module_param(rcv_win, int, 0444);
85 MODULE_PARM_DESC(rcv_win, "TCP receive window in bytes (default=256)");
86
87 static int snd_win = 32 * 1024;
88 module_param(snd_win, int, 0444);
89 MODULE_PARM_DESC(snd_win, "TCP send window in bytes (default=32KB)");
90
91 static unsigned int nocong = 0;
92 module_param(nocong, uint, 0444);
93 MODULE_PARM_DESC(nocong, "Turn off congestion control (default=0)");
94
95 static unsigned int cong_flavor = 1;
96 module_param(cong_flavor, uint, 0444);
97 MODULE_PARM_DESC(cong_flavor, "TCP Congestion control flavor (default=1)");
98
99 static void process_work(struct work_struct *work);
100 static struct workqueue_struct *workq;
101 static DECLARE_WORK(skb_work, process_work);
102
103 static struct sk_buff_head rxq;
104 static cxgb3_cpl_handler_func work_handlers[NUM_CPL_CMDS];
105
106 static struct sk_buff *get_skb(struct sk_buff *skb, int len, gfp_t gfp);
107 static void ep_timeout(unsigned long arg);
108 static void connect_reply_upcall(struct iwch_ep *ep, int status);
109
110 static void start_ep_timer(struct iwch_ep *ep)
111 {
112         PDBG("%s ep %p\n", __FUNCTION__, ep);
113         if (timer_pending(&ep->timer)) {
114                 PDBG("%s stopped / restarted timer ep %p\n", __FUNCTION__, ep);
115                 del_timer_sync(&ep->timer);
116         } else
117                 get_ep(&ep->com);
118         ep->timer.expires = jiffies + ep_timeout_secs * HZ;
119         ep->timer.data = (unsigned long)ep;
120         ep->timer.function = ep_timeout;
121         add_timer(&ep->timer);
122 }
123
124 static void stop_ep_timer(struct iwch_ep *ep)
125 {
126         PDBG("%s ep %p\n", __FUNCTION__, ep);
127         del_timer_sync(&ep->timer);
128         put_ep(&ep->com);
129 }
130
131 static void release_tid(struct t3cdev *tdev, u32 hwtid, struct sk_buff *skb)
132 {
133         struct cpl_tid_release *req;
134
135         skb = get_skb(skb, sizeof *req, GFP_KERNEL);
136         if (!skb)
137                 return;
138         req = (struct cpl_tid_release *) skb_put(skb, sizeof(*req));
139         req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
140         OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_TID_RELEASE, hwtid));
141         skb->priority = CPL_PRIORITY_SETUP;
142         tdev->send(tdev, skb);
143         return;
144 }
145
146 int iwch_quiesce_tid(struct iwch_ep *ep)
147 {
148         struct cpl_set_tcb_field *req;
149         struct sk_buff *skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
150
151         if (!skb)
152                 return -ENOMEM;
153         req = (struct cpl_set_tcb_field *) skb_put(skb, sizeof(*req));
154         req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
155         req->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
156         OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, ep->hwtid));
157         req->reply = 0;
158         req->cpu_idx = 0;
159         req->word = htons(W_TCB_RX_QUIESCE);
160         req->mask = cpu_to_be64(1ULL << S_TCB_RX_QUIESCE);
161         req->val = cpu_to_be64(1 << S_TCB_RX_QUIESCE);
162
163         skb->priority = CPL_PRIORITY_DATA;
164         ep->com.tdev->send(ep->com.tdev, skb);
165         return 0;
166 }
167
168 int iwch_resume_tid(struct iwch_ep *ep)
169 {
170         struct cpl_set_tcb_field *req;
171         struct sk_buff *skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
172
173         if (!skb)
174                 return -ENOMEM;
175         req = (struct cpl_set_tcb_field *) skb_put(skb, sizeof(*req));
176         req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
177         req->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
178         OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, ep->hwtid));
179         req->reply = 0;
180         req->cpu_idx = 0;
181         req->word = htons(W_TCB_RX_QUIESCE);
182         req->mask = cpu_to_be64(1ULL << S_TCB_RX_QUIESCE);
183         req->val = 0;
184
185         skb->priority = CPL_PRIORITY_DATA;
186         ep->com.tdev->send(ep->com.tdev, skb);
187         return 0;
188 }
189
190 static void set_emss(struct iwch_ep *ep, u16 opt)
191 {
192         PDBG("%s ep %p opt %u\n", __FUNCTION__, ep, opt);
193         ep->emss = T3C_DATA(ep->com.tdev)->mtus[G_TCPOPT_MSS(opt)] - 40;
194         if (G_TCPOPT_TSTAMP(opt))
195                 ep->emss -= 12;
196         if (ep->emss < 128)
197                 ep->emss = 128;
198         PDBG("emss=%d\n", ep->emss);
199 }
200
201 static enum iwch_ep_state state_read(struct iwch_ep_common *epc)
202 {
203         unsigned long flags;
204         enum iwch_ep_state state;
205
206         spin_lock_irqsave(&epc->lock, flags);
207         state = epc->state;
208         spin_unlock_irqrestore(&epc->lock, flags);
209         return state;
210 }
211
212 static void __state_set(struct iwch_ep_common *epc, enum iwch_ep_state new)
213 {
214         epc->state = new;
215 }
216
217 static void state_set(struct iwch_ep_common *epc, enum iwch_ep_state new)
218 {
219         unsigned long flags;
220
221         spin_lock_irqsave(&epc->lock, flags);
222         PDBG("%s - %s -> %s\n", __FUNCTION__, states[epc->state], states[new]);
223         __state_set(epc, new);
224         spin_unlock_irqrestore(&epc->lock, flags);
225         return;
226 }
227
228 static void *alloc_ep(int size, gfp_t gfp)
229 {
230         struct iwch_ep_common *epc;
231
232         epc = kmalloc(size, gfp);
233         if (epc) {
234                 memset(epc, 0, size);
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(&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         ep->com.tdev->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 == T3B && 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         ep->com.tdev->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         ep->com.tdev->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 == T3B)
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                 tdev->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         ep->com.tdev->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         abort_connection(ep, NULL, GFP_KERNEL);
1785         put_ep(&ep->com);
1786         return err;
1787 }
1788
1789 int iwch_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
1790 {
1791         int err = 0;
1792         struct iwch_dev *h = to_iwch_dev(cm_id->device);
1793         struct iwch_ep *ep;
1794         struct rtable *rt;
1795
1796         ep = alloc_ep(sizeof(*ep), GFP_KERNEL);
1797         if (!ep) {
1798                 printk(KERN_ERR MOD "%s - cannot alloc ep.\n", __FUNCTION__);
1799                 err = -ENOMEM;
1800                 goto out;
1801         }
1802         init_timer(&ep->timer);
1803         ep->plen = conn_param->private_data_len;
1804         if (ep->plen)
1805                 memcpy(ep->mpa_pkt + sizeof(struct mpa_message),
1806                        conn_param->private_data, ep->plen);
1807         ep->ird = conn_param->ird;
1808         ep->ord = conn_param->ord;
1809         ep->com.tdev = h->rdev.t3cdev_p;
1810
1811         cm_id->add_ref(cm_id);
1812         ep->com.cm_id = cm_id;
1813         ep->com.qp = get_qhp(h, conn_param->qpn);
1814         BUG_ON(!ep->com.qp);
1815         PDBG("%s qpn 0x%x qp %p cm_id %p\n", __FUNCTION__, conn_param->qpn,
1816              ep->com.qp, cm_id);
1817
1818         /*
1819          * Allocate an active TID to initiate a TCP connection.
1820          */
1821         ep->atid = cxgb3_alloc_atid(h->rdev.t3cdev_p, &t3c_client, ep);
1822         if (ep->atid == -1) {
1823                 printk(KERN_ERR MOD "%s - cannot alloc atid.\n", __FUNCTION__);
1824                 err = -ENOMEM;
1825                 goto fail2;
1826         }
1827
1828         /* find a route */
1829         rt = find_route(h->rdev.t3cdev_p,
1830                         cm_id->local_addr.sin_addr.s_addr,
1831                         cm_id->remote_addr.sin_addr.s_addr,
1832                         cm_id->local_addr.sin_port,
1833                         cm_id->remote_addr.sin_port, IPTOS_LOWDELAY);
1834         if (!rt) {
1835                 printk(KERN_ERR MOD "%s - cannot find route.\n", __FUNCTION__);
1836                 err = -EHOSTUNREACH;
1837                 goto fail3;
1838         }
1839         ep->dst = &rt->u.dst;
1840
1841         /* get a l2t entry */
1842         ep->l2t = t3_l2t_get(ep->com.tdev, ep->dst->neighbour,
1843                              ep->dst->neighbour->dev);
1844         if (!ep->l2t) {
1845                 printk(KERN_ERR MOD "%s - cannot alloc l2e.\n", __FUNCTION__);
1846                 err = -ENOMEM;
1847                 goto fail4;
1848         }
1849
1850         state_set(&ep->com, CONNECTING);
1851         ep->tos = IPTOS_LOWDELAY;
1852         ep->com.local_addr = cm_id->local_addr;
1853         ep->com.remote_addr = cm_id->remote_addr;
1854
1855         /* send connect request to rnic */
1856         err = send_connect(ep);
1857         if (!err)
1858                 goto out;
1859
1860         l2t_release(L2DATA(h->rdev.t3cdev_p), ep->l2t);
1861 fail4:
1862         dst_release(ep->dst);
1863 fail3:
1864         cxgb3_free_atid(ep->com.tdev, ep->atid);
1865 fail2:
1866         put_ep(&ep->com);
1867 out:
1868         return err;
1869 }
1870
1871 int iwch_create_listen(struct iw_cm_id *cm_id, int backlog)
1872 {
1873         int err = 0;
1874         struct iwch_dev *h = to_iwch_dev(cm_id->device);
1875         struct iwch_listen_ep *ep;
1876
1877
1878         might_sleep();
1879
1880         ep = alloc_ep(sizeof(*ep), GFP_KERNEL);
1881         if (!ep) {
1882                 printk(KERN_ERR MOD "%s - cannot alloc ep.\n", __FUNCTION__);
1883                 err = -ENOMEM;
1884                 goto fail1;
1885         }
1886         PDBG("%s ep %p\n", __FUNCTION__, ep);
1887         ep->com.tdev = h->rdev.t3cdev_p;
1888         cm_id->add_ref(cm_id);
1889         ep->com.cm_id = cm_id;
1890         ep->backlog = backlog;
1891         ep->com.local_addr = cm_id->local_addr;
1892
1893         /*
1894          * Allocate a server TID.
1895          */
1896         ep->stid = cxgb3_alloc_stid(h->rdev.t3cdev_p, &t3c_client, ep);
1897         if (ep->stid == -1) {
1898                 printk(KERN_ERR MOD "%s - cannot alloc atid.\n", __FUNCTION__);
1899                 err = -ENOMEM;
1900                 goto fail2;
1901         }
1902
1903         state_set(&ep->com, LISTEN);
1904         err = listen_start(ep);
1905         if (err)
1906                 goto fail3;
1907
1908         /* wait for pass_open_rpl */
1909         wait_event(ep->com.waitq, ep->com.rpl_done);
1910         err = ep->com.rpl_err;
1911         if (!err) {
1912                 cm_id->provider_data = ep;
1913                 goto out;
1914         }
1915 fail3:
1916         cxgb3_free_stid(ep->com.tdev, ep->stid);
1917 fail2:
1918         put_ep(&ep->com);
1919 fail1:
1920 out:
1921         return err;
1922 }
1923
1924 int iwch_destroy_listen(struct iw_cm_id *cm_id)
1925 {
1926         int err;
1927         struct iwch_listen_ep *ep = to_listen_ep(cm_id);
1928
1929         PDBG("%s ep %p\n", __FUNCTION__, ep);
1930
1931         might_sleep();
1932         state_set(&ep->com, DEAD);
1933         ep->com.rpl_done = 0;
1934         ep->com.rpl_err = 0;
1935         err = listen_stop(ep);
1936         wait_event(ep->com.waitq, ep->com.rpl_done);
1937         cxgb3_free_stid(ep->com.tdev, ep->stid);
1938         err = ep->com.rpl_err;
1939         cm_id->rem_ref(cm_id);
1940         put_ep(&ep->com);
1941         return err;
1942 }
1943
1944 int iwch_ep_disconnect(struct iwch_ep *ep, int abrupt, gfp_t gfp)
1945 {
1946         int ret=0;
1947         unsigned long flags;
1948         int close = 0;
1949
1950         spin_lock_irqsave(&ep->com.lock, flags);
1951
1952         PDBG("%s ep %p state %s, abrupt %d\n", __FUNCTION__, ep,
1953              states[ep->com.state], abrupt);
1954
1955         if (ep->com.state == DEAD) {
1956                 PDBG("%s already dead ep %p\n", __FUNCTION__, ep);
1957                 goto out;
1958         }
1959
1960         if (abrupt) {
1961                 if (ep->com.state != ABORTING) {
1962                         ep->com.state = ABORTING;
1963                         close = 1;
1964                 }
1965                 goto out;
1966         }
1967
1968         switch (ep->com.state) {
1969         case MPA_REQ_WAIT:
1970         case MPA_REQ_SENT:
1971         case MPA_REQ_RCVD:
1972         case MPA_REP_SENT:
1973         case FPDU_MODE:
1974                 start_ep_timer(ep);
1975                 ep->com.state = CLOSING;
1976                 close = 1;
1977                 break;
1978         case CLOSING:
1979                 ep->com.state = MORIBUND;
1980                 close = 1;
1981                 break;
1982         case MORIBUND:
1983                 break;
1984         default:
1985                 BUG();
1986                 break;
1987         }
1988 out:
1989         spin_unlock_irqrestore(&ep->com.lock, flags);
1990         if (close) {
1991                 if (abrupt)
1992                         ret = send_abort(ep, NULL, gfp);
1993                 else
1994                         ret = send_halfclose(ep, gfp);
1995         }
1996         return ret;
1997 }
1998
1999 int iwch_ep_redirect(void *ctx, struct dst_entry *old, struct dst_entry *new,
2000                      struct l2t_entry *l2t)
2001 {
2002         struct iwch_ep *ep = ctx;
2003
2004         if (ep->dst != old)
2005                 return 0;
2006
2007         PDBG("%s ep %p redirect to dst %p l2t %p\n", __FUNCTION__, ep, new,
2008              l2t);
2009         dst_hold(new);
2010         l2t_release(L2DATA(ep->com.tdev), ep->l2t);
2011         ep->l2t = l2t;
2012         dst_release(old);
2013         ep->dst = new;
2014         return 1;
2015 }
2016
2017 /*
2018  * All the CM events are handled on a work queue to have a safe context.
2019  */
2020 static int sched(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
2021 {
2022         struct iwch_ep_common *epc = ctx;
2023
2024         get_ep(epc);
2025
2026         /*
2027          * Save ctx and tdev in the skb->cb area.
2028          */
2029         *((void **) skb->cb) = ctx;
2030         *((struct t3cdev **) (skb->cb + sizeof(void *))) = tdev;
2031
2032         /*
2033          * Queue the skb and schedule the worker thread.
2034          */
2035         skb_queue_tail(&rxq, skb);
2036         queue_work(workq, &skb_work);
2037         return 0;
2038 }
2039
2040 static int set_tcb_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
2041 {
2042         struct cpl_set_tcb_rpl *rpl = cplhdr(skb);
2043
2044         if (rpl->status != CPL_ERR_NONE) {
2045                 printk(KERN_ERR MOD "Unexpected SET_TCB_RPL status %u "
2046                        "for tid %u\n", rpl->status, GET_TID(rpl));
2047         }
2048         return CPL_RET_BUF_DONE;
2049 }
2050
2051 int __init iwch_cm_init(void)
2052 {
2053         skb_queue_head_init(&rxq);
2054
2055         workq = create_singlethread_workqueue("iw_cxgb3");
2056         if (!workq)
2057                 return -ENOMEM;
2058
2059         /*
2060          * All upcalls from the T3 Core go to sched() to
2061          * schedule the processing on a work queue.
2062          */
2063         t3c_handlers[CPL_ACT_ESTABLISH] = sched;
2064         t3c_handlers[CPL_ACT_OPEN_RPL] = sched;
2065         t3c_handlers[CPL_RX_DATA] = sched;
2066         t3c_handlers[CPL_TX_DMA_ACK] = sched;
2067         t3c_handlers[CPL_ABORT_RPL_RSS] = sched;
2068         t3c_handlers[CPL_ABORT_RPL] = sched;
2069         t3c_handlers[CPL_PASS_OPEN_RPL] = sched;
2070         t3c_handlers[CPL_CLOSE_LISTSRV_RPL] = sched;
2071         t3c_handlers[CPL_PASS_ACCEPT_REQ] = sched;
2072         t3c_handlers[CPL_PASS_ESTABLISH] = sched;
2073         t3c_handlers[CPL_PEER_CLOSE] = sched;
2074         t3c_handlers[CPL_CLOSE_CON_RPL] = sched;
2075         t3c_handlers[CPL_ABORT_REQ_RSS] = sched;
2076         t3c_handlers[CPL_RDMA_TERMINATE] = sched;
2077         t3c_handlers[CPL_RDMA_EC_STATUS] = sched;
2078         t3c_handlers[CPL_SET_TCB_RPL] = set_tcb_rpl;
2079
2080         /*
2081          * These are the real handlers that are called from a
2082          * work queue.
2083          */
2084         work_handlers[CPL_ACT_ESTABLISH] = act_establish;
2085         work_handlers[CPL_ACT_OPEN_RPL] = act_open_rpl;
2086         work_handlers[CPL_RX_DATA] = rx_data;
2087         work_handlers[CPL_TX_DMA_ACK] = tx_ack;
2088         work_handlers[CPL_ABORT_RPL_RSS] = abort_rpl;
2089         work_handlers[CPL_ABORT_RPL] = abort_rpl;
2090         work_handlers[CPL_PASS_OPEN_RPL] = pass_open_rpl;
2091         work_handlers[CPL_CLOSE_LISTSRV_RPL] = close_listsrv_rpl;
2092         work_handlers[CPL_PASS_ACCEPT_REQ] = pass_accept_req;
2093         work_handlers[CPL_PASS_ESTABLISH] = pass_establish;
2094         work_handlers[CPL_PEER_CLOSE] = peer_close;
2095         work_handlers[CPL_ABORT_REQ_RSS] = peer_abort;
2096         work_handlers[CPL_CLOSE_CON_RPL] = close_con_rpl;
2097         work_handlers[CPL_RDMA_TERMINATE] = terminate;
2098         work_handlers[CPL_RDMA_EC_STATUS] = ec_status;
2099         return 0;
2100 }
2101
2102 void __exit iwch_cm_term(void)
2103 {
2104         flush_workqueue(workq);
2105         destroy_workqueue(workq);
2106 }