1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /* Copyright (c) 2017-2018 Mellanox Technologies. All rights reserved */
4 #include <linux/kernel.h>
5 #include <linux/types.h>
6 #include <linux/slab.h>
7 #include <linux/errno.h>
8 #include <linux/rhashtable.h>
9 #include <linux/list.h>
10 #include <linux/idr.h>
11 #include <linux/refcount.h>
12 #include <net/flow_offload.h>
16 #include "core_acl_flex_actions.h"
18 enum mlxsw_afa_set_type {
19 MLXSW_AFA_SET_TYPE_NEXT,
20 MLXSW_AFA_SET_TYPE_GOTO,
24 * Type of the record at the end of the action set.
26 MLXSW_ITEM32(afa, set, type, 0xA0, 28, 4);
28 /* afa_set_next_action_set_ptr
29 * A pointer to the next action set in the KVD Centralized database.
31 MLXSW_ITEM32(afa, set, next_action_set_ptr, 0xA4, 0, 24);
34 * group - When set, the binding is of an ACL group. When cleared,
35 * the binding is of an ACL.
36 * Must be set to 1 for Spectrum.
38 MLXSW_ITEM32(afa, set, goto_g, 0xA4, 29, 1);
40 enum mlxsw_afa_set_goto_binding_cmd {
41 /* continue go the next binding point */
42 MLXSW_AFA_SET_GOTO_BINDING_CMD_NONE,
43 /* jump to the next binding point no return */
44 MLXSW_AFA_SET_GOTO_BINDING_CMD_JUMP,
45 /* terminate the acl binding */
46 MLXSW_AFA_SET_GOTO_BINDING_CMD_TERM = 4,
49 /* afa_set_goto_binding_cmd */
50 MLXSW_ITEM32(afa, set, goto_binding_cmd, 0xA4, 24, 3);
52 /* afa_set_goto_next_binding
53 * ACL/ACL group identifier. If the g bit is set, this field should hold
54 * the acl_group_id, else it should hold the acl_id.
56 MLXSW_ITEM32(afa, set, goto_next_binding, 0xA4, 0, 16);
58 /* afa_all_action_type
61 MLXSW_ITEM32(afa, all, action_type, 0x00, 24, 6);
64 unsigned int max_acts_per_set;
65 const struct mlxsw_afa_ops *ops;
67 struct rhashtable set_ht;
68 struct rhashtable fwd_entry_ht;
69 struct rhashtable cookie_ht;
70 struct idr cookie_idr;
73 #define MLXSW_AFA_SET_LEN 0xA8
75 struct mlxsw_afa_set_ht_key {
76 char enc_actions[MLXSW_AFA_SET_LEN]; /* Encoded set */
80 /* Set structure holds one action set record. It contains up to three
81 * actions (depends on size of particular actions). The set is either
82 * put directly to a rule, or it is stored in KVD linear area.
83 * To prevent duplicate entries in KVD linear area, a hashtable is
84 * used to track sets that were previously inserted and may be shared.
87 struct mlxsw_afa_set {
88 struct rhash_head ht_node;
89 struct mlxsw_afa_set_ht_key ht_key;
91 bool shared; /* Inserted in hashtable (doesn't mean that
92 * kvdl_index is valid).
94 unsigned int ref_count;
95 struct mlxsw_afa_set *next; /* Pointer to the next set. */
96 struct mlxsw_afa_set *prev; /* Pointer to the previous set,
97 * note that set may have multiple
98 * sets from multiple blocks
99 * pointing at it. This is only
100 * usable until commit.
104 static const struct rhashtable_params mlxsw_afa_set_ht_params = {
105 .key_len = sizeof(struct mlxsw_afa_set_ht_key),
106 .key_offset = offsetof(struct mlxsw_afa_set, ht_key),
107 .head_offset = offsetof(struct mlxsw_afa_set, ht_node),
108 .automatic_shrinking = true,
111 struct mlxsw_afa_fwd_entry_ht_key {
115 struct mlxsw_afa_fwd_entry {
116 struct rhash_head ht_node;
117 struct mlxsw_afa_fwd_entry_ht_key ht_key;
119 unsigned int ref_count;
122 static const struct rhashtable_params mlxsw_afa_fwd_entry_ht_params = {
123 .key_len = sizeof(struct mlxsw_afa_fwd_entry_ht_key),
124 .key_offset = offsetof(struct mlxsw_afa_fwd_entry, ht_key),
125 .head_offset = offsetof(struct mlxsw_afa_fwd_entry, ht_node),
126 .automatic_shrinking = true,
129 struct mlxsw_afa_cookie {
130 struct rhash_head ht_node;
131 refcount_t ref_count;
134 struct flow_action_cookie fa_cookie;
137 static u32 mlxsw_afa_cookie_hash(const struct flow_action_cookie *fa_cookie,
140 return jhash2((u32 *) fa_cookie->cookie,
141 fa_cookie->cookie_len / sizeof(u32), seed);
144 static u32 mlxsw_afa_cookie_key_hashfn(const void *data, u32 len, u32 seed)
146 const struct flow_action_cookie *fa_cookie = data;
148 return mlxsw_afa_cookie_hash(fa_cookie, seed);
151 static u32 mlxsw_afa_cookie_obj_hashfn(const void *data, u32 len, u32 seed)
153 const struct mlxsw_afa_cookie *cookie = data;
155 return mlxsw_afa_cookie_hash(&cookie->fa_cookie, seed);
158 static int mlxsw_afa_cookie_obj_cmpfn(struct rhashtable_compare_arg *arg,
161 const struct flow_action_cookie *fa_cookie = arg->key;
162 const struct mlxsw_afa_cookie *cookie = obj;
164 if (cookie->fa_cookie.cookie_len == fa_cookie->cookie_len)
165 return memcmp(cookie->fa_cookie.cookie, fa_cookie->cookie,
166 fa_cookie->cookie_len);
170 static const struct rhashtable_params mlxsw_afa_cookie_ht_params = {
171 .head_offset = offsetof(struct mlxsw_afa_cookie, ht_node),
172 .hashfn = mlxsw_afa_cookie_key_hashfn,
173 .obj_hashfn = mlxsw_afa_cookie_obj_hashfn,
174 .obj_cmpfn = mlxsw_afa_cookie_obj_cmpfn,
175 .automatic_shrinking = true,
178 struct mlxsw_afa *mlxsw_afa_create(unsigned int max_acts_per_set,
179 const struct mlxsw_afa_ops *ops,
182 struct mlxsw_afa *mlxsw_afa;
185 mlxsw_afa = kzalloc(sizeof(*mlxsw_afa), GFP_KERNEL);
187 return ERR_PTR(-ENOMEM);
188 err = rhashtable_init(&mlxsw_afa->set_ht, &mlxsw_afa_set_ht_params);
190 goto err_set_rhashtable_init;
191 err = rhashtable_init(&mlxsw_afa->fwd_entry_ht,
192 &mlxsw_afa_fwd_entry_ht_params);
194 goto err_fwd_entry_rhashtable_init;
195 err = rhashtable_init(&mlxsw_afa->cookie_ht,
196 &mlxsw_afa_cookie_ht_params);
198 goto err_cookie_rhashtable_init;
199 idr_init(&mlxsw_afa->cookie_idr);
200 mlxsw_afa->max_acts_per_set = max_acts_per_set;
201 mlxsw_afa->ops = ops;
202 mlxsw_afa->ops_priv = ops_priv;
205 err_cookie_rhashtable_init:
206 rhashtable_destroy(&mlxsw_afa->fwd_entry_ht);
207 err_fwd_entry_rhashtable_init:
208 rhashtable_destroy(&mlxsw_afa->set_ht);
209 err_set_rhashtable_init:
213 EXPORT_SYMBOL(mlxsw_afa_create);
215 void mlxsw_afa_destroy(struct mlxsw_afa *mlxsw_afa)
217 WARN_ON(!idr_is_empty(&mlxsw_afa->cookie_idr));
218 idr_destroy(&mlxsw_afa->cookie_idr);
219 rhashtable_destroy(&mlxsw_afa->cookie_ht);
220 rhashtable_destroy(&mlxsw_afa->fwd_entry_ht);
221 rhashtable_destroy(&mlxsw_afa->set_ht);
224 EXPORT_SYMBOL(mlxsw_afa_destroy);
226 static void mlxsw_afa_set_goto_set(struct mlxsw_afa_set *set,
227 enum mlxsw_afa_set_goto_binding_cmd cmd,
230 char *actions = set->ht_key.enc_actions;
232 mlxsw_afa_set_type_set(actions, MLXSW_AFA_SET_TYPE_GOTO);
233 mlxsw_afa_set_goto_g_set(actions, true);
234 mlxsw_afa_set_goto_binding_cmd_set(actions, cmd);
235 mlxsw_afa_set_goto_next_binding_set(actions, group_id);
238 static void mlxsw_afa_set_next_set(struct mlxsw_afa_set *set,
239 u32 next_set_kvdl_index)
241 char *actions = set->ht_key.enc_actions;
243 mlxsw_afa_set_type_set(actions, MLXSW_AFA_SET_TYPE_NEXT);
244 mlxsw_afa_set_next_action_set_ptr_set(actions, next_set_kvdl_index);
247 static struct mlxsw_afa_set *mlxsw_afa_set_create(bool is_first)
249 struct mlxsw_afa_set *set;
251 set = kzalloc(sizeof(*set), GFP_KERNEL);
254 /* Need to initialize the set to pass by default */
255 mlxsw_afa_set_goto_set(set, MLXSW_AFA_SET_GOTO_BINDING_CMD_TERM, 0);
256 set->ht_key.is_first = is_first;
261 static void mlxsw_afa_set_destroy(struct mlxsw_afa_set *set)
266 static int mlxsw_afa_set_share(struct mlxsw_afa *mlxsw_afa,
267 struct mlxsw_afa_set *set)
271 err = rhashtable_insert_fast(&mlxsw_afa->set_ht, &set->ht_node,
272 mlxsw_afa_set_ht_params);
275 err = mlxsw_afa->ops->kvdl_set_add(mlxsw_afa->ops_priv,
277 set->ht_key.enc_actions,
278 set->ht_key.is_first);
280 goto err_kvdl_set_add;
286 rhashtable_remove_fast(&mlxsw_afa->set_ht, &set->ht_node,
287 mlxsw_afa_set_ht_params);
291 static void mlxsw_afa_set_unshare(struct mlxsw_afa *mlxsw_afa,
292 struct mlxsw_afa_set *set)
294 mlxsw_afa->ops->kvdl_set_del(mlxsw_afa->ops_priv,
296 set->ht_key.is_first);
297 rhashtable_remove_fast(&mlxsw_afa->set_ht, &set->ht_node,
298 mlxsw_afa_set_ht_params);
302 static void mlxsw_afa_set_put(struct mlxsw_afa *mlxsw_afa,
303 struct mlxsw_afa_set *set)
305 if (--set->ref_count)
308 mlxsw_afa_set_unshare(mlxsw_afa, set);
309 mlxsw_afa_set_destroy(set);
312 static struct mlxsw_afa_set *mlxsw_afa_set_get(struct mlxsw_afa *mlxsw_afa,
313 struct mlxsw_afa_set *orig_set)
315 struct mlxsw_afa_set *set;
318 /* There is a hashtable of sets maintained. If a set with the exact
319 * same encoding exists, we reuse it. Otherwise, the current set
320 * is shared by making it available to others using the hash table.
322 set = rhashtable_lookup_fast(&mlxsw_afa->set_ht, &orig_set->ht_key,
323 mlxsw_afa_set_ht_params);
326 mlxsw_afa_set_put(mlxsw_afa, orig_set);
329 err = mlxsw_afa_set_share(mlxsw_afa, set);
336 /* Block structure holds a list of action sets. One action block
337 * represents one chain of actions executed upon match of a rule.
340 struct mlxsw_afa_block {
341 struct mlxsw_afa *afa;
343 struct mlxsw_afa_set *first_set;
344 struct mlxsw_afa_set *cur_set;
345 unsigned int cur_act_index; /* In current set. */
346 struct list_head resource_list; /* List of resources held by actions
351 struct mlxsw_afa_resource {
352 struct list_head list;
353 void (*destructor)(struct mlxsw_afa_block *block,
354 struct mlxsw_afa_resource *resource);
357 static void mlxsw_afa_resource_add(struct mlxsw_afa_block *block,
358 struct mlxsw_afa_resource *resource)
360 list_add(&resource->list, &block->resource_list);
363 static void mlxsw_afa_resource_del(struct mlxsw_afa_resource *resource)
365 list_del(&resource->list);
368 static void mlxsw_afa_resources_destroy(struct mlxsw_afa_block *block)
370 struct mlxsw_afa_resource *resource, *tmp;
372 list_for_each_entry_safe(resource, tmp, &block->resource_list, list) {
373 resource->destructor(block, resource);
377 struct mlxsw_afa_block *mlxsw_afa_block_create(struct mlxsw_afa *mlxsw_afa)
379 struct mlxsw_afa_block *block;
381 block = kzalloc(sizeof(*block), GFP_KERNEL);
384 INIT_LIST_HEAD(&block->resource_list);
385 block->afa = mlxsw_afa;
387 /* At least one action set is always present, so just create it here */
388 block->first_set = mlxsw_afa_set_create(true);
389 if (!block->first_set)
390 goto err_first_set_create;
392 /* In case user instructs to have dummy first set, we leave it
393 * empty here and create another, real, set right away.
395 if (mlxsw_afa->ops->dummy_first_set) {
396 block->cur_set = mlxsw_afa_set_create(false);
398 goto err_second_set_create;
399 block->cur_set->prev = block->first_set;
400 block->first_set->next = block->cur_set;
402 block->cur_set = block->first_set;
407 err_second_set_create:
408 mlxsw_afa_set_destroy(block->first_set);
409 err_first_set_create:
413 EXPORT_SYMBOL(mlxsw_afa_block_create);
415 void mlxsw_afa_block_destroy(struct mlxsw_afa_block *block)
417 struct mlxsw_afa_set *set = block->first_set;
418 struct mlxsw_afa_set *next_set;
421 next_set = set->next;
422 mlxsw_afa_set_put(block->afa, set);
425 mlxsw_afa_resources_destroy(block);
428 EXPORT_SYMBOL(mlxsw_afa_block_destroy);
430 int mlxsw_afa_block_commit(struct mlxsw_afa_block *block)
432 struct mlxsw_afa_set *set = block->cur_set;
433 struct mlxsw_afa_set *prev_set;
435 block->cur_set = NULL;
436 block->finished = true;
438 /* Go over all linked sets starting from last
439 * and try to find existing set in the hash table.
440 * In case it is not there, assign a KVD linear index
444 prev_set = set->prev;
445 set = mlxsw_afa_set_get(block->afa, set);
447 /* No rollback is needed since the chain is
448 * in consistent state and mlxsw_afa_block_destroy
449 * will take care of putting it away.
453 prev_set->next = set;
454 mlxsw_afa_set_next_set(prev_set, set->kvdl_index);
459 block->first_set = set;
462 EXPORT_SYMBOL(mlxsw_afa_block_commit);
464 char *mlxsw_afa_block_first_set(struct mlxsw_afa_block *block)
466 return block->first_set->ht_key.enc_actions;
468 EXPORT_SYMBOL(mlxsw_afa_block_first_set);
470 char *mlxsw_afa_block_cur_set(struct mlxsw_afa_block *block)
472 return block->cur_set->ht_key.enc_actions;
474 EXPORT_SYMBOL(mlxsw_afa_block_cur_set);
476 u32 mlxsw_afa_block_first_kvdl_index(struct mlxsw_afa_block *block)
478 /* First set is never in KVD linear. So the first set
479 * with valid KVD linear index is always the second one.
481 if (WARN_ON(!block->first_set->next))
483 return block->first_set->next->kvdl_index;
485 EXPORT_SYMBOL(mlxsw_afa_block_first_kvdl_index);
487 int mlxsw_afa_block_activity_get(struct mlxsw_afa_block *block, bool *activity)
489 u32 kvdl_index = mlxsw_afa_block_first_kvdl_index(block);
491 return block->afa->ops->kvdl_set_activity_get(block->afa->ops_priv,
492 kvdl_index, activity);
494 EXPORT_SYMBOL(mlxsw_afa_block_activity_get);
496 int mlxsw_afa_block_continue(struct mlxsw_afa_block *block)
500 mlxsw_afa_set_goto_set(block->cur_set,
501 MLXSW_AFA_SET_GOTO_BINDING_CMD_NONE, 0);
502 block->finished = true;
505 EXPORT_SYMBOL(mlxsw_afa_block_continue);
507 int mlxsw_afa_block_jump(struct mlxsw_afa_block *block, u16 group_id)
511 mlxsw_afa_set_goto_set(block->cur_set,
512 MLXSW_AFA_SET_GOTO_BINDING_CMD_JUMP, group_id);
513 block->finished = true;
516 EXPORT_SYMBOL(mlxsw_afa_block_jump);
518 int mlxsw_afa_block_terminate(struct mlxsw_afa_block *block)
522 mlxsw_afa_set_goto_set(block->cur_set,
523 MLXSW_AFA_SET_GOTO_BINDING_CMD_TERM, 0);
524 block->finished = true;
527 EXPORT_SYMBOL(mlxsw_afa_block_terminate);
529 static struct mlxsw_afa_fwd_entry *
530 mlxsw_afa_fwd_entry_create(struct mlxsw_afa *mlxsw_afa, u8 local_port)
532 struct mlxsw_afa_fwd_entry *fwd_entry;
535 fwd_entry = kzalloc(sizeof(*fwd_entry), GFP_KERNEL);
537 return ERR_PTR(-ENOMEM);
538 fwd_entry->ht_key.local_port = local_port;
539 fwd_entry->ref_count = 1;
541 err = rhashtable_insert_fast(&mlxsw_afa->fwd_entry_ht,
543 mlxsw_afa_fwd_entry_ht_params);
545 goto err_rhashtable_insert;
547 err = mlxsw_afa->ops->kvdl_fwd_entry_add(mlxsw_afa->ops_priv,
548 &fwd_entry->kvdl_index,
551 goto err_kvdl_fwd_entry_add;
554 err_kvdl_fwd_entry_add:
555 rhashtable_remove_fast(&mlxsw_afa->fwd_entry_ht, &fwd_entry->ht_node,
556 mlxsw_afa_fwd_entry_ht_params);
557 err_rhashtable_insert:
562 static void mlxsw_afa_fwd_entry_destroy(struct mlxsw_afa *mlxsw_afa,
563 struct mlxsw_afa_fwd_entry *fwd_entry)
565 mlxsw_afa->ops->kvdl_fwd_entry_del(mlxsw_afa->ops_priv,
566 fwd_entry->kvdl_index);
567 rhashtable_remove_fast(&mlxsw_afa->fwd_entry_ht, &fwd_entry->ht_node,
568 mlxsw_afa_fwd_entry_ht_params);
572 static struct mlxsw_afa_fwd_entry *
573 mlxsw_afa_fwd_entry_get(struct mlxsw_afa *mlxsw_afa, u8 local_port)
575 struct mlxsw_afa_fwd_entry_ht_key ht_key = {0};
576 struct mlxsw_afa_fwd_entry *fwd_entry;
578 ht_key.local_port = local_port;
579 fwd_entry = rhashtable_lookup_fast(&mlxsw_afa->fwd_entry_ht, &ht_key,
580 mlxsw_afa_fwd_entry_ht_params);
582 fwd_entry->ref_count++;
585 return mlxsw_afa_fwd_entry_create(mlxsw_afa, local_port);
588 static void mlxsw_afa_fwd_entry_put(struct mlxsw_afa *mlxsw_afa,
589 struct mlxsw_afa_fwd_entry *fwd_entry)
591 if (--fwd_entry->ref_count)
593 mlxsw_afa_fwd_entry_destroy(mlxsw_afa, fwd_entry);
596 struct mlxsw_afa_fwd_entry_ref {
597 struct mlxsw_afa_resource resource;
598 struct mlxsw_afa_fwd_entry *fwd_entry;
602 mlxsw_afa_fwd_entry_ref_destroy(struct mlxsw_afa_block *block,
603 struct mlxsw_afa_fwd_entry_ref *fwd_entry_ref)
605 mlxsw_afa_resource_del(&fwd_entry_ref->resource);
606 mlxsw_afa_fwd_entry_put(block->afa, fwd_entry_ref->fwd_entry);
607 kfree(fwd_entry_ref);
611 mlxsw_afa_fwd_entry_ref_destructor(struct mlxsw_afa_block *block,
612 struct mlxsw_afa_resource *resource)
614 struct mlxsw_afa_fwd_entry_ref *fwd_entry_ref;
616 fwd_entry_ref = container_of(resource, struct mlxsw_afa_fwd_entry_ref,
618 mlxsw_afa_fwd_entry_ref_destroy(block, fwd_entry_ref);
621 static struct mlxsw_afa_fwd_entry_ref *
622 mlxsw_afa_fwd_entry_ref_create(struct mlxsw_afa_block *block, u8 local_port)
624 struct mlxsw_afa_fwd_entry_ref *fwd_entry_ref;
625 struct mlxsw_afa_fwd_entry *fwd_entry;
628 fwd_entry_ref = kzalloc(sizeof(*fwd_entry_ref), GFP_KERNEL);
630 return ERR_PTR(-ENOMEM);
631 fwd_entry = mlxsw_afa_fwd_entry_get(block->afa, local_port);
632 if (IS_ERR(fwd_entry)) {
633 err = PTR_ERR(fwd_entry);
634 goto err_fwd_entry_get;
636 fwd_entry_ref->fwd_entry = fwd_entry;
637 fwd_entry_ref->resource.destructor = mlxsw_afa_fwd_entry_ref_destructor;
638 mlxsw_afa_resource_add(block, &fwd_entry_ref->resource);
639 return fwd_entry_ref;
642 kfree(fwd_entry_ref);
646 struct mlxsw_afa_counter {
647 struct mlxsw_afa_resource resource;
652 mlxsw_afa_counter_destroy(struct mlxsw_afa_block *block,
653 struct mlxsw_afa_counter *counter)
655 mlxsw_afa_resource_del(&counter->resource);
656 block->afa->ops->counter_index_put(block->afa->ops_priv,
657 counter->counter_index);
662 mlxsw_afa_counter_destructor(struct mlxsw_afa_block *block,
663 struct mlxsw_afa_resource *resource)
665 struct mlxsw_afa_counter *counter;
667 counter = container_of(resource, struct mlxsw_afa_counter, resource);
668 mlxsw_afa_counter_destroy(block, counter);
671 static struct mlxsw_afa_counter *
672 mlxsw_afa_counter_create(struct mlxsw_afa_block *block)
674 struct mlxsw_afa_counter *counter;
677 counter = kzalloc(sizeof(*counter), GFP_KERNEL);
679 return ERR_PTR(-ENOMEM);
681 err = block->afa->ops->counter_index_get(block->afa->ops_priv,
682 &counter->counter_index);
684 goto err_counter_index_get;
685 counter->resource.destructor = mlxsw_afa_counter_destructor;
686 mlxsw_afa_resource_add(block, &counter->resource);
689 err_counter_index_get:
694 /* 20 bits is a maximum that hardware can handle in trap with userdef action
695 * and carry along with the trapped packet.
697 #define MLXSW_AFA_COOKIE_INDEX_BITS 20
698 #define MLXSW_AFA_COOKIE_INDEX_MAX ((1 << MLXSW_AFA_COOKIE_INDEX_BITS) - 1)
700 static struct mlxsw_afa_cookie *
701 mlxsw_afa_cookie_create(struct mlxsw_afa *mlxsw_afa,
702 const struct flow_action_cookie *fa_cookie)
704 struct mlxsw_afa_cookie *cookie;
708 cookie = kzalloc(sizeof(*cookie) + fa_cookie->cookie_len, GFP_KERNEL);
710 return ERR_PTR(-ENOMEM);
711 refcount_set(&cookie->ref_count, 1);
712 memcpy(&cookie->fa_cookie, fa_cookie,
713 sizeof(*fa_cookie) + fa_cookie->cookie_len);
715 err = rhashtable_insert_fast(&mlxsw_afa->cookie_ht, &cookie->ht_node,
716 mlxsw_afa_cookie_ht_params);
718 goto err_rhashtable_insert;
720 /* Start cookie indexes with 1. Leave the 0 index unused. Packets
721 * that come from the HW which are not dropped by drop-with-cookie
722 * action are going to pass cookie_index 0 to lookup.
725 err = idr_alloc_u32(&mlxsw_afa->cookie_idr, cookie, &cookie_index,
726 MLXSW_AFA_COOKIE_INDEX_MAX, GFP_KERNEL);
729 cookie->cookie_index = cookie_index;
733 rhashtable_remove_fast(&mlxsw_afa->cookie_ht, &cookie->ht_node,
734 mlxsw_afa_cookie_ht_params);
735 err_rhashtable_insert:
740 static void mlxsw_afa_cookie_destroy(struct mlxsw_afa *mlxsw_afa,
741 struct mlxsw_afa_cookie *cookie)
743 idr_remove(&mlxsw_afa->cookie_idr, cookie->cookie_index);
744 rhashtable_remove_fast(&mlxsw_afa->cookie_ht, &cookie->ht_node,
745 mlxsw_afa_cookie_ht_params);
746 kfree_rcu(cookie, rcu);
749 static struct mlxsw_afa_cookie *
750 mlxsw_afa_cookie_get(struct mlxsw_afa *mlxsw_afa,
751 const struct flow_action_cookie *fa_cookie)
753 struct mlxsw_afa_cookie *cookie;
755 cookie = rhashtable_lookup_fast(&mlxsw_afa->cookie_ht, fa_cookie,
756 mlxsw_afa_cookie_ht_params);
758 refcount_inc(&cookie->ref_count);
761 return mlxsw_afa_cookie_create(mlxsw_afa, fa_cookie);
764 static void mlxsw_afa_cookie_put(struct mlxsw_afa *mlxsw_afa,
765 struct mlxsw_afa_cookie *cookie)
767 if (!refcount_dec_and_test(&cookie->ref_count))
769 mlxsw_afa_cookie_destroy(mlxsw_afa, cookie);
772 /* RCU read lock must be held */
773 const struct flow_action_cookie *
774 mlxsw_afa_cookie_lookup(struct mlxsw_afa *mlxsw_afa, u32 cookie_index)
776 struct mlxsw_afa_cookie *cookie;
778 /* 0 index means no cookie */
781 cookie = idr_find(&mlxsw_afa->cookie_idr, cookie_index);
784 return &cookie->fa_cookie;
786 EXPORT_SYMBOL(mlxsw_afa_cookie_lookup);
788 struct mlxsw_afa_cookie_ref {
789 struct mlxsw_afa_resource resource;
790 struct mlxsw_afa_cookie *cookie;
794 mlxsw_afa_cookie_ref_destroy(struct mlxsw_afa_block *block,
795 struct mlxsw_afa_cookie_ref *cookie_ref)
797 mlxsw_afa_resource_del(&cookie_ref->resource);
798 mlxsw_afa_cookie_put(block->afa, cookie_ref->cookie);
803 mlxsw_afa_cookie_ref_destructor(struct mlxsw_afa_block *block,
804 struct mlxsw_afa_resource *resource)
806 struct mlxsw_afa_cookie_ref *cookie_ref;
808 cookie_ref = container_of(resource, struct mlxsw_afa_cookie_ref,
810 mlxsw_afa_cookie_ref_destroy(block, cookie_ref);
813 static struct mlxsw_afa_cookie_ref *
814 mlxsw_afa_cookie_ref_create(struct mlxsw_afa_block *block,
815 const struct flow_action_cookie *fa_cookie)
817 struct mlxsw_afa_cookie_ref *cookie_ref;
818 struct mlxsw_afa_cookie *cookie;
821 cookie_ref = kzalloc(sizeof(*cookie_ref), GFP_KERNEL);
823 return ERR_PTR(-ENOMEM);
824 cookie = mlxsw_afa_cookie_get(block->afa, fa_cookie);
825 if (IS_ERR(cookie)) {
826 err = PTR_ERR(cookie);
829 cookie_ref->cookie = cookie;
830 cookie_ref->resource.destructor = mlxsw_afa_cookie_ref_destructor;
831 mlxsw_afa_resource_add(block, &cookie_ref->resource);
839 #define MLXSW_AFA_ONE_ACTION_LEN 32
840 #define MLXSW_AFA_PAYLOAD_OFFSET 4
842 static char *mlxsw_afa_block_append_action(struct mlxsw_afa_block *block,
843 u8 action_code, u8 action_size)
849 return ERR_PTR(-EINVAL);
850 if (block->cur_act_index + action_size >
851 block->afa->max_acts_per_set) {
852 struct mlxsw_afa_set *set;
854 /* The appended action won't fit into the current action set,
855 * so create a new set.
857 set = mlxsw_afa_set_create(false);
859 return ERR_PTR(-ENOBUFS);
860 set->prev = block->cur_set;
861 block->cur_act_index = 0;
862 block->cur_set->next = set;
863 block->cur_set = set;
866 actions = block->cur_set->ht_key.enc_actions;
867 oneact = actions + block->cur_act_index * MLXSW_AFA_ONE_ACTION_LEN;
868 block->cur_act_index += action_size;
869 mlxsw_afa_all_action_type_set(oneact, action_code);
870 return oneact + MLXSW_AFA_PAYLOAD_OFFSET;
875 * VLAN action is used for manipulating VLANs. It can be used to implement QinQ,
876 * VLAN translation, change of PCP bits of the VLAN tag, push, pop as swap VLANs
880 #define MLXSW_AFA_VLAN_CODE 0x02
881 #define MLXSW_AFA_VLAN_SIZE 1
883 enum mlxsw_afa_vlan_vlan_tag_cmd {
884 MLXSW_AFA_VLAN_VLAN_TAG_CMD_NOP,
885 MLXSW_AFA_VLAN_VLAN_TAG_CMD_PUSH_TAG,
886 MLXSW_AFA_VLAN_VLAN_TAG_CMD_POP_TAG,
889 enum mlxsw_afa_vlan_cmd {
890 MLXSW_AFA_VLAN_CMD_NOP,
891 MLXSW_AFA_VLAN_CMD_SET_OUTER,
892 MLXSW_AFA_VLAN_CMD_SET_INNER,
893 MLXSW_AFA_VLAN_CMD_COPY_OUTER_TO_INNER,
894 MLXSW_AFA_VLAN_CMD_COPY_INNER_TO_OUTER,
895 MLXSW_AFA_VLAN_CMD_SWAP,
898 /* afa_vlan_vlan_tag_cmd
899 * Tag command: push, pop, nop VLAN header.
901 MLXSW_ITEM32(afa, vlan, vlan_tag_cmd, 0x00, 29, 3);
903 /* afa_vlan_vid_cmd */
904 MLXSW_ITEM32(afa, vlan, vid_cmd, 0x04, 29, 3);
907 MLXSW_ITEM32(afa, vlan, vid, 0x04, 0, 12);
909 /* afa_vlan_ethertype_cmd */
910 MLXSW_ITEM32(afa, vlan, ethertype_cmd, 0x08, 29, 3);
912 /* afa_vlan_ethertype
913 * Index to EtherTypes in Switch VLAN EtherType Register (SVER).
915 MLXSW_ITEM32(afa, vlan, ethertype, 0x08, 24, 3);
917 /* afa_vlan_pcp_cmd */
918 MLXSW_ITEM32(afa, vlan, pcp_cmd, 0x08, 13, 3);
921 MLXSW_ITEM32(afa, vlan, pcp, 0x08, 8, 3);
924 mlxsw_afa_vlan_pack(char *payload,
925 enum mlxsw_afa_vlan_vlan_tag_cmd vlan_tag_cmd,
926 enum mlxsw_afa_vlan_cmd vid_cmd, u16 vid,
927 enum mlxsw_afa_vlan_cmd pcp_cmd, u8 pcp,
928 enum mlxsw_afa_vlan_cmd ethertype_cmd, u8 ethertype)
930 mlxsw_afa_vlan_vlan_tag_cmd_set(payload, vlan_tag_cmd);
931 mlxsw_afa_vlan_vid_cmd_set(payload, vid_cmd);
932 mlxsw_afa_vlan_vid_set(payload, vid);
933 mlxsw_afa_vlan_pcp_cmd_set(payload, pcp_cmd);
934 mlxsw_afa_vlan_pcp_set(payload, pcp);
935 mlxsw_afa_vlan_ethertype_cmd_set(payload, ethertype_cmd);
936 mlxsw_afa_vlan_ethertype_set(payload, ethertype);
939 int mlxsw_afa_block_append_vlan_modify(struct mlxsw_afa_block *block,
940 u16 vid, u8 pcp, u8 et,
941 struct netlink_ext_ack *extack)
943 char *act = mlxsw_afa_block_append_action(block,
945 MLXSW_AFA_VLAN_SIZE);
948 NL_SET_ERR_MSG_MOD(extack, "Cannot append vlan_modify action");
951 mlxsw_afa_vlan_pack(act, MLXSW_AFA_VLAN_VLAN_TAG_CMD_NOP,
952 MLXSW_AFA_VLAN_CMD_SET_OUTER, vid,
953 MLXSW_AFA_VLAN_CMD_SET_OUTER, pcp,
954 MLXSW_AFA_VLAN_CMD_SET_OUTER, et);
957 EXPORT_SYMBOL(mlxsw_afa_block_append_vlan_modify);
959 /* Trap Action / Trap With Userdef Action
960 * --------------------------------------
961 * The Trap action enables trapping / mirroring packets to the CPU
962 * as well as discarding packets.
963 * The ACL Trap / Discard separates the forward/discard control from CPU
964 * trap control. In addition, the Trap / Discard action enables activating
965 * SPAN (port mirroring).
967 * The Trap with userdef action action has the same functionality as
968 * the Trap action with addition of user defined value that can be set
969 * and used by higher layer applications.
972 #define MLXSW_AFA_TRAP_CODE 0x03
973 #define MLXSW_AFA_TRAP_SIZE 1
975 #define MLXSW_AFA_TRAPWU_CODE 0x04
976 #define MLXSW_AFA_TRAPWU_SIZE 2
978 enum mlxsw_afa_trap_trap_action {
979 MLXSW_AFA_TRAP_TRAP_ACTION_NOP = 0,
980 MLXSW_AFA_TRAP_TRAP_ACTION_TRAP = 2,
983 /* afa_trap_trap_action
986 MLXSW_ITEM32(afa, trap, trap_action, 0x00, 24, 4);
988 enum mlxsw_afa_trap_forward_action {
989 MLXSW_AFA_TRAP_FORWARD_ACTION_FORWARD = 1,
990 MLXSW_AFA_TRAP_FORWARD_ACTION_DISCARD = 3,
993 /* afa_trap_forward_action
996 MLXSW_ITEM32(afa, trap, forward_action, 0x00, 0, 4);
999 * Trap ID to configure.
1001 MLXSW_ITEM32(afa, trap, trap_id, 0x04, 0, 9);
1003 /* afa_trap_mirror_agent
1006 MLXSW_ITEM32(afa, trap, mirror_agent, 0x08, 29, 3);
1008 /* afa_trap_mirror_enable
1011 MLXSW_ITEM32(afa, trap, mirror_enable, 0x08, 24, 1);
1014 * Value for the SW usage. Can be used to pass information of which
1015 * rule has caused a trap. This may be overwritten by later traps.
1016 * This field does a set on the packet's user_def_val only if this
1017 * is the first trap_id or if the trap_id has replaced the previous
1020 MLXSW_ITEM32(afa, trap, user_def_val, 0x0C, 0, 20);
1023 mlxsw_afa_trap_pack(char *payload,
1024 enum mlxsw_afa_trap_trap_action trap_action,
1025 enum mlxsw_afa_trap_forward_action forward_action,
1028 mlxsw_afa_trap_trap_action_set(payload, trap_action);
1029 mlxsw_afa_trap_forward_action_set(payload, forward_action);
1030 mlxsw_afa_trap_trap_id_set(payload, trap_id);
1034 mlxsw_afa_trapwu_pack(char *payload,
1035 enum mlxsw_afa_trap_trap_action trap_action,
1036 enum mlxsw_afa_trap_forward_action forward_action,
1037 u16 trap_id, u32 user_def_val)
1039 mlxsw_afa_trap_pack(payload, trap_action, forward_action, trap_id);
1040 mlxsw_afa_trap_user_def_val_set(payload, user_def_val);
1044 mlxsw_afa_trap_mirror_pack(char *payload, bool mirror_enable,
1047 mlxsw_afa_trap_mirror_enable_set(payload, mirror_enable);
1048 mlxsw_afa_trap_mirror_agent_set(payload, mirror_agent);
1051 static int mlxsw_afa_block_append_drop_plain(struct mlxsw_afa_block *block,
1054 char *act = mlxsw_afa_block_append_action(block, MLXSW_AFA_TRAP_CODE,
1055 MLXSW_AFA_TRAP_SIZE);
1058 return PTR_ERR(act);
1059 mlxsw_afa_trap_pack(act, MLXSW_AFA_TRAP_TRAP_ACTION_TRAP,
1060 MLXSW_AFA_TRAP_FORWARD_ACTION_DISCARD,
1061 ingress ? MLXSW_TRAP_ID_DISCARD_INGRESS_ACL :
1062 MLXSW_TRAP_ID_DISCARD_EGRESS_ACL);
1067 mlxsw_afa_block_append_drop_with_cookie(struct mlxsw_afa_block *block,
1069 const struct flow_action_cookie *fa_cookie,
1070 struct netlink_ext_ack *extack)
1072 struct mlxsw_afa_cookie_ref *cookie_ref;
1077 cookie_ref = mlxsw_afa_cookie_ref_create(block, fa_cookie);
1078 if (IS_ERR(cookie_ref)) {
1079 NL_SET_ERR_MSG_MOD(extack, "Cannot create cookie for drop action");
1080 return PTR_ERR(cookie_ref);
1082 cookie_index = cookie_ref->cookie->cookie_index;
1084 act = mlxsw_afa_block_append_action(block, MLXSW_AFA_TRAPWU_CODE,
1085 MLXSW_AFA_TRAPWU_SIZE);
1087 NL_SET_ERR_MSG_MOD(extack, "Cannot append drop with cookie action");
1089 goto err_append_action;
1091 mlxsw_afa_trapwu_pack(act, MLXSW_AFA_TRAP_TRAP_ACTION_TRAP,
1092 MLXSW_AFA_TRAP_FORWARD_ACTION_DISCARD,
1093 ingress ? MLXSW_TRAP_ID_DISCARD_INGRESS_ACL :
1094 MLXSW_TRAP_ID_DISCARD_EGRESS_ACL,
1099 mlxsw_afa_cookie_ref_destroy(block, cookie_ref);
1103 int mlxsw_afa_block_append_drop(struct mlxsw_afa_block *block, bool ingress,
1104 const struct flow_action_cookie *fa_cookie,
1105 struct netlink_ext_ack *extack)
1108 mlxsw_afa_block_append_drop_with_cookie(block, ingress,
1109 fa_cookie, extack) :
1110 mlxsw_afa_block_append_drop_plain(block, ingress);
1112 EXPORT_SYMBOL(mlxsw_afa_block_append_drop);
1114 int mlxsw_afa_block_append_trap(struct mlxsw_afa_block *block, u16 trap_id)
1116 char *act = mlxsw_afa_block_append_action(block, MLXSW_AFA_TRAP_CODE,
1117 MLXSW_AFA_TRAP_SIZE);
1120 return PTR_ERR(act);
1121 mlxsw_afa_trap_pack(act, MLXSW_AFA_TRAP_TRAP_ACTION_TRAP,
1122 MLXSW_AFA_TRAP_FORWARD_ACTION_DISCARD, trap_id);
1125 EXPORT_SYMBOL(mlxsw_afa_block_append_trap);
1127 int mlxsw_afa_block_append_trap_and_forward(struct mlxsw_afa_block *block,
1130 char *act = mlxsw_afa_block_append_action(block, MLXSW_AFA_TRAP_CODE,
1131 MLXSW_AFA_TRAP_SIZE);
1134 return PTR_ERR(act);
1135 mlxsw_afa_trap_pack(act, MLXSW_AFA_TRAP_TRAP_ACTION_TRAP,
1136 MLXSW_AFA_TRAP_FORWARD_ACTION_FORWARD, trap_id);
1139 EXPORT_SYMBOL(mlxsw_afa_block_append_trap_and_forward);
1141 struct mlxsw_afa_mirror {
1142 struct mlxsw_afa_resource resource;
1149 mlxsw_afa_mirror_destroy(struct mlxsw_afa_block *block,
1150 struct mlxsw_afa_mirror *mirror)
1152 mlxsw_afa_resource_del(&mirror->resource);
1153 block->afa->ops->mirror_del(block->afa->ops_priv,
1154 mirror->local_in_port,
1161 mlxsw_afa_mirror_destructor(struct mlxsw_afa_block *block,
1162 struct mlxsw_afa_resource *resource)
1164 struct mlxsw_afa_mirror *mirror;
1166 mirror = container_of(resource, struct mlxsw_afa_mirror, resource);
1167 mlxsw_afa_mirror_destroy(block, mirror);
1170 static struct mlxsw_afa_mirror *
1171 mlxsw_afa_mirror_create(struct mlxsw_afa_block *block, u8 local_in_port,
1172 const struct net_device *out_dev, bool ingress)
1174 struct mlxsw_afa_mirror *mirror;
1177 mirror = kzalloc(sizeof(*mirror), GFP_KERNEL);
1179 return ERR_PTR(-ENOMEM);
1181 err = block->afa->ops->mirror_add(block->afa->ops_priv,
1182 local_in_port, out_dev,
1183 ingress, &mirror->span_id);
1185 goto err_mirror_add;
1187 mirror->ingress = ingress;
1188 mirror->local_in_port = local_in_port;
1189 mirror->resource.destructor = mlxsw_afa_mirror_destructor;
1190 mlxsw_afa_resource_add(block, &mirror->resource);
1195 return ERR_PTR(err);
1199 mlxsw_afa_block_append_allocated_mirror(struct mlxsw_afa_block *block,
1202 char *act = mlxsw_afa_block_append_action(block,
1203 MLXSW_AFA_TRAP_CODE,
1204 MLXSW_AFA_TRAP_SIZE);
1206 return PTR_ERR(act);
1207 mlxsw_afa_trap_pack(act, MLXSW_AFA_TRAP_TRAP_ACTION_NOP,
1208 MLXSW_AFA_TRAP_FORWARD_ACTION_FORWARD, 0);
1209 mlxsw_afa_trap_mirror_pack(act, true, mirror_agent);
1214 mlxsw_afa_block_append_mirror(struct mlxsw_afa_block *block, u8 local_in_port,
1215 const struct net_device *out_dev, bool ingress,
1216 struct netlink_ext_ack *extack)
1218 struct mlxsw_afa_mirror *mirror;
1221 mirror = mlxsw_afa_mirror_create(block, local_in_port, out_dev,
1223 if (IS_ERR(mirror)) {
1224 NL_SET_ERR_MSG_MOD(extack, "Cannot create mirror action");
1225 return PTR_ERR(mirror);
1227 err = mlxsw_afa_block_append_allocated_mirror(block, mirror->span_id);
1229 NL_SET_ERR_MSG_MOD(extack, "Cannot append mirror action");
1230 goto err_append_allocated_mirror;
1235 err_append_allocated_mirror:
1236 mlxsw_afa_mirror_destroy(block, mirror);
1239 EXPORT_SYMBOL(mlxsw_afa_block_append_mirror);
1243 * The QOS_ACTION is used for manipulating the QoS attributes of a packet. It
1244 * can be used to change the DCSP, ECN, Color and Switch Priority of the packet.
1245 * Note that PCP field can be changed using the VLAN action.
1248 #define MLXSW_AFA_QOS_CODE 0x06
1249 #define MLXSW_AFA_QOS_SIZE 1
1251 enum mlxsw_afa_qos_cmd {
1253 MLXSW_AFA_QOS_CMD_NOP,
1255 MLXSW_AFA_QOS_CMD_SET,
1258 /* afa_qos_switch_prio_cmd
1259 * Switch Priority command as per mlxsw_afa_qos_cmd.
1261 MLXSW_ITEM32(afa, qos, switch_prio_cmd, 0x08, 14, 2);
1263 /* afa_qos_switch_prio
1266 MLXSW_ITEM32(afa, qos, switch_prio, 0x08, 0, 4);
1269 mlxsw_afa_qos_switch_prio_pack(char *payload,
1270 enum mlxsw_afa_qos_cmd prio_cmd, u8 prio)
1272 mlxsw_afa_qos_switch_prio_cmd_set(payload, prio_cmd);
1273 mlxsw_afa_qos_switch_prio_set(payload, prio);
1276 int mlxsw_afa_block_append_qos_switch_prio(struct mlxsw_afa_block *block,
1278 struct netlink_ext_ack *extack)
1280 char *act = mlxsw_afa_block_append_action(block,
1282 MLXSW_AFA_QOS_SIZE);
1285 NL_SET_ERR_MSG_MOD(extack, "Cannot append QOS action");
1286 return PTR_ERR(act);
1288 mlxsw_afa_qos_switch_prio_pack(act, MLXSW_AFA_QOS_CMD_SET,
1292 EXPORT_SYMBOL(mlxsw_afa_block_append_qos_switch_prio);
1294 /* Forwarding Action
1296 * Forwarding Action can be used to implement Policy Based Switching (PBS)
1297 * as well as OpenFlow related "Output" action.
1300 #define MLXSW_AFA_FORWARD_CODE 0x07
1301 #define MLXSW_AFA_FORWARD_SIZE 1
1303 enum mlxsw_afa_forward_type {
1304 /* PBS, Policy Based Switching */
1305 MLXSW_AFA_FORWARD_TYPE_PBS,
1306 /* Output, OpenFlow output type */
1307 MLXSW_AFA_FORWARD_TYPE_OUTPUT,
1310 /* afa_forward_type */
1311 MLXSW_ITEM32(afa, forward, type, 0x00, 24, 2);
1313 /* afa_forward_pbs_ptr
1314 * A pointer to the PBS entry configured by PPBS register.
1315 * Reserved when in_port is set.
1317 MLXSW_ITEM32(afa, forward, pbs_ptr, 0x08, 0, 24);
1319 /* afa_forward_in_port
1320 * Packet is forwarded back to the ingress port.
1322 MLXSW_ITEM32(afa, forward, in_port, 0x0C, 0, 1);
1325 mlxsw_afa_forward_pack(char *payload, enum mlxsw_afa_forward_type type,
1326 u32 pbs_ptr, bool in_port)
1328 mlxsw_afa_forward_type_set(payload, type);
1329 mlxsw_afa_forward_pbs_ptr_set(payload, pbs_ptr);
1330 mlxsw_afa_forward_in_port_set(payload, in_port);
1333 int mlxsw_afa_block_append_fwd(struct mlxsw_afa_block *block,
1334 u8 local_port, bool in_port,
1335 struct netlink_ext_ack *extack)
1337 struct mlxsw_afa_fwd_entry_ref *fwd_entry_ref;
1343 NL_SET_ERR_MSG_MOD(extack, "Forwarding to ingress port is not supported");
1346 fwd_entry_ref = mlxsw_afa_fwd_entry_ref_create(block, local_port);
1347 if (IS_ERR(fwd_entry_ref)) {
1348 NL_SET_ERR_MSG_MOD(extack, "Cannot create forward action");
1349 return PTR_ERR(fwd_entry_ref);
1351 kvdl_index = fwd_entry_ref->fwd_entry->kvdl_index;
1353 act = mlxsw_afa_block_append_action(block, MLXSW_AFA_FORWARD_CODE,
1354 MLXSW_AFA_FORWARD_SIZE);
1356 NL_SET_ERR_MSG_MOD(extack, "Cannot append forward action");
1358 goto err_append_action;
1360 mlxsw_afa_forward_pack(act, MLXSW_AFA_FORWARD_TYPE_PBS,
1361 kvdl_index, in_port);
1365 mlxsw_afa_fwd_entry_ref_destroy(block, fwd_entry_ref);
1368 EXPORT_SYMBOL(mlxsw_afa_block_append_fwd);
1370 /* Policing and Counting Action
1371 * ----------------------------
1372 * Policing and Counting action is used for binding policer and counter
1376 #define MLXSW_AFA_POLCNT_CODE 0x08
1377 #define MLXSW_AFA_POLCNT_SIZE 1
1379 enum mlxsw_afa_polcnt_counter_set_type {
1381 MLXSW_AFA_POLCNT_COUNTER_SET_TYPE_NO_COUNT = 0x00,
1382 /* Count packets and bytes */
1383 MLXSW_AFA_POLCNT_COUNTER_SET_TYPE_PACKETS_BYTES = 0x03,
1384 /* Count only packets */
1385 MLXSW_AFA_POLCNT_COUNTER_SET_TYPE_PACKETS = 0x05,
1388 /* afa_polcnt_counter_set_type
1389 * Counter set type for flow counters.
1391 MLXSW_ITEM32(afa, polcnt, counter_set_type, 0x04, 24, 8);
1393 /* afa_polcnt_counter_index
1394 * Counter index for flow counters.
1396 MLXSW_ITEM32(afa, polcnt, counter_index, 0x04, 0, 24);
1399 mlxsw_afa_polcnt_pack(char *payload,
1400 enum mlxsw_afa_polcnt_counter_set_type set_type,
1403 mlxsw_afa_polcnt_counter_set_type_set(payload, set_type);
1404 mlxsw_afa_polcnt_counter_index_set(payload, counter_index);
1407 int mlxsw_afa_block_append_allocated_counter(struct mlxsw_afa_block *block,
1410 char *act = mlxsw_afa_block_append_action(block, MLXSW_AFA_POLCNT_CODE,
1411 MLXSW_AFA_POLCNT_SIZE);
1413 return PTR_ERR(act);
1414 mlxsw_afa_polcnt_pack(act, MLXSW_AFA_POLCNT_COUNTER_SET_TYPE_PACKETS_BYTES,
1418 EXPORT_SYMBOL(mlxsw_afa_block_append_allocated_counter);
1420 int mlxsw_afa_block_append_counter(struct mlxsw_afa_block *block,
1421 u32 *p_counter_index,
1422 struct netlink_ext_ack *extack)
1424 struct mlxsw_afa_counter *counter;
1428 counter = mlxsw_afa_counter_create(block);
1429 if (IS_ERR(counter)) {
1430 NL_SET_ERR_MSG_MOD(extack, "Cannot create count action");
1431 return PTR_ERR(counter);
1433 counter_index = counter->counter_index;
1435 err = mlxsw_afa_block_append_allocated_counter(block, counter_index);
1437 NL_SET_ERR_MSG_MOD(extack, "Cannot append count action");
1438 goto err_append_allocated_counter;
1440 if (p_counter_index)
1441 *p_counter_index = counter_index;
1444 err_append_allocated_counter:
1445 mlxsw_afa_counter_destroy(block, counter);
1448 EXPORT_SYMBOL(mlxsw_afa_block_append_counter);
1450 /* Virtual Router and Forwarding Domain Action
1451 * -------------------------------------------
1452 * Virtual Switch action is used for manipulate the Virtual Router (VR),
1453 * MPLS label space and the Forwarding Identifier (FID).
1456 #define MLXSW_AFA_VIRFWD_CODE 0x0E
1457 #define MLXSW_AFA_VIRFWD_SIZE 1
1459 enum mlxsw_afa_virfwd_fid_cmd {
1461 MLXSW_AFA_VIRFWD_FID_CMD_NOOP,
1462 /* Set the Forwarding Identifier (FID) to fid */
1463 MLXSW_AFA_VIRFWD_FID_CMD_SET,
1466 /* afa_virfwd_fid_cmd */
1467 MLXSW_ITEM32(afa, virfwd, fid_cmd, 0x08, 29, 3);
1472 MLXSW_ITEM32(afa, virfwd, fid, 0x08, 0, 16);
1474 static inline void mlxsw_afa_virfwd_pack(char *payload,
1475 enum mlxsw_afa_virfwd_fid_cmd fid_cmd,
1478 mlxsw_afa_virfwd_fid_cmd_set(payload, fid_cmd);
1479 mlxsw_afa_virfwd_fid_set(payload, fid);
1482 int mlxsw_afa_block_append_fid_set(struct mlxsw_afa_block *block, u16 fid,
1483 struct netlink_ext_ack *extack)
1485 char *act = mlxsw_afa_block_append_action(block,
1486 MLXSW_AFA_VIRFWD_CODE,
1487 MLXSW_AFA_VIRFWD_SIZE);
1489 NL_SET_ERR_MSG_MOD(extack, "Cannot append fid_set action");
1490 return PTR_ERR(act);
1492 mlxsw_afa_virfwd_pack(act, MLXSW_AFA_VIRFWD_FID_CMD_SET, fid);
1495 EXPORT_SYMBOL(mlxsw_afa_block_append_fid_set);
1497 /* MC Routing Action
1499 * The Multicast router action. Can be used by RMFT_V2 - Router Multicast
1500 * Forwarding Table Version 2 Register.
1503 #define MLXSW_AFA_MCROUTER_CODE 0x10
1504 #define MLXSW_AFA_MCROUTER_SIZE 2
1506 enum mlxsw_afa_mcrouter_rpf_action {
1507 MLXSW_AFA_MCROUTER_RPF_ACTION_NOP,
1508 MLXSW_AFA_MCROUTER_RPF_ACTION_TRAP,
1509 MLXSW_AFA_MCROUTER_RPF_ACTION_DISCARD_ERROR,
1512 /* afa_mcrouter_rpf_action */
1513 MLXSW_ITEM32(afa, mcrouter, rpf_action, 0x00, 28, 3);
1515 /* afa_mcrouter_expected_irif */
1516 MLXSW_ITEM32(afa, mcrouter, expected_irif, 0x00, 0, 16);
1518 /* afa_mcrouter_min_mtu */
1519 MLXSW_ITEM32(afa, mcrouter, min_mtu, 0x08, 0, 16);
1521 enum mlxsw_afa_mrouter_vrmid {
1522 MLXSW_AFA_MCROUTER_VRMID_INVALID,
1523 MLXSW_AFA_MCROUTER_VRMID_VALID
1526 /* afa_mcrouter_vrmid
1527 * Valid RMID: rigr_rmid_index is used as RMID
1529 MLXSW_ITEM32(afa, mcrouter, vrmid, 0x0C, 31, 1);
1531 /* afa_mcrouter_rigr_rmid_index
1532 * When the vrmid field is set to invalid, the field is used as pointer to
1533 * Router Interface Group (RIGR) Table in the KVD linear.
1534 * When the vrmid is set to valid, the field is used as RMID index, ranged
1535 * from 0 to max_mid - 1. The index is to the Port Group Table.
1537 MLXSW_ITEM32(afa, mcrouter, rigr_rmid_index, 0x0C, 0, 24);
1540 mlxsw_afa_mcrouter_pack(char *payload,
1541 enum mlxsw_afa_mcrouter_rpf_action rpf_action,
1542 u16 expected_irif, u16 min_mtu,
1543 enum mlxsw_afa_mrouter_vrmid vrmid, u32 rigr_rmid_index)
1546 mlxsw_afa_mcrouter_rpf_action_set(payload, rpf_action);
1547 mlxsw_afa_mcrouter_expected_irif_set(payload, expected_irif);
1548 mlxsw_afa_mcrouter_min_mtu_set(payload, min_mtu);
1549 mlxsw_afa_mcrouter_vrmid_set(payload, vrmid);
1550 mlxsw_afa_mcrouter_rigr_rmid_index_set(payload, rigr_rmid_index);
1553 int mlxsw_afa_block_append_mcrouter(struct mlxsw_afa_block *block,
1554 u16 expected_irif, u16 min_mtu,
1555 bool rmid_valid, u32 kvdl_index)
1557 char *act = mlxsw_afa_block_append_action(block,
1558 MLXSW_AFA_MCROUTER_CODE,
1559 MLXSW_AFA_MCROUTER_SIZE);
1561 return PTR_ERR(act);
1562 mlxsw_afa_mcrouter_pack(act, MLXSW_AFA_MCROUTER_RPF_ACTION_TRAP,
1563 expected_irif, min_mtu, rmid_valid, kvdl_index);
1566 EXPORT_SYMBOL(mlxsw_afa_block_append_mcrouter);