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 <linux/bpf_trace.h>
44 #define MVNETA_RXQ_CONFIG_REG(q) (0x1400 + ((q) << 2))
45 #define MVNETA_RXQ_HW_BUF_ALLOC BIT(0)
46 #define MVNETA_RXQ_SHORT_POOL_ID_SHIFT 4
47 #define MVNETA_RXQ_SHORT_POOL_ID_MASK 0x30
48 #define MVNETA_RXQ_LONG_POOL_ID_SHIFT 6
49 #define MVNETA_RXQ_LONG_POOL_ID_MASK 0xc0
50 #define MVNETA_RXQ_PKT_OFFSET_ALL_MASK (0xf << 8)
51 #define MVNETA_RXQ_PKT_OFFSET_MASK(offs) ((offs) << 8)
52 #define MVNETA_RXQ_THRESHOLD_REG(q) (0x14c0 + ((q) << 2))
53 #define MVNETA_RXQ_NON_OCCUPIED(v) ((v) << 16)
54 #define MVNETA_RXQ_BASE_ADDR_REG(q) (0x1480 + ((q) << 2))
55 #define MVNETA_RXQ_SIZE_REG(q) (0x14a0 + ((q) << 2))
56 #define MVNETA_RXQ_BUF_SIZE_SHIFT 19
57 #define MVNETA_RXQ_BUF_SIZE_MASK (0x1fff << 19)
58 #define MVNETA_RXQ_STATUS_REG(q) (0x14e0 + ((q) << 2))
59 #define MVNETA_RXQ_OCCUPIED_ALL_MASK 0x3fff
60 #define MVNETA_RXQ_STATUS_UPDATE_REG(q) (0x1500 + ((q) << 2))
61 #define MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT 16
62 #define MVNETA_RXQ_ADD_NON_OCCUPIED_MAX 255
63 #define MVNETA_PORT_POOL_BUFFER_SZ_REG(pool) (0x1700 + ((pool) << 2))
64 #define MVNETA_PORT_POOL_BUFFER_SZ_SHIFT 3
65 #define MVNETA_PORT_POOL_BUFFER_SZ_MASK 0xfff8
66 #define MVNETA_PORT_RX_RESET 0x1cc0
67 #define MVNETA_PORT_RX_DMA_RESET BIT(0)
68 #define MVNETA_PHY_ADDR 0x2000
69 #define MVNETA_PHY_ADDR_MASK 0x1f
70 #define MVNETA_MBUS_RETRY 0x2010
71 #define MVNETA_UNIT_INTR_CAUSE 0x2080
72 #define MVNETA_UNIT_CONTROL 0x20B0
73 #define MVNETA_PHY_POLLING_ENABLE BIT(1)
74 #define MVNETA_WIN_BASE(w) (0x2200 + ((w) << 3))
75 #define MVNETA_WIN_SIZE(w) (0x2204 + ((w) << 3))
76 #define MVNETA_WIN_REMAP(w) (0x2280 + ((w) << 2))
77 #define MVNETA_BASE_ADDR_ENABLE 0x2290
78 #define MVNETA_ACCESS_PROTECT_ENABLE 0x2294
79 #define MVNETA_PORT_CONFIG 0x2400
80 #define MVNETA_UNI_PROMISC_MODE BIT(0)
81 #define MVNETA_DEF_RXQ(q) ((q) << 1)
82 #define MVNETA_DEF_RXQ_ARP(q) ((q) << 4)
83 #define MVNETA_TX_UNSET_ERR_SUM BIT(12)
84 #define MVNETA_DEF_RXQ_TCP(q) ((q) << 16)
85 #define MVNETA_DEF_RXQ_UDP(q) ((q) << 19)
86 #define MVNETA_DEF_RXQ_BPDU(q) ((q) << 22)
87 #define MVNETA_RX_CSUM_WITH_PSEUDO_HDR BIT(25)
88 #define MVNETA_PORT_CONFIG_DEFL_VALUE(q) (MVNETA_DEF_RXQ(q) | \
89 MVNETA_DEF_RXQ_ARP(q) | \
90 MVNETA_DEF_RXQ_TCP(q) | \
91 MVNETA_DEF_RXQ_UDP(q) | \
92 MVNETA_DEF_RXQ_BPDU(q) | \
93 MVNETA_TX_UNSET_ERR_SUM | \
94 MVNETA_RX_CSUM_WITH_PSEUDO_HDR)
95 #define MVNETA_PORT_CONFIG_EXTEND 0x2404
96 #define MVNETA_MAC_ADDR_LOW 0x2414
97 #define MVNETA_MAC_ADDR_HIGH 0x2418
98 #define MVNETA_SDMA_CONFIG 0x241c
99 #define MVNETA_SDMA_BRST_SIZE_16 4
100 #define MVNETA_RX_BRST_SZ_MASK(burst) ((burst) << 1)
101 #define MVNETA_RX_NO_DATA_SWAP BIT(4)
102 #define MVNETA_TX_NO_DATA_SWAP BIT(5)
103 #define MVNETA_DESC_SWAP BIT(6)
104 #define MVNETA_TX_BRST_SZ_MASK(burst) ((burst) << 22)
105 #define MVNETA_PORT_STATUS 0x2444
106 #define MVNETA_TX_IN_PRGRS BIT(1)
107 #define MVNETA_TX_FIFO_EMPTY BIT(8)
108 #define MVNETA_RX_MIN_FRAME_SIZE 0x247c
109 #define MVNETA_SERDES_CFG 0x24A0
110 #define MVNETA_SGMII_SERDES_PROTO 0x0cc7
111 #define MVNETA_QSGMII_SERDES_PROTO 0x0667
112 #define MVNETA_TYPE_PRIO 0x24bc
113 #define MVNETA_FORCE_UNI BIT(21)
114 #define MVNETA_TXQ_CMD_1 0x24e4
115 #define MVNETA_TXQ_CMD 0x2448
116 #define MVNETA_TXQ_DISABLE_SHIFT 8
117 #define MVNETA_TXQ_ENABLE_MASK 0x000000ff
118 #define MVNETA_RX_DISCARD_FRAME_COUNT 0x2484
119 #define MVNETA_OVERRUN_FRAME_COUNT 0x2488
120 #define MVNETA_GMAC_CLOCK_DIVIDER 0x24f4
121 #define MVNETA_GMAC_1MS_CLOCK_ENABLE BIT(31)
122 #define MVNETA_ACC_MODE 0x2500
123 #define MVNETA_BM_ADDRESS 0x2504
124 #define MVNETA_CPU_MAP(cpu) (0x2540 + ((cpu) << 2))
125 #define MVNETA_CPU_RXQ_ACCESS_ALL_MASK 0x000000ff
126 #define MVNETA_CPU_TXQ_ACCESS_ALL_MASK 0x0000ff00
127 #define MVNETA_CPU_RXQ_ACCESS(rxq) BIT(rxq)
128 #define MVNETA_CPU_TXQ_ACCESS(txq) BIT(txq + 8)
129 #define MVNETA_RXQ_TIME_COAL_REG(q) (0x2580 + ((q) << 2))
131 /* Exception Interrupt Port/Queue Cause register
133 * Their behavior depend of the mapping done using the PCPX2Q
134 * registers. For a given CPU if the bit associated to a queue is not
135 * set, then for the register a read from this CPU will always return
136 * 0 and a write won't do anything
139 #define MVNETA_INTR_NEW_CAUSE 0x25a0
140 #define MVNETA_INTR_NEW_MASK 0x25a4
142 /* bits 0..7 = TXQ SENT, one bit per queue.
143 * bits 8..15 = RXQ OCCUP, one bit per queue.
144 * bits 16..23 = RXQ FREE, one bit per queue.
145 * bit 29 = OLD_REG_SUM, see old reg ?
146 * bit 30 = TX_ERR_SUM, one bit for 4 ports
147 * bit 31 = MISC_SUM, one bit for 4 ports
149 #define MVNETA_TX_INTR_MASK(nr_txqs) (((1 << nr_txqs) - 1) << 0)
150 #define MVNETA_TX_INTR_MASK_ALL (0xff << 0)
151 #define MVNETA_RX_INTR_MASK(nr_rxqs) (((1 << nr_rxqs) - 1) << 8)
152 #define MVNETA_RX_INTR_MASK_ALL (0xff << 8)
153 #define MVNETA_MISCINTR_INTR_MASK BIT(31)
155 #define MVNETA_INTR_OLD_CAUSE 0x25a8
156 #define MVNETA_INTR_OLD_MASK 0x25ac
158 /* Data Path Port/Queue Cause Register */
159 #define MVNETA_INTR_MISC_CAUSE 0x25b0
160 #define MVNETA_INTR_MISC_MASK 0x25b4
162 #define MVNETA_CAUSE_PHY_STATUS_CHANGE BIT(0)
163 #define MVNETA_CAUSE_LINK_CHANGE BIT(1)
164 #define MVNETA_CAUSE_PTP BIT(4)
166 #define MVNETA_CAUSE_INTERNAL_ADDR_ERR BIT(7)
167 #define MVNETA_CAUSE_RX_OVERRUN BIT(8)
168 #define MVNETA_CAUSE_RX_CRC_ERROR BIT(9)
169 #define MVNETA_CAUSE_RX_LARGE_PKT BIT(10)
170 #define MVNETA_CAUSE_TX_UNDERUN BIT(11)
171 #define MVNETA_CAUSE_PRBS_ERR BIT(12)
172 #define MVNETA_CAUSE_PSC_SYNC_CHANGE BIT(13)
173 #define MVNETA_CAUSE_SERDES_SYNC_ERR BIT(14)
175 #define MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT 16
176 #define MVNETA_CAUSE_BMU_ALLOC_ERR_ALL_MASK (0xF << MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT)
177 #define MVNETA_CAUSE_BMU_ALLOC_ERR_MASK(pool) (1 << (MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT + (pool)))
179 #define MVNETA_CAUSE_TXQ_ERROR_SHIFT 24
180 #define MVNETA_CAUSE_TXQ_ERROR_ALL_MASK (0xFF << MVNETA_CAUSE_TXQ_ERROR_SHIFT)
181 #define MVNETA_CAUSE_TXQ_ERROR_MASK(q) (1 << (MVNETA_CAUSE_TXQ_ERROR_SHIFT + (q)))
183 #define MVNETA_INTR_ENABLE 0x25b8
184 #define MVNETA_TXQ_INTR_ENABLE_ALL_MASK 0x0000ff00
185 #define MVNETA_RXQ_INTR_ENABLE_ALL_MASK 0x000000ff
187 #define MVNETA_RXQ_CMD 0x2680
188 #define MVNETA_RXQ_DISABLE_SHIFT 8
189 #define MVNETA_RXQ_ENABLE_MASK 0x000000ff
190 #define MVETH_TXQ_TOKEN_COUNT_REG(q) (0x2700 + ((q) << 4))
191 #define MVETH_TXQ_TOKEN_CFG_REG(q) (0x2704 + ((q) << 4))
192 #define MVNETA_GMAC_CTRL_0 0x2c00
193 #define MVNETA_GMAC_MAX_RX_SIZE_SHIFT 2
194 #define MVNETA_GMAC_MAX_RX_SIZE_MASK 0x7ffc
195 #define MVNETA_GMAC0_PORT_1000BASE_X BIT(1)
196 #define MVNETA_GMAC0_PORT_ENABLE BIT(0)
197 #define MVNETA_GMAC_CTRL_2 0x2c08
198 #define MVNETA_GMAC2_INBAND_AN_ENABLE BIT(0)
199 #define MVNETA_GMAC2_PCS_ENABLE BIT(3)
200 #define MVNETA_GMAC2_PORT_RGMII BIT(4)
201 #define MVNETA_GMAC2_PORT_RESET BIT(6)
202 #define MVNETA_GMAC_STATUS 0x2c10
203 #define MVNETA_GMAC_LINK_UP BIT(0)
204 #define MVNETA_GMAC_SPEED_1000 BIT(1)
205 #define MVNETA_GMAC_SPEED_100 BIT(2)
206 #define MVNETA_GMAC_FULL_DUPLEX BIT(3)
207 #define MVNETA_GMAC_RX_FLOW_CTRL_ENABLE BIT(4)
208 #define MVNETA_GMAC_TX_FLOW_CTRL_ENABLE BIT(5)
209 #define MVNETA_GMAC_RX_FLOW_CTRL_ACTIVE BIT(6)
210 #define MVNETA_GMAC_TX_FLOW_CTRL_ACTIVE BIT(7)
211 #define MVNETA_GMAC_AN_COMPLETE BIT(11)
212 #define MVNETA_GMAC_SYNC_OK BIT(14)
213 #define MVNETA_GMAC_AUTONEG_CONFIG 0x2c0c
214 #define MVNETA_GMAC_FORCE_LINK_DOWN BIT(0)
215 #define MVNETA_GMAC_FORCE_LINK_PASS BIT(1)
216 #define MVNETA_GMAC_INBAND_AN_ENABLE BIT(2)
217 #define MVNETA_GMAC_AN_BYPASS_ENABLE BIT(3)
218 #define MVNETA_GMAC_INBAND_RESTART_AN BIT(4)
219 #define MVNETA_GMAC_CONFIG_MII_SPEED BIT(5)
220 #define MVNETA_GMAC_CONFIG_GMII_SPEED BIT(6)
221 #define MVNETA_GMAC_AN_SPEED_EN BIT(7)
222 #define MVNETA_GMAC_CONFIG_FLOW_CTRL BIT(8)
223 #define MVNETA_GMAC_ADVERT_SYM_FLOW_CTRL BIT(9)
224 #define MVNETA_GMAC_AN_FLOW_CTRL_EN BIT(11)
225 #define MVNETA_GMAC_CONFIG_FULL_DUPLEX BIT(12)
226 #define MVNETA_GMAC_AN_DUPLEX_EN BIT(13)
227 #define MVNETA_GMAC_CTRL_4 0x2c90
228 #define MVNETA_GMAC4_SHORT_PREAMBLE_ENABLE BIT(1)
229 #define MVNETA_MIB_COUNTERS_BASE 0x3000
230 #define MVNETA_MIB_LATE_COLLISION 0x7c
231 #define MVNETA_DA_FILT_SPEC_MCAST 0x3400
232 #define MVNETA_DA_FILT_OTH_MCAST 0x3500
233 #define MVNETA_DA_FILT_UCAST_BASE 0x3600
234 #define MVNETA_TXQ_BASE_ADDR_REG(q) (0x3c00 + ((q) << 2))
235 #define MVNETA_TXQ_SIZE_REG(q) (0x3c20 + ((q) << 2))
236 #define MVNETA_TXQ_SENT_THRESH_ALL_MASK 0x3fff0000
237 #define MVNETA_TXQ_SENT_THRESH_MASK(coal) ((coal) << 16)
238 #define MVNETA_TXQ_UPDATE_REG(q) (0x3c60 + ((q) << 2))
239 #define MVNETA_TXQ_DEC_SENT_SHIFT 16
240 #define MVNETA_TXQ_DEC_SENT_MASK 0xff
241 #define MVNETA_TXQ_STATUS_REG(q) (0x3c40 + ((q) << 2))
242 #define MVNETA_TXQ_SENT_DESC_SHIFT 16
243 #define MVNETA_TXQ_SENT_DESC_MASK 0x3fff0000
244 #define MVNETA_PORT_TX_RESET 0x3cf0
245 #define MVNETA_PORT_TX_DMA_RESET BIT(0)
246 #define MVNETA_TX_MTU 0x3e0c
247 #define MVNETA_TX_TOKEN_SIZE 0x3e14
248 #define MVNETA_TX_TOKEN_SIZE_MAX 0xffffffff
249 #define MVNETA_TXQ_TOKEN_SIZE_REG(q) (0x3e40 + ((q) << 2))
250 #define MVNETA_TXQ_TOKEN_SIZE_MAX 0x7fffffff
252 #define MVNETA_LPI_CTRL_0 0x2cc0
253 #define MVNETA_LPI_CTRL_1 0x2cc4
254 #define MVNETA_LPI_REQUEST_ENABLE BIT(0)
255 #define MVNETA_LPI_CTRL_2 0x2cc8
256 #define MVNETA_LPI_STATUS 0x2ccc
258 #define MVNETA_CAUSE_TXQ_SENT_DESC_ALL_MASK 0xff
260 /* Descriptor ring Macros */
261 #define MVNETA_QUEUE_NEXT_DESC(q, index) \
262 (((index) < (q)->last_desc) ? ((index) + 1) : 0)
264 /* Various constants */
267 #define MVNETA_TXDONE_COAL_PKTS 0 /* interrupt per packet */
268 #define MVNETA_RX_COAL_PKTS 32
269 #define MVNETA_RX_COAL_USEC 100
271 /* The two bytes Marvell header. Either contains a special value used
272 * by Marvell switches when a specific hardware mode is enabled (not
273 * supported by this driver) or is filled automatically by zeroes on
274 * the RX side. Those two bytes being at the front of the Ethernet
275 * header, they allow to have the IP header aligned on a 4 bytes
276 * boundary automatically: the hardware skips those two bytes on its
279 #define MVNETA_MH_SIZE 2
281 #define MVNETA_VLAN_TAG_LEN 4
283 #define MVNETA_TX_CSUM_DEF_SIZE 1600
284 #define MVNETA_TX_CSUM_MAX_SIZE 9800
285 #define MVNETA_ACC_MODE_EXT1 1
286 #define MVNETA_ACC_MODE_EXT2 2
288 #define MVNETA_MAX_DECODE_WIN 6
290 /* Timeout constants */
291 #define MVNETA_TX_DISABLE_TIMEOUT_MSEC 1000
292 #define MVNETA_RX_DISABLE_TIMEOUT_MSEC 1000
293 #define MVNETA_TX_FIFO_EMPTY_TIMEOUT 10000
295 #define MVNETA_TX_MTU_MAX 0x3ffff
297 /* The RSS lookup table actually has 256 entries but we do not use
300 #define MVNETA_RSS_LU_TABLE_SIZE 1
302 /* Max number of Rx descriptors */
303 #define MVNETA_MAX_RXD 512
305 /* Max number of Tx descriptors */
306 #define MVNETA_MAX_TXD 1024
308 /* Max number of allowed TCP segments for software TSO */
309 #define MVNETA_MAX_TSO_SEGS 100
311 #define MVNETA_MAX_SKB_DESCS (MVNETA_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
313 /* descriptor aligned size */
314 #define MVNETA_DESC_ALIGNED_SIZE 32
316 /* Number of bytes to be taken into account by HW when putting incoming data
317 * to the buffers. It is needed in case NET_SKB_PAD exceeds maximum packet
318 * offset supported in MVNETA_RXQ_CONFIG_REG(q) registers.
320 #define MVNETA_RX_PKT_OFFSET_CORRECTION 64
322 #define MVNETA_RX_PKT_SIZE(mtu) \
323 ALIGN((mtu) + MVNETA_MH_SIZE + MVNETA_VLAN_TAG_LEN + \
324 ETH_HLEN + ETH_FCS_LEN, \
327 #define MVNETA_SKB_HEADROOM max(XDP_PACKET_HEADROOM, NET_SKB_PAD)
328 #define MVNETA_SKB_PAD (SKB_DATA_ALIGN(sizeof(struct skb_shared_info) + \
329 MVNETA_SKB_HEADROOM))
330 #define MVNETA_SKB_SIZE(len) (SKB_DATA_ALIGN(len) + MVNETA_SKB_PAD)
331 #define MVNETA_MAX_RX_BUF_SIZE (PAGE_SIZE - MVNETA_SKB_PAD)
333 #define IS_TSO_HEADER(txq, addr) \
334 ((addr >= txq->tso_hdrs_phys) && \
335 (addr < txq->tso_hdrs_phys + txq->size * TSO_HEADER_SIZE))
337 #define MVNETA_RX_GET_BM_POOL_ID(rxd) \
338 (((rxd)->status & MVNETA_RXD_BM_POOL_MASK) >> MVNETA_RXD_BM_POOL_SHIFT)
341 ETHTOOL_STAT_EEE_WAKEUP,
342 ETHTOOL_STAT_SKB_ALLOC_ERR,
343 ETHTOOL_STAT_REFILL_ERR,
347 struct mvneta_statistic {
348 unsigned short offset;
350 const char name[ETH_GSTRING_LEN];
357 #define MVNETA_XDP_PASS BIT(0)
358 #define MVNETA_XDP_DROPPED BIT(1)
359 #define MVNETA_XDP_TX BIT(2)
360 #define MVNETA_XDP_REDIR BIT(3)
362 static const struct mvneta_statistic mvneta_statistics[] = {
363 { 0x3000, T_REG_64, "good_octets_received", },
364 { 0x3010, T_REG_32, "good_frames_received", },
365 { 0x3008, T_REG_32, "bad_octets_received", },
366 { 0x3014, T_REG_32, "bad_frames_received", },
367 { 0x3018, T_REG_32, "broadcast_frames_received", },
368 { 0x301c, T_REG_32, "multicast_frames_received", },
369 { 0x3050, T_REG_32, "unrec_mac_control_received", },
370 { 0x3058, T_REG_32, "good_fc_received", },
371 { 0x305c, T_REG_32, "bad_fc_received", },
372 { 0x3060, T_REG_32, "undersize_received", },
373 { 0x3064, T_REG_32, "fragments_received", },
374 { 0x3068, T_REG_32, "oversize_received", },
375 { 0x306c, T_REG_32, "jabber_received", },
376 { 0x3070, T_REG_32, "mac_receive_error", },
377 { 0x3074, T_REG_32, "bad_crc_event", },
378 { 0x3078, T_REG_32, "collision", },
379 { 0x307c, T_REG_32, "late_collision", },
380 { 0x2484, T_REG_32, "rx_discard", },
381 { 0x2488, T_REG_32, "rx_overrun", },
382 { 0x3020, T_REG_32, "frames_64_octets", },
383 { 0x3024, T_REG_32, "frames_65_to_127_octets", },
384 { 0x3028, T_REG_32, "frames_128_to_255_octets", },
385 { 0x302c, T_REG_32, "frames_256_to_511_octets", },
386 { 0x3030, T_REG_32, "frames_512_to_1023_octets", },
387 { 0x3034, T_REG_32, "frames_1024_to_max_octets", },
388 { 0x3038, T_REG_64, "good_octets_sent", },
389 { 0x3040, T_REG_32, "good_frames_sent", },
390 { 0x3044, T_REG_32, "excessive_collision", },
391 { 0x3048, T_REG_32, "multicast_frames_sent", },
392 { 0x304c, T_REG_32, "broadcast_frames_sent", },
393 { 0x3054, T_REG_32, "fc_sent", },
394 { 0x300c, T_REG_32, "internal_mac_transmit_err", },
395 { ETHTOOL_STAT_EEE_WAKEUP, T_SW, "eee_wakeup_errors", },
396 { ETHTOOL_STAT_SKB_ALLOC_ERR, T_SW, "skb_alloc_errors", },
397 { ETHTOOL_STAT_REFILL_ERR, T_SW, "refill_errors", },
400 struct mvneta_pcpu_stats {
401 struct u64_stats_sync syncp;
410 struct mvneta_pcpu_port {
411 /* Pointer to the shared port */
412 struct mvneta_port *pp;
414 /* Pointer to the CPU-local NAPI struct */
415 struct napi_struct napi;
417 /* Cause of the previous interrupt */
423 struct mvneta_pcpu_port __percpu *ports;
424 struct mvneta_pcpu_stats __percpu *stats;
428 struct mvneta_rx_queue *rxqs;
429 struct mvneta_tx_queue *txqs;
430 struct net_device *dev;
431 struct hlist_node node_online;
432 struct hlist_node node_dead;
434 /* Protect the access to the percpu interrupt registers,
435 * ensuring that the configuration remains coherent.
441 struct napi_struct napi;
443 struct bpf_prog *xdp_prog;
453 phy_interface_t phy_interface;
454 struct device_node *dn;
455 unsigned int tx_csum_limit;
456 struct phylink *phylink;
457 struct phylink_config phylink_config;
460 struct mvneta_bm *bm_priv;
461 struct mvneta_bm_pool *pool_long;
462 struct mvneta_bm_pool *pool_short;
469 u64 ethtool_stats[ARRAY_SIZE(mvneta_statistics)];
471 u32 indir[MVNETA_RSS_LU_TABLE_SIZE];
473 /* Flags for special SoC configurations */
474 bool neta_armada3700;
475 u16 rx_offset_correction;
476 const struct mbus_dram_target_info *dram_target_info;
479 /* The mvneta_tx_desc and mvneta_rx_desc structures describe the
480 * layout of the transmit and reception DMA descriptors, and their
481 * layout is therefore defined by the hardware design
484 #define MVNETA_TX_L3_OFF_SHIFT 0
485 #define MVNETA_TX_IP_HLEN_SHIFT 8
486 #define MVNETA_TX_L4_UDP BIT(16)
487 #define MVNETA_TX_L3_IP6 BIT(17)
488 #define MVNETA_TXD_IP_CSUM BIT(18)
489 #define MVNETA_TXD_Z_PAD BIT(19)
490 #define MVNETA_TXD_L_DESC BIT(20)
491 #define MVNETA_TXD_F_DESC BIT(21)
492 #define MVNETA_TXD_FLZ_DESC (MVNETA_TXD_Z_PAD | \
493 MVNETA_TXD_L_DESC | \
495 #define MVNETA_TX_L4_CSUM_FULL BIT(30)
496 #define MVNETA_TX_L4_CSUM_NOT BIT(31)
498 #define MVNETA_RXD_ERR_CRC 0x0
499 #define MVNETA_RXD_BM_POOL_SHIFT 13
500 #define MVNETA_RXD_BM_POOL_MASK (BIT(13) | BIT(14))
501 #define MVNETA_RXD_ERR_SUMMARY BIT(16)
502 #define MVNETA_RXD_ERR_OVERRUN BIT(17)
503 #define MVNETA_RXD_ERR_LEN BIT(18)
504 #define MVNETA_RXD_ERR_RESOURCE (BIT(17) | BIT(18))
505 #define MVNETA_RXD_ERR_CODE_MASK (BIT(17) | BIT(18))
506 #define MVNETA_RXD_L3_IP4 BIT(25)
507 #define MVNETA_RXD_LAST_DESC BIT(26)
508 #define MVNETA_RXD_FIRST_DESC BIT(27)
509 #define MVNETA_RXD_FIRST_LAST_DESC (MVNETA_RXD_FIRST_DESC | \
510 MVNETA_RXD_LAST_DESC)
511 #define MVNETA_RXD_L4_CSUM_OK BIT(30)
513 #if defined(__LITTLE_ENDIAN)
514 struct mvneta_tx_desc {
515 u32 command; /* Options used by HW for packet transmitting.*/
516 u16 reserved1; /* csum_l4 (for future use) */
517 u16 data_size; /* Data size of transmitted packet in bytes */
518 u32 buf_phys_addr; /* Physical addr of transmitted buffer */
519 u32 reserved2; /* hw_cmd - (for future use, PMT) */
520 u32 reserved3[4]; /* Reserved - (for future use) */
523 struct mvneta_rx_desc {
524 u32 status; /* Info about received packet */
525 u16 reserved1; /* pnc_info - (for future use, PnC) */
526 u16 data_size; /* Size of received packet in bytes */
528 u32 buf_phys_addr; /* Physical address of the buffer */
529 u32 reserved2; /* pnc_flow_id (for future use, PnC) */
531 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
532 u16 reserved3; /* prefetch_cmd, for future use */
533 u16 reserved4; /* csum_l4 - (for future use, PnC) */
535 u32 reserved5; /* pnc_extra PnC (for future use, PnC) */
536 u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */
539 struct mvneta_tx_desc {
540 u16 data_size; /* Data size of transmitted packet in bytes */
541 u16 reserved1; /* csum_l4 (for future use) */
542 u32 command; /* Options used by HW for packet transmitting.*/
543 u32 reserved2; /* hw_cmd - (for future use, PMT) */
544 u32 buf_phys_addr; /* Physical addr of transmitted buffer */
545 u32 reserved3[4]; /* Reserved - (for future use) */
548 struct mvneta_rx_desc {
549 u16 data_size; /* Size of received packet in bytes */
550 u16 reserved1; /* pnc_info - (for future use, PnC) */
551 u32 status; /* Info about received packet */
553 u32 reserved2; /* pnc_flow_id (for future use, PnC) */
554 u32 buf_phys_addr; /* Physical address of the buffer */
556 u16 reserved4; /* csum_l4 - (for future use, PnC) */
557 u16 reserved3; /* prefetch_cmd, for future use */
558 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
560 u32 reserved5; /* pnc_extra PnC (for future use, PnC) */
561 u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */
565 enum mvneta_tx_buf_type {
571 struct mvneta_tx_buf {
572 enum mvneta_tx_buf_type type;
574 struct xdp_frame *xdpf;
579 struct mvneta_tx_queue {
580 /* Number of this TX queue, in the range 0-7 */
583 /* Number of TX DMA descriptors in the descriptor ring */
586 /* Number of currently used TX DMA descriptor in the
591 int tx_stop_threshold;
592 int tx_wake_threshold;
594 /* Array of transmitted buffers */
595 struct mvneta_tx_buf *buf;
597 /* Index of last TX DMA descriptor that was inserted */
600 /* Index of the TX DMA descriptor to be cleaned up */
605 /* Virtual address of the TX DMA descriptors array */
606 struct mvneta_tx_desc *descs;
608 /* DMA address of the TX DMA descriptors array */
609 dma_addr_t descs_phys;
611 /* Index of the last TX DMA descriptor */
614 /* Index of the next TX DMA descriptor to process */
615 int next_desc_to_proc;
617 /* DMA buffers for TSO headers */
620 /* DMA address of TSO headers */
621 dma_addr_t tso_hdrs_phys;
623 /* Affinity mask for CPUs*/
624 cpumask_t affinity_mask;
627 struct mvneta_rx_queue {
628 /* rx queue number, in the range 0-7 */
631 /* num of rx descriptors in the rx descriptor ring */
638 struct page_pool *page_pool;
639 struct xdp_rxq_info xdp_rxq;
641 /* Virtual address of the RX buffer */
642 void **buf_virt_addr;
644 /* Virtual address of the RX DMA descriptors array */
645 struct mvneta_rx_desc *descs;
647 /* DMA address of the RX DMA descriptors array */
648 dma_addr_t descs_phys;
650 /* Index of the last RX DMA descriptor */
653 /* Index of the next RX DMA descriptor to process */
654 int next_desc_to_proc;
656 /* Index of first RX DMA descriptor to refill */
660 /* pointer to uncomplete skb buffer */
669 static enum cpuhp_state online_hpstate;
670 /* The hardware supports eight (8) rx queues, but we are only allowing
671 * the first one to be used. Therefore, let's just allocate one queue.
673 static int rxq_number = 8;
674 static int txq_number = 8;
678 static int rx_copybreak __read_mostly = 256;
680 /* HW BM need that each port be identify by a unique ID */
681 static int global_port_id;
683 #define MVNETA_DRIVER_NAME "mvneta"
684 #define MVNETA_DRIVER_VERSION "1.0"
686 /* Utility/helper methods */
688 /* Write helper method */
689 static void mvreg_write(struct mvneta_port *pp, u32 offset, u32 data)
691 writel(data, pp->base + offset);
694 /* Read helper method */
695 static u32 mvreg_read(struct mvneta_port *pp, u32 offset)
697 return readl(pp->base + offset);
700 /* Increment txq get counter */
701 static void mvneta_txq_inc_get(struct mvneta_tx_queue *txq)
703 txq->txq_get_index++;
704 if (txq->txq_get_index == txq->size)
705 txq->txq_get_index = 0;
708 /* Increment txq put counter */
709 static void mvneta_txq_inc_put(struct mvneta_tx_queue *txq)
711 txq->txq_put_index++;
712 if (txq->txq_put_index == txq->size)
713 txq->txq_put_index = 0;
717 /* Clear all MIB counters */
718 static void mvneta_mib_counters_clear(struct mvneta_port *pp)
723 /* Perform dummy reads from MIB counters */
724 for (i = 0; i < MVNETA_MIB_LATE_COLLISION; i += 4)
725 dummy = mvreg_read(pp, (MVNETA_MIB_COUNTERS_BASE + i));
726 dummy = mvreg_read(pp, MVNETA_RX_DISCARD_FRAME_COUNT);
727 dummy = mvreg_read(pp, MVNETA_OVERRUN_FRAME_COUNT);
730 /* Get System Network Statistics */
732 mvneta_get_stats64(struct net_device *dev,
733 struct rtnl_link_stats64 *stats)
735 struct mvneta_port *pp = netdev_priv(dev);
739 for_each_possible_cpu(cpu) {
740 struct mvneta_pcpu_stats *cpu_stats;
748 cpu_stats = per_cpu_ptr(pp->stats, cpu);
750 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
751 rx_packets = cpu_stats->rx_packets;
752 rx_bytes = cpu_stats->rx_bytes;
753 rx_dropped = cpu_stats->rx_dropped;
754 rx_errors = cpu_stats->rx_errors;
755 tx_packets = cpu_stats->tx_packets;
756 tx_bytes = cpu_stats->tx_bytes;
757 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
759 stats->rx_packets += rx_packets;
760 stats->rx_bytes += rx_bytes;
761 stats->rx_dropped += rx_dropped;
762 stats->rx_errors += rx_errors;
763 stats->tx_packets += tx_packets;
764 stats->tx_bytes += tx_bytes;
767 stats->tx_dropped = dev->stats.tx_dropped;
770 /* Rx descriptors helper methods */
772 /* Checks whether the RX descriptor having this status is both the first
773 * and the last descriptor for the RX packet. Each RX packet is currently
774 * received through a single RX descriptor, so not having each RX
775 * descriptor with its first and last bits set is an error
777 static int mvneta_rxq_desc_is_first_last(u32 status)
779 return (status & MVNETA_RXD_FIRST_LAST_DESC) ==
780 MVNETA_RXD_FIRST_LAST_DESC;
783 /* Add number of descriptors ready to receive new packets */
784 static void mvneta_rxq_non_occup_desc_add(struct mvneta_port *pp,
785 struct mvneta_rx_queue *rxq,
788 /* Only MVNETA_RXQ_ADD_NON_OCCUPIED_MAX (255) descriptors can
791 while (ndescs > MVNETA_RXQ_ADD_NON_OCCUPIED_MAX) {
792 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
793 (MVNETA_RXQ_ADD_NON_OCCUPIED_MAX <<
794 MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
795 ndescs -= MVNETA_RXQ_ADD_NON_OCCUPIED_MAX;
798 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
799 (ndescs << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
802 /* Get number of RX descriptors occupied by received packets */
803 static int mvneta_rxq_busy_desc_num_get(struct mvneta_port *pp,
804 struct mvneta_rx_queue *rxq)
808 val = mvreg_read(pp, MVNETA_RXQ_STATUS_REG(rxq->id));
809 return val & MVNETA_RXQ_OCCUPIED_ALL_MASK;
812 /* Update num of rx desc called upon return from rx path or
813 * from mvneta_rxq_drop_pkts().
815 static void mvneta_rxq_desc_num_update(struct mvneta_port *pp,
816 struct mvneta_rx_queue *rxq,
817 int rx_done, int rx_filled)
821 if ((rx_done <= 0xff) && (rx_filled <= 0xff)) {
823 (rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT);
824 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
828 /* Only 255 descriptors can be added at once */
829 while ((rx_done > 0) || (rx_filled > 0)) {
830 if (rx_done <= 0xff) {
837 if (rx_filled <= 0xff) {
838 val |= rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
841 val |= 0xff << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
844 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
848 /* Get pointer to next RX descriptor to be processed by SW */
849 static struct mvneta_rx_desc *
850 mvneta_rxq_next_desc_get(struct mvneta_rx_queue *rxq)
852 int rx_desc = rxq->next_desc_to_proc;
854 rxq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(rxq, rx_desc);
855 prefetch(rxq->descs + rxq->next_desc_to_proc);
856 return rxq->descs + rx_desc;
859 /* Change maximum receive size of the port. */
860 static void mvneta_max_rx_size_set(struct mvneta_port *pp, int max_rx_size)
864 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
865 val &= ~MVNETA_GMAC_MAX_RX_SIZE_MASK;
866 val |= ((max_rx_size - MVNETA_MH_SIZE) / 2) <<
867 MVNETA_GMAC_MAX_RX_SIZE_SHIFT;
868 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
872 /* Set rx queue offset */
873 static void mvneta_rxq_offset_set(struct mvneta_port *pp,
874 struct mvneta_rx_queue *rxq,
879 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
880 val &= ~MVNETA_RXQ_PKT_OFFSET_ALL_MASK;
883 val |= MVNETA_RXQ_PKT_OFFSET_MASK(offset >> 3);
884 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
888 /* Tx descriptors helper methods */
890 /* Update HW with number of TX descriptors to be sent */
891 static void mvneta_txq_pend_desc_add(struct mvneta_port *pp,
892 struct mvneta_tx_queue *txq,
897 pend_desc += txq->pending;
899 /* Only 255 Tx descriptors can be added at once */
901 val = min(pend_desc, 255);
902 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
904 } while (pend_desc > 0);
908 /* Get pointer to next TX descriptor to be processed (send) by HW */
909 static struct mvneta_tx_desc *
910 mvneta_txq_next_desc_get(struct mvneta_tx_queue *txq)
912 int tx_desc = txq->next_desc_to_proc;
914 txq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(txq, tx_desc);
915 return txq->descs + tx_desc;
918 /* Release the last allocated TX descriptor. Useful to handle DMA
919 * mapping failures in the TX path.
921 static void mvneta_txq_desc_put(struct mvneta_tx_queue *txq)
923 if (txq->next_desc_to_proc == 0)
924 txq->next_desc_to_proc = txq->last_desc - 1;
926 txq->next_desc_to_proc--;
929 /* Set rxq buf size */
930 static void mvneta_rxq_buf_size_set(struct mvneta_port *pp,
931 struct mvneta_rx_queue *rxq,
936 val = mvreg_read(pp, MVNETA_RXQ_SIZE_REG(rxq->id));
938 val &= ~MVNETA_RXQ_BUF_SIZE_MASK;
939 val |= ((buf_size >> 3) << MVNETA_RXQ_BUF_SIZE_SHIFT);
941 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), val);
944 /* Disable buffer management (BM) */
945 static void mvneta_rxq_bm_disable(struct mvneta_port *pp,
946 struct mvneta_rx_queue *rxq)
950 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
951 val &= ~MVNETA_RXQ_HW_BUF_ALLOC;
952 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
955 /* Enable buffer management (BM) */
956 static void mvneta_rxq_bm_enable(struct mvneta_port *pp,
957 struct mvneta_rx_queue *rxq)
961 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
962 val |= MVNETA_RXQ_HW_BUF_ALLOC;
963 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
966 /* Notify HW about port's assignment of pool for bigger packets */
967 static void mvneta_rxq_long_pool_set(struct mvneta_port *pp,
968 struct mvneta_rx_queue *rxq)
972 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
973 val &= ~MVNETA_RXQ_LONG_POOL_ID_MASK;
974 val |= (pp->pool_long->id << MVNETA_RXQ_LONG_POOL_ID_SHIFT);
976 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
979 /* Notify HW about port's assignment of pool for smaller packets */
980 static void mvneta_rxq_short_pool_set(struct mvneta_port *pp,
981 struct mvneta_rx_queue *rxq)
985 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
986 val &= ~MVNETA_RXQ_SHORT_POOL_ID_MASK;
987 val |= (pp->pool_short->id << MVNETA_RXQ_SHORT_POOL_ID_SHIFT);
989 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
992 /* Set port's receive buffer size for assigned BM pool */
993 static inline void mvneta_bm_pool_bufsize_set(struct mvneta_port *pp,
999 if (!IS_ALIGNED(buf_size, 8)) {
1000 dev_warn(pp->dev->dev.parent,
1001 "illegal buf_size value %d, round to %d\n",
1002 buf_size, ALIGN(buf_size, 8));
1003 buf_size = ALIGN(buf_size, 8);
1006 val = mvreg_read(pp, MVNETA_PORT_POOL_BUFFER_SZ_REG(pool_id));
1007 val |= buf_size & MVNETA_PORT_POOL_BUFFER_SZ_MASK;
1008 mvreg_write(pp, MVNETA_PORT_POOL_BUFFER_SZ_REG(pool_id), val);
1011 /* Configure MBUS window in order to enable access BM internal SRAM */
1012 static int mvneta_mbus_io_win_set(struct mvneta_port *pp, u32 base, u32 wsize,
1015 u32 win_enable, win_protect;
1018 win_enable = mvreg_read(pp, MVNETA_BASE_ADDR_ENABLE);
1020 if (pp->bm_win_id < 0) {
1021 /* Find first not occupied window */
1022 for (i = 0; i < MVNETA_MAX_DECODE_WIN; i++) {
1023 if (win_enable & (1 << i)) {
1028 if (i == MVNETA_MAX_DECODE_WIN)
1034 mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
1035 mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
1038 mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
1040 mvreg_write(pp, MVNETA_WIN_BASE(i), (base & 0xffff0000) |
1041 (attr << 8) | target);
1043 mvreg_write(pp, MVNETA_WIN_SIZE(i), (wsize - 1) & 0xffff0000);
1045 win_protect = mvreg_read(pp, MVNETA_ACCESS_PROTECT_ENABLE);
1046 win_protect |= 3 << (2 * i);
1047 mvreg_write(pp, MVNETA_ACCESS_PROTECT_ENABLE, win_protect);
1049 win_enable &= ~(1 << i);
1050 mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
1055 static int mvneta_bm_port_mbus_init(struct mvneta_port *pp)
1061 /* Get BM window information */
1062 err = mvebu_mbus_get_io_win_info(pp->bm_priv->bppi_phys_addr, &wsize,
1069 /* Open NETA -> BM window */
1070 err = mvneta_mbus_io_win_set(pp, pp->bm_priv->bppi_phys_addr, wsize,
1073 netdev_info(pp->dev, "fail to configure mbus window to BM\n");
1079 /* Assign and initialize pools for port. In case of fail
1080 * buffer manager will remain disabled for current port.
1082 static int mvneta_bm_port_init(struct platform_device *pdev,
1083 struct mvneta_port *pp)
1085 struct device_node *dn = pdev->dev.of_node;
1086 u32 long_pool_id, short_pool_id;
1088 if (!pp->neta_armada3700) {
1091 ret = mvneta_bm_port_mbus_init(pp);
1096 if (of_property_read_u32(dn, "bm,pool-long", &long_pool_id)) {
1097 netdev_info(pp->dev, "missing long pool id\n");
1101 /* Create port's long pool depending on mtu */
1102 pp->pool_long = mvneta_bm_pool_use(pp->bm_priv, long_pool_id,
1103 MVNETA_BM_LONG, pp->id,
1104 MVNETA_RX_PKT_SIZE(pp->dev->mtu));
1105 if (!pp->pool_long) {
1106 netdev_info(pp->dev, "fail to obtain long pool for port\n");
1110 pp->pool_long->port_map |= 1 << pp->id;
1112 mvneta_bm_pool_bufsize_set(pp, pp->pool_long->buf_size,
1115 /* If short pool id is not defined, assume using single pool */
1116 if (of_property_read_u32(dn, "bm,pool-short", &short_pool_id))
1117 short_pool_id = long_pool_id;
1119 /* Create port's short pool */
1120 pp->pool_short = mvneta_bm_pool_use(pp->bm_priv, short_pool_id,
1121 MVNETA_BM_SHORT, pp->id,
1122 MVNETA_BM_SHORT_PKT_SIZE);
1123 if (!pp->pool_short) {
1124 netdev_info(pp->dev, "fail to obtain short pool for port\n");
1125 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
1129 if (short_pool_id != long_pool_id) {
1130 pp->pool_short->port_map |= 1 << pp->id;
1131 mvneta_bm_pool_bufsize_set(pp, pp->pool_short->buf_size,
1132 pp->pool_short->id);
1138 /* Update settings of a pool for bigger packets */
1139 static void mvneta_bm_update_mtu(struct mvneta_port *pp, int mtu)
1141 struct mvneta_bm_pool *bm_pool = pp->pool_long;
1142 struct hwbm_pool *hwbm_pool = &bm_pool->hwbm_pool;
1145 /* Release all buffers from long pool */
1146 mvneta_bm_bufs_free(pp->bm_priv, bm_pool, 1 << pp->id);
1147 if (hwbm_pool->buf_num) {
1148 WARN(1, "cannot free all buffers in pool %d\n",
1153 bm_pool->pkt_size = MVNETA_RX_PKT_SIZE(mtu);
1154 bm_pool->buf_size = MVNETA_RX_BUF_SIZE(bm_pool->pkt_size);
1155 hwbm_pool->frag_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
1156 SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(bm_pool->pkt_size));
1158 /* Fill entire long pool */
1159 num = hwbm_pool_add(hwbm_pool, hwbm_pool->size);
1160 if (num != hwbm_pool->size) {
1161 WARN(1, "pool %d: %d of %d allocated\n",
1162 bm_pool->id, num, hwbm_pool->size);
1165 mvneta_bm_pool_bufsize_set(pp, bm_pool->buf_size, bm_pool->id);
1170 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
1171 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short, 1 << pp->id);
1174 pp->rx_offset_correction = MVNETA_SKB_HEADROOM;
1175 mvreg_write(pp, MVNETA_ACC_MODE, MVNETA_ACC_MODE_EXT1);
1176 netdev_info(pp->dev, "fail to update MTU, fall back to software BM\n");
1179 /* Start the Ethernet port RX and TX activity */
1180 static void mvneta_port_up(struct mvneta_port *pp)
1185 /* Enable all initialized TXs. */
1187 for (queue = 0; queue < txq_number; queue++) {
1188 struct mvneta_tx_queue *txq = &pp->txqs[queue];
1190 q_map |= (1 << queue);
1192 mvreg_write(pp, MVNETA_TXQ_CMD, q_map);
1195 /* Enable all initialized RXQs. */
1196 for (queue = 0; queue < rxq_number; queue++) {
1197 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
1200 q_map |= (1 << queue);
1202 mvreg_write(pp, MVNETA_RXQ_CMD, q_map);
1205 /* Stop the Ethernet port activity */
1206 static void mvneta_port_down(struct mvneta_port *pp)
1211 /* Stop Rx port activity. Check port Rx activity. */
1212 val = mvreg_read(pp, MVNETA_RXQ_CMD) & MVNETA_RXQ_ENABLE_MASK;
1214 /* Issue stop command for active channels only */
1216 mvreg_write(pp, MVNETA_RXQ_CMD,
1217 val << MVNETA_RXQ_DISABLE_SHIFT);
1219 /* Wait for all Rx activity to terminate. */
1222 if (count++ >= MVNETA_RX_DISABLE_TIMEOUT_MSEC) {
1223 netdev_warn(pp->dev,
1224 "TIMEOUT for RX stopped ! rx_queue_cmd: 0x%08x\n",
1230 val = mvreg_read(pp, MVNETA_RXQ_CMD);
1231 } while (val & MVNETA_RXQ_ENABLE_MASK);
1233 /* Stop Tx port activity. Check port Tx activity. Issue stop
1234 * command for active channels only
1236 val = (mvreg_read(pp, MVNETA_TXQ_CMD)) & MVNETA_TXQ_ENABLE_MASK;
1239 mvreg_write(pp, MVNETA_TXQ_CMD,
1240 (val << MVNETA_TXQ_DISABLE_SHIFT));
1242 /* Wait for all Tx activity to terminate. */
1245 if (count++ >= MVNETA_TX_DISABLE_TIMEOUT_MSEC) {
1246 netdev_warn(pp->dev,
1247 "TIMEOUT for TX stopped status=0x%08x\n",
1253 /* Check TX Command reg that all Txqs are stopped */
1254 val = mvreg_read(pp, MVNETA_TXQ_CMD);
1256 } while (val & MVNETA_TXQ_ENABLE_MASK);
1258 /* Double check to verify that TX FIFO is empty */
1261 if (count++ >= MVNETA_TX_FIFO_EMPTY_TIMEOUT) {
1262 netdev_warn(pp->dev,
1263 "TX FIFO empty timeout status=0x%08x\n",
1269 val = mvreg_read(pp, MVNETA_PORT_STATUS);
1270 } while (!(val & MVNETA_TX_FIFO_EMPTY) &&
1271 (val & MVNETA_TX_IN_PRGRS));
1276 /* Enable the port by setting the port enable bit of the MAC control register */
1277 static void mvneta_port_enable(struct mvneta_port *pp)
1282 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
1283 val |= MVNETA_GMAC0_PORT_ENABLE;
1284 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
1287 /* Disable the port and wait for about 200 usec before retuning */
1288 static void mvneta_port_disable(struct mvneta_port *pp)
1292 /* Reset the Enable bit in the Serial Control Register */
1293 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
1294 val &= ~MVNETA_GMAC0_PORT_ENABLE;
1295 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
1300 /* Multicast tables methods */
1302 /* Set all entries in Unicast MAC Table; queue==-1 means reject all */
1303 static void mvneta_set_ucast_table(struct mvneta_port *pp, int queue)
1311 val = 0x1 | (queue << 1);
1312 val |= (val << 24) | (val << 16) | (val << 8);
1315 for (offset = 0; offset <= 0xc; offset += 4)
1316 mvreg_write(pp, MVNETA_DA_FILT_UCAST_BASE + offset, val);
1319 /* Set all entries in Special Multicast MAC Table; queue==-1 means reject all */
1320 static void mvneta_set_special_mcast_table(struct mvneta_port *pp, int queue)
1328 val = 0x1 | (queue << 1);
1329 val |= (val << 24) | (val << 16) | (val << 8);
1332 for (offset = 0; offset <= 0xfc; offset += 4)
1333 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + offset, val);
1337 /* Set all entries in Other Multicast MAC Table. queue==-1 means reject all */
1338 static void mvneta_set_other_mcast_table(struct mvneta_port *pp, int queue)
1344 memset(pp->mcast_count, 0, sizeof(pp->mcast_count));
1347 memset(pp->mcast_count, 1, sizeof(pp->mcast_count));
1348 val = 0x1 | (queue << 1);
1349 val |= (val << 24) | (val << 16) | (val << 8);
1352 for (offset = 0; offset <= 0xfc; offset += 4)
1353 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + offset, val);
1356 static void mvneta_percpu_unmask_interrupt(void *arg)
1358 struct mvneta_port *pp = arg;
1360 /* All the queue are unmasked, but actually only the ones
1361 * mapped to this CPU will be unmasked
1363 mvreg_write(pp, MVNETA_INTR_NEW_MASK,
1364 MVNETA_RX_INTR_MASK_ALL |
1365 MVNETA_TX_INTR_MASK_ALL |
1366 MVNETA_MISCINTR_INTR_MASK);
1369 static void mvneta_percpu_mask_interrupt(void *arg)
1371 struct mvneta_port *pp = arg;
1373 /* All the queue are masked, but actually only the ones
1374 * mapped to this CPU will be masked
1376 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
1377 mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0);
1378 mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0);
1381 static void mvneta_percpu_clear_intr_cause(void *arg)
1383 struct mvneta_port *pp = arg;
1385 /* All the queue are cleared, but actually only the ones
1386 * mapped to this CPU will be cleared
1388 mvreg_write(pp, MVNETA_INTR_NEW_CAUSE, 0);
1389 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
1390 mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0);
1393 /* This method sets defaults to the NETA port:
1394 * Clears interrupt Cause and Mask registers.
1395 * Clears all MAC tables.
1396 * Sets defaults to all registers.
1397 * Resets RX and TX descriptor rings.
1399 * This method can be called after mvneta_port_down() to return the port
1400 * settings to defaults.
1402 static void mvneta_defaults_set(struct mvneta_port *pp)
1407 int max_cpu = num_present_cpus();
1409 /* Clear all Cause registers */
1410 on_each_cpu(mvneta_percpu_clear_intr_cause, pp, true);
1412 /* Mask all interrupts */
1413 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
1414 mvreg_write(pp, MVNETA_INTR_ENABLE, 0);
1416 /* Enable MBUS Retry bit16 */
1417 mvreg_write(pp, MVNETA_MBUS_RETRY, 0x20);
1419 /* Set CPU queue access map. CPUs are assigned to the RX and
1420 * TX queues modulo their number. If there is only one TX
1421 * queue then it is assigned to the CPU associated to the
1424 for_each_present_cpu(cpu) {
1425 int rxq_map = 0, txq_map = 0;
1427 if (!pp->neta_armada3700) {
1428 for (rxq = 0; rxq < rxq_number; rxq++)
1429 if ((rxq % max_cpu) == cpu)
1430 rxq_map |= MVNETA_CPU_RXQ_ACCESS(rxq);
1432 for (txq = 0; txq < txq_number; txq++)
1433 if ((txq % max_cpu) == cpu)
1434 txq_map |= MVNETA_CPU_TXQ_ACCESS(txq);
1436 /* With only one TX queue we configure a special case
1437 * which will allow to get all the irq on a single
1440 if (txq_number == 1)
1441 txq_map = (cpu == pp->rxq_def) ?
1442 MVNETA_CPU_TXQ_ACCESS(1) : 0;
1445 txq_map = MVNETA_CPU_TXQ_ACCESS_ALL_MASK;
1446 rxq_map = MVNETA_CPU_RXQ_ACCESS_ALL_MASK;
1449 mvreg_write(pp, MVNETA_CPU_MAP(cpu), rxq_map | txq_map);
1452 /* Reset RX and TX DMAs */
1453 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
1454 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
1456 /* Disable Legacy WRR, Disable EJP, Release from reset */
1457 mvreg_write(pp, MVNETA_TXQ_CMD_1, 0);
1458 for (queue = 0; queue < txq_number; queue++) {
1459 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(queue), 0);
1460 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(queue), 0);
1463 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
1464 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
1466 /* Set Port Acceleration Mode */
1468 /* HW buffer management + legacy parser */
1469 val = MVNETA_ACC_MODE_EXT2;
1471 /* SW buffer management + legacy parser */
1472 val = MVNETA_ACC_MODE_EXT1;
1473 mvreg_write(pp, MVNETA_ACC_MODE, val);
1476 mvreg_write(pp, MVNETA_BM_ADDRESS, pp->bm_priv->bppi_phys_addr);
1478 /* Update val of portCfg register accordingly with all RxQueue types */
1479 val = MVNETA_PORT_CONFIG_DEFL_VALUE(pp->rxq_def);
1480 mvreg_write(pp, MVNETA_PORT_CONFIG, val);
1483 mvreg_write(pp, MVNETA_PORT_CONFIG_EXTEND, val);
1484 mvreg_write(pp, MVNETA_RX_MIN_FRAME_SIZE, 64);
1486 /* Build PORT_SDMA_CONFIG_REG */
1489 /* Default burst size */
1490 val |= MVNETA_TX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
1491 val |= MVNETA_RX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
1492 val |= MVNETA_RX_NO_DATA_SWAP | MVNETA_TX_NO_DATA_SWAP;
1494 #if defined(__BIG_ENDIAN)
1495 val |= MVNETA_DESC_SWAP;
1498 /* Assign port SDMA configuration */
1499 mvreg_write(pp, MVNETA_SDMA_CONFIG, val);
1501 /* Disable PHY polling in hardware, since we're using the
1502 * kernel phylib to do this.
1504 val = mvreg_read(pp, MVNETA_UNIT_CONTROL);
1505 val &= ~MVNETA_PHY_POLLING_ENABLE;
1506 mvreg_write(pp, MVNETA_UNIT_CONTROL, val);
1508 mvneta_set_ucast_table(pp, -1);
1509 mvneta_set_special_mcast_table(pp, -1);
1510 mvneta_set_other_mcast_table(pp, -1);
1512 /* Set port interrupt enable register - default enable all */
1513 mvreg_write(pp, MVNETA_INTR_ENABLE,
1514 (MVNETA_RXQ_INTR_ENABLE_ALL_MASK
1515 | MVNETA_TXQ_INTR_ENABLE_ALL_MASK));
1517 mvneta_mib_counters_clear(pp);
1520 /* Set max sizes for tx queues */
1521 static void mvneta_txq_max_tx_size_set(struct mvneta_port *pp, int max_tx_size)
1527 mtu = max_tx_size * 8;
1528 if (mtu > MVNETA_TX_MTU_MAX)
1529 mtu = MVNETA_TX_MTU_MAX;
1532 val = mvreg_read(pp, MVNETA_TX_MTU);
1533 val &= ~MVNETA_TX_MTU_MAX;
1535 mvreg_write(pp, MVNETA_TX_MTU, val);
1537 /* TX token size and all TXQs token size must be larger that MTU */
1538 val = mvreg_read(pp, MVNETA_TX_TOKEN_SIZE);
1540 size = val & MVNETA_TX_TOKEN_SIZE_MAX;
1543 val &= ~MVNETA_TX_TOKEN_SIZE_MAX;
1545 mvreg_write(pp, MVNETA_TX_TOKEN_SIZE, val);
1547 for (queue = 0; queue < txq_number; queue++) {
1548 val = mvreg_read(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue));
1550 size = val & MVNETA_TXQ_TOKEN_SIZE_MAX;
1553 val &= ~MVNETA_TXQ_TOKEN_SIZE_MAX;
1555 mvreg_write(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue), val);
1560 /* Set unicast address */
1561 static void mvneta_set_ucast_addr(struct mvneta_port *pp, u8 last_nibble,
1564 unsigned int unicast_reg;
1565 unsigned int tbl_offset;
1566 unsigned int reg_offset;
1568 /* Locate the Unicast table entry */
1569 last_nibble = (0xf & last_nibble);
1571 /* offset from unicast tbl base */
1572 tbl_offset = (last_nibble / 4) * 4;
1574 /* offset within the above reg */
1575 reg_offset = last_nibble % 4;
1577 unicast_reg = mvreg_read(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset));
1580 /* Clear accepts frame bit at specified unicast DA tbl entry */
1581 unicast_reg &= ~(0xff << (8 * reg_offset));
1583 unicast_reg &= ~(0xff << (8 * reg_offset));
1584 unicast_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
1587 mvreg_write(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset), unicast_reg);
1590 /* Set mac address */
1591 static void mvneta_mac_addr_set(struct mvneta_port *pp, unsigned char *addr,
1598 mac_l = (addr[4] << 8) | (addr[5]);
1599 mac_h = (addr[0] << 24) | (addr[1] << 16) |
1600 (addr[2] << 8) | (addr[3] << 0);
1602 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, mac_l);
1603 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, mac_h);
1606 /* Accept frames of this address */
1607 mvneta_set_ucast_addr(pp, addr[5], queue);
1610 /* Set the number of packets that will be received before RX interrupt
1611 * will be generated by HW.
1613 static void mvneta_rx_pkts_coal_set(struct mvneta_port *pp,
1614 struct mvneta_rx_queue *rxq, u32 value)
1616 mvreg_write(pp, MVNETA_RXQ_THRESHOLD_REG(rxq->id),
1617 value | MVNETA_RXQ_NON_OCCUPIED(0));
1620 /* Set the time delay in usec before RX interrupt will be generated by
1623 static void mvneta_rx_time_coal_set(struct mvneta_port *pp,
1624 struct mvneta_rx_queue *rxq, u32 value)
1627 unsigned long clk_rate;
1629 clk_rate = clk_get_rate(pp->clk);
1630 val = (clk_rate / 1000000) * value;
1632 mvreg_write(pp, MVNETA_RXQ_TIME_COAL_REG(rxq->id), val);
1635 /* Set threshold for TX_DONE pkts coalescing */
1636 static void mvneta_tx_done_pkts_coal_set(struct mvneta_port *pp,
1637 struct mvneta_tx_queue *txq, u32 value)
1641 val = mvreg_read(pp, MVNETA_TXQ_SIZE_REG(txq->id));
1643 val &= ~MVNETA_TXQ_SENT_THRESH_ALL_MASK;
1644 val |= MVNETA_TXQ_SENT_THRESH_MASK(value);
1646 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), val);
1649 /* Handle rx descriptor fill by setting buf_cookie and buf_phys_addr */
1650 static void mvneta_rx_desc_fill(struct mvneta_rx_desc *rx_desc,
1651 u32 phys_addr, void *virt_addr,
1652 struct mvneta_rx_queue *rxq)
1656 rx_desc->buf_phys_addr = phys_addr;
1657 i = rx_desc - rxq->descs;
1658 rxq->buf_virt_addr[i] = virt_addr;
1661 /* Decrement sent descriptors counter */
1662 static void mvneta_txq_sent_desc_dec(struct mvneta_port *pp,
1663 struct mvneta_tx_queue *txq,
1668 /* Only 255 TX descriptors can be updated at once */
1669 while (sent_desc > 0xff) {
1670 val = 0xff << MVNETA_TXQ_DEC_SENT_SHIFT;
1671 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1672 sent_desc = sent_desc - 0xff;
1675 val = sent_desc << MVNETA_TXQ_DEC_SENT_SHIFT;
1676 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1679 /* Get number of TX descriptors already sent by HW */
1680 static int mvneta_txq_sent_desc_num_get(struct mvneta_port *pp,
1681 struct mvneta_tx_queue *txq)
1686 val = mvreg_read(pp, MVNETA_TXQ_STATUS_REG(txq->id));
1687 sent_desc = (val & MVNETA_TXQ_SENT_DESC_MASK) >>
1688 MVNETA_TXQ_SENT_DESC_SHIFT;
1693 /* Get number of sent descriptors and decrement counter.
1694 * The number of sent descriptors is returned.
1696 static int mvneta_txq_sent_desc_proc(struct mvneta_port *pp,
1697 struct mvneta_tx_queue *txq)
1701 /* Get number of sent descriptors */
1702 sent_desc = mvneta_txq_sent_desc_num_get(pp, txq);
1704 /* Decrement sent descriptors counter */
1706 mvneta_txq_sent_desc_dec(pp, txq, sent_desc);
1711 /* Set TXQ descriptors fields relevant for CSUM calculation */
1712 static u32 mvneta_txq_desc_csum(int l3_offs, int l3_proto,
1713 int ip_hdr_len, int l4_proto)
1717 /* Fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
1718 * G_L4_chk, L4_type; required only for checksum
1721 command = l3_offs << MVNETA_TX_L3_OFF_SHIFT;
1722 command |= ip_hdr_len << MVNETA_TX_IP_HLEN_SHIFT;
1724 if (l3_proto == htons(ETH_P_IP))
1725 command |= MVNETA_TXD_IP_CSUM;
1727 command |= MVNETA_TX_L3_IP6;
1729 if (l4_proto == IPPROTO_TCP)
1730 command |= MVNETA_TX_L4_CSUM_FULL;
1731 else if (l4_proto == IPPROTO_UDP)
1732 command |= MVNETA_TX_L4_UDP | MVNETA_TX_L4_CSUM_FULL;
1734 command |= MVNETA_TX_L4_CSUM_NOT;
1740 /* Display more error info */
1741 static void mvneta_rx_error(struct mvneta_port *pp,
1742 struct mvneta_rx_desc *rx_desc)
1744 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
1745 u32 status = rx_desc->status;
1747 /* update per-cpu counter */
1748 u64_stats_update_begin(&stats->syncp);
1750 u64_stats_update_end(&stats->syncp);
1752 switch (status & MVNETA_RXD_ERR_CODE_MASK) {
1753 case MVNETA_RXD_ERR_CRC:
1754 netdev_err(pp->dev, "bad rx status %08x (crc error), size=%d\n",
1755 status, rx_desc->data_size);
1757 case MVNETA_RXD_ERR_OVERRUN:
1758 netdev_err(pp->dev, "bad rx status %08x (overrun error), size=%d\n",
1759 status, rx_desc->data_size);
1761 case MVNETA_RXD_ERR_LEN:
1762 netdev_err(pp->dev, "bad rx status %08x (max frame length error), size=%d\n",
1763 status, rx_desc->data_size);
1765 case MVNETA_RXD_ERR_RESOURCE:
1766 netdev_err(pp->dev, "bad rx status %08x (resource error), size=%d\n",
1767 status, rx_desc->data_size);
1772 /* Handle RX checksum offload based on the descriptor's status */
1773 static void mvneta_rx_csum(struct mvneta_port *pp, u32 status,
1774 struct sk_buff *skb)
1776 if ((pp->dev->features & NETIF_F_RXCSUM) &&
1777 (status & MVNETA_RXD_L3_IP4) &&
1778 (status & MVNETA_RXD_L4_CSUM_OK)) {
1780 skb->ip_summed = CHECKSUM_UNNECESSARY;
1784 skb->ip_summed = CHECKSUM_NONE;
1787 /* Return tx queue pointer (find last set bit) according to <cause> returned
1788 * form tx_done reg. <cause> must not be null. The return value is always a
1789 * valid queue for matching the first one found in <cause>.
1791 static struct mvneta_tx_queue *mvneta_tx_done_policy(struct mvneta_port *pp,
1794 int queue = fls(cause) - 1;
1796 return &pp->txqs[queue];
1799 /* Free tx queue skbuffs */
1800 static void mvneta_txq_bufs_free(struct mvneta_port *pp,
1801 struct mvneta_tx_queue *txq, int num,
1802 struct netdev_queue *nq)
1804 unsigned int bytes_compl = 0, pkts_compl = 0;
1807 for (i = 0; i < num; i++) {
1808 struct mvneta_tx_buf *buf = &txq->buf[txq->txq_get_index];
1809 struct mvneta_tx_desc *tx_desc = txq->descs +
1812 mvneta_txq_inc_get(txq);
1814 if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr) &&
1815 buf->type != MVNETA_TYPE_XDP_TX)
1816 dma_unmap_single(pp->dev->dev.parent,
1817 tx_desc->buf_phys_addr,
1818 tx_desc->data_size, DMA_TO_DEVICE);
1819 if (buf->type == MVNETA_TYPE_SKB && buf->skb) {
1820 bytes_compl += buf->skb->len;
1822 dev_kfree_skb_any(buf->skb);
1823 } else if (buf->type == MVNETA_TYPE_XDP_TX ||
1824 buf->type == MVNETA_TYPE_XDP_NDO) {
1825 xdp_return_frame(buf->xdpf);
1829 netdev_tx_completed_queue(nq, pkts_compl, bytes_compl);
1832 /* Handle end of transmission */
1833 static void mvneta_txq_done(struct mvneta_port *pp,
1834 struct mvneta_tx_queue *txq)
1836 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
1839 tx_done = mvneta_txq_sent_desc_proc(pp, txq);
1843 mvneta_txq_bufs_free(pp, txq, tx_done, nq);
1845 txq->count -= tx_done;
1847 if (netif_tx_queue_stopped(nq)) {
1848 if (txq->count <= txq->tx_wake_threshold)
1849 netif_tx_wake_queue(nq);
1853 /* Refill processing for SW buffer management */
1854 /* Allocate page per descriptor */
1855 static int mvneta_rx_refill(struct mvneta_port *pp,
1856 struct mvneta_rx_desc *rx_desc,
1857 struct mvneta_rx_queue *rxq,
1860 dma_addr_t phys_addr;
1863 page = page_pool_alloc_pages(rxq->page_pool,
1864 gfp_mask | __GFP_NOWARN);
1868 phys_addr = page_pool_get_dma_addr(page) + pp->rx_offset_correction;
1869 mvneta_rx_desc_fill(rx_desc, phys_addr, page, rxq);
1874 /* Handle tx checksum */
1875 static u32 mvneta_skb_tx_csum(struct mvneta_port *pp, struct sk_buff *skb)
1877 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1879 __be16 l3_proto = vlan_get_protocol(skb);
1882 if (l3_proto == htons(ETH_P_IP)) {
1883 struct iphdr *ip4h = ip_hdr(skb);
1885 /* Calculate IPv4 checksum and L4 checksum */
1886 ip_hdr_len = ip4h->ihl;
1887 l4_proto = ip4h->protocol;
1888 } else if (l3_proto == htons(ETH_P_IPV6)) {
1889 struct ipv6hdr *ip6h = ipv6_hdr(skb);
1891 /* Read l4_protocol from one of IPv6 extra headers */
1892 if (skb_network_header_len(skb) > 0)
1893 ip_hdr_len = (skb_network_header_len(skb) >> 2);
1894 l4_proto = ip6h->nexthdr;
1896 return MVNETA_TX_L4_CSUM_NOT;
1898 return mvneta_txq_desc_csum(skb_network_offset(skb),
1899 l3_proto, ip_hdr_len, l4_proto);
1902 return MVNETA_TX_L4_CSUM_NOT;
1905 /* Drop packets received by the RXQ and free buffers */
1906 static void mvneta_rxq_drop_pkts(struct mvneta_port *pp,
1907 struct mvneta_rx_queue *rxq)
1911 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
1913 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
1916 for (i = 0; i < rx_done; i++) {
1917 struct mvneta_rx_desc *rx_desc =
1918 mvneta_rxq_next_desc_get(rxq);
1919 u8 pool_id = MVNETA_RX_GET_BM_POOL_ID(rx_desc);
1920 struct mvneta_bm_pool *bm_pool;
1922 bm_pool = &pp->bm_priv->bm_pools[pool_id];
1923 /* Return dropped buffer to the pool */
1924 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
1925 rx_desc->buf_phys_addr);
1930 for (i = 0; i < rxq->size; i++) {
1931 struct mvneta_rx_desc *rx_desc = rxq->descs + i;
1932 void *data = rxq->buf_virt_addr[i];
1933 if (!data || !(rx_desc->buf_phys_addr))
1936 page_pool_put_page(rxq->page_pool, data, false);
1938 if (xdp_rxq_info_is_reg(&rxq->xdp_rxq))
1939 xdp_rxq_info_unreg(&rxq->xdp_rxq);
1940 page_pool_destroy(rxq->page_pool);
1941 rxq->page_pool = NULL;
1945 mvneta_update_stats(struct mvneta_port *pp, u32 pkts,
1948 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
1950 u64_stats_update_begin(&stats->syncp);
1952 stats->tx_packets += pkts;
1953 stats->tx_bytes += len;
1955 stats->rx_packets += pkts;
1956 stats->rx_bytes += len;
1958 u64_stats_update_end(&stats->syncp);
1962 int mvneta_rx_refill_queue(struct mvneta_port *pp, struct mvneta_rx_queue *rxq)
1964 struct mvneta_rx_desc *rx_desc;
1965 int curr_desc = rxq->first_to_refill;
1968 for (i = 0; (i < rxq->refill_num) && (i < 64); i++) {
1969 rx_desc = rxq->descs + curr_desc;
1970 if (!(rx_desc->buf_phys_addr)) {
1971 if (mvneta_rx_refill(pp, rx_desc, rxq, GFP_ATOMIC)) {
1972 pr_err("Can't refill queue %d. Done %d from %d\n",
1973 rxq->id, i, rxq->refill_num);
1978 curr_desc = MVNETA_QUEUE_NEXT_DESC(rxq, curr_desc);
1980 rxq->refill_num -= i;
1981 rxq->first_to_refill = curr_desc;
1987 mvneta_xdp_submit_frame(struct mvneta_port *pp, struct mvneta_tx_queue *txq,
1988 struct xdp_frame *xdpf, bool dma_map)
1990 struct mvneta_tx_desc *tx_desc;
1991 struct mvneta_tx_buf *buf;
1992 dma_addr_t dma_addr;
1994 if (txq->count >= txq->tx_stop_threshold)
1995 return MVNETA_XDP_DROPPED;
1997 tx_desc = mvneta_txq_next_desc_get(txq);
1999 buf = &txq->buf[txq->txq_put_index];
2002 dma_addr = dma_map_single(pp->dev->dev.parent, xdpf->data,
2003 xdpf->len, DMA_TO_DEVICE);
2004 if (dma_mapping_error(pp->dev->dev.parent, dma_addr)) {
2005 mvneta_txq_desc_put(txq);
2006 return MVNETA_XDP_DROPPED;
2008 buf->type = MVNETA_TYPE_XDP_NDO;
2010 struct page *page = virt_to_page(xdpf->data);
2012 dma_addr = page_pool_get_dma_addr(page) +
2013 sizeof(*xdpf) + xdpf->headroom;
2014 dma_sync_single_for_device(pp->dev->dev.parent, dma_addr,
2015 xdpf->len, DMA_BIDIRECTIONAL);
2016 buf->type = MVNETA_TYPE_XDP_TX;
2020 tx_desc->command = MVNETA_TXD_FLZ_DESC;
2021 tx_desc->buf_phys_addr = dma_addr;
2022 tx_desc->data_size = xdpf->len;
2024 mvneta_update_stats(pp, 1, xdpf->len, true);
2025 mvneta_txq_inc_put(txq);
2029 return MVNETA_XDP_TX;
2033 mvneta_xdp_xmit_back(struct mvneta_port *pp, struct xdp_buff *xdp)
2035 struct mvneta_tx_queue *txq;
2036 struct netdev_queue *nq;
2037 struct xdp_frame *xdpf;
2041 xdpf = convert_to_xdp_frame(xdp);
2042 if (unlikely(!xdpf))
2043 return MVNETA_XDP_DROPPED;
2045 cpu = smp_processor_id();
2046 txq = &pp->txqs[cpu % txq_number];
2047 nq = netdev_get_tx_queue(pp->dev, txq->id);
2049 __netif_tx_lock(nq, cpu);
2050 ret = mvneta_xdp_submit_frame(pp, txq, xdpf, false);
2051 if (ret == MVNETA_XDP_TX)
2052 mvneta_txq_pend_desc_add(pp, txq, 0);
2053 __netif_tx_unlock(nq);
2059 mvneta_xdp_xmit(struct net_device *dev, int num_frame,
2060 struct xdp_frame **frames, u32 flags)
2062 struct mvneta_port *pp = netdev_priv(dev);
2063 int cpu = smp_processor_id();
2064 struct mvneta_tx_queue *txq;
2065 struct netdev_queue *nq;
2069 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
2072 txq = &pp->txqs[cpu % txq_number];
2073 nq = netdev_get_tx_queue(pp->dev, txq->id);
2075 __netif_tx_lock(nq, cpu);
2076 for (i = 0; i < num_frame; i++) {
2077 ret = mvneta_xdp_submit_frame(pp, txq, frames[i], true);
2078 if (ret != MVNETA_XDP_TX) {
2079 xdp_return_frame_rx_napi(frames[i]);
2084 if (unlikely(flags & XDP_XMIT_FLUSH))
2085 mvneta_txq_pend_desc_add(pp, txq, 0);
2086 __netif_tx_unlock(nq);
2088 return num_frame - drops;
2092 mvneta_run_xdp(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
2093 struct bpf_prog *prog, struct xdp_buff *xdp)
2098 len = xdp->data_end - xdp->data_hard_start - pp->rx_offset_correction;
2099 act = bpf_prog_run_xdp(prog, xdp);
2103 ret = MVNETA_XDP_PASS;
2105 case XDP_REDIRECT: {
2108 err = xdp_do_redirect(pp->dev, xdp, prog);
2110 ret = MVNETA_XDP_DROPPED;
2111 __page_pool_put_page(rxq->page_pool,
2112 virt_to_head_page(xdp->data),
2115 ret = MVNETA_XDP_REDIR;
2120 ret = mvneta_xdp_xmit_back(pp, xdp);
2121 if (ret != MVNETA_XDP_TX)
2122 __page_pool_put_page(rxq->page_pool,
2123 virt_to_head_page(xdp->data),
2127 bpf_warn_invalid_xdp_action(act);
2130 trace_xdp_exception(pp->dev, prog, act);
2133 __page_pool_put_page(rxq->page_pool,
2134 virt_to_head_page(xdp->data),
2136 ret = MVNETA_XDP_DROPPED;
2144 mvneta_swbm_rx_frame(struct mvneta_port *pp,
2145 struct mvneta_rx_desc *rx_desc,
2146 struct mvneta_rx_queue *rxq,
2147 struct xdp_buff *xdp,
2148 struct bpf_prog *xdp_prog,
2149 struct page *page, u32 *xdp_ret)
2151 unsigned char *data = page_address(page);
2152 int data_len = -MVNETA_MH_SIZE, len;
2153 struct net_device *dev = pp->dev;
2154 enum dma_data_direction dma_dir;
2156 if (MVNETA_SKB_SIZE(rx_desc->data_size) > PAGE_SIZE) {
2157 len = MVNETA_MAX_RX_BUF_SIZE;
2160 len = rx_desc->data_size;
2161 data_len += len - ETH_FCS_LEN;
2164 dma_dir = page_pool_get_dma_dir(rxq->page_pool);
2165 dma_sync_single_for_cpu(dev->dev.parent,
2166 rx_desc->buf_phys_addr,
2169 /* Prefetch header */
2172 xdp->data_hard_start = data;
2173 xdp->data = data + pp->rx_offset_correction + MVNETA_MH_SIZE;
2174 xdp->data_end = xdp->data + data_len;
2175 xdp_set_data_meta_invalid(xdp);
2180 ret = mvneta_run_xdp(pp, rxq, xdp_prog, xdp);
2181 if (ret != MVNETA_XDP_PASS) {
2182 mvneta_update_stats(pp, 1,
2183 xdp->data_end - xdp->data,
2185 rx_desc->buf_phys_addr = 0;
2191 rxq->skb = build_skb(xdp->data_hard_start, PAGE_SIZE);
2192 if (unlikely(!rxq->skb)) {
2193 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
2195 netdev_err(dev, "Can't allocate skb on queue %d\n", rxq->id);
2196 rxq->skb_alloc_err++;
2198 u64_stats_update_begin(&stats->syncp);
2199 stats->rx_dropped++;
2200 u64_stats_update_end(&stats->syncp);
2204 page_pool_release_page(rxq->page_pool, page);
2206 skb_reserve(rxq->skb,
2207 xdp->data - xdp->data_hard_start);
2208 skb_put(rxq->skb, xdp->data_end - xdp->data);
2209 mvneta_rx_csum(pp, rx_desc->status, rxq->skb);
2211 rxq->left_size = rx_desc->data_size - len;
2212 rx_desc->buf_phys_addr = 0;
2218 mvneta_swbm_add_rx_fragment(struct mvneta_port *pp,
2219 struct mvneta_rx_desc *rx_desc,
2220 struct mvneta_rx_queue *rxq,
2223 struct net_device *dev = pp->dev;
2224 enum dma_data_direction dma_dir;
2227 if (rxq->left_size > MVNETA_MAX_RX_BUF_SIZE) {
2228 len = MVNETA_MAX_RX_BUF_SIZE;
2231 len = rxq->left_size;
2232 data_len = len - ETH_FCS_LEN;
2234 dma_dir = page_pool_get_dma_dir(rxq->page_pool);
2235 dma_sync_single_for_cpu(dev->dev.parent,
2236 rx_desc->buf_phys_addr,
2239 /* refill descriptor with new buffer later */
2240 skb_add_rx_frag(rxq->skb,
2241 skb_shinfo(rxq->skb)->nr_frags,
2242 page, pp->rx_offset_correction, data_len,
2245 page_pool_release_page(rxq->page_pool, page);
2246 rx_desc->buf_phys_addr = 0;
2247 rxq->left_size -= len;
2250 /* Main rx processing when using software buffer management */
2251 static int mvneta_rx_swbm(struct napi_struct *napi,
2252 struct mvneta_port *pp, int budget,
2253 struct mvneta_rx_queue *rxq)
2255 int rcvd_pkts = 0, rcvd_bytes = 0, rx_proc = 0;
2256 struct net_device *dev = pp->dev;
2257 struct bpf_prog *xdp_prog;
2258 struct xdp_buff xdp_buf;
2259 int rx_todo, refill;
2262 /* Get number of received packets */
2263 rx_todo = mvneta_rxq_busy_desc_num_get(pp, rxq);
2266 xdp_prog = READ_ONCE(pp->xdp_prog);
2267 xdp_buf.rxq = &rxq->xdp_rxq;
2269 /* Fairness NAPI loop */
2270 while (rx_proc < budget && rx_proc < rx_todo) {
2271 struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
2272 u32 rx_status, index;
2275 index = rx_desc - rxq->descs;
2276 page = (struct page *)rxq->buf_virt_addr[index];
2278 rx_status = rx_desc->status;
2282 if (rx_status & MVNETA_RXD_FIRST_DESC) {
2285 /* Check errors only for FIRST descriptor */
2286 if (rx_status & MVNETA_RXD_ERR_SUMMARY) {
2287 mvneta_rx_error(pp, rx_desc);
2288 /* leave the descriptor untouched */
2292 err = mvneta_swbm_rx_frame(pp, rx_desc, rxq, &xdp_buf,
2293 xdp_prog, page, &xdp_ret);
2297 if (unlikely(!rxq->skb)) {
2298 pr_debug("no skb for rx_status 0x%x\n",
2302 mvneta_swbm_add_rx_fragment(pp, rx_desc, rxq, page);
2303 } /* Middle or Last descriptor */
2305 if (!(rx_status & MVNETA_RXD_LAST_DESC))
2306 /* no last descriptor this time */
2309 if (rxq->left_size) {
2310 pr_err("get last desc, but left_size (%d) != 0\n",
2312 dev_kfree_skb_any(rxq->skb);
2318 rcvd_bytes += rxq->skb->len;
2320 /* Linux processing */
2321 rxq->skb->protocol = eth_type_trans(rxq->skb, dev);
2323 napi_gro_receive(napi, rxq->skb);
2325 /* clean uncomplete skb pointer in queue */
2330 if (xdp_ret & MVNETA_XDP_REDIR)
2334 mvneta_update_stats(pp, rcvd_pkts, rcvd_bytes, false);
2336 /* return some buffers to hardware queue, one at a time is too slow */
2337 refill = mvneta_rx_refill_queue(pp, rxq);
2339 /* Update rxq management counters */
2340 mvneta_rxq_desc_num_update(pp, rxq, rx_proc, refill);
2345 /* Main rx processing when using hardware buffer management */
2346 static int mvneta_rx_hwbm(struct napi_struct *napi,
2347 struct mvneta_port *pp, int rx_todo,
2348 struct mvneta_rx_queue *rxq)
2350 struct net_device *dev = pp->dev;
2355 /* Get number of received packets */
2356 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
2358 if (rx_todo > rx_done)
2363 /* Fairness NAPI loop */
2364 while (rx_done < rx_todo) {
2365 struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
2366 struct mvneta_bm_pool *bm_pool = NULL;
2367 struct sk_buff *skb;
2368 unsigned char *data;
2369 dma_addr_t phys_addr;
2370 u32 rx_status, frag_size;
2375 rx_status = rx_desc->status;
2376 rx_bytes = rx_desc->data_size - (ETH_FCS_LEN + MVNETA_MH_SIZE);
2377 data = (u8 *)(uintptr_t)rx_desc->buf_cookie;
2378 phys_addr = rx_desc->buf_phys_addr;
2379 pool_id = MVNETA_RX_GET_BM_POOL_ID(rx_desc);
2380 bm_pool = &pp->bm_priv->bm_pools[pool_id];
2382 if (!mvneta_rxq_desc_is_first_last(rx_status) ||
2383 (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
2384 err_drop_frame_ret_pool:
2385 /* Return the buffer to the pool */
2386 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
2387 rx_desc->buf_phys_addr);
2389 mvneta_rx_error(pp, rx_desc);
2390 /* leave the descriptor untouched */
2394 if (rx_bytes <= rx_copybreak) {
2395 /* better copy a small frame and not unmap the DMA region */
2396 skb = netdev_alloc_skb_ip_align(dev, rx_bytes);
2398 goto err_drop_frame_ret_pool;
2400 dma_sync_single_range_for_cpu(&pp->bm_priv->pdev->dev,
2401 rx_desc->buf_phys_addr,
2402 MVNETA_MH_SIZE + NET_SKB_PAD,
2405 skb_put_data(skb, data + MVNETA_MH_SIZE + NET_SKB_PAD,
2408 skb->protocol = eth_type_trans(skb, dev);
2409 mvneta_rx_csum(pp, rx_status, skb);
2410 napi_gro_receive(napi, skb);
2413 rcvd_bytes += rx_bytes;
2415 /* Return the buffer to the pool */
2416 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
2417 rx_desc->buf_phys_addr);
2419 /* leave the descriptor and buffer untouched */
2423 /* Refill processing */
2424 err = hwbm_pool_refill(&bm_pool->hwbm_pool, GFP_ATOMIC);
2426 netdev_err(dev, "Linux processing - Can't refill\n");
2428 goto err_drop_frame_ret_pool;
2431 frag_size = bm_pool->hwbm_pool.frag_size;
2433 skb = build_skb(data, frag_size > PAGE_SIZE ? 0 : frag_size);
2435 /* After refill old buffer has to be unmapped regardless
2436 * the skb is successfully built or not.
2438 dma_unmap_single(&pp->bm_priv->pdev->dev, phys_addr,
2439 bm_pool->buf_size, DMA_FROM_DEVICE);
2441 goto err_drop_frame;
2444 rcvd_bytes += rx_bytes;
2446 /* Linux processing */
2447 skb_reserve(skb, MVNETA_MH_SIZE + NET_SKB_PAD);
2448 skb_put(skb, rx_bytes);
2450 skb->protocol = eth_type_trans(skb, dev);
2452 mvneta_rx_csum(pp, rx_status, skb);
2454 napi_gro_receive(napi, skb);
2458 mvneta_update_stats(pp, rcvd_pkts, rcvd_bytes, false);
2460 /* Update rxq management counters */
2461 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
2467 mvneta_tso_put_hdr(struct sk_buff *skb,
2468 struct mvneta_port *pp, struct mvneta_tx_queue *txq)
2470 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2471 struct mvneta_tx_buf *buf = &txq->buf[txq->txq_put_index];
2472 struct mvneta_tx_desc *tx_desc;
2474 tx_desc = mvneta_txq_next_desc_get(txq);
2475 tx_desc->data_size = hdr_len;
2476 tx_desc->command = mvneta_skb_tx_csum(pp, skb);
2477 tx_desc->command |= MVNETA_TXD_F_DESC;
2478 tx_desc->buf_phys_addr = txq->tso_hdrs_phys +
2479 txq->txq_put_index * TSO_HEADER_SIZE;
2480 buf->type = MVNETA_TYPE_SKB;
2483 mvneta_txq_inc_put(txq);
2487 mvneta_tso_put_data(struct net_device *dev, struct mvneta_tx_queue *txq,
2488 struct sk_buff *skb, char *data, int size,
2489 bool last_tcp, bool is_last)
2491 struct mvneta_tx_buf *buf = &txq->buf[txq->txq_put_index];
2492 struct mvneta_tx_desc *tx_desc;
2494 tx_desc = mvneta_txq_next_desc_get(txq);
2495 tx_desc->data_size = size;
2496 tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, data,
2497 size, DMA_TO_DEVICE);
2498 if (unlikely(dma_mapping_error(dev->dev.parent,
2499 tx_desc->buf_phys_addr))) {
2500 mvneta_txq_desc_put(txq);
2504 tx_desc->command = 0;
2505 buf->type = MVNETA_TYPE_SKB;
2509 /* last descriptor in the TCP packet */
2510 tx_desc->command = MVNETA_TXD_L_DESC;
2512 /* last descriptor in SKB */
2516 mvneta_txq_inc_put(txq);
2520 static int mvneta_tx_tso(struct sk_buff *skb, struct net_device *dev,
2521 struct mvneta_tx_queue *txq)
2523 int total_len, data_left;
2525 struct mvneta_port *pp = netdev_priv(dev);
2527 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2530 /* Count needed descriptors */
2531 if ((txq->count + tso_count_descs(skb)) >= txq->size)
2534 if (skb_headlen(skb) < (skb_transport_offset(skb) + tcp_hdrlen(skb))) {
2535 pr_info("*** Is this even possible???!?!?\n");
2539 /* Initialize the TSO handler, and prepare the first payload */
2540 tso_start(skb, &tso);
2542 total_len = skb->len - hdr_len;
2543 while (total_len > 0) {
2546 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
2547 total_len -= data_left;
2550 /* prepare packet headers: MAC + IP + TCP */
2551 hdr = txq->tso_hdrs + txq->txq_put_index * TSO_HEADER_SIZE;
2552 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
2554 mvneta_tso_put_hdr(skb, pp, txq);
2556 while (data_left > 0) {
2560 size = min_t(int, tso.size, data_left);
2562 if (mvneta_tso_put_data(dev, txq, skb,
2569 tso_build_data(skb, &tso, size);
2576 /* Release all used data descriptors; header descriptors must not
2579 for (i = desc_count - 1; i >= 0; i--) {
2580 struct mvneta_tx_desc *tx_desc = txq->descs + i;
2581 if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr))
2582 dma_unmap_single(pp->dev->dev.parent,
2583 tx_desc->buf_phys_addr,
2586 mvneta_txq_desc_put(txq);
2591 /* Handle tx fragmentation processing */
2592 static int mvneta_tx_frag_process(struct mvneta_port *pp, struct sk_buff *skb,
2593 struct mvneta_tx_queue *txq)
2595 struct mvneta_tx_desc *tx_desc;
2596 int i, nr_frags = skb_shinfo(skb)->nr_frags;
2598 for (i = 0; i < nr_frags; i++) {
2599 struct mvneta_tx_buf *buf = &txq->buf[txq->txq_put_index];
2600 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2601 void *addr = skb_frag_address(frag);
2603 tx_desc = mvneta_txq_next_desc_get(txq);
2604 tx_desc->data_size = skb_frag_size(frag);
2606 tx_desc->buf_phys_addr =
2607 dma_map_single(pp->dev->dev.parent, addr,
2608 tx_desc->data_size, DMA_TO_DEVICE);
2610 if (dma_mapping_error(pp->dev->dev.parent,
2611 tx_desc->buf_phys_addr)) {
2612 mvneta_txq_desc_put(txq);
2616 if (i == nr_frags - 1) {
2617 /* Last descriptor */
2618 tx_desc->command = MVNETA_TXD_L_DESC | MVNETA_TXD_Z_PAD;
2621 /* Descriptor in the middle: Not First, Not Last */
2622 tx_desc->command = 0;
2625 buf->type = MVNETA_TYPE_SKB;
2626 mvneta_txq_inc_put(txq);
2632 /* Release all descriptors that were used to map fragments of
2633 * this packet, as well as the corresponding DMA mappings
2635 for (i = i - 1; i >= 0; i--) {
2636 tx_desc = txq->descs + i;
2637 dma_unmap_single(pp->dev->dev.parent,
2638 tx_desc->buf_phys_addr,
2641 mvneta_txq_desc_put(txq);
2647 /* Main tx processing */
2648 static netdev_tx_t mvneta_tx(struct sk_buff *skb, struct net_device *dev)
2650 struct mvneta_port *pp = netdev_priv(dev);
2651 u16 txq_id = skb_get_queue_mapping(skb);
2652 struct mvneta_tx_queue *txq = &pp->txqs[txq_id];
2653 struct mvneta_tx_buf *buf = &txq->buf[txq->txq_put_index];
2654 struct mvneta_tx_desc *tx_desc;
2659 if (!netif_running(dev))
2662 if (skb_is_gso(skb)) {
2663 frags = mvneta_tx_tso(skb, dev, txq);
2667 frags = skb_shinfo(skb)->nr_frags + 1;
2669 /* Get a descriptor for the first part of the packet */
2670 tx_desc = mvneta_txq_next_desc_get(txq);
2672 tx_cmd = mvneta_skb_tx_csum(pp, skb);
2674 tx_desc->data_size = skb_headlen(skb);
2676 tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, skb->data,
2679 if (unlikely(dma_mapping_error(dev->dev.parent,
2680 tx_desc->buf_phys_addr))) {
2681 mvneta_txq_desc_put(txq);
2686 buf->type = MVNETA_TYPE_SKB;
2688 /* First and Last descriptor */
2689 tx_cmd |= MVNETA_TXD_FLZ_DESC;
2690 tx_desc->command = tx_cmd;
2692 mvneta_txq_inc_put(txq);
2694 /* First but not Last */
2695 tx_cmd |= MVNETA_TXD_F_DESC;
2697 mvneta_txq_inc_put(txq);
2698 tx_desc->command = tx_cmd;
2699 /* Continue with other skb fragments */
2700 if (mvneta_tx_frag_process(pp, skb, txq)) {
2701 dma_unmap_single(dev->dev.parent,
2702 tx_desc->buf_phys_addr,
2705 mvneta_txq_desc_put(txq);
2713 struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
2715 netdev_tx_sent_queue(nq, len);
2717 txq->count += frags;
2718 if (txq->count >= txq->tx_stop_threshold)
2719 netif_tx_stop_queue(nq);
2721 if (!netdev_xmit_more() || netif_xmit_stopped(nq) ||
2722 txq->pending + frags > MVNETA_TXQ_DEC_SENT_MASK)
2723 mvneta_txq_pend_desc_add(pp, txq, frags);
2725 txq->pending += frags;
2727 mvneta_update_stats(pp, 1, len, true);
2729 dev->stats.tx_dropped++;
2730 dev_kfree_skb_any(skb);
2733 return NETDEV_TX_OK;
2737 /* Free tx resources, when resetting a port */
2738 static void mvneta_txq_done_force(struct mvneta_port *pp,
2739 struct mvneta_tx_queue *txq)
2742 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
2743 int tx_done = txq->count;
2745 mvneta_txq_bufs_free(pp, txq, tx_done, nq);
2749 txq->txq_put_index = 0;
2750 txq->txq_get_index = 0;
2753 /* Handle tx done - called in softirq context. The <cause_tx_done> argument
2754 * must be a valid cause according to MVNETA_TXQ_INTR_MASK_ALL.
2756 static void mvneta_tx_done_gbe(struct mvneta_port *pp, u32 cause_tx_done)
2758 struct mvneta_tx_queue *txq;
2759 struct netdev_queue *nq;
2760 int cpu = smp_processor_id();
2762 while (cause_tx_done) {
2763 txq = mvneta_tx_done_policy(pp, cause_tx_done);
2765 nq = netdev_get_tx_queue(pp->dev, txq->id);
2766 __netif_tx_lock(nq, cpu);
2769 mvneta_txq_done(pp, txq);
2771 __netif_tx_unlock(nq);
2772 cause_tx_done &= ~((1 << txq->id));
2776 /* Compute crc8 of the specified address, using a unique algorithm ,
2777 * according to hw spec, different than generic crc8 algorithm
2779 static int mvneta_addr_crc(unsigned char *addr)
2784 for (i = 0; i < ETH_ALEN; i++) {
2787 crc = (crc ^ addr[i]) << 8;
2788 for (j = 7; j >= 0; j--) {
2789 if (crc & (0x100 << j))
2797 /* This method controls the net device special MAC multicast support.
2798 * The Special Multicast Table for MAC addresses supports MAC of the form
2799 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
2800 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
2801 * Table entries in the DA-Filter table. This method set the Special
2802 * Multicast Table appropriate entry.
2804 static void mvneta_set_special_mcast_addr(struct mvneta_port *pp,
2805 unsigned char last_byte,
2808 unsigned int smc_table_reg;
2809 unsigned int tbl_offset;
2810 unsigned int reg_offset;
2812 /* Register offset from SMC table base */
2813 tbl_offset = (last_byte / 4);
2814 /* Entry offset within the above reg */
2815 reg_offset = last_byte % 4;
2817 smc_table_reg = mvreg_read(pp, (MVNETA_DA_FILT_SPEC_MCAST
2821 smc_table_reg &= ~(0xff << (8 * reg_offset));
2823 smc_table_reg &= ~(0xff << (8 * reg_offset));
2824 smc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
2827 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + tbl_offset * 4,
2831 /* This method controls the network device Other MAC multicast support.
2832 * The Other Multicast Table is used for multicast of another type.
2833 * A CRC-8 is used as an index to the Other Multicast Table entries
2834 * in the DA-Filter table.
2835 * The method gets the CRC-8 value from the calling routine and
2836 * sets the Other Multicast Table appropriate entry according to the
2839 static void mvneta_set_other_mcast_addr(struct mvneta_port *pp,
2843 unsigned int omc_table_reg;
2844 unsigned int tbl_offset;
2845 unsigned int reg_offset;
2847 tbl_offset = (crc8 / 4) * 4; /* Register offset from OMC table base */
2848 reg_offset = crc8 % 4; /* Entry offset within the above reg */
2850 omc_table_reg = mvreg_read(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset);
2853 /* Clear accepts frame bit at specified Other DA table entry */
2854 omc_table_reg &= ~(0xff << (8 * reg_offset));
2856 omc_table_reg &= ~(0xff << (8 * reg_offset));
2857 omc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
2860 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset, omc_table_reg);
2863 /* The network device supports multicast using two tables:
2864 * 1) Special Multicast Table for MAC addresses of the form
2865 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
2866 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
2867 * Table entries in the DA-Filter table.
2868 * 2) Other Multicast Table for multicast of another type. A CRC-8 value
2869 * is used as an index to the Other Multicast Table entries in the
2872 static int mvneta_mcast_addr_set(struct mvneta_port *pp, unsigned char *p_addr,
2875 unsigned char crc_result = 0;
2877 if (memcmp(p_addr, "\x01\x00\x5e\x00\x00", 5) == 0) {
2878 mvneta_set_special_mcast_addr(pp, p_addr[5], queue);
2882 crc_result = mvneta_addr_crc(p_addr);
2884 if (pp->mcast_count[crc_result] == 0) {
2885 netdev_info(pp->dev, "No valid Mcast for crc8=0x%02x\n",
2890 pp->mcast_count[crc_result]--;
2891 if (pp->mcast_count[crc_result] != 0) {
2892 netdev_info(pp->dev,
2893 "After delete there are %d valid Mcast for crc8=0x%02x\n",
2894 pp->mcast_count[crc_result], crc_result);
2898 pp->mcast_count[crc_result]++;
2900 mvneta_set_other_mcast_addr(pp, crc_result, queue);
2905 /* Configure Fitering mode of Ethernet port */
2906 static void mvneta_rx_unicast_promisc_set(struct mvneta_port *pp,
2909 u32 port_cfg_reg, val;
2911 port_cfg_reg = mvreg_read(pp, MVNETA_PORT_CONFIG);
2913 val = mvreg_read(pp, MVNETA_TYPE_PRIO);
2915 /* Set / Clear UPM bit in port configuration register */
2917 /* Accept all Unicast addresses */
2918 port_cfg_reg |= MVNETA_UNI_PROMISC_MODE;
2919 val |= MVNETA_FORCE_UNI;
2920 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, 0xffff);
2921 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, 0xffffffff);
2923 /* Reject all Unicast addresses */
2924 port_cfg_reg &= ~MVNETA_UNI_PROMISC_MODE;
2925 val &= ~MVNETA_FORCE_UNI;
2928 mvreg_write(pp, MVNETA_PORT_CONFIG, port_cfg_reg);
2929 mvreg_write(pp, MVNETA_TYPE_PRIO, val);
2932 /* register unicast and multicast addresses */
2933 static void mvneta_set_rx_mode(struct net_device *dev)
2935 struct mvneta_port *pp = netdev_priv(dev);
2936 struct netdev_hw_addr *ha;
2938 if (dev->flags & IFF_PROMISC) {
2939 /* Accept all: Multicast + Unicast */
2940 mvneta_rx_unicast_promisc_set(pp, 1);
2941 mvneta_set_ucast_table(pp, pp->rxq_def);
2942 mvneta_set_special_mcast_table(pp, pp->rxq_def);
2943 mvneta_set_other_mcast_table(pp, pp->rxq_def);
2945 /* Accept single Unicast */
2946 mvneta_rx_unicast_promisc_set(pp, 0);
2947 mvneta_set_ucast_table(pp, -1);
2948 mvneta_mac_addr_set(pp, dev->dev_addr, pp->rxq_def);
2950 if (dev->flags & IFF_ALLMULTI) {
2951 /* Accept all multicast */
2952 mvneta_set_special_mcast_table(pp, pp->rxq_def);
2953 mvneta_set_other_mcast_table(pp, pp->rxq_def);
2955 /* Accept only initialized multicast */
2956 mvneta_set_special_mcast_table(pp, -1);
2957 mvneta_set_other_mcast_table(pp, -1);
2959 if (!netdev_mc_empty(dev)) {
2960 netdev_for_each_mc_addr(ha, dev) {
2961 mvneta_mcast_addr_set(pp, ha->addr,
2969 /* Interrupt handling - the callback for request_irq() */
2970 static irqreturn_t mvneta_isr(int irq, void *dev_id)
2972 struct mvneta_port *pp = (struct mvneta_port *)dev_id;
2974 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
2975 napi_schedule(&pp->napi);
2980 /* Interrupt handling - the callback for request_percpu_irq() */
2981 static irqreturn_t mvneta_percpu_isr(int irq, void *dev_id)
2983 struct mvneta_pcpu_port *port = (struct mvneta_pcpu_port *)dev_id;
2985 disable_percpu_irq(port->pp->dev->irq);
2986 napi_schedule(&port->napi);
2991 static void mvneta_link_change(struct mvneta_port *pp)
2993 u32 gmac_stat = mvreg_read(pp, MVNETA_GMAC_STATUS);
2995 phylink_mac_change(pp->phylink, !!(gmac_stat & MVNETA_GMAC_LINK_UP));
2999 * Bits 0 - 7 of the causeRxTx register indicate that are transmitted
3000 * packets on the corresponding TXQ (Bit 0 is for TX queue 1).
3001 * Bits 8 -15 of the cause Rx Tx register indicate that are received
3002 * packets on the corresponding RXQ (Bit 8 is for RX queue 0).
3003 * Each CPU has its own causeRxTx register
3005 static int mvneta_poll(struct napi_struct *napi, int budget)
3010 struct mvneta_port *pp = netdev_priv(napi->dev);
3011 struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
3013 if (!netif_running(pp->dev)) {
3014 napi_complete(napi);
3018 /* Read cause register */
3019 cause_rx_tx = mvreg_read(pp, MVNETA_INTR_NEW_CAUSE);
3020 if (cause_rx_tx & MVNETA_MISCINTR_INTR_MASK) {
3021 u32 cause_misc = mvreg_read(pp, MVNETA_INTR_MISC_CAUSE);
3023 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
3025 if (cause_misc & (MVNETA_CAUSE_PHY_STATUS_CHANGE |
3026 MVNETA_CAUSE_LINK_CHANGE))
3027 mvneta_link_change(pp);
3030 /* Release Tx descriptors */
3031 if (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL) {
3032 mvneta_tx_done_gbe(pp, (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL));
3033 cause_rx_tx &= ~MVNETA_TX_INTR_MASK_ALL;
3036 /* For the case where the last mvneta_poll did not process all
3039 rx_queue = fls(((cause_rx_tx >> 8) & 0xff));
3041 cause_rx_tx |= pp->neta_armada3700 ? pp->cause_rx_tx :
3045 rx_queue = rx_queue - 1;
3047 rx_done = mvneta_rx_hwbm(napi, pp, budget,
3048 &pp->rxqs[rx_queue]);
3050 rx_done = mvneta_rx_swbm(napi, pp, budget,
3051 &pp->rxqs[rx_queue]);
3054 if (rx_done < budget) {
3056 napi_complete_done(napi, rx_done);
3058 if (pp->neta_armada3700) {
3059 unsigned long flags;
3061 local_irq_save(flags);
3062 mvreg_write(pp, MVNETA_INTR_NEW_MASK,
3063 MVNETA_RX_INTR_MASK(rxq_number) |
3064 MVNETA_TX_INTR_MASK(txq_number) |
3065 MVNETA_MISCINTR_INTR_MASK);
3066 local_irq_restore(flags);
3068 enable_percpu_irq(pp->dev->irq, 0);
3072 if (pp->neta_armada3700)
3073 pp->cause_rx_tx = cause_rx_tx;
3075 port->cause_rx_tx = cause_rx_tx;
3080 static int mvneta_create_page_pool(struct mvneta_port *pp,
3081 struct mvneta_rx_queue *rxq, int size)
3083 struct bpf_prog *xdp_prog = READ_ONCE(pp->xdp_prog);
3084 struct page_pool_params pp_params = {
3086 .flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV,
3088 .nid = NUMA_NO_NODE,
3089 .dev = pp->dev->dev.parent,
3090 .dma_dir = xdp_prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE,
3091 .offset = pp->rx_offset_correction,
3092 .max_len = MVNETA_MAX_RX_BUF_SIZE,
3096 rxq->page_pool = page_pool_create(&pp_params);
3097 if (IS_ERR(rxq->page_pool)) {
3098 err = PTR_ERR(rxq->page_pool);
3099 rxq->page_pool = NULL;
3103 err = xdp_rxq_info_reg(&rxq->xdp_rxq, pp->dev, rxq->id);
3107 err = xdp_rxq_info_reg_mem_model(&rxq->xdp_rxq, MEM_TYPE_PAGE_POOL,
3110 goto err_unregister_rxq;
3115 xdp_rxq_info_unreg(&rxq->xdp_rxq);
3117 page_pool_destroy(rxq->page_pool);
3118 rxq->page_pool = NULL;
3122 /* Handle rxq fill: allocates rxq skbs; called when initializing a port */
3123 static int mvneta_rxq_fill(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
3128 err = mvneta_create_page_pool(pp, rxq, num);
3132 for (i = 0; i < num; i++) {
3133 memset(rxq->descs + i, 0, sizeof(struct mvneta_rx_desc));
3134 if (mvneta_rx_refill(pp, rxq->descs + i, rxq,
3137 "%s:rxq %d, %d of %d buffs filled\n",
3138 __func__, rxq->id, i, num);
3143 /* Add this number of RX descriptors as non occupied (ready to
3146 mvneta_rxq_non_occup_desc_add(pp, rxq, i);
3151 /* Free all packets pending transmit from all TXQs and reset TX port */
3152 static void mvneta_tx_reset(struct mvneta_port *pp)
3156 /* free the skb's in the tx ring */
3157 for (queue = 0; queue < txq_number; queue++)
3158 mvneta_txq_done_force(pp, &pp->txqs[queue]);
3160 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
3161 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
3164 static void mvneta_rx_reset(struct mvneta_port *pp)
3166 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
3167 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
3170 /* Rx/Tx queue initialization/cleanup methods */
3172 static int mvneta_rxq_sw_init(struct mvneta_port *pp,
3173 struct mvneta_rx_queue *rxq)
3175 rxq->size = pp->rx_ring_size;
3177 /* Allocate memory for RX descriptors */
3178 rxq->descs = dma_alloc_coherent(pp->dev->dev.parent,
3179 rxq->size * MVNETA_DESC_ALIGNED_SIZE,
3180 &rxq->descs_phys, GFP_KERNEL);
3184 rxq->last_desc = rxq->size - 1;
3189 static void mvneta_rxq_hw_init(struct mvneta_port *pp,
3190 struct mvneta_rx_queue *rxq)
3192 /* Set Rx descriptors queue starting address */
3193 mvreg_write(pp, MVNETA_RXQ_BASE_ADDR_REG(rxq->id), rxq->descs_phys);
3194 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), rxq->size);
3196 /* Set coalescing pkts and time */
3197 mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
3198 mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
3202 mvneta_rxq_offset_set(pp, rxq, 0);
3203 mvneta_rxq_buf_size_set(pp, rxq, PAGE_SIZE < SZ_64K ?
3204 MVNETA_MAX_RX_BUF_SIZE :
3205 MVNETA_RX_BUF_SIZE(pp->pkt_size));
3206 mvneta_rxq_bm_disable(pp, rxq);
3207 mvneta_rxq_fill(pp, rxq, rxq->size);
3210 mvneta_rxq_offset_set(pp, rxq,
3211 NET_SKB_PAD - pp->rx_offset_correction);
3213 mvneta_rxq_bm_enable(pp, rxq);
3214 /* Fill RXQ with buffers from RX pool */
3215 mvneta_rxq_long_pool_set(pp, rxq);
3216 mvneta_rxq_short_pool_set(pp, rxq);
3217 mvneta_rxq_non_occup_desc_add(pp, rxq, rxq->size);
3221 /* Create a specified RX queue */
3222 static int mvneta_rxq_init(struct mvneta_port *pp,
3223 struct mvneta_rx_queue *rxq)
3228 ret = mvneta_rxq_sw_init(pp, rxq);
3232 mvneta_rxq_hw_init(pp, rxq);
3237 /* Cleanup Rx queue */
3238 static void mvneta_rxq_deinit(struct mvneta_port *pp,
3239 struct mvneta_rx_queue *rxq)
3241 mvneta_rxq_drop_pkts(pp, rxq);
3244 dev_kfree_skb_any(rxq->skb);
3247 dma_free_coherent(pp->dev->dev.parent,
3248 rxq->size * MVNETA_DESC_ALIGNED_SIZE,
3254 rxq->next_desc_to_proc = 0;
3255 rxq->descs_phys = 0;
3256 rxq->first_to_refill = 0;
3257 rxq->refill_num = 0;
3262 static int mvneta_txq_sw_init(struct mvneta_port *pp,
3263 struct mvneta_tx_queue *txq)
3267 txq->size = pp->tx_ring_size;
3269 /* A queue must always have room for at least one skb.
3270 * Therefore, stop the queue when the free entries reaches
3271 * the maximum number of descriptors per skb.
3273 txq->tx_stop_threshold = txq->size - MVNETA_MAX_SKB_DESCS;
3274 txq->tx_wake_threshold = txq->tx_stop_threshold / 2;
3276 /* Allocate memory for TX descriptors */
3277 txq->descs = dma_alloc_coherent(pp->dev->dev.parent,
3278 txq->size * MVNETA_DESC_ALIGNED_SIZE,
3279 &txq->descs_phys, GFP_KERNEL);
3283 txq->last_desc = txq->size - 1;
3285 txq->buf = kmalloc_array(txq->size, sizeof(*txq->buf), GFP_KERNEL);
3287 dma_free_coherent(pp->dev->dev.parent,
3288 txq->size * MVNETA_DESC_ALIGNED_SIZE,
3289 txq->descs, txq->descs_phys);
3293 /* Allocate DMA buffers for TSO MAC/IP/TCP headers */
3294 txq->tso_hdrs = dma_alloc_coherent(pp->dev->dev.parent,
3295 txq->size * TSO_HEADER_SIZE,
3296 &txq->tso_hdrs_phys, GFP_KERNEL);
3297 if (!txq->tso_hdrs) {
3299 dma_free_coherent(pp->dev->dev.parent,
3300 txq->size * MVNETA_DESC_ALIGNED_SIZE,
3301 txq->descs, txq->descs_phys);
3305 /* Setup XPS mapping */
3307 cpu = txq->id % num_present_cpus();
3309 cpu = pp->rxq_def % num_present_cpus();
3310 cpumask_set_cpu(cpu, &txq->affinity_mask);
3311 netif_set_xps_queue(pp->dev, &txq->affinity_mask, txq->id);
3316 static void mvneta_txq_hw_init(struct mvneta_port *pp,
3317 struct mvneta_tx_queue *txq)
3319 /* Set maximum bandwidth for enabled TXQs */
3320 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0x03ffffff);
3321 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0x3fffffff);
3323 /* Set Tx descriptors queue starting address */
3324 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), txq->descs_phys);
3325 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), txq->size);
3327 mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
3330 /* Create and initialize a tx queue */
3331 static int mvneta_txq_init(struct mvneta_port *pp,
3332 struct mvneta_tx_queue *txq)
3336 ret = mvneta_txq_sw_init(pp, txq);
3340 mvneta_txq_hw_init(pp, txq);
3345 /* Free allocated resources when mvneta_txq_init() fails to allocate memory*/
3346 static void mvneta_txq_sw_deinit(struct mvneta_port *pp,
3347 struct mvneta_tx_queue *txq)
3349 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
3354 dma_free_coherent(pp->dev->dev.parent,
3355 txq->size * TSO_HEADER_SIZE,
3356 txq->tso_hdrs, txq->tso_hdrs_phys);
3358 dma_free_coherent(pp->dev->dev.parent,
3359 txq->size * MVNETA_DESC_ALIGNED_SIZE,
3360 txq->descs, txq->descs_phys);
3362 netdev_tx_reset_queue(nq);
3366 txq->next_desc_to_proc = 0;
3367 txq->descs_phys = 0;
3370 static void mvneta_txq_hw_deinit(struct mvneta_port *pp,
3371 struct mvneta_tx_queue *txq)
3373 /* Set minimum bandwidth for disabled TXQs */
3374 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0);
3375 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0);
3377 /* Set Tx descriptors queue starting address and size */
3378 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), 0);
3379 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), 0);
3382 static void mvneta_txq_deinit(struct mvneta_port *pp,
3383 struct mvneta_tx_queue *txq)
3385 mvneta_txq_sw_deinit(pp, txq);
3386 mvneta_txq_hw_deinit(pp, txq);
3389 /* Cleanup all Tx queues */
3390 static void mvneta_cleanup_txqs(struct mvneta_port *pp)
3394 for (queue = 0; queue < txq_number; queue++)
3395 mvneta_txq_deinit(pp, &pp->txqs[queue]);
3398 /* Cleanup all Rx queues */
3399 static void mvneta_cleanup_rxqs(struct mvneta_port *pp)
3403 for (queue = 0; queue < rxq_number; queue++)
3404 mvneta_rxq_deinit(pp, &pp->rxqs[queue]);
3408 /* Init all Rx queues */
3409 static int mvneta_setup_rxqs(struct mvneta_port *pp)
3413 for (queue = 0; queue < rxq_number; queue++) {
3414 int err = mvneta_rxq_init(pp, &pp->rxqs[queue]);
3417 netdev_err(pp->dev, "%s: can't create rxq=%d\n",
3419 mvneta_cleanup_rxqs(pp);
3427 /* Init all tx queues */
3428 static int mvneta_setup_txqs(struct mvneta_port *pp)
3432 for (queue = 0; queue < txq_number; queue++) {
3433 int err = mvneta_txq_init(pp, &pp->txqs[queue]);
3435 netdev_err(pp->dev, "%s: can't create txq=%d\n",
3437 mvneta_cleanup_txqs(pp);
3445 static int mvneta_comphy_init(struct mvneta_port *pp)
3452 ret = phy_set_mode_ext(pp->comphy, PHY_MODE_ETHERNET,
3457 return phy_power_on(pp->comphy);
3460 static void mvneta_start_dev(struct mvneta_port *pp)
3464 WARN_ON(mvneta_comphy_init(pp));
3466 mvneta_max_rx_size_set(pp, pp->pkt_size);
3467 mvneta_txq_max_tx_size_set(pp, pp->pkt_size);
3469 /* start the Rx/Tx activity */
3470 mvneta_port_enable(pp);
3472 if (!pp->neta_armada3700) {
3473 /* Enable polling on the port */
3474 for_each_online_cpu(cpu) {
3475 struct mvneta_pcpu_port *port =
3476 per_cpu_ptr(pp->ports, cpu);
3478 napi_enable(&port->napi);
3481 napi_enable(&pp->napi);
3484 /* Unmask interrupts. It has to be done from each CPU */
3485 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
3487 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
3488 MVNETA_CAUSE_PHY_STATUS_CHANGE |
3489 MVNETA_CAUSE_LINK_CHANGE);
3491 phylink_start(pp->phylink);
3492 netif_tx_start_all_queues(pp->dev);
3495 static void mvneta_stop_dev(struct mvneta_port *pp)
3499 phylink_stop(pp->phylink);
3501 if (!pp->neta_armada3700) {
3502 for_each_online_cpu(cpu) {
3503 struct mvneta_pcpu_port *port =
3504 per_cpu_ptr(pp->ports, cpu);
3506 napi_disable(&port->napi);
3509 napi_disable(&pp->napi);
3512 netif_carrier_off(pp->dev);
3514 mvneta_port_down(pp);
3515 netif_tx_stop_all_queues(pp->dev);
3517 /* Stop the port activity */
3518 mvneta_port_disable(pp);
3520 /* Clear all ethernet port interrupts */
3521 on_each_cpu(mvneta_percpu_clear_intr_cause, pp, true);
3523 /* Mask all ethernet port interrupts */
3524 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
3526 mvneta_tx_reset(pp);
3527 mvneta_rx_reset(pp);
3529 WARN_ON(phy_power_off(pp->comphy));
3532 static void mvneta_percpu_enable(void *arg)
3534 struct mvneta_port *pp = arg;
3536 enable_percpu_irq(pp->dev->irq, IRQ_TYPE_NONE);
3539 static void mvneta_percpu_disable(void *arg)
3541 struct mvneta_port *pp = arg;
3543 disable_percpu_irq(pp->dev->irq);
3546 /* Change the device mtu */
3547 static int mvneta_change_mtu(struct net_device *dev, int mtu)
3549 struct mvneta_port *pp = netdev_priv(dev);
3552 if (!IS_ALIGNED(MVNETA_RX_PKT_SIZE(mtu), 8)) {
3553 netdev_info(dev, "Illegal MTU value %d, rounding to %d\n",
3554 mtu, ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8));
3555 mtu = ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8);
3558 if (pp->xdp_prog && mtu > MVNETA_MAX_RX_BUF_SIZE) {
3559 netdev_info(dev, "Illegal MTU value %d for XDP mode\n", mtu);
3565 if (!netif_running(dev)) {
3567 mvneta_bm_update_mtu(pp, mtu);
3569 netdev_update_features(dev);
3573 /* The interface is running, so we have to force a
3574 * reallocation of the queues
3576 mvneta_stop_dev(pp);
3577 on_each_cpu(mvneta_percpu_disable, pp, true);
3579 mvneta_cleanup_txqs(pp);
3580 mvneta_cleanup_rxqs(pp);
3583 mvneta_bm_update_mtu(pp, mtu);
3585 pp->pkt_size = MVNETA_RX_PKT_SIZE(dev->mtu);
3587 ret = mvneta_setup_rxqs(pp);
3589 netdev_err(dev, "unable to setup rxqs after MTU change\n");
3593 ret = mvneta_setup_txqs(pp);
3595 netdev_err(dev, "unable to setup txqs after MTU change\n");
3599 on_each_cpu(mvneta_percpu_enable, pp, true);
3600 mvneta_start_dev(pp);
3602 netdev_update_features(dev);
3607 static netdev_features_t mvneta_fix_features(struct net_device *dev,
3608 netdev_features_t features)
3610 struct mvneta_port *pp = netdev_priv(dev);
3612 if (pp->tx_csum_limit && dev->mtu > pp->tx_csum_limit) {
3613 features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
3615 "Disable IP checksum for MTU greater than %dB\n",
3622 /* Get mac address */
3623 static void mvneta_get_mac_addr(struct mvneta_port *pp, unsigned char *addr)
3625 u32 mac_addr_l, mac_addr_h;
3627 mac_addr_l = mvreg_read(pp, MVNETA_MAC_ADDR_LOW);
3628 mac_addr_h = mvreg_read(pp, MVNETA_MAC_ADDR_HIGH);
3629 addr[0] = (mac_addr_h >> 24) & 0xFF;
3630 addr[1] = (mac_addr_h >> 16) & 0xFF;
3631 addr[2] = (mac_addr_h >> 8) & 0xFF;
3632 addr[3] = mac_addr_h & 0xFF;
3633 addr[4] = (mac_addr_l >> 8) & 0xFF;
3634 addr[5] = mac_addr_l & 0xFF;
3637 /* Handle setting mac address */
3638 static int mvneta_set_mac_addr(struct net_device *dev, void *addr)
3640 struct mvneta_port *pp = netdev_priv(dev);
3641 struct sockaddr *sockaddr = addr;
3644 ret = eth_prepare_mac_addr_change(dev, addr);
3647 /* Remove previous address table entry */
3648 mvneta_mac_addr_set(pp, dev->dev_addr, -1);
3650 /* Set new addr in hw */
3651 mvneta_mac_addr_set(pp, sockaddr->sa_data, pp->rxq_def);
3653 eth_commit_mac_addr_change(dev, addr);
3657 static void mvneta_validate(struct phylink_config *config,
3658 unsigned long *supported,
3659 struct phylink_link_state *state)
3661 struct net_device *ndev = to_net_dev(config->dev);
3662 struct mvneta_port *pp = netdev_priv(ndev);
3663 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
3665 /* We only support QSGMII, SGMII, 802.3z and RGMII modes */
3666 if (state->interface != PHY_INTERFACE_MODE_NA &&
3667 state->interface != PHY_INTERFACE_MODE_QSGMII &&
3668 state->interface != PHY_INTERFACE_MODE_SGMII &&
3669 !phy_interface_mode_is_8023z(state->interface) &&
3670 !phy_interface_mode_is_rgmii(state->interface)) {
3671 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
3675 /* Allow all the expected bits */
3676 phylink_set(mask, Autoneg);
3677 phylink_set_port_modes(mask);
3679 /* Asymmetric pause is unsupported */
3680 phylink_set(mask, Pause);
3682 /* Half-duplex at speeds higher than 100Mbit is unsupported */
3683 if (pp->comphy || state->interface != PHY_INTERFACE_MODE_2500BASEX) {
3684 phylink_set(mask, 1000baseT_Full);
3685 phylink_set(mask, 1000baseX_Full);
3687 if (pp->comphy || state->interface == PHY_INTERFACE_MODE_2500BASEX) {
3688 phylink_set(mask, 2500baseT_Full);
3689 phylink_set(mask, 2500baseX_Full);
3692 if (!phy_interface_mode_is_8023z(state->interface)) {
3693 /* 10M and 100M are only supported in non-802.3z mode */
3694 phylink_set(mask, 10baseT_Half);
3695 phylink_set(mask, 10baseT_Full);
3696 phylink_set(mask, 100baseT_Half);
3697 phylink_set(mask, 100baseT_Full);
3700 bitmap_and(supported, supported, mask,
3701 __ETHTOOL_LINK_MODE_MASK_NBITS);
3702 bitmap_and(state->advertising, state->advertising, mask,
3703 __ETHTOOL_LINK_MODE_MASK_NBITS);
3705 /* We can only operate at 2500BaseX or 1000BaseX. If requested
3706 * to advertise both, only report advertising at 2500BaseX.
3708 phylink_helper_basex_speed(state);
3711 static void mvneta_mac_pcs_get_state(struct phylink_config *config,
3712 struct phylink_link_state *state)
3714 struct net_device *ndev = to_net_dev(config->dev);
3715 struct mvneta_port *pp = netdev_priv(ndev);
3718 gmac_stat = mvreg_read(pp, MVNETA_GMAC_STATUS);
3720 if (gmac_stat & MVNETA_GMAC_SPEED_1000)
3722 state->interface == PHY_INTERFACE_MODE_2500BASEX ?
3723 SPEED_2500 : SPEED_1000;
3724 else if (gmac_stat & MVNETA_GMAC_SPEED_100)
3725 state->speed = SPEED_100;
3727 state->speed = SPEED_10;
3729 state->an_complete = !!(gmac_stat & MVNETA_GMAC_AN_COMPLETE);
3730 state->link = !!(gmac_stat & MVNETA_GMAC_LINK_UP);
3731 state->duplex = !!(gmac_stat & MVNETA_GMAC_FULL_DUPLEX);
3734 if (gmac_stat & MVNETA_GMAC_RX_FLOW_CTRL_ENABLE)
3735 state->pause |= MLO_PAUSE_RX;
3736 if (gmac_stat & MVNETA_GMAC_TX_FLOW_CTRL_ENABLE)
3737 state->pause |= MLO_PAUSE_TX;
3740 static void mvneta_mac_an_restart(struct phylink_config *config)
3742 struct net_device *ndev = to_net_dev(config->dev);
3743 struct mvneta_port *pp = netdev_priv(ndev);
3744 u32 gmac_an = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3746 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
3747 gmac_an | MVNETA_GMAC_INBAND_RESTART_AN);
3748 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
3749 gmac_an & ~MVNETA_GMAC_INBAND_RESTART_AN);
3752 static void mvneta_mac_config(struct phylink_config *config, unsigned int mode,
3753 const struct phylink_link_state *state)
3755 struct net_device *ndev = to_net_dev(config->dev);
3756 struct mvneta_port *pp = netdev_priv(ndev);
3757 u32 new_ctrl0, gmac_ctrl0 = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
3758 u32 new_ctrl2, gmac_ctrl2 = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
3759 u32 new_ctrl4, gmac_ctrl4 = mvreg_read(pp, MVNETA_GMAC_CTRL_4);
3760 u32 new_clk, gmac_clk = mvreg_read(pp, MVNETA_GMAC_CLOCK_DIVIDER);
3761 u32 new_an, gmac_an = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3763 new_ctrl0 = gmac_ctrl0 & ~MVNETA_GMAC0_PORT_1000BASE_X;
3764 new_ctrl2 = gmac_ctrl2 & ~(MVNETA_GMAC2_INBAND_AN_ENABLE |
3765 MVNETA_GMAC2_PORT_RESET);
3766 new_ctrl4 = gmac_ctrl4 & ~(MVNETA_GMAC4_SHORT_PREAMBLE_ENABLE);
3767 new_clk = gmac_clk & ~MVNETA_GMAC_1MS_CLOCK_ENABLE;
3768 new_an = gmac_an & ~(MVNETA_GMAC_INBAND_AN_ENABLE |
3769 MVNETA_GMAC_INBAND_RESTART_AN |
3770 MVNETA_GMAC_CONFIG_MII_SPEED |
3771 MVNETA_GMAC_CONFIG_GMII_SPEED |
3772 MVNETA_GMAC_AN_SPEED_EN |
3773 MVNETA_GMAC_ADVERT_SYM_FLOW_CTRL |
3774 MVNETA_GMAC_CONFIG_FLOW_CTRL |
3775 MVNETA_GMAC_AN_FLOW_CTRL_EN |
3776 MVNETA_GMAC_CONFIG_FULL_DUPLEX |
3777 MVNETA_GMAC_AN_DUPLEX_EN);
3779 /* Even though it might look weird, when we're configured in
3780 * SGMII or QSGMII mode, the RGMII bit needs to be set.
3782 new_ctrl2 |= MVNETA_GMAC2_PORT_RGMII;
3784 if (state->interface == PHY_INTERFACE_MODE_QSGMII ||
3785 state->interface == PHY_INTERFACE_MODE_SGMII ||
3786 phy_interface_mode_is_8023z(state->interface))
3787 new_ctrl2 |= MVNETA_GMAC2_PCS_ENABLE;
3789 if (phylink_test(state->advertising, Pause))
3790 new_an |= MVNETA_GMAC_ADVERT_SYM_FLOW_CTRL;
3791 if (state->pause & MLO_PAUSE_TXRX_MASK)
3792 new_an |= MVNETA_GMAC_CONFIG_FLOW_CTRL;
3794 if (!phylink_autoneg_inband(mode)) {
3795 /* Phy or fixed speed */
3797 new_an |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
3799 if (state->speed == SPEED_1000 || state->speed == SPEED_2500)
3800 new_an |= MVNETA_GMAC_CONFIG_GMII_SPEED;
3801 else if (state->speed == SPEED_100)
3802 new_an |= MVNETA_GMAC_CONFIG_MII_SPEED;
3803 } else if (state->interface == PHY_INTERFACE_MODE_SGMII) {
3804 /* SGMII mode receives the state from the PHY */
3805 new_ctrl2 |= MVNETA_GMAC2_INBAND_AN_ENABLE;
3806 new_clk |= MVNETA_GMAC_1MS_CLOCK_ENABLE;
3807 new_an = (new_an & ~(MVNETA_GMAC_FORCE_LINK_DOWN |
3808 MVNETA_GMAC_FORCE_LINK_PASS)) |
3809 MVNETA_GMAC_INBAND_AN_ENABLE |
3810 MVNETA_GMAC_AN_SPEED_EN |
3811 MVNETA_GMAC_AN_DUPLEX_EN;
3813 /* 802.3z negotiation - only 1000base-X */
3814 new_ctrl0 |= MVNETA_GMAC0_PORT_1000BASE_X;
3815 new_clk |= MVNETA_GMAC_1MS_CLOCK_ENABLE;
3816 new_an = (new_an & ~(MVNETA_GMAC_FORCE_LINK_DOWN |
3817 MVNETA_GMAC_FORCE_LINK_PASS)) |
3818 MVNETA_GMAC_INBAND_AN_ENABLE |
3819 MVNETA_GMAC_CONFIG_GMII_SPEED |
3820 /* The MAC only supports FD mode */
3821 MVNETA_GMAC_CONFIG_FULL_DUPLEX;
3823 if (state->pause & MLO_PAUSE_AN && state->an_enabled)
3824 new_an |= MVNETA_GMAC_AN_FLOW_CTRL_EN;
3827 /* Armada 370 documentation says we can only change the port mode
3828 * and in-band enable when the link is down, so force it down
3829 * while making these changes. We also do this for GMAC_CTRL2 */
3830 if ((new_ctrl0 ^ gmac_ctrl0) & MVNETA_GMAC0_PORT_1000BASE_X ||
3831 (new_ctrl2 ^ gmac_ctrl2) & MVNETA_GMAC2_INBAND_AN_ENABLE ||
3832 (new_an ^ gmac_an) & MVNETA_GMAC_INBAND_AN_ENABLE) {
3833 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
3834 (gmac_an & ~MVNETA_GMAC_FORCE_LINK_PASS) |
3835 MVNETA_GMAC_FORCE_LINK_DOWN);
3839 /* When at 2.5G, the link partner can send frames with shortened
3842 if (state->speed == SPEED_2500)
3843 new_ctrl4 |= MVNETA_GMAC4_SHORT_PREAMBLE_ENABLE;
3845 if (pp->comphy && pp->phy_interface != state->interface &&
3846 (state->interface == PHY_INTERFACE_MODE_SGMII ||
3847 state->interface == PHY_INTERFACE_MODE_1000BASEX ||
3848 state->interface == PHY_INTERFACE_MODE_2500BASEX)) {
3849 pp->phy_interface = state->interface;
3851 WARN_ON(phy_power_off(pp->comphy));
3852 WARN_ON(mvneta_comphy_init(pp));
3855 if (new_ctrl0 != gmac_ctrl0)
3856 mvreg_write(pp, MVNETA_GMAC_CTRL_0, new_ctrl0);
3857 if (new_ctrl2 != gmac_ctrl2)
3858 mvreg_write(pp, MVNETA_GMAC_CTRL_2, new_ctrl2);
3859 if (new_ctrl4 != gmac_ctrl4)
3860 mvreg_write(pp, MVNETA_GMAC_CTRL_4, new_ctrl4);
3861 if (new_clk != gmac_clk)
3862 mvreg_write(pp, MVNETA_GMAC_CLOCK_DIVIDER, new_clk);
3863 if (new_an != gmac_an)
3864 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, new_an);
3866 if (gmac_ctrl2 & MVNETA_GMAC2_PORT_RESET) {
3867 while ((mvreg_read(pp, MVNETA_GMAC_CTRL_2) &
3868 MVNETA_GMAC2_PORT_RESET) != 0)
3873 static void mvneta_set_eee(struct mvneta_port *pp, bool enable)
3877 lpi_ctl1 = mvreg_read(pp, MVNETA_LPI_CTRL_1);
3879 lpi_ctl1 |= MVNETA_LPI_REQUEST_ENABLE;
3881 lpi_ctl1 &= ~MVNETA_LPI_REQUEST_ENABLE;
3882 mvreg_write(pp, MVNETA_LPI_CTRL_1, lpi_ctl1);
3885 static void mvneta_mac_link_down(struct phylink_config *config,
3886 unsigned int mode, phy_interface_t interface)
3888 struct net_device *ndev = to_net_dev(config->dev);
3889 struct mvneta_port *pp = netdev_priv(ndev);
3892 mvneta_port_down(pp);
3894 if (!phylink_autoneg_inband(mode)) {
3895 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3896 val &= ~MVNETA_GMAC_FORCE_LINK_PASS;
3897 val |= MVNETA_GMAC_FORCE_LINK_DOWN;
3898 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
3901 pp->eee_active = false;
3902 mvneta_set_eee(pp, false);
3905 static void mvneta_mac_link_up(struct phylink_config *config, unsigned int mode,
3906 phy_interface_t interface,
3907 struct phy_device *phy)
3909 struct net_device *ndev = to_net_dev(config->dev);
3910 struct mvneta_port *pp = netdev_priv(ndev);
3913 if (!phylink_autoneg_inband(mode)) {
3914 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3915 val &= ~MVNETA_GMAC_FORCE_LINK_DOWN;
3916 val |= MVNETA_GMAC_FORCE_LINK_PASS;
3917 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
3922 if (phy && pp->eee_enabled) {
3923 pp->eee_active = phy_init_eee(phy, 0) >= 0;
3924 mvneta_set_eee(pp, pp->eee_active && pp->tx_lpi_enabled);
3928 static const struct phylink_mac_ops mvneta_phylink_ops = {
3929 .validate = mvneta_validate,
3930 .mac_pcs_get_state = mvneta_mac_pcs_get_state,
3931 .mac_an_restart = mvneta_mac_an_restart,
3932 .mac_config = mvneta_mac_config,
3933 .mac_link_down = mvneta_mac_link_down,
3934 .mac_link_up = mvneta_mac_link_up,
3937 static int mvneta_mdio_probe(struct mvneta_port *pp)
3939 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
3940 int err = phylink_of_phy_connect(pp->phylink, pp->dn, 0);
3943 netdev_err(pp->dev, "could not attach PHY: %d\n", err);
3945 phylink_ethtool_get_wol(pp->phylink, &wol);
3946 device_set_wakeup_capable(&pp->dev->dev, !!wol.supported);
3951 static void mvneta_mdio_remove(struct mvneta_port *pp)
3953 phylink_disconnect_phy(pp->phylink);
3956 /* Electing a CPU must be done in an atomic way: it should be done
3957 * after or before the removal/insertion of a CPU and this function is
3960 static void mvneta_percpu_elect(struct mvneta_port *pp)
3962 int elected_cpu = 0, max_cpu, cpu, i = 0;
3964 /* Use the cpu associated to the rxq when it is online, in all
3965 * the other cases, use the cpu 0 which can't be offline.
3967 if (cpu_online(pp->rxq_def))
3968 elected_cpu = pp->rxq_def;
3970 max_cpu = num_present_cpus();
3972 for_each_online_cpu(cpu) {
3973 int rxq_map = 0, txq_map = 0;
3976 for (rxq = 0; rxq < rxq_number; rxq++)
3977 if ((rxq % max_cpu) == cpu)
3978 rxq_map |= MVNETA_CPU_RXQ_ACCESS(rxq);
3980 if (cpu == elected_cpu)
3981 /* Map the default receive queue queue to the
3984 rxq_map |= MVNETA_CPU_RXQ_ACCESS(pp->rxq_def);
3986 /* We update the TX queue map only if we have one
3987 * queue. In this case we associate the TX queue to
3988 * the CPU bound to the default RX queue
3990 if (txq_number == 1)
3991 txq_map = (cpu == elected_cpu) ?
3992 MVNETA_CPU_TXQ_ACCESS(1) : 0;
3994 txq_map = mvreg_read(pp, MVNETA_CPU_MAP(cpu)) &
3995 MVNETA_CPU_TXQ_ACCESS_ALL_MASK;
3997 mvreg_write(pp, MVNETA_CPU_MAP(cpu), rxq_map | txq_map);
3999 /* Update the interrupt mask on each CPU according the
4002 smp_call_function_single(cpu, mvneta_percpu_unmask_interrupt,
4009 static int mvneta_cpu_online(unsigned int cpu, struct hlist_node *node)
4012 struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
4014 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
4017 spin_lock(&pp->lock);
4019 * Configuring the driver for a new CPU while the driver is
4020 * stopping is racy, so just avoid it.
4022 if (pp->is_stopped) {
4023 spin_unlock(&pp->lock);
4026 netif_tx_stop_all_queues(pp->dev);
4029 * We have to synchronise on tha napi of each CPU except the one
4030 * just being woken up
4032 for_each_online_cpu(other_cpu) {
4033 if (other_cpu != cpu) {
4034 struct mvneta_pcpu_port *other_port =
4035 per_cpu_ptr(pp->ports, other_cpu);
4037 napi_synchronize(&other_port->napi);
4041 /* Mask all ethernet port interrupts */
4042 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
4043 napi_enable(&port->napi);
4046 * Enable per-CPU interrupts on the CPU that is
4049 mvneta_percpu_enable(pp);
4052 * Enable per-CPU interrupt on the one CPU we care
4055 mvneta_percpu_elect(pp);
4057 /* Unmask all ethernet port interrupts */
4058 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
4059 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
4060 MVNETA_CAUSE_PHY_STATUS_CHANGE |
4061 MVNETA_CAUSE_LINK_CHANGE);
4062 netif_tx_start_all_queues(pp->dev);
4063 spin_unlock(&pp->lock);
4067 static int mvneta_cpu_down_prepare(unsigned int cpu, struct hlist_node *node)
4069 struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
4071 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
4074 * Thanks to this lock we are sure that any pending cpu election is
4077 spin_lock(&pp->lock);
4078 /* Mask all ethernet port interrupts */
4079 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
4080 spin_unlock(&pp->lock);
4082 napi_synchronize(&port->napi);
4083 napi_disable(&port->napi);
4084 /* Disable per-CPU interrupts on the CPU that is brought down. */
4085 mvneta_percpu_disable(pp);
4089 static int mvneta_cpu_dead(unsigned int cpu, struct hlist_node *node)
4091 struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
4094 /* Check if a new CPU must be elected now this on is down */
4095 spin_lock(&pp->lock);
4096 mvneta_percpu_elect(pp);
4097 spin_unlock(&pp->lock);
4098 /* Unmask all ethernet port interrupts */
4099 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
4100 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
4101 MVNETA_CAUSE_PHY_STATUS_CHANGE |
4102 MVNETA_CAUSE_LINK_CHANGE);
4103 netif_tx_start_all_queues(pp->dev);
4107 static int mvneta_open(struct net_device *dev)
4109 struct mvneta_port *pp = netdev_priv(dev);
4112 pp->pkt_size = MVNETA_RX_PKT_SIZE(pp->dev->mtu);
4114 ret = mvneta_setup_rxqs(pp);
4118 ret = mvneta_setup_txqs(pp);
4120 goto err_cleanup_rxqs;
4122 /* Connect to port interrupt line */
4123 if (pp->neta_armada3700)
4124 ret = request_irq(pp->dev->irq, mvneta_isr, 0,
4127 ret = request_percpu_irq(pp->dev->irq, mvneta_percpu_isr,
4128 dev->name, pp->ports);
4130 netdev_err(pp->dev, "cannot request irq %d\n", pp->dev->irq);
4131 goto err_cleanup_txqs;
4134 if (!pp->neta_armada3700) {
4135 /* Enable per-CPU interrupt on all the CPU to handle our RX
4138 on_each_cpu(mvneta_percpu_enable, pp, true);
4140 pp->is_stopped = false;
4141 /* Register a CPU notifier to handle the case where our CPU
4142 * might be taken offline.
4144 ret = cpuhp_state_add_instance_nocalls(online_hpstate,
4149 ret = cpuhp_state_add_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
4152 goto err_free_online_hp;
4155 ret = mvneta_mdio_probe(pp);
4157 netdev_err(dev, "cannot probe MDIO bus\n");
4158 goto err_free_dead_hp;
4161 mvneta_start_dev(pp);
4166 if (!pp->neta_armada3700)
4167 cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
4170 if (!pp->neta_armada3700)
4171 cpuhp_state_remove_instance_nocalls(online_hpstate,
4174 if (pp->neta_armada3700) {
4175 free_irq(pp->dev->irq, pp);
4177 on_each_cpu(mvneta_percpu_disable, pp, true);
4178 free_percpu_irq(pp->dev->irq, pp->ports);
4181 mvneta_cleanup_txqs(pp);
4183 mvneta_cleanup_rxqs(pp);
4187 /* Stop the port, free port interrupt line */
4188 static int mvneta_stop(struct net_device *dev)
4190 struct mvneta_port *pp = netdev_priv(dev);
4192 if (!pp->neta_armada3700) {
4193 /* Inform that we are stopping so we don't want to setup the
4194 * driver for new CPUs in the notifiers. The code of the
4195 * notifier for CPU online is protected by the same spinlock,
4196 * so when we get the lock, the notifer work is done.
4198 spin_lock(&pp->lock);
4199 pp->is_stopped = true;
4200 spin_unlock(&pp->lock);
4202 mvneta_stop_dev(pp);
4203 mvneta_mdio_remove(pp);
4205 cpuhp_state_remove_instance_nocalls(online_hpstate,
4207 cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
4209 on_each_cpu(mvneta_percpu_disable, pp, true);
4210 free_percpu_irq(dev->irq, pp->ports);
4212 mvneta_stop_dev(pp);
4213 mvneta_mdio_remove(pp);
4214 free_irq(dev->irq, pp);
4217 mvneta_cleanup_rxqs(pp);
4218 mvneta_cleanup_txqs(pp);
4223 static int mvneta_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4225 struct mvneta_port *pp = netdev_priv(dev);
4227 return phylink_mii_ioctl(pp->phylink, ifr, cmd);
4230 static int mvneta_xdp_setup(struct net_device *dev, struct bpf_prog *prog,
4231 struct netlink_ext_ack *extack)
4233 bool need_update, running = netif_running(dev);
4234 struct mvneta_port *pp = netdev_priv(dev);
4235 struct bpf_prog *old_prog;
4237 if (prog && dev->mtu > MVNETA_MAX_RX_BUF_SIZE) {
4238 NL_SET_ERR_MSG_MOD(extack, "Jumbo frames not supported on XDP");
4243 NL_SET_ERR_MSG_MOD(extack,
4244 "Hardware Buffer Management not supported on XDP");
4248 need_update = !!pp->xdp_prog != !!prog;
4249 if (running && need_update)
4252 old_prog = xchg(&pp->xdp_prog, prog);
4254 bpf_prog_put(old_prog);
4256 if (running && need_update)
4257 return mvneta_open(dev);
4262 static int mvneta_xdp(struct net_device *dev, struct netdev_bpf *xdp)
4264 struct mvneta_port *pp = netdev_priv(dev);
4266 switch (xdp->command) {
4267 case XDP_SETUP_PROG:
4268 return mvneta_xdp_setup(dev, xdp->prog, xdp->extack);
4269 case XDP_QUERY_PROG:
4270 xdp->prog_id = pp->xdp_prog ? pp->xdp_prog->aux->id : 0;
4277 /* Ethtool methods */
4279 /* Set link ksettings (phy address, speed) for ethtools */
4281 mvneta_ethtool_set_link_ksettings(struct net_device *ndev,
4282 const struct ethtool_link_ksettings *cmd)
4284 struct mvneta_port *pp = netdev_priv(ndev);
4286 return phylink_ethtool_ksettings_set(pp->phylink, cmd);
4289 /* Get link ksettings for ethtools */
4291 mvneta_ethtool_get_link_ksettings(struct net_device *ndev,
4292 struct ethtool_link_ksettings *cmd)
4294 struct mvneta_port *pp = netdev_priv(ndev);
4296 return phylink_ethtool_ksettings_get(pp->phylink, cmd);
4299 static int mvneta_ethtool_nway_reset(struct net_device *dev)
4301 struct mvneta_port *pp = netdev_priv(dev);
4303 return phylink_ethtool_nway_reset(pp->phylink);
4306 /* Set interrupt coalescing for ethtools */
4307 static int mvneta_ethtool_set_coalesce(struct net_device *dev,
4308 struct ethtool_coalesce *c)
4310 struct mvneta_port *pp = netdev_priv(dev);
4313 for (queue = 0; queue < rxq_number; queue++) {
4314 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
4315 rxq->time_coal = c->rx_coalesce_usecs;
4316 rxq->pkts_coal = c->rx_max_coalesced_frames;
4317 mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
4318 mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
4321 for (queue = 0; queue < txq_number; queue++) {
4322 struct mvneta_tx_queue *txq = &pp->txqs[queue];
4323 txq->done_pkts_coal = c->tx_max_coalesced_frames;
4324 mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
4330 /* get coalescing for ethtools */
4331 static int mvneta_ethtool_get_coalesce(struct net_device *dev,
4332 struct ethtool_coalesce *c)
4334 struct mvneta_port *pp = netdev_priv(dev);
4336 c->rx_coalesce_usecs = pp->rxqs[0].time_coal;
4337 c->rx_max_coalesced_frames = pp->rxqs[0].pkts_coal;
4339 c->tx_max_coalesced_frames = pp->txqs[0].done_pkts_coal;
4344 static void mvneta_ethtool_get_drvinfo(struct net_device *dev,
4345 struct ethtool_drvinfo *drvinfo)
4347 strlcpy(drvinfo->driver, MVNETA_DRIVER_NAME,
4348 sizeof(drvinfo->driver));
4349 strlcpy(drvinfo->version, MVNETA_DRIVER_VERSION,
4350 sizeof(drvinfo->version));
4351 strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
4352 sizeof(drvinfo->bus_info));
4356 static void mvneta_ethtool_get_ringparam(struct net_device *netdev,
4357 struct ethtool_ringparam *ring)
4359 struct mvneta_port *pp = netdev_priv(netdev);
4361 ring->rx_max_pending = MVNETA_MAX_RXD;
4362 ring->tx_max_pending = MVNETA_MAX_TXD;
4363 ring->rx_pending = pp->rx_ring_size;
4364 ring->tx_pending = pp->tx_ring_size;
4367 static int mvneta_ethtool_set_ringparam(struct net_device *dev,
4368 struct ethtool_ringparam *ring)
4370 struct mvneta_port *pp = netdev_priv(dev);
4372 if ((ring->rx_pending == 0) || (ring->tx_pending == 0))
4374 pp->rx_ring_size = ring->rx_pending < MVNETA_MAX_RXD ?
4375 ring->rx_pending : MVNETA_MAX_RXD;
4377 pp->tx_ring_size = clamp_t(u16, ring->tx_pending,
4378 MVNETA_MAX_SKB_DESCS * 2, MVNETA_MAX_TXD);
4379 if (pp->tx_ring_size != ring->tx_pending)
4380 netdev_warn(dev, "TX queue size set to %u (requested %u)\n",
4381 pp->tx_ring_size, ring->tx_pending);
4383 if (netif_running(dev)) {
4385 if (mvneta_open(dev)) {
4387 "error on opening device after ring param change\n");
4395 static void mvneta_ethtool_get_pauseparam(struct net_device *dev,
4396 struct ethtool_pauseparam *pause)
4398 struct mvneta_port *pp = netdev_priv(dev);
4400 phylink_ethtool_get_pauseparam(pp->phylink, pause);
4403 static int mvneta_ethtool_set_pauseparam(struct net_device *dev,
4404 struct ethtool_pauseparam *pause)
4406 struct mvneta_port *pp = netdev_priv(dev);
4408 return phylink_ethtool_set_pauseparam(pp->phylink, pause);
4411 static void mvneta_ethtool_get_strings(struct net_device *netdev, u32 sset,
4414 if (sset == ETH_SS_STATS) {
4417 for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++)
4418 memcpy(data + i * ETH_GSTRING_LEN,
4419 mvneta_statistics[i].name, ETH_GSTRING_LEN);
4423 static void mvneta_ethtool_update_stats(struct mvneta_port *pp)
4425 const struct mvneta_statistic *s;
4426 void __iomem *base = pp->base;
4431 for (i = 0, s = mvneta_statistics;
4432 s < mvneta_statistics + ARRAY_SIZE(mvneta_statistics);
4438 val = readl_relaxed(base + s->offset);
4441 /* Docs say to read low 32-bit then high */
4442 low = readl_relaxed(base + s->offset);
4443 high = readl_relaxed(base + s->offset + 4);
4444 val = (u64)high << 32 | low;
4447 switch (s->offset) {
4448 case ETHTOOL_STAT_EEE_WAKEUP:
4449 val = phylink_get_eee_err(pp->phylink);
4451 case ETHTOOL_STAT_SKB_ALLOC_ERR:
4452 val = pp->rxqs[0].skb_alloc_err;
4454 case ETHTOOL_STAT_REFILL_ERR:
4455 val = pp->rxqs[0].refill_err;
4461 pp->ethtool_stats[i] += val;
4465 static void mvneta_ethtool_get_stats(struct net_device *dev,
4466 struct ethtool_stats *stats, u64 *data)
4468 struct mvneta_port *pp = netdev_priv(dev);
4471 mvneta_ethtool_update_stats(pp);
4473 for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++)
4474 *data++ = pp->ethtool_stats[i];
4477 static int mvneta_ethtool_get_sset_count(struct net_device *dev, int sset)
4479 if (sset == ETH_SS_STATS)
4480 return ARRAY_SIZE(mvneta_statistics);
4484 static u32 mvneta_ethtool_get_rxfh_indir_size(struct net_device *dev)
4486 return MVNETA_RSS_LU_TABLE_SIZE;
4489 static int mvneta_ethtool_get_rxnfc(struct net_device *dev,
4490 struct ethtool_rxnfc *info,
4491 u32 *rules __always_unused)
4493 switch (info->cmd) {
4494 case ETHTOOL_GRXRINGS:
4495 info->data = rxq_number;
4504 static int mvneta_config_rss(struct mvneta_port *pp)
4509 netif_tx_stop_all_queues(pp->dev);
4511 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
4513 if (!pp->neta_armada3700) {
4514 /* We have to synchronise on the napi of each CPU */
4515 for_each_online_cpu(cpu) {
4516 struct mvneta_pcpu_port *pcpu_port =
4517 per_cpu_ptr(pp->ports, cpu);
4519 napi_synchronize(&pcpu_port->napi);
4520 napi_disable(&pcpu_port->napi);
4523 napi_synchronize(&pp->napi);
4524 napi_disable(&pp->napi);
4527 pp->rxq_def = pp->indir[0];
4529 /* Update unicast mapping */
4530 mvneta_set_rx_mode(pp->dev);
4532 /* Update val of portCfg register accordingly with all RxQueue types */
4533 val = MVNETA_PORT_CONFIG_DEFL_VALUE(pp->rxq_def);
4534 mvreg_write(pp, MVNETA_PORT_CONFIG, val);
4536 /* Update the elected CPU matching the new rxq_def */
4537 spin_lock(&pp->lock);
4538 mvneta_percpu_elect(pp);
4539 spin_unlock(&pp->lock);
4541 if (!pp->neta_armada3700) {
4542 /* We have to synchronise on the napi of each CPU */
4543 for_each_online_cpu(cpu) {
4544 struct mvneta_pcpu_port *pcpu_port =
4545 per_cpu_ptr(pp->ports, cpu);
4547 napi_enable(&pcpu_port->napi);
4550 napi_enable(&pp->napi);
4553 netif_tx_start_all_queues(pp->dev);
4558 static int mvneta_ethtool_set_rxfh(struct net_device *dev, const u32 *indir,
4559 const u8 *key, const u8 hfunc)
4561 struct mvneta_port *pp = netdev_priv(dev);
4563 /* Current code for Armada 3700 doesn't support RSS features yet */
4564 if (pp->neta_armada3700)
4567 /* We require at least one supported parameter to be changed
4568 * and no change in any of the unsupported parameters
4571 (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
4577 memcpy(pp->indir, indir, MVNETA_RSS_LU_TABLE_SIZE);
4579 return mvneta_config_rss(pp);
4582 static int mvneta_ethtool_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
4585 struct mvneta_port *pp = netdev_priv(dev);
4587 /* Current code for Armada 3700 doesn't support RSS features yet */
4588 if (pp->neta_armada3700)
4592 *hfunc = ETH_RSS_HASH_TOP;
4597 memcpy(indir, pp->indir, MVNETA_RSS_LU_TABLE_SIZE);
4602 static void mvneta_ethtool_get_wol(struct net_device *dev,
4603 struct ethtool_wolinfo *wol)
4605 struct mvneta_port *pp = netdev_priv(dev);
4607 phylink_ethtool_get_wol(pp->phylink, wol);
4610 static int mvneta_ethtool_set_wol(struct net_device *dev,
4611 struct ethtool_wolinfo *wol)
4613 struct mvneta_port *pp = netdev_priv(dev);
4616 ret = phylink_ethtool_set_wol(pp->phylink, wol);
4618 device_set_wakeup_enable(&dev->dev, !!wol->wolopts);
4623 static int mvneta_ethtool_get_eee(struct net_device *dev,
4624 struct ethtool_eee *eee)
4626 struct mvneta_port *pp = netdev_priv(dev);
4629 lpi_ctl0 = mvreg_read(pp, MVNETA_LPI_CTRL_0);
4631 eee->eee_enabled = pp->eee_enabled;
4632 eee->eee_active = pp->eee_active;
4633 eee->tx_lpi_enabled = pp->tx_lpi_enabled;
4634 eee->tx_lpi_timer = (lpi_ctl0) >> 8; // * scale;
4636 return phylink_ethtool_get_eee(pp->phylink, eee);
4639 static int mvneta_ethtool_set_eee(struct net_device *dev,
4640 struct ethtool_eee *eee)
4642 struct mvneta_port *pp = netdev_priv(dev);
4645 /* The Armada 37x documents do not give limits for this other than
4646 * it being an 8-bit register. */
4647 if (eee->tx_lpi_enabled && eee->tx_lpi_timer > 255)
4650 lpi_ctl0 = mvreg_read(pp, MVNETA_LPI_CTRL_0);
4651 lpi_ctl0 &= ~(0xff << 8);
4652 lpi_ctl0 |= eee->tx_lpi_timer << 8;
4653 mvreg_write(pp, MVNETA_LPI_CTRL_0, lpi_ctl0);
4655 pp->eee_enabled = eee->eee_enabled;
4656 pp->tx_lpi_enabled = eee->tx_lpi_enabled;
4658 mvneta_set_eee(pp, eee->tx_lpi_enabled && eee->eee_enabled);
4660 return phylink_ethtool_set_eee(pp->phylink, eee);
4663 static const struct net_device_ops mvneta_netdev_ops = {
4664 .ndo_open = mvneta_open,
4665 .ndo_stop = mvneta_stop,
4666 .ndo_start_xmit = mvneta_tx,
4667 .ndo_set_rx_mode = mvneta_set_rx_mode,
4668 .ndo_set_mac_address = mvneta_set_mac_addr,
4669 .ndo_change_mtu = mvneta_change_mtu,
4670 .ndo_fix_features = mvneta_fix_features,
4671 .ndo_get_stats64 = mvneta_get_stats64,
4672 .ndo_do_ioctl = mvneta_ioctl,
4673 .ndo_bpf = mvneta_xdp,
4674 .ndo_xdp_xmit = mvneta_xdp_xmit,
4677 static const struct ethtool_ops mvneta_eth_tool_ops = {
4678 .nway_reset = mvneta_ethtool_nway_reset,
4679 .get_link = ethtool_op_get_link,
4680 .set_coalesce = mvneta_ethtool_set_coalesce,
4681 .get_coalesce = mvneta_ethtool_get_coalesce,
4682 .get_drvinfo = mvneta_ethtool_get_drvinfo,
4683 .get_ringparam = mvneta_ethtool_get_ringparam,
4684 .set_ringparam = mvneta_ethtool_set_ringparam,
4685 .get_pauseparam = mvneta_ethtool_get_pauseparam,
4686 .set_pauseparam = mvneta_ethtool_set_pauseparam,
4687 .get_strings = mvneta_ethtool_get_strings,
4688 .get_ethtool_stats = mvneta_ethtool_get_stats,
4689 .get_sset_count = mvneta_ethtool_get_sset_count,
4690 .get_rxfh_indir_size = mvneta_ethtool_get_rxfh_indir_size,
4691 .get_rxnfc = mvneta_ethtool_get_rxnfc,
4692 .get_rxfh = mvneta_ethtool_get_rxfh,
4693 .set_rxfh = mvneta_ethtool_set_rxfh,
4694 .get_link_ksettings = mvneta_ethtool_get_link_ksettings,
4695 .set_link_ksettings = mvneta_ethtool_set_link_ksettings,
4696 .get_wol = mvneta_ethtool_get_wol,
4697 .set_wol = mvneta_ethtool_set_wol,
4698 .get_eee = mvneta_ethtool_get_eee,
4699 .set_eee = mvneta_ethtool_set_eee,
4703 static int mvneta_init(struct device *dev, struct mvneta_port *pp)
4708 mvneta_port_disable(pp);
4710 /* Set port default values */
4711 mvneta_defaults_set(pp);
4713 pp->txqs = devm_kcalloc(dev, txq_number, sizeof(*pp->txqs), GFP_KERNEL);
4717 /* Initialize TX descriptor rings */
4718 for (queue = 0; queue < txq_number; queue++) {
4719 struct mvneta_tx_queue *txq = &pp->txqs[queue];
4721 txq->size = pp->tx_ring_size;
4722 txq->done_pkts_coal = MVNETA_TXDONE_COAL_PKTS;
4725 pp->rxqs = devm_kcalloc(dev, rxq_number, sizeof(*pp->rxqs), GFP_KERNEL);
4729 /* Create Rx descriptor rings */
4730 for (queue = 0; queue < rxq_number; queue++) {
4731 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
4733 rxq->size = pp->rx_ring_size;
4734 rxq->pkts_coal = MVNETA_RX_COAL_PKTS;
4735 rxq->time_coal = MVNETA_RX_COAL_USEC;
4737 = devm_kmalloc_array(pp->dev->dev.parent,
4739 sizeof(*rxq->buf_virt_addr),
4741 if (!rxq->buf_virt_addr)
4748 /* platform glue : initialize decoding windows */
4749 static void mvneta_conf_mbus_windows(struct mvneta_port *pp,
4750 const struct mbus_dram_target_info *dram)
4756 for (i = 0; i < 6; i++) {
4757 mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
4758 mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
4761 mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
4768 for (i = 0; i < dram->num_cs; i++) {
4769 const struct mbus_dram_window *cs = dram->cs + i;
4771 mvreg_write(pp, MVNETA_WIN_BASE(i),
4772 (cs->base & 0xffff0000) |
4773 (cs->mbus_attr << 8) |
4774 dram->mbus_dram_target_id);
4776 mvreg_write(pp, MVNETA_WIN_SIZE(i),
4777 (cs->size - 1) & 0xffff0000);
4779 win_enable &= ~(1 << i);
4780 win_protect |= 3 << (2 * i);
4783 /* For Armada3700 open default 4GB Mbus window, leaving
4784 * arbitration of target/attribute to a different layer
4787 mvreg_write(pp, MVNETA_WIN_SIZE(0), 0xffff0000);
4788 win_enable &= ~BIT(0);
4792 mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
4793 mvreg_write(pp, MVNETA_ACCESS_PROTECT_ENABLE, win_protect);
4796 /* Power up the port */
4797 static int mvneta_port_power_up(struct mvneta_port *pp, int phy_mode)
4799 /* MAC Cause register should be cleared */
4800 mvreg_write(pp, MVNETA_UNIT_INTR_CAUSE, 0);
4802 if (phy_mode == PHY_INTERFACE_MODE_QSGMII)
4803 mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_QSGMII_SERDES_PROTO);
4804 else if (phy_mode == PHY_INTERFACE_MODE_SGMII ||
4805 phy_interface_mode_is_8023z(phy_mode))
4806 mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_SGMII_SERDES_PROTO);
4807 else if (!phy_interface_mode_is_rgmii(phy_mode))
4813 /* Device initialization routine */
4814 static int mvneta_probe(struct platform_device *pdev)
4816 struct device_node *dn = pdev->dev.of_node;
4817 struct device_node *bm_node;
4818 struct mvneta_port *pp;
4819 struct net_device *dev;
4820 struct phylink *phylink;
4822 const char *dt_mac_addr;
4823 char hw_mac_addr[ETH_ALEN];
4824 phy_interface_t phy_mode;
4825 const char *mac_from;
4830 dev = devm_alloc_etherdev_mqs(&pdev->dev, sizeof(struct mvneta_port),
4831 txq_number, rxq_number);
4835 dev->irq = irq_of_parse_and_map(dn, 0);
4839 err = of_get_phy_mode(dn, &phy_mode);
4841 dev_err(&pdev->dev, "incorrect phy-mode\n");
4845 comphy = devm_of_phy_get(&pdev->dev, dn, NULL);
4846 if (comphy == ERR_PTR(-EPROBE_DEFER)) {
4847 err = -EPROBE_DEFER;
4849 } else if (IS_ERR(comphy)) {
4853 pp = netdev_priv(dev);
4854 spin_lock_init(&pp->lock);
4856 pp->phylink_config.dev = &dev->dev;
4857 pp->phylink_config.type = PHYLINK_NETDEV;
4859 phylink = phylink_create(&pp->phylink_config, pdev->dev.fwnode,
4860 phy_mode, &mvneta_phylink_ops);
4861 if (IS_ERR(phylink)) {
4862 err = PTR_ERR(phylink);
4866 dev->tx_queue_len = MVNETA_MAX_TXD;
4867 dev->watchdog_timeo = 5 * HZ;
4868 dev->netdev_ops = &mvneta_netdev_ops;
4870 dev->ethtool_ops = &mvneta_eth_tool_ops;
4872 pp->phylink = phylink;
4873 pp->comphy = comphy;
4874 pp->phy_interface = phy_mode;
4877 pp->rxq_def = rxq_def;
4878 pp->indir[0] = rxq_def;
4880 /* Get special SoC configurations */
4881 if (of_device_is_compatible(dn, "marvell,armada-3700-neta"))
4882 pp->neta_armada3700 = true;
4884 pp->clk = devm_clk_get(&pdev->dev, "core");
4885 if (IS_ERR(pp->clk))
4886 pp->clk = devm_clk_get(&pdev->dev, NULL);
4887 if (IS_ERR(pp->clk)) {
4888 err = PTR_ERR(pp->clk);
4889 goto err_free_phylink;
4892 clk_prepare_enable(pp->clk);
4894 pp->clk_bus = devm_clk_get(&pdev->dev, "bus");
4895 if (!IS_ERR(pp->clk_bus))
4896 clk_prepare_enable(pp->clk_bus);
4898 pp->base = devm_platform_ioremap_resource(pdev, 0);
4899 if (IS_ERR(pp->base)) {
4900 err = PTR_ERR(pp->base);
4904 /* Alloc per-cpu port structure */
4905 pp->ports = alloc_percpu(struct mvneta_pcpu_port);
4911 /* Alloc per-cpu stats */
4912 pp->stats = netdev_alloc_pcpu_stats(struct mvneta_pcpu_stats);
4915 goto err_free_ports;
4918 dt_mac_addr = of_get_mac_address(dn);
4919 if (!IS_ERR(dt_mac_addr)) {
4920 mac_from = "device tree";
4921 ether_addr_copy(dev->dev_addr, dt_mac_addr);
4923 mvneta_get_mac_addr(pp, hw_mac_addr);
4924 if (is_valid_ether_addr(hw_mac_addr)) {
4925 mac_from = "hardware";
4926 memcpy(dev->dev_addr, hw_mac_addr, ETH_ALEN);
4928 mac_from = "random";
4929 eth_hw_addr_random(dev);
4933 if (!of_property_read_u32(dn, "tx-csum-limit", &tx_csum_limit)) {
4934 if (tx_csum_limit < 0 ||
4935 tx_csum_limit > MVNETA_TX_CSUM_MAX_SIZE) {
4936 tx_csum_limit = MVNETA_TX_CSUM_DEF_SIZE;
4937 dev_info(&pdev->dev,
4938 "Wrong TX csum limit in DT, set to %dB\n",
4939 MVNETA_TX_CSUM_DEF_SIZE);
4941 } else if (of_device_is_compatible(dn, "marvell,armada-370-neta")) {
4942 tx_csum_limit = MVNETA_TX_CSUM_DEF_SIZE;
4944 tx_csum_limit = MVNETA_TX_CSUM_MAX_SIZE;
4947 pp->tx_csum_limit = tx_csum_limit;
4949 pp->dram_target_info = mv_mbus_dram_info();
4950 /* Armada3700 requires setting default configuration of Mbus
4951 * windows, however without using filled mbus_dram_target_info
4954 if (pp->dram_target_info || pp->neta_armada3700)
4955 mvneta_conf_mbus_windows(pp, pp->dram_target_info);
4957 pp->tx_ring_size = MVNETA_MAX_TXD;
4958 pp->rx_ring_size = MVNETA_MAX_RXD;
4961 SET_NETDEV_DEV(dev, &pdev->dev);
4963 pp->id = global_port_id++;
4965 /* Obtain access to BM resources if enabled and already initialized */
4966 bm_node = of_parse_phandle(dn, "buffer-manager", 0);
4968 pp->bm_priv = mvneta_bm_get(bm_node);
4970 err = mvneta_bm_port_init(pdev, pp);
4972 dev_info(&pdev->dev,
4973 "use SW buffer management\n");
4974 mvneta_bm_put(pp->bm_priv);
4978 /* Set RX packet offset correction for platforms, whose
4979 * NET_SKB_PAD, exceeds 64B. It should be 64B for 64-bit
4980 * platforms and 0B for 32-bit ones.
4982 pp->rx_offset_correction = max(0,
4984 MVNETA_RX_PKT_OFFSET_CORRECTION);
4986 of_node_put(bm_node);
4988 /* sw buffer management */
4990 pp->rx_offset_correction = MVNETA_SKB_HEADROOM;
4992 err = mvneta_init(&pdev->dev, pp);
4996 err = mvneta_port_power_up(pp, phy_mode);
4998 dev_err(&pdev->dev, "can't power up port\n");
5002 /* Armada3700 network controller does not support per-cpu
5003 * operation, so only single NAPI should be initialized.
5005 if (pp->neta_armada3700) {
5006 netif_napi_add(dev, &pp->napi, mvneta_poll, NAPI_POLL_WEIGHT);
5008 for_each_present_cpu(cpu) {
5009 struct mvneta_pcpu_port *port =
5010 per_cpu_ptr(pp->ports, cpu);
5012 netif_napi_add(dev, &port->napi, mvneta_poll,
5018 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
5019 NETIF_F_TSO | NETIF_F_RXCSUM;
5020 dev->hw_features |= dev->features;
5021 dev->vlan_features |= dev->features;
5022 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
5023 dev->gso_max_segs = MVNETA_MAX_TSO_SEGS;
5025 /* MTU range: 68 - 9676 */
5026 dev->min_mtu = ETH_MIN_MTU;
5027 /* 9676 == 9700 - 20 and rounding to 8 */
5028 dev->max_mtu = 9676;
5030 err = register_netdev(dev);
5032 dev_err(&pdev->dev, "failed to register\n");
5036 netdev_info(dev, "Using %s mac address %pM\n", mac_from,
5039 platform_set_drvdata(pdev, pp->dev);
5045 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
5046 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short,
5048 mvneta_bm_put(pp->bm_priv);
5050 free_percpu(pp->stats);
5052 free_percpu(pp->ports);
5054 clk_disable_unprepare(pp->clk_bus);
5055 clk_disable_unprepare(pp->clk);
5058 phylink_destroy(pp->phylink);
5060 irq_dispose_mapping(dev->irq);
5064 /* Device removal routine */
5065 static int mvneta_remove(struct platform_device *pdev)
5067 struct net_device *dev = platform_get_drvdata(pdev);
5068 struct mvneta_port *pp = netdev_priv(dev);
5070 unregister_netdev(dev);
5071 clk_disable_unprepare(pp->clk_bus);
5072 clk_disable_unprepare(pp->clk);
5073 free_percpu(pp->ports);
5074 free_percpu(pp->stats);
5075 irq_dispose_mapping(dev->irq);
5076 phylink_destroy(pp->phylink);
5079 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
5080 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short,
5082 mvneta_bm_put(pp->bm_priv);
5088 #ifdef CONFIG_PM_SLEEP
5089 static int mvneta_suspend(struct device *device)
5092 struct net_device *dev = dev_get_drvdata(device);
5093 struct mvneta_port *pp = netdev_priv(dev);
5095 if (!netif_running(dev))
5098 if (!pp->neta_armada3700) {
5099 spin_lock(&pp->lock);
5100 pp->is_stopped = true;
5101 spin_unlock(&pp->lock);
5103 cpuhp_state_remove_instance_nocalls(online_hpstate,
5105 cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
5110 mvneta_stop_dev(pp);
5113 for (queue = 0; queue < rxq_number; queue++) {
5114 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
5116 mvneta_rxq_drop_pkts(pp, rxq);
5119 for (queue = 0; queue < txq_number; queue++) {
5120 struct mvneta_tx_queue *txq = &pp->txqs[queue];
5122 mvneta_txq_hw_deinit(pp, txq);
5126 netif_device_detach(dev);
5127 clk_disable_unprepare(pp->clk_bus);
5128 clk_disable_unprepare(pp->clk);
5133 static int mvneta_resume(struct device *device)
5135 struct platform_device *pdev = to_platform_device(device);
5136 struct net_device *dev = dev_get_drvdata(device);
5137 struct mvneta_port *pp = netdev_priv(dev);
5140 clk_prepare_enable(pp->clk);
5141 if (!IS_ERR(pp->clk_bus))
5142 clk_prepare_enable(pp->clk_bus);
5143 if (pp->dram_target_info || pp->neta_armada3700)
5144 mvneta_conf_mbus_windows(pp, pp->dram_target_info);
5146 err = mvneta_bm_port_init(pdev, pp);
5148 dev_info(&pdev->dev, "use SW buffer management\n");
5149 pp->rx_offset_correction = MVNETA_SKB_HEADROOM;
5153 mvneta_defaults_set(pp);
5154 err = mvneta_port_power_up(pp, pp->phy_interface);
5156 dev_err(device, "can't power up port\n");
5160 netif_device_attach(dev);
5162 if (!netif_running(dev))
5165 for (queue = 0; queue < rxq_number; queue++) {
5166 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
5168 rxq->next_desc_to_proc = 0;
5169 mvneta_rxq_hw_init(pp, rxq);
5172 for (queue = 0; queue < txq_number; queue++) {
5173 struct mvneta_tx_queue *txq = &pp->txqs[queue];
5175 txq->next_desc_to_proc = 0;
5176 mvneta_txq_hw_init(pp, txq);
5179 if (!pp->neta_armada3700) {
5180 spin_lock(&pp->lock);
5181 pp->is_stopped = false;
5182 spin_unlock(&pp->lock);
5183 cpuhp_state_add_instance_nocalls(online_hpstate,
5185 cpuhp_state_add_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
5190 mvneta_start_dev(pp);
5192 mvneta_set_rx_mode(dev);
5198 static SIMPLE_DEV_PM_OPS(mvneta_pm_ops, mvneta_suspend, mvneta_resume);
5200 static const struct of_device_id mvneta_match[] = {
5201 { .compatible = "marvell,armada-370-neta" },
5202 { .compatible = "marvell,armada-xp-neta" },
5203 { .compatible = "marvell,armada-3700-neta" },
5206 MODULE_DEVICE_TABLE(of, mvneta_match);
5208 static struct platform_driver mvneta_driver = {
5209 .probe = mvneta_probe,
5210 .remove = mvneta_remove,
5212 .name = MVNETA_DRIVER_NAME,
5213 .of_match_table = mvneta_match,
5214 .pm = &mvneta_pm_ops,
5218 static int __init mvneta_driver_init(void)
5222 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "net/mvmeta:online",
5224 mvneta_cpu_down_prepare);
5227 online_hpstate = ret;
5228 ret = cpuhp_setup_state_multi(CPUHP_NET_MVNETA_DEAD, "net/mvneta:dead",
5229 NULL, mvneta_cpu_dead);
5233 ret = platform_driver_register(&mvneta_driver);
5239 cpuhp_remove_multi_state(CPUHP_NET_MVNETA_DEAD);
5241 cpuhp_remove_multi_state(online_hpstate);
5245 module_init(mvneta_driver_init);
5247 static void __exit mvneta_driver_exit(void)
5249 platform_driver_unregister(&mvneta_driver);
5250 cpuhp_remove_multi_state(CPUHP_NET_MVNETA_DEAD);
5251 cpuhp_remove_multi_state(online_hpstate);
5253 module_exit(mvneta_driver_exit);
5255 MODULE_DESCRIPTION("Marvell NETA Ethernet Driver - www.marvell.com");
5257 MODULE_LICENSE("GPL");
5259 module_param(rxq_number, int, 0444);
5260 module_param(txq_number, int, 0444);
5262 module_param(rxq_def, int, 0444);
5263 module_param(rx_copybreak, int, 0644);