Skip to contentSkip to conversation

Gigabit Ethernet driver of Topcliff PCH

PATCH Superseded Masayuki Ohtake 4 participants 7 messages Sep 10, 2010 – Sep 15, 2010

Subsystems (2)

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 Gigabit Ethernet driver of Topcliff PCH

Hi Fujita, Jili and Stephen

I have modified for your comment.
Please confirm below.

On: Thu, 26 Aug 2010 08:57:58 -0700

Quoted 44 lines · Stephen Hemminger
Quoted 29 lines
(quoted diff) +29 -0
+	/* Performs checksum processing */
+	/*
+	 * It is because the hardware accelerator does not support a checksum,
+	 * when the received data size is less than 64 bytes.
+	 */
+	if ((skb->len < PCH_GBE_SHORT_PKT) && (adapter->tx_csum == TRUE)) {
+		frame_ctrl |=
+			PCH_GBE_TXD_CTRL_APAD | PCH_GBE_TXD_CTRL_TCPIP_ACC_OFF;
+		if (skb->protocol == htons(ETH_P_IP)) {
+			struct iphdr *iph = ip_hdr(skb);
+			unsigned int offset;
+			iph->check = 0;
+			iph->check = ip_fast_csum((u8 *) iph, iph->ihl);
+			offset = skb_transport_offset(skb);
+			if (iph->protocol == IPPROTO_TCP) {
+				skb->csum = 0;
+				tcp_hdr(skb)->check = 0;
+				skb->csum =
+					skb_checksum(skb, offset,
+						skb->len - offset, 0);
+				tcp_hdr(skb)->check =
+					csum_tcpudp_magic(iph->saddr,
+							iph->daddr,
+							skb->len - offset,
+							IPPROTO_TCP, skb->csum);
+			} else if (iph->protocol == IPPROTO_UDP) {
+				skb->csum = 0;
+				udp_hdr(skb)->check = 0;
+				skb->csum =

Review

Quoted 10 lines
(quoted diff) +10 -0
+					skb_checksum(skb, offset,
+						skb->len - offset, 0);
+				udp_hdr(skb)->check =
+					csum_tcpudp_magic(iph->saddr,
+							iph->daddr,
+							skb->len - offset,
+							IPPROTO_UDP, skb->csum);
+			}
+		}
+	}

Why not something simpler using skb_checksum_help which is safer since it handles case of shared skb correctly.

Quoted 8 lines
	if (skb->ip_summed = CHECKSUM_COMPLETE &&
           skb->len < PCH_GBE_SHORT_PKT) {
		frame_ctrl |=
			PCH_GBE_TXD_CTRL_APAD | PCH_GBE_TXD_CTRL_TCPIP_ACC_OFF;
		skb->ip_summed = CHECKSUM_NONE;
               if (skb_checksum_help(skb))
                     goto drop;  /* could not expand header to find space */
       }

Since a driver performs a checksam Instead of the checksum of HW, It modified as follows.
However, it did not move normally.
So, modification was given back.

+ if ((skb->len < PCH_GBE_SHORT_PKT) && (adapter->tx_csum)) {
+ frame_ctrl |= PCH_GBE_TXD_CTRL_APAD |
+ PCH_GBE_TXD_CTRL_TCPIP_ACC_OFF;
+ if (skb->protocol == htons(ETH_P_IP)) {
+ if ((ip_hdr(skb)->protocol == IPPROTO_TCP) ||
+ (ip_hdr(skb)->protocol == IPPROTO_UDP)) {
+ skb->ip_summed = CHECKSUM_NONE;
+ if (skb_checksum_help(skb)) {
+ dev_kfree_skb_any(skb);
+ adapter->stats.tx_dropped++;
+ return;
+ }
+ }
+ }
+ }

---
 drivers/net/Kconfig                   |   12 +
 drivers/net/Makefile                  |    1 +
 drivers/net/pch_gbe/Makefile          |    4 +
 drivers/net/pch_gbe/pch_gbe.h         |  661 +++++++++
 drivers/net/pch_gbe/pch_gbe_api.c     |  245 ++++
 drivers/net/pch_gbe/pch_gbe_api.h     |   36 +
 drivers/net/pch_gbe/pch_gbe_ethtool.c |  587 ++++++++
 drivers/net/pch_gbe/pch_gbe_main.c    | 2526 +++++++++++++++++++++++++++++++++
 drivers/net/pch_gbe/pch_gbe_param.c   |  501 +++++++
 drivers/net/pch_gbe/pch_gbe_phy.c     |  274 ++++
 drivers/net/pch_gbe/pch_gbe_phy.h     |   37 +
 11 files changed, 4884 insertions(+), 0 deletions(-)
 create mode 100644 drivers/net/pch_gbe/Makefile
 create mode 100644 drivers/net/pch_gbe/pch_gbe.h
 create mode 100644 drivers/net/pch_gbe/pch_gbe_api.c
 create mode 100644 drivers/net/pch_gbe/pch_gbe_api.h
 create mode 100644 drivers/net/pch_gbe/pch_gbe_ethtool.c
 create mode 100644 drivers/net/pch_gbe/pch_gbe_main.c
 create mode 100644 drivers/net/pch_gbe/pch_gbe_param.c
 create mode 100644 drivers/net/pch_gbe/pch_gbe_phy.c
 create mode 100644 drivers/net/pch_gbe/pch_gbe_phy.h
drivers/net/Kconfig +12 -0
index 2decc59..cf3aff8 100644
@@ -2502,6 +2502,18 @@ config S6GMAC
 
 source "drivers/net/stmmac/Kconfig"
 
+config PCH_GBE
+	tristate "PCH Gigabit Ethernet"
+	depends on PCI
+	---help---
+	  This is a gigabit ethernet driver for Topcliff PCH.
+	  Topcliff PCH is the platform controller hub that is used in Intel's
+	  general embedded platform.
+	  Topcliff PCH has Gigabit Ethernet interface.
+	  Using this interface, it is able to access system devices connected
+	  to Gigabit Ethernet.
+	  This driver enables Gigabit Ethernet function.
+
 endif # NETDEV_1000
 
 #
drivers/net/Makefile +1 -0
index 0a0512a..3df2467 100644
@@ -295,3 +295,4 @@ obj-$(CONFIG_WIMAX) += wimax/
 obj-$(CONFIG_CAIF) += caif/
 
 obj-$(CONFIG_OCTEON_MGMT_ETHERNET) += octeon/
+obj-$(CONFIG_PCH_GBE) += pch_gbe/
drivers/net/pch_gbe/Makefile new file +4 -0
new file mode 100644
index 0000000..31288d4
@@ -0,0 +1,4 @@
+obj-$(CONFIG_PCH_GBE) += pch_gbe.o
+
+pch_gbe-y := pch_gbe_phy.o pch_gbe_ethtool.o pch_gbe_param.o
+pch_gbe-y += pch_gbe_api.o pch_gbe_main.o
drivers/net/pch_gbe/pch_gbe.h new file +661 -0

661 lines not on this page · raw message

drivers/net/pch_gbe/pch_gbe_api.c new file +245 -0

245 lines not on this page · raw message

drivers/net/pch_gbe/pch_gbe_api.h new file +36 -0
new file mode 100644
index 0000000..94aaac5
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 1999 - 2010 Intel Corporation.
+ * Copyright (C) 2010 OKI SEMICONDUCTOR Co., LTD.
+ *
+ * This code was derived from the Intel e1000e Linux driver.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
+ */
+#ifndef _PCH_GBE_API_H_
+#define _PCH_GBE_API_H_
+
+#include "pch_gbe_phy.h"
+
+s32 pch_gbe_hal_setup_init_funcs(struct pch_gbe_hw *hw);
+void pch_gbe_hal_get_bus_info(struct pch_gbe_hw *hw);
+s32 pch_gbe_hal_init_hw(struct pch_gbe_hw *hw);
+s32 pch_gbe_hal_read_phy_reg(struct pch_gbe_hw *hw, u32 offset, u16 *data);
+s32 pch_gbe_hal_write_phy_reg(struct pch_gbe_hw *hw, u32 offset, u16 data);
+void pch_gbe_hal_phy_hw_reset(struct pch_gbe_hw *hw);
+void pch_gbe_hal_phy_sw_reset(struct pch_gbe_hw *hw);
+s32 pch_gbe_hal_read_mac_addr(struct pch_gbe_hw *hw);
+void pch_gbe_hal_power_up_phy(struct pch_gbe_hw *hw);
+void pch_gbe_hal_power_down_phy(struct pch_gbe_hw *hw);
+
+#endif
drivers/net/pch_gbe/pch_gbe_ethtool.c new file +587 -0

587 lines not on this page · raw message

drivers/net/pch_gbe/pch_gbe_main.c new file +2526 -0

1,406 lines not on this page · raw message

drivers/net/pch_gbe/pch_gbe_param.c new file +501 -0

501 lines not on this page · raw message

drivers/net/pch_gbe/pch_gbe_phy.c new file +274 -0

274 lines not on this page · raw message

drivers/net/pch_gbe/pch_gbe_phy.h new file +37 -0
new file mode 100644
index 0000000..03264dc

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Signature · 1 line
1.6.2.5

Part of the diff is not shown here. View the raw message.

#1 lore raw

drivers/net/pch_gbe/pch_gbe_ethtool.c

Quoted 1 line · Masayuki Ohtake
drivers/net/pch_gbe/pch_gbe_ethtool.c +0 -0

[]

drivers/net/pch_gbe/pch_gbe_ethtool.c line 183

Quoted 11 lines
drivers/net/pch_gbe/pch_gbe_ethtool.c +11 -0
+static void pch_gbe_get_regs(struct net_device *netdev,
+				struct ethtool_regs *regs, void *p)
+{
+	struct pch_gbe_adapter *adapter = netdev_priv(netdev);
+	struct pch_gbe_hw *hw = &adapter->hw;
+	struct pci_dev *pdev = adapter->pdev;
+	u32 *regs_buff = p;
+	u16 i, reg, tmp;
+
+	regs->version = pdev->revision;
+	regs->version = 0x1000000 | (regs->version << 16) | pdev->device;

Might be simpler as:

	regs->version = 0x1000000 | (__u32)pdev->revision << 16 | pdev->device;

The block below is a bit confusing to me.

drivers/net/pch_gbe/pch_gbe_ethtool.c line 184

Quoted 1 line
drivers/net/pch_gbe/pch_gbe_ethtool.c +1 -0
+	memset(p, 0, PCH_GBE_REGS_LEN * (int)sizeof(u32));

It seems the memset is unnecessary as it's completely overwritten below.

drivers/net/pch_gbe/pch_gbe_ethtool.c line 192

Quoted 8 lines
drivers/net/pch_gbe/pch_gbe_ethtool.c +8 -0
+	for (i = 0; i < PCH_GBE_MAC_REGS_LEN; i++)
+		regs_buff[i] = ioread32(&hw->reg->INT_ST + i);
+	/* PHY register */
+	for (i = PCH_GBE_MAC_REGS_LEN, reg = 0; reg < PCH_GBE_PHY_REGS_LEN;
+	     i++, reg++) {
+		pch_gbe_hal_read_phy_reg(&adapter->hw, reg, &tmp);
+		regs_buff[i] = tmp;
+	}

I think i'd be simpler to do something like:

	for (i = 0; i < PCH_GBE_MAC_REGS_LEN; i++)
		*regs_buff++ = ioread32(&hw->reg->INT_ST + i);

	/* PHY register */
	for (i = 0; i < PCH_GBE_PHY_REGS_LEN; i++) {
		pch_gbe_hal_read_phy_reg(&adapter->hw, i, &tmp);
		*regs_buff++ = tmp;
	}

[]

+void pch_gbe_mac_mar_set(struct pch_gbe_hw *hw, u8 * addr, u32 index)

Quoted 15 lines
drivers/net/pch_gbe/pch_gbe_ethtool.c +15 -0
+{
+	u32 mar_low, mar_high, adrmask;
+
+	pr_debug("index : 0x%x\n", index);
+
+	/*
+	 * HW expects these in little endian so we reverse the byte order
+	 * from network order (big endian) to little endian
+	 */
+	mar_high = ((u32) addr[0] | ((u32) addr[1] << 8) |
+		   ((u32) addr[2] << 16) | ((u32) addr[3] << 24));
+	mar_low = ((u32) addr[4] | ((u32) addr[5] << 8));
+	/* Stop the MAC Address of index. */
+	adrmask = ioread32(&hw->reg->ADDR_MASK);
+	iowrite32((adrmask | (0x0001 << index)), &hw->reg->ADDR_MASK);

drivers/net/pch_gbe/pch_gbe_ethtool.c

Quoted 10 lines
drivers/net/pch_gbe/pch_gbe_ethtool.c +10 -0
+	/* wait busy */
+	while ((ioread32(&hw->reg->ADDR_MASK) & PCH_GBE_BUSY)) {
+		int tmp = 0;
+		udelay(20);
+		tmp++;
+		if (tmp == 1000) {
+			pr_err("Address mask bit is not cleared\n");
+			break;
+		}
+	}

You need to move the declaration of tmp out of the while.

Do these really need to be busy-waits?

Quoted 6 lines
drivers/net/pch_gbe/pch_gbe_ethtool.c +6 -0
+	/* Set the MAC address to the MAC address 1A/1B register */
+	iowrite32(mar_high, &hw->reg->mac_adr[index].high);
+	iowrite32(mar_low, &hw->reg->mac_adr[index].low);
+	/* Start the MAC address of index */
+	iowrite32((adrmask & ~(0x0001 << index)), &hw->reg->ADDR_MASK);
+	/* wait busy */

drivers/net/pch_gbe/pch_gbe_ethtool.c

Quoted 10 lines
drivers/net/pch_gbe/pch_gbe_ethtool.c +10 -0
+	while ((ioread32(&hw->reg->ADDR_MASK) & PCH_GBE_BUSY)) {
+		int tmp = 0;
+		udelay(20);
+		tmp++;
+		if (tmp == 1000) {
+			pr_err("Address mask bit is not cleared\n");
+			break;
+		}
+	}
+}

Here too. There are more of these too I'm not listing. Perhaps you could review the logic a bit more.

#2 lore raw

Hi Joe

Thank you for your comments.

My reply comments are included in the following. I will resubmit after modified.

Thanks Ohtake
-----
Date: Thu, 09 Sep 2010 23:32:07 -0700
From: "Joe Perches" <[email protected]>

Quoted 17 lines

drivers/net/pch_gbe/pch_gbe_ethtool.c

Quoted 1 line
drivers/net/pch_gbe/pch_gbe_ethtool.c +0 -0

[]

drivers/net/pch_gbe/pch_gbe_ethtool.c line 183

Quoted 11 lines
drivers/net/pch_gbe/pch_gbe_ethtool.c +11 -0
+static void pch_gbe_get_regs(struct net_device *netdev,
+ struct ethtool_regs *regs, void *p)
+{
+ struct pch_gbe_adapter *adapter = netdev_priv(netdev);
+ struct pch_gbe_hw *hw = &adapter->hw;
+ struct pci_dev *pdev = adapter->pdev;
+ u32 *regs_buff = p;
+ u16 i, reg, tmp;
+
+ regs->version = pdev->revision;
+ regs->version = 0x1000000 | (regs->version << 16) | pdev->device;

Might be simpler as:
regs->version = 0x1000000 | (__u32)pdev->revision << 16 | pdev->device;

Quoted 1 line

The block below is a bit confusing to me.

[masa]
This will be modified so.

Quoted 4 lines
Quoted 1 line

+ memset(p, 0, PCH_GBE_REGS_LEN * (int)sizeof(u32));

It seems the memset is unnecessary as it's completely overwritten below.

[masa]
This will be deleted.

Quoted 12 lines
Quoted 9 lines

+ for (i = 0; i < PCH_GBE_MAC_REGS_LEN; i++)
+ regs_buff[i] = ioread32(&hw->reg->INT_ST + i);
+ /* PHY register */
+ for (i = PCH_GBE_MAC_REGS_LEN, reg = 0; reg < PCH_GBE_PHY_REGS_LEN;
+ i++, reg++) {
+ pch_gbe_hal_read_phy_reg(&adapter->hw, reg, &tmp);
+ regs_buff[i] = tmp;
+ }

I think i'd be simpler to do something like:

Quoted 8 lines

for (i = 0; i < PCH_GBE_MAC_REGS_LEN; i++)
*regs_buff++ = ioread32(&hw->reg->INT_ST + i);

/* PHY register */
for (i = 0; i < PCH_GBE_PHY_REGS_LEN; i++) {
pch_gbe_hal_read_phy_reg(&adapter->hw, i, &tmp);
*regs_buff++ = tmp;
}

[masa]
This will be modified so.

Quoted 29 lines

+void pch_gbe_mac_mar_set(struct pch_gbe_hw *hw, u8 * addr, u32 index)

Quoted 15 lines
drivers/net/pch_gbe/pch_gbe_ethtool.c +15 -0
+{
+ u32 mar_low, mar_high, adrmask;
+
+ pr_debug("index : 0x%x\n", index);
+
+ /*
+ * HW expects these in little endian so we reverse the byte order
+ * from network order (big endian) to little endian
+ */
+ mar_high = ((u32) addr[0] | ((u32) addr[1] << 8) |
+    ((u32) addr[2] << 16) | ((u32) addr[3] << 24));
+ mar_low = ((u32) addr[4] | ((u32) addr[5] << 8));
+ /* Stop the MAC Address of index. */
+ adrmask = ioread32(&hw->reg->ADDR_MASK);
+ iowrite32((adrmask | (0x0001 << index)), &hw->reg->ADDR_MASK);

drivers/net/pch_gbe/pch_gbe_ethtool.c

Quoted 10 lines
drivers/net/pch_gbe/pch_gbe_ethtool.c +10 -0
+ /* wait busy */
+ while ((ioread32(&hw->reg->ADDR_MASK) & PCH_GBE_BUSY)) {
+ int tmp = 0;
+ udelay(20);
+ tmp++;
+ if (tmp == 1000) {
+ pr_err("Address mask bit is not cleared\n");
+ break;
+ }
+ }

You need to move the declaration of tmp out of the while.

Quoted 1 line

Do these really need to be busy-waits?

[masa]
The declaration of tmp will be moved.
It is necessary to wait for BUSY by the specification of hardware.

Quoted 18 lines
Quoted 17 lines

+ /* Set the MAC address to the MAC address 1A/1B register */
+ iowrite32(mar_high, &hw->reg->mac_adr[index].high);
+ iowrite32(mar_low, &hw->reg->mac_adr[index].low);
+ /* Start the MAC address of index */
+ iowrite32((adrmask & ~(0x0001 << index)), &hw->reg->ADDR_MASK);
+ /* wait busy */
+ while ((ioread32(&hw->reg->ADDR_MASK) & PCH_GBE_BUSY)) {
+ int tmp = 0;
+ udelay(20);
+ tmp++;
+ if (tmp == 1000) {
+ pr_err("Address mask bit is not cleared\n");
+ break;
+ }
+ }
+}

Quoted 3 lines

Here too. There are more of these too I'm not listing. Perhaps you could review the logic a bit more.

[masa]
This will be modified.

#3 lore raw
In reply to Masayuki Ohtake

Ccing Rafael (please find pch_gbe_resume here)

Quoted 1 line · Masayuki Ohtake

Hi Fujita, Jili and Stephen

Hi, Jili sounds funny :D.

Quoted 3 lines
drivers/net/pch_gbe/pch_gbe_ethtool.c new file +0 -0
@@ -0,0 +1,587 @@

...

Quoted 4 lines
drivers/net/pch_gbe/pch_gbe_ethtool.c new file +4 -0
+static void pch_gbe_get_regs(struct net_device *netdev,
+				struct ethtool_regs *regs, void *p)
+{
+	struct pch_gbe_adapter *adapter = netdev_priv(netdev);

drivers/net/pch_gbe/pch_gbe_ethtool.c line 186

Quoted 10 lines
drivers/net/pch_gbe/pch_gbe_ethtool.c new file +10 -0
+	struct pch_gbe_hw *hw = &adapter->hw;
+	struct pci_dev *pdev = adapter->pdev;
+	u32 *regs_buff = p;
+	u16 i, reg, tmp;
+
+	regs->version = pdev->revision;
+	regs->version = 0x1000000 | (regs->version << 16) | pdev->device;
+	memset(p, 0, PCH_GBE_REGS_LEN * (int)sizeof(u32));
+	for (i = 0; i < PCH_GBE_MAC_REGS_LEN; i++)
+		regs_buff[i] = ioread32(&hw->reg->INT_ST + i);

Well, you switched to ioremap, but left ioread/iowrite*. I wrote you either use pci_iomap+ioread/iowrite32 or pci_ioremap_bar+readl/writel which is faster. (But can be used iff you know it is an MMIO space).

drivers/net/pch_gbe/pch_gbe_ethtool.c line 546

Quoted 12 lines
drivers/net/pch_gbe/pch_gbe_ethtool.c new file +12 -0
+static void pch_gbe_get_ethtool_stats(struct net_device *netdev,
+				  struct ethtool_stats *stats, u64 *data)
+{
+	struct pch_gbe_adapter *adapter = netdev_priv(netdev);
+	int i;
+	const struct pch_gbe_stats *gstats = pch_gbe_gstrings_stats;
+	char *hw_stats = (char *)&adapter->stats;
+
+	pch_gbe_update_stats(adapter);
+	for (i = 0; i < PCH_GBE_GLOBAL_STATS_LEN; i++) {
+		char *p = hw_stats + gstats->offset;
+		data[i] = gstats->size == sizeof(u64) ? *(u64 *)p:(*(u32 *)p);

Actually, how you protect 64-bit stat numbers on 32-bit to be atomic? You change adapter->stats from the irq context and it may need to update 2 words on 32-bit. So instead of having 0x100000000 you can get here 0x1ffffffff or 0x0 since read/write of u64 on 32bit are not atomic and both words may not reach the memory yet. I don't know if that matters -- what are the stats from ethtool used for, but returning invalid values is not nice. Do you need 64bit stats on 32-bit at all? Wouldn't be netdev_stats (they are ulong) enough?

Example:
Given stat = 0xffffffff; on 32-bit stat is composed of stat_hi|stat_lo, compiler generates for instance:
stat_lo++;
if (overflow) (1)
stat_hi++

Then if you read stat from this ethtool function at point (1), you will get 0x0.

Quoted 3 lines
drivers/net/pch_gbe/pch_gbe_ethtool.c new file +3 -0
+		gstats++;
+	}
+}

...

Quoted 3 lines
drivers/net/pch_gbe/pch_gbe_main.c new file +0 -0
@@ -0,0 +1,2526 @@

...

Quoted 16 lines
drivers/net/pch_gbe/pch_gbe_main.c new file +16 -0
+void pch_gbe_mac_mar_set(struct pch_gbe_hw *hw, u8 * addr, u32 index)
+{
+	u32 mar_low, mar_high, adrmask;
+
+	pr_debug("index : 0x%x\n", index);
+
+	/*
+	 * HW expects these in little endian so we reverse the byte order
+	 * from network order (big endian) to little endian
+	 */
+	mar_high = ((u32) addr[0] | ((u32) addr[1] << 8) |
+		   ((u32) addr[2] << 16) | ((u32) addr[3] << 24));
+	mar_low = ((u32) addr[4] | ((u32) addr[5] << 8));
+	/* Stop the MAC Address of index. */
+	adrmask = ioread32(&hw->reg->ADDR_MASK);
+	iowrite32((adrmask | (0x0001 << index)), &hw->reg->ADDR_MASK);

drivers/net/pch_gbe/pch_gbe_main.c line 143

Quoted 10 lines
drivers/net/pch_gbe/pch_gbe_main.c new file +10 -0
+	/* wait busy */
+	while ((ioread32(&hw->reg->ADDR_MASK) & PCH_GBE_BUSY)) {
+		int tmp = 0;
+		udelay(20);
+		tmp++;
+		if (tmp == 1000) {
+			pr_err("Address mask bit is not cleared\n");
+			break;
+		}
+	}

Maybe it would make sense to create a helper for that.

Quoted 4 lines
drivers/net/pch_gbe/pch_gbe_main.c new file +4 -0
+static void pch_gbe_tx_queue(struct pch_gbe_adapter *adapter,
+			      struct pch_gbe_tx_ring *tx_ring,
+			      struct sk_buff *skb)
+{

...

drivers/net/pch_gbe/pch_gbe_main.c line 1047

Quoted 1 line
drivers/net/pch_gbe/pch_gbe_main.c new file +1 -0
+	buffer_info->kernel_skb = skb;

Just curious, why you need ->kernel_skb and ->skb in that structure? Can't you use just ->skb?

Quoted 10 lines
drivers/net/pch_gbe/pch_gbe_main.c new file +10 -0
+int pch_gbe_setup_tx_resources(struct pch_gbe_adapter *adapter,
+				struct pch_gbe_tx_ring *tx_ring)
+{
+	struct pci_dev *pdev = adapter->pdev;
+	struct pch_gbe_tx_desc *tx_desc;
+	int size;
+	int desNo;
+
+	size = (int)sizeof(struct pch_gbe_buffer) * tx_ring->count;
+	tx_ring->buffer_info = vmalloc(size);

drivers/net/pch_gbe/pch_gbe_main.c line 1582

Quoted 10 lines
drivers/net/pch_gbe/pch_gbe_main.c new file +10 -0
+	if (!tx_ring->buffer_info) {
+		pr_err("Unable to allocate memory for the buffer infomation\n");
+		return -ENOMEM;
+	}
+	memset(tx_ring->buffer_info, 0, size);
+
+	tx_ring->size = tx_ring->count * (int)sizeof(struct pch_gbe_tx_desc);
+
+	tx_ring->desc =
+		pci_alloc_consistent(pdev, tx_ring->size, &tx_ring->dma);

As you can sleep here now, you may use dma_alloc_coherent(..., GFP_KERNEL) which allows for better memory reclaim and will fail in less cases. For two reasons:
1) pci_alloc_consistent is deprecated AFAIK
2) pci_alloc_consistent calls page allocator with GFP_ATOMIC and may fail for huge allocations early. And as I computed last time, you allocate up to order-4 pages here.

The same for RX dir.

drivers/net/pch_gbe/pch_gbe_main.c line 1899

Quoted 7 lines
drivers/net/pch_gbe/pch_gbe_main.c new file +7 -0
+static int pch_gbe_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
+{
+	struct pch_gbe_adapter *adapter = netdev_priv(netdev);
+	struct pch_gbe_tx_ring *tx_ring = adapter->tx_ring;
+	unsigned long flags;
+
+	if (unlikely(skb->len <= 0)) {

Can this happen at all? ndev people should tell you better...

drivers/net/pch_gbe/pch_gbe_main.c line 1908

Quoted 9 lines
drivers/net/pch_gbe/pch_gbe_main.c new file +9 -0
+		dev_kfree_skb_any(skb);
+		pr_debug("Return : OK  skb len : %d\n", skb->len);
+		return NETDEV_TX_OK;
+	}
+	if (unlikely(skb->len > (adapter->hw.mac.max_frame_size - 4))) {
+		pr_err("Transfer length Error: skb len: %d > max: %d\n",
+		       skb->len, adapter->hw.mac.max_frame_size);
+		adapter->stats.tx_length_errors++;
+		return NETDEV_TX_BUSY;

Ok, then it's not busy (it will try to send the skb in a loop forever). You should free it and return OK, I think. Anyway, properly set MTU should gurantee that, but it is my guess.

drivers/net/pch_gbe/pch_gbe_main.c line 1939

Quoted 7 lines
drivers/net/pch_gbe/pch_gbe_main.c new file +7 -0
+static struct net_device_stats *pch_gbe_get_stats(
+						struct net_device *netdev)
+{
+	struct pch_gbe_adapter *adapter = netdev_priv(netdev);
+
+	/* only return the current stats */
+	return &adapter->net_stats;

Ah, you have your own copy of netstats. You may use netdev->stats all over the code.

Quoted 7 lines
drivers/net/pch_gbe/pch_gbe_main.c new file +7 -0
+#ifdef CONFIG_PM
+static int pch_gbe_resume(struct pci_dev *pdev)
+{
+	struct net_device *netdev = pci_get_drvdata(pdev);
+	struct pch_gbe_adapter *adapter = netdev_priv(netdev);
+	struct pch_gbe_hw *hw = &adapter->hw;
+	u32 err;

drivers/net/pch_gbe/pch_gbe_main.c line 2287

Quoted 10 lines
drivers/net/pch_gbe/pch_gbe_main.c new file +10 -0
+
+	pci_enable_wake(pdev, PCI_D0, 0);
+	pci_set_power_state(pdev, PCI_D0);
+	pci_restore_state(pdev);
+	err = pci_enable_device(pdev);
+	if (err) {
+		pr_err("Cannot enable PCI device from suspend\n");
+		return err;
+	}
+	pci_set_master(pdev);

Not sure which of those you need. Rafael may tell you more. You should not set power state and restore state, but I might be wrong.

The same you need not to save state from ->suspend.

Quoted 12 lines
drivers/net/pch_gbe/pch_gbe_main.c new file +12 -0
+	pch_gbe_hal_power_up_phy(hw);
+	pch_gbe_reset(adapter);
+	/* Clear wake on lan control and status */
+	pch_gbe_mac_set_wol_event(hw, 0);
+
+	if (netif_running(netdev))
+		pch_gbe_up(adapter);
+	netif_device_attach(netdev);
+
+	return 0;
+}
+#endif /* CONFIG_PM */

...

Quoted 3 lines
drivers/net/pch_gbe/pch_gbe_param.c new file +0 -0
@@ -0,0 +1,501 @@

...

Quoted 1 line

+const struct pch_gbe_opt_list speed_list[] = {

static? And all below...

Quoted 5 lines
drivers/net/pch_gbe/pch_gbe_param.c new file +5 -0
+	{ 0, "" },
+	{ SPEED_10, "" },
+	{ SPEED_100, "" },
+	{ SPEED_1000, "" }
+};

...

drivers/net/pch_gbe/pch_gbe_param.c line 286

Quoted 7 lines
drivers/net/pch_gbe/pch_gbe_param.c new file +7 -0
+static void pch_gbe_check_copper_options(struct pch_gbe_adapter *adapter)
+{
+	struct pch_gbe_hw *hw = &adapter->hw;
+	int speed, dplx;
+
+	{ /* Speed */
+		struct pch_gbe_option opt = {

So may be this static const? And all below...

drivers/net/pch_gbe/pch_gbe_param.c line 296

Quoted 10 lines
drivers/net/pch_gbe/pch_gbe_param.c new file +10 -0
+			.type = list_option,
+			.name = "Speed",
+			.err  = "parameter ignored",
+			.def  = 0,
+			.arg  = { .l = { .nr = (int)ARRAY_SIZE(speed_list),
+					 .p = speed_list } }
+		};
+		speed = Speed;
+		pch_gbe_validate_option(&speed, &opt, adapter);
+	}

regards,

Signature · 2 lines
js
suse labs
#4 lore raw
Quoted 2 lines · Rafael J. Wysocki
Quoted 1 line · Jiri Slaby

Ccing Rafael (please find pch_gbe_resume here)

Thanks.

...

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
drivers/net/pch_gbe/pch_gbe_main.c new file +12 -0
+#ifdef CONFIG_PM
+static int pch_gbe_resume(struct pci_dev *pdev)
+{
+	struct net_device *netdev = pci_get_drvdata(pdev);
+	struct pch_gbe_adapter *adapter = netdev_priv(netdev);
+	struct pch_gbe_hw *hw = &adapter->hw;
+	u32 err;
+
+	pci_enable_wake(pdev, PCI_D0, 0);
+	pci_set_power_state(pdev, PCI_D0);
+	pci_restore_state(pdev);
+	err = pci_enable_device(pdev);
Quoted 8 lines
Quoted 8 lines

drivers/net/pch_gbe/pch_gbe_main.c line 2287

Quoted 5 lines
drivers/net/pch_gbe/pch_gbe_main.c new file +5 -0
+	if (err) {
+		pr_err("Cannot enable PCI device from suspend\n");
+		return err;
+	}
+	pci_set_master(pdev);

Not sure which of those you need. Rafael may tell you more. You should not set power state and restore state, but I might be wrong.

You're basically right. In fact you're supposed to use struct dev_pm_ops for defining PM callbacks and then you don't need do any PCI-specific operations directly in the driver.

All of this is described in detail in Documentation/power/pci.txt and you can have a look into r8169.c for a reference implementation.

Thanks,
Rafael

#5 lore raw

Hi Rafael

Thank you for your comment.
I will confirm pci.txt and r8169.c.

Thanks Ohtake.
-----
Date: Mon, 13 Sep 2010 00:19:26 +0200
From: "Rafael J. Wysocki" <[email protected]>

Quoted 31 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
drivers/net/pch_gbe/pch_gbe_main.c new file +12 -0
+#ifdef CONFIG_PM
+static int pch_gbe_resume(struct pci_dev *pdev)
+{
+ struct net_device *netdev = pci_get_drvdata(pdev);
+ struct pch_gbe_adapter *adapter = netdev_priv(netdev);
+ struct pch_gbe_hw *hw = &adapter->hw;
+ u32 err;
+
+ pci_enable_wake(pdev, PCI_D0, 0);
+ pci_set_power_state(pdev, PCI_D0);
+ pci_restore_state(pdev);
+ err = pci_enable_device(pdev);
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines

+ if (err) {
+ pr_err("Cannot enable PCI device from suspend\n");
+ return err;
+ }
+ pci_set_master(pdev);

Not sure which of those you need. Rafael may tell you more. You should not set power state and restore state, but I might be wrong.

You're basically right. In fact you're supposed to use struct dev_pm_ops for defining PM callbacks and then you don't need do any PCI-specific operations directly in the driver.

All of this is described in detail in Documentation/power/pci.txt and you can have a look into r8169.c for a reference implementation.

Thanks,
Rafael

#6 lore raw
In reply to Jiri Slaby

Hi Jiri

Thank you for your comments.

My reply comments are included in the following. I will resubmit after modified.

Thanks Ohtake
-----
Date: Fri, 10 Sep 2010 10:19:30 +0200
From: "Jiri Slaby" <[email protected]>

Quoted 6 lines

Ccing Rafael (please find pch_gbe_resume here)

Quoted 1 line · Masayuki Ohtake

Hi Fujita, Jili and Stephen

Hi, Jili sounds funny :D.

[masa]
Sorry, :-}

Quoted 15 lines
Quoted 4 lines
drivers/net/pch_gbe/pch_gbe_ethtool.c new file +0 -0
@@ -0,0 +1,587 @@

...

Quoted 10 lines
drivers/net/pch_gbe/pch_gbe_ethtool.c new file +10 -0
+static void pch_gbe_get_regs(struct net_device *netdev,
+ struct ethtool_regs *regs, void *p)
+{
+ struct pch_gbe_adapter *adapter = netdev_priv(netdev);
+ struct pch_gbe_hw *hw = &adapter->hw;
+ struct pci_dev *pdev = adapter->pdev;
+ u32 *regs_buff = p;
+ u16 i, reg, tmp;
+
+ regs->version = pdev->revision;
Quoted 9 lines
Quoted 4 lines

+ regs->version = 0x1000000 | (regs->version << 16) | pdev->device;
+ memset(p, 0, PCH_GBE_REGS_LEN * (int)sizeof(u32));
+ for (i = 0; i < PCH_GBE_MAC_REGS_LEN; i++)
+ regs_buff[i] = ioread32(&hw->reg->INT_ST + i);

Well, you switched to ioremap, but left ioread/iowrite*. I wrote you either use pci_iomap+ioread/iowrite32 or pci_ioremap_bar+readl/writel which is faster. (But can be used iff you know it is an MMIO space).

[masa]
This will be modified. "pci_iomap+ioread/iowrite32" will be used.

Quoted 29 lines

drivers/net/pch_gbe/pch_gbe_ethtool.c line 546

Quoted 12 lines
drivers/net/pch_gbe/pch_gbe_ethtool.c new file +12 -0
+static void pch_gbe_get_ethtool_stats(struct net_device *netdev,
+   struct ethtool_stats *stats, u64 *data)
+{
+ struct pch_gbe_adapter *adapter = netdev_priv(netdev);
+ int i;
+ const struct pch_gbe_stats *gstats = pch_gbe_gstrings_stats;
+ char *hw_stats = (char *)&adapter->stats;
+
+ pch_gbe_update_stats(adapter);
+ for (i = 0; i < PCH_GBE_GLOBAL_STATS_LEN; i++) {
+ char *p = hw_stats + gstats->offset;
+ data[i] = gstats->size == sizeof(u64) ? *(u64 *)p:(*(u32 *)p);

Actually, how you protect 64-bit stat numbers on 32-bit to be atomic? You change adapter->stats from the irq context and it may need to update 2 words on 32-bit. So instead of having 0x100000000 you can get here 0x1ffffffff or 0x0 since read/write of u64 on 32bit are not atomic and both words may not reach the memory yet. I don't know if that matters -- what are the stats from ethtool used for, but returning invalid values is not nice. Do you need 64bit stats on 32-bit at all? Wouldn't be netdev_stats (they are ulong) enough?

Example:
Given stat = 0xffffffff; on 32-bit stat is composed of stat_hi|stat_lo, compiler generates for instance:
stat_lo++;
if (overflow) (1)
stat_hi++

Quoted 2 lines

Then if you read stat from this ethtool function at point (1), you will get 0x0.

[masa]
This will be changed to 32bit stats .

Quoted 29 lines
Quoted 4 lines

+ gstats++;
+ }
+}

...

Quoted 3 lines
drivers/net/pch_gbe/pch_gbe_main.c new file +0 -0
@@ -0,0 +1,2526 @@

...

Quoted 20 lines
drivers/net/pch_gbe/pch_gbe_main.c new file +20 -0
+void pch_gbe_mac_mar_set(struct pch_gbe_hw *hw, u8 * addr, u32 index)
+{
+ u32 mar_low, mar_high, adrmask;
+
+ pr_debug("index : 0x%x\n", index);
+
+ /*
+ * HW expects these in little endian so we reverse the byte order
+ * from network order (big endian) to little endian
+ */
+ mar_high = ((u32) addr[0] | ((u32) addr[1] << 8) |
+    ((u32) addr[2] << 16) | ((u32) addr[3] << 24));
+ mar_low = ((u32) addr[4] | ((u32) addr[5] << 8));
+ /* Stop the MAC Address of index. */
+ adrmask = ioread32(&hw->reg->ADDR_MASK);
+ iowrite32((adrmask | (0x0001 << index)), &hw->reg->ADDR_MASK);
+ /* wait busy */
+ while ((ioread32(&hw->reg->ADDR_MASK) & PCH_GBE_BUSY)) {
+ int tmp = 0;
+ udelay(20);
Quoted 8 lines
Quoted 6 lines

+ tmp++;
+ if (tmp == 1000) {
+ pr_err("Address mask bit is not cleared\n");
+ break;
+ }
+ }

Maybe it would make sense to create a helper for that.

[masa]
The function for wait will be added.

Quoted 10 lines
Quoted 5 lines
drivers/net/pch_gbe/pch_gbe_main.c new file +4 -0
+static void pch_gbe_tx_queue(struct pch_gbe_adapter *adapter,
+       struct pch_gbe_tx_ring *tx_ring,
+       struct sk_buff *skb)
+{

...

Quoted 1 line

+ buffer_info->kernel_skb = skb;

Just curious, why you need ->kernel_skb and ->skb in that structure? Can't you use just ->skb?

[masa]
"->kernel_skb " will be deleted.

Quoted 30 lines
Quoted 11 lines
drivers/net/pch_gbe/pch_gbe_main.c new file +10 -0
+int pch_gbe_setup_tx_resources(struct pch_gbe_adapter *adapter,
+ struct pch_gbe_tx_ring *tx_ring)
+{
+ struct pci_dev *pdev = adapter->pdev;
+ struct pch_gbe_tx_desc *tx_desc;
+ int size;
+ int desNo;
+
+ size = (int)sizeof(struct pch_gbe_buffer) * tx_ring->count;
+ tx_ring->buffer_info = vmalloc(size);

drivers/net/pch_gbe/pch_gbe_main.c line 1582

Quoted 10 lines
drivers/net/pch_gbe/pch_gbe_main.c new file +10 -0
+ if (!tx_ring->buffer_info) {
+ pr_err("Unable to allocate memory for the buffer infomation\n");
+ return -ENOMEM;
+ }
+ memset(tx_ring->buffer_info, 0, size);
+
+ tx_ring->size = tx_ring->count * (int)sizeof(struct pch_gbe_tx_desc);
+
+ tx_ring->desc =
+ pci_alloc_consistent(pdev, tx_ring->size, &tx_ring->dma);

As you can sleep here now, you may use dma_alloc_coherent(..., GFP_KERNEL) which allows for better memory reclaim and will fail in less cases. For two reasons:
1) pci_alloc_consistent is deprecated AFAIK
2) pci_alloc_consistent calls page allocator with GFP_ATOMIC and may fail for huge allocations early. And as I computed last time, you allocate up to order-4 pages here.

Quoted 1 line

The same for RX dir.

[masa]
This will be modified so.

Quoted 10 lines

drivers/net/pch_gbe/pch_gbe_main.c line 1899

Quoted 8 lines
drivers/net/pch_gbe/pch_gbe_main.c new file +7 -0
+static int pch_gbe_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
+{
+ struct pch_gbe_adapter *adapter = netdev_priv(netdev);
+ struct pch_gbe_tx_ring *tx_ring = adapter->tx_ring;
+ unsigned long flags;
+
+ if (unlikely(skb->len <= 0)) {

Can this happen at all? ndev people should tell you better...

[masa]
This will be deleted.

Quoted 11 lines
Quoted 10 lines

+ dev_kfree_skb_any(skb);
+ pr_debug("Return : OK skb len : %d\n", skb->len);
+ return NETDEV_TX_OK;
+ }
+ if (unlikely(skb->len > (adapter->hw.mac.max_frame_size - 4))) {
+ pr_err("Transfer length Error: skb len: %d > max: %d\n",
+ skb->len, adapter->hw.mac.max_frame_size);
+ adapter->stats.tx_length_errors++;
+ return NETDEV_TX_BUSY;

Quoted 3 lines

Ok, then it's not busy (it will try to send the skb in a loop forever). You should free it and return OK, I think. Anyway, properly set MTU should gurantee that, but it is my guess.

[masa]
dev_kfree_skb_any(skb);
return NETDEV_TX_OK;

Quoted 11 lines

drivers/net/pch_gbe/pch_gbe_main.c line 1939

Quoted 8 lines
drivers/net/pch_gbe/pch_gbe_main.c new file +7 -0
+static struct net_device_stats *pch_gbe_get_stats(
+ struct net_device *netdev)
+{
+ struct pch_gbe_adapter *adapter = netdev_priv(netdev);
+
+ /* only return the current stats */
+ return &adapter->net_stats;

Ah, you have your own copy of netstats. You may use netdev->stats all over the code.

[masa]
"netdev->stats" will be used

Quoted 22 lines
Quoted 8 lines
drivers/net/pch_gbe/pch_gbe_main.c new file +7 -0
+#ifdef CONFIG_PM
+static int pch_gbe_resume(struct pci_dev *pdev)
+{
+ struct net_device *netdev = pci_get_drvdata(pdev);
+ struct pch_gbe_adapter *adapter = netdev_priv(netdev);
+ struct pch_gbe_hw *hw = &adapter->hw;
+ u32 err;

drivers/net/pch_gbe/pch_gbe_main.c line 2287

Quoted 10 lines
drivers/net/pch_gbe/pch_gbe_main.c new file +10 -0
+
+ pci_enable_wake(pdev, PCI_D0, 0);
+ pci_set_power_state(pdev, PCI_D0);
+ pci_restore_state(pdev);
+ err = pci_enable_device(pdev);
+ if (err) {
+ pr_err("Cannot enable PCI device from suspend\n");
+ return err;
+ }
+ pci_set_master(pdev);

Not sure which of those you need. Rafael may tell you more. You should not set power state and restore state, but I might be wrong.

Quoted 2 lines

The same you need not to save state from ->suspend.

[masa]
I confirm Rafael's comment.

Quoted 13 lines
Quoted 12 lines

+ pch_gbe_hal_power_up_phy(hw);
+ pch_gbe_reset(adapter);
+ /* Clear wake on lan control and status */
+ pch_gbe_mac_set_wol_event(hw, 0);

drivers/net/pch_gbe/pch_gbe_main.c new file +8 -0
+
+ if (netif_running(netdev))
+ pch_gbe_up(adapter);
+ netif_device_attach(netdev);
+
+ return 0;
+}
+#endif /* CONFIG_PM */

...

Quoted 7 lines
Quoted 3 lines
drivers/net/pch_gbe/pch_gbe_param.c new file +0 -0
@@ -0,0 +1,501 @@

...

Quoted 1 line

+const struct pch_gbe_opt_list speed_list[] = {

static? And all below...

[masa]
This will be modified.

Quoted 7 lines
Quoted 6 lines

+ { 0, "" },
+ { SPEED_10, "" },
+ { SPEED_100, "" },
+ { SPEED_1000, "" }
+};

...

Quoted 9 lines

drivers/net/pch_gbe/pch_gbe_param.c line 286

Quoted 7 lines
drivers/net/pch_gbe/pch_gbe_param.c new file +7 -0
+static void pch_gbe_check_copper_options(struct pch_gbe_adapter *adapter)
+{
+ struct pch_gbe_hw *hw = &adapter->hw;
+ int speed, dplx;
+
+ { /* Speed */
+ struct pch_gbe_option opt = {

So may be this static const? And all below...

[masa]
This will be modified.