xref: /linux/drivers/infiniband/hw/cxgb4/cm.c (revision b85d45947951d23cb22d90caecf4c1eb81342c96)
1 /*
2  * Copyright (c) 2009-2014 Chelsio, Inc. All rights reserved.
3  *
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>
38 #include <linux/inetdevice.h>
39 #include <linux/ip.h>
40 #include <linux/tcp.h>
41 #include <linux/if_vlan.h>
42 
43 #include <net/neighbour.h>
44 #include <net/netevent.h>
45 #include <net/route.h>
46 #include <net/tcp.h>
47 #include <net/ip6_route.h>
48 #include <net/addrconf.h>
49 
50 #include <rdma/ib_addr.h>
51 
52 #include "iw_cxgb4.h"
53 #include "clip_tbl.h"
54 
55 static char *states[] = {
56 	"idle",
57 	"listen",
58 	"connecting",
59 	"mpa_wait_req",
60 	"mpa_req_sent",
61 	"mpa_req_rcvd",
62 	"mpa_rep_sent",
63 	"fpdu_mode",
64 	"aborting",
65 	"closing",
66 	"moribund",
67 	"dead",
68 	NULL,
69 };
70 
71 static int nocong;
72 module_param(nocong, int, 0644);
73 MODULE_PARM_DESC(nocong, "Turn of congestion control (default=0)");
74 
75 static int enable_ecn;
76 module_param(enable_ecn, int, 0644);
77 MODULE_PARM_DESC(enable_ecn, "Enable ECN (default=0/disabled)");
78 
79 static int dack_mode = 1;
80 module_param(dack_mode, int, 0644);
81 MODULE_PARM_DESC(dack_mode, "Delayed ack mode (default=1)");
82 
83 uint c4iw_max_read_depth = 32;
84 module_param(c4iw_max_read_depth, int, 0644);
85 MODULE_PARM_DESC(c4iw_max_read_depth,
86 		 "Per-connection max ORD/IRD (default=32)");
87 
88 static int enable_tcp_timestamps;
89 module_param(enable_tcp_timestamps, int, 0644);
90 MODULE_PARM_DESC(enable_tcp_timestamps, "Enable tcp timestamps (default=0)");
91 
92 static int enable_tcp_sack;
93 module_param(enable_tcp_sack, int, 0644);
94 MODULE_PARM_DESC(enable_tcp_sack, "Enable tcp SACK (default=0)");
95 
96 static int enable_tcp_window_scaling = 1;
97 module_param(enable_tcp_window_scaling, int, 0644);
98 MODULE_PARM_DESC(enable_tcp_window_scaling,
99 		 "Enable tcp window scaling (default=1)");
100 
101 int c4iw_debug;
102 module_param(c4iw_debug, int, 0644);
103 MODULE_PARM_DESC(c4iw_debug, "Enable debug logging (default=0)");
104 
105 static int peer2peer = 1;
106 module_param(peer2peer, int, 0644);
107 MODULE_PARM_DESC(peer2peer, "Support peer2peer ULPs (default=1)");
108 
109 static int p2p_type = FW_RI_INIT_P2PTYPE_READ_REQ;
110 module_param(p2p_type, int, 0644);
111 MODULE_PARM_DESC(p2p_type, "RDMAP opcode to use for the RTR message: "
112 			   "1=RDMA_READ 0=RDMA_WRITE (default 1)");
113 
114 static int ep_timeout_secs = 60;
115 module_param(ep_timeout_secs, int, 0644);
116 MODULE_PARM_DESC(ep_timeout_secs, "CM Endpoint operation timeout "
117 				   "in seconds (default=60)");
118 
119 static int mpa_rev = 2;
120 module_param(mpa_rev, int, 0644);
121 MODULE_PARM_DESC(mpa_rev, "MPA Revision, 0 supports amso1100, "
122 		"1 is RFC0544 spec compliant, 2 is IETF MPA Peer Connect Draft"
123 		" compliant (default=2)");
124 
125 static int markers_enabled;
126 module_param(markers_enabled, int, 0644);
127 MODULE_PARM_DESC(markers_enabled, "Enable MPA MARKERS (default(0)=disabled)");
128 
129 static int crc_enabled = 1;
130 module_param(crc_enabled, int, 0644);
131 MODULE_PARM_DESC(crc_enabled, "Enable MPA CRC (default(1)=enabled)");
132 
133 static int rcv_win = 256 * 1024;
134 module_param(rcv_win, int, 0644);
135 MODULE_PARM_DESC(rcv_win, "TCP receive window in bytes (default=256KB)");
136 
137 static int snd_win = 128 * 1024;
138 module_param(snd_win, int, 0644);
139 MODULE_PARM_DESC(snd_win, "TCP send window in bytes (default=128KB)");
140 
141 static struct workqueue_struct *workq;
142 
143 static struct sk_buff_head rxq;
144 
145 static struct sk_buff *get_skb(struct sk_buff *skb, int len, gfp_t gfp);
146 static void ep_timeout(unsigned long arg);
147 static void connect_reply_upcall(struct c4iw_ep *ep, int status);
148 
149 static LIST_HEAD(timeout_list);
150 static spinlock_t timeout_lock;
151 
152 static void deref_qp(struct c4iw_ep *ep)
153 {
154 	c4iw_qp_rem_ref(&ep->com.qp->ibqp);
155 	clear_bit(QP_REFERENCED, &ep->com.flags);
156 }
157 
158 static void ref_qp(struct c4iw_ep *ep)
159 {
160 	set_bit(QP_REFERENCED, &ep->com.flags);
161 	c4iw_qp_add_ref(&ep->com.qp->ibqp);
162 }
163 
164 static void start_ep_timer(struct c4iw_ep *ep)
165 {
166 	PDBG("%s ep %p\n", __func__, ep);
167 	if (timer_pending(&ep->timer)) {
168 		pr_err("%s timer already started! ep %p\n",
169 		       __func__, ep);
170 		return;
171 	}
172 	clear_bit(TIMEOUT, &ep->com.flags);
173 	c4iw_get_ep(&ep->com);
174 	ep->timer.expires = jiffies + ep_timeout_secs * HZ;
175 	ep->timer.data = (unsigned long)ep;
176 	ep->timer.function = ep_timeout;
177 	add_timer(&ep->timer);
178 }
179 
180 static int stop_ep_timer(struct c4iw_ep *ep)
181 {
182 	PDBG("%s ep %p stopping\n", __func__, ep);
183 	del_timer_sync(&ep->timer);
184 	if (!test_and_set_bit(TIMEOUT, &ep->com.flags)) {
185 		c4iw_put_ep(&ep->com);
186 		return 0;
187 	}
188 	return 1;
189 }
190 
191 static int c4iw_l2t_send(struct c4iw_rdev *rdev, struct sk_buff *skb,
192 		  struct l2t_entry *l2e)
193 {
194 	int	error = 0;
195 
196 	if (c4iw_fatal_error(rdev)) {
197 		kfree_skb(skb);
198 		PDBG("%s - device in error state - dropping\n", __func__);
199 		return -EIO;
200 	}
201 	error = cxgb4_l2t_send(rdev->lldi.ports[0], skb, l2e);
202 	if (error < 0)
203 		kfree_skb(skb);
204 	return error < 0 ? error : 0;
205 }
206 
207 int c4iw_ofld_send(struct c4iw_rdev *rdev, struct sk_buff *skb)
208 {
209 	int	error = 0;
210 
211 	if (c4iw_fatal_error(rdev)) {
212 		kfree_skb(skb);
213 		PDBG("%s - device in error state - dropping\n", __func__);
214 		return -EIO;
215 	}
216 	error = cxgb4_ofld_send(rdev->lldi.ports[0], skb);
217 	if (error < 0)
218 		kfree_skb(skb);
219 	return error < 0 ? error : 0;
220 }
221 
222 static void release_tid(struct c4iw_rdev *rdev, u32 hwtid, struct sk_buff *skb)
223 {
224 	struct cpl_tid_release *req;
225 
226 	skb = get_skb(skb, sizeof *req, GFP_KERNEL);
227 	if (!skb)
228 		return;
229 	req = (struct cpl_tid_release *) skb_put(skb, sizeof(*req));
230 	INIT_TP_WR(req, hwtid);
231 	OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_TID_RELEASE, hwtid));
232 	set_wr_txq(skb, CPL_PRIORITY_SETUP, 0);
233 	c4iw_ofld_send(rdev, skb);
234 	return;
235 }
236 
237 static void set_emss(struct c4iw_ep *ep, u16 opt)
238 {
239 	ep->emss = ep->com.dev->rdev.lldi.mtus[TCPOPT_MSS_G(opt)] -
240 		   ((AF_INET == ep->com.remote_addr.ss_family) ?
241 		    sizeof(struct iphdr) : sizeof(struct ipv6hdr)) -
242 		   sizeof(struct tcphdr);
243 	ep->mss = ep->emss;
244 	if (TCPOPT_TSTAMP_G(opt))
245 		ep->emss -= round_up(TCPOLEN_TIMESTAMP, 4);
246 	if (ep->emss < 128)
247 		ep->emss = 128;
248 	if (ep->emss & 7)
249 		PDBG("Warning: misaligned mtu idx %u mss %u emss=%u\n",
250 		     TCPOPT_MSS_G(opt), ep->mss, ep->emss);
251 	PDBG("%s mss_idx %u mss %u emss=%u\n", __func__, TCPOPT_MSS_G(opt),
252 	     ep->mss, ep->emss);
253 }
254 
255 static enum c4iw_ep_state state_read(struct c4iw_ep_common *epc)
256 {
257 	enum c4iw_ep_state state;
258 
259 	mutex_lock(&epc->mutex);
260 	state = epc->state;
261 	mutex_unlock(&epc->mutex);
262 	return state;
263 }
264 
265 static void __state_set(struct c4iw_ep_common *epc, enum c4iw_ep_state new)
266 {
267 	epc->state = new;
268 }
269 
270 static void state_set(struct c4iw_ep_common *epc, enum c4iw_ep_state new)
271 {
272 	mutex_lock(&epc->mutex);
273 	PDBG("%s - %s -> %s\n", __func__, states[epc->state], states[new]);
274 	__state_set(epc, new);
275 	mutex_unlock(&epc->mutex);
276 	return;
277 }
278 
279 static void *alloc_ep(int size, gfp_t gfp)
280 {
281 	struct c4iw_ep_common *epc;
282 
283 	epc = kzalloc(size, gfp);
284 	if (epc) {
285 		kref_init(&epc->kref);
286 		mutex_init(&epc->mutex);
287 		c4iw_init_wr_wait(&epc->wr_wait);
288 	}
289 	PDBG("%s alloc ep %p\n", __func__, epc);
290 	return epc;
291 }
292 
293 void _c4iw_free_ep(struct kref *kref)
294 {
295 	struct c4iw_ep *ep;
296 
297 	ep = container_of(kref, struct c4iw_ep, com.kref);
298 	PDBG("%s ep %p state %s\n", __func__, ep, states[state_read(&ep->com)]);
299 	if (test_bit(QP_REFERENCED, &ep->com.flags))
300 		deref_qp(ep);
301 	if (test_bit(RELEASE_RESOURCES, &ep->com.flags)) {
302 		if (ep->com.remote_addr.ss_family == AF_INET6) {
303 			struct sockaddr_in6 *sin6 =
304 					(struct sockaddr_in6 *)
305 					&ep->com.mapped_local_addr;
306 
307 			cxgb4_clip_release(
308 					ep->com.dev->rdev.lldi.ports[0],
309 					(const u32 *)&sin6->sin6_addr.s6_addr,
310 					1);
311 		}
312 		remove_handle(ep->com.dev, &ep->com.dev->hwtid_idr, ep->hwtid);
313 		cxgb4_remove_tid(ep->com.dev->rdev.lldi.tids, 0, ep->hwtid);
314 		dst_release(ep->dst);
315 		cxgb4_l2t_release(ep->l2t);
316 	}
317 	if (test_bit(RELEASE_MAPINFO, &ep->com.flags)) {
318 		print_addr(&ep->com, __func__, "remove_mapinfo/mapping");
319 		iwpm_remove_mapinfo(&ep->com.local_addr,
320 				    &ep->com.mapped_local_addr);
321 		iwpm_remove_mapping(&ep->com.local_addr, RDMA_NL_C4IW);
322 	}
323 	kfree(ep);
324 }
325 
326 static void release_ep_resources(struct c4iw_ep *ep)
327 {
328 	set_bit(RELEASE_RESOURCES, &ep->com.flags);
329 	c4iw_put_ep(&ep->com);
330 }
331 
332 static int status2errno(int status)
333 {
334 	switch (status) {
335 	case CPL_ERR_NONE:
336 		return 0;
337 	case CPL_ERR_CONN_RESET:
338 		return -ECONNRESET;
339 	case CPL_ERR_ARP_MISS:
340 		return -EHOSTUNREACH;
341 	case CPL_ERR_CONN_TIMEDOUT:
342 		return -ETIMEDOUT;
343 	case CPL_ERR_TCAM_FULL:
344 		return -ENOMEM;
345 	case CPL_ERR_CONN_EXIST:
346 		return -EADDRINUSE;
347 	default:
348 		return -EIO;
349 	}
350 }
351 
352 /*
353  * Try and reuse skbs already allocated...
354  */
355 static struct sk_buff *get_skb(struct sk_buff *skb, int len, gfp_t gfp)
356 {
357 	if (skb && !skb_is_nonlinear(skb) && !skb_cloned(skb)) {
358 		skb_trim(skb, 0);
359 		skb_get(skb);
360 		skb_reset_transport_header(skb);
361 	} else {
362 		skb = alloc_skb(len, gfp);
363 	}
364 	t4_set_arp_err_handler(skb, NULL, NULL);
365 	return skb;
366 }
367 
368 static struct net_device *get_real_dev(struct net_device *egress_dev)
369 {
370 	return rdma_vlan_dev_real_dev(egress_dev) ? : egress_dev;
371 }
372 
373 static int our_interface(struct c4iw_dev *dev, struct net_device *egress_dev)
374 {
375 	int i;
376 
377 	egress_dev = get_real_dev(egress_dev);
378 	for (i = 0; i < dev->rdev.lldi.nports; i++)
379 		if (dev->rdev.lldi.ports[i] == egress_dev)
380 			return 1;
381 	return 0;
382 }
383 
384 static struct dst_entry *find_route6(struct c4iw_dev *dev, __u8 *local_ip,
385 				     __u8 *peer_ip, __be16 local_port,
386 				     __be16 peer_port, u8 tos,
387 				     __u32 sin6_scope_id)
388 {
389 	struct dst_entry *dst = NULL;
390 
391 	if (IS_ENABLED(CONFIG_IPV6)) {
392 		struct flowi6 fl6;
393 
394 		memset(&fl6, 0, sizeof(fl6));
395 		memcpy(&fl6.daddr, peer_ip, 16);
396 		memcpy(&fl6.saddr, local_ip, 16);
397 		if (ipv6_addr_type(&fl6.daddr) & IPV6_ADDR_LINKLOCAL)
398 			fl6.flowi6_oif = sin6_scope_id;
399 		dst = ip6_route_output(&init_net, NULL, &fl6);
400 		if (!dst)
401 			goto out;
402 		if (!our_interface(dev, ip6_dst_idev(dst)->dev) &&
403 		    !(ip6_dst_idev(dst)->dev->flags & IFF_LOOPBACK)) {
404 			dst_release(dst);
405 			dst = NULL;
406 		}
407 	}
408 
409 out:
410 	return dst;
411 }
412 
413 static struct dst_entry *find_route(struct c4iw_dev *dev, __be32 local_ip,
414 				 __be32 peer_ip, __be16 local_port,
415 				 __be16 peer_port, u8 tos)
416 {
417 	struct rtable *rt;
418 	struct flowi4 fl4;
419 	struct neighbour *n;
420 
421 	rt = ip_route_output_ports(&init_net, &fl4, NULL, peer_ip, local_ip,
422 				   peer_port, local_port, IPPROTO_TCP,
423 				   tos, 0);
424 	if (IS_ERR(rt))
425 		return NULL;
426 	n = dst_neigh_lookup(&rt->dst, &peer_ip);
427 	if (!n)
428 		return NULL;
429 	if (!our_interface(dev, n->dev) &&
430 	    !(n->dev->flags & IFF_LOOPBACK)) {
431 		neigh_release(n);
432 		dst_release(&rt->dst);
433 		return NULL;
434 	}
435 	neigh_release(n);
436 	return &rt->dst;
437 }
438 
439 static void arp_failure_discard(void *handle, struct sk_buff *skb)
440 {
441 	PDBG("%s c4iw_dev %p\n", __func__, handle);
442 	kfree_skb(skb);
443 }
444 
445 /*
446  * Handle an ARP failure for an active open.
447  */
448 static void act_open_req_arp_failure(void *handle, struct sk_buff *skb)
449 {
450 	struct c4iw_ep *ep = handle;
451 
452 	printk(KERN_ERR MOD "ARP failure duing connect\n");
453 	kfree_skb(skb);
454 	connect_reply_upcall(ep, -EHOSTUNREACH);
455 	state_set(&ep->com, DEAD);
456 	if (ep->com.remote_addr.ss_family == AF_INET6) {
457 		struct sockaddr_in6 *sin6 =
458 			(struct sockaddr_in6 *)&ep->com.mapped_local_addr;
459 		cxgb4_clip_release(ep->com.dev->rdev.lldi.ports[0],
460 				   (const u32 *)&sin6->sin6_addr.s6_addr, 1);
461 	}
462 	remove_handle(ep->com.dev, &ep->com.dev->atid_idr, ep->atid);
463 	cxgb4_free_atid(ep->com.dev->rdev.lldi.tids, ep->atid);
464 	dst_release(ep->dst);
465 	cxgb4_l2t_release(ep->l2t);
466 	c4iw_put_ep(&ep->com);
467 }
468 
469 /*
470  * Handle an ARP failure for a CPL_ABORT_REQ.  Change it into a no RST variant
471  * and send it along.
472  */
473 static void abort_arp_failure(void *handle, struct sk_buff *skb)
474 {
475 	struct c4iw_rdev *rdev = handle;
476 	struct cpl_abort_req *req = cplhdr(skb);
477 
478 	PDBG("%s rdev %p\n", __func__, rdev);
479 	req->cmd = CPL_ABORT_NO_RST;
480 	c4iw_ofld_send(rdev, skb);
481 }
482 
483 static void send_flowc(struct c4iw_ep *ep, struct sk_buff *skb)
484 {
485 	unsigned int flowclen = 80;
486 	struct fw_flowc_wr *flowc;
487 	int i;
488 
489 	skb = get_skb(skb, flowclen, GFP_KERNEL);
490 	flowc = (struct fw_flowc_wr *)__skb_put(skb, flowclen);
491 
492 	flowc->op_to_nparams = cpu_to_be32(FW_WR_OP_V(FW_FLOWC_WR) |
493 					   FW_FLOWC_WR_NPARAMS_V(8));
494 	flowc->flowid_len16 = cpu_to_be32(FW_WR_LEN16_V(DIV_ROUND_UP(flowclen,
495 					  16)) | FW_WR_FLOWID_V(ep->hwtid));
496 
497 	flowc->mnemval[0].mnemonic = FW_FLOWC_MNEM_PFNVFN;
498 	flowc->mnemval[0].val = cpu_to_be32(FW_PFVF_CMD_PFN_V
499 					    (ep->com.dev->rdev.lldi.pf));
500 	flowc->mnemval[1].mnemonic = FW_FLOWC_MNEM_CH;
501 	flowc->mnemval[1].val = cpu_to_be32(ep->tx_chan);
502 	flowc->mnemval[2].mnemonic = FW_FLOWC_MNEM_PORT;
503 	flowc->mnemval[2].val = cpu_to_be32(ep->tx_chan);
504 	flowc->mnemval[3].mnemonic = FW_FLOWC_MNEM_IQID;
505 	flowc->mnemval[3].val = cpu_to_be32(ep->rss_qid);
506 	flowc->mnemval[4].mnemonic = FW_FLOWC_MNEM_SNDNXT;
507 	flowc->mnemval[4].val = cpu_to_be32(ep->snd_seq);
508 	flowc->mnemval[5].mnemonic = FW_FLOWC_MNEM_RCVNXT;
509 	flowc->mnemval[5].val = cpu_to_be32(ep->rcv_seq);
510 	flowc->mnemval[6].mnemonic = FW_FLOWC_MNEM_SNDBUF;
511 	flowc->mnemval[6].val = cpu_to_be32(ep->snd_win);
512 	flowc->mnemval[7].mnemonic = FW_FLOWC_MNEM_MSS;
513 	flowc->mnemval[7].val = cpu_to_be32(ep->emss);
514 	/* Pad WR to 16 byte boundary */
515 	flowc->mnemval[8].mnemonic = 0;
516 	flowc->mnemval[8].val = 0;
517 	for (i = 0; i < 9; i++) {
518 		flowc->mnemval[i].r4[0] = 0;
519 		flowc->mnemval[i].r4[1] = 0;
520 		flowc->mnemval[i].r4[2] = 0;
521 	}
522 
523 	set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
524 	c4iw_ofld_send(&ep->com.dev->rdev, skb);
525 }
526 
527 static int send_halfclose(struct c4iw_ep *ep, gfp_t gfp)
528 {
529 	struct cpl_close_con_req *req;
530 	struct sk_buff *skb;
531 	int wrlen = roundup(sizeof *req, 16);
532 
533 	PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
534 	skb = get_skb(NULL, wrlen, gfp);
535 	if (!skb) {
536 		printk(KERN_ERR MOD "%s - failed to alloc skb\n", __func__);
537 		return -ENOMEM;
538 	}
539 	set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
540 	t4_set_arp_err_handler(skb, NULL, arp_failure_discard);
541 	req = (struct cpl_close_con_req *) skb_put(skb, wrlen);
542 	memset(req, 0, wrlen);
543 	INIT_TP_WR(req, ep->hwtid);
544 	OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_CLOSE_CON_REQ,
545 						    ep->hwtid));
546 	return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
547 }
548 
549 static int send_abort(struct c4iw_ep *ep, struct sk_buff *skb, gfp_t gfp)
550 {
551 	struct cpl_abort_req *req;
552 	int wrlen = roundup(sizeof *req, 16);
553 
554 	PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
555 	skb = get_skb(skb, wrlen, gfp);
556 	if (!skb) {
557 		printk(KERN_ERR MOD "%s - failed to alloc skb.\n",
558 		       __func__);
559 		return -ENOMEM;
560 	}
561 	set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
562 	t4_set_arp_err_handler(skb, &ep->com.dev->rdev, abort_arp_failure);
563 	req = (struct cpl_abort_req *) skb_put(skb, wrlen);
564 	memset(req, 0, wrlen);
565 	INIT_TP_WR(req, ep->hwtid);
566 	OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_ABORT_REQ, ep->hwtid));
567 	req->cmd = CPL_ABORT_SEND_RST;
568 	return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
569 }
570 
571 /*
572  * c4iw_form_pm_msg - Form a port mapper message with mapping info
573  */
574 static void c4iw_form_pm_msg(struct c4iw_ep *ep,
575 				struct iwpm_sa_data *pm_msg)
576 {
577 	memcpy(&pm_msg->loc_addr, &ep->com.local_addr,
578 		sizeof(ep->com.local_addr));
579 	memcpy(&pm_msg->rem_addr, &ep->com.remote_addr,
580 		sizeof(ep->com.remote_addr));
581 }
582 
583 /*
584  * c4iw_form_reg_msg - Form a port mapper message with dev info
585  */
586 static void c4iw_form_reg_msg(struct c4iw_dev *dev,
587 				struct iwpm_dev_data *pm_msg)
588 {
589 	memcpy(pm_msg->dev_name, dev->ibdev.name, IWPM_DEVNAME_SIZE);
590 	memcpy(pm_msg->if_name, dev->rdev.lldi.ports[0]->name,
591 				IWPM_IFNAME_SIZE);
592 }
593 
594 static void c4iw_record_pm_msg(struct c4iw_ep *ep,
595 			struct iwpm_sa_data *pm_msg)
596 {
597 	memcpy(&ep->com.mapped_local_addr, &pm_msg->mapped_loc_addr,
598 		sizeof(ep->com.mapped_local_addr));
599 	memcpy(&ep->com.mapped_remote_addr, &pm_msg->mapped_rem_addr,
600 		sizeof(ep->com.mapped_remote_addr));
601 }
602 
603 static int get_remote_addr(struct c4iw_ep *parent_ep, struct c4iw_ep *child_ep)
604 {
605 	int ret;
606 
607 	print_addr(&parent_ep->com, __func__, "get_remote_addr parent_ep ");
608 	print_addr(&child_ep->com, __func__, "get_remote_addr child_ep ");
609 
610 	ret = iwpm_get_remote_info(&parent_ep->com.mapped_local_addr,
611 				   &child_ep->com.mapped_remote_addr,
612 				   &child_ep->com.remote_addr, RDMA_NL_C4IW);
613 	if (ret)
614 		PDBG("Unable to find remote peer addr info - err %d\n", ret);
615 
616 	return ret;
617 }
618 
619 static void best_mtu(const unsigned short *mtus, unsigned short mtu,
620 		     unsigned int *idx, int use_ts, int ipv6)
621 {
622 	unsigned short hdr_size = (ipv6 ?
623 				   sizeof(struct ipv6hdr) :
624 				   sizeof(struct iphdr)) +
625 				  sizeof(struct tcphdr) +
626 				  (use_ts ?
627 				   round_up(TCPOLEN_TIMESTAMP, 4) : 0);
628 	unsigned short data_size = mtu - hdr_size;
629 
630 	cxgb4_best_aligned_mtu(mtus, hdr_size, data_size, 8, idx);
631 }
632 
633 static int send_connect(struct c4iw_ep *ep)
634 {
635 	struct cpl_act_open_req *req;
636 	struct cpl_t5_act_open_req *t5_req;
637 	struct cpl_act_open_req6 *req6;
638 	struct cpl_t5_act_open_req6 *t5_req6;
639 	struct sk_buff *skb;
640 	u64 opt0;
641 	u32 opt2;
642 	unsigned int mtu_idx;
643 	int wscale;
644 	int wrlen;
645 	int sizev4 = is_t4(ep->com.dev->rdev.lldi.adapter_type) ?
646 				sizeof(struct cpl_act_open_req) :
647 				sizeof(struct cpl_t5_act_open_req);
648 	int sizev6 = is_t4(ep->com.dev->rdev.lldi.adapter_type) ?
649 				sizeof(struct cpl_act_open_req6) :
650 				sizeof(struct cpl_t5_act_open_req6);
651 	struct sockaddr_in *la = (struct sockaddr_in *)
652 				 &ep->com.mapped_local_addr;
653 	struct sockaddr_in *ra = (struct sockaddr_in *)
654 				 &ep->com.mapped_remote_addr;
655 	struct sockaddr_in6 *la6 = (struct sockaddr_in6 *)
656 				   &ep->com.mapped_local_addr;
657 	struct sockaddr_in6 *ra6 = (struct sockaddr_in6 *)
658 				   &ep->com.mapped_remote_addr;
659 	int win;
660 	int ret;
661 
662 	wrlen = (ep->com.remote_addr.ss_family == AF_INET) ?
663 			roundup(sizev4, 16) :
664 			roundup(sizev6, 16);
665 
666 	PDBG("%s ep %p atid %u\n", __func__, ep, ep->atid);
667 
668 	skb = get_skb(NULL, wrlen, GFP_KERNEL);
669 	if (!skb) {
670 		printk(KERN_ERR MOD "%s - failed to alloc skb.\n",
671 		       __func__);
672 		return -ENOMEM;
673 	}
674 	set_wr_txq(skb, CPL_PRIORITY_SETUP, ep->ctrlq_idx);
675 
676 	best_mtu(ep->com.dev->rdev.lldi.mtus, ep->mtu, &mtu_idx,
677 		 enable_tcp_timestamps,
678 		 (AF_INET == ep->com.remote_addr.ss_family) ? 0 : 1);
679 	wscale = compute_wscale(rcv_win);
680 
681 	/*
682 	 * Specify the largest window that will fit in opt0. The
683 	 * remainder will be specified in the rx_data_ack.
684 	 */
685 	win = ep->rcv_win >> 10;
686 	if (win > RCV_BUFSIZ_M)
687 		win = RCV_BUFSIZ_M;
688 
689 	opt0 = (nocong ? NO_CONG_F : 0) |
690 	       KEEP_ALIVE_F |
691 	       DELACK_F |
692 	       WND_SCALE_V(wscale) |
693 	       MSS_IDX_V(mtu_idx) |
694 	       L2T_IDX_V(ep->l2t->idx) |
695 	       TX_CHAN_V(ep->tx_chan) |
696 	       SMAC_SEL_V(ep->smac_idx) |
697 	       DSCP_V(ep->tos) |
698 	       ULP_MODE_V(ULP_MODE_TCPDDP) |
699 	       RCV_BUFSIZ_V(win);
700 	opt2 = RX_CHANNEL_V(0) |
701 	       CCTRL_ECN_V(enable_ecn) |
702 	       RSS_QUEUE_VALID_F | RSS_QUEUE_V(ep->rss_qid);
703 	if (enable_tcp_timestamps)
704 		opt2 |= TSTAMPS_EN_F;
705 	if (enable_tcp_sack)
706 		opt2 |= SACK_EN_F;
707 	if (wscale && enable_tcp_window_scaling)
708 		opt2 |= WND_SCALE_EN_F;
709 	if (is_t5(ep->com.dev->rdev.lldi.adapter_type)) {
710 		opt2 |= T5_OPT_2_VALID_F;
711 		opt2 |= CONG_CNTRL_V(CONG_ALG_TAHOE);
712 		opt2 |= T5_ISS_F;
713 	}
714 
715 	if (ep->com.remote_addr.ss_family == AF_INET6)
716 		cxgb4_clip_get(ep->com.dev->rdev.lldi.ports[0],
717 			       (const u32 *)&la6->sin6_addr.s6_addr, 1);
718 
719 	t4_set_arp_err_handler(skb, ep, act_open_req_arp_failure);
720 
721 	if (is_t4(ep->com.dev->rdev.lldi.adapter_type)) {
722 		if (ep->com.remote_addr.ss_family == AF_INET) {
723 			req = (struct cpl_act_open_req *) skb_put(skb, wrlen);
724 			INIT_TP_WR(req, 0);
725 			OPCODE_TID(req) = cpu_to_be32(
726 					MK_OPCODE_TID(CPL_ACT_OPEN_REQ,
727 					((ep->rss_qid << 14) | ep->atid)));
728 			req->local_port = la->sin_port;
729 			req->peer_port = ra->sin_port;
730 			req->local_ip = la->sin_addr.s_addr;
731 			req->peer_ip = ra->sin_addr.s_addr;
732 			req->opt0 = cpu_to_be64(opt0);
733 			req->params = cpu_to_be32(cxgb4_select_ntuple(
734 						ep->com.dev->rdev.lldi.ports[0],
735 						ep->l2t));
736 			req->opt2 = cpu_to_be32(opt2);
737 		} else {
738 			req6 = (struct cpl_act_open_req6 *)skb_put(skb, wrlen);
739 
740 			INIT_TP_WR(req6, 0);
741 			OPCODE_TID(req6) = cpu_to_be32(
742 					   MK_OPCODE_TID(CPL_ACT_OPEN_REQ6,
743 					   ((ep->rss_qid<<14)|ep->atid)));
744 			req6->local_port = la6->sin6_port;
745 			req6->peer_port = ra6->sin6_port;
746 			req6->local_ip_hi = *((__be64 *)
747 						(la6->sin6_addr.s6_addr));
748 			req6->local_ip_lo = *((__be64 *)
749 						(la6->sin6_addr.s6_addr + 8));
750 			req6->peer_ip_hi = *((__be64 *)
751 						(ra6->sin6_addr.s6_addr));
752 			req6->peer_ip_lo = *((__be64 *)
753 						(ra6->sin6_addr.s6_addr + 8));
754 			req6->opt0 = cpu_to_be64(opt0);
755 			req6->params = cpu_to_be32(cxgb4_select_ntuple(
756 						ep->com.dev->rdev.lldi.ports[0],
757 						ep->l2t));
758 			req6->opt2 = cpu_to_be32(opt2);
759 		}
760 	} else {
761 		u32 isn = (prandom_u32() & ~7UL) - 1;
762 
763 		if (peer2peer)
764 			isn += 4;
765 
766 		if (ep->com.remote_addr.ss_family == AF_INET) {
767 			t5_req = (struct cpl_t5_act_open_req *)
768 				 skb_put(skb, wrlen);
769 			INIT_TP_WR(t5_req, 0);
770 			OPCODE_TID(t5_req) = cpu_to_be32(
771 					MK_OPCODE_TID(CPL_ACT_OPEN_REQ,
772 					((ep->rss_qid << 14) | ep->atid)));
773 			t5_req->local_port = la->sin_port;
774 			t5_req->peer_port = ra->sin_port;
775 			t5_req->local_ip = la->sin_addr.s_addr;
776 			t5_req->peer_ip = ra->sin_addr.s_addr;
777 			t5_req->opt0 = cpu_to_be64(opt0);
778 			t5_req->params = cpu_to_be64(FILTER_TUPLE_V(
779 						     cxgb4_select_ntuple(
780 					     ep->com.dev->rdev.lldi.ports[0],
781 					     ep->l2t)));
782 			t5_req->rsvd = cpu_to_be32(isn);
783 			PDBG("%s snd_isn %u\n", __func__,
784 			     be32_to_cpu(t5_req->rsvd));
785 			t5_req->opt2 = cpu_to_be32(opt2);
786 		} else {
787 			t5_req6 = (struct cpl_t5_act_open_req6 *)
788 				  skb_put(skb, wrlen);
789 			INIT_TP_WR(t5_req6, 0);
790 			OPCODE_TID(t5_req6) = cpu_to_be32(
791 					      MK_OPCODE_TID(CPL_ACT_OPEN_REQ6,
792 					      ((ep->rss_qid<<14)|ep->atid)));
793 			t5_req6->local_port = la6->sin6_port;
794 			t5_req6->peer_port = ra6->sin6_port;
795 			t5_req6->local_ip_hi = *((__be64 *)
796 						(la6->sin6_addr.s6_addr));
797 			t5_req6->local_ip_lo = *((__be64 *)
798 						(la6->sin6_addr.s6_addr + 8));
799 			t5_req6->peer_ip_hi = *((__be64 *)
800 						(ra6->sin6_addr.s6_addr));
801 			t5_req6->peer_ip_lo = *((__be64 *)
802 						(ra6->sin6_addr.s6_addr + 8));
803 			t5_req6->opt0 = cpu_to_be64(opt0);
804 			t5_req6->params = cpu_to_be64(FILTER_TUPLE_V(
805 							cxgb4_select_ntuple(
806 						ep->com.dev->rdev.lldi.ports[0],
807 						ep->l2t)));
808 			t5_req6->rsvd = cpu_to_be32(isn);
809 			PDBG("%s snd_isn %u\n", __func__,
810 			     be32_to_cpu(t5_req6->rsvd));
811 			t5_req6->opt2 = cpu_to_be32(opt2);
812 		}
813 	}
814 
815 	set_bit(ACT_OPEN_REQ, &ep->com.history);
816 	ret = c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
817 	if (ret && ep->com.remote_addr.ss_family == AF_INET6)
818 		cxgb4_clip_release(ep->com.dev->rdev.lldi.ports[0],
819 				   (const u32 *)&la6->sin6_addr.s6_addr, 1);
820 	return ret;
821 }
822 
823 static void send_mpa_req(struct c4iw_ep *ep, struct sk_buff *skb,
824 		u8 mpa_rev_to_use)
825 {
826 	int mpalen, wrlen;
827 	struct fw_ofld_tx_data_wr *req;
828 	struct mpa_message *mpa;
829 	struct mpa_v2_conn_params mpa_v2_params;
830 
831 	PDBG("%s ep %p tid %u pd_len %d\n", __func__, ep, ep->hwtid, ep->plen);
832 
833 	BUG_ON(skb_cloned(skb));
834 
835 	mpalen = sizeof(*mpa) + ep->plen;
836 	if (mpa_rev_to_use == 2)
837 		mpalen += sizeof(struct mpa_v2_conn_params);
838 	wrlen = roundup(mpalen + sizeof *req, 16);
839 	skb = get_skb(skb, wrlen, GFP_KERNEL);
840 	if (!skb) {
841 		connect_reply_upcall(ep, -ENOMEM);
842 		return;
843 	}
844 	set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
845 
846 	req = (struct fw_ofld_tx_data_wr *)skb_put(skb, wrlen);
847 	memset(req, 0, wrlen);
848 	req->op_to_immdlen = cpu_to_be32(
849 		FW_WR_OP_V(FW_OFLD_TX_DATA_WR) |
850 		FW_WR_COMPL_F |
851 		FW_WR_IMMDLEN_V(mpalen));
852 	req->flowid_len16 = cpu_to_be32(
853 		FW_WR_FLOWID_V(ep->hwtid) |
854 		FW_WR_LEN16_V(wrlen >> 4));
855 	req->plen = cpu_to_be32(mpalen);
856 	req->tunnel_to_proxy = cpu_to_be32(
857 		FW_OFLD_TX_DATA_WR_FLUSH_F |
858 		FW_OFLD_TX_DATA_WR_SHOVE_F);
859 
860 	mpa = (struct mpa_message *)(req + 1);
861 	memcpy(mpa->key, MPA_KEY_REQ, sizeof(mpa->key));
862 	mpa->flags = (crc_enabled ? MPA_CRC : 0) |
863 		     (markers_enabled ? MPA_MARKERS : 0) |
864 		     (mpa_rev_to_use == 2 ? MPA_ENHANCED_RDMA_CONN : 0);
865 	mpa->private_data_size = htons(ep->plen);
866 	mpa->revision = mpa_rev_to_use;
867 	if (mpa_rev_to_use == 1) {
868 		ep->tried_with_mpa_v1 = 1;
869 		ep->retry_with_mpa_v1 = 0;
870 	}
871 
872 	if (mpa_rev_to_use == 2) {
873 		mpa->private_data_size = htons(ntohs(mpa->private_data_size) +
874 					       sizeof (struct mpa_v2_conn_params));
875 		PDBG("%s initiator ird %u ord %u\n", __func__, ep->ird,
876 		     ep->ord);
877 		mpa_v2_params.ird = htons((u16)ep->ird);
878 		mpa_v2_params.ord = htons((u16)ep->ord);
879 
880 		if (peer2peer) {
881 			mpa_v2_params.ird |= htons(MPA_V2_PEER2PEER_MODEL);
882 			if (p2p_type == FW_RI_INIT_P2PTYPE_RDMA_WRITE)
883 				mpa_v2_params.ord |=
884 					htons(MPA_V2_RDMA_WRITE_RTR);
885 			else if (p2p_type == FW_RI_INIT_P2PTYPE_READ_REQ)
886 				mpa_v2_params.ord |=
887 					htons(MPA_V2_RDMA_READ_RTR);
888 		}
889 		memcpy(mpa->private_data, &mpa_v2_params,
890 		       sizeof(struct mpa_v2_conn_params));
891 
892 		if (ep->plen)
893 			memcpy(mpa->private_data +
894 			       sizeof(struct mpa_v2_conn_params),
895 			       ep->mpa_pkt + sizeof(*mpa), ep->plen);
896 	} else
897 		if (ep->plen)
898 			memcpy(mpa->private_data,
899 					ep->mpa_pkt + sizeof(*mpa), ep->plen);
900 
901 	/*
902 	 * Reference the mpa skb.  This ensures the data area
903 	 * will remain in memory until the hw acks the tx.
904 	 * Function fw4_ack() will deref it.
905 	 */
906 	skb_get(skb);
907 	t4_set_arp_err_handler(skb, NULL, arp_failure_discard);
908 	BUG_ON(ep->mpa_skb);
909 	ep->mpa_skb = skb;
910 	c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
911 	start_ep_timer(ep);
912 	__state_set(&ep->com, MPA_REQ_SENT);
913 	ep->mpa_attr.initiator = 1;
914 	ep->snd_seq += mpalen;
915 	return;
916 }
917 
918 static int send_mpa_reject(struct c4iw_ep *ep, const void *pdata, u8 plen)
919 {
920 	int mpalen, wrlen;
921 	struct fw_ofld_tx_data_wr *req;
922 	struct mpa_message *mpa;
923 	struct sk_buff *skb;
924 	struct mpa_v2_conn_params mpa_v2_params;
925 
926 	PDBG("%s ep %p tid %u pd_len %d\n", __func__, ep, ep->hwtid, ep->plen);
927 
928 	mpalen = sizeof(*mpa) + plen;
929 	if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn)
930 		mpalen += sizeof(struct mpa_v2_conn_params);
931 	wrlen = roundup(mpalen + sizeof *req, 16);
932 
933 	skb = get_skb(NULL, wrlen, GFP_KERNEL);
934 	if (!skb) {
935 		printk(KERN_ERR MOD "%s - cannot alloc skb!\n", __func__);
936 		return -ENOMEM;
937 	}
938 	set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
939 
940 	req = (struct fw_ofld_tx_data_wr *)skb_put(skb, wrlen);
941 	memset(req, 0, wrlen);
942 	req->op_to_immdlen = cpu_to_be32(
943 		FW_WR_OP_V(FW_OFLD_TX_DATA_WR) |
944 		FW_WR_COMPL_F |
945 		FW_WR_IMMDLEN_V(mpalen));
946 	req->flowid_len16 = cpu_to_be32(
947 		FW_WR_FLOWID_V(ep->hwtid) |
948 		FW_WR_LEN16_V(wrlen >> 4));
949 	req->plen = cpu_to_be32(mpalen);
950 	req->tunnel_to_proxy = cpu_to_be32(
951 		FW_OFLD_TX_DATA_WR_FLUSH_F |
952 		FW_OFLD_TX_DATA_WR_SHOVE_F);
953 
954 	mpa = (struct mpa_message *)(req + 1);
955 	memset(mpa, 0, sizeof(*mpa));
956 	memcpy(mpa->key, MPA_KEY_REP, sizeof(mpa->key));
957 	mpa->flags = MPA_REJECT;
958 	mpa->revision = ep->mpa_attr.version;
959 	mpa->private_data_size = htons(plen);
960 
961 	if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn) {
962 		mpa->flags |= MPA_ENHANCED_RDMA_CONN;
963 		mpa->private_data_size = htons(ntohs(mpa->private_data_size) +
964 					       sizeof (struct mpa_v2_conn_params));
965 		mpa_v2_params.ird = htons(((u16)ep->ird) |
966 					  (peer2peer ? MPA_V2_PEER2PEER_MODEL :
967 					   0));
968 		mpa_v2_params.ord = htons(((u16)ep->ord) | (peer2peer ?
969 					  (p2p_type ==
970 					   FW_RI_INIT_P2PTYPE_RDMA_WRITE ?
971 					   MPA_V2_RDMA_WRITE_RTR : p2p_type ==
972 					   FW_RI_INIT_P2PTYPE_READ_REQ ?
973 					   MPA_V2_RDMA_READ_RTR : 0) : 0));
974 		memcpy(mpa->private_data, &mpa_v2_params,
975 		       sizeof(struct mpa_v2_conn_params));
976 
977 		if (ep->plen)
978 			memcpy(mpa->private_data +
979 			       sizeof(struct mpa_v2_conn_params), pdata, plen);
980 	} else
981 		if (plen)
982 			memcpy(mpa->private_data, pdata, plen);
983 
984 	/*
985 	 * Reference the mpa skb again.  This ensures the data area
986 	 * will remain in memory until the hw acks the tx.
987 	 * Function fw4_ack() will deref it.
988 	 */
989 	skb_get(skb);
990 	set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
991 	t4_set_arp_err_handler(skb, NULL, arp_failure_discard);
992 	BUG_ON(ep->mpa_skb);
993 	ep->mpa_skb = skb;
994 	ep->snd_seq += mpalen;
995 	return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
996 }
997 
998 static int send_mpa_reply(struct c4iw_ep *ep, const void *pdata, u8 plen)
999 {
1000 	int mpalen, wrlen;
1001 	struct fw_ofld_tx_data_wr *req;
1002 	struct mpa_message *mpa;
1003 	struct sk_buff *skb;
1004 	struct mpa_v2_conn_params mpa_v2_params;
1005 
1006 	PDBG("%s ep %p tid %u pd_len %d\n", __func__, ep, ep->hwtid, ep->plen);
1007 
1008 	mpalen = sizeof(*mpa) + plen;
1009 	if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn)
1010 		mpalen += sizeof(struct mpa_v2_conn_params);
1011 	wrlen = roundup(mpalen + sizeof *req, 16);
1012 
1013 	skb = get_skb(NULL, wrlen, GFP_KERNEL);
1014 	if (!skb) {
1015 		printk(KERN_ERR MOD "%s - cannot alloc skb!\n", __func__);
1016 		return -ENOMEM;
1017 	}
1018 	set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
1019 
1020 	req = (struct fw_ofld_tx_data_wr *) skb_put(skb, wrlen);
1021 	memset(req, 0, wrlen);
1022 	req->op_to_immdlen = cpu_to_be32(
1023 		FW_WR_OP_V(FW_OFLD_TX_DATA_WR) |
1024 		FW_WR_COMPL_F |
1025 		FW_WR_IMMDLEN_V(mpalen));
1026 	req->flowid_len16 = cpu_to_be32(
1027 		FW_WR_FLOWID_V(ep->hwtid) |
1028 		FW_WR_LEN16_V(wrlen >> 4));
1029 	req->plen = cpu_to_be32(mpalen);
1030 	req->tunnel_to_proxy = cpu_to_be32(
1031 		FW_OFLD_TX_DATA_WR_FLUSH_F |
1032 		FW_OFLD_TX_DATA_WR_SHOVE_F);
1033 
1034 	mpa = (struct mpa_message *)(req + 1);
1035 	memset(mpa, 0, sizeof(*mpa));
1036 	memcpy(mpa->key, MPA_KEY_REP, sizeof(mpa->key));
1037 	mpa->flags = (ep->mpa_attr.crc_enabled ? MPA_CRC : 0) |
1038 		     (markers_enabled ? MPA_MARKERS : 0);
1039 	mpa->revision = ep->mpa_attr.version;
1040 	mpa->private_data_size = htons(plen);
1041 
1042 	if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn) {
1043 		mpa->flags |= MPA_ENHANCED_RDMA_CONN;
1044 		mpa->private_data_size = htons(ntohs(mpa->private_data_size) +
1045 					       sizeof (struct mpa_v2_conn_params));
1046 		mpa_v2_params.ird = htons((u16)ep->ird);
1047 		mpa_v2_params.ord = htons((u16)ep->ord);
1048 		if (peer2peer && (ep->mpa_attr.p2p_type !=
1049 					FW_RI_INIT_P2PTYPE_DISABLED)) {
1050 			mpa_v2_params.ird |= htons(MPA_V2_PEER2PEER_MODEL);
1051 
1052 			if (p2p_type == FW_RI_INIT_P2PTYPE_RDMA_WRITE)
1053 				mpa_v2_params.ord |=
1054 					htons(MPA_V2_RDMA_WRITE_RTR);
1055 			else if (p2p_type == FW_RI_INIT_P2PTYPE_READ_REQ)
1056 				mpa_v2_params.ord |=
1057 					htons(MPA_V2_RDMA_READ_RTR);
1058 		}
1059 
1060 		memcpy(mpa->private_data, &mpa_v2_params,
1061 		       sizeof(struct mpa_v2_conn_params));
1062 
1063 		if (ep->plen)
1064 			memcpy(mpa->private_data +
1065 			       sizeof(struct mpa_v2_conn_params), pdata, plen);
1066 	} else
1067 		if (plen)
1068 			memcpy(mpa->private_data, pdata, plen);
1069 
1070 	/*
1071 	 * Reference the mpa skb.  This ensures the data area
1072 	 * will remain in memory until the hw acks the tx.
1073 	 * Function fw4_ack() will deref it.
1074 	 */
1075 	skb_get(skb);
1076 	t4_set_arp_err_handler(skb, NULL, arp_failure_discard);
1077 	ep->mpa_skb = skb;
1078 	__state_set(&ep->com, MPA_REP_SENT);
1079 	ep->snd_seq += mpalen;
1080 	return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
1081 }
1082 
1083 static int act_establish(struct c4iw_dev *dev, struct sk_buff *skb)
1084 {
1085 	struct c4iw_ep *ep;
1086 	struct cpl_act_establish *req = cplhdr(skb);
1087 	unsigned int tid = GET_TID(req);
1088 	unsigned int atid = TID_TID_G(ntohl(req->tos_atid));
1089 	struct tid_info *t = dev->rdev.lldi.tids;
1090 
1091 	ep = lookup_atid(t, atid);
1092 
1093 	PDBG("%s ep %p tid %u snd_isn %u rcv_isn %u\n", __func__, ep, tid,
1094 	     be32_to_cpu(req->snd_isn), be32_to_cpu(req->rcv_isn));
1095 
1096 	mutex_lock(&ep->com.mutex);
1097 	dst_confirm(ep->dst);
1098 
1099 	/* setup the hwtid for this connection */
1100 	ep->hwtid = tid;
1101 	cxgb4_insert_tid(t, ep, tid);
1102 	insert_handle(dev, &dev->hwtid_idr, ep, ep->hwtid);
1103 
1104 	ep->snd_seq = be32_to_cpu(req->snd_isn);
1105 	ep->rcv_seq = be32_to_cpu(req->rcv_isn);
1106 
1107 	set_emss(ep, ntohs(req->tcp_opt));
1108 
1109 	/* dealloc the atid */
1110 	remove_handle(ep->com.dev, &ep->com.dev->atid_idr, atid);
1111 	cxgb4_free_atid(t, atid);
1112 	set_bit(ACT_ESTAB, &ep->com.history);
1113 
1114 	/* start MPA negotiation */
1115 	send_flowc(ep, NULL);
1116 	if (ep->retry_with_mpa_v1)
1117 		send_mpa_req(ep, skb, 1);
1118 	else
1119 		send_mpa_req(ep, skb, mpa_rev);
1120 	mutex_unlock(&ep->com.mutex);
1121 	return 0;
1122 }
1123 
1124 static void close_complete_upcall(struct c4iw_ep *ep, int status)
1125 {
1126 	struct iw_cm_event event;
1127 
1128 	PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1129 	memset(&event, 0, sizeof(event));
1130 	event.event = IW_CM_EVENT_CLOSE;
1131 	event.status = status;
1132 	if (ep->com.cm_id) {
1133 		PDBG("close complete delivered ep %p cm_id %p tid %u\n",
1134 		     ep, ep->com.cm_id, ep->hwtid);
1135 		ep->com.cm_id->event_handler(ep->com.cm_id, &event);
1136 		ep->com.cm_id->rem_ref(ep->com.cm_id);
1137 		ep->com.cm_id = NULL;
1138 		set_bit(CLOSE_UPCALL, &ep->com.history);
1139 	}
1140 }
1141 
1142 static int abort_connection(struct c4iw_ep *ep, struct sk_buff *skb, gfp_t gfp)
1143 {
1144 	PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1145 	__state_set(&ep->com, ABORTING);
1146 	set_bit(ABORT_CONN, &ep->com.history);
1147 	return send_abort(ep, skb, gfp);
1148 }
1149 
1150 static void peer_close_upcall(struct c4iw_ep *ep)
1151 {
1152 	struct iw_cm_event event;
1153 
1154 	PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1155 	memset(&event, 0, sizeof(event));
1156 	event.event = IW_CM_EVENT_DISCONNECT;
1157 	if (ep->com.cm_id) {
1158 		PDBG("peer close delivered ep %p cm_id %p tid %u\n",
1159 		     ep, ep->com.cm_id, ep->hwtid);
1160 		ep->com.cm_id->event_handler(ep->com.cm_id, &event);
1161 		set_bit(DISCONN_UPCALL, &ep->com.history);
1162 	}
1163 }
1164 
1165 static void peer_abort_upcall(struct c4iw_ep *ep)
1166 {
1167 	struct iw_cm_event event;
1168 
1169 	PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1170 	memset(&event, 0, sizeof(event));
1171 	event.event = IW_CM_EVENT_CLOSE;
1172 	event.status = -ECONNRESET;
1173 	if (ep->com.cm_id) {
1174 		PDBG("abort delivered ep %p cm_id %p tid %u\n", ep,
1175 		     ep->com.cm_id, ep->hwtid);
1176 		ep->com.cm_id->event_handler(ep->com.cm_id, &event);
1177 		ep->com.cm_id->rem_ref(ep->com.cm_id);
1178 		ep->com.cm_id = NULL;
1179 		set_bit(ABORT_UPCALL, &ep->com.history);
1180 	}
1181 }
1182 
1183 static void connect_reply_upcall(struct c4iw_ep *ep, int status)
1184 {
1185 	struct iw_cm_event event;
1186 
1187 	PDBG("%s ep %p tid %u status %d\n", __func__, ep, ep->hwtid, status);
1188 	memset(&event, 0, sizeof(event));
1189 	event.event = IW_CM_EVENT_CONNECT_REPLY;
1190 	event.status = status;
1191 	memcpy(&event.local_addr, &ep->com.local_addr,
1192 	       sizeof(ep->com.local_addr));
1193 	memcpy(&event.remote_addr, &ep->com.remote_addr,
1194 	       sizeof(ep->com.remote_addr));
1195 
1196 	if ((status == 0) || (status == -ECONNREFUSED)) {
1197 		if (!ep->tried_with_mpa_v1) {
1198 			/* this means MPA_v2 is used */
1199 			event.private_data_len = ep->plen -
1200 				sizeof(struct mpa_v2_conn_params);
1201 			event.private_data = ep->mpa_pkt +
1202 				sizeof(struct mpa_message) +
1203 				sizeof(struct mpa_v2_conn_params);
1204 		} else {
1205 			/* this means MPA_v1 is used */
1206 			event.private_data_len = ep->plen;
1207 			event.private_data = ep->mpa_pkt +
1208 				sizeof(struct mpa_message);
1209 		}
1210 	}
1211 
1212 	PDBG("%s ep %p tid %u status %d\n", __func__, ep,
1213 	     ep->hwtid, status);
1214 	set_bit(CONN_RPL_UPCALL, &ep->com.history);
1215 	ep->com.cm_id->event_handler(ep->com.cm_id, &event);
1216 
1217 	if (status < 0) {
1218 		ep->com.cm_id->rem_ref(ep->com.cm_id);
1219 		ep->com.cm_id = NULL;
1220 	}
1221 }
1222 
1223 static int connect_request_upcall(struct c4iw_ep *ep)
1224 {
1225 	struct iw_cm_event event;
1226 	int ret;
1227 
1228 	PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1229 	memset(&event, 0, sizeof(event));
1230 	event.event = IW_CM_EVENT_CONNECT_REQUEST;
1231 	memcpy(&event.local_addr, &ep->com.local_addr,
1232 	       sizeof(ep->com.local_addr));
1233 	memcpy(&event.remote_addr, &ep->com.remote_addr,
1234 	       sizeof(ep->com.remote_addr));
1235 	event.provider_data = ep;
1236 	if (!ep->tried_with_mpa_v1) {
1237 		/* this means MPA_v2 is used */
1238 		event.ord = ep->ord;
1239 		event.ird = ep->ird;
1240 		event.private_data_len = ep->plen -
1241 			sizeof(struct mpa_v2_conn_params);
1242 		event.private_data = ep->mpa_pkt + sizeof(struct mpa_message) +
1243 			sizeof(struct mpa_v2_conn_params);
1244 	} else {
1245 		/* this means MPA_v1 is used. Send max supported */
1246 		event.ord = cur_max_read_depth(ep->com.dev);
1247 		event.ird = cur_max_read_depth(ep->com.dev);
1248 		event.private_data_len = ep->plen;
1249 		event.private_data = ep->mpa_pkt + sizeof(struct mpa_message);
1250 	}
1251 	c4iw_get_ep(&ep->com);
1252 	ret = ep->parent_ep->com.cm_id->event_handler(ep->parent_ep->com.cm_id,
1253 						      &event);
1254 	if (ret)
1255 		c4iw_put_ep(&ep->com);
1256 	set_bit(CONNREQ_UPCALL, &ep->com.history);
1257 	c4iw_put_ep(&ep->parent_ep->com);
1258 	return ret;
1259 }
1260 
1261 static void established_upcall(struct c4iw_ep *ep)
1262 {
1263 	struct iw_cm_event event;
1264 
1265 	PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1266 	memset(&event, 0, sizeof(event));
1267 	event.event = IW_CM_EVENT_ESTABLISHED;
1268 	event.ird = ep->ird;
1269 	event.ord = ep->ord;
1270 	if (ep->com.cm_id) {
1271 		PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1272 		ep->com.cm_id->event_handler(ep->com.cm_id, &event);
1273 		set_bit(ESTAB_UPCALL, &ep->com.history);
1274 	}
1275 }
1276 
1277 static int update_rx_credits(struct c4iw_ep *ep, u32 credits)
1278 {
1279 	struct cpl_rx_data_ack *req;
1280 	struct sk_buff *skb;
1281 	int wrlen = roundup(sizeof *req, 16);
1282 
1283 	PDBG("%s ep %p tid %u credits %u\n", __func__, ep, ep->hwtid, credits);
1284 	skb = get_skb(NULL, wrlen, GFP_KERNEL);
1285 	if (!skb) {
1286 		printk(KERN_ERR MOD "update_rx_credits - cannot alloc skb!\n");
1287 		return 0;
1288 	}
1289 
1290 	/*
1291 	 * If we couldn't specify the entire rcv window at connection setup
1292 	 * due to the limit in the number of bits in the RCV_BUFSIZ field,
1293 	 * then add the overage in to the credits returned.
1294 	 */
1295 	if (ep->rcv_win > RCV_BUFSIZ_M * 1024)
1296 		credits += ep->rcv_win - RCV_BUFSIZ_M * 1024;
1297 
1298 	req = (struct cpl_rx_data_ack *) skb_put(skb, wrlen);
1299 	memset(req, 0, wrlen);
1300 	INIT_TP_WR(req, ep->hwtid);
1301 	OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_RX_DATA_ACK,
1302 						    ep->hwtid));
1303 	req->credit_dack = cpu_to_be32(credits | RX_FORCE_ACK_F |
1304 				       RX_DACK_CHANGE_F |
1305 				       RX_DACK_MODE_V(dack_mode));
1306 	set_wr_txq(skb, CPL_PRIORITY_ACK, ep->ctrlq_idx);
1307 	c4iw_ofld_send(&ep->com.dev->rdev, skb);
1308 	return credits;
1309 }
1310 
1311 #define RELAXED_IRD_NEGOTIATION 1
1312 
1313 static int process_mpa_reply(struct c4iw_ep *ep, struct sk_buff *skb)
1314 {
1315 	struct mpa_message *mpa;
1316 	struct mpa_v2_conn_params *mpa_v2_params;
1317 	u16 plen;
1318 	u16 resp_ird, resp_ord;
1319 	u8 rtr_mismatch = 0, insuff_ird = 0;
1320 	struct c4iw_qp_attributes attrs;
1321 	enum c4iw_qp_attr_mask mask;
1322 	int err;
1323 	int disconnect = 0;
1324 
1325 	PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1326 
1327 	/*
1328 	 * Stop mpa timer.  If it expired, then
1329 	 * we ignore the MPA reply.  process_timeout()
1330 	 * will abort the connection.
1331 	 */
1332 	if (stop_ep_timer(ep))
1333 		return 0;
1334 
1335 	/*
1336 	 * If we get more than the supported amount of private data
1337 	 * then we must fail this connection.
1338 	 */
1339 	if (ep->mpa_pkt_len + skb->len > sizeof(ep->mpa_pkt)) {
1340 		err = -EINVAL;
1341 		goto err;
1342 	}
1343 
1344 	/*
1345 	 * copy the new data into our accumulation buffer.
1346 	 */
1347 	skb_copy_from_linear_data(skb, &(ep->mpa_pkt[ep->mpa_pkt_len]),
1348 				  skb->len);
1349 	ep->mpa_pkt_len += skb->len;
1350 
1351 	/*
1352 	 * if we don't even have the mpa message, then bail.
1353 	 */
1354 	if (ep->mpa_pkt_len < sizeof(*mpa))
1355 		return 0;
1356 	mpa = (struct mpa_message *) ep->mpa_pkt;
1357 
1358 	/* Validate MPA header. */
1359 	if (mpa->revision > mpa_rev) {
1360 		printk(KERN_ERR MOD "%s MPA version mismatch. Local = %d,"
1361 		       " Received = %d\n", __func__, mpa_rev, mpa->revision);
1362 		err = -EPROTO;
1363 		goto err;
1364 	}
1365 	if (memcmp(mpa->key, MPA_KEY_REP, sizeof(mpa->key))) {
1366 		err = -EPROTO;
1367 		goto err;
1368 	}
1369 
1370 	plen = ntohs(mpa->private_data_size);
1371 
1372 	/*
1373 	 * Fail if there's too much private data.
1374 	 */
1375 	if (plen > MPA_MAX_PRIVATE_DATA) {
1376 		err = -EPROTO;
1377 		goto err;
1378 	}
1379 
1380 	/*
1381 	 * If plen does not account for pkt size
1382 	 */
1383 	if (ep->mpa_pkt_len > (sizeof(*mpa) + plen)) {
1384 		err = -EPROTO;
1385 		goto err;
1386 	}
1387 
1388 	ep->plen = (u8) plen;
1389 
1390 	/*
1391 	 * If we don't have all the pdata yet, then bail.
1392 	 * We'll continue process when more data arrives.
1393 	 */
1394 	if (ep->mpa_pkt_len < (sizeof(*mpa) + plen))
1395 		return 0;
1396 
1397 	if (mpa->flags & MPA_REJECT) {
1398 		err = -ECONNREFUSED;
1399 		goto err;
1400 	}
1401 
1402 	/*
1403 	 * If we get here we have accumulated the entire mpa
1404 	 * start reply message including private data. And
1405 	 * the MPA header is valid.
1406 	 */
1407 	__state_set(&ep->com, FPDU_MODE);
1408 	ep->mpa_attr.crc_enabled = (mpa->flags & MPA_CRC) | crc_enabled ? 1 : 0;
1409 	ep->mpa_attr.recv_marker_enabled = markers_enabled;
1410 	ep->mpa_attr.xmit_marker_enabled = mpa->flags & MPA_MARKERS ? 1 : 0;
1411 	ep->mpa_attr.version = mpa->revision;
1412 	ep->mpa_attr.p2p_type = FW_RI_INIT_P2PTYPE_DISABLED;
1413 
1414 	if (mpa->revision == 2) {
1415 		ep->mpa_attr.enhanced_rdma_conn =
1416 			mpa->flags & MPA_ENHANCED_RDMA_CONN ? 1 : 0;
1417 		if (ep->mpa_attr.enhanced_rdma_conn) {
1418 			mpa_v2_params = (struct mpa_v2_conn_params *)
1419 				(ep->mpa_pkt + sizeof(*mpa));
1420 			resp_ird = ntohs(mpa_v2_params->ird) &
1421 				MPA_V2_IRD_ORD_MASK;
1422 			resp_ord = ntohs(mpa_v2_params->ord) &
1423 				MPA_V2_IRD_ORD_MASK;
1424 			PDBG("%s responder ird %u ord %u ep ird %u ord %u\n",
1425 			     __func__, resp_ird, resp_ord, ep->ird, ep->ord);
1426 
1427 			/*
1428 			 * This is a double-check. Ideally, below checks are
1429 			 * not required since ird/ord stuff has been taken
1430 			 * care of in c4iw_accept_cr
1431 			 */
1432 			if (ep->ird < resp_ord) {
1433 				if (RELAXED_IRD_NEGOTIATION && resp_ord <=
1434 				    ep->com.dev->rdev.lldi.max_ordird_qp)
1435 					ep->ird = resp_ord;
1436 				else
1437 					insuff_ird = 1;
1438 			} else if (ep->ird > resp_ord) {
1439 				ep->ird = resp_ord;
1440 			}
1441 			if (ep->ord > resp_ird) {
1442 				if (RELAXED_IRD_NEGOTIATION)
1443 					ep->ord = resp_ird;
1444 				else
1445 					insuff_ird = 1;
1446 			}
1447 			if (insuff_ird) {
1448 				err = -ENOMEM;
1449 				ep->ird = resp_ord;
1450 				ep->ord = resp_ird;
1451 			}
1452 
1453 			if (ntohs(mpa_v2_params->ird) &
1454 					MPA_V2_PEER2PEER_MODEL) {
1455 				if (ntohs(mpa_v2_params->ord) &
1456 						MPA_V2_RDMA_WRITE_RTR)
1457 					ep->mpa_attr.p2p_type =
1458 						FW_RI_INIT_P2PTYPE_RDMA_WRITE;
1459 				else if (ntohs(mpa_v2_params->ord) &
1460 						MPA_V2_RDMA_READ_RTR)
1461 					ep->mpa_attr.p2p_type =
1462 						FW_RI_INIT_P2PTYPE_READ_REQ;
1463 			}
1464 		}
1465 	} else if (mpa->revision == 1)
1466 		if (peer2peer)
1467 			ep->mpa_attr.p2p_type = p2p_type;
1468 
1469 	PDBG("%s - crc_enabled=%d, recv_marker_enabled=%d, "
1470 	     "xmit_marker_enabled=%d, version=%d p2p_type=%d local-p2p_type = "
1471 	     "%d\n", __func__, ep->mpa_attr.crc_enabled,
1472 	     ep->mpa_attr.recv_marker_enabled,
1473 	     ep->mpa_attr.xmit_marker_enabled, ep->mpa_attr.version,
1474 	     ep->mpa_attr.p2p_type, p2p_type);
1475 
1476 	/*
1477 	 * If responder's RTR does not match with that of initiator, assign
1478 	 * FW_RI_INIT_P2PTYPE_DISABLED in mpa attributes so that RTR is not
1479 	 * generated when moving QP to RTS state.
1480 	 * A TERM message will be sent after QP has moved to RTS state
1481 	 */
1482 	if ((ep->mpa_attr.version == 2) && peer2peer &&
1483 			(ep->mpa_attr.p2p_type != p2p_type)) {
1484 		ep->mpa_attr.p2p_type = FW_RI_INIT_P2PTYPE_DISABLED;
1485 		rtr_mismatch = 1;
1486 	}
1487 
1488 	attrs.mpa_attr = ep->mpa_attr;
1489 	attrs.max_ird = ep->ird;
1490 	attrs.max_ord = ep->ord;
1491 	attrs.llp_stream_handle = ep;
1492 	attrs.next_state = C4IW_QP_STATE_RTS;
1493 
1494 	mask = C4IW_QP_ATTR_NEXT_STATE |
1495 	    C4IW_QP_ATTR_LLP_STREAM_HANDLE | C4IW_QP_ATTR_MPA_ATTR |
1496 	    C4IW_QP_ATTR_MAX_IRD | C4IW_QP_ATTR_MAX_ORD;
1497 
1498 	/* bind QP and TID with INIT_WR */
1499 	err = c4iw_modify_qp(ep->com.qp->rhp,
1500 			     ep->com.qp, mask, &attrs, 1);
1501 	if (err)
1502 		goto err;
1503 
1504 	/*
1505 	 * If responder's RTR requirement did not match with what initiator
1506 	 * supports, generate TERM message
1507 	 */
1508 	if (rtr_mismatch) {
1509 		printk(KERN_ERR "%s: RTR mismatch, sending TERM\n", __func__);
1510 		attrs.layer_etype = LAYER_MPA | DDP_LLP;
1511 		attrs.ecode = MPA_NOMATCH_RTR;
1512 		attrs.next_state = C4IW_QP_STATE_TERMINATE;
1513 		attrs.send_term = 1;
1514 		err = c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
1515 				C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
1516 		err = -ENOMEM;
1517 		disconnect = 1;
1518 		goto out;
1519 	}
1520 
1521 	/*
1522 	 * Generate TERM if initiator IRD is not sufficient for responder
1523 	 * provided ORD. Currently, we do the same behaviour even when
1524 	 * responder provided IRD is also not sufficient as regards to
1525 	 * initiator ORD.
1526 	 */
1527 	if (insuff_ird) {
1528 		printk(KERN_ERR "%s: Insufficient IRD, sending TERM\n",
1529 				__func__);
1530 		attrs.layer_etype = LAYER_MPA | DDP_LLP;
1531 		attrs.ecode = MPA_INSUFF_IRD;
1532 		attrs.next_state = C4IW_QP_STATE_TERMINATE;
1533 		attrs.send_term = 1;
1534 		err = c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
1535 				C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
1536 		err = -ENOMEM;
1537 		disconnect = 1;
1538 		goto out;
1539 	}
1540 	goto out;
1541 err:
1542 	__state_set(&ep->com, ABORTING);
1543 	send_abort(ep, skb, GFP_KERNEL);
1544 out:
1545 	connect_reply_upcall(ep, err);
1546 	return disconnect;
1547 }
1548 
1549 static void process_mpa_request(struct c4iw_ep *ep, struct sk_buff *skb)
1550 {
1551 	struct mpa_message *mpa;
1552 	struct mpa_v2_conn_params *mpa_v2_params;
1553 	u16 plen;
1554 
1555 	PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1556 
1557 	/*
1558 	 * If we get more than the supported amount of private data
1559 	 * then we must fail this connection.
1560 	 */
1561 	if (ep->mpa_pkt_len + skb->len > sizeof(ep->mpa_pkt)) {
1562 		(void)stop_ep_timer(ep);
1563 		abort_connection(ep, skb, GFP_KERNEL);
1564 		return;
1565 	}
1566 
1567 	PDBG("%s enter (%s line %u)\n", __func__, __FILE__, __LINE__);
1568 
1569 	/*
1570 	 * Copy the new data into our accumulation buffer.
1571 	 */
1572 	skb_copy_from_linear_data(skb, &(ep->mpa_pkt[ep->mpa_pkt_len]),
1573 				  skb->len);
1574 	ep->mpa_pkt_len += skb->len;
1575 
1576 	/*
1577 	 * If we don't even have the mpa message, then bail.
1578 	 * We'll continue process when more data arrives.
1579 	 */
1580 	if (ep->mpa_pkt_len < sizeof(*mpa))
1581 		return;
1582 
1583 	PDBG("%s enter (%s line %u)\n", __func__, __FILE__, __LINE__);
1584 	mpa = (struct mpa_message *) ep->mpa_pkt;
1585 
1586 	/*
1587 	 * Validate MPA Header.
1588 	 */
1589 	if (mpa->revision > mpa_rev) {
1590 		printk(KERN_ERR MOD "%s MPA version mismatch. Local = %d,"
1591 		       " Received = %d\n", __func__, mpa_rev, mpa->revision);
1592 		(void)stop_ep_timer(ep);
1593 		abort_connection(ep, skb, GFP_KERNEL);
1594 		return;
1595 	}
1596 
1597 	if (memcmp(mpa->key, MPA_KEY_REQ, sizeof(mpa->key))) {
1598 		(void)stop_ep_timer(ep);
1599 		abort_connection(ep, skb, GFP_KERNEL);
1600 		return;
1601 	}
1602 
1603 	plen = ntohs(mpa->private_data_size);
1604 
1605 	/*
1606 	 * Fail if there's too much private data.
1607 	 */
1608 	if (plen > MPA_MAX_PRIVATE_DATA) {
1609 		(void)stop_ep_timer(ep);
1610 		abort_connection(ep, skb, GFP_KERNEL);
1611 		return;
1612 	}
1613 
1614 	/*
1615 	 * If plen does not account for pkt size
1616 	 */
1617 	if (ep->mpa_pkt_len > (sizeof(*mpa) + plen)) {
1618 		(void)stop_ep_timer(ep);
1619 		abort_connection(ep, skb, GFP_KERNEL);
1620 		return;
1621 	}
1622 	ep->plen = (u8) plen;
1623 
1624 	/*
1625 	 * If we don't have all the pdata yet, then bail.
1626 	 */
1627 	if (ep->mpa_pkt_len < (sizeof(*mpa) + plen))
1628 		return;
1629 
1630 	/*
1631 	 * If we get here we have accumulated the entire mpa
1632 	 * start reply message including private data.
1633 	 */
1634 	ep->mpa_attr.initiator = 0;
1635 	ep->mpa_attr.crc_enabled = (mpa->flags & MPA_CRC) | crc_enabled ? 1 : 0;
1636 	ep->mpa_attr.recv_marker_enabled = markers_enabled;
1637 	ep->mpa_attr.xmit_marker_enabled = mpa->flags & MPA_MARKERS ? 1 : 0;
1638 	ep->mpa_attr.version = mpa->revision;
1639 	if (mpa->revision == 1)
1640 		ep->tried_with_mpa_v1 = 1;
1641 	ep->mpa_attr.p2p_type = FW_RI_INIT_P2PTYPE_DISABLED;
1642 
1643 	if (mpa->revision == 2) {
1644 		ep->mpa_attr.enhanced_rdma_conn =
1645 			mpa->flags & MPA_ENHANCED_RDMA_CONN ? 1 : 0;
1646 		if (ep->mpa_attr.enhanced_rdma_conn) {
1647 			mpa_v2_params = (struct mpa_v2_conn_params *)
1648 				(ep->mpa_pkt + sizeof(*mpa));
1649 			ep->ird = ntohs(mpa_v2_params->ird) &
1650 				MPA_V2_IRD_ORD_MASK;
1651 			ep->ord = ntohs(mpa_v2_params->ord) &
1652 				MPA_V2_IRD_ORD_MASK;
1653 			PDBG("%s initiator ird %u ord %u\n", __func__, ep->ird,
1654 			     ep->ord);
1655 			if (ntohs(mpa_v2_params->ird) & MPA_V2_PEER2PEER_MODEL)
1656 				if (peer2peer) {
1657 					if (ntohs(mpa_v2_params->ord) &
1658 							MPA_V2_RDMA_WRITE_RTR)
1659 						ep->mpa_attr.p2p_type =
1660 						FW_RI_INIT_P2PTYPE_RDMA_WRITE;
1661 					else if (ntohs(mpa_v2_params->ord) &
1662 							MPA_V2_RDMA_READ_RTR)
1663 						ep->mpa_attr.p2p_type =
1664 						FW_RI_INIT_P2PTYPE_READ_REQ;
1665 				}
1666 		}
1667 	} else if (mpa->revision == 1)
1668 		if (peer2peer)
1669 			ep->mpa_attr.p2p_type = p2p_type;
1670 
1671 	PDBG("%s - crc_enabled=%d, recv_marker_enabled=%d, "
1672 	     "xmit_marker_enabled=%d, version=%d p2p_type=%d\n", __func__,
1673 	     ep->mpa_attr.crc_enabled, ep->mpa_attr.recv_marker_enabled,
1674 	     ep->mpa_attr.xmit_marker_enabled, ep->mpa_attr.version,
1675 	     ep->mpa_attr.p2p_type);
1676 
1677 	/*
1678 	 * If the endpoint timer already expired, then we ignore
1679 	 * the start request.  process_timeout() will abort
1680 	 * the connection.
1681 	 */
1682 	if (!stop_ep_timer(ep)) {
1683 		__state_set(&ep->com, MPA_REQ_RCVD);
1684 
1685 		/* drive upcall */
1686 		mutex_lock_nested(&ep->parent_ep->com.mutex,
1687 				  SINGLE_DEPTH_NESTING);
1688 		if (ep->parent_ep->com.state != DEAD) {
1689 			if (connect_request_upcall(ep))
1690 				abort_connection(ep, skb, GFP_KERNEL);
1691 		} else {
1692 			abort_connection(ep, skb, GFP_KERNEL);
1693 		}
1694 		mutex_unlock(&ep->parent_ep->com.mutex);
1695 	}
1696 	return;
1697 }
1698 
1699 static int rx_data(struct c4iw_dev *dev, struct sk_buff *skb)
1700 {
1701 	struct c4iw_ep *ep;
1702 	struct cpl_rx_data *hdr = cplhdr(skb);
1703 	unsigned int dlen = ntohs(hdr->len);
1704 	unsigned int tid = GET_TID(hdr);
1705 	struct tid_info *t = dev->rdev.lldi.tids;
1706 	__u8 status = hdr->status;
1707 	int disconnect = 0;
1708 
1709 	ep = lookup_tid(t, tid);
1710 	if (!ep)
1711 		return 0;
1712 	PDBG("%s ep %p tid %u dlen %u\n", __func__, ep, ep->hwtid, dlen);
1713 	skb_pull(skb, sizeof(*hdr));
1714 	skb_trim(skb, dlen);
1715 	mutex_lock(&ep->com.mutex);
1716 
1717 	/* update RX credits */
1718 	update_rx_credits(ep, dlen);
1719 
1720 	switch (ep->com.state) {
1721 	case MPA_REQ_SENT:
1722 		ep->rcv_seq += dlen;
1723 		disconnect = process_mpa_reply(ep, skb);
1724 		break;
1725 	case MPA_REQ_WAIT:
1726 		ep->rcv_seq += dlen;
1727 		process_mpa_request(ep, skb);
1728 		break;
1729 	case FPDU_MODE: {
1730 		struct c4iw_qp_attributes attrs;
1731 		BUG_ON(!ep->com.qp);
1732 		if (status)
1733 			pr_err("%s Unexpected streaming data." \
1734 			       " qpid %u ep %p state %d tid %u status %d\n",
1735 			       __func__, ep->com.qp->wq.sq.qid, ep,
1736 			       ep->com.state, ep->hwtid, status);
1737 		attrs.next_state = C4IW_QP_STATE_TERMINATE;
1738 		c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
1739 			       C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
1740 		disconnect = 1;
1741 		break;
1742 	}
1743 	default:
1744 		break;
1745 	}
1746 	mutex_unlock(&ep->com.mutex);
1747 	if (disconnect)
1748 		c4iw_ep_disconnect(ep, 0, GFP_KERNEL);
1749 	return 0;
1750 }
1751 
1752 static int abort_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
1753 {
1754 	struct c4iw_ep *ep;
1755 	struct cpl_abort_rpl_rss *rpl = cplhdr(skb);
1756 	int release = 0;
1757 	unsigned int tid = GET_TID(rpl);
1758 	struct tid_info *t = dev->rdev.lldi.tids;
1759 
1760 	ep = lookup_tid(t, tid);
1761 	if (!ep) {
1762 		printk(KERN_WARNING MOD "Abort rpl to freed endpoint\n");
1763 		return 0;
1764 	}
1765 	PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1766 	mutex_lock(&ep->com.mutex);
1767 	switch (ep->com.state) {
1768 	case ABORTING:
1769 		c4iw_wake_up(&ep->com.wr_wait, -ECONNRESET);
1770 		__state_set(&ep->com, DEAD);
1771 		release = 1;
1772 		break;
1773 	default:
1774 		printk(KERN_ERR "%s ep %p state %d\n",
1775 		     __func__, ep, ep->com.state);
1776 		break;
1777 	}
1778 	mutex_unlock(&ep->com.mutex);
1779 
1780 	if (release)
1781 		release_ep_resources(ep);
1782 	return 0;
1783 }
1784 
1785 static void send_fw_act_open_req(struct c4iw_ep *ep, unsigned int atid)
1786 {
1787 	struct sk_buff *skb;
1788 	struct fw_ofld_connection_wr *req;
1789 	unsigned int mtu_idx;
1790 	int wscale;
1791 	struct sockaddr_in *sin;
1792 	int win;
1793 
1794 	skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
1795 	req = (struct fw_ofld_connection_wr *)__skb_put(skb, sizeof(*req));
1796 	memset(req, 0, sizeof(*req));
1797 	req->op_compl = htonl(WR_OP_V(FW_OFLD_CONNECTION_WR));
1798 	req->len16_pkd = htonl(FW_WR_LEN16_V(DIV_ROUND_UP(sizeof(*req), 16)));
1799 	req->le.filter = cpu_to_be32(cxgb4_select_ntuple(
1800 				     ep->com.dev->rdev.lldi.ports[0],
1801 				     ep->l2t));
1802 	sin = (struct sockaddr_in *)&ep->com.mapped_local_addr;
1803 	req->le.lport = sin->sin_port;
1804 	req->le.u.ipv4.lip = sin->sin_addr.s_addr;
1805 	sin = (struct sockaddr_in *)&ep->com.mapped_remote_addr;
1806 	req->le.pport = sin->sin_port;
1807 	req->le.u.ipv4.pip = sin->sin_addr.s_addr;
1808 	req->tcb.t_state_to_astid =
1809 			htonl(FW_OFLD_CONNECTION_WR_T_STATE_V(TCP_SYN_SENT) |
1810 			FW_OFLD_CONNECTION_WR_ASTID_V(atid));
1811 	req->tcb.cplrxdataack_cplpassacceptrpl =
1812 			htons(FW_OFLD_CONNECTION_WR_CPLRXDATAACK_F);
1813 	req->tcb.tx_max = (__force __be32) jiffies;
1814 	req->tcb.rcv_adv = htons(1);
1815 	best_mtu(ep->com.dev->rdev.lldi.mtus, ep->mtu, &mtu_idx,
1816 		 enable_tcp_timestamps,
1817 		 (AF_INET == ep->com.remote_addr.ss_family) ? 0 : 1);
1818 	wscale = compute_wscale(rcv_win);
1819 
1820 	/*
1821 	 * Specify the largest window that will fit in opt0. The
1822 	 * remainder will be specified in the rx_data_ack.
1823 	 */
1824 	win = ep->rcv_win >> 10;
1825 	if (win > RCV_BUFSIZ_M)
1826 		win = RCV_BUFSIZ_M;
1827 
1828 	req->tcb.opt0 = (__force __be64) (TCAM_BYPASS_F |
1829 		(nocong ? NO_CONG_F : 0) |
1830 		KEEP_ALIVE_F |
1831 		DELACK_F |
1832 		WND_SCALE_V(wscale) |
1833 		MSS_IDX_V(mtu_idx) |
1834 		L2T_IDX_V(ep->l2t->idx) |
1835 		TX_CHAN_V(ep->tx_chan) |
1836 		SMAC_SEL_V(ep->smac_idx) |
1837 		DSCP_V(ep->tos) |
1838 		ULP_MODE_V(ULP_MODE_TCPDDP) |
1839 		RCV_BUFSIZ_V(win));
1840 	req->tcb.opt2 = (__force __be32) (PACE_V(1) |
1841 		TX_QUEUE_V(ep->com.dev->rdev.lldi.tx_modq[ep->tx_chan]) |
1842 		RX_CHANNEL_V(0) |
1843 		CCTRL_ECN_V(enable_ecn) |
1844 		RSS_QUEUE_VALID_F | RSS_QUEUE_V(ep->rss_qid));
1845 	if (enable_tcp_timestamps)
1846 		req->tcb.opt2 |= (__force __be32)TSTAMPS_EN_F;
1847 	if (enable_tcp_sack)
1848 		req->tcb.opt2 |= (__force __be32)SACK_EN_F;
1849 	if (wscale && enable_tcp_window_scaling)
1850 		req->tcb.opt2 |= (__force __be32)WND_SCALE_EN_F;
1851 	req->tcb.opt0 = cpu_to_be64((__force u64)req->tcb.opt0);
1852 	req->tcb.opt2 = cpu_to_be32((__force u32)req->tcb.opt2);
1853 	set_wr_txq(skb, CPL_PRIORITY_CONTROL, ep->ctrlq_idx);
1854 	set_bit(ACT_OFLD_CONN, &ep->com.history);
1855 	c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
1856 }
1857 
1858 /*
1859  * Return whether a failed active open has allocated a TID
1860  */
1861 static inline int act_open_has_tid(int status)
1862 {
1863 	return status != CPL_ERR_TCAM_FULL && status != CPL_ERR_CONN_EXIST &&
1864 	       status != CPL_ERR_ARP_MISS;
1865 }
1866 
1867 /* Returns whether a CPL status conveys negative advice.
1868  */
1869 static int is_neg_adv(unsigned int status)
1870 {
1871 	return status == CPL_ERR_RTX_NEG_ADVICE ||
1872 	       status == CPL_ERR_PERSIST_NEG_ADVICE ||
1873 	       status == CPL_ERR_KEEPALV_NEG_ADVICE;
1874 }
1875 
1876 static char *neg_adv_str(unsigned int status)
1877 {
1878 	switch (status) {
1879 	case CPL_ERR_RTX_NEG_ADVICE:
1880 		return "Retransmit timeout";
1881 	case CPL_ERR_PERSIST_NEG_ADVICE:
1882 		return "Persist timeout";
1883 	case CPL_ERR_KEEPALV_NEG_ADVICE:
1884 		return "Keepalive timeout";
1885 	default:
1886 		return "Unknown";
1887 	}
1888 }
1889 
1890 static void set_tcp_window(struct c4iw_ep *ep, struct port_info *pi)
1891 {
1892 	ep->snd_win = snd_win;
1893 	ep->rcv_win = rcv_win;
1894 	PDBG("%s snd_win %d rcv_win %d\n", __func__, ep->snd_win, ep->rcv_win);
1895 }
1896 
1897 #define ACT_OPEN_RETRY_COUNT 2
1898 
1899 static int import_ep(struct c4iw_ep *ep, int iptype, __u8 *peer_ip,
1900 		     struct dst_entry *dst, struct c4iw_dev *cdev,
1901 		     bool clear_mpa_v1)
1902 {
1903 	struct neighbour *n;
1904 	int err, step;
1905 	struct net_device *pdev;
1906 
1907 	n = dst_neigh_lookup(dst, peer_ip);
1908 	if (!n)
1909 		return -ENODEV;
1910 
1911 	rcu_read_lock();
1912 	err = -ENOMEM;
1913 	if (n->dev->flags & IFF_LOOPBACK) {
1914 		if (iptype == 4)
1915 			pdev = ip_dev_find(&init_net, *(__be32 *)peer_ip);
1916 		else if (IS_ENABLED(CONFIG_IPV6))
1917 			for_each_netdev(&init_net, pdev) {
1918 				if (ipv6_chk_addr(&init_net,
1919 						  (struct in6_addr *)peer_ip,
1920 						  pdev, 1))
1921 					break;
1922 			}
1923 		else
1924 			pdev = NULL;
1925 
1926 		if (!pdev) {
1927 			err = -ENODEV;
1928 			goto out;
1929 		}
1930 		ep->l2t = cxgb4_l2t_get(cdev->rdev.lldi.l2t,
1931 					n, pdev, 0);
1932 		if (!ep->l2t)
1933 			goto out;
1934 		ep->mtu = pdev->mtu;
1935 		ep->tx_chan = cxgb4_port_chan(pdev);
1936 		ep->smac_idx = (cxgb4_port_viid(pdev) & 0x7F) << 1;
1937 		step = cdev->rdev.lldi.ntxq /
1938 			cdev->rdev.lldi.nchan;
1939 		ep->txq_idx = cxgb4_port_idx(pdev) * step;
1940 		step = cdev->rdev.lldi.nrxq /
1941 			cdev->rdev.lldi.nchan;
1942 		ep->ctrlq_idx = cxgb4_port_idx(pdev);
1943 		ep->rss_qid = cdev->rdev.lldi.rxq_ids[
1944 			cxgb4_port_idx(pdev) * step];
1945 		set_tcp_window(ep, (struct port_info *)netdev_priv(pdev));
1946 		dev_put(pdev);
1947 	} else {
1948 		pdev = get_real_dev(n->dev);
1949 		ep->l2t = cxgb4_l2t_get(cdev->rdev.lldi.l2t,
1950 					n, pdev, 0);
1951 		if (!ep->l2t)
1952 			goto out;
1953 		ep->mtu = dst_mtu(dst);
1954 		ep->tx_chan = cxgb4_port_chan(pdev);
1955 		ep->smac_idx = (cxgb4_port_viid(pdev) & 0x7F) << 1;
1956 		step = cdev->rdev.lldi.ntxq /
1957 			cdev->rdev.lldi.nchan;
1958 		ep->txq_idx = cxgb4_port_idx(pdev) * step;
1959 		ep->ctrlq_idx = cxgb4_port_idx(pdev);
1960 		step = cdev->rdev.lldi.nrxq /
1961 			cdev->rdev.lldi.nchan;
1962 		ep->rss_qid = cdev->rdev.lldi.rxq_ids[
1963 			cxgb4_port_idx(pdev) * step];
1964 		set_tcp_window(ep, (struct port_info *)netdev_priv(pdev));
1965 
1966 		if (clear_mpa_v1) {
1967 			ep->retry_with_mpa_v1 = 0;
1968 			ep->tried_with_mpa_v1 = 0;
1969 		}
1970 	}
1971 	err = 0;
1972 out:
1973 	rcu_read_unlock();
1974 
1975 	neigh_release(n);
1976 
1977 	return err;
1978 }
1979 
1980 static int c4iw_reconnect(struct c4iw_ep *ep)
1981 {
1982 	int err = 0;
1983 	struct sockaddr_in *laddr = (struct sockaddr_in *)
1984 				    &ep->com.cm_id->local_addr;
1985 	struct sockaddr_in *raddr = (struct sockaddr_in *)
1986 				    &ep->com.cm_id->remote_addr;
1987 	struct sockaddr_in6 *laddr6 = (struct sockaddr_in6 *)
1988 				      &ep->com.cm_id->local_addr;
1989 	struct sockaddr_in6 *raddr6 = (struct sockaddr_in6 *)
1990 				      &ep->com.cm_id->remote_addr;
1991 	int iptype;
1992 	__u8 *ra;
1993 
1994 	PDBG("%s qp %p cm_id %p\n", __func__, ep->com.qp, ep->com.cm_id);
1995 	init_timer(&ep->timer);
1996 
1997 	/*
1998 	 * Allocate an active TID to initiate a TCP connection.
1999 	 */
2000 	ep->atid = cxgb4_alloc_atid(ep->com.dev->rdev.lldi.tids, ep);
2001 	if (ep->atid == -1) {
2002 		pr_err("%s - cannot alloc atid.\n", __func__);
2003 		err = -ENOMEM;
2004 		goto fail2;
2005 	}
2006 	insert_handle(ep->com.dev, &ep->com.dev->atid_idr, ep, ep->atid);
2007 
2008 	/* find a route */
2009 	if (ep->com.cm_id->local_addr.ss_family == AF_INET) {
2010 		ep->dst = find_route(ep->com.dev, laddr->sin_addr.s_addr,
2011 				     raddr->sin_addr.s_addr, laddr->sin_port,
2012 				     raddr->sin_port, 0);
2013 		iptype = 4;
2014 		ra = (__u8 *)&raddr->sin_addr;
2015 	} else {
2016 		ep->dst = find_route6(ep->com.dev, laddr6->sin6_addr.s6_addr,
2017 				      raddr6->sin6_addr.s6_addr,
2018 				      laddr6->sin6_port, raddr6->sin6_port, 0,
2019 				      raddr6->sin6_scope_id);
2020 		iptype = 6;
2021 		ra = (__u8 *)&raddr6->sin6_addr;
2022 	}
2023 	if (!ep->dst) {
2024 		pr_err("%s - cannot find route.\n", __func__);
2025 		err = -EHOSTUNREACH;
2026 		goto fail3;
2027 	}
2028 	err = import_ep(ep, iptype, ra, ep->dst, ep->com.dev, false);
2029 	if (err) {
2030 		pr_err("%s - cannot alloc l2e.\n", __func__);
2031 		goto fail4;
2032 	}
2033 
2034 	PDBG("%s txq_idx %u tx_chan %u smac_idx %u rss_qid %u l2t_idx %u\n",
2035 	     __func__, ep->txq_idx, ep->tx_chan, ep->smac_idx, ep->rss_qid,
2036 	     ep->l2t->idx);
2037 
2038 	state_set(&ep->com, CONNECTING);
2039 	ep->tos = 0;
2040 
2041 	/* send connect request to rnic */
2042 	err = send_connect(ep);
2043 	if (!err)
2044 		goto out;
2045 
2046 	cxgb4_l2t_release(ep->l2t);
2047 fail4:
2048 	dst_release(ep->dst);
2049 fail3:
2050 	remove_handle(ep->com.dev, &ep->com.dev->atid_idr, ep->atid);
2051 	cxgb4_free_atid(ep->com.dev->rdev.lldi.tids, ep->atid);
2052 fail2:
2053 	/*
2054 	 * remember to send notification to upper layer.
2055 	 * We are in here so the upper layer is not aware that this is
2056 	 * re-connect attempt and so, upper layer is still waiting for
2057 	 * response of 1st connect request.
2058 	 */
2059 	connect_reply_upcall(ep, -ECONNRESET);
2060 	c4iw_put_ep(&ep->com);
2061 out:
2062 	return err;
2063 }
2064 
2065 static int act_open_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
2066 {
2067 	struct c4iw_ep *ep;
2068 	struct cpl_act_open_rpl *rpl = cplhdr(skb);
2069 	unsigned int atid = TID_TID_G(AOPEN_ATID_G(
2070 				      ntohl(rpl->atid_status)));
2071 	struct tid_info *t = dev->rdev.lldi.tids;
2072 	int status = AOPEN_STATUS_G(ntohl(rpl->atid_status));
2073 	struct sockaddr_in *la;
2074 	struct sockaddr_in *ra;
2075 	struct sockaddr_in6 *la6;
2076 	struct sockaddr_in6 *ra6;
2077 
2078 	ep = lookup_atid(t, atid);
2079 	la = (struct sockaddr_in *)&ep->com.mapped_local_addr;
2080 	ra = (struct sockaddr_in *)&ep->com.mapped_remote_addr;
2081 	la6 = (struct sockaddr_in6 *)&ep->com.mapped_local_addr;
2082 	ra6 = (struct sockaddr_in6 *)&ep->com.mapped_remote_addr;
2083 
2084 	PDBG("%s ep %p atid %u status %u errno %d\n", __func__, ep, atid,
2085 	     status, status2errno(status));
2086 
2087 	if (is_neg_adv(status)) {
2088 		PDBG("%s Connection problems for atid %u status %u (%s)\n",
2089 		     __func__, atid, status, neg_adv_str(status));
2090 		ep->stats.connect_neg_adv++;
2091 		mutex_lock(&dev->rdev.stats.lock);
2092 		dev->rdev.stats.neg_adv++;
2093 		mutex_unlock(&dev->rdev.stats.lock);
2094 		return 0;
2095 	}
2096 
2097 	set_bit(ACT_OPEN_RPL, &ep->com.history);
2098 
2099 	/*
2100 	 * Log interesting failures.
2101 	 */
2102 	switch (status) {
2103 	case CPL_ERR_CONN_RESET:
2104 	case CPL_ERR_CONN_TIMEDOUT:
2105 		break;
2106 	case CPL_ERR_TCAM_FULL:
2107 		mutex_lock(&dev->rdev.stats.lock);
2108 		dev->rdev.stats.tcam_full++;
2109 		mutex_unlock(&dev->rdev.stats.lock);
2110 		if (ep->com.local_addr.ss_family == AF_INET &&
2111 		    dev->rdev.lldi.enable_fw_ofld_conn) {
2112 			send_fw_act_open_req(ep,
2113 					     TID_TID_G(AOPEN_ATID_G(
2114 					     ntohl(rpl->atid_status))));
2115 			return 0;
2116 		}
2117 		break;
2118 	case CPL_ERR_CONN_EXIST:
2119 		if (ep->retry_count++ < ACT_OPEN_RETRY_COUNT) {
2120 			set_bit(ACT_RETRY_INUSE, &ep->com.history);
2121 			if (ep->com.remote_addr.ss_family == AF_INET6) {
2122 				struct sockaddr_in6 *sin6 =
2123 						(struct sockaddr_in6 *)
2124 						&ep->com.mapped_local_addr;
2125 				cxgb4_clip_release(
2126 						ep->com.dev->rdev.lldi.ports[0],
2127 						(const u32 *)
2128 						&sin6->sin6_addr.s6_addr, 1);
2129 			}
2130 			remove_handle(ep->com.dev, &ep->com.dev->atid_idr,
2131 					atid);
2132 			cxgb4_free_atid(t, atid);
2133 			dst_release(ep->dst);
2134 			cxgb4_l2t_release(ep->l2t);
2135 			c4iw_reconnect(ep);
2136 			return 0;
2137 		}
2138 		break;
2139 	default:
2140 		if (ep->com.local_addr.ss_family == AF_INET) {
2141 			pr_info("Active open failure - atid %u status %u errno %d %pI4:%u->%pI4:%u\n",
2142 				atid, status, status2errno(status),
2143 				&la->sin_addr.s_addr, ntohs(la->sin_port),
2144 				&ra->sin_addr.s_addr, ntohs(ra->sin_port));
2145 		} else {
2146 			pr_info("Active open failure - atid %u status %u errno %d %pI6:%u->%pI6:%u\n",
2147 				atid, status, status2errno(status),
2148 				la6->sin6_addr.s6_addr, ntohs(la6->sin6_port),
2149 				ra6->sin6_addr.s6_addr, ntohs(ra6->sin6_port));
2150 		}
2151 		break;
2152 	}
2153 
2154 	connect_reply_upcall(ep, status2errno(status));
2155 	state_set(&ep->com, DEAD);
2156 
2157 	if (ep->com.remote_addr.ss_family == AF_INET6) {
2158 		struct sockaddr_in6 *sin6 =
2159 			(struct sockaddr_in6 *)&ep->com.mapped_local_addr;
2160 		cxgb4_clip_release(ep->com.dev->rdev.lldi.ports[0],
2161 				   (const u32 *)&sin6->sin6_addr.s6_addr, 1);
2162 	}
2163 	if (status && act_open_has_tid(status))
2164 		cxgb4_remove_tid(ep->com.dev->rdev.lldi.tids, 0, GET_TID(rpl));
2165 
2166 	remove_handle(ep->com.dev, &ep->com.dev->atid_idr, atid);
2167 	cxgb4_free_atid(t, atid);
2168 	dst_release(ep->dst);
2169 	cxgb4_l2t_release(ep->l2t);
2170 	c4iw_put_ep(&ep->com);
2171 
2172 	return 0;
2173 }
2174 
2175 static int pass_open_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
2176 {
2177 	struct cpl_pass_open_rpl *rpl = cplhdr(skb);
2178 	struct tid_info *t = dev->rdev.lldi.tids;
2179 	unsigned int stid = GET_TID(rpl);
2180 	struct c4iw_listen_ep *ep = lookup_stid(t, stid);
2181 
2182 	if (!ep) {
2183 		PDBG("%s stid %d lookup failure!\n", __func__, stid);
2184 		goto out;
2185 	}
2186 	PDBG("%s ep %p status %d error %d\n", __func__, ep,
2187 	     rpl->status, status2errno(rpl->status));
2188 	c4iw_wake_up(&ep->com.wr_wait, status2errno(rpl->status));
2189 
2190 out:
2191 	return 0;
2192 }
2193 
2194 static int close_listsrv_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
2195 {
2196 	struct cpl_close_listsvr_rpl *rpl = cplhdr(skb);
2197 	struct tid_info *t = dev->rdev.lldi.tids;
2198 	unsigned int stid = GET_TID(rpl);
2199 	struct c4iw_listen_ep *ep = lookup_stid(t, stid);
2200 
2201 	PDBG("%s ep %p\n", __func__, ep);
2202 	c4iw_wake_up(&ep->com.wr_wait, status2errno(rpl->status));
2203 	return 0;
2204 }
2205 
2206 static void accept_cr(struct c4iw_ep *ep, struct sk_buff *skb,
2207 		      struct cpl_pass_accept_req *req)
2208 {
2209 	struct cpl_pass_accept_rpl *rpl;
2210 	unsigned int mtu_idx;
2211 	u64 opt0;
2212 	u32 opt2;
2213 	int wscale;
2214 	struct cpl_t5_pass_accept_rpl *rpl5 = NULL;
2215 	int win;
2216 
2217 	PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
2218 	BUG_ON(skb_cloned(skb));
2219 
2220 	skb_get(skb);
2221 	rpl = cplhdr(skb);
2222 	if (is_t5(ep->com.dev->rdev.lldi.adapter_type)) {
2223 		skb_trim(skb, roundup(sizeof(*rpl5), 16));
2224 		rpl5 = (void *)rpl;
2225 		INIT_TP_WR(rpl5, ep->hwtid);
2226 	} else {
2227 		skb_trim(skb, sizeof(*rpl));
2228 		INIT_TP_WR(rpl, ep->hwtid);
2229 	}
2230 	OPCODE_TID(rpl) = cpu_to_be32(MK_OPCODE_TID(CPL_PASS_ACCEPT_RPL,
2231 						    ep->hwtid));
2232 
2233 	best_mtu(ep->com.dev->rdev.lldi.mtus, ep->mtu, &mtu_idx,
2234 		 enable_tcp_timestamps && req->tcpopt.tstamp,
2235 		 (AF_INET == ep->com.remote_addr.ss_family) ? 0 : 1);
2236 	wscale = compute_wscale(rcv_win);
2237 
2238 	/*
2239 	 * Specify the largest window that will fit in opt0. The
2240 	 * remainder will be specified in the rx_data_ack.
2241 	 */
2242 	win = ep->rcv_win >> 10;
2243 	if (win > RCV_BUFSIZ_M)
2244 		win = RCV_BUFSIZ_M;
2245 	opt0 = (nocong ? NO_CONG_F : 0) |
2246 	       KEEP_ALIVE_F |
2247 	       DELACK_F |
2248 	       WND_SCALE_V(wscale) |
2249 	       MSS_IDX_V(mtu_idx) |
2250 	       L2T_IDX_V(ep->l2t->idx) |
2251 	       TX_CHAN_V(ep->tx_chan) |
2252 	       SMAC_SEL_V(ep->smac_idx) |
2253 	       DSCP_V(ep->tos >> 2) |
2254 	       ULP_MODE_V(ULP_MODE_TCPDDP) |
2255 	       RCV_BUFSIZ_V(win);
2256 	opt2 = RX_CHANNEL_V(0) |
2257 	       RSS_QUEUE_VALID_F | RSS_QUEUE_V(ep->rss_qid);
2258 
2259 	if (enable_tcp_timestamps && req->tcpopt.tstamp)
2260 		opt2 |= TSTAMPS_EN_F;
2261 	if (enable_tcp_sack && req->tcpopt.sack)
2262 		opt2 |= SACK_EN_F;
2263 	if (wscale && enable_tcp_window_scaling)
2264 		opt2 |= WND_SCALE_EN_F;
2265 	if (enable_ecn) {
2266 		const struct tcphdr *tcph;
2267 		u32 hlen = ntohl(req->hdr_len);
2268 
2269 		tcph = (const void *)(req + 1) + ETH_HDR_LEN_G(hlen) +
2270 			IP_HDR_LEN_G(hlen);
2271 		if (tcph->ece && tcph->cwr)
2272 			opt2 |= CCTRL_ECN_V(1);
2273 	}
2274 	if (is_t5(ep->com.dev->rdev.lldi.adapter_type)) {
2275 		u32 isn = (prandom_u32() & ~7UL) - 1;
2276 		opt2 |= T5_OPT_2_VALID_F;
2277 		opt2 |= CONG_CNTRL_V(CONG_ALG_TAHOE);
2278 		opt2 |= T5_ISS_F;
2279 		rpl5 = (void *)rpl;
2280 		memset(&rpl5->iss, 0, roundup(sizeof(*rpl5)-sizeof(*rpl), 16));
2281 		if (peer2peer)
2282 			isn += 4;
2283 		rpl5->iss = cpu_to_be32(isn);
2284 		PDBG("%s iss %u\n", __func__, be32_to_cpu(rpl5->iss));
2285 	}
2286 
2287 	rpl->opt0 = cpu_to_be64(opt0);
2288 	rpl->opt2 = cpu_to_be32(opt2);
2289 	set_wr_txq(skb, CPL_PRIORITY_SETUP, ep->ctrlq_idx);
2290 	t4_set_arp_err_handler(skb, NULL, arp_failure_discard);
2291 	c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
2292 
2293 	return;
2294 }
2295 
2296 static void reject_cr(struct c4iw_dev *dev, u32 hwtid, struct sk_buff *skb)
2297 {
2298 	PDBG("%s c4iw_dev %p tid %u\n", __func__, dev, hwtid);
2299 	BUG_ON(skb_cloned(skb));
2300 	skb_trim(skb, sizeof(struct cpl_tid_release));
2301 	release_tid(&dev->rdev, hwtid, skb);
2302 	return;
2303 }
2304 
2305 static void get_4tuple(struct cpl_pass_accept_req *req, int *iptype,
2306 		       __u8 *local_ip, __u8 *peer_ip,
2307 		       __be16 *local_port, __be16 *peer_port)
2308 {
2309 	int eth_len = ETH_HDR_LEN_G(be32_to_cpu(req->hdr_len));
2310 	int ip_len = IP_HDR_LEN_G(be32_to_cpu(req->hdr_len));
2311 	struct iphdr *ip = (struct iphdr *)((u8 *)(req + 1) + eth_len);
2312 	struct ipv6hdr *ip6 = (struct ipv6hdr *)((u8 *)(req + 1) + eth_len);
2313 	struct tcphdr *tcp = (struct tcphdr *)
2314 			     ((u8 *)(req + 1) + eth_len + ip_len);
2315 
2316 	if (ip->version == 4) {
2317 		PDBG("%s saddr 0x%x daddr 0x%x sport %u dport %u\n", __func__,
2318 		     ntohl(ip->saddr), ntohl(ip->daddr), ntohs(tcp->source),
2319 		     ntohs(tcp->dest));
2320 		*iptype = 4;
2321 		memcpy(peer_ip, &ip->saddr, 4);
2322 		memcpy(local_ip, &ip->daddr, 4);
2323 	} else {
2324 		PDBG("%s saddr %pI6 daddr %pI6 sport %u dport %u\n", __func__,
2325 		     ip6->saddr.s6_addr, ip6->daddr.s6_addr, ntohs(tcp->source),
2326 		     ntohs(tcp->dest));
2327 		*iptype = 6;
2328 		memcpy(peer_ip, ip6->saddr.s6_addr, 16);
2329 		memcpy(local_ip, ip6->daddr.s6_addr, 16);
2330 	}
2331 	*peer_port = tcp->source;
2332 	*local_port = tcp->dest;
2333 
2334 	return;
2335 }
2336 
2337 static int pass_accept_req(struct c4iw_dev *dev, struct sk_buff *skb)
2338 {
2339 	struct c4iw_ep *child_ep = NULL, *parent_ep;
2340 	struct cpl_pass_accept_req *req = cplhdr(skb);
2341 	unsigned int stid = PASS_OPEN_TID_G(ntohl(req->tos_stid));
2342 	struct tid_info *t = dev->rdev.lldi.tids;
2343 	unsigned int hwtid = GET_TID(req);
2344 	struct dst_entry *dst;
2345 	__u8 local_ip[16], peer_ip[16];
2346 	__be16 local_port, peer_port;
2347 	struct sockaddr_in6 *sin6;
2348 	int err;
2349 	u16 peer_mss = ntohs(req->tcpopt.mss);
2350 	int iptype;
2351 	unsigned short hdrs;
2352 
2353 	parent_ep = lookup_stid(t, stid);
2354 	if (!parent_ep) {
2355 		PDBG("%s connect request on invalid stid %d\n", __func__, stid);
2356 		goto reject;
2357 	}
2358 
2359 	if (state_read(&parent_ep->com) != LISTEN) {
2360 		printk(KERN_ERR "%s - listening ep not in LISTEN\n",
2361 		       __func__);
2362 		goto reject;
2363 	}
2364 
2365 	get_4tuple(req, &iptype, local_ip, peer_ip, &local_port, &peer_port);
2366 
2367 	/* Find output route */
2368 	if (iptype == 4)  {
2369 		PDBG("%s parent ep %p hwtid %u laddr %pI4 raddr %pI4 lport %d rport %d peer_mss %d\n"
2370 		     , __func__, parent_ep, hwtid,
2371 		     local_ip, peer_ip, ntohs(local_port),
2372 		     ntohs(peer_port), peer_mss);
2373 		dst = find_route(dev, *(__be32 *)local_ip, *(__be32 *)peer_ip,
2374 				 local_port, peer_port,
2375 				 PASS_OPEN_TOS_G(ntohl(req->tos_stid)));
2376 	} else {
2377 		PDBG("%s parent ep %p hwtid %u laddr %pI6 raddr %pI6 lport %d rport %d peer_mss %d\n"
2378 		     , __func__, parent_ep, hwtid,
2379 		     local_ip, peer_ip, ntohs(local_port),
2380 		     ntohs(peer_port), peer_mss);
2381 		dst = find_route6(dev, local_ip, peer_ip, local_port, peer_port,
2382 				  PASS_OPEN_TOS_G(ntohl(req->tos_stid)),
2383 				  ((struct sockaddr_in6 *)
2384 				  &parent_ep->com.local_addr)->sin6_scope_id);
2385 	}
2386 	if (!dst) {
2387 		printk(KERN_ERR MOD "%s - failed to find dst entry!\n",
2388 		       __func__);
2389 		goto reject;
2390 	}
2391 
2392 	child_ep = alloc_ep(sizeof(*child_ep), GFP_KERNEL);
2393 	if (!child_ep) {
2394 		printk(KERN_ERR MOD "%s - failed to allocate ep entry!\n",
2395 		       __func__);
2396 		dst_release(dst);
2397 		goto reject;
2398 	}
2399 
2400 	err = import_ep(child_ep, iptype, peer_ip, dst, dev, false);
2401 	if (err) {
2402 		printk(KERN_ERR MOD "%s - failed to allocate l2t entry!\n",
2403 		       __func__);
2404 		dst_release(dst);
2405 		kfree(child_ep);
2406 		goto reject;
2407 	}
2408 
2409 	hdrs = sizeof(struct iphdr) + sizeof(struct tcphdr) +
2410 	       ((enable_tcp_timestamps && req->tcpopt.tstamp) ? 12 : 0);
2411 	if (peer_mss && child_ep->mtu > (peer_mss + hdrs))
2412 		child_ep->mtu = peer_mss + hdrs;
2413 
2414 	state_set(&child_ep->com, CONNECTING);
2415 	child_ep->com.dev = dev;
2416 	child_ep->com.cm_id = NULL;
2417 
2418 	/*
2419 	 * The mapped_local and mapped_remote addresses get setup with
2420 	 * the actual 4-tuple.  The local address will be based on the
2421 	 * actual local address of the connection, but on the port number
2422 	 * of the parent listening endpoint.  The remote address is
2423 	 * setup based on a query to the IWPM since we don't know what it
2424 	 * originally was before mapping.  If no mapping was done, then
2425 	 * mapped_remote == remote, and mapped_local == local.
2426 	 */
2427 	if (iptype == 4) {
2428 		struct sockaddr_in *sin = (struct sockaddr_in *)
2429 			&child_ep->com.mapped_local_addr;
2430 
2431 		sin->sin_family = PF_INET;
2432 		sin->sin_port = local_port;
2433 		sin->sin_addr.s_addr = *(__be32 *)local_ip;
2434 
2435 		sin = (struct sockaddr_in *)&child_ep->com.local_addr;
2436 		sin->sin_family = PF_INET;
2437 		sin->sin_port = ((struct sockaddr_in *)
2438 				 &parent_ep->com.local_addr)->sin_port;
2439 		sin->sin_addr.s_addr = *(__be32 *)local_ip;
2440 
2441 		sin = (struct sockaddr_in *)&child_ep->com.mapped_remote_addr;
2442 		sin->sin_family = PF_INET;
2443 		sin->sin_port = peer_port;
2444 		sin->sin_addr.s_addr = *(__be32 *)peer_ip;
2445 	} else {
2446 		sin6 = (struct sockaddr_in6 *)&child_ep->com.mapped_local_addr;
2447 		sin6->sin6_family = PF_INET6;
2448 		sin6->sin6_port = local_port;
2449 		memcpy(sin6->sin6_addr.s6_addr, local_ip, 16);
2450 
2451 		sin6 = (struct sockaddr_in6 *)&child_ep->com.local_addr;
2452 		sin6->sin6_family = PF_INET6;
2453 		sin6->sin6_port = ((struct sockaddr_in6 *)
2454 				   &parent_ep->com.local_addr)->sin6_port;
2455 		memcpy(sin6->sin6_addr.s6_addr, local_ip, 16);
2456 
2457 		sin6 = (struct sockaddr_in6 *)&child_ep->com.mapped_remote_addr;
2458 		sin6->sin6_family = PF_INET6;
2459 		sin6->sin6_port = peer_port;
2460 		memcpy(sin6->sin6_addr.s6_addr, peer_ip, 16);
2461 	}
2462 	memcpy(&child_ep->com.remote_addr, &child_ep->com.mapped_remote_addr,
2463 	       sizeof(child_ep->com.remote_addr));
2464 	get_remote_addr(parent_ep, child_ep);
2465 
2466 	c4iw_get_ep(&parent_ep->com);
2467 	child_ep->parent_ep = parent_ep;
2468 	child_ep->tos = PASS_OPEN_TOS_G(ntohl(req->tos_stid));
2469 	child_ep->dst = dst;
2470 	child_ep->hwtid = hwtid;
2471 
2472 	PDBG("%s tx_chan %u smac_idx %u rss_qid %u\n", __func__,
2473 	     child_ep->tx_chan, child_ep->smac_idx, child_ep->rss_qid);
2474 
2475 	init_timer(&child_ep->timer);
2476 	cxgb4_insert_tid(t, child_ep, hwtid);
2477 	insert_handle(dev, &dev->hwtid_idr, child_ep, child_ep->hwtid);
2478 	accept_cr(child_ep, skb, req);
2479 	set_bit(PASS_ACCEPT_REQ, &child_ep->com.history);
2480 	if (iptype == 6) {
2481 		sin6 = (struct sockaddr_in6 *)&child_ep->com.mapped_local_addr;
2482 		cxgb4_clip_get(child_ep->com.dev->rdev.lldi.ports[0],
2483 			       (const u32 *)&sin6->sin6_addr.s6_addr, 1);
2484 	}
2485 	goto out;
2486 reject:
2487 	reject_cr(dev, hwtid, skb);
2488 out:
2489 	return 0;
2490 }
2491 
2492 static int pass_establish(struct c4iw_dev *dev, struct sk_buff *skb)
2493 {
2494 	struct c4iw_ep *ep;
2495 	struct cpl_pass_establish *req = cplhdr(skb);
2496 	struct tid_info *t = dev->rdev.lldi.tids;
2497 	unsigned int tid = GET_TID(req);
2498 
2499 	ep = lookup_tid(t, tid);
2500 	PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
2501 	ep->snd_seq = be32_to_cpu(req->snd_isn);
2502 	ep->rcv_seq = be32_to_cpu(req->rcv_isn);
2503 
2504 	PDBG("%s ep %p hwtid %u tcp_opt 0x%02x\n", __func__, ep, tid,
2505 	     ntohs(req->tcp_opt));
2506 
2507 	set_emss(ep, ntohs(req->tcp_opt));
2508 
2509 	dst_confirm(ep->dst);
2510 	state_set(&ep->com, MPA_REQ_WAIT);
2511 	start_ep_timer(ep);
2512 	send_flowc(ep, skb);
2513 	set_bit(PASS_ESTAB, &ep->com.history);
2514 
2515 	return 0;
2516 }
2517 
2518 static int peer_close(struct c4iw_dev *dev, struct sk_buff *skb)
2519 {
2520 	struct cpl_peer_close *hdr = cplhdr(skb);
2521 	struct c4iw_ep *ep;
2522 	struct c4iw_qp_attributes attrs;
2523 	int disconnect = 1;
2524 	int release = 0;
2525 	struct tid_info *t = dev->rdev.lldi.tids;
2526 	unsigned int tid = GET_TID(hdr);
2527 	int ret;
2528 
2529 	ep = lookup_tid(t, tid);
2530 	PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
2531 	dst_confirm(ep->dst);
2532 
2533 	set_bit(PEER_CLOSE, &ep->com.history);
2534 	mutex_lock(&ep->com.mutex);
2535 	switch (ep->com.state) {
2536 	case MPA_REQ_WAIT:
2537 		__state_set(&ep->com, CLOSING);
2538 		break;
2539 	case MPA_REQ_SENT:
2540 		__state_set(&ep->com, CLOSING);
2541 		connect_reply_upcall(ep, -ECONNRESET);
2542 		break;
2543 	case MPA_REQ_RCVD:
2544 
2545 		/*
2546 		 * We're gonna mark this puppy DEAD, but keep
2547 		 * the reference on it until the ULP accepts or
2548 		 * rejects the CR. Also wake up anyone waiting
2549 		 * in rdma connection migration (see c4iw_accept_cr()).
2550 		 */
2551 		__state_set(&ep->com, CLOSING);
2552 		PDBG("waking up ep %p tid %u\n", ep, ep->hwtid);
2553 		c4iw_wake_up(&ep->com.wr_wait, -ECONNRESET);
2554 		break;
2555 	case MPA_REP_SENT:
2556 		__state_set(&ep->com, CLOSING);
2557 		PDBG("waking up ep %p tid %u\n", ep, ep->hwtid);
2558 		c4iw_wake_up(&ep->com.wr_wait, -ECONNRESET);
2559 		break;
2560 	case FPDU_MODE:
2561 		start_ep_timer(ep);
2562 		__state_set(&ep->com, CLOSING);
2563 		attrs.next_state = C4IW_QP_STATE_CLOSING;
2564 		ret = c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
2565 				       C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
2566 		if (ret != -ECONNRESET) {
2567 			peer_close_upcall(ep);
2568 			disconnect = 1;
2569 		}
2570 		break;
2571 	case ABORTING:
2572 		disconnect = 0;
2573 		break;
2574 	case CLOSING:
2575 		__state_set(&ep->com, MORIBUND);
2576 		disconnect = 0;
2577 		break;
2578 	case MORIBUND:
2579 		(void)stop_ep_timer(ep);
2580 		if (ep->com.cm_id && ep->com.qp) {
2581 			attrs.next_state = C4IW_QP_STATE_IDLE;
2582 			c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
2583 				       C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
2584 		}
2585 		close_complete_upcall(ep, 0);
2586 		__state_set(&ep->com, DEAD);
2587 		release = 1;
2588 		disconnect = 0;
2589 		break;
2590 	case DEAD:
2591 		disconnect = 0;
2592 		break;
2593 	default:
2594 		BUG_ON(1);
2595 	}
2596 	mutex_unlock(&ep->com.mutex);
2597 	if (disconnect)
2598 		c4iw_ep_disconnect(ep, 0, GFP_KERNEL);
2599 	if (release)
2600 		release_ep_resources(ep);
2601 	return 0;
2602 }
2603 
2604 static int peer_abort(struct c4iw_dev *dev, struct sk_buff *skb)
2605 {
2606 	struct cpl_abort_req_rss *req = cplhdr(skb);
2607 	struct c4iw_ep *ep;
2608 	struct cpl_abort_rpl *rpl;
2609 	struct sk_buff *rpl_skb;
2610 	struct c4iw_qp_attributes attrs;
2611 	int ret;
2612 	int release = 0;
2613 	struct tid_info *t = dev->rdev.lldi.tids;
2614 	unsigned int tid = GET_TID(req);
2615 
2616 	ep = lookup_tid(t, tid);
2617 	if (is_neg_adv(req->status)) {
2618 		PDBG("%s Negative advice on abort- tid %u status %d (%s)\n",
2619 		     __func__, ep->hwtid, req->status,
2620 		     neg_adv_str(req->status));
2621 		ep->stats.abort_neg_adv++;
2622 		mutex_lock(&dev->rdev.stats.lock);
2623 		dev->rdev.stats.neg_adv++;
2624 		mutex_unlock(&dev->rdev.stats.lock);
2625 		return 0;
2626 	}
2627 	PDBG("%s ep %p tid %u state %u\n", __func__, ep, ep->hwtid,
2628 	     ep->com.state);
2629 	set_bit(PEER_ABORT, &ep->com.history);
2630 
2631 	/*
2632 	 * Wake up any threads in rdma_init() or rdma_fini().
2633 	 * However, this is not needed if com state is just
2634 	 * MPA_REQ_SENT
2635 	 */
2636 	if (ep->com.state != MPA_REQ_SENT)
2637 		c4iw_wake_up(&ep->com.wr_wait, -ECONNRESET);
2638 
2639 	mutex_lock(&ep->com.mutex);
2640 	switch (ep->com.state) {
2641 	case CONNECTING:
2642 		break;
2643 	case MPA_REQ_WAIT:
2644 		(void)stop_ep_timer(ep);
2645 		break;
2646 	case MPA_REQ_SENT:
2647 		(void)stop_ep_timer(ep);
2648 		if (mpa_rev == 1 || (mpa_rev == 2 && ep->tried_with_mpa_v1))
2649 			connect_reply_upcall(ep, -ECONNRESET);
2650 		else {
2651 			/*
2652 			 * we just don't send notification upwards because we
2653 			 * want to retry with mpa_v1 without upper layers even
2654 			 * knowing it.
2655 			 *
2656 			 * do some housekeeping so as to re-initiate the
2657 			 * connection
2658 			 */
2659 			PDBG("%s: mpa_rev=%d. Retrying with mpav1\n", __func__,
2660 			     mpa_rev);
2661 			ep->retry_with_mpa_v1 = 1;
2662 		}
2663 		break;
2664 	case MPA_REP_SENT:
2665 		break;
2666 	case MPA_REQ_RCVD:
2667 		break;
2668 	case MORIBUND:
2669 	case CLOSING:
2670 		stop_ep_timer(ep);
2671 		/*FALLTHROUGH*/
2672 	case FPDU_MODE:
2673 		if (ep->com.cm_id && ep->com.qp) {
2674 			attrs.next_state = C4IW_QP_STATE_ERROR;
2675 			ret = c4iw_modify_qp(ep->com.qp->rhp,
2676 				     ep->com.qp, C4IW_QP_ATTR_NEXT_STATE,
2677 				     &attrs, 1);
2678 			if (ret)
2679 				printk(KERN_ERR MOD
2680 				       "%s - qp <- error failed!\n",
2681 				       __func__);
2682 		}
2683 		peer_abort_upcall(ep);
2684 		break;
2685 	case ABORTING:
2686 		break;
2687 	case DEAD:
2688 		PDBG("%s PEER_ABORT IN DEAD STATE!!!!\n", __func__);
2689 		mutex_unlock(&ep->com.mutex);
2690 		return 0;
2691 	default:
2692 		BUG_ON(1);
2693 		break;
2694 	}
2695 	dst_confirm(ep->dst);
2696 	if (ep->com.state != ABORTING) {
2697 		__state_set(&ep->com, DEAD);
2698 		/* we don't release if we want to retry with mpa_v1 */
2699 		if (!ep->retry_with_mpa_v1)
2700 			release = 1;
2701 	}
2702 	mutex_unlock(&ep->com.mutex);
2703 
2704 	rpl_skb = get_skb(skb, sizeof(*rpl), GFP_KERNEL);
2705 	if (!rpl_skb) {
2706 		printk(KERN_ERR MOD "%s - cannot allocate skb!\n",
2707 		       __func__);
2708 		release = 1;
2709 		goto out;
2710 	}
2711 	set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
2712 	rpl = (struct cpl_abort_rpl *) skb_put(rpl_skb, sizeof(*rpl));
2713 	INIT_TP_WR(rpl, ep->hwtid);
2714 	OPCODE_TID(rpl) = cpu_to_be32(MK_OPCODE_TID(CPL_ABORT_RPL, ep->hwtid));
2715 	rpl->cmd = CPL_ABORT_NO_RST;
2716 	c4iw_ofld_send(&ep->com.dev->rdev, rpl_skb);
2717 out:
2718 	if (release)
2719 		release_ep_resources(ep);
2720 	else if (ep->retry_with_mpa_v1) {
2721 		if (ep->com.remote_addr.ss_family == AF_INET6) {
2722 			struct sockaddr_in6 *sin6 =
2723 					(struct sockaddr_in6 *)
2724 					&ep->com.mapped_local_addr;
2725 			cxgb4_clip_release(
2726 					ep->com.dev->rdev.lldi.ports[0],
2727 					(const u32 *)&sin6->sin6_addr.s6_addr,
2728 					1);
2729 		}
2730 		remove_handle(ep->com.dev, &ep->com.dev->hwtid_idr, ep->hwtid);
2731 		cxgb4_remove_tid(ep->com.dev->rdev.lldi.tids, 0, ep->hwtid);
2732 		dst_release(ep->dst);
2733 		cxgb4_l2t_release(ep->l2t);
2734 		c4iw_reconnect(ep);
2735 	}
2736 
2737 	return 0;
2738 }
2739 
2740 static int close_con_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
2741 {
2742 	struct c4iw_ep *ep;
2743 	struct c4iw_qp_attributes attrs;
2744 	struct cpl_close_con_rpl *rpl = cplhdr(skb);
2745 	int release = 0;
2746 	struct tid_info *t = dev->rdev.lldi.tids;
2747 	unsigned int tid = GET_TID(rpl);
2748 
2749 	ep = lookup_tid(t, tid);
2750 
2751 	PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
2752 	BUG_ON(!ep);
2753 
2754 	/* The cm_id may be null if we failed to connect */
2755 	mutex_lock(&ep->com.mutex);
2756 	switch (ep->com.state) {
2757 	case CLOSING:
2758 		__state_set(&ep->com, MORIBUND);
2759 		break;
2760 	case MORIBUND:
2761 		(void)stop_ep_timer(ep);
2762 		if ((ep->com.cm_id) && (ep->com.qp)) {
2763 			attrs.next_state = C4IW_QP_STATE_IDLE;
2764 			c4iw_modify_qp(ep->com.qp->rhp,
2765 					     ep->com.qp,
2766 					     C4IW_QP_ATTR_NEXT_STATE,
2767 					     &attrs, 1);
2768 		}
2769 		close_complete_upcall(ep, 0);
2770 		__state_set(&ep->com, DEAD);
2771 		release = 1;
2772 		break;
2773 	case ABORTING:
2774 	case DEAD:
2775 		break;
2776 	default:
2777 		BUG_ON(1);
2778 		break;
2779 	}
2780 	mutex_unlock(&ep->com.mutex);
2781 	if (release)
2782 		release_ep_resources(ep);
2783 	return 0;
2784 }
2785 
2786 static int terminate(struct c4iw_dev *dev, struct sk_buff *skb)
2787 {
2788 	struct cpl_rdma_terminate *rpl = cplhdr(skb);
2789 	struct tid_info *t = dev->rdev.lldi.tids;
2790 	unsigned int tid = GET_TID(rpl);
2791 	struct c4iw_ep *ep;
2792 	struct c4iw_qp_attributes attrs;
2793 
2794 	ep = lookup_tid(t, tid);
2795 	BUG_ON(!ep);
2796 
2797 	if (ep && ep->com.qp) {
2798 		printk(KERN_WARNING MOD "TERM received tid %u qpid %u\n", tid,
2799 		       ep->com.qp->wq.sq.qid);
2800 		attrs.next_state = C4IW_QP_STATE_TERMINATE;
2801 		c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
2802 			       C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
2803 	} else
2804 		printk(KERN_WARNING MOD "TERM received tid %u no ep/qp\n", tid);
2805 
2806 	return 0;
2807 }
2808 
2809 /*
2810  * Upcall from the adapter indicating data has been transmitted.
2811  * For us its just the single MPA request or reply.  We can now free
2812  * the skb holding the mpa message.
2813  */
2814 static int fw4_ack(struct c4iw_dev *dev, struct sk_buff *skb)
2815 {
2816 	struct c4iw_ep *ep;
2817 	struct cpl_fw4_ack *hdr = cplhdr(skb);
2818 	u8 credits = hdr->credits;
2819 	unsigned int tid = GET_TID(hdr);
2820 	struct tid_info *t = dev->rdev.lldi.tids;
2821 
2822 
2823 	ep = lookup_tid(t, tid);
2824 	PDBG("%s ep %p tid %u credits %u\n", __func__, ep, ep->hwtid, credits);
2825 	if (credits == 0) {
2826 		PDBG("%s 0 credit ack ep %p tid %u state %u\n",
2827 		     __func__, ep, ep->hwtid, state_read(&ep->com));
2828 		return 0;
2829 	}
2830 
2831 	dst_confirm(ep->dst);
2832 	if (ep->mpa_skb) {
2833 		PDBG("%s last streaming msg ack ep %p tid %u state %u "
2834 		     "initiator %u freeing skb\n", __func__, ep, ep->hwtid,
2835 		     state_read(&ep->com), ep->mpa_attr.initiator ? 1 : 0);
2836 		kfree_skb(ep->mpa_skb);
2837 		ep->mpa_skb = NULL;
2838 	}
2839 	return 0;
2840 }
2841 
2842 int c4iw_reject_cr(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
2843 {
2844 	int err = 0;
2845 	int disconnect = 0;
2846 	struct c4iw_ep *ep = to_ep(cm_id);
2847 	PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
2848 
2849 	mutex_lock(&ep->com.mutex);
2850 	if (ep->com.state == DEAD) {
2851 		mutex_unlock(&ep->com.mutex);
2852 		c4iw_put_ep(&ep->com);
2853 		return -ECONNRESET;
2854 	}
2855 	set_bit(ULP_REJECT, &ep->com.history);
2856 	BUG_ON(ep->com.state != MPA_REQ_RCVD);
2857 	if (mpa_rev == 0)
2858 		abort_connection(ep, NULL, GFP_KERNEL);
2859 	else {
2860 		err = send_mpa_reject(ep, pdata, pdata_len);
2861 		disconnect = 1;
2862 	}
2863 	mutex_unlock(&ep->com.mutex);
2864 	if (disconnect)
2865 		err = c4iw_ep_disconnect(ep, 0, GFP_KERNEL);
2866 	c4iw_put_ep(&ep->com);
2867 	return 0;
2868 }
2869 
2870 int c4iw_accept_cr(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2871 {
2872 	int err;
2873 	struct c4iw_qp_attributes attrs;
2874 	enum c4iw_qp_attr_mask mask;
2875 	struct c4iw_ep *ep = to_ep(cm_id);
2876 	struct c4iw_dev *h = to_c4iw_dev(cm_id->device);
2877 	struct c4iw_qp *qp = get_qhp(h, conn_param->qpn);
2878 
2879 	PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
2880 
2881 	mutex_lock(&ep->com.mutex);
2882 	if (ep->com.state == DEAD) {
2883 		err = -ECONNRESET;
2884 		goto err;
2885 	}
2886 
2887 	BUG_ON(ep->com.state != MPA_REQ_RCVD);
2888 	BUG_ON(!qp);
2889 
2890 	set_bit(ULP_ACCEPT, &ep->com.history);
2891 	if ((conn_param->ord > cur_max_read_depth(ep->com.dev)) ||
2892 	    (conn_param->ird > cur_max_read_depth(ep->com.dev))) {
2893 		abort_connection(ep, NULL, GFP_KERNEL);
2894 		err = -EINVAL;
2895 		goto err;
2896 	}
2897 
2898 	if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn) {
2899 		if (conn_param->ord > ep->ird) {
2900 			if (RELAXED_IRD_NEGOTIATION) {
2901 				ep->ord = ep->ird;
2902 			} else {
2903 				ep->ird = conn_param->ird;
2904 				ep->ord = conn_param->ord;
2905 				send_mpa_reject(ep, conn_param->private_data,
2906 						conn_param->private_data_len);
2907 				abort_connection(ep, NULL, GFP_KERNEL);
2908 				err = -ENOMEM;
2909 				goto err;
2910 			}
2911 		}
2912 		if (conn_param->ird < ep->ord) {
2913 			if (RELAXED_IRD_NEGOTIATION &&
2914 			    ep->ord <= h->rdev.lldi.max_ordird_qp) {
2915 				conn_param->ird = ep->ord;
2916 			} else {
2917 				abort_connection(ep, NULL, GFP_KERNEL);
2918 				err = -ENOMEM;
2919 				goto err;
2920 			}
2921 		}
2922 	}
2923 	ep->ird = conn_param->ird;
2924 	ep->ord = conn_param->ord;
2925 
2926 	if (ep->mpa_attr.version == 1) {
2927 		if (peer2peer && ep->ird == 0)
2928 			ep->ird = 1;
2929 	} else {
2930 		if (peer2peer &&
2931 		    (ep->mpa_attr.p2p_type != FW_RI_INIT_P2PTYPE_DISABLED) &&
2932 		    (p2p_type == FW_RI_INIT_P2PTYPE_READ_REQ) && ep->ord == 0)
2933 			ep->ird = 1;
2934 	}
2935 
2936 	PDBG("%s %d ird %d ord %d\n", __func__, __LINE__, ep->ird, ep->ord);
2937 
2938 	cm_id->add_ref(cm_id);
2939 	ep->com.cm_id = cm_id;
2940 	ep->com.qp = qp;
2941 	ref_qp(ep);
2942 
2943 	/* bind QP to EP and move to RTS */
2944 	attrs.mpa_attr = ep->mpa_attr;
2945 	attrs.max_ird = ep->ird;
2946 	attrs.max_ord = ep->ord;
2947 	attrs.llp_stream_handle = ep;
2948 	attrs.next_state = C4IW_QP_STATE_RTS;
2949 
2950 	/* bind QP and TID with INIT_WR */
2951 	mask = C4IW_QP_ATTR_NEXT_STATE |
2952 			     C4IW_QP_ATTR_LLP_STREAM_HANDLE |
2953 			     C4IW_QP_ATTR_MPA_ATTR |
2954 			     C4IW_QP_ATTR_MAX_IRD |
2955 			     C4IW_QP_ATTR_MAX_ORD;
2956 
2957 	err = c4iw_modify_qp(ep->com.qp->rhp,
2958 			     ep->com.qp, mask, &attrs, 1);
2959 	if (err)
2960 		goto err1;
2961 	err = send_mpa_reply(ep, conn_param->private_data,
2962 			     conn_param->private_data_len);
2963 	if (err)
2964 		goto err1;
2965 
2966 	__state_set(&ep->com, FPDU_MODE);
2967 	established_upcall(ep);
2968 	mutex_unlock(&ep->com.mutex);
2969 	c4iw_put_ep(&ep->com);
2970 	return 0;
2971 err1:
2972 	ep->com.cm_id = NULL;
2973 	abort_connection(ep, NULL, GFP_KERNEL);
2974 	cm_id->rem_ref(cm_id);
2975 err:
2976 	mutex_unlock(&ep->com.mutex);
2977 	c4iw_put_ep(&ep->com);
2978 	return err;
2979 }
2980 
2981 static int pick_local_ipaddrs(struct c4iw_dev *dev, struct iw_cm_id *cm_id)
2982 {
2983 	struct in_device *ind;
2984 	int found = 0;
2985 	struct sockaddr_in *laddr = (struct sockaddr_in *)&cm_id->local_addr;
2986 	struct sockaddr_in *raddr = (struct sockaddr_in *)&cm_id->remote_addr;
2987 
2988 	ind = in_dev_get(dev->rdev.lldi.ports[0]);
2989 	if (!ind)
2990 		return -EADDRNOTAVAIL;
2991 	for_primary_ifa(ind) {
2992 		laddr->sin_addr.s_addr = ifa->ifa_address;
2993 		raddr->sin_addr.s_addr = ifa->ifa_address;
2994 		found = 1;
2995 		break;
2996 	}
2997 	endfor_ifa(ind);
2998 	in_dev_put(ind);
2999 	return found ? 0 : -EADDRNOTAVAIL;
3000 }
3001 
3002 static int get_lladdr(struct net_device *dev, struct in6_addr *addr,
3003 		      unsigned char banned_flags)
3004 {
3005 	struct inet6_dev *idev;
3006 	int err = -EADDRNOTAVAIL;
3007 
3008 	rcu_read_lock();
3009 	idev = __in6_dev_get(dev);
3010 	if (idev != NULL) {
3011 		struct inet6_ifaddr *ifp;
3012 
3013 		read_lock_bh(&idev->lock);
3014 		list_for_each_entry(ifp, &idev->addr_list, if_list) {
3015 			if (ifp->scope == IFA_LINK &&
3016 			    !(ifp->flags & banned_flags)) {
3017 				memcpy(addr, &ifp->addr, 16);
3018 				err = 0;
3019 				break;
3020 			}
3021 		}
3022 		read_unlock_bh(&idev->lock);
3023 	}
3024 	rcu_read_unlock();
3025 	return err;
3026 }
3027 
3028 static int pick_local_ip6addrs(struct c4iw_dev *dev, struct iw_cm_id *cm_id)
3029 {
3030 	struct in6_addr uninitialized_var(addr);
3031 	struct sockaddr_in6 *la6 = (struct sockaddr_in6 *)&cm_id->local_addr;
3032 	struct sockaddr_in6 *ra6 = (struct sockaddr_in6 *)&cm_id->remote_addr;
3033 
3034 	if (!get_lladdr(dev->rdev.lldi.ports[0], &addr, IFA_F_TENTATIVE)) {
3035 		memcpy(la6->sin6_addr.s6_addr, &addr, 16);
3036 		memcpy(ra6->sin6_addr.s6_addr, &addr, 16);
3037 		return 0;
3038 	}
3039 	return -EADDRNOTAVAIL;
3040 }
3041 
3042 int c4iw_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
3043 {
3044 	struct c4iw_dev *dev = to_c4iw_dev(cm_id->device);
3045 	struct c4iw_ep *ep;
3046 	int err = 0;
3047 	struct sockaddr_in *laddr;
3048 	struct sockaddr_in *raddr;
3049 	struct sockaddr_in6 *laddr6;
3050 	struct sockaddr_in6 *raddr6;
3051 	struct iwpm_dev_data pm_reg_msg;
3052 	struct iwpm_sa_data pm_msg;
3053 	__u8 *ra;
3054 	int iptype;
3055 	int iwpm_err = 0;
3056 
3057 	if ((conn_param->ord > cur_max_read_depth(dev)) ||
3058 	    (conn_param->ird > cur_max_read_depth(dev))) {
3059 		err = -EINVAL;
3060 		goto out;
3061 	}
3062 	ep = alloc_ep(sizeof(*ep), GFP_KERNEL);
3063 	if (!ep) {
3064 		printk(KERN_ERR MOD "%s - cannot alloc ep.\n", __func__);
3065 		err = -ENOMEM;
3066 		goto out;
3067 	}
3068 	init_timer(&ep->timer);
3069 	ep->plen = conn_param->private_data_len;
3070 	if (ep->plen)
3071 		memcpy(ep->mpa_pkt + sizeof(struct mpa_message),
3072 		       conn_param->private_data, ep->plen);
3073 	ep->ird = conn_param->ird;
3074 	ep->ord = conn_param->ord;
3075 
3076 	if (peer2peer && ep->ord == 0)
3077 		ep->ord = 1;
3078 
3079 	cm_id->add_ref(cm_id);
3080 	ep->com.dev = dev;
3081 	ep->com.cm_id = cm_id;
3082 	ep->com.qp = get_qhp(dev, conn_param->qpn);
3083 	if (!ep->com.qp) {
3084 		PDBG("%s qpn 0x%x not found!\n", __func__, conn_param->qpn);
3085 		err = -EINVAL;
3086 		goto fail1;
3087 	}
3088 	ref_qp(ep);
3089 	PDBG("%s qpn 0x%x qp %p cm_id %p\n", __func__, conn_param->qpn,
3090 	     ep->com.qp, cm_id);
3091 
3092 	/*
3093 	 * Allocate an active TID to initiate a TCP connection.
3094 	 */
3095 	ep->atid = cxgb4_alloc_atid(dev->rdev.lldi.tids, ep);
3096 	if (ep->atid == -1) {
3097 		printk(KERN_ERR MOD "%s - cannot alloc atid.\n", __func__);
3098 		err = -ENOMEM;
3099 		goto fail1;
3100 	}
3101 	insert_handle(dev, &dev->atid_idr, ep, ep->atid);
3102 
3103 	memcpy(&ep->com.local_addr, &cm_id->local_addr,
3104 	       sizeof(ep->com.local_addr));
3105 	memcpy(&ep->com.remote_addr, &cm_id->remote_addr,
3106 	       sizeof(ep->com.remote_addr));
3107 
3108 	/* No port mapper available, go with the specified peer information */
3109 	memcpy(&ep->com.mapped_local_addr, &cm_id->local_addr,
3110 	       sizeof(ep->com.mapped_local_addr));
3111 	memcpy(&ep->com.mapped_remote_addr, &cm_id->remote_addr,
3112 	       sizeof(ep->com.mapped_remote_addr));
3113 
3114 	c4iw_form_reg_msg(dev, &pm_reg_msg);
3115 	iwpm_err = iwpm_register_pid(&pm_reg_msg, RDMA_NL_C4IW);
3116 	if (iwpm_err) {
3117 		PDBG("%s: Port Mapper reg pid fail (err = %d).\n",
3118 			__func__, iwpm_err);
3119 	}
3120 	if (iwpm_valid_pid() && !iwpm_err) {
3121 		c4iw_form_pm_msg(ep, &pm_msg);
3122 		iwpm_err = iwpm_add_and_query_mapping(&pm_msg, RDMA_NL_C4IW);
3123 		if (iwpm_err)
3124 			PDBG("%s: Port Mapper query fail (err = %d).\n",
3125 				__func__, iwpm_err);
3126 		else
3127 			c4iw_record_pm_msg(ep, &pm_msg);
3128 	}
3129 	if (iwpm_create_mapinfo(&ep->com.local_addr,
3130 				&ep->com.mapped_local_addr, RDMA_NL_C4IW)) {
3131 		iwpm_remove_mapping(&ep->com.local_addr, RDMA_NL_C4IW);
3132 		err = -ENOMEM;
3133 		goto fail1;
3134 	}
3135 	print_addr(&ep->com, __func__, "add_query/create_mapinfo");
3136 	set_bit(RELEASE_MAPINFO, &ep->com.flags);
3137 
3138 	laddr = (struct sockaddr_in *)&ep->com.mapped_local_addr;
3139 	raddr = (struct sockaddr_in *)&ep->com.mapped_remote_addr;
3140 	laddr6 = (struct sockaddr_in6 *)&ep->com.mapped_local_addr;
3141 	raddr6 = (struct sockaddr_in6 *) &ep->com.mapped_remote_addr;
3142 
3143 	if (cm_id->remote_addr.ss_family == AF_INET) {
3144 		iptype = 4;
3145 		ra = (__u8 *)&raddr->sin_addr;
3146 
3147 		/*
3148 		 * Handle loopback requests to INADDR_ANY.
3149 		 */
3150 		if ((__force int)raddr->sin_addr.s_addr == INADDR_ANY) {
3151 			err = pick_local_ipaddrs(dev, cm_id);
3152 			if (err)
3153 				goto fail1;
3154 		}
3155 
3156 		/* find a route */
3157 		PDBG("%s saddr %pI4 sport 0x%x raddr %pI4 rport 0x%x\n",
3158 		     __func__, &laddr->sin_addr, ntohs(laddr->sin_port),
3159 		     ra, ntohs(raddr->sin_port));
3160 		ep->dst = find_route(dev, laddr->sin_addr.s_addr,
3161 				     raddr->sin_addr.s_addr, laddr->sin_port,
3162 				     raddr->sin_port, 0);
3163 	} else {
3164 		iptype = 6;
3165 		ra = (__u8 *)&raddr6->sin6_addr;
3166 
3167 		/*
3168 		 * Handle loopback requests to INADDR_ANY.
3169 		 */
3170 		if (ipv6_addr_type(&raddr6->sin6_addr) == IPV6_ADDR_ANY) {
3171 			err = pick_local_ip6addrs(dev, cm_id);
3172 			if (err)
3173 				goto fail1;
3174 		}
3175 
3176 		/* find a route */
3177 		PDBG("%s saddr %pI6 sport 0x%x raddr %pI6 rport 0x%x\n",
3178 		     __func__, laddr6->sin6_addr.s6_addr,
3179 		     ntohs(laddr6->sin6_port),
3180 		     raddr6->sin6_addr.s6_addr, ntohs(raddr6->sin6_port));
3181 		ep->dst = find_route6(dev, laddr6->sin6_addr.s6_addr,
3182 				      raddr6->sin6_addr.s6_addr,
3183 				      laddr6->sin6_port, raddr6->sin6_port, 0,
3184 				      raddr6->sin6_scope_id);
3185 	}
3186 	if (!ep->dst) {
3187 		printk(KERN_ERR MOD "%s - cannot find route.\n", __func__);
3188 		err = -EHOSTUNREACH;
3189 		goto fail2;
3190 	}
3191 
3192 	err = import_ep(ep, iptype, ra, ep->dst, ep->com.dev, true);
3193 	if (err) {
3194 		printk(KERN_ERR MOD "%s - cannot alloc l2e.\n", __func__);
3195 		goto fail3;
3196 	}
3197 
3198 	PDBG("%s txq_idx %u tx_chan %u smac_idx %u rss_qid %u l2t_idx %u\n",
3199 		__func__, ep->txq_idx, ep->tx_chan, ep->smac_idx, ep->rss_qid,
3200 		ep->l2t->idx);
3201 
3202 	state_set(&ep->com, CONNECTING);
3203 	ep->tos = 0;
3204 
3205 	/* send connect request to rnic */
3206 	err = send_connect(ep);
3207 	if (!err)
3208 		goto out;
3209 
3210 	cxgb4_l2t_release(ep->l2t);
3211 fail3:
3212 	dst_release(ep->dst);
3213 fail2:
3214 	remove_handle(ep->com.dev, &ep->com.dev->atid_idr, ep->atid);
3215 	cxgb4_free_atid(ep->com.dev->rdev.lldi.tids, ep->atid);
3216 fail1:
3217 	cm_id->rem_ref(cm_id);
3218 	c4iw_put_ep(&ep->com);
3219 out:
3220 	return err;
3221 }
3222 
3223 static int create_server6(struct c4iw_dev *dev, struct c4iw_listen_ep *ep)
3224 {
3225 	int err;
3226 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)
3227 				    &ep->com.mapped_local_addr;
3228 
3229 	c4iw_init_wr_wait(&ep->com.wr_wait);
3230 	err = cxgb4_create_server6(ep->com.dev->rdev.lldi.ports[0],
3231 				   ep->stid, &sin6->sin6_addr,
3232 				   sin6->sin6_port,
3233 				   ep->com.dev->rdev.lldi.rxq_ids[0]);
3234 	if (!err)
3235 		err = c4iw_wait_for_reply(&ep->com.dev->rdev,
3236 					  &ep->com.wr_wait,
3237 					  0, 0, __func__);
3238 	else if (err > 0)
3239 		err = net_xmit_errno(err);
3240 	if (err)
3241 		pr_err("cxgb4_create_server6/filter failed err %d stid %d laddr %pI6 lport %d\n",
3242 		       err, ep->stid,
3243 		       sin6->sin6_addr.s6_addr, ntohs(sin6->sin6_port));
3244 	else
3245 		cxgb4_clip_get(ep->com.dev->rdev.lldi.ports[0],
3246 			       (const u32 *)&sin6->sin6_addr.s6_addr, 1);
3247 	return err;
3248 }
3249 
3250 static int create_server4(struct c4iw_dev *dev, struct c4iw_listen_ep *ep)
3251 {
3252 	int err;
3253 	struct sockaddr_in *sin = (struct sockaddr_in *)
3254 				  &ep->com.mapped_local_addr;
3255 
3256 	if (dev->rdev.lldi.enable_fw_ofld_conn) {
3257 		do {
3258 			err = cxgb4_create_server_filter(
3259 				ep->com.dev->rdev.lldi.ports[0], ep->stid,
3260 				sin->sin_addr.s_addr, sin->sin_port, 0,
3261 				ep->com.dev->rdev.lldi.rxq_ids[0], 0, 0);
3262 			if (err == -EBUSY) {
3263 				set_current_state(TASK_UNINTERRUPTIBLE);
3264 				schedule_timeout(usecs_to_jiffies(100));
3265 			}
3266 		} while (err == -EBUSY);
3267 	} else {
3268 		c4iw_init_wr_wait(&ep->com.wr_wait);
3269 		err = cxgb4_create_server(ep->com.dev->rdev.lldi.ports[0],
3270 				ep->stid, sin->sin_addr.s_addr, sin->sin_port,
3271 				0, ep->com.dev->rdev.lldi.rxq_ids[0]);
3272 		if (!err)
3273 			err = c4iw_wait_for_reply(&ep->com.dev->rdev,
3274 						  &ep->com.wr_wait,
3275 						  0, 0, __func__);
3276 		else if (err > 0)
3277 			err = net_xmit_errno(err);
3278 	}
3279 	if (err)
3280 		pr_err("cxgb4_create_server/filter failed err %d stid %d laddr %pI4 lport %d\n"
3281 		       , err, ep->stid,
3282 		       &sin->sin_addr, ntohs(sin->sin_port));
3283 	return err;
3284 }
3285 
3286 int c4iw_create_listen(struct iw_cm_id *cm_id, int backlog)
3287 {
3288 	int err = 0;
3289 	struct c4iw_dev *dev = to_c4iw_dev(cm_id->device);
3290 	struct c4iw_listen_ep *ep;
3291 	struct iwpm_dev_data pm_reg_msg;
3292 	struct iwpm_sa_data pm_msg;
3293 	int iwpm_err = 0;
3294 
3295 	might_sleep();
3296 
3297 	ep = alloc_ep(sizeof(*ep), GFP_KERNEL);
3298 	if (!ep) {
3299 		printk(KERN_ERR MOD "%s - cannot alloc ep.\n", __func__);
3300 		err = -ENOMEM;
3301 		goto fail1;
3302 	}
3303 	PDBG("%s ep %p\n", __func__, ep);
3304 	cm_id->add_ref(cm_id);
3305 	ep->com.cm_id = cm_id;
3306 	ep->com.dev = dev;
3307 	ep->backlog = backlog;
3308 	memcpy(&ep->com.local_addr, &cm_id->local_addr,
3309 	       sizeof(ep->com.local_addr));
3310 
3311 	/*
3312 	 * Allocate a server TID.
3313 	 */
3314 	if (dev->rdev.lldi.enable_fw_ofld_conn &&
3315 	    ep->com.local_addr.ss_family == AF_INET)
3316 		ep->stid = cxgb4_alloc_sftid(dev->rdev.lldi.tids,
3317 					     cm_id->local_addr.ss_family, ep);
3318 	else
3319 		ep->stid = cxgb4_alloc_stid(dev->rdev.lldi.tids,
3320 					    cm_id->local_addr.ss_family, ep);
3321 
3322 	if (ep->stid == -1) {
3323 		printk(KERN_ERR MOD "%s - cannot alloc stid.\n", __func__);
3324 		err = -ENOMEM;
3325 		goto fail2;
3326 	}
3327 	insert_handle(dev, &dev->stid_idr, ep, ep->stid);
3328 
3329 	/* No port mapper available, go with the specified info */
3330 	memcpy(&ep->com.mapped_local_addr, &cm_id->local_addr,
3331 	       sizeof(ep->com.mapped_local_addr));
3332 
3333 	c4iw_form_reg_msg(dev, &pm_reg_msg);
3334 	iwpm_err = iwpm_register_pid(&pm_reg_msg, RDMA_NL_C4IW);
3335 	if (iwpm_err) {
3336 		PDBG("%s: Port Mapper reg pid fail (err = %d).\n",
3337 			__func__, iwpm_err);
3338 	}
3339 	if (iwpm_valid_pid() && !iwpm_err) {
3340 		memcpy(&pm_msg.loc_addr, &ep->com.local_addr,
3341 				sizeof(ep->com.local_addr));
3342 		iwpm_err = iwpm_add_mapping(&pm_msg, RDMA_NL_C4IW);
3343 		if (iwpm_err)
3344 			PDBG("%s: Port Mapper query fail (err = %d).\n",
3345 				__func__, iwpm_err);
3346 		else
3347 			memcpy(&ep->com.mapped_local_addr,
3348 				&pm_msg.mapped_loc_addr,
3349 				sizeof(ep->com.mapped_local_addr));
3350 	}
3351 	if (iwpm_create_mapinfo(&ep->com.local_addr,
3352 				&ep->com.mapped_local_addr, RDMA_NL_C4IW)) {
3353 		err = -ENOMEM;
3354 		goto fail3;
3355 	}
3356 	print_addr(&ep->com, __func__, "add_mapping/create_mapinfo");
3357 
3358 	set_bit(RELEASE_MAPINFO, &ep->com.flags);
3359 	state_set(&ep->com, LISTEN);
3360 	if (ep->com.local_addr.ss_family == AF_INET)
3361 		err = create_server4(dev, ep);
3362 	else
3363 		err = create_server6(dev, ep);
3364 	if (!err) {
3365 		cm_id->provider_data = ep;
3366 		goto out;
3367 	}
3368 
3369 fail3:
3370 	cxgb4_free_stid(ep->com.dev->rdev.lldi.tids, ep->stid,
3371 			ep->com.local_addr.ss_family);
3372 fail2:
3373 	cm_id->rem_ref(cm_id);
3374 	c4iw_put_ep(&ep->com);
3375 fail1:
3376 out:
3377 	return err;
3378 }
3379 
3380 int c4iw_destroy_listen(struct iw_cm_id *cm_id)
3381 {
3382 	int err;
3383 	struct c4iw_listen_ep *ep = to_listen_ep(cm_id);
3384 
3385 	PDBG("%s ep %p\n", __func__, ep);
3386 
3387 	might_sleep();
3388 	state_set(&ep->com, DEAD);
3389 	if (ep->com.dev->rdev.lldi.enable_fw_ofld_conn &&
3390 	    ep->com.local_addr.ss_family == AF_INET) {
3391 		err = cxgb4_remove_server_filter(
3392 			ep->com.dev->rdev.lldi.ports[0], ep->stid,
3393 			ep->com.dev->rdev.lldi.rxq_ids[0], 0);
3394 	} else {
3395 		struct sockaddr_in6 *sin6;
3396 		c4iw_init_wr_wait(&ep->com.wr_wait);
3397 		err = cxgb4_remove_server(
3398 				ep->com.dev->rdev.lldi.ports[0], ep->stid,
3399 				ep->com.dev->rdev.lldi.rxq_ids[0], 0);
3400 		if (err)
3401 			goto done;
3402 		err = c4iw_wait_for_reply(&ep->com.dev->rdev, &ep->com.wr_wait,
3403 					  0, 0, __func__);
3404 		sin6 = (struct sockaddr_in6 *)&ep->com.mapped_local_addr;
3405 		cxgb4_clip_release(ep->com.dev->rdev.lldi.ports[0],
3406 				   (const u32 *)&sin6->sin6_addr.s6_addr, 1);
3407 	}
3408 	remove_handle(ep->com.dev, &ep->com.dev->stid_idr, ep->stid);
3409 	cxgb4_free_stid(ep->com.dev->rdev.lldi.tids, ep->stid,
3410 			ep->com.local_addr.ss_family);
3411 done:
3412 	cm_id->rem_ref(cm_id);
3413 	c4iw_put_ep(&ep->com);
3414 	return err;
3415 }
3416 
3417 int c4iw_ep_disconnect(struct c4iw_ep *ep, int abrupt, gfp_t gfp)
3418 {
3419 	int ret = 0;
3420 	int close = 0;
3421 	int fatal = 0;
3422 	struct c4iw_rdev *rdev;
3423 
3424 	mutex_lock(&ep->com.mutex);
3425 
3426 	PDBG("%s ep %p state %s, abrupt %d\n", __func__, ep,
3427 	     states[ep->com.state], abrupt);
3428 
3429 	rdev = &ep->com.dev->rdev;
3430 	if (c4iw_fatal_error(rdev)) {
3431 		fatal = 1;
3432 		close_complete_upcall(ep, -EIO);
3433 		ep->com.state = DEAD;
3434 	}
3435 	switch (ep->com.state) {
3436 	case MPA_REQ_WAIT:
3437 	case MPA_REQ_SENT:
3438 	case MPA_REQ_RCVD:
3439 	case MPA_REP_SENT:
3440 	case FPDU_MODE:
3441 		close = 1;
3442 		if (abrupt)
3443 			ep->com.state = ABORTING;
3444 		else {
3445 			ep->com.state = CLOSING;
3446 			start_ep_timer(ep);
3447 		}
3448 		set_bit(CLOSE_SENT, &ep->com.flags);
3449 		break;
3450 	case CLOSING:
3451 		if (!test_and_set_bit(CLOSE_SENT, &ep->com.flags)) {
3452 			close = 1;
3453 			if (abrupt) {
3454 				(void)stop_ep_timer(ep);
3455 				ep->com.state = ABORTING;
3456 			} else
3457 				ep->com.state = MORIBUND;
3458 		}
3459 		break;
3460 	case MORIBUND:
3461 	case ABORTING:
3462 	case DEAD:
3463 		PDBG("%s ignoring disconnect ep %p state %u\n",
3464 		     __func__, ep, ep->com.state);
3465 		break;
3466 	default:
3467 		BUG();
3468 		break;
3469 	}
3470 
3471 	if (close) {
3472 		if (abrupt) {
3473 			set_bit(EP_DISC_ABORT, &ep->com.history);
3474 			close_complete_upcall(ep, -ECONNRESET);
3475 			ret = send_abort(ep, NULL, gfp);
3476 		} else {
3477 			set_bit(EP_DISC_CLOSE, &ep->com.history);
3478 			ret = send_halfclose(ep, gfp);
3479 		}
3480 		if (ret)
3481 			fatal = 1;
3482 	}
3483 	mutex_unlock(&ep->com.mutex);
3484 	if (fatal)
3485 		release_ep_resources(ep);
3486 	return ret;
3487 }
3488 
3489 static void active_ofld_conn_reply(struct c4iw_dev *dev, struct sk_buff *skb,
3490 			struct cpl_fw6_msg_ofld_connection_wr_rpl *req)
3491 {
3492 	struct c4iw_ep *ep;
3493 	int atid = be32_to_cpu(req->tid);
3494 
3495 	ep = (struct c4iw_ep *)lookup_atid(dev->rdev.lldi.tids,
3496 					   (__force u32) req->tid);
3497 	if (!ep)
3498 		return;
3499 
3500 	switch (req->retval) {
3501 	case FW_ENOMEM:
3502 		set_bit(ACT_RETRY_NOMEM, &ep->com.history);
3503 		if (ep->retry_count++ < ACT_OPEN_RETRY_COUNT) {
3504 			send_fw_act_open_req(ep, atid);
3505 			return;
3506 		}
3507 	case FW_EADDRINUSE:
3508 		set_bit(ACT_RETRY_INUSE, &ep->com.history);
3509 		if (ep->retry_count++ < ACT_OPEN_RETRY_COUNT) {
3510 			send_fw_act_open_req(ep, atid);
3511 			return;
3512 		}
3513 		break;
3514 	default:
3515 		pr_info("%s unexpected ofld conn wr retval %d\n",
3516 		       __func__, req->retval);
3517 		break;
3518 	}
3519 	pr_err("active ofld_connect_wr failure %d atid %d\n",
3520 	       req->retval, atid);
3521 	mutex_lock(&dev->rdev.stats.lock);
3522 	dev->rdev.stats.act_ofld_conn_fails++;
3523 	mutex_unlock(&dev->rdev.stats.lock);
3524 	connect_reply_upcall(ep, status2errno(req->retval));
3525 	state_set(&ep->com, DEAD);
3526 	if (ep->com.remote_addr.ss_family == AF_INET6) {
3527 		struct sockaddr_in6 *sin6 =
3528 			(struct sockaddr_in6 *)&ep->com.mapped_local_addr;
3529 		cxgb4_clip_release(ep->com.dev->rdev.lldi.ports[0],
3530 				   (const u32 *)&sin6->sin6_addr.s6_addr, 1);
3531 	}
3532 	remove_handle(dev, &dev->atid_idr, atid);
3533 	cxgb4_free_atid(dev->rdev.lldi.tids, atid);
3534 	dst_release(ep->dst);
3535 	cxgb4_l2t_release(ep->l2t);
3536 	c4iw_put_ep(&ep->com);
3537 }
3538 
3539 static void passive_ofld_conn_reply(struct c4iw_dev *dev, struct sk_buff *skb,
3540 			struct cpl_fw6_msg_ofld_connection_wr_rpl *req)
3541 {
3542 	struct sk_buff *rpl_skb;
3543 	struct cpl_pass_accept_req *cpl;
3544 	int ret;
3545 
3546 	rpl_skb = (struct sk_buff *)(unsigned long)req->cookie;
3547 	BUG_ON(!rpl_skb);
3548 	if (req->retval) {
3549 		PDBG("%s passive open failure %d\n", __func__, req->retval);
3550 		mutex_lock(&dev->rdev.stats.lock);
3551 		dev->rdev.stats.pas_ofld_conn_fails++;
3552 		mutex_unlock(&dev->rdev.stats.lock);
3553 		kfree_skb(rpl_skb);
3554 	} else {
3555 		cpl = (struct cpl_pass_accept_req *)cplhdr(rpl_skb);
3556 		OPCODE_TID(cpl) = htonl(MK_OPCODE_TID(CPL_PASS_ACCEPT_REQ,
3557 					(__force u32) htonl(
3558 					(__force u32) req->tid)));
3559 		ret = pass_accept_req(dev, rpl_skb);
3560 		if (!ret)
3561 			kfree_skb(rpl_skb);
3562 	}
3563 	return;
3564 }
3565 
3566 static int deferred_fw6_msg(struct c4iw_dev *dev, struct sk_buff *skb)
3567 {
3568 	struct cpl_fw6_msg *rpl = cplhdr(skb);
3569 	struct cpl_fw6_msg_ofld_connection_wr_rpl *req;
3570 
3571 	switch (rpl->type) {
3572 	case FW6_TYPE_CQE:
3573 		c4iw_ev_dispatch(dev, (struct t4_cqe *)&rpl->data[0]);
3574 		break;
3575 	case FW6_TYPE_OFLD_CONNECTION_WR_RPL:
3576 		req = (struct cpl_fw6_msg_ofld_connection_wr_rpl *)rpl->data;
3577 		switch (req->t_state) {
3578 		case TCP_SYN_SENT:
3579 			active_ofld_conn_reply(dev, skb, req);
3580 			break;
3581 		case TCP_SYN_RECV:
3582 			passive_ofld_conn_reply(dev, skb, req);
3583 			break;
3584 		default:
3585 			pr_err("%s unexpected ofld conn wr state %d\n",
3586 			       __func__, req->t_state);
3587 			break;
3588 		}
3589 		break;
3590 	}
3591 	return 0;
3592 }
3593 
3594 static void build_cpl_pass_accept_req(struct sk_buff *skb, int stid , u8 tos)
3595 {
3596 	u32 l2info;
3597 	u16 vlantag, len, hdr_len, eth_hdr_len;
3598 	u8 intf;
3599 	struct cpl_rx_pkt *cpl = cplhdr(skb);
3600 	struct cpl_pass_accept_req *req;
3601 	struct tcp_options_received tmp_opt;
3602 	struct c4iw_dev *dev;
3603 
3604 	dev = *((struct c4iw_dev **) (skb->cb + sizeof(void *)));
3605 	/* Store values from cpl_rx_pkt in temporary location. */
3606 	vlantag = (__force u16) cpl->vlan;
3607 	len = (__force u16) cpl->len;
3608 	l2info  = (__force u32) cpl->l2info;
3609 	hdr_len = (__force u16) cpl->hdr_len;
3610 	intf = cpl->iff;
3611 
3612 	__skb_pull(skb, sizeof(*req) + sizeof(struct rss_header));
3613 
3614 	/*
3615 	 * We need to parse the TCP options from SYN packet.
3616 	 * to generate cpl_pass_accept_req.
3617 	 */
3618 	memset(&tmp_opt, 0, sizeof(tmp_opt));
3619 	tcp_clear_options(&tmp_opt);
3620 	tcp_parse_options(skb, &tmp_opt, 0, NULL);
3621 
3622 	req = (struct cpl_pass_accept_req *)__skb_push(skb, sizeof(*req));
3623 	memset(req, 0, sizeof(*req));
3624 	req->l2info = cpu_to_be16(SYN_INTF_V(intf) |
3625 			 SYN_MAC_IDX_V(RX_MACIDX_G(
3626 			 (__force int) htonl(l2info))) |
3627 			 SYN_XACT_MATCH_F);
3628 	eth_hdr_len = is_t4(dev->rdev.lldi.adapter_type) ?
3629 			    RX_ETHHDR_LEN_G((__force int)htonl(l2info)) :
3630 			    RX_T5_ETHHDR_LEN_G((__force int)htonl(l2info));
3631 	req->hdr_len = cpu_to_be32(SYN_RX_CHAN_V(RX_CHAN_G(
3632 					(__force int) htonl(l2info))) |
3633 				   TCP_HDR_LEN_V(RX_TCPHDR_LEN_G(
3634 					(__force int) htons(hdr_len))) |
3635 				   IP_HDR_LEN_V(RX_IPHDR_LEN_G(
3636 					(__force int) htons(hdr_len))) |
3637 				   ETH_HDR_LEN_V(RX_ETHHDR_LEN_G(eth_hdr_len)));
3638 	req->vlan = (__force __be16) vlantag;
3639 	req->len = (__force __be16) len;
3640 	req->tos_stid = cpu_to_be32(PASS_OPEN_TID_V(stid) |
3641 				    PASS_OPEN_TOS_V(tos));
3642 	req->tcpopt.mss = htons(tmp_opt.mss_clamp);
3643 	if (tmp_opt.wscale_ok)
3644 		req->tcpopt.wsf = tmp_opt.snd_wscale;
3645 	req->tcpopt.tstamp = tmp_opt.saw_tstamp;
3646 	if (tmp_opt.sack_ok)
3647 		req->tcpopt.sack = 1;
3648 	OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_ACCEPT_REQ, 0));
3649 	return;
3650 }
3651 
3652 static void send_fw_pass_open_req(struct c4iw_dev *dev, struct sk_buff *skb,
3653 				  __be32 laddr, __be16 lport,
3654 				  __be32 raddr, __be16 rport,
3655 				  u32 rcv_isn, u32 filter, u16 window,
3656 				  u32 rss_qid, u8 port_id)
3657 {
3658 	struct sk_buff *req_skb;
3659 	struct fw_ofld_connection_wr *req;
3660 	struct cpl_pass_accept_req *cpl = cplhdr(skb);
3661 	int ret;
3662 
3663 	req_skb = alloc_skb(sizeof(struct fw_ofld_connection_wr), GFP_KERNEL);
3664 	req = (struct fw_ofld_connection_wr *)__skb_put(req_skb, sizeof(*req));
3665 	memset(req, 0, sizeof(*req));
3666 	req->op_compl = htonl(WR_OP_V(FW_OFLD_CONNECTION_WR) | FW_WR_COMPL_F);
3667 	req->len16_pkd = htonl(FW_WR_LEN16_V(DIV_ROUND_UP(sizeof(*req), 16)));
3668 	req->le.version_cpl = htonl(FW_OFLD_CONNECTION_WR_CPL_F);
3669 	req->le.filter = (__force __be32) filter;
3670 	req->le.lport = lport;
3671 	req->le.pport = rport;
3672 	req->le.u.ipv4.lip = laddr;
3673 	req->le.u.ipv4.pip = raddr;
3674 	req->tcb.rcv_nxt = htonl(rcv_isn + 1);
3675 	req->tcb.rcv_adv = htons(window);
3676 	req->tcb.t_state_to_astid =
3677 		 htonl(FW_OFLD_CONNECTION_WR_T_STATE_V(TCP_SYN_RECV) |
3678 			FW_OFLD_CONNECTION_WR_RCV_SCALE_V(cpl->tcpopt.wsf) |
3679 			FW_OFLD_CONNECTION_WR_ASTID_V(
3680 			PASS_OPEN_TID_G(ntohl(cpl->tos_stid))));
3681 
3682 	/*
3683 	 * We store the qid in opt2 which will be used by the firmware
3684 	 * to send us the wr response.
3685 	 */
3686 	req->tcb.opt2 = htonl(RSS_QUEUE_V(rss_qid));
3687 
3688 	/*
3689 	 * We initialize the MSS index in TCB to 0xF.
3690 	 * So that when driver sends cpl_pass_accept_rpl
3691 	 * TCB picks up the correct value. If this was 0
3692 	 * TP will ignore any value > 0 for MSS index.
3693 	 */
3694 	req->tcb.opt0 = cpu_to_be64(MSS_IDX_V(0xF));
3695 	req->cookie = (uintptr_t)skb;
3696 
3697 	set_wr_txq(req_skb, CPL_PRIORITY_CONTROL, port_id);
3698 	ret = cxgb4_ofld_send(dev->rdev.lldi.ports[0], req_skb);
3699 	if (ret < 0) {
3700 		pr_err("%s - cxgb4_ofld_send error %d - dropping\n", __func__,
3701 		       ret);
3702 		kfree_skb(skb);
3703 		kfree_skb(req_skb);
3704 	}
3705 }
3706 
3707 /*
3708  * Handler for CPL_RX_PKT message. Need to handle cpl_rx_pkt
3709  * messages when a filter is being used instead of server to
3710  * redirect a syn packet. When packets hit filter they are redirected
3711  * to the offload queue and driver tries to establish the connection
3712  * using firmware work request.
3713  */
3714 static int rx_pkt(struct c4iw_dev *dev, struct sk_buff *skb)
3715 {
3716 	int stid;
3717 	unsigned int filter;
3718 	struct ethhdr *eh = NULL;
3719 	struct vlan_ethhdr *vlan_eh = NULL;
3720 	struct iphdr *iph;
3721 	struct tcphdr *tcph;
3722 	struct rss_header *rss = (void *)skb->data;
3723 	struct cpl_rx_pkt *cpl = (void *)skb->data;
3724 	struct cpl_pass_accept_req *req = (void *)(rss + 1);
3725 	struct l2t_entry *e;
3726 	struct dst_entry *dst;
3727 	struct c4iw_ep *lep;
3728 	u16 window;
3729 	struct port_info *pi;
3730 	struct net_device *pdev;
3731 	u16 rss_qid, eth_hdr_len;
3732 	int step;
3733 	u32 tx_chan;
3734 	struct neighbour *neigh;
3735 
3736 	/* Drop all non-SYN packets */
3737 	if (!(cpl->l2info & cpu_to_be32(RXF_SYN_F)))
3738 		goto reject;
3739 
3740 	/*
3741 	 * Drop all packets which did not hit the filter.
3742 	 * Unlikely to happen.
3743 	 */
3744 	if (!(rss->filter_hit && rss->filter_tid))
3745 		goto reject;
3746 
3747 	/*
3748 	 * Calculate the server tid from filter hit index from cpl_rx_pkt.
3749 	 */
3750 	stid = (__force int) cpu_to_be32((__force u32) rss->hash_val);
3751 
3752 	lep = (struct c4iw_ep *)lookup_stid(dev->rdev.lldi.tids, stid);
3753 	if (!lep) {
3754 		PDBG("%s connect request on invalid stid %d\n", __func__, stid);
3755 		goto reject;
3756 	}
3757 
3758 	eth_hdr_len = is_t4(dev->rdev.lldi.adapter_type) ?
3759 			    RX_ETHHDR_LEN_G(htonl(cpl->l2info)) :
3760 			    RX_T5_ETHHDR_LEN_G(htonl(cpl->l2info));
3761 	if (eth_hdr_len == ETH_HLEN) {
3762 		eh = (struct ethhdr *)(req + 1);
3763 		iph = (struct iphdr *)(eh + 1);
3764 	} else {
3765 		vlan_eh = (struct vlan_ethhdr *)(req + 1);
3766 		iph = (struct iphdr *)(vlan_eh + 1);
3767 		skb->vlan_tci = ntohs(cpl->vlan);
3768 	}
3769 
3770 	if (iph->version != 0x4)
3771 		goto reject;
3772 
3773 	tcph = (struct tcphdr *)(iph + 1);
3774 	skb_set_network_header(skb, (void *)iph - (void *)rss);
3775 	skb_set_transport_header(skb, (void *)tcph - (void *)rss);
3776 	skb_get(skb);
3777 
3778 	PDBG("%s lip 0x%x lport %u pip 0x%x pport %u tos %d\n", __func__,
3779 	     ntohl(iph->daddr), ntohs(tcph->dest), ntohl(iph->saddr),
3780 	     ntohs(tcph->source), iph->tos);
3781 
3782 	dst = find_route(dev, iph->daddr, iph->saddr, tcph->dest, tcph->source,
3783 			 iph->tos);
3784 	if (!dst) {
3785 		pr_err("%s - failed to find dst entry!\n",
3786 		       __func__);
3787 		goto reject;
3788 	}
3789 	neigh = dst_neigh_lookup_skb(dst, skb);
3790 
3791 	if (!neigh) {
3792 		pr_err("%s - failed to allocate neigh!\n",
3793 		       __func__);
3794 		goto free_dst;
3795 	}
3796 
3797 	if (neigh->dev->flags & IFF_LOOPBACK) {
3798 		pdev = ip_dev_find(&init_net, iph->daddr);
3799 		e = cxgb4_l2t_get(dev->rdev.lldi.l2t, neigh,
3800 				    pdev, 0);
3801 		pi = (struct port_info *)netdev_priv(pdev);
3802 		tx_chan = cxgb4_port_chan(pdev);
3803 		dev_put(pdev);
3804 	} else {
3805 		pdev = get_real_dev(neigh->dev);
3806 		e = cxgb4_l2t_get(dev->rdev.lldi.l2t, neigh,
3807 					pdev, 0);
3808 		pi = (struct port_info *)netdev_priv(pdev);
3809 		tx_chan = cxgb4_port_chan(pdev);
3810 	}
3811 	neigh_release(neigh);
3812 	if (!e) {
3813 		pr_err("%s - failed to allocate l2t entry!\n",
3814 		       __func__);
3815 		goto free_dst;
3816 	}
3817 
3818 	step = dev->rdev.lldi.nrxq / dev->rdev.lldi.nchan;
3819 	rss_qid = dev->rdev.lldi.rxq_ids[pi->port_id * step];
3820 	window = (__force u16) htons((__force u16)tcph->window);
3821 
3822 	/* Calcuate filter portion for LE region. */
3823 	filter = (__force unsigned int) cpu_to_be32(cxgb4_select_ntuple(
3824 						    dev->rdev.lldi.ports[0],
3825 						    e));
3826 
3827 	/*
3828 	 * Synthesize the cpl_pass_accept_req. We have everything except the
3829 	 * TID. Once firmware sends a reply with TID we update the TID field
3830 	 * in cpl and pass it through the regular cpl_pass_accept_req path.
3831 	 */
3832 	build_cpl_pass_accept_req(skb, stid, iph->tos);
3833 	send_fw_pass_open_req(dev, skb, iph->daddr, tcph->dest, iph->saddr,
3834 			      tcph->source, ntohl(tcph->seq), filter, window,
3835 			      rss_qid, pi->port_id);
3836 	cxgb4_l2t_release(e);
3837 free_dst:
3838 	dst_release(dst);
3839 reject:
3840 	return 0;
3841 }
3842 
3843 /*
3844  * These are the real handlers that are called from a
3845  * work queue.
3846  */
3847 static c4iw_handler_func work_handlers[NUM_CPL_CMDS] = {
3848 	[CPL_ACT_ESTABLISH] = act_establish,
3849 	[CPL_ACT_OPEN_RPL] = act_open_rpl,
3850 	[CPL_RX_DATA] = rx_data,
3851 	[CPL_ABORT_RPL_RSS] = abort_rpl,
3852 	[CPL_ABORT_RPL] = abort_rpl,
3853 	[CPL_PASS_OPEN_RPL] = pass_open_rpl,
3854 	[CPL_CLOSE_LISTSRV_RPL] = close_listsrv_rpl,
3855 	[CPL_PASS_ACCEPT_REQ] = pass_accept_req,
3856 	[CPL_PASS_ESTABLISH] = pass_establish,
3857 	[CPL_PEER_CLOSE] = peer_close,
3858 	[CPL_ABORT_REQ_RSS] = peer_abort,
3859 	[CPL_CLOSE_CON_RPL] = close_con_rpl,
3860 	[CPL_RDMA_TERMINATE] = terminate,
3861 	[CPL_FW4_ACK] = fw4_ack,
3862 	[CPL_FW6_MSG] = deferred_fw6_msg,
3863 	[CPL_RX_PKT] = rx_pkt
3864 };
3865 
3866 static void process_timeout(struct c4iw_ep *ep)
3867 {
3868 	struct c4iw_qp_attributes attrs;
3869 	int abort = 1;
3870 
3871 	mutex_lock(&ep->com.mutex);
3872 	PDBG("%s ep %p tid %u state %d\n", __func__, ep, ep->hwtid,
3873 	     ep->com.state);
3874 	set_bit(TIMEDOUT, &ep->com.history);
3875 	switch (ep->com.state) {
3876 	case MPA_REQ_SENT:
3877 		__state_set(&ep->com, ABORTING);
3878 		connect_reply_upcall(ep, -ETIMEDOUT);
3879 		break;
3880 	case MPA_REQ_WAIT:
3881 		__state_set(&ep->com, ABORTING);
3882 		break;
3883 	case CLOSING:
3884 	case MORIBUND:
3885 		if (ep->com.cm_id && ep->com.qp) {
3886 			attrs.next_state = C4IW_QP_STATE_ERROR;
3887 			c4iw_modify_qp(ep->com.qp->rhp,
3888 				     ep->com.qp, C4IW_QP_ATTR_NEXT_STATE,
3889 				     &attrs, 1);
3890 		}
3891 		__state_set(&ep->com, ABORTING);
3892 		close_complete_upcall(ep, -ETIMEDOUT);
3893 		break;
3894 	case ABORTING:
3895 	case DEAD:
3896 
3897 		/*
3898 		 * These states are expected if the ep timed out at the same
3899 		 * time as another thread was calling stop_ep_timer().
3900 		 * So we silently do nothing for these states.
3901 		 */
3902 		abort = 0;
3903 		break;
3904 	default:
3905 		WARN(1, "%s unexpected state ep %p tid %u state %u\n",
3906 			__func__, ep, ep->hwtid, ep->com.state);
3907 		abort = 0;
3908 	}
3909 	if (abort)
3910 		abort_connection(ep, NULL, GFP_KERNEL);
3911 	mutex_unlock(&ep->com.mutex);
3912 	c4iw_put_ep(&ep->com);
3913 }
3914 
3915 static void process_timedout_eps(void)
3916 {
3917 	struct c4iw_ep *ep;
3918 
3919 	spin_lock_irq(&timeout_lock);
3920 	while (!list_empty(&timeout_list)) {
3921 		struct list_head *tmp;
3922 
3923 		tmp = timeout_list.next;
3924 		list_del(tmp);
3925 		tmp->next = NULL;
3926 		tmp->prev = NULL;
3927 		spin_unlock_irq(&timeout_lock);
3928 		ep = list_entry(tmp, struct c4iw_ep, entry);
3929 		process_timeout(ep);
3930 		spin_lock_irq(&timeout_lock);
3931 	}
3932 	spin_unlock_irq(&timeout_lock);
3933 }
3934 
3935 static void process_work(struct work_struct *work)
3936 {
3937 	struct sk_buff *skb = NULL;
3938 	struct c4iw_dev *dev;
3939 	struct cpl_act_establish *rpl;
3940 	unsigned int opcode;
3941 	int ret;
3942 
3943 	process_timedout_eps();
3944 	while ((skb = skb_dequeue(&rxq))) {
3945 		rpl = cplhdr(skb);
3946 		dev = *((struct c4iw_dev **) (skb->cb + sizeof(void *)));
3947 		opcode = rpl->ot.opcode;
3948 
3949 		BUG_ON(!work_handlers[opcode]);
3950 		ret = work_handlers[opcode](dev, skb);
3951 		if (!ret)
3952 			kfree_skb(skb);
3953 		process_timedout_eps();
3954 	}
3955 }
3956 
3957 static DECLARE_WORK(skb_work, process_work);
3958 
3959 static void ep_timeout(unsigned long arg)
3960 {
3961 	struct c4iw_ep *ep = (struct c4iw_ep *)arg;
3962 	int kickit = 0;
3963 
3964 	spin_lock(&timeout_lock);
3965 	if (!test_and_set_bit(TIMEOUT, &ep->com.flags)) {
3966 		/*
3967 		 * Only insert if it is not already on the list.
3968 		 */
3969 		if (!ep->entry.next) {
3970 			list_add_tail(&ep->entry, &timeout_list);
3971 			kickit = 1;
3972 		}
3973 	}
3974 	spin_unlock(&timeout_lock);
3975 	if (kickit)
3976 		queue_work(workq, &skb_work);
3977 }
3978 
3979 /*
3980  * All the CM events are handled on a work queue to have a safe context.
3981  */
3982 static int sched(struct c4iw_dev *dev, struct sk_buff *skb)
3983 {
3984 
3985 	/*
3986 	 * Save dev in the skb->cb area.
3987 	 */
3988 	*((struct c4iw_dev **) (skb->cb + sizeof(void *))) = dev;
3989 
3990 	/*
3991 	 * Queue the skb and schedule the worker thread.
3992 	 */
3993 	skb_queue_tail(&rxq, skb);
3994 	queue_work(workq, &skb_work);
3995 	return 0;
3996 }
3997 
3998 static int set_tcb_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
3999 {
4000 	struct cpl_set_tcb_rpl *rpl = cplhdr(skb);
4001 
4002 	if (rpl->status != CPL_ERR_NONE) {
4003 		printk(KERN_ERR MOD "Unexpected SET_TCB_RPL status %u "
4004 		       "for tid %u\n", rpl->status, GET_TID(rpl));
4005 	}
4006 	kfree_skb(skb);
4007 	return 0;
4008 }
4009 
4010 static int fw6_msg(struct c4iw_dev *dev, struct sk_buff *skb)
4011 {
4012 	struct cpl_fw6_msg *rpl = cplhdr(skb);
4013 	struct c4iw_wr_wait *wr_waitp;
4014 	int ret;
4015 
4016 	PDBG("%s type %u\n", __func__, rpl->type);
4017 
4018 	switch (rpl->type) {
4019 	case FW6_TYPE_WR_RPL:
4020 		ret = (int)((be64_to_cpu(rpl->data[0]) >> 8) & 0xff);
4021 		wr_waitp = (struct c4iw_wr_wait *)(__force unsigned long) rpl->data[1];
4022 		PDBG("%s wr_waitp %p ret %u\n", __func__, wr_waitp, ret);
4023 		if (wr_waitp)
4024 			c4iw_wake_up(wr_waitp, ret ? -ret : 0);
4025 		kfree_skb(skb);
4026 		break;
4027 	case FW6_TYPE_CQE:
4028 	case FW6_TYPE_OFLD_CONNECTION_WR_RPL:
4029 		sched(dev, skb);
4030 		break;
4031 	default:
4032 		printk(KERN_ERR MOD "%s unexpected fw6 msg type %u\n", __func__,
4033 		       rpl->type);
4034 		kfree_skb(skb);
4035 		break;
4036 	}
4037 	return 0;
4038 }
4039 
4040 static int peer_abort_intr(struct c4iw_dev *dev, struct sk_buff *skb)
4041 {
4042 	struct cpl_abort_req_rss *req = cplhdr(skb);
4043 	struct c4iw_ep *ep;
4044 	struct tid_info *t = dev->rdev.lldi.tids;
4045 	unsigned int tid = GET_TID(req);
4046 
4047 	ep = lookup_tid(t, tid);
4048 	if (!ep) {
4049 		printk(KERN_WARNING MOD
4050 		       "Abort on non-existent endpoint, tid %d\n", tid);
4051 		kfree_skb(skb);
4052 		return 0;
4053 	}
4054 	if (is_neg_adv(req->status)) {
4055 		PDBG("%s Negative advice on abort- tid %u status %d (%s)\n",
4056 		     __func__, ep->hwtid, req->status,
4057 		     neg_adv_str(req->status));
4058 		ep->stats.abort_neg_adv++;
4059 		dev->rdev.stats.neg_adv++;
4060 		kfree_skb(skb);
4061 		return 0;
4062 	}
4063 	PDBG("%s ep %p tid %u state %u\n", __func__, ep, ep->hwtid,
4064 	     ep->com.state);
4065 
4066 	/*
4067 	 * Wake up any threads in rdma_init() or rdma_fini().
4068 	 * However, if we are on MPAv2 and want to retry with MPAv1
4069 	 * then, don't wake up yet.
4070 	 */
4071 	if (mpa_rev == 2 && !ep->tried_with_mpa_v1) {
4072 		if (ep->com.state != MPA_REQ_SENT)
4073 			c4iw_wake_up(&ep->com.wr_wait, -ECONNRESET);
4074 	} else
4075 		c4iw_wake_up(&ep->com.wr_wait, -ECONNRESET);
4076 	sched(dev, skb);
4077 	return 0;
4078 }
4079 
4080 /*
4081  * Most upcalls from the T4 Core go to sched() to
4082  * schedule the processing on a work queue.
4083  */
4084 c4iw_handler_func c4iw_handlers[NUM_CPL_CMDS] = {
4085 	[CPL_ACT_ESTABLISH] = sched,
4086 	[CPL_ACT_OPEN_RPL] = sched,
4087 	[CPL_RX_DATA] = sched,
4088 	[CPL_ABORT_RPL_RSS] = sched,
4089 	[CPL_ABORT_RPL] = sched,
4090 	[CPL_PASS_OPEN_RPL] = sched,
4091 	[CPL_CLOSE_LISTSRV_RPL] = sched,
4092 	[CPL_PASS_ACCEPT_REQ] = sched,
4093 	[CPL_PASS_ESTABLISH] = sched,
4094 	[CPL_PEER_CLOSE] = sched,
4095 	[CPL_CLOSE_CON_RPL] = sched,
4096 	[CPL_ABORT_REQ_RSS] = peer_abort_intr,
4097 	[CPL_RDMA_TERMINATE] = sched,
4098 	[CPL_FW4_ACK] = sched,
4099 	[CPL_SET_TCB_RPL] = set_tcb_rpl,
4100 	[CPL_FW6_MSG] = fw6_msg,
4101 	[CPL_RX_PKT] = sched
4102 };
4103 
4104 int __init c4iw_cm_init(void)
4105 {
4106 	spin_lock_init(&timeout_lock);
4107 	skb_queue_head_init(&rxq);
4108 
4109 	workq = create_singlethread_workqueue("iw_cxgb4");
4110 	if (!workq)
4111 		return -ENOMEM;
4112 
4113 	return 0;
4114 }
4115 
4116 void c4iw_cm_term(void)
4117 {
4118 	WARN_ON(!list_empty(&timeout_list));
4119 	flush_workqueue(workq);
4120 	destroy_workqueue(workq);
4121 }
4122