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