• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <net/tcp.h>
19 #include <net/busy_poll.h>
20 #include <crypto/aes.h>
21 
22 #include "chtls.h"
23 #include "chtls_cm.h"
24 
is_tls_tx(struct chtls_sock * csk)25 static bool is_tls_tx(struct chtls_sock *csk)
26 {
27 	return csk->tlshws.txkey >= 0;
28 }
29 
is_tls_rx(struct chtls_sock * csk)30 static bool is_tls_rx(struct chtls_sock *csk)
31 {
32 	return csk->tlshws.rxkey >= 0;
33 }
34 
data_sgl_len(const struct sk_buff * skb)35 static int data_sgl_len(const struct sk_buff *skb)
36 {
37 	unsigned int cnt;
38 
39 	cnt = skb_shinfo(skb)->nr_frags;
40 	return sgl_len(cnt) * 8;
41 }
42 
nos_ivs(struct sock * sk,unsigned int size)43 static int nos_ivs(struct sock *sk, unsigned int size)
44 {
45 	struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
46 
47 	return DIV_ROUND_UP(size, csk->tlshws.mfs);
48 }
49 
set_ivs_imm(struct sock * sk,const struct sk_buff * skb)50 static int set_ivs_imm(struct sock *sk, const struct sk_buff *skb)
51 {
52 	int ivs_size = nos_ivs(sk, skb->len) * CIPHER_BLOCK_SIZE;
53 	int hlen = TLS_WR_CPL_LEN + data_sgl_len(skb);
54 
55 	if ((hlen + KEY_ON_MEM_SZ + ivs_size) <
56 	    MAX_IMM_OFLD_TX_DATA_WR_LEN) {
57 		ULP_SKB_CB(skb)->ulp.tls.iv = 1;
58 		return 1;
59 	}
60 	ULP_SKB_CB(skb)->ulp.tls.iv = 0;
61 	return 0;
62 }
63 
max_ivs_size(struct sock * sk,int size)64 static int max_ivs_size(struct sock *sk, int size)
65 {
66 	return nos_ivs(sk, size) * CIPHER_BLOCK_SIZE;
67 }
68 
ivs_size(struct sock * sk,const struct sk_buff * skb)69 static int ivs_size(struct sock *sk, const struct sk_buff *skb)
70 {
71 	return set_ivs_imm(sk, skb) ? (nos_ivs(sk, skb->len) *
72 		 CIPHER_BLOCK_SIZE) : 0;
73 }
74 
flowc_wr_credits(int nparams,int * flowclenp)75 static int flowc_wr_credits(int nparams, int *flowclenp)
76 {
77 	int flowclen16, flowclen;
78 
79 	flowclen = offsetof(struct fw_flowc_wr, mnemval[nparams]);
80 	flowclen16 = DIV_ROUND_UP(flowclen, 16);
81 	flowclen = flowclen16 * 16;
82 
83 	if (flowclenp)
84 		*flowclenp = flowclen;
85 
86 	return flowclen16;
87 }
88 
create_flowc_wr_skb(struct sock * sk,struct fw_flowc_wr * flowc,int flowclen)89 static struct sk_buff *create_flowc_wr_skb(struct sock *sk,
90 					   struct fw_flowc_wr *flowc,
91 					   int flowclen)
92 {
93 	struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
94 	struct sk_buff *skb;
95 
96 	skb = alloc_skb(flowclen, GFP_ATOMIC);
97 	if (!skb)
98 		return NULL;
99 
100 	memcpy(__skb_put(skb, flowclen), flowc, flowclen);
101 	skb_set_queue_mapping(skb, (csk->txq_idx << 1) | CPL_PRIORITY_DATA);
102 
103 	return skb;
104 }
105 
send_flowc_wr(struct sock * sk,struct fw_flowc_wr * flowc,int flowclen)106 static int send_flowc_wr(struct sock *sk, struct fw_flowc_wr *flowc,
107 			 int flowclen)
108 {
109 	struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
110 	struct tcp_sock *tp = tcp_sk(sk);
111 	struct sk_buff *skb;
112 	int flowclen16;
113 	int ret;
114 
115 	flowclen16 = flowclen / 16;
116 
117 	if (csk_flag(sk, CSK_TX_DATA_SENT)) {
118 		skb = create_flowc_wr_skb(sk, flowc, flowclen);
119 		if (!skb)
120 			return -ENOMEM;
121 
122 		skb_entail(sk, skb,
123 			   ULPCB_FLAG_NO_HDR | ULPCB_FLAG_NO_APPEND);
124 		return 0;
125 	}
126 
127 	ret = cxgb4_immdata_send(csk->egress_dev,
128 				 csk->txq_idx,
129 				 flowc, flowclen);
130 	if (!ret)
131 		return flowclen16;
132 	skb = create_flowc_wr_skb(sk, flowc, flowclen);
133 	if (!skb)
134 		return -ENOMEM;
135 	send_or_defer(sk, tp, skb, 0);
136 	return flowclen16;
137 }
138 
tcp_state_to_flowc_state(u8 state)139 static u8 tcp_state_to_flowc_state(u8 state)
140 {
141 	switch (state) {
142 	case TCP_ESTABLISHED:
143 		return FW_FLOWC_MNEM_TCPSTATE_ESTABLISHED;
144 	case TCP_CLOSE_WAIT:
145 		return FW_FLOWC_MNEM_TCPSTATE_CLOSEWAIT;
146 	case TCP_FIN_WAIT1:
147 		return FW_FLOWC_MNEM_TCPSTATE_FINWAIT1;
148 	case TCP_CLOSING:
149 		return FW_FLOWC_MNEM_TCPSTATE_CLOSING;
150 	case TCP_LAST_ACK:
151 		return FW_FLOWC_MNEM_TCPSTATE_LASTACK;
152 	case TCP_FIN_WAIT2:
153 		return FW_FLOWC_MNEM_TCPSTATE_FINWAIT2;
154 	}
155 
156 	return FW_FLOWC_MNEM_TCPSTATE_ESTABLISHED;
157 }
158 
send_tx_flowc_wr(struct sock * sk,int compl,u32 snd_nxt,u32 rcv_nxt)159 int send_tx_flowc_wr(struct sock *sk, int compl,
160 		     u32 snd_nxt, u32 rcv_nxt)
161 {
162 	struct flowc_packed {
163 		struct fw_flowc_wr fc;
164 		struct fw_flowc_mnemval mnemval[FW_FLOWC_MNEM_MAX];
165 	} __packed sflowc;
166 	int nparams, paramidx, flowclen16, flowclen;
167 	struct fw_flowc_wr *flowc;
168 	struct chtls_sock *csk;
169 	struct tcp_sock *tp;
170 
171 	csk = rcu_dereference_sk_user_data(sk);
172 	tp = tcp_sk(sk);
173 	memset(&sflowc, 0, sizeof(sflowc));
174 	flowc = &sflowc.fc;
175 
176 #define FLOWC_PARAM(__m, __v) \
177 	do { \
178 		flowc->mnemval[paramidx].mnemonic = FW_FLOWC_MNEM_##__m; \
179 		flowc->mnemval[paramidx].val = cpu_to_be32(__v); \
180 		paramidx++; \
181 	} while (0)
182 
183 	paramidx = 0;
184 
185 	FLOWC_PARAM(PFNVFN, FW_PFVF_CMD_PFN_V(csk->cdev->lldi->pf));
186 	FLOWC_PARAM(CH, csk->tx_chan);
187 	FLOWC_PARAM(PORT, csk->tx_chan);
188 	FLOWC_PARAM(IQID, csk->rss_qid);
189 	FLOWC_PARAM(SNDNXT, tp->snd_nxt);
190 	FLOWC_PARAM(RCVNXT, tp->rcv_nxt);
191 	FLOWC_PARAM(SNDBUF, csk->sndbuf);
192 	FLOWC_PARAM(MSS, tp->mss_cache);
193 	FLOWC_PARAM(TCPSTATE, tcp_state_to_flowc_state(sk->sk_state));
194 
195 	if (SND_WSCALE(tp))
196 		FLOWC_PARAM(RCV_SCALE, SND_WSCALE(tp));
197 
198 	if (csk->ulp_mode == ULP_MODE_TLS)
199 		FLOWC_PARAM(ULD_MODE, ULP_MODE_TLS);
200 
201 	if (csk->tlshws.fcplenmax)
202 		FLOWC_PARAM(TXDATAPLEN_MAX, csk->tlshws.fcplenmax);
203 
204 	nparams = paramidx;
205 #undef FLOWC_PARAM
206 
207 	flowclen16 = flowc_wr_credits(nparams, &flowclen);
208 	flowc->op_to_nparams =
209 		cpu_to_be32(FW_WR_OP_V(FW_FLOWC_WR) |
210 			    FW_WR_COMPL_V(compl) |
211 			    FW_FLOWC_WR_NPARAMS_V(nparams));
212 	flowc->flowid_len16 = cpu_to_be32(FW_WR_LEN16_V(flowclen16) |
213 					  FW_WR_FLOWID_V(csk->tid));
214 
215 	return send_flowc_wr(sk, flowc, flowclen);
216 }
217 
218 /* Copy IVs to WR */
tls_copy_ivs(struct sock * sk,struct sk_buff * skb)219 static int tls_copy_ivs(struct sock *sk, struct sk_buff *skb)
220 
221 {
222 	struct chtls_sock *csk;
223 	unsigned char *iv_loc;
224 	struct chtls_hws *hws;
225 	unsigned char *ivs;
226 	u16 number_of_ivs;
227 	struct page *page;
228 	int err = 0;
229 
230 	csk = rcu_dereference_sk_user_data(sk);
231 	hws = &csk->tlshws;
232 	number_of_ivs = nos_ivs(sk, skb->len);
233 
234 	if (number_of_ivs > MAX_IVS_PAGE) {
235 		pr_warn("MAX IVs in PAGE exceeded %d\n", number_of_ivs);
236 		return -ENOMEM;
237 	}
238 
239 	/* generate the  IVs */
240 	ivs = kmalloc_array(CIPHER_BLOCK_SIZE, number_of_ivs, GFP_ATOMIC);
241 	if (!ivs)
242 		return -ENOMEM;
243 	get_random_bytes(ivs, number_of_ivs * CIPHER_BLOCK_SIZE);
244 
245 	if (skb_ulp_tls_iv_imm(skb)) {
246 		/* send the IVs as immediate data in the WR */
247 		iv_loc = (unsigned char *)__skb_push(skb, number_of_ivs *
248 						CIPHER_BLOCK_SIZE);
249 		if (iv_loc)
250 			memcpy(iv_loc, ivs, number_of_ivs * CIPHER_BLOCK_SIZE);
251 
252 		hws->ivsize = number_of_ivs * CIPHER_BLOCK_SIZE;
253 	} else {
254 		/* Send the IVs as sgls */
255 		/* Already accounted IV DSGL for credits */
256 		skb_shinfo(skb)->nr_frags--;
257 		page = alloc_pages(sk->sk_allocation | __GFP_COMP, 0);
258 		if (!page) {
259 			pr_info("%s : Page allocation for IVs failed\n",
260 				__func__);
261 			err = -ENOMEM;
262 			goto out;
263 		}
264 		memcpy(page_address(page), ivs, number_of_ivs *
265 		       CIPHER_BLOCK_SIZE);
266 		skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page, 0,
267 				   number_of_ivs * CIPHER_BLOCK_SIZE);
268 		hws->ivsize = 0;
269 	}
270 out:
271 	kfree(ivs);
272 	return err;
273 }
274 
275 /* Copy Key to WR */
tls_copy_tx_key(struct sock * sk,struct sk_buff * skb)276 static void tls_copy_tx_key(struct sock *sk, struct sk_buff *skb)
277 {
278 	struct ulptx_sc_memrd *sc_memrd;
279 	struct chtls_sock *csk;
280 	struct chtls_dev *cdev;
281 	struct ulptx_idata *sc;
282 	struct chtls_hws *hws;
283 	u32 immdlen;
284 	int kaddr;
285 
286 	csk = rcu_dereference_sk_user_data(sk);
287 	hws = &csk->tlshws;
288 	cdev = csk->cdev;
289 
290 	immdlen = sizeof(*sc) + sizeof(*sc_memrd);
291 	kaddr = keyid_to_addr(cdev->kmap.start, hws->txkey);
292 	sc = (struct ulptx_idata *)__skb_push(skb, immdlen);
293 	if (sc) {
294 		sc->cmd_more = htonl(ULPTX_CMD_V(ULP_TX_SC_NOOP));
295 		sc->len = htonl(0);
296 		sc_memrd = (struct ulptx_sc_memrd *)(sc + 1);
297 		sc_memrd->cmd_to_len =
298 				htonl(ULPTX_CMD_V(ULP_TX_SC_MEMRD) |
299 				ULP_TX_SC_MORE_V(1) |
300 				ULPTX_LEN16_V(hws->keylen >> 4));
301 		sc_memrd->addr = htonl(kaddr);
302 	}
303 }
304 
tlstx_incr_seqnum(struct chtls_hws * hws)305 static u64 tlstx_incr_seqnum(struct chtls_hws *hws)
306 {
307 	return hws->tx_seq_no++;
308 }
309 
is_sg_request(const struct sk_buff * skb)310 static bool is_sg_request(const struct sk_buff *skb)
311 {
312 	return skb->peeked ||
313 		(skb->len > MAX_IMM_ULPTX_WR_LEN);
314 }
315 
316 /*
317  * Returns true if an sk_buff carries urgent data.
318  */
skb_urgent(struct sk_buff * skb)319 static bool skb_urgent(struct sk_buff *skb)
320 {
321 	return ULP_SKB_CB(skb)->flags & ULPCB_FLAG_URG;
322 }
323 
324 /* TLS content type for CPL SFO */
tls_content_type(unsigned char content_type)325 static unsigned char tls_content_type(unsigned char content_type)
326 {
327 	switch (content_type) {
328 	case TLS_HDR_TYPE_CCS:
329 		return CPL_TX_TLS_SFO_TYPE_CCS;
330 	case TLS_HDR_TYPE_ALERT:
331 		return CPL_TX_TLS_SFO_TYPE_ALERT;
332 	case TLS_HDR_TYPE_HANDSHAKE:
333 		return CPL_TX_TLS_SFO_TYPE_HANDSHAKE;
334 	case TLS_HDR_TYPE_HEARTBEAT:
335 		return CPL_TX_TLS_SFO_TYPE_HEARTBEAT;
336 	}
337 	return CPL_TX_TLS_SFO_TYPE_DATA;
338 }
339 
tls_tx_data_wr(struct sock * sk,struct sk_buff * skb,int dlen,int tls_immd,u32 credits,int expn,int pdus)340 static void tls_tx_data_wr(struct sock *sk, struct sk_buff *skb,
341 			   int dlen, int tls_immd, u32 credits,
342 			   int expn, int pdus)
343 {
344 	struct fw_tlstx_data_wr *req_wr;
345 	struct cpl_tx_tls_sfo *req_cpl;
346 	unsigned int wr_ulp_mode_force;
347 	struct tls_scmd *updated_scmd;
348 	unsigned char data_type;
349 	struct chtls_sock *csk;
350 	struct net_device *dev;
351 	struct chtls_hws *hws;
352 	struct tls_scmd *scmd;
353 	struct adapter *adap;
354 	unsigned char *req;
355 	int immd_len;
356 	int iv_imm;
357 	int len;
358 
359 	csk = rcu_dereference_sk_user_data(sk);
360 	iv_imm = skb_ulp_tls_iv_imm(skb);
361 	dev = csk->egress_dev;
362 	adap = netdev2adap(dev);
363 	hws = &csk->tlshws;
364 	scmd = &hws->scmd;
365 	len = dlen + expn;
366 
367 	dlen = (dlen < hws->mfs) ? dlen : hws->mfs;
368 	atomic_inc(&adap->chcr_stats.tls_pdu_tx);
369 
370 	updated_scmd = scmd;
371 	updated_scmd->seqno_numivs &= 0xffffff80;
372 	updated_scmd->seqno_numivs |= SCMD_NUM_IVS_V(pdus);
373 	hws->scmd = *updated_scmd;
374 
375 	req = (unsigned char *)__skb_push(skb, sizeof(struct cpl_tx_tls_sfo));
376 	req_cpl = (struct cpl_tx_tls_sfo *)req;
377 	req = (unsigned char *)__skb_push(skb, (sizeof(struct
378 				fw_tlstx_data_wr)));
379 
380 	req_wr = (struct fw_tlstx_data_wr *)req;
381 	immd_len = (tls_immd ? dlen : 0);
382 	req_wr->op_to_immdlen =
383 		htonl(FW_WR_OP_V(FW_TLSTX_DATA_WR) |
384 		FW_TLSTX_DATA_WR_COMPL_V(1) |
385 		FW_TLSTX_DATA_WR_IMMDLEN_V(immd_len));
386 	req_wr->flowid_len16 = htonl(FW_TLSTX_DATA_WR_FLOWID_V(csk->tid) |
387 				     FW_TLSTX_DATA_WR_LEN16_V(credits));
388 	wr_ulp_mode_force = TX_ULP_MODE_V(ULP_MODE_TLS);
389 
390 	if (is_sg_request(skb))
391 		wr_ulp_mode_force |= FW_OFLD_TX_DATA_WR_ALIGNPLD_F |
392 			((tcp_sk(sk)->nonagle & TCP_NAGLE_OFF) ? 0 :
393 			FW_OFLD_TX_DATA_WR_SHOVE_F);
394 
395 	req_wr->lsodisable_to_flags =
396 			htonl(TX_ULP_MODE_V(ULP_MODE_TLS) |
397 			      TX_URG_V(skb_urgent(skb)) |
398 			      T6_TX_FORCE_F | wr_ulp_mode_force |
399 			      TX_SHOVE_V((!csk_flag(sk, CSK_TX_MORE_DATA)) &&
400 					 skb_queue_empty(&csk->txq)));
401 
402 	req_wr->ctxloc_to_exp =
403 			htonl(FW_TLSTX_DATA_WR_NUMIVS_V(pdus) |
404 			      FW_TLSTX_DATA_WR_EXP_V(expn) |
405 			      FW_TLSTX_DATA_WR_CTXLOC_V(CHTLS_KEY_CONTEXT_DDR) |
406 			      FW_TLSTX_DATA_WR_IVDSGL_V(!iv_imm) |
407 			      FW_TLSTX_DATA_WR_KEYSIZE_V(hws->keylen >> 4));
408 
409 	/* Fill in the length */
410 	req_wr->plen = htonl(len);
411 	req_wr->mfs = htons(hws->mfs);
412 	req_wr->adjustedplen_pkd =
413 		htons(FW_TLSTX_DATA_WR_ADJUSTEDPLEN_V(hws->adjustlen));
414 	req_wr->expinplenmax_pkd =
415 		htons(FW_TLSTX_DATA_WR_EXPINPLENMAX_V(hws->expansion));
416 	req_wr->pdusinplenmax_pkd =
417 		FW_TLSTX_DATA_WR_PDUSINPLENMAX_V(hws->pdus);
418 	req_wr->r10 = 0;
419 
420 	data_type = tls_content_type(ULP_SKB_CB(skb)->ulp.tls.type);
421 	req_cpl->op_to_seg_len = htonl(CPL_TX_TLS_SFO_OPCODE_V(CPL_TX_TLS_SFO) |
422 				       CPL_TX_TLS_SFO_DATA_TYPE_V(data_type) |
423 				       CPL_TX_TLS_SFO_CPL_LEN_V(2) |
424 				       CPL_TX_TLS_SFO_SEG_LEN_V(dlen));
425 	req_cpl->pld_len = htonl(len - expn);
426 
427 	req_cpl->type_protover = htonl(CPL_TX_TLS_SFO_TYPE_V
428 		((data_type == CPL_TX_TLS_SFO_TYPE_HEARTBEAT) ?
429 		TLS_HDR_TYPE_HEARTBEAT : 0) |
430 		CPL_TX_TLS_SFO_PROTOVER_V(0));
431 
432 	/* create the s-command */
433 	req_cpl->r1_lo = 0;
434 	req_cpl->seqno_numivs  = cpu_to_be32(hws->scmd.seqno_numivs);
435 	req_cpl->ivgen_hdrlen = cpu_to_be32(hws->scmd.ivgen_hdrlen);
436 	req_cpl->scmd1 = cpu_to_be64(tlstx_incr_seqnum(hws));
437 }
438 
439 /*
440  * Calculate the TLS data expansion size
441  */
chtls_expansion_size(struct sock * sk,int data_len,int fullpdu,unsigned short * pducnt)442 static int chtls_expansion_size(struct sock *sk, int data_len,
443 				int fullpdu,
444 				unsigned short *pducnt)
445 {
446 	struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
447 	struct chtls_hws *hws = &csk->tlshws;
448 	struct tls_scmd *scmd = &hws->scmd;
449 	int fragsize = hws->mfs;
450 	int expnsize = 0;
451 	int fragleft;
452 	int fragcnt;
453 	int expppdu;
454 
455 	if (SCMD_CIPH_MODE_G(scmd->seqno_numivs) ==
456 	    SCMD_CIPH_MODE_AES_GCM) {
457 		expppdu = GCM_TAG_SIZE + AEAD_EXPLICIT_DATA_SIZE +
458 			  TLS_HEADER_LENGTH;
459 
460 		if (fullpdu) {
461 			*pducnt = data_len / (expppdu + fragsize);
462 			if (*pducnt > 32)
463 				*pducnt = 32;
464 			else if (!*pducnt)
465 				*pducnt = 1;
466 			expnsize = (*pducnt) * expppdu;
467 			return expnsize;
468 		}
469 		fragcnt = (data_len / fragsize);
470 		expnsize =  fragcnt * expppdu;
471 		fragleft = data_len % fragsize;
472 		if (fragleft > 0)
473 			expnsize += expppdu;
474 	}
475 	return expnsize;
476 }
477 
478 /* WR with IV, KEY and CPL SFO added */
make_tlstx_data_wr(struct sock * sk,struct sk_buff * skb,int tls_tx_imm,int tls_len,u32 credits)479 static void make_tlstx_data_wr(struct sock *sk, struct sk_buff *skb,
480 			       int tls_tx_imm, int tls_len, u32 credits)
481 {
482 	unsigned short pdus_per_ulp = 0;
483 	struct chtls_sock *csk;
484 	struct chtls_hws *hws;
485 	int expn_sz;
486 	int pdus;
487 
488 	csk = rcu_dereference_sk_user_data(sk);
489 	hws = &csk->tlshws;
490 	pdus = DIV_ROUND_UP(tls_len, hws->mfs);
491 	expn_sz = chtls_expansion_size(sk, tls_len, 0, NULL);
492 	if (!hws->compute) {
493 		hws->expansion = chtls_expansion_size(sk,
494 						      hws->fcplenmax,
495 						      1, &pdus_per_ulp);
496 		hws->pdus = pdus_per_ulp;
497 		hws->adjustlen = hws->pdus *
498 			((hws->expansion / hws->pdus) + hws->mfs);
499 		hws->compute = 1;
500 	}
501 	if (tls_copy_ivs(sk, skb))
502 		return;
503 	tls_copy_tx_key(sk, skb);
504 	tls_tx_data_wr(sk, skb, tls_len, tls_tx_imm, credits, expn_sz, pdus);
505 	hws->tx_seq_no += (pdus - 1);
506 }
507 
make_tx_data_wr(struct sock * sk,struct sk_buff * skb,unsigned int immdlen,int len,u32 credits,u32 compl)508 static void make_tx_data_wr(struct sock *sk, struct sk_buff *skb,
509 			    unsigned int immdlen, int len,
510 			    u32 credits, u32 compl)
511 {
512 	struct fw_ofld_tx_data_wr *req;
513 	unsigned int wr_ulp_mode_force;
514 	struct chtls_sock *csk;
515 	unsigned int opcode;
516 
517 	csk = rcu_dereference_sk_user_data(sk);
518 	opcode = FW_OFLD_TX_DATA_WR;
519 
520 	req = (struct fw_ofld_tx_data_wr *)__skb_push(skb, sizeof(*req));
521 	req->op_to_immdlen = htonl(WR_OP_V(opcode) |
522 				FW_WR_COMPL_V(compl) |
523 				FW_WR_IMMDLEN_V(immdlen));
524 	req->flowid_len16 = htonl(FW_WR_FLOWID_V(csk->tid) |
525 				FW_WR_LEN16_V(credits));
526 
527 	wr_ulp_mode_force = TX_ULP_MODE_V(csk->ulp_mode);
528 	if (is_sg_request(skb))
529 		wr_ulp_mode_force |= FW_OFLD_TX_DATA_WR_ALIGNPLD_F |
530 			((tcp_sk(sk)->nonagle & TCP_NAGLE_OFF) ? 0 :
531 				FW_OFLD_TX_DATA_WR_SHOVE_F);
532 
533 	req->tunnel_to_proxy = htonl(wr_ulp_mode_force |
534 			TX_URG_V(skb_urgent(skb)) |
535 			TX_SHOVE_V((!csk_flag(sk, CSK_TX_MORE_DATA)) &&
536 				   skb_queue_empty(&csk->txq)));
537 	req->plen = htonl(len);
538 }
539 
chtls_wr_size(struct chtls_sock * csk,const struct sk_buff * skb,bool size)540 static int chtls_wr_size(struct chtls_sock *csk, const struct sk_buff *skb,
541 			 bool size)
542 {
543 	int wr_size;
544 
545 	wr_size = TLS_WR_CPL_LEN;
546 	wr_size += KEY_ON_MEM_SZ;
547 	wr_size += ivs_size(csk->sk, skb);
548 
549 	if (size)
550 		return wr_size;
551 
552 	/* frags counted for IV dsgl */
553 	if (!skb_ulp_tls_iv_imm(skb))
554 		skb_shinfo(skb)->nr_frags++;
555 
556 	return wr_size;
557 }
558 
is_ofld_imm(struct chtls_sock * csk,const struct sk_buff * skb)559 static bool is_ofld_imm(struct chtls_sock *csk, const struct sk_buff *skb)
560 {
561 	int length = skb->len;
562 
563 	if (skb->peeked || skb->len > MAX_IMM_ULPTX_WR_LEN)
564 		return false;
565 
566 	if (likely(ULP_SKB_CB(skb)->flags & ULPCB_FLAG_NEED_HDR)) {
567 		/* Check TLS header len for Immediate */
568 		if (csk->ulp_mode == ULP_MODE_TLS &&
569 		    skb_ulp_tls_inline(skb))
570 			length += chtls_wr_size(csk, skb, true);
571 		else
572 			length += sizeof(struct fw_ofld_tx_data_wr);
573 
574 		return length <= MAX_IMM_OFLD_TX_DATA_WR_LEN;
575 	}
576 	return true;
577 }
578 
calc_tx_flits(const struct sk_buff * skb,unsigned int immdlen)579 static unsigned int calc_tx_flits(const struct sk_buff *skb,
580 				  unsigned int immdlen)
581 {
582 	unsigned int flits, cnt;
583 
584 	flits = immdlen / 8;   /* headers */
585 	cnt = skb_shinfo(skb)->nr_frags;
586 	if (skb_tail_pointer(skb) != skb_transport_header(skb))
587 		cnt++;
588 	return flits + sgl_len(cnt);
589 }
590 
arp_failure_discard(void * handle,struct sk_buff * skb)591 static void arp_failure_discard(void *handle, struct sk_buff *skb)
592 {
593 	kfree_skb(skb);
594 }
595 
chtls_push_frames(struct chtls_sock * csk,int comp)596 int chtls_push_frames(struct chtls_sock *csk, int comp)
597 {
598 	struct chtls_hws *hws = &csk->tlshws;
599 	struct tcp_sock *tp;
600 	struct sk_buff *skb;
601 	int total_size = 0;
602 	struct sock *sk;
603 	int wr_size;
604 
605 	wr_size = sizeof(struct fw_ofld_tx_data_wr);
606 	sk = csk->sk;
607 	tp = tcp_sk(sk);
608 
609 	if (unlikely(sk_in_state(sk, TCPF_SYN_SENT | TCPF_CLOSE)))
610 		return 0;
611 
612 	if (unlikely(csk_flag(sk, CSK_ABORT_SHUTDOWN)))
613 		return 0;
614 
615 	while (csk->wr_credits && (skb = skb_peek(&csk->txq)) &&
616 	       (!(ULP_SKB_CB(skb)->flags & ULPCB_FLAG_HOLD) ||
617 		skb_queue_len(&csk->txq) > 1)) {
618 		unsigned int credit_len = skb->len;
619 		unsigned int credits_needed;
620 		unsigned int completion = 0;
621 		int tls_len = skb->len;/* TLS data len before IV/key */
622 		unsigned int immdlen;
623 		int len = skb->len;    /* length [ulp bytes] inserted by hw */
624 		int flowclen16 = 0;
625 		int tls_tx_imm = 0;
626 
627 		immdlen = skb->len;
628 		if (!is_ofld_imm(csk, skb)) {
629 			immdlen = skb_transport_offset(skb);
630 			if (skb_ulp_tls_inline(skb))
631 				wr_size = chtls_wr_size(csk, skb, false);
632 			credit_len = 8 * calc_tx_flits(skb, immdlen);
633 		} else {
634 			if (skb_ulp_tls_inline(skb)) {
635 				wr_size = chtls_wr_size(csk, skb, false);
636 				tls_tx_imm = 1;
637 			}
638 		}
639 		if (likely(ULP_SKB_CB(skb)->flags & ULPCB_FLAG_NEED_HDR))
640 			credit_len += wr_size;
641 		credits_needed = DIV_ROUND_UP(credit_len, 16);
642 		if (!csk_flag_nochk(csk, CSK_TX_DATA_SENT)) {
643 			flowclen16 = send_tx_flowc_wr(sk, 1, tp->snd_nxt,
644 						      tp->rcv_nxt);
645 			if (flowclen16 <= 0)
646 				break;
647 			csk->wr_credits -= flowclen16;
648 			csk->wr_unacked += flowclen16;
649 			csk->wr_nondata += flowclen16;
650 			csk_set_flag(csk, CSK_TX_DATA_SENT);
651 		}
652 
653 		if (csk->wr_credits < credits_needed) {
654 			if (skb_ulp_tls_inline(skb) &&
655 			    !skb_ulp_tls_iv_imm(skb))
656 				skb_shinfo(skb)->nr_frags--;
657 			break;
658 		}
659 
660 		__skb_unlink(skb, &csk->txq);
661 		skb_set_queue_mapping(skb, (csk->txq_idx << 1) |
662 				      CPL_PRIORITY_DATA);
663 		if (hws->ofld)
664 			hws->txqid = (skb->queue_mapping >> 1);
665 		skb->csum = (__force __wsum)(credits_needed + csk->wr_nondata);
666 		csk->wr_credits -= credits_needed;
667 		csk->wr_unacked += credits_needed;
668 		csk->wr_nondata = 0;
669 		enqueue_wr(csk, skb);
670 
671 		if (likely(ULP_SKB_CB(skb)->flags & ULPCB_FLAG_NEED_HDR)) {
672 			if ((comp && csk->wr_unacked == credits_needed) ||
673 			    (ULP_SKB_CB(skb)->flags & ULPCB_FLAG_COMPL) ||
674 			    csk->wr_unacked >= csk->wr_max_credits / 2) {
675 				completion = 1;
676 				csk->wr_unacked = 0;
677 			}
678 			if (skb_ulp_tls_inline(skb))
679 				make_tlstx_data_wr(sk, skb, tls_tx_imm,
680 						   tls_len, credits_needed);
681 			else
682 				make_tx_data_wr(sk, skb, immdlen, len,
683 						credits_needed, completion);
684 			tp->snd_nxt += len;
685 			tp->lsndtime = tcp_time_stamp(tp);
686 			if (completion)
687 				ULP_SKB_CB(skb)->flags &= ~ULPCB_FLAG_NEED_HDR;
688 		} else {
689 			struct cpl_close_con_req *req = cplhdr(skb);
690 			unsigned int cmd  = CPL_OPCODE_G(ntohl
691 					     (OPCODE_TID(req)));
692 
693 			if (cmd == CPL_CLOSE_CON_REQ)
694 				csk_set_flag(csk,
695 					     CSK_CLOSE_CON_REQUESTED);
696 
697 			if ((ULP_SKB_CB(skb)->flags & ULPCB_FLAG_COMPL) &&
698 			    (csk->wr_unacked >= csk->wr_max_credits / 2)) {
699 				req->wr.wr_hi |= htonl(FW_WR_COMPL_F);
700 				csk->wr_unacked = 0;
701 			}
702 		}
703 		total_size += skb->truesize;
704 		if (ULP_SKB_CB(skb)->flags & ULPCB_FLAG_BARRIER)
705 			csk_set_flag(csk, CSK_TX_WAIT_IDLE);
706 		t4_set_arp_err_handler(skb, NULL, arp_failure_discard);
707 		cxgb4_l2t_send(csk->egress_dev, skb, csk->l2t_entry);
708 	}
709 	sk->sk_wmem_queued -= total_size;
710 	return total_size;
711 }
712 
mark_urg(struct tcp_sock * tp,int flags,struct sk_buff * skb)713 static void mark_urg(struct tcp_sock *tp, int flags,
714 		     struct sk_buff *skb)
715 {
716 	if (unlikely(flags & MSG_OOB)) {
717 		tp->snd_up = tp->write_seq;
718 		ULP_SKB_CB(skb)->flags = ULPCB_FLAG_URG |
719 					 ULPCB_FLAG_BARRIER |
720 					 ULPCB_FLAG_NO_APPEND |
721 					 ULPCB_FLAG_NEED_HDR;
722 	}
723 }
724 
725 /*
726  * Returns true if a connection should send more data to TCP engine
727  */
should_push(struct sock * sk)728 static bool should_push(struct sock *sk)
729 {
730 	struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
731 	struct chtls_dev *cdev = csk->cdev;
732 	struct tcp_sock *tp = tcp_sk(sk);
733 
734 	/*
735 	 * If we've released our offload resources there's nothing to do ...
736 	 */
737 	if (!cdev)
738 		return false;
739 
740 	/*
741 	 * If there aren't any work requests in flight, or there isn't enough
742 	 * data in flight, or Nagle is off then send the current TX_DATA
743 	 * otherwise hold it and wait to accumulate more data.
744 	 */
745 	return csk->wr_credits == csk->wr_max_credits ||
746 		(tp->nonagle & TCP_NAGLE_OFF);
747 }
748 
749 /*
750  * Returns true if a TCP socket is corked.
751  */
corked(const struct tcp_sock * tp,int flags)752 static bool corked(const struct tcp_sock *tp, int flags)
753 {
754 	return (flags & MSG_MORE) || (tp->nonagle & TCP_NAGLE_CORK);
755 }
756 
757 /*
758  * Returns true if a send should try to push new data.
759  */
send_should_push(struct sock * sk,int flags)760 static bool send_should_push(struct sock *sk, int flags)
761 {
762 	return should_push(sk) && !corked(tcp_sk(sk), flags);
763 }
764 
chtls_tcp_push(struct sock * sk,int flags)765 void chtls_tcp_push(struct sock *sk, int flags)
766 {
767 	struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
768 	int qlen = skb_queue_len(&csk->txq);
769 
770 	if (likely(qlen)) {
771 		struct sk_buff *skb = skb_peek_tail(&csk->txq);
772 		struct tcp_sock *tp = tcp_sk(sk);
773 
774 		mark_urg(tp, flags, skb);
775 
776 		if (!(ULP_SKB_CB(skb)->flags & ULPCB_FLAG_NO_APPEND) &&
777 		    corked(tp, flags)) {
778 			ULP_SKB_CB(skb)->flags |= ULPCB_FLAG_HOLD;
779 			return;
780 		}
781 
782 		ULP_SKB_CB(skb)->flags &= ~ULPCB_FLAG_HOLD;
783 		if (qlen == 1 &&
784 		    ((ULP_SKB_CB(skb)->flags & ULPCB_FLAG_NO_APPEND) ||
785 		     should_push(sk)))
786 			chtls_push_frames(csk, 1);
787 	}
788 }
789 
790 /*
791  * Calculate the size for a new send sk_buff.  It's maximum size so we can
792  * pack lots of data into it, unless we plan to send it immediately, in which
793  * case we size it more tightly.
794  *
795  * Note: we don't bother compensating for MSS < PAGE_SIZE because it doesn't
796  * arise in normal cases and when it does we are just wasting memory.
797  */
select_size(struct sock * sk,int io_len,int flags,int len)798 static int select_size(struct sock *sk, int io_len, int flags, int len)
799 {
800 	const int pgbreak = SKB_MAX_HEAD(len);
801 
802 	/*
803 	 * If the data wouldn't fit in the main body anyway, put only the
804 	 * header in the main body so it can use immediate data and place all
805 	 * the payload in page fragments.
806 	 */
807 	if (io_len > pgbreak)
808 		return 0;
809 
810 	/*
811 	 * If we will be accumulating payload get a large main body.
812 	 */
813 	if (!send_should_push(sk, flags))
814 		return pgbreak;
815 
816 	return io_len;
817 }
818 
skb_entail(struct sock * sk,struct sk_buff * skb,int flags)819 void skb_entail(struct sock *sk, struct sk_buff *skb, int flags)
820 {
821 	struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
822 	struct tcp_sock *tp = tcp_sk(sk);
823 
824 	ULP_SKB_CB(skb)->seq = tp->write_seq;
825 	ULP_SKB_CB(skb)->flags = flags;
826 	__skb_queue_tail(&csk->txq, skb);
827 	sk->sk_wmem_queued += skb->truesize;
828 
829 	if (TCP_PAGE(sk) && TCP_OFF(sk)) {
830 		put_page(TCP_PAGE(sk));
831 		TCP_PAGE(sk) = NULL;
832 		TCP_OFF(sk) = 0;
833 	}
834 }
835 
get_tx_skb(struct sock * sk,int size)836 static struct sk_buff *get_tx_skb(struct sock *sk, int size)
837 {
838 	struct sk_buff *skb;
839 
840 	skb = alloc_skb(size + TX_HEADER_LEN, sk->sk_allocation);
841 	if (likely(skb)) {
842 		skb_reserve(skb, TX_HEADER_LEN);
843 		skb_entail(sk, skb, ULPCB_FLAG_NEED_HDR);
844 		skb_reset_transport_header(skb);
845 	}
846 	return skb;
847 }
848 
get_record_skb(struct sock * sk,int size,bool zcopy)849 static struct sk_buff *get_record_skb(struct sock *sk, int size, bool zcopy)
850 {
851 	struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
852 	struct sk_buff *skb;
853 
854 	skb = alloc_skb(((zcopy ? 0 : size) + TX_TLSHDR_LEN +
855 			KEY_ON_MEM_SZ + max_ivs_size(sk, size)),
856 			sk->sk_allocation);
857 	if (likely(skb)) {
858 		skb_reserve(skb, (TX_TLSHDR_LEN +
859 			    KEY_ON_MEM_SZ + max_ivs_size(sk, size)));
860 		skb_entail(sk, skb, ULPCB_FLAG_NEED_HDR);
861 		skb_reset_transport_header(skb);
862 		ULP_SKB_CB(skb)->ulp.tls.ofld = 1;
863 		ULP_SKB_CB(skb)->ulp.tls.type = csk->tlshws.type;
864 	}
865 	return skb;
866 }
867 
tx_skb_finalize(struct sk_buff * skb)868 static void tx_skb_finalize(struct sk_buff *skb)
869 {
870 	struct ulp_skb_cb *cb = ULP_SKB_CB(skb);
871 
872 	if (!(cb->flags & ULPCB_FLAG_NO_HDR))
873 		cb->flags = ULPCB_FLAG_NEED_HDR;
874 	cb->flags |= ULPCB_FLAG_NO_APPEND;
875 }
876 
push_frames_if_head(struct sock * sk)877 static void push_frames_if_head(struct sock *sk)
878 {
879 	struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
880 
881 	if (skb_queue_len(&csk->txq) == 1)
882 		chtls_push_frames(csk, 1);
883 }
884 
chtls_skb_copy_to_page_nocache(struct sock * sk,struct iov_iter * from,struct sk_buff * skb,struct page * page,int off,int copy)885 static int chtls_skb_copy_to_page_nocache(struct sock *sk,
886 					  struct iov_iter *from,
887 					  struct sk_buff *skb,
888 					  struct page *page,
889 					  int off, int copy)
890 {
891 	int err;
892 
893 	err = skb_do_copy_data_nocache(sk, skb, from, page_address(page) +
894 				       off, copy, skb->len);
895 	if (err)
896 		return err;
897 
898 	skb->len             += copy;
899 	skb->data_len        += copy;
900 	skb->truesize        += copy;
901 	sk->sk_wmem_queued   += copy;
902 	return 0;
903 }
904 
905 /* Read TLS header to find content type and data length */
tls_header_read(struct tls_hdr * thdr,struct iov_iter * from)906 static int tls_header_read(struct tls_hdr *thdr, struct iov_iter *from)
907 {
908 	if (copy_from_iter(thdr, sizeof(*thdr), from) != sizeof(*thdr))
909 		return -EFAULT;
910 	return (__force int)cpu_to_be16(thdr->length);
911 }
912 
csk_mem_free(struct chtls_dev * cdev,struct sock * sk)913 static int csk_mem_free(struct chtls_dev *cdev, struct sock *sk)
914 {
915 	return (cdev->max_host_sndbuf - sk->sk_wmem_queued);
916 }
917 
csk_wait_memory(struct chtls_dev * cdev,struct sock * sk,long * timeo_p)918 static int csk_wait_memory(struct chtls_dev *cdev,
919 			   struct sock *sk, long *timeo_p)
920 {
921 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
922 	int err = 0;
923 	long current_timeo;
924 	long vm_wait = 0;
925 	bool noblock;
926 
927 	current_timeo = *timeo_p;
928 	noblock = (*timeo_p ? false : true);
929 	if (csk_mem_free(cdev, sk)) {
930 		current_timeo = (prandom_u32() % (HZ / 5)) + 2;
931 		vm_wait = (prandom_u32() % (HZ / 5)) + 2;
932 	}
933 
934 	add_wait_queue(sk_sleep(sk), &wait);
935 	while (1) {
936 		sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
937 
938 		if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
939 			goto do_error;
940 		if (!*timeo_p) {
941 			if (noblock)
942 				set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
943 			goto do_nonblock;
944 		}
945 		if (signal_pending(current))
946 			goto do_interrupted;
947 		sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
948 		if (csk_mem_free(cdev, sk) && !vm_wait)
949 			break;
950 
951 		set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
952 		sk->sk_write_pending++;
953 		sk_wait_event(sk, &current_timeo, sk->sk_err ||
954 			      (sk->sk_shutdown & SEND_SHUTDOWN) ||
955 			      (csk_mem_free(cdev, sk) && !vm_wait), &wait);
956 		sk->sk_write_pending--;
957 
958 		if (vm_wait) {
959 			vm_wait -= current_timeo;
960 			current_timeo = *timeo_p;
961 			if (current_timeo != MAX_SCHEDULE_TIMEOUT) {
962 				current_timeo -= vm_wait;
963 				if (current_timeo < 0)
964 					current_timeo = 0;
965 			}
966 			vm_wait = 0;
967 		}
968 		*timeo_p = current_timeo;
969 	}
970 do_rm_wq:
971 	remove_wait_queue(sk_sleep(sk), &wait);
972 	return err;
973 do_error:
974 	err = -EPIPE;
975 	goto do_rm_wq;
976 do_nonblock:
977 	err = -EAGAIN;
978 	goto do_rm_wq;
979 do_interrupted:
980 	err = sock_intr_errno(*timeo_p);
981 	goto do_rm_wq;
982 }
983 
chtls_sendmsg(struct sock * sk,struct msghdr * msg,size_t size)984 int chtls_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
985 {
986 	struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
987 	struct chtls_dev *cdev = csk->cdev;
988 	struct tcp_sock *tp = tcp_sk(sk);
989 	struct sk_buff *skb;
990 	int mss, flags, err;
991 	int recordsz = 0;
992 	int copied = 0;
993 	long timeo;
994 
995 	lock_sock(sk);
996 	flags = msg->msg_flags;
997 	timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
998 
999 	if (!sk_in_state(sk, TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) {
1000 		err = sk_stream_wait_connect(sk, &timeo);
1001 		if (err)
1002 			goto out_err;
1003 	}
1004 
1005 	sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1006 	err = -EPIPE;
1007 	if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
1008 		goto out_err;
1009 
1010 	mss = csk->mss;
1011 	csk_set_flag(csk, CSK_TX_MORE_DATA);
1012 
1013 	while (msg_data_left(msg)) {
1014 		int copy = 0;
1015 
1016 		skb = skb_peek_tail(&csk->txq);
1017 		if (skb) {
1018 			copy = mss - skb->len;
1019 			skb->ip_summed = CHECKSUM_UNNECESSARY;
1020 		}
1021 		if (!csk_mem_free(cdev, sk))
1022 			goto wait_for_sndbuf;
1023 
1024 		if (is_tls_tx(csk) && !csk->tlshws.txleft) {
1025 			struct tls_hdr hdr;
1026 
1027 			recordsz = tls_header_read(&hdr, &msg->msg_iter);
1028 			size -= TLS_HEADER_LENGTH;
1029 			copied += TLS_HEADER_LENGTH;
1030 			csk->tlshws.txleft = recordsz;
1031 			csk->tlshws.type = hdr.type;
1032 			if (skb)
1033 				ULP_SKB_CB(skb)->ulp.tls.type = hdr.type;
1034 		}
1035 
1036 		if (!skb || (ULP_SKB_CB(skb)->flags & ULPCB_FLAG_NO_APPEND) ||
1037 		    copy <= 0) {
1038 new_buf:
1039 			if (skb) {
1040 				tx_skb_finalize(skb);
1041 				push_frames_if_head(sk);
1042 			}
1043 
1044 			if (is_tls_tx(csk)) {
1045 				skb = get_record_skb(sk,
1046 						     select_size(sk,
1047 								 recordsz,
1048 								 flags,
1049 								 TX_TLSHDR_LEN),
1050 								 false);
1051 			} else {
1052 				skb = get_tx_skb(sk,
1053 						 select_size(sk, size, flags,
1054 							     TX_HEADER_LEN));
1055 			}
1056 			if (unlikely(!skb))
1057 				goto wait_for_memory;
1058 
1059 			skb->ip_summed = CHECKSUM_UNNECESSARY;
1060 			copy = mss;
1061 		}
1062 		if (copy > size)
1063 			copy = size;
1064 
1065 		if (skb_tailroom(skb) > 0) {
1066 			copy = min(copy, skb_tailroom(skb));
1067 			if (is_tls_tx(csk))
1068 				copy = min_t(int, copy, csk->tlshws.txleft);
1069 			err = skb_add_data_nocache(sk, skb,
1070 						   &msg->msg_iter, copy);
1071 			if (err)
1072 				goto do_fault;
1073 		} else {
1074 			int i = skb_shinfo(skb)->nr_frags;
1075 			struct page *page = TCP_PAGE(sk);
1076 			int pg_size = PAGE_SIZE;
1077 			int off = TCP_OFF(sk);
1078 			bool merge;
1079 
1080 			if (page)
1081 				pg_size = page_size(page);
1082 			if (off < pg_size &&
1083 			    skb_can_coalesce(skb, i, page, off)) {
1084 				merge = 1;
1085 				goto copy;
1086 			}
1087 			merge = 0;
1088 			if (i == (is_tls_tx(csk) ? (MAX_SKB_FRAGS - 1) :
1089 			    MAX_SKB_FRAGS))
1090 				goto new_buf;
1091 
1092 			if (page && off == pg_size) {
1093 				put_page(page);
1094 				TCP_PAGE(sk) = page = NULL;
1095 				pg_size = PAGE_SIZE;
1096 			}
1097 
1098 			if (!page) {
1099 				gfp_t gfp = sk->sk_allocation;
1100 				int order = cdev->send_page_order;
1101 
1102 				if (order) {
1103 					page = alloc_pages(gfp | __GFP_COMP |
1104 							   __GFP_NOWARN |
1105 							   __GFP_NORETRY,
1106 							   order);
1107 					if (page)
1108 						pg_size <<= order;
1109 				}
1110 				if (!page) {
1111 					page = alloc_page(gfp);
1112 					pg_size = PAGE_SIZE;
1113 				}
1114 				if (!page)
1115 					goto wait_for_memory;
1116 				off = 0;
1117 			}
1118 copy:
1119 			if (copy > pg_size - off)
1120 				copy = pg_size - off;
1121 			if (is_tls_tx(csk))
1122 				copy = min_t(int, copy, csk->tlshws.txleft);
1123 
1124 			err = chtls_skb_copy_to_page_nocache(sk, &msg->msg_iter,
1125 							     skb, page,
1126 							     off, copy);
1127 			if (unlikely(err)) {
1128 				if (!TCP_PAGE(sk)) {
1129 					TCP_PAGE(sk) = page;
1130 					TCP_OFF(sk) = 0;
1131 				}
1132 				goto do_fault;
1133 			}
1134 			/* Update the skb. */
1135 			if (merge) {
1136 				skb_frag_size_add(
1137 						&skb_shinfo(skb)->frags[i - 1],
1138 						copy);
1139 			} else {
1140 				skb_fill_page_desc(skb, i, page, off, copy);
1141 				if (off + copy < pg_size) {
1142 					/* space left keep page */
1143 					get_page(page);
1144 					TCP_PAGE(sk) = page;
1145 				} else {
1146 					TCP_PAGE(sk) = NULL;
1147 				}
1148 			}
1149 			TCP_OFF(sk) = off + copy;
1150 		}
1151 		if (unlikely(skb->len == mss))
1152 			tx_skb_finalize(skb);
1153 		tp->write_seq += copy;
1154 		copied += copy;
1155 		size -= copy;
1156 
1157 		if (is_tls_tx(csk))
1158 			csk->tlshws.txleft -= copy;
1159 
1160 		if (corked(tp, flags) &&
1161 		    (sk_stream_wspace(sk) < sk_stream_min_wspace(sk)))
1162 			ULP_SKB_CB(skb)->flags |= ULPCB_FLAG_NO_APPEND;
1163 
1164 		if (size == 0)
1165 			goto out;
1166 
1167 		if (ULP_SKB_CB(skb)->flags & ULPCB_FLAG_NO_APPEND)
1168 			push_frames_if_head(sk);
1169 		continue;
1170 wait_for_sndbuf:
1171 		set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1172 wait_for_memory:
1173 		err = csk_wait_memory(cdev, sk, &timeo);
1174 		if (err)
1175 			goto do_error;
1176 	}
1177 out:
1178 	csk_reset_flag(csk, CSK_TX_MORE_DATA);
1179 	if (copied)
1180 		chtls_tcp_push(sk, flags);
1181 done:
1182 	release_sock(sk);
1183 	return copied;
1184 do_fault:
1185 	if (!skb->len) {
1186 		__skb_unlink(skb, &csk->txq);
1187 		sk->sk_wmem_queued -= skb->truesize;
1188 		__kfree_skb(skb);
1189 	}
1190 do_error:
1191 	if (copied)
1192 		goto out;
1193 out_err:
1194 	if (csk_conn_inline(csk))
1195 		csk_reset_flag(csk, CSK_TX_MORE_DATA);
1196 	copied = sk_stream_error(sk, flags, err);
1197 	goto done;
1198 }
1199 
chtls_sendpage(struct sock * sk,struct page * page,int offset,size_t size,int flags)1200 int chtls_sendpage(struct sock *sk, struct page *page,
1201 		   int offset, size_t size, int flags)
1202 {
1203 	struct chtls_sock *csk;
1204 	struct chtls_dev *cdev;
1205 	int mss, err, copied;
1206 	struct tcp_sock *tp;
1207 	long timeo;
1208 
1209 	tp = tcp_sk(sk);
1210 	copied = 0;
1211 	csk = rcu_dereference_sk_user_data(sk);
1212 	cdev = csk->cdev;
1213 	timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
1214 
1215 	err = sk_stream_wait_connect(sk, &timeo);
1216 	if (!sk_in_state(sk, TCPF_ESTABLISHED | TCPF_CLOSE_WAIT) &&
1217 	    err != 0)
1218 		goto out_err;
1219 
1220 	mss = csk->mss;
1221 	csk_set_flag(csk, CSK_TX_MORE_DATA);
1222 
1223 	while (size > 0) {
1224 		struct sk_buff *skb = skb_peek_tail(&csk->txq);
1225 		int copy, i;
1226 
1227 		if (!skb || (ULP_SKB_CB(skb)->flags & ULPCB_FLAG_NO_APPEND) ||
1228 		    (copy = mss - skb->len) <= 0) {
1229 new_buf:
1230 			if (!csk_mem_free(cdev, sk))
1231 				goto wait_for_sndbuf;
1232 
1233 			if (is_tls_tx(csk)) {
1234 				skb = get_record_skb(sk,
1235 						     select_size(sk, size,
1236 								 flags,
1237 								 TX_TLSHDR_LEN),
1238 						     true);
1239 			} else {
1240 				skb = get_tx_skb(sk, 0);
1241 			}
1242 			if (!skb)
1243 				goto wait_for_memory;
1244 			copy = mss;
1245 		}
1246 		if (copy > size)
1247 			copy = size;
1248 
1249 		i = skb_shinfo(skb)->nr_frags;
1250 		if (skb_can_coalesce(skb, i, page, offset)) {
1251 			skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
1252 		} else if (i < MAX_SKB_FRAGS) {
1253 			get_page(page);
1254 			skb_fill_page_desc(skb, i, page, offset, copy);
1255 		} else {
1256 			tx_skb_finalize(skb);
1257 			push_frames_if_head(sk);
1258 			goto new_buf;
1259 		}
1260 
1261 		skb->len += copy;
1262 		if (skb->len == mss)
1263 			tx_skb_finalize(skb);
1264 		skb->data_len += copy;
1265 		skb->truesize += copy;
1266 		sk->sk_wmem_queued += copy;
1267 		tp->write_seq += copy;
1268 		copied += copy;
1269 		offset += copy;
1270 		size -= copy;
1271 
1272 		if (corked(tp, flags) &&
1273 		    (sk_stream_wspace(sk) < sk_stream_min_wspace(sk)))
1274 			ULP_SKB_CB(skb)->flags |= ULPCB_FLAG_NO_APPEND;
1275 
1276 		if (!size)
1277 			break;
1278 
1279 		if (unlikely(ULP_SKB_CB(skb)->flags & ULPCB_FLAG_NO_APPEND))
1280 			push_frames_if_head(sk);
1281 		continue;
1282 wait_for_sndbuf:
1283 		set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1284 wait_for_memory:
1285 		err = csk_wait_memory(cdev, sk, &timeo);
1286 		if (err)
1287 			goto do_error;
1288 	}
1289 out:
1290 	csk_reset_flag(csk, CSK_TX_MORE_DATA);
1291 	if (copied)
1292 		chtls_tcp_push(sk, flags);
1293 done:
1294 	release_sock(sk);
1295 	return copied;
1296 
1297 do_error:
1298 	if (copied)
1299 		goto out;
1300 
1301 out_err:
1302 	if (csk_conn_inline(csk))
1303 		csk_reset_flag(csk, CSK_TX_MORE_DATA);
1304 	copied = sk_stream_error(sk, flags, err);
1305 	goto done;
1306 }
1307 
chtls_select_window(struct sock * sk)1308 static void chtls_select_window(struct sock *sk)
1309 {
1310 	struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
1311 	struct tcp_sock *tp = tcp_sk(sk);
1312 	unsigned int wnd = tp->rcv_wnd;
1313 
1314 	wnd = max_t(unsigned int, wnd, tcp_full_space(sk));
1315 	wnd = max_t(unsigned int, MIN_RCV_WND, wnd);
1316 
1317 	if (wnd > MAX_RCV_WND)
1318 		wnd = MAX_RCV_WND;
1319 
1320 /*
1321  * Check if we need to grow the receive window in response to an increase in
1322  * the socket's receive buffer size.  Some applications increase the buffer
1323  * size dynamically and rely on the window to grow accordingly.
1324  */
1325 
1326 	if (wnd > tp->rcv_wnd) {
1327 		tp->rcv_wup -= wnd - tp->rcv_wnd;
1328 		tp->rcv_wnd = wnd;
1329 		/* Mark the receive window as updated */
1330 		csk_reset_flag(csk, CSK_UPDATE_RCV_WND);
1331 	}
1332 }
1333 
1334 /*
1335  * Send RX credits through an RX_DATA_ACK CPL message.  We are permitted
1336  * to return without sending the message in case we cannot allocate
1337  * an sk_buff.  Returns the number of credits sent.
1338  */
send_rx_credits(struct chtls_sock * csk,u32 credits)1339 static u32 send_rx_credits(struct chtls_sock *csk, u32 credits)
1340 {
1341 	struct cpl_rx_data_ack *req;
1342 	struct sk_buff *skb;
1343 
1344 	skb = alloc_skb(sizeof(*req), GFP_ATOMIC);
1345 	if (!skb)
1346 		return 0;
1347 	__skb_put(skb, sizeof(*req));
1348 	req = (struct cpl_rx_data_ack *)skb->head;
1349 
1350 	set_wr_txq(skb, CPL_PRIORITY_ACK, csk->port_id);
1351 	INIT_TP_WR(req, csk->tid);
1352 	OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_RX_DATA_ACK,
1353 						    csk->tid));
1354 	req->credit_dack = cpu_to_be32(RX_CREDITS_V(credits) |
1355 				       RX_FORCE_ACK_F);
1356 	cxgb4_ofld_send(csk->cdev->ports[csk->port_id], skb);
1357 	return credits;
1358 }
1359 
1360 #define CREDIT_RETURN_STATE (TCPF_ESTABLISHED | \
1361 			     TCPF_FIN_WAIT1 | \
1362 			     TCPF_FIN_WAIT2)
1363 
1364 /*
1365  * Called after some received data has been read.  It returns RX credits
1366  * to the HW for the amount of data processed.
1367  */
chtls_cleanup_rbuf(struct sock * sk,int copied)1368 static void chtls_cleanup_rbuf(struct sock *sk, int copied)
1369 {
1370 	struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
1371 	struct tcp_sock *tp;
1372 	int must_send;
1373 	u32 credits;
1374 	u32 thres;
1375 
1376 	thres = 15 * 1024;
1377 
1378 	if (!sk_in_state(sk, CREDIT_RETURN_STATE))
1379 		return;
1380 
1381 	chtls_select_window(sk);
1382 	tp = tcp_sk(sk);
1383 	credits = tp->copied_seq - tp->rcv_wup;
1384 	if (unlikely(!credits))
1385 		return;
1386 
1387 /*
1388  * For coalescing to work effectively ensure the receive window has
1389  * at least 16KB left.
1390  */
1391 	must_send = credits + 16384 >= tp->rcv_wnd;
1392 
1393 	if (must_send || credits >= thres)
1394 		tp->rcv_wup += send_rx_credits(csk, credits);
1395 }
1396 
chtls_pt_recvmsg(struct sock * sk,struct msghdr * msg,size_t len,int nonblock,int flags,int * addr_len)1397 static int chtls_pt_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
1398 			    int nonblock, int flags, int *addr_len)
1399 {
1400 	struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
1401 	struct chtls_hws *hws = &csk->tlshws;
1402 	struct tcp_sock *tp = tcp_sk(sk);
1403 	unsigned long avail;
1404 	int buffers_freed;
1405 	int copied = 0;
1406 	int target;
1407 	long timeo;
1408 
1409 	buffers_freed = 0;
1410 
1411 	timeo = sock_rcvtimeo(sk, nonblock);
1412 	target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
1413 
1414 	if (unlikely(csk_flag(sk, CSK_UPDATE_RCV_WND)))
1415 		chtls_cleanup_rbuf(sk, copied);
1416 
1417 	do {
1418 		struct sk_buff *skb;
1419 		u32 offset = 0;
1420 
1421 		if (unlikely(tp->urg_data &&
1422 			     tp->urg_seq == tp->copied_seq)) {
1423 			if (copied)
1424 				break;
1425 			if (signal_pending(current)) {
1426 				copied = timeo ? sock_intr_errno(timeo) :
1427 					-EAGAIN;
1428 				break;
1429 			}
1430 		}
1431 		skb = skb_peek(&sk->sk_receive_queue);
1432 		if (skb)
1433 			goto found_ok_skb;
1434 		if (csk->wr_credits &&
1435 		    skb_queue_len(&csk->txq) &&
1436 		    chtls_push_frames(csk, csk->wr_credits ==
1437 				      csk->wr_max_credits))
1438 			sk->sk_write_space(sk);
1439 
1440 		if (copied >= target && !sk->sk_backlog.tail)
1441 			break;
1442 
1443 		if (copied) {
1444 			if (sk->sk_err || sk->sk_state == TCP_CLOSE ||
1445 			    (sk->sk_shutdown & RCV_SHUTDOWN) ||
1446 			    signal_pending(current))
1447 				break;
1448 
1449 			if (!timeo)
1450 				break;
1451 		} else {
1452 			if (sock_flag(sk, SOCK_DONE))
1453 				break;
1454 			if (sk->sk_err) {
1455 				copied = sock_error(sk);
1456 				break;
1457 			}
1458 			if (sk->sk_shutdown & RCV_SHUTDOWN)
1459 				break;
1460 			if (sk->sk_state == TCP_CLOSE) {
1461 				copied = -ENOTCONN;
1462 				break;
1463 			}
1464 			if (!timeo) {
1465 				copied = -EAGAIN;
1466 				break;
1467 			}
1468 			if (signal_pending(current)) {
1469 				copied = sock_intr_errno(timeo);
1470 				break;
1471 			}
1472 		}
1473 		if (sk->sk_backlog.tail) {
1474 			release_sock(sk);
1475 			lock_sock(sk);
1476 			chtls_cleanup_rbuf(sk, copied);
1477 			continue;
1478 		}
1479 
1480 		if (copied >= target)
1481 			break;
1482 		chtls_cleanup_rbuf(sk, copied);
1483 		sk_wait_data(sk, &timeo, NULL);
1484 		continue;
1485 found_ok_skb:
1486 		if (!skb->len) {
1487 			skb_dst_set(skb, NULL);
1488 			__skb_unlink(skb, &sk->sk_receive_queue);
1489 			kfree_skb(skb);
1490 
1491 			if (!copied && !timeo) {
1492 				copied = -EAGAIN;
1493 				break;
1494 			}
1495 
1496 			if (copied < target) {
1497 				release_sock(sk);
1498 				lock_sock(sk);
1499 				continue;
1500 			}
1501 			break;
1502 		}
1503 		offset = hws->copied_seq;
1504 		avail = skb->len - offset;
1505 		if (len < avail)
1506 			avail = len;
1507 
1508 		if (unlikely(tp->urg_data)) {
1509 			u32 urg_offset = tp->urg_seq - tp->copied_seq;
1510 
1511 			if (urg_offset < avail) {
1512 				if (urg_offset) {
1513 					avail = urg_offset;
1514 				} else if (!sock_flag(sk, SOCK_URGINLINE)) {
1515 					/* First byte is urgent, skip */
1516 					tp->copied_seq++;
1517 					offset++;
1518 					avail--;
1519 					if (!avail)
1520 						goto skip_copy;
1521 				}
1522 			}
1523 		}
1524 		if (skb_copy_datagram_msg(skb, offset, msg, avail)) {
1525 			if (!copied) {
1526 				copied = -EFAULT;
1527 				break;
1528 			}
1529 		}
1530 
1531 		copied += avail;
1532 		len -= avail;
1533 		hws->copied_seq += avail;
1534 skip_copy:
1535 		if (tp->urg_data && after(tp->copied_seq, tp->urg_seq))
1536 			tp->urg_data = 0;
1537 
1538 		if ((avail + offset) >= skb->len) {
1539 			if (ULP_SKB_CB(skb)->flags & ULPCB_FLAG_TLS_HDR) {
1540 				tp->copied_seq += skb->len;
1541 				hws->rcvpld = skb->hdr_len;
1542 			} else {
1543 				tp->copied_seq += hws->rcvpld;
1544 			}
1545 			chtls_free_skb(sk, skb);
1546 			buffers_freed++;
1547 			hws->copied_seq = 0;
1548 			if (copied >= target &&
1549 			    !skb_peek(&sk->sk_receive_queue))
1550 				break;
1551 		}
1552 	} while (len > 0);
1553 
1554 	if (buffers_freed)
1555 		chtls_cleanup_rbuf(sk, copied);
1556 	release_sock(sk);
1557 	return copied;
1558 }
1559 
1560 /*
1561  * Peek at data in a socket's receive buffer.
1562  */
peekmsg(struct sock * sk,struct msghdr * msg,size_t len,int nonblock,int flags)1563 static int peekmsg(struct sock *sk, struct msghdr *msg,
1564 		   size_t len, int nonblock, int flags)
1565 {
1566 	struct tcp_sock *tp = tcp_sk(sk);
1567 	u32 peek_seq, offset;
1568 	struct sk_buff *skb;
1569 	int copied = 0;
1570 	size_t avail;          /* amount of available data in current skb */
1571 	long timeo;
1572 
1573 	lock_sock(sk);
1574 	timeo = sock_rcvtimeo(sk, nonblock);
1575 	peek_seq = tp->copied_seq;
1576 
1577 	do {
1578 		if (unlikely(tp->urg_data && tp->urg_seq == peek_seq)) {
1579 			if (copied)
1580 				break;
1581 			if (signal_pending(current)) {
1582 				copied = timeo ? sock_intr_errno(timeo) :
1583 				-EAGAIN;
1584 				break;
1585 			}
1586 		}
1587 
1588 		skb_queue_walk(&sk->sk_receive_queue, skb) {
1589 			offset = peek_seq - ULP_SKB_CB(skb)->seq;
1590 			if (offset < skb->len)
1591 				goto found_ok_skb;
1592 		}
1593 
1594 		/* empty receive queue */
1595 		if (copied)
1596 			break;
1597 		if (sock_flag(sk, SOCK_DONE))
1598 			break;
1599 		if (sk->sk_err) {
1600 			copied = sock_error(sk);
1601 			break;
1602 		}
1603 		if (sk->sk_shutdown & RCV_SHUTDOWN)
1604 			break;
1605 		if (sk->sk_state == TCP_CLOSE) {
1606 			copied = -ENOTCONN;
1607 			break;
1608 		}
1609 		if (!timeo) {
1610 			copied = -EAGAIN;
1611 			break;
1612 		}
1613 		if (signal_pending(current)) {
1614 			copied = sock_intr_errno(timeo);
1615 			break;
1616 		}
1617 
1618 		if (sk->sk_backlog.tail) {
1619 			/* Do not sleep, just process backlog. */
1620 			release_sock(sk);
1621 			lock_sock(sk);
1622 		} else {
1623 			sk_wait_data(sk, &timeo, NULL);
1624 		}
1625 
1626 		if (unlikely(peek_seq != tp->copied_seq)) {
1627 			if (net_ratelimit())
1628 				pr_info("TCP(%s:%d), race in MSG_PEEK.\n",
1629 					current->comm, current->pid);
1630 			peek_seq = tp->copied_seq;
1631 		}
1632 		continue;
1633 
1634 found_ok_skb:
1635 		avail = skb->len - offset;
1636 		if (len < avail)
1637 			avail = len;
1638 		/*
1639 		 * Do we have urgent data here?  We need to skip over the
1640 		 * urgent byte.
1641 		 */
1642 		if (unlikely(tp->urg_data)) {
1643 			u32 urg_offset = tp->urg_seq - peek_seq;
1644 
1645 			if (urg_offset < avail) {
1646 				/*
1647 				 * The amount of data we are preparing to copy
1648 				 * contains urgent data.
1649 				 */
1650 				if (!urg_offset) { /* First byte is urgent */
1651 					if (!sock_flag(sk, SOCK_URGINLINE)) {
1652 						peek_seq++;
1653 						offset++;
1654 						avail--;
1655 					}
1656 					if (!avail)
1657 						continue;
1658 				} else {
1659 					/* stop short of the urgent data */
1660 					avail = urg_offset;
1661 				}
1662 			}
1663 		}
1664 
1665 		/*
1666 		 * If MSG_TRUNC is specified the data is discarded.
1667 		 */
1668 		if (likely(!(flags & MSG_TRUNC)))
1669 			if (skb_copy_datagram_msg(skb, offset, msg, len)) {
1670 				if (!copied) {
1671 					copied = -EFAULT;
1672 					break;
1673 				}
1674 			}
1675 		peek_seq += avail;
1676 		copied += avail;
1677 		len -= avail;
1678 	} while (len > 0);
1679 
1680 	release_sock(sk);
1681 	return copied;
1682 }
1683 
chtls_recvmsg(struct sock * sk,struct msghdr * msg,size_t len,int nonblock,int flags,int * addr_len)1684 int chtls_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
1685 		  int nonblock, int flags, int *addr_len)
1686 {
1687 	struct tcp_sock *tp = tcp_sk(sk);
1688 	struct chtls_sock *csk;
1689 	unsigned long avail;    /* amount of available data in current skb */
1690 	int buffers_freed;
1691 	int copied = 0;
1692 	long timeo;
1693 	int target;             /* Read at least this many bytes */
1694 
1695 	buffers_freed = 0;
1696 
1697 	if (unlikely(flags & MSG_OOB))
1698 		return tcp_prot.recvmsg(sk, msg, len, nonblock, flags,
1699 					addr_len);
1700 
1701 	if (unlikely(flags & MSG_PEEK))
1702 		return peekmsg(sk, msg, len, nonblock, flags);
1703 
1704 	if (sk_can_busy_loop(sk) &&
1705 	    skb_queue_empty_lockless(&sk->sk_receive_queue) &&
1706 	    sk->sk_state == TCP_ESTABLISHED)
1707 		sk_busy_loop(sk, nonblock);
1708 
1709 	lock_sock(sk);
1710 	csk = rcu_dereference_sk_user_data(sk);
1711 
1712 	if (is_tls_rx(csk))
1713 		return chtls_pt_recvmsg(sk, msg, len, nonblock,
1714 					flags, addr_len);
1715 
1716 	timeo = sock_rcvtimeo(sk, nonblock);
1717 	target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
1718 
1719 	if (unlikely(csk_flag(sk, CSK_UPDATE_RCV_WND)))
1720 		chtls_cleanup_rbuf(sk, copied);
1721 
1722 	do {
1723 		struct sk_buff *skb;
1724 		u32 offset;
1725 
1726 		if (unlikely(tp->urg_data && tp->urg_seq == tp->copied_seq)) {
1727 			if (copied)
1728 				break;
1729 			if (signal_pending(current)) {
1730 				copied = timeo ? sock_intr_errno(timeo) :
1731 					-EAGAIN;
1732 				break;
1733 			}
1734 		}
1735 
1736 		skb = skb_peek(&sk->sk_receive_queue);
1737 		if (skb)
1738 			goto found_ok_skb;
1739 
1740 		if (csk->wr_credits &&
1741 		    skb_queue_len(&csk->txq) &&
1742 		    chtls_push_frames(csk, csk->wr_credits ==
1743 				      csk->wr_max_credits))
1744 			sk->sk_write_space(sk);
1745 
1746 		if (copied >= target && !sk->sk_backlog.tail)
1747 			break;
1748 
1749 		if (copied) {
1750 			if (sk->sk_err || sk->sk_state == TCP_CLOSE ||
1751 			    (sk->sk_shutdown & RCV_SHUTDOWN) ||
1752 			    signal_pending(current))
1753 				break;
1754 		} else {
1755 			if (sock_flag(sk, SOCK_DONE))
1756 				break;
1757 			if (sk->sk_err) {
1758 				copied = sock_error(sk);
1759 				break;
1760 			}
1761 			if (sk->sk_shutdown & RCV_SHUTDOWN)
1762 				break;
1763 			if (sk->sk_state == TCP_CLOSE) {
1764 				copied = -ENOTCONN;
1765 				break;
1766 			}
1767 			if (!timeo) {
1768 				copied = -EAGAIN;
1769 				break;
1770 			}
1771 			if (signal_pending(current)) {
1772 				copied = sock_intr_errno(timeo);
1773 				break;
1774 			}
1775 		}
1776 
1777 		if (sk->sk_backlog.tail) {
1778 			release_sock(sk);
1779 			lock_sock(sk);
1780 			chtls_cleanup_rbuf(sk, copied);
1781 			continue;
1782 		}
1783 
1784 		if (copied >= target)
1785 			break;
1786 		chtls_cleanup_rbuf(sk, copied);
1787 		sk_wait_data(sk, &timeo, NULL);
1788 		continue;
1789 
1790 found_ok_skb:
1791 		if (!skb->len) {
1792 			chtls_kfree_skb(sk, skb);
1793 			if (!copied && !timeo) {
1794 				copied = -EAGAIN;
1795 				break;
1796 			}
1797 
1798 			if (copied < target)
1799 				continue;
1800 
1801 			break;
1802 		}
1803 
1804 		offset = tp->copied_seq - ULP_SKB_CB(skb)->seq;
1805 		avail = skb->len - offset;
1806 		if (len < avail)
1807 			avail = len;
1808 
1809 		if (unlikely(tp->urg_data)) {
1810 			u32 urg_offset = tp->urg_seq - tp->copied_seq;
1811 
1812 			if (urg_offset < avail) {
1813 				if (urg_offset) {
1814 					avail = urg_offset;
1815 				} else if (!sock_flag(sk, SOCK_URGINLINE)) {
1816 					tp->copied_seq++;
1817 					offset++;
1818 					avail--;
1819 					if (!avail)
1820 						goto skip_copy;
1821 				}
1822 			}
1823 		}
1824 
1825 		if (likely(!(flags & MSG_TRUNC))) {
1826 			if (skb_copy_datagram_msg(skb, offset,
1827 						  msg, avail)) {
1828 				if (!copied) {
1829 					copied = -EFAULT;
1830 					break;
1831 				}
1832 			}
1833 		}
1834 
1835 		tp->copied_seq += avail;
1836 		copied += avail;
1837 		len -= avail;
1838 
1839 skip_copy:
1840 		if (tp->urg_data && after(tp->copied_seq, tp->urg_seq))
1841 			tp->urg_data = 0;
1842 
1843 		if (avail + offset >= skb->len) {
1844 			if (likely(skb))
1845 				chtls_free_skb(sk, skb);
1846 			buffers_freed++;
1847 
1848 			if  (copied >= target &&
1849 			     !skb_peek(&sk->sk_receive_queue))
1850 				break;
1851 		}
1852 	} while (len > 0);
1853 
1854 	if (buffers_freed)
1855 		chtls_cleanup_rbuf(sk, copied);
1856 
1857 	release_sock(sk);
1858 	return copied;
1859 }
1860