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