1 /*
2  * Copyright (c) 2016 Chelsio Communications, Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
8 
9 #include <linux/module.h>
10 #include <linux/list.h>
11 #include <linux/workqueue.h>
12 #include <linux/skbuff.h>
13 #include <linux/timer.h>
14 #include <linux/notifier.h>
15 #include <linux/inetdevice.h>
16 #include <linux/ip.h>
17 #include <linux/tcp.h>
18 #include <linux/if_vlan.h>
19 
20 #include <net/neighbour.h>
21 #include <net/netevent.h>
22 #include <net/route.h>
23 #include <net/tcp.h>
24 #include <net/ip6_route.h>
25 #include <net/addrconf.h>
26 
27 #include <libcxgb_cm.h>
28 #include "cxgbit.h"
29 #include "clip_tbl.h"
30 
31 static void cxgbit_init_wr_wait(struct cxgbit_wr_wait *wr_waitp)
32 {
33 	wr_waitp->ret = 0;
34 	reinit_completion(&wr_waitp->completion);
35 }
36 
37 static void
38 cxgbit_wake_up(struct cxgbit_wr_wait *wr_waitp, const char *func, u8 ret)
39 {
40 	if (ret == CPL_ERR_NONE)
41 		wr_waitp->ret = 0;
42 	else
43 		wr_waitp->ret = -EIO;
44 
45 	if (wr_waitp->ret)
46 		pr_err("%s: err:%u", func, ret);
47 
48 	complete(&wr_waitp->completion);
49 }
50 
51 static int
52 cxgbit_wait_for_reply(struct cxgbit_device *cdev,
53 		      struct cxgbit_wr_wait *wr_waitp, u32 tid, u32 timeout,
54 		      const char *func)
55 {
56 	int ret;
57 
58 	if (!test_bit(CDEV_STATE_UP, &cdev->flags)) {
59 		wr_waitp->ret = -EIO;
60 		goto out;
61 	}
62 
63 	ret = wait_for_completion_timeout(&wr_waitp->completion, timeout * HZ);
64 	if (!ret) {
65 		pr_info("%s - Device %s not responding tid %u\n",
66 			func, pci_name(cdev->lldi.pdev), tid);
67 		wr_waitp->ret = -ETIMEDOUT;
68 	}
69 out:
70 	if (wr_waitp->ret)
71 		pr_info("%s: FW reply %d tid %u\n",
72 			pci_name(cdev->lldi.pdev), wr_waitp->ret, tid);
73 	return wr_waitp->ret;
74 }
75 
76 static int cxgbit_np_hashfn(const struct cxgbit_np *cnp)
77 {
78 	return ((unsigned long)cnp >> 10) & (NP_INFO_HASH_SIZE - 1);
79 }
80 
81 static struct np_info *
82 cxgbit_np_hash_add(struct cxgbit_device *cdev, struct cxgbit_np *cnp,
83 		   unsigned int stid)
84 {
85 	struct np_info *p = kzalloc(sizeof(*p), GFP_KERNEL);
86 
87 	if (p) {
88 		int bucket = cxgbit_np_hashfn(cnp);
89 
90 		p->cnp = cnp;
91 		p->stid = stid;
92 		spin_lock(&cdev->np_lock);
93 		p->next = cdev->np_hash_tab[bucket];
94 		cdev->np_hash_tab[bucket] = p;
95 		spin_unlock(&cdev->np_lock);
96 	}
97 
98 	return p;
99 }
100 
101 static int
102 cxgbit_np_hash_find(struct cxgbit_device *cdev, struct cxgbit_np *cnp)
103 {
104 	int stid = -1, bucket = cxgbit_np_hashfn(cnp);
105 	struct np_info *p;
106 
107 	spin_lock(&cdev->np_lock);
108 	for (p = cdev->np_hash_tab[bucket]; p; p = p->next) {
109 		if (p->cnp == cnp) {
110 			stid = p->stid;
111 			break;
112 		}
113 	}
114 	spin_unlock(&cdev->np_lock);
115 
116 	return stid;
117 }
118 
119 static int cxgbit_np_hash_del(struct cxgbit_device *cdev, struct cxgbit_np *cnp)
120 {
121 	int stid = -1, bucket = cxgbit_np_hashfn(cnp);
122 	struct np_info *p, **prev = &cdev->np_hash_tab[bucket];
123 
124 	spin_lock(&cdev->np_lock);
125 	for (p = *prev; p; prev = &p->next, p = p->next) {
126 		if (p->cnp == cnp) {
127 			stid = p->stid;
128 			*prev = p->next;
129 			kfree(p);
130 			break;
131 		}
132 	}
133 	spin_unlock(&cdev->np_lock);
134 
135 	return stid;
136 }
137 
138 void _cxgbit_free_cnp(struct kref *kref)
139 {
140 	struct cxgbit_np *cnp;
141 
142 	cnp = container_of(kref, struct cxgbit_np, kref);
143 	kfree(cnp);
144 }
145 
146 static int
147 cxgbit_create_server6(struct cxgbit_device *cdev, unsigned int stid,
148 		      struct cxgbit_np *cnp)
149 {
150 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)
151 				     &cnp->com.local_addr;
152 	int addr_type;
153 	int ret;
154 
155 	pr_debug("%s: dev = %s; stid = %u; sin6_port = %u\n",
156 		 __func__, cdev->lldi.ports[0]->name, stid, sin6->sin6_port);
157 
158 	addr_type = ipv6_addr_type((const struct in6_addr *)
159 				   &sin6->sin6_addr);
160 	if (addr_type != IPV6_ADDR_ANY) {
161 		ret = cxgb4_clip_get(cdev->lldi.ports[0],
162 				     (const u32 *)&sin6->sin6_addr.s6_addr, 1);
163 		if (ret) {
164 			pr_err("Unable to find clip table entry. laddr %pI6. Error:%d.\n",
165 			       sin6->sin6_addr.s6_addr, ret);
166 			return -ENOMEM;
167 		}
168 	}
169 
170 	cxgbit_get_cnp(cnp);
171 	cxgbit_init_wr_wait(&cnp->com.wr_wait);
172 
173 	ret = cxgb4_create_server6(cdev->lldi.ports[0],
174 				   stid, &sin6->sin6_addr,
175 				   sin6->sin6_port,
176 				   cdev->lldi.rxq_ids[0]);
177 	if (!ret)
178 		ret = cxgbit_wait_for_reply(cdev, &cnp->com.wr_wait,
179 					    0, 10, __func__);
180 	else if (ret > 0)
181 		ret = net_xmit_errno(ret);
182 	else
183 		cxgbit_put_cnp(cnp);
184 
185 	if (ret) {
186 		if (ret != -ETIMEDOUT)
187 			cxgb4_clip_release(cdev->lldi.ports[0],
188 				   (const u32 *)&sin6->sin6_addr.s6_addr, 1);
189 
190 		pr_err("create server6 err %d stid %d laddr %pI6 lport %d\n",
191 		       ret, stid, sin6->sin6_addr.s6_addr,
192 		       ntohs(sin6->sin6_port));
193 	}
194 
195 	return ret;
196 }
197 
198 static int
199 cxgbit_create_server4(struct cxgbit_device *cdev, unsigned int stid,
200 		      struct cxgbit_np *cnp)
201 {
202 	struct sockaddr_in *sin = (struct sockaddr_in *)
203 				   &cnp->com.local_addr;
204 	int ret;
205 
206 	pr_debug("%s: dev = %s; stid = %u; sin_port = %u\n",
207 		 __func__, cdev->lldi.ports[0]->name, stid, sin->sin_port);
208 
209 	cxgbit_get_cnp(cnp);
210 	cxgbit_init_wr_wait(&cnp->com.wr_wait);
211 
212 	ret = cxgb4_create_server(cdev->lldi.ports[0],
213 				  stid, sin->sin_addr.s_addr,
214 				  sin->sin_port, 0,
215 				  cdev->lldi.rxq_ids[0]);
216 	if (!ret)
217 		ret = cxgbit_wait_for_reply(cdev,
218 					    &cnp->com.wr_wait,
219 					    0, 10, __func__);
220 	else if (ret > 0)
221 		ret = net_xmit_errno(ret);
222 	else
223 		cxgbit_put_cnp(cnp);
224 
225 	if (ret)
226 		pr_err("create server failed err %d stid %d laddr %pI4 lport %d\n",
227 		       ret, stid, &sin->sin_addr, ntohs(sin->sin_port));
228 	return ret;
229 }
230 
231 struct cxgbit_device *cxgbit_find_device(struct net_device *ndev, u8 *port_id)
232 {
233 	struct cxgbit_device *cdev;
234 	u8 i;
235 
236 	list_for_each_entry(cdev, &cdev_list_head, list) {
237 		struct cxgb4_lld_info *lldi = &cdev->lldi;
238 
239 		for (i = 0; i < lldi->nports; i++) {
240 			if (lldi->ports[i] == ndev) {
241 				if (port_id)
242 					*port_id = i;
243 				return cdev;
244 			}
245 		}
246 	}
247 
248 	return NULL;
249 }
250 
251 static struct net_device *cxgbit_get_real_dev(struct net_device *ndev)
252 {
253 	if (ndev->priv_flags & IFF_BONDING) {
254 		pr_err("Bond devices are not supported. Interface:%s\n",
255 		       ndev->name);
256 		return NULL;
257 	}
258 
259 	if (is_vlan_dev(ndev))
260 		return vlan_dev_real_dev(ndev);
261 
262 	return ndev;
263 }
264 
265 static struct net_device *cxgbit_ipv4_netdev(__be32 saddr)
266 {
267 	struct net_device *ndev;
268 
269 	ndev = __ip_dev_find(&init_net, saddr, false);
270 	if (!ndev)
271 		return NULL;
272 
273 	return cxgbit_get_real_dev(ndev);
274 }
275 
276 static struct net_device *cxgbit_ipv6_netdev(struct in6_addr *addr6)
277 {
278 	struct net_device *ndev = NULL;
279 	bool found = false;
280 
281 	if (IS_ENABLED(CONFIG_IPV6)) {
282 		for_each_netdev_rcu(&init_net, ndev)
283 			if (ipv6_chk_addr(&init_net, addr6, ndev, 1)) {
284 				found = true;
285 				break;
286 			}
287 	}
288 	if (!found)
289 		return NULL;
290 	return cxgbit_get_real_dev(ndev);
291 }
292 
293 static struct cxgbit_device *cxgbit_find_np_cdev(struct cxgbit_np *cnp)
294 {
295 	struct sockaddr_storage *sockaddr = &cnp->com.local_addr;
296 	int ss_family = sockaddr->ss_family;
297 	struct net_device *ndev = NULL;
298 	struct cxgbit_device *cdev = NULL;
299 
300 	rcu_read_lock();
301 	if (ss_family == AF_INET) {
302 		struct sockaddr_in *sin;
303 
304 		sin = (struct sockaddr_in *)sockaddr;
305 		ndev = cxgbit_ipv4_netdev(sin->sin_addr.s_addr);
306 	} else if (ss_family == AF_INET6) {
307 		struct sockaddr_in6 *sin6;
308 
309 		sin6 = (struct sockaddr_in6 *)sockaddr;
310 		ndev = cxgbit_ipv6_netdev(&sin6->sin6_addr);
311 	}
312 	if (!ndev)
313 		goto out;
314 
315 	cdev = cxgbit_find_device(ndev, NULL);
316 out:
317 	rcu_read_unlock();
318 	return cdev;
319 }
320 
321 static bool cxgbit_inaddr_any(struct cxgbit_np *cnp)
322 {
323 	struct sockaddr_storage *sockaddr = &cnp->com.local_addr;
324 	int ss_family = sockaddr->ss_family;
325 	int addr_type;
326 
327 	if (ss_family == AF_INET) {
328 		struct sockaddr_in *sin;
329 
330 		sin = (struct sockaddr_in *)sockaddr;
331 		if (sin->sin_addr.s_addr == htonl(INADDR_ANY))
332 			return true;
333 	} else if (ss_family == AF_INET6) {
334 		struct sockaddr_in6 *sin6;
335 
336 		sin6 = (struct sockaddr_in6 *)sockaddr;
337 		addr_type = ipv6_addr_type((const struct in6_addr *)
338 				&sin6->sin6_addr);
339 		if (addr_type == IPV6_ADDR_ANY)
340 			return true;
341 	}
342 	return false;
343 }
344 
345 static int
346 __cxgbit_setup_cdev_np(struct cxgbit_device *cdev, struct cxgbit_np *cnp)
347 {
348 	int stid, ret;
349 	int ss_family = cnp->com.local_addr.ss_family;
350 
351 	if (!test_bit(CDEV_STATE_UP, &cdev->flags))
352 		return -EINVAL;
353 
354 	stid = cxgb4_alloc_stid(cdev->lldi.tids, ss_family, cnp);
355 	if (stid < 0)
356 		return -EINVAL;
357 
358 	if (!cxgbit_np_hash_add(cdev, cnp, stid)) {
359 		cxgb4_free_stid(cdev->lldi.tids, stid, ss_family);
360 		return -EINVAL;
361 	}
362 
363 	if (ss_family == AF_INET)
364 		ret = cxgbit_create_server4(cdev, stid, cnp);
365 	else
366 		ret = cxgbit_create_server6(cdev, stid, cnp);
367 
368 	if (ret) {
369 		if (ret != -ETIMEDOUT)
370 			cxgb4_free_stid(cdev->lldi.tids, stid,
371 					ss_family);
372 		cxgbit_np_hash_del(cdev, cnp);
373 		return ret;
374 	}
375 	return ret;
376 }
377 
378 static int cxgbit_setup_cdev_np(struct cxgbit_np *cnp)
379 {
380 	struct cxgbit_device *cdev;
381 	int ret = -1;
382 
383 	mutex_lock(&cdev_list_lock);
384 	cdev = cxgbit_find_np_cdev(cnp);
385 	if (!cdev)
386 		goto out;
387 
388 	if (cxgbit_np_hash_find(cdev, cnp) >= 0)
389 		goto out;
390 
391 	if (__cxgbit_setup_cdev_np(cdev, cnp))
392 		goto out;
393 
394 	cnp->com.cdev = cdev;
395 	ret = 0;
396 out:
397 	mutex_unlock(&cdev_list_lock);
398 	return ret;
399 }
400 
401 static int cxgbit_setup_all_np(struct cxgbit_np *cnp)
402 {
403 	struct cxgbit_device *cdev;
404 	int ret;
405 	u32 count = 0;
406 
407 	mutex_lock(&cdev_list_lock);
408 	list_for_each_entry(cdev, &cdev_list_head, list) {
409 		if (cxgbit_np_hash_find(cdev, cnp) >= 0) {
410 			mutex_unlock(&cdev_list_lock);
411 			return -1;
412 		}
413 	}
414 
415 	list_for_each_entry(cdev, &cdev_list_head, list) {
416 		ret = __cxgbit_setup_cdev_np(cdev, cnp);
417 		if (ret == -ETIMEDOUT)
418 			break;
419 		if (ret != 0)
420 			continue;
421 		count++;
422 	}
423 	mutex_unlock(&cdev_list_lock);
424 
425 	return count ? 0 : -1;
426 }
427 
428 int cxgbit_setup_np(struct iscsi_np *np, struct sockaddr_storage *ksockaddr)
429 {
430 	struct cxgbit_np *cnp;
431 	int ret;
432 
433 	if ((ksockaddr->ss_family != AF_INET) &&
434 	    (ksockaddr->ss_family != AF_INET6))
435 		return -EINVAL;
436 
437 	cnp = kzalloc(sizeof(*cnp), GFP_KERNEL);
438 	if (!cnp)
439 		return -ENOMEM;
440 
441 	init_waitqueue_head(&cnp->accept_wait);
442 	init_completion(&cnp->com.wr_wait.completion);
443 	init_completion(&cnp->accept_comp);
444 	INIT_LIST_HEAD(&cnp->np_accept_list);
445 	spin_lock_init(&cnp->np_accept_lock);
446 	kref_init(&cnp->kref);
447 	memcpy(&np->np_sockaddr, ksockaddr,
448 	       sizeof(struct sockaddr_storage));
449 	memcpy(&cnp->com.local_addr, &np->np_sockaddr,
450 	       sizeof(cnp->com.local_addr));
451 
452 	cnp->np = np;
453 	cnp->com.cdev = NULL;
454 
455 	if (cxgbit_inaddr_any(cnp))
456 		ret = cxgbit_setup_all_np(cnp);
457 	else
458 		ret = cxgbit_setup_cdev_np(cnp);
459 
460 	if (ret) {
461 		cxgbit_put_cnp(cnp);
462 		return -EINVAL;
463 	}
464 
465 	np->np_context = cnp;
466 	cnp->com.state = CSK_STATE_LISTEN;
467 	return 0;
468 }
469 
470 static void
471 cxgbit_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
472 		     struct cxgbit_sock *csk)
473 {
474 	conn->login_family = np->np_sockaddr.ss_family;
475 	conn->login_sockaddr = csk->com.remote_addr;
476 	conn->local_sockaddr = csk->com.local_addr;
477 }
478 
479 int cxgbit_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
480 {
481 	struct cxgbit_np *cnp = np->np_context;
482 	struct cxgbit_sock *csk;
483 	int ret = 0;
484 
485 accept_wait:
486 	ret = wait_for_completion_interruptible(&cnp->accept_comp);
487 	if (ret)
488 		return -ENODEV;
489 
490 	spin_lock_bh(&np->np_thread_lock);
491 	if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
492 		spin_unlock_bh(&np->np_thread_lock);
493 		/**
494 		 * No point in stalling here when np_thread
495 		 * is in state RESET/SHUTDOWN/EXIT - bail
496 		 **/
497 		return -ENODEV;
498 	}
499 	spin_unlock_bh(&np->np_thread_lock);
500 
501 	spin_lock_bh(&cnp->np_accept_lock);
502 	if (list_empty(&cnp->np_accept_list)) {
503 		spin_unlock_bh(&cnp->np_accept_lock);
504 		goto accept_wait;
505 	}
506 
507 	csk = list_first_entry(&cnp->np_accept_list,
508 			       struct cxgbit_sock,
509 			       accept_node);
510 
511 	list_del_init(&csk->accept_node);
512 	spin_unlock_bh(&cnp->np_accept_lock);
513 	conn->context = csk;
514 	csk->conn = conn;
515 
516 	cxgbit_set_conn_info(np, conn, csk);
517 	return 0;
518 }
519 
520 static int
521 __cxgbit_free_cdev_np(struct cxgbit_device *cdev, struct cxgbit_np *cnp)
522 {
523 	int stid, ret;
524 	bool ipv6 = false;
525 
526 	stid = cxgbit_np_hash_del(cdev, cnp);
527 	if (stid < 0)
528 		return -EINVAL;
529 	if (!test_bit(CDEV_STATE_UP, &cdev->flags))
530 		return -EINVAL;
531 
532 	if (cnp->np->np_sockaddr.ss_family == AF_INET6)
533 		ipv6 = true;
534 
535 	cxgbit_get_cnp(cnp);
536 	cxgbit_init_wr_wait(&cnp->com.wr_wait);
537 	ret = cxgb4_remove_server(cdev->lldi.ports[0], stid,
538 				  cdev->lldi.rxq_ids[0], ipv6);
539 
540 	if (ret > 0)
541 		ret = net_xmit_errno(ret);
542 
543 	if (ret) {
544 		cxgbit_put_cnp(cnp);
545 		return ret;
546 	}
547 
548 	ret = cxgbit_wait_for_reply(cdev, &cnp->com.wr_wait,
549 				    0, 10, __func__);
550 	if (ret == -ETIMEDOUT)
551 		return ret;
552 
553 	if (ipv6 && cnp->com.cdev) {
554 		struct sockaddr_in6 *sin6;
555 
556 		sin6 = (struct sockaddr_in6 *)&cnp->com.local_addr;
557 		cxgb4_clip_release(cdev->lldi.ports[0],
558 				   (const u32 *)&sin6->sin6_addr.s6_addr,
559 				   1);
560 	}
561 
562 	cxgb4_free_stid(cdev->lldi.tids, stid,
563 			cnp->com.local_addr.ss_family);
564 	return 0;
565 }
566 
567 static void cxgbit_free_all_np(struct cxgbit_np *cnp)
568 {
569 	struct cxgbit_device *cdev;
570 	int ret;
571 
572 	mutex_lock(&cdev_list_lock);
573 	list_for_each_entry(cdev, &cdev_list_head, list) {
574 		ret = __cxgbit_free_cdev_np(cdev, cnp);
575 		if (ret == -ETIMEDOUT)
576 			break;
577 	}
578 	mutex_unlock(&cdev_list_lock);
579 }
580 
581 static void cxgbit_free_cdev_np(struct cxgbit_np *cnp)
582 {
583 	struct cxgbit_device *cdev;
584 	bool found = false;
585 
586 	mutex_lock(&cdev_list_lock);
587 	list_for_each_entry(cdev, &cdev_list_head, list) {
588 		if (cdev == cnp->com.cdev) {
589 			found = true;
590 			break;
591 		}
592 	}
593 	if (!found)
594 		goto out;
595 
596 	__cxgbit_free_cdev_np(cdev, cnp);
597 out:
598 	mutex_unlock(&cdev_list_lock);
599 }
600 
601 void cxgbit_free_np(struct iscsi_np *np)
602 {
603 	struct cxgbit_np *cnp = np->np_context;
604 
605 	cnp->com.state = CSK_STATE_DEAD;
606 	if (cnp->com.cdev)
607 		cxgbit_free_cdev_np(cnp);
608 	else
609 		cxgbit_free_all_np(cnp);
610 
611 	np->np_context = NULL;
612 	cxgbit_put_cnp(cnp);
613 }
614 
615 static void cxgbit_send_halfclose(struct cxgbit_sock *csk)
616 {
617 	struct sk_buff *skb;
618 	u32 len = roundup(sizeof(struct cpl_close_con_req), 16);
619 
620 	skb = alloc_skb(len, GFP_ATOMIC);
621 	if (!skb)
622 		return;
623 
624 	cxgb_mk_close_con_req(skb, len, csk->tid, csk->txq_idx,
625 			      NULL, NULL);
626 
627 	cxgbit_skcb_flags(skb) |= SKCBF_TX_FLAG_COMPL;
628 	__skb_queue_tail(&csk->txq, skb);
629 	cxgbit_push_tx_frames(csk);
630 }
631 
632 static void cxgbit_arp_failure_discard(void *handle, struct sk_buff *skb)
633 {
634 	pr_debug("%s cxgbit_device %p\n", __func__, handle);
635 	kfree_skb(skb);
636 }
637 
638 static void cxgbit_abort_arp_failure(void *handle, struct sk_buff *skb)
639 {
640 	struct cxgbit_device *cdev = handle;
641 	struct cpl_abort_req *req = cplhdr(skb);
642 
643 	pr_debug("%s cdev %p\n", __func__, cdev);
644 	req->cmd = CPL_ABORT_NO_RST;
645 	cxgbit_ofld_send(cdev, skb);
646 }
647 
648 static int cxgbit_send_abort_req(struct cxgbit_sock *csk)
649 {
650 	struct sk_buff *skb;
651 	u32 len = roundup(sizeof(struct cpl_abort_req), 16);
652 
653 	pr_debug("%s: csk %p tid %u; state %d\n",
654 		 __func__, csk, csk->tid, csk->com.state);
655 
656 	__skb_queue_purge(&csk->txq);
657 
658 	if (!test_and_set_bit(CSK_TX_DATA_SENT, &csk->com.flags))
659 		cxgbit_send_tx_flowc_wr(csk);
660 
661 	skb = __skb_dequeue(&csk->skbq);
662 	cxgb_mk_abort_req(skb, len, csk->tid, csk->txq_idx,
663 			  csk->com.cdev, cxgbit_abort_arp_failure);
664 
665 	return cxgbit_l2t_send(csk->com.cdev, skb, csk->l2t);
666 }
667 
668 void cxgbit_free_conn(struct iscsi_conn *conn)
669 {
670 	struct cxgbit_sock *csk = conn->context;
671 	bool release = false;
672 
673 	pr_debug("%s: state %d\n",
674 		 __func__, csk->com.state);
675 
676 	spin_lock_bh(&csk->lock);
677 	switch (csk->com.state) {
678 	case CSK_STATE_ESTABLISHED:
679 		if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT) {
680 			csk->com.state = CSK_STATE_CLOSING;
681 			cxgbit_send_halfclose(csk);
682 		} else {
683 			csk->com.state = CSK_STATE_ABORTING;
684 			cxgbit_send_abort_req(csk);
685 		}
686 		break;
687 	case CSK_STATE_CLOSING:
688 		csk->com.state = CSK_STATE_MORIBUND;
689 		cxgbit_send_halfclose(csk);
690 		break;
691 	case CSK_STATE_DEAD:
692 		release = true;
693 		break;
694 	default:
695 		pr_err("%s: csk %p; state %d\n",
696 		       __func__, csk, csk->com.state);
697 	}
698 	spin_unlock_bh(&csk->lock);
699 
700 	if (release)
701 		cxgbit_put_csk(csk);
702 }
703 
704 static void cxgbit_set_emss(struct cxgbit_sock *csk, u16 opt)
705 {
706 	csk->emss = csk->com.cdev->lldi.mtus[TCPOPT_MSS_G(opt)] -
707 			((csk->com.remote_addr.ss_family == AF_INET) ?
708 			sizeof(struct iphdr) : sizeof(struct ipv6hdr)) -
709 			sizeof(struct tcphdr);
710 	csk->mss = csk->emss;
711 	if (TCPOPT_TSTAMP_G(opt))
712 		csk->emss -= round_up(TCPOLEN_TIMESTAMP, 4);
713 	if (csk->emss < 128)
714 		csk->emss = 128;
715 	if (csk->emss & 7)
716 		pr_info("Warning: misaligned mtu idx %u mss %u emss=%u\n",
717 			TCPOPT_MSS_G(opt), csk->mss, csk->emss);
718 	pr_debug("%s mss_idx %u mss %u emss=%u\n", __func__, TCPOPT_MSS_G(opt),
719 		 csk->mss, csk->emss);
720 }
721 
722 static void cxgbit_free_skb(struct cxgbit_sock *csk)
723 {
724 	struct sk_buff *skb;
725 
726 	__skb_queue_purge(&csk->txq);
727 	__skb_queue_purge(&csk->rxq);
728 	__skb_queue_purge(&csk->backlogq);
729 	__skb_queue_purge(&csk->ppodq);
730 	__skb_queue_purge(&csk->skbq);
731 
732 	while ((skb = cxgbit_sock_dequeue_wr(csk)))
733 		kfree_skb(skb);
734 
735 	__kfree_skb(csk->lro_hskb);
736 }
737 
738 void _cxgbit_free_csk(struct kref *kref)
739 {
740 	struct cxgbit_sock *csk;
741 	struct cxgbit_device *cdev;
742 
743 	csk = container_of(kref, struct cxgbit_sock, kref);
744 
745 	pr_debug("%s csk %p state %d\n", __func__, csk, csk->com.state);
746 
747 	if (csk->com.local_addr.ss_family == AF_INET6) {
748 		struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)
749 					     &csk->com.local_addr;
750 		cxgb4_clip_release(csk->com.cdev->lldi.ports[0],
751 				   (const u32 *)
752 				   &sin6->sin6_addr.s6_addr, 1);
753 	}
754 
755 	cxgb4_remove_tid(csk->com.cdev->lldi.tids, 0, csk->tid,
756 			 csk->com.local_addr.ss_family);
757 	dst_release(csk->dst);
758 	cxgb4_l2t_release(csk->l2t);
759 
760 	cdev = csk->com.cdev;
761 	spin_lock_bh(&cdev->cskq.lock);
762 	list_del(&csk->list);
763 	spin_unlock_bh(&cdev->cskq.lock);
764 
765 	cxgbit_free_skb(csk);
766 	cxgbit_put_cdev(cdev);
767 
768 	kfree(csk);
769 }
770 
771 static void cxgbit_set_tcp_window(struct cxgbit_sock *csk, struct port_info *pi)
772 {
773 	unsigned int linkspeed;
774 	u8 scale;
775 
776 	linkspeed = pi->link_cfg.speed;
777 	scale = linkspeed / SPEED_10000;
778 
779 #define CXGBIT_10G_RCV_WIN (256 * 1024)
780 	csk->rcv_win = CXGBIT_10G_RCV_WIN;
781 	if (scale)
782 		csk->rcv_win *= scale;
783 
784 #define CXGBIT_10G_SND_WIN (256 * 1024)
785 	csk->snd_win = CXGBIT_10G_SND_WIN;
786 	if (scale)
787 		csk->snd_win *= scale;
788 
789 	pr_debug("%s snd_win %d rcv_win %d\n",
790 		 __func__, csk->snd_win, csk->rcv_win);
791 }
792 
793 #ifdef CONFIG_CHELSIO_T4_DCB
794 static u8 cxgbit_get_iscsi_dcb_state(struct net_device *ndev)
795 {
796 	return ndev->dcbnl_ops->getstate(ndev);
797 }
798 
799 static int cxgbit_select_priority(int pri_mask)
800 {
801 	if (!pri_mask)
802 		return 0;
803 
804 	return (ffs(pri_mask) - 1);
805 }
806 
807 static u8 cxgbit_get_iscsi_dcb_priority(struct net_device *ndev, u16 local_port)
808 {
809 	int ret;
810 	u8 caps;
811 
812 	struct dcb_app iscsi_dcb_app = {
813 		.protocol = local_port
814 	};
815 
816 	ret = (int)ndev->dcbnl_ops->getcap(ndev, DCB_CAP_ATTR_DCBX, &caps);
817 
818 	if (ret)
819 		return 0;
820 
821 	if (caps & DCB_CAP_DCBX_VER_IEEE) {
822 		iscsi_dcb_app.selector = IEEE_8021QAZ_APP_SEL_ANY;
823 
824 		ret = dcb_ieee_getapp_mask(ndev, &iscsi_dcb_app);
825 
826 	} else if (caps & DCB_CAP_DCBX_VER_CEE) {
827 		iscsi_dcb_app.selector = DCB_APP_IDTYPE_PORTNUM;
828 
829 		ret = dcb_getapp(ndev, &iscsi_dcb_app);
830 	}
831 
832 	pr_info("iSCSI priority is set to %u\n", cxgbit_select_priority(ret));
833 
834 	return cxgbit_select_priority(ret);
835 }
836 #endif
837 
838 static int
839 cxgbit_offload_init(struct cxgbit_sock *csk, int iptype, __u8 *peer_ip,
840 		    u16 local_port, struct dst_entry *dst,
841 		    struct cxgbit_device *cdev)
842 {
843 	struct neighbour *n;
844 	int ret, step;
845 	struct net_device *ndev;
846 	u16 rxq_idx, port_id;
847 #ifdef CONFIG_CHELSIO_T4_DCB
848 	u8 priority = 0;
849 #endif
850 
851 	n = dst_neigh_lookup(dst, peer_ip);
852 	if (!n)
853 		return -ENODEV;
854 
855 	rcu_read_lock();
856 	ret = -ENOMEM;
857 	if (n->dev->flags & IFF_LOOPBACK) {
858 		if (iptype == 4)
859 			ndev = cxgbit_ipv4_netdev(*(__be32 *)peer_ip);
860 		else if (IS_ENABLED(CONFIG_IPV6))
861 			ndev = cxgbit_ipv6_netdev((struct in6_addr *)peer_ip);
862 		else
863 			ndev = NULL;
864 
865 		if (!ndev) {
866 			ret = -ENODEV;
867 			goto out;
868 		}
869 
870 		csk->l2t = cxgb4_l2t_get(cdev->lldi.l2t,
871 					 n, ndev, 0);
872 		if (!csk->l2t)
873 			goto out;
874 		csk->mtu = ndev->mtu;
875 		csk->tx_chan = cxgb4_port_chan(ndev);
876 		csk->smac_idx = cxgb4_tp_smt_idx(cdev->lldi.adapter_type,
877 						 cxgb4_port_viid(ndev));
878 		step = cdev->lldi.ntxq /
879 			cdev->lldi.nchan;
880 		csk->txq_idx = cxgb4_port_idx(ndev) * step;
881 		step = cdev->lldi.nrxq /
882 			cdev->lldi.nchan;
883 		csk->ctrlq_idx = cxgb4_port_idx(ndev);
884 		csk->rss_qid = cdev->lldi.rxq_ids[
885 				cxgb4_port_idx(ndev) * step];
886 		csk->port_id = cxgb4_port_idx(ndev);
887 		cxgbit_set_tcp_window(csk,
888 				      (struct port_info *)netdev_priv(ndev));
889 	} else {
890 		ndev = cxgbit_get_real_dev(n->dev);
891 		if (!ndev) {
892 			ret = -ENODEV;
893 			goto out;
894 		}
895 
896 #ifdef CONFIG_CHELSIO_T4_DCB
897 		if (cxgbit_get_iscsi_dcb_state(ndev))
898 			priority = cxgbit_get_iscsi_dcb_priority(ndev,
899 								 local_port);
900 
901 		csk->dcb_priority = priority;
902 
903 		csk->l2t = cxgb4_l2t_get(cdev->lldi.l2t, n, ndev, priority);
904 #else
905 		csk->l2t = cxgb4_l2t_get(cdev->lldi.l2t, n, ndev, 0);
906 #endif
907 		if (!csk->l2t)
908 			goto out;
909 		port_id = cxgb4_port_idx(ndev);
910 		csk->mtu = dst_mtu(dst);
911 		csk->tx_chan = cxgb4_port_chan(ndev);
912 		csk->smac_idx = cxgb4_tp_smt_idx(cdev->lldi.adapter_type,
913 						 cxgb4_port_viid(ndev));
914 		step = cdev->lldi.ntxq /
915 			cdev->lldi.nports;
916 		csk->txq_idx = (port_id * step) +
917 				(cdev->selectq[port_id][0]++ % step);
918 		csk->ctrlq_idx = cxgb4_port_idx(ndev);
919 		step = cdev->lldi.nrxq /
920 			cdev->lldi.nports;
921 		rxq_idx = (port_id * step) +
922 				(cdev->selectq[port_id][1]++ % step);
923 		csk->rss_qid = cdev->lldi.rxq_ids[rxq_idx];
924 		csk->port_id = port_id;
925 		cxgbit_set_tcp_window(csk,
926 				      (struct port_info *)netdev_priv(ndev));
927 	}
928 	ret = 0;
929 out:
930 	rcu_read_unlock();
931 	neigh_release(n);
932 	return ret;
933 }
934 
935 int cxgbit_ofld_send(struct cxgbit_device *cdev, struct sk_buff *skb)
936 {
937 	int ret = 0;
938 
939 	if (!test_bit(CDEV_STATE_UP, &cdev->flags)) {
940 		kfree_skb(skb);
941 		pr_err("%s - device not up - dropping\n", __func__);
942 		return -EIO;
943 	}
944 
945 	ret = cxgb4_ofld_send(cdev->lldi.ports[0], skb);
946 	if (ret < 0)
947 		kfree_skb(skb);
948 	return ret < 0 ? ret : 0;
949 }
950 
951 static void cxgbit_release_tid(struct cxgbit_device *cdev, u32 tid)
952 {
953 	u32 len = roundup(sizeof(struct cpl_tid_release), 16);
954 	struct sk_buff *skb;
955 
956 	skb = alloc_skb(len, GFP_ATOMIC);
957 	if (!skb)
958 		return;
959 
960 	cxgb_mk_tid_release(skb, len, tid, 0);
961 	cxgbit_ofld_send(cdev, skb);
962 }
963 
964 int
965 cxgbit_l2t_send(struct cxgbit_device *cdev, struct sk_buff *skb,
966 		struct l2t_entry *l2e)
967 {
968 	int ret = 0;
969 
970 	if (!test_bit(CDEV_STATE_UP, &cdev->flags)) {
971 		kfree_skb(skb);
972 		pr_err("%s - device not up - dropping\n", __func__);
973 		return -EIO;
974 	}
975 
976 	ret = cxgb4_l2t_send(cdev->lldi.ports[0], skb, l2e);
977 	if (ret < 0)
978 		kfree_skb(skb);
979 	return ret < 0 ? ret : 0;
980 }
981 
982 static void cxgbit_send_rx_credits(struct cxgbit_sock *csk, struct sk_buff *skb)
983 {
984 	if (csk->com.state != CSK_STATE_ESTABLISHED) {
985 		__kfree_skb(skb);
986 		return;
987 	}
988 
989 	cxgbit_ofld_send(csk->com.cdev, skb);
990 }
991 
992 /*
993  * CPL connection rx data ack: host ->
994  * Send RX credits through an RX_DATA_ACK CPL message.
995  * Returns the number of credits sent.
996  */
997 int cxgbit_rx_data_ack(struct cxgbit_sock *csk)
998 {
999 	struct sk_buff *skb;
1000 	u32 len = roundup(sizeof(struct cpl_rx_data_ack), 16);
1001 	u32 credit_dack;
1002 
1003 	skb = alloc_skb(len, GFP_KERNEL);
1004 	if (!skb)
1005 		return -1;
1006 
1007 	credit_dack = RX_DACK_CHANGE_F | RX_DACK_MODE_V(1) |
1008 		      RX_CREDITS_V(csk->rx_credits);
1009 
1010 	cxgb_mk_rx_data_ack(skb, len, csk->tid, csk->ctrlq_idx,
1011 			    credit_dack);
1012 
1013 	csk->rx_credits = 0;
1014 
1015 	spin_lock_bh(&csk->lock);
1016 	if (csk->lock_owner) {
1017 		cxgbit_skcb_rx_backlog_fn(skb) = cxgbit_send_rx_credits;
1018 		__skb_queue_tail(&csk->backlogq, skb);
1019 		spin_unlock_bh(&csk->lock);
1020 		return 0;
1021 	}
1022 
1023 	cxgbit_send_rx_credits(csk, skb);
1024 	spin_unlock_bh(&csk->lock);
1025 
1026 	return 0;
1027 }
1028 
1029 #define FLOWC_WR_NPARAMS_MIN    9
1030 #define FLOWC_WR_NPARAMS_MAX	11
1031 static int cxgbit_alloc_csk_skb(struct cxgbit_sock *csk)
1032 {
1033 	struct sk_buff *skb;
1034 	u32 len, flowclen;
1035 	u8 i;
1036 
1037 	flowclen = offsetof(struct fw_flowc_wr,
1038 			    mnemval[FLOWC_WR_NPARAMS_MAX]);
1039 
1040 	len = max_t(u32, sizeof(struct cpl_abort_req),
1041 		    sizeof(struct cpl_abort_rpl));
1042 
1043 	len = max(len, flowclen);
1044 	len = roundup(len, 16);
1045 
1046 	for (i = 0; i < 3; i++) {
1047 		skb = alloc_skb(len, GFP_ATOMIC);
1048 		if (!skb)
1049 			goto out;
1050 		__skb_queue_tail(&csk->skbq, skb);
1051 	}
1052 
1053 	skb = alloc_skb(LRO_SKB_MIN_HEADROOM, GFP_ATOMIC);
1054 	if (!skb)
1055 		goto out;
1056 
1057 	memset(skb->data, 0, LRO_SKB_MIN_HEADROOM);
1058 	csk->lro_hskb = skb;
1059 
1060 	return 0;
1061 out:
1062 	__skb_queue_purge(&csk->skbq);
1063 	return -ENOMEM;
1064 }
1065 
1066 static void
1067 cxgbit_pass_accept_rpl(struct cxgbit_sock *csk, struct cpl_pass_accept_req *req)
1068 {
1069 	struct sk_buff *skb;
1070 	const struct tcphdr *tcph;
1071 	struct cpl_t5_pass_accept_rpl *rpl5;
1072 	struct cxgb4_lld_info *lldi = &csk->com.cdev->lldi;
1073 	unsigned int len = roundup(sizeof(*rpl5), 16);
1074 	unsigned int mtu_idx;
1075 	u64 opt0;
1076 	u32 opt2, hlen;
1077 	u32 wscale;
1078 	u32 win;
1079 
1080 	pr_debug("%s csk %p tid %u\n", __func__, csk, csk->tid);
1081 
1082 	skb = alloc_skb(len, GFP_ATOMIC);
1083 	if (!skb) {
1084 		cxgbit_put_csk(csk);
1085 		return;
1086 	}
1087 
1088 	rpl5 = __skb_put_zero(skb, len);
1089 
1090 	INIT_TP_WR(rpl5, csk->tid);
1091 	OPCODE_TID(rpl5) = cpu_to_be32(MK_OPCODE_TID(CPL_PASS_ACCEPT_RPL,
1092 						     csk->tid));
1093 	cxgb_best_mtu(csk->com.cdev->lldi.mtus, csk->mtu, &mtu_idx,
1094 		      req->tcpopt.tstamp,
1095 		      (csk->com.remote_addr.ss_family == AF_INET) ? 0 : 1);
1096 	wscale = cxgb_compute_wscale(csk->rcv_win);
1097 	/*
1098 	 * Specify the largest window that will fit in opt0. The
1099 	 * remainder will be specified in the rx_data_ack.
1100 	 */
1101 	win = csk->rcv_win >> 10;
1102 	if (win > RCV_BUFSIZ_M)
1103 		win = RCV_BUFSIZ_M;
1104 	opt0 =  TCAM_BYPASS_F |
1105 		WND_SCALE_V(wscale) |
1106 		MSS_IDX_V(mtu_idx) |
1107 		L2T_IDX_V(csk->l2t->idx) |
1108 		TX_CHAN_V(csk->tx_chan) |
1109 		SMAC_SEL_V(csk->smac_idx) |
1110 		DSCP_V(csk->tos >> 2) |
1111 		ULP_MODE_V(ULP_MODE_ISCSI) |
1112 		RCV_BUFSIZ_V(win);
1113 
1114 	opt2 = RX_CHANNEL_V(0) |
1115 		RSS_QUEUE_VALID_F | RSS_QUEUE_V(csk->rss_qid);
1116 
1117 	if (!is_t5(lldi->adapter_type))
1118 		opt2 |= RX_FC_DISABLE_F;
1119 
1120 	if (req->tcpopt.tstamp)
1121 		opt2 |= TSTAMPS_EN_F;
1122 	if (req->tcpopt.sack)
1123 		opt2 |= SACK_EN_F;
1124 	if (wscale)
1125 		opt2 |= WND_SCALE_EN_F;
1126 
1127 	hlen = ntohl(req->hdr_len);
1128 
1129 	if (is_t5(lldi->adapter_type))
1130 		tcph = (struct tcphdr *)((u8 *)(req + 1) +
1131 		       ETH_HDR_LEN_G(hlen) + IP_HDR_LEN_G(hlen));
1132 	else
1133 		tcph = (struct tcphdr *)((u8 *)(req + 1) +
1134 		       T6_ETH_HDR_LEN_G(hlen) + T6_IP_HDR_LEN_G(hlen));
1135 
1136 	if (tcph->ece && tcph->cwr)
1137 		opt2 |= CCTRL_ECN_V(1);
1138 
1139 	opt2 |= RX_COALESCE_V(3);
1140 	opt2 |= CONG_CNTRL_V(CONG_ALG_NEWRENO);
1141 
1142 	opt2 |= T5_ISS_F;
1143 	rpl5->iss = cpu_to_be32((prandom_u32() & ~7UL) - 1);
1144 
1145 	opt2 |= T5_OPT_2_VALID_F;
1146 
1147 	rpl5->opt0 = cpu_to_be64(opt0);
1148 	rpl5->opt2 = cpu_to_be32(opt2);
1149 	set_wr_txq(skb, CPL_PRIORITY_SETUP, csk->ctrlq_idx);
1150 	t4_set_arp_err_handler(skb, NULL, cxgbit_arp_failure_discard);
1151 	cxgbit_l2t_send(csk->com.cdev, skb, csk->l2t);
1152 }
1153 
1154 static void
1155 cxgbit_pass_accept_req(struct cxgbit_device *cdev, struct sk_buff *skb)
1156 {
1157 	struct cxgbit_sock *csk = NULL;
1158 	struct cxgbit_np *cnp;
1159 	struct cpl_pass_accept_req *req = cplhdr(skb);
1160 	unsigned int stid = PASS_OPEN_TID_G(ntohl(req->tos_stid));
1161 	struct tid_info *t = cdev->lldi.tids;
1162 	unsigned int tid = GET_TID(req);
1163 	u16 peer_mss = ntohs(req->tcpopt.mss);
1164 	unsigned short hdrs;
1165 
1166 	struct dst_entry *dst;
1167 	__u8 local_ip[16], peer_ip[16];
1168 	__be16 local_port, peer_port;
1169 	int ret;
1170 	int iptype;
1171 
1172 	pr_debug("%s: cdev = %p; stid = %u; tid = %u\n",
1173 		 __func__, cdev, stid, tid);
1174 
1175 	cnp = lookup_stid(t, stid);
1176 	if (!cnp) {
1177 		pr_err("%s connect request on invalid stid %d\n",
1178 		       __func__, stid);
1179 		goto rel_skb;
1180 	}
1181 
1182 	if (cnp->com.state != CSK_STATE_LISTEN) {
1183 		pr_err("%s - listening parent not in CSK_STATE_LISTEN\n",
1184 		       __func__);
1185 		goto reject;
1186 	}
1187 
1188 	csk = lookup_tid(t, tid);
1189 	if (csk) {
1190 		pr_err("%s csk not null tid %u\n",
1191 		       __func__, tid);
1192 		goto rel_skb;
1193 	}
1194 
1195 	cxgb_get_4tuple(req, cdev->lldi.adapter_type, &iptype, local_ip,
1196 			peer_ip, &local_port, &peer_port);
1197 
1198 	/* Find output route */
1199 	if (iptype == 4)  {
1200 		pr_debug("%s parent sock %p tid %u laddr %pI4 raddr %pI4 "
1201 			 "lport %d rport %d peer_mss %d\n"
1202 			 , __func__, cnp, tid,
1203 			 local_ip, peer_ip, ntohs(local_port),
1204 			 ntohs(peer_port), peer_mss);
1205 		dst = cxgb_find_route(&cdev->lldi, cxgbit_get_real_dev,
1206 				      *(__be32 *)local_ip,
1207 				      *(__be32 *)peer_ip,
1208 				      local_port, peer_port,
1209 				      PASS_OPEN_TOS_G(ntohl(req->tos_stid)));
1210 	} else {
1211 		pr_debug("%s parent sock %p tid %u laddr %pI6 raddr %pI6 "
1212 			 "lport %d rport %d peer_mss %d\n"
1213 			 , __func__, cnp, tid,
1214 			 local_ip, peer_ip, ntohs(local_port),
1215 			 ntohs(peer_port), peer_mss);
1216 		dst = cxgb_find_route6(&cdev->lldi, cxgbit_get_real_dev,
1217 				       local_ip, peer_ip,
1218 				       local_port, peer_port,
1219 				       PASS_OPEN_TOS_G(ntohl(req->tos_stid)),
1220 				       ((struct sockaddr_in6 *)
1221 					&cnp->com.local_addr)->sin6_scope_id);
1222 	}
1223 	if (!dst) {
1224 		pr_err("%s - failed to find dst entry!\n",
1225 		       __func__);
1226 		goto reject;
1227 	}
1228 
1229 	csk = kzalloc(sizeof(*csk), GFP_ATOMIC);
1230 	if (!csk) {
1231 		dst_release(dst);
1232 		goto rel_skb;
1233 	}
1234 
1235 	ret = cxgbit_offload_init(csk, iptype, peer_ip, ntohs(local_port),
1236 				  dst, cdev);
1237 	if (ret) {
1238 		pr_err("%s - failed to allocate l2t entry!\n",
1239 		       __func__);
1240 		dst_release(dst);
1241 		kfree(csk);
1242 		goto reject;
1243 	}
1244 
1245 	kref_init(&csk->kref);
1246 	init_completion(&csk->com.wr_wait.completion);
1247 
1248 	INIT_LIST_HEAD(&csk->accept_node);
1249 
1250 	hdrs = (iptype == 4 ? sizeof(struct iphdr) : sizeof(struct ipv6hdr)) +
1251 		sizeof(struct tcphdr) +	(req->tcpopt.tstamp ? 12 : 0);
1252 	if (peer_mss && csk->mtu > (peer_mss + hdrs))
1253 		csk->mtu = peer_mss + hdrs;
1254 
1255 	csk->com.state = CSK_STATE_CONNECTING;
1256 	csk->com.cdev = cdev;
1257 	csk->cnp = cnp;
1258 	csk->tos = PASS_OPEN_TOS_G(ntohl(req->tos_stid));
1259 	csk->dst = dst;
1260 	csk->tid = tid;
1261 	csk->wr_cred = cdev->lldi.wr_cred -
1262 			DIV_ROUND_UP(sizeof(struct cpl_abort_req), 16);
1263 	csk->wr_max_cred = csk->wr_cred;
1264 	csk->wr_una_cred = 0;
1265 
1266 	if (iptype == 4) {
1267 		struct sockaddr_in *sin = (struct sockaddr_in *)
1268 					  &csk->com.local_addr;
1269 		sin->sin_family = AF_INET;
1270 		sin->sin_port = local_port;
1271 		sin->sin_addr.s_addr = *(__be32 *)local_ip;
1272 
1273 		sin = (struct sockaddr_in *)&csk->com.remote_addr;
1274 		sin->sin_family = AF_INET;
1275 		sin->sin_port = peer_port;
1276 		sin->sin_addr.s_addr = *(__be32 *)peer_ip;
1277 	} else {
1278 		struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)
1279 					    &csk->com.local_addr;
1280 
1281 		sin6->sin6_family = PF_INET6;
1282 		sin6->sin6_port = local_port;
1283 		memcpy(sin6->sin6_addr.s6_addr, local_ip, 16);
1284 		cxgb4_clip_get(cdev->lldi.ports[0],
1285 			       (const u32 *)&sin6->sin6_addr.s6_addr,
1286 			       1);
1287 
1288 		sin6 = (struct sockaddr_in6 *)&csk->com.remote_addr;
1289 		sin6->sin6_family = PF_INET6;
1290 		sin6->sin6_port = peer_port;
1291 		memcpy(sin6->sin6_addr.s6_addr, peer_ip, 16);
1292 	}
1293 
1294 	skb_queue_head_init(&csk->rxq);
1295 	skb_queue_head_init(&csk->txq);
1296 	skb_queue_head_init(&csk->ppodq);
1297 	skb_queue_head_init(&csk->backlogq);
1298 	skb_queue_head_init(&csk->skbq);
1299 	cxgbit_sock_reset_wr_list(csk);
1300 	spin_lock_init(&csk->lock);
1301 	init_waitqueue_head(&csk->waitq);
1302 	init_waitqueue_head(&csk->ack_waitq);
1303 	csk->lock_owner = false;
1304 
1305 	if (cxgbit_alloc_csk_skb(csk)) {
1306 		dst_release(dst);
1307 		kfree(csk);
1308 		goto rel_skb;
1309 	}
1310 
1311 	cxgbit_get_cdev(cdev);
1312 
1313 	spin_lock(&cdev->cskq.lock);
1314 	list_add_tail(&csk->list, &cdev->cskq.list);
1315 	spin_unlock(&cdev->cskq.lock);
1316 	cxgb4_insert_tid(t, csk, tid, csk->com.local_addr.ss_family);
1317 	cxgbit_pass_accept_rpl(csk, req);
1318 	goto rel_skb;
1319 
1320 reject:
1321 	cxgbit_release_tid(cdev, tid);
1322 rel_skb:
1323 	__kfree_skb(skb);
1324 }
1325 
1326 static u32
1327 cxgbit_tx_flowc_wr_credits(struct cxgbit_sock *csk, u32 *nparamsp,
1328 			   u32 *flowclenp)
1329 {
1330 	u32 nparams, flowclen16, flowclen;
1331 
1332 	nparams = FLOWC_WR_NPARAMS_MIN;
1333 
1334 	if (csk->snd_wscale)
1335 		nparams++;
1336 
1337 #ifdef CONFIG_CHELSIO_T4_DCB
1338 	nparams++;
1339 #endif
1340 	flowclen = offsetof(struct fw_flowc_wr, mnemval[nparams]);
1341 	flowclen16 = DIV_ROUND_UP(flowclen, 16);
1342 	flowclen = flowclen16 * 16;
1343 	/*
1344 	 * Return the number of 16-byte credits used by the flowc request.
1345 	 * Pass back the nparams and actual flowc length if requested.
1346 	 */
1347 	if (nparamsp)
1348 		*nparamsp = nparams;
1349 	if (flowclenp)
1350 		*flowclenp = flowclen;
1351 	return flowclen16;
1352 }
1353 
1354 u32 cxgbit_send_tx_flowc_wr(struct cxgbit_sock *csk)
1355 {
1356 	struct cxgbit_device *cdev = csk->com.cdev;
1357 	struct fw_flowc_wr *flowc;
1358 	u32 nparams, flowclen16, flowclen;
1359 	struct sk_buff *skb;
1360 	u8 index;
1361 
1362 #ifdef CONFIG_CHELSIO_T4_DCB
1363 	u16 vlan = ((struct l2t_entry *)csk->l2t)->vlan;
1364 #endif
1365 
1366 	flowclen16 = cxgbit_tx_flowc_wr_credits(csk, &nparams, &flowclen);
1367 
1368 	skb = __skb_dequeue(&csk->skbq);
1369 	flowc = __skb_put_zero(skb, flowclen);
1370 
1371 	flowc->op_to_nparams = cpu_to_be32(FW_WR_OP_V(FW_FLOWC_WR) |
1372 					   FW_FLOWC_WR_NPARAMS_V(nparams));
1373 	flowc->flowid_len16 = cpu_to_be32(FW_WR_LEN16_V(flowclen16) |
1374 					  FW_WR_FLOWID_V(csk->tid));
1375 	flowc->mnemval[0].mnemonic = FW_FLOWC_MNEM_PFNVFN;
1376 	flowc->mnemval[0].val = cpu_to_be32(FW_PFVF_CMD_PFN_V
1377 					    (csk->com.cdev->lldi.pf));
1378 	flowc->mnemval[1].mnemonic = FW_FLOWC_MNEM_CH;
1379 	flowc->mnemval[1].val = cpu_to_be32(csk->tx_chan);
1380 	flowc->mnemval[2].mnemonic = FW_FLOWC_MNEM_PORT;
1381 	flowc->mnemval[2].val = cpu_to_be32(csk->tx_chan);
1382 	flowc->mnemval[3].mnemonic = FW_FLOWC_MNEM_IQID;
1383 	flowc->mnemval[3].val = cpu_to_be32(csk->rss_qid);
1384 	flowc->mnemval[4].mnemonic = FW_FLOWC_MNEM_SNDNXT;
1385 	flowc->mnemval[4].val = cpu_to_be32(csk->snd_nxt);
1386 	flowc->mnemval[5].mnemonic = FW_FLOWC_MNEM_RCVNXT;
1387 	flowc->mnemval[5].val = cpu_to_be32(csk->rcv_nxt);
1388 	flowc->mnemval[6].mnemonic = FW_FLOWC_MNEM_SNDBUF;
1389 	flowc->mnemval[6].val = cpu_to_be32(csk->snd_win);
1390 	flowc->mnemval[7].mnemonic = FW_FLOWC_MNEM_MSS;
1391 	flowc->mnemval[7].val = cpu_to_be32(csk->emss);
1392 
1393 	flowc->mnemval[8].mnemonic = FW_FLOWC_MNEM_TXDATAPLEN_MAX;
1394 	if (test_bit(CDEV_ISO_ENABLE, &cdev->flags))
1395 		flowc->mnemval[8].val = cpu_to_be32(CXGBIT_MAX_ISO_PAYLOAD);
1396 	else
1397 		flowc->mnemval[8].val = cpu_to_be32(16384);
1398 
1399 	index = 9;
1400 
1401 	if (csk->snd_wscale) {
1402 		flowc->mnemval[index].mnemonic = FW_FLOWC_MNEM_RCV_SCALE;
1403 		flowc->mnemval[index].val = cpu_to_be32(csk->snd_wscale);
1404 		index++;
1405 	}
1406 
1407 #ifdef CONFIG_CHELSIO_T4_DCB
1408 	flowc->mnemval[index].mnemonic = FW_FLOWC_MNEM_DCBPRIO;
1409 	if (vlan == VLAN_NONE) {
1410 		pr_warn("csk %u without VLAN Tag on DCB Link\n", csk->tid);
1411 		flowc->mnemval[index].val = cpu_to_be32(0);
1412 	} else
1413 		flowc->mnemval[index].val = cpu_to_be32(
1414 				(vlan & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT);
1415 #endif
1416 
1417 	pr_debug("%s: csk %p; tx_chan = %u; rss_qid = %u; snd_seq = %u;"
1418 		 " rcv_seq = %u; snd_win = %u; emss = %u\n",
1419 		 __func__, csk, csk->tx_chan, csk->rss_qid, csk->snd_nxt,
1420 		 csk->rcv_nxt, csk->snd_win, csk->emss);
1421 	set_wr_txq(skb, CPL_PRIORITY_DATA, csk->txq_idx);
1422 	cxgbit_ofld_send(csk->com.cdev, skb);
1423 	return flowclen16;
1424 }
1425 
1426 int cxgbit_setup_conn_digest(struct cxgbit_sock *csk)
1427 {
1428 	struct sk_buff *skb;
1429 	struct cpl_set_tcb_field *req;
1430 	u8 hcrc = csk->submode & CXGBIT_SUBMODE_HCRC;
1431 	u8 dcrc = csk->submode & CXGBIT_SUBMODE_DCRC;
1432 	unsigned int len = roundup(sizeof(*req), 16);
1433 	int ret;
1434 
1435 	skb = alloc_skb(len, GFP_KERNEL);
1436 	if (!skb)
1437 		return -ENOMEM;
1438 
1439 	/*  set up ulp submode */
1440 	req = __skb_put_zero(skb, len);
1441 
1442 	INIT_TP_WR(req, csk->tid);
1443 	OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, csk->tid));
1444 	req->reply_ctrl = htons(NO_REPLY_V(0) | QUEUENO_V(csk->rss_qid));
1445 	req->word_cookie = htons(0);
1446 	req->mask = cpu_to_be64(0x3 << 4);
1447 	req->val = cpu_to_be64(((hcrc ? ULP_CRC_HEADER : 0) |
1448 				(dcrc ? ULP_CRC_DATA : 0)) << 4);
1449 	set_wr_txq(skb, CPL_PRIORITY_CONTROL, csk->ctrlq_idx);
1450 
1451 	cxgbit_get_csk(csk);
1452 	cxgbit_init_wr_wait(&csk->com.wr_wait);
1453 
1454 	cxgbit_ofld_send(csk->com.cdev, skb);
1455 
1456 	ret = cxgbit_wait_for_reply(csk->com.cdev,
1457 				    &csk->com.wr_wait,
1458 				    csk->tid, 5, __func__);
1459 	if (ret)
1460 		return -1;
1461 
1462 	return 0;
1463 }
1464 
1465 int cxgbit_setup_conn_pgidx(struct cxgbit_sock *csk, u32 pg_idx)
1466 {
1467 	struct sk_buff *skb;
1468 	struct cpl_set_tcb_field *req;
1469 	unsigned int len = roundup(sizeof(*req), 16);
1470 	int ret;
1471 
1472 	skb = alloc_skb(len, GFP_KERNEL);
1473 	if (!skb)
1474 		return -ENOMEM;
1475 
1476 	req = __skb_put_zero(skb, len);
1477 
1478 	INIT_TP_WR(req, csk->tid);
1479 	OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, csk->tid));
1480 	req->reply_ctrl = htons(NO_REPLY_V(0) | QUEUENO_V(csk->rss_qid));
1481 	req->word_cookie = htons(0);
1482 	req->mask = cpu_to_be64(0x3 << 8);
1483 	req->val = cpu_to_be64(pg_idx << 8);
1484 	set_wr_txq(skb, CPL_PRIORITY_CONTROL, csk->ctrlq_idx);
1485 
1486 	cxgbit_get_csk(csk);
1487 	cxgbit_init_wr_wait(&csk->com.wr_wait);
1488 
1489 	cxgbit_ofld_send(csk->com.cdev, skb);
1490 
1491 	ret = cxgbit_wait_for_reply(csk->com.cdev,
1492 				    &csk->com.wr_wait,
1493 				    csk->tid, 5, __func__);
1494 	if (ret)
1495 		return -1;
1496 
1497 	return 0;
1498 }
1499 
1500 static void
1501 cxgbit_pass_open_rpl(struct cxgbit_device *cdev, struct sk_buff *skb)
1502 {
1503 	struct cpl_pass_open_rpl *rpl = cplhdr(skb);
1504 	struct tid_info *t = cdev->lldi.tids;
1505 	unsigned int stid = GET_TID(rpl);
1506 	struct cxgbit_np *cnp = lookup_stid(t, stid);
1507 
1508 	pr_debug("%s: cnp = %p; stid = %u; status = %d\n",
1509 		 __func__, cnp, stid, rpl->status);
1510 
1511 	if (!cnp) {
1512 		pr_info("%s stid %d lookup failure\n", __func__, stid);
1513 		return;
1514 	}
1515 
1516 	cxgbit_wake_up(&cnp->com.wr_wait, __func__, rpl->status);
1517 	cxgbit_put_cnp(cnp);
1518 }
1519 
1520 static void
1521 cxgbit_close_listsrv_rpl(struct cxgbit_device *cdev, struct sk_buff *skb)
1522 {
1523 	struct cpl_close_listsvr_rpl *rpl = cplhdr(skb);
1524 	struct tid_info *t = cdev->lldi.tids;
1525 	unsigned int stid = GET_TID(rpl);
1526 	struct cxgbit_np *cnp = lookup_stid(t, stid);
1527 
1528 	pr_debug("%s: cnp = %p; stid = %u; status = %d\n",
1529 		 __func__, cnp, stid, rpl->status);
1530 
1531 	if (!cnp) {
1532 		pr_info("%s stid %d lookup failure\n", __func__, stid);
1533 		return;
1534 	}
1535 
1536 	cxgbit_wake_up(&cnp->com.wr_wait, __func__, rpl->status);
1537 	cxgbit_put_cnp(cnp);
1538 }
1539 
1540 static void
1541 cxgbit_pass_establish(struct cxgbit_device *cdev, struct sk_buff *skb)
1542 {
1543 	struct cpl_pass_establish *req = cplhdr(skb);
1544 	struct tid_info *t = cdev->lldi.tids;
1545 	unsigned int tid = GET_TID(req);
1546 	struct cxgbit_sock *csk;
1547 	struct cxgbit_np *cnp;
1548 	u16 tcp_opt = be16_to_cpu(req->tcp_opt);
1549 	u32 snd_isn = be32_to_cpu(req->snd_isn);
1550 	u32 rcv_isn = be32_to_cpu(req->rcv_isn);
1551 
1552 	csk = lookup_tid(t, tid);
1553 	if (unlikely(!csk)) {
1554 		pr_err("can't find connection for tid %u.\n", tid);
1555 		goto rel_skb;
1556 	}
1557 	cnp = csk->cnp;
1558 
1559 	pr_debug("%s: csk %p; tid %u; cnp %p\n",
1560 		 __func__, csk, tid, cnp);
1561 
1562 	csk->write_seq = snd_isn;
1563 	csk->snd_una = snd_isn;
1564 	csk->snd_nxt = snd_isn;
1565 
1566 	csk->rcv_nxt = rcv_isn;
1567 
1568 	if (csk->rcv_win > (RCV_BUFSIZ_M << 10))
1569 		csk->rx_credits = (csk->rcv_win - (RCV_BUFSIZ_M << 10));
1570 
1571 	csk->snd_wscale = TCPOPT_SND_WSCALE_G(tcp_opt);
1572 	cxgbit_set_emss(csk, tcp_opt);
1573 	dst_confirm(csk->dst);
1574 	csk->com.state = CSK_STATE_ESTABLISHED;
1575 	spin_lock_bh(&cnp->np_accept_lock);
1576 	list_add_tail(&csk->accept_node, &cnp->np_accept_list);
1577 	spin_unlock_bh(&cnp->np_accept_lock);
1578 	complete(&cnp->accept_comp);
1579 rel_skb:
1580 	__kfree_skb(skb);
1581 }
1582 
1583 static void cxgbit_queue_rx_skb(struct cxgbit_sock *csk, struct sk_buff *skb)
1584 {
1585 	cxgbit_skcb_flags(skb) = 0;
1586 	spin_lock_bh(&csk->rxq.lock);
1587 	__skb_queue_tail(&csk->rxq, skb);
1588 	spin_unlock_bh(&csk->rxq.lock);
1589 	wake_up(&csk->waitq);
1590 }
1591 
1592 static void cxgbit_peer_close(struct cxgbit_sock *csk, struct sk_buff *skb)
1593 {
1594 	pr_debug("%s: csk %p; tid %u; state %d\n",
1595 		 __func__, csk, csk->tid, csk->com.state);
1596 
1597 	switch (csk->com.state) {
1598 	case CSK_STATE_ESTABLISHED:
1599 		csk->com.state = CSK_STATE_CLOSING;
1600 		cxgbit_queue_rx_skb(csk, skb);
1601 		return;
1602 	case CSK_STATE_CLOSING:
1603 		/* simultaneous close */
1604 		csk->com.state = CSK_STATE_MORIBUND;
1605 		break;
1606 	case CSK_STATE_MORIBUND:
1607 		csk->com.state = CSK_STATE_DEAD;
1608 		cxgbit_put_csk(csk);
1609 		break;
1610 	case CSK_STATE_ABORTING:
1611 		break;
1612 	default:
1613 		pr_info("%s: cpl_peer_close in bad state %d\n",
1614 			__func__, csk->com.state);
1615 	}
1616 
1617 	__kfree_skb(skb);
1618 }
1619 
1620 static void cxgbit_close_con_rpl(struct cxgbit_sock *csk, struct sk_buff *skb)
1621 {
1622 	pr_debug("%s: csk %p; tid %u; state %d\n",
1623 		 __func__, csk, csk->tid, csk->com.state);
1624 
1625 	switch (csk->com.state) {
1626 	case CSK_STATE_CLOSING:
1627 		csk->com.state = CSK_STATE_MORIBUND;
1628 		break;
1629 	case CSK_STATE_MORIBUND:
1630 		csk->com.state = CSK_STATE_DEAD;
1631 		cxgbit_put_csk(csk);
1632 		break;
1633 	case CSK_STATE_ABORTING:
1634 	case CSK_STATE_DEAD:
1635 		break;
1636 	default:
1637 		pr_info("%s: cpl_close_con_rpl in bad state %d\n",
1638 			__func__, csk->com.state);
1639 	}
1640 
1641 	__kfree_skb(skb);
1642 }
1643 
1644 static void cxgbit_abort_req_rss(struct cxgbit_sock *csk, struct sk_buff *skb)
1645 {
1646 	struct cpl_abort_req_rss *hdr = cplhdr(skb);
1647 	unsigned int tid = GET_TID(hdr);
1648 	struct sk_buff *rpl_skb;
1649 	bool release = false;
1650 	bool wakeup_thread = false;
1651 	u32 len = roundup(sizeof(struct cpl_abort_rpl), 16);
1652 
1653 	pr_debug("%s: csk %p; tid %u; state %d\n",
1654 		 __func__, csk, tid, csk->com.state);
1655 
1656 	if (cxgb_is_neg_adv(hdr->status)) {
1657 		pr_err("%s: got neg advise %d on tid %u\n",
1658 		       __func__, hdr->status, tid);
1659 		goto rel_skb;
1660 	}
1661 
1662 	switch (csk->com.state) {
1663 	case CSK_STATE_CONNECTING:
1664 	case CSK_STATE_MORIBUND:
1665 		csk->com.state = CSK_STATE_DEAD;
1666 		release = true;
1667 		break;
1668 	case CSK_STATE_ESTABLISHED:
1669 		csk->com.state = CSK_STATE_DEAD;
1670 		wakeup_thread = true;
1671 		break;
1672 	case CSK_STATE_CLOSING:
1673 		csk->com.state = CSK_STATE_DEAD;
1674 		if (!csk->conn)
1675 			release = true;
1676 		break;
1677 	case CSK_STATE_ABORTING:
1678 		break;
1679 	default:
1680 		pr_info("%s: cpl_abort_req_rss in bad state %d\n",
1681 			__func__, csk->com.state);
1682 		csk->com.state = CSK_STATE_DEAD;
1683 	}
1684 
1685 	__skb_queue_purge(&csk->txq);
1686 
1687 	if (!test_and_set_bit(CSK_TX_DATA_SENT, &csk->com.flags))
1688 		cxgbit_send_tx_flowc_wr(csk);
1689 
1690 	rpl_skb = __skb_dequeue(&csk->skbq);
1691 
1692 	cxgb_mk_abort_rpl(rpl_skb, len, csk->tid, csk->txq_idx);
1693 	cxgbit_ofld_send(csk->com.cdev, rpl_skb);
1694 
1695 	if (wakeup_thread) {
1696 		cxgbit_queue_rx_skb(csk, skb);
1697 		return;
1698 	}
1699 
1700 	if (release)
1701 		cxgbit_put_csk(csk);
1702 rel_skb:
1703 	__kfree_skb(skb);
1704 }
1705 
1706 static void cxgbit_abort_rpl_rss(struct cxgbit_sock *csk, struct sk_buff *skb)
1707 {
1708 	pr_debug("%s: csk %p; tid %u; state %d\n",
1709 		 __func__, csk, csk->tid, csk->com.state);
1710 
1711 	switch (csk->com.state) {
1712 	case CSK_STATE_ABORTING:
1713 		csk->com.state = CSK_STATE_DEAD;
1714 		cxgbit_put_csk(csk);
1715 		break;
1716 	default:
1717 		pr_info("%s: cpl_abort_rpl_rss in state %d\n",
1718 			__func__, csk->com.state);
1719 	}
1720 
1721 	__kfree_skb(skb);
1722 }
1723 
1724 static bool cxgbit_credit_err(const struct cxgbit_sock *csk)
1725 {
1726 	const struct sk_buff *skb = csk->wr_pending_head;
1727 	u32 credit = 0;
1728 
1729 	if (unlikely(csk->wr_cred > csk->wr_max_cred)) {
1730 		pr_err("csk 0x%p, tid %u, credit %u > %u\n",
1731 		       csk, csk->tid, csk->wr_cred, csk->wr_max_cred);
1732 		return true;
1733 	}
1734 
1735 	while (skb) {
1736 		credit += (__force u32)skb->csum;
1737 		skb = cxgbit_skcb_tx_wr_next(skb);
1738 	}
1739 
1740 	if (unlikely((csk->wr_cred + credit) != csk->wr_max_cred)) {
1741 		pr_err("csk 0x%p, tid %u, credit %u + %u != %u.\n",
1742 		       csk, csk->tid, csk->wr_cred,
1743 		       credit, csk->wr_max_cred);
1744 
1745 		return true;
1746 	}
1747 
1748 	return false;
1749 }
1750 
1751 static void cxgbit_fw4_ack(struct cxgbit_sock *csk, struct sk_buff *skb)
1752 {
1753 	struct cpl_fw4_ack *rpl = (struct cpl_fw4_ack *)cplhdr(skb);
1754 	u32 credits = rpl->credits;
1755 	u32 snd_una = ntohl(rpl->snd_una);
1756 
1757 	csk->wr_cred += credits;
1758 	if (csk->wr_una_cred > (csk->wr_max_cred - csk->wr_cred))
1759 		csk->wr_una_cred = csk->wr_max_cred - csk->wr_cred;
1760 
1761 	while (credits) {
1762 		struct sk_buff *p = cxgbit_sock_peek_wr(csk);
1763 		const u32 csum = (__force u32)p->csum;
1764 
1765 		if (unlikely(!p)) {
1766 			pr_err("csk 0x%p,%u, cr %u,%u+%u, empty.\n",
1767 			       csk, csk->tid, credits,
1768 			       csk->wr_cred, csk->wr_una_cred);
1769 			break;
1770 		}
1771 
1772 		if (unlikely(credits < csum)) {
1773 			pr_warn("csk 0x%p,%u, cr %u,%u+%u, < %u.\n",
1774 				csk,  csk->tid,
1775 				credits, csk->wr_cred, csk->wr_una_cred,
1776 				csum);
1777 			p->csum = (__force __wsum)(csum - credits);
1778 			break;
1779 		}
1780 
1781 		cxgbit_sock_dequeue_wr(csk);
1782 		credits -= csum;
1783 		kfree_skb(p);
1784 	}
1785 
1786 	if (unlikely(cxgbit_credit_err(csk))) {
1787 		cxgbit_queue_rx_skb(csk, skb);
1788 		return;
1789 	}
1790 
1791 	if (rpl->seq_vld & CPL_FW4_ACK_FLAGS_SEQVAL) {
1792 		if (unlikely(before(snd_una, csk->snd_una))) {
1793 			pr_warn("csk 0x%p,%u, snd_una %u/%u.",
1794 				csk, csk->tid, snd_una,
1795 				csk->snd_una);
1796 			goto rel_skb;
1797 		}
1798 
1799 		if (csk->snd_una != snd_una) {
1800 			csk->snd_una = snd_una;
1801 			dst_confirm(csk->dst);
1802 			wake_up(&csk->ack_waitq);
1803 		}
1804 	}
1805 
1806 	if (skb_queue_len(&csk->txq))
1807 		cxgbit_push_tx_frames(csk);
1808 
1809 rel_skb:
1810 	__kfree_skb(skb);
1811 }
1812 
1813 static void cxgbit_set_tcb_rpl(struct cxgbit_device *cdev, struct sk_buff *skb)
1814 {
1815 	struct cxgbit_sock *csk;
1816 	struct cpl_set_tcb_rpl *rpl = (struct cpl_set_tcb_rpl *)skb->data;
1817 	unsigned int tid = GET_TID(rpl);
1818 	struct cxgb4_lld_info *lldi = &cdev->lldi;
1819 	struct tid_info *t = lldi->tids;
1820 
1821 	csk = lookup_tid(t, tid);
1822 	if (unlikely(!csk))
1823 		pr_err("can't find connection for tid %u.\n", tid);
1824 	else
1825 		cxgbit_wake_up(&csk->com.wr_wait, __func__, rpl->status);
1826 
1827 	cxgbit_put_csk(csk);
1828 }
1829 
1830 static void cxgbit_rx_data(struct cxgbit_device *cdev, struct sk_buff *skb)
1831 {
1832 	struct cxgbit_sock *csk;
1833 	struct cpl_rx_data *cpl = cplhdr(skb);
1834 	unsigned int tid = GET_TID(cpl);
1835 	struct cxgb4_lld_info *lldi = &cdev->lldi;
1836 	struct tid_info *t = lldi->tids;
1837 
1838 	csk = lookup_tid(t, tid);
1839 	if (unlikely(!csk)) {
1840 		pr_err("can't find conn. for tid %u.\n", tid);
1841 		goto rel_skb;
1842 	}
1843 
1844 	cxgbit_queue_rx_skb(csk, skb);
1845 	return;
1846 rel_skb:
1847 	__kfree_skb(skb);
1848 }
1849 
1850 static void
1851 __cxgbit_process_rx_cpl(struct cxgbit_sock *csk, struct sk_buff *skb)
1852 {
1853 	spin_lock(&csk->lock);
1854 	if (csk->lock_owner) {
1855 		__skb_queue_tail(&csk->backlogq, skb);
1856 		spin_unlock(&csk->lock);
1857 		return;
1858 	}
1859 
1860 	cxgbit_skcb_rx_backlog_fn(skb)(csk, skb);
1861 	spin_unlock(&csk->lock);
1862 }
1863 
1864 static void cxgbit_process_rx_cpl(struct cxgbit_sock *csk, struct sk_buff *skb)
1865 {
1866 	cxgbit_get_csk(csk);
1867 	__cxgbit_process_rx_cpl(csk, skb);
1868 	cxgbit_put_csk(csk);
1869 }
1870 
1871 static void cxgbit_rx_cpl(struct cxgbit_device *cdev, struct sk_buff *skb)
1872 {
1873 	struct cxgbit_sock *csk;
1874 	struct cpl_tx_data *cpl = cplhdr(skb);
1875 	struct cxgb4_lld_info *lldi = &cdev->lldi;
1876 	struct tid_info *t = lldi->tids;
1877 	unsigned int tid = GET_TID(cpl);
1878 	u8 opcode = cxgbit_skcb_rx_opcode(skb);
1879 	bool ref = true;
1880 
1881 	switch (opcode) {
1882 	case CPL_FW4_ACK:
1883 			cxgbit_skcb_rx_backlog_fn(skb) = cxgbit_fw4_ack;
1884 			ref = false;
1885 			break;
1886 	case CPL_PEER_CLOSE:
1887 			cxgbit_skcb_rx_backlog_fn(skb) = cxgbit_peer_close;
1888 			break;
1889 	case CPL_CLOSE_CON_RPL:
1890 			cxgbit_skcb_rx_backlog_fn(skb) = cxgbit_close_con_rpl;
1891 			break;
1892 	case CPL_ABORT_REQ_RSS:
1893 			cxgbit_skcb_rx_backlog_fn(skb) = cxgbit_abort_req_rss;
1894 			break;
1895 	case CPL_ABORT_RPL_RSS:
1896 			cxgbit_skcb_rx_backlog_fn(skb) = cxgbit_abort_rpl_rss;
1897 			break;
1898 	default:
1899 		goto rel_skb;
1900 	}
1901 
1902 	csk = lookup_tid(t, tid);
1903 	if (unlikely(!csk)) {
1904 		pr_err("can't find conn. for tid %u.\n", tid);
1905 		goto rel_skb;
1906 	}
1907 
1908 	if (ref)
1909 		cxgbit_process_rx_cpl(csk, skb);
1910 	else
1911 		__cxgbit_process_rx_cpl(csk, skb);
1912 
1913 	return;
1914 rel_skb:
1915 	__kfree_skb(skb);
1916 }
1917 
1918 cxgbit_cplhandler_func cxgbit_cplhandlers[NUM_CPL_CMDS] = {
1919 	[CPL_PASS_OPEN_RPL]	= cxgbit_pass_open_rpl,
1920 	[CPL_CLOSE_LISTSRV_RPL] = cxgbit_close_listsrv_rpl,
1921 	[CPL_PASS_ACCEPT_REQ]	= cxgbit_pass_accept_req,
1922 	[CPL_PASS_ESTABLISH]	= cxgbit_pass_establish,
1923 	[CPL_SET_TCB_RPL]	= cxgbit_set_tcb_rpl,
1924 	[CPL_RX_DATA]		= cxgbit_rx_data,
1925 	[CPL_FW4_ACK]		= cxgbit_rx_cpl,
1926 	[CPL_PEER_CLOSE]	= cxgbit_rx_cpl,
1927 	[CPL_CLOSE_CON_RPL]	= cxgbit_rx_cpl,
1928 	[CPL_ABORT_REQ_RSS]	= cxgbit_rx_cpl,
1929 	[CPL_ABORT_RPL_RSS]	= cxgbit_rx_cpl,
1930 };
1931