19948a064SJiri Pirko // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
29948a064SJiri Pirko /* Copyright (c) 2017-2018 Mellanox Technologies. All rights reserved */
364eccd00SJiri Pirko
464eccd00SJiri Pirko #include <linux/kernel.h>
564eccd00SJiri Pirko #include <linux/errno.h>
664eccd00SJiri Pirko #include <linux/parman.h>
764eccd00SJiri Pirko
864eccd00SJiri Pirko #include "reg.h"
964eccd00SJiri Pirko #include "core.h"
1064eccd00SJiri Pirko #include "spectrum.h"
1164eccd00SJiri Pirko #include "spectrum_acl_tcam.h"
1264eccd00SJiri Pirko
1364eccd00SJiri Pirko static int
mlxsw_sp_acl_ctcam_region_resize(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam_region * region,u16 new_size)1464eccd00SJiri Pirko mlxsw_sp_acl_ctcam_region_resize(struct mlxsw_sp *mlxsw_sp,
1564eccd00SJiri Pirko struct mlxsw_sp_acl_tcam_region *region,
1664eccd00SJiri Pirko u16 new_size)
1764eccd00SJiri Pirko {
1864eccd00SJiri Pirko char ptar_pl[MLXSW_REG_PTAR_LEN];
1964eccd00SJiri Pirko
2064eccd00SJiri Pirko mlxsw_reg_ptar_pack(ptar_pl, MLXSW_REG_PTAR_OP_RESIZE,
2164eccd00SJiri Pirko region->key_type, new_size, region->id,
2264eccd00SJiri Pirko region->tcam_region_info);
2364eccd00SJiri Pirko return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptar), ptar_pl);
2464eccd00SJiri Pirko }
2564eccd00SJiri Pirko
2664eccd00SJiri Pirko static void
mlxsw_sp_acl_ctcam_region_move(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam_region * region,u16 src_offset,u16 dst_offset,u16 size)2764eccd00SJiri Pirko mlxsw_sp_acl_ctcam_region_move(struct mlxsw_sp *mlxsw_sp,
2864eccd00SJiri Pirko struct mlxsw_sp_acl_tcam_region *region,
2964eccd00SJiri Pirko u16 src_offset, u16 dst_offset, u16 size)
3064eccd00SJiri Pirko {
3164eccd00SJiri Pirko char prcr_pl[MLXSW_REG_PRCR_LEN];
3264eccd00SJiri Pirko
3364eccd00SJiri Pirko mlxsw_reg_prcr_pack(prcr_pl, MLXSW_REG_PRCR_OP_MOVE,
3464eccd00SJiri Pirko region->tcam_region_info, src_offset,
3564eccd00SJiri Pirko region->tcam_region_info, dst_offset, size);
3664eccd00SJiri Pirko mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(prcr), prcr_pl);
3764eccd00SJiri Pirko }
3864eccd00SJiri Pirko
3964eccd00SJiri Pirko static int
mlxsw_sp_acl_ctcam_region_entry_insert(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_ctcam_region * cregion,struct mlxsw_sp_acl_ctcam_entry * centry,struct mlxsw_sp_acl_rule_info * rulei,bool fillup_priority)4064eccd00SJiri Pirko mlxsw_sp_acl_ctcam_region_entry_insert(struct mlxsw_sp *mlxsw_sp,
41a20ff8ebSIdo Schimmel struct mlxsw_sp_acl_ctcam_region *cregion,
42a20ff8ebSIdo Schimmel struct mlxsw_sp_acl_ctcam_entry *centry,
43ea8b2e28SJiri Pirko struct mlxsw_sp_acl_rule_info *rulei,
44ea8b2e28SJiri Pirko bool fillup_priority)
4564eccd00SJiri Pirko {
46a20ff8ebSIdo Schimmel struct mlxsw_sp_acl_tcam_region *region = cregion->region;
47a5995cc8SJiri Pirko struct mlxsw_afk *afk = mlxsw_sp_acl_afk(mlxsw_sp->acl);
4864eccd00SJiri Pirko char ptce2_pl[MLXSW_REG_PTCE2_LEN];
4964eccd00SJiri Pirko char *act_set;
50ea8b2e28SJiri Pirko u32 priority;
5164eccd00SJiri Pirko char *mask;
5264eccd00SJiri Pirko char *key;
53ea8b2e28SJiri Pirko int err;
54ea8b2e28SJiri Pirko
55ea8b2e28SJiri Pirko err = mlxsw_sp_acl_tcam_priority_get(mlxsw_sp, rulei, &priority,
56ea8b2e28SJiri Pirko fillup_priority);
57ea8b2e28SJiri Pirko if (err)
58ea8b2e28SJiri Pirko return err;
5964eccd00SJiri Pirko
6064eccd00SJiri Pirko mlxsw_reg_ptce2_pack(ptce2_pl, true, MLXSW_REG_PTCE2_OP_WRITE_WRITE,
61a20ff8ebSIdo Schimmel region->tcam_region_info,
62a20ff8ebSIdo Schimmel centry->parman_item.index, priority);
6364eccd00SJiri Pirko key = mlxsw_reg_ptce2_flex_key_blocks_data(ptce2_pl);
6464eccd00SJiri Pirko mask = mlxsw_reg_ptce2_mask_data(ptce2_pl);
6559600844SJiri Pirko mlxsw_afk_encode(afk, region->key_info, &rulei->values, key, mask);
6664eccd00SJiri Pirko
67a0a777b9SIdo Schimmel err = cregion->ops->entry_insert(cregion, centry, mask);
68a0a777b9SIdo Schimmel if (err)
69a0a777b9SIdo Schimmel return err;
70a0a777b9SIdo Schimmel
7164eccd00SJiri Pirko /* Only the first action set belongs here, the rest is in KVD */
7264eccd00SJiri Pirko act_set = mlxsw_afa_block_first_set(rulei->act_block);
7364eccd00SJiri Pirko mlxsw_reg_ptce2_flex_action_set_memcpy_to(ptce2_pl, act_set);
7464eccd00SJiri Pirko
75*ff0db43cSNir Dotan err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptce2), ptce2_pl);
76*ff0db43cSNir Dotan if (err)
77*ff0db43cSNir Dotan goto err_ptce2_write;
78*ff0db43cSNir Dotan
79*ff0db43cSNir Dotan return 0;
80*ff0db43cSNir Dotan
81*ff0db43cSNir Dotan err_ptce2_write:
82*ff0db43cSNir Dotan cregion->ops->entry_remove(cregion, centry);
83*ff0db43cSNir Dotan return err;
8464eccd00SJiri Pirko }
8564eccd00SJiri Pirko
8664eccd00SJiri Pirko static void
mlxsw_sp_acl_ctcam_region_entry_remove(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_ctcam_region * cregion,struct mlxsw_sp_acl_ctcam_entry * centry)8764eccd00SJiri Pirko mlxsw_sp_acl_ctcam_region_entry_remove(struct mlxsw_sp *mlxsw_sp,
88a20ff8ebSIdo Schimmel struct mlxsw_sp_acl_ctcam_region *cregion,
89a20ff8ebSIdo Schimmel struct mlxsw_sp_acl_ctcam_entry *centry)
9064eccd00SJiri Pirko {
9164eccd00SJiri Pirko char ptce2_pl[MLXSW_REG_PTCE2_LEN];
9264eccd00SJiri Pirko
9364eccd00SJiri Pirko mlxsw_reg_ptce2_pack(ptce2_pl, false, MLXSW_REG_PTCE2_OP_WRITE_WRITE,
94a20ff8ebSIdo Schimmel cregion->region->tcam_region_info,
95a20ff8ebSIdo Schimmel centry->parman_item.index, 0);
9664eccd00SJiri Pirko mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptce2), ptce2_pl);
97a0a777b9SIdo Schimmel cregion->ops->entry_remove(cregion, centry);
9864eccd00SJiri Pirko }
9964eccd00SJiri Pirko
1002507a64cSNir Dotan static int
mlxsw_sp_acl_ctcam_region_entry_action_replace(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_ctcam_region * cregion,struct mlxsw_sp_acl_ctcam_entry * centry,struct mlxsw_afa_block * afa_block,unsigned int priority)1012507a64cSNir Dotan mlxsw_sp_acl_ctcam_region_entry_action_replace(struct mlxsw_sp *mlxsw_sp,
1022507a64cSNir Dotan struct mlxsw_sp_acl_ctcam_region *cregion,
1032507a64cSNir Dotan struct mlxsw_sp_acl_ctcam_entry *centry,
1042507a64cSNir Dotan struct mlxsw_afa_block *afa_block,
1052507a64cSNir Dotan unsigned int priority)
1062507a64cSNir Dotan {
1072507a64cSNir Dotan char ptce2_pl[MLXSW_REG_PTCE2_LEN];
1082507a64cSNir Dotan char *act_set;
1092507a64cSNir Dotan
1102507a64cSNir Dotan mlxsw_reg_ptce2_pack(ptce2_pl, true, MLXSW_REG_PTCE2_OP_WRITE_UPDATE,
1112507a64cSNir Dotan cregion->region->tcam_region_info,
1122507a64cSNir Dotan centry->parman_item.index, priority);
1132507a64cSNir Dotan
1142507a64cSNir Dotan act_set = mlxsw_afa_block_first_set(afa_block);
1152507a64cSNir Dotan mlxsw_reg_ptce2_flex_action_set_memcpy_to(ptce2_pl, act_set);
1162507a64cSNir Dotan
1172507a64cSNir Dotan return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptce2), ptce2_pl);
1182507a64cSNir Dotan }
1192507a64cSNir Dotan
1202507a64cSNir Dotan
mlxsw_sp_acl_ctcam_region_parman_resize(void * priv,unsigned long new_count)12164eccd00SJiri Pirko static int mlxsw_sp_acl_ctcam_region_parman_resize(void *priv,
12264eccd00SJiri Pirko unsigned long new_count)
12364eccd00SJiri Pirko {
12464eccd00SJiri Pirko struct mlxsw_sp_acl_ctcam_region *cregion = priv;
12564eccd00SJiri Pirko struct mlxsw_sp_acl_tcam_region *region = cregion->region;
12664eccd00SJiri Pirko struct mlxsw_sp *mlxsw_sp = region->mlxsw_sp;
12764eccd00SJiri Pirko u64 max_tcam_rules;
12864eccd00SJiri Pirko
12964eccd00SJiri Pirko max_tcam_rules = MLXSW_CORE_RES_GET(mlxsw_sp->core, ACL_MAX_TCAM_RULES);
13064eccd00SJiri Pirko if (new_count > max_tcam_rules)
13164eccd00SJiri Pirko return -EINVAL;
13264eccd00SJiri Pirko return mlxsw_sp_acl_ctcam_region_resize(mlxsw_sp, region, new_count);
13364eccd00SJiri Pirko }
13464eccd00SJiri Pirko
mlxsw_sp_acl_ctcam_region_parman_move(void * priv,unsigned long from_index,unsigned long to_index,unsigned long count)13564eccd00SJiri Pirko static void mlxsw_sp_acl_ctcam_region_parman_move(void *priv,
13664eccd00SJiri Pirko unsigned long from_index,
13764eccd00SJiri Pirko unsigned long to_index,
13864eccd00SJiri Pirko unsigned long count)
13964eccd00SJiri Pirko {
14064eccd00SJiri Pirko struct mlxsw_sp_acl_ctcam_region *cregion = priv;
14164eccd00SJiri Pirko struct mlxsw_sp_acl_tcam_region *region = cregion->region;
14264eccd00SJiri Pirko struct mlxsw_sp *mlxsw_sp = region->mlxsw_sp;
14364eccd00SJiri Pirko
14464eccd00SJiri Pirko mlxsw_sp_acl_ctcam_region_move(mlxsw_sp, region,
14564eccd00SJiri Pirko from_index, to_index, count);
14664eccd00SJiri Pirko }
14764eccd00SJiri Pirko
14864eccd00SJiri Pirko static const struct parman_ops mlxsw_sp_acl_ctcam_region_parman_ops = {
14964eccd00SJiri Pirko .base_count = MLXSW_SP_ACL_TCAM_REGION_BASE_COUNT,
15064eccd00SJiri Pirko .resize_step = MLXSW_SP_ACL_TCAM_REGION_RESIZE_STEP,
15164eccd00SJiri Pirko .resize = mlxsw_sp_acl_ctcam_region_parman_resize,
15264eccd00SJiri Pirko .move = mlxsw_sp_acl_ctcam_region_parman_move,
15364eccd00SJiri Pirko .algo = PARMAN_ALGO_TYPE_LSORT,
15464eccd00SJiri Pirko };
15564eccd00SJiri Pirko
156a0a777b9SIdo Schimmel int
mlxsw_sp_acl_ctcam_region_init(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_ctcam_region * cregion,struct mlxsw_sp_acl_tcam_region * region,const struct mlxsw_sp_acl_ctcam_region_ops * ops)157a0a777b9SIdo Schimmel mlxsw_sp_acl_ctcam_region_init(struct mlxsw_sp *mlxsw_sp,
15864eccd00SJiri Pirko struct mlxsw_sp_acl_ctcam_region *cregion,
159a0a777b9SIdo Schimmel struct mlxsw_sp_acl_tcam_region *region,
160a0a777b9SIdo Schimmel const struct mlxsw_sp_acl_ctcam_region_ops *ops)
16164eccd00SJiri Pirko {
16264eccd00SJiri Pirko cregion->region = region;
163a0a777b9SIdo Schimmel cregion->ops = ops;
16464eccd00SJiri Pirko cregion->parman = parman_create(&mlxsw_sp_acl_ctcam_region_parman_ops,
16564eccd00SJiri Pirko cregion);
16664eccd00SJiri Pirko if (!cregion->parman)
16764eccd00SJiri Pirko return -ENOMEM;
16864eccd00SJiri Pirko return 0;
16964eccd00SJiri Pirko }
17064eccd00SJiri Pirko
mlxsw_sp_acl_ctcam_region_fini(struct mlxsw_sp_acl_ctcam_region * cregion)17164eccd00SJiri Pirko void mlxsw_sp_acl_ctcam_region_fini(struct mlxsw_sp_acl_ctcam_region *cregion)
17264eccd00SJiri Pirko {
17364eccd00SJiri Pirko parman_destroy(cregion->parman);
17464eccd00SJiri Pirko }
17564eccd00SJiri Pirko
mlxsw_sp_acl_ctcam_chunk_init(struct mlxsw_sp_acl_ctcam_region * cregion,struct mlxsw_sp_acl_ctcam_chunk * cchunk,unsigned int priority)17664eccd00SJiri Pirko void mlxsw_sp_acl_ctcam_chunk_init(struct mlxsw_sp_acl_ctcam_region *cregion,
17764eccd00SJiri Pirko struct mlxsw_sp_acl_ctcam_chunk *cchunk,
17864eccd00SJiri Pirko unsigned int priority)
17964eccd00SJiri Pirko {
18064eccd00SJiri Pirko parman_prio_init(cregion->parman, &cchunk->parman_prio, priority);
18164eccd00SJiri Pirko }
18264eccd00SJiri Pirko
mlxsw_sp_acl_ctcam_chunk_fini(struct mlxsw_sp_acl_ctcam_chunk * cchunk)18364eccd00SJiri Pirko void mlxsw_sp_acl_ctcam_chunk_fini(struct mlxsw_sp_acl_ctcam_chunk *cchunk)
18464eccd00SJiri Pirko {
18564eccd00SJiri Pirko parman_prio_fini(&cchunk->parman_prio);
18664eccd00SJiri Pirko }
18764eccd00SJiri Pirko
mlxsw_sp_acl_ctcam_entry_add(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_ctcam_region * cregion,struct mlxsw_sp_acl_ctcam_chunk * cchunk,struct mlxsw_sp_acl_ctcam_entry * centry,struct mlxsw_sp_acl_rule_info * rulei,bool fillup_priority)18864eccd00SJiri Pirko int mlxsw_sp_acl_ctcam_entry_add(struct mlxsw_sp *mlxsw_sp,
18964eccd00SJiri Pirko struct mlxsw_sp_acl_ctcam_region *cregion,
19064eccd00SJiri Pirko struct mlxsw_sp_acl_ctcam_chunk *cchunk,
19164eccd00SJiri Pirko struct mlxsw_sp_acl_ctcam_entry *centry,
192ea8b2e28SJiri Pirko struct mlxsw_sp_acl_rule_info *rulei,
193ea8b2e28SJiri Pirko bool fillup_priority)
19464eccd00SJiri Pirko {
19564eccd00SJiri Pirko int err;
19664eccd00SJiri Pirko
19764eccd00SJiri Pirko err = parman_item_add(cregion->parman, &cchunk->parman_prio,
19864eccd00SJiri Pirko ¢ry->parman_item);
19964eccd00SJiri Pirko if (err)
20064eccd00SJiri Pirko return err;
20164eccd00SJiri Pirko
202a20ff8ebSIdo Schimmel err = mlxsw_sp_acl_ctcam_region_entry_insert(mlxsw_sp, cregion, centry,
203ea8b2e28SJiri Pirko rulei, fillup_priority);
20464eccd00SJiri Pirko if (err)
20564eccd00SJiri Pirko goto err_rule_insert;
20664eccd00SJiri Pirko return 0;
20764eccd00SJiri Pirko
20864eccd00SJiri Pirko err_rule_insert:
20964eccd00SJiri Pirko parman_item_remove(cregion->parman, &cchunk->parman_prio,
21064eccd00SJiri Pirko ¢ry->parman_item);
21164eccd00SJiri Pirko return err;
21264eccd00SJiri Pirko }
21364eccd00SJiri Pirko
mlxsw_sp_acl_ctcam_entry_del(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_ctcam_region * cregion,struct mlxsw_sp_acl_ctcam_chunk * cchunk,struct mlxsw_sp_acl_ctcam_entry * centry)21464eccd00SJiri Pirko void mlxsw_sp_acl_ctcam_entry_del(struct mlxsw_sp *mlxsw_sp,
21564eccd00SJiri Pirko struct mlxsw_sp_acl_ctcam_region *cregion,
21664eccd00SJiri Pirko struct mlxsw_sp_acl_ctcam_chunk *cchunk,
21764eccd00SJiri Pirko struct mlxsw_sp_acl_ctcam_entry *centry)
21864eccd00SJiri Pirko {
219a20ff8ebSIdo Schimmel mlxsw_sp_acl_ctcam_region_entry_remove(mlxsw_sp, cregion, centry);
22064eccd00SJiri Pirko parman_item_remove(cregion->parman, &cchunk->parman_prio,
22164eccd00SJiri Pirko ¢ry->parman_item);
22264eccd00SJiri Pirko }
2232507a64cSNir Dotan
mlxsw_sp_acl_ctcam_entry_action_replace(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_ctcam_region * cregion,struct mlxsw_sp_acl_ctcam_entry * centry,struct mlxsw_sp_acl_rule_info * rulei)2242507a64cSNir Dotan int mlxsw_sp_acl_ctcam_entry_action_replace(struct mlxsw_sp *mlxsw_sp,
2252507a64cSNir Dotan struct mlxsw_sp_acl_ctcam_region *cregion,
2262507a64cSNir Dotan struct mlxsw_sp_acl_ctcam_entry *centry,
2272507a64cSNir Dotan struct mlxsw_sp_acl_rule_info *rulei)
2282507a64cSNir Dotan {
2292507a64cSNir Dotan return mlxsw_sp_acl_ctcam_region_entry_action_replace(mlxsw_sp, cregion,
2302507a64cSNir Dotan centry,
2312507a64cSNir Dotan rulei->act_block,
2322507a64cSNir Dotan rulei->priority);
2332507a64cSNir Dotan }
234