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
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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
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>
45 #include "cxgb3_offload.h"
47 #include "iwch_provider.h"
50 static char *states[] = {
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);
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);
138 del_timer_sync(&ep->timer);
142 static void release_tid(struct t3cdev *tdev, u32 hwtid, struct sk_buff *skb)
144 struct cpl_tid_release *req;
146 skb = get_skb(skb, sizeof *req, GFP_KERNEL);
149 req = (struct cpl_tid_release *) skb_put(skb, sizeof(*req));
150 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
151 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_TID_RELEASE, hwtid));
152 skb->priority = CPL_PRIORITY_SETUP;
153 cxgb3_ofld_send(tdev, skb);
157 int iwch_quiesce_tid(struct iwch_ep *ep)
159 struct cpl_set_tcb_field *req;
160 struct sk_buff *skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
164 req = (struct cpl_set_tcb_field *) skb_put(skb, sizeof(*req));
165 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
166 req->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
167 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, ep->hwtid));
170 req->word = htons(W_TCB_RX_QUIESCE);
171 req->mask = cpu_to_be64(1ULL << S_TCB_RX_QUIESCE);
172 req->val = cpu_to_be64(1 << S_TCB_RX_QUIESCE);
174 skb->priority = CPL_PRIORITY_DATA;
175 cxgb3_ofld_send(ep->com.tdev, skb);
179 int iwch_resume_tid(struct iwch_ep *ep)
181 struct cpl_set_tcb_field *req;
182 struct sk_buff *skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
186 req = (struct cpl_set_tcb_field *) skb_put(skb, sizeof(*req));
187 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
188 req->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
189 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, ep->hwtid));
192 req->word = htons(W_TCB_RX_QUIESCE);
193 req->mask = cpu_to_be64(1ULL << S_TCB_RX_QUIESCE);
196 skb->priority = CPL_PRIORITY_DATA;
197 cxgb3_ofld_send(ep->com.tdev, skb);
201 static void set_emss(struct iwch_ep *ep, u16 opt)
203 PDBG("%s ep %p opt %u\n", __func__, ep, opt);
204 ep->emss = T3C_DATA(ep->com.tdev)->mtus[G_TCPOPT_MSS(opt)] - 40;
205 if (G_TCPOPT_TSTAMP(opt))
209 PDBG("emss=%d\n", ep->emss);
212 static enum iwch_ep_state state_read(struct iwch_ep_common *epc)
215 enum iwch_ep_state state;
217 spin_lock_irqsave(&epc->lock, flags);
219 spin_unlock_irqrestore(&epc->lock, flags);
223 static void __state_set(struct iwch_ep_common *epc, enum iwch_ep_state new)
228 static void state_set(struct iwch_ep_common *epc, enum iwch_ep_state new)
232 spin_lock_irqsave(&epc->lock, flags);
233 PDBG("%s - %s -> %s\n", __func__, states[epc->state], states[new]);
234 __state_set(epc, new);
235 spin_unlock_irqrestore(&epc->lock, flags);
239 static void *alloc_ep(int size, gfp_t gfp)
241 struct iwch_ep_common *epc;
243 epc = kzalloc(size, gfp);
245 kref_init(&epc->kref);
246 spin_lock_init(&epc->lock);
247 init_waitqueue_head(&epc->waitq);
249 PDBG("%s alloc ep %p\n", __func__, epc);
253 void __free_ep(struct kref *kref)
255 struct iwch_ep_common *epc;
256 epc = container_of(kref, struct iwch_ep_common, kref);
257 PDBG("%s ep %p state %s\n", __func__, epc, states[state_read(epc)]);
261 static void release_ep_resources(struct iwch_ep *ep)
263 PDBG("%s ep %p tid %d\n", __func__, ep, ep->hwtid);
264 cxgb3_remove_tid(ep->com.tdev, (void *)ep, ep->hwtid);
265 dst_release(ep->dst);
266 l2t_release(L2DATA(ep->com.tdev), ep->l2t);
270 static void process_work(struct work_struct *work)
272 struct sk_buff *skb = NULL;
277 while ((skb = skb_dequeue(&rxq))) {
278 ep = *((void **) (skb->cb));
279 tdev = *((struct t3cdev **) (skb->cb + sizeof(void *)));
280 ret = work_handlers[G_OPCODE(ntohl((__force __be32)skb->csum))](tdev, skb, ep);
281 if (ret & CPL_RET_BUF_DONE)
285 * ep was referenced in sched(), and is freed here.
287 put_ep((struct iwch_ep_common *)ep);
291 static int status2errno(int status)
296 case CPL_ERR_CONN_RESET:
298 case CPL_ERR_ARP_MISS:
299 return -EHOSTUNREACH;
300 case CPL_ERR_CONN_TIMEDOUT:
302 case CPL_ERR_TCAM_FULL:
304 case CPL_ERR_CONN_EXIST:
312 * Try and reuse skbs already allocated...
314 static struct sk_buff *get_skb(struct sk_buff *skb, int len, gfp_t gfp)
316 if (skb && !skb_is_nonlinear(skb) && !skb_cloned(skb)) {
320 skb = alloc_skb(len, gfp);
325 static struct rtable *find_route(struct t3cdev *dev, __be32 local_ip,
326 __be32 peer_ip, __be16 local_port,
327 __be16 peer_port, u8 tos)
338 .proto = IPPROTO_TCP,
346 if (ip_route_output_flow(&init_net, &rt, &fl, NULL, 0))
351 static unsigned int find_best_mtu(const struct t3c_data *d, unsigned short mtu)
355 while (i < d->nmtus - 1 && d->mtus[i + 1] <= mtu)
360 static void arp_failure_discard(struct t3cdev *dev, struct sk_buff *skb)
362 PDBG("%s t3cdev %p\n", __func__, dev);
367 * Handle an ARP failure for an active open.
369 static void act_open_req_arp_failure(struct t3cdev *dev, struct sk_buff *skb)
371 printk(KERN_ERR MOD "ARP failure duing connect\n");
376 * Handle an ARP failure for a CPL_ABORT_REQ. Change it into a no RST variant
379 static void abort_arp_failure(struct t3cdev *dev, struct sk_buff *skb)
381 struct cpl_abort_req *req = cplhdr(skb);
383 PDBG("%s t3cdev %p\n", __func__, dev);
384 req->cmd = CPL_ABORT_NO_RST;
385 cxgb3_ofld_send(dev, skb);
388 static int send_halfclose(struct iwch_ep *ep, gfp_t gfp)
390 struct cpl_close_con_req *req;
393 PDBG("%s ep %p\n", __func__, ep);
394 skb = get_skb(NULL, sizeof(*req), gfp);
396 printk(KERN_ERR MOD "%s - failed to alloc skb\n", __func__);
399 skb->priority = CPL_PRIORITY_DATA;
400 set_arp_failure_handler(skb, arp_failure_discard);
401 req = (struct cpl_close_con_req *) skb_put(skb, sizeof(*req));
402 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_CLOSE_CON));
403 req->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
404 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_CON_REQ, ep->hwtid));
405 l2t_send(ep->com.tdev, skb, ep->l2t);
409 static int send_abort(struct iwch_ep *ep, struct sk_buff *skb, gfp_t gfp)
411 struct cpl_abort_req *req;
413 PDBG("%s ep %p\n", __func__, ep);
414 skb = get_skb(skb, sizeof(*req), gfp);
416 printk(KERN_ERR MOD "%s - failed to alloc skb.\n",
420 skb->priority = CPL_PRIORITY_DATA;
421 set_arp_failure_handler(skb, abort_arp_failure);
422 req = (struct cpl_abort_req *) skb_put(skb, sizeof(*req));
423 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_HOST_ABORT_CON_REQ));
424 req->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
425 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_ABORT_REQ, ep->hwtid));
426 req->cmd = CPL_ABORT_SEND_RST;
427 l2t_send(ep->com.tdev, skb, ep->l2t);
431 static int send_connect(struct iwch_ep *ep)
433 struct cpl_act_open_req *req;
435 u32 opt0h, opt0l, opt2;
436 unsigned int mtu_idx;
439 PDBG("%s ep %p\n", __func__, ep);
441 skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
443 printk(KERN_ERR MOD "%s - failed to alloc skb.\n",
447 mtu_idx = find_best_mtu(T3C_DATA(ep->com.tdev), dst_mtu(ep->dst));
448 wscale = compute_wscale(rcv_win);
453 V_WND_SCALE(wscale) |
455 V_L2T_IDX(ep->l2t->idx) | V_TX_CHANNEL(ep->l2t->smt_idx);
456 opt0l = V_TOS((ep->tos >> 2) & M_TOS) | V_RCV_BUFSIZ(rcv_win>>10);
457 opt2 = V_FLAVORS_VALID(1) | V_CONG_CONTROL_FLAVOR(cong_flavor);
458 skb->priority = CPL_PRIORITY_SETUP;
459 set_arp_failure_handler(skb, act_open_req_arp_failure);
461 req = (struct cpl_act_open_req *) skb_put(skb, sizeof(*req));
462 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
463 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_ACT_OPEN_REQ, ep->atid));
464 req->local_port = ep->com.local_addr.sin_port;
465 req->peer_port = ep->com.remote_addr.sin_port;
466 req->local_ip = ep->com.local_addr.sin_addr.s_addr;
467 req->peer_ip = ep->com.remote_addr.sin_addr.s_addr;
468 req->opt0h = htonl(opt0h);
469 req->opt0l = htonl(opt0l);
471 req->opt2 = htonl(opt2);
472 l2t_send(ep->com.tdev, skb, ep->l2t);
476 static void send_mpa_req(struct iwch_ep *ep, struct sk_buff *skb)
479 struct tx_data_wr *req;
480 struct mpa_message *mpa;
483 PDBG("%s ep %p pd_len %d\n", __func__, ep, ep->plen);
485 BUG_ON(skb_cloned(skb));
487 mpalen = sizeof(*mpa) + ep->plen;
488 if (skb->data + mpalen + sizeof(*req) > skb_end_pointer(skb)) {
490 skb=alloc_skb(mpalen + sizeof(*req), GFP_KERNEL);
492 connect_reply_upcall(ep, -ENOMEM);
497 skb_reserve(skb, sizeof(*req));
498 skb_put(skb, mpalen);
499 skb->priority = CPL_PRIORITY_DATA;
500 mpa = (struct mpa_message *) skb->data;
501 memset(mpa, 0, sizeof(*mpa));
502 memcpy(mpa->key, MPA_KEY_REQ, sizeof(mpa->key));
503 mpa->flags = (crc_enabled ? MPA_CRC : 0) |
504 (markers_enabled ? MPA_MARKERS : 0);
505 mpa->private_data_size = htons(ep->plen);
506 mpa->revision = mpa_rev;
509 memcpy(mpa->private_data, ep->mpa_pkt + sizeof(*mpa), ep->plen);
512 * Reference the mpa skb. This ensures the data area
513 * will remain in memory until the hw acks the tx.
514 * Function tx_ack() will deref it.
517 set_arp_failure_handler(skb, arp_failure_discard);
518 skb_reset_transport_header(skb);
520 req = (struct tx_data_wr *) skb_push(skb, sizeof(*req));
521 req->wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_TX_DATA)|F_WR_COMPL);
522 req->wr_lo = htonl(V_WR_TID(ep->hwtid));
523 req->len = htonl(len);
524 req->param = htonl(V_TX_PORT(ep->l2t->smt_idx) |
525 V_TX_SNDBUF(snd_win>>15));
526 req->flags = htonl(F_TX_INIT);
527 req->sndseq = htonl(ep->snd_seq);
530 l2t_send(ep->com.tdev, skb, ep->l2t);
532 state_set(&ep->com, MPA_REQ_SENT);
536 static int send_mpa_reject(struct iwch_ep *ep, const void *pdata, u8 plen)
539 struct tx_data_wr *req;
540 struct mpa_message *mpa;
543 PDBG("%s ep %p plen %d\n", __func__, ep, plen);
545 mpalen = sizeof(*mpa) + plen;
547 skb = get_skb(NULL, mpalen + sizeof(*req), GFP_KERNEL);
549 printk(KERN_ERR MOD "%s - cannot alloc skb!\n", __func__);
552 skb_reserve(skb, sizeof(*req));
553 mpa = (struct mpa_message *) skb_put(skb, mpalen);
554 memset(mpa, 0, sizeof(*mpa));
555 memcpy(mpa->key, MPA_KEY_REP, sizeof(mpa->key));
556 mpa->flags = MPA_REJECT;
557 mpa->revision = mpa_rev;
558 mpa->private_data_size = htons(plen);
560 memcpy(mpa->private_data, pdata, plen);
563 * Reference the mpa skb again. This ensures the data area
564 * will remain in memory until the hw acks the tx.
565 * Function tx_ack() will deref it.
568 skb->priority = CPL_PRIORITY_DATA;
569 set_arp_failure_handler(skb, arp_failure_discard);
570 skb_reset_transport_header(skb);
571 req = (struct tx_data_wr *) skb_push(skb, sizeof(*req));
572 req->wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_TX_DATA)|F_WR_COMPL);
573 req->wr_lo = htonl(V_WR_TID(ep->hwtid));
574 req->len = htonl(mpalen);
575 req->param = htonl(V_TX_PORT(ep->l2t->smt_idx) |
576 V_TX_SNDBUF(snd_win>>15));
577 req->flags = htonl(F_TX_INIT);
578 req->sndseq = htonl(ep->snd_seq);
581 l2t_send(ep->com.tdev, skb, ep->l2t);
585 static int send_mpa_reply(struct iwch_ep *ep, const void *pdata, u8 plen)
588 struct tx_data_wr *req;
589 struct mpa_message *mpa;
593 PDBG("%s ep %p plen %d\n", __func__, ep, plen);
595 mpalen = sizeof(*mpa) + plen;
597 skb = get_skb(NULL, mpalen + sizeof(*req), GFP_KERNEL);
599 printk(KERN_ERR MOD "%s - cannot alloc skb!\n", __func__);
602 skb->priority = CPL_PRIORITY_DATA;
603 skb_reserve(skb, sizeof(*req));
604 mpa = (struct mpa_message *) skb_put(skb, mpalen);
605 memset(mpa, 0, sizeof(*mpa));
606 memcpy(mpa->key, MPA_KEY_REP, sizeof(mpa->key));
607 mpa->flags = (ep->mpa_attr.crc_enabled ? MPA_CRC : 0) |
608 (markers_enabled ? MPA_MARKERS : 0);
609 mpa->revision = mpa_rev;
610 mpa->private_data_size = htons(plen);
612 memcpy(mpa->private_data, pdata, plen);
615 * Reference the mpa skb. This ensures the data area
616 * will remain in memory until the hw acks the tx.
617 * Function tx_ack() will deref it.
620 set_arp_failure_handler(skb, arp_failure_discard);
621 skb_reset_transport_header(skb);
623 req = (struct tx_data_wr *) skb_push(skb, sizeof(*req));
624 req->wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_TX_DATA)|F_WR_COMPL);
625 req->wr_lo = htonl(V_WR_TID(ep->hwtid));
626 req->len = htonl(len);
627 req->param = htonl(V_TX_PORT(ep->l2t->smt_idx) |
628 V_TX_SNDBUF(snd_win>>15));
629 req->flags = htonl(F_TX_INIT);
630 req->sndseq = htonl(ep->snd_seq);
632 state_set(&ep->com, MPA_REP_SENT);
633 l2t_send(ep->com.tdev, skb, ep->l2t);
637 static int act_establish(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
639 struct iwch_ep *ep = ctx;
640 struct cpl_act_establish *req = cplhdr(skb);
641 unsigned int tid = GET_TID(req);
643 PDBG("%s ep %p tid %d\n", __func__, ep, tid);
645 dst_confirm(ep->dst);
647 /* setup the hwtid for this connection */
649 cxgb3_insert_tid(ep->com.tdev, &t3c_client, ep, tid);
651 ep->snd_seq = ntohl(req->snd_isn);
652 ep->rcv_seq = ntohl(req->rcv_isn);
654 set_emss(ep, ntohs(req->tcp_opt));
656 /* dealloc the atid */
657 cxgb3_free_atid(ep->com.tdev, ep->atid);
659 /* start MPA negotiation */
660 send_mpa_req(ep, skb);
665 static void abort_connection(struct iwch_ep *ep, struct sk_buff *skb, gfp_t gfp)
667 PDBG("%s ep %p\n", __FILE__, ep);
668 state_set(&ep->com, ABORTING);
669 send_abort(ep, skb, gfp);
672 static void close_complete_upcall(struct iwch_ep *ep)
674 struct iw_cm_event event;
676 PDBG("%s ep %p\n", __func__, ep);
677 memset(&event, 0, sizeof(event));
678 event.event = IW_CM_EVENT_CLOSE;
680 PDBG("close complete delivered ep %p cm_id %p tid %d\n",
681 ep, ep->com.cm_id, ep->hwtid);
682 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
683 ep->com.cm_id->rem_ref(ep->com.cm_id);
684 ep->com.cm_id = NULL;
689 static void peer_close_upcall(struct iwch_ep *ep)
691 struct iw_cm_event event;
693 PDBG("%s ep %p\n", __func__, ep);
694 memset(&event, 0, sizeof(event));
695 event.event = IW_CM_EVENT_DISCONNECT;
697 PDBG("peer close delivered ep %p cm_id %p tid %d\n",
698 ep, ep->com.cm_id, ep->hwtid);
699 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
703 static void peer_abort_upcall(struct iwch_ep *ep)
705 struct iw_cm_event event;
707 PDBG("%s ep %p\n", __func__, ep);
708 memset(&event, 0, sizeof(event));
709 event.event = IW_CM_EVENT_CLOSE;
710 event.status = -ECONNRESET;
712 PDBG("abort delivered ep %p cm_id %p tid %d\n", ep,
713 ep->com.cm_id, ep->hwtid);
714 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
715 ep->com.cm_id->rem_ref(ep->com.cm_id);
716 ep->com.cm_id = NULL;
721 static void connect_reply_upcall(struct iwch_ep *ep, int status)
723 struct iw_cm_event event;
725 PDBG("%s ep %p status %d\n", __func__, ep, status);
726 memset(&event, 0, sizeof(event));
727 event.event = IW_CM_EVENT_CONNECT_REPLY;
728 event.status = status;
729 event.local_addr = ep->com.local_addr;
730 event.remote_addr = ep->com.remote_addr;
732 if ((status == 0) || (status == -ECONNREFUSED)) {
733 event.private_data_len = ep->plen;
734 event.private_data = ep->mpa_pkt + sizeof(struct mpa_message);
737 PDBG("%s ep %p tid %d status %d\n", __func__, ep,
739 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
742 ep->com.cm_id->rem_ref(ep->com.cm_id);
743 ep->com.cm_id = NULL;
748 static void connect_request_upcall(struct iwch_ep *ep)
750 struct iw_cm_event event;
752 PDBG("%s ep %p tid %d\n", __func__, ep, ep->hwtid);
753 memset(&event, 0, sizeof(event));
754 event.event = IW_CM_EVENT_CONNECT_REQUEST;
755 event.local_addr = ep->com.local_addr;
756 event.remote_addr = ep->com.remote_addr;
757 event.private_data_len = ep->plen;
758 event.private_data = ep->mpa_pkt + sizeof(struct mpa_message);
759 event.provider_data = ep;
760 if (state_read(&ep->parent_ep->com) != DEAD)
761 ep->parent_ep->com.cm_id->event_handler(
762 ep->parent_ep->com.cm_id,
764 put_ep(&ep->parent_ep->com);
765 ep->parent_ep = NULL;
768 static void established_upcall(struct iwch_ep *ep)
770 struct iw_cm_event event;
772 PDBG("%s ep %p\n", __func__, ep);
773 memset(&event, 0, sizeof(event));
774 event.event = IW_CM_EVENT_ESTABLISHED;
776 PDBG("%s ep %p tid %d\n", __func__, ep, ep->hwtid);
777 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
781 static int update_rx_credits(struct iwch_ep *ep, u32 credits)
783 struct cpl_rx_data_ack *req;
786 PDBG("%s ep %p credits %u\n", __func__, ep, credits);
787 skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
789 printk(KERN_ERR MOD "update_rx_credits - cannot alloc skb!\n");
793 req = (struct cpl_rx_data_ack *) skb_put(skb, sizeof(*req));
794 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
795 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_RX_DATA_ACK, ep->hwtid));
796 req->credit_dack = htonl(V_RX_CREDITS(credits) | V_RX_FORCE_ACK(1));
797 skb->priority = CPL_PRIORITY_ACK;
798 cxgb3_ofld_send(ep->com.tdev, skb);
802 static void process_mpa_reply(struct iwch_ep *ep, struct sk_buff *skb)
804 struct mpa_message *mpa;
806 struct iwch_qp_attributes attrs;
807 enum iwch_qp_attr_mask mask;
810 PDBG("%s ep %p\n", __func__, ep);
813 * Stop mpa timer. If it expired, then the state has
814 * changed and we bail since ep_timeout already aborted
818 if (state_read(&ep->com) != MPA_REQ_SENT)
822 * If we get more than the supported amount of private data
823 * then we must fail this connection.
825 if (ep->mpa_pkt_len + skb->len > sizeof(ep->mpa_pkt)) {
831 * copy the new data into our accumulation buffer.
833 skb_copy_from_linear_data(skb, &(ep->mpa_pkt[ep->mpa_pkt_len]),
835 ep->mpa_pkt_len += skb->len;
838 * if we don't even have the mpa message, then bail.
840 if (ep->mpa_pkt_len < sizeof(*mpa))
842 mpa = (struct mpa_message *) ep->mpa_pkt;
844 /* Validate MPA header. */
845 if (mpa->revision != mpa_rev) {
849 if (memcmp(mpa->key, MPA_KEY_REP, sizeof(mpa->key))) {
854 plen = ntohs(mpa->private_data_size);
857 * Fail if there's too much private data.
859 if (plen > MPA_MAX_PRIVATE_DATA) {
865 * If plen does not account for pkt size
867 if (ep->mpa_pkt_len > (sizeof(*mpa) + plen)) {
872 ep->plen = (u8) plen;
875 * If we don't have all the pdata yet, then bail.
876 * We'll continue process when more data arrives.
878 if (ep->mpa_pkt_len < (sizeof(*mpa) + plen))
881 if (mpa->flags & MPA_REJECT) {
887 * If we get here we have accumulated the entire mpa
888 * start reply message including private data. And
889 * the MPA header is valid.
891 state_set(&ep->com, FPDU_MODE);
892 ep->mpa_attr.initiator = 1;
893 ep->mpa_attr.crc_enabled = (mpa->flags & MPA_CRC) | crc_enabled ? 1 : 0;
894 ep->mpa_attr.recv_marker_enabled = markers_enabled;
895 ep->mpa_attr.xmit_marker_enabled = mpa->flags & MPA_MARKERS ? 1 : 0;
896 ep->mpa_attr.version = mpa_rev;
897 PDBG("%s - crc_enabled=%d, recv_marker_enabled=%d, "
898 "xmit_marker_enabled=%d, version=%d\n", __func__,
899 ep->mpa_attr.crc_enabled, ep->mpa_attr.recv_marker_enabled,
900 ep->mpa_attr.xmit_marker_enabled, ep->mpa_attr.version);
902 attrs.mpa_attr = ep->mpa_attr;
903 attrs.max_ird = ep->ird;
904 attrs.max_ord = ep->ord;
905 attrs.llp_stream_handle = ep;
906 attrs.next_state = IWCH_QP_STATE_RTS;
908 mask = IWCH_QP_ATTR_NEXT_STATE |
909 IWCH_QP_ATTR_LLP_STREAM_HANDLE | IWCH_QP_ATTR_MPA_ATTR |
910 IWCH_QP_ATTR_MAX_IRD | IWCH_QP_ATTR_MAX_ORD;
912 /* bind QP and TID with INIT_WR */
913 err = iwch_modify_qp(ep->com.qp->rhp,
914 ep->com.qp, mask, &attrs, 1);
918 if (peer2peer && iwch_rqes_posted(ep->com.qp) == 0) {
919 iwch_post_zb_read(ep->com.qp);
924 abort_connection(ep, skb, GFP_KERNEL);
926 connect_reply_upcall(ep, err);
930 static void process_mpa_request(struct iwch_ep *ep, struct sk_buff *skb)
932 struct mpa_message *mpa;
935 PDBG("%s ep %p\n", __func__, ep);
938 * Stop mpa timer. If it expired, then the state has
939 * changed and we bail since ep_timeout already aborted
943 if (state_read(&ep->com) != MPA_REQ_WAIT)
947 * If we get more than the supported amount of private data
948 * then we must fail this connection.
950 if (ep->mpa_pkt_len + skb->len > sizeof(ep->mpa_pkt)) {
951 abort_connection(ep, skb, GFP_KERNEL);
955 PDBG("%s enter (%s line %u)\n", __func__, __FILE__, __LINE__);
958 * Copy the new data into our accumulation buffer.
960 skb_copy_from_linear_data(skb, &(ep->mpa_pkt[ep->mpa_pkt_len]),
962 ep->mpa_pkt_len += skb->len;
965 * If we don't even have the mpa message, then bail.
966 * We'll continue process when more data arrives.
968 if (ep->mpa_pkt_len < sizeof(*mpa))
970 PDBG("%s enter (%s line %u)\n", __func__, __FILE__, __LINE__);
971 mpa = (struct mpa_message *) ep->mpa_pkt;
974 * Validate MPA Header.
976 if (mpa->revision != mpa_rev) {
977 abort_connection(ep, skb, GFP_KERNEL);
981 if (memcmp(mpa->key, MPA_KEY_REQ, sizeof(mpa->key))) {
982 abort_connection(ep, skb, GFP_KERNEL);
986 plen = ntohs(mpa->private_data_size);
989 * Fail if there's too much private data.
991 if (plen > MPA_MAX_PRIVATE_DATA) {
992 abort_connection(ep, skb, GFP_KERNEL);
997 * If plen does not account for pkt size
999 if (ep->mpa_pkt_len > (sizeof(*mpa) + plen)) {
1000 abort_connection(ep, skb, GFP_KERNEL);
1003 ep->plen = (u8) plen;
1006 * If we don't have all the pdata yet, then bail.
1008 if (ep->mpa_pkt_len < (sizeof(*mpa) + plen))
1012 * If we get here we have accumulated the entire mpa
1013 * start reply message including private data.
1015 ep->mpa_attr.initiator = 0;
1016 ep->mpa_attr.crc_enabled = (mpa->flags & MPA_CRC) | crc_enabled ? 1 : 0;
1017 ep->mpa_attr.recv_marker_enabled = markers_enabled;
1018 ep->mpa_attr.xmit_marker_enabled = mpa->flags & MPA_MARKERS ? 1 : 0;
1019 ep->mpa_attr.version = mpa_rev;
1020 PDBG("%s - crc_enabled=%d, recv_marker_enabled=%d, "
1021 "xmit_marker_enabled=%d, version=%d\n", __func__,
1022 ep->mpa_attr.crc_enabled, ep->mpa_attr.recv_marker_enabled,
1023 ep->mpa_attr.xmit_marker_enabled, ep->mpa_attr.version);
1025 state_set(&ep->com, MPA_REQ_RCVD);
1028 connect_request_upcall(ep);
1032 static int rx_data(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1034 struct iwch_ep *ep = ctx;
1035 struct cpl_rx_data *hdr = cplhdr(skb);
1036 unsigned int dlen = ntohs(hdr->len);
1038 PDBG("%s ep %p dlen %u\n", __func__, ep, dlen);
1040 skb_pull(skb, sizeof(*hdr));
1041 skb_trim(skb, dlen);
1043 ep->rcv_seq += dlen;
1044 BUG_ON(ep->rcv_seq != (ntohl(hdr->seq) + dlen));
1046 switch (state_read(&ep->com)) {
1048 process_mpa_reply(ep, skb);
1051 process_mpa_request(ep, skb);
1056 printk(KERN_ERR MOD "%s Unexpected streaming data."
1057 " ep %p state %d tid %d\n",
1058 __func__, ep, state_read(&ep->com), ep->hwtid);
1061 * The ep will timeout and inform the ULP of the failure.
1067 /* update RX credits */
1068 update_rx_credits(ep, dlen);
1070 return CPL_RET_BUF_DONE;
1074 * Upcall from the adapter indicating data has been transmitted.
1075 * For us its just the single MPA request or reply. We can now free
1076 * the skb holding the mpa message.
1078 static int tx_ack(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1080 struct iwch_ep *ep = ctx;
1081 struct cpl_wr_ack *hdr = cplhdr(skb);
1082 unsigned int credits = ntohs(hdr->credits);
1084 PDBG("%s ep %p credits %u\n", __func__, ep, credits);
1087 PDBG(KERN_ERR "%s 0 credit ack ep %p state %u\n",
1088 __func__, ep, state_read(&ep->com));
1089 return CPL_RET_BUF_DONE;
1092 BUG_ON(credits != 1);
1093 dst_confirm(ep->dst);
1095 PDBG("%s rdma_init wr_ack ep %p state %u\n",
1096 __func__, ep, state_read(&ep->com));
1097 if (ep->mpa_attr.initiator) {
1098 PDBG("%s initiator ep %p state %u\n",
1099 __func__, ep, state_read(&ep->com));
1101 iwch_post_zb_read(ep->com.qp);
1103 PDBG("%s responder ep %p state %u\n",
1104 __func__, ep, state_read(&ep->com));
1105 ep->com.rpl_done = 1;
1106 wake_up(&ep->com.waitq);
1109 PDBG("%s lsm ack ep %p state %u freeing skb\n",
1110 __func__, ep, state_read(&ep->com));
1111 kfree_skb(ep->mpa_skb);
1114 return CPL_RET_BUF_DONE;
1117 static int abort_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1119 struct iwch_ep *ep = ctx;
1120 unsigned long flags;
1123 PDBG("%s ep %p\n", __func__, ep);
1127 * We get 2 abort replies from the HW. The first one must
1128 * be ignored except for scribbling that we need one more.
1130 if (!(ep->flags & ABORT_REQ_IN_PROGRESS)) {
1131 ep->flags |= ABORT_REQ_IN_PROGRESS;
1132 return CPL_RET_BUF_DONE;
1135 spin_lock_irqsave(&ep->com.lock, flags);
1136 switch (ep->com.state) {
1138 close_complete_upcall(ep);
1139 __state_set(&ep->com, DEAD);
1143 printk(KERN_ERR "%s ep %p state %d\n",
1144 __func__, ep, ep->com.state);
1147 spin_unlock_irqrestore(&ep->com.lock, flags);
1150 release_ep_resources(ep);
1151 return CPL_RET_BUF_DONE;
1155 * Return whether a failed active open has allocated a TID
1157 static inline int act_open_has_tid(int status)
1159 return status != CPL_ERR_TCAM_FULL && status != CPL_ERR_CONN_EXIST &&
1160 status != CPL_ERR_ARP_MISS;
1163 static int act_open_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1165 struct iwch_ep *ep = ctx;
1166 struct cpl_act_open_rpl *rpl = cplhdr(skb);
1168 PDBG("%s ep %p status %u errno %d\n", __func__, ep, rpl->status,
1169 status2errno(rpl->status));
1170 connect_reply_upcall(ep, status2errno(rpl->status));
1171 state_set(&ep->com, DEAD);
1172 if (ep->com.tdev->type != T3A && act_open_has_tid(rpl->status))
1173 release_tid(ep->com.tdev, GET_TID(rpl), NULL);
1174 cxgb3_free_atid(ep->com.tdev, ep->atid);
1175 dst_release(ep->dst);
1176 l2t_release(L2DATA(ep->com.tdev), ep->l2t);
1178 return CPL_RET_BUF_DONE;
1181 static int listen_start(struct iwch_listen_ep *ep)
1183 struct sk_buff *skb;
1184 struct cpl_pass_open_req *req;
1186 PDBG("%s ep %p\n", __func__, ep);
1187 skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
1189 printk(KERN_ERR MOD "t3c_listen_start failed to alloc skb!\n");
1193 req = (struct cpl_pass_open_req *) skb_put(skb, sizeof(*req));
1194 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
1195 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ, ep->stid));
1196 req->local_port = ep->com.local_addr.sin_port;
1197 req->local_ip = ep->com.local_addr.sin_addr.s_addr;
1200 req->peer_netmask = 0;
1201 req->opt0h = htonl(F_DELACK | F_TCAM_BYPASS);
1202 req->opt0l = htonl(V_RCV_BUFSIZ(rcv_win>>10));
1203 req->opt1 = htonl(V_CONN_POLICY(CPL_CONN_POLICY_ASK));
1206 cxgb3_ofld_send(ep->com.tdev, skb);
1210 static int pass_open_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1212 struct iwch_listen_ep *ep = ctx;
1213 struct cpl_pass_open_rpl *rpl = cplhdr(skb);
1215 PDBG("%s ep %p status %d error %d\n", __func__, ep,
1216 rpl->status, status2errno(rpl->status));
1217 ep->com.rpl_err = status2errno(rpl->status);
1218 ep->com.rpl_done = 1;
1219 wake_up(&ep->com.waitq);
1221 return CPL_RET_BUF_DONE;
1224 static int listen_stop(struct iwch_listen_ep *ep)
1226 struct sk_buff *skb;
1227 struct cpl_close_listserv_req *req;
1229 PDBG("%s ep %p\n", __func__, ep);
1230 skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
1232 printk(KERN_ERR MOD "%s - failed to alloc skb\n", __func__);
1235 req = (struct cpl_close_listserv_req *) skb_put(skb, sizeof(*req));
1236 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
1238 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_LISTSRV_REQ, ep->stid));
1240 cxgb3_ofld_send(ep->com.tdev, skb);
1244 static int close_listsrv_rpl(struct t3cdev *tdev, struct sk_buff *skb,
1247 struct iwch_listen_ep *ep = ctx;
1248 struct cpl_close_listserv_rpl *rpl = cplhdr(skb);
1250 PDBG("%s ep %p\n", __func__, ep);
1251 ep->com.rpl_err = status2errno(rpl->status);
1252 ep->com.rpl_done = 1;
1253 wake_up(&ep->com.waitq);
1254 return CPL_RET_BUF_DONE;
1257 static void accept_cr(struct iwch_ep *ep, __be32 peer_ip, struct sk_buff *skb)
1259 struct cpl_pass_accept_rpl *rpl;
1260 unsigned int mtu_idx;
1261 u32 opt0h, opt0l, opt2;
1264 PDBG("%s ep %p\n", __func__, ep);
1265 BUG_ON(skb_cloned(skb));
1266 skb_trim(skb, sizeof(*rpl));
1268 mtu_idx = find_best_mtu(T3C_DATA(ep->com.tdev), dst_mtu(ep->dst));
1269 wscale = compute_wscale(rcv_win);
1270 opt0h = V_NAGLE(0) |
1274 V_WND_SCALE(wscale) |
1275 V_MSS_IDX(mtu_idx) |
1276 V_L2T_IDX(ep->l2t->idx) | V_TX_CHANNEL(ep->l2t->smt_idx);
1277 opt0l = V_TOS((ep->tos >> 2) & M_TOS) | V_RCV_BUFSIZ(rcv_win>>10);
1278 opt2 = V_FLAVORS_VALID(1) | V_CONG_CONTROL_FLAVOR(cong_flavor);
1281 rpl->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
1282 OPCODE_TID(rpl) = htonl(MK_OPCODE_TID(CPL_PASS_ACCEPT_RPL, ep->hwtid));
1283 rpl->peer_ip = peer_ip;
1284 rpl->opt0h = htonl(opt0h);
1285 rpl->opt0l_status = htonl(opt0l | CPL_PASS_OPEN_ACCEPT);
1286 rpl->opt2 = htonl(opt2);
1287 rpl->rsvd = rpl->opt2; /* workaround for HW bug */
1288 skb->priority = CPL_PRIORITY_SETUP;
1289 l2t_send(ep->com.tdev, skb, ep->l2t);
1294 static void reject_cr(struct t3cdev *tdev, u32 hwtid, __be32 peer_ip,
1295 struct sk_buff *skb)
1297 PDBG("%s t3cdev %p tid %u peer_ip %x\n", __func__, tdev, hwtid,
1299 BUG_ON(skb_cloned(skb));
1300 skb_trim(skb, sizeof(struct cpl_tid_release));
1303 if (tdev->type != T3A)
1304 release_tid(tdev, hwtid, skb);
1306 struct cpl_pass_accept_rpl *rpl;
1309 skb->priority = CPL_PRIORITY_SETUP;
1310 rpl->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
1311 OPCODE_TID(rpl) = htonl(MK_OPCODE_TID(CPL_PASS_ACCEPT_RPL,
1313 rpl->peer_ip = peer_ip;
1314 rpl->opt0h = htonl(F_TCAM_BYPASS);
1315 rpl->opt0l_status = htonl(CPL_PASS_OPEN_REJECT);
1317 rpl->rsvd = rpl->opt2;
1318 cxgb3_ofld_send(tdev, skb);
1322 static int pass_accept_req(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1324 struct iwch_ep *child_ep, *parent_ep = ctx;
1325 struct cpl_pass_accept_req *req = cplhdr(skb);
1326 unsigned int hwtid = GET_TID(req);
1327 struct dst_entry *dst;
1328 struct l2t_entry *l2t;
1332 PDBG("%s parent ep %p tid %u\n", __func__, parent_ep, hwtid);
1334 if (state_read(&parent_ep->com) != LISTEN) {
1335 printk(KERN_ERR "%s - listening ep not in LISTEN\n",
1341 * Find the netdev for this connection request.
1343 tim.mac_addr = req->dst_mac;
1344 tim.vlan_tag = ntohs(req->vlan_tag);
1345 if (tdev->ctl(tdev, GET_IFF_FROM_MAC, &tim) < 0 || !tim.dev) {
1347 "%s bad dst mac %02x %02x %02x %02x %02x %02x\n",
1358 /* Find output route */
1359 rt = find_route(tdev,
1363 req->peer_port, G_PASS_OPEN_TOS(ntohl(req->tos_tid)));
1365 printk(KERN_ERR MOD "%s - failed to find dst entry!\n",
1370 l2t = t3_l2t_get(tdev, dst->neighbour, dst->neighbour->dev);
1372 printk(KERN_ERR MOD "%s - failed to allocate l2t entry!\n",
1377 child_ep = alloc_ep(sizeof(*child_ep), GFP_KERNEL);
1379 printk(KERN_ERR MOD "%s - failed to allocate ep entry!\n",
1381 l2t_release(L2DATA(tdev), l2t);
1385 state_set(&child_ep->com, CONNECTING);
1386 child_ep->com.tdev = tdev;
1387 child_ep->com.cm_id = NULL;
1388 child_ep->com.local_addr.sin_family = PF_INET;
1389 child_ep->com.local_addr.sin_port = req->local_port;
1390 child_ep->com.local_addr.sin_addr.s_addr = req->local_ip;
1391 child_ep->com.remote_addr.sin_family = PF_INET;
1392 child_ep->com.remote_addr.sin_port = req->peer_port;
1393 child_ep->com.remote_addr.sin_addr.s_addr = req->peer_ip;
1394 get_ep(&parent_ep->com);
1395 child_ep->parent_ep = parent_ep;
1396 child_ep->tos = G_PASS_OPEN_TOS(ntohl(req->tos_tid));
1397 child_ep->l2t = l2t;
1398 child_ep->dst = dst;
1399 child_ep->hwtid = hwtid;
1400 init_timer(&child_ep->timer);
1401 cxgb3_insert_tid(tdev, &t3c_client, child_ep, hwtid);
1402 accept_cr(child_ep, req->peer_ip, skb);
1405 reject_cr(tdev, hwtid, req->peer_ip, skb);
1407 return CPL_RET_BUF_DONE;
1410 static int pass_establish(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1412 struct iwch_ep *ep = ctx;
1413 struct cpl_pass_establish *req = cplhdr(skb);
1415 PDBG("%s ep %p\n", __func__, ep);
1416 ep->snd_seq = ntohl(req->snd_isn);
1417 ep->rcv_seq = ntohl(req->rcv_isn);
1419 set_emss(ep, ntohs(req->tcp_opt));
1421 dst_confirm(ep->dst);
1422 state_set(&ep->com, MPA_REQ_WAIT);
1425 return CPL_RET_BUF_DONE;
1428 static int peer_close(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1430 struct iwch_ep *ep = ctx;
1431 struct iwch_qp_attributes attrs;
1432 unsigned long flags;
1436 PDBG("%s ep %p\n", __func__, ep);
1437 dst_confirm(ep->dst);
1439 spin_lock_irqsave(&ep->com.lock, flags);
1440 switch (ep->com.state) {
1442 __state_set(&ep->com, CLOSING);
1445 __state_set(&ep->com, CLOSING);
1446 connect_reply_upcall(ep, -ECONNRESET);
1451 * We're gonna mark this puppy DEAD, but keep
1452 * the reference on it until the ULP accepts or
1455 __state_set(&ep->com, CLOSING);
1459 __state_set(&ep->com, CLOSING);
1460 ep->com.rpl_done = 1;
1461 ep->com.rpl_err = -ECONNRESET;
1462 PDBG("waking up ep %p\n", ep);
1463 wake_up(&ep->com.waitq);
1467 __state_set(&ep->com, CLOSING);
1468 attrs.next_state = IWCH_QP_STATE_CLOSING;
1469 iwch_modify_qp(ep->com.qp->rhp, ep->com.qp,
1470 IWCH_QP_ATTR_NEXT_STATE, &attrs, 1);
1471 peer_close_upcall(ep);
1477 __state_set(&ep->com, MORIBUND);
1482 if (ep->com.cm_id && ep->com.qp) {
1483 attrs.next_state = IWCH_QP_STATE_IDLE;
1484 iwch_modify_qp(ep->com.qp->rhp, ep->com.qp,
1485 IWCH_QP_ATTR_NEXT_STATE, &attrs, 1);
1487 close_complete_upcall(ep);
1488 __state_set(&ep->com, DEAD);
1498 spin_unlock_irqrestore(&ep->com.lock, flags);
1500 iwch_ep_disconnect(ep, 0, GFP_KERNEL);
1502 release_ep_resources(ep);
1503 return CPL_RET_BUF_DONE;
1507 * Returns whether an ABORT_REQ_RSS message is a negative advice.
1509 static int is_neg_adv_abort(unsigned int status)
1511 return status == CPL_ERR_RTX_NEG_ADVICE ||
1512 status == CPL_ERR_PERSIST_NEG_ADVICE;
1515 static int peer_abort(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1517 struct cpl_abort_req_rss *req = cplhdr(skb);
1518 struct iwch_ep *ep = ctx;
1519 struct cpl_abort_rpl *rpl;
1520 struct sk_buff *rpl_skb;
1521 struct iwch_qp_attributes attrs;
1524 unsigned long flags;
1526 if (is_neg_adv_abort(req->status)) {
1527 PDBG("%s neg_adv_abort ep %p tid %d\n", __func__, ep,
1529 t3_l2t_send_event(ep->com.tdev, ep->l2t);
1530 return CPL_RET_BUF_DONE;
1534 * We get 2 peer aborts from the HW. The first one must
1535 * be ignored except for scribbling that we need one more.
1537 if (!(ep->flags & PEER_ABORT_IN_PROGRESS)) {
1538 ep->flags |= PEER_ABORT_IN_PROGRESS;
1539 return CPL_RET_BUF_DONE;
1542 spin_lock_irqsave(&ep->com.lock, flags);
1543 PDBG("%s ep %p state %u\n", __func__, ep, ep->com.state);
1544 switch (ep->com.state) {
1552 connect_reply_upcall(ep, -ECONNRESET);
1555 ep->com.rpl_done = 1;
1556 ep->com.rpl_err = -ECONNRESET;
1557 PDBG("waking up ep %p\n", ep);
1558 wake_up(&ep->com.waitq);
1563 * We're gonna mark this puppy DEAD, but keep
1564 * the reference on it until the ULP accepts or
1574 if (ep->com.cm_id && ep->com.qp) {
1575 attrs.next_state = IWCH_QP_STATE_ERROR;
1576 ret = iwch_modify_qp(ep->com.qp->rhp,
1577 ep->com.qp, IWCH_QP_ATTR_NEXT_STATE,
1581 "%s - qp <- error failed!\n",
1584 peer_abort_upcall(ep);
1589 PDBG("%s PEER_ABORT IN DEAD STATE!!!!\n", __func__);
1590 spin_unlock_irqrestore(&ep->com.lock, flags);
1591 return CPL_RET_BUF_DONE;
1596 dst_confirm(ep->dst);
1597 if (ep->com.state != ABORTING) {
1598 __state_set(&ep->com, DEAD);
1601 spin_unlock_irqrestore(&ep->com.lock, flags);
1603 rpl_skb = get_skb(skb, sizeof(*rpl), GFP_KERNEL);
1605 printk(KERN_ERR MOD "%s - cannot allocate skb!\n",
1610 rpl_skb->priority = CPL_PRIORITY_DATA;
1611 rpl = (struct cpl_abort_rpl *) skb_put(rpl_skb, sizeof(*rpl));
1612 rpl->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_HOST_ABORT_CON_RPL));
1613 rpl->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
1614 OPCODE_TID(rpl) = htonl(MK_OPCODE_TID(CPL_ABORT_RPL, ep->hwtid));
1615 rpl->cmd = CPL_ABORT_NO_RST;
1616 cxgb3_ofld_send(ep->com.tdev, rpl_skb);
1619 release_ep_resources(ep);
1620 return CPL_RET_BUF_DONE;
1623 static int close_con_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1625 struct iwch_ep *ep = ctx;
1626 struct iwch_qp_attributes attrs;
1627 unsigned long flags;
1630 PDBG("%s ep %p\n", __func__, ep);
1633 /* The cm_id may be null if we failed to connect */
1634 spin_lock_irqsave(&ep->com.lock, flags);
1635 switch (ep->com.state) {
1637 __state_set(&ep->com, MORIBUND);
1641 if ((ep->com.cm_id) && (ep->com.qp)) {
1642 attrs.next_state = IWCH_QP_STATE_IDLE;
1643 iwch_modify_qp(ep->com.qp->rhp,
1645 IWCH_QP_ATTR_NEXT_STATE,
1648 close_complete_upcall(ep);
1649 __state_set(&ep->com, DEAD);
1659 spin_unlock_irqrestore(&ep->com.lock, flags);
1661 release_ep_resources(ep);
1662 return CPL_RET_BUF_DONE;
1666 * T3A does 3 things when a TERM is received:
1667 * 1) send up a CPL_RDMA_TERMINATE message with the TERM packet
1668 * 2) generate an async event on the QP with the TERMINATE opcode
1669 * 3) post a TERMINATE opcde cqe into the associated CQ.
1671 * For (1), we save the message in the qp for later consumer consumption.
1672 * For (2), we move the QP into TERMINATE, post a QP event and disconnect.
1673 * For (3), we toss the CQE in cxio_poll_cq().
1675 * terminate() handles case (1)...
1677 static int terminate(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1679 struct iwch_ep *ep = ctx;
1681 PDBG("%s ep %p\n", __func__, ep);
1682 skb_pull(skb, sizeof(struct cpl_rdma_terminate));
1683 PDBG("%s saving %d bytes of term msg\n", __func__, skb->len);
1684 skb_copy_from_linear_data(skb, ep->com.qp->attr.terminate_buffer,
1686 ep->com.qp->attr.terminate_msg_len = skb->len;
1687 ep->com.qp->attr.is_terminate_local = 0;
1688 return CPL_RET_BUF_DONE;
1691 static int ec_status(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1693 struct cpl_rdma_ec_status *rep = cplhdr(skb);
1694 struct iwch_ep *ep = ctx;
1696 PDBG("%s ep %p tid %u status %d\n", __func__, ep, ep->hwtid,
1699 struct iwch_qp_attributes attrs;
1701 printk(KERN_ERR MOD "%s BAD CLOSE - Aborting tid %u\n",
1702 __func__, ep->hwtid);
1704 attrs.next_state = IWCH_QP_STATE_ERROR;
1705 iwch_modify_qp(ep->com.qp->rhp,
1706 ep->com.qp, IWCH_QP_ATTR_NEXT_STATE,
1708 abort_connection(ep, NULL, GFP_KERNEL);
1710 return CPL_RET_BUF_DONE;
1713 static void ep_timeout(unsigned long arg)
1715 struct iwch_ep *ep = (struct iwch_ep *)arg;
1716 struct iwch_qp_attributes attrs;
1717 unsigned long flags;
1720 spin_lock_irqsave(&ep->com.lock, flags);
1721 PDBG("%s ep %p tid %u state %d\n", __func__, ep, ep->hwtid,
1723 switch (ep->com.state) {
1725 __state_set(&ep->com, ABORTING);
1726 connect_reply_upcall(ep, -ETIMEDOUT);
1729 __state_set(&ep->com, ABORTING);
1733 if (ep->com.cm_id && ep->com.qp) {
1734 attrs.next_state = IWCH_QP_STATE_ERROR;
1735 iwch_modify_qp(ep->com.qp->rhp,
1736 ep->com.qp, IWCH_QP_ATTR_NEXT_STATE,
1739 __state_set(&ep->com, ABORTING);
1742 printk(KERN_ERR "%s unexpected state ep %p state %u\n",
1743 __func__, ep, ep->com.state);
1747 spin_unlock_irqrestore(&ep->com.lock, flags);
1749 abort_connection(ep, NULL, GFP_ATOMIC);
1753 int iwch_reject_cr(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
1756 struct iwch_ep *ep = to_ep(cm_id);
1757 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1759 if (state_read(&ep->com) == DEAD) {
1763 BUG_ON(state_read(&ep->com) != MPA_REQ_RCVD);
1765 abort_connection(ep, NULL, GFP_KERNEL);
1767 err = send_mpa_reject(ep, pdata, pdata_len);
1768 err = iwch_ep_disconnect(ep, 0, GFP_KERNEL);
1773 int iwch_accept_cr(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
1776 struct iwch_qp_attributes attrs;
1777 enum iwch_qp_attr_mask mask;
1778 struct iwch_ep *ep = to_ep(cm_id);
1779 struct iwch_dev *h = to_iwch_dev(cm_id->device);
1780 struct iwch_qp *qp = get_qhp(h, conn_param->qpn);
1782 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1783 if (state_read(&ep->com) == DEAD)
1786 BUG_ON(state_read(&ep->com) != MPA_REQ_RCVD);
1789 if ((conn_param->ord > qp->rhp->attr.max_rdma_read_qp_depth) ||
1790 (conn_param->ird > qp->rhp->attr.max_rdma_reads_per_qp)) {
1791 abort_connection(ep, NULL, GFP_KERNEL);
1795 cm_id->add_ref(cm_id);
1796 ep->com.cm_id = cm_id;
1799 ep->com.rpl_done = 0;
1800 ep->com.rpl_err = 0;
1801 ep->ird = conn_param->ird;
1802 ep->ord = conn_param->ord;
1803 PDBG("%s %d ird %d ord %d\n", __func__, __LINE__, ep->ird, ep->ord);
1807 /* bind QP to EP and move to RTS */
1808 attrs.mpa_attr = ep->mpa_attr;
1809 attrs.max_ird = ep->ird;
1810 attrs.max_ord = ep->ord;
1811 attrs.llp_stream_handle = ep;
1812 attrs.next_state = IWCH_QP_STATE_RTS;
1814 /* bind QP and TID with INIT_WR */
1815 mask = IWCH_QP_ATTR_NEXT_STATE |
1816 IWCH_QP_ATTR_LLP_STREAM_HANDLE |
1817 IWCH_QP_ATTR_MPA_ATTR |
1818 IWCH_QP_ATTR_MAX_IRD |
1819 IWCH_QP_ATTR_MAX_ORD;
1821 err = iwch_modify_qp(ep->com.qp->rhp,
1822 ep->com.qp, mask, &attrs, 1);
1826 /* if needed, wait for wr_ack */
1827 if (iwch_rqes_posted(qp)) {
1828 wait_event(ep->com.waitq, ep->com.rpl_done);
1829 err = ep->com.rpl_err;
1834 err = send_mpa_reply(ep, conn_param->private_data,
1835 conn_param->private_data_len);
1840 state_set(&ep->com, FPDU_MODE);
1841 established_upcall(ep);
1845 ep->com.cm_id = NULL;
1847 cm_id->rem_ref(cm_id);
1852 static int is_loopback_dst(struct iw_cm_id *cm_id)
1854 struct net_device *dev;
1856 dev = ip_dev_find(&init_net, cm_id->remote_addr.sin_addr.s_addr);
1863 int iwch_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
1866 struct iwch_dev *h = to_iwch_dev(cm_id->device);
1870 if (is_loopback_dst(cm_id)) {
1875 ep = alloc_ep(sizeof(*ep), GFP_KERNEL);
1877 printk(KERN_ERR MOD "%s - cannot alloc ep.\n", __func__);
1881 init_timer(&ep->timer);
1882 ep->plen = conn_param->private_data_len;
1884 memcpy(ep->mpa_pkt + sizeof(struct mpa_message),
1885 conn_param->private_data, ep->plen);
1886 ep->ird = conn_param->ird;
1887 ep->ord = conn_param->ord;
1888 ep->com.tdev = h->rdev.t3cdev_p;
1890 cm_id->add_ref(cm_id);
1891 ep->com.cm_id = cm_id;
1892 ep->com.qp = get_qhp(h, conn_param->qpn);
1893 BUG_ON(!ep->com.qp);
1894 PDBG("%s qpn 0x%x qp %p cm_id %p\n", __func__, conn_param->qpn,
1898 * Allocate an active TID to initiate a TCP connection.
1900 ep->atid = cxgb3_alloc_atid(h->rdev.t3cdev_p, &t3c_client, ep);
1901 if (ep->atid == -1) {
1902 printk(KERN_ERR MOD "%s - cannot alloc atid.\n", __func__);
1908 rt = find_route(h->rdev.t3cdev_p,
1909 cm_id->local_addr.sin_addr.s_addr,
1910 cm_id->remote_addr.sin_addr.s_addr,
1911 cm_id->local_addr.sin_port,
1912 cm_id->remote_addr.sin_port, IPTOS_LOWDELAY);
1914 printk(KERN_ERR MOD "%s - cannot find route.\n", __func__);
1915 err = -EHOSTUNREACH;
1918 ep->dst = &rt->u.dst;
1920 /* get a l2t entry */
1921 ep->l2t = t3_l2t_get(ep->com.tdev, ep->dst->neighbour,
1922 ep->dst->neighbour->dev);
1924 printk(KERN_ERR MOD "%s - cannot alloc l2e.\n", __func__);
1929 state_set(&ep->com, CONNECTING);
1930 ep->tos = IPTOS_LOWDELAY;
1931 ep->com.local_addr = cm_id->local_addr;
1932 ep->com.remote_addr = cm_id->remote_addr;
1934 /* send connect request to rnic */
1935 err = send_connect(ep);
1939 l2t_release(L2DATA(h->rdev.t3cdev_p), ep->l2t);
1941 dst_release(ep->dst);
1943 cxgb3_free_atid(ep->com.tdev, ep->atid);
1950 int iwch_create_listen(struct iw_cm_id *cm_id, int backlog)
1953 struct iwch_dev *h = to_iwch_dev(cm_id->device);
1954 struct iwch_listen_ep *ep;
1959 ep = alloc_ep(sizeof(*ep), GFP_KERNEL);
1961 printk(KERN_ERR MOD "%s - cannot alloc ep.\n", __func__);
1965 PDBG("%s ep %p\n", __func__, ep);
1966 ep->com.tdev = h->rdev.t3cdev_p;
1967 cm_id->add_ref(cm_id);
1968 ep->com.cm_id = cm_id;
1969 ep->backlog = backlog;
1970 ep->com.local_addr = cm_id->local_addr;
1973 * Allocate a server TID.
1975 ep->stid = cxgb3_alloc_stid(h->rdev.t3cdev_p, &t3c_client, ep);
1976 if (ep->stid == -1) {
1977 printk(KERN_ERR MOD "%s - cannot alloc atid.\n", __func__);
1982 state_set(&ep->com, LISTEN);
1983 err = listen_start(ep);
1987 /* wait for pass_open_rpl */
1988 wait_event(ep->com.waitq, ep->com.rpl_done);
1989 err = ep->com.rpl_err;
1991 cm_id->provider_data = ep;
1995 cxgb3_free_stid(ep->com.tdev, ep->stid);
1997 cm_id->rem_ref(cm_id);
2004 int iwch_destroy_listen(struct iw_cm_id *cm_id)
2007 struct iwch_listen_ep *ep = to_listen_ep(cm_id);
2009 PDBG("%s ep %p\n", __func__, ep);
2012 state_set(&ep->com, DEAD);
2013 ep->com.rpl_done = 0;
2014 ep->com.rpl_err = 0;
2015 err = listen_stop(ep);
2016 wait_event(ep->com.waitq, ep->com.rpl_done);
2017 cxgb3_free_stid(ep->com.tdev, ep->stid);
2018 err = ep->com.rpl_err;
2019 cm_id->rem_ref(cm_id);
2024 int iwch_ep_disconnect(struct iwch_ep *ep, int abrupt, gfp_t gfp)
2027 unsigned long flags;
2030 spin_lock_irqsave(&ep->com.lock, flags);
2032 PDBG("%s ep %p state %s, abrupt %d\n", __func__, ep,
2033 states[ep->com.state], abrupt);
2035 switch (ep->com.state) {
2043 ep->com.state = ABORTING;
2045 ep->com.state = CLOSING;
2053 ep->com.state = ABORTING;
2055 ep->com.state = MORIBUND;
2060 PDBG("%s ignoring disconnect ep %p state %u\n",
2061 __func__, ep, ep->com.state);
2068 spin_unlock_irqrestore(&ep->com.lock, flags);
2071 ret = send_abort(ep, NULL, gfp);
2073 ret = send_halfclose(ep, gfp);
2078 int iwch_ep_redirect(void *ctx, struct dst_entry *old, struct dst_entry *new,
2079 struct l2t_entry *l2t)
2081 struct iwch_ep *ep = ctx;
2086 PDBG("%s ep %p redirect to dst %p l2t %p\n", __func__, ep, new,
2089 l2t_release(L2DATA(ep->com.tdev), ep->l2t);
2097 * All the CM events are handled on a work queue to have a safe context.
2099 static int sched(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
2101 struct iwch_ep_common *epc = ctx;
2106 * Save ctx and tdev in the skb->cb area.
2108 *((void **) skb->cb) = ctx;
2109 *((struct t3cdev **) (skb->cb + sizeof(void *))) = tdev;
2112 * Queue the skb and schedule the worker thread.
2114 skb_queue_tail(&rxq, skb);
2115 queue_work(workq, &skb_work);
2119 static int set_tcb_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
2121 struct cpl_set_tcb_rpl *rpl = cplhdr(skb);
2123 if (rpl->status != CPL_ERR_NONE) {
2124 printk(KERN_ERR MOD "Unexpected SET_TCB_RPL status %u "
2125 "for tid %u\n", rpl->status, GET_TID(rpl));
2127 return CPL_RET_BUF_DONE;
2130 int __init iwch_cm_init(void)
2132 skb_queue_head_init(&rxq);
2134 workq = create_singlethread_workqueue("iw_cxgb3");
2139 * All upcalls from the T3 Core go to sched() to
2140 * schedule the processing on a work queue.
2142 t3c_handlers[CPL_ACT_ESTABLISH] = sched;
2143 t3c_handlers[CPL_ACT_OPEN_RPL] = sched;
2144 t3c_handlers[CPL_RX_DATA] = sched;
2145 t3c_handlers[CPL_TX_DMA_ACK] = sched;
2146 t3c_handlers[CPL_ABORT_RPL_RSS] = sched;
2147 t3c_handlers[CPL_ABORT_RPL] = sched;
2148 t3c_handlers[CPL_PASS_OPEN_RPL] = sched;
2149 t3c_handlers[CPL_CLOSE_LISTSRV_RPL] = sched;
2150 t3c_handlers[CPL_PASS_ACCEPT_REQ] = sched;
2151 t3c_handlers[CPL_PASS_ESTABLISH] = sched;
2152 t3c_handlers[CPL_PEER_CLOSE] = sched;
2153 t3c_handlers[CPL_CLOSE_CON_RPL] = sched;
2154 t3c_handlers[CPL_ABORT_REQ_RSS] = sched;
2155 t3c_handlers[CPL_RDMA_TERMINATE] = sched;
2156 t3c_handlers[CPL_RDMA_EC_STATUS] = sched;
2157 t3c_handlers[CPL_SET_TCB_RPL] = set_tcb_rpl;
2160 * These are the real handlers that are called from a
2163 work_handlers[CPL_ACT_ESTABLISH] = act_establish;
2164 work_handlers[CPL_ACT_OPEN_RPL] = act_open_rpl;
2165 work_handlers[CPL_RX_DATA] = rx_data;
2166 work_handlers[CPL_TX_DMA_ACK] = tx_ack;
2167 work_handlers[CPL_ABORT_RPL_RSS] = abort_rpl;
2168 work_handlers[CPL_ABORT_RPL] = abort_rpl;
2169 work_handlers[CPL_PASS_OPEN_RPL] = pass_open_rpl;
2170 work_handlers[CPL_CLOSE_LISTSRV_RPL] = close_listsrv_rpl;
2171 work_handlers[CPL_PASS_ACCEPT_REQ] = pass_accept_req;
2172 work_handlers[CPL_PASS_ESTABLISH] = pass_establish;
2173 work_handlers[CPL_PEER_CLOSE] = peer_close;
2174 work_handlers[CPL_ABORT_REQ_RSS] = peer_abort;
2175 work_handlers[CPL_CLOSE_CON_RPL] = close_con_rpl;
2176 work_handlers[CPL_RDMA_TERMINATE] = terminate;
2177 work_handlers[CPL_RDMA_EC_STATUS] = ec_status;
2181 void __exit iwch_cm_term(void)
2183 flush_workqueue(workq);
2184 destroy_workqueue(workq);