Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2 | /* |
Jamie Iles | f75ba50 | 2011-11-08 10:12:32 +0000 | [diff] [blame] | 3 | * Cadence MACB/GEM Ethernet Controller driver |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2004-2006 Atmel Corporation |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 8 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 9 | #include <linux/clk.h> |
Yash Shah | c218ad5 | 2019-06-18 13:26:08 +0530 | [diff] [blame] | 10 | #include <linux/clk-provider.h> |
Claudiu Beznea | 653e92a | 2018-08-07 12:25:14 +0300 | [diff] [blame] | 11 | #include <linux/crc32.h> |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 12 | #include <linux/module.h> |
| 13 | #include <linux/moduleparam.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/types.h> |
Nicolas Ferre | 909a858 | 2012-11-19 06:00:21 +0000 | [diff] [blame] | 16 | #include <linux/circ_buf.h> |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 17 | #include <linux/slab.h> |
| 18 | #include <linux/init.h> |
Soren Brinkmann | 60fe716 | 2013-12-10 16:07:21 -0800 | [diff] [blame] | 19 | #include <linux/io.h> |
Joachim Eastwood | 2dbfdbb9 | 2012-11-11 13:56:27 +0000 | [diff] [blame] | 20 | #include <linux/gpio.h> |
Gregory CLEMENT | 270c499 | 2015-12-17 10:51:04 +0100 | [diff] [blame] | 21 | #include <linux/gpio/consumer.h> |
Alexey Dobriyan | a6b7a40 | 2011-06-06 10:43:46 +0000 | [diff] [blame] | 22 | #include <linux/interrupt.h> |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 23 | #include <linux/netdevice.h> |
| 24 | #include <linux/etherdevice.h> |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 25 | #include <linux/dma-mapping.h> |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 26 | #include <linux/platform_device.h> |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 27 | #include <linux/phylink.h> |
Olof Johansson | b17471f | 2011-12-20 13:13:07 -0800 | [diff] [blame] | 28 | #include <linux/of.h> |
Jean-Christophe PLAGNIOL-VILLARD | fb97a84 | 2011-11-18 15:29:25 +0100 | [diff] [blame] | 29 | #include <linux/of_device.h> |
Gregory CLEMENT | 270c499 | 2015-12-17 10:51:04 +0100 | [diff] [blame] | 30 | #include <linux/of_gpio.h> |
Boris BREZILLON | 148cbb5 | 2013-08-22 17:57:28 +0200 | [diff] [blame] | 31 | #include <linux/of_mdio.h> |
Jean-Christophe PLAGNIOL-VILLARD | fb97a84 | 2011-11-18 15:29:25 +0100 | [diff] [blame] | 32 | #include <linux/of_net.h> |
Rafal Ozieblo | 1629dd4 | 2016-11-16 10:02:34 +0000 | [diff] [blame] | 33 | #include <linux/ip.h> |
| 34 | #include <linux/udp.h> |
| 35 | #include <linux/tcp.h> |
Harini Katakam | 8beb79b | 2019-03-01 16:20:32 +0530 | [diff] [blame] | 36 | #include <linux/iopoll.h> |
Harini Katakam | d54f89a | 2019-03-01 16:20:34 +0530 | [diff] [blame] | 37 | #include <linux/pm_runtime.h> |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 38 | #include "macb.h" |
| 39 | |
Yash Shah | c218ad5 | 2019-06-18 13:26:08 +0530 | [diff] [blame] | 40 | /* This structure is only used for MACB on SiFive FU540 devices */ |
| 41 | struct sifive_fu540_macb_mgmt { |
| 42 | void __iomem *reg; |
| 43 | unsigned long rate; |
| 44 | struct clk_hw hw; |
| 45 | }; |
| 46 | |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 47 | #define MACB_RX_BUFFER_SIZE 128 |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 48 | #define RX_BUFFER_MULTIPLE 64 /* bytes */ |
Zach Brown | 8441bb3 | 2016-10-19 09:56:58 -0500 | [diff] [blame] | 49 | |
Zach Brown | b410d13 | 2016-10-19 09:56:57 -0500 | [diff] [blame] | 50 | #define DEFAULT_RX_RING_SIZE 512 /* must be power of 2 */ |
Zach Brown | 8441bb3 | 2016-10-19 09:56:58 -0500 | [diff] [blame] | 51 | #define MIN_RX_RING_SIZE 64 |
| 52 | #define MAX_RX_RING_SIZE 8192 |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 53 | #define RX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \ |
Zach Brown | b410d13 | 2016-10-19 09:56:57 -0500 | [diff] [blame] | 54 | * (bp)->rx_ring_size) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 55 | |
Zach Brown | b410d13 | 2016-10-19 09:56:57 -0500 | [diff] [blame] | 56 | #define DEFAULT_TX_RING_SIZE 512 /* must be power of 2 */ |
Zach Brown | 8441bb3 | 2016-10-19 09:56:58 -0500 | [diff] [blame] | 57 | #define MIN_TX_RING_SIZE 64 |
| 58 | #define MAX_TX_RING_SIZE 4096 |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 59 | #define TX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \ |
Zach Brown | b410d13 | 2016-10-19 09:56:57 -0500 | [diff] [blame] | 60 | * (bp)->tx_ring_size) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 61 | |
Nicolas Ferre | 909a858 | 2012-11-19 06:00:21 +0000 | [diff] [blame] | 62 | /* level of occupied TX descriptors under which we wake up TX process */ |
Zach Brown | b410d13 | 2016-10-19 09:56:57 -0500 | [diff] [blame] | 63 | #define MACB_TX_WAKEUP_THRESH(bp) (3 * (bp)->tx_ring_size / 4) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 64 | |
Harini Katakam | e501070 | 2019-01-29 15:20:03 +0530 | [diff] [blame] | 65 | #define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(ISR_ROVR)) |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 66 | #define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \ |
| 67 | | MACB_BIT(ISR_RLE) \ |
| 68 | | MACB_BIT(TXERR)) |
Claudiu Beznea | 4298388 | 2018-12-17 10:02:42 +0000 | [diff] [blame] | 69 | #define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP) \ |
| 70 | | MACB_BIT(TXUBR)) |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 71 | |
Rafal Ozieblo | 1629dd4 | 2016-11-16 10:02:34 +0000 | [diff] [blame] | 72 | /* Max length of transmit frame must be a multiple of 8 bytes */ |
| 73 | #define MACB_TX_LEN_ALIGN 8 |
| 74 | #define MACB_MAX_TX_LEN ((unsigned int)((1 << MACB_TX_FRMLEN_SIZE) - 1) & ~((unsigned int)(MACB_TX_LEN_ALIGN - 1))) |
Harini Katakam | f822e9c | 2020-02-05 18:08:12 +0530 | [diff] [blame] | 75 | /* Limit maximum TX length as per Cadence TSO errata. This is to avoid a |
| 76 | * false amba_error in TX path from the DMA assuming there is not enough |
| 77 | * space in the SRAM (16KB) even when there is. |
| 78 | */ |
| 79 | #define GEM_MAX_TX_LEN (unsigned int)(0x3FC0) |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 80 | |
Jarod Wilson | 44770e1 | 2016-10-17 15:54:17 -0400 | [diff] [blame] | 81 | #define GEM_MTU_MIN_SIZE ETH_MIN_MTU |
David S. Miller | f9c45ae | 2017-07-03 06:31:05 -0700 | [diff] [blame] | 82 | #define MACB_NETIF_LSO NETIF_F_TSO |
Harini Katakam | a5898ea | 2015-05-06 22:27:18 +0530 | [diff] [blame] | 83 | |
Sergio Prado | 3e2a5e1 | 2016-02-09 12:07:16 -0200 | [diff] [blame] | 84 | #define MACB_WOL_HAS_MAGIC_PACKET (0x1 << 0) |
| 85 | #define MACB_WOL_ENABLED (0x1 << 1) |
| 86 | |
Parshuram Thombare | e4e143e | 2020-10-29 13:47:07 +0100 | [diff] [blame] | 87 | #define HS_SPEED_10000M 4 |
| 88 | #define MACB_SERDES_RATE_10G 1 |
| 89 | |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 90 | /* Graceful stop timeouts in us. We should allow up to |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 91 | * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions) |
| 92 | */ |
| 93 | #define MACB_HALT_TIMEOUT 1230 |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 94 | |
Harini Katakam | d54f89a | 2019-03-01 16:20:34 +0530 | [diff] [blame] | 95 | #define MACB_PM_TIMEOUT 100 /* ms */ |
| 96 | |
Harini Katakam | 8beb79b | 2019-03-01 16:20:32 +0530 | [diff] [blame] | 97 | #define MACB_MDIO_TIMEOUT 1000000 /* in usecs */ |
| 98 | |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 99 | /* DMA buffer descriptor might be different size |
Rafal Ozieblo | 7b42961 | 2017-06-29 07:12:51 +0100 | [diff] [blame] | 100 | * depends on hardware configuration: |
| 101 | * |
| 102 | * 1. dma address width 32 bits: |
| 103 | * word 1: 32 bit address of Data Buffer |
| 104 | * word 2: control |
| 105 | * |
| 106 | * 2. dma address width 64 bits: |
| 107 | * word 1: 32 bit address of Data Buffer |
| 108 | * word 2: control |
| 109 | * word 3: upper 32 bit address of Data Buffer |
| 110 | * word 4: unused |
| 111 | * |
| 112 | * 3. dma address width 32 bits with hardware timestamping: |
| 113 | * word 1: 32 bit address of Data Buffer |
| 114 | * word 2: control |
| 115 | * word 3: timestamp word 1 |
| 116 | * word 4: timestamp word 2 |
| 117 | * |
| 118 | * 4. dma address width 64 bits with hardware timestamping: |
| 119 | * word 1: 32 bit address of Data Buffer |
| 120 | * word 2: control |
| 121 | * word 3: upper 32 bit address of Data Buffer |
| 122 | * word 4: unused |
| 123 | * word 5: timestamp word 1 |
| 124 | * word 6: timestamp word 2 |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 125 | */ |
| 126 | static unsigned int macb_dma_desc_get_size(struct macb *bp) |
| 127 | { |
Rafal Ozieblo | 7b42961 | 2017-06-29 07:12:51 +0100 | [diff] [blame] | 128 | #ifdef MACB_EXT_DESC |
| 129 | unsigned int desc_size; |
| 130 | |
| 131 | switch (bp->hw_dma_cap) { |
| 132 | case HW_DMA_CAP_64B: |
| 133 | desc_size = sizeof(struct macb_dma_desc) |
| 134 | + sizeof(struct macb_dma_desc_64); |
| 135 | break; |
| 136 | case HW_DMA_CAP_PTP: |
| 137 | desc_size = sizeof(struct macb_dma_desc) |
| 138 | + sizeof(struct macb_dma_desc_ptp); |
| 139 | break; |
| 140 | case HW_DMA_CAP_64B_PTP: |
| 141 | desc_size = sizeof(struct macb_dma_desc) |
| 142 | + sizeof(struct macb_dma_desc_64) |
| 143 | + sizeof(struct macb_dma_desc_ptp); |
| 144 | break; |
| 145 | default: |
| 146 | desc_size = sizeof(struct macb_dma_desc); |
| 147 | } |
| 148 | return desc_size; |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 149 | #endif |
| 150 | return sizeof(struct macb_dma_desc); |
| 151 | } |
| 152 | |
Rafal Ozieblo | 7b42961 | 2017-06-29 07:12:51 +0100 | [diff] [blame] | 153 | static unsigned int macb_adj_dma_desc_idx(struct macb *bp, unsigned int desc_idx) |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 154 | { |
Rafal Ozieblo | 7b42961 | 2017-06-29 07:12:51 +0100 | [diff] [blame] | 155 | #ifdef MACB_EXT_DESC |
| 156 | switch (bp->hw_dma_cap) { |
| 157 | case HW_DMA_CAP_64B: |
| 158 | case HW_DMA_CAP_PTP: |
| 159 | desc_idx <<= 1; |
| 160 | break; |
| 161 | case HW_DMA_CAP_64B_PTP: |
| 162 | desc_idx *= 3; |
| 163 | break; |
| 164 | default: |
| 165 | break; |
| 166 | } |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 167 | #endif |
Rafal Ozieblo | 7b42961 | 2017-06-29 07:12:51 +0100 | [diff] [blame] | 168 | return desc_idx; |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT |
| 172 | static struct macb_dma_desc_64 *macb_64b_desc(struct macb *bp, struct macb_dma_desc *desc) |
| 173 | { |
Shubhrajyoti Datta | 99dcb84 | 2019-09-23 14:03:51 +0530 | [diff] [blame] | 174 | return (struct macb_dma_desc_64 *)((void *)desc |
| 175 | + sizeof(struct macb_dma_desc)); |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 176 | } |
| 177 | #endif |
| 178 | |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 179 | /* Ring buffer accessors */ |
Zach Brown | b410d13 | 2016-10-19 09:56:57 -0500 | [diff] [blame] | 180 | static unsigned int macb_tx_ring_wrap(struct macb *bp, unsigned int index) |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 181 | { |
Zach Brown | b410d13 | 2016-10-19 09:56:57 -0500 | [diff] [blame] | 182 | return index & (bp->tx_ring_size - 1); |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 185 | static struct macb_dma_desc *macb_tx_desc(struct macb_queue *queue, |
| 186 | unsigned int index) |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 187 | { |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 188 | index = macb_tx_ring_wrap(queue->bp, index); |
| 189 | index = macb_adj_dma_desc_idx(queue->bp, index); |
| 190 | return &queue->tx_ring[index]; |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 193 | static struct macb_tx_skb *macb_tx_skb(struct macb_queue *queue, |
| 194 | unsigned int index) |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 195 | { |
Zach Brown | b410d13 | 2016-10-19 09:56:57 -0500 | [diff] [blame] | 196 | return &queue->tx_skb[macb_tx_ring_wrap(queue->bp, index)]; |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 199 | static dma_addr_t macb_tx_dma(struct macb_queue *queue, unsigned int index) |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 200 | { |
| 201 | dma_addr_t offset; |
| 202 | |
Zach Brown | b410d13 | 2016-10-19 09:56:57 -0500 | [diff] [blame] | 203 | offset = macb_tx_ring_wrap(queue->bp, index) * |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 204 | macb_dma_desc_get_size(queue->bp); |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 205 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 206 | return queue->tx_ring_dma + offset; |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Zach Brown | b410d13 | 2016-10-19 09:56:57 -0500 | [diff] [blame] | 209 | static unsigned int macb_rx_ring_wrap(struct macb *bp, unsigned int index) |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 210 | { |
Zach Brown | b410d13 | 2016-10-19 09:56:57 -0500 | [diff] [blame] | 211 | return index & (bp->rx_ring_size - 1); |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 214 | static struct macb_dma_desc *macb_rx_desc(struct macb_queue *queue, unsigned int index) |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 215 | { |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 216 | index = macb_rx_ring_wrap(queue->bp, index); |
| 217 | index = macb_adj_dma_desc_idx(queue->bp, index); |
| 218 | return &queue->rx_ring[index]; |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 221 | static void *macb_rx_buffer(struct macb_queue *queue, unsigned int index) |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 222 | { |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 223 | return queue->rx_buffers + queue->bp->rx_buffer_size * |
| 224 | macb_rx_ring_wrap(queue->bp, index); |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Andy Shevchenko | f2ce8a9e | 2015-07-24 21:23:59 +0300 | [diff] [blame] | 227 | /* I/O accessors */ |
| 228 | static u32 hw_readl_native(struct macb *bp, int offset) |
| 229 | { |
| 230 | return __raw_readl(bp->regs + offset); |
| 231 | } |
| 232 | |
| 233 | static void hw_writel_native(struct macb *bp, int offset, u32 value) |
| 234 | { |
| 235 | __raw_writel(value, bp->regs + offset); |
| 236 | } |
| 237 | |
| 238 | static u32 hw_readl(struct macb *bp, int offset) |
| 239 | { |
| 240 | return readl_relaxed(bp->regs + offset); |
| 241 | } |
| 242 | |
| 243 | static void hw_writel(struct macb *bp, int offset, u32 value) |
| 244 | { |
| 245 | writel_relaxed(value, bp->regs + offset); |
| 246 | } |
| 247 | |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 248 | /* Find the CPU endianness by using the loopback bit of NCR register. When the |
Moritz Fischer | 88023be | 2016-03-29 19:11:15 -0700 | [diff] [blame] | 249 | * CPU is in big endian we need to program swapped mode for management |
Andy Shevchenko | f2ce8a9e | 2015-07-24 21:23:59 +0300 | [diff] [blame] | 250 | * descriptor access. |
| 251 | */ |
| 252 | static bool hw_is_native_io(void __iomem *addr) |
| 253 | { |
| 254 | u32 value = MACB_BIT(LLB); |
| 255 | |
| 256 | __raw_writel(value, addr + MACB_NCR); |
| 257 | value = __raw_readl(addr + MACB_NCR); |
| 258 | |
| 259 | /* Write 0 back to disable everything */ |
| 260 | __raw_writel(0, addr + MACB_NCR); |
| 261 | |
| 262 | return value == MACB_BIT(LLB); |
| 263 | } |
| 264 | |
| 265 | static bool hw_is_gem(void __iomem *addr, bool native_io) |
| 266 | { |
| 267 | u32 id; |
| 268 | |
| 269 | if (native_io) |
| 270 | id = __raw_readl(addr + MACB_MID); |
| 271 | else |
| 272 | id = readl_relaxed(addr + MACB_MID); |
| 273 | |
| 274 | return MACB_BFEXT(IDNUM, id) >= 0x2; |
| 275 | } |
| 276 | |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 277 | static void macb_set_hwaddr(struct macb *bp) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 278 | { |
| 279 | u32 bottom; |
| 280 | u16 top; |
| 281 | |
| 282 | bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr)); |
Jamie Iles | f75ba50 | 2011-11-08 10:12:32 +0000 | [diff] [blame] | 283 | macb_or_gem_writel(bp, SA1B, bottom); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 284 | top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4))); |
Jamie Iles | f75ba50 | 2011-11-08 10:12:32 +0000 | [diff] [blame] | 285 | macb_or_gem_writel(bp, SA1T, top); |
Joachim Eastwood | 3629a6c | 2012-11-11 13:56:28 +0000 | [diff] [blame] | 286 | |
| 287 | /* Clear unused address register sets */ |
| 288 | macb_or_gem_writel(bp, SA2B, 0); |
| 289 | macb_or_gem_writel(bp, SA2T, 0); |
| 290 | macb_or_gem_writel(bp, SA3B, 0); |
| 291 | macb_or_gem_writel(bp, SA3T, 0); |
| 292 | macb_or_gem_writel(bp, SA4B, 0); |
| 293 | macb_or_gem_writel(bp, SA4T, 0); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 294 | } |
| 295 | |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 296 | static void macb_get_hwaddr(struct macb *bp) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 297 | { |
| 298 | u32 bottom; |
| 299 | u16 top; |
| 300 | u8 addr[6]; |
Joachim Eastwood | 17b8bb3 | 2012-11-07 08:14:50 +0000 | [diff] [blame] | 301 | int i; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 302 | |
Moritz Fischer | aa50b55 | 2016-03-29 19:11:13 -0700 | [diff] [blame] | 303 | /* Check all 4 address register for valid address */ |
Joachim Eastwood | 17b8bb3 | 2012-11-07 08:14:50 +0000 | [diff] [blame] | 304 | for (i = 0; i < 4; i++) { |
| 305 | bottom = macb_or_gem_readl(bp, SA1B + i * 8); |
| 306 | top = macb_or_gem_readl(bp, SA1T + i * 8); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 307 | |
Nicolas Ferre | 8b95274 | 2019-05-03 12:36:58 +0200 | [diff] [blame] | 308 | addr[0] = bottom & 0xff; |
| 309 | addr[1] = (bottom >> 8) & 0xff; |
| 310 | addr[2] = (bottom >> 16) & 0xff; |
| 311 | addr[3] = (bottom >> 24) & 0xff; |
| 312 | addr[4] = top & 0xff; |
| 313 | addr[5] = (top >> 8) & 0xff; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 314 | |
Joachim Eastwood | 17b8bb3 | 2012-11-07 08:14:50 +0000 | [diff] [blame] | 315 | if (is_valid_ether_addr(addr)) { |
Jakub Kicinski | c51e506 | 2021-10-13 13:44:32 -0700 | [diff] [blame] | 316 | eth_hw_addr_set(bp->dev, addr); |
Joachim Eastwood | 17b8bb3 | 2012-11-07 08:14:50 +0000 | [diff] [blame] | 317 | return; |
| 318 | } |
Sven Schnelle | d1d5741 | 2008-06-09 16:33:57 -0700 | [diff] [blame] | 319 | } |
Joachim Eastwood | 17b8bb3 | 2012-11-07 08:14:50 +0000 | [diff] [blame] | 320 | |
Andy Shevchenko | a35919e | 2015-07-24 21:24:01 +0300 | [diff] [blame] | 321 | dev_info(&bp->pdev->dev, "invalid hw address, using random\n"); |
Joachim Eastwood | 17b8bb3 | 2012-11-07 08:14:50 +0000 | [diff] [blame] | 322 | eth_hw_addr_random(bp->dev); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 323 | } |
| 324 | |
Harini Katakam | 8beb79b | 2019-03-01 16:20:32 +0530 | [diff] [blame] | 325 | static int macb_mdio_wait_for_idle(struct macb *bp) |
| 326 | { |
| 327 | u32 val; |
| 328 | |
| 329 | return readx_poll_timeout(MACB_READ_NSR, bp, val, val & MACB_BIT(IDLE), |
| 330 | 1, MACB_MDIO_TIMEOUT); |
| 331 | } |
| 332 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 333 | static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 334 | { |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 335 | struct macb *bp = bus->priv; |
Harini Katakam | d54f89a | 2019-03-01 16:20:34 +0530 | [diff] [blame] | 336 | int status; |
Harini Katakam | 8beb79b | 2019-03-01 16:20:32 +0530 | [diff] [blame] | 337 | |
Harini Katakam | d54f89a | 2019-03-01 16:20:34 +0530 | [diff] [blame] | 338 | status = pm_runtime_get_sync(&bp->pdev->dev); |
Andy Shevchenko | 0ce205d | 2020-04-27 13:51:20 +0300 | [diff] [blame] | 339 | if (status < 0) { |
| 340 | pm_runtime_put_noidle(&bp->pdev->dev); |
Harini Katakam | d54f89a | 2019-03-01 16:20:34 +0530 | [diff] [blame] | 341 | goto mdio_pm_exit; |
Andy Shevchenko | 0ce205d | 2020-04-27 13:51:20 +0300 | [diff] [blame] | 342 | } |
Harini Katakam | d54f89a | 2019-03-01 16:20:34 +0530 | [diff] [blame] | 343 | |
| 344 | status = macb_mdio_wait_for_idle(bp); |
| 345 | if (status < 0) |
| 346 | goto mdio_read_exit; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 347 | |
Milind Parab | 43ad352 | 2020-01-09 08:36:46 +0000 | [diff] [blame] | 348 | if (regnum & MII_ADDR_C45) { |
| 349 | macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C45_SOF) |
| 350 | | MACB_BF(RW, MACB_MAN_C45_ADDR) |
| 351 | | MACB_BF(PHYA, mii_id) |
| 352 | | MACB_BF(REGA, (regnum >> 16) & 0x1F) |
| 353 | | MACB_BF(DATA, regnum & 0xFFFF) |
| 354 | | MACB_BF(CODE, MACB_MAN_C45_CODE))); |
| 355 | |
| 356 | status = macb_mdio_wait_for_idle(bp); |
| 357 | if (status < 0) |
| 358 | goto mdio_read_exit; |
| 359 | |
| 360 | macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C45_SOF) |
| 361 | | MACB_BF(RW, MACB_MAN_C45_READ) |
| 362 | | MACB_BF(PHYA, mii_id) |
| 363 | | MACB_BF(REGA, (regnum >> 16) & 0x1F) |
| 364 | | MACB_BF(CODE, MACB_MAN_C45_CODE))); |
| 365 | } else { |
| 366 | macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C22_SOF) |
| 367 | | MACB_BF(RW, MACB_MAN_C22_READ) |
| 368 | | MACB_BF(PHYA, mii_id) |
| 369 | | MACB_BF(REGA, regnum) |
| 370 | | MACB_BF(CODE, MACB_MAN_C22_CODE))); |
| 371 | } |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 372 | |
Harini Katakam | d54f89a | 2019-03-01 16:20:34 +0530 | [diff] [blame] | 373 | status = macb_mdio_wait_for_idle(bp); |
| 374 | if (status < 0) |
| 375 | goto mdio_read_exit; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 376 | |
Harini Katakam | d54f89a | 2019-03-01 16:20:34 +0530 | [diff] [blame] | 377 | status = MACB_BFEXT(DATA, macb_readl(bp, MAN)); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 378 | |
Harini Katakam | d54f89a | 2019-03-01 16:20:34 +0530 | [diff] [blame] | 379 | mdio_read_exit: |
| 380 | pm_runtime_mark_last_busy(&bp->pdev->dev); |
| 381 | pm_runtime_put_autosuspend(&bp->pdev->dev); |
| 382 | mdio_pm_exit: |
| 383 | return status; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 384 | } |
| 385 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 386 | static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum, |
| 387 | u16 value) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 388 | { |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 389 | struct macb *bp = bus->priv; |
Harini Katakam | d54f89a | 2019-03-01 16:20:34 +0530 | [diff] [blame] | 390 | int status; |
Harini Katakam | 8beb79b | 2019-03-01 16:20:32 +0530 | [diff] [blame] | 391 | |
Harini Katakam | d54f89a | 2019-03-01 16:20:34 +0530 | [diff] [blame] | 392 | status = pm_runtime_get_sync(&bp->pdev->dev); |
Andy Shevchenko | 0ce205d | 2020-04-27 13:51:20 +0300 | [diff] [blame] | 393 | if (status < 0) { |
| 394 | pm_runtime_put_noidle(&bp->pdev->dev); |
Harini Katakam | d54f89a | 2019-03-01 16:20:34 +0530 | [diff] [blame] | 395 | goto mdio_pm_exit; |
Andy Shevchenko | 0ce205d | 2020-04-27 13:51:20 +0300 | [diff] [blame] | 396 | } |
Harini Katakam | d54f89a | 2019-03-01 16:20:34 +0530 | [diff] [blame] | 397 | |
| 398 | status = macb_mdio_wait_for_idle(bp); |
| 399 | if (status < 0) |
| 400 | goto mdio_write_exit; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 401 | |
Milind Parab | 43ad352 | 2020-01-09 08:36:46 +0000 | [diff] [blame] | 402 | if (regnum & MII_ADDR_C45) { |
| 403 | macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C45_SOF) |
| 404 | | MACB_BF(RW, MACB_MAN_C45_ADDR) |
| 405 | | MACB_BF(PHYA, mii_id) |
| 406 | | MACB_BF(REGA, (regnum >> 16) & 0x1F) |
| 407 | | MACB_BF(DATA, regnum & 0xFFFF) |
| 408 | | MACB_BF(CODE, MACB_MAN_C45_CODE))); |
| 409 | |
| 410 | status = macb_mdio_wait_for_idle(bp); |
| 411 | if (status < 0) |
| 412 | goto mdio_write_exit; |
| 413 | |
| 414 | macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C45_SOF) |
| 415 | | MACB_BF(RW, MACB_MAN_C45_WRITE) |
| 416 | | MACB_BF(PHYA, mii_id) |
| 417 | | MACB_BF(REGA, (regnum >> 16) & 0x1F) |
| 418 | | MACB_BF(CODE, MACB_MAN_C45_CODE) |
| 419 | | MACB_BF(DATA, value))); |
| 420 | } else { |
| 421 | macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C22_SOF) |
| 422 | | MACB_BF(RW, MACB_MAN_C22_WRITE) |
| 423 | | MACB_BF(PHYA, mii_id) |
| 424 | | MACB_BF(REGA, regnum) |
| 425 | | MACB_BF(CODE, MACB_MAN_C22_CODE) |
| 426 | | MACB_BF(DATA, value))); |
| 427 | } |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 428 | |
Harini Katakam | d54f89a | 2019-03-01 16:20:34 +0530 | [diff] [blame] | 429 | status = macb_mdio_wait_for_idle(bp); |
| 430 | if (status < 0) |
| 431 | goto mdio_write_exit; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 432 | |
Harini Katakam | d54f89a | 2019-03-01 16:20:34 +0530 | [diff] [blame] | 433 | mdio_write_exit: |
| 434 | pm_runtime_mark_last_busy(&bp->pdev->dev); |
| 435 | pm_runtime_put_autosuspend(&bp->pdev->dev); |
| 436 | mdio_pm_exit: |
| 437 | return status; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 438 | } |
| 439 | |
Antoine Tenart | 6e952d9 | 2019-11-13 10:00:05 +0100 | [diff] [blame] | 440 | static void macb_init_buffers(struct macb *bp) |
| 441 | { |
| 442 | struct macb_queue *queue; |
| 443 | unsigned int q; |
| 444 | |
| 445 | for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) { |
| 446 | queue_writel(queue, RBQP, lower_32_bits(queue->rx_ring_dma)); |
| 447 | #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT |
| 448 | if (bp->hw_dma_cap & HW_DMA_CAP_64B) |
| 449 | queue_writel(queue, RBQPH, |
| 450 | upper_32_bits(queue->rx_ring_dma)); |
| 451 | #endif |
| 452 | queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma)); |
| 453 | #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT |
| 454 | if (bp->hw_dma_cap & HW_DMA_CAP_64B) |
| 455 | queue_writel(queue, TBQPH, |
| 456 | upper_32_bits(queue->tx_ring_dma)); |
| 457 | #endif |
| 458 | } |
| 459 | } |
| 460 | |
Soren Brinkmann | e1824df | 2013-12-10 16:07:23 -0800 | [diff] [blame] | 461 | /** |
| 462 | * macb_set_tx_clk() - Set a clock to a new frequency |
Claudiu Beznea | daafa1d | 2020-12-09 15:03:33 +0200 | [diff] [blame] | 463 | * @bp: pointer to struct macb |
Jesse Brandeburg | d0ea5cb | 2020-09-25 15:24:45 -0700 | [diff] [blame] | 464 | * @speed: New frequency in Hz |
Soren Brinkmann | e1824df | 2013-12-10 16:07:23 -0800 | [diff] [blame] | 465 | */ |
Claudiu Beznea | daafa1d | 2020-12-09 15:03:33 +0200 | [diff] [blame] | 466 | static void macb_set_tx_clk(struct macb *bp, int speed) |
Soren Brinkmann | e1824df | 2013-12-10 16:07:23 -0800 | [diff] [blame] | 467 | { |
| 468 | long ferr, rate, rate_rounded; |
| 469 | |
Charles Keepax | 1d0d561 | 2021-01-04 10:38:02 +0000 | [diff] [blame] | 470 | if (!bp->tx_clk || (bp->caps & MACB_CAPS_CLK_HW_CHG)) |
Cyrille Pitchen | 93b31f4 | 2015-03-07 07:23:31 +0100 | [diff] [blame] | 471 | return; |
| 472 | |
Michael Walle | 43e57631 | 2021-01-20 20:43:03 +0100 | [diff] [blame] | 473 | /* In case of MII the PHY is the clock master */ |
| 474 | if (bp->phy_interface == PHY_INTERFACE_MODE_MII) |
| 475 | return; |
| 476 | |
Soren Brinkmann | e1824df | 2013-12-10 16:07:23 -0800 | [diff] [blame] | 477 | switch (speed) { |
| 478 | case SPEED_10: |
| 479 | rate = 2500000; |
| 480 | break; |
| 481 | case SPEED_100: |
| 482 | rate = 25000000; |
| 483 | break; |
| 484 | case SPEED_1000: |
| 485 | rate = 125000000; |
| 486 | break; |
| 487 | default: |
Soren Brinkmann | 9319e47 | 2013-12-10 20:57:57 -0800 | [diff] [blame] | 488 | return; |
Soren Brinkmann | e1824df | 2013-12-10 16:07:23 -0800 | [diff] [blame] | 489 | } |
| 490 | |
Claudiu Beznea | daafa1d | 2020-12-09 15:03:33 +0200 | [diff] [blame] | 491 | rate_rounded = clk_round_rate(bp->tx_clk, rate); |
Soren Brinkmann | e1824df | 2013-12-10 16:07:23 -0800 | [diff] [blame] | 492 | if (rate_rounded < 0) |
| 493 | return; |
| 494 | |
| 495 | /* RGMII allows 50 ppm frequency error. Test and warn if this limit |
| 496 | * is not satisfied. |
| 497 | */ |
| 498 | ferr = abs(rate_rounded - rate); |
| 499 | ferr = DIV_ROUND_UP(ferr, rate / 100000); |
| 500 | if (ferr > 5) |
Claudiu Beznea | daafa1d | 2020-12-09 15:03:33 +0200 | [diff] [blame] | 501 | netdev_warn(bp->dev, |
| 502 | "unable to generate target frequency: %ld Hz\n", |
Moritz Fischer | aa50b55 | 2016-03-29 19:11:13 -0700 | [diff] [blame] | 503 | rate); |
Soren Brinkmann | e1824df | 2013-12-10 16:07:23 -0800 | [diff] [blame] | 504 | |
Claudiu Beznea | daafa1d | 2020-12-09 15:03:33 +0200 | [diff] [blame] | 505 | if (clk_set_rate(bp->tx_clk, rate_rounded)) |
| 506 | netdev_err(bp->dev, "adjusting tx_clk failed.\n"); |
Soren Brinkmann | e1824df | 2013-12-10 16:07:23 -0800 | [diff] [blame] | 507 | } |
| 508 | |
Parshuram Thombare | e4e143e | 2020-10-29 13:47:07 +0100 | [diff] [blame] | 509 | static void macb_usx_pcs_link_up(struct phylink_pcs *pcs, unsigned int mode, |
| 510 | phy_interface_t interface, int speed, |
| 511 | int duplex) |
| 512 | { |
Russell King (Oracle) | 8876769 | 2022-01-05 13:15:15 +0000 | [diff] [blame] | 513 | struct macb *bp = container_of(pcs, struct macb, phylink_usx_pcs); |
Parshuram Thombare | e4e143e | 2020-10-29 13:47:07 +0100 | [diff] [blame] | 514 | u32 config; |
| 515 | |
| 516 | config = gem_readl(bp, USX_CONTROL); |
| 517 | config = GEM_BFINS(SERDES_RATE, MACB_SERDES_RATE_10G, config); |
| 518 | config = GEM_BFINS(USX_CTRL_SPEED, HS_SPEED_10000M, config); |
| 519 | config &= ~(GEM_BIT(TX_SCR_BYPASS) | GEM_BIT(RX_SCR_BYPASS)); |
| 520 | config |= GEM_BIT(TX_EN); |
| 521 | gem_writel(bp, USX_CONTROL, config); |
| 522 | } |
| 523 | |
| 524 | static void macb_usx_pcs_get_state(struct phylink_pcs *pcs, |
Russell King | d46b7e4 | 2019-11-21 00:36:22 +0000 | [diff] [blame] | 525 | struct phylink_link_state *state) |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 526 | { |
Russell King (Oracle) | 8876769 | 2022-01-05 13:15:15 +0000 | [diff] [blame] | 527 | struct macb *bp = container_of(pcs, struct macb, phylink_usx_pcs); |
Parshuram Thombare | e4e143e | 2020-10-29 13:47:07 +0100 | [diff] [blame] | 528 | u32 val; |
| 529 | |
| 530 | state->speed = SPEED_10000; |
| 531 | state->duplex = 1; |
| 532 | state->an_complete = 1; |
| 533 | |
| 534 | val = gem_readl(bp, USX_STATUS); |
| 535 | state->link = !!(val & GEM_BIT(USX_BLOCK_LOCK)); |
| 536 | val = gem_readl(bp, NCFGR); |
| 537 | if (val & GEM_BIT(PAE)) |
| 538 | state->pause = MLO_PAUSE_RX; |
| 539 | } |
| 540 | |
| 541 | static int macb_usx_pcs_config(struct phylink_pcs *pcs, |
| 542 | unsigned int mode, |
| 543 | phy_interface_t interface, |
| 544 | const unsigned long *advertising, |
| 545 | bool permit_pause_to_mac) |
| 546 | { |
Russell King (Oracle) | 8876769 | 2022-01-05 13:15:15 +0000 | [diff] [blame] | 547 | struct macb *bp = container_of(pcs, struct macb, phylink_usx_pcs); |
Parshuram Thombare | e4e143e | 2020-10-29 13:47:07 +0100 | [diff] [blame] | 548 | |
| 549 | gem_writel(bp, USX_CONTROL, gem_readl(bp, USX_CONTROL) | |
| 550 | GEM_BIT(SIGNAL_OK)); |
| 551 | |
| 552 | return 0; |
| 553 | } |
| 554 | |
| 555 | static void macb_pcs_get_state(struct phylink_pcs *pcs, |
| 556 | struct phylink_link_state *state) |
| 557 | { |
Russell King | d46b7e4 | 2019-11-21 00:36:22 +0000 | [diff] [blame] | 558 | state->link = 0; |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 559 | } |
| 560 | |
Parshuram Thombare | e4e143e | 2020-10-29 13:47:07 +0100 | [diff] [blame] | 561 | static void macb_pcs_an_restart(struct phylink_pcs *pcs) |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 562 | { |
| 563 | /* Not supported */ |
| 564 | } |
| 565 | |
Parshuram Thombare | 0012eeb | 2020-11-05 18:58:33 +0100 | [diff] [blame] | 566 | static int macb_pcs_config(struct phylink_pcs *pcs, |
| 567 | unsigned int mode, |
| 568 | phy_interface_t interface, |
| 569 | const unsigned long *advertising, |
| 570 | bool permit_pause_to_mac) |
| 571 | { |
| 572 | return 0; |
| 573 | } |
| 574 | |
Parshuram Thombare | e4e143e | 2020-10-29 13:47:07 +0100 | [diff] [blame] | 575 | static const struct phylink_pcs_ops macb_phylink_usx_pcs_ops = { |
| 576 | .pcs_get_state = macb_usx_pcs_get_state, |
| 577 | .pcs_config = macb_usx_pcs_config, |
| 578 | .pcs_link_up = macb_usx_pcs_link_up, |
| 579 | }; |
| 580 | |
| 581 | static const struct phylink_pcs_ops macb_phylink_pcs_ops = { |
| 582 | .pcs_get_state = macb_pcs_get_state, |
| 583 | .pcs_an_restart = macb_pcs_an_restart, |
Parshuram Thombare | 0012eeb | 2020-11-05 18:58:33 +0100 | [diff] [blame] | 584 | .pcs_config = macb_pcs_config, |
Parshuram Thombare | e4e143e | 2020-10-29 13:47:07 +0100 | [diff] [blame] | 585 | }; |
| 586 | |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 587 | static void macb_mac_config(struct phylink_config *config, unsigned int mode, |
| 588 | const struct phylink_link_state *state) |
| 589 | { |
| 590 | struct net_device *ndev = to_net_dev(config->dev); |
| 591 | struct macb *bp = netdev_priv(ndev); |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 592 | unsigned long flags; |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 593 | u32 old_ctrl, ctrl; |
Parshuram Thombare | e4e143e | 2020-10-29 13:47:07 +0100 | [diff] [blame] | 594 | u32 old_ncr, ncr; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 595 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 596 | spin_lock_irqsave(&bp->lock, flags); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 597 | |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 598 | old_ctrl = ctrl = macb_or_gem_readl(bp, NCFGR); |
Parshuram Thombare | e4e143e | 2020-10-29 13:47:07 +0100 | [diff] [blame] | 599 | old_ncr = ncr = macb_or_gem_readl(bp, NCR); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 600 | |
Alexandre Belloni | ac2fcfa | 2020-02-19 15:15:51 +0100 | [diff] [blame] | 601 | if (bp->caps & MACB_CAPS_MACB_IS_EMAC) { |
| 602 | if (state->interface == PHY_INTERFACE_MODE_RMII) |
| 603 | ctrl |= MACB_BIT(RM9200_RMII); |
Stefan Roese | f7ba7db | 2020-08-04 14:17:16 +0200 | [diff] [blame] | 604 | } else if (macb_is_gem(bp)) { |
Russell King | 633e98a | 2020-02-26 10:24:06 +0000 | [diff] [blame] | 605 | ctrl &= ~(GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL)); |
Parshuram Thombare | e4e143e | 2020-10-29 13:47:07 +0100 | [diff] [blame] | 606 | ncr &= ~GEM_BIT(ENABLE_HS_MAC); |
Alexandre Belloni | ac2fcfa | 2020-02-19 15:15:51 +0100 | [diff] [blame] | 607 | |
Parshuram Thombare | e4e143e | 2020-10-29 13:47:07 +0100 | [diff] [blame] | 608 | if (state->interface == PHY_INTERFACE_MODE_SGMII) { |
Alexandre Belloni | ac2fcfa | 2020-02-19 15:15:51 +0100 | [diff] [blame] | 609 | ctrl |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL); |
Parshuram Thombare | e4e143e | 2020-10-29 13:47:07 +0100 | [diff] [blame] | 610 | } else if (state->interface == PHY_INTERFACE_MODE_10GBASER) { |
| 611 | ctrl |= GEM_BIT(PCSSEL); |
| 612 | ncr |= GEM_BIT(ENABLE_HS_MAC); |
Claudiu Beznea | 1a9b5a2 | 2021-09-17 16:26:14 +0300 | [diff] [blame] | 613 | } else if (bp->caps & MACB_CAPS_MIIONRGMII && |
| 614 | bp->phy_interface == PHY_INTERFACE_MODE_MII) { |
| 615 | ncr |= MACB_BIT(MIIONRGMII); |
Parshuram Thombare | e4e143e | 2020-10-29 13:47:07 +0100 | [diff] [blame] | 616 | } |
Alexandre Belloni | ac2fcfa | 2020-02-19 15:15:51 +0100 | [diff] [blame] | 617 | } |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 618 | |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 619 | /* Apply the new configuration, if any */ |
| 620 | if (old_ctrl ^ ctrl) |
| 621 | macb_or_gem_writel(bp, NCFGR, ctrl); |
| 622 | |
Parshuram Thombare | e4e143e | 2020-10-29 13:47:07 +0100 | [diff] [blame] | 623 | if (old_ncr ^ ncr) |
| 624 | macb_or_gem_writel(bp, NCR, ncr); |
| 625 | |
Robert Hancock | e276e5e4 | 2021-03-11 14:18:13 -0600 | [diff] [blame] | 626 | /* Disable AN for SGMII fixed link configuration, enable otherwise. |
| 627 | * Must be written after PCSSEL is set in NCFGR, |
| 628 | * otherwise writes will not take effect. |
| 629 | */ |
| 630 | if (macb_is_gem(bp) && state->interface == PHY_INTERFACE_MODE_SGMII) { |
| 631 | u32 pcsctrl, old_pcsctrl; |
| 632 | |
| 633 | old_pcsctrl = gem_readl(bp, PCSCNTRL); |
| 634 | if (mode == MLO_AN_FIXED) |
| 635 | pcsctrl = old_pcsctrl & ~GEM_BIT(PCSAUTONEG); |
| 636 | else |
| 637 | pcsctrl = old_pcsctrl | GEM_BIT(PCSAUTONEG); |
| 638 | if (old_pcsctrl != pcsctrl) |
| 639 | gem_writel(bp, PCSCNTRL, pcsctrl); |
| 640 | } |
| 641 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 642 | spin_unlock_irqrestore(&bp->lock, flags); |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 643 | } |
| 644 | |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 645 | static void macb_mac_link_down(struct phylink_config *config, unsigned int mode, |
| 646 | phy_interface_t interface) |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 647 | { |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 648 | struct net_device *ndev = to_net_dev(config->dev); |
| 649 | struct macb *bp = netdev_priv(ndev); |
| 650 | struct macb_queue *queue; |
| 651 | unsigned int q; |
| 652 | u32 ctrl; |
| 653 | |
Alexandre Belloni | ac2fcfa | 2020-02-19 15:15:51 +0100 | [diff] [blame] | 654 | if (!(bp->caps & MACB_CAPS_MACB_IS_EMAC)) |
| 655 | for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) |
| 656 | queue_writel(queue, IDR, |
| 657 | bp->rx_intr_mask | MACB_TX_INT_FLAGS | MACB_BIT(HRESP)); |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 658 | |
| 659 | /* Disable Rx and Tx */ |
| 660 | ctrl = macb_readl(bp, NCR) & ~(MACB_BIT(RE) | MACB_BIT(TE)); |
| 661 | macb_writel(bp, NCR, ctrl); |
| 662 | |
| 663 | netif_tx_stop_all_queues(ndev); |
| 664 | } |
| 665 | |
Russell King | 91a208f | 2020-02-26 10:23:41 +0000 | [diff] [blame] | 666 | static void macb_mac_link_up(struct phylink_config *config, |
| 667 | struct phy_device *phy, |
| 668 | unsigned int mode, phy_interface_t interface, |
| 669 | int speed, int duplex, |
| 670 | bool tx_pause, bool rx_pause) |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 671 | { |
| 672 | struct net_device *ndev = to_net_dev(config->dev); |
| 673 | struct macb *bp = netdev_priv(ndev); |
| 674 | struct macb_queue *queue; |
Russell King | 633e98a | 2020-02-26 10:24:06 +0000 | [diff] [blame] | 675 | unsigned long flags; |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 676 | unsigned int q; |
Russell King | 633e98a | 2020-02-26 10:24:06 +0000 | [diff] [blame] | 677 | u32 ctrl; |
| 678 | |
| 679 | spin_lock_irqsave(&bp->lock, flags); |
| 680 | |
| 681 | ctrl = macb_or_gem_readl(bp, NCFGR); |
| 682 | |
| 683 | ctrl &= ~(MACB_BIT(SPD) | MACB_BIT(FD)); |
| 684 | |
| 685 | if (speed == SPEED_100) |
| 686 | ctrl |= MACB_BIT(SPD); |
| 687 | |
| 688 | if (duplex) |
| 689 | ctrl |= MACB_BIT(FD); |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 690 | |
Alexandre Belloni | ac2fcfa | 2020-02-19 15:15:51 +0100 | [diff] [blame] | 691 | if (!(bp->caps & MACB_CAPS_MACB_IS_EMAC)) { |
Stefan Roese | f7ba7db | 2020-08-04 14:17:16 +0200 | [diff] [blame] | 692 | ctrl &= ~MACB_BIT(PAE); |
| 693 | if (macb_is_gem(bp)) { |
| 694 | ctrl &= ~GEM_BIT(GBE); |
Russell King | 633e98a | 2020-02-26 10:24:06 +0000 | [diff] [blame] | 695 | |
Stefan Roese | f7ba7db | 2020-08-04 14:17:16 +0200 | [diff] [blame] | 696 | if (speed == SPEED_1000) |
| 697 | ctrl |= GEM_BIT(GBE); |
| 698 | } |
Russell King | 633e98a | 2020-02-26 10:24:06 +0000 | [diff] [blame] | 699 | |
Parshuram Thombare | d7739b0 | 2020-09-05 10:21:33 +0200 | [diff] [blame] | 700 | if (rx_pause) |
Russell King | 633e98a | 2020-02-26 10:24:06 +0000 | [diff] [blame] | 701 | ctrl |= MACB_BIT(PAE); |
| 702 | |
Claudiu Beznea | daafa1d | 2020-12-09 15:03:33 +0200 | [diff] [blame] | 703 | macb_set_tx_clk(bp, speed); |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 704 | |
Alexandre Belloni | ac2fcfa | 2020-02-19 15:15:51 +0100 | [diff] [blame] | 705 | /* Initialize rings & buffers as clearing MACB_BIT(TE) in link down |
| 706 | * cleared the pipeline and control registers. |
| 707 | */ |
| 708 | bp->macbgem_ops.mog_init_rings(bp); |
| 709 | macb_init_buffers(bp); |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 710 | |
Alexandre Belloni | ac2fcfa | 2020-02-19 15:15:51 +0100 | [diff] [blame] | 711 | for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) |
| 712 | queue_writel(queue, IER, |
| 713 | bp->rx_intr_mask | MACB_TX_INT_FLAGS | MACB_BIT(HRESP)); |
| 714 | } |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 715 | |
Russell King | 633e98a | 2020-02-26 10:24:06 +0000 | [diff] [blame] | 716 | macb_or_gem_writel(bp, NCFGR, ctrl); |
| 717 | |
Parshuram Thombare | e4e143e | 2020-10-29 13:47:07 +0100 | [diff] [blame] | 718 | if (bp->phy_interface == PHY_INTERFACE_MODE_10GBASER) |
| 719 | gem_writel(bp, HS_MAC_CONFIG, GEM_BFINS(HS_MAC_SPEED, HS_SPEED_10000M, |
| 720 | gem_readl(bp, HS_MAC_CONFIG))); |
| 721 | |
Russell King | 633e98a | 2020-02-26 10:24:06 +0000 | [diff] [blame] | 722 | spin_unlock_irqrestore(&bp->lock, flags); |
| 723 | |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 724 | /* Enable Rx and Tx */ |
| 725 | macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(RE) | MACB_BIT(TE)); |
| 726 | |
| 727 | netif_tx_wake_all_queues(ndev); |
| 728 | } |
| 729 | |
Russell King (Oracle) | 8876769 | 2022-01-05 13:15:15 +0000 | [diff] [blame] | 730 | static struct phylink_pcs *macb_mac_select_pcs(struct phylink_config *config, |
| 731 | phy_interface_t interface) |
Parshuram Thombare | e4e143e | 2020-10-29 13:47:07 +0100 | [diff] [blame] | 732 | { |
| 733 | struct net_device *ndev = to_net_dev(config->dev); |
| 734 | struct macb *bp = netdev_priv(ndev); |
| 735 | |
| 736 | if (interface == PHY_INTERFACE_MODE_10GBASER) |
Russell King (Oracle) | 8876769 | 2022-01-05 13:15:15 +0000 | [diff] [blame] | 737 | return &bp->phylink_usx_pcs; |
Parshuram Thombare | 0012eeb | 2020-11-05 18:58:33 +0100 | [diff] [blame] | 738 | else if (interface == PHY_INTERFACE_MODE_SGMII) |
Russell King (Oracle) | 8876769 | 2022-01-05 13:15:15 +0000 | [diff] [blame] | 739 | return &bp->phylink_sgmii_pcs; |
Parshuram Thombare | 0012eeb | 2020-11-05 18:58:33 +0100 | [diff] [blame] | 740 | else |
Russell King (Oracle) | 8876769 | 2022-01-05 13:15:15 +0000 | [diff] [blame] | 741 | return NULL; |
Parshuram Thombare | e4e143e | 2020-10-29 13:47:07 +0100 | [diff] [blame] | 742 | } |
| 743 | |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 744 | static const struct phylink_mac_ops macb_phylink_ops = { |
Russell King (Oracle) | cc0a75e | 2021-11-24 15:44:43 +0000 | [diff] [blame] | 745 | .validate = phylink_generic_validate, |
Russell King (Oracle) | 8876769 | 2022-01-05 13:15:15 +0000 | [diff] [blame] | 746 | .mac_select_pcs = macb_mac_select_pcs, |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 747 | .mac_config = macb_mac_config, |
| 748 | .mac_link_down = macb_mac_link_down, |
| 749 | .mac_link_up = macb_mac_link_up, |
| 750 | }; |
| 751 | |
Milind Parab | fd2a891 | 2020-01-13 03:30:43 +0000 | [diff] [blame] | 752 | static bool macb_phy_handle_exists(struct device_node *dn) |
| 753 | { |
| 754 | dn = of_parse_phandle(dn, "phy-handle", 0); |
| 755 | of_node_put(dn); |
| 756 | return dn != NULL; |
| 757 | } |
| 758 | |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 759 | static int macb_phylink_connect(struct macb *bp) |
| 760 | { |
Milind Parab | fd2a891 | 2020-01-13 03:30:43 +0000 | [diff] [blame] | 761 | struct device_node *dn = bp->pdev->dev.of_node; |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 762 | struct net_device *dev = bp->dev; |
Jiri Pirko | 7455a76 | 2010-02-08 05:12:08 +0000 | [diff] [blame] | 763 | struct phy_device *phydev; |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 764 | int ret; |
Brad Mouring | 739de9a | 2018-03-13 16:32:13 -0500 | [diff] [blame] | 765 | |
Milind Parab | fd2a891 | 2020-01-13 03:30:43 +0000 | [diff] [blame] | 766 | if (dn) |
| 767 | ret = phylink_of_phy_connect(bp->phylink, dn, 0); |
| 768 | |
| 769 | if (!dn || (ret && !macb_phy_handle_exists(dn))) { |
Michael Grzeschik | dacdbb4 | 2017-06-23 16:54:10 +0200 | [diff] [blame] | 770 | phydev = phy_find_first(bp->mii_bus); |
| 771 | if (!phydev) { |
| 772 | netdev_err(dev, "no PHY found\n"); |
| 773 | return -ENXIO; |
Joachim Eastwood | 2dbfdbb9 | 2012-11-11 13:56:27 +0000 | [diff] [blame] | 774 | } |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 775 | |
Michael Grzeschik | dacdbb4 | 2017-06-23 16:54:10 +0200 | [diff] [blame] | 776 | /* attach the mac to the phy */ |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 777 | ret = phylink_connect_phy(bp->phylink, phydev); |
Milind Parab | fd2a891 | 2020-01-13 03:30:43 +0000 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | if (ret) { |
| 781 | netdev_err(dev, "Could not attach PHY (%d)\n", ret); |
| 782 | return ret; |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 783 | } |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 784 | |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 785 | phylink_start(bp->phylink); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 786 | |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 787 | return 0; |
| 788 | } |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 789 | |
Robert Hancock | 8fab174 | 2021-03-11 14:18:12 -0600 | [diff] [blame] | 790 | static void macb_get_pcs_fixed_state(struct phylink_config *config, |
| 791 | struct phylink_link_state *state) |
| 792 | { |
| 793 | struct net_device *ndev = to_net_dev(config->dev); |
| 794 | struct macb *bp = netdev_priv(ndev); |
| 795 | |
| 796 | state->link = (macb_readl(bp, NSR) & MACB_BIT(NSR_LINK)) != 0; |
| 797 | } |
| 798 | |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 799 | /* based on au1000_eth. c*/ |
| 800 | static int macb_mii_probe(struct net_device *dev) |
| 801 | { |
| 802 | struct macb *bp = netdev_priv(dev); |
| 803 | |
Russell King (Oracle) | 8876769 | 2022-01-05 13:15:15 +0000 | [diff] [blame] | 804 | bp->phylink_sgmii_pcs.ops = &macb_phylink_pcs_ops; |
| 805 | bp->phylink_usx_pcs.ops = &macb_phylink_usx_pcs_ops; |
| 806 | |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 807 | bp->phylink_config.dev = &dev->dev; |
| 808 | bp->phylink_config.type = PHYLINK_NETDEV; |
| 809 | |
Robert Hancock | 8fab174 | 2021-03-11 14:18:12 -0600 | [diff] [blame] | 810 | if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII) { |
| 811 | bp->phylink_config.poll_fixed_state = true; |
| 812 | bp->phylink_config.get_fixed_state = macb_get_pcs_fixed_state; |
| 813 | } |
| 814 | |
Russell King (Oracle) | cc0a75e | 2021-11-24 15:44:43 +0000 | [diff] [blame] | 815 | bp->phylink_config.mac_capabilities = MAC_ASYM_PAUSE | |
| 816 | MAC_10 | MAC_100; |
| 817 | |
| 818 | __set_bit(PHY_INTERFACE_MODE_MII, |
| 819 | bp->phylink_config.supported_interfaces); |
| 820 | __set_bit(PHY_INTERFACE_MODE_RMII, |
| 821 | bp->phylink_config.supported_interfaces); |
| 822 | |
| 823 | /* Determine what modes are supported */ |
| 824 | if (macb_is_gem(bp) && (bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)) { |
| 825 | bp->phylink_config.mac_capabilities |= MAC_1000FD; |
| 826 | if (!(bp->caps & MACB_CAPS_NO_GIGABIT_HALF)) |
| 827 | bp->phylink_config.mac_capabilities |= MAC_1000HD; |
| 828 | |
| 829 | __set_bit(PHY_INTERFACE_MODE_GMII, |
| 830 | bp->phylink_config.supported_interfaces); |
| 831 | phy_interface_set_rgmii(bp->phylink_config.supported_interfaces); |
| 832 | |
| 833 | if (bp->caps & MACB_CAPS_PCS) |
| 834 | __set_bit(PHY_INTERFACE_MODE_SGMII, |
| 835 | bp->phylink_config.supported_interfaces); |
| 836 | |
| 837 | if (bp->caps & MACB_CAPS_HIGH_SPEED) { |
| 838 | __set_bit(PHY_INTERFACE_MODE_10GBASER, |
| 839 | bp->phylink_config.supported_interfaces); |
| 840 | bp->phylink_config.mac_capabilities |= MAC_10000FD; |
| 841 | } |
| 842 | } |
| 843 | |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 844 | bp->phylink = phylink_create(&bp->phylink_config, bp->pdev->dev.fwnode, |
| 845 | bp->phy_interface, &macb_phylink_ops); |
| 846 | if (IS_ERR(bp->phylink)) { |
| 847 | netdev_err(dev, "Could not create a phylink instance (%ld)\n", |
| 848 | PTR_ERR(bp->phylink)); |
| 849 | return PTR_ERR(bp->phylink); |
| 850 | } |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 851 | |
| 852 | return 0; |
| 853 | } |
| 854 | |
Antoine Tenart | ef8a2e2 | 2019-12-17 18:07:42 +0100 | [diff] [blame] | 855 | static int macb_mdiobus_register(struct macb *bp) |
| 856 | { |
| 857 | struct device_node *child, *np = bp->pdev->dev.of_node; |
| 858 | |
Sean Anderson | 4d98bb0 | 2021-10-22 12:35:48 -0400 | [diff] [blame] | 859 | /* If we have a child named mdio, probe it instead of looking for PHYs |
| 860 | * directly under the MAC node |
| 861 | */ |
| 862 | child = of_get_child_by_name(np, "mdio"); |
Guenter Roeck | 8db3cbc | 2021-10-26 10:39:50 -0700 | [diff] [blame] | 863 | if (child) { |
Sean Anderson | 4d98bb0 | 2021-10-22 12:35:48 -0400 | [diff] [blame] | 864 | int ret = of_mdiobus_register(bp->mii_bus, child); |
| 865 | |
| 866 | of_node_put(child); |
| 867 | return ret; |
| 868 | } |
| 869 | |
Codrin Ciubotariu | 79540d1 | 2020-03-31 12:39:35 +0300 | [diff] [blame] | 870 | if (of_phy_is_fixed_link(np)) |
| 871 | return mdiobus_register(bp->mii_bus); |
| 872 | |
Antoine Tenart | ef8a2e2 | 2019-12-17 18:07:42 +0100 | [diff] [blame] | 873 | /* Only create the PHY from the device tree if at least one PHY is |
| 874 | * described. Otherwise scan the entire MDIO bus. We do this to support |
| 875 | * old device tree that did not follow the best practices and did not |
| 876 | * describe their network PHYs. |
| 877 | */ |
| 878 | for_each_available_child_of_node(np, child) |
| 879 | if (of_mdiobus_child_is_phy(child)) { |
| 880 | /* The loop increments the child refcount, |
| 881 | * decrement it before returning. |
| 882 | */ |
| 883 | of_node_put(child); |
| 884 | |
| 885 | return of_mdiobus_register(bp->mii_bus, np); |
| 886 | } |
| 887 | |
| 888 | return mdiobus_register(bp->mii_bus); |
| 889 | } |
| 890 | |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 891 | static int macb_mii_init(struct macb *bp) |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 892 | { |
Ahmad Fatoum | ab5f110 | 2018-08-21 17:35:48 +0200 | [diff] [blame] | 893 | int err = -ENXIO; |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 894 | |
Uwe Kleine-Koenig | 3dbda77 | 2009-07-23 08:31:31 +0200 | [diff] [blame] | 895 | /* Enable management port */ |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 896 | macb_writel(bp, NCR, MACB_BIT(MPE)); |
| 897 | |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 898 | bp->mii_bus = mdiobus_alloc(); |
Moritz Fischer | aa50b55 | 2016-03-29 19:11:13 -0700 | [diff] [blame] | 899 | if (!bp->mii_bus) { |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 900 | err = -ENOMEM; |
| 901 | goto err_out; |
| 902 | } |
| 903 | |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 904 | bp->mii_bus->name = "MACB_mii_bus"; |
| 905 | bp->mii_bus->read = &macb_mdio_read; |
| 906 | bp->mii_bus->write = &macb_mdio_write; |
Florian Fainelli | 98d5e57 | 2012-01-09 23:59:11 +0000 | [diff] [blame] | 907 | snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x", |
Moritz Fischer | aa50b55 | 2016-03-29 19:11:13 -0700 | [diff] [blame] | 908 | bp->pdev->name, bp->pdev->id); |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 909 | bp->mii_bus->priv = bp; |
Florian Fainelli | cf66966 | 2016-05-02 18:38:45 -0700 | [diff] [blame] | 910 | bp->mii_bus->parent = &bp->pdev->dev; |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 911 | |
Jamie Iles | 9152394 | 2011-02-28 04:05:25 +0000 | [diff] [blame] | 912 | dev_set_drvdata(&bp->dev->dev, bp->mii_bus); |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 913 | |
Antoine Tenart | ef8a2e2 | 2019-12-17 18:07:42 +0100 | [diff] [blame] | 914 | err = macb_mdiobus_register(bp); |
Boris BREZILLON | 148cbb5 | 2013-08-22 17:57:28 +0200 | [diff] [blame] | 915 | if (err) |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 916 | goto err_out_free_mdiobus; |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 917 | |
Boris BREZILLON | 7daa78e | 2013-08-27 14:36:14 +0200 | [diff] [blame] | 918 | err = macb_mii_probe(bp->dev); |
| 919 | if (err) |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 920 | goto err_out_unregister_bus; |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 921 | |
| 922 | return 0; |
| 923 | |
| 924 | err_out_unregister_bus: |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 925 | mdiobus_unregister(bp->mii_bus); |
Brad Mouring | 739de9a | 2018-03-13 16:32:13 -0500 | [diff] [blame] | 926 | err_out_free_mdiobus: |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 927 | mdiobus_free(bp->mii_bus); |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 928 | err_out: |
| 929 | return err; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | static void macb_update_stats(struct macb *bp) |
| 933 | { |
Jamie Iles | a494ed8 | 2011-03-09 16:26:35 +0000 | [diff] [blame] | 934 | u32 *p = &bp->hw_stats.macb.rx_pause_frames; |
| 935 | u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1; |
Andy Shevchenko | f2ce8a9e | 2015-07-24 21:23:59 +0300 | [diff] [blame] | 936 | int offset = MACB_PFR; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 937 | |
| 938 | WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4); |
| 939 | |
Moritz Fischer | 96ec631 | 2016-03-29 19:11:11 -0700 | [diff] [blame] | 940 | for (; p < end; p++, offset += 4) |
David S. Miller | 7a6e070 | 2015-07-27 14:24:48 -0700 | [diff] [blame] | 941 | *p += bp->macb_reg_readl(bp, offset); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 942 | } |
| 943 | |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 944 | static int macb_halt_tx(struct macb *bp) |
| 945 | { |
| 946 | unsigned long halt_time, timeout; |
| 947 | u32 status; |
| 948 | |
| 949 | macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT)); |
| 950 | |
| 951 | timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT); |
| 952 | do { |
| 953 | halt_time = jiffies; |
| 954 | status = macb_readl(bp, TSR); |
| 955 | if (!(status & MACB_BIT(TGO))) |
| 956 | return 0; |
| 957 | |
Jia-Ju Bai | 16fe10c | 2018-09-01 20:11:05 +0800 | [diff] [blame] | 958 | udelay(250); |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 959 | } while (time_before(halt_time, timeout)); |
| 960 | |
| 961 | return -ETIMEDOUT; |
| 962 | } |
| 963 | |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 964 | static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb) |
| 965 | { |
| 966 | if (tx_skb->mapping) { |
| 967 | if (tx_skb->mapped_as_page) |
| 968 | dma_unmap_page(&bp->pdev->dev, tx_skb->mapping, |
| 969 | tx_skb->size, DMA_TO_DEVICE); |
| 970 | else |
| 971 | dma_unmap_single(&bp->pdev->dev, tx_skb->mapping, |
| 972 | tx_skb->size, DMA_TO_DEVICE); |
| 973 | tx_skb->mapping = 0; |
| 974 | } |
| 975 | |
| 976 | if (tx_skb->skb) { |
| 977 | dev_kfree_skb_any(tx_skb->skb); |
| 978 | tx_skb->skb = NULL; |
| 979 | } |
| 980 | } |
| 981 | |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 982 | static void macb_set_addr(struct macb *bp, struct macb_dma_desc *desc, dma_addr_t addr) |
Harini Katakam | fff8019 | 2016-08-09 13:15:53 +0530 | [diff] [blame] | 983 | { |
Harini Katakam | fff8019 | 2016-08-09 13:15:53 +0530 | [diff] [blame] | 984 | #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 985 | struct macb_dma_desc_64 *desc_64; |
| 986 | |
Rafal Ozieblo | 7b42961 | 2017-06-29 07:12:51 +0100 | [diff] [blame] | 987 | if (bp->hw_dma_cap & HW_DMA_CAP_64B) { |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 988 | desc_64 = macb_64b_desc(bp, desc); |
| 989 | desc_64->addrh = upper_32_bits(addr); |
Anssi Hannula | e100a89 | 2018-12-17 15:05:39 +0200 | [diff] [blame] | 990 | /* The low bits of RX address contain the RX_USED bit, clearing |
| 991 | * of which allows packet RX. Make sure the high bits are also |
| 992 | * visible to HW at that point. |
| 993 | */ |
| 994 | dma_wmb(); |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 995 | } |
Harini Katakam | fff8019 | 2016-08-09 13:15:53 +0530 | [diff] [blame] | 996 | #endif |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 997 | desc->addr = lower_32_bits(addr); |
| 998 | } |
| 999 | |
| 1000 | static dma_addr_t macb_get_addr(struct macb *bp, struct macb_dma_desc *desc) |
| 1001 | { |
| 1002 | dma_addr_t addr = 0; |
| 1003 | #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT |
| 1004 | struct macb_dma_desc_64 *desc_64; |
| 1005 | |
Rafal Ozieblo | 7b42961 | 2017-06-29 07:12:51 +0100 | [diff] [blame] | 1006 | if (bp->hw_dma_cap & HW_DMA_CAP_64B) { |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 1007 | desc_64 = macb_64b_desc(bp, desc); |
| 1008 | addr = ((u64)(desc_64->addrh) << 32); |
| 1009 | } |
| 1010 | #endif |
| 1011 | addr |= MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr)); |
| 1012 | return addr; |
Harini Katakam | fff8019 | 2016-08-09 13:15:53 +0530 | [diff] [blame] | 1013 | } |
| 1014 | |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 1015 | static void macb_tx_error_task(struct work_struct *work) |
| 1016 | { |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1017 | struct macb_queue *queue = container_of(work, struct macb_queue, |
| 1018 | tx_error_task); |
| 1019 | struct macb *bp = queue->bp; |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 1020 | struct macb_tx_skb *tx_skb; |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1021 | struct macb_dma_desc *desc; |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 1022 | struct sk_buff *skb; |
| 1023 | unsigned int tail; |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1024 | unsigned long flags; |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 1025 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1026 | netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n", |
| 1027 | (unsigned int)(queue - bp->queues), |
| 1028 | queue->tx_tail, queue->tx_head); |
| 1029 | |
| 1030 | /* Prevent the queue IRQ handlers from running: each of them may call |
| 1031 | * macb_tx_interrupt(), which in turn may call netif_wake_subqueue(). |
| 1032 | * As explained below, we have to halt the transmission before updating |
| 1033 | * TBQP registers so we call netif_tx_stop_all_queues() to notify the |
| 1034 | * network engine about the macb/gem being halted. |
| 1035 | */ |
| 1036 | spin_lock_irqsave(&bp->lock, flags); |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 1037 | |
| 1038 | /* Make sure nobody is trying to queue up new packets */ |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1039 | netif_tx_stop_all_queues(bp->dev); |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 1040 | |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 1041 | /* Stop transmission now |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 1042 | * (in case we have just queued new packets) |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1043 | * macb/gem must be halted to write TBQP register |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 1044 | */ |
| 1045 | if (macb_halt_tx(bp)) |
| 1046 | /* Just complain for now, reinitializing TX path can be good */ |
| 1047 | netdev_err(bp->dev, "BUG: halt tx timed out\n"); |
| 1048 | |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 1049 | /* Treat frames in TX queue including the ones that caused the error. |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 1050 | * Free transmit buffers in upper layer. |
| 1051 | */ |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1052 | for (tail = queue->tx_tail; tail != queue->tx_head; tail++) { |
| 1053 | u32 ctrl; |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 1054 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1055 | desc = macb_tx_desc(queue, tail); |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 1056 | ctrl = desc->ctrl; |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1057 | tx_skb = macb_tx_skb(queue, tail); |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 1058 | skb = tx_skb->skb; |
| 1059 | |
| 1060 | if (ctrl & MACB_BIT(TX_USED)) { |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1061 | /* skb is set for the last buffer of the frame */ |
| 1062 | while (!skb) { |
| 1063 | macb_tx_unmap(bp, tx_skb); |
| 1064 | tail++; |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1065 | tx_skb = macb_tx_skb(queue, tail); |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1066 | skb = tx_skb->skb; |
| 1067 | } |
| 1068 | |
| 1069 | /* ctrl still refers to the first buffer descriptor |
| 1070 | * since it's the only one written back by the hardware |
| 1071 | */ |
| 1072 | if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) { |
| 1073 | netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n", |
Zach Brown | b410d13 | 2016-10-19 09:56:57 -0500 | [diff] [blame] | 1074 | macb_tx_ring_wrap(bp, tail), |
| 1075 | skb->data); |
Tobias Klauser | 5f1d3a5 | 2017-04-07 10:17:30 +0200 | [diff] [blame] | 1076 | bp->dev->stats.tx_packets++; |
Rafal Ozieblo | 512286b | 2017-11-30 18:19:56 +0000 | [diff] [blame] | 1077 | queue->stats.tx_packets++; |
Tobias Klauser | 5f1d3a5 | 2017-04-07 10:17:30 +0200 | [diff] [blame] | 1078 | bp->dev->stats.tx_bytes += skb->len; |
Rafal Ozieblo | 512286b | 2017-11-30 18:19:56 +0000 | [diff] [blame] | 1079 | queue->stats.tx_bytes += skb->len; |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1080 | } |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 1081 | } else { |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 1082 | /* "Buffers exhausted mid-frame" errors may only happen |
| 1083 | * if the driver is buggy, so complain loudly about |
| 1084 | * those. Statistics are updated by hardware. |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 1085 | */ |
| 1086 | if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED)) |
| 1087 | netdev_err(bp->dev, |
| 1088 | "BUG: TX buffers exhausted mid-frame\n"); |
| 1089 | |
| 1090 | desc->ctrl = ctrl | MACB_BIT(TX_USED); |
| 1091 | } |
| 1092 | |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1093 | macb_tx_unmap(bp, tx_skb); |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 1094 | } |
| 1095 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1096 | /* Set end of TX queue */ |
| 1097 | desc = macb_tx_desc(queue, 0); |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 1098 | macb_set_addr(bp, desc, 0); |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1099 | desc->ctrl = MACB_BIT(TX_USED); |
| 1100 | |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 1101 | /* Make descriptor updates visible to hardware */ |
| 1102 | wmb(); |
| 1103 | |
| 1104 | /* Reinitialize the TX desc queue */ |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 1105 | queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma)); |
Harini Katakam | fff8019 | 2016-08-09 13:15:53 +0530 | [diff] [blame] | 1106 | #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT |
Rafal Ozieblo | 7b42961 | 2017-06-29 07:12:51 +0100 | [diff] [blame] | 1107 | if (bp->hw_dma_cap & HW_DMA_CAP_64B) |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 1108 | queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma)); |
Harini Katakam | fff8019 | 2016-08-09 13:15:53 +0530 | [diff] [blame] | 1109 | #endif |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 1110 | /* Make TX ring reflect state of hardware */ |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1111 | queue->tx_head = 0; |
| 1112 | queue->tx_tail = 0; |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 1113 | |
| 1114 | /* Housework before enabling TX IRQ */ |
| 1115 | macb_writel(bp, TSR, macb_readl(bp, TSR)); |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1116 | queue_writel(queue, IER, MACB_TX_INT_FLAGS); |
| 1117 | |
| 1118 | /* Now we are ready to start transmission again */ |
| 1119 | netif_tx_start_all_queues(bp->dev); |
| 1120 | macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART)); |
| 1121 | |
| 1122 | spin_unlock_irqrestore(&bp->lock, flags); |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1125 | static void macb_tx_interrupt(struct macb_queue *queue) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1126 | { |
| 1127 | unsigned int tail; |
| 1128 | unsigned int head; |
| 1129 | u32 status; |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1130 | struct macb *bp = queue->bp; |
| 1131 | u16 queue_index = queue - bp->queues; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1132 | |
| 1133 | status = macb_readl(bp, TSR); |
| 1134 | macb_writel(bp, TSR, status); |
| 1135 | |
Nicolas Ferre | 581df9e | 2013-05-14 03:00:16 +0000 | [diff] [blame] | 1136 | if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE) |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1137 | queue_writel(queue, ISR, MACB_BIT(TCOMP)); |
Steffen Trumtrar | 749a2b6 | 2013-03-27 23:07:05 +0000 | [diff] [blame] | 1138 | |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 1139 | netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n", |
Moritz Fischer | aa50b55 | 2016-03-29 19:11:13 -0700 | [diff] [blame] | 1140 | (unsigned long)status); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1141 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1142 | head = queue->tx_head; |
| 1143 | for (tail = queue->tx_tail; tail != head; tail++) { |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 1144 | struct macb_tx_skb *tx_skb; |
| 1145 | struct sk_buff *skb; |
| 1146 | struct macb_dma_desc *desc; |
| 1147 | u32 ctrl; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1148 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1149 | desc = macb_tx_desc(queue, tail); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1150 | |
Havard Skinnemoen | 03dbe05 | 2012-10-31 06:04:51 +0000 | [diff] [blame] | 1151 | /* Make hw descriptor updates visible to CPU */ |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1152 | rmb(); |
Havard Skinnemoen | 03dbe05 | 2012-10-31 06:04:51 +0000 | [diff] [blame] | 1153 | |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 1154 | ctrl = desc->ctrl; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1155 | |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1156 | /* TX_USED bit is only set by hardware on the very first buffer |
| 1157 | * descriptor of the transmitted frame. |
| 1158 | */ |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 1159 | if (!(ctrl & MACB_BIT(TX_USED))) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1160 | break; |
| 1161 | |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1162 | /* Process all buffers of the current transmitted frame */ |
| 1163 | for (;; tail++) { |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1164 | tx_skb = macb_tx_skb(queue, tail); |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1165 | skb = tx_skb->skb; |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 1166 | |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1167 | /* First, update TX stats if needed */ |
| 1168 | if (skb) { |
Paul Thomas | a625204 | 2019-04-08 15:37:54 -0400 | [diff] [blame] | 1169 | if (unlikely(skb_shinfo(skb)->tx_flags & |
| 1170 | SKBTX_HW_TSTAMP) && |
| 1171 | gem_ptp_do_txstamp(queue, skb, desc) == 0) { |
Rafal Ozieblo | ab91f0a | 2017-06-29 07:14:16 +0100 | [diff] [blame] | 1172 | /* skb now belongs to timestamp buffer |
| 1173 | * and will be removed later |
| 1174 | */ |
| 1175 | tx_skb->skb = NULL; |
| 1176 | } |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1177 | netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n", |
Zach Brown | b410d13 | 2016-10-19 09:56:57 -0500 | [diff] [blame] | 1178 | macb_tx_ring_wrap(bp, tail), |
| 1179 | skb->data); |
Tobias Klauser | 5f1d3a5 | 2017-04-07 10:17:30 +0200 | [diff] [blame] | 1180 | bp->dev->stats.tx_packets++; |
Rafal Ozieblo | 512286b | 2017-11-30 18:19:56 +0000 | [diff] [blame] | 1181 | queue->stats.tx_packets++; |
Tobias Klauser | 5f1d3a5 | 2017-04-07 10:17:30 +0200 | [diff] [blame] | 1182 | bp->dev->stats.tx_bytes += skb->len; |
Rafal Ozieblo | 512286b | 2017-11-30 18:19:56 +0000 | [diff] [blame] | 1183 | queue->stats.tx_bytes += skb->len; |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | /* Now we can safely release resources */ |
| 1187 | macb_tx_unmap(bp, tx_skb); |
| 1188 | |
| 1189 | /* skb is set only for the last buffer of the frame. |
| 1190 | * WARNING: at this point skb has been freed by |
| 1191 | * macb_tx_unmap(). |
| 1192 | */ |
| 1193 | if (skb) |
| 1194 | break; |
| 1195 | } |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1196 | } |
| 1197 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1198 | queue->tx_tail = tail; |
| 1199 | if (__netif_subqueue_stopped(bp->dev, queue_index) && |
| 1200 | CIRC_CNT(queue->tx_head, queue->tx_tail, |
Zach Brown | b410d13 | 2016-10-19 09:56:57 -0500 | [diff] [blame] | 1201 | bp->tx_ring_size) <= MACB_TX_WAKEUP_THRESH(bp)) |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1202 | netif_wake_subqueue(bp->dev, queue_index); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1203 | } |
| 1204 | |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 1205 | static void gem_rx_refill(struct macb_queue *queue) |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1206 | { |
| 1207 | unsigned int entry; |
| 1208 | struct sk_buff *skb; |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1209 | dma_addr_t paddr; |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 1210 | struct macb *bp = queue->bp; |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 1211 | struct macb_dma_desc *desc; |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1212 | |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 1213 | while (CIRC_SPACE(queue->rx_prepared_head, queue->rx_tail, |
| 1214 | bp->rx_ring_size) > 0) { |
| 1215 | entry = macb_rx_ring_wrap(bp, queue->rx_prepared_head); |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1216 | |
| 1217 | /* Make hw descriptor updates visible to CPU */ |
| 1218 | rmb(); |
| 1219 | |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 1220 | queue->rx_prepared_head++; |
| 1221 | desc = macb_rx_desc(queue, entry); |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1222 | |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 1223 | if (!queue->rx_skbuff[entry]) { |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1224 | /* allocate sk_buff for this free entry in ring */ |
| 1225 | skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size); |
Moritz Fischer | aa50b55 | 2016-03-29 19:11:13 -0700 | [diff] [blame] | 1226 | if (unlikely(!skb)) { |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1227 | netdev_err(bp->dev, |
| 1228 | "Unable to allocate sk_buff\n"); |
| 1229 | break; |
| 1230 | } |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1231 | |
| 1232 | /* now fill corresponding descriptor entry */ |
| 1233 | paddr = dma_map_single(&bp->pdev->dev, skb->data, |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 1234 | bp->rx_buffer_size, |
| 1235 | DMA_FROM_DEVICE); |
Soren Brinkmann | 9203090 | 2014-03-04 08:46:39 -0800 | [diff] [blame] | 1236 | if (dma_mapping_error(&bp->pdev->dev, paddr)) { |
| 1237 | dev_kfree_skb(skb); |
| 1238 | break; |
| 1239 | } |
| 1240 | |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 1241 | queue->rx_skbuff[entry] = skb; |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1242 | |
Zach Brown | b410d13 | 2016-10-19 09:56:57 -0500 | [diff] [blame] | 1243 | if (entry == bp->rx_ring_size - 1) |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1244 | paddr |= MACB_BIT(RX_WRAP); |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 1245 | desc->ctrl = 0; |
Anssi Hannula | 8159eca | 2018-12-17 15:05:40 +0200 | [diff] [blame] | 1246 | /* Setting addr clears RX_USED and allows reception, |
| 1247 | * make sure ctrl is cleared first to avoid a race. |
| 1248 | */ |
| 1249 | dma_wmb(); |
| 1250 | macb_set_addr(bp, desc, paddr); |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1251 | |
| 1252 | /* properly align Ethernet header */ |
| 1253 | skb_reserve(skb, NET_IP_ALIGN); |
Punnaiah Choudary Kalluri | d4c216c | 2015-04-29 08:34:46 +0530 | [diff] [blame] | 1254 | } else { |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 1255 | desc->ctrl = 0; |
Anssi Hannula | 8159eca | 2018-12-17 15:05:40 +0200 | [diff] [blame] | 1256 | dma_wmb(); |
| 1257 | desc->addr &= ~MACB_BIT(RX_USED); |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1258 | } |
| 1259 | } |
| 1260 | |
| 1261 | /* Make descriptor updates visible to hardware */ |
| 1262 | wmb(); |
| 1263 | |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 1264 | netdev_vdbg(bp->dev, "rx ring: queue: %p, prepared head %d, tail %d\n", |
| 1265 | queue, queue->rx_prepared_head, queue->rx_tail); |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
| 1268 | /* Mark DMA descriptors from begin up to and not including end as unused */ |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 1269 | static void discard_partial_frame(struct macb_queue *queue, unsigned int begin, |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1270 | unsigned int end) |
| 1271 | { |
| 1272 | unsigned int frag; |
| 1273 | |
| 1274 | for (frag = begin; frag != end; frag++) { |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 1275 | struct macb_dma_desc *desc = macb_rx_desc(queue, frag); |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 1276 | |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1277 | desc->addr &= ~MACB_BIT(RX_USED); |
| 1278 | } |
| 1279 | |
| 1280 | /* Make descriptor updates visible to hardware */ |
| 1281 | wmb(); |
| 1282 | |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 1283 | /* When this happens, the hardware stats registers for |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1284 | * whatever caused this is updated, so we don't have to record |
| 1285 | * anything. |
| 1286 | */ |
| 1287 | } |
| 1288 | |
Antoine Tenart | 97236cd | 2019-06-21 17:30:02 +0200 | [diff] [blame] | 1289 | static int gem_rx(struct macb_queue *queue, struct napi_struct *napi, |
| 1290 | int budget) |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1291 | { |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 1292 | struct macb *bp = queue->bp; |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1293 | unsigned int len; |
| 1294 | unsigned int entry; |
| 1295 | struct sk_buff *skb; |
| 1296 | struct macb_dma_desc *desc; |
| 1297 | int count = 0; |
| 1298 | |
| 1299 | while (count < budget) { |
Harini Katakam | fff8019 | 2016-08-09 13:15:53 +0530 | [diff] [blame] | 1300 | u32 ctrl; |
| 1301 | dma_addr_t addr; |
| 1302 | bool rxused; |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1303 | |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 1304 | entry = macb_rx_ring_wrap(bp, queue->rx_tail); |
| 1305 | desc = macb_rx_desc(queue, entry); |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1306 | |
| 1307 | /* Make hw descriptor updates visible to CPU */ |
| 1308 | rmb(); |
| 1309 | |
Harini Katakam | fff8019 | 2016-08-09 13:15:53 +0530 | [diff] [blame] | 1310 | rxused = (desc->addr & MACB_BIT(RX_USED)) ? true : false; |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 1311 | addr = macb_get_addr(bp, desc); |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1312 | |
Harini Katakam | fff8019 | 2016-08-09 13:15:53 +0530 | [diff] [blame] | 1313 | if (!rxused) |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1314 | break; |
| 1315 | |
Anssi Hannula | 6e0af29 | 2018-12-17 15:05:41 +0200 | [diff] [blame] | 1316 | /* Ensure ctrl is at least as up-to-date as rxused */ |
| 1317 | dma_rmb(); |
| 1318 | |
| 1319 | ctrl = desc->ctrl; |
| 1320 | |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 1321 | queue->rx_tail++; |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1322 | count++; |
| 1323 | |
| 1324 | if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) { |
| 1325 | netdev_err(bp->dev, |
| 1326 | "not whole frame pointed by descriptor\n"); |
Tobias Klauser | 5f1d3a5 | 2017-04-07 10:17:30 +0200 | [diff] [blame] | 1327 | bp->dev->stats.rx_dropped++; |
Rafal Ozieblo | 512286b | 2017-11-30 18:19:56 +0000 | [diff] [blame] | 1328 | queue->stats.rx_dropped++; |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1329 | break; |
| 1330 | } |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 1331 | skb = queue->rx_skbuff[entry]; |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1332 | if (unlikely(!skb)) { |
| 1333 | netdev_err(bp->dev, |
| 1334 | "inconsistent Rx descriptor chain\n"); |
Tobias Klauser | 5f1d3a5 | 2017-04-07 10:17:30 +0200 | [diff] [blame] | 1335 | bp->dev->stats.rx_dropped++; |
Rafal Ozieblo | 512286b | 2017-11-30 18:19:56 +0000 | [diff] [blame] | 1336 | queue->stats.rx_dropped++; |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1337 | break; |
| 1338 | } |
| 1339 | /* now everything is ready for receiving packet */ |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 1340 | queue->rx_skbuff[entry] = NULL; |
Harini Katakam | 98b5a0f4 | 2015-05-06 22:27:17 +0530 | [diff] [blame] | 1341 | len = ctrl & bp->rx_frm_len_mask; |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1342 | |
| 1343 | netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len); |
| 1344 | |
| 1345 | skb_put(skb, len); |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1346 | dma_unmap_single(&bp->pdev->dev, addr, |
Soren Brinkmann | 48330e08 | 2014-03-04 08:46:40 -0800 | [diff] [blame] | 1347 | bp->rx_buffer_size, DMA_FROM_DEVICE); |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1348 | |
| 1349 | skb->protocol = eth_type_trans(skb, bp->dev); |
| 1350 | skb_checksum_none_assert(skb); |
Cyrille Pitchen | 924ec53 | 2014-07-24 13:51:01 +0200 | [diff] [blame] | 1351 | if (bp->dev->features & NETIF_F_RXCSUM && |
| 1352 | !(bp->dev->flags & IFF_PROMISC) && |
| 1353 | GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK) |
| 1354 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1355 | |
Tobias Klauser | 5f1d3a5 | 2017-04-07 10:17:30 +0200 | [diff] [blame] | 1356 | bp->dev->stats.rx_packets++; |
Rafal Ozieblo | 512286b | 2017-11-30 18:19:56 +0000 | [diff] [blame] | 1357 | queue->stats.rx_packets++; |
Tobias Klauser | 5f1d3a5 | 2017-04-07 10:17:30 +0200 | [diff] [blame] | 1358 | bp->dev->stats.rx_bytes += skb->len; |
Rafal Ozieblo | 512286b | 2017-11-30 18:19:56 +0000 | [diff] [blame] | 1359 | queue->stats.rx_bytes += skb->len; |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1360 | |
Rafal Ozieblo | ab91f0a | 2017-06-29 07:14:16 +0100 | [diff] [blame] | 1361 | gem_ptp_do_rxstamp(bp, skb, desc); |
| 1362 | |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1363 | #if defined(DEBUG) && defined(VERBOSE_DEBUG) |
| 1364 | netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n", |
| 1365 | skb->len, skb->csum); |
| 1366 | print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1, |
Cyrille Pitchen | 51f8301 | 2014-12-11 11:15:54 +0100 | [diff] [blame] | 1367 | skb_mac_header(skb), 16, true); |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1368 | print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1, |
| 1369 | skb->data, 32, true); |
| 1370 | #endif |
| 1371 | |
Antoine Tenart | 97236cd | 2019-06-21 17:30:02 +0200 | [diff] [blame] | 1372 | napi_gro_receive(napi, skb); |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 1375 | gem_rx_refill(queue); |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1376 | |
| 1377 | return count; |
| 1378 | } |
| 1379 | |
Antoine Tenart | 97236cd | 2019-06-21 17:30:02 +0200 | [diff] [blame] | 1380 | static int macb_rx_frame(struct macb_queue *queue, struct napi_struct *napi, |
| 1381 | unsigned int first_frag, unsigned int last_frag) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1382 | { |
| 1383 | unsigned int len; |
| 1384 | unsigned int frag; |
Havard Skinnemoen | 29bc2e1 | 2012-10-31 06:04:58 +0000 | [diff] [blame] | 1385 | unsigned int offset; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1386 | struct sk_buff *skb; |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 1387 | struct macb_dma_desc *desc; |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 1388 | struct macb *bp = queue->bp; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1389 | |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 1390 | desc = macb_rx_desc(queue, last_frag); |
Harini Katakam | 98b5a0f4 | 2015-05-06 22:27:17 +0530 | [diff] [blame] | 1391 | len = desc->ctrl & bp->rx_frm_len_mask; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1392 | |
Havard Skinnemoen | a268adb | 2012-10-31 06:04:52 +0000 | [diff] [blame] | 1393 | netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n", |
Zach Brown | b410d13 | 2016-10-19 09:56:57 -0500 | [diff] [blame] | 1394 | macb_rx_ring_wrap(bp, first_frag), |
| 1395 | macb_rx_ring_wrap(bp, last_frag), len); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1396 | |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 1397 | /* The ethernet header starts NET_IP_ALIGN bytes into the |
Havard Skinnemoen | 29bc2e1 | 2012-10-31 06:04:58 +0000 | [diff] [blame] | 1398 | * first buffer. Since the header is 14 bytes, this makes the |
| 1399 | * payload word-aligned. |
| 1400 | * |
| 1401 | * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy |
| 1402 | * the two padding bytes into the skb so that we avoid hitting |
| 1403 | * the slowpath in memcpy(), and pull them off afterwards. |
| 1404 | */ |
| 1405 | skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1406 | if (!skb) { |
Tobias Klauser | 5f1d3a5 | 2017-04-07 10:17:30 +0200 | [diff] [blame] | 1407 | bp->dev->stats.rx_dropped++; |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 1408 | for (frag = first_frag; ; frag++) { |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 1409 | desc = macb_rx_desc(queue, frag); |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 1410 | desc->addr &= ~MACB_BIT(RX_USED); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1411 | if (frag == last_frag) |
| 1412 | break; |
| 1413 | } |
Havard Skinnemoen | 03dbe05 | 2012-10-31 06:04:51 +0000 | [diff] [blame] | 1414 | |
| 1415 | /* Make descriptor updates visible to hardware */ |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1416 | wmb(); |
Havard Skinnemoen | 03dbe05 | 2012-10-31 06:04:51 +0000 | [diff] [blame] | 1417 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1418 | return 1; |
| 1419 | } |
| 1420 | |
Havard Skinnemoen | 29bc2e1 | 2012-10-31 06:04:58 +0000 | [diff] [blame] | 1421 | offset = 0; |
| 1422 | len += NET_IP_ALIGN; |
Eric Dumazet | bc8acf2 | 2010-09-02 13:07:41 -0700 | [diff] [blame] | 1423 | skb_checksum_none_assert(skb); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1424 | skb_put(skb, len); |
| 1425 | |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 1426 | for (frag = first_frag; ; frag++) { |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 1427 | unsigned int frag_len = bp->rx_buffer_size; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1428 | |
| 1429 | if (offset + frag_len > len) { |
Cyrille Pitchen | 9ba723b | 2016-03-25 10:37:34 +0100 | [diff] [blame] | 1430 | if (unlikely(frag != last_frag)) { |
| 1431 | dev_kfree_skb_any(skb); |
| 1432 | return -1; |
| 1433 | } |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1434 | frag_len = len - offset; |
| 1435 | } |
Arnaldo Carvalho de Melo | 27d7ff4 | 2007-03-31 11:55:19 -0300 | [diff] [blame] | 1436 | skb_copy_to_linear_data_offset(skb, offset, |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 1437 | macb_rx_buffer(queue, frag), |
Moritz Fischer | aa50b55 | 2016-03-29 19:11:13 -0700 | [diff] [blame] | 1438 | frag_len); |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 1439 | offset += bp->rx_buffer_size; |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 1440 | desc = macb_rx_desc(queue, frag); |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 1441 | desc->addr &= ~MACB_BIT(RX_USED); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1442 | |
| 1443 | if (frag == last_frag) |
| 1444 | break; |
| 1445 | } |
| 1446 | |
Havard Skinnemoen | 03dbe05 | 2012-10-31 06:04:51 +0000 | [diff] [blame] | 1447 | /* Make descriptor updates visible to hardware */ |
| 1448 | wmb(); |
| 1449 | |
Havard Skinnemoen | 29bc2e1 | 2012-10-31 06:04:58 +0000 | [diff] [blame] | 1450 | __skb_pull(skb, NET_IP_ALIGN); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1451 | skb->protocol = eth_type_trans(skb, bp->dev); |
| 1452 | |
Tobias Klauser | 5f1d3a5 | 2017-04-07 10:17:30 +0200 | [diff] [blame] | 1453 | bp->dev->stats.rx_packets++; |
| 1454 | bp->dev->stats.rx_bytes += skb->len; |
Havard Skinnemoen | a268adb | 2012-10-31 06:04:52 +0000 | [diff] [blame] | 1455 | netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n", |
Moritz Fischer | aa50b55 | 2016-03-29 19:11:13 -0700 | [diff] [blame] | 1456 | skb->len, skb->csum); |
Antoine Tenart | 97236cd | 2019-06-21 17:30:02 +0200 | [diff] [blame] | 1457 | napi_gro_receive(napi, skb); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1458 | |
| 1459 | return 0; |
| 1460 | } |
| 1461 | |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 1462 | static inline void macb_init_rx_ring(struct macb_queue *queue) |
Cyrille Pitchen | 9ba723b | 2016-03-25 10:37:34 +0100 | [diff] [blame] | 1463 | { |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 1464 | struct macb *bp = queue->bp; |
Cyrille Pitchen | 9ba723b | 2016-03-25 10:37:34 +0100 | [diff] [blame] | 1465 | dma_addr_t addr; |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 1466 | struct macb_dma_desc *desc = NULL; |
Cyrille Pitchen | 9ba723b | 2016-03-25 10:37:34 +0100 | [diff] [blame] | 1467 | int i; |
| 1468 | |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 1469 | addr = queue->rx_buffers_dma; |
Zach Brown | b410d13 | 2016-10-19 09:56:57 -0500 | [diff] [blame] | 1470 | for (i = 0; i < bp->rx_ring_size; i++) { |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 1471 | desc = macb_rx_desc(queue, i); |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 1472 | macb_set_addr(bp, desc, addr); |
| 1473 | desc->ctrl = 0; |
Cyrille Pitchen | 9ba723b | 2016-03-25 10:37:34 +0100 | [diff] [blame] | 1474 | addr += bp->rx_buffer_size; |
| 1475 | } |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 1476 | desc->addr |= MACB_BIT(RX_WRAP); |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 1477 | queue->rx_tail = 0; |
Cyrille Pitchen | 9ba723b | 2016-03-25 10:37:34 +0100 | [diff] [blame] | 1478 | } |
| 1479 | |
Antoine Tenart | 97236cd | 2019-06-21 17:30:02 +0200 | [diff] [blame] | 1480 | static int macb_rx(struct macb_queue *queue, struct napi_struct *napi, |
| 1481 | int budget) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1482 | { |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 1483 | struct macb *bp = queue->bp; |
Cyrille Pitchen | 9ba723b | 2016-03-25 10:37:34 +0100 | [diff] [blame] | 1484 | bool reset_rx_queue = false; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1485 | int received = 0; |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 1486 | unsigned int tail; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1487 | int first_frag = -1; |
| 1488 | |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 1489 | for (tail = queue->rx_tail; budget > 0; tail++) { |
| 1490 | struct macb_dma_desc *desc = macb_rx_desc(queue, tail); |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 1491 | u32 ctrl; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1492 | |
Havard Skinnemoen | 03dbe05 | 2012-10-31 06:04:51 +0000 | [diff] [blame] | 1493 | /* Make hw descriptor updates visible to CPU */ |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1494 | rmb(); |
Havard Skinnemoen | 03dbe05 | 2012-10-31 06:04:51 +0000 | [diff] [blame] | 1495 | |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 1496 | if (!(desc->addr & MACB_BIT(RX_USED))) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1497 | break; |
| 1498 | |
Anssi Hannula | 6e0af29 | 2018-12-17 15:05:41 +0200 | [diff] [blame] | 1499 | /* Ensure ctrl is at least as up-to-date as addr */ |
| 1500 | dma_rmb(); |
| 1501 | |
| 1502 | ctrl = desc->ctrl; |
| 1503 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1504 | if (ctrl & MACB_BIT(RX_SOF)) { |
| 1505 | if (first_frag != -1) |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 1506 | discard_partial_frame(queue, first_frag, tail); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1507 | first_frag = tail; |
| 1508 | } |
| 1509 | |
| 1510 | if (ctrl & MACB_BIT(RX_EOF)) { |
| 1511 | int dropped; |
Cyrille Pitchen | 9ba723b | 2016-03-25 10:37:34 +0100 | [diff] [blame] | 1512 | |
| 1513 | if (unlikely(first_frag == -1)) { |
| 1514 | reset_rx_queue = true; |
| 1515 | continue; |
| 1516 | } |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1517 | |
Antoine Tenart | 97236cd | 2019-06-21 17:30:02 +0200 | [diff] [blame] | 1518 | dropped = macb_rx_frame(queue, napi, first_frag, tail); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1519 | first_frag = -1; |
Cyrille Pitchen | 9ba723b | 2016-03-25 10:37:34 +0100 | [diff] [blame] | 1520 | if (unlikely(dropped < 0)) { |
| 1521 | reset_rx_queue = true; |
| 1522 | continue; |
| 1523 | } |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1524 | if (!dropped) { |
| 1525 | received++; |
| 1526 | budget--; |
| 1527 | } |
| 1528 | } |
| 1529 | } |
| 1530 | |
Cyrille Pitchen | 9ba723b | 2016-03-25 10:37:34 +0100 | [diff] [blame] | 1531 | if (unlikely(reset_rx_queue)) { |
| 1532 | unsigned long flags; |
| 1533 | u32 ctrl; |
| 1534 | |
| 1535 | netdev_err(bp->dev, "RX queue corruption: reset it\n"); |
| 1536 | |
| 1537 | spin_lock_irqsave(&bp->lock, flags); |
| 1538 | |
| 1539 | ctrl = macb_readl(bp, NCR); |
| 1540 | macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE)); |
| 1541 | |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 1542 | macb_init_rx_ring(queue); |
| 1543 | queue_writel(queue, RBQP, queue->rx_ring_dma); |
Cyrille Pitchen | 9ba723b | 2016-03-25 10:37:34 +0100 | [diff] [blame] | 1544 | |
| 1545 | macb_writel(bp, NCR, ctrl | MACB_BIT(RE)); |
| 1546 | |
| 1547 | spin_unlock_irqrestore(&bp->lock, flags); |
| 1548 | return received; |
| 1549 | } |
| 1550 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1551 | if (first_frag != -1) |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 1552 | queue->rx_tail = first_frag; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1553 | else |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 1554 | queue->rx_tail = tail; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1555 | |
| 1556 | return received; |
| 1557 | } |
| 1558 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 1559 | static int macb_poll(struct napi_struct *napi, int budget) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1560 | { |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 1561 | struct macb_queue *queue = container_of(napi, struct macb_queue, napi); |
| 1562 | struct macb *bp = queue->bp; |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 1563 | int work_done; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1564 | u32 status; |
| 1565 | |
| 1566 | status = macb_readl(bp, RSR); |
| 1567 | macb_writel(bp, RSR, status); |
| 1568 | |
Havard Skinnemoen | a268adb | 2012-10-31 06:04:52 +0000 | [diff] [blame] | 1569 | netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n", |
Moritz Fischer | aa50b55 | 2016-03-29 19:11:13 -0700 | [diff] [blame] | 1570 | (unsigned long)status, budget); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1571 | |
Antoine Tenart | 97236cd | 2019-06-21 17:30:02 +0200 | [diff] [blame] | 1572 | work_done = bp->macbgem_ops.mog_rx(queue, napi, budget); |
Joshua Hoke | b336369 | 2010-10-25 01:44:22 +0000 | [diff] [blame] | 1573 | if (work_done < budget) { |
Eric Dumazet | 6ad2016 | 2017-01-30 08:22:01 -0800 | [diff] [blame] | 1574 | napi_complete_done(napi, work_done); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1575 | |
Nicolas Ferre | 8770e91 | 2013-02-12 11:08:48 +0100 | [diff] [blame] | 1576 | /* Packets received while interrupts were disabled */ |
| 1577 | status = macb_readl(bp, RSR); |
Soren Brinkmann | 504ad98 | 2014-05-04 15:43:01 -0700 | [diff] [blame] | 1578 | if (status) { |
Soren Brinkmann | 02f7a34 | 2014-05-04 15:43:00 -0700 | [diff] [blame] | 1579 | if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE) |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 1580 | queue_writel(queue, ISR, MACB_BIT(RCOMP)); |
Nicolas Ferre | 8770e91 | 2013-02-12 11:08:48 +0100 | [diff] [blame] | 1581 | napi_reschedule(napi); |
Soren Brinkmann | 02f7a34 | 2014-05-04 15:43:00 -0700 | [diff] [blame] | 1582 | } else { |
Harini Katakam | e501070 | 2019-01-29 15:20:03 +0530 | [diff] [blame] | 1583 | queue_writel(queue, IER, bp->rx_intr_mask); |
Soren Brinkmann | 02f7a34 | 2014-05-04 15:43:00 -0700 | [diff] [blame] | 1584 | } |
Joshua Hoke | b336369 | 2010-10-25 01:44:22 +0000 | [diff] [blame] | 1585 | } |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1586 | |
| 1587 | /* TODO: Handle errors */ |
| 1588 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 1589 | return work_done; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1590 | } |
| 1591 | |
Allen Pais | e7412b8 | 2020-09-14 12:59:23 +0530 | [diff] [blame] | 1592 | static void macb_hresp_error_task(struct tasklet_struct *t) |
Harini Katakam | 032dc41 | 2018-01-27 12:09:01 +0530 | [diff] [blame] | 1593 | { |
Allen Pais | e7412b8 | 2020-09-14 12:59:23 +0530 | [diff] [blame] | 1594 | struct macb *bp = from_tasklet(bp, t, hresp_err_tasklet); |
Harini Katakam | 032dc41 | 2018-01-27 12:09:01 +0530 | [diff] [blame] | 1595 | struct net_device *dev = bp->dev; |
Claudiu Beznea | 580d395 | 2020-07-02 12:06:00 +0300 | [diff] [blame] | 1596 | struct macb_queue *queue; |
Harini Katakam | 032dc41 | 2018-01-27 12:09:01 +0530 | [diff] [blame] | 1597 | unsigned int q; |
| 1598 | u32 ctrl; |
| 1599 | |
| 1600 | for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) { |
Harini Katakam | e501070 | 2019-01-29 15:20:03 +0530 | [diff] [blame] | 1601 | queue_writel(queue, IDR, bp->rx_intr_mask | |
Harini Katakam | 032dc41 | 2018-01-27 12:09:01 +0530 | [diff] [blame] | 1602 | MACB_TX_INT_FLAGS | |
| 1603 | MACB_BIT(HRESP)); |
| 1604 | } |
| 1605 | ctrl = macb_readl(bp, NCR); |
| 1606 | ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE)); |
| 1607 | macb_writel(bp, NCR, ctrl); |
| 1608 | |
| 1609 | netif_tx_stop_all_queues(dev); |
| 1610 | netif_carrier_off(dev); |
| 1611 | |
| 1612 | bp->macbgem_ops.mog_init_rings(bp); |
| 1613 | |
| 1614 | /* Initialize TX and RX buffers */ |
Antoine Tenart | 6e952d9 | 2019-11-13 10:00:05 +0100 | [diff] [blame] | 1615 | macb_init_buffers(bp); |
Harini Katakam | 032dc41 | 2018-01-27 12:09:01 +0530 | [diff] [blame] | 1616 | |
Antoine Tenart | 6e952d9 | 2019-11-13 10:00:05 +0100 | [diff] [blame] | 1617 | /* Enable interrupts */ |
| 1618 | for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) |
Harini Katakam | 032dc41 | 2018-01-27 12:09:01 +0530 | [diff] [blame] | 1619 | queue_writel(queue, IER, |
Harini Katakam | e501070 | 2019-01-29 15:20:03 +0530 | [diff] [blame] | 1620 | bp->rx_intr_mask | |
Harini Katakam | 032dc41 | 2018-01-27 12:09:01 +0530 | [diff] [blame] | 1621 | MACB_TX_INT_FLAGS | |
| 1622 | MACB_BIT(HRESP)); |
Harini Katakam | 032dc41 | 2018-01-27 12:09:01 +0530 | [diff] [blame] | 1623 | |
| 1624 | ctrl |= MACB_BIT(RE) | MACB_BIT(TE); |
| 1625 | macb_writel(bp, NCR, ctrl); |
| 1626 | |
| 1627 | netif_carrier_on(dev); |
| 1628 | netif_tx_start_all_queues(dev); |
| 1629 | } |
| 1630 | |
Claudiu Beznea | 4298388 | 2018-12-17 10:02:42 +0000 | [diff] [blame] | 1631 | static void macb_tx_restart(struct macb_queue *queue) |
| 1632 | { |
| 1633 | unsigned int head = queue->tx_head; |
| 1634 | unsigned int tail = queue->tx_tail; |
| 1635 | struct macb *bp = queue->bp; |
| 1636 | |
| 1637 | if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE) |
| 1638 | queue_writel(queue, ISR, MACB_BIT(TXUBR)); |
| 1639 | |
| 1640 | if (head == tail) |
| 1641 | return; |
| 1642 | |
| 1643 | macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART)); |
| 1644 | } |
| 1645 | |
Nicolas Ferre | 9d45c8e | 2020-07-20 10:56:53 +0200 | [diff] [blame] | 1646 | static irqreturn_t macb_wol_interrupt(int irq, void *dev_id) |
| 1647 | { |
| 1648 | struct macb_queue *queue = dev_id; |
| 1649 | struct macb *bp = queue->bp; |
| 1650 | u32 status; |
| 1651 | |
| 1652 | status = queue_readl(queue, ISR); |
| 1653 | |
| 1654 | if (unlikely(!status)) |
| 1655 | return IRQ_NONE; |
| 1656 | |
| 1657 | spin_lock(&bp->lock); |
| 1658 | |
| 1659 | if (status & MACB_BIT(WOL)) { |
| 1660 | queue_writel(queue, IDR, MACB_BIT(WOL)); |
| 1661 | macb_writel(bp, WOL, 0); |
| 1662 | netdev_vdbg(bp->dev, "MACB WoL: queue = %u, isr = 0x%08lx\n", |
| 1663 | (unsigned int)(queue - bp->queues), |
| 1664 | (unsigned long)status); |
| 1665 | if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE) |
| 1666 | queue_writel(queue, ISR, MACB_BIT(WOL)); |
| 1667 | pm_wakeup_event(&bp->pdev->dev, 0); |
| 1668 | } |
| 1669 | |
| 1670 | spin_unlock(&bp->lock); |
| 1671 | |
| 1672 | return IRQ_HANDLED; |
| 1673 | } |
| 1674 | |
Nicolas Ferre | 558e35c | 2020-07-20 10:56:52 +0200 | [diff] [blame] | 1675 | static irqreturn_t gem_wol_interrupt(int irq, void *dev_id) |
| 1676 | { |
| 1677 | struct macb_queue *queue = dev_id; |
| 1678 | struct macb *bp = queue->bp; |
| 1679 | u32 status; |
| 1680 | |
| 1681 | status = queue_readl(queue, ISR); |
| 1682 | |
| 1683 | if (unlikely(!status)) |
| 1684 | return IRQ_NONE; |
| 1685 | |
| 1686 | spin_lock(&bp->lock); |
| 1687 | |
| 1688 | if (status & GEM_BIT(WOL)) { |
| 1689 | queue_writel(queue, IDR, GEM_BIT(WOL)); |
| 1690 | gem_writel(bp, WOL, 0); |
| 1691 | netdev_vdbg(bp->dev, "GEM WoL: queue = %u, isr = 0x%08lx\n", |
| 1692 | (unsigned int)(queue - bp->queues), |
| 1693 | (unsigned long)status); |
| 1694 | if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE) |
| 1695 | queue_writel(queue, ISR, GEM_BIT(WOL)); |
| 1696 | pm_wakeup_event(&bp->pdev->dev, 0); |
| 1697 | } |
| 1698 | |
| 1699 | spin_unlock(&bp->lock); |
| 1700 | |
| 1701 | return IRQ_HANDLED; |
| 1702 | } |
| 1703 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1704 | static irqreturn_t macb_interrupt(int irq, void *dev_id) |
| 1705 | { |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1706 | struct macb_queue *queue = dev_id; |
| 1707 | struct macb *bp = queue->bp; |
| 1708 | struct net_device *dev = bp->dev; |
Nathan Sullivan | bfbb92c | 2015-05-05 15:00:25 -0500 | [diff] [blame] | 1709 | u32 status, ctrl; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1710 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1711 | status = queue_readl(queue, ISR); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1712 | |
| 1713 | if (unlikely(!status)) |
| 1714 | return IRQ_NONE; |
| 1715 | |
| 1716 | spin_lock(&bp->lock); |
| 1717 | |
| 1718 | while (status) { |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1719 | /* close possible race with dev_close */ |
| 1720 | if (unlikely(!netif_running(dev))) { |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1721 | queue_writel(queue, IDR, -1); |
Nathan Sullivan | 2446837 | 2016-01-14 13:27:27 -0600 | [diff] [blame] | 1722 | if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE) |
| 1723 | queue_writel(queue, ISR, -1); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1724 | break; |
| 1725 | } |
| 1726 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1727 | netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n", |
| 1728 | (unsigned int)(queue - bp->queues), |
| 1729 | (unsigned long)status); |
Havard Skinnemoen | a268adb | 2012-10-31 06:04:52 +0000 | [diff] [blame] | 1730 | |
Harini Katakam | e501070 | 2019-01-29 15:20:03 +0530 | [diff] [blame] | 1731 | if (status & bp->rx_intr_mask) { |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 1732 | /* There's no point taking any more interrupts |
Joshua Hoke | b336369 | 2010-10-25 01:44:22 +0000 | [diff] [blame] | 1733 | * until we have processed the buffers. The |
| 1734 | * scheduling call may fail if the poll routine |
| 1735 | * is already scheduled, so disable interrupts |
| 1736 | * now. |
| 1737 | */ |
Harini Katakam | e501070 | 2019-01-29 15:20:03 +0530 | [diff] [blame] | 1738 | queue_writel(queue, IDR, bp->rx_intr_mask); |
Nicolas Ferre | 581df9e | 2013-05-14 03:00:16 +0000 | [diff] [blame] | 1739 | if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE) |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1740 | queue_writel(queue, ISR, MACB_BIT(RCOMP)); |
Joshua Hoke | b336369 | 2010-10-25 01:44:22 +0000 | [diff] [blame] | 1741 | |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 1742 | if (napi_schedule_prep(&queue->napi)) { |
Havard Skinnemoen | a268adb | 2012-10-31 06:04:52 +0000 | [diff] [blame] | 1743 | netdev_vdbg(bp->dev, "scheduling RX softirq\n"); |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 1744 | __napi_schedule(&queue->napi); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1745 | } |
| 1746 | } |
| 1747 | |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 1748 | if (unlikely(status & (MACB_TX_ERR_FLAGS))) { |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1749 | queue_writel(queue, IDR, MACB_TX_INT_FLAGS); |
| 1750 | schedule_work(&queue->tx_error_task); |
Soren Brinkmann | 6a027b7 | 2014-05-04 15:42:59 -0700 | [diff] [blame] | 1751 | |
| 1752 | if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE) |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1753 | queue_writel(queue, ISR, MACB_TX_ERR_FLAGS); |
Soren Brinkmann | 6a027b7 | 2014-05-04 15:42:59 -0700 | [diff] [blame] | 1754 | |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 1755 | break; |
| 1756 | } |
| 1757 | |
| 1758 | if (status & MACB_BIT(TCOMP)) |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1759 | macb_tx_interrupt(queue); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1760 | |
Claudiu Beznea | 4298388 | 2018-12-17 10:02:42 +0000 | [diff] [blame] | 1761 | if (status & MACB_BIT(TXUBR)) |
| 1762 | macb_tx_restart(queue); |
| 1763 | |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 1764 | /* Link change detection isn't possible with RMII, so we'll |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1765 | * add that if/when we get our hands on a full-blown MII PHY. |
| 1766 | */ |
| 1767 | |
Nathan Sullivan | 86b5e7d | 2015-05-13 17:01:36 -0500 | [diff] [blame] | 1768 | /* There is a hardware issue under heavy load where DMA can |
| 1769 | * stop, this causes endless "used buffer descriptor read" |
| 1770 | * interrupts but it can be cleared by re-enabling RX. See |
Harini Katakam | e501070 | 2019-01-29 15:20:03 +0530 | [diff] [blame] | 1771 | * the at91rm9200 manual, section 41.3.1 or the Zynq manual |
| 1772 | * section 16.7.4 for details. RXUBR is only enabled for |
| 1773 | * these two versions. |
Nathan Sullivan | 86b5e7d | 2015-05-13 17:01:36 -0500 | [diff] [blame] | 1774 | */ |
Nathan Sullivan | bfbb92c | 2015-05-05 15:00:25 -0500 | [diff] [blame] | 1775 | if (status & MACB_BIT(RXUBR)) { |
| 1776 | ctrl = macb_readl(bp, NCR); |
| 1777 | macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE)); |
Zumeng Chen | ffac0e9 | 2016-11-28 21:55:00 +0800 | [diff] [blame] | 1778 | wmb(); |
Nathan Sullivan | bfbb92c | 2015-05-05 15:00:25 -0500 | [diff] [blame] | 1779 | macb_writel(bp, NCR, ctrl | MACB_BIT(RE)); |
| 1780 | |
| 1781 | if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE) |
Cyrille Pitchen | ba50499 | 2016-03-24 15:40:04 +0100 | [diff] [blame] | 1782 | queue_writel(queue, ISR, MACB_BIT(RXUBR)); |
Nathan Sullivan | bfbb92c | 2015-05-05 15:00:25 -0500 | [diff] [blame] | 1783 | } |
| 1784 | |
Alexander Stein | b19f7f7 | 2011-04-13 05:03:24 +0000 | [diff] [blame] | 1785 | if (status & MACB_BIT(ISR_ROVR)) { |
| 1786 | /* We missed at least one packet */ |
Jamie Iles | f75ba50 | 2011-11-08 10:12:32 +0000 | [diff] [blame] | 1787 | if (macb_is_gem(bp)) |
| 1788 | bp->hw_stats.gem.rx_overruns++; |
| 1789 | else |
| 1790 | bp->hw_stats.macb.rx_overruns++; |
Soren Brinkmann | 6a027b7 | 2014-05-04 15:42:59 -0700 | [diff] [blame] | 1791 | |
| 1792 | if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE) |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1793 | queue_writel(queue, ISR, MACB_BIT(ISR_ROVR)); |
Alexander Stein | b19f7f7 | 2011-04-13 05:03:24 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1796 | if (status & MACB_BIT(HRESP)) { |
Harini Katakam | 032dc41 | 2018-01-27 12:09:01 +0530 | [diff] [blame] | 1797 | tasklet_schedule(&bp->hresp_err_tasklet); |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 1798 | netdev_err(dev, "DMA bus error: HRESP not OK\n"); |
Soren Brinkmann | 6a027b7 | 2014-05-04 15:42:59 -0700 | [diff] [blame] | 1799 | |
| 1800 | if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE) |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1801 | queue_writel(queue, ISR, MACB_BIT(HRESP)); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1802 | } |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1803 | status = queue_readl(queue, ISR); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1804 | } |
| 1805 | |
| 1806 | spin_unlock(&bp->lock); |
| 1807 | |
| 1808 | return IRQ_HANDLED; |
| 1809 | } |
| 1810 | |
Thomas Petazzoni | 6e8cf5c | 2009-05-04 11:08:41 -0700 | [diff] [blame] | 1811 | #ifdef CONFIG_NET_POLL_CONTROLLER |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 1812 | /* Polling receive - used by netconsole and other diagnostic tools |
Thomas Petazzoni | 6e8cf5c | 2009-05-04 11:08:41 -0700 | [diff] [blame] | 1813 | * to allow network i/o with interrupts disabled. |
| 1814 | */ |
| 1815 | static void macb_poll_controller(struct net_device *dev) |
| 1816 | { |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1817 | struct macb *bp = netdev_priv(dev); |
| 1818 | struct macb_queue *queue; |
Thomas Petazzoni | 6e8cf5c | 2009-05-04 11:08:41 -0700 | [diff] [blame] | 1819 | unsigned long flags; |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1820 | unsigned int q; |
Thomas Petazzoni | 6e8cf5c | 2009-05-04 11:08:41 -0700 | [diff] [blame] | 1821 | |
| 1822 | local_irq_save(flags); |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1823 | for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) |
| 1824 | macb_interrupt(dev->irq, queue); |
Thomas Petazzoni | 6e8cf5c | 2009-05-04 11:08:41 -0700 | [diff] [blame] | 1825 | local_irq_restore(flags); |
| 1826 | } |
| 1827 | #endif |
| 1828 | |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1829 | static unsigned int macb_tx_map(struct macb *bp, |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1830 | struct macb_queue *queue, |
Rafal Ozieblo | 1629dd4 | 2016-11-16 10:02:34 +0000 | [diff] [blame] | 1831 | struct sk_buff *skb, |
| 1832 | unsigned int hdrlen) |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1833 | { |
| 1834 | dma_addr_t mapping; |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1835 | unsigned int len, entry, i, tx_head = queue->tx_head; |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1836 | struct macb_tx_skb *tx_skb = NULL; |
| 1837 | struct macb_dma_desc *desc; |
| 1838 | unsigned int offset, size, count = 0; |
| 1839 | unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags; |
Rafal Ozieblo | 1629dd4 | 2016-11-16 10:02:34 +0000 | [diff] [blame] | 1840 | unsigned int eof = 1, mss_mfs = 0; |
| 1841 | u32 ctrl, lso_ctrl = 0, seq_ctrl = 0; |
| 1842 | |
| 1843 | /* LSO */ |
| 1844 | if (skb_shinfo(skb)->gso_size != 0) { |
| 1845 | if (ip_hdr(skb)->protocol == IPPROTO_UDP) |
| 1846 | /* UDP - UFO */ |
| 1847 | lso_ctrl = MACB_LSO_UFO_ENABLE; |
| 1848 | else |
| 1849 | /* TCP - TSO */ |
| 1850 | lso_ctrl = MACB_LSO_TSO_ENABLE; |
| 1851 | } |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1852 | |
| 1853 | /* First, map non-paged data */ |
| 1854 | len = skb_headlen(skb); |
Rafal Ozieblo | 1629dd4 | 2016-11-16 10:02:34 +0000 | [diff] [blame] | 1855 | |
| 1856 | /* first buffer length */ |
| 1857 | size = hdrlen; |
| 1858 | |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1859 | offset = 0; |
| 1860 | while (len) { |
Zach Brown | b410d13 | 2016-10-19 09:56:57 -0500 | [diff] [blame] | 1861 | entry = macb_tx_ring_wrap(bp, tx_head); |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1862 | tx_skb = &queue->tx_skb[entry]; |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1863 | |
| 1864 | mapping = dma_map_single(&bp->pdev->dev, |
| 1865 | skb->data + offset, |
| 1866 | size, DMA_TO_DEVICE); |
| 1867 | if (dma_mapping_error(&bp->pdev->dev, mapping)) |
| 1868 | goto dma_error; |
| 1869 | |
| 1870 | /* Save info to properly release resources */ |
| 1871 | tx_skb->skb = NULL; |
| 1872 | tx_skb->mapping = mapping; |
| 1873 | tx_skb->size = size; |
| 1874 | tx_skb->mapped_as_page = false; |
| 1875 | |
| 1876 | len -= size; |
| 1877 | offset += size; |
| 1878 | count++; |
| 1879 | tx_head++; |
Rafal Ozieblo | 1629dd4 | 2016-11-16 10:02:34 +0000 | [diff] [blame] | 1880 | |
| 1881 | size = min(len, bp->max_tx_length); |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1882 | } |
| 1883 | |
| 1884 | /* Then, map paged data from fragments */ |
| 1885 | for (f = 0; f < nr_frags; f++) { |
| 1886 | const skb_frag_t *frag = &skb_shinfo(skb)->frags[f]; |
| 1887 | |
| 1888 | len = skb_frag_size(frag); |
| 1889 | offset = 0; |
| 1890 | while (len) { |
| 1891 | size = min(len, bp->max_tx_length); |
Zach Brown | b410d13 | 2016-10-19 09:56:57 -0500 | [diff] [blame] | 1892 | entry = macb_tx_ring_wrap(bp, tx_head); |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1893 | tx_skb = &queue->tx_skb[entry]; |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1894 | |
| 1895 | mapping = skb_frag_dma_map(&bp->pdev->dev, frag, |
| 1896 | offset, size, DMA_TO_DEVICE); |
| 1897 | if (dma_mapping_error(&bp->pdev->dev, mapping)) |
| 1898 | goto dma_error; |
| 1899 | |
| 1900 | /* Save info to properly release resources */ |
| 1901 | tx_skb->skb = NULL; |
| 1902 | tx_skb->mapping = mapping; |
| 1903 | tx_skb->size = size; |
| 1904 | tx_skb->mapped_as_page = true; |
| 1905 | |
| 1906 | len -= size; |
| 1907 | offset += size; |
| 1908 | count++; |
| 1909 | tx_head++; |
| 1910 | } |
| 1911 | } |
| 1912 | |
| 1913 | /* Should never happen */ |
Moritz Fischer | aa50b55 | 2016-03-29 19:11:13 -0700 | [diff] [blame] | 1914 | if (unlikely(!tx_skb)) { |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1915 | netdev_err(bp->dev, "BUG! empty skb!\n"); |
| 1916 | return 0; |
| 1917 | } |
| 1918 | |
| 1919 | /* This is the last buffer of the frame: save socket buffer */ |
| 1920 | tx_skb->skb = skb; |
| 1921 | |
| 1922 | /* Update TX ring: update buffer descriptors in reverse order |
| 1923 | * to avoid race condition |
| 1924 | */ |
| 1925 | |
| 1926 | /* Set 'TX_USED' bit in buffer descriptor at tx_head position |
| 1927 | * to set the end of TX queue |
| 1928 | */ |
| 1929 | i = tx_head; |
Zach Brown | b410d13 | 2016-10-19 09:56:57 -0500 | [diff] [blame] | 1930 | entry = macb_tx_ring_wrap(bp, i); |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1931 | ctrl = MACB_BIT(TX_USED); |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 1932 | desc = macb_tx_desc(queue, entry); |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1933 | desc->ctrl = ctrl; |
| 1934 | |
Rafal Ozieblo | 1629dd4 | 2016-11-16 10:02:34 +0000 | [diff] [blame] | 1935 | if (lso_ctrl) { |
| 1936 | if (lso_ctrl == MACB_LSO_UFO_ENABLE) |
| 1937 | /* include header and FCS in value given to h/w */ |
| 1938 | mss_mfs = skb_shinfo(skb)->gso_size + |
| 1939 | skb_transport_offset(skb) + |
| 1940 | ETH_FCS_LEN; |
| 1941 | else /* TSO */ { |
| 1942 | mss_mfs = skb_shinfo(skb)->gso_size; |
| 1943 | /* TCP Sequence Number Source Select |
| 1944 | * can be set only for TSO |
| 1945 | */ |
| 1946 | seq_ctrl = 0; |
| 1947 | } |
| 1948 | } |
| 1949 | |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1950 | do { |
| 1951 | i--; |
Zach Brown | b410d13 | 2016-10-19 09:56:57 -0500 | [diff] [blame] | 1952 | entry = macb_tx_ring_wrap(bp, i); |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1953 | tx_skb = &queue->tx_skb[entry]; |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 1954 | desc = macb_tx_desc(queue, entry); |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1955 | |
| 1956 | ctrl = (u32)tx_skb->size; |
| 1957 | if (eof) { |
| 1958 | ctrl |= MACB_BIT(TX_LAST); |
| 1959 | eof = 0; |
| 1960 | } |
Zach Brown | b410d13 | 2016-10-19 09:56:57 -0500 | [diff] [blame] | 1961 | if (unlikely(entry == (bp->tx_ring_size - 1))) |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1962 | ctrl |= MACB_BIT(TX_WRAP); |
| 1963 | |
Rafal Ozieblo | 1629dd4 | 2016-11-16 10:02:34 +0000 | [diff] [blame] | 1964 | /* First descriptor is header descriptor */ |
| 1965 | if (i == queue->tx_head) { |
| 1966 | ctrl |= MACB_BF(TX_LSO, lso_ctrl); |
| 1967 | ctrl |= MACB_BF(TX_TCP_SEQ_SRC, seq_ctrl); |
Claudiu Beznea | 653e92a | 2018-08-07 12:25:14 +0300 | [diff] [blame] | 1968 | if ((bp->dev->features & NETIF_F_HW_CSUM) && |
| 1969 | skb->ip_summed != CHECKSUM_PARTIAL && !lso_ctrl) |
| 1970 | ctrl |= MACB_BIT(TX_NOCRC); |
Rafal Ozieblo | 1629dd4 | 2016-11-16 10:02:34 +0000 | [diff] [blame] | 1971 | } else |
| 1972 | /* Only set MSS/MFS on payload descriptors |
| 1973 | * (second or later descriptor) |
| 1974 | */ |
| 1975 | ctrl |= MACB_BF(MSS_MFS, mss_mfs); |
| 1976 | |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1977 | /* Set TX buffer descriptor */ |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 1978 | macb_set_addr(bp, desc, tx_skb->mapping); |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1979 | /* desc->addr must be visible to hardware before clearing |
| 1980 | * 'TX_USED' bit in desc->ctrl. |
| 1981 | */ |
| 1982 | wmb(); |
| 1983 | desc->ctrl = ctrl; |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1984 | } while (i != queue->tx_head); |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1985 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1986 | queue->tx_head = tx_head; |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1987 | |
| 1988 | return count; |
| 1989 | |
| 1990 | dma_error: |
| 1991 | netdev_err(bp->dev, "TX DMA map failed\n"); |
| 1992 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1993 | for (i = queue->tx_head; i != tx_head; i++) { |
| 1994 | tx_skb = macb_tx_skb(queue, i); |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1995 | |
| 1996 | macb_tx_unmap(bp, tx_skb); |
| 1997 | } |
| 1998 | |
| 1999 | return 0; |
| 2000 | } |
| 2001 | |
Rafal Ozieblo | 1629dd4 | 2016-11-16 10:02:34 +0000 | [diff] [blame] | 2002 | static netdev_features_t macb_features_check(struct sk_buff *skb, |
| 2003 | struct net_device *dev, |
| 2004 | netdev_features_t features) |
| 2005 | { |
| 2006 | unsigned int nr_frags, f; |
| 2007 | unsigned int hdrlen; |
| 2008 | |
| 2009 | /* Validate LSO compatibility */ |
| 2010 | |
Harini Katakam | 41c1ef9 | 2020-02-05 18:08:11 +0530 | [diff] [blame] | 2011 | /* there is only one buffer or protocol is not UDP */ |
| 2012 | if (!skb_is_nonlinear(skb) || (ip_hdr(skb)->protocol != IPPROTO_UDP)) |
Rafal Ozieblo | 1629dd4 | 2016-11-16 10:02:34 +0000 | [diff] [blame] | 2013 | return features; |
| 2014 | |
| 2015 | /* length of header */ |
| 2016 | hdrlen = skb_transport_offset(skb); |
Rafal Ozieblo | 1629dd4 | 2016-11-16 10:02:34 +0000 | [diff] [blame] | 2017 | |
Harini Katakam | 41c1ef9 | 2020-02-05 18:08:11 +0530 | [diff] [blame] | 2018 | /* For UFO only: |
Rafal Ozieblo | 1629dd4 | 2016-11-16 10:02:34 +0000 | [diff] [blame] | 2019 | * When software supplies two or more payload buffers all payload buffers |
| 2020 | * apart from the last must be a multiple of 8 bytes in size. |
| 2021 | */ |
| 2022 | if (!IS_ALIGNED(skb_headlen(skb) - hdrlen, MACB_TX_LEN_ALIGN)) |
| 2023 | return features & ~MACB_NETIF_LSO; |
| 2024 | |
| 2025 | nr_frags = skb_shinfo(skb)->nr_frags; |
| 2026 | /* No need to check last fragment */ |
| 2027 | nr_frags--; |
| 2028 | for (f = 0; f < nr_frags; f++) { |
| 2029 | const skb_frag_t *frag = &skb_shinfo(skb)->frags[f]; |
| 2030 | |
| 2031 | if (!IS_ALIGNED(skb_frag_size(frag), MACB_TX_LEN_ALIGN)) |
| 2032 | return features & ~MACB_NETIF_LSO; |
| 2033 | } |
| 2034 | return features; |
| 2035 | } |
| 2036 | |
Helmut Buchsbaum | 007e4ba | 2016-09-04 18:09:47 +0200 | [diff] [blame] | 2037 | static inline int macb_clear_csum(struct sk_buff *skb) |
| 2038 | { |
| 2039 | /* no change for packets without checksum offloading */ |
| 2040 | if (skb->ip_summed != CHECKSUM_PARTIAL) |
| 2041 | return 0; |
| 2042 | |
| 2043 | /* make sure we can modify the header */ |
| 2044 | if (unlikely(skb_cow_head(skb, 0))) |
| 2045 | return -1; |
| 2046 | |
| 2047 | /* initialize checksum field |
| 2048 | * This is required - at least for Zynq, which otherwise calculates |
| 2049 | * wrong UDP header checksums for UDP packets with UDP data len <=2 |
| 2050 | */ |
| 2051 | *(__sum16 *)(skb_checksum_start(skb) + skb->csum_offset) = 0; |
| 2052 | return 0; |
| 2053 | } |
| 2054 | |
Claudiu Beznea | 653e92a | 2018-08-07 12:25:14 +0300 | [diff] [blame] | 2055 | static int macb_pad_and_fcs(struct sk_buff **skb, struct net_device *ndev) |
| 2056 | { |
Mark Deneen | 403dc16 | 2020-10-30 15:58:14 +0000 | [diff] [blame] | 2057 | bool cloned = skb_cloned(*skb) || skb_header_cloned(*skb) || |
| 2058 | skb_is_nonlinear(*skb); |
Claudiu Beznea | 653e92a | 2018-08-07 12:25:14 +0300 | [diff] [blame] | 2059 | int padlen = ETH_ZLEN - (*skb)->len; |
| 2060 | int headroom = skb_headroom(*skb); |
| 2061 | int tailroom = skb_tailroom(*skb); |
| 2062 | struct sk_buff *nskb; |
| 2063 | u32 fcs; |
| 2064 | |
| 2065 | if (!(ndev->features & NETIF_F_HW_CSUM) || |
| 2066 | !((*skb)->ip_summed != CHECKSUM_PARTIAL) || |
| 2067 | skb_shinfo(*skb)->gso_size) /* Not available for GSO */ |
| 2068 | return 0; |
| 2069 | |
| 2070 | if (padlen <= 0) { |
| 2071 | /* FCS could be appeded to tailroom. */ |
| 2072 | if (tailroom >= ETH_FCS_LEN) |
| 2073 | goto add_fcs; |
| 2074 | /* FCS could be appeded by moving data to headroom. */ |
| 2075 | else if (!cloned && headroom + tailroom >= ETH_FCS_LEN) |
| 2076 | padlen = 0; |
| 2077 | /* No room for FCS, need to reallocate skb. */ |
| 2078 | else |
Tristram Ha | 899ecae | 2018-10-24 14:51:23 -0700 | [diff] [blame] | 2079 | padlen = ETH_FCS_LEN; |
Claudiu Beznea | 653e92a | 2018-08-07 12:25:14 +0300 | [diff] [blame] | 2080 | } else { |
| 2081 | /* Add room for FCS. */ |
| 2082 | padlen += ETH_FCS_LEN; |
| 2083 | } |
| 2084 | |
| 2085 | if (!cloned && headroom + tailroom >= padlen) { |
| 2086 | (*skb)->data = memmove((*skb)->head, (*skb)->data, (*skb)->len); |
| 2087 | skb_set_tail_pointer(*skb, (*skb)->len); |
| 2088 | } else { |
| 2089 | nskb = skb_copy_expand(*skb, 0, padlen, GFP_ATOMIC); |
| 2090 | if (!nskb) |
| 2091 | return -ENOMEM; |
| 2092 | |
Huang Zijiang | f3e5c07 | 2019-02-14 14:41:18 +0800 | [diff] [blame] | 2093 | dev_consume_skb_any(*skb); |
Claudiu Beznea | 653e92a | 2018-08-07 12:25:14 +0300 | [diff] [blame] | 2094 | *skb = nskb; |
| 2095 | } |
| 2096 | |
Claudiu Beznea | ba3e184 | 2019-01-03 14:59:35 +0000 | [diff] [blame] | 2097 | if (padlen > ETH_FCS_LEN) |
| 2098 | skb_put_zero(*skb, padlen - ETH_FCS_LEN); |
Claudiu Beznea | 653e92a | 2018-08-07 12:25:14 +0300 | [diff] [blame] | 2099 | |
| 2100 | add_fcs: |
| 2101 | /* set FCS to packet */ |
| 2102 | fcs = crc32_le(~0, (*skb)->data, (*skb)->len); |
| 2103 | fcs = ~fcs; |
| 2104 | |
| 2105 | skb_put_u8(*skb, fcs & 0xff); |
| 2106 | skb_put_u8(*skb, (fcs >> 8) & 0xff); |
| 2107 | skb_put_u8(*skb, (fcs >> 16) & 0xff); |
| 2108 | skb_put_u8(*skb, (fcs >> 24) & 0xff); |
| 2109 | |
| 2110 | return 0; |
| 2111 | } |
| 2112 | |
Claudiu Beznea | d1c3895 | 2018-08-07 12:25:12 +0300 | [diff] [blame] | 2113 | static netdev_tx_t macb_start_xmit(struct sk_buff *skb, struct net_device *dev) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2114 | { |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 2115 | u16 queue_index = skb_get_queue_mapping(skb); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2116 | struct macb *bp = netdev_priv(dev); |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 2117 | struct macb_queue *queue = &bp->queues[queue_index]; |
Dongdong Deng | 4871953 | 2009-08-23 19:49:07 -0700 | [diff] [blame] | 2118 | unsigned long flags; |
Rafal Ozieblo | 1629dd4 | 2016-11-16 10:02:34 +0000 | [diff] [blame] | 2119 | unsigned int desc_cnt, nr_frags, frag_size, f; |
| 2120 | unsigned int hdrlen; |
Claudiu Beznea | 8932b5a | 2020-07-02 12:06:01 +0300 | [diff] [blame] | 2121 | bool is_lso; |
Claudiu Beznea | d1c3895 | 2018-08-07 12:25:12 +0300 | [diff] [blame] | 2122 | netdev_tx_t ret = NETDEV_TX_OK; |
Rafal Ozieblo | 1629dd4 | 2016-11-16 10:02:34 +0000 | [diff] [blame] | 2123 | |
Claudiu Beznea | 33729f2 | 2018-08-07 12:25:13 +0300 | [diff] [blame] | 2124 | if (macb_clear_csum(skb)) { |
| 2125 | dev_kfree_skb_any(skb); |
| 2126 | return ret; |
| 2127 | } |
| 2128 | |
Claudiu Beznea | 653e92a | 2018-08-07 12:25:14 +0300 | [diff] [blame] | 2129 | if (macb_pad_and_fcs(&skb, dev)) { |
| 2130 | dev_kfree_skb_any(skb); |
| 2131 | return ret; |
| 2132 | } |
| 2133 | |
Rafal Ozieblo | 1629dd4 | 2016-11-16 10:02:34 +0000 | [diff] [blame] | 2134 | is_lso = (skb_shinfo(skb)->gso_size != 0); |
| 2135 | |
| 2136 | if (is_lso) { |
Rafal Ozieblo | 1629dd4 | 2016-11-16 10:02:34 +0000 | [diff] [blame] | 2137 | /* length of headers */ |
Claudiu Beznea | 8932b5a | 2020-07-02 12:06:01 +0300 | [diff] [blame] | 2138 | if (ip_hdr(skb)->protocol == IPPROTO_UDP) |
Rafal Ozieblo | 1629dd4 | 2016-11-16 10:02:34 +0000 | [diff] [blame] | 2139 | /* only queue eth + ip headers separately for UDP */ |
| 2140 | hdrlen = skb_transport_offset(skb); |
| 2141 | else |
| 2142 | hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb); |
| 2143 | if (skb_headlen(skb) < hdrlen) { |
| 2144 | netdev_err(bp->dev, "Error - LSO headers fragmented!!!\n"); |
| 2145 | /* if this is required, would need to copy to single buffer */ |
| 2146 | return NETDEV_TX_BUSY; |
| 2147 | } |
| 2148 | } else |
| 2149 | hdrlen = min(skb_headlen(skb), bp->max_tx_length); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2150 | |
Havard Skinnemoen | a268adb | 2012-10-31 06:04:52 +0000 | [diff] [blame] | 2151 | #if defined(DEBUG) && defined(VERBOSE_DEBUG) |
| 2152 | netdev_vdbg(bp->dev, |
Moritz Fischer | aa50b55 | 2016-03-29 19:11:13 -0700 | [diff] [blame] | 2153 | "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n", |
| 2154 | queue_index, skb->len, skb->head, skb->data, |
| 2155 | skb_tail_pointer(skb), skb_end_pointer(skb)); |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 2156 | print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1, |
| 2157 | skb->data, 16, true); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2158 | #endif |
| 2159 | |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 2160 | /* Count how many TX buffer descriptors are needed to send this |
| 2161 | * socket buffer: skb fragments of jumbo frames may need to be |
Moritz Fischer | aa50b55 | 2016-03-29 19:11:13 -0700 | [diff] [blame] | 2162 | * split into many buffer descriptors. |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 2163 | */ |
Rafal Ozieblo | 1629dd4 | 2016-11-16 10:02:34 +0000 | [diff] [blame] | 2164 | if (is_lso && (skb_headlen(skb) > hdrlen)) |
| 2165 | /* extra header descriptor if also payload in first buffer */ |
| 2166 | desc_cnt = DIV_ROUND_UP((skb_headlen(skb) - hdrlen), bp->max_tx_length) + 1; |
| 2167 | else |
| 2168 | desc_cnt = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length); |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 2169 | nr_frags = skb_shinfo(skb)->nr_frags; |
| 2170 | for (f = 0; f < nr_frags; f++) { |
| 2171 | frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]); |
Rafal Ozieblo | 1629dd4 | 2016-11-16 10:02:34 +0000 | [diff] [blame] | 2172 | desc_cnt += DIV_ROUND_UP(frag_size, bp->max_tx_length); |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 2173 | } |
| 2174 | |
Dongdong Deng | 4871953 | 2009-08-23 19:49:07 -0700 | [diff] [blame] | 2175 | spin_lock_irqsave(&bp->lock, flags); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2176 | |
| 2177 | /* This is a hard error, log it. */ |
Zach Brown | b410d13 | 2016-10-19 09:56:57 -0500 | [diff] [blame] | 2178 | if (CIRC_SPACE(queue->tx_head, queue->tx_tail, |
Rafal Ozieblo | 1629dd4 | 2016-11-16 10:02:34 +0000 | [diff] [blame] | 2179 | bp->tx_ring_size) < desc_cnt) { |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 2180 | netif_stop_subqueue(dev, queue_index); |
Dongdong Deng | 4871953 | 2009-08-23 19:49:07 -0700 | [diff] [blame] | 2181 | spin_unlock_irqrestore(&bp->lock, flags); |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 2182 | netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n", |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 2183 | queue->tx_head, queue->tx_tail); |
Patrick McHardy | 5b54814 | 2009-06-12 06:22:29 +0000 | [diff] [blame] | 2184 | return NETDEV_TX_BUSY; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2185 | } |
| 2186 | |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 2187 | /* Map socket buffer for DMA transfer */ |
Rafal Ozieblo | 1629dd4 | 2016-11-16 10:02:34 +0000 | [diff] [blame] | 2188 | if (!macb_tx_map(bp, queue, skb, hdrlen)) { |
Eric W. Biederman | c88b5b6 | 2014-03-15 16:08:27 -0700 | [diff] [blame] | 2189 | dev_kfree_skb_any(skb); |
Soren Brinkmann | 9203090 | 2014-03-04 08:46:39 -0800 | [diff] [blame] | 2190 | goto unlock; |
| 2191 | } |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 2192 | |
Havard Skinnemoen | 03dbe05 | 2012-10-31 06:04:51 +0000 | [diff] [blame] | 2193 | /* Make newly initialized descriptor visible to hardware */ |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2194 | wmb(); |
Richard Cochran | e072092 | 2011-06-19 21:51:28 +0000 | [diff] [blame] | 2195 | skb_tx_timestamp(skb); |
| 2196 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2197 | macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART)); |
| 2198 | |
Zach Brown | b410d13 | 2016-10-19 09:56:57 -0500 | [diff] [blame] | 2199 | if (CIRC_SPACE(queue->tx_head, queue->tx_tail, bp->tx_ring_size) < 1) |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 2200 | netif_stop_subqueue(dev, queue_index); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2201 | |
Soren Brinkmann | 9203090 | 2014-03-04 08:46:39 -0800 | [diff] [blame] | 2202 | unlock: |
Dongdong Deng | 4871953 | 2009-08-23 19:49:07 -0700 | [diff] [blame] | 2203 | spin_unlock_irqrestore(&bp->lock, flags); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2204 | |
Claudiu Beznea | d1c3895 | 2018-08-07 12:25:12 +0300 | [diff] [blame] | 2205 | return ret; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2206 | } |
| 2207 | |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 2208 | static void macb_init_rx_buffer_size(struct macb *bp, size_t size) |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 2209 | { |
| 2210 | if (!macb_is_gem(bp)) { |
| 2211 | bp->rx_buffer_size = MACB_RX_BUFFER_SIZE; |
| 2212 | } else { |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 2213 | bp->rx_buffer_size = size; |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 2214 | |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 2215 | if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) { |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 2216 | netdev_dbg(bp->dev, |
Moritz Fischer | aa50b55 | 2016-03-29 19:11:13 -0700 | [diff] [blame] | 2217 | "RX buffer must be multiple of %d bytes, expanding\n", |
| 2218 | RX_BUFFER_MULTIPLE); |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 2219 | bp->rx_buffer_size = |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 2220 | roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE); |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 2221 | } |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 2222 | } |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 2223 | |
Alexey Dobriyan | 5b5e092 | 2017-02-27 14:30:02 -0800 | [diff] [blame] | 2224 | netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%zu]\n", |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 2225 | bp->dev->mtu, bp->rx_buffer_size); |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 2226 | } |
| 2227 | |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 2228 | static void gem_free_rx_buffers(struct macb *bp) |
| 2229 | { |
| 2230 | struct sk_buff *skb; |
| 2231 | struct macb_dma_desc *desc; |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 2232 | struct macb_queue *queue; |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 2233 | dma_addr_t addr; |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 2234 | unsigned int q; |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 2235 | int i; |
| 2236 | |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 2237 | for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) { |
| 2238 | if (!queue->rx_skbuff) |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 2239 | continue; |
| 2240 | |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 2241 | for (i = 0; i < bp->rx_ring_size; i++) { |
| 2242 | skb = queue->rx_skbuff[i]; |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 2243 | |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 2244 | if (!skb) |
| 2245 | continue; |
| 2246 | |
| 2247 | desc = macb_rx_desc(queue, i); |
| 2248 | addr = macb_get_addr(bp, desc); |
| 2249 | |
| 2250 | dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size, |
| 2251 | DMA_FROM_DEVICE); |
| 2252 | dev_kfree_skb_any(skb); |
| 2253 | skb = NULL; |
| 2254 | } |
| 2255 | |
| 2256 | kfree(queue->rx_skbuff); |
| 2257 | queue->rx_skbuff = NULL; |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 2258 | } |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 2259 | } |
| 2260 | |
| 2261 | static void macb_free_rx_buffers(struct macb *bp) |
| 2262 | { |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 2263 | struct macb_queue *queue = &bp->queues[0]; |
| 2264 | |
| 2265 | if (queue->rx_buffers) { |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 2266 | dma_free_coherent(&bp->pdev->dev, |
Zach Brown | b410d13 | 2016-10-19 09:56:57 -0500 | [diff] [blame] | 2267 | bp->rx_ring_size * bp->rx_buffer_size, |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 2268 | queue->rx_buffers, queue->rx_buffers_dma); |
| 2269 | queue->rx_buffers = NULL; |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 2270 | } |
| 2271 | } |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 2272 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2273 | static void macb_free_consistent(struct macb *bp) |
| 2274 | { |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 2275 | struct macb_queue *queue; |
| 2276 | unsigned int q; |
Harini Katakam | 404cd08 | 2018-07-06 12:18:58 +0530 | [diff] [blame] | 2277 | int size; |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 2278 | |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 2279 | bp->macbgem_ops.mog_free_rx_buffers(bp); |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 2280 | |
| 2281 | for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) { |
| 2282 | kfree(queue->tx_skb); |
| 2283 | queue->tx_skb = NULL; |
| 2284 | if (queue->tx_ring) { |
Harini Katakam | 404cd08 | 2018-07-06 12:18:58 +0530 | [diff] [blame] | 2285 | size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch; |
| 2286 | dma_free_coherent(&bp->pdev->dev, size, |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 2287 | queue->tx_ring, queue->tx_ring_dma); |
| 2288 | queue->tx_ring = NULL; |
| 2289 | } |
Harini Katakam | e50b770 | 2018-07-06 12:18:57 +0530 | [diff] [blame] | 2290 | if (queue->rx_ring) { |
Harini Katakam | 404cd08 | 2018-07-06 12:18:58 +0530 | [diff] [blame] | 2291 | size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch; |
| 2292 | dma_free_coherent(&bp->pdev->dev, size, |
Harini Katakam | e50b770 | 2018-07-06 12:18:57 +0530 | [diff] [blame] | 2293 | queue->rx_ring, queue->rx_ring_dma); |
| 2294 | queue->rx_ring = NULL; |
| 2295 | } |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2296 | } |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 2297 | } |
| 2298 | |
| 2299 | static int gem_alloc_rx_buffers(struct macb *bp) |
| 2300 | { |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 2301 | struct macb_queue *queue; |
| 2302 | unsigned int q; |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 2303 | int size; |
| 2304 | |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 2305 | for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) { |
| 2306 | size = bp->rx_ring_size * sizeof(struct sk_buff *); |
| 2307 | queue->rx_skbuff = kzalloc(size, GFP_KERNEL); |
| 2308 | if (!queue->rx_skbuff) |
| 2309 | return -ENOMEM; |
| 2310 | else |
| 2311 | netdev_dbg(bp->dev, |
| 2312 | "Allocated %d RX struct sk_buff entries at %p\n", |
| 2313 | bp->rx_ring_size, queue->rx_skbuff); |
| 2314 | } |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 2315 | return 0; |
| 2316 | } |
| 2317 | |
| 2318 | static int macb_alloc_rx_buffers(struct macb *bp) |
| 2319 | { |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 2320 | struct macb_queue *queue = &bp->queues[0]; |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 2321 | int size; |
| 2322 | |
Zach Brown | b410d13 | 2016-10-19 09:56:57 -0500 | [diff] [blame] | 2323 | size = bp->rx_ring_size * bp->rx_buffer_size; |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 2324 | queue->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size, |
| 2325 | &queue->rx_buffers_dma, GFP_KERNEL); |
| 2326 | if (!queue->rx_buffers) |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 2327 | return -ENOMEM; |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 2328 | |
| 2329 | netdev_dbg(bp->dev, |
| 2330 | "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n", |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 2331 | size, (unsigned long)queue->rx_buffers_dma, queue->rx_buffers); |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 2332 | return 0; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2333 | } |
| 2334 | |
| 2335 | static int macb_alloc_consistent(struct macb *bp) |
| 2336 | { |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 2337 | struct macb_queue *queue; |
| 2338 | unsigned int q; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2339 | int size; |
| 2340 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 2341 | for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) { |
Harini Katakam | 404cd08 | 2018-07-06 12:18:58 +0530 | [diff] [blame] | 2342 | size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch; |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 2343 | queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size, |
| 2344 | &queue->tx_ring_dma, |
| 2345 | GFP_KERNEL); |
| 2346 | if (!queue->tx_ring) |
| 2347 | goto out_err; |
| 2348 | netdev_dbg(bp->dev, |
| 2349 | "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n", |
| 2350 | q, size, (unsigned long)queue->tx_ring_dma, |
| 2351 | queue->tx_ring); |
| 2352 | |
Zach Brown | b410d13 | 2016-10-19 09:56:57 -0500 | [diff] [blame] | 2353 | size = bp->tx_ring_size * sizeof(struct macb_tx_skb); |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 2354 | queue->tx_skb = kmalloc(size, GFP_KERNEL); |
| 2355 | if (!queue->tx_skb) |
| 2356 | goto out_err; |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 2357 | |
Harini Katakam | 404cd08 | 2018-07-06 12:18:58 +0530 | [diff] [blame] | 2358 | size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch; |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 2359 | queue->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size, |
| 2360 | &queue->rx_ring_dma, GFP_KERNEL); |
| 2361 | if (!queue->rx_ring) |
| 2362 | goto out_err; |
| 2363 | netdev_dbg(bp->dev, |
| 2364 | "Allocated RX ring of %d bytes at %08lx (mapped %p)\n", |
| 2365 | size, (unsigned long)queue->rx_ring_dma, queue->rx_ring); |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 2366 | } |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 2367 | if (bp->macbgem_ops.mog_alloc_rx_buffers(bp)) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2368 | goto out_err; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2369 | |
| 2370 | return 0; |
| 2371 | |
| 2372 | out_err: |
| 2373 | macb_free_consistent(bp); |
| 2374 | return -ENOMEM; |
| 2375 | } |
| 2376 | |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 2377 | static void gem_init_rings(struct macb *bp) |
| 2378 | { |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 2379 | struct macb_queue *queue; |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 2380 | struct macb_dma_desc *desc = NULL; |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 2381 | unsigned int q; |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 2382 | int i; |
| 2383 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 2384 | for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) { |
Zach Brown | b410d13 | 2016-10-19 09:56:57 -0500 | [diff] [blame] | 2385 | for (i = 0; i < bp->tx_ring_size; i++) { |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 2386 | desc = macb_tx_desc(queue, i); |
| 2387 | macb_set_addr(bp, desc, 0); |
| 2388 | desc->ctrl = MACB_BIT(TX_USED); |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 2389 | } |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 2390 | desc->ctrl |= MACB_BIT(TX_WRAP); |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 2391 | queue->tx_head = 0; |
| 2392 | queue->tx_tail = 0; |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 2393 | |
| 2394 | queue->rx_tail = 0; |
| 2395 | queue->rx_prepared_head = 0; |
| 2396 | |
| 2397 | gem_rx_refill(queue); |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 2398 | } |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 2399 | |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 2400 | } |
| 2401 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2402 | static void macb_init_rings(struct macb *bp) |
| 2403 | { |
| 2404 | int i; |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 2405 | struct macb_dma_desc *desc = NULL; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2406 | |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 2407 | macb_init_rx_ring(&bp->queues[0]); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2408 | |
Zach Brown | b410d13 | 2016-10-19 09:56:57 -0500 | [diff] [blame] | 2409 | for (i = 0; i < bp->tx_ring_size; i++) { |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 2410 | desc = macb_tx_desc(&bp->queues[0], i); |
| 2411 | macb_set_addr(bp, desc, 0); |
| 2412 | desc->ctrl = MACB_BIT(TX_USED); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2413 | } |
Ben Shelton | 21d3515 | 2015-04-22 17:28:54 -0500 | [diff] [blame] | 2414 | bp->queues[0].tx_head = 0; |
| 2415 | bp->queues[0].tx_tail = 0; |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 2416 | desc->ctrl |= MACB_BIT(TX_WRAP); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2417 | } |
| 2418 | |
| 2419 | static void macb_reset_hw(struct macb *bp) |
| 2420 | { |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 2421 | struct macb_queue *queue; |
| 2422 | unsigned int q; |
Anssi Hannula | 0da70f8 | 2018-08-23 10:45:22 +0300 | [diff] [blame] | 2423 | u32 ctrl = macb_readl(bp, NCR); |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 2424 | |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 2425 | /* Disable RX and TX (XXX: Should we halt the transmission |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2426 | * more gracefully?) |
| 2427 | */ |
Anssi Hannula | 0da70f8 | 2018-08-23 10:45:22 +0300 | [diff] [blame] | 2428 | ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE)); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2429 | |
| 2430 | /* Clear the stats registers (XXX: Update stats first?) */ |
Anssi Hannula | 0da70f8 | 2018-08-23 10:45:22 +0300 | [diff] [blame] | 2431 | ctrl |= MACB_BIT(CLRSTAT); |
| 2432 | |
| 2433 | macb_writel(bp, NCR, ctrl); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2434 | |
| 2435 | /* Clear all status flags */ |
Joachim Eastwood | 95ebcea | 2012-10-22 08:45:31 +0000 | [diff] [blame] | 2436 | macb_writel(bp, TSR, -1); |
| 2437 | macb_writel(bp, RSR, -1); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2438 | |
| 2439 | /* Disable all interrupts */ |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 2440 | for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) { |
| 2441 | queue_writel(queue, IDR, -1); |
| 2442 | queue_readl(queue, ISR); |
Nathan Sullivan | 2446837 | 2016-01-14 13:27:27 -0600 | [diff] [blame] | 2443 | if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE) |
| 2444 | queue_writel(queue, ISR, -1); |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 2445 | } |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2446 | } |
| 2447 | |
Jamie Iles | 70c9f3d | 2011-03-09 16:22:54 +0000 | [diff] [blame] | 2448 | static u32 gem_mdc_clk_div(struct macb *bp) |
| 2449 | { |
| 2450 | u32 config; |
| 2451 | unsigned long pclk_hz = clk_get_rate(bp->pclk); |
| 2452 | |
| 2453 | if (pclk_hz <= 20000000) |
| 2454 | config = GEM_BF(CLK, GEM_CLK_DIV8); |
| 2455 | else if (pclk_hz <= 40000000) |
| 2456 | config = GEM_BF(CLK, GEM_CLK_DIV16); |
| 2457 | else if (pclk_hz <= 80000000) |
| 2458 | config = GEM_BF(CLK, GEM_CLK_DIV32); |
| 2459 | else if (pclk_hz <= 120000000) |
| 2460 | config = GEM_BF(CLK, GEM_CLK_DIV48); |
| 2461 | else if (pclk_hz <= 160000000) |
| 2462 | config = GEM_BF(CLK, GEM_CLK_DIV64); |
| 2463 | else |
| 2464 | config = GEM_BF(CLK, GEM_CLK_DIV96); |
| 2465 | |
| 2466 | return config; |
| 2467 | } |
| 2468 | |
| 2469 | static u32 macb_mdc_clk_div(struct macb *bp) |
| 2470 | { |
| 2471 | u32 config; |
| 2472 | unsigned long pclk_hz; |
| 2473 | |
| 2474 | if (macb_is_gem(bp)) |
| 2475 | return gem_mdc_clk_div(bp); |
| 2476 | |
| 2477 | pclk_hz = clk_get_rate(bp->pclk); |
| 2478 | if (pclk_hz <= 20000000) |
| 2479 | config = MACB_BF(CLK, MACB_CLK_DIV8); |
| 2480 | else if (pclk_hz <= 40000000) |
| 2481 | config = MACB_BF(CLK, MACB_CLK_DIV16); |
| 2482 | else if (pclk_hz <= 80000000) |
| 2483 | config = MACB_BF(CLK, MACB_CLK_DIV32); |
| 2484 | else |
| 2485 | config = MACB_BF(CLK, MACB_CLK_DIV64); |
| 2486 | |
| 2487 | return config; |
| 2488 | } |
| 2489 | |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 2490 | /* Get the DMA bus width field of the network configuration register that we |
Jamie Iles | 757a03c | 2011-03-09 16:29:59 +0000 | [diff] [blame] | 2491 | * should program. We find the width from decoding the design configuration |
| 2492 | * register to find the maximum supported data bus width. |
| 2493 | */ |
| 2494 | static u32 macb_dbw(struct macb *bp) |
| 2495 | { |
| 2496 | if (!macb_is_gem(bp)) |
| 2497 | return 0; |
| 2498 | |
| 2499 | switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) { |
| 2500 | case 4: |
| 2501 | return GEM_BF(DBW, GEM_DBW128); |
| 2502 | case 2: |
| 2503 | return GEM_BF(DBW, GEM_DBW64); |
| 2504 | case 1: |
| 2505 | default: |
| 2506 | return GEM_BF(DBW, GEM_DBW32); |
| 2507 | } |
| 2508 | } |
| 2509 | |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 2510 | /* Configure the receive DMA engine |
Nicolas Ferre | b3e3bd71 | 2012-11-23 03:49:01 +0000 | [diff] [blame] | 2511 | * - use the correct receive buffer size |
Nicolas Ferre | e175587 | 2014-07-24 13:50:58 +0200 | [diff] [blame] | 2512 | * - set best burst length for DMA operations |
Nicolas Ferre | b3e3bd71 | 2012-11-23 03:49:01 +0000 | [diff] [blame] | 2513 | * (if not supported by FIFO, it will fallback to default) |
| 2514 | * - set both rx/tx packet buffers to full memory size |
| 2515 | * These are configurable parameters for GEM. |
Jamie Iles | 0116da4 | 2011-03-14 17:38:30 +0000 | [diff] [blame] | 2516 | */ |
| 2517 | static void macb_configure_dma(struct macb *bp) |
| 2518 | { |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 2519 | struct macb_queue *queue; |
| 2520 | u32 buffer_size; |
| 2521 | unsigned int q; |
Jamie Iles | 0116da4 | 2011-03-14 17:38:30 +0000 | [diff] [blame] | 2522 | u32 dmacfg; |
| 2523 | |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 2524 | buffer_size = bp->rx_buffer_size / RX_BUFFER_MULTIPLE; |
Jamie Iles | 0116da4 | 2011-03-14 17:38:30 +0000 | [diff] [blame] | 2525 | if (macb_is_gem(bp)) { |
| 2526 | dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L); |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 2527 | for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) { |
| 2528 | if (q) |
| 2529 | queue_writel(queue, RBQS, buffer_size); |
| 2530 | else |
| 2531 | dmacfg |= GEM_BF(RXBS, buffer_size); |
| 2532 | } |
Nicolas Ferre | e175587 | 2014-07-24 13:50:58 +0200 | [diff] [blame] | 2533 | if (bp->dma_burst_length) |
| 2534 | dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg); |
Nicolas Ferre | b3e3bd71 | 2012-11-23 03:49:01 +0000 | [diff] [blame] | 2535 | dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L); |
Arun Chandran | a50dad3 | 2015-02-18 16:59:35 +0530 | [diff] [blame] | 2536 | dmacfg &= ~GEM_BIT(ENDIA_PKT); |
Arun Chandran | 62f6924 | 2015-03-01 11:38:02 +0530 | [diff] [blame] | 2537 | |
Andy Shevchenko | f2ce8a9e | 2015-07-24 21:23:59 +0300 | [diff] [blame] | 2538 | if (bp->native_io) |
Arun Chandran | 62f6924 | 2015-03-01 11:38:02 +0530 | [diff] [blame] | 2539 | dmacfg &= ~GEM_BIT(ENDIA_DESC); |
| 2540 | else |
| 2541 | dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */ |
| 2542 | |
Cyrille Pitchen | 85ff3d8 | 2014-07-24 13:51:00 +0200 | [diff] [blame] | 2543 | if (bp->dev->features & NETIF_F_HW_CSUM) |
| 2544 | dmacfg |= GEM_BIT(TXCOEN); |
| 2545 | else |
| 2546 | dmacfg &= ~GEM_BIT(TXCOEN); |
Harini Katakam | fff8019 | 2016-08-09 13:15:53 +0530 | [diff] [blame] | 2547 | |
Michal Simek | bd62072 | 2018-09-25 08:32:50 +0200 | [diff] [blame] | 2548 | dmacfg &= ~GEM_BIT(ADDR64); |
Harini Katakam | fff8019 | 2016-08-09 13:15:53 +0530 | [diff] [blame] | 2549 | #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT |
Rafal Ozieblo | 7b42961 | 2017-06-29 07:12:51 +0100 | [diff] [blame] | 2550 | if (bp->hw_dma_cap & HW_DMA_CAP_64B) |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 2551 | dmacfg |= GEM_BIT(ADDR64); |
Harini Katakam | fff8019 | 2016-08-09 13:15:53 +0530 | [diff] [blame] | 2552 | #endif |
Rafal Ozieblo | 7b42961 | 2017-06-29 07:12:51 +0100 | [diff] [blame] | 2553 | #ifdef CONFIG_MACB_USE_HWSTAMP |
| 2554 | if (bp->hw_dma_cap & HW_DMA_CAP_PTP) |
| 2555 | dmacfg |= GEM_BIT(RXEXT) | GEM_BIT(TXEXT); |
| 2556 | #endif |
Nicolas Ferre | e175587 | 2014-07-24 13:50:58 +0200 | [diff] [blame] | 2557 | netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n", |
| 2558 | dmacfg); |
Jamie Iles | 0116da4 | 2011-03-14 17:38:30 +0000 | [diff] [blame] | 2559 | gem_writel(bp, DMACFG, dmacfg); |
| 2560 | } |
| 2561 | } |
| 2562 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2563 | static void macb_init_hw(struct macb *bp) |
| 2564 | { |
| 2565 | u32 config; |
| 2566 | |
| 2567 | macb_reset_hw(bp); |
Joachim Eastwood | 314bccc | 2012-11-07 08:14:52 +0000 | [diff] [blame] | 2568 | macb_set_hwaddr(bp); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2569 | |
Jamie Iles | 70c9f3d | 2011-03-09 16:22:54 +0000 | [diff] [blame] | 2570 | config = macb_mdc_clk_div(bp); |
Havard Skinnemoen | 29bc2e1 | 2012-10-31 06:04:58 +0000 | [diff] [blame] | 2571 | config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */ |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2572 | config |= MACB_BIT(DRFCS); /* Discard Rx FCS */ |
Dan Carpenter | a104a6b | 2015-05-12 21:15:24 +0300 | [diff] [blame] | 2573 | if (bp->caps & MACB_CAPS_JUMBO) |
Harini Katakam | 98b5a0f4 | 2015-05-06 22:27:17 +0530 | [diff] [blame] | 2574 | config |= MACB_BIT(JFRAME); /* Enable jumbo frames */ |
| 2575 | else |
| 2576 | config |= MACB_BIT(BIG); /* Receive oversized frames */ |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2577 | if (bp->dev->flags & IFF_PROMISC) |
| 2578 | config |= MACB_BIT(CAF); /* Copy All Frames */ |
Cyrille Pitchen | 924ec53 | 2014-07-24 13:51:01 +0200 | [diff] [blame] | 2579 | else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM) |
| 2580 | config |= GEM_BIT(RXCOEN); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2581 | if (!(bp->dev->flags & IFF_BROADCAST)) |
| 2582 | config |= MACB_BIT(NBC); /* No BroadCast */ |
Jamie Iles | 757a03c | 2011-03-09 16:29:59 +0000 | [diff] [blame] | 2583 | config |= macb_dbw(bp); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2584 | macb_writel(bp, NCFGR, config); |
Dan Carpenter | a104a6b | 2015-05-12 21:15:24 +0300 | [diff] [blame] | 2585 | if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len) |
Harini Katakam | 98b5a0f4 | 2015-05-06 22:27:17 +0530 | [diff] [blame] | 2586 | gem_writel(bp, JML, bp->jumbo_max_len); |
Harini Katakam | 98b5a0f4 | 2015-05-06 22:27:17 +0530 | [diff] [blame] | 2587 | bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK; |
Dan Carpenter | a104a6b | 2015-05-12 21:15:24 +0300 | [diff] [blame] | 2588 | if (bp->caps & MACB_CAPS_JUMBO) |
Harini Katakam | 98b5a0f4 | 2015-05-06 22:27:17 +0530 | [diff] [blame] | 2589 | bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2590 | |
Jamie Iles | 0116da4 | 2011-03-14 17:38:30 +0000 | [diff] [blame] | 2591 | macb_configure_dma(bp); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2592 | } |
| 2593 | |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 2594 | /* The hash address register is 64 bits long and takes up two |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 2595 | * locations in the memory map. The least significant bits are stored |
| 2596 | * in EMAC_HSL and the most significant bits in EMAC_HSH. |
| 2597 | * |
| 2598 | * The unicast hash enable and the multicast hash enable bits in the |
| 2599 | * network configuration register enable the reception of hash matched |
| 2600 | * frames. The destination address is reduced to a 6 bit index into |
| 2601 | * the 64 bit hash register using the following hash function. The |
| 2602 | * hash function is an exclusive or of every sixth bit of the |
| 2603 | * destination address. |
| 2604 | * |
| 2605 | * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47] |
| 2606 | * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46] |
| 2607 | * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45] |
| 2608 | * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44] |
| 2609 | * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43] |
| 2610 | * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42] |
| 2611 | * |
| 2612 | * da[0] represents the least significant bit of the first byte |
| 2613 | * received, that is, the multicast/unicast indicator, and da[47] |
| 2614 | * represents the most significant bit of the last byte received. If |
| 2615 | * the hash index, hi[n], points to a bit that is set in the hash |
| 2616 | * register then the frame will be matched according to whether the |
| 2617 | * frame is multicast or unicast. A multicast match will be signalled |
| 2618 | * if the multicast hash enable bit is set, da[0] is 1 and the hash |
| 2619 | * index points to a bit set in the hash register. A unicast match |
| 2620 | * will be signalled if the unicast hash enable bit is set, da[0] is 0 |
| 2621 | * and the hash index points to a bit set in the hash register. To |
| 2622 | * receive all multicast frames, the hash register should be set with |
| 2623 | * all ones and the multicast hash enable bit should be set in the |
| 2624 | * network configuration register. |
| 2625 | */ |
| 2626 | |
| 2627 | static inline int hash_bit_value(int bitnr, __u8 *addr) |
| 2628 | { |
| 2629 | if (addr[bitnr / 8] & (1 << (bitnr % 8))) |
| 2630 | return 1; |
| 2631 | return 0; |
| 2632 | } |
| 2633 | |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 2634 | /* Return the hash index value for the specified address. */ |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 2635 | static int hash_get_index(__u8 *addr) |
| 2636 | { |
| 2637 | int i, j, bitval; |
| 2638 | int hash_index = 0; |
| 2639 | |
| 2640 | for (j = 0; j < 6; j++) { |
| 2641 | for (i = 0, bitval = 0; i < 8; i++) |
Xander Huff | 2fa45e2 | 2015-01-15 15:55:19 -0600 | [diff] [blame] | 2642 | bitval ^= hash_bit_value(i * 6 + j, addr); |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 2643 | |
| 2644 | hash_index |= (bitval << j); |
| 2645 | } |
| 2646 | |
| 2647 | return hash_index; |
| 2648 | } |
| 2649 | |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 2650 | /* Add multicast addresses to the internal multicast-hash table. */ |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 2651 | static void macb_sethashtable(struct net_device *dev) |
| 2652 | { |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 2653 | struct netdev_hw_addr *ha; |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 2654 | unsigned long mc_filter[2]; |
Jiri Pirko | f9dcbcc | 2010-02-23 09:19:49 +0000 | [diff] [blame] | 2655 | unsigned int bitnr; |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 2656 | struct macb *bp = netdev_priv(dev); |
| 2657 | |
Moritz Fischer | aa50b55 | 2016-03-29 19:11:13 -0700 | [diff] [blame] | 2658 | mc_filter[0] = 0; |
| 2659 | mc_filter[1] = 0; |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 2660 | |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 2661 | netdev_for_each_mc_addr(ha, dev) { |
| 2662 | bitnr = hash_get_index(ha->addr); |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 2663 | mc_filter[bitnr >> 5] |= 1 << (bitnr & 31); |
| 2664 | } |
| 2665 | |
Jamie Iles | f75ba50 | 2011-11-08 10:12:32 +0000 | [diff] [blame] | 2666 | macb_or_gem_writel(bp, HRB, mc_filter[0]); |
| 2667 | macb_or_gem_writel(bp, HRT, mc_filter[1]); |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 2668 | } |
| 2669 | |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 2670 | /* Enable/Disable promiscuous and multicast modes. */ |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 2671 | static void macb_set_rx_mode(struct net_device *dev) |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 2672 | { |
| 2673 | unsigned long cfg; |
| 2674 | struct macb *bp = netdev_priv(dev); |
| 2675 | |
| 2676 | cfg = macb_readl(bp, NCFGR); |
| 2677 | |
Cyrille Pitchen | 924ec53 | 2014-07-24 13:51:01 +0200 | [diff] [blame] | 2678 | if (dev->flags & IFF_PROMISC) { |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 2679 | /* Enable promiscuous mode */ |
| 2680 | cfg |= MACB_BIT(CAF); |
Cyrille Pitchen | 924ec53 | 2014-07-24 13:51:01 +0200 | [diff] [blame] | 2681 | |
| 2682 | /* Disable RX checksum offload */ |
| 2683 | if (macb_is_gem(bp)) |
| 2684 | cfg &= ~GEM_BIT(RXCOEN); |
| 2685 | } else { |
| 2686 | /* Disable promiscuous mode */ |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 2687 | cfg &= ~MACB_BIT(CAF); |
| 2688 | |
Cyrille Pitchen | 924ec53 | 2014-07-24 13:51:01 +0200 | [diff] [blame] | 2689 | /* Enable RX checksum offload only if requested */ |
| 2690 | if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM) |
| 2691 | cfg |= GEM_BIT(RXCOEN); |
| 2692 | } |
| 2693 | |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 2694 | if (dev->flags & IFF_ALLMULTI) { |
| 2695 | /* Enable all multicast mode */ |
Jamie Iles | f75ba50 | 2011-11-08 10:12:32 +0000 | [diff] [blame] | 2696 | macb_or_gem_writel(bp, HRB, -1); |
| 2697 | macb_or_gem_writel(bp, HRT, -1); |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 2698 | cfg |= MACB_BIT(NCFGR_MTI); |
Jiri Pirko | 4cd24ea | 2010-02-08 04:30:35 +0000 | [diff] [blame] | 2699 | } else if (!netdev_mc_empty(dev)) { |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 2700 | /* Enable specific multicasts */ |
| 2701 | macb_sethashtable(dev); |
| 2702 | cfg |= MACB_BIT(NCFGR_MTI); |
| 2703 | } else if (dev->flags & (~IFF_ALLMULTI)) { |
| 2704 | /* Disable all multicast mode */ |
Jamie Iles | f75ba50 | 2011-11-08 10:12:32 +0000 | [diff] [blame] | 2705 | macb_or_gem_writel(bp, HRB, 0); |
| 2706 | macb_or_gem_writel(bp, HRT, 0); |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 2707 | cfg &= ~MACB_BIT(NCFGR_MTI); |
| 2708 | } |
| 2709 | |
| 2710 | macb_writel(bp, NCFGR, cfg); |
| 2711 | } |
| 2712 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2713 | static int macb_open(struct net_device *dev) |
| 2714 | { |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 2715 | size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN; |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 2716 | struct macb *bp = netdev_priv(dev); |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 2717 | struct macb_queue *queue; |
| 2718 | unsigned int q; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2719 | int err; |
| 2720 | |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 2721 | netdev_dbg(bp->dev, "open\n"); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2722 | |
Harini Katakam | d54f89a | 2019-03-01 16:20:34 +0530 | [diff] [blame] | 2723 | err = pm_runtime_get_sync(&bp->pdev->dev); |
| 2724 | if (err < 0) |
| 2725 | goto pm_exit; |
| 2726 | |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 2727 | /* RX buffers initialization */ |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 2728 | macb_init_rx_buffer_size(bp, bufsz); |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 2729 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2730 | err = macb_alloc_consistent(bp); |
| 2731 | if (err) { |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 2732 | netdev_err(dev, "Unable to allocate DMA memory (error %d)\n", |
| 2733 | err); |
Harini Katakam | d54f89a | 2019-03-01 16:20:34 +0530 | [diff] [blame] | 2734 | goto pm_exit; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2735 | } |
| 2736 | |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 2737 | for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) |
| 2738 | napi_enable(&queue->napi); |
| 2739 | |
Harini Katakam | 0504453 | 2019-05-07 19:59:10 +0530 | [diff] [blame] | 2740 | macb_init_hw(bp); |
| 2741 | |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 2742 | err = macb_phylink_connect(bp); |
| 2743 | if (err) |
Claudiu Beznea | faa62087 | 2020-06-18 11:37:40 +0300 | [diff] [blame] | 2744 | goto reset_hw; |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 2745 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 2746 | netif_tx_start_all_queues(dev); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2747 | |
Andrei.Pistirica@microchip.com | c2594d8 | 2017-01-19 17:56:15 +0200 | [diff] [blame] | 2748 | if (bp->ptp_info) |
| 2749 | bp->ptp_info->ptp_init(dev); |
| 2750 | |
Charles Keepax | 939a5bf7 | 2020-06-15 14:18:54 +0100 | [diff] [blame] | 2751 | return 0; |
| 2752 | |
Claudiu Beznea | faa62087 | 2020-06-18 11:37:40 +0300 | [diff] [blame] | 2753 | reset_hw: |
| 2754 | macb_reset_hw(bp); |
Corentin Labbe | 014406b | 2020-06-10 09:53:44 +0000 | [diff] [blame] | 2755 | for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) |
| 2756 | napi_disable(&queue->napi); |
Claudiu Beznea | faa62087 | 2020-06-18 11:37:40 +0300 | [diff] [blame] | 2757 | macb_free_consistent(bp); |
Harini Katakam | d54f89a | 2019-03-01 16:20:34 +0530 | [diff] [blame] | 2758 | pm_exit: |
Charles Keepax | 939a5bf7 | 2020-06-15 14:18:54 +0100 | [diff] [blame] | 2759 | pm_runtime_put_sync(&bp->pdev->dev); |
| 2760 | return err; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2761 | } |
| 2762 | |
| 2763 | static int macb_close(struct net_device *dev) |
| 2764 | { |
| 2765 | struct macb *bp = netdev_priv(dev); |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 2766 | struct macb_queue *queue; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2767 | unsigned long flags; |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 2768 | unsigned int q; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2769 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 2770 | netif_tx_stop_all_queues(dev); |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 2771 | |
| 2772 | for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) |
| 2773 | napi_disable(&queue->napi); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2774 | |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 2775 | phylink_stop(bp->phylink); |
| 2776 | phylink_disconnect_phy(bp->phylink); |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 2777 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2778 | spin_lock_irqsave(&bp->lock, flags); |
| 2779 | macb_reset_hw(bp); |
| 2780 | netif_carrier_off(dev); |
| 2781 | spin_unlock_irqrestore(&bp->lock, flags); |
| 2782 | |
| 2783 | macb_free_consistent(bp); |
| 2784 | |
Andrei.Pistirica@microchip.com | c2594d8 | 2017-01-19 17:56:15 +0200 | [diff] [blame] | 2785 | if (bp->ptp_info) |
| 2786 | bp->ptp_info->ptp_remove(dev); |
| 2787 | |
Harini Katakam | d54f89a | 2019-03-01 16:20:34 +0530 | [diff] [blame] | 2788 | pm_runtime_put(&bp->pdev->dev); |
| 2789 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2790 | return 0; |
| 2791 | } |
| 2792 | |
Harini Katakam | a5898ea | 2015-05-06 22:27:18 +0530 | [diff] [blame] | 2793 | static int macb_change_mtu(struct net_device *dev, int new_mtu) |
| 2794 | { |
Harini Katakam | a5898ea | 2015-05-06 22:27:18 +0530 | [diff] [blame] | 2795 | if (netif_running(dev)) |
| 2796 | return -EBUSY; |
| 2797 | |
Harini Katakam | a5898ea | 2015-05-06 22:27:18 +0530 | [diff] [blame] | 2798 | dev->mtu = new_mtu; |
| 2799 | |
| 2800 | return 0; |
| 2801 | } |
| 2802 | |
Jamie Iles | a494ed8 | 2011-03-09 16:26:35 +0000 | [diff] [blame] | 2803 | static void gem_update_stats(struct macb *bp) |
| 2804 | { |
Rafal Ozieblo | 512286b | 2017-11-30 18:19:56 +0000 | [diff] [blame] | 2805 | struct macb_queue *queue; |
| 2806 | unsigned int i, q, idx; |
| 2807 | unsigned long *stat; |
| 2808 | |
Jamie Iles | a494ed8 | 2011-03-09 16:26:35 +0000 | [diff] [blame] | 2809 | u32 *p = &bp->hw_stats.gem.tx_octets_31_0; |
Jamie Iles | a494ed8 | 2011-03-09 16:26:35 +0000 | [diff] [blame] | 2810 | |
Xander Huff | 3ff13f1 | 2015-01-13 16:15:51 -0600 | [diff] [blame] | 2811 | for (i = 0; i < GEM_STATS_LEN; ++i, ++p) { |
| 2812 | u32 offset = gem_statistics[i].offset; |
David S. Miller | 7a6e070 | 2015-07-27 14:24:48 -0700 | [diff] [blame] | 2813 | u64 val = bp->macb_reg_readl(bp, offset); |
Xander Huff | 3ff13f1 | 2015-01-13 16:15:51 -0600 | [diff] [blame] | 2814 | |
| 2815 | bp->ethtool_stats[i] += val; |
| 2816 | *p += val; |
| 2817 | |
| 2818 | if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) { |
| 2819 | /* Add GEM_OCTTXH, GEM_OCTRXH */ |
David S. Miller | 7a6e070 | 2015-07-27 14:24:48 -0700 | [diff] [blame] | 2820 | val = bp->macb_reg_readl(bp, offset + 4); |
Xander Huff | 2fa45e2 | 2015-01-15 15:55:19 -0600 | [diff] [blame] | 2821 | bp->ethtool_stats[i] += ((u64)val) << 32; |
Xander Huff | 3ff13f1 | 2015-01-13 16:15:51 -0600 | [diff] [blame] | 2822 | *(++p) += val; |
| 2823 | } |
| 2824 | } |
Rafal Ozieblo | 512286b | 2017-11-30 18:19:56 +0000 | [diff] [blame] | 2825 | |
| 2826 | idx = GEM_STATS_LEN; |
| 2827 | for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) |
| 2828 | for (i = 0, stat = &queue->stats.first; i < QUEUE_STATS_LEN; ++i, ++stat) |
| 2829 | bp->ethtool_stats[idx++] = *stat; |
Jamie Iles | a494ed8 | 2011-03-09 16:26:35 +0000 | [diff] [blame] | 2830 | } |
| 2831 | |
| 2832 | static struct net_device_stats *gem_get_stats(struct macb *bp) |
| 2833 | { |
| 2834 | struct gem_stats *hwstat = &bp->hw_stats.gem; |
Tobias Klauser | 5f1d3a5 | 2017-04-07 10:17:30 +0200 | [diff] [blame] | 2835 | struct net_device_stats *nstat = &bp->dev->stats; |
Jamie Iles | a494ed8 | 2011-03-09 16:26:35 +0000 | [diff] [blame] | 2836 | |
Zong Li | 5eff146 | 2021-05-22 17:16:11 +0800 | [diff] [blame] | 2837 | if (!netif_running(bp->dev)) |
| 2838 | return nstat; |
| 2839 | |
Jamie Iles | a494ed8 | 2011-03-09 16:26:35 +0000 | [diff] [blame] | 2840 | gem_update_stats(bp); |
| 2841 | |
| 2842 | nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors + |
| 2843 | hwstat->rx_alignment_errors + |
| 2844 | hwstat->rx_resource_errors + |
| 2845 | hwstat->rx_overruns + |
| 2846 | hwstat->rx_oversize_frames + |
| 2847 | hwstat->rx_jabbers + |
| 2848 | hwstat->rx_undersized_frames + |
| 2849 | hwstat->rx_length_field_frame_errors); |
| 2850 | nstat->tx_errors = (hwstat->tx_late_collisions + |
| 2851 | hwstat->tx_excessive_collisions + |
| 2852 | hwstat->tx_underrun + |
| 2853 | hwstat->tx_carrier_sense_errors); |
| 2854 | nstat->multicast = hwstat->rx_multicast_frames; |
| 2855 | nstat->collisions = (hwstat->tx_single_collision_frames + |
| 2856 | hwstat->tx_multiple_collision_frames + |
| 2857 | hwstat->tx_excessive_collisions); |
| 2858 | nstat->rx_length_errors = (hwstat->rx_oversize_frames + |
| 2859 | hwstat->rx_jabbers + |
| 2860 | hwstat->rx_undersized_frames + |
| 2861 | hwstat->rx_length_field_frame_errors); |
| 2862 | nstat->rx_over_errors = hwstat->rx_resource_errors; |
| 2863 | nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors; |
| 2864 | nstat->rx_frame_errors = hwstat->rx_alignment_errors; |
| 2865 | nstat->rx_fifo_errors = hwstat->rx_overruns; |
| 2866 | nstat->tx_aborted_errors = hwstat->tx_excessive_collisions; |
| 2867 | nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors; |
| 2868 | nstat->tx_fifo_errors = hwstat->tx_underrun; |
| 2869 | |
| 2870 | return nstat; |
| 2871 | } |
| 2872 | |
Xander Huff | 3ff13f1 | 2015-01-13 16:15:51 -0600 | [diff] [blame] | 2873 | static void gem_get_ethtool_stats(struct net_device *dev, |
| 2874 | struct ethtool_stats *stats, u64 *data) |
| 2875 | { |
| 2876 | struct macb *bp; |
| 2877 | |
| 2878 | bp = netdev_priv(dev); |
| 2879 | gem_update_stats(bp); |
Rafal Ozieblo | 512286b | 2017-11-30 18:19:56 +0000 | [diff] [blame] | 2880 | memcpy(data, &bp->ethtool_stats, sizeof(u64) |
| 2881 | * (GEM_STATS_LEN + QUEUE_STATS_LEN * MACB_MAX_QUEUES)); |
Xander Huff | 3ff13f1 | 2015-01-13 16:15:51 -0600 | [diff] [blame] | 2882 | } |
| 2883 | |
| 2884 | static int gem_get_sset_count(struct net_device *dev, int sset) |
| 2885 | { |
Rafal Ozieblo | 512286b | 2017-11-30 18:19:56 +0000 | [diff] [blame] | 2886 | struct macb *bp = netdev_priv(dev); |
| 2887 | |
Xander Huff | 3ff13f1 | 2015-01-13 16:15:51 -0600 | [diff] [blame] | 2888 | switch (sset) { |
| 2889 | case ETH_SS_STATS: |
Rafal Ozieblo | 512286b | 2017-11-30 18:19:56 +0000 | [diff] [blame] | 2890 | return GEM_STATS_LEN + bp->num_queues * QUEUE_STATS_LEN; |
Xander Huff | 3ff13f1 | 2015-01-13 16:15:51 -0600 | [diff] [blame] | 2891 | default: |
| 2892 | return -EOPNOTSUPP; |
| 2893 | } |
| 2894 | } |
| 2895 | |
| 2896 | static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p) |
| 2897 | { |
Rafal Ozieblo | 512286b | 2017-11-30 18:19:56 +0000 | [diff] [blame] | 2898 | char stat_string[ETH_GSTRING_LEN]; |
| 2899 | struct macb *bp = netdev_priv(dev); |
| 2900 | struct macb_queue *queue; |
Andy Shevchenko | 8bcbf82 | 2015-07-24 21:24:02 +0300 | [diff] [blame] | 2901 | unsigned int i; |
Rafal Ozieblo | 512286b | 2017-11-30 18:19:56 +0000 | [diff] [blame] | 2902 | unsigned int q; |
Xander Huff | 3ff13f1 | 2015-01-13 16:15:51 -0600 | [diff] [blame] | 2903 | |
| 2904 | switch (sset) { |
| 2905 | case ETH_SS_STATS: |
| 2906 | for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN) |
| 2907 | memcpy(p, gem_statistics[i].stat_string, |
| 2908 | ETH_GSTRING_LEN); |
Rafal Ozieblo | 512286b | 2017-11-30 18:19:56 +0000 | [diff] [blame] | 2909 | |
| 2910 | for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) { |
| 2911 | for (i = 0; i < QUEUE_STATS_LEN; i++, p += ETH_GSTRING_LEN) { |
| 2912 | snprintf(stat_string, ETH_GSTRING_LEN, "q%d_%s", |
| 2913 | q, queue_statistics[i].stat_string); |
| 2914 | memcpy(p, stat_string, ETH_GSTRING_LEN); |
| 2915 | } |
| 2916 | } |
Xander Huff | 3ff13f1 | 2015-01-13 16:15:51 -0600 | [diff] [blame] | 2917 | break; |
| 2918 | } |
| 2919 | } |
| 2920 | |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 2921 | static struct net_device_stats *macb_get_stats(struct net_device *dev) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2922 | { |
| 2923 | struct macb *bp = netdev_priv(dev); |
Tobias Klauser | 5f1d3a5 | 2017-04-07 10:17:30 +0200 | [diff] [blame] | 2924 | struct net_device_stats *nstat = &bp->dev->stats; |
Jamie Iles | a494ed8 | 2011-03-09 16:26:35 +0000 | [diff] [blame] | 2925 | struct macb_stats *hwstat = &bp->hw_stats.macb; |
| 2926 | |
| 2927 | if (macb_is_gem(bp)) |
| 2928 | return gem_get_stats(bp); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2929 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 2930 | /* read stats from hardware */ |
| 2931 | macb_update_stats(bp); |
| 2932 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2933 | /* Convert HW stats into netdevice stats */ |
| 2934 | nstat->rx_errors = (hwstat->rx_fcs_errors + |
| 2935 | hwstat->rx_align_errors + |
| 2936 | hwstat->rx_resource_errors + |
| 2937 | hwstat->rx_overruns + |
| 2938 | hwstat->rx_oversize_pkts + |
| 2939 | hwstat->rx_jabbers + |
| 2940 | hwstat->rx_undersize_pkts + |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2941 | hwstat->rx_length_mismatch); |
| 2942 | nstat->tx_errors = (hwstat->tx_late_cols + |
| 2943 | hwstat->tx_excessive_cols + |
| 2944 | hwstat->tx_underruns + |
Wolfgang Steinwender | 716723c | 2015-04-10 11:42:56 +0200 | [diff] [blame] | 2945 | hwstat->tx_carrier_errors + |
| 2946 | hwstat->sqe_test_errors); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2947 | nstat->collisions = (hwstat->tx_single_cols + |
| 2948 | hwstat->tx_multiple_cols + |
| 2949 | hwstat->tx_excessive_cols); |
| 2950 | nstat->rx_length_errors = (hwstat->rx_oversize_pkts + |
| 2951 | hwstat->rx_jabbers + |
| 2952 | hwstat->rx_undersize_pkts + |
| 2953 | hwstat->rx_length_mismatch); |
Alexander Stein | b19f7f7 | 2011-04-13 05:03:24 +0000 | [diff] [blame] | 2954 | nstat->rx_over_errors = hwstat->rx_resource_errors + |
| 2955 | hwstat->rx_overruns; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2956 | nstat->rx_crc_errors = hwstat->rx_fcs_errors; |
| 2957 | nstat->rx_frame_errors = hwstat->rx_align_errors; |
| 2958 | nstat->rx_fifo_errors = hwstat->rx_overruns; |
| 2959 | /* XXX: What does "missed" mean? */ |
| 2960 | nstat->tx_aborted_errors = hwstat->tx_excessive_cols; |
| 2961 | nstat->tx_carrier_errors = hwstat->tx_carrier_errors; |
| 2962 | nstat->tx_fifo_errors = hwstat->tx_underruns; |
| 2963 | /* Don't know about heartbeat or window errors... */ |
| 2964 | |
| 2965 | return nstat; |
| 2966 | } |
| 2967 | |
Nicolas Ferre | d1d1b53 | 2012-10-31 06:04:56 +0000 | [diff] [blame] | 2968 | static int macb_get_regs_len(struct net_device *netdev) |
| 2969 | { |
| 2970 | return MACB_GREGS_NBR * sizeof(u32); |
| 2971 | } |
| 2972 | |
| 2973 | static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs, |
| 2974 | void *p) |
| 2975 | { |
| 2976 | struct macb *bp = netdev_priv(dev); |
| 2977 | unsigned int tail, head; |
| 2978 | u32 *regs_buff = p; |
| 2979 | |
| 2980 | regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1)) |
| 2981 | | MACB_GREGS_VERSION; |
| 2982 | |
Zach Brown | b410d13 | 2016-10-19 09:56:57 -0500 | [diff] [blame] | 2983 | tail = macb_tx_ring_wrap(bp, bp->queues[0].tx_tail); |
| 2984 | head = macb_tx_ring_wrap(bp, bp->queues[0].tx_head); |
Nicolas Ferre | d1d1b53 | 2012-10-31 06:04:56 +0000 | [diff] [blame] | 2985 | |
| 2986 | regs_buff[0] = macb_readl(bp, NCR); |
| 2987 | regs_buff[1] = macb_or_gem_readl(bp, NCFGR); |
| 2988 | regs_buff[2] = macb_readl(bp, NSR); |
| 2989 | regs_buff[3] = macb_readl(bp, TSR); |
| 2990 | regs_buff[4] = macb_readl(bp, RBQP); |
| 2991 | regs_buff[5] = macb_readl(bp, TBQP); |
| 2992 | regs_buff[6] = macb_readl(bp, RSR); |
| 2993 | regs_buff[7] = macb_readl(bp, IMR); |
| 2994 | |
| 2995 | regs_buff[8] = tail; |
| 2996 | regs_buff[9] = head; |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 2997 | regs_buff[10] = macb_tx_dma(&bp->queues[0], tail); |
| 2998 | regs_buff[11] = macb_tx_dma(&bp->queues[0], head); |
Nicolas Ferre | d1d1b53 | 2012-10-31 06:04:56 +0000 | [diff] [blame] | 2999 | |
Neil Armstrong | ce721a7 | 2016-01-05 14:39:16 +0100 | [diff] [blame] | 3000 | if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) |
| 3001 | regs_buff[12] = macb_or_gem_readl(bp, USRIO); |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 3002 | if (macb_is_gem(bp)) |
Nicolas Ferre | d1d1b53 | 2012-10-31 06:04:56 +0000 | [diff] [blame] | 3003 | regs_buff[13] = gem_readl(bp, DMACFG); |
Nicolas Ferre | d1d1b53 | 2012-10-31 06:04:56 +0000 | [diff] [blame] | 3004 | } |
| 3005 | |
Sergio Prado | 3e2a5e1 | 2016-02-09 12:07:16 -0200 | [diff] [blame] | 3006 | static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol) |
| 3007 | { |
| 3008 | struct macb *bp = netdev_priv(netdev); |
| 3009 | |
Nicolas Ferre | 253fe09 | 2020-07-10 14:46:43 +0200 | [diff] [blame] | 3010 | if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET) { |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 3011 | phylink_ethtool_get_wol(bp->phylink, wol); |
Nicolas Ferre | 253fe09 | 2020-07-10 14:46:43 +0200 | [diff] [blame] | 3012 | wol->supported |= WAKE_MAGIC; |
| 3013 | |
| 3014 | if (bp->wol & MACB_WOL_ENABLED) |
| 3015 | wol->wolopts |= WAKE_MAGIC; |
| 3016 | } |
Sergio Prado | 3e2a5e1 | 2016-02-09 12:07:16 -0200 | [diff] [blame] | 3017 | } |
| 3018 | |
| 3019 | static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol) |
| 3020 | { |
| 3021 | struct macb *bp = netdev_priv(netdev); |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 3022 | int ret; |
| 3023 | |
Nicolas Ferre | 253fe09 | 2020-07-10 14:46:43 +0200 | [diff] [blame] | 3024 | /* Pass the order to phylink layer */ |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 3025 | ret = phylink_ethtool_set_wol(bp->phylink, wol); |
Nicolas Ferre | 253fe09 | 2020-07-10 14:46:43 +0200 | [diff] [blame] | 3026 | /* Don't manage WoL on MAC if handled by the PHY |
| 3027 | * or if there's a failure in talking to the PHY |
| 3028 | */ |
| 3029 | if (!ret || ret != -EOPNOTSUPP) |
| 3030 | return ret; |
Sergio Prado | 3e2a5e1 | 2016-02-09 12:07:16 -0200 | [diff] [blame] | 3031 | |
| 3032 | if (!(bp->wol & MACB_WOL_HAS_MAGIC_PACKET) || |
| 3033 | (wol->wolopts & ~WAKE_MAGIC)) |
| 3034 | return -EOPNOTSUPP; |
| 3035 | |
| 3036 | if (wol->wolopts & WAKE_MAGIC) |
| 3037 | bp->wol |= MACB_WOL_ENABLED; |
| 3038 | else |
| 3039 | bp->wol &= ~MACB_WOL_ENABLED; |
| 3040 | |
| 3041 | device_set_wakeup_enable(&bp->pdev->dev, bp->wol & MACB_WOL_ENABLED); |
| 3042 | |
| 3043 | return 0; |
| 3044 | } |
| 3045 | |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 3046 | static int macb_get_link_ksettings(struct net_device *netdev, |
| 3047 | struct ethtool_link_ksettings *kset) |
| 3048 | { |
| 3049 | struct macb *bp = netdev_priv(netdev); |
| 3050 | |
| 3051 | return phylink_ethtool_ksettings_get(bp->phylink, kset); |
| 3052 | } |
| 3053 | |
| 3054 | static int macb_set_link_ksettings(struct net_device *netdev, |
| 3055 | const struct ethtool_link_ksettings *kset) |
| 3056 | { |
| 3057 | struct macb *bp = netdev_priv(netdev); |
| 3058 | |
| 3059 | return phylink_ethtool_ksettings_set(bp->phylink, kset); |
| 3060 | } |
| 3061 | |
Zach Brown | 8441bb3 | 2016-10-19 09:56:58 -0500 | [diff] [blame] | 3062 | static void macb_get_ringparam(struct net_device *netdev, |
Hao Chen | 7462494 | 2021-11-18 20:12:43 +0800 | [diff] [blame] | 3063 | struct ethtool_ringparam *ring, |
| 3064 | struct kernel_ethtool_ringparam *kernel_ring, |
| 3065 | struct netlink_ext_ack *extack) |
Zach Brown | 8441bb3 | 2016-10-19 09:56:58 -0500 | [diff] [blame] | 3066 | { |
| 3067 | struct macb *bp = netdev_priv(netdev); |
| 3068 | |
| 3069 | ring->rx_max_pending = MAX_RX_RING_SIZE; |
| 3070 | ring->tx_max_pending = MAX_TX_RING_SIZE; |
| 3071 | |
| 3072 | ring->rx_pending = bp->rx_ring_size; |
| 3073 | ring->tx_pending = bp->tx_ring_size; |
| 3074 | } |
| 3075 | |
| 3076 | static int macb_set_ringparam(struct net_device *netdev, |
Hao Chen | 7462494 | 2021-11-18 20:12:43 +0800 | [diff] [blame] | 3077 | struct ethtool_ringparam *ring, |
| 3078 | struct kernel_ethtool_ringparam *kernel_ring, |
| 3079 | struct netlink_ext_ack *extack) |
Zach Brown | 8441bb3 | 2016-10-19 09:56:58 -0500 | [diff] [blame] | 3080 | { |
| 3081 | struct macb *bp = netdev_priv(netdev); |
| 3082 | u32 new_rx_size, new_tx_size; |
| 3083 | unsigned int reset = 0; |
| 3084 | |
| 3085 | if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending)) |
| 3086 | return -EINVAL; |
| 3087 | |
| 3088 | new_rx_size = clamp_t(u32, ring->rx_pending, |
| 3089 | MIN_RX_RING_SIZE, MAX_RX_RING_SIZE); |
| 3090 | new_rx_size = roundup_pow_of_two(new_rx_size); |
| 3091 | |
| 3092 | new_tx_size = clamp_t(u32, ring->tx_pending, |
| 3093 | MIN_TX_RING_SIZE, MAX_TX_RING_SIZE); |
| 3094 | new_tx_size = roundup_pow_of_two(new_tx_size); |
| 3095 | |
| 3096 | if ((new_tx_size == bp->tx_ring_size) && |
| 3097 | (new_rx_size == bp->rx_ring_size)) { |
| 3098 | /* nothing to do */ |
| 3099 | return 0; |
| 3100 | } |
| 3101 | |
| 3102 | if (netif_running(bp->dev)) { |
| 3103 | reset = 1; |
| 3104 | macb_close(bp->dev); |
| 3105 | } |
| 3106 | |
| 3107 | bp->rx_ring_size = new_rx_size; |
| 3108 | bp->tx_ring_size = new_tx_size; |
| 3109 | |
| 3110 | if (reset) |
| 3111 | macb_open(bp->dev); |
| 3112 | |
| 3113 | return 0; |
| 3114 | } |
| 3115 | |
Rafal Ozieblo | ab91f0a | 2017-06-29 07:14:16 +0100 | [diff] [blame] | 3116 | #ifdef CONFIG_MACB_USE_HWSTAMP |
| 3117 | static unsigned int gem_get_tsu_rate(struct macb *bp) |
| 3118 | { |
| 3119 | struct clk *tsu_clk; |
| 3120 | unsigned int tsu_rate; |
| 3121 | |
| 3122 | tsu_clk = devm_clk_get(&bp->pdev->dev, "tsu_clk"); |
| 3123 | if (!IS_ERR(tsu_clk)) |
| 3124 | tsu_rate = clk_get_rate(tsu_clk); |
| 3125 | /* try pclk instead */ |
| 3126 | else if (!IS_ERR(bp->pclk)) { |
| 3127 | tsu_clk = bp->pclk; |
| 3128 | tsu_rate = clk_get_rate(tsu_clk); |
| 3129 | } else |
| 3130 | return -ENOTSUPP; |
| 3131 | return tsu_rate; |
| 3132 | } |
| 3133 | |
| 3134 | static s32 gem_get_ptp_max_adj(void) |
| 3135 | { |
| 3136 | return 64000000; |
| 3137 | } |
| 3138 | |
| 3139 | static int gem_get_ts_info(struct net_device *dev, |
| 3140 | struct ethtool_ts_info *info) |
| 3141 | { |
| 3142 | struct macb *bp = netdev_priv(dev); |
| 3143 | |
| 3144 | if ((bp->hw_dma_cap & HW_DMA_CAP_PTP) == 0) { |
| 3145 | ethtool_op_get_ts_info(dev, info); |
| 3146 | return 0; |
| 3147 | } |
| 3148 | |
| 3149 | info->so_timestamping = |
| 3150 | SOF_TIMESTAMPING_TX_SOFTWARE | |
| 3151 | SOF_TIMESTAMPING_RX_SOFTWARE | |
| 3152 | SOF_TIMESTAMPING_SOFTWARE | |
| 3153 | SOF_TIMESTAMPING_TX_HARDWARE | |
| 3154 | SOF_TIMESTAMPING_RX_HARDWARE | |
| 3155 | SOF_TIMESTAMPING_RAW_HARDWARE; |
| 3156 | info->tx_types = |
| 3157 | (1 << HWTSTAMP_TX_ONESTEP_SYNC) | |
| 3158 | (1 << HWTSTAMP_TX_OFF) | |
| 3159 | (1 << HWTSTAMP_TX_ON); |
| 3160 | info->rx_filters = |
| 3161 | (1 << HWTSTAMP_FILTER_NONE) | |
| 3162 | (1 << HWTSTAMP_FILTER_ALL); |
| 3163 | |
| 3164 | info->phc_index = bp->ptp_clock ? ptp_clock_index(bp->ptp_clock) : -1; |
| 3165 | |
| 3166 | return 0; |
| 3167 | } |
| 3168 | |
| 3169 | static struct macb_ptp_info gem_ptp_info = { |
| 3170 | .ptp_init = gem_ptp_init, |
| 3171 | .ptp_remove = gem_ptp_remove, |
| 3172 | .get_ptp_max_adj = gem_get_ptp_max_adj, |
| 3173 | .get_tsu_rate = gem_get_tsu_rate, |
| 3174 | .get_ts_info = gem_get_ts_info, |
| 3175 | .get_hwtst = gem_get_hwtst, |
| 3176 | .set_hwtst = gem_set_hwtst, |
| 3177 | }; |
| 3178 | #endif |
| 3179 | |
Andrei.Pistirica@microchip.com | c2594d8 | 2017-01-19 17:56:15 +0200 | [diff] [blame] | 3180 | static int macb_get_ts_info(struct net_device *netdev, |
| 3181 | struct ethtool_ts_info *info) |
| 3182 | { |
| 3183 | struct macb *bp = netdev_priv(netdev); |
| 3184 | |
| 3185 | if (bp->ptp_info) |
| 3186 | return bp->ptp_info->get_ts_info(netdev, info); |
| 3187 | |
| 3188 | return ethtool_op_get_ts_info(netdev, info); |
| 3189 | } |
| 3190 | |
Rafal Ozieblo | ae8223de | 2017-11-30 18:20:44 +0000 | [diff] [blame] | 3191 | static void gem_enable_flow_filters(struct macb *bp, bool enable) |
| 3192 | { |
Claudiu Beznea | c1e85c6c | 2019-05-22 08:24:43 +0000 | [diff] [blame] | 3193 | struct net_device *netdev = bp->dev; |
Rafal Ozieblo | ae8223de | 2017-11-30 18:20:44 +0000 | [diff] [blame] | 3194 | struct ethtool_rx_fs_item *item; |
| 3195 | u32 t2_scr; |
| 3196 | int num_t2_scr; |
| 3197 | |
Claudiu Beznea | c1e85c6c | 2019-05-22 08:24:43 +0000 | [diff] [blame] | 3198 | if (!(netdev->features & NETIF_F_NTUPLE)) |
| 3199 | return; |
| 3200 | |
Rafal Ozieblo | ae8223de | 2017-11-30 18:20:44 +0000 | [diff] [blame] | 3201 | num_t2_scr = GEM_BFEXT(T2SCR, gem_readl(bp, DCFG8)); |
| 3202 | |
| 3203 | list_for_each_entry(item, &bp->rx_fs_list.list, list) { |
| 3204 | struct ethtool_rx_flow_spec *fs = &item->fs; |
| 3205 | struct ethtool_tcpip4_spec *tp4sp_m; |
| 3206 | |
| 3207 | if (fs->location >= num_t2_scr) |
| 3208 | continue; |
| 3209 | |
| 3210 | t2_scr = gem_readl_n(bp, SCRT2, fs->location); |
| 3211 | |
| 3212 | /* enable/disable screener regs for the flow entry */ |
| 3213 | t2_scr = GEM_BFINS(ETHTEN, enable, t2_scr); |
| 3214 | |
| 3215 | /* only enable fields with no masking */ |
| 3216 | tp4sp_m = &(fs->m_u.tcp_ip4_spec); |
| 3217 | |
| 3218 | if (enable && (tp4sp_m->ip4src == 0xFFFFFFFF)) |
| 3219 | t2_scr = GEM_BFINS(CMPAEN, 1, t2_scr); |
| 3220 | else |
| 3221 | t2_scr = GEM_BFINS(CMPAEN, 0, t2_scr); |
| 3222 | |
| 3223 | if (enable && (tp4sp_m->ip4dst == 0xFFFFFFFF)) |
| 3224 | t2_scr = GEM_BFINS(CMPBEN, 1, t2_scr); |
| 3225 | else |
| 3226 | t2_scr = GEM_BFINS(CMPBEN, 0, t2_scr); |
| 3227 | |
| 3228 | if (enable && ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF))) |
| 3229 | t2_scr = GEM_BFINS(CMPCEN, 1, t2_scr); |
| 3230 | else |
| 3231 | t2_scr = GEM_BFINS(CMPCEN, 0, t2_scr); |
| 3232 | |
| 3233 | gem_writel_n(bp, SCRT2, fs->location, t2_scr); |
| 3234 | } |
| 3235 | } |
| 3236 | |
| 3237 | static void gem_prog_cmp_regs(struct macb *bp, struct ethtool_rx_flow_spec *fs) |
| 3238 | { |
| 3239 | struct ethtool_tcpip4_spec *tp4sp_v, *tp4sp_m; |
| 3240 | uint16_t index = fs->location; |
| 3241 | u32 w0, w1, t2_scr; |
| 3242 | bool cmp_a = false; |
| 3243 | bool cmp_b = false; |
| 3244 | bool cmp_c = false; |
| 3245 | |
Claudiu Beznea | a14d273 | 2021-04-02 15:42:53 +0300 | [diff] [blame] | 3246 | if (!macb_is_gem(bp)) |
| 3247 | return; |
| 3248 | |
Rafal Ozieblo | ae8223de | 2017-11-30 18:20:44 +0000 | [diff] [blame] | 3249 | tp4sp_v = &(fs->h_u.tcp_ip4_spec); |
| 3250 | tp4sp_m = &(fs->m_u.tcp_ip4_spec); |
| 3251 | |
| 3252 | /* ignore field if any masking set */ |
| 3253 | if (tp4sp_m->ip4src == 0xFFFFFFFF) { |
| 3254 | /* 1st compare reg - IP source address */ |
| 3255 | w0 = 0; |
| 3256 | w1 = 0; |
| 3257 | w0 = tp4sp_v->ip4src; |
| 3258 | w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */ |
| 3259 | w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1); |
| 3260 | w1 = GEM_BFINS(T2OFST, ETYPE_SRCIP_OFFSET, w1); |
| 3261 | gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w0); |
| 3262 | gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w1); |
| 3263 | cmp_a = true; |
| 3264 | } |
| 3265 | |
| 3266 | /* ignore field if any masking set */ |
| 3267 | if (tp4sp_m->ip4dst == 0xFFFFFFFF) { |
| 3268 | /* 2nd compare reg - IP destination address */ |
| 3269 | w0 = 0; |
| 3270 | w1 = 0; |
| 3271 | w0 = tp4sp_v->ip4dst; |
| 3272 | w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */ |
| 3273 | w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1); |
| 3274 | w1 = GEM_BFINS(T2OFST, ETYPE_DSTIP_OFFSET, w1); |
| 3275 | gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4DST_CMP(index)), w0); |
| 3276 | gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4DST_CMP(index)), w1); |
| 3277 | cmp_b = true; |
| 3278 | } |
| 3279 | |
| 3280 | /* ignore both port fields if masking set in both */ |
| 3281 | if ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)) { |
| 3282 | /* 3rd compare reg - source port, destination port */ |
| 3283 | w0 = 0; |
| 3284 | w1 = 0; |
| 3285 | w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_IPHDR, w1); |
| 3286 | if (tp4sp_m->psrc == tp4sp_m->pdst) { |
| 3287 | w0 = GEM_BFINS(T2MASK, tp4sp_v->psrc, w0); |
| 3288 | w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0); |
| 3289 | w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */ |
| 3290 | w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1); |
| 3291 | } else { |
| 3292 | /* only one port definition */ |
| 3293 | w1 = GEM_BFINS(T2DISMSK, 0, w1); /* 16-bit compare */ |
| 3294 | w0 = GEM_BFINS(T2MASK, 0xFFFF, w0); |
| 3295 | if (tp4sp_m->psrc == 0xFFFF) { /* src port */ |
| 3296 | w0 = GEM_BFINS(T2CMP, tp4sp_v->psrc, w0); |
| 3297 | w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1); |
| 3298 | } else { /* dst port */ |
| 3299 | w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0); |
| 3300 | w1 = GEM_BFINS(T2OFST, IPHDR_DSTPORT_OFFSET, w1); |
| 3301 | } |
| 3302 | } |
| 3303 | gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_PORT_CMP(index)), w0); |
| 3304 | gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_PORT_CMP(index)), w1); |
| 3305 | cmp_c = true; |
| 3306 | } |
| 3307 | |
| 3308 | t2_scr = 0; |
| 3309 | t2_scr = GEM_BFINS(QUEUE, (fs->ring_cookie) & 0xFF, t2_scr); |
| 3310 | t2_scr = GEM_BFINS(ETHT2IDX, SCRT2_ETHT, t2_scr); |
| 3311 | if (cmp_a) |
| 3312 | t2_scr = GEM_BFINS(CMPA, GEM_IP4SRC_CMP(index), t2_scr); |
| 3313 | if (cmp_b) |
| 3314 | t2_scr = GEM_BFINS(CMPB, GEM_IP4DST_CMP(index), t2_scr); |
| 3315 | if (cmp_c) |
| 3316 | t2_scr = GEM_BFINS(CMPC, GEM_PORT_CMP(index), t2_scr); |
| 3317 | gem_writel_n(bp, SCRT2, index, t2_scr); |
| 3318 | } |
| 3319 | |
| 3320 | static int gem_add_flow_filter(struct net_device *netdev, |
| 3321 | struct ethtool_rxnfc *cmd) |
| 3322 | { |
| 3323 | struct macb *bp = netdev_priv(netdev); |
| 3324 | struct ethtool_rx_flow_spec *fs = &cmd->fs; |
| 3325 | struct ethtool_rx_fs_item *item, *newfs; |
Julia Cartwright | 7038cdb | 2017-12-05 18:02:49 -0600 | [diff] [blame] | 3326 | unsigned long flags; |
Rafal Ozieblo | ae8223de | 2017-11-30 18:20:44 +0000 | [diff] [blame] | 3327 | int ret = -EINVAL; |
| 3328 | bool added = false; |
| 3329 | |
Julia Cartwright | cc1674e | 2017-12-05 18:02:50 -0600 | [diff] [blame] | 3330 | newfs = kmalloc(sizeof(*newfs), GFP_KERNEL); |
Rafal Ozieblo | ae8223de | 2017-11-30 18:20:44 +0000 | [diff] [blame] | 3331 | if (newfs == NULL) |
| 3332 | return -ENOMEM; |
| 3333 | memcpy(&newfs->fs, fs, sizeof(newfs->fs)); |
| 3334 | |
| 3335 | netdev_dbg(netdev, |
| 3336 | "Adding flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n", |
| 3337 | fs->flow_type, (int)fs->ring_cookie, fs->location, |
| 3338 | htonl(fs->h_u.tcp_ip4_spec.ip4src), |
| 3339 | htonl(fs->h_u.tcp_ip4_spec.ip4dst), |
| 3340 | htons(fs->h_u.tcp_ip4_spec.psrc), htons(fs->h_u.tcp_ip4_spec.pdst)); |
| 3341 | |
Julia Cartwright | 7038cdb | 2017-12-05 18:02:49 -0600 | [diff] [blame] | 3342 | spin_lock_irqsave(&bp->rx_fs_lock, flags); |
| 3343 | |
Rafal Ozieblo | ae8223de | 2017-11-30 18:20:44 +0000 | [diff] [blame] | 3344 | /* find correct place to add in list */ |
Julia Cartwright | a3da8ad | 2017-12-05 18:02:48 -0600 | [diff] [blame] | 3345 | list_for_each_entry(item, &bp->rx_fs_list.list, list) { |
| 3346 | if (item->fs.location > newfs->fs.location) { |
| 3347 | list_add_tail(&newfs->list, &item->list); |
| 3348 | added = true; |
| 3349 | break; |
| 3350 | } else if (item->fs.location == fs->location) { |
| 3351 | netdev_err(netdev, "Rule not added: location %d not free!\n", |
| 3352 | fs->location); |
| 3353 | ret = -EBUSY; |
| 3354 | goto err; |
Rafal Ozieblo | ae8223de | 2017-11-30 18:20:44 +0000 | [diff] [blame] | 3355 | } |
Rafal Ozieblo | ae8223de | 2017-11-30 18:20:44 +0000 | [diff] [blame] | 3356 | } |
Julia Cartwright | a3da8ad | 2017-12-05 18:02:48 -0600 | [diff] [blame] | 3357 | if (!added) |
| 3358 | list_add_tail(&newfs->list, &bp->rx_fs_list.list); |
Rafal Ozieblo | ae8223de | 2017-11-30 18:20:44 +0000 | [diff] [blame] | 3359 | |
| 3360 | gem_prog_cmp_regs(bp, fs); |
| 3361 | bp->rx_fs_list.count++; |
| 3362 | /* enable filtering if NTUPLE on */ |
Claudiu Beznea | c1e85c6c | 2019-05-22 08:24:43 +0000 | [diff] [blame] | 3363 | gem_enable_flow_filters(bp, 1); |
Rafal Ozieblo | ae8223de | 2017-11-30 18:20:44 +0000 | [diff] [blame] | 3364 | |
Julia Cartwright | 7038cdb | 2017-12-05 18:02:49 -0600 | [diff] [blame] | 3365 | spin_unlock_irqrestore(&bp->rx_fs_lock, flags); |
Rafal Ozieblo | ae8223de | 2017-11-30 18:20:44 +0000 | [diff] [blame] | 3366 | return 0; |
| 3367 | |
| 3368 | err: |
Julia Cartwright | 7038cdb | 2017-12-05 18:02:49 -0600 | [diff] [blame] | 3369 | spin_unlock_irqrestore(&bp->rx_fs_lock, flags); |
Rafal Ozieblo | ae8223de | 2017-11-30 18:20:44 +0000 | [diff] [blame] | 3370 | kfree(newfs); |
| 3371 | return ret; |
| 3372 | } |
| 3373 | |
| 3374 | static int gem_del_flow_filter(struct net_device *netdev, |
| 3375 | struct ethtool_rxnfc *cmd) |
| 3376 | { |
| 3377 | struct macb *bp = netdev_priv(netdev); |
| 3378 | struct ethtool_rx_fs_item *item; |
| 3379 | struct ethtool_rx_flow_spec *fs; |
Julia Cartwright | 7038cdb | 2017-12-05 18:02:49 -0600 | [diff] [blame] | 3380 | unsigned long flags; |
| 3381 | |
| 3382 | spin_lock_irqsave(&bp->rx_fs_lock, flags); |
Rafal Ozieblo | ae8223de | 2017-11-30 18:20:44 +0000 | [diff] [blame] | 3383 | |
Rafal Ozieblo | ae8223de | 2017-11-30 18:20:44 +0000 | [diff] [blame] | 3384 | list_for_each_entry(item, &bp->rx_fs_list.list, list) { |
| 3385 | if (item->fs.location == cmd->fs.location) { |
| 3386 | /* disable screener regs for the flow entry */ |
| 3387 | fs = &(item->fs); |
| 3388 | netdev_dbg(netdev, |
| 3389 | "Deleting flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n", |
| 3390 | fs->flow_type, (int)fs->ring_cookie, fs->location, |
| 3391 | htonl(fs->h_u.tcp_ip4_spec.ip4src), |
| 3392 | htonl(fs->h_u.tcp_ip4_spec.ip4dst), |
| 3393 | htons(fs->h_u.tcp_ip4_spec.psrc), |
| 3394 | htons(fs->h_u.tcp_ip4_spec.pdst)); |
| 3395 | |
| 3396 | gem_writel_n(bp, SCRT2, fs->location, 0); |
| 3397 | |
| 3398 | list_del(&item->list); |
Rafal Ozieblo | ae8223de | 2017-11-30 18:20:44 +0000 | [diff] [blame] | 3399 | bp->rx_fs_list.count--; |
Julia Cartwright | 7038cdb | 2017-12-05 18:02:49 -0600 | [diff] [blame] | 3400 | spin_unlock_irqrestore(&bp->rx_fs_lock, flags); |
| 3401 | kfree(item); |
Rafal Ozieblo | ae8223de | 2017-11-30 18:20:44 +0000 | [diff] [blame] | 3402 | return 0; |
| 3403 | } |
| 3404 | } |
| 3405 | |
Julia Cartwright | 7038cdb | 2017-12-05 18:02:49 -0600 | [diff] [blame] | 3406 | spin_unlock_irqrestore(&bp->rx_fs_lock, flags); |
Rafal Ozieblo | ae8223de | 2017-11-30 18:20:44 +0000 | [diff] [blame] | 3407 | return -EINVAL; |
| 3408 | } |
| 3409 | |
| 3410 | static int gem_get_flow_entry(struct net_device *netdev, |
| 3411 | struct ethtool_rxnfc *cmd) |
| 3412 | { |
| 3413 | struct macb *bp = netdev_priv(netdev); |
| 3414 | struct ethtool_rx_fs_item *item; |
| 3415 | |
| 3416 | list_for_each_entry(item, &bp->rx_fs_list.list, list) { |
| 3417 | if (item->fs.location == cmd->fs.location) { |
| 3418 | memcpy(&cmd->fs, &item->fs, sizeof(cmd->fs)); |
| 3419 | return 0; |
| 3420 | } |
| 3421 | } |
| 3422 | return -EINVAL; |
| 3423 | } |
| 3424 | |
| 3425 | static int gem_get_all_flow_entries(struct net_device *netdev, |
| 3426 | struct ethtool_rxnfc *cmd, u32 *rule_locs) |
| 3427 | { |
| 3428 | struct macb *bp = netdev_priv(netdev); |
| 3429 | struct ethtool_rx_fs_item *item; |
| 3430 | uint32_t cnt = 0; |
| 3431 | |
| 3432 | list_for_each_entry(item, &bp->rx_fs_list.list, list) { |
| 3433 | if (cnt == cmd->rule_cnt) |
| 3434 | return -EMSGSIZE; |
| 3435 | rule_locs[cnt] = item->fs.location; |
| 3436 | cnt++; |
| 3437 | } |
| 3438 | cmd->data = bp->max_tuples; |
| 3439 | cmd->rule_cnt = cnt; |
| 3440 | |
| 3441 | return 0; |
| 3442 | } |
| 3443 | |
| 3444 | static int gem_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd, |
| 3445 | u32 *rule_locs) |
| 3446 | { |
| 3447 | struct macb *bp = netdev_priv(netdev); |
| 3448 | int ret = 0; |
| 3449 | |
| 3450 | switch (cmd->cmd) { |
| 3451 | case ETHTOOL_GRXRINGS: |
| 3452 | cmd->data = bp->num_queues; |
| 3453 | break; |
| 3454 | case ETHTOOL_GRXCLSRLCNT: |
| 3455 | cmd->rule_cnt = bp->rx_fs_list.count; |
| 3456 | break; |
| 3457 | case ETHTOOL_GRXCLSRULE: |
| 3458 | ret = gem_get_flow_entry(netdev, cmd); |
| 3459 | break; |
| 3460 | case ETHTOOL_GRXCLSRLALL: |
| 3461 | ret = gem_get_all_flow_entries(netdev, cmd, rule_locs); |
| 3462 | break; |
| 3463 | default: |
| 3464 | netdev_err(netdev, |
| 3465 | "Command parameter %d is not supported\n", cmd->cmd); |
| 3466 | ret = -EOPNOTSUPP; |
| 3467 | } |
| 3468 | |
| 3469 | return ret; |
| 3470 | } |
| 3471 | |
| 3472 | static int gem_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd) |
| 3473 | { |
| 3474 | struct macb *bp = netdev_priv(netdev); |
Rafal Ozieblo | ae8223de | 2017-11-30 18:20:44 +0000 | [diff] [blame] | 3475 | int ret; |
| 3476 | |
Rafal Ozieblo | ae8223de | 2017-11-30 18:20:44 +0000 | [diff] [blame] | 3477 | switch (cmd->cmd) { |
| 3478 | case ETHTOOL_SRXCLSRLINS: |
| 3479 | if ((cmd->fs.location >= bp->max_tuples) |
| 3480 | || (cmd->fs.ring_cookie >= bp->num_queues)) { |
| 3481 | ret = -EINVAL; |
| 3482 | break; |
| 3483 | } |
| 3484 | ret = gem_add_flow_filter(netdev, cmd); |
| 3485 | break; |
| 3486 | case ETHTOOL_SRXCLSRLDEL: |
| 3487 | ret = gem_del_flow_filter(netdev, cmd); |
| 3488 | break; |
| 3489 | default: |
| 3490 | netdev_err(netdev, |
| 3491 | "Command parameter %d is not supported\n", cmd->cmd); |
| 3492 | ret = -EOPNOTSUPP; |
| 3493 | } |
| 3494 | |
Rafal Ozieblo | ae8223de | 2017-11-30 18:20:44 +0000 | [diff] [blame] | 3495 | return ret; |
| 3496 | } |
| 3497 | |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 3498 | static const struct ethtool_ops macb_ethtool_ops = { |
Nicolas Ferre | d1d1b53 | 2012-10-31 06:04:56 +0000 | [diff] [blame] | 3499 | .get_regs_len = macb_get_regs_len, |
| 3500 | .get_regs = macb_get_regs, |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 3501 | .get_link = ethtool_op_get_link, |
Richard Cochran | 17f393e | 2012-04-03 22:59:31 +0000 | [diff] [blame] | 3502 | .get_ts_info = ethtool_op_get_ts_info, |
Sergio Prado | 3e2a5e1 | 2016-02-09 12:07:16 -0200 | [diff] [blame] | 3503 | .get_wol = macb_get_wol, |
| 3504 | .set_wol = macb_set_wol, |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 3505 | .get_link_ksettings = macb_get_link_ksettings, |
| 3506 | .set_link_ksettings = macb_set_link_ksettings, |
Zach Brown | 8441bb3 | 2016-10-19 09:56:58 -0500 | [diff] [blame] | 3507 | .get_ringparam = macb_get_ringparam, |
| 3508 | .set_ringparam = macb_set_ringparam, |
Xander Huff | 8cd5a56 | 2015-01-15 15:55:20 -0600 | [diff] [blame] | 3509 | }; |
Xander Huff | 8cd5a56 | 2015-01-15 15:55:20 -0600 | [diff] [blame] | 3510 | |
Lad, Prabhakar | 8093b1c | 2015-02-05 16:21:07 +0000 | [diff] [blame] | 3511 | static const struct ethtool_ops gem_ethtool_ops = { |
Xander Huff | 8cd5a56 | 2015-01-15 15:55:20 -0600 | [diff] [blame] | 3512 | .get_regs_len = macb_get_regs_len, |
| 3513 | .get_regs = macb_get_regs, |
Nicolas Ferre | 558e35c | 2020-07-20 10:56:52 +0200 | [diff] [blame] | 3514 | .get_wol = macb_get_wol, |
| 3515 | .set_wol = macb_set_wol, |
Xander Huff | 8cd5a56 | 2015-01-15 15:55:20 -0600 | [diff] [blame] | 3516 | .get_link = ethtool_op_get_link, |
Andrei.Pistirica@microchip.com | c2594d8 | 2017-01-19 17:56:15 +0200 | [diff] [blame] | 3517 | .get_ts_info = macb_get_ts_info, |
Xander Huff | 3ff13f1 | 2015-01-13 16:15:51 -0600 | [diff] [blame] | 3518 | .get_ethtool_stats = gem_get_ethtool_stats, |
| 3519 | .get_strings = gem_get_ethtool_strings, |
| 3520 | .get_sset_count = gem_get_sset_count, |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 3521 | .get_link_ksettings = macb_get_link_ksettings, |
| 3522 | .set_link_ksettings = macb_set_link_ksettings, |
Zach Brown | 8441bb3 | 2016-10-19 09:56:58 -0500 | [diff] [blame] | 3523 | .get_ringparam = macb_get_ringparam, |
| 3524 | .set_ringparam = macb_set_ringparam, |
Rafal Ozieblo | ae8223de | 2017-11-30 18:20:44 +0000 | [diff] [blame] | 3525 | .get_rxnfc = gem_get_rxnfc, |
| 3526 | .set_rxnfc = gem_set_rxnfc, |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 3527 | }; |
| 3528 | |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 3529 | static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 3530 | { |
Andrei.Pistirica@microchip.com | c2594d8 | 2017-01-19 17:56:15 +0200 | [diff] [blame] | 3531 | struct macb *bp = netdev_priv(dev); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 3532 | |
| 3533 | if (!netif_running(dev)) |
| 3534 | return -EINVAL; |
| 3535 | |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 3536 | if (bp->ptp_info) { |
| 3537 | switch (cmd) { |
| 3538 | case SIOCSHWTSTAMP: |
| 3539 | return bp->ptp_info->set_hwtst(dev, rq, cmd); |
| 3540 | case SIOCGHWTSTAMP: |
| 3541 | return bp->ptp_info->get_hwtst(dev, rq); |
| 3542 | } |
Andrei.Pistirica@microchip.com | c2594d8 | 2017-01-19 17:56:15 +0200 | [diff] [blame] | 3543 | } |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 3544 | |
| 3545 | return phylink_mii_ioctl(bp->phylink, rq, cmd); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 3546 | } |
| 3547 | |
Claudiu Beznea | c1e85c6c | 2019-05-22 08:24:43 +0000 | [diff] [blame] | 3548 | static inline void macb_set_txcsum_feature(struct macb *bp, |
| 3549 | netdev_features_t features) |
| 3550 | { |
| 3551 | u32 val; |
| 3552 | |
| 3553 | if (!macb_is_gem(bp)) |
| 3554 | return; |
| 3555 | |
| 3556 | val = gem_readl(bp, DMACFG); |
| 3557 | if (features & NETIF_F_HW_CSUM) |
| 3558 | val |= GEM_BIT(TXCOEN); |
| 3559 | else |
| 3560 | val &= ~GEM_BIT(TXCOEN); |
| 3561 | |
| 3562 | gem_writel(bp, DMACFG, val); |
| 3563 | } |
| 3564 | |
| 3565 | static inline void macb_set_rxcsum_feature(struct macb *bp, |
| 3566 | netdev_features_t features) |
| 3567 | { |
| 3568 | struct net_device *netdev = bp->dev; |
| 3569 | u32 val; |
| 3570 | |
| 3571 | if (!macb_is_gem(bp)) |
| 3572 | return; |
| 3573 | |
| 3574 | val = gem_readl(bp, NCFGR); |
| 3575 | if ((features & NETIF_F_RXCSUM) && !(netdev->flags & IFF_PROMISC)) |
| 3576 | val |= GEM_BIT(RXCOEN); |
| 3577 | else |
| 3578 | val &= ~GEM_BIT(RXCOEN); |
| 3579 | |
| 3580 | gem_writel(bp, NCFGR, val); |
| 3581 | } |
| 3582 | |
| 3583 | static inline void macb_set_rxflow_feature(struct macb *bp, |
| 3584 | netdev_features_t features) |
| 3585 | { |
| 3586 | if (!macb_is_gem(bp)) |
| 3587 | return; |
| 3588 | |
| 3589 | gem_enable_flow_filters(bp, !!(features & NETIF_F_NTUPLE)); |
| 3590 | } |
| 3591 | |
Cyrille Pitchen | 85ff3d8 | 2014-07-24 13:51:00 +0200 | [diff] [blame] | 3592 | static int macb_set_features(struct net_device *netdev, |
| 3593 | netdev_features_t features) |
| 3594 | { |
| 3595 | struct macb *bp = netdev_priv(netdev); |
| 3596 | netdev_features_t changed = features ^ netdev->features; |
| 3597 | |
| 3598 | /* TX checksum offload */ |
Claudiu Beznea | c1e85c6c | 2019-05-22 08:24:43 +0000 | [diff] [blame] | 3599 | if (changed & NETIF_F_HW_CSUM) |
| 3600 | macb_set_txcsum_feature(bp, features); |
Cyrille Pitchen | 85ff3d8 | 2014-07-24 13:51:00 +0200 | [diff] [blame] | 3601 | |
Cyrille Pitchen | 924ec53 | 2014-07-24 13:51:01 +0200 | [diff] [blame] | 3602 | /* RX checksum offload */ |
Claudiu Beznea | c1e85c6c | 2019-05-22 08:24:43 +0000 | [diff] [blame] | 3603 | if (changed & NETIF_F_RXCSUM) |
| 3604 | macb_set_rxcsum_feature(bp, features); |
Cyrille Pitchen | 924ec53 | 2014-07-24 13:51:01 +0200 | [diff] [blame] | 3605 | |
Rafal Ozieblo | ae8223de | 2017-11-30 18:20:44 +0000 | [diff] [blame] | 3606 | /* RX Flow Filters */ |
Claudiu Beznea | c1e85c6c | 2019-05-22 08:24:43 +0000 | [diff] [blame] | 3607 | if (changed & NETIF_F_NTUPLE) |
| 3608 | macb_set_rxflow_feature(bp, features); |
Rafal Ozieblo | ae8223de | 2017-11-30 18:20:44 +0000 | [diff] [blame] | 3609 | |
Cyrille Pitchen | 85ff3d8 | 2014-07-24 13:51:00 +0200 | [diff] [blame] | 3610 | return 0; |
| 3611 | } |
| 3612 | |
Claudiu Beznea | c1e85c6c | 2019-05-22 08:24:43 +0000 | [diff] [blame] | 3613 | static void macb_restore_features(struct macb *bp) |
| 3614 | { |
| 3615 | struct net_device *netdev = bp->dev; |
| 3616 | netdev_features_t features = netdev->features; |
Claudiu Beznea | a14d273 | 2021-04-02 15:42:53 +0300 | [diff] [blame] | 3617 | struct ethtool_rx_fs_item *item; |
Claudiu Beznea | c1e85c6c | 2019-05-22 08:24:43 +0000 | [diff] [blame] | 3618 | |
| 3619 | /* TX checksum offload */ |
| 3620 | macb_set_txcsum_feature(bp, features); |
| 3621 | |
| 3622 | /* RX checksum offload */ |
| 3623 | macb_set_rxcsum_feature(bp, features); |
| 3624 | |
| 3625 | /* RX Flow Filters */ |
Claudiu Beznea | a14d273 | 2021-04-02 15:42:53 +0300 | [diff] [blame] | 3626 | list_for_each_entry(item, &bp->rx_fs_list.list, list) |
| 3627 | gem_prog_cmp_regs(bp, &item->fs); |
| 3628 | |
Claudiu Beznea | c1e85c6c | 2019-05-22 08:24:43 +0000 | [diff] [blame] | 3629 | macb_set_rxflow_feature(bp, features); |
| 3630 | } |
| 3631 | |
Alexander Beregalov | 5f1fa99 | 2009-04-11 07:42:26 +0000 | [diff] [blame] | 3632 | static const struct net_device_ops macb_netdev_ops = { |
| 3633 | .ndo_open = macb_open, |
| 3634 | .ndo_stop = macb_close, |
| 3635 | .ndo_start_xmit = macb_start_xmit, |
Jiri Pirko | afc4b13 | 2011-08-16 06:29:01 +0000 | [diff] [blame] | 3636 | .ndo_set_rx_mode = macb_set_rx_mode, |
Alexander Beregalov | 5f1fa99 | 2009-04-11 07:42:26 +0000 | [diff] [blame] | 3637 | .ndo_get_stats = macb_get_stats, |
Arnd Bergmann | a760537 | 2021-07-27 15:45:13 +0200 | [diff] [blame] | 3638 | .ndo_eth_ioctl = macb_ioctl, |
Alexander Beregalov | 5f1fa99 | 2009-04-11 07:42:26 +0000 | [diff] [blame] | 3639 | .ndo_validate_addr = eth_validate_addr, |
Harini Katakam | a5898ea | 2015-05-06 22:27:18 +0530 | [diff] [blame] | 3640 | .ndo_change_mtu = macb_change_mtu, |
Alexander Beregalov | 5f1fa99 | 2009-04-11 07:42:26 +0000 | [diff] [blame] | 3641 | .ndo_set_mac_address = eth_mac_addr, |
Thomas Petazzoni | 6e8cf5c | 2009-05-04 11:08:41 -0700 | [diff] [blame] | 3642 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 3643 | .ndo_poll_controller = macb_poll_controller, |
| 3644 | #endif |
Cyrille Pitchen | 85ff3d8 | 2014-07-24 13:51:00 +0200 | [diff] [blame] | 3645 | .ndo_set_features = macb_set_features, |
Rafal Ozieblo | 1629dd4 | 2016-11-16 10:02:34 +0000 | [diff] [blame] | 3646 | .ndo_features_check = macb_features_check, |
Alexander Beregalov | 5f1fa99 | 2009-04-11 07:42:26 +0000 | [diff] [blame] | 3647 | }; |
| 3648 | |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 3649 | /* Configure peripheral capabilities according to device tree |
Nicolas Ferre | e175587 | 2014-07-24 13:50:58 +0200 | [diff] [blame] | 3650 | * and integration options used |
| 3651 | */ |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 3652 | static void macb_configure_caps(struct macb *bp, |
| 3653 | const struct macb_config *dt_conf) |
Nicolas Ferre | e175587 | 2014-07-24 13:50:58 +0200 | [diff] [blame] | 3654 | { |
| 3655 | u32 dcfg; |
Nicolas Ferre | e175587 | 2014-07-24 13:50:58 +0200 | [diff] [blame] | 3656 | |
Nicolas Ferre | f697050 | 2015-03-31 15:02:01 +0200 | [diff] [blame] | 3657 | if (dt_conf) |
| 3658 | bp->caps = dt_conf->caps; |
| 3659 | |
Andy Shevchenko | f2ce8a9e | 2015-07-24 21:23:59 +0300 | [diff] [blame] | 3660 | if (hw_is_gem(bp->regs, bp->native_io)) { |
Nicolas Ferre | e175587 | 2014-07-24 13:50:58 +0200 | [diff] [blame] | 3661 | bp->caps |= MACB_CAPS_MACB_IS_GEM; |
| 3662 | |
Nicolas Ferre | e175587 | 2014-07-24 13:50:58 +0200 | [diff] [blame] | 3663 | dcfg = gem_readl(bp, DCFG1); |
| 3664 | if (GEM_BFEXT(IRQCOR, dcfg) == 0) |
| 3665 | bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE; |
Parshuram Thombare | e4e143e | 2020-10-29 13:47:07 +0100 | [diff] [blame] | 3666 | if (GEM_BFEXT(NO_PCS, dcfg) == 0) |
| 3667 | bp->caps |= MACB_CAPS_PCS; |
| 3668 | dcfg = gem_readl(bp, DCFG12); |
| 3669 | if (GEM_BFEXT(HIGH_SPEED, dcfg) == 1) |
| 3670 | bp->caps |= MACB_CAPS_HIGH_SPEED; |
Nicolas Ferre | e175587 | 2014-07-24 13:50:58 +0200 | [diff] [blame] | 3671 | dcfg = gem_readl(bp, DCFG2); |
| 3672 | if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0) |
| 3673 | bp->caps |= MACB_CAPS_FIFO_MODE; |
Rafal Ozieblo | ab91f0a | 2017-06-29 07:14:16 +0100 | [diff] [blame] | 3674 | #ifdef CONFIG_MACB_USE_HWSTAMP |
| 3675 | if (gem_has_ptp(bp)) { |
Rafal Ozieblo | 7b42961 | 2017-06-29 07:12:51 +0100 | [diff] [blame] | 3676 | if (!GEM_BFEXT(TSU, gem_readl(bp, DCFG5))) |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 3677 | dev_err(&bp->pdev->dev, |
| 3678 | "GEM doesn't support hardware ptp.\n"); |
Rafal Ozieblo | ab91f0a | 2017-06-29 07:14:16 +0100 | [diff] [blame] | 3679 | else { |
Rafal Ozieblo | 7b42961 | 2017-06-29 07:12:51 +0100 | [diff] [blame] | 3680 | bp->hw_dma_cap |= HW_DMA_CAP_PTP; |
Rafal Ozieblo | ab91f0a | 2017-06-29 07:14:16 +0100 | [diff] [blame] | 3681 | bp->ptp_info = &gem_ptp_info; |
| 3682 | } |
Rafal Ozieblo | 7b42961 | 2017-06-29 07:12:51 +0100 | [diff] [blame] | 3683 | } |
Rafal Ozieblo | ab91f0a | 2017-06-29 07:14:16 +0100 | [diff] [blame] | 3684 | #endif |
Nicolas Ferre | e175587 | 2014-07-24 13:50:58 +0200 | [diff] [blame] | 3685 | } |
| 3686 | |
Andy Shevchenko | a35919e | 2015-07-24 21:24:01 +0300 | [diff] [blame] | 3687 | dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps); |
Nicolas Ferre | e175587 | 2014-07-24 13:50:58 +0200 | [diff] [blame] | 3688 | } |
| 3689 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 3690 | static void macb_probe_queues(void __iomem *mem, |
Andy Shevchenko | f2ce8a9e | 2015-07-24 21:23:59 +0300 | [diff] [blame] | 3691 | bool native_io, |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 3692 | unsigned int *queue_mask, |
| 3693 | unsigned int *num_queues) |
| 3694 | { |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 3695 | *queue_mask = 0x1; |
| 3696 | *num_queues = 1; |
| 3697 | |
Nicolas Ferre | da12011 | 2015-03-31 15:02:00 +0200 | [diff] [blame] | 3698 | /* is it macb or gem ? |
| 3699 | * |
| 3700 | * We need to read directly from the hardware here because |
| 3701 | * we are early in the probe process and don't have the |
| 3702 | * MACB_CAPS_MACB_IS_GEM flag positioned |
| 3703 | */ |
Andy Shevchenko | f2ce8a9e | 2015-07-24 21:23:59 +0300 | [diff] [blame] | 3704 | if (!hw_is_gem(mem, native_io)) |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 3705 | return; |
| 3706 | |
| 3707 | /* bit 0 is never set but queue 0 always exists */ |
Claudiu Beznea | fec371f | 2020-07-02 12:05:58 +0300 | [diff] [blame] | 3708 | *queue_mask |= readl_relaxed(mem + GEM_DCFG6) & 0xff; |
Claudiu Beznea | b7ab39b | 2020-07-02 12:05:59 +0300 | [diff] [blame] | 3709 | *num_queues = hweight32(*queue_mask); |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 3710 | } |
| 3711 | |
Claudiu Beznea | 38493da | 2020-12-09 15:03:34 +0200 | [diff] [blame] | 3712 | static void macb_clks_disable(struct clk *pclk, struct clk *hclk, struct clk *tx_clk, |
| 3713 | struct clk *rx_clk, struct clk *tsu_clk) |
| 3714 | { |
| 3715 | struct clk_bulk_data clks[] = { |
| 3716 | { .clk = tsu_clk, }, |
| 3717 | { .clk = rx_clk, }, |
| 3718 | { .clk = pclk, }, |
| 3719 | { .clk = hclk, }, |
| 3720 | { .clk = tx_clk }, |
| 3721 | }; |
| 3722 | |
| 3723 | clk_bulk_disable_unprepare(ARRAY_SIZE(clks), clks); |
| 3724 | } |
| 3725 | |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 3726 | static int macb_clk_init(struct platform_device *pdev, struct clk **pclk, |
shubhrajyoti.datta@xilinx.com | aead88b | 2016-08-16 10:14:50 +0530 | [diff] [blame] | 3727 | struct clk **hclk, struct clk **tx_clk, |
Harini Katakam | f5473d1 | 2019-03-01 16:20:33 +0530 | [diff] [blame] | 3728 | struct clk **rx_clk, struct clk **tsu_clk) |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 3729 | { |
Bartosz Folta | 83a77e9 | 2016-12-14 06:39:15 +0000 | [diff] [blame] | 3730 | struct macb_platform_data *pdata; |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 3731 | int err; |
| 3732 | |
Bartosz Folta | 83a77e9 | 2016-12-14 06:39:15 +0000 | [diff] [blame] | 3733 | pdata = dev_get_platdata(&pdev->dev); |
| 3734 | if (pdata) { |
| 3735 | *pclk = pdata->pclk; |
| 3736 | *hclk = pdata->hclk; |
| 3737 | } else { |
| 3738 | *pclk = devm_clk_get(&pdev->dev, "pclk"); |
| 3739 | *hclk = devm_clk_get(&pdev->dev, "hclk"); |
| 3740 | } |
| 3741 | |
Michael Tretter | a04be4b | 2021-03-17 17:16:09 +0100 | [diff] [blame] | 3742 | if (IS_ERR_OR_NULL(*pclk)) |
| 3743 | return dev_err_probe(&pdev->dev, |
| 3744 | IS_ERR(*pclk) ? PTR_ERR(*pclk) : -ENODEV, |
| 3745 | "failed to get pclk\n"); |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 3746 | |
Michael Tretter | a04be4b | 2021-03-17 17:16:09 +0100 | [diff] [blame] | 3747 | if (IS_ERR_OR_NULL(*hclk)) |
| 3748 | return dev_err_probe(&pdev->dev, |
| 3749 | IS_ERR(*hclk) ? PTR_ERR(*hclk) : -ENODEV, |
| 3750 | "failed to get hclk\n"); |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 3751 | |
Michael Tretter | bd310aca | 2019-10-18 16:11:43 +0200 | [diff] [blame] | 3752 | *tx_clk = devm_clk_get_optional(&pdev->dev, "tx_clk"); |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 3753 | if (IS_ERR(*tx_clk)) |
Michael Tretter | bd310aca | 2019-10-18 16:11:43 +0200 | [diff] [blame] | 3754 | return PTR_ERR(*tx_clk); |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 3755 | |
Michael Tretter | bd310aca | 2019-10-18 16:11:43 +0200 | [diff] [blame] | 3756 | *rx_clk = devm_clk_get_optional(&pdev->dev, "rx_clk"); |
shubhrajyoti.datta@xilinx.com | aead88b | 2016-08-16 10:14:50 +0530 | [diff] [blame] | 3757 | if (IS_ERR(*rx_clk)) |
Michael Tretter | bd310aca | 2019-10-18 16:11:43 +0200 | [diff] [blame] | 3758 | return PTR_ERR(*rx_clk); |
shubhrajyoti.datta@xilinx.com | aead88b | 2016-08-16 10:14:50 +0530 | [diff] [blame] | 3759 | |
Michael Tretter | bd310aca | 2019-10-18 16:11:43 +0200 | [diff] [blame] | 3760 | *tsu_clk = devm_clk_get_optional(&pdev->dev, "tsu_clk"); |
Harini Katakam | f5473d1 | 2019-03-01 16:20:33 +0530 | [diff] [blame] | 3761 | if (IS_ERR(*tsu_clk)) |
Michael Tretter | bd310aca | 2019-10-18 16:11:43 +0200 | [diff] [blame] | 3762 | return PTR_ERR(*tsu_clk); |
Harini Katakam | f5473d1 | 2019-03-01 16:20:33 +0530 | [diff] [blame] | 3763 | |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 3764 | err = clk_prepare_enable(*pclk); |
| 3765 | if (err) { |
Luca Ceresoli | f413cbb | 2019-05-14 15:23:07 +0200 | [diff] [blame] | 3766 | dev_err(&pdev->dev, "failed to enable pclk (%d)\n", err); |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 3767 | return err; |
| 3768 | } |
| 3769 | |
| 3770 | err = clk_prepare_enable(*hclk); |
| 3771 | if (err) { |
Luca Ceresoli | f413cbb | 2019-05-14 15:23:07 +0200 | [diff] [blame] | 3772 | dev_err(&pdev->dev, "failed to enable hclk (%d)\n", err); |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 3773 | goto err_disable_pclk; |
| 3774 | } |
| 3775 | |
| 3776 | err = clk_prepare_enable(*tx_clk); |
| 3777 | if (err) { |
Luca Ceresoli | f413cbb | 2019-05-14 15:23:07 +0200 | [diff] [blame] | 3778 | dev_err(&pdev->dev, "failed to enable tx_clk (%d)\n", err); |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 3779 | goto err_disable_hclk; |
| 3780 | } |
| 3781 | |
shubhrajyoti.datta@xilinx.com | aead88b | 2016-08-16 10:14:50 +0530 | [diff] [blame] | 3782 | err = clk_prepare_enable(*rx_clk); |
| 3783 | if (err) { |
Luca Ceresoli | f413cbb | 2019-05-14 15:23:07 +0200 | [diff] [blame] | 3784 | dev_err(&pdev->dev, "failed to enable rx_clk (%d)\n", err); |
shubhrajyoti.datta@xilinx.com | aead88b | 2016-08-16 10:14:50 +0530 | [diff] [blame] | 3785 | goto err_disable_txclk; |
| 3786 | } |
| 3787 | |
Harini Katakam | f5473d1 | 2019-03-01 16:20:33 +0530 | [diff] [blame] | 3788 | err = clk_prepare_enable(*tsu_clk); |
| 3789 | if (err) { |
Luca Ceresoli | f413cbb | 2019-05-14 15:23:07 +0200 | [diff] [blame] | 3790 | dev_err(&pdev->dev, "failed to enable tsu_clk (%d)\n", err); |
Harini Katakam | f5473d1 | 2019-03-01 16:20:33 +0530 | [diff] [blame] | 3791 | goto err_disable_rxclk; |
| 3792 | } |
| 3793 | |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 3794 | return 0; |
| 3795 | |
Harini Katakam | f5473d1 | 2019-03-01 16:20:33 +0530 | [diff] [blame] | 3796 | err_disable_rxclk: |
| 3797 | clk_disable_unprepare(*rx_clk); |
| 3798 | |
shubhrajyoti.datta@xilinx.com | aead88b | 2016-08-16 10:14:50 +0530 | [diff] [blame] | 3799 | err_disable_txclk: |
| 3800 | clk_disable_unprepare(*tx_clk); |
| 3801 | |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 3802 | err_disable_hclk: |
| 3803 | clk_disable_unprepare(*hclk); |
| 3804 | |
| 3805 | err_disable_pclk: |
| 3806 | clk_disable_unprepare(*pclk); |
| 3807 | |
| 3808 | return err; |
| 3809 | } |
| 3810 | |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 3811 | static int macb_init(struct platform_device *pdev) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 3812 | { |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 3813 | struct net_device *dev = platform_get_drvdata(pdev); |
Nicolas Ferre | bfa0914 | 2015-03-31 15:01:59 +0200 | [diff] [blame] | 3814 | unsigned int hw_q, q; |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 3815 | struct macb *bp = netdev_priv(dev); |
| 3816 | struct macb_queue *queue; |
| 3817 | int err; |
Rafal Ozieblo | ae8223de | 2017-11-30 18:20:44 +0000 | [diff] [blame] | 3818 | u32 val, reg; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 3819 | |
Zach Brown | b410d13 | 2016-10-19 09:56:57 -0500 | [diff] [blame] | 3820 | bp->tx_ring_size = DEFAULT_TX_RING_SIZE; |
| 3821 | bp->rx_ring_size = DEFAULT_RX_RING_SIZE; |
| 3822 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 3823 | /* set the queue register mapping once for all: queue0 has a special |
| 3824 | * register mapping but we don't want to test the queue index then |
| 3825 | * compute the corresponding register offset at run time. |
| 3826 | */ |
Cyrille Pitchen | cf250de | 2014-12-15 15:13:32 +0100 | [diff] [blame] | 3827 | for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) { |
Nicolas Ferre | bfa0914 | 2015-03-31 15:01:59 +0200 | [diff] [blame] | 3828 | if (!(bp->queue_mask & (1 << hw_q))) |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 3829 | continue; |
Jamie Iles | 461845d | 2011-03-08 20:19:23 +0000 | [diff] [blame] | 3830 | |
Cyrille Pitchen | cf250de | 2014-12-15 15:13:32 +0100 | [diff] [blame] | 3831 | queue = &bp->queues[q]; |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 3832 | queue->bp = bp; |
Antoine Tenart | 760a3c1 | 2019-06-21 17:28:55 +0200 | [diff] [blame] | 3833 | netif_napi_add(dev, &queue->napi, macb_poll, NAPI_POLL_WEIGHT); |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 3834 | if (hw_q) { |
| 3835 | queue->ISR = GEM_ISR(hw_q - 1); |
| 3836 | queue->IER = GEM_IER(hw_q - 1); |
| 3837 | queue->IDR = GEM_IDR(hw_q - 1); |
| 3838 | queue->IMR = GEM_IMR(hw_q - 1); |
| 3839 | queue->TBQP = GEM_TBQP(hw_q - 1); |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 3840 | queue->RBQP = GEM_RBQP(hw_q - 1); |
| 3841 | queue->RBQS = GEM_RBQS(hw_q - 1); |
Harini Katakam | fff8019 | 2016-08-09 13:15:53 +0530 | [diff] [blame] | 3842 | #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 3843 | if (bp->hw_dma_cap & HW_DMA_CAP_64B) { |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 3844 | queue->TBQPH = GEM_TBQPH(hw_q - 1); |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 3845 | queue->RBQPH = GEM_RBQPH(hw_q - 1); |
| 3846 | } |
Harini Katakam | fff8019 | 2016-08-09 13:15:53 +0530 | [diff] [blame] | 3847 | #endif |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 3848 | } else { |
| 3849 | /* queue0 uses legacy registers */ |
| 3850 | queue->ISR = MACB_ISR; |
| 3851 | queue->IER = MACB_IER; |
| 3852 | queue->IDR = MACB_IDR; |
| 3853 | queue->IMR = MACB_IMR; |
| 3854 | queue->TBQP = MACB_TBQP; |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 3855 | queue->RBQP = MACB_RBQP; |
Harini Katakam | fff8019 | 2016-08-09 13:15:53 +0530 | [diff] [blame] | 3856 | #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 3857 | if (bp->hw_dma_cap & HW_DMA_CAP_64B) { |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 3858 | queue->TBQPH = MACB_TBQPH; |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 3859 | queue->RBQPH = MACB_RBQPH; |
| 3860 | } |
Harini Katakam | fff8019 | 2016-08-09 13:15:53 +0530 | [diff] [blame] | 3861 | #endif |
Soren Brinkmann | e1824df | 2013-12-10 16:07:23 -0800 | [diff] [blame] | 3862 | } |
Soren Brinkmann | e1824df | 2013-12-10 16:07:23 -0800 | [diff] [blame] | 3863 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 3864 | /* get irq: here we use the linux queue index, not the hardware |
| 3865 | * queue index. the queue irq definitions in the device tree |
| 3866 | * must remove the optional gaps that could exist in the |
| 3867 | * hardware queue mask. |
| 3868 | */ |
Cyrille Pitchen | cf250de | 2014-12-15 15:13:32 +0100 | [diff] [blame] | 3869 | queue->irq = platform_get_irq(pdev, q); |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 3870 | err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt, |
Punnaiah Choudary Kalluri | 2048823 | 2015-03-06 18:29:12 +0100 | [diff] [blame] | 3871 | IRQF_SHARED, dev->name, queue); |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 3872 | if (err) { |
| 3873 | dev_err(&pdev->dev, |
| 3874 | "Unable to request IRQ %d (error %d)\n", |
| 3875 | queue->irq, err); |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 3876 | return err; |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 3877 | } |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 3878 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 3879 | INIT_WORK(&queue->tx_error_task, macb_tx_error_task); |
Cyrille Pitchen | cf250de | 2014-12-15 15:13:32 +0100 | [diff] [blame] | 3880 | q++; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 3881 | } |
| 3882 | |
Alexander Beregalov | 5f1fa99 | 2009-04-11 07:42:26 +0000 | [diff] [blame] | 3883 | dev->netdev_ops = &macb_netdev_ops; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 3884 | |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 3885 | /* setup appropriated routines according to adapter type */ |
| 3886 | if (macb_is_gem(bp)) { |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 3887 | bp->max_tx_length = GEM_MAX_TX_LEN; |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 3888 | bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers; |
| 3889 | bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers; |
| 3890 | bp->macbgem_ops.mog_init_rings = gem_init_rings; |
| 3891 | bp->macbgem_ops.mog_rx = gem_rx; |
Xander Huff | 8cd5a56 | 2015-01-15 15:55:20 -0600 | [diff] [blame] | 3892 | dev->ethtool_ops = &gem_ethtool_ops; |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 3893 | } else { |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 3894 | bp->max_tx_length = MACB_MAX_TX_LEN; |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 3895 | bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers; |
| 3896 | bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers; |
| 3897 | bp->macbgem_ops.mog_init_rings = macb_init_rings; |
| 3898 | bp->macbgem_ops.mog_rx = macb_rx; |
Xander Huff | 8cd5a56 | 2015-01-15 15:55:20 -0600 | [diff] [blame] | 3899 | dev->ethtool_ops = &macb_ethtool_ops; |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 3900 | } |
| 3901 | |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 3902 | /* Set features */ |
| 3903 | dev->hw_features = NETIF_F_SG; |
Rafal Ozieblo | 1629dd4 | 2016-11-16 10:02:34 +0000 | [diff] [blame] | 3904 | |
| 3905 | /* Check LSO capability */ |
| 3906 | if (GEM_BFEXT(PBUF_LSO, gem_readl(bp, DCFG6))) |
| 3907 | dev->hw_features |= MACB_NETIF_LSO; |
| 3908 | |
Cyrille Pitchen | 85ff3d8 | 2014-07-24 13:51:00 +0200 | [diff] [blame] | 3909 | /* Checksum offload is only available on gem with packet buffer */ |
| 3910 | if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE)) |
Cyrille Pitchen | 924ec53 | 2014-07-24 13:51:01 +0200 | [diff] [blame] | 3911 | dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM; |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 3912 | if (bp->caps & MACB_CAPS_SG_DISABLED) |
| 3913 | dev->hw_features &= ~NETIF_F_SG; |
| 3914 | dev->features = dev->hw_features; |
| 3915 | |
Rafal Ozieblo | ae8223de | 2017-11-30 18:20:44 +0000 | [diff] [blame] | 3916 | /* Check RX Flow Filters support. |
| 3917 | * Max Rx flows set by availability of screeners & compare regs: |
| 3918 | * each 4-tuple define requires 1 T2 screener reg + 3 compare regs |
| 3919 | */ |
| 3920 | reg = gem_readl(bp, DCFG8); |
| 3921 | bp->max_tuples = min((GEM_BFEXT(SCR2CMP, reg) / 3), |
| 3922 | GEM_BFEXT(T2SCR, reg)); |
Claudiu Beznea | a714e27 | 2021-04-14 14:20:29 +0300 | [diff] [blame] | 3923 | INIT_LIST_HEAD(&bp->rx_fs_list.list); |
Rafal Ozieblo | ae8223de | 2017-11-30 18:20:44 +0000 | [diff] [blame] | 3924 | if (bp->max_tuples > 0) { |
| 3925 | /* also needs one ethtype match to check IPv4 */ |
| 3926 | if (GEM_BFEXT(SCR2ETH, reg) > 0) { |
| 3927 | /* program this reg now */ |
| 3928 | reg = 0; |
| 3929 | reg = GEM_BFINS(ETHTCMP, (uint16_t)ETH_P_IP, reg); |
| 3930 | gem_writel_n(bp, ETHT, SCRT2_ETHT, reg); |
| 3931 | /* Filtering is supported in hw but don't enable it in kernel now */ |
| 3932 | dev->hw_features |= NETIF_F_NTUPLE; |
| 3933 | /* init Rx flow definitions */ |
Rafal Ozieblo | ae8223de | 2017-11-30 18:20:44 +0000 | [diff] [blame] | 3934 | bp->rx_fs_list.count = 0; |
| 3935 | spin_lock_init(&bp->rx_fs_lock); |
| 3936 | } else |
| 3937 | bp->max_tuples = 0; |
| 3938 | } |
| 3939 | |
Neil Armstrong | ce721a7 | 2016-01-05 14:39:16 +0100 | [diff] [blame] | 3940 | if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) { |
| 3941 | val = 0; |
Alexandre Belloni | 2ccb016 | 2020-07-18 01:32:21 +0200 | [diff] [blame] | 3942 | if (phy_interface_mode_is_rgmii(bp->phy_interface)) |
Claudiu Beznea | edac638 | 2020-12-09 15:03:32 +0200 | [diff] [blame] | 3943 | val = bp->usrio->rgmii; |
Neil Armstrong | ce721a7 | 2016-01-05 14:39:16 +0100 | [diff] [blame] | 3944 | else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII && |
Nicolas Ferre | 6bdaa5e | 2016-03-10 16:44:32 +0100 | [diff] [blame] | 3945 | (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII)) |
Claudiu Beznea | edac638 | 2020-12-09 15:03:32 +0200 | [diff] [blame] | 3946 | val = bp->usrio->rmii; |
Nicolas Ferre | 6bdaa5e | 2016-03-10 16:44:32 +0100 | [diff] [blame] | 3947 | else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII)) |
Claudiu Beznea | edac638 | 2020-12-09 15:03:32 +0200 | [diff] [blame] | 3948 | val = bp->usrio->mii; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 3949 | |
Neil Armstrong | ce721a7 | 2016-01-05 14:39:16 +0100 | [diff] [blame] | 3950 | if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN) |
Claudiu Beznea | edac638 | 2020-12-09 15:03:32 +0200 | [diff] [blame] | 3951 | val |= bp->usrio->refclk; |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 3952 | |
Neil Armstrong | ce721a7 | 2016-01-05 14:39:16 +0100 | [diff] [blame] | 3953 | macb_or_gem_writel(bp, USRIO, val); |
| 3954 | } |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 3955 | |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 3956 | /* Set MII management clock divider */ |
| 3957 | val = macb_mdc_clk_div(bp); |
| 3958 | val |= macb_dbw(bp); |
Punnaiah Choudary Kalluri | 022be25 | 2015-11-18 09:03:50 +0530 | [diff] [blame] | 3959 | if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII) |
| 3960 | val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL); |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 3961 | macb_writel(bp, NCFGR, val); |
| 3962 | |
| 3963 | return 0; |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 3964 | } |
| 3965 | |
Atish Patra | b124223 | 2021-03-03 11:55:49 -0800 | [diff] [blame] | 3966 | static const struct macb_usrio_config macb_default_usrio = { |
| 3967 | .mii = MACB_BIT(MII), |
| 3968 | .rmii = MACB_BIT(RMII), |
| 3969 | .rgmii = GEM_BIT(RGMII), |
| 3970 | .refclk = MACB_BIT(CLKEN), |
| 3971 | }; |
| 3972 | |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 3973 | #if defined(CONFIG_OF) |
| 3974 | /* 1518 rounded up */ |
| 3975 | #define AT91ETHER_MAX_RBUFF_SZ 0x600 |
| 3976 | /* max number of receive buffers */ |
| 3977 | #define AT91ETHER_MAX_RX_DESCR 9 |
| 3978 | |
Arnd Bergmann | 49db922 | 2019-07-08 14:48:23 +0200 | [diff] [blame] | 3979 | static struct sifive_fu540_macb_mgmt *mgmt; |
| 3980 | |
Claudiu Beznea | 33fdef2 | 2020-06-24 13:08:18 +0300 | [diff] [blame] | 3981 | static int at91ether_alloc_coherent(struct macb *lp) |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 3982 | { |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 3983 | struct macb_queue *q = &lp->queues[0]; |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 3984 | |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 3985 | q->rx_ring = dma_alloc_coherent(&lp->pdev->dev, |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 3986 | (AT91ETHER_MAX_RX_DESCR * |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 3987 | macb_dma_desc_get_size(lp)), |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 3988 | &q->rx_ring_dma, GFP_KERNEL); |
| 3989 | if (!q->rx_ring) |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 3990 | return -ENOMEM; |
| 3991 | |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 3992 | q->rx_buffers = dma_alloc_coherent(&lp->pdev->dev, |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 3993 | AT91ETHER_MAX_RX_DESCR * |
| 3994 | AT91ETHER_MAX_RBUFF_SZ, |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 3995 | &q->rx_buffers_dma, GFP_KERNEL); |
| 3996 | if (!q->rx_buffers) { |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 3997 | dma_free_coherent(&lp->pdev->dev, |
| 3998 | AT91ETHER_MAX_RX_DESCR * |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 3999 | macb_dma_desc_get_size(lp), |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 4000 | q->rx_ring, q->rx_ring_dma); |
| 4001 | q->rx_ring = NULL; |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4002 | return -ENOMEM; |
| 4003 | } |
| 4004 | |
Claudiu Beznea | 33fdef2 | 2020-06-24 13:08:18 +0300 | [diff] [blame] | 4005 | return 0; |
| 4006 | } |
| 4007 | |
| 4008 | static void at91ether_free_coherent(struct macb *lp) |
| 4009 | { |
| 4010 | struct macb_queue *q = &lp->queues[0]; |
| 4011 | |
| 4012 | if (q->rx_ring) { |
| 4013 | dma_free_coherent(&lp->pdev->dev, |
| 4014 | AT91ETHER_MAX_RX_DESCR * |
| 4015 | macb_dma_desc_get_size(lp), |
| 4016 | q->rx_ring, q->rx_ring_dma); |
| 4017 | q->rx_ring = NULL; |
| 4018 | } |
| 4019 | |
| 4020 | if (q->rx_buffers) { |
| 4021 | dma_free_coherent(&lp->pdev->dev, |
| 4022 | AT91ETHER_MAX_RX_DESCR * |
| 4023 | AT91ETHER_MAX_RBUFF_SZ, |
| 4024 | q->rx_buffers, q->rx_buffers_dma); |
| 4025 | q->rx_buffers = NULL; |
| 4026 | } |
| 4027 | } |
| 4028 | |
| 4029 | /* Initialize and start the Receiver and Transmit subsystems */ |
| 4030 | static int at91ether_start(struct macb *lp) |
| 4031 | { |
| 4032 | struct macb_queue *q = &lp->queues[0]; |
| 4033 | struct macb_dma_desc *desc; |
| 4034 | dma_addr_t addr; |
| 4035 | u32 ctl; |
| 4036 | int i, ret; |
| 4037 | |
| 4038 | ret = at91ether_alloc_coherent(lp); |
| 4039 | if (ret) |
| 4040 | return ret; |
| 4041 | |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 4042 | addr = q->rx_buffers_dma; |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4043 | for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) { |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 4044 | desc = macb_rx_desc(q, i); |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 4045 | macb_set_addr(lp, desc, addr); |
| 4046 | desc->ctrl = 0; |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4047 | addr += AT91ETHER_MAX_RBUFF_SZ; |
| 4048 | } |
| 4049 | |
| 4050 | /* Set the Wrap bit on the last descriptor */ |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 4051 | desc->addr |= MACB_BIT(RX_WRAP); |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4052 | |
| 4053 | /* Reset buffer index */ |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 4054 | q->rx_tail = 0; |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4055 | |
| 4056 | /* Program address of descriptor list in Rx Buffer Queue register */ |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 4057 | macb_writel(lp, RBQP, q->rx_ring_dma); |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4058 | |
| 4059 | /* Enable Receive and Transmit */ |
| 4060 | ctl = macb_readl(lp, NCR); |
| 4061 | macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE)); |
| 4062 | |
Claudiu Beznea | 33fdef2 | 2020-06-24 13:08:18 +0300 | [diff] [blame] | 4063 | /* Enable MAC interrupts */ |
| 4064 | macb_writel(lp, IER, MACB_BIT(RCOMP) | |
| 4065 | MACB_BIT(RXUBR) | |
| 4066 | MACB_BIT(ISR_TUND) | |
| 4067 | MACB_BIT(ISR_RLE) | |
| 4068 | MACB_BIT(TCOMP) | |
| 4069 | MACB_BIT(ISR_ROVR) | |
| 4070 | MACB_BIT(HRESP)); |
| 4071 | |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4072 | return 0; |
| 4073 | } |
| 4074 | |
Claudiu Beznea | 33fdef2 | 2020-06-24 13:08:18 +0300 | [diff] [blame] | 4075 | static void at91ether_stop(struct macb *lp) |
| 4076 | { |
| 4077 | u32 ctl; |
| 4078 | |
| 4079 | /* Disable MAC interrupts */ |
| 4080 | macb_writel(lp, IDR, MACB_BIT(RCOMP) | |
| 4081 | MACB_BIT(RXUBR) | |
| 4082 | MACB_BIT(ISR_TUND) | |
| 4083 | MACB_BIT(ISR_RLE) | |
| 4084 | MACB_BIT(TCOMP) | |
| 4085 | MACB_BIT(ISR_ROVR) | |
| 4086 | MACB_BIT(HRESP)); |
| 4087 | |
| 4088 | /* Disable Receiver and Transmitter */ |
| 4089 | ctl = macb_readl(lp, NCR); |
| 4090 | macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE))); |
| 4091 | |
| 4092 | /* Free resources. */ |
| 4093 | at91ether_free_coherent(lp); |
| 4094 | } |
| 4095 | |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4096 | /* Open the ethernet interface */ |
| 4097 | static int at91ether_open(struct net_device *dev) |
| 4098 | { |
| 4099 | struct macb *lp = netdev_priv(dev); |
| 4100 | u32 ctl; |
| 4101 | int ret; |
| 4102 | |
Alexandre Belloni | e6a41c2 | 2020-02-12 17:45:38 +0100 | [diff] [blame] | 4103 | ret = pm_runtime_get_sync(&lp->pdev->dev); |
Andy Shevchenko | 0ce205d | 2020-04-27 13:51:20 +0300 | [diff] [blame] | 4104 | if (ret < 0) { |
| 4105 | pm_runtime_put_noidle(&lp->pdev->dev); |
Alexandre Belloni | e6a41c2 | 2020-02-12 17:45:38 +0100 | [diff] [blame] | 4106 | return ret; |
Andy Shevchenko | 0ce205d | 2020-04-27 13:51:20 +0300 | [diff] [blame] | 4107 | } |
Alexandre Belloni | e6a41c2 | 2020-02-12 17:45:38 +0100 | [diff] [blame] | 4108 | |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4109 | /* Clear internal statistics */ |
| 4110 | ctl = macb_readl(lp, NCR); |
| 4111 | macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT)); |
| 4112 | |
| 4113 | macb_set_hwaddr(lp); |
| 4114 | |
Claudiu Beznea | 33fdef2 | 2020-06-24 13:08:18 +0300 | [diff] [blame] | 4115 | ret = at91ether_start(lp); |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4116 | if (ret) |
Claudiu Beznea | 0eaf228 | 2020-06-24 13:08:17 +0300 | [diff] [blame] | 4117 | goto pm_exit; |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4118 | |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 4119 | ret = macb_phylink_connect(lp); |
| 4120 | if (ret) |
Claudiu Beznea | 33fdef2 | 2020-06-24 13:08:18 +0300 | [diff] [blame] | 4121 | goto stop; |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4122 | |
| 4123 | netif_start_queue(dev); |
| 4124 | |
| 4125 | return 0; |
Claudiu Beznea | 0eaf228 | 2020-06-24 13:08:17 +0300 | [diff] [blame] | 4126 | |
Claudiu Beznea | 33fdef2 | 2020-06-24 13:08:18 +0300 | [diff] [blame] | 4127 | stop: |
| 4128 | at91ether_stop(lp); |
Claudiu Beznea | 0eaf228 | 2020-06-24 13:08:17 +0300 | [diff] [blame] | 4129 | pm_exit: |
| 4130 | pm_runtime_put_sync(&lp->pdev->dev); |
| 4131 | return ret; |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4132 | } |
| 4133 | |
| 4134 | /* Close the interface */ |
| 4135 | static int at91ether_close(struct net_device *dev) |
| 4136 | { |
| 4137 | struct macb *lp = netdev_priv(dev); |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4138 | |
| 4139 | netif_stop_queue(dev); |
| 4140 | |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 4141 | phylink_stop(lp->phylink); |
| 4142 | phylink_disconnect_phy(lp->phylink); |
| 4143 | |
Claudiu Beznea | 33fdef2 | 2020-06-24 13:08:18 +0300 | [diff] [blame] | 4144 | at91ether_stop(lp); |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4145 | |
Alexandre Belloni | e6a41c2 | 2020-02-12 17:45:38 +0100 | [diff] [blame] | 4146 | return pm_runtime_put(&lp->pdev->dev); |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4147 | } |
| 4148 | |
| 4149 | /* Transmit packet */ |
Claudiu Beznea | d1c3895 | 2018-08-07 12:25:12 +0300 | [diff] [blame] | 4150 | static netdev_tx_t at91ether_start_xmit(struct sk_buff *skb, |
| 4151 | struct net_device *dev) |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4152 | { |
| 4153 | struct macb *lp = netdev_priv(dev); |
| 4154 | |
Willy Tarreau | 1d608d2 | 2020-12-09 19:47:40 +0100 | [diff] [blame] | 4155 | if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) { |
| 4156 | int desc = 0; |
| 4157 | |
| 4158 | netif_stop_queue(dev); |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4159 | |
| 4160 | /* Store packet information (to free when Tx completed) */ |
Willy Tarreau | 73d7422 | 2020-10-11 11:09:43 +0200 | [diff] [blame] | 4161 | lp->rm9200_txq[desc].skb = skb; |
| 4162 | lp->rm9200_txq[desc].size = skb->len; |
| 4163 | lp->rm9200_txq[desc].mapping = dma_map_single(&lp->pdev->dev, skb->data, |
| 4164 | skb->len, DMA_TO_DEVICE); |
| 4165 | if (dma_mapping_error(&lp->pdev->dev, lp->rm9200_txq[desc].mapping)) { |
Alexey Khoroshilov | 178c7ae | 2016-11-19 01:40:10 +0300 | [diff] [blame] | 4166 | dev_kfree_skb_any(skb); |
| 4167 | dev->stats.tx_dropped++; |
| 4168 | netdev_err(dev, "%s: DMA mapping error\n", __func__); |
| 4169 | return NETDEV_TX_OK; |
| 4170 | } |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4171 | |
| 4172 | /* Set address of the data in the Transmit Address register */ |
Willy Tarreau | 73d7422 | 2020-10-11 11:09:43 +0200 | [diff] [blame] | 4173 | macb_writel(lp, TAR, lp->rm9200_txq[desc].mapping); |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4174 | /* Set length of the packet in the Transmit Control register */ |
| 4175 | macb_writel(lp, TCR, skb->len); |
| 4176 | |
| 4177 | } else { |
| 4178 | netdev_err(dev, "%s called, but device is busy!\n", __func__); |
| 4179 | return NETDEV_TX_BUSY; |
| 4180 | } |
| 4181 | |
| 4182 | return NETDEV_TX_OK; |
| 4183 | } |
| 4184 | |
| 4185 | /* Extract received frame from buffer descriptors and sent to upper layers. |
| 4186 | * (Called from interrupt context) |
| 4187 | */ |
| 4188 | static void at91ether_rx(struct net_device *dev) |
| 4189 | { |
| 4190 | struct macb *lp = netdev_priv(dev); |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 4191 | struct macb_queue *q = &lp->queues[0]; |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 4192 | struct macb_dma_desc *desc; |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4193 | unsigned char *p_recv; |
| 4194 | struct sk_buff *skb; |
| 4195 | unsigned int pktlen; |
| 4196 | |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 4197 | desc = macb_rx_desc(q, q->rx_tail); |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 4198 | while (desc->addr & MACB_BIT(RX_USED)) { |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 4199 | p_recv = q->rx_buffers + q->rx_tail * AT91ETHER_MAX_RBUFF_SZ; |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 4200 | pktlen = MACB_BF(RX_FRMLEN, desc->ctrl); |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4201 | skb = netdev_alloc_skb(dev, pktlen + 2); |
| 4202 | if (skb) { |
| 4203 | skb_reserve(skb, 2); |
Johannes Berg | 59ae1d1 | 2017-06-16 14:29:20 +0200 | [diff] [blame] | 4204 | skb_put_data(skb, p_recv, pktlen); |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4205 | |
| 4206 | skb->protocol = eth_type_trans(skb, dev); |
Tobias Klauser | 5f1d3a5 | 2017-04-07 10:17:30 +0200 | [diff] [blame] | 4207 | dev->stats.rx_packets++; |
| 4208 | dev->stats.rx_bytes += pktlen; |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4209 | netif_rx(skb); |
| 4210 | } else { |
Tobias Klauser | 5f1d3a5 | 2017-04-07 10:17:30 +0200 | [diff] [blame] | 4211 | dev->stats.rx_dropped++; |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4212 | } |
| 4213 | |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 4214 | if (desc->ctrl & MACB_BIT(RX_MHASH_MATCH)) |
Tobias Klauser | 5f1d3a5 | 2017-04-07 10:17:30 +0200 | [diff] [blame] | 4215 | dev->stats.multicast++; |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4216 | |
| 4217 | /* reset ownership bit */ |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 4218 | desc->addr &= ~MACB_BIT(RX_USED); |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4219 | |
| 4220 | /* wrap after last buffer */ |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 4221 | if (q->rx_tail == AT91ETHER_MAX_RX_DESCR - 1) |
| 4222 | q->rx_tail = 0; |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4223 | else |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 4224 | q->rx_tail++; |
Rafal Ozieblo | dc97a89 | 2017-01-27 15:08:20 +0000 | [diff] [blame] | 4225 | |
Rafal Ozieblo | ae1f2a5 | 2017-11-30 18:19:15 +0000 | [diff] [blame] | 4226 | desc = macb_rx_desc(q, q->rx_tail); |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4227 | } |
| 4228 | } |
| 4229 | |
| 4230 | /* MAC interrupt handler */ |
| 4231 | static irqreturn_t at91ether_interrupt(int irq, void *dev_id) |
| 4232 | { |
| 4233 | struct net_device *dev = dev_id; |
| 4234 | struct macb *lp = netdev_priv(dev); |
| 4235 | u32 intstatus, ctl; |
Willy Tarreau | 73d7422 | 2020-10-11 11:09:43 +0200 | [diff] [blame] | 4236 | unsigned int desc; |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4237 | |
| 4238 | /* MAC Interrupt Status register indicates what interrupts are pending. |
| 4239 | * It is automatically cleared once read. |
| 4240 | */ |
| 4241 | intstatus = macb_readl(lp, ISR); |
| 4242 | |
| 4243 | /* Receive complete */ |
| 4244 | if (intstatus & MACB_BIT(RCOMP)) |
| 4245 | at91ether_rx(dev); |
| 4246 | |
| 4247 | /* Transmit complete */ |
Willy Tarreau | 1d608d2 | 2020-12-09 19:47:40 +0100 | [diff] [blame] | 4248 | if (intstatus & MACB_BIT(TCOMP)) { |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4249 | /* The TCOM bit is set even if the transmission failed */ |
| 4250 | if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE))) |
Tobias Klauser | 5f1d3a5 | 2017-04-07 10:17:30 +0200 | [diff] [blame] | 4251 | dev->stats.tx_errors++; |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4252 | |
Willy Tarreau | 1d608d2 | 2020-12-09 19:47:40 +0100 | [diff] [blame] | 4253 | desc = 0; |
| 4254 | if (lp->rm9200_txq[desc].skb) { |
Willy Tarreau | 73d7422 | 2020-10-11 11:09:43 +0200 | [diff] [blame] | 4255 | dev_consume_skb_irq(lp->rm9200_txq[desc].skb); |
| 4256 | lp->rm9200_txq[desc].skb = NULL; |
| 4257 | dma_unmap_single(&lp->pdev->dev, lp->rm9200_txq[desc].mapping, |
| 4258 | lp->rm9200_txq[desc].size, DMA_TO_DEVICE); |
Tobias Klauser | 5f1d3a5 | 2017-04-07 10:17:30 +0200 | [diff] [blame] | 4259 | dev->stats.tx_packets++; |
Willy Tarreau | 73d7422 | 2020-10-11 11:09:43 +0200 | [diff] [blame] | 4260 | dev->stats.tx_bytes += lp->rm9200_txq[desc].size; |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4261 | } |
Willy Tarreau | 1d608d2 | 2020-12-09 19:47:40 +0100 | [diff] [blame] | 4262 | netif_wake_queue(dev); |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4263 | } |
| 4264 | |
| 4265 | /* Work-around for EMAC Errata section 41.3.1 */ |
| 4266 | if (intstatus & MACB_BIT(RXUBR)) { |
| 4267 | ctl = macb_readl(lp, NCR); |
| 4268 | macb_writel(lp, NCR, ctl & ~MACB_BIT(RE)); |
Zumeng Chen | ffac0e9 | 2016-11-28 21:55:00 +0800 | [diff] [blame] | 4269 | wmb(); |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4270 | macb_writel(lp, NCR, ctl | MACB_BIT(RE)); |
| 4271 | } |
| 4272 | |
| 4273 | if (intstatus & MACB_BIT(ISR_ROVR)) |
| 4274 | netdev_err(dev, "ROVR error\n"); |
| 4275 | |
| 4276 | return IRQ_HANDLED; |
| 4277 | } |
| 4278 | |
| 4279 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 4280 | static void at91ether_poll_controller(struct net_device *dev) |
| 4281 | { |
| 4282 | unsigned long flags; |
| 4283 | |
| 4284 | local_irq_save(flags); |
| 4285 | at91ether_interrupt(dev->irq, dev); |
| 4286 | local_irq_restore(flags); |
| 4287 | } |
| 4288 | #endif |
| 4289 | |
| 4290 | static const struct net_device_ops at91ether_netdev_ops = { |
| 4291 | .ndo_open = at91ether_open, |
| 4292 | .ndo_stop = at91ether_close, |
| 4293 | .ndo_start_xmit = at91ether_start_xmit, |
| 4294 | .ndo_get_stats = macb_get_stats, |
| 4295 | .ndo_set_rx_mode = macb_set_rx_mode, |
| 4296 | .ndo_set_mac_address = eth_mac_addr, |
Arnd Bergmann | a760537 | 2021-07-27 15:45:13 +0200 | [diff] [blame] | 4297 | .ndo_eth_ioctl = macb_ioctl, |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4298 | .ndo_validate_addr = eth_validate_addr, |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4299 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 4300 | .ndo_poll_controller = at91ether_poll_controller, |
| 4301 | #endif |
| 4302 | }; |
| 4303 | |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 4304 | static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk, |
shubhrajyoti.datta@xilinx.com | aead88b | 2016-08-16 10:14:50 +0530 | [diff] [blame] | 4305 | struct clk **hclk, struct clk **tx_clk, |
Harini Katakam | f5473d1 | 2019-03-01 16:20:33 +0530 | [diff] [blame] | 4306 | struct clk **rx_clk, struct clk **tsu_clk) |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 4307 | { |
| 4308 | int err; |
| 4309 | |
| 4310 | *hclk = NULL; |
| 4311 | *tx_clk = NULL; |
shubhrajyoti.datta@xilinx.com | aead88b | 2016-08-16 10:14:50 +0530 | [diff] [blame] | 4312 | *rx_clk = NULL; |
Harini Katakam | f5473d1 | 2019-03-01 16:20:33 +0530 | [diff] [blame] | 4313 | *tsu_clk = NULL; |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 4314 | |
| 4315 | *pclk = devm_clk_get(&pdev->dev, "ether_clk"); |
| 4316 | if (IS_ERR(*pclk)) |
| 4317 | return PTR_ERR(*pclk); |
| 4318 | |
| 4319 | err = clk_prepare_enable(*pclk); |
| 4320 | if (err) { |
Luca Ceresoli | f413cbb | 2019-05-14 15:23:07 +0200 | [diff] [blame] | 4321 | dev_err(&pdev->dev, "failed to enable pclk (%d)\n", err); |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 4322 | return err; |
| 4323 | } |
| 4324 | |
| 4325 | return 0; |
| 4326 | } |
| 4327 | |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4328 | static int at91ether_init(struct platform_device *pdev) |
| 4329 | { |
| 4330 | struct net_device *dev = platform_get_drvdata(pdev); |
| 4331 | struct macb *bp = netdev_priv(dev); |
| 4332 | int err; |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4333 | |
Alexandre Belloni | fec9d3b | 2018-06-26 10:44:01 +0200 | [diff] [blame] | 4334 | bp->queues[0].bp = bp; |
| 4335 | |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4336 | dev->netdev_ops = &at91ether_netdev_ops; |
| 4337 | dev->ethtool_ops = &macb_ethtool_ops; |
| 4338 | |
| 4339 | err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt, |
| 4340 | 0, dev->name, dev); |
| 4341 | if (err) |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 4342 | return err; |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4343 | |
| 4344 | macb_writel(bp, NCR, 0); |
| 4345 | |
Alexandre Belloni | ac2fcfa | 2020-02-19 15:15:51 +0100 | [diff] [blame] | 4346 | macb_writel(bp, NCFGR, MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG)); |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4347 | |
| 4348 | return 0; |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4349 | } |
| 4350 | |
Yash Shah | c218ad5 | 2019-06-18 13:26:08 +0530 | [diff] [blame] | 4351 | static unsigned long fu540_macb_tx_recalc_rate(struct clk_hw *hw, |
| 4352 | unsigned long parent_rate) |
| 4353 | { |
| 4354 | return mgmt->rate; |
| 4355 | } |
| 4356 | |
| 4357 | static long fu540_macb_tx_round_rate(struct clk_hw *hw, unsigned long rate, |
| 4358 | unsigned long *parent_rate) |
| 4359 | { |
| 4360 | if (WARN_ON(rate < 2500000)) |
| 4361 | return 2500000; |
| 4362 | else if (rate == 2500000) |
| 4363 | return 2500000; |
| 4364 | else if (WARN_ON(rate < 13750000)) |
| 4365 | return 2500000; |
| 4366 | else if (WARN_ON(rate < 25000000)) |
| 4367 | return 25000000; |
| 4368 | else if (rate == 25000000) |
| 4369 | return 25000000; |
| 4370 | else if (WARN_ON(rate < 75000000)) |
| 4371 | return 25000000; |
| 4372 | else if (WARN_ON(rate < 125000000)) |
| 4373 | return 125000000; |
| 4374 | else if (rate == 125000000) |
| 4375 | return 125000000; |
| 4376 | |
| 4377 | WARN_ON(rate > 125000000); |
| 4378 | |
| 4379 | return 125000000; |
| 4380 | } |
| 4381 | |
| 4382 | static int fu540_macb_tx_set_rate(struct clk_hw *hw, unsigned long rate, |
| 4383 | unsigned long parent_rate) |
| 4384 | { |
| 4385 | rate = fu540_macb_tx_round_rate(hw, rate, &parent_rate); |
| 4386 | if (rate != 125000000) |
| 4387 | iowrite32(1, mgmt->reg); |
| 4388 | else |
| 4389 | iowrite32(0, mgmt->reg); |
| 4390 | mgmt->rate = rate; |
| 4391 | |
| 4392 | return 0; |
| 4393 | } |
| 4394 | |
| 4395 | static const struct clk_ops fu540_c000_ops = { |
| 4396 | .recalc_rate = fu540_macb_tx_recalc_rate, |
| 4397 | .round_rate = fu540_macb_tx_round_rate, |
| 4398 | .set_rate = fu540_macb_tx_set_rate, |
| 4399 | }; |
| 4400 | |
| 4401 | static int fu540_c000_clk_init(struct platform_device *pdev, struct clk **pclk, |
| 4402 | struct clk **hclk, struct clk **tx_clk, |
| 4403 | struct clk **rx_clk, struct clk **tsu_clk) |
| 4404 | { |
| 4405 | struct clk_init_data init; |
| 4406 | int err = 0; |
| 4407 | |
| 4408 | err = macb_clk_init(pdev, pclk, hclk, tx_clk, rx_clk, tsu_clk); |
| 4409 | if (err) |
| 4410 | return err; |
| 4411 | |
| 4412 | mgmt = devm_kzalloc(&pdev->dev, sizeof(*mgmt), GFP_KERNEL); |
Claudiu Beznea | f4de93f | 2020-12-09 15:03:35 +0200 | [diff] [blame] | 4413 | if (!mgmt) { |
| 4414 | err = -ENOMEM; |
| 4415 | goto err_disable_clks; |
| 4416 | } |
Yash Shah | c218ad5 | 2019-06-18 13:26:08 +0530 | [diff] [blame] | 4417 | |
| 4418 | init.name = "sifive-gemgxl-mgmt"; |
| 4419 | init.ops = &fu540_c000_ops; |
| 4420 | init.flags = 0; |
| 4421 | init.num_parents = 0; |
| 4422 | |
| 4423 | mgmt->rate = 0; |
| 4424 | mgmt->hw.init = &init; |
| 4425 | |
Stephen Boyd | d89091a | 2020-01-03 16:19:21 -0800 | [diff] [blame] | 4426 | *tx_clk = devm_clk_register(&pdev->dev, &mgmt->hw); |
Claudiu Beznea | f4de93f | 2020-12-09 15:03:35 +0200 | [diff] [blame] | 4427 | if (IS_ERR(*tx_clk)) { |
| 4428 | err = PTR_ERR(*tx_clk); |
| 4429 | goto err_disable_clks; |
| 4430 | } |
Yash Shah | c218ad5 | 2019-06-18 13:26:08 +0530 | [diff] [blame] | 4431 | |
| 4432 | err = clk_prepare_enable(*tx_clk); |
Claudiu Beznea | f4de93f | 2020-12-09 15:03:35 +0200 | [diff] [blame] | 4433 | if (err) { |
Yash Shah | c218ad5 | 2019-06-18 13:26:08 +0530 | [diff] [blame] | 4434 | dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err); |
Claudiu Beznea | f4de93f | 2020-12-09 15:03:35 +0200 | [diff] [blame] | 4435 | *tx_clk = NULL; |
| 4436 | goto err_disable_clks; |
| 4437 | } else { |
Yash Shah | c218ad5 | 2019-06-18 13:26:08 +0530 | [diff] [blame] | 4438 | dev_info(&pdev->dev, "Registered clk switch '%s'\n", init.name); |
Claudiu Beznea | f4de93f | 2020-12-09 15:03:35 +0200 | [diff] [blame] | 4439 | } |
Yash Shah | c218ad5 | 2019-06-18 13:26:08 +0530 | [diff] [blame] | 4440 | |
| 4441 | return 0; |
Claudiu Beznea | f4de93f | 2020-12-09 15:03:35 +0200 | [diff] [blame] | 4442 | |
| 4443 | err_disable_clks: |
| 4444 | macb_clks_disable(*pclk, *hclk, *tx_clk, *rx_clk, *tsu_clk); |
| 4445 | |
| 4446 | return err; |
Yash Shah | c218ad5 | 2019-06-18 13:26:08 +0530 | [diff] [blame] | 4447 | } |
| 4448 | |
| 4449 | static int fu540_c000_init(struct platform_device *pdev) |
| 4450 | { |
Dejin Zheng | b959c77 | 2020-05-03 20:32:26 +0800 | [diff] [blame] | 4451 | mgmt->reg = devm_platform_ioremap_resource(pdev, 1); |
| 4452 | if (IS_ERR(mgmt->reg)) |
| 4453 | return PTR_ERR(mgmt->reg); |
Yash Shah | c218ad5 | 2019-06-18 13:26:08 +0530 | [diff] [blame] | 4454 | |
| 4455 | return macb_init(pdev); |
| 4456 | } |
| 4457 | |
Claudiu Beznea | ec771de | 2020-12-09 15:03:38 +0200 | [diff] [blame] | 4458 | static const struct macb_usrio_config sama7g5_usrio = { |
| 4459 | .mii = 0, |
| 4460 | .rmii = 1, |
| 4461 | .rgmii = 2, |
| 4462 | .refclk = BIT(2), |
| 4463 | .hdfctlen = BIT(6), |
| 4464 | }; |
| 4465 | |
Yash Shah | c218ad5 | 2019-06-18 13:26:08 +0530 | [diff] [blame] | 4466 | static const struct macb_config fu540_c000_config = { |
| 4467 | .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO | |
| 4468 | MACB_CAPS_GEM_HAS_PTP, |
| 4469 | .dma_burst_length = 16, |
| 4470 | .clk_init = fu540_c000_clk_init, |
| 4471 | .init = fu540_c000_init, |
| 4472 | .jumbo_max_len = 10240, |
Claudiu Beznea | edac638 | 2020-12-09 15:03:32 +0200 | [diff] [blame] | 4473 | .usrio = &macb_default_usrio, |
Yash Shah | c218ad5 | 2019-06-18 13:26:08 +0530 | [diff] [blame] | 4474 | }; |
| 4475 | |
David S. Miller | 3cef5c5 | 2015-03-09 23:38:02 -0400 | [diff] [blame] | 4476 | static const struct macb_config at91sam9260_config = { |
Nicolas Ferre | 6bdaa5e | 2016-03-10 16:44:32 +0100 | [diff] [blame] | 4477 | .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII, |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 4478 | .clk_init = macb_clk_init, |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4479 | .init = macb_init, |
Claudiu Beznea | edac638 | 2020-12-09 15:03:32 +0200 | [diff] [blame] | 4480 | .usrio = &macb_default_usrio, |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4481 | }; |
| 4482 | |
Nicolas Ferre | eb4ed8e | 2018-09-14 17:48:10 +0200 | [diff] [blame] | 4483 | static const struct macb_config sama5d3macb_config = { |
| 4484 | .caps = MACB_CAPS_SG_DISABLED |
| 4485 | | MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII, |
| 4486 | .clk_init = macb_clk_init, |
| 4487 | .init = macb_init, |
Claudiu Beznea | edac638 | 2020-12-09 15:03:32 +0200 | [diff] [blame] | 4488 | .usrio = &macb_default_usrio, |
Nicolas Ferre | eb4ed8e | 2018-09-14 17:48:10 +0200 | [diff] [blame] | 4489 | }; |
| 4490 | |
David S. Miller | 3cef5c5 | 2015-03-09 23:38:02 -0400 | [diff] [blame] | 4491 | static const struct macb_config pc302gem_config = { |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4492 | .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE, |
| 4493 | .dma_burst_length = 16, |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 4494 | .clk_init = macb_clk_init, |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4495 | .init = macb_init, |
Claudiu Beznea | edac638 | 2020-12-09 15:03:32 +0200 | [diff] [blame] | 4496 | .usrio = &macb_default_usrio, |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4497 | }; |
| 4498 | |
Cyrille Pitchen | 5c8fe71 | 2015-06-18 16:27:23 +0200 | [diff] [blame] | 4499 | static const struct macb_config sama5d2_config = { |
Nicolas Ferre | 6bdaa5e | 2016-03-10 16:44:32 +0100 | [diff] [blame] | 4500 | .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII, |
Cyrille Pitchen | 5c8fe71 | 2015-06-18 16:27:23 +0200 | [diff] [blame] | 4501 | .dma_burst_length = 16, |
| 4502 | .clk_init = macb_clk_init, |
| 4503 | .init = macb_init, |
Claudiu Beznea | edac638 | 2020-12-09 15:03:32 +0200 | [diff] [blame] | 4504 | .usrio = &macb_default_usrio, |
Cyrille Pitchen | 5c8fe71 | 2015-06-18 16:27:23 +0200 | [diff] [blame] | 4505 | }; |
| 4506 | |
Hari Prasath | 7d13ad5 | 2021-08-12 13:14:21 +0530 | [diff] [blame] | 4507 | static const struct macb_config sama5d29_config = { |
| 4508 | .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_GEM_HAS_PTP, |
| 4509 | .dma_burst_length = 16, |
| 4510 | .clk_init = macb_clk_init, |
| 4511 | .init = macb_init, |
| 4512 | .usrio = &macb_default_usrio, |
| 4513 | }; |
| 4514 | |
David S. Miller | 3cef5c5 | 2015-03-09 23:38:02 -0400 | [diff] [blame] | 4515 | static const struct macb_config sama5d3_config = { |
Nicolas Ferre | 6bdaa5e | 2016-03-10 16:44:32 +0100 | [diff] [blame] | 4516 | .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE |
vishnuvardhan | 233a158 | 2017-07-05 17:36:16 +0200 | [diff] [blame] | 4517 | | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_JUMBO, |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4518 | .dma_burst_length = 16, |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 4519 | .clk_init = macb_clk_init, |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4520 | .init = macb_init, |
vishnuvardhan | 233a158 | 2017-07-05 17:36:16 +0200 | [diff] [blame] | 4521 | .jumbo_max_len = 10240, |
Claudiu Beznea | edac638 | 2020-12-09 15:03:32 +0200 | [diff] [blame] | 4522 | .usrio = &macb_default_usrio, |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4523 | }; |
| 4524 | |
David S. Miller | 3cef5c5 | 2015-03-09 23:38:02 -0400 | [diff] [blame] | 4525 | static const struct macb_config sama5d4_config = { |
Nicolas Ferre | 6bdaa5e | 2016-03-10 16:44:32 +0100 | [diff] [blame] | 4526 | .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII, |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4527 | .dma_burst_length = 4, |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 4528 | .clk_init = macb_clk_init, |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4529 | .init = macb_init, |
Claudiu Beznea | edac638 | 2020-12-09 15:03:32 +0200 | [diff] [blame] | 4530 | .usrio = &macb_default_usrio, |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4531 | }; |
| 4532 | |
David S. Miller | 3cef5c5 | 2015-03-09 23:38:02 -0400 | [diff] [blame] | 4533 | static const struct macb_config emac_config = { |
Alexandre Belloni | ac2fcfa | 2020-02-19 15:15:51 +0100 | [diff] [blame] | 4534 | .caps = MACB_CAPS_NEEDS_RSTONUBR | MACB_CAPS_MACB_IS_EMAC, |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 4535 | .clk_init = at91ether_clk_init, |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4536 | .init = at91ether_init, |
Claudiu Beznea | edac638 | 2020-12-09 15:03:32 +0200 | [diff] [blame] | 4537 | .usrio = &macb_default_usrio, |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4538 | }; |
| 4539 | |
Neil Armstrong | e611b5b | 2016-01-05 14:39:17 +0100 | [diff] [blame] | 4540 | static const struct macb_config np4_config = { |
| 4541 | .caps = MACB_CAPS_USRIO_DISABLED, |
| 4542 | .clk_init = macb_clk_init, |
| 4543 | .init = macb_init, |
Claudiu Beznea | edac638 | 2020-12-09 15:03:32 +0200 | [diff] [blame] | 4544 | .usrio = &macb_default_usrio, |
Neil Armstrong | e611b5b | 2016-01-05 14:39:17 +0100 | [diff] [blame] | 4545 | }; |
David S. Miller | 36583eb | 2015-05-23 01:22:35 -0400 | [diff] [blame] | 4546 | |
Harini Katakam | 7b61f9c | 2015-05-06 22:27:16 +0530 | [diff] [blame] | 4547 | static const struct macb_config zynqmp_config = { |
Rafal Ozieblo | ab91f0a | 2017-06-29 07:14:16 +0100 | [diff] [blame] | 4548 | .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | |
| 4549 | MACB_CAPS_JUMBO | |
Harini Katakam | 404cd08 | 2018-07-06 12:18:58 +0530 | [diff] [blame] | 4550 | MACB_CAPS_GEM_HAS_PTP | MACB_CAPS_BD_RD_PREFETCH, |
Harini Katakam | 7b61f9c | 2015-05-06 22:27:16 +0530 | [diff] [blame] | 4551 | .dma_burst_length = 16, |
| 4552 | .clk_init = macb_clk_init, |
| 4553 | .init = macb_init, |
Harini Katakam | 98b5a0f4 | 2015-05-06 22:27:17 +0530 | [diff] [blame] | 4554 | .jumbo_max_len = 10240, |
Claudiu Beznea | edac638 | 2020-12-09 15:03:32 +0200 | [diff] [blame] | 4555 | .usrio = &macb_default_usrio, |
Harini Katakam | 7b61f9c | 2015-05-06 22:27:16 +0530 | [diff] [blame] | 4556 | }; |
| 4557 | |
Nathan Sullivan | 222ca8e | 2015-05-22 09:22:10 -0500 | [diff] [blame] | 4558 | static const struct macb_config zynq_config = { |
Harini Katakam | e501070 | 2019-01-29 15:20:03 +0530 | [diff] [blame] | 4559 | .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF | |
| 4560 | MACB_CAPS_NEEDS_RSTONUBR, |
Nathan Sullivan | 222ca8e | 2015-05-22 09:22:10 -0500 | [diff] [blame] | 4561 | .dma_burst_length = 16, |
| 4562 | .clk_init = macb_clk_init, |
| 4563 | .init = macb_init, |
Claudiu Beznea | edac638 | 2020-12-09 15:03:32 +0200 | [diff] [blame] | 4564 | .usrio = &macb_default_usrio, |
Nathan Sullivan | 222ca8e | 2015-05-22 09:22:10 -0500 | [diff] [blame] | 4565 | }; |
| 4566 | |
Claudiu Beznea | ec771de | 2020-12-09 15:03:38 +0200 | [diff] [blame] | 4567 | static const struct macb_config sama7g5_gem_config = { |
Claudiu Beznea | 0f4f6d7 | 2021-09-17 16:26:15 +0300 | [diff] [blame] | 4568 | .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_CLK_HW_CHG | |
| 4569 | MACB_CAPS_MIIONRGMII, |
Claudiu Beznea | ec771de | 2020-12-09 15:03:38 +0200 | [diff] [blame] | 4570 | .dma_burst_length = 16, |
| 4571 | .clk_init = macb_clk_init, |
| 4572 | .init = macb_init, |
| 4573 | .usrio = &sama7g5_usrio, |
| 4574 | }; |
| 4575 | |
Claudiu Beznea | 700d566 | 2020-12-09 15:03:39 +0200 | [diff] [blame] | 4576 | static const struct macb_config sama7g5_emac_config = { |
Claudiu Beznea | 0f4f6d7 | 2021-09-17 16:26:15 +0300 | [diff] [blame] | 4577 | .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | |
| 4578 | MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_MIIONRGMII, |
Claudiu Beznea | 700d566 | 2020-12-09 15:03:39 +0200 | [diff] [blame] | 4579 | .dma_burst_length = 16, |
| 4580 | .clk_init = macb_clk_init, |
| 4581 | .init = macb_init, |
| 4582 | .usrio = &sama7g5_usrio, |
| 4583 | }; |
| 4584 | |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4585 | static const struct of_device_id macb_dt_ids[] = { |
| 4586 | { .compatible = "cdns,at32ap7000-macb" }, |
| 4587 | { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config }, |
| 4588 | { .compatible = "cdns,macb" }, |
Neil Armstrong | e611b5b | 2016-01-05 14:39:17 +0100 | [diff] [blame] | 4589 | { .compatible = "cdns,np4-macb", .data = &np4_config }, |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4590 | { .compatible = "cdns,pc302-gem", .data = &pc302gem_config }, |
| 4591 | { .compatible = "cdns,gem", .data = &pc302gem_config }, |
Nicolas Ferre | 3e3e0cd | 2019-02-06 18:56:10 +0100 | [diff] [blame] | 4592 | { .compatible = "cdns,sam9x60-macb", .data = &at91sam9260_config }, |
Cyrille Pitchen | 5c8fe71 | 2015-06-18 16:27:23 +0200 | [diff] [blame] | 4593 | { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config }, |
Hari Prasath | 7d13ad5 | 2021-08-12 13:14:21 +0530 | [diff] [blame] | 4594 | { .compatible = "atmel,sama5d29-gem", .data = &sama5d29_config }, |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4595 | { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config }, |
Nicolas Ferre | eb4ed8e | 2018-09-14 17:48:10 +0200 | [diff] [blame] | 4596 | { .compatible = "atmel,sama5d3-macb", .data = &sama5d3macb_config }, |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4597 | { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config }, |
| 4598 | { .compatible = "cdns,at91rm9200-emac", .data = &emac_config }, |
| 4599 | { .compatible = "cdns,emac", .data = &emac_config }, |
Harini Katakam | 7b61f9c | 2015-05-06 22:27:16 +0530 | [diff] [blame] | 4600 | { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config}, |
Nathan Sullivan | 222ca8e | 2015-05-22 09:22:10 -0500 | [diff] [blame] | 4601 | { .compatible = "cdns,zynq-gem", .data = &zynq_config }, |
Yash Shah | 6342ea8 | 2019-08-27 10:36:04 +0530 | [diff] [blame] | 4602 | { .compatible = "sifive,fu540-c000-gem", .data = &fu540_c000_config }, |
Claudiu Beznea | ec771de | 2020-12-09 15:03:38 +0200 | [diff] [blame] | 4603 | { .compatible = "microchip,sama7g5-gem", .data = &sama7g5_gem_config }, |
Claudiu Beznea | 700d566 | 2020-12-09 15:03:39 +0200 | [diff] [blame] | 4604 | { .compatible = "microchip,sama7g5-emac", .data = &sama7g5_emac_config }, |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4605 | { /* sentinel */ } |
| 4606 | }; |
| 4607 | MODULE_DEVICE_TABLE(of, macb_dt_ids); |
| 4608 | #endif /* CONFIG_OF */ |
| 4609 | |
Bartosz Folta | 83a77e9 | 2016-12-14 06:39:15 +0000 | [diff] [blame] | 4610 | static const struct macb_config default_gem_config = { |
Rafal Ozieblo | ab91f0a | 2017-06-29 07:14:16 +0100 | [diff] [blame] | 4611 | .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | |
| 4612 | MACB_CAPS_JUMBO | |
| 4613 | MACB_CAPS_GEM_HAS_PTP, |
Bartosz Folta | 83a77e9 | 2016-12-14 06:39:15 +0000 | [diff] [blame] | 4614 | .dma_burst_length = 16, |
| 4615 | .clk_init = macb_clk_init, |
| 4616 | .init = macb_init, |
Atish Patra | b124223 | 2021-03-03 11:55:49 -0800 | [diff] [blame] | 4617 | .usrio = &macb_default_usrio, |
Bartosz Folta | 83a77e9 | 2016-12-14 06:39:15 +0000 | [diff] [blame] | 4618 | .jumbo_max_len = 10240, |
| 4619 | }; |
| 4620 | |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4621 | static int macb_probe(struct platform_device *pdev) |
| 4622 | { |
Bartosz Folta | 83a77e9 | 2016-12-14 06:39:15 +0000 | [diff] [blame] | 4623 | const struct macb_config *macb_config = &default_gem_config; |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 4624 | int (*clk_init)(struct platform_device *, struct clk **, |
Harini Katakam | f5473d1 | 2019-03-01 16:20:33 +0530 | [diff] [blame] | 4625 | struct clk **, struct clk **, struct clk **, |
| 4626 | struct clk **) = macb_config->clk_init; |
Bartosz Folta | 83a77e9 | 2016-12-14 06:39:15 +0000 | [diff] [blame] | 4627 | int (*init)(struct platform_device *) = macb_config->init; |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4628 | struct device_node *np = pdev->dev.of_node; |
shubhrajyoti.datta@xilinx.com | aead88b | 2016-08-16 10:14:50 +0530 | [diff] [blame] | 4629 | struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL; |
Harini Katakam | f5473d1 | 2019-03-01 16:20:33 +0530 | [diff] [blame] | 4630 | struct clk *tsu_clk = NULL; |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4631 | unsigned int queue_mask, num_queues; |
Andy Shevchenko | f2ce8a9e | 2015-07-24 21:23:59 +0300 | [diff] [blame] | 4632 | bool native_io; |
Andrew Lunn | 0c65b2b | 2019-11-04 02:40:33 +0100 | [diff] [blame] | 4633 | phy_interface_t interface; |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4634 | struct net_device *dev; |
| 4635 | struct resource *regs; |
| 4636 | void __iomem *mem; |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4637 | struct macb *bp; |
Harini Katakam | 404cd08 | 2018-07-06 12:18:58 +0530 | [diff] [blame] | 4638 | int err, val; |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4639 | |
Yang Yingliang | 809660c | 2021-06-07 21:43:54 +0800 | [diff] [blame] | 4640 | mem = devm_platform_get_and_ioremap_resource(pdev, 0, ®s); |
Andy Shevchenko | f2ce8a9e | 2015-07-24 21:23:59 +0300 | [diff] [blame] | 4641 | if (IS_ERR(mem)) |
| 4642 | return PTR_ERR(mem); |
| 4643 | |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 4644 | if (np) { |
| 4645 | const struct of_device_id *match; |
| 4646 | |
| 4647 | match = of_match_node(macb_dt_ids, np); |
| 4648 | if (match && match->data) { |
| 4649 | macb_config = match->data; |
| 4650 | clk_init = macb_config->clk_init; |
| 4651 | init = macb_config->init; |
| 4652 | } |
| 4653 | } |
| 4654 | |
Harini Katakam | f5473d1 | 2019-03-01 16:20:33 +0530 | [diff] [blame] | 4655 | err = clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk, &tsu_clk); |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 4656 | if (err) |
| 4657 | return err; |
| 4658 | |
Harini Katakam | d54f89a | 2019-03-01 16:20:34 +0530 | [diff] [blame] | 4659 | pm_runtime_set_autosuspend_delay(&pdev->dev, MACB_PM_TIMEOUT); |
| 4660 | pm_runtime_use_autosuspend(&pdev->dev); |
| 4661 | pm_runtime_get_noresume(&pdev->dev); |
| 4662 | pm_runtime_set_active(&pdev->dev); |
| 4663 | pm_runtime_enable(&pdev->dev); |
Andy Shevchenko | f2ce8a9e | 2015-07-24 21:23:59 +0300 | [diff] [blame] | 4664 | native_io = hw_is_native_io(mem); |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4665 | |
Andy Shevchenko | f2ce8a9e | 2015-07-24 21:23:59 +0300 | [diff] [blame] | 4666 | macb_probe_queues(mem, native_io, &queue_mask, &num_queues); |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4667 | dev = alloc_etherdev_mq(sizeof(*bp), num_queues); |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 4668 | if (!dev) { |
| 4669 | err = -ENOMEM; |
| 4670 | goto err_disable_clocks; |
| 4671 | } |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4672 | |
| 4673 | dev->base_addr = regs->start; |
| 4674 | |
| 4675 | SET_NETDEV_DEV(dev, &pdev->dev); |
| 4676 | |
| 4677 | bp = netdev_priv(dev); |
| 4678 | bp->pdev = pdev; |
| 4679 | bp->dev = dev; |
| 4680 | bp->regs = mem; |
Andy Shevchenko | f2ce8a9e | 2015-07-24 21:23:59 +0300 | [diff] [blame] | 4681 | bp->native_io = native_io; |
| 4682 | if (native_io) { |
David S. Miller | 7a6e070 | 2015-07-27 14:24:48 -0700 | [diff] [blame] | 4683 | bp->macb_reg_readl = hw_readl_native; |
| 4684 | bp->macb_reg_writel = hw_writel_native; |
Andy Shevchenko | f2ce8a9e | 2015-07-24 21:23:59 +0300 | [diff] [blame] | 4685 | } else { |
David S. Miller | 7a6e070 | 2015-07-27 14:24:48 -0700 | [diff] [blame] | 4686 | bp->macb_reg_readl = hw_readl; |
| 4687 | bp->macb_reg_writel = hw_writel; |
Andy Shevchenko | f2ce8a9e | 2015-07-24 21:23:59 +0300 | [diff] [blame] | 4688 | } |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4689 | bp->num_queues = num_queues; |
Nicolas Ferre | bfa0914 | 2015-03-31 15:01:59 +0200 | [diff] [blame] | 4690 | bp->queue_mask = queue_mask; |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 4691 | if (macb_config) |
| 4692 | bp->dma_burst_length = macb_config->dma_burst_length; |
| 4693 | bp->pclk = pclk; |
| 4694 | bp->hclk = hclk; |
| 4695 | bp->tx_clk = tx_clk; |
shubhrajyoti.datta@xilinx.com | aead88b | 2016-08-16 10:14:50 +0530 | [diff] [blame] | 4696 | bp->rx_clk = rx_clk; |
Harini Katakam | f5473d1 | 2019-03-01 16:20:33 +0530 | [diff] [blame] | 4697 | bp->tsu_clk = tsu_clk; |
Andy Shevchenko | f36dbe6 | 2015-07-24 21:24:00 +0300 | [diff] [blame] | 4698 | if (macb_config) |
Harini Katakam | 98b5a0f4 | 2015-05-06 22:27:17 +0530 | [diff] [blame] | 4699 | bp->jumbo_max_len = macb_config->jumbo_max_len; |
Harini Katakam | 98b5a0f4 | 2015-05-06 22:27:17 +0530 | [diff] [blame] | 4700 | |
Sergio Prado | 3e2a5e1 | 2016-02-09 12:07:16 -0200 | [diff] [blame] | 4701 | bp->wol = 0; |
Sergio Prado | 7c4a1d0 | 2016-02-16 21:10:45 -0200 | [diff] [blame] | 4702 | if (of_get_property(np, "magic-packet", NULL)) |
Sergio Prado | 3e2a5e1 | 2016-02-09 12:07:16 -0200 | [diff] [blame] | 4703 | bp->wol |= MACB_WOL_HAS_MAGIC_PACKET; |
Nicolas Ferre | ced4799 | 2020-07-10 14:46:42 +0200 | [diff] [blame] | 4704 | device_set_wakeup_capable(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET); |
Sergio Prado | 3e2a5e1 | 2016-02-09 12:07:16 -0200 | [diff] [blame] | 4705 | |
Claudiu Beznea | edac638 | 2020-12-09 15:03:32 +0200 | [diff] [blame] | 4706 | bp->usrio = macb_config->usrio; |
| 4707 | |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4708 | spin_lock_init(&bp->lock); |
| 4709 | |
Nicolas Ferre | ad78347 | 2015-03-31 15:02:02 +0200 | [diff] [blame] | 4710 | /* setup capabilities */ |
Nicolas Ferre | f697050 | 2015-03-31 15:02:01 +0200 | [diff] [blame] | 4711 | macb_configure_caps(bp, macb_config); |
| 4712 | |
Rafal Ozieblo | 7b42961 | 2017-06-29 07:12:51 +0100 | [diff] [blame] | 4713 | #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT |
| 4714 | if (GEM_BFEXT(DAW64, gem_readl(bp, DCFG6))) { |
| 4715 | dma_set_mask(&pdev->dev, DMA_BIT_MASK(44)); |
| 4716 | bp->hw_dma_cap |= HW_DMA_CAP_64B; |
| 4717 | } |
| 4718 | #endif |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4719 | platform_set_drvdata(pdev, dev); |
| 4720 | |
| 4721 | dev->irq = platform_get_irq(pdev, 0); |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 4722 | if (dev->irq < 0) { |
| 4723 | err = dev->irq; |
Wei Yongjun | b22ae0b | 2016-08-12 15:43:54 +0000 | [diff] [blame] | 4724 | goto err_out_free_netdev; |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 4725 | } |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4726 | |
Jarod Wilson | 44770e1 | 2016-10-17 15:54:17 -0400 | [diff] [blame] | 4727 | /* MTU range: 68 - 1500 or 10240 */ |
| 4728 | dev->min_mtu = GEM_MTU_MIN_SIZE; |
| 4729 | if (bp->caps & MACB_CAPS_JUMBO) |
| 4730 | dev->max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN; |
| 4731 | else |
| 4732 | dev->max_mtu = ETH_DATA_LEN; |
| 4733 | |
Harini Katakam | 404cd08 | 2018-07-06 12:18:58 +0530 | [diff] [blame] | 4734 | if (bp->caps & MACB_CAPS_BD_RD_PREFETCH) { |
| 4735 | val = GEM_BFEXT(RXBD_RDBUFF, gem_readl(bp, DCFG10)); |
| 4736 | if (val) |
| 4737 | bp->rx_bd_rd_prefetch = (2 << (val - 1)) * |
| 4738 | macb_dma_desc_get_size(bp); |
| 4739 | |
| 4740 | val = GEM_BFEXT(TXBD_RDBUFF, gem_readl(bp, DCFG10)); |
| 4741 | if (val) |
| 4742 | bp->tx_bd_rd_prefetch = (2 << (val - 1)) * |
| 4743 | macb_dma_desc_get_size(bp); |
| 4744 | } |
| 4745 | |
Harini Katakam | e501070 | 2019-01-29 15:20:03 +0530 | [diff] [blame] | 4746 | bp->rx_intr_mask = MACB_RX_INT_FLAGS; |
| 4747 | if (bp->caps & MACB_CAPS_NEEDS_RSTONUBR) |
| 4748 | bp->rx_intr_mask |= MACB_BIT(RXUBR); |
| 4749 | |
Jakub Kicinski | 9ca01b2 | 2021-10-06 18:06:56 -0700 | [diff] [blame] | 4750 | err = of_get_ethdev_address(np, bp->dev); |
Michael Walle | 83216e3 | 2021-04-12 19:47:17 +0200 | [diff] [blame] | 4751 | if (err == -EPROBE_DEFER) |
Petr Å tetiar | 541ddc6 | 2019-05-03 16:27:08 +0200 | [diff] [blame] | 4752 | goto err_out_free_netdev; |
Michael Walle | 83216e3 | 2021-04-12 19:47:17 +0200 | [diff] [blame] | 4753 | else if (err) |
Petr Å tetiar | 541ddc6 | 2019-05-03 16:27:08 +0200 | [diff] [blame] | 4754 | macb_get_hwaddr(bp); |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 4755 | |
Andrew Lunn | 0c65b2b | 2019-11-04 02:40:33 +0100 | [diff] [blame] | 4756 | err = of_get_phy_mode(np, &interface); |
| 4757 | if (err) |
Nicolas Ferre | 8b95274 | 2019-05-03 12:36:58 +0200 | [diff] [blame] | 4758 | /* not found in DT, MII by default */ |
| 4759 | bp->phy_interface = PHY_INTERFACE_MODE_MII; |
| 4760 | else |
Andrew Lunn | 0c65b2b | 2019-11-04 02:40:33 +0100 | [diff] [blame] | 4761 | bp->phy_interface = interface; |
Jean-Christophe PLAGNIOL-VILLARD | fb97a84 | 2011-11-18 15:29:25 +0100 | [diff] [blame] | 4762 | |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4763 | /* IP specific init */ |
| 4764 | err = init(pdev); |
| 4765 | if (err) |
| 4766 | goto err_out_free_netdev; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 4767 | |
Florian Fainelli | cf66966 | 2016-05-02 18:38:45 -0700 | [diff] [blame] | 4768 | err = macb_mii_init(bp); |
| 4769 | if (err) |
| 4770 | goto err_out_free_netdev; |
| 4771 | |
Florian Fainelli | cf66966 | 2016-05-02 18:38:45 -0700 | [diff] [blame] | 4772 | netif_carrier_off(dev); |
| 4773 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 4774 | err = register_netdev(dev); |
| 4775 | if (err) { |
| 4776 | dev_err(&pdev->dev, "Cannot register net device, aborting.\n"); |
Florian Fainelli | cf66966 | 2016-05-02 18:38:45 -0700 | [diff] [blame] | 4777 | goto err_out_unregister_mdio; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 4778 | } |
| 4779 | |
Allen Pais | e7412b8 | 2020-09-14 12:59:23 +0530 | [diff] [blame] | 4780 | tasklet_setup(&bp->hresp_err_tasklet, macb_hresp_error_task); |
Harini Katakam | 032dc41 | 2018-01-27 12:09:01 +0530 | [diff] [blame] | 4781 | |
Bo Shen | 5879823 | 2014-09-13 01:57:49 +0200 | [diff] [blame] | 4782 | netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n", |
| 4783 | macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID), |
| 4784 | dev->base_addr, dev->irq, dev->dev_addr); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 4785 | |
Harini Katakam | d54f89a | 2019-03-01 16:20:34 +0530 | [diff] [blame] | 4786 | pm_runtime_mark_last_busy(&bp->pdev->dev); |
| 4787 | pm_runtime_put_autosuspend(&bp->pdev->dev); |
| 4788 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 4789 | return 0; |
| 4790 | |
Florian Fainelli | cf66966 | 2016-05-02 18:38:45 -0700 | [diff] [blame] | 4791 | err_out_unregister_mdio: |
Florian Fainelli | cf66966 | 2016-05-02 18:38:45 -0700 | [diff] [blame] | 4792 | mdiobus_unregister(bp->mii_bus); |
| 4793 | mdiobus_free(bp->mii_bus); |
| 4794 | |
Cyrille Pitchen | cf250de | 2014-12-15 15:13:32 +0100 | [diff] [blame] | 4795 | err_out_free_netdev: |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 4796 | free_netdev(dev); |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 4797 | |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 4798 | err_disable_clocks: |
Claudiu Beznea | 38493da | 2020-12-09 15:03:34 +0200 | [diff] [blame] | 4799 | macb_clks_disable(pclk, hclk, tx_clk, rx_clk, tsu_clk); |
Harini Katakam | d54f89a | 2019-03-01 16:20:34 +0530 | [diff] [blame] | 4800 | pm_runtime_disable(&pdev->dev); |
| 4801 | pm_runtime_set_suspended(&pdev->dev); |
| 4802 | pm_runtime_dont_use_autosuspend(&pdev->dev); |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 4803 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 4804 | return err; |
| 4805 | } |
| 4806 | |
Nicolae Rosia | 9e86d766 | 2015-01-22 17:31:05 +0000 | [diff] [blame] | 4807 | static int macb_remove(struct platform_device *pdev) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 4808 | { |
| 4809 | struct net_device *dev; |
| 4810 | struct macb *bp; |
| 4811 | |
| 4812 | dev = platform_get_drvdata(pdev); |
| 4813 | |
| 4814 | if (dev) { |
| 4815 | bp = netdev_priv(dev); |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 4816 | mdiobus_unregister(bp->mii_bus); |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 4817 | mdiobus_free(bp->mii_bus); |
Gregory CLEMENT | 5833e05 | 2015-12-11 11:34:53 +0100 | [diff] [blame] | 4818 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 4819 | unregister_netdev(dev); |
Chuhong Yuan | 61183b0 | 2019-11-28 10:00:21 +0800 | [diff] [blame] | 4820 | tasklet_kill(&bp->hresp_err_tasklet); |
Harini Katakam | d54f89a | 2019-03-01 16:20:34 +0530 | [diff] [blame] | 4821 | pm_runtime_disable(&pdev->dev); |
| 4822 | pm_runtime_dont_use_autosuspend(&pdev->dev); |
| 4823 | if (!pm_runtime_suspended(&pdev->dev)) { |
Claudiu Beznea | 38493da | 2020-12-09 15:03:34 +0200 | [diff] [blame] | 4824 | macb_clks_disable(bp->pclk, bp->hclk, bp->tx_clk, |
| 4825 | bp->rx_clk, bp->tsu_clk); |
Harini Katakam | d54f89a | 2019-03-01 16:20:34 +0530 | [diff] [blame] | 4826 | pm_runtime_set_suspended(&pdev->dev); |
| 4827 | } |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 4828 | phylink_destroy(bp->phylink); |
Cyrille Pitchen | e965be7 | 2014-12-15 15:13:31 +0100 | [diff] [blame] | 4829 | free_netdev(dev); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 4830 | } |
| 4831 | |
| 4832 | return 0; |
| 4833 | } |
| 4834 | |
Michal Simek | d23823d | 2015-01-23 09:36:03 +0100 | [diff] [blame] | 4835 | static int __maybe_unused macb_suspend(struct device *dev) |
Haavard Skinnemoen | c1f598f | 2008-03-04 13:39:29 +0100 | [diff] [blame] | 4836 | { |
Wolfram Sang | ce886a4 | 2018-10-21 22:00:14 +0200 | [diff] [blame] | 4837 | struct net_device *netdev = dev_get_drvdata(dev); |
Haavard Skinnemoen | c1f598f | 2008-03-04 13:39:29 +0100 | [diff] [blame] | 4838 | struct macb *bp = netdev_priv(netdev); |
Jiapeng Chong | bbf6ace | 2021-04-29 18:25:46 +0800 | [diff] [blame] | 4839 | struct macb_queue *queue; |
Harini Katakam | de991c5 | 2019-03-01 16:20:35 +0530 | [diff] [blame] | 4840 | unsigned long flags; |
| 4841 | unsigned int q; |
Nicolas Ferre | 558e35c | 2020-07-20 10:56:52 +0200 | [diff] [blame] | 4842 | int err; |
Haavard Skinnemoen | c1f598f | 2008-03-04 13:39:29 +0100 | [diff] [blame] | 4843 | |
Harini Katakam | de991c5 | 2019-03-01 16:20:35 +0530 | [diff] [blame] | 4844 | if (!netif_running(netdev)) |
| 4845 | return 0; |
| 4846 | |
Sergio Prado | 3e2a5e1 | 2016-02-09 12:07:16 -0200 | [diff] [blame] | 4847 | if (bp->wol & MACB_WOL_ENABLED) { |
Nicolas Ferre | 558e35c | 2020-07-20 10:56:52 +0200 | [diff] [blame] | 4848 | spin_lock_irqsave(&bp->lock, flags); |
| 4849 | /* Flush all status bits */ |
| 4850 | macb_writel(bp, TSR, -1); |
| 4851 | macb_writel(bp, RSR, -1); |
Harini Katakam | de991c5 | 2019-03-01 16:20:35 +0530 | [diff] [blame] | 4852 | for (q = 0, queue = bp->queues; q < bp->num_queues; |
Nicolas Ferre | 558e35c | 2020-07-20 10:56:52 +0200 | [diff] [blame] | 4853 | ++q, ++queue) { |
| 4854 | /* Disable all interrupts */ |
| 4855 | queue_writel(queue, IDR, -1); |
| 4856 | queue_readl(queue, ISR); |
| 4857 | if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE) |
| 4858 | queue_writel(queue, ISR, -1); |
| 4859 | } |
| 4860 | /* Change interrupt handler and |
| 4861 | * Enable WoL IRQ on queue 0 |
| 4862 | */ |
Nicolas Ferre | 9d45c8e | 2020-07-20 10:56:53 +0200 | [diff] [blame] | 4863 | devm_free_irq(dev, bp->queues[0].irq, bp->queues); |
Nicolas Ferre | 558e35c | 2020-07-20 10:56:52 +0200 | [diff] [blame] | 4864 | if (macb_is_gem(bp)) { |
Nicolas Ferre | 558e35c | 2020-07-20 10:56:52 +0200 | [diff] [blame] | 4865 | err = devm_request_irq(dev, bp->queues[0].irq, gem_wol_interrupt, |
| 4866 | IRQF_SHARED, netdev->name, bp->queues); |
| 4867 | if (err) { |
| 4868 | dev_err(dev, |
| 4869 | "Unable to request IRQ %d (error %d)\n", |
| 4870 | bp->queues[0].irq, err); |
| 4871 | spin_unlock_irqrestore(&bp->lock, flags); |
| 4872 | return err; |
| 4873 | } |
| 4874 | queue_writel(bp->queues, IER, GEM_BIT(WOL)); |
| 4875 | gem_writel(bp, WOL, MACB_BIT(MAG)); |
| 4876 | } else { |
Nicolas Ferre | 9d45c8e | 2020-07-20 10:56:53 +0200 | [diff] [blame] | 4877 | err = devm_request_irq(dev, bp->queues[0].irq, macb_wol_interrupt, |
| 4878 | IRQF_SHARED, netdev->name, bp->queues); |
| 4879 | if (err) { |
| 4880 | dev_err(dev, |
| 4881 | "Unable to request IRQ %d (error %d)\n", |
| 4882 | bp->queues[0].irq, err); |
| 4883 | spin_unlock_irqrestore(&bp->lock, flags); |
| 4884 | return err; |
| 4885 | } |
Nicolas Ferre | 558e35c | 2020-07-20 10:56:52 +0200 | [diff] [blame] | 4886 | queue_writel(bp->queues, IER, MACB_BIT(WOL)); |
| 4887 | macb_writel(bp, WOL, MACB_BIT(MAG)); |
| 4888 | } |
| 4889 | spin_unlock_irqrestore(&bp->lock, flags); |
| 4890 | |
| 4891 | enable_irq_wake(bp->queues[0].irq); |
| 4892 | } |
| 4893 | |
| 4894 | netif_device_detach(netdev); |
| 4895 | for (q = 0, queue = bp->queues; q < bp->num_queues; |
| 4896 | ++q, ++queue) |
| 4897 | napi_disable(&queue->napi); |
| 4898 | |
| 4899 | if (!(bp->wol & MACB_WOL_ENABLED)) { |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 4900 | rtnl_lock(); |
| 4901 | phylink_stop(bp->phylink); |
| 4902 | rtnl_unlock(); |
Harini Katakam | de991c5 | 2019-03-01 16:20:35 +0530 | [diff] [blame] | 4903 | spin_lock_irqsave(&bp->lock, flags); |
| 4904 | macb_reset_hw(bp); |
| 4905 | spin_unlock_irqrestore(&bp->lock, flags); |
Harini Katakam | d54f89a | 2019-03-01 16:20:34 +0530 | [diff] [blame] | 4906 | } |
| 4907 | |
Nicolas Ferre | 558e35c | 2020-07-20 10:56:52 +0200 | [diff] [blame] | 4908 | if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) |
| 4909 | bp->pm_data.usrio = macb_or_gem_readl(bp, USRIO); |
| 4910 | |
| 4911 | if (netdev->hw_features & NETIF_F_NTUPLE) |
| 4912 | bp->pm_data.scrt2 = gem_readl_n(bp, ETHT, SCRT2_ETHT); |
| 4913 | |
Harini Katakam | de991c5 | 2019-03-01 16:20:35 +0530 | [diff] [blame] | 4914 | if (bp->ptp_info) |
| 4915 | bp->ptp_info->ptp_remove(netdev); |
Nicolas Ferre | 6c8f85c | 2020-07-10 14:46:45 +0200 | [diff] [blame] | 4916 | if (!device_may_wakeup(dev)) |
| 4917 | pm_runtime_force_suspend(dev); |
Harini Katakam | d54f89a | 2019-03-01 16:20:34 +0530 | [diff] [blame] | 4918 | |
| 4919 | return 0; |
| 4920 | } |
| 4921 | |
| 4922 | static int __maybe_unused macb_resume(struct device *dev) |
| 4923 | { |
| 4924 | struct net_device *netdev = dev_get_drvdata(dev); |
| 4925 | struct macb *bp = netdev_priv(netdev); |
Jiapeng Chong | bbf6ace | 2021-04-29 18:25:46 +0800 | [diff] [blame] | 4926 | struct macb_queue *queue; |
Nicolas Ferre | 558e35c | 2020-07-20 10:56:52 +0200 | [diff] [blame] | 4927 | unsigned long flags; |
Harini Katakam | de991c5 | 2019-03-01 16:20:35 +0530 | [diff] [blame] | 4928 | unsigned int q; |
Nicolas Ferre | 558e35c | 2020-07-20 10:56:52 +0200 | [diff] [blame] | 4929 | int err; |
Harini Katakam | de991c5 | 2019-03-01 16:20:35 +0530 | [diff] [blame] | 4930 | |
| 4931 | if (!netif_running(netdev)) |
| 4932 | return 0; |
Harini Katakam | d54f89a | 2019-03-01 16:20:34 +0530 | [diff] [blame] | 4933 | |
Nicolas Ferre | 6c8f85c | 2020-07-10 14:46:45 +0200 | [diff] [blame] | 4934 | if (!device_may_wakeup(dev)) |
| 4935 | pm_runtime_force_resume(dev); |
Harini Katakam | d54f89a | 2019-03-01 16:20:34 +0530 | [diff] [blame] | 4936 | |
| 4937 | if (bp->wol & MACB_WOL_ENABLED) { |
Nicolas Ferre | 558e35c | 2020-07-20 10:56:52 +0200 | [diff] [blame] | 4938 | spin_lock_irqsave(&bp->lock, flags); |
| 4939 | /* Disable WoL */ |
| 4940 | if (macb_is_gem(bp)) { |
| 4941 | queue_writel(bp->queues, IDR, GEM_BIT(WOL)); |
| 4942 | gem_writel(bp, WOL, 0); |
| 4943 | } else { |
| 4944 | queue_writel(bp->queues, IDR, MACB_BIT(WOL)); |
| 4945 | macb_writel(bp, WOL, 0); |
| 4946 | } |
| 4947 | /* Clear ISR on queue 0 */ |
| 4948 | queue_readl(bp->queues, ISR); |
| 4949 | if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE) |
| 4950 | queue_writel(bp->queues, ISR, -1); |
| 4951 | /* Replace interrupt handler on queue 0 */ |
| 4952 | devm_free_irq(dev, bp->queues[0].irq, bp->queues); |
| 4953 | err = devm_request_irq(dev, bp->queues[0].irq, macb_interrupt, |
| 4954 | IRQF_SHARED, netdev->name, bp->queues); |
| 4955 | if (err) { |
| 4956 | dev_err(dev, |
| 4957 | "Unable to request IRQ %d (error %d)\n", |
| 4958 | bp->queues[0].irq, err); |
| 4959 | spin_unlock_irqrestore(&bp->lock, flags); |
| 4960 | return err; |
| 4961 | } |
| 4962 | spin_unlock_irqrestore(&bp->lock, flags); |
| 4963 | |
Harini Katakam | d54f89a | 2019-03-01 16:20:34 +0530 | [diff] [blame] | 4964 | disable_irq_wake(bp->queues[0].irq); |
Claudiu Beznea | c1e85c6c | 2019-05-22 08:24:43 +0000 | [diff] [blame] | 4965 | |
Nicolas Ferre | 558e35c | 2020-07-20 10:56:52 +0200 | [diff] [blame] | 4966 | /* Now make sure we disable phy before moving |
| 4967 | * to common restore path |
| 4968 | */ |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 4969 | rtnl_lock(); |
Nicolas Ferre | 558e35c | 2020-07-20 10:56:52 +0200 | [diff] [blame] | 4970 | phylink_stop(bp->phylink); |
Antoine Tenart | 7897b07 | 2019-11-13 10:00:06 +0100 | [diff] [blame] | 4971 | rtnl_unlock(); |
Harini Katakam | d54f89a | 2019-03-01 16:20:34 +0530 | [diff] [blame] | 4972 | } |
| 4973 | |
Nicolas Ferre | 558e35c | 2020-07-20 10:56:52 +0200 | [diff] [blame] | 4974 | for (q = 0, queue = bp->queues; q < bp->num_queues; |
| 4975 | ++q, ++queue) |
| 4976 | napi_enable(&queue->napi); |
| 4977 | |
| 4978 | if (netdev->hw_features & NETIF_F_NTUPLE) |
| 4979 | gem_writel_n(bp, ETHT, SCRT2_ETHT, bp->pm_data.scrt2); |
| 4980 | |
| 4981 | if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) |
| 4982 | macb_or_gem_writel(bp, USRIO, bp->pm_data.usrio); |
| 4983 | |
| 4984 | macb_writel(bp, NCR, MACB_BIT(MPE)); |
Harini Katakam | de991c5 | 2019-03-01 16:20:35 +0530 | [diff] [blame] | 4985 | macb_init_hw(bp); |
| 4986 | macb_set_rx_mode(netdev); |
Claudiu Beznea | c1e85c6c | 2019-05-22 08:24:43 +0000 | [diff] [blame] | 4987 | macb_restore_features(bp); |
Nicolas Ferre | 558e35c | 2020-07-20 10:56:52 +0200 | [diff] [blame] | 4988 | rtnl_lock(); |
| 4989 | phylink_start(bp->phylink); |
| 4990 | rtnl_unlock(); |
| 4991 | |
Harini Katakam | d54f89a | 2019-03-01 16:20:34 +0530 | [diff] [blame] | 4992 | netif_device_attach(netdev); |
Harini Katakam | de991c5 | 2019-03-01 16:20:35 +0530 | [diff] [blame] | 4993 | if (bp->ptp_info) |
| 4994 | bp->ptp_info->ptp_init(netdev); |
Harini Katakam | d54f89a | 2019-03-01 16:20:34 +0530 | [diff] [blame] | 4995 | |
| 4996 | return 0; |
| 4997 | } |
| 4998 | |
| 4999 | static int __maybe_unused macb_runtime_suspend(struct device *dev) |
| 5000 | { |
Wolfram Sang | f9cb759 | 2019-03-19 17:36:34 +0100 | [diff] [blame] | 5001 | struct net_device *netdev = dev_get_drvdata(dev); |
Harini Katakam | d54f89a | 2019-03-01 16:20:34 +0530 | [diff] [blame] | 5002 | struct macb *bp = netdev_priv(netdev); |
| 5003 | |
Claudiu Beznea | 38493da | 2020-12-09 15:03:34 +0200 | [diff] [blame] | 5004 | if (!(device_may_wakeup(dev))) |
| 5005 | macb_clks_disable(bp->pclk, bp->hclk, bp->tx_clk, bp->rx_clk, bp->tsu_clk); |
| 5006 | else |
| 5007 | macb_clks_disable(NULL, NULL, NULL, NULL, bp->tsu_clk); |
Haavard Skinnemoen | c1f598f | 2008-03-04 13:39:29 +0100 | [diff] [blame] | 5008 | |
| 5009 | return 0; |
| 5010 | } |
| 5011 | |
Harini Katakam | d54f89a | 2019-03-01 16:20:34 +0530 | [diff] [blame] | 5012 | static int __maybe_unused macb_runtime_resume(struct device *dev) |
Haavard Skinnemoen | c1f598f | 2008-03-04 13:39:29 +0100 | [diff] [blame] | 5013 | { |
Wolfram Sang | f9cb759 | 2019-03-19 17:36:34 +0100 | [diff] [blame] | 5014 | struct net_device *netdev = dev_get_drvdata(dev); |
Haavard Skinnemoen | c1f598f | 2008-03-04 13:39:29 +0100 | [diff] [blame] | 5015 | struct macb *bp = netdev_priv(netdev); |
| 5016 | |
Nicolas Ferre | 515a10a | 2020-07-10 14:46:41 +0200 | [diff] [blame] | 5017 | if (!(device_may_wakeup(dev))) { |
Sergio Prado | 3e2a5e1 | 2016-02-09 12:07:16 -0200 | [diff] [blame] | 5018 | clk_prepare_enable(bp->pclk); |
| 5019 | clk_prepare_enable(bp->hclk); |
| 5020 | clk_prepare_enable(bp->tx_clk); |
shubhrajyoti.datta@xilinx.com | aead88b | 2016-08-16 10:14:50 +0530 | [diff] [blame] | 5021 | clk_prepare_enable(bp->rx_clk); |
Sergio Prado | 3e2a5e1 | 2016-02-09 12:07:16 -0200 | [diff] [blame] | 5022 | } |
Harini Katakam | f5473d1 | 2019-03-01 16:20:33 +0530 | [diff] [blame] | 5023 | clk_prepare_enable(bp->tsu_clk); |
Haavard Skinnemoen | c1f598f | 2008-03-04 13:39:29 +0100 | [diff] [blame] | 5024 | |
Haavard Skinnemoen | c1f598f | 2008-03-04 13:39:29 +0100 | [diff] [blame] | 5025 | return 0; |
| 5026 | } |
Haavard Skinnemoen | c1f598f | 2008-03-04 13:39:29 +0100 | [diff] [blame] | 5027 | |
Harini Katakam | d54f89a | 2019-03-01 16:20:34 +0530 | [diff] [blame] | 5028 | static const struct dev_pm_ops macb_pm_ops = { |
| 5029 | SET_SYSTEM_SLEEP_PM_OPS(macb_suspend, macb_resume) |
| 5030 | SET_RUNTIME_PM_OPS(macb_runtime_suspend, macb_runtime_resume, NULL) |
| 5031 | }; |
Soren Brinkmann | 0dfc3e1 | 2013-12-10 16:07:19 -0800 | [diff] [blame] | 5032 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 5033 | static struct platform_driver macb_driver = { |
Nicolae Rosia | 9e86d766 | 2015-01-22 17:31:05 +0000 | [diff] [blame] | 5034 | .probe = macb_probe, |
| 5035 | .remove = macb_remove, |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 5036 | .driver = { |
| 5037 | .name = "macb", |
Jean-Christophe PLAGNIOL-VILLARD | fb97a84 | 2011-11-18 15:29:25 +0100 | [diff] [blame] | 5038 | .of_match_table = of_match_ptr(macb_dt_ids), |
Soren Brinkmann | 0dfc3e1 | 2013-12-10 16:07:19 -0800 | [diff] [blame] | 5039 | .pm = &macb_pm_ops, |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 5040 | }, |
| 5041 | }; |
| 5042 | |
Nicolae Rosia | 9e86d766 | 2015-01-22 17:31:05 +0000 | [diff] [blame] | 5043 | module_platform_driver(macb_driver); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 5044 | |
| 5045 | MODULE_LICENSE("GPL"); |
Jamie Iles | f75ba50 | 2011-11-08 10:12:32 +0000 | [diff] [blame] | 5046 | MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver"); |
Jean Delvare | e05503e | 2011-05-18 16:49:24 +0200 | [diff] [blame] | 5047 | MODULE_AUTHOR("Haavard Skinnemoen (Atmel)"); |
Kay Sievers | 72abb46 | 2008-04-18 13:50:44 -0700 | [diff] [blame] | 5048 | MODULE_ALIAS("platform:macb"); |