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