Skip to contentSkip to conversation

mediatek: add support for MediaTek Ethernet MAC

PATCH Superseded Bartosz Golaszewski 5 participants 23 messages May 11, 2020 – May 20, 2020

Subsystems (5)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 cover mediatek: add support for MediaTek Ethernet MAC

From: Bartosz Golaszewski <[email protected]>

This adds support for the Ethernet Controller present on MediaTeK SoCs from the MT8* family.

The first two patches add binding documents for the PERICFG syscon and for the MAC itself.

Patches 3/14 & 4/14 do some cleanup of the mediatek ethernet drivers directory.

Patch 5/14 provides a new helper that allows to retrieve the address of the net_device associated with given private data address.

Patches 6-8/14 introduce the managed variant of register_netdev().

Patch 9/11 adds the new ethernet driver.

The rest of the patches add DT fixups for the boards already supported upstream.

v1 -> v2:
- add a generic helper for retrieving the net_device associated with given private data
- fix several typos in commit messages
- remove MTK_MAC_VERSION and don't set the driver version
- use NET_IP_ALIGN instead of a magic number (2) but redefine it as it defaults to 0 on arm64
- don't manually turn the carrier off in mtk_mac_enable()
- process TX cleanup in napi poll callback
- configure pause in the adjust_link callback
- use regmap_read_poll_timeout() instead of handcoding the polling
- use devres_find() to verify that struct net_device is managed by devres in devm_register_netdev()
- add a patch moving all networking devres helpers into net/devres.c
- tweak the dma barriers: remove where unnecessary and add comments to the remaining barriers
- don't reset internal counters when enabling the NIC
- set the net_device's mtu size instead of checking the framesize in ndo_start_xmit() callback
- fix a race condition in waking up the netif queue
- don't emit log messages on OOM errors
- use dma_set_mask_and_coherent()
- use eth_hw_addr_random()
- rework the receive callback so that we reuse the previous skb if unmapping fails, like we already do if skb allocation fails
- rework hash table operations: add proper timeout handling and clear bits when appropriate

Bartosz Golaszewski (14):
dt-bindings: arm: add a binding document for MediaTek PERICFG
controller
dt-bindings: net: add a binding document for MediaTek Ethernet MAC
net: ethernet: mediatek: rename Kconfig prompt
net: ethernet: mediatek: remove unnecessary spaces from Makefile
net: core: provide priv_to_netdev()
net: move devres helpers into a separate source file
net: devres: define a separate devres structure for
devm_alloc_etherdev()
net: devres: provide devm_register_netdev()
net: ethernet: mtk-eth-mac: new driver
ARM64: dts: mediatek: add pericfg syscon to mt8516.dtsi
ARM64: dts: mediatek: add the ethernet node to mt8516.dtsi
ARM64: dts: mediatek: add an alias for ethernet0 for pumpkin boards
ARM64: dts: mediatek: add ethernet pins for pumpkin boards
ARM64: dts: mediatek: enable ethernet on pumpkin boards

 .../arm/mediatek/mediatek,pericfg.yaml        |   34 +
 .../bindings/net/mediatek,eth-mac.yaml        |   80 +
 arch/arm64/boot/dts/mediatek/mt8516.dtsi      |   17 +
 .../boot/dts/mediatek/pumpkin-common.dtsi     |   34 +
 drivers/net/ethernet/mediatek/Kconfig         |    8 +-
 drivers/net/ethernet/mediatek/Makefile        |    3 +-
 drivers/net/ethernet/mediatek/mtk_eth_mac.c   | 1561 +++++++++++++++++
 include/linux/netdevice.h                     |   14 +
 net/Makefile                                  |    2 +-
 net/devres.c                                  |   95 +
 net/ethernet/eth.c                            |   28 -
 11 files changed, 1845 insertions(+), 31 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/arm/mediatek/mediatek,pericfg.yaml
 create mode 100644 Documentation/devicetree/bindings/net/mediatek,eth-mac.yaml
 create mode 100644 drivers/net/ethernet/mediatek/mtk_eth_mac.c
 create mode 100644 net/devres.c
Signature · 1 line
2.25.0
#1 lore raw

PATCH v2 1/14 dt-bindings: arm: add a binding document for MediaTek PERICFG controller

From: Bartosz Golaszewski <[email protected]>

This adds a binding document for the PERICFG controller present on MediaTek SoCs. For now the only variant supported is 'mt8516-pericfg'.

---
 .../arm/mediatek/mediatek,pericfg.yaml        | 34 +++++++++++++++++++
 1 file changed, 34 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/arm/mediatek/mediatek,pericfg.yaml
Documentation/devicetree/bindings/arm/mediatek/mediatek,pericfg.yaml new file +34 -0
new file mode 100644
index 000000000000..74b2a6173ffb
@@ -0,0 +1,34 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: "http://devicetree.org/schemas/arm/mediatek/mediatek,pericfg.yaml#"
+$schema: "http://devicetree.org/meta-schemas/core.yaml#"
+
+title: MediaTek Peripheral Configuration Controller
+
+maintainers:
+  - Bartosz Golaszewski <[email protected]>
+
+properties:
+  compatible:
+    oneOf:
+      - items:
+        - enum:
+          - mediatek,pericfg
+        - const: syscon
+
+  reg:
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+
+additionalProperties: false
+
+examples:
+  - |
+    pericfg: pericfg@10003050 {
+        compatible = "mediatek,mt8516-pericfg", "syscon";
+        reg = <0 0x10003050 0 0x1000>;
+    };
Signature · 1 line
2.25.0
#5 lore raw

PATCH v2 3/14 net: ethernet: mediatek: rename Kconfig prompt

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

We'll soon by adding a second MediaTek Ethernet driver so modify the Kconfig prompt.

---
 drivers/net/ethernet/mediatek/Kconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/mediatek/Kconfig +1 -1
index 4968352ba188..5079b8090f16 100644
@@ -1,6 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0-only
 config NET_VENDOR_MEDIATEK
-	bool "MediaTek ethernet driver"
+	bool "MediaTek devices"
 	depends on ARCH_MEDIATEK || SOC_MT7621 || SOC_MT7620
 	---help---
 	  If you have a Mediatek SoC with ethernet, say Y.
Signature · 1 line
2.25.0
#6 lore raw

PATCH v2 4/14 net: ethernet: mediatek: remove unnecessary spaces from Makefile

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

The Makefile formatting in the kernel tree usually doesn't use tabs, so remove them before we add a second driver.

---
 drivers/net/ethernet/mediatek/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/mediatek/Makefile +1 -1
index 2d8362f9341b..3362fb7ef859 100644
@@ -3,5 +3,5 @@
 # Makefile for the Mediatek SoCs built-in ethernet macs
 #
 
-obj-$(CONFIG_NET_MEDIATEK_SOC)                 += mtk_eth.o
+obj-$(CONFIG_NET_MEDIATEK_SOC) += mtk_eth.o
 mtk_eth-y := mtk_eth_soc.o mtk_sgmii.o mtk_eth_path.o
Signature · 1 line
2.25.0
#7 lore raw

PATCH v2 5/14 net: core: provide priv_to_netdev()

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Appropriate amount of extra memory for private data is allocated at the end of struct net_device. We have a helper - netdev_priv() - that returns its address but we don't have the reverse: a function which given the address of the private data, returns the address of struct net_device.

This has caused many drivers to store the pointer to net_device in the private data structure, which basically means storing the pointer to a structure in this very structure.

This patch proposes to add priv_to_netdev() - a helper which converts the address of the private data to the address of the associated net_device.

---
 include/linux/netdevice.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)
include/linux/netdevice.h +12 -0
index 130a668049ab..933c6808a87f 100644
@@ -2249,6 +2249,18 @@ static inline void *netdev_priv(const struct net_device *dev)
 	return (char *)dev + ALIGN(sizeof(struct net_device), NETDEV_ALIGN);
 }
 
+/**
+ *	priv_to_netdev - get the net_device from private data
+ *	@priv: net_device private data
+ *
+ * Get the address of network device associated with this private data.
+ */
+static inline struct net_device *priv_to_netdev(void *priv)
+{
+	priv = (char *)priv - ALIGN(sizeof(struct net_device), NETDEV_ALIGN);
+	return (struct net_device *)priv;
+}
+
 /* Set the sysfs physical device reference for the network logical device
  * if set prior to registration will cause a symlink during initialization.
  */
Signature · 1 line
2.25.0
#12 lore raw

PATCH v2 6/14 net: move devres helpers into a separate source file

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

There's currently only a single devres helper in net/ - devm variant of alloc_etherdev. Let's move it to net/devres.c with the intention of assing a second one: devm_register_netdev(). This new routine will need to know the address of the release function of devm_alloc_etherdev() so that it can verify (using devres_find()) that the struct net_device that's being passed to it is also resource managed.

---
 net/Makefile       |  2 +-
 net/devres.c       | 36 ++++++++++++++++++++++++++++++++++++
 net/ethernet/eth.c | 28 ----------------------------
 3 files changed, 37 insertions(+), 29 deletions(-)
 create mode 100644 net/devres.c
net/Makefile +1 -1
index 07ea48160874..5744bf1997fd 100644
@@ -6,7 +6,7 @@
 # Rewritten to use lists instead of if-statements.
 #
 
-obj-$(CONFIG_NET)		:= socket.o core/
+obj-$(CONFIG_NET)		:= devres.o socket.o core/
 
 tmp-$(CONFIG_COMPAT) 		:= compat.o
 obj-$(CONFIG_NET)		+= $(tmp-y)
net/devres.c new file +36 -0
new file mode 100644
index 000000000000..c1465d9f9019
@@ -0,0 +1,36 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * This file contains all networking devres helpers.
+ */
+
+#include <linux/device.h>
+#include <linux/etherdevice.h>
+#include <linux/netdevice.h>
+
+static void devm_free_netdev(struct device *dev, void *res)
+{
+	free_netdev(*(struct net_device **)res);
+}
+
+struct net_device *devm_alloc_etherdev_mqs(struct device *dev, int sizeof_priv,
+					   unsigned int txqs, unsigned int rxqs)
+{
+	struct net_device **dr;
+	struct net_device *netdev;
+
+	dr = devres_alloc(devm_free_netdev, sizeof(*dr), GFP_KERNEL);
+	if (!dr)
+		return NULL;
+
+	netdev = alloc_etherdev_mqs(sizeof_priv, txqs, rxqs);
+	if (!netdev) {
+		devres_free(dr);
+		return NULL;
+	}
+
+	*dr = netdev;
+	devres_add(dev, dr);
+
+	return netdev;
+}
+EXPORT_SYMBOL(devm_alloc_etherdev_mqs);
net/ethernet/eth.c +0 -28
index c8b903302ff2..dac65180c4ef 100644
@@ -400,34 +400,6 @@ struct net_device *alloc_etherdev_mqs(int sizeof_priv, unsigned int txqs,
 }
 EXPORT_SYMBOL(alloc_etherdev_mqs);
 
-static void devm_free_netdev(struct device *dev, void *res)
-{
-	free_netdev(*(struct net_device **)res);
-}
-
-struct net_device *devm_alloc_etherdev_mqs(struct device *dev, int sizeof_priv,
-					   unsigned int txqs, unsigned int rxqs)
-{
-	struct net_device **dr;
-	struct net_device *netdev;
-
-	dr = devres_alloc(devm_free_netdev, sizeof(*dr), GFP_KERNEL);
-	if (!dr)
-		return NULL;
-
-	netdev = alloc_etherdev_mqs(sizeof_priv, txqs, rxqs);
-	if (!netdev) {
-		devres_free(dr);
-		return NULL;
-	}
-
-	*dr = netdev;
-	devres_add(dev, dr);
-
-	return netdev;
-}
-EXPORT_SYMBOL(devm_alloc_etherdev_mqs);
-
 ssize_t sysfs_format_mac(char *buf, const unsigned char *addr, int len)
 {
 	return scnprintf(buf, PAGE_SIZE, "%*phC\n", len, addr);
Signature · 1 line
2.25.0
#14 lore raw

PATCH v2 8/14 net: devres: provide devm_register_netdev()

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Provide devm_register_netdev() - a device resource managed variant of register_netdev(). This new helper will only work for net_device structs that are also already managed by devres.

---
 include/linux/netdevice.h |  2 ++
 net/devres.c              | 55 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+)
include/linux/netdevice.h +2 -0
index 933c6808a87f..d3d1b9251ae5 100644
@@ -4220,6 +4220,8 @@ struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
 int register_netdev(struct net_device *dev);
 void unregister_netdev(struct net_device *dev);
 
+int devm_register_netdev(struct device *dev, struct net_device *ndev);
+
 /* General hardware address lists handling functions */
 int __hw_addr_sync(struct netdev_hw_addr_list *to_list,
 		   struct netdev_hw_addr_list *from_list, int addr_len);
net/devres.c +55 -0

58 lines not on this page · raw message

Signature · 1 line
2.25.0
#15 lore raw

PATCH v2 9/14 net: ethernet: mtk-eth-mac: new driver

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

This adds the driver for the MediaTek Ethernet MAC used on the MT8* SoC family. For now we only support full-duplex.

---
 drivers/net/ethernet/mediatek/Kconfig       |    6 +
 drivers/net/ethernet/mediatek/Makefile      |    1 +
 drivers/net/ethernet/mediatek/mtk_eth_mac.c | 1561 +++++++++++++++++++
 3 files changed, 1568 insertions(+)
 create mode 100644 drivers/net/ethernet/mediatek/mtk_eth_mac.c
drivers/net/ethernet/mediatek/Kconfig +6 -0
index 5079b8090f16..5c3793076765 100644
@@ -14,4 +14,10 @@ config NET_MEDIATEK_SOC
 	  This driver supports the gigabit ethernet MACs in the
 	  MediaTek SoC family.
 
+config NET_MEDIATEK_MAC
+	tristate "MediaTek Ethernet MAC support"
+	select PHYLIB
+	help
+	  This driver supports the ethernet IP on MediaTek MT85** SoCs.
+
 endif #NET_VENDOR_MEDIATEK
drivers/net/ethernet/mediatek/Makefile +1 -0
index 3362fb7ef859..f7f5638943a0 100644
@@ -5,3 +5,4 @@
 
 obj-$(CONFIG_NET_MEDIATEK_SOC) += mtk_eth.o
 mtk_eth-y := mtk_eth_soc.o mtk_sgmii.o mtk_eth_path.o
+obj-$(CONFIG_NET_MEDIATEK_MAC) += mtk_eth_mac.o
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +1561 -0

1,561 lines not on this page · raw message

Signature · 1 line
2.25.0
#18 lore raw

PATCH v2 10/14 ARM64: dts: mediatek: add pericfg syscon to mt8516.dtsi

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

This adds support for the PERICFG register range as a syscon. This will soon be used by the MediaTek Ethernet MAC driver for NIC configuration.

---
 arch/arm64/boot/dts/mediatek/mt8516.dtsi | 5 +++++
 1 file changed, 5 insertions(+)
arch/arm64/boot/dts/mediatek/mt8516.dtsi +5 -0
index 2f8adf042195..8cedaf74ae86 100644
@@ -191,6 +191,11 @@ infracfg: infracfg@10001000 {
 			#clock-cells = <1>;
 		};
 
+		pericfg: pericfg@10003050 {
+			compatible = "mediatek,mt8516-pericfg", "syscon";
+			reg = <0 0x10003050 0 0x1000>;
+		};
+
 		apmixedsys: apmixedsys@10018000 {
 			compatible = "mediatek,mt8516-apmixedsys", "syscon";
 			reg = <0 0x10018000 0 0x710>;
Signature · 1 line
2.25.0
#20 lore raw

PATCH v2 12/14 ARM64: dts: mediatek: add an alias for ethernet0 for pumpkin boards

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Add the ethernet0 alias for ethernet so that u-boot can find this node and fill in the MAC address.

---
 arch/arm64/boot/dts/mediatek/pumpkin-common.dtsi | 1 +
 1 file changed, 1 insertion(+)
arch/arm64/boot/dts/mediatek/pumpkin-common.dtsi +1 -0
index a31093d7142b..97d9b000c37e 100644
@@ -9,6 +9,7 @@
 / {
 	aliases {
 		serial0 = &uart0;
+		ethernet0 = &ethernet;
 	};
 
 	chosen {
Signature · 1 line
2.25.0
#19 lore raw

PATCH v2 11/14 ARM64: dts: mediatek: add the ethernet node to mt8516.dtsi

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Add the Ethernet MAC node to mt8516.dtsi. This defines parameters common to all the boards based on this SoC.

---
 arch/arm64/boot/dts/mediatek/mt8516.dtsi | 12 ++++++++++++
 1 file changed, 12 insertions(+)
arch/arm64/boot/dts/mediatek/mt8516.dtsi +12 -0
index 8cedaf74ae86..89af661e7f63 100644
@@ -406,6 +406,18 @@ mmc2: mmc@11170000 {
 			status = "disabled";
 		};
 
+		ethernet: ethernet@11180000 {
+			compatible = "mediatek,mt8516-eth";
+			reg = <0 0x11180000 0 0x1000>;
+			mediatek,pericfg = <&pericfg>;
+			interrupts = <GIC_SPI 111 IRQ_TYPE_LEVEL_LOW>;
+			clocks = <&topckgen CLK_TOP_RG_ETH>,
+				 <&topckgen CLK_TOP_66M_ETH>,
+				 <&topckgen CLK_TOP_133M_ETH>;
+			clock-names = "core", "reg", "trans";
+			status = "disabled";
+		};
+
 		rng: rng@1020c000 {
 			compatible = "mediatek,mt8516-rng",
 				     "mediatek,mt7623-rng";
Signature · 1 line
2.25.0
#22 lore raw

PATCH v2 14/14 ARM64: dts: mediatek: enable ethernet on pumpkin boards

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Add remaining properties to the ethernet node and enable it.

---
 .../boot/dts/mediatek/pumpkin-common.dtsi      | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
arch/arm64/boot/dts/mediatek/pumpkin-common.dtsi +18 -0
index 4b1d5f69aba6..dfceffe6950a 100644
@@ -167,6 +167,24 @@ &uart0 {
 	status = "okay";
 };
 
+&ethernet {
+	pinctrl-names = "default";
+	pinctrl-0 = <&ethernet_pins_default>;
+	phy-handle = <&eth_phy>;
+	phy-mode = "rmii";
+	mac-address = [00 00 00 00 00 00];
+	status = "okay";
+
+	mdio {
+		#address-cells = <1>;
+		#size-cells = <0>;
+
+		eth_phy: ethernet-phy@0 {
+			reg = <0>;
+		};
+	};
+};
+
 &usb0 {
 	status = "okay";
 	dr_mode = "peripheral";
Signature · 1 line
2.25.0
#21 lore raw

PATCH v2 13/14 ARM64: dts: mediatek: add ethernet pins for pumpkin boards

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Setup the pin control for the Ethernet MAC.

---
 arch/arm64/boot/dts/mediatek/pumpkin-common.dtsi | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
arch/arm64/boot/dts/mediatek/pumpkin-common.dtsi +15 -0
index 97d9b000c37e..4b1d5f69aba6 100644
@@ -219,4 +219,19 @@ gpio_mux_int_n_pin {
 			bias-pull-up;
 		};
 	};
+
+	ethernet_pins_default: ethernet {
+		pins_ethernet {
+			pinmux = <MT8516_PIN_0_EINT0__FUNC_EXT_TXD0>,
+				 <MT8516_PIN_1_EINT1__FUNC_EXT_TXD1>,
+				 <MT8516_PIN_5_EINT5__FUNC_EXT_RXER>,
+				 <MT8516_PIN_6_EINT6__FUNC_EXT_RXC>,
+				 <MT8516_PIN_7_EINT7__FUNC_EXT_RXDV>,
+				 <MT8516_PIN_8_EINT8__FUNC_EXT_RXD0>,
+				 <MT8516_PIN_9_EINT9__FUNC_EXT_RXD1>,
+				 <MT8516_PIN_12_EINT12__FUNC_EXT_TXEN>,
+				 <MT8516_PIN_38_MRG_DI__FUNC_EXT_MDIO>,
+				 <MT8516_PIN_39_MRG_DO__FUNC_EXT_MDC>;
+		};
+	};
 };
Signature · 1 line
2.25.0
#4 lore raw

PATCH v2 2/14 dt-bindings: net: add a binding document for MediaTek Ethernet MAC

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

This adds yaml DT bindings for the MediaTek Ethernet MAC present on the mt8* family of SoCs.

---
 .../bindings/net/mediatek,eth-mac.yaml        | 80 +++++++++++++++++++
 1 file changed, 80 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/net/mediatek,eth-mac.yaml
Documentation/devicetree/bindings/net/mediatek,eth-mac.yaml new file +80 -0
new file mode 100644
index 000000000000..7682fe9d8109
@@ -0,0 +1,80 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/net/mediatek,eth-mac.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: MediaTek Ethernet MAC Controller
+
+maintainers:
+  - Bartosz Golaszewski <[email protected]>
+
+description:
+  This Ethernet MAC is used on the MT8* family of SoCs from MediaTek.
+  It's compliant with 802.3 standards and supports half- and full-duplex
+  modes with flow-control as well as CRC offloading and VLAN tags.
+
+properties:
+  compatible:
+    enum:
+      - mediatek,mt8516-eth
+      - mediatek,mt8518-eth
+      - mediatek,mt8175-eth
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  clocks:
+    minItems: 3
+    maxItems: 3
+
+  clock-names:
+    additionalItems: false
+    items:
+      - const: core
+      - const: reg
+      - const: trans
+
+  mediatek,pericfg:
+    $ref: /schemas/types.yaml#definitions/phandle
+    description:
+      Phandle to the device containing the PERICFG register range.
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - clocks
+  - clock-names
+  - mediatek,pericfg
+  - phy-handle
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/arm-gic.h>
+    #include <dt-bindings/clock/mt8516-clk.h>
+
+    ethernet: ethernet@11180000 {
+        compatible = "mediatek,mt8516-eth";
+        reg = <0 0x11180000 0 0x1000>;
+        mediatek,pericfg = <&pericfg>;
+        interrupts = <GIC_SPI 111 IRQ_TYPE_LEVEL_LOW>;
+        clocks = <&topckgen CLK_TOP_RG_ETH>,
+                 <&topckgen CLK_TOP_66M_ETH>,
+                 <&topckgen CLK_TOP_133M_ETH>;
+        clock-names = "core", "reg", "trans";
+        phy-handle = <&eth_phy>;
+        phy-mode = "rmii";
+
+        mdio {
+            #address-cells = <1>;
+            #size-cells = <0>;
+
+            eth_phy: ethernet-phy@0 {
+                reg = <0>;
+            };
+        };
+    };
Signature · 7 lines
2.25.0


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
#13 lore raw

PATCH v2 7/14 net: devres: define a separate devres structure for devm_alloc_etherdev()

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Not using a proxy structure to store struct net_device doesn't save anything in terms of compiled code size or memory usage but significantly decreases the readability of the code with all the pointer casting.

Define struct net_device_devres and use it in devm_alloc_etherdev_mqs().

---
 net/devres.c | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)
net/devres.c +12 -8
index c1465d9f9019..b97b0c5a8216 100644
@@ -7,30 +7,34 @@
 #include <linux/etherdevice.h>
 #include <linux/netdevice.h>
 
-static void devm_free_netdev(struct device *dev, void *res)
+struct net_device_devres {
+	struct net_device *ndev;
+};
+
+static void devm_free_netdev(struct device *dev, void *this)
 {
-	free_netdev(*(struct net_device **)res);
+	struct net_device_devres *res = this;
+
+	free_netdev(res->ndev);
 }
 
 struct net_device *devm_alloc_etherdev_mqs(struct device *dev, int sizeof_priv,
 					   unsigned int txqs, unsigned int rxqs)
 {
-	struct net_device **dr;
-	struct net_device *netdev;
+	struct net_device_devres *dr;
 
 	dr = devres_alloc(devm_free_netdev, sizeof(*dr), GFP_KERNEL);
 	if (!dr)
 		return NULL;
 
-	netdev = alloc_etherdev_mqs(sizeof_priv, txqs, rxqs);
-	if (!netdev) {
+	dr->ndev = alloc_etherdev_mqs(sizeof_priv, txqs, rxqs);
+	if (!dr->ndev) {
 		devres_free(dr);
 		return NULL;
 	}
 
-	*dr = netdev;
 	devres_add(dev, dr);
 
-	return netdev;
+	return dr->ndev;
 }
 EXPORT_SYMBOL(devm_alloc_etherdev_mqs);
Signature · 1 line
2.25.0
#8 lore raw

Re: [PATCH v2 05/14] net: core: provide priv_to_netdev()

In reply to Bartosz Golaszewski
Quoted 11 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Appropriate amount of extra memory for private data is allocated at the end of struct net_device. We have a helper - netdev_priv() - that returns its address but we don't have the reverse: a function which given the address of the private data, returns the address of struct net_device.

This has caused many drivers to store the pointer to net_device in the private data structure, which basically means storing the pointer to a structure in this very structure.

To some extent, that is the way it is done now. To do anything else just makes your driver different and so harder to maintain. Is 4/8 bytes for a pointer really worth being different?

Andrew

#16 lore raw

Re: [PATCH v2 09/14] net: ethernet: mtk-eth-mac: new driver

In reply to Bartosz Golaszewski
Quoted 7 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

This adds the driver for the MediaTek Ethernet MAC used on the MT8* SoC family. For now we only support full-duplex.

---

[snip]

Quoted 12 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +12 -0
+static int mtk_mac_ring_pop_tail(struct mtk_mac_ring *ring,
+				 struct mtk_mac_ring_desc_data *desc_data)
+{
+	struct mtk_mac_ring_desc *desc = &ring->descs[ring->tail];
+	unsigned int status;
+
+	/* Let the device release the descriptor. */
+	dma_rmb();
+	status = desc->status;
+
+	if (!(status & MTK_MAC_DESC_BIT_COWN))
+		return -1;

drivers/net/ethernet/mediatek/mtk_eth_mac.c line 313

Quoted 10 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +10 -0
+
+	desc_data->len = status & MTK_MAC_DESC_MSK_LEN;
+	desc_data->flags = status & ~MTK_MAC_DESC_MSK_LEN;
+	desc_data->dma_addr = desc->data_ptr;
+	desc_data->skb = ring->skbs[ring->tail];
+
+	desc->data_ptr = 0;
+	desc->status = MTK_MAC_DESC_BIT_COWN;
+	if (status & MTK_MAC_DESC_BIT_EOR)
+		desc->status |= MTK_MAC_DESC_BIT_EOR;

Don't you need a dma_wmb() for the device to observe the new descriptor here?

[snip]

drivers/net/ethernet/mediatek/mtk_eth_mac.c line 415

Quoted 7 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +7 -0
+static void mtk_mac_dma_unmap_tx(struct mtk_mac_priv *priv,
+				 struct mtk_mac_ring_desc_data *desc_data)
+{
+	struct device *dev = mtk_mac_get_dev(priv);
+
+	return dma_unmap_single(dev, desc_data->dma_addr,
+				desc_data->len, DMA_TO_DEVICE);

If you stored a pointer to the sk_buff you transmitted, then you would need an expensive read to the descriptor to determine the address and length, and you would also not be at the mercy of the HW putting incorrect values there.

sp

Quoted 15 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +15 -0
+static void mtk_mac_dma_init(struct mtk_mac_priv *priv)
+{
+	struct mtk_mac_ring_desc *desc;
+	unsigned int val;
+	int i;
+
+	priv->descs_base = (struct mtk_mac_ring_desc *)priv->ring_base;
+
+	for (i = 0; i < MTK_MAC_NUM_DESCS_TOTAL; i++) {
+		desc = &priv->descs_base[i];
+
+		memset(desc, 0, sizeof(*desc));
+		desc->status = MTK_MAC_DESC_BIT_COWN;
+		if ((i == MTK_MAC_NUM_TX_DESCS - 1) ||
+		    (i == MTK_MAC_NUM_DESCS_TOTAL - 1))

drivers/net/ethernet/mediatek/mtk_eth_mac.c line 468

Quoted 10 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +10 -0
+			desc->status |= MTK_MAC_DESC_BIT_EOR;
+	}
+
+	mtk_mac_ring_init(&priv->tx_ring, priv->descs_base, 0);
+	mtk_mac_ring_init(&priv->rx_ring,
+			  priv->descs_base + MTK_MAC_NUM_TX_DESCS,
+			  MTK_MAC_NUM_RX_DESCS);
+
+	/* Set DMA pointers. */
+	val = (unsigned int)priv->dma_addr;

You would probably add a WARN_ON() or something that catches the upper 32-bits of the dma_addr being set, see my comment about the DMA mask setting.

[snip]

Quoted 15 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +15 -0
+static void mtk_mac_tx_complete_all(struct mtk_mac_priv *priv)
+{
+	struct net_device *ndev = priv_to_netdev(priv);
+	struct mtk_mac_ring *ring = &priv->tx_ring;
+	int ret;
+
+	for (;;) {
+		mtk_mac_lock(priv);
+
+		if (!mtk_mac_ring_descs_available(ring)) {
+			mtk_mac_unlock(priv);
+			break;
+		}
+
+		ret = mtk_mac_tx_complete_one(priv);

drivers/net/ethernet/mediatek/mtk_eth_mac.c line 1094

Quoted 10 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +10 -0
+		if (ret) {
+			mtk_mac_unlock(priv);
+			break;
+		}
+
+		if (netif_queue_stopped(ndev))
+			netif_wake_queue(ndev);
+
+		mtk_mac_unlock(priv);
+	}

Where do you increment the net_device statistics to indicate the bytes and packets transmitted?

[snip]

drivers/net/ethernet/mediatek/mtk_eth_mac.c line 1512

Quoted 3 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +3 -0
+	mtk_mac_set_mode_rmii(priv);
+
+	dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));

Your code assumes that DMA addresses are not going to be >= 4GB so you should be checking this function's return code and abort here otherwise your driver will fail in surprisingly difficult ways to debug.

Signature · 1 line
Florian
maintainer
#9 lore raw

Re: [PATCH v2 05/14] net: core: provide priv_to_netdev()

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>
Date: Mon, 11 May 2020 17:07:50 +0200

Quoted 12 lines

From: Bartosz Golaszewski <[email protected]>

Appropriate amount of extra memory for private data is allocated at the end of struct net_device. We have a helper - netdev_priv() - that returns its address but we don't have the reverse: a function which given the address of the private data, returns the address of struct net_device.

This has caused many drivers to store the pointer to net_device in the private data structure, which basically means storing the pointer to a structure in this very structure.

Quoted 5 lines

This patch proposes to add priv_to_netdev() - a helper which converts the address of the private data to the address of the associated net_device.

Sorry, please don't do this. We had this almost two decades ago and explicitly removed it intentionally.

Store the back pointer in your software state just like everyone else does.

#10 lore raw

Re: [PATCH v2 05/14] net: core: provide priv_to_netdev()

pon., 11 maj 2020 o 22:41 David Miller <[email protected]> napisał(a):

Quoted 20 lines

From: Bartosz Golaszewski <[email protected]>
Date: Mon, 11 May 2020 17:07:50 +0200

Quoted 16 lines

From: Bartosz Golaszewski <[email protected]>

Appropriate amount of extra memory for private data is allocated at the end of struct net_device. We have a helper - netdev_priv() - that returns its address but we don't have the reverse: a function which given the address of the private data, returns the address of struct net_device.

This has caused many drivers to store the pointer to net_device in the private data structure, which basically means storing the pointer to a structure in this very structure.

This patch proposes to add priv_to_netdev() - a helper which converts the address of the private data to the address of the associated net_device.

Quoted 7 lines
Quoted 1 line

Sorry, please don't do this. We had this almost two decades ago and explicitly removed it intentionally.

Store the back pointer in your software state just like everyone else does.

I will if you insist but would you mind sharing some details on why it was removed? To me it still makes more sense than storing the pointer to a structure in *that* structure.

Bart

#17 lore raw

Re: [PATCH v2 09/14] net: ethernet: mtk-eth-mac: new driver

In reply to Florian Fainelli

pon., 11 maj 2020 o 21:24 Florian Fainelli <[email protected]> napisał(a):

Quoted 31 lines · Bartosz Golaszewski
Quoted 7 lines

From: Bartosz Golaszewski <[email protected]>

This adds the driver for the MediaTek Ethernet MAC used on the MT8* SoC family. For now we only support full-duplex.

---

[snip]

Quoted 17 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +17 -0
+static int mtk_mac_ring_pop_tail(struct mtk_mac_ring *ring,
+                              struct mtk_mac_ring_desc_data *desc_data)
+{
+     struct mtk_mac_ring_desc *desc = &ring->descs[ring->tail];
+     unsigned int status;
+
+     /* Let the device release the descriptor. */
+     dma_rmb();
+     status = desc->status;
+
+     if (!(status & MTK_MAC_DESC_BIT_COWN))
+             return -1;
+
+     desc_data->len = status & MTK_MAC_DESC_MSK_LEN;
+     desc_data->flags = status & ~MTK_MAC_DESC_MSK_LEN;
+     desc_data->dma_addr = desc->data_ptr;
+     desc_data->skb = ring->skbs[ring->tail];
Quoted 9 lines

drivers/net/ethernet/mediatek/mtk_eth_mac.c line 313

Quoted 5 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +5 -0
+
+     desc->data_ptr = 0;
+     desc->status = MTK_MAC_DESC_BIT_COWN;
+     if (status & MTK_MAC_DESC_BIT_EOR)
+             desc->status |= MTK_MAC_DESC_BIT_EOR;

Don't you need a dma_wmb() for the device to observe the new descriptor here?

HW has released the descriptor (set the COWN bit) and I just clear all other bits here really. Yeah, I guess it won't hurt to make sure.

Quoted 15 lines

[snip]

drivers/net/ethernet/mediatek/mtk_eth_mac.c line 415

Quoted 7 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +7 -0
+static void mtk_mac_dma_unmap_tx(struct mtk_mac_priv *priv,
+                              struct mtk_mac_ring_desc_data *desc_data)
+{
+     struct device *dev = mtk_mac_get_dev(priv);
+
+     return dma_unmap_single(dev, desc_data->dma_addr,
+                             desc_data->len, DMA_TO_DEVICE);

If you stored a pointer to the sk_buff you transmitted, then you would need an expensive read to the descriptor to determine the address and length, and you would also not be at the mercy of the HW putting incorrect values there.

You mean store the mapped addresses? Yeah I can do that but I'll still need to read the descriptor memory to verify it was released by HW.

Quoted 22 lines

sp

Quoted 21 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +21 -0
+static void mtk_mac_dma_init(struct mtk_mac_priv *priv)
+{
+     struct mtk_mac_ring_desc *desc;
+     unsigned int val;
+     int i;
+
+     priv->descs_base = (struct mtk_mac_ring_desc *)priv->ring_base;
+
+     for (i = 0; i < MTK_MAC_NUM_DESCS_TOTAL; i++) {
+             desc = &priv->descs_base[i];
+
+             memset(desc, 0, sizeof(*desc));
+             desc->status = MTK_MAC_DESC_BIT_COWN;
+             if ((i == MTK_MAC_NUM_TX_DESCS - 1) ||
+                 (i == MTK_MAC_NUM_DESCS_TOTAL - 1))
+                     desc->status |= MTK_MAC_DESC_BIT_EOR;
+     }
+
+     mtk_mac_ring_init(&priv->tx_ring, priv->descs_base, 0);
+     mtk_mac_ring_init(&priv->rx_ring,
+                       priv->descs_base + MTK_MAC_NUM_TX_DESCS,
Quoted 9 lines

drivers/net/ethernet/mediatek/mtk_eth_mac.c line 468

Quoted 4 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +4 -0
+                       MTK_MAC_NUM_RX_DESCS);
+
+     /* Set DMA pointers. */
+     val = (unsigned int)priv->dma_addr;

You would probably add a WARN_ON() or something that catches the upper 32-bits of the dma_addr being set, see my comment about the DMA mask setting.

Can it still happen if I check the return value of dma_set_mask_and_coherent()?

Quoted 22 lines

[snip]

Quoted 20 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +20 -0
+static void mtk_mac_tx_complete_all(struct mtk_mac_priv *priv)
+{
+     struct net_device *ndev = priv_to_netdev(priv);
+     struct mtk_mac_ring *ring = &priv->tx_ring;
+     int ret;
+
+     for (;;) {
+             mtk_mac_lock(priv);
+
+             if (!mtk_mac_ring_descs_available(ring)) {
+                     mtk_mac_unlock(priv);
+                     break;
+             }
+
+             ret = mtk_mac_tx_complete_one(priv);
+             if (ret) {
+                     mtk_mac_unlock(priv);
+                     break;
+             }
+
Quoted 9 lines

drivers/net/ethernet/mediatek/mtk_eth_mac.c line 1094

Quoted 5 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +5 -0
+             if (netif_queue_stopped(ndev))
+                     netif_wake_queue(ndev);
+
+             mtk_mac_unlock(priv);
+     }

Where do you increment the net_device statistics to indicate the bytes and packets transmitted?

I don't. I use the counters provided by HW for that.

Quoted 9 lines

[snip]

drivers/net/ethernet/mediatek/mtk_eth_mac.c line 1512

Quoted 3 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +3 -0
+     mtk_mac_set_mode_rmii(priv);
+
+     dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));

Your code assumes that DMA addresses are not going to be >= 4GB so you should be checking this function's return code and abort here otherwise your driver will fail in surprisingly difficult ways to debug.

Sure, thanks.

Bart

maintainer
#11 lore raw

Re: [PATCH v2 05/14] net: core: provide priv_to_netdev()

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>
Date: Tue, 12 May 2020 08:04:39 +0200

Quoted 3 lines

I will if you insist but would you mind sharing some details on why it was removed? To me it still makes more sense than storing the pointer to a structure in *that* structure.

Flexibility in implementation of where the private data is located and how it is allocated.

And yes, I do insist.

maintainer
#2 lore raw

Re: [PATCH v2 01/14] dt-bindings: arm: add a binding document for MediaTek PERICFG controller

In reply to Bartosz Golaszewski
Quoted 21 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

This adds a binding document for the PERICFG controller present on MediaTek SoCs. For now the only variant supported is 'mt8516-pericfg'.

---
 .../arm/mediatek/mediatek,pericfg.yaml        | 34 +++++++++++++++++++
 1 file changed, 34 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/arm/mediatek/mediatek,pericfg.yaml
Documentation/devicetree/bindings/arm/mediatek/mediatek,pericfg.yaml new file +4 -0
new file mode 100644
index 000000000000..74b2a6173ffb
@@ -0,0 +1,34 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: "http://devicetree.org/schemas/arm/mediatek/mediatek,pericfg.yaml#"

Documentation/devicetree/bindings/arm/mediatek/mediatek,pericfg.yaml line 14

Quoted 10 lines
Documentation/devicetree/bindings/arm/mediatek/mediatek,pericfg.yaml new file +10 -0
+$schema: "http://devicetree.org/meta-schemas/core.yaml#"
+
+title: MediaTek Peripheral Configuration Controller
+
+maintainers:
+  - Bartosz Golaszewski <[email protected]>
+
+properties:
+  compatible:
+    oneOf:

Don't need oneOf here.

Documentation/devicetree/bindings/arm/mediatek/mediatek,pericfg.yaml line 17

Quoted 3 lines
Documentation/devicetree/bindings/arm/mediatek/mediatek,pericfg.yaml new file +3 -0
+      - items:
+        - enum:
+          - mediatek,pericfg

Doesn't match the example (which is correct).

Quoted 20 lines
Documentation/devicetree/bindings/arm/mediatek/mediatek,pericfg.yaml new file +17 -0
+        - const: syscon
+
+  reg:
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+
+additionalProperties: false
+
+examples:
+  - |
+    pericfg: pericfg@10003050 {
+        compatible = "mediatek,mt8516-pericfg", "syscon";
+        reg = <0 0x10003050 0 0x1000>;
+    };
Signature · 1 line
2.25.0
#3 lore raw

Re: [PATCH v2 01/14] dt-bindings: arm: add a binding document for MediaTek PERICFG controller

wt., 19 maj 2020 o 20:28 Rob Herring <[email protected]> napisał(a):

Quoted 36 lines · Bartosz Golaszewski
Quoted 21 lines

From: Bartosz Golaszewski <[email protected]>

This adds a binding document for the PERICFG controller present on MediaTek SoCs. For now the only variant supported is 'mt8516-pericfg'.

---
 .../arm/mediatek/mediatek,pericfg.yaml        | 34 +++++++++++++++++++
 1 file changed, 34 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/arm/mediatek/mediatek,pericfg.yaml
Documentation/devicetree/bindings/arm/mediatek/mediatek,pericfg.yaml new file +4 -0
new file mode 100644
index 000000000000..74b2a6173ffb
@@ -0,0 +1,34 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: "http://devicetree.org/schemas/arm/mediatek/mediatek,pericfg.yaml#"

Documentation/devicetree/bindings/arm/mediatek/mediatek,pericfg.yaml line 14

Quoted 10 lines
Documentation/devicetree/bindings/arm/mediatek/mediatek,pericfg.yaml new file +10 -0
+$schema: "http://devicetree.org/meta-schemas/core.yaml#"
+
+title: MediaTek Peripheral Configuration Controller
+
+maintainers:
+  - Bartosz Golaszewski <[email protected]>
+
+properties:
+  compatible:
+    oneOf:

Don't need oneOf here.

Quoted 6 lines

Documentation/devicetree/bindings/arm/mediatek/mediatek,pericfg.yaml line 17

Quoted 3 lines
Documentation/devicetree/bindings/arm/mediatek/mediatek,pericfg.yaml new file +3 -0
+      - items:
+        - enum:
+          - mediatek,pericfg

Doesn't match the example (which is correct).

Hi Rob,

FYI this was superseded by v3 which should now be correct.

Bart