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