xref: /freebsd/sys/dev/mlx5/mlx5_en/mlx5_en_hw_tls.c (revision 1f474190)
1 /*-
2  * Copyright (c) 2019 Mellanox Technologies. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS `AS IS' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  * $FreeBSD$
26  */
27 
28 #include "opt_kern_tls.h"
29 
30 #include "en.h"
31 
32 #include <dev/mlx5/tls.h>
33 
34 #include <linux/delay.h>
35 #include <sys/ktls.h>
36 #include <opencrypto/cryptodev.h>
37 
38 #ifdef KERN_TLS
39 
40 MALLOC_DEFINE(M_MLX5E_TLS, "MLX5E_TLS", "MLX5 ethernet HW TLS");
41 
42 /* software TLS context */
43 struct mlx5_ifc_sw_tls_cntx_bits {
44 	struct mlx5_ifc_tls_static_params_bits param;
45 	struct mlx5_ifc_tls_progress_params_bits progress;
46 	struct {
47 		uint8_t key_data[8][0x20];
48 		uint8_t key_len[0x20];
49 	} key;
50 };
51 
52 CTASSERT(MLX5_ST_SZ_BYTES(sw_tls_cntx) <= sizeof(((struct mlx5e_tls_tag *)0)->crypto_params));
53 CTASSERT(MLX5_ST_SZ_BYTES(mkc) == sizeof(((struct mlx5e_tx_umr_wqe *)0)->mkc));
54 
55 static const char *mlx5e_tls_stats_desc[] = {
56 	MLX5E_TLS_STATS(MLX5E_STATS_DESC)
57 };
58 
59 static void mlx5e_tls_work(struct work_struct *);
60 
61 static int
62 mlx5e_tls_tag_zinit(void *mem, int size, int flags)
63 {
64 	struct mlx5e_tls_tag *ptag = mem;
65 
66 	MPASS(size == sizeof(*ptag));
67 
68 	memset(ptag, 0, sizeof(*ptag));
69 	mtx_init(&ptag->mtx, "mlx5-tls-tag-mtx", NULL, MTX_DEF);
70 	INIT_WORK(&ptag->work, mlx5e_tls_work);
71 
72 	return (0);
73 }
74 
75 static void
76 mlx5e_tls_tag_zfini(void *mem, int size)
77 {
78 	struct mlx5e_tls_tag *ptag = mem;
79 	struct mlx5e_priv *priv;
80 	struct mlx5e_tls *ptls;
81 
82 	ptls = ptag->tls;
83 	priv = container_of(ptls, struct mlx5e_priv, tls);
84 
85 	flush_work(&ptag->work);
86 
87 	if (ptag->tisn != 0) {
88 		mlx5_tls_close_tis(priv->mdev, ptag->tisn);
89 		atomic_add_32(&ptls->num_resources, -1U);
90 	}
91 
92 	mtx_destroy(&ptag->mtx);
93 }
94 
95 static void
96 mlx5e_tls_tag_zfree(struct mlx5e_tls_tag *ptag)
97 {
98 
99 	/* reset some variables */
100 	ptag->state = MLX5E_TLS_ST_INIT;
101 	ptag->dek_index = 0;
102 	ptag->dek_index_ok = 0;
103 
104 	/* avoid leaking keys */
105 	memset(ptag->crypto_params, 0, sizeof(ptag->crypto_params));
106 
107 	/* update number of TIS contexts */
108 	if (ptag->tisn == 0)
109 		atomic_add_32(&ptag->tls->num_resources, -1U);
110 
111 	/* return tag to UMA */
112 	uma_zfree(ptag->tls->zone, ptag);
113 }
114 
115 int
116 mlx5e_tls_init(struct mlx5e_priv *priv)
117 {
118 	struct mlx5e_tls *ptls = &priv->tls;
119 	struct sysctl_oid *node;
120 	uint32_t x;
121 
122 	if (MLX5_CAP_GEN(priv->mdev, tls_tx) == 0)
123 		return (0);
124 
125 	ptls->wq = create_singlethread_workqueue("mlx5-tls-wq");
126 	if (ptls->wq == NULL)
127 		return (ENOMEM);
128 
129 	sysctl_ctx_init(&ptls->ctx);
130 
131 	snprintf(ptls->zname, sizeof(ptls->zname),
132 	    "mlx5_%u_tls", device_get_unit(priv->mdev->pdev->dev.bsddev));
133 
134 	ptls->zone = uma_zcreate(ptls->zname, sizeof(struct mlx5e_tls_tag),
135 	    NULL, NULL, mlx5e_tls_tag_zinit, mlx5e_tls_tag_zfini, UMA_ALIGN_CACHE, 0);
136 
137 	ptls->max_resources = 1U << MLX5_CAP_GEN(priv->mdev, log_max_dek);
138 
139 	for (x = 0; x != MLX5E_TLS_STATS_NUM; x++)
140 		ptls->stats.arg[x] = counter_u64_alloc(M_WAITOK);
141 
142 	ptls->init = 1;
143 
144 	node = SYSCTL_ADD_NODE(&priv->sysctl_ctx,
145 	    SYSCTL_CHILDREN(priv->sysctl_ifnet), OID_AUTO,
146 	    "tls", CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, "Hardware TLS offload");
147 	if (node == NULL)
148 		return (0);
149 
150 	mlx5e_create_counter_stats(&ptls->ctx,
151 	    SYSCTL_CHILDREN(node), "stats",
152 	    mlx5e_tls_stats_desc, MLX5E_TLS_STATS_NUM,
153 	    ptls->stats.arg);
154 
155 	return (0);
156 }
157 
158 void
159 mlx5e_tls_cleanup(struct mlx5e_priv *priv)
160 {
161 	struct mlx5e_tls *ptls = &priv->tls;
162 	uint32_t x;
163 
164 	if (MLX5_CAP_GEN(priv->mdev, tls_tx) == 0)
165 		return;
166 
167 	ptls->init = 0;
168 	flush_workqueue(ptls->wq);
169 	sysctl_ctx_free(&ptls->ctx);
170 	uma_zdestroy(ptls->zone);
171 	destroy_workqueue(ptls->wq);
172 
173 	/* check if all resources are freed */
174 	MPASS(priv->tls.num_resources == 0);
175 
176 	for (x = 0; x != MLX5E_TLS_STATS_NUM; x++)
177 		counter_u64_free(ptls->stats.arg[x]);
178 }
179 
180 static void
181 mlx5e_tls_work(struct work_struct *work)
182 {
183 	struct mlx5e_tls_tag *ptag;
184 	struct mlx5e_priv *priv;
185 	int err;
186 
187 	ptag = container_of(work, struct mlx5e_tls_tag, work);
188 	priv = container_of(ptag->tls, struct mlx5e_priv, tls);
189 
190 	switch (ptag->state) {
191 	case MLX5E_TLS_ST_INIT:
192 		/* try to open TIS, if not present */
193 		if (ptag->tisn == 0) {
194 			err = mlx5_tls_open_tis(priv->mdev, 0, priv->tdn,
195 			    priv->pdn, &ptag->tisn);
196 			if (err) {
197 				MLX5E_TLS_STAT_INC(ptag, tx_error, 1);
198 				break;
199 			}
200 		}
201 		MLX5_SET(sw_tls_cntx, ptag->crypto_params, progress.pd, ptag->tisn);
202 
203 		/* try to allocate a DEK context ID */
204 		err = mlx5_encryption_key_create(priv->mdev, priv->pdn,
205 		    MLX5_ADDR_OF(sw_tls_cntx, ptag->crypto_params, key.key_data),
206 		    MLX5_GET(sw_tls_cntx, ptag->crypto_params, key.key_len),
207 		    &ptag->dek_index);
208 		if (err) {
209 			MLX5E_TLS_STAT_INC(ptag, tx_error, 1);
210 			break;
211 		}
212 
213 		MLX5_SET(sw_tls_cntx, ptag->crypto_params, param.dek_index, ptag->dek_index);
214 
215 		ptag->dek_index_ok = 1;
216 
217 		MLX5E_TLS_TAG_LOCK(ptag);
218 		if (ptag->state == MLX5E_TLS_ST_INIT)
219 			ptag->state = MLX5E_TLS_ST_SETUP;
220 		MLX5E_TLS_TAG_UNLOCK(ptag);
221 		break;
222 
223 	case MLX5E_TLS_ST_FREED:
224 		/* wait for all refs to go away */
225 		while (ptag->refs != 0)
226 			msleep(1);
227 
228 		/* try to destroy DEK context by ID */
229 		if (ptag->dek_index_ok)
230 			err = mlx5_encryption_key_destroy(priv->mdev, ptag->dek_index);
231 
232 		/* free tag */
233 		mlx5e_tls_tag_zfree(ptag);
234 		break;
235 
236 	default:
237 		break;
238 	}
239 }
240 
241 static int
242 mlx5e_tls_set_params(void *ctx, const struct tls_session_params *en)
243 {
244 
245 	MLX5_SET(sw_tls_cntx, ctx, param.const_2, 2);
246 	if (en->tls_vminor == TLS_MINOR_VER_TWO)
247 		MLX5_SET(sw_tls_cntx, ctx, param.tls_version, 2); /* v1.2 */
248 	else
249 		MLX5_SET(sw_tls_cntx, ctx, param.tls_version, 3); /* v1.3 */
250 	MLX5_SET(sw_tls_cntx, ctx, param.const_1, 1);
251 	MLX5_SET(sw_tls_cntx, ctx, param.encryption_standard, 1); /* TLS */
252 
253 	/* copy the initial vector in place */
254 	switch (en->iv_len) {
255 	case MLX5_FLD_SZ_BYTES(sw_tls_cntx, param.gcm_iv):
256 	case MLX5_FLD_SZ_BYTES(sw_tls_cntx, param.gcm_iv) +
257 	     MLX5_FLD_SZ_BYTES(sw_tls_cntx, param.implicit_iv):
258 		memcpy(MLX5_ADDR_OF(sw_tls_cntx, ctx, param.gcm_iv),
259 		    en->iv, en->iv_len);
260 		break;
261 	default:
262 		return (EINVAL);
263 	}
264 
265 	if (en->cipher_key_len <= MLX5_FLD_SZ_BYTES(sw_tls_cntx, key.key_data)) {
266 		memcpy(MLX5_ADDR_OF(sw_tls_cntx, ctx, key.key_data),
267 		    en->cipher_key, en->cipher_key_len);
268 		MLX5_SET(sw_tls_cntx, ctx, key.key_len, en->cipher_key_len);
269 	} else {
270 		return (EINVAL);
271 	}
272 	return (0);
273 }
274 
275 /* Verify zero default */
276 CTASSERT(MLX5E_TLS_ST_INIT == 0);
277 
278 int
279 mlx5e_tls_snd_tag_alloc(struct ifnet *ifp,
280     union if_snd_tag_alloc_params *params,
281     struct m_snd_tag **ppmt)
282 {
283 	struct if_snd_tag_alloc_rate_limit rl_params;
284 	struct mlx5e_priv *priv;
285 	struct mlx5e_tls_tag *ptag;
286 	const struct tls_session_params *en;
287 	int error;
288 
289 	priv = ifp->if_softc;
290 
291 	if (priv->tls.init == 0)
292 		return (EOPNOTSUPP);
293 
294 	/* allocate new tag from zone, if any */
295 	ptag = uma_zalloc(priv->tls.zone, M_NOWAIT);
296 	if (ptag == NULL)
297 		return (ENOMEM);
298 
299 	/* sanity check default values */
300 	MPASS(ptag->state == MLX5E_TLS_ST_INIT);
301 	MPASS(ptag->dek_index == 0);
302 	MPASS(ptag->dek_index_ok == 0);
303 
304 	/* setup TLS tag */
305 	ptag->tls = &priv->tls;
306 
307 	/* check if there is no TIS context */
308 	if (ptag->tisn == 0) {
309 		uint32_t value;
310 
311 		value = atomic_fetchadd_32(&priv->tls.num_resources, 1U);
312 
313 		/* check resource limits */
314 		if (value >= priv->tls.max_resources) {
315 			error = ENOMEM;
316 			goto failure;
317 		}
318 	}
319 
320 	en = &params->tls.tls->params;
321 
322 	/* only TLS v1.2 and v1.3 is currently supported */
323 	if (en->tls_vmajor != TLS_MAJOR_VER_ONE ||
324 	    (en->tls_vminor != TLS_MINOR_VER_TWO
325 #ifdef TLS_MINOR_VER_THREE
326 	     && en->tls_vminor != TLS_MINOR_VER_THREE
327 #endif
328 	     )) {
329 		error = EPROTONOSUPPORT;
330 		goto failure;
331 	}
332 
333 	switch (en->cipher_algorithm) {
334 	case CRYPTO_AES_NIST_GCM_16:
335 		switch (en->cipher_key_len) {
336 		case 128 / 8:
337 			if (en->tls_vminor == TLS_MINOR_VER_TWO) {
338 				if (MLX5_CAP_TLS(priv->mdev, tls_1_2_aes_gcm_128) == 0) {
339 					error = EPROTONOSUPPORT;
340 					goto failure;
341 				}
342 			} else {
343 				if (MLX5_CAP_TLS(priv->mdev, tls_1_3_aes_gcm_128) == 0) {
344 					error = EPROTONOSUPPORT;
345 					goto failure;
346 				}
347 			}
348 			error = mlx5e_tls_set_params(ptag->crypto_params, en);
349 			if (error)
350 				goto failure;
351 			break;
352 
353 		case 256 / 8:
354 			if (en->tls_vminor == TLS_MINOR_VER_TWO) {
355 				if (MLX5_CAP_TLS(priv->mdev, tls_1_2_aes_gcm_256) == 0) {
356 					error = EPROTONOSUPPORT;
357 					goto failure;
358 				}
359 			} else {
360 				if (MLX5_CAP_TLS(priv->mdev, tls_1_3_aes_gcm_256) == 0) {
361 					error = EPROTONOSUPPORT;
362 					goto failure;
363 				}
364 			}
365 			error = mlx5e_tls_set_params(ptag->crypto_params, en);
366 			if (error)
367 				goto failure;
368 			break;
369 
370 		default:
371 			error = EINVAL;
372 			goto failure;
373 		}
374 		break;
375 	default:
376 		error = EPROTONOSUPPORT;
377 		goto failure;
378 	}
379 
380 	switch (params->hdr.type) {
381 #if defined(RATELIMIT) && defined(IF_SND_TAG_TYPE_TLS_RATE_LIMIT)
382 	case IF_SND_TAG_TYPE_TLS_RATE_LIMIT:
383 		memset(&rl_params, 0, sizeof(rl_params));
384 		rl_params.hdr = params->tls_rate_limit.hdr;
385 		rl_params.hdr.type = IF_SND_TAG_TYPE_RATE_LIMIT;
386 		rl_params.max_rate = params->tls_rate_limit.max_rate;
387 
388 		error = mlx5e_rl_snd_tag_alloc(ifp,
389 		    container_of(&rl_params, union if_snd_tag_alloc_params, rate_limit),
390 		    &ptag->rl_tag);
391 		if (error)
392 			goto failure;
393 		break;
394 #endif
395 	case IF_SND_TAG_TYPE_TLS:
396 		memset(&rl_params, 0, sizeof(rl_params));
397 		rl_params.hdr = params->tls.hdr;
398 		rl_params.hdr.type = IF_SND_TAG_TYPE_UNLIMITED;
399 
400 		error = mlx5e_ul_snd_tag_alloc(ifp,
401 		    container_of(&rl_params, union if_snd_tag_alloc_params, unlimited),
402 		    &ptag->rl_tag);
403 		if (error)
404 			goto failure;
405 		break;
406 	default:
407 		error = EOPNOTSUPP;
408 		goto failure;
409 	}
410 
411 	/* store pointer to mbuf tag */
412 	MPASS(ptag->tag.refcount == 0);
413 	m_snd_tag_init(&ptag->tag, ifp, params->hdr.type);
414 	*ppmt = &ptag->tag;
415 
416 	queue_work(priv->tls.wq, &ptag->work);
417 	flush_work(&ptag->work);
418 
419 	return (0);
420 
421 failure:
422 	mlx5e_tls_tag_zfree(ptag);
423 	return (error);
424 }
425 
426 int
427 mlx5e_tls_snd_tag_modify(struct m_snd_tag *pmt, union if_snd_tag_modify_params *params)
428 {
429 #if defined(RATELIMIT) && defined(IF_SND_TAG_TYPE_TLS_RATE_LIMIT)
430 	struct if_snd_tag_rate_limit_params rl_params;
431 	struct mlx5e_tls_tag *ptag =
432 	    container_of(pmt, struct mlx5e_tls_tag, tag);
433 	int error;
434 #endif
435 
436 	switch (pmt->type) {
437 #if defined(RATELIMIT) && defined(IF_SND_TAG_TYPE_TLS_RATE_LIMIT)
438 	case IF_SND_TAG_TYPE_TLS_RATE_LIMIT:
439 		memset(&rl_params, 0, sizeof(rl_params));
440 		rl_params.max_rate = params->tls_rate_limit.max_rate;
441 		error = mlx5e_rl_snd_tag_modify(ptag->rl_tag,
442 		    container_of(&rl_params, union if_snd_tag_modify_params, rate_limit));
443 		return (error);
444 #endif
445 	default:
446 		return (EOPNOTSUPP);
447 	}
448 }
449 
450 int
451 mlx5e_tls_snd_tag_query(struct m_snd_tag *pmt, union if_snd_tag_query_params *params)
452 {
453 	struct mlx5e_tls_tag *ptag =
454 	    container_of(pmt, struct mlx5e_tls_tag, tag);
455 	int error;
456 
457 	switch (pmt->type) {
458 #if defined(RATELIMIT) && defined(IF_SND_TAG_TYPE_TLS_RATE_LIMIT)
459 	case IF_SND_TAG_TYPE_TLS_RATE_LIMIT:
460 		error = mlx5e_rl_snd_tag_query(ptag->rl_tag, params);
461 		break;
462 #endif
463 	case IF_SND_TAG_TYPE_TLS:
464 		error = mlx5e_ul_snd_tag_query(ptag->rl_tag, params);
465 		break;
466 	default:
467 		error = EOPNOTSUPP;
468 		break;
469 	}
470 	return (error);
471 }
472 
473 void
474 mlx5e_tls_snd_tag_free(struct m_snd_tag *pmt)
475 {
476 	struct mlx5e_tls_tag *ptag =
477 	    container_of(pmt, struct mlx5e_tls_tag, tag);
478 	struct mlx5e_priv *priv;
479 
480 	switch (pmt->type) {
481 #if defined(RATELIMIT) && defined(IF_SND_TAG_TYPE_TLS_RATE_LIMIT)
482 	case IF_SND_TAG_TYPE_TLS_RATE_LIMIT:
483 		mlx5e_rl_snd_tag_free(ptag->rl_tag);
484 		break;
485 #endif
486 	case IF_SND_TAG_TYPE_TLS:
487 		mlx5e_ul_snd_tag_free(ptag->rl_tag);
488 		break;
489 	default:
490 		break;
491 	}
492 
493 	MLX5E_TLS_TAG_LOCK(ptag);
494 	ptag->state = MLX5E_TLS_ST_FREED;
495 	MLX5E_TLS_TAG_UNLOCK(ptag);
496 
497 	priv = ptag->tag.ifp->if_softc;
498 	queue_work(priv->tls.wq, &ptag->work);
499 }
500 
501 CTASSERT((MLX5_FLD_SZ_BYTES(sw_tls_cntx, param) % 16) == 0);
502 
503 static void
504 mlx5e_tls_send_static_parameters(struct mlx5e_sq *sq, struct mlx5e_tls_tag *ptag)
505 {
506 	const u32 ds_cnt = DIV_ROUND_UP(sizeof(struct mlx5e_tx_umr_wqe) +
507 	    MLX5_FLD_SZ_BYTES(sw_tls_cntx, param), MLX5_SEND_WQE_DS);
508 	struct mlx5e_tx_umr_wqe *wqe;
509 	u16 pi;
510 
511 	pi = sq->pc & sq->wq.sz_m1;
512 	wqe = mlx5_wq_cyc_get_wqe(&sq->wq, pi);
513 
514 	memset(wqe, 0, sizeof(*wqe));
515 
516 	wqe->ctrl.opmod_idx_opcode = cpu_to_be32((sq->pc << 8) |
517 	    MLX5_OPCODE_UMR | (MLX5_OPCODE_MOD_UMR_TLS_TIS_STATIC_PARAMS << 24));
518 	wqe->ctrl.qpn_ds = cpu_to_be32((sq->sqn << 8) | ds_cnt);
519 	wqe->ctrl.imm = cpu_to_be32(ptag->tisn << 8);
520 
521 	if (mlx5e_do_send_cqe(sq))
522 		wqe->ctrl.fm_ce_se = MLX5_WQE_CTRL_CQ_UPDATE | MLX5_FENCE_MODE_INITIATOR_SMALL;
523 	else
524 		wqe->ctrl.fm_ce_se = MLX5_FENCE_MODE_INITIATOR_SMALL;
525 
526 	/* fill out UMR control segment */
527 	wqe->umr.flags = 0x80;	/* inline data */
528 	wqe->umr.bsf_octowords = cpu_to_be16(MLX5_FLD_SZ_BYTES(sw_tls_cntx, param) / 16);
529 
530 	/* copy in the static crypto parameters */
531 	memcpy(wqe + 1, MLX5_ADDR_OF(sw_tls_cntx, ptag->crypto_params, param),
532 	    MLX5_FLD_SZ_BYTES(sw_tls_cntx, param));
533 
534 	/* copy data for doorbell */
535 	memcpy(sq->doorbell.d32, &wqe->ctrl, sizeof(sq->doorbell.d32));
536 
537 	sq->mbuf[pi].mbuf = NULL;
538 	sq->mbuf[pi].num_bytes = 0;
539 	sq->mbuf[pi].num_wqebbs = DIV_ROUND_UP(ds_cnt, MLX5_SEND_WQEBB_NUM_DS);
540 	sq->mbuf[pi].p_refcount = &ptag->refs;
541 	atomic_add_int(&ptag->refs, 1);
542 	sq->pc += sq->mbuf[pi].num_wqebbs;
543 }
544 
545 CTASSERT(MLX5_FLD_SZ_BYTES(sw_tls_cntx, progress) ==
546     sizeof(((struct mlx5e_tx_psv_wqe *)0)->psv));
547 
548 static void
549 mlx5e_tls_send_progress_parameters(struct mlx5e_sq *sq, struct mlx5e_tls_tag *ptag)
550 {
551 	const u32 ds_cnt = DIV_ROUND_UP(sizeof(struct mlx5e_tx_psv_wqe),
552 	    MLX5_SEND_WQE_DS);
553 	struct mlx5e_tx_psv_wqe *wqe;
554 	u16 pi;
555 
556 	pi = sq->pc & sq->wq.sz_m1;
557 	wqe = mlx5_wq_cyc_get_wqe(&sq->wq, pi);
558 
559 	memset(wqe, 0, sizeof(*wqe));
560 
561 	wqe->ctrl.opmod_idx_opcode = cpu_to_be32((sq->pc << 8) |
562 	    MLX5_OPCODE_SET_PSV | (MLX5_OPCODE_MOD_PSV_TLS_TIS_PROGRESS_PARAMS << 24));
563 	wqe->ctrl.qpn_ds = cpu_to_be32((sq->sqn << 8) | ds_cnt);
564 
565 	if (mlx5e_do_send_cqe(sq))
566 		wqe->ctrl.fm_ce_se = MLX5_WQE_CTRL_CQ_UPDATE;
567 
568 	/* copy in the PSV control segment */
569 	memcpy(&wqe->psv, MLX5_ADDR_OF(sw_tls_cntx, ptag->crypto_params, progress),
570 	    sizeof(wqe->psv));
571 
572 	/* copy data for doorbell */
573 	memcpy(sq->doorbell.d32, &wqe->ctrl, sizeof(sq->doorbell.d32));
574 
575 	sq->mbuf[pi].mbuf = NULL;
576 	sq->mbuf[pi].num_bytes = 0;
577 	sq->mbuf[pi].num_wqebbs = DIV_ROUND_UP(ds_cnt, MLX5_SEND_WQEBB_NUM_DS);
578 	sq->mbuf[pi].p_refcount = &ptag->refs;
579 	atomic_add_int(&ptag->refs, 1);
580 	sq->pc += sq->mbuf[pi].num_wqebbs;
581 }
582 
583 static void
584 mlx5e_tls_send_nop(struct mlx5e_sq *sq, struct mlx5e_tls_tag *ptag)
585 {
586 	const u32 ds_cnt = MLX5_SEND_WQEBB_NUM_DS;
587 	struct mlx5e_tx_wqe *wqe;
588 	u16 pi;
589 
590 	pi = sq->pc & sq->wq.sz_m1;
591 	wqe = mlx5_wq_cyc_get_wqe(&sq->wq, pi);
592 
593 	memset(&wqe->ctrl, 0, sizeof(wqe->ctrl));
594 
595 	wqe->ctrl.opmod_idx_opcode = cpu_to_be32((sq->pc << 8) | MLX5_OPCODE_NOP);
596 	wqe->ctrl.qpn_ds = cpu_to_be32((sq->sqn << 8) | ds_cnt);
597 	if (mlx5e_do_send_cqe(sq))
598 		wqe->ctrl.fm_ce_se = MLX5_WQE_CTRL_CQ_UPDATE | MLX5_FENCE_MODE_INITIATOR_SMALL;
599 	else
600 		wqe->ctrl.fm_ce_se = MLX5_FENCE_MODE_INITIATOR_SMALL;
601 
602 	/* Copy data for doorbell */
603 	memcpy(sq->doorbell.d32, &wqe->ctrl, sizeof(sq->doorbell.d32));
604 
605 	sq->mbuf[pi].mbuf = NULL;
606 	sq->mbuf[pi].num_bytes = 0;
607 	sq->mbuf[pi].num_wqebbs = DIV_ROUND_UP(ds_cnt, MLX5_SEND_WQEBB_NUM_DS);
608 	sq->mbuf[pi].p_refcount = &ptag->refs;
609 	atomic_add_int(&ptag->refs, 1);
610 	sq->pc += sq->mbuf[pi].num_wqebbs;
611 }
612 
613 #define	SBTLS_MBUF_NO_DATA ((struct mbuf *)1)
614 
615 static struct mbuf *
616 sbtls_recover_record(struct mbuf *mb, int wait, uint32_t tcp_old, uint32_t *ptcp_seq, bool *pis_start)
617 {
618 	struct mbuf *mr, *top;
619 	uint32_t offset;
620 	uint32_t delta;
621 
622 	/* check format of incoming mbuf */
623 	if (mb->m_next == NULL ||
624 	    (mb->m_next->m_flags & (M_EXTPG | M_EXT)) != (M_EXTPG | M_EXT)) {
625 		top = NULL;
626 		goto done;
627 	}
628 
629 	/* get unmapped data offset */
630 	offset = mtod(mb->m_next, uintptr_t);
631 
632 	/* check if we don't need to re-transmit anything */
633 	if (offset == 0) {
634 		top = SBTLS_MBUF_NO_DATA;
635 		*pis_start = true;
636 		goto done;
637 	}
638 
639 	/* try to get a new  packet header */
640 	top = m_gethdr(wait, MT_DATA);
641 	if (top == NULL)
642 		goto done;
643 
644 	mr = m_get(wait, MT_DATA);
645 	if (mr == NULL) {
646 		m_free(top);
647 		top = NULL;
648 		goto done;
649 	}
650 
651 	top->m_next = mr;
652 
653 	mb_dupcl(mr, mb->m_next);
654 
655 	/* the beginning of the TLS record */
656 	mr->m_data = NULL;
657 
658 	/* setup packet header length */
659 	top->m_pkthdr.len = mr->m_len = offset;
660 	top->m_len = 0;
661 
662 	/* check for partial re-transmit */
663 	delta = *ptcp_seq - tcp_old;
664 
665 	if (delta < offset) {
666 		m_adj(top, offset - delta);
667 		offset = delta;
668 
669 		/* continue where we left off */
670 		*pis_start = false;
671 	} else {
672 		*pis_start = true;
673 	}
674 
675 	/*
676 	 * Rewind the TCP sequence number by the amount of data
677 	 * retransmitted:
678 	 */
679 	*ptcp_seq -= offset;
680 done:
681 	return (top);
682 }
683 
684 static int
685 mlx5e_sq_tls_populate(struct mbuf *mb, uint64_t *pseq)
686 {
687 
688 	for (; mb != NULL; mb = mb->m_next) {
689 		if (!(mb->m_flags & M_EXTPG))
690 			continue;
691 		*pseq = mb->m_epg_seqno;
692 		return (1);
693 	}
694 	return (0);
695 }
696 
697 int
698 mlx5e_sq_tls_xmit(struct mlx5e_sq *sq, struct mlx5e_xmit_args *parg, struct mbuf **ppmb)
699 {
700 	struct mlx5e_tls_tag *ptls_tag;
701 	struct m_snd_tag *ptag;
702 	const struct tcphdr *th;
703 	struct mbuf *mb = *ppmb;
704 	u64 rcd_sn;
705 	u32 header_size;
706 	u32 mb_seq;
707 
708 	if ((mb->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0)
709 		return (MLX5E_TLS_CONTINUE);
710 
711 	ptag = mb->m_pkthdr.snd_tag;
712 
713 	if (
714 #if defined(RATELIMIT) && defined(IF_SND_TAG_TYPE_TLS_RATE_LIMIT)
715 	    ptag->type != IF_SND_TAG_TYPE_TLS_RATE_LIMIT &&
716 #endif
717 	    ptag->type != IF_SND_TAG_TYPE_TLS)
718 		return (MLX5E_TLS_CONTINUE);
719 
720 	ptls_tag = container_of(ptag, struct mlx5e_tls_tag, tag);
721 
722 	header_size = mlx5e_get_full_header_size(mb, &th);
723 	if (unlikely(header_size == 0 || th == NULL))
724 		return (MLX5E_TLS_FAILURE);
725 
726 	/*
727 	 * Send non-TLS TCP packets AS-IS:
728 	 */
729 	if (header_size == mb->m_pkthdr.len ||
730 	    mlx5e_sq_tls_populate(mb, &rcd_sn) == 0) {
731 		parg->tisn = 0;
732 		parg->ihs = header_size;
733 		return (MLX5E_TLS_CONTINUE);
734 	}
735 
736 	mb_seq = ntohl(th->th_seq);
737 
738 	MLX5E_TLS_TAG_LOCK(ptls_tag);
739 	switch (ptls_tag->state) {
740 	case MLX5E_TLS_ST_INIT:
741 		MLX5E_TLS_TAG_UNLOCK(ptls_tag);
742 		return (MLX5E_TLS_FAILURE);
743 	case MLX5E_TLS_ST_SETUP:
744 		ptls_tag->state = MLX5E_TLS_ST_TXRDY;
745 		ptls_tag->expected_seq = ~mb_seq;	/* force setup */
746 	default:
747 		MLX5E_TLS_TAG_UNLOCK(ptls_tag);
748 		break;
749 	}
750 
751 	if (unlikely(ptls_tag->expected_seq != mb_seq)) {
752 		bool is_start;
753 		struct mbuf *r_mb;
754 		uint32_t tcp_seq = mb_seq;
755 
756 		r_mb = sbtls_recover_record(mb, M_NOWAIT, ptls_tag->expected_seq, &tcp_seq, &is_start);
757 		if (r_mb == NULL) {
758 			MLX5E_TLS_STAT_INC(ptls_tag, tx_error, 1);
759 			return (MLX5E_TLS_FAILURE);
760 		}
761 
762 		MLX5E_TLS_STAT_INC(ptls_tag, tx_packets_ooo, 1);
763 
764 		/* check if this is the first fragment of a TLS record */
765 		if (is_start) {
766 			/* setup TLS static parameters */
767 			MLX5_SET64(sw_tls_cntx, ptls_tag->crypto_params,
768 			    param.initial_record_number, rcd_sn);
769 
770 			/*
771 			 * NOTE: The sendqueue should have enough room to
772 			 * carry both the static and the progress parameters
773 			 * when we get here!
774 			 */
775 			mlx5e_tls_send_static_parameters(sq, ptls_tag);
776 			mlx5e_tls_send_progress_parameters(sq, ptls_tag);
777 
778 			if (r_mb == SBTLS_MBUF_NO_DATA) {
779 				mlx5e_tls_send_nop(sq, ptls_tag);
780 				ptls_tag->expected_seq = mb_seq;
781 				return (MLX5E_TLS_LOOP);
782 			}
783 		}
784 
785 		MLX5E_TLS_STAT_INC(ptls_tag, tx_bytes_ooo, r_mb->m_pkthdr.len);
786 
787 		/* setup transmit arguments */
788 		parg->tisn = ptls_tag->tisn;
789 		parg->pref = &ptls_tag->refs;
790 
791 		/* try to send DUMP data */
792 		if (mlx5e_sq_dump_xmit(sq, parg, &r_mb) != 0) {
793 			m_freem(r_mb);
794 			ptls_tag->expected_seq = tcp_seq;
795 			return (MLX5E_TLS_FAILURE);
796 		} else {
797 			ptls_tag->expected_seq = mb_seq;
798 			return (MLX5E_TLS_LOOP);
799 		}
800 	} else {
801 		MLX5E_TLS_STAT_INC(ptls_tag, tx_packets, 1);
802 		MLX5E_TLS_STAT_INC(ptls_tag, tx_bytes, mb->m_pkthdr.len);
803 	}
804 	ptls_tag->expected_seq += mb->m_pkthdr.len - header_size;
805 
806 	parg->tisn = ptls_tag->tisn;
807 	parg->ihs = header_size;
808 	parg->pref = &ptls_tag->refs;
809 	return (MLX5E_TLS_CONTINUE);
810 }
811 
812 #else
813 
814 int
815 mlx5e_tls_init(struct mlx5e_priv *priv)
816 {
817 
818 	return (0);
819 }
820 
821 void
822 mlx5e_tls_cleanup(struct mlx5e_priv *priv)
823 {
824 	/* NOP */
825 }
826 
827 #endif		/* KERN_TLS */
828