xref: /linux/drivers/net/ethernet/mellanox/mlxsw/spectrum_acl_ctcam.c (revision cad4977344b35ea116ec5fefe91a76b1dfa113f5)
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
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
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
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 	char *act_set;
50 	u32 priority;
51 	char *mask;
52 	char *key;
53 	int err;
54 
55 	err = mlxsw_sp_acl_tcam_priority_get(mlxsw_sp, rulei, &priority,
56 					     fillup_priority);
57 	if (err)
58 		return err;
59 
60 	mlxsw_reg_ptce2_pack(ptce2_pl, true, MLXSW_REG_PTCE2_OP_WRITE_WRITE,
61 			     region->tcam_region_info,
62 			     centry->parman_item.index, priority);
63 	key = mlxsw_reg_ptce2_flex_key_blocks_data(ptce2_pl);
64 	mask = mlxsw_reg_ptce2_mask_data(ptce2_pl);
65 	mlxsw_afk_encode(afk, region->key_info, &rulei->values, key, mask);
66 
67 	err = cregion->ops->entry_insert(cregion, centry, mask);
68 	if (err)
69 		return err;
70 
71 	/* Only the first action set belongs here, the rest is in KVD */
72 	act_set = mlxsw_afa_block_first_set(rulei->act_block);
73 	mlxsw_reg_ptce2_flex_action_set_memcpy_to(ptce2_pl, act_set);
74 
75 	return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptce2), ptce2_pl);
76 }
77 
78 static void
79 mlxsw_sp_acl_ctcam_region_entry_remove(struct mlxsw_sp *mlxsw_sp,
80 				       struct mlxsw_sp_acl_ctcam_region *cregion,
81 				       struct mlxsw_sp_acl_ctcam_entry *centry)
82 {
83 	char ptce2_pl[MLXSW_REG_PTCE2_LEN];
84 
85 	mlxsw_reg_ptce2_pack(ptce2_pl, false, MLXSW_REG_PTCE2_OP_WRITE_WRITE,
86 			     cregion->region->tcam_region_info,
87 			     centry->parman_item.index, 0);
88 	mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptce2), ptce2_pl);
89 	cregion->ops->entry_remove(cregion, centry);
90 }
91 
92 static int mlxsw_sp_acl_ctcam_region_parman_resize(void *priv,
93 						   unsigned long new_count)
94 {
95 	struct mlxsw_sp_acl_ctcam_region *cregion = priv;
96 	struct mlxsw_sp_acl_tcam_region *region = cregion->region;
97 	struct mlxsw_sp *mlxsw_sp = region->mlxsw_sp;
98 	u64 max_tcam_rules;
99 
100 	max_tcam_rules = MLXSW_CORE_RES_GET(mlxsw_sp->core, ACL_MAX_TCAM_RULES);
101 	if (new_count > max_tcam_rules)
102 		return -EINVAL;
103 	return mlxsw_sp_acl_ctcam_region_resize(mlxsw_sp, region, new_count);
104 }
105 
106 static void mlxsw_sp_acl_ctcam_region_parman_move(void *priv,
107 						  unsigned long from_index,
108 						  unsigned long to_index,
109 						  unsigned long count)
110 {
111 	struct mlxsw_sp_acl_ctcam_region *cregion = priv;
112 	struct mlxsw_sp_acl_tcam_region *region = cregion->region;
113 	struct mlxsw_sp *mlxsw_sp = region->mlxsw_sp;
114 
115 	mlxsw_sp_acl_ctcam_region_move(mlxsw_sp, region,
116 				       from_index, to_index, count);
117 }
118 
119 static const struct parman_ops mlxsw_sp_acl_ctcam_region_parman_ops = {
120 	.base_count	= MLXSW_SP_ACL_TCAM_REGION_BASE_COUNT,
121 	.resize_step	= MLXSW_SP_ACL_TCAM_REGION_RESIZE_STEP,
122 	.resize		= mlxsw_sp_acl_ctcam_region_parman_resize,
123 	.move		= mlxsw_sp_acl_ctcam_region_parman_move,
124 	.algo		= PARMAN_ALGO_TYPE_LSORT,
125 };
126 
127 int
128 mlxsw_sp_acl_ctcam_region_init(struct mlxsw_sp *mlxsw_sp,
129 			       struct mlxsw_sp_acl_ctcam_region *cregion,
130 			       struct mlxsw_sp_acl_tcam_region *region,
131 			       const struct mlxsw_sp_acl_ctcam_region_ops *ops)
132 {
133 	cregion->region = region;
134 	cregion->ops = ops;
135 	cregion->parman = parman_create(&mlxsw_sp_acl_ctcam_region_parman_ops,
136 					cregion);
137 	if (!cregion->parman)
138 		return -ENOMEM;
139 	return 0;
140 }
141 
142 void mlxsw_sp_acl_ctcam_region_fini(struct mlxsw_sp_acl_ctcam_region *cregion)
143 {
144 	parman_destroy(cregion->parman);
145 }
146 
147 void mlxsw_sp_acl_ctcam_chunk_init(struct mlxsw_sp_acl_ctcam_region *cregion,
148 				   struct mlxsw_sp_acl_ctcam_chunk *cchunk,
149 				   unsigned int priority)
150 {
151 	parman_prio_init(cregion->parman, &cchunk->parman_prio, priority);
152 }
153 
154 void mlxsw_sp_acl_ctcam_chunk_fini(struct mlxsw_sp_acl_ctcam_chunk *cchunk)
155 {
156 	parman_prio_fini(&cchunk->parman_prio);
157 }
158 
159 int mlxsw_sp_acl_ctcam_entry_add(struct mlxsw_sp *mlxsw_sp,
160 				 struct mlxsw_sp_acl_ctcam_region *cregion,
161 				 struct mlxsw_sp_acl_ctcam_chunk *cchunk,
162 				 struct mlxsw_sp_acl_ctcam_entry *centry,
163 				 struct mlxsw_sp_acl_rule_info *rulei,
164 				 bool fillup_priority)
165 {
166 	int err;
167 
168 	err = parman_item_add(cregion->parman, &cchunk->parman_prio,
169 			      &centry->parman_item);
170 	if (err)
171 		return err;
172 
173 	err = mlxsw_sp_acl_ctcam_region_entry_insert(mlxsw_sp, cregion, centry,
174 						     rulei, fillup_priority);
175 	if (err)
176 		goto err_rule_insert;
177 	return 0;
178 
179 err_rule_insert:
180 	parman_item_remove(cregion->parman, &cchunk->parman_prio,
181 			   &centry->parman_item);
182 	return err;
183 }
184 
185 void mlxsw_sp_acl_ctcam_entry_del(struct mlxsw_sp *mlxsw_sp,
186 				  struct mlxsw_sp_acl_ctcam_region *cregion,
187 				  struct mlxsw_sp_acl_ctcam_chunk *cchunk,
188 				  struct mlxsw_sp_acl_ctcam_entry *centry)
189 {
190 	mlxsw_sp_acl_ctcam_region_entry_remove(mlxsw_sp, cregion, centry);
191 	parman_item_remove(cregion->parman, &cchunk->parman_prio,
192 			   &centry->parman_item);
193 }
194