mediatek: add support for MediaTek Ethernet MAC
Subsystems (7)
v3 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.
First we convert the existing DT bindings for the PERICFG controller to YAML and add a new compatible string for mt8516 variant of it. Then we add the DT bindings for the MAC.
Next we do some cleanup of the mediatek ethernet drivers directory and update the devres documentation with existing networking devres helpers.
The following patches introduce a resource managed variant of register_netdev() and move all networking devres helpers into a separate .c file.
The largest patch in the series adds the actual new 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
v2 -> v3:
- drop the patch adding priv_to_netdev() and store the netdev pointer in the driver private data
- add an additional dma_wmb() after reseting the descriptor in mtk_mac_ring_pop_tail()
- check the return value of dma_set_mask_and_coherent()
- improve the DT bindings for mtk-eth-mac: make the reg property in the example use single-cell address and size, extend the description of the PERICFG phandle and document the mdio sub-node
- add a patch converting the old .txt bindings for PERICFG to yaml
- limit reading the DMA memory by storing the mapped addresses in the driver private structure
- add a patch documenting the existing networking devres helpers
Bartosz Golaszewski (15):
dt-bindings: convert the binding document for mediatek PERICFG to yaml
dt-bindings: add new compatible to mediatek,pericfg
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
Documentation: devres: add a missing section for networking helpers
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.txt | 36 - .../arm/mediatek/mediatek,pericfg.yaml | 64 + .../bindings/net/mediatek,eth-mac.yaml | 89 + .../driver-api/driver-model/devres.rst | 5 + 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 | 1578 +++++++++++++++++ include/linux/netdevice.h | 2 + net/Makefile | 2 +- net/devres.c | 95 + net/ethernet/eth.c | 28 - 13 files changed, 1894 insertions(+), 67 deletions(-) delete mode 100644 Documentation/devicetree/bindings/arm/mediatek/mediatek,pericfg.txt 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
PATCH v3 1/15 dt-bindings: convert the binding document for mediatek PERICFG to yaml
From: Bartosz Golaszewski <[email protected]>
Convert the DT binding .txt file for MediaTek's peripheral configuration controller to YAML. There's one special case where the compatible has three positions. Otherwise, it's a pretty normal syscon.
--- .../arm/mediatek/mediatek,pericfg.txt | 36 ----------- .../arm/mediatek/mediatek,pericfg.yaml | 63 +++++++++++++++++++ 2 files changed, 63 insertions(+), 36 deletions(-) delete mode 100644 Documentation/devicetree/bindings/arm/mediatek/mediatek,pericfg.txt create mode 100644 Documentation/devicetree/bindings/arm/mediatek/mediatek,pericfg.yaml
Documentation/devicetree/bindings/arm/mediatek/mediatek,pericfg.txt deleted +0 -36
@@ -1,36 +0,0 @@ -Mediatek pericfg controller -=========================== - -The Mediatek pericfg controller provides various clocks and reset -outputs to the system. - -Required Properties: - -- compatible: Should be one of: - - "mediatek,mt2701-pericfg", "syscon" - - "mediatek,mt2712-pericfg", "syscon" - - "mediatek,mt7622-pericfg", "syscon" - - "mediatek,mt7623-pericfg", "mediatek,mt2701-pericfg", "syscon" - - "mediatek,mt7629-pericfg", "syscon" - - "mediatek,mt8135-pericfg", "syscon" - - "mediatek,mt8173-pericfg", "syscon" - - "mediatek,mt8183-pericfg", "syscon" -- #clock-cells: Must be 1 -- #reset-cells: Must be 1 - -The pericfg controller uses the common clk binding from -Documentation/devicetree/bindings/clock/clock-bindings.txt -The available clocks are defined in dt-bindings/clock/mt*-clk.h. -Also it uses the common reset controller binding from -Documentation/devicetree/bindings/reset/reset.txt. -The available reset outputs are defined in -dt-bindings/reset/mt*-resets.h - -Example: - -pericfg: power-controller@10003000 { - compatible = "mediatek,mt8173-pericfg", "syscon"; - reg = <0 0x10003000 0 0x1000>; - #clock-cells = <1>; - #reset-cells = <1>; -};
Documentation/devicetree/bindings/arm/mediatek/mediatek,pericfg.yaml new file +63 -0
63 lines not on this page · raw message
Signature · 1 line
2.25.0
PATCH v3 2/15 dt-bindings: add new compatible to mediatek,pericfg
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
The PERICFG controller is present on the MT8516 SoC. Add an appropriate compatible variant.
--- .../devicetree/bindings/arm/mediatek/mediatek,pericfg.yaml | 1 + 1 file changed, 1 insertion(+)
Documentation/devicetree/bindings/arm/mediatek/mediatek,pericfg.yaml +1 -0
@@ -25,6 +25,7 @@ properties: - mediatek,mt8135-pericfg - mediatek,mt8173-pericfg - mediatek,mt8183-pericfg + - mediatek,mt8516-pericfg - const: syscon - items: # Special case for mt7623 for backward compatibility
Signature · 7 lines
2.25.0 _______________________________________________ Linux-mediatek mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-mediatek
PATCH v3 3/15 dt-bindings: net: add a binding document for MediaTek Ethernet MAC
In reply to Bartosz GolaszewskiFrom: 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 | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 Documentation/devicetree/bindings/net/mediatek,eth-mac.yaml
Documentation/devicetree/bindings/net/mediatek,eth-mac.yaml new file +89 -0
@@ -0,0 +1,89 @@ +# 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. + +allOf: + - $ref: "ethernet-controller.yaml#" + +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. This is used + to control the MII mode. + + mdio: + type: object + description: + Creates and registers an MDIO bus. + +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 = <0x11180000 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 = <ð_phy>; + phy-mode = "rmii"; + + mdio { + #address-cells = <1>; + #size-cells = <0>; + + eth_phy: ethernet-phy@0 { + reg = <0>; + }; + }; + };
Signature · 1 line
2.25.0
PATCH v3 4/15 net: ethernet: mediatek: rename Kconfig prompt
In reply to Bartosz GolaszewskiFrom: 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
@@ -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 · 7 lines
2.25.0 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
PATCH v3 5/15 net: ethernet: mediatek: remove unnecessary spaces from Makefile
In reply to Bartosz GolaszewskiFrom: 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
@@ -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
PATCH v3 6/15 Documentation: devres: add a missing section for networking helpers
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
Add a new section for networking devres helpers to devres.rst and list the two existing devm functions.
--- Documentation/driver-api/driver-model/devres.rst | 4 ++++ 1 file changed, 4 insertions(+)
Documentation/driver-api/driver-model/devres.rst +4 -0
@@ -372,6 +372,10 @@ MUX devm_mux_chip_register() devm_mux_control_get() +NET + devm_alloc_etherdev() + devm_alloc_etherdev_mqs() + PER-CPU MEM devm_alloc_percpu() devm_free_percpu()
Signature · 1 line
2.25.0
PATCH v3 7/15 net: move devres helpers into a separate source file
In reply to Bartosz GolaszewskiFrom: 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
@@ -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
@@ -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
@@ -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
PATCH v3 8/15 net: devres: define a separate devres structure for devm_alloc_etherdev()
In reply to Bartosz GolaszewskiFrom: 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
@@ -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
PATCH v3 9/15 net: devres: provide devm_register_netdev()
In reply to Bartosz GolaszewskiFrom: 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.
--- .../driver-api/driver-model/devres.rst | 1 + include/linux/netdevice.h | 2 + net/devres.c | 55 +++++++++++++++++++ 3 files changed, 58 insertions(+)
Documentation/driver-api/driver-model/devres.rst +1 -0
@@ -375,6 +375,7 @@ MUX NET devm_alloc_etherdev() devm_alloc_etherdev_mqs() + devm_register_netdev() PER-CPU MEM devm_alloc_percpu()
include/linux/netdevice.h +2 -0
@@ -4208,6 +4208,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
PATCH v3 10/15 net: ethernet: mtk-eth-mac: new driver
In reply to Bartosz GolaszewskiFrom: 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 | 1578 +++++++++++++++++++ 3 files changed, 1585 insertions(+) create mode 100644 drivers/net/ethernet/mediatek/mtk_eth_mac.c
drivers/net/ethernet/mediatek/Kconfig +6 -0
@@ -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
@@ -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 +1578 -0
1,578 lines not on this page · raw message
Signature · 1 line
2.25.0
Re: [PATCH v3 10/15] net: ethernet: mtk-eth-mac: new driver
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.
Signed-off-by Bartosz Golaszewski <[email protected]>
Looks very nice overall. Just a few things I noticed, and some ideas that may or may not make sense:
drivers/net/ethernet/mediatek/mtk_eth_mac.c line 42
Quoted 7 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +7
-0+/* This is defined to 0 on arm64 in arch/arm64/include/asm/processor.h but +* this IP doesn't work without this alignment being equal to 2. +*/ +#ifdef NET_IP_ALIGN +#undef NET_IP_ALIGN +#endif +#define NET_IP_ALIGN 2
Maybe you should just define your own macro instead of replacing the normal one then?
drivers/net/ethernet/mediatek/mtk_eth_mac.c line 387
Quoted 9 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +9
-0+static void mtk_mac_lock (struct mtk_mac_priv *priv) +{ +spin_lock_irqsave (&priv->lock, priv->lock_flags); +} + +static void mtk_mac_unlock (struct mtk_mac_priv *priv) +{ +spin_unlock_irqrestore (&priv->lock, priv->lock_flags); +}
This looks wrong: you should not have shared 'flags' passed into spin_lock_irqsave(), and I don't even see a need to use the irqsave variant of the lock in the first place.
Maybe start by open-coding the lock and remove the wrappers above.
Then see if you can use a cheaper spin_lock_bh() or plain spin_lock() instead of irqsave.
Finally, see if this can be done in a lockless way by relying on appropriate barriers and separating the writers into separate cache lines. From a brief look at the driver I think it can be done without too much trouble.
drivers/net/ethernet/mediatek/mtk_eth_mac.c line 449
Quoted 9 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +9
-0+static unsigned int mtk_mac_intr_read_and_clear (struct mtk_mac_priv *priv) +{ +unsigned int val; + +regmap_read (priv->regs,MTK_MAC_REG_INT_STS , &val); +regmap_write (priv->regs,MTK_MAC_REG_INT_STS , val); + +return val; +}
Do you actually need to read the register? That is usually a relatively expensive operation, so if possible try to use clear the bits when you don't care which bits were set.
Quoted 38 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +36
-0+/* All processing for TX and RX happens in the napi poll callback. */ +static irqreturn_t mtk_mac_handle_irq (int irq,void *data) +{ +struct mtk_mac_priv *priv; +struct net_device *ndev; + + ndev = data; + priv =netdev_priv (ndev); + +if (netif_running (ndev)) { +mtk_mac_intr_mask_all (priv); +napi_schedule (&priv->napi); + } + +return IRQ_HANDLED ; +static int mtk_mac_netdev_start_xmit (struct sk_buff *skb, +struct net_device *ndev) +{ +struct mtk_mac_priv *priv =netdev_priv (ndev); +struct mtk_mac_ring *ring = &priv->tx_ring; +struct device *dev =mtk_mac_get_dev (priv); +struct mtk_mac_ring_desc_data desc_data; + + desc_data.dma_addr =mtk_mac_dma_map_tx (priv, skb); +if (dma_mapping_error (dev, desc_data.dma_addr)) + goto err_drop_packet; + + desc_data.skb = skb; + desc_data.len = skb->len; + +mtk_mac_lock (priv); +mtk_mac_ring_push_head_tx (ring, &desc_data); + +if (mtk_mac_ring_full (ring)) +netif_stop_queue (ndev); +mtk_mac_unlock (priv);
drivers/net/ethernet/mediatek/mtk_eth_mac.c line 780
Quoted 10 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +10
-0+ +mtk_mac_dma_resume_tx (priv); + +return NETDEV_TX_OK ; + +err_drop_packet: +dev_kfree_skb (skb); + ndev->stats.tx_dropped++; +return NETDEV_TX_BUSY ; +}
I would always add BQL flow control in new drivers, using netdev_sent_queue here...
Quoted 5 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +5
-0+static int mtk_mac_tx_complete_one (struct mtk_mac_priv *priv) +{ +struct mtk_mac_ring *ring = &priv->tx_ring; +struct mtk_mac_ring_desc_data desc_data; +int ret;
drivers/net/ethernet/mediatek/mtk_eth_mac.c line 1077
Quoted 10 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +10
-0+ + ret =mtk_mac_ring_pop_tail (ring, &desc_data); +if (ret) +return ret; + +mtk_mac_dma_unmap_tx (priv, &desc_data); +dev_kfree_skb_irq (desc_data.skb); + +return 0 ; +}
... and netdev_completed_queue() here.
Quoted 16 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +16
-0+static void mtk_mac_tx_complete_all (struct mtk_mac_priv *priv) +{ +struct mtk_mac_ring *ring = &priv->tx_ring; +struct net_device *ndev = priv->ndev; +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) {
drivers/net/ethernet/mediatek/mtk_eth_mac.c line 1104
Quoted 10 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +10
-0+mtk_mac_unlock (priv); +break ; + } + +if (netif_queue_stopped (ndev)) +netif_wake_queue (ndev); + +mtk_mac_unlock (priv); + } +}
It looks like most of the stuff inside of the loop can be pulled out and only done once here.
Quoted 23 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +23
-0+static int mtk_mac_poll (struct napi_struct *napi,int budget) +{ +struct mtk_mac_priv *priv; +unsigned int status; +int received =0 ; + + priv = container_of(napi, struct mtk_mac_priv, napi); + + status =mtk_mac_intr_read_and_clear (priv); + +/* Clean up TX */ +if (status &MTK_MAC_BIT_INT_STS_TNTC ) +mtk_mac_tx_complete_all (priv); + +/* Receive up to $budget packets */ +if (status &MTK_MAC_BIT_INT_STS_FNRC ) + received =mtk_mac_process_rx (priv, budget); + +/* One of the counter reached 0x8000000 - update stats and reset all +* counters. +*/ +if (status &MTK_MAC_REG_INT_STS_MIB_CNT_TH ) { +mtk_mac_update_stats (priv);
drivers/net/ethernet/mediatek/mtk_eth_mac.c line 1293
Quoted 10 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +10
-0+mtk_mac_reset_counters (priv); + } + +if (received < budget) +napi_complete_done (napi, received); + +mtk_mac_intr_unmask_all (priv); + +return received; +}
I think you want to leave (at least some of) the interrupts masked if your budget is exhausted, to avoid generating unnecessary irqs.
It may also be faster to not mask/unmask at all but just clear the interrupts that you have finished processing
Arnd
Re: [PATCH v3 10/15] net: ethernet: mtk-eth-mac: new driver
czw., 14 maj 2020 o 18:19 Arnd Bergmann <[email protected]> napisał(a):
Quoted 13 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.
Signed-off-by Bartosz Golaszewski <[email protected]>Looks very nice overall. Just a few things I noticed, and some ideas that may or may not make sense:
Quoted 11 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c line 42
Quoted 7 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +7
-0+/* This is defined to 0 on arm64 in arch/arm64/include/asm/processor.h but +* this IP doesn't work without this alignment being equal to 2. +*/ +#ifdef NET_IP_ALIGN +#undef NET_IP_ALIGN +#endif +#define NET_IP_ALIGN 2Maybe you should just define your own macro instead of replacing the normal one then?
I did in an earlier version and was told to use NET_IP_ALIGN but then found out its value on arm64 doesn't work for me so I did the thing that won't make anybody happy - redefine the existing constant. :)
Quoted 14 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c line 387
Quoted 9 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +9
-0+static void mtk_mac_lock (struct mtk_mac_priv *priv) +{ +spin_lock_irqsave (&priv->lock, priv->lock_flags); +} + +static void mtk_mac_unlock (struct mtk_mac_priv *priv) +{ +spin_unlock_irqrestore (&priv->lock, priv->lock_flags); +}This looks wrong: you should not have shared 'flags' passed into
spin_lock_irqsave(), and I don't even see a need to use the irqsave variant of the lock in the first place.
Quoted 6 lines
Maybe start by open-coding the lock and remove the wrappers above.
Then see if you can use a cheaper
spin_lock_bh()or plainspin_lock()instead of irqsave.
This is from an earlier version where I did a lot more in hard irq context. Now that almost all of the processing happens in soft-irq context I guess you're right - I can go with a regular spin_lock().
Quoted 5 lines
Finally, see if this can be done in a lockless way by relying on appropriate barriers and separating the writers into separate cache lines. From a brief look at the driver I think it can be done without too much trouble.
Unfortunately I do need some locking. Accessing RX and TX descriptors at the same time seems to upset the controller. I experimented a lot with barriers but it turned out that I got a lot of weird bugs at high throughput.
Quoted 5 lines
Quoted 5 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +5
-0+static unsigned int mtk_mac_intr_read_and_clear (struct mtk_mac_priv *priv) +{ +unsigned int val; + +regmap_read (priv->regs,MTK_MAC_REG_INT_STS , &val);
Quoted 9 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c line 449
Quoted 4 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +4
-0+regmap_write (priv->regs,MTK_MAC_REG_INT_STS , val); + +return val; +}Do you actually need to read the register? That is usually a relatively expensive operation, so if possible try to use clear the bits when you don't care which bits were set.
I do care, I'm afraid. The returned value is being used in the napi poll callback to see which ring to process.
Quoted 43 lines
Quoted 43 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +41
-0+/* All processing for TX and RX happens in the napi poll callback. */ +static irqreturn_t mtk_mac_handle_irq (int irq,void *data) +{ +struct mtk_mac_priv *priv; +struct net_device *ndev; + + ndev = data; + priv =netdev_priv (ndev); + +if (netif_running (ndev)) { +mtk_mac_intr_mask_all (priv); +napi_schedule (&priv->napi); + } + +return IRQ_HANDLED ; +static int mtk_mac_netdev_start_xmit (struct sk_buff *skb, +struct net_device *ndev) +{ +struct mtk_mac_priv *priv =netdev_priv (ndev); +struct mtk_mac_ring *ring = &priv->tx_ring; +struct device *dev =mtk_mac_get_dev (priv); +struct mtk_mac_ring_desc_data desc_data; + + desc_data.dma_addr =mtk_mac_dma_map_tx (priv, skb); +if (dma_mapping_error (dev, desc_data.dma_addr)) + goto err_drop_packet; + + desc_data.skb = skb; + desc_data.len = skb->len; + +mtk_mac_lock (priv); +mtk_mac_ring_push_head_tx (ring, &desc_data); + +if (mtk_mac_ring_full (ring)) +netif_stop_queue (ndev); +mtk_mac_unlock (priv); + +mtk_mac_dma_resume_tx (priv); + +return NETDEV_TX_OK ; +
Quoted 9 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c line 1061
Quoted 5 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +5
-0+err_drop_packet: +dev_kfree_skb (skb); + ndev->stats.tx_dropped++; +return NETDEV_TX_BUSY ; +}I would always add BQL flow control in new drivers, using
netdev_sent_queuehere...
Ok, will do.
Quoted 9 lines
Quoted 9 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +9
-0+static int mtk_mac_tx_complete_one (struct mtk_mac_priv *priv) +{ +struct mtk_mac_ring *ring = &priv->tx_ring; +struct mtk_mac_ring_desc_data desc_data; +int ret; + + ret =mtk_mac_ring_pop_tail (ring, &desc_data); +if (ret) +return ret;
Quoted 9 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c line 1077
Quoted 6 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +6
-0+ +mtk_mac_dma_unmap_tx (priv, &desc_data); +dev_kfree_skb_irq (desc_data.skb); + +return 0 ; +}... and
netdev_completed_queue()here.
Same here.
Quoted 21 lines
Quoted 21 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +21
-0+static void mtk_mac_tx_complete_all (struct mtk_mac_priv *priv) +{ +struct mtk_mac_ring *ring = &priv->tx_ring; +struct net_device *ndev = priv->ndev; +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 ; + } + +if (netif_queue_stopped (ndev))
Quoted 9 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c line 1104
Quoted 5 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +5
-0+netif_wake_queue (ndev); + +mtk_mac_unlock (priv); + } +}It looks like most of the stuff inside of the loop can be pulled out and only done once here.
I did that in one of the previous submissions but it was pointed out to me that a parallel TX path may fill up the queue before I wake it.
Quoted 29 lines
Quoted 29 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +29
-0+static int mtk_mac_poll (struct napi_struct *napi,int budget) +{ +struct mtk_mac_priv *priv; +unsigned int status; +int received =0 ; + + priv = container_of(napi, struct mtk_mac_priv, napi); + + status =mtk_mac_intr_read_and_clear (priv); + +/* Clean up TX */ +if (status &MTK_MAC_BIT_INT_STS_TNTC ) +mtk_mac_tx_complete_all (priv); + +/* Receive up to $budget packets */ +if (status &MTK_MAC_BIT_INT_STS_FNRC ) + received =mtk_mac_process_rx (priv, budget); + +/* One of the counter reached 0x8000000 - update stats and reset all +* counters. +*/ +if (status &MTK_MAC_REG_INT_STS_MIB_CNT_TH ) { +mtk_mac_update_stats (priv); +mtk_mac_reset_counters (priv); + } + +if (received < budget) +napi_complete_done (napi, received); +
Quoted 9 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c line 1293
Quoted 4 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +4
-0+mtk_mac_intr_unmask_all (priv); + +return received; +}I think you want to leave (at least some of) the interrupts masked if your budget is exhausted, to avoid generating unnecessary irqs.
The networking stack shouldn't queue any new TX packets if the queue is stopped - is this really worth complicating the code? Looks like premature optimization IMO.
Quoted 3 lines
It may also be faster to not mask/unmask at all but just clear the interrupts that you have finished processing
Bart
Re: [PATCH v3 10/15] net: ethernet: mtk-eth-mac: new driver
Quoted 19 lines · Bartosz Golaszewski
czw., 14 maj 2020 o 18:19 Arnd Bergmann <[email protected]> napisał(a):
Quoted 7 lines · Bartosz Golaszewski
Quoted 5 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +5
-0+static unsigned int mtk_mac_intr_read_and_clear (struct mtk_mac_priv *priv) +{ +unsigned int val; + +regmap_read (priv->regs,MTK_MAC_REG_INT_STS , &val);Quoted 9 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c line 449
Quoted 4 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +4
-0+regmap_write (priv->regs,MTK_MAC_REG_INT_STS , val); + +return val; +}Do you actually need to read the register? That is usually a relatively expensive operation, so if possible try to use clear the bits when you don't care which bits were set.
Quoted 2 lines
I do care, I'm afraid. The returned value is being used in the napi poll callback to see which ring to process.
I suppose the other callers are not performance critical.
For the rx and tx processing, it should be better to just always look at the queue directly and ignore the irq status, in particular when you are already in polling mode: suppose you receive ten frames at once and only process five but clear the irq flag.
When the poll function is called again, you still need to process the others, but I would assume that the status tells you that nothing new has arrived so you don't process them until the next interrupt.
For the statistics, I assume you do need to look at the irq status, but this doesn't have to be done in the poll function. How about something like:
- in hardirq context, read the irq status word
- irq rx or tx irq pending, call napi_schedule
- if stats irq pending, schedule a work function
- in napi poll, process both queues until empty or budget exhausted
- if packet processing completed in poll function ack the irq and check again, call napi_complete
- in work function, handle stats irq, then ack it
Quoted 31 lines
Quoted 21 lines
Quoted 21 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +21
-0+static void mtk_mac_tx_complete_all (struct mtk_mac_priv *priv) +{ +struct mtk_mac_ring *ring = &priv->tx_ring; +struct net_device *ndev = priv->ndev; +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 ; + } + +if (netif_queue_stopped (ndev))Quoted 9 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c line 1104
Quoted 5 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +5
-0+netif_wake_queue (ndev); + +mtk_mac_unlock (priv); + } +}It looks like most of the stuff inside of the loop can be pulled out and only done once here.
Quoted 2 lines
I did that in one of the previous submissions but it was pointed out to me that a parallel TX path may fill up the queue before I wake it.
Right, I see you plugged that hole, however the way you hold the spinlock across the expensive DMA management but then give it up in each loop iteration feels like this is not the most efficient way.
The easy way would be to just hold the lock across the entire loop and then be sure you do it right. Alternatively you could minimize the locking and only do the wakeup after up do the final update to the tail pointer, at which point you know the queue is not full because you have just freed up at least one entry.
Quoted 39 lines
Quoted 29 lines
Quoted 29 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +29
-0+static int mtk_mac_poll (struct napi_struct *napi,int budget) +{ +struct mtk_mac_priv *priv; +unsigned int status; +int received =0 ; + + priv = container_of(napi, struct mtk_mac_priv, napi); + + status =mtk_mac_intr_read_and_clear (priv); + +/* Clean up TX */ +if (status &MTK_MAC_BIT_INT_STS_TNTC ) +mtk_mac_tx_complete_all (priv); + +/* Receive up to $budget packets */ +if (status &MTK_MAC_BIT_INT_STS_FNRC ) + received =mtk_mac_process_rx (priv, budget); + +/* One of the counter reached 0x8000000 - update stats and reset all +* counters. +*/ +if (status &MTK_MAC_REG_INT_STS_MIB_CNT_TH ) { +mtk_mac_update_stats (priv); +mtk_mac_reset_counters (priv); + } + +if (received < budget) +napi_complete_done (napi, received); +Quoted 9 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c line 1293
Quoted 4 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +4
-0+mtk_mac_intr_unmask_all (priv); + +return received; +}I think you want to leave (at least some of) the interrupts masked if your budget is exhausted, to avoid generating unnecessary irqs.
Quoted 3 lines
The networking stack shouldn't queue any new TX packets if the queue is stopped - is this really worth complicating the code? Looks like premature optimization IMO.
Avoiding IRQs is one of the central aspects of using NAPI -- the idea is that either you know there is more work to do and you will be called again in the near future with additional budget, or you enable interrupts and the irq handler calls napi_schedule, but not both.
This is mostly about RX processing, which is limited by the budget, for TX you already free all descriptors regardless of the budget.
Arnd
Re: [PATCH v3 10/15] net: ethernet: mtk-eth-mac: new driver
pt., 15 maj 2020 o 14:04 Arnd Bergmann <[email protected]> napisał(a):
Quoted 39 lines · Bartosz Golaszewski
Quoted 19 lines
czw., 14 maj 2020 o 18:19 Arnd Bergmann <[email protected]> napisał(a):
Quoted 7 lines · Bartosz Golaszewski
Quoted 5 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +5
-0+static unsigned int mtk_mac_intr_read_and_clear (struct mtk_mac_priv *priv) +{ +unsigned int val; + +regmap_read (priv->regs,MTK_MAC_REG_INT_STS , &val);Quoted 9 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c line 449
Quoted 4 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +4
-0+regmap_write (priv->regs,MTK_MAC_REG_INT_STS , val); + +return val; +}Do you actually need to read the register? That is usually a relatively expensive operation, so if possible try to use clear the bits when you don't care which bits were set.
Quoted 2 lines
I do care, I'm afraid. The returned value is being used in the napi poll callback to see which ring to process.
I suppose the other callers are not performance critical.
For the rx and tx processing, it should be better to just always look at the queue directly and ignore the irq status, in particular when you are already in polling mode: suppose you receive ten frames at once and only process five but clear the irq flag.
When the poll function is called again, you still need to process the others, but I would assume that the status tells you that nothing new has arrived so you don't process them until the next interrupt.
For the statistics, I assume you do need to look at the irq status, but this doesn't have to be done in the poll function. How about something like:
Quoted 9 lines
- in hardirq context, read the irq status word
- irq rx or tx irq pending, callnapi_schedule
- if stats irq pending, schedule a work function
- in napi poll, process both queues until empty or budget exhausted
- if packet processing completed in poll function ack the irq and check again, callnapi_complete
- in work function, handle stats irq, then ack it
I see your point. I'll try to come up with something and send a new version on Monday.
Quoted 31 lines
Quoted 31 lines
Quoted 30 lines
Quoted 16 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +16
-0+static void mtk_mac_tx_complete_all (struct mtk_mac_priv *priv) +{ +struct mtk_mac_ring *ring = &priv->tx_ring; +struct net_device *ndev = priv->ndev; +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) {drivers/net/ethernet/mediatek/mtk_eth_mac.c line 1104
Quoted 10 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +10
-0+mtk_mac_unlock (priv); +break ; + } + +if (netif_queue_stopped (ndev)) +netif_wake_queue (ndev); + +mtk_mac_unlock (priv); + } +}It looks like most of the stuff inside of the loop can be pulled out and only done once here.
Quoted 8 lines
Quoted 2 lines
I did that in one of the previous submissions but it was pointed out to me that a parallel TX path may fill up the queue before I wake it.
Right, I see you plugged that hole, however the way you hold the spinlock across the expensive DMA management but then give it up in each loop iteration feels like this is not the most efficient way.
Maybe my thinking is wrong here, but I assumed that with a spinlock it's better to give other threads the chance to run in between each iteration. I didn't benchmark it though.
Quoted 6 lines
The easy way would be to just hold the lock across the entire loop and then be sure you do it right. Alternatively you could minimize the locking and only do the wakeup after up do the final update to the tail pointer, at which point you know the queue is not full because you have just freed up at least one entry.
Makes sense, I'll see what I can do.
Bartosz
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Re: [PATCH v3 10/15] net: ethernet: mtk-eth-mac: new driver
Quoted 17 lines · Bartosz Golaszewski
pt., 15 maj 2020 o 14:04 Arnd Bergmann <[email protected]> napisał(a):
Quoted 7 lines · Bartosz Golaszewski
Quoted 6 lines
Quoted 5 lines
It looks like most of the stuff inside of the loop can be pulled out and only done once here.
Quoted 8 lines
Quoted 2 lines
I did that in one of the previous submissions but it was pointed out to me that a parallel TX path may fill up the queue before I wake it.
Right, I see you plugged that hole, however the way you hold the spinlock across the expensive DMA management but then give it up in each loop iteration feels like this is not the most efficient way.
Quoted 3 lines
Maybe my thinking is wrong here, but I assumed that with a spinlock it's better to give other threads the chance to run in between each iteration. I didn't benchmark it though.
It depends. You want to avoid lock contention (two threads trying to get the lock at the same time) but you also want to avoid bouncing around the spinlock between the caches.
In the contention case, what I think would happen here is that the cleanup thread gives up the lock and the xmit function gets it, but then the cleanup thread is spinning again, so you are still blocked on one of the two CPUs but also pay the overhead of synchronizing between the two.
Holding the lock the whole time would speed up both the good case (no contention) and the bad case (bouncing the lock) a little bit because it saves some overhead. Holding the lock for shorter times (i.e. not during the cache operations) would reduce the amount of lock-contention but not help in the good case.
Not needing a lock at all is generally best, but getting it right is tricky ;-)
Arnd
Re: [PATCH v3 10/15] net: ethernet: mtk-eth-mac: new driver
In reply to Bartosz GolaszewskiQuoted 26 lines · Bartosz Golaszewski
czw., 14 maj 2020 o 18:19 Arnd Bergmann <[email protected]> napisał(a):
Quoted 13 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.
Signed-off-by Bartosz Golaszewski <[email protected]>Looks very nice overall. Just a few things I noticed, and some ideas that may or may not make sense:
Quoted 11 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c line 42
Quoted 7 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +7
-0+/* This is defined to 0 on arm64 in arch/arm64/include/asm/processor.h but +* this IP doesn't work without this alignment being equal to 2. +*/ +#ifdef NET_IP_ALIGN +#undef NET_IP_ALIGN +#endif +#define NET_IP_ALIGN 2Maybe you should just define your own macro instead of replacing the normal one then?
Quoted 3 lines
I did in an earlier version and was told to use
NET_IP_ALIGNbut then found out its value on arm64 doesn't work for me so I did the thing that won't make anybody happy - redefine the existing constant. :)
Hi Bartosz
I did not realise ARM64 set it to 0. As Arnd suggested, please define your own macro.
Andrew
Re: [PATCH v3 10/15] net: ethernet: mtk-eth-mac: new driver
In reply to Bartosz GolaszewskiReview
Quoted 2 lines · Bartosz Golaszewski
(quoted diff) +2
-0+static int mtk_mac_ring_pop_tail(struct mtk_mac_ring *ring, + struct mtk_mac_ring_desc_data *desc_data)
I took another look at this function because of your comment on the locking the descriptor updates, which seemed suspicious as the device side does not actually use the locks to access them
drivers/net/ethernet/mediatek/mtk_eth_mac.c line 306
Quoted 9 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +9
-0+{ +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 ;
The dma_rmb() seems odd here, as I don't see which prior read is being protected by this.
drivers/net/ethernet/mediatek/mtk_eth_mac.c line 319
Quoted 12 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +12
-0+ desc_data->len = status &MTK_MAC_DESC_MSK_LEN ; + desc_data->flags = status & ~MTK_MAC_DESC_MSK_LEN ; + desc_data->dma_addr = ring->dma_addrs[ring->tail]; + 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 ; + +/* Flush writes to descriptor memory. */ +dma_wmb ();
The comment and the barrier here seem odd as well. I would have expected a barrier after the update to the data pointer, and only a single store but no read of the status flag instead of the read-modify-write, something like
desc->data_ptr = 0;
dma_wmb(); /* make pointer update visible before status update */
desc->status = MTK_MAC_DESC_BIT_COWN | (status & MTK_MAC_DESC_BIT_EOR);drivers/net/ethernet/mediatek/mtk_eth_mac.c line 322
Quoted 2 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +2
-0+ ring->tail = (ring->tail +1 ) %MTK_MAC_RING_NUM_DESCS ; + ring->count--;
I would get rid of the 'count' here, as it duplicates the information that is already known from the difference between head and tail, and you can't update it atomically without holding a lock around the access to the ring. The way I'd do this is to have the head and tail pointers in separate cache lines, and then use READ_ONCE/WRITE_ONCE and smp barriers to access them, with each one updated on one thread but read by the other.
Arnd
Re: [PATCH v3 10/15] net: ethernet: mtk-eth-mac: new driver
pt., 15 maj 2020 o 15:32 Arnd Bergmann <[email protected]> napisał(a):
Quoted 47 lines · Bartosz Golaszewski
Review
Quoted 2 lines
(quoted diff) +2
-0+static int mtk_mac_ring_pop_tail(struct mtk_mac_ring *ring, + struct mtk_mac_ring_desc_data *desc_data)I took another look at this function because of your comment on the locking the descriptor updates, which seemed suspicious as the device side does not actually use the locks to access them
drivers/net/ethernet/mediatek/mtk_eth_mac.c line 306
Quoted 9 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +9
-0+{ +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 ;The
dma_rmb()seems odd here, as I don't see which prior read is being protected by this.drivers/net/ethernet/mediatek/mtk_eth_mac.c line 319
Quoted 12 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +12
-0+ desc_data->len = status &MTK_MAC_DESC_MSK_LEN ; + desc_data->flags = status & ~MTK_MAC_DESC_MSK_LEN ; + desc_data->dma_addr = ring->dma_addrs[ring->tail]; + 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 ; + +/* Flush writes to descriptor memory. */ +dma_wmb ();The comment and the barrier here seem odd as well. I would have expected a barrier after the update to the data pointer, and only a single store but no read of the status flag instead of the read-modify-write, something like
desc->data_ptr = 0; dma_wmb(); /* make pointer update visible before status update */ desc->status = MTK_MAC_DESC_BIT_COWN | (status & MTK_MAC_DESC_BIT_EOR);drivers/net/ethernet/mediatek/mtk_eth_mac.c line 322
Quoted 2 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +2
-0+ ring->tail = (ring->tail +1 ) %MTK_MAC_RING_NUM_DESCS ; + ring->count--;
Quoted 8 lines
I would get rid of the 'count' here, as it duplicates the information that is already known from the difference between head and tail, and you can't update it atomically without holding a lock around the access to the ring. The way I'd do this is to have the head and tail pointers in separate cache lines, and then use READ_ONCE/WRITE_ONCE and smp barriers to access them, with each one updated on one thread but read by the other.
Your previous solution seems much more reliable though. For instance in the above: when we're doing the TX cleanup (we got the TX ready irq, we're iterating over descriptors until we know there are no more packets scheduled (count == 0) or we encounter one that's still owned by DMA), a parallel TX path can schedule new packets to be sent and I don't see how we can atomically check the count (understood as a difference between tail and head) and run a new iteration (where we'd modify the head or tail) without risking the other path getting in the way. We'd have to always check the descriptor.
I experimented a bit with this and couldn't come up with anything that would pass any stress test.
On the other hand: spin_lock_bh() works fine and I like your approach from the previous e-mail - except for the work for updating stats as we could potentially lose some stats when we're updating in process context with RX/TX paths running in parallel in napi context but that would be rare enough to overlook it.
I hope v4 will be good enough even with spinlocks. :)
Bart
Re: [PATCH v3 10/15] net: ethernet: mtk-eth-mac: new driver
Quoted 11 lines · Bartosz Golaszewski
pt., 15 maj 2020 o 15:32 Arnd Bergmann <[email protected]> napisał(a):
Quoted 8 lines
I would get rid of the 'count' here, as it duplicates the information that is already known from the difference between head and tail, and you can't update it atomically without holding a lock around the access to the ring. The way I'd do this is to have the head and tail pointers in separate cache lines, and then use READ_ONCE/WRITE_ONCE and smp barriers to access them, with each one updated on one thread but read by the other.
Quoted 9 lines
Your previous solution seems much more reliable though. For instance in the above: when we're doing the TX cleanup (we got the TX ready irq, we're iterating over descriptors until we know there are no more packets scheduled (count == 0) or we encounter one that's still owned by DMA), a parallel TX path can schedule new packets to be sent and I don't see how we can atomically check the count (understood as a difference between tail and head) and run a new iteration (where we'd modify the head or tail) without risking the other path getting in the way. We'd have to always check the descriptor.
It should be enough to read both pointers once at the start of each side, then do whatever work you want to do (cleaning, sending, receiving, refilling) and finally updating the one pointer that changed. If both sides do that, you minimize the cache line bouncing and always do a useful amount of work that guarantees forward progress and does not interfere with the other side.
Quoted 10 lines
I experimented a bit with this and couldn't come up with anything that would pass any stress test.
On the other hand:
spin_lock_bh()works fine and I like your approach from the previous e-mail - except for the work for updating stats as we could potentially lose some stats when we're updating in process context with RX/TX paths running in parallel in napi context but that would be rare enough to overlook it.I hope v4 will be good enough even with spinlocks. :)
Yes, it should be fine. Avoiding all the locks is mainly an optimization for the number of CPU cycles spent per packet, the other points are more important to get right, in particular the flow control.
Arnd
PATCH v3 11/15 ARM64: dts: mediatek: add pericfg syscon to mt8516.dtsi
In reply to Bartosz GolaszewskiFrom: 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
@@ -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
PATCH v3 12/15 ARM64: dts: mediatek: add the ethernet node to mt8516.dtsi
In reply to Bartosz GolaszewskiFrom: 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
@@ -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
PATCH v3 13/15 ARM64: dts: mediatek: add an alias for ethernet0 for pumpkin boards
In reply to Bartosz GolaszewskiFrom: 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
@@ -9,6 +9,7 @@ / { aliases { serial0 = &uart0; + ethernet0 = ðernet; }; chosen {
Signature · 1 line
2.25.0
PATCH v3 14/15 ARM64: dts: mediatek: add ethernet pins for pumpkin boards
In reply to Bartosz GolaszewskiFrom: 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
@@ -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
PATCH v3 15/15 ARM64: dts: mediatek: enable ethernet on pumpkin boards
In reply to Bartosz GolaszewskiFrom: 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
@@ -167,6 +167,24 @@ &uart0 { status = "okay"; }; +ðernet { + pinctrl-names = "default"; + pinctrl-0 = <ðernet_pins_default>; + phy-handle = <ð_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
David Miller
maintainerFrom: Bartosz Golaszewski <[email protected]>
Date: Thu, 14 May 2020 09:59:27 +0200
Quoted 2 lines
Next we do some cleanup of the mediatek ethernet drivers directory and update the devres documentation with existing networking devres helpers.
I don't agree with the new devres stuff.
You have to be very careful with the ordering of when you map/unmap registers, free up anciliary resources, etc. in relationship to when the netdev unregister happens.
Please submit this driver without these controversial devres changes, and then you can submit and discuss those changes separately later.
Thanks.