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