Skip to contentSkip to conversation

MII bus API for PHY devices

PATCH Jason McMullan 3 participants 6 messages Nov 11, 2004 – Nov 13, 2004 Network drivers

lore
Share & tools
lore
#0 lore raw

Second attempt, more polish.

(Though I'm leaving my macro abuse in for now!)

Description: MII Bus interface
Date:        Thu, 11 Nov 2004 17:44:57 -0500

Depends:

	linux-2.6.9

###############################

Index of changes:

 drivers/net/Makefile          |    2 
 linux/drivers/net/mii_bus.c   |  857 ++++++++++++++++++++++++++++++++++++++++++
 linux/include/linux/mii_bus.h |  132 ++++++
 3 files changed, 990 insertions(+), 1 deletion(-)
drivers/net/Makefile +1 -1
@@ -62,7 +62,7 @@
 # end link order section
 #
 
-obj-$(CONFIG_MII) += mii.o
+obj-$(CONFIG_MII) += mii.o mii_bus.o
 
 obj-$(CONFIG_SUNDANCE) += sundance.o
 obj-$(CONFIG_HAMACHI) += hamachi.o
drivers/net/mii_bus.c new file +857 -0

857 lines not on this page · raw message

include/linux/mii_bus.h new file +132 -0

132 lines not on this page · raw message

Signature · 2 lines
Jason McMullan <[email protected]>
TimeSys Corporation
#1 lore raw

Jason McMullan <[email protected]> :
[...]

Quoted 2 lines
drivers/net/mii_bus.c new file +0 -0

[...]

drivers/net/mii_bus.c line 55

Quoted 4 lines
drivers/net/mii_bus.c new file +4 -0
+struct phy_cmd {
+    u32 mii_reg;
+    u32 mii_data;
+    u16 (*funct) (u16 mii_reg, int bus, int id);

^^^^

-> spaces

[...]

drivers/net/mii_bus.c line 103

Quoted 8 lines
drivers/net/mii_bus.c new file +8 -0
+    /* Local state information */
+    struct {
+	int irq;
+	unsigned long msecs;
+	void (*func)(void *data);
+	void *data;
+    	struct work_struct tq;
+	struct timer_list timer;

^^^^^

-> tab...

Quoted 1 line

+ } delta;

^^^^

-> ...spaces

[...]

drivers/net/mii_bus.c line 135

Quoted 5 lines
drivers/net/mii_bus.c new file +5 -0
+/* Write value to the PHY for this device to the register at regnum, */
+/* waiting until the write is done before it returns.  All PHY */
+/* configuration has to be done through the TSEC1 MIIM regs */
+EXPORT_SYMBOL(mii_bus_write);
+int mii_bus_write(int bus, int id, int regnum, uint16_t value)
                                                  ^^^^^^^^

-> the code of this file has already used some u16/u32 before this point.

drivers/net/mii_bus.c line 140

Quoted 5 lines
drivers/net/mii_bus.c new file +5 -0
+{
+	if (!VALID_BUS(bus))
+		return -EINVAL;
+
+       	mii_bus[bus]->write(mii_bus[bus]->priv,id,regnum,value);

^^^^^^^^^^^^^

-> spaces instead of tab
-> please add a space after a colon

-> AFAIKS, the code guarantees that VALID_BUS will not fail.

drivers/net/mii_bus.c line 153

Quoted 13 lines
drivers/net/mii_bus.c new file +13 -0
+	return 0;
+}
+
+/* Reads from register regnum in the PHY for device dev, */
+/* returning the value.  Clears miimcom first.  All PHY */
+/* configuration has to be done through the TSEC1 MIIM regs */
+EXPORT_SYMBOL(mii_bus_read);
+int mii_bus_read(int bus, int id, int regnum)
+{
+	if (!VALID_BUS(bus))
+		return -EINVAL;
+
+       	return mii_bus[bus]->read(mii_bus[bus]->priv,id,regnum);

-> see above.

[...]

drivers/net/mii_bus.c line 170

Quoted 8 lines
drivers/net/mii_bus.c new file +8 -0
+#define BRIEF_MII_ERRORS
+/* Wait for auto-negotiation to complete */
+u16 mii_parse_sr(u16 mii_reg, int bus, int id)
+{
+	unsigned int timeout = MII_TIMEOUT;
+	struct phy_state *pstate;
+
+	if (!VALID(bus, id)) return 0xffff;

-> the return on a separate line only costs an extra character
and makes the style more consistent (see the code above).

[...]

drivers/net/mii_bus.c line 263

Quoted 13 lines
drivers/net/mii_bus.c new file +13 -0
+u16 mii_parse_cis8201(u16 mii_reg, int bus, int id)
+{
+	unsigned int speed;
+	struct phy_state *pstate;
+
+	if (!VALID(bus, id)) return 0xffff;
+	pstate = &mii_bus[bus]->phy[id]->state;
+
+	if (pstate->link) {
+		if (mii_reg & MIIM_CIS8201_AUXCONSTAT_DUPLEX)
+			pstate->duplex = 1;
+		else
+			pstate->duplex = 0;

-> If you are really concerned about the size of the source code:

		pstate->duplex =
			(mii_reg & MIIM_CIS8201_AUXCONSTAT_DUPLEX) ? 1 : 0;

drivers/net/mii_bus.c line 480

Quoted 12 lines
drivers/net/mii_bus.c new file +12 -0
+	NULL
+};
+
+/* Use the PHY ID registers to determine what type of PHY is attached
+ * to device dev.  return a struct phy_info structure describing that PHY
+ */
+struct phy_info *mii_phy_get_info(int bus, int id)
+{
+	u16 phy_reg;
+	u32 phy_ID;
+	int i;
+	struct phy_info *theInfo = NULL;

-> s/theInfo/info/ ?

drivers/net/mii_bus.c

Quoted 2 lines
drivers/net/mii_bus.c new file +2 -0
+	if (mii_bus[bus] == NULL)
+		return NULL;

-> This function is not exported and the caller has already
issued a VALID_BUS.

[...]

drivers/net/mii_bus.c line 497

Quoted 5 lines
drivers/net/mii_bus.c new file +5 -0
+	/* loop through all the known PHY types, and find one that */
+	/* matches the ID we read from the PHY. */
+	for (i = 0; phy_info[i]; i++)
+		if (phy_info[i]->id == (phy_ID >> phy_info[i]->shift))
+			theInfo = phy_info[i];

-> add braces around the for block + break ?

drivers/net/mii_bus.c line 501

Quoted 4 lines
drivers/net/mii_bus.c new file +4 -0
+
+	if (theInfo == NULL) {
+		printk("phy %d.%d: PHY id 0x%x is not supported!\n", bus, id, phy_ID);
+		return NULL;

-> no need to return here.

Quoted 7 lines
drivers/net/mii_bus.c new file +7 -0
+	} else {
+		printk("phy %d.%d: PHY is %s (%x)\n", bus, id, theInfo->name,
+		       phy_ID);
+	}
+
+	return theInfo;
+}

[...]

Quoted 10 lines
drivers/net/mii_bus.c new file +10 -0
+int mii_phy_attach(struct mii_if_info *mii, struct net_device *dev, int bus, int id)
+{
+	struct phy_info *phy,*info;
+
+	if (mii_bus[bus] == NULL) {
+		printk(KERN_ERR "mii_phy_attach: Can't attach %s, no MII bus %d present\n",dev->name,bus);
+		return -ENODEV;
+	}
+
+	if (VALID(bus,id)) {

drivers/net/mii_bus.c line 737

Quoted 10 lines
drivers/net/mii_bus.c new file +10 -0
+		printk(KERN_ERR "mii_phy_attach: PHY %d.%d is already attached to %s\n",bus,id,dev->name);
+		return -EBUSY;
+	}
+
+	info = mii_phy_get_info(bus, id);
+	if (info == NULL)
+		return -ENODEV;
+
+	phy = kmalloc(sizeof(*phy), GFP_KERNEL);
+	memcpy(phy,info,sizeof(*phy));

-> kmalloc can fail.

[...]

Quoted 7 lines
drivers/net/mii_bus.c new file +7 -0
+int mii_bus_register(struct mii_bus *bus)
+{
+	int bus_id;
+
+	if (bus == NULL || bus->name == NULL || bus->read == NULL ||
+	    bus->write == NULL)
+		return -EINVAL;

[...]

drivers/net/mii_bus.c line 830

Quoted 3 lines
drivers/net/mii_bus.c new file +3 -0
+	mii_bus[bus_id] = bus;
+
+	if (mii_bus[bus_id] == NULL) {

-> bus is not NULL, neither is mii_bus[bus_id] (see above).

drivers/net/mii_bus.c line 836

Quoted 6 lines
drivers/net/mii_bus.c new file +6 -0
+		up(&mii_bus_lock);
+		return -EINVAL;
+	}
+
+	if (mii_bus[bus_id]->reset)
+		mii_bus[bus_id]->reset(mii_bus[bus_id]->priv);
	if (bus->reset)
		bus->reset(bus->priv);

Please, pretty please, more polish (I did not say that it could be done instantly :o) ).

--
Ueimor

#3 lore raw
In reply to Jason McMullan
Quoted 9 lines · Jason McMullan

Second attempt, more polish.

(Though I'm leaving my macro abuse in for now!)

Description: MII Bus interface
Date:        Thu, 11 Nov 2004 17:44:57 -0500

Depends:

	linux-2.6.9

Have you looked at what sungem/sungem_phy does ?

Among others, sungem has an algorithm for automatically testing fallback forced speeds when aneg fails, which has proven useful with a variety of PHY/hub combos, plus a "magic_aneg" bit in the PHY definition for PHYs taht can do that themselves automatically.

Also, besides shutdown(), you probably want a suspend() callback used by the MAC driver when the machine is entering a suspend() state (I definitely need that with various PHYs on powermacs) along with the various WOL parameters.

Ben.

#4 lore raw
Quoted 6 lines · Benjamin Herrenschmidt

Have you looked at what sungem/sungem_phy does ?

Among others, sungem has an algorithm for automatically testing fallback forced speeds when aneg fails, which has proven useful with a variety of PHY/hub combos, plus a "magic_aneg" bit in the PHY definition for PHYs taht can do that themselves automatically.

I'll look into it.

Quoted 4 lines

Also, besides shutdown(), you probably want a suspend() callback used by the MAC driver when the machine is entering a suspend() state (I definitely need that with various PHYs on powermacs) along with the various WOL parameters.

For the 'wake on lan' stuff, could you give me a list of the
types of features you'd need? I haven't really looked at WOL just yet.

We can add WOL, suspend, etc. as needed. I just want to
get the base infrastructure in first, and gradually start migrating phys to the mii_bus on embedded systems.

Signature · 1 line
Jason McMullan <[email protected]>
#5 lore raw
Quoted 2 lines · Jason McMullan

For the 'wake on lan' stuff, could you give me a list of the
types of features you'd need? I haven't really looked at WOL just yet.

Me neither, though Colin Leroy has, I'll check out his stuff next week.

Quoted 4 lines

We can add WOL, suspend, etc. as needed. I just want to
get the base infrastructure in first, and gradually start migrating phys to the mii_bus on embedded systems.

Signature · 1 line
Benjamin Herrenschmidt <[email protected]>