GNU Linux-libre 4.19.245-gnu1
[releases.git] / drivers / net / ethernet / mellanox / mlxsw / spectrum_acl_ctcam.c
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         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         err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptce2), ptce2_pl);
79         if (err)
80                 goto err_ptce2_write;
81
82         return 0;
83
84 err_ptce2_write:
85         cregion->ops->entry_remove(cregion, centry);
86         return err;
87 }
88
89 static void
90 mlxsw_sp_acl_ctcam_region_entry_remove(struct mlxsw_sp *mlxsw_sp,
91                                        struct mlxsw_sp_acl_ctcam_region *cregion,
92                                        struct mlxsw_sp_acl_ctcam_entry *centry)
93 {
94         char ptce2_pl[MLXSW_REG_PTCE2_LEN];
95
96         mlxsw_reg_ptce2_pack(ptce2_pl, false, MLXSW_REG_PTCE2_OP_WRITE_WRITE,
97                              cregion->region->tcam_region_info,
98                              centry->parman_item.index, 0);
99         mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptce2), ptce2_pl);
100         cregion->ops->entry_remove(cregion, centry);
101 }
102
103 static int mlxsw_sp_acl_ctcam_region_parman_resize(void *priv,
104                                                    unsigned long new_count)
105 {
106         struct mlxsw_sp_acl_ctcam_region *cregion = priv;
107         struct mlxsw_sp_acl_tcam_region *region = cregion->region;
108         struct mlxsw_sp *mlxsw_sp = region->mlxsw_sp;
109         u64 max_tcam_rules;
110
111         max_tcam_rules = MLXSW_CORE_RES_GET(mlxsw_sp->core, ACL_MAX_TCAM_RULES);
112         if (new_count > max_tcam_rules)
113                 return -EINVAL;
114         return mlxsw_sp_acl_ctcam_region_resize(mlxsw_sp, region, new_count);
115 }
116
117 static void mlxsw_sp_acl_ctcam_region_parman_move(void *priv,
118                                                   unsigned long from_index,
119                                                   unsigned long to_index,
120                                                   unsigned long count)
121 {
122         struct mlxsw_sp_acl_ctcam_region *cregion = priv;
123         struct mlxsw_sp_acl_tcam_region *region = cregion->region;
124         struct mlxsw_sp *mlxsw_sp = region->mlxsw_sp;
125
126         mlxsw_sp_acl_ctcam_region_move(mlxsw_sp, region,
127                                        from_index, to_index, count);
128 }
129
130 static const struct parman_ops mlxsw_sp_acl_ctcam_region_parman_ops = {
131         .base_count     = MLXSW_SP_ACL_TCAM_REGION_BASE_COUNT,
132         .resize_step    = MLXSW_SP_ACL_TCAM_REGION_RESIZE_STEP,
133         .resize         = mlxsw_sp_acl_ctcam_region_parman_resize,
134         .move           = mlxsw_sp_acl_ctcam_region_parman_move,
135         .algo           = PARMAN_ALGO_TYPE_LSORT,
136 };
137
138 int
139 mlxsw_sp_acl_ctcam_region_init(struct mlxsw_sp *mlxsw_sp,
140                                struct mlxsw_sp_acl_ctcam_region *cregion,
141                                struct mlxsw_sp_acl_tcam_region *region,
142                                const struct mlxsw_sp_acl_ctcam_region_ops *ops)
143 {
144         cregion->region = region;
145         cregion->ops = ops;
146         cregion->parman = parman_create(&mlxsw_sp_acl_ctcam_region_parman_ops,
147                                         cregion);
148         if (!cregion->parman)
149                 return -ENOMEM;
150         return 0;
151 }
152
153 void mlxsw_sp_acl_ctcam_region_fini(struct mlxsw_sp_acl_ctcam_region *cregion)
154 {
155         parman_destroy(cregion->parman);
156 }
157
158 void mlxsw_sp_acl_ctcam_chunk_init(struct mlxsw_sp_acl_ctcam_region *cregion,
159                                    struct mlxsw_sp_acl_ctcam_chunk *cchunk,
160                                    unsigned int priority)
161 {
162         parman_prio_init(cregion->parman, &cchunk->parman_prio, priority);
163 }
164
165 void mlxsw_sp_acl_ctcam_chunk_fini(struct mlxsw_sp_acl_ctcam_chunk *cchunk)
166 {
167         parman_prio_fini(&cchunk->parman_prio);
168 }
169
170 int mlxsw_sp_acl_ctcam_entry_add(struct mlxsw_sp *mlxsw_sp,
171                                  struct mlxsw_sp_acl_ctcam_region *cregion,
172                                  struct mlxsw_sp_acl_ctcam_chunk *cchunk,
173                                  struct mlxsw_sp_acl_ctcam_entry *centry,
174                                  struct mlxsw_sp_acl_rule_info *rulei,
175                                  bool fillup_priority)
176 {
177         int err;
178
179         err = parman_item_add(cregion->parman, &cchunk->parman_prio,
180                               &centry->parman_item);
181         if (err)
182                 return err;
183
184         err = mlxsw_sp_acl_ctcam_region_entry_insert(mlxsw_sp, cregion, centry,
185                                                      rulei, fillup_priority);
186         if (err)
187                 goto err_rule_insert;
188         return 0;
189
190 err_rule_insert:
191         parman_item_remove(cregion->parman, &cchunk->parman_prio,
192                            &centry->parman_item);
193         return err;
194 }
195
196 void mlxsw_sp_acl_ctcam_entry_del(struct mlxsw_sp *mlxsw_sp,
197                                   struct mlxsw_sp_acl_ctcam_region *cregion,
198                                   struct mlxsw_sp_acl_ctcam_chunk *cchunk,
199                                   struct mlxsw_sp_acl_ctcam_entry *centry)
200 {
201         mlxsw_sp_acl_ctcam_region_entry_remove(mlxsw_sp, cregion, centry);
202         parman_item_remove(cregion->parman, &cchunk->parman_prio,
203                            &centry->parman_item);
204 }