Skip to contentSkip to conversation

ethernet/arc/arc_emac - Add new driver

PATCH Superseded Alexey Brodkin 6 participants 15 messages Jun 13, 2013 – Sep 4, 2013

Subsystems (3)

v3 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 ethernet/arc/arc_emac - Add new driver

Driver for non-standard on-chip ethernet device ARC EMAC 10/100, instantiated in some legacy ARC (Synopsys) FPGA Boards such as ARCAngel4/ML50x.

This is based off of current Linus tree.

Cc Andy Shevchenko <[email protected]>
Cc Francois Romieu <[email protected]>
Cc Joe Perches <[email protected]>
Cc Vineet Gupta <[email protected]>
Cc Mischa Jonker <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Grant Likely <[email protected]>
Cc Rob Herring <[email protected]>
Cc Paul Gortmaker <[email protected]>
Cc "David S. Miller" <[email protected]>

---
Hi all,

this is 3rd re-spin of my patch.

It introduces pretty significant changes in comparison to v2. Those changes were inspiresd by very useful comments of:
* Andy Shevchenko
* Francois Romieu
* Joe Perches

I hope now driver looks simpler, cleaner and better complies to the most recent "standards" for network drivers in Linux kernel.

Below is a list of main changes in this respin.

1. Functional changes:
1.1. Heavy re-work of "arc_emac_poll", "arc_emac_tx" and "arc_emac_intr". Now they look simpler and implement most of bullets below.
1.2. Properly support DMA-API: dma_{map|unmap}_single, error checking etc.
1.3. Rework "arc_emac_set_address" to be endian-friendly. 1.4. Use "netdev_alloc_skb_ip_align" instead of "netdev_alloc_skb" + "skb_reserve".
1.5. Move PHY connection from "arc_emac_open" to "arc_emac_probe". 1.6. Merge EMAC and MDIO private structures into single one. 1.7. Add use of "netif_{stop|resume}_queue".
1.8. Add "max-speed" DT item for EMAC for proper PHY setup. Some systems have limited memory bandwidth so we need to limit incoming data-rate. 1.9. Convert register accessors from macros into in-lined functions.

2. Cosmetics:
2.1. Remove change-log.
2.2. With inverted logic saved 2 indentation levels. 2.3. Merge multi-line text comments into 1 line. 2.4. Use "ndev" instead of "net_dev".
2.5. All functions in "arc_emac_main.c" are "static" now.

---
 Documentation/devicetree/bindings/net/arc_emac.txt |   38 +
 drivers/net/ethernet/Kconfig                       |    1 +
 drivers/net/ethernet/Makefile                      |    1 +
 drivers/net/ethernet/arc/Kconfig                   |   31 +
 drivers/net/ethernet/arc/Makefile                  |    6 +
 drivers/net/ethernet/arc/arc_emac.h                |  201 +++++
 drivers/net/ethernet/arc/arc_emac_main.c           |  813 ++++++++++++++++++++
 drivers/net/ethernet/arc/arc_emac_mdio.c           |  152 ++++
 8 files changed, 1243 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/net/arc_emac.txt
 create mode 100644 drivers/net/ethernet/arc/Kconfig
 create mode 100644 drivers/net/ethernet/arc/Makefile
 create mode 100644 drivers/net/ethernet/arc/arc_emac.h
 create mode 100644 drivers/net/ethernet/arc/arc_emac_main.c
 create mode 100644 drivers/net/ethernet/arc/arc_emac_mdio.c
Documentation/devicetree/bindings/net/arc_emac.txt new file +38 -0
new file mode 100644
index 0000000..bcbc3f0
@@ -0,0 +1,38 @@
+* Synopsys ARC EMAC 10/100 Ethernet driver (EMAC)
+
+Required properties:
+- compatible: Should be "snps,arc-emac"
+- reg: Address and length of the register set for the device
+- interrupts: Should contain the EMAC interrupts
+- clock-frequency: CPU frequency. It is needed to calculate and set polling
+period of EMAC.
+- max-speed: Maximum supported data-rate in Mbit/s. In some HW configurations
+bandwidth of external memory controller might be a limiting factor. That's why
+it's required to specify which data-rate is supported on current SoC or FPGA.
+For example if only 10 Mbit/s is supported (10BASE-T) set "10". If 100 Mbit/s is
+supported (100BASE-TX) set "100".
+- phy: PHY device attached to the EMAC via MDIO bus
+
+Child nodes of the driver are the individual PHY devices connected to the
+MDIO bus. They must have a "reg" property given the PHY address on the MDIO bus.
+
+Optional properties:
+- mac-address: 6 bytes, mac address
+
+Examples:
+
+	ethernet@c0fc2000 {
+		compatible = "snps,arc-emac";
+		reg = <0xc0fc2000 0x3c>;
+		interrupts = <6>;
+		mac-address = [ 00 11 22 33 44 55 ];
+		clock-frequency = <80000000>;
+		max-speed = <100>;
+		phy = <&phy0>;
+
+		#address-cells = <1>;
+		#size-cells = <0>;
+		phy0: ethernet-phy@0 {
+			reg = <1>;
+		};
+	};
drivers/net/ethernet/Kconfig +1 -0
index 4bedae2..98eec48 100644
@@ -23,6 +23,7 @@ source "drivers/net/ethernet/aeroflex/Kconfig"
 source "drivers/net/ethernet/alteon/Kconfig"
 source "drivers/net/ethernet/amd/Kconfig"
 source "drivers/net/ethernet/apple/Kconfig"
+source "drivers/net/ethernet/arc/Kconfig"
 source "drivers/net/ethernet/atheros/Kconfig"
 source "drivers/net/ethernet/cadence/Kconfig"
 source "drivers/net/ethernet/adi/Kconfig"
drivers/net/ethernet/Makefile +1 -0
index 183e3f4..b764c73 100644
@@ -9,6 +9,7 @@ obj-$(CONFIG_GRETH) += aeroflex/
 obj-$(CONFIG_NET_VENDOR_ALTEON) += alteon/
 obj-$(CONFIG_NET_VENDOR_AMD) += amd/
 obj-$(CONFIG_NET_VENDOR_APPLE) += apple/
+obj-$(CONFIG_NET_VENDOR_ARC) += arc/
 obj-$(CONFIG_NET_VENDOR_ATHEROS) += atheros/
 obj-$(CONFIG_NET_CADENCE) += cadence/
 obj-$(CONFIG_NET_BFIN) += adi/
drivers/net/ethernet/arc/Kconfig new file +31 -0
new file mode 100644
index 0000000..514c57f
@@ -0,0 +1,31 @@
+#
+# ARC EMAC network device configuration
+#
+
+config NET_VENDOR_ARC
+	bool "ARC devices"
+	default y
+	---help---
+	  If you have a network (Ethernet) card belonging to this class, say Y
+	  and read the Ethernet-HOWTO, available from
+	  <http://www.tldp.org/docs.html#howto>.
+
+	  Note that the answer to this question doesn't directly affect the
+	  kernel: saying N will just cause the configurator to skip all
+	  the questions about ARC cards. If you say Y, you will be asked for
+	  your specific card in the following questions.
+
+if NET_VENDOR_ARC
+
+config ARC_EMAC
+	tristate "ARC EMAC support"
+	select MII
+	select PHYLIB
+	depends on OF_IRQ
+	depends on OF_NET
+	---help---
+	  On some legacy ARC (Synopsys) FPGA boards such as ARCAngel4/ML50x
+	  non-standard on-chip ethernet device ARC EMAC 10/100 is used.
+	  Say Y here if you have such a board.  If unsure, say N.
+
+endif # NET_VENDOR_ARC
drivers/net/ethernet/arc/Makefile new file +6 -0
new file mode 100644
index 0000000..f5b73e5
@@ -0,0 +1,6 @@
+#
+# Makefile for the ARC network device drivers.
+#
+
+arc_emac-objs := arc_emac_main.o arc_emac_mdio.o
+obj-$(CONFIG_ARC_EMAC) += arc_emac.o
drivers/net/ethernet/arc/arc_emac.h new file +201 -0

201 lines not on this page · raw message

drivers/net/ethernet/arc/arc_emac_main.c new file +813 -0

813 lines not on this page · raw message

drivers/net/ethernet/arc/arc_emac_mdio.c new file +152 -0

152 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#1 lore raw
Quoted 3 lines · Alexey Brodkin

Driver for non-standard on-chip ethernet device ARC EMAC 10/100, instantiated in some legacy ARC (Synopsys) FPGA Boards such as ARCAngel4/ML50x.

Much better. But still few comments below.

Quoted 1 line

+++ b/drivers/net/ethernet/arc/arc_emac.h

What the point to keep arc_emac prefix for files? You have separate folder for them. This particular one can be main.h.

drivers/net/ethernet/arc/arc_emac.h line 111

Quoted 8 lines
drivers/net/ethernet/arc/arc_emac.h new file +8 -0
+struct arc_emac_priv {
+       struct net_device_stats stats;
+       unsigned int clock_frequency;
+       unsigned int max_speed;
+
+       /* Pointers to BD rings - CPU side */
+       struct arc_emac_bd_t *rxbd;
+       struct arc_emac_bd_t *txbd;

Usually "_t" means "type" that is used in definitions without 'struct' word. I don't think this is the case here.

Quoted 21 lines
drivers/net/ethernet/arc/arc_emac.h new file +21 -0
+       /* Pointers to BD rings - Device side */
+       dma_addr_t rxbd_dma_hdl;
+       dma_addr_t txbd_dma_hdl;
+
+       /* The actual socket buffers */
+       struct buffer_state rx_buff[RX_BD_NUM];
+       struct buffer_state tx_buff[TX_BD_NUM];
+       unsigned int txbd_curr;
+       unsigned int txbd_dirty;
+
+       /* Remember where driver last saw a packet, so next iteration it
+        * starts from here and not 0
+        */
+       unsigned int last_rx_bd;
+
+       struct napi_struct napi;
+
+       /* Phy saved state */
+       int duplex;
+       int speed;
+

drivers/net/ethernet/arc/arc_emac.h line 143

Quoted 10 lines
drivers/net/ethernet/arc/arc_emac.h new file +10 -0
+       /* Devices */
+       struct device *dev;
+       struct device_node *phy_node;
+       struct phy_device *phy_dev;
+       struct net_device *ndev;
+       struct mii_bus *bus;
+
+       /* EMAC registers base address */
+       void __iomem *reg_base_addr;
+};

It seems you missed my comments against the names of the members. Can you address them or comment why not?

drivers/net/ethernet/arc/arc_emac_main.c line 87

Quoted 11 lines

+++ b/drivers/net/ethernet/arc/arc_emac_main.c

drivers/net/ethernet/arc/arc_emac_main.c new file +9 -0
+static int arc_emac_get_settings(struct net_device *ndev,
+                                struct ethtool_cmd *cmd)
+{
+       struct arc_emac_priv *priv = netdev_priv(ndev);
+
+       if (priv->phy_dev)
+               return phy_ethtool_gset(priv->phy_dev, cmd);
+
+       return -EINVAL;

May be
if (!...)
return -EINVAL;
return ...;

drivers/net/ethernet/arc/arc_emac_main.c line 112

Quoted 12 lines
drivers/net/ethernet/arc/arc_emac_main.c new file +12 -0
+static int arc_emac_set_settings(struct net_device *ndev,
+                                struct ethtool_cmd *cmd)
+{
+       struct arc_emac_priv *priv = netdev_priv(ndev);
+
+       if (!capable(CAP_NET_ADMIN))
+               return -EPERM;
+
+       if (priv->phy_dev)
+               return phy_ethtool_sset(priv->phy_dev, cmd);
+
+       return -EINVAL;

Ditto.

Quoted 6 lines
drivers/net/ethernet/arc/arc_emac_main.c new file +6 -0
+static int arc_emac_poll(struct napi_struct *napi, int budget)
+{
+       struct net_device *ndev = napi->dev;
+       struct arc_emac_priv *priv = netdev_priv(ndev);
+       unsigned int i, loop, work_done = 0;
+       struct sk_buff *skb;

drivers/net/ethernet/arc/arc_emac_main.c line 166

Quoted 10 lines
drivers/net/ethernet/arc/arc_emac_main.c new file +9 -0
 
+       for (loop = 0; loop < RX_BD_NUM; loop++) {
+               struct net_device_stats *stats = &priv->stats;
+               struct buffer_state *rx_buff;
+               struct arc_emac_bd_t *rxbd;
+               dma_addr_t addr;
+               unsigned int info, pktlen;
+               unsigned int buflen = ndev->mtu + EMAC_BUFFER_PAD;
+
+               i = (i + 1) % RX_BD_NUM;

I just realize you are using circular buffer here. What about to use them with linux' native framework? Documentation/circular-buffers.txt

drivers/net/ethernet/arc/arc_emac_main.c line 176

Quoted 4 lines
drivers/net/ethernet/arc/arc_emac_main.c new file +4 -0
+               if (unlikely((info & OWN_MASK) == FOR_EMAC)) {
+                       /* BD is still owned by EMAC */
+                       continue;
+               }

Redundant braces.

drivers/net/ethernet/arc/arc_emac_main.c line 232

Quoted 5 lines
drivers/net/ethernet/arc/arc_emac_main.c new file +5 -0
+               addr = dma_map_single(&ndev->dev, (void *)rx_buff->skb->data,
+                                     buflen, DMA_FROM_DEVICE);
+               if (dma_mapping_error(&ndev->dev, addr)) {
+                       if (net_ratelimit())
+                               if (net_ratelimit())

Is it not a typo?

drivers/net/ethernet/arc/arc_emac_main.c line 248

Quoted 5 lines
drivers/net/ethernet/arc/arc_emac_main.c new file +4 -0
+                                       netdev_err(ndev, "cannot dma map\n");
 
+               work_done++;
+               if (work_done >= budget)
+                       break;

Those three could easily go to the for () on the top of this function.

Quoted 5 lines
drivers/net/ethernet/arc/arc_emac_main.c new file +4 -0
+static irqreturn_t arc_emac_intr(int irq, void *dev_instance)
+{
 
+       if (status & TXINT_MASK) {
+               unsigned int i;

drivers/net/ethernet/arc/arc_emac_main.c line 333

Quoted 10 lines
drivers/net/ethernet/arc/arc_emac_main.c new file +9 -0
+
+               for (i = 0; i < TX_BD_NUM; i++) {
+                       unsigned int *txbd_dirty = &priv->txbd_dirty;
+                       struct arc_emac_bd_t *txbd = &priv->txbd[*txbd_dirty];
+                       struct buffer_state *tx_buff = &priv->tx_buff[*txbd_dirty];
+                       struct sk_buff *skb = tx_buff->skb;
+                       unsigned int info = txbd->info;
 
+                       txbd->data = 0x0;
+                       txbd->info = 0x0;

Plain 0?

Quoted 1 line

+ *txbd_dirty = (*txbd_dirty + 1) % TX_BD_NUM;

Same idea about circular buffers.

drivers/net/ethernet/arc/arc_emac_main.c line 444

Quoted 6 lines
drivers/net/ethernet/arc/arc_emac_main.c new file +5 -0
+static int arc_emac_open(struct net_device *ndev)
+{
 
+       /* Set Poll rate so that it polls every 1 ms */
+       arc_reg_set(priv, R_POLLRATE,
+                    priv->clock_frequency / 1000000);

I don't understand how you end up with 1ms here. 1000000 is just a magic number, clock_frequency generally is an arbitrary value.

drivers/net/ethernet/arc/arc_emac_main.c line 509

Quoted 12 lines
drivers/net/ethernet/arc/arc_emac_main.c new file +12 -0
+static struct net_device_stats *arc_emac_stats(struct net_device *ndev)
+{
+       unsigned long miss, rxerr, rxfram, rxcrc, rxoflow;
+       struct arc_emac_priv *priv = netdev_priv(ndev);
+       struct net_device_stats *stats = &priv->stats;
+
+       rxerr = arc_reg_get(priv, R_RXERR);
+       miss = arc_reg_get(priv, R_MISS);
+
+       rxcrc = rxerr & 0xff;
+       rxfram = rxerr >> 8 & 0xff;
+       rxoflow = rxerr >> 16 & 0xff;

If you declare those three as u8, you can avoid those & 0xff.

drivers/net/ethernet/arc/arc_emac_main.c line 630

Quoted 5 lines
drivers/net/ethernet/arc/arc_emac_main.c new file +5 -0
+       ndev = alloc_etherdev(sizeof(struct arc_emac_priv));
+       if (!ndev) {
+               err = -ENOMEM;
+               goto out;
+       }

Redundant goto. Just return here.

drivers/net/ethernet/arc/arc_emac.h line 167

Quoted 3 lines
drivers/net/ethernet/arc/arc_emac.h new file +3 -0
+       }
+
+

Extra line.

--
With Best Regards,
Andy Shevchenko

#2 lore raw
Quoted 7 lines · Andy Shevchenko
Quoted 3 lines · Alexey Brodkin

Driver for non-standard on-chip ethernet device ARC EMAC 10/100, instantiated in some legacy ARC (Synopsys) FPGA Boards such as ARCAngel4/ML50x.

Much better. But still few comments below.

Sensible comments save for this truly trivial one

Quoted 1 line
Quoted 1 line

+++ b/drivers/net/ethernet/arc/arc_emac.h

[]

Quoted 6 lines

drivers/net/ethernet/arc/arc_emac_main.c line 176

Quoted 4 lines
drivers/net/ethernet/arc/arc_emac_main.c new file +4 -0
+               if (unlikely((info & OWN_MASK) == FOR_EMAC)) {
+                       /* BD is still owned by EMAC */
+                       continue;
+               }

Redundant braces.

Maybe not. Braces can be a visual aid when there is a comment in the test.

#3 lore raw
Quoted 14 lines · Joe Perches
Quoted 7 lines · Andy Shevchenko
Quoted 5 lines · Alexey Brodkin

Driver for non-standard on-chip ethernet device ARC EMAC 10/100, instantiated in some legacy ARC (Synopsys) FPGA Boards such as ARCAngel4/ML50x.

+++ b/drivers/net/ethernet/arc/arc_emac.h

[]

Quoted 5 lines
Quoted 4 lines
drivers/net/ethernet/arc/arc_emac_main.c new file +4 -0
+               if (unlikely((info & OWN_MASK) == FOR_EMAC)) {
+                       /* BD is still owned by EMAC */
+                       continue;
+               }
Quoted 4 lines
Quoted 1 line

Redundant braces.

Maybe not. Braces can be a visual aid when there is a comment in the test.

I'm okay with both approaches. If author considers the comment is useful in that form, I agree with him.

--
With Best Regards,
Andy Shevchenko

#5 lore raw
In reply to Andy Shevchenko

Andy Shevchenko <[email protected]> :
[...]

Quoted 2 lines

I just realize you are using circular buffer here. What about to use them with linux' native framework? Documentation/circular-buffers.txt

It's almost unused in drivers/net. I won't mind if it stays this way.

Signature · 1 line
Ueimor
maintainer
#4 lore raw
In reply to Joe Perches

From: Joe Perches <[email protected]>
Date: Thu, 13 Jun 2013 11:33:21 -0700

Quoted 11 lines · Andy Shevchenko
Quoted 7 lines

On Thu, Jun 13, 2013 at 5:37 PM, Alexey Brodkin

drivers/net/ethernet/arc/arc_emac_main.c line 176

Quoted 4 lines
drivers/net/ethernet/arc/arc_emac_main.c new file +4 -0
+               if (unlikely((info & OWN_MASK) == FOR_EMAC)) {
+                       /* BD is still owned by EMAC */
+                       continue;
+               }

Redundant braces.

Maybe not. Braces can be a visual aid when there is a comment in the test.

Agreed.

#6 lore raw
In reply to Andy Shevchenko
Quoted 6 lines · Andy Shevchenko
Quoted 3 lines · Alexey Brodkin

Driver for non-standard on-chip ethernet device ARC EMAC 10/100, instantiated in some legacy ARC (Synopsys) FPGA Boards such as ARCAngel4/ML50x.

Quoted 6 lines

Much better. But still few comments below.

Quoted 1 line

+++ b/drivers/net/ethernet/arc/arc_emac.h

What the point to keep arc_emac prefix for files? You have separate folder for them. This particular one can be main.h.

A reason is to still have an ability to tell what's a source a developer is dealing with. But in general your idea looks good. Will simplify names.

And what about function names? Do you think it worth to shorten them too since most of them aren't visible outside (static).

Quoted 11 lines

drivers/net/ethernet/arc/arc_emac.h line 111

Quoted 8 lines
drivers/net/ethernet/arc/arc_emac.h new file +8 -0
+struct arc_emac_priv {
+       struct net_device_stats stats;
+       unsigned int clock_frequency;
+       unsigned int max_speed;
+
+       /* Pointers to BD rings - CPU side */
+       struct arc_emac_bd_t *rxbd;
+       struct arc_emac_bd_t *txbd;

Usually "_t" means "type" that is used in definitions without 'struct' word. I don't think this is the case here.

Right, it is a legacy. Back in the day it was "typedef" I later changed into "struct ...".

Quoted 26 lines
Quoted 26 lines
drivers/net/ethernet/arc/arc_emac.h new file +26 -0
+       /* Pointers to BD rings - Device side */
+       dma_addr_t rxbd_dma_hdl;
+       dma_addr_t txbd_dma_hdl;
+
+       /* The actual socket buffers */
+       struct buffer_state rx_buff[RX_BD_NUM];
+       struct buffer_state tx_buff[TX_BD_NUM];
+       unsigned int txbd_curr;
+       unsigned int txbd_dirty;
+
+       /* Remember where driver last saw a packet, so next iteration it
+        * starts from here and not 0
+        */
+       unsigned int last_rx_bd;
+
+       struct napi_struct napi;
+
+       /* Phy saved state */
+       int duplex;
+       int speed;
+
+       /* Devices */
+       struct device *dev;
+       struct device_node *phy_node;
+       struct phy_device *phy_dev;
+       struct net_device *ndev;
Quoted 8 lines

drivers/net/ethernet/arc/arc_emac.h line 143

Quoted 5 lines
drivers/net/ethernet/arc/arc_emac.h new file +5 -0
+       struct mii_bus *bus;
+
+       /* EMAC registers base address */
+       void __iomem *reg_base_addr;
+};

It seems you missed my comments against the names of the members. Can you address them or comment why not?

You mean to add description in kerneldoc format for all the fields in structures?

Well while in general it could be "a proper way" of documenting sources I found it not that convenient especially in case of really long structures.

Consider an example from here
https://www.kernel.org/doc/Documentation/kernel-doc-nano-HOWTO.txt:
=================
Example kernel-doc data structure comment.

/**
* struct blah - the basic blah structure
* @mem1: describe the first member of struct blah
* @mem2: describe the second member of struct blah,
* perhaps with more lines and words.
*
* Longer description of this structure.
*/
=================

In my case "arc_emac_priv" structure has 21 members, so right before structure itself there will be another at least 21 line of comments.

Moreover: "The kernel-doc function comments describe each parameter to the function, in order, with the @name lines."

While I don't think that each and every member needs description. At least some pairs like Tx/Rx I believe may share the only comment saying "Pointers to BD rings - CPU side".

Also I barely can find an example of strict usage of kernel-doc format for data structures in drivers nearby.

For example take a look at STMMAC - drivers/net/ethernet/stmicro/stmmac/ Lots of structures defined, non with kernel-doc description.

Still you think the only way to go is to add kernel-doc description then I'll add it ASAP, might be it will be a good example for other developers.

Quoted 16 lines

drivers/net/ethernet/arc/arc_emac_main.c line 87

Quoted 11 lines

+++ b/drivers/net/ethernet/arc/arc_emac_main.c

drivers/net/ethernet/arc/arc_emac_main.c new file +9 -0
+static int arc_emac_get_settings(struct net_device *ndev,
+                                struct ethtool_cmd *cmd)
+{
+       struct arc_emac_priv *priv = netdev_priv(ndev);
+
+       if (priv->phy_dev)
+               return phy_ethtool_gset(priv->phy_dev, cmd);
+
+       return -EINVAL;

May be
if (!...)
return -EINVAL;
return ...;

Agree, this will be better.

Quoted 6 lines
Quoted 6 lines
drivers/net/ethernet/arc/arc_emac_main.c new file +6 -0
+static int arc_emac_set_settings(struct net_device *ndev,
+                                struct ethtool_cmd *cmd)
+{
+       struct arc_emac_priv *priv = netdev_priv(ndev);
+
+       if (!capable(CAP_NET_ADMIN))
Quoted 8 lines

drivers/net/ethernet/arc/arc_emac_main.c line 112

Quoted 6 lines
drivers/net/ethernet/arc/arc_emac_main.c new file +6 -0
+               return -EPERM;
+
+       if (priv->phy_dev)
+               return phy_ethtool_sset(priv->phy_dev, cmd);
+
+       return -EINVAL;

Ditto.

Agree.

Quoted 17 lines
Quoted 6 lines
drivers/net/ethernet/arc/arc_emac_main.c new file +6 -0
+static int arc_emac_poll(struct napi_struct *napi, int budget)
+{
+       struct net_device *ndev = napi->dev;
+       struct arc_emac_priv *priv = netdev_priv(ndev);
+       unsigned int i, loop, work_done = 0;
+       struct sk_buff *skb;

drivers/net/ethernet/arc/arc_emac_main.c line 166

Quoted 10 lines
drivers/net/ethernet/arc/arc_emac_main.c new file +9 -0
 
+       for (loop = 0; loop < RX_BD_NUM; loop++) {
+               struct net_device_stats *stats = &priv->stats;
+               struct buffer_state *rx_buff;
+               struct arc_emac_bd_t *rxbd;
+               dma_addr_t addr;
+               unsigned int info, pktlen;
+               unsigned int buflen = ndev->mtu + EMAC_BUFFER_PAD;
+
+               i = (i + 1) % RX_BD_NUM;
Quoted 2 lines

I just realize you are using circular buffer here. What about to use them with linux' native framework? Documentation/circular-buffers.txt

Exactly as Francois commented already - I didn't find it to be widely used in network drivers so I decided to not use it at least for now.

Quoted 6 lines

drivers/net/ethernet/arc/arc_emac_main.c line 176

Quoted 4 lines
drivers/net/ethernet/arc/arc_emac_main.c new file +4 -0
+               if (unlikely((info & OWN_MASK) == FOR_EMAC)) {
+                       /* BD is still owned by EMAC */
+                       continue;
+               }

Redundant braces.

The question is then where to put a comment?
Before "if"? But it only applicable to contents of "if", right? If I put the comment right after "if" then "continue" will be a bit far from "if". And this may confuse a reader. That's why to make reading easier I put braces in place.

Quoted 7 lines

drivers/net/ethernet/arc/arc_emac_main.c line 232

Quoted 5 lines
drivers/net/ethernet/arc/arc_emac_main.c new file +5 -0
+               addr = dma_map_single(&ndev->dev, (void *)rx_buff->skb->data,
+                                     buflen, DMA_FROM_DEVICE);
+               if (dma_mapping_error(&ndev->dev, addr)) {
+                       if (net_ratelimit())
+                               if (net_ratelimit())

Is it not a typo?

Definitely not a typo! Copy-paste! I should have found it myself...

Quoted 7 lines

drivers/net/ethernet/arc/arc_emac_main.c line 248

Quoted 5 lines
drivers/net/ethernet/arc/arc_emac_main.c new file +4 -0
+                                       netdev_err(ndev, "cannot dma map\n");
 
+               work_done++;
+               if (work_done >= budget)
+                       break;

Those three could easily go to the for () on the top of this function.

Correct. It should be like this on top of the "arc_emac_poll":
====

		if (work_done >= budget)
			break;
		work_done++;

====

Quoted 13 lines
Quoted 12 lines
drivers/net/ethernet/arc/arc_emac_main.c new file +11 -0
+static irqreturn_t arc_emac_intr(int irq, void *dev_instance)
+{
 
+       if (status & TXINT_MASK) {
+               unsigned int i;
+
+               for (i = 0; i < TX_BD_NUM; i++) {
+                       unsigned int *txbd_dirty = &priv->txbd_dirty;
+                       struct arc_emac_bd_t *txbd = &priv->txbd[*txbd_dirty];
+                       struct buffer_state *tx_buff = &priv->tx_buff[*txbd_dirty];
+                       struct sk_buff *skb = tx_buff->skb;
+                       unsigned int info = txbd->info;
Quoted 4 lines

drivers/net/ethernet/arc/arc_emac_main.c line 333

Quoted 2 lines
drivers/net/ethernet/arc/arc_emac_main.c new file +2 -0
+                       txbd->data = 0x0;
+                       txbd->info = 0x0;

Plain 0?

Right, 0 might be nicer)

Quoted 7 lines
Quoted 1 line

+ *txbd_dirty = (*txbd_dirty + 1) % TX_BD_NUM;

Same idea about circular buffers.

Quoted 2 lines
drivers/net/ethernet/arc/arc_emac_main.c new file +2 -0
+static int arc_emac_open(struct net_device *ndev)
+{
Quoted 6 lines

drivers/net/ethernet/arc/arc_emac_main.c line 444

Quoted 3 lines
drivers/net/ethernet/arc/arc_emac_main.c new file +3 -0
+       /* Set Poll rate so that it polls every 1 ms */
+       arc_reg_set(priv, R_POLLRATE,
+                    priv->clock_frequency / 1000000);

I don't understand how you end up with 1ms here. 1000000 is just a magic number, clock_frequency generally is an arbitrary value.

Here's an extract from documentation:
==========
The value programmed into this register is number of clocks between polls times 1024. A value of 1 will cause a poll every 1024 clocks, 2=2048 clocks. 0 is not valid. A CPU clock frequency of 100Mhz would typically want to program the POLLRATE register with a value of 100 which will cause a poll to occur about once every millisecond (10ns * 1024 * 100=1ms)
==========

Do you think this info should be put in comment somewhere near? I'd say it's pretty specific and common reader doesn't need to go in such sort of details especially if he doesn't have access to Hw documentation.

Otherwise we'll need to add to many technical details in comments, right?

Quoted 6 lines
Quoted 6 lines
drivers/net/ethernet/arc/arc_emac_main.c new file +6 -0
+static struct net_device_stats *arc_emac_stats(struct net_device *ndev)
+{
+       unsigned long miss, rxerr, rxfram, rxcrc, rxoflow;
+       struct arc_emac_priv *priv = netdev_priv(ndev);
+       struct net_device_stats *stats = &priv->stats;
+
Quoted 8 lines

drivers/net/ethernet/arc/arc_emac_main.c line 509

Quoted 6 lines
drivers/net/ethernet/arc/arc_emac_main.c new file +6 -0
+       rxerr = arc_reg_get(priv, R_RXERR);
+       miss = arc_reg_get(priv, R_MISS);
+
+       rxcrc = rxerr & 0xff;
+       rxfram = rxerr >> 8 & 0xff;
+       rxoflow = rxerr >> 16 & 0xff;

If you declare those three as u8, you can avoid those & 0xff.

Makes sense.

Quoted 7 lines

drivers/net/ethernet/arc/arc_emac_main.c line 630

Quoted 5 lines
drivers/net/ethernet/arc/arc_emac_main.c new file +5 -0
+       ndev = alloc_etherdev(sizeof(struct arc_emac_priv));
+       if (!ndev) {
+               err = -ENOMEM;
+               goto out;
+       }

Redundant goto. Just return here.

Correct.

Quoted 5 lines

drivers/net/ethernet/arc/arc_emac.h line 167

Quoted 3 lines
drivers/net/ethernet/arc/arc_emac.h new file +3 -0
+       }
+
+

Extra line.

Thanks :)

Quoted 4 lines

--
With Best Regards,
Andy Shevchenko

Regards,
Alexey

#7 lore raw
Quoted 11 lines · Alexey Brodkin
Quoted 7 lines · Andy Shevchenko
Quoted 5 lines · Alexey Brodkin

Driver for non-standard on-chip ethernet device ARC EMAC 10/100, instantiated in some legacy ARC (Synopsys) FPGA Boards such as ARCAngel4/ML50x.

+++ b/drivers/net/ethernet/arc/arc_emac.h

And what about function names? Do you think it worth to shorten them too since most of them aren't visible outside (static).

It's better to keep them in their namespace. So, leave them as is.

Quoted 9 lines
Quoted 6 lines

drivers/net/ethernet/arc/arc_emac.h line 143

Quoted 3 lines
drivers/net/ethernet/arc/arc_emac.h new file +2 -0
+struct arc_emac_priv {
 
+};

It seems you missed my comments against the names of the members. Can you address them or comment why not?

You mean to add description in kerneldoc format for all the fields in structures?

Not only that one. About member names as well.

Quoted 5 lines

Well while in general it could be "a proper way" of documenting sources I found it not that convenient especially in case of really long structures.

In my case "arc_emac_priv" structure has 21 members, so right before structure itself there will be another at least 21 line of comments.

Not an argument, you understand.

Quoted 4 lines

Moreover: "The kernel-doc function comments describe each parameter to the function, in order, with the @name lines."

While I don't think that each and every member needs description.

Describe them in couple of words.

Quoted 3 lines

At
least some pairs like Tx/Rx I believe may share the only comment saying "Pointers to BD rings - CPU side".

What BD means? May be it worth to describe as well?

Quoted 5 lines

Also I barely can find an example of strict usage of kernel-doc format for data structures in drivers nearby.

For example take a look at STMMAC - drivers/net/ethernet/stmicro/stmmac/ Lots of structures defined, non with kernel-doc description.

Again, not an excuse :-)

Quoted 2 lines

Still you think the only way to go is to add kernel-doc description then I'll add it ASAP, might be it will be a good example for other developers.

Right.

Quoted 14 lines
Quoted 14 lines
Quoted 13 lines

+++ b/drivers/net/ethernet/arc/arc_emac_main.c

(quoted diff) +7 -0
+static int arc_emac_poll(struct napi_struct *napi, int budget)
+{
 
+       unsigned int i, loop, work_done = 0;
 
+       for (loop = 0; loop < RX_BD_NUM; loop++) {
 
+               work_done++;
+               if (work_done >= budget)
+                       break;
Quoted 8 lines
Quoted 1 line

Those three could easily go to the for () on the top of this function.

Correct. It should be like this on top of the "arc_emac_poll":
====

                if (work_done >= budget)
                        break;
                work_done++;

====

I meant something like
for (loop = 0; loop < RX_BD_NUM && work_done < budget; loop++, work_done++)

Quoted 9 lines
Quoted 9 lines

drivers/net/ethernet/arc/arc_emac_main.c line 444

Quoted 6 lines
drivers/net/ethernet/arc/arc_emac_main.c new file +5 -0
+static int arc_emac_open(struct net_device *ndev)
+{
 
+       /* Set Poll rate so that it polls every 1 ms */
+       arc_reg_set(priv, R_POLLRATE,
+                    priv->clock_frequency / 1000000);

I don't understand how you end up with 1ms here. 1000000 is just a magic number, clock_frequency generally is an arbitrary value.

I meant how do you guarantee this is 1ms? What if clock_frequency is not 100MHz?

--
With Best Regards,
Andy Shevchenko

#8 lore raw
Quoted 9 lines · Andy Shevchenko
Quoted 6 lines · Alexey Brodkin
Quoted 5 lines · Andy Shevchenko
Quoted 3 lines · Alexey Brodkin

Driver for non-standard on-chip ethernet device ARC EMAC 10/100, instantiated in some legacy ARC (Synopsys) FPGA Boards such as ARCAngel4/ML50x.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line

+++ b/drivers/net/ethernet/arc/arc_emac.h

And what about function names? Do you think it worth to shorten them too since most of them aren't visible outside (static).

It's better to keep them in their namespace. So, leave them as is.

Ok, makes sense.

Quoted 11 lines
Quoted 9 lines
Quoted 6 lines

drivers/net/ethernet/arc/arc_emac.h line 143

Quoted 3 lines
drivers/net/ethernet/arc/arc_emac.h new file +2 -0
+struct arc_emac_priv {
 
+};

It seems you missed my comments against the names of the members. Can you address them or comment why not?

You mean to add description in kerneldoc format for all the fields in structures?

Not only that one. About member names as well.

Ok, I'll re-visit member names as advised.

Quoted 21 lines
Quoted 5 lines

Well while in general it could be "a proper way" of documenting sources I found it not that convenient especially in case of really long structures.

In my case "arc_emac_priv" structure has 21 members, so right before structure itself there will be another at least 21 line of comments.

Not an argument, you understand.

Quoted 4 lines

Moreover: "The kernel-doc function comments describe each parameter to the function, in order, with the @name lines."

While I don't think that each and every member needs description.

Describe them in couple of words.

Quoted 3 lines

At
least some pairs like Tx/Rx I believe may share the only comment saying "Pointers to BD rings - CPU side".

What BD means? May be it worth to describe as well?

Quoted 7 lines
Quoted 5 lines

Also I barely can find an example of strict usage of kernel-doc format for data structures in drivers nearby.

For example take a look at STMMAC - drivers/net/ethernet/stmicro/stmmac/ Lots of structures defined, non with kernel-doc description.

Again, not an excuse :-)

Ok, will add description)

Quoted 28 lines
Quoted 2 lines

Still you think the only way to go is to add kernel-doc description then I'll add it ASAP, might be it will be a good example for other developers.

Right.

Quoted 14 lines
Quoted 14 lines
Quoted 13 lines

+++ b/drivers/net/ethernet/arc/arc_emac_main.c

(quoted diff) +7 -0
+static int arc_emac_poll(struct napi_struct *napi, int budget)
+{
 
+       unsigned int i, loop, work_done = 0;
 
+       for (loop = 0; loop < RX_BD_NUM; loop++) {
 
+               work_done++;
+               if (work_done >= budget)
+                       break;
Quoted 8 lines
Quoted 1 line

Those three could easily go to the for () on the top of this function.

Correct. It should be like this on top of the "arc_emac_poll":
====

                 if (work_done >= budget)
                         break;
                 work_done++;

====

Quoted 2 lines

I meant something like
for (loop = 0; loop < RX_BD_NUM && work_done < budget; loop++, work_done++)

Well, it turned out that counter increment was put in the end of the loop on purpose.

But with help of this hint I understood that "arc_emac_poll" was implemented a bit incorrectly. Now I added an exit from the loop on the first BD owned by EMAC. This is possible because we know that buffers are processed in order.

Quoted 11 lines
Quoted 9 lines
Quoted 9 lines

drivers/net/ethernet/arc/arc_emac_main.c line 444

Quoted 6 lines
drivers/net/ethernet/arc/arc_emac_main.c new file +5 -0
+static int arc_emac_open(struct net_device *ndev)
+{
 
+       /* Set Poll rate so that it polls every 1 ms */
+       arc_reg_set(priv, R_POLLRATE,
+                    priv->clock_frequency / 1000000);

I don't understand how you end up with 1ms here. 1000000 is just a magic number, clock_frequency generally is an arbitrary value.

I meant how do you guarantee this is 1ms? What if clock_frequency is not 100MHz?

That's why I needed to pass CPU frequency to "arc_emac_probe" via DT. As we see from description in HW documentation poll period measured in CPU cycles = "1024 cycles * R_POLLRATE", or "1024 * priv->clock_frequency / 1000000", or "priv->clock_frequency / 1000". Now since length of 1 cycle is "1 / priv->clock_frequency" we may calculate poll period measured in seconds. It will be "priv->clock_frequency / 1000 * (1 / priv->clock_frequency)" which is 1/1000 of second i.e. 1 millisecond. Makes sense?

Regards,
Alexey

#9 lore raw
In reply to Alexey Brodkin

Alexey Brodkin <[email protected]> :
[...]

Quoted 5 lines
drivers/net/ethernet/arc/arc_emac.h new file +0 -0
new file mode 100644
index 0000000..6691db2

[...]

drivers/net/ethernet/arc/arc_emac.h line 110

Quoted 7 lines
drivers/net/ethernet/arc/arc_emac.h new file +7 -0
+struct arc_emac_priv {
+	struct net_device_stats stats;
+	unsigned int clock_frequency;
+	unsigned int max_speed;
+
+	/* Pointers to BD rings - CPU side */
+	struct arc_emac_bd_t *rxbd;

There does not seem to be much need for rxbd->data.

Quoted 1 line
drivers/net/ethernet/arc/arc_emac.h new file +1 -0
+	struct arc_emac_bd_t *txbd;

[...]

drivers/net/ethernet/arc/arc_emac.h

Quoted 7 lines
drivers/net/ethernet/arc/arc_emac.h new file +7 -0
+static int arc_emac_get_settings(struct net_device *ndev,
+				 struct ethtool_cmd *cmd)
+{
+	struct arc_emac_priv *priv = netdev_priv(ndev);
+
+	if (priv->phy_dev)
+		return phy_ethtool_gset(priv->phy_dev, cmd);

arc_emac_probe() succeeded : priv->phy_dev can't be NULL.

[...]

drivers/net/ethernet/arc/arc_emac.h

Quoted 10 lines
drivers/net/ethernet/arc/arc_emac.h new file +10 -0
+static int arc_emac_set_settings(struct net_device *ndev,
+				 struct ethtool_cmd *cmd)
+{
+	struct arc_emac_priv *priv = netdev_priv(ndev);
+
+	if (!capable(CAP_NET_ADMIN))
+		return -EPERM;
+
+	if (priv->phy_dev)
+		return phy_ethtool_sset(priv->phy_dev, cmd);

priv->phy_dev can't be NULL either.

[...]

Quoted 14 lines
drivers/net/ethernet/arc/arc_emac.h new file +14 -0
+static int arc_emac_poll(struct napi_struct *napi, int budget)
+{
+	struct net_device *ndev = napi->dev;
+	struct arc_emac_priv *priv = netdev_priv(ndev);
+	unsigned int i, loop, work_done = 0;
+	struct sk_buff *skb;
+
+	/* Loop thru the BD chain, but not always from 0.
+	 * Start from right after where we last saw a packet.
+	 */
+	i = priv->last_rx_bd;
+
+	for (loop = 0; loop < RX_BD_NUM; loop++) {
+		struct net_device_stats *stats = &priv->stats;

drivers/net/ethernet/arc/arc_emac.h

Quoted 10 lines
drivers/net/ethernet/arc/arc_emac.h new file +10 -0
+		struct buffer_state *rx_buff;
+		struct arc_emac_bd_t *rxbd;
+		dma_addr_t addr;
+		unsigned int info, pktlen;
+		unsigned int buflen = ndev->mtu + EMAC_BUFFER_PAD;
+
+		i = (i + 1) % RX_BD_NUM;
+		rx_buff = &priv->rx_buff[i];
+		rxbd = &priv->rxbd[i];
+		info = rxbd->info;

You may / should update 'i' so that you can:

		struct buffer_state *rx_buff = priv->rx_buff + i;
		struct arc_emac_bd_t *rxbd = priv->rxbd + i;
		unsigned int pktlen, info = rxbd->info;

drivers/net/ethernet/arc/arc_emac.h

Quoted 7 lines
drivers/net/ethernet/arc/arc_emac.h new file +7 -0
+
+		/* Process current BD */
+
+		if (unlikely((info & OWN_MASK) == FOR_EMAC)) {
+			/* BD is still owned by EMAC */
+			continue;
+		}

Processing starts at priv->last_rx_bd. Could it not be a 'break' ?

[...]

drivers/net/ethernet/arc/arc_emac.h

Quoted 13 lines
drivers/net/ethernet/arc/arc_emac.h new file +13 -0
+		pktlen = info & LEN_MASK;
+		stats->rx_packets++;
+		stats->rx_bytes += pktlen;
+		skb = rx_buff->skb;
+		skb_put(skb, pktlen);
+		skb->dev = ndev;
+		skb->protocol = eth_type_trans(skb, ndev);
+		netif_receive_skb(skb);
+
+		dma_unmap_single(&ndev->dev,
+				 dma_unmap_addr(&rx_buff, addr),
+				 dma_unmap_len(&rx_buff, len),
+				 DMA_FROM_DEVICE);
		dma_unmap_single(&ndev->dev, dma_unmap_addr(&rx_buff, addr),
				 dma_unmap_len(&rx_buff, len), DMA_FROM_DEVICE);

Please unmap before netif_receive_skb.

drivers/net/ethernet/arc/arc_emac.h

Quoted 9 lines
drivers/net/ethernet/arc/arc_emac.h new file +9 -0
+
+		/* Prepare the BD for next cycle */
+		rx_buff->skb = netdev_alloc_skb_ip_align(ndev, buflen);
+		if (unlikely(!rx_buff->skb)) {
+			if (net_ratelimit())
+				netdev_err(ndev, "cannot allocate skb\n");
+			stats->rx_errors++;
+			continue;
+		}

The descriptor entry is left unchanged. Afaiu the driver will move to the next descriptor and crash on dereferencing NULL (rx_buff->)skb next time it wraps.

I suggest avoiding holes: don't netif_receive_skb if you can't alloc a new skb.

[...]

Quoted 11 lines
drivers/net/ethernet/arc/arc_emac.h new file +11 -0
+static irqreturn_t arc_emac_intr(int irq, void *dev_instance)
+{
+	struct net_device *ndev = dev_instance;
+	struct arc_emac_priv *priv = netdev_priv(ndev);
+	struct net_device_stats *stats = &priv->stats;
+	unsigned int status;
+
+	status = arc_reg_get(priv, R_STATUS);
+	status &= ~MDIO_MASK;
+
+	/* Reset all flags except "MDIO complete"*/

drivers/net/ethernet/arc/arc_emac.h

Quoted 10 lines
drivers/net/ethernet/arc/arc_emac.h new file +10 -0
+	arc_reg_set(priv, R_STATUS, status);
+
+	if (status & RXINT_MASK) {
+		if (likely(napi_schedule_prep(&priv->napi))) {
+			arc_reg_clr(priv, R_ENABLE, RXINT_MASK);
+			__napi_schedule(&priv->napi);
+		}
+	}
+
+	if (status & TXINT_MASK) {

You may consider moving everything into the napi poll handler.

[...]

Quoted 19 lines
drivers/net/ethernet/arc/arc_emac.h new file +19 -0
+static int arc_emac_open(struct net_device *ndev)
+{
+	struct arc_emac_priv *priv = netdev_priv(ndev);
+	struct phy_device *phy_dev = priv->phy_dev;
+	struct arc_emac_bd_t *bd;
+	struct sk_buff *skb;
+	int i;
+
+	phy_dev->autoneg = AUTONEG_ENABLE;
+	phy_dev->speed = 0;
+	phy_dev->duplex = 0;
+	phy_dev->advertising = phy_dev->supported;
+
+	if (priv->max_speed > 100) {
+		phy_dev->advertising &= PHY_GBIT_FEATURES;
+	} else if (priv->max_speed <= 100) {
+		phy_dev->advertising &= PHY_BASIC_FEATURES;
+		if (priv->max_speed <= 10) {
+			phy_dev->advertising &= ~SUPPORTED_100baseT_Half;

drivers/net/ethernet/arc/arc_emac.h

Quoted 10 lines
drivers/net/ethernet/arc/arc_emac.h new file +10 -0
+			phy_dev->advertising &= ~SUPPORTED_100baseT_Full;
+		}
+	}
+
+	/* Allocate and set buffers for Rx BD's */
+	bd = priv->rxbd;
+	for (i = 0; i < RX_BD_NUM; i++) {
+		skb = netdev_alloc_skb_ip_align(ndev, ndev->mtu +
+						EMAC_BUFFER_PAD);
+

Missing NULL check.

[...]

drivers/net/ethernet/arc/arc_emac.h

Quoted 3 lines
drivers/net/ethernet/arc/arc_emac.h new file +3 -0
+	/* Set BD ring pointers for device side */
+	arc_reg_set(priv, R_RX_RING,
+		     (unsigned int)priv->rxbd_dma_hdl);
	arc_reg_set(priv, R_RX_RING, (unsigned int)priv->rxbd_dma_hdl);

drivers/net/ethernet/arc/arc_emac.h

Quoted 3 lines
drivers/net/ethernet/arc/arc_emac.h new file +3 -0
+
+	arc_reg_set(priv, R_TX_RING,
+		     (unsigned int)priv->rxbd_dma_hdl + RX_RING_SZ);

Forgot priv->txbd_dma_hdl ?

drivers/net/ethernet/arc/arc_emac.h

Quoted 4 lines
drivers/net/ethernet/arc/arc_emac.h new file +4 -0
+
+	/* Set Poll rate so that it polls every 1 ms */
+	arc_reg_set(priv, R_POLLRATE,
+		     priv->clock_frequency / 1000000);
	arc_reg_set(priv, R_POLLRATE, priv->clock_frequency / 1000000);

drivers/net/ethernet/arc/arc_emac.h

Quoted 4 lines
drivers/net/ethernet/arc/arc_emac.h new file +4 -0
+
+	/* Enable interrupts */
+	arc_reg_set(priv, R_ENABLE,
+		     TXINT_MASK | RXINT_MASK | ERR_MASK);
	arc_reg_set(priv, R_ENABLE, TXINT_MASK | RXINT_MASK | ERR_MASK);
Quoted 4 lines
drivers/net/ethernet/arc/arc_emac.h new file +4 -0
+
+	/* Set CONTROL */
+	arc_reg_set(priv, R_CTRL,
+		     (RX_BD_NUM << 24) |	/* RX BD table length */

drivers/net/ethernet/arc/arc_emac.h

Quoted 10 lines
drivers/net/ethernet/arc/arc_emac.h new file +10 -0
+		     (TX_BD_NUM << 16) |	/* TX BD table length */
+		     TXRN_MASK | RXRN_MASK);
+
+	/* Enable EMAC */
+	arc_reg_or(priv, R_CTRL, EN_MASK);
+
+	phy_start_aneg(priv->phy_dev);
+
+	netif_start_queue(ndev);
+	napi_enable(&priv->napi);

napi must be enabled before the first rx packet / irq kicks in.

[...]

drivers/net/ethernet/arc/arc_emac.h

Quoted 8 lines
drivers/net/ethernet/arc/arc_emac.h new file +8 -0
+static int arc_emac_tx(struct sk_buff *skb, struct net_device *ndev)
+{
+	struct arc_emac_priv *priv = netdev_priv(ndev);
+	unsigned int info, len, *txbd_curr = &priv->txbd_curr;
+	dma_addr_t addr;
+	char *pkt = skb->data;
+
+	len = max_t(unsigned int, ETH_ZLEN, skb->len);

The device automatically pads, right ?

drivers/net/ethernet/arc/arc_emac.h

Quoted 8 lines
drivers/net/ethernet/arc/arc_emac.h new file +8 -0
+	info = priv->txbd[*txbd_curr].info;
+
+	/* EMAC still holds this buffer in its possession.
+	 * CPU must not modify this buffer descriptor
+	 */
+	if (unlikely((info & OWN_MASK) == FOR_EMAC)) {
+		netif_stop_queue(ndev);
+		return NETDEV_TX_BUSY;

It is more than unlikely: you may check for it as a bug but you should stop_queue when the tx ring is full (before returning below).

drivers/net/ethernet/arc/arc_emac.h

Quoted 6 lines
drivers/net/ethernet/arc/arc_emac.h new file +6 -0
+	}
+
+	addr = dma_map_single(&ndev->dev, (void *)pkt, len, DMA_TO_DEVICE);
+	if (unlikely(dma_mapping_error(&ndev->dev, addr))) {
+		dev_kfree_skb(skb);
+		return NETDEV_TX_OK;

Please update tx_dropped stats.

drivers/net/ethernet/arc/arc_emac.h

Quoted 6 lines
drivers/net/ethernet/arc/arc_emac.h new file +6 -0
+	}
+	dma_unmap_addr_set(&priv->tx_buff[*txbd_curr], mapping, addr);
+	dma_unmap_len_set(&priv->tx_buff[*txbd_curr], len, len);
+
+	priv->tx_buff[*txbd_curr].skb = skb;
+	priv->txbd[*txbd_curr].data = pkt;

Insert memory barrier.

Quoted 10 lines
drivers/net/ethernet/arc/arc_emac.h new file +10 -0
+	priv->txbd[*txbd_curr].info = FOR_EMAC | FRST_MASK | LAST_MASK | len;
+
+	*txbd_curr = (*txbd_curr + 1) % TX_BD_NUM;
+
+	arc_reg_set(priv, R_STATUS, TXPL_MASK);
+
+	skb_tx_timestamp(skb);
+
+	return NETDEV_TX_OK;
+}

[...]

Quoted 2 lines
drivers/net/ethernet/arc/arc_emac.h new file +2 -0
+static int arc_emac_probe(struct platform_device *pdev)
+{

[...]

drivers/net/ethernet/arc/arc_emac.h

Quoted 13 lines
drivers/net/ethernet/arc/arc_emac.h new file +13 -0
+	err = arc_mdio_probe(pdev->dev.of_node, priv);
+	if (err) {
+		dev_err(&pdev->dev, "failed to probe MII bus\n");
+		goto out;
+	}
+
+
+	priv->phy_dev = of_phy_connect(ndev, priv->phy_node,
+				       arc_emac_adjust_link, 0,
+				       PHY_INTERFACE_MODE_MII);
+	if (!priv->phy_dev) {
+		netdev_err(ndev, "of_phy_connect() failed\n");
+		return -ENODEV;

arc_mdio_remove leak.

Signature · 1 line
Ueimor
#10 lore raw
Quoted 8 lines · Francois Romieu

Alexey Brodkin <[email protected]> :
[...]

Quoted 5 lines
drivers/net/ethernet/arc/arc_emac.h new file +0 -0
new file mode 100644
index 0000000..6691db2

[...]

Quoted 9 lines

drivers/net/ethernet/arc/arc_emac.h line 110

Quoted 7 lines
drivers/net/ethernet/arc/arc_emac.h new file +7 -0
+struct arc_emac_priv {
+	struct net_device_stats stats;
+	unsigned int clock_frequency;
+	unsigned int max_speed;
+
+	/* Pointers to BD rings - CPU side */
+	struct arc_emac_bd_t *rxbd;

There does not seem to be much need for rxbd->data.

Could you please clarify this comment? Not clear what do you mean.

Quoted 14 lines
Quoted 1 line
drivers/net/ethernet/arc/arc_emac.h new file +1 -0
+	struct arc_emac_bd_t *txbd;

[...]

drivers/net/ethernet/arc/arc_emac.h

Quoted 7 lines
drivers/net/ethernet/arc/arc_emac.h new file +7 -0
+static int arc_emac_get_settings(struct net_device *ndev,
+				 struct ethtool_cmd *cmd)
+{
+	struct arc_emac_priv *priv = netdev_priv(ndev);
+
+	if (priv->phy_dev)
+		return phy_ethtool_gset(priv->phy_dev, cmd);

arc_emac_probe() succeeded : priv->phy_dev can't be NULL.

[...]

Quoted 12 lines

drivers/net/ethernet/arc/arc_emac.h

Quoted 10 lines
drivers/net/ethernet/arc/arc_emac.h new file +10 -0
+static int arc_emac_set_settings(struct net_device *ndev,
+				 struct ethtool_cmd *cmd)
+{
+	struct arc_emac_priv *priv = netdev_priv(ndev);
+
+	if (!capable(CAP_NET_ADMIN))
+		return -EPERM;
+
+	if (priv->phy_dev)
+		return phy_ethtool_sset(priv->phy_dev, cmd);

priv->phy_dev can't be NULL either.

Nice catch! Fortunately I recognized it as well and already removed those useless checks.

Quoted 28 lines

[...]

Quoted 14 lines
drivers/net/ethernet/arc/arc_emac.h new file +14 -0
+static int arc_emac_poll(struct napi_struct *napi, int budget)
+{
+	struct net_device *ndev = napi->dev;
+	struct arc_emac_priv *priv = netdev_priv(ndev);
+	unsigned int i, loop, work_done = 0;
+	struct sk_buff *skb;
+
+	/* Loop thru the BD chain, but not always from 0.
+	 * Start from right after where we last saw a packet.
+	 */
+	i = priv->last_rx_bd;
+
+	for (loop = 0; loop < RX_BD_NUM; loop++) {
+		struct net_device_stats *stats = &priv->stats;

drivers/net/ethernet/arc/arc_emac.h

Quoted 10 lines
drivers/net/ethernet/arc/arc_emac.h new file +10 -0
+		struct buffer_state *rx_buff;
+		struct arc_emac_bd_t *rxbd;
+		dma_addr_t addr;
+		unsigned int info, pktlen;
+		unsigned int buflen = ndev->mtu + EMAC_BUFFER_PAD;
+
+		i = (i + 1) % RX_BD_NUM;
+		rx_buff = &priv->rx_buff[i];
+		rxbd = &priv->rxbd[i];
+		info = rxbd->info;

You may / should update 'i' so that you can:

Quoted 3 lines
		struct buffer_state *rx_buff = priv->rx_buff + i;
		struct arc_emac_bd_t *rxbd = priv->rxbd + i;
		unsigned int pktlen, info = rxbd->info;

Done)

Quoted 9 lines

drivers/net/ethernet/arc/arc_emac.h

Quoted 7 lines
drivers/net/ethernet/arc/arc_emac.h new file +7 -0
+
+		/* Process current BD */
+
+		if (unlikely((info & OWN_MASK) == FOR_EMAC)) {
+			/* BD is still owned by EMAC */
+			continue;
+		}

Processing starts at priv->last_rx_bd. Could it not be a 'break' ?

It has to be! Fixed.

Quoted 18 lines

[...]

drivers/net/ethernet/arc/arc_emac.h

Quoted 13 lines
drivers/net/ethernet/arc/arc_emac.h new file +13 -0
+		pktlen = info & LEN_MASK;
+		stats->rx_packets++;
+		stats->rx_bytes += pktlen;
+		skb = rx_buff->skb;
+		skb_put(skb, pktlen);
+		skb->dev = ndev;
+		skb->protocol = eth_type_trans(skb, ndev);
+		netif_receive_skb(skb);
+
+		dma_unmap_single(&ndev->dev,
+				 dma_unmap_addr(&rx_buff, addr),
+				 dma_unmap_len(&rx_buff, len),
+				 DMA_FROM_DEVICE);
		dma_unmap_single(&ndev->dev, dma_unmap_addr(&rx_buff, addr),
				 dma_unmap_len(&rx_buff, len), DMA_FROM_DEVICE);
Quoted 1 line

Please unmap before netif_receive_skb.

Done. Thanks.

Quoted 14 lines

drivers/net/ethernet/arc/arc_emac.h

Quoted 9 lines
drivers/net/ethernet/arc/arc_emac.h new file +9 -0
+
+		/* Prepare the BD for next cycle */
+		rx_buff->skb = netdev_alloc_skb_ip_align(ndev, buflen);
+		if (unlikely(!rx_buff->skb)) {
+			if (net_ratelimit())
+				netdev_err(ndev, "cannot allocate skb\n");
+			stats->rx_errors++;
+			continue;
+		}

The descriptor entry is left unchanged. Afaiu the driver will move to the next descriptor and crash on dereferencing NULL (rx_buff->)skb next time it wraps.

Quoted 2 lines

I suggest avoiding holes: don't netif_receive_skb if you can't alloc a new skb.

Frankly I cannot understand how "don't netif_receive_skb" for one of the received packets helps to prevent crash on the next iteration? And I don't see a way to return any error state from NAPI poll handler. Could you please clarify your idea?

Quoted 16 lines

[...]

Quoted 15 lines
drivers/net/ethernet/arc/arc_emac.h new file +15 -0
+static irqreturn_t arc_emac_intr(int irq, void *dev_instance)
+{
+	struct net_device *ndev = dev_instance;
+	struct arc_emac_priv *priv = netdev_priv(ndev);
+	struct net_device_stats *stats = &priv->stats;
+	unsigned int status;
+
+	status = arc_reg_get(priv, R_STATUS);
+	status &= ~MDIO_MASK;
+
+	/* Reset all flags except "MDIO complete"*/
+	arc_reg_set(priv, R_STATUS, status);
+
+	if (status & RXINT_MASK) {
+		if (likely(napi_schedule_prep(&priv->napi))) {
Quoted 8 lines

drivers/net/ethernet/arc/arc_emac.h

Quoted 6 lines
drivers/net/ethernet/arc/arc_emac.h new file +6 -0
+			arc_reg_clr(priv, R_ENABLE, RXINT_MASK);
+			__napi_schedule(&priv->napi);
+		}
+	}
+
+	if (status & TXINT_MASK) {

You may consider moving everything into the napi poll handler.

Agree. Would be good - implementing.

Quoted 24 lines

[...]

Quoted 23 lines
drivers/net/ethernet/arc/arc_emac.h new file +23 -0
+static int arc_emac_open(struct net_device *ndev)
+{
+	struct arc_emac_priv *priv = netdev_priv(ndev);
+	struct phy_device *phy_dev = priv->phy_dev;
+	struct arc_emac_bd_t *bd;
+	struct sk_buff *skb;
+	int i;
+
+	phy_dev->autoneg = AUTONEG_ENABLE;
+	phy_dev->speed = 0;
+	phy_dev->duplex = 0;
+	phy_dev->advertising = phy_dev->supported;
+
+	if (priv->max_speed > 100) {
+		phy_dev->advertising &= PHY_GBIT_FEATURES;
+	} else if (priv->max_speed <= 100) {
+		phy_dev->advertising &= PHY_BASIC_FEATURES;
+		if (priv->max_speed <= 10) {
+			phy_dev->advertising &= ~SUPPORTED_100baseT_Half;
+			phy_dev->advertising &= ~SUPPORTED_100baseT_Full;
+		}
+	}
+
Quoted 8 lines

drivers/net/ethernet/arc/arc_emac.h

Quoted 6 lines
drivers/net/ethernet/arc/arc_emac.h new file +6 -0
+	/* Allocate and set buffers for Rx BD's */
+	bd = priv->rxbd;
+	for (i = 0; i < RX_BD_NUM; i++) {
+		skb = netdev_alloc_skb_ip_align(ndev, ndev->mtu +
+						EMAC_BUFFER_PAD);
+

Missing NULL check.

Thanks!

Quoted 7 lines

[...]

drivers/net/ethernet/arc/arc_emac.h

Quoted 3 lines
drivers/net/ethernet/arc/arc_emac.h new file +3 -0
+	/* Set BD ring pointers for device side */
+	arc_reg_set(priv, R_RX_RING,
+		     (unsigned int)priv->rxbd_dma_hdl);
	arc_reg_set(priv, R_RX_RING, (unsigned int)priv->rxbd_dma_hdl);
Quoted 5 lines

drivers/net/ethernet/arc/arc_emac.h

Quoted 3 lines
drivers/net/ethernet/arc/arc_emac.h new file +3 -0
+
+	arc_reg_set(priv, R_TX_RING,
+		     (unsigned int)priv->rxbd_dma_hdl + RX_RING_SZ);

Forgot priv->txbd_dma_hdl ?

Well, it was used implicitly, because priv->rxbd_dma_hdl + RX_RING_SZ = priv->txbd_dma_hdl. Replaced with explicit priv->txbd_dma_hdl.

Quoted 7 lines

drivers/net/ethernet/arc/arc_emac.h

Quoted 4 lines
drivers/net/ethernet/arc/arc_emac.h new file +4 -0
+
+	/* Set Poll rate so that it polls every 1 ms */
+	arc_reg_set(priv, R_POLLRATE,
+		     priv->clock_frequency / 1000000);
	arc_reg_set(priv, R_POLLRATE, priv->clock_frequency / 1000000);
Quoted 6 lines

drivers/net/ethernet/arc/arc_emac.h

Quoted 4 lines
drivers/net/ethernet/arc/arc_emac.h new file +4 -0
+
+	/* Enable interrupts */
+	arc_reg_set(priv, R_ENABLE,
+		     TXINT_MASK | RXINT_MASK | ERR_MASK);
	arc_reg_set(priv, R_ENABLE, TXINT_MASK | RXINT_MASK | ERR_MASK);

I see, thanks.

Quoted 8 lines
Quoted 8 lines
drivers/net/ethernet/arc/arc_emac.h new file +8 -0
+
+	/* Set CONTROL */
+	arc_reg_set(priv, R_CTRL,
+		     (RX_BD_NUM << 24) |	/* RX BD table length */
+		     (TX_BD_NUM << 16) |	/* TX BD table length */
+		     TXRN_MASK | RXRN_MASK);
+
+	/* Enable EMAC */
Quoted 8 lines

drivers/net/ethernet/arc/arc_emac.h

Quoted 6 lines
drivers/net/ethernet/arc/arc_emac.h new file +6 -0
+	arc_reg_or(priv, R_CTRL, EN_MASK);
+
+	phy_start_aneg(priv->phy_dev);
+
+	netif_start_queue(ndev);
+	napi_enable(&priv->napi);

napi must be enabled before the first rx packet / irq kicks in.

Thanks.

Quoted 11 lines

[...]

drivers/net/ethernet/arc/arc_emac.h

Quoted 8 lines
drivers/net/ethernet/arc/arc_emac.h new file +8 -0
+static int arc_emac_tx(struct sk_buff *skb, struct net_device *ndev)
+{
+	struct arc_emac_priv *priv = netdev_priv(ndev);
+	unsigned int info, len, *txbd_curr = &priv->txbd_curr;
+	dma_addr_t addr;
+	char *pkt = skb->data;
+
+	len = max_t(unsigned int, ETH_ZLEN, skb->len);

The device automatically pads, right ?

What do you mean here?

Quoted 11 lines

drivers/net/ethernet/arc/arc_emac.h

Quoted 8 lines
drivers/net/ethernet/arc/arc_emac.h new file +8 -0
+	info = priv->txbd[*txbd_curr].info;
+
+	/* EMAC still holds this buffer in its possession.
+	 * CPU must not modify this buffer descriptor
+	 */
+	if (unlikely((info & OWN_MASK) == FOR_EMAC)) {
+		netif_stop_queue(ndev);
+		return NETDEV_TX_BUSY;

It is more than unlikely: you may check for it as a bug but you should stop_queue when the tx ring is full (before returning below).

So I left this check, but add another one near bottom of Tx function to prevent mentioned situation.

Quoted 8 lines

drivers/net/ethernet/arc/arc_emac.h

Quoted 6 lines
drivers/net/ethernet/arc/arc_emac.h new file +6 -0
+	}
+
+	addr = dma_map_single(&ndev->dev, (void *)pkt, len, DMA_TO_DEVICE);
+	if (unlikely(dma_mapping_error(&ndev->dev, addr))) {
+		dev_kfree_skb(skb);
+		return NETDEV_TX_OK;

Please update tx_dropped stats.

Done.

Quoted 8 lines

drivers/net/ethernet/arc/arc_emac.h

Quoted 6 lines
drivers/net/ethernet/arc/arc_emac.h new file +6 -0
+	}
+	dma_unmap_addr_set(&priv->tx_buff[*txbd_curr], mapping, addr);
+	dma_unmap_len_set(&priv->tx_buff[*txbd_curr], len, len);
+
+	priv->tx_buff[*txbd_curr].skb = skb;
+	priv->txbd[*txbd_curr].data = pkt;

Insert memory barrier.

Very good and useful point, done with verbose comment.

Quoted 15 lines
Quoted 10 lines
drivers/net/ethernet/arc/arc_emac.h new file +10 -0
+	priv->txbd[*txbd_curr].info = FOR_EMAC | FRST_MASK | LAST_MASK | len;
+
+	*txbd_curr = (*txbd_curr + 1) % TX_BD_NUM;
+
+	arc_reg_set(priv, R_STATUS, TXPL_MASK);
+
+	skb_tx_timestamp(skb);
+
+	return NETDEV_TX_OK;
+}

[...]

Quoted 2 lines
drivers/net/ethernet/arc/arc_emac.h new file +2 -0
+static int arc_emac_probe(struct platform_device *pdev)
+{

[...]

Quoted 15 lines

drivers/net/ethernet/arc/arc_emac.h

Quoted 13 lines
drivers/net/ethernet/arc/arc_emac.h new file +13 -0
+	err = arc_mdio_probe(pdev->dev.of_node, priv);
+	if (err) {
+		dev_err(&pdev->dev, "failed to probe MII bus\n");
+		goto out;
+	}
+
+
+	priv->phy_dev = of_phy_connect(ndev, priv->phy_node,
+				       arc_emac_adjust_link, 0,
+				       PHY_INTERFACE_MODE_MII);
+	if (!priv->phy_dev) {
+		netdev_err(ndev, "of_phy_connect() failed\n");
+		return -ENODEV;

arc_mdio_remove leak.

Indeed) Fixed.

-Alexey

#11 lore raw

Alexey Brodkin <[email protected]> :

Quoted 1 line · Francois Romieu

[...]

Quoted 11 lines
Quoted 9 lines

drivers/net/ethernet/arc/arc_emac.h line 110

Quoted 7 lines
drivers/net/ethernet/arc/arc_emac.h new file +7 -0
+struct arc_emac_priv {
+	struct net_device_stats stats;
+	unsigned int clock_frequency;
+	unsigned int max_speed;
+
+	/* Pointers to BD rings - CPU side */
+	struct arc_emac_bd_t *rxbd;

There does not seem to be much need for rxbd->data.

Could you please clarify this comment? Not clear what do you mean.

Rx and Tx use the same struct but they don't work the same. They could/should use differents struct.

[...]

Quoted 11 lines
Quoted 6 lines

The descriptor entry is left unchanged. Afaiu the driver will move to the next descriptor and crash on dereferencing NULL (rx_buff->)skb next time it wraps.

I suggest avoiding holes: don't netif_receive_skb if you can't alloc a new skb.

Frankly I cannot understand how "don't netif_receive_skb" for one of the received packets helps to prevent crash on the next iteration? And I don't see a way to return any error state from NAPI poll handler. Could you please clarify your idea?

The driver assigns the otherwise-netif_received skb to the current rx descriptor as if it was a newly allocated one. The driver increases stats.rx_dropped. The rx descriptor ring doesn't ever exhibit a hole.

[...]

Quoted 11 lines
Quoted 10 lines

drivers/net/ethernet/arc/arc_emac_main.c line 539

Quoted 8 lines
drivers/net/ethernet/arc/arc_emac_main.c new file +8 -0
+static int arc_emac_tx(struct sk_buff *skb, struct net_device *ndev)
+{
+	struct arc_emac_priv *priv = netdev_priv(ndev);
+	unsigned int info, len, *txbd_curr = &priv->txbd_curr;
+	dma_addr_t addr;
+	char *pkt = skb->data;
+
+	len = max_t(unsigned int, ETH_ZLEN, skb->len);

The device automatically pads, right ?

Quoted 1 line

What do you mean here?

Does the device fill a smaller than 64 bytes packet with zeroes or may the driver leak information ? The driver should use skb_pad if the latter applies.

Signature · 1 line
Ueimor
#12 lore raw
Quoted 13 lines · Francois Romieu

Alexey Brodkin <[email protected]> :

Quoted 1 line · Francois Romieu

[...]

Quoted 10 lines
Quoted 9 lines

drivers/net/ethernet/arc/arc_emac.h line 110

Quoted 7 lines
drivers/net/ethernet/arc/arc_emac.h new file +7 -0
+struct arc_emac_priv {
+	struct net_device_stats stats;
+	unsigned int clock_frequency;
+	unsigned int max_speed;
+
+	/* Pointers to BD rings - CPU side */
+	struct arc_emac_bd_t *rxbd;

There does not seem to be much need for rxbd->data.

Quoted 4 lines
Quoted 1 line

Could you please clarify this comment? Not clear what do you mean.

Rx and Tx use the same struct but they don't work the same. They could/should use differents struct.

Structure "arc_emac_bd" represents a buffer descriptor that is used for intercommunication between EMAC and CPU for both Tx and Rx.

"info" field contains a number of flags (who's an owner of this BD now, if this buffer contains beginning and/or end of received packets, received packet length etc.) Those flags are used for both Tx and Rx. At least some of them like "owner" and "length". In case of Tx "length" indicates size of memory buffer available for receiving next packet.

"data" field is a 32-bit pointer to memory buffer.
In case of Tx buffer contains data to send, in case of Rx buffer is used for receiving data from network.

If my comment doesn't make sense to you or I misunderstood your message could you please add more clarifications for your idea?

Quoted 8 lines

[...]

Quoted 7 lines
Quoted 6 lines

The descriptor entry is left unchanged. Afaiu the driver will move to the next descriptor and crash on dereferencing NULL (rx_buff->)skb next time it wraps.

I suggest avoiding holes: don't netif_receive_skb if you can't alloc a new skb.

Quoted 8 lines
Quoted 4 lines

Frankly I cannot understand how "don't netif_receive_skb" for one of the received packets helps to prevent crash on the next iteration? And I don't see a way to return any error state from NAPI poll handler. Could you please clarify your idea?

The driver assigns the otherwise-netif_received skb to the current rx descriptor as if it was a newly allocated one. The driver increases stats.rx_dropped. The rx descriptor ring doesn't ever exhibit a hole.

Makes sense, thanks!

Quoted 12 lines

[...]

Quoted 11 lines
Quoted 10 lines

drivers/net/ethernet/arc/arc_emac_main.c line 539

Quoted 8 lines
drivers/net/ethernet/arc/arc_emac_main.c new file +8 -0
+static int arc_emac_tx(struct sk_buff *skb, struct net_device *ndev)
+{
+	struct arc_emac_priv *priv = netdev_priv(ndev);
+	unsigned int info, len, *txbd_curr = &priv->txbd_curr;
+	dma_addr_t addr;
+	char *pkt = skb->data;
+
+	len = max_t(unsigned int, ETH_ZLEN, skb->len);

The device automatically pads, right ?

Quoted 5 lines
Quoted 1 line

What do you mean here?

Does the device fill a smaller than 64 bytes packet with zeroes or may the driver leak information ? The driver should use skb_pad if the latter applies.

Done, thanks.

-Alexey

#13 lore raw

Alexey Brodkin <[email protected]> :
[...]

Quoted 2 lines

If my comment doesn't make sense to you or I misunderstood your message could you please add more clarifications for your idea?

Sorry, I temporarily forgot it was the descriptor ring itself. :o/

It would make sense to qualify the descriptor members as __{le/be}32 then use them with proper cpu_to_{le/be} wrappers _and_ mapping addresses instead of direct skb->data assignment.

Signature · 1 line
Ueimor
#14 lore raw

Query about TX BD Reclaim in Napi poll path (was Re: [PATCH v3] ethernet/arc/arc_emac - Add new driver)

In reply to Francois Romieu

Hi Francois,

Resurrecting an old thread.

Quoted 14 lines · Francois Romieu
Quoted 14 lines

+static irqreturn_t arc_emac_intr(int irq, void *dev_instance)

Quoted 13 lines
drivers/net/ethernet/arc/arc_emac_main.c new file +13 -0
+{
+	struct net_device *ndev = dev_instance;
+	struct arc_emac_priv *priv = netdev_priv(ndev);
+	struct net_device_stats *stats = &priv->stats;
+	unsigned int status;
+
+	status = arc_reg_get(priv, R_STATUS);
+	status &= ~MDIO_MASK;
+
+	/* Reset all flags except "MDIO complete"*/
+	arc_reg_set(priv, R_STATUS, status);
+
+	if (status & RXINT_MASK) {
Quoted 8 lines
Quoted 7 lines

drivers/net/ethernet/arc/arc_emac_main.c line 289

Quoted 7 lines
drivers/net/ethernet/arc/arc_emac_main.c new file +7 -0
+		if (likely(napi_schedule_prep(&priv->napi))) {
+			arc_reg_clr(priv, R_ENABLE, RXINT_MASK);
+			__napi_schedule(&priv->napi);
+		}
+	}
+
+	if (status & TXINT_MASK) {

You may consider moving everything into the napi poll handler.

I has to revisit this now-mainlined driver recently for fixing a bug. Per your suggestion above, the TX BD reclaim was moved from interrupt context to NAPI context. I was wondering if that is the right thing to do (I'm not a networking expert but have worked on this driver heavily before it was mainlined by Alexey).

In case of large burst transfers by networking stack (say a large file copy over NFS) will it not delay the TX BD reclaim possibly dropping more packets. Ofcourse doing this requires enabling Tx interrupts which adds to overall cost from a system perspective, but assuming the controller can coalesce the Tx interrupts, will it not be better.

I did a quick hack to move the TX reclaim in intr path and it seems to be doing slightly better than the current code - so the advantages are not sky high, but I want to understand the implications nevertheless.

TIA,
-Vineet