initial commit with v2.6.32.60
[linux-2.6.32.60-moxart.git] / drivers / infiniband / hw / cxgb3 / iwch_cm.c
blob675fc042bc6003d2bcf291bddc63f579e53a0f4c
1 /*
2 * Copyright (c) 2006 Chelsio, Inc. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
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.
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.
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>
40 #include <net/neighbour.h>
41 #include <net/netevent.h>
42 #include <net/route.h>
44 #include "tcb.h"
45 #include "cxgb3_offload.h"
46 #include "iwch.h"
47 #include "iwch_provider.h"
48 #include "iwch_cm.h"
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,
66 int peer2peer = 0;
67 module_param(peer2peer, int, 0644);
68 MODULE_PARM_DESC(peer2peer, "Support peer2peer ULPs (default=0)");
70 static int ep_timeout_secs = 60;
71 module_param(ep_timeout_secs, int, 0644);
72 MODULE_PARM_DESC(ep_timeout_secs, "CM Endpoint operation timeout "
73 "in seconds (default=60)");
75 static int mpa_rev = 1;
76 module_param(mpa_rev, int, 0644);
77 MODULE_PARM_DESC(mpa_rev, "MPA Revision, 0 supports amso1100, "
78 "1 is spec compliant. (default=1)");
80 static int markers_enabled = 0;
81 module_param(markers_enabled, int, 0644);
82 MODULE_PARM_DESC(markers_enabled, "Enable MPA MARKERS (default(0)=disabled)");
84 static int crc_enabled = 1;
85 module_param(crc_enabled, int, 0644);
86 MODULE_PARM_DESC(crc_enabled, "Enable MPA CRC (default(1)=enabled)");
88 static int rcv_win = 256 * 1024;
89 module_param(rcv_win, int, 0644);
90 MODULE_PARM_DESC(rcv_win, "TCP receive window in bytes (default=256)");
92 static int snd_win = 32 * 1024;
93 module_param(snd_win, int, 0644);
94 MODULE_PARM_DESC(snd_win, "TCP send window in bytes (default=32KB)");
96 static unsigned int nocong = 0;
97 module_param(nocong, uint, 0644);
98 MODULE_PARM_DESC(nocong, "Turn off congestion control (default=0)");
100 static unsigned int cong_flavor = 1;
101 module_param(cong_flavor, uint, 0644);
102 MODULE_PARM_DESC(cong_flavor, "TCP Congestion control flavor (default=1)");
104 static void process_work(struct work_struct *work);
105 static struct workqueue_struct *workq;
106 static DECLARE_WORK(skb_work, process_work);
108 static struct sk_buff_head rxq;
109 static cxgb3_cpl_handler_func work_handlers[NUM_CPL_CMDS];
111 static struct sk_buff *get_skb(struct sk_buff *skb, int len, gfp_t gfp);
112 static void ep_timeout(unsigned long arg);
113 static void connect_reply_upcall(struct iwch_ep *ep, int status);
115 static void start_ep_timer(struct iwch_ep *ep)
117 PDBG("%s ep %p\n", __func__, ep);
118 if (timer_pending(&ep->timer)) {
119 PDBG("%s stopped / restarted timer ep %p\n", __func__, ep);
120 del_timer_sync(&ep->timer);
121 } else
122 get_ep(&ep->com);
123 ep->timer.expires = jiffies + ep_timeout_secs * HZ;
124 ep->timer.data = (unsigned long)ep;
125 ep->timer.function = ep_timeout;
126 add_timer(&ep->timer);
129 static void stop_ep_timer(struct iwch_ep *ep)
131 PDBG("%s ep %p\n", __func__, ep);
132 if (!timer_pending(&ep->timer)) {
133 printk(KERN_ERR "%s timer stopped when its not running! ep %p state %u\n",
134 __func__, ep, ep->com.state);
135 WARN_ON(1);
136 return;
138 del_timer_sync(&ep->timer);
139 put_ep(&ep->com);
142 int iwch_l2t_send(struct t3cdev *tdev, struct sk_buff *skb, struct l2t_entry *l2e)
144 int error = 0;
145 struct cxio_rdev *rdev;
147 rdev = (struct cxio_rdev *)tdev->ulp;
148 if (cxio_fatal_error(rdev)) {
149 kfree_skb(skb);
150 return -EIO;
152 error = l2t_send(tdev, skb, l2e);
153 if (error)
154 kfree_skb(skb);
155 return error;
158 int iwch_cxgb3_ofld_send(struct t3cdev *tdev, struct sk_buff *skb)
160 int error = 0;
161 struct cxio_rdev *rdev;
163 rdev = (struct cxio_rdev *)tdev->ulp;
164 if (cxio_fatal_error(rdev)) {
165 kfree_skb(skb);
166 return -EIO;
168 error = cxgb3_ofld_send(tdev, skb);
169 if (error)
170 kfree_skb(skb);
171 return error;
174 static void release_tid(struct t3cdev *tdev, u32 hwtid, struct sk_buff *skb)
176 struct cpl_tid_release *req;
178 skb = get_skb(skb, sizeof *req, GFP_KERNEL);
179 if (!skb)
180 return;
181 req = (struct cpl_tid_release *) skb_put(skb, sizeof(*req));
182 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
183 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_TID_RELEASE, hwtid));
184 skb->priority = CPL_PRIORITY_SETUP;
185 iwch_cxgb3_ofld_send(tdev, skb);
186 return;
189 int iwch_quiesce_tid(struct iwch_ep *ep)
191 struct cpl_set_tcb_field *req;
192 struct sk_buff *skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
194 if (!skb)
195 return -ENOMEM;
196 req = (struct cpl_set_tcb_field *) skb_put(skb, sizeof(*req));
197 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
198 req->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
199 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, ep->hwtid));
200 req->reply = 0;
201 req->cpu_idx = 0;
202 req->word = htons(W_TCB_RX_QUIESCE);
203 req->mask = cpu_to_be64(1ULL << S_TCB_RX_QUIESCE);
204 req->val = cpu_to_be64(1 << S_TCB_RX_QUIESCE);
206 skb->priority = CPL_PRIORITY_DATA;
207 return iwch_cxgb3_ofld_send(ep->com.tdev, skb);
210 int iwch_resume_tid(struct iwch_ep *ep)
212 struct cpl_set_tcb_field *req;
213 struct sk_buff *skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
215 if (!skb)
216 return -ENOMEM;
217 req = (struct cpl_set_tcb_field *) skb_put(skb, sizeof(*req));
218 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
219 req->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
220 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, ep->hwtid));
221 req->reply = 0;
222 req->cpu_idx = 0;
223 req->word = htons(W_TCB_RX_QUIESCE);
224 req->mask = cpu_to_be64(1ULL << S_TCB_RX_QUIESCE);
225 req->val = 0;
227 skb->priority = CPL_PRIORITY_DATA;
228 return iwch_cxgb3_ofld_send(ep->com.tdev, skb);
231 static void set_emss(struct iwch_ep *ep, u16 opt)
233 PDBG("%s ep %p opt %u\n", __func__, ep, opt);
234 ep->emss = T3C_DATA(ep->com.tdev)->mtus[G_TCPOPT_MSS(opt)] - 40;
235 if (G_TCPOPT_TSTAMP(opt))
236 ep->emss -= 12;
237 if (ep->emss < 128)
238 ep->emss = 128;
239 PDBG("emss=%d\n", ep->emss);
242 static enum iwch_ep_state state_read(struct iwch_ep_common *epc)
244 unsigned long flags;
245 enum iwch_ep_state state;
247 spin_lock_irqsave(&epc->lock, flags);
248 state = epc->state;
249 spin_unlock_irqrestore(&epc->lock, flags);
250 return state;
253 static void __state_set(struct iwch_ep_common *epc, enum iwch_ep_state new)
255 epc->state = new;
258 static void state_set(struct iwch_ep_common *epc, enum iwch_ep_state new)
260 unsigned long flags;
262 spin_lock_irqsave(&epc->lock, flags);
263 PDBG("%s - %s -> %s\n", __func__, states[epc->state], states[new]);
264 __state_set(epc, new);
265 spin_unlock_irqrestore(&epc->lock, flags);
266 return;
269 static void *alloc_ep(int size, gfp_t gfp)
271 struct iwch_ep_common *epc;
273 epc = kzalloc(size, gfp);
274 if (epc) {
275 kref_init(&epc->kref);
276 spin_lock_init(&epc->lock);
277 init_waitqueue_head(&epc->waitq);
279 PDBG("%s alloc ep %p\n", __func__, epc);
280 return epc;
283 void __free_ep(struct kref *kref)
285 struct iwch_ep *ep;
286 ep = container_of(container_of(kref, struct iwch_ep_common, kref),
287 struct iwch_ep, com);
288 PDBG("%s ep %p state %s\n", __func__, ep, states[state_read(&ep->com)]);
289 if (test_bit(RELEASE_RESOURCES, &ep->com.flags)) {
290 cxgb3_remove_tid(ep->com.tdev, (void *)ep, ep->hwtid);
291 dst_release(ep->dst);
292 l2t_release(L2DATA(ep->com.tdev), ep->l2t);
294 kfree(ep);
297 static void release_ep_resources(struct iwch_ep *ep)
299 PDBG("%s ep %p tid %d\n", __func__, ep, ep->hwtid);
300 set_bit(RELEASE_RESOURCES, &ep->com.flags);
301 put_ep(&ep->com);
304 static void process_work(struct work_struct *work)
306 struct sk_buff *skb = NULL;
307 void *ep;
308 struct t3cdev *tdev;
309 int ret;
311 while ((skb = skb_dequeue(&rxq))) {
312 ep = *((void **) (skb->cb));
313 tdev = *((struct t3cdev **) (skb->cb + sizeof(void *)));
314 ret = work_handlers[G_OPCODE(ntohl((__force __be32)skb->csum))](tdev, skb, ep);
315 if (ret & CPL_RET_BUF_DONE)
316 kfree_skb(skb);
319 * ep was referenced in sched(), and is freed here.
321 put_ep((struct iwch_ep_common *)ep);
325 static int status2errno(int status)
327 switch (status) {
328 case CPL_ERR_NONE:
329 return 0;
330 case CPL_ERR_CONN_RESET:
331 return -ECONNRESET;
332 case CPL_ERR_ARP_MISS:
333 return -EHOSTUNREACH;
334 case CPL_ERR_CONN_TIMEDOUT:
335 return -ETIMEDOUT;
336 case CPL_ERR_TCAM_FULL:
337 return -ENOMEM;
338 case CPL_ERR_CONN_EXIST:
339 return -EADDRINUSE;
340 default:
341 return -EIO;
346 * Try and reuse skbs already allocated...
348 static struct sk_buff *get_skb(struct sk_buff *skb, int len, gfp_t gfp)
350 if (skb && !skb_is_nonlinear(skb) && !skb_cloned(skb)) {
351 skb_trim(skb, 0);
352 skb_get(skb);
353 } else {
354 skb = alloc_skb(len, gfp);
356 return skb;
359 static struct rtable *find_route(struct t3cdev *dev, __be32 local_ip,
360 __be32 peer_ip, __be16 local_port,
361 __be16 peer_port, u8 tos)
363 struct rtable *rt;
364 struct flowi fl = {
365 .oif = 0,
366 .nl_u = {
367 .ip4_u = {
368 .daddr = peer_ip,
369 .saddr = local_ip,
370 .tos = tos}
372 .proto = IPPROTO_TCP,
373 .uli_u = {
374 .ports = {
375 .sport = local_port,
376 .dport = peer_port}
380 if (ip_route_output_flow(&init_net, &rt, &fl, NULL, 0))
381 return NULL;
382 return rt;
385 static unsigned int find_best_mtu(const struct t3c_data *d, unsigned short mtu)
387 int i = 0;
389 while (i < d->nmtus - 1 && d->mtus[i + 1] <= mtu)
390 ++i;
391 return i;
394 static void arp_failure_discard(struct t3cdev *dev, struct sk_buff *skb)
396 PDBG("%s t3cdev %p\n", __func__, dev);
397 kfree_skb(skb);
401 * Handle an ARP failure for an active open.
403 static void act_open_req_arp_failure(struct t3cdev *dev, struct sk_buff *skb)
405 printk(KERN_ERR MOD "ARP failure duing connect\n");
406 kfree_skb(skb);
410 * Handle an ARP failure for a CPL_ABORT_REQ. Change it into a no RST variant
411 * and send it along.
413 static void abort_arp_failure(struct t3cdev *dev, struct sk_buff *skb)
415 struct cpl_abort_req *req = cplhdr(skb);
417 PDBG("%s t3cdev %p\n", __func__, dev);
418 req->cmd = CPL_ABORT_NO_RST;
419 iwch_cxgb3_ofld_send(dev, skb);
422 static int send_halfclose(struct iwch_ep *ep, gfp_t gfp)
424 struct cpl_close_con_req *req;
425 struct sk_buff *skb;
427 PDBG("%s ep %p\n", __func__, ep);
428 skb = get_skb(NULL, sizeof(*req), gfp);
429 if (!skb) {
430 printk(KERN_ERR MOD "%s - failed to alloc skb\n", __func__);
431 return -ENOMEM;
433 skb->priority = CPL_PRIORITY_DATA;
434 set_arp_failure_handler(skb, arp_failure_discard);
435 req = (struct cpl_close_con_req *) skb_put(skb, sizeof(*req));
436 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_CLOSE_CON));
437 req->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
438 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_CON_REQ, ep->hwtid));
439 return iwch_l2t_send(ep->com.tdev, skb, ep->l2t);
442 static int send_abort(struct iwch_ep *ep, struct sk_buff *skb, gfp_t gfp)
444 struct cpl_abort_req *req;
446 PDBG("%s ep %p\n", __func__, ep);
447 skb = get_skb(skb, sizeof(*req), gfp);
448 if (!skb) {
449 printk(KERN_ERR MOD "%s - failed to alloc skb.\n",
450 __func__);
451 return -ENOMEM;
453 skb->priority = CPL_PRIORITY_DATA;
454 set_arp_failure_handler(skb, abort_arp_failure);
455 req = (struct cpl_abort_req *) skb_put(skb, sizeof(*req));
456 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_HOST_ABORT_CON_REQ));
457 req->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
458 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_ABORT_REQ, ep->hwtid));
459 req->cmd = CPL_ABORT_SEND_RST;
460 return iwch_l2t_send(ep->com.tdev, skb, ep->l2t);
463 static int send_connect(struct iwch_ep *ep)
465 struct cpl_act_open_req *req;
466 struct sk_buff *skb;
467 u32 opt0h, opt0l, opt2;
468 unsigned int mtu_idx;
469 int wscale;
471 PDBG("%s ep %p\n", __func__, ep);
473 skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
474 if (!skb) {
475 printk(KERN_ERR MOD "%s - failed to alloc skb.\n",
476 __func__);
477 return -ENOMEM;
479 mtu_idx = find_best_mtu(T3C_DATA(ep->com.tdev), dst_mtu(ep->dst));
480 wscale = compute_wscale(rcv_win);
481 opt0h = V_NAGLE(0) |
482 V_NO_CONG(nocong) |
483 V_KEEP_ALIVE(1) |
484 F_TCAM_BYPASS |
485 V_WND_SCALE(wscale) |
486 V_MSS_IDX(mtu_idx) |
487 V_L2T_IDX(ep->l2t->idx) | V_TX_CHANNEL(ep->l2t->smt_idx);
488 opt0l = V_TOS((ep->tos >> 2) & M_TOS) | V_RCV_BUFSIZ(rcv_win>>10);
489 opt2 = F_RX_COALESCE_VALID | V_RX_COALESCE(0) | V_FLAVORS_VALID(1) |
490 V_CONG_CONTROL_FLAVOR(cong_flavor);
491 skb->priority = CPL_PRIORITY_SETUP;
492 set_arp_failure_handler(skb, act_open_req_arp_failure);
494 req = (struct cpl_act_open_req *) skb_put(skb, sizeof(*req));
495 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
496 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_ACT_OPEN_REQ, ep->atid));
497 req->local_port = ep->com.local_addr.sin_port;
498 req->peer_port = ep->com.remote_addr.sin_port;
499 req->local_ip = ep->com.local_addr.sin_addr.s_addr;
500 req->peer_ip = ep->com.remote_addr.sin_addr.s_addr;
501 req->opt0h = htonl(opt0h);
502 req->opt0l = htonl(opt0l);
503 req->params = 0;
504 req->opt2 = htonl(opt2);
505 return iwch_l2t_send(ep->com.tdev, skb, ep->l2t);
508 static void send_mpa_req(struct iwch_ep *ep, struct sk_buff *skb)
510 int mpalen;
511 struct tx_data_wr *req;
512 struct mpa_message *mpa;
513 int len;
515 PDBG("%s ep %p pd_len %d\n", __func__, ep, ep->plen);
517 BUG_ON(skb_cloned(skb));
519 mpalen = sizeof(*mpa) + ep->plen;
520 if (skb->data + mpalen + sizeof(*req) > skb_end_pointer(skb)) {
521 kfree_skb(skb);
522 skb=alloc_skb(mpalen + sizeof(*req), GFP_KERNEL);
523 if (!skb) {
524 connect_reply_upcall(ep, -ENOMEM);
525 return;
528 skb_trim(skb, 0);
529 skb_reserve(skb, sizeof(*req));
530 skb_put(skb, mpalen);
531 skb->priority = CPL_PRIORITY_DATA;
532 mpa = (struct mpa_message *) skb->data;
533 memset(mpa, 0, sizeof(*mpa));
534 memcpy(mpa->key, MPA_KEY_REQ, sizeof(mpa->key));
535 mpa->flags = (crc_enabled ? MPA_CRC : 0) |
536 (markers_enabled ? MPA_MARKERS : 0);
537 mpa->private_data_size = htons(ep->plen);
538 mpa->revision = mpa_rev;
540 if (ep->plen)
541 memcpy(mpa->private_data, ep->mpa_pkt + sizeof(*mpa), ep->plen);
544 * Reference the mpa skb. This ensures the data area
545 * will remain in memory until the hw acks the tx.
546 * Function tx_ack() will deref it.
548 skb_get(skb);
549 set_arp_failure_handler(skb, arp_failure_discard);
550 skb_reset_transport_header(skb);
551 len = skb->len;
552 req = (struct tx_data_wr *) skb_push(skb, sizeof(*req));
553 req->wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_TX_DATA)|F_WR_COMPL);
554 req->wr_lo = htonl(V_WR_TID(ep->hwtid));
555 req->len = htonl(len);
556 req->param = htonl(V_TX_PORT(ep->l2t->smt_idx) |
557 V_TX_SNDBUF(snd_win>>15));
558 req->flags = htonl(F_TX_INIT);
559 req->sndseq = htonl(ep->snd_seq);
560 BUG_ON(ep->mpa_skb);
561 ep->mpa_skb = skb;
562 iwch_l2t_send(ep->com.tdev, skb, ep->l2t);
563 start_ep_timer(ep);
564 state_set(&ep->com, MPA_REQ_SENT);
565 return;
568 static int send_mpa_reject(struct iwch_ep *ep, const void *pdata, u8 plen)
570 int mpalen;
571 struct tx_data_wr *req;
572 struct mpa_message *mpa;
573 struct sk_buff *skb;
575 PDBG("%s ep %p plen %d\n", __func__, ep, plen);
577 mpalen = sizeof(*mpa) + plen;
579 skb = get_skb(NULL, mpalen + sizeof(*req), GFP_KERNEL);
580 if (!skb) {
581 printk(KERN_ERR MOD "%s - cannot alloc skb!\n", __func__);
582 return -ENOMEM;
584 skb_reserve(skb, sizeof(*req));
585 mpa = (struct mpa_message *) skb_put(skb, mpalen);
586 memset(mpa, 0, sizeof(*mpa));
587 memcpy(mpa->key, MPA_KEY_REP, sizeof(mpa->key));
588 mpa->flags = MPA_REJECT;
589 mpa->revision = mpa_rev;
590 mpa->private_data_size = htons(plen);
591 if (plen)
592 memcpy(mpa->private_data, pdata, plen);
595 * Reference the mpa skb again. This ensures the data area
596 * will remain in memory until the hw acks the tx.
597 * Function tx_ack() will deref it.
599 skb_get(skb);
600 skb->priority = CPL_PRIORITY_DATA;
601 set_arp_failure_handler(skb, arp_failure_discard);
602 skb_reset_transport_header(skb);
603 req = (struct tx_data_wr *) skb_push(skb, sizeof(*req));
604 req->wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_TX_DATA)|F_WR_COMPL);
605 req->wr_lo = htonl(V_WR_TID(ep->hwtid));
606 req->len = htonl(mpalen);
607 req->param = htonl(V_TX_PORT(ep->l2t->smt_idx) |
608 V_TX_SNDBUF(snd_win>>15));
609 req->flags = htonl(F_TX_INIT);
610 req->sndseq = htonl(ep->snd_seq);
611 BUG_ON(ep->mpa_skb);
612 ep->mpa_skb = skb;
613 return iwch_l2t_send(ep->com.tdev, skb, ep->l2t);
616 static int send_mpa_reply(struct iwch_ep *ep, const void *pdata, u8 plen)
618 int mpalen;
619 struct tx_data_wr *req;
620 struct mpa_message *mpa;
621 int len;
622 struct sk_buff *skb;
624 PDBG("%s ep %p plen %d\n", __func__, ep, plen);
626 mpalen = sizeof(*mpa) + plen;
628 skb = get_skb(NULL, mpalen + sizeof(*req), GFP_KERNEL);
629 if (!skb) {
630 printk(KERN_ERR MOD "%s - cannot alloc skb!\n", __func__);
631 return -ENOMEM;
633 skb->priority = CPL_PRIORITY_DATA;
634 skb_reserve(skb, sizeof(*req));
635 mpa = (struct mpa_message *) skb_put(skb, mpalen);
636 memset(mpa, 0, sizeof(*mpa));
637 memcpy(mpa->key, MPA_KEY_REP, sizeof(mpa->key));
638 mpa->flags = (ep->mpa_attr.crc_enabled ? MPA_CRC : 0) |
639 (markers_enabled ? MPA_MARKERS : 0);
640 mpa->revision = mpa_rev;
641 mpa->private_data_size = htons(plen);
642 if (plen)
643 memcpy(mpa->private_data, pdata, plen);
646 * Reference the mpa skb. This ensures the data area
647 * will remain in memory until the hw acks the tx.
648 * Function tx_ack() will deref it.
650 skb_get(skb);
651 set_arp_failure_handler(skb, arp_failure_discard);
652 skb_reset_transport_header(skb);
653 len = skb->len;
654 req = (struct tx_data_wr *) skb_push(skb, sizeof(*req));
655 req->wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_TX_DATA)|F_WR_COMPL);
656 req->wr_lo = htonl(V_WR_TID(ep->hwtid));
657 req->len = htonl(len);
658 req->param = htonl(V_TX_PORT(ep->l2t->smt_idx) |
659 V_TX_SNDBUF(snd_win>>15));
660 req->flags = htonl(F_TX_INIT);
661 req->sndseq = htonl(ep->snd_seq);
662 ep->mpa_skb = skb;
663 state_set(&ep->com, MPA_REP_SENT);
664 return iwch_l2t_send(ep->com.tdev, skb, ep->l2t);
667 static int act_establish(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
669 struct iwch_ep *ep = ctx;
670 struct cpl_act_establish *req = cplhdr(skb);
671 unsigned int tid = GET_TID(req);
673 PDBG("%s ep %p tid %d\n", __func__, ep, tid);
675 dst_confirm(ep->dst);
677 /* setup the hwtid for this connection */
678 ep->hwtid = tid;
679 cxgb3_insert_tid(ep->com.tdev, &t3c_client, ep, tid);
681 ep->snd_seq = ntohl(req->snd_isn);
682 ep->rcv_seq = ntohl(req->rcv_isn);
684 set_emss(ep, ntohs(req->tcp_opt));
686 /* dealloc the atid */
687 cxgb3_free_atid(ep->com.tdev, ep->atid);
689 /* start MPA negotiation */
690 send_mpa_req(ep, skb);
692 return 0;
695 static void abort_connection(struct iwch_ep *ep, struct sk_buff *skb, gfp_t gfp)
697 PDBG("%s ep %p\n", __FILE__, ep);
698 state_set(&ep->com, ABORTING);
699 send_abort(ep, skb, gfp);
702 static void close_complete_upcall(struct iwch_ep *ep)
704 struct iw_cm_event event;
706 PDBG("%s ep %p\n", __func__, ep);
707 memset(&event, 0, sizeof(event));
708 event.event = IW_CM_EVENT_CLOSE;
709 if (ep->com.cm_id) {
710 PDBG("close complete delivered ep %p cm_id %p tid %d\n",
711 ep, ep->com.cm_id, ep->hwtid);
712 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
713 ep->com.cm_id->rem_ref(ep->com.cm_id);
714 ep->com.cm_id = NULL;
715 ep->com.qp = NULL;
719 static void peer_close_upcall(struct iwch_ep *ep)
721 struct iw_cm_event event;
723 PDBG("%s ep %p\n", __func__, ep);
724 memset(&event, 0, sizeof(event));
725 event.event = IW_CM_EVENT_DISCONNECT;
726 if (ep->com.cm_id) {
727 PDBG("peer close delivered ep %p cm_id %p tid %d\n",
728 ep, ep->com.cm_id, ep->hwtid);
729 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
733 static void peer_abort_upcall(struct iwch_ep *ep)
735 struct iw_cm_event event;
737 PDBG("%s ep %p\n", __func__, ep);
738 memset(&event, 0, sizeof(event));
739 event.event = IW_CM_EVENT_CLOSE;
740 event.status = -ECONNRESET;
741 if (ep->com.cm_id) {
742 PDBG("abort delivered ep %p cm_id %p tid %d\n", ep,
743 ep->com.cm_id, ep->hwtid);
744 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
745 ep->com.cm_id->rem_ref(ep->com.cm_id);
746 ep->com.cm_id = NULL;
747 ep->com.qp = NULL;
751 static void connect_reply_upcall(struct iwch_ep *ep, int status)
753 struct iw_cm_event event;
755 PDBG("%s ep %p status %d\n", __func__, ep, status);
756 memset(&event, 0, sizeof(event));
757 event.event = IW_CM_EVENT_CONNECT_REPLY;
758 event.status = status;
759 event.local_addr = ep->com.local_addr;
760 event.remote_addr = ep->com.remote_addr;
762 if ((status == 0) || (status == -ECONNREFUSED)) {
763 event.private_data_len = ep->plen;
764 event.private_data = ep->mpa_pkt + sizeof(struct mpa_message);
766 if (ep->com.cm_id) {
767 PDBG("%s ep %p tid %d status %d\n", __func__, ep,
768 ep->hwtid, status);
769 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
771 if (status < 0) {
772 ep->com.cm_id->rem_ref(ep->com.cm_id);
773 ep->com.cm_id = NULL;
774 ep->com.qp = NULL;
778 static void connect_request_upcall(struct iwch_ep *ep)
780 struct iw_cm_event event;
782 PDBG("%s ep %p tid %d\n", __func__, ep, ep->hwtid);
783 memset(&event, 0, sizeof(event));
784 event.event = IW_CM_EVENT_CONNECT_REQUEST;
785 event.local_addr = ep->com.local_addr;
786 event.remote_addr = ep->com.remote_addr;
787 event.private_data_len = ep->plen;
788 event.private_data = ep->mpa_pkt + sizeof(struct mpa_message);
789 event.provider_data = ep;
790 if (state_read(&ep->parent_ep->com) != DEAD) {
791 get_ep(&ep->com);
792 ep->parent_ep->com.cm_id->event_handler(
793 ep->parent_ep->com.cm_id,
794 &event);
796 put_ep(&ep->parent_ep->com);
797 ep->parent_ep = NULL;
800 static void established_upcall(struct iwch_ep *ep)
802 struct iw_cm_event event;
804 PDBG("%s ep %p\n", __func__, ep);
805 memset(&event, 0, sizeof(event));
806 event.event = IW_CM_EVENT_ESTABLISHED;
807 if (ep->com.cm_id) {
808 PDBG("%s ep %p tid %d\n", __func__, ep, ep->hwtid);
809 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
813 static int update_rx_credits(struct iwch_ep *ep, u32 credits)
815 struct cpl_rx_data_ack *req;
816 struct sk_buff *skb;
818 PDBG("%s ep %p credits %u\n", __func__, ep, credits);
819 skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
820 if (!skb) {
821 printk(KERN_ERR MOD "update_rx_credits - cannot alloc skb!\n");
822 return 0;
825 req = (struct cpl_rx_data_ack *) skb_put(skb, sizeof(*req));
826 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
827 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_RX_DATA_ACK, ep->hwtid));
828 req->credit_dack = htonl(V_RX_CREDITS(credits) | V_RX_FORCE_ACK(1));
829 skb->priority = CPL_PRIORITY_ACK;
830 iwch_cxgb3_ofld_send(ep->com.tdev, skb);
831 return credits;
834 static void process_mpa_reply(struct iwch_ep *ep, struct sk_buff *skb)
836 struct mpa_message *mpa;
837 u16 plen;
838 struct iwch_qp_attributes attrs;
839 enum iwch_qp_attr_mask mask;
840 int err;
842 PDBG("%s ep %p\n", __func__, ep);
845 * Stop mpa timer. If it expired, then the state has
846 * changed and we bail since ep_timeout already aborted
847 * the connection.
849 stop_ep_timer(ep);
850 if (state_read(&ep->com) != MPA_REQ_SENT)
851 return;
854 * If we get more than the supported amount of private data
855 * then we must fail this connection.
857 if (ep->mpa_pkt_len + skb->len > sizeof(ep->mpa_pkt)) {
858 err = -EINVAL;
859 goto err;
863 * copy the new data into our accumulation buffer.
865 skb_copy_from_linear_data(skb, &(ep->mpa_pkt[ep->mpa_pkt_len]),
866 skb->len);
867 ep->mpa_pkt_len += skb->len;
870 * if we don't even have the mpa message, then bail.
872 if (ep->mpa_pkt_len < sizeof(*mpa))
873 return;
874 mpa = (struct mpa_message *) ep->mpa_pkt;
876 /* Validate MPA header. */
877 if (mpa->revision != mpa_rev) {
878 err = -EPROTO;
879 goto err;
881 if (memcmp(mpa->key, MPA_KEY_REP, sizeof(mpa->key))) {
882 err = -EPROTO;
883 goto err;
886 plen = ntohs(mpa->private_data_size);
889 * Fail if there's too much private data.
891 if (plen > MPA_MAX_PRIVATE_DATA) {
892 err = -EPROTO;
893 goto err;
897 * If plen does not account for pkt size
899 if (ep->mpa_pkt_len > (sizeof(*mpa) + plen)) {
900 err = -EPROTO;
901 goto err;
904 ep->plen = (u8) plen;
907 * If we don't have all the pdata yet, then bail.
908 * We'll continue process when more data arrives.
910 if (ep->mpa_pkt_len < (sizeof(*mpa) + plen))
911 return;
913 if (mpa->flags & MPA_REJECT) {
914 err = -ECONNREFUSED;
915 goto err;
919 * If we get here we have accumulated the entire mpa
920 * start reply message including private data. And
921 * the MPA header is valid.
923 state_set(&ep->com, FPDU_MODE);
924 ep->mpa_attr.initiator = 1;
925 ep->mpa_attr.crc_enabled = (mpa->flags & MPA_CRC) | crc_enabled ? 1 : 0;
926 ep->mpa_attr.recv_marker_enabled = markers_enabled;
927 ep->mpa_attr.xmit_marker_enabled = mpa->flags & MPA_MARKERS ? 1 : 0;
928 ep->mpa_attr.version = mpa_rev;
929 PDBG("%s - crc_enabled=%d, recv_marker_enabled=%d, "
930 "xmit_marker_enabled=%d, version=%d\n", __func__,
931 ep->mpa_attr.crc_enabled, ep->mpa_attr.recv_marker_enabled,
932 ep->mpa_attr.xmit_marker_enabled, ep->mpa_attr.version);
934 attrs.mpa_attr = ep->mpa_attr;
935 attrs.max_ird = ep->ird;
936 attrs.max_ord = ep->ord;
937 attrs.llp_stream_handle = ep;
938 attrs.next_state = IWCH_QP_STATE_RTS;
940 mask = IWCH_QP_ATTR_NEXT_STATE |
941 IWCH_QP_ATTR_LLP_STREAM_HANDLE | IWCH_QP_ATTR_MPA_ATTR |
942 IWCH_QP_ATTR_MAX_IRD | IWCH_QP_ATTR_MAX_ORD;
944 /* bind QP and TID with INIT_WR */
945 err = iwch_modify_qp(ep->com.qp->rhp,
946 ep->com.qp, mask, &attrs, 1);
947 if (err)
948 goto err;
950 if (peer2peer && iwch_rqes_posted(ep->com.qp) == 0) {
951 iwch_post_zb_read(ep->com.qp);
954 goto out;
955 err:
956 abort_connection(ep, skb, GFP_KERNEL);
957 out:
958 connect_reply_upcall(ep, err);
959 return;
962 static void process_mpa_request(struct iwch_ep *ep, struct sk_buff *skb)
964 struct mpa_message *mpa;
965 u16 plen;
967 PDBG("%s ep %p\n", __func__, ep);
970 * Stop mpa timer. If it expired, then the state has
971 * changed and we bail since ep_timeout already aborted
972 * the connection.
974 stop_ep_timer(ep);
975 if (state_read(&ep->com) != MPA_REQ_WAIT)
976 return;
979 * If we get more than the supported amount of private data
980 * then we must fail this connection.
982 if (ep->mpa_pkt_len + skb->len > sizeof(ep->mpa_pkt)) {
983 abort_connection(ep, skb, GFP_KERNEL);
984 return;
987 PDBG("%s enter (%s line %u)\n", __func__, __FILE__, __LINE__);
990 * Copy the new data into our accumulation buffer.
992 skb_copy_from_linear_data(skb, &(ep->mpa_pkt[ep->mpa_pkt_len]),
993 skb->len);
994 ep->mpa_pkt_len += skb->len;
997 * If we don't even have the mpa message, then bail.
998 * We'll continue process when more data arrives.
1000 if (ep->mpa_pkt_len < sizeof(*mpa))
1001 return;
1002 PDBG("%s enter (%s line %u)\n", __func__, __FILE__, __LINE__);
1003 mpa = (struct mpa_message *) ep->mpa_pkt;
1006 * Validate MPA Header.
1008 if (mpa->revision != mpa_rev) {
1009 abort_connection(ep, skb, GFP_KERNEL);
1010 return;
1013 if (memcmp(mpa->key, MPA_KEY_REQ, sizeof(mpa->key))) {
1014 abort_connection(ep, skb, GFP_KERNEL);
1015 return;
1018 plen = ntohs(mpa->private_data_size);
1021 * Fail if there's too much private data.
1023 if (plen > MPA_MAX_PRIVATE_DATA) {
1024 abort_connection(ep, skb, GFP_KERNEL);
1025 return;
1029 * If plen does not account for pkt size
1031 if (ep->mpa_pkt_len > (sizeof(*mpa) + plen)) {
1032 abort_connection(ep, skb, GFP_KERNEL);
1033 return;
1035 ep->plen = (u8) plen;
1038 * If we don't have all the pdata yet, then bail.
1040 if (ep->mpa_pkt_len < (sizeof(*mpa) + plen))
1041 return;
1044 * If we get here we have accumulated the entire mpa
1045 * start reply message including private data.
1047 ep->mpa_attr.initiator = 0;
1048 ep->mpa_attr.crc_enabled = (mpa->flags & MPA_CRC) | crc_enabled ? 1 : 0;
1049 ep->mpa_attr.recv_marker_enabled = markers_enabled;
1050 ep->mpa_attr.xmit_marker_enabled = mpa->flags & MPA_MARKERS ? 1 : 0;
1051 ep->mpa_attr.version = mpa_rev;
1052 PDBG("%s - crc_enabled=%d, recv_marker_enabled=%d, "
1053 "xmit_marker_enabled=%d, version=%d\n", __func__,
1054 ep->mpa_attr.crc_enabled, ep->mpa_attr.recv_marker_enabled,
1055 ep->mpa_attr.xmit_marker_enabled, ep->mpa_attr.version);
1057 state_set(&ep->com, MPA_REQ_RCVD);
1059 /* drive upcall */
1060 connect_request_upcall(ep);
1061 return;
1064 static int rx_data(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1066 struct iwch_ep *ep = ctx;
1067 struct cpl_rx_data *hdr = cplhdr(skb);
1068 unsigned int dlen = ntohs(hdr->len);
1070 PDBG("%s ep %p dlen %u\n", __func__, ep, dlen);
1072 skb_pull(skb, sizeof(*hdr));
1073 skb_trim(skb, dlen);
1075 ep->rcv_seq += dlen;
1076 BUG_ON(ep->rcv_seq != (ntohl(hdr->seq) + dlen));
1078 switch (state_read(&ep->com)) {
1079 case MPA_REQ_SENT:
1080 process_mpa_reply(ep, skb);
1081 break;
1082 case MPA_REQ_WAIT:
1083 process_mpa_request(ep, skb);
1084 break;
1085 case MPA_REP_SENT:
1086 break;
1087 default:
1088 printk(KERN_ERR MOD "%s Unexpected streaming data."
1089 " ep %p state %d tid %d\n",
1090 __func__, ep, state_read(&ep->com), ep->hwtid);
1093 * The ep will timeout and inform the ULP of the failure.
1094 * See ep_timeout().
1096 break;
1099 /* update RX credits */
1100 update_rx_credits(ep, dlen);
1102 return CPL_RET_BUF_DONE;
1106 * Upcall from the adapter indicating data has been transmitted.
1107 * For us its just the single MPA request or reply. We can now free
1108 * the skb holding the mpa message.
1110 static int tx_ack(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1112 struct iwch_ep *ep = ctx;
1113 struct cpl_wr_ack *hdr = cplhdr(skb);
1114 unsigned int credits = ntohs(hdr->credits);
1116 PDBG("%s ep %p credits %u\n", __func__, ep, credits);
1118 if (credits == 0) {
1119 PDBG(KERN_ERR "%s 0 credit ack ep %p state %u\n",
1120 __func__, ep, state_read(&ep->com));
1121 return CPL_RET_BUF_DONE;
1124 BUG_ON(credits != 1);
1125 dst_confirm(ep->dst);
1126 if (!ep->mpa_skb) {
1127 PDBG("%s rdma_init wr_ack ep %p state %u\n",
1128 __func__, ep, state_read(&ep->com));
1129 if (ep->mpa_attr.initiator) {
1130 PDBG("%s initiator ep %p state %u\n",
1131 __func__, ep, state_read(&ep->com));
1132 if (peer2peer)
1133 iwch_post_zb_read(ep->com.qp);
1134 } else {
1135 PDBG("%s responder ep %p state %u\n",
1136 __func__, ep, state_read(&ep->com));
1137 ep->com.rpl_done = 1;
1138 wake_up(&ep->com.waitq);
1140 } else {
1141 PDBG("%s lsm ack ep %p state %u freeing skb\n",
1142 __func__, ep, state_read(&ep->com));
1143 kfree_skb(ep->mpa_skb);
1144 ep->mpa_skb = NULL;
1146 return CPL_RET_BUF_DONE;
1149 static int abort_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1151 struct iwch_ep *ep = ctx;
1152 unsigned long flags;
1153 int release = 0;
1155 PDBG("%s ep %p\n", __func__, ep);
1156 BUG_ON(!ep);
1159 * We get 2 abort replies from the HW. The first one must
1160 * be ignored except for scribbling that we need one more.
1162 if (!test_and_set_bit(ABORT_REQ_IN_PROGRESS, &ep->com.flags)) {
1163 return CPL_RET_BUF_DONE;
1166 spin_lock_irqsave(&ep->com.lock, flags);
1167 switch (ep->com.state) {
1168 case ABORTING:
1169 close_complete_upcall(ep);
1170 __state_set(&ep->com, DEAD);
1171 release = 1;
1172 break;
1173 default:
1174 printk(KERN_ERR "%s ep %p state %d\n",
1175 __func__, ep, ep->com.state);
1176 break;
1178 spin_unlock_irqrestore(&ep->com.lock, flags);
1180 if (release)
1181 release_ep_resources(ep);
1182 return CPL_RET_BUF_DONE;
1186 * Return whether a failed active open has allocated a TID
1188 static inline int act_open_has_tid(int status)
1190 return status != CPL_ERR_TCAM_FULL && status != CPL_ERR_CONN_EXIST &&
1191 status != CPL_ERR_ARP_MISS;
1194 static int act_open_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1196 struct iwch_ep *ep = ctx;
1197 struct cpl_act_open_rpl *rpl = cplhdr(skb);
1199 PDBG("%s ep %p status %u errno %d\n", __func__, ep, rpl->status,
1200 status2errno(rpl->status));
1201 connect_reply_upcall(ep, status2errno(rpl->status));
1202 state_set(&ep->com, DEAD);
1203 if (ep->com.tdev->type != T3A && act_open_has_tid(rpl->status))
1204 release_tid(ep->com.tdev, GET_TID(rpl), NULL);
1205 cxgb3_free_atid(ep->com.tdev, ep->atid);
1206 dst_release(ep->dst);
1207 l2t_release(L2DATA(ep->com.tdev), ep->l2t);
1208 put_ep(&ep->com);
1209 return CPL_RET_BUF_DONE;
1212 static int listen_start(struct iwch_listen_ep *ep)
1214 struct sk_buff *skb;
1215 struct cpl_pass_open_req *req;
1217 PDBG("%s ep %p\n", __func__, ep);
1218 skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
1219 if (!skb) {
1220 printk(KERN_ERR MOD "t3c_listen_start failed to alloc skb!\n");
1221 return -ENOMEM;
1224 req = (struct cpl_pass_open_req *) skb_put(skb, sizeof(*req));
1225 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
1226 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ, ep->stid));
1227 req->local_port = ep->com.local_addr.sin_port;
1228 req->local_ip = ep->com.local_addr.sin_addr.s_addr;
1229 req->peer_port = 0;
1230 req->peer_ip = 0;
1231 req->peer_netmask = 0;
1232 req->opt0h = htonl(F_DELACK | F_TCAM_BYPASS);
1233 req->opt0l = htonl(V_RCV_BUFSIZ(rcv_win>>10));
1234 req->opt1 = htonl(V_CONN_POLICY(CPL_CONN_POLICY_ASK));
1236 skb->priority = 1;
1237 return iwch_cxgb3_ofld_send(ep->com.tdev, skb);
1240 static int pass_open_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1242 struct iwch_listen_ep *ep = ctx;
1243 struct cpl_pass_open_rpl *rpl = cplhdr(skb);
1245 PDBG("%s ep %p status %d error %d\n", __func__, ep,
1246 rpl->status, status2errno(rpl->status));
1247 ep->com.rpl_err = status2errno(rpl->status);
1248 ep->com.rpl_done = 1;
1249 wake_up(&ep->com.waitq);
1251 return CPL_RET_BUF_DONE;
1254 static int listen_stop(struct iwch_listen_ep *ep)
1256 struct sk_buff *skb;
1257 struct cpl_close_listserv_req *req;
1259 PDBG("%s ep %p\n", __func__, ep);
1260 skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
1261 if (!skb) {
1262 printk(KERN_ERR MOD "%s - failed to alloc skb\n", __func__);
1263 return -ENOMEM;
1265 req = (struct cpl_close_listserv_req *) skb_put(skb, sizeof(*req));
1266 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
1267 req->cpu_idx = 0;
1268 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_LISTSRV_REQ, ep->stid));
1269 skb->priority = 1;
1270 return iwch_cxgb3_ofld_send(ep->com.tdev, skb);
1273 static int close_listsrv_rpl(struct t3cdev *tdev, struct sk_buff *skb,
1274 void *ctx)
1276 struct iwch_listen_ep *ep = ctx;
1277 struct cpl_close_listserv_rpl *rpl = cplhdr(skb);
1279 PDBG("%s ep %p\n", __func__, ep);
1280 ep->com.rpl_err = status2errno(rpl->status);
1281 ep->com.rpl_done = 1;
1282 wake_up(&ep->com.waitq);
1283 return CPL_RET_BUF_DONE;
1286 static void accept_cr(struct iwch_ep *ep, __be32 peer_ip, struct sk_buff *skb)
1288 struct cpl_pass_accept_rpl *rpl;
1289 unsigned int mtu_idx;
1290 u32 opt0h, opt0l, opt2;
1291 int wscale;
1293 PDBG("%s ep %p\n", __func__, ep);
1294 BUG_ON(skb_cloned(skb));
1295 skb_trim(skb, sizeof(*rpl));
1296 skb_get(skb);
1297 mtu_idx = find_best_mtu(T3C_DATA(ep->com.tdev), dst_mtu(ep->dst));
1298 wscale = compute_wscale(rcv_win);
1299 opt0h = V_NAGLE(0) |
1300 V_NO_CONG(nocong) |
1301 V_KEEP_ALIVE(1) |
1302 F_TCAM_BYPASS |
1303 V_WND_SCALE(wscale) |
1304 V_MSS_IDX(mtu_idx) |
1305 V_L2T_IDX(ep->l2t->idx) | V_TX_CHANNEL(ep->l2t->smt_idx);
1306 opt0l = V_TOS((ep->tos >> 2) & M_TOS) | V_RCV_BUFSIZ(rcv_win>>10);
1307 opt2 = F_RX_COALESCE_VALID | V_RX_COALESCE(0) | V_FLAVORS_VALID(1) |
1308 V_CONG_CONTROL_FLAVOR(cong_flavor);
1310 rpl = cplhdr(skb);
1311 rpl->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
1312 OPCODE_TID(rpl) = htonl(MK_OPCODE_TID(CPL_PASS_ACCEPT_RPL, ep->hwtid));
1313 rpl->peer_ip = peer_ip;
1314 rpl->opt0h = htonl(opt0h);
1315 rpl->opt0l_status = htonl(opt0l | CPL_PASS_OPEN_ACCEPT);
1316 rpl->opt2 = htonl(opt2);
1317 rpl->rsvd = rpl->opt2; /* workaround for HW bug */
1318 skb->priority = CPL_PRIORITY_SETUP;
1319 iwch_l2t_send(ep->com.tdev, skb, ep->l2t);
1321 return;
1324 static void reject_cr(struct t3cdev *tdev, u32 hwtid, __be32 peer_ip,
1325 struct sk_buff *skb)
1327 PDBG("%s t3cdev %p tid %u peer_ip %x\n", __func__, tdev, hwtid,
1328 peer_ip);
1329 BUG_ON(skb_cloned(skb));
1330 skb_trim(skb, sizeof(struct cpl_tid_release));
1331 skb_get(skb);
1333 if (tdev->type != T3A)
1334 release_tid(tdev, hwtid, skb);
1335 else {
1336 struct cpl_pass_accept_rpl *rpl;
1338 rpl = cplhdr(skb);
1339 skb->priority = CPL_PRIORITY_SETUP;
1340 rpl->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
1341 OPCODE_TID(rpl) = htonl(MK_OPCODE_TID(CPL_PASS_ACCEPT_RPL,
1342 hwtid));
1343 rpl->peer_ip = peer_ip;
1344 rpl->opt0h = htonl(F_TCAM_BYPASS);
1345 rpl->opt0l_status = htonl(CPL_PASS_OPEN_REJECT);
1346 rpl->opt2 = 0;
1347 rpl->rsvd = rpl->opt2;
1348 iwch_cxgb3_ofld_send(tdev, skb);
1352 static int pass_accept_req(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1354 struct iwch_ep *child_ep, *parent_ep = ctx;
1355 struct cpl_pass_accept_req *req = cplhdr(skb);
1356 unsigned int hwtid = GET_TID(req);
1357 struct dst_entry *dst;
1358 struct l2t_entry *l2t;
1359 struct rtable *rt;
1360 struct iff_mac tim;
1362 PDBG("%s parent ep %p tid %u\n", __func__, parent_ep, hwtid);
1364 if (state_read(&parent_ep->com) != LISTEN) {
1365 printk(KERN_ERR "%s - listening ep not in LISTEN\n",
1366 __func__);
1367 goto reject;
1371 * Find the netdev for this connection request.
1373 tim.mac_addr = req->dst_mac;
1374 tim.vlan_tag = ntohs(req->vlan_tag);
1375 if (tdev->ctl(tdev, GET_IFF_FROM_MAC, &tim) < 0 || !tim.dev) {
1376 printk(KERN_ERR
1377 "%s bad dst mac %02x %02x %02x %02x %02x %02x\n",
1378 __func__,
1379 req->dst_mac[0],
1380 req->dst_mac[1],
1381 req->dst_mac[2],
1382 req->dst_mac[3],
1383 req->dst_mac[4],
1384 req->dst_mac[5]);
1385 goto reject;
1388 /* Find output route */
1389 rt = find_route(tdev,
1390 req->local_ip,
1391 req->peer_ip,
1392 req->local_port,
1393 req->peer_port, G_PASS_OPEN_TOS(ntohl(req->tos_tid)));
1394 if (!rt) {
1395 printk(KERN_ERR MOD "%s - failed to find dst entry!\n",
1396 __func__);
1397 goto reject;
1399 dst = &rt->u.dst;
1400 l2t = t3_l2t_get(tdev, dst->neighbour, dst->neighbour->dev);
1401 if (!l2t) {
1402 printk(KERN_ERR MOD "%s - failed to allocate l2t entry!\n",
1403 __func__);
1404 dst_release(dst);
1405 goto reject;
1407 child_ep = alloc_ep(sizeof(*child_ep), GFP_KERNEL);
1408 if (!child_ep) {
1409 printk(KERN_ERR MOD "%s - failed to allocate ep entry!\n",
1410 __func__);
1411 l2t_release(L2DATA(tdev), l2t);
1412 dst_release(dst);
1413 goto reject;
1415 state_set(&child_ep->com, CONNECTING);
1416 child_ep->com.tdev = tdev;
1417 child_ep->com.cm_id = NULL;
1418 child_ep->com.local_addr.sin_family = PF_INET;
1419 child_ep->com.local_addr.sin_port = req->local_port;
1420 child_ep->com.local_addr.sin_addr.s_addr = req->local_ip;
1421 child_ep->com.remote_addr.sin_family = PF_INET;
1422 child_ep->com.remote_addr.sin_port = req->peer_port;
1423 child_ep->com.remote_addr.sin_addr.s_addr = req->peer_ip;
1424 get_ep(&parent_ep->com);
1425 child_ep->parent_ep = parent_ep;
1426 child_ep->tos = G_PASS_OPEN_TOS(ntohl(req->tos_tid));
1427 child_ep->l2t = l2t;
1428 child_ep->dst = dst;
1429 child_ep->hwtid = hwtid;
1430 init_timer(&child_ep->timer);
1431 cxgb3_insert_tid(tdev, &t3c_client, child_ep, hwtid);
1432 accept_cr(child_ep, req->peer_ip, skb);
1433 goto out;
1434 reject:
1435 reject_cr(tdev, hwtid, req->peer_ip, skb);
1436 out:
1437 return CPL_RET_BUF_DONE;
1440 static int pass_establish(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1442 struct iwch_ep *ep = ctx;
1443 struct cpl_pass_establish *req = cplhdr(skb);
1445 PDBG("%s ep %p\n", __func__, ep);
1446 ep->snd_seq = ntohl(req->snd_isn);
1447 ep->rcv_seq = ntohl(req->rcv_isn);
1449 set_emss(ep, ntohs(req->tcp_opt));
1451 dst_confirm(ep->dst);
1452 state_set(&ep->com, MPA_REQ_WAIT);
1453 start_ep_timer(ep);
1455 return CPL_RET_BUF_DONE;
1458 static int peer_close(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1460 struct iwch_ep *ep = ctx;
1461 struct iwch_qp_attributes attrs;
1462 unsigned long flags;
1463 int disconnect = 1;
1464 int release = 0;
1466 PDBG("%s ep %p\n", __func__, ep);
1467 dst_confirm(ep->dst);
1469 spin_lock_irqsave(&ep->com.lock, flags);
1470 switch (ep->com.state) {
1471 case MPA_REQ_WAIT:
1472 __state_set(&ep->com, CLOSING);
1473 break;
1474 case MPA_REQ_SENT:
1475 __state_set(&ep->com, CLOSING);
1476 connect_reply_upcall(ep, -ECONNRESET);
1477 break;
1478 case MPA_REQ_RCVD:
1481 * We're gonna mark this puppy DEAD, but keep
1482 * the reference on it until the ULP accepts or
1483 * rejects the CR. Also wake up anyone waiting
1484 * in rdma connection migration (see iwch_accept_cr()).
1486 __state_set(&ep->com, CLOSING);
1487 ep->com.rpl_done = 1;
1488 ep->com.rpl_err = -ECONNRESET;
1489 PDBG("waking up ep %p\n", ep);
1490 wake_up(&ep->com.waitq);
1491 break;
1492 case MPA_REP_SENT:
1493 __state_set(&ep->com, CLOSING);
1494 ep->com.rpl_done = 1;
1495 ep->com.rpl_err = -ECONNRESET;
1496 PDBG("waking up ep %p\n", ep);
1497 wake_up(&ep->com.waitq);
1498 break;
1499 case FPDU_MODE:
1500 start_ep_timer(ep);
1501 __state_set(&ep->com, CLOSING);
1502 attrs.next_state = IWCH_QP_STATE_CLOSING;
1503 iwch_modify_qp(ep->com.qp->rhp, ep->com.qp,
1504 IWCH_QP_ATTR_NEXT_STATE, &attrs, 1);
1505 peer_close_upcall(ep);
1506 break;
1507 case ABORTING:
1508 disconnect = 0;
1509 break;
1510 case CLOSING:
1511 __state_set(&ep->com, MORIBUND);
1512 disconnect = 0;
1513 break;
1514 case MORIBUND:
1515 stop_ep_timer(ep);
1516 if (ep->com.cm_id && ep->com.qp) {
1517 attrs.next_state = IWCH_QP_STATE_IDLE;
1518 iwch_modify_qp(ep->com.qp->rhp, ep->com.qp,
1519 IWCH_QP_ATTR_NEXT_STATE, &attrs, 1);
1521 close_complete_upcall(ep);
1522 __state_set(&ep->com, DEAD);
1523 release = 1;
1524 disconnect = 0;
1525 break;
1526 case DEAD:
1527 disconnect = 0;
1528 break;
1529 default:
1530 BUG_ON(1);
1532 spin_unlock_irqrestore(&ep->com.lock, flags);
1533 if (disconnect)
1534 iwch_ep_disconnect(ep, 0, GFP_KERNEL);
1535 if (release)
1536 release_ep_resources(ep);
1537 return CPL_RET_BUF_DONE;
1541 * Returns whether an ABORT_REQ_RSS message is a negative advice.
1543 static int is_neg_adv_abort(unsigned int status)
1545 return status == CPL_ERR_RTX_NEG_ADVICE ||
1546 status == CPL_ERR_PERSIST_NEG_ADVICE;
1549 static int peer_abort(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1551 struct cpl_abort_req_rss *req = cplhdr(skb);
1552 struct iwch_ep *ep = ctx;
1553 struct cpl_abort_rpl *rpl;
1554 struct sk_buff *rpl_skb;
1555 struct iwch_qp_attributes attrs;
1556 int ret;
1557 int release = 0;
1558 unsigned long flags;
1560 if (is_neg_adv_abort(req->status)) {
1561 PDBG("%s neg_adv_abort ep %p tid %d\n", __func__, ep,
1562 ep->hwtid);
1563 t3_l2t_send_event(ep->com.tdev, ep->l2t);
1564 return CPL_RET_BUF_DONE;
1568 * We get 2 peer aborts from the HW. The first one must
1569 * be ignored except for scribbling that we need one more.
1571 if (!test_and_set_bit(PEER_ABORT_IN_PROGRESS, &ep->com.flags)) {
1572 return CPL_RET_BUF_DONE;
1575 spin_lock_irqsave(&ep->com.lock, flags);
1576 PDBG("%s ep %p state %u\n", __func__, ep, ep->com.state);
1577 switch (ep->com.state) {
1578 case CONNECTING:
1579 break;
1580 case MPA_REQ_WAIT:
1581 stop_ep_timer(ep);
1582 break;
1583 case MPA_REQ_SENT:
1584 stop_ep_timer(ep);
1585 connect_reply_upcall(ep, -ECONNRESET);
1586 break;
1587 case MPA_REP_SENT:
1588 ep->com.rpl_done = 1;
1589 ep->com.rpl_err = -ECONNRESET;
1590 PDBG("waking up ep %p\n", ep);
1591 wake_up(&ep->com.waitq);
1592 break;
1593 case MPA_REQ_RCVD:
1596 * We're gonna mark this puppy DEAD, but keep
1597 * the reference on it until the ULP accepts or
1598 * rejects the CR. Also wake up anyone waiting
1599 * in rdma connection migration (see iwch_accept_cr()).
1601 ep->com.rpl_done = 1;
1602 ep->com.rpl_err = -ECONNRESET;
1603 PDBG("waking up ep %p\n", ep);
1604 wake_up(&ep->com.waitq);
1605 break;
1606 case MORIBUND:
1607 case CLOSING:
1608 stop_ep_timer(ep);
1609 /*FALLTHROUGH*/
1610 case FPDU_MODE:
1611 if (ep->com.cm_id && ep->com.qp) {
1612 attrs.next_state = IWCH_QP_STATE_ERROR;
1613 ret = iwch_modify_qp(ep->com.qp->rhp,
1614 ep->com.qp, IWCH_QP_ATTR_NEXT_STATE,
1615 &attrs, 1);
1616 if (ret)
1617 printk(KERN_ERR MOD
1618 "%s - qp <- error failed!\n",
1619 __func__);
1621 peer_abort_upcall(ep);
1622 break;
1623 case ABORTING:
1624 break;
1625 case DEAD:
1626 PDBG("%s PEER_ABORT IN DEAD STATE!!!!\n", __func__);
1627 spin_unlock_irqrestore(&ep->com.lock, flags);
1628 return CPL_RET_BUF_DONE;
1629 default:
1630 BUG_ON(1);
1631 break;
1633 dst_confirm(ep->dst);
1634 if (ep->com.state != ABORTING) {
1635 __state_set(&ep->com, DEAD);
1636 release = 1;
1638 spin_unlock_irqrestore(&ep->com.lock, flags);
1640 rpl_skb = get_skb(skb, sizeof(*rpl), GFP_KERNEL);
1641 if (!rpl_skb) {
1642 printk(KERN_ERR MOD "%s - cannot allocate skb!\n",
1643 __func__);
1644 release = 1;
1645 goto out;
1647 rpl_skb->priority = CPL_PRIORITY_DATA;
1648 rpl = (struct cpl_abort_rpl *) skb_put(rpl_skb, sizeof(*rpl));
1649 rpl->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_HOST_ABORT_CON_RPL));
1650 rpl->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
1651 OPCODE_TID(rpl) = htonl(MK_OPCODE_TID(CPL_ABORT_RPL, ep->hwtid));
1652 rpl->cmd = CPL_ABORT_NO_RST;
1653 iwch_cxgb3_ofld_send(ep->com.tdev, rpl_skb);
1654 out:
1655 if (release)
1656 release_ep_resources(ep);
1657 return CPL_RET_BUF_DONE;
1660 static int close_con_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1662 struct iwch_ep *ep = ctx;
1663 struct iwch_qp_attributes attrs;
1664 unsigned long flags;
1665 int release = 0;
1667 PDBG("%s ep %p\n", __func__, ep);
1668 BUG_ON(!ep);
1670 /* The cm_id may be null if we failed to connect */
1671 spin_lock_irqsave(&ep->com.lock, flags);
1672 switch (ep->com.state) {
1673 case CLOSING:
1674 __state_set(&ep->com, MORIBUND);
1675 break;
1676 case MORIBUND:
1677 stop_ep_timer(ep);
1678 if ((ep->com.cm_id) && (ep->com.qp)) {
1679 attrs.next_state = IWCH_QP_STATE_IDLE;
1680 iwch_modify_qp(ep->com.qp->rhp,
1681 ep->com.qp,
1682 IWCH_QP_ATTR_NEXT_STATE,
1683 &attrs, 1);
1685 close_complete_upcall(ep);
1686 __state_set(&ep->com, DEAD);
1687 release = 1;
1688 break;
1689 case ABORTING:
1690 case DEAD:
1691 break;
1692 default:
1693 BUG_ON(1);
1694 break;
1696 spin_unlock_irqrestore(&ep->com.lock, flags);
1697 if (release)
1698 release_ep_resources(ep);
1699 return CPL_RET_BUF_DONE;
1703 * T3A does 3 things when a TERM is received:
1704 * 1) send up a CPL_RDMA_TERMINATE message with the TERM packet
1705 * 2) generate an async event on the QP with the TERMINATE opcode
1706 * 3) post a TERMINATE opcde cqe into the associated CQ.
1708 * For (1), we save the message in the qp for later consumer consumption.
1709 * For (2), we move the QP into TERMINATE, post a QP event and disconnect.
1710 * For (3), we toss the CQE in cxio_poll_cq().
1712 * terminate() handles case (1)...
1714 static int terminate(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1716 struct iwch_ep *ep = ctx;
1718 if (state_read(&ep->com) != FPDU_MODE)
1719 return CPL_RET_BUF_DONE;
1721 PDBG("%s ep %p\n", __func__, ep);
1722 skb_pull(skb, sizeof(struct cpl_rdma_terminate));
1723 PDBG("%s saving %d bytes of term msg\n", __func__, skb->len);
1724 skb_copy_from_linear_data(skb, ep->com.qp->attr.terminate_buffer,
1725 skb->len);
1726 ep->com.qp->attr.terminate_msg_len = skb->len;
1727 ep->com.qp->attr.is_terminate_local = 0;
1728 return CPL_RET_BUF_DONE;
1731 static int ec_status(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1733 struct cpl_rdma_ec_status *rep = cplhdr(skb);
1734 struct iwch_ep *ep = ctx;
1736 PDBG("%s ep %p tid %u status %d\n", __func__, ep, ep->hwtid,
1737 rep->status);
1738 if (rep->status) {
1739 struct iwch_qp_attributes attrs;
1741 printk(KERN_ERR MOD "%s BAD CLOSE - Aborting tid %u\n",
1742 __func__, ep->hwtid);
1743 stop_ep_timer(ep);
1744 attrs.next_state = IWCH_QP_STATE_ERROR;
1745 iwch_modify_qp(ep->com.qp->rhp,
1746 ep->com.qp, IWCH_QP_ATTR_NEXT_STATE,
1747 &attrs, 1);
1748 abort_connection(ep, NULL, GFP_KERNEL);
1750 return CPL_RET_BUF_DONE;
1753 static void ep_timeout(unsigned long arg)
1755 struct iwch_ep *ep = (struct iwch_ep *)arg;
1756 struct iwch_qp_attributes attrs;
1757 unsigned long flags;
1758 int abort = 1;
1760 spin_lock_irqsave(&ep->com.lock, flags);
1761 PDBG("%s ep %p tid %u state %d\n", __func__, ep, ep->hwtid,
1762 ep->com.state);
1763 switch (ep->com.state) {
1764 case MPA_REQ_SENT:
1765 __state_set(&ep->com, ABORTING);
1766 connect_reply_upcall(ep, -ETIMEDOUT);
1767 break;
1768 case MPA_REQ_WAIT:
1769 __state_set(&ep->com, ABORTING);
1770 break;
1771 case CLOSING:
1772 case MORIBUND:
1773 if (ep->com.cm_id && ep->com.qp) {
1774 attrs.next_state = IWCH_QP_STATE_ERROR;
1775 iwch_modify_qp(ep->com.qp->rhp,
1776 ep->com.qp, IWCH_QP_ATTR_NEXT_STATE,
1777 &attrs, 1);
1779 __state_set(&ep->com, ABORTING);
1780 break;
1781 default:
1782 printk(KERN_ERR "%s unexpected state ep %p state %u\n",
1783 __func__, ep, ep->com.state);
1784 WARN_ON(1);
1785 abort = 0;
1787 spin_unlock_irqrestore(&ep->com.lock, flags);
1788 if (abort)
1789 abort_connection(ep, NULL, GFP_ATOMIC);
1790 put_ep(&ep->com);
1793 int iwch_reject_cr(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
1795 int err;
1796 struct iwch_ep *ep = to_ep(cm_id);
1797 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1799 if (state_read(&ep->com) == DEAD) {
1800 put_ep(&ep->com);
1801 return -ECONNRESET;
1803 BUG_ON(state_read(&ep->com) != MPA_REQ_RCVD);
1804 if (mpa_rev == 0)
1805 abort_connection(ep, NULL, GFP_KERNEL);
1806 else {
1807 err = send_mpa_reject(ep, pdata, pdata_len);
1808 err = iwch_ep_disconnect(ep, 0, GFP_KERNEL);
1810 put_ep(&ep->com);
1811 return 0;
1814 int iwch_accept_cr(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
1816 int err;
1817 struct iwch_qp_attributes attrs;
1818 enum iwch_qp_attr_mask mask;
1819 struct iwch_ep *ep = to_ep(cm_id);
1820 struct iwch_dev *h = to_iwch_dev(cm_id->device);
1821 struct iwch_qp *qp = get_qhp(h, conn_param->qpn);
1823 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1824 if (state_read(&ep->com) == DEAD) {
1825 err = -ECONNRESET;
1826 goto err;
1829 BUG_ON(state_read(&ep->com) != MPA_REQ_RCVD);
1830 BUG_ON(!qp);
1832 if ((conn_param->ord > qp->rhp->attr.max_rdma_read_qp_depth) ||
1833 (conn_param->ird > qp->rhp->attr.max_rdma_reads_per_qp)) {
1834 abort_connection(ep, NULL, GFP_KERNEL);
1835 err = -EINVAL;
1836 goto err;
1839 cm_id->add_ref(cm_id);
1840 ep->com.cm_id = cm_id;
1841 ep->com.qp = qp;
1843 ep->ird = conn_param->ird;
1844 ep->ord = conn_param->ord;
1846 if (peer2peer && ep->ird == 0)
1847 ep->ird = 1;
1849 PDBG("%s %d ird %d ord %d\n", __func__, __LINE__, ep->ird, ep->ord);
1851 /* bind QP to EP and move to RTS */
1852 attrs.mpa_attr = ep->mpa_attr;
1853 attrs.max_ird = ep->ird;
1854 attrs.max_ord = ep->ord;
1855 attrs.llp_stream_handle = ep;
1856 attrs.next_state = IWCH_QP_STATE_RTS;
1858 /* bind QP and TID with INIT_WR */
1859 mask = IWCH_QP_ATTR_NEXT_STATE |
1860 IWCH_QP_ATTR_LLP_STREAM_HANDLE |
1861 IWCH_QP_ATTR_MPA_ATTR |
1862 IWCH_QP_ATTR_MAX_IRD |
1863 IWCH_QP_ATTR_MAX_ORD;
1865 err = iwch_modify_qp(ep->com.qp->rhp,
1866 ep->com.qp, mask, &attrs, 1);
1867 if (err)
1868 goto err1;
1870 /* if needed, wait for wr_ack */
1871 if (iwch_rqes_posted(qp)) {
1872 wait_event(ep->com.waitq, ep->com.rpl_done);
1873 err = ep->com.rpl_err;
1874 if (err)
1875 goto err1;
1878 err = send_mpa_reply(ep, conn_param->private_data,
1879 conn_param->private_data_len);
1880 if (err)
1881 goto err1;
1884 state_set(&ep->com, FPDU_MODE);
1885 established_upcall(ep);
1886 put_ep(&ep->com);
1887 return 0;
1888 err1:
1889 ep->com.cm_id = NULL;
1890 ep->com.qp = NULL;
1891 cm_id->rem_ref(cm_id);
1892 err:
1893 put_ep(&ep->com);
1894 return err;
1897 static int is_loopback_dst(struct iw_cm_id *cm_id)
1899 struct net_device *dev;
1901 dev = ip_dev_find(&init_net, cm_id->remote_addr.sin_addr.s_addr);
1902 if (!dev)
1903 return 0;
1904 dev_put(dev);
1905 return 1;
1908 int iwch_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
1910 int err = 0;
1911 struct iwch_dev *h = to_iwch_dev(cm_id->device);
1912 struct iwch_ep *ep;
1913 struct rtable *rt;
1915 if (is_loopback_dst(cm_id)) {
1916 err = -ENOSYS;
1917 goto out;
1920 ep = alloc_ep(sizeof(*ep), GFP_KERNEL);
1921 if (!ep) {
1922 printk(KERN_ERR MOD "%s - cannot alloc ep.\n", __func__);
1923 err = -ENOMEM;
1924 goto out;
1926 init_timer(&ep->timer);
1927 ep->plen = conn_param->private_data_len;
1928 if (ep->plen)
1929 memcpy(ep->mpa_pkt + sizeof(struct mpa_message),
1930 conn_param->private_data, ep->plen);
1931 ep->ird = conn_param->ird;
1932 ep->ord = conn_param->ord;
1934 if (peer2peer && ep->ord == 0)
1935 ep->ord = 1;
1937 ep->com.tdev = h->rdev.t3cdev_p;
1939 cm_id->add_ref(cm_id);
1940 ep->com.cm_id = cm_id;
1941 ep->com.qp = get_qhp(h, conn_param->qpn);
1942 BUG_ON(!ep->com.qp);
1943 PDBG("%s qpn 0x%x qp %p cm_id %p\n", __func__, conn_param->qpn,
1944 ep->com.qp, cm_id);
1947 * Allocate an active TID to initiate a TCP connection.
1949 ep->atid = cxgb3_alloc_atid(h->rdev.t3cdev_p, &t3c_client, ep);
1950 if (ep->atid == -1) {
1951 printk(KERN_ERR MOD "%s - cannot alloc atid.\n", __func__);
1952 err = -ENOMEM;
1953 goto fail2;
1956 /* find a route */
1957 rt = find_route(h->rdev.t3cdev_p,
1958 cm_id->local_addr.sin_addr.s_addr,
1959 cm_id->remote_addr.sin_addr.s_addr,
1960 cm_id->local_addr.sin_port,
1961 cm_id->remote_addr.sin_port, IPTOS_LOWDELAY);
1962 if (!rt) {
1963 printk(KERN_ERR MOD "%s - cannot find route.\n", __func__);
1964 err = -EHOSTUNREACH;
1965 goto fail3;
1967 ep->dst = &rt->u.dst;
1969 /* get a l2t entry */
1970 ep->l2t = t3_l2t_get(ep->com.tdev, ep->dst->neighbour,
1971 ep->dst->neighbour->dev);
1972 if (!ep->l2t) {
1973 printk(KERN_ERR MOD "%s - cannot alloc l2e.\n", __func__);
1974 err = -ENOMEM;
1975 goto fail4;
1978 state_set(&ep->com, CONNECTING);
1979 ep->tos = IPTOS_LOWDELAY;
1980 ep->com.local_addr = cm_id->local_addr;
1981 ep->com.remote_addr = cm_id->remote_addr;
1983 /* send connect request to rnic */
1984 err = send_connect(ep);
1985 if (!err)
1986 goto out;
1988 l2t_release(L2DATA(h->rdev.t3cdev_p), ep->l2t);
1989 fail4:
1990 dst_release(ep->dst);
1991 fail3:
1992 cxgb3_free_atid(ep->com.tdev, ep->atid);
1993 fail2:
1994 cm_id->rem_ref(cm_id);
1995 put_ep(&ep->com);
1996 out:
1997 return err;
2000 int iwch_create_listen(struct iw_cm_id *cm_id, int backlog)
2002 int err = 0;
2003 struct iwch_dev *h = to_iwch_dev(cm_id->device);
2004 struct iwch_listen_ep *ep;
2007 might_sleep();
2009 ep = alloc_ep(sizeof(*ep), GFP_KERNEL);
2010 if (!ep) {
2011 printk(KERN_ERR MOD "%s - cannot alloc ep.\n", __func__);
2012 err = -ENOMEM;
2013 goto fail1;
2015 PDBG("%s ep %p\n", __func__, ep);
2016 ep->com.tdev = h->rdev.t3cdev_p;
2017 cm_id->add_ref(cm_id);
2018 ep->com.cm_id = cm_id;
2019 ep->backlog = backlog;
2020 ep->com.local_addr = cm_id->local_addr;
2023 * Allocate a server TID.
2025 ep->stid = cxgb3_alloc_stid(h->rdev.t3cdev_p, &t3c_client, ep);
2026 if (ep->stid == -1) {
2027 printk(KERN_ERR MOD "%s - cannot alloc atid.\n", __func__);
2028 err = -ENOMEM;
2029 goto fail2;
2032 state_set(&ep->com, LISTEN);
2033 err = listen_start(ep);
2034 if (err)
2035 goto fail3;
2037 /* wait for pass_open_rpl */
2038 wait_event(ep->com.waitq, ep->com.rpl_done);
2039 err = ep->com.rpl_err;
2040 if (!err) {
2041 cm_id->provider_data = ep;
2042 goto out;
2044 fail3:
2045 cxgb3_free_stid(ep->com.tdev, ep->stid);
2046 fail2:
2047 cm_id->rem_ref(cm_id);
2048 put_ep(&ep->com);
2049 fail1:
2050 out:
2051 return err;
2054 int iwch_destroy_listen(struct iw_cm_id *cm_id)
2056 int err;
2057 struct iwch_listen_ep *ep = to_listen_ep(cm_id);
2059 PDBG("%s ep %p\n", __func__, ep);
2061 might_sleep();
2062 state_set(&ep->com, DEAD);
2063 ep->com.rpl_done = 0;
2064 ep->com.rpl_err = 0;
2065 err = listen_stop(ep);
2066 if (err)
2067 goto done;
2068 wait_event(ep->com.waitq, ep->com.rpl_done);
2069 cxgb3_free_stid(ep->com.tdev, ep->stid);
2070 done:
2071 err = ep->com.rpl_err;
2072 cm_id->rem_ref(cm_id);
2073 put_ep(&ep->com);
2074 return err;
2077 int iwch_ep_disconnect(struct iwch_ep *ep, int abrupt, gfp_t gfp)
2079 int ret=0;
2080 unsigned long flags;
2081 int close = 0;
2082 int fatal = 0;
2083 struct t3cdev *tdev;
2084 struct cxio_rdev *rdev;
2086 spin_lock_irqsave(&ep->com.lock, flags);
2088 PDBG("%s ep %p state %s, abrupt %d\n", __func__, ep,
2089 states[ep->com.state], abrupt);
2091 tdev = (struct t3cdev *)ep->com.tdev;
2092 rdev = (struct cxio_rdev *)tdev->ulp;
2093 if (cxio_fatal_error(rdev)) {
2094 fatal = 1;
2095 close_complete_upcall(ep);
2096 ep->com.state = DEAD;
2098 switch (ep->com.state) {
2099 case MPA_REQ_WAIT:
2100 case MPA_REQ_SENT:
2101 case MPA_REQ_RCVD:
2102 case MPA_REP_SENT:
2103 case FPDU_MODE:
2104 close = 1;
2105 if (abrupt)
2106 ep->com.state = ABORTING;
2107 else {
2108 ep->com.state = CLOSING;
2109 start_ep_timer(ep);
2111 set_bit(CLOSE_SENT, &ep->com.flags);
2112 break;
2113 case CLOSING:
2114 if (!test_and_set_bit(CLOSE_SENT, &ep->com.flags)) {
2115 close = 1;
2116 if (abrupt) {
2117 stop_ep_timer(ep);
2118 ep->com.state = ABORTING;
2119 } else
2120 ep->com.state = MORIBUND;
2122 break;
2123 case MORIBUND:
2124 case ABORTING:
2125 case DEAD:
2126 PDBG("%s ignoring disconnect ep %p state %u\n",
2127 __func__, ep, ep->com.state);
2128 break;
2129 default:
2130 BUG();
2131 break;
2134 spin_unlock_irqrestore(&ep->com.lock, flags);
2135 if (close) {
2136 if (abrupt)
2137 ret = send_abort(ep, NULL, gfp);
2138 else
2139 ret = send_halfclose(ep, gfp);
2140 if (ret)
2141 fatal = 1;
2143 if (fatal)
2144 release_ep_resources(ep);
2145 return ret;
2148 int iwch_ep_redirect(void *ctx, struct dst_entry *old, struct dst_entry *new,
2149 struct l2t_entry *l2t)
2151 struct iwch_ep *ep = ctx;
2153 if (ep->dst != old)
2154 return 0;
2156 PDBG("%s ep %p redirect to dst %p l2t %p\n", __func__, ep, new,
2157 l2t);
2158 dst_hold(new);
2159 l2t_release(L2DATA(ep->com.tdev), ep->l2t);
2160 ep->l2t = l2t;
2161 dst_release(old);
2162 ep->dst = new;
2163 return 1;
2167 * All the CM events are handled on a work queue to have a safe context.
2169 static int sched(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
2171 struct iwch_ep_common *epc = ctx;
2173 get_ep(epc);
2176 * Save ctx and tdev in the skb->cb area.
2178 *((void **) skb->cb) = ctx;
2179 *((struct t3cdev **) (skb->cb + sizeof(void *))) = tdev;
2182 * Queue the skb and schedule the worker thread.
2184 skb_queue_tail(&rxq, skb);
2185 queue_work(workq, &skb_work);
2186 return 0;
2189 static int set_tcb_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
2191 struct cpl_set_tcb_rpl *rpl = cplhdr(skb);
2193 if (rpl->status != CPL_ERR_NONE) {
2194 printk(KERN_ERR MOD "Unexpected SET_TCB_RPL status %u "
2195 "for tid %u\n", rpl->status, GET_TID(rpl));
2197 return CPL_RET_BUF_DONE;
2200 int __init iwch_cm_init(void)
2202 skb_queue_head_init(&rxq);
2204 workq = create_singlethread_workqueue("iw_cxgb3");
2205 if (!workq)
2206 return -ENOMEM;
2209 * All upcalls from the T3 Core go to sched() to
2210 * schedule the processing on a work queue.
2212 t3c_handlers[CPL_ACT_ESTABLISH] = sched;
2213 t3c_handlers[CPL_ACT_OPEN_RPL] = sched;
2214 t3c_handlers[CPL_RX_DATA] = sched;
2215 t3c_handlers[CPL_TX_DMA_ACK] = sched;
2216 t3c_handlers[CPL_ABORT_RPL_RSS] = sched;
2217 t3c_handlers[CPL_ABORT_RPL] = sched;
2218 t3c_handlers[CPL_PASS_OPEN_RPL] = sched;
2219 t3c_handlers[CPL_CLOSE_LISTSRV_RPL] = sched;
2220 t3c_handlers[CPL_PASS_ACCEPT_REQ] = sched;
2221 t3c_handlers[CPL_PASS_ESTABLISH] = sched;
2222 t3c_handlers[CPL_PEER_CLOSE] = sched;
2223 t3c_handlers[CPL_CLOSE_CON_RPL] = sched;
2224 t3c_handlers[CPL_ABORT_REQ_RSS] = sched;
2225 t3c_handlers[CPL_RDMA_TERMINATE] = sched;
2226 t3c_handlers[CPL_RDMA_EC_STATUS] = sched;
2227 t3c_handlers[CPL_SET_TCB_RPL] = set_tcb_rpl;
2230 * These are the real handlers that are called from a
2231 * work queue.
2233 work_handlers[CPL_ACT_ESTABLISH] = act_establish;
2234 work_handlers[CPL_ACT_OPEN_RPL] = act_open_rpl;
2235 work_handlers[CPL_RX_DATA] = rx_data;
2236 work_handlers[CPL_TX_DMA_ACK] = tx_ack;
2237 work_handlers[CPL_ABORT_RPL_RSS] = abort_rpl;
2238 work_handlers[CPL_ABORT_RPL] = abort_rpl;
2239 work_handlers[CPL_PASS_OPEN_RPL] = pass_open_rpl;
2240 work_handlers[CPL_CLOSE_LISTSRV_RPL] = close_listsrv_rpl;
2241 work_handlers[CPL_PASS_ACCEPT_REQ] = pass_accept_req;
2242 work_handlers[CPL_PASS_ESTABLISH] = pass_establish;
2243 work_handlers[CPL_PEER_CLOSE] = peer_close;
2244 work_handlers[CPL_ABORT_REQ_RSS] = peer_abort;
2245 work_handlers[CPL_CLOSE_CON_RPL] = close_con_rpl;
2246 work_handlers[CPL_RDMA_TERMINATE] = terminate;
2247 work_handlers[CPL_RDMA_EC_STATUS] = ec_status;
2248 return 0;
2251 void __exit iwch_cm_term(void)
2253 flush_workqueue(workq);
2254 destroy_workqueue(workq);