mediatek: add support for MediaTek Ethernet MAC
Subsystems (5)
v4 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.
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
v3 -> v4:
- drop the devres patches: they will be sent separately
- call netdev_sent_queue() & netdev_completed_queue() where appropriate
- don't redefine NET_IP_ALIGN: define a private constant in the driver
- fix a couple typos
- only disabe/enable the MAC in suspend/resume if netif is running
- drop the count field from the ring structure and instead calculate the number of used descriptors from the tail and head indicies
- rework the locking used to protect the ring structures from concurrent
access: use cheaper spin_lock_bh() and completely disable the internal spinlock used by regmap
- rework the interrupt handling to make it more fine-grained: onle re-enable TX and RX interrupts while they're needed, process the stats updates in a workqueue, not in napi context
- shrink the code responsible for unmapping and freeing skb memory
- rework the barriers as advised by Arnd
Bartosz Golaszewski (11):
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
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 + 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 | 1668 +++++++++++++++++ 8 files changed, 1881 insertions(+), 38 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
Signature · 1 line
2.25.0
PATCH v4 1/11 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 v4 2/11 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 · 1 line
2.25.0
PATCH v4 3/11 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 v4 4/11 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 · 1 line
2.25.0
PATCH v4 5/11 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 v4 6/11 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 | 1668 +++++++++++++++++++ 3 files changed, 1675 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 +1668 -0
1,668 lines not on this page · raw message
Signature · 1 line
2.25.0
Re: [PATCH v4 06/11] 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 much better, thanks for addressing my feedback. A few more things about this version:
Quoted 12 lines
--- drivers/net/ethernet/mediatek/Kconfig | 6 + drivers/net/ethernet/mediatek/Makefile | 1 + drivers/net/ethernet/mediatek/mtk_eth_mac.c | 1668 +++++++++++++++++++ 3 files changed, 1675 insertions(+) create mode 100644 drivers/net/ethernet/mediatek/mtk_eth_mac.cdrivers/net/ethernet/mediatek/Kconfig +0
-0
drivers/net/ethernet/mediatek/Kconfig config NET_MEDIATEK_SOC · line 21
Quoted 8 lines
drivers/net/ethernet/mediatek/Kconfig +5
-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.
I just noticed how the naming of NET_MEDIATEK_MAC and NET_MEDIATEK_SOC for two different drivers doing the same thing is really confusing.
Maybe someone can come up with a better name, such as one based on the soc it first showed up in.
Quoted 7 lines
drivers/net/ethernet/mediatek/Kconfig +7
-0+ struct mtk_mac_ring_desc *desc = &ring->descs[ring->head]; + unsigned int status; + + status = desc->status; + + ring->skbs[ring->head] = desc_data->skb; + ring->dma_addrs[ring->head] = desc_data->dma_addr;
drivers/net/ethernet/mediatek/Kconfig config NET_MEDIATEK_SOC
Quoted 10 lines
drivers/net/ethernet/mediatek/Kconfig +10
-0+ desc->data_ptr = desc_data->dma_addr; + + status |= desc_data->len; + if (flags) + status |= flags; + desc->status = status; + + /* Flush previous modifications before ownership change. */ + dma_wmb(); + desc->status &= ~MTK_MAC_DESC_BIT_COWN;
You still do the read-modify-write on the word here, which is expensive on uncached memory. You have read the value already, so better use an assignment rather than &=, or (better) READ_ONCE() and WRITE_ONCE() to prevent the compiler from adding further accesses.
drivers/net/ethernet/mediatek/Kconfig
Quoted 9 lines
drivers/net/ethernet/mediatek/Kconfig +9
-0+static void mtk_mac_lock(struct mtk_mac_priv *priv) +{ + spin_lock_bh(&priv->lock); +} + +static void mtk_mac_unlock(struct mtk_mac_priv *priv) +{ + spin_unlock_bh(&priv->lock); +}
I think open-coding the locks would make this more readable, and let you use spin_lock() instead of spin_lock_bh() in those functions that are already in softirq context.
drivers/net/ethernet/mediatek/Kconfig
Quoted 10 lines
drivers/net/ethernet/mediatek/Kconfig +10
-0+static void mtk_mac_intr_enable_tx(struct mtk_mac_priv *priv) +{ + regmap_update_bits(priv->regs, MTK_MAC_REG_INT_MASK, + MTK_MAC_BIT_INT_STS_TNTC, 0); +} +static void mtk_mac_intr_enable_rx(struct mtk_mac_priv *priv) +{ + regmap_update_bits(priv->regs, MTK_MAC_REG_INT_MASK, + MTK_MAC_BIT_INT_STS_FNRC, 0); +}
These imply reading the irq mask register and then writing it again, which is much more expensive than just writing it. It's also not atomic since the regmap does not use a lock.
I don't think you actually need to enable/disable rx and tx separately, but if you do, then writing to the Ack register as I suggested instead of updating the mask would let you do this.
Quoted 13 lines
drivers/net/ethernet/mediatek/Kconfig +13
-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; + bool need_napi = false; + unsigned int status; + + ndev = data; + priv = netdev_priv(ndev); + + if (netif_running(ndev)) { + status = mtk_mac_intr_read(priv);
drivers/net/ethernet/mediatek/Kconfig
Quoted 10 lines
drivers/net/ethernet/mediatek/Kconfig +10
-0+ + if (status & MTK_MAC_BIT_INT_STS_TNTC) { + mtk_mac_intr_disable_tx(priv); + need_napi = true; + } + + if (status & MTK_MAC_BIT_INT_STS_FNRC) { + mtk_mac_intr_disable_rx(priv); + need_napi = true; + }
I think you mixed up the rx and tx bits here: when you get an rx interrupt, that one is already blocked until it gets acked and you just need to disable tx until the end of the poll function.
However, I suspect that the overhead of turning them off is higher than what you can save, and simply ignoring the mask with
if (status & (MTK_MAC_BIT_INT_STS_FNRC | MTK_MAC_BIT_INT_STS_TNTC))
napi_schedule(&priv->napi);
would be simpler and faster.
+ /* One of the counters reached 0x8000000 - update stats and
drivers/net/ethernet/mediatek/Kconfig
Quoted 8 lines
drivers/net/ethernet/mediatek/Kconfig +8
-0+ * reset all counters. + */ + if (unlikely(status & MTK_MAC_REG_INT_STS_MIB_CNT_TH)) { + mtk_mac_intr_disable_stats(priv); + schedule_work(&priv->stats_work); + } + befor + mtk_mac_intr_ack_all(priv);
The ack here needs to be dropped, otherwise you can get further interrupts before the bottom half has had a chance to run.
You might be lucky because you had already disabled the individual bits earlier, but I don't think that was intentional here.
Quoted 17 lines
drivers/net/ethernet/mediatek/Kconfig +17
-0+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); +
drivers/net/ethernet/mediatek/Kconfig
Quoted 10 lines
drivers/net/ethernet/mediatek/Kconfig +10
-0+ mtk_mac_ring_push_head_tx(ring, &desc_data); + + netdev_sent_queue(ndev, skb->len); + + if (mtk_mac_ring_full(ring)) + netif_stop_queue(ndev); + + mtk_mac_unlock(priv); + + mtk_mac_dma_resume_tx(priv);
mtk_mac_dma_resume_tx() is an expensive read-modify-write on an mmio register, so it would make sense to defer it based on netdev_xmit_more(). (I had missed this in the previous review)
Quoted 15 lines
drivers/net/ethernet/mediatek/Kconfig +15
-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, pkts_compl, bytes_compl; + bool wake = false; + + mtk_mac_lock(priv); + + for (pkts_compl = 0, bytes_compl = 0;; + pkts_compl++, bytes_compl += ret, wake = true) { + if (!mtk_mac_ring_descs_available(ring)) + break; + + ret = mtk_mac_tx_complete_one(priv);
drivers/net/ethernet/mediatek/Kconfig
Quoted 10 lines
drivers/net/ethernet/mediatek/Kconfig +10
-0+ if (ret < 0) + break; + } + + netdev_completed_queue(ndev, pkts_compl, bytes_compl); + + if (wake && netif_queue_stopped(ndev)) + netif_wake_queue(ndev); + + mtk_mac_intr_enable_tx(priv);
No need to ack the interrupt here if napi is still active. Just ack both rx and tx when calling napi_complete().
Some drivers actually use the napi budget for both rx and tx:
if you have more than 'budget' completed tx frames, return early from this function and skip the napi_complete even when less than 'budget' rx frames have arrived.
This way you get more fairness between devices and can run for longer with irqs disabled as long as either rx or tx is busy.
Arnd
Re: [PATCH v4 06/11] net: ethernet: mtk-eth-mac: new driver
śr., 20 maj 2020 o 16:37 Arnd Bergmann <[email protected]> napisał(a):
Quoted 37 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 much better, thanks for addressing my feedback. A few more things about this version:
Quoted 12 lines
--- drivers/net/ethernet/mediatek/Kconfig | 6 + drivers/net/ethernet/mediatek/Makefile | 1 + drivers/net/ethernet/mediatek/mtk_eth_mac.c | 1668 +++++++++++++++++++ 3 files changed, 1675 insertions(+) create mode 100644 drivers/net/ethernet/mediatek/mtk_eth_mac.cdrivers/net/ethernet/mediatek/Kconfig +0
-0drivers/net/ethernet/mediatek/Kconfig config NET_MEDIATEK_SOC · line 21
Quoted 8 lines
drivers/net/ethernet/mediatek/Kconfig +5
-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.I just noticed how the naming of
NET_MEDIATEK_MACandNET_MEDIATEK_SOCfor two different drivers doing the same thing is really confusing.
Quoted 3 lines
Maybe someone can come up with a better name, such as one based on the soc it first showed up in.
This has been discussed under one of the previous submissions. MediaTek wants to use this IP on future designs as well and it's already used on multiple SoCs so they want the name to be generic. I also argued that this is a driver strongly tied to a specific platform(s) so if someone wants to compile it - they probably know what they're doing.
That being said: I verified with MediaTek and the name of the IP I can use is "star" so they proposed "mtk-star-eth". I would personally maybe go with "mtk-star-mac". How about those two?
Quoted 15 lines
Quoted 15 lines
drivers/net/ethernet/mediatek/Kconfig +15
-0+ struct mtk_mac_ring_desc *desc = &ring->descs[ring->head]; + unsigned int status; + + status = desc->status; + + ring->skbs[ring->head] = desc_data->skb; + ring->dma_addrs[ring->head] = desc_data->dma_addr; + desc->data_ptr = desc_data->dma_addr; + + status |= desc_data->len; + if (flags) + status |= flags; + desc->status = status; + + /* Flush previous modifications before ownership change. */
Quoted 9 lines
drivers/net/ethernet/mediatek/Kconfig
Quoted 2 lines
drivers/net/ethernet/mediatek/Kconfig +2
-0+ dma_wmb(); + desc->status &= ~MTK_MAC_DESC_BIT_COWN;You still do the read-modify-write on the word here, which is expensive on uncached memory. You have read the value already, so better use an assignment rather than &=, or (better)
READ_ONCE()andWRITE_ONCE()to prevent the compiler from adding further accesses.
Will do.
Quoted 15 lines
drivers/net/ethernet/mediatek/Kconfig
Quoted 10 lines
drivers/net/ethernet/mediatek/Kconfig +9
-0+static void mtk_mac_lock(struct mtk_mac_priv *priv) +{ + spin_lock_bh(&priv->lock); +} + +static void mtk_mac_unlock(struct mtk_mac_priv *priv) +{ + spin_unlock_bh(&priv->lock); +}I think open-coding the locks would make this more readable, and let you use
spin_lock()instead ofspin_lock_bh()in those functions that are already in softirq context.
Will do.
Quoted 67 lines
drivers/net/ethernet/mediatek/Kconfig
Quoted 10 lines
drivers/net/ethernet/mediatek/Kconfig +10
-0+static void mtk_mac_intr_enable_tx(struct mtk_mac_priv *priv) +{ + regmap_update_bits(priv->regs, MTK_MAC_REG_INT_MASK, + MTK_MAC_BIT_INT_STS_TNTC, 0); +} +static void mtk_mac_intr_enable_rx(struct mtk_mac_priv *priv) +{ + regmap_update_bits(priv->regs, MTK_MAC_REG_INT_MASK, + MTK_MAC_BIT_INT_STS_FNRC, 0); +}These imply reading the irq mask register and then writing it again, which is much more expensive than just writing it. It's also not atomic since the regmap does not use a lock.
I don't think you actually need to enable/disable rx and tx separately, but if you do, then writing to the Ack register as I suggested instead of updating the mask would let you do this.
Quoted 13 lines
drivers/net/ethernet/mediatek/Kconfig +13
-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; + bool need_napi = false; + unsigned int status; + + ndev = data; + priv = netdev_priv(ndev); + + if (netif_running(ndev)) { + status = mtk_mac_intr_read(priv);drivers/net/ethernet/mediatek/Kconfig
Quoted 10 lines
drivers/net/ethernet/mediatek/Kconfig +10
-0+ + if (status & MTK_MAC_BIT_INT_STS_TNTC) { + mtk_mac_intr_disable_tx(priv); + need_napi = true; + } + + if (status & MTK_MAC_BIT_INT_STS_FNRC) { + mtk_mac_intr_disable_rx(priv); + need_napi = true; + }I think you mixed up the rx and tx bits here: when you get an rx interrupt, that one is already blocked until it gets acked and you just need to disable tx until the end of the poll function.
However, I suspect that the overhead of turning them off is higher than what you can save, and simply ignoring the mask with
if (status & (
MTK_MAC_BIT_INT_STS_FNRC|MTK_MAC_BIT_INT_STS_TNTC))napi_schedule(&priv->napi);would be simpler and faster.
+ /* One of the counters reached
0x8000000- update stats anddrivers/net/ethernet/mediatek/Kconfig
Quoted 8 lines
drivers/net/ethernet/mediatek/Kconfig +8
-0+ * reset all counters. + */ + if (unlikely(status & MTK_MAC_REG_INT_STS_MIB_CNT_TH)) { + mtk_mac_intr_disable_stats(priv); + schedule_work(&priv->stats_work); + } + befor + mtk_mac_intr_ack_all(priv);
Quoted 3 lines
The ack here needs to be dropped, otherwise you can get further interrupts before the bottom half has had a chance to run.
My thinking was this: if I mask the relevant interrupt (TX/RX complete) and ack it right away, the status bit will be asserted on the next packet received/sent but the process won't get interrupted and when I unmask it, it will fire right away and I won't have to recheck the status register. I noticed that if I ack it at the end of napi poll callback, I end up missing certain TX complete interrupts and end up seeing a lot of retransmissions even if I reread the status register. I'm not yet sure where this race happens.
Quoted 27 lines
You might be lucky because you had already disabled the individual bits earlier, but I don't think that was intentional here.
Quoted 24 lines
drivers/net/ethernet/mediatek/Kconfig +24
-0+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); + + netdev_sent_queue(ndev, skb->len); + + if (mtk_mac_ring_full(ring)) + netif_stop_queue(ndev); +
Quoted 9 lines
drivers/net/ethernet/mediatek/Kconfig
Quoted 3 lines
drivers/net/ethernet/mediatek/Kconfig +3
-0+ mtk_mac_unlock(priv); + + mtk_mac_dma_resume_tx(priv);
mtk_mac_dma_resume_tx()is an expensive read-modify-write on an mmio register, so it would make sense to defer it based onnetdev_xmit_more(). (I had missed this in the previous review)
Thanks for bringing it to my attention, I'll see about using it.
Quoted 29 lines
Quoted 15 lines
drivers/net/ethernet/mediatek/Kconfig +15
-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, pkts_compl, bytes_compl; + bool wake = false; + + mtk_mac_lock(priv); + + for (pkts_compl = 0, bytes_compl = 0;; + pkts_compl++, bytes_compl += ret, wake = true) { + if (!mtk_mac_ring_descs_available(ring)) + break; + + ret = mtk_mac_tx_complete_one(priv);drivers/net/ethernet/mediatek/Kconfig
Quoted 10 lines
drivers/net/ethernet/mediatek/Kconfig +10
-0+ if (ret < 0) + break; + } + + netdev_completed_queue(ndev, pkts_compl, bytes_compl); + + if (wake && netif_queue_stopped(ndev)) + netif_wake_queue(ndev); + + mtk_mac_intr_enable_tx(priv);No need to ack the interrupt here if napi is still active. Just ack both rx and tx when calling
napi_complete().
Quoted 5 lines
Some drivers actually use the napi budget for both rx and tx:
if you have more than 'budget' completed tx frames, return early from this function and skip thenapi_completeeven when less than 'budget' rx frames have arrived.
IIRC Jakub said that the most seen approach is to free all TX descs and receive up to budget packets, so this is what I did. I think it makes the most sense.
Best regards,
Bartosz
Quoted 5 lines
This way you get more fairness between devices and can run for longer with irqs disabled as long as either rx or tx is busy.
Arnd
Re: [PATCH v4 06/11] net: ethernet: mtk-eth-mac: new driver
Quoted 16 lines · Bartosz Golaszewski
śr., 20 maj 2020 o 16:37 Arnd Bergmann <[email protected]> napisał(a):
Quoted 6 lines
I just noticed how the naming of
NET_MEDIATEK_MACandNET_MEDIATEK_SOCfor two different drivers doing the same thing is really confusing.Maybe someone can come up with a better name, such as one based on the soc it first showed up in.
This has been discussed under one of the previous submissions. MediaTek wants to use this IP on future designs as well and it's already used on multiple SoCs so they want the name to be generic. I also argued that this is a driver strongly tied to a specific platform(s) so if someone wants to compile it - they probably know what they're doing.
Quoted 3 lines
That being said: I verified with MediaTek and the name of the IP I can use is "star" so they proposed "mtk-star-eth". I would personally maybe go with "mtk-star-mac". How about those two?
Both seem fine to me. If this was previously discussed, I don't want do further bike-shedding and I'd trust you to pick a sensible name based on the earlier discussions.
Quoted 14 lines
Quoted 10 lines
+ /* One of the counters reached
0x8000000- update stats anddrivers/net/ethernet/mediatek/mtk_eth_mac.c line 816
Quoted 8 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +8
-0+* reset all counters. +*/ +if (unlikely (status &MTK_MAC_REG_INT_STS_MIB_CNT_TH )) { +mtk_mac_intr_disable_stats (priv); +schedule_work (&priv->stats_work); + } +befor +mtk_mac_intr_ack_all (priv );Quoted 3 lines
The ack here needs to be dropped, otherwise you can get further interrupts before the bottom half has had a chance to run.
Quoted 8 lines
My thinking was this: if I mask the relevant interrupt (TX/RX complete) and ack it right away, the status bit will be asserted on the next packet received/sent but the process won't get interrupted and when I unmask it, it will fire right away and I won't have to recheck the status register. I noticed that if I ack it at the end of napi poll callback, I end up missing certain TX complete interrupts and end up seeing a lot of retransmissions even if I reread the status register. I'm not yet sure where this race happens.
Right, I see. If you just ack at the end of the poll function, you need to check the rings again to ensure you did not miss an interrupt between checking observing both rings to be empty and the irq-ack.
I suspect it's still cheaper to check the two rings with an uncached read from memory than to to do the read-modify-write on the mmio, but you'd have to measure that to be sure.
Quoted 35 lines
Quoted 29 lines
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 mtk_mac_ring *ring = &priv->tx_ring; +struct net_device *ndev = priv->ndev; +int ret, pkts_compl, bytes_compl; +bool wake = false; + +mtk_mac_lock (priv); + +for (pkts_compl =0 , bytes_compl =0 ;; + pkts_compl++, bytes_compl += ret, wake = true) { +if (!mtk_mac_ring_descs_available (ring)) +break ; + + ret =mtk_mac_tx_complete_one (priv);drivers/net/ethernet/mediatek/mtk_eth_mac.c line 1185
Quoted 10 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +10
-0+if (ret <0 ) +break ; + } + +netdev_completed_queue (ndev, pkts_compl, bytes_compl); + +if (wake &&netif_queue_stopped (ndev)) +netif_wake_queue (ndev); + +mtk_mac_intr_enable_tx (priv);No need to ack the interrupt here if napi is still active. Just ack both rx and tx when calling
napi_complete().Quoted 5 lines
Some drivers actually use the napi budget for both rx and tx:
if you have more than 'budget' completed tx frames, return early from this function and skip thenapi_completeeven when less than 'budget' rx frames have arrived.
Quoted 3 lines
IIRC Jakub said that the most seen approach is to free all TX descs and receive up to budget packets, so this is what I did. I think it makes the most sense.
Ok, he's probably right then.
My idea was that the dma_unmap operation for the tx cleanup is rather expensive on chips without cache-coherent DMA, so you might not want to do too much of it but rather do it in reasonably sized batches. It would also avoid the case where you renable the tx-complete interrupt after cleaning the already-sent frames but then immediately get an irq when the next frame that is already queued is done.
This probably depends on the specific workload which one works better here.
Arnd
Re: [PATCH v4 06/11] net: ethernet: mtk-eth-mac: new driver
śr., 20 maj 2020 o 23:23 Arnd Bergmann <[email protected]> napisał(a):
Quoted 49 lines · Bartosz Golaszewski
Quoted 16 lines
śr., 20 maj 2020 o 16:37 Arnd Bergmann <[email protected]> napisał(a):
Quoted 6 lines
I just noticed how the naming of
NET_MEDIATEK_MACandNET_MEDIATEK_SOCfor two different drivers doing the same thing is really confusing.Maybe someone can come up with a better name, such as one based on the soc it first showed up in.
This has been discussed under one of the previous submissions. MediaTek wants to use this IP on future designs as well and it's already used on multiple SoCs so they want the name to be generic. I also argued that this is a driver strongly tied to a specific platform(s) so if someone wants to compile it - they probably know what they're doing.
Quoted 3 lines
That being said: I verified with MediaTek and the name of the IP I can use is "star" so they proposed "mtk-star-eth". I would personally maybe go with "mtk-star-mac". How about those two?
Both seem fine to me. If this was previously discussed, I don't want do further bike-shedding and I'd trust you to pick a sensible name based on the earlier discussions.
Quoted 14 lines
Quoted 10 lines
+ /* One of the counters reached
0x8000000- update stats anddrivers/net/ethernet/mediatek/mtk_eth_mac.c line 816
Quoted 8 lines
drivers/net/ethernet/mediatek/mtk_eth_mac.c new file +8
-0+* reset all counters. +*/ +if (unlikely (status &MTK_MAC_REG_INT_STS_MIB_CNT_TH )) { +mtk_mac_intr_disable_stats (priv); +schedule_work (&priv->stats_work); + } +befor +mtk_mac_intr_ack_all (priv );Quoted 3 lines
The ack here needs to be dropped, otherwise you can get further interrupts before the bottom half has had a chance to run.
Quoted 8 lines
My thinking was this: if I mask the relevant interrupt (TX/RX complete) and ack it right away, the status bit will be asserted on the next packet received/sent but the process won't get interrupted and when I unmask it, it will fire right away and I won't have to recheck the status register. I noticed that if I ack it at the end of napi poll callback, I end up missing certain TX complete interrupts and end up seeing a lot of retransmissions even if I reread the status register. I'm not yet sure where this race happens.
Quoted 8 lines
Right, I see. If you just ack at the end of the poll function, you need to check the rings again to ensure you did not miss an interrupt between checking observing both rings to be empty and the irq-ack.
I suspect it's still cheaper to check the two rings with an uncached read from memory than to to do the read-modify-write on the mmio, but you'd have to measure that to be sure.
Unfortunately the PHY on the board I have is 100Mbps which is the limiting factor in benchmarking this driver. :(
If you're fine with this - I'd like to fix the minor issues you pointed out and stick with the current approach for now. We can always fix the implementation in the future once a board with a Gigabit PHY is out. Most ethernet drivers don't use such fine-grained interrupt control anyway. I expect the performance differences to be miniscule really.
Bart
Re: [PATCH v4 06/11] net: ethernet: mtk-eth-mac: new driver
Quoted 25 lines · Bartosz Golaszewski
śr., 20 maj 2020 o 23:23 Arnd Bergmann <[email protected]> napisał(a):
Quoted 12 lines · Bartosz Golaszewski
Quoted 10 lines
śr., 20 maj 2020 o 16:37 Arnd Bergmann <[email protected]> napisał(a):
My thinking was this: if I mask the relevant interrupt (TX/RX complete) and ack it right away, the status bit will be asserted on the next packet received/sent but the process won't get interrupted and when I unmask it, it will fire right away and I won't have to recheck the status register. I noticed that if I ack it at the end of napi poll callback, I end up missing certain TX complete interrupts and end up seeing a lot of retransmissions even if I reread the status register. I'm not yet sure where this race happens.
Quoted 8 lines
Right, I see. If you just ack at the end of the poll function, you need to check the rings again to ensure you did not miss an interrupt between checking observing both rings to be empty and the irq-ack.
I suspect it's still cheaper to check the two rings with an uncached read from memory than to to do the read-modify-write on the mmio, but you'd have to measure that to be sure.
Unfortunately the PHY on the board I have is 100Mbps which is the limiting factor in benchmarking this driver. :(
Quoted 6 lines
If you're fine with this - I'd like to fix the minor issues you pointed out and stick with the current approach for now. We can always fix the implementation in the future once a board with a Gigabit PHY is out. Most ethernet drivers don't use such fine-grained interrupt control anyway. I expect the performance differences to be miniscule really.
Ok, fair enough. The BQL limiting is the part that matters the most for performance on slow lines (preventing long latencies from buffer bloat), and you have that now.
Arnd
PATCH v4 7/11 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 v4 8/11 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 v4 9/11 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 v4 10/11 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 v4 11/11 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