Skip to content

Commit da7b9e9

Browse files
ffainellidavem330
authored andcommitted
net: dsa: Add ndo_get_phys_port_name() for CPU port
There is not currently way to infer the port number through sysfs that is being used as the CPU port number. Overlay a ndo_get_phys_port_name() operation onto the DSA master network device in order to retrieve that information. Signed-off-by: Florian Fainelli <f.fainelli@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 44543f1 commit da7b9e9

2 files changed

Lines changed: 60 additions & 1 deletion

File tree

‎include/net/dsa.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,11 @@ struct dsa_port {
208208
* Original copy of the master netdev ethtool_ops
209209
*/
210210
const struct ethtool_ops *orig_ethtool_ops;
211+
212+
/*
213+
* Original copy of the master netdev net_device_ops
214+
*/
215+
const struct net_device_ops *orig_ndo_ops;
211216
};
212217

213218
struct dsa_switch {

‎net/dsa/master.c‎

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,17 @@ static void dsa_master_get_strings(struct net_device *dev, uint32_t stringset,
126126
}
127127
}
128128

129+
static int dsa_master_get_phys_port_name(struct net_device *dev,
130+
char *name, size_t len)
131+
{
132+
struct dsa_port *cpu_dp = dev->dsa_ptr;
133+
134+
if (snprintf(name, len, "p%d", cpu_dp->index) >= len)
135+
return -EINVAL;
136+
137+
return 0;
138+
}
139+
129140
static int dsa_master_ethtool_setup(struct net_device *dev)
130141
{
131142
struct dsa_port *cpu_dp = dev->dsa_ptr;
@@ -158,6 +169,38 @@ static void dsa_master_ethtool_teardown(struct net_device *dev)
158169
cpu_dp->orig_ethtool_ops = NULL;
159170
}
160171

172+
static int dsa_master_ndo_setup(struct net_device *dev)
173+
{
174+
struct dsa_port *cpu_dp = dev->dsa_ptr;
175+
struct dsa_switch *ds = cpu_dp->ds;
176+
struct net_device_ops *ops;
177+
178+
if (dev->netdev_ops->ndo_get_phys_port_name)
179+
return 0;
180+
181+
ops = devm_kzalloc(ds->dev, sizeof(*ops), GFP_KERNEL);
182+
if (!ops)
183+
return -ENOMEM;
184+
185+
cpu_dp->orig_ndo_ops = dev->netdev_ops;
186+
if (cpu_dp->orig_ndo_ops)
187+
memcpy(ops, cpu_dp->orig_ndo_ops, sizeof(*ops));
188+
189+
ops->ndo_get_phys_port_name = dsa_master_get_phys_port_name;
190+
191+
dev->netdev_ops = ops;
192+
193+
return 0;
194+
}
195+
196+
static void dsa_master_ndo_teardown(struct net_device *dev)
197+
{
198+
struct dsa_port *cpu_dp = dev->dsa_ptr;
199+
200+
dev->netdev_ops = cpu_dp->orig_ndo_ops;
201+
cpu_dp->orig_ndo_ops = NULL;
202+
}
203+
161204
static ssize_t tagging_show(struct device *d, struct device_attribute *attr,
162205
char *buf)
163206
{
@@ -223,16 +266,27 @@ int dsa_master_setup(struct net_device *dev, struct dsa_port *cpu_dp)
223266
if (ret)
224267
return ret;
225268

269+
ret = dsa_master_ndo_setup(dev);
270+
if (ret)
271+
goto out_err_ethtool_teardown;
272+
226273
ret = sysfs_create_group(&dev->dev.kobj, &dsa_group);
227274
if (ret)
228-
dsa_master_ethtool_teardown(dev);
275+
goto out_err_ndo_teardown;
276+
277+
return ret;
229278

279+
out_err_ndo_teardown:
280+
dsa_master_ndo_teardown(dev);
281+
out_err_ethtool_teardown:
282+
dsa_master_ethtool_teardown(dev);
230283
return ret;
231284
}
232285

233286
void dsa_master_teardown(struct net_device *dev)
234287
{
235288
sysfs_remove_group(&dev->dev.kobj, &dsa_group);
289+
dsa_master_ndo_teardown(dev);
236290
dsa_master_ethtool_teardown(dev);
237291
dsa_master_reset_mtu(dev);
238292

0 commit comments

Comments
 (0)