net: emac: emac gigabit ethernet controller driver
Subsystems (5)
PATCH v6 1/1 net: emac: emac gigabit ethernet controller driver
Add supports for ethernet controller HW on Qualcomm Technologies, Inc. SoC. This driver supports the following features:
1) Checksum offload.
2) Interrupt coalescing support.
3) SGMII phy.
4) phylib interface for external phy
Based on original work by
Niranjana Vishwanathapura <[email protected]> Gilad Avidov <[email protected]>
---
v6:
- Properly ordered local variables
- use built-in GEN_MASK instead of BITS_MASK
- remove redundant call to emac_rx_mode_set from emac_mac_up
- removed emac_rfd structure, use dma_addr_t directly instead
- removed emac_mac_speed enun, replaced with macros
- removed superfluous phy_stop from emac_mac_down(), which prevented reloading module
- add missing netif_napi_del
- set the DMA mask
v5:
- changed author to Timur, added MAINTAINERS entry
- use phylib, replacing internal phy code
- added support for EMAC internal SGMII v2
- fix ~DIS_INT warning
- update DT bindings, including removing unused properties
- removed interrupt handler for internal sgmii
- removed link status check handler/state (replaced with phylib)
- removed periodic timer handler (replaced with phylib)
- removed power management code (will be rewritten later)
- external phy is now required, not optional
- removed redundant EMAC_STATUS_DOWN status flag
- removed redundant link status and speed variables
- removed redundant status bits (vlan strip, promiscuous, loopback, etc)
- removed useless watchdog status
- removed command-line parameters
- cleaned up probe messages
- removed redundant params from emac_sgmii_link_init()
- always call netdev_completed_queue() (per review comment)
- fix emac_napi_rtx() (per review comment)
- removed max_ints loop in interrupt handler
- removed redundant mutex around phy read/write calls
- added lock for reading emac status (per review comment)
- generate random MAC address if it can't be read from firmware
- replace EMAC_DMA_ADDR_HI/LO with upper/lower_32_bits
- don't test return value from platform_get_resource (per review comment)
- use net_warn_ratelimited (per review comment)
- don't set the dma masks (will be set by DT or IORT code)
- remove unused emac_tx_tpd_ts_save()
- removed redundant local MTU variable
v4:
- add missing ipv6 header file
- correct compatible string
- fix spacing in emac_reg_write arrays
- drop unnecessary cell-index property
- remove unsupported DT properties from docs
- remove GPIO initialization and update docs
v3:
- remove most of the memory barriers by using the non xxx_relaxed() api.
- remove RSS and WOL support.
- correct comments from physical address to dma address.
- rearrange structs to make them packed.
- replace polling loops with readl_poll_timeout().
- remove unnecessary wrapper functions from phy layer.
- add blank line before return statements.
- set to null clocks after clk_put().
- use module_platform_driver() and dma_set_mask_and_coherent()
- replace long hex bitmasks with BIT() macro.
v2:
- replace hw bit fields to macros with bitwise operations.
- change all iterators to unsized types (int)
- some minor code flow improvements.
- change return type to void for functions which return value is never used.
- replace instance of xxxxl_relaxed() io followed by mb() with a readl()/writel().
.../devicetree/bindings/net/qcom-emac.txt | 63 + MAINTAINERS | 6 + drivers/net/ethernet/qualcomm/Kconfig | 11 + drivers/net/ethernet/qualcomm/Makefile | 2 + drivers/net/ethernet/qualcomm/emac/Makefile | 7 + drivers/net/ethernet/qualcomm/emac/emac-mac.c | 1661 ++++++++++++++++++++ drivers/net/ethernet/qualcomm/emac/emac-mac.h | 271 ++++ drivers/net/ethernet/qualcomm/emac/emac-phy.c | 211 +++ drivers/net/ethernet/qualcomm/emac/emac-phy.h | 32 + drivers/net/ethernet/qualcomm/emac/emac-sgmii.c | 700 +++++++++ drivers/net/ethernet/qualcomm/emac/emac-sgmii.h | 24 + drivers/net/ethernet/qualcomm/emac/emac.c | 809 ++++++++++ drivers/net/ethernet/qualcomm/emac/emac.h | 369 +++++ 13 files changed, 4166 insertions(+) create mode 100644 Documentation/devicetree/bindings/net/qcom-emac.txt create mode 100644 drivers/net/ethernet/qualcomm/emac/Makefile create mode 100644 drivers/net/ethernet/qualcomm/emac/emac-mac.c create mode 100644 drivers/net/ethernet/qualcomm/emac/emac-mac.h create mode 100644 drivers/net/ethernet/qualcomm/emac/emac-phy.c create mode 100644 drivers/net/ethernet/qualcomm/emac/emac-phy.h create mode 100644 drivers/net/ethernet/qualcomm/emac/emac-sgmii.c create mode 100644 drivers/net/ethernet/qualcomm/emac/emac-sgmii.h create mode 100644 drivers/net/ethernet/qualcomm/emac/emac.c create mode 100644 drivers/net/ethernet/qualcomm/emac/emac.h
Documentation/devicetree/bindings/net/qcom-emac.txt new file +62 -0
@@ -0,0 +1,63 @@ +Qualcomm EMAC Gigabit Ethernet Controller + +Required properties: +- compatible : Should be "qcom,fsm9900-emac". +- reg : Offset and length of the register regions for the device +- reg-names : Register region names referenced in 'reg' above. + Required register resource entries are: + "base" : EMAC controller base register block. + "csr" : EMAC wrapper register block. + "sgmii" : EMAC SGMII PHY register block. + Optional register resource entries are: + "ptp" : EMAC PTP (1588) register block. +- interrupts : Interrupt numbers used by this controller +- interrupt-names : Interrupt resource names referenced in 'interrupts' above. + Required interrupt resource entries are: + "emac_core0" : EMAC core0 interrupt. + "sgmii_irq" : EMAC SGMII interrupt. +- mac-address : The 6-byte MAC address. If present, it is the + default MAC address. + +The external phy child node: +- compatible : Should be "qcom,fsm9900-emac-phy". +- reg : The phy address + +Example: +soc { + #address-cells = <1>; + #size-cells = <1>; + dma-ranges = <0 0 0xffffffff>; + + emac0: ethernet@feb20000 { + compatible = "qcom,fsm9900-emac"; + #address-cells = <1>; + #size-cells = <1>; + reg-names = "base", "csr", "ptp", "sgmii"; + reg = <0xfeb20000 0x10000>, + <0xfeb36000 0x1000>, + <0xfeb3c000 0x4000>, + <0xfeb38000 0x400>; + interrupt-parent = <&emac0>; + #interrupt-cells = <1>; + interrupts = <76 80>; + interrupt-names = "emac_core0", "sgmii_irq"; + phy0: ethernet-phy@0 { + compatible = "qcom,fsm9900-emac-phy"; + reg = <0>; + } + + pinctrl-names = "default"; + pinctrl-0 = <&mdio_pins_a>; + }; + + tlmm: pinctrl@fd510000 { + compatible = "qcom,fsm9900-pinctrl"; + + mdio_pins_a: mdio { + state { + pins = "gpio123", "gpio124"; + function = "mdio"; + }; + }; + };
MAINTAINERS +6 -0
@@ -8974,6 +8974,12 @@ T: git git://git.kernel.org/pub/scm/linux/kernel/git/kvalo/ath.git S: Supported F: drivers/net/wireless/ath/ath10k/ +QUALCOMM EMAC GIGABIT ETHERNET DRIVER +M: Timur Tabi <[email protected]> +L: [email protected] +S: Supported +F: drivers/net/ethernet/qualcomm/emac/ + QUALCOMM HEXAGON ARCHITECTURE M: Richard Kuo <[email protected]> L: [email protected]
drivers/net/ethernet/qualcomm/Kconfig +11 -0
@@ -24,4 +24,15 @@ config QCA7000 To compile this driver as a module, choose M here. The module will be called qcaspi. +config QCOM_EMAC + tristate "Qualcomm Technologies, Inc. EMAC Gigabit Ethernet support" + select CRC32 + ---help--- + This driver supports the Qualcomm Technologies, Inc. Gigabit + Ethernet Media Access Controller (EMAC). The controller + supports IEEE 802.3-2002, half-duplex mode at 10/100 Mb/s, + full-duplex mode at 10/100/1000Mb/s, Wake On LAN (WOL) for + low power, Receive-Side Scaling (RSS), and IEEE 1588-2008 + Precision Clock Synchronization Protocol. + endif # NET_VENDOR_QUALCOMM
drivers/net/ethernet/qualcomm/Makefile +2 -0
@@ -4,3 +4,5 @@ obj-$(CONFIG_QCA7000) += qcaspi.o qcaspi-objs := qca_spi.o qca_framing.o qca_7k.o qca_debug.o + +obj-$(CONFIG_QCOM_EMAC) += emac/
drivers/net/ethernet/qualcomm/emac/Makefile new file +7 -0
@@ -0,0 +1,7 @@ +# +# Makefile for the Qualcomm Technologies, Inc. EMAC Gigabit Ethernet driver +# + +obj-$(CONFIG_QCOM_EMAC) += qcom-emac.o + +qcom-emac-objs := emac.o emac-mac.o emac-phy.o emac-sgmii.o
drivers/net/ethernet/qualcomm/emac/emac-mac.c new file +1661 -0
1,661 lines not on this page · raw message
drivers/net/ethernet/qualcomm/emac/emac-mac.h new file +271 -0
271 lines not on this page · raw message
drivers/net/ethernet/qualcomm/emac/emac-phy.c new file +211 -0
211 lines not on this page · raw message
drivers/net/ethernet/qualcomm/emac/emac-phy.h new file +32 -0
@@ -0,0 +1,32 @@ +/* Copyright (c) 2015, The Linux Foundation. All rights reserved. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 and +* only version 2 as published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*/ + +#ifndef _EMAC_PHY_H_ +#define _EMAC_PHY_H_ + +/* emac_phy +* @base register file base address space. +* @digital per-lane digital block +* @version the internal phy version +*/ +struct emac_phy { +void __iomem *base; +void __iomem *digital; + +unsigned int version; +}; + +struct emac_adapter ; + +int emac_phy_config (struct platform_device *pdev,struct emac_adapter *adpt); + +#endif /* _EMAC_PHY_H_ */
drivers/net/ethernet/qualcomm/emac/emac-sgmii.c new file +700 -0
700 lines not on this page · raw message
drivers/net/ethernet/qualcomm/emac/emac-sgmii.h new file +24 -0
@@ -0,0 +1,24 @@ +/* Copyright (c) 2015, The Linux Foundation. All rights reserved. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 and +* only version 2 as published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*/ + +#ifndef _EMAC_SGMII_H_ +#define _EMAC_SGMII_H_ + +struct emac_adapter ; +struct platform_device ; + +int emac_sgmii_init_v1 (struct emac_adapter *adpt); +int emac_sgmii_init_v2 (struct emac_adapter *adpt); +int emac_sgmii_config (struct platform_device *pdev,struct emac_adapter *adpt); +void emac_sgmii_reset (struct emac_adapter *adpt); + +#endif
drivers/net/ethernet/qualcomm/emac/emac.c new file +809 -0
809 lines not on this page · raw message
drivers/net/ethernet/qualcomm/emac/emac.h new file +369 -0
369 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 3 lines
Qualcomm Innovation Center, Inc. The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project.
Part of the diff is not shown here. View the raw message.
Quoted 85 lines · Timur Tabi
Add supports for ethernet controller HW on Qualcomm Technologies, Inc. SoC. This driver supports the following features:
1) Checksum offload.
2) Interrupt coalescing support.
3) SGMII phy.
4) phylib interface for external phyBased on original work by
Niranjana Vishwanathapura <[email protected]> Gilad Avidov <[email protected]>Signed-off-by Timur Tabi <[email protected]>---
v6:
- Properly ordered local variables
- use built-inGEN_MASKinstead ofBITS_MASK
- remove redundant call toemac_rx_mode_setfromemac_mac_up
- removedemac_rfdstructure, usedma_addr_tdirectly instead
- removedemac_mac_speedenun, replaced with macros
- removed superfluousphy_stopfromemac_mac_down(), which prevented reloading module
- add missingnetif_napi_del
- set the DMA maskv5:
- changed author to Timur, addedMAINTAINERSentry
- use phylib, replacing internal phy code
- added support for EMAC internal SGMII v2
- fix ~DIS_INT warning
- update DT bindings, including removing unused properties
- removed interrupt handler for internal sgmii
- removed link status check handler/state (replaced with phylib)
- removed periodic timer handler (replaced with phylib)
- removed power management code (will be rewritten later)
- external phy is now required, not optional
- removed redundantEMAC_STATUS_DOWNstatus flag
- removed redundant link status and speed variables
- removed redundant status bits (vlan strip, promiscuous, loopback, etc)
- removed useless watchdog status
- removed command-line parameters
- cleaned up probe messages
- removed redundant params fromemac_sgmii_link_init()
- always callnetdev_completed_queue()(per review comment)
- fixemac_napi_rtx()(per review comment)
- removedmax_intsloop in interrupt handler
- removed redundant mutex around phy read/write calls
- added lock for reading emac status (per review comment)
- generate random MAC address if it can't be read from firmware
- replace EMAC_DMA_ADDR_HI/LO with upper/lower_32_bits
- don't test return value fromplatform_get_resource(per review comment)
- usenet_warn_ratelimited(per review comment)
- don't set the dma masks (will be set by DT or IORT code)
- remove unusedemac_tx_tpd_ts_save()
- removed redundant local MTU variablev4:
- add missing ipv6 header file
- correct compatible string
- fix spacing inemac_reg_writearrays
- drop unnecessary cell-index property
- remove unsupported DT properties from docs
- remove GPIO initialization and update docsv3:
- remove most of the memory barriers by using the nonxxx_relaxed()api.
- remove RSS and WOL support.
- correct comments from physical address to dma address.
- rearrange structs to make them packed.
- replace polling loops withreadl_poll_timeout().
- remove unnecessary wrapper functions from phy layer.
- add blank line before return statements.
- set to null clocks afterclk_put().
- usemodule_platform_driver()anddma_set_mask_and_coherent()
- replace long hex bitmasks withBIT()macro.v2:
- replace hw bit fields to macros with bitwise operations.
- change all iterators to unsized types (int)
- some minor code flow improvements.
- change return type to void for functions which return value is never used.
- replace instance ofxxxxl_relaxed()io followed bymb()with a readl()/writel().
Quoted 1 line
.../devicetree/bindings/net/qcom-emac.txt | 63 +
Quoted 23 lines
MAINTAINERS | 6 + drivers/net/ethernet/qualcomm/Kconfig | 11 + drivers/net/ethernet/qualcomm/Makefile | 2 + drivers/net/ethernet/qualcomm/emac/Makefile | 7 + drivers/net/ethernet/qualcomm/emac/emac-mac.c | 1661 ++++++++++++++++++++ drivers/net/ethernet/qualcomm/emac/emac-mac.h | 271 ++++ drivers/net/ethernet/qualcomm/emac/emac-phy.c | 211 +++ drivers/net/ethernet/qualcomm/emac/emac-phy.h | 32 + drivers/net/ethernet/qualcomm/emac/emac-sgmii.c | 700 +++++++++ drivers/net/ethernet/qualcomm/emac/emac-sgmii.h | 24 + drivers/net/ethernet/qualcomm/emac/emac.c | 809 ++++++++++ drivers/net/ethernet/qualcomm/emac/emac.h | 369 +++++ 13 files changed, 4166 insertions(+) create mode 100644 Documentation/devicetree/bindings/net/qcom-emac.txt create mode 100644 drivers/net/ethernet/qualcomm/emac/Makefile create mode 100644 drivers/net/ethernet/qualcomm/emac/emac-mac.c create mode 100644 drivers/net/ethernet/qualcomm/emac/emac-mac.h create mode 100644 drivers/net/ethernet/qualcomm/emac/emac-phy.c create mode 100644 drivers/net/ethernet/qualcomm/emac/emac-phy.h create mode 100644 drivers/net/ethernet/qualcomm/emac/emac-sgmii.c create mode 100644 drivers/net/ethernet/qualcomm/emac/emac-sgmii.h create mode 100644 drivers/net/ethernet/qualcomm/emac/emac.c create mode 100644 drivers/net/ethernet/qualcomm/emac/emac.h
David Miller
maintainerFrom: Timur Tabi <[email protected]>
Date: Fri, 24 Jun 2016 18:46:48 -0500
Quoted 2 lines
(quoted diff) +2
-0+ while (test_and_set_bit(EMAC_STATUS_RESETTING, &adpt->status)) + msleep(20); /* Reset might take few 10s of ms */
...
Review
Quoted 2 lines
(quoted diff) +2
-0+ while (test_and_set_bit(EMAC_STATUS_RESETTING, &adpt->status)) + msleep(20); /* Reset might take few 10s of ms */
You cannot spin in the kernel potentially forever if the tested condition is never met.
Instead you must have some limit for the loop, and signal a failure if it is reached.
drivers/net/ethernet/qualcomm/emac/emac.c line 659
Quoted 9 lines · CEST Timur Tabi
drivers/net/ethernet/qualcomm/emac/emac.c new file +8
-0+/* The EMAC itself is capable of 64-bit DMA. If the SOC limits that +* range, then we expect platform code to adjust the mask accordingly. +*/ + ret =dma_set_mask_and_coherent (&pdev->dev,DMA_BIT_MASK (64 )); +if (ret) { +dev_err (&pdev->dev,"could not set DMA mask\n" ); +return ret; + }
The comment does not match the code: if the platform has no IOMMU and the bus limit is smaller than the memory, dma_set_mask_and_coherent() will fail, and the driver should instead ensure that the buffers are allocated from the 32-bit area.
Alternatively, adjust the comment to explain that this is a limitation in the driver that can be lifted if necessary.
Arnd
Quoted 10 lines · Arnd Bergmann
Quoted 9 lines · CEST Timur Tabi
drivers/net/ethernet/qualcomm/emac/emac.c line 659
Quoted 9 lines
drivers/net/ethernet/qualcomm/emac/emac.c new file +8
-0+/* The EMAC itself is capable of 64-bit DMA. If the SOC limits that +* range, then we expect platform code to adjust the mask accordingly. +*/ + ret =dma_set_mask_and_coherent (&pdev->dev,DMA_BIT_MASK (64 )); +if (ret) { +dev_err (&pdev->dev,"could not set DMA mask\n" ); +return ret; + }
Quoted 7 lines
The comment does not match the code: if the platform has no IOMMU and the bus limit is smaller than the memory,
dma_set_mask_and_coherent()will fail, and the driver should instead ensure that the buffers are allocated from the 32-bit area.Alternatively, adjust the comment to explain that this is a limitation in the driver that can be lifted if necessary.
I'm not sure I understand. The EMAC hardware is capable of 64-bit DMA.
This is true on every platform -- the hardware registers that take bus
addresses are 64-bit. The driver itself has no limitations.
And that's what the dma_set_mask_and_coherent() does. It tells the kernel what the device is capable of.
However, on some SOCs, only a subset of those address lines are connected to the memory bus. So for instance, some platforms only have 32 bits connected.
There's no way for the EMAC driver to know this, so it expects other code in the kernel to adjust. I'm not exactly sure what this code is supposed to be, because I get conflicting information. At one point, I thought that the dma-ranges property would handle that. The kernel would parse that property, see that the DMA range is limited to 32 bits, and adjust the DMA mask accordingly. However, with dma-ranges in the parent node, I don't see how that can work.
So my question is, how do I handle the situation where a subset of the DMA address lines are masked off by the SOC? I've seen code like this:
ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)); if (ret)
ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
But this has never made any sense to me. If DMA_BIT_MASK(64) fails, then how can DMA_BIT_MASK(32) succeed?
Signature · 3 lines
Sent by an employee of the Qualcomm Innovation Center, Inc. The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, hosted by The Linux Foundation.
Quoted 19 lines · CEST Timur Tabi
Quoted 10 lines · Arnd Bergmann
Quoted 9 lines · CEST Timur Tabi
drivers/net/ethernet/qualcomm/emac/emac.c line 659
Quoted 9 lines
drivers/net/ethernet/qualcomm/emac/emac.c new file +8
-0+/* The EMAC itself is capable of 64-bit DMA. If the SOC limits that +* range, then we expect platform code to adjust the mask accordingly. +*/ + ret =dma_set_mask_and_coherent (&pdev->dev,DMA_BIT_MASK (64 )); +if (ret) { +dev_err (&pdev->dev,"could not set DMA mask\n" ); +return ret; + }Quoted 7 lines
The comment does not match the code: if the platform has no IOMMU and the bus limit is smaller than the memory,
dma_set_mask_and_coherent()will fail, and the driver should instead ensure that the buffers are allocated from the 32-bit area.Alternatively, adjust the comment to explain that this is a limitation in the driver that can be lifted if necessary.
Quoted 6 lines
I'm not sure I understand. The EMAC hardware is capable of 64-bit DMA.
This is true on every platform -- the hardware registers that take bus
addresses are 64-bit. The driver itself has no limitations.And that's what the
dma_set_mask_and_coherent()does. It tells the kernel what the device is capable of.
dma_set_mask_and_coherent() is a two-way interface, the driver says what it wants to do, and the platform reports on whether that is possible.
Quoted 11 lines
However, on some SOCs, only a subset of those address lines are connected to the memory bus. So for instance, some platforms only have 32 bits connected.
There's no way for the EMAC driver to know this, so it expects other code in the kernel to adjust. I'm not exactly sure what this code is supposed to be, because I get conflicting information. At one point, I thought that the dma-ranges property would handle that. The kernel would parse that property, see that the DMA range is limited to 32 bits, and adjust the DMA mask accordingly. However, with dma-ranges in the parent node, I don't see how that can work.
dma-ranges in fact is what should handle it, but arm64 currently does not interpret it correctly, and just allows the mask to be set regardless, which I consider a bug in the architecture specific code.
Quoted 9 lines
So my question is, how do I handle the situation where a subset of the DMA address lines are masked off by the SOC? I've seen code like this:
ret =
dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)); if (ret)ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));But this has never made any sense to me. If
DMA_BIT_MASK(64)fails, then how canDMA_BIT_MASK(32)succeed?
If the ranges property lists the bus as dma capable for only the lower 32 bits, then dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)); should fail, otherwise dma_alloc_coherent() will return an invalid memory area.
Another twist is how arm64 currently uses SWIOTLB unconditionally:
As long as SWIOTLB (or iommu) is enabled, dma_set_mask_and_coherent() should succeed for any mask(), but not actually update the mask of the device to more than the bus can handle.
Arnd
Quoted 4 lines · Arnd Bergmann
If the ranges property lists the bus as dma capable for only the lower 32 bits, then
dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)); should fail, otherwisedma_alloc_coherent()will return an invalid memory area.
That seems wrong. dma_alloc_coherent() should be smart enough to restrict itself to the the dma-ranges property. Isn't that why the property exists? When dma_alloc_coherent() looks for memory, it should knows it has to create a 32-bit address. That's why we have ZONE_DMA.
Quoted 4 lines
Another twist is how arm64 currently uses SWIOTLB unconditionally:
As long as SWIOTLB (or iommu) is enabled,dma_set_mask_and_coherent()should succeed for anymask(), but not actually update the mask of the device to more than the bus can handle.
That just seems like a bug in ARM64 SWIOTLB. SWIOTLB should inject itself when the driver tries to map memory outside of its DMA range.
In this case, SWIOTLB/IOMMU is handling the translation from low memory to high memory, eliminating the need to restrict memory access to a specific physical range.
Without SWIOTLB/IOMMU, dma_alloc_coherent() should be aware of the platform-specific limitations of each device and ensure that it only allocates memory that conforms *all* limitations. For example, if the platform is capable of 64-bit DMA, but a legacy device can only handle 32-bit bus addresses, then the driver should do this:
dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32))
If there's no SWIOTLB or IOMMU, then dma_alloc_coherent() should allocate only 32-bit addresses.
Signature · 3 lines
Sent by an employee of the Qualcomm Innovation Center, Inc. The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, hosted by The Linux Foundation.
Quoted 10 lines · CEST Timur Tabi
Quoted 4 lines · Arnd Bergmann
If the ranges property lists the bus as dma capable for only the lower 32 bits, then
dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)); should fail, otherwisedma_alloc_coherent()will return an invalid memory area.That seems wrong.
dma_alloc_coherent()should be smart enough to restrict itself to the the dma-ranges property. Isn't that why the property exists? Whendma_alloc_coherent()looks for memory, it should knows it has to create a 32-bit address. That's why we haveZONE_DMA.
No, dma_alloc_coherent() is documented to use the dma_mask as its reference, it's supposed to be independent of the underlying bus.
dma-ranges is just how we communicate the limitation of the bus to the kernel, but relying on dma-ranges itself would fail to consider drivers that impose additional limitations, i.e. when a device needs a smaller mask and calls 'dma_set_mask(dev, DMA_BIT_MASK(24))' or something like that.
Quoted 7 lines
Quoted 4 lines
Another twist is how arm64 currently uses SWIOTLB unconditionally:
As long as SWIOTLB (or iommu) is enabled,dma_set_mask_and_coherent()should succeed for anymask(), but not actually update the mask of the device to more than the bus can handle.That just seems like a bug in ARM64 SWIOTLB. SWIOTLB should inject itself when the driver tries to map memory outside of its DMA range.
Again, the dma mask is how swiotlb_map_*() finds whether a page needs a bounce buffer or not, so it has to be set to whatever the device can address.
Quoted 7 lines
Without SWIOTLB/IOMMU,
dma_alloc_coherent()should be aware of the platform-specific limitations of each device and ensure that it only allocates memory that conforms *all* limitations. For example, if the platform is capable of 64-bit DMA, but a legacy device can only handle 32-bit bus addresses, then the driver should do this:dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32))
That's also not how it works: each device starts out with a 32-bit mask, because that's what historically all PCI devices can do. If a device is 64-bit DMA capable, it can extend the mask by passing DMA_BIT_MASK(64) (or whatever it can support), and the platform code checks if that's possible.
Arnd
Quoted 5 lines · Arnd Bergmann
That's also not how it works: each device starts out with a 32-bit mask, because that's what historically all PCI devices can do. If a device is 64-bit DMA capable, it can extend the mask by passing
DMA_BIT_MASK(64)(or whatever it can support), and the platform code checks if that's possible.
So if it's not possible, then dma_set_mask returns an error, and the driver should try a smaller mask? Doesn't that mean that every driver for a 64-bit device should do this:
for (i = 64; i >=32; i--) {
ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(i));
if (!ret)
break;
}
if (ret)
return ret;Sure, this is overkill, but it seems to me that the driver does not really know what mask is actually valid, so it has to find the largest mask that works.
Signature · 3 lines
Sent by an employee of the Qualcomm Innovation Center, Inc. The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, hosted by The Linux Foundation.
Quoted 20 lines · CEST Timur Tabi
Quoted 5 lines · Arnd Bergmann
That's also not how it works: each device starts out with a 32-bit mask, because that's what historically all PCI devices can do. If a device is 64-bit DMA capable, it can extend the mask by passing
DMA_BIT_MASK(64)(or whatever it can support), and the platform code checks if that's possible.So if it's not possible, then
dma_set_maskreturns an error, and the driver should try a smaller mask? Doesn't that mean that every driver for a 64-bit device should do this:for (i = 64; i >=32; i--) { ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(i)); if (!ret) break; } if (ret) return ret;
Quoted 4 lines
Sure, this is overkill, but it seems to me that the driver does not really know what mask is actually valid, so it has to find the largest mask that works.
Usually drivers try 64-bit mask and 32-bit masks, and the 32 bit mask is practically guaranteed to succeed.
Platforms will also allow allow the driver to set a mask that is larger than what the bus supports, as long as all RAM is reachable by the bus.
Arnd
Quoted 2 lines · Arnd Bergmann
Usually drivers try 64-bit mask and 32-bit masks, and the 32 bit mask is practically guaranteed to succeed.
Sure, but in theory, my for-loop is correct, right? Wouldn't there be some value in setting a 36-bit or 40-bit DMA mask if it works? We have a platform where memory starts at a 40-bit address, so some devices have a 44-bit address bus. If a 64-bit mask doesn't work, then a 32-bit mask certainly wont.
Quoted 3 lines
Platforms will also allow allow the driver to set a mask that is larger than what the bus supports, as long as all RAM is reachable by the bus.
And that check (like all others) is made in the dma_set_mask call?
Signature · 3 lines
Qualcomm Innovation Center, Inc. The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation collaborative project.
Quoted 9 lines · CEST Timur Tabi
Quoted 2 lines · Arnd Bergmann
Usually drivers try 64-bit mask and 32-bit masks, and the 32 bit mask is practically guaranteed to succeed.
Sure, but in theory, my for-loop is correct, right? Wouldn't there be some value in setting a 36-bit or 40-bit DMA mask if it works? We have a platform where memory starts at a 40-bit address, so some devices have a 44-bit address bus. If a 64-bit mask doesn't work, then a 32-bit mask certainly wont.
The question is whether it makes any difference to the driver: what decision does the driver make differently if it has set a 44-bit mask?
We don't have ZONE_DMA44 (and it's very unlikely that we will introduce it), so neither the driver nor the network stack can actually act on the fact that a 64-bit mask failed but a 44-bit mask succeeded.
Quoted 5 lines
Quoted 3 lines
Platforms will also allow allow the driver to set a mask that is larger than what the bus supports, as long as all RAM is reachable by the bus.
And that check (like all others) is made in the
dma_set_maskcall?
Yes. Regarding the system you mentioned: I understand that it has no memory in ZONE_DMA32 and no IOMMU (btw, that also means 32-bit PCI devices are fundamentally broken), and the bus can only address the lower 44 bit of address space.
Does this system support more than 15TB of RAM? If not, then there is no problem, and if it does, I think your best way out is not to disable SWIOTLB. That will be a slower compared to a system with an IOMMU or the fictional ZONE_DMA44, but it should work correctly. In either case (less than 15TB without swiotlb, or more than 15TB with swiotlb), the driver should just ask for a 64-bit mask and that will succeed.
Arnd
Quoted 6 lines · Arnd Bergmann
Quoted 5 lines
Sure, but in theory, my for-loop is correct, right? Wouldn't there be some value in setting a 36-bit or 40-bit DMA mask if it works? We have a platform where memory starts at a 40-bit address, so some devices have a 44-bit address bus. If a 64-bit mask doesn't work, then a 32-bit mask certainly wont.
Quoted 6 lines
The question is whether it makes any difference to the driver: what decision does the driver make differently if it has set a 44-bit mask?
We don't have
ZONE_DMA44(and it's very unlikely that we will introduce it), so neither the driver nor the network stack can actually act on the fact that a 64-bit mask failed but a 44-bit mask succeeded.
A 44-bit mask would have access to much larger regions of memory than a 32-bit mask. Maybe one day we will have a version of dma_alloc_coherent that uses the DMA mask to specify an actual range of physical addresses to allocate from. In this situation, a mask of 44 would be much better than a mask of 32.
It just seems wrong for driver to hard-code 64-bit and 32-bit masks and pretend like those are the only two sizes. Very few 64-bit SOCs have a full 64-bit address bus. Most have actually around 36-48 bits. If dma_set_mask(64) fails on these systems, then every driver will fall back to 32 bits, even though they can technically access all of DDR.
Maybe we need a function like this:
int dma_find_best_mask(struct device *dev, unsigned int max)
{
struct dma_map_ops *ops = get_dma_ops(dev);
unsigned int mask;
int ret;
if (!ops || !ops->dma_supported)
return max;
for (mask = max; mask >= 32; mask--) {
ret = ops->dma_supported(dev, mask);
if (ret < 0)
return ret;
if (ret > 0)
return mask;
}
return -ENOMEM;
}You pass a mask size that you know is the maximum that the device could support (in my case, 64). It then returns a number that should be passed to dma_set_mask().
Quoted 10 lines
Quoted 5 lines
Quoted 3 lines
Platforms will also allow allow the driver to set a mask that is larger than what the bus supports, as long as all RAM is reachable by the bus.
And that check (like all others) is made in the
dma_set_maskcall?Yes. Regarding the system you mentioned: I understand that it has no memory in
ZONE_DMA32and no IOMMU (btw, that also means 32-bit PCI devices are fundamentally broken), and the bus can only address the lower 44 bit of address space.
Actually, we have an IOMMU, and the size of the address space depends on the SOC. Some SOCs have 32 bits, some have over 40. The EMAC is the same on all of these SOCs.
Quoted 7 lines
Does this system support more than 15TB of RAM? If not, then there is no problem, and if it does, I think your best way out is not to disable SWIOTLB. That will be a slower compared to a system with an IOMMU or the fictional
ZONE_DMA44, but it should work correctly. In either case (less than 15TB without swiotlb, or more than 15TB with swiotlb), the driver should just ask for a 64-bit mask and that will succeed.
Some SOCs can support over 15TB. They all have IOMMUs.
Apparently, dma_set_mask(64) won't always succeed. If the SOC has fewer DMA address lines than total memory, then a mask of 64 will fail.
I don't want to implement a solution that just happens to work on the EMAC. I'm trying to come up with a generic solution that can work in any driver on any SOC, whether you use device tree or ACPI.
Signature · 3 lines
Qualcomm Innovation Center, Inc. The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation collaborative project.
Quoted 21 lines · CEST Timur Tabi
Quoted 6 lines · Arnd Bergmann
Quoted 5 lines
Sure, but in theory, my for-loop is correct, right? Wouldn't there be some value in setting a 36-bit or 40-bit DMA mask if it works? We have a platform where memory starts at a 40-bit address, so some devices have a 44-bit address bus. If a 64-bit mask doesn't work, then a 32-bit mask certainly wont.
Quoted 6 lines
The question is whether it makes any difference to the driver: what decision does the driver make differently if it has set a 44-bit mask?
We don't have
ZONE_DMA44(and it's very unlikely that we will introduce it), so neither the driver nor the network stack can actually act on the fact that a 64-bit mask failed but a 44-bit mask succeeded.A 44-bit mask would have access to much larger regions of memory than a 32-bit mask. Maybe one day we will have a version of
dma_alloc_coherentthat uses the DMA mask to specify an actual range of physical addresses to allocate from. In this situation, a mask of 44 would be much better than a mask of 32.
Quoted 5 lines
It just seems wrong for driver to hard-code 64-bit and 32-bit masks and pretend like those are the only two sizes. Very few 64-bit SOCs have a full 64-bit address bus. Most have actually around 36-48 bits. If
dma_set_mask(64)fails on these systems, then every driver will fall back to 32 bits, even though they can technically access all of DDR.
To repeat myself: if you have swiotlb enabled, or you have an iommu or all of your RAM is reachable by the DMA master, then dma_set_mask() should succeed and do the right thing with setting the correct mask even if that is different from the one that the driver asked for.
The only case in which it fails is when the device is essentially unusable and needs to fall back to allocating from ZONE_DMA32 (in case of a mask larger than 32) or cannot allocate from any zone (in case of mask smaller or equal to 32).
Quoted 23 lines
Maybe we need a function like this:
int
dma_find_best_mask(struct device *dev, unsigned int max){ struct dma_map_ops *ops = get_dma_ops(dev); unsigned int mask; int ret; if (!ops || !ops->dma_supported) return max; for (mask = max; mask >= 32; mask--) { ret = ops->dma_supported(dev, mask); if (ret < 0) return ret; if (ret > 0) return mask; } return -ENOMEM; }
Quoted 3 lines
You pass a mask size that you know is the maximum that the device could support (in my case, 64). It then returns a number that should be passed to
dma_set_mask().
You still haven't been able to explain what the driver would do with that number other than to pass it into dma_set_mask().
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 3 lines
Platforms will also allow allow the driver to set a mask that is larger than what the bus supports, as long as all RAM is reachable by the bus.
And that check (like all others) is made in the
dma_set_maskcall?
Quoted 8 lines
Quoted 4 lines
Yes. Regarding the system you mentioned: I understand that it has no memory in
ZONE_DMA32and no IOMMU (btw, that also means 32-bit PCI devices are fundamentally broken), and the bus can only address the lower 44 bit of address space.Actually, we have an IOMMU, and the size of the address space depends on the SOC. Some SOCs have 32 bits, some have over 40. The EMAC is the same on all of these SOCs.
If there is an IOMMU, just use it.
Quoted 8 lines
Quoted 7 lines
Does this system support more than 15TB of RAM? If not, then there is no problem, and if it does, I think your best way out is not to disable SWIOTLB. That will be a slower compared to a system with an IOMMU or the fictional
ZONE_DMA44, but it should work correctly. In either case (less than 15TB without swiotlb, or more than 15TB with swiotlb), the driver should just ask for a 64-bit mask and that will succeed.
Quoted 4 lines
Some SOCs can support over 15TB. They all have IOMMUs.
Apparently,
dma_set_mask(64)won't always succeed. If the SOC has fewer DMA address lines than total memory, then a mask of 64 will fail.
Correct, unless you have SWIOTLB or IOMMU enabled.
Quoted 3 lines
I don't want to implement a solution that just happens to work on the EMAC. I'm trying to come up with a generic solution that can work in any driver on any SOC, whether you use device tree or ACPI.
As I said, this is inherently driver specific. If setting the 64-bit mask fails, the driver itself needs to fall back to the 32-bit mask so it can allocate buffers from ZONE_DMA instead of ZONE_NORMAL.
I have not been able to find out how network drivers normally handle this on 64-bit architectures. On 32-bit architectures, this is handled by calling __skb_linearize on TX SKB with high data, which will move the data into lowmem when the mask doesn't match and on the receive path the drivers just don't pass GFP_HIGHMEM.
My interpretation is that on 64-bit architectures, the network code simply assumes that either an SWIOTLB or IOMMU is always there, and lets that handle the copying instead of copying data to lowmem itself.
Arnd
Hi,
some remarks below.
Quoted 20 lines
drivers/net/ethernet/qualcomm/emac/emac-mac.c new file +20
-0+/* Fill up receive queue's RFD with preallocated receive buffers */ +static void emac_mac_rx_descs_refill (struct emac_adapter *adpt, +struct emac_rx_queue *rx_q) +{ +struct emac_buffer *curr_rxbuf; +struct emac_buffer *next_rxbuf; +unsigned int count =0 ; +u32 next_produce_idx; + + next_produce_idx = rx_q->rfd.produce_idx +1 ; +if (next_produce_idx == rx_q->rfd.count) + next_produce_idx =0 ; + + curr_rxbuf =GET_RFD_BUFFER (rx_q, rx_q->rfd.produce_idx); + next_rxbuf =GET_RFD_BUFFER (rx_q, next_produce_idx); + +/* this always has a blank rx_buffer*/ +while (!next_rxbuf->dma_addr) { +struct sk_buff *skb; +void *skb_data;
drivers/net/ethernet/qualcomm/emac/emac-mac.c line 960
Quoted 10 lines
drivers/net/ethernet/qualcomm/emac/emac-mac.c new file +10
-0+ + skb =dev_alloc_skb (adpt->rxbuf_size +NET_IP_ALIGN ); +if (!skb) +break ; + +/* Make buffer alignment 2 beyond a 16 byte boundary +* this will result in a 16 byte aligned IP header after +* the 14 byte MAC header is removed +*/ +skb_reserve (skb,NET_IP_ALIGN );
__netdev_alloc_skb_ip_align will do this for you.
drivers/net/ethernet/qualcomm/emac/emac-mac.c line 967
Quoted 7 lines
drivers/net/ethernet/qualcomm/emac/emac-mac.c new file +7
-0+ skb_data = skb->data; + curr_rxbuf->skb = skb; + curr_rxbuf->length = adpt->rxbuf_size; + curr_rxbuf->dma_addr =dma_map_single (adpt->netdev->dev.parent, + skb_data, + curr_rxbuf->length, +DMA_FROM_DEVICE );
Mapping can fail. You should check the result via dma_mapping_error(). There are several other places in which dma_map_single() is called and the return value is not checked.
Quoted 16 lines
drivers/net/ethernet/qualcomm/emac/emac-mac.c new file +16
-0+/* Bringup the interface/HW */ +int emac_mac_up (struct emac_adapter *adpt) +{ +struct net_device *netdev = adpt->netdev; +struct emac_irq *irq = &adpt->irq; +int ret; + +emac_mac_rx_tx_ring_reset_all (adpt); +emac_mac_config (adpt); + + ret =request_irq (irq->irq, emac_isr,0 ,EMAC_MAC_IRQ_RES , irq); +if (ret) { +netdev_err (adpt->netdev, +"error:%d on request_irq(%d:%s flags:0)\n" , ret, + irq->irq,EMAC_MAC_IRQ_RES ); +return ret;
drivers/net/ethernet/qualcomm/emac/emac-mac.c line 1024
Quoted 10 lines
drivers/net/ethernet/qualcomm/emac/emac-mac.c new file +10
-0+ } + +emac_mac_rx_descs_refill (adpt, &adpt->rx_q); + + ret =phy_connect_direct (netdev, adpt->phydev, emac_adjust_link, +PHY_INTERFACE_MODE_SGMII ); +if (ret) { +netdev_err (adpt->netdev, +"error:%d on request_irq(%d:%s flags:0)\n" , ret, + irq->irq,EMAC_MAC_IRQ_RES );
freeing the irq is missing
Quoted 11 lines
drivers/net/ethernet/qualcomm/emac/emac-mac.c new file +11
-0+ +/* Bring down the interface/HW */ +void emac_mac_down (struct emac_adapter *adpt,bool reset) +{ +struct net_device *netdev = adpt->netdev; +unsigned long flags; + +netif_stop_queue (netdev); +napi_disable (&adpt->rx_q.napi); + +phy_disconnect (adpt->phydev);
drivers/net/ethernet/qualcomm/emac/emac-mac.c line 1060
Quoted 10 lines
drivers/net/ethernet/qualcomm/emac/emac-mac.c new file +10
-0+ +/* disable mac irq */ +writel (DIS_INT , adpt->base +EMAC_INT_STATUS ); +writel (0 , adpt->base +EMAC_INT_MASK ); +synchronize_irq (adpt->irq.irq); +free_irq (adpt->irq.irq, &adpt->irq); +clear_bit (EMAC_STATUS_TASK_REINIT_REQ , &adpt->status); + +cancel_work_sync (&adpt->tx_ts_task); +spin_lock_irqsave (&adpt->tx_ts_lock, flags);
Maybe I am missing something but AFAICS tx_ts_lock is never called from irq context, so there is no reason to disable irqs.
Quoted 4 lines
drivers/net/ethernet/qualcomm/emac/emac-mac.c new file +4
-0+ +/* Push the received skb to upper layers */ +static void emac_receive_skb(struct emac_rx_queue *rx_q, + struct sk_buff *skb,
drivers/net/ethernet/qualcomm/emac/emac-mac.c line 1289
Quoted 10 lines
drivers/net/ethernet/qualcomm/emac/emac-mac.c new file +10
-0+ u16 vlan_tag, bool vlan_flag) +{ +if (vlan_flag) { +u16 vlan; + +EMAC_TAG_TO_VLAN (vlan_tag, vlan); +__vlan_hwaccel_put_tag (skb,htons (ETH_P_8021Q ), vlan); + } + +napi_gro_receive (&rx_q->napi, skb);
napi_gro_receive requires rx checksum offload. However emac_receive_skb() is also called if hardware checksumming is disabled.
drivers/net/ethernet/qualcomm/emac/emac-mac.c line 1626
Quoted 9 lines
drivers/net/ethernet/qualcomm/emac/emac-mac.c new file +9
-0+ +/* Transmit the packet using specified transmit queue */ +int emac_mac_tx_buf_send (struct emac_adapter *adpt,struct emac_tx_queue *tx_q, +struct sk_buff *skb) +{ +struct emac_tpd tpd; +u32 prod_idx; + +if (!emac_tx_has_enough_descs (tx_q, skb)) {
Drivers should avoid this situation right from the start by checking after each transmission if the max number
of possible descriptors is still available for a further transmission and stop the queue if there are not.
Furthermore there does not seem to be any function that wakes the queue up again once it has been stopped.
drivers/net/ethernet/qualcomm/emac/emac.c line 110
Quoted 11 lines
drivers/net/ethernet/qualcomm/emac/emac.c new file +11
-0+ +/* reinitialize */ +void emac_reinit_locked (struct emac_adapter *adpt) +{ +while (test_and_set_bit (EMAC_STATUS_RESETTING , &adpt->status)) +msleep (20 );/* Reset might take few 10s of ms */ + +emac_mac_down (adpt, true); + +emac_sgmii_reset (adpt); +emac_mac_up (adpt);
emac_mac_up() may fail, so this case should be handled properly.
Quoted 4 lines
drivers/net/ethernet/qualcomm/emac/emac.c new file +4
-0+/* Change the Maximum Transfer Unit (MTU) */ +static int emac_change_mtu (struct net_device *netdev,int new_mtu) +{ +unsigned int max_frame = new_mtu +ETH_HLEN +ETH_FCS_LEN +VLAN_HLEN ;
drivers/net/ethernet/qualcomm/emac/emac.c line 250
Quoted 10 lines
drivers/net/ethernet/qualcomm/emac/emac.c new file +10
-0+struct emac_adapter *adpt =netdev_priv (netdev); +unsigned int old_mtu = netdev->mtu; + +if ((max_frame <EMAC_MIN_ETH_FRAME_SIZE ) || + (max_frame >EMAC_MAX_ETH_FRAME_SIZE )) { +netdev_err (adpt->netdev,"error: invalid MTU setting\n" ); +return -EINVAL ; + } + +if ((old_mtu != new_mtu) &&netif_running (netdev)) {
Setting the new mtu in case that the interface is down is missing. Also the first check is not needed, since this function is only called if there is a change of the mtu.
drivers/net/ethernet/qualcomm/emac/emac.c line 335
Quoted 11 lines
drivers/net/ethernet/qualcomm/emac/emac.c new file +11
-0+/* Provide network statistics info for the interface */ +static struct rtnl_link_stats64 *emac_get_stats64 (struct net_device *netdev, +struct rtnl_link_stats64 *net_stats) +{ +struct emac_adapter *adpt =netdev_priv (netdev); +unsigned int addr =REG_MAC_RX_STATUS_BIN ; +struct emac_stats *stats = &adpt->stats; +u64 *stats_itr = &adpt->stats.rx_ok; +u32 val; + +mutex_lock (&stats->lock);
It is not allowed to sleep in this function, so you have to use something else for locking, e.g. a spinlock.
Quoted 85 lines
drivers/net/ethernet/qualcomm/emac/emac.c new file +85
-0+static int emac_probe (struct platform_device *pdev) +{ +struct net_device *netdev; +struct emac_adapter *adpt; +struct emac_phy *phy; +u16 devid, revid; +u32 reg; +int ret; + +/* The EMAC itself is capable of 64-bit DMA. If the SOC limits that +* range, then we expect platform code to adjust the mask accordingly. +*/ + ret =dma_set_mask_and_coherent (&pdev->dev,DMA_BIT_MASK (64 )); +if (ret) { +dev_err (&pdev->dev,"could not set DMA mask\n" ); +return ret; + } + + netdev =alloc_etherdev (sizeof (struct emac_adapter )); +if (!netdev) +return -ENOMEM ; + +dev_set_drvdata (&pdev->dev, netdev); +SET_NETDEV_DEV (netdev, &pdev->dev); + + adpt =netdev_priv (netdev); + adpt->netdev = netdev; + phy = &adpt->phy; + adpt->msg_enable =EMAC_MSG_DEFAULT ; + +mutex_init (&adpt->stats.lock); + + adpt->irq.mask =RX_PKT_INT0 |IMR_NORMAL_MASK ; + + ret =emac_probe_resources (pdev, adpt); +if (ret) + goto err_undo_netdev; + +/* initialize clocks */ + ret =emac_clks_phase1_init (pdev, adpt); +if (ret) + goto err_undo_resources; + + netdev->watchdog_timeo =EMAC_WATCHDOG_TIME ; + netdev->irq = adpt->irq.irq; + +if (adpt->timestamp_en) + adpt->rrd_size =EMAC_TS_RRD_SIZE ; +else + adpt->rrd_size =EMAC_RRD_SIZE ; + + adpt->tpd_size =EMAC_TPD_SIZE ; + adpt->rfd_size =EMAC_RFD_SIZE ; + +/* init netdev */ + netdev->netdev_ops = &emac_netdev_ops; + +/* init adapter */ +emac_init_adapter (adpt); + +/* init phy */ + ret =emac_phy_config (pdev, adpt); +if (ret) + goto err_undo_clk_phase1; + +/* enable clocks */ + ret =emac_clks_phase2_init (pdev, adpt); +if (ret) + goto err_undo_clk_phase2; + +/* reset mac */ +emac_mac_reset (adpt); + +/* set hw features */ + netdev->features =NETIF_F_SG |NETIF_F_HW_CSUM |NETIF_F_RXCSUM | +NETIF_F_TSO |NETIF_F_TSO6 |NETIF_F_HW_VLAN_CTAG_RX | +NETIF_F_HW_VLAN_CTAG_TX ; + netdev->hw_features = netdev->features; + + netdev->vlan_features |=NETIF_F_SG |NETIF_F_HW_CSUM | +NETIF_F_TSO |NETIF_F_TSO6 ; + +INIT_WORK (&adpt->work_thread, emac_work_thread); + +/* Initialize queues */
drivers/net/ethernet/qualcomm/emac/emac.c line 737
Quoted 10 lines
drivers/net/ethernet/qualcomm/emac/emac.c new file +10
-0+emac_mac_rx_tx_ring_init_all (pdev, adpt); + +netif_napi_add (netdev, &adpt->rx_q.napi, emac_napi_rtx,64 ); + +spin_lock_init (&adpt->tx_ts_lock); +skb_queue_head_init (&adpt->tx_ts_pending_queue); +skb_queue_head_init (&adpt->tx_ts_ready_queue); +INIT_WORK (&adpt->tx_ts_task, emac_mac_tx_ts_periodic_routine); + +strlcpy (netdev->name,"eth%d" ,sizeof (netdev->name));
This is already done by alloc_etherdev.
Regards,
Lino
Quoted 11 lines · Lino Sanfilippo
drivers/net/ethernet/qualcomm/emac/emac-mac.c line 960
Quoted 9 lines
drivers/net/ethernet/qualcomm/emac/emac-mac.c new file +9
-0+ skb =dev_alloc_skb (adpt->rxbuf_size +NET_IP_ALIGN ); +if (!skb) +break ; + +/* Make buffer alignment 2 beyond a 16 byte boundary +* this will result in a 16 byte aligned IP header after +* the 14 byte MAC header is removed +*/ +skb_reserve (skb,NET_IP_ALIGN );
__netdev_alloc_skb_ip_alignwill do this for you.
Will fix.
Quoted 9 lines
drivers/net/ethernet/qualcomm/emac/emac-mac.c line 967
Quoted 4 lines
drivers/net/ethernet/qualcomm/emac/emac-mac.c new file +4
-0+ curr_rxbuf->dma_addr =dma_map_single (adpt->netdev->dev.parent, + skb_data, + curr_rxbuf->length, +DMA_FROM_DEVICE );Mapping can fail. You should check the result via
dma_mapping_error(). There are several other places in whichdma_map_single()is called and the return value is not checked.
Will fix.
Quoted 6 lines
drivers/net/ethernet/qualcomm/emac/emac-mac.c line 1013
Quoted 4 lines
drivers/net/ethernet/qualcomm/emac/emac-mac.c new file +4
-0+if (ret) { +netdev_err (adpt->netdev, +"error:%d on request_irq(%d:%s flags:0)\n" , ret, + irq->irq,EMAC_MAC_IRQ_RES );freeing the irq is missing
Will fix.
Quoted 4 lines
Quoted 4 lines
drivers/net/ethernet/qualcomm/emac/emac-mac.c new file +4
-0+/* disable mac irq */ +writel (DIS_INT , adpt->base +EMAC_INT_STATUS ); +writel (0 , adpt->base +EMAC_INT_MASK ); +synchronize_irq (adpt->irq.irq);
Quoted 8 lines
drivers/net/ethernet/qualcomm/emac/emac-mac.c line 1060
Quoted 5 lines
drivers/net/ethernet/qualcomm/emac/emac-mac.c new file +5
-0+free_irq (adpt->irq.irq, &adpt->irq); +clear_bit (EMAC_STATUS_TASK_REINIT_REQ , &adpt->status); + +cancel_work_sync (&adpt->tx_ts_task); +spin_lock_irqsave (&adpt->tx_ts_lock, flags);Maybe I am missing something but AFAICS
tx_ts_lockis never called from irq context, so there is no reason to disable irqs.
It might have been that way in an older version of the code, but it appears you are correct. I will change it to a normal spinlock. Thanks.
Quoted 8 lines
Quoted 8 lines
drivers/net/ethernet/qualcomm/emac/emac-mac.c new file +8
-0+/* Push the received skb to upper layers */ +static void emac_receive_skb (struct emac_rx_queue *rx_q, +struct sk_buff *skb, +u16 vlan_tag,bool vlan_flag) +{ +if (vlan_flag) { +u16 vlan; +
Quoted 8 lines
drivers/net/ethernet/qualcomm/emac/emac-mac.c line 1289
Quoted 5 lines
drivers/net/ethernet/qualcomm/emac/emac-mac.c new file +5
-0+EMAC_TAG_TO_VLAN (vlan_tag, vlan); +__vlan_hwaccel_put_tag (skb,htons (ETH_P_8021Q ), vlan); + } + +napi_gro_receive (&rx_q->napi, skb);
napi_gro_receiverequires rx checksum offload. Howeveremac_receive_skb()is also called if hardware checksumming is disabled.
So the hardware is a little weird here. Apparently, there is a bug in the parsing of the packet headers that is avoided if we disable hardware checksumming.
In emac_mac_rx_process(), right before it calls emac_receive_skb(), it does this:
if (netdev->features & NETIF_F_RXCSUM)
skb->ip_summed = RRD_L4F(&rrd) ? CHECKSUM_NONE : CHECKSUM_UNNECESSARY; else skb_checksum_none_assert(skb);
RRD_L4F(&rrd) is always zero and NETIF_F_RXCSUM is set by default, so ip_summed is set to CHECKSUM_UNNECESSARY.
So you're saying that if NETIF_F_RXCSUM is not set, then napi_gro_receive() should not be called?
I see examples of other drivers that *appear* to call napi_gro_receive() even when hardware checksumming is disabled.
For example, bfin_mac_rx() in adi/bfin_mac.c does this:
/*
* Disable hardware checksum for bug #5600 if writeback cache is
* enabled. Otherwize, corrupted RX packet will be sent up stack
* without error mark.
*/
#ifndef CONFIG_BFIN_EXTMEM_WRITEBACK
#define BFIN_MAC_CSUM_OFFLOAD
#endif
...
#if defined(BFIN_MAC_CSUM_OFFLOAD)
...
#endif napi_gro_receive(&lp->napi, skb);
Shouldn't the call to napi_gro_receive() be before the #endif?
Function i40e_receive_skb() has similar code to my driver.
In fact, I have not been able to find any clear example of a driver that intentionally avoids calling napi_gro_receive() if hardware checksumming is disabled.
Quoted 11 lines
drivers/net/ethernet/qualcomm/emac/emac-mac.c line 1626
Quoted 8 lines
drivers/net/ethernet/qualcomm/emac/emac-mac.c new file +8
-0+/* Transmit the packet using specified transmit queue */ +int emac_mac_tx_buf_send (struct emac_adapter *adpt,struct emac_tx_queue *tx_q, +struct sk_buff *skb) +{ +struct emac_tpd tpd; +u32 prod_idx; + +if (!emac_tx_has_enough_descs (tx_q, skb)) {Drivers should avoid this situation right from the start by checking after each transmission if the max number
of possible descriptors is still available for a further transmission and stop the queue if there are not.
Ok, to be clear, you're saying I should do what bcmgenet_xmit() does.
if (ring->free_bds <= (MAX_SKB_FRAGS + 1)) netif_tx_stop_queue(txq);
At the end of emac_mac_tx_buf_send(), I should call emac_tpd_num_free_descs() and check to see whether the number of free descriptors is <= (MAX_SKB_FRAGS + 1).
Quoted 1 line
Furthermore there does not seem to be any function that wakes the queue up again once it has been stopped.
If I make the above fix, won't that also fix this bug?
Quoted 4 lines
Quoted 4 lines
drivers/net/ethernet/qualcomm/emac/emac.c new file +4
-0+/* reinitialize */ +void emac_reinit_locked (struct emac_adapter *adpt) +{ +while (test_and_set_bit (EMAC_STATUS_RESETTING , &adpt->status))
Quoted 8 lines
drivers/net/ethernet/qualcomm/emac/emac.c line 110
Quoted 6 lines
drivers/net/ethernet/qualcomm/emac/emac.c new file +6
-0+msleep (20 );/* Reset might take few 10s of ms */ + +emac_mac_down (adpt, true); + +emac_sgmii_reset (adpt); +emac_mac_up (adpt);
emac_mac_up()may fail, so this case should be handled properly.
Ok.
Quoted 17 lines
Quoted 5 lines
drivers/net/ethernet/qualcomm/emac/emac.c new file +4
-0+/* Change the Maximum Transfer Unit (MTU) */ +static int emac_change_mtu (struct net_device *netdev,int new_mtu) +{ +unsigned int max_frame = new_mtu +ETH_HLEN +ETH_FCS_LEN +VLAN_HLEN ;drivers/net/ethernet/qualcomm/emac/emac.c line 250
Quoted 10 lines
drivers/net/ethernet/qualcomm/emac/emac.c new file +10
-0+struct emac_adapter *adpt =netdev_priv (netdev); +unsigned int old_mtu = netdev->mtu; + +if ((max_frame <EMAC_MIN_ETH_FRAME_SIZE ) || + (max_frame >EMAC_MAX_ETH_FRAME_SIZE )) { +netdev_err (adpt->netdev,"error: invalid MTU setting\n" ); +return -EINVAL ; + } + +if ((old_mtu != new_mtu) &&netif_running (netdev)) {Setting the new mtu in case that the interface is down is missing.
Should I just move the "netdev->mtu = new_mtu" line outside of the if-statement?
Quoted 2 lines
Also the first check is not needed, since this function is only called if there is a change of the mtu.
Will fix.
Quoted 6 lines
Quoted 6 lines
drivers/net/ethernet/qualcomm/emac/emac.c new file +6
-0+/* Provide network statistics info for the interface */ +static struct rtnl_link_stats64 *emac_get_stats64 (struct net_device *netdev, +struct rtnl_link_stats64 *net_stats) +{ +struct emac_adapter *adpt =netdev_priv (netdev); +unsigned int addr =REG_MAC_RX_STATUS_BIN ;
Quoted 8 lines
drivers/net/ethernet/qualcomm/emac/emac.c line 335
Quoted 5 lines
drivers/net/ethernet/qualcomm/emac/emac.c new file +5
-0+struct emac_stats *stats = &adpt->stats; +u64 *stats_itr = &adpt->stats.rx_ok; +u32 val; + +mutex_lock (&stats->lock);It is not allowed to sleep in this function, so you have to use something else for locking, e.g. a spinlock.
Will fix.
Quoted 3 lines
drivers/net/ethernet/qualcomm/emac/emac.c line 737
Quoted 1 line
drivers/net/ethernet/qualcomm/emac/emac.c new file +1
-0+strlcpy (netdev->name,"eth%d" ,sizeof (netdev->name));This is already done by
alloc_etherdev.
Will fix.
Thank you very much for reviewing my code. I hope my questions haven't been too stupid.
Signature · 3 lines
Qualcomm Datacenter Technologies, Inc. as an affiliate of Qualcomm Technologies, Inc. Qualcomm Technologies, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project.
Quoted 23 lines · Timur Tabi
Quoted 7 lines
drivers/net/ethernet/qualcomm/emac/emac-mac.c line 1013
Quoted 5 lines
drivers/net/ethernet/qualcomm/emac/emac-mac.c new file +4
-0+if (ret) { +netdev_err (adpt->netdev, +"error:%d on request_irq(%d:%s flags:0)\n" , ret, + irq->irq,EMAC_MAC_IRQ_RES );freeing the irq is missing
Will fix.
Quoted 4 lines
Quoted 4 lines
drivers/net/ethernet/qualcomm/emac/emac-mac.c new file +4
-0+/* disable mac irq */ +writel (DIS_INT , adpt->base +EMAC_INT_STATUS ); +writel (0 , adpt->base +EMAC_INT_MASK ); +synchronize_irq (adpt->irq.irq);Quoted 8 lines
drivers/net/ethernet/qualcomm/emac/emac-mac.c line 1060
Quoted 5 lines
drivers/net/ethernet/qualcomm/emac/emac-mac.c new file +5
-0+free_irq (adpt->irq.irq, &adpt->irq); +clear_bit (EMAC_STATUS_TASK_REINIT_REQ , &adpt->status); + +cancel_work_sync (&adpt->tx_ts_task); +spin_lock_irqsave (&adpt->tx_ts_lock, flags);Maybe I am missing something but AFAICS
tx_ts_lockis never called from irq context, so there is no reason to disable irqs.
Quoted 2 lines
It might have been that way in an older version of the code, but it appears you are correct. I will change it to a normal spinlock. Thanks.
By looking closer to the code, the lock seems to serve the protection of a list of skbs that are queued to be timestamped. However there is nothing that ever enqueues those skbs, is it? I assume that this is a leftover of a previous version of that driver. Code to be merged into mailine has to be completely cleaned up and must not contains any functions which are never called. So either you implement the timestamping feature completely or you remove the concerning code altogether for the initial mainline driver version. You can add that feature any time later.
Quoted 31 lines
Quoted 17 lines
Quoted 4 lines
drivers/net/ethernet/qualcomm/emac/emac-mac.c new file +3
-0+/* Push the received skb to upper layers */ +static void emac_receive_skb(struct emac_rx_queue *rx_q, + struct sk_buff *skb,drivers/net/ethernet/qualcomm/emac/emac-mac.c line 1289
Quoted 10 lines
drivers/net/ethernet/qualcomm/emac/emac-mac.c new file +10
-0+ u16 vlan_tag, bool vlan_flag) +{ +if (vlan_flag) { +u16 vlan; + +EMAC_TAG_TO_VLAN (vlan_tag, vlan); +__vlan_hwaccel_put_tag (skb,htons (ETH_P_8021Q ), vlan); + } + +napi_gro_receive (&rx_q->napi, skb);
napi_gro_receiverequires rx checksum offload. Howeveremac_receive_skb()is also called if hardware checksumming is disabled.So the hardware is a little weird here. Apparently, there is a bug in the parsing of the packet headers that is avoided if we disable hardware checksumming.
In
emac_mac_rx_process(), right before it callsemac_receive_skb(), it does this:if (
netdev->features&NETIF_F_RXCSUM)skb->ip_summed = RRD_L4F(&rrd) ? CHECKSUM_NONE : CHECKSUM_UNNECESSARY; else skb_checksum_none_assert(skb);
Quoted 5 lines
RRD_L4F(&rrd)is always zero andNETIF_F_RXCSUMis set by default, soip_summedis set toCHECKSUM_UNNECESSARY.So you're saying that if
NETIF_F_RXCSUMis not set, thennapi_gro_receive()should not be called?
This requirement seems indeed to be obsolete now so you can ignore my former complaint and leave it as it is.
Quoted 3 lines
In fact, I have not been able to find any clear example of a driver that intentionally avoids calling
napi_gro_receive()if hardware checksumming is disabled.
Some drivers still make the differentiation:
http://lxr.free-electrons.com/source/drivers/net/ethernet/marvell/sky2.c#L2656
http://lxr.free-electrons.com/source/drivers/net/ethernet/samsung/sxgbe/sxgbe_main.c#L1548
and several more.
Quoted 12 lines
Quoted 11 lines
drivers/net/ethernet/qualcomm/emac/emac-mac.c line 1626
Quoted 8 lines
drivers/net/ethernet/qualcomm/emac/emac-mac.c new file +8
-0+/* Transmit the packet using specified transmit queue */ +int emac_mac_tx_buf_send (struct emac_adapter *adpt,struct emac_tx_queue *tx_q, +struct sk_buff *skb) +{ +struct emac_tpd tpd; +u32 prod_idx; + +if (!emac_tx_has_enough_descs (tx_q, skb)) {Drivers should avoid this situation right from the start by checking after each transmission if the max number
of possible descriptors is still available for a further transmission and stop the queue if there are not.
Quoted 8 lines
Ok, to be clear, you're saying I should do what
bcmgenet_xmit()does.if (ring->free_bds <= (MAX_SKB_FRAGS + 1)) netif_tx_stop_queue(txq);At the end of
emac_mac_tx_buf_send(), I should callemac_tpd_num_free_descs()and check to see whether the number of free descriptors is <= (MAX_SKB_FRAGS+ 1).
Right. The current implemented approach to check the number of descs at the beginning instead of at the end of a transmission is not wrong but it leads to one extra call of xmit if the driver is running out of descs. So most drivers avoid this by stopping the queue as soon as they detect that the max required number of descs for a further transmission is not available.
Quoted 4 lines
Quoted 2 lines
Furthermore there does not seem to be any function that wakes the queue up again once it has been stopped.
If I make the above fix, won't that also fix this bug?
No, how should it? There still is nothing that wakes up the queue once it is stopped. Stopping and restarting/waking up a queue is up to the driver, since the network stack cant know if the hw is ready to queue another transmission or not. Usually the queue is stopped in the xmit function as soon as there are not enough descs left. Stopping the queue tells the network stack not to call the xmit function any more. When there are enough descs available again the driver has to wake up the queue. This is normally done in the tx completion handler (emac_mac_tx_process() in your case) as soon as enough free list elements are available again. Take a look at other drivers and when they call netif_wake_queue (or one of its variants).
Quoted 19 lines
Quoted 18 lines
Quoted 6 lines
drivers/net/ethernet/qualcomm/emac/emac.c new file +4
-0+/* Change the Maximum Transfer Unit (MTU) */ +static int emac_change_mtu (struct net_device *netdev,int new_mtu) +{ +unsigned int max_frame = new_mtu +ETH_HLEN +ETH_FCS_LEN +VLAN_HLEN ;drivers/net/ethernet/qualcomm/emac/emac.c line 250
Quoted 10 lines
drivers/net/ethernet/qualcomm/emac/emac.c new file +10
-0+struct emac_adapter *adpt =netdev_priv (netdev); +unsigned int old_mtu = netdev->mtu; + +if ((max_frame <EMAC_MIN_ETH_FRAME_SIZE ) || + (max_frame >EMAC_MAX_ETH_FRAME_SIZE )) { +netdev_err (adpt->netdev,"error: invalid MTU setting\n" ); +return -EINVAL ; + } + +if ((old_mtu != new_mtu) &&netif_running (netdev)) {Setting the new mtu in case that the interface is down is missing.
Quoted 2 lines
Should I just move the "
netdev->mtu=new_mtu" line outside of the if-statement?
You can do that, but take care to ajdust dpt->rxbuf_size to the correct value as soon as the interface is brought up. (The same applies to the initialization of the mac with the new mtu value of course).
Regards,
Lino
Quoted 6 lines · Lino Sanfilippo
By looking closer to the code, the lock seems to serve the protection of a list of skbs that are queued to be timestamped. However there is nothing that ever enqueues those skbs, is it? I assume that this is a leftover of a previous version of that driver. Code to be merged into mailine has to be completely cleaned up and must not contains any functions which are never called. So either you implement the timestamping feature completely or you remove the concerning code altogether for the initial mainline driver version. You can add that feature any time later.
I will remove it. It's not enabled by default on my platform anyway. I didn't realize it wasn't properly implemented.
Quoted 5 lines
Quoted 2 lines
So you're saying that if
NETIF_F_RXCSUMis not set, thennapi_gro_receive()should not be called?This requirement seems indeed to be obsolete now so you can ignore my former complaint and leave it as it is.
Ok.
Quoted 8 lines
No, how should it? There still is nothing that wakes up the queue once it is stopped. Stopping and restarting/waking up a queue is up to the driver, since the network stack cant know if the hw is ready to queue another transmission or not. Usually the queue is stopped in the xmit function as soon as there are not enough descs left. Stopping the queue tells the network stack not to call the xmit function any more. When there are enough descs available again the driver has to wake up the queue. This is normally done in the tx completion handler (
emac_mac_tx_process()in your case) as soon as enough free list elements are available again. Take a look at other drivers and when they callnetif_wake_queue(or one of its variants).
Something must have gotten deleted by accident. The internal version of the driver has this:
if (netif_queue_stopped(adpt->netdev) &&
netif_carrier_ok(adpt->netdev) &&
(emac_get_num_free_tpdescs(txque) >= (txque->tpd.count / 8)))
netif_wake_queue(adpt->netdev);My version replaces this with:
netdev_completed_queue(adpt->netdev, pkts_compl, bytes_compl);
I don't know why this change was made. However, if I comment out this line, the transmit queue times out, so obviously it's necessary.
I notice that *some* drivers, follow that with some variant of:
if (netif_queue_stopped(bgmac->net_dev)) netif_wake_queue(bgmac->net_dev);
Is there a good way to test my code? ping and iperf appear to send no more than 3 packets at a time, which comes nowhere close to filling the queue (which holds 512 normally). netif_queue_stopped() never returns true, no matter what I do.
Quoted 6 lines
Quoted 2 lines
Should I just move the "
netdev->mtu=new_mtu" line outside of the if-statement?You can do that, but take care to ajdust
dpt->rxbuf_sizeto the correct value as soon as the interface is brought up. (The same applies to the initialization of the mac with the new mtu value of course).
Is there any reason this doesn't work:
netdev->mtu = new_mtu; adpt->rxbuf_size = new_mtu > EMAC_DEF_RX_BUF_SIZE ? ALIGN(max_frame, 8) : EMAC_DEF_RX_BUF_SIZE; if (netif_running(netdev)) return emac_reinit_locked(adpt);
I set rxbuf_size regardless. If the interface is up, it will reinitialize and load the new value. If the interface is down, rxbuf_size will be ignored until emac_mac_up() is called.
Signature · 3 lines
Qualcomm Datacenter Technologies, Inc. as an affiliate of Qualcomm Technologies, Inc. Qualcomm Technologies, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project.
Quoted 5 lines · Timur Tabi
Is there a good way to test my code? ping and iperf appear to send no more than 3 packets at a time, which comes nowhere close to filling the queue (which holds 512 normally).
netif_queue_stopped()never returns true, no matter what I do.
Never mind, I fixed this problem. I had a race condition between
/* update produce idx */ prod_idx = (tx_q->tpd.produce_idx << tx_q->produce_shift) & tx_q->produce_mask; emac_reg_update32(adpt->base + tx_q->produce_reg, tx_q->produce_mask, prod_idx);
and the call to netif_stop_queue(). The emac_reg_update32() signalled to the hardware that data is available, and it immediately called the ISR, before I had a chance to call netif_stop_queue(). So now I call emac_reg_update32() after netif_stop_queue(), and everything works.
I'll post a v7 soon.
Signature · 3 lines
Qualcomm Datacenter Technologies, Inc. as an affiliate of Qualcomm Technologies, Inc. Qualcomm Technologies, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project.
Quoted 3 lines · Arnd Bergmann
As I said, this is inherently driver specific. If setting the 64-bit mask fails, the driver itself needs to fall back to the 32-bit mask so it can allocate buffers from
ZONE_DMAinstead ofZONE_NORMAL.
I just posted a v7 of my patch, but I forgot to fix the dma_set_mask call. I'll post a v8 soon, but before I do, what do you think of this:
/* The EMAC itself is capable of 64-bit DMA, so try that first. */ ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
if (ret) {
/* Some platforms may restrict the EMAC's address bus to less
* then the size of DDR. In this case, we need to try a
* smaller mask. We could try every possible smaller mask,
* but that's overkill. Instead, just fall to 32-bit, which
* should always work.
*/
ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
if (ret) {
dev_err(&pdev->dev, "could not set DMA mask\n");
return ret;
}
}Signature · 3 lines
Qualcomm Datacenter Technologies, Inc. as an affiliate of Qualcomm Technologies, Inc. Qualcomm Technologies, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project.
Quoted 10 lines · CEST Timur Tabi
Quoted 3 lines · Arnd Bergmann
As I said, this is inherently driver specific. If setting the 64-bit mask fails, the driver itself needs to fall back to the 32-bit mask so it can allocate buffers from
ZONE_DMAinstead ofZONE_NORMAL.I just posted a v7 of my patch, but I forgot to fix the
dma_set_maskcall. I'll post a v8 soon, but before I do, what do you think of this:/* The EMAC itself is capable of 64-bit DMA, so try that first. */ ret =
dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Quoted 13 lines
if (ret) {
/* Some platforms may restrict the EMAC's address bus to less
* then the size of DDR. In this case, we need to try a
* smaller mask. We could try every possible smaller mask,
* but that's overkill. Instead, just fall to 32-bit, which
* should always work.
*/
ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
if (ret) {
dev_err(&pdev->dev, "could not set DMA mask\n");
return ret;
}
}This is basically ok, but then I think you should pass GFP_DMA or GFP_DMA32 to all allocations that the driver does after the 64-bit mask fails, otherwise you get a significant overhead in the bounce buffers.
For data that comes from the network stack, we have no choice and always use bounce buffers for data that is beyond the mask.
Arnd
Quoted 4 lines · Arnd Bergmann
This is basically ok, but then I think you should pass
GFP_DMAorGFP_DMA32to all allocations that the driver does after the 64-bit mask fails, otherwise you get a significant overhead in the bounce buffers.
Well, for starters, ZONE_DMA32 is the same as ZONE_NORMAL on ARM, because CONFIG_ZONE_DMA32 is not defined.
#ifdef CONFIG_ZONE_DMA32
#define OPT_ZONE_DMA32 ZONE_DMA32
#else
#define OPT_ZONE_DMA32 ZONE_NORMAL
#endif
(I wonder if this should say instead:
#ifdef CONFIG_ZONE_DMA32
#define OPT_ZONE_DMA32 ZONE_DMA32
#else
#define OPT_ZONE_DMA32 ZONE_DMA <----
#endif
)
However, I'm not sure where I should be using GFP_DMA anyway. Whenever the driver allocates memory for DMA, it uses dma_zalloc_coherent():
ring_header->v_addr = dma_zalloc_coherent(dev, ring_header->size,
&ring_header->dma_addr, GFP_KERNEL);
and I don't think I need to pass GFP_DMA to dma_zalloc_coherent. Every other memory allocation is a kmalloc variant, but that's never for DMA, so that memory can be anywhere.
I found about 70 drivers that fall-back to 32-bit DMA if 64-bit fails. None of them do as you suggest. They all just set the mask to 64 or 32 and that's it.
Some drivers set NETIF_F_HIGHDMA if 64-bit DMA is enabled:
if (pci_using_dac) netdev->features |= NETIF_F_HIGHDMA;
I could do this, but I think it has no meaning on ARM64 because it depends on CONFIG_HIGHMEM.
Signature · 3 lines
Qualcomm Datacenter Technologies, Inc. as an affiliate of Qualcomm Technologies, Inc. Qualcomm Technologies, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project.