Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Broadcom GENET (Gigabit Ethernet) controller driver |
| 4 | * |
Doug Berger | 99d5563 | 2019-12-17 16:51:08 -0800 | [diff] [blame] | 5 | * Copyright (c) 2014-2019 Broadcom |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #define pr_fmt(fmt) "bcmgenet: " fmt |
| 9 | |
Jeremy Linton | 99c6b06 | 2020-02-24 16:54:01 -0600 | [diff] [blame] | 10 | #include <linux/acpi.h> |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 11 | #include <linux/kernel.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/sched.h> |
| 14 | #include <linux/types.h> |
| 15 | #include <linux/fcntl.h> |
| 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/string.h> |
| 18 | #include <linux/if_ether.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/errno.h> |
| 21 | #include <linux/delay.h> |
| 22 | #include <linux/platform_device.h> |
| 23 | #include <linux/dma-mapping.h> |
| 24 | #include <linux/pm.h> |
| 25 | #include <linux/clk.h> |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 26 | #include <linux/of.h> |
| 27 | #include <linux/of_address.h> |
| 28 | #include <linux/of_irq.h> |
| 29 | #include <linux/of_net.h> |
| 30 | #include <linux/of_platform.h> |
| 31 | #include <net/arp.h> |
| 32 | |
| 33 | #include <linux/mii.h> |
| 34 | #include <linux/ethtool.h> |
| 35 | #include <linux/netdevice.h> |
| 36 | #include <linux/inetdevice.h> |
| 37 | #include <linux/etherdevice.h> |
| 38 | #include <linux/skbuff.h> |
| 39 | #include <linux/in.h> |
| 40 | #include <linux/ip.h> |
| 41 | #include <linux/ipv6.h> |
| 42 | #include <linux/phy.h> |
Petri Gynther | b0ba512 | 2014-12-01 16:18:08 -0800 | [diff] [blame] | 43 | #include <linux/platform_data/bcmgenet.h> |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 44 | |
| 45 | #include <asm/unaligned.h> |
| 46 | |
| 47 | #include "bcmgenet.h" |
| 48 | |
| 49 | /* Maximum number of hardware queues, downsized if needed */ |
| 50 | #define GENET_MAX_MQ_CNT 4 |
| 51 | |
| 52 | /* Default highest priority queue for multi queue support */ |
| 53 | #define GENET_Q0_PRIORITY 0 |
| 54 | |
Petri Gynther | 3feafa0 | 2015-03-05 17:40:14 -0800 | [diff] [blame] | 55 | #define GENET_Q16_RX_BD_CNT \ |
| 56 | (TOTAL_DESC - priv->hw_params->rx_queues * priv->hw_params->rx_bds_per_q) |
Petri Gynther | 51a966a | 2015-02-23 11:00:46 -0800 | [diff] [blame] | 57 | #define GENET_Q16_TX_BD_CNT \ |
| 58 | (TOTAL_DESC - priv->hw_params->tx_queues * priv->hw_params->tx_bds_per_q) |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 59 | |
| 60 | #define RX_BUF_LENGTH 2048 |
| 61 | #define SKB_ALIGNMENT 32 |
| 62 | |
| 63 | /* Tx/Rx DMA register offset, skip 256 descriptors */ |
| 64 | #define WORDS_PER_BD(p) (p->hw_params->words_per_bd) |
| 65 | #define DMA_DESC_SIZE (WORDS_PER_BD(priv) * sizeof(u32)) |
| 66 | |
| 67 | #define GENET_TDMA_REG_OFF (priv->hw_params->tdma_offset + \ |
| 68 | TOTAL_DESC * DMA_DESC_SIZE) |
| 69 | |
| 70 | #define GENET_RDMA_REG_OFF (priv->hw_params->rdma_offset + \ |
| 71 | TOTAL_DESC * DMA_DESC_SIZE) |
| 72 | |
Florian Fainelli | 69d2ea9 | 2017-08-29 12:25:31 -0700 | [diff] [blame] | 73 | static inline void bcmgenet_writel(u32 value, void __iomem *offset) |
| 74 | { |
| 75 | /* MIPS chips strapped for BE will automagically configure the |
| 76 | * peripheral registers for CPU-native byte order. |
| 77 | */ |
| 78 | if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN)) |
| 79 | __raw_writel(value, offset); |
| 80 | else |
| 81 | writel_relaxed(value, offset); |
| 82 | } |
| 83 | |
| 84 | static inline u32 bcmgenet_readl(void __iomem *offset) |
| 85 | { |
| 86 | if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN)) |
| 87 | return __raw_readl(offset); |
| 88 | else |
| 89 | return readl_relaxed(offset); |
| 90 | } |
| 91 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 92 | static inline void dmadesc_set_length_status(struct bcmgenet_priv *priv, |
Florian Fainelli | c91b7f6 | 2014-07-23 10:42:12 -0700 | [diff] [blame] | 93 | void __iomem *d, u32 value) |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 94 | { |
Florian Fainelli | 69d2ea9 | 2017-08-29 12:25:31 -0700 | [diff] [blame] | 95 | bcmgenet_writel(value, d + DMA_DESC_LENGTH_STATUS); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 98 | static inline void dmadesc_set_addr(struct bcmgenet_priv *priv, |
| 99 | void __iomem *d, |
| 100 | dma_addr_t addr) |
| 101 | { |
Florian Fainelli | 69d2ea9 | 2017-08-29 12:25:31 -0700 | [diff] [blame] | 102 | bcmgenet_writel(lower_32_bits(addr), d + DMA_DESC_ADDRESS_LO); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 103 | |
| 104 | /* Register writes to GISB bus can take couple hundred nanoseconds |
| 105 | * and are done for each packet, save these expensive writes unless |
Brian Norris | 7fc527f | 2014-07-29 14:34:14 -0700 | [diff] [blame] | 106 | * the platform is explicitly configured for 64-bits/LPAE. |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 107 | */ |
| 108 | #ifdef CONFIG_PHYS_ADDR_T_64BIT |
| 109 | if (priv->hw_params->flags & GENET_HAS_40BITS) |
Florian Fainelli | 69d2ea9 | 2017-08-29 12:25:31 -0700 | [diff] [blame] | 110 | bcmgenet_writel(upper_32_bits(addr), d + DMA_DESC_ADDRESS_HI); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 111 | #endif |
| 112 | } |
| 113 | |
| 114 | /* Combined address + length/status setter */ |
| 115 | static inline void dmadesc_set(struct bcmgenet_priv *priv, |
Florian Fainelli | c91b7f6 | 2014-07-23 10:42:12 -0700 | [diff] [blame] | 116 | void __iomem *d, dma_addr_t addr, u32 val) |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 117 | { |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 118 | dmadesc_set_addr(priv, d, addr); |
Petri Gynther | 7ee4062 | 2016-04-05 14:00:01 -0700 | [diff] [blame] | 119 | dmadesc_set_length_status(priv, d, val); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | static inline dma_addr_t dmadesc_get_addr(struct bcmgenet_priv *priv, |
| 123 | void __iomem *d) |
| 124 | { |
| 125 | dma_addr_t addr; |
| 126 | |
Florian Fainelli | 69d2ea9 | 2017-08-29 12:25:31 -0700 | [diff] [blame] | 127 | addr = bcmgenet_readl(d + DMA_DESC_ADDRESS_LO); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 128 | |
| 129 | /* Register writes to GISB bus can take couple hundred nanoseconds |
| 130 | * and are done for each packet, save these expensive writes unless |
Brian Norris | 7fc527f | 2014-07-29 14:34:14 -0700 | [diff] [blame] | 131 | * the platform is explicitly configured for 64-bits/LPAE. |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 132 | */ |
| 133 | #ifdef CONFIG_PHYS_ADDR_T_64BIT |
| 134 | if (priv->hw_params->flags & GENET_HAS_40BITS) |
Florian Fainelli | 69d2ea9 | 2017-08-29 12:25:31 -0700 | [diff] [blame] | 135 | addr |= (u64)bcmgenet_readl(d + DMA_DESC_ADDRESS_HI) << 32; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 136 | #endif |
| 137 | return addr; |
| 138 | } |
| 139 | |
| 140 | #define GENET_VER_FMT "%1d.%1d EPHY: 0x%04x" |
| 141 | |
| 142 | #define GENET_MSG_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | \ |
| 143 | NETIF_MSG_LINK) |
| 144 | |
| 145 | static inline u32 bcmgenet_rbuf_ctrl_get(struct bcmgenet_priv *priv) |
| 146 | { |
| 147 | if (GENET_IS_V1(priv)) |
| 148 | return bcmgenet_rbuf_readl(priv, RBUF_FLUSH_CTRL_V1); |
| 149 | else |
| 150 | return bcmgenet_sys_readl(priv, SYS_RBUF_FLUSH_CTRL); |
| 151 | } |
| 152 | |
| 153 | static inline void bcmgenet_rbuf_ctrl_set(struct bcmgenet_priv *priv, u32 val) |
| 154 | { |
| 155 | if (GENET_IS_V1(priv)) |
| 156 | bcmgenet_rbuf_writel(priv, val, RBUF_FLUSH_CTRL_V1); |
| 157 | else |
| 158 | bcmgenet_sys_writel(priv, val, SYS_RBUF_FLUSH_CTRL); |
| 159 | } |
| 160 | |
| 161 | /* These macros are defined to deal with register map change |
| 162 | * between GENET1.1 and GENET2. Only those currently being used |
| 163 | * by driver are defined. |
| 164 | */ |
| 165 | static inline u32 bcmgenet_tbuf_ctrl_get(struct bcmgenet_priv *priv) |
| 166 | { |
| 167 | if (GENET_IS_V1(priv)) |
| 168 | return bcmgenet_rbuf_readl(priv, TBUF_CTRL_V1); |
| 169 | else |
Florian Fainelli | 69d2ea9 | 2017-08-29 12:25:31 -0700 | [diff] [blame] | 170 | return bcmgenet_readl(priv->base + |
| 171 | priv->hw_params->tbuf_offset + TBUF_CTRL); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | static inline void bcmgenet_tbuf_ctrl_set(struct bcmgenet_priv *priv, u32 val) |
| 175 | { |
| 176 | if (GENET_IS_V1(priv)) |
| 177 | bcmgenet_rbuf_writel(priv, val, TBUF_CTRL_V1); |
| 178 | else |
Florian Fainelli | 69d2ea9 | 2017-08-29 12:25:31 -0700 | [diff] [blame] | 179 | bcmgenet_writel(val, priv->base + |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 180 | priv->hw_params->tbuf_offset + TBUF_CTRL); |
| 181 | } |
| 182 | |
| 183 | static inline u32 bcmgenet_bp_mc_get(struct bcmgenet_priv *priv) |
| 184 | { |
| 185 | if (GENET_IS_V1(priv)) |
| 186 | return bcmgenet_rbuf_readl(priv, TBUF_BP_MC_V1); |
| 187 | else |
Florian Fainelli | 69d2ea9 | 2017-08-29 12:25:31 -0700 | [diff] [blame] | 188 | return bcmgenet_readl(priv->base + |
| 189 | priv->hw_params->tbuf_offset + TBUF_BP_MC); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | static inline void bcmgenet_bp_mc_set(struct bcmgenet_priv *priv, u32 val) |
| 193 | { |
| 194 | if (GENET_IS_V1(priv)) |
| 195 | bcmgenet_rbuf_writel(priv, val, TBUF_BP_MC_V1); |
| 196 | else |
Florian Fainelli | 69d2ea9 | 2017-08-29 12:25:31 -0700 | [diff] [blame] | 197 | bcmgenet_writel(val, priv->base + |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 198 | priv->hw_params->tbuf_offset + TBUF_BP_MC); |
| 199 | } |
| 200 | |
| 201 | /* RX/TX DMA register accessors */ |
| 202 | enum dma_reg { |
| 203 | DMA_RING_CFG = 0, |
| 204 | DMA_CTRL, |
| 205 | DMA_STATUS, |
| 206 | DMA_SCB_BURST_SIZE, |
| 207 | DMA_ARB_CTRL, |
Petri Gynther | 3774216 | 2014-10-07 09:30:01 -0700 | [diff] [blame] | 208 | DMA_PRIORITY_0, |
| 209 | DMA_PRIORITY_1, |
| 210 | DMA_PRIORITY_2, |
Petri Gynther | 0034de4 | 2015-03-13 14:45:00 -0700 | [diff] [blame] | 211 | DMA_INDEX2RING_0, |
| 212 | DMA_INDEX2RING_1, |
| 213 | DMA_INDEX2RING_2, |
| 214 | DMA_INDEX2RING_3, |
| 215 | DMA_INDEX2RING_4, |
| 216 | DMA_INDEX2RING_5, |
| 217 | DMA_INDEX2RING_6, |
| 218 | DMA_INDEX2RING_7, |
Florian Fainelli | 4a29645 | 2015-09-16 16:47:40 -0700 | [diff] [blame] | 219 | DMA_RING0_TIMEOUT, |
| 220 | DMA_RING1_TIMEOUT, |
| 221 | DMA_RING2_TIMEOUT, |
| 222 | DMA_RING3_TIMEOUT, |
| 223 | DMA_RING4_TIMEOUT, |
| 224 | DMA_RING5_TIMEOUT, |
| 225 | DMA_RING6_TIMEOUT, |
| 226 | DMA_RING7_TIMEOUT, |
| 227 | DMA_RING8_TIMEOUT, |
| 228 | DMA_RING9_TIMEOUT, |
| 229 | DMA_RING10_TIMEOUT, |
| 230 | DMA_RING11_TIMEOUT, |
| 231 | DMA_RING12_TIMEOUT, |
| 232 | DMA_RING13_TIMEOUT, |
| 233 | DMA_RING14_TIMEOUT, |
| 234 | DMA_RING15_TIMEOUT, |
| 235 | DMA_RING16_TIMEOUT, |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 236 | }; |
| 237 | |
| 238 | static const u8 bcmgenet_dma_regs_v3plus[] = { |
| 239 | [DMA_RING_CFG] = 0x00, |
| 240 | [DMA_CTRL] = 0x04, |
| 241 | [DMA_STATUS] = 0x08, |
| 242 | [DMA_SCB_BURST_SIZE] = 0x0C, |
| 243 | [DMA_ARB_CTRL] = 0x2C, |
Petri Gynther | 3774216 | 2014-10-07 09:30:01 -0700 | [diff] [blame] | 244 | [DMA_PRIORITY_0] = 0x30, |
| 245 | [DMA_PRIORITY_1] = 0x34, |
| 246 | [DMA_PRIORITY_2] = 0x38, |
Florian Fainelli | 4a29645 | 2015-09-16 16:47:40 -0700 | [diff] [blame] | 247 | [DMA_RING0_TIMEOUT] = 0x2C, |
| 248 | [DMA_RING1_TIMEOUT] = 0x30, |
| 249 | [DMA_RING2_TIMEOUT] = 0x34, |
| 250 | [DMA_RING3_TIMEOUT] = 0x38, |
| 251 | [DMA_RING4_TIMEOUT] = 0x3c, |
| 252 | [DMA_RING5_TIMEOUT] = 0x40, |
| 253 | [DMA_RING6_TIMEOUT] = 0x44, |
| 254 | [DMA_RING7_TIMEOUT] = 0x48, |
| 255 | [DMA_RING8_TIMEOUT] = 0x4c, |
| 256 | [DMA_RING9_TIMEOUT] = 0x50, |
| 257 | [DMA_RING10_TIMEOUT] = 0x54, |
| 258 | [DMA_RING11_TIMEOUT] = 0x58, |
| 259 | [DMA_RING12_TIMEOUT] = 0x5c, |
| 260 | [DMA_RING13_TIMEOUT] = 0x60, |
| 261 | [DMA_RING14_TIMEOUT] = 0x64, |
| 262 | [DMA_RING15_TIMEOUT] = 0x68, |
| 263 | [DMA_RING16_TIMEOUT] = 0x6C, |
Petri Gynther | 0034de4 | 2015-03-13 14:45:00 -0700 | [diff] [blame] | 264 | [DMA_INDEX2RING_0] = 0x70, |
| 265 | [DMA_INDEX2RING_1] = 0x74, |
| 266 | [DMA_INDEX2RING_2] = 0x78, |
| 267 | [DMA_INDEX2RING_3] = 0x7C, |
| 268 | [DMA_INDEX2RING_4] = 0x80, |
| 269 | [DMA_INDEX2RING_5] = 0x84, |
| 270 | [DMA_INDEX2RING_6] = 0x88, |
| 271 | [DMA_INDEX2RING_7] = 0x8C, |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 272 | }; |
| 273 | |
| 274 | static const u8 bcmgenet_dma_regs_v2[] = { |
| 275 | [DMA_RING_CFG] = 0x00, |
| 276 | [DMA_CTRL] = 0x04, |
| 277 | [DMA_STATUS] = 0x08, |
| 278 | [DMA_SCB_BURST_SIZE] = 0x0C, |
| 279 | [DMA_ARB_CTRL] = 0x30, |
Petri Gynther | 3774216 | 2014-10-07 09:30:01 -0700 | [diff] [blame] | 280 | [DMA_PRIORITY_0] = 0x34, |
| 281 | [DMA_PRIORITY_1] = 0x38, |
| 282 | [DMA_PRIORITY_2] = 0x3C, |
Florian Fainelli | 4a29645 | 2015-09-16 16:47:40 -0700 | [diff] [blame] | 283 | [DMA_RING0_TIMEOUT] = 0x2C, |
| 284 | [DMA_RING1_TIMEOUT] = 0x30, |
| 285 | [DMA_RING2_TIMEOUT] = 0x34, |
| 286 | [DMA_RING3_TIMEOUT] = 0x38, |
| 287 | [DMA_RING4_TIMEOUT] = 0x3c, |
| 288 | [DMA_RING5_TIMEOUT] = 0x40, |
| 289 | [DMA_RING6_TIMEOUT] = 0x44, |
| 290 | [DMA_RING7_TIMEOUT] = 0x48, |
| 291 | [DMA_RING8_TIMEOUT] = 0x4c, |
| 292 | [DMA_RING9_TIMEOUT] = 0x50, |
| 293 | [DMA_RING10_TIMEOUT] = 0x54, |
| 294 | [DMA_RING11_TIMEOUT] = 0x58, |
| 295 | [DMA_RING12_TIMEOUT] = 0x5c, |
| 296 | [DMA_RING13_TIMEOUT] = 0x60, |
| 297 | [DMA_RING14_TIMEOUT] = 0x64, |
| 298 | [DMA_RING15_TIMEOUT] = 0x68, |
| 299 | [DMA_RING16_TIMEOUT] = 0x6C, |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 300 | }; |
| 301 | |
| 302 | static const u8 bcmgenet_dma_regs_v1[] = { |
| 303 | [DMA_CTRL] = 0x00, |
| 304 | [DMA_STATUS] = 0x04, |
| 305 | [DMA_SCB_BURST_SIZE] = 0x0C, |
| 306 | [DMA_ARB_CTRL] = 0x30, |
Petri Gynther | 3774216 | 2014-10-07 09:30:01 -0700 | [diff] [blame] | 307 | [DMA_PRIORITY_0] = 0x34, |
| 308 | [DMA_PRIORITY_1] = 0x38, |
| 309 | [DMA_PRIORITY_2] = 0x3C, |
Florian Fainelli | 4a29645 | 2015-09-16 16:47:40 -0700 | [diff] [blame] | 310 | [DMA_RING0_TIMEOUT] = 0x2C, |
| 311 | [DMA_RING1_TIMEOUT] = 0x30, |
| 312 | [DMA_RING2_TIMEOUT] = 0x34, |
| 313 | [DMA_RING3_TIMEOUT] = 0x38, |
| 314 | [DMA_RING4_TIMEOUT] = 0x3c, |
| 315 | [DMA_RING5_TIMEOUT] = 0x40, |
| 316 | [DMA_RING6_TIMEOUT] = 0x44, |
| 317 | [DMA_RING7_TIMEOUT] = 0x48, |
| 318 | [DMA_RING8_TIMEOUT] = 0x4c, |
| 319 | [DMA_RING9_TIMEOUT] = 0x50, |
| 320 | [DMA_RING10_TIMEOUT] = 0x54, |
| 321 | [DMA_RING11_TIMEOUT] = 0x58, |
| 322 | [DMA_RING12_TIMEOUT] = 0x5c, |
| 323 | [DMA_RING13_TIMEOUT] = 0x60, |
| 324 | [DMA_RING14_TIMEOUT] = 0x64, |
| 325 | [DMA_RING15_TIMEOUT] = 0x68, |
| 326 | [DMA_RING16_TIMEOUT] = 0x6C, |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 327 | }; |
| 328 | |
| 329 | /* Set at runtime once bcmgenet version is known */ |
| 330 | static const u8 *bcmgenet_dma_regs; |
| 331 | |
| 332 | static inline struct bcmgenet_priv *dev_to_priv(struct device *dev) |
| 333 | { |
| 334 | return netdev_priv(dev_get_drvdata(dev)); |
| 335 | } |
| 336 | |
| 337 | static inline u32 bcmgenet_tdma_readl(struct bcmgenet_priv *priv, |
Florian Fainelli | c91b7f6 | 2014-07-23 10:42:12 -0700 | [diff] [blame] | 338 | enum dma_reg r) |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 339 | { |
Florian Fainelli | 69d2ea9 | 2017-08-29 12:25:31 -0700 | [diff] [blame] | 340 | return bcmgenet_readl(priv->base + GENET_TDMA_REG_OFF + |
| 341 | DMA_RINGS_SIZE + bcmgenet_dma_regs[r]); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | static inline void bcmgenet_tdma_writel(struct bcmgenet_priv *priv, |
| 345 | u32 val, enum dma_reg r) |
| 346 | { |
Florian Fainelli | 69d2ea9 | 2017-08-29 12:25:31 -0700 | [diff] [blame] | 347 | bcmgenet_writel(val, priv->base + GENET_TDMA_REG_OFF + |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 348 | DMA_RINGS_SIZE + bcmgenet_dma_regs[r]); |
| 349 | } |
| 350 | |
| 351 | static inline u32 bcmgenet_rdma_readl(struct bcmgenet_priv *priv, |
Florian Fainelli | c91b7f6 | 2014-07-23 10:42:12 -0700 | [diff] [blame] | 352 | enum dma_reg r) |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 353 | { |
Florian Fainelli | 69d2ea9 | 2017-08-29 12:25:31 -0700 | [diff] [blame] | 354 | return bcmgenet_readl(priv->base + GENET_RDMA_REG_OFF + |
| 355 | DMA_RINGS_SIZE + bcmgenet_dma_regs[r]); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | static inline void bcmgenet_rdma_writel(struct bcmgenet_priv *priv, |
| 359 | u32 val, enum dma_reg r) |
| 360 | { |
Florian Fainelli | 69d2ea9 | 2017-08-29 12:25:31 -0700 | [diff] [blame] | 361 | bcmgenet_writel(val, priv->base + GENET_RDMA_REG_OFF + |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 362 | DMA_RINGS_SIZE + bcmgenet_dma_regs[r]); |
| 363 | } |
| 364 | |
| 365 | /* RDMA/TDMA ring registers and accessors |
| 366 | * we merge the common fields and just prefix with T/D the registers |
| 367 | * having different meaning depending on the direction |
| 368 | */ |
| 369 | enum dma_ring_reg { |
| 370 | TDMA_READ_PTR = 0, |
| 371 | RDMA_WRITE_PTR = TDMA_READ_PTR, |
| 372 | TDMA_READ_PTR_HI, |
| 373 | RDMA_WRITE_PTR_HI = TDMA_READ_PTR_HI, |
| 374 | TDMA_CONS_INDEX, |
| 375 | RDMA_PROD_INDEX = TDMA_CONS_INDEX, |
| 376 | TDMA_PROD_INDEX, |
| 377 | RDMA_CONS_INDEX = TDMA_PROD_INDEX, |
| 378 | DMA_RING_BUF_SIZE, |
| 379 | DMA_START_ADDR, |
| 380 | DMA_START_ADDR_HI, |
| 381 | DMA_END_ADDR, |
| 382 | DMA_END_ADDR_HI, |
| 383 | DMA_MBUF_DONE_THRESH, |
| 384 | TDMA_FLOW_PERIOD, |
| 385 | RDMA_XON_XOFF_THRESH = TDMA_FLOW_PERIOD, |
| 386 | TDMA_WRITE_PTR, |
| 387 | RDMA_READ_PTR = TDMA_WRITE_PTR, |
| 388 | TDMA_WRITE_PTR_HI, |
| 389 | RDMA_READ_PTR_HI = TDMA_WRITE_PTR_HI |
| 390 | }; |
| 391 | |
| 392 | /* GENET v4 supports 40-bits pointer addressing |
| 393 | * for obvious reasons the LO and HI word parts |
| 394 | * are contiguous, but this offsets the other |
| 395 | * registers. |
| 396 | */ |
| 397 | static const u8 genet_dma_ring_regs_v4[] = { |
| 398 | [TDMA_READ_PTR] = 0x00, |
| 399 | [TDMA_READ_PTR_HI] = 0x04, |
| 400 | [TDMA_CONS_INDEX] = 0x08, |
| 401 | [TDMA_PROD_INDEX] = 0x0C, |
| 402 | [DMA_RING_BUF_SIZE] = 0x10, |
| 403 | [DMA_START_ADDR] = 0x14, |
| 404 | [DMA_START_ADDR_HI] = 0x18, |
| 405 | [DMA_END_ADDR] = 0x1C, |
| 406 | [DMA_END_ADDR_HI] = 0x20, |
| 407 | [DMA_MBUF_DONE_THRESH] = 0x24, |
| 408 | [TDMA_FLOW_PERIOD] = 0x28, |
| 409 | [TDMA_WRITE_PTR] = 0x2C, |
| 410 | [TDMA_WRITE_PTR_HI] = 0x30, |
| 411 | }; |
| 412 | |
| 413 | static const u8 genet_dma_ring_regs_v123[] = { |
| 414 | [TDMA_READ_PTR] = 0x00, |
| 415 | [TDMA_CONS_INDEX] = 0x04, |
| 416 | [TDMA_PROD_INDEX] = 0x08, |
| 417 | [DMA_RING_BUF_SIZE] = 0x0C, |
| 418 | [DMA_START_ADDR] = 0x10, |
| 419 | [DMA_END_ADDR] = 0x14, |
| 420 | [DMA_MBUF_DONE_THRESH] = 0x18, |
| 421 | [TDMA_FLOW_PERIOD] = 0x1C, |
| 422 | [TDMA_WRITE_PTR] = 0x20, |
| 423 | }; |
| 424 | |
| 425 | /* Set at runtime once GENET version is known */ |
| 426 | static const u8 *genet_dma_ring_regs; |
| 427 | |
| 428 | static inline u32 bcmgenet_tdma_ring_readl(struct bcmgenet_priv *priv, |
Florian Fainelli | c91b7f6 | 2014-07-23 10:42:12 -0700 | [diff] [blame] | 429 | unsigned int ring, |
| 430 | enum dma_ring_reg r) |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 431 | { |
Florian Fainelli | 69d2ea9 | 2017-08-29 12:25:31 -0700 | [diff] [blame] | 432 | return bcmgenet_readl(priv->base + GENET_TDMA_REG_OFF + |
| 433 | (DMA_RING_SIZE * ring) + |
| 434 | genet_dma_ring_regs[r]); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | static inline void bcmgenet_tdma_ring_writel(struct bcmgenet_priv *priv, |
Florian Fainelli | c91b7f6 | 2014-07-23 10:42:12 -0700 | [diff] [blame] | 438 | unsigned int ring, u32 val, |
| 439 | enum dma_ring_reg r) |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 440 | { |
Florian Fainelli | 69d2ea9 | 2017-08-29 12:25:31 -0700 | [diff] [blame] | 441 | bcmgenet_writel(val, priv->base + GENET_TDMA_REG_OFF + |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 442 | (DMA_RING_SIZE * ring) + |
| 443 | genet_dma_ring_regs[r]); |
| 444 | } |
| 445 | |
| 446 | static inline u32 bcmgenet_rdma_ring_readl(struct bcmgenet_priv *priv, |
Florian Fainelli | c91b7f6 | 2014-07-23 10:42:12 -0700 | [diff] [blame] | 447 | unsigned int ring, |
| 448 | enum dma_ring_reg r) |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 449 | { |
Florian Fainelli | 69d2ea9 | 2017-08-29 12:25:31 -0700 | [diff] [blame] | 450 | return bcmgenet_readl(priv->base + GENET_RDMA_REG_OFF + |
| 451 | (DMA_RING_SIZE * ring) + |
| 452 | genet_dma_ring_regs[r]); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | static inline void bcmgenet_rdma_ring_writel(struct bcmgenet_priv *priv, |
Florian Fainelli | c91b7f6 | 2014-07-23 10:42:12 -0700 | [diff] [blame] | 456 | unsigned int ring, u32 val, |
| 457 | enum dma_ring_reg r) |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 458 | { |
Florian Fainelli | 69d2ea9 | 2017-08-29 12:25:31 -0700 | [diff] [blame] | 459 | bcmgenet_writel(val, priv->base + GENET_RDMA_REG_OFF + |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 460 | (DMA_RING_SIZE * ring) + |
| 461 | genet_dma_ring_regs[r]); |
| 462 | } |
| 463 | |
Edwin Chan | 89316fa | 2017-03-09 16:58:49 -0800 | [diff] [blame] | 464 | static int bcmgenet_begin(struct net_device *dev) |
| 465 | { |
| 466 | struct bcmgenet_priv *priv = netdev_priv(dev); |
| 467 | |
| 468 | /* Turn on the clock */ |
| 469 | return clk_prepare_enable(priv->clk); |
| 470 | } |
| 471 | |
| 472 | static void bcmgenet_complete(struct net_device *dev) |
| 473 | { |
| 474 | struct bcmgenet_priv *priv = netdev_priv(dev); |
| 475 | |
| 476 | /* Turn off the clock */ |
| 477 | clk_disable_unprepare(priv->clk); |
| 478 | } |
| 479 | |
Philippe Reynes | fa92bf0 | 2016-09-26 22:31:57 +0200 | [diff] [blame] | 480 | static int bcmgenet_get_link_ksettings(struct net_device *dev, |
| 481 | struct ethtool_link_ksettings *cmd) |
Philippe Reynes | bac65c4 | 2016-07-09 00:54:47 +0200 | [diff] [blame] | 482 | { |
| 483 | if (!netif_running(dev)) |
| 484 | return -EINVAL; |
| 485 | |
Doug Berger | 6c97f01 | 2017-10-25 15:04:19 -0700 | [diff] [blame] | 486 | if (!dev->phydev) |
Philippe Reynes | bac65c4 | 2016-07-09 00:54:47 +0200 | [diff] [blame] | 487 | return -ENODEV; |
| 488 | |
Doug Berger | 6c97f01 | 2017-10-25 15:04:19 -0700 | [diff] [blame] | 489 | phy_ethtool_ksettings_get(dev->phydev, cmd); |
yuval.shaia@oracle.com | 5514174 | 2017-06-13 10:09:46 +0300 | [diff] [blame] | 490 | |
| 491 | return 0; |
Philippe Reynes | bac65c4 | 2016-07-09 00:54:47 +0200 | [diff] [blame] | 492 | } |
| 493 | |
Philippe Reynes | fa92bf0 | 2016-09-26 22:31:57 +0200 | [diff] [blame] | 494 | static int bcmgenet_set_link_ksettings(struct net_device *dev, |
| 495 | const struct ethtool_link_ksettings *cmd) |
Philippe Reynes | bac65c4 | 2016-07-09 00:54:47 +0200 | [diff] [blame] | 496 | { |
| 497 | if (!netif_running(dev)) |
| 498 | return -EINVAL; |
| 499 | |
Doug Berger | 6c97f01 | 2017-10-25 15:04:19 -0700 | [diff] [blame] | 500 | if (!dev->phydev) |
Philippe Reynes | bac65c4 | 2016-07-09 00:54:47 +0200 | [diff] [blame] | 501 | return -ENODEV; |
| 502 | |
Doug Berger | 6c97f01 | 2017-10-25 15:04:19 -0700 | [diff] [blame] | 503 | return phy_ethtool_ksettings_set(dev->phydev, cmd); |
Philippe Reynes | bac65c4 | 2016-07-09 00:54:47 +0200 | [diff] [blame] | 504 | } |
| 505 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 506 | static int bcmgenet_set_features(struct net_device *dev, |
Florian Fainelli | c91b7f6 | 2014-07-23 10:42:12 -0700 | [diff] [blame] | 507 | netdev_features_t features) |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 508 | { |
Doug Berger | f63db4e | 2019-12-17 16:51:11 -0800 | [diff] [blame] | 509 | struct bcmgenet_priv *priv = netdev_priv(dev); |
| 510 | u32 reg; |
| 511 | int ret; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 512 | |
Doug Berger | f63db4e | 2019-12-17 16:51:11 -0800 | [diff] [blame] | 513 | ret = clk_prepare_enable(priv->clk); |
| 514 | if (ret) |
| 515 | return ret; |
| 516 | |
| 517 | /* Make sure we reflect the value of CRC_CMD_FWD */ |
| 518 | reg = bcmgenet_umac_readl(priv, UMAC_CMD); |
| 519 | priv->crc_fwd_en = !!(reg & CMD_CRC_FWD); |
| 520 | |
Doug Berger | f63db4e | 2019-12-17 16:51:11 -0800 | [diff] [blame] | 521 | clk_disable_unprepare(priv->clk); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 522 | |
| 523 | return ret; |
| 524 | } |
| 525 | |
| 526 | static u32 bcmgenet_get_msglevel(struct net_device *dev) |
| 527 | { |
| 528 | struct bcmgenet_priv *priv = netdev_priv(dev); |
| 529 | |
| 530 | return priv->msg_enable; |
| 531 | } |
| 532 | |
| 533 | static void bcmgenet_set_msglevel(struct net_device *dev, u32 level) |
| 534 | { |
| 535 | struct bcmgenet_priv *priv = netdev_priv(dev); |
| 536 | |
| 537 | priv->msg_enable = level; |
| 538 | } |
| 539 | |
Florian Fainelli | 2f91307 | 2015-09-16 16:47:39 -0700 | [diff] [blame] | 540 | static int bcmgenet_get_coalesce(struct net_device *dev, |
| 541 | struct ethtool_coalesce *ec) |
| 542 | { |
| 543 | struct bcmgenet_priv *priv = netdev_priv(dev); |
Florian Fainelli | 9f4ca05 | 2018-03-22 18:19:33 -0700 | [diff] [blame] | 544 | struct bcmgenet_rx_ring *ring; |
| 545 | unsigned int i; |
Florian Fainelli | 2f91307 | 2015-09-16 16:47:39 -0700 | [diff] [blame] | 546 | |
| 547 | ec->tx_max_coalesced_frames = |
| 548 | bcmgenet_tdma_ring_readl(priv, DESC_INDEX, |
| 549 | DMA_MBUF_DONE_THRESH); |
Florian Fainelli | 4a29645 | 2015-09-16 16:47:40 -0700 | [diff] [blame] | 550 | ec->rx_max_coalesced_frames = |
| 551 | bcmgenet_rdma_ring_readl(priv, DESC_INDEX, |
| 552 | DMA_MBUF_DONE_THRESH); |
| 553 | ec->rx_coalesce_usecs = |
| 554 | bcmgenet_rdma_readl(priv, DMA_RING16_TIMEOUT) * 8192 / 1000; |
Florian Fainelli | 2f91307 | 2015-09-16 16:47:39 -0700 | [diff] [blame] | 555 | |
Florian Fainelli | 9f4ca05 | 2018-03-22 18:19:33 -0700 | [diff] [blame] | 556 | for (i = 0; i < priv->hw_params->rx_queues; i++) { |
| 557 | ring = &priv->rx_rings[i]; |
| 558 | ec->use_adaptive_rx_coalesce |= ring->dim.use_dim; |
| 559 | } |
| 560 | ring = &priv->rx_rings[DESC_INDEX]; |
| 561 | ec->use_adaptive_rx_coalesce |= ring->dim.use_dim; |
| 562 | |
Florian Fainelli | 2f91307 | 2015-09-16 16:47:39 -0700 | [diff] [blame] | 563 | return 0; |
| 564 | } |
| 565 | |
Florian Fainelli | 5e6ce1f | 2018-03-28 15:15:38 -0700 | [diff] [blame] | 566 | static void bcmgenet_set_rx_coalesce(struct bcmgenet_rx_ring *ring, |
| 567 | u32 usecs, u32 pkts) |
Florian Fainelli | 9f4ca05 | 2018-03-22 18:19:33 -0700 | [diff] [blame] | 568 | { |
| 569 | struct bcmgenet_priv *priv = ring->priv; |
| 570 | unsigned int i = ring->index; |
| 571 | u32 reg; |
| 572 | |
Florian Fainelli | 5e6ce1f | 2018-03-28 15:15:38 -0700 | [diff] [blame] | 573 | bcmgenet_rdma_ring_writel(priv, i, pkts, DMA_MBUF_DONE_THRESH); |
Florian Fainelli | 9f4ca05 | 2018-03-22 18:19:33 -0700 | [diff] [blame] | 574 | |
| 575 | reg = bcmgenet_rdma_readl(priv, DMA_RING0_TIMEOUT + i); |
| 576 | reg &= ~DMA_TIMEOUT_MASK; |
Florian Fainelli | 5e6ce1f | 2018-03-28 15:15:38 -0700 | [diff] [blame] | 577 | reg |= DIV_ROUND_UP(usecs * 1000, 8192); |
Florian Fainelli | 9f4ca05 | 2018-03-22 18:19:33 -0700 | [diff] [blame] | 578 | bcmgenet_rdma_writel(priv, reg, DMA_RING0_TIMEOUT + i); |
| 579 | } |
| 580 | |
Florian Fainelli | 5e6ce1f | 2018-03-28 15:15:38 -0700 | [diff] [blame] | 581 | static void bcmgenet_set_ring_rx_coalesce(struct bcmgenet_rx_ring *ring, |
| 582 | struct ethtool_coalesce *ec) |
| 583 | { |
Tal Gilboa | 8960b38 | 2019-01-31 16:44:48 +0200 | [diff] [blame] | 584 | struct dim_cq_moder moder; |
Florian Fainelli | 5e6ce1f | 2018-03-28 15:15:38 -0700 | [diff] [blame] | 585 | u32 usecs, pkts; |
| 586 | |
| 587 | ring->rx_coalesce_usecs = ec->rx_coalesce_usecs; |
| 588 | ring->rx_max_coalesced_frames = ec->rx_max_coalesced_frames; |
| 589 | usecs = ring->rx_coalesce_usecs; |
| 590 | pkts = ring->rx_max_coalesced_frames; |
| 591 | |
| 592 | if (ec->use_adaptive_rx_coalesce && !ring->dim.use_dim) { |
Tal Gilboa | 026a807 | 2018-04-24 13:36:01 +0300 | [diff] [blame] | 593 | moder = net_dim_get_def_rx_moderation(ring->dim.dim.mode); |
Florian Fainelli | 5e6ce1f | 2018-03-28 15:15:38 -0700 | [diff] [blame] | 594 | usecs = moder.usec; |
| 595 | pkts = moder.pkts; |
| 596 | } |
| 597 | |
| 598 | ring->dim.use_dim = ec->use_adaptive_rx_coalesce; |
| 599 | bcmgenet_set_rx_coalesce(ring, usecs, pkts); |
| 600 | } |
| 601 | |
Florian Fainelli | 2f91307 | 2015-09-16 16:47:39 -0700 | [diff] [blame] | 602 | static int bcmgenet_set_coalesce(struct net_device *dev, |
| 603 | struct ethtool_coalesce *ec) |
| 604 | { |
| 605 | struct bcmgenet_priv *priv = netdev_priv(dev); |
| 606 | unsigned int i; |
| 607 | |
Florian Fainelli | 4a29645 | 2015-09-16 16:47:40 -0700 | [diff] [blame] | 608 | /* Base system clock is 125Mhz, DMA timeout is this reference clock |
| 609 | * divided by 1024, which yields roughly 8.192us, our maximum value |
| 610 | * has to fit in the DMA_TIMEOUT_MASK (16 bits) |
| 611 | */ |
Florian Fainelli | 2f91307 | 2015-09-16 16:47:39 -0700 | [diff] [blame] | 612 | if (ec->tx_max_coalesced_frames > DMA_INTR_THRESHOLD_MASK || |
Florian Fainelli | 4a29645 | 2015-09-16 16:47:40 -0700 | [diff] [blame] | 613 | ec->tx_max_coalesced_frames == 0 || |
| 614 | ec->rx_max_coalesced_frames > DMA_INTR_THRESHOLD_MASK || |
| 615 | ec->rx_coalesce_usecs > (DMA_TIMEOUT_MASK * 8) + 1) |
| 616 | return -EINVAL; |
| 617 | |
| 618 | if (ec->rx_coalesce_usecs == 0 && ec->rx_max_coalesced_frames == 0) |
Florian Fainelli | 2f91307 | 2015-09-16 16:47:39 -0700 | [diff] [blame] | 619 | return -EINVAL; |
| 620 | |
| 621 | /* GENET TDMA hardware does not support a configurable timeout, but will |
| 622 | * always generate an interrupt either after MBDONE packets have been |
Doug Berger | 556c2cf | 2017-03-13 17:41:34 -0700 | [diff] [blame] | 623 | * transmitted, or when the ring is empty. |
Florian Fainelli | 2f91307 | 2015-09-16 16:47:39 -0700 | [diff] [blame] | 624 | */ |
Florian Fainelli | 2f91307 | 2015-09-16 16:47:39 -0700 | [diff] [blame] | 625 | |
| 626 | /* Program all TX queues with the same values, as there is no |
| 627 | * ethtool knob to do coalescing on a per-queue basis |
| 628 | */ |
| 629 | for (i = 0; i < priv->hw_params->tx_queues; i++) |
| 630 | bcmgenet_tdma_ring_writel(priv, i, |
| 631 | ec->tx_max_coalesced_frames, |
| 632 | DMA_MBUF_DONE_THRESH); |
| 633 | bcmgenet_tdma_ring_writel(priv, DESC_INDEX, |
| 634 | ec->tx_max_coalesced_frames, |
| 635 | DMA_MBUF_DONE_THRESH); |
| 636 | |
Florian Fainelli | 5e6ce1f | 2018-03-28 15:15:38 -0700 | [diff] [blame] | 637 | for (i = 0; i < priv->hw_params->rx_queues; i++) |
| 638 | bcmgenet_set_ring_rx_coalesce(&priv->rx_rings[i], ec); |
| 639 | bcmgenet_set_ring_rx_coalesce(&priv->rx_rings[DESC_INDEX], ec); |
Florian Fainelli | 4a29645 | 2015-09-16 16:47:40 -0700 | [diff] [blame] | 640 | |
Florian Fainelli | 2f91307 | 2015-09-16 16:47:39 -0700 | [diff] [blame] | 641 | return 0; |
| 642 | } |
| 643 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 644 | /* standard ethtool support functions. */ |
| 645 | enum bcmgenet_stat_type { |
| 646 | BCMGENET_STAT_NETDEV = -1, |
| 647 | BCMGENET_STAT_MIB_RX, |
| 648 | BCMGENET_STAT_MIB_TX, |
| 649 | BCMGENET_STAT_RUNT, |
| 650 | BCMGENET_STAT_MISC, |
Florian Fainelli | f62ba9c | 2015-02-28 18:09:16 -0800 | [diff] [blame] | 651 | BCMGENET_STAT_SOFT, |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 652 | }; |
| 653 | |
| 654 | struct bcmgenet_stats { |
| 655 | char stat_string[ETH_GSTRING_LEN]; |
| 656 | int stat_sizeof; |
| 657 | int stat_offset; |
| 658 | enum bcmgenet_stat_type type; |
| 659 | /* reg offset from UMAC base for misc counters */ |
| 660 | u16 reg_offset; |
| 661 | }; |
| 662 | |
| 663 | #define STAT_NETDEV(m) { \ |
| 664 | .stat_string = __stringify(m), \ |
| 665 | .stat_sizeof = sizeof(((struct net_device_stats *)0)->m), \ |
| 666 | .stat_offset = offsetof(struct net_device_stats, m), \ |
| 667 | .type = BCMGENET_STAT_NETDEV, \ |
| 668 | } |
| 669 | |
| 670 | #define STAT_GENET_MIB(str, m, _type) { \ |
| 671 | .stat_string = str, \ |
| 672 | .stat_sizeof = sizeof(((struct bcmgenet_priv *)0)->m), \ |
| 673 | .stat_offset = offsetof(struct bcmgenet_priv, m), \ |
| 674 | .type = _type, \ |
| 675 | } |
| 676 | |
| 677 | #define STAT_GENET_MIB_RX(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_MIB_RX) |
| 678 | #define STAT_GENET_MIB_TX(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_MIB_TX) |
| 679 | #define STAT_GENET_RUNT(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_RUNT) |
Florian Fainelli | f62ba9c | 2015-02-28 18:09:16 -0800 | [diff] [blame] | 680 | #define STAT_GENET_SOFT_MIB(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_SOFT) |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 681 | |
| 682 | #define STAT_GENET_MISC(str, m, offset) { \ |
| 683 | .stat_string = str, \ |
| 684 | .stat_sizeof = sizeof(((struct bcmgenet_priv *)0)->m), \ |
| 685 | .stat_offset = offsetof(struct bcmgenet_priv, m), \ |
| 686 | .type = BCMGENET_STAT_MISC, \ |
| 687 | .reg_offset = offset, \ |
| 688 | } |
| 689 | |
Florian Fainelli | 37a30b4 | 2017-03-16 10:27:08 -0700 | [diff] [blame] | 690 | #define STAT_GENET_Q(num) \ |
| 691 | STAT_GENET_SOFT_MIB("txq" __stringify(num) "_packets", \ |
| 692 | tx_rings[num].packets), \ |
| 693 | STAT_GENET_SOFT_MIB("txq" __stringify(num) "_bytes", \ |
| 694 | tx_rings[num].bytes), \ |
| 695 | STAT_GENET_SOFT_MIB("rxq" __stringify(num) "_bytes", \ |
| 696 | rx_rings[num].bytes), \ |
| 697 | STAT_GENET_SOFT_MIB("rxq" __stringify(num) "_packets", \ |
| 698 | rx_rings[num].packets), \ |
| 699 | STAT_GENET_SOFT_MIB("rxq" __stringify(num) "_errors", \ |
| 700 | rx_rings[num].errors), \ |
| 701 | STAT_GENET_SOFT_MIB("rxq" __stringify(num) "_dropped", \ |
| 702 | rx_rings[num].dropped) |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 703 | |
| 704 | /* There is a 0xC gap between the end of RX and beginning of TX stats and then |
| 705 | * between the end of TX stats and the beginning of the RX RUNT |
| 706 | */ |
| 707 | #define BCMGENET_STAT_OFFSET 0xc |
| 708 | |
| 709 | /* Hardware counters must be kept in sync because the order/offset |
| 710 | * is important here (order in structure declaration = order in hardware) |
| 711 | */ |
| 712 | static const struct bcmgenet_stats bcmgenet_gstrings_stats[] = { |
| 713 | /* general stats */ |
| 714 | STAT_NETDEV(rx_packets), |
| 715 | STAT_NETDEV(tx_packets), |
| 716 | STAT_NETDEV(rx_bytes), |
| 717 | STAT_NETDEV(tx_bytes), |
| 718 | STAT_NETDEV(rx_errors), |
| 719 | STAT_NETDEV(tx_errors), |
| 720 | STAT_NETDEV(rx_dropped), |
| 721 | STAT_NETDEV(tx_dropped), |
| 722 | STAT_NETDEV(multicast), |
| 723 | /* UniMAC RSV counters */ |
| 724 | STAT_GENET_MIB_RX("rx_64_octets", mib.rx.pkt_cnt.cnt_64), |
| 725 | STAT_GENET_MIB_RX("rx_65_127_oct", mib.rx.pkt_cnt.cnt_127), |
| 726 | STAT_GENET_MIB_RX("rx_128_255_oct", mib.rx.pkt_cnt.cnt_255), |
| 727 | STAT_GENET_MIB_RX("rx_256_511_oct", mib.rx.pkt_cnt.cnt_511), |
| 728 | STAT_GENET_MIB_RX("rx_512_1023_oct", mib.rx.pkt_cnt.cnt_1023), |
| 729 | STAT_GENET_MIB_RX("rx_1024_1518_oct", mib.rx.pkt_cnt.cnt_1518), |
| 730 | STAT_GENET_MIB_RX("rx_vlan_1519_1522_oct", mib.rx.pkt_cnt.cnt_mgv), |
| 731 | STAT_GENET_MIB_RX("rx_1522_2047_oct", mib.rx.pkt_cnt.cnt_2047), |
| 732 | STAT_GENET_MIB_RX("rx_2048_4095_oct", mib.rx.pkt_cnt.cnt_4095), |
| 733 | STAT_GENET_MIB_RX("rx_4096_9216_oct", mib.rx.pkt_cnt.cnt_9216), |
| 734 | STAT_GENET_MIB_RX("rx_pkts", mib.rx.pkt), |
| 735 | STAT_GENET_MIB_RX("rx_bytes", mib.rx.bytes), |
| 736 | STAT_GENET_MIB_RX("rx_multicast", mib.rx.mca), |
| 737 | STAT_GENET_MIB_RX("rx_broadcast", mib.rx.bca), |
| 738 | STAT_GENET_MIB_RX("rx_fcs", mib.rx.fcs), |
| 739 | STAT_GENET_MIB_RX("rx_control", mib.rx.cf), |
| 740 | STAT_GENET_MIB_RX("rx_pause", mib.rx.pf), |
| 741 | STAT_GENET_MIB_RX("rx_unknown", mib.rx.uo), |
| 742 | STAT_GENET_MIB_RX("rx_align", mib.rx.aln), |
| 743 | STAT_GENET_MIB_RX("rx_outrange", mib.rx.flr), |
| 744 | STAT_GENET_MIB_RX("rx_code", mib.rx.cde), |
| 745 | STAT_GENET_MIB_RX("rx_carrier", mib.rx.fcr), |
| 746 | STAT_GENET_MIB_RX("rx_oversize", mib.rx.ovr), |
| 747 | STAT_GENET_MIB_RX("rx_jabber", mib.rx.jbr), |
| 748 | STAT_GENET_MIB_RX("rx_mtu_err", mib.rx.mtue), |
| 749 | STAT_GENET_MIB_RX("rx_good_pkts", mib.rx.pok), |
| 750 | STAT_GENET_MIB_RX("rx_unicast", mib.rx.uc), |
| 751 | STAT_GENET_MIB_RX("rx_ppp", mib.rx.ppp), |
| 752 | STAT_GENET_MIB_RX("rx_crc", mib.rx.rcrc), |
| 753 | /* UniMAC TSV counters */ |
| 754 | STAT_GENET_MIB_TX("tx_64_octets", mib.tx.pkt_cnt.cnt_64), |
| 755 | STAT_GENET_MIB_TX("tx_65_127_oct", mib.tx.pkt_cnt.cnt_127), |
| 756 | STAT_GENET_MIB_TX("tx_128_255_oct", mib.tx.pkt_cnt.cnt_255), |
| 757 | STAT_GENET_MIB_TX("tx_256_511_oct", mib.tx.pkt_cnt.cnt_511), |
| 758 | STAT_GENET_MIB_TX("tx_512_1023_oct", mib.tx.pkt_cnt.cnt_1023), |
| 759 | STAT_GENET_MIB_TX("tx_1024_1518_oct", mib.tx.pkt_cnt.cnt_1518), |
| 760 | STAT_GENET_MIB_TX("tx_vlan_1519_1522_oct", mib.tx.pkt_cnt.cnt_mgv), |
| 761 | STAT_GENET_MIB_TX("tx_1522_2047_oct", mib.tx.pkt_cnt.cnt_2047), |
| 762 | STAT_GENET_MIB_TX("tx_2048_4095_oct", mib.tx.pkt_cnt.cnt_4095), |
| 763 | STAT_GENET_MIB_TX("tx_4096_9216_oct", mib.tx.pkt_cnt.cnt_9216), |
| 764 | STAT_GENET_MIB_TX("tx_pkts", mib.tx.pkts), |
| 765 | STAT_GENET_MIB_TX("tx_multicast", mib.tx.mca), |
| 766 | STAT_GENET_MIB_TX("tx_broadcast", mib.tx.bca), |
| 767 | STAT_GENET_MIB_TX("tx_pause", mib.tx.pf), |
| 768 | STAT_GENET_MIB_TX("tx_control", mib.tx.cf), |
| 769 | STAT_GENET_MIB_TX("tx_fcs_err", mib.tx.fcs), |
| 770 | STAT_GENET_MIB_TX("tx_oversize", mib.tx.ovr), |
| 771 | STAT_GENET_MIB_TX("tx_defer", mib.tx.drf), |
| 772 | STAT_GENET_MIB_TX("tx_excess_defer", mib.tx.edf), |
| 773 | STAT_GENET_MIB_TX("tx_single_col", mib.tx.scl), |
| 774 | STAT_GENET_MIB_TX("tx_multi_col", mib.tx.mcl), |
| 775 | STAT_GENET_MIB_TX("tx_late_col", mib.tx.lcl), |
| 776 | STAT_GENET_MIB_TX("tx_excess_col", mib.tx.ecl), |
| 777 | STAT_GENET_MIB_TX("tx_frags", mib.tx.frg), |
| 778 | STAT_GENET_MIB_TX("tx_total_col", mib.tx.ncl), |
| 779 | STAT_GENET_MIB_TX("tx_jabber", mib.tx.jbr), |
| 780 | STAT_GENET_MIB_TX("tx_bytes", mib.tx.bytes), |
| 781 | STAT_GENET_MIB_TX("tx_good_pkts", mib.tx.pok), |
| 782 | STAT_GENET_MIB_TX("tx_unicast", mib.tx.uc), |
| 783 | /* UniMAC RUNT counters */ |
| 784 | STAT_GENET_RUNT("rx_runt_pkts", mib.rx_runt_cnt), |
| 785 | STAT_GENET_RUNT("rx_runt_valid_fcs", mib.rx_runt_fcs), |
| 786 | STAT_GENET_RUNT("rx_runt_inval_fcs_align", mib.rx_runt_fcs_align), |
| 787 | STAT_GENET_RUNT("rx_runt_bytes", mib.rx_runt_bytes), |
| 788 | /* Misc UniMAC counters */ |
| 789 | STAT_GENET_MISC("rbuf_ovflow_cnt", mib.rbuf_ovflow_cnt, |
Doug Berger | ffff713 | 2017-03-09 16:58:43 -0800 | [diff] [blame] | 790 | UMAC_RBUF_OVFL_CNT_V1), |
| 791 | STAT_GENET_MISC("rbuf_err_cnt", mib.rbuf_err_cnt, |
| 792 | UMAC_RBUF_ERR_CNT_V1), |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 793 | STAT_GENET_MISC("mdf_err_cnt", mib.mdf_err_cnt, UMAC_MDF_ERR_CNT), |
Florian Fainelli | f62ba9c | 2015-02-28 18:09:16 -0800 | [diff] [blame] | 794 | STAT_GENET_SOFT_MIB("alloc_rx_buff_failed", mib.alloc_rx_buff_failed), |
| 795 | STAT_GENET_SOFT_MIB("rx_dma_failed", mib.rx_dma_failed), |
| 796 | STAT_GENET_SOFT_MIB("tx_dma_failed", mib.tx_dma_failed), |
Doug Berger | f1af17c | 2019-12-17 16:51:15 -0800 | [diff] [blame] | 797 | STAT_GENET_SOFT_MIB("tx_realloc_tsb", mib.tx_realloc_tsb), |
| 798 | STAT_GENET_SOFT_MIB("tx_realloc_tsb_failed", |
| 799 | mib.tx_realloc_tsb_failed), |
Florian Fainelli | 37a30b4 | 2017-03-16 10:27:08 -0700 | [diff] [blame] | 800 | /* Per TX queues */ |
| 801 | STAT_GENET_Q(0), |
| 802 | STAT_GENET_Q(1), |
| 803 | STAT_GENET_Q(2), |
| 804 | STAT_GENET_Q(3), |
| 805 | STAT_GENET_Q(16), |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 806 | }; |
| 807 | |
| 808 | #define BCMGENET_STATS_LEN ARRAY_SIZE(bcmgenet_gstrings_stats) |
| 809 | |
| 810 | static void bcmgenet_get_drvinfo(struct net_device *dev, |
Florian Fainelli | c91b7f6 | 2014-07-23 10:42:12 -0700 | [diff] [blame] | 811 | struct ethtool_drvinfo *info) |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 812 | { |
| 813 | strlcpy(info->driver, "bcmgenet", sizeof(info->driver)); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | static int bcmgenet_get_sset_count(struct net_device *dev, int string_set) |
| 817 | { |
| 818 | switch (string_set) { |
| 819 | case ETH_SS_STATS: |
| 820 | return BCMGENET_STATS_LEN; |
| 821 | default: |
| 822 | return -EOPNOTSUPP; |
| 823 | } |
| 824 | } |
| 825 | |
Florian Fainelli | c91b7f6 | 2014-07-23 10:42:12 -0700 | [diff] [blame] | 826 | static void bcmgenet_get_strings(struct net_device *dev, u32 stringset, |
| 827 | u8 *data) |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 828 | { |
| 829 | int i; |
| 830 | |
| 831 | switch (stringset) { |
| 832 | case ETH_SS_STATS: |
| 833 | for (i = 0; i < BCMGENET_STATS_LEN; i++) { |
| 834 | memcpy(data + i * ETH_GSTRING_LEN, |
Florian Fainelli | c91b7f6 | 2014-07-23 10:42:12 -0700 | [diff] [blame] | 835 | bcmgenet_gstrings_stats[i].stat_string, |
| 836 | ETH_GSTRING_LEN); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 837 | } |
| 838 | break; |
| 839 | } |
| 840 | } |
| 841 | |
Doug Berger | ffff713 | 2017-03-09 16:58:43 -0800 | [diff] [blame] | 842 | static u32 bcmgenet_update_stat_misc(struct bcmgenet_priv *priv, u16 offset) |
| 843 | { |
| 844 | u16 new_offset; |
| 845 | u32 val; |
| 846 | |
| 847 | switch (offset) { |
| 848 | case UMAC_RBUF_OVFL_CNT_V1: |
| 849 | if (GENET_IS_V2(priv)) |
| 850 | new_offset = RBUF_OVFL_CNT_V2; |
| 851 | else |
| 852 | new_offset = RBUF_OVFL_CNT_V3PLUS; |
| 853 | |
| 854 | val = bcmgenet_rbuf_readl(priv, new_offset); |
| 855 | /* clear if overflowed */ |
| 856 | if (val == ~0) |
| 857 | bcmgenet_rbuf_writel(priv, 0, new_offset); |
| 858 | break; |
| 859 | case UMAC_RBUF_ERR_CNT_V1: |
| 860 | if (GENET_IS_V2(priv)) |
| 861 | new_offset = RBUF_ERR_CNT_V2; |
| 862 | else |
| 863 | new_offset = RBUF_ERR_CNT_V3PLUS; |
| 864 | |
| 865 | val = bcmgenet_rbuf_readl(priv, new_offset); |
| 866 | /* clear if overflowed */ |
| 867 | if (val == ~0) |
| 868 | bcmgenet_rbuf_writel(priv, 0, new_offset); |
| 869 | break; |
| 870 | default: |
| 871 | val = bcmgenet_umac_readl(priv, offset); |
| 872 | /* clear if overflowed */ |
| 873 | if (val == ~0) |
| 874 | bcmgenet_umac_writel(priv, 0, offset); |
| 875 | break; |
| 876 | } |
| 877 | |
| 878 | return val; |
| 879 | } |
| 880 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 881 | static void bcmgenet_update_mib_counters(struct bcmgenet_priv *priv) |
| 882 | { |
| 883 | int i, j = 0; |
| 884 | |
| 885 | for (i = 0; i < BCMGENET_STATS_LEN; i++) { |
| 886 | const struct bcmgenet_stats *s; |
| 887 | u8 offset = 0; |
| 888 | u32 val = 0; |
| 889 | char *p; |
| 890 | |
| 891 | s = &bcmgenet_gstrings_stats[i]; |
| 892 | switch (s->type) { |
| 893 | case BCMGENET_STAT_NETDEV: |
Florian Fainelli | f62ba9c | 2015-02-28 18:09:16 -0800 | [diff] [blame] | 894 | case BCMGENET_STAT_SOFT: |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 895 | continue; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 896 | case BCMGENET_STAT_RUNT: |
Doug Berger | 1ad3d22 | 2017-03-09 16:58:44 -0800 | [diff] [blame] | 897 | offset += BCMGENET_STAT_OFFSET; |
| 898 | /* fall through */ |
| 899 | case BCMGENET_STAT_MIB_TX: |
| 900 | offset += BCMGENET_STAT_OFFSET; |
| 901 | /* fall through */ |
| 902 | case BCMGENET_STAT_MIB_RX: |
Florian Fainelli | c91b7f6 | 2014-07-23 10:42:12 -0700 | [diff] [blame] | 903 | val = bcmgenet_umac_readl(priv, |
| 904 | UMAC_MIB_START + j + offset); |
Doug Berger | 1ad3d22 | 2017-03-09 16:58:44 -0800 | [diff] [blame] | 905 | offset = 0; /* Reset Offset */ |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 906 | break; |
| 907 | case BCMGENET_STAT_MISC: |
Doug Berger | ffff713 | 2017-03-09 16:58:43 -0800 | [diff] [blame] | 908 | if (GENET_IS_V1(priv)) { |
| 909 | val = bcmgenet_umac_readl(priv, s->reg_offset); |
| 910 | /* clear if overflowed */ |
| 911 | if (val == ~0) |
| 912 | bcmgenet_umac_writel(priv, 0, |
| 913 | s->reg_offset); |
| 914 | } else { |
| 915 | val = bcmgenet_update_stat_misc(priv, |
| 916 | s->reg_offset); |
| 917 | } |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 918 | break; |
| 919 | } |
| 920 | |
| 921 | j += s->stat_sizeof; |
| 922 | p = (char *)priv + s->stat_offset; |
| 923 | *(u32 *)p = val; |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | static void bcmgenet_get_ethtool_stats(struct net_device *dev, |
Florian Fainelli | c91b7f6 | 2014-07-23 10:42:12 -0700 | [diff] [blame] | 928 | struct ethtool_stats *stats, |
| 929 | u64 *data) |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 930 | { |
| 931 | struct bcmgenet_priv *priv = netdev_priv(dev); |
| 932 | int i; |
| 933 | |
| 934 | if (netif_running(dev)) |
| 935 | bcmgenet_update_mib_counters(priv); |
| 936 | |
Doug Berger | a6d0b83 | 2020-04-23 15:44:17 -0700 | [diff] [blame^] | 937 | dev->netdev_ops->ndo_get_stats(dev); |
| 938 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 939 | for (i = 0; i < BCMGENET_STATS_LEN; i++) { |
| 940 | const struct bcmgenet_stats *s; |
| 941 | char *p; |
| 942 | |
| 943 | s = &bcmgenet_gstrings_stats[i]; |
| 944 | if (s->type == BCMGENET_STAT_NETDEV) |
| 945 | p = (char *)&dev->stats; |
| 946 | else |
| 947 | p = (char *)priv; |
| 948 | p += s->stat_offset; |
Eric Dumazet | 6517eb5 | 2016-04-15 10:47:52 -0700 | [diff] [blame] | 949 | if (sizeof(unsigned long) != sizeof(u32) && |
| 950 | s->stat_sizeof == sizeof(unsigned long)) |
| 951 | data[i] = *(unsigned long *)p; |
| 952 | else |
| 953 | data[i] = *(u32 *)p; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 954 | } |
| 955 | } |
| 956 | |
Florian Fainelli | 6ef398e | 2014-11-25 21:16:35 -0800 | [diff] [blame] | 957 | static void bcmgenet_eee_enable_set(struct net_device *dev, bool enable) |
| 958 | { |
| 959 | struct bcmgenet_priv *priv = netdev_priv(dev); |
| 960 | u32 off = priv->hw_params->tbuf_offset + TBUF_ENERGY_CTRL; |
| 961 | u32 reg; |
| 962 | |
| 963 | if (enable && !priv->clk_eee_enabled) { |
| 964 | clk_prepare_enable(priv->clk_eee); |
| 965 | priv->clk_eee_enabled = true; |
| 966 | } |
| 967 | |
| 968 | reg = bcmgenet_umac_readl(priv, UMAC_EEE_CTRL); |
| 969 | if (enable) |
| 970 | reg |= EEE_EN; |
| 971 | else |
| 972 | reg &= ~EEE_EN; |
| 973 | bcmgenet_umac_writel(priv, reg, UMAC_EEE_CTRL); |
| 974 | |
| 975 | /* Enable EEE and switch to a 27Mhz clock automatically */ |
Florian Fainelli | 69d2ea9 | 2017-08-29 12:25:31 -0700 | [diff] [blame] | 976 | reg = bcmgenet_readl(priv->base + off); |
Florian Fainelli | 6ef398e | 2014-11-25 21:16:35 -0800 | [diff] [blame] | 977 | if (enable) |
| 978 | reg |= TBUF_EEE_EN | TBUF_PM_EN; |
| 979 | else |
| 980 | reg &= ~(TBUF_EEE_EN | TBUF_PM_EN); |
Florian Fainelli | 69d2ea9 | 2017-08-29 12:25:31 -0700 | [diff] [blame] | 981 | bcmgenet_writel(reg, priv->base + off); |
Florian Fainelli | 6ef398e | 2014-11-25 21:16:35 -0800 | [diff] [blame] | 982 | |
| 983 | /* Do the same for thing for RBUF */ |
| 984 | reg = bcmgenet_rbuf_readl(priv, RBUF_ENERGY_CTRL); |
| 985 | if (enable) |
| 986 | reg |= RBUF_EEE_EN | RBUF_PM_EN; |
| 987 | else |
| 988 | reg &= ~(RBUF_EEE_EN | RBUF_PM_EN); |
| 989 | bcmgenet_rbuf_writel(priv, reg, RBUF_ENERGY_CTRL); |
| 990 | |
| 991 | if (!enable && priv->clk_eee_enabled) { |
| 992 | clk_disable_unprepare(priv->clk_eee); |
| 993 | priv->clk_eee_enabled = false; |
| 994 | } |
| 995 | |
| 996 | priv->eee.eee_enabled = enable; |
| 997 | priv->eee.eee_active = enable; |
| 998 | } |
| 999 | |
| 1000 | static int bcmgenet_get_eee(struct net_device *dev, struct ethtool_eee *e) |
| 1001 | { |
| 1002 | struct bcmgenet_priv *priv = netdev_priv(dev); |
| 1003 | struct ethtool_eee *p = &priv->eee; |
| 1004 | |
| 1005 | if (GENET_IS_V1(priv)) |
| 1006 | return -EOPNOTSUPP; |
| 1007 | |
Doug Berger | 6c97f01 | 2017-10-25 15:04:19 -0700 | [diff] [blame] | 1008 | if (!dev->phydev) |
| 1009 | return -ENODEV; |
| 1010 | |
Florian Fainelli | 6ef398e | 2014-11-25 21:16:35 -0800 | [diff] [blame] | 1011 | e->eee_enabled = p->eee_enabled; |
| 1012 | e->eee_active = p->eee_active; |
| 1013 | e->tx_lpi_timer = bcmgenet_umac_readl(priv, UMAC_EEE_LPI_TIMER); |
| 1014 | |
Doug Berger | 6c97f01 | 2017-10-25 15:04:19 -0700 | [diff] [blame] | 1015 | return phy_ethtool_get_eee(dev->phydev, e); |
Florian Fainelli | 6ef398e | 2014-11-25 21:16:35 -0800 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | static int bcmgenet_set_eee(struct net_device *dev, struct ethtool_eee *e) |
| 1019 | { |
| 1020 | struct bcmgenet_priv *priv = netdev_priv(dev); |
| 1021 | struct ethtool_eee *p = &priv->eee; |
| 1022 | int ret = 0; |
| 1023 | |
| 1024 | if (GENET_IS_V1(priv)) |
| 1025 | return -EOPNOTSUPP; |
| 1026 | |
Doug Berger | 6c97f01 | 2017-10-25 15:04:19 -0700 | [diff] [blame] | 1027 | if (!dev->phydev) |
| 1028 | return -ENODEV; |
| 1029 | |
Florian Fainelli | 6ef398e | 2014-11-25 21:16:35 -0800 | [diff] [blame] | 1030 | p->eee_enabled = e->eee_enabled; |
| 1031 | |
| 1032 | if (!p->eee_enabled) { |
| 1033 | bcmgenet_eee_enable_set(dev, false); |
| 1034 | } else { |
Doug Berger | 6c97f01 | 2017-10-25 15:04:19 -0700 | [diff] [blame] | 1035 | ret = phy_init_eee(dev->phydev, 0); |
Florian Fainelli | 6ef398e | 2014-11-25 21:16:35 -0800 | [diff] [blame] | 1036 | if (ret) { |
| 1037 | netif_err(priv, hw, dev, "EEE initialization failed\n"); |
| 1038 | return ret; |
| 1039 | } |
| 1040 | |
| 1041 | bcmgenet_umac_writel(priv, e->tx_lpi_timer, UMAC_EEE_LPI_TIMER); |
| 1042 | bcmgenet_eee_enable_set(dev, true); |
| 1043 | } |
| 1044 | |
Doug Berger | 6c97f01 | 2017-10-25 15:04:19 -0700 | [diff] [blame] | 1045 | return phy_ethtool_set_eee(dev->phydev, e); |
Florian Fainelli | 6ef398e | 2014-11-25 21:16:35 -0800 | [diff] [blame] | 1046 | } |
| 1047 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1048 | /* standard ethtool support functions. */ |
Julia Lawall | 70591ab | 2016-08-31 09:30:45 +0200 | [diff] [blame] | 1049 | static const struct ethtool_ops bcmgenet_ethtool_ops = { |
Jakub Kicinski | f6f508c | 2020-03-09 19:15:03 -0700 | [diff] [blame] | 1050 | .supported_coalesce_params = ETHTOOL_COALESCE_RX_USECS | |
| 1051 | ETHTOOL_COALESCE_MAX_FRAMES | |
| 1052 | ETHTOOL_COALESCE_USE_ADAPTIVE_RX, |
Edwin Chan | 89316fa | 2017-03-09 16:58:49 -0800 | [diff] [blame] | 1053 | .begin = bcmgenet_begin, |
| 1054 | .complete = bcmgenet_complete, |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1055 | .get_strings = bcmgenet_get_strings, |
| 1056 | .get_sset_count = bcmgenet_get_sset_count, |
| 1057 | .get_ethtool_stats = bcmgenet_get_ethtool_stats, |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1058 | .get_drvinfo = bcmgenet_get_drvinfo, |
| 1059 | .get_link = ethtool_op_get_link, |
| 1060 | .get_msglevel = bcmgenet_get_msglevel, |
| 1061 | .set_msglevel = bcmgenet_set_msglevel, |
Florian Fainelli | 06ba837 | 2014-07-21 15:29:29 -0700 | [diff] [blame] | 1062 | .get_wol = bcmgenet_get_wol, |
| 1063 | .set_wol = bcmgenet_set_wol, |
Florian Fainelli | 6ef398e | 2014-11-25 21:16:35 -0800 | [diff] [blame] | 1064 | .get_eee = bcmgenet_get_eee, |
| 1065 | .set_eee = bcmgenet_set_eee, |
Florian Fainelli | 016e770 | 2016-11-15 10:06:38 -0800 | [diff] [blame] | 1066 | .nway_reset = phy_ethtool_nway_reset, |
Florian Fainelli | 2f91307 | 2015-09-16 16:47:39 -0700 | [diff] [blame] | 1067 | .get_coalesce = bcmgenet_get_coalesce, |
| 1068 | .set_coalesce = bcmgenet_set_coalesce, |
Philippe Reynes | fa92bf0 | 2016-09-26 22:31:57 +0200 | [diff] [blame] | 1069 | .get_link_ksettings = bcmgenet_get_link_ksettings, |
| 1070 | .set_link_ksettings = bcmgenet_set_link_ksettings, |
Ryan M. Collins | dd1bf47 | 2019-08-30 14:49:55 -0400 | [diff] [blame] | 1071 | .get_ts_info = ethtool_op_get_ts_info, |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1072 | }; |
| 1073 | |
| 1074 | /* Power down the unimac, based on mode. */ |
Florian Fainelli | ca8cf34 | 2015-03-23 15:09:51 -0700 | [diff] [blame] | 1075 | static int bcmgenet_power_down(struct bcmgenet_priv *priv, |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1076 | enum bcmgenet_power_mode mode) |
| 1077 | { |
Florian Fainelli | ca8cf34 | 2015-03-23 15:09:51 -0700 | [diff] [blame] | 1078 | int ret = 0; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1079 | u32 reg; |
| 1080 | |
| 1081 | switch (mode) { |
| 1082 | case GENET_POWER_CABLE_SENSE: |
Doug Berger | 6c97f01 | 2017-10-25 15:04:19 -0700 | [diff] [blame] | 1083 | phy_detach(priv->dev->phydev); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1084 | break; |
| 1085 | |
Florian Fainelli | c3ae64a | 2014-07-21 15:29:25 -0700 | [diff] [blame] | 1086 | case GENET_POWER_WOL_MAGIC: |
Florian Fainelli | ca8cf34 | 2015-03-23 15:09:51 -0700 | [diff] [blame] | 1087 | ret = bcmgenet_wol_power_down_cfg(priv, mode); |
Florian Fainelli | c3ae64a | 2014-07-21 15:29:25 -0700 | [diff] [blame] | 1088 | break; |
| 1089 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1090 | case GENET_POWER_PASSIVE: |
| 1091 | /* Power down LED */ |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1092 | if (priv->hw_params->flags & GENET_HAS_EXT) { |
| 1093 | reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT); |
Doug Berger | 4213808 | 2017-03-13 17:41:42 -0700 | [diff] [blame] | 1094 | if (GENET_IS_V5(priv)) |
| 1095 | reg |= EXT_PWR_DOWN_PHY_EN | |
| 1096 | EXT_PWR_DOWN_PHY_RD | |
| 1097 | EXT_PWR_DOWN_PHY_SD | |
| 1098 | EXT_PWR_DOWN_PHY_RX | |
| 1099 | EXT_PWR_DOWN_PHY_TX | |
| 1100 | EXT_IDDQ_GLBL_PWR; |
| 1101 | else |
| 1102 | reg |= EXT_PWR_DOWN_PHY; |
| 1103 | |
| 1104 | reg |= (EXT_PWR_DOWN_DLL | EXT_PWR_DOWN_BIAS); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1105 | bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT); |
Florian Fainelli | a642c4f | 2015-03-23 15:09:56 -0700 | [diff] [blame] | 1106 | |
| 1107 | bcmgenet_phy_power_set(priv->dev, false); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1108 | } |
| 1109 | break; |
| 1110 | default: |
| 1111 | break; |
| 1112 | } |
Florian Fainelli | ca8cf34 | 2015-03-23 15:09:51 -0700 | [diff] [blame] | 1113 | |
YueHaibing | 0db5509 | 2018-11-08 02:08:43 +0000 | [diff] [blame] | 1114 | return ret; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | static void bcmgenet_power_up(struct bcmgenet_priv *priv, |
Florian Fainelli | c91b7f6 | 2014-07-23 10:42:12 -0700 | [diff] [blame] | 1118 | enum bcmgenet_power_mode mode) |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1119 | { |
| 1120 | u32 reg; |
| 1121 | |
| 1122 | if (!(priv->hw_params->flags & GENET_HAS_EXT)) |
| 1123 | return; |
| 1124 | |
| 1125 | reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT); |
| 1126 | |
| 1127 | switch (mode) { |
| 1128 | case GENET_POWER_PASSIVE: |
Doug Berger | 4213808 | 2017-03-13 17:41:42 -0700 | [diff] [blame] | 1129 | reg &= ~(EXT_PWR_DOWN_DLL | EXT_PWR_DOWN_BIAS); |
| 1130 | if (GENET_IS_V5(priv)) { |
| 1131 | reg &= ~(EXT_PWR_DOWN_PHY_EN | |
| 1132 | EXT_PWR_DOWN_PHY_RD | |
| 1133 | EXT_PWR_DOWN_PHY_SD | |
| 1134 | EXT_PWR_DOWN_PHY_RX | |
| 1135 | EXT_PWR_DOWN_PHY_TX | |
| 1136 | EXT_IDDQ_GLBL_PWR); |
| 1137 | reg |= EXT_PHY_RESET; |
| 1138 | bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT); |
| 1139 | mdelay(1); |
| 1140 | |
| 1141 | reg &= ~EXT_PHY_RESET; |
| 1142 | } else { |
| 1143 | reg &= ~EXT_PWR_DOWN_PHY; |
| 1144 | reg |= EXT_PWR_DN_EN_LD; |
| 1145 | } |
| 1146 | bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT); |
| 1147 | bcmgenet_phy_power_set(priv->dev, true); |
Doug Berger | 4213808 | 2017-03-13 17:41:42 -0700 | [diff] [blame] | 1148 | break; |
| 1149 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1150 | case GENET_POWER_CABLE_SENSE: |
| 1151 | /* enable APD */ |
Doug Berger | 4213808 | 2017-03-13 17:41:42 -0700 | [diff] [blame] | 1152 | if (!GENET_IS_V5(priv)) { |
| 1153 | reg |= EXT_PWR_DN_EN_LD; |
| 1154 | bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT); |
| 1155 | } |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1156 | break; |
Florian Fainelli | c3ae64a | 2014-07-21 15:29:25 -0700 | [diff] [blame] | 1157 | case GENET_POWER_WOL_MAGIC: |
| 1158 | bcmgenet_wol_power_up_cfg(priv, mode); |
| 1159 | return; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1160 | default: |
| 1161 | break; |
| 1162 | } |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1163 | } |
| 1164 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1165 | static struct enet_cb *bcmgenet_get_txcb(struct bcmgenet_priv *priv, |
| 1166 | struct bcmgenet_tx_ring *ring) |
| 1167 | { |
| 1168 | struct enet_cb *tx_cb_ptr; |
| 1169 | |
| 1170 | tx_cb_ptr = ring->cbs; |
| 1171 | tx_cb_ptr += ring->write_ptr - ring->cb_ptr; |
Petri Gynther | 014012a | 2015-02-23 11:00:45 -0800 | [diff] [blame] | 1172 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1173 | /* Advancing local write pointer */ |
| 1174 | if (ring->write_ptr == ring->end_ptr) |
| 1175 | ring->write_ptr = ring->cb_ptr; |
| 1176 | else |
| 1177 | ring->write_ptr++; |
| 1178 | |
| 1179 | return tx_cb_ptr; |
| 1180 | } |
| 1181 | |
Doug Berger | 876dbad | 2017-07-14 16:12:09 -0700 | [diff] [blame] | 1182 | static struct enet_cb *bcmgenet_put_txcb(struct bcmgenet_priv *priv, |
| 1183 | struct bcmgenet_tx_ring *ring) |
| 1184 | { |
| 1185 | struct enet_cb *tx_cb_ptr; |
| 1186 | |
| 1187 | tx_cb_ptr = ring->cbs; |
| 1188 | tx_cb_ptr += ring->write_ptr - ring->cb_ptr; |
| 1189 | |
| 1190 | /* Rewinding local write pointer */ |
| 1191 | if (ring->write_ptr == ring->cb_ptr) |
| 1192 | ring->write_ptr = ring->end_ptr; |
| 1193 | else |
| 1194 | ring->write_ptr--; |
| 1195 | |
| 1196 | return tx_cb_ptr; |
| 1197 | } |
| 1198 | |
Petri Gynther | 4055eae | 2015-03-25 12:35:16 -0700 | [diff] [blame] | 1199 | static inline void bcmgenet_rx_ring16_int_disable(struct bcmgenet_rx_ring *ring) |
| 1200 | { |
Petri Gynther | ee7d8c2 | 2015-03-30 00:28:50 -0700 | [diff] [blame] | 1201 | bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_RXDMA_DONE, |
Petri Gynther | 4055eae | 2015-03-25 12:35:16 -0700 | [diff] [blame] | 1202 | INTRL2_CPU_MASK_SET); |
| 1203 | } |
| 1204 | |
| 1205 | static inline void bcmgenet_rx_ring16_int_enable(struct bcmgenet_rx_ring *ring) |
| 1206 | { |
Petri Gynther | ee7d8c2 | 2015-03-30 00:28:50 -0700 | [diff] [blame] | 1207 | bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_RXDMA_DONE, |
Petri Gynther | 4055eae | 2015-03-25 12:35:16 -0700 | [diff] [blame] | 1208 | INTRL2_CPU_MASK_CLEAR); |
| 1209 | } |
| 1210 | |
| 1211 | static inline void bcmgenet_rx_ring_int_disable(struct bcmgenet_rx_ring *ring) |
| 1212 | { |
| 1213 | bcmgenet_intrl2_1_writel(ring->priv, |
| 1214 | 1 << (UMAC_IRQ1_RX_INTR_SHIFT + ring->index), |
| 1215 | INTRL2_CPU_MASK_SET); |
| 1216 | } |
| 1217 | |
| 1218 | static inline void bcmgenet_rx_ring_int_enable(struct bcmgenet_rx_ring *ring) |
| 1219 | { |
| 1220 | bcmgenet_intrl2_1_writel(ring->priv, |
| 1221 | 1 << (UMAC_IRQ1_RX_INTR_SHIFT + ring->index), |
| 1222 | INTRL2_CPU_MASK_CLEAR); |
| 1223 | } |
| 1224 | |
Petri Gynther | 9dbac28 | 2015-03-25 12:35:10 -0700 | [diff] [blame] | 1225 | static inline void bcmgenet_tx_ring16_int_disable(struct bcmgenet_tx_ring *ring) |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1226 | { |
Petri Gynther | ee7d8c2 | 2015-03-30 00:28:50 -0700 | [diff] [blame] | 1227 | bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_TXDMA_DONE, |
Florian Fainelli | c91b7f6 | 2014-07-23 10:42:12 -0700 | [diff] [blame] | 1228 | INTRL2_CPU_MASK_SET); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1229 | } |
| 1230 | |
Petri Gynther | 9dbac28 | 2015-03-25 12:35:10 -0700 | [diff] [blame] | 1231 | static inline void bcmgenet_tx_ring16_int_enable(struct bcmgenet_tx_ring *ring) |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1232 | { |
Petri Gynther | ee7d8c2 | 2015-03-30 00:28:50 -0700 | [diff] [blame] | 1233 | bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_TXDMA_DONE, |
Florian Fainelli | c91b7f6 | 2014-07-23 10:42:12 -0700 | [diff] [blame] | 1234 | INTRL2_CPU_MASK_CLEAR); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1235 | } |
| 1236 | |
Petri Gynther | 9dbac28 | 2015-03-25 12:35:10 -0700 | [diff] [blame] | 1237 | static inline void bcmgenet_tx_ring_int_enable(struct bcmgenet_tx_ring *ring) |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1238 | { |
Petri Gynther | 9dbac28 | 2015-03-25 12:35:10 -0700 | [diff] [blame] | 1239 | bcmgenet_intrl2_1_writel(ring->priv, 1 << ring->index, |
Florian Fainelli | c91b7f6 | 2014-07-23 10:42:12 -0700 | [diff] [blame] | 1240 | INTRL2_CPU_MASK_CLEAR); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1241 | } |
| 1242 | |
Petri Gynther | 9dbac28 | 2015-03-25 12:35:10 -0700 | [diff] [blame] | 1243 | static inline void bcmgenet_tx_ring_int_disable(struct bcmgenet_tx_ring *ring) |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1244 | { |
Petri Gynther | 9dbac28 | 2015-03-25 12:35:10 -0700 | [diff] [blame] | 1245 | bcmgenet_intrl2_1_writel(ring->priv, 1 << ring->index, |
Florian Fainelli | c91b7f6 | 2014-07-23 10:42:12 -0700 | [diff] [blame] | 1246 | INTRL2_CPU_MASK_SET); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1247 | } |
| 1248 | |
Doug Berger | f48bed1 | 2017-07-14 16:12:10 -0700 | [diff] [blame] | 1249 | /* Simple helper to free a transmit control block's resources |
| 1250 | * Returns an skb when the last transmit control block associated with the |
| 1251 | * skb is freed. The skb should be freed by the caller if necessary. |
| 1252 | */ |
| 1253 | static struct sk_buff *bcmgenet_free_tx_cb(struct device *dev, |
| 1254 | struct enet_cb *cb) |
| 1255 | { |
| 1256 | struct sk_buff *skb; |
| 1257 | |
| 1258 | skb = cb->skb; |
| 1259 | |
| 1260 | if (skb) { |
| 1261 | cb->skb = NULL; |
| 1262 | if (cb == GENET_CB(skb)->first_cb) |
| 1263 | dma_unmap_single(dev, dma_unmap_addr(cb, dma_addr), |
| 1264 | dma_unmap_len(cb, dma_len), |
| 1265 | DMA_TO_DEVICE); |
| 1266 | else |
| 1267 | dma_unmap_page(dev, dma_unmap_addr(cb, dma_addr), |
| 1268 | dma_unmap_len(cb, dma_len), |
| 1269 | DMA_TO_DEVICE); |
| 1270 | dma_unmap_addr_set(cb, dma_addr, 0); |
| 1271 | |
| 1272 | if (cb == GENET_CB(skb)->last_cb) |
| 1273 | return skb; |
| 1274 | |
| 1275 | } else if (dma_unmap_addr(cb, dma_addr)) { |
| 1276 | dma_unmap_page(dev, |
| 1277 | dma_unmap_addr(cb, dma_addr), |
| 1278 | dma_unmap_len(cb, dma_len), |
| 1279 | DMA_TO_DEVICE); |
| 1280 | dma_unmap_addr_set(cb, dma_addr, 0); |
| 1281 | } |
| 1282 | |
Wei Yongjun | 335ab8b | 2018-03-28 12:51:19 +0000 | [diff] [blame] | 1283 | return NULL; |
Doug Berger | f48bed1 | 2017-07-14 16:12:10 -0700 | [diff] [blame] | 1284 | } |
| 1285 | |
| 1286 | /* Simple helper to free a receive control block's resources */ |
| 1287 | static struct sk_buff *bcmgenet_free_rx_cb(struct device *dev, |
| 1288 | struct enet_cb *cb) |
| 1289 | { |
| 1290 | struct sk_buff *skb; |
| 1291 | |
| 1292 | skb = cb->skb; |
| 1293 | cb->skb = NULL; |
| 1294 | |
| 1295 | if (dma_unmap_addr(cb, dma_addr)) { |
| 1296 | dma_unmap_single(dev, dma_unmap_addr(cb, dma_addr), |
| 1297 | dma_unmap_len(cb, dma_len), DMA_FROM_DEVICE); |
| 1298 | dma_unmap_addr_set(cb, dma_addr, 0); |
| 1299 | } |
| 1300 | |
| 1301 | return skb; |
| 1302 | } |
| 1303 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1304 | /* Unlocked version of the reclaim routine */ |
Jaedon Shin | 4092e6a | 2015-02-28 11:48:26 +0900 | [diff] [blame] | 1305 | static unsigned int __bcmgenet_tx_reclaim(struct net_device *dev, |
| 1306 | struct bcmgenet_tx_ring *ring) |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1307 | { |
| 1308 | struct bcmgenet_priv *priv = netdev_priv(dev); |
Petri Gynther | 66d0675 | 2015-03-04 14:30:01 -0800 | [diff] [blame] | 1309 | unsigned int txbds_processed = 0; |
Doug Berger | f48bed1 | 2017-07-14 16:12:10 -0700 | [diff] [blame] | 1310 | unsigned int bytes_compl = 0; |
| 1311 | unsigned int pkts_compl = 0; |
| 1312 | unsigned int txbds_ready; |
| 1313 | unsigned int c_index; |
| 1314 | struct sk_buff *skb; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1315 | |
Doug Berger | d5810ca | 2017-03-13 17:41:37 -0700 | [diff] [blame] | 1316 | /* Clear status before servicing to reduce spurious interrupts */ |
| 1317 | if (ring->index == DESC_INDEX) |
| 1318 | bcmgenet_intrl2_0_writel(priv, UMAC_IRQ_TXDMA_DONE, |
| 1319 | INTRL2_CPU_CLEAR); |
| 1320 | else |
| 1321 | bcmgenet_intrl2_1_writel(priv, (1 << ring->index), |
| 1322 | INTRL2_CPU_CLEAR); |
| 1323 | |
Brian Norris | 7fc527f | 2014-07-29 14:34:14 -0700 | [diff] [blame] | 1324 | /* Compute how many buffers are transmitted since last xmit call */ |
Doug Berger | c298ede | 2017-03-13 17:41:33 -0700 | [diff] [blame] | 1325 | c_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_CONS_INDEX) |
| 1326 | & DMA_C_INDEX_MASK; |
| 1327 | txbds_ready = (c_index - ring->c_index) & DMA_C_INDEX_MASK; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1328 | |
| 1329 | netif_dbg(priv, tx_done, dev, |
Petri Gynther | 66d0675 | 2015-03-04 14:30:01 -0800 | [diff] [blame] | 1330 | "%s ring=%d old_c_index=%u c_index=%u txbds_ready=%u\n", |
| 1331 | __func__, ring->index, ring->c_index, c_index, txbds_ready); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1332 | |
| 1333 | /* Reclaim transmitted buffers */ |
Petri Gynther | 66d0675 | 2015-03-04 14:30:01 -0800 | [diff] [blame] | 1334 | while (txbds_processed < txbds_ready) { |
Doug Berger | f48bed1 | 2017-07-14 16:12:10 -0700 | [diff] [blame] | 1335 | skb = bcmgenet_free_tx_cb(&priv->pdev->dev, |
| 1336 | &priv->tx_cbs[ring->clean_ptr]); |
| 1337 | if (skb) { |
Jaedon Shin | 4092e6a | 2015-02-28 11:48:26 +0900 | [diff] [blame] | 1338 | pkts_compl++; |
Doug Berger | f48bed1 | 2017-07-14 16:12:10 -0700 | [diff] [blame] | 1339 | bytes_compl += GENET_CB(skb)->bytes_sent; |
Florian Fainelli | d4fec85 | 2017-08-24 15:56:29 -0700 | [diff] [blame] | 1340 | dev_consume_skb_any(skb); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1341 | } |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1342 | |
Petri Gynther | 66d0675 | 2015-03-04 14:30:01 -0800 | [diff] [blame] | 1343 | txbds_processed++; |
| 1344 | if (likely(ring->clean_ptr < ring->end_ptr)) |
| 1345 | ring->clean_ptr++; |
| 1346 | else |
| 1347 | ring->clean_ptr = ring->cb_ptr; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1348 | } |
| 1349 | |
Petri Gynther | 66d0675 | 2015-03-04 14:30:01 -0800 | [diff] [blame] | 1350 | ring->free_bds += txbds_processed; |
Doug Berger | c4d453d | 2017-03-13 17:41:38 -0700 | [diff] [blame] | 1351 | ring->c_index = c_index; |
Petri Gynther | 66d0675 | 2015-03-04 14:30:01 -0800 | [diff] [blame] | 1352 | |
Florian Fainelli | 37a30b4 | 2017-03-16 10:27:08 -0700 | [diff] [blame] | 1353 | ring->packets += pkts_compl; |
| 1354 | ring->bytes += bytes_compl; |
Petri Gynther | 5586812 | 2016-03-24 11:27:20 -0700 | [diff] [blame] | 1355 | |
Doug Berger | 6d22fe1 | 2017-03-09 16:58:50 -0800 | [diff] [blame] | 1356 | netdev_tx_completed_queue(netdev_get_tx_queue(dev, ring->queue), |
| 1357 | pkts_compl, bytes_compl); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1358 | |
Doug Berger | c4d453d | 2017-03-13 17:41:38 -0700 | [diff] [blame] | 1359 | return txbds_processed; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1360 | } |
| 1361 | |
Jaedon Shin | 4092e6a | 2015-02-28 11:48:26 +0900 | [diff] [blame] | 1362 | static unsigned int bcmgenet_tx_reclaim(struct net_device *dev, |
Florian Fainelli | c91b7f6 | 2014-07-23 10:42:12 -0700 | [diff] [blame] | 1363 | struct bcmgenet_tx_ring *ring) |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1364 | { |
Jaedon Shin | 4092e6a | 2015-02-28 11:48:26 +0900 | [diff] [blame] | 1365 | unsigned int released; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1366 | |
Doug Berger | b0447ec | 2017-10-25 15:04:17 -0700 | [diff] [blame] | 1367 | spin_lock_bh(&ring->lock); |
Jaedon Shin | 4092e6a | 2015-02-28 11:48:26 +0900 | [diff] [blame] | 1368 | released = __bcmgenet_tx_reclaim(dev, ring); |
Doug Berger | b0447ec | 2017-10-25 15:04:17 -0700 | [diff] [blame] | 1369 | spin_unlock_bh(&ring->lock); |
Jaedon Shin | 4092e6a | 2015-02-28 11:48:26 +0900 | [diff] [blame] | 1370 | |
| 1371 | return released; |
| 1372 | } |
| 1373 | |
| 1374 | static int bcmgenet_tx_poll(struct napi_struct *napi, int budget) |
| 1375 | { |
| 1376 | struct bcmgenet_tx_ring *ring = |
| 1377 | container_of(napi, struct bcmgenet_tx_ring, napi); |
| 1378 | unsigned int work_done = 0; |
Doug Berger | 6d22fe1 | 2017-03-09 16:58:50 -0800 | [diff] [blame] | 1379 | struct netdev_queue *txq; |
Jaedon Shin | 4092e6a | 2015-02-28 11:48:26 +0900 | [diff] [blame] | 1380 | |
Doug Berger | b0447ec | 2017-10-25 15:04:17 -0700 | [diff] [blame] | 1381 | spin_lock(&ring->lock); |
Doug Berger | 6d22fe1 | 2017-03-09 16:58:50 -0800 | [diff] [blame] | 1382 | work_done = __bcmgenet_tx_reclaim(ring->priv->dev, ring); |
| 1383 | if (ring->free_bds > (MAX_SKB_FRAGS + 1)) { |
| 1384 | txq = netdev_get_tx_queue(ring->priv->dev, ring->queue); |
| 1385 | netif_tx_wake_queue(txq); |
| 1386 | } |
Doug Berger | b0447ec | 2017-10-25 15:04:17 -0700 | [diff] [blame] | 1387 | spin_unlock(&ring->lock); |
Jaedon Shin | 4092e6a | 2015-02-28 11:48:26 +0900 | [diff] [blame] | 1388 | |
| 1389 | if (work_done == 0) { |
| 1390 | napi_complete(napi); |
Petri Gynther | 9dbac28 | 2015-03-25 12:35:10 -0700 | [diff] [blame] | 1391 | ring->int_enable(ring); |
Jaedon Shin | 4092e6a | 2015-02-28 11:48:26 +0900 | [diff] [blame] | 1392 | |
| 1393 | return 0; |
| 1394 | } |
| 1395 | |
| 1396 | return budget; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | static void bcmgenet_tx_reclaim_all(struct net_device *dev) |
| 1400 | { |
| 1401 | struct bcmgenet_priv *priv = netdev_priv(dev); |
| 1402 | int i; |
| 1403 | |
| 1404 | if (netif_is_multiqueue(dev)) { |
| 1405 | for (i = 0; i < priv->hw_params->tx_queues; i++) |
| 1406 | bcmgenet_tx_reclaim(dev, &priv->tx_rings[i]); |
| 1407 | } |
| 1408 | |
| 1409 | bcmgenet_tx_reclaim(dev, &priv->tx_rings[DESC_INDEX]); |
| 1410 | } |
| 1411 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1412 | /* Reallocate the SKB to put enough headroom in front of it and insert |
| 1413 | * the transmit checksum offsets in the descriptors |
| 1414 | */ |
Doug Berger | 9a9ba2a | 2020-03-17 17:05:36 -0700 | [diff] [blame] | 1415 | static struct sk_buff *bcmgenet_add_tsb(struct net_device *dev, |
| 1416 | struct sk_buff *skb) |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1417 | { |
Doug Berger | f1af17c | 2019-12-17 16:51:15 -0800 | [diff] [blame] | 1418 | struct bcmgenet_priv *priv = netdev_priv(dev); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1419 | struct status_64 *status = NULL; |
| 1420 | struct sk_buff *new_skb; |
| 1421 | u16 offset; |
| 1422 | u8 ip_proto; |
Florian Fainelli | 6f89421 | 2018-04-02 15:58:55 -0700 | [diff] [blame] | 1423 | __be16 ip_ver; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1424 | u32 tx_csum_info; |
| 1425 | |
| 1426 | if (unlikely(skb_headroom(skb) < sizeof(*status))) { |
| 1427 | /* If 64 byte status block enabled, must make sure skb has |
| 1428 | * enough headroom for us to insert 64B status block. |
| 1429 | */ |
| 1430 | new_skb = skb_realloc_headroom(skb, sizeof(*status)); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1431 | if (!new_skb) { |
Doug Berger | e3fa858 | 2019-12-17 16:51:14 -0800 | [diff] [blame] | 1432 | dev_kfree_skb_any(skb); |
Doug Berger | f1af17c | 2019-12-17 16:51:15 -0800 | [diff] [blame] | 1433 | priv->mib.tx_realloc_tsb_failed++; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1434 | dev->stats.tx_dropped++; |
Petri Gynther | bc23333 | 2014-10-01 11:30:01 -0700 | [diff] [blame] | 1435 | return NULL; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1436 | } |
Doug Berger | e3fa858 | 2019-12-17 16:51:14 -0800 | [diff] [blame] | 1437 | dev_consume_skb_any(skb); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1438 | skb = new_skb; |
Doug Berger | f1af17c | 2019-12-17 16:51:15 -0800 | [diff] [blame] | 1439 | priv->mib.tx_realloc_tsb++; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | skb_push(skb, sizeof(*status)); |
| 1443 | status = (struct status_64 *)skb->data; |
| 1444 | |
| 1445 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
Florian Fainelli | 6f89421 | 2018-04-02 15:58:55 -0700 | [diff] [blame] | 1446 | ip_ver = skb->protocol; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1447 | switch (ip_ver) { |
Florian Fainelli | 6f89421 | 2018-04-02 15:58:55 -0700 | [diff] [blame] | 1448 | case htons(ETH_P_IP): |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1449 | ip_proto = ip_hdr(skb)->protocol; |
| 1450 | break; |
Florian Fainelli | 6f89421 | 2018-04-02 15:58:55 -0700 | [diff] [blame] | 1451 | case htons(ETH_P_IPV6): |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1452 | ip_proto = ipv6_hdr(skb)->nexthdr; |
| 1453 | break; |
| 1454 | default: |
Doug Berger | dd8e911 | 2019-12-17 16:51:09 -0800 | [diff] [blame] | 1455 | /* don't use UDP flag */ |
| 1456 | ip_proto = 0; |
| 1457 | break; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1458 | } |
| 1459 | |
| 1460 | offset = skb_checksum_start_offset(skb) - sizeof(*status); |
| 1461 | tx_csum_info = (offset << STATUS_TX_CSUM_START_SHIFT) | |
Doug Berger | dd8e911 | 2019-12-17 16:51:09 -0800 | [diff] [blame] | 1462 | (offset + skb->csum_offset) | |
| 1463 | STATUS_TX_CSUM_LV; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1464 | |
Doug Berger | dd8e911 | 2019-12-17 16:51:09 -0800 | [diff] [blame] | 1465 | /* Set the special UDP flag for UDP */ |
| 1466 | if (ip_proto == IPPROTO_UDP) |
| 1467 | tx_csum_info |= STATUS_TX_CSUM_PROTO_UDP; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1468 | |
| 1469 | status->tx_csum_info = tx_csum_info; |
| 1470 | } |
| 1471 | |
Petri Gynther | bc23333 | 2014-10-01 11:30:01 -0700 | [diff] [blame] | 1472 | return skb; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1473 | } |
| 1474 | |
| 1475 | static netdev_tx_t bcmgenet_xmit(struct sk_buff *skb, struct net_device *dev) |
| 1476 | { |
| 1477 | struct bcmgenet_priv *priv = netdev_priv(dev); |
Doug Berger | 876dbad | 2017-07-14 16:12:09 -0700 | [diff] [blame] | 1478 | struct device *kdev = &priv->pdev->dev; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1479 | struct bcmgenet_tx_ring *ring = NULL; |
Doug Berger | 876dbad | 2017-07-14 16:12:09 -0700 | [diff] [blame] | 1480 | struct enet_cb *tx_cb_ptr; |
Florian Fainelli | b2cde2c | 2014-03-20 10:53:23 -0700 | [diff] [blame] | 1481 | struct netdev_queue *txq; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1482 | int nr_frags, index; |
Doug Berger | 876dbad | 2017-07-14 16:12:09 -0700 | [diff] [blame] | 1483 | dma_addr_t mapping; |
| 1484 | unsigned int size; |
| 1485 | skb_frag_t *frag; |
| 1486 | u32 len_stat; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1487 | int ret; |
| 1488 | int i; |
| 1489 | |
| 1490 | index = skb_get_queue_mapping(skb); |
| 1491 | /* Mapping strategy: |
| 1492 | * queue_mapping = 0, unclassified, packet xmited through ring16 |
| 1493 | * queue_mapping = 1, goes to ring 0. (highest priority queue |
| 1494 | * queue_mapping = 2, goes to ring 1. |
| 1495 | * queue_mapping = 3, goes to ring 2. |
| 1496 | * queue_mapping = 4, goes to ring 3. |
| 1497 | */ |
| 1498 | if (index == 0) |
| 1499 | index = DESC_INDEX; |
| 1500 | else |
| 1501 | index -= 1; |
| 1502 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1503 | ring = &priv->tx_rings[index]; |
Florian Fainelli | b2cde2c | 2014-03-20 10:53:23 -0700 | [diff] [blame] | 1504 | txq = netdev_get_tx_queue(dev, ring->queue); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1505 | |
Petri Gynther | f5a9ec2 | 2016-04-05 13:59:59 -0700 | [diff] [blame] | 1506 | nr_frags = skb_shinfo(skb)->nr_frags; |
| 1507 | |
Doug Berger | b0447ec | 2017-10-25 15:04:17 -0700 | [diff] [blame] | 1508 | spin_lock(&ring->lock); |
Petri Gynther | f5a9ec2 | 2016-04-05 13:59:59 -0700 | [diff] [blame] | 1509 | if (ring->free_bds <= (nr_frags + 1)) { |
| 1510 | if (!netif_tx_queue_stopped(txq)) { |
| 1511 | netif_tx_stop_queue(txq); |
| 1512 | netdev_err(dev, |
| 1513 | "%s: tx ring %d full when queue %d awake\n", |
| 1514 | __func__, index, ring->queue); |
| 1515 | } |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1516 | ret = NETDEV_TX_BUSY; |
| 1517 | goto out; |
| 1518 | } |
| 1519 | |
Florian Fainelli | 474ea9c | 2014-07-22 11:01:52 -0700 | [diff] [blame] | 1520 | if (skb_padto(skb, ETH_ZLEN)) { |
| 1521 | ret = NETDEV_TX_OK; |
| 1522 | goto out; |
| 1523 | } |
| 1524 | |
Petri Gynther | 5586812 | 2016-03-24 11:27:20 -0700 | [diff] [blame] | 1525 | /* Retain how many bytes will be sent on the wire, without TSB inserted |
| 1526 | * by transmit checksum offload |
| 1527 | */ |
| 1528 | GENET_CB(skb)->bytes_sent = skb->len; |
| 1529 | |
Doug Berger | 9a9ba2a | 2020-03-17 17:05:36 -0700 | [diff] [blame] | 1530 | /* add the Transmit Status Block */ |
| 1531 | skb = bcmgenet_add_tsb(dev, skb); |
| 1532 | if (!skb) { |
| 1533 | ret = NETDEV_TX_OK; |
| 1534 | goto out; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1535 | } |
| 1536 | |
Doug Berger | 876dbad | 2017-07-14 16:12:09 -0700 | [diff] [blame] | 1537 | for (i = 0; i <= nr_frags; i++) { |
| 1538 | tx_cb_ptr = bcmgenet_get_txcb(priv, ring); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1539 | |
Gustavo A. R. Silva | 4fa112f | 2017-10-26 07:16:01 -0500 | [diff] [blame] | 1540 | BUG_ON(!tx_cb_ptr); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1541 | |
Doug Berger | 876dbad | 2017-07-14 16:12:09 -0700 | [diff] [blame] | 1542 | if (!i) { |
| 1543 | /* Transmit single SKB or head of fragment list */ |
Doug Berger | f48bed1 | 2017-07-14 16:12:10 -0700 | [diff] [blame] | 1544 | GENET_CB(skb)->first_cb = tx_cb_ptr; |
Doug Berger | 876dbad | 2017-07-14 16:12:09 -0700 | [diff] [blame] | 1545 | size = skb_headlen(skb); |
| 1546 | mapping = dma_map_single(kdev, skb->data, size, |
| 1547 | DMA_TO_DEVICE); |
| 1548 | } else { |
| 1549 | /* xmit fragment */ |
Doug Berger | 876dbad | 2017-07-14 16:12:09 -0700 | [diff] [blame] | 1550 | frag = &skb_shinfo(skb)->frags[i - 1]; |
| 1551 | size = skb_frag_size(frag); |
| 1552 | mapping = skb_frag_dma_map(kdev, frag, 0, size, |
| 1553 | DMA_TO_DEVICE); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1554 | } |
Doug Berger | 876dbad | 2017-07-14 16:12:09 -0700 | [diff] [blame] | 1555 | |
| 1556 | ret = dma_mapping_error(kdev, mapping); |
| 1557 | if (ret) { |
| 1558 | priv->mib.tx_dma_failed++; |
| 1559 | netif_err(priv, tx_err, dev, "Tx DMA map failed\n"); |
| 1560 | ret = NETDEV_TX_OK; |
| 1561 | goto out_unmap_frags; |
| 1562 | } |
| 1563 | dma_unmap_addr_set(tx_cb_ptr, dma_addr, mapping); |
| 1564 | dma_unmap_len_set(tx_cb_ptr, dma_len, size); |
| 1565 | |
Doug Berger | f48bed1 | 2017-07-14 16:12:10 -0700 | [diff] [blame] | 1566 | tx_cb_ptr->skb = skb; |
| 1567 | |
Doug Berger | 876dbad | 2017-07-14 16:12:09 -0700 | [diff] [blame] | 1568 | len_stat = (size << DMA_BUFLENGTH_SHIFT) | |
| 1569 | (priv->hw_params->qtag_mask << DMA_TX_QTAG_SHIFT); |
| 1570 | |
| 1571 | if (!i) { |
| 1572 | len_stat |= DMA_TX_APPEND_CRC | DMA_SOP; |
| 1573 | if (skb->ip_summed == CHECKSUM_PARTIAL) |
| 1574 | len_stat |= DMA_TX_DO_CSUM; |
| 1575 | } |
| 1576 | if (i == nr_frags) |
| 1577 | len_stat |= DMA_EOP; |
| 1578 | |
| 1579 | dmadesc_set(priv, tx_cb_ptr->bd_addr, mapping, len_stat); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1580 | } |
| 1581 | |
Doug Berger | f48bed1 | 2017-07-14 16:12:10 -0700 | [diff] [blame] | 1582 | GENET_CB(skb)->last_cb = tx_cb_ptr; |
Florian Fainelli | d03825f | 2014-03-20 10:53:21 -0700 | [diff] [blame] | 1583 | skb_tx_timestamp(skb); |
| 1584 | |
Florian Fainelli | ae67bf0 | 2015-03-13 12:11:06 -0700 | [diff] [blame] | 1585 | /* Decrement total BD count and advance our write pointer */ |
| 1586 | ring->free_bds -= nr_frags + 1; |
| 1587 | ring->prod_index += nr_frags + 1; |
| 1588 | ring->prod_index &= DMA_P_INDEX_MASK; |
| 1589 | |
Petri Gynther | e178c8c | 2016-04-09 00:20:36 -0700 | [diff] [blame] | 1590 | netdev_tx_sent_queue(txq, GENET_CB(skb)->bytes_sent); |
| 1591 | |
Jaedon Shin | 4092e6a | 2015-02-28 11:48:26 +0900 | [diff] [blame] | 1592 | if (ring->free_bds <= (MAX_SKB_FRAGS + 1)) |
Florian Fainelli | b2cde2c | 2014-03-20 10:53:23 -0700 | [diff] [blame] | 1593 | netif_tx_stop_queue(txq); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1594 | |
Florian Westphal | 6b16f9e | 2019-04-01 16:42:14 +0200 | [diff] [blame] | 1595 | if (!netdev_xmit_more() || netif_xmit_stopped(txq)) |
Florian Fainelli | ddd0ca5 | 2015-03-13 12:11:07 -0700 | [diff] [blame] | 1596 | /* Packets are ready, update producer index */ |
| 1597 | bcmgenet_tdma_ring_writel(priv, ring->index, |
| 1598 | ring->prod_index, TDMA_PROD_INDEX); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1599 | out: |
Doug Berger | b0447ec | 2017-10-25 15:04:17 -0700 | [diff] [blame] | 1600 | spin_unlock(&ring->lock); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1601 | |
| 1602 | return ret; |
Doug Berger | 876dbad | 2017-07-14 16:12:09 -0700 | [diff] [blame] | 1603 | |
| 1604 | out_unmap_frags: |
| 1605 | /* Back up for failed control block mapping */ |
| 1606 | bcmgenet_put_txcb(priv, ring); |
| 1607 | |
| 1608 | /* Unmap successfully mapped control blocks */ |
| 1609 | while (i-- > 0) { |
| 1610 | tx_cb_ptr = bcmgenet_put_txcb(priv, ring); |
Doug Berger | f48bed1 | 2017-07-14 16:12:10 -0700 | [diff] [blame] | 1611 | bcmgenet_free_tx_cb(kdev, tx_cb_ptr); |
Doug Berger | 876dbad | 2017-07-14 16:12:09 -0700 | [diff] [blame] | 1612 | } |
| 1613 | |
| 1614 | dev_kfree_skb(skb); |
| 1615 | goto out; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1616 | } |
| 1617 | |
Petri Gynther | d6707be | 2015-03-12 15:48:00 -0700 | [diff] [blame] | 1618 | static struct sk_buff *bcmgenet_rx_refill(struct bcmgenet_priv *priv, |
| 1619 | struct enet_cb *cb) |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1620 | { |
| 1621 | struct device *kdev = &priv->pdev->dev; |
| 1622 | struct sk_buff *skb; |
Petri Gynther | d6707be | 2015-03-12 15:48:00 -0700 | [diff] [blame] | 1623 | struct sk_buff *rx_skb; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1624 | dma_addr_t mapping; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1625 | |
Petri Gynther | d6707be | 2015-03-12 15:48:00 -0700 | [diff] [blame] | 1626 | /* Allocate a new Rx skb */ |
Florian Fainelli | c91b7f6 | 2014-07-23 10:42:12 -0700 | [diff] [blame] | 1627 | skb = netdev_alloc_skb(priv->dev, priv->rx_buf_len + SKB_ALIGNMENT); |
Petri Gynther | d6707be | 2015-03-12 15:48:00 -0700 | [diff] [blame] | 1628 | if (!skb) { |
| 1629 | priv->mib.alloc_rx_buff_failed++; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1630 | netif_err(priv, rx_err, priv->dev, |
Petri Gynther | d6707be | 2015-03-12 15:48:00 -0700 | [diff] [blame] | 1631 | "%s: Rx skb allocation failed\n", __func__); |
| 1632 | return NULL; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1633 | } |
| 1634 | |
Petri Gynther | d6707be | 2015-03-12 15:48:00 -0700 | [diff] [blame] | 1635 | /* DMA-map the new Rx skb */ |
| 1636 | mapping = dma_map_single(kdev, skb->data, priv->rx_buf_len, |
| 1637 | DMA_FROM_DEVICE); |
| 1638 | if (dma_mapping_error(kdev, mapping)) { |
| 1639 | priv->mib.rx_dma_failed++; |
| 1640 | dev_kfree_skb_any(skb); |
| 1641 | netif_err(priv, rx_err, priv->dev, |
| 1642 | "%s: Rx skb DMA mapping failed\n", __func__); |
| 1643 | return NULL; |
| 1644 | } |
| 1645 | |
| 1646 | /* Grab the current Rx skb from the ring and DMA-unmap it */ |
Doug Berger | f48bed1 | 2017-07-14 16:12:10 -0700 | [diff] [blame] | 1647 | rx_skb = bcmgenet_free_rx_cb(kdev, cb); |
Petri Gynther | d6707be | 2015-03-12 15:48:00 -0700 | [diff] [blame] | 1648 | |
| 1649 | /* Put the new Rx skb on the ring */ |
| 1650 | cb->skb = skb; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1651 | dma_unmap_addr_set(cb, dma_addr, mapping); |
Doug Berger | f48bed1 | 2017-07-14 16:12:10 -0700 | [diff] [blame] | 1652 | dma_unmap_len_set(cb, dma_len, priv->rx_buf_len); |
Petri Gynther | 8ac467e | 2015-03-09 13:40:00 -0700 | [diff] [blame] | 1653 | dmadesc_set_addr(priv, cb->bd_addr, mapping); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1654 | |
Petri Gynther | d6707be | 2015-03-12 15:48:00 -0700 | [diff] [blame] | 1655 | /* Return the current Rx skb to caller */ |
| 1656 | return rx_skb; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1657 | } |
| 1658 | |
| 1659 | /* bcmgenet_desc_rx - descriptor based rx process. |
| 1660 | * this could be called from bottom half, or from NAPI polling method. |
| 1661 | */ |
Petri Gynther | 4055eae | 2015-03-25 12:35:16 -0700 | [diff] [blame] | 1662 | static unsigned int bcmgenet_desc_rx(struct bcmgenet_rx_ring *ring, |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1663 | unsigned int budget) |
| 1664 | { |
Petri Gynther | 4055eae | 2015-03-25 12:35:16 -0700 | [diff] [blame] | 1665 | struct bcmgenet_priv *priv = ring->priv; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1666 | struct net_device *dev = priv->dev; |
| 1667 | struct enet_cb *cb; |
| 1668 | struct sk_buff *skb; |
| 1669 | u32 dma_length_status; |
| 1670 | unsigned long dma_flag; |
Petri Gynther | d6707be | 2015-03-12 15:48:00 -0700 | [diff] [blame] | 1671 | int len; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1672 | unsigned int rxpktprocessed = 0, rxpkttoprocess; |
Florian Fainelli | 9f4ca05 | 2018-03-22 18:19:33 -0700 | [diff] [blame] | 1673 | unsigned int bytes_processed = 0; |
Doug Berger | d5810ca | 2017-03-13 17:41:37 -0700 | [diff] [blame] | 1674 | unsigned int p_index, mask; |
Petri Gynther | d26ea6c | 2015-03-10 15:55:00 -0700 | [diff] [blame] | 1675 | unsigned int discards; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1676 | |
Doug Berger | d5810ca | 2017-03-13 17:41:37 -0700 | [diff] [blame] | 1677 | /* Clear status before servicing to reduce spurious interrupts */ |
| 1678 | if (ring->index == DESC_INDEX) { |
| 1679 | bcmgenet_intrl2_0_writel(priv, UMAC_IRQ_RXDMA_DONE, |
| 1680 | INTRL2_CPU_CLEAR); |
| 1681 | } else { |
| 1682 | mask = 1 << (UMAC_IRQ1_RX_INTR_SHIFT + ring->index); |
| 1683 | bcmgenet_intrl2_1_writel(priv, |
| 1684 | mask, |
| 1685 | INTRL2_CPU_CLEAR); |
| 1686 | } |
| 1687 | |
Petri Gynther | 4055eae | 2015-03-25 12:35:16 -0700 | [diff] [blame] | 1688 | p_index = bcmgenet_rdma_ring_readl(priv, ring->index, RDMA_PROD_INDEX); |
Petri Gynther | d26ea6c | 2015-03-10 15:55:00 -0700 | [diff] [blame] | 1689 | |
| 1690 | discards = (p_index >> DMA_P_INDEX_DISCARD_CNT_SHIFT) & |
| 1691 | DMA_P_INDEX_DISCARD_CNT_MASK; |
| 1692 | if (discards > ring->old_discards) { |
| 1693 | discards = discards - ring->old_discards; |
Florian Fainelli | 37a30b4 | 2017-03-16 10:27:08 -0700 | [diff] [blame] | 1694 | ring->errors += discards; |
Petri Gynther | d26ea6c | 2015-03-10 15:55:00 -0700 | [diff] [blame] | 1695 | ring->old_discards += discards; |
| 1696 | |
| 1697 | /* Clear HW register when we reach 75% of maximum 0xFFFF */ |
| 1698 | if (ring->old_discards >= 0xC000) { |
| 1699 | ring->old_discards = 0; |
Petri Gynther | 4055eae | 2015-03-25 12:35:16 -0700 | [diff] [blame] | 1700 | bcmgenet_rdma_ring_writel(priv, ring->index, 0, |
Petri Gynther | d26ea6c | 2015-03-10 15:55:00 -0700 | [diff] [blame] | 1701 | RDMA_PROD_INDEX); |
| 1702 | } |
| 1703 | } |
| 1704 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1705 | p_index &= DMA_P_INDEX_MASK; |
Doug Berger | c298ede | 2017-03-13 17:41:33 -0700 | [diff] [blame] | 1706 | rxpkttoprocess = (p_index - ring->c_index) & DMA_C_INDEX_MASK; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1707 | |
| 1708 | netif_dbg(priv, rx_status, dev, |
Florian Fainelli | c91b7f6 | 2014-07-23 10:42:12 -0700 | [diff] [blame] | 1709 | "RDMA: rxpkttoprocess=%d\n", rxpkttoprocess); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1710 | |
| 1711 | while ((rxpktprocessed < rxpkttoprocess) && |
Florian Fainelli | c91b7f6 | 2014-07-23 10:42:12 -0700 | [diff] [blame] | 1712 | (rxpktprocessed < budget)) { |
Doug Berger | 9a9ba2a | 2020-03-17 17:05:36 -0700 | [diff] [blame] | 1713 | struct status_64 *status; |
| 1714 | __be16 rx_csum; |
| 1715 | |
Petri Gynther | 8ac467e | 2015-03-09 13:40:00 -0700 | [diff] [blame] | 1716 | cb = &priv->rx_cbs[ring->read_ptr]; |
Petri Gynther | d6707be | 2015-03-12 15:48:00 -0700 | [diff] [blame] | 1717 | skb = bcmgenet_rx_refill(priv, cb); |
Florian Fainelli | b629be5 | 2014-09-08 11:37:52 -0700 | [diff] [blame] | 1718 | |
Florian Fainelli | b629be5 | 2014-09-08 11:37:52 -0700 | [diff] [blame] | 1719 | if (unlikely(!skb)) { |
Florian Fainelli | 37a30b4 | 2017-03-16 10:27:08 -0700 | [diff] [blame] | 1720 | ring->dropped++; |
Petri Gynther | d6707be | 2015-03-12 15:48:00 -0700 | [diff] [blame] | 1721 | goto next; |
Florian Fainelli | b629be5 | 2014-09-08 11:37:52 -0700 | [diff] [blame] | 1722 | } |
| 1723 | |
Doug Berger | 9a9ba2a | 2020-03-17 17:05:36 -0700 | [diff] [blame] | 1724 | status = (struct status_64 *)skb->data; |
| 1725 | dma_length_status = status->length_status; |
| 1726 | if (dev->features & NETIF_F_RXCSUM) { |
Doug Berger | 8101553 | 2019-12-17 16:51:10 -0800 | [diff] [blame] | 1727 | rx_csum = (__force __be16)(status->rx_csum & 0xffff); |
Doug Berger | 9a9ba2a | 2020-03-17 17:05:36 -0700 | [diff] [blame] | 1728 | skb->csum = (__force __wsum)ntohs(rx_csum); |
| 1729 | skb->ip_summed = CHECKSUM_COMPLETE; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1730 | } |
| 1731 | |
| 1732 | /* DMA flags and length are still valid no matter how |
| 1733 | * we got the Receive Status Vector (64B RSB or register) |
| 1734 | */ |
| 1735 | dma_flag = dma_length_status & 0xffff; |
| 1736 | len = dma_length_status >> DMA_BUFLENGTH_SHIFT; |
| 1737 | |
| 1738 | netif_dbg(priv, rx_status, dev, |
Florian Fainelli | c91b7f6 | 2014-07-23 10:42:12 -0700 | [diff] [blame] | 1739 | "%s:p_ind=%d c_ind=%d read_ptr=%d len_stat=0x%08x\n", |
Petri Gynther | 8ac467e | 2015-03-09 13:40:00 -0700 | [diff] [blame] | 1740 | __func__, p_index, ring->c_index, |
| 1741 | ring->read_ptr, dma_length_status); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1742 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1743 | if (unlikely(!(dma_flag & DMA_EOP) || !(dma_flag & DMA_SOP))) { |
| 1744 | netif_err(priv, rx_status, dev, |
Florian Fainelli | c91b7f6 | 2014-07-23 10:42:12 -0700 | [diff] [blame] | 1745 | "dropping fragmented packet!\n"); |
Florian Fainelli | 37a30b4 | 2017-03-16 10:27:08 -0700 | [diff] [blame] | 1746 | ring->errors++; |
Petri Gynther | d6707be | 2015-03-12 15:48:00 -0700 | [diff] [blame] | 1747 | dev_kfree_skb_any(skb); |
| 1748 | goto next; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1749 | } |
Petri Gynther | d6707be | 2015-03-12 15:48:00 -0700 | [diff] [blame] | 1750 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1751 | /* report errors */ |
| 1752 | if (unlikely(dma_flag & (DMA_RX_CRC_ERROR | |
| 1753 | DMA_RX_OV | |
| 1754 | DMA_RX_NO | |
| 1755 | DMA_RX_LG | |
| 1756 | DMA_RX_RXER))) { |
| 1757 | netif_err(priv, rx_status, dev, "dma_flag=0x%x\n", |
Florian Fainelli | c91b7f6 | 2014-07-23 10:42:12 -0700 | [diff] [blame] | 1758 | (unsigned int)dma_flag); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1759 | if (dma_flag & DMA_RX_CRC_ERROR) |
| 1760 | dev->stats.rx_crc_errors++; |
| 1761 | if (dma_flag & DMA_RX_OV) |
| 1762 | dev->stats.rx_over_errors++; |
| 1763 | if (dma_flag & DMA_RX_NO) |
| 1764 | dev->stats.rx_frame_errors++; |
| 1765 | if (dma_flag & DMA_RX_LG) |
| 1766 | dev->stats.rx_length_errors++; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1767 | dev->stats.rx_errors++; |
Petri Gynther | d6707be | 2015-03-12 15:48:00 -0700 | [diff] [blame] | 1768 | dev_kfree_skb_any(skb); |
| 1769 | goto next; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1770 | } /* error packet */ |
| 1771 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1772 | skb_put(skb, len); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1773 | |
Doug Berger | 9a9ba2a | 2020-03-17 17:05:36 -0700 | [diff] [blame] | 1774 | /* remove RSB and hardware 2bytes added for IP alignment */ |
| 1775 | skb_pull(skb, 66); |
| 1776 | len -= 66; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1777 | |
| 1778 | if (priv->crc_fwd_en) { |
| 1779 | skb_trim(skb, len - ETH_FCS_LEN); |
| 1780 | len -= ETH_FCS_LEN; |
| 1781 | } |
| 1782 | |
Florian Fainelli | 9f4ca05 | 2018-03-22 18:19:33 -0700 | [diff] [blame] | 1783 | bytes_processed += len; |
| 1784 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1785 | /*Finish setting up the received SKB and send it to the kernel*/ |
| 1786 | skb->protocol = eth_type_trans(skb, priv->dev); |
Florian Fainelli | 37a30b4 | 2017-03-16 10:27:08 -0700 | [diff] [blame] | 1787 | ring->packets++; |
| 1788 | ring->bytes += len; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1789 | if (dma_flag & DMA_RX_MULT) |
| 1790 | dev->stats.multicast++; |
| 1791 | |
| 1792 | /* Notify kernel */ |
Petri Gynther | 4055eae | 2015-03-25 12:35:16 -0700 | [diff] [blame] | 1793 | napi_gro_receive(&ring->napi, skb); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1794 | netif_dbg(priv, rx_status, dev, "pushed up to kernel\n"); |
| 1795 | |
Petri Gynther | d6707be | 2015-03-12 15:48:00 -0700 | [diff] [blame] | 1796 | next: |
Florian Fainelli | cf377d8 | 2014-10-10 10:51:52 -0700 | [diff] [blame] | 1797 | rxpktprocessed++; |
Petri Gynther | 8ac467e | 2015-03-09 13:40:00 -0700 | [diff] [blame] | 1798 | if (likely(ring->read_ptr < ring->end_ptr)) |
| 1799 | ring->read_ptr++; |
| 1800 | else |
| 1801 | ring->read_ptr = ring->cb_ptr; |
| 1802 | |
| 1803 | ring->c_index = (ring->c_index + 1) & DMA_C_INDEX_MASK; |
Petri Gynther | 4055eae | 2015-03-25 12:35:16 -0700 | [diff] [blame] | 1804 | bcmgenet_rdma_ring_writel(priv, ring->index, ring->c_index, RDMA_CONS_INDEX); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1805 | } |
| 1806 | |
Florian Fainelli | 9f4ca05 | 2018-03-22 18:19:33 -0700 | [diff] [blame] | 1807 | ring->dim.bytes = bytes_processed; |
| 1808 | ring->dim.packets = rxpktprocessed; |
| 1809 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1810 | return rxpktprocessed; |
| 1811 | } |
| 1812 | |
Petri Gynther | 3ab1133 | 2015-03-25 12:35:15 -0700 | [diff] [blame] | 1813 | /* Rx NAPI polling method */ |
| 1814 | static int bcmgenet_rx_poll(struct napi_struct *napi, int budget) |
| 1815 | { |
Petri Gynther | 4055eae | 2015-03-25 12:35:16 -0700 | [diff] [blame] | 1816 | struct bcmgenet_rx_ring *ring = container_of(napi, |
| 1817 | struct bcmgenet_rx_ring, napi); |
Yamin Friedman | f06d0ca | 2019-07-23 10:22:47 +0300 | [diff] [blame] | 1818 | struct dim_sample dim_sample = {}; |
Petri Gynther | 3ab1133 | 2015-03-25 12:35:15 -0700 | [diff] [blame] | 1819 | unsigned int work_done; |
| 1820 | |
Petri Gynther | 4055eae | 2015-03-25 12:35:16 -0700 | [diff] [blame] | 1821 | work_done = bcmgenet_desc_rx(ring, budget); |
Petri Gynther | 3ab1133 | 2015-03-25 12:35:15 -0700 | [diff] [blame] | 1822 | |
| 1823 | if (work_done < budget) { |
Eric Dumazet | eb96ce0 | 2016-04-08 22:06:40 -0700 | [diff] [blame] | 1824 | napi_complete_done(napi, work_done); |
Petri Gynther | 4055eae | 2015-03-25 12:35:16 -0700 | [diff] [blame] | 1825 | ring->int_enable(ring); |
Petri Gynther | 3ab1133 | 2015-03-25 12:35:15 -0700 | [diff] [blame] | 1826 | } |
| 1827 | |
Florian Fainelli | 9f4ca05 | 2018-03-22 18:19:33 -0700 | [diff] [blame] | 1828 | if (ring->dim.use_dim) { |
Tal Gilboa | 8960b38 | 2019-01-31 16:44:48 +0200 | [diff] [blame] | 1829 | dim_update_sample(ring->dim.event_ctr, ring->dim.packets, |
| 1830 | ring->dim.bytes, &dim_sample); |
Florian Fainelli | 9f4ca05 | 2018-03-22 18:19:33 -0700 | [diff] [blame] | 1831 | net_dim(&ring->dim.dim, dim_sample); |
| 1832 | } |
| 1833 | |
Petri Gynther | 3ab1133 | 2015-03-25 12:35:15 -0700 | [diff] [blame] | 1834 | return work_done; |
| 1835 | } |
| 1836 | |
Florian Fainelli | 9f4ca05 | 2018-03-22 18:19:33 -0700 | [diff] [blame] | 1837 | static void bcmgenet_dim_work(struct work_struct *work) |
| 1838 | { |
Tal Gilboa | 8960b38 | 2019-01-31 16:44:48 +0200 | [diff] [blame] | 1839 | struct dim *dim = container_of(work, struct dim, work); |
Florian Fainelli | 9f4ca05 | 2018-03-22 18:19:33 -0700 | [diff] [blame] | 1840 | struct bcmgenet_net_dim *ndim = |
| 1841 | container_of(dim, struct bcmgenet_net_dim, dim); |
| 1842 | struct bcmgenet_rx_ring *ring = |
| 1843 | container_of(ndim, struct bcmgenet_rx_ring, dim); |
Tal Gilboa | 8960b38 | 2019-01-31 16:44:48 +0200 | [diff] [blame] | 1844 | struct dim_cq_moder cur_profile = |
Tal Gilboa | 026a807 | 2018-04-24 13:36:01 +0300 | [diff] [blame] | 1845 | net_dim_get_rx_moderation(dim->mode, dim->profile_ix); |
Florian Fainelli | 9f4ca05 | 2018-03-22 18:19:33 -0700 | [diff] [blame] | 1846 | |
Florian Fainelli | 5e6ce1f | 2018-03-28 15:15:38 -0700 | [diff] [blame] | 1847 | bcmgenet_set_rx_coalesce(ring, cur_profile.usec, cur_profile.pkts); |
Tal Gilboa | c002bd5 | 2018-11-05 12:07:52 +0200 | [diff] [blame] | 1848 | dim->state = DIM_START_MEASURE; |
Florian Fainelli | 9f4ca05 | 2018-03-22 18:19:33 -0700 | [diff] [blame] | 1849 | } |
| 1850 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1851 | /* Assign skb to RX DMA descriptor. */ |
Petri Gynther | 8ac467e | 2015-03-09 13:40:00 -0700 | [diff] [blame] | 1852 | static int bcmgenet_alloc_rx_buffers(struct bcmgenet_priv *priv, |
| 1853 | struct bcmgenet_rx_ring *ring) |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1854 | { |
| 1855 | struct enet_cb *cb; |
Petri Gynther | d6707be | 2015-03-12 15:48:00 -0700 | [diff] [blame] | 1856 | struct sk_buff *skb; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1857 | int i; |
| 1858 | |
Petri Gynther | 8ac467e | 2015-03-09 13:40:00 -0700 | [diff] [blame] | 1859 | netif_dbg(priv, hw, priv->dev, "%s\n", __func__); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1860 | |
| 1861 | /* loop here for each buffer needing assign */ |
Petri Gynther | 8ac467e | 2015-03-09 13:40:00 -0700 | [diff] [blame] | 1862 | for (i = 0; i < ring->size; i++) { |
| 1863 | cb = ring->cbs + i; |
Petri Gynther | d6707be | 2015-03-12 15:48:00 -0700 | [diff] [blame] | 1864 | skb = bcmgenet_rx_refill(priv, cb); |
| 1865 | if (skb) |
Florian Fainelli | d4fec85 | 2017-08-24 15:56:29 -0700 | [diff] [blame] | 1866 | dev_consume_skb_any(skb); |
Petri Gynther | d6707be | 2015-03-12 15:48:00 -0700 | [diff] [blame] | 1867 | if (!cb->skb) |
| 1868 | return -ENOMEM; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1869 | } |
| 1870 | |
Petri Gynther | d6707be | 2015-03-12 15:48:00 -0700 | [diff] [blame] | 1871 | return 0; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1872 | } |
| 1873 | |
| 1874 | static void bcmgenet_free_rx_buffers(struct bcmgenet_priv *priv) |
| 1875 | { |
Doug Berger | f48bed1 | 2017-07-14 16:12:10 -0700 | [diff] [blame] | 1876 | struct sk_buff *skb; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1877 | struct enet_cb *cb; |
| 1878 | int i; |
| 1879 | |
| 1880 | for (i = 0; i < priv->num_rx_bds; i++) { |
| 1881 | cb = &priv->rx_cbs[i]; |
| 1882 | |
Doug Berger | f48bed1 | 2017-07-14 16:12:10 -0700 | [diff] [blame] | 1883 | skb = bcmgenet_free_rx_cb(&priv->pdev->dev, cb); |
| 1884 | if (skb) |
Florian Fainelli | d4fec85 | 2017-08-24 15:56:29 -0700 | [diff] [blame] | 1885 | dev_consume_skb_any(skb); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1886 | } |
| 1887 | } |
| 1888 | |
Florian Fainelli | c91b7f6 | 2014-07-23 10:42:12 -0700 | [diff] [blame] | 1889 | static void umac_enable_set(struct bcmgenet_priv *priv, u32 mask, bool enable) |
Florian Fainelli | e29585b | 2014-07-21 15:29:20 -0700 | [diff] [blame] | 1890 | { |
| 1891 | u32 reg; |
| 1892 | |
| 1893 | reg = bcmgenet_umac_readl(priv, UMAC_CMD); |
Doug Berger | 88f6c8b | 2020-03-16 14:44:56 -0700 | [diff] [blame] | 1894 | if (reg & CMD_SW_RESET) |
| 1895 | return; |
Florian Fainelli | e29585b | 2014-07-21 15:29:20 -0700 | [diff] [blame] | 1896 | if (enable) |
| 1897 | reg |= mask; |
| 1898 | else |
| 1899 | reg &= ~mask; |
| 1900 | bcmgenet_umac_writel(priv, reg, UMAC_CMD); |
| 1901 | |
| 1902 | /* UniMAC stops on a packet boundary, wait for a full-size packet |
| 1903 | * to be processed |
| 1904 | */ |
| 1905 | if (enable == 0) |
| 1906 | usleep_range(1000, 2000); |
| 1907 | } |
| 1908 | |
Doug Berger | 28c2d1a | 2017-10-25 15:04:13 -0700 | [diff] [blame] | 1909 | static void reset_umac(struct bcmgenet_priv *priv) |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1910 | { |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1911 | /* 7358a0/7552a0: bad default in RBUF_FLUSH_CTRL.umac_sw_rst */ |
| 1912 | bcmgenet_rbuf_ctrl_set(priv, 0); |
| 1913 | udelay(10); |
| 1914 | |
Doug Berger | 88f6c8b | 2020-03-16 14:44:56 -0700 | [diff] [blame] | 1915 | /* issue soft reset and disable MAC while updating its registers */ |
| 1916 | bcmgenet_umac_writel(priv, CMD_SW_RESET, UMAC_CMD); |
Doug Berger | 612eb1c | 2020-03-16 14:44:55 -0700 | [diff] [blame] | 1917 | udelay(2); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1918 | } |
| 1919 | |
Florian Fainelli | 909ff5e | 2014-07-21 15:29:21 -0700 | [diff] [blame] | 1920 | static void bcmgenet_intr_disable(struct bcmgenet_priv *priv) |
| 1921 | { |
| 1922 | /* Mask all interrupts.*/ |
| 1923 | bcmgenet_intrl2_0_writel(priv, 0xFFFFFFFF, INTRL2_CPU_MASK_SET); |
| 1924 | bcmgenet_intrl2_0_writel(priv, 0xFFFFFFFF, INTRL2_CPU_CLEAR); |
Florian Fainelli | 909ff5e | 2014-07-21 15:29:21 -0700 | [diff] [blame] | 1925 | bcmgenet_intrl2_1_writel(priv, 0xFFFFFFFF, INTRL2_CPU_MASK_SET); |
| 1926 | bcmgenet_intrl2_1_writel(priv, 0xFFFFFFFF, INTRL2_CPU_CLEAR); |
Florian Fainelli | 909ff5e | 2014-07-21 15:29:21 -0700 | [diff] [blame] | 1927 | } |
| 1928 | |
Florian Fainelli | 37850e3 | 2015-10-17 14:22:46 -0700 | [diff] [blame] | 1929 | static void bcmgenet_link_intr_enable(struct bcmgenet_priv *priv) |
| 1930 | { |
| 1931 | u32 int0_enable = 0; |
| 1932 | |
| 1933 | /* Monitor cable plug/unplugged event for internal PHY, external PHY |
| 1934 | * and MoCA PHY |
| 1935 | */ |
| 1936 | if (priv->internal_phy) { |
| 1937 | int0_enable |= UMAC_IRQ_LINK_EVENT; |
Doug Berger | 25382b9 | 2019-10-16 16:06:32 -0700 | [diff] [blame] | 1938 | if (GENET_IS_V1(priv) || GENET_IS_V2(priv) || GENET_IS_V3(priv)) |
| 1939 | int0_enable |= UMAC_IRQ_PHY_DET_R; |
Florian Fainelli | 37850e3 | 2015-10-17 14:22:46 -0700 | [diff] [blame] | 1940 | } else if (priv->ext_phy) { |
| 1941 | int0_enable |= UMAC_IRQ_LINK_EVENT; |
| 1942 | } else if (priv->phy_interface == PHY_INTERFACE_MODE_MOCA) { |
| 1943 | if (priv->hw_params->flags & GENET_HAS_MOCA_LINK_DET) |
| 1944 | int0_enable |= UMAC_IRQ_LINK_EVENT; |
| 1945 | } |
| 1946 | bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR); |
| 1947 | } |
| 1948 | |
Doug Berger | 28c2d1a | 2017-10-25 15:04:13 -0700 | [diff] [blame] | 1949 | static void init_umac(struct bcmgenet_priv *priv) |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1950 | { |
| 1951 | struct device *kdev = &priv->pdev->dev; |
Petri Gynther | b2e97ec | 2015-03-25 12:35:12 -0700 | [diff] [blame] | 1952 | u32 reg; |
| 1953 | u32 int0_enable = 0; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1954 | |
| 1955 | dev_dbg(&priv->pdev->dev, "bcmgenet: init_umac\n"); |
| 1956 | |
Doug Berger | 28c2d1a | 2017-10-25 15:04:13 -0700 | [diff] [blame] | 1957 | reset_umac(priv); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1958 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1959 | /* clear tx/rx counter */ |
| 1960 | bcmgenet_umac_writel(priv, |
Florian Fainelli | c91b7f6 | 2014-07-23 10:42:12 -0700 | [diff] [blame] | 1961 | MIB_RESET_RX | MIB_RESET_TX | MIB_RESET_RUNT, |
| 1962 | UMAC_MIB_CTRL); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1963 | bcmgenet_umac_writel(priv, 0, UMAC_MIB_CTRL); |
| 1964 | |
| 1965 | bcmgenet_umac_writel(priv, ENET_MAX_MTU_SIZE, UMAC_MAX_FRAME_LEN); |
| 1966 | |
Doug Berger | 9a9ba2a | 2020-03-17 17:05:36 -0700 | [diff] [blame] | 1967 | /* init tx registers, enable TSB */ |
| 1968 | reg = bcmgenet_tbuf_ctrl_get(priv); |
| 1969 | reg |= TBUF_64B_EN; |
| 1970 | bcmgenet_tbuf_ctrl_set(priv, reg); |
| 1971 | |
| 1972 | /* init rx registers, enable ip header optimization and RSB */ |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1973 | reg = bcmgenet_rbuf_readl(priv, RBUF_CTRL); |
Doug Berger | 9a9ba2a | 2020-03-17 17:05:36 -0700 | [diff] [blame] | 1974 | reg |= RBUF_ALIGN_2B | RBUF_64B_EN; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1975 | bcmgenet_rbuf_writel(priv, reg, RBUF_CTRL); |
| 1976 | |
Doug Berger | 9a9ba2a | 2020-03-17 17:05:36 -0700 | [diff] [blame] | 1977 | /* enable rx checksumming */ |
| 1978 | reg = bcmgenet_rbuf_readl(priv, RBUF_CHK_CTRL); |
| 1979 | reg |= RBUF_RXCHK_EN | RBUF_L3_PARSE_DIS; |
| 1980 | /* If UniMAC forwards CRC, we need to skip over it to get |
| 1981 | * a valid CHK bit to be set in the per-packet status word |
| 1982 | */ |
| 1983 | if (priv->crc_fwd_en) |
| 1984 | reg |= RBUF_SKIP_FCS; |
| 1985 | else |
| 1986 | reg &= ~RBUF_SKIP_FCS; |
| 1987 | bcmgenet_rbuf_writel(priv, reg, RBUF_CHK_CTRL); |
| 1988 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1989 | if (!GENET_IS_V1(priv) && !GENET_IS_V2(priv)) |
| 1990 | bcmgenet_rbuf_writel(priv, 1, RBUF_TBUF_SIZE_CTRL); |
| 1991 | |
Florian Fainelli | 909ff5e | 2014-07-21 15:29:21 -0700 | [diff] [blame] | 1992 | bcmgenet_intr_disable(priv); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1993 | |
Florian Fainelli | 37850e3 | 2015-10-17 14:22:46 -0700 | [diff] [blame] | 1994 | /* Configure backpressure vectors for MoCA */ |
| 1995 | if (priv->phy_interface == PHY_INTERFACE_MODE_MOCA) { |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 1996 | reg = bcmgenet_bp_mc_get(priv); |
| 1997 | reg |= BIT(priv->hw_params->bp_in_en_shift); |
| 1998 | |
| 1999 | /* bp_mask: back pressure mask */ |
| 2000 | if (netif_is_multiqueue(priv->dev)) |
| 2001 | reg |= priv->hw_params->bp_in_mask; |
| 2002 | else |
| 2003 | reg &= ~priv->hw_params->bp_in_mask; |
| 2004 | bcmgenet_bp_mc_set(priv, reg); |
| 2005 | } |
| 2006 | |
| 2007 | /* Enable MDIO interrupts on GENET v3+ */ |
| 2008 | if (priv->hw_params->flags & GENET_HAS_MDIO_INTR) |
Petri Gynther | b2e97ec | 2015-03-25 12:35:12 -0700 | [diff] [blame] | 2009 | int0_enable |= (UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2010 | |
Petri Gynther | b2e97ec | 2015-03-25 12:35:12 -0700 | [diff] [blame] | 2011 | bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR); |
Jaedon Shin | 4092e6a | 2015-02-28 11:48:26 +0900 | [diff] [blame] | 2012 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2013 | dev_dbg(kdev, "done init umac\n"); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2014 | } |
| 2015 | |
Florian Fainelli | 5e6ce1f | 2018-03-28 15:15:38 -0700 | [diff] [blame] | 2016 | static void bcmgenet_init_dim(struct bcmgenet_rx_ring *ring, |
Florian Fainelli | 9f4ca05 | 2018-03-22 18:19:33 -0700 | [diff] [blame] | 2017 | void (*cb)(struct work_struct *work)) |
| 2018 | { |
Florian Fainelli | 5e6ce1f | 2018-03-28 15:15:38 -0700 | [diff] [blame] | 2019 | struct bcmgenet_net_dim *dim = &ring->dim; |
| 2020 | |
Florian Fainelli | 9f4ca05 | 2018-03-22 18:19:33 -0700 | [diff] [blame] | 2021 | INIT_WORK(&dim->dim.work, cb); |
Tal Gilboa | c002bd5 | 2018-11-05 12:07:52 +0200 | [diff] [blame] | 2022 | dim->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE; |
Florian Fainelli | 9f4ca05 | 2018-03-22 18:19:33 -0700 | [diff] [blame] | 2023 | dim->event_ctr = 0; |
| 2024 | dim->packets = 0; |
| 2025 | dim->bytes = 0; |
| 2026 | } |
| 2027 | |
Florian Fainelli | 5e6ce1f | 2018-03-28 15:15:38 -0700 | [diff] [blame] | 2028 | static void bcmgenet_init_rx_coalesce(struct bcmgenet_rx_ring *ring) |
| 2029 | { |
| 2030 | struct bcmgenet_net_dim *dim = &ring->dim; |
Tal Gilboa | 8960b38 | 2019-01-31 16:44:48 +0200 | [diff] [blame] | 2031 | struct dim_cq_moder moder; |
Florian Fainelli | 5e6ce1f | 2018-03-28 15:15:38 -0700 | [diff] [blame] | 2032 | u32 usecs, pkts; |
| 2033 | |
| 2034 | usecs = ring->rx_coalesce_usecs; |
| 2035 | pkts = ring->rx_max_coalesced_frames; |
| 2036 | |
| 2037 | /* If DIM was enabled, re-apply default parameters */ |
| 2038 | if (dim->use_dim) { |
Tal Gilboa | 026a807 | 2018-04-24 13:36:01 +0300 | [diff] [blame] | 2039 | moder = net_dim_get_def_rx_moderation(dim->dim.mode); |
Florian Fainelli | 5e6ce1f | 2018-03-28 15:15:38 -0700 | [diff] [blame] | 2040 | usecs = moder.usec; |
| 2041 | pkts = moder.pkts; |
| 2042 | } |
| 2043 | |
| 2044 | bcmgenet_set_rx_coalesce(ring, usecs, pkts); |
| 2045 | } |
| 2046 | |
Petri Gynther | 4f8b2d7 | 2015-02-23 11:00:45 -0800 | [diff] [blame] | 2047 | /* Initialize a Tx ring along with corresponding hardware registers */ |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2048 | static void bcmgenet_init_tx_ring(struct bcmgenet_priv *priv, |
| 2049 | unsigned int index, unsigned int size, |
Petri Gynther | 4f8b2d7 | 2015-02-23 11:00:45 -0800 | [diff] [blame] | 2050 | unsigned int start_ptr, unsigned int end_ptr) |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2051 | { |
| 2052 | struct bcmgenet_tx_ring *ring = &priv->tx_rings[index]; |
| 2053 | u32 words_per_bd = WORDS_PER_BD(priv); |
| 2054 | u32 flow_period_val = 0; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2055 | |
| 2056 | spin_lock_init(&ring->lock); |
Jaedon Shin | 4092e6a | 2015-02-28 11:48:26 +0900 | [diff] [blame] | 2057 | ring->priv = priv; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2058 | ring->index = index; |
| 2059 | if (index == DESC_INDEX) { |
| 2060 | ring->queue = 0; |
| 2061 | ring->int_enable = bcmgenet_tx_ring16_int_enable; |
| 2062 | ring->int_disable = bcmgenet_tx_ring16_int_disable; |
| 2063 | } else { |
| 2064 | ring->queue = index + 1; |
| 2065 | ring->int_enable = bcmgenet_tx_ring_int_enable; |
| 2066 | ring->int_disable = bcmgenet_tx_ring_int_disable; |
| 2067 | } |
Petri Gynther | 4f8b2d7 | 2015-02-23 11:00:45 -0800 | [diff] [blame] | 2068 | ring->cbs = priv->tx_cbs + start_ptr; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2069 | ring->size = size; |
Petri Gynther | 66d0675 | 2015-03-04 14:30:01 -0800 | [diff] [blame] | 2070 | ring->clean_ptr = start_ptr; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2071 | ring->c_index = 0; |
| 2072 | ring->free_bds = size; |
Petri Gynther | 4f8b2d7 | 2015-02-23 11:00:45 -0800 | [diff] [blame] | 2073 | ring->write_ptr = start_ptr; |
| 2074 | ring->cb_ptr = start_ptr; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2075 | ring->end_ptr = end_ptr - 1; |
| 2076 | ring->prod_index = 0; |
| 2077 | |
| 2078 | /* Set flow period for ring != 16 */ |
| 2079 | if (index != DESC_INDEX) |
| 2080 | flow_period_val = ENET_MAX_MTU_SIZE << 16; |
| 2081 | |
| 2082 | bcmgenet_tdma_ring_writel(priv, index, 0, TDMA_PROD_INDEX); |
| 2083 | bcmgenet_tdma_ring_writel(priv, index, 0, TDMA_CONS_INDEX); |
| 2084 | bcmgenet_tdma_ring_writel(priv, index, 1, DMA_MBUF_DONE_THRESH); |
| 2085 | /* Disable rate control for now */ |
| 2086 | bcmgenet_tdma_ring_writel(priv, index, flow_period_val, |
Florian Fainelli | c91b7f6 | 2014-07-23 10:42:12 -0700 | [diff] [blame] | 2087 | TDMA_FLOW_PERIOD); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2088 | bcmgenet_tdma_ring_writel(priv, index, |
Florian Fainelli | c91b7f6 | 2014-07-23 10:42:12 -0700 | [diff] [blame] | 2089 | ((size << DMA_RING_SIZE_SHIFT) | |
| 2090 | RX_BUF_LENGTH), DMA_RING_BUF_SIZE); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2091 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2092 | /* Set start and end address, read and write pointers */ |
Petri Gynther | 4f8b2d7 | 2015-02-23 11:00:45 -0800 | [diff] [blame] | 2093 | bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd, |
Florian Fainelli | c91b7f6 | 2014-07-23 10:42:12 -0700 | [diff] [blame] | 2094 | DMA_START_ADDR); |
Petri Gynther | 4f8b2d7 | 2015-02-23 11:00:45 -0800 | [diff] [blame] | 2095 | bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd, |
Florian Fainelli | c91b7f6 | 2014-07-23 10:42:12 -0700 | [diff] [blame] | 2096 | TDMA_READ_PTR); |
Petri Gynther | 4f8b2d7 | 2015-02-23 11:00:45 -0800 | [diff] [blame] | 2097 | bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd, |
Florian Fainelli | c91b7f6 | 2014-07-23 10:42:12 -0700 | [diff] [blame] | 2098 | TDMA_WRITE_PTR); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2099 | bcmgenet_tdma_ring_writel(priv, index, end_ptr * words_per_bd - 1, |
Florian Fainelli | c91b7f6 | 2014-07-23 10:42:12 -0700 | [diff] [blame] | 2100 | DMA_END_ADDR); |
Doug Berger | 7587935 | 2017-10-25 15:04:14 -0700 | [diff] [blame] | 2101 | |
| 2102 | /* Initialize Tx NAPI */ |
Florian Fainelli | 148965d | 2020-01-23 09:49:34 -0800 | [diff] [blame] | 2103 | netif_tx_napi_add(priv->dev, &ring->napi, bcmgenet_tx_poll, |
| 2104 | NAPI_POLL_WEIGHT); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2105 | } |
| 2106 | |
| 2107 | /* Initialize a RDMA ring */ |
| 2108 | static int bcmgenet_init_rx_ring(struct bcmgenet_priv *priv, |
Petri Gynther | 8ac467e | 2015-03-09 13:40:00 -0700 | [diff] [blame] | 2109 | unsigned int index, unsigned int size, |
| 2110 | unsigned int start_ptr, unsigned int end_ptr) |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2111 | { |
Petri Gynther | 8ac467e | 2015-03-09 13:40:00 -0700 | [diff] [blame] | 2112 | struct bcmgenet_rx_ring *ring = &priv->rx_rings[index]; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2113 | u32 words_per_bd = WORDS_PER_BD(priv); |
| 2114 | int ret; |
| 2115 | |
Petri Gynther | 4055eae | 2015-03-25 12:35:16 -0700 | [diff] [blame] | 2116 | ring->priv = priv; |
Petri Gynther | 8ac467e | 2015-03-09 13:40:00 -0700 | [diff] [blame] | 2117 | ring->index = index; |
Petri Gynther | 4055eae | 2015-03-25 12:35:16 -0700 | [diff] [blame] | 2118 | if (index == DESC_INDEX) { |
| 2119 | ring->int_enable = bcmgenet_rx_ring16_int_enable; |
| 2120 | ring->int_disable = bcmgenet_rx_ring16_int_disable; |
| 2121 | } else { |
| 2122 | ring->int_enable = bcmgenet_rx_ring_int_enable; |
| 2123 | ring->int_disable = bcmgenet_rx_ring_int_disable; |
| 2124 | } |
Petri Gynther | 8ac467e | 2015-03-09 13:40:00 -0700 | [diff] [blame] | 2125 | ring->cbs = priv->rx_cbs + start_ptr; |
| 2126 | ring->size = size; |
| 2127 | ring->c_index = 0; |
| 2128 | ring->read_ptr = start_ptr; |
| 2129 | ring->cb_ptr = start_ptr; |
| 2130 | ring->end_ptr = end_ptr - 1; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2131 | |
Petri Gynther | 8ac467e | 2015-03-09 13:40:00 -0700 | [diff] [blame] | 2132 | ret = bcmgenet_alloc_rx_buffers(priv, ring); |
| 2133 | if (ret) |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2134 | return ret; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2135 | |
Florian Fainelli | 5e6ce1f | 2018-03-28 15:15:38 -0700 | [diff] [blame] | 2136 | bcmgenet_init_dim(ring, bcmgenet_dim_work); |
| 2137 | bcmgenet_init_rx_coalesce(ring); |
Florian Fainelli | 9f4ca05 | 2018-03-22 18:19:33 -0700 | [diff] [blame] | 2138 | |
Doug Berger | 7587935 | 2017-10-25 15:04:14 -0700 | [diff] [blame] | 2139 | /* Initialize Rx NAPI */ |
| 2140 | netif_napi_add(priv->dev, &ring->napi, bcmgenet_rx_poll, |
| 2141 | NAPI_POLL_WEIGHT); |
| 2142 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2143 | bcmgenet_rdma_ring_writel(priv, index, 0, RDMA_PROD_INDEX); |
| 2144 | bcmgenet_rdma_ring_writel(priv, index, 0, RDMA_CONS_INDEX); |
| 2145 | bcmgenet_rdma_ring_writel(priv, index, |
Florian Fainelli | c91b7f6 | 2014-07-23 10:42:12 -0700 | [diff] [blame] | 2146 | ((size << DMA_RING_SIZE_SHIFT) | |
| 2147 | RX_BUF_LENGTH), DMA_RING_BUF_SIZE); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2148 | bcmgenet_rdma_ring_writel(priv, index, |
Florian Fainelli | c91b7f6 | 2014-07-23 10:42:12 -0700 | [diff] [blame] | 2149 | (DMA_FC_THRESH_LO << |
| 2150 | DMA_XOFF_THRESHOLD_SHIFT) | |
| 2151 | DMA_FC_THRESH_HI, RDMA_XON_XOFF_THRESH); |
Petri Gynther | 6f5a272 | 2015-03-06 13:45:00 -0800 | [diff] [blame] | 2152 | |
| 2153 | /* Set start and end address, read and write pointers */ |
Petri Gynther | 8ac467e | 2015-03-09 13:40:00 -0700 | [diff] [blame] | 2154 | bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd, |
| 2155 | DMA_START_ADDR); |
| 2156 | bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd, |
| 2157 | RDMA_READ_PTR); |
| 2158 | bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd, |
| 2159 | RDMA_WRITE_PTR); |
| 2160 | bcmgenet_rdma_ring_writel(priv, index, end_ptr * words_per_bd - 1, |
Petri Gynther | 6f5a272 | 2015-03-06 13:45:00 -0800 | [diff] [blame] | 2161 | DMA_END_ADDR); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2162 | |
| 2163 | return ret; |
| 2164 | } |
| 2165 | |
Petri Gynther | e2aadb4 | 2015-03-25 12:35:14 -0700 | [diff] [blame] | 2166 | static void bcmgenet_enable_tx_napi(struct bcmgenet_priv *priv) |
| 2167 | { |
| 2168 | unsigned int i; |
| 2169 | struct bcmgenet_tx_ring *ring; |
| 2170 | |
| 2171 | for (i = 0; i < priv->hw_params->tx_queues; ++i) { |
| 2172 | ring = &priv->tx_rings[i]; |
| 2173 | napi_enable(&ring->napi); |
Doug Berger | fbf557d | 2017-10-25 15:04:15 -0700 | [diff] [blame] | 2174 | ring->int_enable(ring); |
Petri Gynther | e2aadb4 | 2015-03-25 12:35:14 -0700 | [diff] [blame] | 2175 | } |
| 2176 | |
| 2177 | ring = &priv->tx_rings[DESC_INDEX]; |
| 2178 | napi_enable(&ring->napi); |
Doug Berger | fbf557d | 2017-10-25 15:04:15 -0700 | [diff] [blame] | 2179 | ring->int_enable(ring); |
Petri Gynther | e2aadb4 | 2015-03-25 12:35:14 -0700 | [diff] [blame] | 2180 | } |
| 2181 | |
| 2182 | static void bcmgenet_disable_tx_napi(struct bcmgenet_priv *priv) |
| 2183 | { |
| 2184 | unsigned int i; |
| 2185 | struct bcmgenet_tx_ring *ring; |
| 2186 | |
| 2187 | for (i = 0; i < priv->hw_params->tx_queues; ++i) { |
| 2188 | ring = &priv->tx_rings[i]; |
| 2189 | napi_disable(&ring->napi); |
| 2190 | } |
| 2191 | |
| 2192 | ring = &priv->tx_rings[DESC_INDEX]; |
| 2193 | napi_disable(&ring->napi); |
| 2194 | } |
| 2195 | |
| 2196 | static void bcmgenet_fini_tx_napi(struct bcmgenet_priv *priv) |
| 2197 | { |
| 2198 | unsigned int i; |
| 2199 | struct bcmgenet_tx_ring *ring; |
| 2200 | |
| 2201 | for (i = 0; i < priv->hw_params->tx_queues; ++i) { |
| 2202 | ring = &priv->tx_rings[i]; |
| 2203 | netif_napi_del(&ring->napi); |
| 2204 | } |
| 2205 | |
| 2206 | ring = &priv->tx_rings[DESC_INDEX]; |
| 2207 | netif_napi_del(&ring->napi); |
| 2208 | } |
| 2209 | |
Petri Gynther | 16c6d66 | 2015-02-23 11:00:45 -0800 | [diff] [blame] | 2210 | /* Initialize Tx queues |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2211 | * |
Petri Gynther | 16c6d66 | 2015-02-23 11:00:45 -0800 | [diff] [blame] | 2212 | * Queues 0-3 are priority-based, each one has 32 descriptors, |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2213 | * with queue 0 being the highest priority queue. |
| 2214 | * |
Petri Gynther | 16c6d66 | 2015-02-23 11:00:45 -0800 | [diff] [blame] | 2215 | * Queue 16 is the default Tx queue with |
Petri Gynther | 51a966a | 2015-02-23 11:00:46 -0800 | [diff] [blame] | 2216 | * GENET_Q16_TX_BD_CNT = 256 - 4 * 32 = 128 descriptors. |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2217 | * |
Petri Gynther | 16c6d66 | 2015-02-23 11:00:45 -0800 | [diff] [blame] | 2218 | * The transmit control block pool is then partitioned as follows: |
| 2219 | * - Tx queue 0 uses tx_cbs[0..31] |
| 2220 | * - Tx queue 1 uses tx_cbs[32..63] |
| 2221 | * - Tx queue 2 uses tx_cbs[64..95] |
| 2222 | * - Tx queue 3 uses tx_cbs[96..127] |
| 2223 | * - Tx queue 16 uses tx_cbs[128..255] |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2224 | */ |
Petri Gynther | 16c6d66 | 2015-02-23 11:00:45 -0800 | [diff] [blame] | 2225 | static void bcmgenet_init_tx_queues(struct net_device *dev) |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2226 | { |
| 2227 | struct bcmgenet_priv *priv = netdev_priv(dev); |
Petri Gynther | 16c6d66 | 2015-02-23 11:00:45 -0800 | [diff] [blame] | 2228 | u32 i, dma_enable; |
| 2229 | u32 dma_ctrl, ring_cfg; |
Petri Gynther | 3774216 | 2014-10-07 09:30:01 -0700 | [diff] [blame] | 2230 | u32 dma_priority[3] = {0, 0, 0}; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2231 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2232 | dma_ctrl = bcmgenet_tdma_readl(priv, DMA_CTRL); |
| 2233 | dma_enable = dma_ctrl & DMA_EN; |
| 2234 | dma_ctrl &= ~DMA_EN; |
| 2235 | bcmgenet_tdma_writel(priv, dma_ctrl, DMA_CTRL); |
| 2236 | |
Petri Gynther | 16c6d66 | 2015-02-23 11:00:45 -0800 | [diff] [blame] | 2237 | dma_ctrl = 0; |
| 2238 | ring_cfg = 0; |
| 2239 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2240 | /* Enable strict priority arbiter mode */ |
| 2241 | bcmgenet_tdma_writel(priv, DMA_ARBITER_SP, DMA_ARB_CTRL); |
| 2242 | |
Petri Gynther | 16c6d66 | 2015-02-23 11:00:45 -0800 | [diff] [blame] | 2243 | /* Initialize Tx priority queues */ |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2244 | for (i = 0; i < priv->hw_params->tx_queues; i++) { |
Petri Gynther | 51a966a | 2015-02-23 11:00:46 -0800 | [diff] [blame] | 2245 | bcmgenet_init_tx_ring(priv, i, priv->hw_params->tx_bds_per_q, |
| 2246 | i * priv->hw_params->tx_bds_per_q, |
| 2247 | (i + 1) * priv->hw_params->tx_bds_per_q); |
Petri Gynther | 16c6d66 | 2015-02-23 11:00:45 -0800 | [diff] [blame] | 2248 | ring_cfg |= (1 << i); |
| 2249 | dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT)); |
Petri Gynther | 3774216 | 2014-10-07 09:30:01 -0700 | [diff] [blame] | 2250 | dma_priority[DMA_PRIO_REG_INDEX(i)] |= |
| 2251 | ((GENET_Q0_PRIORITY + i) << DMA_PRIO_REG_SHIFT(i)); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2252 | } |
| 2253 | |
Petri Gynther | 16c6d66 | 2015-02-23 11:00:45 -0800 | [diff] [blame] | 2254 | /* Initialize Tx default queue 16 */ |
Petri Gynther | 51a966a | 2015-02-23 11:00:46 -0800 | [diff] [blame] | 2255 | bcmgenet_init_tx_ring(priv, DESC_INDEX, GENET_Q16_TX_BD_CNT, |
Petri Gynther | 16c6d66 | 2015-02-23 11:00:45 -0800 | [diff] [blame] | 2256 | priv->hw_params->tx_queues * |
Petri Gynther | 51a966a | 2015-02-23 11:00:46 -0800 | [diff] [blame] | 2257 | priv->hw_params->tx_bds_per_q, |
Petri Gynther | 16c6d66 | 2015-02-23 11:00:45 -0800 | [diff] [blame] | 2258 | TOTAL_DESC); |
| 2259 | ring_cfg |= (1 << DESC_INDEX); |
| 2260 | dma_ctrl |= (1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT)); |
Petri Gynther | 3774216 | 2014-10-07 09:30:01 -0700 | [diff] [blame] | 2261 | dma_priority[DMA_PRIO_REG_INDEX(DESC_INDEX)] |= |
| 2262 | ((GENET_Q0_PRIORITY + priv->hw_params->tx_queues) << |
| 2263 | DMA_PRIO_REG_SHIFT(DESC_INDEX)); |
Petri Gynther | 16c6d66 | 2015-02-23 11:00:45 -0800 | [diff] [blame] | 2264 | |
| 2265 | /* Set Tx queue priorities */ |
Petri Gynther | 3774216 | 2014-10-07 09:30:01 -0700 | [diff] [blame] | 2266 | bcmgenet_tdma_writel(priv, dma_priority[0], DMA_PRIORITY_0); |
| 2267 | bcmgenet_tdma_writel(priv, dma_priority[1], DMA_PRIORITY_1); |
| 2268 | bcmgenet_tdma_writel(priv, dma_priority[2], DMA_PRIORITY_2); |
| 2269 | |
Petri Gynther | 16c6d66 | 2015-02-23 11:00:45 -0800 | [diff] [blame] | 2270 | /* Enable Tx queues */ |
| 2271 | bcmgenet_tdma_writel(priv, ring_cfg, DMA_RING_CFG); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2272 | |
Petri Gynther | 16c6d66 | 2015-02-23 11:00:45 -0800 | [diff] [blame] | 2273 | /* Enable Tx DMA */ |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2274 | if (dma_enable) |
Petri Gynther | 16c6d66 | 2015-02-23 11:00:45 -0800 | [diff] [blame] | 2275 | dma_ctrl |= DMA_EN; |
| 2276 | bcmgenet_tdma_writel(priv, dma_ctrl, DMA_CTRL); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2277 | } |
| 2278 | |
Petri Gynther | 3ab1133 | 2015-03-25 12:35:15 -0700 | [diff] [blame] | 2279 | static void bcmgenet_enable_rx_napi(struct bcmgenet_priv *priv) |
| 2280 | { |
Petri Gynther | 4055eae | 2015-03-25 12:35:16 -0700 | [diff] [blame] | 2281 | unsigned int i; |
| 2282 | struct bcmgenet_rx_ring *ring; |
| 2283 | |
| 2284 | for (i = 0; i < priv->hw_params->rx_queues; ++i) { |
| 2285 | ring = &priv->rx_rings[i]; |
| 2286 | napi_enable(&ring->napi); |
Doug Berger | fbf557d | 2017-10-25 15:04:15 -0700 | [diff] [blame] | 2287 | ring->int_enable(ring); |
Petri Gynther | 4055eae | 2015-03-25 12:35:16 -0700 | [diff] [blame] | 2288 | } |
| 2289 | |
| 2290 | ring = &priv->rx_rings[DESC_INDEX]; |
| 2291 | napi_enable(&ring->napi); |
Doug Berger | fbf557d | 2017-10-25 15:04:15 -0700 | [diff] [blame] | 2292 | ring->int_enable(ring); |
Petri Gynther | 3ab1133 | 2015-03-25 12:35:15 -0700 | [diff] [blame] | 2293 | } |
| 2294 | |
| 2295 | static void bcmgenet_disable_rx_napi(struct bcmgenet_priv *priv) |
| 2296 | { |
Petri Gynther | 4055eae | 2015-03-25 12:35:16 -0700 | [diff] [blame] | 2297 | unsigned int i; |
| 2298 | struct bcmgenet_rx_ring *ring; |
| 2299 | |
| 2300 | for (i = 0; i < priv->hw_params->rx_queues; ++i) { |
| 2301 | ring = &priv->rx_rings[i]; |
| 2302 | napi_disable(&ring->napi); |
Florian Fainelli | 9f4ca05 | 2018-03-22 18:19:33 -0700 | [diff] [blame] | 2303 | cancel_work_sync(&ring->dim.dim.work); |
Petri Gynther | 4055eae | 2015-03-25 12:35:16 -0700 | [diff] [blame] | 2304 | } |
| 2305 | |
| 2306 | ring = &priv->rx_rings[DESC_INDEX]; |
| 2307 | napi_disable(&ring->napi); |
Florian Fainelli | 9f4ca05 | 2018-03-22 18:19:33 -0700 | [diff] [blame] | 2308 | cancel_work_sync(&ring->dim.dim.work); |
Petri Gynther | 3ab1133 | 2015-03-25 12:35:15 -0700 | [diff] [blame] | 2309 | } |
| 2310 | |
| 2311 | static void bcmgenet_fini_rx_napi(struct bcmgenet_priv *priv) |
| 2312 | { |
Petri Gynther | 4055eae | 2015-03-25 12:35:16 -0700 | [diff] [blame] | 2313 | unsigned int i; |
| 2314 | struct bcmgenet_rx_ring *ring; |
| 2315 | |
| 2316 | for (i = 0; i < priv->hw_params->rx_queues; ++i) { |
| 2317 | ring = &priv->rx_rings[i]; |
| 2318 | netif_napi_del(&ring->napi); |
| 2319 | } |
| 2320 | |
| 2321 | ring = &priv->rx_rings[DESC_INDEX]; |
| 2322 | netif_napi_del(&ring->napi); |
Petri Gynther | 3ab1133 | 2015-03-25 12:35:15 -0700 | [diff] [blame] | 2323 | } |
| 2324 | |
Petri Gynther | 8ac467e | 2015-03-09 13:40:00 -0700 | [diff] [blame] | 2325 | /* Initialize Rx queues |
| 2326 | * |
| 2327 | * Queues 0-15 are priority queues. Hardware Filtering Block (HFB) can be |
| 2328 | * used to direct traffic to these queues. |
| 2329 | * |
| 2330 | * Queue 16 is the default Rx queue with GENET_Q16_RX_BD_CNT descriptors. |
| 2331 | */ |
| 2332 | static int bcmgenet_init_rx_queues(struct net_device *dev) |
| 2333 | { |
| 2334 | struct bcmgenet_priv *priv = netdev_priv(dev); |
| 2335 | u32 i; |
| 2336 | u32 dma_enable; |
| 2337 | u32 dma_ctrl; |
| 2338 | u32 ring_cfg; |
| 2339 | int ret; |
| 2340 | |
| 2341 | dma_ctrl = bcmgenet_rdma_readl(priv, DMA_CTRL); |
| 2342 | dma_enable = dma_ctrl & DMA_EN; |
| 2343 | dma_ctrl &= ~DMA_EN; |
| 2344 | bcmgenet_rdma_writel(priv, dma_ctrl, DMA_CTRL); |
| 2345 | |
| 2346 | dma_ctrl = 0; |
| 2347 | ring_cfg = 0; |
| 2348 | |
| 2349 | /* Initialize Rx priority queues */ |
| 2350 | for (i = 0; i < priv->hw_params->rx_queues; i++) { |
| 2351 | ret = bcmgenet_init_rx_ring(priv, i, |
| 2352 | priv->hw_params->rx_bds_per_q, |
| 2353 | i * priv->hw_params->rx_bds_per_q, |
| 2354 | (i + 1) * |
| 2355 | priv->hw_params->rx_bds_per_q); |
| 2356 | if (ret) |
| 2357 | return ret; |
| 2358 | |
| 2359 | ring_cfg |= (1 << i); |
| 2360 | dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT)); |
| 2361 | } |
| 2362 | |
| 2363 | /* Initialize Rx default queue 16 */ |
| 2364 | ret = bcmgenet_init_rx_ring(priv, DESC_INDEX, GENET_Q16_RX_BD_CNT, |
| 2365 | priv->hw_params->rx_queues * |
| 2366 | priv->hw_params->rx_bds_per_q, |
| 2367 | TOTAL_DESC); |
| 2368 | if (ret) |
| 2369 | return ret; |
| 2370 | |
| 2371 | ring_cfg |= (1 << DESC_INDEX); |
| 2372 | dma_ctrl |= (1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT)); |
| 2373 | |
| 2374 | /* Enable rings */ |
| 2375 | bcmgenet_rdma_writel(priv, ring_cfg, DMA_RING_CFG); |
| 2376 | |
| 2377 | /* Configure ring as descriptor ring and re-enable DMA if enabled */ |
| 2378 | if (dma_enable) |
| 2379 | dma_ctrl |= DMA_EN; |
| 2380 | bcmgenet_rdma_writel(priv, dma_ctrl, DMA_CTRL); |
| 2381 | |
| 2382 | return 0; |
| 2383 | } |
| 2384 | |
Florian Fainelli | 4a0c081e | 2014-09-22 11:54:43 -0700 | [diff] [blame] | 2385 | static int bcmgenet_dma_teardown(struct bcmgenet_priv *priv) |
| 2386 | { |
| 2387 | int ret = 0; |
| 2388 | int timeout = 0; |
| 2389 | u32 reg; |
Jaedon Shin | b6df7d6 | 2015-08-21 10:08:26 +0900 | [diff] [blame] | 2390 | u32 dma_ctrl; |
| 2391 | int i; |
Florian Fainelli | 4a0c081e | 2014-09-22 11:54:43 -0700 | [diff] [blame] | 2392 | |
| 2393 | /* Disable TDMA to stop add more frames in TX DMA */ |
| 2394 | reg = bcmgenet_tdma_readl(priv, DMA_CTRL); |
| 2395 | reg &= ~DMA_EN; |
| 2396 | bcmgenet_tdma_writel(priv, reg, DMA_CTRL); |
| 2397 | |
| 2398 | /* Check TDMA status register to confirm TDMA is disabled */ |
| 2399 | while (timeout++ < DMA_TIMEOUT_VAL) { |
| 2400 | reg = bcmgenet_tdma_readl(priv, DMA_STATUS); |
| 2401 | if (reg & DMA_DISABLED) |
| 2402 | break; |
| 2403 | |
| 2404 | udelay(1); |
| 2405 | } |
| 2406 | |
| 2407 | if (timeout == DMA_TIMEOUT_VAL) { |
| 2408 | netdev_warn(priv->dev, "Timed out while disabling TX DMA\n"); |
| 2409 | ret = -ETIMEDOUT; |
| 2410 | } |
| 2411 | |
| 2412 | /* Wait 10ms for packet drain in both tx and rx dma */ |
| 2413 | usleep_range(10000, 20000); |
| 2414 | |
| 2415 | /* Disable RDMA */ |
| 2416 | reg = bcmgenet_rdma_readl(priv, DMA_CTRL); |
| 2417 | reg &= ~DMA_EN; |
| 2418 | bcmgenet_rdma_writel(priv, reg, DMA_CTRL); |
| 2419 | |
| 2420 | timeout = 0; |
| 2421 | /* Check RDMA status register to confirm RDMA is disabled */ |
| 2422 | while (timeout++ < DMA_TIMEOUT_VAL) { |
| 2423 | reg = bcmgenet_rdma_readl(priv, DMA_STATUS); |
| 2424 | if (reg & DMA_DISABLED) |
| 2425 | break; |
| 2426 | |
| 2427 | udelay(1); |
| 2428 | } |
| 2429 | |
| 2430 | if (timeout == DMA_TIMEOUT_VAL) { |
| 2431 | netdev_warn(priv->dev, "Timed out while disabling RX DMA\n"); |
| 2432 | ret = -ETIMEDOUT; |
| 2433 | } |
| 2434 | |
Jaedon Shin | b6df7d6 | 2015-08-21 10:08:26 +0900 | [diff] [blame] | 2435 | dma_ctrl = 0; |
| 2436 | for (i = 0; i < priv->hw_params->rx_queues; i++) |
| 2437 | dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT)); |
| 2438 | reg = bcmgenet_rdma_readl(priv, DMA_CTRL); |
| 2439 | reg &= ~dma_ctrl; |
| 2440 | bcmgenet_rdma_writel(priv, reg, DMA_CTRL); |
| 2441 | |
| 2442 | dma_ctrl = 0; |
| 2443 | for (i = 0; i < priv->hw_params->tx_queues; i++) |
| 2444 | dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT)); |
| 2445 | reg = bcmgenet_tdma_readl(priv, DMA_CTRL); |
| 2446 | reg &= ~dma_ctrl; |
| 2447 | bcmgenet_tdma_writel(priv, reg, DMA_CTRL); |
| 2448 | |
Florian Fainelli | 4a0c081e | 2014-09-22 11:54:43 -0700 | [diff] [blame] | 2449 | return ret; |
| 2450 | } |
| 2451 | |
Petri Gynther | 9abab96 | 2015-03-30 00:29:01 -0700 | [diff] [blame] | 2452 | static void bcmgenet_fini_dma(struct bcmgenet_priv *priv) |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2453 | { |
Petri Gynther | e178c8c | 2016-04-09 00:20:36 -0700 | [diff] [blame] | 2454 | struct netdev_queue *txq; |
Doug Berger | f48bed1 | 2017-07-14 16:12:10 -0700 | [diff] [blame] | 2455 | int i; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2456 | |
Petri Gynther | 9abab96 | 2015-03-30 00:29:01 -0700 | [diff] [blame] | 2457 | bcmgenet_fini_rx_napi(priv); |
| 2458 | bcmgenet_fini_tx_napi(priv); |
| 2459 | |
Markus Elfring | 399e06a | 2019-08-22 20:02:56 +0200 | [diff] [blame] | 2460 | for (i = 0; i < priv->num_tx_bds; i++) |
| 2461 | dev_kfree_skb(bcmgenet_free_tx_cb(&priv->pdev->dev, |
| 2462 | priv->tx_cbs + i)); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2463 | |
Petri Gynther | e178c8c | 2016-04-09 00:20:36 -0700 | [diff] [blame] | 2464 | for (i = 0; i < priv->hw_params->tx_queues; i++) { |
| 2465 | txq = netdev_get_tx_queue(priv->dev, priv->tx_rings[i].queue); |
| 2466 | netdev_tx_reset_queue(txq); |
| 2467 | } |
| 2468 | |
| 2469 | txq = netdev_get_tx_queue(priv->dev, priv->tx_rings[DESC_INDEX].queue); |
| 2470 | netdev_tx_reset_queue(txq); |
| 2471 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2472 | bcmgenet_free_rx_buffers(priv); |
| 2473 | kfree(priv->rx_cbs); |
| 2474 | kfree(priv->tx_cbs); |
| 2475 | } |
| 2476 | |
| 2477 | /* init_edma: Initialize DMA control register */ |
| 2478 | static int bcmgenet_init_dma(struct bcmgenet_priv *priv) |
| 2479 | { |
| 2480 | int ret; |
Petri Gynther | 014012a | 2015-02-23 11:00:45 -0800 | [diff] [blame] | 2481 | unsigned int i; |
| 2482 | struct enet_cb *cb; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2483 | |
Petri Gynther | 6f5a272 | 2015-03-06 13:45:00 -0800 | [diff] [blame] | 2484 | netif_dbg(priv, hw, priv->dev, "%s\n", __func__); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2485 | |
Petri Gynther | 6f5a272 | 2015-03-06 13:45:00 -0800 | [diff] [blame] | 2486 | /* Initialize common Rx ring structures */ |
| 2487 | priv->rx_bds = priv->base + priv->hw_params->rdma_offset; |
| 2488 | priv->num_rx_bds = TOTAL_DESC; |
| 2489 | priv->rx_cbs = kcalloc(priv->num_rx_bds, sizeof(struct enet_cb), |
| 2490 | GFP_KERNEL); |
| 2491 | if (!priv->rx_cbs) |
| 2492 | return -ENOMEM; |
| 2493 | |
| 2494 | for (i = 0; i < priv->num_rx_bds; i++) { |
| 2495 | cb = priv->rx_cbs + i; |
| 2496 | cb->bd_addr = priv->rx_bds + i * DMA_DESC_SIZE; |
| 2497 | } |
| 2498 | |
Brian Norris | 7fc527f | 2014-07-29 14:34:14 -0700 | [diff] [blame] | 2499 | /* Initialize common TX ring structures */ |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2500 | priv->tx_bds = priv->base + priv->hw_params->tdma_offset; |
| 2501 | priv->num_tx_bds = TOTAL_DESC; |
Florian Fainelli | c489be0 | 2014-07-23 10:42:15 -0700 | [diff] [blame] | 2502 | priv->tx_cbs = kcalloc(priv->num_tx_bds, sizeof(struct enet_cb), |
Florian Fainelli | c91b7f6 | 2014-07-23 10:42:12 -0700 | [diff] [blame] | 2503 | GFP_KERNEL); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2504 | if (!priv->tx_cbs) { |
Petri Gynther | ebbd96f | 2015-03-25 12:35:11 -0700 | [diff] [blame] | 2505 | kfree(priv->rx_cbs); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2506 | return -ENOMEM; |
| 2507 | } |
| 2508 | |
Petri Gynther | 014012a | 2015-02-23 11:00:45 -0800 | [diff] [blame] | 2509 | for (i = 0; i < priv->num_tx_bds; i++) { |
| 2510 | cb = priv->tx_cbs + i; |
| 2511 | cb->bd_addr = priv->tx_bds + i * DMA_DESC_SIZE; |
| 2512 | } |
| 2513 | |
Petri Gynther | ebbd96f | 2015-03-25 12:35:11 -0700 | [diff] [blame] | 2514 | /* Init rDma */ |
Stefan Wahren | a50e3a9 | 2019-11-11 20:49:23 +0100 | [diff] [blame] | 2515 | bcmgenet_rdma_writel(priv, priv->dma_max_burst_length, |
| 2516 | DMA_SCB_BURST_SIZE); |
Petri Gynther | ebbd96f | 2015-03-25 12:35:11 -0700 | [diff] [blame] | 2517 | |
| 2518 | /* Initialize Rx queues */ |
| 2519 | ret = bcmgenet_init_rx_queues(priv->dev); |
| 2520 | if (ret) { |
| 2521 | netdev_err(priv->dev, "failed to initialize Rx queues\n"); |
| 2522 | bcmgenet_free_rx_buffers(priv); |
| 2523 | kfree(priv->rx_cbs); |
| 2524 | kfree(priv->tx_cbs); |
| 2525 | return ret; |
| 2526 | } |
| 2527 | |
| 2528 | /* Init tDma */ |
Stefan Wahren | a50e3a9 | 2019-11-11 20:49:23 +0100 | [diff] [blame] | 2529 | bcmgenet_tdma_writel(priv, priv->dma_max_burst_length, |
| 2530 | DMA_SCB_BURST_SIZE); |
Petri Gynther | ebbd96f | 2015-03-25 12:35:11 -0700 | [diff] [blame] | 2531 | |
Petri Gynther | 16c6d66 | 2015-02-23 11:00:45 -0800 | [diff] [blame] | 2532 | /* Initialize Tx queues */ |
| 2533 | bcmgenet_init_tx_queues(priv->dev); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2534 | |
| 2535 | return 0; |
| 2536 | } |
| 2537 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2538 | /* Interrupt bottom half */ |
| 2539 | static void bcmgenet_irq_task(struct work_struct *work) |
| 2540 | { |
Doug Berger | 07c52d6 | 2017-03-09 16:58:47 -0800 | [diff] [blame] | 2541 | unsigned int status; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2542 | struct bcmgenet_priv *priv = container_of( |
| 2543 | work, struct bcmgenet_priv, bcmgenet_irq_work); |
| 2544 | |
| 2545 | netif_dbg(priv, intr, priv->dev, "%s\n", __func__); |
| 2546 | |
Doug Berger | b0447ec | 2017-10-25 15:04:17 -0700 | [diff] [blame] | 2547 | spin_lock_irq(&priv->lock); |
Doug Berger | 07c52d6 | 2017-03-09 16:58:47 -0800 | [diff] [blame] | 2548 | status = priv->irq0_stat; |
| 2549 | priv->irq0_stat = 0; |
Doug Berger | b0447ec | 2017-10-25 15:04:17 -0700 | [diff] [blame] | 2550 | spin_unlock_irq(&priv->lock); |
Doug Berger | 07c52d6 | 2017-03-09 16:58:47 -0800 | [diff] [blame] | 2551 | |
Doug Berger | 25382b9 | 2019-10-16 16:06:32 -0700 | [diff] [blame] | 2552 | if (status & UMAC_IRQ_PHY_DET_R && |
Doug Berger | 0686bd9 | 2019-11-05 11:07:26 -0800 | [diff] [blame] | 2553 | priv->dev->phydev->autoneg != AUTONEG_ENABLE) { |
Doug Berger | 25382b9 | 2019-10-16 16:06:32 -0700 | [diff] [blame] | 2554 | phy_init_hw(priv->dev->phydev); |
Doug Berger | 0686bd9 | 2019-11-05 11:07:26 -0800 | [diff] [blame] | 2555 | genphy_config_aneg(priv->dev->phydev); |
| 2556 | } |
Doug Berger | 25382b9 | 2019-10-16 16:06:32 -0700 | [diff] [blame] | 2557 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2558 | /* Link UP/DOWN event */ |
Doug Berger | 7de4840 | 2019-10-16 16:06:29 -0700 | [diff] [blame] | 2559 | if (status & UMAC_IRQ_LINK_EVENT) |
Heiner Kallweit | 28b2e0d | 2018-01-10 21:21:31 +0100 | [diff] [blame] | 2560 | phy_mac_interrupt(priv->dev->phydev); |
Doug Berger | 25382b9 | 2019-10-16 16:06:32 -0700 | [diff] [blame] | 2561 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2562 | } |
| 2563 | |
Petri Gynther | 4055eae | 2015-03-25 12:35:16 -0700 | [diff] [blame] | 2564 | /* bcmgenet_isr1: handle Rx and Tx priority queues */ |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2565 | static irqreturn_t bcmgenet_isr1(int irq, void *dev_id) |
| 2566 | { |
| 2567 | struct bcmgenet_priv *priv = dev_id; |
Petri Gynther | 4055eae | 2015-03-25 12:35:16 -0700 | [diff] [blame] | 2568 | struct bcmgenet_rx_ring *rx_ring; |
| 2569 | struct bcmgenet_tx_ring *tx_ring; |
Doug Berger | 07c52d6 | 2017-03-09 16:58:47 -0800 | [diff] [blame] | 2570 | unsigned int index, status; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2571 | |
Doug Berger | 07c52d6 | 2017-03-09 16:58:47 -0800 | [diff] [blame] | 2572 | /* Read irq status */ |
| 2573 | status = bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_STAT) & |
Jaedon Shin | 4092e6a | 2015-02-28 11:48:26 +0900 | [diff] [blame] | 2574 | ~bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_MASK_STATUS); |
Petri Gynther | 4055eae | 2015-03-25 12:35:16 -0700 | [diff] [blame] | 2575 | |
Brian Norris | 7fc527f | 2014-07-29 14:34:14 -0700 | [diff] [blame] | 2576 | /* clear interrupts */ |
Doug Berger | 07c52d6 | 2017-03-09 16:58:47 -0800 | [diff] [blame] | 2577 | bcmgenet_intrl2_1_writel(priv, status, INTRL2_CPU_CLEAR); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2578 | |
| 2579 | netif_dbg(priv, intr, priv->dev, |
Doug Berger | 07c52d6 | 2017-03-09 16:58:47 -0800 | [diff] [blame] | 2580 | "%s: IRQ=0x%x\n", __func__, status); |
Jaedon Shin | 4092e6a | 2015-02-28 11:48:26 +0900 | [diff] [blame] | 2581 | |
Petri Gynther | 4055eae | 2015-03-25 12:35:16 -0700 | [diff] [blame] | 2582 | /* Check Rx priority queue interrupts */ |
| 2583 | for (index = 0; index < priv->hw_params->rx_queues; index++) { |
Doug Berger | 07c52d6 | 2017-03-09 16:58:47 -0800 | [diff] [blame] | 2584 | if (!(status & BIT(UMAC_IRQ1_RX_INTR_SHIFT + index))) |
Petri Gynther | 4055eae | 2015-03-25 12:35:16 -0700 | [diff] [blame] | 2585 | continue; |
| 2586 | |
| 2587 | rx_ring = &priv->rx_rings[index]; |
Florian Fainelli | 9f4ca05 | 2018-03-22 18:19:33 -0700 | [diff] [blame] | 2588 | rx_ring->dim.event_ctr++; |
Petri Gynther | 4055eae | 2015-03-25 12:35:16 -0700 | [diff] [blame] | 2589 | |
| 2590 | if (likely(napi_schedule_prep(&rx_ring->napi))) { |
| 2591 | rx_ring->int_disable(rx_ring); |
Florian Fainelli | dac916f | 2016-04-08 22:30:56 -0700 | [diff] [blame] | 2592 | __napi_schedule_irqoff(&rx_ring->napi); |
Petri Gynther | 4055eae | 2015-03-25 12:35:16 -0700 | [diff] [blame] | 2593 | } |
| 2594 | } |
| 2595 | |
| 2596 | /* Check Tx priority queue interrupts */ |
Jaedon Shin | 4092e6a | 2015-02-28 11:48:26 +0900 | [diff] [blame] | 2597 | for (index = 0; index < priv->hw_params->tx_queues; index++) { |
Doug Berger | 07c52d6 | 2017-03-09 16:58:47 -0800 | [diff] [blame] | 2598 | if (!(status & BIT(index))) |
Jaedon Shin | 4092e6a | 2015-02-28 11:48:26 +0900 | [diff] [blame] | 2599 | continue; |
| 2600 | |
Petri Gynther | 4055eae | 2015-03-25 12:35:16 -0700 | [diff] [blame] | 2601 | tx_ring = &priv->tx_rings[index]; |
Jaedon Shin | 4092e6a | 2015-02-28 11:48:26 +0900 | [diff] [blame] | 2602 | |
Petri Gynther | 4055eae | 2015-03-25 12:35:16 -0700 | [diff] [blame] | 2603 | if (likely(napi_schedule_prep(&tx_ring->napi))) { |
| 2604 | tx_ring->int_disable(tx_ring); |
Florian Fainelli | dac916f | 2016-04-08 22:30:56 -0700 | [diff] [blame] | 2605 | __napi_schedule_irqoff(&tx_ring->napi); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2606 | } |
| 2607 | } |
Jaedon Shin | 4092e6a | 2015-02-28 11:48:26 +0900 | [diff] [blame] | 2608 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2609 | return IRQ_HANDLED; |
| 2610 | } |
| 2611 | |
Petri Gynther | 4055eae | 2015-03-25 12:35:16 -0700 | [diff] [blame] | 2612 | /* bcmgenet_isr0: handle Rx and Tx default queues + other stuff */ |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2613 | static irqreturn_t bcmgenet_isr0(int irq, void *dev_id) |
| 2614 | { |
| 2615 | struct bcmgenet_priv *priv = dev_id; |
Petri Gynther | 4055eae | 2015-03-25 12:35:16 -0700 | [diff] [blame] | 2616 | struct bcmgenet_rx_ring *rx_ring; |
| 2617 | struct bcmgenet_tx_ring *tx_ring; |
Doug Berger | 07c52d6 | 2017-03-09 16:58:47 -0800 | [diff] [blame] | 2618 | unsigned int status; |
| 2619 | unsigned long flags; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2620 | |
Doug Berger | 07c52d6 | 2017-03-09 16:58:47 -0800 | [diff] [blame] | 2621 | /* Read irq status */ |
| 2622 | status = bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_STAT) & |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2623 | ~bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_MASK_STATUS); |
Petri Gynther | 4055eae | 2015-03-25 12:35:16 -0700 | [diff] [blame] | 2624 | |
Brian Norris | 7fc527f | 2014-07-29 14:34:14 -0700 | [diff] [blame] | 2625 | /* clear interrupts */ |
Doug Berger | 07c52d6 | 2017-03-09 16:58:47 -0800 | [diff] [blame] | 2626 | bcmgenet_intrl2_0_writel(priv, status, INTRL2_CPU_CLEAR); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2627 | |
| 2628 | netif_dbg(priv, intr, priv->dev, |
Doug Berger | 07c52d6 | 2017-03-09 16:58:47 -0800 | [diff] [blame] | 2629 | "IRQ=0x%x\n", status); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2630 | |
Doug Berger | 07c52d6 | 2017-03-09 16:58:47 -0800 | [diff] [blame] | 2631 | if (status & UMAC_IRQ_RXDMA_DONE) { |
Petri Gynther | 4055eae | 2015-03-25 12:35:16 -0700 | [diff] [blame] | 2632 | rx_ring = &priv->rx_rings[DESC_INDEX]; |
Florian Fainelli | 9f4ca05 | 2018-03-22 18:19:33 -0700 | [diff] [blame] | 2633 | rx_ring->dim.event_ctr++; |
Jaedon Shin | 4092e6a | 2015-02-28 11:48:26 +0900 | [diff] [blame] | 2634 | |
Petri Gynther | 4055eae | 2015-03-25 12:35:16 -0700 | [diff] [blame] | 2635 | if (likely(napi_schedule_prep(&rx_ring->napi))) { |
| 2636 | rx_ring->int_disable(rx_ring); |
Florian Fainelli | dac916f | 2016-04-08 22:30:56 -0700 | [diff] [blame] | 2637 | __napi_schedule_irqoff(&rx_ring->napi); |
Jaedon Shin | 4092e6a | 2015-02-28 11:48:26 +0900 | [diff] [blame] | 2638 | } |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2639 | } |
Petri Gynther | 4055eae | 2015-03-25 12:35:16 -0700 | [diff] [blame] | 2640 | |
Doug Berger | 07c52d6 | 2017-03-09 16:58:47 -0800 | [diff] [blame] | 2641 | if (status & UMAC_IRQ_TXDMA_DONE) { |
Petri Gynther | 4055eae | 2015-03-25 12:35:16 -0700 | [diff] [blame] | 2642 | tx_ring = &priv->tx_rings[DESC_INDEX]; |
| 2643 | |
| 2644 | if (likely(napi_schedule_prep(&tx_ring->napi))) { |
| 2645 | tx_ring->int_disable(tx_ring); |
Florian Fainelli | dac916f | 2016-04-08 22:30:56 -0700 | [diff] [blame] | 2646 | __napi_schedule_irqoff(&tx_ring->napi); |
Petri Gynther | 4055eae | 2015-03-25 12:35:16 -0700 | [diff] [blame] | 2647 | } |
| 2648 | } |
| 2649 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2650 | if ((priv->hw_params->flags & GENET_HAS_MDIO_INTR) && |
Doug Berger | 07c52d6 | 2017-03-09 16:58:47 -0800 | [diff] [blame] | 2651 | status & (UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR)) { |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2652 | wake_up(&priv->wq); |
| 2653 | } |
| 2654 | |
Doug Berger | 07c52d6 | 2017-03-09 16:58:47 -0800 | [diff] [blame] | 2655 | /* all other interested interrupts handled in bottom half */ |
Doug Berger | 25382b9 | 2019-10-16 16:06:32 -0700 | [diff] [blame] | 2656 | status &= (UMAC_IRQ_LINK_EVENT | UMAC_IRQ_PHY_DET_R); |
Doug Berger | 07c52d6 | 2017-03-09 16:58:47 -0800 | [diff] [blame] | 2657 | if (status) { |
| 2658 | /* Save irq status for bottom-half processing. */ |
| 2659 | spin_lock_irqsave(&priv->lock, flags); |
| 2660 | priv->irq0_stat |= status; |
| 2661 | spin_unlock_irqrestore(&priv->lock, flags); |
| 2662 | |
| 2663 | schedule_work(&priv->bcmgenet_irq_work); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2664 | } |
| 2665 | |
| 2666 | return IRQ_HANDLED; |
| 2667 | } |
| 2668 | |
Florian Fainelli | 8562056 | 2014-07-21 15:29:23 -0700 | [diff] [blame] | 2669 | static irqreturn_t bcmgenet_wol_isr(int irq, void *dev_id) |
| 2670 | { |
| 2671 | struct bcmgenet_priv *priv = dev_id; |
| 2672 | |
| 2673 | pm_wakeup_event(&priv->pdev->dev, 0); |
| 2674 | |
| 2675 | return IRQ_HANDLED; |
| 2676 | } |
| 2677 | |
Florian Fainelli | 4d2e888 | 2015-07-31 11:42:54 -0700 | [diff] [blame] | 2678 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 2679 | static void bcmgenet_poll_controller(struct net_device *dev) |
| 2680 | { |
| 2681 | struct bcmgenet_priv *priv = netdev_priv(dev); |
| 2682 | |
| 2683 | /* Invoke the main RX/TX interrupt handler */ |
| 2684 | disable_irq(priv->irq0); |
| 2685 | bcmgenet_isr0(priv->irq0, priv); |
| 2686 | enable_irq(priv->irq0); |
| 2687 | |
| 2688 | /* And the interrupt handler for RX/TX priority queues */ |
| 2689 | disable_irq(priv->irq1); |
| 2690 | bcmgenet_isr1(priv->irq1, priv); |
| 2691 | enable_irq(priv->irq1); |
| 2692 | } |
| 2693 | #endif |
| 2694 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2695 | static void bcmgenet_umac_reset(struct bcmgenet_priv *priv) |
| 2696 | { |
| 2697 | u32 reg; |
| 2698 | |
| 2699 | reg = bcmgenet_rbuf_ctrl_get(priv); |
| 2700 | reg |= BIT(1); |
| 2701 | bcmgenet_rbuf_ctrl_set(priv, reg); |
| 2702 | udelay(10); |
| 2703 | |
| 2704 | reg &= ~BIT(1); |
| 2705 | bcmgenet_rbuf_ctrl_set(priv, reg); |
| 2706 | udelay(10); |
| 2707 | } |
| 2708 | |
| 2709 | static void bcmgenet_set_hw_addr(struct bcmgenet_priv *priv, |
Florian Fainelli | c91b7f6 | 2014-07-23 10:42:12 -0700 | [diff] [blame] | 2710 | unsigned char *addr) |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2711 | { |
| 2712 | bcmgenet_umac_writel(priv, (addr[0] << 24) | (addr[1] << 16) | |
| 2713 | (addr[2] << 8) | addr[3], UMAC_MAC0); |
| 2714 | bcmgenet_umac_writel(priv, (addr[4] << 8) | addr[5], UMAC_MAC1); |
| 2715 | } |
| 2716 | |
Jeremy Linton | 26bd9cc | 2020-02-24 16:54:02 -0600 | [diff] [blame] | 2717 | static void bcmgenet_get_hw_addr(struct bcmgenet_priv *priv, |
| 2718 | unsigned char *addr) |
| 2719 | { |
| 2720 | u32 addr_tmp; |
| 2721 | |
| 2722 | addr_tmp = bcmgenet_umac_readl(priv, UMAC_MAC0); |
| 2723 | addr[0] = addr_tmp >> 24; |
| 2724 | addr[1] = (addr_tmp >> 16) & 0xff; |
| 2725 | addr[2] = (addr_tmp >> 8) & 0xff; |
| 2726 | addr[3] = addr_tmp & 0xff; |
| 2727 | addr_tmp = bcmgenet_umac_readl(priv, UMAC_MAC1); |
| 2728 | addr[4] = (addr_tmp >> 8) & 0xff; |
| 2729 | addr[5] = addr_tmp & 0xff; |
| 2730 | } |
| 2731 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2732 | /* Returns a reusable dma control register value */ |
| 2733 | static u32 bcmgenet_dma_disable(struct bcmgenet_priv *priv) |
| 2734 | { |
| 2735 | u32 reg; |
| 2736 | u32 dma_ctrl; |
| 2737 | |
| 2738 | /* disable DMA */ |
| 2739 | dma_ctrl = 1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT) | DMA_EN; |
| 2740 | reg = bcmgenet_tdma_readl(priv, DMA_CTRL); |
| 2741 | reg &= ~dma_ctrl; |
| 2742 | bcmgenet_tdma_writel(priv, reg, DMA_CTRL); |
| 2743 | |
| 2744 | reg = bcmgenet_rdma_readl(priv, DMA_CTRL); |
| 2745 | reg &= ~dma_ctrl; |
| 2746 | bcmgenet_rdma_writel(priv, reg, DMA_CTRL); |
| 2747 | |
| 2748 | bcmgenet_umac_writel(priv, 1, UMAC_TX_FLUSH); |
| 2749 | udelay(10); |
| 2750 | bcmgenet_umac_writel(priv, 0, UMAC_TX_FLUSH); |
| 2751 | |
| 2752 | return dma_ctrl; |
| 2753 | } |
| 2754 | |
| 2755 | static void bcmgenet_enable_dma(struct bcmgenet_priv *priv, u32 dma_ctrl) |
| 2756 | { |
| 2757 | u32 reg; |
| 2758 | |
| 2759 | reg = bcmgenet_rdma_readl(priv, DMA_CTRL); |
| 2760 | reg |= dma_ctrl; |
| 2761 | bcmgenet_rdma_writel(priv, reg, DMA_CTRL); |
| 2762 | |
| 2763 | reg = bcmgenet_tdma_readl(priv, DMA_CTRL); |
| 2764 | reg |= dma_ctrl; |
| 2765 | bcmgenet_tdma_writel(priv, reg, DMA_CTRL); |
| 2766 | } |
| 2767 | |
Petri Gynther | 0034de4 | 2015-03-13 14:45:00 -0700 | [diff] [blame] | 2768 | /* bcmgenet_hfb_clear |
| 2769 | * |
| 2770 | * Clear Hardware Filter Block and disable all filtering. |
| 2771 | */ |
| 2772 | static void bcmgenet_hfb_clear(struct bcmgenet_priv *priv) |
| 2773 | { |
| 2774 | u32 i; |
| 2775 | |
| 2776 | bcmgenet_hfb_reg_writel(priv, 0x0, HFB_CTRL); |
| 2777 | bcmgenet_hfb_reg_writel(priv, 0x0, HFB_FLT_ENABLE_V3PLUS); |
| 2778 | bcmgenet_hfb_reg_writel(priv, 0x0, HFB_FLT_ENABLE_V3PLUS + 4); |
| 2779 | |
| 2780 | for (i = DMA_INDEX2RING_0; i <= DMA_INDEX2RING_7; i++) |
| 2781 | bcmgenet_rdma_writel(priv, 0x0, i); |
| 2782 | |
| 2783 | for (i = 0; i < (priv->hw_params->hfb_filter_cnt / 4); i++) |
| 2784 | bcmgenet_hfb_reg_writel(priv, 0x0, |
| 2785 | HFB_FLT_LEN_V3PLUS + i * sizeof(u32)); |
| 2786 | |
| 2787 | for (i = 0; i < priv->hw_params->hfb_filter_cnt * |
| 2788 | priv->hw_params->hfb_filter_size; i++) |
| 2789 | bcmgenet_hfb_writel(priv, 0x0, i * sizeof(u32)); |
| 2790 | } |
| 2791 | |
| 2792 | static void bcmgenet_hfb_init(struct bcmgenet_priv *priv) |
| 2793 | { |
| 2794 | if (GENET_IS_V1(priv) || GENET_IS_V2(priv)) |
| 2795 | return; |
| 2796 | |
| 2797 | bcmgenet_hfb_clear(priv); |
| 2798 | } |
| 2799 | |
Florian Fainelli | 909ff5e | 2014-07-21 15:29:21 -0700 | [diff] [blame] | 2800 | static void bcmgenet_netif_start(struct net_device *dev) |
| 2801 | { |
| 2802 | struct bcmgenet_priv *priv = netdev_priv(dev); |
| 2803 | |
| 2804 | /* Start the network engine */ |
Petri Gynther | 3ab1133 | 2015-03-25 12:35:15 -0700 | [diff] [blame] | 2805 | bcmgenet_enable_rx_napi(priv); |
Florian Fainelli | 909ff5e | 2014-07-21 15:29:21 -0700 | [diff] [blame] | 2806 | |
| 2807 | umac_enable_set(priv, CMD_TX_EN | CMD_RX_EN, true); |
| 2808 | |
Doug Berger | d215dba | 2017-10-25 15:04:16 -0700 | [diff] [blame] | 2809 | bcmgenet_enable_tx_napi(priv); |
Florian Fainelli | 909ff5e | 2014-07-21 15:29:21 -0700 | [diff] [blame] | 2810 | |
Florian Fainelli | 37850e3 | 2015-10-17 14:22:46 -0700 | [diff] [blame] | 2811 | /* Monitor link interrupts now */ |
| 2812 | bcmgenet_link_intr_enable(priv); |
| 2813 | |
Doug Berger | 6c97f01 | 2017-10-25 15:04:19 -0700 | [diff] [blame] | 2814 | phy_start(dev->phydev); |
Florian Fainelli | 909ff5e | 2014-07-21 15:29:21 -0700 | [diff] [blame] | 2815 | } |
| 2816 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2817 | static int bcmgenet_open(struct net_device *dev) |
| 2818 | { |
| 2819 | struct bcmgenet_priv *priv = netdev_priv(dev); |
| 2820 | unsigned long dma_ctrl; |
| 2821 | u32 reg; |
| 2822 | int ret; |
| 2823 | |
| 2824 | netif_dbg(priv, ifup, dev, "bcmgenet_open\n"); |
| 2825 | |
| 2826 | /* Turn on the clock */ |
Florian Fainelli | 7d5d307 | 2015-07-22 17:28:23 -0700 | [diff] [blame] | 2827 | clk_prepare_enable(priv->clk); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2828 | |
Florian Fainelli | a642c4f | 2015-03-23 15:09:56 -0700 | [diff] [blame] | 2829 | /* If this is an internal GPHY, power it back on now, before UniMAC is |
| 2830 | * brought out of reset as absolutely no UniMAC activity is allowed |
| 2831 | */ |
Florian Fainelli | c624f89 | 2015-07-16 15:51:17 -0700 | [diff] [blame] | 2832 | if (priv->internal_phy) |
Florian Fainelli | a642c4f | 2015-03-23 15:09:56 -0700 | [diff] [blame] | 2833 | bcmgenet_power_up(priv, GENET_POWER_PASSIVE); |
| 2834 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2835 | /* take MAC out of reset */ |
| 2836 | bcmgenet_umac_reset(priv); |
| 2837 | |
Doug Berger | 28c2d1a | 2017-10-25 15:04:13 -0700 | [diff] [blame] | 2838 | init_umac(priv); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2839 | |
Doug Berger | 206f54b | 2019-12-17 16:51:12 -0800 | [diff] [blame] | 2840 | /* Apply features again in case we changed them while interface was |
| 2841 | * down |
| 2842 | */ |
| 2843 | bcmgenet_set_features(dev, dev->features); |
| 2844 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2845 | bcmgenet_set_hw_addr(priv, dev->dev_addr); |
| 2846 | |
Florian Fainelli | c624f89 | 2015-07-16 15:51:17 -0700 | [diff] [blame] | 2847 | if (priv->internal_phy) { |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2848 | reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT); |
| 2849 | reg |= EXT_ENERGY_DET_MASK; |
| 2850 | bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT); |
| 2851 | } |
| 2852 | |
| 2853 | /* Disable RX/TX DMA and flush TX queues */ |
| 2854 | dma_ctrl = bcmgenet_dma_disable(priv); |
| 2855 | |
| 2856 | /* Reinitialize TDMA and RDMA and SW housekeeping */ |
| 2857 | ret = bcmgenet_init_dma(priv); |
| 2858 | if (ret) { |
| 2859 | netdev_err(dev, "failed to initialize DMA\n"); |
Doug Berger | 6b6d017f | 2019-11-05 11:07:25 -0800 | [diff] [blame] | 2860 | goto err_clk_disable; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2861 | } |
| 2862 | |
| 2863 | /* Always enable ring 16 - descriptor ring */ |
| 2864 | bcmgenet_enable_dma(priv, dma_ctrl); |
| 2865 | |
Petri Gynther | 0034de4 | 2015-03-13 14:45:00 -0700 | [diff] [blame] | 2866 | /* HFB init */ |
| 2867 | bcmgenet_hfb_init(priv); |
| 2868 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2869 | ret = request_irq(priv->irq0, bcmgenet_isr0, IRQF_SHARED, |
Florian Fainelli | c91b7f6 | 2014-07-23 10:42:12 -0700 | [diff] [blame] | 2870 | dev->name, priv); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2871 | if (ret < 0) { |
| 2872 | netdev_err(dev, "can't request IRQ %d\n", priv->irq0); |
| 2873 | goto err_fini_dma; |
| 2874 | } |
| 2875 | |
| 2876 | ret = request_irq(priv->irq1, bcmgenet_isr1, IRQF_SHARED, |
Florian Fainelli | c91b7f6 | 2014-07-23 10:42:12 -0700 | [diff] [blame] | 2877 | dev->name, priv); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2878 | if (ret < 0) { |
| 2879 | netdev_err(dev, "can't request IRQ %d\n", priv->irq1); |
| 2880 | goto err_irq0; |
| 2881 | } |
| 2882 | |
Doug Berger | 6b6d017f | 2019-11-05 11:07:25 -0800 | [diff] [blame] | 2883 | ret = bcmgenet_mii_probe(dev); |
| 2884 | if (ret) { |
| 2885 | netdev_err(dev, "failed to connect to PHY\n"); |
| 2886 | goto err_irq1; |
| 2887 | } |
| 2888 | |
Florian Fainelli | 909ff5e | 2014-07-21 15:29:21 -0700 | [diff] [blame] | 2889 | bcmgenet_netif_start(dev); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2890 | |
Doug Berger | 09e805d | 2018-11-01 15:55:37 -0700 | [diff] [blame] | 2891 | netif_tx_start_all_queues(dev); |
| 2892 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2893 | return 0; |
| 2894 | |
Doug Berger | 6b6d017f | 2019-11-05 11:07:25 -0800 | [diff] [blame] | 2895 | err_irq1: |
| 2896 | free_irq(priv->irq1, priv); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2897 | err_irq0: |
Florian Fainelli | 978ffac4 | 2015-07-16 15:51:15 -0700 | [diff] [blame] | 2898 | free_irq(priv->irq0, priv); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2899 | err_fini_dma: |
Doug Berger | 4fd6dc9 | 2017-10-25 15:04:12 -0700 | [diff] [blame] | 2900 | bcmgenet_dma_teardown(priv); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2901 | bcmgenet_fini_dma(priv); |
| 2902 | err_clk_disable: |
Doug Berger | 7627409 | 2017-03-09 16:58:46 -0800 | [diff] [blame] | 2903 | if (priv->internal_phy) |
| 2904 | bcmgenet_power_down(priv, GENET_POWER_PASSIVE); |
Florian Fainelli | 7d5d307 | 2015-07-22 17:28:23 -0700 | [diff] [blame] | 2905 | clk_disable_unprepare(priv->clk); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2906 | return ret; |
| 2907 | } |
| 2908 | |
Florian Fainelli | 909ff5e | 2014-07-21 15:29:21 -0700 | [diff] [blame] | 2909 | static void bcmgenet_netif_stop(struct net_device *dev) |
| 2910 | { |
| 2911 | struct bcmgenet_priv *priv = netdev_priv(dev); |
| 2912 | |
Doug Berger | d215dba | 2017-10-25 15:04:16 -0700 | [diff] [blame] | 2913 | bcmgenet_disable_tx_napi(priv); |
Doug Berger | 09e805d | 2018-11-01 15:55:37 -0700 | [diff] [blame] | 2914 | netif_tx_disable(dev); |
Doug Berger | d215dba | 2017-10-25 15:04:16 -0700 | [diff] [blame] | 2915 | |
| 2916 | /* Disable MAC receive */ |
| 2917 | umac_enable_set(priv, CMD_RX_EN, false); |
| 2918 | |
| 2919 | bcmgenet_dma_teardown(priv); |
| 2920 | |
| 2921 | /* Disable MAC transmit. TX DMA disabled must be done before this */ |
| 2922 | umac_enable_set(priv, CMD_TX_EN, false); |
| 2923 | |
Doug Berger | 6c97f01 | 2017-10-25 15:04:19 -0700 | [diff] [blame] | 2924 | phy_stop(dev->phydev); |
Petri Gynther | 3ab1133 | 2015-03-25 12:35:15 -0700 | [diff] [blame] | 2925 | bcmgenet_disable_rx_napi(priv); |
Doug Berger | fbf557d | 2017-10-25 15:04:15 -0700 | [diff] [blame] | 2926 | bcmgenet_intr_disable(priv); |
Florian Fainelli | 909ff5e | 2014-07-21 15:29:21 -0700 | [diff] [blame] | 2927 | |
| 2928 | /* Wait for pending work items to complete. Since interrupts are |
| 2929 | * disabled no new work will be scheduled. |
| 2930 | */ |
| 2931 | cancel_work_sync(&priv->bcmgenet_irq_work); |
Florian Fainelli | cc013fb | 2014-08-11 14:50:43 -0700 | [diff] [blame] | 2932 | |
Florian Fainelli | cc013fb | 2014-08-11 14:50:43 -0700 | [diff] [blame] | 2933 | priv->old_link = -1; |
Petri Gynther | 5ad6e6c | 2014-10-03 12:25:01 -0700 | [diff] [blame] | 2934 | priv->old_speed = -1; |
Florian Fainelli | cc013fb | 2014-08-11 14:50:43 -0700 | [diff] [blame] | 2935 | priv->old_duplex = -1; |
Petri Gynther | 5ad6e6c | 2014-10-03 12:25:01 -0700 | [diff] [blame] | 2936 | priv->old_pause = -1; |
Doug Berger | d215dba | 2017-10-25 15:04:16 -0700 | [diff] [blame] | 2937 | |
| 2938 | /* tx reclaim */ |
| 2939 | bcmgenet_tx_reclaim_all(dev); |
| 2940 | bcmgenet_fini_dma(priv); |
Florian Fainelli | 909ff5e | 2014-07-21 15:29:21 -0700 | [diff] [blame] | 2941 | } |
| 2942 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2943 | static int bcmgenet_close(struct net_device *dev) |
| 2944 | { |
| 2945 | struct bcmgenet_priv *priv = netdev_priv(dev); |
Doug Berger | d215dba | 2017-10-25 15:04:16 -0700 | [diff] [blame] | 2946 | int ret = 0; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2947 | |
| 2948 | netif_dbg(priv, ifdown, dev, "bcmgenet_close\n"); |
| 2949 | |
Florian Fainelli | 909ff5e | 2014-07-21 15:29:21 -0700 | [diff] [blame] | 2950 | bcmgenet_netif_stop(dev); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2951 | |
Florian Fainelli | c96e731 | 2014-11-10 18:06:20 -0800 | [diff] [blame] | 2952 | /* Really kill the PHY state machine and disconnect from it */ |
Doug Berger | 6c97f01 | 2017-10-25 15:04:19 -0700 | [diff] [blame] | 2953 | phy_disconnect(dev->phydev); |
Florian Fainelli | c96e731 | 2014-11-10 18:06:20 -0800 | [diff] [blame] | 2954 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2955 | free_irq(priv->irq0, priv); |
| 2956 | free_irq(priv->irq1, priv); |
| 2957 | |
Florian Fainelli | c624f89 | 2015-07-16 15:51:17 -0700 | [diff] [blame] | 2958 | if (priv->internal_phy) |
Florian Fainelli | ca8cf34 | 2015-03-23 15:09:51 -0700 | [diff] [blame] | 2959 | ret = bcmgenet_power_down(priv, GENET_POWER_PASSIVE); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2960 | |
Florian Fainelli | 7d5d307 | 2015-07-22 17:28:23 -0700 | [diff] [blame] | 2961 | clk_disable_unprepare(priv->clk); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2962 | |
Florian Fainelli | ca8cf34 | 2015-03-23 15:09:51 -0700 | [diff] [blame] | 2963 | return ret; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 2964 | } |
| 2965 | |
Florian Fainelli | 13ea657 | 2015-06-04 16:15:50 -0700 | [diff] [blame] | 2966 | static void bcmgenet_dump_tx_queue(struct bcmgenet_tx_ring *ring) |
| 2967 | { |
| 2968 | struct bcmgenet_priv *priv = ring->priv; |
| 2969 | u32 p_index, c_index, intsts, intmsk; |
| 2970 | struct netdev_queue *txq; |
| 2971 | unsigned int free_bds; |
Florian Fainelli | 13ea657 | 2015-06-04 16:15:50 -0700 | [diff] [blame] | 2972 | bool txq_stopped; |
| 2973 | |
| 2974 | if (!netif_msg_tx_err(priv)) |
| 2975 | return; |
| 2976 | |
| 2977 | txq = netdev_get_tx_queue(priv->dev, ring->queue); |
| 2978 | |
Doug Berger | b0447ec | 2017-10-25 15:04:17 -0700 | [diff] [blame] | 2979 | spin_lock(&ring->lock); |
Florian Fainelli | 13ea657 | 2015-06-04 16:15:50 -0700 | [diff] [blame] | 2980 | if (ring->index == DESC_INDEX) { |
| 2981 | intsts = ~bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_MASK_STATUS); |
| 2982 | intmsk = UMAC_IRQ_TXDMA_DONE | UMAC_IRQ_TXDMA_MBDONE; |
| 2983 | } else { |
| 2984 | intsts = ~bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_MASK_STATUS); |
| 2985 | intmsk = 1 << ring->index; |
| 2986 | } |
| 2987 | c_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_CONS_INDEX); |
| 2988 | p_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_PROD_INDEX); |
| 2989 | txq_stopped = netif_tx_queue_stopped(txq); |
| 2990 | free_bds = ring->free_bds; |
Doug Berger | b0447ec | 2017-10-25 15:04:17 -0700 | [diff] [blame] | 2991 | spin_unlock(&ring->lock); |
Florian Fainelli | 13ea657 | 2015-06-04 16:15:50 -0700 | [diff] [blame] | 2992 | |
| 2993 | netif_err(priv, tx_err, priv->dev, "Ring %d queue %d status summary\n" |
| 2994 | "TX queue status: %s, interrupts: %s\n" |
| 2995 | "(sw)free_bds: %d (sw)size: %d\n" |
| 2996 | "(sw)p_index: %d (hw)p_index: %d\n" |
| 2997 | "(sw)c_index: %d (hw)c_index: %d\n" |
| 2998 | "(sw)clean_p: %d (sw)write_p: %d\n" |
| 2999 | "(sw)cb_ptr: %d (sw)end_ptr: %d\n", |
| 3000 | ring->index, ring->queue, |
| 3001 | txq_stopped ? "stopped" : "active", |
| 3002 | intsts & intmsk ? "enabled" : "disabled", |
| 3003 | free_bds, ring->size, |
| 3004 | ring->prod_index, p_index & DMA_P_INDEX_MASK, |
| 3005 | ring->c_index, c_index & DMA_C_INDEX_MASK, |
| 3006 | ring->clean_ptr, ring->write_ptr, |
| 3007 | ring->cb_ptr, ring->end_ptr); |
| 3008 | } |
| 3009 | |
Michael S. Tsirkin | 0290bd2 | 2019-12-10 09:23:51 -0500 | [diff] [blame] | 3010 | static void bcmgenet_timeout(struct net_device *dev, unsigned int txqueue) |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3011 | { |
| 3012 | struct bcmgenet_priv *priv = netdev_priv(dev); |
Florian Fainelli | 13ea657 | 2015-06-04 16:15:50 -0700 | [diff] [blame] | 3013 | u32 int0_enable = 0; |
| 3014 | u32 int1_enable = 0; |
| 3015 | unsigned int q; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3016 | |
| 3017 | netif_dbg(priv, tx_err, dev, "bcmgenet_timeout\n"); |
| 3018 | |
Florian Fainelli | 13ea657 | 2015-06-04 16:15:50 -0700 | [diff] [blame] | 3019 | for (q = 0; q < priv->hw_params->tx_queues; q++) |
| 3020 | bcmgenet_dump_tx_queue(&priv->tx_rings[q]); |
| 3021 | bcmgenet_dump_tx_queue(&priv->tx_rings[DESC_INDEX]); |
| 3022 | |
| 3023 | bcmgenet_tx_reclaim_all(dev); |
| 3024 | |
| 3025 | for (q = 0; q < priv->hw_params->tx_queues; q++) |
| 3026 | int1_enable |= (1 << q); |
| 3027 | |
| 3028 | int0_enable = UMAC_IRQ_TXDMA_DONE; |
| 3029 | |
| 3030 | /* Re-enable TX interrupts if disabled */ |
| 3031 | bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR); |
| 3032 | bcmgenet_intrl2_1_writel(priv, int1_enable, INTRL2_CPU_MASK_CLEAR); |
| 3033 | |
Florian Westphal | 860e953 | 2016-05-03 16:33:13 +0200 | [diff] [blame] | 3034 | netif_trans_update(dev); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3035 | |
| 3036 | dev->stats.tx_errors++; |
| 3037 | |
| 3038 | netif_tx_wake_all_queues(dev); |
| 3039 | } |
| 3040 | |
Justin Chen | 35cbef9 | 2019-07-17 14:58:53 -0700 | [diff] [blame] | 3041 | #define MAX_MDF_FILTER 17 |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3042 | |
| 3043 | static inline void bcmgenet_set_mdf_addr(struct bcmgenet_priv *priv, |
| 3044 | unsigned char *addr, |
Justin Chen | 35cbef9 | 2019-07-17 14:58:53 -0700 | [diff] [blame] | 3045 | int *i) |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3046 | { |
Florian Fainelli | c91b7f6 | 2014-07-23 10:42:12 -0700 | [diff] [blame] | 3047 | bcmgenet_umac_writel(priv, addr[0] << 8 | addr[1], |
| 3048 | UMAC_MDF_ADDR + (*i * 4)); |
| 3049 | bcmgenet_umac_writel(priv, addr[2] << 24 | addr[3] << 16 | |
| 3050 | addr[4] << 8 | addr[5], |
| 3051 | UMAC_MDF_ADDR + ((*i + 1) * 4)); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3052 | *i += 2; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3053 | } |
| 3054 | |
| 3055 | static void bcmgenet_set_rx_mode(struct net_device *dev) |
| 3056 | { |
| 3057 | struct bcmgenet_priv *priv = netdev_priv(dev); |
| 3058 | struct netdev_hw_addr *ha; |
Justin Chen | 35cbef9 | 2019-07-17 14:58:53 -0700 | [diff] [blame] | 3059 | int i, nfilter; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3060 | u32 reg; |
| 3061 | |
| 3062 | netif_dbg(priv, hw, dev, "%s: %08X\n", __func__, dev->flags); |
| 3063 | |
Justin Chen | 35cbef9 | 2019-07-17 14:58:53 -0700 | [diff] [blame] | 3064 | /* Number of filters needed */ |
| 3065 | nfilter = netdev_uc_count(dev) + netdev_mc_count(dev) + 2; |
| 3066 | |
| 3067 | /* |
| 3068 | * Turn on promicuous mode for three scenarios |
| 3069 | * 1. IFF_PROMISC flag is set |
| 3070 | * 2. IFF_ALLMULTI flag is set |
| 3071 | * 3. The number of filters needed exceeds the number filters |
| 3072 | * supported by the hardware. |
| 3073 | */ |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3074 | reg = bcmgenet_umac_readl(priv, UMAC_CMD); |
Justin Chen | 35cbef9 | 2019-07-17 14:58:53 -0700 | [diff] [blame] | 3075 | if ((dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) || |
| 3076 | (nfilter > MAX_MDF_FILTER)) { |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3077 | reg |= CMD_PROMISC; |
| 3078 | bcmgenet_umac_writel(priv, reg, UMAC_CMD); |
| 3079 | bcmgenet_umac_writel(priv, 0, UMAC_MDF_CTRL); |
| 3080 | return; |
| 3081 | } else { |
| 3082 | reg &= ~CMD_PROMISC; |
| 3083 | bcmgenet_umac_writel(priv, reg, UMAC_CMD); |
| 3084 | } |
| 3085 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3086 | /* update MDF filter */ |
| 3087 | i = 0; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3088 | /* Broadcast */ |
Justin Chen | 35cbef9 | 2019-07-17 14:58:53 -0700 | [diff] [blame] | 3089 | bcmgenet_set_mdf_addr(priv, dev->broadcast, &i); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3090 | /* my own address.*/ |
Justin Chen | 35cbef9 | 2019-07-17 14:58:53 -0700 | [diff] [blame] | 3091 | bcmgenet_set_mdf_addr(priv, dev->dev_addr, &i); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3092 | |
Justin Chen | 35cbef9 | 2019-07-17 14:58:53 -0700 | [diff] [blame] | 3093 | /* Unicast */ |
| 3094 | netdev_for_each_uc_addr(ha, dev) |
| 3095 | bcmgenet_set_mdf_addr(priv, ha->addr, &i); |
| 3096 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3097 | /* Multicast */ |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3098 | netdev_for_each_mc_addr(ha, dev) |
Justin Chen | 35cbef9 | 2019-07-17 14:58:53 -0700 | [diff] [blame] | 3099 | bcmgenet_set_mdf_addr(priv, ha->addr, &i); |
| 3100 | |
| 3101 | /* Enable filters */ |
| 3102 | reg = GENMASK(MAX_MDF_FILTER - 1, MAX_MDF_FILTER - nfilter); |
| 3103 | bcmgenet_umac_writel(priv, reg, UMAC_MDF_CTRL); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3104 | } |
| 3105 | |
| 3106 | /* Set the hardware MAC address. */ |
| 3107 | static int bcmgenet_set_mac_addr(struct net_device *dev, void *p) |
| 3108 | { |
| 3109 | struct sockaddr *addr = p; |
| 3110 | |
| 3111 | /* Setting the MAC address at the hardware level is not possible |
| 3112 | * without disabling the UniMAC RX/TX enable bits. |
| 3113 | */ |
| 3114 | if (netif_running(dev)) |
| 3115 | return -EBUSY; |
| 3116 | |
| 3117 | ether_addr_copy(dev->dev_addr, addr->sa_data); |
| 3118 | |
| 3119 | return 0; |
| 3120 | } |
| 3121 | |
Florian Fainelli | 37a30b4 | 2017-03-16 10:27:08 -0700 | [diff] [blame] | 3122 | static struct net_device_stats *bcmgenet_get_stats(struct net_device *dev) |
| 3123 | { |
| 3124 | struct bcmgenet_priv *priv = netdev_priv(dev); |
| 3125 | unsigned long tx_bytes = 0, tx_packets = 0; |
| 3126 | unsigned long rx_bytes = 0, rx_packets = 0; |
| 3127 | unsigned long rx_errors = 0, rx_dropped = 0; |
| 3128 | struct bcmgenet_tx_ring *tx_ring; |
| 3129 | struct bcmgenet_rx_ring *rx_ring; |
| 3130 | unsigned int q; |
| 3131 | |
| 3132 | for (q = 0; q < priv->hw_params->tx_queues; q++) { |
| 3133 | tx_ring = &priv->tx_rings[q]; |
| 3134 | tx_bytes += tx_ring->bytes; |
| 3135 | tx_packets += tx_ring->packets; |
| 3136 | } |
| 3137 | tx_ring = &priv->tx_rings[DESC_INDEX]; |
| 3138 | tx_bytes += tx_ring->bytes; |
| 3139 | tx_packets += tx_ring->packets; |
| 3140 | |
| 3141 | for (q = 0; q < priv->hw_params->rx_queues; q++) { |
| 3142 | rx_ring = &priv->rx_rings[q]; |
| 3143 | |
| 3144 | rx_bytes += rx_ring->bytes; |
| 3145 | rx_packets += rx_ring->packets; |
| 3146 | rx_errors += rx_ring->errors; |
| 3147 | rx_dropped += rx_ring->dropped; |
| 3148 | } |
| 3149 | rx_ring = &priv->rx_rings[DESC_INDEX]; |
| 3150 | rx_bytes += rx_ring->bytes; |
| 3151 | rx_packets += rx_ring->packets; |
| 3152 | rx_errors += rx_ring->errors; |
| 3153 | rx_dropped += rx_ring->dropped; |
| 3154 | |
| 3155 | dev->stats.tx_bytes = tx_bytes; |
| 3156 | dev->stats.tx_packets = tx_packets; |
| 3157 | dev->stats.rx_bytes = rx_bytes; |
| 3158 | dev->stats.rx_packets = rx_packets; |
| 3159 | dev->stats.rx_errors = rx_errors; |
| 3160 | dev->stats.rx_missed_errors = rx_errors; |
Doug Berger | a6d0b83 | 2020-04-23 15:44:17 -0700 | [diff] [blame^] | 3161 | dev->stats.rx_dropped = rx_dropped; |
Florian Fainelli | 37a30b4 | 2017-03-16 10:27:08 -0700 | [diff] [blame] | 3162 | return &dev->stats; |
| 3163 | } |
| 3164 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3165 | static const struct net_device_ops bcmgenet_netdev_ops = { |
| 3166 | .ndo_open = bcmgenet_open, |
| 3167 | .ndo_stop = bcmgenet_close, |
| 3168 | .ndo_start_xmit = bcmgenet_xmit, |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3169 | .ndo_tx_timeout = bcmgenet_timeout, |
| 3170 | .ndo_set_rx_mode = bcmgenet_set_rx_mode, |
| 3171 | .ndo_set_mac_address = bcmgenet_set_mac_addr, |
Heiner Kallweit | fd786fb1 | 2020-01-21 22:09:33 +0100 | [diff] [blame] | 3172 | .ndo_do_ioctl = phy_do_ioctl_running, |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3173 | .ndo_set_features = bcmgenet_set_features, |
Florian Fainelli | 4d2e888 | 2015-07-31 11:42:54 -0700 | [diff] [blame] | 3174 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 3175 | .ndo_poll_controller = bcmgenet_poll_controller, |
| 3176 | #endif |
Florian Fainelli | 37a30b4 | 2017-03-16 10:27:08 -0700 | [diff] [blame] | 3177 | .ndo_get_stats = bcmgenet_get_stats, |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3178 | }; |
| 3179 | |
| 3180 | /* Array of GENET hardware parameters/characteristics */ |
| 3181 | static struct bcmgenet_hw_params bcmgenet_hw_params[] = { |
| 3182 | [GENET_V1] = { |
| 3183 | .tx_queues = 0, |
Petri Gynther | 51a966a | 2015-02-23 11:00:46 -0800 | [diff] [blame] | 3184 | .tx_bds_per_q = 0, |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3185 | .rx_queues = 0, |
Petri Gynther | 3feafa0 | 2015-03-05 17:40:14 -0800 | [diff] [blame] | 3186 | .rx_bds_per_q = 0, |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3187 | .bp_in_en_shift = 16, |
| 3188 | .bp_in_mask = 0xffff, |
| 3189 | .hfb_filter_cnt = 16, |
| 3190 | .qtag_mask = 0x1F, |
| 3191 | .hfb_offset = 0x1000, |
| 3192 | .rdma_offset = 0x2000, |
| 3193 | .tdma_offset = 0x3000, |
| 3194 | .words_per_bd = 2, |
| 3195 | }, |
| 3196 | [GENET_V2] = { |
| 3197 | .tx_queues = 4, |
Petri Gynther | 51a966a | 2015-02-23 11:00:46 -0800 | [diff] [blame] | 3198 | .tx_bds_per_q = 32, |
Petri Gynther | 7e906e0 | 2015-03-05 17:40:10 -0800 | [diff] [blame] | 3199 | .rx_queues = 0, |
Petri Gynther | 3feafa0 | 2015-03-05 17:40:14 -0800 | [diff] [blame] | 3200 | .rx_bds_per_q = 0, |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3201 | .bp_in_en_shift = 16, |
| 3202 | .bp_in_mask = 0xffff, |
| 3203 | .hfb_filter_cnt = 16, |
| 3204 | .qtag_mask = 0x1F, |
| 3205 | .tbuf_offset = 0x0600, |
| 3206 | .hfb_offset = 0x1000, |
| 3207 | .hfb_reg_offset = 0x2000, |
| 3208 | .rdma_offset = 0x3000, |
| 3209 | .tdma_offset = 0x4000, |
| 3210 | .words_per_bd = 2, |
| 3211 | .flags = GENET_HAS_EXT, |
| 3212 | }, |
| 3213 | [GENET_V3] = { |
| 3214 | .tx_queues = 4, |
Petri Gynther | 51a966a | 2015-02-23 11:00:46 -0800 | [diff] [blame] | 3215 | .tx_bds_per_q = 32, |
Petri Gynther | 7e906e0 | 2015-03-05 17:40:10 -0800 | [diff] [blame] | 3216 | .rx_queues = 0, |
Petri Gynther | 3feafa0 | 2015-03-05 17:40:14 -0800 | [diff] [blame] | 3217 | .rx_bds_per_q = 0, |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3218 | .bp_in_en_shift = 17, |
| 3219 | .bp_in_mask = 0x1ffff, |
| 3220 | .hfb_filter_cnt = 48, |
Petri Gynther | 0034de4 | 2015-03-13 14:45:00 -0700 | [diff] [blame] | 3221 | .hfb_filter_size = 128, |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3222 | .qtag_mask = 0x3F, |
| 3223 | .tbuf_offset = 0x0600, |
| 3224 | .hfb_offset = 0x8000, |
| 3225 | .hfb_reg_offset = 0xfc00, |
| 3226 | .rdma_offset = 0x10000, |
| 3227 | .tdma_offset = 0x11000, |
| 3228 | .words_per_bd = 2, |
Petri Gynther | 8d88c6e | 2015-04-01 00:40:00 -0700 | [diff] [blame] | 3229 | .flags = GENET_HAS_EXT | GENET_HAS_MDIO_INTR | |
| 3230 | GENET_HAS_MOCA_LINK_DET, |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3231 | }, |
| 3232 | [GENET_V4] = { |
| 3233 | .tx_queues = 4, |
Petri Gynther | 51a966a | 2015-02-23 11:00:46 -0800 | [diff] [blame] | 3234 | .tx_bds_per_q = 32, |
Petri Gynther | 7e906e0 | 2015-03-05 17:40:10 -0800 | [diff] [blame] | 3235 | .rx_queues = 0, |
Petri Gynther | 3feafa0 | 2015-03-05 17:40:14 -0800 | [diff] [blame] | 3236 | .rx_bds_per_q = 0, |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3237 | .bp_in_en_shift = 17, |
| 3238 | .bp_in_mask = 0x1ffff, |
| 3239 | .hfb_filter_cnt = 48, |
Petri Gynther | 0034de4 | 2015-03-13 14:45:00 -0700 | [diff] [blame] | 3240 | .hfb_filter_size = 128, |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3241 | .qtag_mask = 0x3F, |
| 3242 | .tbuf_offset = 0x0600, |
| 3243 | .hfb_offset = 0x8000, |
| 3244 | .hfb_reg_offset = 0xfc00, |
| 3245 | .rdma_offset = 0x2000, |
| 3246 | .tdma_offset = 0x4000, |
| 3247 | .words_per_bd = 3, |
Petri Gynther | 8d88c6e | 2015-04-01 00:40:00 -0700 | [diff] [blame] | 3248 | .flags = GENET_HAS_40BITS | GENET_HAS_EXT | |
| 3249 | GENET_HAS_MDIO_INTR | GENET_HAS_MOCA_LINK_DET, |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3250 | }, |
Doug Berger | 4213808 | 2017-03-13 17:41:42 -0700 | [diff] [blame] | 3251 | [GENET_V5] = { |
| 3252 | .tx_queues = 4, |
| 3253 | .tx_bds_per_q = 32, |
| 3254 | .rx_queues = 0, |
| 3255 | .rx_bds_per_q = 0, |
| 3256 | .bp_in_en_shift = 17, |
| 3257 | .bp_in_mask = 0x1ffff, |
| 3258 | .hfb_filter_cnt = 48, |
| 3259 | .hfb_filter_size = 128, |
| 3260 | .qtag_mask = 0x3F, |
| 3261 | .tbuf_offset = 0x0600, |
| 3262 | .hfb_offset = 0x8000, |
| 3263 | .hfb_reg_offset = 0xfc00, |
| 3264 | .rdma_offset = 0x2000, |
| 3265 | .tdma_offset = 0x4000, |
| 3266 | .words_per_bd = 3, |
| 3267 | .flags = GENET_HAS_40BITS | GENET_HAS_EXT | |
| 3268 | GENET_HAS_MDIO_INTR | GENET_HAS_MOCA_LINK_DET, |
| 3269 | }, |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3270 | }; |
| 3271 | |
| 3272 | /* Infer hardware parameters from the detected GENET version */ |
| 3273 | static void bcmgenet_set_hw_params(struct bcmgenet_priv *priv) |
| 3274 | { |
| 3275 | struct bcmgenet_hw_params *params; |
| 3276 | u32 reg; |
| 3277 | u8 major; |
Florian Fainelli | b04a2f5 | 2014-12-03 09:56:59 -0800 | [diff] [blame] | 3278 | u16 gphy_rev; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3279 | |
Doug Berger | 4213808 | 2017-03-13 17:41:42 -0700 | [diff] [blame] | 3280 | if (GENET_IS_V5(priv) || GENET_IS_V4(priv)) { |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3281 | bcmgenet_dma_regs = bcmgenet_dma_regs_v3plus; |
| 3282 | genet_dma_ring_regs = genet_dma_ring_regs_v4; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3283 | } else if (GENET_IS_V3(priv)) { |
| 3284 | bcmgenet_dma_regs = bcmgenet_dma_regs_v3plus; |
| 3285 | genet_dma_ring_regs = genet_dma_ring_regs_v123; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3286 | } else if (GENET_IS_V2(priv)) { |
| 3287 | bcmgenet_dma_regs = bcmgenet_dma_regs_v2; |
| 3288 | genet_dma_ring_regs = genet_dma_ring_regs_v123; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3289 | } else if (GENET_IS_V1(priv)) { |
| 3290 | bcmgenet_dma_regs = bcmgenet_dma_regs_v1; |
| 3291 | genet_dma_ring_regs = genet_dma_ring_regs_v123; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3292 | } |
| 3293 | |
| 3294 | /* enum genet_version starts at 1 */ |
| 3295 | priv->hw_params = &bcmgenet_hw_params[priv->version]; |
| 3296 | params = priv->hw_params; |
| 3297 | |
| 3298 | /* Read GENET HW version */ |
| 3299 | reg = bcmgenet_sys_readl(priv, SYS_REV_CTRL); |
| 3300 | major = (reg >> 24 & 0x0f); |
Doug Berger | 4213808 | 2017-03-13 17:41:42 -0700 | [diff] [blame] | 3301 | if (major == 6) |
| 3302 | major = 5; |
| 3303 | else if (major == 5) |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3304 | major = 4; |
| 3305 | else if (major == 0) |
| 3306 | major = 1; |
| 3307 | if (major != priv->version) { |
| 3308 | dev_err(&priv->pdev->dev, |
| 3309 | "GENET version mismatch, got: %d, configured for: %d\n", |
| 3310 | major, priv->version); |
| 3311 | } |
| 3312 | |
| 3313 | /* Print the GENET core version */ |
| 3314 | dev_info(&priv->pdev->dev, "GENET " GENET_VER_FMT, |
Florian Fainelli | c91b7f6 | 2014-07-23 10:42:12 -0700 | [diff] [blame] | 3315 | major, (reg >> 16) & 0x0f, reg & 0xffff); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3316 | |
Florian Fainelli | 487320c | 2014-09-19 13:07:53 -0700 | [diff] [blame] | 3317 | /* Store the integrated PHY revision for the MDIO probing function |
| 3318 | * to pass this information to the PHY driver. The PHY driver expects |
| 3319 | * to find the PHY major revision in bits 15:8 while the GENET register |
| 3320 | * stores that information in bits 7:0, account for that. |
Florian Fainelli | b04a2f5 | 2014-12-03 09:56:59 -0800 | [diff] [blame] | 3321 | * |
| 3322 | * On newer chips, starting with PHY revision G0, a new scheme is |
| 3323 | * deployed similar to the Starfighter 2 switch with GPHY major |
| 3324 | * revision in bits 15:8 and patch level in bits 7:0. Major revision 0 |
| 3325 | * is reserved as well as special value 0x01ff, we have a small |
| 3326 | * heuristic to check for the new GPHY revision and re-arrange things |
| 3327 | * so the GPHY driver is happy. |
Florian Fainelli | 487320c | 2014-09-19 13:07:53 -0700 | [diff] [blame] | 3328 | */ |
Florian Fainelli | b04a2f5 | 2014-12-03 09:56:59 -0800 | [diff] [blame] | 3329 | gphy_rev = reg & 0xffff; |
| 3330 | |
Doug Berger | 4213808 | 2017-03-13 17:41:42 -0700 | [diff] [blame] | 3331 | if (GENET_IS_V5(priv)) { |
| 3332 | /* The EPHY revision should come from the MDIO registers of |
| 3333 | * the PHY not from GENET. |
| 3334 | */ |
| 3335 | if (gphy_rev != 0) { |
| 3336 | pr_warn("GENET is reporting EPHY revision: 0x%04x\n", |
| 3337 | gphy_rev); |
| 3338 | } |
Doug Berger | eca4bad | 2017-03-09 16:58:45 -0800 | [diff] [blame] | 3339 | /* This is reserved so should require special treatment */ |
David S. Miller | 101c431 | 2017-03-15 11:59:10 -0700 | [diff] [blame] | 3340 | } else if (gphy_rev == 0 || gphy_rev == 0x01ff) { |
Doug Berger | eca4bad | 2017-03-09 16:58:45 -0800 | [diff] [blame] | 3341 | pr_warn("Invalid GPHY revision detected: 0x%04x\n", gphy_rev); |
| 3342 | return; |
Florian Fainelli | b04a2f5 | 2014-12-03 09:56:59 -0800 | [diff] [blame] | 3343 | /* This is the good old scheme, just GPHY major, no minor nor patch */ |
Doug Berger | 4213808 | 2017-03-13 17:41:42 -0700 | [diff] [blame] | 3344 | } else if ((gphy_rev & 0xf0) != 0) { |
Florian Fainelli | b04a2f5 | 2014-12-03 09:56:59 -0800 | [diff] [blame] | 3345 | priv->gphy_rev = gphy_rev << 8; |
Florian Fainelli | b04a2f5 | 2014-12-03 09:56:59 -0800 | [diff] [blame] | 3346 | /* This is the new scheme, GPHY major rolls over with 0x10 = rev G0 */ |
Doug Berger | 4213808 | 2017-03-13 17:41:42 -0700 | [diff] [blame] | 3347 | } else if ((gphy_rev & 0xff00) != 0) { |
Florian Fainelli | b04a2f5 | 2014-12-03 09:56:59 -0800 | [diff] [blame] | 3348 | priv->gphy_rev = gphy_rev; |
Florian Fainelli | b04a2f5 | 2014-12-03 09:56:59 -0800 | [diff] [blame] | 3349 | } |
Florian Fainelli | 487320c | 2014-09-19 13:07:53 -0700 | [diff] [blame] | 3350 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3351 | #ifdef CONFIG_PHYS_ADDR_T_64BIT |
| 3352 | if (!(params->flags & GENET_HAS_40BITS)) |
| 3353 | pr_warn("GENET does not support 40-bits PA\n"); |
| 3354 | #endif |
| 3355 | |
| 3356 | pr_debug("Configuration for version: %d\n" |
Petri Gynther | 3feafa0 | 2015-03-05 17:40:14 -0800 | [diff] [blame] | 3357 | "TXq: %1d, TXqBDs: %1d, RXq: %1d, RXqBDs: %1d\n" |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3358 | "BP << en: %2d, BP msk: 0x%05x\n" |
| 3359 | "HFB count: %2d, QTAQ msk: 0x%05x\n" |
| 3360 | "TBUF: 0x%04x, HFB: 0x%04x, HFBreg: 0x%04x\n" |
| 3361 | "RDMA: 0x%05x, TDMA: 0x%05x\n" |
| 3362 | "Words/BD: %d\n", |
| 3363 | priv->version, |
Petri Gynther | 51a966a | 2015-02-23 11:00:46 -0800 | [diff] [blame] | 3364 | params->tx_queues, params->tx_bds_per_q, |
Petri Gynther | 3feafa0 | 2015-03-05 17:40:14 -0800 | [diff] [blame] | 3365 | params->rx_queues, params->rx_bds_per_q, |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3366 | params->bp_in_en_shift, params->bp_in_mask, |
| 3367 | params->hfb_filter_cnt, params->qtag_mask, |
| 3368 | params->tbuf_offset, params->hfb_offset, |
| 3369 | params->hfb_reg_offset, |
| 3370 | params->rdma_offset, params->tdma_offset, |
| 3371 | params->words_per_bd); |
| 3372 | } |
| 3373 | |
Stefan Wahren | a50e3a9 | 2019-11-11 20:49:23 +0100 | [diff] [blame] | 3374 | struct bcmgenet_plat_data { |
| 3375 | enum bcmgenet_version version; |
| 3376 | u32 dma_max_burst_length; |
| 3377 | }; |
| 3378 | |
| 3379 | static const struct bcmgenet_plat_data v1_plat_data = { |
| 3380 | .version = GENET_V1, |
| 3381 | .dma_max_burst_length = DMA_MAX_BURST_LENGTH, |
| 3382 | }; |
| 3383 | |
| 3384 | static const struct bcmgenet_plat_data v2_plat_data = { |
| 3385 | .version = GENET_V2, |
| 3386 | .dma_max_burst_length = DMA_MAX_BURST_LENGTH, |
| 3387 | }; |
| 3388 | |
| 3389 | static const struct bcmgenet_plat_data v3_plat_data = { |
| 3390 | .version = GENET_V3, |
| 3391 | .dma_max_burst_length = DMA_MAX_BURST_LENGTH, |
| 3392 | }; |
| 3393 | |
| 3394 | static const struct bcmgenet_plat_data v4_plat_data = { |
| 3395 | .version = GENET_V4, |
| 3396 | .dma_max_burst_length = DMA_MAX_BURST_LENGTH, |
| 3397 | }; |
| 3398 | |
| 3399 | static const struct bcmgenet_plat_data v5_plat_data = { |
| 3400 | .version = GENET_V5, |
| 3401 | .dma_max_burst_length = DMA_MAX_BURST_LENGTH, |
| 3402 | }; |
| 3403 | |
| 3404 | static const struct bcmgenet_plat_data bcm2711_plat_data = { |
| 3405 | .version = GENET_V5, |
| 3406 | .dma_max_burst_length = 0x08, |
| 3407 | }; |
| 3408 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3409 | static const struct of_device_id bcmgenet_match[] = { |
Stefan Wahren | a50e3a9 | 2019-11-11 20:49:23 +0100 | [diff] [blame] | 3410 | { .compatible = "brcm,genet-v1", .data = &v1_plat_data }, |
| 3411 | { .compatible = "brcm,genet-v2", .data = &v2_plat_data }, |
| 3412 | { .compatible = "brcm,genet-v3", .data = &v3_plat_data }, |
| 3413 | { .compatible = "brcm,genet-v4", .data = &v4_plat_data }, |
| 3414 | { .compatible = "brcm,genet-v5", .data = &v5_plat_data }, |
| 3415 | { .compatible = "brcm,bcm2711-genet-v5", .data = &bcm2711_plat_data }, |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3416 | { }, |
| 3417 | }; |
Luis de Bethencourt | e8048e5 | 2015-09-18 17:55:02 +0200 | [diff] [blame] | 3418 | MODULE_DEVICE_TABLE(of, bcmgenet_match); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3419 | |
| 3420 | static int bcmgenet_probe(struct platform_device *pdev) |
| 3421 | { |
Petri Gynther | b0ba512 | 2014-12-01 16:18:08 -0800 | [diff] [blame] | 3422 | struct bcmgenet_platform_data *pd = pdev->dev.platform_data; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3423 | struct device_node *dn = pdev->dev.of_node; |
Petri Gynther | b0ba512 | 2014-12-01 16:18:08 -0800 | [diff] [blame] | 3424 | const struct of_device_id *of_id = NULL; |
Stefan Wahren | a50e3a9 | 2019-11-11 20:49:23 +0100 | [diff] [blame] | 3425 | const struct bcmgenet_plat_data *pdata; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3426 | struct bcmgenet_priv *priv; |
| 3427 | struct net_device *dev; |
Florian Fainelli | 5e6ce1f | 2018-03-28 15:15:38 -0700 | [diff] [blame] | 3428 | unsigned int i; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3429 | int err = -EIO; |
| 3430 | |
Petri Gynther | 3feafee | 2015-03-05 17:40:12 -0800 | [diff] [blame] | 3431 | /* Up to GENET_MAX_MQ_CNT + 1 TX queues and RX queues */ |
| 3432 | dev = alloc_etherdev_mqs(sizeof(*priv), GENET_MAX_MQ_CNT + 1, |
| 3433 | GENET_MAX_MQ_CNT + 1); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3434 | if (!dev) { |
| 3435 | dev_err(&pdev->dev, "can't allocate net device\n"); |
| 3436 | return -ENOMEM; |
| 3437 | } |
| 3438 | |
Petri Gynther | b0ba512 | 2014-12-01 16:18:08 -0800 | [diff] [blame] | 3439 | if (dn) { |
| 3440 | of_id = of_match_node(bcmgenet_match, dn); |
| 3441 | if (!of_id) |
| 3442 | return -EINVAL; |
| 3443 | } |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3444 | |
| 3445 | priv = netdev_priv(dev); |
| 3446 | priv->irq0 = platform_get_irq(pdev, 0); |
Stefan Wahren | 2b65f93 | 2019-11-11 20:49:21 +0100 | [diff] [blame] | 3447 | if (priv->irq0 < 0) { |
| 3448 | err = priv->irq0; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3449 | goto err; |
| 3450 | } |
Stefan Wahren | 2b65f93 | 2019-11-11 20:49:21 +0100 | [diff] [blame] | 3451 | priv->irq1 = platform_get_irq(pdev, 1); |
| 3452 | if (priv->irq1 < 0) { |
| 3453 | err = priv->irq1; |
| 3454 | goto err; |
| 3455 | } |
| 3456 | priv->wol_irq = platform_get_irq_optional(pdev, 2); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3457 | |
YueHaibing | 4ca3348 | 2019-08-21 21:41:31 +0800 | [diff] [blame] | 3458 | priv->base = devm_platform_ioremap_resource(pdev, 0); |
Fabio Estevam | 5343a10 | 2014-02-24 00:47:24 -0300 | [diff] [blame] | 3459 | if (IS_ERR(priv->base)) { |
| 3460 | err = PTR_ERR(priv->base); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3461 | goto err; |
| 3462 | } |
| 3463 | |
Doug Berger | 07c52d6 | 2017-03-09 16:58:47 -0800 | [diff] [blame] | 3464 | spin_lock_init(&priv->lock); |
| 3465 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3466 | SET_NETDEV_DEV(dev, &pdev->dev); |
| 3467 | dev_set_drvdata(&pdev->dev, dev); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3468 | dev->watchdog_timeo = 2 * HZ; |
Wilfried Klaebe | 7ad24ea | 2014-05-11 00:12:32 +0000 | [diff] [blame] | 3469 | dev->ethtool_ops = &bcmgenet_ethtool_ops; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3470 | dev->netdev_ops = &bcmgenet_netdev_ops; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3471 | |
| 3472 | priv->msg_enable = netif_msg_init(-1, GENET_MSG_DEFAULT); |
| 3473 | |
Doug Berger | ae895c4 | 2019-12-17 16:51:13 -0800 | [diff] [blame] | 3474 | /* Set default features */ |
| 3475 | dev->features |= NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_HW_CSUM | |
| 3476 | NETIF_F_RXCSUM; |
| 3477 | dev->hw_features |= dev->features; |
| 3478 | dev->vlan_features |= dev->features; |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3479 | |
Florian Fainelli | 8562056 | 2014-07-21 15:29:23 -0700 | [diff] [blame] | 3480 | /* Request the WOL interrupt and advertise suspend if available */ |
| 3481 | priv->wol_irq_disabled = true; |
| 3482 | err = devm_request_irq(&pdev->dev, priv->wol_irq, bcmgenet_wol_isr, 0, |
| 3483 | dev->name, priv); |
| 3484 | if (!err) |
| 3485 | device_set_wakeup_capable(&pdev->dev, 1); |
| 3486 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3487 | /* Set the needed headroom to account for any possible |
| 3488 | * features enabling/disabling at runtime |
| 3489 | */ |
| 3490 | dev->needed_headroom += 64; |
| 3491 | |
| 3492 | netdev_boot_setup_check(dev); |
| 3493 | |
| 3494 | priv->dev = dev; |
| 3495 | priv->pdev = pdev; |
Jeremy Linton | 99c6b06 | 2020-02-24 16:54:01 -0600 | [diff] [blame] | 3496 | |
| 3497 | pdata = device_get_match_data(&pdev->dev); |
| 3498 | if (pdata) { |
Stefan Wahren | a50e3a9 | 2019-11-11 20:49:23 +0100 | [diff] [blame] | 3499 | priv->version = pdata->version; |
| 3500 | priv->dma_max_burst_length = pdata->dma_max_burst_length; |
| 3501 | } else { |
Petri Gynther | b0ba512 | 2014-12-01 16:18:08 -0800 | [diff] [blame] | 3502 | priv->version = pd->genet_version; |
Stefan Wahren | a50e3a9 | 2019-11-11 20:49:23 +0100 | [diff] [blame] | 3503 | priv->dma_max_burst_length = DMA_MAX_BURST_LENGTH; |
| 3504 | } |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3505 | |
Florian Fainelli | e4a60a9 | 2014-08-11 14:50:42 -0700 | [diff] [blame] | 3506 | priv->clk = devm_clk_get(&priv->pdev->dev, "enet"); |
Florian Fainelli | 7d5d307 | 2015-07-22 17:28:23 -0700 | [diff] [blame] | 3507 | if (IS_ERR(priv->clk)) { |
Jeremy Linton | ae200c2 | 2020-02-24 16:54:03 -0600 | [diff] [blame] | 3508 | dev_dbg(&priv->pdev->dev, "failed to get enet clock\n"); |
Florian Fainelli | 7d5d307 | 2015-07-22 17:28:23 -0700 | [diff] [blame] | 3509 | priv->clk = NULL; |
| 3510 | } |
Florian Fainelli | e4a60a9 | 2014-08-11 14:50:42 -0700 | [diff] [blame] | 3511 | |
Florian Fainelli | 7d5d307 | 2015-07-22 17:28:23 -0700 | [diff] [blame] | 3512 | clk_prepare_enable(priv->clk); |
Florian Fainelli | e4a60a9 | 2014-08-11 14:50:42 -0700 | [diff] [blame] | 3513 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3514 | bcmgenet_set_hw_params(priv); |
| 3515 | |
Doug Berger | 99d5563 | 2019-12-17 16:51:08 -0800 | [diff] [blame] | 3516 | err = -EIO; |
| 3517 | if (priv->hw_params->flags & GENET_HAS_40BITS) |
| 3518 | err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(40)); |
| 3519 | if (err) |
| 3520 | err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); |
| 3521 | if (err) |
| 3522 | goto err; |
| 3523 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3524 | /* Mii wait queue */ |
| 3525 | init_waitqueue_head(&priv->wq); |
| 3526 | /* Always use RX_BUF_LENGTH (2KB) buffer for all chips */ |
| 3527 | priv->rx_buf_len = RX_BUF_LENGTH; |
| 3528 | INIT_WORK(&priv->bcmgenet_irq_work, bcmgenet_irq_task); |
| 3529 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3530 | priv->clk_wol = devm_clk_get(&priv->pdev->dev, "enet-wol"); |
Florian Fainelli | 7d5d307 | 2015-07-22 17:28:23 -0700 | [diff] [blame] | 3531 | if (IS_ERR(priv->clk_wol)) { |
Jeremy Linton | ae200c2 | 2020-02-24 16:54:03 -0600 | [diff] [blame] | 3532 | dev_dbg(&priv->pdev->dev, "failed to get enet-wol clock\n"); |
Florian Fainelli | 7d5d307 | 2015-07-22 17:28:23 -0700 | [diff] [blame] | 3533 | priv->clk_wol = NULL; |
| 3534 | } |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3535 | |
Florian Fainelli | 6ef398e | 2014-11-25 21:16:35 -0800 | [diff] [blame] | 3536 | priv->clk_eee = devm_clk_get(&priv->pdev->dev, "enet-eee"); |
| 3537 | if (IS_ERR(priv->clk_eee)) { |
Jeremy Linton | ae200c2 | 2020-02-24 16:54:03 -0600 | [diff] [blame] | 3538 | dev_dbg(&priv->pdev->dev, "failed to get enet-eee clock\n"); |
Florian Fainelli | 6ef398e | 2014-11-25 21:16:35 -0800 | [diff] [blame] | 3539 | priv->clk_eee = NULL; |
| 3540 | } |
| 3541 | |
Doug Berger | 6be371b | 2017-03-09 16:58:48 -0800 | [diff] [blame] | 3542 | /* If this is an internal GPHY, power it on now, before UniMAC is |
| 3543 | * brought out of reset as absolutely no UniMAC activity is allowed |
| 3544 | */ |
Jeremy Linton | 99c6b06 | 2020-02-24 16:54:01 -0600 | [diff] [blame] | 3545 | if (device_get_phy_mode(&pdev->dev) == PHY_INTERFACE_MODE_INTERNAL) |
Doug Berger | 6be371b | 2017-03-09 16:58:48 -0800 | [diff] [blame] | 3546 | bcmgenet_power_up(priv, GENET_POWER_PASSIVE); |
| 3547 | |
Jeremy Linton | 26bd9cc | 2020-02-24 16:54:02 -0600 | [diff] [blame] | 3548 | if ((pd) && (!IS_ERR_OR_NULL(pd->mac_address))) |
| 3549 | ether_addr_copy(dev->dev_addr, pd->mac_address); |
| 3550 | else |
| 3551 | if (!device_get_mac_address(&pdev->dev, dev->dev_addr, ETH_ALEN)) |
| 3552 | if (has_acpi_companion(&pdev->dev)) |
| 3553 | bcmgenet_get_hw_addr(priv, dev->dev_addr); |
| 3554 | |
| 3555 | if (!is_valid_ether_addr(dev->dev_addr)) { |
| 3556 | dev_warn(&pdev->dev, "using random Ethernet MAC\n"); |
| 3557 | eth_hw_addr_random(dev); |
| 3558 | } |
| 3559 | |
Doug Berger | 28c2d1a | 2017-10-25 15:04:13 -0700 | [diff] [blame] | 3560 | reset_umac(priv); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3561 | |
| 3562 | err = bcmgenet_mii_init(dev); |
| 3563 | if (err) |
| 3564 | goto err_clk_disable; |
| 3565 | |
| 3566 | /* setup number of real queues + 1 (GENET_V1 has 0 hardware queues |
| 3567 | * just the ring 16 descriptor based TX |
| 3568 | */ |
| 3569 | netif_set_real_num_tx_queues(priv->dev, priv->hw_params->tx_queues + 1); |
| 3570 | netif_set_real_num_rx_queues(priv->dev, priv->hw_params->rx_queues + 1); |
| 3571 | |
Florian Fainelli | 5e6ce1f | 2018-03-28 15:15:38 -0700 | [diff] [blame] | 3572 | /* Set default coalescing parameters */ |
| 3573 | for (i = 0; i < priv->hw_params->rx_queues; i++) |
| 3574 | priv->rx_rings[i].rx_max_coalesced_frames = 1; |
| 3575 | priv->rx_rings[DESC_INDEX].rx_max_coalesced_frames = 1; |
| 3576 | |
Florian Fainelli | 219575e | 2014-06-26 10:26:21 -0700 | [diff] [blame] | 3577 | /* libphy will determine the link state */ |
| 3578 | netif_carrier_off(dev); |
| 3579 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3580 | /* Turn off the main clock, WOL clock is handled separately */ |
Florian Fainelli | 7d5d307 | 2015-07-22 17:28:23 -0700 | [diff] [blame] | 3581 | clk_disable_unprepare(priv->clk); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3582 | |
Florian Fainelli | 0f50ce9 | 2014-06-26 10:26:20 -0700 | [diff] [blame] | 3583 | err = register_netdev(dev); |
| 3584 | if (err) |
| 3585 | goto err; |
| 3586 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3587 | return err; |
| 3588 | |
| 3589 | err_clk_disable: |
Florian Fainelli | 7d5d307 | 2015-07-22 17:28:23 -0700 | [diff] [blame] | 3590 | clk_disable_unprepare(priv->clk); |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3591 | err: |
| 3592 | free_netdev(dev); |
| 3593 | return err; |
| 3594 | } |
| 3595 | |
| 3596 | static int bcmgenet_remove(struct platform_device *pdev) |
| 3597 | { |
| 3598 | struct bcmgenet_priv *priv = dev_to_priv(&pdev->dev); |
| 3599 | |
| 3600 | dev_set_drvdata(&pdev->dev, NULL); |
| 3601 | unregister_netdev(priv->dev); |
| 3602 | bcmgenet_mii_exit(priv->dev); |
| 3603 | free_netdev(priv->dev); |
| 3604 | |
| 3605 | return 0; |
| 3606 | } |
| 3607 | |
Florian Fainelli | d9f45ab | 2019-10-15 10:36:24 -0700 | [diff] [blame] | 3608 | static void bcmgenet_shutdown(struct platform_device *pdev) |
| 3609 | { |
| 3610 | bcmgenet_remove(pdev); |
| 3611 | } |
| 3612 | |
Florian Fainelli | b6e978e | 2014-07-21 15:29:22 -0700 | [diff] [blame] | 3613 | #ifdef CONFIG_PM_SLEEP |
Florian Fainelli | b6e978e | 2014-07-21 15:29:22 -0700 | [diff] [blame] | 3614 | static int bcmgenet_resume(struct device *d) |
| 3615 | { |
| 3616 | struct net_device *dev = dev_get_drvdata(d); |
| 3617 | struct bcmgenet_priv *priv = netdev_priv(dev); |
| 3618 | unsigned long dma_ctrl; |
| 3619 | int ret; |
| 3620 | u32 reg; |
| 3621 | |
| 3622 | if (!netif_running(dev)) |
| 3623 | return 0; |
| 3624 | |
| 3625 | /* Turn on the clock */ |
| 3626 | ret = clk_prepare_enable(priv->clk); |
| 3627 | if (ret) |
| 3628 | return ret; |
| 3629 | |
Florian Fainelli | a6f31f5 | 2015-03-23 15:09:57 -0700 | [diff] [blame] | 3630 | /* If this is an internal GPHY, power it back on now, before UniMAC is |
| 3631 | * brought out of reset as absolutely no UniMAC activity is allowed |
| 3632 | */ |
Florian Fainelli | c624f89 | 2015-07-16 15:51:17 -0700 | [diff] [blame] | 3633 | if (priv->internal_phy) |
Florian Fainelli | a6f31f5 | 2015-03-23 15:09:57 -0700 | [diff] [blame] | 3634 | bcmgenet_power_up(priv, GENET_POWER_PASSIVE); |
| 3635 | |
Florian Fainelli | b6e978e | 2014-07-21 15:29:22 -0700 | [diff] [blame] | 3636 | bcmgenet_umac_reset(priv); |
| 3637 | |
Doug Berger | 28c2d1a | 2017-10-25 15:04:13 -0700 | [diff] [blame] | 3638 | init_umac(priv); |
Florian Fainelli | b6e978e | 2014-07-21 15:29:22 -0700 | [diff] [blame] | 3639 | |
Tobias Klauser | 0a29b3d | 2014-09-23 15:19:41 +0200 | [diff] [blame] | 3640 | /* From WOL-enabled suspend, switch to regular clock */ |
| 3641 | if (priv->wolopts) |
| 3642 | clk_disable_unprepare(priv->clk_wol); |
| 3643 | |
Doug Berger | 6b6d017f | 2019-11-05 11:07:25 -0800 | [diff] [blame] | 3644 | phy_init_hw(dev->phydev); |
| 3645 | |
Tobias Klauser | 0a29b3d | 2014-09-23 15:19:41 +0200 | [diff] [blame] | 3646 | /* Speed settings must be restored */ |
Doug Berger | 0686bd9 | 2019-11-05 11:07:26 -0800 | [diff] [blame] | 3647 | genphy_config_aneg(dev->phydev); |
Florian Fainelli | 00d5109 | 2017-07-31 11:05:32 -0700 | [diff] [blame] | 3648 | bcmgenet_mii_config(priv->dev, false); |
Florian Fainelli | 8c90db7 | 2014-07-21 15:29:28 -0700 | [diff] [blame] | 3649 | |
Doug Berger | 206f54b | 2019-12-17 16:51:12 -0800 | [diff] [blame] | 3650 | /* Restore enabled features */ |
| 3651 | bcmgenet_set_features(dev, dev->features); |
| 3652 | |
Florian Fainelli | b6e978e | 2014-07-21 15:29:22 -0700 | [diff] [blame] | 3653 | bcmgenet_set_hw_addr(priv, dev->dev_addr); |
| 3654 | |
Florian Fainelli | c624f89 | 2015-07-16 15:51:17 -0700 | [diff] [blame] | 3655 | if (priv->internal_phy) { |
Florian Fainelli | b6e978e | 2014-07-21 15:29:22 -0700 | [diff] [blame] | 3656 | reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT); |
| 3657 | reg |= EXT_ENERGY_DET_MASK; |
| 3658 | bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT); |
| 3659 | } |
| 3660 | |
Florian Fainelli | 98bb739 | 2014-08-11 14:50:45 -0700 | [diff] [blame] | 3661 | if (priv->wolopts) |
| 3662 | bcmgenet_power_up(priv, GENET_POWER_WOL_MAGIC); |
| 3663 | |
Florian Fainelli | b6e978e | 2014-07-21 15:29:22 -0700 | [diff] [blame] | 3664 | /* Disable RX/TX DMA and flush TX queues */ |
| 3665 | dma_ctrl = bcmgenet_dma_disable(priv); |
| 3666 | |
| 3667 | /* Reinitialize TDMA and RDMA and SW housekeeping */ |
| 3668 | ret = bcmgenet_init_dma(priv); |
| 3669 | if (ret) { |
| 3670 | netdev_err(dev, "failed to initialize DMA\n"); |
| 3671 | goto out_clk_disable; |
| 3672 | } |
| 3673 | |
| 3674 | /* Always enable ring 16 - descriptor ring */ |
| 3675 | bcmgenet_enable_dma(priv, dma_ctrl); |
| 3676 | |
Florian Fainelli | 5371bbf4 | 2017-03-15 12:57:21 -0700 | [diff] [blame] | 3677 | if (!device_may_wakeup(d)) |
Doug Berger | 6c97f01 | 2017-10-25 15:04:19 -0700 | [diff] [blame] | 3678 | phy_resume(dev->phydev); |
Florian Fainelli | cc013fb | 2014-08-11 14:50:43 -0700 | [diff] [blame] | 3679 | |
Florian Fainelli | 6ef398e | 2014-11-25 21:16:35 -0800 | [diff] [blame] | 3680 | if (priv->eee.eee_enabled) |
| 3681 | bcmgenet_eee_enable_set(dev, true); |
| 3682 | |
Florian Fainelli | b6e978e | 2014-07-21 15:29:22 -0700 | [diff] [blame] | 3683 | bcmgenet_netif_start(dev); |
| 3684 | |
Doug Berger | 09e805d | 2018-11-01 15:55:37 -0700 | [diff] [blame] | 3685 | netif_device_attach(dev); |
| 3686 | |
Florian Fainelli | b6e978e | 2014-07-21 15:29:22 -0700 | [diff] [blame] | 3687 | return 0; |
| 3688 | |
| 3689 | out_clk_disable: |
Doug Berger | 7627409 | 2017-03-09 16:58:46 -0800 | [diff] [blame] | 3690 | if (priv->internal_phy) |
| 3691 | bcmgenet_power_down(priv, GENET_POWER_PASSIVE); |
Florian Fainelli | b6e978e | 2014-07-21 15:29:22 -0700 | [diff] [blame] | 3692 | clk_disable_unprepare(priv->clk); |
| 3693 | return ret; |
| 3694 | } |
Doug Berger | a94cbf0 | 2018-11-16 18:00:21 -0800 | [diff] [blame] | 3695 | |
| 3696 | static int bcmgenet_suspend(struct device *d) |
| 3697 | { |
| 3698 | struct net_device *dev = dev_get_drvdata(d); |
| 3699 | struct bcmgenet_priv *priv = netdev_priv(dev); |
| 3700 | int ret = 0; |
| 3701 | |
| 3702 | if (!netif_running(dev)) |
| 3703 | return 0; |
| 3704 | |
| 3705 | netif_device_detach(dev); |
| 3706 | |
| 3707 | bcmgenet_netif_stop(dev); |
| 3708 | |
| 3709 | if (!device_may_wakeup(d)) |
| 3710 | phy_suspend(dev->phydev); |
| 3711 | |
| 3712 | /* Prepare the device for Wake-on-LAN and switch to the slow clock */ |
| 3713 | if (device_may_wakeup(d) && priv->wolopts) { |
| 3714 | ret = bcmgenet_power_down(priv, GENET_POWER_WOL_MAGIC); |
| 3715 | clk_prepare_enable(priv->clk_wol); |
| 3716 | } else if (priv->internal_phy) { |
| 3717 | ret = bcmgenet_power_down(priv, GENET_POWER_PASSIVE); |
| 3718 | } |
| 3719 | |
| 3720 | /* Turn off the clocks */ |
| 3721 | clk_disable_unprepare(priv->clk); |
| 3722 | |
Doug Berger | c5a54bb | 2018-11-16 18:00:22 -0800 | [diff] [blame] | 3723 | if (ret) |
| 3724 | bcmgenet_resume(d); |
| 3725 | |
Doug Berger | a94cbf0 | 2018-11-16 18:00:21 -0800 | [diff] [blame] | 3726 | return ret; |
| 3727 | } |
Florian Fainelli | b6e978e | 2014-07-21 15:29:22 -0700 | [diff] [blame] | 3728 | #endif /* CONFIG_PM_SLEEP */ |
| 3729 | |
| 3730 | static SIMPLE_DEV_PM_OPS(bcmgenet_pm_ops, bcmgenet_suspend, bcmgenet_resume); |
| 3731 | |
Jeremy Linton | 99c6b06 | 2020-02-24 16:54:01 -0600 | [diff] [blame] | 3732 | static const struct acpi_device_id genet_acpi_match[] = { |
| 3733 | { "BCM6E4E", (kernel_ulong_t)&bcm2711_plat_data }, |
| 3734 | { }, |
| 3735 | }; |
| 3736 | MODULE_DEVICE_TABLE(acpi, genet_acpi_match); |
| 3737 | |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3738 | static struct platform_driver bcmgenet_driver = { |
| 3739 | .probe = bcmgenet_probe, |
| 3740 | .remove = bcmgenet_remove, |
Florian Fainelli | d9f45ab | 2019-10-15 10:36:24 -0700 | [diff] [blame] | 3741 | .shutdown = bcmgenet_shutdown, |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3742 | .driver = { |
| 3743 | .name = "bcmgenet", |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3744 | .of_match_table = bcmgenet_match, |
Florian Fainelli | b6e978e | 2014-07-21 15:29:22 -0700 | [diff] [blame] | 3745 | .pm = &bcmgenet_pm_ops, |
Jeremy Linton | 99c6b06 | 2020-02-24 16:54:01 -0600 | [diff] [blame] | 3746 | .acpi_match_table = ACPI_PTR(genet_acpi_match), |
Florian Fainelli | 1c1008c | 2014-02-13 16:08:47 -0800 | [diff] [blame] | 3747 | }, |
| 3748 | }; |
| 3749 | module_platform_driver(bcmgenet_driver); |
| 3750 | |
| 3751 | MODULE_AUTHOR("Broadcom Corporation"); |
| 3752 | MODULE_DESCRIPTION("Broadcom GENET Ethernet controller driver"); |
| 3753 | MODULE_ALIAS("platform:bcmgenet"); |
| 3754 | MODULE_LICENSE("GPL"); |