2 * drivers/net/ethernet/rocker/rocker_ofdpa.c - Rocker switch OF-DPA-like
4 * Copyright (c) 2014 Scott Feldman <sfeldma@gmail.com>
5 * Copyright (c) 2014-2016 Jiri Pirko <jiri@mellanox.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
13 #include <linux/kernel.h>
14 #include <linux/types.h>
15 #include <linux/spinlock.h>
16 #include <linux/hashtable.h>
17 #include <linux/crc32.h>
18 #include <linux/netdevice.h>
19 #include <linux/inetdevice.h>
20 #include <linux/if_vlan.h>
21 #include <linux/if_bridge.h>
22 #include <net/neighbour.h>
23 #include <net/switchdev.h>
24 #include <net/ip_fib.h>
28 #include "rocker_tlv.h"
30 struct ofdpa_flow_tbl_key {
32 enum rocker_of_dpa_table_id tbl_id;
37 enum rocker_of_dpa_table_id goto_tbl;
43 enum rocker_of_dpa_table_id goto_tbl;
52 u8 eth_dst_mask[ETH_ALEN];
55 enum rocker_of_dpa_table_id goto_tbl;
62 enum rocker_of_dpa_table_id goto_tbl;
67 u8 eth_dst_mask[ETH_ALEN];
72 enum rocker_of_dpa_table_id goto_tbl;
80 u8 eth_src_mask[ETH_ALEN];
82 u8 eth_dst_mask[ETH_ALEN];
95 struct ofdpa_flow_tbl_entry {
96 struct hlist_node entry;
99 struct ofdpa_flow_tbl_key key;
101 u32 key_crc32; /* key */
105 struct ofdpa_group_tbl_entry {
106 struct hlist_node entry;
108 u32 group_id; /* key */
116 u8 eth_src[ETH_ALEN];
117 u8 eth_dst[ETH_ALEN];
122 u8 eth_src[ETH_ALEN];
123 u8 eth_dst[ETH_ALEN];
131 struct ofdpa_fdb_tbl_entry {
132 struct hlist_node entry;
133 u32 key_crc32; /* key */
135 unsigned long touched;
136 struct ofdpa_fdb_tbl_key {
137 struct ofdpa_port *ofdpa_port;
143 struct ofdpa_internal_vlan_tbl_entry {
144 struct hlist_node entry;
145 int ifindex; /* key */
150 struct ofdpa_neigh_tbl_entry {
151 struct hlist_node entry;
152 __be32 ip_addr; /* key */
153 struct net_device *dev;
156 u8 eth_dst[ETH_ALEN];
161 OFDPA_CTRL_LINK_LOCAL_MCAST,
162 OFDPA_CTRL_LOCAL_ARP,
163 OFDPA_CTRL_IPV4_MCAST,
164 OFDPA_CTRL_IPV6_MCAST,
165 OFDPA_CTRL_DFLT_BRIDGING,
170 #define OFDPA_INTERNAL_VLAN_ID_BASE 0x0f00
171 #define OFDPA_N_INTERNAL_VLANS 255
172 #define OFDPA_VLAN_BITMAP_LEN BITS_TO_LONGS(VLAN_N_VID)
173 #define OFDPA_INTERNAL_VLAN_BITMAP_LEN BITS_TO_LONGS(OFDPA_N_INTERNAL_VLANS)
174 #define OFDPA_UNTAGGED_VID 0
177 struct rocker *rocker;
178 DECLARE_HASHTABLE(flow_tbl, 16);
179 spinlock_t flow_tbl_lock; /* for flow tbl accesses */
180 u64 flow_tbl_next_cookie;
181 DECLARE_HASHTABLE(group_tbl, 16);
182 spinlock_t group_tbl_lock; /* for group tbl accesses */
183 struct timer_list fdb_cleanup_timer;
184 DECLARE_HASHTABLE(fdb_tbl, 16);
185 spinlock_t fdb_tbl_lock; /* for fdb tbl accesses */
186 unsigned long internal_vlan_bitmap[OFDPA_INTERNAL_VLAN_BITMAP_LEN];
187 DECLARE_HASHTABLE(internal_vlan_tbl, 8);
188 spinlock_t internal_vlan_tbl_lock; /* for vlan tbl accesses */
189 DECLARE_HASHTABLE(neigh_tbl, 16);
190 spinlock_t neigh_tbl_lock; /* for neigh tbl accesses */
191 u32 neigh_tbl_next_index;
192 unsigned long ageing_time;
198 struct rocker_port *rocker_port;
199 struct net_device *dev;
201 struct net_device *bridge_dev;
202 __be16 internal_vlan_id;
205 unsigned long ageing_time;
206 bool ctrls[OFDPA_CTRL_MAX];
207 unsigned long vlan_bitmap[OFDPA_VLAN_BITMAP_LEN];
210 static const u8 zero_mac[ETH_ALEN] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
211 static const u8 ff_mac[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
212 static const u8 ll_mac[ETH_ALEN] = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 };
213 static const u8 ll_mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0 };
214 static const u8 mcast_mac[ETH_ALEN] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 };
215 static const u8 ipv4_mcast[ETH_ALEN] = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
216 static const u8 ipv4_mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0x80, 0x00, 0x00 };
217 static const u8 ipv6_mcast[ETH_ALEN] = { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
218 static const u8 ipv6_mask[ETH_ALEN] = { 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 };
220 /* Rocker priority levels for flow table entries. Higher
221 * priority match takes precedence over lower priority match.
225 OFDPA_PRIORITY_UNKNOWN = 0,
226 OFDPA_PRIORITY_IG_PORT = 1,
227 OFDPA_PRIORITY_VLAN = 1,
228 OFDPA_PRIORITY_TERM_MAC_UCAST = 0,
229 OFDPA_PRIORITY_TERM_MAC_MCAST = 1,
230 OFDPA_PRIORITY_BRIDGING_VLAN_DFLT_EXACT = 1,
231 OFDPA_PRIORITY_BRIDGING_VLAN_DFLT_WILD = 2,
232 OFDPA_PRIORITY_BRIDGING_VLAN = 3,
233 OFDPA_PRIORITY_BRIDGING_TENANT_DFLT_EXACT = 1,
234 OFDPA_PRIORITY_BRIDGING_TENANT_DFLT_WILD = 2,
235 OFDPA_PRIORITY_BRIDGING_TENANT = 3,
236 OFDPA_PRIORITY_ACL_CTRL = 3,
237 OFDPA_PRIORITY_ACL_NORMAL = 2,
238 OFDPA_PRIORITY_ACL_DFLT = 1,
241 static bool ofdpa_vlan_id_is_internal(__be16 vlan_id)
243 u16 start = OFDPA_INTERNAL_VLAN_ID_BASE;
245 u16 _vlan_id = ntohs(vlan_id);
247 return (_vlan_id >= start && _vlan_id <= end);
250 static __be16 ofdpa_port_vid_to_vlan(const struct ofdpa_port *ofdpa_port,
251 u16 vid, bool *pop_vlan)
257 vlan_id = htons(vid);
259 vlan_id = ofdpa_port->internal_vlan_id;
267 static u16 ofdpa_port_vlan_to_vid(const struct ofdpa_port *ofdpa_port,
270 if (ofdpa_vlan_id_is_internal(vlan_id))
273 return ntohs(vlan_id);
276 static bool ofdpa_port_is_slave(const struct ofdpa_port *ofdpa_port,
279 return ofdpa_port->bridge_dev &&
280 !strcmp(ofdpa_port->bridge_dev->rtnl_link_ops->kind, kind);
283 static bool ofdpa_port_is_bridged(const struct ofdpa_port *ofdpa_port)
285 return ofdpa_port_is_slave(ofdpa_port, "bridge");
288 static bool ofdpa_port_is_ovsed(const struct ofdpa_port *ofdpa_port)
290 return ofdpa_port_is_slave(ofdpa_port, "openvswitch");
293 #define OFDPA_OP_FLAG_REMOVE BIT(0)
294 #define OFDPA_OP_FLAG_NOWAIT BIT(1)
295 #define OFDPA_OP_FLAG_LEARNED BIT(2)
296 #define OFDPA_OP_FLAG_REFRESH BIT(3)
298 static bool ofdpa_flags_nowait(int flags)
300 return flags & OFDPA_OP_FLAG_NOWAIT;
303 static void *__ofdpa_mem_alloc(struct switchdev_trans *trans, int flags,
306 struct switchdev_trans_item *elem = NULL;
307 gfp_t gfp_flags = (flags & OFDPA_OP_FLAG_NOWAIT) ?
308 GFP_ATOMIC : GFP_KERNEL;
310 /* If in transaction prepare phase, allocate the memory
311 * and enqueue it on a transaction. If in transaction
312 * commit phase, dequeue the memory from the transaction
313 * rather than re-allocating the memory. The idea is the
314 * driver code paths for prepare and commit are identical
315 * so the memory allocated in the prepare phase is the
316 * memory used in the commit phase.
320 elem = kzalloc(size + sizeof(*elem), gfp_flags);
321 } else if (switchdev_trans_ph_prepare(trans)) {
322 elem = kzalloc(size + sizeof(*elem), gfp_flags);
325 switchdev_trans_item_enqueue(trans, elem, kfree, elem);
327 elem = switchdev_trans_item_dequeue(trans);
330 return elem ? elem + 1 : NULL;
333 static void *ofdpa_kzalloc(struct switchdev_trans *trans, int flags,
336 return __ofdpa_mem_alloc(trans, flags, size);
339 static void *ofdpa_kcalloc(struct switchdev_trans *trans, int flags,
340 size_t n, size_t size)
342 return __ofdpa_mem_alloc(trans, flags, n * size);
345 static void ofdpa_kfree(struct switchdev_trans *trans, const void *mem)
347 struct switchdev_trans_item *elem;
349 /* Frees are ignored if in transaction prepare phase. The
350 * memory remains on the per-port list until freed in the
354 if (switchdev_trans_ph_prepare(trans))
357 elem = (struct switchdev_trans_item *) mem - 1;
361 /*************************************************************
362 * Flow, group, FDB, internal VLAN and neigh command prepares
363 *************************************************************/
366 ofdpa_cmd_flow_tbl_add_ig_port(struct rocker_desc_info *desc_info,
367 const struct ofdpa_flow_tbl_entry *entry)
369 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT,
370 entry->key.ig_port.in_pport))
372 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT_MASK,
373 entry->key.ig_port.in_pport_mask))
375 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_GOTO_TABLE_ID,
376 entry->key.ig_port.goto_tbl))
383 ofdpa_cmd_flow_tbl_add_vlan(struct rocker_desc_info *desc_info,
384 const struct ofdpa_flow_tbl_entry *entry)
386 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT,
387 entry->key.vlan.in_pport))
389 if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID,
390 entry->key.vlan.vlan_id))
392 if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID_MASK,
393 entry->key.vlan.vlan_id_mask))
395 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_GOTO_TABLE_ID,
396 entry->key.vlan.goto_tbl))
398 if (entry->key.vlan.untagged &&
399 rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_NEW_VLAN_ID,
400 entry->key.vlan.new_vlan_id))
407 ofdpa_cmd_flow_tbl_add_term_mac(struct rocker_desc_info *desc_info,
408 const struct ofdpa_flow_tbl_entry *entry)
410 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT,
411 entry->key.term_mac.in_pport))
413 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT_MASK,
414 entry->key.term_mac.in_pport_mask))
416 if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_ETHERTYPE,
417 entry->key.term_mac.eth_type))
419 if (rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC,
420 ETH_ALEN, entry->key.term_mac.eth_dst))
422 if (rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC_MASK,
423 ETH_ALEN, entry->key.term_mac.eth_dst_mask))
425 if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID,
426 entry->key.term_mac.vlan_id))
428 if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID_MASK,
429 entry->key.term_mac.vlan_id_mask))
431 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_GOTO_TABLE_ID,
432 entry->key.term_mac.goto_tbl))
434 if (entry->key.term_mac.copy_to_cpu &&
435 rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_COPY_CPU_ACTION,
436 entry->key.term_mac.copy_to_cpu))
443 ofdpa_cmd_flow_tbl_add_ucast_routing(struct rocker_desc_info *desc_info,
444 const struct ofdpa_flow_tbl_entry *entry)
446 if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_ETHERTYPE,
447 entry->key.ucast_routing.eth_type))
449 if (rocker_tlv_put_be32(desc_info, ROCKER_TLV_OF_DPA_DST_IP,
450 entry->key.ucast_routing.dst4))
452 if (rocker_tlv_put_be32(desc_info, ROCKER_TLV_OF_DPA_DST_IP_MASK,
453 entry->key.ucast_routing.dst4_mask))
455 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_GOTO_TABLE_ID,
456 entry->key.ucast_routing.goto_tbl))
458 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID,
459 entry->key.ucast_routing.group_id))
466 ofdpa_cmd_flow_tbl_add_bridge(struct rocker_desc_info *desc_info,
467 const struct ofdpa_flow_tbl_entry *entry)
469 if (entry->key.bridge.has_eth_dst &&
470 rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC,
471 ETH_ALEN, entry->key.bridge.eth_dst))
473 if (entry->key.bridge.has_eth_dst_mask &&
474 rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC_MASK,
475 ETH_ALEN, entry->key.bridge.eth_dst_mask))
477 if (entry->key.bridge.vlan_id &&
478 rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID,
479 entry->key.bridge.vlan_id))
481 if (entry->key.bridge.tunnel_id &&
482 rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_TUNNEL_ID,
483 entry->key.bridge.tunnel_id))
485 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_GOTO_TABLE_ID,
486 entry->key.bridge.goto_tbl))
488 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID,
489 entry->key.bridge.group_id))
491 if (entry->key.bridge.copy_to_cpu &&
492 rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_COPY_CPU_ACTION,
493 entry->key.bridge.copy_to_cpu))
500 ofdpa_cmd_flow_tbl_add_acl(struct rocker_desc_info *desc_info,
501 const struct ofdpa_flow_tbl_entry *entry)
503 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT,
504 entry->key.acl.in_pport))
506 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT_MASK,
507 entry->key.acl.in_pport_mask))
509 if (rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_SRC_MAC,
510 ETH_ALEN, entry->key.acl.eth_src))
512 if (rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_SRC_MAC_MASK,
513 ETH_ALEN, entry->key.acl.eth_src_mask))
515 if (rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC,
516 ETH_ALEN, entry->key.acl.eth_dst))
518 if (rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC_MASK,
519 ETH_ALEN, entry->key.acl.eth_dst_mask))
521 if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_ETHERTYPE,
522 entry->key.acl.eth_type))
524 if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID,
525 entry->key.acl.vlan_id))
527 if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID_MASK,
528 entry->key.acl.vlan_id_mask))
531 switch (ntohs(entry->key.acl.eth_type)) {
534 if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_IP_PROTO,
535 entry->key.acl.ip_proto))
537 if (rocker_tlv_put_u8(desc_info,
538 ROCKER_TLV_OF_DPA_IP_PROTO_MASK,
539 entry->key.acl.ip_proto_mask))
541 if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_IP_DSCP,
542 entry->key.acl.ip_tos & 0x3f))
544 if (rocker_tlv_put_u8(desc_info,
545 ROCKER_TLV_OF_DPA_IP_DSCP_MASK,
546 entry->key.acl.ip_tos_mask & 0x3f))
548 if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_IP_ECN,
549 (entry->key.acl.ip_tos & 0xc0) >> 6))
551 if (rocker_tlv_put_u8(desc_info,
552 ROCKER_TLV_OF_DPA_IP_ECN_MASK,
553 (entry->key.acl.ip_tos_mask & 0xc0) >> 6))
558 if (entry->key.acl.group_id != ROCKER_GROUP_NONE &&
559 rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID,
560 entry->key.acl.group_id))
566 static int ofdpa_cmd_flow_tbl_add(const struct rocker_port *rocker_port,
567 struct rocker_desc_info *desc_info,
570 const struct ofdpa_flow_tbl_entry *entry = priv;
571 struct rocker_tlv *cmd_info;
574 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE, entry->cmd))
576 cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
579 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_TABLE_ID,
582 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_PRIORITY,
583 entry->key.priority))
585 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_HARDTIME, 0))
587 if (rocker_tlv_put_u64(desc_info, ROCKER_TLV_OF_DPA_COOKIE,
591 switch (entry->key.tbl_id) {
592 case ROCKER_OF_DPA_TABLE_ID_INGRESS_PORT:
593 err = ofdpa_cmd_flow_tbl_add_ig_port(desc_info, entry);
595 case ROCKER_OF_DPA_TABLE_ID_VLAN:
596 err = ofdpa_cmd_flow_tbl_add_vlan(desc_info, entry);
598 case ROCKER_OF_DPA_TABLE_ID_TERMINATION_MAC:
599 err = ofdpa_cmd_flow_tbl_add_term_mac(desc_info, entry);
601 case ROCKER_OF_DPA_TABLE_ID_UNICAST_ROUTING:
602 err = ofdpa_cmd_flow_tbl_add_ucast_routing(desc_info, entry);
604 case ROCKER_OF_DPA_TABLE_ID_BRIDGING:
605 err = ofdpa_cmd_flow_tbl_add_bridge(desc_info, entry);
607 case ROCKER_OF_DPA_TABLE_ID_ACL_POLICY:
608 err = ofdpa_cmd_flow_tbl_add_acl(desc_info, entry);
618 rocker_tlv_nest_end(desc_info, cmd_info);
623 static int ofdpa_cmd_flow_tbl_del(const struct rocker_port *rocker_port,
624 struct rocker_desc_info *desc_info,
627 const struct ofdpa_flow_tbl_entry *entry = priv;
628 struct rocker_tlv *cmd_info;
630 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE, entry->cmd))
632 cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
635 if (rocker_tlv_put_u64(desc_info, ROCKER_TLV_OF_DPA_COOKIE,
638 rocker_tlv_nest_end(desc_info, cmd_info);
644 ofdpa_cmd_group_tbl_add_l2_interface(struct rocker_desc_info *desc_info,
645 struct ofdpa_group_tbl_entry *entry)
647 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_OUT_PPORT,
648 ROCKER_GROUP_PORT_GET(entry->group_id)))
650 if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_POP_VLAN,
651 entry->l2_interface.pop_vlan))
658 ofdpa_cmd_group_tbl_add_l2_rewrite(struct rocker_desc_info *desc_info,
659 const struct ofdpa_group_tbl_entry *entry)
661 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID_LOWER,
662 entry->l2_rewrite.group_id))
664 if (!is_zero_ether_addr(entry->l2_rewrite.eth_src) &&
665 rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_SRC_MAC,
666 ETH_ALEN, entry->l2_rewrite.eth_src))
668 if (!is_zero_ether_addr(entry->l2_rewrite.eth_dst) &&
669 rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC,
670 ETH_ALEN, entry->l2_rewrite.eth_dst))
672 if (entry->l2_rewrite.vlan_id &&
673 rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID,
674 entry->l2_rewrite.vlan_id))
681 ofdpa_cmd_group_tbl_add_group_ids(struct rocker_desc_info *desc_info,
682 const struct ofdpa_group_tbl_entry *entry)
685 struct rocker_tlv *group_ids;
687 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_GROUP_COUNT,
691 group_ids = rocker_tlv_nest_start(desc_info,
692 ROCKER_TLV_OF_DPA_GROUP_IDS);
696 for (i = 0; i < entry->group_count; i++)
697 /* Note TLV array is 1-based */
698 if (rocker_tlv_put_u32(desc_info, i + 1, entry->group_ids[i]))
701 rocker_tlv_nest_end(desc_info, group_ids);
707 ofdpa_cmd_group_tbl_add_l3_unicast(struct rocker_desc_info *desc_info,
708 const struct ofdpa_group_tbl_entry *entry)
710 if (!is_zero_ether_addr(entry->l3_unicast.eth_src) &&
711 rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_SRC_MAC,
712 ETH_ALEN, entry->l3_unicast.eth_src))
714 if (!is_zero_ether_addr(entry->l3_unicast.eth_dst) &&
715 rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC,
716 ETH_ALEN, entry->l3_unicast.eth_dst))
718 if (entry->l3_unicast.vlan_id &&
719 rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID,
720 entry->l3_unicast.vlan_id))
722 if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_TTL_CHECK,
723 entry->l3_unicast.ttl_check))
725 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID_LOWER,
726 entry->l3_unicast.group_id))
732 static int ofdpa_cmd_group_tbl_add(const struct rocker_port *rocker_port,
733 struct rocker_desc_info *desc_info,
736 struct ofdpa_group_tbl_entry *entry = priv;
737 struct rocker_tlv *cmd_info;
740 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE, entry->cmd))
742 cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
746 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID,
750 switch (ROCKER_GROUP_TYPE_GET(entry->group_id)) {
751 case ROCKER_OF_DPA_GROUP_TYPE_L2_INTERFACE:
752 err = ofdpa_cmd_group_tbl_add_l2_interface(desc_info, entry);
754 case ROCKER_OF_DPA_GROUP_TYPE_L2_REWRITE:
755 err = ofdpa_cmd_group_tbl_add_l2_rewrite(desc_info, entry);
757 case ROCKER_OF_DPA_GROUP_TYPE_L2_FLOOD:
758 case ROCKER_OF_DPA_GROUP_TYPE_L2_MCAST:
759 err = ofdpa_cmd_group_tbl_add_group_ids(desc_info, entry);
761 case ROCKER_OF_DPA_GROUP_TYPE_L3_UCAST:
762 err = ofdpa_cmd_group_tbl_add_l3_unicast(desc_info, entry);
772 rocker_tlv_nest_end(desc_info, cmd_info);
777 static int ofdpa_cmd_group_tbl_del(const struct rocker_port *rocker_port,
778 struct rocker_desc_info *desc_info,
781 const struct ofdpa_group_tbl_entry *entry = priv;
782 struct rocker_tlv *cmd_info;
784 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE, entry->cmd))
786 cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
789 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID,
792 rocker_tlv_nest_end(desc_info, cmd_info);
797 /***************************************************
798 * Flow, group, FDB, internal VLAN and neigh tables
799 ***************************************************/
801 static struct ofdpa_flow_tbl_entry *
802 ofdpa_flow_tbl_find(const struct ofdpa *ofdpa,
803 const struct ofdpa_flow_tbl_entry *match)
805 struct ofdpa_flow_tbl_entry *found;
806 size_t key_len = match->key_len ? match->key_len : sizeof(found->key);
808 hash_for_each_possible(ofdpa->flow_tbl, found,
809 entry, match->key_crc32) {
810 if (memcmp(&found->key, &match->key, key_len) == 0)
817 static int ofdpa_flow_tbl_add(struct ofdpa_port *ofdpa_port,
818 struct switchdev_trans *trans, int flags,
819 struct ofdpa_flow_tbl_entry *match)
821 struct ofdpa *ofdpa = ofdpa_port->ofdpa;
822 struct ofdpa_flow_tbl_entry *found;
823 size_t key_len = match->key_len ? match->key_len : sizeof(found->key);
824 unsigned long lock_flags;
826 match->key_crc32 = crc32(~0, &match->key, key_len);
828 spin_lock_irqsave(&ofdpa->flow_tbl_lock, lock_flags);
830 found = ofdpa_flow_tbl_find(ofdpa, match);
833 match->cookie = found->cookie;
834 if (!switchdev_trans_ph_prepare(trans))
835 hash_del(&found->entry);
836 ofdpa_kfree(trans, found);
838 found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_FLOW_MOD;
841 found->cookie = ofdpa->flow_tbl_next_cookie++;
842 found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_FLOW_ADD;
845 if (!switchdev_trans_ph_prepare(trans))
846 hash_add(ofdpa->flow_tbl, &found->entry, found->key_crc32);
848 spin_unlock_irqrestore(&ofdpa->flow_tbl_lock, lock_flags);
850 if (!switchdev_trans_ph_prepare(trans))
851 return rocker_cmd_exec(ofdpa_port->rocker_port,
852 ofdpa_flags_nowait(flags),
853 ofdpa_cmd_flow_tbl_add,
858 static int ofdpa_flow_tbl_del(struct ofdpa_port *ofdpa_port,
859 struct switchdev_trans *trans, int flags,
860 struct ofdpa_flow_tbl_entry *match)
862 struct ofdpa *ofdpa = ofdpa_port->ofdpa;
863 struct ofdpa_flow_tbl_entry *found;
864 size_t key_len = match->key_len ? match->key_len : sizeof(found->key);
865 unsigned long lock_flags;
868 match->key_crc32 = crc32(~0, &match->key, key_len);
870 spin_lock_irqsave(&ofdpa->flow_tbl_lock, lock_flags);
872 found = ofdpa_flow_tbl_find(ofdpa, match);
875 if (!switchdev_trans_ph_prepare(trans))
876 hash_del(&found->entry);
877 found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_FLOW_DEL;
880 spin_unlock_irqrestore(&ofdpa->flow_tbl_lock, lock_flags);
882 ofdpa_kfree(trans, match);
885 if (!switchdev_trans_ph_prepare(trans))
886 err = rocker_cmd_exec(ofdpa_port->rocker_port,
887 ofdpa_flags_nowait(flags),
888 ofdpa_cmd_flow_tbl_del,
890 ofdpa_kfree(trans, found);
896 static int ofdpa_flow_tbl_do(struct ofdpa_port *ofdpa_port,
897 struct switchdev_trans *trans, int flags,
898 struct ofdpa_flow_tbl_entry *entry)
900 if (flags & OFDPA_OP_FLAG_REMOVE)
901 return ofdpa_flow_tbl_del(ofdpa_port, trans, flags, entry);
903 return ofdpa_flow_tbl_add(ofdpa_port, trans, flags, entry);
906 static int ofdpa_flow_tbl_ig_port(struct ofdpa_port *ofdpa_port,
907 struct switchdev_trans *trans, int flags,
908 u32 in_pport, u32 in_pport_mask,
909 enum rocker_of_dpa_table_id goto_tbl)
911 struct ofdpa_flow_tbl_entry *entry;
913 entry = ofdpa_kzalloc(trans, flags, sizeof(*entry));
917 entry->key.priority = OFDPA_PRIORITY_IG_PORT;
918 entry->key.tbl_id = ROCKER_OF_DPA_TABLE_ID_INGRESS_PORT;
919 entry->key.ig_port.in_pport = in_pport;
920 entry->key.ig_port.in_pport_mask = in_pport_mask;
921 entry->key.ig_port.goto_tbl = goto_tbl;
923 return ofdpa_flow_tbl_do(ofdpa_port, trans, flags, entry);
926 static int ofdpa_flow_tbl_vlan(struct ofdpa_port *ofdpa_port,
927 struct switchdev_trans *trans, int flags,
928 u32 in_pport, __be16 vlan_id,
930 enum rocker_of_dpa_table_id goto_tbl,
931 bool untagged, __be16 new_vlan_id)
933 struct ofdpa_flow_tbl_entry *entry;
935 entry = ofdpa_kzalloc(trans, flags, sizeof(*entry));
939 entry->key.priority = OFDPA_PRIORITY_VLAN;
940 entry->key.tbl_id = ROCKER_OF_DPA_TABLE_ID_VLAN;
941 entry->key.vlan.in_pport = in_pport;
942 entry->key.vlan.vlan_id = vlan_id;
943 entry->key.vlan.vlan_id_mask = vlan_id_mask;
944 entry->key.vlan.goto_tbl = goto_tbl;
946 entry->key.vlan.untagged = untagged;
947 entry->key.vlan.new_vlan_id = new_vlan_id;
949 return ofdpa_flow_tbl_do(ofdpa_port, trans, flags, entry);
952 static int ofdpa_flow_tbl_term_mac(struct ofdpa_port *ofdpa_port,
953 struct switchdev_trans *trans,
954 u32 in_pport, u32 in_pport_mask,
955 __be16 eth_type, const u8 *eth_dst,
956 const u8 *eth_dst_mask, __be16 vlan_id,
957 __be16 vlan_id_mask, bool copy_to_cpu,
960 struct ofdpa_flow_tbl_entry *entry;
962 entry = ofdpa_kzalloc(trans, flags, sizeof(*entry));
966 if (is_multicast_ether_addr(eth_dst)) {
967 entry->key.priority = OFDPA_PRIORITY_TERM_MAC_MCAST;
968 entry->key.term_mac.goto_tbl =
969 ROCKER_OF_DPA_TABLE_ID_MULTICAST_ROUTING;
971 entry->key.priority = OFDPA_PRIORITY_TERM_MAC_UCAST;
972 entry->key.term_mac.goto_tbl =
973 ROCKER_OF_DPA_TABLE_ID_UNICAST_ROUTING;
976 entry->key.tbl_id = ROCKER_OF_DPA_TABLE_ID_TERMINATION_MAC;
977 entry->key.term_mac.in_pport = in_pport;
978 entry->key.term_mac.in_pport_mask = in_pport_mask;
979 entry->key.term_mac.eth_type = eth_type;
980 ether_addr_copy(entry->key.term_mac.eth_dst, eth_dst);
981 ether_addr_copy(entry->key.term_mac.eth_dst_mask, eth_dst_mask);
982 entry->key.term_mac.vlan_id = vlan_id;
983 entry->key.term_mac.vlan_id_mask = vlan_id_mask;
984 entry->key.term_mac.copy_to_cpu = copy_to_cpu;
986 return ofdpa_flow_tbl_do(ofdpa_port, trans, flags, entry);
989 static int ofdpa_flow_tbl_bridge(struct ofdpa_port *ofdpa_port,
990 struct switchdev_trans *trans, int flags,
991 const u8 *eth_dst, const u8 *eth_dst_mask,
992 __be16 vlan_id, u32 tunnel_id,
993 enum rocker_of_dpa_table_id goto_tbl,
994 u32 group_id, bool copy_to_cpu)
996 struct ofdpa_flow_tbl_entry *entry;
998 bool vlan_bridging = !!vlan_id;
999 bool dflt = !eth_dst || (eth_dst && eth_dst_mask);
1002 entry = ofdpa_kzalloc(trans, flags, sizeof(*entry));
1006 entry->key.tbl_id = ROCKER_OF_DPA_TABLE_ID_BRIDGING;
1009 entry->key.bridge.has_eth_dst = 1;
1010 ether_addr_copy(entry->key.bridge.eth_dst, eth_dst);
1013 entry->key.bridge.has_eth_dst_mask = 1;
1014 ether_addr_copy(entry->key.bridge.eth_dst_mask, eth_dst_mask);
1015 if (!ether_addr_equal(eth_dst_mask, ff_mac))
1019 priority = OFDPA_PRIORITY_UNKNOWN;
1020 if (vlan_bridging && dflt && wild)
1021 priority = OFDPA_PRIORITY_BRIDGING_VLAN_DFLT_WILD;
1022 else if (vlan_bridging && dflt && !wild)
1023 priority = OFDPA_PRIORITY_BRIDGING_VLAN_DFLT_EXACT;
1024 else if (vlan_bridging && !dflt)
1025 priority = OFDPA_PRIORITY_BRIDGING_VLAN;
1026 else if (!vlan_bridging && dflt && wild)
1027 priority = OFDPA_PRIORITY_BRIDGING_TENANT_DFLT_WILD;
1028 else if (!vlan_bridging && dflt && !wild)
1029 priority = OFDPA_PRIORITY_BRIDGING_TENANT_DFLT_EXACT;
1030 else if (!vlan_bridging && !dflt)
1031 priority = OFDPA_PRIORITY_BRIDGING_TENANT;
1033 entry->key.priority = priority;
1034 entry->key.bridge.vlan_id = vlan_id;
1035 entry->key.bridge.tunnel_id = tunnel_id;
1036 entry->key.bridge.goto_tbl = goto_tbl;
1037 entry->key.bridge.group_id = group_id;
1038 entry->key.bridge.copy_to_cpu = copy_to_cpu;
1040 return ofdpa_flow_tbl_do(ofdpa_port, trans, flags, entry);
1043 static int ofdpa_flow_tbl_ucast4_routing(struct ofdpa_port *ofdpa_port,
1044 struct switchdev_trans *trans,
1045 __be16 eth_type, __be32 dst,
1046 __be32 dst_mask, u32 priority,
1047 enum rocker_of_dpa_table_id goto_tbl,
1048 u32 group_id, struct fib_info *fi,
1051 struct ofdpa_flow_tbl_entry *entry;
1053 entry = ofdpa_kzalloc(trans, flags, sizeof(*entry));
1057 entry->key.tbl_id = ROCKER_OF_DPA_TABLE_ID_UNICAST_ROUTING;
1058 entry->key.priority = priority;
1059 entry->key.ucast_routing.eth_type = eth_type;
1060 entry->key.ucast_routing.dst4 = dst;
1061 entry->key.ucast_routing.dst4_mask = dst_mask;
1062 entry->key.ucast_routing.goto_tbl = goto_tbl;
1063 entry->key.ucast_routing.group_id = group_id;
1064 entry->key_len = offsetof(struct ofdpa_flow_tbl_key,
1065 ucast_routing.group_id);
1068 return ofdpa_flow_tbl_do(ofdpa_port, trans, flags, entry);
1071 static int ofdpa_flow_tbl_acl(struct ofdpa_port *ofdpa_port,
1072 struct switchdev_trans *trans, int flags,
1073 u32 in_pport, u32 in_pport_mask,
1074 const u8 *eth_src, const u8 *eth_src_mask,
1075 const u8 *eth_dst, const u8 *eth_dst_mask,
1076 __be16 eth_type, __be16 vlan_id,
1077 __be16 vlan_id_mask, u8 ip_proto,
1078 u8 ip_proto_mask, u8 ip_tos, u8 ip_tos_mask,
1082 struct ofdpa_flow_tbl_entry *entry;
1084 entry = ofdpa_kzalloc(trans, flags, sizeof(*entry));
1088 priority = OFDPA_PRIORITY_ACL_NORMAL;
1089 if (eth_dst && eth_dst_mask) {
1090 if (ether_addr_equal(eth_dst_mask, mcast_mac))
1091 priority = OFDPA_PRIORITY_ACL_DFLT;
1092 else if (is_link_local_ether_addr(eth_dst))
1093 priority = OFDPA_PRIORITY_ACL_CTRL;
1096 entry->key.priority = priority;
1097 entry->key.tbl_id = ROCKER_OF_DPA_TABLE_ID_ACL_POLICY;
1098 entry->key.acl.in_pport = in_pport;
1099 entry->key.acl.in_pport_mask = in_pport_mask;
1102 ether_addr_copy(entry->key.acl.eth_src, eth_src);
1104 ether_addr_copy(entry->key.acl.eth_src_mask, eth_src_mask);
1106 ether_addr_copy(entry->key.acl.eth_dst, eth_dst);
1108 ether_addr_copy(entry->key.acl.eth_dst_mask, eth_dst_mask);
1110 entry->key.acl.eth_type = eth_type;
1111 entry->key.acl.vlan_id = vlan_id;
1112 entry->key.acl.vlan_id_mask = vlan_id_mask;
1113 entry->key.acl.ip_proto = ip_proto;
1114 entry->key.acl.ip_proto_mask = ip_proto_mask;
1115 entry->key.acl.ip_tos = ip_tos;
1116 entry->key.acl.ip_tos_mask = ip_tos_mask;
1117 entry->key.acl.group_id = group_id;
1119 return ofdpa_flow_tbl_do(ofdpa_port, trans, flags, entry);
1122 static struct ofdpa_group_tbl_entry *
1123 ofdpa_group_tbl_find(const struct ofdpa *ofdpa,
1124 const struct ofdpa_group_tbl_entry *match)
1126 struct ofdpa_group_tbl_entry *found;
1128 hash_for_each_possible(ofdpa->group_tbl, found,
1129 entry, match->group_id) {
1130 if (found->group_id == match->group_id)
1137 static void ofdpa_group_tbl_entry_free(struct switchdev_trans *trans,
1138 struct ofdpa_group_tbl_entry *entry)
1140 switch (ROCKER_GROUP_TYPE_GET(entry->group_id)) {
1141 case ROCKER_OF_DPA_GROUP_TYPE_L2_FLOOD:
1142 case ROCKER_OF_DPA_GROUP_TYPE_L2_MCAST:
1143 ofdpa_kfree(trans, entry->group_ids);
1148 ofdpa_kfree(trans, entry);
1151 static int ofdpa_group_tbl_add(struct ofdpa_port *ofdpa_port,
1152 struct switchdev_trans *trans, int flags,
1153 struct ofdpa_group_tbl_entry *match)
1155 struct ofdpa *ofdpa = ofdpa_port->ofdpa;
1156 struct ofdpa_group_tbl_entry *found;
1157 unsigned long lock_flags;
1159 spin_lock_irqsave(&ofdpa->group_tbl_lock, lock_flags);
1161 found = ofdpa_group_tbl_find(ofdpa, match);
1164 if (!switchdev_trans_ph_prepare(trans))
1165 hash_del(&found->entry);
1166 ofdpa_group_tbl_entry_free(trans, found);
1168 found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_GROUP_MOD;
1171 found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_GROUP_ADD;
1174 if (!switchdev_trans_ph_prepare(trans))
1175 hash_add(ofdpa->group_tbl, &found->entry, found->group_id);
1177 spin_unlock_irqrestore(&ofdpa->group_tbl_lock, lock_flags);
1179 if (!switchdev_trans_ph_prepare(trans))
1180 return rocker_cmd_exec(ofdpa_port->rocker_port,
1181 ofdpa_flags_nowait(flags),
1182 ofdpa_cmd_group_tbl_add,
1187 static int ofdpa_group_tbl_del(struct ofdpa_port *ofdpa_port,
1188 struct switchdev_trans *trans, int flags,
1189 struct ofdpa_group_tbl_entry *match)
1191 struct ofdpa *ofdpa = ofdpa_port->ofdpa;
1192 struct ofdpa_group_tbl_entry *found;
1193 unsigned long lock_flags;
1196 spin_lock_irqsave(&ofdpa->group_tbl_lock, lock_flags);
1198 found = ofdpa_group_tbl_find(ofdpa, match);
1201 if (!switchdev_trans_ph_prepare(trans))
1202 hash_del(&found->entry);
1203 found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_GROUP_DEL;
1206 spin_unlock_irqrestore(&ofdpa->group_tbl_lock, lock_flags);
1208 ofdpa_group_tbl_entry_free(trans, match);
1211 if (!switchdev_trans_ph_prepare(trans))
1212 err = rocker_cmd_exec(ofdpa_port->rocker_port,
1213 ofdpa_flags_nowait(flags),
1214 ofdpa_cmd_group_tbl_del,
1216 ofdpa_group_tbl_entry_free(trans, found);
1222 static int ofdpa_group_tbl_do(struct ofdpa_port *ofdpa_port,
1223 struct switchdev_trans *trans, int flags,
1224 struct ofdpa_group_tbl_entry *entry)
1226 if (flags & OFDPA_OP_FLAG_REMOVE)
1227 return ofdpa_group_tbl_del(ofdpa_port, trans, flags, entry);
1229 return ofdpa_group_tbl_add(ofdpa_port, trans, flags, entry);
1232 static int ofdpa_group_l2_interface(struct ofdpa_port *ofdpa_port,
1233 struct switchdev_trans *trans, int flags,
1234 __be16 vlan_id, u32 out_pport,
1237 struct ofdpa_group_tbl_entry *entry;
1239 entry = ofdpa_kzalloc(trans, flags, sizeof(*entry));
1243 entry->group_id = ROCKER_GROUP_L2_INTERFACE(vlan_id, out_pport);
1244 entry->l2_interface.pop_vlan = pop_vlan;
1246 return ofdpa_group_tbl_do(ofdpa_port, trans, flags, entry);
1249 static int ofdpa_group_l2_fan_out(struct ofdpa_port *ofdpa_port,
1250 struct switchdev_trans *trans,
1251 int flags, u8 group_count,
1252 const u32 *group_ids, u32 group_id)
1254 struct ofdpa_group_tbl_entry *entry;
1256 entry = ofdpa_kzalloc(trans, flags, sizeof(*entry));
1260 entry->group_id = group_id;
1261 entry->group_count = group_count;
1263 entry->group_ids = ofdpa_kcalloc(trans, flags,
1264 group_count, sizeof(u32));
1265 if (!entry->group_ids) {
1266 ofdpa_kfree(trans, entry);
1269 memcpy(entry->group_ids, group_ids, group_count * sizeof(u32));
1271 return ofdpa_group_tbl_do(ofdpa_port, trans, flags, entry);
1274 static int ofdpa_group_l2_flood(struct ofdpa_port *ofdpa_port,
1275 struct switchdev_trans *trans, int flags,
1276 __be16 vlan_id, u8 group_count,
1277 const u32 *group_ids, u32 group_id)
1279 return ofdpa_group_l2_fan_out(ofdpa_port, trans, flags,
1280 group_count, group_ids,
1284 static int ofdpa_group_l3_unicast(struct ofdpa_port *ofdpa_port,
1285 struct switchdev_trans *trans, int flags,
1286 u32 index, const u8 *src_mac, const u8 *dst_mac,
1287 __be16 vlan_id, bool ttl_check, u32 pport)
1289 struct ofdpa_group_tbl_entry *entry;
1291 entry = ofdpa_kzalloc(trans, flags, sizeof(*entry));
1295 entry->group_id = ROCKER_GROUP_L3_UNICAST(index);
1297 ether_addr_copy(entry->l3_unicast.eth_src, src_mac);
1299 ether_addr_copy(entry->l3_unicast.eth_dst, dst_mac);
1300 entry->l3_unicast.vlan_id = vlan_id;
1301 entry->l3_unicast.ttl_check = ttl_check;
1302 entry->l3_unicast.group_id = ROCKER_GROUP_L2_INTERFACE(vlan_id, pport);
1304 return ofdpa_group_tbl_do(ofdpa_port, trans, flags, entry);
1307 static struct ofdpa_neigh_tbl_entry *
1308 ofdpa_neigh_tbl_find(const struct ofdpa *ofdpa, __be32 ip_addr)
1310 struct ofdpa_neigh_tbl_entry *found;
1312 hash_for_each_possible(ofdpa->neigh_tbl, found,
1313 entry, be32_to_cpu(ip_addr))
1314 if (found->ip_addr == ip_addr)
1320 static void ofdpa_neigh_add(struct ofdpa *ofdpa,
1321 struct switchdev_trans *trans,
1322 struct ofdpa_neigh_tbl_entry *entry)
1324 if (!switchdev_trans_ph_commit(trans))
1325 entry->index = ofdpa->neigh_tbl_next_index++;
1326 if (switchdev_trans_ph_prepare(trans))
1329 hash_add(ofdpa->neigh_tbl, &entry->entry,
1330 be32_to_cpu(entry->ip_addr));
1333 static void ofdpa_neigh_del(struct switchdev_trans *trans,
1334 struct ofdpa_neigh_tbl_entry *entry)
1336 if (switchdev_trans_ph_prepare(trans))
1338 if (--entry->ref_count == 0) {
1339 hash_del(&entry->entry);
1340 ofdpa_kfree(trans, entry);
1344 static void ofdpa_neigh_update(struct ofdpa_neigh_tbl_entry *entry,
1345 struct switchdev_trans *trans,
1346 const u8 *eth_dst, bool ttl_check)
1349 ether_addr_copy(entry->eth_dst, eth_dst);
1350 entry->ttl_check = ttl_check;
1351 } else if (!switchdev_trans_ph_prepare(trans)) {
1356 static int ofdpa_port_ipv4_neigh(struct ofdpa_port *ofdpa_port,
1357 struct switchdev_trans *trans,
1358 int flags, __be32 ip_addr, const u8 *eth_dst)
1360 struct ofdpa *ofdpa = ofdpa_port->ofdpa;
1361 struct ofdpa_neigh_tbl_entry *entry;
1362 struct ofdpa_neigh_tbl_entry *found;
1363 unsigned long lock_flags;
1364 __be16 eth_type = htons(ETH_P_IP);
1365 enum rocker_of_dpa_table_id goto_tbl =
1366 ROCKER_OF_DPA_TABLE_ID_ACL_POLICY;
1369 bool adding = !(flags & OFDPA_OP_FLAG_REMOVE);
1374 entry = ofdpa_kzalloc(trans, flags, sizeof(*entry));
1378 spin_lock_irqsave(&ofdpa->neigh_tbl_lock, lock_flags);
1380 found = ofdpa_neigh_tbl_find(ofdpa, ip_addr);
1382 updating = found && adding;
1383 removing = found && !adding;
1384 adding = !found && adding;
1387 entry->ip_addr = ip_addr;
1388 entry->dev = ofdpa_port->dev;
1389 ether_addr_copy(entry->eth_dst, eth_dst);
1390 entry->ttl_check = true;
1391 ofdpa_neigh_add(ofdpa, trans, entry);
1392 } else if (removing) {
1393 memcpy(entry, found, sizeof(*entry));
1394 ofdpa_neigh_del(trans, found);
1395 } else if (updating) {
1396 ofdpa_neigh_update(found, trans, eth_dst, true);
1397 memcpy(entry, found, sizeof(*entry));
1402 spin_unlock_irqrestore(&ofdpa->neigh_tbl_lock, lock_flags);
1407 /* For each active neighbor, we have an L3 unicast group and
1408 * a /32 route to the neighbor, which uses the L3 unicast
1409 * group. The L3 unicast group can also be referred to by
1410 * other routes' nexthops.
1413 err = ofdpa_group_l3_unicast(ofdpa_port, trans, flags,
1415 ofdpa_port->dev->dev_addr,
1417 ofdpa_port->internal_vlan_id,
1421 netdev_err(ofdpa_port->dev, "Error (%d) L3 unicast group index %d\n",
1426 if (adding || removing) {
1427 group_id = ROCKER_GROUP_L3_UNICAST(entry->index);
1428 err = ofdpa_flow_tbl_ucast4_routing(ofdpa_port, trans,
1432 group_id, NULL, flags);
1435 netdev_err(ofdpa_port->dev, "Error (%d) /32 unicast route %pI4 group 0x%08x\n",
1436 err, &entry->ip_addr, group_id);
1441 ofdpa_kfree(trans, entry);
1446 static int ofdpa_port_ipv4_resolve(struct ofdpa_port *ofdpa_port,
1447 struct switchdev_trans *trans,
1450 struct net_device *dev = ofdpa_port->dev;
1451 struct neighbour *n = __ipv4_neigh_lookup(dev, (__force u32)ip_addr);
1455 n = neigh_create(&arp_tbl, &ip_addr, dev);
1460 /* If the neigh is already resolved, then go ahead and
1461 * install the entry, otherwise start the ARP process to
1462 * resolve the neigh.
1465 if (n->nud_state & NUD_VALID)
1466 err = ofdpa_port_ipv4_neigh(ofdpa_port, trans, 0,
1469 neigh_event_send(n, NULL);
1475 static int ofdpa_port_ipv4_nh(struct ofdpa_port *ofdpa_port,
1476 struct switchdev_trans *trans, int flags,
1477 __be32 ip_addr, u32 *index)
1479 struct ofdpa *ofdpa = ofdpa_port->ofdpa;
1480 struct ofdpa_neigh_tbl_entry *entry;
1481 struct ofdpa_neigh_tbl_entry *found;
1482 unsigned long lock_flags;
1483 bool adding = !(flags & OFDPA_OP_FLAG_REMOVE);
1486 bool resolved = true;
1489 entry = ofdpa_kzalloc(trans, flags, sizeof(*entry));
1493 spin_lock_irqsave(&ofdpa->neigh_tbl_lock, lock_flags);
1495 found = ofdpa_neigh_tbl_find(ofdpa, ip_addr);
1497 updating = found && adding;
1498 removing = found && !adding;
1499 adding = !found && adding;
1502 entry->ip_addr = ip_addr;
1503 entry->dev = ofdpa_port->dev;
1504 ofdpa_neigh_add(ofdpa, trans, entry);
1505 *index = entry->index;
1507 } else if (removing) {
1508 *index = found->index;
1509 ofdpa_neigh_del(trans, found);
1510 } else if (updating) {
1511 ofdpa_neigh_update(found, trans, NULL, false);
1512 resolved = !is_zero_ether_addr(found->eth_dst);
1513 *index = found->index;
1518 spin_unlock_irqrestore(&ofdpa->neigh_tbl_lock, lock_flags);
1521 ofdpa_kfree(trans, entry);
1526 /* Resolved means neigh ip_addr is resolved to neigh mac. */
1529 err = ofdpa_port_ipv4_resolve(ofdpa_port, trans, ip_addr);
1534 static struct ofdpa_port *ofdpa_port_get(const struct ofdpa *ofdpa,
1537 struct rocker_port *rocker_port;
1539 rocker_port = ofdpa->rocker->ports[port_index];
1540 return rocker_port ? rocker_port->wpriv : NULL;
1543 static int ofdpa_port_vlan_flood_group(struct ofdpa_port *ofdpa_port,
1544 struct switchdev_trans *trans,
1545 int flags, __be16 vlan_id)
1547 struct ofdpa_port *p;
1548 const struct ofdpa *ofdpa = ofdpa_port->ofdpa;
1549 unsigned int port_count = ofdpa->rocker->port_count;
1550 u32 group_id = ROCKER_GROUP_L2_FLOOD(vlan_id, 0);
1556 group_ids = ofdpa_kcalloc(trans, flags, port_count, sizeof(u32));
1560 /* Adjust the flood group for this VLAN. The flood group
1561 * references an L2 interface group for each port in this
1565 for (i = 0; i < port_count; i++) {
1566 p = ofdpa_port_get(ofdpa, i);
1569 if (!ofdpa_port_is_bridged(p))
1571 if (test_bit(ntohs(vlan_id), p->vlan_bitmap)) {
1572 group_ids[group_count++] =
1573 ROCKER_GROUP_L2_INTERFACE(vlan_id, p->pport);
1577 /* If there are no bridged ports in this VLAN, we're done */
1578 if (group_count == 0)
1579 goto no_ports_in_vlan;
1581 err = ofdpa_group_l2_flood(ofdpa_port, trans, flags, vlan_id,
1582 group_count, group_ids, group_id);
1584 netdev_err(ofdpa_port->dev, "Error (%d) port VLAN l2 flood group\n", err);
1587 ofdpa_kfree(trans, group_ids);
1591 static int ofdpa_port_vlan_l2_groups(struct ofdpa_port *ofdpa_port,
1592 struct switchdev_trans *trans, int flags,
1593 __be16 vlan_id, bool pop_vlan)
1595 const struct ofdpa *ofdpa = ofdpa_port->ofdpa;
1596 unsigned int port_count = ofdpa->rocker->port_count;
1597 struct ofdpa_port *p;
1598 bool adding = !(flags & OFDPA_OP_FLAG_REMOVE);
1604 /* An L2 interface group for this port in this VLAN, but
1605 * only when port STP state is LEARNING|FORWARDING.
1608 if (ofdpa_port->stp_state == BR_STATE_LEARNING ||
1609 ofdpa_port->stp_state == BR_STATE_FORWARDING) {
1610 out_pport = ofdpa_port->pport;
1611 err = ofdpa_group_l2_interface(ofdpa_port, trans, flags,
1612 vlan_id, out_pport, pop_vlan);
1614 netdev_err(ofdpa_port->dev, "Error (%d) port VLAN l2 group for pport %d\n",
1620 /* An L2 interface group for this VLAN to CPU port.
1621 * Add when first port joins this VLAN and destroy when
1622 * last port leaves this VLAN.
1625 for (i = 0; i < port_count; i++) {
1626 p = ofdpa_port_get(ofdpa, i);
1627 if (p && test_bit(ntohs(vlan_id), p->vlan_bitmap))
1631 if ((!adding || ref != 1) && (adding || ref != 0))
1635 err = ofdpa_group_l2_interface(ofdpa_port, trans, flags,
1636 vlan_id, out_pport, pop_vlan);
1638 netdev_err(ofdpa_port->dev, "Error (%d) port VLAN l2 group for CPU port\n", err);
1645 static struct ofdpa_ctrl {
1647 const u8 *eth_dst_mask;
1654 [OFDPA_CTRL_LINK_LOCAL_MCAST] = {
1655 /* pass link local multicast pkts up to CPU for filtering */
1657 .eth_dst_mask = ll_mask,
1660 [OFDPA_CTRL_LOCAL_ARP] = {
1661 /* pass local ARP pkts up to CPU */
1662 .eth_dst = zero_mac,
1663 .eth_dst_mask = zero_mac,
1664 .eth_type = htons(ETH_P_ARP),
1667 [OFDPA_CTRL_IPV4_MCAST] = {
1668 /* pass IPv4 mcast pkts up to CPU, RFC 1112 */
1669 .eth_dst = ipv4_mcast,
1670 .eth_dst_mask = ipv4_mask,
1671 .eth_type = htons(ETH_P_IP),
1673 .copy_to_cpu = true,
1675 [OFDPA_CTRL_IPV6_MCAST] = {
1676 /* pass IPv6 mcast pkts up to CPU, RFC 2464 */
1677 .eth_dst = ipv6_mcast,
1678 .eth_dst_mask = ipv6_mask,
1679 .eth_type = htons(ETH_P_IPV6),
1681 .copy_to_cpu = true,
1683 [OFDPA_CTRL_DFLT_BRIDGING] = {
1684 /* flood any pkts on vlan */
1686 .copy_to_cpu = true,
1688 [OFDPA_CTRL_DFLT_OVS] = {
1689 /* pass all pkts up to CPU */
1690 .eth_dst = zero_mac,
1691 .eth_dst_mask = zero_mac,
1696 static int ofdpa_port_ctrl_vlan_acl(struct ofdpa_port *ofdpa_port,
1697 struct switchdev_trans *trans, int flags,
1698 const struct ofdpa_ctrl *ctrl, __be16 vlan_id)
1700 u32 in_pport = ofdpa_port->pport;
1701 u32 in_pport_mask = 0xffffffff;
1703 const u8 *eth_src = NULL;
1704 const u8 *eth_src_mask = NULL;
1705 __be16 vlan_id_mask = htons(0xffff);
1707 u8 ip_proto_mask = 0;
1710 u32 group_id = ROCKER_GROUP_L2_INTERFACE(vlan_id, out_pport);
1713 err = ofdpa_flow_tbl_acl(ofdpa_port, trans, flags,
1714 in_pport, in_pport_mask,
1715 eth_src, eth_src_mask,
1716 ctrl->eth_dst, ctrl->eth_dst_mask,
1718 vlan_id, vlan_id_mask,
1719 ip_proto, ip_proto_mask,
1720 ip_tos, ip_tos_mask,
1724 netdev_err(ofdpa_port->dev, "Error (%d) ctrl ACL\n", err);
1729 static int ofdpa_port_ctrl_vlan_bridge(struct ofdpa_port *ofdpa_port,
1730 struct switchdev_trans *trans,
1732 const struct ofdpa_ctrl *ctrl,
1735 enum rocker_of_dpa_table_id goto_tbl =
1736 ROCKER_OF_DPA_TABLE_ID_ACL_POLICY;
1737 u32 group_id = ROCKER_GROUP_L2_FLOOD(vlan_id, 0);
1741 if (!ofdpa_port_is_bridged(ofdpa_port))
1744 err = ofdpa_flow_tbl_bridge(ofdpa_port, trans, flags,
1745 ctrl->eth_dst, ctrl->eth_dst_mask,
1747 goto_tbl, group_id, ctrl->copy_to_cpu);
1750 netdev_err(ofdpa_port->dev, "Error (%d) ctrl FLOOD\n", err);
1755 static int ofdpa_port_ctrl_vlan_term(struct ofdpa_port *ofdpa_port,
1756 struct switchdev_trans *trans, int flags,
1757 const struct ofdpa_ctrl *ctrl, __be16 vlan_id)
1759 u32 in_pport_mask = 0xffffffff;
1760 __be16 vlan_id_mask = htons(0xffff);
1763 if (ntohs(vlan_id) == 0)
1764 vlan_id = ofdpa_port->internal_vlan_id;
1766 err = ofdpa_flow_tbl_term_mac(ofdpa_port, trans,
1767 ofdpa_port->pport, in_pport_mask,
1768 ctrl->eth_type, ctrl->eth_dst,
1769 ctrl->eth_dst_mask, vlan_id,
1770 vlan_id_mask, ctrl->copy_to_cpu,
1774 netdev_err(ofdpa_port->dev, "Error (%d) ctrl term\n", err);
1779 static int ofdpa_port_ctrl_vlan(struct ofdpa_port *ofdpa_port,
1780 struct switchdev_trans *trans, int flags,
1781 const struct ofdpa_ctrl *ctrl, __be16 vlan_id)
1784 return ofdpa_port_ctrl_vlan_acl(ofdpa_port, trans, flags,
1787 return ofdpa_port_ctrl_vlan_bridge(ofdpa_port, trans, flags,
1791 return ofdpa_port_ctrl_vlan_term(ofdpa_port, trans, flags,
1797 static int ofdpa_port_ctrl_vlan_add(struct ofdpa_port *ofdpa_port,
1798 struct switchdev_trans *trans, int flags,
1804 for (i = 0; i < OFDPA_CTRL_MAX; i++) {
1805 if (ofdpa_port->ctrls[i]) {
1806 err = ofdpa_port_ctrl_vlan(ofdpa_port, trans, flags,
1807 &ofdpa_ctrls[i], vlan_id);
1816 static int ofdpa_port_ctrl(struct ofdpa_port *ofdpa_port,
1817 struct switchdev_trans *trans, int flags,
1818 const struct ofdpa_ctrl *ctrl)
1823 for (vid = 1; vid < VLAN_N_VID; vid++) {
1824 if (!test_bit(vid, ofdpa_port->vlan_bitmap))
1826 err = ofdpa_port_ctrl_vlan(ofdpa_port, trans, flags,
1835 static int ofdpa_port_vlan(struct ofdpa_port *ofdpa_port,
1836 struct switchdev_trans *trans, int flags, u16 vid)
1838 enum rocker_of_dpa_table_id goto_tbl =
1839 ROCKER_OF_DPA_TABLE_ID_TERMINATION_MAC;
1840 u32 in_pport = ofdpa_port->pport;
1841 __be16 vlan_id = htons(vid);
1842 __be16 vlan_id_mask = htons(0xffff);
1843 __be16 internal_vlan_id;
1845 bool adding = !(flags & OFDPA_OP_FLAG_REMOVE);
1848 internal_vlan_id = ofdpa_port_vid_to_vlan(ofdpa_port, vid, &untagged);
1851 test_bit(ntohs(internal_vlan_id), ofdpa_port->vlan_bitmap))
1852 return 0; /* already added */
1854 !test_bit(ntohs(internal_vlan_id), ofdpa_port->vlan_bitmap))
1855 return 0; /* already removed */
1857 change_bit(ntohs(internal_vlan_id), ofdpa_port->vlan_bitmap);
1860 err = ofdpa_port_ctrl_vlan_add(ofdpa_port, trans, flags,
1863 netdev_err(ofdpa_port->dev, "Error (%d) port ctrl vlan add\n", err);
1868 err = ofdpa_port_vlan_l2_groups(ofdpa_port, trans, flags,
1869 internal_vlan_id, untagged);
1871 netdev_err(ofdpa_port->dev, "Error (%d) port VLAN l2 groups\n", err);
1875 err = ofdpa_port_vlan_flood_group(ofdpa_port, trans, flags,
1878 netdev_err(ofdpa_port->dev, "Error (%d) port VLAN l2 flood group\n", err);
1882 err = ofdpa_flow_tbl_vlan(ofdpa_port, trans, flags,
1883 in_pport, vlan_id, vlan_id_mask,
1884 goto_tbl, untagged, internal_vlan_id);
1886 netdev_err(ofdpa_port->dev, "Error (%d) port VLAN table\n", err);
1889 if (switchdev_trans_ph_prepare(trans))
1890 change_bit(ntohs(internal_vlan_id), ofdpa_port->vlan_bitmap);
1895 static int ofdpa_port_ig_tbl(struct ofdpa_port *ofdpa_port,
1896 struct switchdev_trans *trans, int flags)
1898 enum rocker_of_dpa_table_id goto_tbl;
1903 /* Normal Ethernet Frames. Matches pkts from any local physical
1904 * ports. Goto VLAN tbl.
1908 in_pport_mask = 0xffff0000;
1909 goto_tbl = ROCKER_OF_DPA_TABLE_ID_VLAN;
1911 err = ofdpa_flow_tbl_ig_port(ofdpa_port, trans, flags,
1912 in_pport, in_pport_mask,
1915 netdev_err(ofdpa_port->dev, "Error (%d) ingress port table entry\n", err);
1920 struct ofdpa_fdb_learn_work {
1921 struct work_struct work;
1922 struct ofdpa_port *ofdpa_port;
1923 struct switchdev_trans *trans;
1929 static void ofdpa_port_fdb_learn_work(struct work_struct *work)
1931 const struct ofdpa_fdb_learn_work *lw =
1932 container_of(work, struct ofdpa_fdb_learn_work, work);
1933 bool removing = (lw->flags & OFDPA_OP_FLAG_REMOVE);
1934 bool learned = (lw->flags & OFDPA_OP_FLAG_LEARNED);
1935 struct switchdev_notifier_fdb_info info;
1937 info.addr = lw->addr;
1941 if (learned && removing)
1942 call_switchdev_notifiers(SWITCHDEV_FDB_DEL,
1943 lw->ofdpa_port->dev, &info.info);
1944 else if (learned && !removing)
1945 call_switchdev_notifiers(SWITCHDEV_FDB_ADD,
1946 lw->ofdpa_port->dev, &info.info);
1949 ofdpa_kfree(lw->trans, work);
1952 static int ofdpa_port_fdb_learn(struct ofdpa_port *ofdpa_port,
1953 struct switchdev_trans *trans, int flags,
1954 const u8 *addr, __be16 vlan_id)
1956 struct ofdpa_fdb_learn_work *lw;
1957 enum rocker_of_dpa_table_id goto_tbl =
1958 ROCKER_OF_DPA_TABLE_ID_ACL_POLICY;
1959 u32 out_pport = ofdpa_port->pport;
1961 u32 group_id = ROCKER_GROUP_NONE;
1962 bool syncing = !!(ofdpa_port->brport_flags & BR_LEARNING_SYNC);
1963 bool copy_to_cpu = false;
1966 if (ofdpa_port_is_bridged(ofdpa_port))
1967 group_id = ROCKER_GROUP_L2_INTERFACE(vlan_id, out_pport);
1969 if (!(flags & OFDPA_OP_FLAG_REFRESH)) {
1970 err = ofdpa_flow_tbl_bridge(ofdpa_port, trans, flags, addr,
1971 NULL, vlan_id, tunnel_id, goto_tbl,
1972 group_id, copy_to_cpu);
1980 if (!ofdpa_port_is_bridged(ofdpa_port))
1983 lw = ofdpa_kzalloc(trans, flags, sizeof(*lw));
1987 INIT_WORK(&lw->work, ofdpa_port_fdb_learn_work);
1989 lw->ofdpa_port = ofdpa_port;
1992 ether_addr_copy(lw->addr, addr);
1993 lw->vid = ofdpa_port_vlan_to_vid(ofdpa_port, vlan_id);
1995 if (switchdev_trans_ph_prepare(trans))
1996 ofdpa_kfree(trans, lw);
1998 schedule_work(&lw->work);
2003 static struct ofdpa_fdb_tbl_entry *
2004 ofdpa_fdb_tbl_find(const struct ofdpa *ofdpa,
2005 const struct ofdpa_fdb_tbl_entry *match)
2007 struct ofdpa_fdb_tbl_entry *found;
2009 hash_for_each_possible(ofdpa->fdb_tbl, found, entry, match->key_crc32)
2010 if (memcmp(&found->key, &match->key, sizeof(found->key)) == 0)
2016 static int ofdpa_port_fdb(struct ofdpa_port *ofdpa_port,
2017 struct switchdev_trans *trans,
2018 const unsigned char *addr,
2019 __be16 vlan_id, int flags)
2021 struct ofdpa *ofdpa = ofdpa_port->ofdpa;
2022 struct ofdpa_fdb_tbl_entry *fdb;
2023 struct ofdpa_fdb_tbl_entry *found;
2024 bool removing = (flags & OFDPA_OP_FLAG_REMOVE);
2025 unsigned long lock_flags;
2027 fdb = ofdpa_kzalloc(trans, flags, sizeof(*fdb));
2031 fdb->learned = (flags & OFDPA_OP_FLAG_LEARNED);
2032 fdb->touched = jiffies;
2033 fdb->key.ofdpa_port = ofdpa_port;
2034 ether_addr_copy(fdb->key.addr, addr);
2035 fdb->key.vlan_id = vlan_id;
2036 fdb->key_crc32 = crc32(~0, &fdb->key, sizeof(fdb->key));
2038 spin_lock_irqsave(&ofdpa->fdb_tbl_lock, lock_flags);
2040 found = ofdpa_fdb_tbl_find(ofdpa, fdb);
2043 found->touched = jiffies;
2045 ofdpa_kfree(trans, fdb);
2046 if (!switchdev_trans_ph_prepare(trans))
2047 hash_del(&found->entry);
2049 } else if (!removing) {
2050 if (!switchdev_trans_ph_prepare(trans))
2051 hash_add(ofdpa->fdb_tbl, &fdb->entry,
2055 spin_unlock_irqrestore(&ofdpa->fdb_tbl_lock, lock_flags);
2057 /* Check if adding and already exists, or removing and can't find */
2058 if (!found != !removing) {
2059 ofdpa_kfree(trans, fdb);
2060 if (!found && removing)
2062 /* Refreshing existing to update aging timers */
2063 flags |= OFDPA_OP_FLAG_REFRESH;
2066 return ofdpa_port_fdb_learn(ofdpa_port, trans, flags, addr, vlan_id);
2069 static int ofdpa_port_fdb_flush(struct ofdpa_port *ofdpa_port,
2070 struct switchdev_trans *trans, int flags)
2072 struct ofdpa *ofdpa = ofdpa_port->ofdpa;
2073 struct ofdpa_fdb_tbl_entry *found;
2074 unsigned long lock_flags;
2075 struct hlist_node *tmp;
2079 if (ofdpa_port->stp_state == BR_STATE_LEARNING ||
2080 ofdpa_port->stp_state == BR_STATE_FORWARDING)
2083 flags |= OFDPA_OP_FLAG_NOWAIT | OFDPA_OP_FLAG_REMOVE;
2085 spin_lock_irqsave(&ofdpa->fdb_tbl_lock, lock_flags);
2087 hash_for_each_safe(ofdpa->fdb_tbl, bkt, tmp, found, entry) {
2088 if (found->key.ofdpa_port != ofdpa_port)
2090 if (!found->learned)
2092 err = ofdpa_port_fdb_learn(ofdpa_port, trans, flags,
2094 found->key.vlan_id);
2097 if (!switchdev_trans_ph_prepare(trans))
2098 hash_del(&found->entry);
2102 spin_unlock_irqrestore(&ofdpa->fdb_tbl_lock, lock_flags);
2107 static void ofdpa_fdb_cleanup(unsigned long data)
2109 struct ofdpa *ofdpa = (struct ofdpa *)data;
2110 struct ofdpa_port *ofdpa_port;
2111 struct ofdpa_fdb_tbl_entry *entry;
2112 struct hlist_node *tmp;
2113 unsigned long next_timer = jiffies + ofdpa->ageing_time;
2114 unsigned long expires;
2115 unsigned long lock_flags;
2116 int flags = OFDPA_OP_FLAG_NOWAIT | OFDPA_OP_FLAG_REMOVE |
2117 OFDPA_OP_FLAG_LEARNED;
2120 spin_lock_irqsave(&ofdpa->fdb_tbl_lock, lock_flags);
2122 hash_for_each_safe(ofdpa->fdb_tbl, bkt, tmp, entry, entry) {
2123 if (!entry->learned)
2125 ofdpa_port = entry->key.ofdpa_port;
2126 expires = entry->touched + ofdpa_port->ageing_time;
2127 if (time_before_eq(expires, jiffies)) {
2128 ofdpa_port_fdb_learn(ofdpa_port, NULL,
2129 flags, entry->key.addr,
2130 entry->key.vlan_id);
2131 hash_del(&entry->entry);
2132 } else if (time_before(expires, next_timer)) {
2133 next_timer = expires;
2137 spin_unlock_irqrestore(&ofdpa->fdb_tbl_lock, lock_flags);
2139 mod_timer(&ofdpa->fdb_cleanup_timer, round_jiffies_up(next_timer));
2142 static int ofdpa_port_router_mac(struct ofdpa_port *ofdpa_port,
2143 struct switchdev_trans *trans, int flags,
2146 u32 in_pport_mask = 0xffffffff;
2148 const u8 *dst_mac_mask = ff_mac;
2149 __be16 vlan_id_mask = htons(0xffff);
2150 bool copy_to_cpu = false;
2153 if (ntohs(vlan_id) == 0)
2154 vlan_id = ofdpa_port->internal_vlan_id;
2156 eth_type = htons(ETH_P_IP);
2157 err = ofdpa_flow_tbl_term_mac(ofdpa_port, trans,
2158 ofdpa_port->pport, in_pport_mask,
2159 eth_type, ofdpa_port->dev->dev_addr,
2160 dst_mac_mask, vlan_id, vlan_id_mask,
2161 copy_to_cpu, flags);
2165 eth_type = htons(ETH_P_IPV6);
2166 err = ofdpa_flow_tbl_term_mac(ofdpa_port, trans,
2167 ofdpa_port->pport, in_pport_mask,
2168 eth_type, ofdpa_port->dev->dev_addr,
2169 dst_mac_mask, vlan_id, vlan_id_mask,
2170 copy_to_cpu, flags);
2175 static int ofdpa_port_fwding(struct ofdpa_port *ofdpa_port,
2176 struct switchdev_trans *trans, int flags)
2184 /* Port will be forwarding-enabled if its STP state is LEARNING
2185 * or FORWARDING. Traffic from CPU can still egress, regardless of
2186 * port STP state. Use L2 interface group on port VLANs as a way
2187 * to toggle port forwarding: if forwarding is disabled, L2
2188 * interface group will not exist.
2191 if (ofdpa_port->stp_state != BR_STATE_LEARNING &&
2192 ofdpa_port->stp_state != BR_STATE_FORWARDING)
2193 flags |= OFDPA_OP_FLAG_REMOVE;
2195 out_pport = ofdpa_port->pport;
2196 for (vid = 1; vid < VLAN_N_VID; vid++) {
2197 if (!test_bit(vid, ofdpa_port->vlan_bitmap))
2199 vlan_id = htons(vid);
2200 pop_vlan = ofdpa_vlan_id_is_internal(vlan_id);
2201 err = ofdpa_group_l2_interface(ofdpa_port, trans, flags,
2202 vlan_id, out_pport, pop_vlan);
2204 netdev_err(ofdpa_port->dev, "Error (%d) port VLAN l2 group for pport %d\n",
2213 static int ofdpa_port_stp_update(struct ofdpa_port *ofdpa_port,
2214 struct switchdev_trans *trans,
2215 int flags, u8 state)
2217 bool want[OFDPA_CTRL_MAX] = { 0, };
2218 bool prev_ctrls[OFDPA_CTRL_MAX];
2219 u8 uninitialized_var(prev_state);
2223 if (switchdev_trans_ph_prepare(trans)) {
2224 memcpy(prev_ctrls, ofdpa_port->ctrls, sizeof(prev_ctrls));
2225 prev_state = ofdpa_port->stp_state;
2228 if (ofdpa_port->stp_state == state)
2231 ofdpa_port->stp_state = state;
2234 case BR_STATE_DISABLED:
2235 /* port is completely disabled */
2237 case BR_STATE_LISTENING:
2238 case BR_STATE_BLOCKING:
2239 want[OFDPA_CTRL_LINK_LOCAL_MCAST] = true;
2241 case BR_STATE_LEARNING:
2242 case BR_STATE_FORWARDING:
2243 if (!ofdpa_port_is_ovsed(ofdpa_port))
2244 want[OFDPA_CTRL_LINK_LOCAL_MCAST] = true;
2245 want[OFDPA_CTRL_IPV4_MCAST] = true;
2246 want[OFDPA_CTRL_IPV6_MCAST] = true;
2247 if (ofdpa_port_is_bridged(ofdpa_port))
2248 want[OFDPA_CTRL_DFLT_BRIDGING] = true;
2249 else if (ofdpa_port_is_ovsed(ofdpa_port))
2250 want[OFDPA_CTRL_DFLT_OVS] = true;
2252 want[OFDPA_CTRL_LOCAL_ARP] = true;
2256 for (i = 0; i < OFDPA_CTRL_MAX; i++) {
2257 if (want[i] != ofdpa_port->ctrls[i]) {
2258 int ctrl_flags = flags |
2259 (want[i] ? 0 : OFDPA_OP_FLAG_REMOVE);
2260 err = ofdpa_port_ctrl(ofdpa_port, trans, ctrl_flags,
2264 ofdpa_port->ctrls[i] = want[i];
2268 err = ofdpa_port_fdb_flush(ofdpa_port, trans, flags);
2272 err = ofdpa_port_fwding(ofdpa_port, trans, flags);
2275 if (switchdev_trans_ph_prepare(trans)) {
2276 memcpy(ofdpa_port->ctrls, prev_ctrls, sizeof(prev_ctrls));
2277 ofdpa_port->stp_state = prev_state;
2283 static int ofdpa_port_fwd_enable(struct ofdpa_port *ofdpa_port, int flags)
2285 if (ofdpa_port_is_bridged(ofdpa_port))
2286 /* bridge STP will enable port */
2289 /* port is not bridged, so simulate going to FORWARDING state */
2290 return ofdpa_port_stp_update(ofdpa_port, NULL, flags,
2291 BR_STATE_FORWARDING);
2294 static int ofdpa_port_fwd_disable(struct ofdpa_port *ofdpa_port, int flags)
2296 if (ofdpa_port_is_bridged(ofdpa_port))
2297 /* bridge STP will disable port */
2300 /* port is not bridged, so simulate going to DISABLED state */
2301 return ofdpa_port_stp_update(ofdpa_port, NULL, flags,
2305 static int ofdpa_port_vlan_add(struct ofdpa_port *ofdpa_port,
2306 struct switchdev_trans *trans,
2311 /* XXX deal with flags for PVID and untagged */
2313 err = ofdpa_port_vlan(ofdpa_port, trans, 0, vid);
2317 err = ofdpa_port_router_mac(ofdpa_port, trans, 0, htons(vid));
2319 ofdpa_port_vlan(ofdpa_port, trans,
2320 OFDPA_OP_FLAG_REMOVE, vid);
2325 static int ofdpa_port_vlan_del(struct ofdpa_port *ofdpa_port,
2330 err = ofdpa_port_router_mac(ofdpa_port, NULL,
2331 OFDPA_OP_FLAG_REMOVE, htons(vid));
2335 return ofdpa_port_vlan(ofdpa_port, NULL,
2336 OFDPA_OP_FLAG_REMOVE, vid);
2339 static struct ofdpa_internal_vlan_tbl_entry *
2340 ofdpa_internal_vlan_tbl_find(const struct ofdpa *ofdpa, int ifindex)
2342 struct ofdpa_internal_vlan_tbl_entry *found;
2344 hash_for_each_possible(ofdpa->internal_vlan_tbl, found,
2346 if (found->ifindex == ifindex)
2353 static __be16 ofdpa_port_internal_vlan_id_get(struct ofdpa_port *ofdpa_port,
2356 struct ofdpa *ofdpa = ofdpa_port->ofdpa;
2357 struct ofdpa_internal_vlan_tbl_entry *entry;
2358 struct ofdpa_internal_vlan_tbl_entry *found;
2359 unsigned long lock_flags;
2362 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
2366 entry->ifindex = ifindex;
2368 spin_lock_irqsave(&ofdpa->internal_vlan_tbl_lock, lock_flags);
2370 found = ofdpa_internal_vlan_tbl_find(ofdpa, ifindex);
2377 hash_add(ofdpa->internal_vlan_tbl, &found->entry, found->ifindex);
2379 for (i = 0; i < OFDPA_N_INTERNAL_VLANS; i++) {
2380 if (test_and_set_bit(i, ofdpa->internal_vlan_bitmap))
2382 found->vlan_id = htons(OFDPA_INTERNAL_VLAN_ID_BASE + i);
2386 netdev_err(ofdpa_port->dev, "Out of internal VLAN IDs\n");
2390 spin_unlock_irqrestore(&ofdpa->internal_vlan_tbl_lock, lock_flags);
2392 return found->vlan_id;
2395 static int ofdpa_port_fib_ipv4(struct ofdpa_port *ofdpa_port,
2396 struct switchdev_trans *trans, __be32 dst,
2397 int dst_len, struct fib_info *fi,
2398 u32 tb_id, int flags)
2400 const struct fib_nh *nh;
2401 __be16 eth_type = htons(ETH_P_IP);
2402 __be32 dst_mask = inet_make_mask(dst_len);
2403 __be16 internal_vlan_id = ofdpa_port->internal_vlan_id;
2404 u32 priority = fi->fib_priority;
2405 enum rocker_of_dpa_table_id goto_tbl =
2406 ROCKER_OF_DPA_TABLE_ID_ACL_POLICY;
2413 /* XXX support ECMP */
2416 nh_on_port = (fi->fib_dev == ofdpa_port->dev);
2417 has_gw = !!nh->nh_gw;
2419 if (has_gw && nh_on_port) {
2420 err = ofdpa_port_ipv4_nh(ofdpa_port, trans, flags,
2425 group_id = ROCKER_GROUP_L3_UNICAST(index);
2427 /* Send to CPU for processing */
2428 group_id = ROCKER_GROUP_L2_INTERFACE(internal_vlan_id, 0);
2431 err = ofdpa_flow_tbl_ucast4_routing(ofdpa_port, trans, eth_type, dst,
2432 dst_mask, priority, goto_tbl,
2433 group_id, fi, flags);
2435 netdev_err(ofdpa_port->dev, "Error (%d) IPv4 route %pI4\n",
2442 ofdpa_port_internal_vlan_id_put(const struct ofdpa_port *ofdpa_port,
2445 struct ofdpa *ofdpa = ofdpa_port->ofdpa;
2446 struct ofdpa_internal_vlan_tbl_entry *found;
2447 unsigned long lock_flags;
2450 spin_lock_irqsave(&ofdpa->internal_vlan_tbl_lock, lock_flags);
2452 found = ofdpa_internal_vlan_tbl_find(ofdpa, ifindex);
2454 netdev_err(ofdpa_port->dev,
2455 "ifindex (%d) not found in internal VLAN tbl\n",
2460 if (--found->ref_count <= 0) {
2461 bit = ntohs(found->vlan_id) - OFDPA_INTERNAL_VLAN_ID_BASE;
2462 clear_bit(bit, ofdpa->internal_vlan_bitmap);
2463 hash_del(&found->entry);
2468 spin_unlock_irqrestore(&ofdpa->internal_vlan_tbl_lock, lock_flags);
2471 /**********************************
2472 * Rocker world ops implementation
2473 **********************************/
2475 static int ofdpa_init(struct rocker *rocker)
2477 struct ofdpa *ofdpa = rocker->wpriv;
2479 ofdpa->rocker = rocker;
2481 hash_init(ofdpa->flow_tbl);
2482 spin_lock_init(&ofdpa->flow_tbl_lock);
2484 hash_init(ofdpa->group_tbl);
2485 spin_lock_init(&ofdpa->group_tbl_lock);
2487 hash_init(ofdpa->fdb_tbl);
2488 spin_lock_init(&ofdpa->fdb_tbl_lock);
2490 hash_init(ofdpa->internal_vlan_tbl);
2491 spin_lock_init(&ofdpa->internal_vlan_tbl_lock);
2493 hash_init(ofdpa->neigh_tbl);
2494 spin_lock_init(&ofdpa->neigh_tbl_lock);
2496 setup_timer(&ofdpa->fdb_cleanup_timer, ofdpa_fdb_cleanup,
2497 (unsigned long) ofdpa);
2498 mod_timer(&ofdpa->fdb_cleanup_timer, jiffies);
2500 ofdpa->ageing_time = BR_DEFAULT_AGEING_TIME;
2505 static void ofdpa_fini(struct rocker *rocker)
2507 struct ofdpa *ofdpa = rocker->wpriv;
2509 unsigned long flags;
2510 struct ofdpa_flow_tbl_entry *flow_entry;
2511 struct ofdpa_group_tbl_entry *group_entry;
2512 struct ofdpa_fdb_tbl_entry *fdb_entry;
2513 struct ofdpa_internal_vlan_tbl_entry *internal_vlan_entry;
2514 struct ofdpa_neigh_tbl_entry *neigh_entry;
2515 struct hlist_node *tmp;
2518 del_timer_sync(&ofdpa->fdb_cleanup_timer);
2520 spin_lock_irqsave(&ofdpa->flow_tbl_lock, flags);
2521 hash_for_each_safe(ofdpa->flow_tbl, bkt, tmp, flow_entry, entry)
2522 hash_del(&flow_entry->entry);
2523 spin_unlock_irqrestore(&ofdpa->flow_tbl_lock, flags);
2525 spin_lock_irqsave(&ofdpa->group_tbl_lock, flags);
2526 hash_for_each_safe(ofdpa->group_tbl, bkt, tmp, group_entry, entry)
2527 hash_del(&group_entry->entry);
2528 spin_unlock_irqrestore(&ofdpa->group_tbl_lock, flags);
2530 spin_lock_irqsave(&ofdpa->fdb_tbl_lock, flags);
2531 hash_for_each_safe(ofdpa->fdb_tbl, bkt, tmp, fdb_entry, entry)
2532 hash_del(&fdb_entry->entry);
2533 spin_unlock_irqrestore(&ofdpa->fdb_tbl_lock, flags);
2535 spin_lock_irqsave(&ofdpa->internal_vlan_tbl_lock, flags);
2536 hash_for_each_safe(ofdpa->internal_vlan_tbl, bkt,
2537 tmp, internal_vlan_entry, entry)
2538 hash_del(&internal_vlan_entry->entry);
2539 spin_unlock_irqrestore(&ofdpa->internal_vlan_tbl_lock, flags);
2541 spin_lock_irqsave(&ofdpa->neigh_tbl_lock, flags);
2542 hash_for_each_safe(ofdpa->neigh_tbl, bkt, tmp, neigh_entry, entry)
2543 hash_del(&neigh_entry->entry);
2544 spin_unlock_irqrestore(&ofdpa->neigh_tbl_lock, flags);
2547 static int ofdpa_port_pre_init(struct rocker_port *rocker_port)
2549 struct ofdpa_port *ofdpa_port = rocker_port->wpriv;
2551 ofdpa_port->ofdpa = rocker_port->rocker->wpriv;
2552 ofdpa_port->rocker_port = rocker_port;
2553 ofdpa_port->dev = rocker_port->dev;
2554 ofdpa_port->pport = rocker_port->pport;
2555 ofdpa_port->brport_flags = BR_LEARNING | BR_LEARNING_SYNC;
2556 ofdpa_port->ageing_time = BR_DEFAULT_AGEING_TIME;
2560 static int ofdpa_port_init(struct rocker_port *rocker_port)
2562 struct ofdpa_port *ofdpa_port = rocker_port->wpriv;
2565 rocker_port_set_learning(rocker_port,
2566 !!(ofdpa_port->brport_flags & BR_LEARNING));
2568 err = ofdpa_port_ig_tbl(ofdpa_port, NULL, 0);
2570 netdev_err(ofdpa_port->dev, "install ig port table failed\n");
2574 ofdpa_port->internal_vlan_id =
2575 ofdpa_port_internal_vlan_id_get(ofdpa_port,
2576 ofdpa_port->dev->ifindex);
2578 err = ofdpa_port_vlan_add(ofdpa_port, NULL, OFDPA_UNTAGGED_VID, 0);
2580 netdev_err(ofdpa_port->dev, "install untagged VLAN failed\n");
2581 goto err_untagged_vlan;
2586 ofdpa_port_ig_tbl(ofdpa_port, NULL, OFDPA_OP_FLAG_REMOVE);
2590 static void ofdpa_port_fini(struct rocker_port *rocker_port)
2592 struct ofdpa_port *ofdpa_port = rocker_port->wpriv;
2594 ofdpa_port_ig_tbl(ofdpa_port, NULL, OFDPA_OP_FLAG_REMOVE);
2597 static int ofdpa_port_open(struct rocker_port *rocker_port)
2599 struct ofdpa_port *ofdpa_port = rocker_port->wpriv;
2601 return ofdpa_port_fwd_enable(ofdpa_port, 0);
2604 static void ofdpa_port_stop(struct rocker_port *rocker_port)
2606 struct ofdpa_port *ofdpa_port = rocker_port->wpriv;
2608 ofdpa_port_fwd_disable(ofdpa_port, OFDPA_OP_FLAG_NOWAIT);
2611 static int ofdpa_port_attr_stp_state_set(struct rocker_port *rocker_port,
2613 struct switchdev_trans *trans)
2615 struct ofdpa_port *ofdpa_port = rocker_port->wpriv;
2617 return ofdpa_port_stp_update(ofdpa_port, trans, 0, state);
2620 static int ofdpa_port_attr_bridge_flags_set(struct rocker_port *rocker_port,
2621 unsigned long brport_flags,
2622 struct switchdev_trans *trans)
2624 struct ofdpa_port *ofdpa_port = rocker_port->wpriv;
2625 unsigned long orig_flags;
2628 orig_flags = ofdpa_port->brport_flags;
2629 ofdpa_port->brport_flags = brport_flags;
2630 if ((orig_flags ^ ofdpa_port->brport_flags) & BR_LEARNING &&
2631 !switchdev_trans_ph_prepare(trans))
2632 err = rocker_port_set_learning(ofdpa_port->rocker_port,
2633 !!(ofdpa_port->brport_flags & BR_LEARNING));
2635 if (switchdev_trans_ph_prepare(trans))
2636 ofdpa_port->brport_flags = orig_flags;
2642 ofdpa_port_attr_bridge_flags_get(const struct rocker_port *rocker_port,
2643 unsigned long *p_brport_flags)
2645 const struct ofdpa_port *ofdpa_port = rocker_port->wpriv;
2647 *p_brport_flags = ofdpa_port->brport_flags;
2652 ofdpa_port_attr_bridge_ageing_time_set(struct rocker_port *rocker_port,
2654 struct switchdev_trans *trans)
2656 struct ofdpa_port *ofdpa_port = rocker_port->wpriv;
2657 struct ofdpa *ofdpa = ofdpa_port->ofdpa;
2659 if (!switchdev_trans_ph_prepare(trans)) {
2660 ofdpa_port->ageing_time = clock_t_to_jiffies(ageing_time);
2661 if (ofdpa_port->ageing_time < ofdpa->ageing_time)
2662 ofdpa->ageing_time = ofdpa_port->ageing_time;
2663 mod_timer(&ofdpa_port->ofdpa->fdb_cleanup_timer, jiffies);
2669 static int ofdpa_port_obj_vlan_add(struct rocker_port *rocker_port,
2670 const struct switchdev_obj_port_vlan *vlan,
2671 struct switchdev_trans *trans)
2673 struct ofdpa_port *ofdpa_port = rocker_port->wpriv;
2677 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
2678 err = ofdpa_port_vlan_add(ofdpa_port, trans, vid, vlan->flags);
2686 static int ofdpa_port_obj_vlan_del(struct rocker_port *rocker_port,
2687 const struct switchdev_obj_port_vlan *vlan)
2689 struct ofdpa_port *ofdpa_port = rocker_port->wpriv;
2693 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
2694 err = ofdpa_port_vlan_del(ofdpa_port, vid, vlan->flags);
2702 static int ofdpa_port_obj_vlan_dump(const struct rocker_port *rocker_port,
2703 struct switchdev_obj_port_vlan *vlan,
2704 switchdev_obj_dump_cb_t *cb)
2706 const struct ofdpa_port *ofdpa_port = rocker_port->wpriv;
2710 for (vid = 1; vid < VLAN_N_VID; vid++) {
2711 if (!test_bit(vid, ofdpa_port->vlan_bitmap))
2714 if (ofdpa_vlan_id_is_internal(htons(vid)))
2715 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
2716 vlan->vid_begin = vlan->vid_end = vid;
2717 err = cb(&vlan->obj);
2725 static int ofdpa_port_obj_fdb_add(struct rocker_port *rocker_port,
2726 const struct switchdev_obj_port_fdb *fdb,
2727 struct switchdev_trans *trans)
2729 struct ofdpa_port *ofdpa_port = rocker_port->wpriv;
2730 __be16 vlan_id = ofdpa_port_vid_to_vlan(ofdpa_port, fdb->vid, NULL);
2732 if (!ofdpa_port_is_bridged(ofdpa_port))
2735 return ofdpa_port_fdb(ofdpa_port, trans, fdb->addr, vlan_id, 0);
2738 static int ofdpa_port_obj_fdb_del(struct rocker_port *rocker_port,
2739 const struct switchdev_obj_port_fdb *fdb)
2741 struct ofdpa_port *ofdpa_port = rocker_port->wpriv;
2742 __be16 vlan_id = ofdpa_port_vid_to_vlan(ofdpa_port, fdb->vid, NULL);
2743 int flags = OFDPA_OP_FLAG_REMOVE;
2745 if (!ofdpa_port_is_bridged(ofdpa_port))
2748 return ofdpa_port_fdb(ofdpa_port, NULL, fdb->addr, vlan_id, flags);
2751 static int ofdpa_port_obj_fdb_dump(const struct rocker_port *rocker_port,
2752 struct switchdev_obj_port_fdb *fdb,
2753 switchdev_obj_dump_cb_t *cb)
2755 const struct ofdpa_port *ofdpa_port = rocker_port->wpriv;
2756 struct ofdpa *ofdpa = ofdpa_port->ofdpa;
2757 struct ofdpa_fdb_tbl_entry *found;
2758 struct hlist_node *tmp;
2759 unsigned long lock_flags;
2763 spin_lock_irqsave(&ofdpa->fdb_tbl_lock, lock_flags);
2764 hash_for_each_safe(ofdpa->fdb_tbl, bkt, tmp, found, entry) {
2765 if (found->key.ofdpa_port != ofdpa_port)
2767 ether_addr_copy(fdb->addr, found->key.addr);
2768 fdb->ndm_state = NUD_REACHABLE;
2769 fdb->vid = ofdpa_port_vlan_to_vid(ofdpa_port,
2770 found->key.vlan_id);
2771 err = cb(&fdb->obj);
2775 spin_unlock_irqrestore(&ofdpa->fdb_tbl_lock, lock_flags);
2780 static int ofdpa_port_bridge_join(struct ofdpa_port *ofdpa_port,
2781 struct net_device *bridge)
2785 /* Port is joining bridge, so the internal VLAN for the
2786 * port is going to change to the bridge internal VLAN.
2787 * Let's remove untagged VLAN (vid=0) from port and
2788 * re-add once internal VLAN has changed.
2791 err = ofdpa_port_vlan_del(ofdpa_port, OFDPA_UNTAGGED_VID, 0);
2795 ofdpa_port_internal_vlan_id_put(ofdpa_port,
2796 ofdpa_port->dev->ifindex);
2797 ofdpa_port->internal_vlan_id =
2798 ofdpa_port_internal_vlan_id_get(ofdpa_port, bridge->ifindex);
2800 ofdpa_port->bridge_dev = bridge;
2802 return ofdpa_port_vlan_add(ofdpa_port, NULL, OFDPA_UNTAGGED_VID, 0);
2805 static int ofdpa_port_bridge_leave(struct ofdpa_port *ofdpa_port)
2809 err = ofdpa_port_vlan_del(ofdpa_port, OFDPA_UNTAGGED_VID, 0);
2813 ofdpa_port_internal_vlan_id_put(ofdpa_port,
2814 ofdpa_port->bridge_dev->ifindex);
2815 ofdpa_port->internal_vlan_id =
2816 ofdpa_port_internal_vlan_id_get(ofdpa_port,
2817 ofdpa_port->dev->ifindex);
2819 ofdpa_port->bridge_dev = NULL;
2821 err = ofdpa_port_vlan_add(ofdpa_port, NULL, OFDPA_UNTAGGED_VID, 0);
2825 if (ofdpa_port->dev->flags & IFF_UP)
2826 err = ofdpa_port_fwd_enable(ofdpa_port, 0);
2831 static int ofdpa_port_ovs_changed(struct ofdpa_port *ofdpa_port,
2832 struct net_device *master)
2836 ofdpa_port->bridge_dev = master;
2838 err = ofdpa_port_fwd_disable(ofdpa_port, 0);
2841 err = ofdpa_port_fwd_enable(ofdpa_port, 0);
2846 static int ofdpa_port_master_linked(struct rocker_port *rocker_port,
2847 struct net_device *master)
2849 struct ofdpa_port *ofdpa_port = rocker_port->wpriv;
2852 if (netif_is_bridge_master(master))
2853 err = ofdpa_port_bridge_join(ofdpa_port, master);
2854 else if (netif_is_ovs_master(master))
2855 err = ofdpa_port_ovs_changed(ofdpa_port, master);
2859 static int ofdpa_port_master_unlinked(struct rocker_port *rocker_port,
2860 struct net_device *master)
2862 struct ofdpa_port *ofdpa_port = rocker_port->wpriv;
2865 if (ofdpa_port_is_bridged(ofdpa_port))
2866 err = ofdpa_port_bridge_leave(ofdpa_port);
2867 else if (ofdpa_port_is_ovsed(ofdpa_port))
2868 err = ofdpa_port_ovs_changed(ofdpa_port, NULL);
2872 static int ofdpa_port_neigh_update(struct rocker_port *rocker_port,
2873 struct neighbour *n)
2875 struct ofdpa_port *ofdpa_port = rocker_port->wpriv;
2876 int flags = (n->nud_state & NUD_VALID ? 0 : OFDPA_OP_FLAG_REMOVE) |
2877 OFDPA_OP_FLAG_NOWAIT;
2878 __be32 ip_addr = *(__be32 *) n->primary_key;
2880 return ofdpa_port_ipv4_neigh(ofdpa_port, NULL, flags, ip_addr, n->ha);
2883 static int ofdpa_port_neigh_destroy(struct rocker_port *rocker_port,
2884 struct neighbour *n)
2886 struct ofdpa_port *ofdpa_port = rocker_port->wpriv;
2887 int flags = OFDPA_OP_FLAG_REMOVE | OFDPA_OP_FLAG_NOWAIT;
2888 __be32 ip_addr = *(__be32 *) n->primary_key;
2890 return ofdpa_port_ipv4_neigh(ofdpa_port, NULL, flags, ip_addr, n->ha);
2893 static int ofdpa_port_ev_mac_vlan_seen(struct rocker_port *rocker_port,
2894 const unsigned char *addr,
2897 struct ofdpa_port *ofdpa_port = rocker_port->wpriv;
2898 int flags = OFDPA_OP_FLAG_NOWAIT | OFDPA_OP_FLAG_LEARNED;
2900 if (ofdpa_port->stp_state != BR_STATE_LEARNING &&
2901 ofdpa_port->stp_state != BR_STATE_FORWARDING)
2904 return ofdpa_port_fdb(ofdpa_port, NULL, addr, vlan_id, flags);
2907 static struct ofdpa_port *ofdpa_port_dev_lower_find(struct net_device *dev,
2908 struct rocker *rocker)
2910 struct rocker_port *rocker_port;
2912 rocker_port = rocker_port_dev_lower_find(dev, rocker);
2913 return rocker_port ? rocker_port->wpriv : NULL;
2916 static int ofdpa_fib4_add(struct rocker *rocker,
2917 const struct fib_entry_notifier_info *fen_info)
2919 struct ofdpa *ofdpa = rocker->wpriv;
2920 struct ofdpa_port *ofdpa_port;
2923 if (ofdpa->fib_aborted)
2925 ofdpa_port = ofdpa_port_dev_lower_find(fen_info->fi->fib_dev, rocker);
2928 err = ofdpa_port_fib_ipv4(ofdpa_port, NULL, htonl(fen_info->dst),
2929 fen_info->dst_len, fen_info->fi,
2930 fen_info->tb_id, 0);
2933 fib_info_offload_inc(fen_info->fi);
2937 static int ofdpa_fib4_del(struct rocker *rocker,
2938 const struct fib_entry_notifier_info *fen_info)
2940 struct ofdpa *ofdpa = rocker->wpriv;
2941 struct ofdpa_port *ofdpa_port;
2943 if (ofdpa->fib_aborted)
2945 ofdpa_port = ofdpa_port_dev_lower_find(fen_info->fi->fib_dev, rocker);
2948 fib_info_offload_dec(fen_info->fi);
2949 return ofdpa_port_fib_ipv4(ofdpa_port, NULL, htonl(fen_info->dst),
2950 fen_info->dst_len, fen_info->fi,
2951 fen_info->tb_id, OFDPA_OP_FLAG_REMOVE);
2954 static void ofdpa_fib4_abort(struct rocker *rocker)
2956 struct ofdpa *ofdpa = rocker->wpriv;
2957 struct ofdpa_port *ofdpa_port;
2958 struct ofdpa_flow_tbl_entry *flow_entry;
2959 struct hlist_node *tmp;
2960 unsigned long flags;
2963 if (ofdpa->fib_aborted)
2966 spin_lock_irqsave(&ofdpa->flow_tbl_lock, flags);
2967 hash_for_each_safe(ofdpa->flow_tbl, bkt, tmp, flow_entry, entry) {
2968 if (flow_entry->key.tbl_id !=
2969 ROCKER_OF_DPA_TABLE_ID_UNICAST_ROUTING)
2971 ofdpa_port = ofdpa_port_dev_lower_find(flow_entry->fi->fib_dev,
2975 fib_info_offload_dec(flow_entry->fi);
2976 ofdpa_flow_tbl_del(ofdpa_port, NULL, OFDPA_OP_FLAG_REMOVE,
2979 spin_unlock_irqrestore(&ofdpa->flow_tbl_lock, flags);
2980 ofdpa->fib_aborted = true;
2983 struct rocker_world_ops rocker_ofdpa_ops = {
2985 .priv_size = sizeof(struct ofdpa),
2986 .port_priv_size = sizeof(struct ofdpa_port),
2987 .mode = ROCKER_PORT_MODE_OF_DPA,
2990 .port_pre_init = ofdpa_port_pre_init,
2991 .port_init = ofdpa_port_init,
2992 .port_fini = ofdpa_port_fini,
2993 .port_open = ofdpa_port_open,
2994 .port_stop = ofdpa_port_stop,
2995 .port_attr_stp_state_set = ofdpa_port_attr_stp_state_set,
2996 .port_attr_bridge_flags_set = ofdpa_port_attr_bridge_flags_set,
2997 .port_attr_bridge_flags_get = ofdpa_port_attr_bridge_flags_get,
2998 .port_attr_bridge_ageing_time_set = ofdpa_port_attr_bridge_ageing_time_set,
2999 .port_obj_vlan_add = ofdpa_port_obj_vlan_add,
3000 .port_obj_vlan_del = ofdpa_port_obj_vlan_del,
3001 .port_obj_vlan_dump = ofdpa_port_obj_vlan_dump,
3002 .port_obj_fdb_add = ofdpa_port_obj_fdb_add,
3003 .port_obj_fdb_del = ofdpa_port_obj_fdb_del,
3004 .port_obj_fdb_dump = ofdpa_port_obj_fdb_dump,
3005 .port_master_linked = ofdpa_port_master_linked,
3006 .port_master_unlinked = ofdpa_port_master_unlinked,
3007 .port_neigh_update = ofdpa_port_neigh_update,
3008 .port_neigh_destroy = ofdpa_port_neigh_destroy,
3009 .port_ev_mac_vlan_seen = ofdpa_port_ev_mac_vlan_seen,
3010 .fib4_add = ofdpa_fib4_add,
3011 .fib4_del = ofdpa_fib4_del,
3012 .fib4_abort = ofdpa_fib4_abort,