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