1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2 /* Microsemi Ocelot Switch driver
4 * Copyright (c) 2017, 2019 Microsemi Corporation
5 * Copyright 2020-2021 NXP
8 #include <linux/if_bridge.h>
9 #include <linux/mrp_bridge.h>
10 #include <soc/mscc/ocelot_vcap.h>
11 #include <uapi/linux/mrp_bridge.h>
13 #include "ocelot_vcap.h"
15 static const u8 mrp_test_dmac[] = { 0x01, 0x15, 0x4e, 0x00, 0x00, 0x01 };
16 static const u8 mrp_control_dmac[] = { 0x01, 0x15, 0x4e, 0x00, 0x00, 0x02 };
18 static int ocelot_mrp_find_partner_port(struct ocelot *ocelot,
19 struct ocelot_port *p)
23 for (i = 0; i < ocelot->num_phys_ports; ++i) {
24 struct ocelot_port *ocelot_port = ocelot->ports[i];
26 if (!ocelot_port || p == ocelot_port)
29 if (ocelot_port->mrp_ring_id == p->mrp_ring_id)
36 static int ocelot_mrp_del_vcap(struct ocelot *ocelot, int id)
38 struct ocelot_vcap_block *block_vcap_is2;
39 struct ocelot_vcap_filter *filter;
41 block_vcap_is2 = &ocelot->block[VCAP_IS2];
42 filter = ocelot_vcap_block_find_filter_by_id(block_vcap_is2, id,
47 return ocelot_vcap_filter_del(ocelot, filter);
50 static int ocelot_mrp_redirect_add_vcap(struct ocelot *ocelot, int src_port,
53 const u8 mrp_test_mask[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
54 struct ocelot_vcap_filter *filter;
57 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
61 filter->key_type = OCELOT_VCAP_KEY_ETYPE;
63 filter->id.cookie = OCELOT_VCAP_IS2_MRP_REDIRECT(ocelot, src_port);
64 filter->id.tc_offload = false;
65 filter->block_id = VCAP_IS2;
66 filter->type = OCELOT_VCAP_FILTER_OFFLOAD;
67 filter->ingress_port_mask = BIT(src_port);
68 ether_addr_copy(filter->key.etype.dmac.value, mrp_test_dmac);
69 ether_addr_copy(filter->key.etype.dmac.mask, mrp_test_mask);
70 filter->action.mask_mode = OCELOT_MASK_MODE_REDIRECT;
71 filter->action.port_mask = BIT(dst_port);
73 err = ocelot_vcap_filter_add(ocelot, filter, NULL);
80 static void ocelot_populate_mrp_trap_key(struct ocelot_vcap_filter *filter)
82 const u8 mrp_mask[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 };
84 /* Here is possible to use control or test dmac because the mask
85 * doesn't cover the LSB
87 ether_addr_copy(filter->key.etype.dmac.value, mrp_test_dmac);
88 ether_addr_copy(filter->key.etype.dmac.mask, mrp_mask);
91 static int ocelot_mrp_trap_add(struct ocelot *ocelot, int port)
93 unsigned long cookie = OCELOT_VCAP_IS2_MRP_TRAP(ocelot);
95 return ocelot_trap_add(ocelot, port, cookie, false,
96 ocelot_populate_mrp_trap_key);
99 static int ocelot_mrp_trap_del(struct ocelot *ocelot, int port)
101 unsigned long cookie = OCELOT_VCAP_IS2_MRP_TRAP(ocelot);
103 return ocelot_trap_del(ocelot, port, cookie);
106 static void ocelot_mrp_save_mac(struct ocelot *ocelot,
107 struct ocelot_port *port)
109 ocelot_mact_learn(ocelot, PGID_BLACKHOLE, mrp_test_dmac,
110 OCELOT_STANDALONE_PVID, ENTRYTYPE_LOCKED);
111 ocelot_mact_learn(ocelot, PGID_BLACKHOLE, mrp_control_dmac,
112 OCELOT_STANDALONE_PVID, ENTRYTYPE_LOCKED);
115 static void ocelot_mrp_del_mac(struct ocelot *ocelot,
116 struct ocelot_port *port)
118 ocelot_mact_forget(ocelot, mrp_test_dmac, OCELOT_STANDALONE_PVID);
119 ocelot_mact_forget(ocelot, mrp_control_dmac, OCELOT_STANDALONE_PVID);
122 int ocelot_mrp_add(struct ocelot *ocelot, int port,
123 const struct switchdev_obj_mrp *mrp)
125 struct ocelot_port *ocelot_port = ocelot->ports[port];
126 struct ocelot_port_private *priv;
127 struct net_device *dev;
132 priv = container_of(ocelot_port, struct ocelot_port_private, port);
135 if (mrp->p_port != dev && mrp->s_port != dev)
138 ocelot_port->mrp_ring_id = mrp->ring_id;
142 EXPORT_SYMBOL(ocelot_mrp_add);
144 int ocelot_mrp_del(struct ocelot *ocelot, int port,
145 const struct switchdev_obj_mrp *mrp)
147 struct ocelot_port *ocelot_port = ocelot->ports[port];
152 if (ocelot_port->mrp_ring_id != mrp->ring_id)
155 ocelot_port->mrp_ring_id = 0;
159 EXPORT_SYMBOL(ocelot_mrp_del);
161 int ocelot_mrp_add_ring_role(struct ocelot *ocelot, int port,
162 const struct switchdev_obj_ring_role_mrp *mrp)
164 struct ocelot_port *ocelot_port = ocelot->ports[port];
171 if (mrp->ring_role != BR_MRP_RING_ROLE_MRC && !mrp->sw_backup)
174 if (ocelot_port->mrp_ring_id != mrp->ring_id)
177 ocelot_mrp_save_mac(ocelot, ocelot_port);
179 if (mrp->ring_role != BR_MRP_RING_ROLE_MRC)
180 return ocelot_mrp_trap_add(ocelot, port);
182 dst_port = ocelot_mrp_find_partner_port(ocelot, ocelot_port);
186 err = ocelot_mrp_redirect_add_vcap(ocelot, port, dst_port);
190 err = ocelot_mrp_trap_add(ocelot, port);
192 ocelot_mrp_del_vcap(ocelot,
193 OCELOT_VCAP_IS2_MRP_REDIRECT(ocelot, port));
199 EXPORT_SYMBOL(ocelot_mrp_add_ring_role);
201 int ocelot_mrp_del_ring_role(struct ocelot *ocelot, int port,
202 const struct switchdev_obj_ring_role_mrp *mrp)
204 struct ocelot_port *ocelot_port = ocelot->ports[port];
210 if (mrp->ring_role != BR_MRP_RING_ROLE_MRC && !mrp->sw_backup)
213 if (ocelot_port->mrp_ring_id != mrp->ring_id)
216 err = ocelot_mrp_trap_del(ocelot, port);
220 ocelot_mrp_del_vcap(ocelot, OCELOT_VCAP_IS2_MRP_REDIRECT(ocelot, port));
222 for (i = 0; i < ocelot->num_phys_ports; ++i) {
223 ocelot_port = ocelot->ports[i];
228 if (ocelot_port->mrp_ring_id != 0)
232 ocelot_mrp_del_mac(ocelot, ocelot->ports[port]);
236 EXPORT_SYMBOL(ocelot_mrp_del_ring_role);