Merge branch 'sched-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-microblaze.git] / drivers / net / ethernet / mellanox / mlxsw / core_acl_flex_actions.c
1 /*
2  * drivers/net/ethernet/mellanox/mlxsw/core_acl_flex_actions.c
3  * Copyright (c) 2017, 2018 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/types.h>
37 #include <linux/slab.h>
38 #include <linux/errno.h>
39 #include <linux/rhashtable.h>
40 #include <linux/list.h>
41
42 #include "item.h"
43 #include "trap.h"
44 #include "core_acl_flex_actions.h"
45
46 enum mlxsw_afa_set_type {
47         MLXSW_AFA_SET_TYPE_NEXT,
48         MLXSW_AFA_SET_TYPE_GOTO,
49 };
50
51 /* afa_set_type
52  * Type of the record at the end of the action set.
53  */
54 MLXSW_ITEM32(afa, set, type, 0xA0, 28, 4);
55
56 /* afa_set_next_action_set_ptr
57  * A pointer to the next action set in the KVD Centralized database.
58  */
59 MLXSW_ITEM32(afa, set, next_action_set_ptr, 0xA4, 0, 24);
60
61 /* afa_set_goto_g
62  * group - When set, the binding is of an ACL group. When cleared,
63  * the binding is of an ACL.
64  * Must be set to 1 for Spectrum.
65  */
66 MLXSW_ITEM32(afa, set, goto_g, 0xA4, 29, 1);
67
68 enum mlxsw_afa_set_goto_binding_cmd {
69         /* continue go the next binding point */
70         MLXSW_AFA_SET_GOTO_BINDING_CMD_NONE,
71         /* jump to the next binding point no return */
72         MLXSW_AFA_SET_GOTO_BINDING_CMD_JUMP,
73         /* terminate the acl binding */
74         MLXSW_AFA_SET_GOTO_BINDING_CMD_TERM = 4,
75 };
76
77 /* afa_set_goto_binding_cmd */
78 MLXSW_ITEM32(afa, set, goto_binding_cmd, 0xA4, 24, 3);
79
80 /* afa_set_goto_next_binding
81  * ACL/ACL group identifier. If the g bit is set, this field should hold
82  * the acl_group_id, else it should hold the acl_id.
83  */
84 MLXSW_ITEM32(afa, set, goto_next_binding, 0xA4, 0, 16);
85
86 /* afa_all_action_type
87  * Action Type.
88  */
89 MLXSW_ITEM32(afa, all, action_type, 0x00, 24, 6);
90
91 struct mlxsw_afa {
92         unsigned int max_acts_per_set;
93         const struct mlxsw_afa_ops *ops;
94         void *ops_priv;
95         struct rhashtable set_ht;
96         struct rhashtable fwd_entry_ht;
97 };
98
99 #define MLXSW_AFA_SET_LEN 0xA8
100
101 struct mlxsw_afa_set_ht_key {
102         char enc_actions[MLXSW_AFA_SET_LEN]; /* Encoded set */
103         bool is_first;
104 };
105
106 /* Set structure holds one action set record. It contains up to three
107  * actions (depends on size of particular actions). The set is either
108  * put directly to a rule, or it is stored in KVD linear area.
109  * To prevent duplicate entries in KVD linear area, a hashtable is
110  * used to track sets that were previously inserted and may be shared.
111  */
112
113 struct mlxsw_afa_set {
114         struct rhash_head ht_node;
115         struct mlxsw_afa_set_ht_key ht_key;
116         u32 kvdl_index;
117         bool shared; /* Inserted in hashtable (doesn't mean that
118                       * kvdl_index is valid).
119                       */
120         unsigned int ref_count;
121         struct mlxsw_afa_set *next; /* Pointer to the next set. */
122         struct mlxsw_afa_set *prev; /* Pointer to the previous set,
123                                      * note that set may have multiple
124                                      * sets from multiple blocks
125                                      * pointing at it. This is only
126                                      * usable until commit.
127                                      */
128 };
129
130 static const struct rhashtable_params mlxsw_afa_set_ht_params = {
131         .key_len = sizeof(struct mlxsw_afa_set_ht_key),
132         .key_offset = offsetof(struct mlxsw_afa_set, ht_key),
133         .head_offset = offsetof(struct mlxsw_afa_set, ht_node),
134         .automatic_shrinking = true,
135 };
136
137 struct mlxsw_afa_fwd_entry_ht_key {
138         u8 local_port;
139 };
140
141 struct mlxsw_afa_fwd_entry {
142         struct rhash_head ht_node;
143         struct mlxsw_afa_fwd_entry_ht_key ht_key;
144         u32 kvdl_index;
145         unsigned int ref_count;
146 };
147
148 static const struct rhashtable_params mlxsw_afa_fwd_entry_ht_params = {
149         .key_len = sizeof(struct mlxsw_afa_fwd_entry_ht_key),
150         .key_offset = offsetof(struct mlxsw_afa_fwd_entry, ht_key),
151         .head_offset = offsetof(struct mlxsw_afa_fwd_entry, ht_node),
152         .automatic_shrinking = true,
153 };
154
155 struct mlxsw_afa *mlxsw_afa_create(unsigned int max_acts_per_set,
156                                    const struct mlxsw_afa_ops *ops,
157                                    void *ops_priv)
158 {
159         struct mlxsw_afa *mlxsw_afa;
160         int err;
161
162         mlxsw_afa = kzalloc(sizeof(*mlxsw_afa), GFP_KERNEL);
163         if (!mlxsw_afa)
164                 return ERR_PTR(-ENOMEM);
165         err = rhashtable_init(&mlxsw_afa->set_ht, &mlxsw_afa_set_ht_params);
166         if (err)
167                 goto err_set_rhashtable_init;
168         err = rhashtable_init(&mlxsw_afa->fwd_entry_ht,
169                               &mlxsw_afa_fwd_entry_ht_params);
170         if (err)
171                 goto err_fwd_entry_rhashtable_init;
172         mlxsw_afa->max_acts_per_set = max_acts_per_set;
173         mlxsw_afa->ops = ops;
174         mlxsw_afa->ops_priv = ops_priv;
175         return mlxsw_afa;
176
177 err_fwd_entry_rhashtable_init:
178         rhashtable_destroy(&mlxsw_afa->set_ht);
179 err_set_rhashtable_init:
180         kfree(mlxsw_afa);
181         return ERR_PTR(err);
182 }
183 EXPORT_SYMBOL(mlxsw_afa_create);
184
185 void mlxsw_afa_destroy(struct mlxsw_afa *mlxsw_afa)
186 {
187         rhashtable_destroy(&mlxsw_afa->fwd_entry_ht);
188         rhashtable_destroy(&mlxsw_afa->set_ht);
189         kfree(mlxsw_afa);
190 }
191 EXPORT_SYMBOL(mlxsw_afa_destroy);
192
193 static void mlxsw_afa_set_goto_set(struct mlxsw_afa_set *set,
194                                    enum mlxsw_afa_set_goto_binding_cmd cmd,
195                                    u16 group_id)
196 {
197         char *actions = set->ht_key.enc_actions;
198
199         mlxsw_afa_set_type_set(actions, MLXSW_AFA_SET_TYPE_GOTO);
200         mlxsw_afa_set_goto_g_set(actions, true);
201         mlxsw_afa_set_goto_binding_cmd_set(actions, cmd);
202         mlxsw_afa_set_goto_next_binding_set(actions, group_id);
203 }
204
205 static void mlxsw_afa_set_next_set(struct mlxsw_afa_set *set,
206                                    u32 next_set_kvdl_index)
207 {
208         char *actions = set->ht_key.enc_actions;
209
210         mlxsw_afa_set_type_set(actions, MLXSW_AFA_SET_TYPE_NEXT);
211         mlxsw_afa_set_next_action_set_ptr_set(actions, next_set_kvdl_index);
212 }
213
214 static struct mlxsw_afa_set *mlxsw_afa_set_create(bool is_first)
215 {
216         struct mlxsw_afa_set *set;
217
218         set = kzalloc(sizeof(*set), GFP_KERNEL);
219         if (!set)
220                 return NULL;
221         /* Need to initialize the set to pass by default */
222         mlxsw_afa_set_goto_set(set, MLXSW_AFA_SET_GOTO_BINDING_CMD_TERM, 0);
223         set->ht_key.is_first = is_first;
224         set->ref_count = 1;
225         return set;
226 }
227
228 static void mlxsw_afa_set_destroy(struct mlxsw_afa_set *set)
229 {
230         kfree(set);
231 }
232
233 static int mlxsw_afa_set_share(struct mlxsw_afa *mlxsw_afa,
234                                struct mlxsw_afa_set *set)
235 {
236         int err;
237
238         err = rhashtable_insert_fast(&mlxsw_afa->set_ht, &set->ht_node,
239                                      mlxsw_afa_set_ht_params);
240         if (err)
241                 return err;
242         err = mlxsw_afa->ops->kvdl_set_add(mlxsw_afa->ops_priv,
243                                            &set->kvdl_index,
244                                            set->ht_key.enc_actions,
245                                            set->ht_key.is_first);
246         if (err)
247                 goto err_kvdl_set_add;
248         set->shared = true;
249         set->prev = NULL;
250         return 0;
251
252 err_kvdl_set_add:
253         rhashtable_remove_fast(&mlxsw_afa->set_ht, &set->ht_node,
254                                mlxsw_afa_set_ht_params);
255         return err;
256 }
257
258 static void mlxsw_afa_set_unshare(struct mlxsw_afa *mlxsw_afa,
259                                   struct mlxsw_afa_set *set)
260 {
261         mlxsw_afa->ops->kvdl_set_del(mlxsw_afa->ops_priv,
262                                      set->kvdl_index,
263                                      set->ht_key.is_first);
264         rhashtable_remove_fast(&mlxsw_afa->set_ht, &set->ht_node,
265                                mlxsw_afa_set_ht_params);
266         set->shared = false;
267 }
268
269 static void mlxsw_afa_set_put(struct mlxsw_afa *mlxsw_afa,
270                               struct mlxsw_afa_set *set)
271 {
272         if (--set->ref_count)
273                 return;
274         if (set->shared)
275                 mlxsw_afa_set_unshare(mlxsw_afa, set);
276         mlxsw_afa_set_destroy(set);
277 }
278
279 static struct mlxsw_afa_set *mlxsw_afa_set_get(struct mlxsw_afa *mlxsw_afa,
280                                                struct mlxsw_afa_set *orig_set)
281 {
282         struct mlxsw_afa_set *set;
283         int err;
284
285         /* There is a hashtable of sets maintained. If a set with the exact
286          * same encoding exists, we reuse it. Otherwise, the current set
287          * is shared by making it available to others using the hash table.
288          */
289         set = rhashtable_lookup_fast(&mlxsw_afa->set_ht, &orig_set->ht_key,
290                                      mlxsw_afa_set_ht_params);
291         if (set) {
292                 set->ref_count++;
293                 mlxsw_afa_set_put(mlxsw_afa, orig_set);
294         } else {
295                 set = orig_set;
296                 err = mlxsw_afa_set_share(mlxsw_afa, set);
297                 if (err)
298                         return ERR_PTR(err);
299         }
300         return set;
301 }
302
303 /* Block structure holds a list of action sets. One action block
304  * represents one chain of actions executed upon match of a rule.
305  */
306
307 struct mlxsw_afa_block {
308         struct mlxsw_afa *afa;
309         bool finished;
310         struct mlxsw_afa_set *first_set;
311         struct mlxsw_afa_set *cur_set;
312         unsigned int cur_act_index; /* In current set. */
313         struct list_head resource_list; /* List of resources held by actions
314                                          * in this block.
315                                          */
316 };
317
318 struct mlxsw_afa_resource {
319         struct list_head list;
320         void (*destructor)(struct mlxsw_afa_block *block,
321                            struct mlxsw_afa_resource *resource);
322 };
323
324 static void mlxsw_afa_resource_add(struct mlxsw_afa_block *block,
325                                    struct mlxsw_afa_resource *resource)
326 {
327         list_add(&resource->list, &block->resource_list);
328 }
329
330 static void mlxsw_afa_resource_del(struct mlxsw_afa_resource *resource)
331 {
332         list_del(&resource->list);
333 }
334
335 static void mlxsw_afa_resources_destroy(struct mlxsw_afa_block *block)
336 {
337         struct mlxsw_afa_resource *resource, *tmp;
338
339         list_for_each_entry_safe(resource, tmp, &block->resource_list, list) {
340                 resource->destructor(block, resource);
341         }
342 }
343
344 struct mlxsw_afa_block *mlxsw_afa_block_create(struct mlxsw_afa *mlxsw_afa)
345 {
346         struct mlxsw_afa_block *block;
347
348         block = kzalloc(sizeof(*block), GFP_KERNEL);
349         if (!block)
350                 return NULL;
351         INIT_LIST_HEAD(&block->resource_list);
352         block->afa = mlxsw_afa;
353
354         /* At least one action set is always present, so just create it here */
355         block->first_set = mlxsw_afa_set_create(true);
356         if (!block->first_set)
357                 goto err_first_set_create;
358         block->cur_set = block->first_set;
359         return block;
360
361 err_first_set_create:
362         kfree(block);
363         return NULL;
364 }
365 EXPORT_SYMBOL(mlxsw_afa_block_create);
366
367 void mlxsw_afa_block_destroy(struct mlxsw_afa_block *block)
368 {
369         struct mlxsw_afa_set *set = block->first_set;
370         struct mlxsw_afa_set *next_set;
371
372         do {
373                 next_set = set->next;
374                 mlxsw_afa_set_put(block->afa, set);
375                 set = next_set;
376         } while (set);
377         mlxsw_afa_resources_destroy(block);
378         kfree(block);
379 }
380 EXPORT_SYMBOL(mlxsw_afa_block_destroy);
381
382 int mlxsw_afa_block_commit(struct mlxsw_afa_block *block)
383 {
384         struct mlxsw_afa_set *set = block->cur_set;
385         struct mlxsw_afa_set *prev_set;
386
387         block->cur_set = NULL;
388         block->finished = true;
389
390         /* Go over all linked sets starting from last
391          * and try to find existing set in the hash table.
392          * In case it is not there, assign a KVD linear index
393          * and insert it.
394          */
395         do {
396                 prev_set = set->prev;
397                 set = mlxsw_afa_set_get(block->afa, set);
398                 if (IS_ERR(set))
399                         /* No rollback is needed since the chain is
400                          * in consistent state and mlxsw_afa_block_destroy
401                          * will take care of putting it away.
402                          */
403                         return PTR_ERR(set);
404                 if (prev_set) {
405                         prev_set->next = set;
406                         mlxsw_afa_set_next_set(prev_set, set->kvdl_index);
407                         set = prev_set;
408                 }
409         } while (prev_set);
410
411         block->first_set = set;
412         return 0;
413 }
414 EXPORT_SYMBOL(mlxsw_afa_block_commit);
415
416 char *mlxsw_afa_block_first_set(struct mlxsw_afa_block *block)
417 {
418         return block->first_set->ht_key.enc_actions;
419 }
420 EXPORT_SYMBOL(mlxsw_afa_block_first_set);
421
422 u32 mlxsw_afa_block_first_set_kvdl_index(struct mlxsw_afa_block *block)
423 {
424         return block->first_set->kvdl_index;
425 }
426 EXPORT_SYMBOL(mlxsw_afa_block_first_set_kvdl_index);
427
428 int mlxsw_afa_block_continue(struct mlxsw_afa_block *block)
429 {
430         if (block->finished)
431                 return -EINVAL;
432         mlxsw_afa_set_goto_set(block->cur_set,
433                                MLXSW_AFA_SET_GOTO_BINDING_CMD_NONE, 0);
434         block->finished = true;
435         return 0;
436 }
437 EXPORT_SYMBOL(mlxsw_afa_block_continue);
438
439 int mlxsw_afa_block_jump(struct mlxsw_afa_block *block, u16 group_id)
440 {
441         if (block->finished)
442                 return -EINVAL;
443         mlxsw_afa_set_goto_set(block->cur_set,
444                                MLXSW_AFA_SET_GOTO_BINDING_CMD_JUMP, group_id);
445         block->finished = true;
446         return 0;
447 }
448 EXPORT_SYMBOL(mlxsw_afa_block_jump);
449
450 int mlxsw_afa_block_terminate(struct mlxsw_afa_block *block)
451 {
452         if (block->finished)
453                 return -EINVAL;
454         mlxsw_afa_set_goto_set(block->cur_set,
455                                MLXSW_AFA_SET_GOTO_BINDING_CMD_TERM, 0);
456         block->finished = true;
457         return 0;
458 }
459 EXPORT_SYMBOL(mlxsw_afa_block_terminate);
460
461 static struct mlxsw_afa_fwd_entry *
462 mlxsw_afa_fwd_entry_create(struct mlxsw_afa *mlxsw_afa, u8 local_port)
463 {
464         struct mlxsw_afa_fwd_entry *fwd_entry;
465         int err;
466
467         fwd_entry = kzalloc(sizeof(*fwd_entry), GFP_KERNEL);
468         if (!fwd_entry)
469                 return ERR_PTR(-ENOMEM);
470         fwd_entry->ht_key.local_port = local_port;
471         fwd_entry->ref_count = 1;
472
473         err = rhashtable_insert_fast(&mlxsw_afa->fwd_entry_ht,
474                                      &fwd_entry->ht_node,
475                                      mlxsw_afa_fwd_entry_ht_params);
476         if (err)
477                 goto err_rhashtable_insert;
478
479         err = mlxsw_afa->ops->kvdl_fwd_entry_add(mlxsw_afa->ops_priv,
480                                                  &fwd_entry->kvdl_index,
481                                                  local_port);
482         if (err)
483                 goto err_kvdl_fwd_entry_add;
484         return fwd_entry;
485
486 err_kvdl_fwd_entry_add:
487         rhashtable_remove_fast(&mlxsw_afa->fwd_entry_ht, &fwd_entry->ht_node,
488                                mlxsw_afa_fwd_entry_ht_params);
489 err_rhashtable_insert:
490         kfree(fwd_entry);
491         return ERR_PTR(err);
492 }
493
494 static void mlxsw_afa_fwd_entry_destroy(struct mlxsw_afa *mlxsw_afa,
495                                         struct mlxsw_afa_fwd_entry *fwd_entry)
496 {
497         mlxsw_afa->ops->kvdl_fwd_entry_del(mlxsw_afa->ops_priv,
498                                            fwd_entry->kvdl_index);
499         rhashtable_remove_fast(&mlxsw_afa->fwd_entry_ht, &fwd_entry->ht_node,
500                                mlxsw_afa_fwd_entry_ht_params);
501         kfree(fwd_entry);
502 }
503
504 static struct mlxsw_afa_fwd_entry *
505 mlxsw_afa_fwd_entry_get(struct mlxsw_afa *mlxsw_afa, u8 local_port)
506 {
507         struct mlxsw_afa_fwd_entry_ht_key ht_key = {0};
508         struct mlxsw_afa_fwd_entry *fwd_entry;
509
510         ht_key.local_port = local_port;
511         fwd_entry = rhashtable_lookup_fast(&mlxsw_afa->fwd_entry_ht, &ht_key,
512                                            mlxsw_afa_fwd_entry_ht_params);
513         if (fwd_entry) {
514                 fwd_entry->ref_count++;
515                 return fwd_entry;
516         }
517         return mlxsw_afa_fwd_entry_create(mlxsw_afa, local_port);
518 }
519
520 static void mlxsw_afa_fwd_entry_put(struct mlxsw_afa *mlxsw_afa,
521                                     struct mlxsw_afa_fwd_entry *fwd_entry)
522 {
523         if (--fwd_entry->ref_count)
524                 return;
525         mlxsw_afa_fwd_entry_destroy(mlxsw_afa, fwd_entry);
526 }
527
528 struct mlxsw_afa_fwd_entry_ref {
529         struct mlxsw_afa_resource resource;
530         struct mlxsw_afa_fwd_entry *fwd_entry;
531 };
532
533 static void
534 mlxsw_afa_fwd_entry_ref_destroy(struct mlxsw_afa_block *block,
535                                 struct mlxsw_afa_fwd_entry_ref *fwd_entry_ref)
536 {
537         mlxsw_afa_resource_del(&fwd_entry_ref->resource);
538         mlxsw_afa_fwd_entry_put(block->afa, fwd_entry_ref->fwd_entry);
539         kfree(fwd_entry_ref);
540 }
541
542 static void
543 mlxsw_afa_fwd_entry_ref_destructor(struct mlxsw_afa_block *block,
544                                    struct mlxsw_afa_resource *resource)
545 {
546         struct mlxsw_afa_fwd_entry_ref *fwd_entry_ref;
547
548         fwd_entry_ref = container_of(resource, struct mlxsw_afa_fwd_entry_ref,
549                                      resource);
550         mlxsw_afa_fwd_entry_ref_destroy(block, fwd_entry_ref);
551 }
552
553 static struct mlxsw_afa_fwd_entry_ref *
554 mlxsw_afa_fwd_entry_ref_create(struct mlxsw_afa_block *block, u8 local_port)
555 {
556         struct mlxsw_afa_fwd_entry_ref *fwd_entry_ref;
557         struct mlxsw_afa_fwd_entry *fwd_entry;
558         int err;
559
560         fwd_entry_ref = kzalloc(sizeof(*fwd_entry_ref), GFP_KERNEL);
561         if (!fwd_entry_ref)
562                 return ERR_PTR(-ENOMEM);
563         fwd_entry = mlxsw_afa_fwd_entry_get(block->afa, local_port);
564         if (IS_ERR(fwd_entry)) {
565                 err = PTR_ERR(fwd_entry);
566                 goto err_fwd_entry_get;
567         }
568         fwd_entry_ref->fwd_entry = fwd_entry;
569         fwd_entry_ref->resource.destructor = mlxsw_afa_fwd_entry_ref_destructor;
570         mlxsw_afa_resource_add(block, &fwd_entry_ref->resource);
571         return fwd_entry_ref;
572
573 err_fwd_entry_get:
574         kfree(fwd_entry_ref);
575         return ERR_PTR(err);
576 }
577
578 struct mlxsw_afa_counter {
579         struct mlxsw_afa_resource resource;
580         u32 counter_index;
581 };
582
583 static void
584 mlxsw_afa_counter_destroy(struct mlxsw_afa_block *block,
585                           struct mlxsw_afa_counter *counter)
586 {
587         mlxsw_afa_resource_del(&counter->resource);
588         block->afa->ops->counter_index_put(block->afa->ops_priv,
589                                            counter->counter_index);
590         kfree(counter);
591 }
592
593 static void
594 mlxsw_afa_counter_destructor(struct mlxsw_afa_block *block,
595                              struct mlxsw_afa_resource *resource)
596 {
597         struct mlxsw_afa_counter *counter;
598
599         counter = container_of(resource, struct mlxsw_afa_counter, resource);
600         mlxsw_afa_counter_destroy(block, counter);
601 }
602
603 static struct mlxsw_afa_counter *
604 mlxsw_afa_counter_create(struct mlxsw_afa_block *block)
605 {
606         struct mlxsw_afa_counter *counter;
607         int err;
608
609         counter = kzalloc(sizeof(*counter), GFP_KERNEL);
610         if (!counter)
611                 return ERR_PTR(-ENOMEM);
612
613         err = block->afa->ops->counter_index_get(block->afa->ops_priv,
614                                                  &counter->counter_index);
615         if (err)
616                 goto err_counter_index_get;
617         counter->resource.destructor = mlxsw_afa_counter_destructor;
618         mlxsw_afa_resource_add(block, &counter->resource);
619         return counter;
620
621 err_counter_index_get:
622         kfree(counter);
623         return ERR_PTR(err);
624 }
625
626 #define MLXSW_AFA_ONE_ACTION_LEN 32
627 #define MLXSW_AFA_PAYLOAD_OFFSET 4
628
629 static char *mlxsw_afa_block_append_action(struct mlxsw_afa_block *block,
630                                            u8 action_code, u8 action_size)
631 {
632         char *oneact;
633         char *actions;
634
635         if (block->finished)
636                 return ERR_PTR(-EINVAL);
637         if (block->cur_act_index + action_size >
638             block->afa->max_acts_per_set) {
639                 struct mlxsw_afa_set *set;
640
641                 /* The appended action won't fit into the current action set,
642                  * so create a new set.
643                  */
644                 set = mlxsw_afa_set_create(false);
645                 if (!set)
646                         return ERR_PTR(-ENOBUFS);
647                 set->prev = block->cur_set;
648                 block->cur_act_index = 0;
649                 block->cur_set->next = set;
650                 block->cur_set = set;
651         }
652
653         actions = block->cur_set->ht_key.enc_actions;
654         oneact = actions + block->cur_act_index * MLXSW_AFA_ONE_ACTION_LEN;
655         block->cur_act_index += action_size;
656         mlxsw_afa_all_action_type_set(oneact, action_code);
657         return oneact + MLXSW_AFA_PAYLOAD_OFFSET;
658 }
659
660 /* VLAN Action
661  * -----------
662  * VLAN action is used for manipulating VLANs. It can be used to implement QinQ,
663  * VLAN translation, change of PCP bits of the VLAN tag, push, pop as swap VLANs
664  * and more.
665  */
666
667 #define MLXSW_AFA_VLAN_CODE 0x02
668 #define MLXSW_AFA_VLAN_SIZE 1
669
670 enum mlxsw_afa_vlan_vlan_tag_cmd {
671         MLXSW_AFA_VLAN_VLAN_TAG_CMD_NOP,
672         MLXSW_AFA_VLAN_VLAN_TAG_CMD_PUSH_TAG,
673         MLXSW_AFA_VLAN_VLAN_TAG_CMD_POP_TAG,
674 };
675
676 enum mlxsw_afa_vlan_cmd {
677         MLXSW_AFA_VLAN_CMD_NOP,
678         MLXSW_AFA_VLAN_CMD_SET_OUTER,
679         MLXSW_AFA_VLAN_CMD_SET_INNER,
680         MLXSW_AFA_VLAN_CMD_COPY_OUTER_TO_INNER,
681         MLXSW_AFA_VLAN_CMD_COPY_INNER_TO_OUTER,
682         MLXSW_AFA_VLAN_CMD_SWAP,
683 };
684
685 /* afa_vlan_vlan_tag_cmd
686  * Tag command: push, pop, nop VLAN header.
687  */
688 MLXSW_ITEM32(afa, vlan, vlan_tag_cmd, 0x00, 29, 3);
689
690 /* afa_vlan_vid_cmd */
691 MLXSW_ITEM32(afa, vlan, vid_cmd, 0x04, 29, 3);
692
693 /* afa_vlan_vid */
694 MLXSW_ITEM32(afa, vlan, vid, 0x04, 0, 12);
695
696 /* afa_vlan_ethertype_cmd */
697 MLXSW_ITEM32(afa, vlan, ethertype_cmd, 0x08, 29, 3);
698
699 /* afa_vlan_ethertype
700  * Index to EtherTypes in Switch VLAN EtherType Register (SVER).
701  */
702 MLXSW_ITEM32(afa, vlan, ethertype, 0x08, 24, 3);
703
704 /* afa_vlan_pcp_cmd */
705 MLXSW_ITEM32(afa, vlan, pcp_cmd, 0x08, 13, 3);
706
707 /* afa_vlan_pcp */
708 MLXSW_ITEM32(afa, vlan, pcp, 0x08, 8, 3);
709
710 static inline void
711 mlxsw_afa_vlan_pack(char *payload,
712                     enum mlxsw_afa_vlan_vlan_tag_cmd vlan_tag_cmd,
713                     enum mlxsw_afa_vlan_cmd vid_cmd, u16 vid,
714                     enum mlxsw_afa_vlan_cmd pcp_cmd, u8 pcp,
715                     enum mlxsw_afa_vlan_cmd ethertype_cmd, u8 ethertype)
716 {
717         mlxsw_afa_vlan_vlan_tag_cmd_set(payload, vlan_tag_cmd);
718         mlxsw_afa_vlan_vid_cmd_set(payload, vid_cmd);
719         mlxsw_afa_vlan_vid_set(payload, vid);
720         mlxsw_afa_vlan_pcp_cmd_set(payload, pcp_cmd);
721         mlxsw_afa_vlan_pcp_set(payload, pcp);
722         mlxsw_afa_vlan_ethertype_cmd_set(payload, ethertype_cmd);
723         mlxsw_afa_vlan_ethertype_set(payload, ethertype);
724 }
725
726 int mlxsw_afa_block_append_vlan_modify(struct mlxsw_afa_block *block,
727                                        u16 vid, u8 pcp, u8 et)
728 {
729         char *act = mlxsw_afa_block_append_action(block,
730                                                   MLXSW_AFA_VLAN_CODE,
731                                                   MLXSW_AFA_VLAN_SIZE);
732
733         if (IS_ERR(act))
734                 return PTR_ERR(act);
735         mlxsw_afa_vlan_pack(act, MLXSW_AFA_VLAN_VLAN_TAG_CMD_NOP,
736                             MLXSW_AFA_VLAN_CMD_SET_OUTER, vid,
737                             MLXSW_AFA_VLAN_CMD_SET_OUTER, pcp,
738                             MLXSW_AFA_VLAN_CMD_SET_OUTER, et);
739         return 0;
740 }
741 EXPORT_SYMBOL(mlxsw_afa_block_append_vlan_modify);
742
743 /* Trap / Discard Action
744  * ---------------------
745  * The Trap / Discard action enables trapping / mirroring packets to the CPU
746  * as well as discarding packets.
747  * The ACL Trap / Discard separates the forward/discard control from CPU
748  * trap control. In addition, the Trap / Discard action enables activating
749  * SPAN (port mirroring).
750  */
751
752 #define MLXSW_AFA_TRAPDISC_CODE 0x03
753 #define MLXSW_AFA_TRAPDISC_SIZE 1
754
755 enum mlxsw_afa_trapdisc_trap_action {
756         MLXSW_AFA_TRAPDISC_TRAP_ACTION_NOP = 0,
757         MLXSW_AFA_TRAPDISC_TRAP_ACTION_TRAP = 2,
758 };
759
760 /* afa_trapdisc_trap_action
761  * Trap Action.
762  */
763 MLXSW_ITEM32(afa, trapdisc, trap_action, 0x00, 24, 4);
764
765 enum mlxsw_afa_trapdisc_forward_action {
766         MLXSW_AFA_TRAPDISC_FORWARD_ACTION_FORWARD = 1,
767         MLXSW_AFA_TRAPDISC_FORWARD_ACTION_DISCARD = 3,
768 };
769
770 /* afa_trapdisc_forward_action
771  * Forward Action.
772  */
773 MLXSW_ITEM32(afa, trapdisc, forward_action, 0x00, 0, 4);
774
775 /* afa_trapdisc_trap_id
776  * Trap ID to configure.
777  */
778 MLXSW_ITEM32(afa, trapdisc, trap_id, 0x04, 0, 9);
779
780 /* afa_trapdisc_mirror_agent
781  * Mirror agent.
782  */
783 MLXSW_ITEM32(afa, trapdisc, mirror_agent, 0x08, 29, 3);
784
785 /* afa_trapdisc_mirror_enable
786  * Mirror enable.
787  */
788 MLXSW_ITEM32(afa, trapdisc, mirror_enable, 0x08, 24, 1);
789
790 static inline void
791 mlxsw_afa_trapdisc_pack(char *payload,
792                         enum mlxsw_afa_trapdisc_trap_action trap_action,
793                         enum mlxsw_afa_trapdisc_forward_action forward_action,
794                         u16 trap_id)
795 {
796         mlxsw_afa_trapdisc_trap_action_set(payload, trap_action);
797         mlxsw_afa_trapdisc_forward_action_set(payload, forward_action);
798         mlxsw_afa_trapdisc_trap_id_set(payload, trap_id);
799 }
800
801 static inline void
802 mlxsw_afa_trapdisc_mirror_pack(char *payload, bool mirror_enable,
803                                u8 mirror_agent)
804 {
805         mlxsw_afa_trapdisc_mirror_enable_set(payload, mirror_enable);
806         mlxsw_afa_trapdisc_mirror_agent_set(payload, mirror_agent);
807 }
808
809 int mlxsw_afa_block_append_drop(struct mlxsw_afa_block *block)
810 {
811         char *act = mlxsw_afa_block_append_action(block,
812                                                   MLXSW_AFA_TRAPDISC_CODE,
813                                                   MLXSW_AFA_TRAPDISC_SIZE);
814
815         if (IS_ERR(act))
816                 return PTR_ERR(act);
817         mlxsw_afa_trapdisc_pack(act, MLXSW_AFA_TRAPDISC_TRAP_ACTION_NOP,
818                                 MLXSW_AFA_TRAPDISC_FORWARD_ACTION_DISCARD, 0);
819         return 0;
820 }
821 EXPORT_SYMBOL(mlxsw_afa_block_append_drop);
822
823 int mlxsw_afa_block_append_trap(struct mlxsw_afa_block *block, u16 trap_id)
824 {
825         char *act = mlxsw_afa_block_append_action(block,
826                                                   MLXSW_AFA_TRAPDISC_CODE,
827                                                   MLXSW_AFA_TRAPDISC_SIZE);
828
829         if (IS_ERR(act))
830                 return PTR_ERR(act);
831         mlxsw_afa_trapdisc_pack(act, MLXSW_AFA_TRAPDISC_TRAP_ACTION_TRAP,
832                                 MLXSW_AFA_TRAPDISC_FORWARD_ACTION_DISCARD,
833                                 trap_id);
834         return 0;
835 }
836 EXPORT_SYMBOL(mlxsw_afa_block_append_trap);
837
838 int mlxsw_afa_block_append_trap_and_forward(struct mlxsw_afa_block *block,
839                                             u16 trap_id)
840 {
841         char *act = mlxsw_afa_block_append_action(block,
842                                                   MLXSW_AFA_TRAPDISC_CODE,
843                                                   MLXSW_AFA_TRAPDISC_SIZE);
844
845         if (IS_ERR(act))
846                 return PTR_ERR(act);
847         mlxsw_afa_trapdisc_pack(act, MLXSW_AFA_TRAPDISC_TRAP_ACTION_TRAP,
848                                 MLXSW_AFA_TRAPDISC_FORWARD_ACTION_FORWARD,
849                                 trap_id);
850         return 0;
851 }
852 EXPORT_SYMBOL(mlxsw_afa_block_append_trap_and_forward);
853
854 struct mlxsw_afa_mirror {
855         struct mlxsw_afa_resource resource;
856         int span_id;
857         u8 local_in_port;
858         bool ingress;
859 };
860
861 static void
862 mlxsw_afa_mirror_destroy(struct mlxsw_afa_block *block,
863                          struct mlxsw_afa_mirror *mirror)
864 {
865         mlxsw_afa_resource_del(&mirror->resource);
866         block->afa->ops->mirror_del(block->afa->ops_priv,
867                                     mirror->local_in_port,
868                                     mirror->span_id,
869                                     mirror->ingress);
870         kfree(mirror);
871 }
872
873 static void
874 mlxsw_afa_mirror_destructor(struct mlxsw_afa_block *block,
875                             struct mlxsw_afa_resource *resource)
876 {
877         struct mlxsw_afa_mirror *mirror;
878
879         mirror = container_of(resource, struct mlxsw_afa_mirror, resource);
880         mlxsw_afa_mirror_destroy(block, mirror);
881 }
882
883 static struct mlxsw_afa_mirror *
884 mlxsw_afa_mirror_create(struct mlxsw_afa_block *block, u8 local_in_port,
885                         const struct net_device *out_dev, bool ingress)
886 {
887         struct mlxsw_afa_mirror *mirror;
888         int err;
889
890         mirror = kzalloc(sizeof(*mirror), GFP_KERNEL);
891         if (!mirror)
892                 return ERR_PTR(-ENOMEM);
893
894         err = block->afa->ops->mirror_add(block->afa->ops_priv,
895                                           local_in_port, out_dev,
896                                           ingress, &mirror->span_id);
897         if (err)
898                 goto err_mirror_add;
899
900         mirror->ingress = ingress;
901         mirror->local_in_port = local_in_port;
902         mirror->resource.destructor = mlxsw_afa_mirror_destructor;
903         mlxsw_afa_resource_add(block, &mirror->resource);
904         return mirror;
905
906 err_mirror_add:
907         kfree(mirror);
908         return ERR_PTR(err);
909 }
910
911 static int
912 mlxsw_afa_block_append_allocated_mirror(struct mlxsw_afa_block *block,
913                                         u8 mirror_agent)
914 {
915         char *act = mlxsw_afa_block_append_action(block,
916                                                   MLXSW_AFA_TRAPDISC_CODE,
917                                                   MLXSW_AFA_TRAPDISC_SIZE);
918         if (IS_ERR(act))
919                 return PTR_ERR(act);
920         mlxsw_afa_trapdisc_pack(act, MLXSW_AFA_TRAPDISC_TRAP_ACTION_NOP,
921                                 MLXSW_AFA_TRAPDISC_FORWARD_ACTION_FORWARD, 0);
922         mlxsw_afa_trapdisc_mirror_pack(act, true, mirror_agent);
923         return 0;
924 }
925
926 int
927 mlxsw_afa_block_append_mirror(struct mlxsw_afa_block *block, u8 local_in_port,
928                               const struct net_device *out_dev, bool ingress)
929 {
930         struct mlxsw_afa_mirror *mirror;
931         int err;
932
933         mirror = mlxsw_afa_mirror_create(block, local_in_port, out_dev,
934                                          ingress);
935         if (IS_ERR(mirror))
936                 return PTR_ERR(mirror);
937
938         err = mlxsw_afa_block_append_allocated_mirror(block, mirror->span_id);
939         if (err)
940                 goto err_append_allocated_mirror;
941
942         return 0;
943
944 err_append_allocated_mirror:
945         mlxsw_afa_mirror_destroy(block, mirror);
946         return err;
947 }
948 EXPORT_SYMBOL(mlxsw_afa_block_append_mirror);
949
950 /* Forwarding Action
951  * -----------------
952  * Forwarding Action can be used to implement Policy Based Switching (PBS)
953  * as well as OpenFlow related "Output" action.
954  */
955
956 #define MLXSW_AFA_FORWARD_CODE 0x07
957 #define MLXSW_AFA_FORWARD_SIZE 1
958
959 enum mlxsw_afa_forward_type {
960         /* PBS, Policy Based Switching */
961         MLXSW_AFA_FORWARD_TYPE_PBS,
962         /* Output, OpenFlow output type */
963         MLXSW_AFA_FORWARD_TYPE_OUTPUT,
964 };
965
966 /* afa_forward_type */
967 MLXSW_ITEM32(afa, forward, type, 0x00, 24, 2);
968
969 /* afa_forward_pbs_ptr
970  * A pointer to the PBS entry configured by PPBS register.
971  * Reserved when in_port is set.
972  */
973 MLXSW_ITEM32(afa, forward, pbs_ptr, 0x08, 0, 24);
974
975 /* afa_forward_in_port
976  * Packet is forwarded back to the ingress port.
977  */
978 MLXSW_ITEM32(afa, forward, in_port, 0x0C, 0, 1);
979
980 static inline void
981 mlxsw_afa_forward_pack(char *payload, enum mlxsw_afa_forward_type type,
982                        u32 pbs_ptr, bool in_port)
983 {
984         mlxsw_afa_forward_type_set(payload, type);
985         mlxsw_afa_forward_pbs_ptr_set(payload, pbs_ptr);
986         mlxsw_afa_forward_in_port_set(payload, in_port);
987 }
988
989 int mlxsw_afa_block_append_fwd(struct mlxsw_afa_block *block,
990                                u8 local_port, bool in_port)
991 {
992         struct mlxsw_afa_fwd_entry_ref *fwd_entry_ref;
993         u32 kvdl_index;
994         char *act;
995         int err;
996
997         if (in_port)
998                 return -EOPNOTSUPP;
999         fwd_entry_ref = mlxsw_afa_fwd_entry_ref_create(block, local_port);
1000         if (IS_ERR(fwd_entry_ref))
1001                 return PTR_ERR(fwd_entry_ref);
1002         kvdl_index = fwd_entry_ref->fwd_entry->kvdl_index;
1003
1004         act = mlxsw_afa_block_append_action(block, MLXSW_AFA_FORWARD_CODE,
1005                                             MLXSW_AFA_FORWARD_SIZE);
1006         if (IS_ERR(act)) {
1007                 err = PTR_ERR(act);
1008                 goto err_append_action;
1009         }
1010         mlxsw_afa_forward_pack(act, MLXSW_AFA_FORWARD_TYPE_PBS,
1011                                kvdl_index, in_port);
1012         return 0;
1013
1014 err_append_action:
1015         mlxsw_afa_fwd_entry_ref_destroy(block, fwd_entry_ref);
1016         return err;
1017 }
1018 EXPORT_SYMBOL(mlxsw_afa_block_append_fwd);
1019
1020 /* Policing and Counting Action
1021  * ----------------------------
1022  * Policing and Counting action is used for binding policer and counter
1023  * to ACL rules.
1024  */
1025
1026 #define MLXSW_AFA_POLCNT_CODE 0x08
1027 #define MLXSW_AFA_POLCNT_SIZE 1
1028
1029 enum mlxsw_afa_polcnt_counter_set_type {
1030         /* No count */
1031         MLXSW_AFA_POLCNT_COUNTER_SET_TYPE_NO_COUNT = 0x00,
1032         /* Count packets and bytes */
1033         MLXSW_AFA_POLCNT_COUNTER_SET_TYPE_PACKETS_BYTES = 0x03,
1034         /* Count only packets */
1035         MLXSW_AFA_POLCNT_COUNTER_SET_TYPE_PACKETS = 0x05,
1036 };
1037
1038 /* afa_polcnt_counter_set_type
1039  * Counter set type for flow counters.
1040  */
1041 MLXSW_ITEM32(afa, polcnt, counter_set_type, 0x04, 24, 8);
1042
1043 /* afa_polcnt_counter_index
1044  * Counter index for flow counters.
1045  */
1046 MLXSW_ITEM32(afa, polcnt, counter_index, 0x04, 0, 24);
1047
1048 static inline void
1049 mlxsw_afa_polcnt_pack(char *payload,
1050                       enum mlxsw_afa_polcnt_counter_set_type set_type,
1051                       u32 counter_index)
1052 {
1053         mlxsw_afa_polcnt_counter_set_type_set(payload, set_type);
1054         mlxsw_afa_polcnt_counter_index_set(payload, counter_index);
1055 }
1056
1057 int mlxsw_afa_block_append_allocated_counter(struct mlxsw_afa_block *block,
1058                                              u32 counter_index)
1059 {
1060         char *act = mlxsw_afa_block_append_action(block, MLXSW_AFA_POLCNT_CODE,
1061                                                   MLXSW_AFA_POLCNT_SIZE);
1062         if (IS_ERR(act))
1063                 return PTR_ERR(act);
1064         mlxsw_afa_polcnt_pack(act, MLXSW_AFA_POLCNT_COUNTER_SET_TYPE_PACKETS_BYTES,
1065                               counter_index);
1066         return 0;
1067 }
1068 EXPORT_SYMBOL(mlxsw_afa_block_append_allocated_counter);
1069
1070 int mlxsw_afa_block_append_counter(struct mlxsw_afa_block *block,
1071                                    u32 *p_counter_index)
1072 {
1073         struct mlxsw_afa_counter *counter;
1074         u32 counter_index;
1075         int err;
1076
1077         counter = mlxsw_afa_counter_create(block);
1078         if (IS_ERR(counter))
1079                 return PTR_ERR(counter);
1080         counter_index = counter->counter_index;
1081
1082         err = mlxsw_afa_block_append_allocated_counter(block, counter_index);
1083         if (err)
1084                 goto err_append_allocated_counter;
1085
1086         if (p_counter_index)
1087                 *p_counter_index = counter_index;
1088         return 0;
1089
1090 err_append_allocated_counter:
1091         mlxsw_afa_counter_destroy(block, counter);
1092         return err;
1093 }
1094 EXPORT_SYMBOL(mlxsw_afa_block_append_counter);
1095
1096 /* Virtual Router and Forwarding Domain Action
1097  * -------------------------------------------
1098  * Virtual Switch action is used for manipulate the Virtual Router (VR),
1099  * MPLS label space and the Forwarding Identifier (FID).
1100  */
1101
1102 #define MLXSW_AFA_VIRFWD_CODE 0x0E
1103 #define MLXSW_AFA_VIRFWD_SIZE 1
1104
1105 enum mlxsw_afa_virfwd_fid_cmd {
1106         /* Do nothing */
1107         MLXSW_AFA_VIRFWD_FID_CMD_NOOP,
1108         /* Set the Forwarding Identifier (FID) to fid */
1109         MLXSW_AFA_VIRFWD_FID_CMD_SET,
1110 };
1111
1112 /* afa_virfwd_fid_cmd */
1113 MLXSW_ITEM32(afa, virfwd, fid_cmd, 0x08, 29, 3);
1114
1115 /* afa_virfwd_fid
1116  * The FID value.
1117  */
1118 MLXSW_ITEM32(afa, virfwd, fid, 0x08, 0, 16);
1119
1120 static inline void mlxsw_afa_virfwd_pack(char *payload,
1121                                          enum mlxsw_afa_virfwd_fid_cmd fid_cmd,
1122                                          u16 fid)
1123 {
1124         mlxsw_afa_virfwd_fid_cmd_set(payload, fid_cmd);
1125         mlxsw_afa_virfwd_fid_set(payload, fid);
1126 }
1127
1128 int mlxsw_afa_block_append_fid_set(struct mlxsw_afa_block *block, u16 fid)
1129 {
1130         char *act = mlxsw_afa_block_append_action(block,
1131                                                   MLXSW_AFA_VIRFWD_CODE,
1132                                                   MLXSW_AFA_VIRFWD_SIZE);
1133         if (IS_ERR(act))
1134                 return PTR_ERR(act);
1135         mlxsw_afa_virfwd_pack(act, MLXSW_AFA_VIRFWD_FID_CMD_SET, fid);
1136         return 0;
1137 }
1138 EXPORT_SYMBOL(mlxsw_afa_block_append_fid_set);
1139
1140 /* MC Routing Action
1141  * -----------------
1142  * The Multicast router action. Can be used by RMFT_V2 - Router Multicast
1143  * Forwarding Table Version 2 Register.
1144  */
1145
1146 #define MLXSW_AFA_MCROUTER_CODE 0x10
1147 #define MLXSW_AFA_MCROUTER_SIZE 2
1148
1149 enum mlxsw_afa_mcrouter_rpf_action {
1150         MLXSW_AFA_MCROUTER_RPF_ACTION_NOP,
1151         MLXSW_AFA_MCROUTER_RPF_ACTION_TRAP,
1152         MLXSW_AFA_MCROUTER_RPF_ACTION_DISCARD_ERROR,
1153 };
1154
1155 /* afa_mcrouter_rpf_action */
1156 MLXSW_ITEM32(afa, mcrouter, rpf_action, 0x00, 28, 3);
1157
1158 /* afa_mcrouter_expected_irif */
1159 MLXSW_ITEM32(afa, mcrouter, expected_irif, 0x00, 0, 16);
1160
1161 /* afa_mcrouter_min_mtu */
1162 MLXSW_ITEM32(afa, mcrouter, min_mtu, 0x08, 0, 16);
1163
1164 enum mlxsw_afa_mrouter_vrmid {
1165         MLXSW_AFA_MCROUTER_VRMID_INVALID,
1166         MLXSW_AFA_MCROUTER_VRMID_VALID
1167 };
1168
1169 /* afa_mcrouter_vrmid
1170  * Valid RMID: rigr_rmid_index is used as RMID
1171  */
1172 MLXSW_ITEM32(afa, mcrouter, vrmid, 0x0C, 31, 1);
1173
1174 /* afa_mcrouter_rigr_rmid_index
1175  * When the vrmid field is set to invalid, the field is used as pointer to
1176  * Router Interface Group (RIGR) Table in the KVD linear.
1177  * When the vrmid is set to valid, the field is used as RMID index, ranged
1178  * from 0 to max_mid - 1. The index is to the Port Group Table.
1179  */
1180 MLXSW_ITEM32(afa, mcrouter, rigr_rmid_index, 0x0C, 0, 24);
1181
1182 static inline void
1183 mlxsw_afa_mcrouter_pack(char *payload,
1184                         enum mlxsw_afa_mcrouter_rpf_action rpf_action,
1185                         u16 expected_irif, u16 min_mtu,
1186                         enum mlxsw_afa_mrouter_vrmid vrmid, u32 rigr_rmid_index)
1187
1188 {
1189         mlxsw_afa_mcrouter_rpf_action_set(payload, rpf_action);
1190         mlxsw_afa_mcrouter_expected_irif_set(payload, expected_irif);
1191         mlxsw_afa_mcrouter_min_mtu_set(payload, min_mtu);
1192         mlxsw_afa_mcrouter_vrmid_set(payload, vrmid);
1193         mlxsw_afa_mcrouter_rigr_rmid_index_set(payload, rigr_rmid_index);
1194 }
1195
1196 int mlxsw_afa_block_append_mcrouter(struct mlxsw_afa_block *block,
1197                                     u16 expected_irif, u16 min_mtu,
1198                                     bool rmid_valid, u32 kvdl_index)
1199 {
1200         char *act = mlxsw_afa_block_append_action(block,
1201                                                   MLXSW_AFA_MCROUTER_CODE,
1202                                                   MLXSW_AFA_MCROUTER_SIZE);
1203         if (IS_ERR(act))
1204                 return PTR_ERR(act);
1205         mlxsw_afa_mcrouter_pack(act, MLXSW_AFA_MCROUTER_RPF_ACTION_TRAP,
1206                                 expected_irif, min_mtu, rmid_valid, kvdl_index);
1207         return 0;
1208 }
1209 EXPORT_SYMBOL(mlxsw_afa_block_append_mcrouter);