|
| 1 | +/* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */ |
| 2 | +/* Copyright (c) 2019 Mellanox Technologies. */ |
| 3 | + |
| 4 | +#include <linux/module.h> |
| 5 | +#include <linux/mlx5/driver.h> |
| 6 | +#include <linux/mlx5/port.h> |
| 7 | +#include <linux/mlx5/cmd.h> |
| 8 | +#include "mlx5_core.h" |
| 9 | +#include "lib/port_tun.h" |
| 10 | + |
| 11 | +struct mlx5_port_tun_entropy_flags { |
| 12 | + bool force_supported, force_enabled; |
| 13 | + bool calc_supported, calc_enabled; |
| 14 | + bool gre_calc_supported, gre_calc_enabled; |
| 15 | +}; |
| 16 | + |
| 17 | +static void mlx5_query_port_tun_entropy(struct mlx5_core_dev *mdev, |
| 18 | + struct mlx5_port_tun_entropy_flags *entropy_flags) |
| 19 | +{ |
| 20 | + u32 out[MLX5_ST_SZ_DW(pcmr_reg)]; |
| 21 | + /* Default values for FW which do not support MLX5_REG_PCMR */ |
| 22 | + entropy_flags->force_supported = false; |
| 23 | + entropy_flags->calc_supported = false; |
| 24 | + entropy_flags->gre_calc_supported = false; |
| 25 | + entropy_flags->force_enabled = false; |
| 26 | + entropy_flags->calc_enabled = true; |
| 27 | + entropy_flags->gre_calc_enabled = true; |
| 28 | + |
| 29 | + if (!MLX5_CAP_GEN(mdev, ports_check)) |
| 30 | + return; |
| 31 | + |
| 32 | + if (mlx5_query_ports_check(mdev, out, sizeof(out))) |
| 33 | + return; |
| 34 | + |
| 35 | + entropy_flags->force_supported = !!(MLX5_GET(pcmr_reg, out, entropy_force_cap)); |
| 36 | + entropy_flags->calc_supported = !!(MLX5_GET(pcmr_reg, out, entropy_calc_cap)); |
| 37 | + entropy_flags->gre_calc_supported = !!(MLX5_GET(pcmr_reg, out, entropy_gre_calc_cap)); |
| 38 | + entropy_flags->force_enabled = !!(MLX5_GET(pcmr_reg, out, entropy_force)); |
| 39 | + entropy_flags->calc_enabled = !!(MLX5_GET(pcmr_reg, out, entropy_calc)); |
| 40 | + entropy_flags->gre_calc_enabled = !!(MLX5_GET(pcmr_reg, out, entropy_gre_calc)); |
| 41 | +} |
| 42 | + |
| 43 | +static int mlx5_set_port_tun_entropy_calc(struct mlx5_core_dev *mdev, u8 enable, |
| 44 | + u8 force) |
| 45 | +{ |
| 46 | + u32 in[MLX5_ST_SZ_DW(pcmr_reg)] = {0}; |
| 47 | + int err; |
| 48 | + |
| 49 | + err = mlx5_query_ports_check(mdev, in, sizeof(in)); |
| 50 | + if (err) |
| 51 | + return err; |
| 52 | + MLX5_SET(pcmr_reg, in, local_port, 1); |
| 53 | + MLX5_SET(pcmr_reg, in, entropy_force, force); |
| 54 | + MLX5_SET(pcmr_reg, in, entropy_calc, enable); |
| 55 | + return mlx5_set_ports_check(mdev, in, sizeof(in)); |
| 56 | +} |
| 57 | + |
| 58 | +static int mlx5_set_port_gre_tun_entropy_calc(struct mlx5_core_dev *mdev, |
| 59 | + u8 enable, u8 force) |
| 60 | +{ |
| 61 | + u32 in[MLX5_ST_SZ_DW(pcmr_reg)] = {0}; |
| 62 | + int err; |
| 63 | + |
| 64 | + err = mlx5_query_ports_check(mdev, in, sizeof(in)); |
| 65 | + if (err) |
| 66 | + return err; |
| 67 | + MLX5_SET(pcmr_reg, in, local_port, 1); |
| 68 | + MLX5_SET(pcmr_reg, in, entropy_force, force); |
| 69 | + MLX5_SET(pcmr_reg, in, entropy_gre_calc, enable); |
| 70 | + return mlx5_set_ports_check(mdev, in, sizeof(in)); |
| 71 | +} |
| 72 | + |
| 73 | +void mlx5_init_port_tun_entropy(struct mlx5_tun_entropy *tun_entropy, |
| 74 | + struct mlx5_core_dev *mdev) |
| 75 | +{ |
| 76 | + struct mlx5_port_tun_entropy_flags entropy_flags; |
| 77 | + |
| 78 | + tun_entropy->mdev = mdev; |
| 79 | + mutex_init(&tun_entropy->lock); |
| 80 | + mlx5_query_port_tun_entropy(mdev, &entropy_flags); |
| 81 | + tun_entropy->num_enabling_entries = 0; |
| 82 | + tun_entropy->num_disabling_entries = 0; |
| 83 | + tun_entropy->enabled = entropy_flags.calc_enabled; |
| 84 | + tun_entropy->enabled = |
| 85 | + (entropy_flags.calc_supported) ? |
| 86 | + entropy_flags.calc_enabled : true; |
| 87 | +} |
| 88 | + |
| 89 | +static int mlx5_set_entropy(struct mlx5_tun_entropy *tun_entropy, |
| 90 | + int reformat_type, bool enable) |
| 91 | +{ |
| 92 | + struct mlx5_port_tun_entropy_flags entropy_flags; |
| 93 | + int err; |
| 94 | + |
| 95 | + mlx5_query_port_tun_entropy(tun_entropy->mdev, &entropy_flags); |
| 96 | + /* Tunnel entropy calculation may be controlled either on port basis |
| 97 | + * for all tunneling protocols or specifically for GRE protocol. |
| 98 | + * Prioritize GRE protocol control (if capable) over global port |
| 99 | + * configuration. |
| 100 | + */ |
| 101 | + if (entropy_flags.gre_calc_supported && |
| 102 | + reformat_type == MLX5_REFORMAT_TYPE_L2_TO_NVGRE) { |
| 103 | + /* Other applications may change the global FW entropy |
| 104 | + * calculations settings. Check that the current entropy value |
| 105 | + * is the negative of the updated value. |
| 106 | + */ |
| 107 | + if (entropy_flags.force_enabled && |
| 108 | + enable == entropy_flags.gre_calc_enabled) { |
| 109 | + mlx5_core_warn(tun_entropy->mdev, |
| 110 | + "Unexpected GRE entropy calc setting - expected %d", |
| 111 | + !entropy_flags.gre_calc_enabled); |
| 112 | + return -EOPNOTSUPP; |
| 113 | + } |
| 114 | + err = mlx5_set_port_gre_tun_entropy_calc(tun_entropy->mdev, enable, |
| 115 | + entropy_flags.force_supported); |
| 116 | + if (err) |
| 117 | + return err; |
| 118 | + /* if we turn on the entropy we don't need to force it anymore */ |
| 119 | + if (entropy_flags.force_supported && enable) { |
| 120 | + err = mlx5_set_port_gre_tun_entropy_calc(tun_entropy->mdev, 1, 0); |
| 121 | + if (err) |
| 122 | + return err; |
| 123 | + } |
| 124 | + } else if (entropy_flags.calc_supported) { |
| 125 | + /* Other applications may change the global FW entropy |
| 126 | + * calculations settings. Check that the current entropy value |
| 127 | + * is the negative of the updated value. |
| 128 | + */ |
| 129 | + if (entropy_flags.force_enabled && |
| 130 | + enable == entropy_flags.calc_enabled) { |
| 131 | + mlx5_core_warn(tun_entropy->mdev, |
| 132 | + "Unexpected entropy calc setting - expected %d", |
| 133 | + !entropy_flags.calc_enabled); |
| 134 | + return -EOPNOTSUPP; |
| 135 | + } |
| 136 | + /* GRE requires disabling entropy calculation. if there are |
| 137 | + * enabling entries (i.e VXLAN) we cannot turn it off for them, |
| 138 | + * thus fail. |
| 139 | + */ |
| 140 | + if (tun_entropy->num_enabling_entries) |
| 141 | + return -EOPNOTSUPP; |
| 142 | + err = mlx5_set_port_tun_entropy_calc(tun_entropy->mdev, enable, |
| 143 | + entropy_flags.force_supported); |
| 144 | + if (err) |
| 145 | + return err; |
| 146 | + tun_entropy->enabled = enable; |
| 147 | + /* if we turn on the entropy we don't need to force it anymore */ |
| 148 | + if (entropy_flags.force_supported && enable) { |
| 149 | + err = mlx5_set_port_tun_entropy_calc(tun_entropy->mdev, 1, 0); |
| 150 | + if (err) |
| 151 | + return err; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + return 0; |
| 156 | +} |
| 157 | + |
| 158 | +/* the function manages the refcount for enabling/disabling tunnel types. |
| 159 | + * the return value indicates if the inc is successful or not, depending on |
| 160 | + * entropy capabilities and configuration. |
| 161 | + */ |
| 162 | +int mlx5_tun_entropy_refcount_inc(struct mlx5_tun_entropy *tun_entropy, |
| 163 | + int reformat_type) |
| 164 | +{ |
| 165 | + /* the default is error for unknown (non VXLAN/GRE tunnel types) */ |
| 166 | + int err = -EOPNOTSUPP; |
| 167 | + |
| 168 | + mutex_lock(&tun_entropy->lock); |
| 169 | + if (reformat_type == MLX5_REFORMAT_TYPE_L2_TO_VXLAN && |
| 170 | + tun_entropy->enabled) { |
| 171 | + /* in case entropy calculation is enabled for all tunneling |
| 172 | + * types, it is ok for VXLAN, so approve. |
| 173 | + * otherwise keep the error default. |
| 174 | + */ |
| 175 | + tun_entropy->num_enabling_entries++; |
| 176 | + err = 0; |
| 177 | + } else if (reformat_type == MLX5_REFORMAT_TYPE_L2_TO_NVGRE) { |
| 178 | + /* turn off the entropy only for the first GRE rule. |
| 179 | + * for the next rules the entropy was already disabled |
| 180 | + * successfully. |
| 181 | + */ |
| 182 | + if (tun_entropy->num_disabling_entries == 0) |
| 183 | + err = mlx5_set_entropy(tun_entropy, reformat_type, 0); |
| 184 | + else |
| 185 | + err = 0; |
| 186 | + if (!err) |
| 187 | + tun_entropy->num_disabling_entries++; |
| 188 | + } |
| 189 | + mutex_unlock(&tun_entropy->lock); |
| 190 | + |
| 191 | + return err; |
| 192 | +} |
| 193 | + |
| 194 | +void mlx5_tun_entropy_refcount_dec(struct mlx5_tun_entropy *tun_entropy, |
| 195 | + int reformat_type) |
| 196 | +{ |
| 197 | + mutex_lock(&tun_entropy->lock); |
| 198 | + if (reformat_type == MLX5_REFORMAT_TYPE_L2_TO_VXLAN) |
| 199 | + tun_entropy->num_enabling_entries--; |
| 200 | + else if (reformat_type == MLX5_REFORMAT_TYPE_L2_TO_NVGRE && |
| 201 | + --tun_entropy->num_disabling_entries == 0) |
| 202 | + mlx5_set_entropy(tun_entropy, reformat_type, 1); |
| 203 | + mutex_unlock(&tun_entropy->lock); |
| 204 | +} |
| 205 | + |
0 commit comments