]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/cxgbe/tom/t4_tls.c
Add support for KTLS in the Chelsio TOE module.
[FreeBSD/FreeBSD.git] / sys / dev / cxgbe / tom / t4_tls.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2017-2018 Chelsio Communications, Inc.
5  * All rights reserved.
6  * Written by: John Baldwin <jhb@FreeBSD.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include "opt_inet.h"
31 #include "opt_kern_tls.h"
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include <sys/param.h>
37 #include <sys/ktr.h>
38 #ifdef KERN_TLS
39 #include <sys/ktls.h>
40 #endif
41 #include <sys/sglist.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44 #include <sys/systm.h>
45 #include <netinet/in.h>
46 #include <netinet/in_pcb.h>
47 #include <netinet/tcp_var.h>
48 #include <netinet/toecore.h>
49 #ifdef KERN_TLS
50 #include <opencrypto/cryptodev.h>
51 #include <opencrypto/xform.h>
52 #endif
53
54 #ifdef TCP_OFFLOAD
55 #include "common/common.h"
56 #include "common/t4_tcb.h"
57 #include "crypto/t4_crypto.h"
58 #include "tom/t4_tom_l2t.h"
59 #include "tom/t4_tom.h"
60
61 /*
62  * The TCP sequence number of a CPL_TLS_DATA mbuf is saved here while
63  * the mbuf is in the ulp_pdu_reclaimq.
64  */
65 #define tls_tcp_seq     PH_loc.thirtytwo[0]
66
67 /*
68  * Handshake lock used for the handshake timer.  Having a global lock
69  * is perhaps not ideal, but it avoids having to use callout_drain()
70  * in tls_uninit_toep() which can't block.  Also, the timer shouldn't
71  * actually fire for most connections.
72  */
73 static struct mtx tls_handshake_lock;
74
75 static void
76 t4_set_tls_tcb_field(struct toepcb *toep, uint16_t word, uint64_t mask,
77     uint64_t val)
78 {
79         struct adapter *sc = td_adapter(toep->td);
80
81         t4_set_tcb_field(sc, toep->ofld_txq, toep, word, mask, val, 0, 0);
82 }
83
84 /* TLS and DTLS common routines */
85 bool
86 can_tls_offload(struct adapter *sc)
87 {
88
89         return (sc->tt.tls && sc->cryptocaps & FW_CAPS_CONFIG_TLSKEYS);
90 }
91
92 int
93 tls_tx_key(struct toepcb *toep)
94 {
95         struct tls_ofld_info *tls_ofld = &toep->tls;
96
97         return (tls_ofld->tx_key_addr >= 0);
98 }
99
100 int
101 tls_rx_key(struct toepcb *toep)
102 {
103         struct tls_ofld_info *tls_ofld = &toep->tls;
104
105         return (tls_ofld->rx_key_addr >= 0);
106 }
107
108 static int
109 key_size(struct toepcb *toep)
110 {
111         struct tls_ofld_info *tls_ofld = &toep->tls;
112
113         return ((tls_ofld->key_location == TLS_SFO_WR_CONTEXTLOC_IMMEDIATE) ?
114                 tls_ofld->k_ctx.tx_key_info_size : KEY_IN_DDR_SIZE);
115 }
116
117 /* Set TLS Key-Id in TCB */
118 static void
119 t4_set_tls_keyid(struct toepcb *toep, unsigned int key_id)
120 {
121
122         t4_set_tls_tcb_field(toep, W_TCB_RX_TLS_KEY_TAG,
123                          V_TCB_RX_TLS_KEY_TAG(M_TCB_RX_TLS_BUF_TAG),
124                          V_TCB_RX_TLS_KEY_TAG(key_id));
125 }
126
127 /* Clear TF_RX_QUIESCE to re-enable receive. */
128 static void
129 t4_clear_rx_quiesce(struct toepcb *toep)
130 {
131
132         t4_set_tls_tcb_field(toep, W_TCB_T_FLAGS, V_TF_RX_QUIESCE(1), 0);
133 }
134
135 static void
136 tls_clr_ofld_mode(struct toepcb *toep)
137 {
138
139         tls_stop_handshake_timer(toep);
140
141         /* Operate in PDU extraction mode only. */
142         t4_set_tls_tcb_field(toep, W_TCB_ULP_RAW,
143             V_TCB_ULP_RAW(M_TCB_ULP_RAW),
144             V_TCB_ULP_RAW(V_TF_TLS_ENABLE(1)));
145         t4_clear_rx_quiesce(toep);
146 }
147
148 static void
149 tls_clr_quiesce(struct toepcb *toep)
150 {
151
152         tls_stop_handshake_timer(toep);
153         t4_clear_rx_quiesce(toep);
154 }
155
156 /*
157  * Calculate the TLS data expansion size
158  */
159 static int
160 tls_expansion_size(struct toepcb *toep, int data_len, int full_pdus_only,
161     unsigned short *pdus_per_ulp)
162 {
163         struct tls_ofld_info *tls_ofld = &toep->tls;
164         struct tls_scmd *scmd = &tls_ofld->scmd0;
165         int expn_size = 0, frag_count = 0, pad_per_pdu = 0,
166             pad_last_pdu = 0, last_frag_size = 0, max_frag_size = 0;
167         int exp_per_pdu = 0;
168         int hdr_len = TLS_HEADER_LENGTH;
169
170         do {
171                 max_frag_size = tls_ofld->k_ctx.frag_size;
172                 if (G_SCMD_CIPH_MODE(scmd->seqno_numivs) ==
173                    SCMD_CIPH_MODE_AES_GCM) {
174                         frag_count = (data_len / max_frag_size);
175                         exp_per_pdu = GCM_TAG_SIZE + AEAD_EXPLICIT_DATA_SIZE +
176                                 hdr_len;
177                         expn_size =  frag_count * exp_per_pdu;
178                         if (full_pdus_only) {
179                                 *pdus_per_ulp = data_len / (exp_per_pdu +
180                                         max_frag_size);
181                                 if (*pdus_per_ulp > 32)
182                                         *pdus_per_ulp = 32;
183                                 else if(!*pdus_per_ulp)
184                                         *pdus_per_ulp = 1;
185                                 expn_size = (*pdus_per_ulp) * exp_per_pdu;
186                                 break;
187                         }
188                         if ((last_frag_size = data_len % max_frag_size) > 0) {
189                                 frag_count += 1;
190                                 expn_size += exp_per_pdu;
191                         }
192                         break;
193                 } else if (G_SCMD_CIPH_MODE(scmd->seqno_numivs) !=
194                            SCMD_CIPH_MODE_NOP) {
195                         /* Calculate the number of fragments we can make */
196                         frag_count  = (data_len / max_frag_size);
197                         if (frag_count > 0) {
198                                 pad_per_pdu = (((howmany((max_frag_size +
199                                                        tls_ofld->mac_length),
200                                                       CIPHER_BLOCK_SIZE)) *
201                                                 CIPHER_BLOCK_SIZE) -
202                                                (max_frag_size +
203                                                 tls_ofld->mac_length));
204                                 if (!pad_per_pdu)
205                                         pad_per_pdu = CIPHER_BLOCK_SIZE;
206                                 exp_per_pdu = pad_per_pdu +
207                                         tls_ofld->mac_length +
208                                         hdr_len + CIPHER_BLOCK_SIZE;
209                                 expn_size = frag_count * exp_per_pdu;
210                         }
211                         if (full_pdus_only) {
212                                 *pdus_per_ulp = data_len / (exp_per_pdu +
213                                         max_frag_size);
214                                 if (*pdus_per_ulp > 32)
215                                         *pdus_per_ulp = 32;
216                                 else if (!*pdus_per_ulp)
217                                         *pdus_per_ulp = 1;
218                                 expn_size = (*pdus_per_ulp) * exp_per_pdu;
219                                 break;
220                         }
221                         /* Consider the last fragment */
222                         if ((last_frag_size = data_len % max_frag_size) > 0) {
223                                 pad_last_pdu = (((howmany((last_frag_size +
224                                                         tls_ofld->mac_length),
225                                                        CIPHER_BLOCK_SIZE)) *
226                                                  CIPHER_BLOCK_SIZE) -
227                                                 (last_frag_size +
228                                                  tls_ofld->mac_length));
229                                 if (!pad_last_pdu)
230                                         pad_last_pdu = CIPHER_BLOCK_SIZE;
231                                 expn_size += (pad_last_pdu +
232                                               tls_ofld->mac_length + hdr_len +
233                                               CIPHER_BLOCK_SIZE);
234                         }
235                 }
236         } while (0);
237
238         return (expn_size);
239 }
240
241 /* Copy Key to WR */
242 static void
243 tls_copy_tx_key(struct toepcb *toep, void *dst)
244 {
245         struct tls_ofld_info *tls_ofld = &toep->tls;
246         struct ulptx_sc_memrd *sc_memrd;
247         struct ulptx_idata *sc;
248
249         if (tls_ofld->k_ctx.tx_key_info_size <= 0)
250                 return;
251
252         if (tls_ofld->key_location == TLS_SFO_WR_CONTEXTLOC_DDR) {
253                 sc = dst;
254                 sc->cmd_more = htobe32(V_ULPTX_CMD(ULP_TX_SC_NOOP));
255                 sc->len = htobe32(0);
256                 sc_memrd = (struct ulptx_sc_memrd *)(sc + 1);
257                 sc_memrd->cmd_to_len = htobe32(V_ULPTX_CMD(ULP_TX_SC_MEMRD) |
258                     V_ULP_TX_SC_MORE(1) |
259                     V_ULPTX_LEN16(tls_ofld->k_ctx.tx_key_info_size >> 4));
260                 sc_memrd->addr = htobe32(tls_ofld->tx_key_addr >> 5);
261         } else if (tls_ofld->key_location == TLS_SFO_WR_CONTEXTLOC_IMMEDIATE) {
262                 memcpy(dst, &tls_ofld->k_ctx.tx,
263                     tls_ofld->k_ctx.tx_key_info_size);
264         }
265 }
266
267 /* TLS/DTLS content type  for CPL SFO */
268 static inline unsigned char
269 tls_content_type(unsigned char content_type)
270 {
271         /*
272          * XXX: Shouldn't this map CONTENT_TYPE_APP_DATA to DATA and
273          * default to "CUSTOM" for all other types including
274          * heartbeat?
275          */
276         switch (content_type) {
277         case CONTENT_TYPE_CCS:
278                 return CPL_TX_TLS_SFO_TYPE_CCS;
279         case CONTENT_TYPE_ALERT:
280                 return CPL_TX_TLS_SFO_TYPE_ALERT;
281         case CONTENT_TYPE_HANDSHAKE:
282                 return CPL_TX_TLS_SFO_TYPE_HANDSHAKE;
283         case CONTENT_TYPE_HEARTBEAT:
284                 return CPL_TX_TLS_SFO_TYPE_HEARTBEAT;
285         }
286         return CPL_TX_TLS_SFO_TYPE_DATA;
287 }
288
289 static unsigned char
290 get_cipher_key_size(unsigned int ck_size)
291 {
292         switch (ck_size) {
293         case AES_NOP: /* NOP */
294                 return 15;
295         case AES_128: /* AES128 */
296                 return CH_CK_SIZE_128;
297         case AES_192: /* AES192 */
298                 return CH_CK_SIZE_192;
299         case AES_256: /* AES256 */
300                 return CH_CK_SIZE_256;
301         default:
302                 return CH_CK_SIZE_256;
303         }
304 }
305
306 static unsigned char
307 get_mac_key_size(unsigned int mk_size)
308 {
309         switch (mk_size) {
310         case SHA_NOP: /* NOP */
311                 return CH_MK_SIZE_128;
312         case SHA_GHASH: /* GHASH */
313         case SHA_512: /* SHA512 */
314                 return CH_MK_SIZE_512;
315         case SHA_224: /* SHA2-224 */
316                 return CH_MK_SIZE_192;
317         case SHA_256: /* SHA2-256*/
318                 return CH_MK_SIZE_256;
319         case SHA_384: /* SHA384 */
320                 return CH_MK_SIZE_512;
321         case SHA1: /* SHA1 */
322         default:
323                 return CH_MK_SIZE_160;
324         }
325 }
326
327 static unsigned int
328 get_proto_ver(int proto_ver)
329 {
330         switch (proto_ver) {
331         case TLS1_2_VERSION:
332                 return TLS_1_2_VERSION;
333         case TLS1_1_VERSION:
334                 return TLS_1_1_VERSION;
335         case DTLS1_2_VERSION:
336                 return DTLS_1_2_VERSION;
337         default:
338                 return TLS_VERSION_MAX;
339         }
340 }
341
342 static void
343 tls_rxkey_flit1(struct tls_keyctx *kwr, struct tls_key_context *kctx)
344 {
345
346         if (kctx->state.enc_mode == CH_EVP_CIPH_GCM_MODE) {
347                 kwr->u.rxhdr.ivinsert_to_authinsrt =
348                     htobe64(V_TLS_KEYCTX_TX_WR_IVINSERT(6ULL) |
349                         V_TLS_KEYCTX_TX_WR_AADSTRTOFST(1ULL) |
350                         V_TLS_KEYCTX_TX_WR_AADSTOPOFST(5ULL) |
351                         V_TLS_KEYCTX_TX_WR_AUTHSRTOFST(14ULL) |
352                         V_TLS_KEYCTX_TX_WR_AUTHSTOPOFST(16ULL) |
353                         V_TLS_KEYCTX_TX_WR_CIPHERSRTOFST(14ULL) |
354                         V_TLS_KEYCTX_TX_WR_CIPHERSTOPOFST(0ULL) |
355                         V_TLS_KEYCTX_TX_WR_AUTHINSRT(16ULL));
356                 kwr->u.rxhdr.ivpresent_to_rxmk_size &=
357                         ~(V_TLS_KEYCTX_TX_WR_RXOPAD_PRESENT(1));
358                 kwr->u.rxhdr.authmode_to_rxvalid &=
359                         ~(V_TLS_KEYCTX_TX_WR_CIPHAUTHSEQCTRL(1));
360         } else {
361                 kwr->u.rxhdr.ivinsert_to_authinsrt =
362                     htobe64(V_TLS_KEYCTX_TX_WR_IVINSERT(6ULL) |
363                         V_TLS_KEYCTX_TX_WR_AADSTRTOFST(1ULL) |
364                         V_TLS_KEYCTX_TX_WR_AADSTOPOFST(5ULL) |
365                         V_TLS_KEYCTX_TX_WR_AUTHSRTOFST(22ULL) |
366                         V_TLS_KEYCTX_TX_WR_AUTHSTOPOFST(0ULL) |
367                         V_TLS_KEYCTX_TX_WR_CIPHERSRTOFST(22ULL) |
368                         V_TLS_KEYCTX_TX_WR_CIPHERSTOPOFST(0ULL) |
369                         V_TLS_KEYCTX_TX_WR_AUTHINSRT(0ULL));
370         }
371 }
372
373 /* Rx key */
374 static void
375 prepare_rxkey_wr(struct tls_keyctx *kwr, struct tls_key_context *kctx)
376 {
377         unsigned int ck_size = kctx->cipher_secret_size;
378         unsigned int mk_size = kctx->mac_secret_size;
379         int proto_ver = kctx->proto_ver;
380
381         kwr->u.rxhdr.flitcnt_hmacctrl =
382                 ((kctx->tx_key_info_size >> 4) << 3) | kctx->hmac_ctrl;
383
384         kwr->u.rxhdr.protover_ciphmode =
385                 V_TLS_KEYCTX_TX_WR_PROTOVER(get_proto_ver(proto_ver)) |
386                 V_TLS_KEYCTX_TX_WR_CIPHMODE(kctx->state.enc_mode);
387
388         kwr->u.rxhdr.authmode_to_rxvalid =
389                 V_TLS_KEYCTX_TX_WR_AUTHMODE(kctx->state.auth_mode) |
390                 V_TLS_KEYCTX_TX_WR_CIPHAUTHSEQCTRL(1) |
391                 V_TLS_KEYCTX_TX_WR_SEQNUMCTRL(3) |
392                 V_TLS_KEYCTX_TX_WR_RXVALID(1);
393
394         kwr->u.rxhdr.ivpresent_to_rxmk_size =
395                 V_TLS_KEYCTX_TX_WR_IVPRESENT(0) |
396                 V_TLS_KEYCTX_TX_WR_RXOPAD_PRESENT(1) |
397                 V_TLS_KEYCTX_TX_WR_RXCK_SIZE(get_cipher_key_size(ck_size)) |
398                 V_TLS_KEYCTX_TX_WR_RXMK_SIZE(get_mac_key_size(mk_size));
399
400         tls_rxkey_flit1(kwr, kctx);
401
402         /* No key reversal for GCM */
403         if (kctx->state.enc_mode != CH_EVP_CIPH_GCM_MODE) {
404                 t4_aes_getdeckey(kwr->keys.edkey, kctx->rx.key,
405                                  (kctx->cipher_secret_size << 3));
406                 memcpy(kwr->keys.edkey + kctx->cipher_secret_size,
407                        kctx->rx.key + kctx->cipher_secret_size,
408                        (IPAD_SIZE + OPAD_SIZE));
409         } else {
410                 memcpy(kwr->keys.edkey, kctx->rx.key,
411                        (kctx->tx_key_info_size - SALT_SIZE));
412                 memcpy(kwr->u.rxhdr.rxsalt, kctx->rx.salt, SALT_SIZE);
413         }
414 }
415
416 /* Tx key */
417 static void
418 prepare_txkey_wr(struct tls_keyctx *kwr, struct tls_key_context *kctx)
419 {
420         unsigned int ck_size = kctx->cipher_secret_size;
421         unsigned int mk_size = kctx->mac_secret_size;
422
423         kwr->u.txhdr.ctxlen =
424                 (kctx->tx_key_info_size >> 4);
425         kwr->u.txhdr.dualck_to_txvalid =
426                 V_TLS_KEYCTX_TX_WR_TXOPAD_PRESENT(1) |
427                 V_TLS_KEYCTX_TX_WR_SALT_PRESENT(1) |
428                 V_TLS_KEYCTX_TX_WR_TXCK_SIZE(get_cipher_key_size(ck_size)) |
429                 V_TLS_KEYCTX_TX_WR_TXMK_SIZE(get_mac_key_size(mk_size)) |
430                 V_TLS_KEYCTX_TX_WR_TXVALID(1);
431
432         memcpy(kwr->keys.edkey, kctx->tx.key, HDR_KCTX_SIZE);
433         if (kctx->state.enc_mode == CH_EVP_CIPH_GCM_MODE) {
434                 memcpy(kwr->u.txhdr.txsalt, kctx->tx.salt, SALT_SIZE);
435                 kwr->u.txhdr.dualck_to_txvalid &=
436                         ~(V_TLS_KEYCTX_TX_WR_TXOPAD_PRESENT(1));
437         }
438         kwr->u.txhdr.dualck_to_txvalid = htons(kwr->u.txhdr.dualck_to_txvalid);
439 }
440
441 /* TLS Key memory management */
442 static int
443 get_new_keyid(struct toepcb *toep, struct tls_key_context *k_ctx)
444 {
445         struct adapter *sc = td_adapter(toep->td);
446         vmem_addr_t addr;
447
448         if (vmem_alloc(sc->key_map, TLS_KEY_CONTEXT_SZ, M_NOWAIT | M_FIRSTFIT,
449             &addr) != 0)
450                 return (-1);
451
452         return (addr);
453 }
454
455 static void
456 free_keyid(struct toepcb *toep, int keyid)
457 {
458         struct adapter *sc = td_adapter(toep->td);
459
460         vmem_free(sc->key_map, keyid, TLS_KEY_CONTEXT_SZ);
461 }
462
463 static void
464 clear_tls_keyid(struct toepcb *toep)
465 {
466         struct tls_ofld_info *tls_ofld = &toep->tls;
467
468         if (tls_ofld->rx_key_addr >= 0) {
469                 free_keyid(toep, tls_ofld->rx_key_addr);
470                 tls_ofld->rx_key_addr = -1;
471         }
472         if (tls_ofld->tx_key_addr >= 0) {
473                 free_keyid(toep, tls_ofld->tx_key_addr);
474                 tls_ofld->tx_key_addr = -1;
475         }
476 }
477
478 static int
479 get_keyid(struct tls_ofld_info *tls_ofld, unsigned int ops)
480 {
481         return (ops & KEY_WRITE_RX ? tls_ofld->rx_key_addr :
482                 ((ops & KEY_WRITE_TX) ? tls_ofld->tx_key_addr : -1));
483 }
484
485 static int
486 get_tp_plen_max(struct tls_ofld_info *tls_ofld)
487 {
488         int plen = ((min(3*4096, TP_TX_PG_SZ))/1448) * 1448;
489
490         return (tls_ofld->k_ctx.frag_size <= 8192 ? plen : FC_TP_PLEN_MAX);
491 }
492
493 /* Send request to get the key-id */
494 static int
495 tls_program_key_id(struct toepcb *toep, struct tls_key_context *k_ctx)
496 {
497         struct tls_ofld_info *tls_ofld = &toep->tls;
498         struct adapter *sc = td_adapter(toep->td);
499         struct ofld_tx_sdesc *txsd;
500         int kwrlen, kctxlen, keyid, len;
501         struct wrqe *wr;
502         struct tls_key_req *kwr;
503         struct tls_keyctx *kctx;
504
505         kwrlen = sizeof(*kwr);
506         kctxlen = roundup2(sizeof(*kctx), 32);
507         len = roundup2(kwrlen + kctxlen, 16);
508
509         if (toep->txsd_avail == 0)
510                 return (EAGAIN);
511
512         /* Dont initialize key for re-neg */
513         if (!G_KEY_CLR_LOC(k_ctx->l_p_key)) {
514                 if ((keyid = get_new_keyid(toep, k_ctx)) < 0) {
515                         return (ENOSPC);
516                 }
517         } else {
518                 keyid = get_keyid(tls_ofld, k_ctx->l_p_key);
519         }
520
521         wr = alloc_wrqe(len, toep->ofld_txq);
522         if (wr == NULL) {
523                 free_keyid(toep, keyid);
524                 return (ENOMEM);
525         }
526         kwr = wrtod(wr);
527         memset(kwr, 0, kwrlen);
528
529         kwr->wr_hi = htobe32(V_FW_WR_OP(FW_ULPTX_WR) | F_FW_WR_COMPL |
530             F_FW_WR_ATOMIC);
531         kwr->wr_mid = htobe32(V_FW_WR_LEN16(DIV_ROUND_UP(len, 16)) |
532             V_FW_WR_FLOWID(toep->tid));
533         kwr->protocol = get_proto_ver(k_ctx->proto_ver);
534         kwr->mfs = htons(k_ctx->frag_size);
535         kwr->reneg_to_write_rx = k_ctx->l_p_key;
536
537         /* master command */
538         kwr->cmd = htobe32(V_ULPTX_CMD(ULP_TX_MEM_WRITE) |
539             V_T5_ULP_MEMIO_ORDER(1) | V_T5_ULP_MEMIO_IMM(1));
540         kwr->dlen = htobe32(V_ULP_MEMIO_DATA_LEN(kctxlen >> 5));
541         kwr->len16 = htobe32((toep->tid << 8) |
542             DIV_ROUND_UP(len - sizeof(struct work_request_hdr), 16));
543         kwr->kaddr = htobe32(V_ULP_MEMIO_ADDR(keyid >> 5));
544
545         /* sub command */
546         kwr->sc_more = htobe32(V_ULPTX_CMD(ULP_TX_SC_IMM));
547         kwr->sc_len = htobe32(kctxlen);
548
549         kctx = (struct tls_keyctx *)(kwr + 1);
550         memset(kctx, 0, kctxlen);
551
552         if (G_KEY_GET_LOC(k_ctx->l_p_key) == KEY_WRITE_TX) {
553                 tls_ofld->tx_key_addr = keyid;
554                 prepare_txkey_wr(kctx, k_ctx);
555         } else if (G_KEY_GET_LOC(k_ctx->l_p_key) == KEY_WRITE_RX) {
556                 tls_ofld->rx_key_addr = keyid;
557                 prepare_rxkey_wr(kctx, k_ctx);
558         }
559
560         txsd = &toep->txsd[toep->txsd_pidx];
561         txsd->tx_credits = DIV_ROUND_UP(len, 16);
562         txsd->plen = 0;
563         toep->tx_credits -= txsd->tx_credits;
564         if (__predict_false(++toep->txsd_pidx == toep->txsd_total))
565                 toep->txsd_pidx = 0;
566         toep->txsd_avail--;
567
568         t4_wrq_tx(sc, wr);
569
570         return (0);
571 }
572
573 /* Store a key received from SSL in DDR. */
574 static int
575 program_key_context(struct tcpcb *tp, struct toepcb *toep,
576     struct tls_key_context *uk_ctx)
577 {
578         struct adapter *sc = td_adapter(toep->td);
579         struct tls_ofld_info *tls_ofld = &toep->tls;
580         struct tls_key_context *k_ctx;
581         int error, key_offset;
582
583         if (tp->t_state != TCPS_ESTABLISHED) {
584                 /*
585                  * XXX: Matches Linux driver, but not sure this is a
586                  * very appropriate error.
587                  */
588                 return (ENOENT);
589         }
590
591         /* Stop timer on handshake completion */
592         tls_stop_handshake_timer(toep);
593
594         toep->flags &= ~TPF_FORCE_CREDITS;
595
596         CTR4(KTR_CXGBE, "%s: tid %d %s proto_ver %#x", __func__, toep->tid,
597             G_KEY_GET_LOC(uk_ctx->l_p_key) == KEY_WRITE_RX ? "KEY_WRITE_RX" :
598             "KEY_WRITE_TX", uk_ctx->proto_ver);
599
600         if (G_KEY_GET_LOC(uk_ctx->l_p_key) == KEY_WRITE_RX &&
601             ulp_mode(toep) != ULP_MODE_TLS)
602                 return (EOPNOTSUPP);
603
604         /* Don't copy the 'tx' and 'rx' fields. */
605         k_ctx = &tls_ofld->k_ctx;
606         memcpy(&k_ctx->l_p_key, &uk_ctx->l_p_key,
607             sizeof(*k_ctx) - offsetof(struct tls_key_context, l_p_key));
608
609         /* TLS version != 1.1 and !1.2 OR DTLS != 1.2 */
610         if (get_proto_ver(k_ctx->proto_ver) > DTLS_1_2_VERSION) {
611                 if (G_KEY_GET_LOC(k_ctx->l_p_key) == KEY_WRITE_RX) {
612                         tls_ofld->rx_key_addr = -1;
613                         t4_clear_rx_quiesce(toep);
614                 } else {
615                         tls_ofld->tx_key_addr = -1;
616                 }
617                 return (0);
618         }
619
620         if (k_ctx->state.enc_mode == CH_EVP_CIPH_GCM_MODE) {
621                 k_ctx->iv_size = 4;
622                 k_ctx->mac_first = 0;
623                 k_ctx->hmac_ctrl = 0;
624         } else {
625                 k_ctx->iv_size = 8; /* for CBC, iv is 16B, unit of 2B */
626                 k_ctx->mac_first = 1;
627         }
628
629         tls_ofld->scmd0.seqno_numivs =
630                 (V_SCMD_SEQ_NO_CTRL(3) |
631                  V_SCMD_PROTO_VERSION(get_proto_ver(k_ctx->proto_ver)) |
632                  V_SCMD_ENC_DEC_CTRL(SCMD_ENCDECCTRL_ENCRYPT) |
633                  V_SCMD_CIPH_AUTH_SEQ_CTRL((k_ctx->mac_first == 0)) |
634                  V_SCMD_CIPH_MODE(k_ctx->state.enc_mode) |
635                  V_SCMD_AUTH_MODE(k_ctx->state.auth_mode) |
636                  V_SCMD_HMAC_CTRL(k_ctx->hmac_ctrl) |
637                  V_SCMD_IV_SIZE(k_ctx->iv_size));
638
639         tls_ofld->scmd0.ivgen_hdrlen =
640                 (V_SCMD_IV_GEN_CTRL(k_ctx->iv_ctrl) |
641                  V_SCMD_KEY_CTX_INLINE(0) |
642                  V_SCMD_TLS_FRAG_ENABLE(1));
643
644         tls_ofld->mac_length = k_ctx->mac_secret_size;
645
646         if (G_KEY_GET_LOC(k_ctx->l_p_key) == KEY_WRITE_RX) {
647                 k_ctx->rx = uk_ctx->rx;
648                 /* Dont initialize key for re-neg */
649                 if (!G_KEY_CLR_LOC(k_ctx->l_p_key))
650                         tls_ofld->rx_key_addr = -1;
651         } else {
652                 k_ctx->tx = uk_ctx->tx;
653                 /* Dont initialize key for re-neg */
654                 if (!G_KEY_CLR_LOC(k_ctx->l_p_key))
655                         tls_ofld->tx_key_addr = -1;
656         }
657
658         /* Flush pending data before new Tx key becomes active */
659         if (G_KEY_GET_LOC(k_ctx->l_p_key) == KEY_WRITE_TX) {
660                 struct sockbuf *sb;
661
662                 /* XXX: This might not drain everything. */
663                 t4_push_frames(sc, toep, 0);
664                 sb = &toep->inp->inp_socket->so_snd;
665                 SOCKBUF_LOCK(sb);
666
667                 /* XXX: This asserts that everything has been pushed. */
668                 MPASS(sb->sb_sndptr == NULL || sb->sb_sndptr->m_next == NULL);
669                 sb->sb_sndptr = NULL;
670                 tls_ofld->sb_off = sbavail(sb);
671                 SOCKBUF_UNLOCK(sb);
672                 tls_ofld->tx_seq_no = 0;
673         }
674
675         if ((G_KEY_GET_LOC(k_ctx->l_p_key) == KEY_WRITE_RX) ||
676             (tls_ofld->key_location == TLS_SFO_WR_CONTEXTLOC_DDR)) {
677                 error = tls_program_key_id(toep, k_ctx);
678                 if (error) {
679                         /* XXX: Only clear quiesce for KEY_WRITE_RX? */
680                         t4_clear_rx_quiesce(toep);
681                         return (error);
682                 }
683         }
684
685         if (G_KEY_GET_LOC(k_ctx->l_p_key) == KEY_WRITE_RX) {
686                 /*
687                  * RX key tags are an index into the key portion of MA
688                  * memory stored as an offset from the base address in
689                  * units of 64 bytes.
690                  */
691                 key_offset = tls_ofld->rx_key_addr - sc->vres.key.start;
692                 t4_set_tls_keyid(toep, key_offset / 64);
693                 t4_set_tls_tcb_field(toep, W_TCB_ULP_RAW,
694                                  V_TCB_ULP_RAW(M_TCB_ULP_RAW),
695                                  V_TCB_ULP_RAW((V_TF_TLS_KEY_SIZE(3) |
696                                                 V_TF_TLS_CONTROL(1) |
697                                                 V_TF_TLS_ACTIVE(1) |
698                                                 V_TF_TLS_ENABLE(1))));
699                 t4_set_tls_tcb_field(toep, W_TCB_TLS_SEQ,
700                                  V_TCB_TLS_SEQ(M_TCB_TLS_SEQ),
701                                  V_TCB_TLS_SEQ(0));
702                 t4_clear_rx_quiesce(toep);
703         } else {
704                 unsigned short pdus_per_ulp;
705
706                 if (tls_ofld->key_location == TLS_SFO_WR_CONTEXTLOC_IMMEDIATE)
707                         tls_ofld->tx_key_addr = 1;
708
709                 tls_ofld->fcplenmax = get_tp_plen_max(tls_ofld);
710                 tls_ofld->expn_per_ulp = tls_expansion_size(toep,
711                                 tls_ofld->fcplenmax, 1, &pdus_per_ulp);
712                 tls_ofld->pdus_per_ulp = pdus_per_ulp;
713                 tls_ofld->adjusted_plen = tls_ofld->pdus_per_ulp *
714                         ((tls_ofld->expn_per_ulp/tls_ofld->pdus_per_ulp) +
715                          tls_ofld->k_ctx.frag_size);
716         }
717
718         return (0);
719 }
720
721 /*
722  * In some cases a client connection can hang without sending the
723  * ServerHelloDone message from the NIC to the host.  Send a dummy
724  * RX_DATA_ACK with RX_MODULATE to unstick the connection.
725  */
726 static void
727 tls_send_handshake_ack(void *arg)
728 {
729         struct toepcb *toep = arg;
730         struct tls_ofld_info *tls_ofld = &toep->tls;
731         struct adapter *sc = td_adapter(toep->td);
732
733         /*
734          * XXX: Does not have the t4_get_tcb() checks to refine the
735          * workaround.
736          */
737         callout_schedule(&tls_ofld->handshake_timer, TLS_SRV_HELLO_RD_TM * hz);
738
739         CTR2(KTR_CXGBE, "%s: tid %d sending RX_DATA_ACK", __func__, toep->tid);
740         send_rx_modulate(sc, toep);
741 }
742
743 static void
744 tls_start_handshake_timer(struct toepcb *toep)
745 {
746         struct tls_ofld_info *tls_ofld = &toep->tls;
747
748         mtx_lock(&tls_handshake_lock);
749         callout_reset(&tls_ofld->handshake_timer, TLS_SRV_HELLO_BKOFF_TM * hz,
750             tls_send_handshake_ack, toep);
751         mtx_unlock(&tls_handshake_lock);
752 }
753
754 void
755 tls_stop_handshake_timer(struct toepcb *toep)
756 {
757         struct tls_ofld_info *tls_ofld = &toep->tls;
758
759         mtx_lock(&tls_handshake_lock);
760         callout_stop(&tls_ofld->handshake_timer);
761         mtx_unlock(&tls_handshake_lock);
762 }
763
764 int
765 t4_ctloutput_tls(struct socket *so, struct sockopt *sopt)
766 {
767         struct tls_key_context uk_ctx;
768         struct inpcb *inp;
769         struct tcpcb *tp;
770         struct toepcb *toep;
771         int error, optval;
772
773         error = 0;
774         if (sopt->sopt_dir == SOPT_SET &&
775             sopt->sopt_name == TCP_TLSOM_SET_TLS_CONTEXT) {
776                 error = sooptcopyin(sopt, &uk_ctx, sizeof(uk_ctx),
777                     sizeof(uk_ctx));
778                 if (error)
779                         return (error);
780         }
781
782         inp = sotoinpcb(so);
783         KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
784         INP_WLOCK(inp);
785         if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
786                 INP_WUNLOCK(inp);
787                 return (ECONNRESET);
788         }
789         tp = intotcpcb(inp);
790         toep = tp->t_toe;
791         switch (sopt->sopt_dir) {
792         case SOPT_SET:
793                 switch (sopt->sopt_name) {
794                 case TCP_TLSOM_SET_TLS_CONTEXT:
795                         if (toep->tls.mode == TLS_MODE_KTLS)
796                                 error = EINVAL;
797                         else {
798                                 error = program_key_context(tp, toep, &uk_ctx);
799                                 if (error == 0)
800                                         toep->tls.mode = TLS_MODE_TLSOM;
801                         }
802                         INP_WUNLOCK(inp);
803                         break;
804                 case TCP_TLSOM_CLR_TLS_TOM:
805                         if (toep->tls.mode == TLS_MODE_KTLS)
806                                 error = EINVAL;
807                         else if (ulp_mode(toep) == ULP_MODE_TLS) {
808                                 CTR2(KTR_CXGBE, "%s: tid %d CLR_TLS_TOM",
809                                     __func__, toep->tid);
810                                 tls_clr_ofld_mode(toep);
811                         } else
812                                 error = EOPNOTSUPP;
813                         INP_WUNLOCK(inp);
814                         break;
815                 case TCP_TLSOM_CLR_QUIES:
816                         if (toep->tls.mode == TLS_MODE_KTLS)
817                                 error = EINVAL;
818                         else if (ulp_mode(toep) == ULP_MODE_TLS) {
819                                 CTR2(KTR_CXGBE, "%s: tid %d CLR_QUIES",
820                                     __func__, toep->tid);
821                                 tls_clr_quiesce(toep);
822                         } else
823                                 error = EOPNOTSUPP;
824                         INP_WUNLOCK(inp);
825                         break;
826                 default:
827                         INP_WUNLOCK(inp);
828                         error = EOPNOTSUPP;
829                         break;
830                 }
831                 break;
832         case SOPT_GET:
833                 switch (sopt->sopt_name) {
834                 case TCP_TLSOM_GET_TLS_TOM:
835                         /*
836                          * TLS TX is permitted on any TOE socket, but
837                          * TLS RX requires a TLS ULP mode.
838                          */
839                         optval = TLS_TOM_NONE;
840                         if (can_tls_offload(td_adapter(toep->td)) &&
841                             toep->tls.mode != TLS_MODE_KTLS) {
842                                 switch (ulp_mode(toep)) {
843                                 case ULP_MODE_NONE:
844                                 case ULP_MODE_TCPDDP:
845                                         optval = TLS_TOM_TXONLY;
846                                         break;
847                                 case ULP_MODE_TLS:
848                                         optval = TLS_TOM_BOTH;
849                                         break;
850                                 }
851                         }
852                         CTR3(KTR_CXGBE, "%s: tid %d GET_TLS_TOM = %d",
853                             __func__, toep->tid, optval);
854                         INP_WUNLOCK(inp);
855                         error = sooptcopyout(sopt, &optval, sizeof(optval));
856                         break;
857                 default:
858                         INP_WUNLOCK(inp);
859                         error = EOPNOTSUPP;
860                         break;
861                 }
862                 break;
863         }
864         return (error);
865 }
866
867 #ifdef KERN_TLS
868 /* XXX: Should share this with ccr(4) eventually. */
869 static void
870 init_ktls_gmac_hash(const char *key, int klen, char *ghash)
871 {
872         static char zeroes[GMAC_BLOCK_LEN];
873         uint32_t keysched[4 * (RIJNDAEL_MAXNR + 1)];
874         int rounds;
875
876         rounds = rijndaelKeySetupEnc(keysched, key, klen);
877         rijndaelEncrypt(keysched, rounds, zeroes, ghash);
878 }
879
880 /* XXX: Should share this with ccr(4) eventually. */
881 static void
882 ktls_copy_partial_hash(void *dst, int cri_alg, union authctx *auth_ctx)
883 {
884         uint32_t *u32;
885         uint64_t *u64;
886         u_int i;
887
888         u32 = (uint32_t *)dst;
889         u64 = (uint64_t *)dst;
890         switch (cri_alg) {
891         case CRYPTO_SHA1_HMAC:
892                 for (i = 0; i < SHA1_HASH_LEN / 4; i++)
893                         u32[i] = htobe32(auth_ctx->sha1ctx.h.b32[i]);
894                 break;
895         case CRYPTO_SHA2_256_HMAC:
896                 for (i = 0; i < SHA2_256_HASH_LEN / 4; i++)
897                         u32[i] = htobe32(auth_ctx->sha256ctx.state[i]);
898                 break;
899         case CRYPTO_SHA2_384_HMAC:
900                 for (i = 0; i < SHA2_512_HASH_LEN / 8; i++)
901                         u64[i] = htobe64(auth_ctx->sha384ctx.state[i]);
902                 break;
903         }
904 }
905
906 static void
907 init_ktls_hmac_digest(struct auth_hash *axf, u_int partial_digest_len,
908     char *key, int klen, char *dst)
909 {
910         union authctx auth_ctx;
911         char ipad[SHA2_512_BLOCK_LEN], opad[SHA2_512_BLOCK_LEN];
912         u_int i;
913
914         /*
915          * If the key is larger than the block size, use the digest of
916          * the key as the key instead.
917          */
918         klen /= 8;
919         if (klen > axf->blocksize) {
920                 axf->Init(&auth_ctx);
921                 axf->Update(&auth_ctx, key, klen);
922                 axf->Final(ipad, &auth_ctx);
923                 klen = axf->hashsize;
924         } else
925                 memcpy(ipad, key, klen);
926
927         memset(ipad + klen, 0, axf->blocksize - klen);
928         memcpy(opad, ipad, axf->blocksize);
929
930         for (i = 0; i < axf->blocksize; i++) {
931                 ipad[i] ^= HMAC_IPAD_VAL;
932                 opad[i] ^= HMAC_OPAD_VAL;
933         }
934
935         /*
936          * Hash the raw ipad and opad and store the partial results in
937          * the key context.
938          */
939         axf->Init(&auth_ctx);
940         axf->Update(&auth_ctx, ipad, axf->blocksize);
941         ktls_copy_partial_hash(dst, axf->type, &auth_ctx);
942
943         dst += roundup2(partial_digest_len, 16);
944         axf->Init(&auth_ctx);
945         axf->Update(&auth_ctx, opad, axf->blocksize);
946         ktls_copy_partial_hash(dst, axf->type, &auth_ctx);
947 }
948
949 static void
950 init_ktls_key_context(struct ktls_session *tls, struct tls_key_context *k_ctx)
951 {
952         struct auth_hash *axf;
953         u_int mac_key_size;
954         char *hash;
955
956         k_ctx->l_p_key = V_KEY_GET_LOC(KEY_WRITE_TX);
957         if (tls->params.tls_vminor == TLS_MINOR_VER_ONE)
958                 k_ctx->proto_ver = SCMD_PROTO_VERSION_TLS_1_1;
959         else
960                 k_ctx->proto_ver = SCMD_PROTO_VERSION_TLS_1_2;
961         k_ctx->cipher_secret_size = tls->params.cipher_key_len;
962         k_ctx->tx_key_info_size = sizeof(struct tx_keyctx_hdr) +
963             k_ctx->cipher_secret_size;
964         memcpy(k_ctx->tx.key, tls->params.cipher_key,
965             tls->params.cipher_key_len);
966         hash = k_ctx->tx.key + tls->params.cipher_key_len;
967         if (tls->params.cipher_algorithm == CRYPTO_AES_NIST_GCM_16) {
968                 k_ctx->state.auth_mode = SCMD_AUTH_MODE_GHASH;
969                 k_ctx->state.enc_mode = SCMD_CIPH_MODE_AES_GCM;
970                 k_ctx->iv_size = 4;
971                 k_ctx->mac_first = 0;
972                 k_ctx->hmac_ctrl = SCMD_HMAC_CTRL_NOP;
973                 k_ctx->tx_key_info_size += GMAC_BLOCK_LEN;
974                 memcpy(k_ctx->tx.salt, tls->params.iv, SALT_SIZE);
975                 init_ktls_gmac_hash(tls->params.cipher_key,
976                     tls->params.cipher_key_len * 8, hash);
977         } else {
978                 switch (tls->params.auth_algorithm) {
979                 case CRYPTO_SHA1_HMAC:
980                         axf = &auth_hash_hmac_sha1;
981                         mac_key_size = SHA1_HASH_LEN;
982                         k_ctx->state.auth_mode = SCMD_AUTH_MODE_SHA1;
983                         break;
984                 case CRYPTO_SHA2_256_HMAC:
985                         axf = &auth_hash_hmac_sha2_256;
986                         mac_key_size = SHA2_256_HASH_LEN;
987                         k_ctx->state.auth_mode = SCMD_AUTH_MODE_SHA256;
988                         break;
989                 case CRYPTO_SHA2_384_HMAC:
990                         axf = &auth_hash_hmac_sha2_384;
991                         mac_key_size = SHA2_512_HASH_LEN;
992                         k_ctx->state.auth_mode = SCMD_AUTH_MODE_SHA512_384;
993                         break;
994                 default:
995                         panic("bad auth mode");
996                 }
997                 k_ctx->state.enc_mode = SCMD_CIPH_MODE_AES_CBC;
998                 k_ctx->iv_size = 8; /* for CBC, iv is 16B, unit of 2B */
999                 k_ctx->mac_first = 1;
1000                 k_ctx->hmac_ctrl = SCMD_HMAC_CTRL_NO_TRUNC;
1001                 k_ctx->tx_key_info_size += roundup2(mac_key_size, 16) * 2;
1002                 k_ctx->mac_secret_size = mac_key_size;
1003                 init_ktls_hmac_digest(axf, mac_key_size, tls->params.auth_key,
1004                     tls->params.auth_key_len * 8, hash);
1005         }
1006
1007         k_ctx->frag_size = tls->params.max_frame_len;
1008         k_ctx->iv_ctrl = 1;
1009 }
1010
1011 int
1012 tls_alloc_ktls(struct toepcb *toep, struct ktls_session *tls)
1013 {
1014         struct tls_key_context *k_ctx;
1015         int error;
1016
1017         if (toep->tls.mode == TLS_MODE_TLSOM)
1018                 return (EINVAL);
1019         if (!can_tls_offload(td_adapter(toep->td)))
1020                 return (EINVAL);
1021         switch (ulp_mode(toep)) {
1022         case ULP_MODE_NONE:
1023         case ULP_MODE_TCPDDP:
1024                 break;
1025         default:
1026                 return (EINVAL);
1027         }
1028
1029         switch (tls->params.cipher_algorithm) {
1030         case CRYPTO_AES_CBC:
1031                 /* XXX: Explicitly ignore any provided IV. */
1032                 switch (tls->params.cipher_key_len) {
1033                 case 128 / 8:
1034                 case 192 / 8:
1035                 case 256 / 8:
1036                         break;
1037                 default:
1038                         return (EINVAL);
1039                 }
1040                 switch (tls->params.auth_algorithm) {
1041                 case CRYPTO_SHA1_HMAC:
1042                 case CRYPTO_SHA2_256_HMAC:
1043                 case CRYPTO_SHA2_384_HMAC:
1044                         break;
1045                 default:
1046                         return (EPROTONOSUPPORT);
1047                 }
1048                 break;
1049         case CRYPTO_AES_NIST_GCM_16:
1050                 if (tls->params.iv_len != SALT_SIZE)
1051                         return (EINVAL);
1052                 switch (tls->params.cipher_key_len) {
1053                 case 128 / 8:
1054                 case 192 / 8:
1055                 case 256 / 8:
1056                         break;
1057                 default:
1058                         return (EINVAL);
1059                 }
1060                 break;
1061         default:
1062                 return (EPROTONOSUPPORT);
1063         }
1064
1065         /* Only TLS 1.1 and TLS 1.2 are currently supported. */
1066         if (tls->params.tls_vmajor != TLS_MAJOR_VER_ONE ||
1067             tls->params.tls_vminor < TLS_MINOR_VER_ONE ||
1068             tls->params.tls_vminor > TLS_MINOR_VER_TWO)
1069                 return (EPROTONOSUPPORT);
1070
1071         /*
1072          * XXX: This assumes no key renegotation.  If KTLS ever supports
1073          * that we will want to allocate TLS sessions dynamically rather
1074          * than as a static member of toep.
1075          */
1076         k_ctx = &toep->tls.k_ctx;
1077         init_ktls_key_context(tls, k_ctx);
1078
1079         toep->tls.scmd0.seqno_numivs =
1080                 (V_SCMD_SEQ_NO_CTRL(3) |
1081                  V_SCMD_PROTO_VERSION(k_ctx->proto_ver) |
1082                  V_SCMD_ENC_DEC_CTRL(SCMD_ENCDECCTRL_ENCRYPT) |
1083                  V_SCMD_CIPH_AUTH_SEQ_CTRL((k_ctx->mac_first == 0)) |
1084                  V_SCMD_CIPH_MODE(k_ctx->state.enc_mode) |
1085                  V_SCMD_AUTH_MODE(k_ctx->state.auth_mode) |
1086                  V_SCMD_HMAC_CTRL(k_ctx->hmac_ctrl) |
1087                  V_SCMD_IV_SIZE(k_ctx->iv_size));
1088
1089         toep->tls.scmd0.ivgen_hdrlen =
1090                 (V_SCMD_IV_GEN_CTRL(k_ctx->iv_ctrl) |
1091                  V_SCMD_KEY_CTX_INLINE(0) |
1092                  V_SCMD_TLS_FRAG_ENABLE(1));
1093
1094         if (tls->params.cipher_algorithm == CRYPTO_AES_NIST_GCM_16)
1095                 toep->tls.iv_len = 8;
1096         else
1097                 toep->tls.iv_len = AES_BLOCK_LEN;
1098
1099         toep->tls.mac_length = k_ctx->mac_secret_size;
1100
1101         toep->tls.tx_key_addr = -1;
1102
1103         error = tls_program_key_id(toep, k_ctx);
1104         if (error)
1105                 return (error);
1106
1107         toep->tls.fcplenmax = get_tp_plen_max(&toep->tls);
1108         toep->tls.expn_per_ulp = tls->params.tls_hlen + tls->params.tls_tlen;
1109         toep->tls.pdus_per_ulp = 1;
1110         toep->tls.adjusted_plen = toep->tls.expn_per_ulp +
1111             toep->tls.k_ctx.frag_size;
1112
1113         toep->tls.mode = TLS_MODE_KTLS;
1114
1115         return (0);
1116 }
1117 #endif
1118
1119 void
1120 tls_init_toep(struct toepcb *toep)
1121 {
1122         struct tls_ofld_info *tls_ofld = &toep->tls;
1123
1124         tls_ofld->mode = TLS_MODE_OFF;
1125         tls_ofld->key_location = TLS_SFO_WR_CONTEXTLOC_DDR;
1126         tls_ofld->rx_key_addr = -1;
1127         tls_ofld->tx_key_addr = -1;
1128         if (ulp_mode(toep) == ULP_MODE_TLS)
1129                 callout_init_mtx(&tls_ofld->handshake_timer,
1130                     &tls_handshake_lock, 0);
1131 }
1132
1133 void
1134 tls_establish(struct toepcb *toep)
1135 {
1136
1137         /*
1138          * Enable PDU extraction.
1139          *
1140          * XXX: Supposedly this should be done by the firmware when
1141          * the ULP_MODE FLOWC parameter is set in send_flowc_wr(), but
1142          * in practice this seems to be required.
1143          */
1144         CTR2(KTR_CXGBE, "%s: tid %d setting TLS_ENABLE", __func__, toep->tid);
1145         t4_set_tls_tcb_field(toep, W_TCB_ULP_RAW, V_TCB_ULP_RAW(M_TCB_ULP_RAW),
1146             V_TCB_ULP_RAW(V_TF_TLS_ENABLE(1)));
1147
1148         toep->flags |= TPF_FORCE_CREDITS;
1149
1150         tls_start_handshake_timer(toep);
1151 }
1152
1153 void
1154 tls_uninit_toep(struct toepcb *toep)
1155 {
1156
1157         if (ulp_mode(toep) == ULP_MODE_TLS)
1158                 tls_stop_handshake_timer(toep);
1159         clear_tls_keyid(toep);
1160 }
1161
1162 #define MAX_OFLD_TX_CREDITS (SGE_MAX_WR_LEN / 16)
1163 #define MIN_OFLD_TLSTX_CREDITS(toep)                                    \
1164         (howmany(sizeof(struct fw_tlstx_data_wr) +                      \
1165             sizeof(struct cpl_tx_tls_sfo) + key_size((toep)) +          \
1166             CIPHER_BLOCK_SIZE + 1, 16))
1167
1168 static inline u_int
1169 max_imm_tls_space(int tx_credits)
1170 {
1171         const int n = 2;        /* Use only up to 2 desc for imm. data WR */
1172         int space;
1173
1174         KASSERT(tx_credits >= 0 &&
1175                 tx_credits <= MAX_OFLD_TX_CREDITS,
1176                 ("%s: %d credits", __func__, tx_credits));
1177
1178         if (tx_credits >= (n * EQ_ESIZE) / 16)
1179                 space = (n * EQ_ESIZE);
1180         else
1181                 space = tx_credits * 16;
1182         return (space);
1183 }
1184
1185 static int
1186 count_mbuf_segs(struct mbuf *m, int skip, int len, int *max_nsegs_1mbufp)
1187 {
1188         int max_nsegs_1mbuf, n, nsegs;
1189
1190         while (skip >= m->m_len) {
1191                 skip -= m->m_len;
1192                 m = m->m_next;
1193         }
1194
1195         nsegs = 0;
1196         max_nsegs_1mbuf = 0;
1197         while (len > 0) {
1198                 n = sglist_count(mtod(m, char *) + skip, m->m_len - skip);
1199                 if (n > max_nsegs_1mbuf)
1200                         max_nsegs_1mbuf = n;
1201                 nsegs += n;
1202                 len -= m->m_len - skip;
1203                 skip = 0;
1204                 m = m->m_next;
1205         }
1206         *max_nsegs_1mbufp = max_nsegs_1mbuf;
1207         return (nsegs);
1208 }
1209
1210 static void
1211 write_tlstx_wr(struct fw_tlstx_data_wr *txwr, struct toepcb *toep,
1212     unsigned int immdlen, unsigned int plen, unsigned int expn,
1213     unsigned int pdus, uint8_t credits, int shove, int imm_ivs)
1214 {
1215         struct tls_ofld_info *tls_ofld = &toep->tls;
1216         unsigned int len = plen + expn;
1217
1218         txwr->op_to_immdlen = htobe32(V_WR_OP(FW_TLSTX_DATA_WR) |
1219             V_FW_TLSTX_DATA_WR_COMPL(1) |
1220             V_FW_TLSTX_DATA_WR_IMMDLEN(immdlen));
1221         txwr->flowid_len16 = htobe32(V_FW_TLSTX_DATA_WR_FLOWID(toep->tid) |
1222             V_FW_TLSTX_DATA_WR_LEN16(credits));
1223         txwr->plen = htobe32(len);
1224         txwr->lsodisable_to_flags = htobe32(V_TX_ULP_MODE(ULP_MODE_TLS) |
1225             V_TX_URG(0) | /* F_T6_TX_FORCE | */ V_TX_SHOVE(shove));
1226         txwr->ctxloc_to_exp = htobe32(V_FW_TLSTX_DATA_WR_NUMIVS(pdus) |
1227             V_FW_TLSTX_DATA_WR_EXP(expn) |
1228             V_FW_TLSTX_DATA_WR_CTXLOC(tls_ofld->key_location) |
1229             V_FW_TLSTX_DATA_WR_IVDSGL(!imm_ivs) |
1230             V_FW_TLSTX_DATA_WR_KEYSIZE(tls_ofld->k_ctx.tx_key_info_size >> 4));
1231         txwr->mfs = htobe16(tls_ofld->k_ctx.frag_size);
1232         txwr->adjustedplen_pkd = htobe16(
1233             V_FW_TLSTX_DATA_WR_ADJUSTEDPLEN(tls_ofld->adjusted_plen));
1234         txwr->expinplenmax_pkd = htobe16(
1235             V_FW_TLSTX_DATA_WR_EXPINPLENMAX(tls_ofld->expn_per_ulp));
1236         txwr->pdusinplenmax_pkd = 
1237             V_FW_TLSTX_DATA_WR_PDUSINPLENMAX(tls_ofld->pdus_per_ulp);
1238 }
1239
1240 static void
1241 write_tlstx_cpl(struct cpl_tx_tls_sfo *cpl, struct toepcb *toep,
1242     struct tls_hdr *tls_hdr, unsigned int plen, unsigned int pdus)
1243 {
1244         struct tls_ofld_info *tls_ofld = &toep->tls;
1245         int data_type, seglen;
1246
1247         if (plen < tls_ofld->k_ctx.frag_size)
1248                 seglen = plen;
1249         else
1250                 seglen = tls_ofld->k_ctx.frag_size;
1251         data_type = tls_content_type(tls_hdr->type);
1252         cpl->op_to_seg_len = htobe32(V_CPL_TX_TLS_SFO_OPCODE(CPL_TX_TLS_SFO) |
1253             V_CPL_TX_TLS_SFO_DATA_TYPE(data_type) |
1254             V_CPL_TX_TLS_SFO_CPL_LEN(2) | V_CPL_TX_TLS_SFO_SEG_LEN(seglen));
1255         cpl->pld_len = htobe32(plen);
1256         if (data_type == CPL_TX_TLS_SFO_TYPE_HEARTBEAT)
1257                 cpl->type_protover = htobe32(
1258                     V_CPL_TX_TLS_SFO_TYPE(tls_hdr->type));
1259         cpl->seqno_numivs = htobe32(tls_ofld->scmd0.seqno_numivs |
1260             V_SCMD_NUM_IVS(pdus));
1261         cpl->ivgen_hdrlen = htobe32(tls_ofld->scmd0.ivgen_hdrlen);
1262         cpl->scmd1 = htobe64(tls_ofld->tx_seq_no);
1263         tls_ofld->tx_seq_no += pdus;
1264 }
1265
1266 /*
1267  * Similar to write_tx_sgl() except that it accepts an optional
1268  * trailer buffer for IVs.
1269  */
1270 static void
1271 write_tlstx_sgl(void *dst, struct mbuf *start, int skip, int plen,
1272     void *iv_buffer, int iv_len, int nsegs, int n)
1273 {
1274         struct mbuf *m;
1275         struct ulptx_sgl *usgl = dst;
1276         int i, j, rc;
1277         struct sglist sg;
1278         struct sglist_seg segs[n];
1279
1280         KASSERT(nsegs > 0, ("%s: nsegs 0", __func__));
1281
1282         sglist_init(&sg, n, segs);
1283         usgl->cmd_nsge = htobe32(V_ULPTX_CMD(ULP_TX_SC_DSGL) |
1284             V_ULPTX_NSGE(nsegs));
1285
1286         for (m = start; skip >= m->m_len; m = m->m_next)
1287                 skip -= m->m_len;
1288
1289         i = -1;
1290         for (m = start; plen > 0; m = m->m_next) {
1291                 rc = sglist_append(&sg, mtod(m, char *) + skip,
1292                     m->m_len - skip);
1293                 if (__predict_false(rc != 0))
1294                         panic("%s: sglist_append %d", __func__, rc);
1295                 plen -= m->m_len - skip;
1296                 skip = 0;
1297
1298                 for (j = 0; j < sg.sg_nseg; i++, j++) {
1299                         if (i < 0) {
1300                                 usgl->len0 = htobe32(segs[j].ss_len);
1301                                 usgl->addr0 = htobe64(segs[j].ss_paddr);
1302                         } else {
1303                                 usgl->sge[i / 2].len[i & 1] =
1304                                     htobe32(segs[j].ss_len);
1305                                 usgl->sge[i / 2].addr[i & 1] =
1306                                     htobe64(segs[j].ss_paddr);
1307                         }
1308 #ifdef INVARIANTS
1309                         nsegs--;
1310 #endif
1311                 }
1312                 sglist_reset(&sg);
1313         }
1314         if (iv_buffer != NULL) {
1315                 rc = sglist_append(&sg, iv_buffer, iv_len);
1316                 if (__predict_false(rc != 0))
1317                         panic("%s: sglist_append %d", __func__, rc);
1318
1319                 for (j = 0; j < sg.sg_nseg; i++, j++) {
1320                         if (i < 0) {
1321                                 usgl->len0 = htobe32(segs[j].ss_len);
1322                                 usgl->addr0 = htobe64(segs[j].ss_paddr);
1323                         } else {
1324                                 usgl->sge[i / 2].len[i & 1] =
1325                                     htobe32(segs[j].ss_len);
1326                                 usgl->sge[i / 2].addr[i & 1] =
1327                                     htobe64(segs[j].ss_paddr);
1328                         }
1329 #ifdef INVARIANTS
1330                         nsegs--;
1331 #endif
1332                 }
1333         }
1334         if (i & 1)
1335                 usgl->sge[i / 2].len[1] = htobe32(0);
1336         KASSERT(nsegs == 0, ("%s: nsegs %d, start %p, iv_buffer %p",
1337             __func__, nsegs, start, iv_buffer));
1338 }
1339
1340 /*
1341  * Similar to t4_push_frames() but handles TLS sockets when TLS offload
1342  * is enabled.  Rather than transmitting bulk data, the socket buffer
1343  * contains TLS records.  The work request requires a full TLS record,
1344  * so batch mbufs up until a full TLS record is seen.  This requires
1345  * reading the TLS header out of the start of each record to determine
1346  * its length.
1347  */
1348 void
1349 t4_push_tls_records(struct adapter *sc, struct toepcb *toep, int drop)
1350 {
1351         struct tls_hdr thdr;
1352         struct mbuf *sndptr;
1353         struct fw_tlstx_data_wr *txwr;
1354         struct cpl_tx_tls_sfo *cpl;
1355         struct wrqe *wr;
1356         u_int plen, nsegs, credits, space, max_nsegs_1mbuf, wr_len;
1357         u_int expn_size, iv_len, pdus, sndptroff;
1358         struct tls_ofld_info *tls_ofld = &toep->tls;
1359         struct inpcb *inp = toep->inp;
1360         struct tcpcb *tp = intotcpcb(inp);
1361         struct socket *so = inp->inp_socket;
1362         struct sockbuf *sb = &so->so_snd;
1363         int tls_size, tx_credits, shove, /* compl,*/ sowwakeup;
1364         struct ofld_tx_sdesc *txsd;
1365         bool imm_ivs, imm_payload;
1366         void *iv_buffer, *iv_dst, *buf;
1367
1368         INP_WLOCK_ASSERT(inp);
1369         KASSERT(toep->flags & TPF_FLOWC_WR_SENT,
1370             ("%s: flowc_wr not sent for tid %u.", __func__, toep->tid));
1371
1372         KASSERT(ulp_mode(toep) == ULP_MODE_NONE ||
1373             ulp_mode(toep) == ULP_MODE_TCPDDP || ulp_mode(toep) == ULP_MODE_TLS,
1374             ("%s: ulp_mode %u for toep %p", __func__, ulp_mode(toep), toep));
1375         KASSERT(tls_tx_key(toep),
1376             ("%s: TX key not set for toep %p", __func__, toep));
1377
1378 #ifdef VERBOSE_TRACES
1379         CTR4(KTR_CXGBE, "%s: tid %d toep flags %#x tp flags %#x drop %d",
1380             __func__, toep->tid, toep->flags, tp->t_flags);
1381 #endif
1382         if (__predict_false(toep->flags & TPF_ABORT_SHUTDOWN))
1383                 return;
1384
1385 #ifdef RATELIMIT
1386         if (__predict_false(inp->inp_flags2 & INP_RATE_LIMIT_CHANGED) &&
1387             (update_tx_rate_limit(sc, toep, so->so_max_pacing_rate) == 0)) {
1388                 inp->inp_flags2 &= ~INP_RATE_LIMIT_CHANGED;
1389         }
1390 #endif
1391
1392         /*
1393          * This function doesn't resume by itself.  Someone else must clear the
1394          * flag and call this function.
1395          */
1396         if (__predict_false(toep->flags & TPF_TX_SUSPENDED)) {
1397                 KASSERT(drop == 0,
1398                     ("%s: drop (%d) != 0 but tx is suspended", __func__, drop));
1399                 return;
1400         }
1401
1402         txsd = &toep->txsd[toep->txsd_pidx];
1403         for (;;) {
1404                 tx_credits = min(toep->tx_credits, MAX_OFLD_TX_CREDITS);
1405                 space = max_imm_tls_space(tx_credits);
1406                 wr_len = sizeof(struct fw_tlstx_data_wr) +
1407                     sizeof(struct cpl_tx_tls_sfo) + key_size(toep);
1408                 if (wr_len + CIPHER_BLOCK_SIZE + 1 > space) {
1409 #ifdef VERBOSE_TRACES
1410                         CTR5(KTR_CXGBE,
1411                             "%s: tid %d tx_credits %d min_wr %d space %d",
1412                             __func__, toep->tid, tx_credits, wr_len +
1413                             CIPHER_BLOCK_SIZE + 1, space);
1414 #endif
1415                         return;
1416                 }
1417
1418                 SOCKBUF_LOCK(sb);
1419                 sowwakeup = drop;
1420                 if (drop) {
1421                         sbdrop_locked(sb, drop);
1422                         MPASS(tls_ofld->sb_off >= drop);
1423                         tls_ofld->sb_off -= drop;
1424                         drop = 0;
1425                 }
1426
1427                 /*
1428                  * Send a FIN if requested, but only if there's no
1429                  * more data to send.
1430                  */
1431                 if (sbavail(sb) == tls_ofld->sb_off &&
1432                     toep->flags & TPF_SEND_FIN) {
1433                         if (sowwakeup)
1434                                 sowwakeup_locked(so);
1435                         else
1436                                 SOCKBUF_UNLOCK(sb);
1437                         SOCKBUF_UNLOCK_ASSERT(sb);
1438                         t4_close_conn(sc, toep);
1439                         return;
1440                 }
1441
1442                 if (sbavail(sb) < tls_ofld->sb_off + TLS_HEADER_LENGTH) {
1443                         /*
1444                          * A full TLS header is not yet queued, stop
1445                          * for now until more data is added to the
1446                          * socket buffer.  However, if the connection
1447                          * has been closed, we will never get the rest
1448                          * of the header so just discard the partial
1449                          * header and close the connection.
1450                          */
1451 #ifdef VERBOSE_TRACES
1452                         CTR5(KTR_CXGBE, "%s: tid %d sbavail %d sb_off %d%s",
1453                             __func__, toep->tid, sbavail(sb), tls_ofld->sb_off,
1454                             toep->flags & TPF_SEND_FIN ? "" : " SEND_FIN");
1455 #endif
1456                         if (sowwakeup)
1457                                 sowwakeup_locked(so);
1458                         else
1459                                 SOCKBUF_UNLOCK(sb);
1460                         SOCKBUF_UNLOCK_ASSERT(sb);
1461                         if (toep->flags & TPF_SEND_FIN)
1462                                 t4_close_conn(sc, toep);
1463                         return;
1464                 }
1465
1466                 /* Read the header of the next TLS record. */
1467                 sndptr = sbsndmbuf(sb, tls_ofld->sb_off, &sndptroff);
1468                 m_copydata(sndptr, sndptroff, sizeof(thdr), (caddr_t)&thdr);
1469                 tls_size = htons(thdr.length);
1470                 plen = TLS_HEADER_LENGTH + tls_size;
1471                 pdus = howmany(tls_size, tls_ofld->k_ctx.frag_size);
1472                 iv_len = pdus * CIPHER_BLOCK_SIZE;
1473
1474                 if (sbavail(sb) < tls_ofld->sb_off + plen) {
1475                         /*
1476                          * The full TLS record is not yet queued, stop
1477                          * for now until more data is added to the
1478                          * socket buffer.  However, if the connection
1479                          * has been closed, we will never get the rest
1480                          * of the record so just discard the partial
1481                          * record and close the connection.
1482                          */
1483 #ifdef VERBOSE_TRACES
1484                         CTR6(KTR_CXGBE,
1485                             "%s: tid %d sbavail %d sb_off %d plen %d%s",
1486                             __func__, toep->tid, sbavail(sb), tls_ofld->sb_off,
1487                             plen, toep->flags & TPF_SEND_FIN ? "" :
1488                             " SEND_FIN");
1489 #endif
1490                         if (sowwakeup)
1491                                 sowwakeup_locked(so);
1492                         else
1493                                 SOCKBUF_UNLOCK(sb);
1494                         SOCKBUF_UNLOCK_ASSERT(sb);
1495                         if (toep->flags & TPF_SEND_FIN)
1496                                 t4_close_conn(sc, toep);
1497                         return;
1498                 }
1499
1500                 /* Shove if there is no additional data pending. */
1501                 shove = (sbavail(sb) == tls_ofld->sb_off + plen) &&
1502                     !(tp->t_flags & TF_MORETOCOME);
1503
1504                 if (sb->sb_flags & SB_AUTOSIZE &&
1505                     V_tcp_do_autosndbuf &&
1506                     sb->sb_hiwat < V_tcp_autosndbuf_max &&
1507                     sbused(sb) >= sb->sb_hiwat * 7 / 8) {
1508                         int newsize = min(sb->sb_hiwat + V_tcp_autosndbuf_inc,
1509                             V_tcp_autosndbuf_max);
1510
1511                         if (!sbreserve_locked(sb, newsize, so, NULL))
1512                                 sb->sb_flags &= ~SB_AUTOSIZE;
1513                         else
1514                                 sowwakeup = 1;  /* room available */
1515                 }
1516                 if (sowwakeup)
1517                         sowwakeup_locked(so);
1518                 else
1519                         SOCKBUF_UNLOCK(sb);
1520                 SOCKBUF_UNLOCK_ASSERT(sb);
1521
1522                 if (__predict_false(toep->flags & TPF_FIN_SENT))
1523                         panic("%s: excess tx.", __func__);
1524
1525                 /* Determine whether to use immediate vs SGL. */
1526                 imm_payload = false;
1527                 imm_ivs = false;
1528                 if (wr_len + iv_len <= space) {
1529                         imm_ivs = true;
1530                         wr_len += iv_len;
1531                         if (wr_len + tls_size <= space) {
1532                                 wr_len += tls_size;
1533                                 imm_payload = true;
1534                         }
1535                 }
1536
1537                 /* Allocate space for IVs if needed. */
1538                 if (!imm_ivs) {
1539                         iv_buffer = malloc(iv_len, M_CXGBE, M_NOWAIT);
1540                         if (iv_buffer == NULL) {
1541                                 /*
1542                                  * XXX: How to restart this?
1543                                  */
1544                                 if (sowwakeup)
1545                                         sowwakeup_locked(so);
1546                                 else
1547                                         SOCKBUF_UNLOCK(sb);
1548                                 SOCKBUF_UNLOCK_ASSERT(sb);
1549                                 CTR3(KTR_CXGBE,
1550                             "%s: tid %d failed to alloc IV space len %d",
1551                                     __func__, toep->tid, iv_len);
1552                                 return;
1553                         }
1554                 } else
1555                         iv_buffer = NULL;
1556
1557                 /* Determine size of SGL. */
1558                 nsegs = 0;
1559                 max_nsegs_1mbuf = 0; /* max # of SGL segments in any one mbuf */
1560                 if (!imm_payload) {
1561                         nsegs = count_mbuf_segs(sndptr, sndptroff +
1562                             TLS_HEADER_LENGTH, tls_size, &max_nsegs_1mbuf);
1563                         if (!imm_ivs) {
1564                                 int n = sglist_count(iv_buffer, iv_len);
1565                                 nsegs += n;
1566                                 if (n > max_nsegs_1mbuf)
1567                                         max_nsegs_1mbuf = n;
1568                         }
1569
1570                         /* Account for SGL in work request length. */
1571                         wr_len += sizeof(struct ulptx_sgl) +
1572                             ((3 * (nsegs - 1)) / 2 + ((nsegs - 1) & 1)) * 8;
1573                 }
1574
1575                 wr = alloc_wrqe(roundup2(wr_len, 16), toep->ofld_txq);
1576                 if (wr == NULL) {
1577                         /* XXX: how will we recover from this? */
1578                         toep->flags |= TPF_TX_SUSPENDED;
1579                         return;
1580                 }
1581
1582 #ifdef VERBOSE_TRACES
1583                 CTR5(KTR_CXGBE, "%s: tid %d TLS record %d len %#x pdus %d",
1584                     __func__, toep->tid, thdr.type, tls_size, pdus);
1585 #endif
1586                 txwr = wrtod(wr);
1587                 cpl = (struct cpl_tx_tls_sfo *)(txwr + 1);
1588                 memset(txwr, 0, roundup2(wr_len, 16));
1589                 credits = howmany(wr_len, 16);
1590                 expn_size = tls_expansion_size(toep, tls_size, 0, NULL);
1591                 write_tlstx_wr(txwr, toep, imm_payload ? tls_size : 0,
1592                     tls_size, expn_size, pdus, credits, shove, imm_ivs ? 1 : 0);
1593                 write_tlstx_cpl(cpl, toep, &thdr, tls_size, pdus);
1594                 tls_copy_tx_key(toep, cpl + 1);
1595
1596                 /* Generate random IVs */
1597                 buf = (char *)(cpl + 1) + key_size(toep);
1598                 if (imm_ivs) {
1599                         MPASS(iv_buffer == NULL);
1600                         iv_dst = buf;
1601                         buf = (char *)iv_dst + iv_len;
1602                 } else
1603                         iv_dst = iv_buffer;
1604                 arc4rand(iv_dst, iv_len, 0);
1605
1606                 if (imm_payload) {
1607                         m_copydata(sndptr, sndptroff + TLS_HEADER_LENGTH,
1608                             tls_size, buf);
1609                 } else {
1610                         write_tlstx_sgl(buf, sndptr,
1611                             sndptroff + TLS_HEADER_LENGTH, tls_size, iv_buffer,
1612                             iv_len, nsegs, max_nsegs_1mbuf);
1613                 }
1614
1615                 KASSERT(toep->tx_credits >= credits,
1616                         ("%s: not enough credits", __func__));
1617
1618                 toep->tx_credits -= credits;
1619
1620                 tp->snd_nxt += plen;
1621                 tp->snd_max += plen;
1622
1623                 SOCKBUF_LOCK(sb);
1624                 sbsndptr_adv(sb, sb->sb_sndptr, plen);
1625                 tls_ofld->sb_off += plen;
1626                 SOCKBUF_UNLOCK(sb);
1627
1628                 toep->flags |= TPF_TX_DATA_SENT;
1629                 if (toep->tx_credits < MIN_OFLD_TLSTX_CREDITS(toep))
1630                         toep->flags |= TPF_TX_SUSPENDED;
1631
1632                 KASSERT(toep->txsd_avail > 0, ("%s: no txsd", __func__));
1633                 txsd->plen = plen;
1634                 txsd->tx_credits = credits;
1635                 txsd->iv_buffer = iv_buffer;
1636                 txsd++;
1637                 if (__predict_false(++toep->txsd_pidx == toep->txsd_total)) {
1638                         toep->txsd_pidx = 0;
1639                         txsd = &toep->txsd[0];
1640                 }
1641                 toep->txsd_avail--;
1642
1643                 atomic_add_long(&toep->vi->pi->tx_tls_records, 1);
1644                 atomic_add_long(&toep->vi->pi->tx_tls_octets, plen);
1645
1646                 t4_l2t_send(sc, wr, toep->l2te);
1647         }
1648 }
1649
1650 #ifdef KERN_TLS
1651 static int
1652 count_ext_pgs_segs(struct mbuf_ext_pgs *ext_pgs)
1653 {
1654         vm_paddr_t nextpa;
1655         u_int i, nsegs;
1656
1657         MPASS(ext_pgs->npgs > 0);
1658         nsegs = 1;
1659         nextpa = ext_pgs->pa[0] + PAGE_SIZE;
1660         for (i = 1; i < ext_pgs->npgs; i++) {
1661                 if (nextpa != ext_pgs->pa[i])
1662                         nsegs++;
1663                 nextpa = ext_pgs->pa[i] + PAGE_SIZE;
1664         }
1665         return (nsegs);
1666 }
1667
1668 static void
1669 write_ktlstx_sgl(void *dst, struct mbuf_ext_pgs *ext_pgs, int nsegs)
1670 {
1671         struct ulptx_sgl *usgl = dst;
1672         vm_paddr_t pa;
1673         uint32_t len;
1674         int i, j;
1675
1676         KASSERT(nsegs > 0, ("%s: nsegs 0", __func__));
1677
1678         usgl->cmd_nsge = htobe32(V_ULPTX_CMD(ULP_TX_SC_DSGL) |
1679             V_ULPTX_NSGE(nsegs));
1680
1681         /* Figure out the first S/G length. */
1682         pa = ext_pgs->pa[0] + ext_pgs->first_pg_off;
1683         usgl->addr0 = htobe64(pa);
1684         len = mbuf_ext_pg_len(ext_pgs, 0, ext_pgs->first_pg_off);
1685         pa += len;
1686         for (i = 1; i < ext_pgs->npgs; i++) {
1687                 if (ext_pgs->pa[i] != pa)
1688                         break;
1689                 len += mbuf_ext_pg_len(ext_pgs, i, 0);
1690                 pa += mbuf_ext_pg_len(ext_pgs, i, 0);
1691         }
1692         usgl->len0 = htobe32(len);
1693 #ifdef INVARIANTS
1694         nsegs--;
1695 #endif
1696
1697         j = -1;
1698         for (; i < ext_pgs->npgs; i++) {
1699                 if (j == -1 || ext_pgs->pa[i] != pa) {
1700                         if (j >= 0)
1701                                 usgl->sge[j / 2].len[j & 1] = htobe32(len);
1702                         j++;
1703 #ifdef INVARIANTS
1704                         nsegs--;
1705 #endif
1706                         pa = ext_pgs->pa[i];
1707                         usgl->sge[j / 2].addr[j & 1] = htobe64(pa);
1708                         len = mbuf_ext_pg_len(ext_pgs, i, 0);
1709                         pa += len;
1710                 } else {
1711                         len += mbuf_ext_pg_len(ext_pgs, i, 0);
1712                         pa += mbuf_ext_pg_len(ext_pgs, i, 0);
1713                 }
1714         }
1715         if (j >= 0) {
1716                 usgl->sge[j / 2].len[j & 1] = htobe32(len);
1717
1718                 if ((j & 1) == 0)
1719                         usgl->sge[j / 2].len[1] = htobe32(0);
1720         }
1721         KASSERT(nsegs == 0, ("%s: nsegs %d, ext_pgs %p", __func__, nsegs,
1722             ext_pgs));
1723 }
1724
1725 /*
1726  * Similar to t4_push_frames() but handles sockets that contain TLS
1727  * record mbufs.  Unlike TLSOM, each mbuf is a complete TLS record and
1728  * corresponds to a single work request.
1729  */
1730 void
1731 t4_push_ktls(struct adapter *sc, struct toepcb *toep, int drop)
1732 {
1733         struct tls_hdr *thdr;
1734         struct fw_tlstx_data_wr *txwr;
1735         struct cpl_tx_tls_sfo *cpl;
1736         struct wrqe *wr;
1737         struct mbuf *m;
1738         u_int nsegs, credits, wr_len;
1739         u_int expn_size;
1740         struct inpcb *inp = toep->inp;
1741         struct tcpcb *tp = intotcpcb(inp);
1742         struct socket *so = inp->inp_socket;
1743         struct sockbuf *sb = &so->so_snd;
1744         int tls_size, tx_credits, shove, sowwakeup;
1745         struct ofld_tx_sdesc *txsd;
1746         char *buf;
1747
1748         INP_WLOCK_ASSERT(inp);
1749         KASSERT(toep->flags & TPF_FLOWC_WR_SENT,
1750             ("%s: flowc_wr not sent for tid %u.", __func__, toep->tid));
1751
1752         KASSERT(ulp_mode(toep) == ULP_MODE_NONE ||
1753             ulp_mode(toep) == ULP_MODE_TCPDDP,
1754             ("%s: ulp_mode %u for toep %p", __func__, ulp_mode(toep), toep));
1755         KASSERT(tls_tx_key(toep),
1756             ("%s: TX key not set for toep %p", __func__, toep));
1757
1758 #ifdef VERBOSE_TRACES
1759         CTR4(KTR_CXGBE, "%s: tid %d toep flags %#x tp flags %#x drop %d",
1760             __func__, toep->tid, toep->flags, tp->t_flags);
1761 #endif
1762         if (__predict_false(toep->flags & TPF_ABORT_SHUTDOWN))
1763                 return;
1764
1765 #ifdef RATELIMIT
1766         if (__predict_false(inp->inp_flags2 & INP_RATE_LIMIT_CHANGED) &&
1767             (update_tx_rate_limit(sc, toep, so->so_max_pacing_rate) == 0)) {
1768                 inp->inp_flags2 &= ~INP_RATE_LIMIT_CHANGED;
1769         }
1770 #endif
1771
1772         /*
1773          * This function doesn't resume by itself.  Someone else must clear the
1774          * flag and call this function.
1775          */
1776         if (__predict_false(toep->flags & TPF_TX_SUSPENDED)) {
1777                 KASSERT(drop == 0,
1778                     ("%s: drop (%d) != 0 but tx is suspended", __func__, drop));
1779                 return;
1780         }
1781
1782         txsd = &toep->txsd[toep->txsd_pidx];
1783         for (;;) {
1784                 tx_credits = min(toep->tx_credits, MAX_OFLD_TX_CREDITS);
1785
1786                 SOCKBUF_LOCK(sb);
1787                 sowwakeup = drop;
1788                 if (drop) {
1789                         sbdrop_locked(sb, drop);
1790                         drop = 0;
1791                 }
1792
1793                 m = sb->sb_sndptr != NULL ? sb->sb_sndptr->m_next : sb->sb_mb;
1794
1795                 /*
1796                  * Send a FIN if requested, but only if there's no
1797                  * more data to send.
1798                  */
1799                 if (m == NULL && toep->flags & TPF_SEND_FIN) {
1800                         if (sowwakeup)
1801                                 sowwakeup_locked(so);
1802                         else
1803                                 SOCKBUF_UNLOCK(sb);
1804                         SOCKBUF_UNLOCK_ASSERT(sb);
1805                         t4_close_conn(sc, toep);
1806                         return;
1807                 }
1808
1809                 /*
1810                  * If there is no ready data to send, wait until more
1811                  * data arrives.
1812                  */
1813                 if (m == NULL || (m->m_flags & M_NOTAVAIL) != 0) {
1814                         if (sowwakeup)
1815                                 sowwakeup_locked(so);
1816                         else
1817                                 SOCKBUF_UNLOCK(sb);
1818                         SOCKBUF_UNLOCK_ASSERT(sb);
1819 #ifdef VERBOSE_TRACES
1820                         CTR2(KTR_CXGBE, "%s: tid %d no ready data to send",
1821                             __func__, toep->tid);
1822 #endif
1823                         return;
1824                 }
1825
1826                 KASSERT(m->m_flags & M_NOMAP, ("%s: mbuf %p is not NOMAP",
1827                     __func__, m));
1828                 KASSERT(m->m_ext.ext_pgs->tls != NULL,
1829                     ("%s: mbuf %p doesn't have TLS session", __func__, m));
1830
1831                 /* Calculate WR length. */
1832                 wr_len = sizeof(struct fw_tlstx_data_wr) +
1833                     sizeof(struct cpl_tx_tls_sfo) + key_size(toep);
1834
1835                 /* Explicit IVs for AES-CBC and AES-GCM are <= 16. */
1836                 MPASS(toep->tls.iv_len <= AES_BLOCK_LEN);
1837                 wr_len += AES_BLOCK_LEN;
1838
1839                 /* Account for SGL in work request length. */
1840                 nsegs = count_ext_pgs_segs(m->m_ext.ext_pgs);
1841                 wr_len += sizeof(struct ulptx_sgl) +
1842                     ((3 * (nsegs - 1)) / 2 + ((nsegs - 1) & 1)) * 8;
1843
1844                 /* Not enough credits for this work request. */
1845                 if (howmany(wr_len, 16) > tx_credits) {
1846                         if (sowwakeup)
1847                                 sowwakeup_locked(so);
1848                         else
1849                                 SOCKBUF_UNLOCK(sb);
1850                         SOCKBUF_UNLOCK_ASSERT(sb);
1851 #ifdef VERBOSE_TRACES
1852                         CTR5(KTR_CXGBE,
1853             "%s: tid %d mbuf %p requires %d credits, but only %d available",
1854                             __func__, toep->tid, m, howmany(wr_len, 16),
1855                             tx_credits);
1856 #endif
1857                         toep->flags |= TPF_TX_SUSPENDED;
1858                         return;
1859                 }
1860         
1861                 /* Shove if there is no additional data pending. */
1862                 shove = ((m->m_next == NULL ||
1863                     (m->m_next->m_flags & M_NOTAVAIL) != 0)) &&
1864                     (tp->t_flags & TF_MORETOCOME) == 0;
1865
1866                 if (sb->sb_flags & SB_AUTOSIZE &&
1867                     V_tcp_do_autosndbuf &&
1868                     sb->sb_hiwat < V_tcp_autosndbuf_max &&
1869                     sbused(sb) >= sb->sb_hiwat * 7 / 8) {
1870                         int newsize = min(sb->sb_hiwat + V_tcp_autosndbuf_inc,
1871                             V_tcp_autosndbuf_max);
1872
1873                         if (!sbreserve_locked(sb, newsize, so, NULL))
1874                                 sb->sb_flags &= ~SB_AUTOSIZE;
1875                         else
1876                                 sowwakeup = 1;  /* room available */
1877                 }
1878                 if (sowwakeup)
1879                         sowwakeup_locked(so);
1880                 else
1881                         SOCKBUF_UNLOCK(sb);
1882                 SOCKBUF_UNLOCK_ASSERT(sb);
1883
1884                 if (__predict_false(toep->flags & TPF_FIN_SENT))
1885                         panic("%s: excess tx.", __func__);
1886
1887                 wr = alloc_wrqe(roundup2(wr_len, 16), toep->ofld_txq);
1888                 if (wr == NULL) {
1889                         /* XXX: how will we recover from this? */
1890                         toep->flags |= TPF_TX_SUSPENDED;
1891                         return;
1892                 }
1893
1894                 thdr = (struct tls_hdr *)m->m_ext.ext_pgs->hdr;
1895 #ifdef VERBOSE_TRACES
1896                 CTR5(KTR_CXGBE, "%s: tid %d TLS record %ju type %d len %#x",
1897                     __func__, toep->tid, m->m_ext.ext_pgs->seqno, thdr->type,
1898                     m->m_len);
1899 #endif
1900                 txwr = wrtod(wr);
1901                 cpl = (struct cpl_tx_tls_sfo *)(txwr + 1);
1902                 memset(txwr, 0, roundup2(wr_len, 16));
1903                 credits = howmany(wr_len, 16);
1904                 expn_size = m->m_ext.ext_pgs->hdr_len +
1905                     m->m_ext.ext_pgs->trail_len;
1906                 tls_size = m->m_len - expn_size;
1907                 write_tlstx_wr(txwr, toep, 0,
1908                     tls_size, expn_size, 1, credits, shove, 1);
1909                 toep->tls.tx_seq_no = m->m_ext.ext_pgs->seqno;
1910                 write_tlstx_cpl(cpl, toep, thdr, tls_size, 1);
1911                 tls_copy_tx_key(toep, cpl + 1);
1912
1913                 /* Copy IV. */
1914                 buf = (char *)(cpl + 1) + key_size(toep);
1915                 memcpy(buf, thdr + 1, toep->tls.iv_len);
1916                 buf += AES_BLOCK_LEN;
1917
1918                 write_ktlstx_sgl(buf, m->m_ext.ext_pgs, nsegs);
1919
1920                 KASSERT(toep->tx_credits >= credits,
1921                         ("%s: not enough credits", __func__));
1922
1923                 toep->tx_credits -= credits;
1924
1925                 tp->snd_nxt += m->m_len;
1926                 tp->snd_max += m->m_len;
1927
1928                 SOCKBUF_LOCK(sb);
1929                 sb->sb_sndptr = m;
1930                 SOCKBUF_UNLOCK(sb);
1931
1932                 toep->flags |= TPF_TX_DATA_SENT;
1933                 if (toep->tx_credits < MIN_OFLD_TLSTX_CREDITS(toep))
1934                         toep->flags |= TPF_TX_SUSPENDED;
1935
1936                 KASSERT(toep->txsd_avail > 0, ("%s: no txsd", __func__));
1937                 txsd->plen = m->m_len;
1938                 txsd->tx_credits = credits;
1939                 txsd++;
1940                 if (__predict_false(++toep->txsd_pidx == toep->txsd_total)) {
1941                         toep->txsd_pidx = 0;
1942                         txsd = &toep->txsd[0];
1943                 }
1944                 toep->txsd_avail--;
1945
1946                 atomic_add_long(&toep->vi->pi->tx_tls_records, 1);
1947                 atomic_add_long(&toep->vi->pi->tx_tls_octets, m->m_len);
1948
1949                 t4_l2t_send(sc, wr, toep->l2te);
1950         }
1951 }
1952 #endif
1953
1954 /*
1955  * For TLS data we place received mbufs received via CPL_TLS_DATA into
1956  * an mbufq in the TLS offload state.  When CPL_RX_TLS_CMP is
1957  * received, the completed PDUs are placed into the socket receive
1958  * buffer.
1959  *
1960  * The TLS code reuses the ulp_pdu_reclaimq to hold the pending mbufs.
1961  */
1962 static int
1963 do_tls_data(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
1964 {
1965         struct adapter *sc = iq->adapter;
1966         const struct cpl_tls_data *cpl = mtod(m, const void *);
1967         unsigned int tid = GET_TID(cpl);
1968         struct toepcb *toep = lookup_tid(sc, tid);
1969         struct inpcb *inp = toep->inp;
1970         struct tcpcb *tp;
1971         int len;
1972
1973         /* XXX: Should this match do_rx_data instead? */
1974         KASSERT(!(toep->flags & TPF_SYNQE),
1975             ("%s: toep %p claims to be a synq entry", __func__, toep));
1976
1977         KASSERT(toep->tid == tid, ("%s: toep tid/atid mismatch", __func__));
1978
1979         /* strip off CPL header */
1980         m_adj(m, sizeof(*cpl));
1981         len = m->m_pkthdr.len;
1982
1983         atomic_add_long(&toep->vi->pi->rx_tls_octets, len);
1984
1985         KASSERT(len == G_CPL_TLS_DATA_LENGTH(be32toh(cpl->length_pkd)),
1986             ("%s: payload length mismatch", __func__));
1987
1988         INP_WLOCK(inp);
1989         if (inp->inp_flags & (INP_DROPPED | INP_TIMEWAIT)) {
1990                 CTR4(KTR_CXGBE, "%s: tid %u, rx (%d bytes), inp_flags 0x%x",
1991                     __func__, tid, len, inp->inp_flags);
1992                 INP_WUNLOCK(inp);
1993                 m_freem(m);
1994                 return (0);
1995         }
1996
1997         /* Save TCP sequence number. */
1998         m->m_pkthdr.tls_tcp_seq = be32toh(cpl->seq);
1999
2000         if (mbufq_enqueue(&toep->ulp_pdu_reclaimq, m)) {
2001 #ifdef INVARIANTS
2002                 panic("Failed to queue TLS data packet");
2003 #else
2004                 printf("%s: Failed to queue TLS data packet\n", __func__);
2005                 INP_WUNLOCK(inp);
2006                 m_freem(m);
2007                 return (0);
2008 #endif
2009         }
2010
2011         tp = intotcpcb(inp);
2012         tp->t_rcvtime = ticks;
2013
2014 #ifdef VERBOSE_TRACES
2015         CTR4(KTR_CXGBE, "%s: tid %u len %d seq %u", __func__, tid, len,
2016             be32toh(cpl->seq));
2017 #endif
2018
2019         INP_WUNLOCK(inp);
2020         return (0);
2021 }
2022
2023 static int
2024 do_rx_tls_cmp(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
2025 {
2026         struct adapter *sc = iq->adapter;
2027         const struct cpl_rx_tls_cmp *cpl = mtod(m, const void *);
2028         struct tlsrx_hdr_pkt *tls_hdr_pkt;
2029         unsigned int tid = GET_TID(cpl);
2030         struct toepcb *toep = lookup_tid(sc, tid);
2031         struct inpcb *inp = toep->inp;
2032         struct tcpcb *tp;
2033         struct socket *so;
2034         struct sockbuf *sb;
2035         struct mbuf *tls_data;
2036         int len, pdu_length, rx_credits;
2037
2038         KASSERT(toep->tid == tid, ("%s: toep tid/atid mismatch", __func__));
2039         KASSERT(!(toep->flags & TPF_SYNQE),
2040             ("%s: toep %p claims to be a synq entry", __func__, toep));
2041
2042         /* strip off CPL header */
2043         m_adj(m, sizeof(*cpl));
2044         len = m->m_pkthdr.len;
2045
2046         atomic_add_long(&toep->vi->pi->rx_tls_records, 1);
2047
2048         KASSERT(len == G_CPL_RX_TLS_CMP_LENGTH(be32toh(cpl->pdulength_length)),
2049             ("%s: payload length mismatch", __func__));
2050
2051         INP_WLOCK(inp);
2052         if (inp->inp_flags & (INP_DROPPED | INP_TIMEWAIT)) {
2053                 CTR4(KTR_CXGBE, "%s: tid %u, rx (%d bytes), inp_flags 0x%x",
2054                     __func__, tid, len, inp->inp_flags);
2055                 INP_WUNLOCK(inp);
2056                 m_freem(m);
2057                 return (0);
2058         }
2059
2060         pdu_length = G_CPL_RX_TLS_CMP_PDULENGTH(be32toh(cpl->pdulength_length));
2061
2062         tp = intotcpcb(inp);
2063
2064 #ifdef VERBOSE_TRACES
2065         CTR6(KTR_CXGBE, "%s: tid %u PDU len %d len %d seq %u, rcv_nxt %u",
2066             __func__, tid, pdu_length, len, be32toh(cpl->seq), tp->rcv_nxt);
2067 #endif
2068
2069         tp->rcv_nxt += pdu_length;
2070         if (tp->rcv_wnd < pdu_length) {
2071                 toep->tls.rcv_over += pdu_length - tp->rcv_wnd;
2072                 tp->rcv_wnd = 0;
2073         } else
2074                 tp->rcv_wnd -= pdu_length;
2075
2076         /* XXX: Not sure what to do about urgent data. */
2077
2078         /*
2079          * The payload of this CPL is the TLS header followed by
2080          * additional fields.
2081          */
2082         KASSERT(m->m_len >= sizeof(*tls_hdr_pkt),
2083             ("%s: payload too small", __func__));
2084         tls_hdr_pkt = mtod(m, void *);
2085
2086         /*
2087          * Only the TLS header is sent to OpenSSL, so report errors by
2088          * altering the record type.
2089          */
2090         if ((tls_hdr_pkt->res_to_mac_error & M_TLSRX_HDR_PKT_ERROR) != 0)
2091                 tls_hdr_pkt->type = CONTENT_TYPE_ERROR;
2092
2093         /* Trim this CPL's mbuf to only include the TLS header. */
2094         KASSERT(m->m_len == len && m->m_next == NULL,
2095             ("%s: CPL spans multiple mbufs", __func__));
2096         m->m_len = TLS_HEADER_LENGTH;
2097         m->m_pkthdr.len = TLS_HEADER_LENGTH;
2098
2099         tls_data = mbufq_dequeue(&toep->ulp_pdu_reclaimq);
2100         if (tls_data != NULL) {
2101                 KASSERT(be32toh(cpl->seq) == tls_data->m_pkthdr.tls_tcp_seq,
2102                     ("%s: sequence mismatch", __func__));
2103
2104                 /*
2105                  * Update the TLS header length to be the length of
2106                  * the payload data.
2107                  */
2108                 tls_hdr_pkt->length = htobe16(tls_data->m_pkthdr.len);
2109
2110                 m->m_next = tls_data;
2111                 m->m_pkthdr.len += tls_data->m_len;
2112         }
2113
2114         so = inp_inpcbtosocket(inp);
2115         sb = &so->so_rcv;
2116         SOCKBUF_LOCK(sb);
2117
2118         if (__predict_false(sb->sb_state & SBS_CANTRCVMORE)) {
2119                 struct epoch_tracker et;
2120
2121                 CTR3(KTR_CXGBE, "%s: tid %u, excess rx (%d bytes)",
2122                     __func__, tid, pdu_length);
2123                 m_freem(m);
2124                 SOCKBUF_UNLOCK(sb);
2125                 INP_WUNLOCK(inp);
2126
2127                 CURVNET_SET(toep->vnet);
2128                 INP_INFO_RLOCK_ET(&V_tcbinfo, et);
2129                 INP_WLOCK(inp);
2130                 tp = tcp_drop(tp, ECONNRESET);
2131                 if (tp)
2132                         INP_WUNLOCK(inp);
2133                 INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
2134                 CURVNET_RESTORE();
2135
2136                 return (0);
2137         }
2138
2139         /*
2140          * Not all of the bytes on the wire are included in the socket buffer
2141          * (e.g. the MAC of the TLS record).  However, those bytes are included
2142          * in the TCP sequence space.
2143          */
2144
2145         /* receive buffer autosize */
2146         MPASS(toep->vnet == so->so_vnet);
2147         CURVNET_SET(toep->vnet);
2148         if (sb->sb_flags & SB_AUTOSIZE &&
2149             V_tcp_do_autorcvbuf &&
2150             sb->sb_hiwat < V_tcp_autorcvbuf_max &&
2151             m->m_pkthdr.len > (sbspace(sb) / 8 * 7)) {
2152                 unsigned int hiwat = sb->sb_hiwat;
2153                 unsigned int newsize = min(hiwat + sc->tt.autorcvbuf_inc,
2154                     V_tcp_autorcvbuf_max);
2155
2156                 if (!sbreserve_locked(sb, newsize, so, NULL))
2157                         sb->sb_flags &= ~SB_AUTOSIZE;
2158         }
2159
2160         sbappendstream_locked(sb, m, 0);
2161         rx_credits = sbspace(sb) > tp->rcv_wnd ? sbspace(sb) - tp->rcv_wnd : 0;
2162 #ifdef VERBOSE_TRACES
2163         CTR4(KTR_CXGBE, "%s: tid %u rx_credits %u rcv_wnd %u",
2164             __func__, tid, rx_credits, tp->rcv_wnd);
2165 #endif
2166         if (rx_credits > 0 && sbused(sb) + tp->rcv_wnd < sb->sb_lowat) {
2167                 rx_credits = send_rx_credits(sc, toep, rx_credits);
2168                 tp->rcv_wnd += rx_credits;
2169                 tp->rcv_adv += rx_credits;
2170         }
2171
2172         sorwakeup_locked(so);
2173         SOCKBUF_UNLOCK_ASSERT(sb);
2174
2175         INP_WUNLOCK(inp);
2176         CURVNET_RESTORE();
2177         return (0);
2178 }
2179
2180 void
2181 t4_tls_mod_load(void)
2182 {
2183
2184         mtx_init(&tls_handshake_lock, "t4tls handshake", NULL, MTX_DEF);
2185         t4_register_cpl_handler(CPL_TLS_DATA, do_tls_data);
2186         t4_register_cpl_handler(CPL_RX_TLS_CMP, do_rx_tls_cmp);
2187 }
2188
2189 void
2190 t4_tls_mod_unload(void)
2191 {
2192
2193         t4_register_cpl_handler(CPL_TLS_DATA, NULL);
2194         t4_register_cpl_handler(CPL_RX_TLS_CMP, NULL);
2195         mtx_destroy(&tls_handshake_lock);
2196 }
2197 #endif  /* TCP_OFFLOAD */