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