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 del_timer_sync(&ep->timer);
132 static void release_tid(struct t3cdev *tdev, u32 hwtid, struct sk_buff *skb)
134 struct cpl_tid_release *req;
136 skb = get_skb(skb, sizeof *req, GFP_KERNEL);
139 req = (struct cpl_tid_release *) skb_put(skb, sizeof(*req));
140 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
141 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_TID_RELEASE, hwtid));
142 skb->priority = CPL_PRIORITY_SETUP;
143 cxgb3_ofld_send(tdev, skb);
147 int iwch_quiesce_tid(struct iwch_ep *ep)
149 struct cpl_set_tcb_field *req;
150 struct sk_buff *skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
154 req = (struct cpl_set_tcb_field *) skb_put(skb, sizeof(*req));
155 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
156 req->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
157 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, ep->hwtid));
160 req->word = htons(W_TCB_RX_QUIESCE);
161 req->mask = cpu_to_be64(1ULL << S_TCB_RX_QUIESCE);
162 req->val = cpu_to_be64(1 << S_TCB_RX_QUIESCE);
164 skb->priority = CPL_PRIORITY_DATA;
165 cxgb3_ofld_send(ep->com.tdev, skb);
169 int iwch_resume_tid(struct iwch_ep *ep)
171 struct cpl_set_tcb_field *req;
172 struct sk_buff *skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
176 req = (struct cpl_set_tcb_field *) skb_put(skb, sizeof(*req));
177 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
178 req->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
179 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, ep->hwtid));
182 req->word = htons(W_TCB_RX_QUIESCE);
183 req->mask = cpu_to_be64(1ULL << S_TCB_RX_QUIESCE);
186 skb->priority = CPL_PRIORITY_DATA;
187 cxgb3_ofld_send(ep->com.tdev, skb);
191 static void set_emss(struct iwch_ep *ep, u16 opt)
193 PDBG("%s ep %p opt %u\n", __func__, ep, opt);
194 ep->emss = T3C_DATA(ep->com.tdev)->mtus[G_TCPOPT_MSS(opt)] - 40;
195 if (G_TCPOPT_TSTAMP(opt))
199 PDBG("emss=%d\n", ep->emss);
202 static enum iwch_ep_state state_read(struct iwch_ep_common *epc)
205 enum iwch_ep_state state;
207 spin_lock_irqsave(&epc->lock, flags);
209 spin_unlock_irqrestore(&epc->lock, flags);
213 static void __state_set(struct iwch_ep_common *epc, enum iwch_ep_state new)
218 static void state_set(struct iwch_ep_common *epc, enum iwch_ep_state new)
222 spin_lock_irqsave(&epc->lock, flags);
223 PDBG("%s - %s -> %s\n", __func__, states[epc->state], states[new]);
224 __state_set(epc, new);
225 spin_unlock_irqrestore(&epc->lock, flags);
229 static void *alloc_ep(int size, gfp_t gfp)
231 struct iwch_ep_common *epc;
233 epc = kzalloc(size, gfp);
235 kref_init(&epc->kref);
236 spin_lock_init(&epc->lock);
237 init_waitqueue_head(&epc->waitq);
239 PDBG("%s alloc ep %p\n", __func__, epc);
243 void __free_ep(struct kref *kref)
245 struct iwch_ep_common *epc;
246 epc = container_of(kref, struct iwch_ep_common, kref);
247 PDBG("%s ep %p state %s\n", __func__, epc, states[state_read(epc)]);
251 static void release_ep_resources(struct iwch_ep *ep)
253 PDBG("%s ep %p tid %d\n", __func__, ep, ep->hwtid);
254 cxgb3_remove_tid(ep->com.tdev, (void *)ep, ep->hwtid);
255 dst_release(ep->dst);
256 l2t_release(L2DATA(ep->com.tdev), ep->l2t);
260 static void process_work(struct work_struct *work)
262 struct sk_buff *skb = NULL;
267 while ((skb = skb_dequeue(&rxq))) {
268 ep = *((void **) (skb->cb));
269 tdev = *((struct t3cdev **) (skb->cb + sizeof(void *)));
270 ret = work_handlers[G_OPCODE(ntohl((__force __be32)skb->csum))](tdev, skb, ep);
271 if (ret & CPL_RET_BUF_DONE)
275 * ep was referenced in sched(), and is freed here.
277 put_ep((struct iwch_ep_common *)ep);
281 static int status2errno(int status)
286 case CPL_ERR_CONN_RESET:
288 case CPL_ERR_ARP_MISS:
289 return -EHOSTUNREACH;
290 case CPL_ERR_CONN_TIMEDOUT:
292 case CPL_ERR_TCAM_FULL:
294 case CPL_ERR_CONN_EXIST:
302 * Try and reuse skbs already allocated...
304 static struct sk_buff *get_skb(struct sk_buff *skb, int len, gfp_t gfp)
306 if (skb && !skb_is_nonlinear(skb) && !skb_cloned(skb)) {
310 skb = alloc_skb(len, gfp);
315 static struct rtable *find_route(struct t3cdev *dev, __be32 local_ip,
316 __be32 peer_ip, __be16 local_port,
317 __be16 peer_port, u8 tos)
328 .proto = IPPROTO_TCP,
336 if (ip_route_output_flow(&init_net, &rt, &fl, NULL, 0))
341 static unsigned int find_best_mtu(const struct t3c_data *d, unsigned short mtu)
345 while (i < d->nmtus - 1 && d->mtus[i + 1] <= mtu)
350 static void arp_failure_discard(struct t3cdev *dev, struct sk_buff *skb)
352 PDBG("%s t3cdev %p\n", __func__, dev);
357 * Handle an ARP failure for an active open.
359 static void act_open_req_arp_failure(struct t3cdev *dev, struct sk_buff *skb)
361 printk(KERN_ERR MOD "ARP failure duing connect\n");
366 * Handle an ARP failure for a CPL_ABORT_REQ. Change it into a no RST variant
369 static void abort_arp_failure(struct t3cdev *dev, struct sk_buff *skb)
371 struct cpl_abort_req *req = cplhdr(skb);
373 PDBG("%s t3cdev %p\n", __func__, dev);
374 req->cmd = CPL_ABORT_NO_RST;
375 cxgb3_ofld_send(dev, skb);
378 static int send_halfclose(struct iwch_ep *ep, gfp_t gfp)
380 struct cpl_close_con_req *req;
383 PDBG("%s ep %p\n", __func__, ep);
384 skb = get_skb(NULL, sizeof(*req), gfp);
386 printk(KERN_ERR MOD "%s - failed to alloc skb\n", __func__);
389 skb->priority = CPL_PRIORITY_DATA;
390 set_arp_failure_handler(skb, arp_failure_discard);
391 req = (struct cpl_close_con_req *) skb_put(skb, sizeof(*req));
392 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_CLOSE_CON));
393 req->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
394 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_CON_REQ, ep->hwtid));
395 l2t_send(ep->com.tdev, skb, ep->l2t);
399 static int send_abort(struct iwch_ep *ep, struct sk_buff *skb, gfp_t gfp)
401 struct cpl_abort_req *req;
403 PDBG("%s ep %p\n", __func__, ep);
404 skb = get_skb(skb, sizeof(*req), gfp);
406 printk(KERN_ERR MOD "%s - failed to alloc skb.\n",
410 skb->priority = CPL_PRIORITY_DATA;
411 set_arp_failure_handler(skb, abort_arp_failure);
412 req = (struct cpl_abort_req *) skb_put(skb, sizeof(*req));
413 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_HOST_ABORT_CON_REQ));
414 req->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
415 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_ABORT_REQ, ep->hwtid));
416 req->cmd = CPL_ABORT_SEND_RST;
417 l2t_send(ep->com.tdev, skb, ep->l2t);
421 static int send_connect(struct iwch_ep *ep)
423 struct cpl_act_open_req *req;
425 u32 opt0h, opt0l, opt2;
426 unsigned int mtu_idx;
429 PDBG("%s ep %p\n", __func__, ep);
431 skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
433 printk(KERN_ERR MOD "%s - failed to alloc skb.\n",
437 mtu_idx = find_best_mtu(T3C_DATA(ep->com.tdev), dst_mtu(ep->dst));
438 wscale = compute_wscale(rcv_win);
443 V_WND_SCALE(wscale) |
445 V_L2T_IDX(ep->l2t->idx) | V_TX_CHANNEL(ep->l2t->smt_idx);
446 opt0l = V_TOS((ep->tos >> 2) & M_TOS) | V_RCV_BUFSIZ(rcv_win>>10);
447 opt2 = V_FLAVORS_VALID(1) | V_CONG_CONTROL_FLAVOR(cong_flavor);
448 skb->priority = CPL_PRIORITY_SETUP;
449 set_arp_failure_handler(skb, act_open_req_arp_failure);
451 req = (struct cpl_act_open_req *) skb_put(skb, sizeof(*req));
452 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
453 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_ACT_OPEN_REQ, ep->atid));
454 req->local_port = ep->com.local_addr.sin_port;
455 req->peer_port = ep->com.remote_addr.sin_port;
456 req->local_ip = ep->com.local_addr.sin_addr.s_addr;
457 req->peer_ip = ep->com.remote_addr.sin_addr.s_addr;
458 req->opt0h = htonl(opt0h);
459 req->opt0l = htonl(opt0l);
461 req->opt2 = htonl(opt2);
462 l2t_send(ep->com.tdev, skb, ep->l2t);
466 static void send_mpa_req(struct iwch_ep *ep, struct sk_buff *skb)
469 struct tx_data_wr *req;
470 struct mpa_message *mpa;
473 PDBG("%s ep %p pd_len %d\n", __func__, ep, ep->plen);
475 BUG_ON(skb_cloned(skb));
477 mpalen = sizeof(*mpa) + ep->plen;
478 if (skb->data + mpalen + sizeof(*req) > skb_end_pointer(skb)) {
480 skb=alloc_skb(mpalen + sizeof(*req), GFP_KERNEL);
482 connect_reply_upcall(ep, -ENOMEM);
487 skb_reserve(skb, sizeof(*req));
488 skb_put(skb, mpalen);
489 skb->priority = CPL_PRIORITY_DATA;
490 mpa = (struct mpa_message *) skb->data;
491 memset(mpa, 0, sizeof(*mpa));
492 memcpy(mpa->key, MPA_KEY_REQ, sizeof(mpa->key));
493 mpa->flags = (crc_enabled ? MPA_CRC : 0) |
494 (markers_enabled ? MPA_MARKERS : 0);
495 mpa->private_data_size = htons(ep->plen);
496 mpa->revision = mpa_rev;
499 memcpy(mpa->private_data, ep->mpa_pkt + sizeof(*mpa), ep->plen);
502 * Reference the mpa skb. This ensures the data area
503 * will remain in memory until the hw acks the tx.
504 * Function tx_ack() will deref it.
507 set_arp_failure_handler(skb, arp_failure_discard);
508 skb_reset_transport_header(skb);
510 req = (struct tx_data_wr *) skb_push(skb, sizeof(*req));
511 req->wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_TX_DATA));
512 req->wr_lo = htonl(V_WR_TID(ep->hwtid));
513 req->len = htonl(len);
514 req->param = htonl(V_TX_PORT(ep->l2t->smt_idx) |
515 V_TX_SNDBUF(snd_win>>15));
516 req->flags = htonl(F_TX_INIT);
517 req->sndseq = htonl(ep->snd_seq);
520 l2t_send(ep->com.tdev, skb, ep->l2t);
522 state_set(&ep->com, MPA_REQ_SENT);
526 static int send_mpa_reject(struct iwch_ep *ep, const void *pdata, u8 plen)
529 struct tx_data_wr *req;
530 struct mpa_message *mpa;
533 PDBG("%s ep %p plen %d\n", __func__, ep, plen);
535 mpalen = sizeof(*mpa) + plen;
537 skb = get_skb(NULL, mpalen + sizeof(*req), GFP_KERNEL);
539 printk(KERN_ERR MOD "%s - cannot alloc skb!\n", __func__);
542 skb_reserve(skb, sizeof(*req));
543 mpa = (struct mpa_message *) skb_put(skb, mpalen);
544 memset(mpa, 0, sizeof(*mpa));
545 memcpy(mpa->key, MPA_KEY_REP, sizeof(mpa->key));
546 mpa->flags = MPA_REJECT;
547 mpa->revision = mpa_rev;
548 mpa->private_data_size = htons(plen);
550 memcpy(mpa->private_data, pdata, plen);
553 * Reference the mpa skb again. This ensures the data area
554 * will remain in memory until the hw acks the tx.
555 * Function tx_ack() will deref it.
558 skb->priority = CPL_PRIORITY_DATA;
559 set_arp_failure_handler(skb, arp_failure_discard);
560 skb_reset_transport_header(skb);
561 req = (struct tx_data_wr *) skb_push(skb, sizeof(*req));
562 req->wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_TX_DATA));
563 req->wr_lo = htonl(V_WR_TID(ep->hwtid));
564 req->len = htonl(mpalen);
565 req->param = htonl(V_TX_PORT(ep->l2t->smt_idx) |
566 V_TX_SNDBUF(snd_win>>15));
567 req->flags = htonl(F_TX_INIT);
568 req->sndseq = htonl(ep->snd_seq);
571 l2t_send(ep->com.tdev, skb, ep->l2t);
575 static int send_mpa_reply(struct iwch_ep *ep, const void *pdata, u8 plen)
578 struct tx_data_wr *req;
579 struct mpa_message *mpa;
583 PDBG("%s ep %p plen %d\n", __func__, ep, plen);
585 mpalen = sizeof(*mpa) + plen;
587 skb = get_skb(NULL, mpalen + sizeof(*req), GFP_KERNEL);
589 printk(KERN_ERR MOD "%s - cannot alloc skb!\n", __func__);
592 skb->priority = CPL_PRIORITY_DATA;
593 skb_reserve(skb, sizeof(*req));
594 mpa = (struct mpa_message *) skb_put(skb, mpalen);
595 memset(mpa, 0, sizeof(*mpa));
596 memcpy(mpa->key, MPA_KEY_REP, sizeof(mpa->key));
597 mpa->flags = (ep->mpa_attr.crc_enabled ? MPA_CRC : 0) |
598 (markers_enabled ? MPA_MARKERS : 0);
599 mpa->revision = mpa_rev;
600 mpa->private_data_size = htons(plen);
602 memcpy(mpa->private_data, pdata, plen);
605 * Reference the mpa skb. This ensures the data area
606 * will remain in memory until the hw acks the tx.
607 * Function tx_ack() will deref it.
610 set_arp_failure_handler(skb, arp_failure_discard);
611 skb_reset_transport_header(skb);
613 req = (struct tx_data_wr *) skb_push(skb, sizeof(*req));
614 req->wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_TX_DATA));
615 req->wr_lo = htonl(V_WR_TID(ep->hwtid));
616 req->len = htonl(len);
617 req->param = htonl(V_TX_PORT(ep->l2t->smt_idx) |
618 V_TX_SNDBUF(snd_win>>15));
619 req->flags = htonl(F_TX_INIT);
620 req->sndseq = htonl(ep->snd_seq);
622 state_set(&ep->com, MPA_REP_SENT);
623 l2t_send(ep->com.tdev, skb, ep->l2t);
627 static int act_establish(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
629 struct iwch_ep *ep = ctx;
630 struct cpl_act_establish *req = cplhdr(skb);
631 unsigned int tid = GET_TID(req);
633 PDBG("%s ep %p tid %d\n", __func__, ep, tid);
635 dst_confirm(ep->dst);
637 /* setup the hwtid for this connection */
639 cxgb3_insert_tid(ep->com.tdev, &t3c_client, ep, tid);
641 ep->snd_seq = ntohl(req->snd_isn);
642 ep->rcv_seq = ntohl(req->rcv_isn);
644 set_emss(ep, ntohs(req->tcp_opt));
646 /* dealloc the atid */
647 cxgb3_free_atid(ep->com.tdev, ep->atid);
649 /* start MPA negotiation */
650 send_mpa_req(ep, skb);
655 static void abort_connection(struct iwch_ep *ep, struct sk_buff *skb, gfp_t gfp)
657 PDBG("%s ep %p\n", __FILE__, ep);
658 state_set(&ep->com, ABORTING);
659 send_abort(ep, skb, gfp);
662 static void close_complete_upcall(struct iwch_ep *ep)
664 struct iw_cm_event event;
666 PDBG("%s ep %p\n", __func__, ep);
667 memset(&event, 0, sizeof(event));
668 event.event = IW_CM_EVENT_CLOSE;
670 PDBG("close complete delivered ep %p cm_id %p tid %d\n",
671 ep, ep->com.cm_id, ep->hwtid);
672 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
673 ep->com.cm_id->rem_ref(ep->com.cm_id);
674 ep->com.cm_id = NULL;
679 static void peer_close_upcall(struct iwch_ep *ep)
681 struct iw_cm_event event;
683 PDBG("%s ep %p\n", __func__, ep);
684 memset(&event, 0, sizeof(event));
685 event.event = IW_CM_EVENT_DISCONNECT;
687 PDBG("peer close delivered ep %p cm_id %p tid %d\n",
688 ep, ep->com.cm_id, ep->hwtid);
689 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
693 static void peer_abort_upcall(struct iwch_ep *ep)
695 struct iw_cm_event event;
697 PDBG("%s ep %p\n", __func__, ep);
698 memset(&event, 0, sizeof(event));
699 event.event = IW_CM_EVENT_CLOSE;
700 event.status = -ECONNRESET;
702 PDBG("abort delivered ep %p cm_id %p tid %d\n", ep,
703 ep->com.cm_id, ep->hwtid);
704 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
705 ep->com.cm_id->rem_ref(ep->com.cm_id);
706 ep->com.cm_id = NULL;
711 static void connect_reply_upcall(struct iwch_ep *ep, int status)
713 struct iw_cm_event event;
715 PDBG("%s ep %p status %d\n", __func__, ep, status);
716 memset(&event, 0, sizeof(event));
717 event.event = IW_CM_EVENT_CONNECT_REPLY;
718 event.status = status;
719 event.local_addr = ep->com.local_addr;
720 event.remote_addr = ep->com.remote_addr;
722 if ((status == 0) || (status == -ECONNREFUSED)) {
723 event.private_data_len = ep->plen;
724 event.private_data = ep->mpa_pkt + sizeof(struct mpa_message);
727 PDBG("%s ep %p tid %d status %d\n", __func__, ep,
729 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
732 ep->com.cm_id->rem_ref(ep->com.cm_id);
733 ep->com.cm_id = NULL;
738 static void connect_request_upcall(struct iwch_ep *ep)
740 struct iw_cm_event event;
742 PDBG("%s ep %p tid %d\n", __func__, ep, ep->hwtid);
743 memset(&event, 0, sizeof(event));
744 event.event = IW_CM_EVENT_CONNECT_REQUEST;
745 event.local_addr = ep->com.local_addr;
746 event.remote_addr = ep->com.remote_addr;
747 event.private_data_len = ep->plen;
748 event.private_data = ep->mpa_pkt + sizeof(struct mpa_message);
749 event.provider_data = ep;
750 if (state_read(&ep->parent_ep->com) != DEAD)
751 ep->parent_ep->com.cm_id->event_handler(
752 ep->parent_ep->com.cm_id,
754 put_ep(&ep->parent_ep->com);
755 ep->parent_ep = NULL;
758 static void established_upcall(struct iwch_ep *ep)
760 struct iw_cm_event event;
762 PDBG("%s ep %p\n", __func__, ep);
763 memset(&event, 0, sizeof(event));
764 event.event = IW_CM_EVENT_ESTABLISHED;
766 PDBG("%s ep %p tid %d\n", __func__, ep, ep->hwtid);
767 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
771 static int update_rx_credits(struct iwch_ep *ep, u32 credits)
773 struct cpl_rx_data_ack *req;
776 PDBG("%s ep %p credits %u\n", __func__, ep, credits);
777 skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
779 printk(KERN_ERR MOD "update_rx_credits - cannot alloc skb!\n");
783 req = (struct cpl_rx_data_ack *) skb_put(skb, sizeof(*req));
784 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
785 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_RX_DATA_ACK, ep->hwtid));
786 req->credit_dack = htonl(V_RX_CREDITS(credits) | V_RX_FORCE_ACK(1));
787 skb->priority = CPL_PRIORITY_ACK;
788 cxgb3_ofld_send(ep->com.tdev, skb);
792 static void process_mpa_reply(struct iwch_ep *ep, struct sk_buff *skb)
794 struct mpa_message *mpa;
796 struct iwch_qp_attributes attrs;
797 enum iwch_qp_attr_mask mask;
800 PDBG("%s ep %p\n", __func__, ep);
803 * Stop mpa timer. If it expired, then the state has
804 * changed and we bail since ep_timeout already aborted
808 if (state_read(&ep->com) != MPA_REQ_SENT)
812 * If we get more than the supported amount of private data
813 * then we must fail this connection.
815 if (ep->mpa_pkt_len + skb->len > sizeof(ep->mpa_pkt)) {
821 * copy the new data into our accumulation buffer.
823 skb_copy_from_linear_data(skb, &(ep->mpa_pkt[ep->mpa_pkt_len]),
825 ep->mpa_pkt_len += skb->len;
828 * if we don't even have the mpa message, then bail.
830 if (ep->mpa_pkt_len < sizeof(*mpa))
832 mpa = (struct mpa_message *) ep->mpa_pkt;
834 /* Validate MPA header. */
835 if (mpa->revision != mpa_rev) {
839 if (memcmp(mpa->key, MPA_KEY_REP, sizeof(mpa->key))) {
844 plen = ntohs(mpa->private_data_size);
847 * Fail if there's too much private data.
849 if (plen > MPA_MAX_PRIVATE_DATA) {
855 * If plen does not account for pkt size
857 if (ep->mpa_pkt_len > (sizeof(*mpa) + plen)) {
862 ep->plen = (u8) plen;
865 * If we don't have all the pdata yet, then bail.
866 * We'll continue process when more data arrives.
868 if (ep->mpa_pkt_len < (sizeof(*mpa) + plen))
871 if (mpa->flags & MPA_REJECT) {
877 * If we get here we have accumulated the entire mpa
878 * start reply message including private data. And
879 * the MPA header is valid.
881 state_set(&ep->com, FPDU_MODE);
882 ep->mpa_attr.crc_enabled = (mpa->flags & MPA_CRC) | crc_enabled ? 1 : 0;
883 ep->mpa_attr.recv_marker_enabled = markers_enabled;
884 ep->mpa_attr.xmit_marker_enabled = mpa->flags & MPA_MARKERS ? 1 : 0;
885 ep->mpa_attr.version = mpa_rev;
886 PDBG("%s - crc_enabled=%d, recv_marker_enabled=%d, "
887 "xmit_marker_enabled=%d, version=%d\n", __func__,
888 ep->mpa_attr.crc_enabled, ep->mpa_attr.recv_marker_enabled,
889 ep->mpa_attr.xmit_marker_enabled, ep->mpa_attr.version);
891 attrs.mpa_attr = ep->mpa_attr;
892 attrs.max_ird = ep->ird;
893 attrs.max_ord = ep->ord;
894 attrs.llp_stream_handle = ep;
895 attrs.next_state = IWCH_QP_STATE_RTS;
897 mask = IWCH_QP_ATTR_NEXT_STATE |
898 IWCH_QP_ATTR_LLP_STREAM_HANDLE | IWCH_QP_ATTR_MPA_ATTR |
899 IWCH_QP_ATTR_MAX_IRD | IWCH_QP_ATTR_MAX_ORD;
901 /* bind QP and TID with INIT_WR */
902 err = iwch_modify_qp(ep->com.qp->rhp,
903 ep->com.qp, mask, &attrs, 1);
907 abort_connection(ep, skb, GFP_KERNEL);
909 connect_reply_upcall(ep, err);
913 static void process_mpa_request(struct iwch_ep *ep, struct sk_buff *skb)
915 struct mpa_message *mpa;
918 PDBG("%s ep %p\n", __func__, ep);
921 * Stop mpa timer. If it expired, then the state has
922 * changed and we bail since ep_timeout already aborted
926 if (state_read(&ep->com) != MPA_REQ_WAIT)
930 * If we get more than the supported amount of private data
931 * then we must fail this connection.
933 if (ep->mpa_pkt_len + skb->len > sizeof(ep->mpa_pkt)) {
934 abort_connection(ep, skb, GFP_KERNEL);
938 PDBG("%s enter (%s line %u)\n", __func__, __FILE__, __LINE__);
941 * Copy the new data into our accumulation buffer.
943 skb_copy_from_linear_data(skb, &(ep->mpa_pkt[ep->mpa_pkt_len]),
945 ep->mpa_pkt_len += skb->len;
948 * If we don't even have the mpa message, then bail.
949 * We'll continue process when more data arrives.
951 if (ep->mpa_pkt_len < sizeof(*mpa))
953 PDBG("%s enter (%s line %u)\n", __func__, __FILE__, __LINE__);
954 mpa = (struct mpa_message *) ep->mpa_pkt;
957 * Validate MPA Header.
959 if (mpa->revision != mpa_rev) {
960 abort_connection(ep, skb, GFP_KERNEL);
964 if (memcmp(mpa->key, MPA_KEY_REQ, sizeof(mpa->key))) {
965 abort_connection(ep, skb, GFP_KERNEL);
969 plen = ntohs(mpa->private_data_size);
972 * Fail if there's too much private data.
974 if (plen > MPA_MAX_PRIVATE_DATA) {
975 abort_connection(ep, skb, GFP_KERNEL);
980 * If plen does not account for pkt size
982 if (ep->mpa_pkt_len > (sizeof(*mpa) + plen)) {
983 abort_connection(ep, skb, GFP_KERNEL);
986 ep->plen = (u8) plen;
989 * If we don't have all the pdata yet, then bail.
991 if (ep->mpa_pkt_len < (sizeof(*mpa) + plen))
995 * If we get here we have accumulated the entire mpa
996 * start reply message including private data.
998 ep->mpa_attr.crc_enabled = (mpa->flags & MPA_CRC) | crc_enabled ? 1 : 0;
999 ep->mpa_attr.recv_marker_enabled = markers_enabled;
1000 ep->mpa_attr.xmit_marker_enabled = mpa->flags & MPA_MARKERS ? 1 : 0;
1001 ep->mpa_attr.version = mpa_rev;
1002 PDBG("%s - crc_enabled=%d, recv_marker_enabled=%d, "
1003 "xmit_marker_enabled=%d, version=%d\n", __func__,
1004 ep->mpa_attr.crc_enabled, ep->mpa_attr.recv_marker_enabled,
1005 ep->mpa_attr.xmit_marker_enabled, ep->mpa_attr.version);
1007 state_set(&ep->com, MPA_REQ_RCVD);
1010 connect_request_upcall(ep);
1014 static int rx_data(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1016 struct iwch_ep *ep = ctx;
1017 struct cpl_rx_data *hdr = cplhdr(skb);
1018 unsigned int dlen = ntohs(hdr->len);
1020 PDBG("%s ep %p dlen %u\n", __func__, ep, dlen);
1022 skb_pull(skb, sizeof(*hdr));
1023 skb_trim(skb, dlen);
1025 ep->rcv_seq += dlen;
1026 BUG_ON(ep->rcv_seq != (ntohl(hdr->seq) + dlen));
1028 switch (state_read(&ep->com)) {
1030 process_mpa_reply(ep, skb);
1033 process_mpa_request(ep, skb);
1038 printk(KERN_ERR MOD "%s Unexpected streaming data."
1039 " ep %p state %d tid %d\n",
1040 __func__, ep, state_read(&ep->com), ep->hwtid);
1043 * The ep will timeout and inform the ULP of the failure.
1049 /* update RX credits */
1050 update_rx_credits(ep, dlen);
1052 return CPL_RET_BUF_DONE;
1056 * Upcall from the adapter indicating data has been transmitted.
1057 * For us its just the single MPA request or reply. We can now free
1058 * the skb holding the mpa message.
1060 static int tx_ack(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1062 struct iwch_ep *ep = ctx;
1063 struct cpl_wr_ack *hdr = cplhdr(skb);
1064 unsigned int credits = ntohs(hdr->credits);
1066 PDBG("%s ep %p credits %u\n", __func__, ep, credits);
1069 return CPL_RET_BUF_DONE;
1070 BUG_ON(credits != 1);
1071 BUG_ON(ep->mpa_skb == NULL);
1072 kfree_skb(ep->mpa_skb);
1074 dst_confirm(ep->dst);
1075 if (state_read(&ep->com) == MPA_REP_SENT) {
1076 ep->com.rpl_done = 1;
1077 PDBG("waking up ep %p\n", ep);
1078 wake_up(&ep->com.waitq);
1080 return CPL_RET_BUF_DONE;
1083 static int abort_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1085 struct iwch_ep *ep = ctx;
1087 PDBG("%s ep %p\n", __func__, ep);
1090 * We get 2 abort replies from the HW. The first one must
1091 * be ignored except for scribbling that we need one more.
1093 if (!(ep->flags & ABORT_REQ_IN_PROGRESS)) {
1094 ep->flags |= ABORT_REQ_IN_PROGRESS;
1095 return CPL_RET_BUF_DONE;
1098 close_complete_upcall(ep);
1099 state_set(&ep->com, DEAD);
1100 release_ep_resources(ep);
1101 return CPL_RET_BUF_DONE;
1105 * Return whether a failed active open has allocated a TID
1107 static inline int act_open_has_tid(int status)
1109 return status != CPL_ERR_TCAM_FULL && status != CPL_ERR_CONN_EXIST &&
1110 status != CPL_ERR_ARP_MISS;
1113 static int act_open_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1115 struct iwch_ep *ep = ctx;
1116 struct cpl_act_open_rpl *rpl = cplhdr(skb);
1118 PDBG("%s ep %p status %u errno %d\n", __func__, ep, rpl->status,
1119 status2errno(rpl->status));
1120 connect_reply_upcall(ep, status2errno(rpl->status));
1121 state_set(&ep->com, DEAD);
1122 if (ep->com.tdev->type != T3A && act_open_has_tid(rpl->status))
1123 release_tid(ep->com.tdev, GET_TID(rpl), NULL);
1124 cxgb3_free_atid(ep->com.tdev, ep->atid);
1125 dst_release(ep->dst);
1126 l2t_release(L2DATA(ep->com.tdev), ep->l2t);
1128 return CPL_RET_BUF_DONE;
1131 static int listen_start(struct iwch_listen_ep *ep)
1133 struct sk_buff *skb;
1134 struct cpl_pass_open_req *req;
1136 PDBG("%s ep %p\n", __func__, ep);
1137 skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
1139 printk(KERN_ERR MOD "t3c_listen_start failed to alloc skb!\n");
1143 req = (struct cpl_pass_open_req *) skb_put(skb, sizeof(*req));
1144 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
1145 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ, ep->stid));
1146 req->local_port = ep->com.local_addr.sin_port;
1147 req->local_ip = ep->com.local_addr.sin_addr.s_addr;
1150 req->peer_netmask = 0;
1151 req->opt0h = htonl(F_DELACK | F_TCAM_BYPASS);
1152 req->opt0l = htonl(V_RCV_BUFSIZ(rcv_win>>10));
1153 req->opt1 = htonl(V_CONN_POLICY(CPL_CONN_POLICY_ASK));
1156 cxgb3_ofld_send(ep->com.tdev, skb);
1160 static int pass_open_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1162 struct iwch_listen_ep *ep = ctx;
1163 struct cpl_pass_open_rpl *rpl = cplhdr(skb);
1165 PDBG("%s ep %p status %d error %d\n", __func__, ep,
1166 rpl->status, status2errno(rpl->status));
1167 ep->com.rpl_err = status2errno(rpl->status);
1168 ep->com.rpl_done = 1;
1169 wake_up(&ep->com.waitq);
1171 return CPL_RET_BUF_DONE;
1174 static int listen_stop(struct iwch_listen_ep *ep)
1176 struct sk_buff *skb;
1177 struct cpl_close_listserv_req *req;
1179 PDBG("%s ep %p\n", __func__, ep);
1180 skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
1182 printk(KERN_ERR MOD "%s - failed to alloc skb\n", __func__);
1185 req = (struct cpl_close_listserv_req *) skb_put(skb, sizeof(*req));
1186 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
1188 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_LISTSRV_REQ, ep->stid));
1190 cxgb3_ofld_send(ep->com.tdev, skb);
1194 static int close_listsrv_rpl(struct t3cdev *tdev, struct sk_buff *skb,
1197 struct iwch_listen_ep *ep = ctx;
1198 struct cpl_close_listserv_rpl *rpl = cplhdr(skb);
1200 PDBG("%s ep %p\n", __func__, ep);
1201 ep->com.rpl_err = status2errno(rpl->status);
1202 ep->com.rpl_done = 1;
1203 wake_up(&ep->com.waitq);
1204 return CPL_RET_BUF_DONE;
1207 static void accept_cr(struct iwch_ep *ep, __be32 peer_ip, struct sk_buff *skb)
1209 struct cpl_pass_accept_rpl *rpl;
1210 unsigned int mtu_idx;
1211 u32 opt0h, opt0l, opt2;
1214 PDBG("%s ep %p\n", __func__, ep);
1215 BUG_ON(skb_cloned(skb));
1216 skb_trim(skb, sizeof(*rpl));
1218 mtu_idx = find_best_mtu(T3C_DATA(ep->com.tdev), dst_mtu(ep->dst));
1219 wscale = compute_wscale(rcv_win);
1220 opt0h = V_NAGLE(0) |
1224 V_WND_SCALE(wscale) |
1225 V_MSS_IDX(mtu_idx) |
1226 V_L2T_IDX(ep->l2t->idx) | V_TX_CHANNEL(ep->l2t->smt_idx);
1227 opt0l = V_TOS((ep->tos >> 2) & M_TOS) | V_RCV_BUFSIZ(rcv_win>>10);
1228 opt2 = V_FLAVORS_VALID(1) | V_CONG_CONTROL_FLAVOR(cong_flavor);
1231 rpl->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
1232 OPCODE_TID(rpl) = htonl(MK_OPCODE_TID(CPL_PASS_ACCEPT_RPL, ep->hwtid));
1233 rpl->peer_ip = peer_ip;
1234 rpl->opt0h = htonl(opt0h);
1235 rpl->opt0l_status = htonl(opt0l | CPL_PASS_OPEN_ACCEPT);
1236 rpl->opt2 = htonl(opt2);
1237 rpl->rsvd = rpl->opt2; /* workaround for HW bug */
1238 skb->priority = CPL_PRIORITY_SETUP;
1239 l2t_send(ep->com.tdev, skb, ep->l2t);
1244 static void reject_cr(struct t3cdev *tdev, u32 hwtid, __be32 peer_ip,
1245 struct sk_buff *skb)
1247 PDBG("%s t3cdev %p tid %u peer_ip %x\n", __func__, tdev, hwtid,
1249 BUG_ON(skb_cloned(skb));
1250 skb_trim(skb, sizeof(struct cpl_tid_release));
1253 if (tdev->type != T3A)
1254 release_tid(tdev, hwtid, skb);
1256 struct cpl_pass_accept_rpl *rpl;
1259 skb->priority = CPL_PRIORITY_SETUP;
1260 rpl->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
1261 OPCODE_TID(rpl) = htonl(MK_OPCODE_TID(CPL_PASS_ACCEPT_RPL,
1263 rpl->peer_ip = peer_ip;
1264 rpl->opt0h = htonl(F_TCAM_BYPASS);
1265 rpl->opt0l_status = htonl(CPL_PASS_OPEN_REJECT);
1267 rpl->rsvd = rpl->opt2;
1268 cxgb3_ofld_send(tdev, skb);
1272 static int pass_accept_req(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1274 struct iwch_ep *child_ep, *parent_ep = ctx;
1275 struct cpl_pass_accept_req *req = cplhdr(skb);
1276 unsigned int hwtid = GET_TID(req);
1277 struct dst_entry *dst;
1278 struct l2t_entry *l2t;
1282 PDBG("%s parent ep %p tid %u\n", __func__, parent_ep, hwtid);
1284 if (state_read(&parent_ep->com) != LISTEN) {
1285 printk(KERN_ERR "%s - listening ep not in LISTEN\n",
1291 * Find the netdev for this connection request.
1293 tim.mac_addr = req->dst_mac;
1294 tim.vlan_tag = ntohs(req->vlan_tag);
1295 if (tdev->ctl(tdev, GET_IFF_FROM_MAC, &tim) < 0 || !tim.dev) {
1297 "%s bad dst mac %02x %02x %02x %02x %02x %02x\n",
1308 /* Find output route */
1309 rt = find_route(tdev,
1313 req->peer_port, G_PASS_OPEN_TOS(ntohl(req->tos_tid)));
1315 printk(KERN_ERR MOD "%s - failed to find dst entry!\n",
1320 l2t = t3_l2t_get(tdev, dst->neighbour, dst->neighbour->dev);
1322 printk(KERN_ERR MOD "%s - failed to allocate l2t entry!\n",
1327 child_ep = alloc_ep(sizeof(*child_ep), GFP_KERNEL);
1329 printk(KERN_ERR MOD "%s - failed to allocate ep entry!\n",
1331 l2t_release(L2DATA(tdev), l2t);
1335 state_set(&child_ep->com, CONNECTING);
1336 child_ep->com.tdev = tdev;
1337 child_ep->com.cm_id = NULL;
1338 child_ep->com.local_addr.sin_family = PF_INET;
1339 child_ep->com.local_addr.sin_port = req->local_port;
1340 child_ep->com.local_addr.sin_addr.s_addr = req->local_ip;
1341 child_ep->com.remote_addr.sin_family = PF_INET;
1342 child_ep->com.remote_addr.sin_port = req->peer_port;
1343 child_ep->com.remote_addr.sin_addr.s_addr = req->peer_ip;
1344 get_ep(&parent_ep->com);
1345 child_ep->parent_ep = parent_ep;
1346 child_ep->tos = G_PASS_OPEN_TOS(ntohl(req->tos_tid));
1347 child_ep->l2t = l2t;
1348 child_ep->dst = dst;
1349 child_ep->hwtid = hwtid;
1350 init_timer(&child_ep->timer);
1351 cxgb3_insert_tid(tdev, &t3c_client, child_ep, hwtid);
1352 accept_cr(child_ep, req->peer_ip, skb);
1355 reject_cr(tdev, hwtid, req->peer_ip, skb);
1357 return CPL_RET_BUF_DONE;
1360 static int pass_establish(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1362 struct iwch_ep *ep = ctx;
1363 struct cpl_pass_establish *req = cplhdr(skb);
1365 PDBG("%s ep %p\n", __func__, ep);
1366 ep->snd_seq = ntohl(req->snd_isn);
1367 ep->rcv_seq = ntohl(req->rcv_isn);
1369 set_emss(ep, ntohs(req->tcp_opt));
1371 dst_confirm(ep->dst);
1372 state_set(&ep->com, MPA_REQ_WAIT);
1375 return CPL_RET_BUF_DONE;
1378 static int peer_close(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1380 struct iwch_ep *ep = ctx;
1381 struct iwch_qp_attributes attrs;
1382 unsigned long flags;
1386 PDBG("%s ep %p\n", __func__, ep);
1387 dst_confirm(ep->dst);
1389 spin_lock_irqsave(&ep->com.lock, flags);
1390 switch (ep->com.state) {
1392 __state_set(&ep->com, CLOSING);
1395 __state_set(&ep->com, CLOSING);
1396 connect_reply_upcall(ep, -ECONNRESET);
1401 * We're gonna mark this puppy DEAD, but keep
1402 * the reference on it until the ULP accepts or
1405 __state_set(&ep->com, CLOSING);
1409 __state_set(&ep->com, CLOSING);
1410 ep->com.rpl_done = 1;
1411 ep->com.rpl_err = -ECONNRESET;
1412 PDBG("waking up ep %p\n", ep);
1413 wake_up(&ep->com.waitq);
1417 __state_set(&ep->com, CLOSING);
1418 attrs.next_state = IWCH_QP_STATE_CLOSING;
1419 iwch_modify_qp(ep->com.qp->rhp, ep->com.qp,
1420 IWCH_QP_ATTR_NEXT_STATE, &attrs, 1);
1421 peer_close_upcall(ep);
1427 __state_set(&ep->com, MORIBUND);
1432 if (ep->com.cm_id && ep->com.qp) {
1433 attrs.next_state = IWCH_QP_STATE_IDLE;
1434 iwch_modify_qp(ep->com.qp->rhp, ep->com.qp,
1435 IWCH_QP_ATTR_NEXT_STATE, &attrs, 1);
1437 close_complete_upcall(ep);
1438 __state_set(&ep->com, DEAD);
1448 spin_unlock_irqrestore(&ep->com.lock, flags);
1450 iwch_ep_disconnect(ep, 0, GFP_KERNEL);
1452 release_ep_resources(ep);
1453 return CPL_RET_BUF_DONE;
1457 * Returns whether an ABORT_REQ_RSS message is a negative advice.
1459 static int is_neg_adv_abort(unsigned int status)
1461 return status == CPL_ERR_RTX_NEG_ADVICE ||
1462 status == CPL_ERR_PERSIST_NEG_ADVICE;
1465 static int peer_abort(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1467 struct cpl_abort_req_rss *req = cplhdr(skb);
1468 struct iwch_ep *ep = ctx;
1469 struct cpl_abort_rpl *rpl;
1470 struct sk_buff *rpl_skb;
1471 struct iwch_qp_attributes attrs;
1475 if (is_neg_adv_abort(req->status)) {
1476 PDBG("%s neg_adv_abort ep %p tid %d\n", __func__, ep,
1478 t3_l2t_send_event(ep->com.tdev, ep->l2t);
1479 return CPL_RET_BUF_DONE;
1483 * We get 2 peer aborts from the HW. The first one must
1484 * be ignored except for scribbling that we need one more.
1486 if (!(ep->flags & PEER_ABORT_IN_PROGRESS)) {
1487 ep->flags |= PEER_ABORT_IN_PROGRESS;
1488 return CPL_RET_BUF_DONE;
1491 state = state_read(&ep->com);
1492 PDBG("%s ep %p state %u\n", __func__, ep, state);
1501 connect_reply_upcall(ep, -ECONNRESET);
1504 ep->com.rpl_done = 1;
1505 ep->com.rpl_err = -ECONNRESET;
1506 PDBG("waking up ep %p\n", ep);
1507 wake_up(&ep->com.waitq);
1512 * We're gonna mark this puppy DEAD, but keep
1513 * the reference on it until the ULP accepts or
1523 if (ep->com.cm_id && ep->com.qp) {
1524 attrs.next_state = IWCH_QP_STATE_ERROR;
1525 ret = iwch_modify_qp(ep->com.qp->rhp,
1526 ep->com.qp, IWCH_QP_ATTR_NEXT_STATE,
1530 "%s - qp <- error failed!\n",
1533 peer_abort_upcall(ep);
1538 PDBG("%s PEER_ABORT IN DEAD STATE!!!!\n", __func__);
1539 return CPL_RET_BUF_DONE;
1544 dst_confirm(ep->dst);
1546 rpl_skb = get_skb(skb, sizeof(*rpl), GFP_KERNEL);
1548 printk(KERN_ERR MOD "%s - cannot allocate skb!\n",
1550 dst_release(ep->dst);
1551 l2t_release(L2DATA(ep->com.tdev), ep->l2t);
1553 return CPL_RET_BUF_DONE;
1555 rpl_skb->priority = CPL_PRIORITY_DATA;
1556 rpl = (struct cpl_abort_rpl *) skb_put(rpl_skb, sizeof(*rpl));
1557 rpl->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_HOST_ABORT_CON_RPL));
1558 rpl->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
1559 OPCODE_TID(rpl) = htonl(MK_OPCODE_TID(CPL_ABORT_RPL, ep->hwtid));
1560 rpl->cmd = CPL_ABORT_NO_RST;
1561 cxgb3_ofld_send(ep->com.tdev, rpl_skb);
1562 if (state != ABORTING) {
1563 state_set(&ep->com, DEAD);
1564 release_ep_resources(ep);
1566 return CPL_RET_BUF_DONE;
1569 static int close_con_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1571 struct iwch_ep *ep = ctx;
1572 struct iwch_qp_attributes attrs;
1573 unsigned long flags;
1576 PDBG("%s ep %p\n", __func__, ep);
1579 /* The cm_id may be null if we failed to connect */
1580 spin_lock_irqsave(&ep->com.lock, flags);
1581 switch (ep->com.state) {
1583 __state_set(&ep->com, MORIBUND);
1587 if ((ep->com.cm_id) && (ep->com.qp)) {
1588 attrs.next_state = IWCH_QP_STATE_IDLE;
1589 iwch_modify_qp(ep->com.qp->rhp,
1591 IWCH_QP_ATTR_NEXT_STATE,
1594 close_complete_upcall(ep);
1595 __state_set(&ep->com, DEAD);
1605 spin_unlock_irqrestore(&ep->com.lock, flags);
1607 release_ep_resources(ep);
1608 return CPL_RET_BUF_DONE;
1612 * T3A does 3 things when a TERM is received:
1613 * 1) send up a CPL_RDMA_TERMINATE message with the TERM packet
1614 * 2) generate an async event on the QP with the TERMINATE opcode
1615 * 3) post a TERMINATE opcde cqe into the associated CQ.
1617 * For (1), we save the message in the qp for later consumer consumption.
1618 * For (2), we move the QP into TERMINATE, post a QP event and disconnect.
1619 * For (3), we toss the CQE in cxio_poll_cq().
1621 * terminate() handles case (1)...
1623 static int terminate(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1625 struct iwch_ep *ep = ctx;
1627 PDBG("%s ep %p\n", __func__, ep);
1628 skb_pull(skb, sizeof(struct cpl_rdma_terminate));
1629 PDBG("%s saving %d bytes of term msg\n", __func__, skb->len);
1630 skb_copy_from_linear_data(skb, ep->com.qp->attr.terminate_buffer,
1632 ep->com.qp->attr.terminate_msg_len = skb->len;
1633 ep->com.qp->attr.is_terminate_local = 0;
1634 return CPL_RET_BUF_DONE;
1637 static int ec_status(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1639 struct cpl_rdma_ec_status *rep = cplhdr(skb);
1640 struct iwch_ep *ep = ctx;
1642 PDBG("%s ep %p tid %u status %d\n", __func__, ep, ep->hwtid,
1645 struct iwch_qp_attributes attrs;
1647 printk(KERN_ERR MOD "%s BAD CLOSE - Aborting tid %u\n",
1648 __func__, ep->hwtid);
1650 attrs.next_state = IWCH_QP_STATE_ERROR;
1651 iwch_modify_qp(ep->com.qp->rhp,
1652 ep->com.qp, IWCH_QP_ATTR_NEXT_STATE,
1654 abort_connection(ep, NULL, GFP_KERNEL);
1656 return CPL_RET_BUF_DONE;
1659 static void ep_timeout(unsigned long arg)
1661 struct iwch_ep *ep = (struct iwch_ep *)arg;
1662 struct iwch_qp_attributes attrs;
1663 unsigned long flags;
1665 spin_lock_irqsave(&ep->com.lock, flags);
1666 PDBG("%s ep %p tid %u state %d\n", __func__, ep, ep->hwtid,
1668 switch (ep->com.state) {
1670 connect_reply_upcall(ep, -ETIMEDOUT);
1676 if (ep->com.cm_id && ep->com.qp) {
1677 attrs.next_state = IWCH_QP_STATE_ERROR;
1678 iwch_modify_qp(ep->com.qp->rhp,
1679 ep->com.qp, IWCH_QP_ATTR_NEXT_STATE,
1686 __state_set(&ep->com, CLOSING);
1687 spin_unlock_irqrestore(&ep->com.lock, flags);
1688 abort_connection(ep, NULL, GFP_ATOMIC);
1692 int iwch_reject_cr(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
1695 struct iwch_ep *ep = to_ep(cm_id);
1696 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1698 if (state_read(&ep->com) == DEAD) {
1702 BUG_ON(state_read(&ep->com) != MPA_REQ_RCVD);
1704 abort_connection(ep, NULL, GFP_KERNEL);
1706 err = send_mpa_reject(ep, pdata, pdata_len);
1707 err = iwch_ep_disconnect(ep, 0, GFP_KERNEL);
1712 int iwch_accept_cr(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
1715 struct iwch_qp_attributes attrs;
1716 enum iwch_qp_attr_mask mask;
1717 struct iwch_ep *ep = to_ep(cm_id);
1718 struct iwch_dev *h = to_iwch_dev(cm_id->device);
1719 struct iwch_qp *qp = get_qhp(h, conn_param->qpn);
1721 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1722 if (state_read(&ep->com) == DEAD)
1725 BUG_ON(state_read(&ep->com) != MPA_REQ_RCVD);
1728 if ((conn_param->ord > qp->rhp->attr.max_rdma_read_qp_depth) ||
1729 (conn_param->ird > qp->rhp->attr.max_rdma_reads_per_qp)) {
1730 abort_connection(ep, NULL, GFP_KERNEL);
1734 cm_id->add_ref(cm_id);
1735 ep->com.cm_id = cm_id;
1738 ep->com.rpl_done = 0;
1739 ep->com.rpl_err = 0;
1740 ep->ird = conn_param->ird;
1741 ep->ord = conn_param->ord;
1742 PDBG("%s %d ird %d ord %d\n", __func__, __LINE__, ep->ird, ep->ord);
1746 /* bind QP to EP and move to RTS */
1747 attrs.mpa_attr = ep->mpa_attr;
1748 attrs.max_ird = ep->ird;
1749 attrs.max_ord = ep->ord;
1750 attrs.llp_stream_handle = ep;
1751 attrs.next_state = IWCH_QP_STATE_RTS;
1753 /* bind QP and TID with INIT_WR */
1754 mask = IWCH_QP_ATTR_NEXT_STATE |
1755 IWCH_QP_ATTR_LLP_STREAM_HANDLE |
1756 IWCH_QP_ATTR_MPA_ATTR |
1757 IWCH_QP_ATTR_MAX_IRD |
1758 IWCH_QP_ATTR_MAX_ORD;
1760 err = iwch_modify_qp(ep->com.qp->rhp,
1761 ep->com.qp, mask, &attrs, 1);
1765 err = send_mpa_reply(ep, conn_param->private_data,
1766 conn_param->private_data_len);
1770 /* wait for wr_ack */
1771 wait_event(ep->com.waitq, ep->com.rpl_done);
1772 err = ep->com.rpl_err;
1776 state_set(&ep->com, FPDU_MODE);
1777 established_upcall(ep);
1781 ep->com.cm_id = NULL;
1783 cm_id->rem_ref(cm_id);
1788 static int is_loopback_dst(struct iw_cm_id *cm_id)
1790 struct net_device *dev;
1792 dev = ip_dev_find(&init_net, cm_id->remote_addr.sin_addr.s_addr);
1799 int iwch_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
1802 struct iwch_dev *h = to_iwch_dev(cm_id->device);
1806 if (is_loopback_dst(cm_id)) {
1811 ep = alloc_ep(sizeof(*ep), GFP_KERNEL);
1813 printk(KERN_ERR MOD "%s - cannot alloc ep.\n", __func__);
1817 init_timer(&ep->timer);
1818 ep->plen = conn_param->private_data_len;
1820 memcpy(ep->mpa_pkt + sizeof(struct mpa_message),
1821 conn_param->private_data, ep->plen);
1822 ep->ird = conn_param->ird;
1823 ep->ord = conn_param->ord;
1824 ep->com.tdev = h->rdev.t3cdev_p;
1826 cm_id->add_ref(cm_id);
1827 ep->com.cm_id = cm_id;
1828 ep->com.qp = get_qhp(h, conn_param->qpn);
1829 BUG_ON(!ep->com.qp);
1830 PDBG("%s qpn 0x%x qp %p cm_id %p\n", __func__, conn_param->qpn,
1834 * Allocate an active TID to initiate a TCP connection.
1836 ep->atid = cxgb3_alloc_atid(h->rdev.t3cdev_p, &t3c_client, ep);
1837 if (ep->atid == -1) {
1838 printk(KERN_ERR MOD "%s - cannot alloc atid.\n", __func__);
1844 rt = find_route(h->rdev.t3cdev_p,
1845 cm_id->local_addr.sin_addr.s_addr,
1846 cm_id->remote_addr.sin_addr.s_addr,
1847 cm_id->local_addr.sin_port,
1848 cm_id->remote_addr.sin_port, IPTOS_LOWDELAY);
1850 printk(KERN_ERR MOD "%s - cannot find route.\n", __func__);
1851 err = -EHOSTUNREACH;
1854 ep->dst = &rt->u.dst;
1856 /* get a l2t entry */
1857 ep->l2t = t3_l2t_get(ep->com.tdev, ep->dst->neighbour,
1858 ep->dst->neighbour->dev);
1860 printk(KERN_ERR MOD "%s - cannot alloc l2e.\n", __func__);
1865 state_set(&ep->com, CONNECTING);
1866 ep->tos = IPTOS_LOWDELAY;
1867 ep->com.local_addr = cm_id->local_addr;
1868 ep->com.remote_addr = cm_id->remote_addr;
1870 /* send connect request to rnic */
1871 err = send_connect(ep);
1875 l2t_release(L2DATA(h->rdev.t3cdev_p), ep->l2t);
1877 dst_release(ep->dst);
1879 cxgb3_free_atid(ep->com.tdev, ep->atid);
1886 int iwch_create_listen(struct iw_cm_id *cm_id, int backlog)
1889 struct iwch_dev *h = to_iwch_dev(cm_id->device);
1890 struct iwch_listen_ep *ep;
1895 ep = alloc_ep(sizeof(*ep), GFP_KERNEL);
1897 printk(KERN_ERR MOD "%s - cannot alloc ep.\n", __func__);
1901 PDBG("%s ep %p\n", __func__, ep);
1902 ep->com.tdev = h->rdev.t3cdev_p;
1903 cm_id->add_ref(cm_id);
1904 ep->com.cm_id = cm_id;
1905 ep->backlog = backlog;
1906 ep->com.local_addr = cm_id->local_addr;
1909 * Allocate a server TID.
1911 ep->stid = cxgb3_alloc_stid(h->rdev.t3cdev_p, &t3c_client, ep);
1912 if (ep->stid == -1) {
1913 printk(KERN_ERR MOD "%s - cannot alloc atid.\n", __func__);
1918 state_set(&ep->com, LISTEN);
1919 err = listen_start(ep);
1923 /* wait for pass_open_rpl */
1924 wait_event(ep->com.waitq, ep->com.rpl_done);
1925 err = ep->com.rpl_err;
1927 cm_id->provider_data = ep;
1931 cxgb3_free_stid(ep->com.tdev, ep->stid);
1933 cm_id->rem_ref(cm_id);
1940 int iwch_destroy_listen(struct iw_cm_id *cm_id)
1943 struct iwch_listen_ep *ep = to_listen_ep(cm_id);
1945 PDBG("%s ep %p\n", __func__, ep);
1948 state_set(&ep->com, DEAD);
1949 ep->com.rpl_done = 0;
1950 ep->com.rpl_err = 0;
1951 err = listen_stop(ep);
1952 wait_event(ep->com.waitq, ep->com.rpl_done);
1953 cxgb3_free_stid(ep->com.tdev, ep->stid);
1954 err = ep->com.rpl_err;
1955 cm_id->rem_ref(cm_id);
1960 int iwch_ep_disconnect(struct iwch_ep *ep, int abrupt, gfp_t gfp)
1963 unsigned long flags;
1966 spin_lock_irqsave(&ep->com.lock, flags);
1968 PDBG("%s ep %p state %s, abrupt %d\n", __func__, ep,
1969 states[ep->com.state], abrupt);
1971 if (ep->com.state == DEAD) {
1972 PDBG("%s already dead ep %p\n", __func__, ep);
1977 if (ep->com.state != ABORTING) {
1978 ep->com.state = ABORTING;
1984 switch (ep->com.state) {
1991 ep->com.state = CLOSING;
1995 ep->com.state = MORIBUND;
2005 spin_unlock_irqrestore(&ep->com.lock, flags);
2008 ret = send_abort(ep, NULL, gfp);
2010 ret = send_halfclose(ep, gfp);
2015 int iwch_ep_redirect(void *ctx, struct dst_entry *old, struct dst_entry *new,
2016 struct l2t_entry *l2t)
2018 struct iwch_ep *ep = ctx;
2023 PDBG("%s ep %p redirect to dst %p l2t %p\n", __func__, ep, new,
2026 l2t_release(L2DATA(ep->com.tdev), ep->l2t);
2034 * All the CM events are handled on a work queue to have a safe context.
2036 static int sched(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
2038 struct iwch_ep_common *epc = ctx;
2043 * Save ctx and tdev in the skb->cb area.
2045 *((void **) skb->cb) = ctx;
2046 *((struct t3cdev **) (skb->cb + sizeof(void *))) = tdev;
2049 * Queue the skb and schedule the worker thread.
2051 skb_queue_tail(&rxq, skb);
2052 queue_work(workq, &skb_work);
2056 static int set_tcb_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
2058 struct cpl_set_tcb_rpl *rpl = cplhdr(skb);
2060 if (rpl->status != CPL_ERR_NONE) {
2061 printk(KERN_ERR MOD "Unexpected SET_TCB_RPL status %u "
2062 "for tid %u\n", rpl->status, GET_TID(rpl));
2064 return CPL_RET_BUF_DONE;
2067 int __init iwch_cm_init(void)
2069 skb_queue_head_init(&rxq);
2071 workq = create_singlethread_workqueue("iw_cxgb3");
2076 * All upcalls from the T3 Core go to sched() to
2077 * schedule the processing on a work queue.
2079 t3c_handlers[CPL_ACT_ESTABLISH] = sched;
2080 t3c_handlers[CPL_ACT_OPEN_RPL] = sched;
2081 t3c_handlers[CPL_RX_DATA] = sched;
2082 t3c_handlers[CPL_TX_DMA_ACK] = sched;
2083 t3c_handlers[CPL_ABORT_RPL_RSS] = sched;
2084 t3c_handlers[CPL_ABORT_RPL] = sched;
2085 t3c_handlers[CPL_PASS_OPEN_RPL] = sched;
2086 t3c_handlers[CPL_CLOSE_LISTSRV_RPL] = sched;
2087 t3c_handlers[CPL_PASS_ACCEPT_REQ] = sched;
2088 t3c_handlers[CPL_PASS_ESTABLISH] = sched;
2089 t3c_handlers[CPL_PEER_CLOSE] = sched;
2090 t3c_handlers[CPL_CLOSE_CON_RPL] = sched;
2091 t3c_handlers[CPL_ABORT_REQ_RSS] = sched;
2092 t3c_handlers[CPL_RDMA_TERMINATE] = sched;
2093 t3c_handlers[CPL_RDMA_EC_STATUS] = sched;
2094 t3c_handlers[CPL_SET_TCB_RPL] = set_tcb_rpl;
2097 * These are the real handlers that are called from a
2100 work_handlers[CPL_ACT_ESTABLISH] = act_establish;
2101 work_handlers[CPL_ACT_OPEN_RPL] = act_open_rpl;
2102 work_handlers[CPL_RX_DATA] = rx_data;
2103 work_handlers[CPL_TX_DMA_ACK] = tx_ack;
2104 work_handlers[CPL_ABORT_RPL_RSS] = abort_rpl;
2105 work_handlers[CPL_ABORT_RPL] = abort_rpl;
2106 work_handlers[CPL_PASS_OPEN_RPL] = pass_open_rpl;
2107 work_handlers[CPL_CLOSE_LISTSRV_RPL] = close_listsrv_rpl;
2108 work_handlers[CPL_PASS_ACCEPT_REQ] = pass_accept_req;
2109 work_handlers[CPL_PASS_ESTABLISH] = pass_establish;
2110 work_handlers[CPL_PEER_CLOSE] = peer_close;
2111 work_handlers[CPL_ABORT_REQ_RSS] = peer_abort;
2112 work_handlers[CPL_CLOSE_CON_RPL] = close_con_rpl;
2113 work_handlers[CPL_RDMA_TERMINATE] = terminate;
2114 work_handlers[CPL_RDMA_EC_STATUS] = ec_status;
2118 void __exit iwch_cm_term(void)
2120 flush_workqueue(workq);
2121 destroy_workqueue(workq);