blob: d94388b81a4005b1e5d0dab5bdb780910c1d3665 [file] [log] [blame]
Steve Wiseb038ced2007-02-12 16:16:18 -08001/*
2 * Copyright (c) 2006 Chelsio, Inc. All rights reserved.
Steve Wiseb038ced2007-02-12 16:16:18 -08003 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 */
32#include <linux/module.h>
33#include <linux/list.h>
34#include <linux/workqueue.h>
35#include <linux/skbuff.h>
36#include <linux/timer.h>
37#include <linux/notifier.h>
Steve Wise8704e9a2008-02-12 16:09:29 -060038#include <linux/inetdevice.h>
Steve Wiseb038ced2007-02-12 16:16:18 -080039
40#include <net/neighbour.h>
41#include <net/netevent.h>
42#include <net/route.h>
43
44#include "tcb.h"
45#include "cxgb3_offload.h"
46#include "iwch.h"
47#include "iwch_provider.h"
48#include "iwch_cm.h"
49
50static char *states[] = {
51 "idle",
52 "listen",
53 "connecting",
54 "mpa_wait_req",
55 "mpa_req_sent",
56 "mpa_req_rcvd",
57 "mpa_rep_sent",
58 "fpdu_mode",
59 "aborting",
60 "closing",
61 "moribund",
62 "dead",
63 NULL,
64};
65
Steve Wisef8b0dfd2008-04-29 13:46:52 -070066int peer2peer = 0;
67module_param(peer2peer, int, 0644);
68MODULE_PARM_DESC(peer2peer, "Support peer2peer ULPs (default=0)");
69
Steve Wise77a8d572008-05-02 10:57:09 -070070static int ep_timeout_secs = 60;
Steve Wisee54664c2007-07-29 15:12:26 -050071module_param(ep_timeout_secs, int, 0644);
Steve Wiseb038ced2007-02-12 16:16:18 -080072MODULE_PARM_DESC(ep_timeout_secs, "CM Endpoint operation timeout "
Steve Wise77a8d572008-05-02 10:57:09 -070073 "in seconds (default=60)");
Steve Wiseb038ced2007-02-12 16:16:18 -080074
75static int mpa_rev = 1;
Steve Wisee54664c2007-07-29 15:12:26 -050076module_param(mpa_rev, int, 0644);
Steve Wiseb038ced2007-02-12 16:16:18 -080077MODULE_PARM_DESC(mpa_rev, "MPA Revision, 0 supports amso1100, "
78 "1 is spec compliant. (default=1)");
79
80static int markers_enabled = 0;
Steve Wisee54664c2007-07-29 15:12:26 -050081module_param(markers_enabled, int, 0644);
Steve Wiseb038ced2007-02-12 16:16:18 -080082MODULE_PARM_DESC(markers_enabled, "Enable MPA MARKERS (default(0)=disabled)");
83
84static int crc_enabled = 1;
Steve Wisee54664c2007-07-29 15:12:26 -050085module_param(crc_enabled, int, 0644);
Steve Wiseb038ced2007-02-12 16:16:18 -080086MODULE_PARM_DESC(crc_enabled, "Enable MPA CRC (default(1)=enabled)");
87
88static int rcv_win = 256 * 1024;
Steve Wisee54664c2007-07-29 15:12:26 -050089module_param(rcv_win, int, 0644);
Steve Wiseb038ced2007-02-12 16:16:18 -080090MODULE_PARM_DESC(rcv_win, "TCP receive window in bytes (default=256)");
91
92static int snd_win = 32 * 1024;
Steve Wisee54664c2007-07-29 15:12:26 -050093module_param(snd_win, int, 0644);
Steve Wiseb038ced2007-02-12 16:16:18 -080094MODULE_PARM_DESC(snd_win, "TCP send window in bytes (default=32KB)");
95
96static unsigned int nocong = 0;
Steve Wisee54664c2007-07-29 15:12:26 -050097module_param(nocong, uint, 0644);
Steve Wiseb038ced2007-02-12 16:16:18 -080098MODULE_PARM_DESC(nocong, "Turn off congestion control (default=0)");
99
100static unsigned int cong_flavor = 1;
Steve Wisee54664c2007-07-29 15:12:26 -0500101module_param(cong_flavor, uint, 0644);
Steve Wiseb038ced2007-02-12 16:16:18 -0800102MODULE_PARM_DESC(cong_flavor, "TCP Congestion control flavor (default=1)");
103
104static void process_work(struct work_struct *work);
105static struct workqueue_struct *workq;
106static DECLARE_WORK(skb_work, process_work);
107
108static struct sk_buff_head rxq;
109static cxgb3_cpl_handler_func work_handlers[NUM_CPL_CMDS];
110
111static struct sk_buff *get_skb(struct sk_buff *skb, int len, gfp_t gfp);
112static void ep_timeout(unsigned long arg);
113static void connect_reply_upcall(struct iwch_ep *ep, int status);
114
115static void start_ep_timer(struct iwch_ep *ep)
116{
Harvey Harrison33718362008-04-16 21:01:10 -0700117 PDBG("%s ep %p\n", __func__, ep);
Steve Wiseb038ced2007-02-12 16:16:18 -0800118 if (timer_pending(&ep->timer)) {
Harvey Harrison33718362008-04-16 21:01:10 -0700119 PDBG("%s stopped / restarted timer ep %p\n", __func__, ep);
Steve Wiseb038ced2007-02-12 16:16:18 -0800120 del_timer_sync(&ep->timer);
121 } else
122 get_ep(&ep->com);
123 ep->timer.expires = jiffies + ep_timeout_secs * HZ;
124 ep->timer.data = (unsigned long)ep;
125 ep->timer.function = ep_timeout;
126 add_timer(&ep->timer);
127}
128
129static void stop_ep_timer(struct iwch_ep *ep)
130{
Harvey Harrison33718362008-04-16 21:01:10 -0700131 PDBG("%s ep %p\n", __func__, ep);
Steve Wise989a1782008-04-29 13:46:51 -0700132 if (!timer_pending(&ep->timer)) {
133 printk(KERN_ERR "%s timer stopped when its not running! ep %p state %u\n",
134 __func__, ep, ep->com.state);
135 WARN_ON(1);
136 return;
137 }
Steve Wiseb038ced2007-02-12 16:16:18 -0800138 del_timer_sync(&ep->timer);
139 put_ep(&ep->com);
140}
141
Steve Wise04b5d022009-03-30 08:37:56 -0700142int iwch_l2t_send(struct t3cdev *tdev, struct sk_buff *skb, struct l2t_entry *l2e)
143{
144 int error = 0;
145 struct cxio_rdev *rdev;
146
147 rdev = (struct cxio_rdev *)tdev->ulp;
148 if (cxio_fatal_error(rdev)) {
149 kfree_skb(skb);
150 return -EIO;
151 }
152 error = l2t_send(tdev, skb, l2e);
153 if (error)
154 kfree_skb(skb);
155 return error;
156}
157
158int iwch_cxgb3_ofld_send(struct t3cdev *tdev, struct sk_buff *skb)
159{
160 int error = 0;
161 struct cxio_rdev *rdev;
162
163 rdev = (struct cxio_rdev *)tdev->ulp;
164 if (cxio_fatal_error(rdev)) {
165 kfree_skb(skb);
166 return -EIO;
167 }
168 error = cxgb3_ofld_send(tdev, skb);
169 if (error)
170 kfree_skb(skb);
171 return error;
172}
173
Steve Wiseb038ced2007-02-12 16:16:18 -0800174static void release_tid(struct t3cdev *tdev, u32 hwtid, struct sk_buff *skb)
175{
176 struct cpl_tid_release *req;
177
178 skb = get_skb(skb, sizeof *req, GFP_KERNEL);
179 if (!skb)
180 return;
181 req = (struct cpl_tid_release *) skb_put(skb, sizeof(*req));
182 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
183 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_TID_RELEASE, hwtid));
184 skb->priority = CPL_PRIORITY_SETUP;
Steve Wise04b5d022009-03-30 08:37:56 -0700185 iwch_cxgb3_ofld_send(tdev, skb);
Steve Wiseb038ced2007-02-12 16:16:18 -0800186 return;
187}
188
189int iwch_quiesce_tid(struct iwch_ep *ep)
190{
191 struct cpl_set_tcb_field *req;
192 struct sk_buff *skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
193
194 if (!skb)
195 return -ENOMEM;
196 req = (struct cpl_set_tcb_field *) skb_put(skb, sizeof(*req));
197 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
198 req->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
199 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, ep->hwtid));
200 req->reply = 0;
201 req->cpu_idx = 0;
202 req->word = htons(W_TCB_RX_QUIESCE);
203 req->mask = cpu_to_be64(1ULL << S_TCB_RX_QUIESCE);
204 req->val = cpu_to_be64(1 << S_TCB_RX_QUIESCE);
205
206 skb->priority = CPL_PRIORITY_DATA;
Steve Wise04b5d022009-03-30 08:37:56 -0700207 return iwch_cxgb3_ofld_send(ep->com.tdev, skb);
Steve Wiseb038ced2007-02-12 16:16:18 -0800208}
209
210int iwch_resume_tid(struct iwch_ep *ep)
211{
212 struct cpl_set_tcb_field *req;
213 struct sk_buff *skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
214
215 if (!skb)
216 return -ENOMEM;
217 req = (struct cpl_set_tcb_field *) skb_put(skb, sizeof(*req));
218 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
219 req->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
220 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, ep->hwtid));
221 req->reply = 0;
222 req->cpu_idx = 0;
223 req->word = htons(W_TCB_RX_QUIESCE);
224 req->mask = cpu_to_be64(1ULL << S_TCB_RX_QUIESCE);
225 req->val = 0;
226
227 skb->priority = CPL_PRIORITY_DATA;
Steve Wise04b5d022009-03-30 08:37:56 -0700228 return iwch_cxgb3_ofld_send(ep->com.tdev, skb);
Steve Wiseb038ced2007-02-12 16:16:18 -0800229}
230
231static void set_emss(struct iwch_ep *ep, u16 opt)
232{
Harvey Harrison33718362008-04-16 21:01:10 -0700233 PDBG("%s ep %p opt %u\n", __func__, ep, opt);
Steve Wiseb038ced2007-02-12 16:16:18 -0800234 ep->emss = T3C_DATA(ep->com.tdev)->mtus[G_TCPOPT_MSS(opt)] - 40;
235 if (G_TCPOPT_TSTAMP(opt))
236 ep->emss -= 12;
237 if (ep->emss < 128)
238 ep->emss = 128;
239 PDBG("emss=%d\n", ep->emss);
240}
241
242static enum iwch_ep_state state_read(struct iwch_ep_common *epc)
243{
244 unsigned long flags;
245 enum iwch_ep_state state;
246
247 spin_lock_irqsave(&epc->lock, flags);
248 state = epc->state;
249 spin_unlock_irqrestore(&epc->lock, flags);
250 return state;
251}
252
Adrian Bunk2b540352007-02-21 11:52:49 +0100253static void __state_set(struct iwch_ep_common *epc, enum iwch_ep_state new)
Steve Wiseb038ced2007-02-12 16:16:18 -0800254{
255 epc->state = new;
256}
257
258static void state_set(struct iwch_ep_common *epc, enum iwch_ep_state new)
259{
260 unsigned long flags;
261
262 spin_lock_irqsave(&epc->lock, flags);
Harvey Harrison33718362008-04-16 21:01:10 -0700263 PDBG("%s - %s -> %s\n", __func__, states[epc->state], states[new]);
Steve Wiseb038ced2007-02-12 16:16:18 -0800264 __state_set(epc, new);
265 spin_unlock_irqrestore(&epc->lock, flags);
266 return;
267}
268
269static void *alloc_ep(int size, gfp_t gfp)
270{
271 struct iwch_ep_common *epc;
272
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700273 epc = kzalloc(size, gfp);
Steve Wiseb038ced2007-02-12 16:16:18 -0800274 if (epc) {
Steve Wiseb038ced2007-02-12 16:16:18 -0800275 kref_init(&epc->kref);
276 spin_lock_init(&epc->lock);
277 init_waitqueue_head(&epc->waitq);
278 }
Harvey Harrison33718362008-04-16 21:01:10 -0700279 PDBG("%s alloc ep %p\n", __func__, epc);
Steve Wiseb038ced2007-02-12 16:16:18 -0800280 return epc;
281}
282
283void __free_ep(struct kref *kref)
284{
Steve Wise874d8df2009-03-30 08:37:59 -0700285 struct iwch_ep *ep;
286 ep = container_of(container_of(kref, struct iwch_ep_common, kref),
287 struct iwch_ep, com);
288 PDBG("%s ep %p state %s\n", __func__, ep, states[state_read(&ep->com)]);
Steve Wise6e47fe42009-09-05 20:22:38 -0700289 if (test_bit(RELEASE_RESOURCES, &ep->com.flags)) {
Steve Wise874d8df2009-03-30 08:37:59 -0700290 cxgb3_remove_tid(ep->com.tdev, (void *)ep, ep->hwtid);
291 dst_release(ep->dst);
292 l2t_release(L2DATA(ep->com.tdev), ep->l2t);
293 }
294 kfree(ep);
Steve Wiseb038ced2007-02-12 16:16:18 -0800295}
296
297static void release_ep_resources(struct iwch_ep *ep)
298{
Harvey Harrison33718362008-04-16 21:01:10 -0700299 PDBG("%s ep %p tid %d\n", __func__, ep, ep->hwtid);
Steve Wise6e47fe42009-09-05 20:22:38 -0700300 set_bit(RELEASE_RESOURCES, &ep->com.flags);
Steve Wiseb038ced2007-02-12 16:16:18 -0800301 put_ep(&ep->com);
302}
303
304static void process_work(struct work_struct *work)
305{
306 struct sk_buff *skb = NULL;
307 void *ep;
308 struct t3cdev *tdev;
309 int ret;
310
311 while ((skb = skb_dequeue(&rxq))) {
312 ep = *((void **) (skb->cb));
313 tdev = *((struct t3cdev **) (skb->cb + sizeof(void *)));
314 ret = work_handlers[G_OPCODE(ntohl((__force __be32)skb->csum))](tdev, skb, ep);
315 if (ret & CPL_RET_BUF_DONE)
316 kfree_skb(skb);
317
318 /*
319 * ep was referenced in sched(), and is freed here.
320 */
321 put_ep((struct iwch_ep_common *)ep);
322 }
323}
324
325static int status2errno(int status)
326{
327 switch (status) {
328 case CPL_ERR_NONE:
329 return 0;
330 case CPL_ERR_CONN_RESET:
331 return -ECONNRESET;
332 case CPL_ERR_ARP_MISS:
333 return -EHOSTUNREACH;
334 case CPL_ERR_CONN_TIMEDOUT:
335 return -ETIMEDOUT;
336 case CPL_ERR_TCAM_FULL:
337 return -ENOMEM;
338 case CPL_ERR_CONN_EXIST:
339 return -EADDRINUSE;
340 default:
341 return -EIO;
342 }
343}
344
345/*
346 * Try and reuse skbs already allocated...
347 */
348static struct sk_buff *get_skb(struct sk_buff *skb, int len, gfp_t gfp)
349{
Steve Wise1f6a8492007-03-06 14:44:05 -0600350 if (skb && !skb_is_nonlinear(skb) && !skb_cloned(skb)) {
Steve Wiseb038ced2007-02-12 16:16:18 -0800351 skb_trim(skb, 0);
352 skb_get(skb);
353 } else {
354 skb = alloc_skb(len, gfp);
355 }
356 return skb;
357}
358
359static struct rtable *find_route(struct t3cdev *dev, __be32 local_ip,
360 __be32 peer_ip, __be16 local_port,
361 __be16 peer_port, u8 tos)
362{
363 struct rtable *rt;
364 struct flowi fl = {
365 .oif = 0,
366 .nl_u = {
367 .ip4_u = {
368 .daddr = peer_ip,
369 .saddr = local_ip,
370 .tos = tos}
371 },
372 .proto = IPPROTO_TCP,
373 .uli_u = {
374 .ports = {
375 .sport = local_port,
376 .dport = peer_port}
377 }
378 };
379
Denis V. Lunevf1b050b2008-01-22 22:07:10 -0800380 if (ip_route_output_flow(&init_net, &rt, &fl, NULL, 0))
Steve Wiseb038ced2007-02-12 16:16:18 -0800381 return NULL;
382 return rt;
383}
384
385static unsigned int find_best_mtu(const struct t3c_data *d, unsigned short mtu)
386{
387 int i = 0;
388
389 while (i < d->nmtus - 1 && d->mtus[i + 1] <= mtu)
390 ++i;
391 return i;
392}
393
394static void arp_failure_discard(struct t3cdev *dev, struct sk_buff *skb)
395{
Harvey Harrison33718362008-04-16 21:01:10 -0700396 PDBG("%s t3cdev %p\n", __func__, dev);
Steve Wiseb038ced2007-02-12 16:16:18 -0800397 kfree_skb(skb);
398}
399
400/*
401 * Handle an ARP failure for an active open.
402 */
403static void act_open_req_arp_failure(struct t3cdev *dev, struct sk_buff *skb)
404{
405 printk(KERN_ERR MOD "ARP failure duing connect\n");
406 kfree_skb(skb);
407}
408
409/*
410 * Handle an ARP failure for a CPL_ABORT_REQ. Change it into a no RST variant
411 * and send it along.
412 */
413static void abort_arp_failure(struct t3cdev *dev, struct sk_buff *skb)
414{
415 struct cpl_abort_req *req = cplhdr(skb);
416
Harvey Harrison33718362008-04-16 21:01:10 -0700417 PDBG("%s t3cdev %p\n", __func__, dev);
Steve Wiseb038ced2007-02-12 16:16:18 -0800418 req->cmd = CPL_ABORT_NO_RST;
Steve Wise04b5d022009-03-30 08:37:56 -0700419 iwch_cxgb3_ofld_send(dev, skb);
Steve Wiseb038ced2007-02-12 16:16:18 -0800420}
421
422static int send_halfclose(struct iwch_ep *ep, gfp_t gfp)
423{
424 struct cpl_close_con_req *req;
425 struct sk_buff *skb;
426
Harvey Harrison33718362008-04-16 21:01:10 -0700427 PDBG("%s ep %p\n", __func__, ep);
Steve Wiseb038ced2007-02-12 16:16:18 -0800428 skb = get_skb(NULL, sizeof(*req), gfp);
429 if (!skb) {
Harvey Harrison33718362008-04-16 21:01:10 -0700430 printk(KERN_ERR MOD "%s - failed to alloc skb\n", __func__);
Steve Wiseb038ced2007-02-12 16:16:18 -0800431 return -ENOMEM;
432 }
433 skb->priority = CPL_PRIORITY_DATA;
434 set_arp_failure_handler(skb, arp_failure_discard);
435 req = (struct cpl_close_con_req *) skb_put(skb, sizeof(*req));
436 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_CLOSE_CON));
437 req->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
438 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_CON_REQ, ep->hwtid));
Steve Wise04b5d022009-03-30 08:37:56 -0700439 return iwch_l2t_send(ep->com.tdev, skb, ep->l2t);
Steve Wiseb038ced2007-02-12 16:16:18 -0800440}
441
442static int send_abort(struct iwch_ep *ep, struct sk_buff *skb, gfp_t gfp)
443{
444 struct cpl_abort_req *req;
445
Harvey Harrison33718362008-04-16 21:01:10 -0700446 PDBG("%s ep %p\n", __func__, ep);
Steve Wiseb038ced2007-02-12 16:16:18 -0800447 skb = get_skb(skb, sizeof(*req), gfp);
448 if (!skb) {
449 printk(KERN_ERR MOD "%s - failed to alloc skb.\n",
Harvey Harrison33718362008-04-16 21:01:10 -0700450 __func__);
Steve Wiseb038ced2007-02-12 16:16:18 -0800451 return -ENOMEM;
452 }
453 skb->priority = CPL_PRIORITY_DATA;
454 set_arp_failure_handler(skb, abort_arp_failure);
455 req = (struct cpl_abort_req *) skb_put(skb, sizeof(*req));
456 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_HOST_ABORT_CON_REQ));
457 req->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
458 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_ABORT_REQ, ep->hwtid));
459 req->cmd = CPL_ABORT_SEND_RST;
Steve Wise04b5d022009-03-30 08:37:56 -0700460 return iwch_l2t_send(ep->com.tdev, skb, ep->l2t);
Steve Wiseb038ced2007-02-12 16:16:18 -0800461}
462
463static int send_connect(struct iwch_ep *ep)
464{
465 struct cpl_act_open_req *req;
466 struct sk_buff *skb;
467 u32 opt0h, opt0l, opt2;
468 unsigned int mtu_idx;
469 int wscale;
470
Harvey Harrison33718362008-04-16 21:01:10 -0700471 PDBG("%s ep %p\n", __func__, ep);
Steve Wiseb038ced2007-02-12 16:16:18 -0800472
473 skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
474 if (!skb) {
475 printk(KERN_ERR MOD "%s - failed to alloc skb.\n",
Harvey Harrison33718362008-04-16 21:01:10 -0700476 __func__);
Steve Wiseb038ced2007-02-12 16:16:18 -0800477 return -ENOMEM;
478 }
479 mtu_idx = find_best_mtu(T3C_DATA(ep->com.tdev), dst_mtu(ep->dst));
480 wscale = compute_wscale(rcv_win);
481 opt0h = V_NAGLE(0) |
482 V_NO_CONG(nocong) |
483 V_KEEP_ALIVE(1) |
484 F_TCAM_BYPASS |
485 V_WND_SCALE(wscale) |
486 V_MSS_IDX(mtu_idx) |
487 V_L2T_IDX(ep->l2t->idx) | V_TX_CHANNEL(ep->l2t->smt_idx);
488 opt0l = V_TOS((ep->tos >> 2) & M_TOS) | V_RCV_BUFSIZ(rcv_win>>10);
489 opt2 = V_FLAVORS_VALID(1) | V_CONG_CONTROL_FLAVOR(cong_flavor);
490 skb->priority = CPL_PRIORITY_SETUP;
491 set_arp_failure_handler(skb, act_open_req_arp_failure);
492
493 req = (struct cpl_act_open_req *) skb_put(skb, sizeof(*req));
494 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
495 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_ACT_OPEN_REQ, ep->atid));
496 req->local_port = ep->com.local_addr.sin_port;
497 req->peer_port = ep->com.remote_addr.sin_port;
498 req->local_ip = ep->com.local_addr.sin_addr.s_addr;
499 req->peer_ip = ep->com.remote_addr.sin_addr.s_addr;
500 req->opt0h = htonl(opt0h);
501 req->opt0l = htonl(opt0l);
502 req->params = 0;
503 req->opt2 = htonl(opt2);
Steve Wise04b5d022009-03-30 08:37:56 -0700504 return iwch_l2t_send(ep->com.tdev, skb, ep->l2t);
Steve Wiseb038ced2007-02-12 16:16:18 -0800505}
506
507static void send_mpa_req(struct iwch_ep *ep, struct sk_buff *skb)
508{
509 int mpalen;
510 struct tx_data_wr *req;
511 struct mpa_message *mpa;
512 int len;
513
Harvey Harrison33718362008-04-16 21:01:10 -0700514 PDBG("%s ep %p pd_len %d\n", __func__, ep, ep->plen);
Steve Wiseb038ced2007-02-12 16:16:18 -0800515
516 BUG_ON(skb_cloned(skb));
517
518 mpalen = sizeof(*mpa) + ep->plen;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700519 if (skb->data + mpalen + sizeof(*req) > skb_end_pointer(skb)) {
Steve Wiseb038ced2007-02-12 16:16:18 -0800520 kfree_skb(skb);
521 skb=alloc_skb(mpalen + sizeof(*req), GFP_KERNEL);
522 if (!skb) {
523 connect_reply_upcall(ep, -ENOMEM);
524 return;
525 }
526 }
527 skb_trim(skb, 0);
528 skb_reserve(skb, sizeof(*req));
529 skb_put(skb, mpalen);
530 skb->priority = CPL_PRIORITY_DATA;
531 mpa = (struct mpa_message *) skb->data;
532 memset(mpa, 0, sizeof(*mpa));
533 memcpy(mpa->key, MPA_KEY_REQ, sizeof(mpa->key));
534 mpa->flags = (crc_enabled ? MPA_CRC : 0) |
535 (markers_enabled ? MPA_MARKERS : 0);
536 mpa->private_data_size = htons(ep->plen);
537 mpa->revision = mpa_rev;
538
539 if (ep->plen)
540 memcpy(mpa->private_data, ep->mpa_pkt + sizeof(*mpa), ep->plen);
541
542 /*
543 * Reference the mpa skb. This ensures the data area
544 * will remain in memory until the hw acks the tx.
545 * Function tx_ack() will deref it.
546 */
547 skb_get(skb);
548 set_arp_failure_handler(skb, arp_failure_discard);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300549 skb_reset_transport_header(skb);
Steve Wiseb038ced2007-02-12 16:16:18 -0800550 len = skb->len;
551 req = (struct tx_data_wr *) skb_push(skb, sizeof(*req));
Steve Wisef8b0dfd2008-04-29 13:46:52 -0700552 req->wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_TX_DATA)|F_WR_COMPL);
Steve Wiseb038ced2007-02-12 16:16:18 -0800553 req->wr_lo = htonl(V_WR_TID(ep->hwtid));
554 req->len = htonl(len);
555 req->param = htonl(V_TX_PORT(ep->l2t->smt_idx) |
556 V_TX_SNDBUF(snd_win>>15));
Steve Wisede3d3532007-05-14 13:27:27 -0500557 req->flags = htonl(F_TX_INIT);
Steve Wiseb038ced2007-02-12 16:16:18 -0800558 req->sndseq = htonl(ep->snd_seq);
559 BUG_ON(ep->mpa_skb);
560 ep->mpa_skb = skb;
Steve Wise04b5d022009-03-30 08:37:56 -0700561 iwch_l2t_send(ep->com.tdev, skb, ep->l2t);
Steve Wiseb038ced2007-02-12 16:16:18 -0800562 start_ep_timer(ep);
563 state_set(&ep->com, MPA_REQ_SENT);
564 return;
565}
566
567static int send_mpa_reject(struct iwch_ep *ep, const void *pdata, u8 plen)
568{
569 int mpalen;
570 struct tx_data_wr *req;
571 struct mpa_message *mpa;
572 struct sk_buff *skb;
573
Harvey Harrison33718362008-04-16 21:01:10 -0700574 PDBG("%s ep %p plen %d\n", __func__, ep, plen);
Steve Wiseb038ced2007-02-12 16:16:18 -0800575
576 mpalen = sizeof(*mpa) + plen;
577
578 skb = get_skb(NULL, mpalen + sizeof(*req), GFP_KERNEL);
579 if (!skb) {
Harvey Harrison33718362008-04-16 21:01:10 -0700580 printk(KERN_ERR MOD "%s - cannot alloc skb!\n", __func__);
Steve Wiseb038ced2007-02-12 16:16:18 -0800581 return -ENOMEM;
582 }
583 skb_reserve(skb, sizeof(*req));
584 mpa = (struct mpa_message *) skb_put(skb, mpalen);
585 memset(mpa, 0, sizeof(*mpa));
586 memcpy(mpa->key, MPA_KEY_REP, sizeof(mpa->key));
587 mpa->flags = MPA_REJECT;
588 mpa->revision = mpa_rev;
589 mpa->private_data_size = htons(plen);
590 if (plen)
591 memcpy(mpa->private_data, pdata, plen);
592
593 /*
594 * Reference the mpa skb again. This ensures the data area
595 * will remain in memory until the hw acks the tx.
596 * Function tx_ack() will deref it.
597 */
598 skb_get(skb);
599 skb->priority = CPL_PRIORITY_DATA;
600 set_arp_failure_handler(skb, arp_failure_discard);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300601 skb_reset_transport_header(skb);
Steve Wiseb038ced2007-02-12 16:16:18 -0800602 req = (struct tx_data_wr *) skb_push(skb, sizeof(*req));
Steve Wisef8b0dfd2008-04-29 13:46:52 -0700603 req->wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_TX_DATA)|F_WR_COMPL);
Steve Wiseb038ced2007-02-12 16:16:18 -0800604 req->wr_lo = htonl(V_WR_TID(ep->hwtid));
605 req->len = htonl(mpalen);
606 req->param = htonl(V_TX_PORT(ep->l2t->smt_idx) |
607 V_TX_SNDBUF(snd_win>>15));
Steve Wisede3d3532007-05-14 13:27:27 -0500608 req->flags = htonl(F_TX_INIT);
Steve Wiseb038ced2007-02-12 16:16:18 -0800609 req->sndseq = htonl(ep->snd_seq);
610 BUG_ON(ep->mpa_skb);
611 ep->mpa_skb = skb;
Steve Wise04b5d022009-03-30 08:37:56 -0700612 return iwch_l2t_send(ep->com.tdev, skb, ep->l2t);
Steve Wiseb038ced2007-02-12 16:16:18 -0800613}
614
615static int send_mpa_reply(struct iwch_ep *ep, const void *pdata, u8 plen)
616{
617 int mpalen;
618 struct tx_data_wr *req;
619 struct mpa_message *mpa;
620 int len;
621 struct sk_buff *skb;
622
Harvey Harrison33718362008-04-16 21:01:10 -0700623 PDBG("%s ep %p plen %d\n", __func__, ep, plen);
Steve Wiseb038ced2007-02-12 16:16:18 -0800624
625 mpalen = sizeof(*mpa) + plen;
626
627 skb = get_skb(NULL, mpalen + sizeof(*req), GFP_KERNEL);
628 if (!skb) {
Harvey Harrison33718362008-04-16 21:01:10 -0700629 printk(KERN_ERR MOD "%s - cannot alloc skb!\n", __func__);
Steve Wiseb038ced2007-02-12 16:16:18 -0800630 return -ENOMEM;
631 }
632 skb->priority = CPL_PRIORITY_DATA;
633 skb_reserve(skb, sizeof(*req));
634 mpa = (struct mpa_message *) skb_put(skb, mpalen);
635 memset(mpa, 0, sizeof(*mpa));
636 memcpy(mpa->key, MPA_KEY_REP, sizeof(mpa->key));
637 mpa->flags = (ep->mpa_attr.crc_enabled ? MPA_CRC : 0) |
638 (markers_enabled ? MPA_MARKERS : 0);
639 mpa->revision = mpa_rev;
640 mpa->private_data_size = htons(plen);
641 if (plen)
642 memcpy(mpa->private_data, pdata, plen);
643
644 /*
645 * Reference the mpa skb. This ensures the data area
646 * will remain in memory until the hw acks the tx.
647 * Function tx_ack() will deref it.
648 */
649 skb_get(skb);
650 set_arp_failure_handler(skb, arp_failure_discard);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300651 skb_reset_transport_header(skb);
Steve Wiseb038ced2007-02-12 16:16:18 -0800652 len = skb->len;
653 req = (struct tx_data_wr *) skb_push(skb, sizeof(*req));
Steve Wisef8b0dfd2008-04-29 13:46:52 -0700654 req->wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_TX_DATA)|F_WR_COMPL);
Steve Wiseb038ced2007-02-12 16:16:18 -0800655 req->wr_lo = htonl(V_WR_TID(ep->hwtid));
656 req->len = htonl(len);
657 req->param = htonl(V_TX_PORT(ep->l2t->smt_idx) |
658 V_TX_SNDBUF(snd_win>>15));
Steve Wisede3d3532007-05-14 13:27:27 -0500659 req->flags = htonl(F_TX_INIT);
Steve Wiseb038ced2007-02-12 16:16:18 -0800660 req->sndseq = htonl(ep->snd_seq);
661 ep->mpa_skb = skb;
662 state_set(&ep->com, MPA_REP_SENT);
Steve Wise04b5d022009-03-30 08:37:56 -0700663 return iwch_l2t_send(ep->com.tdev, skb, ep->l2t);
Steve Wiseb038ced2007-02-12 16:16:18 -0800664}
665
666static int act_establish(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
667{
668 struct iwch_ep *ep = ctx;
669 struct cpl_act_establish *req = cplhdr(skb);
670 unsigned int tid = GET_TID(req);
671
Harvey Harrison33718362008-04-16 21:01:10 -0700672 PDBG("%s ep %p tid %d\n", __func__, ep, tid);
Steve Wiseb038ced2007-02-12 16:16:18 -0800673
674 dst_confirm(ep->dst);
675
676 /* setup the hwtid for this connection */
677 ep->hwtid = tid;
678 cxgb3_insert_tid(ep->com.tdev, &t3c_client, ep, tid);
679
680 ep->snd_seq = ntohl(req->snd_isn);
Steve Wisede3d3532007-05-14 13:27:27 -0500681 ep->rcv_seq = ntohl(req->rcv_isn);
Steve Wiseb038ced2007-02-12 16:16:18 -0800682
683 set_emss(ep, ntohs(req->tcp_opt));
684
685 /* dealloc the atid */
686 cxgb3_free_atid(ep->com.tdev, ep->atid);
687
688 /* start MPA negotiation */
689 send_mpa_req(ep, skb);
690
691 return 0;
692}
693
694static void abort_connection(struct iwch_ep *ep, struct sk_buff *skb, gfp_t gfp)
695{
696 PDBG("%s ep %p\n", __FILE__, ep);
697 state_set(&ep->com, ABORTING);
698 send_abort(ep, skb, gfp);
699}
700
701static void close_complete_upcall(struct iwch_ep *ep)
702{
703 struct iw_cm_event event;
704
Harvey Harrison33718362008-04-16 21:01:10 -0700705 PDBG("%s ep %p\n", __func__, ep);
Steve Wiseb038ced2007-02-12 16:16:18 -0800706 memset(&event, 0, sizeof(event));
707 event.event = IW_CM_EVENT_CLOSE;
708 if (ep->com.cm_id) {
709 PDBG("close complete delivered ep %p cm_id %p tid %d\n",
710 ep, ep->com.cm_id, ep->hwtid);
711 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
712 ep->com.cm_id->rem_ref(ep->com.cm_id);
713 ep->com.cm_id = NULL;
714 ep->com.qp = NULL;
715 }
716}
717
718static void peer_close_upcall(struct iwch_ep *ep)
719{
720 struct iw_cm_event event;
721
Harvey Harrison33718362008-04-16 21:01:10 -0700722 PDBG("%s ep %p\n", __func__, ep);
Steve Wiseb038ced2007-02-12 16:16:18 -0800723 memset(&event, 0, sizeof(event));
724 event.event = IW_CM_EVENT_DISCONNECT;
725 if (ep->com.cm_id) {
726 PDBG("peer close delivered ep %p cm_id %p tid %d\n",
727 ep, ep->com.cm_id, ep->hwtid);
728 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
729 }
730}
731
732static void peer_abort_upcall(struct iwch_ep *ep)
733{
734 struct iw_cm_event event;
735
Harvey Harrison33718362008-04-16 21:01:10 -0700736 PDBG("%s ep %p\n", __func__, ep);
Steve Wiseb038ced2007-02-12 16:16:18 -0800737 memset(&event, 0, sizeof(event));
738 event.event = IW_CM_EVENT_CLOSE;
739 event.status = -ECONNRESET;
740 if (ep->com.cm_id) {
741 PDBG("abort delivered ep %p cm_id %p tid %d\n", ep,
742 ep->com.cm_id, ep->hwtid);
743 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
744 ep->com.cm_id->rem_ref(ep->com.cm_id);
745 ep->com.cm_id = NULL;
746 ep->com.qp = NULL;
747 }
748}
749
750static void connect_reply_upcall(struct iwch_ep *ep, int status)
751{
752 struct iw_cm_event event;
753
Harvey Harrison33718362008-04-16 21:01:10 -0700754 PDBG("%s ep %p status %d\n", __func__, ep, status);
Steve Wiseb038ced2007-02-12 16:16:18 -0800755 memset(&event, 0, sizeof(event));
756 event.event = IW_CM_EVENT_CONNECT_REPLY;
757 event.status = status;
758 event.local_addr = ep->com.local_addr;
759 event.remote_addr = ep->com.remote_addr;
760
761 if ((status == 0) || (status == -ECONNREFUSED)) {
762 event.private_data_len = ep->plen;
763 event.private_data = ep->mpa_pkt + sizeof(struct mpa_message);
764 }
765 if (ep->com.cm_id) {
Harvey Harrison33718362008-04-16 21:01:10 -0700766 PDBG("%s ep %p tid %d status %d\n", __func__, ep,
Steve Wiseb038ced2007-02-12 16:16:18 -0800767 ep->hwtid, status);
768 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
769 }
770 if (status < 0) {
771 ep->com.cm_id->rem_ref(ep->com.cm_id);
772 ep->com.cm_id = NULL;
773 ep->com.qp = NULL;
774 }
775}
776
777static void connect_request_upcall(struct iwch_ep *ep)
778{
779 struct iw_cm_event event;
780
Harvey Harrison33718362008-04-16 21:01:10 -0700781 PDBG("%s ep %p tid %d\n", __func__, ep, ep->hwtid);
Steve Wiseb038ced2007-02-12 16:16:18 -0800782 memset(&event, 0, sizeof(event));
783 event.event = IW_CM_EVENT_CONNECT_REQUEST;
784 event.local_addr = ep->com.local_addr;
785 event.remote_addr = ep->com.remote_addr;
786 event.private_data_len = ep->plen;
787 event.private_data = ep->mpa_pkt + sizeof(struct mpa_message);
788 event.provider_data = ep;
Steve Wise6e47fe42009-09-05 20:22:38 -0700789 if (state_read(&ep->parent_ep->com) != DEAD) {
790 get_ep(&ep->com);
Steve Wiseb038ced2007-02-12 16:16:18 -0800791 ep->parent_ep->com.cm_id->event_handler(
792 ep->parent_ep->com.cm_id,
793 &event);
Steve Wise6e47fe42009-09-05 20:22:38 -0700794 }
Steve Wiseb038ced2007-02-12 16:16:18 -0800795 put_ep(&ep->parent_ep->com);
796 ep->parent_ep = NULL;
797}
798
799static void established_upcall(struct iwch_ep *ep)
800{
801 struct iw_cm_event event;
802
Harvey Harrison33718362008-04-16 21:01:10 -0700803 PDBG("%s ep %p\n", __func__, ep);
Steve Wiseb038ced2007-02-12 16:16:18 -0800804 memset(&event, 0, sizeof(event));
805 event.event = IW_CM_EVENT_ESTABLISHED;
806 if (ep->com.cm_id) {
Harvey Harrison33718362008-04-16 21:01:10 -0700807 PDBG("%s ep %p tid %d\n", __func__, ep, ep->hwtid);
Steve Wiseb038ced2007-02-12 16:16:18 -0800808 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
809 }
810}
811
812static int update_rx_credits(struct iwch_ep *ep, u32 credits)
813{
814 struct cpl_rx_data_ack *req;
815 struct sk_buff *skb;
816
Harvey Harrison33718362008-04-16 21:01:10 -0700817 PDBG("%s ep %p credits %u\n", __func__, ep, credits);
Steve Wiseb038ced2007-02-12 16:16:18 -0800818 skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
819 if (!skb) {
820 printk(KERN_ERR MOD "update_rx_credits - cannot alloc skb!\n");
821 return 0;
822 }
823
824 req = (struct cpl_rx_data_ack *) skb_put(skb, sizeof(*req));
825 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
826 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_RX_DATA_ACK, ep->hwtid));
827 req->credit_dack = htonl(V_RX_CREDITS(credits) | V_RX_FORCE_ACK(1));
828 skb->priority = CPL_PRIORITY_ACK;
Steve Wise04b5d022009-03-30 08:37:56 -0700829 iwch_cxgb3_ofld_send(ep->com.tdev, skb);
Steve Wiseb038ced2007-02-12 16:16:18 -0800830 return credits;
831}
832
833static void process_mpa_reply(struct iwch_ep *ep, struct sk_buff *skb)
834{
835 struct mpa_message *mpa;
836 u16 plen;
837 struct iwch_qp_attributes attrs;
838 enum iwch_qp_attr_mask mask;
839 int err;
840
Harvey Harrison33718362008-04-16 21:01:10 -0700841 PDBG("%s ep %p\n", __func__, ep);
Steve Wiseb038ced2007-02-12 16:16:18 -0800842
843 /*
844 * Stop mpa timer. If it expired, then the state has
845 * changed and we bail since ep_timeout already aborted
846 * the connection.
847 */
848 stop_ep_timer(ep);
849 if (state_read(&ep->com) != MPA_REQ_SENT)
850 return;
851
852 /*
853 * If we get more than the supported amount of private data
854 * then we must fail this connection.
855 */
856 if (ep->mpa_pkt_len + skb->len > sizeof(ep->mpa_pkt)) {
857 err = -EINVAL;
858 goto err;
859 }
860
861 /*
862 * copy the new data into our accumulation buffer.
863 */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300864 skb_copy_from_linear_data(skb, &(ep->mpa_pkt[ep->mpa_pkt_len]),
865 skb->len);
Steve Wiseb038ced2007-02-12 16:16:18 -0800866 ep->mpa_pkt_len += skb->len;
867
868 /*
869 * if we don't even have the mpa message, then bail.
870 */
871 if (ep->mpa_pkt_len < sizeof(*mpa))
872 return;
873 mpa = (struct mpa_message *) ep->mpa_pkt;
874
875 /* Validate MPA header. */
876 if (mpa->revision != mpa_rev) {
877 err = -EPROTO;
878 goto err;
879 }
880 if (memcmp(mpa->key, MPA_KEY_REP, sizeof(mpa->key))) {
881 err = -EPROTO;
882 goto err;
883 }
884
885 plen = ntohs(mpa->private_data_size);
886
887 /*
888 * Fail if there's too much private data.
889 */
890 if (plen > MPA_MAX_PRIVATE_DATA) {
891 err = -EPROTO;
892 goto err;
893 }
894
895 /*
896 * If plen does not account for pkt size
897 */
898 if (ep->mpa_pkt_len > (sizeof(*mpa) + plen)) {
899 err = -EPROTO;
900 goto err;
901 }
902
903 ep->plen = (u8) plen;
904
905 /*
906 * If we don't have all the pdata yet, then bail.
907 * We'll continue process when more data arrives.
908 */
909 if (ep->mpa_pkt_len < (sizeof(*mpa) + plen))
910 return;
911
912 if (mpa->flags & MPA_REJECT) {
913 err = -ECONNREFUSED;
914 goto err;
915 }
916
917 /*
918 * If we get here we have accumulated the entire mpa
919 * start reply message including private data. And
920 * the MPA header is valid.
921 */
922 state_set(&ep->com, FPDU_MODE);
Steve Wisef8b0dfd2008-04-29 13:46:52 -0700923 ep->mpa_attr.initiator = 1;
Steve Wiseb038ced2007-02-12 16:16:18 -0800924 ep->mpa_attr.crc_enabled = (mpa->flags & MPA_CRC) | crc_enabled ? 1 : 0;
925 ep->mpa_attr.recv_marker_enabled = markers_enabled;
926 ep->mpa_attr.xmit_marker_enabled = mpa->flags & MPA_MARKERS ? 1 : 0;
927 ep->mpa_attr.version = mpa_rev;
928 PDBG("%s - crc_enabled=%d, recv_marker_enabled=%d, "
Harvey Harrison33718362008-04-16 21:01:10 -0700929 "xmit_marker_enabled=%d, version=%d\n", __func__,
Steve Wiseb038ced2007-02-12 16:16:18 -0800930 ep->mpa_attr.crc_enabled, ep->mpa_attr.recv_marker_enabled,
931 ep->mpa_attr.xmit_marker_enabled, ep->mpa_attr.version);
932
933 attrs.mpa_attr = ep->mpa_attr;
934 attrs.max_ird = ep->ird;
935 attrs.max_ord = ep->ord;
936 attrs.llp_stream_handle = ep;
937 attrs.next_state = IWCH_QP_STATE_RTS;
938
939 mask = IWCH_QP_ATTR_NEXT_STATE |
940 IWCH_QP_ATTR_LLP_STREAM_HANDLE | IWCH_QP_ATTR_MPA_ATTR |
941 IWCH_QP_ATTR_MAX_IRD | IWCH_QP_ATTR_MAX_ORD;
942
943 /* bind QP and TID with INIT_WR */
944 err = iwch_modify_qp(ep->com.qp->rhp,
945 ep->com.qp, mask, &attrs, 1);
Steve Wisef8b0dfd2008-04-29 13:46:52 -0700946 if (err)
947 goto err;
948
949 if (peer2peer && iwch_rqes_posted(ep->com.qp) == 0) {
950 iwch_post_zb_read(ep->com.qp);
951 }
952
953 goto out;
Steve Wiseb038ced2007-02-12 16:16:18 -0800954err:
955 abort_connection(ep, skb, GFP_KERNEL);
956out:
957 connect_reply_upcall(ep, err);
958 return;
959}
960
961static void process_mpa_request(struct iwch_ep *ep, struct sk_buff *skb)
962{
963 struct mpa_message *mpa;
964 u16 plen;
965
Harvey Harrison33718362008-04-16 21:01:10 -0700966 PDBG("%s ep %p\n", __func__, ep);
Steve Wiseb038ced2007-02-12 16:16:18 -0800967
968 /*
969 * Stop mpa timer. If it expired, then the state has
970 * changed and we bail since ep_timeout already aborted
971 * the connection.
972 */
973 stop_ep_timer(ep);
974 if (state_read(&ep->com) != MPA_REQ_WAIT)
975 return;
976
977 /*
978 * If we get more than the supported amount of private data
979 * then we must fail this connection.
980 */
981 if (ep->mpa_pkt_len + skb->len > sizeof(ep->mpa_pkt)) {
982 abort_connection(ep, skb, GFP_KERNEL);
983 return;
984 }
985
Harvey Harrison33718362008-04-16 21:01:10 -0700986 PDBG("%s enter (%s line %u)\n", __func__, __FILE__, __LINE__);
Steve Wiseb038ced2007-02-12 16:16:18 -0800987
988 /*
989 * Copy the new data into our accumulation buffer.
990 */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300991 skb_copy_from_linear_data(skb, &(ep->mpa_pkt[ep->mpa_pkt_len]),
992 skb->len);
Steve Wiseb038ced2007-02-12 16:16:18 -0800993 ep->mpa_pkt_len += skb->len;
994
995 /*
996 * If we don't even have the mpa message, then bail.
997 * We'll continue process when more data arrives.
998 */
999 if (ep->mpa_pkt_len < sizeof(*mpa))
1000 return;
Harvey Harrison33718362008-04-16 21:01:10 -07001001 PDBG("%s enter (%s line %u)\n", __func__, __FILE__, __LINE__);
Steve Wiseb038ced2007-02-12 16:16:18 -08001002 mpa = (struct mpa_message *) ep->mpa_pkt;
1003
1004 /*
1005 * Validate MPA Header.
1006 */
1007 if (mpa->revision != mpa_rev) {
1008 abort_connection(ep, skb, GFP_KERNEL);
1009 return;
1010 }
1011
1012 if (memcmp(mpa->key, MPA_KEY_REQ, sizeof(mpa->key))) {
1013 abort_connection(ep, skb, GFP_KERNEL);
1014 return;
1015 }
1016
1017 plen = ntohs(mpa->private_data_size);
1018
1019 /*
1020 * Fail if there's too much private data.
1021 */
1022 if (plen > MPA_MAX_PRIVATE_DATA) {
1023 abort_connection(ep, skb, GFP_KERNEL);
1024 return;
1025 }
1026
1027 /*
1028 * If plen does not account for pkt size
1029 */
1030 if (ep->mpa_pkt_len > (sizeof(*mpa) + plen)) {
1031 abort_connection(ep, skb, GFP_KERNEL);
1032 return;
1033 }
1034 ep->plen = (u8) plen;
1035
1036 /*
1037 * If we don't have all the pdata yet, then bail.
1038 */
1039 if (ep->mpa_pkt_len < (sizeof(*mpa) + plen))
1040 return;
1041
1042 /*
1043 * If we get here we have accumulated the entire mpa
1044 * start reply message including private data.
1045 */
Steve Wisef8b0dfd2008-04-29 13:46:52 -07001046 ep->mpa_attr.initiator = 0;
Steve Wiseb038ced2007-02-12 16:16:18 -08001047 ep->mpa_attr.crc_enabled = (mpa->flags & MPA_CRC) | crc_enabled ? 1 : 0;
1048 ep->mpa_attr.recv_marker_enabled = markers_enabled;
1049 ep->mpa_attr.xmit_marker_enabled = mpa->flags & MPA_MARKERS ? 1 : 0;
1050 ep->mpa_attr.version = mpa_rev;
1051 PDBG("%s - crc_enabled=%d, recv_marker_enabled=%d, "
Harvey Harrison33718362008-04-16 21:01:10 -07001052 "xmit_marker_enabled=%d, version=%d\n", __func__,
Steve Wiseb038ced2007-02-12 16:16:18 -08001053 ep->mpa_attr.crc_enabled, ep->mpa_attr.recv_marker_enabled,
1054 ep->mpa_attr.xmit_marker_enabled, ep->mpa_attr.version);
1055
1056 state_set(&ep->com, MPA_REQ_RCVD);
1057
1058 /* drive upcall */
1059 connect_request_upcall(ep);
1060 return;
1061}
1062
1063static int rx_data(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1064{
1065 struct iwch_ep *ep = ctx;
1066 struct cpl_rx_data *hdr = cplhdr(skb);
1067 unsigned int dlen = ntohs(hdr->len);
1068
Harvey Harrison33718362008-04-16 21:01:10 -07001069 PDBG("%s ep %p dlen %u\n", __func__, ep, dlen);
Steve Wiseb038ced2007-02-12 16:16:18 -08001070
1071 skb_pull(skb, sizeof(*hdr));
1072 skb_trim(skb, dlen);
1073
Steve Wisede3d3532007-05-14 13:27:27 -05001074 ep->rcv_seq += dlen;
1075 BUG_ON(ep->rcv_seq != (ntohl(hdr->seq) + dlen));
1076
Steve Wiseb038ced2007-02-12 16:16:18 -08001077 switch (state_read(&ep->com)) {
1078 case MPA_REQ_SENT:
1079 process_mpa_reply(ep, skb);
1080 break;
1081 case MPA_REQ_WAIT:
1082 process_mpa_request(ep, skb);
1083 break;
1084 case MPA_REP_SENT:
1085 break;
1086 default:
1087 printk(KERN_ERR MOD "%s Unexpected streaming data."
1088 " ep %p state %d tid %d\n",
Harvey Harrison33718362008-04-16 21:01:10 -07001089 __func__, ep, state_read(&ep->com), ep->hwtid);
Steve Wiseb038ced2007-02-12 16:16:18 -08001090
1091 /*
1092 * The ep will timeout and inform the ULP of the failure.
1093 * See ep_timeout().
1094 */
1095 break;
1096 }
1097
1098 /* update RX credits */
1099 update_rx_credits(ep, dlen);
1100
1101 return CPL_RET_BUF_DONE;
1102}
1103
1104/*
1105 * Upcall from the adapter indicating data has been transmitted.
1106 * For us its just the single MPA request or reply. We can now free
1107 * the skb holding the mpa message.
1108 */
1109static int tx_ack(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1110{
1111 struct iwch_ep *ep = ctx;
1112 struct cpl_wr_ack *hdr = cplhdr(skb);
1113 unsigned int credits = ntohs(hdr->credits);
Steve Wiseb038ced2007-02-12 16:16:18 -08001114
Harvey Harrison33718362008-04-16 21:01:10 -07001115 PDBG("%s ep %p credits %u\n", __func__, ep, credits);
Steve Wiseb038ced2007-02-12 16:16:18 -08001116
Steve Wisef8b0dfd2008-04-29 13:46:52 -07001117 if (credits == 0) {
1118 PDBG(KERN_ERR "%s 0 credit ack ep %p state %u\n",
1119 __func__, ep, state_read(&ep->com));
Steve Wiseb038ced2007-02-12 16:16:18 -08001120 return CPL_RET_BUF_DONE;
Steve Wisef8b0dfd2008-04-29 13:46:52 -07001121 }
1122
Steve Wiseb038ced2007-02-12 16:16:18 -08001123 BUG_ON(credits != 1);
Steve Wiseb038ced2007-02-12 16:16:18 -08001124 dst_confirm(ep->dst);
Steve Wisef8b0dfd2008-04-29 13:46:52 -07001125 if (!ep->mpa_skb) {
1126 PDBG("%s rdma_init wr_ack ep %p state %u\n",
1127 __func__, ep, state_read(&ep->com));
1128 if (ep->mpa_attr.initiator) {
1129 PDBG("%s initiator ep %p state %u\n",
1130 __func__, ep, state_read(&ep->com));
1131 if (peer2peer)
1132 iwch_post_zb_read(ep->com.qp);
1133 } else {
1134 PDBG("%s responder ep %p state %u\n",
1135 __func__, ep, state_read(&ep->com));
1136 ep->com.rpl_done = 1;
1137 wake_up(&ep->com.waitq);
1138 }
1139 } else {
1140 PDBG("%s lsm ack ep %p state %u freeing skb\n",
1141 __func__, ep, state_read(&ep->com));
1142 kfree_skb(ep->mpa_skb);
1143 ep->mpa_skb = NULL;
Steve Wiseb038ced2007-02-12 16:16:18 -08001144 }
1145 return CPL_RET_BUF_DONE;
1146}
1147
1148static int abort_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1149{
1150 struct iwch_ep *ep = ctx;
Steve Wise989a1782008-04-29 13:46:51 -07001151 unsigned long flags;
1152 int release = 0;
Steve Wiseb038ced2007-02-12 16:16:18 -08001153
Harvey Harrison33718362008-04-16 21:01:10 -07001154 PDBG("%s ep %p\n", __func__, ep);
Steve Wise989a1782008-04-29 13:46:51 -07001155 BUG_ON(!ep);
Steve Wiseb038ced2007-02-12 16:16:18 -08001156
Steve Wiseaff9e392007-04-26 15:21:20 -05001157 /*
1158 * We get 2 abort replies from the HW. The first one must
1159 * be ignored except for scribbling that we need one more.
1160 */
Steve Wise6e47fe42009-09-05 20:22:38 -07001161 if (!test_and_set_bit(ABORT_REQ_IN_PROGRESS, &ep->com.flags)) {
Steve Wiseaff9e392007-04-26 15:21:20 -05001162 return CPL_RET_BUF_DONE;
1163 }
1164
Steve Wise989a1782008-04-29 13:46:51 -07001165 spin_lock_irqsave(&ep->com.lock, flags);
1166 switch (ep->com.state) {
1167 case ABORTING:
1168 close_complete_upcall(ep);
1169 __state_set(&ep->com, DEAD);
1170 release = 1;
1171 break;
1172 default:
1173 printk(KERN_ERR "%s ep %p state %d\n",
1174 __func__, ep, ep->com.state);
1175 break;
1176 }
1177 spin_unlock_irqrestore(&ep->com.lock, flags);
1178
1179 if (release)
1180 release_ep_resources(ep);
Steve Wiseb038ced2007-02-12 16:16:18 -08001181 return CPL_RET_BUF_DONE;
1182}
1183
Steve Wise96d0e492007-06-21 18:17:57 -05001184/*
1185 * Return whether a failed active open has allocated a TID
1186 */
1187static inline int act_open_has_tid(int status)
1188{
1189 return status != CPL_ERR_TCAM_FULL && status != CPL_ERR_CONN_EXIST &&
1190 status != CPL_ERR_ARP_MISS;
1191}
1192
Steve Wiseb038ced2007-02-12 16:16:18 -08001193static int act_open_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1194{
1195 struct iwch_ep *ep = ctx;
1196 struct cpl_act_open_rpl *rpl = cplhdr(skb);
1197
Harvey Harrison33718362008-04-16 21:01:10 -07001198 PDBG("%s ep %p status %u errno %d\n", __func__, ep, rpl->status,
Steve Wiseb038ced2007-02-12 16:16:18 -08001199 status2errno(rpl->status));
1200 connect_reply_upcall(ep, status2errno(rpl->status));
1201 state_set(&ep->com, DEAD);
Steve Wise8176d292008-01-24 16:30:16 -06001202 if (ep->com.tdev->type != T3A && act_open_has_tid(rpl->status))
Steve Wiseb038ced2007-02-12 16:16:18 -08001203 release_tid(ep->com.tdev, GET_TID(rpl), NULL);
1204 cxgb3_free_atid(ep->com.tdev, ep->atid);
1205 dst_release(ep->dst);
1206 l2t_release(L2DATA(ep->com.tdev), ep->l2t);
1207 put_ep(&ep->com);
1208 return CPL_RET_BUF_DONE;
1209}
1210
1211static int listen_start(struct iwch_listen_ep *ep)
1212{
1213 struct sk_buff *skb;
1214 struct cpl_pass_open_req *req;
1215
Harvey Harrison33718362008-04-16 21:01:10 -07001216 PDBG("%s ep %p\n", __func__, ep);
Steve Wiseb038ced2007-02-12 16:16:18 -08001217 skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
1218 if (!skb) {
1219 printk(KERN_ERR MOD "t3c_listen_start failed to alloc skb!\n");
1220 return -ENOMEM;
1221 }
1222
1223 req = (struct cpl_pass_open_req *) skb_put(skb, sizeof(*req));
1224 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
1225 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ, ep->stid));
1226 req->local_port = ep->com.local_addr.sin_port;
1227 req->local_ip = ep->com.local_addr.sin_addr.s_addr;
1228 req->peer_port = 0;
1229 req->peer_ip = 0;
1230 req->peer_netmask = 0;
1231 req->opt0h = htonl(F_DELACK | F_TCAM_BYPASS);
1232 req->opt0l = htonl(V_RCV_BUFSIZ(rcv_win>>10));
1233 req->opt1 = htonl(V_CONN_POLICY(CPL_CONN_POLICY_ASK));
1234
1235 skb->priority = 1;
Steve Wise04b5d022009-03-30 08:37:56 -07001236 return iwch_cxgb3_ofld_send(ep->com.tdev, skb);
Steve Wiseb038ced2007-02-12 16:16:18 -08001237}
1238
1239static int pass_open_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1240{
1241 struct iwch_listen_ep *ep = ctx;
1242 struct cpl_pass_open_rpl *rpl = cplhdr(skb);
1243
Harvey Harrison33718362008-04-16 21:01:10 -07001244 PDBG("%s ep %p status %d error %d\n", __func__, ep,
Steve Wiseb038ced2007-02-12 16:16:18 -08001245 rpl->status, status2errno(rpl->status));
1246 ep->com.rpl_err = status2errno(rpl->status);
1247 ep->com.rpl_done = 1;
1248 wake_up(&ep->com.waitq);
1249
1250 return CPL_RET_BUF_DONE;
1251}
1252
1253static int listen_stop(struct iwch_listen_ep *ep)
1254{
1255 struct sk_buff *skb;
1256 struct cpl_close_listserv_req *req;
1257
Harvey Harrison33718362008-04-16 21:01:10 -07001258 PDBG("%s ep %p\n", __func__, ep);
Steve Wiseb038ced2007-02-12 16:16:18 -08001259 skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
1260 if (!skb) {
Harvey Harrison33718362008-04-16 21:01:10 -07001261 printk(KERN_ERR MOD "%s - failed to alloc skb\n", __func__);
Steve Wiseb038ced2007-02-12 16:16:18 -08001262 return -ENOMEM;
1263 }
1264 req = (struct cpl_close_listserv_req *) skb_put(skb, sizeof(*req));
1265 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
Steve Wise60be4b52007-04-26 15:21:15 -05001266 req->cpu_idx = 0;
Steve Wiseb038ced2007-02-12 16:16:18 -08001267 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_LISTSRV_REQ, ep->stid));
1268 skb->priority = 1;
Steve Wise04b5d022009-03-30 08:37:56 -07001269 return iwch_cxgb3_ofld_send(ep->com.tdev, skb);
Steve Wiseb038ced2007-02-12 16:16:18 -08001270}
1271
1272static int close_listsrv_rpl(struct t3cdev *tdev, struct sk_buff *skb,
1273 void *ctx)
1274{
1275 struct iwch_listen_ep *ep = ctx;
1276 struct cpl_close_listserv_rpl *rpl = cplhdr(skb);
1277
Harvey Harrison33718362008-04-16 21:01:10 -07001278 PDBG("%s ep %p\n", __func__, ep);
Steve Wiseb038ced2007-02-12 16:16:18 -08001279 ep->com.rpl_err = status2errno(rpl->status);
1280 ep->com.rpl_done = 1;
1281 wake_up(&ep->com.waitq);
1282 return CPL_RET_BUF_DONE;
1283}
1284
1285static void accept_cr(struct iwch_ep *ep, __be32 peer_ip, struct sk_buff *skb)
1286{
1287 struct cpl_pass_accept_rpl *rpl;
1288 unsigned int mtu_idx;
1289 u32 opt0h, opt0l, opt2;
1290 int wscale;
1291
Harvey Harrison33718362008-04-16 21:01:10 -07001292 PDBG("%s ep %p\n", __func__, ep);
Steve Wiseb038ced2007-02-12 16:16:18 -08001293 BUG_ON(skb_cloned(skb));
1294 skb_trim(skb, sizeof(*rpl));
1295 skb_get(skb);
1296 mtu_idx = find_best_mtu(T3C_DATA(ep->com.tdev), dst_mtu(ep->dst));
1297 wscale = compute_wscale(rcv_win);
1298 opt0h = V_NAGLE(0) |
1299 V_NO_CONG(nocong) |
1300 V_KEEP_ALIVE(1) |
1301 F_TCAM_BYPASS |
1302 V_WND_SCALE(wscale) |
1303 V_MSS_IDX(mtu_idx) |
1304 V_L2T_IDX(ep->l2t->idx) | V_TX_CHANNEL(ep->l2t->smt_idx);
1305 opt0l = V_TOS((ep->tos >> 2) & M_TOS) | V_RCV_BUFSIZ(rcv_win>>10);
1306 opt2 = V_FLAVORS_VALID(1) | V_CONG_CONTROL_FLAVOR(cong_flavor);
1307
1308 rpl = cplhdr(skb);
1309 rpl->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
1310 OPCODE_TID(rpl) = htonl(MK_OPCODE_TID(CPL_PASS_ACCEPT_RPL, ep->hwtid));
1311 rpl->peer_ip = peer_ip;
1312 rpl->opt0h = htonl(opt0h);
1313 rpl->opt0l_status = htonl(opt0l | CPL_PASS_OPEN_ACCEPT);
1314 rpl->opt2 = htonl(opt2);
1315 rpl->rsvd = rpl->opt2; /* workaround for HW bug */
1316 skb->priority = CPL_PRIORITY_SETUP;
Steve Wise04b5d022009-03-30 08:37:56 -07001317 iwch_l2t_send(ep->com.tdev, skb, ep->l2t);
Steve Wiseb038ced2007-02-12 16:16:18 -08001318
1319 return;
1320}
1321
1322static void reject_cr(struct t3cdev *tdev, u32 hwtid, __be32 peer_ip,
1323 struct sk_buff *skb)
1324{
Harvey Harrison33718362008-04-16 21:01:10 -07001325 PDBG("%s t3cdev %p tid %u peer_ip %x\n", __func__, tdev, hwtid,
Steve Wiseb038ced2007-02-12 16:16:18 -08001326 peer_ip);
1327 BUG_ON(skb_cloned(skb));
1328 skb_trim(skb, sizeof(struct cpl_tid_release));
1329 skb_get(skb);
1330
Steve Wise8176d292008-01-24 16:30:16 -06001331 if (tdev->type != T3A)
Steve Wiseb038ced2007-02-12 16:16:18 -08001332 release_tid(tdev, hwtid, skb);
1333 else {
1334 struct cpl_pass_accept_rpl *rpl;
1335
1336 rpl = cplhdr(skb);
1337 skb->priority = CPL_PRIORITY_SETUP;
1338 rpl->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
1339 OPCODE_TID(rpl) = htonl(MK_OPCODE_TID(CPL_PASS_ACCEPT_RPL,
1340 hwtid));
1341 rpl->peer_ip = peer_ip;
1342 rpl->opt0h = htonl(F_TCAM_BYPASS);
1343 rpl->opt0l_status = htonl(CPL_PASS_OPEN_REJECT);
1344 rpl->opt2 = 0;
1345 rpl->rsvd = rpl->opt2;
Steve Wise04b5d022009-03-30 08:37:56 -07001346 iwch_cxgb3_ofld_send(tdev, skb);
Steve Wiseb038ced2007-02-12 16:16:18 -08001347 }
1348}
1349
1350static int pass_accept_req(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1351{
1352 struct iwch_ep *child_ep, *parent_ep = ctx;
1353 struct cpl_pass_accept_req *req = cplhdr(skb);
1354 unsigned int hwtid = GET_TID(req);
1355 struct dst_entry *dst;
1356 struct l2t_entry *l2t;
1357 struct rtable *rt;
1358 struct iff_mac tim;
1359
Harvey Harrison33718362008-04-16 21:01:10 -07001360 PDBG("%s parent ep %p tid %u\n", __func__, parent_ep, hwtid);
Steve Wiseb038ced2007-02-12 16:16:18 -08001361
1362 if (state_read(&parent_ep->com) != LISTEN) {
1363 printk(KERN_ERR "%s - listening ep not in LISTEN\n",
Harvey Harrison33718362008-04-16 21:01:10 -07001364 __func__);
Steve Wiseb038ced2007-02-12 16:16:18 -08001365 goto reject;
1366 }
1367
1368 /*
1369 * Find the netdev for this connection request.
1370 */
1371 tim.mac_addr = req->dst_mac;
1372 tim.vlan_tag = ntohs(req->vlan_tag);
1373 if (tdev->ctl(tdev, GET_IFF_FROM_MAC, &tim) < 0 || !tim.dev) {
H Hartley Sweeteneacc4d62010-01-07 01:17:27 -08001374 printk(KERN_ERR "%s bad dst mac %pM\n",
1375 __func__, req->dst_mac);
Steve Wiseb038ced2007-02-12 16:16:18 -08001376 goto reject;
1377 }
1378
1379 /* Find output route */
1380 rt = find_route(tdev,
1381 req->local_ip,
1382 req->peer_ip,
1383 req->local_port,
1384 req->peer_port, G_PASS_OPEN_TOS(ntohl(req->tos_tid)));
1385 if (!rt) {
1386 printk(KERN_ERR MOD "%s - failed to find dst entry!\n",
Harvey Harrison33718362008-04-16 21:01:10 -07001387 __func__);
Steve Wiseb038ced2007-02-12 16:16:18 -08001388 goto reject;
1389 }
1390 dst = &rt->u.dst;
1391 l2t = t3_l2t_get(tdev, dst->neighbour, dst->neighbour->dev);
1392 if (!l2t) {
1393 printk(KERN_ERR MOD "%s - failed to allocate l2t entry!\n",
Harvey Harrison33718362008-04-16 21:01:10 -07001394 __func__);
Steve Wiseb038ced2007-02-12 16:16:18 -08001395 dst_release(dst);
1396 goto reject;
1397 }
1398 child_ep = alloc_ep(sizeof(*child_ep), GFP_KERNEL);
1399 if (!child_ep) {
1400 printk(KERN_ERR MOD "%s - failed to allocate ep entry!\n",
Harvey Harrison33718362008-04-16 21:01:10 -07001401 __func__);
Steve Wiseb038ced2007-02-12 16:16:18 -08001402 l2t_release(L2DATA(tdev), l2t);
1403 dst_release(dst);
1404 goto reject;
1405 }
1406 state_set(&child_ep->com, CONNECTING);
1407 child_ep->com.tdev = tdev;
1408 child_ep->com.cm_id = NULL;
1409 child_ep->com.local_addr.sin_family = PF_INET;
1410 child_ep->com.local_addr.sin_port = req->local_port;
1411 child_ep->com.local_addr.sin_addr.s_addr = req->local_ip;
1412 child_ep->com.remote_addr.sin_family = PF_INET;
1413 child_ep->com.remote_addr.sin_port = req->peer_port;
1414 child_ep->com.remote_addr.sin_addr.s_addr = req->peer_ip;
1415 get_ep(&parent_ep->com);
1416 child_ep->parent_ep = parent_ep;
1417 child_ep->tos = G_PASS_OPEN_TOS(ntohl(req->tos_tid));
1418 child_ep->l2t = l2t;
1419 child_ep->dst = dst;
1420 child_ep->hwtid = hwtid;
1421 init_timer(&child_ep->timer);
1422 cxgb3_insert_tid(tdev, &t3c_client, child_ep, hwtid);
1423 accept_cr(child_ep, req->peer_ip, skb);
1424 goto out;
1425reject:
1426 reject_cr(tdev, hwtid, req->peer_ip, skb);
1427out:
1428 return CPL_RET_BUF_DONE;
1429}
1430
1431static int pass_establish(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1432{
1433 struct iwch_ep *ep = ctx;
1434 struct cpl_pass_establish *req = cplhdr(skb);
1435
Harvey Harrison33718362008-04-16 21:01:10 -07001436 PDBG("%s ep %p\n", __func__, ep);
Steve Wiseb038ced2007-02-12 16:16:18 -08001437 ep->snd_seq = ntohl(req->snd_isn);
Steve Wisede3d3532007-05-14 13:27:27 -05001438 ep->rcv_seq = ntohl(req->rcv_isn);
Steve Wiseb038ced2007-02-12 16:16:18 -08001439
1440 set_emss(ep, ntohs(req->tcp_opt));
1441
1442 dst_confirm(ep->dst);
1443 state_set(&ep->com, MPA_REQ_WAIT);
1444 start_ep_timer(ep);
1445
1446 return CPL_RET_BUF_DONE;
1447}
1448
1449static int peer_close(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1450{
1451 struct iwch_ep *ep = ctx;
1452 struct iwch_qp_attributes attrs;
1453 unsigned long flags;
1454 int disconnect = 1;
1455 int release = 0;
1456
Harvey Harrison33718362008-04-16 21:01:10 -07001457 PDBG("%s ep %p\n", __func__, ep);
Steve Wiseb038ced2007-02-12 16:16:18 -08001458 dst_confirm(ep->dst);
1459
1460 spin_lock_irqsave(&ep->com.lock, flags);
1461 switch (ep->com.state) {
1462 case MPA_REQ_WAIT:
1463 __state_set(&ep->com, CLOSING);
1464 break;
1465 case MPA_REQ_SENT:
1466 __state_set(&ep->com, CLOSING);
1467 connect_reply_upcall(ep, -ECONNRESET);
1468 break;
1469 case MPA_REQ_RCVD:
1470
1471 /*
1472 * We're gonna mark this puppy DEAD, but keep
1473 * the reference on it until the ULP accepts or
Steve Wisea52bf982009-09-05 20:22:38 -07001474 * rejects the CR. Also wake up anyone waiting
1475 * in rdma connection migration (see iwch_accept_cr()).
Steve Wiseb038ced2007-02-12 16:16:18 -08001476 */
1477 __state_set(&ep->com, CLOSING);
Steve Wisea52bf982009-09-05 20:22:38 -07001478 ep->com.rpl_done = 1;
1479 ep->com.rpl_err = -ECONNRESET;
1480 PDBG("waking up ep %p\n", ep);
1481 wake_up(&ep->com.waitq);
Steve Wiseb038ced2007-02-12 16:16:18 -08001482 break;
1483 case MPA_REP_SENT:
1484 __state_set(&ep->com, CLOSING);
1485 ep->com.rpl_done = 1;
1486 ep->com.rpl_err = -ECONNRESET;
1487 PDBG("waking up ep %p\n", ep);
1488 wake_up(&ep->com.waitq);
1489 break;
1490 case FPDU_MODE:
Steve Wise42e31752007-03-06 14:43:56 -06001491 start_ep_timer(ep);
Steve Wiseb038ced2007-02-12 16:16:18 -08001492 __state_set(&ep->com, CLOSING);
1493 attrs.next_state = IWCH_QP_STATE_CLOSING;
1494 iwch_modify_qp(ep->com.qp->rhp, ep->com.qp,
1495 IWCH_QP_ATTR_NEXT_STATE, &attrs, 1);
1496 peer_close_upcall(ep);
1497 break;
1498 case ABORTING:
1499 disconnect = 0;
1500 break;
1501 case CLOSING:
Steve Wiseb038ced2007-02-12 16:16:18 -08001502 __state_set(&ep->com, MORIBUND);
1503 disconnect = 0;
1504 break;
1505 case MORIBUND:
1506 stop_ep_timer(ep);
1507 if (ep->com.cm_id && ep->com.qp) {
1508 attrs.next_state = IWCH_QP_STATE_IDLE;
1509 iwch_modify_qp(ep->com.qp->rhp, ep->com.qp,
1510 IWCH_QP_ATTR_NEXT_STATE, &attrs, 1);
1511 }
1512 close_complete_upcall(ep);
1513 __state_set(&ep->com, DEAD);
1514 release = 1;
1515 disconnect = 0;
1516 break;
1517 case DEAD:
1518 disconnect = 0;
1519 break;
1520 default:
1521 BUG_ON(1);
1522 }
1523 spin_unlock_irqrestore(&ep->com.lock, flags);
1524 if (disconnect)
1525 iwch_ep_disconnect(ep, 0, GFP_KERNEL);
1526 if (release)
1527 release_ep_resources(ep);
1528 return CPL_RET_BUF_DONE;
1529}
1530
1531/*
1532 * Returns whether an ABORT_REQ_RSS message is a negative advice.
1533 */
Adrian Bunk2b540352007-02-21 11:52:49 +01001534static int is_neg_adv_abort(unsigned int status)
Steve Wiseb038ced2007-02-12 16:16:18 -08001535{
1536 return status == CPL_ERR_RTX_NEG_ADVICE ||
1537 status == CPL_ERR_PERSIST_NEG_ADVICE;
1538}
1539
1540static int peer_abort(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1541{
1542 struct cpl_abort_req_rss *req = cplhdr(skb);
1543 struct iwch_ep *ep = ctx;
1544 struct cpl_abort_rpl *rpl;
1545 struct sk_buff *rpl_skb;
1546 struct iwch_qp_attributes attrs;
1547 int ret;
Steve Wise989a1782008-04-29 13:46:51 -07001548 int release = 0;
1549 unsigned long flags;
Steve Wiseb038ced2007-02-12 16:16:18 -08001550
Steve Wise15803672007-06-19 09:27:48 -05001551 if (is_neg_adv_abort(req->status)) {
Harvey Harrison33718362008-04-16 21:01:10 -07001552 PDBG("%s neg_adv_abort ep %p tid %d\n", __func__, ep,
Steve Wise15803672007-06-19 09:27:48 -05001553 ep->hwtid);
1554 t3_l2t_send_event(ep->com.tdev, ep->l2t);
1555 return CPL_RET_BUF_DONE;
1556 }
1557
Steve Wiseaff9e392007-04-26 15:21:20 -05001558 /*
1559 * We get 2 peer aborts from the HW. The first one must
1560 * be ignored except for scribbling that we need one more.
1561 */
Steve Wise6e47fe42009-09-05 20:22:38 -07001562 if (!test_and_set_bit(PEER_ABORT_IN_PROGRESS, &ep->com.flags)) {
Steve Wiseaff9e392007-04-26 15:21:20 -05001563 return CPL_RET_BUF_DONE;
1564 }
1565
Steve Wise989a1782008-04-29 13:46:51 -07001566 spin_lock_irqsave(&ep->com.lock, flags);
1567 PDBG("%s ep %p state %u\n", __func__, ep, ep->com.state);
1568 switch (ep->com.state) {
Steve Wiseb038ced2007-02-12 16:16:18 -08001569 case CONNECTING:
1570 break;
1571 case MPA_REQ_WAIT:
Steve Wiseadf376b2007-03-06 14:44:01 -06001572 stop_ep_timer(ep);
Steve Wiseb038ced2007-02-12 16:16:18 -08001573 break;
1574 case MPA_REQ_SENT:
Steve Wiseadf376b2007-03-06 14:44:01 -06001575 stop_ep_timer(ep);
Steve Wiseb038ced2007-02-12 16:16:18 -08001576 connect_reply_upcall(ep, -ECONNRESET);
1577 break;
1578 case MPA_REP_SENT:
1579 ep->com.rpl_done = 1;
1580 ep->com.rpl_err = -ECONNRESET;
1581 PDBG("waking up ep %p\n", ep);
1582 wake_up(&ep->com.waitq);
1583 break;
1584 case MPA_REQ_RCVD:
1585
1586 /*
1587 * We're gonna mark this puppy DEAD, but keep
1588 * the reference on it until the ULP accepts or
Steve Wisea52bf982009-09-05 20:22:38 -07001589 * rejects the CR. Also wake up anyone waiting
1590 * in rdma connection migration (see iwch_accept_cr()).
Steve Wiseb038ced2007-02-12 16:16:18 -08001591 */
Steve Wisea52bf982009-09-05 20:22:38 -07001592 ep->com.rpl_done = 1;
1593 ep->com.rpl_err = -ECONNRESET;
1594 PDBG("waking up ep %p\n", ep);
1595 wake_up(&ep->com.waitq);
Steve Wiseb038ced2007-02-12 16:16:18 -08001596 break;
1597 case MORIBUND:
Steve Wiseb038ced2007-02-12 16:16:18 -08001598 case CLOSING:
Steve Wise42e31752007-03-06 14:43:56 -06001599 stop_ep_timer(ep);
1600 /*FALLTHROUGH*/
1601 case FPDU_MODE:
Steve Wiseb038ced2007-02-12 16:16:18 -08001602 if (ep->com.cm_id && ep->com.qp) {
1603 attrs.next_state = IWCH_QP_STATE_ERROR;
1604 ret = iwch_modify_qp(ep->com.qp->rhp,
1605 ep->com.qp, IWCH_QP_ATTR_NEXT_STATE,
1606 &attrs, 1);
1607 if (ret)
1608 printk(KERN_ERR MOD
1609 "%s - qp <- error failed!\n",
Harvey Harrison33718362008-04-16 21:01:10 -07001610 __func__);
Steve Wiseb038ced2007-02-12 16:16:18 -08001611 }
1612 peer_abort_upcall(ep);
1613 break;
1614 case ABORTING:
1615 break;
1616 case DEAD:
Harvey Harrison33718362008-04-16 21:01:10 -07001617 PDBG("%s PEER_ABORT IN DEAD STATE!!!!\n", __func__);
Steve Wise989a1782008-04-29 13:46:51 -07001618 spin_unlock_irqrestore(&ep->com.lock, flags);
Steve Wiseb038ced2007-02-12 16:16:18 -08001619 return CPL_RET_BUF_DONE;
1620 default:
1621 BUG_ON(1);
1622 break;
1623 }
1624 dst_confirm(ep->dst);
Steve Wise989a1782008-04-29 13:46:51 -07001625 if (ep->com.state != ABORTING) {
1626 __state_set(&ep->com, DEAD);
1627 release = 1;
1628 }
1629 spin_unlock_irqrestore(&ep->com.lock, flags);
Steve Wiseb038ced2007-02-12 16:16:18 -08001630
1631 rpl_skb = get_skb(skb, sizeof(*rpl), GFP_KERNEL);
1632 if (!rpl_skb) {
1633 printk(KERN_ERR MOD "%s - cannot allocate skb!\n",
Harvey Harrison33718362008-04-16 21:01:10 -07001634 __func__);
Steve Wise989a1782008-04-29 13:46:51 -07001635 release = 1;
1636 goto out;
Steve Wiseb038ced2007-02-12 16:16:18 -08001637 }
1638 rpl_skb->priority = CPL_PRIORITY_DATA;
1639 rpl = (struct cpl_abort_rpl *) skb_put(rpl_skb, sizeof(*rpl));
1640 rpl->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_HOST_ABORT_CON_RPL));
1641 rpl->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
1642 OPCODE_TID(rpl) = htonl(MK_OPCODE_TID(CPL_ABORT_RPL, ep->hwtid));
1643 rpl->cmd = CPL_ABORT_NO_RST;
Steve Wise04b5d022009-03-30 08:37:56 -07001644 iwch_cxgb3_ofld_send(ep->com.tdev, rpl_skb);
Steve Wise989a1782008-04-29 13:46:51 -07001645out:
1646 if (release)
Steve Wiseb038ced2007-02-12 16:16:18 -08001647 release_ep_resources(ep);
Steve Wiseb038ced2007-02-12 16:16:18 -08001648 return CPL_RET_BUF_DONE;
1649}
1650
1651static int close_con_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1652{
1653 struct iwch_ep *ep = ctx;
1654 struct iwch_qp_attributes attrs;
1655 unsigned long flags;
1656 int release = 0;
1657
Harvey Harrison33718362008-04-16 21:01:10 -07001658 PDBG("%s ep %p\n", __func__, ep);
Steve Wiseb038ced2007-02-12 16:16:18 -08001659 BUG_ON(!ep);
1660
1661 /* The cm_id may be null if we failed to connect */
1662 spin_lock_irqsave(&ep->com.lock, flags);
1663 switch (ep->com.state) {
1664 case CLOSING:
Steve Wiseb038ced2007-02-12 16:16:18 -08001665 __state_set(&ep->com, MORIBUND);
1666 break;
1667 case MORIBUND:
1668 stop_ep_timer(ep);
1669 if ((ep->com.cm_id) && (ep->com.qp)) {
1670 attrs.next_state = IWCH_QP_STATE_IDLE;
1671 iwch_modify_qp(ep->com.qp->rhp,
1672 ep->com.qp,
1673 IWCH_QP_ATTR_NEXT_STATE,
1674 &attrs, 1);
1675 }
1676 close_complete_upcall(ep);
1677 __state_set(&ep->com, DEAD);
1678 release = 1;
1679 break;
Steve Wise42e31752007-03-06 14:43:56 -06001680 case ABORTING:
Steve Wiseb038ced2007-02-12 16:16:18 -08001681 case DEAD:
Steve Wisec4d49772008-05-02 10:57:09 -07001682 break;
Steve Wiseb038ced2007-02-12 16:16:18 -08001683 default:
1684 BUG_ON(1);
1685 break;
1686 }
1687 spin_unlock_irqrestore(&ep->com.lock, flags);
1688 if (release)
1689 release_ep_resources(ep);
1690 return CPL_RET_BUF_DONE;
1691}
1692
1693/*
1694 * T3A does 3 things when a TERM is received:
1695 * 1) send up a CPL_RDMA_TERMINATE message with the TERM packet
1696 * 2) generate an async event on the QP with the TERMINATE opcode
1697 * 3) post a TERMINATE opcde cqe into the associated CQ.
1698 *
1699 * For (1), we save the message in the qp for later consumer consumption.
1700 * For (2), we move the QP into TERMINATE, post a QP event and disconnect.
1701 * For (3), we toss the CQE in cxio_poll_cq().
1702 *
1703 * terminate() handles case (1)...
1704 */
1705static int terminate(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1706{
1707 struct iwch_ep *ep = ctx;
1708
Steve Wise42fb61f2009-02-10 16:38:57 -08001709 if (state_read(&ep->com) != FPDU_MODE)
1710 return CPL_RET_BUF_DONE;
1711
Harvey Harrison33718362008-04-16 21:01:10 -07001712 PDBG("%s ep %p\n", __func__, ep);
Steve Wiseb038ced2007-02-12 16:16:18 -08001713 skb_pull(skb, sizeof(struct cpl_rdma_terminate));
Harvey Harrison33718362008-04-16 21:01:10 -07001714 PDBG("%s saving %d bytes of term msg\n", __func__, skb->len);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001715 skb_copy_from_linear_data(skb, ep->com.qp->attr.terminate_buffer,
1716 skb->len);
Steve Wiseb038ced2007-02-12 16:16:18 -08001717 ep->com.qp->attr.terminate_msg_len = skb->len;
1718 ep->com.qp->attr.is_terminate_local = 0;
1719 return CPL_RET_BUF_DONE;
1720}
1721
1722static int ec_status(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1723{
1724 struct cpl_rdma_ec_status *rep = cplhdr(skb);
1725 struct iwch_ep *ep = ctx;
1726
Harvey Harrison33718362008-04-16 21:01:10 -07001727 PDBG("%s ep %p tid %u status %d\n", __func__, ep, ep->hwtid,
Steve Wiseb038ced2007-02-12 16:16:18 -08001728 rep->status);
1729 if (rep->status) {
1730 struct iwch_qp_attributes attrs;
1731
1732 printk(KERN_ERR MOD "%s BAD CLOSE - Aborting tid %u\n",
Harvey Harrison33718362008-04-16 21:01:10 -07001733 __func__, ep->hwtid);
Steve Wise2f236732007-02-21 14:45:39 -06001734 stop_ep_timer(ep);
Steve Wiseb038ced2007-02-12 16:16:18 -08001735 attrs.next_state = IWCH_QP_STATE_ERROR;
1736 iwch_modify_qp(ep->com.qp->rhp,
1737 ep->com.qp, IWCH_QP_ATTR_NEXT_STATE,
1738 &attrs, 1);
1739 abort_connection(ep, NULL, GFP_KERNEL);
1740 }
1741 return CPL_RET_BUF_DONE;
1742}
1743
1744static void ep_timeout(unsigned long arg)
1745{
1746 struct iwch_ep *ep = (struct iwch_ep *)arg;
1747 struct iwch_qp_attributes attrs;
1748 unsigned long flags;
Steve Wise989a1782008-04-29 13:46:51 -07001749 int abort = 1;
Steve Wiseb038ced2007-02-12 16:16:18 -08001750
1751 spin_lock_irqsave(&ep->com.lock, flags);
Harvey Harrison33718362008-04-16 21:01:10 -07001752 PDBG("%s ep %p tid %u state %d\n", __func__, ep, ep->hwtid,
Steve Wiseb038ced2007-02-12 16:16:18 -08001753 ep->com.state);
1754 switch (ep->com.state) {
1755 case MPA_REQ_SENT:
Steve Wise989a1782008-04-29 13:46:51 -07001756 __state_set(&ep->com, ABORTING);
Steve Wiseb038ced2007-02-12 16:16:18 -08001757 connect_reply_upcall(ep, -ETIMEDOUT);
1758 break;
1759 case MPA_REQ_WAIT:
Steve Wise989a1782008-04-29 13:46:51 -07001760 __state_set(&ep->com, ABORTING);
Steve Wiseb038ced2007-02-12 16:16:18 -08001761 break;
Steve Wise42e31752007-03-06 14:43:56 -06001762 case CLOSING:
Steve Wiseb038ced2007-02-12 16:16:18 -08001763 case MORIBUND:
1764 if (ep->com.cm_id && ep->com.qp) {
1765 attrs.next_state = IWCH_QP_STATE_ERROR;
1766 iwch_modify_qp(ep->com.qp->rhp,
1767 ep->com.qp, IWCH_QP_ATTR_NEXT_STATE,
1768 &attrs, 1);
1769 }
Steve Wise989a1782008-04-29 13:46:51 -07001770 __state_set(&ep->com, ABORTING);
Steve Wiseb038ced2007-02-12 16:16:18 -08001771 break;
1772 default:
Steve Wise989a1782008-04-29 13:46:51 -07001773 printk(KERN_ERR "%s unexpected state ep %p state %u\n",
1774 __func__, ep, ep->com.state);
1775 WARN_ON(1);
1776 abort = 0;
Steve Wiseb038ced2007-02-12 16:16:18 -08001777 }
Steve Wiseb038ced2007-02-12 16:16:18 -08001778 spin_unlock_irqrestore(&ep->com.lock, flags);
Steve Wise989a1782008-04-29 13:46:51 -07001779 if (abort)
1780 abort_connection(ep, NULL, GFP_ATOMIC);
Steve Wiseb038ced2007-02-12 16:16:18 -08001781 put_ep(&ep->com);
1782}
1783
1784int iwch_reject_cr(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
1785{
1786 int err;
1787 struct iwch_ep *ep = to_ep(cm_id);
Harvey Harrison33718362008-04-16 21:01:10 -07001788 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
Steve Wiseb038ced2007-02-12 16:16:18 -08001789
1790 if (state_read(&ep->com) == DEAD) {
1791 put_ep(&ep->com);
1792 return -ECONNRESET;
1793 }
1794 BUG_ON(state_read(&ep->com) != MPA_REQ_RCVD);
Steve Wiseb038ced2007-02-12 16:16:18 -08001795 if (mpa_rev == 0)
1796 abort_connection(ep, NULL, GFP_KERNEL);
1797 else {
1798 err = send_mpa_reject(ep, pdata, pdata_len);
Steve Wise7d526e62007-03-05 17:32:46 -06001799 err = iwch_ep_disconnect(ep, 0, GFP_KERNEL);
Steve Wiseb038ced2007-02-12 16:16:18 -08001800 }
Steve Wise6e47fe42009-09-05 20:22:38 -07001801 put_ep(&ep->com);
Steve Wiseb038ced2007-02-12 16:16:18 -08001802 return 0;
1803}
1804
1805int iwch_accept_cr(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
1806{
1807 int err;
1808 struct iwch_qp_attributes attrs;
1809 enum iwch_qp_attr_mask mask;
1810 struct iwch_ep *ep = to_ep(cm_id);
1811 struct iwch_dev *h = to_iwch_dev(cm_id->device);
1812 struct iwch_qp *qp = get_qhp(h, conn_param->qpn);
1813
Harvey Harrison33718362008-04-16 21:01:10 -07001814 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
Steve Wise6e47fe42009-09-05 20:22:38 -07001815 if (state_read(&ep->com) == DEAD) {
1816 err = -ECONNRESET;
1817 goto err;
1818 }
Steve Wiseb038ced2007-02-12 16:16:18 -08001819
1820 BUG_ON(state_read(&ep->com) != MPA_REQ_RCVD);
1821 BUG_ON(!qp);
1822
1823 if ((conn_param->ord > qp->rhp->attr.max_rdma_read_qp_depth) ||
1824 (conn_param->ird > qp->rhp->attr.max_rdma_reads_per_qp)) {
1825 abort_connection(ep, NULL, GFP_KERNEL);
Steve Wise6e47fe42009-09-05 20:22:38 -07001826 err = -EINVAL;
1827 goto err;
Steve Wiseb038ced2007-02-12 16:16:18 -08001828 }
1829
1830 cm_id->add_ref(cm_id);
1831 ep->com.cm_id = cm_id;
1832 ep->com.qp = qp;
1833
Steve Wiseb038ced2007-02-12 16:16:18 -08001834 ep->ird = conn_param->ird;
1835 ep->ord = conn_param->ord;
Steve Wise96ac7e82009-04-20 13:53:15 -07001836
1837 if (peer2peer && ep->ird == 0)
1838 ep->ird = 1;
1839
Harvey Harrison33718362008-04-16 21:01:10 -07001840 PDBG("%s %d ird %d ord %d\n", __func__, __LINE__, ep->ird, ep->ord);
Steve Wisede3d3532007-05-14 13:27:27 -05001841
Steve Wiseb038ced2007-02-12 16:16:18 -08001842 /* bind QP to EP and move to RTS */
1843 attrs.mpa_attr = ep->mpa_attr;
Roland Dreier1f71f502008-03-28 10:28:17 -07001844 attrs.max_ird = ep->ird;
Steve Wiseb038ced2007-02-12 16:16:18 -08001845 attrs.max_ord = ep->ord;
1846 attrs.llp_stream_handle = ep;
1847 attrs.next_state = IWCH_QP_STATE_RTS;
1848
1849 /* bind QP and TID with INIT_WR */
1850 mask = IWCH_QP_ATTR_NEXT_STATE |
1851 IWCH_QP_ATTR_LLP_STREAM_HANDLE |
1852 IWCH_QP_ATTR_MPA_ATTR |
1853 IWCH_QP_ATTR_MAX_IRD |
1854 IWCH_QP_ATTR_MAX_ORD;
1855
1856 err = iwch_modify_qp(ep->com.qp->rhp,
1857 ep->com.qp, mask, &attrs, 1);
Steve Wisede3d3532007-05-14 13:27:27 -05001858 if (err)
Steve Wise6e47fe42009-09-05 20:22:38 -07001859 goto err1;
Steve Wiseb038ced2007-02-12 16:16:18 -08001860
Steve Wisef8b0dfd2008-04-29 13:46:52 -07001861 /* if needed, wait for wr_ack */
1862 if (iwch_rqes_posted(qp)) {
1863 wait_event(ep->com.waitq, ep->com.rpl_done);
1864 err = ep->com.rpl_err;
1865 if (err)
Steve Wise6e47fe42009-09-05 20:22:38 -07001866 goto err1;
Steve Wisef8b0dfd2008-04-29 13:46:52 -07001867 }
1868
Steve Wisede3d3532007-05-14 13:27:27 -05001869 err = send_mpa_reply(ep, conn_param->private_data,
1870 conn_param->private_data_len);
1871 if (err)
Steve Wise6e47fe42009-09-05 20:22:38 -07001872 goto err1;
Steve Wisede3d3532007-05-14 13:27:27 -05001873
Steve Wisede3d3532007-05-14 13:27:27 -05001874
1875 state_set(&ep->com, FPDU_MODE);
1876 established_upcall(ep);
1877 put_ep(&ep->com);
1878 return 0;
Steve Wise6e47fe42009-09-05 20:22:38 -07001879err1:
Steve Wisede3d3532007-05-14 13:27:27 -05001880 ep->com.cm_id = NULL;
1881 ep->com.qp = NULL;
1882 cm_id->rem_ref(cm_id);
Steve Wise6e47fe42009-09-05 20:22:38 -07001883err:
Steve Wiseb038ced2007-02-12 16:16:18 -08001884 put_ep(&ep->com);
1885 return err;
1886}
1887
Steve Wise8704e9a2008-02-12 16:09:29 -06001888static int is_loopback_dst(struct iw_cm_id *cm_id)
1889{
1890 struct net_device *dev;
1891
1892 dev = ip_dev_find(&init_net, cm_id->remote_addr.sin_addr.s_addr);
1893 if (!dev)
1894 return 0;
1895 dev_put(dev);
1896 return 1;
1897}
1898
Steve Wiseb038ced2007-02-12 16:16:18 -08001899int iwch_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
1900{
1901 int err = 0;
1902 struct iwch_dev *h = to_iwch_dev(cm_id->device);
1903 struct iwch_ep *ep;
1904 struct rtable *rt;
1905
Steve Wise8704e9a2008-02-12 16:09:29 -06001906 if (is_loopback_dst(cm_id)) {
1907 err = -ENOSYS;
1908 goto out;
1909 }
1910
Steve Wiseb038ced2007-02-12 16:16:18 -08001911 ep = alloc_ep(sizeof(*ep), GFP_KERNEL);
1912 if (!ep) {
Harvey Harrison33718362008-04-16 21:01:10 -07001913 printk(KERN_ERR MOD "%s - cannot alloc ep.\n", __func__);
Steve Wiseb038ced2007-02-12 16:16:18 -08001914 err = -ENOMEM;
1915 goto out;
1916 }
1917 init_timer(&ep->timer);
1918 ep->plen = conn_param->private_data_len;
1919 if (ep->plen)
1920 memcpy(ep->mpa_pkt + sizeof(struct mpa_message),
1921 conn_param->private_data, ep->plen);
1922 ep->ird = conn_param->ird;
1923 ep->ord = conn_param->ord;
Steve Wise96ac7e82009-04-20 13:53:15 -07001924
1925 if (peer2peer && ep->ord == 0)
1926 ep->ord = 1;
1927
Steve Wiseb038ced2007-02-12 16:16:18 -08001928 ep->com.tdev = h->rdev.t3cdev_p;
1929
1930 cm_id->add_ref(cm_id);
1931 ep->com.cm_id = cm_id;
1932 ep->com.qp = get_qhp(h, conn_param->qpn);
1933 BUG_ON(!ep->com.qp);
Harvey Harrison33718362008-04-16 21:01:10 -07001934 PDBG("%s qpn 0x%x qp %p cm_id %p\n", __func__, conn_param->qpn,
Steve Wiseb038ced2007-02-12 16:16:18 -08001935 ep->com.qp, cm_id);
1936
1937 /*
1938 * Allocate an active TID to initiate a TCP connection.
1939 */
1940 ep->atid = cxgb3_alloc_atid(h->rdev.t3cdev_p, &t3c_client, ep);
1941 if (ep->atid == -1) {
Harvey Harrison33718362008-04-16 21:01:10 -07001942 printk(KERN_ERR MOD "%s - cannot alloc atid.\n", __func__);
Steve Wiseb038ced2007-02-12 16:16:18 -08001943 err = -ENOMEM;
1944 goto fail2;
1945 }
1946
1947 /* find a route */
1948 rt = find_route(h->rdev.t3cdev_p,
1949 cm_id->local_addr.sin_addr.s_addr,
1950 cm_id->remote_addr.sin_addr.s_addr,
1951 cm_id->local_addr.sin_port,
1952 cm_id->remote_addr.sin_port, IPTOS_LOWDELAY);
1953 if (!rt) {
Harvey Harrison33718362008-04-16 21:01:10 -07001954 printk(KERN_ERR MOD "%s - cannot find route.\n", __func__);
Steve Wiseb038ced2007-02-12 16:16:18 -08001955 err = -EHOSTUNREACH;
1956 goto fail3;
1957 }
1958 ep->dst = &rt->u.dst;
1959
1960 /* get a l2t entry */
1961 ep->l2t = t3_l2t_get(ep->com.tdev, ep->dst->neighbour,
1962 ep->dst->neighbour->dev);
1963 if (!ep->l2t) {
Harvey Harrison33718362008-04-16 21:01:10 -07001964 printk(KERN_ERR MOD "%s - cannot alloc l2e.\n", __func__);
Steve Wiseb038ced2007-02-12 16:16:18 -08001965 err = -ENOMEM;
1966 goto fail4;
1967 }
1968
1969 state_set(&ep->com, CONNECTING);
1970 ep->tos = IPTOS_LOWDELAY;
1971 ep->com.local_addr = cm_id->local_addr;
1972 ep->com.remote_addr = cm_id->remote_addr;
1973
1974 /* send connect request to rnic */
1975 err = send_connect(ep);
1976 if (!err)
1977 goto out;
1978
1979 l2t_release(L2DATA(h->rdev.t3cdev_p), ep->l2t);
1980fail4:
1981 dst_release(ep->dst);
1982fail3:
1983 cxgb3_free_atid(ep->com.tdev, ep->atid);
1984fail2:
Steve Wisedc35fac2008-10-15 10:50:34 -07001985 cm_id->rem_ref(cm_id);
Steve Wiseb038ced2007-02-12 16:16:18 -08001986 put_ep(&ep->com);
1987out:
1988 return err;
1989}
1990
1991int iwch_create_listen(struct iw_cm_id *cm_id, int backlog)
1992{
1993 int err = 0;
1994 struct iwch_dev *h = to_iwch_dev(cm_id->device);
1995 struct iwch_listen_ep *ep;
1996
1997
1998 might_sleep();
1999
2000 ep = alloc_ep(sizeof(*ep), GFP_KERNEL);
2001 if (!ep) {
Harvey Harrison33718362008-04-16 21:01:10 -07002002 printk(KERN_ERR MOD "%s - cannot alloc ep.\n", __func__);
Steve Wiseb038ced2007-02-12 16:16:18 -08002003 err = -ENOMEM;
2004 goto fail1;
2005 }
Harvey Harrison33718362008-04-16 21:01:10 -07002006 PDBG("%s ep %p\n", __func__, ep);
Steve Wiseb038ced2007-02-12 16:16:18 -08002007 ep->com.tdev = h->rdev.t3cdev_p;
2008 cm_id->add_ref(cm_id);
2009 ep->com.cm_id = cm_id;
2010 ep->backlog = backlog;
2011 ep->com.local_addr = cm_id->local_addr;
2012
2013 /*
2014 * Allocate a server TID.
2015 */
2016 ep->stid = cxgb3_alloc_stid(h->rdev.t3cdev_p, &t3c_client, ep);
2017 if (ep->stid == -1) {
Harvey Harrison33718362008-04-16 21:01:10 -07002018 printk(KERN_ERR MOD "%s - cannot alloc atid.\n", __func__);
Steve Wiseb038ced2007-02-12 16:16:18 -08002019 err = -ENOMEM;
2020 goto fail2;
2021 }
2022
2023 state_set(&ep->com, LISTEN);
2024 err = listen_start(ep);
2025 if (err)
2026 goto fail3;
2027
2028 /* wait for pass_open_rpl */
2029 wait_event(ep->com.waitq, ep->com.rpl_done);
2030 err = ep->com.rpl_err;
2031 if (!err) {
2032 cm_id->provider_data = ep;
2033 goto out;
2034 }
2035fail3:
2036 cxgb3_free_stid(ep->com.tdev, ep->stid);
2037fail2:
Steve Wise1b07db72007-07-11 13:04:35 -05002038 cm_id->rem_ref(cm_id);
Steve Wiseb038ced2007-02-12 16:16:18 -08002039 put_ep(&ep->com);
2040fail1:
2041out:
2042 return err;
2043}
2044
2045int iwch_destroy_listen(struct iw_cm_id *cm_id)
2046{
2047 int err;
2048 struct iwch_listen_ep *ep = to_listen_ep(cm_id);
2049
Harvey Harrison33718362008-04-16 21:01:10 -07002050 PDBG("%s ep %p\n", __func__, ep);
Steve Wiseb038ced2007-02-12 16:16:18 -08002051
2052 might_sleep();
2053 state_set(&ep->com, DEAD);
2054 ep->com.rpl_done = 0;
2055 ep->com.rpl_err = 0;
2056 err = listen_stop(ep);
Steve Wise04b5d022009-03-30 08:37:56 -07002057 if (err)
2058 goto done;
Steve Wiseb038ced2007-02-12 16:16:18 -08002059 wait_event(ep->com.waitq, ep->com.rpl_done);
2060 cxgb3_free_stid(ep->com.tdev, ep->stid);
Steve Wise04b5d022009-03-30 08:37:56 -07002061done:
Steve Wiseb038ced2007-02-12 16:16:18 -08002062 err = ep->com.rpl_err;
2063 cm_id->rem_ref(cm_id);
2064 put_ep(&ep->com);
2065 return err;
2066}
2067
2068int iwch_ep_disconnect(struct iwch_ep *ep, int abrupt, gfp_t gfp)
2069{
2070 int ret=0;
2071 unsigned long flags;
2072 int close = 0;
Steve Wise04b5d022009-03-30 08:37:56 -07002073 int fatal = 0;
2074 struct t3cdev *tdev;
2075 struct cxio_rdev *rdev;
Steve Wiseb038ced2007-02-12 16:16:18 -08002076
2077 spin_lock_irqsave(&ep->com.lock, flags);
2078
Harvey Harrison33718362008-04-16 21:01:10 -07002079 PDBG("%s ep %p state %s, abrupt %d\n", __func__, ep,
Steve Wiseb038ced2007-02-12 16:16:18 -08002080 states[ep->com.state], abrupt);
2081
Steve Wise04b5d022009-03-30 08:37:56 -07002082 tdev = (struct t3cdev *)ep->com.tdev;
2083 rdev = (struct cxio_rdev *)tdev->ulp;
2084 if (cxio_fatal_error(rdev)) {
2085 fatal = 1;
2086 close_complete_upcall(ep);
2087 ep->com.state = DEAD;
2088 }
Steve Wiseb038ced2007-02-12 16:16:18 -08002089 switch (ep->com.state) {
2090 case MPA_REQ_WAIT:
2091 case MPA_REQ_SENT:
2092 case MPA_REQ_RCVD:
2093 case MPA_REP_SENT:
2094 case FPDU_MODE:
Steve Wiseb038ced2007-02-12 16:16:18 -08002095 close = 1;
Steve Wise989a1782008-04-29 13:46:51 -07002096 if (abrupt)
2097 ep->com.state = ABORTING;
2098 else {
2099 ep->com.state = CLOSING;
2100 start_ep_timer(ep);
2101 }
Steve Wise6e47fe42009-09-05 20:22:38 -07002102 set_bit(CLOSE_SENT, &ep->com.flags);
Steve Wiseb038ced2007-02-12 16:16:18 -08002103 break;
2104 case CLOSING:
Steve Wise6e47fe42009-09-05 20:22:38 -07002105 if (!test_and_set_bit(CLOSE_SENT, &ep->com.flags)) {
2106 close = 1;
2107 if (abrupt) {
2108 stop_ep_timer(ep);
2109 ep->com.state = ABORTING;
2110 } else
2111 ep->com.state = MORIBUND;
2112 }
Steve Wiseb038ced2007-02-12 16:16:18 -08002113 break;
2114 case MORIBUND:
Steve Wise989a1782008-04-29 13:46:51 -07002115 case ABORTING:
2116 case DEAD:
2117 PDBG("%s ignoring disconnect ep %p state %u\n",
2118 __func__, ep, ep->com.state);
Steve Wiseb038ced2007-02-12 16:16:18 -08002119 break;
2120 default:
2121 BUG();
2122 break;
2123 }
Steve Wise989a1782008-04-29 13:46:51 -07002124
Steve Wiseb038ced2007-02-12 16:16:18 -08002125 spin_unlock_irqrestore(&ep->com.lock, flags);
2126 if (close) {
2127 if (abrupt)
2128 ret = send_abort(ep, NULL, gfp);
2129 else
2130 ret = send_halfclose(ep, gfp);
Steve Wise04b5d022009-03-30 08:37:56 -07002131 if (ret)
2132 fatal = 1;
Steve Wiseb038ced2007-02-12 16:16:18 -08002133 }
Steve Wise04b5d022009-03-30 08:37:56 -07002134 if (fatal)
2135 release_ep_resources(ep);
Steve Wiseb038ced2007-02-12 16:16:18 -08002136 return ret;
2137}
2138
2139int iwch_ep_redirect(void *ctx, struct dst_entry *old, struct dst_entry *new,
2140 struct l2t_entry *l2t)
2141{
2142 struct iwch_ep *ep = ctx;
2143
2144 if (ep->dst != old)
2145 return 0;
2146
Harvey Harrison33718362008-04-16 21:01:10 -07002147 PDBG("%s ep %p redirect to dst %p l2t %p\n", __func__, ep, new,
Steve Wiseb038ced2007-02-12 16:16:18 -08002148 l2t);
2149 dst_hold(new);
2150 l2t_release(L2DATA(ep->com.tdev), ep->l2t);
2151 ep->l2t = l2t;
2152 dst_release(old);
2153 ep->dst = new;
2154 return 1;
2155}
2156
2157/*
2158 * All the CM events are handled on a work queue to have a safe context.
2159 */
2160static int sched(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
2161{
2162 struct iwch_ep_common *epc = ctx;
2163
2164 get_ep(epc);
2165
2166 /*
2167 * Save ctx and tdev in the skb->cb area.
2168 */
2169 *((void **) skb->cb) = ctx;
2170 *((struct t3cdev **) (skb->cb + sizeof(void *))) = tdev;
2171
2172 /*
2173 * Queue the skb and schedule the worker thread.
2174 */
2175 skb_queue_tail(&rxq, skb);
2176 queue_work(workq, &skb_work);
2177 return 0;
2178}
2179
Steve Wise1ca19772007-04-12 07:56:34 -05002180static int set_tcb_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
2181{
2182 struct cpl_set_tcb_rpl *rpl = cplhdr(skb);
2183
2184 if (rpl->status != CPL_ERR_NONE) {
2185 printk(KERN_ERR MOD "Unexpected SET_TCB_RPL status %u "
2186 "for tid %u\n", rpl->status, GET_TID(rpl));
2187 }
2188 return CPL_RET_BUF_DONE;
2189}
2190
Steve Wiseb038ced2007-02-12 16:16:18 -08002191int __init iwch_cm_init(void)
2192{
2193 skb_queue_head_init(&rxq);
2194
2195 workq = create_singlethread_workqueue("iw_cxgb3");
2196 if (!workq)
2197 return -ENOMEM;
2198
2199 /*
2200 * All upcalls from the T3 Core go to sched() to
2201 * schedule the processing on a work queue.
2202 */
2203 t3c_handlers[CPL_ACT_ESTABLISH] = sched;
2204 t3c_handlers[CPL_ACT_OPEN_RPL] = sched;
2205 t3c_handlers[CPL_RX_DATA] = sched;
2206 t3c_handlers[CPL_TX_DMA_ACK] = sched;
2207 t3c_handlers[CPL_ABORT_RPL_RSS] = sched;
2208 t3c_handlers[CPL_ABORT_RPL] = sched;
2209 t3c_handlers[CPL_PASS_OPEN_RPL] = sched;
2210 t3c_handlers[CPL_CLOSE_LISTSRV_RPL] = sched;
2211 t3c_handlers[CPL_PASS_ACCEPT_REQ] = sched;
2212 t3c_handlers[CPL_PASS_ESTABLISH] = sched;
2213 t3c_handlers[CPL_PEER_CLOSE] = sched;
2214 t3c_handlers[CPL_CLOSE_CON_RPL] = sched;
2215 t3c_handlers[CPL_ABORT_REQ_RSS] = sched;
2216 t3c_handlers[CPL_RDMA_TERMINATE] = sched;
2217 t3c_handlers[CPL_RDMA_EC_STATUS] = sched;
Steve Wise1ca19772007-04-12 07:56:34 -05002218 t3c_handlers[CPL_SET_TCB_RPL] = set_tcb_rpl;
Steve Wiseb038ced2007-02-12 16:16:18 -08002219
2220 /*
2221 * These are the real handlers that are called from a
2222 * work queue.
2223 */
2224 work_handlers[CPL_ACT_ESTABLISH] = act_establish;
2225 work_handlers[CPL_ACT_OPEN_RPL] = act_open_rpl;
2226 work_handlers[CPL_RX_DATA] = rx_data;
2227 work_handlers[CPL_TX_DMA_ACK] = tx_ack;
2228 work_handlers[CPL_ABORT_RPL_RSS] = abort_rpl;
2229 work_handlers[CPL_ABORT_RPL] = abort_rpl;
2230 work_handlers[CPL_PASS_OPEN_RPL] = pass_open_rpl;
2231 work_handlers[CPL_CLOSE_LISTSRV_RPL] = close_listsrv_rpl;
2232 work_handlers[CPL_PASS_ACCEPT_REQ] = pass_accept_req;
2233 work_handlers[CPL_PASS_ESTABLISH] = pass_establish;
2234 work_handlers[CPL_PEER_CLOSE] = peer_close;
2235 work_handlers[CPL_ABORT_REQ_RSS] = peer_abort;
2236 work_handlers[CPL_CLOSE_CON_RPL] = close_con_rpl;
2237 work_handlers[CPL_RDMA_TERMINATE] = terminate;
2238 work_handlers[CPL_RDMA_EC_STATUS] = ec_status;
2239 return 0;
2240}
2241
2242void __exit iwch_cm_term(void)
2243{
2244 flush_workqueue(workq);
2245 destroy_workqueue(workq);
2246}