OSDN Git Service

c97d40ccf7ad79c69deeedde1924d5c62326ecb2
[uclinux-h8/linux.git] / drivers / net / ethernet / mellanox / mlxsw / spectrum_acl.c
1 /*
2  * drivers/net/ethernet/mellanox/mlxsw/spectrum_acl.c
3  * Copyright (c) 2017 Mellanox Technologies. All rights reserved.
4  * Copyright (c) 2017 Jiri Pirko <jiri@mellanox.com>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. Neither the names of the copyright holders nor the names of its
15  *    contributors may be used to endorse or promote products derived from
16  *    this software without specific prior written permission.
17  *
18  * Alternatively, this software may be distributed under the terms of the
19  * GNU General Public License ("GPL") version 2 as published by the Free
20  * Software Foundation.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 #include <linux/kernel.h>
36 #include <linux/slab.h>
37 #include <linux/errno.h>
38 #include <linux/list.h>
39 #include <linux/string.h>
40 #include <linux/rhashtable.h>
41 #include <linux/netdevice.h>
42 #include <net/net_namespace.h>
43 #include <net/tc_act/tc_vlan.h>
44
45 #include "reg.h"
46 #include "core.h"
47 #include "resources.h"
48 #include "spectrum.h"
49 #include "core_acl_flex_keys.h"
50 #include "core_acl_flex_actions.h"
51 #include "spectrum_acl_tcam.h"
52
53 struct mlxsw_sp_acl {
54         struct mlxsw_sp *mlxsw_sp;
55         struct mlxsw_afk *afk;
56         struct mlxsw_sp_fid *dummy_fid;
57         struct rhashtable ruleset_ht;
58         struct list_head rules;
59         struct {
60                 struct delayed_work dw;
61                 unsigned long interval; /* ms */
62 #define MLXSW_SP_ACL_RULE_ACTIVITY_UPDATE_PERIOD_MS 1000
63         } rule_activity_update;
64         struct mlxsw_sp_acl_tcam tcam;
65 };
66
67 struct mlxsw_afk *mlxsw_sp_acl_afk(struct mlxsw_sp_acl *acl)
68 {
69         return acl->afk;
70 }
71
72 struct mlxsw_sp_acl_block_binding {
73         struct list_head list;
74         struct net_device *dev;
75         struct mlxsw_sp_port *mlxsw_sp_port;
76         bool ingress;
77 };
78
79 struct mlxsw_sp_acl_block {
80         struct list_head binding_list;
81         struct mlxsw_sp_acl_ruleset *ruleset_zero;
82         struct mlxsw_sp *mlxsw_sp;
83         unsigned int rule_count;
84         unsigned int disable_count;
85 };
86
87 struct mlxsw_sp_acl_ruleset_ht_key {
88         struct mlxsw_sp_acl_block *block;
89         u32 chain_index;
90         const struct mlxsw_sp_acl_profile_ops *ops;
91 };
92
93 struct mlxsw_sp_acl_ruleset {
94         struct rhash_head ht_node; /* Member of acl HT */
95         struct mlxsw_sp_acl_ruleset_ht_key ht_key;
96         struct rhashtable rule_ht;
97         unsigned int ref_count;
98         unsigned long priv[0];
99         /* priv has to be always the last item */
100 };
101
102 struct mlxsw_sp_acl_rule {
103         struct rhash_head ht_node; /* Member of rule HT */
104         struct list_head list;
105         unsigned long cookie; /* HT key */
106         struct mlxsw_sp_acl_ruleset *ruleset;
107         struct mlxsw_sp_acl_rule_info *rulei;
108         u64 last_used;
109         u64 last_packets;
110         u64 last_bytes;
111         unsigned long priv[0];
112         /* priv has to be always the last item */
113 };
114
115 static const struct rhashtable_params mlxsw_sp_acl_ruleset_ht_params = {
116         .key_len = sizeof(struct mlxsw_sp_acl_ruleset_ht_key),
117         .key_offset = offsetof(struct mlxsw_sp_acl_ruleset, ht_key),
118         .head_offset = offsetof(struct mlxsw_sp_acl_ruleset, ht_node),
119         .automatic_shrinking = true,
120 };
121
122 static const struct rhashtable_params mlxsw_sp_acl_rule_ht_params = {
123         .key_len = sizeof(unsigned long),
124         .key_offset = offsetof(struct mlxsw_sp_acl_rule, cookie),
125         .head_offset = offsetof(struct mlxsw_sp_acl_rule, ht_node),
126         .automatic_shrinking = true,
127 };
128
129 struct mlxsw_sp_fid *mlxsw_sp_acl_dummy_fid(struct mlxsw_sp *mlxsw_sp)
130 {
131         return mlxsw_sp->acl->dummy_fid;
132 }
133
134 struct mlxsw_sp *mlxsw_sp_acl_block_mlxsw_sp(struct mlxsw_sp_acl_block *block)
135 {
136         return block->mlxsw_sp;
137 }
138
139 unsigned int mlxsw_sp_acl_block_rule_count(struct mlxsw_sp_acl_block *block)
140 {
141         return block ? block->rule_count : 0;
142 }
143
144 void mlxsw_sp_acl_block_disable_inc(struct mlxsw_sp_acl_block *block)
145 {
146         if (block)
147                 block->disable_count++;
148 }
149
150 void mlxsw_sp_acl_block_disable_dec(struct mlxsw_sp_acl_block *block)
151 {
152         if (block)
153                 block->disable_count--;
154 }
155
156 bool mlxsw_sp_acl_block_disabled(struct mlxsw_sp_acl_block *block)
157 {
158         return block->disable_count;
159 }
160
161 static bool
162 mlxsw_sp_acl_ruleset_is_singular(const struct mlxsw_sp_acl_ruleset *ruleset)
163 {
164         /* We hold a reference on ruleset ourselves */
165         return ruleset->ref_count == 2;
166 }
167
168 static int
169 mlxsw_sp_acl_ruleset_bind(struct mlxsw_sp *mlxsw_sp,
170                           struct mlxsw_sp_acl_block *block,
171                           struct mlxsw_sp_acl_block_binding *binding)
172 {
173         struct mlxsw_sp_acl_ruleset *ruleset = block->ruleset_zero;
174         const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
175
176         return ops->ruleset_bind(mlxsw_sp, ruleset->priv,
177                                  binding->mlxsw_sp_port, binding->ingress);
178 }
179
180 static void
181 mlxsw_sp_acl_ruleset_unbind(struct mlxsw_sp *mlxsw_sp,
182                             struct mlxsw_sp_acl_block *block,
183                             struct mlxsw_sp_acl_block_binding *binding)
184 {
185         struct mlxsw_sp_acl_ruleset *ruleset = block->ruleset_zero;
186         const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
187
188         ops->ruleset_unbind(mlxsw_sp, ruleset->priv,
189                             binding->mlxsw_sp_port, binding->ingress);
190 }
191
192 static bool mlxsw_sp_acl_ruleset_block_bound(struct mlxsw_sp_acl_block *block)
193 {
194         return block->ruleset_zero;
195 }
196
197 static int
198 mlxsw_sp_acl_ruleset_block_bind(struct mlxsw_sp *mlxsw_sp,
199                                 struct mlxsw_sp_acl_ruleset *ruleset,
200                                 struct mlxsw_sp_acl_block *block)
201 {
202         struct mlxsw_sp_acl_block_binding *binding;
203         int err;
204
205         block->ruleset_zero = ruleset;
206         list_for_each_entry(binding, &block->binding_list, list) {
207                 err = mlxsw_sp_acl_ruleset_bind(mlxsw_sp, block, binding);
208                 if (err)
209                         goto rollback;
210         }
211         return 0;
212
213 rollback:
214         list_for_each_entry_continue_reverse(binding, &block->binding_list,
215                                              list)
216                 mlxsw_sp_acl_ruleset_unbind(mlxsw_sp, block, binding);
217         block->ruleset_zero = NULL;
218
219         return err;
220 }
221
222 static void
223 mlxsw_sp_acl_ruleset_block_unbind(struct mlxsw_sp *mlxsw_sp,
224                                   struct mlxsw_sp_acl_ruleset *ruleset,
225                                   struct mlxsw_sp_acl_block *block)
226 {
227         struct mlxsw_sp_acl_block_binding *binding;
228
229         list_for_each_entry(binding, &block->binding_list, list)
230                 mlxsw_sp_acl_ruleset_unbind(mlxsw_sp, block, binding);
231         block->ruleset_zero = NULL;
232 }
233
234 struct mlxsw_sp_acl_block *mlxsw_sp_acl_block_create(struct mlxsw_sp *mlxsw_sp,
235                                                      struct net *net)
236 {
237         struct mlxsw_sp_acl_block *block;
238
239         block = kzalloc(sizeof(*block), GFP_KERNEL);
240         if (!block)
241                 return NULL;
242         INIT_LIST_HEAD(&block->binding_list);
243         block->mlxsw_sp = mlxsw_sp;
244         return block;
245 }
246
247 void mlxsw_sp_acl_block_destroy(struct mlxsw_sp_acl_block *block)
248 {
249         WARN_ON(!list_empty(&block->binding_list));
250         kfree(block);
251 }
252
253 static struct mlxsw_sp_acl_block_binding *
254 mlxsw_sp_acl_block_lookup(struct mlxsw_sp_acl_block *block,
255                           struct mlxsw_sp_port *mlxsw_sp_port, bool ingress)
256 {
257         struct mlxsw_sp_acl_block_binding *binding;
258
259         list_for_each_entry(binding, &block->binding_list, list)
260                 if (binding->mlxsw_sp_port == mlxsw_sp_port &&
261                     binding->ingress == ingress)
262                         return binding;
263         return NULL;
264 }
265
266 int mlxsw_sp_acl_block_bind(struct mlxsw_sp *mlxsw_sp,
267                             struct mlxsw_sp_acl_block *block,
268                             struct mlxsw_sp_port *mlxsw_sp_port,
269                             bool ingress)
270 {
271         struct mlxsw_sp_acl_block_binding *binding;
272         int err;
273
274         if (WARN_ON(mlxsw_sp_acl_block_lookup(block, mlxsw_sp_port, ingress)))
275                 return -EEXIST;
276
277         binding = kzalloc(sizeof(*binding), GFP_KERNEL);
278         if (!binding)
279                 return -ENOMEM;
280         binding->mlxsw_sp_port = mlxsw_sp_port;
281         binding->ingress = ingress;
282
283         if (mlxsw_sp_acl_ruleset_block_bound(block)) {
284                 err = mlxsw_sp_acl_ruleset_bind(mlxsw_sp, block, binding);
285                 if (err)
286                         goto err_ruleset_bind;
287         }
288
289         list_add(&binding->list, &block->binding_list);
290         return 0;
291
292 err_ruleset_bind:
293         kfree(binding);
294         return err;
295 }
296
297 int mlxsw_sp_acl_block_unbind(struct mlxsw_sp *mlxsw_sp,
298                               struct mlxsw_sp_acl_block *block,
299                               struct mlxsw_sp_port *mlxsw_sp_port,
300                               bool ingress)
301 {
302         struct mlxsw_sp_acl_block_binding *binding;
303
304         binding = mlxsw_sp_acl_block_lookup(block, mlxsw_sp_port, ingress);
305         if (!binding)
306                 return -ENOENT;
307
308         list_del(&binding->list);
309
310         if (mlxsw_sp_acl_ruleset_block_bound(block))
311                 mlxsw_sp_acl_ruleset_unbind(mlxsw_sp, block, binding);
312
313         kfree(binding);
314         return 0;
315 }
316
317 static struct mlxsw_sp_acl_ruleset *
318 mlxsw_sp_acl_ruleset_create(struct mlxsw_sp *mlxsw_sp,
319                             struct mlxsw_sp_acl_block *block, u32 chain_index,
320                             const struct mlxsw_sp_acl_profile_ops *ops,
321                             struct mlxsw_afk_element_usage *tmplt_elusage)
322 {
323         struct mlxsw_sp_acl *acl = mlxsw_sp->acl;
324         struct mlxsw_sp_acl_ruleset *ruleset;
325         size_t alloc_size;
326         int err;
327
328         alloc_size = sizeof(*ruleset) + ops->ruleset_priv_size;
329         ruleset = kzalloc(alloc_size, GFP_KERNEL);
330         if (!ruleset)
331                 return ERR_PTR(-ENOMEM);
332         ruleset->ref_count = 1;
333         ruleset->ht_key.block = block;
334         ruleset->ht_key.chain_index = chain_index;
335         ruleset->ht_key.ops = ops;
336
337         err = rhashtable_init(&ruleset->rule_ht, &mlxsw_sp_acl_rule_ht_params);
338         if (err)
339                 goto err_rhashtable_init;
340
341         err = ops->ruleset_add(mlxsw_sp, &acl->tcam, ruleset->priv,
342                                tmplt_elusage);
343         if (err)
344                 goto err_ops_ruleset_add;
345
346         err = rhashtable_insert_fast(&acl->ruleset_ht, &ruleset->ht_node,
347                                      mlxsw_sp_acl_ruleset_ht_params);
348         if (err)
349                 goto err_ht_insert;
350
351         return ruleset;
352
353 err_ht_insert:
354         ops->ruleset_del(mlxsw_sp, ruleset->priv);
355 err_ops_ruleset_add:
356         rhashtable_destroy(&ruleset->rule_ht);
357 err_rhashtable_init:
358         kfree(ruleset);
359         return ERR_PTR(err);
360 }
361
362 static void mlxsw_sp_acl_ruleset_destroy(struct mlxsw_sp *mlxsw_sp,
363                                          struct mlxsw_sp_acl_ruleset *ruleset)
364 {
365         const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
366         struct mlxsw_sp_acl *acl = mlxsw_sp->acl;
367
368         rhashtable_remove_fast(&acl->ruleset_ht, &ruleset->ht_node,
369                                mlxsw_sp_acl_ruleset_ht_params);
370         ops->ruleset_del(mlxsw_sp, ruleset->priv);
371         rhashtable_destroy(&ruleset->rule_ht);
372         kfree(ruleset);
373 }
374
375 static void mlxsw_sp_acl_ruleset_ref_inc(struct mlxsw_sp_acl_ruleset *ruleset)
376 {
377         ruleset->ref_count++;
378 }
379
380 static void mlxsw_sp_acl_ruleset_ref_dec(struct mlxsw_sp *mlxsw_sp,
381                                          struct mlxsw_sp_acl_ruleset *ruleset)
382 {
383         if (--ruleset->ref_count)
384                 return;
385         mlxsw_sp_acl_ruleset_destroy(mlxsw_sp, ruleset);
386 }
387
388 static struct mlxsw_sp_acl_ruleset *
389 __mlxsw_sp_acl_ruleset_lookup(struct mlxsw_sp_acl *acl,
390                               struct mlxsw_sp_acl_block *block, u32 chain_index,
391                               const struct mlxsw_sp_acl_profile_ops *ops)
392 {
393         struct mlxsw_sp_acl_ruleset_ht_key ht_key;
394
395         memset(&ht_key, 0, sizeof(ht_key));
396         ht_key.block = block;
397         ht_key.chain_index = chain_index;
398         ht_key.ops = ops;
399         return rhashtable_lookup_fast(&acl->ruleset_ht, &ht_key,
400                                       mlxsw_sp_acl_ruleset_ht_params);
401 }
402
403 struct mlxsw_sp_acl_ruleset *
404 mlxsw_sp_acl_ruleset_lookup(struct mlxsw_sp *mlxsw_sp,
405                             struct mlxsw_sp_acl_block *block, u32 chain_index,
406                             enum mlxsw_sp_acl_profile profile)
407 {
408         const struct mlxsw_sp_acl_profile_ops *ops;
409         struct mlxsw_sp_acl *acl = mlxsw_sp->acl;
410         struct mlxsw_sp_acl_ruleset *ruleset;
411
412         ops = mlxsw_sp_acl_tcam_profile_ops(mlxsw_sp, profile);
413         if (!ops)
414                 return ERR_PTR(-EINVAL);
415         ruleset = __mlxsw_sp_acl_ruleset_lookup(acl, block, chain_index, ops);
416         if (!ruleset)
417                 return ERR_PTR(-ENOENT);
418         return ruleset;
419 }
420
421 struct mlxsw_sp_acl_ruleset *
422 mlxsw_sp_acl_ruleset_get(struct mlxsw_sp *mlxsw_sp,
423                          struct mlxsw_sp_acl_block *block, u32 chain_index,
424                          enum mlxsw_sp_acl_profile profile,
425                          struct mlxsw_afk_element_usage *tmplt_elusage)
426 {
427         const struct mlxsw_sp_acl_profile_ops *ops;
428         struct mlxsw_sp_acl *acl = mlxsw_sp->acl;
429         struct mlxsw_sp_acl_ruleset *ruleset;
430
431         ops = mlxsw_sp_acl_tcam_profile_ops(mlxsw_sp, profile);
432         if (!ops)
433                 return ERR_PTR(-EINVAL);
434
435         ruleset = __mlxsw_sp_acl_ruleset_lookup(acl, block, chain_index, ops);
436         if (ruleset) {
437                 mlxsw_sp_acl_ruleset_ref_inc(ruleset);
438                 return ruleset;
439         }
440         return mlxsw_sp_acl_ruleset_create(mlxsw_sp, block, chain_index, ops,
441                                            tmplt_elusage);
442 }
443
444 void mlxsw_sp_acl_ruleset_put(struct mlxsw_sp *mlxsw_sp,
445                               struct mlxsw_sp_acl_ruleset *ruleset)
446 {
447         mlxsw_sp_acl_ruleset_ref_dec(mlxsw_sp, ruleset);
448 }
449
450 u16 mlxsw_sp_acl_ruleset_group_id(struct mlxsw_sp_acl_ruleset *ruleset)
451 {
452         const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
453
454         return ops->ruleset_group_id(ruleset->priv);
455 }
456
457 struct mlxsw_sp_acl_rule_info *
458 mlxsw_sp_acl_rulei_create(struct mlxsw_sp_acl *acl)
459 {
460         struct mlxsw_sp_acl_rule_info *rulei;
461         int err;
462
463         rulei = kzalloc(sizeof(*rulei), GFP_KERNEL);
464         if (!rulei)
465                 return NULL;
466         rulei->act_block = mlxsw_afa_block_create(acl->mlxsw_sp->afa);
467         if (IS_ERR(rulei->act_block)) {
468                 err = PTR_ERR(rulei->act_block);
469                 goto err_afa_block_create;
470         }
471         return rulei;
472
473 err_afa_block_create:
474         kfree(rulei);
475         return ERR_PTR(err);
476 }
477
478 void mlxsw_sp_acl_rulei_destroy(struct mlxsw_sp_acl_rule_info *rulei)
479 {
480         mlxsw_afa_block_destroy(rulei->act_block);
481         kfree(rulei);
482 }
483
484 int mlxsw_sp_acl_rulei_commit(struct mlxsw_sp_acl_rule_info *rulei)
485 {
486         return mlxsw_afa_block_commit(rulei->act_block);
487 }
488
489 void mlxsw_sp_acl_rulei_priority(struct mlxsw_sp_acl_rule_info *rulei,
490                                  unsigned int priority)
491 {
492         rulei->priority = priority >> 16;
493 }
494
495 void mlxsw_sp_acl_rulei_keymask_u32(struct mlxsw_sp_acl_rule_info *rulei,
496                                     enum mlxsw_afk_element element,
497                                     u32 key_value, u32 mask_value)
498 {
499         mlxsw_afk_values_add_u32(&rulei->values, element,
500                                  key_value, mask_value);
501 }
502
503 void mlxsw_sp_acl_rulei_keymask_buf(struct mlxsw_sp_acl_rule_info *rulei,
504                                     enum mlxsw_afk_element element,
505                                     const char *key_value,
506                                     const char *mask_value, unsigned int len)
507 {
508         mlxsw_afk_values_add_buf(&rulei->values, element,
509                                  key_value, mask_value, len);
510 }
511
512 int mlxsw_sp_acl_rulei_act_continue(struct mlxsw_sp_acl_rule_info *rulei)
513 {
514         return mlxsw_afa_block_continue(rulei->act_block);
515 }
516
517 int mlxsw_sp_acl_rulei_act_jump(struct mlxsw_sp_acl_rule_info *rulei,
518                                 u16 group_id)
519 {
520         return mlxsw_afa_block_jump(rulei->act_block, group_id);
521 }
522
523 int mlxsw_sp_acl_rulei_act_terminate(struct mlxsw_sp_acl_rule_info *rulei)
524 {
525         return mlxsw_afa_block_terminate(rulei->act_block);
526 }
527
528 int mlxsw_sp_acl_rulei_act_drop(struct mlxsw_sp_acl_rule_info *rulei)
529 {
530         return mlxsw_afa_block_append_drop(rulei->act_block);
531 }
532
533 int mlxsw_sp_acl_rulei_act_trap(struct mlxsw_sp_acl_rule_info *rulei)
534 {
535         return mlxsw_afa_block_append_trap(rulei->act_block,
536                                            MLXSW_TRAP_ID_ACL0);
537 }
538
539 int mlxsw_sp_acl_rulei_act_fwd(struct mlxsw_sp *mlxsw_sp,
540                                struct mlxsw_sp_acl_rule_info *rulei,
541                                struct net_device *out_dev,
542                                struct netlink_ext_ack *extack)
543 {
544         struct mlxsw_sp_port *mlxsw_sp_port;
545         u8 local_port;
546         bool in_port;
547
548         if (out_dev) {
549                 if (!mlxsw_sp_port_dev_check(out_dev))
550                         return -EINVAL;
551                 mlxsw_sp_port = netdev_priv(out_dev);
552                 if (mlxsw_sp_port->mlxsw_sp != mlxsw_sp)
553                         return -EINVAL;
554                 local_port = mlxsw_sp_port->local_port;
555                 in_port = false;
556         } else {
557                 /* If out_dev is NULL, the caller wants to
558                  * set forward to ingress port.
559                  */
560                 local_port = 0;
561                 in_port = true;
562         }
563         return mlxsw_afa_block_append_fwd(rulei->act_block,
564                                           local_port, in_port, extack);
565 }
566
567 int mlxsw_sp_acl_rulei_act_mirror(struct mlxsw_sp *mlxsw_sp,
568                                   struct mlxsw_sp_acl_rule_info *rulei,
569                                   struct mlxsw_sp_acl_block *block,
570                                   struct net_device *out_dev,
571                                   struct netlink_ext_ack *extack)
572 {
573         struct mlxsw_sp_acl_block_binding *binding;
574         struct mlxsw_sp_port *in_port;
575
576         if (!list_is_singular(&block->binding_list))
577                 return -EOPNOTSUPP;
578
579         binding = list_first_entry(&block->binding_list,
580                                    struct mlxsw_sp_acl_block_binding, list);
581         in_port = binding->mlxsw_sp_port;
582
583         return mlxsw_afa_block_append_mirror(rulei->act_block,
584                                              in_port->local_port,
585                                              out_dev,
586                                              binding->ingress,
587                                              extack);
588 }
589
590 int mlxsw_sp_acl_rulei_act_vlan(struct mlxsw_sp *mlxsw_sp,
591                                 struct mlxsw_sp_acl_rule_info *rulei,
592                                 u32 action, u16 vid, u16 proto, u8 prio,
593                                 struct netlink_ext_ack *extack)
594 {
595         u8 ethertype;
596
597         if (action == TCA_VLAN_ACT_MODIFY) {
598                 switch (proto) {
599                 case ETH_P_8021Q:
600                         ethertype = 0;
601                         break;
602                 case ETH_P_8021AD:
603                         ethertype = 1;
604                         break;
605                 default:
606                         dev_err(mlxsw_sp->bus_info->dev, "Unsupported VLAN protocol %#04x\n",
607                                 proto);
608                         return -EINVAL;
609                 }
610
611                 return mlxsw_afa_block_append_vlan_modify(rulei->act_block,
612                                                           vid, prio, ethertype,
613                                                           extack);
614         } else {
615                 dev_err(mlxsw_sp->bus_info->dev, "Unsupported VLAN action\n");
616                 return -EINVAL;
617         }
618 }
619
620 int mlxsw_sp_acl_rulei_act_count(struct mlxsw_sp *mlxsw_sp,
621                                  struct mlxsw_sp_acl_rule_info *rulei,
622                                  struct netlink_ext_ack *extack)
623 {
624         return mlxsw_afa_block_append_counter(rulei->act_block,
625                                               &rulei->counter_index, extack);
626 }
627
628 int mlxsw_sp_acl_rulei_act_fid_set(struct mlxsw_sp *mlxsw_sp,
629                                    struct mlxsw_sp_acl_rule_info *rulei,
630                                    u16 fid, struct netlink_ext_ack *extack)
631 {
632         return mlxsw_afa_block_append_fid_set(rulei->act_block, fid, extack);
633 }
634
635 struct mlxsw_sp_acl_rule *
636 mlxsw_sp_acl_rule_create(struct mlxsw_sp *mlxsw_sp,
637                          struct mlxsw_sp_acl_ruleset *ruleset,
638                          unsigned long cookie,
639                          struct netlink_ext_ack *extack)
640 {
641         const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
642         struct mlxsw_sp_acl_rule *rule;
643         int err;
644
645         mlxsw_sp_acl_ruleset_ref_inc(ruleset);
646         rule = kzalloc(sizeof(*rule) + ops->rule_priv_size(mlxsw_sp),
647                        GFP_KERNEL);
648         if (!rule) {
649                 err = -ENOMEM;
650                 goto err_alloc;
651         }
652         rule->cookie = cookie;
653         rule->ruleset = ruleset;
654
655         rule->rulei = mlxsw_sp_acl_rulei_create(mlxsw_sp->acl);
656         if (IS_ERR(rule->rulei)) {
657                 err = PTR_ERR(rule->rulei);
658                 goto err_rulei_create;
659         }
660
661         return rule;
662
663 err_rulei_create:
664         kfree(rule);
665 err_alloc:
666         mlxsw_sp_acl_ruleset_ref_dec(mlxsw_sp, ruleset);
667         return ERR_PTR(err);
668 }
669
670 void mlxsw_sp_acl_rule_destroy(struct mlxsw_sp *mlxsw_sp,
671                                struct mlxsw_sp_acl_rule *rule)
672 {
673         struct mlxsw_sp_acl_ruleset *ruleset = rule->ruleset;
674
675         mlxsw_sp_acl_rulei_destroy(rule->rulei);
676         kfree(rule);
677         mlxsw_sp_acl_ruleset_ref_dec(mlxsw_sp, ruleset);
678 }
679
680 int mlxsw_sp_acl_rule_add(struct mlxsw_sp *mlxsw_sp,
681                           struct mlxsw_sp_acl_rule *rule)
682 {
683         struct mlxsw_sp_acl_ruleset *ruleset = rule->ruleset;
684         const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
685         int err;
686
687         err = ops->rule_add(mlxsw_sp, ruleset->priv, rule->priv, rule->rulei);
688         if (err)
689                 return err;
690
691         err = rhashtable_insert_fast(&ruleset->rule_ht, &rule->ht_node,
692                                      mlxsw_sp_acl_rule_ht_params);
693         if (err)
694                 goto err_rhashtable_insert;
695
696         if (!ruleset->ht_key.chain_index &&
697             mlxsw_sp_acl_ruleset_is_singular(ruleset)) {
698                 /* We only need ruleset with chain index 0, the implicit
699                  * one, to be directly bound to device. The rest of the
700                  * rulesets are bound by "Goto action set".
701                  */
702                 err = mlxsw_sp_acl_ruleset_block_bind(mlxsw_sp, ruleset,
703                                                       ruleset->ht_key.block);
704                 if (err)
705                         goto err_ruleset_block_bind;
706         }
707
708         list_add_tail(&rule->list, &mlxsw_sp->acl->rules);
709         ruleset->ht_key.block->rule_count++;
710         return 0;
711
712 err_ruleset_block_bind:
713         rhashtable_remove_fast(&ruleset->rule_ht, &rule->ht_node,
714                                mlxsw_sp_acl_rule_ht_params);
715 err_rhashtable_insert:
716         ops->rule_del(mlxsw_sp, rule->priv);
717         return err;
718 }
719
720 void mlxsw_sp_acl_rule_del(struct mlxsw_sp *mlxsw_sp,
721                            struct mlxsw_sp_acl_rule *rule)
722 {
723         struct mlxsw_sp_acl_ruleset *ruleset = rule->ruleset;
724         const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
725
726         ruleset->ht_key.block->rule_count--;
727         list_del(&rule->list);
728         if (!ruleset->ht_key.chain_index &&
729             mlxsw_sp_acl_ruleset_is_singular(ruleset))
730                 mlxsw_sp_acl_ruleset_block_unbind(mlxsw_sp, ruleset,
731                                                   ruleset->ht_key.block);
732         rhashtable_remove_fast(&ruleset->rule_ht, &rule->ht_node,
733                                mlxsw_sp_acl_rule_ht_params);
734         ops->rule_del(mlxsw_sp, rule->priv);
735 }
736
737 struct mlxsw_sp_acl_rule *
738 mlxsw_sp_acl_rule_lookup(struct mlxsw_sp *mlxsw_sp,
739                          struct mlxsw_sp_acl_ruleset *ruleset,
740                          unsigned long cookie)
741 {
742         return rhashtable_lookup_fast(&ruleset->rule_ht, &cookie,
743                                        mlxsw_sp_acl_rule_ht_params);
744 }
745
746 struct mlxsw_sp_acl_rule_info *
747 mlxsw_sp_acl_rule_rulei(struct mlxsw_sp_acl_rule *rule)
748 {
749         return rule->rulei;
750 }
751
752 static int mlxsw_sp_acl_rule_activity_update(struct mlxsw_sp *mlxsw_sp,
753                                              struct mlxsw_sp_acl_rule *rule)
754 {
755         struct mlxsw_sp_acl_ruleset *ruleset = rule->ruleset;
756         const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
757         bool active;
758         int err;
759
760         err = ops->rule_activity_get(mlxsw_sp, rule->priv, &active);
761         if (err)
762                 return err;
763         if (active)
764                 rule->last_used = jiffies;
765         return 0;
766 }
767
768 static int mlxsw_sp_acl_rules_activity_update(struct mlxsw_sp_acl *acl)
769 {
770         struct mlxsw_sp_acl_rule *rule;
771         int err;
772
773         /* Protect internal structures from changes */
774         rtnl_lock();
775         list_for_each_entry(rule, &acl->rules, list) {
776                 err = mlxsw_sp_acl_rule_activity_update(acl->mlxsw_sp,
777                                                         rule);
778                 if (err)
779                         goto err_rule_update;
780         }
781         rtnl_unlock();
782         return 0;
783
784 err_rule_update:
785         rtnl_unlock();
786         return err;
787 }
788
789 static void mlxsw_sp_acl_rule_activity_work_schedule(struct mlxsw_sp_acl *acl)
790 {
791         unsigned long interval = acl->rule_activity_update.interval;
792
793         mlxsw_core_schedule_dw(&acl->rule_activity_update.dw,
794                                msecs_to_jiffies(interval));
795 }
796
797 static void mlxsw_sp_acl_rul_activity_update_work(struct work_struct *work)
798 {
799         struct mlxsw_sp_acl *acl = container_of(work, struct mlxsw_sp_acl,
800                                                 rule_activity_update.dw.work);
801         int err;
802
803         err = mlxsw_sp_acl_rules_activity_update(acl);
804         if (err)
805                 dev_err(acl->mlxsw_sp->bus_info->dev, "Could not update acl activity");
806
807         mlxsw_sp_acl_rule_activity_work_schedule(acl);
808 }
809
810 int mlxsw_sp_acl_rule_get_stats(struct mlxsw_sp *mlxsw_sp,
811                                 struct mlxsw_sp_acl_rule *rule,
812                                 u64 *packets, u64 *bytes, u64 *last_use)
813
814 {
815         struct mlxsw_sp_acl_rule_info *rulei;
816         u64 current_packets;
817         u64 current_bytes;
818         int err;
819
820         rulei = mlxsw_sp_acl_rule_rulei(rule);
821         err = mlxsw_sp_flow_counter_get(mlxsw_sp, rulei->counter_index,
822                                         &current_packets, &current_bytes);
823         if (err)
824                 return err;
825
826         *packets = current_packets - rule->last_packets;
827         *bytes = current_bytes - rule->last_bytes;
828         *last_use = rule->last_used;
829
830         rule->last_bytes = current_bytes;
831         rule->last_packets = current_packets;
832
833         return 0;
834 }
835
836 int mlxsw_sp_acl_init(struct mlxsw_sp *mlxsw_sp)
837 {
838         struct mlxsw_sp_fid *fid;
839         struct mlxsw_sp_acl *acl;
840         size_t alloc_size;
841         int err;
842
843         alloc_size = sizeof(*acl) + mlxsw_sp_acl_tcam_priv_size(mlxsw_sp);
844         acl = kzalloc(alloc_size, GFP_KERNEL);
845         if (!acl)
846                 return -ENOMEM;
847         mlxsw_sp->acl = acl;
848         acl->mlxsw_sp = mlxsw_sp;
849         acl->afk = mlxsw_afk_create(MLXSW_CORE_RES_GET(mlxsw_sp->core,
850                                                        ACL_FLEX_KEYS),
851                                     mlxsw_sp->afk_ops);
852         if (!acl->afk) {
853                 err = -ENOMEM;
854                 goto err_afk_create;
855         }
856
857         err = rhashtable_init(&acl->ruleset_ht,
858                               &mlxsw_sp_acl_ruleset_ht_params);
859         if (err)
860                 goto err_rhashtable_init;
861
862         fid = mlxsw_sp_fid_dummy_get(mlxsw_sp);
863         if (IS_ERR(fid)) {
864                 err = PTR_ERR(fid);
865                 goto err_fid_get;
866         }
867         acl->dummy_fid = fid;
868
869         INIT_LIST_HEAD(&acl->rules);
870         err = mlxsw_sp_acl_tcam_init(mlxsw_sp, &acl->tcam);
871         if (err)
872                 goto err_acl_ops_init;
873
874         /* Create the delayed work for the rule activity_update */
875         INIT_DELAYED_WORK(&acl->rule_activity_update.dw,
876                           mlxsw_sp_acl_rul_activity_update_work);
877         acl->rule_activity_update.interval = MLXSW_SP_ACL_RULE_ACTIVITY_UPDATE_PERIOD_MS;
878         mlxsw_core_schedule_dw(&acl->rule_activity_update.dw, 0);
879         return 0;
880
881 err_acl_ops_init:
882         mlxsw_sp_fid_put(fid);
883 err_fid_get:
884         rhashtable_destroy(&acl->ruleset_ht);
885 err_rhashtable_init:
886         mlxsw_afk_destroy(acl->afk);
887 err_afk_create:
888         kfree(acl);
889         return err;
890 }
891
892 void mlxsw_sp_acl_fini(struct mlxsw_sp *mlxsw_sp)
893 {
894         struct mlxsw_sp_acl *acl = mlxsw_sp->acl;
895
896         cancel_delayed_work_sync(&mlxsw_sp->acl->rule_activity_update.dw);
897         mlxsw_sp_acl_tcam_fini(mlxsw_sp, &acl->tcam);
898         WARN_ON(!list_empty(&acl->rules));
899         mlxsw_sp_fid_put(acl->dummy_fid);
900         rhashtable_destroy(&acl->ruleset_ht);
901         mlxsw_afk_destroy(acl->afk);
902         kfree(acl);
903 }