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