Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
0002 /* Microsemi Ocelot Switch driver
0003  *
0004  * Copyright (c) 2017, 2019 Microsemi Corporation
0005  * Copyright 2020-2021 NXP
0006  */
0007 
0008 #include <linux/if_bridge.h>
0009 #include <linux/mrp_bridge.h>
0010 #include <soc/mscc/ocelot_vcap.h>
0011 #include <uapi/linux/mrp_bridge.h>
0012 #include "ocelot.h"
0013 #include "ocelot_vcap.h"
0014 
0015 static const u8 mrp_test_dmac[] = { 0x01, 0x15, 0x4e, 0x00, 0x00, 0x01 };
0016 static const u8 mrp_control_dmac[] = { 0x01, 0x15, 0x4e, 0x00, 0x00, 0x02 };
0017 
0018 static int ocelot_mrp_find_partner_port(struct ocelot *ocelot,
0019                     struct ocelot_port *p)
0020 {
0021     int i;
0022 
0023     for (i = 0; i < ocelot->num_phys_ports; ++i) {
0024         struct ocelot_port *ocelot_port = ocelot->ports[i];
0025 
0026         if (!ocelot_port || p == ocelot_port)
0027             continue;
0028 
0029         if (ocelot_port->mrp_ring_id == p->mrp_ring_id)
0030             return i;
0031     }
0032 
0033     return -1;
0034 }
0035 
0036 static int ocelot_mrp_del_vcap(struct ocelot *ocelot, int id)
0037 {
0038     struct ocelot_vcap_block *block_vcap_is2;
0039     struct ocelot_vcap_filter *filter;
0040 
0041     block_vcap_is2 = &ocelot->block[VCAP_IS2];
0042     filter = ocelot_vcap_block_find_filter_by_id(block_vcap_is2, id,
0043                              false);
0044     if (!filter)
0045         return 0;
0046 
0047     return ocelot_vcap_filter_del(ocelot, filter);
0048 }
0049 
0050 static int ocelot_mrp_redirect_add_vcap(struct ocelot *ocelot, int src_port,
0051                     int dst_port)
0052 {
0053     const u8 mrp_test_mask[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
0054     struct ocelot_vcap_filter *filter;
0055     int err;
0056 
0057     filter = kzalloc(sizeof(*filter), GFP_KERNEL);
0058     if (!filter)
0059         return -ENOMEM;
0060 
0061     filter->key_type = OCELOT_VCAP_KEY_ETYPE;
0062     filter->prio = 1;
0063     filter->id.cookie = OCELOT_VCAP_IS2_MRP_REDIRECT(ocelot, src_port);
0064     filter->id.tc_offload = false;
0065     filter->block_id = VCAP_IS2;
0066     filter->type = OCELOT_VCAP_FILTER_OFFLOAD;
0067     filter->ingress_port_mask = BIT(src_port);
0068     ether_addr_copy(filter->key.etype.dmac.value, mrp_test_dmac);
0069     ether_addr_copy(filter->key.etype.dmac.mask, mrp_test_mask);
0070     filter->action.mask_mode = OCELOT_MASK_MODE_REDIRECT;
0071     filter->action.port_mask = BIT(dst_port);
0072 
0073     err = ocelot_vcap_filter_add(ocelot, filter, NULL);
0074     if (err)
0075         kfree(filter);
0076 
0077     return err;
0078 }
0079 
0080 static void ocelot_populate_mrp_trap_key(struct ocelot_vcap_filter *filter)
0081 {
0082     const u8 mrp_mask[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 };
0083 
0084     /* Here is possible to use control or test dmac because the mask
0085      * doesn't cover the LSB
0086      */
0087     ether_addr_copy(filter->key.etype.dmac.value, mrp_test_dmac);
0088     ether_addr_copy(filter->key.etype.dmac.mask, mrp_mask);
0089 }
0090 
0091 static int ocelot_mrp_trap_add(struct ocelot *ocelot, int port)
0092 {
0093     unsigned long cookie = OCELOT_VCAP_IS2_MRP_TRAP(ocelot);
0094 
0095     return ocelot_trap_add(ocelot, port, cookie, false,
0096                    ocelot_populate_mrp_trap_key);
0097 }
0098 
0099 static int ocelot_mrp_trap_del(struct ocelot *ocelot, int port)
0100 {
0101     unsigned long cookie = OCELOT_VCAP_IS2_MRP_TRAP(ocelot);
0102 
0103     return ocelot_trap_del(ocelot, port, cookie);
0104 }
0105 
0106 static void ocelot_mrp_save_mac(struct ocelot *ocelot,
0107                 struct ocelot_port *port)
0108 {
0109     ocelot_mact_learn(ocelot, PGID_BLACKHOLE, mrp_test_dmac,
0110               OCELOT_STANDALONE_PVID, ENTRYTYPE_LOCKED);
0111     ocelot_mact_learn(ocelot, PGID_BLACKHOLE, mrp_control_dmac,
0112               OCELOT_STANDALONE_PVID, ENTRYTYPE_LOCKED);
0113 }
0114 
0115 static void ocelot_mrp_del_mac(struct ocelot *ocelot,
0116                    struct ocelot_port *port)
0117 {
0118     ocelot_mact_forget(ocelot, mrp_test_dmac, OCELOT_STANDALONE_PVID);
0119     ocelot_mact_forget(ocelot, mrp_control_dmac, OCELOT_STANDALONE_PVID);
0120 }
0121 
0122 int ocelot_mrp_add(struct ocelot *ocelot, int port,
0123            const struct switchdev_obj_mrp *mrp)
0124 {
0125     struct ocelot_port *ocelot_port = ocelot->ports[port];
0126     struct ocelot_port_private *priv;
0127     struct net_device *dev;
0128 
0129     if (!ocelot_port)
0130         return -EOPNOTSUPP;
0131 
0132     priv = container_of(ocelot_port, struct ocelot_port_private, port);
0133     dev = priv->dev;
0134 
0135     if (mrp->p_port != dev && mrp->s_port != dev)
0136         return 0;
0137 
0138     ocelot_port->mrp_ring_id = mrp->ring_id;
0139 
0140     return 0;
0141 }
0142 EXPORT_SYMBOL(ocelot_mrp_add);
0143 
0144 int ocelot_mrp_del(struct ocelot *ocelot, int port,
0145            const struct switchdev_obj_mrp *mrp)
0146 {
0147     struct ocelot_port *ocelot_port = ocelot->ports[port];
0148 
0149     if (!ocelot_port)
0150         return -EOPNOTSUPP;
0151 
0152     if (ocelot_port->mrp_ring_id != mrp->ring_id)
0153         return 0;
0154 
0155     ocelot_port->mrp_ring_id = 0;
0156 
0157     return 0;
0158 }
0159 EXPORT_SYMBOL(ocelot_mrp_del);
0160 
0161 int ocelot_mrp_add_ring_role(struct ocelot *ocelot, int port,
0162                  const struct switchdev_obj_ring_role_mrp *mrp)
0163 {
0164     struct ocelot_port *ocelot_port = ocelot->ports[port];
0165     int dst_port;
0166     int err;
0167 
0168     if (!ocelot_port)
0169         return -EOPNOTSUPP;
0170 
0171     if (mrp->ring_role != BR_MRP_RING_ROLE_MRC && !mrp->sw_backup)
0172         return -EOPNOTSUPP;
0173 
0174     if (ocelot_port->mrp_ring_id != mrp->ring_id)
0175         return 0;
0176 
0177     ocelot_mrp_save_mac(ocelot, ocelot_port);
0178 
0179     if (mrp->ring_role != BR_MRP_RING_ROLE_MRC)
0180         return ocelot_mrp_trap_add(ocelot, port);
0181 
0182     dst_port = ocelot_mrp_find_partner_port(ocelot, ocelot_port);
0183     if (dst_port == -1)
0184         return -EINVAL;
0185 
0186     err = ocelot_mrp_redirect_add_vcap(ocelot, port, dst_port);
0187     if (err)
0188         return err;
0189 
0190     err = ocelot_mrp_trap_add(ocelot, port);
0191     if (err) {
0192         ocelot_mrp_del_vcap(ocelot,
0193                     OCELOT_VCAP_IS2_MRP_REDIRECT(ocelot, port));
0194         return err;
0195     }
0196 
0197     return 0;
0198 }
0199 EXPORT_SYMBOL(ocelot_mrp_add_ring_role);
0200 
0201 int ocelot_mrp_del_ring_role(struct ocelot *ocelot, int port,
0202                  const struct switchdev_obj_ring_role_mrp *mrp)
0203 {
0204     struct ocelot_port *ocelot_port = ocelot->ports[port];
0205     int err, i;
0206 
0207     if (!ocelot_port)
0208         return -EOPNOTSUPP;
0209 
0210     if (mrp->ring_role != BR_MRP_RING_ROLE_MRC && !mrp->sw_backup)
0211         return -EOPNOTSUPP;
0212 
0213     if (ocelot_port->mrp_ring_id != mrp->ring_id)
0214         return 0;
0215 
0216     err = ocelot_mrp_trap_del(ocelot, port);
0217     if (err)
0218         return err;
0219 
0220     ocelot_mrp_del_vcap(ocelot, OCELOT_VCAP_IS2_MRP_REDIRECT(ocelot, port));
0221 
0222     for (i = 0; i < ocelot->num_phys_ports; ++i) {
0223         ocelot_port = ocelot->ports[i];
0224 
0225         if (!ocelot_port)
0226             continue;
0227 
0228         if (ocelot_port->mrp_ring_id != 0)
0229             goto out;
0230     }
0231 
0232     ocelot_mrp_del_mac(ocelot, ocelot->ports[port]);
0233 out:
0234     return 0;
0235 }
0236 EXPORT_SYMBOL(ocelot_mrp_del_ring_role);