17272f9cdSHans Petter Selasky /*-
20f7b6e11SKonstantin Belousov * Copyright (c) 2019-2021 Mellanox Technologies. All rights reserved.
3ebdb7006SHans Petter Selasky * Copyright (c) 2022 NVIDIA corporation & affiliates.
47272f9cdSHans Petter Selasky *
57272f9cdSHans Petter Selasky * Redistribution and use in source and binary forms, with or without
67272f9cdSHans Petter Selasky * modification, are permitted provided that the following conditions
77272f9cdSHans Petter Selasky * are met:
87272f9cdSHans Petter Selasky * 1. Redistributions of source code must retain the above copyright
97272f9cdSHans Petter Selasky * notice, this list of conditions and the following disclaimer.
107272f9cdSHans Petter Selasky * 2. Redistributions in binary form must reproduce the above copyright
117272f9cdSHans Petter Selasky * notice, this list of conditions and the following disclaimer in the
127272f9cdSHans Petter Selasky * documentation and/or other materials provided with the distribution.
137272f9cdSHans Petter Selasky *
147272f9cdSHans Petter Selasky * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS `AS IS' AND
157272f9cdSHans Petter Selasky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
167272f9cdSHans Petter Selasky * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
177272f9cdSHans Petter Selasky * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
187272f9cdSHans Petter Selasky * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
197272f9cdSHans Petter Selasky * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
207272f9cdSHans Petter Selasky * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
217272f9cdSHans Petter Selasky * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
227272f9cdSHans Petter Selasky * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
237272f9cdSHans Petter Selasky * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
247272f9cdSHans Petter Selasky * SUCH DAMAGE.
257272f9cdSHans Petter Selasky */
267272f9cdSHans Petter Selasky
277272f9cdSHans Petter Selasky #include "opt_kern_tls.h"
28b984b956SKonstantin Belousov #include "opt_rss.h"
29b984b956SKonstantin Belousov #include "opt_ratelimit.h"
307272f9cdSHans Petter Selasky
3189918a23SKonstantin Belousov #include <dev/mlx5/mlx5_en/en.h>
327272f9cdSHans Petter Selasky
337272f9cdSHans Petter Selasky #include <dev/mlx5/tls.h>
34e23731dbSKonstantin Belousov #include <dev/mlx5/crypto.h>
357272f9cdSHans Petter Selasky
367272f9cdSHans Petter Selasky #include <linux/delay.h>
377272f9cdSHans Petter Selasky #include <sys/ktls.h>
387272f9cdSHans Petter Selasky #include <opencrypto/cryptodev.h>
397272f9cdSHans Petter Selasky
407272f9cdSHans Petter Selasky #ifdef KERN_TLS
417272f9cdSHans Petter Selasky
42c782ea8bSJohn Baldwin #ifdef RATELIMIT
43c782ea8bSJohn Baldwin static if_snd_tag_modify_t mlx5e_tls_rl_snd_tag_modify;
44c782ea8bSJohn Baldwin #endif
45c782ea8bSJohn Baldwin static if_snd_tag_query_t mlx5e_tls_snd_tag_query;
46c782ea8bSJohn Baldwin static if_snd_tag_free_t mlx5e_tls_snd_tag_free;
47c782ea8bSJohn Baldwin
48c782ea8bSJohn Baldwin static const struct if_snd_tag_sw mlx5e_tls_snd_tag_sw = {
49c782ea8bSJohn Baldwin .snd_tag_query = mlx5e_tls_snd_tag_query,
50c782ea8bSJohn Baldwin .snd_tag_free = mlx5e_tls_snd_tag_free,
51c782ea8bSJohn Baldwin .type = IF_SND_TAG_TYPE_TLS
52c782ea8bSJohn Baldwin };
53c782ea8bSJohn Baldwin
54c782ea8bSJohn Baldwin #ifdef RATELIMIT
55c782ea8bSJohn Baldwin static const struct if_snd_tag_sw mlx5e_tls_rl_snd_tag_sw = {
56c782ea8bSJohn Baldwin .snd_tag_modify = mlx5e_tls_rl_snd_tag_modify,
57c782ea8bSJohn Baldwin .snd_tag_query = mlx5e_tls_snd_tag_query,
58c782ea8bSJohn Baldwin .snd_tag_free = mlx5e_tls_snd_tag_free,
59c782ea8bSJohn Baldwin .type = IF_SND_TAG_TYPE_TLS_RATE_LIMIT
60c782ea8bSJohn Baldwin };
61c782ea8bSJohn Baldwin #endif
62c782ea8bSJohn Baldwin
637272f9cdSHans Petter Selasky MALLOC_DEFINE(M_MLX5E_TLS, "MLX5E_TLS", "MLX5 ethernet HW TLS");
647272f9cdSHans Petter Selasky
657272f9cdSHans Petter Selasky /* software TLS context */
667272f9cdSHans Petter Selasky struct mlx5_ifc_sw_tls_cntx_bits {
677272f9cdSHans Petter Selasky struct mlx5_ifc_tls_static_params_bits param;
687272f9cdSHans Petter Selasky struct mlx5_ifc_tls_progress_params_bits progress;
697272f9cdSHans Petter Selasky struct {
707272f9cdSHans Petter Selasky uint8_t key_data[8][0x20];
717272f9cdSHans Petter Selasky uint8_t key_len[0x20];
727272f9cdSHans Petter Selasky } key;
737272f9cdSHans Petter Selasky };
747272f9cdSHans Petter Selasky
757272f9cdSHans Petter Selasky CTASSERT(MLX5_ST_SZ_BYTES(sw_tls_cntx) <= sizeof(((struct mlx5e_tls_tag *)0)->crypto_params));
767272f9cdSHans Petter Selasky CTASSERT(MLX5_ST_SZ_BYTES(mkc) == sizeof(((struct mlx5e_tx_umr_wqe *)0)->mkc));
777272f9cdSHans Petter Selasky
787272f9cdSHans Petter Selasky static const char *mlx5e_tls_stats_desc[] = {
797272f9cdSHans Petter Selasky MLX5E_TLS_STATS(MLX5E_STATS_DESC)
807272f9cdSHans Petter Selasky };
817272f9cdSHans Petter Selasky
827272f9cdSHans Petter Selasky static void mlx5e_tls_work(struct work_struct *);
837272f9cdSHans Petter Selasky
8481b38bceSAndrew Gallatin /*
8581b38bceSAndrew Gallatin * Expand the tls tag UMA zone in a sleepable context
8681b38bceSAndrew Gallatin */
8781b38bceSAndrew Gallatin
8881b38bceSAndrew Gallatin static void
mlx5e_prealloc_tags(struct mlx5e_priv * priv,int nitems)8981b38bceSAndrew Gallatin mlx5e_prealloc_tags(struct mlx5e_priv *priv, int nitems)
9081b38bceSAndrew Gallatin {
9181b38bceSAndrew Gallatin struct mlx5e_tls_tag **tags;
9281b38bceSAndrew Gallatin int i;
9381b38bceSAndrew Gallatin
9481b38bceSAndrew Gallatin tags = malloc(sizeof(tags[0]) * nitems,
9581b38bceSAndrew Gallatin M_MLX5E_TLS, M_WAITOK);
9681b38bceSAndrew Gallatin for (i = 0; i < nitems; i++)
9781b38bceSAndrew Gallatin tags[i] = uma_zalloc(priv->tls.zone, M_WAITOK);
9881b38bceSAndrew Gallatin __compiler_membar();
9981b38bceSAndrew Gallatin for (i = 0; i < nitems; i++)
10081b38bceSAndrew Gallatin uma_zfree(priv->tls.zone, tags[i]);
10181b38bceSAndrew Gallatin free(tags, M_MLX5E_TLS);
10281b38bceSAndrew Gallatin }
10381b38bceSAndrew Gallatin
1047272f9cdSHans Petter Selasky static int
mlx5e_tls_tag_import(void * arg,void ** store,int cnt,int domain,int flags)1050f7b6e11SKonstantin Belousov mlx5e_tls_tag_import(void *arg, void **store, int cnt, int domain, int flags)
1067272f9cdSHans Petter Selasky {
1070f7b6e11SKonstantin Belousov struct mlx5e_tls_tag *ptag;
10881b38bceSAndrew Gallatin struct mlx5e_priv *priv = arg;
10981b38bceSAndrew Gallatin int err, i;
11081b38bceSAndrew Gallatin
11181b38bceSAndrew Gallatin /*
11281b38bceSAndrew Gallatin * mlx5_tls_open_tis() sleeps on a firmware command, so
11381b38bceSAndrew Gallatin * zone allocations must be done from a sleepable context.
11481b38bceSAndrew Gallatin * Note that the uma_zalloc() in mlx5e_tls_snd_tag_alloc()
11581b38bceSAndrew Gallatin * is done with M_NOWAIT so that hitting the zone limit does
11681b38bceSAndrew Gallatin * not cause the allocation to pause forever.
11781b38bceSAndrew Gallatin */
1187272f9cdSHans Petter Selasky
1190f7b6e11SKonstantin Belousov for (i = 0; i != cnt; i++) {
1200f7b6e11SKonstantin Belousov ptag = malloc_domainset(sizeof(*ptag), M_MLX5E_TLS,
121*85af37e1SSlava Shwartsman mlx5_dev_domainset(priv->mdev), flags | M_ZERO);
12281b38bceSAndrew Gallatin if (ptag == NULL)
12381b38bceSAndrew Gallatin return (i);
12481b38bceSAndrew Gallatin ptag->tls = &priv->tls;
1257272f9cdSHans Petter Selasky mtx_init(&ptag->mtx, "mlx5-tls-tag-mtx", NULL, MTX_DEF);
1267272f9cdSHans Petter Selasky INIT_WORK(&ptag->work, mlx5e_tls_work);
12781b38bceSAndrew Gallatin err = mlx5_tls_open_tis(priv->mdev, 0, priv->tdn,
12881b38bceSAndrew Gallatin priv->pdn, &ptag->tisn);
12981b38bceSAndrew Gallatin if (err) {
13081b38bceSAndrew Gallatin MLX5E_TLS_STAT_INC(ptag, tx_error, 1);
13181b38bceSAndrew Gallatin free(ptag, M_MLX5E_TLS);
13281b38bceSAndrew Gallatin return (i);
13381b38bceSAndrew Gallatin }
13481b38bceSAndrew Gallatin
1350f7b6e11SKonstantin Belousov store[i] = ptag;
1360f7b6e11SKonstantin Belousov }
1370f7b6e11SKonstantin Belousov return (i);
1387272f9cdSHans Petter Selasky }
1397272f9cdSHans Petter Selasky
1407272f9cdSHans Petter Selasky static void
mlx5e_tls_tag_release(void * arg,void ** store,int cnt)1410f7b6e11SKonstantin Belousov mlx5e_tls_tag_release(void *arg, void **store, int cnt)
1427272f9cdSHans Petter Selasky {
1430f7b6e11SKonstantin Belousov struct mlx5e_tls_tag *ptag;
1447272f9cdSHans Petter Selasky struct mlx5e_priv *priv;
1457272f9cdSHans Petter Selasky struct mlx5e_tls *ptls;
1460f7b6e11SKonstantin Belousov int i;
1477272f9cdSHans Petter Selasky
1480f7b6e11SKonstantin Belousov for (i = 0; i != cnt; i++) {
1490f7b6e11SKonstantin Belousov ptag = store[i];
1507272f9cdSHans Petter Selasky ptls = ptag->tls;
1517272f9cdSHans Petter Selasky priv = container_of(ptls, struct mlx5e_priv, tls);
1527272f9cdSHans Petter Selasky
1537272f9cdSHans Petter Selasky flush_work(&ptag->work);
1547272f9cdSHans Petter Selasky
1557272f9cdSHans Petter Selasky if (ptag->tisn != 0) {
1567272f9cdSHans Petter Selasky mlx5_tls_close_tis(priv->mdev, ptag->tisn);
1577272f9cdSHans Petter Selasky }
1587272f9cdSHans Petter Selasky
1597272f9cdSHans Petter Selasky mtx_destroy(&ptag->mtx);
1600f7b6e11SKonstantin Belousov
1610f7b6e11SKonstantin Belousov free(ptag, M_MLX5E_TLS);
1620f7b6e11SKonstantin Belousov }
1637272f9cdSHans Petter Selasky }
1647272f9cdSHans Petter Selasky
1657272f9cdSHans Petter Selasky static void
mlx5e_tls_tag_zfree(struct mlx5e_tls_tag * ptag)1667272f9cdSHans Petter Selasky mlx5e_tls_tag_zfree(struct mlx5e_tls_tag *ptag)
1677272f9cdSHans Petter Selasky {
168015f22f5SHans Petter Selasky /* make sure any unhandled taskqueue events are ignored */
169015f22f5SHans Petter Selasky ptag->state = MLX5E_TLS_ST_FREED;
1707272f9cdSHans Petter Selasky
1717272f9cdSHans Petter Selasky /* reset some variables */
1727272f9cdSHans Petter Selasky ptag->dek_index = 0;
1737272f9cdSHans Petter Selasky ptag->dek_index_ok = 0;
1747272f9cdSHans Petter Selasky
1757272f9cdSHans Petter Selasky /* avoid leaking keys */
1767272f9cdSHans Petter Selasky memset(ptag->crypto_params, 0, sizeof(ptag->crypto_params));
1777272f9cdSHans Petter Selasky
1787272f9cdSHans Petter Selasky /* return tag to UMA */
1797272f9cdSHans Petter Selasky uma_zfree(ptag->tls->zone, ptag);
1807272f9cdSHans Petter Selasky }
1817272f9cdSHans Petter Selasky
18281b38bceSAndrew Gallatin static int
mlx5e_max_tag_proc(SYSCTL_HANDLER_ARGS)18381b38bceSAndrew Gallatin mlx5e_max_tag_proc(SYSCTL_HANDLER_ARGS)
18481b38bceSAndrew Gallatin {
18581b38bceSAndrew Gallatin struct mlx5e_priv *priv = (struct mlx5e_priv *)arg1;
18681b38bceSAndrew Gallatin struct mlx5e_tls *ptls = &priv->tls;
18781b38bceSAndrew Gallatin int err;
18881b38bceSAndrew Gallatin unsigned int max_tags;
18981b38bceSAndrew Gallatin
19081b38bceSAndrew Gallatin max_tags = ptls->zone_max;
19181b38bceSAndrew Gallatin err = sysctl_handle_int(oidp, &max_tags, arg2, req);
19281b38bceSAndrew Gallatin if (err != 0 || req->newptr == NULL )
19381b38bceSAndrew Gallatin return err;
19481b38bceSAndrew Gallatin if (max_tags == ptls->zone_max)
19581b38bceSAndrew Gallatin return 0;
19681b38bceSAndrew Gallatin if (max_tags > priv->tls.max_resources || max_tags == 0)
19781b38bceSAndrew Gallatin return (EINVAL);
19881b38bceSAndrew Gallatin ptls->zone_max = max_tags;
19981b38bceSAndrew Gallatin uma_zone_set_max(ptls->zone, ptls->zone_max);
20081b38bceSAndrew Gallatin return 0;
20181b38bceSAndrew Gallatin }
20281b38bceSAndrew Gallatin
2037272f9cdSHans Petter Selasky int
mlx5e_tls_init(struct mlx5e_priv * priv)2047272f9cdSHans Petter Selasky mlx5e_tls_init(struct mlx5e_priv *priv)
2057272f9cdSHans Petter Selasky {
2067272f9cdSHans Petter Selasky struct mlx5e_tls *ptls = &priv->tls;
2077272f9cdSHans Petter Selasky struct sysctl_oid *node;
20881b38bceSAndrew Gallatin uint32_t max_dek, max_tis, x;
20981b38bceSAndrew Gallatin int zone_max = 0, prealloc_tags = 0;
2107272f9cdSHans Petter Selasky
21175767cb8SHans Petter Selasky if (MLX5_CAP_GEN(priv->mdev, tls_tx) == 0 ||
21275767cb8SHans Petter Selasky MLX5_CAP_GEN(priv->mdev, log_max_dek) == 0)
2137272f9cdSHans Petter Selasky return (0);
2147272f9cdSHans Petter Selasky
2157272f9cdSHans Petter Selasky ptls->wq = create_singlethread_workqueue("mlx5-tls-wq");
2167272f9cdSHans Petter Selasky if (ptls->wq == NULL)
2177272f9cdSHans Petter Selasky return (ENOMEM);
2187272f9cdSHans Petter Selasky
2197272f9cdSHans Petter Selasky sysctl_ctx_init(&ptls->ctx);
2207272f9cdSHans Petter Selasky
2217272f9cdSHans Petter Selasky snprintf(ptls->zname, sizeof(ptls->zname),
2227272f9cdSHans Petter Selasky "mlx5_%u_tls", device_get_unit(priv->mdev->pdev->dev.bsddev));
2237272f9cdSHans Petter Selasky
22481b38bceSAndrew Gallatin
22581b38bceSAndrew Gallatin TUNABLE_INT_FETCH("hw.mlx5.tls_max_tags", &zone_max);
22681b38bceSAndrew Gallatin TUNABLE_INT_FETCH("hw.mlx5.tls_prealloc_tags", &prealloc_tags);
22781b38bceSAndrew Gallatin
2280f7b6e11SKonstantin Belousov ptls->zone = uma_zcache_create(ptls->zname,
2290f7b6e11SKonstantin Belousov sizeof(struct mlx5e_tls_tag), NULL, NULL, NULL, NULL,
23081b38bceSAndrew Gallatin mlx5e_tls_tag_import, mlx5e_tls_tag_release, priv,
23181b38bceSAndrew Gallatin UMA_ZONE_UNMANAGED | (prealloc_tags ? UMA_ZONE_NOFREE : 0));
2327272f9cdSHans Petter Selasky
23375767cb8SHans Petter Selasky /* shared between RX and TX TLS */
23481b38bceSAndrew Gallatin max_dek = 1U << (MLX5_CAP_GEN(priv->mdev, log_max_dek) - 1);
23581b38bceSAndrew Gallatin max_tis = 1U << (MLX5_CAP_GEN(priv->mdev, log_max_tis) - 1);
23681b38bceSAndrew Gallatin ptls->max_resources = MIN(max_dek, max_tis);
23781b38bceSAndrew Gallatin
23881b38bceSAndrew Gallatin if (zone_max != 0) {
23981b38bceSAndrew Gallatin ptls->zone_max = zone_max;
24081b38bceSAndrew Gallatin if (ptls->zone_max > priv->tls.max_resources)
24181b38bceSAndrew Gallatin ptls->zone_max = priv->tls.max_resources;
24281b38bceSAndrew Gallatin } else {
24381b38bceSAndrew Gallatin ptls->zone_max = priv->tls.max_resources;
24481b38bceSAndrew Gallatin }
24581b38bceSAndrew Gallatin
24681b38bceSAndrew Gallatin uma_zone_set_max(ptls->zone, ptls->zone_max);
24781b38bceSAndrew Gallatin if (prealloc_tags != 0)
24881b38bceSAndrew Gallatin mlx5e_prealloc_tags(priv, ptls->zone_max);
2497272f9cdSHans Petter Selasky
2507272f9cdSHans Petter Selasky for (x = 0; x != MLX5E_TLS_STATS_NUM; x++)
2517272f9cdSHans Petter Selasky ptls->stats.arg[x] = counter_u64_alloc(M_WAITOK);
2527272f9cdSHans Petter Selasky
2537272f9cdSHans Petter Selasky ptls->init = 1;
2547272f9cdSHans Petter Selasky
2557272f9cdSHans Petter Selasky node = SYSCTL_ADD_NODE(&priv->sysctl_ctx,
2567272f9cdSHans Petter Selasky SYSCTL_CHILDREN(priv->sysctl_ifnet), OID_AUTO,
2577029da5cSPawel Biernacki "tls", CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, "Hardware TLS offload");
2587272f9cdSHans Petter Selasky if (node == NULL)
2597272f9cdSHans Petter Selasky return (0);
2607272f9cdSHans Petter Selasky
26181b38bceSAndrew Gallatin SYSCTL_ADD_PROC(&priv->sysctl_ctx, SYSCTL_CHILDREN(node), OID_AUTO, "tls_max_tag",
26281b38bceSAndrew Gallatin CTLFLAG_RW | CTLTYPE_UINT | CTLFLAG_MPSAFE, priv, 0, mlx5e_max_tag_proc,
26381b38bceSAndrew Gallatin "IU", "Max number of TLS offload session tags");
26481b38bceSAndrew Gallatin
2657272f9cdSHans Petter Selasky mlx5e_create_counter_stats(&ptls->ctx,
2667272f9cdSHans Petter Selasky SYSCTL_CHILDREN(node), "stats",
2677272f9cdSHans Petter Selasky mlx5e_tls_stats_desc, MLX5E_TLS_STATS_NUM,
2687272f9cdSHans Petter Selasky ptls->stats.arg);
2697272f9cdSHans Petter Selasky
2707272f9cdSHans Petter Selasky return (0);
2717272f9cdSHans Petter Selasky }
2727272f9cdSHans Petter Selasky
2737272f9cdSHans Petter Selasky void
mlx5e_tls_cleanup(struct mlx5e_priv * priv)2747272f9cdSHans Petter Selasky mlx5e_tls_cleanup(struct mlx5e_priv *priv)
2757272f9cdSHans Petter Selasky {
2767272f9cdSHans Petter Selasky struct mlx5e_tls *ptls = &priv->tls;
2777272f9cdSHans Petter Selasky uint32_t x;
2787272f9cdSHans Petter Selasky
27975767cb8SHans Petter Selasky if (ptls->init == 0)
2807272f9cdSHans Petter Selasky return;
2817272f9cdSHans Petter Selasky
2827272f9cdSHans Petter Selasky ptls->init = 0;
2837272f9cdSHans Petter Selasky flush_workqueue(ptls->wq);
2847272f9cdSHans Petter Selasky sysctl_ctx_free(&ptls->ctx);
2857272f9cdSHans Petter Selasky uma_zdestroy(ptls->zone);
2867272f9cdSHans Petter Selasky destroy_workqueue(ptls->wq);
2877272f9cdSHans Petter Selasky
2887272f9cdSHans Petter Selasky for (x = 0; x != MLX5E_TLS_STATS_NUM; x++)
2897272f9cdSHans Petter Selasky counter_u64_free(ptls->stats.arg[x]);
2907272f9cdSHans Petter Selasky }
2917272f9cdSHans Petter Selasky
29281dbc22cSAndrew Gallatin
29381dbc22cSAndrew Gallatin static int
mlx5e_tls_st_init(struct mlx5e_priv * priv,struct mlx5e_tls_tag * ptag)29481dbc22cSAndrew Gallatin mlx5e_tls_st_init(struct mlx5e_priv *priv, struct mlx5e_tls_tag *ptag)
2957272f9cdSHans Petter Selasky {
2967272f9cdSHans Petter Selasky int err;
2977272f9cdSHans Petter Selasky
2987272f9cdSHans Petter Selasky /* try to open TIS, if not present */
2997272f9cdSHans Petter Selasky if (ptag->tisn == 0) {
3007272f9cdSHans Petter Selasky err = mlx5_tls_open_tis(priv->mdev, 0, priv->tdn,
3017272f9cdSHans Petter Selasky priv->pdn, &ptag->tisn);
3027272f9cdSHans Petter Selasky if (err) {
3037272f9cdSHans Petter Selasky MLX5E_TLS_STAT_INC(ptag, tx_error, 1);
3040d38b0bcSKonstantin Belousov return (-err);
3057272f9cdSHans Petter Selasky }
3067272f9cdSHans Petter Selasky }
3077272f9cdSHans Petter Selasky MLX5_SET(sw_tls_cntx, ptag->crypto_params, progress.pd, ptag->tisn);
3087272f9cdSHans Petter Selasky
3097272f9cdSHans Petter Selasky /* try to allocate a DEK context ID */
3107272f9cdSHans Petter Selasky err = mlx5_encryption_key_create(priv->mdev, priv->pdn,
311e23731dbSKonstantin Belousov MLX5_GENERAL_OBJECT_TYPE_ENCRYPTION_KEY_TYPE_TLS,
3127272f9cdSHans Petter Selasky MLX5_ADDR_OF(sw_tls_cntx, ptag->crypto_params, key.key_data),
3137272f9cdSHans Petter Selasky MLX5_GET(sw_tls_cntx, ptag->crypto_params, key.key_len),
3147272f9cdSHans Petter Selasky &ptag->dek_index);
3157272f9cdSHans Petter Selasky if (err) {
3167272f9cdSHans Petter Selasky MLX5E_TLS_STAT_INC(ptag, tx_error, 1);
3170d38b0bcSKonstantin Belousov return (-err);
3187272f9cdSHans Petter Selasky }
3197272f9cdSHans Petter Selasky
3207272f9cdSHans Petter Selasky MLX5_SET(sw_tls_cntx, ptag->crypto_params, param.dek_index, ptag->dek_index);
3217272f9cdSHans Petter Selasky
3227272f9cdSHans Petter Selasky ptag->dek_index_ok = 1;
3237272f9cdSHans Petter Selasky
3247272f9cdSHans Petter Selasky MLX5E_TLS_TAG_LOCK(ptag);
32578ae1e6eSHans Petter Selasky if (ptag->state == MLX5E_TLS_ST_INIT)
32678ae1e6eSHans Petter Selasky ptag->state = MLX5E_TLS_ST_SETUP;
3277272f9cdSHans Petter Selasky MLX5E_TLS_TAG_UNLOCK(ptag);
32881dbc22cSAndrew Gallatin return (0);
32981dbc22cSAndrew Gallatin }
33081dbc22cSAndrew Gallatin
33181dbc22cSAndrew Gallatin static void
mlx5e_tls_work(struct work_struct * work)33281dbc22cSAndrew Gallatin mlx5e_tls_work(struct work_struct *work)
33381dbc22cSAndrew Gallatin {
33481dbc22cSAndrew Gallatin struct mlx5e_tls_tag *ptag;
33581dbc22cSAndrew Gallatin struct mlx5e_priv *priv;
33681dbc22cSAndrew Gallatin
33781dbc22cSAndrew Gallatin ptag = container_of(work, struct mlx5e_tls_tag, work);
33881dbc22cSAndrew Gallatin priv = container_of(ptag->tls, struct mlx5e_priv, tls);
33981dbc22cSAndrew Gallatin
34081dbc22cSAndrew Gallatin switch (ptag->state) {
34181dbc22cSAndrew Gallatin case MLX5E_TLS_ST_INIT:
34281dbc22cSAndrew Gallatin (void)mlx5e_tls_st_init(priv, ptag);
3437272f9cdSHans Petter Selasky break;
3447272f9cdSHans Petter Selasky
345015f22f5SHans Petter Selasky case MLX5E_TLS_ST_RELEASE:
3467272f9cdSHans Petter Selasky /* try to destroy DEK context by ID */
3477272f9cdSHans Petter Selasky if (ptag->dek_index_ok)
34881dbc22cSAndrew Gallatin (void)mlx5_encryption_key_destroy(priv->mdev, ptag->dek_index);
3497272f9cdSHans Petter Selasky
3507272f9cdSHans Petter Selasky /* free tag */
3517272f9cdSHans Petter Selasky mlx5e_tls_tag_zfree(ptag);
3527272f9cdSHans Petter Selasky break;
3537272f9cdSHans Petter Selasky
3547272f9cdSHans Petter Selasky default:
3557272f9cdSHans Petter Selasky break;
3567272f9cdSHans Petter Selasky }
3577272f9cdSHans Petter Selasky }
3587272f9cdSHans Petter Selasky
3597272f9cdSHans Petter Selasky static int
mlx5e_tls_set_params(void * ctx,const struct tls_session_params * en)3607272f9cdSHans Petter Selasky mlx5e_tls_set_params(void *ctx, const struct tls_session_params *en)
3617272f9cdSHans Petter Selasky {
3627272f9cdSHans Petter Selasky
3637272f9cdSHans Petter Selasky MLX5_SET(sw_tls_cntx, ctx, param.const_2, 2);
3647272f9cdSHans Petter Selasky if (en->tls_vminor == TLS_MINOR_VER_TWO)
3657272f9cdSHans Petter Selasky MLX5_SET(sw_tls_cntx, ctx, param.tls_version, 2); /* v1.2 */
3667272f9cdSHans Petter Selasky else
3677272f9cdSHans Petter Selasky MLX5_SET(sw_tls_cntx, ctx, param.tls_version, 3); /* v1.3 */
3687272f9cdSHans Petter Selasky MLX5_SET(sw_tls_cntx, ctx, param.const_1, 1);
3697272f9cdSHans Petter Selasky MLX5_SET(sw_tls_cntx, ctx, param.encryption_standard, 1); /* TLS */
3707272f9cdSHans Petter Selasky
3717272f9cdSHans Petter Selasky /* copy the initial vector in place */
372233a6665SHans Petter Selasky switch (en->iv_len) {
373233a6665SHans Petter Selasky case MLX5_FLD_SZ_BYTES(sw_tls_cntx, param.gcm_iv):
374233a6665SHans Petter Selasky case MLX5_FLD_SZ_BYTES(sw_tls_cntx, param.gcm_iv) +
375233a6665SHans Petter Selasky MLX5_FLD_SZ_BYTES(sw_tls_cntx, param.implicit_iv):
3767272f9cdSHans Petter Selasky memcpy(MLX5_ADDR_OF(sw_tls_cntx, ctx, param.gcm_iv),
377233a6665SHans Petter Selasky en->iv, en->iv_len);
378233a6665SHans Petter Selasky break;
379233a6665SHans Petter Selasky default:
3807272f9cdSHans Petter Selasky return (EINVAL);
3817272f9cdSHans Petter Selasky }
3827272f9cdSHans Petter Selasky
3837272f9cdSHans Petter Selasky if (en->cipher_key_len <= MLX5_FLD_SZ_BYTES(sw_tls_cntx, key.key_data)) {
3847272f9cdSHans Petter Selasky memcpy(MLX5_ADDR_OF(sw_tls_cntx, ctx, key.key_data),
3857272f9cdSHans Petter Selasky en->cipher_key, en->cipher_key_len);
3867272f9cdSHans Petter Selasky MLX5_SET(sw_tls_cntx, ctx, key.key_len, en->cipher_key_len);
3877272f9cdSHans Petter Selasky } else {
3887272f9cdSHans Petter Selasky return (EINVAL);
3897272f9cdSHans Petter Selasky }
3907272f9cdSHans Petter Selasky return (0);
3917272f9cdSHans Petter Selasky }
3927272f9cdSHans Petter Selasky
3937272f9cdSHans Petter Selasky /* Verify zero default */
3947272f9cdSHans Petter Selasky CTASSERT(MLX5E_TLS_ST_INIT == 0);
3957272f9cdSHans Petter Selasky
3967272f9cdSHans Petter Selasky int
mlx5e_tls_snd_tag_alloc(if_t ifp,union if_snd_tag_alloc_params * params,struct m_snd_tag ** ppmt)39764bf5a43SKonstantin Belousov mlx5e_tls_snd_tag_alloc(if_t ifp, union if_snd_tag_alloc_params *params,
3987272f9cdSHans Petter Selasky struct m_snd_tag **ppmt)
3997272f9cdSHans Petter Selasky {
400638000c0SJohn Baldwin union if_snd_tag_alloc_params rl_params;
401c782ea8bSJohn Baldwin const struct if_snd_tag_sw *snd_tag_sw;
4027272f9cdSHans Petter Selasky struct mlx5e_priv *priv;
4037272f9cdSHans Petter Selasky struct mlx5e_tls_tag *ptag;
4047272f9cdSHans Petter Selasky const struct tls_session_params *en;
4057272f9cdSHans Petter Selasky int error;
4067272f9cdSHans Petter Selasky
4075dc00f00SJustin Hibbits priv = if_getsoftc(ifp);
4087272f9cdSHans Petter Selasky
4093a934ba7SHans Petter Selasky if (priv->gone != 0 || priv->tls.init == 0)
4107272f9cdSHans Petter Selasky return (EOPNOTSUPP);
4117272f9cdSHans Petter Selasky
41267f93079SGleb Smirnoff ptag = uma_zalloc(priv->tls.zone, M_NOWAIT);
41381b38bceSAndrew Gallatin if (ptag == NULL)
41481b38bceSAndrew Gallatin return (ENOMEM);
4157272f9cdSHans Petter Selasky
4167272f9cdSHans Petter Selasky /* sanity check default values */
4177272f9cdSHans Petter Selasky MPASS(ptag->dek_index == 0);
4187272f9cdSHans Petter Selasky MPASS(ptag->dek_index_ok == 0);
4197272f9cdSHans Petter Selasky
4207272f9cdSHans Petter Selasky /* check if there is no TIS context */
42181b38bceSAndrew Gallatin KASSERT(ptag->tisn != 0, ("ptag %p w/0 tisn", ptag));
4227272f9cdSHans Petter Selasky
4237272f9cdSHans Petter Selasky en = ¶ms->tls.tls->params;
4247272f9cdSHans Petter Selasky
4257272f9cdSHans Petter Selasky /* only TLS v1.2 and v1.3 is currently supported */
4267272f9cdSHans Petter Selasky if (en->tls_vmajor != TLS_MAJOR_VER_ONE ||
4277272f9cdSHans Petter Selasky (en->tls_vminor != TLS_MINOR_VER_TWO
4287272f9cdSHans Petter Selasky #ifdef TLS_MINOR_VER_THREE
4297272f9cdSHans Petter Selasky && en->tls_vminor != TLS_MINOR_VER_THREE
4307272f9cdSHans Petter Selasky #endif
4317272f9cdSHans Petter Selasky )) {
4327272f9cdSHans Petter Selasky error = EPROTONOSUPPORT;
4337272f9cdSHans Petter Selasky goto failure;
4347272f9cdSHans Petter Selasky }
4357272f9cdSHans Petter Selasky
4367272f9cdSHans Petter Selasky switch (en->cipher_algorithm) {
4377272f9cdSHans Petter Selasky case CRYPTO_AES_NIST_GCM_16:
4387272f9cdSHans Petter Selasky switch (en->cipher_key_len) {
4397272f9cdSHans Petter Selasky case 128 / 8:
4407272f9cdSHans Petter Selasky if (en->tls_vminor == TLS_MINOR_VER_TWO) {
4417272f9cdSHans Petter Selasky if (MLX5_CAP_TLS(priv->mdev, tls_1_2_aes_gcm_128) == 0) {
4427272f9cdSHans Petter Selasky error = EPROTONOSUPPORT;
4437272f9cdSHans Petter Selasky goto failure;
4447272f9cdSHans Petter Selasky }
4457272f9cdSHans Petter Selasky } else {
4467272f9cdSHans Petter Selasky if (MLX5_CAP_TLS(priv->mdev, tls_1_3_aes_gcm_128) == 0) {
4477272f9cdSHans Petter Selasky error = EPROTONOSUPPORT;
4487272f9cdSHans Petter Selasky goto failure;
4497272f9cdSHans Petter Selasky }
4507272f9cdSHans Petter Selasky }
4517272f9cdSHans Petter Selasky error = mlx5e_tls_set_params(ptag->crypto_params, en);
4527272f9cdSHans Petter Selasky if (error)
4537272f9cdSHans Petter Selasky goto failure;
4547272f9cdSHans Petter Selasky break;
4557272f9cdSHans Petter Selasky
4567272f9cdSHans Petter Selasky case 256 / 8:
4577272f9cdSHans Petter Selasky if (en->tls_vminor == TLS_MINOR_VER_TWO) {
4587272f9cdSHans Petter Selasky if (MLX5_CAP_TLS(priv->mdev, tls_1_2_aes_gcm_256) == 0) {
4597272f9cdSHans Petter Selasky error = EPROTONOSUPPORT;
4607272f9cdSHans Petter Selasky goto failure;
4617272f9cdSHans Petter Selasky }
4627272f9cdSHans Petter Selasky } else {
4637272f9cdSHans Petter Selasky if (MLX5_CAP_TLS(priv->mdev, tls_1_3_aes_gcm_256) == 0) {
4647272f9cdSHans Petter Selasky error = EPROTONOSUPPORT;
4657272f9cdSHans Petter Selasky goto failure;
4667272f9cdSHans Petter Selasky }
4677272f9cdSHans Petter Selasky }
4687272f9cdSHans Petter Selasky error = mlx5e_tls_set_params(ptag->crypto_params, en);
4697272f9cdSHans Petter Selasky if (error)
4707272f9cdSHans Petter Selasky goto failure;
4717272f9cdSHans Petter Selasky break;
4727272f9cdSHans Petter Selasky
4737272f9cdSHans Petter Selasky default:
4747272f9cdSHans Petter Selasky error = EINVAL;
4757272f9cdSHans Petter Selasky goto failure;
4767272f9cdSHans Petter Selasky }
4777272f9cdSHans Petter Selasky break;
4787272f9cdSHans Petter Selasky default:
4797272f9cdSHans Petter Selasky error = EPROTONOSUPPORT;
4807272f9cdSHans Petter Selasky goto failure;
4817272f9cdSHans Petter Selasky }
4827272f9cdSHans Petter Selasky
483638000c0SJohn Baldwin memset(&rl_params, 0, sizeof(rl_params));
484638000c0SJohn Baldwin rl_params.hdr = params->hdr;
48556fb710fSJohn Baldwin switch (params->hdr.type) {
486b7d92a66SJohn Baldwin #ifdef RATELIMIT
4877272f9cdSHans Petter Selasky case IF_SND_TAG_TYPE_TLS_RATE_LIMIT:
4887272f9cdSHans Petter Selasky rl_params.hdr.type = IF_SND_TAG_TYPE_RATE_LIMIT;
489418b5444SJohn Baldwin rl_params.rate_limit.max_rate = params->tls_rate_limit.max_rate;
490c782ea8bSJohn Baldwin snd_tag_sw = &mlx5e_tls_rl_snd_tag_sw;
4917272f9cdSHans Petter Selasky break;
4927272f9cdSHans Petter Selasky #endif
4937272f9cdSHans Petter Selasky case IF_SND_TAG_TYPE_TLS:
4947272f9cdSHans Petter Selasky rl_params.hdr.type = IF_SND_TAG_TYPE_UNLIMITED;
495c782ea8bSJohn Baldwin snd_tag_sw = &mlx5e_tls_snd_tag_sw;
4967272f9cdSHans Petter Selasky break;
4977272f9cdSHans Petter Selasky default:
4987272f9cdSHans Petter Selasky error = EOPNOTSUPP;
4997272f9cdSHans Petter Selasky goto failure;
5007272f9cdSHans Petter Selasky }
5017272f9cdSHans Petter Selasky
50236e0a362SJohn Baldwin error = m_snd_tag_alloc(ifp, &rl_params, &ptag->rl_tag);
503638000c0SJohn Baldwin if (error)
504638000c0SJohn Baldwin goto failure;
505638000c0SJohn Baldwin
5067272f9cdSHans Petter Selasky /* store pointer to mbuf tag */
50756fb710fSJohn Baldwin MPASS(ptag->tag.refcount == 0);
508c782ea8bSJohn Baldwin m_snd_tag_init(&ptag->tag, ifp, snd_tag_sw);
50956fb710fSJohn Baldwin *ppmt = &ptag->tag;
51078ae1e6eSHans Petter Selasky
511015f22f5SHans Petter Selasky /* reset state */
512015f22f5SHans Petter Selasky ptag->state = MLX5E_TLS_ST_INIT;
513015f22f5SHans Petter Selasky
51481dbc22cSAndrew Gallatin error = mlx5e_tls_st_init(priv, ptag);
51581b38bceSAndrew Gallatin if (error != 0)
51681b38bceSAndrew Gallatin goto failure;
51778ae1e6eSHans Petter Selasky
5187272f9cdSHans Petter Selasky return (0);
5197272f9cdSHans Petter Selasky
5207272f9cdSHans Petter Selasky failure:
5217272f9cdSHans Petter Selasky mlx5e_tls_tag_zfree(ptag);
5227272f9cdSHans Petter Selasky return (error);
5237272f9cdSHans Petter Selasky }
5247272f9cdSHans Petter Selasky
525b7d92a66SJohn Baldwin #ifdef RATELIMIT
526c782ea8bSJohn Baldwin static int
mlx5e_tls_rl_snd_tag_modify(struct m_snd_tag * pmt,union if_snd_tag_modify_params * params)527c782ea8bSJohn Baldwin mlx5e_tls_rl_snd_tag_modify(struct m_snd_tag *pmt, union if_snd_tag_modify_params *params)
528c782ea8bSJohn Baldwin {
529638000c0SJohn Baldwin union if_snd_tag_modify_params rl_params;
53056fb710fSJohn Baldwin struct mlx5e_tls_tag *ptag =
53156fb710fSJohn Baldwin container_of(pmt, struct mlx5e_tls_tag, tag);
5327272f9cdSHans Petter Selasky int error;
5337272f9cdSHans Petter Selasky
5347272f9cdSHans Petter Selasky memset(&rl_params, 0, sizeof(rl_params));
535638000c0SJohn Baldwin rl_params.rate_limit.max_rate = params->tls_rate_limit.max_rate;
536c782ea8bSJohn Baldwin error = ptag->rl_tag->sw->snd_tag_modify(ptag->rl_tag, &rl_params);
5377272f9cdSHans Petter Selasky return (error);
538c782ea8bSJohn Baldwin }
5397272f9cdSHans Petter Selasky #endif
5407272f9cdSHans Petter Selasky
541c782ea8bSJohn Baldwin static int
mlx5e_tls_snd_tag_query(struct m_snd_tag * pmt,union if_snd_tag_query_params * params)5427272f9cdSHans Petter Selasky mlx5e_tls_snd_tag_query(struct m_snd_tag *pmt, union if_snd_tag_query_params *params)
5437272f9cdSHans Petter Selasky {
5447272f9cdSHans Petter Selasky struct mlx5e_tls_tag *ptag =
54556fb710fSJohn Baldwin container_of(pmt, struct mlx5e_tls_tag, tag);
5467272f9cdSHans Petter Selasky
547c782ea8bSJohn Baldwin return (ptag->rl_tag->sw->snd_tag_query(ptag->rl_tag, params));
5487272f9cdSHans Petter Selasky }
5497272f9cdSHans Petter Selasky
550c782ea8bSJohn Baldwin static void
mlx5e_tls_snd_tag_free(struct m_snd_tag * pmt)5517272f9cdSHans Petter Selasky mlx5e_tls_snd_tag_free(struct m_snd_tag *pmt)
5527272f9cdSHans Petter Selasky {
5537272f9cdSHans Petter Selasky struct mlx5e_tls_tag *ptag =
55456fb710fSJohn Baldwin container_of(pmt, struct mlx5e_tls_tag, tag);
5557272f9cdSHans Petter Selasky struct mlx5e_priv *priv;
5567272f9cdSHans Petter Selasky
557638000c0SJohn Baldwin m_snd_tag_rele(ptag->rl_tag);
5587272f9cdSHans Petter Selasky
5597272f9cdSHans Petter Selasky MLX5E_TLS_TAG_LOCK(ptag);
560015f22f5SHans Petter Selasky ptag->state = MLX5E_TLS_ST_RELEASE;
5617272f9cdSHans Petter Selasky MLX5E_TLS_TAG_UNLOCK(ptag);
5627272f9cdSHans Petter Selasky
5635dc00f00SJustin Hibbits priv = if_getsoftc(ptag->tag.ifp);
5647272f9cdSHans Petter Selasky queue_work(priv->tls.wq, &ptag->work);
5657272f9cdSHans Petter Selasky }
5667272f9cdSHans Petter Selasky
5677272f9cdSHans Petter Selasky CTASSERT((MLX5_FLD_SZ_BYTES(sw_tls_cntx, param) % 16) == 0);
5687272f9cdSHans Petter Selasky
5697272f9cdSHans Petter Selasky static void
mlx5e_tls_send_static_parameters(struct mlx5e_sq * sq,struct mlx5e_tls_tag * ptag)5707272f9cdSHans Petter Selasky mlx5e_tls_send_static_parameters(struct mlx5e_sq *sq, struct mlx5e_tls_tag *ptag)
5717272f9cdSHans Petter Selasky {
5727272f9cdSHans Petter Selasky const u32 ds_cnt = DIV_ROUND_UP(sizeof(struct mlx5e_tx_umr_wqe) +
5737272f9cdSHans Petter Selasky MLX5_FLD_SZ_BYTES(sw_tls_cntx, param), MLX5_SEND_WQE_DS);
5747272f9cdSHans Petter Selasky struct mlx5e_tx_umr_wqe *wqe;
5757272f9cdSHans Petter Selasky u16 pi;
5767272f9cdSHans Petter Selasky
5777272f9cdSHans Petter Selasky pi = sq->pc & sq->wq.sz_m1;
5787272f9cdSHans Petter Selasky wqe = mlx5_wq_cyc_get_wqe(&sq->wq, pi);
5797272f9cdSHans Petter Selasky
5807272f9cdSHans Petter Selasky memset(wqe, 0, sizeof(*wqe));
5817272f9cdSHans Petter Selasky
5827272f9cdSHans Petter Selasky wqe->ctrl.opmod_idx_opcode = cpu_to_be32((sq->pc << 8) |
5837272f9cdSHans Petter Selasky MLX5_OPCODE_UMR | (MLX5_OPCODE_MOD_UMR_TLS_TIS_STATIC_PARAMS << 24));
5847272f9cdSHans Petter Selasky wqe->ctrl.qpn_ds = cpu_to_be32((sq->sqn << 8) | ds_cnt);
5857272f9cdSHans Petter Selasky wqe->ctrl.imm = cpu_to_be32(ptag->tisn << 8);
5867272f9cdSHans Petter Selasky
5877272f9cdSHans Petter Selasky if (mlx5e_do_send_cqe(sq))
5887272f9cdSHans Petter Selasky wqe->ctrl.fm_ce_se = MLX5_WQE_CTRL_CQ_UPDATE | MLX5_FENCE_MODE_INITIATOR_SMALL;
5897272f9cdSHans Petter Selasky else
5907272f9cdSHans Petter Selasky wqe->ctrl.fm_ce_se = MLX5_FENCE_MODE_INITIATOR_SMALL;
5917272f9cdSHans Petter Selasky
5927272f9cdSHans Petter Selasky /* fill out UMR control segment */
5937272f9cdSHans Petter Selasky wqe->umr.flags = 0x80; /* inline data */
5947272f9cdSHans Petter Selasky wqe->umr.bsf_octowords = cpu_to_be16(MLX5_FLD_SZ_BYTES(sw_tls_cntx, param) / 16);
5957272f9cdSHans Petter Selasky
5967272f9cdSHans Petter Selasky /* copy in the static crypto parameters */
5977272f9cdSHans Petter Selasky memcpy(wqe + 1, MLX5_ADDR_OF(sw_tls_cntx, ptag->crypto_params, param),
5987272f9cdSHans Petter Selasky MLX5_FLD_SZ_BYTES(sw_tls_cntx, param));
5997272f9cdSHans Petter Selasky
6007272f9cdSHans Petter Selasky /* copy data for doorbell */
6017272f9cdSHans Petter Selasky memcpy(sq->doorbell.d32, &wqe->ctrl, sizeof(sq->doorbell.d32));
6027272f9cdSHans Petter Selasky
6037272f9cdSHans Petter Selasky sq->mbuf[pi].mbuf = NULL;
6047272f9cdSHans Petter Selasky sq->mbuf[pi].num_bytes = 0;
6057272f9cdSHans Petter Selasky sq->mbuf[pi].num_wqebbs = DIV_ROUND_UP(ds_cnt, MLX5_SEND_WQEBB_NUM_DS);
606ebdb7006SHans Petter Selasky sq->mbuf[pi].mst = m_snd_tag_ref(&ptag->tag);
607ebdb7006SHans Petter Selasky
6087272f9cdSHans Petter Selasky sq->pc += sq->mbuf[pi].num_wqebbs;
6097272f9cdSHans Petter Selasky }
6107272f9cdSHans Petter Selasky
6117272f9cdSHans Petter Selasky CTASSERT(MLX5_FLD_SZ_BYTES(sw_tls_cntx, progress) ==
6127272f9cdSHans Petter Selasky sizeof(((struct mlx5e_tx_psv_wqe *)0)->psv));
6137272f9cdSHans Petter Selasky
6147272f9cdSHans Petter Selasky static void
mlx5e_tls_send_progress_parameters(struct mlx5e_sq * sq,struct mlx5e_tls_tag * ptag)6157272f9cdSHans Petter Selasky mlx5e_tls_send_progress_parameters(struct mlx5e_sq *sq, struct mlx5e_tls_tag *ptag)
6167272f9cdSHans Petter Selasky {
6177272f9cdSHans Petter Selasky const u32 ds_cnt = DIV_ROUND_UP(sizeof(struct mlx5e_tx_psv_wqe),
6187272f9cdSHans Petter Selasky MLX5_SEND_WQE_DS);
6197272f9cdSHans Petter Selasky struct mlx5e_tx_psv_wqe *wqe;
6207272f9cdSHans Petter Selasky u16 pi;
6217272f9cdSHans Petter Selasky
6227272f9cdSHans Petter Selasky pi = sq->pc & sq->wq.sz_m1;
6237272f9cdSHans Petter Selasky wqe = mlx5_wq_cyc_get_wqe(&sq->wq, pi);
6247272f9cdSHans Petter Selasky
6257272f9cdSHans Petter Selasky memset(wqe, 0, sizeof(*wqe));
6267272f9cdSHans Petter Selasky
6277272f9cdSHans Petter Selasky wqe->ctrl.opmod_idx_opcode = cpu_to_be32((sq->pc << 8) |
6287272f9cdSHans Petter Selasky MLX5_OPCODE_SET_PSV | (MLX5_OPCODE_MOD_PSV_TLS_TIS_PROGRESS_PARAMS << 24));
6297272f9cdSHans Petter Selasky wqe->ctrl.qpn_ds = cpu_to_be32((sq->sqn << 8) | ds_cnt);
6307272f9cdSHans Petter Selasky
6317272f9cdSHans Petter Selasky if (mlx5e_do_send_cqe(sq))
632ce69b842SHans Petter Selasky wqe->ctrl.fm_ce_se = MLX5_WQE_CTRL_CQ_UPDATE;
6337272f9cdSHans Petter Selasky
6347272f9cdSHans Petter Selasky /* copy in the PSV control segment */
6357272f9cdSHans Petter Selasky memcpy(&wqe->psv, MLX5_ADDR_OF(sw_tls_cntx, ptag->crypto_params, progress),
6367272f9cdSHans Petter Selasky sizeof(wqe->psv));
6377272f9cdSHans Petter Selasky
6387272f9cdSHans Petter Selasky /* copy data for doorbell */
6397272f9cdSHans Petter Selasky memcpy(sq->doorbell.d32, &wqe->ctrl, sizeof(sq->doorbell.d32));
6407272f9cdSHans Petter Selasky
6417272f9cdSHans Petter Selasky sq->mbuf[pi].mbuf = NULL;
6427272f9cdSHans Petter Selasky sq->mbuf[pi].num_bytes = 0;
6437272f9cdSHans Petter Selasky sq->mbuf[pi].num_wqebbs = DIV_ROUND_UP(ds_cnt, MLX5_SEND_WQEBB_NUM_DS);
644ebdb7006SHans Petter Selasky sq->mbuf[pi].mst = m_snd_tag_ref(&ptag->tag);
645ebdb7006SHans Petter Selasky
6467272f9cdSHans Petter Selasky sq->pc += sq->mbuf[pi].num_wqebbs;
6477272f9cdSHans Petter Selasky }
6487272f9cdSHans Petter Selasky
6497272f9cdSHans Petter Selasky static void
mlx5e_tls_send_nop(struct mlx5e_sq * sq,struct mlx5e_tls_tag * ptag)6507272f9cdSHans Petter Selasky mlx5e_tls_send_nop(struct mlx5e_sq *sq, struct mlx5e_tls_tag *ptag)
6517272f9cdSHans Petter Selasky {
6527272f9cdSHans Petter Selasky const u32 ds_cnt = MLX5_SEND_WQEBB_NUM_DS;
6537272f9cdSHans Petter Selasky struct mlx5e_tx_wqe *wqe;
6547272f9cdSHans Petter Selasky u16 pi;
6557272f9cdSHans Petter Selasky
6567272f9cdSHans Petter Selasky pi = sq->pc & sq->wq.sz_m1;
6577272f9cdSHans Petter Selasky wqe = mlx5_wq_cyc_get_wqe(&sq->wq, pi);
6587272f9cdSHans Petter Selasky
6597272f9cdSHans Petter Selasky memset(&wqe->ctrl, 0, sizeof(wqe->ctrl));
6607272f9cdSHans Petter Selasky
6617272f9cdSHans Petter Selasky wqe->ctrl.opmod_idx_opcode = cpu_to_be32((sq->pc << 8) | MLX5_OPCODE_NOP);
6627272f9cdSHans Petter Selasky wqe->ctrl.qpn_ds = cpu_to_be32((sq->sqn << 8) | ds_cnt);
6637272f9cdSHans Petter Selasky if (mlx5e_do_send_cqe(sq))
6647272f9cdSHans Petter Selasky wqe->ctrl.fm_ce_se = MLX5_WQE_CTRL_CQ_UPDATE | MLX5_FENCE_MODE_INITIATOR_SMALL;
6657272f9cdSHans Petter Selasky else
6667272f9cdSHans Petter Selasky wqe->ctrl.fm_ce_se = MLX5_FENCE_MODE_INITIATOR_SMALL;
6677272f9cdSHans Petter Selasky
6687272f9cdSHans Petter Selasky /* Copy data for doorbell */
6697272f9cdSHans Petter Selasky memcpy(sq->doorbell.d32, &wqe->ctrl, sizeof(sq->doorbell.d32));
6707272f9cdSHans Petter Selasky
6717272f9cdSHans Petter Selasky sq->mbuf[pi].mbuf = NULL;
6727272f9cdSHans Petter Selasky sq->mbuf[pi].num_bytes = 0;
6737272f9cdSHans Petter Selasky sq->mbuf[pi].num_wqebbs = DIV_ROUND_UP(ds_cnt, MLX5_SEND_WQEBB_NUM_DS);
674ebdb7006SHans Petter Selasky sq->mbuf[pi].mst = m_snd_tag_ref(&ptag->tag);
675ebdb7006SHans Petter Selasky
6767272f9cdSHans Petter Selasky sq->pc += sq->mbuf[pi].num_wqebbs;
6777272f9cdSHans Petter Selasky }
6787272f9cdSHans Petter Selasky
6797272f9cdSHans Petter Selasky #define SBTLS_MBUF_NO_DATA ((struct mbuf *)1)
6807272f9cdSHans Petter Selasky
6817272f9cdSHans Petter Selasky static struct mbuf *
sbtls_recover_record(struct mbuf * mb,int wait,uint32_t tcp_old,uint32_t * ptcp_seq,bool * pis_start)68211304ef5SHans Petter Selasky sbtls_recover_record(struct mbuf *mb, int wait, uint32_t tcp_old, uint32_t *ptcp_seq, bool *pis_start)
6837272f9cdSHans Petter Selasky {
68423feb563SAndrew Gallatin struct mbuf *mr, *top;
6857272f9cdSHans Petter Selasky uint32_t offset;
6867272f9cdSHans Petter Selasky uint32_t delta;
6877272f9cdSHans Petter Selasky
6887272f9cdSHans Petter Selasky /* check format of incoming mbuf */
6897272f9cdSHans Petter Selasky if (mb->m_next == NULL ||
6906edfd179SGleb Smirnoff (mb->m_next->m_flags & (M_EXTPG | M_EXT)) != (M_EXTPG | M_EXT)) {
69123feb563SAndrew Gallatin top = NULL;
6927272f9cdSHans Petter Selasky goto done;
6937272f9cdSHans Petter Selasky }
6947272f9cdSHans Petter Selasky
6957272f9cdSHans Petter Selasky /* get unmapped data offset */
6967272f9cdSHans Petter Selasky offset = mtod(mb->m_next, uintptr_t);
6977272f9cdSHans Petter Selasky
6987272f9cdSHans Petter Selasky /* check if we don't need to re-transmit anything */
6997272f9cdSHans Petter Selasky if (offset == 0) {
70023feb563SAndrew Gallatin top = SBTLS_MBUF_NO_DATA;
70111304ef5SHans Petter Selasky *pis_start = true;
7027272f9cdSHans Petter Selasky goto done;
7037272f9cdSHans Petter Selasky }
7047272f9cdSHans Petter Selasky
70523feb563SAndrew Gallatin /* try to get a new packet header */
70623feb563SAndrew Gallatin top = m_gethdr(wait, MT_DATA);
70723feb563SAndrew Gallatin if (top == NULL)
7087272f9cdSHans Petter Selasky goto done;
7097272f9cdSHans Petter Selasky
71023feb563SAndrew Gallatin mr = m_get(wait, MT_DATA);
71123feb563SAndrew Gallatin if (mr == NULL) {
71223feb563SAndrew Gallatin m_free(top);
71323feb563SAndrew Gallatin top = NULL;
71423feb563SAndrew Gallatin goto done;
71523feb563SAndrew Gallatin }
71623feb563SAndrew Gallatin
71723feb563SAndrew Gallatin top->m_next = mr;
71823feb563SAndrew Gallatin
7197272f9cdSHans Petter Selasky mb_dupcl(mr, mb->m_next);
7207272f9cdSHans Petter Selasky
7217272f9cdSHans Petter Selasky /* the beginning of the TLS record */
7227272f9cdSHans Petter Selasky mr->m_data = NULL;
7237272f9cdSHans Petter Selasky
7247272f9cdSHans Petter Selasky /* setup packet header length */
72523feb563SAndrew Gallatin top->m_pkthdr.len = mr->m_len = offset;
72611304ef5SHans Petter Selasky top->m_len = 0;
7277272f9cdSHans Petter Selasky
7287272f9cdSHans Petter Selasky /* check for partial re-transmit */
7297272f9cdSHans Petter Selasky delta = *ptcp_seq - tcp_old;
7307272f9cdSHans Petter Selasky
7317272f9cdSHans Petter Selasky if (delta < offset) {
73211304ef5SHans Petter Selasky m_adj(top, offset - delta);
7337272f9cdSHans Petter Selasky offset = delta;
73411304ef5SHans Petter Selasky
73511304ef5SHans Petter Selasky /* continue where we left off */
73611304ef5SHans Petter Selasky *pis_start = false;
73711304ef5SHans Petter Selasky } else {
73811304ef5SHans Petter Selasky *pis_start = true;
7397272f9cdSHans Petter Selasky }
7407272f9cdSHans Petter Selasky
7417272f9cdSHans Petter Selasky /*
7427272f9cdSHans Petter Selasky * Rewind the TCP sequence number by the amount of data
7437272f9cdSHans Petter Selasky * retransmitted:
7447272f9cdSHans Petter Selasky */
7457272f9cdSHans Petter Selasky *ptcp_seq -= offset;
7467272f9cdSHans Petter Selasky done:
74723feb563SAndrew Gallatin return (top);
7487272f9cdSHans Petter Selasky }
7497272f9cdSHans Petter Selasky
7507272f9cdSHans Petter Selasky static int
mlx5e_sq_tls_populate(struct mbuf * mb,uint64_t * pseq)7517272f9cdSHans Petter Selasky mlx5e_sq_tls_populate(struct mbuf *mb, uint64_t *pseq)
7527272f9cdSHans Petter Selasky {
7537272f9cdSHans Petter Selasky
7547272f9cdSHans Petter Selasky for (; mb != NULL; mb = mb->m_next) {
7556edfd179SGleb Smirnoff if (!(mb->m_flags & M_EXTPG))
7567272f9cdSHans Petter Selasky continue;
7577b6c99d0SGleb Smirnoff *pseq = mb->m_epg_seqno;
7587272f9cdSHans Petter Selasky return (1);
7597272f9cdSHans Petter Selasky }
7607272f9cdSHans Petter Selasky return (0);
7617272f9cdSHans Petter Selasky }
7627272f9cdSHans Petter Selasky
7637272f9cdSHans Petter Selasky int
mlx5e_sq_tls_xmit(struct mlx5e_sq * sq,struct mlx5e_xmit_args * parg,struct mbuf ** ppmb)7647272f9cdSHans Petter Selasky mlx5e_sq_tls_xmit(struct mlx5e_sq *sq, struct mlx5e_xmit_args *parg, struct mbuf **ppmb)
7657272f9cdSHans Petter Selasky {
7667272f9cdSHans Petter Selasky struct mlx5e_tls_tag *ptls_tag;
76756fb710fSJohn Baldwin struct m_snd_tag *ptag;
7689eb1e4aaSHans Petter Selasky const struct tcphdr *th;
7697272f9cdSHans Petter Selasky struct mbuf *mb = *ppmb;
7707272f9cdSHans Petter Selasky u64 rcd_sn;
7717272f9cdSHans Petter Selasky u32 header_size;
7727272f9cdSHans Petter Selasky u32 mb_seq;
7737272f9cdSHans Petter Selasky
7747272f9cdSHans Petter Selasky if ((mb->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0)
7757272f9cdSHans Petter Selasky return (MLX5E_TLS_CONTINUE);
7767272f9cdSHans Petter Selasky
77756fb710fSJohn Baldwin ptag = mb->m_pkthdr.snd_tag;
7787272f9cdSHans Petter Selasky
7797272f9cdSHans Petter Selasky if (
780b7d92a66SJohn Baldwin #ifdef RATELIMIT
781c782ea8bSJohn Baldwin ptag->sw->type != IF_SND_TAG_TYPE_TLS_RATE_LIMIT &&
7827272f9cdSHans Petter Selasky #endif
783c782ea8bSJohn Baldwin ptag->sw->type != IF_SND_TAG_TYPE_TLS)
7847272f9cdSHans Petter Selasky return (MLX5E_TLS_CONTINUE);
7857272f9cdSHans Petter Selasky
7867272f9cdSHans Petter Selasky ptls_tag = container_of(ptag, struct mlx5e_tls_tag, tag);
7877272f9cdSHans Petter Selasky
7887272f9cdSHans Petter Selasky header_size = mlx5e_get_full_header_size(mb, &th);
7897272f9cdSHans Petter Selasky if (unlikely(header_size == 0 || th == NULL))
7907272f9cdSHans Petter Selasky return (MLX5E_TLS_FAILURE);
7917272f9cdSHans Petter Selasky
7927272f9cdSHans Petter Selasky /*
7937272f9cdSHans Petter Selasky * Send non-TLS TCP packets AS-IS:
7947272f9cdSHans Petter Selasky */
7957272f9cdSHans Petter Selasky if (header_size == mb->m_pkthdr.len ||
7967272f9cdSHans Petter Selasky mlx5e_sq_tls_populate(mb, &rcd_sn) == 0) {
7977272f9cdSHans Petter Selasky parg->tisn = 0;
7987272f9cdSHans Petter Selasky parg->ihs = header_size;
7997272f9cdSHans Petter Selasky return (MLX5E_TLS_CONTINUE);
8007272f9cdSHans Petter Selasky }
8017272f9cdSHans Petter Selasky
8027272f9cdSHans Petter Selasky mb_seq = ntohl(th->th_seq);
8037272f9cdSHans Petter Selasky
8047272f9cdSHans Petter Selasky MLX5E_TLS_TAG_LOCK(ptls_tag);
8057272f9cdSHans Petter Selasky switch (ptls_tag->state) {
8067272f9cdSHans Petter Selasky case MLX5E_TLS_ST_INIT:
8077272f9cdSHans Petter Selasky MLX5E_TLS_TAG_UNLOCK(ptls_tag);
8087272f9cdSHans Petter Selasky return (MLX5E_TLS_FAILURE);
8097272f9cdSHans Petter Selasky case MLX5E_TLS_ST_SETUP:
81078ae1e6eSHans Petter Selasky ptls_tag->state = MLX5E_TLS_ST_TXRDY;
81178ae1e6eSHans Petter Selasky ptls_tag->expected_seq = ~mb_seq; /* force setup */
8127272f9cdSHans Petter Selasky default:
8137272f9cdSHans Petter Selasky MLX5E_TLS_TAG_UNLOCK(ptls_tag);
8147272f9cdSHans Petter Selasky break;
8157272f9cdSHans Petter Selasky }
8167272f9cdSHans Petter Selasky
8177272f9cdSHans Petter Selasky if (unlikely(ptls_tag->expected_seq != mb_seq)) {
81811304ef5SHans Petter Selasky bool is_start;
8197272f9cdSHans Petter Selasky struct mbuf *r_mb;
8207272f9cdSHans Petter Selasky uint32_t tcp_seq = mb_seq;
8217272f9cdSHans Petter Selasky
82211304ef5SHans Petter Selasky r_mb = sbtls_recover_record(mb, M_NOWAIT, ptls_tag->expected_seq, &tcp_seq, &is_start);
8237272f9cdSHans Petter Selasky if (r_mb == NULL) {
8247272f9cdSHans Petter Selasky MLX5E_TLS_STAT_INC(ptls_tag, tx_error, 1);
8257272f9cdSHans Petter Selasky return (MLX5E_TLS_FAILURE);
8267272f9cdSHans Petter Selasky }
8277272f9cdSHans Petter Selasky
8287272f9cdSHans Petter Selasky MLX5E_TLS_STAT_INC(ptls_tag, tx_packets_ooo, 1);
8297272f9cdSHans Petter Selasky
8307272f9cdSHans Petter Selasky /* check if this is the first fragment of a TLS record */
83111304ef5SHans Petter Selasky if (is_start) {
8327272f9cdSHans Petter Selasky /* setup TLS static parameters */
8337272f9cdSHans Petter Selasky MLX5_SET64(sw_tls_cntx, ptls_tag->crypto_params,
8347272f9cdSHans Petter Selasky param.initial_record_number, rcd_sn);
8357272f9cdSHans Petter Selasky
8367272f9cdSHans Petter Selasky /*
8377272f9cdSHans Petter Selasky * NOTE: The sendqueue should have enough room to
8387272f9cdSHans Petter Selasky * carry both the static and the progress parameters
8397272f9cdSHans Petter Selasky * when we get here!
8407272f9cdSHans Petter Selasky */
8417272f9cdSHans Petter Selasky mlx5e_tls_send_static_parameters(sq, ptls_tag);
8427272f9cdSHans Petter Selasky mlx5e_tls_send_progress_parameters(sq, ptls_tag);
8437272f9cdSHans Petter Selasky
8447272f9cdSHans Petter Selasky if (r_mb == SBTLS_MBUF_NO_DATA) {
8457272f9cdSHans Petter Selasky mlx5e_tls_send_nop(sq, ptls_tag);
8467272f9cdSHans Petter Selasky ptls_tag->expected_seq = mb_seq;
8477272f9cdSHans Petter Selasky return (MLX5E_TLS_LOOP);
8487272f9cdSHans Petter Selasky }
8497272f9cdSHans Petter Selasky }
8507272f9cdSHans Petter Selasky
8517272f9cdSHans Petter Selasky MLX5E_TLS_STAT_INC(ptls_tag, tx_bytes_ooo, r_mb->m_pkthdr.len);
8527272f9cdSHans Petter Selasky
8537272f9cdSHans Petter Selasky /* setup transmit arguments */
8547272f9cdSHans Petter Selasky parg->tisn = ptls_tag->tisn;
855ebdb7006SHans Petter Selasky parg->mst = &ptls_tag->tag;
8567272f9cdSHans Petter Selasky
8577272f9cdSHans Petter Selasky /* try to send DUMP data */
8587272f9cdSHans Petter Selasky if (mlx5e_sq_dump_xmit(sq, parg, &r_mb) != 0) {
8597272f9cdSHans Petter Selasky m_freem(r_mb);
8607272f9cdSHans Petter Selasky ptls_tag->expected_seq = tcp_seq;
8617272f9cdSHans Petter Selasky return (MLX5E_TLS_FAILURE);
8627272f9cdSHans Petter Selasky } else {
8637272f9cdSHans Petter Selasky ptls_tag->expected_seq = mb_seq;
8647272f9cdSHans Petter Selasky return (MLX5E_TLS_LOOP);
8657272f9cdSHans Petter Selasky }
8667272f9cdSHans Petter Selasky } else {
8677272f9cdSHans Petter Selasky MLX5E_TLS_STAT_INC(ptls_tag, tx_packets, 1);
8687272f9cdSHans Petter Selasky MLX5E_TLS_STAT_INC(ptls_tag, tx_bytes, mb->m_pkthdr.len);
8697272f9cdSHans Petter Selasky }
8707272f9cdSHans Petter Selasky ptls_tag->expected_seq += mb->m_pkthdr.len - header_size;
8717272f9cdSHans Petter Selasky
8727272f9cdSHans Petter Selasky parg->tisn = ptls_tag->tisn;
8737272f9cdSHans Petter Selasky parg->ihs = header_size;
874ebdb7006SHans Petter Selasky parg->mst = &ptls_tag->tag;
8757272f9cdSHans Petter Selasky return (MLX5E_TLS_CONTINUE);
8767272f9cdSHans Petter Selasky }
8777272f9cdSHans Petter Selasky
8787272f9cdSHans Petter Selasky #else
8797272f9cdSHans Petter Selasky
8807272f9cdSHans Petter Selasky int
mlx5e_tls_init(struct mlx5e_priv * priv)8817272f9cdSHans Petter Selasky mlx5e_tls_init(struct mlx5e_priv *priv)
8827272f9cdSHans Petter Selasky {
8837272f9cdSHans Petter Selasky
8847272f9cdSHans Petter Selasky return (0);
8857272f9cdSHans Petter Selasky }
8867272f9cdSHans Petter Selasky
8877272f9cdSHans Petter Selasky void
mlx5e_tls_cleanup(struct mlx5e_priv * priv)8887272f9cdSHans Petter Selasky mlx5e_tls_cleanup(struct mlx5e_priv *priv)
8897272f9cdSHans Petter Selasky {
8907272f9cdSHans Petter Selasky /* NOP */
8917272f9cdSHans Petter Selasky }
8927272f9cdSHans Petter Selasky
8937272f9cdSHans Petter Selasky #endif /* KERN_TLS */
894