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