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 Semiconductors
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 = 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 int ocelot_mrp_copy_add_vcap(struct ocelot *ocelot, int port,
81 int prio, unsigned long cookie)
83 const u8 mrp_mask[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 };
84 struct ocelot_vcap_filter *filter;
87 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
91 filter->key_type = OCELOT_VCAP_KEY_ETYPE;
93 filter->id.cookie = cookie;
94 filter->id.tc_offload = false;
95 filter->block_id = VCAP_IS2;
96 filter->type = OCELOT_VCAP_FILTER_OFFLOAD;
97 filter->ingress_port_mask = BIT(port);
98 /* Here is possible to use control or test dmac because the mask
99 * doesn't cover the LSB
101 ether_addr_copy(filter->key.etype.dmac.value, mrp_test_dmac);
102 ether_addr_copy(filter->key.etype.dmac.mask, mrp_mask);
103 filter->action.mask_mode = OCELOT_MASK_MODE_PERMIT_DENY;
104 filter->action.port_mask = 0x0;
105 filter->action.cpu_copy_ena = true;
106 filter->action.cpu_qu_num = OCELOT_MRP_CPUQ;
108 err = ocelot_vcap_filter_add(ocelot, filter, NULL);
115 static void ocelot_mrp_save_mac(struct ocelot *ocelot,
116 struct ocelot_port *port)
118 ocelot_mact_learn(ocelot, PGID_BLACKHOLE, mrp_test_dmac,
119 port->pvid_vlan.vid, ENTRYTYPE_LOCKED);
120 ocelot_mact_learn(ocelot, PGID_BLACKHOLE, mrp_control_dmac,
121 port->pvid_vlan.vid, ENTRYTYPE_LOCKED);
124 static void ocelot_mrp_del_mac(struct ocelot *ocelot,
125 struct ocelot_port *port)
127 ocelot_mact_forget(ocelot, mrp_test_dmac, port->pvid_vlan.vid);
128 ocelot_mact_forget(ocelot, mrp_control_dmac, port->pvid_vlan.vid);
131 int ocelot_mrp_add(struct ocelot *ocelot, int port,
132 const struct switchdev_obj_mrp *mrp)
134 struct ocelot_port *ocelot_port = ocelot->ports[port];
135 struct ocelot_port_private *priv;
136 struct net_device *dev;
141 priv = container_of(ocelot_port, struct ocelot_port_private, port);
144 if (mrp->p_port != dev && mrp->s_port != dev)
147 ocelot_port->mrp_ring_id = mrp->ring_id;
151 EXPORT_SYMBOL(ocelot_mrp_add);
153 int ocelot_mrp_del(struct ocelot *ocelot, int port,
154 const struct switchdev_obj_mrp *mrp)
156 struct ocelot_port *ocelot_port = ocelot->ports[port];
161 if (ocelot_port->mrp_ring_id != mrp->ring_id)
164 ocelot_port->mrp_ring_id = 0;
168 EXPORT_SYMBOL(ocelot_mrp_del);
170 int ocelot_mrp_add_ring_role(struct ocelot *ocelot, int port,
171 const struct switchdev_obj_ring_role_mrp *mrp)
173 struct ocelot_port *ocelot_port = ocelot->ports[port];
180 if (mrp->ring_role != BR_MRP_RING_ROLE_MRC && !mrp->sw_backup)
183 if (ocelot_port->mrp_ring_id != mrp->ring_id)
186 ocelot_mrp_save_mac(ocelot, ocelot_port);
188 if (mrp->ring_role != BR_MRP_RING_ROLE_MRC)
189 return ocelot_mrp_copy_add_vcap(ocelot, port, 1, port);
191 dst_port = ocelot_mrp_find_partner_port(ocelot, ocelot_port);
195 err = ocelot_mrp_redirect_add_vcap(ocelot, port, dst_port);
199 err = ocelot_mrp_copy_add_vcap(ocelot, port, 2,
200 port + ocelot->num_phys_ports);
202 ocelot_mrp_del_vcap(ocelot, port);
208 EXPORT_SYMBOL(ocelot_mrp_add_ring_role);
210 int ocelot_mrp_del_ring_role(struct ocelot *ocelot, int port,
211 const struct switchdev_obj_ring_role_mrp *mrp)
213 struct ocelot_port *ocelot_port = ocelot->ports[port];
219 if (mrp->ring_role != BR_MRP_RING_ROLE_MRC && !mrp->sw_backup)
222 if (ocelot_port->mrp_ring_id != mrp->ring_id)
225 ocelot_mrp_del_vcap(ocelot, port);
226 ocelot_mrp_del_vcap(ocelot, port + ocelot->num_phys_ports);
228 for (i = 0; i < ocelot->num_phys_ports; ++i) {
229 ocelot_port = ocelot->ports[i];
234 if (ocelot_port->mrp_ring_id != 0)
238 ocelot_mrp_del_mac(ocelot, ocelot->ports[port]);
242 EXPORT_SYMBOL(ocelot_mrp_del_ring_role);