Module Name:    src
Committed By:   bouyer
Date:           Wed Sep 18 20:00:53 UTC 2013

Modified Files:
        src/sys/arch/sandpoint/stand/altboot [netbsd-6]: brdsetup.c rge.c
        src/sys/dev/ic [netbsd-6]: rtl8169.c

Log Message:
Pull up following revision(s) (requested by phx in ticket #946):
        sys/arch/sandpoint/stand/altboot/rge.c: revision 1.7
        sys/arch/sandpoint/stand/altboot/brdsetup.c: revision 1.32
        sys/dev/ic/rtl8169.c: revision 1.138
QNAP V200 boards have no EEPROM for the MAC address, so all devices default
to the same address (00:e0:4c:69:20:01).
Now we read the real MAC address from the flash ROM. It is stored at the
beginning of a 512-byte block in ASCII format. Some QNAP's have a broken
ext2 file system, so we cannot look for the file ETH0.MAC_ADDR therein,
but have to search the whole flash in 512-byte steps for candidates...
Make re(4) driver always use IDR register values for its MAC address.
Some sandpoint NAS firmwares set MAC address per their
firmware settings and don't use re(4)'s EEPROM values.
Per rtl8169 manuals re(4) chip reads EEPROM automatically after
hardware reset and Linux driver also uses IDR registers,
so this change should not affect existing other boards
which actually have vaild EEPROM.
Per discussion in old tech-kern@ thread:
http://mail-index.netbsd.org/tech-kern/2012/12/01/msg014573.html
Note rtl81x9.c is still shared among rtk(4) only for a multicast function
(to avoid boring refactoring work).


To generate a diff of this commit:
cvs rdiff -u -r1.27 -r1.27.2.1 \
    src/sys/arch/sandpoint/stand/altboot/brdsetup.c
cvs rdiff -u -r1.6 -r1.6.8.1 src/sys/arch/sandpoint/stand/altboot/rge.c
cvs rdiff -u -r1.134.4.2 -r1.134.4.3 src/sys/dev/ic/rtl8169.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/sandpoint/stand/altboot/brdsetup.c
diff -u src/sys/arch/sandpoint/stand/altboot/brdsetup.c:1.27 src/sys/arch/sandpoint/stand/altboot/brdsetup.c:1.27.2.1
--- src/sys/arch/sandpoint/stand/altboot/brdsetup.c:1.27	Sat Jan 14 22:36:54 2012
+++ src/sys/arch/sandpoint/stand/altboot/brdsetup.c	Wed Sep 18 20:00:53 2013
@@ -1,4 +1,4 @@
-/* $NetBSD: brdsetup.c,v 1.27 2012/01/14 22:36:54 phx Exp $ */
+/* $NetBSD: brdsetup.c,v 1.27.2.1 2013/09/18 20:00:53 bouyer Exp $ */
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -1181,6 +1181,41 @@ read_mac_string(uint8_t *mac, char *p)
 }
 
 /*
+ * Scan through the Flash memory and look for a string starting at 512 bytes
+ * block boundaries, matching the format: xx:xx:xx:xx:xx:xx<NUL>, where "x"
+ * are hexadecimal digits.
+ * Read the first match as our MAC address.
+ * The start address of the search, p, *must* be dividable by 512!
+ * Return false when no suitable MAC string was found.
+ */
+static int
+find_mac_string(uint8_t *mac, char *p)
+{
+	int i;
+
+	for (;;) {
+		for (i = 0; i < 3 * 6; i += 3) {
+			if (!isxdigit((unsigned)p[i]) ||
+			    !isxdigit((unsigned)p[i + 1]))
+				break;
+			if ((i < 5 && p[i + 2] != ':') ||
+			    (i >= 5 && p[i + 2] != '\0'))
+				break;
+		}
+		if (i >= 6) {
+			/* found a valid MAC address */
+			read_mac_string(mac, p);
+			return 1;
+		}
+		if (p >= (char *)0xfffffe00)
+			break;
+		p += 0x200;
+	}
+	return 0;
+}
+
+
+/*
  * For cost saving reasons some NAS boxes lack SEEPROM for NIC's
  * ethernet address and keep it in their Flash memory instead.
  */
@@ -1199,6 +1234,10 @@ read_mac_from_flash(uint8_t *mac)
 	case BRD_DLINKDSM:
 		read_mac_string(mac, (char *)0xfff0ff80);
 		return;
+	case BRD_QNAPTS:
+		if (find_mac_string(mac, (char *)0xfff00000))
+			return;
+		break;
 	default:
 		printf("Warning: This board has no known method defined "
 		    "to determine its MAC address!\n");

Index: src/sys/arch/sandpoint/stand/altboot/rge.c
diff -u src/sys/arch/sandpoint/stand/altboot/rge.c:1.6 src/sys/arch/sandpoint/stand/altboot/rge.c:1.6.8.1
--- src/sys/arch/sandpoint/stand/altboot/rge.c:1.6	Sun Oct 30 21:08:33 2011
+++ src/sys/arch/sandpoint/stand/altboot/rge.c	Wed Sep 18 20:00:53 2013
@@ -1,4 +1,4 @@
-/* $NetBSD: rge.c,v 1.6 2011/10/30 21:08:33 phx Exp $ */
+/* $NetBSD: rge.c,v 1.6.8.1 2013/09/18 20:00:53 bouyer Exp $ */
 
 /*-
  * Copyright (c) 2007 The NetBSD Foundation, Inc.
@@ -102,6 +102,9 @@ struct desc {
 #define	 RCR_AM		(1U << 2)	/* accept multicast frame */
 #define	 RCR_APM	(1U << 1)	/* accept unicast frame */
 #define	 RCR_AAP	(1U << 0)	/* promiscuous */
+#define RGE_EECMD	0x50		/* EEPROM command register */
+#define  EECMD_LOCK	0x00
+#define  EECMD_UNLOCK	0xc0
 #define RGE_PHYAR	0x60		/* PHY access */
 #define RGE_PHYSR	0x6c		/* PHY status */
 #define RGE_RMS		0xda		/* Rx maximum frame size */
@@ -146,7 +149,8 @@ rge_init(unsigned tag, void *data)
 	unsigned val;
 	struct local *l;
 	struct desc *txd, *rxd;
-	uint8_t *en = data;
+	uint32_t reg;
+	uint8_t *en;
 
 	l = ALLOC(struct local, 256);	/* desc alignment */
 	memset(l, 0, sizeof(struct local));
@@ -158,14 +162,27 @@ rge_init(unsigned tag, void *data)
 	} while (val & CR_RESET);
 
 	mii_initphy(l);
-
 	en = data;
-	en[0] = CSR_READ_1(l, RGE_IDR0);
-	en[1] = CSR_READ_1(l, RGE_IDR1);
-	en[2] = CSR_READ_1(l, RGE_IDR2);
-	en[3] = CSR_READ_1(l, RGE_IDR3);
-	en[4] = CSR_READ_1(l, RGE_IDR4);
-	en[5] = CSR_READ_1(l, RGE_IDR5);
+
+	if (brdtype == BRD_QNAPTS) {
+		/* read the MAC from flash and write it into the ID-Regs */
+		read_mac_from_flash(en);
+
+		CSR_WRITE_1(l, RGE_EECMD, EECMD_UNLOCK);
+		reg = en[0] | (en[1] << 8) | (en[2] << 16) | (en[3] << 24);
+		CSR_WRITE_4(l, RGE_IDR0, reg);
+		reg = en[4] | (en[5] << 8);
+		CSR_WRITE_4(l, RGE_IDR4, reg);
+		CSR_WRITE_1(l, RGE_EECMD, EECMD_LOCK);
+	} else {
+		/* pretent the ID-Regs have the correct address */
+		en[0] = CSR_READ_1(l, RGE_IDR0);
+		en[1] = CSR_READ_1(l, RGE_IDR1);
+		en[2] = CSR_READ_1(l, RGE_IDR2);
+		en[3] = CSR_READ_1(l, RGE_IDR3);
+		en[4] = CSR_READ_1(l, RGE_IDR4);
+		en[5] = CSR_READ_1(l, RGE_IDR5);
+	}
 
 	printf("MAC address %02x:%02x:%02x:%02x:%02x:%02x\n",
 	    en[0], en[1], en[2], en[3], en[4], en[5]);

Index: src/sys/dev/ic/rtl8169.c
diff -u src/sys/dev/ic/rtl8169.c:1.134.4.2 src/sys/dev/ic/rtl8169.c:1.134.4.3
--- src/sys/dev/ic/rtl8169.c:1.134.4.2	Mon Jul 29 08:11:53 2013
+++ src/sys/dev/ic/rtl8169.c	Wed Sep 18 20:00:53 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: rtl8169.c,v 1.134.4.2 2013/07/29 08:11:53 msaitoh Exp $	*/
+/*	$NetBSD: rtl8169.c,v 1.134.4.3 2013/09/18 20:00:53 bouyer Exp $	*/
 
 /*
  * Copyright (c) 1997, 1998-2003
@@ -33,7 +33,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: rtl8169.c,v 1.134.4.2 2013/07/29 08:11:53 msaitoh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: rtl8169.c,v 1.134.4.3 2013/09/18 20:00:53 bouyer Exp $");
 /* $FreeBSD: /repoman/r/ncvs/src/sys/dev/re/if_re.c,v 1.20 2004/04/11 20:34:08 ru Exp $ */
 
 /*
@@ -554,9 +554,8 @@ void
 re_attach(struct rtk_softc *sc)
 {
 	uint8_t eaddr[ETHER_ADDR_LEN];
-	uint16_t val;
 	struct ifnet *ifp;
-	int error = 0, i, addr_len;
+	int error = 0, i;
 
 	if ((sc->sc_quirk & RTKQ_8139CPLUS) == 0) {
 		uint32_t hwrev;
@@ -644,6 +643,12 @@ re_attach(struct rtk_softc *sc)
 	/* Reset the adapter. */
 	re_reset(sc);
 
+	/*
+	 * RTL81x9 chips automatically read EEPROM to init MAC address,
+	 * and some NAS override its MAC address per own configuration,
+	 * so no need to explicitely read EEPROM and set ID registers.
+	 */
+#ifdef RE_USE_EECMD
 	if ((sc->sc_quirk & RTKQ_NOEECMD) != 0) {
 		/*
 		 * Get station address from ID registers.
@@ -651,6 +656,9 @@ re_attach(struct rtk_softc *sc)
 		for (i = 0; i < ETHER_ADDR_LEN; i++)
 			eaddr[i] = CSR_READ_1(sc, RTK_IDR0 + i);
 	} else {
+		uint16_t val;
+		int addr_len;
+
 		/*
 		 * Get station address from the EEPROM.
 		 */
@@ -668,6 +676,13 @@ re_attach(struct rtk_softc *sc)
 			eaddr[(i * 2) + 1] = val >> 8;
 		}
 	}
+#else
+	/*
+	 * Get station address from ID registers.
+	 */
+	for (i = 0; i < ETHER_ADDR_LEN; i++)
+		eaddr[i] = CSR_READ_1(sc, RTK_IDR0 + i);
+#endif
 
 	/* Take PHY out of power down mode. */
 	if ((sc->sc_quirk & RTKQ_PHYWAKE_PM) != 0)
@@ -1726,11 +1741,13 @@ static int
 re_init(struct ifnet *ifp)
 {
 	struct rtk_softc *sc = ifp->if_softc;
-	const uint8_t *enaddr;
 	uint32_t rxcfg = 0;
-	uint32_t reg;
 	uint16_t cfg;
 	int error;
+#ifdef RE_USE_EECMD
+	const uint8_t *enaddr;
+	uint32_t reg;
+#endif
 
 	if ((error = re_enable(sc)) != 0)
 		goto out;
@@ -1775,6 +1792,7 @@ re_init(struct ifnet *ifp)
 
 	DELAY(10000);
 
+#ifdef RE_USE_EECMD
 	/*
 	 * Init our MAC address.  Even though the chipset
 	 * documentation doesn't mention it, we need to enter "Config
@@ -1788,6 +1806,7 @@ re_init(struct ifnet *ifp)
 	reg = enaddr[4] | (enaddr[5] << 8);
 	CSR_WRITE_4(sc, RTK_IDR4, reg);
 	CSR_WRITE_1(sc, RTK_EECMD, RTK_EEMODE_OFF);
+#endif
 
 	/*
 	 * For C+ mode, initialize the RX descriptors and mbufs.

Reply via email to