1 /* 2 * Driver for Marvell NETA network card for Armada XP and Armada 370 SoCs. 3 * 4 * Copyright (C) 2012 Marvell 5 * 6 * Rami Rosen <rosenr@marvell.com> 7 * Thomas Petazzoni <thomas.petazzoni@free-electrons.com> 8 * 9 * This file is licensed under the terms of the GNU General Public 10 * License version 2. This program is licensed "as is" without any 11 * warranty of any kind, whether express or implied. 12 */ 13 14 #include <linux/clk.h> 15 #include <linux/cpu.h> 16 #include <linux/etherdevice.h> 17 #include <linux/if_vlan.h> 18 #include <linux/inetdevice.h> 19 #include <linux/interrupt.h> 20 #include <linux/io.h> 21 #include <linux/kernel.h> 22 #include <linux/mbus.h> 23 #include <linux/module.h> 24 #include <linux/netdevice.h> 25 #include <linux/of.h> 26 #include <linux/of_address.h> 27 #include <linux/of_irq.h> 28 #include <linux/of_mdio.h> 29 #include <linux/of_net.h> 30 #include <linux/phy/phy.h> 31 #include <linux/phy.h> 32 #include <linux/phylink.h> 33 #include <linux/platform_device.h> 34 #include <linux/skbuff.h> 35 #include <net/hwbm.h> 36 #include "mvneta_bm.h" 37 #include <net/ip.h> 38 #include <net/ipv6.h> 39 #include <net/tso.h> 40 #include <net/page_pool.h> 41 #include <linux/bpf_trace.h> 42 43 /* Registers */ 44 #define MVNETA_RXQ_CONFIG_REG(q) (0x1400 + ((q) << 2)) 45 #define MVNETA_RXQ_HW_BUF_ALLOC BIT(0) 46 #define MVNETA_RXQ_SHORT_POOL_ID_SHIFT 4 47 #define MVNETA_RXQ_SHORT_POOL_ID_MASK 0x30 48 #define MVNETA_RXQ_LONG_POOL_ID_SHIFT 6 49 #define MVNETA_RXQ_LONG_POOL_ID_MASK 0xc0 50 #define MVNETA_RXQ_PKT_OFFSET_ALL_MASK (0xf << 8) 51 #define MVNETA_RXQ_PKT_OFFSET_MASK(offs) ((offs) << 8) 52 #define MVNETA_RXQ_THRESHOLD_REG(q) (0x14c0 + ((q) << 2)) 53 #define MVNETA_RXQ_NON_OCCUPIED(v) ((v) << 16) 54 #define MVNETA_RXQ_BASE_ADDR_REG(q) (0x1480 + ((q) << 2)) 55 #define MVNETA_RXQ_SIZE_REG(q) (0x14a0 + ((q) << 2)) 56 #define MVNETA_RXQ_BUF_SIZE_SHIFT 19 57 #define MVNETA_RXQ_BUF_SIZE_MASK (0x1fff << 19) 58 #define MVNETA_RXQ_STATUS_REG(q) (0x14e0 + ((q) << 2)) 59 #define MVNETA_RXQ_OCCUPIED_ALL_MASK 0x3fff 60 #define MVNETA_RXQ_STATUS_UPDATE_REG(q) (0x1500 + ((q) << 2)) 61 #define MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT 16 62 #define MVNETA_RXQ_ADD_NON_OCCUPIED_MAX 255 63 #define MVNETA_PORT_POOL_BUFFER_SZ_REG(pool) (0x1700 + ((pool) << 2)) 64 #define MVNETA_PORT_POOL_BUFFER_SZ_SHIFT 3 65 #define MVNETA_PORT_POOL_BUFFER_SZ_MASK 0xfff8 66 #define MVNETA_PORT_RX_RESET 0x1cc0 67 #define MVNETA_PORT_RX_DMA_RESET BIT(0) 68 #define MVNETA_PHY_ADDR 0x2000 69 #define MVNETA_PHY_ADDR_MASK 0x1f 70 #define MVNETA_MBUS_RETRY 0x2010 71 #define MVNETA_UNIT_INTR_CAUSE 0x2080 72 #define MVNETA_UNIT_CONTROL 0x20B0 73 #define MVNETA_PHY_POLLING_ENABLE BIT(1) 74 #define MVNETA_WIN_BASE(w) (0x2200 + ((w) << 3)) 75 #define MVNETA_WIN_SIZE(w) (0x2204 + ((w) << 3)) 76 #define MVNETA_WIN_REMAP(w) (0x2280 + ((w) << 2)) 77 #define MVNETA_BASE_ADDR_ENABLE 0x2290 78 #define MVNETA_ACCESS_PROTECT_ENABLE 0x2294 79 #define MVNETA_PORT_CONFIG 0x2400 80 #define MVNETA_UNI_PROMISC_MODE BIT(0) 81 #define MVNETA_DEF_RXQ(q) ((q) << 1) 82 #define MVNETA_DEF_RXQ_ARP(q) ((q) << 4) 83 #define MVNETA_TX_UNSET_ERR_SUM BIT(12) 84 #define MVNETA_DEF_RXQ_TCP(q) ((q) << 16) 85 #define MVNETA_DEF_RXQ_UDP(q) ((q) << 19) 86 #define MVNETA_DEF_RXQ_BPDU(q) ((q) << 22) 87 #define MVNETA_RX_CSUM_WITH_PSEUDO_HDR BIT(25) 88 #define MVNETA_PORT_CONFIG_DEFL_VALUE(q) (MVNETA_DEF_RXQ(q) | \ 89 MVNETA_DEF_RXQ_ARP(q) | \ 90 MVNETA_DEF_RXQ_TCP(q) | \ 91 MVNETA_DEF_RXQ_UDP(q) | \ 92 MVNETA_DEF_RXQ_BPDU(q) | \ 93 MVNETA_TX_UNSET_ERR_SUM | \ 94 MVNETA_RX_CSUM_WITH_PSEUDO_HDR) 95 #define MVNETA_PORT_CONFIG_EXTEND 0x2404 96 #define MVNETA_MAC_ADDR_LOW 0x2414 97 #define MVNETA_MAC_ADDR_HIGH 0x2418 98 #define MVNETA_SDMA_CONFIG 0x241c 99 #define MVNETA_SDMA_BRST_SIZE_16 4 100 #define MVNETA_RX_BRST_SZ_MASK(burst) ((burst) << 1) 101 #define MVNETA_RX_NO_DATA_SWAP BIT(4) 102 #define MVNETA_TX_NO_DATA_SWAP BIT(5) 103 #define MVNETA_DESC_SWAP BIT(6) 104 #define MVNETA_TX_BRST_SZ_MASK(burst) ((burst) << 22) 105 #define MVNETA_PORT_STATUS 0x2444 106 #define MVNETA_TX_IN_PRGRS BIT(1) 107 #define MVNETA_TX_FIFO_EMPTY BIT(8) 108 #define MVNETA_RX_MIN_FRAME_SIZE 0x247c 109 /* Only exists on Armada XP and Armada 370 */ 110 #define MVNETA_SERDES_CFG 0x24A0 111 #define MVNETA_SGMII_SERDES_PROTO 0x0cc7 112 #define MVNETA_QSGMII_SERDES_PROTO 0x0667 113 #define MVNETA_HSGMII_SERDES_PROTO 0x1107 114 #define MVNETA_TYPE_PRIO 0x24bc 115 #define MVNETA_FORCE_UNI BIT(21) 116 #define MVNETA_TXQ_CMD_1 0x24e4 117 #define MVNETA_TXQ_CMD 0x2448 118 #define MVNETA_TXQ_DISABLE_SHIFT 8 119 #define MVNETA_TXQ_ENABLE_MASK 0x000000ff 120 #define MVNETA_RX_DISCARD_FRAME_COUNT 0x2484 121 #define MVNETA_OVERRUN_FRAME_COUNT 0x2488 122 #define MVNETA_GMAC_CLOCK_DIVIDER 0x24f4 123 #define MVNETA_GMAC_1MS_CLOCK_ENABLE BIT(31) 124 #define MVNETA_ACC_MODE 0x2500 125 #define MVNETA_BM_ADDRESS 0x2504 126 #define MVNETA_CPU_MAP(cpu) (0x2540 + ((cpu) << 2)) 127 #define MVNETA_CPU_RXQ_ACCESS_ALL_MASK 0x000000ff 128 #define MVNETA_CPU_TXQ_ACCESS_ALL_MASK 0x0000ff00 129 #define MVNETA_CPU_RXQ_ACCESS(rxq) BIT(rxq) 130 #define MVNETA_CPU_TXQ_ACCESS(txq) BIT(txq + 8) 131 #define MVNETA_RXQ_TIME_COAL_REG(q) (0x2580 + ((q) << 2)) 132 133 /* Exception Interrupt Port/Queue Cause register 134 * 135 * Their behavior depend of the mapping done using the PCPX2Q 136 * registers. For a given CPU if the bit associated to a queue is not 137 * set, then for the register a read from this CPU will always return 138 * 0 and a write won't do anything 139 */ 140 141 #define MVNETA_INTR_NEW_CAUSE 0x25a0 142 #define MVNETA_INTR_NEW_MASK 0x25a4 143 144 /* bits 0..7 = TXQ SENT, one bit per queue. 145 * bits 8..15 = RXQ OCCUP, one bit per queue. 146 * bits 16..23 = RXQ FREE, one bit per queue. 147 * bit 29 = OLD_REG_SUM, see old reg ? 148 * bit 30 = TX_ERR_SUM, one bit for 4 ports 149 * bit 31 = MISC_SUM, one bit for 4 ports 150 */ 151 #define MVNETA_TX_INTR_MASK(nr_txqs) (((1 << nr_txqs) - 1) << 0) 152 #define MVNETA_TX_INTR_MASK_ALL (0xff << 0) 153 #define MVNETA_RX_INTR_MASK(nr_rxqs) (((1 << nr_rxqs) - 1) << 8) 154 #define MVNETA_RX_INTR_MASK_ALL (0xff << 8) 155 #define MVNETA_MISCINTR_INTR_MASK BIT(31) 156 157 #define MVNETA_INTR_OLD_CAUSE 0x25a8 158 #define MVNETA_INTR_OLD_MASK 0x25ac 159 160 /* Data Path Port/Queue Cause Register */ 161 #define MVNETA_INTR_MISC_CAUSE 0x25b0 162 #define MVNETA_INTR_MISC_MASK 0x25b4 163 164 #define MVNETA_CAUSE_PHY_STATUS_CHANGE BIT(0) 165 #define MVNETA_CAUSE_LINK_CHANGE BIT(1) 166 #define MVNETA_CAUSE_PTP BIT(4) 167 168 #define MVNETA_CAUSE_INTERNAL_ADDR_ERR BIT(7) 169 #define MVNETA_CAUSE_RX_OVERRUN BIT(8) 170 #define MVNETA_CAUSE_RX_CRC_ERROR BIT(9) 171 #define MVNETA_CAUSE_RX_LARGE_PKT BIT(10) 172 #define MVNETA_CAUSE_TX_UNDERUN BIT(11) 173 #define MVNETA_CAUSE_PRBS_ERR BIT(12) 174 #define MVNETA_CAUSE_PSC_SYNC_CHANGE BIT(13) 175 #define MVNETA_CAUSE_SERDES_SYNC_ERR BIT(14) 176 177 #define MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT 16 178 #define MVNETA_CAUSE_BMU_ALLOC_ERR_ALL_MASK (0xF << MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT) 179 #define MVNETA_CAUSE_BMU_ALLOC_ERR_MASK(pool) (1 << (MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT + (pool))) 180 181 #define MVNETA_CAUSE_TXQ_ERROR_SHIFT 24 182 #define MVNETA_CAUSE_TXQ_ERROR_ALL_MASK (0xFF << MVNETA_CAUSE_TXQ_ERROR_SHIFT) 183 #define MVNETA_CAUSE_TXQ_ERROR_MASK(q) (1 << (MVNETA_CAUSE_TXQ_ERROR_SHIFT + (q))) 184 185 #define MVNETA_INTR_ENABLE 0x25b8 186 #define MVNETA_TXQ_INTR_ENABLE_ALL_MASK 0x0000ff00 187 #define MVNETA_RXQ_INTR_ENABLE_ALL_MASK 0x000000ff 188 189 #define MVNETA_RXQ_CMD 0x2680 190 #define MVNETA_RXQ_DISABLE_SHIFT 8 191 #define MVNETA_RXQ_ENABLE_MASK 0x000000ff 192 #define MVETH_TXQ_TOKEN_COUNT_REG(q) (0x2700 + ((q) << 4)) 193 #define MVETH_TXQ_TOKEN_CFG_REG(q) (0x2704 + ((q) << 4)) 194 #define MVNETA_GMAC_CTRL_0 0x2c00 195 #define MVNETA_GMAC_MAX_RX_SIZE_SHIFT 2 196 #define MVNETA_GMAC_MAX_RX_SIZE_MASK 0x7ffc 197 #define MVNETA_GMAC0_PORT_1000BASE_X BIT(1) 198 #define MVNETA_GMAC0_PORT_ENABLE BIT(0) 199 #define MVNETA_GMAC_CTRL_2 0x2c08 200 #define MVNETA_GMAC2_INBAND_AN_ENABLE BIT(0) 201 #define MVNETA_GMAC2_PCS_ENABLE BIT(3) 202 #define MVNETA_GMAC2_PORT_RGMII BIT(4) 203 #define MVNETA_GMAC2_PORT_RESET BIT(6) 204 #define MVNETA_GMAC_STATUS 0x2c10 205 #define MVNETA_GMAC_LINK_UP BIT(0) 206 #define MVNETA_GMAC_SPEED_1000 BIT(1) 207 #define MVNETA_GMAC_SPEED_100 BIT(2) 208 #define MVNETA_GMAC_FULL_DUPLEX BIT(3) 209 #define MVNETA_GMAC_RX_FLOW_CTRL_ENABLE BIT(4) 210 #define MVNETA_GMAC_TX_FLOW_CTRL_ENABLE BIT(5) 211 #define MVNETA_GMAC_RX_FLOW_CTRL_ACTIVE BIT(6) 212 #define MVNETA_GMAC_TX_FLOW_CTRL_ACTIVE BIT(7) 213 #define MVNETA_GMAC_AN_COMPLETE BIT(11) 214 #define MVNETA_GMAC_SYNC_OK BIT(14) 215 #define MVNETA_GMAC_AUTONEG_CONFIG 0x2c0c 216 #define MVNETA_GMAC_FORCE_LINK_DOWN BIT(0) 217 #define MVNETA_GMAC_FORCE_LINK_PASS BIT(1) 218 #define MVNETA_GMAC_INBAND_AN_ENABLE BIT(2) 219 #define MVNETA_GMAC_AN_BYPASS_ENABLE BIT(3) 220 #define MVNETA_GMAC_INBAND_RESTART_AN BIT(4) 221 #define MVNETA_GMAC_CONFIG_MII_SPEED BIT(5) 222 #define MVNETA_GMAC_CONFIG_GMII_SPEED BIT(6) 223 #define MVNETA_GMAC_AN_SPEED_EN BIT(7) 224 #define MVNETA_GMAC_CONFIG_FLOW_CTRL BIT(8) 225 #define MVNETA_GMAC_ADVERT_SYM_FLOW_CTRL BIT(9) 226 #define MVNETA_GMAC_AN_FLOW_CTRL_EN BIT(11) 227 #define MVNETA_GMAC_CONFIG_FULL_DUPLEX BIT(12) 228 #define MVNETA_GMAC_AN_DUPLEX_EN BIT(13) 229 #define MVNETA_GMAC_CTRL_4 0x2c90 230 #define MVNETA_GMAC4_SHORT_PREAMBLE_ENABLE BIT(1) 231 #define MVNETA_MIB_COUNTERS_BASE 0x3000 232 #define MVNETA_MIB_LATE_COLLISION 0x7c 233 #define MVNETA_DA_FILT_SPEC_MCAST 0x3400 234 #define MVNETA_DA_FILT_OTH_MCAST 0x3500 235 #define MVNETA_DA_FILT_UCAST_BASE 0x3600 236 #define MVNETA_TXQ_BASE_ADDR_REG(q) (0x3c00 + ((q) << 2)) 237 #define MVNETA_TXQ_SIZE_REG(q) (0x3c20 + ((q) << 2)) 238 #define MVNETA_TXQ_SENT_THRESH_ALL_MASK 0x3fff0000 239 #define MVNETA_TXQ_SENT_THRESH_MASK(coal) ((coal) << 16) 240 #define MVNETA_TXQ_UPDATE_REG(q) (0x3c60 + ((q) << 2)) 241 #define MVNETA_TXQ_DEC_SENT_SHIFT 16 242 #define MVNETA_TXQ_DEC_SENT_MASK 0xff 243 #define MVNETA_TXQ_STATUS_REG(q) (0x3c40 + ((q) << 2)) 244 #define MVNETA_TXQ_SENT_DESC_SHIFT 16 245 #define MVNETA_TXQ_SENT_DESC_MASK 0x3fff0000 246 #define MVNETA_PORT_TX_RESET 0x3cf0 247 #define MVNETA_PORT_TX_DMA_RESET BIT(0) 248 #define MVNETA_TX_MTU 0x3e0c 249 #define MVNETA_TX_TOKEN_SIZE 0x3e14 250 #define MVNETA_TX_TOKEN_SIZE_MAX 0xffffffff 251 #define MVNETA_TXQ_TOKEN_SIZE_REG(q) (0x3e40 + ((q) << 2)) 252 #define MVNETA_TXQ_TOKEN_SIZE_MAX 0x7fffffff 253 254 #define MVNETA_LPI_CTRL_0 0x2cc0 255 #define MVNETA_LPI_CTRL_1 0x2cc4 256 #define MVNETA_LPI_REQUEST_ENABLE BIT(0) 257 #define MVNETA_LPI_CTRL_2 0x2cc8 258 #define MVNETA_LPI_STATUS 0x2ccc 259 260 #define MVNETA_CAUSE_TXQ_SENT_DESC_ALL_MASK 0xff 261 262 /* Descriptor ring Macros */ 263 #define MVNETA_QUEUE_NEXT_DESC(q, index) \ 264 (((index) < (q)->last_desc) ? ((index) + 1) : 0) 265 266 /* Various constants */ 267 268 /* Coalescing */ 269 #define MVNETA_TXDONE_COAL_PKTS 0 /* interrupt per packet */ 270 #define MVNETA_RX_COAL_PKTS 32 271 #define MVNETA_RX_COAL_USEC 100 272 273 /* The two bytes Marvell header. Either contains a special value used 274 * by Marvell switches when a specific hardware mode is enabled (not 275 * supported by this driver) or is filled automatically by zeroes on 276 * the RX side. Those two bytes being at the front of the Ethernet 277 * header, they allow to have the IP header aligned on a 4 bytes 278 * boundary automatically: the hardware skips those two bytes on its 279 * own. 280 */ 281 #define MVNETA_MH_SIZE 2 282 283 #define MVNETA_VLAN_TAG_LEN 4 284 285 #define MVNETA_TX_CSUM_DEF_SIZE 1600 286 #define MVNETA_TX_CSUM_MAX_SIZE 9800 287 #define MVNETA_ACC_MODE_EXT1 1 288 #define MVNETA_ACC_MODE_EXT2 2 289 290 #define MVNETA_MAX_DECODE_WIN 6 291 292 /* Timeout constants */ 293 #define MVNETA_TX_DISABLE_TIMEOUT_MSEC 1000 294 #define MVNETA_RX_DISABLE_TIMEOUT_MSEC 1000 295 #define MVNETA_TX_FIFO_EMPTY_TIMEOUT 10000 296 297 #define MVNETA_TX_MTU_MAX 0x3ffff 298 299 /* The RSS lookup table actually has 256 entries but we do not use 300 * them yet 301 */ 302 #define MVNETA_RSS_LU_TABLE_SIZE 1 303 304 /* Max number of Rx descriptors */ 305 #define MVNETA_MAX_RXD 512 306 307 /* Max number of Tx descriptors */ 308 #define MVNETA_MAX_TXD 1024 309 310 /* Max number of allowed TCP segments for software TSO */ 311 #define MVNETA_MAX_TSO_SEGS 100 312 313 #define MVNETA_MAX_SKB_DESCS (MVNETA_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS) 314 315 /* descriptor aligned size */ 316 #define MVNETA_DESC_ALIGNED_SIZE 32 317 318 /* Number of bytes to be taken into account by HW when putting incoming data 319 * to the buffers. It is needed in case NET_SKB_PAD exceeds maximum packet 320 * offset supported in MVNETA_RXQ_CONFIG_REG(q) registers. 321 */ 322 #define MVNETA_RX_PKT_OFFSET_CORRECTION 64 323 324 #define MVNETA_RX_PKT_SIZE(mtu) \ 325 ALIGN((mtu) + MVNETA_MH_SIZE + MVNETA_VLAN_TAG_LEN + \ 326 ETH_HLEN + ETH_FCS_LEN, \ 327 cache_line_size()) 328 329 /* Driver assumes that the last 3 bits are 0 */ 330 #define MVNETA_SKB_HEADROOM ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) 331 #define MVNETA_SKB_PAD (SKB_DATA_ALIGN(sizeof(struct skb_shared_info) + \ 332 MVNETA_SKB_HEADROOM)) 333 #define MVNETA_SKB_SIZE(len) (SKB_DATA_ALIGN(len) + MVNETA_SKB_PAD) 334 #define MVNETA_MAX_RX_BUF_SIZE (PAGE_SIZE - MVNETA_SKB_PAD) 335 336 #define IS_TSO_HEADER(txq, addr) \ 337 ((addr >= txq->tso_hdrs_phys) && \ 338 (addr < txq->tso_hdrs_phys + txq->size * TSO_HEADER_SIZE)) 339 340 #define MVNETA_RX_GET_BM_POOL_ID(rxd) \ 341 (((rxd)->status & MVNETA_RXD_BM_POOL_MASK) >> MVNETA_RXD_BM_POOL_SHIFT) 342 343 enum { 344 ETHTOOL_STAT_EEE_WAKEUP, 345 ETHTOOL_STAT_SKB_ALLOC_ERR, 346 ETHTOOL_STAT_REFILL_ERR, 347 ETHTOOL_XDP_REDIRECT, 348 ETHTOOL_XDP_PASS, 349 ETHTOOL_XDP_DROP, 350 ETHTOOL_XDP_TX, 351 ETHTOOL_XDP_TX_ERR, 352 ETHTOOL_XDP_XMIT, 353 ETHTOOL_XDP_XMIT_ERR, 354 ETHTOOL_MAX_STATS, 355 }; 356 357 struct mvneta_statistic { 358 unsigned short offset; 359 unsigned short type; 360 const char name[ETH_GSTRING_LEN]; 361 }; 362 363 #define T_REG_32 32 364 #define T_REG_64 64 365 #define T_SW 1 366 367 #define MVNETA_XDP_PASS 0 368 #define MVNETA_XDP_DROPPED BIT(0) 369 #define MVNETA_XDP_TX BIT(1) 370 #define MVNETA_XDP_REDIR BIT(2) 371 372 static const struct mvneta_statistic mvneta_statistics[] = { 373 { 0x3000, T_REG_64, "good_octets_received", }, 374 { 0x3010, T_REG_32, "good_frames_received", }, 375 { 0x3008, T_REG_32, "bad_octets_received", }, 376 { 0x3014, T_REG_32, "bad_frames_received", }, 377 { 0x3018, T_REG_32, "broadcast_frames_received", }, 378 { 0x301c, T_REG_32, "multicast_frames_received", }, 379 { 0x3050, T_REG_32, "unrec_mac_control_received", }, 380 { 0x3058, T_REG_32, "good_fc_received", }, 381 { 0x305c, T_REG_32, "bad_fc_received", }, 382 { 0x3060, T_REG_32, "undersize_received", }, 383 { 0x3064, T_REG_32, "fragments_received", }, 384 { 0x3068, T_REG_32, "oversize_received", }, 385 { 0x306c, T_REG_32, "jabber_received", }, 386 { 0x3070, T_REG_32, "mac_receive_error", }, 387 { 0x3074, T_REG_32, "bad_crc_event", }, 388 { 0x3078, T_REG_32, "collision", }, 389 { 0x307c, T_REG_32, "late_collision", }, 390 { 0x2484, T_REG_32, "rx_discard", }, 391 { 0x2488, T_REG_32, "rx_overrun", }, 392 { 0x3020, T_REG_32, "frames_64_octets", }, 393 { 0x3024, T_REG_32, "frames_65_to_127_octets", }, 394 { 0x3028, T_REG_32, "frames_128_to_255_octets", }, 395 { 0x302c, T_REG_32, "frames_256_to_511_octets", }, 396 { 0x3030, T_REG_32, "frames_512_to_1023_octets", }, 397 { 0x3034, T_REG_32, "frames_1024_to_max_octets", }, 398 { 0x3038, T_REG_64, "good_octets_sent", }, 399 { 0x3040, T_REG_32, "good_frames_sent", }, 400 { 0x3044, T_REG_32, "excessive_collision", }, 401 { 0x3048, T_REG_32, "multicast_frames_sent", }, 402 { 0x304c, T_REG_32, "broadcast_frames_sent", }, 403 { 0x3054, T_REG_32, "fc_sent", }, 404 { 0x300c, T_REG_32, "internal_mac_transmit_err", }, 405 { ETHTOOL_STAT_EEE_WAKEUP, T_SW, "eee_wakeup_errors", }, 406 { ETHTOOL_STAT_SKB_ALLOC_ERR, T_SW, "skb_alloc_errors", }, 407 { ETHTOOL_STAT_REFILL_ERR, T_SW, "refill_errors", }, 408 { ETHTOOL_XDP_REDIRECT, T_SW, "rx_xdp_redirect", }, 409 { ETHTOOL_XDP_PASS, T_SW, "rx_xdp_pass", }, 410 { ETHTOOL_XDP_DROP, T_SW, "rx_xdp_drop", }, 411 { ETHTOOL_XDP_TX, T_SW, "rx_xdp_tx", }, 412 { ETHTOOL_XDP_TX_ERR, T_SW, "rx_xdp_tx_errors", }, 413 { ETHTOOL_XDP_XMIT, T_SW, "tx_xdp_xmit", }, 414 { ETHTOOL_XDP_XMIT_ERR, T_SW, "tx_xdp_xmit_errors", }, 415 }; 416 417 struct mvneta_stats { 418 u64 rx_packets; 419 u64 rx_bytes; 420 u64 tx_packets; 421 u64 tx_bytes; 422 /* xdp */ 423 u64 xdp_redirect; 424 u64 xdp_pass; 425 u64 xdp_drop; 426 u64 xdp_xmit; 427 u64 xdp_xmit_err; 428 u64 xdp_tx; 429 u64 xdp_tx_err; 430 }; 431 432 struct mvneta_ethtool_stats { 433 struct mvneta_stats ps; 434 u64 skb_alloc_error; 435 u64 refill_error; 436 }; 437 438 struct mvneta_pcpu_stats { 439 struct u64_stats_sync syncp; 440 441 struct mvneta_ethtool_stats es; 442 u64 rx_dropped; 443 u64 rx_errors; 444 }; 445 446 struct mvneta_pcpu_port { 447 /* Pointer to the shared port */ 448 struct mvneta_port *pp; 449 450 /* Pointer to the CPU-local NAPI struct */ 451 struct napi_struct napi; 452 453 /* Cause of the previous interrupt */ 454 u32 cause_rx_tx; 455 }; 456 457 enum { 458 __MVNETA_DOWN, 459 }; 460 461 struct mvneta_port { 462 u8 id; 463 struct mvneta_pcpu_port __percpu *ports; 464 struct mvneta_pcpu_stats __percpu *stats; 465 466 unsigned long state; 467 468 int pkt_size; 469 void __iomem *base; 470 struct mvneta_rx_queue *rxqs; 471 struct mvneta_tx_queue *txqs; 472 struct net_device *dev; 473 struct hlist_node node_online; 474 struct hlist_node node_dead; 475 int rxq_def; 476 /* Protect the access to the percpu interrupt registers, 477 * ensuring that the configuration remains coherent. 478 */ 479 spinlock_t lock; 480 bool is_stopped; 481 482 u32 cause_rx_tx; 483 struct napi_struct napi; 484 485 struct bpf_prog *xdp_prog; 486 487 /* Core clock */ 488 struct clk *clk; 489 /* AXI clock */ 490 struct clk *clk_bus; 491 u8 mcast_count[256]; 492 u16 tx_ring_size; 493 u16 rx_ring_size; 494 495 phy_interface_t phy_interface; 496 struct device_node *dn; 497 unsigned int tx_csum_limit; 498 struct phylink *phylink; 499 struct phylink_config phylink_config; 500 struct phy *comphy; 501 502 struct mvneta_bm *bm_priv; 503 struct mvneta_bm_pool *pool_long; 504 struct mvneta_bm_pool *pool_short; 505 int bm_win_id; 506 507 bool eee_enabled; 508 bool eee_active; 509 bool tx_lpi_enabled; 510 511 u64 ethtool_stats[ARRAY_SIZE(mvneta_statistics)]; 512 513 u32 indir[MVNETA_RSS_LU_TABLE_SIZE]; 514 515 /* Flags for special SoC configurations */ 516 bool neta_armada3700; 517 u16 rx_offset_correction; 518 const struct mbus_dram_target_info *dram_target_info; 519 }; 520 521 /* The mvneta_tx_desc and mvneta_rx_desc structures describe the 522 * layout of the transmit and reception DMA descriptors, and their 523 * layout is therefore defined by the hardware design 524 */ 525 526 #define MVNETA_TX_L3_OFF_SHIFT 0 527 #define MVNETA_TX_IP_HLEN_SHIFT 8 528 #define MVNETA_TX_L4_UDP BIT(16) 529 #define MVNETA_TX_L3_IP6 BIT(17) 530 #define MVNETA_TXD_IP_CSUM BIT(18) 531 #define MVNETA_TXD_Z_PAD BIT(19) 532 #define MVNETA_TXD_L_DESC BIT(20) 533 #define MVNETA_TXD_F_DESC BIT(21) 534 #define MVNETA_TXD_FLZ_DESC (MVNETA_TXD_Z_PAD | \ 535 MVNETA_TXD_L_DESC | \ 536 MVNETA_TXD_F_DESC) 537 #define MVNETA_TX_L4_CSUM_FULL BIT(30) 538 #define MVNETA_TX_L4_CSUM_NOT BIT(31) 539 540 #define MVNETA_RXD_ERR_CRC 0x0 541 #define MVNETA_RXD_BM_POOL_SHIFT 13 542 #define MVNETA_RXD_BM_POOL_MASK (BIT(13) | BIT(14)) 543 #define MVNETA_RXD_ERR_SUMMARY BIT(16) 544 #define MVNETA_RXD_ERR_OVERRUN BIT(17) 545 #define MVNETA_RXD_ERR_LEN BIT(18) 546 #define MVNETA_RXD_ERR_RESOURCE (BIT(17) | BIT(18)) 547 #define MVNETA_RXD_ERR_CODE_MASK (BIT(17) | BIT(18)) 548 #define MVNETA_RXD_L3_IP4 BIT(25) 549 #define MVNETA_RXD_LAST_DESC BIT(26) 550 #define MVNETA_RXD_FIRST_DESC BIT(27) 551 #define MVNETA_RXD_FIRST_LAST_DESC (MVNETA_RXD_FIRST_DESC | \ 552 MVNETA_RXD_LAST_DESC) 553 #define MVNETA_RXD_L4_CSUM_OK BIT(30) 554 555 #if defined(__LITTLE_ENDIAN) 556 struct mvneta_tx_desc { 557 u32 command; /* Options used by HW for packet transmitting.*/ 558 u16 reserved1; /* csum_l4 (for future use) */ 559 u16 data_size; /* Data size of transmitted packet in bytes */ 560 u32 buf_phys_addr; /* Physical addr of transmitted buffer */ 561 u32 reserved2; /* hw_cmd - (for future use, PMT) */ 562 u32 reserved3[4]; /* Reserved - (for future use) */ 563 }; 564 565 struct mvneta_rx_desc { 566 u32 status; /* Info about received packet */ 567 u16 reserved1; /* pnc_info - (for future use, PnC) */ 568 u16 data_size; /* Size of received packet in bytes */ 569 570 u32 buf_phys_addr; /* Physical address of the buffer */ 571 u32 reserved2; /* pnc_flow_id (for future use, PnC) */ 572 573 u32 buf_cookie; /* cookie for access to RX buffer in rx path */ 574 u16 reserved3; /* prefetch_cmd, for future use */ 575 u16 reserved4; /* csum_l4 - (for future use, PnC) */ 576 577 u32 reserved5; /* pnc_extra PnC (for future use, PnC) */ 578 u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */ 579 }; 580 #else 581 struct mvneta_tx_desc { 582 u16 data_size; /* Data size of transmitted packet in bytes */ 583 u16 reserved1; /* csum_l4 (for future use) */ 584 u32 command; /* Options used by HW for packet transmitting.*/ 585 u32 reserved2; /* hw_cmd - (for future use, PMT) */ 586 u32 buf_phys_addr; /* Physical addr of transmitted buffer */ 587 u32 reserved3[4]; /* Reserved - (for future use) */ 588 }; 589 590 struct mvneta_rx_desc { 591 u16 data_size; /* Size of received packet in bytes */ 592 u16 reserved1; /* pnc_info - (for future use, PnC) */ 593 u32 status; /* Info about received packet */ 594 595 u32 reserved2; /* pnc_flow_id (for future use, PnC) */ 596 u32 buf_phys_addr; /* Physical address of the buffer */ 597 598 u16 reserved4; /* csum_l4 - (for future use, PnC) */ 599 u16 reserved3; /* prefetch_cmd, for future use */ 600 u32 buf_cookie; /* cookie for access to RX buffer in rx path */ 601 602 u32 reserved5; /* pnc_extra PnC (for future use, PnC) */ 603 u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */ 604 }; 605 #endif 606 607 enum mvneta_tx_buf_type { 608 MVNETA_TYPE_SKB, 609 MVNETA_TYPE_XDP_TX, 610 MVNETA_TYPE_XDP_NDO, 611 }; 612 613 struct mvneta_tx_buf { 614 enum mvneta_tx_buf_type type; 615 union { 616 struct xdp_frame *xdpf; 617 struct sk_buff *skb; 618 }; 619 }; 620 621 struct mvneta_tx_queue { 622 /* Number of this TX queue, in the range 0-7 */ 623 u8 id; 624 625 /* Number of TX DMA descriptors in the descriptor ring */ 626 int size; 627 628 /* Number of currently used TX DMA descriptor in the 629 * descriptor ring 630 */ 631 int count; 632 int pending; 633 int tx_stop_threshold; 634 int tx_wake_threshold; 635 636 /* Array of transmitted buffers */ 637 struct mvneta_tx_buf *buf; 638 639 /* Index of last TX DMA descriptor that was inserted */ 640 int txq_put_index; 641 642 /* Index of the TX DMA descriptor to be cleaned up */ 643 int txq_get_index; 644 645 u32 done_pkts_coal; 646 647 /* Virtual address of the TX DMA descriptors array */ 648 struct mvneta_tx_desc *descs; 649 650 /* DMA address of the TX DMA descriptors array */ 651 dma_addr_t descs_phys; 652 653 /* Index of the last TX DMA descriptor */ 654 int last_desc; 655 656 /* Index of the next TX DMA descriptor to process */ 657 int next_desc_to_proc; 658 659 /* DMA buffers for TSO headers */ 660 char *tso_hdrs; 661 662 /* DMA address of TSO headers */ 663 dma_addr_t tso_hdrs_phys; 664 665 /* Affinity mask for CPUs*/ 666 cpumask_t affinity_mask; 667 }; 668 669 struct mvneta_rx_queue { 670 /* rx queue number, in the range 0-7 */ 671 u8 id; 672 673 /* num of rx descriptors in the rx descriptor ring */ 674 int size; 675 676 u32 pkts_coal; 677 u32 time_coal; 678 679 /* page_pool */ 680 struct page_pool *page_pool; 681 struct xdp_rxq_info xdp_rxq; 682 683 /* Virtual address of the RX buffer */ 684 void **buf_virt_addr; 685 686 /* Virtual address of the RX DMA descriptors array */ 687 struct mvneta_rx_desc *descs; 688 689 /* DMA address of the RX DMA descriptors array */ 690 dma_addr_t descs_phys; 691 692 /* Index of the last RX DMA descriptor */ 693 int last_desc; 694 695 /* Index of the next RX DMA descriptor to process */ 696 int next_desc_to_proc; 697 698 /* Index of first RX DMA descriptor to refill */ 699 int first_to_refill; 700 u32 refill_num; 701 702 /* pointer to uncomplete skb buffer */ 703 struct sk_buff *skb; 704 int left_size; 705 }; 706 707 static enum cpuhp_state online_hpstate; 708 /* The hardware supports eight (8) rx queues, but we are only allowing 709 * the first one to be used. Therefore, let's just allocate one queue. 710 */ 711 static int rxq_number = 8; 712 static int txq_number = 8; 713 714 static int rxq_def; 715 716 static int rx_copybreak __read_mostly = 256; 717 718 /* HW BM need that each port be identify by a unique ID */ 719 static int global_port_id; 720 721 #define MVNETA_DRIVER_NAME "mvneta" 722 #define MVNETA_DRIVER_VERSION "1.0" 723 724 /* Utility/helper methods */ 725 726 /* Write helper method */ 727 static void mvreg_write(struct mvneta_port *pp, u32 offset, u32 data) 728 { 729 writel(data, pp->base + offset); 730 } 731 732 /* Read helper method */ 733 static u32 mvreg_read(struct mvneta_port *pp, u32 offset) 734 { 735 return readl(pp->base + offset); 736 } 737 738 /* Increment txq get counter */ 739 static void mvneta_txq_inc_get(struct mvneta_tx_queue *txq) 740 { 741 txq->txq_get_index++; 742 if (txq->txq_get_index == txq->size) 743 txq->txq_get_index = 0; 744 } 745 746 /* Increment txq put counter */ 747 static void mvneta_txq_inc_put(struct mvneta_tx_queue *txq) 748 { 749 txq->txq_put_index++; 750 if (txq->txq_put_index == txq->size) 751 txq->txq_put_index = 0; 752 } 753 754 755 /* Clear all MIB counters */ 756 static void mvneta_mib_counters_clear(struct mvneta_port *pp) 757 { 758 int i; 759 u32 dummy; 760 761 /* Perform dummy reads from MIB counters */ 762 for (i = 0; i < MVNETA_MIB_LATE_COLLISION; i += 4) 763 dummy = mvreg_read(pp, (MVNETA_MIB_COUNTERS_BASE + i)); 764 dummy = mvreg_read(pp, MVNETA_RX_DISCARD_FRAME_COUNT); 765 dummy = mvreg_read(pp, MVNETA_OVERRUN_FRAME_COUNT); 766 } 767 768 /* Get System Network Statistics */ 769 static void 770 mvneta_get_stats64(struct net_device *dev, 771 struct rtnl_link_stats64 *stats) 772 { 773 struct mvneta_port *pp = netdev_priv(dev); 774 unsigned int start; 775 int cpu; 776 777 for_each_possible_cpu(cpu) { 778 struct mvneta_pcpu_stats *cpu_stats; 779 u64 rx_packets; 780 u64 rx_bytes; 781 u64 rx_dropped; 782 u64 rx_errors; 783 u64 tx_packets; 784 u64 tx_bytes; 785 786 cpu_stats = per_cpu_ptr(pp->stats, cpu); 787 do { 788 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp); 789 rx_packets = cpu_stats->es.ps.rx_packets; 790 rx_bytes = cpu_stats->es.ps.rx_bytes; 791 rx_dropped = cpu_stats->rx_dropped; 792 rx_errors = cpu_stats->rx_errors; 793 tx_packets = cpu_stats->es.ps.tx_packets; 794 tx_bytes = cpu_stats->es.ps.tx_bytes; 795 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start)); 796 797 stats->rx_packets += rx_packets; 798 stats->rx_bytes += rx_bytes; 799 stats->rx_dropped += rx_dropped; 800 stats->rx_errors += rx_errors; 801 stats->tx_packets += tx_packets; 802 stats->tx_bytes += tx_bytes; 803 } 804 805 stats->tx_dropped = dev->stats.tx_dropped; 806 } 807 808 /* Rx descriptors helper methods */ 809 810 /* Checks whether the RX descriptor having this status is both the first 811 * and the last descriptor for the RX packet. Each RX packet is currently 812 * received through a single RX descriptor, so not having each RX 813 * descriptor with its first and last bits set is an error 814 */ 815 static int mvneta_rxq_desc_is_first_last(u32 status) 816 { 817 return (status & MVNETA_RXD_FIRST_LAST_DESC) == 818 MVNETA_RXD_FIRST_LAST_DESC; 819 } 820 821 /* Add number of descriptors ready to receive new packets */ 822 static void mvneta_rxq_non_occup_desc_add(struct mvneta_port *pp, 823 struct mvneta_rx_queue *rxq, 824 int ndescs) 825 { 826 /* Only MVNETA_RXQ_ADD_NON_OCCUPIED_MAX (255) descriptors can 827 * be added at once 828 */ 829 while (ndescs > MVNETA_RXQ_ADD_NON_OCCUPIED_MAX) { 830 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), 831 (MVNETA_RXQ_ADD_NON_OCCUPIED_MAX << 832 MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT)); 833 ndescs -= MVNETA_RXQ_ADD_NON_OCCUPIED_MAX; 834 } 835 836 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), 837 (ndescs << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT)); 838 } 839 840 /* Get number of RX descriptors occupied by received packets */ 841 static int mvneta_rxq_busy_desc_num_get(struct mvneta_port *pp, 842 struct mvneta_rx_queue *rxq) 843 { 844 u32 val; 845 846 val = mvreg_read(pp, MVNETA_RXQ_STATUS_REG(rxq->id)); 847 return val & MVNETA_RXQ_OCCUPIED_ALL_MASK; 848 } 849 850 /* Update num of rx desc called upon return from rx path or 851 * from mvneta_rxq_drop_pkts(). 852 */ 853 static void mvneta_rxq_desc_num_update(struct mvneta_port *pp, 854 struct mvneta_rx_queue *rxq, 855 int rx_done, int rx_filled) 856 { 857 u32 val; 858 859 if ((rx_done <= 0xff) && (rx_filled <= 0xff)) { 860 val = rx_done | 861 (rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT); 862 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val); 863 return; 864 } 865 866 /* Only 255 descriptors can be added at once */ 867 while ((rx_done > 0) || (rx_filled > 0)) { 868 if (rx_done <= 0xff) { 869 val = rx_done; 870 rx_done = 0; 871 } else { 872 val = 0xff; 873 rx_done -= 0xff; 874 } 875 if (rx_filled <= 0xff) { 876 val |= rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT; 877 rx_filled = 0; 878 } else { 879 val |= 0xff << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT; 880 rx_filled -= 0xff; 881 } 882 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val); 883 } 884 } 885 886 /* Get pointer to next RX descriptor to be processed by SW */ 887 static struct mvneta_rx_desc * 888 mvneta_rxq_next_desc_get(struct mvneta_rx_queue *rxq) 889 { 890 int rx_desc = rxq->next_desc_to_proc; 891 892 rxq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(rxq, rx_desc); 893 prefetch(rxq->descs + rxq->next_desc_to_proc); 894 return rxq->descs + rx_desc; 895 } 896 897 /* Change maximum receive size of the port. */ 898 static void mvneta_max_rx_size_set(struct mvneta_port *pp, int max_rx_size) 899 { 900 u32 val; 901 902 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0); 903 val &= ~MVNETA_GMAC_MAX_RX_SIZE_MASK; 904 val |= ((max_rx_size - MVNETA_MH_SIZE) / 2) << 905 MVNETA_GMAC_MAX_RX_SIZE_SHIFT; 906 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val); 907 } 908 909 910 /* Set rx queue offset */ 911 static void mvneta_rxq_offset_set(struct mvneta_port *pp, 912 struct mvneta_rx_queue *rxq, 913 int offset) 914 { 915 u32 val; 916 917 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id)); 918 val &= ~MVNETA_RXQ_PKT_OFFSET_ALL_MASK; 919 920 /* Offset is in */ 921 val |= MVNETA_RXQ_PKT_OFFSET_MASK(offset >> 3); 922 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val); 923 } 924 925 926 /* Tx descriptors helper methods */ 927 928 /* Update HW with number of TX descriptors to be sent */ 929 static void mvneta_txq_pend_desc_add(struct mvneta_port *pp, 930 struct mvneta_tx_queue *txq, 931 int pend_desc) 932 { 933 u32 val; 934 935 pend_desc += txq->pending; 936 937 /* Only 255 Tx descriptors can be added at once */ 938 do { 939 val = min(pend_desc, 255); 940 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val); 941 pend_desc -= val; 942 } while (pend_desc > 0); 943 txq->pending = 0; 944 } 945 946 /* Get pointer to next TX descriptor to be processed (send) by HW */ 947 static struct mvneta_tx_desc * 948 mvneta_txq_next_desc_get(struct mvneta_tx_queue *txq) 949 { 950 int tx_desc = txq->next_desc_to_proc; 951 952 txq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(txq, tx_desc); 953 return txq->descs + tx_desc; 954 } 955 956 /* Release the last allocated TX descriptor. Useful to handle DMA 957 * mapping failures in the TX path. 958 */ 959 static void mvneta_txq_desc_put(struct mvneta_tx_queue *txq) 960 { 961 if (txq->next_desc_to_proc == 0) 962 txq->next_desc_to_proc = txq->last_desc - 1; 963 else 964 txq->next_desc_to_proc--; 965 } 966 967 /* Set rxq buf size */ 968 static void mvneta_rxq_buf_size_set(struct mvneta_port *pp, 969 struct mvneta_rx_queue *rxq, 970 int buf_size) 971 { 972 u32 val; 973 974 val = mvreg_read(pp, MVNETA_RXQ_SIZE_REG(rxq->id)); 975 976 val &= ~MVNETA_RXQ_BUF_SIZE_MASK; 977 val |= ((buf_size >> 3) << MVNETA_RXQ_BUF_SIZE_SHIFT); 978 979 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), val); 980 } 981 982 /* Disable buffer management (BM) */ 983 static void mvneta_rxq_bm_disable(struct mvneta_port *pp, 984 struct mvneta_rx_queue *rxq) 985 { 986 u32 val; 987 988 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id)); 989 val &= ~MVNETA_RXQ_HW_BUF_ALLOC; 990 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val); 991 } 992 993 /* Enable buffer management (BM) */ 994 static void mvneta_rxq_bm_enable(struct mvneta_port *pp, 995 struct mvneta_rx_queue *rxq) 996 { 997 u32 val; 998 999 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id)); 1000 val |= MVNETA_RXQ_HW_BUF_ALLOC; 1001 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val); 1002 } 1003 1004 /* Notify HW about port's assignment of pool for bigger packets */ 1005 static void mvneta_rxq_long_pool_set(struct mvneta_port *pp, 1006 struct mvneta_rx_queue *rxq) 1007 { 1008 u32 val; 1009 1010 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id)); 1011 val &= ~MVNETA_RXQ_LONG_POOL_ID_MASK; 1012 val |= (pp->pool_long->id << MVNETA_RXQ_LONG_POOL_ID_SHIFT); 1013 1014 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val); 1015 } 1016 1017 /* Notify HW about port's assignment of pool for smaller packets */ 1018 static void mvneta_rxq_short_pool_set(struct mvneta_port *pp, 1019 struct mvneta_rx_queue *rxq) 1020 { 1021 u32 val; 1022 1023 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id)); 1024 val &= ~MVNETA_RXQ_SHORT_POOL_ID_MASK; 1025 val |= (pp->pool_short->id << MVNETA_RXQ_SHORT_POOL_ID_SHIFT); 1026 1027 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val); 1028 } 1029 1030 /* Set port's receive buffer size for assigned BM pool */ 1031 static inline void mvneta_bm_pool_bufsize_set(struct mvneta_port *pp, 1032 int buf_size, 1033 u8 pool_id) 1034 { 1035 u32 val; 1036 1037 if (!IS_ALIGNED(buf_size, 8)) { 1038 dev_warn(pp->dev->dev.parent, 1039 "illegal buf_size value %d, round to %d\n", 1040 buf_size, ALIGN(buf_size, 8)); 1041 buf_size = ALIGN(buf_size, 8); 1042 } 1043 1044 val = mvreg_read(pp, MVNETA_PORT_POOL_BUFFER_SZ_REG(pool_id)); 1045 val |= buf_size & MVNETA_PORT_POOL_BUFFER_SZ_MASK; 1046 mvreg_write(pp, MVNETA_PORT_POOL_BUFFER_SZ_REG(pool_id), val); 1047 } 1048 1049 /* Configure MBUS window in order to enable access BM internal SRAM */ 1050 static int mvneta_mbus_io_win_set(struct mvneta_port *pp, u32 base, u32 wsize, 1051 u8 target, u8 attr) 1052 { 1053 u32 win_enable, win_protect; 1054 int i; 1055 1056 win_enable = mvreg_read(pp, MVNETA_BASE_ADDR_ENABLE); 1057 1058 if (pp->bm_win_id < 0) { 1059 /* Find first not occupied window */ 1060 for (i = 0; i < MVNETA_MAX_DECODE_WIN; i++) { 1061 if (win_enable & (1 << i)) { 1062 pp->bm_win_id = i; 1063 break; 1064 } 1065 } 1066 if (i == MVNETA_MAX_DECODE_WIN) 1067 return -ENOMEM; 1068 } else { 1069 i = pp->bm_win_id; 1070 } 1071 1072 mvreg_write(pp, MVNETA_WIN_BASE(i), 0); 1073 mvreg_write(pp, MVNETA_WIN_SIZE(i), 0); 1074 1075 if (i < 4) 1076 mvreg_write(pp, MVNETA_WIN_REMAP(i), 0); 1077 1078 mvreg_write(pp, MVNETA_WIN_BASE(i), (base & 0xffff0000) | 1079 (attr << 8) | target); 1080 1081 mvreg_write(pp, MVNETA_WIN_SIZE(i), (wsize - 1) & 0xffff0000); 1082 1083 win_protect = mvreg_read(pp, MVNETA_ACCESS_PROTECT_ENABLE); 1084 win_protect |= 3 << (2 * i); 1085 mvreg_write(pp, MVNETA_ACCESS_PROTECT_ENABLE, win_protect); 1086 1087 win_enable &= ~(1 << i); 1088 mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable); 1089 1090 return 0; 1091 } 1092 1093 static int mvneta_bm_port_mbus_init(struct mvneta_port *pp) 1094 { 1095 u32 wsize; 1096 u8 target, attr; 1097 int err; 1098 1099 /* Get BM window information */ 1100 err = mvebu_mbus_get_io_win_info(pp->bm_priv->bppi_phys_addr, &wsize, 1101 &target, &attr); 1102 if (err < 0) 1103 return err; 1104 1105 pp->bm_win_id = -1; 1106 1107 /* Open NETA -> BM window */ 1108 err = mvneta_mbus_io_win_set(pp, pp->bm_priv->bppi_phys_addr, wsize, 1109 target, attr); 1110 if (err < 0) { 1111 netdev_info(pp->dev, "fail to configure mbus window to BM\n"); 1112 return err; 1113 } 1114 return 0; 1115 } 1116 1117 /* Assign and initialize pools for port. In case of fail 1118 * buffer manager will remain disabled for current port. 1119 */ 1120 static int mvneta_bm_port_init(struct platform_device *pdev, 1121 struct mvneta_port *pp) 1122 { 1123 struct device_node *dn = pdev->dev.of_node; 1124 u32 long_pool_id, short_pool_id; 1125 1126 if (!pp->neta_armada3700) { 1127 int ret; 1128 1129 ret = mvneta_bm_port_mbus_init(pp); 1130 if (ret) 1131 return ret; 1132 } 1133 1134 if (of_property_read_u32(dn, "bm,pool-long", &long_pool_id)) { 1135 netdev_info(pp->dev, "missing long pool id\n"); 1136 return -EINVAL; 1137 } 1138 1139 /* Create port's long pool depending on mtu */ 1140 pp->pool_long = mvneta_bm_pool_use(pp->bm_priv, long_pool_id, 1141 MVNETA_BM_LONG, pp->id, 1142 MVNETA_RX_PKT_SIZE(pp->dev->mtu)); 1143 if (!pp->pool_long) { 1144 netdev_info(pp->dev, "fail to obtain long pool for port\n"); 1145 return -ENOMEM; 1146 } 1147 1148 pp->pool_long->port_map |= 1 << pp->id; 1149 1150 mvneta_bm_pool_bufsize_set(pp, pp->pool_long->buf_size, 1151 pp->pool_long->id); 1152 1153 /* If short pool id is not defined, assume using single pool */ 1154 if (of_property_read_u32(dn, "bm,pool-short", &short_pool_id)) 1155 short_pool_id = long_pool_id; 1156 1157 /* Create port's short pool */ 1158 pp->pool_short = mvneta_bm_pool_use(pp->bm_priv, short_pool_id, 1159 MVNETA_BM_SHORT, pp->id, 1160 MVNETA_BM_SHORT_PKT_SIZE); 1161 if (!pp->pool_short) { 1162 netdev_info(pp->dev, "fail to obtain short pool for port\n"); 1163 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id); 1164 return -ENOMEM; 1165 } 1166 1167 if (short_pool_id != long_pool_id) { 1168 pp->pool_short->port_map |= 1 << pp->id; 1169 mvneta_bm_pool_bufsize_set(pp, pp->pool_short->buf_size, 1170 pp->pool_short->id); 1171 } 1172 1173 return 0; 1174 } 1175 1176 /* Update settings of a pool for bigger packets */ 1177 static void mvneta_bm_update_mtu(struct mvneta_port *pp, int mtu) 1178 { 1179 struct mvneta_bm_pool *bm_pool = pp->pool_long; 1180 struct hwbm_pool *hwbm_pool = &bm_pool->hwbm_pool; 1181 int num; 1182 1183 /* Release all buffers from long pool */ 1184 mvneta_bm_bufs_free(pp->bm_priv, bm_pool, 1 << pp->id); 1185 if (hwbm_pool->buf_num) { 1186 WARN(1, "cannot free all buffers in pool %d\n", 1187 bm_pool->id); 1188 goto bm_mtu_err; 1189 } 1190 1191 bm_pool->pkt_size = MVNETA_RX_PKT_SIZE(mtu); 1192 bm_pool->buf_size = MVNETA_RX_BUF_SIZE(bm_pool->pkt_size); 1193 hwbm_pool->frag_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) + 1194 SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(bm_pool->pkt_size)); 1195 1196 /* Fill entire long pool */ 1197 num = hwbm_pool_add(hwbm_pool, hwbm_pool->size); 1198 if (num != hwbm_pool->size) { 1199 WARN(1, "pool %d: %d of %d allocated\n", 1200 bm_pool->id, num, hwbm_pool->size); 1201 goto bm_mtu_err; 1202 } 1203 mvneta_bm_pool_bufsize_set(pp, bm_pool->buf_size, bm_pool->id); 1204 1205 return; 1206 1207 bm_mtu_err: 1208 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id); 1209 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short, 1 << pp->id); 1210 1211 pp->bm_priv = NULL; 1212 pp->rx_offset_correction = MVNETA_SKB_HEADROOM; 1213 mvreg_write(pp, MVNETA_ACC_MODE, MVNETA_ACC_MODE_EXT1); 1214 netdev_info(pp->dev, "fail to update MTU, fall back to software BM\n"); 1215 } 1216 1217 /* Start the Ethernet port RX and TX activity */ 1218 static void mvneta_port_up(struct mvneta_port *pp) 1219 { 1220 int queue; 1221 u32 q_map; 1222 1223 /* Enable all initialized TXs. */ 1224 q_map = 0; 1225 for (queue = 0; queue < txq_number; queue++) { 1226 struct mvneta_tx_queue *txq = &pp->txqs[queue]; 1227 if (txq->descs) 1228 q_map |= (1 << queue); 1229 } 1230 mvreg_write(pp, MVNETA_TXQ_CMD, q_map); 1231 1232 q_map = 0; 1233 /* Enable all initialized RXQs. */ 1234 for (queue = 0; queue < rxq_number; queue++) { 1235 struct mvneta_rx_queue *rxq = &pp->rxqs[queue]; 1236 1237 if (rxq->descs) 1238 q_map |= (1 << queue); 1239 } 1240 mvreg_write(pp, MVNETA_RXQ_CMD, q_map); 1241 } 1242 1243 /* Stop the Ethernet port activity */ 1244 static void mvneta_port_down(struct mvneta_port *pp) 1245 { 1246 u32 val; 1247 int count; 1248 1249 /* Stop Rx port activity. Check port Rx activity. */ 1250 val = mvreg_read(pp, MVNETA_RXQ_CMD) & MVNETA_RXQ_ENABLE_MASK; 1251 1252 /* Issue stop command for active channels only */ 1253 if (val != 0) 1254 mvreg_write(pp, MVNETA_RXQ_CMD, 1255 val << MVNETA_RXQ_DISABLE_SHIFT); 1256 1257 /* Wait for all Rx activity to terminate. */ 1258 count = 0; 1259 do { 1260 if (count++ >= MVNETA_RX_DISABLE_TIMEOUT_MSEC) { 1261 netdev_warn(pp->dev, 1262 "TIMEOUT for RX stopped ! rx_queue_cmd: 0x%08x\n", 1263 val); 1264 break; 1265 } 1266 mdelay(1); 1267 1268 val = mvreg_read(pp, MVNETA_RXQ_CMD); 1269 } while (val & MVNETA_RXQ_ENABLE_MASK); 1270 1271 /* Stop Tx port activity. Check port Tx activity. Issue stop 1272 * command for active channels only 1273 */ 1274 val = (mvreg_read(pp, MVNETA_TXQ_CMD)) & MVNETA_TXQ_ENABLE_MASK; 1275 1276 if (val != 0) 1277 mvreg_write(pp, MVNETA_TXQ_CMD, 1278 (val << MVNETA_TXQ_DISABLE_SHIFT)); 1279 1280 /* Wait for all Tx activity to terminate. */ 1281 count = 0; 1282 do { 1283 if (count++ >= MVNETA_TX_DISABLE_TIMEOUT_MSEC) { 1284 netdev_warn(pp->dev, 1285 "TIMEOUT for TX stopped status=0x%08x\n", 1286 val); 1287 break; 1288 } 1289 mdelay(1); 1290 1291 /* Check TX Command reg that all Txqs are stopped */ 1292 val = mvreg_read(pp, MVNETA_TXQ_CMD); 1293 1294 } while (val & MVNETA_TXQ_ENABLE_MASK); 1295 1296 /* Double check to verify that TX FIFO is empty */ 1297 count = 0; 1298 do { 1299 if (count++ >= MVNETA_TX_FIFO_EMPTY_TIMEOUT) { 1300 netdev_warn(pp->dev, 1301 "TX FIFO empty timeout status=0x%08x\n", 1302 val); 1303 break; 1304 } 1305 mdelay(1); 1306 1307 val = mvreg_read(pp, MVNETA_PORT_STATUS); 1308 } while (!(val & MVNETA_TX_FIFO_EMPTY) && 1309 (val & MVNETA_TX_IN_PRGRS)); 1310 1311 udelay(200); 1312 } 1313 1314 /* Enable the port by setting the port enable bit of the MAC control register */ 1315 static void mvneta_port_enable(struct mvneta_port *pp) 1316 { 1317 u32 val; 1318 1319 /* Enable port */ 1320 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0); 1321 val |= MVNETA_GMAC0_PORT_ENABLE; 1322 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val); 1323 } 1324 1325 /* Disable the port and wait for about 200 usec before retuning */ 1326 static void mvneta_port_disable(struct mvneta_port *pp) 1327 { 1328 u32 val; 1329 1330 /* Reset the Enable bit in the Serial Control Register */ 1331 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0); 1332 val &= ~MVNETA_GMAC0_PORT_ENABLE; 1333 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val); 1334 1335 udelay(200); 1336 } 1337 1338 /* Multicast tables methods */ 1339 1340 /* Set all entries in Unicast MAC Table; queue==-1 means reject all */ 1341 static void mvneta_set_ucast_table(struct mvneta_port *pp, int queue) 1342 { 1343 int offset; 1344 u32 val; 1345 1346 if (queue == -1) { 1347 val = 0; 1348 } else { 1349 val = 0x1 | (queue << 1); 1350 val |= (val << 24) | (val << 16) | (val << 8); 1351 } 1352 1353 for (offset = 0; offset <= 0xc; offset += 4) 1354 mvreg_write(pp, MVNETA_DA_FILT_UCAST_BASE + offset, val); 1355 } 1356 1357 /* Set all entries in Special Multicast MAC Table; queue==-1 means reject all */ 1358 static void mvneta_set_special_mcast_table(struct mvneta_port *pp, int queue) 1359 { 1360 int offset; 1361 u32 val; 1362 1363 if (queue == -1) { 1364 val = 0; 1365 } else { 1366 val = 0x1 | (queue << 1); 1367 val |= (val << 24) | (val << 16) | (val << 8); 1368 } 1369 1370 for (offset = 0; offset <= 0xfc; offset += 4) 1371 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + offset, val); 1372 1373 } 1374 1375 /* Set all entries in Other Multicast MAC Table. queue==-1 means reject all */ 1376 static void mvneta_set_other_mcast_table(struct mvneta_port *pp, int queue) 1377 { 1378 int offset; 1379 u32 val; 1380 1381 if (queue == -1) { 1382 memset(pp->mcast_count, 0, sizeof(pp->mcast_count)); 1383 val = 0; 1384 } else { 1385 memset(pp->mcast_count, 1, sizeof(pp->mcast_count)); 1386 val = 0x1 | (queue << 1); 1387 val |= (val << 24) | (val << 16) | (val << 8); 1388 } 1389 1390 for (offset = 0; offset <= 0xfc; offset += 4) 1391 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + offset, val); 1392 } 1393 1394 static void mvneta_percpu_unmask_interrupt(void *arg) 1395 { 1396 struct mvneta_port *pp = arg; 1397 1398 /* All the queue are unmasked, but actually only the ones 1399 * mapped to this CPU will be unmasked 1400 */ 1401 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 1402 MVNETA_RX_INTR_MASK_ALL | 1403 MVNETA_TX_INTR_MASK_ALL | 1404 MVNETA_MISCINTR_INTR_MASK); 1405 } 1406 1407 static void mvneta_percpu_mask_interrupt(void *arg) 1408 { 1409 struct mvneta_port *pp = arg; 1410 1411 /* All the queue are masked, but actually only the ones 1412 * mapped to this CPU will be masked 1413 */ 1414 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0); 1415 mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0); 1416 mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0); 1417 } 1418 1419 static void mvneta_percpu_clear_intr_cause(void *arg) 1420 { 1421 struct mvneta_port *pp = arg; 1422 1423 /* All the queue are cleared, but actually only the ones 1424 * mapped to this CPU will be cleared 1425 */ 1426 mvreg_write(pp, MVNETA_INTR_NEW_CAUSE, 0); 1427 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0); 1428 mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0); 1429 } 1430 1431 /* This method sets defaults to the NETA port: 1432 * Clears interrupt Cause and Mask registers. 1433 * Clears all MAC tables. 1434 * Sets defaults to all registers. 1435 * Resets RX and TX descriptor rings. 1436 * Resets PHY. 1437 * This method can be called after mvneta_port_down() to return the port 1438 * settings to defaults. 1439 */ 1440 static void mvneta_defaults_set(struct mvneta_port *pp) 1441 { 1442 int cpu; 1443 int queue; 1444 u32 val; 1445 int max_cpu = num_present_cpus(); 1446 1447 /* Clear all Cause registers */ 1448 on_each_cpu(mvneta_percpu_clear_intr_cause, pp, true); 1449 1450 /* Mask all interrupts */ 1451 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true); 1452 mvreg_write(pp, MVNETA_INTR_ENABLE, 0); 1453 1454 /* Enable MBUS Retry bit16 */ 1455 mvreg_write(pp, MVNETA_MBUS_RETRY, 0x20); 1456 1457 /* Set CPU queue access map. CPUs are assigned to the RX and 1458 * TX queues modulo their number. If there is only one TX 1459 * queue then it is assigned to the CPU associated to the 1460 * default RX queue. 1461 */ 1462 for_each_present_cpu(cpu) { 1463 int rxq_map = 0, txq_map = 0; 1464 int rxq, txq; 1465 if (!pp->neta_armada3700) { 1466 for (rxq = 0; rxq < rxq_number; rxq++) 1467 if ((rxq % max_cpu) == cpu) 1468 rxq_map |= MVNETA_CPU_RXQ_ACCESS(rxq); 1469 1470 for (txq = 0; txq < txq_number; txq++) 1471 if ((txq % max_cpu) == cpu) 1472 txq_map |= MVNETA_CPU_TXQ_ACCESS(txq); 1473 1474 /* With only one TX queue we configure a special case 1475 * which will allow to get all the irq on a single 1476 * CPU 1477 */ 1478 if (txq_number == 1) 1479 txq_map = (cpu == pp->rxq_def) ? 1480 MVNETA_CPU_TXQ_ACCESS(1) : 0; 1481 1482 } else { 1483 txq_map = MVNETA_CPU_TXQ_ACCESS_ALL_MASK; 1484 rxq_map = MVNETA_CPU_RXQ_ACCESS_ALL_MASK; 1485 } 1486 1487 mvreg_write(pp, MVNETA_CPU_MAP(cpu), rxq_map | txq_map); 1488 } 1489 1490 /* Reset RX and TX DMAs */ 1491 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET); 1492 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET); 1493 1494 /* Disable Legacy WRR, Disable EJP, Release from reset */ 1495 mvreg_write(pp, MVNETA_TXQ_CMD_1, 0); 1496 for (queue = 0; queue < txq_number; queue++) { 1497 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(queue), 0); 1498 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(queue), 0); 1499 } 1500 1501 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0); 1502 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0); 1503 1504 /* Set Port Acceleration Mode */ 1505 if (pp->bm_priv) 1506 /* HW buffer management + legacy parser */ 1507 val = MVNETA_ACC_MODE_EXT2; 1508 else 1509 /* SW buffer management + legacy parser */ 1510 val = MVNETA_ACC_MODE_EXT1; 1511 mvreg_write(pp, MVNETA_ACC_MODE, val); 1512 1513 if (pp->bm_priv) 1514 mvreg_write(pp, MVNETA_BM_ADDRESS, pp->bm_priv->bppi_phys_addr); 1515 1516 /* Update val of portCfg register accordingly with all RxQueue types */ 1517 val = MVNETA_PORT_CONFIG_DEFL_VALUE(pp->rxq_def); 1518 mvreg_write(pp, MVNETA_PORT_CONFIG, val); 1519 1520 val = 0; 1521 mvreg_write(pp, MVNETA_PORT_CONFIG_EXTEND, val); 1522 mvreg_write(pp, MVNETA_RX_MIN_FRAME_SIZE, 64); 1523 1524 /* Build PORT_SDMA_CONFIG_REG */ 1525 val = 0; 1526 1527 /* Default burst size */ 1528 val |= MVNETA_TX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16); 1529 val |= MVNETA_RX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16); 1530 val |= MVNETA_RX_NO_DATA_SWAP | MVNETA_TX_NO_DATA_SWAP; 1531 1532 #if defined(__BIG_ENDIAN) 1533 val |= MVNETA_DESC_SWAP; 1534 #endif 1535 1536 /* Assign port SDMA configuration */ 1537 mvreg_write(pp, MVNETA_SDMA_CONFIG, val); 1538 1539 /* Disable PHY polling in hardware, since we're using the 1540 * kernel phylib to do this. 1541 */ 1542 val = mvreg_read(pp, MVNETA_UNIT_CONTROL); 1543 val &= ~MVNETA_PHY_POLLING_ENABLE; 1544 mvreg_write(pp, MVNETA_UNIT_CONTROL, val); 1545 1546 mvneta_set_ucast_table(pp, -1); 1547 mvneta_set_special_mcast_table(pp, -1); 1548 mvneta_set_other_mcast_table(pp, -1); 1549 1550 /* Set port interrupt enable register - default enable all */ 1551 mvreg_write(pp, MVNETA_INTR_ENABLE, 1552 (MVNETA_RXQ_INTR_ENABLE_ALL_MASK 1553 | MVNETA_TXQ_INTR_ENABLE_ALL_MASK)); 1554 1555 mvneta_mib_counters_clear(pp); 1556 } 1557 1558 /* Set max sizes for tx queues */ 1559 static void mvneta_txq_max_tx_size_set(struct mvneta_port *pp, int max_tx_size) 1560 1561 { 1562 u32 val, size, mtu; 1563 int queue; 1564 1565 mtu = max_tx_size * 8; 1566 if (mtu > MVNETA_TX_MTU_MAX) 1567 mtu = MVNETA_TX_MTU_MAX; 1568 1569 /* Set MTU */ 1570 val = mvreg_read(pp, MVNETA_TX_MTU); 1571 val &= ~MVNETA_TX_MTU_MAX; 1572 val |= mtu; 1573 mvreg_write(pp, MVNETA_TX_MTU, val); 1574 1575 /* TX token size and all TXQs token size must be larger that MTU */ 1576 val = mvreg_read(pp, MVNETA_TX_TOKEN_SIZE); 1577 1578 size = val & MVNETA_TX_TOKEN_SIZE_MAX; 1579 if (size < mtu) { 1580 size = mtu; 1581 val &= ~MVNETA_TX_TOKEN_SIZE_MAX; 1582 val |= size; 1583 mvreg_write(pp, MVNETA_TX_TOKEN_SIZE, val); 1584 } 1585 for (queue = 0; queue < txq_number; queue++) { 1586 val = mvreg_read(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue)); 1587 1588 size = val & MVNETA_TXQ_TOKEN_SIZE_MAX; 1589 if (size < mtu) { 1590 size = mtu; 1591 val &= ~MVNETA_TXQ_TOKEN_SIZE_MAX; 1592 val |= size; 1593 mvreg_write(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue), val); 1594 } 1595 } 1596 } 1597 1598 /* Set unicast address */ 1599 static void mvneta_set_ucast_addr(struct mvneta_port *pp, u8 last_nibble, 1600 int queue) 1601 { 1602 unsigned int unicast_reg; 1603 unsigned int tbl_offset; 1604 unsigned int reg_offset; 1605 1606 /* Locate the Unicast table entry */ 1607 last_nibble = (0xf & last_nibble); 1608 1609 /* offset from unicast tbl base */ 1610 tbl_offset = (last_nibble / 4) * 4; 1611 1612 /* offset within the above reg */ 1613 reg_offset = last_nibble % 4; 1614 1615 unicast_reg = mvreg_read(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset)); 1616 1617 if (queue == -1) { 1618 /* Clear accepts frame bit at specified unicast DA tbl entry */ 1619 unicast_reg &= ~(0xff << (8 * reg_offset)); 1620 } else { 1621 unicast_reg &= ~(0xff << (8 * reg_offset)); 1622 unicast_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset)); 1623 } 1624 1625 mvreg_write(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset), unicast_reg); 1626 } 1627 1628 /* Set mac address */ 1629 static void mvneta_mac_addr_set(struct mvneta_port *pp, unsigned char *addr, 1630 int queue) 1631 { 1632 unsigned int mac_h; 1633 unsigned int mac_l; 1634 1635 if (queue != -1) { 1636 mac_l = (addr[4] << 8) | (addr[5]); 1637 mac_h = (addr[0] << 24) | (addr[1] << 16) | 1638 (addr[2] << 8) | (addr[3] << 0); 1639 1640 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, mac_l); 1641 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, mac_h); 1642 } 1643 1644 /* Accept frames of this address */ 1645 mvneta_set_ucast_addr(pp, addr[5], queue); 1646 } 1647 1648 /* Set the number of packets that will be received before RX interrupt 1649 * will be generated by HW. 1650 */ 1651 static void mvneta_rx_pkts_coal_set(struct mvneta_port *pp, 1652 struct mvneta_rx_queue *rxq, u32 value) 1653 { 1654 mvreg_write(pp, MVNETA_RXQ_THRESHOLD_REG(rxq->id), 1655 value | MVNETA_RXQ_NON_OCCUPIED(0)); 1656 } 1657 1658 /* Set the time delay in usec before RX interrupt will be generated by 1659 * HW. 1660 */ 1661 static void mvneta_rx_time_coal_set(struct mvneta_port *pp, 1662 struct mvneta_rx_queue *rxq, u32 value) 1663 { 1664 u32 val; 1665 unsigned long clk_rate; 1666 1667 clk_rate = clk_get_rate(pp->clk); 1668 val = (clk_rate / 1000000) * value; 1669 1670 mvreg_write(pp, MVNETA_RXQ_TIME_COAL_REG(rxq->id), val); 1671 } 1672 1673 /* Set threshold for TX_DONE pkts coalescing */ 1674 static void mvneta_tx_done_pkts_coal_set(struct mvneta_port *pp, 1675 struct mvneta_tx_queue *txq, u32 value) 1676 { 1677 u32 val; 1678 1679 val = mvreg_read(pp, MVNETA_TXQ_SIZE_REG(txq->id)); 1680 1681 val &= ~MVNETA_TXQ_SENT_THRESH_ALL_MASK; 1682 val |= MVNETA_TXQ_SENT_THRESH_MASK(value); 1683 1684 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), val); 1685 } 1686 1687 /* Handle rx descriptor fill by setting buf_cookie and buf_phys_addr */ 1688 static void mvneta_rx_desc_fill(struct mvneta_rx_desc *rx_desc, 1689 u32 phys_addr, void *virt_addr, 1690 struct mvneta_rx_queue *rxq) 1691 { 1692 int i; 1693 1694 rx_desc->buf_phys_addr = phys_addr; 1695 i = rx_desc - rxq->descs; 1696 rxq->buf_virt_addr[i] = virt_addr; 1697 } 1698 1699 /* Decrement sent descriptors counter */ 1700 static void mvneta_txq_sent_desc_dec(struct mvneta_port *pp, 1701 struct mvneta_tx_queue *txq, 1702 int sent_desc) 1703 { 1704 u32 val; 1705 1706 /* Only 255 TX descriptors can be updated at once */ 1707 while (sent_desc > 0xff) { 1708 val = 0xff << MVNETA_TXQ_DEC_SENT_SHIFT; 1709 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val); 1710 sent_desc = sent_desc - 0xff; 1711 } 1712 1713 val = sent_desc << MVNETA_TXQ_DEC_SENT_SHIFT; 1714 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val); 1715 } 1716 1717 /* Get number of TX descriptors already sent by HW */ 1718 static int mvneta_txq_sent_desc_num_get(struct mvneta_port *pp, 1719 struct mvneta_tx_queue *txq) 1720 { 1721 u32 val; 1722 int sent_desc; 1723 1724 val = mvreg_read(pp, MVNETA_TXQ_STATUS_REG(txq->id)); 1725 sent_desc = (val & MVNETA_TXQ_SENT_DESC_MASK) >> 1726 MVNETA_TXQ_SENT_DESC_SHIFT; 1727 1728 return sent_desc; 1729 } 1730 1731 /* Get number of sent descriptors and decrement counter. 1732 * The number of sent descriptors is returned. 1733 */ 1734 static int mvneta_txq_sent_desc_proc(struct mvneta_port *pp, 1735 struct mvneta_tx_queue *txq) 1736 { 1737 int sent_desc; 1738 1739 /* Get number of sent descriptors */ 1740 sent_desc = mvneta_txq_sent_desc_num_get(pp, txq); 1741 1742 /* Decrement sent descriptors counter */ 1743 if (sent_desc) 1744 mvneta_txq_sent_desc_dec(pp, txq, sent_desc); 1745 1746 return sent_desc; 1747 } 1748 1749 /* Set TXQ descriptors fields relevant for CSUM calculation */ 1750 static u32 mvneta_txq_desc_csum(int l3_offs, int l3_proto, 1751 int ip_hdr_len, int l4_proto) 1752 { 1753 u32 command; 1754 1755 /* Fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk, 1756 * G_L4_chk, L4_type; required only for checksum 1757 * calculation 1758 */ 1759 command = l3_offs << MVNETA_TX_L3_OFF_SHIFT; 1760 command |= ip_hdr_len << MVNETA_TX_IP_HLEN_SHIFT; 1761 1762 if (l3_proto == htons(ETH_P_IP)) 1763 command |= MVNETA_TXD_IP_CSUM; 1764 else 1765 command |= MVNETA_TX_L3_IP6; 1766 1767 if (l4_proto == IPPROTO_TCP) 1768 command |= MVNETA_TX_L4_CSUM_FULL; 1769 else if (l4_proto == IPPROTO_UDP) 1770 command |= MVNETA_TX_L4_UDP | MVNETA_TX_L4_CSUM_FULL; 1771 else 1772 command |= MVNETA_TX_L4_CSUM_NOT; 1773 1774 return command; 1775 } 1776 1777 1778 /* Display more error info */ 1779 static void mvneta_rx_error(struct mvneta_port *pp, 1780 struct mvneta_rx_desc *rx_desc) 1781 { 1782 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats); 1783 u32 status = rx_desc->status; 1784 1785 /* update per-cpu counter */ 1786 u64_stats_update_begin(&stats->syncp); 1787 stats->rx_errors++; 1788 u64_stats_update_end(&stats->syncp); 1789 1790 switch (status & MVNETA_RXD_ERR_CODE_MASK) { 1791 case MVNETA_RXD_ERR_CRC: 1792 netdev_err(pp->dev, "bad rx status %08x (crc error), size=%d\n", 1793 status, rx_desc->data_size); 1794 break; 1795 case MVNETA_RXD_ERR_OVERRUN: 1796 netdev_err(pp->dev, "bad rx status %08x (overrun error), size=%d\n", 1797 status, rx_desc->data_size); 1798 break; 1799 case MVNETA_RXD_ERR_LEN: 1800 netdev_err(pp->dev, "bad rx status %08x (max frame length error), size=%d\n", 1801 status, rx_desc->data_size); 1802 break; 1803 case MVNETA_RXD_ERR_RESOURCE: 1804 netdev_err(pp->dev, "bad rx status %08x (resource error), size=%d\n", 1805 status, rx_desc->data_size); 1806 break; 1807 } 1808 } 1809 1810 /* Handle RX checksum offload based on the descriptor's status */ 1811 static void mvneta_rx_csum(struct mvneta_port *pp, u32 status, 1812 struct sk_buff *skb) 1813 { 1814 if ((pp->dev->features & NETIF_F_RXCSUM) && 1815 (status & MVNETA_RXD_L3_IP4) && 1816 (status & MVNETA_RXD_L4_CSUM_OK)) { 1817 skb->csum = 0; 1818 skb->ip_summed = CHECKSUM_UNNECESSARY; 1819 return; 1820 } 1821 1822 skb->ip_summed = CHECKSUM_NONE; 1823 } 1824 1825 /* Return tx queue pointer (find last set bit) according to <cause> returned 1826 * form tx_done reg. <cause> must not be null. The return value is always a 1827 * valid queue for matching the first one found in <cause>. 1828 */ 1829 static struct mvneta_tx_queue *mvneta_tx_done_policy(struct mvneta_port *pp, 1830 u32 cause) 1831 { 1832 int queue = fls(cause) - 1; 1833 1834 return &pp->txqs[queue]; 1835 } 1836 1837 /* Free tx queue skbuffs */ 1838 static void mvneta_txq_bufs_free(struct mvneta_port *pp, 1839 struct mvneta_tx_queue *txq, int num, 1840 struct netdev_queue *nq) 1841 { 1842 unsigned int bytes_compl = 0, pkts_compl = 0; 1843 int i; 1844 1845 for (i = 0; i < num; i++) { 1846 struct mvneta_tx_buf *buf = &txq->buf[txq->txq_get_index]; 1847 struct mvneta_tx_desc *tx_desc = txq->descs + 1848 txq->txq_get_index; 1849 1850 mvneta_txq_inc_get(txq); 1851 1852 if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr) && 1853 buf->type != MVNETA_TYPE_XDP_TX) 1854 dma_unmap_single(pp->dev->dev.parent, 1855 tx_desc->buf_phys_addr, 1856 tx_desc->data_size, DMA_TO_DEVICE); 1857 if (buf->type == MVNETA_TYPE_SKB && buf->skb) { 1858 bytes_compl += buf->skb->len; 1859 pkts_compl++; 1860 dev_kfree_skb_any(buf->skb); 1861 } else if (buf->type == MVNETA_TYPE_XDP_TX || 1862 buf->type == MVNETA_TYPE_XDP_NDO) { 1863 xdp_return_frame(buf->xdpf); 1864 } 1865 } 1866 1867 netdev_tx_completed_queue(nq, pkts_compl, bytes_compl); 1868 } 1869 1870 /* Handle end of transmission */ 1871 static void mvneta_txq_done(struct mvneta_port *pp, 1872 struct mvneta_tx_queue *txq) 1873 { 1874 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id); 1875 int tx_done; 1876 1877 tx_done = mvneta_txq_sent_desc_proc(pp, txq); 1878 if (!tx_done) 1879 return; 1880 1881 mvneta_txq_bufs_free(pp, txq, tx_done, nq); 1882 1883 txq->count -= tx_done; 1884 1885 if (netif_tx_queue_stopped(nq)) { 1886 if (txq->count <= txq->tx_wake_threshold) 1887 netif_tx_wake_queue(nq); 1888 } 1889 } 1890 1891 /* Refill processing for SW buffer management */ 1892 /* Allocate page per descriptor */ 1893 static int mvneta_rx_refill(struct mvneta_port *pp, 1894 struct mvneta_rx_desc *rx_desc, 1895 struct mvneta_rx_queue *rxq, 1896 gfp_t gfp_mask) 1897 { 1898 dma_addr_t phys_addr; 1899 struct page *page; 1900 1901 page = page_pool_alloc_pages(rxq->page_pool, 1902 gfp_mask | __GFP_NOWARN); 1903 if (!page) 1904 return -ENOMEM; 1905 1906 phys_addr = page_pool_get_dma_addr(page) + pp->rx_offset_correction; 1907 mvneta_rx_desc_fill(rx_desc, phys_addr, page, rxq); 1908 1909 return 0; 1910 } 1911 1912 /* Handle tx checksum */ 1913 static u32 mvneta_skb_tx_csum(struct mvneta_port *pp, struct sk_buff *skb) 1914 { 1915 if (skb->ip_summed == CHECKSUM_PARTIAL) { 1916 int ip_hdr_len = 0; 1917 __be16 l3_proto = vlan_get_protocol(skb); 1918 u8 l4_proto; 1919 1920 if (l3_proto == htons(ETH_P_IP)) { 1921 struct iphdr *ip4h = ip_hdr(skb); 1922 1923 /* Calculate IPv4 checksum and L4 checksum */ 1924 ip_hdr_len = ip4h->ihl; 1925 l4_proto = ip4h->protocol; 1926 } else if (l3_proto == htons(ETH_P_IPV6)) { 1927 struct ipv6hdr *ip6h = ipv6_hdr(skb); 1928 1929 /* Read l4_protocol from one of IPv6 extra headers */ 1930 if (skb_network_header_len(skb) > 0) 1931 ip_hdr_len = (skb_network_header_len(skb) >> 2); 1932 l4_proto = ip6h->nexthdr; 1933 } else 1934 return MVNETA_TX_L4_CSUM_NOT; 1935 1936 return mvneta_txq_desc_csum(skb_network_offset(skb), 1937 l3_proto, ip_hdr_len, l4_proto); 1938 } 1939 1940 return MVNETA_TX_L4_CSUM_NOT; 1941 } 1942 1943 /* Drop packets received by the RXQ and free buffers */ 1944 static void mvneta_rxq_drop_pkts(struct mvneta_port *pp, 1945 struct mvneta_rx_queue *rxq) 1946 { 1947 int rx_done, i; 1948 1949 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq); 1950 if (rx_done) 1951 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done); 1952 1953 if (pp->bm_priv) { 1954 for (i = 0; i < rx_done; i++) { 1955 struct mvneta_rx_desc *rx_desc = 1956 mvneta_rxq_next_desc_get(rxq); 1957 u8 pool_id = MVNETA_RX_GET_BM_POOL_ID(rx_desc); 1958 struct mvneta_bm_pool *bm_pool; 1959 1960 bm_pool = &pp->bm_priv->bm_pools[pool_id]; 1961 /* Return dropped buffer to the pool */ 1962 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool, 1963 rx_desc->buf_phys_addr); 1964 } 1965 return; 1966 } 1967 1968 for (i = 0; i < rxq->size; i++) { 1969 struct mvneta_rx_desc *rx_desc = rxq->descs + i; 1970 void *data = rxq->buf_virt_addr[i]; 1971 if (!data || !(rx_desc->buf_phys_addr)) 1972 continue; 1973 1974 page_pool_put_full_page(rxq->page_pool, data, false); 1975 } 1976 if (xdp_rxq_info_is_reg(&rxq->xdp_rxq)) 1977 xdp_rxq_info_unreg(&rxq->xdp_rxq); 1978 page_pool_destroy(rxq->page_pool); 1979 rxq->page_pool = NULL; 1980 } 1981 1982 static void 1983 mvneta_update_stats(struct mvneta_port *pp, 1984 struct mvneta_stats *ps) 1985 { 1986 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats); 1987 1988 u64_stats_update_begin(&stats->syncp); 1989 stats->es.ps.rx_packets += ps->rx_packets; 1990 stats->es.ps.rx_bytes += ps->rx_bytes; 1991 /* xdp */ 1992 stats->es.ps.xdp_redirect += ps->xdp_redirect; 1993 stats->es.ps.xdp_pass += ps->xdp_pass; 1994 stats->es.ps.xdp_drop += ps->xdp_drop; 1995 u64_stats_update_end(&stats->syncp); 1996 } 1997 1998 static inline 1999 int mvneta_rx_refill_queue(struct mvneta_port *pp, struct mvneta_rx_queue *rxq) 2000 { 2001 struct mvneta_rx_desc *rx_desc; 2002 int curr_desc = rxq->first_to_refill; 2003 int i; 2004 2005 for (i = 0; (i < rxq->refill_num) && (i < 64); i++) { 2006 rx_desc = rxq->descs + curr_desc; 2007 if (!(rx_desc->buf_phys_addr)) { 2008 if (mvneta_rx_refill(pp, rx_desc, rxq, GFP_ATOMIC)) { 2009 struct mvneta_pcpu_stats *stats; 2010 2011 pr_err("Can't refill queue %d. Done %d from %d\n", 2012 rxq->id, i, rxq->refill_num); 2013 2014 stats = this_cpu_ptr(pp->stats); 2015 u64_stats_update_begin(&stats->syncp); 2016 stats->es.refill_error++; 2017 u64_stats_update_end(&stats->syncp); 2018 break; 2019 } 2020 } 2021 curr_desc = MVNETA_QUEUE_NEXT_DESC(rxq, curr_desc); 2022 } 2023 rxq->refill_num -= i; 2024 rxq->first_to_refill = curr_desc; 2025 2026 return i; 2027 } 2028 2029 static int 2030 mvneta_xdp_submit_frame(struct mvneta_port *pp, struct mvneta_tx_queue *txq, 2031 struct xdp_frame *xdpf, bool dma_map) 2032 { 2033 struct mvneta_tx_desc *tx_desc; 2034 struct mvneta_tx_buf *buf; 2035 dma_addr_t dma_addr; 2036 2037 if (txq->count >= txq->tx_stop_threshold) 2038 return MVNETA_XDP_DROPPED; 2039 2040 tx_desc = mvneta_txq_next_desc_get(txq); 2041 2042 buf = &txq->buf[txq->txq_put_index]; 2043 if (dma_map) { 2044 /* ndo_xdp_xmit */ 2045 dma_addr = dma_map_single(pp->dev->dev.parent, xdpf->data, 2046 xdpf->len, DMA_TO_DEVICE); 2047 if (dma_mapping_error(pp->dev->dev.parent, dma_addr)) { 2048 mvneta_txq_desc_put(txq); 2049 return MVNETA_XDP_DROPPED; 2050 } 2051 buf->type = MVNETA_TYPE_XDP_NDO; 2052 } else { 2053 struct page *page = virt_to_page(xdpf->data); 2054 2055 dma_addr = page_pool_get_dma_addr(page) + 2056 sizeof(*xdpf) + xdpf->headroom; 2057 dma_sync_single_for_device(pp->dev->dev.parent, dma_addr, 2058 xdpf->len, DMA_BIDIRECTIONAL); 2059 buf->type = MVNETA_TYPE_XDP_TX; 2060 } 2061 buf->xdpf = xdpf; 2062 2063 tx_desc->command = MVNETA_TXD_FLZ_DESC; 2064 tx_desc->buf_phys_addr = dma_addr; 2065 tx_desc->data_size = xdpf->len; 2066 2067 mvneta_txq_inc_put(txq); 2068 txq->pending++; 2069 txq->count++; 2070 2071 return MVNETA_XDP_TX; 2072 } 2073 2074 static int 2075 mvneta_xdp_xmit_back(struct mvneta_port *pp, struct xdp_buff *xdp) 2076 { 2077 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats); 2078 struct mvneta_tx_queue *txq; 2079 struct netdev_queue *nq; 2080 struct xdp_frame *xdpf; 2081 int cpu; 2082 u32 ret; 2083 2084 xdpf = xdp_convert_buff_to_frame(xdp); 2085 if (unlikely(!xdpf)) 2086 return MVNETA_XDP_DROPPED; 2087 2088 cpu = smp_processor_id(); 2089 txq = &pp->txqs[cpu % txq_number]; 2090 nq = netdev_get_tx_queue(pp->dev, txq->id); 2091 2092 __netif_tx_lock(nq, cpu); 2093 ret = mvneta_xdp_submit_frame(pp, txq, xdpf, false); 2094 if (ret == MVNETA_XDP_TX) { 2095 u64_stats_update_begin(&stats->syncp); 2096 stats->es.ps.tx_bytes += xdpf->len; 2097 stats->es.ps.tx_packets++; 2098 stats->es.ps.xdp_tx++; 2099 u64_stats_update_end(&stats->syncp); 2100 2101 mvneta_txq_pend_desc_add(pp, txq, 0); 2102 } else { 2103 u64_stats_update_begin(&stats->syncp); 2104 stats->es.ps.xdp_tx_err++; 2105 u64_stats_update_end(&stats->syncp); 2106 } 2107 __netif_tx_unlock(nq); 2108 2109 return ret; 2110 } 2111 2112 static int 2113 mvneta_xdp_xmit(struct net_device *dev, int num_frame, 2114 struct xdp_frame **frames, u32 flags) 2115 { 2116 struct mvneta_port *pp = netdev_priv(dev); 2117 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats); 2118 int i, nxmit_byte = 0, nxmit = num_frame; 2119 int cpu = smp_processor_id(); 2120 struct mvneta_tx_queue *txq; 2121 struct netdev_queue *nq; 2122 u32 ret; 2123 2124 if (unlikely(test_bit(__MVNETA_DOWN, &pp->state))) 2125 return -ENETDOWN; 2126 2127 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) 2128 return -EINVAL; 2129 2130 txq = &pp->txqs[cpu % txq_number]; 2131 nq = netdev_get_tx_queue(pp->dev, txq->id); 2132 2133 __netif_tx_lock(nq, cpu); 2134 for (i = 0; i < num_frame; i++) { 2135 ret = mvneta_xdp_submit_frame(pp, txq, frames[i], true); 2136 if (ret == MVNETA_XDP_TX) { 2137 nxmit_byte += frames[i]->len; 2138 } else { 2139 xdp_return_frame_rx_napi(frames[i]); 2140 nxmit--; 2141 } 2142 } 2143 2144 if (unlikely(flags & XDP_XMIT_FLUSH)) 2145 mvneta_txq_pend_desc_add(pp, txq, 0); 2146 __netif_tx_unlock(nq); 2147 2148 u64_stats_update_begin(&stats->syncp); 2149 stats->es.ps.tx_bytes += nxmit_byte; 2150 stats->es.ps.tx_packets += nxmit; 2151 stats->es.ps.xdp_xmit += nxmit; 2152 stats->es.ps.xdp_xmit_err += num_frame - nxmit; 2153 u64_stats_update_end(&stats->syncp); 2154 2155 return nxmit; 2156 } 2157 2158 static int 2159 mvneta_run_xdp(struct mvneta_port *pp, struct mvneta_rx_queue *rxq, 2160 struct bpf_prog *prog, struct xdp_buff *xdp, 2161 struct mvneta_stats *stats) 2162 { 2163 unsigned int len, sync; 2164 struct page *page; 2165 u32 ret, act; 2166 2167 len = xdp->data_end - xdp->data_hard_start - pp->rx_offset_correction; 2168 act = bpf_prog_run_xdp(prog, xdp); 2169 2170 /* Due xdp_adjust_tail: DMA sync for_device cover max len CPU touch */ 2171 sync = xdp->data_end - xdp->data_hard_start - pp->rx_offset_correction; 2172 sync = max(sync, len); 2173 2174 switch (act) { 2175 case XDP_PASS: 2176 stats->xdp_pass++; 2177 return MVNETA_XDP_PASS; 2178 case XDP_REDIRECT: { 2179 int err; 2180 2181 err = xdp_do_redirect(pp->dev, xdp, prog); 2182 if (unlikely(err)) { 2183 ret = MVNETA_XDP_DROPPED; 2184 page = virt_to_head_page(xdp->data); 2185 page_pool_put_page(rxq->page_pool, page, sync, true); 2186 } else { 2187 ret = MVNETA_XDP_REDIR; 2188 stats->xdp_redirect++; 2189 } 2190 break; 2191 } 2192 case XDP_TX: 2193 ret = mvneta_xdp_xmit_back(pp, xdp); 2194 if (ret != MVNETA_XDP_TX) { 2195 page = virt_to_head_page(xdp->data); 2196 page_pool_put_page(rxq->page_pool, page, sync, true); 2197 } 2198 break; 2199 default: 2200 bpf_warn_invalid_xdp_action(act); 2201 /* fall through */ 2202 case XDP_ABORTED: 2203 trace_xdp_exception(pp->dev, prog, act); 2204 /* fall through */ 2205 case XDP_DROP: 2206 page = virt_to_head_page(xdp->data); 2207 page_pool_put_page(rxq->page_pool, page, sync, true); 2208 ret = MVNETA_XDP_DROPPED; 2209 stats->xdp_drop++; 2210 break; 2211 } 2212 2213 stats->rx_bytes += xdp->data_end - xdp->data; 2214 stats->rx_packets++; 2215 2216 return ret; 2217 } 2218 2219 static int 2220 mvneta_swbm_rx_frame(struct mvneta_port *pp, 2221 struct mvneta_rx_desc *rx_desc, 2222 struct mvneta_rx_queue *rxq, 2223 struct xdp_buff *xdp, 2224 struct bpf_prog *xdp_prog, 2225 struct page *page, 2226 struct mvneta_stats *stats) 2227 { 2228 unsigned char *data = page_address(page); 2229 int data_len = -MVNETA_MH_SIZE, len; 2230 struct net_device *dev = pp->dev; 2231 enum dma_data_direction dma_dir; 2232 int ret = 0; 2233 2234 if (MVNETA_SKB_SIZE(rx_desc->data_size) > PAGE_SIZE) { 2235 len = MVNETA_MAX_RX_BUF_SIZE; 2236 data_len += len; 2237 } else { 2238 len = rx_desc->data_size; 2239 data_len += len - ETH_FCS_LEN; 2240 } 2241 2242 dma_dir = page_pool_get_dma_dir(rxq->page_pool); 2243 dma_sync_single_for_cpu(dev->dev.parent, 2244 rx_desc->buf_phys_addr, 2245 len, dma_dir); 2246 2247 /* Prefetch header */ 2248 prefetch(data); 2249 2250 xdp->data_hard_start = data; 2251 xdp->data = data + pp->rx_offset_correction + MVNETA_MH_SIZE; 2252 xdp->data_end = xdp->data + data_len; 2253 xdp_set_data_meta_invalid(xdp); 2254 2255 if (xdp_prog) { 2256 ret = mvneta_run_xdp(pp, rxq, xdp_prog, xdp, stats); 2257 if (ret) 2258 goto out; 2259 } 2260 2261 rxq->skb = build_skb(xdp->data_hard_start, PAGE_SIZE); 2262 if (unlikely(!rxq->skb)) { 2263 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats); 2264 2265 netdev_err(dev, "Can't allocate skb on queue %d\n", rxq->id); 2266 2267 u64_stats_update_begin(&stats->syncp); 2268 stats->es.skb_alloc_error++; 2269 stats->rx_dropped++; 2270 u64_stats_update_end(&stats->syncp); 2271 2272 return -ENOMEM; 2273 } 2274 page_pool_release_page(rxq->page_pool, page); 2275 2276 skb_reserve(rxq->skb, 2277 xdp->data - xdp->data_hard_start); 2278 skb_put(rxq->skb, xdp->data_end - xdp->data); 2279 mvneta_rx_csum(pp, rx_desc->status, rxq->skb); 2280 2281 rxq->left_size = rx_desc->data_size - len; 2282 2283 out: 2284 rx_desc->buf_phys_addr = 0; 2285 2286 return ret; 2287 } 2288 2289 static void 2290 mvneta_swbm_add_rx_fragment(struct mvneta_port *pp, 2291 struct mvneta_rx_desc *rx_desc, 2292 struct mvneta_rx_queue *rxq, 2293 struct page *page) 2294 { 2295 struct net_device *dev = pp->dev; 2296 enum dma_data_direction dma_dir; 2297 int data_len, len; 2298 2299 if (rxq->left_size > MVNETA_MAX_RX_BUF_SIZE) { 2300 len = MVNETA_MAX_RX_BUF_SIZE; 2301 data_len = len; 2302 } else { 2303 len = rxq->left_size; 2304 data_len = len - ETH_FCS_LEN; 2305 } 2306 dma_dir = page_pool_get_dma_dir(rxq->page_pool); 2307 dma_sync_single_for_cpu(dev->dev.parent, 2308 rx_desc->buf_phys_addr, 2309 len, dma_dir); 2310 if (data_len > 0) { 2311 /* refill descriptor with new buffer later */ 2312 skb_add_rx_frag(rxq->skb, 2313 skb_shinfo(rxq->skb)->nr_frags, 2314 page, pp->rx_offset_correction, data_len, 2315 PAGE_SIZE); 2316 } 2317 page_pool_release_page(rxq->page_pool, page); 2318 rx_desc->buf_phys_addr = 0; 2319 rxq->left_size -= len; 2320 } 2321 2322 /* Main rx processing when using software buffer management */ 2323 static int mvneta_rx_swbm(struct napi_struct *napi, 2324 struct mvneta_port *pp, int budget, 2325 struct mvneta_rx_queue *rxq) 2326 { 2327 int rx_proc = 0, rx_todo, refill; 2328 struct net_device *dev = pp->dev; 2329 struct mvneta_stats ps = {}; 2330 struct bpf_prog *xdp_prog; 2331 struct xdp_buff xdp_buf; 2332 2333 /* Get number of received packets */ 2334 rx_todo = mvneta_rxq_busy_desc_num_get(pp, rxq); 2335 2336 rcu_read_lock(); 2337 xdp_prog = READ_ONCE(pp->xdp_prog); 2338 xdp_buf.rxq = &rxq->xdp_rxq; 2339 xdp_buf.frame_sz = PAGE_SIZE; 2340 2341 /* Fairness NAPI loop */ 2342 while (rx_proc < budget && rx_proc < rx_todo) { 2343 struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq); 2344 u32 rx_status, index; 2345 struct page *page; 2346 2347 index = rx_desc - rxq->descs; 2348 page = (struct page *)rxq->buf_virt_addr[index]; 2349 2350 rx_status = rx_desc->status; 2351 rx_proc++; 2352 rxq->refill_num++; 2353 2354 if (rx_status & MVNETA_RXD_FIRST_DESC) { 2355 int err; 2356 2357 /* Check errors only for FIRST descriptor */ 2358 if (rx_status & MVNETA_RXD_ERR_SUMMARY) { 2359 mvneta_rx_error(pp, rx_desc); 2360 /* leave the descriptor untouched */ 2361 continue; 2362 } 2363 2364 err = mvneta_swbm_rx_frame(pp, rx_desc, rxq, &xdp_buf, 2365 xdp_prog, page, &ps); 2366 if (err) 2367 continue; 2368 } else { 2369 if (unlikely(!rxq->skb)) { 2370 pr_debug("no skb for rx_status 0x%x\n", 2371 rx_status); 2372 continue; 2373 } 2374 mvneta_swbm_add_rx_fragment(pp, rx_desc, rxq, page); 2375 } /* Middle or Last descriptor */ 2376 2377 if (!(rx_status & MVNETA_RXD_LAST_DESC)) 2378 /* no last descriptor this time */ 2379 continue; 2380 2381 if (rxq->left_size) { 2382 pr_err("get last desc, but left_size (%d) != 0\n", 2383 rxq->left_size); 2384 dev_kfree_skb_any(rxq->skb); 2385 rxq->left_size = 0; 2386 rxq->skb = NULL; 2387 continue; 2388 } 2389 2390 ps.rx_bytes += rxq->skb->len; 2391 ps.rx_packets++; 2392 2393 /* Linux processing */ 2394 rxq->skb->protocol = eth_type_trans(rxq->skb, dev); 2395 2396 napi_gro_receive(napi, rxq->skb); 2397 2398 /* clean uncomplete skb pointer in queue */ 2399 rxq->skb = NULL; 2400 } 2401 rcu_read_unlock(); 2402 2403 if (ps.xdp_redirect) 2404 xdp_do_flush_map(); 2405 2406 if (ps.rx_packets) 2407 mvneta_update_stats(pp, &ps); 2408 2409 /* return some buffers to hardware queue, one at a time is too slow */ 2410 refill = mvneta_rx_refill_queue(pp, rxq); 2411 2412 /* Update rxq management counters */ 2413 mvneta_rxq_desc_num_update(pp, rxq, rx_proc, refill); 2414 2415 return ps.rx_packets; 2416 } 2417 2418 /* Main rx processing when using hardware buffer management */ 2419 static int mvneta_rx_hwbm(struct napi_struct *napi, 2420 struct mvneta_port *pp, int rx_todo, 2421 struct mvneta_rx_queue *rxq) 2422 { 2423 struct net_device *dev = pp->dev; 2424 int rx_done; 2425 u32 rcvd_pkts = 0; 2426 u32 rcvd_bytes = 0; 2427 2428 /* Get number of received packets */ 2429 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq); 2430 2431 if (rx_todo > rx_done) 2432 rx_todo = rx_done; 2433 2434 rx_done = 0; 2435 2436 /* Fairness NAPI loop */ 2437 while (rx_done < rx_todo) { 2438 struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq); 2439 struct mvneta_bm_pool *bm_pool = NULL; 2440 struct sk_buff *skb; 2441 unsigned char *data; 2442 dma_addr_t phys_addr; 2443 u32 rx_status, frag_size; 2444 int rx_bytes, err; 2445 u8 pool_id; 2446 2447 rx_done++; 2448 rx_status = rx_desc->status; 2449 rx_bytes = rx_desc->data_size - (ETH_FCS_LEN + MVNETA_MH_SIZE); 2450 data = (u8 *)(uintptr_t)rx_desc->buf_cookie; 2451 phys_addr = rx_desc->buf_phys_addr; 2452 pool_id = MVNETA_RX_GET_BM_POOL_ID(rx_desc); 2453 bm_pool = &pp->bm_priv->bm_pools[pool_id]; 2454 2455 if (!mvneta_rxq_desc_is_first_last(rx_status) || 2456 (rx_status & MVNETA_RXD_ERR_SUMMARY)) { 2457 err_drop_frame_ret_pool: 2458 /* Return the buffer to the pool */ 2459 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool, 2460 rx_desc->buf_phys_addr); 2461 err_drop_frame: 2462 mvneta_rx_error(pp, rx_desc); 2463 /* leave the descriptor untouched */ 2464 continue; 2465 } 2466 2467 if (rx_bytes <= rx_copybreak) { 2468 /* better copy a small frame and not unmap the DMA region */ 2469 skb = netdev_alloc_skb_ip_align(dev, rx_bytes); 2470 if (unlikely(!skb)) 2471 goto err_drop_frame_ret_pool; 2472 2473 dma_sync_single_range_for_cpu(&pp->bm_priv->pdev->dev, 2474 rx_desc->buf_phys_addr, 2475 MVNETA_MH_SIZE + NET_SKB_PAD, 2476 rx_bytes, 2477 DMA_FROM_DEVICE); 2478 skb_put_data(skb, data + MVNETA_MH_SIZE + NET_SKB_PAD, 2479 rx_bytes); 2480 2481 skb->protocol = eth_type_trans(skb, dev); 2482 mvneta_rx_csum(pp, rx_status, skb); 2483 napi_gro_receive(napi, skb); 2484 2485 rcvd_pkts++; 2486 rcvd_bytes += rx_bytes; 2487 2488 /* Return the buffer to the pool */ 2489 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool, 2490 rx_desc->buf_phys_addr); 2491 2492 /* leave the descriptor and buffer untouched */ 2493 continue; 2494 } 2495 2496 /* Refill processing */ 2497 err = hwbm_pool_refill(&bm_pool->hwbm_pool, GFP_ATOMIC); 2498 if (err) { 2499 struct mvneta_pcpu_stats *stats; 2500 2501 netdev_err(dev, "Linux processing - Can't refill\n"); 2502 2503 stats = this_cpu_ptr(pp->stats); 2504 u64_stats_update_begin(&stats->syncp); 2505 stats->es.refill_error++; 2506 u64_stats_update_end(&stats->syncp); 2507 2508 goto err_drop_frame_ret_pool; 2509 } 2510 2511 frag_size = bm_pool->hwbm_pool.frag_size; 2512 2513 skb = build_skb(data, frag_size > PAGE_SIZE ? 0 : frag_size); 2514 2515 /* After refill old buffer has to be unmapped regardless 2516 * the skb is successfully built or not. 2517 */ 2518 dma_unmap_single(&pp->bm_priv->pdev->dev, phys_addr, 2519 bm_pool->buf_size, DMA_FROM_DEVICE); 2520 if (!skb) 2521 goto err_drop_frame; 2522 2523 rcvd_pkts++; 2524 rcvd_bytes += rx_bytes; 2525 2526 /* Linux processing */ 2527 skb_reserve(skb, MVNETA_MH_SIZE + NET_SKB_PAD); 2528 skb_put(skb, rx_bytes); 2529 2530 skb->protocol = eth_type_trans(skb, dev); 2531 2532 mvneta_rx_csum(pp, rx_status, skb); 2533 2534 napi_gro_receive(napi, skb); 2535 } 2536 2537 if (rcvd_pkts) { 2538 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats); 2539 2540 u64_stats_update_begin(&stats->syncp); 2541 stats->es.ps.rx_packets += rcvd_pkts; 2542 stats->es.ps.rx_bytes += rcvd_bytes; 2543 u64_stats_update_end(&stats->syncp); 2544 } 2545 2546 /* Update rxq management counters */ 2547 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done); 2548 2549 return rx_done; 2550 } 2551 2552 static inline void 2553 mvneta_tso_put_hdr(struct sk_buff *skb, 2554 struct mvneta_port *pp, struct mvneta_tx_queue *txq) 2555 { 2556 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb); 2557 struct mvneta_tx_buf *buf = &txq->buf[txq->txq_put_index]; 2558 struct mvneta_tx_desc *tx_desc; 2559 2560 tx_desc = mvneta_txq_next_desc_get(txq); 2561 tx_desc->data_size = hdr_len; 2562 tx_desc->command = mvneta_skb_tx_csum(pp, skb); 2563 tx_desc->command |= MVNETA_TXD_F_DESC; 2564 tx_desc->buf_phys_addr = txq->tso_hdrs_phys + 2565 txq->txq_put_index * TSO_HEADER_SIZE; 2566 buf->type = MVNETA_TYPE_SKB; 2567 buf->skb = NULL; 2568 2569 mvneta_txq_inc_put(txq); 2570 } 2571 2572 static inline int 2573 mvneta_tso_put_data(struct net_device *dev, struct mvneta_tx_queue *txq, 2574 struct sk_buff *skb, char *data, int size, 2575 bool last_tcp, bool is_last) 2576 { 2577 struct mvneta_tx_buf *buf = &txq->buf[txq->txq_put_index]; 2578 struct mvneta_tx_desc *tx_desc; 2579 2580 tx_desc = mvneta_txq_next_desc_get(txq); 2581 tx_desc->data_size = size; 2582 tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, data, 2583 size, DMA_TO_DEVICE); 2584 if (unlikely(dma_mapping_error(dev->dev.parent, 2585 tx_desc->buf_phys_addr))) { 2586 mvneta_txq_desc_put(txq); 2587 return -ENOMEM; 2588 } 2589 2590 tx_desc->command = 0; 2591 buf->type = MVNETA_TYPE_SKB; 2592 buf->skb = NULL; 2593 2594 if (last_tcp) { 2595 /* last descriptor in the TCP packet */ 2596 tx_desc->command = MVNETA_TXD_L_DESC; 2597 2598 /* last descriptor in SKB */ 2599 if (is_last) 2600 buf->skb = skb; 2601 } 2602 mvneta_txq_inc_put(txq); 2603 return 0; 2604 } 2605 2606 static int mvneta_tx_tso(struct sk_buff *skb, struct net_device *dev, 2607 struct mvneta_tx_queue *txq) 2608 { 2609 int hdr_len, total_len, data_left; 2610 int desc_count = 0; 2611 struct mvneta_port *pp = netdev_priv(dev); 2612 struct tso_t tso; 2613 int i; 2614 2615 /* Count needed descriptors */ 2616 if ((txq->count + tso_count_descs(skb)) >= txq->size) 2617 return 0; 2618 2619 if (skb_headlen(skb) < (skb_transport_offset(skb) + tcp_hdrlen(skb))) { 2620 pr_info("*** Is this even possible???!?!?\n"); 2621 return 0; 2622 } 2623 2624 /* Initialize the TSO handler, and prepare the first payload */ 2625 hdr_len = tso_start(skb, &tso); 2626 2627 total_len = skb->len - hdr_len; 2628 while (total_len > 0) { 2629 char *hdr; 2630 2631 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len); 2632 total_len -= data_left; 2633 desc_count++; 2634 2635 /* prepare packet headers: MAC + IP + TCP */ 2636 hdr = txq->tso_hdrs + txq->txq_put_index * TSO_HEADER_SIZE; 2637 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0); 2638 2639 mvneta_tso_put_hdr(skb, pp, txq); 2640 2641 while (data_left > 0) { 2642 int size; 2643 desc_count++; 2644 2645 size = min_t(int, tso.size, data_left); 2646 2647 if (mvneta_tso_put_data(dev, txq, skb, 2648 tso.data, size, 2649 size == data_left, 2650 total_len == 0)) 2651 goto err_release; 2652 data_left -= size; 2653 2654 tso_build_data(skb, &tso, size); 2655 } 2656 } 2657 2658 return desc_count; 2659 2660 err_release: 2661 /* Release all used data descriptors; header descriptors must not 2662 * be DMA-unmapped. 2663 */ 2664 for (i = desc_count - 1; i >= 0; i--) { 2665 struct mvneta_tx_desc *tx_desc = txq->descs + i; 2666 if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr)) 2667 dma_unmap_single(pp->dev->dev.parent, 2668 tx_desc->buf_phys_addr, 2669 tx_desc->data_size, 2670 DMA_TO_DEVICE); 2671 mvneta_txq_desc_put(txq); 2672 } 2673 return 0; 2674 } 2675 2676 /* Handle tx fragmentation processing */ 2677 static int mvneta_tx_frag_process(struct mvneta_port *pp, struct sk_buff *skb, 2678 struct mvneta_tx_queue *txq) 2679 { 2680 struct mvneta_tx_desc *tx_desc; 2681 int i, nr_frags = skb_shinfo(skb)->nr_frags; 2682 2683 for (i = 0; i < nr_frags; i++) { 2684 struct mvneta_tx_buf *buf = &txq->buf[txq->txq_put_index]; 2685 skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 2686 void *addr = skb_frag_address(frag); 2687 2688 tx_desc = mvneta_txq_next_desc_get(txq); 2689 tx_desc->data_size = skb_frag_size(frag); 2690 2691 tx_desc->buf_phys_addr = 2692 dma_map_single(pp->dev->dev.parent, addr, 2693 tx_desc->data_size, DMA_TO_DEVICE); 2694 2695 if (dma_mapping_error(pp->dev->dev.parent, 2696 tx_desc->buf_phys_addr)) { 2697 mvneta_txq_desc_put(txq); 2698 goto error; 2699 } 2700 2701 if (i == nr_frags - 1) { 2702 /* Last descriptor */ 2703 tx_desc->command = MVNETA_TXD_L_DESC | MVNETA_TXD_Z_PAD; 2704 buf->skb = skb; 2705 } else { 2706 /* Descriptor in the middle: Not First, Not Last */ 2707 tx_desc->command = 0; 2708 buf->skb = NULL; 2709 } 2710 buf->type = MVNETA_TYPE_SKB; 2711 mvneta_txq_inc_put(txq); 2712 } 2713 2714 return 0; 2715 2716 error: 2717 /* Release all descriptors that were used to map fragments of 2718 * this packet, as well as the corresponding DMA mappings 2719 */ 2720 for (i = i - 1; i >= 0; i--) { 2721 tx_desc = txq->descs + i; 2722 dma_unmap_single(pp->dev->dev.parent, 2723 tx_desc->buf_phys_addr, 2724 tx_desc->data_size, 2725 DMA_TO_DEVICE); 2726 mvneta_txq_desc_put(txq); 2727 } 2728 2729 return -ENOMEM; 2730 } 2731 2732 /* Main tx processing */ 2733 static netdev_tx_t mvneta_tx(struct sk_buff *skb, struct net_device *dev) 2734 { 2735 struct mvneta_port *pp = netdev_priv(dev); 2736 u16 txq_id = skb_get_queue_mapping(skb); 2737 struct mvneta_tx_queue *txq = &pp->txqs[txq_id]; 2738 struct mvneta_tx_buf *buf = &txq->buf[txq->txq_put_index]; 2739 struct mvneta_tx_desc *tx_desc; 2740 int len = skb->len; 2741 int frags = 0; 2742 u32 tx_cmd; 2743 2744 if (!netif_running(dev)) 2745 goto out; 2746 2747 if (skb_is_gso(skb)) { 2748 frags = mvneta_tx_tso(skb, dev, txq); 2749 goto out; 2750 } 2751 2752 frags = skb_shinfo(skb)->nr_frags + 1; 2753 2754 /* Get a descriptor for the first part of the packet */ 2755 tx_desc = mvneta_txq_next_desc_get(txq); 2756 2757 tx_cmd = mvneta_skb_tx_csum(pp, skb); 2758 2759 tx_desc->data_size = skb_headlen(skb); 2760 2761 tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, skb->data, 2762 tx_desc->data_size, 2763 DMA_TO_DEVICE); 2764 if (unlikely(dma_mapping_error(dev->dev.parent, 2765 tx_desc->buf_phys_addr))) { 2766 mvneta_txq_desc_put(txq); 2767 frags = 0; 2768 goto out; 2769 } 2770 2771 buf->type = MVNETA_TYPE_SKB; 2772 if (frags == 1) { 2773 /* First and Last descriptor */ 2774 tx_cmd |= MVNETA_TXD_FLZ_DESC; 2775 tx_desc->command = tx_cmd; 2776 buf->skb = skb; 2777 mvneta_txq_inc_put(txq); 2778 } else { 2779 /* First but not Last */ 2780 tx_cmd |= MVNETA_TXD_F_DESC; 2781 buf->skb = NULL; 2782 mvneta_txq_inc_put(txq); 2783 tx_desc->command = tx_cmd; 2784 /* Continue with other skb fragments */ 2785 if (mvneta_tx_frag_process(pp, skb, txq)) { 2786 dma_unmap_single(dev->dev.parent, 2787 tx_desc->buf_phys_addr, 2788 tx_desc->data_size, 2789 DMA_TO_DEVICE); 2790 mvneta_txq_desc_put(txq); 2791 frags = 0; 2792 goto out; 2793 } 2794 } 2795 2796 out: 2797 if (frags > 0) { 2798 struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id); 2799 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats); 2800 2801 netdev_tx_sent_queue(nq, len); 2802 2803 txq->count += frags; 2804 if (txq->count >= txq->tx_stop_threshold) 2805 netif_tx_stop_queue(nq); 2806 2807 if (!netdev_xmit_more() || netif_xmit_stopped(nq) || 2808 txq->pending + frags > MVNETA_TXQ_DEC_SENT_MASK) 2809 mvneta_txq_pend_desc_add(pp, txq, frags); 2810 else 2811 txq->pending += frags; 2812 2813 u64_stats_update_begin(&stats->syncp); 2814 stats->es.ps.tx_bytes += len; 2815 stats->es.ps.tx_packets++; 2816 u64_stats_update_end(&stats->syncp); 2817 } else { 2818 dev->stats.tx_dropped++; 2819 dev_kfree_skb_any(skb); 2820 } 2821 2822 return NETDEV_TX_OK; 2823 } 2824 2825 2826 /* Free tx resources, when resetting a port */ 2827 static void mvneta_txq_done_force(struct mvneta_port *pp, 2828 struct mvneta_tx_queue *txq) 2829 2830 { 2831 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id); 2832 int tx_done = txq->count; 2833 2834 mvneta_txq_bufs_free(pp, txq, tx_done, nq); 2835 2836 /* reset txq */ 2837 txq->count = 0; 2838 txq->txq_put_index = 0; 2839 txq->txq_get_index = 0; 2840 } 2841 2842 /* Handle tx done - called in softirq context. The <cause_tx_done> argument 2843 * must be a valid cause according to MVNETA_TXQ_INTR_MASK_ALL. 2844 */ 2845 static void mvneta_tx_done_gbe(struct mvneta_port *pp, u32 cause_tx_done) 2846 { 2847 struct mvneta_tx_queue *txq; 2848 struct netdev_queue *nq; 2849 int cpu = smp_processor_id(); 2850 2851 while (cause_tx_done) { 2852 txq = mvneta_tx_done_policy(pp, cause_tx_done); 2853 2854 nq = netdev_get_tx_queue(pp->dev, txq->id); 2855 __netif_tx_lock(nq, cpu); 2856 2857 if (txq->count) 2858 mvneta_txq_done(pp, txq); 2859 2860 __netif_tx_unlock(nq); 2861 cause_tx_done &= ~((1 << txq->id)); 2862 } 2863 } 2864 2865 /* Compute crc8 of the specified address, using a unique algorithm , 2866 * according to hw spec, different than generic crc8 algorithm 2867 */ 2868 static int mvneta_addr_crc(unsigned char *addr) 2869 { 2870 int crc = 0; 2871 int i; 2872 2873 for (i = 0; i < ETH_ALEN; i++) { 2874 int j; 2875 2876 crc = (crc ^ addr[i]) << 8; 2877 for (j = 7; j >= 0; j--) { 2878 if (crc & (0x100 << j)) 2879 crc ^= 0x107 << j; 2880 } 2881 } 2882 2883 return crc; 2884 } 2885 2886 /* This method controls the net device special MAC multicast support. 2887 * The Special Multicast Table for MAC addresses supports MAC of the form 2888 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF). 2889 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast 2890 * Table entries in the DA-Filter table. This method set the Special 2891 * Multicast Table appropriate entry. 2892 */ 2893 static void mvneta_set_special_mcast_addr(struct mvneta_port *pp, 2894 unsigned char last_byte, 2895 int queue) 2896 { 2897 unsigned int smc_table_reg; 2898 unsigned int tbl_offset; 2899 unsigned int reg_offset; 2900 2901 /* Register offset from SMC table base */ 2902 tbl_offset = (last_byte / 4); 2903 /* Entry offset within the above reg */ 2904 reg_offset = last_byte % 4; 2905 2906 smc_table_reg = mvreg_read(pp, (MVNETA_DA_FILT_SPEC_MCAST 2907 + tbl_offset * 4)); 2908 2909 if (queue == -1) 2910 smc_table_reg &= ~(0xff << (8 * reg_offset)); 2911 else { 2912 smc_table_reg &= ~(0xff << (8 * reg_offset)); 2913 smc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset)); 2914 } 2915 2916 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + tbl_offset * 4, 2917 smc_table_reg); 2918 } 2919 2920 /* This method controls the network device Other MAC multicast support. 2921 * The Other Multicast Table is used for multicast of another type. 2922 * A CRC-8 is used as an index to the Other Multicast Table entries 2923 * in the DA-Filter table. 2924 * The method gets the CRC-8 value from the calling routine and 2925 * sets the Other Multicast Table appropriate entry according to the 2926 * specified CRC-8 . 2927 */ 2928 static void mvneta_set_other_mcast_addr(struct mvneta_port *pp, 2929 unsigned char crc8, 2930 int queue) 2931 { 2932 unsigned int omc_table_reg; 2933 unsigned int tbl_offset; 2934 unsigned int reg_offset; 2935 2936 tbl_offset = (crc8 / 4) * 4; /* Register offset from OMC table base */ 2937 reg_offset = crc8 % 4; /* Entry offset within the above reg */ 2938 2939 omc_table_reg = mvreg_read(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset); 2940 2941 if (queue == -1) { 2942 /* Clear accepts frame bit at specified Other DA table entry */ 2943 omc_table_reg &= ~(0xff << (8 * reg_offset)); 2944 } else { 2945 omc_table_reg &= ~(0xff << (8 * reg_offset)); 2946 omc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset)); 2947 } 2948 2949 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset, omc_table_reg); 2950 } 2951 2952 /* The network device supports multicast using two tables: 2953 * 1) Special Multicast Table for MAC addresses of the form 2954 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF). 2955 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast 2956 * Table entries in the DA-Filter table. 2957 * 2) Other Multicast Table for multicast of another type. A CRC-8 value 2958 * is used as an index to the Other Multicast Table entries in the 2959 * DA-Filter table. 2960 */ 2961 static int mvneta_mcast_addr_set(struct mvneta_port *pp, unsigned char *p_addr, 2962 int queue) 2963 { 2964 unsigned char crc_result = 0; 2965 2966 if (memcmp(p_addr, "\x01\x00\x5e\x00\x00", 5) == 0) { 2967 mvneta_set_special_mcast_addr(pp, p_addr[5], queue); 2968 return 0; 2969 } 2970 2971 crc_result = mvneta_addr_crc(p_addr); 2972 if (queue == -1) { 2973 if (pp->mcast_count[crc_result] == 0) { 2974 netdev_info(pp->dev, "No valid Mcast for crc8=0x%02x\n", 2975 crc_result); 2976 return -EINVAL; 2977 } 2978 2979 pp->mcast_count[crc_result]--; 2980 if (pp->mcast_count[crc_result] != 0) { 2981 netdev_info(pp->dev, 2982 "After delete there are %d valid Mcast for crc8=0x%02x\n", 2983 pp->mcast_count[crc_result], crc_result); 2984 return -EINVAL; 2985 } 2986 } else 2987 pp->mcast_count[crc_result]++; 2988 2989 mvneta_set_other_mcast_addr(pp, crc_result, queue); 2990 2991 return 0; 2992 } 2993 2994 /* Configure Fitering mode of Ethernet port */ 2995 static void mvneta_rx_unicast_promisc_set(struct mvneta_port *pp, 2996 int is_promisc) 2997 { 2998 u32 port_cfg_reg, val; 2999 3000 port_cfg_reg = mvreg_read(pp, MVNETA_PORT_CONFIG); 3001 3002 val = mvreg_read(pp, MVNETA_TYPE_PRIO); 3003 3004 /* Set / Clear UPM bit in port configuration register */ 3005 if (is_promisc) { 3006 /* Accept all Unicast addresses */ 3007 port_cfg_reg |= MVNETA_UNI_PROMISC_MODE; 3008 val |= MVNETA_FORCE_UNI; 3009 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, 0xffff); 3010 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, 0xffffffff); 3011 } else { 3012 /* Reject all Unicast addresses */ 3013 port_cfg_reg &= ~MVNETA_UNI_PROMISC_MODE; 3014 val &= ~MVNETA_FORCE_UNI; 3015 } 3016 3017 mvreg_write(pp, MVNETA_PORT_CONFIG, port_cfg_reg); 3018 mvreg_write(pp, MVNETA_TYPE_PRIO, val); 3019 } 3020 3021 /* register unicast and multicast addresses */ 3022 static void mvneta_set_rx_mode(struct net_device *dev) 3023 { 3024 struct mvneta_port *pp = netdev_priv(dev); 3025 struct netdev_hw_addr *ha; 3026 3027 if (dev->flags & IFF_PROMISC) { 3028 /* Accept all: Multicast + Unicast */ 3029 mvneta_rx_unicast_promisc_set(pp, 1); 3030 mvneta_set_ucast_table(pp, pp->rxq_def); 3031 mvneta_set_special_mcast_table(pp, pp->rxq_def); 3032 mvneta_set_other_mcast_table(pp, pp->rxq_def); 3033 } else { 3034 /* Accept single Unicast */ 3035 mvneta_rx_unicast_promisc_set(pp, 0); 3036 mvneta_set_ucast_table(pp, -1); 3037 mvneta_mac_addr_set(pp, dev->dev_addr, pp->rxq_def); 3038 3039 if (dev->flags & IFF_ALLMULTI) { 3040 /* Accept all multicast */ 3041 mvneta_set_special_mcast_table(pp, pp->rxq_def); 3042 mvneta_set_other_mcast_table(pp, pp->rxq_def); 3043 } else { 3044 /* Accept only initialized multicast */ 3045 mvneta_set_special_mcast_table(pp, -1); 3046 mvneta_set_other_mcast_table(pp, -1); 3047 3048 if (!netdev_mc_empty(dev)) { 3049 netdev_for_each_mc_addr(ha, dev) { 3050 mvneta_mcast_addr_set(pp, ha->addr, 3051 pp->rxq_def); 3052 } 3053 } 3054 } 3055 } 3056 } 3057 3058 /* Interrupt handling - the callback for request_irq() */ 3059 static irqreturn_t mvneta_isr(int irq, void *dev_id) 3060 { 3061 struct mvneta_port *pp = (struct mvneta_port *)dev_id; 3062 3063 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0); 3064 napi_schedule(&pp->napi); 3065 3066 return IRQ_HANDLED; 3067 } 3068 3069 /* Interrupt handling - the callback for request_percpu_irq() */ 3070 static irqreturn_t mvneta_percpu_isr(int irq, void *dev_id) 3071 { 3072 struct mvneta_pcpu_port *port = (struct mvneta_pcpu_port *)dev_id; 3073 3074 disable_percpu_irq(port->pp->dev->irq); 3075 napi_schedule(&port->napi); 3076 3077 return IRQ_HANDLED; 3078 } 3079 3080 static void mvneta_link_change(struct mvneta_port *pp) 3081 { 3082 u32 gmac_stat = mvreg_read(pp, MVNETA_GMAC_STATUS); 3083 3084 phylink_mac_change(pp->phylink, !!(gmac_stat & MVNETA_GMAC_LINK_UP)); 3085 } 3086 3087 /* NAPI handler 3088 * Bits 0 - 7 of the causeRxTx register indicate that are transmitted 3089 * packets on the corresponding TXQ (Bit 0 is for TX queue 1). 3090 * Bits 8 -15 of the cause Rx Tx register indicate that are received 3091 * packets on the corresponding RXQ (Bit 8 is for RX queue 0). 3092 * Each CPU has its own causeRxTx register 3093 */ 3094 static int mvneta_poll(struct napi_struct *napi, int budget) 3095 { 3096 int rx_done = 0; 3097 u32 cause_rx_tx; 3098 int rx_queue; 3099 struct mvneta_port *pp = netdev_priv(napi->dev); 3100 struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports); 3101 3102 if (!netif_running(pp->dev)) { 3103 napi_complete(napi); 3104 return rx_done; 3105 } 3106 3107 /* Read cause register */ 3108 cause_rx_tx = mvreg_read(pp, MVNETA_INTR_NEW_CAUSE); 3109 if (cause_rx_tx & MVNETA_MISCINTR_INTR_MASK) { 3110 u32 cause_misc = mvreg_read(pp, MVNETA_INTR_MISC_CAUSE); 3111 3112 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0); 3113 3114 if (cause_misc & (MVNETA_CAUSE_PHY_STATUS_CHANGE | 3115 MVNETA_CAUSE_LINK_CHANGE)) 3116 mvneta_link_change(pp); 3117 } 3118 3119 /* Release Tx descriptors */ 3120 if (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL) { 3121 mvneta_tx_done_gbe(pp, (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL)); 3122 cause_rx_tx &= ~MVNETA_TX_INTR_MASK_ALL; 3123 } 3124 3125 /* For the case where the last mvneta_poll did not process all 3126 * RX packets 3127 */ 3128 cause_rx_tx |= pp->neta_armada3700 ? pp->cause_rx_tx : 3129 port->cause_rx_tx; 3130 3131 rx_queue = fls(((cause_rx_tx >> 8) & 0xff)); 3132 if (rx_queue) { 3133 rx_queue = rx_queue - 1; 3134 if (pp->bm_priv) 3135 rx_done = mvneta_rx_hwbm(napi, pp, budget, 3136 &pp->rxqs[rx_queue]); 3137 else 3138 rx_done = mvneta_rx_swbm(napi, pp, budget, 3139 &pp->rxqs[rx_queue]); 3140 } 3141 3142 if (rx_done < budget) { 3143 cause_rx_tx = 0; 3144 napi_complete_done(napi, rx_done); 3145 3146 if (pp->neta_armada3700) { 3147 unsigned long flags; 3148 3149 local_irq_save(flags); 3150 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 3151 MVNETA_RX_INTR_MASK(rxq_number) | 3152 MVNETA_TX_INTR_MASK(txq_number) | 3153 MVNETA_MISCINTR_INTR_MASK); 3154 local_irq_restore(flags); 3155 } else { 3156 enable_percpu_irq(pp->dev->irq, 0); 3157 } 3158 } 3159 3160 if (pp->neta_armada3700) 3161 pp->cause_rx_tx = cause_rx_tx; 3162 else 3163 port->cause_rx_tx = cause_rx_tx; 3164 3165 return rx_done; 3166 } 3167 3168 static int mvneta_create_page_pool(struct mvneta_port *pp, 3169 struct mvneta_rx_queue *rxq, int size) 3170 { 3171 struct bpf_prog *xdp_prog = READ_ONCE(pp->xdp_prog); 3172 struct page_pool_params pp_params = { 3173 .order = 0, 3174 .flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV, 3175 .pool_size = size, 3176 .nid = NUMA_NO_NODE, 3177 .dev = pp->dev->dev.parent, 3178 .dma_dir = xdp_prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE, 3179 .offset = pp->rx_offset_correction, 3180 .max_len = MVNETA_MAX_RX_BUF_SIZE, 3181 }; 3182 int err; 3183 3184 rxq->page_pool = page_pool_create(&pp_params); 3185 if (IS_ERR(rxq->page_pool)) { 3186 err = PTR_ERR(rxq->page_pool); 3187 rxq->page_pool = NULL; 3188 return err; 3189 } 3190 3191 err = xdp_rxq_info_reg(&rxq->xdp_rxq, pp->dev, rxq->id); 3192 if (err < 0) 3193 goto err_free_pp; 3194 3195 err = xdp_rxq_info_reg_mem_model(&rxq->xdp_rxq, MEM_TYPE_PAGE_POOL, 3196 rxq->page_pool); 3197 if (err) 3198 goto err_unregister_rxq; 3199 3200 return 0; 3201 3202 err_unregister_rxq: 3203 xdp_rxq_info_unreg(&rxq->xdp_rxq); 3204 err_free_pp: 3205 page_pool_destroy(rxq->page_pool); 3206 rxq->page_pool = NULL; 3207 return err; 3208 } 3209 3210 /* Handle rxq fill: allocates rxq skbs; called when initializing a port */ 3211 static int mvneta_rxq_fill(struct mvneta_port *pp, struct mvneta_rx_queue *rxq, 3212 int num) 3213 { 3214 int i, err; 3215 3216 err = mvneta_create_page_pool(pp, rxq, num); 3217 if (err < 0) 3218 return err; 3219 3220 for (i = 0; i < num; i++) { 3221 memset(rxq->descs + i, 0, sizeof(struct mvneta_rx_desc)); 3222 if (mvneta_rx_refill(pp, rxq->descs + i, rxq, 3223 GFP_KERNEL) != 0) { 3224 netdev_err(pp->dev, 3225 "%s:rxq %d, %d of %d buffs filled\n", 3226 __func__, rxq->id, i, num); 3227 break; 3228 } 3229 } 3230 3231 /* Add this number of RX descriptors as non occupied (ready to 3232 * get packets) 3233 */ 3234 mvneta_rxq_non_occup_desc_add(pp, rxq, i); 3235 3236 return i; 3237 } 3238 3239 /* Free all packets pending transmit from all TXQs and reset TX port */ 3240 static void mvneta_tx_reset(struct mvneta_port *pp) 3241 { 3242 int queue; 3243 3244 /* free the skb's in the tx ring */ 3245 for (queue = 0; queue < txq_number; queue++) 3246 mvneta_txq_done_force(pp, &pp->txqs[queue]); 3247 3248 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET); 3249 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0); 3250 } 3251 3252 static void mvneta_rx_reset(struct mvneta_port *pp) 3253 { 3254 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET); 3255 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0); 3256 } 3257 3258 /* Rx/Tx queue initialization/cleanup methods */ 3259 3260 static int mvneta_rxq_sw_init(struct mvneta_port *pp, 3261 struct mvneta_rx_queue *rxq) 3262 { 3263 rxq->size = pp->rx_ring_size; 3264 3265 /* Allocate memory for RX descriptors */ 3266 rxq->descs = dma_alloc_coherent(pp->dev->dev.parent, 3267 rxq->size * MVNETA_DESC_ALIGNED_SIZE, 3268 &rxq->descs_phys, GFP_KERNEL); 3269 if (!rxq->descs) 3270 return -ENOMEM; 3271 3272 rxq->last_desc = rxq->size - 1; 3273 3274 return 0; 3275 } 3276 3277 static void mvneta_rxq_hw_init(struct mvneta_port *pp, 3278 struct mvneta_rx_queue *rxq) 3279 { 3280 /* Set Rx descriptors queue starting address */ 3281 mvreg_write(pp, MVNETA_RXQ_BASE_ADDR_REG(rxq->id), rxq->descs_phys); 3282 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), rxq->size); 3283 3284 /* Set coalescing pkts and time */ 3285 mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal); 3286 mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal); 3287 3288 if (!pp->bm_priv) { 3289 /* Set Offset */ 3290 mvneta_rxq_offset_set(pp, rxq, 0); 3291 mvneta_rxq_buf_size_set(pp, rxq, PAGE_SIZE < SZ_64K ? 3292 MVNETA_MAX_RX_BUF_SIZE : 3293 MVNETA_RX_BUF_SIZE(pp->pkt_size)); 3294 mvneta_rxq_bm_disable(pp, rxq); 3295 mvneta_rxq_fill(pp, rxq, rxq->size); 3296 } else { 3297 /* Set Offset */ 3298 mvneta_rxq_offset_set(pp, rxq, 3299 NET_SKB_PAD - pp->rx_offset_correction); 3300 3301 mvneta_rxq_bm_enable(pp, rxq); 3302 /* Fill RXQ with buffers from RX pool */ 3303 mvneta_rxq_long_pool_set(pp, rxq); 3304 mvneta_rxq_short_pool_set(pp, rxq); 3305 mvneta_rxq_non_occup_desc_add(pp, rxq, rxq->size); 3306 } 3307 } 3308 3309 /* Create a specified RX queue */ 3310 static int mvneta_rxq_init(struct mvneta_port *pp, 3311 struct mvneta_rx_queue *rxq) 3312 3313 { 3314 int ret; 3315 3316 ret = mvneta_rxq_sw_init(pp, rxq); 3317 if (ret < 0) 3318 return ret; 3319 3320 mvneta_rxq_hw_init(pp, rxq); 3321 3322 return 0; 3323 } 3324 3325 /* Cleanup Rx queue */ 3326 static void mvneta_rxq_deinit(struct mvneta_port *pp, 3327 struct mvneta_rx_queue *rxq) 3328 { 3329 mvneta_rxq_drop_pkts(pp, rxq); 3330 3331 if (rxq->skb) 3332 dev_kfree_skb_any(rxq->skb); 3333 3334 if (rxq->descs) 3335 dma_free_coherent(pp->dev->dev.parent, 3336 rxq->size * MVNETA_DESC_ALIGNED_SIZE, 3337 rxq->descs, 3338 rxq->descs_phys); 3339 3340 rxq->descs = NULL; 3341 rxq->last_desc = 0; 3342 rxq->next_desc_to_proc = 0; 3343 rxq->descs_phys = 0; 3344 rxq->first_to_refill = 0; 3345 rxq->refill_num = 0; 3346 rxq->skb = NULL; 3347 rxq->left_size = 0; 3348 } 3349 3350 static int mvneta_txq_sw_init(struct mvneta_port *pp, 3351 struct mvneta_tx_queue *txq) 3352 { 3353 int cpu; 3354 3355 txq->size = pp->tx_ring_size; 3356 3357 /* A queue must always have room for at least one skb. 3358 * Therefore, stop the queue when the free entries reaches 3359 * the maximum number of descriptors per skb. 3360 */ 3361 txq->tx_stop_threshold = txq->size - MVNETA_MAX_SKB_DESCS; 3362 txq->tx_wake_threshold = txq->tx_stop_threshold / 2; 3363 3364 /* Allocate memory for TX descriptors */ 3365 txq->descs = dma_alloc_coherent(pp->dev->dev.parent, 3366 txq->size * MVNETA_DESC_ALIGNED_SIZE, 3367 &txq->descs_phys, GFP_KERNEL); 3368 if (!txq->descs) 3369 return -ENOMEM; 3370 3371 txq->last_desc = txq->size - 1; 3372 3373 txq->buf = kmalloc_array(txq->size, sizeof(*txq->buf), GFP_KERNEL); 3374 if (!txq->buf) { 3375 dma_free_coherent(pp->dev->dev.parent, 3376 txq->size * MVNETA_DESC_ALIGNED_SIZE, 3377 txq->descs, txq->descs_phys); 3378 return -ENOMEM; 3379 } 3380 3381 /* Allocate DMA buffers for TSO MAC/IP/TCP headers */ 3382 txq->tso_hdrs = dma_alloc_coherent(pp->dev->dev.parent, 3383 txq->size * TSO_HEADER_SIZE, 3384 &txq->tso_hdrs_phys, GFP_KERNEL); 3385 if (!txq->tso_hdrs) { 3386 kfree(txq->buf); 3387 dma_free_coherent(pp->dev->dev.parent, 3388 txq->size * MVNETA_DESC_ALIGNED_SIZE, 3389 txq->descs, txq->descs_phys); 3390 return -ENOMEM; 3391 } 3392 3393 /* Setup XPS mapping */ 3394 if (txq_number > 1) 3395 cpu = txq->id % num_present_cpus(); 3396 else 3397 cpu = pp->rxq_def % num_present_cpus(); 3398 cpumask_set_cpu(cpu, &txq->affinity_mask); 3399 netif_set_xps_queue(pp->dev, &txq->affinity_mask, txq->id); 3400 3401 return 0; 3402 } 3403 3404 static void mvneta_txq_hw_init(struct mvneta_port *pp, 3405 struct mvneta_tx_queue *txq) 3406 { 3407 /* Set maximum bandwidth for enabled TXQs */ 3408 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0x03ffffff); 3409 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0x3fffffff); 3410 3411 /* Set Tx descriptors queue starting address */ 3412 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), txq->descs_phys); 3413 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), txq->size); 3414 3415 mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal); 3416 } 3417 3418 /* Create and initialize a tx queue */ 3419 static int mvneta_txq_init(struct mvneta_port *pp, 3420 struct mvneta_tx_queue *txq) 3421 { 3422 int ret; 3423 3424 ret = mvneta_txq_sw_init(pp, txq); 3425 if (ret < 0) 3426 return ret; 3427 3428 mvneta_txq_hw_init(pp, txq); 3429 3430 return 0; 3431 } 3432 3433 /* Free allocated resources when mvneta_txq_init() fails to allocate memory*/ 3434 static void mvneta_txq_sw_deinit(struct mvneta_port *pp, 3435 struct mvneta_tx_queue *txq) 3436 { 3437 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id); 3438 3439 kfree(txq->buf); 3440 3441 if (txq->tso_hdrs) 3442 dma_free_coherent(pp->dev->dev.parent, 3443 txq->size * TSO_HEADER_SIZE, 3444 txq->tso_hdrs, txq->tso_hdrs_phys); 3445 if (txq->descs) 3446 dma_free_coherent(pp->dev->dev.parent, 3447 txq->size * MVNETA_DESC_ALIGNED_SIZE, 3448 txq->descs, txq->descs_phys); 3449 3450 netdev_tx_reset_queue(nq); 3451 3452 txq->descs = NULL; 3453 txq->last_desc = 0; 3454 txq->next_desc_to_proc = 0; 3455 txq->descs_phys = 0; 3456 } 3457 3458 static void mvneta_txq_hw_deinit(struct mvneta_port *pp, 3459 struct mvneta_tx_queue *txq) 3460 { 3461 /* Set minimum bandwidth for disabled TXQs */ 3462 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0); 3463 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0); 3464 3465 /* Set Tx descriptors queue starting address and size */ 3466 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), 0); 3467 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), 0); 3468 } 3469 3470 static void mvneta_txq_deinit(struct mvneta_port *pp, 3471 struct mvneta_tx_queue *txq) 3472 { 3473 mvneta_txq_sw_deinit(pp, txq); 3474 mvneta_txq_hw_deinit(pp, txq); 3475 } 3476 3477 /* Cleanup all Tx queues */ 3478 static void mvneta_cleanup_txqs(struct mvneta_port *pp) 3479 { 3480 int queue; 3481 3482 for (queue = 0; queue < txq_number; queue++) 3483 mvneta_txq_deinit(pp, &pp->txqs[queue]); 3484 } 3485 3486 /* Cleanup all Rx queues */ 3487 static void mvneta_cleanup_rxqs(struct mvneta_port *pp) 3488 { 3489 int queue; 3490 3491 for (queue = 0; queue < rxq_number; queue++) 3492 mvneta_rxq_deinit(pp, &pp->rxqs[queue]); 3493 } 3494 3495 3496 /* Init all Rx queues */ 3497 static int mvneta_setup_rxqs(struct mvneta_port *pp) 3498 { 3499 int queue; 3500 3501 for (queue = 0; queue < rxq_number; queue++) { 3502 int err = mvneta_rxq_init(pp, &pp->rxqs[queue]); 3503 3504 if (err) { 3505 netdev_err(pp->dev, "%s: can't create rxq=%d\n", 3506 __func__, queue); 3507 mvneta_cleanup_rxqs(pp); 3508 return err; 3509 } 3510 } 3511 3512 return 0; 3513 } 3514 3515 /* Init all tx queues */ 3516 static int mvneta_setup_txqs(struct mvneta_port *pp) 3517 { 3518 int queue; 3519 3520 for (queue = 0; queue < txq_number; queue++) { 3521 int err = mvneta_txq_init(pp, &pp->txqs[queue]); 3522 if (err) { 3523 netdev_err(pp->dev, "%s: can't create txq=%d\n", 3524 __func__, queue); 3525 mvneta_cleanup_txqs(pp); 3526 return err; 3527 } 3528 } 3529 3530 return 0; 3531 } 3532 3533 static int mvneta_comphy_init(struct mvneta_port *pp, phy_interface_t interface) 3534 { 3535 int ret; 3536 3537 ret = phy_set_mode_ext(pp->comphy, PHY_MODE_ETHERNET, interface); 3538 if (ret) 3539 return ret; 3540 3541 return phy_power_on(pp->comphy); 3542 } 3543 3544 static int mvneta_config_interface(struct mvneta_port *pp, 3545 phy_interface_t interface) 3546 { 3547 int ret = 0; 3548 3549 if (pp->comphy) { 3550 if (interface == PHY_INTERFACE_MODE_SGMII || 3551 interface == PHY_INTERFACE_MODE_1000BASEX || 3552 interface == PHY_INTERFACE_MODE_2500BASEX) { 3553 ret = mvneta_comphy_init(pp, interface); 3554 } 3555 } else { 3556 switch (interface) { 3557 case PHY_INTERFACE_MODE_QSGMII: 3558 mvreg_write(pp, MVNETA_SERDES_CFG, 3559 MVNETA_QSGMII_SERDES_PROTO); 3560 break; 3561 3562 case PHY_INTERFACE_MODE_SGMII: 3563 case PHY_INTERFACE_MODE_1000BASEX: 3564 mvreg_write(pp, MVNETA_SERDES_CFG, 3565 MVNETA_SGMII_SERDES_PROTO); 3566 break; 3567 3568 case PHY_INTERFACE_MODE_2500BASEX: 3569 mvreg_write(pp, MVNETA_SERDES_CFG, 3570 MVNETA_HSGMII_SERDES_PROTO); 3571 break; 3572 default: 3573 break; 3574 } 3575 } 3576 3577 pp->phy_interface = interface; 3578 3579 return ret; 3580 } 3581 3582 static void mvneta_start_dev(struct mvneta_port *pp) 3583 { 3584 int cpu; 3585 3586 WARN_ON(mvneta_config_interface(pp, pp->phy_interface)); 3587 3588 mvneta_max_rx_size_set(pp, pp->pkt_size); 3589 mvneta_txq_max_tx_size_set(pp, pp->pkt_size); 3590 3591 /* start the Rx/Tx activity */ 3592 mvneta_port_enable(pp); 3593 3594 if (!pp->neta_armada3700) { 3595 /* Enable polling on the port */ 3596 for_each_online_cpu(cpu) { 3597 struct mvneta_pcpu_port *port = 3598 per_cpu_ptr(pp->ports, cpu); 3599 3600 napi_enable(&port->napi); 3601 } 3602 } else { 3603 napi_enable(&pp->napi); 3604 } 3605 3606 /* Unmask interrupts. It has to be done from each CPU */ 3607 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true); 3608 3609 mvreg_write(pp, MVNETA_INTR_MISC_MASK, 3610 MVNETA_CAUSE_PHY_STATUS_CHANGE | 3611 MVNETA_CAUSE_LINK_CHANGE); 3612 3613 phylink_start(pp->phylink); 3614 3615 /* We may have called phy_speed_down before */ 3616 phylink_speed_up(pp->phylink); 3617 3618 netif_tx_start_all_queues(pp->dev); 3619 3620 clear_bit(__MVNETA_DOWN, &pp->state); 3621 } 3622 3623 static void mvneta_stop_dev(struct mvneta_port *pp) 3624 { 3625 unsigned int cpu; 3626 3627 set_bit(__MVNETA_DOWN, &pp->state); 3628 3629 if (device_may_wakeup(&pp->dev->dev)) 3630 phylink_speed_down(pp->phylink, false); 3631 3632 phylink_stop(pp->phylink); 3633 3634 if (!pp->neta_armada3700) { 3635 for_each_online_cpu(cpu) { 3636 struct mvneta_pcpu_port *port = 3637 per_cpu_ptr(pp->ports, cpu); 3638 3639 napi_disable(&port->napi); 3640 } 3641 } else { 3642 napi_disable(&pp->napi); 3643 } 3644 3645 netif_carrier_off(pp->dev); 3646 3647 mvneta_port_down(pp); 3648 netif_tx_stop_all_queues(pp->dev); 3649 3650 /* Stop the port activity */ 3651 mvneta_port_disable(pp); 3652 3653 /* Clear all ethernet port interrupts */ 3654 on_each_cpu(mvneta_percpu_clear_intr_cause, pp, true); 3655 3656 /* Mask all ethernet port interrupts */ 3657 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true); 3658 3659 mvneta_tx_reset(pp); 3660 mvneta_rx_reset(pp); 3661 3662 WARN_ON(phy_power_off(pp->comphy)); 3663 } 3664 3665 static void mvneta_percpu_enable(void *arg) 3666 { 3667 struct mvneta_port *pp = arg; 3668 3669 enable_percpu_irq(pp->dev->irq, IRQ_TYPE_NONE); 3670 } 3671 3672 static void mvneta_percpu_disable(void *arg) 3673 { 3674 struct mvneta_port *pp = arg; 3675 3676 disable_percpu_irq(pp->dev->irq); 3677 } 3678 3679 /* Change the device mtu */ 3680 static int mvneta_change_mtu(struct net_device *dev, int mtu) 3681 { 3682 struct mvneta_port *pp = netdev_priv(dev); 3683 int ret; 3684 3685 if (!IS_ALIGNED(MVNETA_RX_PKT_SIZE(mtu), 8)) { 3686 netdev_info(dev, "Illegal MTU value %d, rounding to %d\n", 3687 mtu, ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8)); 3688 mtu = ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8); 3689 } 3690 3691 if (pp->xdp_prog && mtu > MVNETA_MAX_RX_BUF_SIZE) { 3692 netdev_info(dev, "Illegal MTU value %d for XDP mode\n", mtu); 3693 return -EINVAL; 3694 } 3695 3696 dev->mtu = mtu; 3697 3698 if (!netif_running(dev)) { 3699 if (pp->bm_priv) 3700 mvneta_bm_update_mtu(pp, mtu); 3701 3702 netdev_update_features(dev); 3703 return 0; 3704 } 3705 3706 /* The interface is running, so we have to force a 3707 * reallocation of the queues 3708 */ 3709 mvneta_stop_dev(pp); 3710 on_each_cpu(mvneta_percpu_disable, pp, true); 3711 3712 mvneta_cleanup_txqs(pp); 3713 mvneta_cleanup_rxqs(pp); 3714 3715 if (pp->bm_priv) 3716 mvneta_bm_update_mtu(pp, mtu); 3717 3718 pp->pkt_size = MVNETA_RX_PKT_SIZE(dev->mtu); 3719 3720 ret = mvneta_setup_rxqs(pp); 3721 if (ret) { 3722 netdev_err(dev, "unable to setup rxqs after MTU change\n"); 3723 return ret; 3724 } 3725 3726 ret = mvneta_setup_txqs(pp); 3727 if (ret) { 3728 netdev_err(dev, "unable to setup txqs after MTU change\n"); 3729 return ret; 3730 } 3731 3732 on_each_cpu(mvneta_percpu_enable, pp, true); 3733 mvneta_start_dev(pp); 3734 3735 netdev_update_features(dev); 3736 3737 return 0; 3738 } 3739 3740 static netdev_features_t mvneta_fix_features(struct net_device *dev, 3741 netdev_features_t features) 3742 { 3743 struct mvneta_port *pp = netdev_priv(dev); 3744 3745 if (pp->tx_csum_limit && dev->mtu > pp->tx_csum_limit) { 3746 features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO); 3747 netdev_info(dev, 3748 "Disable IP checksum for MTU greater than %dB\n", 3749 pp->tx_csum_limit); 3750 } 3751 3752 return features; 3753 } 3754 3755 /* Get mac address */ 3756 static void mvneta_get_mac_addr(struct mvneta_port *pp, unsigned char *addr) 3757 { 3758 u32 mac_addr_l, mac_addr_h; 3759 3760 mac_addr_l = mvreg_read(pp, MVNETA_MAC_ADDR_LOW); 3761 mac_addr_h = mvreg_read(pp, MVNETA_MAC_ADDR_HIGH); 3762 addr[0] = (mac_addr_h >> 24) & 0xFF; 3763 addr[1] = (mac_addr_h >> 16) & 0xFF; 3764 addr[2] = (mac_addr_h >> 8) & 0xFF; 3765 addr[3] = mac_addr_h & 0xFF; 3766 addr[4] = (mac_addr_l >> 8) & 0xFF; 3767 addr[5] = mac_addr_l & 0xFF; 3768 } 3769 3770 /* Handle setting mac address */ 3771 static int mvneta_set_mac_addr(struct net_device *dev, void *addr) 3772 { 3773 struct mvneta_port *pp = netdev_priv(dev); 3774 struct sockaddr *sockaddr = addr; 3775 int ret; 3776 3777 ret = eth_prepare_mac_addr_change(dev, addr); 3778 if (ret < 0) 3779 return ret; 3780 /* Remove previous address table entry */ 3781 mvneta_mac_addr_set(pp, dev->dev_addr, -1); 3782 3783 /* Set new addr in hw */ 3784 mvneta_mac_addr_set(pp, sockaddr->sa_data, pp->rxq_def); 3785 3786 eth_commit_mac_addr_change(dev, addr); 3787 return 0; 3788 } 3789 3790 static void mvneta_validate(struct phylink_config *config, 3791 unsigned long *supported, 3792 struct phylink_link_state *state) 3793 { 3794 struct net_device *ndev = to_net_dev(config->dev); 3795 struct mvneta_port *pp = netdev_priv(ndev); 3796 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, }; 3797 3798 /* We only support QSGMII, SGMII, 802.3z and RGMII modes */ 3799 if (state->interface != PHY_INTERFACE_MODE_NA && 3800 state->interface != PHY_INTERFACE_MODE_QSGMII && 3801 state->interface != PHY_INTERFACE_MODE_SGMII && 3802 !phy_interface_mode_is_8023z(state->interface) && 3803 !phy_interface_mode_is_rgmii(state->interface)) { 3804 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS); 3805 return; 3806 } 3807 3808 /* Allow all the expected bits */ 3809 phylink_set(mask, Autoneg); 3810 phylink_set_port_modes(mask); 3811 3812 /* Asymmetric pause is unsupported */ 3813 phylink_set(mask, Pause); 3814 3815 /* Half-duplex at speeds higher than 100Mbit is unsupported */ 3816 if (pp->comphy || state->interface != PHY_INTERFACE_MODE_2500BASEX) { 3817 phylink_set(mask, 1000baseT_Full); 3818 phylink_set(mask, 1000baseX_Full); 3819 } 3820 if (pp->comphy || state->interface == PHY_INTERFACE_MODE_2500BASEX) { 3821 phylink_set(mask, 2500baseT_Full); 3822 phylink_set(mask, 2500baseX_Full); 3823 } 3824 3825 if (!phy_interface_mode_is_8023z(state->interface)) { 3826 /* 10M and 100M are only supported in non-802.3z mode */ 3827 phylink_set(mask, 10baseT_Half); 3828 phylink_set(mask, 10baseT_Full); 3829 phylink_set(mask, 100baseT_Half); 3830 phylink_set(mask, 100baseT_Full); 3831 } 3832 3833 bitmap_and(supported, supported, mask, 3834 __ETHTOOL_LINK_MODE_MASK_NBITS); 3835 bitmap_and(state->advertising, state->advertising, mask, 3836 __ETHTOOL_LINK_MODE_MASK_NBITS); 3837 3838 /* We can only operate at 2500BaseX or 1000BaseX. If requested 3839 * to advertise both, only report advertising at 2500BaseX. 3840 */ 3841 phylink_helper_basex_speed(state); 3842 } 3843 3844 static void mvneta_mac_pcs_get_state(struct phylink_config *config, 3845 struct phylink_link_state *state) 3846 { 3847 struct net_device *ndev = to_net_dev(config->dev); 3848 struct mvneta_port *pp = netdev_priv(ndev); 3849 u32 gmac_stat; 3850 3851 gmac_stat = mvreg_read(pp, MVNETA_GMAC_STATUS); 3852 3853 if (gmac_stat & MVNETA_GMAC_SPEED_1000) 3854 state->speed = 3855 state->interface == PHY_INTERFACE_MODE_2500BASEX ? 3856 SPEED_2500 : SPEED_1000; 3857 else if (gmac_stat & MVNETA_GMAC_SPEED_100) 3858 state->speed = SPEED_100; 3859 else 3860 state->speed = SPEED_10; 3861 3862 state->an_complete = !!(gmac_stat & MVNETA_GMAC_AN_COMPLETE); 3863 state->link = !!(gmac_stat & MVNETA_GMAC_LINK_UP); 3864 state->duplex = !!(gmac_stat & MVNETA_GMAC_FULL_DUPLEX); 3865 3866 state->pause = 0; 3867 if (gmac_stat & MVNETA_GMAC_RX_FLOW_CTRL_ENABLE) 3868 state->pause |= MLO_PAUSE_RX; 3869 if (gmac_stat & MVNETA_GMAC_TX_FLOW_CTRL_ENABLE) 3870 state->pause |= MLO_PAUSE_TX; 3871 } 3872 3873 static void mvneta_mac_an_restart(struct phylink_config *config) 3874 { 3875 struct net_device *ndev = to_net_dev(config->dev); 3876 struct mvneta_port *pp = netdev_priv(ndev); 3877 u32 gmac_an = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG); 3878 3879 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, 3880 gmac_an | MVNETA_GMAC_INBAND_RESTART_AN); 3881 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, 3882 gmac_an & ~MVNETA_GMAC_INBAND_RESTART_AN); 3883 } 3884 3885 static void mvneta_mac_config(struct phylink_config *config, unsigned int mode, 3886 const struct phylink_link_state *state) 3887 { 3888 struct net_device *ndev = to_net_dev(config->dev); 3889 struct mvneta_port *pp = netdev_priv(ndev); 3890 u32 new_ctrl0, gmac_ctrl0 = mvreg_read(pp, MVNETA_GMAC_CTRL_0); 3891 u32 new_ctrl2, gmac_ctrl2 = mvreg_read(pp, MVNETA_GMAC_CTRL_2); 3892 u32 new_ctrl4, gmac_ctrl4 = mvreg_read(pp, MVNETA_GMAC_CTRL_4); 3893 u32 new_clk, gmac_clk = mvreg_read(pp, MVNETA_GMAC_CLOCK_DIVIDER); 3894 u32 new_an, gmac_an = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG); 3895 3896 new_ctrl0 = gmac_ctrl0 & ~MVNETA_GMAC0_PORT_1000BASE_X; 3897 new_ctrl2 = gmac_ctrl2 & ~(MVNETA_GMAC2_INBAND_AN_ENABLE | 3898 MVNETA_GMAC2_PORT_RESET); 3899 new_ctrl4 = gmac_ctrl4 & ~(MVNETA_GMAC4_SHORT_PREAMBLE_ENABLE); 3900 new_clk = gmac_clk & ~MVNETA_GMAC_1MS_CLOCK_ENABLE; 3901 new_an = gmac_an & ~(MVNETA_GMAC_INBAND_AN_ENABLE | 3902 MVNETA_GMAC_INBAND_RESTART_AN | 3903 MVNETA_GMAC_AN_SPEED_EN | 3904 MVNETA_GMAC_ADVERT_SYM_FLOW_CTRL | 3905 MVNETA_GMAC_AN_FLOW_CTRL_EN | 3906 MVNETA_GMAC_AN_DUPLEX_EN); 3907 3908 /* Even though it might look weird, when we're configured in 3909 * SGMII or QSGMII mode, the RGMII bit needs to be set. 3910 */ 3911 new_ctrl2 |= MVNETA_GMAC2_PORT_RGMII; 3912 3913 if (state->interface == PHY_INTERFACE_MODE_QSGMII || 3914 state->interface == PHY_INTERFACE_MODE_SGMII || 3915 phy_interface_mode_is_8023z(state->interface)) 3916 new_ctrl2 |= MVNETA_GMAC2_PCS_ENABLE; 3917 3918 if (phylink_test(state->advertising, Pause)) 3919 new_an |= MVNETA_GMAC_ADVERT_SYM_FLOW_CTRL; 3920 3921 if (!phylink_autoneg_inband(mode)) { 3922 /* Phy or fixed speed - nothing to do, leave the 3923 * configured speed, duplex and flow control as-is. 3924 */ 3925 } else if (state->interface == PHY_INTERFACE_MODE_SGMII) { 3926 /* SGMII mode receives the state from the PHY */ 3927 new_ctrl2 |= MVNETA_GMAC2_INBAND_AN_ENABLE; 3928 new_clk |= MVNETA_GMAC_1MS_CLOCK_ENABLE; 3929 new_an = (new_an & ~(MVNETA_GMAC_FORCE_LINK_DOWN | 3930 MVNETA_GMAC_FORCE_LINK_PASS | 3931 MVNETA_GMAC_CONFIG_MII_SPEED | 3932 MVNETA_GMAC_CONFIG_GMII_SPEED | 3933 MVNETA_GMAC_CONFIG_FULL_DUPLEX)) | 3934 MVNETA_GMAC_INBAND_AN_ENABLE | 3935 MVNETA_GMAC_AN_SPEED_EN | 3936 MVNETA_GMAC_AN_DUPLEX_EN; 3937 } else { 3938 /* 802.3z negotiation - only 1000base-X */ 3939 new_ctrl0 |= MVNETA_GMAC0_PORT_1000BASE_X; 3940 new_clk |= MVNETA_GMAC_1MS_CLOCK_ENABLE; 3941 new_an = (new_an & ~(MVNETA_GMAC_FORCE_LINK_DOWN | 3942 MVNETA_GMAC_FORCE_LINK_PASS | 3943 MVNETA_GMAC_CONFIG_MII_SPEED)) | 3944 MVNETA_GMAC_INBAND_AN_ENABLE | 3945 MVNETA_GMAC_CONFIG_GMII_SPEED | 3946 /* The MAC only supports FD mode */ 3947 MVNETA_GMAC_CONFIG_FULL_DUPLEX; 3948 3949 if (state->pause & MLO_PAUSE_AN && state->an_enabled) 3950 new_an |= MVNETA_GMAC_AN_FLOW_CTRL_EN; 3951 } 3952 3953 /* Armada 370 documentation says we can only change the port mode 3954 * and in-band enable when the link is down, so force it down 3955 * while making these changes. We also do this for GMAC_CTRL2 */ 3956 if ((new_ctrl0 ^ gmac_ctrl0) & MVNETA_GMAC0_PORT_1000BASE_X || 3957 (new_ctrl2 ^ gmac_ctrl2) & MVNETA_GMAC2_INBAND_AN_ENABLE || 3958 (new_an ^ gmac_an) & MVNETA_GMAC_INBAND_AN_ENABLE) { 3959 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, 3960 (gmac_an & ~MVNETA_GMAC_FORCE_LINK_PASS) | 3961 MVNETA_GMAC_FORCE_LINK_DOWN); 3962 } 3963 3964 3965 /* When at 2.5G, the link partner can send frames with shortened 3966 * preambles. 3967 */ 3968 if (state->interface == PHY_INTERFACE_MODE_2500BASEX) 3969 new_ctrl4 |= MVNETA_GMAC4_SHORT_PREAMBLE_ENABLE; 3970 3971 if (pp->phy_interface != state->interface) { 3972 if (pp->comphy) 3973 WARN_ON(phy_power_off(pp->comphy)); 3974 WARN_ON(mvneta_config_interface(pp, state->interface)); 3975 } 3976 3977 if (new_ctrl0 != gmac_ctrl0) 3978 mvreg_write(pp, MVNETA_GMAC_CTRL_0, new_ctrl0); 3979 if (new_ctrl2 != gmac_ctrl2) 3980 mvreg_write(pp, MVNETA_GMAC_CTRL_2, new_ctrl2); 3981 if (new_ctrl4 != gmac_ctrl4) 3982 mvreg_write(pp, MVNETA_GMAC_CTRL_4, new_ctrl4); 3983 if (new_clk != gmac_clk) 3984 mvreg_write(pp, MVNETA_GMAC_CLOCK_DIVIDER, new_clk); 3985 if (new_an != gmac_an) 3986 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, new_an); 3987 3988 if (gmac_ctrl2 & MVNETA_GMAC2_PORT_RESET) { 3989 while ((mvreg_read(pp, MVNETA_GMAC_CTRL_2) & 3990 MVNETA_GMAC2_PORT_RESET) != 0) 3991 continue; 3992 } 3993 } 3994 3995 static void mvneta_set_eee(struct mvneta_port *pp, bool enable) 3996 { 3997 u32 lpi_ctl1; 3998 3999 lpi_ctl1 = mvreg_read(pp, MVNETA_LPI_CTRL_1); 4000 if (enable) 4001 lpi_ctl1 |= MVNETA_LPI_REQUEST_ENABLE; 4002 else 4003 lpi_ctl1 &= ~MVNETA_LPI_REQUEST_ENABLE; 4004 mvreg_write(pp, MVNETA_LPI_CTRL_1, lpi_ctl1); 4005 } 4006 4007 static void mvneta_mac_link_down(struct phylink_config *config, 4008 unsigned int mode, phy_interface_t interface) 4009 { 4010 struct net_device *ndev = to_net_dev(config->dev); 4011 struct mvneta_port *pp = netdev_priv(ndev); 4012 u32 val; 4013 4014 mvneta_port_down(pp); 4015 4016 if (!phylink_autoneg_inband(mode)) { 4017 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG); 4018 val &= ~MVNETA_GMAC_FORCE_LINK_PASS; 4019 val |= MVNETA_GMAC_FORCE_LINK_DOWN; 4020 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val); 4021 } 4022 4023 pp->eee_active = false; 4024 mvneta_set_eee(pp, false); 4025 } 4026 4027 static void mvneta_mac_link_up(struct phylink_config *config, 4028 struct phy_device *phy, 4029 unsigned int mode, phy_interface_t interface, 4030 int speed, int duplex, 4031 bool tx_pause, bool rx_pause) 4032 { 4033 struct net_device *ndev = to_net_dev(config->dev); 4034 struct mvneta_port *pp = netdev_priv(ndev); 4035 u32 val; 4036 4037 if (!phylink_autoneg_inband(mode)) { 4038 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG); 4039 val &= ~(MVNETA_GMAC_FORCE_LINK_DOWN | 4040 MVNETA_GMAC_CONFIG_MII_SPEED | 4041 MVNETA_GMAC_CONFIG_GMII_SPEED | 4042 MVNETA_GMAC_CONFIG_FLOW_CTRL | 4043 MVNETA_GMAC_CONFIG_FULL_DUPLEX); 4044 val |= MVNETA_GMAC_FORCE_LINK_PASS; 4045 4046 if (speed == SPEED_1000 || speed == SPEED_2500) 4047 val |= MVNETA_GMAC_CONFIG_GMII_SPEED; 4048 else if (speed == SPEED_100) 4049 val |= MVNETA_GMAC_CONFIG_MII_SPEED; 4050 4051 if (duplex == DUPLEX_FULL) 4052 val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX; 4053 4054 if (tx_pause || rx_pause) 4055 val |= MVNETA_GMAC_CONFIG_FLOW_CTRL; 4056 4057 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val); 4058 } else { 4059 /* When inband doesn't cover flow control or flow control is 4060 * disabled, we need to manually configure it. This bit will 4061 * only have effect if MVNETA_GMAC_AN_FLOW_CTRL_EN is unset. 4062 */ 4063 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG); 4064 val &= ~MVNETA_GMAC_CONFIG_FLOW_CTRL; 4065 4066 if (tx_pause || rx_pause) 4067 val |= MVNETA_GMAC_CONFIG_FLOW_CTRL; 4068 4069 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val); 4070 } 4071 4072 mvneta_port_up(pp); 4073 4074 if (phy && pp->eee_enabled) { 4075 pp->eee_active = phy_init_eee(phy, 0) >= 0; 4076 mvneta_set_eee(pp, pp->eee_active && pp->tx_lpi_enabled); 4077 } 4078 } 4079 4080 static const struct phylink_mac_ops mvneta_phylink_ops = { 4081 .validate = mvneta_validate, 4082 .mac_pcs_get_state = mvneta_mac_pcs_get_state, 4083 .mac_an_restart = mvneta_mac_an_restart, 4084 .mac_config = mvneta_mac_config, 4085 .mac_link_down = mvneta_mac_link_down, 4086 .mac_link_up = mvneta_mac_link_up, 4087 }; 4088 4089 static int mvneta_mdio_probe(struct mvneta_port *pp) 4090 { 4091 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL }; 4092 int err = phylink_of_phy_connect(pp->phylink, pp->dn, 0); 4093 4094 if (err) 4095 netdev_err(pp->dev, "could not attach PHY: %d\n", err); 4096 4097 phylink_ethtool_get_wol(pp->phylink, &wol); 4098 device_set_wakeup_capable(&pp->dev->dev, !!wol.supported); 4099 4100 /* PHY WoL may be enabled but device wakeup disabled */ 4101 if (wol.supported) 4102 device_set_wakeup_enable(&pp->dev->dev, !!wol.wolopts); 4103 4104 return err; 4105 } 4106 4107 static void mvneta_mdio_remove(struct mvneta_port *pp) 4108 { 4109 phylink_disconnect_phy(pp->phylink); 4110 } 4111 4112 /* Electing a CPU must be done in an atomic way: it should be done 4113 * after or before the removal/insertion of a CPU and this function is 4114 * not reentrant. 4115 */ 4116 static void mvneta_percpu_elect(struct mvneta_port *pp) 4117 { 4118 int elected_cpu = 0, max_cpu, cpu, i = 0; 4119 4120 /* Use the cpu associated to the rxq when it is online, in all 4121 * the other cases, use the cpu 0 which can't be offline. 4122 */ 4123 if (cpu_online(pp->rxq_def)) 4124 elected_cpu = pp->rxq_def; 4125 4126 max_cpu = num_present_cpus(); 4127 4128 for_each_online_cpu(cpu) { 4129 int rxq_map = 0, txq_map = 0; 4130 int rxq; 4131 4132 for (rxq = 0; rxq < rxq_number; rxq++) 4133 if ((rxq % max_cpu) == cpu) 4134 rxq_map |= MVNETA_CPU_RXQ_ACCESS(rxq); 4135 4136 if (cpu == elected_cpu) 4137 /* Map the default receive queue queue to the 4138 * elected CPU 4139 */ 4140 rxq_map |= MVNETA_CPU_RXQ_ACCESS(pp->rxq_def); 4141 4142 /* We update the TX queue map only if we have one 4143 * queue. In this case we associate the TX queue to 4144 * the CPU bound to the default RX queue 4145 */ 4146 if (txq_number == 1) 4147 txq_map = (cpu == elected_cpu) ? 4148 MVNETA_CPU_TXQ_ACCESS(1) : 0; 4149 else 4150 txq_map = mvreg_read(pp, MVNETA_CPU_MAP(cpu)) & 4151 MVNETA_CPU_TXQ_ACCESS_ALL_MASK; 4152 4153 mvreg_write(pp, MVNETA_CPU_MAP(cpu), rxq_map | txq_map); 4154 4155 /* Update the interrupt mask on each CPU according the 4156 * new mapping 4157 */ 4158 smp_call_function_single(cpu, mvneta_percpu_unmask_interrupt, 4159 pp, true); 4160 i++; 4161 4162 } 4163 }; 4164 4165 static int mvneta_cpu_online(unsigned int cpu, struct hlist_node *node) 4166 { 4167 int other_cpu; 4168 struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port, 4169 node_online); 4170 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu); 4171 4172 4173 spin_lock(&pp->lock); 4174 /* 4175 * Configuring the driver for a new CPU while the driver is 4176 * stopping is racy, so just avoid it. 4177 */ 4178 if (pp->is_stopped) { 4179 spin_unlock(&pp->lock); 4180 return 0; 4181 } 4182 netif_tx_stop_all_queues(pp->dev); 4183 4184 /* 4185 * We have to synchronise on tha napi of each CPU except the one 4186 * just being woken up 4187 */ 4188 for_each_online_cpu(other_cpu) { 4189 if (other_cpu != cpu) { 4190 struct mvneta_pcpu_port *other_port = 4191 per_cpu_ptr(pp->ports, other_cpu); 4192 4193 napi_synchronize(&other_port->napi); 4194 } 4195 } 4196 4197 /* Mask all ethernet port interrupts */ 4198 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true); 4199 napi_enable(&port->napi); 4200 4201 /* 4202 * Enable per-CPU interrupts on the CPU that is 4203 * brought up. 4204 */ 4205 mvneta_percpu_enable(pp); 4206 4207 /* 4208 * Enable per-CPU interrupt on the one CPU we care 4209 * about. 4210 */ 4211 mvneta_percpu_elect(pp); 4212 4213 /* Unmask all ethernet port interrupts */ 4214 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true); 4215 mvreg_write(pp, MVNETA_INTR_MISC_MASK, 4216 MVNETA_CAUSE_PHY_STATUS_CHANGE | 4217 MVNETA_CAUSE_LINK_CHANGE); 4218 netif_tx_start_all_queues(pp->dev); 4219 spin_unlock(&pp->lock); 4220 return 0; 4221 } 4222 4223 static int mvneta_cpu_down_prepare(unsigned int cpu, struct hlist_node *node) 4224 { 4225 struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port, 4226 node_online); 4227 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu); 4228 4229 /* 4230 * Thanks to this lock we are sure that any pending cpu election is 4231 * done. 4232 */ 4233 spin_lock(&pp->lock); 4234 /* Mask all ethernet port interrupts */ 4235 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true); 4236 spin_unlock(&pp->lock); 4237 4238 napi_synchronize(&port->napi); 4239 napi_disable(&port->napi); 4240 /* Disable per-CPU interrupts on the CPU that is brought down. */ 4241 mvneta_percpu_disable(pp); 4242 return 0; 4243 } 4244 4245 static int mvneta_cpu_dead(unsigned int cpu, struct hlist_node *node) 4246 { 4247 struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port, 4248 node_dead); 4249 4250 /* Check if a new CPU must be elected now this on is down */ 4251 spin_lock(&pp->lock); 4252 mvneta_percpu_elect(pp); 4253 spin_unlock(&pp->lock); 4254 /* Unmask all ethernet port interrupts */ 4255 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true); 4256 mvreg_write(pp, MVNETA_INTR_MISC_MASK, 4257 MVNETA_CAUSE_PHY_STATUS_CHANGE | 4258 MVNETA_CAUSE_LINK_CHANGE); 4259 netif_tx_start_all_queues(pp->dev); 4260 return 0; 4261 } 4262 4263 static int mvneta_open(struct net_device *dev) 4264 { 4265 struct mvneta_port *pp = netdev_priv(dev); 4266 int ret; 4267 4268 pp->pkt_size = MVNETA_RX_PKT_SIZE(pp->dev->mtu); 4269 4270 ret = mvneta_setup_rxqs(pp); 4271 if (ret) 4272 return ret; 4273 4274 ret = mvneta_setup_txqs(pp); 4275 if (ret) 4276 goto err_cleanup_rxqs; 4277 4278 /* Connect to port interrupt line */ 4279 if (pp->neta_armada3700) 4280 ret = request_irq(pp->dev->irq, mvneta_isr, 0, 4281 dev->name, pp); 4282 else 4283 ret = request_percpu_irq(pp->dev->irq, mvneta_percpu_isr, 4284 dev->name, pp->ports); 4285 if (ret) { 4286 netdev_err(pp->dev, "cannot request irq %d\n", pp->dev->irq); 4287 goto err_cleanup_txqs; 4288 } 4289 4290 if (!pp->neta_armada3700) { 4291 /* Enable per-CPU interrupt on all the CPU to handle our RX 4292 * queue interrupts 4293 */ 4294 on_each_cpu(mvneta_percpu_enable, pp, true); 4295 4296 pp->is_stopped = false; 4297 /* Register a CPU notifier to handle the case where our CPU 4298 * might be taken offline. 4299 */ 4300 ret = cpuhp_state_add_instance_nocalls(online_hpstate, 4301 &pp->node_online); 4302 if (ret) 4303 goto err_free_irq; 4304 4305 ret = cpuhp_state_add_instance_nocalls(CPUHP_NET_MVNETA_DEAD, 4306 &pp->node_dead); 4307 if (ret) 4308 goto err_free_online_hp; 4309 } 4310 4311 ret = mvneta_mdio_probe(pp); 4312 if (ret < 0) { 4313 netdev_err(dev, "cannot probe MDIO bus\n"); 4314 goto err_free_dead_hp; 4315 } 4316 4317 mvneta_start_dev(pp); 4318 4319 return 0; 4320 4321 err_free_dead_hp: 4322 if (!pp->neta_armada3700) 4323 cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD, 4324 &pp->node_dead); 4325 err_free_online_hp: 4326 if (!pp->neta_armada3700) 4327 cpuhp_state_remove_instance_nocalls(online_hpstate, 4328 &pp->node_online); 4329 err_free_irq: 4330 if (pp->neta_armada3700) { 4331 free_irq(pp->dev->irq, pp); 4332 } else { 4333 on_each_cpu(mvneta_percpu_disable, pp, true); 4334 free_percpu_irq(pp->dev->irq, pp->ports); 4335 } 4336 err_cleanup_txqs: 4337 mvneta_cleanup_txqs(pp); 4338 err_cleanup_rxqs: 4339 mvneta_cleanup_rxqs(pp); 4340 return ret; 4341 } 4342 4343 /* Stop the port, free port interrupt line */ 4344 static int mvneta_stop(struct net_device *dev) 4345 { 4346 struct mvneta_port *pp = netdev_priv(dev); 4347 4348 if (!pp->neta_armada3700) { 4349 /* Inform that we are stopping so we don't want to setup the 4350 * driver for new CPUs in the notifiers. The code of the 4351 * notifier for CPU online is protected by the same spinlock, 4352 * so when we get the lock, the notifer work is done. 4353 */ 4354 spin_lock(&pp->lock); 4355 pp->is_stopped = true; 4356 spin_unlock(&pp->lock); 4357 4358 mvneta_stop_dev(pp); 4359 mvneta_mdio_remove(pp); 4360 4361 cpuhp_state_remove_instance_nocalls(online_hpstate, 4362 &pp->node_online); 4363 cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD, 4364 &pp->node_dead); 4365 on_each_cpu(mvneta_percpu_disable, pp, true); 4366 free_percpu_irq(dev->irq, pp->ports); 4367 } else { 4368 mvneta_stop_dev(pp); 4369 mvneta_mdio_remove(pp); 4370 free_irq(dev->irq, pp); 4371 } 4372 4373 mvneta_cleanup_rxqs(pp); 4374 mvneta_cleanup_txqs(pp); 4375 4376 return 0; 4377 } 4378 4379 static int mvneta_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) 4380 { 4381 struct mvneta_port *pp = netdev_priv(dev); 4382 4383 return phylink_mii_ioctl(pp->phylink, ifr, cmd); 4384 } 4385 4386 static int mvneta_xdp_setup(struct net_device *dev, struct bpf_prog *prog, 4387 struct netlink_ext_ack *extack) 4388 { 4389 bool need_update, running = netif_running(dev); 4390 struct mvneta_port *pp = netdev_priv(dev); 4391 struct bpf_prog *old_prog; 4392 4393 if (prog && dev->mtu > MVNETA_MAX_RX_BUF_SIZE) { 4394 NL_SET_ERR_MSG_MOD(extack, "Jumbo frames not supported on XDP"); 4395 return -EOPNOTSUPP; 4396 } 4397 4398 if (pp->bm_priv) { 4399 NL_SET_ERR_MSG_MOD(extack, 4400 "Hardware Buffer Management not supported on XDP"); 4401 return -EOPNOTSUPP; 4402 } 4403 4404 need_update = !!pp->xdp_prog != !!prog; 4405 if (running && need_update) 4406 mvneta_stop(dev); 4407 4408 old_prog = xchg(&pp->xdp_prog, prog); 4409 if (old_prog) 4410 bpf_prog_put(old_prog); 4411 4412 if (running && need_update) 4413 return mvneta_open(dev); 4414 4415 return 0; 4416 } 4417 4418 static int mvneta_xdp(struct net_device *dev, struct netdev_bpf *xdp) 4419 { 4420 struct mvneta_port *pp = netdev_priv(dev); 4421 4422 switch (xdp->command) { 4423 case XDP_SETUP_PROG: 4424 return mvneta_xdp_setup(dev, xdp->prog, xdp->extack); 4425 case XDP_QUERY_PROG: 4426 xdp->prog_id = pp->xdp_prog ? pp->xdp_prog->aux->id : 0; 4427 return 0; 4428 default: 4429 return -EINVAL; 4430 } 4431 } 4432 4433 /* Ethtool methods */ 4434 4435 /* Set link ksettings (phy address, speed) for ethtools */ 4436 static int 4437 mvneta_ethtool_set_link_ksettings(struct net_device *ndev, 4438 const struct ethtool_link_ksettings *cmd) 4439 { 4440 struct mvneta_port *pp = netdev_priv(ndev); 4441 4442 return phylink_ethtool_ksettings_set(pp->phylink, cmd); 4443 } 4444 4445 /* Get link ksettings for ethtools */ 4446 static int 4447 mvneta_ethtool_get_link_ksettings(struct net_device *ndev, 4448 struct ethtool_link_ksettings *cmd) 4449 { 4450 struct mvneta_port *pp = netdev_priv(ndev); 4451 4452 return phylink_ethtool_ksettings_get(pp->phylink, cmd); 4453 } 4454 4455 static int mvneta_ethtool_nway_reset(struct net_device *dev) 4456 { 4457 struct mvneta_port *pp = netdev_priv(dev); 4458 4459 return phylink_ethtool_nway_reset(pp->phylink); 4460 } 4461 4462 /* Set interrupt coalescing for ethtools */ 4463 static int mvneta_ethtool_set_coalesce(struct net_device *dev, 4464 struct ethtool_coalesce *c) 4465 { 4466 struct mvneta_port *pp = netdev_priv(dev); 4467 int queue; 4468 4469 for (queue = 0; queue < rxq_number; queue++) { 4470 struct mvneta_rx_queue *rxq = &pp->rxqs[queue]; 4471 rxq->time_coal = c->rx_coalesce_usecs; 4472 rxq->pkts_coal = c->rx_max_coalesced_frames; 4473 mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal); 4474 mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal); 4475 } 4476 4477 for (queue = 0; queue < txq_number; queue++) { 4478 struct mvneta_tx_queue *txq = &pp->txqs[queue]; 4479 txq->done_pkts_coal = c->tx_max_coalesced_frames; 4480 mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal); 4481 } 4482 4483 return 0; 4484 } 4485 4486 /* get coalescing for ethtools */ 4487 static int mvneta_ethtool_get_coalesce(struct net_device *dev, 4488 struct ethtool_coalesce *c) 4489 { 4490 struct mvneta_port *pp = netdev_priv(dev); 4491 4492 c->rx_coalesce_usecs = pp->rxqs[0].time_coal; 4493 c->rx_max_coalesced_frames = pp->rxqs[0].pkts_coal; 4494 4495 c->tx_max_coalesced_frames = pp->txqs[0].done_pkts_coal; 4496 return 0; 4497 } 4498 4499 4500 static void mvneta_ethtool_get_drvinfo(struct net_device *dev, 4501 struct ethtool_drvinfo *drvinfo) 4502 { 4503 strlcpy(drvinfo->driver, MVNETA_DRIVER_NAME, 4504 sizeof(drvinfo->driver)); 4505 strlcpy(drvinfo->version, MVNETA_DRIVER_VERSION, 4506 sizeof(drvinfo->version)); 4507 strlcpy(drvinfo->bus_info, dev_name(&dev->dev), 4508 sizeof(drvinfo->bus_info)); 4509 } 4510 4511 4512 static void mvneta_ethtool_get_ringparam(struct net_device *netdev, 4513 struct ethtool_ringparam *ring) 4514 { 4515 struct mvneta_port *pp = netdev_priv(netdev); 4516 4517 ring->rx_max_pending = MVNETA_MAX_RXD; 4518 ring->tx_max_pending = MVNETA_MAX_TXD; 4519 ring->rx_pending = pp->rx_ring_size; 4520 ring->tx_pending = pp->tx_ring_size; 4521 } 4522 4523 static int mvneta_ethtool_set_ringparam(struct net_device *dev, 4524 struct ethtool_ringparam *ring) 4525 { 4526 struct mvneta_port *pp = netdev_priv(dev); 4527 4528 if ((ring->rx_pending == 0) || (ring->tx_pending == 0)) 4529 return -EINVAL; 4530 pp->rx_ring_size = ring->rx_pending < MVNETA_MAX_RXD ? 4531 ring->rx_pending : MVNETA_MAX_RXD; 4532 4533 pp->tx_ring_size = clamp_t(u16, ring->tx_pending, 4534 MVNETA_MAX_SKB_DESCS * 2, MVNETA_MAX_TXD); 4535 if (pp->tx_ring_size != ring->tx_pending) 4536 netdev_warn(dev, "TX queue size set to %u (requested %u)\n", 4537 pp->tx_ring_size, ring->tx_pending); 4538 4539 if (netif_running(dev)) { 4540 mvneta_stop(dev); 4541 if (mvneta_open(dev)) { 4542 netdev_err(dev, 4543 "error on opening device after ring param change\n"); 4544 return -ENOMEM; 4545 } 4546 } 4547 4548 return 0; 4549 } 4550 4551 static void mvneta_ethtool_get_pauseparam(struct net_device *dev, 4552 struct ethtool_pauseparam *pause) 4553 { 4554 struct mvneta_port *pp = netdev_priv(dev); 4555 4556 phylink_ethtool_get_pauseparam(pp->phylink, pause); 4557 } 4558 4559 static int mvneta_ethtool_set_pauseparam(struct net_device *dev, 4560 struct ethtool_pauseparam *pause) 4561 { 4562 struct mvneta_port *pp = netdev_priv(dev); 4563 4564 return phylink_ethtool_set_pauseparam(pp->phylink, pause); 4565 } 4566 4567 static void mvneta_ethtool_get_strings(struct net_device *netdev, u32 sset, 4568 u8 *data) 4569 { 4570 if (sset == ETH_SS_STATS) { 4571 int i; 4572 4573 for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++) 4574 memcpy(data + i * ETH_GSTRING_LEN, 4575 mvneta_statistics[i].name, ETH_GSTRING_LEN); 4576 } 4577 } 4578 4579 static void 4580 mvneta_ethtool_update_pcpu_stats(struct mvneta_port *pp, 4581 struct mvneta_ethtool_stats *es) 4582 { 4583 unsigned int start; 4584 int cpu; 4585 4586 for_each_possible_cpu(cpu) { 4587 struct mvneta_pcpu_stats *stats; 4588 u64 skb_alloc_error; 4589 u64 refill_error; 4590 u64 xdp_redirect; 4591 u64 xdp_xmit_err; 4592 u64 xdp_tx_err; 4593 u64 xdp_pass; 4594 u64 xdp_drop; 4595 u64 xdp_xmit; 4596 u64 xdp_tx; 4597 4598 stats = per_cpu_ptr(pp->stats, cpu); 4599 do { 4600 start = u64_stats_fetch_begin_irq(&stats->syncp); 4601 skb_alloc_error = stats->es.skb_alloc_error; 4602 refill_error = stats->es.refill_error; 4603 xdp_redirect = stats->es.ps.xdp_redirect; 4604 xdp_pass = stats->es.ps.xdp_pass; 4605 xdp_drop = stats->es.ps.xdp_drop; 4606 xdp_xmit = stats->es.ps.xdp_xmit; 4607 xdp_xmit_err = stats->es.ps.xdp_xmit_err; 4608 xdp_tx = stats->es.ps.xdp_tx; 4609 xdp_tx_err = stats->es.ps.xdp_tx_err; 4610 } while (u64_stats_fetch_retry_irq(&stats->syncp, start)); 4611 4612 es->skb_alloc_error += skb_alloc_error; 4613 es->refill_error += refill_error; 4614 es->ps.xdp_redirect += xdp_redirect; 4615 es->ps.xdp_pass += xdp_pass; 4616 es->ps.xdp_drop += xdp_drop; 4617 es->ps.xdp_xmit += xdp_xmit; 4618 es->ps.xdp_xmit_err += xdp_xmit_err; 4619 es->ps.xdp_tx += xdp_tx; 4620 es->ps.xdp_tx_err += xdp_tx_err; 4621 } 4622 } 4623 4624 static void mvneta_ethtool_update_stats(struct mvneta_port *pp) 4625 { 4626 struct mvneta_ethtool_stats stats = {}; 4627 const struct mvneta_statistic *s; 4628 void __iomem *base = pp->base; 4629 u32 high, low; 4630 u64 val; 4631 int i; 4632 4633 mvneta_ethtool_update_pcpu_stats(pp, &stats); 4634 for (i = 0, s = mvneta_statistics; 4635 s < mvneta_statistics + ARRAY_SIZE(mvneta_statistics); 4636 s++, i++) { 4637 switch (s->type) { 4638 case T_REG_32: 4639 val = readl_relaxed(base + s->offset); 4640 pp->ethtool_stats[i] += val; 4641 break; 4642 case T_REG_64: 4643 /* Docs say to read low 32-bit then high */ 4644 low = readl_relaxed(base + s->offset); 4645 high = readl_relaxed(base + s->offset + 4); 4646 val = (u64)high << 32 | low; 4647 pp->ethtool_stats[i] += val; 4648 break; 4649 case T_SW: 4650 switch (s->offset) { 4651 case ETHTOOL_STAT_EEE_WAKEUP: 4652 val = phylink_get_eee_err(pp->phylink); 4653 pp->ethtool_stats[i] += val; 4654 break; 4655 case ETHTOOL_STAT_SKB_ALLOC_ERR: 4656 pp->ethtool_stats[i] = stats.skb_alloc_error; 4657 break; 4658 case ETHTOOL_STAT_REFILL_ERR: 4659 pp->ethtool_stats[i] = stats.refill_error; 4660 break; 4661 case ETHTOOL_XDP_REDIRECT: 4662 pp->ethtool_stats[i] = stats.ps.xdp_redirect; 4663 break; 4664 case ETHTOOL_XDP_PASS: 4665 pp->ethtool_stats[i] = stats.ps.xdp_pass; 4666 break; 4667 case ETHTOOL_XDP_DROP: 4668 pp->ethtool_stats[i] = stats.ps.xdp_drop; 4669 break; 4670 case ETHTOOL_XDP_TX: 4671 pp->ethtool_stats[i] = stats.ps.xdp_tx; 4672 break; 4673 case ETHTOOL_XDP_TX_ERR: 4674 pp->ethtool_stats[i] = stats.ps.xdp_tx_err; 4675 break; 4676 case ETHTOOL_XDP_XMIT: 4677 pp->ethtool_stats[i] = stats.ps.xdp_xmit; 4678 break; 4679 case ETHTOOL_XDP_XMIT_ERR: 4680 pp->ethtool_stats[i] = stats.ps.xdp_xmit_err; 4681 break; 4682 } 4683 break; 4684 } 4685 } 4686 } 4687 4688 static void mvneta_ethtool_get_stats(struct net_device *dev, 4689 struct ethtool_stats *stats, u64 *data) 4690 { 4691 struct mvneta_port *pp = netdev_priv(dev); 4692 int i; 4693 4694 mvneta_ethtool_update_stats(pp); 4695 4696 for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++) 4697 *data++ = pp->ethtool_stats[i]; 4698 } 4699 4700 static int mvneta_ethtool_get_sset_count(struct net_device *dev, int sset) 4701 { 4702 if (sset == ETH_SS_STATS) 4703 return ARRAY_SIZE(mvneta_statistics); 4704 return -EOPNOTSUPP; 4705 } 4706 4707 static u32 mvneta_ethtool_get_rxfh_indir_size(struct net_device *dev) 4708 { 4709 return MVNETA_RSS_LU_TABLE_SIZE; 4710 } 4711 4712 static int mvneta_ethtool_get_rxnfc(struct net_device *dev, 4713 struct ethtool_rxnfc *info, 4714 u32 *rules __always_unused) 4715 { 4716 switch (info->cmd) { 4717 case ETHTOOL_GRXRINGS: 4718 info->data = rxq_number; 4719 return 0; 4720 case ETHTOOL_GRXFH: 4721 return -EOPNOTSUPP; 4722 default: 4723 return -EOPNOTSUPP; 4724 } 4725 } 4726 4727 static int mvneta_config_rss(struct mvneta_port *pp) 4728 { 4729 int cpu; 4730 u32 val; 4731 4732 netif_tx_stop_all_queues(pp->dev); 4733 4734 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true); 4735 4736 if (!pp->neta_armada3700) { 4737 /* We have to synchronise on the napi of each CPU */ 4738 for_each_online_cpu(cpu) { 4739 struct mvneta_pcpu_port *pcpu_port = 4740 per_cpu_ptr(pp->ports, cpu); 4741 4742 napi_synchronize(&pcpu_port->napi); 4743 napi_disable(&pcpu_port->napi); 4744 } 4745 } else { 4746 napi_synchronize(&pp->napi); 4747 napi_disable(&pp->napi); 4748 } 4749 4750 pp->rxq_def = pp->indir[0]; 4751 4752 /* Update unicast mapping */ 4753 mvneta_set_rx_mode(pp->dev); 4754 4755 /* Update val of portCfg register accordingly with all RxQueue types */ 4756 val = MVNETA_PORT_CONFIG_DEFL_VALUE(pp->rxq_def); 4757 mvreg_write(pp, MVNETA_PORT_CONFIG, val); 4758 4759 /* Update the elected CPU matching the new rxq_def */ 4760 spin_lock(&pp->lock); 4761 mvneta_percpu_elect(pp); 4762 spin_unlock(&pp->lock); 4763 4764 if (!pp->neta_armada3700) { 4765 /* We have to synchronise on the napi of each CPU */ 4766 for_each_online_cpu(cpu) { 4767 struct mvneta_pcpu_port *pcpu_port = 4768 per_cpu_ptr(pp->ports, cpu); 4769 4770 napi_enable(&pcpu_port->napi); 4771 } 4772 } else { 4773 napi_enable(&pp->napi); 4774 } 4775 4776 netif_tx_start_all_queues(pp->dev); 4777 4778 return 0; 4779 } 4780 4781 static int mvneta_ethtool_set_rxfh(struct net_device *dev, const u32 *indir, 4782 const u8 *key, const u8 hfunc) 4783 { 4784 struct mvneta_port *pp = netdev_priv(dev); 4785 4786 /* Current code for Armada 3700 doesn't support RSS features yet */ 4787 if (pp->neta_armada3700) 4788 return -EOPNOTSUPP; 4789 4790 /* We require at least one supported parameter to be changed 4791 * and no change in any of the unsupported parameters 4792 */ 4793 if (key || 4794 (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)) 4795 return -EOPNOTSUPP; 4796 4797 if (!indir) 4798 return 0; 4799 4800 memcpy(pp->indir, indir, MVNETA_RSS_LU_TABLE_SIZE); 4801 4802 return mvneta_config_rss(pp); 4803 } 4804 4805 static int mvneta_ethtool_get_rxfh(struct net_device *dev, u32 *indir, u8 *key, 4806 u8 *hfunc) 4807 { 4808 struct mvneta_port *pp = netdev_priv(dev); 4809 4810 /* Current code for Armada 3700 doesn't support RSS features yet */ 4811 if (pp->neta_armada3700) 4812 return -EOPNOTSUPP; 4813 4814 if (hfunc) 4815 *hfunc = ETH_RSS_HASH_TOP; 4816 4817 if (!indir) 4818 return 0; 4819 4820 memcpy(indir, pp->indir, MVNETA_RSS_LU_TABLE_SIZE); 4821 4822 return 0; 4823 } 4824 4825 static void mvneta_ethtool_get_wol(struct net_device *dev, 4826 struct ethtool_wolinfo *wol) 4827 { 4828 struct mvneta_port *pp = netdev_priv(dev); 4829 4830 phylink_ethtool_get_wol(pp->phylink, wol); 4831 } 4832 4833 static int mvneta_ethtool_set_wol(struct net_device *dev, 4834 struct ethtool_wolinfo *wol) 4835 { 4836 struct mvneta_port *pp = netdev_priv(dev); 4837 int ret; 4838 4839 ret = phylink_ethtool_set_wol(pp->phylink, wol); 4840 if (!ret) 4841 device_set_wakeup_enable(&dev->dev, !!wol->wolopts); 4842 4843 return ret; 4844 } 4845 4846 static int mvneta_ethtool_get_eee(struct net_device *dev, 4847 struct ethtool_eee *eee) 4848 { 4849 struct mvneta_port *pp = netdev_priv(dev); 4850 u32 lpi_ctl0; 4851 4852 lpi_ctl0 = mvreg_read(pp, MVNETA_LPI_CTRL_0); 4853 4854 eee->eee_enabled = pp->eee_enabled; 4855 eee->eee_active = pp->eee_active; 4856 eee->tx_lpi_enabled = pp->tx_lpi_enabled; 4857 eee->tx_lpi_timer = (lpi_ctl0) >> 8; // * scale; 4858 4859 return phylink_ethtool_get_eee(pp->phylink, eee); 4860 } 4861 4862 static int mvneta_ethtool_set_eee(struct net_device *dev, 4863 struct ethtool_eee *eee) 4864 { 4865 struct mvneta_port *pp = netdev_priv(dev); 4866 u32 lpi_ctl0; 4867 4868 /* The Armada 37x documents do not give limits for this other than 4869 * it being an 8-bit register. */ 4870 if (eee->tx_lpi_enabled && eee->tx_lpi_timer > 255) 4871 return -EINVAL; 4872 4873 lpi_ctl0 = mvreg_read(pp, MVNETA_LPI_CTRL_0); 4874 lpi_ctl0 &= ~(0xff << 8); 4875 lpi_ctl0 |= eee->tx_lpi_timer << 8; 4876 mvreg_write(pp, MVNETA_LPI_CTRL_0, lpi_ctl0); 4877 4878 pp->eee_enabled = eee->eee_enabled; 4879 pp->tx_lpi_enabled = eee->tx_lpi_enabled; 4880 4881 mvneta_set_eee(pp, eee->tx_lpi_enabled && eee->eee_enabled); 4882 4883 return phylink_ethtool_set_eee(pp->phylink, eee); 4884 } 4885 4886 static const struct net_device_ops mvneta_netdev_ops = { 4887 .ndo_open = mvneta_open, 4888 .ndo_stop = mvneta_stop, 4889 .ndo_start_xmit = mvneta_tx, 4890 .ndo_set_rx_mode = mvneta_set_rx_mode, 4891 .ndo_set_mac_address = mvneta_set_mac_addr, 4892 .ndo_change_mtu = mvneta_change_mtu, 4893 .ndo_fix_features = mvneta_fix_features, 4894 .ndo_get_stats64 = mvneta_get_stats64, 4895 .ndo_do_ioctl = mvneta_ioctl, 4896 .ndo_bpf = mvneta_xdp, 4897 .ndo_xdp_xmit = mvneta_xdp_xmit, 4898 }; 4899 4900 static const struct ethtool_ops mvneta_eth_tool_ops = { 4901 .supported_coalesce_params = ETHTOOL_COALESCE_RX_USECS | 4902 ETHTOOL_COALESCE_MAX_FRAMES, 4903 .nway_reset = mvneta_ethtool_nway_reset, 4904 .get_link = ethtool_op_get_link, 4905 .set_coalesce = mvneta_ethtool_set_coalesce, 4906 .get_coalesce = mvneta_ethtool_get_coalesce, 4907 .get_drvinfo = mvneta_ethtool_get_drvinfo, 4908 .get_ringparam = mvneta_ethtool_get_ringparam, 4909 .set_ringparam = mvneta_ethtool_set_ringparam, 4910 .get_pauseparam = mvneta_ethtool_get_pauseparam, 4911 .set_pauseparam = mvneta_ethtool_set_pauseparam, 4912 .get_strings = mvneta_ethtool_get_strings, 4913 .get_ethtool_stats = mvneta_ethtool_get_stats, 4914 .get_sset_count = mvneta_ethtool_get_sset_count, 4915 .get_rxfh_indir_size = mvneta_ethtool_get_rxfh_indir_size, 4916 .get_rxnfc = mvneta_ethtool_get_rxnfc, 4917 .get_rxfh = mvneta_ethtool_get_rxfh, 4918 .set_rxfh = mvneta_ethtool_set_rxfh, 4919 .get_link_ksettings = mvneta_ethtool_get_link_ksettings, 4920 .set_link_ksettings = mvneta_ethtool_set_link_ksettings, 4921 .get_wol = mvneta_ethtool_get_wol, 4922 .set_wol = mvneta_ethtool_set_wol, 4923 .get_eee = mvneta_ethtool_get_eee, 4924 .set_eee = mvneta_ethtool_set_eee, 4925 }; 4926 4927 /* Initialize hw */ 4928 static int mvneta_init(struct device *dev, struct mvneta_port *pp) 4929 { 4930 int queue; 4931 4932 /* Disable port */ 4933 mvneta_port_disable(pp); 4934 4935 /* Set port default values */ 4936 mvneta_defaults_set(pp); 4937 4938 pp->txqs = devm_kcalloc(dev, txq_number, sizeof(*pp->txqs), GFP_KERNEL); 4939 if (!pp->txqs) 4940 return -ENOMEM; 4941 4942 /* Initialize TX descriptor rings */ 4943 for (queue = 0; queue < txq_number; queue++) { 4944 struct mvneta_tx_queue *txq = &pp->txqs[queue]; 4945 txq->id = queue; 4946 txq->size = pp->tx_ring_size; 4947 txq->done_pkts_coal = MVNETA_TXDONE_COAL_PKTS; 4948 } 4949 4950 pp->rxqs = devm_kcalloc(dev, rxq_number, sizeof(*pp->rxqs), GFP_KERNEL); 4951 if (!pp->rxqs) 4952 return -ENOMEM; 4953 4954 /* Create Rx descriptor rings */ 4955 for (queue = 0; queue < rxq_number; queue++) { 4956 struct mvneta_rx_queue *rxq = &pp->rxqs[queue]; 4957 rxq->id = queue; 4958 rxq->size = pp->rx_ring_size; 4959 rxq->pkts_coal = MVNETA_RX_COAL_PKTS; 4960 rxq->time_coal = MVNETA_RX_COAL_USEC; 4961 rxq->buf_virt_addr 4962 = devm_kmalloc_array(pp->dev->dev.parent, 4963 rxq->size, 4964 sizeof(*rxq->buf_virt_addr), 4965 GFP_KERNEL); 4966 if (!rxq->buf_virt_addr) 4967 return -ENOMEM; 4968 } 4969 4970 return 0; 4971 } 4972 4973 /* platform glue : initialize decoding windows */ 4974 static void mvneta_conf_mbus_windows(struct mvneta_port *pp, 4975 const struct mbus_dram_target_info *dram) 4976 { 4977 u32 win_enable; 4978 u32 win_protect; 4979 int i; 4980 4981 for (i = 0; i < 6; i++) { 4982 mvreg_write(pp, MVNETA_WIN_BASE(i), 0); 4983 mvreg_write(pp, MVNETA_WIN_SIZE(i), 0); 4984 4985 if (i < 4) 4986 mvreg_write(pp, MVNETA_WIN_REMAP(i), 0); 4987 } 4988 4989 win_enable = 0x3f; 4990 win_protect = 0; 4991 4992 if (dram) { 4993 for (i = 0; i < dram->num_cs; i++) { 4994 const struct mbus_dram_window *cs = dram->cs + i; 4995 4996 mvreg_write(pp, MVNETA_WIN_BASE(i), 4997 (cs->base & 0xffff0000) | 4998 (cs->mbus_attr << 8) | 4999 dram->mbus_dram_target_id); 5000 5001 mvreg_write(pp, MVNETA_WIN_SIZE(i), 5002 (cs->size - 1) & 0xffff0000); 5003 5004 win_enable &= ~(1 << i); 5005 win_protect |= 3 << (2 * i); 5006 } 5007 } else { 5008 /* For Armada3700 open default 4GB Mbus window, leaving 5009 * arbitration of target/attribute to a different layer 5010 * of configuration. 5011 */ 5012 mvreg_write(pp, MVNETA_WIN_SIZE(0), 0xffff0000); 5013 win_enable &= ~BIT(0); 5014 win_protect = 3; 5015 } 5016 5017 mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable); 5018 mvreg_write(pp, MVNETA_ACCESS_PROTECT_ENABLE, win_protect); 5019 } 5020 5021 /* Power up the port */ 5022 static int mvneta_port_power_up(struct mvneta_port *pp, int phy_mode) 5023 { 5024 /* MAC Cause register should be cleared */ 5025 mvreg_write(pp, MVNETA_UNIT_INTR_CAUSE, 0); 5026 5027 if (phy_mode != PHY_INTERFACE_MODE_QSGMII && 5028 phy_mode != PHY_INTERFACE_MODE_SGMII && 5029 !phy_interface_mode_is_8023z(phy_mode) && 5030 !phy_interface_mode_is_rgmii(phy_mode)) 5031 return -EINVAL; 5032 5033 return 0; 5034 } 5035 5036 /* Device initialization routine */ 5037 static int mvneta_probe(struct platform_device *pdev) 5038 { 5039 struct device_node *dn = pdev->dev.of_node; 5040 struct device_node *bm_node; 5041 struct mvneta_port *pp; 5042 struct net_device *dev; 5043 struct phylink *phylink; 5044 struct phy *comphy; 5045 const char *dt_mac_addr; 5046 char hw_mac_addr[ETH_ALEN]; 5047 phy_interface_t phy_mode; 5048 const char *mac_from; 5049 int tx_csum_limit; 5050 int err; 5051 int cpu; 5052 5053 dev = devm_alloc_etherdev_mqs(&pdev->dev, sizeof(struct mvneta_port), 5054 txq_number, rxq_number); 5055 if (!dev) 5056 return -ENOMEM; 5057 5058 dev->irq = irq_of_parse_and_map(dn, 0); 5059 if (dev->irq == 0) 5060 return -EINVAL; 5061 5062 err = of_get_phy_mode(dn, &phy_mode); 5063 if (err) { 5064 dev_err(&pdev->dev, "incorrect phy-mode\n"); 5065 goto err_free_irq; 5066 } 5067 5068 comphy = devm_of_phy_get(&pdev->dev, dn, NULL); 5069 if (comphy == ERR_PTR(-EPROBE_DEFER)) { 5070 err = -EPROBE_DEFER; 5071 goto err_free_irq; 5072 } else if (IS_ERR(comphy)) { 5073 comphy = NULL; 5074 } 5075 5076 pp = netdev_priv(dev); 5077 spin_lock_init(&pp->lock); 5078 5079 pp->phylink_config.dev = &dev->dev; 5080 pp->phylink_config.type = PHYLINK_NETDEV; 5081 5082 phylink = phylink_create(&pp->phylink_config, pdev->dev.fwnode, 5083 phy_mode, &mvneta_phylink_ops); 5084 if (IS_ERR(phylink)) { 5085 err = PTR_ERR(phylink); 5086 goto err_free_irq; 5087 } 5088 5089 dev->tx_queue_len = MVNETA_MAX_TXD; 5090 dev->watchdog_timeo = 5 * HZ; 5091 dev->netdev_ops = &mvneta_netdev_ops; 5092 5093 dev->ethtool_ops = &mvneta_eth_tool_ops; 5094 5095 pp->phylink = phylink; 5096 pp->comphy = comphy; 5097 pp->phy_interface = phy_mode; 5098 pp->dn = dn; 5099 5100 pp->rxq_def = rxq_def; 5101 pp->indir[0] = rxq_def; 5102 5103 /* Get special SoC configurations */ 5104 if (of_device_is_compatible(dn, "marvell,armada-3700-neta")) 5105 pp->neta_armada3700 = true; 5106 5107 pp->clk = devm_clk_get(&pdev->dev, "core"); 5108 if (IS_ERR(pp->clk)) 5109 pp->clk = devm_clk_get(&pdev->dev, NULL); 5110 if (IS_ERR(pp->clk)) { 5111 err = PTR_ERR(pp->clk); 5112 goto err_free_phylink; 5113 } 5114 5115 clk_prepare_enable(pp->clk); 5116 5117 pp->clk_bus = devm_clk_get(&pdev->dev, "bus"); 5118 if (!IS_ERR(pp->clk_bus)) 5119 clk_prepare_enable(pp->clk_bus); 5120 5121 pp->base = devm_platform_ioremap_resource(pdev, 0); 5122 if (IS_ERR(pp->base)) { 5123 err = PTR_ERR(pp->base); 5124 goto err_clk; 5125 } 5126 5127 /* Alloc per-cpu port structure */ 5128 pp->ports = alloc_percpu(struct mvneta_pcpu_port); 5129 if (!pp->ports) { 5130 err = -ENOMEM; 5131 goto err_clk; 5132 } 5133 5134 /* Alloc per-cpu stats */ 5135 pp->stats = netdev_alloc_pcpu_stats(struct mvneta_pcpu_stats); 5136 if (!pp->stats) { 5137 err = -ENOMEM; 5138 goto err_free_ports; 5139 } 5140 5141 dt_mac_addr = of_get_mac_address(dn); 5142 if (!IS_ERR(dt_mac_addr)) { 5143 mac_from = "device tree"; 5144 ether_addr_copy(dev->dev_addr, dt_mac_addr); 5145 } else { 5146 mvneta_get_mac_addr(pp, hw_mac_addr); 5147 if (is_valid_ether_addr(hw_mac_addr)) { 5148 mac_from = "hardware"; 5149 memcpy(dev->dev_addr, hw_mac_addr, ETH_ALEN); 5150 } else { 5151 mac_from = "random"; 5152 eth_hw_addr_random(dev); 5153 } 5154 } 5155 5156 if (!of_property_read_u32(dn, "tx-csum-limit", &tx_csum_limit)) { 5157 if (tx_csum_limit < 0 || 5158 tx_csum_limit > MVNETA_TX_CSUM_MAX_SIZE) { 5159 tx_csum_limit = MVNETA_TX_CSUM_DEF_SIZE; 5160 dev_info(&pdev->dev, 5161 "Wrong TX csum limit in DT, set to %dB\n", 5162 MVNETA_TX_CSUM_DEF_SIZE); 5163 } 5164 } else if (of_device_is_compatible(dn, "marvell,armada-370-neta")) { 5165 tx_csum_limit = MVNETA_TX_CSUM_DEF_SIZE; 5166 } else { 5167 tx_csum_limit = MVNETA_TX_CSUM_MAX_SIZE; 5168 } 5169 5170 pp->tx_csum_limit = tx_csum_limit; 5171 5172 pp->dram_target_info = mv_mbus_dram_info(); 5173 /* Armada3700 requires setting default configuration of Mbus 5174 * windows, however without using filled mbus_dram_target_info 5175 * structure. 5176 */ 5177 if (pp->dram_target_info || pp->neta_armada3700) 5178 mvneta_conf_mbus_windows(pp, pp->dram_target_info); 5179 5180 pp->tx_ring_size = MVNETA_MAX_TXD; 5181 pp->rx_ring_size = MVNETA_MAX_RXD; 5182 5183 pp->dev = dev; 5184 SET_NETDEV_DEV(dev, &pdev->dev); 5185 5186 pp->id = global_port_id++; 5187 5188 /* Obtain access to BM resources if enabled and already initialized */ 5189 bm_node = of_parse_phandle(dn, "buffer-manager", 0); 5190 if (bm_node) { 5191 pp->bm_priv = mvneta_bm_get(bm_node); 5192 if (pp->bm_priv) { 5193 err = mvneta_bm_port_init(pdev, pp); 5194 if (err < 0) { 5195 dev_info(&pdev->dev, 5196 "use SW buffer management\n"); 5197 mvneta_bm_put(pp->bm_priv); 5198 pp->bm_priv = NULL; 5199 } 5200 } 5201 /* Set RX packet offset correction for platforms, whose 5202 * NET_SKB_PAD, exceeds 64B. It should be 64B for 64-bit 5203 * platforms and 0B for 32-bit ones. 5204 */ 5205 pp->rx_offset_correction = max(0, 5206 NET_SKB_PAD - 5207 MVNETA_RX_PKT_OFFSET_CORRECTION); 5208 } 5209 of_node_put(bm_node); 5210 5211 /* sw buffer management */ 5212 if (!pp->bm_priv) 5213 pp->rx_offset_correction = MVNETA_SKB_HEADROOM; 5214 5215 err = mvneta_init(&pdev->dev, pp); 5216 if (err < 0) 5217 goto err_netdev; 5218 5219 err = mvneta_port_power_up(pp, pp->phy_interface); 5220 if (err < 0) { 5221 dev_err(&pdev->dev, "can't power up port\n"); 5222 return err; 5223 } 5224 5225 /* Armada3700 network controller does not support per-cpu 5226 * operation, so only single NAPI should be initialized. 5227 */ 5228 if (pp->neta_armada3700) { 5229 netif_napi_add(dev, &pp->napi, mvneta_poll, NAPI_POLL_WEIGHT); 5230 } else { 5231 for_each_present_cpu(cpu) { 5232 struct mvneta_pcpu_port *port = 5233 per_cpu_ptr(pp->ports, cpu); 5234 5235 netif_napi_add(dev, &port->napi, mvneta_poll, 5236 NAPI_POLL_WEIGHT); 5237 port->pp = pp; 5238 } 5239 } 5240 5241 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | 5242 NETIF_F_TSO | NETIF_F_RXCSUM; 5243 dev->hw_features |= dev->features; 5244 dev->vlan_features |= dev->features; 5245 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; 5246 dev->gso_max_segs = MVNETA_MAX_TSO_SEGS; 5247 5248 /* MTU range: 68 - 9676 */ 5249 dev->min_mtu = ETH_MIN_MTU; 5250 /* 9676 == 9700 - 20 and rounding to 8 */ 5251 dev->max_mtu = 9676; 5252 5253 err = register_netdev(dev); 5254 if (err < 0) { 5255 dev_err(&pdev->dev, "failed to register\n"); 5256 goto err_netdev; 5257 } 5258 5259 netdev_info(dev, "Using %s mac address %pM\n", mac_from, 5260 dev->dev_addr); 5261 5262 platform_set_drvdata(pdev, pp->dev); 5263 5264 return 0; 5265 5266 err_netdev: 5267 if (pp->bm_priv) { 5268 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id); 5269 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short, 5270 1 << pp->id); 5271 mvneta_bm_put(pp->bm_priv); 5272 } 5273 free_percpu(pp->stats); 5274 err_free_ports: 5275 free_percpu(pp->ports); 5276 err_clk: 5277 clk_disable_unprepare(pp->clk_bus); 5278 clk_disable_unprepare(pp->clk); 5279 err_free_phylink: 5280 if (pp->phylink) 5281 phylink_destroy(pp->phylink); 5282 err_free_irq: 5283 irq_dispose_mapping(dev->irq); 5284 return err; 5285 } 5286 5287 /* Device removal routine */ 5288 static int mvneta_remove(struct platform_device *pdev) 5289 { 5290 struct net_device *dev = platform_get_drvdata(pdev); 5291 struct mvneta_port *pp = netdev_priv(dev); 5292 5293 unregister_netdev(dev); 5294 clk_disable_unprepare(pp->clk_bus); 5295 clk_disable_unprepare(pp->clk); 5296 free_percpu(pp->ports); 5297 free_percpu(pp->stats); 5298 irq_dispose_mapping(dev->irq); 5299 phylink_destroy(pp->phylink); 5300 5301 if (pp->bm_priv) { 5302 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id); 5303 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short, 5304 1 << pp->id); 5305 mvneta_bm_put(pp->bm_priv); 5306 } 5307 5308 return 0; 5309 } 5310 5311 #ifdef CONFIG_PM_SLEEP 5312 static int mvneta_suspend(struct device *device) 5313 { 5314 int queue; 5315 struct net_device *dev = dev_get_drvdata(device); 5316 struct mvneta_port *pp = netdev_priv(dev); 5317 5318 if (!netif_running(dev)) 5319 goto clean_exit; 5320 5321 if (!pp->neta_armada3700) { 5322 spin_lock(&pp->lock); 5323 pp->is_stopped = true; 5324 spin_unlock(&pp->lock); 5325 5326 cpuhp_state_remove_instance_nocalls(online_hpstate, 5327 &pp->node_online); 5328 cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD, 5329 &pp->node_dead); 5330 } 5331 5332 rtnl_lock(); 5333 mvneta_stop_dev(pp); 5334 rtnl_unlock(); 5335 5336 for (queue = 0; queue < rxq_number; queue++) { 5337 struct mvneta_rx_queue *rxq = &pp->rxqs[queue]; 5338 5339 mvneta_rxq_drop_pkts(pp, rxq); 5340 } 5341 5342 for (queue = 0; queue < txq_number; queue++) { 5343 struct mvneta_tx_queue *txq = &pp->txqs[queue]; 5344 5345 mvneta_txq_hw_deinit(pp, txq); 5346 } 5347 5348 clean_exit: 5349 netif_device_detach(dev); 5350 clk_disable_unprepare(pp->clk_bus); 5351 clk_disable_unprepare(pp->clk); 5352 5353 return 0; 5354 } 5355 5356 static int mvneta_resume(struct device *device) 5357 { 5358 struct platform_device *pdev = to_platform_device(device); 5359 struct net_device *dev = dev_get_drvdata(device); 5360 struct mvneta_port *pp = netdev_priv(dev); 5361 int err, queue; 5362 5363 clk_prepare_enable(pp->clk); 5364 if (!IS_ERR(pp->clk_bus)) 5365 clk_prepare_enable(pp->clk_bus); 5366 if (pp->dram_target_info || pp->neta_armada3700) 5367 mvneta_conf_mbus_windows(pp, pp->dram_target_info); 5368 if (pp->bm_priv) { 5369 err = mvneta_bm_port_init(pdev, pp); 5370 if (err < 0) { 5371 dev_info(&pdev->dev, "use SW buffer management\n"); 5372 pp->rx_offset_correction = MVNETA_SKB_HEADROOM; 5373 pp->bm_priv = NULL; 5374 } 5375 } 5376 mvneta_defaults_set(pp); 5377 err = mvneta_port_power_up(pp, pp->phy_interface); 5378 if (err < 0) { 5379 dev_err(device, "can't power up port\n"); 5380 return err; 5381 } 5382 5383 netif_device_attach(dev); 5384 5385 if (!netif_running(dev)) 5386 return 0; 5387 5388 for (queue = 0; queue < rxq_number; queue++) { 5389 struct mvneta_rx_queue *rxq = &pp->rxqs[queue]; 5390 5391 rxq->next_desc_to_proc = 0; 5392 mvneta_rxq_hw_init(pp, rxq); 5393 } 5394 5395 for (queue = 0; queue < txq_number; queue++) { 5396 struct mvneta_tx_queue *txq = &pp->txqs[queue]; 5397 5398 txq->next_desc_to_proc = 0; 5399 mvneta_txq_hw_init(pp, txq); 5400 } 5401 5402 if (!pp->neta_armada3700) { 5403 spin_lock(&pp->lock); 5404 pp->is_stopped = false; 5405 spin_unlock(&pp->lock); 5406 cpuhp_state_add_instance_nocalls(online_hpstate, 5407 &pp->node_online); 5408 cpuhp_state_add_instance_nocalls(CPUHP_NET_MVNETA_DEAD, 5409 &pp->node_dead); 5410 } 5411 5412 rtnl_lock(); 5413 mvneta_start_dev(pp); 5414 rtnl_unlock(); 5415 mvneta_set_rx_mode(dev); 5416 5417 return 0; 5418 } 5419 #endif 5420 5421 static SIMPLE_DEV_PM_OPS(mvneta_pm_ops, mvneta_suspend, mvneta_resume); 5422 5423 static const struct of_device_id mvneta_match[] = { 5424 { .compatible = "marvell,armada-370-neta" }, 5425 { .compatible = "marvell,armada-xp-neta" }, 5426 { .compatible = "marvell,armada-3700-neta" }, 5427 { } 5428 }; 5429 MODULE_DEVICE_TABLE(of, mvneta_match); 5430 5431 static struct platform_driver mvneta_driver = { 5432 .probe = mvneta_probe, 5433 .remove = mvneta_remove, 5434 .driver = { 5435 .name = MVNETA_DRIVER_NAME, 5436 .of_match_table = mvneta_match, 5437 .pm = &mvneta_pm_ops, 5438 }, 5439 }; 5440 5441 static int __init mvneta_driver_init(void) 5442 { 5443 int ret; 5444 5445 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "net/mvneta:online", 5446 mvneta_cpu_online, 5447 mvneta_cpu_down_prepare); 5448 if (ret < 0) 5449 goto out; 5450 online_hpstate = ret; 5451 ret = cpuhp_setup_state_multi(CPUHP_NET_MVNETA_DEAD, "net/mvneta:dead", 5452 NULL, mvneta_cpu_dead); 5453 if (ret) 5454 goto err_dead; 5455 5456 ret = platform_driver_register(&mvneta_driver); 5457 if (ret) 5458 goto err; 5459 return 0; 5460 5461 err: 5462 cpuhp_remove_multi_state(CPUHP_NET_MVNETA_DEAD); 5463 err_dead: 5464 cpuhp_remove_multi_state(online_hpstate); 5465 out: 5466 return ret; 5467 } 5468 module_init(mvneta_driver_init); 5469 5470 static void __exit mvneta_driver_exit(void) 5471 { 5472 platform_driver_unregister(&mvneta_driver); 5473 cpuhp_remove_multi_state(CPUHP_NET_MVNETA_DEAD); 5474 cpuhp_remove_multi_state(online_hpstate); 5475 } 5476 module_exit(mvneta_driver_exit); 5477 5478 MODULE_DESCRIPTION("Marvell NETA Ethernet Driver - www.marvell.com"); 5479 MODULE_AUTHOR("Rami Rosen <rosenr@marvell.com>, Thomas Petazzoni <thomas.petazzoni@free-electrons.com>"); 5480 MODULE_LICENSE("GPL"); 5481 5482 module_param(rxq_number, int, 0444); 5483 module_param(txq_number, int, 0444); 5484 5485 module_param(rxq_def, int, 0444); 5486 module_param(rx_copybreak, int, 0644); 5487