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