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