All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ivan Vecera <ivecera@redhat.com>
To: "Ilpo Järvinen" <ilpo.jarvinen@helsinki.fi>,
	"Francois Romieu" <romieu@fr.zoreil.com>
Cc: Netdev <netdev@vger.kernel.org>, Edward Hsu <edward_hsu@realtek.com.tw>
Subject: Re: [PATCH] r8169: read MAC address from EEPROM on init
Date: Fri, 19 Sep 2008 15:05:05 +0200	[thread overview]
Message-ID: <48D3A381.1080500@redhat.com> (raw)
In-Reply-To: <Pine.LNX.4.64.0809182235550.4742@wrl-59.cs.helsinki.fi>

Ilpo Järvinen wrote:
>> +	 * is always consecutive 4-byte data starting from the VPD address
>> +	 * specified."
>> +	 */
>> +	if (rtl_eeprom_read(tp->pci_dev, vpd_cap, 0x000e, &low) < 0 ||
>> +	    rtl_eeprom_read(tp->pci_dev, vpd_cap, 0x0012, &high) < 0) {
>> +		dprintk("Reading MAC address from EEPROM failed\n");
>> +		return;
>> +	}
>> +
>> +	/* Mask hi-word */
>> +	high &= 0xffff;
> 
> ..Hmm, and besides fixing sparse printouts, this looks like a real 
> endianness bug.

Yes it is, my mistake. The patch below will be better.

---
 drivers/net/r8169.c |   74 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 73 insertions(+), 1 deletions(-)

diff --git a/drivers/net/r8169.c b/drivers/net/r8169.c
index befc927..cff8dd6 100644
--- a/drivers/net/r8169.c
+++ b/drivers/net/r8169.c
@@ -1910,6 +1910,75 @@ static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
 	}
 }

+static int rtl_eeprom_read(struct pci_dev *pdev, int cap, int addr, __le32 *val)
+{
+	int ret, count = 100;
+	u16 status = 0;
+	u32 value;
+
+	ret = pci_write_config_word(pdev, cap + PCI_VPD_ADDR, addr);
+	if (ret < 0)
+		return ret;
+
+	do {
+		udelay(10);
+		ret = pci_read_config_word(pdev, cap + PCI_VPD_ADDR, &status);
+		if (ret < 0)
+			return ret;
+	} while (!(status & PCI_VPD_ADDR_F) && --count);
+
+	if (!(status & PCI_VPD_ADDR_F))
+		return -ETIMEDOUT;
+
+	ret = pci_read_config_dword(pdev, cap + PCI_VPD_DATA, &value);
+	if (ret < 0)
+		return ret;
+
+	*val = cpu_to_le32(value);
+
+	return 0;
+}
+
+static void rtl_init_mac_address(struct rtl8169_private *tp,
+				 void __iomem *ioaddr)
+{
+	struct pci_dev *pdev = tp->pci_dev;
+	u8 cfg1;
+	int vpd_cap;
+	u8 mac[8];
+
+	cfg1 = RTL_R8(Config1);
+	if (!(cfg1  & VPD)) {
+		dprintk("VPD access not enabled, enabling\n");
+		RTL_W8(Cfg9346, Cfg9346_Unlock);
+		RTL_W8(Config1, cfg1 | VPD);
+		RTL_W8(Cfg9346, Cfg9346_Lock);
+	}
+
+	vpd_cap = pci_find_capability(pdev, PCI_CAP_ID_VPD);
+	if (!vpd_cap)
+		return;
+
+	/* MAC address is stored in EEPROM at offset 0x0e
+	 * Realtek says: "The VPD address does not have to be a DWORD-aligned
+	 * address as defined in the PCI 2.2 Specifications, but the VPD data
+	 * is always consecutive 4-byte data starting from the VPD address
+	 * specified."
+	 */
+	if (rtl_eeprom_read(pdev, vpd_cap, 0x000e, (__le32*)&mac[0]) < 0 ||
+	    rtl_eeprom_read(pdev, vpd_cap, 0x0012, (__le32*)&mac[4]) < 0) {
+		dprintk("Reading MAC address from EEPROM failed\n");
+		return;
+	}
+
+	dprintk("MAC address found in EEPROM: "
+		"%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n",
+		mac[0], mac[1], mac[2], mac[3], mac[4], mac[5], mac[6]);
+
+	/* Write MAC address */
+	rtl_rar_set(tp, mac);
+}
+
 static int __devinit
 rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
 {
@@ -2079,7 +2148,10 @@ rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
 		dev->do_ioctl = rtl8169_ioctl;
 	}

-	/* Get MAC address.  FIXME: read EEPROM */
+	/* Read MAC address from EEPROM */
+	rtl_init_mac_address(tp, ioaddr);
+
+	/* Get MAC address */
 	for (i = 0; i < MAC_ADDR_LEN; i++)
 		dev->dev_addr[i] = RTL_R8(MAC0 + i);
 	memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);


  reply	other threads:[~2008-09-19 13:08 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-09-18 13:46 [PATCH] r8169: read MAC address from EEPROM on init Ivan Vecera
2008-09-18 19:34 ` Ilpo Järvinen
2008-09-18 19:53 ` Ilpo Järvinen
2008-09-19 13:05   ` Ivan Vecera [this message]
2008-09-20 22:04     ` Ilpo Järvinen
2008-09-24  8:46       ` Ivan Vecera
2008-09-24 21:10         ` Francois Romieu
2008-09-25  9:38           ` Ivan Vecera
2008-09-25 10:24             ` Ben Hutchings
2008-09-25 11:04               ` Ivan Vecera
2008-09-25 16:44                 ` Stephen Hemminger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=48D3A381.1080500@redhat.com \
    --to=ivecera@redhat.com \
    --cc=edward_hsu@realtek.com.tw \
    --cc=ilpo.jarvinen@helsinki.fi \
    --cc=netdev@vger.kernel.org \
    --cc=romieu@fr.zoreil.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.