2 * Driver for Marvell NETA network card for Armada XP and Armada 370 SoCs.
4 * Copyright (C) 2012 Marvell
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.
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>
21 #include <linux/kernel.h>
22 #include <linux/mbus.h>
23 #include <linux/module.h>
24 #include <linux/netdevice.h>
26 #include <linux/of_address.h>
27 #include <linux/of_irq.h>
28 #include <linux/of_mdio.h>
29 #include <linux/of_net.h>
30 #include <linux/phy/phy.h>
31 #include <linux/phy.h>
32 #include <linux/phylink.h>
33 #include <linux/platform_device.h>
34 #include <linux/skbuff.h>
36 #include "mvneta_bm.h"
40 #include <net/page_pool.h>
41 #include <net/pkt_sched.h>
42 #include <linux/bpf_trace.h>
45 #define MVNETA_RXQ_CONFIG_REG(q) (0x1400 + ((q) << 2))
46 #define MVNETA_RXQ_HW_BUF_ALLOC BIT(0)
47 #define MVNETA_RXQ_SHORT_POOL_ID_SHIFT 4
48 #define MVNETA_RXQ_SHORT_POOL_ID_MASK 0x30
49 #define MVNETA_RXQ_LONG_POOL_ID_SHIFT 6
50 #define MVNETA_RXQ_LONG_POOL_ID_MASK 0xc0
51 #define MVNETA_RXQ_PKT_OFFSET_ALL_MASK (0xf << 8)
52 #define MVNETA_RXQ_PKT_OFFSET_MASK(offs) ((offs) << 8)
53 #define MVNETA_RXQ_THRESHOLD_REG(q) (0x14c0 + ((q) << 2))
54 #define MVNETA_RXQ_NON_OCCUPIED(v) ((v) << 16)
55 #define MVNETA_RXQ_BASE_ADDR_REG(q) (0x1480 + ((q) << 2))
56 #define MVNETA_RXQ_SIZE_REG(q) (0x14a0 + ((q) << 2))
57 #define MVNETA_RXQ_BUF_SIZE_SHIFT 19
58 #define MVNETA_RXQ_BUF_SIZE_MASK (0x1fff << 19)
59 #define MVNETA_RXQ_STATUS_REG(q) (0x14e0 + ((q) << 2))
60 #define MVNETA_RXQ_OCCUPIED_ALL_MASK 0x3fff
61 #define MVNETA_RXQ_STATUS_UPDATE_REG(q) (0x1500 + ((q) << 2))
62 #define MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT 16
63 #define MVNETA_RXQ_ADD_NON_OCCUPIED_MAX 255
64 #define MVNETA_PORT_POOL_BUFFER_SZ_REG(pool) (0x1700 + ((pool) << 2))
65 #define MVNETA_PORT_POOL_BUFFER_SZ_SHIFT 3
66 #define MVNETA_PORT_POOL_BUFFER_SZ_MASK 0xfff8
67 #define MVNETA_PORT_RX_RESET 0x1cc0
68 #define MVNETA_PORT_RX_DMA_RESET BIT(0)
69 #define MVNETA_PHY_ADDR 0x2000
70 #define MVNETA_PHY_ADDR_MASK 0x1f
71 #define MVNETA_MBUS_RETRY 0x2010
72 #define MVNETA_UNIT_INTR_CAUSE 0x2080
73 #define MVNETA_UNIT_CONTROL 0x20B0
74 #define MVNETA_PHY_POLLING_ENABLE BIT(1)
75 #define MVNETA_WIN_BASE(w) (0x2200 + ((w) << 3))
76 #define MVNETA_WIN_SIZE(w) (0x2204 + ((w) << 3))
77 #define MVNETA_WIN_REMAP(w) (0x2280 + ((w) << 2))
78 #define MVNETA_BASE_ADDR_ENABLE 0x2290
79 #define MVNETA_AC5_CNM_DDR_TARGET 0x2
80 #define MVNETA_AC5_CNM_DDR_ATTR 0xb
81 #define MVNETA_ACCESS_PROTECT_ENABLE 0x2294
82 #define MVNETA_PORT_CONFIG 0x2400
83 #define MVNETA_UNI_PROMISC_MODE BIT(0)
84 #define MVNETA_DEF_RXQ(q) ((q) << 1)
85 #define MVNETA_DEF_RXQ_ARP(q) ((q) << 4)
86 #define MVNETA_TX_UNSET_ERR_SUM BIT(12)
87 #define MVNETA_DEF_RXQ_TCP(q) ((q) << 16)
88 #define MVNETA_DEF_RXQ_UDP(q) ((q) << 19)
89 #define MVNETA_DEF_RXQ_BPDU(q) ((q) << 22)
90 #define MVNETA_RX_CSUM_WITH_PSEUDO_HDR BIT(25)
91 #define MVNETA_PORT_CONFIG_DEFL_VALUE(q) (MVNETA_DEF_RXQ(q) | \
92 MVNETA_DEF_RXQ_ARP(q) | \
93 MVNETA_DEF_RXQ_TCP(q) | \
94 MVNETA_DEF_RXQ_UDP(q) | \
95 MVNETA_DEF_RXQ_BPDU(q) | \
96 MVNETA_TX_UNSET_ERR_SUM | \
97 MVNETA_RX_CSUM_WITH_PSEUDO_HDR)
98 #define MVNETA_PORT_CONFIG_EXTEND 0x2404
99 #define MVNETA_MAC_ADDR_LOW 0x2414
100 #define MVNETA_MAC_ADDR_HIGH 0x2418
101 #define MVNETA_SDMA_CONFIG 0x241c
102 #define MVNETA_SDMA_BRST_SIZE_16 4
103 #define MVNETA_RX_BRST_SZ_MASK(burst) ((burst) << 1)
104 #define MVNETA_RX_NO_DATA_SWAP BIT(4)
105 #define MVNETA_TX_NO_DATA_SWAP BIT(5)
106 #define MVNETA_DESC_SWAP BIT(6)
107 #define MVNETA_TX_BRST_SZ_MASK(burst) ((burst) << 22)
108 #define MVNETA_VLAN_PRIO_TO_RXQ 0x2440
109 #define MVNETA_VLAN_PRIO_RXQ_MAP(prio, rxq) ((rxq) << ((prio) * 3))
110 #define MVNETA_PORT_STATUS 0x2444
111 #define MVNETA_TX_IN_PRGRS BIT(0)
112 #define MVNETA_TX_FIFO_EMPTY BIT(8)
113 #define MVNETA_RX_MIN_FRAME_SIZE 0x247c
114 /* Only exists on Armada XP and Armada 370 */
115 #define MVNETA_SERDES_CFG 0x24A0
116 #define MVNETA_SGMII_SERDES_PROTO 0x0cc7
117 #define MVNETA_QSGMII_SERDES_PROTO 0x0667
118 #define MVNETA_HSGMII_SERDES_PROTO 0x1107
119 #define MVNETA_TYPE_PRIO 0x24bc
120 #define MVNETA_FORCE_UNI BIT(21)
121 #define MVNETA_TXQ_CMD_1 0x24e4
122 #define MVNETA_TXQ_CMD 0x2448
123 #define MVNETA_TXQ_DISABLE_SHIFT 8
124 #define MVNETA_TXQ_ENABLE_MASK 0x000000ff
125 #define MVNETA_RX_DISCARD_FRAME_COUNT 0x2484
126 #define MVNETA_OVERRUN_FRAME_COUNT 0x2488
127 #define MVNETA_GMAC_CLOCK_DIVIDER 0x24f4
128 #define MVNETA_GMAC_1MS_CLOCK_ENABLE BIT(31)
129 #define MVNETA_ACC_MODE 0x2500
130 #define MVNETA_BM_ADDRESS 0x2504
131 #define MVNETA_CPU_MAP(cpu) (0x2540 + ((cpu) << 2))
132 #define MVNETA_CPU_RXQ_ACCESS_ALL_MASK 0x000000ff
133 #define MVNETA_CPU_TXQ_ACCESS_ALL_MASK 0x0000ff00
134 #define MVNETA_CPU_RXQ_ACCESS(rxq) BIT(rxq)
135 #define MVNETA_CPU_TXQ_ACCESS(txq) BIT(txq + 8)
136 #define MVNETA_RXQ_TIME_COAL_REG(q) (0x2580 + ((q) << 2))
138 /* Exception Interrupt Port/Queue Cause register
140 * Their behavior depend of the mapping done using the PCPX2Q
141 * registers. For a given CPU if the bit associated to a queue is not
142 * set, then for the register a read from this CPU will always return
143 * 0 and a write won't do anything
146 #define MVNETA_INTR_NEW_CAUSE 0x25a0
147 #define MVNETA_INTR_NEW_MASK 0x25a4
149 /* bits 0..7 = TXQ SENT, one bit per queue.
150 * bits 8..15 = RXQ OCCUP, one bit per queue.
151 * bits 16..23 = RXQ FREE, one bit per queue.
152 * bit 29 = OLD_REG_SUM, see old reg ?
153 * bit 30 = TX_ERR_SUM, one bit for 4 ports
154 * bit 31 = MISC_SUM, one bit for 4 ports
156 #define MVNETA_TX_INTR_MASK(nr_txqs) (((1 << nr_txqs) - 1) << 0)
157 #define MVNETA_TX_INTR_MASK_ALL (0xff << 0)
158 #define MVNETA_RX_INTR_MASK(nr_rxqs) (((1 << nr_rxqs) - 1) << 8)
159 #define MVNETA_RX_INTR_MASK_ALL (0xff << 8)
160 #define MVNETA_MISCINTR_INTR_MASK BIT(31)
162 #define MVNETA_INTR_OLD_CAUSE 0x25a8
163 #define MVNETA_INTR_OLD_MASK 0x25ac
165 /* Data Path Port/Queue Cause Register */
166 #define MVNETA_INTR_MISC_CAUSE 0x25b0
167 #define MVNETA_INTR_MISC_MASK 0x25b4
169 #define MVNETA_CAUSE_PHY_STATUS_CHANGE BIT(0)
170 #define MVNETA_CAUSE_LINK_CHANGE BIT(1)
171 #define MVNETA_CAUSE_PTP BIT(4)
173 #define MVNETA_CAUSE_INTERNAL_ADDR_ERR BIT(7)
174 #define MVNETA_CAUSE_RX_OVERRUN BIT(8)
175 #define MVNETA_CAUSE_RX_CRC_ERROR BIT(9)
176 #define MVNETA_CAUSE_RX_LARGE_PKT BIT(10)
177 #define MVNETA_CAUSE_TX_UNDERUN BIT(11)
178 #define MVNETA_CAUSE_PRBS_ERR BIT(12)
179 #define MVNETA_CAUSE_PSC_SYNC_CHANGE BIT(13)
180 #define MVNETA_CAUSE_SERDES_SYNC_ERR BIT(14)
182 #define MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT 16
183 #define MVNETA_CAUSE_BMU_ALLOC_ERR_ALL_MASK (0xF << MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT)
184 #define MVNETA_CAUSE_BMU_ALLOC_ERR_MASK(pool) (1 << (MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT + (pool)))
186 #define MVNETA_CAUSE_TXQ_ERROR_SHIFT 24
187 #define MVNETA_CAUSE_TXQ_ERROR_ALL_MASK (0xFF << MVNETA_CAUSE_TXQ_ERROR_SHIFT)
188 #define MVNETA_CAUSE_TXQ_ERROR_MASK(q) (1 << (MVNETA_CAUSE_TXQ_ERROR_SHIFT + (q)))
190 #define MVNETA_INTR_ENABLE 0x25b8
191 #define MVNETA_TXQ_INTR_ENABLE_ALL_MASK 0x0000ff00
192 #define MVNETA_RXQ_INTR_ENABLE_ALL_MASK 0x000000ff
194 #define MVNETA_RXQ_CMD 0x2680
195 #define MVNETA_RXQ_DISABLE_SHIFT 8
196 #define MVNETA_RXQ_ENABLE_MASK 0x000000ff
197 #define MVETH_TXQ_TOKEN_COUNT_REG(q) (0x2700 + ((q) << 4))
198 #define MVETH_TXQ_TOKEN_CFG_REG(q) (0x2704 + ((q) << 4))
199 #define MVNETA_GMAC_CTRL_0 0x2c00
200 #define MVNETA_GMAC_MAX_RX_SIZE_SHIFT 2
201 #define MVNETA_GMAC_MAX_RX_SIZE_MASK 0x7ffc
202 #define MVNETA_GMAC0_PORT_1000BASE_X BIT(1)
203 #define MVNETA_GMAC0_PORT_ENABLE BIT(0)
204 #define MVNETA_GMAC_CTRL_2 0x2c08
205 #define MVNETA_GMAC2_INBAND_AN_ENABLE BIT(0)
206 #define MVNETA_GMAC2_PCS_ENABLE BIT(3)
207 #define MVNETA_GMAC2_PORT_RGMII BIT(4)
208 #define MVNETA_GMAC2_PORT_RESET BIT(6)
209 #define MVNETA_GMAC_STATUS 0x2c10
210 #define MVNETA_GMAC_LINK_UP BIT(0)
211 #define MVNETA_GMAC_SPEED_1000 BIT(1)
212 #define MVNETA_GMAC_SPEED_100 BIT(2)
213 #define MVNETA_GMAC_FULL_DUPLEX BIT(3)
214 #define MVNETA_GMAC_RX_FLOW_CTRL_ENABLE BIT(4)
215 #define MVNETA_GMAC_TX_FLOW_CTRL_ENABLE BIT(5)
216 #define MVNETA_GMAC_RX_FLOW_CTRL_ACTIVE BIT(6)
217 #define MVNETA_GMAC_TX_FLOW_CTRL_ACTIVE BIT(7)
218 #define MVNETA_GMAC_AN_COMPLETE BIT(11)
219 #define MVNETA_GMAC_SYNC_OK BIT(14)
220 #define MVNETA_GMAC_AUTONEG_CONFIG 0x2c0c
221 #define MVNETA_GMAC_FORCE_LINK_DOWN BIT(0)
222 #define MVNETA_GMAC_FORCE_LINK_PASS BIT(1)
223 #define MVNETA_GMAC_INBAND_AN_ENABLE BIT(2)
224 #define MVNETA_GMAC_AN_BYPASS_ENABLE BIT(3)
225 #define MVNETA_GMAC_INBAND_RESTART_AN BIT(4)
226 #define MVNETA_GMAC_CONFIG_MII_SPEED BIT(5)
227 #define MVNETA_GMAC_CONFIG_GMII_SPEED BIT(6)
228 #define MVNETA_GMAC_AN_SPEED_EN BIT(7)
229 #define MVNETA_GMAC_CONFIG_FLOW_CTRL BIT(8)
230 #define MVNETA_GMAC_ADVERT_SYM_FLOW_CTRL BIT(9)
231 #define MVNETA_GMAC_AN_FLOW_CTRL_EN BIT(11)
232 #define MVNETA_GMAC_CONFIG_FULL_DUPLEX BIT(12)
233 #define MVNETA_GMAC_AN_DUPLEX_EN BIT(13)
234 #define MVNETA_GMAC_CTRL_4 0x2c90
235 #define MVNETA_GMAC4_SHORT_PREAMBLE_ENABLE BIT(1)
236 #define MVNETA_MIB_COUNTERS_BASE 0x3000
237 #define MVNETA_MIB_LATE_COLLISION 0x7c
238 #define MVNETA_DA_FILT_SPEC_MCAST 0x3400
239 #define MVNETA_DA_FILT_OTH_MCAST 0x3500
240 #define MVNETA_DA_FILT_UCAST_BASE 0x3600
241 #define MVNETA_TXQ_BASE_ADDR_REG(q) (0x3c00 + ((q) << 2))
242 #define MVNETA_TXQ_SIZE_REG(q) (0x3c20 + ((q) << 2))
243 #define MVNETA_TXQ_SENT_THRESH_ALL_MASK 0x3fff0000
244 #define MVNETA_TXQ_SENT_THRESH_MASK(coal) ((coal) << 16)
245 #define MVNETA_TXQ_UPDATE_REG(q) (0x3c60 + ((q) << 2))
246 #define MVNETA_TXQ_DEC_SENT_SHIFT 16
247 #define MVNETA_TXQ_DEC_SENT_MASK 0xff
248 #define MVNETA_TXQ_STATUS_REG(q) (0x3c40 + ((q) << 2))
249 #define MVNETA_TXQ_SENT_DESC_SHIFT 16
250 #define MVNETA_TXQ_SENT_DESC_MASK 0x3fff0000
251 #define MVNETA_PORT_TX_RESET 0x3cf0
252 #define MVNETA_PORT_TX_DMA_RESET BIT(0)
253 #define MVNETA_TXQ_CMD1_REG 0x3e00
254 #define MVNETA_TXQ_CMD1_BW_LIM_SEL_V1 BIT(3)
255 #define MVNETA_TXQ_CMD1_BW_LIM_EN BIT(0)
256 #define MVNETA_REFILL_NUM_CLK_REG 0x3e08
257 #define MVNETA_REFILL_MAX_NUM_CLK 0x0000ffff
258 #define MVNETA_TX_MTU 0x3e0c
259 #define MVNETA_TX_TOKEN_SIZE 0x3e14
260 #define MVNETA_TX_TOKEN_SIZE_MAX 0xffffffff
261 #define MVNETA_TXQ_BUCKET_REFILL_REG(q) (0x3e20 + ((q) << 2))
262 #define MVNETA_TXQ_BUCKET_REFILL_PERIOD_MASK 0x3ff00000
263 #define MVNETA_TXQ_BUCKET_REFILL_PERIOD_SHIFT 20
264 #define MVNETA_TXQ_BUCKET_REFILL_VALUE_MAX 0x0007ffff
265 #define MVNETA_TXQ_TOKEN_SIZE_REG(q) (0x3e40 + ((q) << 2))
266 #define MVNETA_TXQ_TOKEN_SIZE_MAX 0x7fffffff
268 /* The values of the bucket refill base period and refill period are taken from
269 * the reference manual, and adds up to a base resolution of 10Kbps. This allows
270 * to cover all rate-limit values from 10Kbps up to 5Gbps
273 /* Base period for the rate limit algorithm */
274 #define MVNETA_TXQ_BUCKET_REFILL_BASE_PERIOD_NS 100
276 /* Number of Base Period to wait between each bucket refill */
277 #define MVNETA_TXQ_BUCKET_REFILL_PERIOD 1000
279 /* The base resolution for rate limiting, in bps. Any max_rate value should be
280 * a multiple of that value.
282 #define MVNETA_TXQ_RATE_LIMIT_RESOLUTION (NSEC_PER_SEC / \
283 (MVNETA_TXQ_BUCKET_REFILL_BASE_PERIOD_NS * \
284 MVNETA_TXQ_BUCKET_REFILL_PERIOD))
286 #define MVNETA_LPI_CTRL_0 0x2cc0
287 #define MVNETA_LPI_CTRL_1 0x2cc4
288 #define MVNETA_LPI_REQUEST_ENABLE BIT(0)
289 #define MVNETA_LPI_CTRL_2 0x2cc8
290 #define MVNETA_LPI_STATUS 0x2ccc
292 #define MVNETA_CAUSE_TXQ_SENT_DESC_ALL_MASK 0xff
294 /* Descriptor ring Macros */
295 #define MVNETA_QUEUE_NEXT_DESC(q, index) \
296 (((index) < (q)->last_desc) ? ((index) + 1) : 0)
298 /* Various constants */
301 #define MVNETA_TXDONE_COAL_PKTS 0 /* interrupt per packet */
302 #define MVNETA_RX_COAL_PKTS 32
303 #define MVNETA_RX_COAL_USEC 100
305 /* The two bytes Marvell header. Either contains a special value used
306 * by Marvell switches when a specific hardware mode is enabled (not
307 * supported by this driver) or is filled automatically by zeroes on
308 * the RX side. Those two bytes being at the front of the Ethernet
309 * header, they allow to have the IP header aligned on a 4 bytes
310 * boundary automatically: the hardware skips those two bytes on its
313 #define MVNETA_MH_SIZE 2
315 #define MVNETA_VLAN_TAG_LEN 4
317 #define MVNETA_TX_CSUM_DEF_SIZE 1600
318 #define MVNETA_TX_CSUM_MAX_SIZE 9800
319 #define MVNETA_ACC_MODE_EXT1 1
320 #define MVNETA_ACC_MODE_EXT2 2
322 #define MVNETA_MAX_DECODE_WIN 6
324 /* Timeout constants */
325 #define MVNETA_TX_DISABLE_TIMEOUT_MSEC 1000
326 #define MVNETA_RX_DISABLE_TIMEOUT_MSEC 1000
327 #define MVNETA_TX_FIFO_EMPTY_TIMEOUT 10000
329 #define MVNETA_TX_MTU_MAX 0x3ffff
331 /* The RSS lookup table actually has 256 entries but we do not use
334 #define MVNETA_RSS_LU_TABLE_SIZE 1
336 /* Max number of Rx descriptors */
337 #define MVNETA_MAX_RXD 512
339 /* Max number of Tx descriptors */
340 #define MVNETA_MAX_TXD 1024
342 /* Max number of allowed TCP segments for software TSO */
343 #define MVNETA_MAX_TSO_SEGS 100
345 #define MVNETA_MAX_SKB_DESCS (MVNETA_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
347 /* descriptor aligned size */
348 #define MVNETA_DESC_ALIGNED_SIZE 32
350 /* Number of bytes to be taken into account by HW when putting incoming data
351 * to the buffers. It is needed in case NET_SKB_PAD exceeds maximum packet
352 * offset supported in MVNETA_RXQ_CONFIG_REG(q) registers.
354 #define MVNETA_RX_PKT_OFFSET_CORRECTION 64
356 #define MVNETA_RX_PKT_SIZE(mtu) \
357 ALIGN((mtu) + MVNETA_MH_SIZE + MVNETA_VLAN_TAG_LEN + \
358 ETH_HLEN + ETH_FCS_LEN, \
361 /* Driver assumes that the last 3 bits are 0 */
362 #define MVNETA_SKB_HEADROOM ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8)
363 #define MVNETA_SKB_PAD (SKB_DATA_ALIGN(sizeof(struct skb_shared_info) + \
364 MVNETA_SKB_HEADROOM))
365 #define MVNETA_MAX_RX_BUF_SIZE (PAGE_SIZE - MVNETA_SKB_PAD)
367 #define IS_TSO_HEADER(txq, addr) \
368 ((addr >= txq->tso_hdrs_phys) && \
369 (addr < txq->tso_hdrs_phys + txq->size * TSO_HEADER_SIZE))
371 #define MVNETA_RX_GET_BM_POOL_ID(rxd) \
372 (((rxd)->status & MVNETA_RXD_BM_POOL_MASK) >> MVNETA_RXD_BM_POOL_SHIFT)
375 ETHTOOL_STAT_EEE_WAKEUP,
376 ETHTOOL_STAT_SKB_ALLOC_ERR,
377 ETHTOOL_STAT_REFILL_ERR,
378 ETHTOOL_XDP_REDIRECT,
384 ETHTOOL_XDP_XMIT_ERR,
388 struct mvneta_statistic {
389 unsigned short offset;
391 const char name[ETH_GSTRING_LEN];
398 #define MVNETA_XDP_PASS 0
399 #define MVNETA_XDP_DROPPED BIT(0)
400 #define MVNETA_XDP_TX BIT(1)
401 #define MVNETA_XDP_REDIR BIT(2)
403 static const struct mvneta_statistic mvneta_statistics[] = {
404 { 0x3000, T_REG_64, "good_octets_received", },
405 { 0x3010, T_REG_32, "good_frames_received", },
406 { 0x3008, T_REG_32, "bad_octets_received", },
407 { 0x3014, T_REG_32, "bad_frames_received", },
408 { 0x3018, T_REG_32, "broadcast_frames_received", },
409 { 0x301c, T_REG_32, "multicast_frames_received", },
410 { 0x3050, T_REG_32, "unrec_mac_control_received", },
411 { 0x3058, T_REG_32, "good_fc_received", },
412 { 0x305c, T_REG_32, "bad_fc_received", },
413 { 0x3060, T_REG_32, "undersize_received", },
414 { 0x3064, T_REG_32, "fragments_received", },
415 { 0x3068, T_REG_32, "oversize_received", },
416 { 0x306c, T_REG_32, "jabber_received", },
417 { 0x3070, T_REG_32, "mac_receive_error", },
418 { 0x3074, T_REG_32, "bad_crc_event", },
419 { 0x3078, T_REG_32, "collision", },
420 { 0x307c, T_REG_32, "late_collision", },
421 { 0x2484, T_REG_32, "rx_discard", },
422 { 0x2488, T_REG_32, "rx_overrun", },
423 { 0x3020, T_REG_32, "frames_64_octets", },
424 { 0x3024, T_REG_32, "frames_65_to_127_octets", },
425 { 0x3028, T_REG_32, "frames_128_to_255_octets", },
426 { 0x302c, T_REG_32, "frames_256_to_511_octets", },
427 { 0x3030, T_REG_32, "frames_512_to_1023_octets", },
428 { 0x3034, T_REG_32, "frames_1024_to_max_octets", },
429 { 0x3038, T_REG_64, "good_octets_sent", },
430 { 0x3040, T_REG_32, "good_frames_sent", },
431 { 0x3044, T_REG_32, "excessive_collision", },
432 { 0x3048, T_REG_32, "multicast_frames_sent", },
433 { 0x304c, T_REG_32, "broadcast_frames_sent", },
434 { 0x3054, T_REG_32, "fc_sent", },
435 { 0x300c, T_REG_32, "internal_mac_transmit_err", },
436 { ETHTOOL_STAT_EEE_WAKEUP, T_SW, "eee_wakeup_errors", },
437 { ETHTOOL_STAT_SKB_ALLOC_ERR, T_SW, "skb_alloc_errors", },
438 { ETHTOOL_STAT_REFILL_ERR, T_SW, "refill_errors", },
439 { ETHTOOL_XDP_REDIRECT, T_SW, "rx_xdp_redirect", },
440 { ETHTOOL_XDP_PASS, T_SW, "rx_xdp_pass", },
441 { ETHTOOL_XDP_DROP, T_SW, "rx_xdp_drop", },
442 { ETHTOOL_XDP_TX, T_SW, "rx_xdp_tx", },
443 { ETHTOOL_XDP_TX_ERR, T_SW, "rx_xdp_tx_errors", },
444 { ETHTOOL_XDP_XMIT, T_SW, "tx_xdp_xmit", },
445 { ETHTOOL_XDP_XMIT_ERR, T_SW, "tx_xdp_xmit_errors", },
448 struct mvneta_stats {
463 struct mvneta_ethtool_stats {
464 struct mvneta_stats ps;
469 struct mvneta_pcpu_stats {
470 struct u64_stats_sync syncp;
472 struct mvneta_ethtool_stats es;
477 struct mvneta_pcpu_port {
478 /* Pointer to the shared port */
479 struct mvneta_port *pp;
481 /* Pointer to the CPU-local NAPI struct */
482 struct napi_struct napi;
484 /* Cause of the previous interrupt */
494 struct mvneta_pcpu_port __percpu *ports;
495 struct mvneta_pcpu_stats __percpu *stats;
501 struct mvneta_rx_queue *rxqs;
502 struct mvneta_tx_queue *txqs;
503 struct net_device *dev;
504 struct hlist_node node_online;
505 struct hlist_node node_dead;
507 /* Protect the access to the percpu interrupt registers,
508 * ensuring that the configuration remains coherent.
514 struct napi_struct napi;
516 struct bpf_prog *xdp_prog;
526 phy_interface_t phy_interface;
527 struct device_node *dn;
528 unsigned int tx_csum_limit;
529 struct phylink *phylink;
530 struct phylink_config phylink_config;
531 struct phylink_pcs phylink_pcs;
534 struct mvneta_bm *bm_priv;
535 struct mvneta_bm_pool *pool_long;
536 struct mvneta_bm_pool *pool_short;
543 u64 ethtool_stats[ARRAY_SIZE(mvneta_statistics)];
545 u32 indir[MVNETA_RSS_LU_TABLE_SIZE];
547 /* Flags for special SoC configurations */
548 bool neta_armada3700;
550 u16 rx_offset_correction;
551 const struct mbus_dram_target_info *dram_target_info;
554 /* The mvneta_tx_desc and mvneta_rx_desc structures describe the
555 * layout of the transmit and reception DMA descriptors, and their
556 * layout is therefore defined by the hardware design
559 #define MVNETA_TX_L3_OFF_SHIFT 0
560 #define MVNETA_TX_IP_HLEN_SHIFT 8
561 #define MVNETA_TX_L4_UDP BIT(16)
562 #define MVNETA_TX_L3_IP6 BIT(17)
563 #define MVNETA_TXD_IP_CSUM BIT(18)
564 #define MVNETA_TXD_Z_PAD BIT(19)
565 #define MVNETA_TXD_L_DESC BIT(20)
566 #define MVNETA_TXD_F_DESC BIT(21)
567 #define MVNETA_TXD_FLZ_DESC (MVNETA_TXD_Z_PAD | \
568 MVNETA_TXD_L_DESC | \
570 #define MVNETA_TX_L4_CSUM_FULL BIT(30)
571 #define MVNETA_TX_L4_CSUM_NOT BIT(31)
573 #define MVNETA_RXD_ERR_CRC 0x0
574 #define MVNETA_RXD_BM_POOL_SHIFT 13
575 #define MVNETA_RXD_BM_POOL_MASK (BIT(13) | BIT(14))
576 #define MVNETA_RXD_ERR_SUMMARY BIT(16)
577 #define MVNETA_RXD_ERR_OVERRUN BIT(17)
578 #define MVNETA_RXD_ERR_LEN BIT(18)
579 #define MVNETA_RXD_ERR_RESOURCE (BIT(17) | BIT(18))
580 #define MVNETA_RXD_ERR_CODE_MASK (BIT(17) | BIT(18))
581 #define MVNETA_RXD_L3_IP4 BIT(25)
582 #define MVNETA_RXD_LAST_DESC BIT(26)
583 #define MVNETA_RXD_FIRST_DESC BIT(27)
584 #define MVNETA_RXD_FIRST_LAST_DESC (MVNETA_RXD_FIRST_DESC | \
585 MVNETA_RXD_LAST_DESC)
586 #define MVNETA_RXD_L4_CSUM_OK BIT(30)
588 #if defined(__LITTLE_ENDIAN)
589 struct mvneta_tx_desc {
590 u32 command; /* Options used by HW for packet transmitting.*/
591 u16 reserved1; /* csum_l4 (for future use) */
592 u16 data_size; /* Data size of transmitted packet in bytes */
593 u32 buf_phys_addr; /* Physical addr of transmitted buffer */
594 u32 reserved2; /* hw_cmd - (for future use, PMT) */
595 u32 reserved3[4]; /* Reserved - (for future use) */
598 struct mvneta_rx_desc {
599 u32 status; /* Info about received packet */
600 u16 reserved1; /* pnc_info - (for future use, PnC) */
601 u16 data_size; /* Size of received packet in bytes */
603 u32 buf_phys_addr; /* Physical address of the buffer */
604 u32 reserved2; /* pnc_flow_id (for future use, PnC) */
606 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
607 u16 reserved3; /* prefetch_cmd, for future use */
608 u16 reserved4; /* csum_l4 - (for future use, PnC) */
610 u32 reserved5; /* pnc_extra PnC (for future use, PnC) */
611 u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */
614 struct mvneta_tx_desc {
615 u16 data_size; /* Data size of transmitted packet in bytes */
616 u16 reserved1; /* csum_l4 (for future use) */
617 u32 command; /* Options used by HW for packet transmitting.*/
618 u32 reserved2; /* hw_cmd - (for future use, PMT) */
619 u32 buf_phys_addr; /* Physical addr of transmitted buffer */
620 u32 reserved3[4]; /* Reserved - (for future use) */
623 struct mvneta_rx_desc {
624 u16 data_size; /* Size of received packet in bytes */
625 u16 reserved1; /* pnc_info - (for future use, PnC) */
626 u32 status; /* Info about received packet */
628 u32 reserved2; /* pnc_flow_id (for future use, PnC) */
629 u32 buf_phys_addr; /* Physical address of the buffer */
631 u16 reserved4; /* csum_l4 - (for future use, PnC) */
632 u16 reserved3; /* prefetch_cmd, for future use */
633 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
635 u32 reserved5; /* pnc_extra PnC (for future use, PnC) */
636 u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */
640 enum mvneta_tx_buf_type {
646 struct mvneta_tx_buf {
647 enum mvneta_tx_buf_type type;
649 struct xdp_frame *xdpf;
654 struct mvneta_tx_queue {
655 /* Number of this TX queue, in the range 0-7 */
658 /* Number of TX DMA descriptors in the descriptor ring */
661 /* Number of currently used TX DMA descriptor in the
666 int tx_stop_threshold;
667 int tx_wake_threshold;
669 /* Array of transmitted buffers */
670 struct mvneta_tx_buf *buf;
672 /* Index of last TX DMA descriptor that was inserted */
675 /* Index of the TX DMA descriptor to be cleaned up */
680 /* Virtual address of the TX DMA descriptors array */
681 struct mvneta_tx_desc *descs;
683 /* DMA address of the TX DMA descriptors array */
684 dma_addr_t descs_phys;
686 /* Index of the last TX DMA descriptor */
689 /* Index of the next TX DMA descriptor to process */
690 int next_desc_to_proc;
692 /* DMA buffers for TSO headers */
695 /* DMA address of TSO headers */
696 dma_addr_t tso_hdrs_phys;
698 /* Affinity mask for CPUs*/
699 cpumask_t affinity_mask;
702 struct mvneta_rx_queue {
703 /* rx queue number, in the range 0-7 */
706 /* num of rx descriptors in the rx descriptor ring */
713 struct page_pool *page_pool;
714 struct xdp_rxq_info xdp_rxq;
716 /* Virtual address of the RX buffer */
717 void **buf_virt_addr;
719 /* Virtual address of the RX DMA descriptors array */
720 struct mvneta_rx_desc *descs;
722 /* DMA address of the RX DMA descriptors array */
723 dma_addr_t descs_phys;
725 /* Index of the last RX DMA descriptor */
728 /* Index of the next RX DMA descriptor to process */
729 int next_desc_to_proc;
731 /* Index of first RX DMA descriptor to refill */
736 static enum cpuhp_state online_hpstate;
737 /* The hardware supports eight (8) rx queues, but we are only allowing
738 * the first one to be used. Therefore, let's just allocate one queue.
740 static int rxq_number = 8;
741 static int txq_number = 8;
745 static int rx_copybreak __read_mostly = 256;
747 /* HW BM need that each port be identify by a unique ID */
748 static int global_port_id;
750 #define MVNETA_DRIVER_NAME "mvneta"
751 #define MVNETA_DRIVER_VERSION "1.0"
753 /* Utility/helper methods */
755 /* Write helper method */
756 static void mvreg_write(struct mvneta_port *pp, u32 offset, u32 data)
758 writel(data, pp->base + offset);
761 /* Read helper method */
762 static u32 mvreg_read(struct mvneta_port *pp, u32 offset)
764 return readl(pp->base + offset);
767 /* Increment txq get counter */
768 static void mvneta_txq_inc_get(struct mvneta_tx_queue *txq)
770 txq->txq_get_index++;
771 if (txq->txq_get_index == txq->size)
772 txq->txq_get_index = 0;
775 /* Increment txq put counter */
776 static void mvneta_txq_inc_put(struct mvneta_tx_queue *txq)
778 txq->txq_put_index++;
779 if (txq->txq_put_index == txq->size)
780 txq->txq_put_index = 0;
784 /* Clear all MIB counters */
785 static void mvneta_mib_counters_clear(struct mvneta_port *pp)
789 /* Perform dummy reads from MIB counters */
790 for (i = 0; i < MVNETA_MIB_LATE_COLLISION; i += 4)
791 mvreg_read(pp, (MVNETA_MIB_COUNTERS_BASE + i));
792 mvreg_read(pp, MVNETA_RX_DISCARD_FRAME_COUNT);
793 mvreg_read(pp, MVNETA_OVERRUN_FRAME_COUNT);
796 /* Get System Network Statistics */
798 mvneta_get_stats64(struct net_device *dev,
799 struct rtnl_link_stats64 *stats)
801 struct mvneta_port *pp = netdev_priv(dev);
805 for_each_possible_cpu(cpu) {
806 struct mvneta_pcpu_stats *cpu_stats;
814 cpu_stats = per_cpu_ptr(pp->stats, cpu);
816 start = u64_stats_fetch_begin(&cpu_stats->syncp);
817 rx_packets = cpu_stats->es.ps.rx_packets;
818 rx_bytes = cpu_stats->es.ps.rx_bytes;
819 rx_dropped = cpu_stats->rx_dropped;
820 rx_errors = cpu_stats->rx_errors;
821 tx_packets = cpu_stats->es.ps.tx_packets;
822 tx_bytes = cpu_stats->es.ps.tx_bytes;
823 } while (u64_stats_fetch_retry(&cpu_stats->syncp, start));
825 stats->rx_packets += rx_packets;
826 stats->rx_bytes += rx_bytes;
827 stats->rx_dropped += rx_dropped;
828 stats->rx_errors += rx_errors;
829 stats->tx_packets += tx_packets;
830 stats->tx_bytes += tx_bytes;
833 stats->tx_dropped = dev->stats.tx_dropped;
836 /* Rx descriptors helper methods */
838 /* Checks whether the RX descriptor having this status is both the first
839 * and the last descriptor for the RX packet. Each RX packet is currently
840 * received through a single RX descriptor, so not having each RX
841 * descriptor with its first and last bits set is an error
843 static int mvneta_rxq_desc_is_first_last(u32 status)
845 return (status & MVNETA_RXD_FIRST_LAST_DESC) ==
846 MVNETA_RXD_FIRST_LAST_DESC;
849 /* Add number of descriptors ready to receive new packets */
850 static void mvneta_rxq_non_occup_desc_add(struct mvneta_port *pp,
851 struct mvneta_rx_queue *rxq,
854 /* Only MVNETA_RXQ_ADD_NON_OCCUPIED_MAX (255) descriptors can
857 while (ndescs > MVNETA_RXQ_ADD_NON_OCCUPIED_MAX) {
858 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
859 (MVNETA_RXQ_ADD_NON_OCCUPIED_MAX <<
860 MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
861 ndescs -= MVNETA_RXQ_ADD_NON_OCCUPIED_MAX;
864 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
865 (ndescs << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
868 /* Get number of RX descriptors occupied by received packets */
869 static int mvneta_rxq_busy_desc_num_get(struct mvneta_port *pp,
870 struct mvneta_rx_queue *rxq)
874 val = mvreg_read(pp, MVNETA_RXQ_STATUS_REG(rxq->id));
875 return val & MVNETA_RXQ_OCCUPIED_ALL_MASK;
878 /* Update num of rx desc called upon return from rx path or
879 * from mvneta_rxq_drop_pkts().
881 static void mvneta_rxq_desc_num_update(struct mvneta_port *pp,
882 struct mvneta_rx_queue *rxq,
883 int rx_done, int rx_filled)
887 if ((rx_done <= 0xff) && (rx_filled <= 0xff)) {
889 (rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT);
890 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
894 /* Only 255 descriptors can be added at once */
895 while ((rx_done > 0) || (rx_filled > 0)) {
896 if (rx_done <= 0xff) {
903 if (rx_filled <= 0xff) {
904 val |= rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
907 val |= 0xff << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
910 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
914 /* Get pointer to next RX descriptor to be processed by SW */
915 static struct mvneta_rx_desc *
916 mvneta_rxq_next_desc_get(struct mvneta_rx_queue *rxq)
918 int rx_desc = rxq->next_desc_to_proc;
920 rxq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(rxq, rx_desc);
921 prefetch(rxq->descs + rxq->next_desc_to_proc);
922 return rxq->descs + rx_desc;
925 /* Change maximum receive size of the port. */
926 static void mvneta_max_rx_size_set(struct mvneta_port *pp, int max_rx_size)
930 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
931 val &= ~MVNETA_GMAC_MAX_RX_SIZE_MASK;
932 val |= ((max_rx_size - MVNETA_MH_SIZE) / 2) <<
933 MVNETA_GMAC_MAX_RX_SIZE_SHIFT;
934 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
938 /* Set rx queue offset */
939 static void mvneta_rxq_offset_set(struct mvneta_port *pp,
940 struct mvneta_rx_queue *rxq,
945 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
946 val &= ~MVNETA_RXQ_PKT_OFFSET_ALL_MASK;
949 val |= MVNETA_RXQ_PKT_OFFSET_MASK(offset >> 3);
950 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
954 /* Tx descriptors helper methods */
956 /* Update HW with number of TX descriptors to be sent */
957 static void mvneta_txq_pend_desc_add(struct mvneta_port *pp,
958 struct mvneta_tx_queue *txq,
963 pend_desc += txq->pending;
965 /* Only 255 Tx descriptors can be added at once */
967 val = min(pend_desc, 255);
968 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
970 } while (pend_desc > 0);
974 /* Get pointer to next TX descriptor to be processed (send) by HW */
975 static struct mvneta_tx_desc *
976 mvneta_txq_next_desc_get(struct mvneta_tx_queue *txq)
978 int tx_desc = txq->next_desc_to_proc;
980 txq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(txq, tx_desc);
981 return txq->descs + tx_desc;
984 /* Release the last allocated TX descriptor. Useful to handle DMA
985 * mapping failures in the TX path.
987 static void mvneta_txq_desc_put(struct mvneta_tx_queue *txq)
989 if (txq->next_desc_to_proc == 0)
990 txq->next_desc_to_proc = txq->last_desc - 1;
992 txq->next_desc_to_proc--;
995 /* Set rxq buf size */
996 static void mvneta_rxq_buf_size_set(struct mvneta_port *pp,
997 struct mvneta_rx_queue *rxq,
1002 val = mvreg_read(pp, MVNETA_RXQ_SIZE_REG(rxq->id));
1004 val &= ~MVNETA_RXQ_BUF_SIZE_MASK;
1005 val |= ((buf_size >> 3) << MVNETA_RXQ_BUF_SIZE_SHIFT);
1007 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), val);
1010 /* Disable buffer management (BM) */
1011 static void mvneta_rxq_bm_disable(struct mvneta_port *pp,
1012 struct mvneta_rx_queue *rxq)
1016 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
1017 val &= ~MVNETA_RXQ_HW_BUF_ALLOC;
1018 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
1021 /* Enable buffer management (BM) */
1022 static void mvneta_rxq_bm_enable(struct mvneta_port *pp,
1023 struct mvneta_rx_queue *rxq)
1027 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
1028 val |= MVNETA_RXQ_HW_BUF_ALLOC;
1029 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
1032 /* Notify HW about port's assignment of pool for bigger packets */
1033 static void mvneta_rxq_long_pool_set(struct mvneta_port *pp,
1034 struct mvneta_rx_queue *rxq)
1038 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
1039 val &= ~MVNETA_RXQ_LONG_POOL_ID_MASK;
1040 val |= (pp->pool_long->id << MVNETA_RXQ_LONG_POOL_ID_SHIFT);
1042 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
1045 /* Notify HW about port's assignment of pool for smaller packets */
1046 static void mvneta_rxq_short_pool_set(struct mvneta_port *pp,
1047 struct mvneta_rx_queue *rxq)
1051 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
1052 val &= ~MVNETA_RXQ_SHORT_POOL_ID_MASK;
1053 val |= (pp->pool_short->id << MVNETA_RXQ_SHORT_POOL_ID_SHIFT);
1055 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
1058 /* Set port's receive buffer size for assigned BM pool */
1059 static inline void mvneta_bm_pool_bufsize_set(struct mvneta_port *pp,
1065 if (!IS_ALIGNED(buf_size, 8)) {
1066 dev_warn(pp->dev->dev.parent,
1067 "illegal buf_size value %d, round to %d\n",
1068 buf_size, ALIGN(buf_size, 8));
1069 buf_size = ALIGN(buf_size, 8);
1072 val = mvreg_read(pp, MVNETA_PORT_POOL_BUFFER_SZ_REG(pool_id));
1073 val |= buf_size & MVNETA_PORT_POOL_BUFFER_SZ_MASK;
1074 mvreg_write(pp, MVNETA_PORT_POOL_BUFFER_SZ_REG(pool_id), val);
1077 /* Configure MBUS window in order to enable access BM internal SRAM */
1078 static int mvneta_mbus_io_win_set(struct mvneta_port *pp, u32 base, u32 wsize,
1081 u32 win_enable, win_protect;
1084 win_enable = mvreg_read(pp, MVNETA_BASE_ADDR_ENABLE);
1086 if (pp->bm_win_id < 0) {
1087 /* Find first not occupied window */
1088 for (i = 0; i < MVNETA_MAX_DECODE_WIN; i++) {
1089 if (win_enable & (1 << i)) {
1094 if (i == MVNETA_MAX_DECODE_WIN)
1100 mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
1101 mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
1104 mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
1106 mvreg_write(pp, MVNETA_WIN_BASE(i), (base & 0xffff0000) |
1107 (attr << 8) | target);
1109 mvreg_write(pp, MVNETA_WIN_SIZE(i), (wsize - 1) & 0xffff0000);
1111 win_protect = mvreg_read(pp, MVNETA_ACCESS_PROTECT_ENABLE);
1112 win_protect |= 3 << (2 * i);
1113 mvreg_write(pp, MVNETA_ACCESS_PROTECT_ENABLE, win_protect);
1115 win_enable &= ~(1 << i);
1116 mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
1121 static int mvneta_bm_port_mbus_init(struct mvneta_port *pp)
1127 /* Get BM window information */
1128 err = mvebu_mbus_get_io_win_info(pp->bm_priv->bppi_phys_addr, &wsize,
1135 /* Open NETA -> BM window */
1136 err = mvneta_mbus_io_win_set(pp, pp->bm_priv->bppi_phys_addr, wsize,
1139 netdev_info(pp->dev, "fail to configure mbus window to BM\n");
1145 /* Assign and initialize pools for port. In case of fail
1146 * buffer manager will remain disabled for current port.
1148 static int mvneta_bm_port_init(struct platform_device *pdev,
1149 struct mvneta_port *pp)
1151 struct device_node *dn = pdev->dev.of_node;
1152 u32 long_pool_id, short_pool_id;
1154 if (!pp->neta_armada3700) {
1157 ret = mvneta_bm_port_mbus_init(pp);
1162 if (of_property_read_u32(dn, "bm,pool-long", &long_pool_id)) {
1163 netdev_info(pp->dev, "missing long pool id\n");
1167 /* Create port's long pool depending on mtu */
1168 pp->pool_long = mvneta_bm_pool_use(pp->bm_priv, long_pool_id,
1169 MVNETA_BM_LONG, pp->id,
1170 MVNETA_RX_PKT_SIZE(pp->dev->mtu));
1171 if (!pp->pool_long) {
1172 netdev_info(pp->dev, "fail to obtain long pool for port\n");
1176 pp->pool_long->port_map |= 1 << pp->id;
1178 mvneta_bm_pool_bufsize_set(pp, pp->pool_long->buf_size,
1181 /* If short pool id is not defined, assume using single pool */
1182 if (of_property_read_u32(dn, "bm,pool-short", &short_pool_id))
1183 short_pool_id = long_pool_id;
1185 /* Create port's short pool */
1186 pp->pool_short = mvneta_bm_pool_use(pp->bm_priv, short_pool_id,
1187 MVNETA_BM_SHORT, pp->id,
1188 MVNETA_BM_SHORT_PKT_SIZE);
1189 if (!pp->pool_short) {
1190 netdev_info(pp->dev, "fail to obtain short pool for port\n");
1191 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
1195 if (short_pool_id != long_pool_id) {
1196 pp->pool_short->port_map |= 1 << pp->id;
1197 mvneta_bm_pool_bufsize_set(pp, pp->pool_short->buf_size,
1198 pp->pool_short->id);
1204 /* Update settings of a pool for bigger packets */
1205 static void mvneta_bm_update_mtu(struct mvneta_port *pp, int mtu)
1207 struct mvneta_bm_pool *bm_pool = pp->pool_long;
1208 struct hwbm_pool *hwbm_pool = &bm_pool->hwbm_pool;
1211 /* Release all buffers from long pool */
1212 mvneta_bm_bufs_free(pp->bm_priv, bm_pool, 1 << pp->id);
1213 if (hwbm_pool->buf_num) {
1214 WARN(1, "cannot free all buffers in pool %d\n",
1219 bm_pool->pkt_size = MVNETA_RX_PKT_SIZE(mtu);
1220 bm_pool->buf_size = MVNETA_RX_BUF_SIZE(bm_pool->pkt_size);
1221 hwbm_pool->frag_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
1222 SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(bm_pool->pkt_size));
1224 /* Fill entire long pool */
1225 num = hwbm_pool_add(hwbm_pool, hwbm_pool->size);
1226 if (num != hwbm_pool->size) {
1227 WARN(1, "pool %d: %d of %d allocated\n",
1228 bm_pool->id, num, hwbm_pool->size);
1231 mvneta_bm_pool_bufsize_set(pp, bm_pool->buf_size, bm_pool->id);
1236 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
1237 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short, 1 << pp->id);
1240 pp->rx_offset_correction = MVNETA_SKB_HEADROOM;
1241 mvreg_write(pp, MVNETA_ACC_MODE, MVNETA_ACC_MODE_EXT1);
1242 netdev_info(pp->dev, "fail to update MTU, fall back to software BM\n");
1245 /* Start the Ethernet port RX and TX activity */
1246 static void mvneta_port_up(struct mvneta_port *pp)
1251 /* Enable all initialized TXs. */
1253 for (queue = 0; queue < txq_number; queue++) {
1254 struct mvneta_tx_queue *txq = &pp->txqs[queue];
1256 q_map |= (1 << queue);
1258 mvreg_write(pp, MVNETA_TXQ_CMD, q_map);
1261 /* Enable all initialized RXQs. */
1262 for (queue = 0; queue < rxq_number; queue++) {
1263 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
1266 q_map |= (1 << queue);
1268 mvreg_write(pp, MVNETA_RXQ_CMD, q_map);
1271 /* Stop the Ethernet port activity */
1272 static void mvneta_port_down(struct mvneta_port *pp)
1277 /* Stop Rx port activity. Check port Rx activity. */
1278 val = mvreg_read(pp, MVNETA_RXQ_CMD) & MVNETA_RXQ_ENABLE_MASK;
1280 /* Issue stop command for active channels only */
1282 mvreg_write(pp, MVNETA_RXQ_CMD,
1283 val << MVNETA_RXQ_DISABLE_SHIFT);
1285 /* Wait for all Rx activity to terminate. */
1288 if (count++ >= MVNETA_RX_DISABLE_TIMEOUT_MSEC) {
1289 netdev_warn(pp->dev,
1290 "TIMEOUT for RX stopped ! rx_queue_cmd: 0x%08x\n",
1296 val = mvreg_read(pp, MVNETA_RXQ_CMD);
1297 } while (val & MVNETA_RXQ_ENABLE_MASK);
1299 /* Stop Tx port activity. Check port Tx activity. Issue stop
1300 * command for active channels only
1302 val = (mvreg_read(pp, MVNETA_TXQ_CMD)) & MVNETA_TXQ_ENABLE_MASK;
1305 mvreg_write(pp, MVNETA_TXQ_CMD,
1306 (val << MVNETA_TXQ_DISABLE_SHIFT));
1308 /* Wait for all Tx activity to terminate. */
1311 if (count++ >= MVNETA_TX_DISABLE_TIMEOUT_MSEC) {
1312 netdev_warn(pp->dev,
1313 "TIMEOUT for TX stopped status=0x%08x\n",
1319 /* Check TX Command reg that all Txqs are stopped */
1320 val = mvreg_read(pp, MVNETA_TXQ_CMD);
1322 } while (val & MVNETA_TXQ_ENABLE_MASK);
1324 /* Double check to verify that TX FIFO is empty */
1327 if (count++ >= MVNETA_TX_FIFO_EMPTY_TIMEOUT) {
1328 netdev_warn(pp->dev,
1329 "TX FIFO empty timeout status=0x%08x\n",
1335 val = mvreg_read(pp, MVNETA_PORT_STATUS);
1336 } while (!(val & MVNETA_TX_FIFO_EMPTY) &&
1337 (val & MVNETA_TX_IN_PRGRS));
1342 /* Enable the port by setting the port enable bit of the MAC control register */
1343 static void mvneta_port_enable(struct mvneta_port *pp)
1348 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
1349 val |= MVNETA_GMAC0_PORT_ENABLE;
1350 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
1353 /* Disable the port and wait for about 200 usec before retuning */
1354 static void mvneta_port_disable(struct mvneta_port *pp)
1358 /* Reset the Enable bit in the Serial Control Register */
1359 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
1360 val &= ~MVNETA_GMAC0_PORT_ENABLE;
1361 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
1366 /* Multicast tables methods */
1368 /* Set all entries in Unicast MAC Table; queue==-1 means reject all */
1369 static void mvneta_set_ucast_table(struct mvneta_port *pp, int queue)
1377 val = 0x1 | (queue << 1);
1378 val |= (val << 24) | (val << 16) | (val << 8);
1381 for (offset = 0; offset <= 0xc; offset += 4)
1382 mvreg_write(pp, MVNETA_DA_FILT_UCAST_BASE + offset, val);
1385 /* Set all entries in Special Multicast MAC Table; queue==-1 means reject all */
1386 static void mvneta_set_special_mcast_table(struct mvneta_port *pp, int queue)
1394 val = 0x1 | (queue << 1);
1395 val |= (val << 24) | (val << 16) | (val << 8);
1398 for (offset = 0; offset <= 0xfc; offset += 4)
1399 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + offset, val);
1403 /* Set all entries in Other Multicast MAC Table. queue==-1 means reject all */
1404 static void mvneta_set_other_mcast_table(struct mvneta_port *pp, int queue)
1410 memset(pp->mcast_count, 0, sizeof(pp->mcast_count));
1413 memset(pp->mcast_count, 1, sizeof(pp->mcast_count));
1414 val = 0x1 | (queue << 1);
1415 val |= (val << 24) | (val << 16) | (val << 8);
1418 for (offset = 0; offset <= 0xfc; offset += 4)
1419 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + offset, val);
1422 static void mvneta_percpu_unmask_interrupt(void *arg)
1424 struct mvneta_port *pp = arg;
1426 /* All the queue are unmasked, but actually only the ones
1427 * mapped to this CPU will be unmasked
1429 mvreg_write(pp, MVNETA_INTR_NEW_MASK,
1430 MVNETA_RX_INTR_MASK_ALL |
1431 MVNETA_TX_INTR_MASK_ALL |
1432 MVNETA_MISCINTR_INTR_MASK);
1435 static void mvneta_percpu_mask_interrupt(void *arg)
1437 struct mvneta_port *pp = arg;
1439 /* All the queue are masked, but actually only the ones
1440 * mapped to this CPU will be masked
1442 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
1443 mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0);
1444 mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0);
1447 static void mvneta_percpu_clear_intr_cause(void *arg)
1449 struct mvneta_port *pp = arg;
1451 /* All the queue are cleared, but actually only the ones
1452 * mapped to this CPU will be cleared
1454 mvreg_write(pp, MVNETA_INTR_NEW_CAUSE, 0);
1455 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
1456 mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0);
1459 /* This method sets defaults to the NETA port:
1460 * Clears interrupt Cause and Mask registers.
1461 * Clears all MAC tables.
1462 * Sets defaults to all registers.
1463 * Resets RX and TX descriptor rings.
1465 * This method can be called after mvneta_port_down() to return the port
1466 * settings to defaults.
1468 static void mvneta_defaults_set(struct mvneta_port *pp)
1473 int max_cpu = num_present_cpus();
1475 /* Clear all Cause registers */
1476 on_each_cpu(mvneta_percpu_clear_intr_cause, pp, true);
1478 /* Mask all interrupts */
1479 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
1480 mvreg_write(pp, MVNETA_INTR_ENABLE, 0);
1482 /* Enable MBUS Retry bit16 */
1483 mvreg_write(pp, MVNETA_MBUS_RETRY, 0x20);
1485 /* Set CPU queue access map. CPUs are assigned to the RX and
1486 * TX queues modulo their number. If there is only one TX
1487 * queue then it is assigned to the CPU associated to the
1490 for_each_present_cpu(cpu) {
1491 int rxq_map = 0, txq_map = 0;
1493 if (!pp->neta_armada3700) {
1494 for (rxq = 0; rxq < rxq_number; rxq++)
1495 if ((rxq % max_cpu) == cpu)
1496 rxq_map |= MVNETA_CPU_RXQ_ACCESS(rxq);
1498 for (txq = 0; txq < txq_number; txq++)
1499 if ((txq % max_cpu) == cpu)
1500 txq_map |= MVNETA_CPU_TXQ_ACCESS(txq);
1502 /* With only one TX queue we configure a special case
1503 * which will allow to get all the irq on a single
1506 if (txq_number == 1)
1507 txq_map = (cpu == pp->rxq_def) ?
1508 MVNETA_CPU_TXQ_ACCESS(1) : 0;
1511 txq_map = MVNETA_CPU_TXQ_ACCESS_ALL_MASK;
1512 rxq_map = MVNETA_CPU_RXQ_ACCESS_ALL_MASK;
1515 mvreg_write(pp, MVNETA_CPU_MAP(cpu), rxq_map | txq_map);
1518 /* Reset RX and TX DMAs */
1519 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
1520 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
1522 /* Disable Legacy WRR, Disable EJP, Release from reset */
1523 mvreg_write(pp, MVNETA_TXQ_CMD_1, 0);
1524 for (queue = 0; queue < txq_number; queue++) {
1525 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(queue), 0);
1526 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(queue), 0);
1529 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
1530 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
1532 /* Set Port Acceleration Mode */
1534 /* HW buffer management + legacy parser */
1535 val = MVNETA_ACC_MODE_EXT2;
1537 /* SW buffer management + legacy parser */
1538 val = MVNETA_ACC_MODE_EXT1;
1539 mvreg_write(pp, MVNETA_ACC_MODE, val);
1542 mvreg_write(pp, MVNETA_BM_ADDRESS, pp->bm_priv->bppi_phys_addr);
1544 /* Update val of portCfg register accordingly with all RxQueue types */
1545 val = MVNETA_PORT_CONFIG_DEFL_VALUE(pp->rxq_def);
1546 mvreg_write(pp, MVNETA_PORT_CONFIG, val);
1549 mvreg_write(pp, MVNETA_PORT_CONFIG_EXTEND, val);
1550 mvreg_write(pp, MVNETA_RX_MIN_FRAME_SIZE, 64);
1552 /* Build PORT_SDMA_CONFIG_REG */
1555 /* Default burst size */
1556 val |= MVNETA_TX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
1557 val |= MVNETA_RX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
1558 val |= MVNETA_RX_NO_DATA_SWAP | MVNETA_TX_NO_DATA_SWAP;
1560 #if defined(__BIG_ENDIAN)
1561 val |= MVNETA_DESC_SWAP;
1564 /* Assign port SDMA configuration */
1565 mvreg_write(pp, MVNETA_SDMA_CONFIG, val);
1567 /* Disable PHY polling in hardware, since we're using the
1568 * kernel phylib to do this.
1570 val = mvreg_read(pp, MVNETA_UNIT_CONTROL);
1571 val &= ~MVNETA_PHY_POLLING_ENABLE;
1572 mvreg_write(pp, MVNETA_UNIT_CONTROL, val);
1574 mvneta_set_ucast_table(pp, -1);
1575 mvneta_set_special_mcast_table(pp, -1);
1576 mvneta_set_other_mcast_table(pp, -1);
1578 /* Set port interrupt enable register - default enable all */
1579 mvreg_write(pp, MVNETA_INTR_ENABLE,
1580 (MVNETA_RXQ_INTR_ENABLE_ALL_MASK
1581 | MVNETA_TXQ_INTR_ENABLE_ALL_MASK));
1583 mvneta_mib_counters_clear(pp);
1586 /* Set max sizes for tx queues */
1587 static void mvneta_txq_max_tx_size_set(struct mvneta_port *pp, int max_tx_size)
1593 mtu = max_tx_size * 8;
1594 if (mtu > MVNETA_TX_MTU_MAX)
1595 mtu = MVNETA_TX_MTU_MAX;
1598 val = mvreg_read(pp, MVNETA_TX_MTU);
1599 val &= ~MVNETA_TX_MTU_MAX;
1601 mvreg_write(pp, MVNETA_TX_MTU, val);
1603 /* TX token size and all TXQs token size must be larger that MTU */
1604 val = mvreg_read(pp, MVNETA_TX_TOKEN_SIZE);
1606 size = val & MVNETA_TX_TOKEN_SIZE_MAX;
1609 val &= ~MVNETA_TX_TOKEN_SIZE_MAX;
1611 mvreg_write(pp, MVNETA_TX_TOKEN_SIZE, val);
1613 for (queue = 0; queue < txq_number; queue++) {
1614 val = mvreg_read(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue));
1616 size = val & MVNETA_TXQ_TOKEN_SIZE_MAX;
1619 val &= ~MVNETA_TXQ_TOKEN_SIZE_MAX;
1621 mvreg_write(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue), val);
1626 /* Set unicast address */
1627 static void mvneta_set_ucast_addr(struct mvneta_port *pp, u8 last_nibble,
1630 unsigned int unicast_reg;
1631 unsigned int tbl_offset;
1632 unsigned int reg_offset;
1634 /* Locate the Unicast table entry */
1635 last_nibble = (0xf & last_nibble);
1637 /* offset from unicast tbl base */
1638 tbl_offset = (last_nibble / 4) * 4;
1640 /* offset within the above reg */
1641 reg_offset = last_nibble % 4;
1643 unicast_reg = mvreg_read(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset));
1646 /* Clear accepts frame bit at specified unicast DA tbl entry */
1647 unicast_reg &= ~(0xff << (8 * reg_offset));
1649 unicast_reg &= ~(0xff << (8 * reg_offset));
1650 unicast_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
1653 mvreg_write(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset), unicast_reg);
1656 /* Set mac address */
1657 static void mvneta_mac_addr_set(struct mvneta_port *pp,
1658 const unsigned char *addr, int queue)
1664 mac_l = (addr[4] << 8) | (addr[5]);
1665 mac_h = (addr[0] << 24) | (addr[1] << 16) |
1666 (addr[2] << 8) | (addr[3] << 0);
1668 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, mac_l);
1669 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, mac_h);
1672 /* Accept frames of this address */
1673 mvneta_set_ucast_addr(pp, addr[5], queue);
1676 /* Set the number of packets that will be received before RX interrupt
1677 * will be generated by HW.
1679 static void mvneta_rx_pkts_coal_set(struct mvneta_port *pp,
1680 struct mvneta_rx_queue *rxq, u32 value)
1682 mvreg_write(pp, MVNETA_RXQ_THRESHOLD_REG(rxq->id),
1683 value | MVNETA_RXQ_NON_OCCUPIED(0));
1686 /* Set the time delay in usec before RX interrupt will be generated by
1689 static void mvneta_rx_time_coal_set(struct mvneta_port *pp,
1690 struct mvneta_rx_queue *rxq, u32 value)
1693 unsigned long clk_rate;
1695 clk_rate = clk_get_rate(pp->clk);
1696 val = (clk_rate / 1000000) * value;
1698 mvreg_write(pp, MVNETA_RXQ_TIME_COAL_REG(rxq->id), val);
1701 /* Set threshold for TX_DONE pkts coalescing */
1702 static void mvneta_tx_done_pkts_coal_set(struct mvneta_port *pp,
1703 struct mvneta_tx_queue *txq, u32 value)
1707 val = mvreg_read(pp, MVNETA_TXQ_SIZE_REG(txq->id));
1709 val &= ~MVNETA_TXQ_SENT_THRESH_ALL_MASK;
1710 val |= MVNETA_TXQ_SENT_THRESH_MASK(value);
1712 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), val);
1715 /* Handle rx descriptor fill by setting buf_cookie and buf_phys_addr */
1716 static void mvneta_rx_desc_fill(struct mvneta_rx_desc *rx_desc,
1717 u32 phys_addr, void *virt_addr,
1718 struct mvneta_rx_queue *rxq)
1722 rx_desc->buf_phys_addr = phys_addr;
1723 i = rx_desc - rxq->descs;
1724 rxq->buf_virt_addr[i] = virt_addr;
1727 /* Decrement sent descriptors counter */
1728 static void mvneta_txq_sent_desc_dec(struct mvneta_port *pp,
1729 struct mvneta_tx_queue *txq,
1734 /* Only 255 TX descriptors can be updated at once */
1735 while (sent_desc > 0xff) {
1736 val = 0xff << MVNETA_TXQ_DEC_SENT_SHIFT;
1737 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1738 sent_desc = sent_desc - 0xff;
1741 val = sent_desc << MVNETA_TXQ_DEC_SENT_SHIFT;
1742 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1745 /* Get number of TX descriptors already sent by HW */
1746 static int mvneta_txq_sent_desc_num_get(struct mvneta_port *pp,
1747 struct mvneta_tx_queue *txq)
1752 val = mvreg_read(pp, MVNETA_TXQ_STATUS_REG(txq->id));
1753 sent_desc = (val & MVNETA_TXQ_SENT_DESC_MASK) >>
1754 MVNETA_TXQ_SENT_DESC_SHIFT;
1759 /* Get number of sent descriptors and decrement counter.
1760 * The number of sent descriptors is returned.
1762 static int mvneta_txq_sent_desc_proc(struct mvneta_port *pp,
1763 struct mvneta_tx_queue *txq)
1767 /* Get number of sent descriptors */
1768 sent_desc = mvneta_txq_sent_desc_num_get(pp, txq);
1770 /* Decrement sent descriptors counter */
1772 mvneta_txq_sent_desc_dec(pp, txq, sent_desc);
1777 /* Set TXQ descriptors fields relevant for CSUM calculation */
1778 static u32 mvneta_txq_desc_csum(int l3_offs, int l3_proto,
1779 int ip_hdr_len, int l4_proto)
1783 /* Fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
1784 * G_L4_chk, L4_type; required only for checksum
1787 command = l3_offs << MVNETA_TX_L3_OFF_SHIFT;
1788 command |= ip_hdr_len << MVNETA_TX_IP_HLEN_SHIFT;
1790 if (l3_proto == htons(ETH_P_IP))
1791 command |= MVNETA_TXD_IP_CSUM;
1793 command |= MVNETA_TX_L3_IP6;
1795 if (l4_proto == IPPROTO_TCP)
1796 command |= MVNETA_TX_L4_CSUM_FULL;
1797 else if (l4_proto == IPPROTO_UDP)
1798 command |= MVNETA_TX_L4_UDP | MVNETA_TX_L4_CSUM_FULL;
1800 command |= MVNETA_TX_L4_CSUM_NOT;
1806 /* Display more error info */
1807 static void mvneta_rx_error(struct mvneta_port *pp,
1808 struct mvneta_rx_desc *rx_desc)
1810 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
1811 u32 status = rx_desc->status;
1813 /* update per-cpu counter */
1814 u64_stats_update_begin(&stats->syncp);
1816 u64_stats_update_end(&stats->syncp);
1818 switch (status & MVNETA_RXD_ERR_CODE_MASK) {
1819 case MVNETA_RXD_ERR_CRC:
1820 netdev_err(pp->dev, "bad rx status %08x (crc error), size=%d\n",
1821 status, rx_desc->data_size);
1823 case MVNETA_RXD_ERR_OVERRUN:
1824 netdev_err(pp->dev, "bad rx status %08x (overrun error), size=%d\n",
1825 status, rx_desc->data_size);
1827 case MVNETA_RXD_ERR_LEN:
1828 netdev_err(pp->dev, "bad rx status %08x (max frame length error), size=%d\n",
1829 status, rx_desc->data_size);
1831 case MVNETA_RXD_ERR_RESOURCE:
1832 netdev_err(pp->dev, "bad rx status %08x (resource error), size=%d\n",
1833 status, rx_desc->data_size);
1838 /* Handle RX checksum offload based on the descriptor's status */
1839 static int mvneta_rx_csum(struct mvneta_port *pp, u32 status)
1841 if ((pp->dev->features & NETIF_F_RXCSUM) &&
1842 (status & MVNETA_RXD_L3_IP4) &&
1843 (status & MVNETA_RXD_L4_CSUM_OK))
1844 return CHECKSUM_UNNECESSARY;
1846 return CHECKSUM_NONE;
1849 /* Return tx queue pointer (find last set bit) according to <cause> returned
1850 * form tx_done reg. <cause> must not be null. The return value is always a
1851 * valid queue for matching the first one found in <cause>.
1853 static struct mvneta_tx_queue *mvneta_tx_done_policy(struct mvneta_port *pp,
1856 int queue = fls(cause) - 1;
1858 return &pp->txqs[queue];
1861 /* Free tx queue skbuffs */
1862 static void mvneta_txq_bufs_free(struct mvneta_port *pp,
1863 struct mvneta_tx_queue *txq, int num,
1864 struct netdev_queue *nq, bool napi)
1866 unsigned int bytes_compl = 0, pkts_compl = 0;
1867 struct xdp_frame_bulk bq;
1870 xdp_frame_bulk_init(&bq);
1872 rcu_read_lock(); /* need for xdp_return_frame_bulk */
1874 for (i = 0; i < num; i++) {
1875 struct mvneta_tx_buf *buf = &txq->buf[txq->txq_get_index];
1876 struct mvneta_tx_desc *tx_desc = txq->descs +
1879 mvneta_txq_inc_get(txq);
1881 if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr) &&
1882 buf->type != MVNETA_TYPE_XDP_TX)
1883 dma_unmap_single(pp->dev->dev.parent,
1884 tx_desc->buf_phys_addr,
1885 tx_desc->data_size, DMA_TO_DEVICE);
1886 if (buf->type == MVNETA_TYPE_SKB && buf->skb) {
1887 bytes_compl += buf->skb->len;
1889 dev_kfree_skb_any(buf->skb);
1890 } else if ((buf->type == MVNETA_TYPE_XDP_TX ||
1891 buf->type == MVNETA_TYPE_XDP_NDO) && buf->xdpf) {
1892 if (napi && buf->type == MVNETA_TYPE_XDP_TX)
1893 xdp_return_frame_rx_napi(buf->xdpf);
1895 xdp_return_frame_bulk(buf->xdpf, &bq);
1898 xdp_flush_frame_bulk(&bq);
1902 netdev_tx_completed_queue(nq, pkts_compl, bytes_compl);
1905 /* Handle end of transmission */
1906 static void mvneta_txq_done(struct mvneta_port *pp,
1907 struct mvneta_tx_queue *txq)
1909 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
1912 tx_done = mvneta_txq_sent_desc_proc(pp, txq);
1916 mvneta_txq_bufs_free(pp, txq, tx_done, nq, true);
1918 txq->count -= tx_done;
1920 if (netif_tx_queue_stopped(nq)) {
1921 if (txq->count <= txq->tx_wake_threshold)
1922 netif_tx_wake_queue(nq);
1926 /* Refill processing for SW buffer management */
1927 /* Allocate page per descriptor */
1928 static int mvneta_rx_refill(struct mvneta_port *pp,
1929 struct mvneta_rx_desc *rx_desc,
1930 struct mvneta_rx_queue *rxq,
1933 dma_addr_t phys_addr;
1936 page = page_pool_alloc_pages(rxq->page_pool,
1937 gfp_mask | __GFP_NOWARN);
1941 phys_addr = page_pool_get_dma_addr(page) + pp->rx_offset_correction;
1942 mvneta_rx_desc_fill(rx_desc, phys_addr, page, rxq);
1947 /* Handle tx checksum */
1948 static u32 mvneta_skb_tx_csum(struct sk_buff *skb)
1950 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1952 __be16 l3_proto = vlan_get_protocol(skb);
1955 if (l3_proto == htons(ETH_P_IP)) {
1956 struct iphdr *ip4h = ip_hdr(skb);
1958 /* Calculate IPv4 checksum and L4 checksum */
1959 ip_hdr_len = ip4h->ihl;
1960 l4_proto = ip4h->protocol;
1961 } else if (l3_proto == htons(ETH_P_IPV6)) {
1962 struct ipv6hdr *ip6h = ipv6_hdr(skb);
1964 /* Read l4_protocol from one of IPv6 extra headers */
1965 if (skb_network_header_len(skb) > 0)
1966 ip_hdr_len = (skb_network_header_len(skb) >> 2);
1967 l4_proto = ip6h->nexthdr;
1969 return MVNETA_TX_L4_CSUM_NOT;
1971 return mvneta_txq_desc_csum(skb_network_offset(skb),
1972 l3_proto, ip_hdr_len, l4_proto);
1975 return MVNETA_TX_L4_CSUM_NOT;
1978 /* Drop packets received by the RXQ and free buffers */
1979 static void mvneta_rxq_drop_pkts(struct mvneta_port *pp,
1980 struct mvneta_rx_queue *rxq)
1984 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
1986 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
1989 for (i = 0; i < rx_done; i++) {
1990 struct mvneta_rx_desc *rx_desc =
1991 mvneta_rxq_next_desc_get(rxq);
1992 u8 pool_id = MVNETA_RX_GET_BM_POOL_ID(rx_desc);
1993 struct mvneta_bm_pool *bm_pool;
1995 bm_pool = &pp->bm_priv->bm_pools[pool_id];
1996 /* Return dropped buffer to the pool */
1997 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
1998 rx_desc->buf_phys_addr);
2003 for (i = 0; i < rxq->size; i++) {
2004 struct mvneta_rx_desc *rx_desc = rxq->descs + i;
2005 void *data = rxq->buf_virt_addr[i];
2006 if (!data || !(rx_desc->buf_phys_addr))
2009 page_pool_put_full_page(rxq->page_pool, data, false);
2011 if (xdp_rxq_info_is_reg(&rxq->xdp_rxq))
2012 xdp_rxq_info_unreg(&rxq->xdp_rxq);
2013 page_pool_destroy(rxq->page_pool);
2014 rxq->page_pool = NULL;
2018 mvneta_update_stats(struct mvneta_port *pp,
2019 struct mvneta_stats *ps)
2021 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
2023 u64_stats_update_begin(&stats->syncp);
2024 stats->es.ps.rx_packets += ps->rx_packets;
2025 stats->es.ps.rx_bytes += ps->rx_bytes;
2027 stats->es.ps.xdp_redirect += ps->xdp_redirect;
2028 stats->es.ps.xdp_pass += ps->xdp_pass;
2029 stats->es.ps.xdp_drop += ps->xdp_drop;
2030 u64_stats_update_end(&stats->syncp);
2034 int mvneta_rx_refill_queue(struct mvneta_port *pp, struct mvneta_rx_queue *rxq)
2036 struct mvneta_rx_desc *rx_desc;
2037 int curr_desc = rxq->first_to_refill;
2040 for (i = 0; (i < rxq->refill_num) && (i < 64); i++) {
2041 rx_desc = rxq->descs + curr_desc;
2042 if (!(rx_desc->buf_phys_addr)) {
2043 if (mvneta_rx_refill(pp, rx_desc, rxq, GFP_ATOMIC)) {
2044 struct mvneta_pcpu_stats *stats;
2046 pr_err("Can't refill queue %d. Done %d from %d\n",
2047 rxq->id, i, rxq->refill_num);
2049 stats = this_cpu_ptr(pp->stats);
2050 u64_stats_update_begin(&stats->syncp);
2051 stats->es.refill_error++;
2052 u64_stats_update_end(&stats->syncp);
2056 curr_desc = MVNETA_QUEUE_NEXT_DESC(rxq, curr_desc);
2058 rxq->refill_num -= i;
2059 rxq->first_to_refill = curr_desc;
2065 mvneta_xdp_put_buff(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
2066 struct xdp_buff *xdp, int sync_len)
2068 struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
2071 if (likely(!xdp_buff_has_frags(xdp)))
2074 for (i = 0; i < sinfo->nr_frags; i++)
2075 page_pool_put_full_page(rxq->page_pool,
2076 skb_frag_page(&sinfo->frags[i]), true);
2079 page_pool_put_page(rxq->page_pool, virt_to_head_page(xdp->data),
2084 mvneta_xdp_submit_frame(struct mvneta_port *pp, struct mvneta_tx_queue *txq,
2085 struct xdp_frame *xdpf, int *nxmit_byte, bool dma_map)
2087 struct skb_shared_info *sinfo = xdp_get_shared_info_from_frame(xdpf);
2088 struct device *dev = pp->dev->dev.parent;
2089 struct mvneta_tx_desc *tx_desc;
2090 int i, num_frames = 1;
2093 if (unlikely(xdp_frame_has_frags(xdpf)))
2094 num_frames += sinfo->nr_frags;
2096 if (txq->count + num_frames >= txq->size)
2097 return MVNETA_XDP_DROPPED;
2099 for (i = 0; i < num_frames; i++) {
2100 struct mvneta_tx_buf *buf = &txq->buf[txq->txq_put_index];
2101 skb_frag_t *frag = NULL;
2102 int len = xdpf->len;
2103 dma_addr_t dma_addr;
2105 if (unlikely(i)) { /* paged area */
2106 frag = &sinfo->frags[i - 1];
2107 len = skb_frag_size(frag);
2110 tx_desc = mvneta_txq_next_desc_get(txq);
2115 data = unlikely(frag) ? skb_frag_address(frag)
2117 dma_addr = dma_map_single(dev, data, len,
2119 if (dma_mapping_error(dev, dma_addr)) {
2120 mvneta_txq_desc_put(txq);
2124 buf->type = MVNETA_TYPE_XDP_NDO;
2126 page = unlikely(frag) ? skb_frag_page(frag)
2127 : virt_to_page(xdpf->data);
2128 dma_addr = page_pool_get_dma_addr(page);
2130 dma_addr += skb_frag_off(frag);
2132 dma_addr += sizeof(*xdpf) + xdpf->headroom;
2133 dma_sync_single_for_device(dev, dma_addr, len,
2135 buf->type = MVNETA_TYPE_XDP_TX;
2137 buf->xdpf = unlikely(i) ? NULL : xdpf;
2139 tx_desc->command = unlikely(i) ? 0 : MVNETA_TXD_F_DESC;
2140 tx_desc->buf_phys_addr = dma_addr;
2141 tx_desc->data_size = len;
2144 mvneta_txq_inc_put(txq);
2146 /*last descriptor */
2147 tx_desc->command |= MVNETA_TXD_L_DESC | MVNETA_TXD_Z_PAD;
2149 txq->pending += num_frames;
2150 txq->count += num_frames;
2152 return MVNETA_XDP_TX;
2155 for (i--; i >= 0; i--) {
2156 mvneta_txq_desc_put(txq);
2157 tx_desc = txq->descs + txq->next_desc_to_proc;
2158 dma_unmap_single(dev, tx_desc->buf_phys_addr,
2163 return MVNETA_XDP_DROPPED;
2167 mvneta_xdp_xmit_back(struct mvneta_port *pp, struct xdp_buff *xdp)
2169 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
2170 struct mvneta_tx_queue *txq;
2171 struct netdev_queue *nq;
2172 int cpu, nxmit_byte = 0;
2173 struct xdp_frame *xdpf;
2176 xdpf = xdp_convert_buff_to_frame(xdp);
2177 if (unlikely(!xdpf))
2178 return MVNETA_XDP_DROPPED;
2180 cpu = smp_processor_id();
2181 txq = &pp->txqs[cpu % txq_number];
2182 nq = netdev_get_tx_queue(pp->dev, txq->id);
2184 __netif_tx_lock(nq, cpu);
2185 ret = mvneta_xdp_submit_frame(pp, txq, xdpf, &nxmit_byte, false);
2186 if (ret == MVNETA_XDP_TX) {
2187 u64_stats_update_begin(&stats->syncp);
2188 stats->es.ps.tx_bytes += nxmit_byte;
2189 stats->es.ps.tx_packets++;
2190 stats->es.ps.xdp_tx++;
2191 u64_stats_update_end(&stats->syncp);
2193 mvneta_txq_pend_desc_add(pp, txq, 0);
2195 u64_stats_update_begin(&stats->syncp);
2196 stats->es.ps.xdp_tx_err++;
2197 u64_stats_update_end(&stats->syncp);
2199 __netif_tx_unlock(nq);
2205 mvneta_xdp_xmit(struct net_device *dev, int num_frame,
2206 struct xdp_frame **frames, u32 flags)
2208 struct mvneta_port *pp = netdev_priv(dev);
2209 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
2210 int i, nxmit_byte = 0, nxmit = 0;
2211 int cpu = smp_processor_id();
2212 struct mvneta_tx_queue *txq;
2213 struct netdev_queue *nq;
2216 if (unlikely(test_bit(__MVNETA_DOWN, &pp->state)))
2219 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
2222 txq = &pp->txqs[cpu % txq_number];
2223 nq = netdev_get_tx_queue(pp->dev, txq->id);
2225 __netif_tx_lock(nq, cpu);
2226 for (i = 0; i < num_frame; i++) {
2227 ret = mvneta_xdp_submit_frame(pp, txq, frames[i], &nxmit_byte,
2229 if (ret != MVNETA_XDP_TX)
2235 if (unlikely(flags & XDP_XMIT_FLUSH))
2236 mvneta_txq_pend_desc_add(pp, txq, 0);
2237 __netif_tx_unlock(nq);
2239 u64_stats_update_begin(&stats->syncp);
2240 stats->es.ps.tx_bytes += nxmit_byte;
2241 stats->es.ps.tx_packets += nxmit;
2242 stats->es.ps.xdp_xmit += nxmit;
2243 stats->es.ps.xdp_xmit_err += num_frame - nxmit;
2244 u64_stats_update_end(&stats->syncp);
2250 mvneta_run_xdp(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
2251 struct bpf_prog *prog, struct xdp_buff *xdp,
2252 u32 frame_sz, struct mvneta_stats *stats)
2254 unsigned int len, data_len, sync;
2257 len = xdp->data_end - xdp->data_hard_start - pp->rx_offset_correction;
2258 data_len = xdp->data_end - xdp->data;
2259 act = bpf_prog_run_xdp(prog, xdp);
2261 /* Due xdp_adjust_tail: DMA sync for_device cover max len CPU touch */
2262 sync = xdp->data_end - xdp->data_hard_start - pp->rx_offset_correction;
2263 sync = max(sync, len);
2268 return MVNETA_XDP_PASS;
2269 case XDP_REDIRECT: {
2272 err = xdp_do_redirect(pp->dev, xdp, prog);
2273 if (unlikely(err)) {
2274 mvneta_xdp_put_buff(pp, rxq, xdp, sync);
2275 ret = MVNETA_XDP_DROPPED;
2277 ret = MVNETA_XDP_REDIR;
2278 stats->xdp_redirect++;
2283 ret = mvneta_xdp_xmit_back(pp, xdp);
2284 if (ret != MVNETA_XDP_TX)
2285 mvneta_xdp_put_buff(pp, rxq, xdp, sync);
2288 bpf_warn_invalid_xdp_action(pp->dev, prog, act);
2291 trace_xdp_exception(pp->dev, prog, act);
2294 mvneta_xdp_put_buff(pp, rxq, xdp, sync);
2295 ret = MVNETA_XDP_DROPPED;
2300 stats->rx_bytes += frame_sz + xdp->data_end - xdp->data - data_len;
2301 stats->rx_packets++;
2307 mvneta_swbm_rx_frame(struct mvneta_port *pp,
2308 struct mvneta_rx_desc *rx_desc,
2309 struct mvneta_rx_queue *rxq,
2310 struct xdp_buff *xdp, int *size,
2313 unsigned char *data = page_address(page);
2314 int data_len = -MVNETA_MH_SIZE, len;
2315 struct net_device *dev = pp->dev;
2316 enum dma_data_direction dma_dir;
2318 if (*size > MVNETA_MAX_RX_BUF_SIZE) {
2319 len = MVNETA_MAX_RX_BUF_SIZE;
2323 data_len += len - ETH_FCS_LEN;
2325 *size = *size - len;
2327 dma_dir = page_pool_get_dma_dir(rxq->page_pool);
2328 dma_sync_single_for_cpu(dev->dev.parent,
2329 rx_desc->buf_phys_addr,
2332 rx_desc->buf_phys_addr = 0;
2334 /* Prefetch header */
2336 xdp_buff_clear_frags_flag(xdp);
2337 xdp_prepare_buff(xdp, data, pp->rx_offset_correction + MVNETA_MH_SIZE,
2342 mvneta_swbm_add_rx_fragment(struct mvneta_port *pp,
2343 struct mvneta_rx_desc *rx_desc,
2344 struct mvneta_rx_queue *rxq,
2345 struct xdp_buff *xdp, int *size,
2348 struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
2349 struct net_device *dev = pp->dev;
2350 enum dma_data_direction dma_dir;
2353 if (*size > MVNETA_MAX_RX_BUF_SIZE) {
2354 len = MVNETA_MAX_RX_BUF_SIZE;
2358 data_len = len - ETH_FCS_LEN;
2360 dma_dir = page_pool_get_dma_dir(rxq->page_pool);
2361 dma_sync_single_for_cpu(dev->dev.parent,
2362 rx_desc->buf_phys_addr,
2364 rx_desc->buf_phys_addr = 0;
2366 if (!xdp_buff_has_frags(xdp))
2367 sinfo->nr_frags = 0;
2369 if (data_len > 0 && sinfo->nr_frags < MAX_SKB_FRAGS) {
2370 skb_frag_t *frag = &sinfo->frags[sinfo->nr_frags++];
2372 skb_frag_off_set(frag, pp->rx_offset_correction);
2373 skb_frag_size_set(frag, data_len);
2374 __skb_frag_set_page(frag, page);
2376 if (!xdp_buff_has_frags(xdp)) {
2377 sinfo->xdp_frags_size = *size;
2378 xdp_buff_set_frags_flag(xdp);
2380 if (page_is_pfmemalloc(page))
2381 xdp_buff_set_frag_pfmemalloc(xdp);
2383 page_pool_put_full_page(rxq->page_pool, page, true);
2388 static struct sk_buff *
2389 mvneta_swbm_build_skb(struct mvneta_port *pp, struct page_pool *pool,
2390 struct xdp_buff *xdp, u32 desc_status)
2392 struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
2393 struct sk_buff *skb;
2396 if (unlikely(xdp_buff_has_frags(xdp)))
2397 num_frags = sinfo->nr_frags;
2399 skb = build_skb(xdp->data_hard_start, PAGE_SIZE);
2401 return ERR_PTR(-ENOMEM);
2403 skb_mark_for_recycle(skb);
2405 skb_reserve(skb, xdp->data - xdp->data_hard_start);
2406 skb_put(skb, xdp->data_end - xdp->data);
2407 skb->ip_summed = mvneta_rx_csum(pp, desc_status);
2409 if (unlikely(xdp_buff_has_frags(xdp)))
2410 xdp_update_skb_shared_info(skb, num_frags,
2411 sinfo->xdp_frags_size,
2412 num_frags * xdp->frame_sz,
2413 xdp_buff_is_frag_pfmemalloc(xdp));
2418 /* Main rx processing when using software buffer management */
2419 static int mvneta_rx_swbm(struct napi_struct *napi,
2420 struct mvneta_port *pp, int budget,
2421 struct mvneta_rx_queue *rxq)
2423 int rx_proc = 0, rx_todo, refill, size = 0;
2424 struct net_device *dev = pp->dev;
2425 struct mvneta_stats ps = {};
2426 struct bpf_prog *xdp_prog;
2427 u32 desc_status, frame_sz;
2428 struct xdp_buff xdp_buf;
2430 xdp_init_buff(&xdp_buf, PAGE_SIZE, &rxq->xdp_rxq);
2431 xdp_buf.data_hard_start = NULL;
2433 /* Get number of received packets */
2434 rx_todo = mvneta_rxq_busy_desc_num_get(pp, rxq);
2436 xdp_prog = READ_ONCE(pp->xdp_prog);
2438 /* Fairness NAPI loop */
2439 while (rx_proc < budget && rx_proc < rx_todo) {
2440 struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
2441 u32 rx_status, index;
2442 struct sk_buff *skb;
2445 index = rx_desc - rxq->descs;
2446 page = (struct page *)rxq->buf_virt_addr[index];
2448 rx_status = rx_desc->status;
2452 if (rx_status & MVNETA_RXD_FIRST_DESC) {
2453 /* Check errors only for FIRST descriptor */
2454 if (rx_status & MVNETA_RXD_ERR_SUMMARY) {
2455 mvneta_rx_error(pp, rx_desc);
2459 size = rx_desc->data_size;
2460 frame_sz = size - ETH_FCS_LEN;
2461 desc_status = rx_status;
2463 mvneta_swbm_rx_frame(pp, rx_desc, rxq, &xdp_buf,
2466 if (unlikely(!xdp_buf.data_hard_start)) {
2467 rx_desc->buf_phys_addr = 0;
2468 page_pool_put_full_page(rxq->page_pool, page,
2473 mvneta_swbm_add_rx_fragment(pp, rx_desc, rxq, &xdp_buf,
2475 } /* Middle or Last descriptor */
2477 if (!(rx_status & MVNETA_RXD_LAST_DESC))
2478 /* no last descriptor this time */
2482 mvneta_xdp_put_buff(pp, rxq, &xdp_buf, -1);
2487 mvneta_run_xdp(pp, rxq, xdp_prog, &xdp_buf, frame_sz, &ps))
2490 skb = mvneta_swbm_build_skb(pp, rxq->page_pool, &xdp_buf, desc_status);
2492 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
2494 mvneta_xdp_put_buff(pp, rxq, &xdp_buf, -1);
2496 u64_stats_update_begin(&stats->syncp);
2497 stats->es.skb_alloc_error++;
2498 stats->rx_dropped++;
2499 u64_stats_update_end(&stats->syncp);
2504 ps.rx_bytes += skb->len;
2507 skb->protocol = eth_type_trans(skb, dev);
2508 napi_gro_receive(napi, skb);
2510 xdp_buf.data_hard_start = NULL;
2513 if (xdp_buf.data_hard_start)
2514 mvneta_xdp_put_buff(pp, rxq, &xdp_buf, -1);
2516 if (ps.xdp_redirect)
2520 mvneta_update_stats(pp, &ps);
2522 /* return some buffers to hardware queue, one at a time is too slow */
2523 refill = mvneta_rx_refill_queue(pp, rxq);
2525 /* Update rxq management counters */
2526 mvneta_rxq_desc_num_update(pp, rxq, rx_proc, refill);
2528 return ps.rx_packets;
2531 /* Main rx processing when using hardware buffer management */
2532 static int mvneta_rx_hwbm(struct napi_struct *napi,
2533 struct mvneta_port *pp, int rx_todo,
2534 struct mvneta_rx_queue *rxq)
2536 struct net_device *dev = pp->dev;
2541 /* Get number of received packets */
2542 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
2544 if (rx_todo > rx_done)
2549 /* Fairness NAPI loop */
2550 while (rx_done < rx_todo) {
2551 struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
2552 struct mvneta_bm_pool *bm_pool = NULL;
2553 struct sk_buff *skb;
2554 unsigned char *data;
2555 dma_addr_t phys_addr;
2556 u32 rx_status, frag_size;
2561 rx_status = rx_desc->status;
2562 rx_bytes = rx_desc->data_size - (ETH_FCS_LEN + MVNETA_MH_SIZE);
2563 data = (u8 *)(uintptr_t)rx_desc->buf_cookie;
2564 phys_addr = rx_desc->buf_phys_addr;
2565 pool_id = MVNETA_RX_GET_BM_POOL_ID(rx_desc);
2566 bm_pool = &pp->bm_priv->bm_pools[pool_id];
2568 if (!mvneta_rxq_desc_is_first_last(rx_status) ||
2569 (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
2570 err_drop_frame_ret_pool:
2571 /* Return the buffer to the pool */
2572 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
2573 rx_desc->buf_phys_addr);
2575 mvneta_rx_error(pp, rx_desc);
2576 /* leave the descriptor untouched */
2580 if (rx_bytes <= rx_copybreak) {
2581 /* better copy a small frame and not unmap the DMA region */
2582 skb = netdev_alloc_skb_ip_align(dev, rx_bytes);
2584 goto err_drop_frame_ret_pool;
2586 dma_sync_single_range_for_cpu(&pp->bm_priv->pdev->dev,
2587 rx_desc->buf_phys_addr,
2588 MVNETA_MH_SIZE + NET_SKB_PAD,
2591 skb_put_data(skb, data + MVNETA_MH_SIZE + NET_SKB_PAD,
2594 skb->protocol = eth_type_trans(skb, dev);
2595 skb->ip_summed = mvneta_rx_csum(pp, rx_status);
2596 napi_gro_receive(napi, skb);
2599 rcvd_bytes += rx_bytes;
2601 /* Return the buffer to the pool */
2602 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
2603 rx_desc->buf_phys_addr);
2605 /* leave the descriptor and buffer untouched */
2609 /* Refill processing */
2610 err = hwbm_pool_refill(&bm_pool->hwbm_pool, GFP_ATOMIC);
2612 struct mvneta_pcpu_stats *stats;
2614 netdev_err(dev, "Linux processing - Can't refill\n");
2616 stats = this_cpu_ptr(pp->stats);
2617 u64_stats_update_begin(&stats->syncp);
2618 stats->es.refill_error++;
2619 u64_stats_update_end(&stats->syncp);
2621 goto err_drop_frame_ret_pool;
2624 frag_size = bm_pool->hwbm_pool.frag_size;
2626 skb = build_skb(data, frag_size > PAGE_SIZE ? 0 : frag_size);
2628 /* After refill old buffer has to be unmapped regardless
2629 * the skb is successfully built or not.
2631 dma_unmap_single(&pp->bm_priv->pdev->dev, phys_addr,
2632 bm_pool->buf_size, DMA_FROM_DEVICE);
2634 goto err_drop_frame;
2637 rcvd_bytes += rx_bytes;
2639 /* Linux processing */
2640 skb_reserve(skb, MVNETA_MH_SIZE + NET_SKB_PAD);
2641 skb_put(skb, rx_bytes);
2643 skb->protocol = eth_type_trans(skb, dev);
2644 skb->ip_summed = mvneta_rx_csum(pp, rx_status);
2646 napi_gro_receive(napi, skb);
2650 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
2652 u64_stats_update_begin(&stats->syncp);
2653 stats->es.ps.rx_packets += rcvd_pkts;
2654 stats->es.ps.rx_bytes += rcvd_bytes;
2655 u64_stats_update_end(&stats->syncp);
2658 /* Update rxq management counters */
2659 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
2665 mvneta_tso_put_hdr(struct sk_buff *skb, struct mvneta_tx_queue *txq)
2667 struct mvneta_tx_buf *buf = &txq->buf[txq->txq_put_index];
2668 int hdr_len = skb_tcp_all_headers(skb);
2669 struct mvneta_tx_desc *tx_desc;
2671 tx_desc = mvneta_txq_next_desc_get(txq);
2672 tx_desc->data_size = hdr_len;
2673 tx_desc->command = mvneta_skb_tx_csum(skb);
2674 tx_desc->command |= MVNETA_TXD_F_DESC;
2675 tx_desc->buf_phys_addr = txq->tso_hdrs_phys +
2676 txq->txq_put_index * TSO_HEADER_SIZE;
2677 buf->type = MVNETA_TYPE_SKB;
2680 mvneta_txq_inc_put(txq);
2684 mvneta_tso_put_data(struct net_device *dev, struct mvneta_tx_queue *txq,
2685 struct sk_buff *skb, char *data, int size,
2686 bool last_tcp, bool is_last)
2688 struct mvneta_tx_buf *buf = &txq->buf[txq->txq_put_index];
2689 struct mvneta_tx_desc *tx_desc;
2691 tx_desc = mvneta_txq_next_desc_get(txq);
2692 tx_desc->data_size = size;
2693 tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, data,
2694 size, DMA_TO_DEVICE);
2695 if (unlikely(dma_mapping_error(dev->dev.parent,
2696 tx_desc->buf_phys_addr))) {
2697 mvneta_txq_desc_put(txq);
2701 tx_desc->command = 0;
2702 buf->type = MVNETA_TYPE_SKB;
2706 /* last descriptor in the TCP packet */
2707 tx_desc->command = MVNETA_TXD_L_DESC;
2709 /* last descriptor in SKB */
2713 mvneta_txq_inc_put(txq);
2717 static int mvneta_tx_tso(struct sk_buff *skb, struct net_device *dev,
2718 struct mvneta_tx_queue *txq)
2720 int hdr_len, total_len, data_left;
2722 struct mvneta_port *pp = netdev_priv(dev);
2726 /* Count needed descriptors */
2727 if ((txq->count + tso_count_descs(skb)) >= txq->size)
2730 if (skb_headlen(skb) < skb_tcp_all_headers(skb)) {
2731 pr_info("*** Is this even possible?\n");
2735 /* Initialize the TSO handler, and prepare the first payload */
2736 hdr_len = tso_start(skb, &tso);
2738 total_len = skb->len - hdr_len;
2739 while (total_len > 0) {
2742 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
2743 total_len -= data_left;
2746 /* prepare packet headers: MAC + IP + TCP */
2747 hdr = txq->tso_hdrs + txq->txq_put_index * TSO_HEADER_SIZE;
2748 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
2750 mvneta_tso_put_hdr(skb, txq);
2752 while (data_left > 0) {
2756 size = min_t(int, tso.size, data_left);
2758 if (mvneta_tso_put_data(dev, txq, skb,
2765 tso_build_data(skb, &tso, size);
2772 /* Release all used data descriptors; header descriptors must not
2775 for (i = desc_count - 1; i >= 0; i--) {
2776 struct mvneta_tx_desc *tx_desc = txq->descs + i;
2777 if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr))
2778 dma_unmap_single(pp->dev->dev.parent,
2779 tx_desc->buf_phys_addr,
2782 mvneta_txq_desc_put(txq);
2787 /* Handle tx fragmentation processing */
2788 static int mvneta_tx_frag_process(struct mvneta_port *pp, struct sk_buff *skb,
2789 struct mvneta_tx_queue *txq)
2791 struct mvneta_tx_desc *tx_desc;
2792 int i, nr_frags = skb_shinfo(skb)->nr_frags;
2794 for (i = 0; i < nr_frags; i++) {
2795 struct mvneta_tx_buf *buf = &txq->buf[txq->txq_put_index];
2796 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2797 void *addr = skb_frag_address(frag);
2799 tx_desc = mvneta_txq_next_desc_get(txq);
2800 tx_desc->data_size = skb_frag_size(frag);
2802 tx_desc->buf_phys_addr =
2803 dma_map_single(pp->dev->dev.parent, addr,
2804 tx_desc->data_size, DMA_TO_DEVICE);
2806 if (dma_mapping_error(pp->dev->dev.parent,
2807 tx_desc->buf_phys_addr)) {
2808 mvneta_txq_desc_put(txq);
2812 if (i == nr_frags - 1) {
2813 /* Last descriptor */
2814 tx_desc->command = MVNETA_TXD_L_DESC | MVNETA_TXD_Z_PAD;
2817 /* Descriptor in the middle: Not First, Not Last */
2818 tx_desc->command = 0;
2821 buf->type = MVNETA_TYPE_SKB;
2822 mvneta_txq_inc_put(txq);
2828 /* Release all descriptors that were used to map fragments of
2829 * this packet, as well as the corresponding DMA mappings
2831 for (i = i - 1; i >= 0; i--) {
2832 tx_desc = txq->descs + i;
2833 dma_unmap_single(pp->dev->dev.parent,
2834 tx_desc->buf_phys_addr,
2837 mvneta_txq_desc_put(txq);
2843 /* Main tx processing */
2844 static netdev_tx_t mvneta_tx(struct sk_buff *skb, struct net_device *dev)
2846 struct mvneta_port *pp = netdev_priv(dev);
2847 u16 txq_id = skb_get_queue_mapping(skb);
2848 struct mvneta_tx_queue *txq = &pp->txqs[txq_id];
2849 struct mvneta_tx_buf *buf = &txq->buf[txq->txq_put_index];
2850 struct mvneta_tx_desc *tx_desc;
2855 if (!netif_running(dev))
2858 if (skb_is_gso(skb)) {
2859 frags = mvneta_tx_tso(skb, dev, txq);
2863 frags = skb_shinfo(skb)->nr_frags + 1;
2865 /* Get a descriptor for the first part of the packet */
2866 tx_desc = mvneta_txq_next_desc_get(txq);
2868 tx_cmd = mvneta_skb_tx_csum(skb);
2870 tx_desc->data_size = skb_headlen(skb);
2872 tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, skb->data,
2875 if (unlikely(dma_mapping_error(dev->dev.parent,
2876 tx_desc->buf_phys_addr))) {
2877 mvneta_txq_desc_put(txq);
2882 buf->type = MVNETA_TYPE_SKB;
2884 /* First and Last descriptor */
2885 tx_cmd |= MVNETA_TXD_FLZ_DESC;
2886 tx_desc->command = tx_cmd;
2888 mvneta_txq_inc_put(txq);
2890 /* First but not Last */
2891 tx_cmd |= MVNETA_TXD_F_DESC;
2893 mvneta_txq_inc_put(txq);
2894 tx_desc->command = tx_cmd;
2895 /* Continue with other skb fragments */
2896 if (mvneta_tx_frag_process(pp, skb, txq)) {
2897 dma_unmap_single(dev->dev.parent,
2898 tx_desc->buf_phys_addr,
2901 mvneta_txq_desc_put(txq);
2909 struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
2910 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
2912 netdev_tx_sent_queue(nq, len);
2914 txq->count += frags;
2915 if (txq->count >= txq->tx_stop_threshold)
2916 netif_tx_stop_queue(nq);
2918 if (!netdev_xmit_more() || netif_xmit_stopped(nq) ||
2919 txq->pending + frags > MVNETA_TXQ_DEC_SENT_MASK)
2920 mvneta_txq_pend_desc_add(pp, txq, frags);
2922 txq->pending += frags;
2924 u64_stats_update_begin(&stats->syncp);
2925 stats->es.ps.tx_bytes += len;
2926 stats->es.ps.tx_packets++;
2927 u64_stats_update_end(&stats->syncp);
2929 dev->stats.tx_dropped++;
2930 dev_kfree_skb_any(skb);
2933 return NETDEV_TX_OK;
2937 /* Free tx resources, when resetting a port */
2938 static void mvneta_txq_done_force(struct mvneta_port *pp,
2939 struct mvneta_tx_queue *txq)
2942 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
2943 int tx_done = txq->count;
2945 mvneta_txq_bufs_free(pp, txq, tx_done, nq, false);
2949 txq->txq_put_index = 0;
2950 txq->txq_get_index = 0;
2953 /* Handle tx done - called in softirq context. The <cause_tx_done> argument
2954 * must be a valid cause according to MVNETA_TXQ_INTR_MASK_ALL.
2956 static void mvneta_tx_done_gbe(struct mvneta_port *pp, u32 cause_tx_done)
2958 struct mvneta_tx_queue *txq;
2959 struct netdev_queue *nq;
2960 int cpu = smp_processor_id();
2962 while (cause_tx_done) {
2963 txq = mvneta_tx_done_policy(pp, cause_tx_done);
2965 nq = netdev_get_tx_queue(pp->dev, txq->id);
2966 __netif_tx_lock(nq, cpu);
2969 mvneta_txq_done(pp, txq);
2971 __netif_tx_unlock(nq);
2972 cause_tx_done &= ~((1 << txq->id));
2976 /* Compute crc8 of the specified address, using a unique algorithm ,
2977 * according to hw spec, different than generic crc8 algorithm
2979 static int mvneta_addr_crc(unsigned char *addr)
2984 for (i = 0; i < ETH_ALEN; i++) {
2987 crc = (crc ^ addr[i]) << 8;
2988 for (j = 7; j >= 0; j--) {
2989 if (crc & (0x100 << j))
2997 /* This method controls the net device special MAC multicast support.
2998 * The Special Multicast Table for MAC addresses supports MAC of the form
2999 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
3000 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
3001 * Table entries in the DA-Filter table. This method set the Special
3002 * Multicast Table appropriate entry.
3004 static void mvneta_set_special_mcast_addr(struct mvneta_port *pp,
3005 unsigned char last_byte,
3008 unsigned int smc_table_reg;
3009 unsigned int tbl_offset;
3010 unsigned int reg_offset;
3012 /* Register offset from SMC table base */
3013 tbl_offset = (last_byte / 4);
3014 /* Entry offset within the above reg */
3015 reg_offset = last_byte % 4;
3017 smc_table_reg = mvreg_read(pp, (MVNETA_DA_FILT_SPEC_MCAST
3021 smc_table_reg &= ~(0xff << (8 * reg_offset));
3023 smc_table_reg &= ~(0xff << (8 * reg_offset));
3024 smc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
3027 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + tbl_offset * 4,
3031 /* This method controls the network device Other MAC multicast support.
3032 * The Other Multicast Table is used for multicast of another type.
3033 * A CRC-8 is used as an index to the Other Multicast Table entries
3034 * in the DA-Filter table.
3035 * The method gets the CRC-8 value from the calling routine and
3036 * sets the Other Multicast Table appropriate entry according to the
3039 static void mvneta_set_other_mcast_addr(struct mvneta_port *pp,
3043 unsigned int omc_table_reg;
3044 unsigned int tbl_offset;
3045 unsigned int reg_offset;
3047 tbl_offset = (crc8 / 4) * 4; /* Register offset from OMC table base */
3048 reg_offset = crc8 % 4; /* Entry offset within the above reg */
3050 omc_table_reg = mvreg_read(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset);
3053 /* Clear accepts frame bit at specified Other DA table entry */
3054 omc_table_reg &= ~(0xff << (8 * reg_offset));
3056 omc_table_reg &= ~(0xff << (8 * reg_offset));
3057 omc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
3060 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset, omc_table_reg);
3063 /* The network device supports multicast using two tables:
3064 * 1) Special Multicast Table for MAC addresses of the form
3065 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
3066 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
3067 * Table entries in the DA-Filter table.
3068 * 2) Other Multicast Table for multicast of another type. A CRC-8 value
3069 * is used as an index to the Other Multicast Table entries in the
3072 static int mvneta_mcast_addr_set(struct mvneta_port *pp, unsigned char *p_addr,
3075 unsigned char crc_result = 0;
3077 if (memcmp(p_addr, "\x01\x00\x5e\x00\x00", 5) == 0) {
3078 mvneta_set_special_mcast_addr(pp, p_addr[5], queue);
3082 crc_result = mvneta_addr_crc(p_addr);
3084 if (pp->mcast_count[crc_result] == 0) {
3085 netdev_info(pp->dev, "No valid Mcast for crc8=0x%02x\n",
3090 pp->mcast_count[crc_result]--;
3091 if (pp->mcast_count[crc_result] != 0) {
3092 netdev_info(pp->dev,
3093 "After delete there are %d valid Mcast for crc8=0x%02x\n",
3094 pp->mcast_count[crc_result], crc_result);
3098 pp->mcast_count[crc_result]++;
3100 mvneta_set_other_mcast_addr(pp, crc_result, queue);
3105 /* Configure Fitering mode of Ethernet port */
3106 static void mvneta_rx_unicast_promisc_set(struct mvneta_port *pp,
3109 u32 port_cfg_reg, val;
3111 port_cfg_reg = mvreg_read(pp, MVNETA_PORT_CONFIG);
3113 val = mvreg_read(pp, MVNETA_TYPE_PRIO);
3115 /* Set / Clear UPM bit in port configuration register */
3117 /* Accept all Unicast addresses */
3118 port_cfg_reg |= MVNETA_UNI_PROMISC_MODE;
3119 val |= MVNETA_FORCE_UNI;
3120 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, 0xffff);
3121 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, 0xffffffff);
3123 /* Reject all Unicast addresses */
3124 port_cfg_reg &= ~MVNETA_UNI_PROMISC_MODE;
3125 val &= ~MVNETA_FORCE_UNI;
3128 mvreg_write(pp, MVNETA_PORT_CONFIG, port_cfg_reg);
3129 mvreg_write(pp, MVNETA_TYPE_PRIO, val);
3132 /* register unicast and multicast addresses */
3133 static void mvneta_set_rx_mode(struct net_device *dev)
3135 struct mvneta_port *pp = netdev_priv(dev);
3136 struct netdev_hw_addr *ha;
3138 if (dev->flags & IFF_PROMISC) {
3139 /* Accept all: Multicast + Unicast */
3140 mvneta_rx_unicast_promisc_set(pp, 1);
3141 mvneta_set_ucast_table(pp, pp->rxq_def);
3142 mvneta_set_special_mcast_table(pp, pp->rxq_def);
3143 mvneta_set_other_mcast_table(pp, pp->rxq_def);
3145 /* Accept single Unicast */
3146 mvneta_rx_unicast_promisc_set(pp, 0);
3147 mvneta_set_ucast_table(pp, -1);
3148 mvneta_mac_addr_set(pp, dev->dev_addr, pp->rxq_def);
3150 if (dev->flags & IFF_ALLMULTI) {
3151 /* Accept all multicast */
3152 mvneta_set_special_mcast_table(pp, pp->rxq_def);
3153 mvneta_set_other_mcast_table(pp, pp->rxq_def);
3155 /* Accept only initialized multicast */
3156 mvneta_set_special_mcast_table(pp, -1);
3157 mvneta_set_other_mcast_table(pp, -1);
3159 if (!netdev_mc_empty(dev)) {
3160 netdev_for_each_mc_addr(ha, dev) {
3161 mvneta_mcast_addr_set(pp, ha->addr,
3169 /* Interrupt handling - the callback for request_irq() */
3170 static irqreturn_t mvneta_isr(int irq, void *dev_id)
3172 struct mvneta_port *pp = (struct mvneta_port *)dev_id;
3174 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
3175 napi_schedule(&pp->napi);
3180 /* Interrupt handling - the callback for request_percpu_irq() */
3181 static irqreturn_t mvneta_percpu_isr(int irq, void *dev_id)
3183 struct mvneta_pcpu_port *port = (struct mvneta_pcpu_port *)dev_id;
3185 disable_percpu_irq(port->pp->dev->irq);
3186 napi_schedule(&port->napi);
3191 static void mvneta_link_change(struct mvneta_port *pp)
3193 u32 gmac_stat = mvreg_read(pp, MVNETA_GMAC_STATUS);
3195 phylink_mac_change(pp->phylink, !!(gmac_stat & MVNETA_GMAC_LINK_UP));
3199 * Bits 0 - 7 of the causeRxTx register indicate that are transmitted
3200 * packets on the corresponding TXQ (Bit 0 is for TX queue 1).
3201 * Bits 8 -15 of the cause Rx Tx register indicate that are received
3202 * packets on the corresponding RXQ (Bit 8 is for RX queue 0).
3203 * Each CPU has its own causeRxTx register
3205 static int mvneta_poll(struct napi_struct *napi, int budget)
3210 struct mvneta_port *pp = netdev_priv(napi->dev);
3211 struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
3213 if (!netif_running(pp->dev)) {
3214 napi_complete(napi);
3218 /* Read cause register */
3219 cause_rx_tx = mvreg_read(pp, MVNETA_INTR_NEW_CAUSE);
3220 if (cause_rx_tx & MVNETA_MISCINTR_INTR_MASK) {
3221 u32 cause_misc = mvreg_read(pp, MVNETA_INTR_MISC_CAUSE);
3223 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
3225 if (cause_misc & (MVNETA_CAUSE_PHY_STATUS_CHANGE |
3226 MVNETA_CAUSE_LINK_CHANGE))
3227 mvneta_link_change(pp);
3230 /* Release Tx descriptors */
3231 if (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL) {
3232 mvneta_tx_done_gbe(pp, (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL));
3233 cause_rx_tx &= ~MVNETA_TX_INTR_MASK_ALL;
3236 /* For the case where the last mvneta_poll did not process all
3239 cause_rx_tx |= pp->neta_armada3700 ? pp->cause_rx_tx :
3242 rx_queue = fls(((cause_rx_tx >> 8) & 0xff));
3244 rx_queue = rx_queue - 1;
3246 rx_done = mvneta_rx_hwbm(napi, pp, budget,
3247 &pp->rxqs[rx_queue]);
3249 rx_done = mvneta_rx_swbm(napi, pp, budget,
3250 &pp->rxqs[rx_queue]);
3253 if (rx_done < budget) {
3255 napi_complete_done(napi, rx_done);
3257 if (pp->neta_armada3700) {
3258 unsigned long flags;
3260 local_irq_save(flags);
3261 mvreg_write(pp, MVNETA_INTR_NEW_MASK,
3262 MVNETA_RX_INTR_MASK(rxq_number) |
3263 MVNETA_TX_INTR_MASK(txq_number) |
3264 MVNETA_MISCINTR_INTR_MASK);
3265 local_irq_restore(flags);
3267 enable_percpu_irq(pp->dev->irq, 0);
3271 if (pp->neta_armada3700)
3272 pp->cause_rx_tx = cause_rx_tx;
3274 port->cause_rx_tx = cause_rx_tx;
3279 static int mvneta_create_page_pool(struct mvneta_port *pp,
3280 struct mvneta_rx_queue *rxq, int size)
3282 struct bpf_prog *xdp_prog = READ_ONCE(pp->xdp_prog);
3283 struct page_pool_params pp_params = {
3285 .flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV,
3287 .nid = NUMA_NO_NODE,
3288 .dev = pp->dev->dev.parent,
3289 .dma_dir = xdp_prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE,
3290 .offset = pp->rx_offset_correction,
3291 .max_len = MVNETA_MAX_RX_BUF_SIZE,
3295 rxq->page_pool = page_pool_create(&pp_params);
3296 if (IS_ERR(rxq->page_pool)) {
3297 err = PTR_ERR(rxq->page_pool);
3298 rxq->page_pool = NULL;
3302 err = __xdp_rxq_info_reg(&rxq->xdp_rxq, pp->dev, rxq->id, 0,
3307 err = xdp_rxq_info_reg_mem_model(&rxq->xdp_rxq, MEM_TYPE_PAGE_POOL,
3310 goto err_unregister_rxq;
3315 xdp_rxq_info_unreg(&rxq->xdp_rxq);
3317 page_pool_destroy(rxq->page_pool);
3318 rxq->page_pool = NULL;
3322 /* Handle rxq fill: allocates rxq skbs; called when initializing a port */
3323 static int mvneta_rxq_fill(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
3328 err = mvneta_create_page_pool(pp, rxq, num);
3332 for (i = 0; i < num; i++) {
3333 memset(rxq->descs + i, 0, sizeof(struct mvneta_rx_desc));
3334 if (mvneta_rx_refill(pp, rxq->descs + i, rxq,
3337 "%s:rxq %d, %d of %d buffs filled\n",
3338 __func__, rxq->id, i, num);
3343 /* Add this number of RX descriptors as non occupied (ready to
3346 mvneta_rxq_non_occup_desc_add(pp, rxq, i);
3351 /* Free all packets pending transmit from all TXQs and reset TX port */
3352 static void mvneta_tx_reset(struct mvneta_port *pp)
3356 /* free the skb's in the tx ring */
3357 for (queue = 0; queue < txq_number; queue++)
3358 mvneta_txq_done_force(pp, &pp->txqs[queue]);
3360 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
3361 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
3364 static void mvneta_rx_reset(struct mvneta_port *pp)
3366 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
3367 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
3370 /* Rx/Tx queue initialization/cleanup methods */
3372 static int mvneta_rxq_sw_init(struct mvneta_port *pp,
3373 struct mvneta_rx_queue *rxq)
3375 rxq->size = pp->rx_ring_size;
3377 /* Allocate memory for RX descriptors */
3378 rxq->descs = dma_alloc_coherent(pp->dev->dev.parent,
3379 rxq->size * MVNETA_DESC_ALIGNED_SIZE,
3380 &rxq->descs_phys, GFP_KERNEL);
3384 rxq->last_desc = rxq->size - 1;
3389 static void mvneta_rxq_hw_init(struct mvneta_port *pp,
3390 struct mvneta_rx_queue *rxq)
3392 /* Set Rx descriptors queue starting address */
3393 mvreg_write(pp, MVNETA_RXQ_BASE_ADDR_REG(rxq->id), rxq->descs_phys);
3394 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), rxq->size);
3396 /* Set coalescing pkts and time */
3397 mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
3398 mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
3402 mvneta_rxq_offset_set(pp, rxq, 0);
3403 mvneta_rxq_buf_size_set(pp, rxq, PAGE_SIZE < SZ_64K ?
3404 MVNETA_MAX_RX_BUF_SIZE :
3405 MVNETA_RX_BUF_SIZE(pp->pkt_size));
3406 mvneta_rxq_bm_disable(pp, rxq);
3407 mvneta_rxq_fill(pp, rxq, rxq->size);
3410 mvneta_rxq_offset_set(pp, rxq,
3411 NET_SKB_PAD - pp->rx_offset_correction);
3413 mvneta_rxq_bm_enable(pp, rxq);
3414 /* Fill RXQ with buffers from RX pool */
3415 mvneta_rxq_long_pool_set(pp, rxq);
3416 mvneta_rxq_short_pool_set(pp, rxq);
3417 mvneta_rxq_non_occup_desc_add(pp, rxq, rxq->size);
3421 /* Create a specified RX queue */
3422 static int mvneta_rxq_init(struct mvneta_port *pp,
3423 struct mvneta_rx_queue *rxq)
3428 ret = mvneta_rxq_sw_init(pp, rxq);
3432 mvneta_rxq_hw_init(pp, rxq);
3437 /* Cleanup Rx queue */
3438 static void mvneta_rxq_deinit(struct mvneta_port *pp,
3439 struct mvneta_rx_queue *rxq)
3441 mvneta_rxq_drop_pkts(pp, rxq);
3444 dma_free_coherent(pp->dev->dev.parent,
3445 rxq->size * MVNETA_DESC_ALIGNED_SIZE,
3451 rxq->next_desc_to_proc = 0;
3452 rxq->descs_phys = 0;
3453 rxq->first_to_refill = 0;
3454 rxq->refill_num = 0;
3457 static int mvneta_txq_sw_init(struct mvneta_port *pp,
3458 struct mvneta_tx_queue *txq)
3462 txq->size = pp->tx_ring_size;
3464 /* A queue must always have room for at least one skb.
3465 * Therefore, stop the queue when the free entries reaches
3466 * the maximum number of descriptors per skb.
3468 txq->tx_stop_threshold = txq->size - MVNETA_MAX_SKB_DESCS;
3469 txq->tx_wake_threshold = txq->tx_stop_threshold / 2;
3471 /* Allocate memory for TX descriptors */
3472 txq->descs = dma_alloc_coherent(pp->dev->dev.parent,
3473 txq->size * MVNETA_DESC_ALIGNED_SIZE,
3474 &txq->descs_phys, GFP_KERNEL);
3478 txq->last_desc = txq->size - 1;
3480 txq->buf = kmalloc_array(txq->size, sizeof(*txq->buf), GFP_KERNEL);
3484 /* Allocate DMA buffers for TSO MAC/IP/TCP headers */
3485 txq->tso_hdrs = dma_alloc_coherent(pp->dev->dev.parent,
3486 txq->size * TSO_HEADER_SIZE,
3487 &txq->tso_hdrs_phys, GFP_KERNEL);
3491 /* Setup XPS mapping */
3492 if (pp->neta_armada3700)
3494 else if (txq_number > 1)
3495 cpu = txq->id % num_present_cpus();
3497 cpu = pp->rxq_def % num_present_cpus();
3498 cpumask_set_cpu(cpu, &txq->affinity_mask);
3499 netif_set_xps_queue(pp->dev, &txq->affinity_mask, txq->id);
3504 static void mvneta_txq_hw_init(struct mvneta_port *pp,
3505 struct mvneta_tx_queue *txq)
3507 /* Set maximum bandwidth for enabled TXQs */
3508 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0x03ffffff);
3509 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0x3fffffff);
3511 /* Set Tx descriptors queue starting address */
3512 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), txq->descs_phys);
3513 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), txq->size);
3515 mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
3518 /* Create and initialize a tx queue */
3519 static int mvneta_txq_init(struct mvneta_port *pp,
3520 struct mvneta_tx_queue *txq)
3524 ret = mvneta_txq_sw_init(pp, txq);
3528 mvneta_txq_hw_init(pp, txq);
3533 /* Free allocated resources when mvneta_txq_init() fails to allocate memory*/
3534 static void mvneta_txq_sw_deinit(struct mvneta_port *pp,
3535 struct mvneta_tx_queue *txq)
3537 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
3542 dma_free_coherent(pp->dev->dev.parent,
3543 txq->size * TSO_HEADER_SIZE,
3544 txq->tso_hdrs, txq->tso_hdrs_phys);
3546 dma_free_coherent(pp->dev->dev.parent,
3547 txq->size * MVNETA_DESC_ALIGNED_SIZE,
3548 txq->descs, txq->descs_phys);
3550 netdev_tx_reset_queue(nq);
3553 txq->tso_hdrs = NULL;
3556 txq->next_desc_to_proc = 0;
3557 txq->descs_phys = 0;
3560 static void mvneta_txq_hw_deinit(struct mvneta_port *pp,
3561 struct mvneta_tx_queue *txq)
3563 /* Set minimum bandwidth for disabled TXQs */
3564 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0);
3565 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0);
3567 /* Set Tx descriptors queue starting address and size */
3568 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), 0);
3569 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), 0);
3572 static void mvneta_txq_deinit(struct mvneta_port *pp,
3573 struct mvneta_tx_queue *txq)
3575 mvneta_txq_sw_deinit(pp, txq);
3576 mvneta_txq_hw_deinit(pp, txq);
3579 /* Cleanup all Tx queues */
3580 static void mvneta_cleanup_txqs(struct mvneta_port *pp)
3584 for (queue = 0; queue < txq_number; queue++)
3585 mvneta_txq_deinit(pp, &pp->txqs[queue]);
3588 /* Cleanup all Rx queues */
3589 static void mvneta_cleanup_rxqs(struct mvneta_port *pp)
3593 for (queue = 0; queue < rxq_number; queue++)
3594 mvneta_rxq_deinit(pp, &pp->rxqs[queue]);
3598 /* Init all Rx queues */
3599 static int mvneta_setup_rxqs(struct mvneta_port *pp)
3603 for (queue = 0; queue < rxq_number; queue++) {
3604 int err = mvneta_rxq_init(pp, &pp->rxqs[queue]);
3607 netdev_err(pp->dev, "%s: can't create rxq=%d\n",
3609 mvneta_cleanup_rxqs(pp);
3617 /* Init all tx queues */
3618 static int mvneta_setup_txqs(struct mvneta_port *pp)
3622 for (queue = 0; queue < txq_number; queue++) {
3623 int err = mvneta_txq_init(pp, &pp->txqs[queue]);
3625 netdev_err(pp->dev, "%s: can't create txq=%d\n",
3627 mvneta_cleanup_txqs(pp);
3635 static int mvneta_comphy_init(struct mvneta_port *pp, phy_interface_t interface)
3639 ret = phy_set_mode_ext(pp->comphy, PHY_MODE_ETHERNET, interface);
3643 return phy_power_on(pp->comphy);
3646 static int mvneta_config_interface(struct mvneta_port *pp,
3647 phy_interface_t interface)
3652 if (interface == PHY_INTERFACE_MODE_SGMII ||
3653 interface == PHY_INTERFACE_MODE_1000BASEX ||
3654 interface == PHY_INTERFACE_MODE_2500BASEX) {
3655 ret = mvneta_comphy_init(pp, interface);
3658 switch (interface) {
3659 case PHY_INTERFACE_MODE_QSGMII:
3660 mvreg_write(pp, MVNETA_SERDES_CFG,
3661 MVNETA_QSGMII_SERDES_PROTO);
3664 case PHY_INTERFACE_MODE_SGMII:
3665 case PHY_INTERFACE_MODE_1000BASEX:
3666 mvreg_write(pp, MVNETA_SERDES_CFG,
3667 MVNETA_SGMII_SERDES_PROTO);
3670 case PHY_INTERFACE_MODE_2500BASEX:
3671 mvreg_write(pp, MVNETA_SERDES_CFG,
3672 MVNETA_HSGMII_SERDES_PROTO);
3679 pp->phy_interface = interface;
3684 static void mvneta_start_dev(struct mvneta_port *pp)
3688 WARN_ON(mvneta_config_interface(pp, pp->phy_interface));
3690 mvneta_max_rx_size_set(pp, pp->pkt_size);
3691 mvneta_txq_max_tx_size_set(pp, pp->pkt_size);
3693 /* start the Rx/Tx activity */
3694 mvneta_port_enable(pp);
3696 if (!pp->neta_armada3700) {
3697 /* Enable polling on the port */
3698 for_each_online_cpu(cpu) {
3699 struct mvneta_pcpu_port *port =
3700 per_cpu_ptr(pp->ports, cpu);
3702 napi_enable(&port->napi);
3705 napi_enable(&pp->napi);
3708 /* Unmask interrupts. It has to be done from each CPU */
3709 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
3711 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
3712 MVNETA_CAUSE_PHY_STATUS_CHANGE |
3713 MVNETA_CAUSE_LINK_CHANGE);
3715 phylink_start(pp->phylink);
3717 /* We may have called phylink_speed_down before */
3718 phylink_speed_up(pp->phylink);
3720 netif_tx_start_all_queues(pp->dev);
3722 clear_bit(__MVNETA_DOWN, &pp->state);
3725 static void mvneta_stop_dev(struct mvneta_port *pp)
3729 set_bit(__MVNETA_DOWN, &pp->state);
3731 if (device_may_wakeup(&pp->dev->dev))
3732 phylink_speed_down(pp->phylink, false);
3734 phylink_stop(pp->phylink);
3736 if (!pp->neta_armada3700) {
3737 for_each_online_cpu(cpu) {
3738 struct mvneta_pcpu_port *port =
3739 per_cpu_ptr(pp->ports, cpu);
3741 napi_disable(&port->napi);
3744 napi_disable(&pp->napi);
3747 netif_carrier_off(pp->dev);
3749 mvneta_port_down(pp);
3750 netif_tx_stop_all_queues(pp->dev);
3752 /* Stop the port activity */
3753 mvneta_port_disable(pp);
3755 /* Clear all ethernet port interrupts */
3756 on_each_cpu(mvneta_percpu_clear_intr_cause, pp, true);
3758 /* Mask all ethernet port interrupts */
3759 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
3761 mvneta_tx_reset(pp);
3762 mvneta_rx_reset(pp);
3764 WARN_ON(phy_power_off(pp->comphy));
3767 static void mvneta_percpu_enable(void *arg)
3769 struct mvneta_port *pp = arg;
3771 enable_percpu_irq(pp->dev->irq, IRQ_TYPE_NONE);
3774 static void mvneta_percpu_disable(void *arg)
3776 struct mvneta_port *pp = arg;
3778 disable_percpu_irq(pp->dev->irq);
3781 /* Change the device mtu */
3782 static int mvneta_change_mtu(struct net_device *dev, int mtu)
3784 struct mvneta_port *pp = netdev_priv(dev);
3785 struct bpf_prog *prog = pp->xdp_prog;
3788 if (!IS_ALIGNED(MVNETA_RX_PKT_SIZE(mtu), 8)) {
3789 netdev_info(dev, "Illegal MTU value %d, rounding to %d\n",
3790 mtu, ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8));
3791 mtu = ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8);
3794 if (prog && !prog->aux->xdp_has_frags &&
3795 mtu > MVNETA_MAX_RX_BUF_SIZE) {
3796 netdev_info(dev, "Illegal MTU %d for XDP prog without frags\n",
3804 if (!netif_running(dev)) {
3806 mvneta_bm_update_mtu(pp, mtu);
3808 netdev_update_features(dev);
3812 /* The interface is running, so we have to force a
3813 * reallocation of the queues
3815 mvneta_stop_dev(pp);
3816 on_each_cpu(mvneta_percpu_disable, pp, true);
3818 mvneta_cleanup_txqs(pp);
3819 mvneta_cleanup_rxqs(pp);
3822 mvneta_bm_update_mtu(pp, mtu);
3824 pp->pkt_size = MVNETA_RX_PKT_SIZE(dev->mtu);
3826 ret = mvneta_setup_rxqs(pp);
3828 netdev_err(dev, "unable to setup rxqs after MTU change\n");
3832 ret = mvneta_setup_txqs(pp);
3834 netdev_err(dev, "unable to setup txqs after MTU change\n");
3838 on_each_cpu(mvneta_percpu_enable, pp, true);
3839 mvneta_start_dev(pp);
3841 netdev_update_features(dev);
3846 static netdev_features_t mvneta_fix_features(struct net_device *dev,
3847 netdev_features_t features)
3849 struct mvneta_port *pp = netdev_priv(dev);
3851 if (pp->tx_csum_limit && dev->mtu > pp->tx_csum_limit) {
3852 features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
3854 "Disable IP checksum for MTU greater than %dB\n",
3861 /* Get mac address */
3862 static void mvneta_get_mac_addr(struct mvneta_port *pp, unsigned char *addr)
3864 u32 mac_addr_l, mac_addr_h;
3866 mac_addr_l = mvreg_read(pp, MVNETA_MAC_ADDR_LOW);
3867 mac_addr_h = mvreg_read(pp, MVNETA_MAC_ADDR_HIGH);
3868 addr[0] = (mac_addr_h >> 24) & 0xFF;
3869 addr[1] = (mac_addr_h >> 16) & 0xFF;
3870 addr[2] = (mac_addr_h >> 8) & 0xFF;
3871 addr[3] = mac_addr_h & 0xFF;
3872 addr[4] = (mac_addr_l >> 8) & 0xFF;
3873 addr[5] = mac_addr_l & 0xFF;
3876 /* Handle setting mac address */
3877 static int mvneta_set_mac_addr(struct net_device *dev, void *addr)
3879 struct mvneta_port *pp = netdev_priv(dev);
3880 struct sockaddr *sockaddr = addr;
3883 ret = eth_prepare_mac_addr_change(dev, addr);
3886 /* Remove previous address table entry */
3887 mvneta_mac_addr_set(pp, dev->dev_addr, -1);
3889 /* Set new addr in hw */
3890 mvneta_mac_addr_set(pp, sockaddr->sa_data, pp->rxq_def);
3892 eth_commit_mac_addr_change(dev, addr);
3896 static struct mvneta_port *mvneta_pcs_to_port(struct phylink_pcs *pcs)
3898 return container_of(pcs, struct mvneta_port, phylink_pcs);
3901 static int mvneta_pcs_validate(struct phylink_pcs *pcs,
3902 unsigned long *supported,
3903 const struct phylink_link_state *state)
3905 /* We only support QSGMII, SGMII, 802.3z and RGMII modes.
3906 * When in 802.3z mode, we must have AN enabled:
3907 * "Bit 2 Field InBandAnEn In-band Auto-Negotiation enable. ...
3908 * When <PortType> = 1 (1000BASE-X) this field must be set to 1."
3910 if (phy_interface_mode_is_8023z(state->interface) &&
3911 !phylink_test(state->advertising, Autoneg))
3917 static void mvneta_pcs_get_state(struct phylink_pcs *pcs,
3918 struct phylink_link_state *state)
3920 struct mvneta_port *pp = mvneta_pcs_to_port(pcs);
3923 gmac_stat = mvreg_read(pp, MVNETA_GMAC_STATUS);
3925 if (gmac_stat & MVNETA_GMAC_SPEED_1000)
3927 state->interface == PHY_INTERFACE_MODE_2500BASEX ?
3928 SPEED_2500 : SPEED_1000;
3929 else if (gmac_stat & MVNETA_GMAC_SPEED_100)
3930 state->speed = SPEED_100;
3932 state->speed = SPEED_10;
3934 state->an_complete = !!(gmac_stat & MVNETA_GMAC_AN_COMPLETE);
3935 state->link = !!(gmac_stat & MVNETA_GMAC_LINK_UP);
3936 state->duplex = !!(gmac_stat & MVNETA_GMAC_FULL_DUPLEX);
3938 if (gmac_stat & MVNETA_GMAC_RX_FLOW_CTRL_ENABLE)
3939 state->pause |= MLO_PAUSE_RX;
3940 if (gmac_stat & MVNETA_GMAC_TX_FLOW_CTRL_ENABLE)
3941 state->pause |= MLO_PAUSE_TX;
3944 static int mvneta_pcs_config(struct phylink_pcs *pcs,
3945 unsigned int mode, phy_interface_t interface,
3946 const unsigned long *advertising,
3947 bool permit_pause_to_mac)
3949 struct mvneta_port *pp = mvneta_pcs_to_port(pcs);
3950 u32 mask, val, an, old_an, changed;
3952 mask = MVNETA_GMAC_INBAND_AN_ENABLE |
3953 MVNETA_GMAC_INBAND_RESTART_AN |
3954 MVNETA_GMAC_AN_SPEED_EN |
3955 MVNETA_GMAC_AN_FLOW_CTRL_EN |
3956 MVNETA_GMAC_AN_DUPLEX_EN;
3958 if (phylink_autoneg_inband(mode)) {
3959 mask |= MVNETA_GMAC_CONFIG_MII_SPEED |
3960 MVNETA_GMAC_CONFIG_GMII_SPEED |
3961 MVNETA_GMAC_CONFIG_FULL_DUPLEX;
3962 val = MVNETA_GMAC_INBAND_AN_ENABLE;
3964 if (interface == PHY_INTERFACE_MODE_SGMII) {
3965 /* SGMII mode receives the speed and duplex from PHY */
3966 val |= MVNETA_GMAC_AN_SPEED_EN |
3967 MVNETA_GMAC_AN_DUPLEX_EN;
3969 /* 802.3z mode has fixed speed and duplex */
3970 val |= MVNETA_GMAC_CONFIG_GMII_SPEED |
3971 MVNETA_GMAC_CONFIG_FULL_DUPLEX;
3973 /* The FLOW_CTRL_EN bit selects either the hardware
3974 * automatically or the CONFIG_FLOW_CTRL manually
3975 * controls the GMAC pause mode.
3977 if (permit_pause_to_mac)
3978 val |= MVNETA_GMAC_AN_FLOW_CTRL_EN;
3980 /* Update the advertisement bits */
3981 mask |= MVNETA_GMAC_ADVERT_SYM_FLOW_CTRL;
3982 if (phylink_test(advertising, Pause))
3983 val |= MVNETA_GMAC_ADVERT_SYM_FLOW_CTRL;
3986 /* Phy or fixed speed - disable in-band AN modes */
3990 old_an = an = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3991 an = (an & ~mask) | val;
3992 changed = old_an ^ an;
3994 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, an);
3996 /* We are only interested in the advertisement bits changing */
3997 return !!(changed & MVNETA_GMAC_ADVERT_SYM_FLOW_CTRL);
4000 static void mvneta_pcs_an_restart(struct phylink_pcs *pcs)
4002 struct mvneta_port *pp = mvneta_pcs_to_port(pcs);
4003 u32 gmac_an = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
4005 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
4006 gmac_an | MVNETA_GMAC_INBAND_RESTART_AN);
4007 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
4008 gmac_an & ~MVNETA_GMAC_INBAND_RESTART_AN);
4011 static const struct phylink_pcs_ops mvneta_phylink_pcs_ops = {
4012 .pcs_validate = mvneta_pcs_validate,
4013 .pcs_get_state = mvneta_pcs_get_state,
4014 .pcs_config = mvneta_pcs_config,
4015 .pcs_an_restart = mvneta_pcs_an_restart,
4018 static struct phylink_pcs *mvneta_mac_select_pcs(struct phylink_config *config,
4019 phy_interface_t interface)
4021 struct net_device *ndev = to_net_dev(config->dev);
4022 struct mvneta_port *pp = netdev_priv(ndev);
4024 return &pp->phylink_pcs;
4027 static int mvneta_mac_prepare(struct phylink_config *config, unsigned int mode,
4028 phy_interface_t interface)
4030 struct net_device *ndev = to_net_dev(config->dev);
4031 struct mvneta_port *pp = netdev_priv(ndev);
4034 if (pp->phy_interface != interface ||
4035 phylink_autoneg_inband(mode)) {
4036 /* Force the link down when changing the interface or if in
4037 * in-band mode. According to Armada 370 documentation, we
4038 * can only change the port mode and in-band enable when the
4041 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
4042 val &= ~MVNETA_GMAC_FORCE_LINK_PASS;
4043 val |= MVNETA_GMAC_FORCE_LINK_DOWN;
4044 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
4047 if (pp->phy_interface != interface)
4048 WARN_ON(phy_power_off(pp->comphy));
4050 /* Enable the 1ms clock */
4051 if (phylink_autoneg_inband(mode)) {
4052 unsigned long rate = clk_get_rate(pp->clk);
4054 mvreg_write(pp, MVNETA_GMAC_CLOCK_DIVIDER,
4055 MVNETA_GMAC_1MS_CLOCK_ENABLE | (rate / 1000));
4061 static void mvneta_mac_config(struct phylink_config *config, unsigned int mode,
4062 const struct phylink_link_state *state)
4064 struct net_device *ndev = to_net_dev(config->dev);
4065 struct mvneta_port *pp = netdev_priv(ndev);
4066 u32 new_ctrl0, gmac_ctrl0 = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
4067 u32 new_ctrl2, gmac_ctrl2 = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
4068 u32 new_ctrl4, gmac_ctrl4 = mvreg_read(pp, MVNETA_GMAC_CTRL_4);
4070 new_ctrl0 = gmac_ctrl0 & ~MVNETA_GMAC0_PORT_1000BASE_X;
4071 new_ctrl2 = gmac_ctrl2 & ~(MVNETA_GMAC2_INBAND_AN_ENABLE |
4072 MVNETA_GMAC2_PORT_RESET);
4073 new_ctrl4 = gmac_ctrl4 & ~(MVNETA_GMAC4_SHORT_PREAMBLE_ENABLE);
4075 /* Even though it might look weird, when we're configured in
4076 * SGMII or QSGMII mode, the RGMII bit needs to be set.
4078 new_ctrl2 |= MVNETA_GMAC2_PORT_RGMII;
4080 if (state->interface == PHY_INTERFACE_MODE_QSGMII ||
4081 state->interface == PHY_INTERFACE_MODE_SGMII ||
4082 phy_interface_mode_is_8023z(state->interface))
4083 new_ctrl2 |= MVNETA_GMAC2_PCS_ENABLE;
4085 if (!phylink_autoneg_inband(mode)) {
4086 /* Phy or fixed speed - nothing to do, leave the
4087 * configured speed, duplex and flow control as-is.
4089 } else if (state->interface == PHY_INTERFACE_MODE_SGMII) {
4090 /* SGMII mode receives the state from the PHY */
4091 new_ctrl2 |= MVNETA_GMAC2_INBAND_AN_ENABLE;
4093 /* 802.3z negotiation - only 1000base-X */
4094 new_ctrl0 |= MVNETA_GMAC0_PORT_1000BASE_X;
4097 /* When at 2.5G, the link partner can send frames with shortened
4100 if (state->interface == PHY_INTERFACE_MODE_2500BASEX)
4101 new_ctrl4 |= MVNETA_GMAC4_SHORT_PREAMBLE_ENABLE;
4103 if (new_ctrl0 != gmac_ctrl0)
4104 mvreg_write(pp, MVNETA_GMAC_CTRL_0, new_ctrl0);
4105 if (new_ctrl2 != gmac_ctrl2)
4106 mvreg_write(pp, MVNETA_GMAC_CTRL_2, new_ctrl2);
4107 if (new_ctrl4 != gmac_ctrl4)
4108 mvreg_write(pp, MVNETA_GMAC_CTRL_4, new_ctrl4);
4110 if (gmac_ctrl2 & MVNETA_GMAC2_PORT_RESET) {
4111 while ((mvreg_read(pp, MVNETA_GMAC_CTRL_2) &
4112 MVNETA_GMAC2_PORT_RESET) != 0)
4117 static int mvneta_mac_finish(struct phylink_config *config, unsigned int mode,
4118 phy_interface_t interface)
4120 struct net_device *ndev = to_net_dev(config->dev);
4121 struct mvneta_port *pp = netdev_priv(ndev);
4124 /* Disable 1ms clock if not in in-band mode */
4125 if (!phylink_autoneg_inband(mode)) {
4126 clk = mvreg_read(pp, MVNETA_GMAC_CLOCK_DIVIDER);
4127 clk &= ~MVNETA_GMAC_1MS_CLOCK_ENABLE;
4128 mvreg_write(pp, MVNETA_GMAC_CLOCK_DIVIDER, clk);
4131 if (pp->phy_interface != interface)
4132 /* Enable the Serdes PHY */
4133 WARN_ON(mvneta_config_interface(pp, interface));
4135 /* Allow the link to come up if in in-band mode, otherwise the
4136 * link is forced via mac_link_down()/mac_link_up()
4138 if (phylink_autoneg_inband(mode)) {
4139 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
4140 val &= ~MVNETA_GMAC_FORCE_LINK_DOWN;
4141 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
4147 static void mvneta_set_eee(struct mvneta_port *pp, bool enable)
4151 lpi_ctl1 = mvreg_read(pp, MVNETA_LPI_CTRL_1);
4153 lpi_ctl1 |= MVNETA_LPI_REQUEST_ENABLE;
4155 lpi_ctl1 &= ~MVNETA_LPI_REQUEST_ENABLE;
4156 mvreg_write(pp, MVNETA_LPI_CTRL_1, lpi_ctl1);
4159 static void mvneta_mac_link_down(struct phylink_config *config,
4160 unsigned int mode, phy_interface_t interface)
4162 struct net_device *ndev = to_net_dev(config->dev);
4163 struct mvneta_port *pp = netdev_priv(ndev);
4166 mvneta_port_down(pp);
4168 if (!phylink_autoneg_inband(mode)) {
4169 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
4170 val &= ~MVNETA_GMAC_FORCE_LINK_PASS;
4171 val |= MVNETA_GMAC_FORCE_LINK_DOWN;
4172 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
4175 pp->eee_active = false;
4176 mvneta_set_eee(pp, false);
4179 static void mvneta_mac_link_up(struct phylink_config *config,
4180 struct phy_device *phy,
4181 unsigned int mode, phy_interface_t interface,
4182 int speed, int duplex,
4183 bool tx_pause, bool rx_pause)
4185 struct net_device *ndev = to_net_dev(config->dev);
4186 struct mvneta_port *pp = netdev_priv(ndev);
4189 if (!phylink_autoneg_inband(mode)) {
4190 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
4191 val &= ~(MVNETA_GMAC_FORCE_LINK_DOWN |
4192 MVNETA_GMAC_CONFIG_MII_SPEED |
4193 MVNETA_GMAC_CONFIG_GMII_SPEED |
4194 MVNETA_GMAC_CONFIG_FLOW_CTRL |
4195 MVNETA_GMAC_CONFIG_FULL_DUPLEX);
4196 val |= MVNETA_GMAC_FORCE_LINK_PASS;
4198 if (speed == SPEED_1000 || speed == SPEED_2500)
4199 val |= MVNETA_GMAC_CONFIG_GMII_SPEED;
4200 else if (speed == SPEED_100)
4201 val |= MVNETA_GMAC_CONFIG_MII_SPEED;
4203 if (duplex == DUPLEX_FULL)
4204 val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
4206 if (tx_pause || rx_pause)
4207 val |= MVNETA_GMAC_CONFIG_FLOW_CTRL;
4209 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
4211 /* When inband doesn't cover flow control or flow control is
4212 * disabled, we need to manually configure it. This bit will
4213 * only have effect if MVNETA_GMAC_AN_FLOW_CTRL_EN is unset.
4215 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
4216 val &= ~MVNETA_GMAC_CONFIG_FLOW_CTRL;
4218 if (tx_pause || rx_pause)
4219 val |= MVNETA_GMAC_CONFIG_FLOW_CTRL;
4221 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
4226 if (phy && pp->eee_enabled) {
4227 pp->eee_active = phy_init_eee(phy, false) >= 0;
4228 mvneta_set_eee(pp, pp->eee_active && pp->tx_lpi_enabled);
4232 static const struct phylink_mac_ops mvneta_phylink_ops = {
4233 .mac_select_pcs = mvneta_mac_select_pcs,
4234 .mac_prepare = mvneta_mac_prepare,
4235 .mac_config = mvneta_mac_config,
4236 .mac_finish = mvneta_mac_finish,
4237 .mac_link_down = mvneta_mac_link_down,
4238 .mac_link_up = mvneta_mac_link_up,
4241 static int mvneta_mdio_probe(struct mvneta_port *pp)
4243 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
4244 int err = phylink_of_phy_connect(pp->phylink, pp->dn, 0);
4247 netdev_err(pp->dev, "could not attach PHY: %d\n", err);
4249 phylink_ethtool_get_wol(pp->phylink, &wol);
4250 device_set_wakeup_capable(&pp->dev->dev, !!wol.supported);
4252 /* PHY WoL may be enabled but device wakeup disabled */
4254 device_set_wakeup_enable(&pp->dev->dev, !!wol.wolopts);
4259 static void mvneta_mdio_remove(struct mvneta_port *pp)
4261 phylink_disconnect_phy(pp->phylink);
4264 /* Electing a CPU must be done in an atomic way: it should be done
4265 * after or before the removal/insertion of a CPU and this function is
4268 static void mvneta_percpu_elect(struct mvneta_port *pp)
4270 int elected_cpu = 0, max_cpu, cpu;
4272 /* Use the cpu associated to the rxq when it is online, in all
4273 * the other cases, use the cpu 0 which can't be offline.
4275 if (pp->rxq_def < nr_cpu_ids && cpu_online(pp->rxq_def))
4276 elected_cpu = pp->rxq_def;
4278 max_cpu = num_present_cpus();
4280 for_each_online_cpu(cpu) {
4281 int rxq_map = 0, txq_map = 0;
4284 for (rxq = 0; rxq < rxq_number; rxq++)
4285 if ((rxq % max_cpu) == cpu)
4286 rxq_map |= MVNETA_CPU_RXQ_ACCESS(rxq);
4288 if (cpu == elected_cpu)
4289 /* Map the default receive queue to the elected CPU */
4290 rxq_map |= MVNETA_CPU_RXQ_ACCESS(pp->rxq_def);
4292 /* We update the TX queue map only if we have one
4293 * queue. In this case we associate the TX queue to
4294 * the CPU bound to the default RX queue
4296 if (txq_number == 1)
4297 txq_map = (cpu == elected_cpu) ?
4298 MVNETA_CPU_TXQ_ACCESS(1) : 0;
4300 txq_map = mvreg_read(pp, MVNETA_CPU_MAP(cpu)) &
4301 MVNETA_CPU_TXQ_ACCESS_ALL_MASK;
4303 mvreg_write(pp, MVNETA_CPU_MAP(cpu), rxq_map | txq_map);
4305 /* Update the interrupt mask on each CPU according the
4308 smp_call_function_single(cpu, mvneta_percpu_unmask_interrupt,
4313 static int mvneta_cpu_online(unsigned int cpu, struct hlist_node *node)
4316 struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
4318 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
4320 /* Armada 3700's per-cpu interrupt for mvneta is broken, all interrupts
4321 * are routed to CPU 0, so we don't need all the cpu-hotplug support
4323 if (pp->neta_armada3700)
4326 spin_lock(&pp->lock);
4328 * Configuring the driver for a new CPU while the driver is
4329 * stopping is racy, so just avoid it.
4331 if (pp->is_stopped) {
4332 spin_unlock(&pp->lock);
4335 netif_tx_stop_all_queues(pp->dev);
4338 * We have to synchronise on tha napi of each CPU except the one
4339 * just being woken up
4341 for_each_online_cpu(other_cpu) {
4342 if (other_cpu != cpu) {
4343 struct mvneta_pcpu_port *other_port =
4344 per_cpu_ptr(pp->ports, other_cpu);
4346 napi_synchronize(&other_port->napi);
4350 /* Mask all ethernet port interrupts */
4351 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
4352 napi_enable(&port->napi);
4355 * Enable per-CPU interrupts on the CPU that is
4358 mvneta_percpu_enable(pp);
4361 * Enable per-CPU interrupt on the one CPU we care
4364 mvneta_percpu_elect(pp);
4366 /* Unmask all ethernet port interrupts */
4367 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
4368 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
4369 MVNETA_CAUSE_PHY_STATUS_CHANGE |
4370 MVNETA_CAUSE_LINK_CHANGE);
4371 netif_tx_start_all_queues(pp->dev);
4372 spin_unlock(&pp->lock);
4376 static int mvneta_cpu_down_prepare(unsigned int cpu, struct hlist_node *node)
4378 struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
4380 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
4383 * Thanks to this lock we are sure that any pending cpu election is
4386 spin_lock(&pp->lock);
4387 /* Mask all ethernet port interrupts */
4388 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
4389 spin_unlock(&pp->lock);
4391 napi_synchronize(&port->napi);
4392 napi_disable(&port->napi);
4393 /* Disable per-CPU interrupts on the CPU that is brought down. */
4394 mvneta_percpu_disable(pp);
4398 static int mvneta_cpu_dead(unsigned int cpu, struct hlist_node *node)
4400 struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
4403 /* Check if a new CPU must be elected now this on is down */
4404 spin_lock(&pp->lock);
4405 mvneta_percpu_elect(pp);
4406 spin_unlock(&pp->lock);
4407 /* Unmask all ethernet port interrupts */
4408 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
4409 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
4410 MVNETA_CAUSE_PHY_STATUS_CHANGE |
4411 MVNETA_CAUSE_LINK_CHANGE);
4412 netif_tx_start_all_queues(pp->dev);
4416 static int mvneta_open(struct net_device *dev)
4418 struct mvneta_port *pp = netdev_priv(dev);
4421 pp->pkt_size = MVNETA_RX_PKT_SIZE(pp->dev->mtu);
4423 ret = mvneta_setup_rxqs(pp);
4427 ret = mvneta_setup_txqs(pp);
4429 goto err_cleanup_rxqs;
4431 /* Connect to port interrupt line */
4432 if (pp->neta_armada3700)
4433 ret = request_irq(pp->dev->irq, mvneta_isr, 0,
4436 ret = request_percpu_irq(pp->dev->irq, mvneta_percpu_isr,
4437 dev->name, pp->ports);
4439 netdev_err(pp->dev, "cannot request irq %d\n", pp->dev->irq);
4440 goto err_cleanup_txqs;
4443 if (!pp->neta_armada3700) {
4444 /* Enable per-CPU interrupt on all the CPU to handle our RX
4447 on_each_cpu(mvneta_percpu_enable, pp, true);
4449 pp->is_stopped = false;
4450 /* Register a CPU notifier to handle the case where our CPU
4451 * might be taken offline.
4453 ret = cpuhp_state_add_instance_nocalls(online_hpstate,
4458 ret = cpuhp_state_add_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
4461 goto err_free_online_hp;
4464 ret = mvneta_mdio_probe(pp);
4466 netdev_err(dev, "cannot probe MDIO bus\n");
4467 goto err_free_dead_hp;
4470 mvneta_start_dev(pp);
4475 if (!pp->neta_armada3700)
4476 cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
4479 if (!pp->neta_armada3700)
4480 cpuhp_state_remove_instance_nocalls(online_hpstate,
4483 if (pp->neta_armada3700) {
4484 free_irq(pp->dev->irq, pp);
4486 on_each_cpu(mvneta_percpu_disable, pp, true);
4487 free_percpu_irq(pp->dev->irq, pp->ports);
4490 mvneta_cleanup_txqs(pp);
4492 mvneta_cleanup_rxqs(pp);
4496 /* Stop the port, free port interrupt line */
4497 static int mvneta_stop(struct net_device *dev)
4499 struct mvneta_port *pp = netdev_priv(dev);
4501 if (!pp->neta_armada3700) {
4502 /* Inform that we are stopping so we don't want to setup the
4503 * driver for new CPUs in the notifiers. The code of the
4504 * notifier for CPU online is protected by the same spinlock,
4505 * so when we get the lock, the notifer work is done.
4507 spin_lock(&pp->lock);
4508 pp->is_stopped = true;
4509 spin_unlock(&pp->lock);
4511 mvneta_stop_dev(pp);
4512 mvneta_mdio_remove(pp);
4514 cpuhp_state_remove_instance_nocalls(online_hpstate,
4516 cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
4518 on_each_cpu(mvneta_percpu_disable, pp, true);
4519 free_percpu_irq(dev->irq, pp->ports);
4521 mvneta_stop_dev(pp);
4522 mvneta_mdio_remove(pp);
4523 free_irq(dev->irq, pp);
4526 mvneta_cleanup_rxqs(pp);
4527 mvneta_cleanup_txqs(pp);
4532 static int mvneta_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4534 struct mvneta_port *pp = netdev_priv(dev);
4536 return phylink_mii_ioctl(pp->phylink, ifr, cmd);
4539 static int mvneta_xdp_setup(struct net_device *dev, struct bpf_prog *prog,
4540 struct netlink_ext_ack *extack)
4542 bool need_update, running = netif_running(dev);
4543 struct mvneta_port *pp = netdev_priv(dev);
4544 struct bpf_prog *old_prog;
4546 if (prog && !prog->aux->xdp_has_frags &&
4547 dev->mtu > MVNETA_MAX_RX_BUF_SIZE) {
4548 NL_SET_ERR_MSG_MOD(extack, "prog does not support XDP frags");
4553 NL_SET_ERR_MSG_MOD(extack,
4554 "Hardware Buffer Management not supported on XDP");
4558 need_update = !!pp->xdp_prog != !!prog;
4559 if (running && need_update)
4562 old_prog = xchg(&pp->xdp_prog, prog);
4564 bpf_prog_put(old_prog);
4566 if (running && need_update)
4567 return mvneta_open(dev);
4572 static int mvneta_xdp(struct net_device *dev, struct netdev_bpf *xdp)
4574 switch (xdp->command) {
4575 case XDP_SETUP_PROG:
4576 return mvneta_xdp_setup(dev, xdp->prog, xdp->extack);
4582 /* Ethtool methods */
4584 /* Set link ksettings (phy address, speed) for ethtools */
4586 mvneta_ethtool_set_link_ksettings(struct net_device *ndev,
4587 const struct ethtool_link_ksettings *cmd)
4589 struct mvneta_port *pp = netdev_priv(ndev);
4591 return phylink_ethtool_ksettings_set(pp->phylink, cmd);
4594 /* Get link ksettings for ethtools */
4596 mvneta_ethtool_get_link_ksettings(struct net_device *ndev,
4597 struct ethtool_link_ksettings *cmd)
4599 struct mvneta_port *pp = netdev_priv(ndev);
4601 return phylink_ethtool_ksettings_get(pp->phylink, cmd);
4604 static int mvneta_ethtool_nway_reset(struct net_device *dev)
4606 struct mvneta_port *pp = netdev_priv(dev);
4608 return phylink_ethtool_nway_reset(pp->phylink);
4611 /* Set interrupt coalescing for ethtools */
4613 mvneta_ethtool_set_coalesce(struct net_device *dev,
4614 struct ethtool_coalesce *c,
4615 struct kernel_ethtool_coalesce *kernel_coal,
4616 struct netlink_ext_ack *extack)
4618 struct mvneta_port *pp = netdev_priv(dev);
4621 for (queue = 0; queue < rxq_number; queue++) {
4622 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
4623 rxq->time_coal = c->rx_coalesce_usecs;
4624 rxq->pkts_coal = c->rx_max_coalesced_frames;
4625 mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
4626 mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
4629 for (queue = 0; queue < txq_number; queue++) {
4630 struct mvneta_tx_queue *txq = &pp->txqs[queue];
4631 txq->done_pkts_coal = c->tx_max_coalesced_frames;
4632 mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
4638 /* get coalescing for ethtools */
4640 mvneta_ethtool_get_coalesce(struct net_device *dev,
4641 struct ethtool_coalesce *c,
4642 struct kernel_ethtool_coalesce *kernel_coal,
4643 struct netlink_ext_ack *extack)
4645 struct mvneta_port *pp = netdev_priv(dev);
4647 c->rx_coalesce_usecs = pp->rxqs[0].time_coal;
4648 c->rx_max_coalesced_frames = pp->rxqs[0].pkts_coal;
4650 c->tx_max_coalesced_frames = pp->txqs[0].done_pkts_coal;
4655 static void mvneta_ethtool_get_drvinfo(struct net_device *dev,
4656 struct ethtool_drvinfo *drvinfo)
4658 strscpy(drvinfo->driver, MVNETA_DRIVER_NAME,
4659 sizeof(drvinfo->driver));
4660 strscpy(drvinfo->version, MVNETA_DRIVER_VERSION,
4661 sizeof(drvinfo->version));
4662 strscpy(drvinfo->bus_info, dev_name(&dev->dev),
4663 sizeof(drvinfo->bus_info));
4668 mvneta_ethtool_get_ringparam(struct net_device *netdev,
4669 struct ethtool_ringparam *ring,
4670 struct kernel_ethtool_ringparam *kernel_ring,
4671 struct netlink_ext_ack *extack)
4673 struct mvneta_port *pp = netdev_priv(netdev);
4675 ring->rx_max_pending = MVNETA_MAX_RXD;
4676 ring->tx_max_pending = MVNETA_MAX_TXD;
4677 ring->rx_pending = pp->rx_ring_size;
4678 ring->tx_pending = pp->tx_ring_size;
4682 mvneta_ethtool_set_ringparam(struct net_device *dev,
4683 struct ethtool_ringparam *ring,
4684 struct kernel_ethtool_ringparam *kernel_ring,
4685 struct netlink_ext_ack *extack)
4687 struct mvneta_port *pp = netdev_priv(dev);
4689 if ((ring->rx_pending == 0) || (ring->tx_pending == 0))
4691 pp->rx_ring_size = ring->rx_pending < MVNETA_MAX_RXD ?
4692 ring->rx_pending : MVNETA_MAX_RXD;
4694 pp->tx_ring_size = clamp_t(u16, ring->tx_pending,
4695 MVNETA_MAX_SKB_DESCS * 2, MVNETA_MAX_TXD);
4696 if (pp->tx_ring_size != ring->tx_pending)
4697 netdev_warn(dev, "TX queue size set to %u (requested %u)\n",
4698 pp->tx_ring_size, ring->tx_pending);
4700 if (netif_running(dev)) {
4702 if (mvneta_open(dev)) {
4704 "error on opening device after ring param change\n");
4712 static void mvneta_ethtool_get_pauseparam(struct net_device *dev,
4713 struct ethtool_pauseparam *pause)
4715 struct mvneta_port *pp = netdev_priv(dev);
4717 phylink_ethtool_get_pauseparam(pp->phylink, pause);
4720 static int mvneta_ethtool_set_pauseparam(struct net_device *dev,
4721 struct ethtool_pauseparam *pause)
4723 struct mvneta_port *pp = netdev_priv(dev);
4725 return phylink_ethtool_set_pauseparam(pp->phylink, pause);
4728 static void mvneta_ethtool_get_strings(struct net_device *netdev, u32 sset,
4731 if (sset == ETH_SS_STATS) {
4734 for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++)
4735 memcpy(data + i * ETH_GSTRING_LEN,
4736 mvneta_statistics[i].name, ETH_GSTRING_LEN);
4738 data += ETH_GSTRING_LEN * ARRAY_SIZE(mvneta_statistics);
4739 page_pool_ethtool_stats_get_strings(data);
4744 mvneta_ethtool_update_pcpu_stats(struct mvneta_port *pp,
4745 struct mvneta_ethtool_stats *es)
4750 for_each_possible_cpu(cpu) {
4751 struct mvneta_pcpu_stats *stats;
4752 u64 skb_alloc_error;
4762 stats = per_cpu_ptr(pp->stats, cpu);
4764 start = u64_stats_fetch_begin(&stats->syncp);
4765 skb_alloc_error = stats->es.skb_alloc_error;
4766 refill_error = stats->es.refill_error;
4767 xdp_redirect = stats->es.ps.xdp_redirect;
4768 xdp_pass = stats->es.ps.xdp_pass;
4769 xdp_drop = stats->es.ps.xdp_drop;
4770 xdp_xmit = stats->es.ps.xdp_xmit;
4771 xdp_xmit_err = stats->es.ps.xdp_xmit_err;
4772 xdp_tx = stats->es.ps.xdp_tx;
4773 xdp_tx_err = stats->es.ps.xdp_tx_err;
4774 } while (u64_stats_fetch_retry(&stats->syncp, start));
4776 es->skb_alloc_error += skb_alloc_error;
4777 es->refill_error += refill_error;
4778 es->ps.xdp_redirect += xdp_redirect;
4779 es->ps.xdp_pass += xdp_pass;
4780 es->ps.xdp_drop += xdp_drop;
4781 es->ps.xdp_xmit += xdp_xmit;
4782 es->ps.xdp_xmit_err += xdp_xmit_err;
4783 es->ps.xdp_tx += xdp_tx;
4784 es->ps.xdp_tx_err += xdp_tx_err;
4788 static void mvneta_ethtool_update_stats(struct mvneta_port *pp)
4790 struct mvneta_ethtool_stats stats = {};
4791 const struct mvneta_statistic *s;
4792 void __iomem *base = pp->base;
4797 mvneta_ethtool_update_pcpu_stats(pp, &stats);
4798 for (i = 0, s = mvneta_statistics;
4799 s < mvneta_statistics + ARRAY_SIZE(mvneta_statistics);
4803 val = readl_relaxed(base + s->offset);
4804 pp->ethtool_stats[i] += val;
4807 /* Docs say to read low 32-bit then high */
4808 low = readl_relaxed(base + s->offset);
4809 high = readl_relaxed(base + s->offset + 4);
4810 val = (u64)high << 32 | low;
4811 pp->ethtool_stats[i] += val;
4814 switch (s->offset) {
4815 case ETHTOOL_STAT_EEE_WAKEUP:
4816 val = phylink_get_eee_err(pp->phylink);
4817 pp->ethtool_stats[i] += val;
4819 case ETHTOOL_STAT_SKB_ALLOC_ERR:
4820 pp->ethtool_stats[i] = stats.skb_alloc_error;
4822 case ETHTOOL_STAT_REFILL_ERR:
4823 pp->ethtool_stats[i] = stats.refill_error;
4825 case ETHTOOL_XDP_REDIRECT:
4826 pp->ethtool_stats[i] = stats.ps.xdp_redirect;
4828 case ETHTOOL_XDP_PASS:
4829 pp->ethtool_stats[i] = stats.ps.xdp_pass;
4831 case ETHTOOL_XDP_DROP:
4832 pp->ethtool_stats[i] = stats.ps.xdp_drop;
4834 case ETHTOOL_XDP_TX:
4835 pp->ethtool_stats[i] = stats.ps.xdp_tx;
4837 case ETHTOOL_XDP_TX_ERR:
4838 pp->ethtool_stats[i] = stats.ps.xdp_tx_err;
4840 case ETHTOOL_XDP_XMIT:
4841 pp->ethtool_stats[i] = stats.ps.xdp_xmit;
4843 case ETHTOOL_XDP_XMIT_ERR:
4844 pp->ethtool_stats[i] = stats.ps.xdp_xmit_err;
4852 static void mvneta_ethtool_pp_stats(struct mvneta_port *pp, u64 *data)
4854 struct page_pool_stats stats = {};
4857 for (i = 0; i < rxq_number; i++)
4858 page_pool_get_stats(pp->rxqs[i].page_pool, &stats);
4860 page_pool_ethtool_stats_get(data, &stats);
4863 static void mvneta_ethtool_get_stats(struct net_device *dev,
4864 struct ethtool_stats *stats, u64 *data)
4866 struct mvneta_port *pp = netdev_priv(dev);
4869 mvneta_ethtool_update_stats(pp);
4871 for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++)
4872 *data++ = pp->ethtool_stats[i];
4874 mvneta_ethtool_pp_stats(pp, data);
4877 static int mvneta_ethtool_get_sset_count(struct net_device *dev, int sset)
4879 if (sset == ETH_SS_STATS)
4880 return ARRAY_SIZE(mvneta_statistics) +
4881 page_pool_ethtool_stats_get_count();
4886 static u32 mvneta_ethtool_get_rxfh_indir_size(struct net_device *dev)
4888 return MVNETA_RSS_LU_TABLE_SIZE;
4891 static int mvneta_ethtool_get_rxnfc(struct net_device *dev,
4892 struct ethtool_rxnfc *info,
4893 u32 *rules __always_unused)
4895 switch (info->cmd) {
4896 case ETHTOOL_GRXRINGS:
4897 info->data = rxq_number;
4906 static int mvneta_config_rss(struct mvneta_port *pp)
4911 netif_tx_stop_all_queues(pp->dev);
4913 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
4915 if (!pp->neta_armada3700) {
4916 /* We have to synchronise on the napi of each CPU */
4917 for_each_online_cpu(cpu) {
4918 struct mvneta_pcpu_port *pcpu_port =
4919 per_cpu_ptr(pp->ports, cpu);
4921 napi_synchronize(&pcpu_port->napi);
4922 napi_disable(&pcpu_port->napi);
4925 napi_synchronize(&pp->napi);
4926 napi_disable(&pp->napi);
4929 pp->rxq_def = pp->indir[0];
4931 /* Update unicast mapping */
4932 mvneta_set_rx_mode(pp->dev);
4934 /* Update val of portCfg register accordingly with all RxQueue types */
4935 val = MVNETA_PORT_CONFIG_DEFL_VALUE(pp->rxq_def);
4936 mvreg_write(pp, MVNETA_PORT_CONFIG, val);
4938 /* Update the elected CPU matching the new rxq_def */
4939 spin_lock(&pp->lock);
4940 mvneta_percpu_elect(pp);
4941 spin_unlock(&pp->lock);
4943 if (!pp->neta_armada3700) {
4944 /* We have to synchronise on the napi of each CPU */
4945 for_each_online_cpu(cpu) {
4946 struct mvneta_pcpu_port *pcpu_port =
4947 per_cpu_ptr(pp->ports, cpu);
4949 napi_enable(&pcpu_port->napi);
4952 napi_enable(&pp->napi);
4955 netif_tx_start_all_queues(pp->dev);
4960 static int mvneta_ethtool_set_rxfh(struct net_device *dev, const u32 *indir,
4961 const u8 *key, const u8 hfunc)
4963 struct mvneta_port *pp = netdev_priv(dev);
4965 /* Current code for Armada 3700 doesn't support RSS features yet */
4966 if (pp->neta_armada3700)
4969 /* We require at least one supported parameter to be changed
4970 * and no change in any of the unsupported parameters
4973 (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
4979 memcpy(pp->indir, indir, MVNETA_RSS_LU_TABLE_SIZE);
4981 return mvneta_config_rss(pp);
4984 static int mvneta_ethtool_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
4987 struct mvneta_port *pp = netdev_priv(dev);
4989 /* Current code for Armada 3700 doesn't support RSS features yet */
4990 if (pp->neta_armada3700)
4994 *hfunc = ETH_RSS_HASH_TOP;
4999 memcpy(indir, pp->indir, MVNETA_RSS_LU_TABLE_SIZE);
5004 static void mvneta_ethtool_get_wol(struct net_device *dev,
5005 struct ethtool_wolinfo *wol)
5007 struct mvneta_port *pp = netdev_priv(dev);
5009 phylink_ethtool_get_wol(pp->phylink, wol);
5012 static int mvneta_ethtool_set_wol(struct net_device *dev,
5013 struct ethtool_wolinfo *wol)
5015 struct mvneta_port *pp = netdev_priv(dev);
5018 ret = phylink_ethtool_set_wol(pp->phylink, wol);
5020 device_set_wakeup_enable(&dev->dev, !!wol->wolopts);
5025 static int mvneta_ethtool_get_eee(struct net_device *dev,
5026 struct ethtool_eee *eee)
5028 struct mvneta_port *pp = netdev_priv(dev);
5031 lpi_ctl0 = mvreg_read(pp, MVNETA_LPI_CTRL_0);
5033 eee->eee_enabled = pp->eee_enabled;
5034 eee->eee_active = pp->eee_active;
5035 eee->tx_lpi_enabled = pp->tx_lpi_enabled;
5036 eee->tx_lpi_timer = (lpi_ctl0) >> 8; // * scale;
5038 return phylink_ethtool_get_eee(pp->phylink, eee);
5041 static int mvneta_ethtool_set_eee(struct net_device *dev,
5042 struct ethtool_eee *eee)
5044 struct mvneta_port *pp = netdev_priv(dev);
5047 /* The Armada 37x documents do not give limits for this other than
5048 * it being an 8-bit register.
5050 if (eee->tx_lpi_enabled && eee->tx_lpi_timer > 255)
5053 lpi_ctl0 = mvreg_read(pp, MVNETA_LPI_CTRL_0);
5054 lpi_ctl0 &= ~(0xff << 8);
5055 lpi_ctl0 |= eee->tx_lpi_timer << 8;
5056 mvreg_write(pp, MVNETA_LPI_CTRL_0, lpi_ctl0);
5058 pp->eee_enabled = eee->eee_enabled;
5059 pp->tx_lpi_enabled = eee->tx_lpi_enabled;
5061 mvneta_set_eee(pp, eee->tx_lpi_enabled && eee->eee_enabled);
5063 return phylink_ethtool_set_eee(pp->phylink, eee);
5066 static void mvneta_clear_rx_prio_map(struct mvneta_port *pp)
5068 mvreg_write(pp, MVNETA_VLAN_PRIO_TO_RXQ, 0);
5071 static void mvneta_map_vlan_prio_to_rxq(struct mvneta_port *pp, u8 pri, u8 rxq)
5073 u32 val = mvreg_read(pp, MVNETA_VLAN_PRIO_TO_RXQ);
5075 val &= ~MVNETA_VLAN_PRIO_RXQ_MAP(pri, 0x7);
5076 val |= MVNETA_VLAN_PRIO_RXQ_MAP(pri, rxq);
5078 mvreg_write(pp, MVNETA_VLAN_PRIO_TO_RXQ, val);
5081 static int mvneta_enable_per_queue_rate_limit(struct mvneta_port *pp)
5083 unsigned long core_clk_rate;
5087 core_clk_rate = clk_get_rate(pp->clk);
5091 refill_cycles = MVNETA_TXQ_BUCKET_REFILL_BASE_PERIOD_NS /
5092 (NSEC_PER_SEC / core_clk_rate);
5094 if (refill_cycles > MVNETA_REFILL_MAX_NUM_CLK)
5097 /* Enable bw limit algorithm version 3 */
5098 val = mvreg_read(pp, MVNETA_TXQ_CMD1_REG);
5099 val &= ~(MVNETA_TXQ_CMD1_BW_LIM_SEL_V1 | MVNETA_TXQ_CMD1_BW_LIM_EN);
5100 mvreg_write(pp, MVNETA_TXQ_CMD1_REG, val);
5102 /* Set the base refill rate */
5103 mvreg_write(pp, MVNETA_REFILL_NUM_CLK_REG, refill_cycles);
5108 static void mvneta_disable_per_queue_rate_limit(struct mvneta_port *pp)
5110 u32 val = mvreg_read(pp, MVNETA_TXQ_CMD1_REG);
5112 val |= (MVNETA_TXQ_CMD1_BW_LIM_SEL_V1 | MVNETA_TXQ_CMD1_BW_LIM_EN);
5113 mvreg_write(pp, MVNETA_TXQ_CMD1_REG, val);
5116 static int mvneta_setup_queue_rates(struct mvneta_port *pp, int queue,
5117 u64 min_rate, u64 max_rate)
5119 u32 refill_val, rem;
5122 /* Convert to from Bps to bps */
5128 refill_val = div_u64_rem(max_rate, MVNETA_TXQ_RATE_LIMIT_RESOLUTION,
5131 if (rem || !refill_val ||
5132 refill_val > MVNETA_TXQ_BUCKET_REFILL_VALUE_MAX)
5136 val |= (MVNETA_TXQ_BUCKET_REFILL_PERIOD <<
5137 MVNETA_TXQ_BUCKET_REFILL_PERIOD_SHIFT);
5139 mvreg_write(pp, MVNETA_TXQ_BUCKET_REFILL_REG(queue), val);
5144 static int mvneta_setup_mqprio(struct net_device *dev,
5145 struct tc_mqprio_qopt_offload *mqprio)
5147 struct mvneta_port *pp = netdev_priv(dev);
5148 int rxq, txq, tc, ret;
5151 if (mqprio->qopt.hw != TC_MQPRIO_HW_OFFLOAD_TCS)
5154 num_tc = mqprio->qopt.num_tc;
5156 if (num_tc > rxq_number)
5159 mvneta_clear_rx_prio_map(pp);
5162 mvneta_disable_per_queue_rate_limit(pp);
5163 netdev_reset_tc(dev);
5167 netdev_set_num_tc(dev, mqprio->qopt.num_tc);
5169 for (tc = 0; tc < mqprio->qopt.num_tc; tc++) {
5170 netdev_set_tc_queue(dev, tc, mqprio->qopt.count[tc],
5171 mqprio->qopt.offset[tc]);
5173 for (rxq = mqprio->qopt.offset[tc];
5174 rxq < mqprio->qopt.count[tc] + mqprio->qopt.offset[tc];
5176 if (rxq >= rxq_number)
5179 mvneta_map_vlan_prio_to_rxq(pp, tc, rxq);
5183 if (mqprio->shaper != TC_MQPRIO_SHAPER_BW_RATE) {
5184 mvneta_disable_per_queue_rate_limit(pp);
5188 if (mqprio->qopt.num_tc > txq_number)
5191 ret = mvneta_enable_per_queue_rate_limit(pp);
5195 for (tc = 0; tc < mqprio->qopt.num_tc; tc++) {
5196 for (txq = mqprio->qopt.offset[tc];
5197 txq < mqprio->qopt.count[tc] + mqprio->qopt.offset[tc];
5199 if (txq >= txq_number)
5202 ret = mvneta_setup_queue_rates(pp, txq,
5203 mqprio->min_rate[tc],
5204 mqprio->max_rate[tc]);
5213 static int mvneta_setup_tc(struct net_device *dev, enum tc_setup_type type,
5217 case TC_SETUP_QDISC_MQPRIO:
5218 return mvneta_setup_mqprio(dev, type_data);
5224 static const struct net_device_ops mvneta_netdev_ops = {
5225 .ndo_open = mvneta_open,
5226 .ndo_stop = mvneta_stop,
5227 .ndo_start_xmit = mvneta_tx,
5228 .ndo_set_rx_mode = mvneta_set_rx_mode,
5229 .ndo_set_mac_address = mvneta_set_mac_addr,
5230 .ndo_change_mtu = mvneta_change_mtu,
5231 .ndo_fix_features = mvneta_fix_features,
5232 .ndo_get_stats64 = mvneta_get_stats64,
5233 .ndo_eth_ioctl = mvneta_ioctl,
5234 .ndo_bpf = mvneta_xdp,
5235 .ndo_xdp_xmit = mvneta_xdp_xmit,
5236 .ndo_setup_tc = mvneta_setup_tc,
5239 static const struct ethtool_ops mvneta_eth_tool_ops = {
5240 .supported_coalesce_params = ETHTOOL_COALESCE_RX_USECS |
5241 ETHTOOL_COALESCE_MAX_FRAMES,
5242 .nway_reset = mvneta_ethtool_nway_reset,
5243 .get_link = ethtool_op_get_link,
5244 .set_coalesce = mvneta_ethtool_set_coalesce,
5245 .get_coalesce = mvneta_ethtool_get_coalesce,
5246 .get_drvinfo = mvneta_ethtool_get_drvinfo,
5247 .get_ringparam = mvneta_ethtool_get_ringparam,
5248 .set_ringparam = mvneta_ethtool_set_ringparam,
5249 .get_pauseparam = mvneta_ethtool_get_pauseparam,
5250 .set_pauseparam = mvneta_ethtool_set_pauseparam,
5251 .get_strings = mvneta_ethtool_get_strings,
5252 .get_ethtool_stats = mvneta_ethtool_get_stats,
5253 .get_sset_count = mvneta_ethtool_get_sset_count,
5254 .get_rxfh_indir_size = mvneta_ethtool_get_rxfh_indir_size,
5255 .get_rxnfc = mvneta_ethtool_get_rxnfc,
5256 .get_rxfh = mvneta_ethtool_get_rxfh,
5257 .set_rxfh = mvneta_ethtool_set_rxfh,
5258 .get_link_ksettings = mvneta_ethtool_get_link_ksettings,
5259 .set_link_ksettings = mvneta_ethtool_set_link_ksettings,
5260 .get_wol = mvneta_ethtool_get_wol,
5261 .set_wol = mvneta_ethtool_set_wol,
5262 .get_eee = mvneta_ethtool_get_eee,
5263 .set_eee = mvneta_ethtool_set_eee,
5267 static int mvneta_init(struct device *dev, struct mvneta_port *pp)
5272 mvneta_port_disable(pp);
5274 /* Set port default values */
5275 mvneta_defaults_set(pp);
5277 pp->txqs = devm_kcalloc(dev, txq_number, sizeof(*pp->txqs), GFP_KERNEL);
5281 /* Initialize TX descriptor rings */
5282 for (queue = 0; queue < txq_number; queue++) {
5283 struct mvneta_tx_queue *txq = &pp->txqs[queue];
5285 txq->size = pp->tx_ring_size;
5286 txq->done_pkts_coal = MVNETA_TXDONE_COAL_PKTS;
5289 pp->rxqs = devm_kcalloc(dev, rxq_number, sizeof(*pp->rxqs), GFP_KERNEL);
5293 /* Create Rx descriptor rings */
5294 for (queue = 0; queue < rxq_number; queue++) {
5295 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
5297 rxq->size = pp->rx_ring_size;
5298 rxq->pkts_coal = MVNETA_RX_COAL_PKTS;
5299 rxq->time_coal = MVNETA_RX_COAL_USEC;
5301 = devm_kmalloc_array(pp->dev->dev.parent,
5303 sizeof(*rxq->buf_virt_addr),
5305 if (!rxq->buf_virt_addr)
5312 /* platform glue : initialize decoding windows */
5313 static void mvneta_conf_mbus_windows(struct mvneta_port *pp,
5314 const struct mbus_dram_target_info *dram)
5320 for (i = 0; i < 6; i++) {
5321 mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
5322 mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
5325 mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
5332 for (i = 0; i < dram->num_cs; i++) {
5333 const struct mbus_dram_window *cs = dram->cs + i;
5335 mvreg_write(pp, MVNETA_WIN_BASE(i),
5336 (cs->base & 0xffff0000) |
5337 (cs->mbus_attr << 8) |
5338 dram->mbus_dram_target_id);
5340 mvreg_write(pp, MVNETA_WIN_SIZE(i),
5341 (cs->size - 1) & 0xffff0000);
5343 win_enable &= ~(1 << i);
5344 win_protect |= 3 << (2 * i);
5348 mvreg_write(pp, MVNETA_WIN_BASE(0),
5349 (MVNETA_AC5_CNM_DDR_ATTR << 8) |
5350 MVNETA_AC5_CNM_DDR_TARGET);
5351 /* For Armada3700 open default 4GB Mbus window, leaving
5352 * arbitration of target/attribute to a different layer
5355 mvreg_write(pp, MVNETA_WIN_SIZE(0), 0xffff0000);
5356 win_enable &= ~BIT(0);
5360 mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
5361 mvreg_write(pp, MVNETA_ACCESS_PROTECT_ENABLE, win_protect);
5364 /* Power up the port */
5365 static int mvneta_port_power_up(struct mvneta_port *pp, int phy_mode)
5367 /* MAC Cause register should be cleared */
5368 mvreg_write(pp, MVNETA_UNIT_INTR_CAUSE, 0);
5370 if (phy_mode != PHY_INTERFACE_MODE_QSGMII &&
5371 phy_mode != PHY_INTERFACE_MODE_SGMII &&
5372 !phy_interface_mode_is_8023z(phy_mode) &&
5373 !phy_interface_mode_is_rgmii(phy_mode))
5379 /* Device initialization routine */
5380 static int mvneta_probe(struct platform_device *pdev)
5382 struct device_node *dn = pdev->dev.of_node;
5383 struct device_node *bm_node;
5384 struct mvneta_port *pp;
5385 struct net_device *dev;
5386 struct phylink *phylink;
5388 char hw_mac_addr[ETH_ALEN];
5389 phy_interface_t phy_mode;
5390 const char *mac_from;
5395 dev = devm_alloc_etherdev_mqs(&pdev->dev, sizeof(struct mvneta_port),
5396 txq_number, rxq_number);
5400 dev->tx_queue_len = MVNETA_MAX_TXD;
5401 dev->watchdog_timeo = 5 * HZ;
5402 dev->netdev_ops = &mvneta_netdev_ops;
5403 dev->ethtool_ops = &mvneta_eth_tool_ops;
5405 pp = netdev_priv(dev);
5406 spin_lock_init(&pp->lock);
5409 pp->rxq_def = rxq_def;
5410 pp->indir[0] = rxq_def;
5412 err = of_get_phy_mode(dn, &phy_mode);
5414 dev_err(&pdev->dev, "incorrect phy-mode\n");
5418 pp->phy_interface = phy_mode;
5420 comphy = devm_of_phy_get(&pdev->dev, dn, NULL);
5421 if (comphy == ERR_PTR(-EPROBE_DEFER))
5422 return -EPROBE_DEFER;
5427 pp->comphy = comphy;
5429 pp->base = devm_platform_ioremap_resource(pdev, 0);
5430 if (IS_ERR(pp->base))
5431 return PTR_ERR(pp->base);
5433 /* Get special SoC configurations */
5434 if (of_device_is_compatible(dn, "marvell,armada-3700-neta"))
5435 pp->neta_armada3700 = true;
5436 if (of_device_is_compatible(dn, "marvell,armada-ac5-neta")) {
5437 pp->neta_armada3700 = true;
5438 pp->neta_ac5 = true;
5441 dev->irq = irq_of_parse_and_map(dn, 0);
5445 pp->clk = devm_clk_get(&pdev->dev, "core");
5446 if (IS_ERR(pp->clk))
5447 pp->clk = devm_clk_get(&pdev->dev, NULL);
5448 if (IS_ERR(pp->clk)) {
5449 err = PTR_ERR(pp->clk);
5453 clk_prepare_enable(pp->clk);
5455 pp->clk_bus = devm_clk_get(&pdev->dev, "bus");
5456 if (!IS_ERR(pp->clk_bus))
5457 clk_prepare_enable(pp->clk_bus);
5459 pp->phylink_pcs.ops = &mvneta_phylink_pcs_ops;
5461 pp->phylink_config.dev = &dev->dev;
5462 pp->phylink_config.type = PHYLINK_NETDEV;
5463 pp->phylink_config.mac_capabilities = MAC_SYM_PAUSE | MAC_10 |
5464 MAC_100 | MAC_1000FD | MAC_2500FD;
5466 phy_interface_set_rgmii(pp->phylink_config.supported_interfaces);
5467 __set_bit(PHY_INTERFACE_MODE_QSGMII,
5468 pp->phylink_config.supported_interfaces);
5470 /* If a COMPHY is present, we can support any of the serdes
5471 * modes and switch between them.
5473 __set_bit(PHY_INTERFACE_MODE_SGMII,
5474 pp->phylink_config.supported_interfaces);
5475 __set_bit(PHY_INTERFACE_MODE_1000BASEX,
5476 pp->phylink_config.supported_interfaces);
5477 __set_bit(PHY_INTERFACE_MODE_2500BASEX,
5478 pp->phylink_config.supported_interfaces);
5479 } else if (phy_mode == PHY_INTERFACE_MODE_2500BASEX) {
5480 /* No COMPHY, with only 2500BASE-X mode supported */
5481 __set_bit(PHY_INTERFACE_MODE_2500BASEX,
5482 pp->phylink_config.supported_interfaces);
5483 } else if (phy_mode == PHY_INTERFACE_MODE_1000BASEX ||
5484 phy_mode == PHY_INTERFACE_MODE_SGMII) {
5485 /* No COMPHY, we can switch between 1000BASE-X and SGMII */
5486 __set_bit(PHY_INTERFACE_MODE_1000BASEX,
5487 pp->phylink_config.supported_interfaces);
5488 __set_bit(PHY_INTERFACE_MODE_SGMII,
5489 pp->phylink_config.supported_interfaces);
5492 phylink = phylink_create(&pp->phylink_config, pdev->dev.fwnode,
5493 phy_mode, &mvneta_phylink_ops);
5494 if (IS_ERR(phylink)) {
5495 err = PTR_ERR(phylink);
5499 pp->phylink = phylink;
5501 /* Alloc per-cpu port structure */
5502 pp->ports = alloc_percpu(struct mvneta_pcpu_port);
5505 goto err_free_phylink;
5508 /* Alloc per-cpu stats */
5509 pp->stats = netdev_alloc_pcpu_stats(struct mvneta_pcpu_stats);
5512 goto err_free_ports;
5515 err = of_get_ethdev_address(dn, dev);
5517 mac_from = "device tree";
5519 mvneta_get_mac_addr(pp, hw_mac_addr);
5520 if (is_valid_ether_addr(hw_mac_addr)) {
5521 mac_from = "hardware";
5522 eth_hw_addr_set(dev, hw_mac_addr);
5524 mac_from = "random";
5525 eth_hw_addr_random(dev);
5529 if (!of_property_read_u32(dn, "tx-csum-limit", &tx_csum_limit)) {
5530 if (tx_csum_limit < 0 ||
5531 tx_csum_limit > MVNETA_TX_CSUM_MAX_SIZE) {
5532 tx_csum_limit = MVNETA_TX_CSUM_DEF_SIZE;
5533 dev_info(&pdev->dev,
5534 "Wrong TX csum limit in DT, set to %dB\n",
5535 MVNETA_TX_CSUM_DEF_SIZE);
5537 } else if (of_device_is_compatible(dn, "marvell,armada-370-neta")) {
5538 tx_csum_limit = MVNETA_TX_CSUM_DEF_SIZE;
5540 tx_csum_limit = MVNETA_TX_CSUM_MAX_SIZE;
5543 pp->tx_csum_limit = tx_csum_limit;
5545 pp->dram_target_info = mv_mbus_dram_info();
5546 /* Armada3700 requires setting default configuration of Mbus
5547 * windows, however without using filled mbus_dram_target_info
5550 if (pp->dram_target_info || pp->neta_armada3700)
5551 mvneta_conf_mbus_windows(pp, pp->dram_target_info);
5553 pp->tx_ring_size = MVNETA_MAX_TXD;
5554 pp->rx_ring_size = MVNETA_MAX_RXD;
5557 SET_NETDEV_DEV(dev, &pdev->dev);
5559 pp->id = global_port_id++;
5561 /* Obtain access to BM resources if enabled and already initialized */
5562 bm_node = of_parse_phandle(dn, "buffer-manager", 0);
5564 pp->bm_priv = mvneta_bm_get(bm_node);
5566 err = mvneta_bm_port_init(pdev, pp);
5568 dev_info(&pdev->dev,
5569 "use SW buffer management\n");
5570 mvneta_bm_put(pp->bm_priv);
5574 /* Set RX packet offset correction for platforms, whose
5575 * NET_SKB_PAD, exceeds 64B. It should be 64B for 64-bit
5576 * platforms and 0B for 32-bit ones.
5578 pp->rx_offset_correction = max(0,
5580 MVNETA_RX_PKT_OFFSET_CORRECTION);
5582 of_node_put(bm_node);
5584 /* sw buffer management */
5586 pp->rx_offset_correction = MVNETA_SKB_HEADROOM;
5588 err = mvneta_init(&pdev->dev, pp);
5592 err = mvneta_port_power_up(pp, pp->phy_interface);
5594 dev_err(&pdev->dev, "can't power up port\n");
5598 /* Armada3700 network controller does not support per-cpu
5599 * operation, so only single NAPI should be initialized.
5601 if (pp->neta_armada3700) {
5602 netif_napi_add(dev, &pp->napi, mvneta_poll);
5604 for_each_present_cpu(cpu) {
5605 struct mvneta_pcpu_port *port =
5606 per_cpu_ptr(pp->ports, cpu);
5608 netif_napi_add(dev, &port->napi, mvneta_poll);
5613 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
5614 NETIF_F_TSO | NETIF_F_RXCSUM;
5615 dev->hw_features |= dev->features;
5616 dev->vlan_features |= dev->features;
5618 dev->xdp_features = NETDEV_XDP_ACT_BASIC |
5619 NETDEV_XDP_ACT_REDIRECT |
5620 NETDEV_XDP_ACT_NDO_XMIT |
5621 NETDEV_XDP_ACT_RX_SG |
5622 NETDEV_XDP_ACT_NDO_XMIT_SG;
5623 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
5624 netif_set_tso_max_segs(dev, MVNETA_MAX_TSO_SEGS);
5626 /* MTU range: 68 - 9676 */
5627 dev->min_mtu = ETH_MIN_MTU;
5628 /* 9676 == 9700 - 20 and rounding to 8 */
5629 dev->max_mtu = 9676;
5631 err = register_netdev(dev);
5633 dev_err(&pdev->dev, "failed to register\n");
5637 netdev_info(dev, "Using %s mac address %pM\n", mac_from,
5640 platform_set_drvdata(pdev, pp->dev);
5646 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
5647 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short,
5649 mvneta_bm_put(pp->bm_priv);
5651 free_percpu(pp->stats);
5653 free_percpu(pp->ports);
5656 phylink_destroy(pp->phylink);
5658 clk_disable_unprepare(pp->clk_bus);
5659 clk_disable_unprepare(pp->clk);
5661 irq_dispose_mapping(dev->irq);
5665 /* Device removal routine */
5666 static int mvneta_remove(struct platform_device *pdev)
5668 struct net_device *dev = platform_get_drvdata(pdev);
5669 struct mvneta_port *pp = netdev_priv(dev);
5671 unregister_netdev(dev);
5672 clk_disable_unprepare(pp->clk_bus);
5673 clk_disable_unprepare(pp->clk);
5674 free_percpu(pp->ports);
5675 free_percpu(pp->stats);
5676 irq_dispose_mapping(dev->irq);
5677 phylink_destroy(pp->phylink);
5680 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
5681 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short,
5683 mvneta_bm_put(pp->bm_priv);
5689 #ifdef CONFIG_PM_SLEEP
5690 static int mvneta_suspend(struct device *device)
5693 struct net_device *dev = dev_get_drvdata(device);
5694 struct mvneta_port *pp = netdev_priv(dev);
5696 if (!netif_running(dev))
5699 if (!pp->neta_armada3700) {
5700 spin_lock(&pp->lock);
5701 pp->is_stopped = true;
5702 spin_unlock(&pp->lock);
5704 cpuhp_state_remove_instance_nocalls(online_hpstate,
5706 cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
5711 mvneta_stop_dev(pp);
5714 for (queue = 0; queue < rxq_number; queue++) {
5715 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
5717 mvneta_rxq_drop_pkts(pp, rxq);
5720 for (queue = 0; queue < txq_number; queue++) {
5721 struct mvneta_tx_queue *txq = &pp->txqs[queue];
5723 mvneta_txq_hw_deinit(pp, txq);
5727 netif_device_detach(dev);
5728 clk_disable_unprepare(pp->clk_bus);
5729 clk_disable_unprepare(pp->clk);
5734 static int mvneta_resume(struct device *device)
5736 struct platform_device *pdev = to_platform_device(device);
5737 struct net_device *dev = dev_get_drvdata(device);
5738 struct mvneta_port *pp = netdev_priv(dev);
5741 clk_prepare_enable(pp->clk);
5742 if (!IS_ERR(pp->clk_bus))
5743 clk_prepare_enable(pp->clk_bus);
5744 if (pp->dram_target_info || pp->neta_armada3700)
5745 mvneta_conf_mbus_windows(pp, pp->dram_target_info);
5747 err = mvneta_bm_port_init(pdev, pp);
5749 dev_info(&pdev->dev, "use SW buffer management\n");
5750 pp->rx_offset_correction = MVNETA_SKB_HEADROOM;
5754 mvneta_defaults_set(pp);
5755 err = mvneta_port_power_up(pp, pp->phy_interface);
5757 dev_err(device, "can't power up port\n");
5761 netif_device_attach(dev);
5763 if (!netif_running(dev))
5766 for (queue = 0; queue < rxq_number; queue++) {
5767 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
5769 rxq->next_desc_to_proc = 0;
5770 mvneta_rxq_hw_init(pp, rxq);
5773 for (queue = 0; queue < txq_number; queue++) {
5774 struct mvneta_tx_queue *txq = &pp->txqs[queue];
5776 txq->next_desc_to_proc = 0;
5777 mvneta_txq_hw_init(pp, txq);
5780 if (!pp->neta_armada3700) {
5781 spin_lock(&pp->lock);
5782 pp->is_stopped = false;
5783 spin_unlock(&pp->lock);
5784 cpuhp_state_add_instance_nocalls(online_hpstate,
5786 cpuhp_state_add_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
5791 mvneta_start_dev(pp);
5793 mvneta_set_rx_mode(dev);
5799 static SIMPLE_DEV_PM_OPS(mvneta_pm_ops, mvneta_suspend, mvneta_resume);
5801 static const struct of_device_id mvneta_match[] = {
5802 { .compatible = "marvell,armada-370-neta" },
5803 { .compatible = "marvell,armada-xp-neta" },
5804 { .compatible = "marvell,armada-3700-neta" },
5805 { .compatible = "marvell,armada-ac5-neta" },
5808 MODULE_DEVICE_TABLE(of, mvneta_match);
5810 static struct platform_driver mvneta_driver = {
5811 .probe = mvneta_probe,
5812 .remove = mvneta_remove,
5814 .name = MVNETA_DRIVER_NAME,
5815 .of_match_table = mvneta_match,
5816 .pm = &mvneta_pm_ops,
5820 static int __init mvneta_driver_init(void)
5824 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "net/mvneta:online",
5826 mvneta_cpu_down_prepare);
5829 online_hpstate = ret;
5830 ret = cpuhp_setup_state_multi(CPUHP_NET_MVNETA_DEAD, "net/mvneta:dead",
5831 NULL, mvneta_cpu_dead);
5835 ret = platform_driver_register(&mvneta_driver);
5841 cpuhp_remove_multi_state(CPUHP_NET_MVNETA_DEAD);
5843 cpuhp_remove_multi_state(online_hpstate);
5847 module_init(mvneta_driver_init);
5849 static void __exit mvneta_driver_exit(void)
5851 platform_driver_unregister(&mvneta_driver);
5852 cpuhp_remove_multi_state(CPUHP_NET_MVNETA_DEAD);
5853 cpuhp_remove_multi_state(online_hpstate);
5855 module_exit(mvneta_driver_exit);
5857 MODULE_DESCRIPTION("Marvell NETA Ethernet Driver - www.marvell.com");
5859 MODULE_LICENSE("GPL");
5861 module_param(rxq_number, int, 0444);
5862 module_param(txq_number, int, 0444);
5864 module_param(rxq_def, int, 0444);
5865 module_param(rx_copybreak, int, 0644);