Add DWMAC glue driver for Motorcomm YT6801
Subsystems (7)
v3 cover Add DWMAC glue driver for Motorcomm YT6801
This series adds glue driver for Motorcomm YT6801 PCIe ethernet controller, which is considered mostly compatible with DWMAC-4 IP by inspecting the register layout[1]. It integrates a Motorcomm YT8531S PHY (confirmed by reading PHY ID) and GMII is used to connect the PHY to MAC[2].
The initialization logic of the MAC is mostly based on previous upstream effort for the controller[3] and the Deepin-maintained downstream Linux driver[4] licensed under GPL-2.0 according to its SPDX headers. However, this series is a completely re-write of the previous patch series, utilizing the existing DWMAC4 driver and introducing a glue driver only.
This series only aims to add basic networking functions for the controller, features like WoL, RSS and LED control are omitted for now. Testing is done on Loongson 3A5000 machine. Through a local GbE switch, it reaches 869Mbps (TX)/943Mbps (RX) on average,
## YT6801 TX
Connecting to host 172.16.70.12, port 5201
[ 5] local 172.16.70.230 port 43802 connected to 172.16.70.12 port 5201
[ ID] Interval Transfer Bitrate Retr Cwnd
[ 5] 0.00-1.00 sec 102 MBytes 858 Mbits/sec 0 335 KBytes [ 5] 1.00-2.00 sec 103 MBytes 867 Mbits/sec 0 441 KBytes [ 5] 2.00-3.00 sec 103 MBytes 863 Mbits/sec 0 441 KBytes [ 5] 3.00-4.00 sec 104 MBytes 870 Mbits/sec 0 441 KBytes [ 5] 4.00-5.00 sec 104 MBytes 869 Mbits/sec 0 441 KBytes [ 5] 5.00-6.00 sec 104 MBytes 869 Mbits/sec 0 591 KBytes [ 5] 6.00-7.00 sec 104 MBytes 876 Mbits/sec 0 629 KBytes [ 5] 7.00-8.00 sec 105 MBytes 878 Mbits/sec 0 629 KBytes [ 5] 8.00-9.00 sec 104 MBytes 872 Mbits/sec 0 629 KBytes [ 5] 9.00-10.00 sec 104 MBytes 871 Mbits/sec 0 629 KBytes
## YT6801 RX
Connecting to host 172.16.70.230, port 5201
[ 5] local 172.16.70.12 port 60866 connected to 172.16.70.230 port 5201
[ ID] Interval Transfer Bitrate Retr Cwnd
[ 5] 0.00-1.00 sec 113 MBytes 949 Mbits/sec 0 352 KBytes [ 5] 1.00-2.00 sec 113 MBytes 945 Mbits/sec 0 352 KBytes [ 5] 2.00-3.00 sec 112 MBytes 938 Mbits/sec 0 352 KBytes [ 5] 3.00-4.00 sec 113 MBytes 945 Mbits/sec 0 352 KBytes [ 5] 4.00-5.00 sec 112 MBytes 938 Mbits/sec 0 407 KBytes [ 5] 5.00-6.00 sec 112 MBytes 940 Mbits/sec 0 407 KBytes [ 5] 6.00-7.00 sec 113 MBytes 945 Mbits/sec 0 436 KBytes [ 5] 7.00-8.00 sec 112 MBytes 941 Mbits/sec 0 436 KBytes [ 5] 8.00-9.00 sec 113 MBytes 947 Mbits/sec 0 645 KBytes [ 5] 9.00-10.00 sec 112 MBytes 939 Mbits/sec 0 645 KBytes
This series depends on v5 of series "Unify platform suspend/resume routines for PCI DWMAC glue"[5] for a clean apply. It has been some time since I sent v1 of the series, I'm sorry for the delay. Many thanks for your time and review.
[1]: https://lore.kernel.org/all/[email protected]/
[2]: https://lore.kernel.org/all/[email protected]/
[3]: https://lore.kernel.org/all/[email protected]/
[4]: https://github.com/deepin-community/kernel/tree/dc61248a0e21/drivers/net/ethernet/motorcomm/yt6801
[5]: https://lore.kernel.org/netdev/[email protected]/
Changed from v2
- Rebase on top of next-20251124
- Switch to stmmac_plat_dat_alloc() then drop now redundant parameters from motorcomm_default_plat_data()
- Set STMMAC_FLAG_EN_TX_LPI_CLK_PHY_CAP
- Add a comment indicating the possible source of CSR clock
- Link to v2: https://lore.kernel.org/netdev/[email protected]/
Changed from v1
- Drop (original) PATCH 1, add no vendor ID entry to linux/pci_ids.h
- Use PHY_INTERFACE_MODE_GMII instead of PHY_INTERFACE_MODE_INTERNAL
- Drop extra register read in motorcomm_efuse_read_byte()
- Rename EPHY_RESET to EPHY_MDIO_PHY_RESET, add a comment to reflect its function better
- Use the newly-introduced generic PCI suspend/resume routines
- Generate a random MAC address instead of failing to probe when no MAC address is programmed in eFuse (seen on some OEM EVBs).
- Collect Tested-by tags
- Link to v1: https://lore.kernel.org/netdev/[email protected]/
Yao Zi (3):
net: phy: motorcomm: Support YT8531S PHY in YT6801 Ethernet controller
net: stmmac: Add glue driver for Motorcomm YT6801 ethernet controller
MAINTAINERS: Assign myself as maintainer of Motorcomm DWMAC glue
driver
MAINTAINERS | 6 + drivers/net/ethernet/stmicro/stmmac/Kconfig | 9 + drivers/net/ethernet/stmicro/stmmac/Makefile | 1 + .../ethernet/stmicro/stmmac/dwmac-motorcomm.c | 378 ++++++++++++++++++ drivers/net/phy/motorcomm.c | 4 + 5 files changed, 398 insertions(+) create mode 100644 drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c
Signature · 1 line
2.51.2
PATCH v3 1/3 net: phy: motorcomm: Support YT8531S PHY in YT6801 Ethernet controller
YT6801's internal PHY is confirmed as a GMII-capable variant of YT8531S by a previous series[1] and reading PHY ID. Add support for PHY_INTERFACE_MODE_GMII for YT8531S to allow the Ethernet driver to reuse the PHY code for its internal PHY.
--- drivers/net/phy/motorcomm.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/net/phy/motorcomm.c +4 -0
@@ -910,6 +910,10 @@ static int ytphy_rgmii_clk_delay_config(struct phy_device *phydev) val |=FIELD_PREP (YT8521_RC1R_RX_DELAY_MASK , rx_reg) |FIELD_PREP (YT8521_RC1R_GE_TX_DELAY_MASK , tx_reg);break ; +case PHY_INTERFACE_MODE_GMII : +if (phydev->drv->phy_id !=PHY_ID_YT8531S ) +return -EOPNOTSUPP ; +break ;default :/* do not support other modes */ return -EOPNOTSUPP ; }
Signature · 1 line
2.51.2
PATCH v3 2/3 net: stmmac: Add glue driver for Motorcomm YT6801 ethernet controller
In reply to Yao ZiMotorcomm YT6801 is a PCIe ethernet controller based on DWMAC4 IP. It integrates an GbE phy, supporting WOL, VLAN tagging and various types of offloading. It ships an on-chip eFuse for storing various vendor configuration, including MAC address.
This patch adds basic glue code for the controller, allowing it to be set up and transmit data at a reasonable speed. Features like WOL could be implemented in the future.
--- drivers/net/ethernet/stmicro/stmmac/Kconfig | 9 + drivers/net/ethernet/stmicro/stmmac/Makefile | 1 + .../ethernet/stmicro/stmmac/dwmac-motorcomm.c | 378 ++++++++++++++++++ 3 files changed, 388 insertions(+) create mode 100644 drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c
drivers/net/ethernet/stmicro/stmmac/Kconfig +9 -0
@@ -374,6 +374,15 @@ config DWMAC_LOONGSON This selects the LOONGSON PCI bus support for the stmmac driver, Support for ethernet controller on Loongson-2K1000 SoC and LS7A1000 bridge. +config DWMAC_MOTORCOMM + tristate "Motorcomm PCI DWMAC support" + depends on PCI + select MOTORCOMM_PHY + select STMMAC_LIBPCI + help + This enables glue driver for Motorcomm DWMAC-based PCI Ethernet + controllers. Currently only YT6801 is supported. + config STMMAC_PCI tristate "STMMAC PCI bus support" depends on PCI
drivers/net/ethernet/stmicro/stmmac/Makefile +1 -0
@@ -48,4 +48,5 @@ obj-$(CONFIG_STMMAC_LIBPCI) += stmmac_libpci.o obj-$(CONFIG_STMMAC_PCI) += stmmac-pci.o obj-$(CONFIG_DWMAC_INTEL) += dwmac-intel.o obj-$(CONFIG_DWMAC_LOONGSON) += dwmac-loongson.o +obj-$(CONFIG_DWMAC_MOTORCOMM) += dwmac-motorcomm.o stmmac-pci-objs:= stmmac_pci.o
drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c new file +378 -0
378 lines not on this page · raw message
Signature · 1 line
2.51.2
Russell King (Oracle)
reviewerRe: [PATCH net-next v3 2/3] net: stmmac: Add glue driver for Motorcomm YT6801 ethernet controller
Quoted 32 lines · Yao Zi
drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c new file +32
-0+static int motorcomm_setup_irq (struct pci_dev *pdev, +struct stmmac_resources *res, +struct plat_stmmacenet_data *plat) +{ +int ret; + + ret =pci_alloc_irq_vectors (pdev,6 ,6 ,PCI_IRQ_MSIX ); +if (ret >0 ) { + res->rx_irq[0 ] =pci_irq_vector (pdev,0 ); + res->tx_irq[0 ] =pci_irq_vector (pdev,4 ); + res->irq =pci_irq_vector (pdev,5 ); + + plat->flags |=STMMAC_FLAG_MULTI_MSI_EN ; + +return 0 ; + } + +dev_info (&pdev->dev,"failed to allocate MSI-X vector: %d\n" , ret); +dev_info (&pdev->dev,"try MSI instead\n" ); + + ret =pci_alloc_irq_vectors (pdev,1 ,1 ,PCI_IRQ_MSI ); +if (ret <0 ) +return dev_err_probe (&pdev->dev, ret, +"failed to allocate MSI\n" ); + + res->irq =pci_irq_vector (pdev,0 ); + +return 0 ; +} + +static int motorcomm_probe (struct pci_dev *pdev,const struct pci_device_id *id) +{
...
drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c line 349
Quoted 9 lines
drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c new file +9
-0+ ret =motorcomm_setup_irq (pdev, &res, plat); +if (ret) +return dev_err_probe (&pdev->dev, ret,"failed to setup IRQ\n" ); + +motorcomm_init (priv); + + res.addr = priv->base +GMAC_OFFSET ; + +return stmmac_dvr_probe (&pdev->dev, plat, &res);
If stmmac_dvr_probe() fails, then it will return an error code. This leaves the PCI MSI interrupt allocated...
drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c line 355
Quoted 6 lines
drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c new file +6
-0+} + +static void motorcomm_remove (struct pci_dev *pdev) +{ +stmmac_dvr_remove (&pdev->dev); +pci_free_irq_vectors (pdev);
... which stood out because of the presence of this function doing stuff after the call to stmmac_dvr_remove().
So... reviewing the other stmmac PCI drivers:
- dwmac-intel calls pci_alloc_irq_vectors() but does not call pci_free_irq_vectors(). This looks like a bug.
- dwmac-intel calls pcim_enable_device() in its probe function, and also its intel_eth_pci_resume() - pcim_enable_device() is the devres managed function, so we end up adding more and more devres entries each time intel_eth_pci_resume() is resumed. Note that intel_eth_pci_suspend() doesn't disable the device. So, this should probably be the non-devres version.
- dwmac-loongson looks sane, but the checks for ld->multichan before calling loongson_dwmac_msi_clear() look unnecessary, as pci_free_irq_vectors() can be safely called even if MSI/MSI-X have not been (successfully) allocated.
So, I wonder whether there is scope to have a common way to clean up PCI drivers. Could you look into this please?
Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/ FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!
Re: [PATCH net-next v3 2/3] net: stmmac: Add glue driver for Motorcomm YT6801 ethernet controller
Hi Russell,
Sorry for the late reply,
Quoted 34 lines · Russell King (Oracle)
Quoted 32 lines · Yao Zi
drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c new file +32
-0+static int motorcomm_setup_irq (struct pci_dev *pdev, +struct stmmac_resources *res, +struct plat_stmmacenet_data *plat) +{ +int ret; + + ret =pci_alloc_irq_vectors (pdev,6 ,6 ,PCI_IRQ_MSIX ); +if (ret >0 ) { + res->rx_irq[0 ] =pci_irq_vector (pdev,0 ); + res->tx_irq[0 ] =pci_irq_vector (pdev,4 ); + res->irq =pci_irq_vector (pdev,5 ); + + plat->flags |=STMMAC_FLAG_MULTI_MSI_EN ; + +return 0 ; + } + +dev_info (&pdev->dev,"failed to allocate MSI-X vector: %d\n" , ret); +dev_info (&pdev->dev,"try MSI instead\n" ); + + ret =pci_alloc_irq_vectors (pdev,1 ,1 ,PCI_IRQ_MSI ); +if (ret <0 ) +return dev_err_probe (&pdev->dev, ret, +"failed to allocate MSI\n" ); + + res->irq =pci_irq_vector (pdev,0 ); + +return 0 ; +} + +static int motorcomm_probe (struct pci_dev *pdev,const struct pci_device_id *id) +{...
Quoted 12 lines
drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c line 349
Quoted 9 lines
drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c new file +9
-0+ ret =motorcomm_setup_irq (pdev, &res, plat); +if (ret) +return dev_err_probe (&pdev->dev, ret,"failed to setup IRQ\n" ); + +motorcomm_init (priv); + + res.addr = priv->base +GMAC_OFFSET ; + +return stmmac_dvr_probe (&pdev->dev, plat, &res);If
stmmac_dvr_probe()fails, then it will return an error code. This leaves the PCI MSI interrupt allocated...
This isn't true. MSI API is a little magical: when the device is enabled through pcim_enable_device(), the device becomes devres-managed, and a plain call to pci_alloc_irq_vectors() becomes managed, too, even if its name doesn't indicate it's a devres-managed API.
pci_free_irq_vectors() will be automatically called on driver deattach. See pcim_setup_msi_release() in drivers/pci/msi/msi.c, which is invoked by pci_alloc_irq_vectors() internally.
Quoted 9 lines
drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c line 355
Quoted 6 lines
drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c new file +6
-0+} + +static void motorcomm_remove (struct pci_dev *pdev) +{ +stmmac_dvr_remove (&pdev->dev); +pci_free_irq_vectors (pdev);... which stood out because of the presence of this function doing stuff after the call to
stmmac_dvr_remove().
But yes, this call to pci_free_irq_vectors() is redundant, since pci_free_irq_vectors() will be automatically invoked. I'll remove it in the next version.
Quoted 4 lines
So... reviewing the other stmmac PCI drivers:
- dwmac-intel calls
pci_alloc_irq_vectors()but does not callpci_free_irq_vectors(). This looks like a bug.
The driver does call pcim_enable_device() thus enables devres for the PCI device, in which case manually calling pci_free_irq_vectors() is unnecessary. I don't think there's a bug.
Quoted 6 lines
- dwmac-intel calls
pcim_enable_device()in its probe function, and also itsintel_eth_pci_resume()-pcim_enable_device()is the devres managed function, so we end up adding more and more devres entries each timeintel_eth_pci_resume()is resumed. Note thatintel_eth_pci_suspend()doesn't disable the device. So, this should probably be the non-devres version.
Agree.
Quoted 4 lines
- dwmac-loongson looks sane, but the checks for
ld->multichanbefore callingloongson_dwmac_msi_clear()look unnecessary, aspci_free_irq_vectors()can be safely called even if MSI/MSI-X have not been (successfully) allocated.
loongson-dwmac doesn't enable devres for the PCI device (it calls pci_enable_device() instead), so manually freeing the interrupts is indeed necessary. However, I'd suggest moving to the devres variant and simplify the error handling (and clean up) path.
Quoted 2 lines
So, I wonder whether there is scope to have a common way to clean up PCI drivers. Could you look into this please?
In short, enabling the device with pcim_enable_device() to make it devres-managed should be the best solution, in which case pci_alloc_irq_vectors() is devres-managed, too, and at least we don't need to worry about interrupts anymore on error handling/removal path.
Regards,
Yao Zi
Quoted 3 lines
Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/ FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!
Russell King (Oracle)
reviewerRe: [PATCH net-next v3 2/3] net: stmmac: Add glue driver for Motorcomm YT6801 ethernet controller
Quoted 52 lines · Yao Zi
Hi Russell,
Sorry for the late reply,
Quoted 34 lines · Russell King (Oracle)
Quoted 32 lines · Yao Zi
drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c new file +32
-0+static int motorcomm_setup_irq (struct pci_dev *pdev, +struct stmmac_resources *res, +struct plat_stmmacenet_data *plat) +{ +int ret; + + ret =pci_alloc_irq_vectors (pdev,6 ,6 ,PCI_IRQ_MSIX ); +if (ret >0 ) { + res->rx_irq[0 ] =pci_irq_vector (pdev,0 ); + res->tx_irq[0 ] =pci_irq_vector (pdev,4 ); + res->irq =pci_irq_vector (pdev,5 ); + + plat->flags |=STMMAC_FLAG_MULTI_MSI_EN ; + +return 0 ; + } + +dev_info (&pdev->dev,"failed to allocate MSI-X vector: %d\n" , ret); +dev_info (&pdev->dev,"try MSI instead\n" ); + + ret =pci_alloc_irq_vectors (pdev,1 ,1 ,PCI_IRQ_MSI ); +if (ret <0 ) +return dev_err_probe (&pdev->dev, ret, +"failed to allocate MSI\n" ); + + res->irq =pci_irq_vector (pdev,0 ); + +return 0 ; +} + +static int motorcomm_probe (struct pci_dev *pdev,const struct pci_device_id *id) +{...
Quoted 12 lines
drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c line 349
Quoted 9 lines
drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c new file +9
-0+ ret =motorcomm_setup_irq (pdev, &res, plat); +if (ret) +return dev_err_probe (&pdev->dev, ret,"failed to setup IRQ\n" ); + +motorcomm_init (priv); + + res.addr = priv->base +GMAC_OFFSET ; + +return stmmac_dvr_probe (&pdev->dev, plat, &res);If
stmmac_dvr_probe()fails, then it will return an error code. This leaves the PCI MSI interrupt allocated...
Quoted 8 lines
This isn't true. MSI API is a little magical: when the device is enabled through
pcim_enable_device(), the device becomes devres-managed, and a plain call topci_alloc_irq_vectors()becomes managed, too, even if its name doesn't indicate it's a devres-managed API.
pci_free_irq_vectors()will be automatically called on driver deattach. Seepcim_setup_msi_release()indrivers/pci/msi/msi.c, which is invoked bypci_alloc_irq_vectors()internally.
This looks very non-intuitive, and the documentation for pci_alloc_irq_vectors() doesn't help:
* Upon a successful allocation, the caller should use pci_irq_vector()
* to get the Linux IRQ number to be passed to request_threaded_irq().
* The driver must call pci_free_irq_vectors() on cleanup. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
because if what you say is correct (and it looks like it is) then this line is blatently incorrect.
Bjorn?
Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/ FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!
Re: [PATCH net-next v3 2/3] net: stmmac: Add glue driver for Motorcomm YT6801 ethernet controller
[+to Philipp, Thomas for MSI devres question]
Quoted 70 lines · Russell King (Oracle)
Quoted 48 lines · Yao Zi
Quoted 34 lines · Russell King (Oracle)
Quoted 32 lines · Yao Zi
drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c new file +32
-0+static int motorcomm_setup_irq (struct pci_dev *pdev, +struct stmmac_resources *res, +struct plat_stmmacenet_data *plat) +{ +int ret; + + ret =pci_alloc_irq_vectors (pdev,6 ,6 ,PCI_IRQ_MSIX ); +if (ret >0 ) { + res->rx_irq[0 ] =pci_irq_vector (pdev,0 ); + res->tx_irq[0 ] =pci_irq_vector (pdev,4 ); + res->irq =pci_irq_vector (pdev,5 ); + + plat->flags |=STMMAC_FLAG_MULTI_MSI_EN ; + +return 0 ; + } + +dev_info (&pdev->dev,"failed to allocate MSI-X vector: %d\n" , ret); +dev_info (&pdev->dev,"try MSI instead\n" ); + + ret =pci_alloc_irq_vectors (pdev,1 ,1 ,PCI_IRQ_MSI ); +if (ret <0 ) +return dev_err_probe (&pdev->dev, ret, +"failed to allocate MSI\n" ); + + res->irq =pci_irq_vector (pdev,0 ); + +return 0 ; +} + +static int motorcomm_probe (struct pci_dev *pdev,const struct pci_device_id *id) +{...
Quoted 12 lines
drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c line 349
Quoted 9 lines
drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c new file +9
-0+ ret =motorcomm_setup_irq (pdev, &res, plat); +if (ret) +return dev_err_probe (&pdev->dev, ret,"failed to setup IRQ\n" ); + +motorcomm_init (priv); + + res.addr = priv->base +GMAC_OFFSET ; + +return stmmac_dvr_probe (&pdev->dev, plat, &res);If
stmmac_dvr_probe()fails, then it will return an error code. This leaves the PCI MSI interrupt allocated...Quoted 8 lines
This isn't true. MSI API is a little magical: when the device is enabled through
pcim_enable_device(), the device becomes devres-managed, and a plain call topci_alloc_irq_vectors()becomes managed, too, even if its name doesn't indicate it's a devres-managed API.
pci_free_irq_vectors()will be automatically called on driver deattach. Seepcim_setup_msi_release()indrivers/pci/msi/msi.c, which is invoked bypci_alloc_irq_vectors()internally.This looks very non-intuitive, and the documentation for
pci_alloc_irq_vectors()doesn't help:* Upon a successful allocation, the caller should use
pci_irq_vector()
* to get the Linux IRQ number to be passed torequest_threaded_irq().
* The driver must callpci_free_irq_vectors()on cleanup. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^because if what you say is correct (and it looks like it is) then this line is blatently incorrect.
Bjorn?
Re: [PATCH net-next v3 2/3] net: stmmac: Add glue driver for Motorcomm YT6801 ethernet controller
Quoted 53 lines · Bjorn Helgaas
[+to Philipp, Thomas for MSI devres question]
Quoted 50 lines · Russell King (Oracle)
Quoted 49 lines · Yao Zi
Quoted 34 lines · Russell King (Oracle)
Quoted 32 lines · Yao Zi
drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c new file +32
-0+static int motorcomm_setup_irq (struct pci_dev *pdev, + struct stmmac_resources *res, + struct plat_stmmacenet_data *plat) +{ +int ret; + + ret =pci_alloc_irq_vectors (pdev,6 ,6 ,PCI_IRQ_MSIX ); +if (ret >0 ) { + res->rx_irq[0 ] =pci_irq_vector (pdev,0 ); + res->tx_irq[0 ] =pci_irq_vector (pdev,4 ); + res->irq =pci_irq_vector (pdev,5 ); + + plat->flags |=STMMAC_FLAG_MULTI_MSI_EN ; + +return 0 ; + } + +dev_info (&pdev->dev,"failed to allocate MSI-X vector: %d\n" , ret); +dev_info (&pdev->dev,"try MSI instead\n" ); + + ret =pci_alloc_irq_vectors (pdev,1 ,1 ,PCI_IRQ_MSI ); +if (ret <0 ) +return dev_err_probe (&pdev->dev, ret, + "failed to allocate MSI\n"); + + res->irq =pci_irq_vector (pdev,0 ); + +return 0 ; +} + +static int motorcomm_probe (struct pci_dev *pdev,const struct pci_device_id *id) +{...
Quoted 12 lines
drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c line 349
Quoted 9 lines
drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c new file +9
-0+ ret =motorcomm_setup_irq (pdev, &res, plat); +if (ret) +return dev_err_probe (&pdev->dev, ret,"failed to setup IRQ\n" ); + +motorcomm_init (priv); + + res.addr = priv->base +GMAC_OFFSET ; + +return stmmac_dvr_probe (&pdev->dev, plat, &res);If
stmmac_dvr_probe()fails, then it will return an error code. This leaves the PCI MSI interrupt allocated...This isn't true. MSI API is a little magical: when the device is enabled
s/magical/confusing and explosive
;)
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
through
pcim_enable_device(), the device becomes devres-managed, and a plain call topci_alloc_irq_vectors()becomes managed, too, even if its name doesn't indicate it's a devres-managed API.
Just to be clear: Callers of pci_setup_msi_context() are the last users in PCI which express this strange behavior. All the other APIs behave as one could expect from their name (pcim_ vs pci_).
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
pci_free_irq_vectors()will be automatically called on driver deattach. Seepcim_setup_msi_release()indrivers/pci/msi/msi.c, which is invoked bypci_alloc_irq_vectors()internally.
Yes, this is correct.
Quoted 11 lines
Quoted 11 lines
This looks very non-intuitive, and the documentation for
pci_alloc_irq_vectors()doesn't help:* Upon a successful allocation, the caller should use
pci_irq_vector()
* to get the Linux IRQ number to be passed torequest_threaded_irq().
* The driver must callpci_free_irq_vectors()on cleanup. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^because if what you say is correct (and it looks like it is) then this line is blatently incorrect.
True, this line is false. It should probably state "If you didn't enable your PCI device with pcim_enable_device(), you must call pci_free_irq_vectors() on cleanup."
I don't know whether calling pci_free_irq_vectors() is a bug (once manually, once hidden by devres), though.
If it's not a bug, one could keep the docu that way or at least phrase it in a way so that no additional users start relying on that hybrid mechanism.
There is actually no reason anymore to call pcim_enable_device() at all, other than that you get automatic device disablement.
Quoted 2 lines
Quoted 2 lines
Bjorn?
BTW, if PCI has a TODO list somewhere, removing that hybrid devres feature for MSI should be on it, for obvious reasons.
The good news is that it's the last remainder of PCI hybrid devres and getting rid of it would allow for removal of some additional code, too (e.g., is_enabled bit and pcim_pin_device()).
The bad news is that it's not super trivial to remove. I looked into it about two times and decided I can't invest that time currently. You need to go over all drivers again to see who uses pcim_enable_device(), then add free_irq_vecs() for them all and so on…
If you give me a pointer I can provide a TODO entry. In any case, feel free to set me as a reviewer!
Regards
Philipp
Russell King (Oracle)
reviewerRe: [PATCH net-next v3 2/3] net: stmmac: Add glue driver for Motorcomm YT6801 ethernet controller
Quoted 4 lines · Philipp Stanner
The bad news is that it's not super trivial to remove. I looked into it about two times and decided I can't invest that time currently. You need to go over all drivers again to see who uses
pcim_enable_device(), then addfree_irq_vecs()for them all and so on…
So that I can confirm, you're saying that all drivers that call pci_alloc_irq_vectors() should call pci_free_irq_vectors() in their ->remove() method and not rely on the devres behaviour that pcim_enable_device() will permit.
In terms of whether it's safe to call this twice, pci_free_irq_vectors() calls pci_disable_msix() and pci_disable_msi().
pci_disable_msix() checks:
if (!pci_msi_enabled() || !dev || !dev->msix_enabled)
return;which will set dev->msix_enabled to 0 via pci_msix_shutdown().
pci_disable_msi() does a similar check:
if (!pci_msi_enabled() || !dev || !dev->msi_enabled)
return;and similarly pci_msi_shutdown() sets dev->msi_enabled to 0.
So my conclusion is it's safe to call pci_free_irq_vectors() twice for the same device.
Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/ FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!
Re: [PATCH net-next v3 2/3] net: stmmac: Add glue driver for Motorcomm YT6801 ethernet controller
Quoted 10 lines · Russell King (Oracle)
Quoted 4 lines · Philipp Stanner
The bad news is that it's not super trivial to remove. I looked into it about two times and decided I can't invest that time currently. You need to go over all drivers again to see who uses
pcim_enable_device(), then addfree_irq_vecs()for them all and so on…So that I can confirm, you're saying that all drivers that call
pci_alloc_irq_vectors()should callpci_free_irq_vectors()in their ->remove() method and not rely on the devres behaviour thatpcim_enable_device()will permit.
"permit" is kind of a generous word. This behavior is dangerous and there were bugs because of that in the past, because it confused programmers. See:
f00059b4c1b0 drm/vboxvideo: fix mapping leaks
pcim_enable_device() used to switch all sorts of functions into managed mode. As far as I could figure out through git, back in 2009 it was intended that ALL pci functions are switched into managed mode that way. That's also how it was documented.
The ecosystem then fractured, however. Some functions were always managed (pcim_), some never, and some sometimes.
I removed all "sometimes managed" functions since 2024. The last remainder is MSI.
If we want to remove that, we need to:
1. Find all drivers that rely on pci_free_irq_vectors() being run automatically. IOW those that use pcim_enable_device() + wrappers around pci_setup_msi_context().
2. Port those drivers to do the free_irq_vecs manually, if it's not a problem if it's called twice. If that were a problem, those drivers would also need to replace pcim_enable_device() with pci_enable_device().
3. Once all drivers are ported, remove the devres code from msi.c
4. Do associated cleanup work in PCI.
Quoted 13 lines
In terms of whether it's safe to call this twice,
pci_free_irq_vectors()callspci_disable_msix()andpci_disable_msi().
pci_disable_msix()checks:if (!pci_msi_enabled() || !dev || !dev->msix_enabled) return;
which will set
dev->msix_enabledto 0 viapci_msix_shutdown().
pci_disable_msi()does a similar check:
Quoted 8 lines
if (!pci_msi_enabled() || !dev || !dev->msi_enabled) return;
and similarly
pci_msi_shutdown()setsdev->msi_enabledto 0.So my conclusion is it's safe to call
pci_free_irq_vectors()twice for the same device.
Hm. Looks good.
P.
Russell King (Oracle)
reviewerRe: [PATCH net-next v3 2/3] net: stmmac: Add glue driver for Motorcomm YT6801 ethernet controller
Quoted 57 lines · Philipp Stanner
Quoted 10 lines · Russell King (Oracle)
Quoted 4 lines · Philipp Stanner
The bad news is that it's not super trivial to remove. I looked into it about two times and decided I can't invest that time currently. You need to go over all drivers again to see who uses
pcim_enable_device(), then addfree_irq_vecs()for them all and so on…So that I can confirm, you're saying that all drivers that call
pci_alloc_irq_vectors()should callpci_free_irq_vectors()in their ->remove() method and not rely on the devres behaviour thatpcim_enable_device()will permit."permit" is kind of a generous word. This behavior is dangerous and there were bugs because of that in the past, because it confused programmers. See:
f00059b4c1b0drm/vboxvideo: fix mapping leaks
pcim_enable_device()used to switch all sorts of functions into managed mode. As far as I could figure out through git, back in 2009 it was intended that ALL pci functions are switched into managed mode that way. That's also how it was documented.The ecosystem then fractured, however. Some functions were always managed (
pcim_), some never, and some sometimes.I removed all "sometimes managed" functions since 2024. The last remainder is MSI.
If we want to remove that, we need to:
1. Find all drivers that rely onpci_free_irq_vectors()being run automatically. IOW those that usepcim_enable_device()+ wrappers aroundpci_setup_msi_context().
2. Port those drivers to do thefree_irq_vecsmanually, if it's not a problem if it's called twice. If that were a problem, those drivers would also need to replacepcim_enable_device()withpci_enable_device().
3. Once all drivers are ported, remove the devres code frommsi.c
4. Do associated cleanup work in PCI.Quoted 16 lines
In terms of whether it's safe to call this twice,
pci_free_irq_vectors()callspci_disable_msix()andpci_disable_msi().
pci_disable_msix()checks:if (!pci_msi_enabled() || !dev || !dev->msix_enabled) return;
which will set
dev->msix_enabledto 0 viapci_msix_shutdown().
pci_disable_msi()does a similar check:if (!pci_msi_enabled() || !dev || !dev->msi_enabled) return;
Quoted 7 lines
Quoted 5 lines
and similarly
pci_msi_shutdown()setsdev->msi_enabledto 0.So my conclusion is it's safe to call
pci_free_irq_vectors()twice for the same device.Hm. Looks good.
So, what do you want to see for new drivers such as the one at the top of this thread? Should they explicitly call pci_free_irq_vectors() even though they call pcim_enable_device() ?
Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/ FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!
Re: [PATCH net-next v3 2/3] net: stmmac: Add glue driver for Motorcomm YT6801 ethernet controller
Quoted 64 lines · Russell King (Oracle)
Quoted 63 lines · Philipp Stanner
Quoted 10 lines · Russell King (Oracle)
Quoted 4 lines · Philipp Stanner
The bad news is that it's not super trivial to remove. I looked into it about two times and decided I can't invest that time currently. You need to go over all drivers again to see who uses
pcim_enable_device(), then addfree_irq_vecs()for them all and so on…So that I can confirm, you're saying that all drivers that call
pci_alloc_irq_vectors()should callpci_free_irq_vectors()in their ->remove() method and not rely on the devres behaviour thatpcim_enable_device()will permit."permit" is kind of a generous word. This behavior is dangerous and there were bugs because of that in the past, because it confused programmers. See:
f00059b4c1b0drm/vboxvideo: fix mapping leaks
pcim_enable_device()used to switch all sorts of functions into managed mode. As far as I could figure out through git, back in 2009 it was intended that ALL pci functions are switched into managed mode that way. That's also how it was documented.The ecosystem then fractured, however. Some functions were always managed (
pcim_), some never, and some sometimes.I removed all "sometimes managed" functions since 2024. The last remainder is MSI.
If we want to remove that, we need to:
1. Find all drivers that rely onpci_free_irq_vectors()being run automatically. IOW those that usepcim_enable_device()+ wrappers aroundpci_setup_msi_context().
2. Port those drivers to do thefree_irq_vecsmanually, if it's not a problem if it's called twice. If that were a problem, those drivers would also need to replacepcim_enable_device()withpci_enable_device().
3. Once all drivers are ported, remove the devres code frommsi.c
4. Do associated cleanup work in PCI.Quoted 21 lines
In terms of whether it's safe to call this twice,
pci_free_irq_vectors()callspci_disable_msix()andpci_disable_msi().
pci_disable_msix()checks:if (!pci_msi_enabled() || !dev || !dev->msix_enabled) return;
which will set
dev->msix_enabledto 0 viapci_msix_shutdown().
pci_disable_msi()does a similar check:if (!pci_msi_enabled() || !dev || !dev->msi_enabled) return;
and similarly
pci_msi_shutdown()setsdev->msi_enabledto 0.So my conclusion is it's safe to call
pci_free_irq_vectors()twice for the same device.
Quoted 5 lines
Quoted 1 line
Hm. Looks good.
So, what do you want to see for new drivers such as the one at the top of this thread? Should they explicitly call
pci_free_irq_vectors()even though they callpcim_enable_device()?
Yes, I think that's the right thing to do. It makes removing that feature from MSI easier, since there will not be even more drivers to port.
P.
Yao Zi
maintainerRe: [PATCH net-next v3 2/3] net: stmmac: Add glue driver for Motorcomm YT6801 ethernet controller
In reply to Philipp StannerQuoted 7 lines · Philipp Stanner
Quoted 6 lines · Bjorn Helgaas
[+to Philipp, Thomas for MSI devres question]
Quoted 3 lines · Russell King (Oracle)
Quoted 2 lines · Yao Zi
Quoted 1 line · Russell King (Oracle)
On Mon, Nov 24, 2025 at 04:32:10PM +0000, Yao Zi wrote:
...
Quoted 11 lines
Quoted 10 lines
Quoted 10 lines
This looks very non-intuitive, and the documentation for
pci_alloc_irq_vectors()doesn't help:* Upon a successful allocation, the caller should use
pci_irq_vector()
* to get the Linux IRQ number to be passed torequest_threaded_irq().
* The driver must callpci_free_irq_vectors()on cleanup. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^because if what you say is correct (and it looks like it is) then this line is blatently incorrect.
Quoted 7 lines
True, this line is false. It should probably state "If you didn't enable your PCI device with
pcim_enable_device(), you must callpci_free_irq_vectors()on cleanup."If it's not a bug, one could keep the docu that way or at least phrase it in a way so that no additional users start relying on that hybrid mechanism.
Thanks for the clarification, would you mind me sending a patch to fix the description, and also mention the automatic clean-up behavior shouldn't be relied anymore in new code?
...
Quoted 8 lines
The good news is that it's the last remainder of PCI hybrid devres and getting rid of it would allow for removal of some additional code, too (e.g.,
is_enabledbit andpcim_pin_device()).The bad news is that it's not super trivial to remove. I looked into it about two times and decided I can't invest that time currently. You need to go over all drivers again to see who uses
pcim_enable_device(), then addfree_irq_vecs()for them all and so on…
Do you think adding an implementation of pcim_alloc_irq_vectors(), that always call pci_free_irq_vectors() regardless whether the PCI device is managed, will help the conversion?
This will make it more trival to rewrite drivers depending on the automatic clean-up behavior: since calling pci_free_irq_vectors() several times is okay, we could simply change pci_alloc_irq_vectors() to pcim_alloc_irq_vectors(), without considering where to call pci_free_irq_vectors().
Introducing pcim_alloc_irq_vectors() will also help newly-introduced drivers to reduce duplicated code to handle resource clean-up.
Quoted 5 lines
If you give me a pointer I can provide a TODO entry. In any case, feel free to set me as a reviewer!
Regards
Philipp
Regards,
Yao Zi
Re: [PATCH net-next v3 2/3] net: stmmac: Add glue driver for Motorcomm YT6801 ethernet controller
Quoted 26 lines · Yao Zi
Quoted 7 lines · Philipp Stanner
Quoted 6 lines · Bjorn Helgaas
[+to Philipp, Thomas for MSI devres question]
Quoted 3 lines · Russell King (Oracle)
Quoted 2 lines · Yao Zi
Quoted 1 line · Russell King (Oracle)
On Mon, Nov 24, 2025 at 04:32:10PM +0000, Yao Zi wrote:
...
Quoted 15 lines
Quoted 10 lines
Quoted 10 lines
This looks very non-intuitive, and the documentation for
pci_alloc_irq_vectors()doesn't help:* Upon a successful allocation, the caller should use
pci_irq_vector()
* to get the Linux IRQ number to be passed torequest_threaded_irq().
* The driver must callpci_free_irq_vectors()on cleanup. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^because if what you say is correct (and it looks like it is) then this line is blatently incorrect.
True, this line is false. It should probably state "If you didn't enable your PCI device with
pcim_enable_device(), you must callpci_free_irq_vectors()on cleanup."
Quoted 7 lines
Quoted 3 lines
If it's not a bug, one could keep the docu that way or at least phrase it in a way so that no additional users start relying on that hybrid mechanism.
Thanks for the clarification, would you mind me sending a patch to fix the description, and also mention the automatic clean-up behavior shouldn't be relied anymore in new code?
If I would mind if *you* send such a patch? I for sure wouldn't mind, that's the entire idea of open source development ^^
I can of course review it if you +Cc me.
Quoted 16 lines
...
Quoted 8 lines
The good news is that it's the last remainder of PCI hybrid devres and getting rid of it would allow for removal of some additional code, too (e.g.,
is_enabledbit andpcim_pin_device()).The bad news is that it's not super trivial to remove. I looked into it about two times and decided I can't invest that time currently. You need to go over all drivers again to see who uses
pcim_enable_device(), then addfree_irq_vecs()for them all and so on…Do you think adding an implementation of
pcim_alloc_irq_vectors(), that always callpci_free_irq_vectors()regardless whether the PCI device is managed, will help the conversion?
Quoted 8 lines
This will make it more trival to rewrite drivers depending on the automatic clean-up behavior: since calling
pci_free_irq_vectors()several times is okay, we could simply changepci_alloc_irq_vectors()topcim_alloc_irq_vectors(), without considering where to callpci_free_irq_vectors().Introducing
pcim_alloc_irq_vectors()will also help newly-introduced drivers to reduce duplicated code to handle resource clean-up.
That's in fact how I have cleaned up the hybrid nature that was present until this year in pci_request_region() et al.:
https://lore.kernel.org/linux-pci/[email protected]/
It's one way to do it. First port everyone who relies on managed behavior to a pcim_ function, and once they're all ported, remove the hybrid nature from the pci_ function.
So that works, yes. The real question is just whether one wants a pcim_ function for the irq vectors. My personal impression is that this looks like a useful feature; but my expertise with MSI is a bit limited. There's also this strange kernel-wide msi_enabled global bool..
I guess the best way to find out is to try implementing it.
In case of doubt, the boring unmanaged pci_ version is the safe choice. One contributor around here has once called the managed versions "the crazy devres voodoo" :p
(BTW, just to be sure, pcim_ functions must not be interconnected with pcim_enable() in the future anymore, nor shall they use global state. All PCI devres functionality should purely work on the device file through pure devres. The pcim_enable() interconnection cam only from the hybrid feature.)
P.
Quoted 9 lines
Quoted 6 lines
If you give me a pointer I can provide a TODO entry. In any case, feel free to set me as a reviewer!
Regards
PhilippRegards,
Yao Zi
Russell King (Oracle)
reviewerRe: [PATCH net-next v3 2/3] net: stmmac: Add glue driver for Motorcomm YT6801 ethernet controller
In reply to Yao ZiQuoted 52 lines · Yao Zi
Hi Russell,
Sorry for the late reply,
Quoted 34 lines · Russell King (Oracle)
Quoted 32 lines · Yao Zi
drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c new file +32
-0+static int motorcomm_setup_irq (struct pci_dev *pdev, +struct stmmac_resources *res, +struct plat_stmmacenet_data *plat) +{ +int ret; + + ret =pci_alloc_irq_vectors (pdev,6 ,6 ,PCI_IRQ_MSIX ); +if (ret >0 ) { + res->rx_irq[0 ] =pci_irq_vector (pdev,0 ); + res->tx_irq[0 ] =pci_irq_vector (pdev,4 ); + res->irq =pci_irq_vector (pdev,5 ); + + plat->flags |=STMMAC_FLAG_MULTI_MSI_EN ; + +return 0 ; + } + +dev_info (&pdev->dev,"failed to allocate MSI-X vector: %d\n" , ret); +dev_info (&pdev->dev,"try MSI instead\n" ); + + ret =pci_alloc_irq_vectors (pdev,1 ,1 ,PCI_IRQ_MSI ); +if (ret <0 ) +return dev_err_probe (&pdev->dev, ret, +"failed to allocate MSI\n" ); + + res->irq =pci_irq_vector (pdev,0 ); + +return 0 ; +} + +static int motorcomm_probe (struct pci_dev *pdev,const struct pci_device_id *id) +{...
Quoted 12 lines
drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c line 349
Quoted 9 lines
drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c new file +9
-0+ ret =motorcomm_setup_irq (pdev, &res, plat); +if (ret) +return dev_err_probe (&pdev->dev, ret,"failed to setup IRQ\n" ); + +motorcomm_init (priv); + + res.addr = priv->base +GMAC_OFFSET ; + +return stmmac_dvr_probe (&pdev->dev, plat, &res);If
stmmac_dvr_probe()fails, then it will return an error code. This leaves the PCI MSI interrupt allocated...
Quoted 8 lines
This isn't true. MSI API is a little magical: when the device is enabled through
pcim_enable_device(), the device becomes devres-managed, and a plain call topci_alloc_irq_vectors()becomes managed, too, even if its name doesn't indicate it's a devres-managed API.
pci_free_irq_vectors()will be automatically called on driver deattach. Seepcim_setup_msi_release()indrivers/pci/msi/msi.c, which is invoked bypci_alloc_irq_vectors()internally.
As discussed in the sub-thread with Philipp Stanner, please explicitly call pci_alloc_irq_vectors() in the cleanup path to avoid adding to the burden of drivers that need to be fixed allow this "magical" behaviour to be removed in the future.
Thanks.
Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/ FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!
PATCH v3 3/3 MAINTAINERS: Assign myself as maintainer of Motorcomm DWMAC glue driver
In reply to Yao ZiI volunteer to maintain the DWMAC glue driver for Motorcomm ethernet controllers.
--- MAINTAINERS | 6 ++++++ 1 file changed, 6 insertions(+)
MAINTAINERS +6 -0
@@ -17621,6 +17621,12 @@ F: drivers/most/ F: drivers/staging/most/ F: include/linux/most.h +MOTORCOMM DWMAC GLUE DRIVER +M: Yao Zi <[email protected]> +L: [email protected] +S: Maintained +F: drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c + MOTORCOMM PHY DRIVER M: Frank <[email protected]> L: [email protected]
Signature · 1 line
2.51.2