root/drivers/net/ethernet/mellanox/mlxsw/spectrum_acl_bloom_filter.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. mlxsw_sp_acl_bf_crc_byte
  2. mlxsw_sp_acl_bf_crc
  3. mlxsw_sp_acl_bf_key_encode
  4. mlxsw_sp_acl_bf_rule_count_index_get
  5. mlxsw_sp_acl_bf_index_get
  6. mlxsw_sp_acl_bf_entry_add
  7. mlxsw_sp_acl_bf_entry_del
  8. mlxsw_sp_acl_bf_init
  9. mlxsw_sp_acl_bf_fini

   1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
   2 /* Copyright (c) 2018 Mellanox Technologies. All rights reserved */
   3 
   4 #include <linux/errno.h>
   5 #include <linux/gfp.h>
   6 #include <linux/kernel.h>
   7 #include <linux/refcount.h>
   8 #include <linux/mutex.h>
   9 
  10 #include "spectrum.h"
  11 #include "spectrum_acl_tcam.h"
  12 
  13 struct mlxsw_sp_acl_bf {
  14         struct mutex lock; /* Protects Bloom Filter updates. */
  15         unsigned int bank_size;
  16         refcount_t refcnt[0];
  17 };
  18 
  19 /* Bloom filter uses a crc-16 hash over chunks of data which contain 4 key
  20  * blocks, eRP ID and region ID. In Spectrum-2, region key is combined of up to
  21  * 12 key blocks, so there can be up to 3 chunks in the Bloom filter key,
  22  * depending on the actual number of key blocks used in the region.
  23  * The layout of the Bloom filter key is as follows:
  24  *
  25  * +-------------------------+------------------------+------------------------+
  26  * | Chunk 2 Key blocks 11-8 | Chunk 1 Key blocks 7-4 | Chunk 0 Key blocks 3-0 |
  27  * +-------------------------+------------------------+------------------------+
  28  */
  29 #define MLXSW_BLOOM_KEY_CHUNKS 3
  30 #define MLXSW_BLOOM_KEY_LEN 69
  31 
  32 /* Each chunk size is 23 bytes. 18 bytes of it contain 4 key blocks, each is
  33  * 36 bits, 2 bytes which hold eRP ID and region ID, and 3 bytes of zero
  34  * padding.
  35  * The layout of each chunk is as follows:
  36  *
  37  * +---------+----------------------+-----------------------------------+
  38  * | 3 bytes |        2 bytes       |              18 bytes             |
  39  * +---------+-----------+----------+-----------------------------------+
  40  * | 183:158 |  157:148  | 147:144  |               143:0               |
  41  * +---------+-----------+----------+-----------------------------------+
  42  * |    0    | region ID |  eRP ID  |      4 Key blocks (18 Bytes)      |
  43  * +---------+-----------+----------+-----------------------------------+
  44  */
  45 #define MLXSW_BLOOM_CHUNK_PAD_BYTES 3
  46 #define MLXSW_BLOOM_CHUNK_KEY_BYTES 18
  47 #define MLXSW_BLOOM_KEY_CHUNK_BYTES 23
  48 
  49 /* The offset of the key block within a chunk is 5 bytes as it comes after
  50  * 3 bytes of zero padding and 16 bits of region ID and eRP ID.
  51  */
  52 #define MLXSW_BLOOM_CHUNK_KEY_OFFSET 5
  53 
  54 /* Each chunk contains 4 key blocks. Chunk 2 uses key blocks 11-8,
  55  * and we need to populate it with 4 key blocks copied from the entry encoded
  56  * key. Since the encoded key contains a padding, key block 11 starts at offset
  57  * 2. block 7 that is used in chunk 1 starts at offset 20 as 4 key blocks take
  58  * 18 bytes.
  59  * This array defines key offsets for easy access when copying key blocks from
  60  * entry key to Bloom filter chunk.
  61  */
  62 static const u8 chunk_key_offsets[MLXSW_BLOOM_KEY_CHUNKS] = {2, 20, 38};
  63 
  64 /* This table is just the CRC of each possible byte. It is
  65  * computed, Msbit first, for the Bloom filter polynomial
  66  * which is 0x8529 (1 + x^3 + x^5 + x^8 + x^10 + x^15 and
  67  * the implicit x^16).
  68  */
  69 static const u16 mlxsw_sp_acl_bf_crc_tab[256] = {
  70 0x0000, 0x8529, 0x8f7b, 0x0a52, 0x9bdf, 0x1ef6, 0x14a4, 0x918d,
  71 0xb297, 0x37be, 0x3dec, 0xb8c5, 0x2948, 0xac61, 0xa633, 0x231a,
  72 0xe007, 0x652e, 0x6f7c, 0xea55, 0x7bd8, 0xfef1, 0xf4a3, 0x718a,
  73 0x5290, 0xd7b9, 0xddeb, 0x58c2, 0xc94f, 0x4c66, 0x4634, 0xc31d,
  74 0x4527, 0xc00e, 0xca5c, 0x4f75, 0xdef8, 0x5bd1, 0x5183, 0xd4aa,
  75 0xf7b0, 0x7299, 0x78cb, 0xfde2, 0x6c6f, 0xe946, 0xe314, 0x663d,
  76 0xa520, 0x2009, 0x2a5b, 0xaf72, 0x3eff, 0xbbd6, 0xb184, 0x34ad,
  77 0x17b7, 0x929e, 0x98cc, 0x1de5, 0x8c68, 0x0941, 0x0313, 0x863a,
  78 0x8a4e, 0x0f67, 0x0535, 0x801c, 0x1191, 0x94b8, 0x9eea, 0x1bc3,
  79 0x38d9, 0xbdf0, 0xb7a2, 0x328b, 0xa306, 0x262f, 0x2c7d, 0xa954,
  80 0x6a49, 0xef60, 0xe532, 0x601b, 0xf196, 0x74bf, 0x7eed, 0xfbc4,
  81 0xd8de, 0x5df7, 0x57a5, 0xd28c, 0x4301, 0xc628, 0xcc7a, 0x4953,
  82 0xcf69, 0x4a40, 0x4012, 0xc53b, 0x54b6, 0xd19f, 0xdbcd, 0x5ee4,
  83 0x7dfe, 0xf8d7, 0xf285, 0x77ac, 0xe621, 0x6308, 0x695a, 0xec73,
  84 0x2f6e, 0xaa47, 0xa015, 0x253c, 0xb4b1, 0x3198, 0x3bca, 0xbee3,
  85 0x9df9, 0x18d0, 0x1282, 0x97ab, 0x0626, 0x830f, 0x895d, 0x0c74,
  86 0x91b5, 0x149c, 0x1ece, 0x9be7, 0x0a6a, 0x8f43, 0x8511, 0x0038,
  87 0x2322, 0xa60b, 0xac59, 0x2970, 0xb8fd, 0x3dd4, 0x3786, 0xb2af,
  88 0x71b2, 0xf49b, 0xfec9, 0x7be0, 0xea6d, 0x6f44, 0x6516, 0xe03f,
  89 0xc325, 0x460c, 0x4c5e, 0xc977, 0x58fa, 0xddd3, 0xd781, 0x52a8,
  90 0xd492, 0x51bb, 0x5be9, 0xdec0, 0x4f4d, 0xca64, 0xc036, 0x451f,
  91 0x6605, 0xe32c, 0xe97e, 0x6c57, 0xfdda, 0x78f3, 0x72a1, 0xf788,
  92 0x3495, 0xb1bc, 0xbbee, 0x3ec7, 0xaf4a, 0x2a63, 0x2031, 0xa518,
  93 0x8602, 0x032b, 0x0979, 0x8c50, 0x1ddd, 0x98f4, 0x92a6, 0x178f,
  94 0x1bfb, 0x9ed2, 0x9480, 0x11a9, 0x8024, 0x050d, 0x0f5f, 0x8a76,
  95 0xa96c, 0x2c45, 0x2617, 0xa33e, 0x32b3, 0xb79a, 0xbdc8, 0x38e1,
  96 0xfbfc, 0x7ed5, 0x7487, 0xf1ae, 0x6023, 0xe50a, 0xef58, 0x6a71,
  97 0x496b, 0xcc42, 0xc610, 0x4339, 0xd2b4, 0x579d, 0x5dcf, 0xd8e6,
  98 0x5edc, 0xdbf5, 0xd1a7, 0x548e, 0xc503, 0x402a, 0x4a78, 0xcf51,
  99 0xec4b, 0x6962, 0x6330, 0xe619, 0x7794, 0xf2bd, 0xf8ef, 0x7dc6,
 100 0xbedb, 0x3bf2, 0x31a0, 0xb489, 0x2504, 0xa02d, 0xaa7f, 0x2f56,
 101 0x0c4c, 0x8965, 0x8337, 0x061e, 0x9793, 0x12ba, 0x18e8, 0x9dc1,
 102 };
 103 
 104 static u16 mlxsw_sp_acl_bf_crc_byte(u16 crc, u8 c)
 105 {
 106         return (crc << 8) ^ mlxsw_sp_acl_bf_crc_tab[(crc >> 8) ^ c];
 107 }
 108 
 109 static u16 mlxsw_sp_acl_bf_crc(const u8 *buffer, size_t len)
 110 {
 111         u16 crc = 0;
 112 
 113         while (len--)
 114                 crc = mlxsw_sp_acl_bf_crc_byte(crc, *buffer++);
 115         return crc;
 116 }
 117 
 118 static void
 119 mlxsw_sp_acl_bf_key_encode(struct mlxsw_sp_acl_atcam_region *aregion,
 120                            struct mlxsw_sp_acl_atcam_entry *aentry,
 121                            char *output, u8 *len)
 122 {
 123         struct mlxsw_afk_key_info *key_info = aregion->region->key_info;
 124         u8 chunk_index, chunk_count, block_count;
 125         char *chunk = output;
 126         __be16 erp_region_id;
 127 
 128         block_count = mlxsw_afk_key_info_blocks_count_get(key_info);
 129         chunk_count = 1 + ((block_count - 1) >> 2);
 130         erp_region_id = cpu_to_be16(aentry->ht_key.erp_id |
 131                                    (aregion->region->id << 4));
 132         for (chunk_index = MLXSW_BLOOM_KEY_CHUNKS - chunk_count;
 133              chunk_index < MLXSW_BLOOM_KEY_CHUNKS; chunk_index++) {
 134                 memset(chunk, 0, MLXSW_BLOOM_CHUNK_PAD_BYTES);
 135                 memcpy(chunk + MLXSW_BLOOM_CHUNK_PAD_BYTES, &erp_region_id,
 136                        sizeof(erp_region_id));
 137                 memcpy(chunk + MLXSW_BLOOM_CHUNK_KEY_OFFSET,
 138                        &aentry->enc_key[chunk_key_offsets[chunk_index]],
 139                        MLXSW_BLOOM_CHUNK_KEY_BYTES);
 140                 chunk += MLXSW_BLOOM_KEY_CHUNK_BYTES;
 141         }
 142         *len = chunk_count * MLXSW_BLOOM_KEY_CHUNK_BYTES;
 143 }
 144 
 145 static unsigned int
 146 mlxsw_sp_acl_bf_rule_count_index_get(struct mlxsw_sp_acl_bf *bf,
 147                                      unsigned int erp_bank,
 148                                      unsigned int bf_index)
 149 {
 150         return erp_bank * bf->bank_size + bf_index;
 151 }
 152 
 153 static unsigned int
 154 mlxsw_sp_acl_bf_index_get(struct mlxsw_sp_acl_bf *bf,
 155                           struct mlxsw_sp_acl_atcam_region *aregion,
 156                           struct mlxsw_sp_acl_atcam_entry *aentry)
 157 {
 158         char bf_key[MLXSW_BLOOM_KEY_LEN];
 159         u8 bf_size;
 160 
 161         mlxsw_sp_acl_bf_key_encode(aregion, aentry, bf_key, &bf_size);
 162         return mlxsw_sp_acl_bf_crc(bf_key, bf_size);
 163 }
 164 
 165 int
 166 mlxsw_sp_acl_bf_entry_add(struct mlxsw_sp *mlxsw_sp,
 167                           struct mlxsw_sp_acl_bf *bf,
 168                           struct mlxsw_sp_acl_atcam_region *aregion,
 169                           unsigned int erp_bank,
 170                           struct mlxsw_sp_acl_atcam_entry *aentry)
 171 {
 172         unsigned int rule_index;
 173         char *peabfe_pl;
 174         u16 bf_index;
 175         int err;
 176 
 177         mutex_lock(&bf->lock);
 178 
 179         bf_index = mlxsw_sp_acl_bf_index_get(bf, aregion, aentry);
 180         rule_index = mlxsw_sp_acl_bf_rule_count_index_get(bf, erp_bank,
 181                                                           bf_index);
 182 
 183         if (refcount_inc_not_zero(&bf->refcnt[rule_index])) {
 184                 err = 0;
 185                 goto unlock;
 186         }
 187 
 188         peabfe_pl = kmalloc(MLXSW_REG_PEABFE_LEN, GFP_KERNEL);
 189         if (!peabfe_pl) {
 190                 err = -ENOMEM;
 191                 goto unlock;
 192         }
 193 
 194         mlxsw_reg_peabfe_pack(peabfe_pl);
 195         mlxsw_reg_peabfe_rec_pack(peabfe_pl, 0, 1, erp_bank, bf_index);
 196         err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(peabfe), peabfe_pl);
 197         kfree(peabfe_pl);
 198         if (err)
 199                 goto unlock;
 200 
 201         refcount_set(&bf->refcnt[rule_index], 1);
 202         err = 0;
 203 
 204 unlock:
 205         mutex_unlock(&bf->lock);
 206         return err;
 207 }
 208 
 209 void
 210 mlxsw_sp_acl_bf_entry_del(struct mlxsw_sp *mlxsw_sp,
 211                           struct mlxsw_sp_acl_bf *bf,
 212                           struct mlxsw_sp_acl_atcam_region *aregion,
 213                           unsigned int erp_bank,
 214                           struct mlxsw_sp_acl_atcam_entry *aentry)
 215 {
 216         unsigned int rule_index;
 217         char *peabfe_pl;
 218         u16 bf_index;
 219 
 220         mutex_lock(&bf->lock);
 221 
 222         bf_index = mlxsw_sp_acl_bf_index_get(bf, aregion, aentry);
 223         rule_index = mlxsw_sp_acl_bf_rule_count_index_get(bf, erp_bank,
 224                                                           bf_index);
 225 
 226         if (refcount_dec_and_test(&bf->refcnt[rule_index])) {
 227                 peabfe_pl = kmalloc(MLXSW_REG_PEABFE_LEN, GFP_KERNEL);
 228                 if (!peabfe_pl)
 229                         goto unlock;
 230 
 231                 mlxsw_reg_peabfe_pack(peabfe_pl);
 232                 mlxsw_reg_peabfe_rec_pack(peabfe_pl, 0, 0, erp_bank, bf_index);
 233                 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(peabfe), peabfe_pl);
 234                 kfree(peabfe_pl);
 235         }
 236 
 237 unlock:
 238         mutex_unlock(&bf->lock);
 239 }
 240 
 241 struct mlxsw_sp_acl_bf *
 242 mlxsw_sp_acl_bf_init(struct mlxsw_sp *mlxsw_sp, unsigned int num_erp_banks)
 243 {
 244         struct mlxsw_sp_acl_bf *bf;
 245         unsigned int bf_bank_size;
 246 
 247         if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, ACL_MAX_BF_LOG))
 248                 return ERR_PTR(-EIO);
 249 
 250         /* Bloom filter size per erp_table_bank
 251          * is 2^ACL_MAX_BF_LOG
 252          */
 253         bf_bank_size = 1 << MLXSW_CORE_RES_GET(mlxsw_sp->core, ACL_MAX_BF_LOG);
 254         bf = kzalloc(struct_size(bf, refcnt, bf_bank_size * num_erp_banks),
 255                      GFP_KERNEL);
 256         if (!bf)
 257                 return ERR_PTR(-ENOMEM);
 258 
 259         bf->bank_size = bf_bank_size;
 260         mutex_init(&bf->lock);
 261 
 262         return bf;
 263 }
 264 
 265 void mlxsw_sp_acl_bf_fini(struct mlxsw_sp_acl_bf *bf)
 266 {
 267         mutex_destroy(&bf->lock);
 268         kfree(bf);
 269 }

/* [<][>][^][v][top][bottom][index][help] */