1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /* Copyright (c) 2017-2018 Mellanox Technologies. All rights reserved */
3
4 #include <linux/kernel.h>
5 #include <linux/errno.h>
6 #include <linux/parman.h>
7
8 #include "reg.h"
9 #include "core.h"
10 #include "spectrum.h"
11 #include "spectrum_acl_tcam.h"
12
13 static int
mlxsw_sp_acl_ctcam_region_resize(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam_region * region,u16 new_size)14 mlxsw_sp_acl_ctcam_region_resize(struct mlxsw_sp *mlxsw_sp,
15 struct mlxsw_sp_acl_tcam_region *region,
16 u16 new_size)
17 {
18 char ptar_pl[MLXSW_REG_PTAR_LEN];
19
20 mlxsw_reg_ptar_pack(ptar_pl, MLXSW_REG_PTAR_OP_RESIZE,
21 region->key_type, new_size, region->id,
22 region->tcam_region_info);
23 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptar), ptar_pl);
24 }
25
26 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)27 mlxsw_sp_acl_ctcam_region_move(struct mlxsw_sp *mlxsw_sp,
28 struct mlxsw_sp_acl_tcam_region *region,
29 u16 src_offset, u16 dst_offset, u16 size)
30 {
31 char prcr_pl[MLXSW_REG_PRCR_LEN];
32
33 mlxsw_reg_prcr_pack(prcr_pl, MLXSW_REG_PRCR_OP_MOVE,
34 region->tcam_region_info, src_offset,
35 region->tcam_region_info, dst_offset, size);
36 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(prcr), prcr_pl);
37 }
38
39 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)40 mlxsw_sp_acl_ctcam_region_entry_insert(struct mlxsw_sp *mlxsw_sp,
41 struct mlxsw_sp_acl_ctcam_region *cregion,
42 struct mlxsw_sp_acl_ctcam_entry *centry,
43 struct mlxsw_sp_acl_rule_info *rulei,
44 bool fillup_priority)
45 {
46 struct mlxsw_sp_acl_tcam_region *region = cregion->region;
47 struct mlxsw_afk *afk = mlxsw_sp_acl_afk(mlxsw_sp->acl);
48 char ptce2_pl[MLXSW_REG_PTCE2_LEN];
49 unsigned int blocks_count;
50 char *act_set;
51 u32 priority;
52 char *mask;
53 char *key;
54 int err;
55
56 err = mlxsw_sp_acl_tcam_priority_get(mlxsw_sp, rulei, &priority,
57 fillup_priority);
58 if (err)
59 return err;
60
61 mlxsw_reg_ptce2_pack(ptce2_pl, true, MLXSW_REG_PTCE2_OP_WRITE_WRITE,
62 region->tcam_region_info,
63 centry->parman_item.index, priority);
64 key = mlxsw_reg_ptce2_flex_key_blocks_data(ptce2_pl);
65 mask = mlxsw_reg_ptce2_mask_data(ptce2_pl);
66 blocks_count = mlxsw_afk_key_info_blocks_count_get(region->key_info);
67 mlxsw_afk_encode(afk, region->key_info, &rulei->values, key, mask, 0,
68 blocks_count - 1);
69
70 err = cregion->ops->entry_insert(cregion, centry, mask);
71 if (err)
72 return err;
73
74 /* Only the first action set belongs here, the rest is in KVD */
75 act_set = mlxsw_afa_block_first_set(rulei->act_block);
76 mlxsw_reg_ptce2_flex_action_set_memcpy_to(ptce2_pl, act_set);
77
78 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptce2), ptce2_pl);
79 }
80
81 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)82 mlxsw_sp_acl_ctcam_region_entry_remove(struct mlxsw_sp *mlxsw_sp,
83 struct mlxsw_sp_acl_ctcam_region *cregion,
84 struct mlxsw_sp_acl_ctcam_entry *centry)
85 {
86 char ptce2_pl[MLXSW_REG_PTCE2_LEN];
87
88 mlxsw_reg_ptce2_pack(ptce2_pl, false, MLXSW_REG_PTCE2_OP_WRITE_WRITE,
89 cregion->region->tcam_region_info,
90 centry->parman_item.index, 0);
91 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptce2), ptce2_pl);
92 cregion->ops->entry_remove(cregion, centry);
93 }
94
mlxsw_sp_acl_ctcam_region_parman_resize(void * priv,unsigned long new_count)95 static int mlxsw_sp_acl_ctcam_region_parman_resize(void *priv,
96 unsigned long new_count)
97 {
98 struct mlxsw_sp_acl_ctcam_region *cregion = priv;
99 struct mlxsw_sp_acl_tcam_region *region = cregion->region;
100 struct mlxsw_sp *mlxsw_sp = region->mlxsw_sp;
101 u64 max_tcam_rules;
102
103 max_tcam_rules = MLXSW_CORE_RES_GET(mlxsw_sp->core, ACL_MAX_TCAM_RULES);
104 if (new_count > max_tcam_rules)
105 return -EINVAL;
106 return mlxsw_sp_acl_ctcam_region_resize(mlxsw_sp, region, new_count);
107 }
108
mlxsw_sp_acl_ctcam_region_parman_move(void * priv,unsigned long from_index,unsigned long to_index,unsigned long count)109 static void mlxsw_sp_acl_ctcam_region_parman_move(void *priv,
110 unsigned long from_index,
111 unsigned long to_index,
112 unsigned long count)
113 {
114 struct mlxsw_sp_acl_ctcam_region *cregion = priv;
115 struct mlxsw_sp_acl_tcam_region *region = cregion->region;
116 struct mlxsw_sp *mlxsw_sp = region->mlxsw_sp;
117
118 mlxsw_sp_acl_ctcam_region_move(mlxsw_sp, region,
119 from_index, to_index, count);
120 }
121
122 static const struct parman_ops mlxsw_sp_acl_ctcam_region_parman_ops = {
123 .base_count = MLXSW_SP_ACL_TCAM_REGION_BASE_COUNT,
124 .resize_step = MLXSW_SP_ACL_TCAM_REGION_RESIZE_STEP,
125 .resize = mlxsw_sp_acl_ctcam_region_parman_resize,
126 .move = mlxsw_sp_acl_ctcam_region_parman_move,
127 .algo = PARMAN_ALGO_TYPE_LSORT,
128 };
129
130 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)131 mlxsw_sp_acl_ctcam_region_init(struct mlxsw_sp *mlxsw_sp,
132 struct mlxsw_sp_acl_ctcam_region *cregion,
133 struct mlxsw_sp_acl_tcam_region *region,
134 const struct mlxsw_sp_acl_ctcam_region_ops *ops)
135 {
136 cregion->region = region;
137 cregion->ops = ops;
138 cregion->parman = parman_create(&mlxsw_sp_acl_ctcam_region_parman_ops,
139 cregion);
140 if (!cregion->parman)
141 return -ENOMEM;
142 return 0;
143 }
144
mlxsw_sp_acl_ctcam_region_fini(struct mlxsw_sp_acl_ctcam_region * cregion)145 void mlxsw_sp_acl_ctcam_region_fini(struct mlxsw_sp_acl_ctcam_region *cregion)
146 {
147 parman_destroy(cregion->parman);
148 }
149
mlxsw_sp_acl_ctcam_chunk_init(struct mlxsw_sp_acl_ctcam_region * cregion,struct mlxsw_sp_acl_ctcam_chunk * cchunk,unsigned int priority)150 void mlxsw_sp_acl_ctcam_chunk_init(struct mlxsw_sp_acl_ctcam_region *cregion,
151 struct mlxsw_sp_acl_ctcam_chunk *cchunk,
152 unsigned int priority)
153 {
154 parman_prio_init(cregion->parman, &cchunk->parman_prio, priority);
155 }
156
mlxsw_sp_acl_ctcam_chunk_fini(struct mlxsw_sp_acl_ctcam_chunk * cchunk)157 void mlxsw_sp_acl_ctcam_chunk_fini(struct mlxsw_sp_acl_ctcam_chunk *cchunk)
158 {
159 parman_prio_fini(&cchunk->parman_prio);
160 }
161
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)162 int mlxsw_sp_acl_ctcam_entry_add(struct mlxsw_sp *mlxsw_sp,
163 struct mlxsw_sp_acl_ctcam_region *cregion,
164 struct mlxsw_sp_acl_ctcam_chunk *cchunk,
165 struct mlxsw_sp_acl_ctcam_entry *centry,
166 struct mlxsw_sp_acl_rule_info *rulei,
167 bool fillup_priority)
168 {
169 int err;
170
171 err = parman_item_add(cregion->parman, &cchunk->parman_prio,
172 ¢ry->parman_item);
173 if (err)
174 return err;
175
176 err = mlxsw_sp_acl_ctcam_region_entry_insert(mlxsw_sp, cregion, centry,
177 rulei, fillup_priority);
178 if (err)
179 goto err_rule_insert;
180 return 0;
181
182 err_rule_insert:
183 parman_item_remove(cregion->parman, &cchunk->parman_prio,
184 ¢ry->parman_item);
185 return err;
186 }
187
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)188 void mlxsw_sp_acl_ctcam_entry_del(struct mlxsw_sp *mlxsw_sp,
189 struct mlxsw_sp_acl_ctcam_region *cregion,
190 struct mlxsw_sp_acl_ctcam_chunk *cchunk,
191 struct mlxsw_sp_acl_ctcam_entry *centry)
192 {
193 mlxsw_sp_acl_ctcam_region_entry_remove(mlxsw_sp, cregion, centry);
194 parman_item_remove(cregion->parman, &cchunk->parman_prio,
195 ¢ry->parman_item);
196 }
197