xref: /linux/drivers/net/ethernet/chelsio/inline_crypto/chtls/chtls_cm.c (revision c28efb15f9e51a96c6bce2b92c0f3a4da87db877)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2018 Chelsio Communications, Inc.
4  *
5  * Written by: Atul Gupta (atul.gupta@chelsio.com)
6  */
7 
8 #include <linux/module.h>
9 #include <linux/list.h>
10 #include <linux/workqueue.h>
11 #include <linux/skbuff.h>
12 #include <linux/timer.h>
13 #include <linux/notifier.h>
14 #include <linux/inetdevice.h>
15 #include <linux/ip.h>
16 #include <linux/tcp.h>
17 #include <linux/sched/signal.h>
18 #include <linux/kallsyms.h>
19 #include <linux/kprobes.h>
20 #include <linux/if_vlan.h>
21 #include <linux/ipv6.h>
22 #include <net/ipv6.h>
23 #include <net/transp_v6.h>
24 #include <net/ip6_route.h>
25 #include <net/inet_common.h>
26 #include <net/tcp.h>
27 #include <net/dst.h>
28 #include <net/tls.h>
29 #include <net/addrconf.h>
30 #include <net/secure_seq.h>
31 
32 #include "chtls.h"
33 #include "chtls_cm.h"
34 #include "clip_tbl.h"
35 
36 /*
37  * State transitions and actions for close.  Note that if we are in SYN_SENT
38  * we remain in that state as we cannot control a connection while it's in
39  * SYN_SENT; such connections are allowed to establish and are then aborted.
40  */
41 static unsigned char new_state[16] = {
42 	/* current state:     new state:      action: */
43 	/* (Invalid)       */ TCP_CLOSE,
44 	/* TCP_ESTABLISHED */ TCP_FIN_WAIT1 | TCP_ACTION_FIN,
45 	/* TCP_SYN_SENT    */ TCP_SYN_SENT,
46 	/* TCP_SYN_RECV    */ TCP_FIN_WAIT1 | TCP_ACTION_FIN,
47 	/* TCP_FIN_WAIT1   */ TCP_FIN_WAIT1,
48 	/* TCP_FIN_WAIT2   */ TCP_FIN_WAIT2,
49 	/* TCP_TIME_WAIT   */ TCP_CLOSE,
50 	/* TCP_CLOSE       */ TCP_CLOSE,
51 	/* TCP_CLOSE_WAIT  */ TCP_LAST_ACK | TCP_ACTION_FIN,
52 	/* TCP_LAST_ACK    */ TCP_LAST_ACK,
53 	/* TCP_LISTEN      */ TCP_CLOSE,
54 	/* TCP_CLOSING     */ TCP_CLOSING,
55 };
56 
57 static struct chtls_sock *chtls_sock_create(struct chtls_dev *cdev)
58 {
59 	struct chtls_sock *csk = kzalloc(sizeof(*csk), GFP_ATOMIC);
60 
61 	if (!csk)
62 		return NULL;
63 
64 	csk->txdata_skb_cache = alloc_skb(TXDATA_SKB_LEN, GFP_ATOMIC);
65 	if (!csk->txdata_skb_cache) {
66 		kfree(csk);
67 		return NULL;
68 	}
69 
70 	kref_init(&csk->kref);
71 	csk->cdev = cdev;
72 	skb_queue_head_init(&csk->txq);
73 	csk->wr_skb_head = NULL;
74 	csk->wr_skb_tail = NULL;
75 	csk->mss = MAX_MSS;
76 	csk->tlshws.ofld = 1;
77 	csk->tlshws.txkey = -1;
78 	csk->tlshws.rxkey = -1;
79 	csk->tlshws.mfs = TLS_MFS;
80 	skb_queue_head_init(&csk->tlshws.sk_recv_queue);
81 	return csk;
82 }
83 
84 static void chtls_sock_release(struct kref *ref)
85 {
86 	struct chtls_sock *csk =
87 		container_of(ref, struct chtls_sock, kref);
88 
89 	kfree(csk);
90 }
91 
92 static struct net_device *chtls_find_netdev(struct chtls_dev *cdev,
93 					    struct sock *sk)
94 {
95 	struct adapter *adap = pci_get_drvdata(cdev->pdev);
96 	struct net_device *ndev = cdev->ports[0];
97 #if IS_ENABLED(CONFIG_IPV6)
98 	struct net_device *temp;
99 	int addr_type;
100 #endif
101 	int i;
102 
103 	switch (sk->sk_family) {
104 	case PF_INET:
105 		if (likely(!inet_sk(sk)->inet_rcv_saddr))
106 			return ndev;
107 		ndev = __ip_dev_find(&init_net, inet_sk(sk)->inet_rcv_saddr, false);
108 		break;
109 #if IS_ENABLED(CONFIG_IPV6)
110 	case PF_INET6:
111 		addr_type = ipv6_addr_type(&sk->sk_v6_rcv_saddr);
112 		if (likely(addr_type == IPV6_ADDR_ANY))
113 			return ndev;
114 
115 		for_each_netdev_rcu(&init_net, temp) {
116 			if (ipv6_chk_addr(&init_net, (struct in6_addr *)
117 					  &sk->sk_v6_rcv_saddr, temp, 1)) {
118 				ndev = temp;
119 				break;
120 			}
121 		}
122 	break;
123 #endif
124 	default:
125 		return NULL;
126 	}
127 
128 	if (!ndev)
129 		return NULL;
130 
131 	if (is_vlan_dev(ndev))
132 		ndev = vlan_dev_real_dev(ndev);
133 
134 	for_each_port(adap, i)
135 		if (cdev->ports[i] == ndev)
136 			return ndev;
137 	return NULL;
138 }
139 
140 static void assign_rxopt(struct sock *sk, unsigned int opt)
141 {
142 	const struct chtls_dev *cdev;
143 	struct chtls_sock *csk;
144 	struct tcp_sock *tp;
145 
146 	csk = rcu_dereference_sk_user_data(sk);
147 	tp = tcp_sk(sk);
148 
149 	cdev = csk->cdev;
150 	tp->tcp_header_len           = sizeof(struct tcphdr);
151 	tp->rx_opt.mss_clamp         = cdev->mtus[TCPOPT_MSS_G(opt)] - 40;
152 	tp->mss_cache                = tp->rx_opt.mss_clamp;
153 	tp->rx_opt.tstamp_ok         = TCPOPT_TSTAMP_G(opt);
154 	tp->rx_opt.snd_wscale        = TCPOPT_SACK_G(opt);
155 	tp->rx_opt.wscale_ok         = TCPOPT_WSCALE_OK_G(opt);
156 	SND_WSCALE(tp)               = TCPOPT_SND_WSCALE_G(opt);
157 	if (!tp->rx_opt.wscale_ok)
158 		tp->rx_opt.rcv_wscale = 0;
159 	if (tp->rx_opt.tstamp_ok) {
160 		tp->tcp_header_len += TCPOLEN_TSTAMP_ALIGNED;
161 		tp->rx_opt.mss_clamp -= TCPOLEN_TSTAMP_ALIGNED;
162 	} else if (csk->opt2 & TSTAMPS_EN_F) {
163 		csk->opt2 &= ~TSTAMPS_EN_F;
164 		csk->mtu_idx = TCPOPT_MSS_G(opt);
165 	}
166 }
167 
168 static void chtls_purge_receive_queue(struct sock *sk)
169 {
170 	struct sk_buff *skb;
171 
172 	while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
173 		skb_dst_set(skb, (void *)NULL);
174 		kfree_skb(skb);
175 	}
176 }
177 
178 static void chtls_purge_write_queue(struct sock *sk)
179 {
180 	struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
181 	struct sk_buff *skb;
182 
183 	while ((skb = __skb_dequeue(&csk->txq))) {
184 		sk->sk_wmem_queued -= skb->truesize;
185 		__kfree_skb(skb);
186 	}
187 }
188 
189 static void chtls_purge_recv_queue(struct sock *sk)
190 {
191 	struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
192 	struct chtls_hws *tlsk = &csk->tlshws;
193 	struct sk_buff *skb;
194 
195 	while ((skb = __skb_dequeue(&tlsk->sk_recv_queue)) != NULL) {
196 		skb_dst_set(skb, NULL);
197 		kfree_skb(skb);
198 	}
199 }
200 
201 static void abort_arp_failure(void *handle, struct sk_buff *skb)
202 {
203 	struct cpl_abort_req *req = cplhdr(skb);
204 	struct chtls_dev *cdev;
205 
206 	cdev = (struct chtls_dev *)handle;
207 	req->cmd = CPL_ABORT_NO_RST;
208 	cxgb4_ofld_send(cdev->lldi->ports[0], skb);
209 }
210 
211 static struct sk_buff *alloc_ctrl_skb(struct sk_buff *skb, int len)
212 {
213 	if (likely(skb && !skb_shared(skb) && !skb_cloned(skb))) {
214 		__skb_trim(skb, 0);
215 		refcount_inc(&skb->users);
216 	} else {
217 		skb = alloc_skb(len, GFP_KERNEL | __GFP_NOFAIL);
218 	}
219 	return skb;
220 }
221 
222 static void chtls_send_abort(struct sock *sk, int mode, struct sk_buff *skb)
223 {
224 	struct cpl_abort_req *req;
225 	struct chtls_sock *csk;
226 	struct tcp_sock *tp;
227 
228 	csk = rcu_dereference_sk_user_data(sk);
229 	tp = tcp_sk(sk);
230 
231 	if (!skb)
232 		skb = alloc_ctrl_skb(csk->txdata_skb_cache, sizeof(*req));
233 
234 	req = (struct cpl_abort_req *)skb_put(skb, sizeof(*req));
235 	INIT_TP_WR_CPL(req, CPL_ABORT_REQ, csk->tid);
236 	skb_set_queue_mapping(skb, (csk->txq_idx << 1) | CPL_PRIORITY_DATA);
237 	req->rsvd0 = htonl(tp->snd_nxt);
238 	req->rsvd1 = !csk_flag_nochk(csk, CSK_TX_DATA_SENT);
239 	req->cmd = mode;
240 	t4_set_arp_err_handler(skb, csk->cdev, abort_arp_failure);
241 	send_or_defer(sk, tp, skb, mode == CPL_ABORT_SEND_RST);
242 }
243 
244 static void chtls_send_reset(struct sock *sk, int mode, struct sk_buff *skb)
245 {
246 	struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
247 
248 	if (unlikely(csk_flag_nochk(csk, CSK_ABORT_SHUTDOWN) ||
249 		     !csk->cdev)) {
250 		if (sk->sk_state == TCP_SYN_RECV)
251 			csk_set_flag(csk, CSK_RST_ABORTED);
252 		goto out;
253 	}
254 
255 	if (!csk_flag_nochk(csk, CSK_TX_DATA_SENT)) {
256 		struct tcp_sock *tp = tcp_sk(sk);
257 
258 		if (send_tx_flowc_wr(sk, 0, tp->snd_nxt, tp->rcv_nxt) < 0)
259 			WARN_ONCE(1, "send tx flowc error");
260 		csk_set_flag(csk, CSK_TX_DATA_SENT);
261 	}
262 
263 	csk_set_flag(csk, CSK_ABORT_RPL_PENDING);
264 	chtls_purge_write_queue(sk);
265 
266 	csk_set_flag(csk, CSK_ABORT_SHUTDOWN);
267 	if (sk->sk_state != TCP_SYN_RECV)
268 		chtls_send_abort(sk, mode, skb);
269 	else
270 		goto out;
271 
272 	return;
273 out:
274 	kfree_skb(skb);
275 }
276 
277 static void release_tcp_port(struct sock *sk)
278 {
279 	if (inet_csk(sk)->icsk_bind_hash)
280 		inet_put_port(sk);
281 }
282 
283 static void tcp_uncork(struct sock *sk)
284 {
285 	struct tcp_sock *tp = tcp_sk(sk);
286 
287 	if (tp->nonagle & TCP_NAGLE_CORK) {
288 		tp->nonagle &= ~TCP_NAGLE_CORK;
289 		chtls_tcp_push(sk, 0);
290 	}
291 }
292 
293 static void chtls_close_conn(struct sock *sk)
294 {
295 	struct cpl_close_con_req *req;
296 	struct chtls_sock *csk;
297 	struct sk_buff *skb;
298 	unsigned int tid;
299 	unsigned int len;
300 
301 	len = roundup(sizeof(struct cpl_close_con_req), 16);
302 	csk = rcu_dereference_sk_user_data(sk);
303 	tid = csk->tid;
304 
305 	skb = alloc_skb(len, GFP_KERNEL | __GFP_NOFAIL);
306 	req = (struct cpl_close_con_req *)__skb_put(skb, len);
307 	memset(req, 0, len);
308 	req->wr.wr_hi = htonl(FW_WR_OP_V(FW_TP_WR) |
309 			      FW_WR_IMMDLEN_V(sizeof(*req) -
310 					      sizeof(req->wr)));
311 	req->wr.wr_mid = htonl(FW_WR_LEN16_V(DIV_ROUND_UP(sizeof(*req), 16)) |
312 			       FW_WR_FLOWID_V(tid));
313 
314 	OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_CON_REQ, tid));
315 
316 	tcp_uncork(sk);
317 	skb_entail(sk, skb, ULPCB_FLAG_NO_HDR | ULPCB_FLAG_NO_APPEND);
318 	if (sk->sk_state != TCP_SYN_SENT)
319 		chtls_push_frames(csk, 1);
320 }
321 
322 /*
323  * Perform a state transition during close and return the actions indicated
324  * for the transition.  Do not make this function inline, the main reason
325  * it exists at all is to avoid multiple inlining of tcp_set_state.
326  */
327 static int make_close_transition(struct sock *sk)
328 {
329 	int next = (int)new_state[sk->sk_state];
330 
331 	tcp_set_state(sk, next & TCP_STATE_MASK);
332 	return next & TCP_ACTION_FIN;
333 }
334 
335 void chtls_close(struct sock *sk, long timeout)
336 {
337 	int data_lost, prev_state;
338 	struct chtls_sock *csk;
339 
340 	csk = rcu_dereference_sk_user_data(sk);
341 
342 	lock_sock(sk);
343 	sk->sk_shutdown |= SHUTDOWN_MASK;
344 
345 	data_lost = skb_queue_len(&sk->sk_receive_queue);
346 	data_lost |= skb_queue_len(&csk->tlshws.sk_recv_queue);
347 	chtls_purge_recv_queue(sk);
348 	chtls_purge_receive_queue(sk);
349 
350 	if (sk->sk_state == TCP_CLOSE) {
351 		goto wait;
352 	} else if (data_lost || sk->sk_state == TCP_SYN_SENT) {
353 		chtls_send_reset(sk, CPL_ABORT_SEND_RST, NULL);
354 		release_tcp_port(sk);
355 		goto unlock;
356 	} else if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) {
357 		sk->sk_prot->disconnect(sk, 0);
358 	} else if (make_close_transition(sk)) {
359 		chtls_close_conn(sk);
360 	}
361 wait:
362 	if (timeout)
363 		sk_stream_wait_close(sk, timeout);
364 
365 unlock:
366 	prev_state = sk->sk_state;
367 	sock_hold(sk);
368 	sock_orphan(sk);
369 
370 	release_sock(sk);
371 
372 	local_bh_disable();
373 	bh_lock_sock(sk);
374 
375 	if (prev_state != TCP_CLOSE && sk->sk_state == TCP_CLOSE)
376 		goto out;
377 
378 	if (sk->sk_state == TCP_FIN_WAIT2 && tcp_sk(sk)->linger2 < 0 &&
379 	    !csk_flag(sk, CSK_ABORT_SHUTDOWN)) {
380 		struct sk_buff *skb;
381 
382 		skb = alloc_skb(sizeof(struct cpl_abort_req), GFP_ATOMIC);
383 		if (skb)
384 			chtls_send_reset(sk, CPL_ABORT_SEND_RST, skb);
385 	}
386 
387 	if (sk->sk_state == TCP_CLOSE)
388 		inet_csk_destroy_sock(sk);
389 
390 out:
391 	bh_unlock_sock(sk);
392 	local_bh_enable();
393 	sock_put(sk);
394 }
395 
396 /*
397  * Wait until a socket enters on of the given states.
398  */
399 static int wait_for_states(struct sock *sk, unsigned int states)
400 {
401 	DECLARE_WAITQUEUE(wait, current);
402 	struct socket_wq _sk_wq;
403 	long current_timeo;
404 	int err = 0;
405 
406 	current_timeo = 200;
407 
408 	/*
409 	 * We want this to work even when there's no associated struct socket.
410 	 * In that case we provide a temporary wait_queue_head_t.
411 	 */
412 	if (!sk->sk_wq) {
413 		init_waitqueue_head(&_sk_wq.wait);
414 		_sk_wq.fasync_list = NULL;
415 		init_rcu_head_on_stack(&_sk_wq.rcu);
416 		RCU_INIT_POINTER(sk->sk_wq, &_sk_wq);
417 	}
418 
419 	add_wait_queue(sk_sleep(sk), &wait);
420 	while (!sk_in_state(sk, states)) {
421 		if (!current_timeo) {
422 			err = -EBUSY;
423 			break;
424 		}
425 		if (signal_pending(current)) {
426 			err = sock_intr_errno(current_timeo);
427 			break;
428 		}
429 		set_current_state(TASK_UNINTERRUPTIBLE);
430 		release_sock(sk);
431 		if (!sk_in_state(sk, states))
432 			current_timeo = schedule_timeout(current_timeo);
433 		__set_current_state(TASK_RUNNING);
434 		lock_sock(sk);
435 	}
436 	remove_wait_queue(sk_sleep(sk), &wait);
437 
438 	if (rcu_dereference(sk->sk_wq) == &_sk_wq)
439 		sk->sk_wq = NULL;
440 	return err;
441 }
442 
443 int chtls_disconnect(struct sock *sk, int flags)
444 {
445 	struct tcp_sock *tp;
446 	int err;
447 
448 	tp = tcp_sk(sk);
449 	chtls_purge_recv_queue(sk);
450 	chtls_purge_receive_queue(sk);
451 	chtls_purge_write_queue(sk);
452 
453 	if (sk->sk_state != TCP_CLOSE) {
454 		sk->sk_err = ECONNRESET;
455 		chtls_send_reset(sk, CPL_ABORT_SEND_RST, NULL);
456 		err = wait_for_states(sk, TCPF_CLOSE);
457 		if (err)
458 			return err;
459 	}
460 	chtls_purge_recv_queue(sk);
461 	chtls_purge_receive_queue(sk);
462 	tp->max_window = 0xFFFF << (tp->rx_opt.snd_wscale);
463 	return tcp_disconnect(sk, flags);
464 }
465 
466 #define SHUTDOWN_ELIGIBLE_STATE (TCPF_ESTABLISHED | \
467 				 TCPF_SYN_RECV | TCPF_CLOSE_WAIT)
468 void chtls_shutdown(struct sock *sk, int how)
469 {
470 	if ((how & SEND_SHUTDOWN) &&
471 	    sk_in_state(sk, SHUTDOWN_ELIGIBLE_STATE) &&
472 	    make_close_transition(sk))
473 		chtls_close_conn(sk);
474 }
475 
476 void chtls_destroy_sock(struct sock *sk)
477 {
478 	struct chtls_sock *csk;
479 
480 	csk = rcu_dereference_sk_user_data(sk);
481 	chtls_purge_recv_queue(sk);
482 	csk->ulp_mode = ULP_MODE_NONE;
483 	chtls_purge_write_queue(sk);
484 	free_tls_keyid(sk);
485 	kref_put(&csk->kref, chtls_sock_release);
486 	if (sk->sk_family == AF_INET)
487 		sk->sk_prot = &tcp_prot;
488 #if IS_ENABLED(CONFIG_IPV6)
489 	else
490 		sk->sk_prot = &tcpv6_prot;
491 #endif
492 	sk->sk_prot->destroy(sk);
493 }
494 
495 static void reset_listen_child(struct sock *child)
496 {
497 	struct chtls_sock *csk = rcu_dereference_sk_user_data(child);
498 	struct sk_buff *skb;
499 
500 	skb = alloc_ctrl_skb(csk->txdata_skb_cache,
501 			     sizeof(struct cpl_abort_req));
502 
503 	chtls_send_reset(child, CPL_ABORT_SEND_RST, skb);
504 	sock_orphan(child);
505 	INC_ORPHAN_COUNT(child);
506 	if (child->sk_state == TCP_CLOSE)
507 		inet_csk_destroy_sock(child);
508 }
509 
510 static void chtls_disconnect_acceptq(struct sock *listen_sk)
511 {
512 	struct request_sock **pprev;
513 
514 	pprev = ACCEPT_QUEUE(listen_sk);
515 	while (*pprev) {
516 		struct request_sock *req = *pprev;
517 
518 		if (req->rsk_ops == &chtls_rsk_ops ||
519 		    req->rsk_ops == &chtls_rsk_opsv6) {
520 			struct sock *child = req->sk;
521 
522 			*pprev = req->dl_next;
523 			sk_acceptq_removed(listen_sk);
524 			reqsk_put(req);
525 			sock_hold(child);
526 			local_bh_disable();
527 			bh_lock_sock(child);
528 			release_tcp_port(child);
529 			reset_listen_child(child);
530 			bh_unlock_sock(child);
531 			local_bh_enable();
532 			sock_put(child);
533 		} else {
534 			pprev = &req->dl_next;
535 		}
536 	}
537 }
538 
539 static int listen_hashfn(const struct sock *sk)
540 {
541 	return ((unsigned long)sk >> 10) & (LISTEN_INFO_HASH_SIZE - 1);
542 }
543 
544 static struct listen_info *listen_hash_add(struct chtls_dev *cdev,
545 					   struct sock *sk,
546 					   unsigned int stid)
547 {
548 	struct listen_info *p = kmalloc(sizeof(*p), GFP_KERNEL);
549 
550 	if (p) {
551 		int key = listen_hashfn(sk);
552 
553 		p->sk = sk;
554 		p->stid = stid;
555 		spin_lock(&cdev->listen_lock);
556 		p->next = cdev->listen_hash_tab[key];
557 		cdev->listen_hash_tab[key] = p;
558 		spin_unlock(&cdev->listen_lock);
559 	}
560 	return p;
561 }
562 
563 static int listen_hash_find(struct chtls_dev *cdev,
564 			    struct sock *sk)
565 {
566 	struct listen_info *p;
567 	int stid = -1;
568 	int key;
569 
570 	key = listen_hashfn(sk);
571 
572 	spin_lock(&cdev->listen_lock);
573 	for (p = cdev->listen_hash_tab[key]; p; p = p->next)
574 		if (p->sk == sk) {
575 			stid = p->stid;
576 			break;
577 		}
578 	spin_unlock(&cdev->listen_lock);
579 	return stid;
580 }
581 
582 static int listen_hash_del(struct chtls_dev *cdev,
583 			   struct sock *sk)
584 {
585 	struct listen_info *p, **prev;
586 	int stid = -1;
587 	int key;
588 
589 	key = listen_hashfn(sk);
590 	prev = &cdev->listen_hash_tab[key];
591 
592 	spin_lock(&cdev->listen_lock);
593 	for (p = *prev; p; prev = &p->next, p = p->next)
594 		if (p->sk == sk) {
595 			stid = p->stid;
596 			*prev = p->next;
597 			kfree(p);
598 			break;
599 		}
600 	spin_unlock(&cdev->listen_lock);
601 	return stid;
602 }
603 
604 static void cleanup_syn_rcv_conn(struct sock *child, struct sock *parent)
605 {
606 	struct request_sock *req;
607 	struct chtls_sock *csk;
608 
609 	csk = rcu_dereference_sk_user_data(child);
610 	req = csk->passive_reap_next;
611 
612 	reqsk_queue_removed(&inet_csk(parent)->icsk_accept_queue, req);
613 	__skb_unlink((struct sk_buff *)&csk->synq, &csk->listen_ctx->synq);
614 	chtls_reqsk_free(req);
615 	csk->passive_reap_next = NULL;
616 }
617 
618 static void chtls_reset_synq(struct listen_ctx *listen_ctx)
619 {
620 	struct sock *listen_sk = listen_ctx->lsk;
621 
622 	while (!skb_queue_empty(&listen_ctx->synq)) {
623 		struct chtls_sock *csk =
624 			container_of((struct synq *)skb_peek
625 				(&listen_ctx->synq), struct chtls_sock, synq);
626 		struct sock *child = csk->sk;
627 
628 		cleanup_syn_rcv_conn(child, listen_sk);
629 		sock_hold(child);
630 		local_bh_disable();
631 		bh_lock_sock(child);
632 		release_tcp_port(child);
633 		reset_listen_child(child);
634 		bh_unlock_sock(child);
635 		local_bh_enable();
636 		sock_put(child);
637 	}
638 }
639 
640 int chtls_listen_start(struct chtls_dev *cdev, struct sock *sk)
641 {
642 	struct net_device *ndev;
643 #if IS_ENABLED(CONFIG_IPV6)
644 	bool clip_valid = false;
645 #endif
646 	struct listen_ctx *ctx;
647 	struct adapter *adap;
648 	struct port_info *pi;
649 	int ret = 0;
650 	int stid;
651 
652 	rcu_read_lock();
653 	ndev = chtls_find_netdev(cdev, sk);
654 	rcu_read_unlock();
655 	if (!ndev)
656 		return -EBADF;
657 
658 	pi = netdev_priv(ndev);
659 	adap = pi->adapter;
660 	if (!(adap->flags & CXGB4_FULL_INIT_DONE))
661 		return -EBADF;
662 
663 	if (listen_hash_find(cdev, sk) >= 0)   /* already have it */
664 		return -EADDRINUSE;
665 
666 	ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
667 	if (!ctx)
668 		return -ENOMEM;
669 
670 	__module_get(THIS_MODULE);
671 	ctx->lsk = sk;
672 	ctx->cdev = cdev;
673 	ctx->state = T4_LISTEN_START_PENDING;
674 	skb_queue_head_init(&ctx->synq);
675 
676 	stid = cxgb4_alloc_stid(cdev->tids, sk->sk_family, ctx);
677 	if (stid < 0)
678 		goto free_ctx;
679 
680 	sock_hold(sk);
681 	if (!listen_hash_add(cdev, sk, stid))
682 		goto free_stid;
683 
684 	if (sk->sk_family == PF_INET) {
685 		ret = cxgb4_create_server(ndev, stid,
686 					  inet_sk(sk)->inet_rcv_saddr,
687 					  inet_sk(sk)->inet_sport, 0,
688 					  cdev->lldi->rxq_ids[0]);
689 #if IS_ENABLED(CONFIG_IPV6)
690 	} else {
691 		int addr_type;
692 
693 		addr_type = ipv6_addr_type(&sk->sk_v6_rcv_saddr);
694 		if (addr_type != IPV6_ADDR_ANY) {
695 			ret = cxgb4_clip_get(ndev, (const u32 *)
696 					     &sk->sk_v6_rcv_saddr, 1);
697 			if (ret)
698 				goto del_hash;
699 			clip_valid = true;
700 		}
701 		ret = cxgb4_create_server6(ndev, stid,
702 					   &sk->sk_v6_rcv_saddr,
703 					   inet_sk(sk)->inet_sport,
704 					   cdev->lldi->rxq_ids[0]);
705 #endif
706 	}
707 	if (ret > 0)
708 		ret = net_xmit_errno(ret);
709 	if (ret)
710 		goto del_hash;
711 	return 0;
712 del_hash:
713 #if IS_ENABLED(CONFIG_IPV6)
714 	if (clip_valid)
715 		cxgb4_clip_release(ndev, (const u32 *)&sk->sk_v6_rcv_saddr, 1);
716 #endif
717 	listen_hash_del(cdev, sk);
718 free_stid:
719 	cxgb4_free_stid(cdev->tids, stid, sk->sk_family);
720 	sock_put(sk);
721 free_ctx:
722 	kfree(ctx);
723 	module_put(THIS_MODULE);
724 	return -EBADF;
725 }
726 
727 void chtls_listen_stop(struct chtls_dev *cdev, struct sock *sk)
728 {
729 	struct listen_ctx *listen_ctx;
730 	int stid;
731 
732 	stid = listen_hash_del(cdev, sk);
733 	if (stid < 0)
734 		return;
735 
736 	listen_ctx = (struct listen_ctx *)lookup_stid(cdev->tids, stid);
737 	chtls_reset_synq(listen_ctx);
738 
739 	cxgb4_remove_server(cdev->lldi->ports[0], stid,
740 			    cdev->lldi->rxq_ids[0], sk->sk_family == PF_INET6);
741 
742 #if IS_ENABLED(CONFIG_IPV6)
743 	if (sk->sk_family == PF_INET6) {
744 		struct net_device *ndev = chtls_find_netdev(cdev, sk);
745 		int addr_type = 0;
746 
747 		addr_type = ipv6_addr_type((const struct in6_addr *)
748 					  &sk->sk_v6_rcv_saddr);
749 		if (addr_type != IPV6_ADDR_ANY)
750 			cxgb4_clip_release(ndev, (const u32 *)
751 					   &sk->sk_v6_rcv_saddr, 1);
752 	}
753 #endif
754 	chtls_disconnect_acceptq(sk);
755 }
756 
757 static int chtls_pass_open_rpl(struct chtls_dev *cdev, struct sk_buff *skb)
758 {
759 	struct cpl_pass_open_rpl *rpl = cplhdr(skb) + RSS_HDR;
760 	unsigned int stid = GET_TID(rpl);
761 	struct listen_ctx *listen_ctx;
762 
763 	listen_ctx = (struct listen_ctx *)lookup_stid(cdev->tids, stid);
764 	if (!listen_ctx)
765 		return CPL_RET_BUF_DONE;
766 
767 	if (listen_ctx->state == T4_LISTEN_START_PENDING) {
768 		listen_ctx->state = T4_LISTEN_STARTED;
769 		return CPL_RET_BUF_DONE;
770 	}
771 
772 	if (rpl->status != CPL_ERR_NONE) {
773 		pr_info("Unexpected PASS_OPEN_RPL status %u for STID %u\n",
774 			rpl->status, stid);
775 	} else {
776 		cxgb4_free_stid(cdev->tids, stid, listen_ctx->lsk->sk_family);
777 		sock_put(listen_ctx->lsk);
778 		kfree(listen_ctx);
779 		module_put(THIS_MODULE);
780 	}
781 	return CPL_RET_BUF_DONE;
782 }
783 
784 static int chtls_close_listsrv_rpl(struct chtls_dev *cdev, struct sk_buff *skb)
785 {
786 	struct cpl_close_listsvr_rpl *rpl = cplhdr(skb) + RSS_HDR;
787 	struct listen_ctx *listen_ctx;
788 	unsigned int stid;
789 	void *data;
790 
791 	stid = GET_TID(rpl);
792 	data = lookup_stid(cdev->tids, stid);
793 	listen_ctx = (struct listen_ctx *)data;
794 
795 	if (rpl->status != CPL_ERR_NONE) {
796 		pr_info("Unexpected CLOSE_LISTSRV_RPL status %u for STID %u\n",
797 			rpl->status, stid);
798 	} else {
799 		cxgb4_free_stid(cdev->tids, stid, listen_ctx->lsk->sk_family);
800 		sock_put(listen_ctx->lsk);
801 		kfree(listen_ctx);
802 		module_put(THIS_MODULE);
803 	}
804 	return CPL_RET_BUF_DONE;
805 }
806 
807 static void chtls_purge_wr_queue(struct sock *sk)
808 {
809 	struct sk_buff *skb;
810 
811 	while ((skb = dequeue_wr(sk)) != NULL)
812 		kfree_skb(skb);
813 }
814 
815 static void chtls_release_resources(struct sock *sk)
816 {
817 	struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
818 	struct chtls_dev *cdev = csk->cdev;
819 	unsigned int tid = csk->tid;
820 	struct tid_info *tids;
821 
822 	if (!cdev)
823 		return;
824 
825 	tids = cdev->tids;
826 	kfree_skb(csk->txdata_skb_cache);
827 	csk->txdata_skb_cache = NULL;
828 
829 	if (csk->wr_credits != csk->wr_max_credits) {
830 		chtls_purge_wr_queue(sk);
831 		chtls_reset_wr_list(csk);
832 	}
833 
834 	if (csk->l2t_entry) {
835 		cxgb4_l2t_release(csk->l2t_entry);
836 		csk->l2t_entry = NULL;
837 	}
838 
839 	if (sk->sk_state != TCP_SYN_SENT) {
840 		cxgb4_remove_tid(tids, csk->port_id, tid, sk->sk_family);
841 		sock_put(sk);
842 	}
843 }
844 
845 static void chtls_conn_done(struct sock *sk)
846 {
847 	if (sock_flag(sk, SOCK_DEAD))
848 		chtls_purge_receive_queue(sk);
849 	sk_wakeup_sleepers(sk, 0);
850 	tcp_done(sk);
851 }
852 
853 static void do_abort_syn_rcv(struct sock *child, struct sock *parent)
854 {
855 	/*
856 	 * If the server is still open we clean up the child connection,
857 	 * otherwise the server already did the clean up as it was purging
858 	 * its SYN queue and the skb was just sitting in its backlog.
859 	 */
860 	if (likely(parent->sk_state == TCP_LISTEN)) {
861 		cleanup_syn_rcv_conn(child, parent);
862 		/* Without the below call to sock_orphan,
863 		 * we leak the socket resource with syn_flood test
864 		 * as inet_csk_destroy_sock will not be called
865 		 * in tcp_done since SOCK_DEAD flag is not set.
866 		 * Kernel handles this differently where new socket is
867 		 * created only after 3 way handshake is done.
868 		 */
869 		sock_orphan(child);
870 		percpu_counter_inc((child)->sk_prot->orphan_count);
871 		chtls_release_resources(child);
872 		chtls_conn_done(child);
873 	} else {
874 		if (csk_flag(child, CSK_RST_ABORTED)) {
875 			chtls_release_resources(child);
876 			chtls_conn_done(child);
877 		}
878 	}
879 }
880 
881 static void pass_open_abort(struct sock *child, struct sock *parent,
882 			    struct sk_buff *skb)
883 {
884 	do_abort_syn_rcv(child, parent);
885 	kfree_skb(skb);
886 }
887 
888 static void bl_pass_open_abort(struct sock *lsk, struct sk_buff *skb)
889 {
890 	pass_open_abort(skb->sk, lsk, skb);
891 }
892 
893 static void chtls_pass_open_arp_failure(struct sock *sk,
894 					struct sk_buff *skb)
895 {
896 	const struct request_sock *oreq;
897 	struct chtls_sock *csk;
898 	struct chtls_dev *cdev;
899 	struct sock *parent;
900 	void *data;
901 
902 	csk = rcu_dereference_sk_user_data(sk);
903 	cdev = csk->cdev;
904 
905 	/*
906 	 * If the connection is being aborted due to the parent listening
907 	 * socket going away there's nothing to do, the ABORT_REQ will close
908 	 * the connection.
909 	 */
910 	if (csk_flag(sk, CSK_ABORT_RPL_PENDING)) {
911 		kfree_skb(skb);
912 		return;
913 	}
914 
915 	oreq = csk->passive_reap_next;
916 	data = lookup_stid(cdev->tids, oreq->ts_recent);
917 	parent = ((struct listen_ctx *)data)->lsk;
918 
919 	bh_lock_sock(parent);
920 	if (!sock_owned_by_user(parent)) {
921 		pass_open_abort(sk, parent, skb);
922 	} else {
923 		BLOG_SKB_CB(skb)->backlog_rcv = bl_pass_open_abort;
924 		__sk_add_backlog(parent, skb);
925 	}
926 	bh_unlock_sock(parent);
927 }
928 
929 static void chtls_accept_rpl_arp_failure(void *handle,
930 					 struct sk_buff *skb)
931 {
932 	struct sock *sk = (struct sock *)handle;
933 
934 	sock_hold(sk);
935 	process_cpl_msg(chtls_pass_open_arp_failure, sk, skb);
936 	sock_put(sk);
937 }
938 
939 static unsigned int chtls_select_mss(const struct chtls_sock *csk,
940 				     unsigned int pmtu,
941 				     struct cpl_pass_accept_req *req)
942 {
943 	struct chtls_dev *cdev;
944 	struct dst_entry *dst;
945 	unsigned int tcpoptsz;
946 	unsigned int iphdrsz;
947 	unsigned int mtu_idx;
948 	struct tcp_sock *tp;
949 	unsigned int mss;
950 	struct sock *sk;
951 
952 	mss = ntohs(req->tcpopt.mss);
953 	sk = csk->sk;
954 	dst = __sk_dst_get(sk);
955 	cdev = csk->cdev;
956 	tp = tcp_sk(sk);
957 	tcpoptsz = 0;
958 
959 #if IS_ENABLED(CONFIG_IPV6)
960 	if (sk->sk_family == AF_INET6)
961 		iphdrsz = sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
962 	else
963 #endif
964 		iphdrsz = sizeof(struct iphdr) + sizeof(struct tcphdr);
965 	if (req->tcpopt.tstamp)
966 		tcpoptsz += round_up(TCPOLEN_TIMESTAMP, 4);
967 
968 	tp->advmss = dst_metric_advmss(dst);
969 	if (USER_MSS(tp) && tp->advmss > USER_MSS(tp))
970 		tp->advmss = USER_MSS(tp);
971 	if (tp->advmss > pmtu - iphdrsz)
972 		tp->advmss = pmtu - iphdrsz;
973 	if (mss && tp->advmss > mss)
974 		tp->advmss = mss;
975 
976 	tp->advmss = cxgb4_best_aligned_mtu(cdev->lldi->mtus,
977 					    iphdrsz + tcpoptsz,
978 					    tp->advmss - tcpoptsz,
979 					    8, &mtu_idx);
980 	tp->advmss -= iphdrsz;
981 
982 	inet_csk(sk)->icsk_pmtu_cookie = pmtu;
983 	return mtu_idx;
984 }
985 
986 static unsigned int select_rcv_wscale(int space, int wscale_ok, int win_clamp)
987 {
988 	int wscale = 0;
989 
990 	if (space > MAX_RCV_WND)
991 		space = MAX_RCV_WND;
992 	if (win_clamp && win_clamp < space)
993 		space = win_clamp;
994 
995 	if (wscale_ok) {
996 		while (wscale < 14 && (65535 << wscale) < space)
997 			wscale++;
998 	}
999 	return wscale;
1000 }
1001 
1002 static void chtls_pass_accept_rpl(struct sk_buff *skb,
1003 				  struct cpl_pass_accept_req *req,
1004 				  unsigned int tid)
1005 
1006 {
1007 	struct cpl_t5_pass_accept_rpl *rpl5;
1008 	struct cxgb4_lld_info *lldi;
1009 	const struct tcphdr *tcph;
1010 	const struct tcp_sock *tp;
1011 	struct chtls_sock *csk;
1012 	unsigned int len;
1013 	struct sock *sk;
1014 	u32 opt2, hlen;
1015 	u64 opt0;
1016 
1017 	sk = skb->sk;
1018 	tp = tcp_sk(sk);
1019 	csk = sk->sk_user_data;
1020 	csk->tid = tid;
1021 	lldi = csk->cdev->lldi;
1022 	len = roundup(sizeof(*rpl5), 16);
1023 
1024 	rpl5 = __skb_put_zero(skb, len);
1025 	INIT_TP_WR(rpl5, tid);
1026 
1027 	OPCODE_TID(rpl5) = cpu_to_be32(MK_OPCODE_TID(CPL_PASS_ACCEPT_RPL,
1028 						     csk->tid));
1029 	csk->mtu_idx = chtls_select_mss(csk, dst_mtu(__sk_dst_get(sk)),
1030 					req);
1031 	opt0 = TCAM_BYPASS_F |
1032 	       WND_SCALE_V(RCV_WSCALE(tp)) |
1033 	       MSS_IDX_V(csk->mtu_idx) |
1034 	       L2T_IDX_V(csk->l2t_entry->idx) |
1035 	       NAGLE_V(!(tp->nonagle & TCP_NAGLE_OFF)) |
1036 	       TX_CHAN_V(csk->tx_chan) |
1037 	       SMAC_SEL_V(csk->smac_idx) |
1038 	       DSCP_V(csk->tos >> 2) |
1039 	       ULP_MODE_V(ULP_MODE_TLS) |
1040 	       RCV_BUFSIZ_V(min(tp->rcv_wnd >> 10, RCV_BUFSIZ_M));
1041 
1042 	opt2 = RX_CHANNEL_V(0) |
1043 		RSS_QUEUE_VALID_F | RSS_QUEUE_V(csk->rss_qid);
1044 
1045 	if (!is_t5(lldi->adapter_type))
1046 		opt2 |= RX_FC_DISABLE_F;
1047 	if (req->tcpopt.tstamp)
1048 		opt2 |= TSTAMPS_EN_F;
1049 	if (req->tcpopt.sack)
1050 		opt2 |= SACK_EN_F;
1051 	hlen = ntohl(req->hdr_len);
1052 
1053 	tcph = (struct tcphdr *)((u8 *)(req + 1) +
1054 			T6_ETH_HDR_LEN_G(hlen) + T6_IP_HDR_LEN_G(hlen));
1055 	if (tcph->ece && tcph->cwr)
1056 		opt2 |= CCTRL_ECN_V(1);
1057 	opt2 |= CONG_CNTRL_V(CONG_ALG_NEWRENO);
1058 	opt2 |= T5_ISS_F;
1059 	opt2 |= T5_OPT_2_VALID_F;
1060 	opt2 |= WND_SCALE_EN_V(WSCALE_OK(tp));
1061 	rpl5->opt0 = cpu_to_be64(opt0);
1062 	rpl5->opt2 = cpu_to_be32(opt2);
1063 	rpl5->iss = cpu_to_be32((prandom_u32() & ~7UL) - 1);
1064 	set_wr_txq(skb, CPL_PRIORITY_SETUP, csk->port_id);
1065 	t4_set_arp_err_handler(skb, sk, chtls_accept_rpl_arp_failure);
1066 	cxgb4_l2t_send(csk->egress_dev, skb, csk->l2t_entry);
1067 }
1068 
1069 static void inet_inherit_port(struct inet_hashinfo *hash_info,
1070 			      struct sock *lsk, struct sock *newsk)
1071 {
1072 	local_bh_disable();
1073 	__inet_inherit_port(lsk, newsk);
1074 	local_bh_enable();
1075 }
1076 
1077 static int chtls_backlog_rcv(struct sock *sk, struct sk_buff *skb)
1078 {
1079 	if (skb->protocol) {
1080 		kfree_skb(skb);
1081 		return 0;
1082 	}
1083 	BLOG_SKB_CB(skb)->backlog_rcv(sk, skb);
1084 	return 0;
1085 }
1086 
1087 static void chtls_set_tcp_window(struct chtls_sock *csk)
1088 {
1089 	struct net_device *ndev = csk->egress_dev;
1090 	struct port_info *pi = netdev_priv(ndev);
1091 	unsigned int linkspeed;
1092 	u8 scale;
1093 
1094 	linkspeed = pi->link_cfg.speed;
1095 	scale = linkspeed / SPEED_10000;
1096 #define CHTLS_10G_RCVWIN (256 * 1024)
1097 	csk->rcv_win = CHTLS_10G_RCVWIN;
1098 	if (scale)
1099 		csk->rcv_win *= scale;
1100 #define CHTLS_10G_SNDWIN (256 * 1024)
1101 	csk->snd_win = CHTLS_10G_SNDWIN;
1102 	if (scale)
1103 		csk->snd_win *= scale;
1104 }
1105 
1106 static struct sock *chtls_recv_sock(struct sock *lsk,
1107 				    struct request_sock *oreq,
1108 				    void *network_hdr,
1109 				    const struct cpl_pass_accept_req *req,
1110 				    struct chtls_dev *cdev)
1111 {
1112 	struct adapter *adap = pci_get_drvdata(cdev->pdev);
1113 	struct neighbour *n = NULL;
1114 	struct inet_sock *newinet;
1115 	const struct iphdr *iph;
1116 	struct tls_context *ctx;
1117 	struct net_device *ndev;
1118 	struct chtls_sock *csk;
1119 	struct dst_entry *dst;
1120 	struct tcp_sock *tp;
1121 	struct sock *newsk;
1122 	bool found = false;
1123 	u16 port_id;
1124 	int rxq_idx;
1125 	int step, i;
1126 
1127 	iph = (const struct iphdr *)network_hdr;
1128 	newsk = tcp_create_openreq_child(lsk, oreq, cdev->askb);
1129 	if (!newsk)
1130 		goto free_oreq;
1131 
1132 	if (lsk->sk_family == AF_INET) {
1133 		dst = inet_csk_route_child_sock(lsk, newsk, oreq);
1134 		if (!dst)
1135 			goto free_sk;
1136 
1137 		n = dst_neigh_lookup(dst, &iph->saddr);
1138 #if IS_ENABLED(CONFIG_IPV6)
1139 	} else {
1140 		const struct ipv6hdr *ip6h;
1141 		struct flowi6 fl6;
1142 
1143 		ip6h = (const struct ipv6hdr *)network_hdr;
1144 		memset(&fl6, 0, sizeof(fl6));
1145 		fl6.flowi6_proto = IPPROTO_TCP;
1146 		fl6.saddr = ip6h->daddr;
1147 		fl6.daddr = ip6h->saddr;
1148 		fl6.fl6_dport = inet_rsk(oreq)->ir_rmt_port;
1149 		fl6.fl6_sport = htons(inet_rsk(oreq)->ir_num);
1150 		security_req_classify_flow(oreq, flowi6_to_flowi_common(&fl6));
1151 		dst = ip6_dst_lookup_flow(sock_net(lsk), lsk, &fl6, NULL);
1152 		if (IS_ERR(dst))
1153 			goto free_sk;
1154 		n = dst_neigh_lookup(dst, &ip6h->saddr);
1155 #endif
1156 	}
1157 	if (!n || !n->dev)
1158 		goto free_sk;
1159 
1160 	ndev = n->dev;
1161 	if (!ndev)
1162 		goto free_dst;
1163 	if (is_vlan_dev(ndev))
1164 		ndev = vlan_dev_real_dev(ndev);
1165 
1166 	for_each_port(adap, i)
1167 		if (cdev->ports[i] == ndev)
1168 			found = true;
1169 
1170 	if (!found)
1171 		goto free_dst;
1172 
1173 	port_id = cxgb4_port_idx(ndev);
1174 
1175 	csk = chtls_sock_create(cdev);
1176 	if (!csk)
1177 		goto free_dst;
1178 
1179 	csk->l2t_entry = cxgb4_l2t_get(cdev->lldi->l2t, n, ndev, 0);
1180 	if (!csk->l2t_entry)
1181 		goto free_csk;
1182 
1183 	newsk->sk_user_data = csk;
1184 	newsk->sk_backlog_rcv = chtls_backlog_rcv;
1185 
1186 	tp = tcp_sk(newsk);
1187 	newinet = inet_sk(newsk);
1188 
1189 	if (iph->version == 0x4) {
1190 		newinet->inet_daddr = iph->saddr;
1191 		newinet->inet_rcv_saddr = iph->daddr;
1192 		newinet->inet_saddr = iph->daddr;
1193 #if IS_ENABLED(CONFIG_IPV6)
1194 	} else {
1195 		struct tcp6_sock *newtcp6sk = (struct tcp6_sock *)newsk;
1196 		struct inet_request_sock *treq = inet_rsk(oreq);
1197 		struct ipv6_pinfo *newnp = inet6_sk(newsk);
1198 		struct ipv6_pinfo *np = inet6_sk(lsk);
1199 
1200 		inet_sk(newsk)->pinet6 = &newtcp6sk->inet6;
1201 		memcpy(newnp, np, sizeof(struct ipv6_pinfo));
1202 		newsk->sk_v6_daddr = treq->ir_v6_rmt_addr;
1203 		newsk->sk_v6_rcv_saddr = treq->ir_v6_loc_addr;
1204 		inet6_sk(newsk)->saddr = treq->ir_v6_loc_addr;
1205 		newnp->ipv6_fl_list = NULL;
1206 		newnp->pktoptions = NULL;
1207 		newsk->sk_bound_dev_if = treq->ir_iif;
1208 		newinet->inet_opt = NULL;
1209 		newinet->inet_daddr = LOOPBACK4_IPV6;
1210 		newinet->inet_saddr = LOOPBACK4_IPV6;
1211 #endif
1212 	}
1213 
1214 	oreq->ts_recent = PASS_OPEN_TID_G(ntohl(req->tos_stid));
1215 	sk_setup_caps(newsk, dst);
1216 	ctx = tls_get_ctx(lsk);
1217 	newsk->sk_destruct = ctx->sk_destruct;
1218 	newsk->sk_prot_creator = lsk->sk_prot_creator;
1219 	csk->sk = newsk;
1220 	csk->passive_reap_next = oreq;
1221 	csk->tx_chan = cxgb4_port_chan(ndev);
1222 	csk->port_id = port_id;
1223 	csk->egress_dev = ndev;
1224 	csk->tos = PASS_OPEN_TOS_G(ntohl(req->tos_stid));
1225 	chtls_set_tcp_window(csk);
1226 	tp->rcv_wnd = csk->rcv_win;
1227 	csk->sndbuf = csk->snd_win;
1228 	csk->ulp_mode = ULP_MODE_TLS;
1229 	step = cdev->lldi->nrxq / cdev->lldi->nchan;
1230 	rxq_idx = port_id * step;
1231 	rxq_idx += cdev->round_robin_cnt++ % step;
1232 	csk->rss_qid = cdev->lldi->rxq_ids[rxq_idx];
1233 	csk->txq_idx = (rxq_idx < cdev->lldi->ntxq) ? rxq_idx :
1234 			port_id * step;
1235 	csk->sndbuf = newsk->sk_sndbuf;
1236 	csk->smac_idx = ((struct port_info *)netdev_priv(ndev))->smt_idx;
1237 	RCV_WSCALE(tp) = select_rcv_wscale(tcp_full_space(newsk),
1238 					   sock_net(newsk)->
1239 						ipv4.sysctl_tcp_window_scaling,
1240 					   tp->window_clamp);
1241 	neigh_release(n);
1242 	inet_inherit_port(&tcp_hashinfo, lsk, newsk);
1243 	csk_set_flag(csk, CSK_CONN_INLINE);
1244 	bh_unlock_sock(newsk); /* tcp_create_openreq_child ->sk_clone_lock */
1245 
1246 	return newsk;
1247 free_csk:
1248 	chtls_sock_release(&csk->kref);
1249 free_dst:
1250 	neigh_release(n);
1251 	dst_release(dst);
1252 free_sk:
1253 	inet_csk_prepare_forced_close(newsk);
1254 	tcp_done(newsk);
1255 free_oreq:
1256 	chtls_reqsk_free(oreq);
1257 	return NULL;
1258 }
1259 
1260 /*
1261  * Populate a TID_RELEASE WR.  The skb must be already propely sized.
1262  */
1263 static  void mk_tid_release(struct sk_buff *skb,
1264 			    unsigned int chan, unsigned int tid)
1265 {
1266 	struct cpl_tid_release *req;
1267 	unsigned int len;
1268 
1269 	len = roundup(sizeof(struct cpl_tid_release), 16);
1270 	req = (struct cpl_tid_release *)__skb_put(skb, len);
1271 	memset(req, 0, len);
1272 	set_wr_txq(skb, CPL_PRIORITY_SETUP, chan);
1273 	INIT_TP_WR_CPL(req, CPL_TID_RELEASE, tid);
1274 }
1275 
1276 static int chtls_get_module(struct sock *sk)
1277 {
1278 	struct inet_connection_sock *icsk = inet_csk(sk);
1279 
1280 	if (!try_module_get(icsk->icsk_ulp_ops->owner))
1281 		return -1;
1282 
1283 	return 0;
1284 }
1285 
1286 static void chtls_pass_accept_request(struct sock *sk,
1287 				      struct sk_buff *skb)
1288 {
1289 	struct cpl_t5_pass_accept_rpl *rpl;
1290 	struct cpl_pass_accept_req *req;
1291 	struct listen_ctx *listen_ctx;
1292 	struct vlan_ethhdr *vlan_eh;
1293 	struct request_sock *oreq;
1294 	struct sk_buff *reply_skb;
1295 	struct chtls_sock *csk;
1296 	struct chtls_dev *cdev;
1297 	struct ipv6hdr *ip6h;
1298 	struct tcphdr *tcph;
1299 	struct sock *newsk;
1300 	struct ethhdr *eh;
1301 	struct iphdr *iph;
1302 	void *network_hdr;
1303 	unsigned int stid;
1304 	unsigned int len;
1305 	unsigned int tid;
1306 	bool th_ecn, ect;
1307 	__u8 ip_dsfield; /* IPv4 tos or IPv6 dsfield */
1308 	u16 eth_hdr_len;
1309 	bool ecn_ok;
1310 
1311 	req = cplhdr(skb) + RSS_HDR;
1312 	tid = GET_TID(req);
1313 	cdev = BLOG_SKB_CB(skb)->cdev;
1314 	newsk = lookup_tid(cdev->tids, tid);
1315 	stid = PASS_OPEN_TID_G(ntohl(req->tos_stid));
1316 	if (newsk) {
1317 		pr_info("tid (%d) already in use\n", tid);
1318 		return;
1319 	}
1320 
1321 	len = roundup(sizeof(*rpl), 16);
1322 	reply_skb = alloc_skb(len, GFP_ATOMIC);
1323 	if (!reply_skb) {
1324 		cxgb4_remove_tid(cdev->tids, 0, tid, sk->sk_family);
1325 		kfree_skb(skb);
1326 		return;
1327 	}
1328 
1329 	if (sk->sk_state != TCP_LISTEN)
1330 		goto reject;
1331 
1332 	if (inet_csk_reqsk_queue_is_full(sk))
1333 		goto reject;
1334 
1335 	if (sk_acceptq_is_full(sk))
1336 		goto reject;
1337 
1338 
1339 	eth_hdr_len = T6_ETH_HDR_LEN_G(ntohl(req->hdr_len));
1340 	if (eth_hdr_len == ETH_HLEN) {
1341 		eh = (struct ethhdr *)(req + 1);
1342 		iph = (struct iphdr *)(eh + 1);
1343 		ip6h = (struct ipv6hdr *)(eh + 1);
1344 		network_hdr = (void *)(eh + 1);
1345 	} else {
1346 		vlan_eh = (struct vlan_ethhdr *)(req + 1);
1347 		iph = (struct iphdr *)(vlan_eh + 1);
1348 		ip6h = (struct ipv6hdr *)(vlan_eh + 1);
1349 		network_hdr = (void *)(vlan_eh + 1);
1350 	}
1351 
1352 	if (iph->version == 0x4) {
1353 		tcph = (struct tcphdr *)(iph + 1);
1354 		skb_set_network_header(skb, (void *)iph - (void *)req);
1355 		oreq = inet_reqsk_alloc(&chtls_rsk_ops, sk, true);
1356 	} else {
1357 		tcph = (struct tcphdr *)(ip6h + 1);
1358 		skb_set_network_header(skb, (void *)ip6h - (void *)req);
1359 		oreq = inet_reqsk_alloc(&chtls_rsk_opsv6, sk, false);
1360 	}
1361 
1362 	if (!oreq)
1363 		goto reject;
1364 
1365 	oreq->rsk_rcv_wnd = 0;
1366 	oreq->rsk_window_clamp = 0;
1367 	oreq->syncookie = 0;
1368 	oreq->mss = 0;
1369 	oreq->ts_recent = 0;
1370 
1371 	tcp_rsk(oreq)->tfo_listener = false;
1372 	tcp_rsk(oreq)->rcv_isn = ntohl(tcph->seq);
1373 	chtls_set_req_port(oreq, tcph->source, tcph->dest);
1374 	if (iph->version == 0x4) {
1375 		chtls_set_req_addr(oreq, iph->daddr, iph->saddr);
1376 		ip_dsfield = ipv4_get_dsfield(iph);
1377 #if IS_ENABLED(CONFIG_IPV6)
1378 	} else {
1379 		inet_rsk(oreq)->ir_v6_rmt_addr = ipv6_hdr(skb)->saddr;
1380 		inet_rsk(oreq)->ir_v6_loc_addr = ipv6_hdr(skb)->daddr;
1381 		ip_dsfield = ipv6_get_dsfield(ipv6_hdr(skb));
1382 #endif
1383 	}
1384 	if (req->tcpopt.wsf <= 14 &&
1385 	    sock_net(sk)->ipv4.sysctl_tcp_window_scaling) {
1386 		inet_rsk(oreq)->wscale_ok = 1;
1387 		inet_rsk(oreq)->snd_wscale = req->tcpopt.wsf;
1388 	}
1389 	inet_rsk(oreq)->ir_iif = sk->sk_bound_dev_if;
1390 	th_ecn = tcph->ece && tcph->cwr;
1391 	if (th_ecn) {
1392 		ect = !INET_ECN_is_not_ect(ip_dsfield);
1393 		ecn_ok = sock_net(sk)->ipv4.sysctl_tcp_ecn;
1394 		if ((!ect && ecn_ok) || tcp_ca_needs_ecn(sk))
1395 			inet_rsk(oreq)->ecn_ok = 1;
1396 	}
1397 
1398 	newsk = chtls_recv_sock(sk, oreq, network_hdr, req, cdev);
1399 	if (!newsk)
1400 		goto reject;
1401 
1402 	if (chtls_get_module(newsk))
1403 		goto reject;
1404 	inet_csk_reqsk_queue_added(sk);
1405 	reply_skb->sk = newsk;
1406 	chtls_install_cpl_ops(newsk);
1407 	cxgb4_insert_tid(cdev->tids, newsk, tid, newsk->sk_family);
1408 	csk = rcu_dereference_sk_user_data(newsk);
1409 	listen_ctx = (struct listen_ctx *)lookup_stid(cdev->tids, stid);
1410 	csk->listen_ctx = listen_ctx;
1411 	__skb_queue_tail(&listen_ctx->synq, (struct sk_buff *)&csk->synq);
1412 	chtls_pass_accept_rpl(reply_skb, req, tid);
1413 	kfree_skb(skb);
1414 	return;
1415 
1416 reject:
1417 	mk_tid_release(reply_skb, 0, tid);
1418 	cxgb4_ofld_send(cdev->lldi->ports[0], reply_skb);
1419 	kfree_skb(skb);
1420 }
1421 
1422 /*
1423  * Handle a CPL_PASS_ACCEPT_REQ message.
1424  */
1425 static int chtls_pass_accept_req(struct chtls_dev *cdev, struct sk_buff *skb)
1426 {
1427 	struct cpl_pass_accept_req *req = cplhdr(skb) + RSS_HDR;
1428 	struct listen_ctx *ctx;
1429 	unsigned int stid;
1430 	unsigned int tid;
1431 	struct sock *lsk;
1432 	void *data;
1433 
1434 	stid = PASS_OPEN_TID_G(ntohl(req->tos_stid));
1435 	tid = GET_TID(req);
1436 
1437 	data = lookup_stid(cdev->tids, stid);
1438 	if (!data)
1439 		return 1;
1440 
1441 	ctx = (struct listen_ctx *)data;
1442 	lsk = ctx->lsk;
1443 
1444 	if (unlikely(tid_out_of_range(cdev->tids, tid))) {
1445 		pr_info("passive open TID %u too large\n", tid);
1446 		return 1;
1447 	}
1448 
1449 	BLOG_SKB_CB(skb)->cdev = cdev;
1450 	process_cpl_msg(chtls_pass_accept_request, lsk, skb);
1451 	return 0;
1452 }
1453 
1454 /*
1455  * Completes some final bits of initialization for just established connections
1456  * and changes their state to TCP_ESTABLISHED.
1457  *
1458  * snd_isn here is the ISN after the SYN, i.e., the true ISN + 1.
1459  */
1460 static void make_established(struct sock *sk, u32 snd_isn, unsigned int opt)
1461 {
1462 	struct tcp_sock *tp = tcp_sk(sk);
1463 
1464 	tp->pushed_seq = snd_isn;
1465 	tp->write_seq = snd_isn;
1466 	tp->snd_nxt = snd_isn;
1467 	tp->snd_una = snd_isn;
1468 	inet_sk(sk)->inet_id = prandom_u32();
1469 	assign_rxopt(sk, opt);
1470 
1471 	if (tp->rcv_wnd > (RCV_BUFSIZ_M << 10))
1472 		tp->rcv_wup -= tp->rcv_wnd - (RCV_BUFSIZ_M << 10);
1473 
1474 	smp_mb();
1475 	tcp_set_state(sk, TCP_ESTABLISHED);
1476 }
1477 
1478 static void chtls_abort_conn(struct sock *sk, struct sk_buff *skb)
1479 {
1480 	struct sk_buff *abort_skb;
1481 
1482 	abort_skb = alloc_skb(sizeof(struct cpl_abort_req), GFP_ATOMIC);
1483 	if (abort_skb)
1484 		chtls_send_reset(sk, CPL_ABORT_SEND_RST, abort_skb);
1485 }
1486 
1487 static struct sock *reap_list;
1488 static DEFINE_SPINLOCK(reap_list_lock);
1489 
1490 /*
1491  * Process the reap list.
1492  */
1493 DECLARE_TASK_FUNC(process_reap_list, task_param)
1494 {
1495 	spin_lock_bh(&reap_list_lock);
1496 	while (reap_list) {
1497 		struct sock *sk = reap_list;
1498 		struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
1499 
1500 		reap_list = csk->passive_reap_next;
1501 		csk->passive_reap_next = NULL;
1502 		spin_unlock(&reap_list_lock);
1503 		sock_hold(sk);
1504 
1505 		bh_lock_sock(sk);
1506 		chtls_abort_conn(sk, NULL);
1507 		sock_orphan(sk);
1508 		if (sk->sk_state == TCP_CLOSE)
1509 			inet_csk_destroy_sock(sk);
1510 		bh_unlock_sock(sk);
1511 		sock_put(sk);
1512 		spin_lock(&reap_list_lock);
1513 	}
1514 	spin_unlock_bh(&reap_list_lock);
1515 }
1516 
1517 static DECLARE_WORK(reap_task, process_reap_list);
1518 
1519 static void add_to_reap_list(struct sock *sk)
1520 {
1521 	struct chtls_sock *csk = sk->sk_user_data;
1522 
1523 	local_bh_disable();
1524 	release_tcp_port(sk); /* release the port immediately */
1525 
1526 	spin_lock(&reap_list_lock);
1527 	csk->passive_reap_next = reap_list;
1528 	reap_list = sk;
1529 	if (!csk->passive_reap_next)
1530 		schedule_work(&reap_task);
1531 	spin_unlock(&reap_list_lock);
1532 	local_bh_enable();
1533 }
1534 
1535 static void add_pass_open_to_parent(struct sock *child, struct sock *lsk,
1536 				    struct chtls_dev *cdev)
1537 {
1538 	struct request_sock *oreq;
1539 	struct chtls_sock *csk;
1540 
1541 	if (lsk->sk_state != TCP_LISTEN)
1542 		return;
1543 
1544 	csk = child->sk_user_data;
1545 	oreq = csk->passive_reap_next;
1546 	csk->passive_reap_next = NULL;
1547 
1548 	reqsk_queue_removed(&inet_csk(lsk)->icsk_accept_queue, oreq);
1549 	__skb_unlink((struct sk_buff *)&csk->synq, &csk->listen_ctx->synq);
1550 
1551 	if (sk_acceptq_is_full(lsk)) {
1552 		chtls_reqsk_free(oreq);
1553 		add_to_reap_list(child);
1554 	} else {
1555 		refcount_set(&oreq->rsk_refcnt, 1);
1556 		inet_csk_reqsk_queue_add(lsk, oreq, child);
1557 		lsk->sk_data_ready(lsk);
1558 	}
1559 }
1560 
1561 static void bl_add_pass_open_to_parent(struct sock *lsk, struct sk_buff *skb)
1562 {
1563 	struct sock *child = skb->sk;
1564 
1565 	skb->sk = NULL;
1566 	add_pass_open_to_parent(child, lsk, BLOG_SKB_CB(skb)->cdev);
1567 	kfree_skb(skb);
1568 }
1569 
1570 static int chtls_pass_establish(struct chtls_dev *cdev, struct sk_buff *skb)
1571 {
1572 	struct cpl_pass_establish *req = cplhdr(skb) + RSS_HDR;
1573 	struct chtls_sock *csk;
1574 	struct sock *lsk, *sk;
1575 	unsigned int hwtid;
1576 
1577 	hwtid = GET_TID(req);
1578 	sk = lookup_tid(cdev->tids, hwtid);
1579 	if (!sk)
1580 		return (CPL_RET_UNKNOWN_TID | CPL_RET_BUF_DONE);
1581 
1582 	bh_lock_sock(sk);
1583 	if (unlikely(sock_owned_by_user(sk))) {
1584 		kfree_skb(skb);
1585 	} else {
1586 		unsigned int stid;
1587 		void *data;
1588 
1589 		csk = sk->sk_user_data;
1590 		csk->wr_max_credits = 64;
1591 		csk->wr_credits = 64;
1592 		csk->wr_unacked = 0;
1593 		make_established(sk, ntohl(req->snd_isn), ntohs(req->tcp_opt));
1594 		stid = PASS_OPEN_TID_G(ntohl(req->tos_stid));
1595 		sk->sk_state_change(sk);
1596 		if (unlikely(sk->sk_socket))
1597 			sk_wake_async(sk, 0, POLL_OUT);
1598 
1599 		data = lookup_stid(cdev->tids, stid);
1600 		if (!data) {
1601 			/* listening server close */
1602 			kfree_skb(skb);
1603 			goto unlock;
1604 		}
1605 		lsk = ((struct listen_ctx *)data)->lsk;
1606 
1607 		bh_lock_sock(lsk);
1608 		if (unlikely(skb_queue_empty(&csk->listen_ctx->synq))) {
1609 			/* removed from synq */
1610 			bh_unlock_sock(lsk);
1611 			kfree_skb(skb);
1612 			goto unlock;
1613 		}
1614 
1615 		if (likely(!sock_owned_by_user(lsk))) {
1616 			kfree_skb(skb);
1617 			add_pass_open_to_parent(sk, lsk, cdev);
1618 		} else {
1619 			skb->sk = sk;
1620 			BLOG_SKB_CB(skb)->cdev = cdev;
1621 			BLOG_SKB_CB(skb)->backlog_rcv =
1622 				bl_add_pass_open_to_parent;
1623 			__sk_add_backlog(lsk, skb);
1624 		}
1625 		bh_unlock_sock(lsk);
1626 	}
1627 unlock:
1628 	bh_unlock_sock(sk);
1629 	return 0;
1630 }
1631 
1632 /*
1633  * Handle receipt of an urgent pointer.
1634  */
1635 static void handle_urg_ptr(struct sock *sk, u32 urg_seq)
1636 {
1637 	struct tcp_sock *tp = tcp_sk(sk);
1638 
1639 	urg_seq--;
1640 	if (tp->urg_data && !after(urg_seq, tp->urg_seq))
1641 		return;	/* duplicate pointer */
1642 
1643 	sk_send_sigurg(sk);
1644 	if (tp->urg_seq == tp->copied_seq && tp->urg_data &&
1645 	    !sock_flag(sk, SOCK_URGINLINE) &&
1646 	    tp->copied_seq != tp->rcv_nxt) {
1647 		struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
1648 
1649 		tp->copied_seq++;
1650 		if (skb && tp->copied_seq - ULP_SKB_CB(skb)->seq >= skb->len)
1651 			chtls_free_skb(sk, skb);
1652 	}
1653 
1654 	tp->urg_data = TCP_URG_NOTYET;
1655 	tp->urg_seq = urg_seq;
1656 }
1657 
1658 static void check_sk_callbacks(struct chtls_sock *csk)
1659 {
1660 	struct sock *sk = csk->sk;
1661 
1662 	if (unlikely(sk->sk_user_data &&
1663 		     !csk_flag_nochk(csk, CSK_CALLBACKS_CHKD)))
1664 		csk_set_flag(csk, CSK_CALLBACKS_CHKD);
1665 }
1666 
1667 /*
1668  * Handles Rx data that arrives in a state where the socket isn't accepting
1669  * new data.
1670  */
1671 static void handle_excess_rx(struct sock *sk, struct sk_buff *skb)
1672 {
1673 	if (!csk_flag(sk, CSK_ABORT_SHUTDOWN))
1674 		chtls_abort_conn(sk, skb);
1675 
1676 	kfree_skb(skb);
1677 }
1678 
1679 static void chtls_recv_data(struct sock *sk, struct sk_buff *skb)
1680 {
1681 	struct cpl_rx_data *hdr = cplhdr(skb) + RSS_HDR;
1682 	struct chtls_sock *csk;
1683 	struct tcp_sock *tp;
1684 
1685 	csk = rcu_dereference_sk_user_data(sk);
1686 	tp = tcp_sk(sk);
1687 
1688 	if (unlikely(sk->sk_shutdown & RCV_SHUTDOWN)) {
1689 		handle_excess_rx(sk, skb);
1690 		return;
1691 	}
1692 
1693 	ULP_SKB_CB(skb)->seq = ntohl(hdr->seq);
1694 	ULP_SKB_CB(skb)->psh = hdr->psh;
1695 	skb_ulp_mode(skb) = ULP_MODE_NONE;
1696 
1697 	skb_reset_transport_header(skb);
1698 	__skb_pull(skb, sizeof(*hdr) + RSS_HDR);
1699 	if (!skb->data_len)
1700 		__skb_trim(skb, ntohs(hdr->len));
1701 
1702 	if (unlikely(hdr->urg))
1703 		handle_urg_ptr(sk, tp->rcv_nxt + ntohs(hdr->urg));
1704 	if (unlikely(tp->urg_data == TCP_URG_NOTYET &&
1705 		     tp->urg_seq - tp->rcv_nxt < skb->len))
1706 		tp->urg_data = TCP_URG_VALID |
1707 			       skb->data[tp->urg_seq - tp->rcv_nxt];
1708 
1709 	if (unlikely(hdr->dack_mode != csk->delack_mode)) {
1710 		csk->delack_mode = hdr->dack_mode;
1711 		csk->delack_seq = tp->rcv_nxt;
1712 	}
1713 
1714 	tcp_hdr(skb)->fin = 0;
1715 	tp->rcv_nxt += skb->len;
1716 
1717 	__skb_queue_tail(&sk->sk_receive_queue, skb);
1718 
1719 	if (!sock_flag(sk, SOCK_DEAD)) {
1720 		check_sk_callbacks(csk);
1721 		sk->sk_data_ready(sk);
1722 	}
1723 }
1724 
1725 static int chtls_rx_data(struct chtls_dev *cdev, struct sk_buff *skb)
1726 {
1727 	struct cpl_rx_data *req = cplhdr(skb) + RSS_HDR;
1728 	unsigned int hwtid = GET_TID(req);
1729 	struct sock *sk;
1730 
1731 	sk = lookup_tid(cdev->tids, hwtid);
1732 	if (unlikely(!sk)) {
1733 		pr_err("can't find conn. for hwtid %u.\n", hwtid);
1734 		return -EINVAL;
1735 	}
1736 	skb_dst_set(skb, NULL);
1737 	process_cpl_msg(chtls_recv_data, sk, skb);
1738 	return 0;
1739 }
1740 
1741 static void chtls_recv_pdu(struct sock *sk, struct sk_buff *skb)
1742 {
1743 	struct cpl_tls_data *hdr = cplhdr(skb);
1744 	struct chtls_sock *csk;
1745 	struct chtls_hws *tlsk;
1746 	struct tcp_sock *tp;
1747 
1748 	csk = rcu_dereference_sk_user_data(sk);
1749 	tlsk = &csk->tlshws;
1750 	tp = tcp_sk(sk);
1751 
1752 	if (unlikely(sk->sk_shutdown & RCV_SHUTDOWN)) {
1753 		handle_excess_rx(sk, skb);
1754 		return;
1755 	}
1756 
1757 	ULP_SKB_CB(skb)->seq = ntohl(hdr->seq);
1758 	ULP_SKB_CB(skb)->flags = 0;
1759 	skb_ulp_mode(skb) = ULP_MODE_TLS;
1760 
1761 	skb_reset_transport_header(skb);
1762 	__skb_pull(skb, sizeof(*hdr));
1763 	if (!skb->data_len)
1764 		__skb_trim(skb,
1765 			   CPL_TLS_DATA_LENGTH_G(ntohl(hdr->length_pkd)));
1766 
1767 	if (unlikely(tp->urg_data == TCP_URG_NOTYET && tp->urg_seq -
1768 		     tp->rcv_nxt < skb->len))
1769 		tp->urg_data = TCP_URG_VALID |
1770 			       skb->data[tp->urg_seq - tp->rcv_nxt];
1771 
1772 	tcp_hdr(skb)->fin = 0;
1773 	tlsk->pldlen = CPL_TLS_DATA_LENGTH_G(ntohl(hdr->length_pkd));
1774 	__skb_queue_tail(&tlsk->sk_recv_queue, skb);
1775 }
1776 
1777 static int chtls_rx_pdu(struct chtls_dev *cdev, struct sk_buff *skb)
1778 {
1779 	struct cpl_tls_data *req = cplhdr(skb);
1780 	unsigned int hwtid = GET_TID(req);
1781 	struct sock *sk;
1782 
1783 	sk = lookup_tid(cdev->tids, hwtid);
1784 	if (unlikely(!sk)) {
1785 		pr_err("can't find conn. for hwtid %u.\n", hwtid);
1786 		return -EINVAL;
1787 	}
1788 	skb_dst_set(skb, NULL);
1789 	process_cpl_msg(chtls_recv_pdu, sk, skb);
1790 	return 0;
1791 }
1792 
1793 static void chtls_set_hdrlen(struct sk_buff *skb, unsigned int nlen)
1794 {
1795 	struct tlsrx_cmp_hdr *tls_cmp_hdr = cplhdr(skb);
1796 
1797 	skb->hdr_len = ntohs((__force __be16)tls_cmp_hdr->length);
1798 	tls_cmp_hdr->length = ntohs((__force __be16)nlen);
1799 }
1800 
1801 static void chtls_rx_hdr(struct sock *sk, struct sk_buff *skb)
1802 {
1803 	struct tlsrx_cmp_hdr *tls_hdr_pkt;
1804 	struct cpl_rx_tls_cmp *cmp_cpl;
1805 	struct sk_buff *skb_rec;
1806 	struct chtls_sock *csk;
1807 	struct chtls_hws *tlsk;
1808 	struct tcp_sock *tp;
1809 
1810 	cmp_cpl = cplhdr(skb);
1811 	csk = rcu_dereference_sk_user_data(sk);
1812 	tlsk = &csk->tlshws;
1813 	tp = tcp_sk(sk);
1814 
1815 	ULP_SKB_CB(skb)->seq = ntohl(cmp_cpl->seq);
1816 	ULP_SKB_CB(skb)->flags = 0;
1817 
1818 	skb_reset_transport_header(skb);
1819 	__skb_pull(skb, sizeof(*cmp_cpl));
1820 	tls_hdr_pkt = (struct tlsrx_cmp_hdr *)skb->data;
1821 	if (tls_hdr_pkt->res_to_mac_error & TLSRX_HDR_PKT_ERROR_M)
1822 		tls_hdr_pkt->type = CONTENT_TYPE_ERROR;
1823 	if (!skb->data_len)
1824 		__skb_trim(skb, TLS_HEADER_LENGTH);
1825 
1826 	tp->rcv_nxt +=
1827 		CPL_RX_TLS_CMP_PDULENGTH_G(ntohl(cmp_cpl->pdulength_length));
1828 
1829 	ULP_SKB_CB(skb)->flags |= ULPCB_FLAG_TLS_HDR;
1830 	skb_rec = __skb_dequeue(&tlsk->sk_recv_queue);
1831 	if (!skb_rec) {
1832 		__skb_queue_tail(&sk->sk_receive_queue, skb);
1833 	} else {
1834 		chtls_set_hdrlen(skb, tlsk->pldlen);
1835 		tlsk->pldlen = 0;
1836 		__skb_queue_tail(&sk->sk_receive_queue, skb);
1837 		__skb_queue_tail(&sk->sk_receive_queue, skb_rec);
1838 	}
1839 
1840 	if (!sock_flag(sk, SOCK_DEAD)) {
1841 		check_sk_callbacks(csk);
1842 		sk->sk_data_ready(sk);
1843 	}
1844 }
1845 
1846 static int chtls_rx_cmp(struct chtls_dev *cdev, struct sk_buff *skb)
1847 {
1848 	struct cpl_rx_tls_cmp *req = cplhdr(skb);
1849 	unsigned int hwtid = GET_TID(req);
1850 	struct sock *sk;
1851 
1852 	sk = lookup_tid(cdev->tids, hwtid);
1853 	if (unlikely(!sk)) {
1854 		pr_err("can't find conn. for hwtid %u.\n", hwtid);
1855 		return -EINVAL;
1856 	}
1857 	skb_dst_set(skb, NULL);
1858 	process_cpl_msg(chtls_rx_hdr, sk, skb);
1859 
1860 	return 0;
1861 }
1862 
1863 static void chtls_timewait(struct sock *sk)
1864 {
1865 	struct tcp_sock *tp = tcp_sk(sk);
1866 
1867 	tp->rcv_nxt++;
1868 	tp->rx_opt.ts_recent_stamp = ktime_get_seconds();
1869 	tp->srtt_us = 0;
1870 	tcp_time_wait(sk, TCP_TIME_WAIT, 0);
1871 }
1872 
1873 static void chtls_peer_close(struct sock *sk, struct sk_buff *skb)
1874 {
1875 	struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
1876 
1877 	if (csk_flag_nochk(csk, CSK_ABORT_RPL_PENDING))
1878 		goto out;
1879 
1880 	sk->sk_shutdown |= RCV_SHUTDOWN;
1881 	sock_set_flag(sk, SOCK_DONE);
1882 
1883 	switch (sk->sk_state) {
1884 	case TCP_SYN_RECV:
1885 	case TCP_ESTABLISHED:
1886 		tcp_set_state(sk, TCP_CLOSE_WAIT);
1887 		break;
1888 	case TCP_FIN_WAIT1:
1889 		tcp_set_state(sk, TCP_CLOSING);
1890 		break;
1891 	case TCP_FIN_WAIT2:
1892 		chtls_release_resources(sk);
1893 		if (csk_flag_nochk(csk, CSK_ABORT_RPL_PENDING))
1894 			chtls_conn_done(sk);
1895 		else
1896 			chtls_timewait(sk);
1897 		break;
1898 	default:
1899 		pr_info("cpl_peer_close in bad state %d\n", sk->sk_state);
1900 	}
1901 
1902 	if (!sock_flag(sk, SOCK_DEAD)) {
1903 		sk->sk_state_change(sk);
1904 		/* Do not send POLL_HUP for half duplex close. */
1905 
1906 		if ((sk->sk_shutdown & SEND_SHUTDOWN) ||
1907 		    sk->sk_state == TCP_CLOSE)
1908 			sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
1909 		else
1910 			sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
1911 	}
1912 out:
1913 	kfree_skb(skb);
1914 }
1915 
1916 static void chtls_close_con_rpl(struct sock *sk, struct sk_buff *skb)
1917 {
1918 	struct cpl_close_con_rpl *rpl = cplhdr(skb) + RSS_HDR;
1919 	struct chtls_sock *csk;
1920 	struct tcp_sock *tp;
1921 
1922 	csk = rcu_dereference_sk_user_data(sk);
1923 
1924 	if (csk_flag_nochk(csk, CSK_ABORT_RPL_PENDING))
1925 		goto out;
1926 
1927 	tp = tcp_sk(sk);
1928 
1929 	tp->snd_una = ntohl(rpl->snd_nxt) - 1;  /* exclude FIN */
1930 
1931 	switch (sk->sk_state) {
1932 	case TCP_CLOSING:
1933 		chtls_release_resources(sk);
1934 		if (csk_flag_nochk(csk, CSK_ABORT_RPL_PENDING))
1935 			chtls_conn_done(sk);
1936 		else
1937 			chtls_timewait(sk);
1938 		break;
1939 	case TCP_LAST_ACK:
1940 		chtls_release_resources(sk);
1941 		chtls_conn_done(sk);
1942 		break;
1943 	case TCP_FIN_WAIT1:
1944 		tcp_set_state(sk, TCP_FIN_WAIT2);
1945 		sk->sk_shutdown |= SEND_SHUTDOWN;
1946 
1947 		if (!sock_flag(sk, SOCK_DEAD))
1948 			sk->sk_state_change(sk);
1949 		else if (tcp_sk(sk)->linger2 < 0 &&
1950 			 !csk_flag_nochk(csk, CSK_ABORT_SHUTDOWN))
1951 			chtls_abort_conn(sk, skb);
1952 		break;
1953 	default:
1954 		pr_info("close_con_rpl in bad state %d\n", sk->sk_state);
1955 	}
1956 out:
1957 	kfree_skb(skb);
1958 }
1959 
1960 static struct sk_buff *get_cpl_skb(struct sk_buff *skb,
1961 				   size_t len, gfp_t gfp)
1962 {
1963 	if (likely(!skb_is_nonlinear(skb) && !skb_cloned(skb))) {
1964 		WARN_ONCE(skb->len < len, "skb alloc error");
1965 		__skb_trim(skb, len);
1966 		skb_get(skb);
1967 	} else {
1968 		skb = alloc_skb(len, gfp);
1969 		if (skb)
1970 			__skb_put(skb, len);
1971 	}
1972 	return skb;
1973 }
1974 
1975 static void set_abort_rpl_wr(struct sk_buff *skb, unsigned int tid,
1976 			     int cmd)
1977 {
1978 	struct cpl_abort_rpl *rpl = cplhdr(skb);
1979 
1980 	INIT_TP_WR_CPL(rpl, CPL_ABORT_RPL, tid);
1981 	rpl->cmd = cmd;
1982 }
1983 
1984 static void send_defer_abort_rpl(struct chtls_dev *cdev, struct sk_buff *skb)
1985 {
1986 	struct cpl_abort_req_rss *req = cplhdr(skb);
1987 	struct sk_buff *reply_skb;
1988 
1989 	reply_skb = alloc_skb(sizeof(struct cpl_abort_rpl),
1990 			      GFP_KERNEL | __GFP_NOFAIL);
1991 	__skb_put(reply_skb, sizeof(struct cpl_abort_rpl));
1992 	set_abort_rpl_wr(reply_skb, GET_TID(req),
1993 			 (req->status & CPL_ABORT_NO_RST));
1994 	set_wr_txq(reply_skb, CPL_PRIORITY_DATA, req->status >> 1);
1995 	cxgb4_ofld_send(cdev->lldi->ports[0], reply_skb);
1996 	kfree_skb(skb);
1997 }
1998 
1999 /*
2000  * Add an skb to the deferred skb queue for processing from process context.
2001  */
2002 static void t4_defer_reply(struct sk_buff *skb, struct chtls_dev *cdev,
2003 			   defer_handler_t handler)
2004 {
2005 	DEFERRED_SKB_CB(skb)->handler = handler;
2006 	spin_lock_bh(&cdev->deferq.lock);
2007 	__skb_queue_tail(&cdev->deferq, skb);
2008 	if (skb_queue_len(&cdev->deferq) == 1)
2009 		schedule_work(&cdev->deferq_task);
2010 	spin_unlock_bh(&cdev->deferq.lock);
2011 }
2012 
2013 static void chtls_send_abort_rpl(struct sock *sk, struct sk_buff *skb,
2014 				 struct chtls_dev *cdev,
2015 				 int status, int queue)
2016 {
2017 	struct cpl_abort_req_rss *req = cplhdr(skb) + RSS_HDR;
2018 	struct sk_buff *reply_skb;
2019 	struct chtls_sock *csk;
2020 	unsigned int tid;
2021 
2022 	csk = rcu_dereference_sk_user_data(sk);
2023 	tid = GET_TID(req);
2024 
2025 	reply_skb = get_cpl_skb(skb, sizeof(struct cpl_abort_rpl), gfp_any());
2026 	if (!reply_skb) {
2027 		req->status = (queue << 1) | status;
2028 		t4_defer_reply(skb, cdev, send_defer_abort_rpl);
2029 		return;
2030 	}
2031 
2032 	set_abort_rpl_wr(reply_skb, tid, status);
2033 	kfree_skb(skb);
2034 	set_wr_txq(reply_skb, CPL_PRIORITY_DATA, queue);
2035 	if (csk_conn_inline(csk)) {
2036 		struct l2t_entry *e = csk->l2t_entry;
2037 
2038 		if (e && sk->sk_state != TCP_SYN_RECV) {
2039 			cxgb4_l2t_send(csk->egress_dev, reply_skb, e);
2040 			return;
2041 		}
2042 	}
2043 	cxgb4_ofld_send(cdev->lldi->ports[0], reply_skb);
2044 }
2045 
2046 /*
2047  * This is run from a listener's backlog to abort a child connection in
2048  * SYN_RCV state (i.e., one on the listener's SYN queue).
2049  */
2050 static void bl_abort_syn_rcv(struct sock *lsk, struct sk_buff *skb)
2051 {
2052 	struct chtls_sock *csk;
2053 	struct sock *child;
2054 	int queue;
2055 
2056 	child = skb->sk;
2057 	csk = rcu_dereference_sk_user_data(child);
2058 	queue = csk->txq_idx;
2059 
2060 	skb->sk	= NULL;
2061 	chtls_send_abort_rpl(child, skb, BLOG_SKB_CB(skb)->cdev,
2062 			     CPL_ABORT_NO_RST, queue);
2063 	do_abort_syn_rcv(child, lsk);
2064 }
2065 
2066 static int abort_syn_rcv(struct sock *sk, struct sk_buff *skb)
2067 {
2068 	const struct request_sock *oreq;
2069 	struct listen_ctx *listen_ctx;
2070 	struct chtls_sock *csk;
2071 	struct chtls_dev *cdev;
2072 	struct sock *psk;
2073 	void *ctx;
2074 
2075 	csk = sk->sk_user_data;
2076 	oreq = csk->passive_reap_next;
2077 	cdev = csk->cdev;
2078 
2079 	if (!oreq)
2080 		return -1;
2081 
2082 	ctx = lookup_stid(cdev->tids, oreq->ts_recent);
2083 	if (!ctx)
2084 		return -1;
2085 
2086 	listen_ctx = (struct listen_ctx *)ctx;
2087 	psk = listen_ctx->lsk;
2088 
2089 	bh_lock_sock(psk);
2090 	if (!sock_owned_by_user(psk)) {
2091 		int queue = csk->txq_idx;
2092 
2093 		chtls_send_abort_rpl(sk, skb, cdev, CPL_ABORT_NO_RST, queue);
2094 		do_abort_syn_rcv(sk, psk);
2095 	} else {
2096 		skb->sk = sk;
2097 		BLOG_SKB_CB(skb)->backlog_rcv = bl_abort_syn_rcv;
2098 		__sk_add_backlog(psk, skb);
2099 	}
2100 	bh_unlock_sock(psk);
2101 	return 0;
2102 }
2103 
2104 static void chtls_abort_req_rss(struct sock *sk, struct sk_buff *skb)
2105 {
2106 	const struct cpl_abort_req_rss *req = cplhdr(skb) + RSS_HDR;
2107 	struct chtls_sock *csk = sk->sk_user_data;
2108 	int rst_status = CPL_ABORT_NO_RST;
2109 	int queue = csk->txq_idx;
2110 
2111 	if (is_neg_adv(req->status)) {
2112 		kfree_skb(skb);
2113 		return;
2114 	}
2115 
2116 	csk_reset_flag(csk, CSK_ABORT_REQ_RCVD);
2117 
2118 	if (!csk_flag_nochk(csk, CSK_ABORT_SHUTDOWN) &&
2119 	    !csk_flag_nochk(csk, CSK_TX_DATA_SENT)) {
2120 		struct tcp_sock *tp = tcp_sk(sk);
2121 
2122 		if (send_tx_flowc_wr(sk, 0, tp->snd_nxt, tp->rcv_nxt) < 0)
2123 			WARN_ONCE(1, "send_tx_flowc error");
2124 		csk_set_flag(csk, CSK_TX_DATA_SENT);
2125 	}
2126 
2127 	csk_set_flag(csk, CSK_ABORT_SHUTDOWN);
2128 
2129 	if (!csk_flag_nochk(csk, CSK_ABORT_RPL_PENDING)) {
2130 		sk->sk_err = ETIMEDOUT;
2131 
2132 		if (!sock_flag(sk, SOCK_DEAD))
2133 			sk->sk_error_report(sk);
2134 
2135 		if (sk->sk_state == TCP_SYN_RECV && !abort_syn_rcv(sk, skb))
2136 			return;
2137 
2138 	}
2139 
2140 	chtls_send_abort_rpl(sk, skb, BLOG_SKB_CB(skb)->cdev,
2141 			     rst_status, queue);
2142 	chtls_release_resources(sk);
2143 	chtls_conn_done(sk);
2144 }
2145 
2146 static void chtls_abort_rpl_rss(struct sock *sk, struct sk_buff *skb)
2147 {
2148 	struct cpl_abort_rpl_rss *rpl = cplhdr(skb) + RSS_HDR;
2149 	struct chtls_sock *csk;
2150 	struct chtls_dev *cdev;
2151 
2152 	csk = rcu_dereference_sk_user_data(sk);
2153 	cdev = csk->cdev;
2154 
2155 	if (csk_flag_nochk(csk, CSK_ABORT_RPL_PENDING)) {
2156 		csk_reset_flag(csk, CSK_ABORT_RPL_PENDING);
2157 		if (!csk_flag_nochk(csk, CSK_ABORT_REQ_RCVD)) {
2158 			if (sk->sk_state == TCP_SYN_SENT) {
2159 				cxgb4_remove_tid(cdev->tids,
2160 						 csk->port_id,
2161 						 GET_TID(rpl),
2162 						 sk->sk_family);
2163 				sock_put(sk);
2164 			}
2165 			chtls_release_resources(sk);
2166 			chtls_conn_done(sk);
2167 		}
2168 	}
2169 	kfree_skb(skb);
2170 }
2171 
2172 static int chtls_conn_cpl(struct chtls_dev *cdev, struct sk_buff *skb)
2173 {
2174 	struct cpl_peer_close *req = cplhdr(skb) + RSS_HDR;
2175 	void (*fn)(struct sock *sk, struct sk_buff *skb);
2176 	unsigned int hwtid = GET_TID(req);
2177 	struct chtls_sock *csk;
2178 	struct sock *sk;
2179 	u8 opcode;
2180 
2181 	opcode = ((const struct rss_header *)cplhdr(skb))->opcode;
2182 
2183 	sk = lookup_tid(cdev->tids, hwtid);
2184 	if (!sk)
2185 		goto rel_skb;
2186 
2187 	csk = sk->sk_user_data;
2188 
2189 	switch (opcode) {
2190 	case CPL_PEER_CLOSE:
2191 		fn = chtls_peer_close;
2192 		break;
2193 	case CPL_CLOSE_CON_RPL:
2194 		fn = chtls_close_con_rpl;
2195 		break;
2196 	case CPL_ABORT_REQ_RSS:
2197 		/*
2198 		 * Save the offload device in the skb, we may process this
2199 		 * message after the socket has closed.
2200 		 */
2201 		BLOG_SKB_CB(skb)->cdev = csk->cdev;
2202 		fn = chtls_abort_req_rss;
2203 		break;
2204 	case CPL_ABORT_RPL_RSS:
2205 		fn = chtls_abort_rpl_rss;
2206 		break;
2207 	default:
2208 		goto rel_skb;
2209 	}
2210 
2211 	process_cpl_msg(fn, sk, skb);
2212 	return 0;
2213 
2214 rel_skb:
2215 	kfree_skb(skb);
2216 	return 0;
2217 }
2218 
2219 static void chtls_rx_ack(struct sock *sk, struct sk_buff *skb)
2220 {
2221 	struct cpl_fw4_ack *hdr = cplhdr(skb) + RSS_HDR;
2222 	struct chtls_sock *csk = sk->sk_user_data;
2223 	struct tcp_sock *tp = tcp_sk(sk);
2224 	u32 credits = hdr->credits;
2225 	u32 snd_una;
2226 
2227 	snd_una = ntohl(hdr->snd_una);
2228 	csk->wr_credits += credits;
2229 
2230 	if (csk->wr_unacked > csk->wr_max_credits - csk->wr_credits)
2231 		csk->wr_unacked = csk->wr_max_credits - csk->wr_credits;
2232 
2233 	while (credits) {
2234 		struct sk_buff *pskb = csk->wr_skb_head;
2235 		u32 csum;
2236 
2237 		if (unlikely(!pskb)) {
2238 			if (csk->wr_nondata)
2239 				csk->wr_nondata -= credits;
2240 			break;
2241 		}
2242 		csum = (__force u32)pskb->csum;
2243 		if (unlikely(credits < csum)) {
2244 			pskb->csum = (__force __wsum)(csum - credits);
2245 			break;
2246 		}
2247 		dequeue_wr(sk);
2248 		credits -= csum;
2249 		kfree_skb(pskb);
2250 	}
2251 	if (hdr->seq_vld & CPL_FW4_ACK_FLAGS_SEQVAL) {
2252 		if (unlikely(before(snd_una, tp->snd_una))) {
2253 			kfree_skb(skb);
2254 			return;
2255 		}
2256 
2257 		if (tp->snd_una != snd_una) {
2258 			tp->snd_una = snd_una;
2259 			tp->rcv_tstamp = tcp_time_stamp(tp);
2260 			if (tp->snd_una == tp->snd_nxt &&
2261 			    !csk_flag_nochk(csk, CSK_TX_FAILOVER))
2262 				csk_reset_flag(csk, CSK_TX_WAIT_IDLE);
2263 		}
2264 	}
2265 
2266 	if (hdr->seq_vld & CPL_FW4_ACK_FLAGS_CH) {
2267 		unsigned int fclen16 = roundup(failover_flowc_wr_len, 16);
2268 
2269 		csk->wr_credits -= fclen16;
2270 		csk_reset_flag(csk, CSK_TX_WAIT_IDLE);
2271 		csk_reset_flag(csk, CSK_TX_FAILOVER);
2272 	}
2273 	if (skb_queue_len(&csk->txq) && chtls_push_frames(csk, 0))
2274 		sk->sk_write_space(sk);
2275 
2276 	kfree_skb(skb);
2277 }
2278 
2279 static int chtls_wr_ack(struct chtls_dev *cdev, struct sk_buff *skb)
2280 {
2281 	struct cpl_fw4_ack *rpl = cplhdr(skb) + RSS_HDR;
2282 	unsigned int hwtid = GET_TID(rpl);
2283 	struct sock *sk;
2284 
2285 	sk = lookup_tid(cdev->tids, hwtid);
2286 	if (unlikely(!sk)) {
2287 		pr_err("can't find conn. for hwtid %u.\n", hwtid);
2288 		return -EINVAL;
2289 	}
2290 	process_cpl_msg(chtls_rx_ack, sk, skb);
2291 
2292 	return 0;
2293 }
2294 
2295 chtls_handler_func chtls_handlers[NUM_CPL_CMDS] = {
2296 	[CPL_PASS_OPEN_RPL]     = chtls_pass_open_rpl,
2297 	[CPL_CLOSE_LISTSRV_RPL] = chtls_close_listsrv_rpl,
2298 	[CPL_PASS_ACCEPT_REQ]   = chtls_pass_accept_req,
2299 	[CPL_PASS_ESTABLISH]    = chtls_pass_establish,
2300 	[CPL_RX_DATA]           = chtls_rx_data,
2301 	[CPL_TLS_DATA]          = chtls_rx_pdu,
2302 	[CPL_RX_TLS_CMP]        = chtls_rx_cmp,
2303 	[CPL_PEER_CLOSE]        = chtls_conn_cpl,
2304 	[CPL_CLOSE_CON_RPL]     = chtls_conn_cpl,
2305 	[CPL_ABORT_REQ_RSS]     = chtls_conn_cpl,
2306 	[CPL_ABORT_RPL_RSS]     = chtls_conn_cpl,
2307 	[CPL_FW4_ACK]           = chtls_wr_ack,
2308 };
2309