Skip to content

Commit 319e4dd

Browse files
vladimirolteandavem330
authored andcommitted
net: mscc: ocelot: introduce conversion helpers between port and netdev
Since the mscc_ocelot_switch_lib is common between a pure switchdev and a DSA driver, the procedure of retrieving a net_device for a certain port index differs, as those are registered by their individual front-ends. Up to now that has been dealt with by always passing the port index to the switch library, but now, we're going to need to work with net_device pointers from the tc-flower offload, for things like indev, or mirred. It is not desirable to refactor that, so let's make sure that the flower offload core has the ability to translate between a net_device and a port index properly. Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com> Acked-by: Alexandre Belloni <alexandre.belloni@bootlin.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent ea9d1f3 commit 319e4dd

8 files changed

Lines changed: 65 additions & 0 deletions

File tree

‎drivers/net/dsa/ocelot/felix.c‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,3 +810,25 @@ const struct dsa_switch_ops felix_switch_ops = {
810810
.cls_flower_stats = felix_cls_flower_stats,
811811
.port_setup_tc = felix_port_setup_tc,
812812
};
813+
814+
struct net_device *felix_port_to_netdev(struct ocelot *ocelot, int port)
815+
{
816+
struct felix *felix = ocelot_to_felix(ocelot);
817+
struct dsa_switch *ds = felix->ds;
818+
819+
if (!dsa_is_user_port(ds, port))
820+
return NULL;
821+
822+
return dsa_to_port(ds, port)->slave;
823+
}
824+
825+
int felix_netdev_to_port(struct net_device *dev)
826+
{
827+
struct dsa_port *dp;
828+
829+
dp = dsa_port_from_netdev(dev);
830+
if (IS_ERR(dp))
831+
return -EINVAL;
832+
833+
return dp->index;
834+
}

‎drivers/net/dsa/ocelot/felix.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,7 @@ struct felix {
5252
resource_size_t imdio_base;
5353
};
5454

55+
struct net_device *felix_port_to_netdev(struct ocelot *ocelot, int port);
56+
int felix_netdev_to_port(struct net_device *dev);
57+
5558
#endif

‎drivers/net/dsa/ocelot/felix_vsc9959.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,8 @@ static u16 vsc9959_wm_enc(u16 value)
10061006
static const struct ocelot_ops vsc9959_ops = {
10071007
.reset = vsc9959_reset,
10081008
.wm_enc = vsc9959_wm_enc,
1009+
.port_to_netdev = felix_port_to_netdev,
1010+
.netdev_to_port = felix_netdev_to_port,
10091011
};
10101012

10111013
static int vsc9959_mdio_bus_alloc(struct ocelot *ocelot)

‎drivers/net/dsa/ocelot/seville_vsc9953.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,8 @@ static u16 vsc9953_wm_enc(u16 value)
10581058
static const struct ocelot_ops vsc9953_ops = {
10591059
.reset = vsc9953_reset,
10601060
.wm_enc = vsc9953_wm_enc,
1061+
.port_to_netdev = felix_port_to_netdev,
1062+
.netdev_to_port = felix_netdev_to_port,
10611063
};
10621064

10631065
static int vsc9953_mdio_bus_alloc(struct ocelot *ocelot)

‎drivers/net/ethernet/mscc/ocelot.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ int ocelot_port_lag_join(struct ocelot *ocelot, int port,
9898
struct net_device *bond);
9999
void ocelot_port_lag_leave(struct ocelot *ocelot, int port,
100100
struct net_device *bond);
101+
struct net_device *ocelot_port_to_netdev(struct ocelot *ocelot, int port);
102+
int ocelot_netdev_to_port(struct net_device *dev);
101103

102104
u32 ocelot_port_readl(struct ocelot_port *port, u32 reg);
103105
void ocelot_port_writel(struct ocelot_port *port, u32 val, u32 reg);

‎drivers/net/ethernet/mscc/ocelot_net.c‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,36 @@ static const struct net_device_ops ocelot_port_netdev_ops = {
656656
.ndo_do_ioctl = ocelot_ioctl,
657657
};
658658

659+
struct net_device *ocelot_port_to_netdev(struct ocelot *ocelot, int port)
660+
{
661+
struct ocelot_port *ocelot_port = ocelot->ports[port];
662+
struct ocelot_port_private *priv;
663+
664+
if (!ocelot_port)
665+
return NULL;
666+
667+
priv = container_of(ocelot_port, struct ocelot_port_private, port);
668+
669+
return priv->dev;
670+
}
671+
672+
static bool ocelot_port_dev_check(const struct net_device *dev)
673+
{
674+
return dev->netdev_ops == &ocelot_port_netdev_ops;
675+
}
676+
677+
int ocelot_netdev_to_port(struct net_device *dev)
678+
{
679+
struct ocelot_port_private *priv;
680+
681+
if (!dev || !ocelot_port_dev_check(dev))
682+
return -EINVAL;
683+
684+
priv = netdev_priv(dev);
685+
686+
return priv->chip_port;
687+
}
688+
659689
static void ocelot_port_get_strings(struct net_device *netdev, u32 sset,
660690
u8 *data)
661691
{

‎drivers/net/ethernet/mscc/ocelot_vsc7514.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,8 @@ static u16 ocelot_wm_enc(u16 value)
763763
static const struct ocelot_ops ocelot_ops = {
764764
.reset = ocelot_reset,
765765
.wm_enc = ocelot_wm_enc,
766+
.port_to_netdev = ocelot_port_to_netdev,
767+
.netdev_to_port = ocelot_netdev_to_port,
766768
};
767769

768770
static const struct vcap_field vsc7514_vcap_es0_keys[] = {

‎include/soc/mscc/ocelot.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,8 @@ enum ocelot_tag_prefix {
559559
struct ocelot;
560560

561561
struct ocelot_ops {
562+
struct net_device *(*port_to_netdev)(struct ocelot *ocelot, int port);
563+
int (*netdev_to_port)(struct net_device *dev);
562564
int (*reset)(struct ocelot *ocelot);
563565
u16 (*wm_enc)(u16 value);
564566
};

0 commit comments

Comments
 (0)