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[] = {
66 static int ep_timeout_secs = 10;
67 module_param(ep_timeout_secs, int, 0644);
68 MODULE_PARM_DESC(ep_timeout_secs, "CM Endpoint operation timeout "
69 "in seconds (default=10)");
71 static int mpa_rev = 1;
72 module_param(mpa_rev, int, 0644);
73 MODULE_PARM_DESC(mpa_rev, "MPA Revision, 0 supports amso1100, "
74 "1 is spec compliant. (default=1)");
76 static int markers_enabled = 0;
77 module_param(markers_enabled, int, 0644);
78 MODULE_PARM_DESC(markers_enabled, "Enable MPA MARKERS (default(0)=disabled)");
80 static int crc_enabled = 1;
81 module_param(crc_enabled, int, 0644);
82 MODULE_PARM_DESC(crc_enabled, "Enable MPA CRC (default(1)=enabled)");
84 static int rcv_win = 256 * 1024;
85 module_param(rcv_win, int, 0644);
86 MODULE_PARM_DESC(rcv_win, "TCP receive window in bytes (default=256)");
88 static int snd_win = 32 * 1024;
89 module_param(snd_win, int, 0644);
90 MODULE_PARM_DESC(snd_win, "TCP send window in bytes (default=32KB)");
92 static unsigned int nocong = 0;
93 module_param(nocong, uint, 0644);
94 MODULE_PARM_DESC(nocong, "Turn off congestion control (default=0)");
96 static unsigned int cong_flavor = 1;
97 module_param(cong_flavor, uint, 0644);
98 MODULE_PARM_DESC(cong_flavor, "TCP Congestion control flavor (default=1)");
100 static void process_work(struct work_struct *work);
101 static struct workqueue_struct *workq;
102 static DECLARE_WORK(skb_work, process_work);
104 static struct sk_buff_head rxq;
105 static cxgb3_cpl_handler_func work_handlers[NUM_CPL_CMDS];
107 static struct sk_buff *get_skb(struct sk_buff *skb, int len, gfp_t gfp);
108 static void ep_timeout(unsigned long arg);
109 static void connect_reply_upcall(struct iwch_ep *ep, int status);
111 static void start_ep_timer(struct iwch_ep *ep)
113 PDBG("%s ep %p\n", __func__, ep);
114 if (timer_pending(&ep->timer)) {
115 PDBG("%s stopped / restarted timer ep %p\n", __func__, ep);
116 del_timer_sync(&ep->timer);
119 ep->timer.expires = jiffies + ep_timeout_secs * HZ;
120 ep->timer.data = (unsigned long)ep;
121 ep->timer.function = ep_timeout;
122 add_timer(&ep->timer);
125 static void stop_ep_timer(struct iwch_ep *ep)
127 PDBG("%s ep %p\n", __func__, ep);
128 if (!timer_pending(&ep->timer)) {
129 printk(KERN_ERR "%s timer stopped when its not running! ep %p state %u\n",
130 __func__, ep, ep->com.state);
134 del_timer_sync(&ep->timer);
138 static void release_tid(struct t3cdev *tdev, u32 hwtid, struct sk_buff *skb)
140 struct cpl_tid_release *req;
142 skb = get_skb(skb, sizeof *req, GFP_KERNEL);
145 req = (struct cpl_tid_release *) skb_put(skb, sizeof(*req));
146 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
147 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_TID_RELEASE, hwtid));
148 skb->priority = CPL_PRIORITY_SETUP;
149 cxgb3_ofld_send(tdev, skb);
153 int iwch_quiesce_tid(struct iwch_ep *ep)
155 struct cpl_set_tcb_field *req;
156 struct sk_buff *skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
160 req = (struct cpl_set_tcb_field *) skb_put(skb, sizeof(*req));
161 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
162 req->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
163 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, ep->hwtid));
166 req->word = htons(W_TCB_RX_QUIESCE);
167 req->mask = cpu_to_be64(1ULL << S_TCB_RX_QUIESCE);
168 req->val = cpu_to_be64(1 << S_TCB_RX_QUIESCE);
170 skb->priority = CPL_PRIORITY_DATA;
171 cxgb3_ofld_send(ep->com.tdev, skb);
175 int iwch_resume_tid(struct iwch_ep *ep)
177 struct cpl_set_tcb_field *req;
178 struct sk_buff *skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
182 req = (struct cpl_set_tcb_field *) skb_put(skb, sizeof(*req));
183 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
184 req->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
185 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, ep->hwtid));
188 req->word = htons(W_TCB_RX_QUIESCE);
189 req->mask = cpu_to_be64(1ULL << S_TCB_RX_QUIESCE);
192 skb->priority = CPL_PRIORITY_DATA;
193 cxgb3_ofld_send(ep->com.tdev, skb);
197 static void set_emss(struct iwch_ep *ep, u16 opt)
199 PDBG("%s ep %p opt %u\n", __func__, ep, opt);
200 ep->emss = T3C_DATA(ep->com.tdev)->mtus[G_TCPOPT_MSS(opt)] - 40;
201 if (G_TCPOPT_TSTAMP(opt))
205 PDBG("emss=%d\n", ep->emss);
208 static enum iwch_ep_state state_read(struct iwch_ep_common *epc)
211 enum iwch_ep_state state;
213 spin_lock_irqsave(&epc->lock, flags);
215 spin_unlock_irqrestore(&epc->lock, flags);
219 static void __state_set(struct iwch_ep_common *epc, enum iwch_ep_state new)
224 static void state_set(struct iwch_ep_common *epc, enum iwch_ep_state new)
228 spin_lock_irqsave(&epc->lock, flags);
229 PDBG("%s - %s -> %s\n", __func__, states[epc->state], states[new]);
230 __state_set(epc, new);
231 spin_unlock_irqrestore(&epc->lock, flags);
235 static void *alloc_ep(int size, gfp_t gfp)
237 struct iwch_ep_common *epc;
239 epc = kzalloc(size, gfp);
241 kref_init(&epc->kref);
242 spin_lock_init(&epc->lock);
243 init_waitqueue_head(&epc->waitq);
245 PDBG("%s alloc ep %p\n", __func__, epc);
249 void __free_ep(struct kref *kref)
251 struct iwch_ep_common *epc;
252 epc = container_of(kref, struct iwch_ep_common, kref);
253 PDBG("%s ep %p state %s\n", __func__, epc, states[state_read(epc)]);
257 static void release_ep_resources(struct iwch_ep *ep)
259 PDBG("%s ep %p tid %d\n", __func__, ep, ep->hwtid);
260 cxgb3_remove_tid(ep->com.tdev, (void *)ep, ep->hwtid);
261 dst_release(ep->dst);
262 l2t_release(L2DATA(ep->com.tdev), ep->l2t);
266 static void process_work(struct work_struct *work)
268 struct sk_buff *skb = NULL;
273 while ((skb = skb_dequeue(&rxq))) {
274 ep = *((void **) (skb->cb));
275 tdev = *((struct t3cdev **) (skb->cb + sizeof(void *)));
276 ret = work_handlers[G_OPCODE(ntohl((__force __be32)skb->csum))](tdev, skb, ep);
277 if (ret & CPL_RET_BUF_DONE)
281 * ep was referenced in sched(), and is freed here.
283 put_ep((struct iwch_ep_common *)ep);
287 static int status2errno(int status)
292 case CPL_ERR_CONN_RESET:
294 case CPL_ERR_ARP_MISS:
295 return -EHOSTUNREACH;
296 case CPL_ERR_CONN_TIMEDOUT:
298 case CPL_ERR_TCAM_FULL:
300 case CPL_ERR_CONN_EXIST:
308 * Try and reuse skbs already allocated...
310 static struct sk_buff *get_skb(struct sk_buff *skb, int len, gfp_t gfp)
312 if (skb && !skb_is_nonlinear(skb) && !skb_cloned(skb)) {
316 skb = alloc_skb(len, gfp);
321 static struct rtable *find_route(struct t3cdev *dev, __be32 local_ip,
322 __be32 peer_ip, __be16 local_port,
323 __be16 peer_port, u8 tos)
334 .proto = IPPROTO_TCP,
342 if (ip_route_output_flow(&init_net, &rt, &fl, NULL, 0))
347 static unsigned int find_best_mtu(const struct t3c_data *d, unsigned short mtu)
351 while (i < d->nmtus - 1 && d->mtus[i + 1] <= mtu)
356 static void arp_failure_discard(struct t3cdev *dev, struct sk_buff *skb)
358 PDBG("%s t3cdev %p\n", __func__, dev);
363 * Handle an ARP failure for an active open.
365 static void act_open_req_arp_failure(struct t3cdev *dev, struct sk_buff *skb)
367 printk(KERN_ERR MOD "ARP failure duing connect\n");
372 * Handle an ARP failure for a CPL_ABORT_REQ. Change it into a no RST variant
375 static void abort_arp_failure(struct t3cdev *dev, struct sk_buff *skb)
377 struct cpl_abort_req *req = cplhdr(skb);
379 PDBG("%s t3cdev %p\n", __func__, dev);
380 req->cmd = CPL_ABORT_NO_RST;
381 cxgb3_ofld_send(dev, skb);
384 static int send_halfclose(struct iwch_ep *ep, gfp_t gfp)
386 struct cpl_close_con_req *req;
389 PDBG("%s ep %p\n", __func__, ep);
390 skb = get_skb(NULL, sizeof(*req), gfp);
392 printk(KERN_ERR MOD "%s - failed to alloc skb\n", __func__);
395 skb->priority = CPL_PRIORITY_DATA;
396 set_arp_failure_handler(skb, arp_failure_discard);
397 req = (struct cpl_close_con_req *) skb_put(skb, sizeof(*req));
398 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_CLOSE_CON));
399 req->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
400 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_CON_REQ, ep->hwtid));
401 l2t_send(ep->com.tdev, skb, ep->l2t);
405 static int send_abort(struct iwch_ep *ep, struct sk_buff *skb, gfp_t gfp)
407 struct cpl_abort_req *req;
409 PDBG("%s ep %p\n", __func__, ep);
410 skb = get_skb(skb, sizeof(*req), gfp);
412 printk(KERN_ERR MOD "%s - failed to alloc skb.\n",
416 skb->priority = CPL_PRIORITY_DATA;
417 set_arp_failure_handler(skb, abort_arp_failure);
418 req = (struct cpl_abort_req *) skb_put(skb, sizeof(*req));
419 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_HOST_ABORT_CON_REQ));
420 req->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
421 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_ABORT_REQ, ep->hwtid));
422 req->cmd = CPL_ABORT_SEND_RST;
423 l2t_send(ep->com.tdev, skb, ep->l2t);
427 static int send_connect(struct iwch_ep *ep)
429 struct cpl_act_open_req *req;
431 u32 opt0h, opt0l, opt2;
432 unsigned int mtu_idx;
435 PDBG("%s ep %p\n", __func__, ep);
437 skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
439 printk(KERN_ERR MOD "%s - failed to alloc skb.\n",
443 mtu_idx = find_best_mtu(T3C_DATA(ep->com.tdev), dst_mtu(ep->dst));
444 wscale = compute_wscale(rcv_win);
449 V_WND_SCALE(wscale) |
451 V_L2T_IDX(ep->l2t->idx) | V_TX_CHANNEL(ep->l2t->smt_idx);
452 opt0l = V_TOS((ep->tos >> 2) & M_TOS) | V_RCV_BUFSIZ(rcv_win>>10);
453 opt2 = V_FLAVORS_VALID(1) | V_CONG_CONTROL_FLAVOR(cong_flavor);
454 skb->priority = CPL_PRIORITY_SETUP;
455 set_arp_failure_handler(skb, act_open_req_arp_failure);
457 req = (struct cpl_act_open_req *) skb_put(skb, sizeof(*req));
458 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
459 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_ACT_OPEN_REQ, ep->atid));
460 req->local_port = ep->com.local_addr.sin_port;
461 req->peer_port = ep->com.remote_addr.sin_port;
462 req->local_ip = ep->com.local_addr.sin_addr.s_addr;
463 req->peer_ip = ep->com.remote_addr.sin_addr.s_addr;
464 req->opt0h = htonl(opt0h);
465 req->opt0l = htonl(opt0l);
467 req->opt2 = htonl(opt2);
468 l2t_send(ep->com.tdev, skb, ep->l2t);
472 static void send_mpa_req(struct iwch_ep *ep, struct sk_buff *skb)
475 struct tx_data_wr *req;
476 struct mpa_message *mpa;
479 PDBG("%s ep %p pd_len %d\n", __func__, ep, ep->plen);
481 BUG_ON(skb_cloned(skb));
483 mpalen = sizeof(*mpa) + ep->plen;
484 if (skb->data + mpalen + sizeof(*req) > skb_end_pointer(skb)) {
486 skb=alloc_skb(mpalen + sizeof(*req), GFP_KERNEL);
488 connect_reply_upcall(ep, -ENOMEM);
493 skb_reserve(skb, sizeof(*req));
494 skb_put(skb, mpalen);
495 skb->priority = CPL_PRIORITY_DATA;
496 mpa = (struct mpa_message *) skb->data;
497 memset(mpa, 0, sizeof(*mpa));
498 memcpy(mpa->key, MPA_KEY_REQ, sizeof(mpa->key));
499 mpa->flags = (crc_enabled ? MPA_CRC : 0) |
500 (markers_enabled ? MPA_MARKERS : 0);
501 mpa->private_data_size = htons(ep->plen);
502 mpa->revision = mpa_rev;
505 memcpy(mpa->private_data, ep->mpa_pkt + sizeof(*mpa), ep->plen);
508 * Reference the mpa skb. This ensures the data area
509 * will remain in memory until the hw acks the tx.
510 * Function tx_ack() will deref it.
513 set_arp_failure_handler(skb, arp_failure_discard);
514 skb_reset_transport_header(skb);
516 req = (struct tx_data_wr *) skb_push(skb, sizeof(*req));
517 req->wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_TX_DATA));
518 req->wr_lo = htonl(V_WR_TID(ep->hwtid));
519 req->len = htonl(len);
520 req->param = htonl(V_TX_PORT(ep->l2t->smt_idx) |
521 V_TX_SNDBUF(snd_win>>15));
522 req->flags = htonl(F_TX_INIT);
523 req->sndseq = htonl(ep->snd_seq);
526 l2t_send(ep->com.tdev, skb, ep->l2t);
528 state_set(&ep->com, MPA_REQ_SENT);
532 static int send_mpa_reject(struct iwch_ep *ep, const void *pdata, u8 plen)
535 struct tx_data_wr *req;
536 struct mpa_message *mpa;
539 PDBG("%s ep %p plen %d\n", __func__, ep, plen);
541 mpalen = sizeof(*mpa) + plen;
543 skb = get_skb(NULL, mpalen + sizeof(*req), GFP_KERNEL);
545 printk(KERN_ERR MOD "%s - cannot alloc skb!\n", __func__);
548 skb_reserve(skb, sizeof(*req));
549 mpa = (struct mpa_message *) skb_put(skb, mpalen);
550 memset(mpa, 0, sizeof(*mpa));
551 memcpy(mpa->key, MPA_KEY_REP, sizeof(mpa->key));
552 mpa->flags = MPA_REJECT;
553 mpa->revision = mpa_rev;
554 mpa->private_data_size = htons(plen);
556 memcpy(mpa->private_data, pdata, plen);
559 * Reference the mpa skb again. This ensures the data area
560 * will remain in memory until the hw acks the tx.
561 * Function tx_ack() will deref it.
564 skb->priority = CPL_PRIORITY_DATA;
565 set_arp_failure_handler(skb, arp_failure_discard);
566 skb_reset_transport_header(skb);
567 req = (struct tx_data_wr *) skb_push(skb, sizeof(*req));
568 req->wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_TX_DATA));
569 req->wr_lo = htonl(V_WR_TID(ep->hwtid));
570 req->len = htonl(mpalen);
571 req->param = htonl(V_TX_PORT(ep->l2t->smt_idx) |
572 V_TX_SNDBUF(snd_win>>15));
573 req->flags = htonl(F_TX_INIT);
574 req->sndseq = htonl(ep->snd_seq);
577 l2t_send(ep->com.tdev, skb, ep->l2t);
581 static int send_mpa_reply(struct iwch_ep *ep, const void *pdata, u8 plen)
584 struct tx_data_wr *req;
585 struct mpa_message *mpa;
589 PDBG("%s ep %p plen %d\n", __func__, ep, plen);
591 mpalen = sizeof(*mpa) + plen;
593 skb = get_skb(NULL, mpalen + sizeof(*req), GFP_KERNEL);
595 printk(KERN_ERR MOD "%s - cannot alloc skb!\n", __func__);
598 skb->priority = CPL_PRIORITY_DATA;
599 skb_reserve(skb, sizeof(*req));
600 mpa = (struct mpa_message *) skb_put(skb, mpalen);
601 memset(mpa, 0, sizeof(*mpa));
602 memcpy(mpa->key, MPA_KEY_REP, sizeof(mpa->key));
603 mpa->flags = (ep->mpa_attr.crc_enabled ? MPA_CRC : 0) |
604 (markers_enabled ? MPA_MARKERS : 0);
605 mpa->revision = mpa_rev;
606 mpa->private_data_size = htons(plen);
608 memcpy(mpa->private_data, pdata, plen);
611 * Reference the mpa skb. This ensures the data area
612 * will remain in memory until the hw acks the tx.
613 * Function tx_ack() will deref it.
616 set_arp_failure_handler(skb, arp_failure_discard);
617 skb_reset_transport_header(skb);
619 req = (struct tx_data_wr *) skb_push(skb, sizeof(*req));
620 req->wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_TX_DATA));
621 req->wr_lo = htonl(V_WR_TID(ep->hwtid));
622 req->len = htonl(len);
623 req->param = htonl(V_TX_PORT(ep->l2t->smt_idx) |
624 V_TX_SNDBUF(snd_win>>15));
625 req->flags = htonl(F_TX_INIT);
626 req->sndseq = htonl(ep->snd_seq);
628 state_set(&ep->com, MPA_REP_SENT);
629 l2t_send(ep->com.tdev, skb, ep->l2t);
633 static int act_establish(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
635 struct iwch_ep *ep = ctx;
636 struct cpl_act_establish *req = cplhdr(skb);
637 unsigned int tid = GET_TID(req);
639 PDBG("%s ep %p tid %d\n", __func__, ep, tid);
641 dst_confirm(ep->dst);
643 /* setup the hwtid for this connection */
645 cxgb3_insert_tid(ep->com.tdev, &t3c_client, ep, tid);
647 ep->snd_seq = ntohl(req->snd_isn);
648 ep->rcv_seq = ntohl(req->rcv_isn);
650 set_emss(ep, ntohs(req->tcp_opt));
652 /* dealloc the atid */
653 cxgb3_free_atid(ep->com.tdev, ep->atid);
655 /* start MPA negotiation */
656 send_mpa_req(ep, skb);
661 static void abort_connection(struct iwch_ep *ep, struct sk_buff *skb, gfp_t gfp)
663 PDBG("%s ep %p\n", __FILE__, ep);
664 state_set(&ep->com, ABORTING);
665 send_abort(ep, skb, gfp);
668 static void close_complete_upcall(struct iwch_ep *ep)
670 struct iw_cm_event event;
672 PDBG("%s ep %p\n", __func__, ep);
673 memset(&event, 0, sizeof(event));
674 event.event = IW_CM_EVENT_CLOSE;
676 PDBG("close complete delivered ep %p cm_id %p tid %d\n",
677 ep, ep->com.cm_id, ep->hwtid);
678 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
679 ep->com.cm_id->rem_ref(ep->com.cm_id);
680 ep->com.cm_id = NULL;
685 static void peer_close_upcall(struct iwch_ep *ep)
687 struct iw_cm_event event;
689 PDBG("%s ep %p\n", __func__, ep);
690 memset(&event, 0, sizeof(event));
691 event.event = IW_CM_EVENT_DISCONNECT;
693 PDBG("peer close delivered ep %p cm_id %p tid %d\n",
694 ep, ep->com.cm_id, ep->hwtid);
695 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
699 static void peer_abort_upcall(struct iwch_ep *ep)
701 struct iw_cm_event event;
703 PDBG("%s ep %p\n", __func__, ep);
704 memset(&event, 0, sizeof(event));
705 event.event = IW_CM_EVENT_CLOSE;
706 event.status = -ECONNRESET;
708 PDBG("abort delivered ep %p cm_id %p tid %d\n", ep,
709 ep->com.cm_id, ep->hwtid);
710 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
711 ep->com.cm_id->rem_ref(ep->com.cm_id);
712 ep->com.cm_id = NULL;
717 static void connect_reply_upcall(struct iwch_ep *ep, int status)
719 struct iw_cm_event event;
721 PDBG("%s ep %p status %d\n", __func__, ep, status);
722 memset(&event, 0, sizeof(event));
723 event.event = IW_CM_EVENT_CONNECT_REPLY;
724 event.status = status;
725 event.local_addr = ep->com.local_addr;
726 event.remote_addr = ep->com.remote_addr;
728 if ((status == 0) || (status == -ECONNREFUSED)) {
729 event.private_data_len = ep->plen;
730 event.private_data = ep->mpa_pkt + sizeof(struct mpa_message);
733 PDBG("%s ep %p tid %d status %d\n", __func__, ep,
735 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
738 ep->com.cm_id->rem_ref(ep->com.cm_id);
739 ep->com.cm_id = NULL;
744 static void connect_request_upcall(struct iwch_ep *ep)
746 struct iw_cm_event event;
748 PDBG("%s ep %p tid %d\n", __func__, ep, ep->hwtid);
749 memset(&event, 0, sizeof(event));
750 event.event = IW_CM_EVENT_CONNECT_REQUEST;
751 event.local_addr = ep->com.local_addr;
752 event.remote_addr = ep->com.remote_addr;
753 event.private_data_len = ep->plen;
754 event.private_data = ep->mpa_pkt + sizeof(struct mpa_message);
755 event.provider_data = ep;
756 if (state_read(&ep->parent_ep->com) != DEAD)
757 ep->parent_ep->com.cm_id->event_handler(
758 ep->parent_ep->com.cm_id,
760 put_ep(&ep->parent_ep->com);
761 ep->parent_ep = NULL;
764 static void established_upcall(struct iwch_ep *ep)
766 struct iw_cm_event event;
768 PDBG("%s ep %p\n", __func__, ep);
769 memset(&event, 0, sizeof(event));
770 event.event = IW_CM_EVENT_ESTABLISHED;
772 PDBG("%s ep %p tid %d\n", __func__, ep, ep->hwtid);
773 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
777 static int update_rx_credits(struct iwch_ep *ep, u32 credits)
779 struct cpl_rx_data_ack *req;
782 PDBG("%s ep %p credits %u\n", __func__, ep, credits);
783 skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
785 printk(KERN_ERR MOD "update_rx_credits - cannot alloc skb!\n");
789 req = (struct cpl_rx_data_ack *) skb_put(skb, sizeof(*req));
790 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
791 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_RX_DATA_ACK, ep->hwtid));
792 req->credit_dack = htonl(V_RX_CREDITS(credits) | V_RX_FORCE_ACK(1));
793 skb->priority = CPL_PRIORITY_ACK;
794 cxgb3_ofld_send(ep->com.tdev, skb);
798 static void process_mpa_reply(struct iwch_ep *ep, struct sk_buff *skb)
800 struct mpa_message *mpa;
802 struct iwch_qp_attributes attrs;
803 enum iwch_qp_attr_mask mask;
806 PDBG("%s ep %p\n", __func__, ep);
809 * Stop mpa timer. If it expired, then the state has
810 * changed and we bail since ep_timeout already aborted
814 if (state_read(&ep->com) != MPA_REQ_SENT)
818 * If we get more than the supported amount of private data
819 * then we must fail this connection.
821 if (ep->mpa_pkt_len + skb->len > sizeof(ep->mpa_pkt)) {
827 * copy the new data into our accumulation buffer.
829 skb_copy_from_linear_data(skb, &(ep->mpa_pkt[ep->mpa_pkt_len]),
831 ep->mpa_pkt_len += skb->len;
834 * if we don't even have the mpa message, then bail.
836 if (ep->mpa_pkt_len < sizeof(*mpa))
838 mpa = (struct mpa_message *) ep->mpa_pkt;
840 /* Validate MPA header. */
841 if (mpa->revision != mpa_rev) {
845 if (memcmp(mpa->key, MPA_KEY_REP, sizeof(mpa->key))) {
850 plen = ntohs(mpa->private_data_size);
853 * Fail if there's too much private data.
855 if (plen > MPA_MAX_PRIVATE_DATA) {
861 * If plen does not account for pkt size
863 if (ep->mpa_pkt_len > (sizeof(*mpa) + plen)) {
868 ep->plen = (u8) plen;
871 * If we don't have all the pdata yet, then bail.
872 * We'll continue process when more data arrives.
874 if (ep->mpa_pkt_len < (sizeof(*mpa) + plen))
877 if (mpa->flags & MPA_REJECT) {
883 * If we get here we have accumulated the entire mpa
884 * start reply message including private data. And
885 * the MPA header is valid.
887 state_set(&ep->com, FPDU_MODE);
888 ep->mpa_attr.crc_enabled = (mpa->flags & MPA_CRC) | crc_enabled ? 1 : 0;
889 ep->mpa_attr.recv_marker_enabled = markers_enabled;
890 ep->mpa_attr.xmit_marker_enabled = mpa->flags & MPA_MARKERS ? 1 : 0;
891 ep->mpa_attr.version = mpa_rev;
892 PDBG("%s - crc_enabled=%d, recv_marker_enabled=%d, "
893 "xmit_marker_enabled=%d, version=%d\n", __func__,
894 ep->mpa_attr.crc_enabled, ep->mpa_attr.recv_marker_enabled,
895 ep->mpa_attr.xmit_marker_enabled, ep->mpa_attr.version);
897 attrs.mpa_attr = ep->mpa_attr;
898 attrs.max_ird = ep->ird;
899 attrs.max_ord = ep->ord;
900 attrs.llp_stream_handle = ep;
901 attrs.next_state = IWCH_QP_STATE_RTS;
903 mask = IWCH_QP_ATTR_NEXT_STATE |
904 IWCH_QP_ATTR_LLP_STREAM_HANDLE | IWCH_QP_ATTR_MPA_ATTR |
905 IWCH_QP_ATTR_MAX_IRD | IWCH_QP_ATTR_MAX_ORD;
907 /* bind QP and TID with INIT_WR */
908 err = iwch_modify_qp(ep->com.qp->rhp,
909 ep->com.qp, mask, &attrs, 1);
913 abort_connection(ep, skb, GFP_KERNEL);
915 connect_reply_upcall(ep, err);
919 static void process_mpa_request(struct iwch_ep *ep, struct sk_buff *skb)
921 struct mpa_message *mpa;
924 PDBG("%s ep %p\n", __func__, ep);
927 * Stop mpa timer. If it expired, then the state has
928 * changed and we bail since ep_timeout already aborted
932 if (state_read(&ep->com) != MPA_REQ_WAIT)
936 * If we get more than the supported amount of private data
937 * then we must fail this connection.
939 if (ep->mpa_pkt_len + skb->len > sizeof(ep->mpa_pkt)) {
940 abort_connection(ep, skb, GFP_KERNEL);
944 PDBG("%s enter (%s line %u)\n", __func__, __FILE__, __LINE__);
947 * Copy the new data into our accumulation buffer.
949 skb_copy_from_linear_data(skb, &(ep->mpa_pkt[ep->mpa_pkt_len]),
951 ep->mpa_pkt_len += skb->len;
954 * If we don't even have the mpa message, then bail.
955 * We'll continue process when more data arrives.
957 if (ep->mpa_pkt_len < sizeof(*mpa))
959 PDBG("%s enter (%s line %u)\n", __func__, __FILE__, __LINE__);
960 mpa = (struct mpa_message *) ep->mpa_pkt;
963 * Validate MPA Header.
965 if (mpa->revision != mpa_rev) {
966 abort_connection(ep, skb, GFP_KERNEL);
970 if (memcmp(mpa->key, MPA_KEY_REQ, sizeof(mpa->key))) {
971 abort_connection(ep, skb, GFP_KERNEL);
975 plen = ntohs(mpa->private_data_size);
978 * Fail if there's too much private data.
980 if (plen > MPA_MAX_PRIVATE_DATA) {
981 abort_connection(ep, skb, GFP_KERNEL);
986 * If plen does not account for pkt size
988 if (ep->mpa_pkt_len > (sizeof(*mpa) + plen)) {
989 abort_connection(ep, skb, GFP_KERNEL);
992 ep->plen = (u8) plen;
995 * If we don't have all the pdata yet, then bail.
997 if (ep->mpa_pkt_len < (sizeof(*mpa) + plen))
1001 * If we get here we have accumulated the entire mpa
1002 * start reply message including private data.
1004 ep->mpa_attr.crc_enabled = (mpa->flags & MPA_CRC) | crc_enabled ? 1 : 0;
1005 ep->mpa_attr.recv_marker_enabled = markers_enabled;
1006 ep->mpa_attr.xmit_marker_enabled = mpa->flags & MPA_MARKERS ? 1 : 0;
1007 ep->mpa_attr.version = mpa_rev;
1008 PDBG("%s - crc_enabled=%d, recv_marker_enabled=%d, "
1009 "xmit_marker_enabled=%d, version=%d\n", __func__,
1010 ep->mpa_attr.crc_enabled, ep->mpa_attr.recv_marker_enabled,
1011 ep->mpa_attr.xmit_marker_enabled, ep->mpa_attr.version);
1013 state_set(&ep->com, MPA_REQ_RCVD);
1016 connect_request_upcall(ep);
1020 static int rx_data(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1022 struct iwch_ep *ep = ctx;
1023 struct cpl_rx_data *hdr = cplhdr(skb);
1024 unsigned int dlen = ntohs(hdr->len);
1026 PDBG("%s ep %p dlen %u\n", __func__, ep, dlen);
1028 skb_pull(skb, sizeof(*hdr));
1029 skb_trim(skb, dlen);
1031 ep->rcv_seq += dlen;
1032 BUG_ON(ep->rcv_seq != (ntohl(hdr->seq) + dlen));
1034 switch (state_read(&ep->com)) {
1036 process_mpa_reply(ep, skb);
1039 process_mpa_request(ep, skb);
1044 printk(KERN_ERR MOD "%s Unexpected streaming data."
1045 " ep %p state %d tid %d\n",
1046 __func__, ep, state_read(&ep->com), ep->hwtid);
1049 * The ep will timeout and inform the ULP of the failure.
1055 /* update RX credits */
1056 update_rx_credits(ep, dlen);
1058 return CPL_RET_BUF_DONE;
1062 * Upcall from the adapter indicating data has been transmitted.
1063 * For us its just the single MPA request or reply. We can now free
1064 * the skb holding the mpa message.
1066 static int tx_ack(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1068 struct iwch_ep *ep = ctx;
1069 struct cpl_wr_ack *hdr = cplhdr(skb);
1070 unsigned int credits = ntohs(hdr->credits);
1072 PDBG("%s ep %p credits %u\n", __func__, ep, credits);
1075 return CPL_RET_BUF_DONE;
1076 BUG_ON(credits != 1);
1077 BUG_ON(ep->mpa_skb == NULL);
1078 kfree_skb(ep->mpa_skb);
1080 dst_confirm(ep->dst);
1081 if (state_read(&ep->com) == MPA_REP_SENT) {
1082 ep->com.rpl_done = 1;
1083 PDBG("waking up ep %p\n", ep);
1084 wake_up(&ep->com.waitq);
1086 return CPL_RET_BUF_DONE;
1089 static int abort_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1091 struct iwch_ep *ep = ctx;
1092 unsigned long flags;
1095 PDBG("%s ep %p\n", __func__, ep);
1099 * We get 2 abort replies from the HW. The first one must
1100 * be ignored except for scribbling that we need one more.
1102 if (!(ep->flags & ABORT_REQ_IN_PROGRESS)) {
1103 ep->flags |= ABORT_REQ_IN_PROGRESS;
1104 return CPL_RET_BUF_DONE;
1107 spin_lock_irqsave(&ep->com.lock, flags);
1108 switch (ep->com.state) {
1110 close_complete_upcall(ep);
1111 __state_set(&ep->com, DEAD);
1115 printk(KERN_ERR "%s ep %p state %d\n",
1116 __func__, ep, ep->com.state);
1119 spin_unlock_irqrestore(&ep->com.lock, flags);
1122 release_ep_resources(ep);
1123 return CPL_RET_BUF_DONE;
1127 * Return whether a failed active open has allocated a TID
1129 static inline int act_open_has_tid(int status)
1131 return status != CPL_ERR_TCAM_FULL && status != CPL_ERR_CONN_EXIST &&
1132 status != CPL_ERR_ARP_MISS;
1135 static int act_open_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1137 struct iwch_ep *ep = ctx;
1138 struct cpl_act_open_rpl *rpl = cplhdr(skb);
1140 PDBG("%s ep %p status %u errno %d\n", __func__, ep, rpl->status,
1141 status2errno(rpl->status));
1142 connect_reply_upcall(ep, status2errno(rpl->status));
1143 state_set(&ep->com, DEAD);
1144 if (ep->com.tdev->type != T3A && act_open_has_tid(rpl->status))
1145 release_tid(ep->com.tdev, GET_TID(rpl), NULL);
1146 cxgb3_free_atid(ep->com.tdev, ep->atid);
1147 dst_release(ep->dst);
1148 l2t_release(L2DATA(ep->com.tdev), ep->l2t);
1150 return CPL_RET_BUF_DONE;
1153 static int listen_start(struct iwch_listen_ep *ep)
1155 struct sk_buff *skb;
1156 struct cpl_pass_open_req *req;
1158 PDBG("%s ep %p\n", __func__, ep);
1159 skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
1161 printk(KERN_ERR MOD "t3c_listen_start failed to alloc skb!\n");
1165 req = (struct cpl_pass_open_req *) skb_put(skb, sizeof(*req));
1166 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
1167 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ, ep->stid));
1168 req->local_port = ep->com.local_addr.sin_port;
1169 req->local_ip = ep->com.local_addr.sin_addr.s_addr;
1172 req->peer_netmask = 0;
1173 req->opt0h = htonl(F_DELACK | F_TCAM_BYPASS);
1174 req->opt0l = htonl(V_RCV_BUFSIZ(rcv_win>>10));
1175 req->opt1 = htonl(V_CONN_POLICY(CPL_CONN_POLICY_ASK));
1178 cxgb3_ofld_send(ep->com.tdev, skb);
1182 static int pass_open_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1184 struct iwch_listen_ep *ep = ctx;
1185 struct cpl_pass_open_rpl *rpl = cplhdr(skb);
1187 PDBG("%s ep %p status %d error %d\n", __func__, ep,
1188 rpl->status, status2errno(rpl->status));
1189 ep->com.rpl_err = status2errno(rpl->status);
1190 ep->com.rpl_done = 1;
1191 wake_up(&ep->com.waitq);
1193 return CPL_RET_BUF_DONE;
1196 static int listen_stop(struct iwch_listen_ep *ep)
1198 struct sk_buff *skb;
1199 struct cpl_close_listserv_req *req;
1201 PDBG("%s ep %p\n", __func__, ep);
1202 skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
1204 printk(KERN_ERR MOD "%s - failed to alloc skb\n", __func__);
1207 req = (struct cpl_close_listserv_req *) skb_put(skb, sizeof(*req));
1208 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
1210 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_LISTSRV_REQ, ep->stid));
1212 cxgb3_ofld_send(ep->com.tdev, skb);
1216 static int close_listsrv_rpl(struct t3cdev *tdev, struct sk_buff *skb,
1219 struct iwch_listen_ep *ep = ctx;
1220 struct cpl_close_listserv_rpl *rpl = cplhdr(skb);
1222 PDBG("%s ep %p\n", __func__, ep);
1223 ep->com.rpl_err = status2errno(rpl->status);
1224 ep->com.rpl_done = 1;
1225 wake_up(&ep->com.waitq);
1226 return CPL_RET_BUF_DONE;
1229 static void accept_cr(struct iwch_ep *ep, __be32 peer_ip, struct sk_buff *skb)
1231 struct cpl_pass_accept_rpl *rpl;
1232 unsigned int mtu_idx;
1233 u32 opt0h, opt0l, opt2;
1236 PDBG("%s ep %p\n", __func__, ep);
1237 BUG_ON(skb_cloned(skb));
1238 skb_trim(skb, sizeof(*rpl));
1240 mtu_idx = find_best_mtu(T3C_DATA(ep->com.tdev), dst_mtu(ep->dst));
1241 wscale = compute_wscale(rcv_win);
1242 opt0h = V_NAGLE(0) |
1246 V_WND_SCALE(wscale) |
1247 V_MSS_IDX(mtu_idx) |
1248 V_L2T_IDX(ep->l2t->idx) | V_TX_CHANNEL(ep->l2t->smt_idx);
1249 opt0l = V_TOS((ep->tos >> 2) & M_TOS) | V_RCV_BUFSIZ(rcv_win>>10);
1250 opt2 = V_FLAVORS_VALID(1) | V_CONG_CONTROL_FLAVOR(cong_flavor);
1253 rpl->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
1254 OPCODE_TID(rpl) = htonl(MK_OPCODE_TID(CPL_PASS_ACCEPT_RPL, ep->hwtid));
1255 rpl->peer_ip = peer_ip;
1256 rpl->opt0h = htonl(opt0h);
1257 rpl->opt0l_status = htonl(opt0l | CPL_PASS_OPEN_ACCEPT);
1258 rpl->opt2 = htonl(opt2);
1259 rpl->rsvd = rpl->opt2; /* workaround for HW bug */
1260 skb->priority = CPL_PRIORITY_SETUP;
1261 l2t_send(ep->com.tdev, skb, ep->l2t);
1266 static void reject_cr(struct t3cdev *tdev, u32 hwtid, __be32 peer_ip,
1267 struct sk_buff *skb)
1269 PDBG("%s t3cdev %p tid %u peer_ip %x\n", __func__, tdev, hwtid,
1271 BUG_ON(skb_cloned(skb));
1272 skb_trim(skb, sizeof(struct cpl_tid_release));
1275 if (tdev->type != T3A)
1276 release_tid(tdev, hwtid, skb);
1278 struct cpl_pass_accept_rpl *rpl;
1281 skb->priority = CPL_PRIORITY_SETUP;
1282 rpl->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
1283 OPCODE_TID(rpl) = htonl(MK_OPCODE_TID(CPL_PASS_ACCEPT_RPL,
1285 rpl->peer_ip = peer_ip;
1286 rpl->opt0h = htonl(F_TCAM_BYPASS);
1287 rpl->opt0l_status = htonl(CPL_PASS_OPEN_REJECT);
1289 rpl->rsvd = rpl->opt2;
1290 cxgb3_ofld_send(tdev, skb);
1294 static int pass_accept_req(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1296 struct iwch_ep *child_ep, *parent_ep = ctx;
1297 struct cpl_pass_accept_req *req = cplhdr(skb);
1298 unsigned int hwtid = GET_TID(req);
1299 struct dst_entry *dst;
1300 struct l2t_entry *l2t;
1304 PDBG("%s parent ep %p tid %u\n", __func__, parent_ep, hwtid);
1306 if (state_read(&parent_ep->com) != LISTEN) {
1307 printk(KERN_ERR "%s - listening ep not in LISTEN\n",
1313 * Find the netdev for this connection request.
1315 tim.mac_addr = req->dst_mac;
1316 tim.vlan_tag = ntohs(req->vlan_tag);
1317 if (tdev->ctl(tdev, GET_IFF_FROM_MAC, &tim) < 0 || !tim.dev) {
1319 "%s bad dst mac %02x %02x %02x %02x %02x %02x\n",
1330 /* Find output route */
1331 rt = find_route(tdev,
1335 req->peer_port, G_PASS_OPEN_TOS(ntohl(req->tos_tid)));
1337 printk(KERN_ERR MOD "%s - failed to find dst entry!\n",
1342 l2t = t3_l2t_get(tdev, dst->neighbour, dst->neighbour->dev);
1344 printk(KERN_ERR MOD "%s - failed to allocate l2t entry!\n",
1349 child_ep = alloc_ep(sizeof(*child_ep), GFP_KERNEL);
1351 printk(KERN_ERR MOD "%s - failed to allocate ep entry!\n",
1353 l2t_release(L2DATA(tdev), l2t);
1357 state_set(&child_ep->com, CONNECTING);
1358 child_ep->com.tdev = tdev;
1359 child_ep->com.cm_id = NULL;
1360 child_ep->com.local_addr.sin_family = PF_INET;
1361 child_ep->com.local_addr.sin_port = req->local_port;
1362 child_ep->com.local_addr.sin_addr.s_addr = req->local_ip;
1363 child_ep->com.remote_addr.sin_family = PF_INET;
1364 child_ep->com.remote_addr.sin_port = req->peer_port;
1365 child_ep->com.remote_addr.sin_addr.s_addr = req->peer_ip;
1366 get_ep(&parent_ep->com);
1367 child_ep->parent_ep = parent_ep;
1368 child_ep->tos = G_PASS_OPEN_TOS(ntohl(req->tos_tid));
1369 child_ep->l2t = l2t;
1370 child_ep->dst = dst;
1371 child_ep->hwtid = hwtid;
1372 init_timer(&child_ep->timer);
1373 cxgb3_insert_tid(tdev, &t3c_client, child_ep, hwtid);
1374 accept_cr(child_ep, req->peer_ip, skb);
1377 reject_cr(tdev, hwtid, req->peer_ip, skb);
1379 return CPL_RET_BUF_DONE;
1382 static int pass_establish(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1384 struct iwch_ep *ep = ctx;
1385 struct cpl_pass_establish *req = cplhdr(skb);
1387 PDBG("%s ep %p\n", __func__, ep);
1388 ep->snd_seq = ntohl(req->snd_isn);
1389 ep->rcv_seq = ntohl(req->rcv_isn);
1391 set_emss(ep, ntohs(req->tcp_opt));
1393 dst_confirm(ep->dst);
1394 state_set(&ep->com, MPA_REQ_WAIT);
1397 return CPL_RET_BUF_DONE;
1400 static int peer_close(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1402 struct iwch_ep *ep = ctx;
1403 struct iwch_qp_attributes attrs;
1404 unsigned long flags;
1408 PDBG("%s ep %p\n", __func__, ep);
1409 dst_confirm(ep->dst);
1411 spin_lock_irqsave(&ep->com.lock, flags);
1412 switch (ep->com.state) {
1414 __state_set(&ep->com, CLOSING);
1417 __state_set(&ep->com, CLOSING);
1418 connect_reply_upcall(ep, -ECONNRESET);
1423 * We're gonna mark this puppy DEAD, but keep
1424 * the reference on it until the ULP accepts or
1427 __state_set(&ep->com, CLOSING);
1431 __state_set(&ep->com, CLOSING);
1432 ep->com.rpl_done = 1;
1433 ep->com.rpl_err = -ECONNRESET;
1434 PDBG("waking up ep %p\n", ep);
1435 wake_up(&ep->com.waitq);
1439 __state_set(&ep->com, CLOSING);
1440 attrs.next_state = IWCH_QP_STATE_CLOSING;
1441 iwch_modify_qp(ep->com.qp->rhp, ep->com.qp,
1442 IWCH_QP_ATTR_NEXT_STATE, &attrs, 1);
1443 peer_close_upcall(ep);
1449 __state_set(&ep->com, MORIBUND);
1454 if (ep->com.cm_id && ep->com.qp) {
1455 attrs.next_state = IWCH_QP_STATE_IDLE;
1456 iwch_modify_qp(ep->com.qp->rhp, ep->com.qp,
1457 IWCH_QP_ATTR_NEXT_STATE, &attrs, 1);
1459 close_complete_upcall(ep);
1460 __state_set(&ep->com, DEAD);
1470 spin_unlock_irqrestore(&ep->com.lock, flags);
1472 iwch_ep_disconnect(ep, 0, GFP_KERNEL);
1474 release_ep_resources(ep);
1475 return CPL_RET_BUF_DONE;
1479 * Returns whether an ABORT_REQ_RSS message is a negative advice.
1481 static int is_neg_adv_abort(unsigned int status)
1483 return status == CPL_ERR_RTX_NEG_ADVICE ||
1484 status == CPL_ERR_PERSIST_NEG_ADVICE;
1487 static int peer_abort(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1489 struct cpl_abort_req_rss *req = cplhdr(skb);
1490 struct iwch_ep *ep = ctx;
1491 struct cpl_abort_rpl *rpl;
1492 struct sk_buff *rpl_skb;
1493 struct iwch_qp_attributes attrs;
1496 unsigned long flags;
1498 if (is_neg_adv_abort(req->status)) {
1499 PDBG("%s neg_adv_abort ep %p tid %d\n", __func__, ep,
1501 t3_l2t_send_event(ep->com.tdev, ep->l2t);
1502 return CPL_RET_BUF_DONE;
1506 * We get 2 peer aborts from the HW. The first one must
1507 * be ignored except for scribbling that we need one more.
1509 if (!(ep->flags & PEER_ABORT_IN_PROGRESS)) {
1510 ep->flags |= PEER_ABORT_IN_PROGRESS;
1511 return CPL_RET_BUF_DONE;
1514 spin_lock_irqsave(&ep->com.lock, flags);
1515 PDBG("%s ep %p state %u\n", __func__, ep, ep->com.state);
1516 switch (ep->com.state) {
1524 connect_reply_upcall(ep, -ECONNRESET);
1527 ep->com.rpl_done = 1;
1528 ep->com.rpl_err = -ECONNRESET;
1529 PDBG("waking up ep %p\n", ep);
1530 wake_up(&ep->com.waitq);
1535 * We're gonna mark this puppy DEAD, but keep
1536 * the reference on it until the ULP accepts or
1546 if (ep->com.cm_id && ep->com.qp) {
1547 attrs.next_state = IWCH_QP_STATE_ERROR;
1548 ret = iwch_modify_qp(ep->com.qp->rhp,
1549 ep->com.qp, IWCH_QP_ATTR_NEXT_STATE,
1553 "%s - qp <- error failed!\n",
1556 peer_abort_upcall(ep);
1561 PDBG("%s PEER_ABORT IN DEAD STATE!!!!\n", __func__);
1562 spin_unlock_irqrestore(&ep->com.lock, flags);
1563 return CPL_RET_BUF_DONE;
1568 dst_confirm(ep->dst);
1569 if (ep->com.state != ABORTING) {
1570 __state_set(&ep->com, DEAD);
1573 spin_unlock_irqrestore(&ep->com.lock, flags);
1575 rpl_skb = get_skb(skb, sizeof(*rpl), GFP_KERNEL);
1577 printk(KERN_ERR MOD "%s - cannot allocate skb!\n",
1582 rpl_skb->priority = CPL_PRIORITY_DATA;
1583 rpl = (struct cpl_abort_rpl *) skb_put(rpl_skb, sizeof(*rpl));
1584 rpl->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_HOST_ABORT_CON_RPL));
1585 rpl->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
1586 OPCODE_TID(rpl) = htonl(MK_OPCODE_TID(CPL_ABORT_RPL, ep->hwtid));
1587 rpl->cmd = CPL_ABORT_NO_RST;
1588 cxgb3_ofld_send(ep->com.tdev, rpl_skb);
1591 release_ep_resources(ep);
1592 return CPL_RET_BUF_DONE;
1595 static int close_con_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1597 struct iwch_ep *ep = ctx;
1598 struct iwch_qp_attributes attrs;
1599 unsigned long flags;
1602 PDBG("%s ep %p\n", __func__, ep);
1605 /* The cm_id may be null if we failed to connect */
1606 spin_lock_irqsave(&ep->com.lock, flags);
1607 switch (ep->com.state) {
1609 __state_set(&ep->com, MORIBUND);
1613 if ((ep->com.cm_id) && (ep->com.qp)) {
1614 attrs.next_state = IWCH_QP_STATE_IDLE;
1615 iwch_modify_qp(ep->com.qp->rhp,
1617 IWCH_QP_ATTR_NEXT_STATE,
1620 close_complete_upcall(ep);
1621 __state_set(&ep->com, DEAD);
1631 spin_unlock_irqrestore(&ep->com.lock, flags);
1633 release_ep_resources(ep);
1634 return CPL_RET_BUF_DONE;
1638 * T3A does 3 things when a TERM is received:
1639 * 1) send up a CPL_RDMA_TERMINATE message with the TERM packet
1640 * 2) generate an async event on the QP with the TERMINATE opcode
1641 * 3) post a TERMINATE opcde cqe into the associated CQ.
1643 * For (1), we save the message in the qp for later consumer consumption.
1644 * For (2), we move the QP into TERMINATE, post a QP event and disconnect.
1645 * For (3), we toss the CQE in cxio_poll_cq().
1647 * terminate() handles case (1)...
1649 static int terminate(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1651 struct iwch_ep *ep = ctx;
1653 PDBG("%s ep %p\n", __func__, ep);
1654 skb_pull(skb, sizeof(struct cpl_rdma_terminate));
1655 PDBG("%s saving %d bytes of term msg\n", __func__, skb->len);
1656 skb_copy_from_linear_data(skb, ep->com.qp->attr.terminate_buffer,
1658 ep->com.qp->attr.terminate_msg_len = skb->len;
1659 ep->com.qp->attr.is_terminate_local = 0;
1660 return CPL_RET_BUF_DONE;
1663 static int ec_status(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1665 struct cpl_rdma_ec_status *rep = cplhdr(skb);
1666 struct iwch_ep *ep = ctx;
1668 PDBG("%s ep %p tid %u status %d\n", __func__, ep, ep->hwtid,
1671 struct iwch_qp_attributes attrs;
1673 printk(KERN_ERR MOD "%s BAD CLOSE - Aborting tid %u\n",
1674 __func__, ep->hwtid);
1676 attrs.next_state = IWCH_QP_STATE_ERROR;
1677 iwch_modify_qp(ep->com.qp->rhp,
1678 ep->com.qp, IWCH_QP_ATTR_NEXT_STATE,
1680 abort_connection(ep, NULL, GFP_KERNEL);
1682 return CPL_RET_BUF_DONE;
1685 static void ep_timeout(unsigned long arg)
1687 struct iwch_ep *ep = (struct iwch_ep *)arg;
1688 struct iwch_qp_attributes attrs;
1689 unsigned long flags;
1692 spin_lock_irqsave(&ep->com.lock, flags);
1693 PDBG("%s ep %p tid %u state %d\n", __func__, ep, ep->hwtid,
1695 switch (ep->com.state) {
1697 __state_set(&ep->com, ABORTING);
1698 connect_reply_upcall(ep, -ETIMEDOUT);
1701 __state_set(&ep->com, ABORTING);
1705 if (ep->com.cm_id && ep->com.qp) {
1706 attrs.next_state = IWCH_QP_STATE_ERROR;
1707 iwch_modify_qp(ep->com.qp->rhp,
1708 ep->com.qp, IWCH_QP_ATTR_NEXT_STATE,
1711 __state_set(&ep->com, ABORTING);
1714 printk(KERN_ERR "%s unexpected state ep %p state %u\n",
1715 __func__, ep, ep->com.state);
1719 spin_unlock_irqrestore(&ep->com.lock, flags);
1721 abort_connection(ep, NULL, GFP_ATOMIC);
1725 int iwch_reject_cr(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
1728 struct iwch_ep *ep = to_ep(cm_id);
1729 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1731 if (state_read(&ep->com) == DEAD) {
1735 BUG_ON(state_read(&ep->com) != MPA_REQ_RCVD);
1737 abort_connection(ep, NULL, GFP_KERNEL);
1739 err = send_mpa_reject(ep, pdata, pdata_len);
1740 err = iwch_ep_disconnect(ep, 0, GFP_KERNEL);
1745 int iwch_accept_cr(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
1748 struct iwch_qp_attributes attrs;
1749 enum iwch_qp_attr_mask mask;
1750 struct iwch_ep *ep = to_ep(cm_id);
1751 struct iwch_dev *h = to_iwch_dev(cm_id->device);
1752 struct iwch_qp *qp = get_qhp(h, conn_param->qpn);
1754 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1755 if (state_read(&ep->com) == DEAD)
1758 BUG_ON(state_read(&ep->com) != MPA_REQ_RCVD);
1761 if ((conn_param->ord > qp->rhp->attr.max_rdma_read_qp_depth) ||
1762 (conn_param->ird > qp->rhp->attr.max_rdma_reads_per_qp)) {
1763 abort_connection(ep, NULL, GFP_KERNEL);
1767 cm_id->add_ref(cm_id);
1768 ep->com.cm_id = cm_id;
1771 ep->com.rpl_done = 0;
1772 ep->com.rpl_err = 0;
1773 ep->ird = conn_param->ird;
1774 ep->ord = conn_param->ord;
1775 PDBG("%s %d ird %d ord %d\n", __func__, __LINE__, ep->ird, ep->ord);
1779 /* bind QP to EP and move to RTS */
1780 attrs.mpa_attr = ep->mpa_attr;
1781 attrs.max_ird = ep->ird;
1782 attrs.max_ord = ep->ord;
1783 attrs.llp_stream_handle = ep;
1784 attrs.next_state = IWCH_QP_STATE_RTS;
1786 /* bind QP and TID with INIT_WR */
1787 mask = IWCH_QP_ATTR_NEXT_STATE |
1788 IWCH_QP_ATTR_LLP_STREAM_HANDLE |
1789 IWCH_QP_ATTR_MPA_ATTR |
1790 IWCH_QP_ATTR_MAX_IRD |
1791 IWCH_QP_ATTR_MAX_ORD;
1793 err = iwch_modify_qp(ep->com.qp->rhp,
1794 ep->com.qp, mask, &attrs, 1);
1798 err = send_mpa_reply(ep, conn_param->private_data,
1799 conn_param->private_data_len);
1803 /* wait for wr_ack */
1804 wait_event(ep->com.waitq, ep->com.rpl_done);
1805 err = ep->com.rpl_err;
1809 state_set(&ep->com, FPDU_MODE);
1810 established_upcall(ep);
1814 ep->com.cm_id = NULL;
1816 cm_id->rem_ref(cm_id);
1821 static int is_loopback_dst(struct iw_cm_id *cm_id)
1823 struct net_device *dev;
1825 dev = ip_dev_find(&init_net, cm_id->remote_addr.sin_addr.s_addr);
1832 int iwch_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
1835 struct iwch_dev *h = to_iwch_dev(cm_id->device);
1839 if (is_loopback_dst(cm_id)) {
1844 ep = alloc_ep(sizeof(*ep), GFP_KERNEL);
1846 printk(KERN_ERR MOD "%s - cannot alloc ep.\n", __func__);
1850 init_timer(&ep->timer);
1851 ep->plen = conn_param->private_data_len;
1853 memcpy(ep->mpa_pkt + sizeof(struct mpa_message),
1854 conn_param->private_data, ep->plen);
1855 ep->ird = conn_param->ird;
1856 ep->ord = conn_param->ord;
1857 ep->com.tdev = h->rdev.t3cdev_p;
1859 cm_id->add_ref(cm_id);
1860 ep->com.cm_id = cm_id;
1861 ep->com.qp = get_qhp(h, conn_param->qpn);
1862 BUG_ON(!ep->com.qp);
1863 PDBG("%s qpn 0x%x qp %p cm_id %p\n", __func__, conn_param->qpn,
1867 * Allocate an active TID to initiate a TCP connection.
1869 ep->atid = cxgb3_alloc_atid(h->rdev.t3cdev_p, &t3c_client, ep);
1870 if (ep->atid == -1) {
1871 printk(KERN_ERR MOD "%s - cannot alloc atid.\n", __func__);
1877 rt = find_route(h->rdev.t3cdev_p,
1878 cm_id->local_addr.sin_addr.s_addr,
1879 cm_id->remote_addr.sin_addr.s_addr,
1880 cm_id->local_addr.sin_port,
1881 cm_id->remote_addr.sin_port, IPTOS_LOWDELAY);
1883 printk(KERN_ERR MOD "%s - cannot find route.\n", __func__);
1884 err = -EHOSTUNREACH;
1887 ep->dst = &rt->u.dst;
1889 /* get a l2t entry */
1890 ep->l2t = t3_l2t_get(ep->com.tdev, ep->dst->neighbour,
1891 ep->dst->neighbour->dev);
1893 printk(KERN_ERR MOD "%s - cannot alloc l2e.\n", __func__);
1898 state_set(&ep->com, CONNECTING);
1899 ep->tos = IPTOS_LOWDELAY;
1900 ep->com.local_addr = cm_id->local_addr;
1901 ep->com.remote_addr = cm_id->remote_addr;
1903 /* send connect request to rnic */
1904 err = send_connect(ep);
1908 l2t_release(L2DATA(h->rdev.t3cdev_p), ep->l2t);
1910 dst_release(ep->dst);
1912 cxgb3_free_atid(ep->com.tdev, ep->atid);
1919 int iwch_create_listen(struct iw_cm_id *cm_id, int backlog)
1922 struct iwch_dev *h = to_iwch_dev(cm_id->device);
1923 struct iwch_listen_ep *ep;
1928 ep = alloc_ep(sizeof(*ep), GFP_KERNEL);
1930 printk(KERN_ERR MOD "%s - cannot alloc ep.\n", __func__);
1934 PDBG("%s ep %p\n", __func__, ep);
1935 ep->com.tdev = h->rdev.t3cdev_p;
1936 cm_id->add_ref(cm_id);
1937 ep->com.cm_id = cm_id;
1938 ep->backlog = backlog;
1939 ep->com.local_addr = cm_id->local_addr;
1942 * Allocate a server TID.
1944 ep->stid = cxgb3_alloc_stid(h->rdev.t3cdev_p, &t3c_client, ep);
1945 if (ep->stid == -1) {
1946 printk(KERN_ERR MOD "%s - cannot alloc atid.\n", __func__);
1951 state_set(&ep->com, LISTEN);
1952 err = listen_start(ep);
1956 /* wait for pass_open_rpl */
1957 wait_event(ep->com.waitq, ep->com.rpl_done);
1958 err = ep->com.rpl_err;
1960 cm_id->provider_data = ep;
1964 cxgb3_free_stid(ep->com.tdev, ep->stid);
1966 cm_id->rem_ref(cm_id);
1973 int iwch_destroy_listen(struct iw_cm_id *cm_id)
1976 struct iwch_listen_ep *ep = to_listen_ep(cm_id);
1978 PDBG("%s ep %p\n", __func__, ep);
1981 state_set(&ep->com, DEAD);
1982 ep->com.rpl_done = 0;
1983 ep->com.rpl_err = 0;
1984 err = listen_stop(ep);
1985 wait_event(ep->com.waitq, ep->com.rpl_done);
1986 cxgb3_free_stid(ep->com.tdev, ep->stid);
1987 err = ep->com.rpl_err;
1988 cm_id->rem_ref(cm_id);
1993 int iwch_ep_disconnect(struct iwch_ep *ep, int abrupt, gfp_t gfp)
1996 unsigned long flags;
1999 spin_lock_irqsave(&ep->com.lock, flags);
2001 PDBG("%s ep %p state %s, abrupt %d\n", __func__, ep,
2002 states[ep->com.state], abrupt);
2004 switch (ep->com.state) {
2012 ep->com.state = ABORTING;
2014 ep->com.state = CLOSING;
2022 ep->com.state = ABORTING;
2024 ep->com.state = MORIBUND;
2029 PDBG("%s ignoring disconnect ep %p state %u\n",
2030 __func__, ep, ep->com.state);
2037 spin_unlock_irqrestore(&ep->com.lock, flags);
2040 ret = send_abort(ep, NULL, gfp);
2042 ret = send_halfclose(ep, gfp);
2047 int iwch_ep_redirect(void *ctx, struct dst_entry *old, struct dst_entry *new,
2048 struct l2t_entry *l2t)
2050 struct iwch_ep *ep = ctx;
2055 PDBG("%s ep %p redirect to dst %p l2t %p\n", __func__, ep, new,
2058 l2t_release(L2DATA(ep->com.tdev), ep->l2t);
2066 * All the CM events are handled on a work queue to have a safe context.
2068 static int sched(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
2070 struct iwch_ep_common *epc = ctx;
2075 * Save ctx and tdev in the skb->cb area.
2077 *((void **) skb->cb) = ctx;
2078 *((struct t3cdev **) (skb->cb + sizeof(void *))) = tdev;
2081 * Queue the skb and schedule the worker thread.
2083 skb_queue_tail(&rxq, skb);
2084 queue_work(workq, &skb_work);
2088 static int set_tcb_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
2090 struct cpl_set_tcb_rpl *rpl = cplhdr(skb);
2092 if (rpl->status != CPL_ERR_NONE) {
2093 printk(KERN_ERR MOD "Unexpected SET_TCB_RPL status %u "
2094 "for tid %u\n", rpl->status, GET_TID(rpl));
2096 return CPL_RET_BUF_DONE;
2099 int __init iwch_cm_init(void)
2101 skb_queue_head_init(&rxq);
2103 workq = create_singlethread_workqueue("iw_cxgb3");
2108 * All upcalls from the T3 Core go to sched() to
2109 * schedule the processing on a work queue.
2111 t3c_handlers[CPL_ACT_ESTABLISH] = sched;
2112 t3c_handlers[CPL_ACT_OPEN_RPL] = sched;
2113 t3c_handlers[CPL_RX_DATA] = sched;
2114 t3c_handlers[CPL_TX_DMA_ACK] = sched;
2115 t3c_handlers[CPL_ABORT_RPL_RSS] = sched;
2116 t3c_handlers[CPL_ABORT_RPL] = sched;
2117 t3c_handlers[CPL_PASS_OPEN_RPL] = sched;
2118 t3c_handlers[CPL_CLOSE_LISTSRV_RPL] = sched;
2119 t3c_handlers[CPL_PASS_ACCEPT_REQ] = sched;
2120 t3c_handlers[CPL_PASS_ESTABLISH] = sched;
2121 t3c_handlers[CPL_PEER_CLOSE] = sched;
2122 t3c_handlers[CPL_CLOSE_CON_RPL] = sched;
2123 t3c_handlers[CPL_ABORT_REQ_RSS] = sched;
2124 t3c_handlers[CPL_RDMA_TERMINATE] = sched;
2125 t3c_handlers[CPL_RDMA_EC_STATUS] = sched;
2126 t3c_handlers[CPL_SET_TCB_RPL] = set_tcb_rpl;
2129 * These are the real handlers that are called from a
2132 work_handlers[CPL_ACT_ESTABLISH] = act_establish;
2133 work_handlers[CPL_ACT_OPEN_RPL] = act_open_rpl;
2134 work_handlers[CPL_RX_DATA] = rx_data;
2135 work_handlers[CPL_TX_DMA_ACK] = tx_ack;
2136 work_handlers[CPL_ABORT_RPL_RSS] = abort_rpl;
2137 work_handlers[CPL_ABORT_RPL] = abort_rpl;
2138 work_handlers[CPL_PASS_OPEN_RPL] = pass_open_rpl;
2139 work_handlers[CPL_CLOSE_LISTSRV_RPL] = close_listsrv_rpl;
2140 work_handlers[CPL_PASS_ACCEPT_REQ] = pass_accept_req;
2141 work_handlers[CPL_PASS_ESTABLISH] = pass_establish;
2142 work_handlers[CPL_PEER_CLOSE] = peer_close;
2143 work_handlers[CPL_ABORT_REQ_RSS] = peer_abort;
2144 work_handlers[CPL_CLOSE_CON_RPL] = close_con_rpl;
2145 work_handlers[CPL_RDMA_TERMINATE] = terminate;
2146 work_handlers[CPL_RDMA_EC_STATUS] = ec_status;
2150 void __exit iwch_cm_term(void)
2152 flush_workqueue(workq);
2153 destroy_workqueue(workq);