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