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