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