Skip to contentSkip to conversation

net: ethernet driver: Fujitsu OGMA

PATCH In review Andy Green 2 participants 3 messages Jun 20, 2014 – Jun 26, 2014

Subsystems (3)
lore
Share & tools
lore

Andy Green

author
#0 lore raw

PATCH v4 1/1 net: ethernet driver: Fujitsu OGMA

This driver adds support for "ogma", a Fujitsu Semiconductor Ltd IP Gigabit Ethernet + PHY IP used in a variety of their ARM-based ASICs.

We are preparing to upstream the main platform support for these chips which is currently waiting for mailbox driver in v7 now and waiting for a final ACK

https://lkml.org/lkml/2014/6/12/470

This driver was originally written by guys inside Fujitsu as an abstracted "you can build this for Windows as well" type code, I've removed all that, modernized various things, added runtime_pm, and ported it to work with Device Tree, using only the bindings already mentioned in

./Documentation/devicetree/bindings/net/ethernet.txt

There is only one checkpatch complaint, both about documentation for the DT vendor missing. But we will document vendor "fujitsu" in the main mach support patches. Bindings documentation is added by this patch.

The patch is based on net-next 88729dd18995a87f0f8 from today, and the unchanged patch has been tested on real hardware on an integration tree based on 3.16-rc1.

Allmodconfig doesn't generate any warnings (although on current net-next build is broken by a staging driver).

Any comments about how to further improve and align it with current best- practice for upstream Ethernet drivers are appreciated.

Changes since v3:
- Followed comments from Florian Fainelli, Joe Perches and Stephen Hemminger
- Strip messages on ENOMEM
- dma_zalloc_coherent
- refactor structs for better packing
- don't treat dma_addr_t as phys_addr_t
- rings in an enum
- handle all error paths coming from timeout-capable reg wrappers
- -ETIME -> -ETIMEDOUT
- phylib renders some sanity checking needless
- better mii name
- tx processing logic in own function
- disable NAPI before hardware
- remove kcalloc for fragment handling from start_xmit() use buffer in priv
- use phydev vars
- remove stuff phylib already takes care about
- move netif_start_queue
- simplify probe regs mapping
- move irq request and free to ndo open / close
- likewise defer allocation and free of ring buffers to ndo open / close
- genphy_soft_reset

Changes since v2:

- Followed comments from Florian Fainelli and Joe Perches
- Use Phylib
- Register as mii device
- Fill out ethtool info
- Use ethtool coalesce control struct
- use netdev-related log apis
- Many other cleanups and normalizations

Changes since v1:

- Followed comments from Francois Romieu about style issues and eliminate spinlock wrappers

- Remove remaining excess ()
- Pass checkpatch --strict now
- Use netdev_alloc_skb_ip_align as suggested
- Set hardware endian support according to cpu endianess
- change error handling targets from "bailX" to "errX"

---
 .../devicetree/bindings/net/fujitsu-ogma.txt       |   43 +
 drivers/net/ethernet/fujitsu/Kconfig               |   12 
 drivers/net/ethernet/fujitsu/Makefile              |    1 
 drivers/net/ethernet/fujitsu/ogma/Makefile         |    6 
 drivers/net/ethernet/fujitsu/ogma/ogma.h           |  377 ++++++++++++
 .../ethernet/fujitsu/ogma/ogma_desc_ring_access.c  |  630 ++++++++++++++++++++
 drivers/net/ethernet/fujitsu/ogma/ogma_ethtool.c   |   95 +++
 .../net/ethernet/fujitsu/ogma/ogma_gmac_access.c   |  296 +++++++++
 drivers/net/ethernet/fujitsu/ogma/ogma_netdev.c    |  611 +++++++++++++++++++
 drivers/net/ethernet/fujitsu/ogma/ogma_platform.c  |  340 +++++++++++
 10 files changed, 2411 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/net/fujitsu-ogma.txt
 create mode 100644 drivers/net/ethernet/fujitsu/ogma/Makefile
 create mode 100644 drivers/net/ethernet/fujitsu/ogma/ogma.h
 create mode 100644 drivers/net/ethernet/fujitsu/ogma/ogma_desc_ring_access.c
 create mode 100644 drivers/net/ethernet/fujitsu/ogma/ogma_ethtool.c
 create mode 100644 drivers/net/ethernet/fujitsu/ogma/ogma_gmac_access.c
 create mode 100644 drivers/net/ethernet/fujitsu/ogma/ogma_netdev.c
 create mode 100644 drivers/net/ethernet/fujitsu/ogma/ogma_platform.c
Documentation/devicetree/bindings/net/fujitsu-ogma.txt new file +43 -0
new file mode 100644
index 0000000..1fd680f
@@ -0,0 +1,43 @@
+* Fujistu OGMA Ethernet Controller IP
+
+Required properties:
+- compatible: Should be "fujitsu,ogma"
+- reg: Address and length of the register sets, the first is the main
+	registers, then the rdlar and tdlar regions for the SoC
+- interrupts: Should contain ethernet controller interrupt
+- clocks: phandle to any clocks to be switched by runtime_pm
+- phy-mode: See ethernet.txt file in the same directory
+- max-speed: See ethernet.txt file in the same directory
+- max-frame-size: See ethernet.txt file in the same directory, if 9000 or
+	above jumbo frames are enabled
+- local-mac-address: See ethernet.txt file in the same directory
+- phy-handle: phandle to select child phy
+
+For the child phy
+
+- compatible "ethernet-phy-ieee802.3-c22" is needed
+- device_type "ethernet-phy"
+- reg: phy address
+
+
+Example:
+	eth0: f_taiki {
+                compatible = "fujitsu,ogma";
+		reg = <0 0x31600000 0x10000>, <0 0x31618000 0x4000>, <0 0x3161c000 0x4000>;
+		interrupts = <0 163 0x4>;
+		clocks = <&clk_alw_0_8>;
+		phy-mode = "rgmii";
+		max-speed = <1000>;
+		max-frame-size = <9000>;
+		local-mac-address = [ a4 17 31 00 00 ed ];
+		phy-handle = <&ethphy0>;
+
+		#address-cells = <1>;
+		#size-cells = <0>;
+
+		ethphy0: ethernet-phy@1 {
+			device_type = "ethernet-phy";
+			compatible = "ethernet-phy-ieee802.3-c22";
+			reg = <1>;
+		};
+	};
drivers/net/ethernet/fujitsu/Kconfig +12 -0
index 1085257..bbe3ee8 100644
@@ -28,4 +28,16 @@ config PCMCIA_FMVJ18X
 	  To compile this driver as a module, choose M here: the module will be
 	  called fmvj18x_cs.  If unsure, say N.
 
+config NET_FUJITSU_OGMA
+	tristate "Fujitsu OGMA network support"
+	depends on OF
+	select PHYLIB
+	select MII
+help
+	  Enable for OGMA support of Fujitsu FGAMC4 IP
+	  Provides Gigabit ethernet support
+
+	  To compile this driver as a module, choose M here: the module will be
+	  called ogma.  If unsure, say N.
+
 endif # NET_VENDOR_FUJITSU
drivers/net/ethernet/fujitsu/Makefile +1 -0
index 21561fd..b90a445 100644
@@ -3,3 +3,4 @@
 #
 
 obj-$(CONFIG_PCMCIA_FMVJ18X) += fmvj18x_cs.o
+obj-$(CONFIG_NET_FUJITSU_OGMA) += ogma/
drivers/net/ethernet/fujitsu/ogma/Makefile new file +6 -0
new file mode 100644
index 0000000..8661027
@@ -0,0 +1,6 @@
+obj-m := ogma.o
+ogma-objs := ogma_desc_ring_access.o \
+		ogma_netdev.o \
+		ogma_ethtool.o \
+		ogma_platform.o \
+		ogma_gmac_access.o
drivers/net/ethernet/fujitsu/ogma/ogma.h new file +377 -0

377 lines not on this page · raw message

drivers/net/ethernet/fujitsu/ogma/ogma_desc_ring_access.c new file +630 -0

630 lines not on this page · raw message

drivers/net/ethernet/fujitsu/ogma/ogma_ethtool.c new file +95 -0

95 lines not on this page · raw message

drivers/net/ethernet/fujitsu/ogma/ogma_gmac_access.c new file +296 -0

296 lines not on this page · raw message

drivers/net/ethernet/fujitsu/ogma/ogma_netdev.c new file +611 -0

611 lines not on this page · raw message

drivers/net/ethernet/fujitsu/ogma/ogma_platform.c new file +340 -0

340 lines not on this page · raw message

maintainer
#1 lore raw

From: Andy Green <[email protected]>
Date: Fri, 20 Jun 2014 15:09:21 +0800

drivers/net/ethernet/fujitsu/ogma/ogma.h line 257

Quoted 2 lines
drivers/net/ethernet/fujitsu/ogma/ogma.h new file +2 -0
+	u8 running:1;
+	u8 full:1;

Please use bool and true/false.

drivers/net/ethernet/fujitsu/ogma/ogma.h line 292

Quoted 3 lines
drivers/net/ethernet/fujitsu/ogma/ogma.h new file +3 -0
+	u8 rx_cksum_offload_flag:1;
+	u8 actual_duplex:1;
+	u8 irq_registered:1;

Likewise.

drivers/net/ethernet/fujitsu/ogma/ogma.h line 323

Quoted 4 lines
drivers/net/ethernet/fujitsu/ogma/ogma.h new file +4 -0
+};
+
+
+static inline void ogma_writel(struct ogma_priv *priv, u32 reg_addr, u32 val)

Please no more than one empty line between top level declarations.

drivers/net/ethernet/fujitsu/ogma/ogma.h line 335

Quoted 3 lines
drivers/net/ethernet/fujitsu/ogma/ogma.h new file +3 -0
+static inline void ogma_mark_skb_type(void *skb, bool recv_buf_flag)
+{
+	struct sk_buff *skb_p = skb;

There is zero reason to declar 'skb' as a void pointer, "struct sk_buff *" is fine and all callers pass the proper type.

Furthermore, it is much cleaner to define an explicit skb control block structure to cast skb->cb to and from.

drivers/net/ethernet/fujitsu/ogma/ogma_desc_ring_access.c line 23

Quoted 5 lines
drivers/net/ethernet/fujitsu/ogma/ogma_desc_ring_access.c new file +5 -0
+static const u32 ads_irq_set[] = {
+	OGMA_REG_NRM_TX_INTEN_SET,
+	OGMA_REG_NRM_RX_INTEN_SET,
+};
+static const u32 desc_ring_irq_inten_clr_reg_addr[] = {

One empty line between top-level object declarations.

drivers/net/ethernet/fujitsu/ogma/ogma_desc_ring_access.c line 27

Quoted 4 lines
drivers/net/ethernet/fujitsu/ogma/ogma_desc_ring_access.c new file +4 -0
+	OGMA_REG_NRM_TX_INTEN_CLR,
+	OGMA_REG_NRM_RX_INTEN_CLR,
+};
+static const u32 int_tmr_reg_addr[] = {

Likewise.

drivers/net/ethernet/fujitsu/ogma/ogma_desc_ring_access.c line 31

Quoted 4 lines
drivers/net/ethernet/fujitsu/ogma/ogma_desc_ring_access.c new file +4 -0
+	OGMA_REG_NRM_TX_TXINT_TMR,
+	OGMA_REG_NRM_RX_RXINT_TMR,
+};
+static const u32 rx_pkt_cnt_reg_addr[] = {

Likewise.

drivers/net/ethernet/fujitsu/ogma/ogma_desc_ring_access.c line 35

Quoted 4 lines
drivers/net/ethernet/fujitsu/ogma/ogma_desc_ring_access.c new file +4 -0
+	0,
+	OGMA_REG_NRM_RX_PKTCNT,
+};
+static const u32 tx_pkt_cnt_reg_addr[] = {

Likewise. etc. there are more such cases, please audit your entire driver submission for this.

Quoted 11 lines
drivers/net/ethernet/fujitsu/ogma/ogma_desc_ring_access.c new file +11 -0
+int ogma_ring_irq_enable(struct ogma_priv *priv, enum ogma_rings id, u32 irqf)
+{
+	struct ogma_desc_ring *desc = &priv->desc_ring[id];
+	int ret = 0;
+
+	spin_lock(&desc->spinlock_desc);
+
+	if (!desc->running) {
+		netif_err(priv, drv, priv->net_device,
+			  "desc ring not running\n");
+		ret = -ENODEV;

drivers/net/ethernet/fujitsu/ogma/ogma_desc_ring_access.c line 81

Quoted 10 lines
drivers/net/ethernet/fujitsu/ogma/ogma_desc_ring_access.c new file +10 -0
+		goto err;
+	}
+
+	ogma_writel(priv, ads_irq_set[id], irqf);
+
+err:
+	spin_unlock(&desc->spinlock_desc);
+
+	return ret;
+}

Taking this lock just for this sanity check is _WAY_ overkill. Just do the single atomic register write and that's it.

A 21 line function when a 1 line statement would do... :-/

Quoted 8 lines
drivers/net/ethernet/fujitsu/ogma/ogma_desc_ring_access.c new file +8 -0
+static int alloc_rx_pkt_buf(struct ogma_priv *priv, struct ogma_frag_info *info,
+			    struct sk_buff **skb)
+{
+	*skb = netdev_alloc_skb_ip_align(priv->net_device, info->len);
+	if (!*skb)
+		return -ENOMEM;
+
+	ogma_mark_skb_type(*skb, OGMA_RING_RX);

drivers/net/ethernet/fujitsu/ogma/ogma_desc_ring_access.c line 105

Quoted 10 lines
drivers/net/ethernet/fujitsu/ogma/ogma_desc_ring_access.c new file +10 -0
+	info->addr = (*skb)->data;
+	info->dma_addr = dma_map_single(priv->dev, info->addr, info->len,
+					DMA_FROM_DEVICE);
+	if (dma_mapping_error(priv->dev, info->dma_addr)) {
+		dev_kfree_skb(*skb);
+		return -ENOMEM;
+	}
+
+	return 0;
+}

Do not pass pointers to pointers unless absolutely necessary, and here it is not. *skb never changes, so just pass plain "struct sk_buff *" and avoid all of these excess address-of and dereference operations.

drivers/net/ethernet/fujitsu/ogma/ogma_desc_ring_access.c line 182

Quoted 13 lines
drivers/net/ethernet/fujitsu/ogma/ogma_desc_ring_access.c new file +13 -0
+void ogma_free_desc_ring(struct ogma_priv *priv, struct ogma_desc_ring *desc)
+{
+	if (desc->ring_vaddr && desc->frag && desc->priv)
+		ogma_uninit_pkt_desc_ring(priv, desc);
+
+	if (desc->ring_vaddr)
+		dma_free_coherent(priv->dev, desc->len * DESC_NUM,
+				  desc->ring_vaddr, desc->desc_phys);
+	kfree(desc->frag);
+	kfree(desc->priv);
+
+	memset(desc, 0, sizeof(*desc));
+}

This memset makes no sense, especially as "zero" is not an appropriate initial state for desc->spinlock_desc.

Just NULL out the items explicitly, one by one, as you free the resources they point to.

That's much easier to audit than this sledgehammer memset().

drivers/net/ethernet/fujitsu/ogma/ogma_desc_ring_access.c line 202

Quoted 5 lines
drivers/net/ethernet/fujitsu/ogma/ogma_desc_ring_access.c new file +5 -0
+	de->data_buf_addr = info->dma_addr;
+	de->buf_len_info = info->len;
+	de->reserved = 0;
+	smp_wmb(); /* make sure descriptor body is written */
+	de->attr = attr; /* write this last */

You need a larger and more detailed comment than this.

Memory barriers must describe exactly what must be seen, in what order, and by _who_.

author
#2 lore raw
Quoted 2 lines · David Miller

From: Andy Green <[email protected]>
Date: Fri, 20 Jun 2014 15:09:21 +0800

Thanks for taking the time to review it.

Quoted 10 lines

drivers/net/ethernet/fujitsu/ogma/ogma.h line 257

Quoted 2 lines
drivers/net/ethernet/fujitsu/ogma/ogma.h new file +2 -0
+     u8 running:1;
+     u8 full:1;

Please use bool and true/false.

drivers/net/ethernet/fujitsu/ogma/ogma.h line 292

Quoted 3 lines
drivers/net/ethernet/fujitsu/ogma/ogma.h new file +3 -0
+     u8 rx_cksum_offload_flag:1;
+     u8 actual_duplex:1;
+     u8 irq_registered:1;

Likewise.

I got rid of all the non-register parsing bitfields and replaced by bool, and checked they're set by true / false.

Quoted 6 lines

drivers/net/ethernet/fujitsu/ogma/ogma.h line 323

Quoted 4 lines
drivers/net/ethernet/fujitsu/ogma/ogma.h new file +4 -0
+};
+
+
+static inline void ogma_writel(struct ogma_priv *priv, u32 reg_addr, u32 val)

Please no more than one empty line between top level declarations.

OK

Quoted 9 lines

drivers/net/ethernet/fujitsu/ogma/ogma.h line 335

Quoted 3 lines
drivers/net/ethernet/fujitsu/ogma/ogma.h new file +3 -0
+static inline void ogma_mark_skb_type(void *skb, bool recv_buf_flag)
+{
+     struct sk_buff *skb_p = skb;

There is zero reason to declar 'skb' as a void pointer, "struct sk_buff *" is fine and all callers pass the proper type.

Furthermore, it is much cleaner to define an explicit skb control block structure to cast skb->cb to and from.

You're right it's just noise from the original implementation. I added a struct to contain the bool we actually use.

Quoted 22 lines

drivers/net/ethernet/fujitsu/ogma/ogma_desc_ring_access.c line 23

Quoted 5 lines
drivers/net/ethernet/fujitsu/ogma/ogma_desc_ring_access.c new file +5 -0
+static const u32 ads_irq_set[] = {
+     OGMA_REG_NRM_TX_INTEN_SET,
+     OGMA_REG_NRM_RX_INTEN_SET,
+};
+static const u32 desc_ring_irq_inten_clr_reg_addr[] = {

One empty line between top-level object declarations.

drivers/net/ethernet/fujitsu/ogma/ogma_desc_ring_access.c line 27

Quoted 4 lines
drivers/net/ethernet/fujitsu/ogma/ogma_desc_ring_access.c new file +4 -0
+     OGMA_REG_NRM_TX_INTEN_CLR,
+     OGMA_REG_NRM_RX_INTEN_CLR,
+};
+static const u32 int_tmr_reg_addr[] = {

Likewise.

drivers/net/ethernet/fujitsu/ogma/ogma_desc_ring_access.c line 31

Quoted 4 lines
drivers/net/ethernet/fujitsu/ogma/ogma_desc_ring_access.c new file +4 -0
+     OGMA_REG_NRM_TX_TXINT_TMR,
+     OGMA_REG_NRM_RX_RXINT_TMR,
+};
+static const u32 rx_pkt_cnt_reg_addr[] = {

Likewise.

Quoted 7 lines

drivers/net/ethernet/fujitsu/ogma/ogma_desc_ring_access.c line 35

Quoted 4 lines
drivers/net/ethernet/fujitsu/ogma/ogma_desc_ring_access.c new file +4 -0
+     0,
+     OGMA_REG_NRM_RX_PKTCNT,
+};
+static const u32 tx_pkt_cnt_reg_addr[] = {

Likewise. etc. there are more such cases, please audit your entire driver submission for this.

It seemed that was the only place where there's a bunch all together, I spaced them out.

Quoted 25 lines
Quoted 11 lines
drivers/net/ethernet/fujitsu/ogma/ogma_desc_ring_access.c new file +11 -0
+int ogma_ring_irq_enable(struct ogma_priv *priv, enum ogma_rings id, u32 irqf)
+{
+     struct ogma_desc_ring *desc = &priv->desc_ring[id];
+     int ret = 0;
+
+     spin_lock(&desc->spinlock_desc);
+
+     if (!desc->running) {
+             netif_err(priv, drv, priv->net_device,
+                       "desc ring not running\n");
+             ret = -ENODEV;

drivers/net/ethernet/fujitsu/ogma/ogma_desc_ring_access.c line 81

Quoted 10 lines
drivers/net/ethernet/fujitsu/ogma/ogma_desc_ring_access.c new file +10 -0
+             goto err;
+     }
+
+     ogma_writel(priv, ads_irq_set[id], irqf);
+
+err:
+     spin_unlock(&desc->spinlock_desc);
+
+     return ret;
+}

Taking this lock just for this sanity check is _WAY_ overkill. Just do the single atomic register write and that's it.

Quoted 1 line

A 21 line function when a 1 line statement would do... :-/

OK, thanks for pointing it out.

Quoted 14 lines
Quoted 14 lines
drivers/net/ethernet/fujitsu/ogma/ogma_desc_ring_access.c new file +14 -0
+static int alloc_rx_pkt_buf(struct ogma_priv *priv, struct ogma_frag_info *info,
+                         struct sk_buff **skb)
+{
+     *skb = netdev_alloc_skb_ip_align(priv->net_device, info->len);
+     if (!*skb)
+             return -ENOMEM;
+
+     ogma_mark_skb_type(*skb, OGMA_RING_RX);
+     info->addr = (*skb)->data;
+     info->dma_addr = dma_map_single(priv->dev, info->addr, info->len,
+                                     DMA_FROM_DEVICE);
+     if (dma_mapping_error(priv->dev, info->dma_addr)) {
+             dev_kfree_skb(*skb);
+             return -ENOMEM;
Quoted 8 lines

drivers/net/ethernet/fujitsu/ogma/ogma_desc_ring_access.c line 105

Quoted 4 lines
drivers/net/ethernet/fujitsu/ogma/ogma_desc_ring_access.c new file +4 -0
+     }
+
+     return 0;
+}

Do not pass pointers to pointers unless absolutely necessary, and here it is not. *skb never changes, so just pass plain "struct sk_buff *" and avoid all of these excess address-of and dereference operations.

No the code is allocating an skb, so he has a use for ** here to set the pointer in the caller; *skb does change, it's set on the first line.

But I agree it's not necessary to do it like that, I changed it to return the skb pointer and removed the skb arg.

Quoted 17 lines

drivers/net/ethernet/fujitsu/ogma/ogma_desc_ring_access.c line 182

Quoted 13 lines
drivers/net/ethernet/fujitsu/ogma/ogma_desc_ring_access.c new file +13 -0
+void ogma_free_desc_ring(struct ogma_priv *priv, struct ogma_desc_ring *desc)
+{
+     if (desc->ring_vaddr && desc->frag && desc->priv)
+             ogma_uninit_pkt_desc_ring(priv, desc);
+
+     if (desc->ring_vaddr)
+             dma_free_coherent(priv->dev, desc->len * DESC_NUM,
+                               desc->ring_vaddr, desc->desc_phys);
+     kfree(desc->frag);
+     kfree(desc->priv);
+
+     memset(desc, 0, sizeof(*desc));
+}

This memset makes no sense, especially as "zero" is not an appropriate initial state for desc->spinlock_desc.

Quoted 4 lines

Just NULL out the items explicitly, one by one, as you free the resources they point to.

That's much easier to audit than this sledgehammer memset().

OK.

Quoted 10 lines

drivers/net/ethernet/fujitsu/ogma/ogma_desc_ring_access.c line 202

Quoted 5 lines
drivers/net/ethernet/fujitsu/ogma/ogma_desc_ring_access.c new file +5 -0
+     de->data_buf_addr = info->dma_addr;
+     de->buf_len_info = info->len;
+     de->reserved = 0;
+     smp_wmb(); /* make sure descriptor body is written */
+     de->attr = attr; /* write this last */

You need a larger and more detailed comment than this.

Memory barriers must describe exactly what must be seen, in what order, and by _who_.

I explained in the comment now the reasoning.

Thanks again for your comments.

Jassi Brar will send the v5 patch shortly as part of a series including mailbox and the arch support patches for the SoCs that use the ogma driver.

-Andy