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