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