Skip to content

Commit 654c9c5

Browse files
committed
Merge branch 'mdiobus_nested_read_write'
Neil Armstrong says: ==================== Refactor nested mdiobus read/write functions In order to avoid locked signal false positive for nested mdiobus read/write calls, nested code was introduced in mv88e6xxx and mdio-mux. But mv88e6060 also needs such nested mdiobus read/write calls. For sake of refactoring, introduce nested variants of mdiobus read/write and make them used by mv88e6xxx and mv88e6060. In a next patch, mdio-mux should also use these variant calls. ==================== Signed-off-by: David S. Miller <[email protected]>
2 parents 6fb3b6b + f050561 commit 654c9c5

File tree

4 files changed

+68
-39
lines changed

4 files changed

+68
-39
lines changed

drivers/net/dsa/mv88e6060.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ static int reg_read(struct dsa_switch *ds, int addr, int reg)
2626
if (bus == NULL)
2727
return -EINVAL;
2828

29-
return mdiobus_read(bus, ds->pd->sw_addr + addr, reg);
29+
return mdiobus_read_nested(bus, ds->pd->sw_addr + addr, reg);
3030
}
3131

3232
#define REG_READ(addr, reg) \
@@ -47,7 +47,7 @@ static int reg_write(struct dsa_switch *ds, int addr, int reg, u16 val)
4747
if (bus == NULL)
4848
return -EINVAL;
4949

50-
return mdiobus_write(bus, ds->pd->sw_addr + addr, reg, val);
50+
return mdiobus_write_nested(bus, ds->pd->sw_addr + addr, reg, val);
5151
}
5252

5353
#define REG_WRITE(addr, reg, val) \

drivers/net/dsa/mv88e6xxx.c

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,6 @@
2424
#include <net/switchdev.h>
2525
#include "mv88e6xxx.h"
2626

27-
/* MDIO bus access can be nested in the case of PHYs connected to the
28-
* internal MDIO bus of the switch, which is accessed via MDIO bus of
29-
* the Ethernet interface. Avoid lockdep false positives by using
30-
* mutex_lock_nested().
31-
*/
32-
static int mv88e6xxx_mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
33-
{
34-
int ret;
35-
36-
mutex_lock_nested(&bus->mdio_lock, SINGLE_DEPTH_NESTING);
37-
ret = bus->read(bus, addr, regnum);
38-
mutex_unlock(&bus->mdio_lock);
39-
40-
return ret;
41-
}
42-
43-
static int mv88e6xxx_mdiobus_write(struct mii_bus *bus, int addr, u32 regnum,
44-
u16 val)
45-
{
46-
int ret;
47-
48-
mutex_lock_nested(&bus->mdio_lock, SINGLE_DEPTH_NESTING);
49-
ret = bus->write(bus, addr, regnum, val);
50-
mutex_unlock(&bus->mdio_lock);
51-
52-
return ret;
53-
}
54-
5527
/* If the switch's ADDR[4:0] strap pins are strapped to zero, it will
5628
* use all 32 SMI bus addresses on its SMI bus, and all switch registers
5729
* will be directly accessible on some {device address,register address}
@@ -66,7 +38,7 @@ static int mv88e6xxx_reg_wait_ready(struct mii_bus *bus, int sw_addr)
6638
int i;
6739

6840
for (i = 0; i < 16; i++) {
69-
ret = mv88e6xxx_mdiobus_read(bus, sw_addr, SMI_CMD);
41+
ret = mdiobus_read_nested(bus, sw_addr, SMI_CMD);
7042
if (ret < 0)
7143
return ret;
7244

@@ -82,16 +54,16 @@ int __mv88e6xxx_reg_read(struct mii_bus *bus, int sw_addr, int addr, int reg)
8254
int ret;
8355

8456
if (sw_addr == 0)
85-
return mv88e6xxx_mdiobus_read(bus, addr, reg);
57+
return mdiobus_read_nested(bus, addr, reg);
8658

8759
/* Wait for the bus to become free. */
8860
ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
8961
if (ret < 0)
9062
return ret;
9163

9264
/* Transmit the read command. */
93-
ret = mv88e6xxx_mdiobus_write(bus, sw_addr, SMI_CMD,
94-
SMI_CMD_OP_22_READ | (addr << 5) | reg);
65+
ret = mdiobus_write_nested(bus, sw_addr, SMI_CMD,
66+
SMI_CMD_OP_22_READ | (addr << 5) | reg);
9567
if (ret < 0)
9668
return ret;
9769

@@ -101,7 +73,7 @@ int __mv88e6xxx_reg_read(struct mii_bus *bus, int sw_addr, int addr, int reg)
10173
return ret;
10274

10375
/* Read the data. */
104-
ret = mv88e6xxx_mdiobus_read(bus, sw_addr, SMI_DATA);
76+
ret = mdiobus_read_nested(bus, sw_addr, SMI_DATA);
10577
if (ret < 0)
10678
return ret;
10779

@@ -145,21 +117,21 @@ int __mv88e6xxx_reg_write(struct mii_bus *bus, int sw_addr, int addr,
145117
int ret;
146118

147119
if (sw_addr == 0)
148-
return mv88e6xxx_mdiobus_write(bus, addr, reg, val);
120+
return mdiobus_write_nested(bus, addr, reg, val);
149121

150122
/* Wait for the bus to become free. */
151123
ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
152124
if (ret < 0)
153125
return ret;
154126

155127
/* Transmit the data to write. */
156-
ret = mv88e6xxx_mdiobus_write(bus, sw_addr, SMI_DATA, val);
128+
ret = mdiobus_write_nested(bus, sw_addr, SMI_DATA, val);
157129
if (ret < 0)
158130
return ret;
159131

160132
/* Transmit the write command. */
161-
ret = mv88e6xxx_mdiobus_write(bus, sw_addr, SMI_CMD,
162-
SMI_CMD_OP_22_WRITE | (addr << 5) | reg);
133+
ret = mdiobus_write_nested(bus, sw_addr, SMI_CMD,
134+
SMI_CMD_OP_22_WRITE | (addr << 5) | reg);
163135
if (ret < 0)
164136
return ret;
165137

drivers/net/phy/mdio_bus.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,33 @@ struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
371371
}
372372
EXPORT_SYMBOL(mdiobus_scan);
373373

374+
/**
375+
* mdiobus_read_nested - Nested version of the mdiobus_read function
376+
* @bus: the mii_bus struct
377+
* @addr: the phy address
378+
* @regnum: register number to read
379+
*
380+
* In case of nested MDIO bus access avoid lockdep false positives by
381+
* using mutex_lock_nested().
382+
*
383+
* NOTE: MUST NOT be called from interrupt context,
384+
* because the bus read/write functions may wait for an interrupt
385+
* to conclude the operation.
386+
*/
387+
int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum)
388+
{
389+
int retval;
390+
391+
BUG_ON(in_interrupt());
392+
393+
mutex_lock_nested(&bus->mdio_lock, SINGLE_DEPTH_NESTING);
394+
retval = bus->read(bus, addr, regnum);
395+
mutex_unlock(&bus->mdio_lock);
396+
397+
return retval;
398+
}
399+
EXPORT_SYMBOL(mdiobus_read_nested);
400+
374401
/**
375402
* mdiobus_read - Convenience function for reading a given MII mgmt register
376403
* @bus: the mii_bus struct
@@ -395,6 +422,34 @@ int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
395422
}
396423
EXPORT_SYMBOL(mdiobus_read);
397424

425+
/**
426+
* mdiobus_write_nested - Nested version of the mdiobus_write function
427+
* @bus: the mii_bus struct
428+
* @addr: the phy address
429+
* @regnum: register number to write
430+
* @val: value to write to @regnum
431+
*
432+
* In case of nested MDIO bus access avoid lockdep false positives by
433+
* using mutex_lock_nested().
434+
*
435+
* NOTE: MUST NOT be called from interrupt context,
436+
* because the bus read/write functions may wait for an interrupt
437+
* to conclude the operation.
438+
*/
439+
int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val)
440+
{
441+
int err;
442+
443+
BUG_ON(in_interrupt());
444+
445+
mutex_lock_nested(&bus->mdio_lock, SINGLE_DEPTH_NESTING);
446+
err = bus->write(bus, addr, regnum, val);
447+
mutex_unlock(&bus->mdio_lock);
448+
449+
return err;
450+
}
451+
EXPORT_SYMBOL(mdiobus_write_nested);
452+
398453
/**
399454
* mdiobus_write - Convenience function for writing a given MII mgmt register
400455
* @bus: the mii_bus struct

include/linux/phy.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,9 @@ static inline struct mii_bus *devm_mdiobus_alloc(struct device *dev)
213213
void devm_mdiobus_free(struct device *dev, struct mii_bus *bus);
214214
struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr);
215215
int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum);
216+
int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum);
216217
int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val);
218+
int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val);
217219

218220

219221
#define PHY_INTERRUPT_DISABLED 0x0

0 commit comments

Comments
 (0)