add NPCM7xx EMC 10/100 Ethernet driver
Avi Fishman
maintainercover add NPCM7xx EMC 10/100 Ethernet driver
EMC Ethernet Media Access Controller supports 10/100 Mbps and RMII. This driver has been working on Nuvoton BMC NPCM7xx.
Avi Fishman (2):
dt-binding: net: document NPCM7xx EMC 10/100 DT bindings
net: npcm: add NPCM7xx EMC 10/100 Ethernet driver
.../bindings/net/nuvoton,npcm7xx-emc.txt | 38 + drivers/net/ethernet/nuvoton/Kconfig | 21 +- drivers/net/ethernet/nuvoton/Makefile | 2 + drivers/net/ethernet/nuvoton/npcm7xx_emc.c | 2073 +++++++++++++++++ 4 files changed, 2131 insertions(+), 3 deletions(-) create mode 100644 Documentation/devicetree/bindings/net/nuvoton,npcm7xx-emc.txt create mode 100644 drivers/net/ethernet/nuvoton/npcm7xx_emc.c
Signature · 1 line
2.18.0
Avi Fishman
maintainerPATCH 1/2 dt-binding: net: document NPCM7xx EMC 10/100 DT bindings
Added device tree binding documentation for Nuvoton NPCM7xx Ethernet MAC Controller (EMC) 10/100 RMII
--- .../bindings/net/nuvoton,npcm7xx-emc.txt | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Documentation/devicetree/bindings/net/nuvoton,npcm7xx-emc.txt
Documentation/devicetree/bindings/net/nuvoton,npcm7xx-emc.txt new file +38 -0
@@ -0,0 +1,38 @@ +Nuvoton NPCM7XX 10/100 Ethernet MAC Controller (EMC) + +The NPCM7XX provides one or two Ethernet MAC RMII Controllers +for WAN/LAN applications + +Required properties: +- device_type : Should be "network" +- compatible : "nuvoton,npcm750-emc" for Poleg NPCM7XX. +- reg : Offset and length of the register set for the device. +- interrupts : Contain the emc interrupts with flags for falling edge. + first interrupt dedicated to Txirq + second interrupt dedicated to Rxirq +- phy-mode : Should be "rmii" (see ethernet.txt in the same directory) +- clocks : phandle of emc reference clock. +- resets : phandle to the reset control for this device. +- use-ncsi : Use the NC-SI stack instead of an MDIO PHY + +Example: + +emc0: eth@f0825000 { + device_type = "network"; + compatible = "nuvoton,npcm750-emc"; + reg = <0xf0825000 0x1000>; + interrupts = <GIC_SPI 16 IRQ_TYPE_LEVEL_HIGH>, + <GIC_SPI 15 IRQ_TYPE_LEVEL_HIGH>; + phy-mode = "rmii"; + clocks = <&clk NPCM7XX_CLK_AHB>; + + #use-ncsi; /* add this to support ncsi */ + + clock-names = "clk_emc"; + resets = <&rstc 6>; + pinctrl-names = "default"; + pinctrl-0 = <&r1_pins + &r1err_pins + &r1md_pins>; + status = "okay"; +};
Signature · 1 line
2.18.0
Avi Fishman
maintainerPATCH 2/2 net: npcm: add NPCM7xx EMC 10/100 Ethernet driver
In reply to Avi FishmanEMC Ethernet Media Access Controller supports 10/100 Mbps and RMII.
This driver has been working on Nuvoton BMC NPCM7xx.
--- drivers/net/ethernet/nuvoton/Kconfig | 21 +- drivers/net/ethernet/nuvoton/Makefile | 2 + drivers/net/ethernet/nuvoton/npcm7xx_emc.c | 2073 ++++++++++++++++++++ 3 files changed, 2093 insertions(+), 3 deletions(-) create mode 100644 drivers/net/ethernet/nuvoton/npcm7xx_emc.c
drivers/net/ethernet/nuvoton/Kconfig +18 -3
@@ -6,8 +6,8 @@ config NET_VENDOR_NUVOTON bool "Nuvoton devices" default y - depends on ARM && ARCH_W90X900 - ---help--- + depends on ARM && (ARCH_W90X900 || ARCH_NPCM7XX) + help If you have a network (Ethernet) card belonging to this class, say Y. Note that the answer to this question doesn't directly affect the
@@ -22,8 +22,23 @@ config W90P910_ETH depends on ARM && ARCH_W90X900 select PHYLIB select MII - ---help--- + help Say Y here if you want to use built-in Ethernet ports on w90p910 processor. +config NPCM7XX_EMC_ETH + bool "Nuvoton NPCM7XX Ethernet EMC" + depends on ARM && ARCH_NPCM7XX + select PHYLIB + select MII + help + Say Y here if you want to use built-in Ethernet MAC + on NPCM750 MCU. + +config NPCM7XX_EMC_ETH_DEBUG + bool "Nuvoton NPCM7XX Ethernet EMC debug" + depends on NPCM7XX_EMC_ETH + help + Say Y here if you want debug info via /proc/driver/npcm7xx_emc.x + endif # NET_VENDOR_NUVOTON
drivers/net/ethernet/nuvoton/Makefile +2 -0
@@ -3,4 +3,6 @@ # Makefile for the Nuvoton network device drivers. # +#Eternet 10/100 EMC obj-$(CONFIG_W90P910_ETH) += w90p910_ether.o +obj-$(CONFIG_NPCM7XX_EMC_ETH) += npcm7xx_emc.o
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +2073 -0
2,073 lines not on this page · raw message
Signature · 1 line
2.18.0
David Miller
maintainerRe: [PATCH v1 2/2] net: npcm: add NPCM7xx EMC 10/100 Ethernet driver
From: Avi Fishman <[email protected]>
Date: Thu, 1 Aug 2019 10:26:11 +0300
Quoted 1 line
+#Eternet 10/100 EMC
"Ethernet"
drivers/net/ethernet/nuvoton/npcm7xx_emc.c line 6
Quoted 3 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +3
-0+#ifdef CONFIG_NPCM7XX_EMC_ETH_DEBUG +#define DEBUG +#endif
Please don't control the DEBUG define in this way.
drivers/net/ethernet/nuvoton/npcm7xx_emc.c line 300
Quoted 5 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +5
-0+#if definedCONFIG_NPCM7XX_EMC_ETH_DEBUG || definedCONFIG_DEBUG_FS +#define REG_PRINT (reg_name) {t = scnprintf(next, size, "%-10s = %08X\n", \ + #reg_name, readl(ether->reg + (reg_name))); size -= t; next += t; } +#define DUMP_PRINT(f, x...) {t = scnprintf(next, size, f, ## x); size -= t; \ + next += t; }
Really, get rid of this custom debugging infrastructure and just use generic facilities the kernel has for this, as designed.
drivers/net/ethernet/nuvoton/npcm7xx_emc.c line 312
Quoted 11 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +11
-0+static int npcm7xx_info_dump (char *buf,int count,struct net_device *netdev) +{ +struct npcm7xx_ether *ether =netdev_priv (netdev); +struct npcm7xx_txbd *txbd; +struct npcm7xx_rxbd *rxbd; +unsigned long flags; +unsigned int i, cur, txd_offset, rxd_offset; +char *next = buf; +unsigned int size = count; +int t; +int is_locked =spin_is_locked (ðer->lock);
Reverse christmas tree (longest to shortest) ordering for local variables please.
Audit your entire submission for this problem.
Re: [PATCH v1 2/2] net: npcm: add NPCM7xx EMC 10/100 Ethernet driver
In reply to Avi FishmanQuoted 16 lines · Avi Fishman
EMC Ethernet Media Access Controller supports 10/100 Mbps and RMII.
This driver has been working on Nuvoton BMC NPCM7xx.Signed-off-by Avi Fishman <[email protected]>drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +7
-0+/* global setting for driver */ +#define RX_QUEUE_LEN 128 +#define TX_QUEUE_LEN 64 +#define MAX_RBUFF_SZ 0x600 +#define MAX_TBUFF_SZ 0x600 +#define TX_TIMEOUT 50 +#define DELAY 1000
drivers/net/ethernet/nuvoton/npcm7xx_emc.c line 207
Quoted 10 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +10
-0+#define CAM0 0x0 +#define RX_POLL_SIZE 16 + +#ifdef CONFIG_VLAN_8021Q +#define IS_VLAN 1 +#else +#define IS_VLAN 0 +#endif + +#define MAX_PACKET_SIZE (1514 + (IS_VLAN * 4))
1514 -> ETH_FRAME_LEN
4 -> VLAN_HLEN
Does this device support stacked VLAN?
Is this really the device maximum?
Quoted 1 line
+#define
MAX_PACKET_SIZE_W_CRC(MAX_PACKET_SIZE+ 4) /* 1518 */
4 -> ETH_FCS_LEN
Quoted 100 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +100
-0+#if definedCONFIG_NPCM7XX_EMC_ETH_DEBUG || definedCONFIG_DEBUG_FS +#define REG_PRINT (reg_name) {t = scnprintf(next, size, "%-10s = %08X\n", \ + #reg_name, readl(ether->reg + (reg_name))); size -= t; next += t; } +#define DUMP_PRINT(f, x...) {t = scnprintf(next, size, f, ## x); size -= t; \ + next += t; } + +static int npcm7xx_info_dump (char *buf,int count,struct net_device *netdev) +{ +struct npcm7xx_ether *ether =netdev_priv (netdev); +struct npcm7xx_txbd *txbd; +struct npcm7xx_rxbd *rxbd; +unsigned long flags; +unsigned int i, cur, txd_offset, rxd_offset; +char *next = buf; +unsigned int size = count; +int t; +int is_locked =spin_is_locked (ðer->lock); + +if (!is_locked) +spin_lock_irqsave (ðer->lock, flags); + +/* ------basic driver information ---- */ +DUMP_PRINT ("NPCM7XX EMC %s driver version: %s\n" , netdev->name, +DRV_MODULE_VERSION ); + +REG_PRINT (REG_CAMCMR ); +REG_PRINT (REG_CAMEN ); +REG_PRINT (REG_CAMM_BASE ); +REG_PRINT (REG_CAML_BASE ); +REG_PRINT (REG_TXDLSA ); +REG_PRINT (REG_RXDLSA ); +REG_PRINT (REG_MCMDR ); +REG_PRINT (REG_MIID ); +REG_PRINT (REG_MIIDA ); +REG_PRINT (REG_FFTCR ); +REG_PRINT (REG_TSDR ); +REG_PRINT (REG_RSDR ); +REG_PRINT (REG_DMARFC ); +REG_PRINT (REG_MIEN ); +REG_PRINT (REG_MISTA ); +REG_PRINT (REG_MGSTA ); +REG_PRINT (REG_MPCNT ); +writel (0x7FFF , (ether->reg +REG_MPCNT )); +REG_PRINT (REG_MRPC ); +REG_PRINT (REG_MRPCC ); +REG_PRINT (REG_MREPC ); +REG_PRINT (REG_DMARFS ); +REG_PRINT (REG_CTXDSA ); +REG_PRINT (REG_CTXBSA ); +REG_PRINT (REG_CRXDSA ); +REG_PRINT (REG_CRXBSA ); +REG_PRINT (REG_RXFSM ); +REG_PRINT (REG_TXFSM ); +REG_PRINT (REG_FSM0 ); +REG_PRINT (REG_FSM1 ); +REG_PRINT (REG_DCR ); +REG_PRINT (REG_DMMIR ); +REG_PRINT (REG_BISTR ); +DUMP_PRINT ("\n" ); + +DUMP_PRINT ("netif_queue %s\n\n" ,netif_queue_stopped (netdev) ? +"Stopped" :"Running" ); +if (ether->rdesc) +DUMP_PRINT ("napi is %s\n\n" ,test_bit (NAPI_STATE_SCHED , + ðer->napi.state) ? +"scheduled" : +"not scheduled" ); + + txd_offset = (readl ((ether->reg +REG_CTXDSA )) - +readl ((ether->reg +REG_TXDLSA ))) / +sizeof (struct npcm7xx_txbd ); +DUMP_PRINT ("TXD offset %6d\n" , txd_offset); +DUMP_PRINT ("cur_tx %6d\n" , ether->cur_tx); +DUMP_PRINT ("finish_tx %6d\n" , ether->finish_tx); +DUMP_PRINT ("pending_tx %6d\n" , ether->pending_tx); +/* debug counters */ +DUMP_PRINT ("tx_tdu %6d\n" , ether->tx_tdu); + ether->tx_tdu =0 ; +DUMP_PRINT ("tx_tdu_i %6d\n" , ether->tx_tdu_i); + ether->tx_tdu_i =0 ; +DUMP_PRINT ("tx_cp_i %6d\n" , ether->tx_cp_i); + ether->tx_cp_i =0 ; +DUMP_PRINT ("tx_int_count %6d\n" , ether->tx_int_count); + ether->tx_int_count =0 ; +DUMP_PRINT ("count_xmit tx %6d\n" , ether->count_xmit); + ether->count_xmit =0 ; +DUMP_PRINT ("count_finish %6d\n" , ether->count_finish); + ether->count_finish =0 ; +DUMP_PRINT ("\n" ); + + rxd_offset = (readl ((ether->reg +REG_CRXDSA )) - +readl ((ether->reg +REG_RXDLSA ))) + /sizeof (struct npcm7xx_txbd ); +DUMP_PRINT ("RXD offset %6d\n" , rxd_offset); +DUMP_PRINT ("cur_rx %6d\n" , ether->cur_rx); +DUMP_PRINT ("rx_err %6d\n" , ether->rx_err); + ether->rx_err =0 ; +DUMP_PRINT ("rx_berr %6d\n" , ether->rx_berr); + ether->rx_berr =0 ; +DUMP_PRINT ("rx_stuck %6d\n" , ether->rx_stuck);
drivers/net/ethernet/nuvoton/npcm7xx_emc.c line 405
Quoted 10 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +10
-0+ ether->rx_stuck =0 ; +DUMP_PRINT ("rdu %6d\n" , ether->rdu); + ether->rdu =0 ; +DUMP_PRINT ("rxov rx %6d\n" , ether->rxov); + ether->rxov =0 ; +/* debug counters */ +DUMP_PRINT ("rx_int_count %6d\n" , ether->rx_int_count); + ether->rx_int_count =0 ; +DUMP_PRINT ("rx_err_count %6d\n" , ether->rx_err_count); + ether->rx_err_count =0 ;
Basic counters like tx_packets and rx_errors are probably better exported regardless of debug level as net_device_stats. And then don't need to be copied in debug output.
Less standard counters like tx interrupt count are probably better candidates for ethtool -S.
Quoted 17 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +17
-0+#ifdef CONFIG_NPCM7XX_EMC_ETH_DEBUG +static void npcm7xx_info_print (struct net_device *netdev) +{ +char *emc_dump_buf; +int count; +struct npcm7xx_ether *ether; +struct platform_device *pdev; +char c; +char *tmp_buf; +const size_t print_size =5 *PAGE_SIZE ; + + ether =netdev_priv (netdev); + pdev = ether->pdev; + + emc_dump_buf =kmalloc (print_size,GFP_KERNEL ); +if (!emc_dump_buf) +return ;
drivers/net/ethernet/nuvoton/npcm7xx_emc.c line 498
Quoted 10 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +10
-0+ + tmp_buf = emc_dump_buf; + count =npcm7xx_info_dump (emc_dump_buf, print_size, netdev); +while (count >512 ) { + c = tmp_buf[512 ]; + tmp_buf[512 ] =0 ; +dev_info (&pdev->dev,"%s" , tmp_buf); + tmp_buf +=512 ; + tmp_buf[0 ] = c; + count -=512 ;
Missing closing parenthesis.
Also, why this buffering to printk?
drivers/net/ethernet/nuvoton/npcm7xx_emc.c line 681
Quoted 9 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +9
-0+static void npcm7xx_write_cam (struct net_device *netdev, +unsigned int x,unsigned char *pval) +{ +struct npcm7xx_ether *ether =netdev_priv (netdev); +__le32 msw, lsw; + + msw = (pval[0 ] <<24 ) | (pval[1 ] <<16 ) | (pval[2 ] <<8 ) | pval[3 ]; + + lsw = (pval[4 ] <<24 ) | (pval[5 ] <<16 );
Does __le32 plus this explicit shifting define host endianness? Better to keep independent?
Quoted 7 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +7
-0+ +writel (lsw, (ether->reg +REG_CAML_BASE ) + x *CAM_ENTRY_SIZE ); +writel (msw, (ether->reg +REG_CAMM_BASE ) + x *CAM_ENTRY_SIZE ); +dev_dbg (ðer->pdev->dev, +"REG_CAML_BASE = 0x%08X REG_CAMM_BASE = 0x%08X" , lsw, msw); +} +
drivers/net/ethernet/nuvoton/npcm7xx_emc.c line 698
Quoted 10 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +10
-0+static struct sk_buff *get_new_skb (struct net_device *netdev,u32 i) +{ +__le32 buffer; +struct npcm7xx_ether *ether =netdev_priv (netdev); +struct sk_buff *skb =netdev_alloc_skb (netdev, +roundup (MAX_PACKET_SIZE_W_CRC ,4 )); + +if (unlikely (!skb)) { +if (net_ratelimit ()) +netdev_warn (netdev,"failed to allocate rx skb\n" );
can use net_warn_ratelimited (here and elsewhere)
drivers/net/ethernet/nuvoton/npcm7xx_emc.c line 704
Quoted 6 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +6
-0+ buffer = ether->rx_scratch_dma; + }else { +/* Do not unmark the following skb_reserve() Receive Buffer +* Starting Address must be aligned to 4 bytes and the following +* line if unmarked will make it align to 2 and this likely will +* hult the RX and crash the linux
halt?
Quoted 30 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +29
-0+* skb_reserve(skb, NET_IP_ALIGN); +*/ + skb->dev = netdev; + buffer =dma_map_single (&netdev->dev, + skb->data, +roundup (MAX_PACKET_SIZE_W_CRC ,4 ), +DMA_FROM_DEVICE ); +if (unlikely (dma_mapping_error (&netdev->dev, buffer))) { +if (net_ratelimit ()) +netdev_err (netdev,"failed to map rx page\n" ); +dev_kfree_skb_any (skb); + buffer = ether->rx_scratch_dma; + skb =NULL ; + } + } + ether->rx_skb[i] = skb; + ether->rdesc[i].buffer = buffer; + +return skb; +} + +static int npcm7xx_ether_close (struct net_device *netdev) +{ +struct npcm7xx_ether *ether =netdev_priv (netdev); + +npcm7xx_return_default_idle (netdev); + +if (ether->phy_dev) +phy_stop (ether->phy_dev);
drivers/net/ethernet/nuvoton/npcm7xx_emc.c
Quoted 10 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +10
-0+else if (ether->use_ncsi) +ncsi_stop_dev (ether->ncsidev); + +msleep (20 ); + +free_irq (ether->txirq, netdev); +free_irq (ether->rxirq, netdev); + +netif_stop_queue (netdev); +napi_disable (ðer->napi);
Cleanup state in reverse of allocation.
Quoted 9 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +9
-0+static int npcm7xx_ether_start_xmit (struct sk_buff *skb,struct net_device *netdev) +{ +struct npcm7xx_ether *ether =netdev_priv (netdev); +struct npcm7xx_txbd *txbd; +unsigned long flags; + + ether->count_xmit++; + +/* Insert new buffer */
drivers/net/ethernet/nuvoton/npcm7xx_emc.c line 1246
Quoted 10 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +9
-0+ txbd = (ether->tdesc + ether->cur_tx); + txbd->buffer =dma_map_single (&netdev->dev, skb->data, skb->len, +DMA_TO_DEVICE ); + ether->tx_skb[ether->cur_tx] = skb; +if (skb->len >MAX_PACKET_SIZE ) +dev_err (ðer->pdev->dev, +"skb->len (= %d) > MAX_PACKET_SIZE (= %d)\n" , + skb->len,MAX_PACKET_SIZE ); + txbd->sl = skb->len >MAX_PACKET_SIZE ?MAX_PACKET_SIZE : skb->len;
Check for errors before mapping to device, and drop packet? Probably don't want to output truncated packets.
Also rate limit such messages.
Quoted 29 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +29
-0+dma_wmb (); + + txbd->mode =TX_OWN_DMA |PADDINGMODE |CRCMODE ; + +/* make sure that data is in memory before we trigger TX */ +wmb (); + +/* trigger TX */ +writel (ENSTART , (ether->reg +REG_TSDR )); + +if (++ether->cur_tx >=TX_QUEUE_LEN ) + ether->cur_tx =0 ; + +spin_lock_irqsave (ðer->lock, flags); + ether->pending_tx++; + +/* sometimes we miss the tx interrupt due to HW issue, so NAPI will not +* clean the pending tx, so we clean it also here +*/ +npcm7xx_clean_tx (netdev, true); + +if (ether->pending_tx >=TX_QUEUE_LEN -1 ) { +__le32 reg_mien; +unsigned int index_to_wake = ether->cur_tx + + ((TX_QUEUE_LEN *3 ) /4 ); + +if (index_to_wake >=TX_QUEUE_LEN ) + index_to_wake -=TX_QUEUE_LEN ; +
drivers/net/ethernet/nuvoton/npcm7xx_emc.c line 1285
Quoted 10 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +10
-0+ txbd = (ether->tdesc + index_to_wake); + txbd->mode =TX_OWN_DMA |PADDINGMODE |CRCMODE |MACTXINTEN ; + +/* make sure that data is in memory before we trigger TX */ +wmb (); + +/* Clear TDU interrupt */ +writel (MISTA_TDU , (ether->reg +REG_MISTA )); + +/* due to HW issue somtimes, we miss the TX interrupt we just
somtimes -> sometimes
Quoted 33 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +33
-0+* set (MACTXINTEN), so we also set TDU for Transmit +* Descriptor Unavailable interrupt +*/ + reg_mien =readl ((ether->reg +REG_MIEN )); +if (reg_mien !=0 ) +/* Enable TDU interrupt */ +writel (reg_mien |ENTDU , (ether->reg +REG_MIEN )); + + ether->tx_tdu++; +netif_stop_queue (netdev); + } + +spin_unlock_irqrestore (ðer->lock, flags); + +return 0 ; +} + +static irqreturn_t npcm7xx_tx_interrupt (int irq,void *dev_id) +{ +struct npcm7xx_ether *ether; +struct platform_device *pdev; +struct net_device *netdev; +__le32 status; +unsigned long flags; + + netdev = dev_id; + ether =netdev_priv (netdev); + pdev = ether->pdev; + +npcm7xx_get_and_clear_int (netdev, &status,0xFFFF0000 ); + + ether->tx_int_count++; +
drivers/net/ethernet/nuvoton/npcm7xx_emc.c line 1328
Quoted 10 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +10
-0+if (status &MISTA_EXDEF ) +dev_err (&pdev->dev,"emc defer exceed interrupt status=0x%08X\n" + , status); +else if (status &MISTA_TXBERR ) { +dev_err (&pdev->dev,"emc bus error interrupt status=0x%08X\n" , + status); +#ifdef CONFIG_NPCM7XX_EMC_ETH_DEBUG +npcm7xx_info_print (netdev); +#endif +spin_lock_irqsave (ðer->lock, flags);
irqsave in hard interrupt context?
drivers/net/ethernet/nuvoton/npcm7xx_emc.c line 1336
Quoted 8 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +8
-0+writel (0 , (ether->reg +REG_MIEN ));/* disable any interrupt */ +spin_unlock_irqrestore (ðer->lock, flags); + ether->need_reset =1 ; + }else if (status & ~(MISTA_TXINTR |MISTA_TXCP |MISTA_TDU )) +dev_err (&pdev->dev,"emc other error interrupt status=0x%08X\n" , + status); + +/* if we got MISTA_TXCP | MISTA_TDU remove those interrupt and call napi */
The goal of napi is to keep interrupts disabled until napi completes.
Quoted 26 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +26
-0+if (status & (MISTA_TXCP |MISTA_TDU ) & +readl ((ether->reg +REG_MIEN ))) { +__le32 reg_mien; + +spin_lock_irqsave (ðer->lock, flags); + reg_mien =readl ((ether->reg +REG_MIEN )); +if (reg_mien &ENTDU ) +/* Disable TDU interrupt */ +writel (reg_mien & (~ENTDU ), (ether->reg +REG_MIEN )); + +spin_unlock_irqrestore (ðer->lock, flags); + +if (status &MISTA_TXCP ) + ether->tx_cp_i++; +if (status &MISTA_TDU ) + ether->tx_tdu_i++; + }else { +dev_dbg (&pdev->dev,"status=0x%08X\n" , status); + } + +napi_schedule (ðer->napi); + +return IRQ_HANDLED ; +} + +static irqreturn_t npcm7xx_rx_interrupt (int irq,void *dev_id)
drivers/net/ethernet/nuvoton/npcm7xx_emc.c line 1372
Quoted 10 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +10
-0+{ +struct net_device *netdev = (struct net_device *)dev_id; +struct npcm7xx_ether *ether =netdev_priv (netdev); +struct platform_device *pdev = ether->pdev; +__le32 status; +unsigned long flags; +unsigned int any_err =0 ; +__le32 rxfsm; + +npcm7xx_get_and_clear_int (netdev, &status,0xFFFF );
Same here
Quoted 15 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +15
-0+static int npcm7xx_poll (struct napi_struct *napi,int budget) +{ +struct npcm7xx_ether *ether = + container_of(napi, struct npcm7xx_ether, napi); +struct npcm7xx_rxbd *rxbd; +struct net_device *netdev = ether->netdev; +struct platform_device *pdev = ether->pdev; +struct sk_buff *s; +unsigned int length; +__le32 status; +unsigned long flags; +int rx_cnt =0 ; +int complete =0 ; +unsigned int rx_offset = (readl ((ether->reg +REG_CRXDSA )) - + ether->start_rx_ptr) /
drivers/net/ethernet/nuvoton/npcm7xx_emc.c line 1490
Quoted 10 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +10
-0+sizeof (struct npcm7xx_txbd ); +unsigned int local_count = (rx_offset >= ether->cur_rx) ? + rx_offset - ether->cur_rx : rx_offset + +RX_QUEUE_LEN - ether->cur_rx; + +if (local_count > ether->max_waiting_rx) + ether->max_waiting_rx = local_count; + +if (local_count > (4 *RX_POLL_SIZE )) +/* we are porbably in a storm of short packets and we don't
porbably - probably
Quoted 13 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +13
-0+* want to get into RDU since short packets in RDU cause +* many RXOV which may cause EMC halt, so we filter out all +* coming packets +*/ +writel (0 , (ether->reg +REG_CAMCMR )); + +if (local_count <= budget) +/* we can restore accepting of packets */ +writel (ether->camcmr, (ether->reg +REG_CAMCMR )); + +spin_lock_irqsave (ðer->lock, flags); +npcm7xx_clean_tx (netdev, false); +spin_unlock_irqrestore (ðer->lock, flags);
drivers/net/ethernet/nuvoton/npcm7xx_emc.c line 1513
Quoted 10 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +10
-0+ + rxbd = (ether->rdesc + ether->cur_rx); + +while (rx_cnt < budget) { + status = rxbd->sl; +if ((status &RX_OWN_DMA ) ==RX_OWN_DMA ) { + complete =1 ; +break ; + } +/* for debug puposes we save the previous value */
puposes -> purposes
drivers/net/ethernet/nuvoton/npcm7xx_emc.c line 1518
Quoted 5 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +5
-0+ rxbd->reserved = status; + s = ether->rx_skb[ether->cur_rx]; + length = status &0xFFFF ; + +/* If VLAN is not supporte RXDS_PTLE (packet too long) is also
supporte -> supported (stopping pointing out typos after this).
drivers/net/ethernet/nuvoton/npcm7xx_emc.c line 1759
Quoted 10 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +10
-0+static const struct net_device_ops npcm7xx_ether_netdev_ops = { + .ndo_open = npcm7xx_ether_open, + .ndo_stop = npcm7xx_ether_close, + .ndo_start_xmit = npcm7xx_ether_start_xmit, + .ndo_get_stats = npcm7xx_ether_stats, + .ndo_set_rx_mode = npcm7xx_ether_set_rx_mode, + .ndo_set_mac_address = npcm7xx_set_mac_address, + .ndo_do_ioctl = npcm7xx_ether_ioctl, + .ndo_validate_addr = eth_validate_addr, + .ndo_change_mtu = eth_change_mtu,
This is marked as deprecated. Also in light of the hardcoded MAX_PACKET_SIZE, probably want to set dev->max_mtu.
Quoted 24 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +24
-0+static int npcm7xx_ether_probe (struct platform_device *pdev) +{ +struct npcm7xx_ether *ether; +struct net_device *netdev; +int error; + +struct clk *emc_clk =NULL ; +struct device_node *np = pdev->dev.of_node; + + pdev->id =of_alias_get_id (np,"ethernet" ); +if (pdev->id <0 ) + pdev->id =0 ; + + emc_clk =devm_clk_get (&pdev->dev,NULL ); + +if (IS_ERR (emc_clk)) +return PTR_ERR (emc_clk); + +/* Enable Clock */ +clk_prepare_enable (emc_clk); + + error =dma_coerce_mask_and_coherent (&pdev->dev,DMA_BIT_MASK (32 )); +if (error) +return -ENODEV ;
drivers/net/ethernet/nuvoton/npcm7xx_emc.c line 1912
Quoted 10 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +10
-0+ + netdev =alloc_etherdev (sizeof (struct npcm7xx_ether )); +if (!netdev) +return -ENOMEM ; + + ether =netdev_priv (netdev); + + ether->reset =devm_reset_control_get (&pdev->dev,NULL ); +if (IS_ERR (ether->reset)) +return PTR_ERR (ether->reset);
Memory leak on error path
Also missing netif_napi_del in npcm7xx_ether_remove?
Avi Fishman
maintainerRe: [PATCH v1 2/2] net: npcm: add NPCM7xx EMC 10/100 Ethernet driver
Thanks for the input Willem,
Before I will submit a new version please help me with some questions:
Quoted 31 lines · Willem de Bruijn
Quoted 16 lines · Avi Fishman
EMC Ethernet Media Access Controller supports 10/100 Mbps and RMII.
This driver has been working on Nuvoton BMC NPCM7xx.Signed-off-by Avi Fishman <[email protected]>drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +7
-0+/* global setting for driver */ +#define RX_QUEUE_LEN 128 +#define TX_QUEUE_LEN 64 +#define MAX_RBUFF_SZ 0x600 +#define MAX_TBUFF_SZ 0x600 +#define TX_TIMEOUT 50 +#define DELAY 1000drivers/net/ethernet/nuvoton/npcm7xx_emc.c line 207
Quoted 10 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +10
-0+#define CAM0 0x0 +#define RX_POLL_SIZE 16 + +#ifdef CONFIG_VLAN_8021Q +#define IS_VLAN 1 +#else +#define IS_VLAN 0 +#endif + +#define MAX_PACKET_SIZE (1514 + (IS_VLAN * 4))1514 ->
ETH_FRAME_LEN
Quoted 1 line
4 ->
VLAN_HLEN
OK
Quoted 2 lines
Does this device support stacked VLAN?
I am not familiar with stacked VLAN.
Our HW for sure there is no support. can the SW stack handle it for me? Does it mean that the packets can be larger?
Quoted 2 lines
Is this really the device maximum?
The device can support upto 64KB, but of course I will not allocate for each RX data such a big buffer.
Can I know what is the maximum value the network stack may request? I saw many driver allocating 1536 for each packet.
Quoted 4 lines
Quoted 2 lines
+#define
MAX_PACKET_SIZE_W_CRC(MAX_PACKET_SIZE+ 4) /* 1518 */4 ->
ETH_FCS_LEN
OK
Quoted 107 lines
Quoted 107 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +106
-0+#if definedCONFIG_NPCM7XX_EMC_ETH_DEBUG || definedCONFIG_DEBUG_FS +#define REG_PRINT (reg_name) {t = scnprintf(next, size, "%-10s = %08X\n", \ + #reg_name, readl(ether->reg + (reg_name))); size -= t; next += t; } +#define DUMP_PRINT(f, x...) {t = scnprintf(next, size, f, ## x); size -= t; \ + next += t; } + +static int npcm7xx_info_dump (char *buf,int count,struct net_device *netdev) +{ +struct npcm7xx_ether *ether =netdev_priv (netdev); +struct npcm7xx_txbd *txbd; +struct npcm7xx_rxbd *rxbd; +unsigned long flags; +unsigned int i, cur, txd_offset, rxd_offset; +char *next = buf; +unsigned int size = count; +int t; +int is_locked =spin_is_locked (ðer->lock); + +if (!is_locked) +spin_lock_irqsave (ðer->lock, flags); + +/* ------basic driver information ---- */ +DUMP_PRINT ("NPCM7XX EMC %s driver version: %s\n" , netdev->name, +DRV_MODULE_VERSION ); + +REG_PRINT (REG_CAMCMR ); +REG_PRINT (REG_CAMEN ); +REG_PRINT (REG_CAMM_BASE ); +REG_PRINT (REG_CAML_BASE ); +REG_PRINT (REG_TXDLSA ); +REG_PRINT (REG_RXDLSA ); +REG_PRINT (REG_MCMDR ); +REG_PRINT (REG_MIID ); +REG_PRINT (REG_MIIDA ); +REG_PRINT (REG_FFTCR ); +REG_PRINT (REG_TSDR ); +REG_PRINT (REG_RSDR ); +REG_PRINT (REG_DMARFC ); +REG_PRINT (REG_MIEN ); +REG_PRINT (REG_MISTA ); +REG_PRINT (REG_MGSTA ); +REG_PRINT (REG_MPCNT ); +writel (0x7FFF , (ether->reg +REG_MPCNT )); +REG_PRINT (REG_MRPC ); +REG_PRINT (REG_MRPCC ); +REG_PRINT (REG_MREPC ); +REG_PRINT (REG_DMARFS ); +REG_PRINT (REG_CTXDSA ); +REG_PRINT (REG_CTXBSA ); +REG_PRINT (REG_CRXDSA ); +REG_PRINT (REG_CRXBSA ); +REG_PRINT (REG_RXFSM ); +REG_PRINT (REG_TXFSM ); +REG_PRINT (REG_FSM0 ); +REG_PRINT (REG_FSM1 ); +REG_PRINT (REG_DCR ); +REG_PRINT (REG_DMMIR ); +REG_PRINT (REG_BISTR ); +DUMP_PRINT ("\n" ); + +DUMP_PRINT ("netif_queue %s\n\n" ,netif_queue_stopped (netdev) ? +"Stopped" :"Running" ); +if (ether->rdesc) +DUMP_PRINT ("napi is %s\n\n" ,test_bit (NAPI_STATE_SCHED , + ðer->napi.state) ? +"scheduled" : +"not scheduled" ); + + txd_offset = (readl ((ether->reg +REG_CTXDSA )) - +readl ((ether->reg +REG_TXDLSA ))) / +sizeof (struct npcm7xx_txbd ); +DUMP_PRINT ("TXD offset %6d\n" , txd_offset); +DUMP_PRINT ("cur_tx %6d\n" , ether->cur_tx); +DUMP_PRINT ("finish_tx %6d\n" , ether->finish_tx); +DUMP_PRINT ("pending_tx %6d\n" , ether->pending_tx); +/* debug counters */ +DUMP_PRINT ("tx_tdu %6d\n" , ether->tx_tdu); + ether->tx_tdu =0 ; +DUMP_PRINT ("tx_tdu_i %6d\n" , ether->tx_tdu_i); + ether->tx_tdu_i =0 ; +DUMP_PRINT ("tx_cp_i %6d\n" , ether->tx_cp_i); + ether->tx_cp_i =0 ; +DUMP_PRINT ("tx_int_count %6d\n" , ether->tx_int_count); + ether->tx_int_count =0 ; +DUMP_PRINT ("count_xmit tx %6d\n" , ether->count_xmit); + ether->count_xmit =0 ; +DUMP_PRINT ("count_finish %6d\n" , ether->count_finish); + ether->count_finish =0 ; +DUMP_PRINT ("\n" ); + + rxd_offset = (readl ((ether->reg +REG_CRXDSA )) - +readl ((ether->reg +REG_RXDLSA ))) + /sizeof (struct npcm7xx_txbd ); +DUMP_PRINT ("RXD offset %6d\n" , rxd_offset); +DUMP_PRINT ("cur_rx %6d\n" , ether->cur_rx); +DUMP_PRINT ("rx_err %6d\n" , ether->rx_err); + ether->rx_err =0 ; +DUMP_PRINT ("rx_berr %6d\n" , ether->rx_berr); + ether->rx_berr =0 ; +DUMP_PRINT ("rx_stuck %6d\n" , ether->rx_stuck); + ether->rx_stuck =0 ; +DUMP_PRINT ("rdu %6d\n" , ether->rdu); + ether->rdu =0 ; +DUMP_PRINT ("rxov rx %6d\n" , ether->rxov); + ether->rxov =0 ; +/* debug counters */
Quoted 8 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c line 405
Quoted 4 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +4
-0+DUMP_PRINT ("rx_int_count %6d\n" , ether->rx_int_count); + ether->rx_int_count =0 ; +DUMP_PRINT ("rx_err_count %6d\n" , ether->rx_err_count); + ether->rx_err_count =0 ;Basic counters like
tx_packetsandrx_errorsare probably better exported regardless of debug level asnet_device_stats. And then don't need to be copied in debug output.
They are also exported there, see below ether->stats.tx_packets++; and ether->stats.rx_errors++;
those are different counters for debug since we had HW issues that we needed to workaround and might need them for future use. Currently the driver is stable on millions of parts in the field.
Quoted 3 lines
Less standard counters like tx interrupt count are probably better candidates for ethtool -S.
I don't have support for ethtool.
is it a must? it is quite some work and this driver is already in the field for quite some years.
Quoted 11 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c line 481
Quoted 11 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +10
-0+#ifdef CONFIG_NPCM7XX_EMC_ETH_DEBUG +static void npcm7xx_info_print (struct net_device *netdev) +{ +char *emc_dump_buf; +int count; +struct npcm7xx_ether *ether; +struct platform_device *pdev; +char c; +char *tmp_buf; +const size_t print_size =5 *PAGE_SIZE ;
I will change print_size to 0x5000 since PAGE_SIZE is not a fixed value on all arch.
Quoted 11 lines
Quoted 11 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +11
-0+ + ether =netdev_priv (netdev); + pdev = ether->pdev; + + emc_dump_buf =kmalloc (print_size,GFP_KERNEL ); +if (!emc_dump_buf) +return ; + + tmp_buf = emc_dump_buf; + count = ether->stats.rx_errors++;(emc_dump_buf, print_size, netdev); +while (count >512 ) {
Quoted 8 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c line 498
Quoted 6 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +6
-0+ c = tmp_buf[512 ]; + tmp_buf[512 ] =0 ; +dev_info (&pdev->dev,"%s" , tmp_buf); + tmp_buf +=512 ; + tmp_buf[0 ] = c; + count -=512 ;Missing closing parenthesis.
WOW! good catch, before submitting I made few change due to checkpatch.pl and didn't compile with CONFIG_NPCM7XX_EMC_ETH_DEBUG
Quoted 2 lines
Also, why this buffering to printk?
I prepare the buffer in advance for real-time reasons because of the lock in order to get a correct snapshot of the registers.
The buffer is big and I saw that printk has limited length so I split it.
Quoted 13 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c line 681
Quoted 10 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +9
-0+static void npcm7xx_write_cam (struct net_device *netdev, +unsigned int x,unsigned char *pval) +{ +struct npcm7xx_ether *ether =netdev_priv (netdev); +__le32 msw, lsw; + + msw = (pval[0 ] <<24 ) | (pval[1 ] <<16 ) | (pval[2 ] <<8 ) | pval[3 ]; + + lsw = (pval[4 ] <<24 ) | (pval[5 ] <<16 );Does
__le32plus this explicit shifting define host endianness? Better to keep independent?
OK
Quoted 12 lines
Quoted 12 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +11
-0+ +writel (lsw, (ether->reg +REG_CAML_BASE ) + x *CAM_ENTRY_SIZE ); +writel (msw, (ether->reg +REG_CAMM_BASE ) + x *CAM_ENTRY_SIZE ); +dev_dbg (ðer->pdev->dev, +"REG_CAML_BASE = 0x%08X REG_CAMM_BASE = 0x%08X" , lsw, msw); +} + +static struct sk_buff *get_new_skb (struct net_device *netdev,u32 i) +{ +__le32 buffer; +struct npcm7xx_ether *ether =netdev_priv (netdev);
Quoted 8 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c line 698
Quoted 6 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +6
-0+struct sk_buff *skb =netdev_alloc_skb (netdev, +roundup (MAX_PACKET_SIZE_W_CRC ,4 )); + +if (unlikely (!skb)) { +if (net_ratelimit ()) +netdev_warn (netdev,"failed to allocate rx skb\n" );can use
net_warn_ratelimited(here and elsewhere)
should I replace every netdev_warn/err/info with net_warn/err/inf_ratelimited? I saw in drivers that are using net_warn_ratelimited, that many time uses also netdev_warn/err/info
Quoted 9 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c line 704
Quoted 7 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +6
-0+ buffer = ether->rx_scratch_dma; + }else { +/* Do not unmark the following skb_reserve() Receive Buffer +* Starting Address must be aligned to 4 bytes and the following +* line if unmarked will make it align to 2 and this likely will +* hult the RX and crash the linux halt?
will fix typo.
Quoted 35 lines
Quoted 35 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +33
-0+* skb_reserve(skb, NET_IP_ALIGN); +*/ + skb->dev = netdev; + buffer =dma_map_single (&netdev->dev, + skb->data, +roundup (MAX_PACKET_SIZE_W_CRC ,4 ), +DMA_FROM_DEVICE ); +if (unlikely (dma_mapping_error (&netdev->dev, buffer))) { +if (net_ratelimit ()) +netdev_err (netdev,"failed to map rx page\n" ); +dev_kfree_skb_any (skb); + buffer = ether->rx_scratch_dma; + skb =NULL ; + } + } + ether->rx_skb[i] = skb; + ether->rdesc[i].buffer = buffer; + +return skb; +} + +static int npcm7xx_ether_close (struct net_device *netdev) +{ +struct npcm7xx_ether *ether =netdev_priv (netdev); + +npcm7xx_return_default_idle (netdev); + +if (ether->phy_dev) +phy_stop (ether->phy_dev); +else if (ether->use_ncsi) +ncsi_stop_dev (ether->ncsidev); + +msleep (20 );
Quoted 8 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c line 1151
Quoted 6 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +6
-0+ +free_irq (ether->txirq, netdev); +free_irq (ether->rxirq, netdev); + +netif_stop_queue (netdev); +napi_disable (ðer->napi);Cleanup state in reverse of allocation.
OK
Quoted 19 lines
Quoted 18 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +17
-0+static int npcm7xx_ether_start_xmit (struct sk_buff *skb,struct net_device *netdev) +{ +struct npcm7xx_ether *ether =netdev_priv (netdev); +struct npcm7xx_txbd *txbd; +unsigned long flags; + + ether->count_xmit++; + +/* Insert new buffer */ + txbd = (ether->tdesc + ether->cur_tx); + txbd->buffer =dma_map_single (&netdev->dev, skb->data, skb->len, +DMA_TO_DEVICE ); + ether->tx_skb[ether->cur_tx] = skb; +if (skb->len >MAX_PACKET_SIZE ) +dev_err (ðer->pdev->dev, +"skb->len (= %d) > MAX_PACKET_SIZE (= %d)\n" , + skb->len,MAX_PACKET_SIZE );
Quoted 6 lines
Quoted 1 line
+
txbd->sl=skb->len>MAX_PACKET_SIZE?MAX_PACKET_SIZE:skb->len;Check for errors before mapping to device, and drop packet? Probably don't want to output truncated packets.
Also rate limit such messages.
OK
Quoted 34 lines
Quoted 34 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +33
-0+dma_wmb (); + + txbd->mode =TX_OWN_DMA |PADDINGMODE |CRCMODE ; + +/* make sure that data is in memory before we trigger TX */ +wmb (); + +/* trigger TX */ +writel (ENSTART , (ether->reg +REG_TSDR )); + +if (++ether->cur_tx >=TX_QUEUE_LEN ) + ether->cur_tx =0 ; + +spin_lock_irqsave (ðer->lock, flags); + ether->pending_tx++; + +/* sometimes we miss the tx interrupt due to HW issue, so NAPI will not +* clean the pending tx, so we clean it also here +*/ +npcm7xx_clean_tx (netdev, true); + +if (ether->pending_tx >=TX_QUEUE_LEN -1 ) { +__le32 reg_mien; +unsigned int index_to_wake = ether->cur_tx + + ((TX_QUEUE_LEN *3 ) /4 ); + +if (index_to_wake >=TX_QUEUE_LEN ) + index_to_wake -=TX_QUEUE_LEN ; + + txbd = (ether->tdesc + index_to_wake); + txbd->mode =TX_OWN_DMA |PADDINGMODE |CRCMODE |MACTXINTEN ; + +/* make sure that data is in memory before we trigger TX */
Quoted 8 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c line 1285
Quoted 6 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +6
-0+wmb (); + +/* Clear TDU interrupt */ +writel (MISTA_TDU , (ether->reg +REG_MISTA )); + +/* due to HW issue somtimes, we miss the TX interrupt we just somtimes -> sometimes
OK
Quoted 38 lines
Quoted 38 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +37
-0+* set (MACTXINTEN), so we also set TDU for Transmit +* Descriptor Unavailable interrupt +*/ + reg_mien =readl ((ether->reg +REG_MIEN )); +if (reg_mien !=0 ) +/* Enable TDU interrupt */ +writel (reg_mien |ENTDU , (ether->reg +REG_MIEN )); + + ether->tx_tdu++; +netif_stop_queue (netdev); + } + +spin_unlock_irqrestore (ðer->lock, flags); + +return 0 ; +} + +static irqreturn_t npcm7xx_tx_interrupt (int irq,void *dev_id) +{ +struct npcm7xx_ether *ether; +struct platform_device *pdev; +struct net_device *netdev; +__le32 status; +unsigned long flags; + + netdev = dev_id; + ether =netdev_priv (netdev); + pdev = ether->pdev; + +npcm7xx_get_and_clear_int (netdev, &status,0xFFFF0000 ); + + ether->tx_int_count++; + +if (status &MISTA_EXDEF ) +dev_err (&pdev->dev,"emc defer exceed interrupt status=0x%08X\n" + , status); +else if (status &MISTA_TXBERR ) {
Quoted 8 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c line 1328
Quoted 6 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +6
-0+dev_err (&pdev->dev,"emc bus error interrupt status=0x%08X\n" , + status); +#ifdef CONFIG_NPCM7XX_EMC_ETH_DEBUG +npcm7xx_info_print (netdev); +#endif +spin_lock_irqsave (ðer->lock, flags);irqsave in hard interrupt context?
I need to protect my REG_MIEN register that is changed in other places. I think that spin_lock_irqsave() is relevant when working in SMP, since other CPU may still be running.
Isn't it?
Quoted 11 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c line 1336
Quoted 9 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +8
-0+writel (0 , (ether->reg +REG_MIEN ));/* disable any interrupt */ +spin_unlock_irqrestore (ðer->lock, flags); + ether->need_reset =1 ; + }else if (status & ~(MISTA_TXINTR |MISTA_TXCP |MISTA_TDU )) +dev_err (&pdev->dev,"emc other error interrupt status=0x%08X\n" , + status); + +/* if we got MISTA_TXCP | MISTA_TDU remove those interrupt and call napi */ The goal of napi is to keep interrupts disabled until napi completes.
We have a HW issue that because of it I still enabled TX complete interrupt, I will see if I can disable all interrupts and only leave the error interrupts
Quoted 31 lines
Quoted 31 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +30
-0+if (status & (MISTA_TXCP |MISTA_TDU ) & +readl ((ether->reg +REG_MIEN ))) { +__le32 reg_mien; + +spin_lock_irqsave (ðer->lock, flags); + reg_mien =readl ((ether->reg +REG_MIEN )); +if (reg_mien &ENTDU ) +/* Disable TDU interrupt */ +writel (reg_mien & (~ENTDU ), (ether->reg +REG_MIEN )); + +spin_unlock_irqrestore (ðer->lock, flags); + +if (status &MISTA_TXCP ) + ether->tx_cp_i++; +if (status &MISTA_TDU ) + ether->tx_tdu_i++; + }else { +dev_dbg (&pdev->dev,"status=0x%08X\n" , status); + } + +napi_schedule (ðer->napi); + +return IRQ_HANDLED ; +} + +static irqreturn_t npcm7xx_rx_interrupt (int irq,void *dev_id) +{ +struct net_device *netdev = (struct net_device *)dev_id; +struct npcm7xx_ether *ether =netdev_priv (netdev); +struct platform_device *pdev = ether->pdev;
Quoted 8 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c line 1372
Quoted 6 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +6
-0+__le32 status; +unsigned long flags; +unsigned int any_err =0 ; +__le32 rxfsm; + +npcm7xx_get_and_clear_int (netdev, &status,0xFFFF );Same here
in non error case I do leave only the error interrupts and schedule napi.
Quoted 20 lines
Quoted 20 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +19
-0+static int npcm7xx_poll (struct napi_struct *napi,int budget) +{ +struct npcm7xx_ether *ether = + container_of(napi, struct npcm7xx_ether, napi); +struct npcm7xx_rxbd *rxbd; +struct net_device *netdev = ether->netdev; +struct platform_device *pdev = ether->pdev; +struct sk_buff *s; +unsigned int length; +__le32 status; +unsigned long flags; +int rx_cnt =0 ; +int complete =0 ; +unsigned int rx_offset = (readl ((ether->reg +REG_CRXDSA )) - + ether->start_rx_ptr) / +sizeof (struct npcm7xx_txbd ); +unsigned int local_count = (rx_offset >= ether->cur_rx) ? + rx_offset - ether->cur_rx : rx_offset + +RX_QUEUE_LEN - ether->cur_rx;
Quoted 8 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c line 1490
Quoted 6 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +6
-0+ +if (local_count > ether->max_waiting_rx) + ether->max_waiting_rx = local_count; + +if (local_count > (4 *RX_POLL_SIZE )) +/* we are porbably in a storm of short packets and we don't porbably - probably
OK
Quoted 18 lines
Quoted 18 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +17
-0+* want to get into RDU since short packets in RDU cause +* many RXOV which may cause EMC halt, so we filter out all +* coming packets +*/ +writel (0 , (ether->reg +REG_CAMCMR )); + +if (local_count <= budget) +/* we can restore accepting of packets */ +writel (ether->camcmr, (ether->reg +REG_CAMCMR )); + +spin_lock_irqsave (ðer->lock, flags); +npcm7xx_clean_tx (netdev, false); +spin_unlock_irqrestore (ðer->lock, flags); + + rxbd = (ether->rdesc + ether->cur_rx); + +while (rx_cnt < budget) {
Quoted 8 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c line 1513
Quoted 6 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +6
-0+ status = rxbd->sl; +if ((status &RX_OWN_DMA ) ==RX_OWN_DMA ) { + complete =1 ; +break ; + } +/* for debug puposes we save the previous value */ puposes -> purposes
OK
Quoted 8 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c line 1518
Quoted 6 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +5
-0+ rxbd->reserved = status; + s = ether->rx_skb[ether->cur_rx]; + length = status &0xFFFF ; + +/* If VLAN is not supporte RXDS_PTLE (packet too long) is also supporte -> supported (stopping pointing out typos after this).
OK will review rest
Quoted 12 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c line 1759
Quoted 11 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +10
-0+static const struct net_device_ops npcm7xx_ether_netdev_ops = { + .ndo_open = npcm7xx_ether_open, + .ndo_stop = npcm7xx_ether_close, + .ndo_start_xmit = npcm7xx_ether_start_xmit, + .ndo_get_stats = npcm7xx_ether_stats, + .ndo_set_rx_mode = npcm7xx_ether_set_rx_mode, + .ndo_set_mac_address = npcm7xx_set_mac_address, + .ndo_do_ioctl = npcm7xx_ether_ioctl, + .ndo_validate_addr = eth_validate_addr, + .ndo_change_mtu = eth_change_mtu,
Quoted 2 lines
This is marked as deprecated. Also in light of the hardcoded
MAX_PACKET_SIZE, probably want to setdev->max_mtu.
can I just not set .ndo_change_mtu? or I must add my own implementation? setting of dev->max_mtu, can be done in probe, yes?
BTW, I see that currently the mtu is 1500 but I do get transactions with len of 1514 (I didn't compile with VLAN)
Quoted 38 lines
Quoted 25 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +24
-0+static int npcm7xx_ether_probe (struct platform_device *pdev) +{ +struct npcm7xx_ether *ether; +struct net_device *netdev; +int error; + +struct clk *emc_clk =NULL ; +struct device_node *np = pdev->dev.of_node; + + pdev->id =of_alias_get_id (np,"ethernet" ); +if (pdev->id <0 ) + pdev->id =0 ; + + emc_clk =devm_clk_get (&pdev->dev,NULL ); + +if (IS_ERR (emc_clk)) +return PTR_ERR (emc_clk); + +/* Enable Clock */ +clk_prepare_enable (emc_clk); + + error =dma_coerce_mask_and_coherent (&pdev->dev,DMA_BIT_MASK (32 )); +if (error) +return -ENODEV ;drivers/net/ethernet/nuvoton/npcm7xx_emc.c line 1912
Quoted 10 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +10
-0+ + netdev =alloc_etherdev (sizeof (struct npcm7xx_ether )); +if (!netdev) +return -ENOMEM ; + + ether =netdev_priv (netdev); + + ether->reset =devm_reset_control_get (&pdev->dev,NULL ); +if (IS_ERR (ether->reset)) +return PTR_ERR (ether->reset);Memory leak on error path
Quoted 1 line
Also missing
netif_napi_delinnpcm7xx_ether_remove?
added
--
Regards,
Avi
Re: [PATCH v1 2/2] net: npcm: add NPCM7xx EMC 10/100 Ethernet driver
Quoted 4 lines
Quoted 1 line
Does this device support stacked VLAN?
I am not familiar with stacked VLAN.
Our HW for sure there is no support. can the SW stack handle it for me? Does it mean that the packets can be larger?
If the device does not support it, I believe it's fine to leave it to S/W.
Quoted 6 lines
Quoted 1 line
Is this really the device maximum?
The device can support upto 64KB, but of course I will not allocate for each RX data such a big buffer.
Can I know what is the maximum value the network stack may request? I saw many driver allocating 1536 for each packet.
The maximum is the minimum of the link layer and device h/w limits, but you indeed don't want to allocate for worst case if that is highly unlikely.
Your choice of 1500 is fine for this initial commit, really. More on MTU below with ndo_change_mtu
Quoted 32 lines
Quoted 21 lines
Quoted 10 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +10
-0+ rxd_offset = (readl ((ether->reg +REG_CRXDSA )) - +readl ((ether->reg +REG_RXDLSA ))) + /sizeof (struct npcm7xx_txbd ); +DUMP_PRINT ("RXD offset %6d\n" , rxd_offset); +DUMP_PRINT ("cur_rx %6d\n" , ether->cur_rx); +DUMP_PRINT ("rx_err %6d\n" , ether->rx_err); + ether->rx_err =0 ; +DUMP_PRINT ("rx_berr %6d\n" , ether->rx_berr); + ether->rx_berr =0 ; +DUMP_PRINT ("rx_stuck %6d\n" , ether->rx_stuck);drivers/net/ethernet/nuvoton/npcm7xx_emc.c line 405
Quoted 10 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +10
-0+ ether->rx_stuck =0 ; +DUMP_PRINT ("rdu %6d\n" , ether->rdu); + ether->rdu =0 ; +DUMP_PRINT ("rxov rx %6d\n" , ether->rxov); + ether->rxov =0 ; +/* debug counters */ +DUMP_PRINT ("rx_int_count %6d\n" , ether->rx_int_count); + ether->rx_int_count =0 ; +DUMP_PRINT ("rx_err_count %6d\n" , ether->rx_err_count); + ether->rx_err_count =0 ;Quoted 3 lines
Basic counters like
tx_packetsandrx_errorsare probably better exported regardless of debug level asnet_device_stats. And then don't need to be copied in debug output.They are also exported there, see below ether->stats.tx_packets++; and ether->stats.rx_errors++;
those are different counters for debug since we had HW issues that we needed to workaround and might need them for future use. Currently the driver is stable on millions of parts in the field.Quoted 1 line
Quoted 6 lines
Quoted 2 lines
Less standard counters like tx interrupt count are probably better candidates for ethtool -S.
I don't have support for ethtool.
is it a must? it is quite some work and this driver is already in the field for quite some years.
Your driver includes a struct ethtool_ops. Implementing its callback .get_ethtool_stats seems straightforward?
David also requested using standard infrastructure over this custom debug logic. Ethool stats appear the most logical choice to me. But there may be others.
Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +4
-0+static struct sk_buff *get_new_skb (struct net_device *netdev,u32 i) +{ +__le32 buffer; +struct npcm7xx_ether *ether =netdev_priv (netdev);Quoted 8 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c line 698
Quoted 6 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +6
-0+struct sk_buff *skb =netdev_alloc_skb (netdev, +roundup (MAX_PACKET_SIZE_W_CRC ,4 )); + +if (unlikely (!skb)) { +if (net_ratelimit ()) +netdev_warn (netdev,"failed to allocate rx skb\n" );can use
net_warn_ratelimited(here and elsewhere)
Quoted 3 lines
should I replace every netdev_warn/err/info with net_warn/err/inf_ratelimited? I saw in drivers that are using
net_warn_ratelimited, that many time uses also netdev_warn/err/info
They probably use the second in non-ratelimited cases?
Quoted 29 lines
Quoted 20 lines
Quoted 20 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +20
-0+static irqreturn_t npcm7xx_tx_interrupt (int irq,void *dev_id) +{ +struct npcm7xx_ether *ether; +struct platform_device *pdev; +struct net_device *netdev; +__le32 status; +unsigned long flags; + + netdev = dev_id; + ether =netdev_priv (netdev); + pdev = ether->pdev; + +npcm7xx_get_and_clear_int (netdev, &status,0xFFFF0000 ); + + ether->tx_int_count++; + +if (status &MISTA_EXDEF ) +dev_err (&pdev->dev,"emc defer exceed interrupt status=0x%08X\n" + , status); +else if (status &MISTA_TXBERR ) {Quoted 8 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c line 1328
Quoted 6 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +6
-0+dev_err (&pdev->dev,"emc bus error interrupt status=0x%08X\n" , + status); +#ifdef CONFIG_NPCM7XX_EMC_ETH_DEBUG +npcm7xx_info_print (netdev); +#endif +spin_lock_irqsave (ðer->lock, flags);irqsave in hard interrupt context?
Quoted 4 lines
I need to protect my
REG_MIENregister that is changed in other places. I think thatspin_lock_irqsave()is relevant when working in SMP, since other CPU may still be running.
Isn't it?
This is an interesting case. The hardware interrupt handler will not interrupt itself. But it is architecture dependent whether all interrupts are disabled when a particular interrupt handler is running (as per the unreliable guide to locking).
So even in absence of SMP, this would indeed need spin_lock_irqsave if there are multiple hardware interrupt handlers potentially accessing the same data. That sounds unlikely in general, but does happen here for REG_MIEN, in npcm7xx_tx_interrupt and npcm7xx_rx_interrupt. So I was mistaken, this is not only the most conservative locking method, it is indeed required.
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +8
-0+writel (0 , (ether->reg +REG_MIEN ));/* disable any interrupt */ +spin_unlock_irqrestore (ðer->lock, flags); + ether->need_reset =1 ; + }else if (status & ~(MISTA_TXINTR |MISTA_TXCP |MISTA_TDU )) +dev_err (&pdev->dev,"emc other error interrupt status=0x%08X\n" , + status); + +/* if we got MISTA_TXCP | MISTA_TDU remove those interrupt and call napi */
Quoted 4 lines
Quoted 1 line
The goal of napi is to keep interrupts disabled until napi completes.
We have a HW issue that because of it I still enabled TX complete interrupt, I will see if I can disable all interrupts and only leave the error interrupts
Please do. I'm not sure what happens when trying to schedule napi while it is already scheduled or running. Even in the best case (nothing), these spurious interrupts are inefficient.
Quoted 40 lines
Quoted 31 lines
Quoted 31 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +30
-0+if (status & (MISTA_TXCP |MISTA_TDU ) & +readl ((ether->reg +REG_MIEN ))) { +__le32 reg_mien; + +spin_lock_irqsave (ðer->lock, flags); + reg_mien =readl ((ether->reg +REG_MIEN )); +if (reg_mien &ENTDU ) +/* Disable TDU interrupt */ +writel (reg_mien & (~ENTDU ), (ether->reg +REG_MIEN )); + +spin_unlock_irqrestore (ðer->lock, flags); + +if (status &MISTA_TXCP ) + ether->tx_cp_i++; +if (status &MISTA_TDU ) + ether->tx_tdu_i++; + }else { +dev_dbg (&pdev->dev,"status=0x%08X\n" , status); + } + +napi_schedule (ðer->napi); + +return IRQ_HANDLED ; +} + +static irqreturn_t npcm7xx_rx_interrupt (int irq,void *dev_id) +{ +struct net_device *netdev = (struct net_device *)dev_id; +struct npcm7xx_ether *ether =netdev_priv (netdev); +struct platform_device *pdev = ether->pdev;Quoted 8 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c line 1372
Quoted 6 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +6
-0+__le32 status; +unsigned long flags; +unsigned int any_err =0 ; +__le32 rxfsm; + +npcm7xx_get_and_clear_int (netdev, &status,0xFFFF );Same here
Quoted 1 line
in non error case I do leave only the error interrupts and schedule napi.
Oh, so the Rx interrupt remains suppressed. Then that's fine.
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
drivers/net/ethernet/nuvoton/npcm7xx_emc.c new file +10
-0+static const struct net_device_ops npcm7xx_ether_netdev_ops = { + .ndo_open = npcm7xx_ether_open, + .ndo_stop = npcm7xx_ether_close, + .ndo_start_xmit = npcm7xx_ether_start_xmit, + .ndo_get_stats = npcm7xx_ether_stats, + .ndo_set_rx_mode = npcm7xx_ether_set_rx_mode, + .ndo_set_mac_address = npcm7xx_set_mac_address, + .ndo_do_ioctl = npcm7xx_ether_ioctl, + .ndo_validate_addr = eth_validate_addr, + .ndo_change_mtu = eth_change_mtu,
Quoted 5 lines
Quoted 2 lines
This is marked as deprecated. Also in light of the hardcoded
MAX_PACKET_SIZE, probably want to setdev->max_mtu.can I just not set .ndo_change_mtu? or I must add my own implementation? setting of
dev->max_mtu, can be done in probe, yes?
It's fine to just not have it. The patchset that introduced max_mtu (61e84623ace3, a52ad514fdf3) removed many.
One reason to have a callback function is to bring the device down/up with different sized rx buffers.
But handling that might be too much extra complexity for the initial patch. It's fine to keep the fixed rx alloc size as is.
Quoted 2 lines
BTW, I see that currently the mtu is 1500 but I do get transactions with len of 1514 (I didn't compile with VLAN)
That is to be expected, as MTU excludes link layer header (and FCS, and perhaps also vlan?)
Rob Herring
maintainerRe: [PATCH v1 1/2] dt-binding: net: document NPCM7xx EMC 10/100 DT bindings
In reply to Avi FishmanQuoted 8 lines · Avi Fishman
Added device tree binding documentation for Nuvoton NPCM7xx Ethernet MAC Controller (EMC) 10/100 RMII
Signed-off-by Avi Fishman <[email protected]>--- .../bindings/net/nuvoton,npcm7xx-emc.txt | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Documentation/devicetree/bindings/net/nuvoton,npcm7xx-emc.txt
Consider converting this to DT schema (YAML).
Quoted 7 lines
Documentation/devicetree/bindings/net/nuvoton,npcm7xx-emc.txt new file +0
-0
Documentation/devicetree/bindings/net/nuvoton,npcm7xx-emc.txt line 7
Quoted 7 lines
Documentation/devicetree/bindings/net/nuvoton,npcm7xx-emc.txt new file +7
-0@@ -0,0 +1,38 @@ +Nuvoton NPCM7XX 10/100 Ethernet MAC Controller (EMC) + +The NPCM7XX provides one or two Ethernet MAC RMII Controllers +for WAN/LAN applications + +Required properties: +- device_type : Should be "network"
Drop this. device_type is deprecated for FDT except for a few cases.
Documentation/devicetree/bindings/net/nuvoton,npcm7xx-emc.txt line 16
Quoted 9 lines
Documentation/devicetree/bindings/net/nuvoton,npcm7xx-emc.txt new file +9
-0+- compatible : "nuvoton,npcm750-emc" for Poleg NPCM7XX. +- reg : Offset and length of the register set for the device. +- interrupts : Contain the emc interrupts with flags for falling edge. + first interrupt dedicated to Txirq + second interrupt dedicated to Rxirq +- phy-mode : Should be "rmii" (see ethernet.txt in the same directory) +- clocks : phandle of emc reference clock. +- resets : phandle to the reset control for this device. +- use-ncsi : Use the NC-SI stack instead of an MDIO PHY
Vendor prefix needed.
Documentation/devicetree/bindings/net/nuvoton,npcm7xx-emc.txt line 20
Quoted 4 lines
Documentation/devicetree/bindings/net/nuvoton,npcm7xx-emc.txt new file +4
-0+ +Example: + +emc0: eth@f0825000 {
ethernet@...
Documentation/devicetree/bindings/net/nuvoton,npcm7xx-emc.txt line 29
Quoted 9 lines
Documentation/devicetree/bindings/net/nuvoton,npcm7xx-emc.txt new file +9
-0+ device_type = "network"; + compatible = "nuvoton,npcm750-emc"; + reg = <0xf0825000 0x1000>; + interrupts = <GIC_SPI 16 IRQ_TYPE_LEVEL_HIGH>, + <GIC_SPI 15 IRQ_TYPE_LEVEL_HIGH>; + phy-mode = "rmii"; + clocks = <&clk NPCM7XX_CLK_AHB>; + + #use-ncsi; /* add this to support ncsi */
Doesn't match the binding.
Documentation/devicetree/bindings/net/nuvoton,npcm7xx-emc.txt line 37
Quoted 8 lines
Documentation/devicetree/bindings/net/nuvoton,npcm7xx-emc.txt new file +8
-0+ + clock-names = "clk_emc"; + resets = <&rstc 6>; + pinctrl-names = "default"; + pinctrl-0 = <&r1_pins + &r1err_pins + &r1md_pins>; + status = "okay";
Drop status in examples.
Quoted 4 lines
Documentation/devicetree/bindings/net/nuvoton,npcm7xx-emc.txt new file +1
-0+};Signature · 1 line
2.18.0