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.h>
31 #include <linux/phylink.h>
32 #include <linux/platform_device.h>
33 #include <linux/skbuff.h>
35 #include "mvneta_bm.h"
41 #define MVNETA_RXQ_CONFIG_REG(q) (0x1400 + ((q) << 2))
42 #define MVNETA_RXQ_HW_BUF_ALLOC BIT(0)
43 #define MVNETA_RXQ_SHORT_POOL_ID_SHIFT 4
44 #define MVNETA_RXQ_SHORT_POOL_ID_MASK 0x30
45 #define MVNETA_RXQ_LONG_POOL_ID_SHIFT 6
46 #define MVNETA_RXQ_LONG_POOL_ID_MASK 0xc0
47 #define MVNETA_RXQ_PKT_OFFSET_ALL_MASK (0xf << 8)
48 #define MVNETA_RXQ_PKT_OFFSET_MASK(offs) ((offs) << 8)
49 #define MVNETA_RXQ_THRESHOLD_REG(q) (0x14c0 + ((q) << 2))
50 #define MVNETA_RXQ_NON_OCCUPIED(v) ((v) << 16)
51 #define MVNETA_RXQ_BASE_ADDR_REG(q) (0x1480 + ((q) << 2))
52 #define MVNETA_RXQ_SIZE_REG(q) (0x14a0 + ((q) << 2))
53 #define MVNETA_RXQ_BUF_SIZE_SHIFT 19
54 #define MVNETA_RXQ_BUF_SIZE_MASK (0x1fff << 19)
55 #define MVNETA_RXQ_STATUS_REG(q) (0x14e0 + ((q) << 2))
56 #define MVNETA_RXQ_OCCUPIED_ALL_MASK 0x3fff
57 #define MVNETA_RXQ_STATUS_UPDATE_REG(q) (0x1500 + ((q) << 2))
58 #define MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT 16
59 #define MVNETA_RXQ_ADD_NON_OCCUPIED_MAX 255
60 #define MVNETA_PORT_POOL_BUFFER_SZ_REG(pool) (0x1700 + ((pool) << 2))
61 #define MVNETA_PORT_POOL_BUFFER_SZ_SHIFT 3
62 #define MVNETA_PORT_POOL_BUFFER_SZ_MASK 0xfff8
63 #define MVNETA_PORT_RX_RESET 0x1cc0
64 #define MVNETA_PORT_RX_DMA_RESET BIT(0)
65 #define MVNETA_PHY_ADDR 0x2000
66 #define MVNETA_PHY_ADDR_MASK 0x1f
67 #define MVNETA_MBUS_RETRY 0x2010
68 #define MVNETA_UNIT_INTR_CAUSE 0x2080
69 #define MVNETA_UNIT_CONTROL 0x20B0
70 #define MVNETA_PHY_POLLING_ENABLE BIT(1)
71 #define MVNETA_WIN_BASE(w) (0x2200 + ((w) << 3))
72 #define MVNETA_WIN_SIZE(w) (0x2204 + ((w) << 3))
73 #define MVNETA_WIN_REMAP(w) (0x2280 + ((w) << 2))
74 #define MVNETA_BASE_ADDR_ENABLE 0x2290
75 #define MVNETA_ACCESS_PROTECT_ENABLE 0x2294
76 #define MVNETA_PORT_CONFIG 0x2400
77 #define MVNETA_UNI_PROMISC_MODE BIT(0)
78 #define MVNETA_DEF_RXQ(q) ((q) << 1)
79 #define MVNETA_DEF_RXQ_ARP(q) ((q) << 4)
80 #define MVNETA_TX_UNSET_ERR_SUM BIT(12)
81 #define MVNETA_DEF_RXQ_TCP(q) ((q) << 16)
82 #define MVNETA_DEF_RXQ_UDP(q) ((q) << 19)
83 #define MVNETA_DEF_RXQ_BPDU(q) ((q) << 22)
84 #define MVNETA_RX_CSUM_WITH_PSEUDO_HDR BIT(25)
85 #define MVNETA_PORT_CONFIG_DEFL_VALUE(q) (MVNETA_DEF_RXQ(q) | \
86 MVNETA_DEF_RXQ_ARP(q) | \
87 MVNETA_DEF_RXQ_TCP(q) | \
88 MVNETA_DEF_RXQ_UDP(q) | \
89 MVNETA_DEF_RXQ_BPDU(q) | \
90 MVNETA_TX_UNSET_ERR_SUM | \
91 MVNETA_RX_CSUM_WITH_PSEUDO_HDR)
92 #define MVNETA_PORT_CONFIG_EXTEND 0x2404
93 #define MVNETA_MAC_ADDR_LOW 0x2414
94 #define MVNETA_MAC_ADDR_HIGH 0x2418
95 #define MVNETA_SDMA_CONFIG 0x241c
96 #define MVNETA_SDMA_BRST_SIZE_16 4
97 #define MVNETA_RX_BRST_SZ_MASK(burst) ((burst) << 1)
98 #define MVNETA_RX_NO_DATA_SWAP BIT(4)
99 #define MVNETA_TX_NO_DATA_SWAP BIT(5)
100 #define MVNETA_DESC_SWAP BIT(6)
101 #define MVNETA_TX_BRST_SZ_MASK(burst) ((burst) << 22)
102 #define MVNETA_PORT_STATUS 0x2444
103 #define MVNETA_TX_IN_PRGRS BIT(1)
104 #define MVNETA_TX_FIFO_EMPTY BIT(8)
105 #define MVNETA_RX_MIN_FRAME_SIZE 0x247c
106 #define MVNETA_SERDES_CFG 0x24A0
107 #define MVNETA_SGMII_SERDES_PROTO 0x0cc7
108 #define MVNETA_QSGMII_SERDES_PROTO 0x0667
109 #define MVNETA_TYPE_PRIO 0x24bc
110 #define MVNETA_FORCE_UNI BIT(21)
111 #define MVNETA_TXQ_CMD_1 0x24e4
112 #define MVNETA_TXQ_CMD 0x2448
113 #define MVNETA_TXQ_DISABLE_SHIFT 8
114 #define MVNETA_TXQ_ENABLE_MASK 0x000000ff
115 #define MVNETA_RX_DISCARD_FRAME_COUNT 0x2484
116 #define MVNETA_OVERRUN_FRAME_COUNT 0x2488
117 #define MVNETA_GMAC_CLOCK_DIVIDER 0x24f4
118 #define MVNETA_GMAC_1MS_CLOCK_ENABLE BIT(31)
119 #define MVNETA_ACC_MODE 0x2500
120 #define MVNETA_BM_ADDRESS 0x2504
121 #define MVNETA_CPU_MAP(cpu) (0x2540 + ((cpu) << 2))
122 #define MVNETA_CPU_RXQ_ACCESS_ALL_MASK 0x000000ff
123 #define MVNETA_CPU_TXQ_ACCESS_ALL_MASK 0x0000ff00
124 #define MVNETA_CPU_RXQ_ACCESS(rxq) BIT(rxq)
125 #define MVNETA_CPU_TXQ_ACCESS(txq) BIT(txq + 8)
126 #define MVNETA_RXQ_TIME_COAL_REG(q) (0x2580 + ((q) << 2))
128 /* Exception Interrupt Port/Queue Cause register
130 * Their behavior depend of the mapping done using the PCPX2Q
131 * registers. For a given CPU if the bit associated to a queue is not
132 * set, then for the register a read from this CPU will always return
133 * 0 and a write won't do anything
136 #define MVNETA_INTR_NEW_CAUSE 0x25a0
137 #define MVNETA_INTR_NEW_MASK 0x25a4
139 /* bits 0..7 = TXQ SENT, one bit per queue.
140 * bits 8..15 = RXQ OCCUP, one bit per queue.
141 * bits 16..23 = RXQ FREE, one bit per queue.
142 * bit 29 = OLD_REG_SUM, see old reg ?
143 * bit 30 = TX_ERR_SUM, one bit for 4 ports
144 * bit 31 = MISC_SUM, one bit for 4 ports
146 #define MVNETA_TX_INTR_MASK(nr_txqs) (((1 << nr_txqs) - 1) << 0)
147 #define MVNETA_TX_INTR_MASK_ALL (0xff << 0)
148 #define MVNETA_RX_INTR_MASK(nr_rxqs) (((1 << nr_rxqs) - 1) << 8)
149 #define MVNETA_RX_INTR_MASK_ALL (0xff << 8)
150 #define MVNETA_MISCINTR_INTR_MASK BIT(31)
152 #define MVNETA_INTR_OLD_CAUSE 0x25a8
153 #define MVNETA_INTR_OLD_MASK 0x25ac
155 /* Data Path Port/Queue Cause Register */
156 #define MVNETA_INTR_MISC_CAUSE 0x25b0
157 #define MVNETA_INTR_MISC_MASK 0x25b4
159 #define MVNETA_CAUSE_PHY_STATUS_CHANGE BIT(0)
160 #define MVNETA_CAUSE_LINK_CHANGE BIT(1)
161 #define MVNETA_CAUSE_PTP BIT(4)
163 #define MVNETA_CAUSE_INTERNAL_ADDR_ERR BIT(7)
164 #define MVNETA_CAUSE_RX_OVERRUN BIT(8)
165 #define MVNETA_CAUSE_RX_CRC_ERROR BIT(9)
166 #define MVNETA_CAUSE_RX_LARGE_PKT BIT(10)
167 #define MVNETA_CAUSE_TX_UNDERUN BIT(11)
168 #define MVNETA_CAUSE_PRBS_ERR BIT(12)
169 #define MVNETA_CAUSE_PSC_SYNC_CHANGE BIT(13)
170 #define MVNETA_CAUSE_SERDES_SYNC_ERR BIT(14)
172 #define MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT 16
173 #define MVNETA_CAUSE_BMU_ALLOC_ERR_ALL_MASK (0xF << MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT)
174 #define MVNETA_CAUSE_BMU_ALLOC_ERR_MASK(pool) (1 << (MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT + (pool)))
176 #define MVNETA_CAUSE_TXQ_ERROR_SHIFT 24
177 #define MVNETA_CAUSE_TXQ_ERROR_ALL_MASK (0xFF << MVNETA_CAUSE_TXQ_ERROR_SHIFT)
178 #define MVNETA_CAUSE_TXQ_ERROR_MASK(q) (1 << (MVNETA_CAUSE_TXQ_ERROR_SHIFT + (q)))
180 #define MVNETA_INTR_ENABLE 0x25b8
181 #define MVNETA_TXQ_INTR_ENABLE_ALL_MASK 0x0000ff00
182 #define MVNETA_RXQ_INTR_ENABLE_ALL_MASK 0x000000ff
184 #define MVNETA_RXQ_CMD 0x2680
185 #define MVNETA_RXQ_DISABLE_SHIFT 8
186 #define MVNETA_RXQ_ENABLE_MASK 0x000000ff
187 #define MVETH_TXQ_TOKEN_COUNT_REG(q) (0x2700 + ((q) << 4))
188 #define MVETH_TXQ_TOKEN_CFG_REG(q) (0x2704 + ((q) << 4))
189 #define MVNETA_GMAC_CTRL_0 0x2c00
190 #define MVNETA_GMAC_MAX_RX_SIZE_SHIFT 2
191 #define MVNETA_GMAC_MAX_RX_SIZE_MASK 0x7ffc
192 #define MVNETA_GMAC0_PORT_1000BASE_X BIT(1)
193 #define MVNETA_GMAC0_PORT_ENABLE BIT(0)
194 #define MVNETA_GMAC_CTRL_2 0x2c08
195 #define MVNETA_GMAC2_INBAND_AN_ENABLE BIT(0)
196 #define MVNETA_GMAC2_PCS_ENABLE BIT(3)
197 #define MVNETA_GMAC2_PORT_RGMII BIT(4)
198 #define MVNETA_GMAC2_PORT_RESET BIT(6)
199 #define MVNETA_GMAC_STATUS 0x2c10
200 #define MVNETA_GMAC_LINK_UP BIT(0)
201 #define MVNETA_GMAC_SPEED_1000 BIT(1)
202 #define MVNETA_GMAC_SPEED_100 BIT(2)
203 #define MVNETA_GMAC_FULL_DUPLEX BIT(3)
204 #define MVNETA_GMAC_RX_FLOW_CTRL_ENABLE BIT(4)
205 #define MVNETA_GMAC_TX_FLOW_CTRL_ENABLE BIT(5)
206 #define MVNETA_GMAC_RX_FLOW_CTRL_ACTIVE BIT(6)
207 #define MVNETA_GMAC_TX_FLOW_CTRL_ACTIVE BIT(7)
208 #define MVNETA_GMAC_AN_COMPLETE BIT(11)
209 #define MVNETA_GMAC_SYNC_OK BIT(14)
210 #define MVNETA_GMAC_AUTONEG_CONFIG 0x2c0c
211 #define MVNETA_GMAC_FORCE_LINK_DOWN BIT(0)
212 #define MVNETA_GMAC_FORCE_LINK_PASS BIT(1)
213 #define MVNETA_GMAC_INBAND_AN_ENABLE BIT(2)
214 #define MVNETA_GMAC_AN_BYPASS_ENABLE BIT(3)
215 #define MVNETA_GMAC_INBAND_RESTART_AN BIT(4)
216 #define MVNETA_GMAC_CONFIG_MII_SPEED BIT(5)
217 #define MVNETA_GMAC_CONFIG_GMII_SPEED BIT(6)
218 #define MVNETA_GMAC_AN_SPEED_EN BIT(7)
219 #define MVNETA_GMAC_CONFIG_FLOW_CTRL BIT(8)
220 #define MVNETA_GMAC_ADVERT_SYM_FLOW_CTRL BIT(9)
221 #define MVNETA_GMAC_AN_FLOW_CTRL_EN BIT(11)
222 #define MVNETA_GMAC_CONFIG_FULL_DUPLEX BIT(12)
223 #define MVNETA_GMAC_AN_DUPLEX_EN BIT(13)
224 #define MVNETA_GMAC_CTRL_4 0x2c90
225 #define MVNETA_GMAC4_SHORT_PREAMBLE_ENABLE BIT(1)
226 #define MVNETA_MIB_COUNTERS_BASE 0x3000
227 #define MVNETA_MIB_LATE_COLLISION 0x7c
228 #define MVNETA_DA_FILT_SPEC_MCAST 0x3400
229 #define MVNETA_DA_FILT_OTH_MCAST 0x3500
230 #define MVNETA_DA_FILT_UCAST_BASE 0x3600
231 #define MVNETA_TXQ_BASE_ADDR_REG(q) (0x3c00 + ((q) << 2))
232 #define MVNETA_TXQ_SIZE_REG(q) (0x3c20 + ((q) << 2))
233 #define MVNETA_TXQ_SENT_THRESH_ALL_MASK 0x3fff0000
234 #define MVNETA_TXQ_SENT_THRESH_MASK(coal) ((coal) << 16)
235 #define MVNETA_TXQ_UPDATE_REG(q) (0x3c60 + ((q) << 2))
236 #define MVNETA_TXQ_DEC_SENT_SHIFT 16
237 #define MVNETA_TXQ_DEC_SENT_MASK 0xff
238 #define MVNETA_TXQ_STATUS_REG(q) (0x3c40 + ((q) << 2))
239 #define MVNETA_TXQ_SENT_DESC_SHIFT 16
240 #define MVNETA_TXQ_SENT_DESC_MASK 0x3fff0000
241 #define MVNETA_PORT_TX_RESET 0x3cf0
242 #define MVNETA_PORT_TX_DMA_RESET BIT(0)
243 #define MVNETA_TX_MTU 0x3e0c
244 #define MVNETA_TX_TOKEN_SIZE 0x3e14
245 #define MVNETA_TX_TOKEN_SIZE_MAX 0xffffffff
246 #define MVNETA_TXQ_TOKEN_SIZE_REG(q) (0x3e40 + ((q) << 2))
247 #define MVNETA_TXQ_TOKEN_SIZE_MAX 0x7fffffff
249 #define MVNETA_LPI_CTRL_0 0x2cc0
250 #define MVNETA_LPI_CTRL_1 0x2cc4
251 #define MVNETA_LPI_REQUEST_ENABLE BIT(0)
252 #define MVNETA_LPI_CTRL_2 0x2cc8
253 #define MVNETA_LPI_STATUS 0x2ccc
255 #define MVNETA_CAUSE_TXQ_SENT_DESC_ALL_MASK 0xff
257 /* Descriptor ring Macros */
258 #define MVNETA_QUEUE_NEXT_DESC(q, index) \
259 (((index) < (q)->last_desc) ? ((index) + 1) : 0)
261 /* Various constants */
264 #define MVNETA_TXDONE_COAL_PKTS 0 /* interrupt per packet */
265 #define MVNETA_RX_COAL_PKTS 32
266 #define MVNETA_RX_COAL_USEC 100
268 /* The two bytes Marvell header. Either contains a special value used
269 * by Marvell switches when a specific hardware mode is enabled (not
270 * supported by this driver) or is filled automatically by zeroes on
271 * the RX side. Those two bytes being at the front of the Ethernet
272 * header, they allow to have the IP header aligned on a 4 bytes
273 * boundary automatically: the hardware skips those two bytes on its
276 #define MVNETA_MH_SIZE 2
278 #define MVNETA_VLAN_TAG_LEN 4
280 #define MVNETA_TX_CSUM_DEF_SIZE 1600
281 #define MVNETA_TX_CSUM_MAX_SIZE 9800
282 #define MVNETA_ACC_MODE_EXT1 1
283 #define MVNETA_ACC_MODE_EXT2 2
285 #define MVNETA_MAX_DECODE_WIN 6
287 /* Timeout constants */
288 #define MVNETA_TX_DISABLE_TIMEOUT_MSEC 1000
289 #define MVNETA_RX_DISABLE_TIMEOUT_MSEC 1000
290 #define MVNETA_TX_FIFO_EMPTY_TIMEOUT 10000
292 #define MVNETA_TX_MTU_MAX 0x3ffff
294 /* The RSS lookup table actually has 256 entries but we do not use
297 #define MVNETA_RSS_LU_TABLE_SIZE 1
299 /* Max number of Rx descriptors */
300 #define MVNETA_MAX_RXD 512
302 /* Max number of Tx descriptors */
303 #define MVNETA_MAX_TXD 1024
305 /* Max number of allowed TCP segments for software TSO */
306 #define MVNETA_MAX_TSO_SEGS 100
308 #define MVNETA_MAX_SKB_DESCS (MVNETA_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
310 /* descriptor aligned size */
311 #define MVNETA_DESC_ALIGNED_SIZE 32
313 /* Number of bytes to be taken into account by HW when putting incoming data
314 * to the buffers. It is needed in case NET_SKB_PAD exceeds maximum packet
315 * offset supported in MVNETA_RXQ_CONFIG_REG(q) registers.
317 #define MVNETA_RX_PKT_OFFSET_CORRECTION 64
319 #define MVNETA_RX_PKT_SIZE(mtu) \
320 ALIGN((mtu) + MVNETA_MH_SIZE + MVNETA_VLAN_TAG_LEN + \
321 ETH_HLEN + ETH_FCS_LEN, \
324 #define IS_TSO_HEADER(txq, addr) \
325 ((addr >= txq->tso_hdrs_phys) && \
326 (addr < txq->tso_hdrs_phys + txq->size * TSO_HEADER_SIZE))
328 #define MVNETA_RX_GET_BM_POOL_ID(rxd) \
329 (((rxd)->status & MVNETA_RXD_BM_POOL_MASK) >> MVNETA_RXD_BM_POOL_SHIFT)
332 ETHTOOL_STAT_EEE_WAKEUP,
333 ETHTOOL_STAT_SKB_ALLOC_ERR,
334 ETHTOOL_STAT_REFILL_ERR,
338 struct mvneta_statistic {
339 unsigned short offset;
341 const char name[ETH_GSTRING_LEN];
348 static const struct mvneta_statistic mvneta_statistics[] = {
349 { 0x3000, T_REG_64, "good_octets_received", },
350 { 0x3010, T_REG_32, "good_frames_received", },
351 { 0x3008, T_REG_32, "bad_octets_received", },
352 { 0x3014, T_REG_32, "bad_frames_received", },
353 { 0x3018, T_REG_32, "broadcast_frames_received", },
354 { 0x301c, T_REG_32, "multicast_frames_received", },
355 { 0x3050, T_REG_32, "unrec_mac_control_received", },
356 { 0x3058, T_REG_32, "good_fc_received", },
357 { 0x305c, T_REG_32, "bad_fc_received", },
358 { 0x3060, T_REG_32, "undersize_received", },
359 { 0x3064, T_REG_32, "fragments_received", },
360 { 0x3068, T_REG_32, "oversize_received", },
361 { 0x306c, T_REG_32, "jabber_received", },
362 { 0x3070, T_REG_32, "mac_receive_error", },
363 { 0x3074, T_REG_32, "bad_crc_event", },
364 { 0x3078, T_REG_32, "collision", },
365 { 0x307c, T_REG_32, "late_collision", },
366 { 0x2484, T_REG_32, "rx_discard", },
367 { 0x2488, T_REG_32, "rx_overrun", },
368 { 0x3020, T_REG_32, "frames_64_octets", },
369 { 0x3024, T_REG_32, "frames_65_to_127_octets", },
370 { 0x3028, T_REG_32, "frames_128_to_255_octets", },
371 { 0x302c, T_REG_32, "frames_256_to_511_octets", },
372 { 0x3030, T_REG_32, "frames_512_to_1023_octets", },
373 { 0x3034, T_REG_32, "frames_1024_to_max_octets", },
374 { 0x3038, T_REG_64, "good_octets_sent", },
375 { 0x3040, T_REG_32, "good_frames_sent", },
376 { 0x3044, T_REG_32, "excessive_collision", },
377 { 0x3048, T_REG_32, "multicast_frames_sent", },
378 { 0x304c, T_REG_32, "broadcast_frames_sent", },
379 { 0x3054, T_REG_32, "fc_sent", },
380 { 0x300c, T_REG_32, "internal_mac_transmit_err", },
381 { ETHTOOL_STAT_EEE_WAKEUP, T_SW, "eee_wakeup_errors", },
382 { ETHTOOL_STAT_SKB_ALLOC_ERR, T_SW, "skb_alloc_errors", },
383 { ETHTOOL_STAT_REFILL_ERR, T_SW, "refill_errors", },
386 struct mvneta_pcpu_stats {
387 struct u64_stats_sync syncp;
394 struct mvneta_pcpu_port {
395 /* Pointer to the shared port */
396 struct mvneta_port *pp;
398 /* Pointer to the CPU-local NAPI struct */
399 struct napi_struct napi;
401 /* Cause of the previous interrupt */
407 struct mvneta_pcpu_port __percpu *ports;
408 struct mvneta_pcpu_stats __percpu *stats;
411 unsigned int frag_size;
413 struct mvneta_rx_queue *rxqs;
414 struct mvneta_tx_queue *txqs;
415 struct net_device *dev;
416 struct hlist_node node_online;
417 struct hlist_node node_dead;
419 /* Protect the access to the percpu interrupt registers,
420 * ensuring that the configuration remains coherent.
426 struct napi_struct napi;
436 phy_interface_t phy_interface;
437 struct device_node *dn;
438 unsigned int tx_csum_limit;
439 struct phylink *phylink;
441 struct mvneta_bm *bm_priv;
442 struct mvneta_bm_pool *pool_long;
443 struct mvneta_bm_pool *pool_short;
450 u64 ethtool_stats[ARRAY_SIZE(mvneta_statistics)];
452 u32 indir[MVNETA_RSS_LU_TABLE_SIZE];
454 /* Flags for special SoC configurations */
455 bool neta_armada3700;
456 u16 rx_offset_correction;
457 const struct mbus_dram_target_info *dram_target_info;
460 /* The mvneta_tx_desc and mvneta_rx_desc structures describe the
461 * layout of the transmit and reception DMA descriptors, and their
462 * layout is therefore defined by the hardware design
465 #define MVNETA_TX_L3_OFF_SHIFT 0
466 #define MVNETA_TX_IP_HLEN_SHIFT 8
467 #define MVNETA_TX_L4_UDP BIT(16)
468 #define MVNETA_TX_L3_IP6 BIT(17)
469 #define MVNETA_TXD_IP_CSUM BIT(18)
470 #define MVNETA_TXD_Z_PAD BIT(19)
471 #define MVNETA_TXD_L_DESC BIT(20)
472 #define MVNETA_TXD_F_DESC BIT(21)
473 #define MVNETA_TXD_FLZ_DESC (MVNETA_TXD_Z_PAD | \
474 MVNETA_TXD_L_DESC | \
476 #define MVNETA_TX_L4_CSUM_FULL BIT(30)
477 #define MVNETA_TX_L4_CSUM_NOT BIT(31)
479 #define MVNETA_RXD_ERR_CRC 0x0
480 #define MVNETA_RXD_BM_POOL_SHIFT 13
481 #define MVNETA_RXD_BM_POOL_MASK (BIT(13) | BIT(14))
482 #define MVNETA_RXD_ERR_SUMMARY BIT(16)
483 #define MVNETA_RXD_ERR_OVERRUN BIT(17)
484 #define MVNETA_RXD_ERR_LEN BIT(18)
485 #define MVNETA_RXD_ERR_RESOURCE (BIT(17) | BIT(18))
486 #define MVNETA_RXD_ERR_CODE_MASK (BIT(17) | BIT(18))
487 #define MVNETA_RXD_L3_IP4 BIT(25)
488 #define MVNETA_RXD_LAST_DESC BIT(26)
489 #define MVNETA_RXD_FIRST_DESC BIT(27)
490 #define MVNETA_RXD_FIRST_LAST_DESC (MVNETA_RXD_FIRST_DESC | \
491 MVNETA_RXD_LAST_DESC)
492 #define MVNETA_RXD_L4_CSUM_OK BIT(30)
494 #if defined(__LITTLE_ENDIAN)
495 struct mvneta_tx_desc {
496 u32 command; /* Options used by HW for packet transmitting.*/
497 u16 reserved1; /* csum_l4 (for future use) */
498 u16 data_size; /* Data size of transmitted packet in bytes */
499 u32 buf_phys_addr; /* Physical addr of transmitted buffer */
500 u32 reserved2; /* hw_cmd - (for future use, PMT) */
501 u32 reserved3[4]; /* Reserved - (for future use) */
504 struct mvneta_rx_desc {
505 u32 status; /* Info about received packet */
506 u16 reserved1; /* pnc_info - (for future use, PnC) */
507 u16 data_size; /* Size of received packet in bytes */
509 u32 buf_phys_addr; /* Physical address of the buffer */
510 u32 reserved2; /* pnc_flow_id (for future use, PnC) */
512 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
513 u16 reserved3; /* prefetch_cmd, for future use */
514 u16 reserved4; /* csum_l4 - (for future use, PnC) */
516 u32 reserved5; /* pnc_extra PnC (for future use, PnC) */
517 u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */
520 struct mvneta_tx_desc {
521 u16 data_size; /* Data size of transmitted packet in bytes */
522 u16 reserved1; /* csum_l4 (for future use) */
523 u32 command; /* Options used by HW for packet transmitting.*/
524 u32 reserved2; /* hw_cmd - (for future use, PMT) */
525 u32 buf_phys_addr; /* Physical addr of transmitted buffer */
526 u32 reserved3[4]; /* Reserved - (for future use) */
529 struct mvneta_rx_desc {
530 u16 data_size; /* Size of received packet in bytes */
531 u16 reserved1; /* pnc_info - (for future use, PnC) */
532 u32 status; /* Info about received packet */
534 u32 reserved2; /* pnc_flow_id (for future use, PnC) */
535 u32 buf_phys_addr; /* Physical address of the buffer */
537 u16 reserved4; /* csum_l4 - (for future use, PnC) */
538 u16 reserved3; /* prefetch_cmd, for future use */
539 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
541 u32 reserved5; /* pnc_extra PnC (for future use, PnC) */
542 u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */
546 struct mvneta_tx_queue {
547 /* Number of this TX queue, in the range 0-7 */
550 /* Number of TX DMA descriptors in the descriptor ring */
553 /* Number of currently used TX DMA descriptor in the
558 int tx_stop_threshold;
559 int tx_wake_threshold;
561 /* Array of transmitted skb */
562 struct sk_buff **tx_skb;
564 /* Index of last TX DMA descriptor that was inserted */
567 /* Index of the TX DMA descriptor to be cleaned up */
572 /* Virtual address of the TX DMA descriptors array */
573 struct mvneta_tx_desc *descs;
575 /* DMA address of the TX DMA descriptors array */
576 dma_addr_t descs_phys;
578 /* Index of the last TX DMA descriptor */
581 /* Index of the next TX DMA descriptor to process */
582 int next_desc_to_proc;
584 /* DMA buffers for TSO headers */
587 /* DMA address of TSO headers */
588 dma_addr_t tso_hdrs_phys;
590 /* Affinity mask for CPUs*/
591 cpumask_t affinity_mask;
594 struct mvneta_rx_queue {
595 /* rx queue number, in the range 0-7 */
598 /* num of rx descriptors in the rx descriptor ring */
604 /* Virtual address of the RX buffer */
605 void **buf_virt_addr;
607 /* Virtual address of the RX DMA descriptors array */
608 struct mvneta_rx_desc *descs;
610 /* DMA address of the RX DMA descriptors array */
611 dma_addr_t descs_phys;
613 /* Index of the last RX DMA descriptor */
616 /* Index of the next RX DMA descriptor to process */
617 int next_desc_to_proc;
619 /* Index of first RX DMA descriptor to refill */
623 /* pointer to uncomplete skb buffer */
632 static enum cpuhp_state online_hpstate;
633 /* The hardware supports eight (8) rx queues, but we are only allowing
634 * the first one to be used. Therefore, let's just allocate one queue.
636 static int rxq_number = 8;
637 static int txq_number = 8;
641 static int rx_copybreak __read_mostly = 256;
642 static int rx_header_size __read_mostly = 128;
644 /* HW BM need that each port be identify by a unique ID */
645 static int global_port_id;
647 #define MVNETA_DRIVER_NAME "mvneta"
648 #define MVNETA_DRIVER_VERSION "1.0"
650 /* Utility/helper methods */
652 /* Write helper method */
653 static void mvreg_write(struct mvneta_port *pp, u32 offset, u32 data)
655 writel(data, pp->base + offset);
658 /* Read helper method */
659 static u32 mvreg_read(struct mvneta_port *pp, u32 offset)
661 return readl(pp->base + offset);
664 /* Increment txq get counter */
665 static void mvneta_txq_inc_get(struct mvneta_tx_queue *txq)
667 txq->txq_get_index++;
668 if (txq->txq_get_index == txq->size)
669 txq->txq_get_index = 0;
672 /* Increment txq put counter */
673 static void mvneta_txq_inc_put(struct mvneta_tx_queue *txq)
675 txq->txq_put_index++;
676 if (txq->txq_put_index == txq->size)
677 txq->txq_put_index = 0;
681 /* Clear all MIB counters */
682 static void mvneta_mib_counters_clear(struct mvneta_port *pp)
687 /* Perform dummy reads from MIB counters */
688 for (i = 0; i < MVNETA_MIB_LATE_COLLISION; i += 4)
689 dummy = mvreg_read(pp, (MVNETA_MIB_COUNTERS_BASE + i));
690 dummy = mvreg_read(pp, MVNETA_RX_DISCARD_FRAME_COUNT);
691 dummy = mvreg_read(pp, MVNETA_OVERRUN_FRAME_COUNT);
694 /* Get System Network Statistics */
696 mvneta_get_stats64(struct net_device *dev,
697 struct rtnl_link_stats64 *stats)
699 struct mvneta_port *pp = netdev_priv(dev);
703 for_each_possible_cpu(cpu) {
704 struct mvneta_pcpu_stats *cpu_stats;
710 cpu_stats = per_cpu_ptr(pp->stats, cpu);
712 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
713 rx_packets = cpu_stats->rx_packets;
714 rx_bytes = cpu_stats->rx_bytes;
715 tx_packets = cpu_stats->tx_packets;
716 tx_bytes = cpu_stats->tx_bytes;
717 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
719 stats->rx_packets += rx_packets;
720 stats->rx_bytes += rx_bytes;
721 stats->tx_packets += tx_packets;
722 stats->tx_bytes += tx_bytes;
725 stats->rx_errors = dev->stats.rx_errors;
726 stats->rx_dropped = dev->stats.rx_dropped;
728 stats->tx_dropped = dev->stats.tx_dropped;
731 /* Rx descriptors helper methods */
733 /* Checks whether the RX descriptor having this status is both the first
734 * and the last descriptor for the RX packet. Each RX packet is currently
735 * received through a single RX descriptor, so not having each RX
736 * descriptor with its first and last bits set is an error
738 static int mvneta_rxq_desc_is_first_last(u32 status)
740 return (status & MVNETA_RXD_FIRST_LAST_DESC) ==
741 MVNETA_RXD_FIRST_LAST_DESC;
744 /* Add number of descriptors ready to receive new packets */
745 static void mvneta_rxq_non_occup_desc_add(struct mvneta_port *pp,
746 struct mvneta_rx_queue *rxq,
749 /* Only MVNETA_RXQ_ADD_NON_OCCUPIED_MAX (255) descriptors can
752 while (ndescs > MVNETA_RXQ_ADD_NON_OCCUPIED_MAX) {
753 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
754 (MVNETA_RXQ_ADD_NON_OCCUPIED_MAX <<
755 MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
756 ndescs -= MVNETA_RXQ_ADD_NON_OCCUPIED_MAX;
759 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
760 (ndescs << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
763 /* Get number of RX descriptors occupied by received packets */
764 static int mvneta_rxq_busy_desc_num_get(struct mvneta_port *pp,
765 struct mvneta_rx_queue *rxq)
769 val = mvreg_read(pp, MVNETA_RXQ_STATUS_REG(rxq->id));
770 return val & MVNETA_RXQ_OCCUPIED_ALL_MASK;
773 /* Update num of rx desc called upon return from rx path or
774 * from mvneta_rxq_drop_pkts().
776 static void mvneta_rxq_desc_num_update(struct mvneta_port *pp,
777 struct mvneta_rx_queue *rxq,
778 int rx_done, int rx_filled)
782 if ((rx_done <= 0xff) && (rx_filled <= 0xff)) {
784 (rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT);
785 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
789 /* Only 255 descriptors can be added at once */
790 while ((rx_done > 0) || (rx_filled > 0)) {
791 if (rx_done <= 0xff) {
798 if (rx_filled <= 0xff) {
799 val |= rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
802 val |= 0xff << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
805 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
809 /* Get pointer to next RX descriptor to be processed by SW */
810 static struct mvneta_rx_desc *
811 mvneta_rxq_next_desc_get(struct mvneta_rx_queue *rxq)
813 int rx_desc = rxq->next_desc_to_proc;
815 rxq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(rxq, rx_desc);
816 prefetch(rxq->descs + rxq->next_desc_to_proc);
817 return rxq->descs + rx_desc;
820 /* Change maximum receive size of the port. */
821 static void mvneta_max_rx_size_set(struct mvneta_port *pp, int max_rx_size)
825 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
826 val &= ~MVNETA_GMAC_MAX_RX_SIZE_MASK;
827 val |= ((max_rx_size - MVNETA_MH_SIZE) / 2) <<
828 MVNETA_GMAC_MAX_RX_SIZE_SHIFT;
829 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
833 /* Set rx queue offset */
834 static void mvneta_rxq_offset_set(struct mvneta_port *pp,
835 struct mvneta_rx_queue *rxq,
840 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
841 val &= ~MVNETA_RXQ_PKT_OFFSET_ALL_MASK;
844 val |= MVNETA_RXQ_PKT_OFFSET_MASK(offset >> 3);
845 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
849 /* Tx descriptors helper methods */
851 /* Update HW with number of TX descriptors to be sent */
852 static void mvneta_txq_pend_desc_add(struct mvneta_port *pp,
853 struct mvneta_tx_queue *txq,
858 pend_desc += txq->pending;
860 /* Only 255 Tx descriptors can be added at once */
862 val = min(pend_desc, 255);
863 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
865 } while (pend_desc > 0);
869 /* Get pointer to next TX descriptor to be processed (send) by HW */
870 static struct mvneta_tx_desc *
871 mvneta_txq_next_desc_get(struct mvneta_tx_queue *txq)
873 int tx_desc = txq->next_desc_to_proc;
875 txq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(txq, tx_desc);
876 return txq->descs + tx_desc;
879 /* Release the last allocated TX descriptor. Useful to handle DMA
880 * mapping failures in the TX path.
882 static void mvneta_txq_desc_put(struct mvneta_tx_queue *txq)
884 if (txq->next_desc_to_proc == 0)
885 txq->next_desc_to_proc = txq->last_desc - 1;
887 txq->next_desc_to_proc--;
890 /* Set rxq buf size */
891 static void mvneta_rxq_buf_size_set(struct mvneta_port *pp,
892 struct mvneta_rx_queue *rxq,
897 val = mvreg_read(pp, MVNETA_RXQ_SIZE_REG(rxq->id));
899 val &= ~MVNETA_RXQ_BUF_SIZE_MASK;
900 val |= ((buf_size >> 3) << MVNETA_RXQ_BUF_SIZE_SHIFT);
902 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), val);
905 /* Disable buffer management (BM) */
906 static void mvneta_rxq_bm_disable(struct mvneta_port *pp,
907 struct mvneta_rx_queue *rxq)
911 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
912 val &= ~MVNETA_RXQ_HW_BUF_ALLOC;
913 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
916 /* Enable buffer management (BM) */
917 static void mvneta_rxq_bm_enable(struct mvneta_port *pp,
918 struct mvneta_rx_queue *rxq)
922 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
923 val |= MVNETA_RXQ_HW_BUF_ALLOC;
924 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
927 /* Notify HW about port's assignment of pool for bigger packets */
928 static void mvneta_rxq_long_pool_set(struct mvneta_port *pp,
929 struct mvneta_rx_queue *rxq)
933 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
934 val &= ~MVNETA_RXQ_LONG_POOL_ID_MASK;
935 val |= (pp->pool_long->id << MVNETA_RXQ_LONG_POOL_ID_SHIFT);
937 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
940 /* Notify HW about port's assignment of pool for smaller packets */
941 static void mvneta_rxq_short_pool_set(struct mvneta_port *pp,
942 struct mvneta_rx_queue *rxq)
946 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
947 val &= ~MVNETA_RXQ_SHORT_POOL_ID_MASK;
948 val |= (pp->pool_short->id << MVNETA_RXQ_SHORT_POOL_ID_SHIFT);
950 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
953 /* Set port's receive buffer size for assigned BM pool */
954 static inline void mvneta_bm_pool_bufsize_set(struct mvneta_port *pp,
960 if (!IS_ALIGNED(buf_size, 8)) {
961 dev_warn(pp->dev->dev.parent,
962 "illegal buf_size value %d, round to %d\n",
963 buf_size, ALIGN(buf_size, 8));
964 buf_size = ALIGN(buf_size, 8);
967 val = mvreg_read(pp, MVNETA_PORT_POOL_BUFFER_SZ_REG(pool_id));
968 val |= buf_size & MVNETA_PORT_POOL_BUFFER_SZ_MASK;
969 mvreg_write(pp, MVNETA_PORT_POOL_BUFFER_SZ_REG(pool_id), val);
972 /* Configure MBUS window in order to enable access BM internal SRAM */
973 static int mvneta_mbus_io_win_set(struct mvneta_port *pp, u32 base, u32 wsize,
976 u32 win_enable, win_protect;
979 win_enable = mvreg_read(pp, MVNETA_BASE_ADDR_ENABLE);
981 if (pp->bm_win_id < 0) {
982 /* Find first not occupied window */
983 for (i = 0; i < MVNETA_MAX_DECODE_WIN; i++) {
984 if (win_enable & (1 << i)) {
989 if (i == MVNETA_MAX_DECODE_WIN)
995 mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
996 mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
999 mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
1001 mvreg_write(pp, MVNETA_WIN_BASE(i), (base & 0xffff0000) |
1002 (attr << 8) | target);
1004 mvreg_write(pp, MVNETA_WIN_SIZE(i), (wsize - 1) & 0xffff0000);
1006 win_protect = mvreg_read(pp, MVNETA_ACCESS_PROTECT_ENABLE);
1007 win_protect |= 3 << (2 * i);
1008 mvreg_write(pp, MVNETA_ACCESS_PROTECT_ENABLE, win_protect);
1010 win_enable &= ~(1 << i);
1011 mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
1016 static int mvneta_bm_port_mbus_init(struct mvneta_port *pp)
1022 /* Get BM window information */
1023 err = mvebu_mbus_get_io_win_info(pp->bm_priv->bppi_phys_addr, &wsize,
1030 /* Open NETA -> BM window */
1031 err = mvneta_mbus_io_win_set(pp, pp->bm_priv->bppi_phys_addr, wsize,
1034 netdev_info(pp->dev, "fail to configure mbus window to BM\n");
1040 /* Assign and initialize pools for port. In case of fail
1041 * buffer manager will remain disabled for current port.
1043 static int mvneta_bm_port_init(struct platform_device *pdev,
1044 struct mvneta_port *pp)
1046 struct device_node *dn = pdev->dev.of_node;
1047 u32 long_pool_id, short_pool_id;
1049 if (!pp->neta_armada3700) {
1052 ret = mvneta_bm_port_mbus_init(pp);
1057 if (of_property_read_u32(dn, "bm,pool-long", &long_pool_id)) {
1058 netdev_info(pp->dev, "missing long pool id\n");
1062 /* Create port's long pool depending on mtu */
1063 pp->pool_long = mvneta_bm_pool_use(pp->bm_priv, long_pool_id,
1064 MVNETA_BM_LONG, pp->id,
1065 MVNETA_RX_PKT_SIZE(pp->dev->mtu));
1066 if (!pp->pool_long) {
1067 netdev_info(pp->dev, "fail to obtain long pool for port\n");
1071 pp->pool_long->port_map |= 1 << pp->id;
1073 mvneta_bm_pool_bufsize_set(pp, pp->pool_long->buf_size,
1076 /* If short pool id is not defined, assume using single pool */
1077 if (of_property_read_u32(dn, "bm,pool-short", &short_pool_id))
1078 short_pool_id = long_pool_id;
1080 /* Create port's short pool */
1081 pp->pool_short = mvneta_bm_pool_use(pp->bm_priv, short_pool_id,
1082 MVNETA_BM_SHORT, pp->id,
1083 MVNETA_BM_SHORT_PKT_SIZE);
1084 if (!pp->pool_short) {
1085 netdev_info(pp->dev, "fail to obtain short pool for port\n");
1086 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
1090 if (short_pool_id != long_pool_id) {
1091 pp->pool_short->port_map |= 1 << pp->id;
1092 mvneta_bm_pool_bufsize_set(pp, pp->pool_short->buf_size,
1093 pp->pool_short->id);
1099 /* Update settings of a pool for bigger packets */
1100 static void mvneta_bm_update_mtu(struct mvneta_port *pp, int mtu)
1102 struct mvneta_bm_pool *bm_pool = pp->pool_long;
1103 struct hwbm_pool *hwbm_pool = &bm_pool->hwbm_pool;
1106 /* Release all buffers from long pool */
1107 mvneta_bm_bufs_free(pp->bm_priv, bm_pool, 1 << pp->id);
1108 if (hwbm_pool->buf_num) {
1109 WARN(1, "cannot free all buffers in pool %d\n",
1114 bm_pool->pkt_size = MVNETA_RX_PKT_SIZE(mtu);
1115 bm_pool->buf_size = MVNETA_RX_BUF_SIZE(bm_pool->pkt_size);
1116 hwbm_pool->frag_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
1117 SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(bm_pool->pkt_size));
1119 /* Fill entire long pool */
1120 num = hwbm_pool_add(hwbm_pool, hwbm_pool->size, GFP_ATOMIC);
1121 if (num != hwbm_pool->size) {
1122 WARN(1, "pool %d: %d of %d allocated\n",
1123 bm_pool->id, num, hwbm_pool->size);
1126 mvneta_bm_pool_bufsize_set(pp, bm_pool->buf_size, bm_pool->id);
1131 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
1132 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short, 1 << pp->id);
1135 mvreg_write(pp, MVNETA_ACC_MODE, MVNETA_ACC_MODE_EXT1);
1136 netdev_info(pp->dev, "fail to update MTU, fall back to software BM\n");
1139 /* Start the Ethernet port RX and TX activity */
1140 static void mvneta_port_up(struct mvneta_port *pp)
1145 /* Enable all initialized TXs. */
1147 for (queue = 0; queue < txq_number; queue++) {
1148 struct mvneta_tx_queue *txq = &pp->txqs[queue];
1150 q_map |= (1 << queue);
1152 mvreg_write(pp, MVNETA_TXQ_CMD, q_map);
1155 /* Enable all initialized RXQs. */
1156 for (queue = 0; queue < rxq_number; queue++) {
1157 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
1160 q_map |= (1 << queue);
1162 mvreg_write(pp, MVNETA_RXQ_CMD, q_map);
1165 /* Stop the Ethernet port activity */
1166 static void mvneta_port_down(struct mvneta_port *pp)
1171 /* Stop Rx port activity. Check port Rx activity. */
1172 val = mvreg_read(pp, MVNETA_RXQ_CMD) & MVNETA_RXQ_ENABLE_MASK;
1174 /* Issue stop command for active channels only */
1176 mvreg_write(pp, MVNETA_RXQ_CMD,
1177 val << MVNETA_RXQ_DISABLE_SHIFT);
1179 /* Wait for all Rx activity to terminate. */
1182 if (count++ >= MVNETA_RX_DISABLE_TIMEOUT_MSEC) {
1183 netdev_warn(pp->dev,
1184 "TIMEOUT for RX stopped ! rx_queue_cmd: 0x%08x\n",
1190 val = mvreg_read(pp, MVNETA_RXQ_CMD);
1191 } while (val & MVNETA_RXQ_ENABLE_MASK);
1193 /* Stop Tx port activity. Check port Tx activity. Issue stop
1194 * command for active channels only
1196 val = (mvreg_read(pp, MVNETA_TXQ_CMD)) & MVNETA_TXQ_ENABLE_MASK;
1199 mvreg_write(pp, MVNETA_TXQ_CMD,
1200 (val << MVNETA_TXQ_DISABLE_SHIFT));
1202 /* Wait for all Tx activity to terminate. */
1205 if (count++ >= MVNETA_TX_DISABLE_TIMEOUT_MSEC) {
1206 netdev_warn(pp->dev,
1207 "TIMEOUT for TX stopped status=0x%08x\n",
1213 /* Check TX Command reg that all Txqs are stopped */
1214 val = mvreg_read(pp, MVNETA_TXQ_CMD);
1216 } while (val & MVNETA_TXQ_ENABLE_MASK);
1218 /* Double check to verify that TX FIFO is empty */
1221 if (count++ >= MVNETA_TX_FIFO_EMPTY_TIMEOUT) {
1222 netdev_warn(pp->dev,
1223 "TX FIFO empty timeout status=0x%08x\n",
1229 val = mvreg_read(pp, MVNETA_PORT_STATUS);
1230 } while (!(val & MVNETA_TX_FIFO_EMPTY) &&
1231 (val & MVNETA_TX_IN_PRGRS));
1236 /* Enable the port by setting the port enable bit of the MAC control register */
1237 static void mvneta_port_enable(struct mvneta_port *pp)
1242 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
1243 val |= MVNETA_GMAC0_PORT_ENABLE;
1244 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
1247 /* Disable the port and wait for about 200 usec before retuning */
1248 static void mvneta_port_disable(struct mvneta_port *pp)
1252 /* Reset the Enable bit in the Serial Control Register */
1253 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
1254 val &= ~MVNETA_GMAC0_PORT_ENABLE;
1255 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
1260 /* Multicast tables methods */
1262 /* Set all entries in Unicast MAC Table; queue==-1 means reject all */
1263 static void mvneta_set_ucast_table(struct mvneta_port *pp, int queue)
1271 val = 0x1 | (queue << 1);
1272 val |= (val << 24) | (val << 16) | (val << 8);
1275 for (offset = 0; offset <= 0xc; offset += 4)
1276 mvreg_write(pp, MVNETA_DA_FILT_UCAST_BASE + offset, val);
1279 /* Set all entries in Special Multicast MAC Table; queue==-1 means reject all */
1280 static void mvneta_set_special_mcast_table(struct mvneta_port *pp, int queue)
1288 val = 0x1 | (queue << 1);
1289 val |= (val << 24) | (val << 16) | (val << 8);
1292 for (offset = 0; offset <= 0xfc; offset += 4)
1293 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + offset, val);
1297 /* Set all entries in Other Multicast MAC Table. queue==-1 means reject all */
1298 static void mvneta_set_other_mcast_table(struct mvneta_port *pp, int queue)
1304 memset(pp->mcast_count, 0, sizeof(pp->mcast_count));
1307 memset(pp->mcast_count, 1, sizeof(pp->mcast_count));
1308 val = 0x1 | (queue << 1);
1309 val |= (val << 24) | (val << 16) | (val << 8);
1312 for (offset = 0; offset <= 0xfc; offset += 4)
1313 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + offset, val);
1316 static void mvneta_percpu_unmask_interrupt(void *arg)
1318 struct mvneta_port *pp = arg;
1320 /* All the queue are unmasked, but actually only the ones
1321 * mapped to this CPU will be unmasked
1323 mvreg_write(pp, MVNETA_INTR_NEW_MASK,
1324 MVNETA_RX_INTR_MASK_ALL |
1325 MVNETA_TX_INTR_MASK_ALL |
1326 MVNETA_MISCINTR_INTR_MASK);
1329 static void mvneta_percpu_mask_interrupt(void *arg)
1331 struct mvneta_port *pp = arg;
1333 /* All the queue are masked, but actually only the ones
1334 * mapped to this CPU will be masked
1336 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
1337 mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0);
1338 mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0);
1341 static void mvneta_percpu_clear_intr_cause(void *arg)
1343 struct mvneta_port *pp = arg;
1345 /* All the queue are cleared, but actually only the ones
1346 * mapped to this CPU will be cleared
1348 mvreg_write(pp, MVNETA_INTR_NEW_CAUSE, 0);
1349 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
1350 mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0);
1353 /* This method sets defaults to the NETA port:
1354 * Clears interrupt Cause and Mask registers.
1355 * Clears all MAC tables.
1356 * Sets defaults to all registers.
1357 * Resets RX and TX descriptor rings.
1359 * This method can be called after mvneta_port_down() to return the port
1360 * settings to defaults.
1362 static void mvneta_defaults_set(struct mvneta_port *pp)
1367 int max_cpu = num_present_cpus();
1369 /* Clear all Cause registers */
1370 on_each_cpu(mvneta_percpu_clear_intr_cause, pp, true);
1372 /* Mask all interrupts */
1373 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
1374 mvreg_write(pp, MVNETA_INTR_ENABLE, 0);
1376 /* Enable MBUS Retry bit16 */
1377 mvreg_write(pp, MVNETA_MBUS_RETRY, 0x20);
1379 /* Set CPU queue access map. CPUs are assigned to the RX and
1380 * TX queues modulo their number. If there is only one TX
1381 * queue then it is assigned to the CPU associated to the
1384 for_each_present_cpu(cpu) {
1385 int rxq_map = 0, txq_map = 0;
1387 if (!pp->neta_armada3700) {
1388 for (rxq = 0; rxq < rxq_number; rxq++)
1389 if ((rxq % max_cpu) == cpu)
1390 rxq_map |= MVNETA_CPU_RXQ_ACCESS(rxq);
1392 for (txq = 0; txq < txq_number; txq++)
1393 if ((txq % max_cpu) == cpu)
1394 txq_map |= MVNETA_CPU_TXQ_ACCESS(txq);
1396 /* With only one TX queue we configure a special case
1397 * which will allow to get all the irq on a single
1400 if (txq_number == 1)
1401 txq_map = (cpu == pp->rxq_def) ?
1402 MVNETA_CPU_TXQ_ACCESS(1) : 0;
1405 txq_map = MVNETA_CPU_TXQ_ACCESS_ALL_MASK;
1406 rxq_map = MVNETA_CPU_RXQ_ACCESS_ALL_MASK;
1409 mvreg_write(pp, MVNETA_CPU_MAP(cpu), rxq_map | txq_map);
1412 /* Reset RX and TX DMAs */
1413 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
1414 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
1416 /* Disable Legacy WRR, Disable EJP, Release from reset */
1417 mvreg_write(pp, MVNETA_TXQ_CMD_1, 0);
1418 for (queue = 0; queue < txq_number; queue++) {
1419 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(queue), 0);
1420 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(queue), 0);
1423 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
1424 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
1426 /* Set Port Acceleration Mode */
1428 /* HW buffer management + legacy parser */
1429 val = MVNETA_ACC_MODE_EXT2;
1431 /* SW buffer management + legacy parser */
1432 val = MVNETA_ACC_MODE_EXT1;
1433 mvreg_write(pp, MVNETA_ACC_MODE, val);
1436 mvreg_write(pp, MVNETA_BM_ADDRESS, pp->bm_priv->bppi_phys_addr);
1438 /* Update val of portCfg register accordingly with all RxQueue types */
1439 val = MVNETA_PORT_CONFIG_DEFL_VALUE(pp->rxq_def);
1440 mvreg_write(pp, MVNETA_PORT_CONFIG, val);
1443 mvreg_write(pp, MVNETA_PORT_CONFIG_EXTEND, val);
1444 mvreg_write(pp, MVNETA_RX_MIN_FRAME_SIZE, 64);
1446 /* Build PORT_SDMA_CONFIG_REG */
1449 /* Default burst size */
1450 val |= MVNETA_TX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
1451 val |= MVNETA_RX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
1452 val |= MVNETA_RX_NO_DATA_SWAP | MVNETA_TX_NO_DATA_SWAP;
1454 #if defined(__BIG_ENDIAN)
1455 val |= MVNETA_DESC_SWAP;
1458 /* Assign port SDMA configuration */
1459 mvreg_write(pp, MVNETA_SDMA_CONFIG, val);
1461 /* Disable PHY polling in hardware, since we're using the
1462 * kernel phylib to do this.
1464 val = mvreg_read(pp, MVNETA_UNIT_CONTROL);
1465 val &= ~MVNETA_PHY_POLLING_ENABLE;
1466 mvreg_write(pp, MVNETA_UNIT_CONTROL, val);
1468 mvneta_set_ucast_table(pp, -1);
1469 mvneta_set_special_mcast_table(pp, -1);
1470 mvneta_set_other_mcast_table(pp, -1);
1472 /* Set port interrupt enable register - default enable all */
1473 mvreg_write(pp, MVNETA_INTR_ENABLE,
1474 (MVNETA_RXQ_INTR_ENABLE_ALL_MASK
1475 | MVNETA_TXQ_INTR_ENABLE_ALL_MASK));
1477 mvneta_mib_counters_clear(pp);
1480 /* Set max sizes for tx queues */
1481 static void mvneta_txq_max_tx_size_set(struct mvneta_port *pp, int max_tx_size)
1487 mtu = max_tx_size * 8;
1488 if (mtu > MVNETA_TX_MTU_MAX)
1489 mtu = MVNETA_TX_MTU_MAX;
1492 val = mvreg_read(pp, MVNETA_TX_MTU);
1493 val &= ~MVNETA_TX_MTU_MAX;
1495 mvreg_write(pp, MVNETA_TX_MTU, val);
1497 /* TX token size and all TXQs token size must be larger that MTU */
1498 val = mvreg_read(pp, MVNETA_TX_TOKEN_SIZE);
1500 size = val & MVNETA_TX_TOKEN_SIZE_MAX;
1503 val &= ~MVNETA_TX_TOKEN_SIZE_MAX;
1505 mvreg_write(pp, MVNETA_TX_TOKEN_SIZE, val);
1507 for (queue = 0; queue < txq_number; queue++) {
1508 val = mvreg_read(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue));
1510 size = val & MVNETA_TXQ_TOKEN_SIZE_MAX;
1513 val &= ~MVNETA_TXQ_TOKEN_SIZE_MAX;
1515 mvreg_write(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue), val);
1520 /* Set unicast address */
1521 static void mvneta_set_ucast_addr(struct mvneta_port *pp, u8 last_nibble,
1524 unsigned int unicast_reg;
1525 unsigned int tbl_offset;
1526 unsigned int reg_offset;
1528 /* Locate the Unicast table entry */
1529 last_nibble = (0xf & last_nibble);
1531 /* offset from unicast tbl base */
1532 tbl_offset = (last_nibble / 4) * 4;
1534 /* offset within the above reg */
1535 reg_offset = last_nibble % 4;
1537 unicast_reg = mvreg_read(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset));
1540 /* Clear accepts frame bit at specified unicast DA tbl entry */
1541 unicast_reg &= ~(0xff << (8 * reg_offset));
1543 unicast_reg &= ~(0xff << (8 * reg_offset));
1544 unicast_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
1547 mvreg_write(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset), unicast_reg);
1550 /* Set mac address */
1551 static void mvneta_mac_addr_set(struct mvneta_port *pp, unsigned char *addr,
1558 mac_l = (addr[4] << 8) | (addr[5]);
1559 mac_h = (addr[0] << 24) | (addr[1] << 16) |
1560 (addr[2] << 8) | (addr[3] << 0);
1562 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, mac_l);
1563 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, mac_h);
1566 /* Accept frames of this address */
1567 mvneta_set_ucast_addr(pp, addr[5], queue);
1570 /* Set the number of packets that will be received before RX interrupt
1571 * will be generated by HW.
1573 static void mvneta_rx_pkts_coal_set(struct mvneta_port *pp,
1574 struct mvneta_rx_queue *rxq, u32 value)
1576 mvreg_write(pp, MVNETA_RXQ_THRESHOLD_REG(rxq->id),
1577 value | MVNETA_RXQ_NON_OCCUPIED(0));
1580 /* Set the time delay in usec before RX interrupt will be generated by
1583 static void mvneta_rx_time_coal_set(struct mvneta_port *pp,
1584 struct mvneta_rx_queue *rxq, u32 value)
1587 unsigned long clk_rate;
1589 clk_rate = clk_get_rate(pp->clk);
1590 val = (clk_rate / 1000000) * value;
1592 mvreg_write(pp, MVNETA_RXQ_TIME_COAL_REG(rxq->id), val);
1595 /* Set threshold for TX_DONE pkts coalescing */
1596 static void mvneta_tx_done_pkts_coal_set(struct mvneta_port *pp,
1597 struct mvneta_tx_queue *txq, u32 value)
1601 val = mvreg_read(pp, MVNETA_TXQ_SIZE_REG(txq->id));
1603 val &= ~MVNETA_TXQ_SENT_THRESH_ALL_MASK;
1604 val |= MVNETA_TXQ_SENT_THRESH_MASK(value);
1606 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), val);
1609 /* Handle rx descriptor fill by setting buf_cookie and buf_phys_addr */
1610 static void mvneta_rx_desc_fill(struct mvneta_rx_desc *rx_desc,
1611 u32 phys_addr, void *virt_addr,
1612 struct mvneta_rx_queue *rxq)
1616 rx_desc->buf_phys_addr = phys_addr;
1617 i = rx_desc - rxq->descs;
1618 rxq->buf_virt_addr[i] = virt_addr;
1621 /* Decrement sent descriptors counter */
1622 static void mvneta_txq_sent_desc_dec(struct mvneta_port *pp,
1623 struct mvneta_tx_queue *txq,
1628 /* Only 255 TX descriptors can be updated at once */
1629 while (sent_desc > 0xff) {
1630 val = 0xff << MVNETA_TXQ_DEC_SENT_SHIFT;
1631 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1632 sent_desc = sent_desc - 0xff;
1635 val = sent_desc << MVNETA_TXQ_DEC_SENT_SHIFT;
1636 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1639 /* Get number of TX descriptors already sent by HW */
1640 static int mvneta_txq_sent_desc_num_get(struct mvneta_port *pp,
1641 struct mvneta_tx_queue *txq)
1646 val = mvreg_read(pp, MVNETA_TXQ_STATUS_REG(txq->id));
1647 sent_desc = (val & MVNETA_TXQ_SENT_DESC_MASK) >>
1648 MVNETA_TXQ_SENT_DESC_SHIFT;
1653 /* Get number of sent descriptors and decrement counter.
1654 * The number of sent descriptors is returned.
1656 static int mvneta_txq_sent_desc_proc(struct mvneta_port *pp,
1657 struct mvneta_tx_queue *txq)
1661 /* Get number of sent descriptors */
1662 sent_desc = mvneta_txq_sent_desc_num_get(pp, txq);
1664 /* Decrement sent descriptors counter */
1666 mvneta_txq_sent_desc_dec(pp, txq, sent_desc);
1671 /* Set TXQ descriptors fields relevant for CSUM calculation */
1672 static u32 mvneta_txq_desc_csum(int l3_offs, int l3_proto,
1673 int ip_hdr_len, int l4_proto)
1677 /* Fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
1678 * G_L4_chk, L4_type; required only for checksum
1681 command = l3_offs << MVNETA_TX_L3_OFF_SHIFT;
1682 command |= ip_hdr_len << MVNETA_TX_IP_HLEN_SHIFT;
1684 if (l3_proto == htons(ETH_P_IP))
1685 command |= MVNETA_TXD_IP_CSUM;
1687 command |= MVNETA_TX_L3_IP6;
1689 if (l4_proto == IPPROTO_TCP)
1690 command |= MVNETA_TX_L4_CSUM_FULL;
1691 else if (l4_proto == IPPROTO_UDP)
1692 command |= MVNETA_TX_L4_UDP | MVNETA_TX_L4_CSUM_FULL;
1694 command |= MVNETA_TX_L4_CSUM_NOT;
1700 /* Display more error info */
1701 static void mvneta_rx_error(struct mvneta_port *pp,
1702 struct mvneta_rx_desc *rx_desc)
1704 u32 status = rx_desc->status;
1706 switch (status & MVNETA_RXD_ERR_CODE_MASK) {
1707 case MVNETA_RXD_ERR_CRC:
1708 netdev_err(pp->dev, "bad rx status %08x (crc error), size=%d\n",
1709 status, rx_desc->data_size);
1711 case MVNETA_RXD_ERR_OVERRUN:
1712 netdev_err(pp->dev, "bad rx status %08x (overrun error), size=%d\n",
1713 status, rx_desc->data_size);
1715 case MVNETA_RXD_ERR_LEN:
1716 netdev_err(pp->dev, "bad rx status %08x (max frame length error), size=%d\n",
1717 status, rx_desc->data_size);
1719 case MVNETA_RXD_ERR_RESOURCE:
1720 netdev_err(pp->dev, "bad rx status %08x (resource error), size=%d\n",
1721 status, rx_desc->data_size);
1726 /* Handle RX checksum offload based on the descriptor's status */
1727 static void mvneta_rx_csum(struct mvneta_port *pp, u32 status,
1728 struct sk_buff *skb)
1730 if ((pp->dev->features & NETIF_F_RXCSUM) &&
1731 (status & MVNETA_RXD_L3_IP4) &&
1732 (status & MVNETA_RXD_L4_CSUM_OK)) {
1734 skb->ip_summed = CHECKSUM_UNNECESSARY;
1738 skb->ip_summed = CHECKSUM_NONE;
1741 /* Return tx queue pointer (find last set bit) according to <cause> returned
1742 * form tx_done reg. <cause> must not be null. The return value is always a
1743 * valid queue for matching the first one found in <cause>.
1745 static struct mvneta_tx_queue *mvneta_tx_done_policy(struct mvneta_port *pp,
1748 int queue = fls(cause) - 1;
1750 return &pp->txqs[queue];
1753 /* Free tx queue skbuffs */
1754 static void mvneta_txq_bufs_free(struct mvneta_port *pp,
1755 struct mvneta_tx_queue *txq, int num,
1756 struct netdev_queue *nq)
1758 unsigned int bytes_compl = 0, pkts_compl = 0;
1761 for (i = 0; i < num; i++) {
1762 struct mvneta_tx_desc *tx_desc = txq->descs +
1764 struct sk_buff *skb = txq->tx_skb[txq->txq_get_index];
1767 bytes_compl += skb->len;
1771 mvneta_txq_inc_get(txq);
1773 if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr))
1774 dma_unmap_single(pp->dev->dev.parent,
1775 tx_desc->buf_phys_addr,
1776 tx_desc->data_size, DMA_TO_DEVICE);
1779 dev_kfree_skb_any(skb);
1782 netdev_tx_completed_queue(nq, pkts_compl, bytes_compl);
1785 /* Handle end of transmission */
1786 static void mvneta_txq_done(struct mvneta_port *pp,
1787 struct mvneta_tx_queue *txq)
1789 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
1792 tx_done = mvneta_txq_sent_desc_proc(pp, txq);
1796 mvneta_txq_bufs_free(pp, txq, tx_done, nq);
1798 txq->count -= tx_done;
1800 if (netif_tx_queue_stopped(nq)) {
1801 if (txq->count <= txq->tx_wake_threshold)
1802 netif_tx_wake_queue(nq);
1806 /* Refill processing for SW buffer management */
1807 /* Allocate page per descriptor */
1808 static int mvneta_rx_refill(struct mvneta_port *pp,
1809 struct mvneta_rx_desc *rx_desc,
1810 struct mvneta_rx_queue *rxq,
1813 dma_addr_t phys_addr;
1816 page = __dev_alloc_page(gfp_mask);
1820 /* map page for use */
1821 phys_addr = dma_map_page(pp->dev->dev.parent, page, 0, PAGE_SIZE,
1823 if (unlikely(dma_mapping_error(pp->dev->dev.parent, phys_addr))) {
1828 phys_addr += pp->rx_offset_correction;
1829 mvneta_rx_desc_fill(rx_desc, phys_addr, page, rxq);
1833 /* Handle tx checksum */
1834 static u32 mvneta_skb_tx_csum(struct mvneta_port *pp, struct sk_buff *skb)
1836 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1838 __be16 l3_proto = vlan_get_protocol(skb);
1841 if (l3_proto == htons(ETH_P_IP)) {
1842 struct iphdr *ip4h = ip_hdr(skb);
1844 /* Calculate IPv4 checksum and L4 checksum */
1845 ip_hdr_len = ip4h->ihl;
1846 l4_proto = ip4h->protocol;
1847 } else if (l3_proto == htons(ETH_P_IPV6)) {
1848 struct ipv6hdr *ip6h = ipv6_hdr(skb);
1850 /* Read l4_protocol from one of IPv6 extra headers */
1851 if (skb_network_header_len(skb) > 0)
1852 ip_hdr_len = (skb_network_header_len(skb) >> 2);
1853 l4_proto = ip6h->nexthdr;
1855 return MVNETA_TX_L4_CSUM_NOT;
1857 return mvneta_txq_desc_csum(skb_network_offset(skb),
1858 l3_proto, ip_hdr_len, l4_proto);
1861 return MVNETA_TX_L4_CSUM_NOT;
1864 /* Drop packets received by the RXQ and free buffers */
1865 static void mvneta_rxq_drop_pkts(struct mvneta_port *pp,
1866 struct mvneta_rx_queue *rxq)
1870 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
1872 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
1875 for (i = 0; i < rx_done; i++) {
1876 struct mvneta_rx_desc *rx_desc =
1877 mvneta_rxq_next_desc_get(rxq);
1878 u8 pool_id = MVNETA_RX_GET_BM_POOL_ID(rx_desc);
1879 struct mvneta_bm_pool *bm_pool;
1881 bm_pool = &pp->bm_priv->bm_pools[pool_id];
1882 /* Return dropped buffer to the pool */
1883 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
1884 rx_desc->buf_phys_addr);
1889 for (i = 0; i < rxq->size; i++) {
1890 struct mvneta_rx_desc *rx_desc = rxq->descs + i;
1891 void *data = rxq->buf_virt_addr[i];
1892 if (!data || !(rx_desc->buf_phys_addr))
1895 dma_unmap_page(pp->dev->dev.parent, rx_desc->buf_phys_addr,
1896 PAGE_SIZE, DMA_FROM_DEVICE);
1902 int mvneta_rx_refill_queue(struct mvneta_port *pp, struct mvneta_rx_queue *rxq)
1904 struct mvneta_rx_desc *rx_desc;
1905 int curr_desc = rxq->first_to_refill;
1908 for (i = 0; (i < rxq->refill_num) && (i < 64); i++) {
1909 rx_desc = rxq->descs + curr_desc;
1910 if (!(rx_desc->buf_phys_addr)) {
1911 if (mvneta_rx_refill(pp, rx_desc, rxq, GFP_ATOMIC)) {
1912 pr_err("Can't refill queue %d. Done %d from %d\n",
1913 rxq->id, i, rxq->refill_num);
1918 curr_desc = MVNETA_QUEUE_NEXT_DESC(rxq, curr_desc);
1920 rxq->refill_num -= i;
1921 rxq->first_to_refill = curr_desc;
1926 /* Main rx processing when using software buffer management */
1927 static int mvneta_rx_swbm(struct napi_struct *napi,
1928 struct mvneta_port *pp, int budget,
1929 struct mvneta_rx_queue *rxq)
1931 struct net_device *dev = pp->dev;
1932 int rx_todo, rx_proc;
1937 /* Get number of received packets */
1938 rx_todo = mvneta_rxq_busy_desc_num_get(pp, rxq);
1941 /* Fairness NAPI loop */
1942 while ((rcvd_pkts < budget) && (rx_proc < rx_todo)) {
1943 struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
1944 unsigned char *data;
1946 dma_addr_t phys_addr;
1947 u32 rx_status, index;
1948 int rx_bytes, skb_size, copy_size;
1949 int frag_num, frag_size, frag_offset;
1951 index = rx_desc - rxq->descs;
1952 page = (struct page *)rxq->buf_virt_addr[index];
1953 data = page_address(page);
1954 /* Prefetch header */
1957 phys_addr = rx_desc->buf_phys_addr;
1958 rx_status = rx_desc->status;
1962 if (rx_status & MVNETA_RXD_FIRST_DESC) {
1963 /* Check errors only for FIRST descriptor */
1964 if (rx_status & MVNETA_RXD_ERR_SUMMARY) {
1965 mvneta_rx_error(pp, rx_desc);
1966 dev->stats.rx_errors++;
1967 /* leave the descriptor untouched */
1970 rx_bytes = rx_desc->data_size -
1971 (ETH_FCS_LEN + MVNETA_MH_SIZE);
1973 /* Allocate small skb for each new packet */
1974 skb_size = max(rx_copybreak, rx_header_size);
1975 rxq->skb = netdev_alloc_skb_ip_align(dev, skb_size);
1976 if (unlikely(!rxq->skb)) {
1978 "Can't allocate skb on queue %d\n",
1980 dev->stats.rx_dropped++;
1981 rxq->skb_alloc_err++;
1984 copy_size = min(skb_size, rx_bytes);
1986 /* Copy data from buffer to SKB, skip Marvell header */
1987 memcpy(rxq->skb->data, data + MVNETA_MH_SIZE,
1989 skb_put(rxq->skb, copy_size);
1990 rxq->left_size = rx_bytes - copy_size;
1992 mvneta_rx_csum(pp, rx_status, rxq->skb);
1993 if (rxq->left_size == 0) {
1994 int size = copy_size + MVNETA_MH_SIZE;
1996 dma_sync_single_range_for_cpu(dev->dev.parent,
2001 /* leave the descriptor and buffer untouched */
2003 /* refill descriptor with new buffer later */
2004 rx_desc->buf_phys_addr = 0;
2007 frag_offset = copy_size + MVNETA_MH_SIZE;
2008 frag_size = min(rxq->left_size,
2009 (int)(PAGE_SIZE - frag_offset));
2010 skb_add_rx_frag(rxq->skb, frag_num, page,
2011 frag_offset, frag_size,
2013 dma_unmap_page(dev->dev.parent, phys_addr,
2014 PAGE_SIZE, DMA_FROM_DEVICE);
2015 rxq->left_size -= frag_size;
2018 /* Middle or Last descriptor */
2019 if (unlikely(!rxq->skb)) {
2020 pr_debug("no skb for rx_status 0x%x\n",
2024 if (!rxq->left_size) {
2025 /* last descriptor has only FCS */
2026 /* and can be discarded */
2027 dma_sync_single_range_for_cpu(dev->dev.parent,
2031 /* leave the descriptor and buffer untouched */
2033 /* refill descriptor with new buffer later */
2034 rx_desc->buf_phys_addr = 0;
2036 frag_num = skb_shinfo(rxq->skb)->nr_frags;
2038 frag_size = min(rxq->left_size,
2039 (int)(PAGE_SIZE - frag_offset));
2040 skb_add_rx_frag(rxq->skb, frag_num, page,
2041 frag_offset, frag_size,
2044 dma_unmap_page(dev->dev.parent, phys_addr,
2045 PAGE_SIZE, DMA_FROM_DEVICE);
2047 rxq->left_size -= frag_size;
2049 } /* Middle or Last descriptor */
2051 if (!(rx_status & MVNETA_RXD_LAST_DESC))
2052 /* no last descriptor this time */
2055 if (rxq->left_size) {
2056 pr_err("get last desc, but left_size (%d) != 0\n",
2058 dev_kfree_skb_any(rxq->skb);
2064 rcvd_bytes += rxq->skb->len;
2066 /* Linux processing */
2067 rxq->skb->protocol = eth_type_trans(rxq->skb, dev);
2069 napi_gro_receive(napi, rxq->skb);
2071 /* clean uncomplete skb pointer in queue */
2077 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
2079 u64_stats_update_begin(&stats->syncp);
2080 stats->rx_packets += rcvd_pkts;
2081 stats->rx_bytes += rcvd_bytes;
2082 u64_stats_update_end(&stats->syncp);
2085 /* return some buffers to hardware queue, one at a time is too slow */
2086 refill = mvneta_rx_refill_queue(pp, rxq);
2088 /* Update rxq management counters */
2089 mvneta_rxq_desc_num_update(pp, rxq, rx_proc, refill);
2094 /* Main rx processing when using hardware buffer management */
2095 static int mvneta_rx_hwbm(struct napi_struct *napi,
2096 struct mvneta_port *pp, int rx_todo,
2097 struct mvneta_rx_queue *rxq)
2099 struct net_device *dev = pp->dev;
2104 /* Get number of received packets */
2105 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
2107 if (rx_todo > rx_done)
2112 /* Fairness NAPI loop */
2113 while (rx_done < rx_todo) {
2114 struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
2115 struct mvneta_bm_pool *bm_pool = NULL;
2116 struct sk_buff *skb;
2117 unsigned char *data;
2118 dma_addr_t phys_addr;
2119 u32 rx_status, frag_size;
2124 rx_status = rx_desc->status;
2125 rx_bytes = rx_desc->data_size - (ETH_FCS_LEN + MVNETA_MH_SIZE);
2126 data = (u8 *)(uintptr_t)rx_desc->buf_cookie;
2127 phys_addr = rx_desc->buf_phys_addr;
2128 pool_id = MVNETA_RX_GET_BM_POOL_ID(rx_desc);
2129 bm_pool = &pp->bm_priv->bm_pools[pool_id];
2131 if (!mvneta_rxq_desc_is_first_last(rx_status) ||
2132 (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
2133 err_drop_frame_ret_pool:
2134 /* Return the buffer to the pool */
2135 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
2136 rx_desc->buf_phys_addr);
2138 dev->stats.rx_errors++;
2139 mvneta_rx_error(pp, rx_desc);
2140 /* leave the descriptor untouched */
2144 if (rx_bytes <= rx_copybreak) {
2145 /* better copy a small frame and not unmap the DMA region */
2146 skb = netdev_alloc_skb_ip_align(dev, rx_bytes);
2148 goto err_drop_frame_ret_pool;
2150 dma_sync_single_range_for_cpu(dev->dev.parent,
2151 rx_desc->buf_phys_addr,
2152 MVNETA_MH_SIZE + NET_SKB_PAD,
2155 skb_put_data(skb, data + MVNETA_MH_SIZE + NET_SKB_PAD,
2158 skb->protocol = eth_type_trans(skb, dev);
2159 mvneta_rx_csum(pp, rx_status, skb);
2160 napi_gro_receive(napi, skb);
2163 rcvd_bytes += rx_bytes;
2165 /* Return the buffer to the pool */
2166 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
2167 rx_desc->buf_phys_addr);
2169 /* leave the descriptor and buffer untouched */
2173 /* Refill processing */
2174 err = hwbm_pool_refill(&bm_pool->hwbm_pool, GFP_ATOMIC);
2176 netdev_err(dev, "Linux processing - Can't refill\n");
2178 goto err_drop_frame_ret_pool;
2181 frag_size = bm_pool->hwbm_pool.frag_size;
2183 skb = build_skb(data, frag_size > PAGE_SIZE ? 0 : frag_size);
2185 /* After refill old buffer has to be unmapped regardless
2186 * the skb is successfully built or not.
2188 dma_unmap_single(&pp->bm_priv->pdev->dev, phys_addr,
2189 bm_pool->buf_size, DMA_FROM_DEVICE);
2191 goto err_drop_frame;
2194 rcvd_bytes += rx_bytes;
2196 /* Linux processing */
2197 skb_reserve(skb, MVNETA_MH_SIZE + NET_SKB_PAD);
2198 skb_put(skb, rx_bytes);
2200 skb->protocol = eth_type_trans(skb, dev);
2202 mvneta_rx_csum(pp, rx_status, skb);
2204 napi_gro_receive(napi, skb);
2208 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
2210 u64_stats_update_begin(&stats->syncp);
2211 stats->rx_packets += rcvd_pkts;
2212 stats->rx_bytes += rcvd_bytes;
2213 u64_stats_update_end(&stats->syncp);
2216 /* Update rxq management counters */
2217 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
2223 mvneta_tso_put_hdr(struct sk_buff *skb,
2224 struct mvneta_port *pp, struct mvneta_tx_queue *txq)
2226 struct mvneta_tx_desc *tx_desc;
2227 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2229 txq->tx_skb[txq->txq_put_index] = NULL;
2230 tx_desc = mvneta_txq_next_desc_get(txq);
2231 tx_desc->data_size = hdr_len;
2232 tx_desc->command = mvneta_skb_tx_csum(pp, skb);
2233 tx_desc->command |= MVNETA_TXD_F_DESC;
2234 tx_desc->buf_phys_addr = txq->tso_hdrs_phys +
2235 txq->txq_put_index * TSO_HEADER_SIZE;
2236 mvneta_txq_inc_put(txq);
2240 mvneta_tso_put_data(struct net_device *dev, struct mvneta_tx_queue *txq,
2241 struct sk_buff *skb, char *data, int size,
2242 bool last_tcp, bool is_last)
2244 struct mvneta_tx_desc *tx_desc;
2246 tx_desc = mvneta_txq_next_desc_get(txq);
2247 tx_desc->data_size = size;
2248 tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, data,
2249 size, DMA_TO_DEVICE);
2250 if (unlikely(dma_mapping_error(dev->dev.parent,
2251 tx_desc->buf_phys_addr))) {
2252 mvneta_txq_desc_put(txq);
2256 tx_desc->command = 0;
2257 txq->tx_skb[txq->txq_put_index] = NULL;
2260 /* last descriptor in the TCP packet */
2261 tx_desc->command = MVNETA_TXD_L_DESC;
2263 /* last descriptor in SKB */
2265 txq->tx_skb[txq->txq_put_index] = skb;
2267 mvneta_txq_inc_put(txq);
2271 static int mvneta_tx_tso(struct sk_buff *skb, struct net_device *dev,
2272 struct mvneta_tx_queue *txq)
2274 int total_len, data_left;
2276 struct mvneta_port *pp = netdev_priv(dev);
2278 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2281 /* Count needed descriptors */
2282 if ((txq->count + tso_count_descs(skb)) >= txq->size)
2285 if (skb_headlen(skb) < (skb_transport_offset(skb) + tcp_hdrlen(skb))) {
2286 pr_info("*** Is this even possible???!?!?\n");
2290 /* Initialize the TSO handler, and prepare the first payload */
2291 tso_start(skb, &tso);
2293 total_len = skb->len - hdr_len;
2294 while (total_len > 0) {
2297 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
2298 total_len -= data_left;
2301 /* prepare packet headers: MAC + IP + TCP */
2302 hdr = txq->tso_hdrs + txq->txq_put_index * TSO_HEADER_SIZE;
2303 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
2305 mvneta_tso_put_hdr(skb, pp, txq);
2307 while (data_left > 0) {
2311 size = min_t(int, tso.size, data_left);
2313 if (mvneta_tso_put_data(dev, txq, skb,
2320 tso_build_data(skb, &tso, size);
2327 /* Release all used data descriptors; header descriptors must not
2330 for (i = desc_count - 1; i >= 0; i--) {
2331 struct mvneta_tx_desc *tx_desc = txq->descs + i;
2332 if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr))
2333 dma_unmap_single(pp->dev->dev.parent,
2334 tx_desc->buf_phys_addr,
2337 mvneta_txq_desc_put(txq);
2342 /* Handle tx fragmentation processing */
2343 static int mvneta_tx_frag_process(struct mvneta_port *pp, struct sk_buff *skb,
2344 struct mvneta_tx_queue *txq)
2346 struct mvneta_tx_desc *tx_desc;
2347 int i, nr_frags = skb_shinfo(skb)->nr_frags;
2349 for (i = 0; i < nr_frags; i++) {
2350 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2351 void *addr = page_address(frag->page.p) + frag->page_offset;
2353 tx_desc = mvneta_txq_next_desc_get(txq);
2354 tx_desc->data_size = frag->size;
2356 tx_desc->buf_phys_addr =
2357 dma_map_single(pp->dev->dev.parent, addr,
2358 tx_desc->data_size, DMA_TO_DEVICE);
2360 if (dma_mapping_error(pp->dev->dev.parent,
2361 tx_desc->buf_phys_addr)) {
2362 mvneta_txq_desc_put(txq);
2366 if (i == nr_frags - 1) {
2367 /* Last descriptor */
2368 tx_desc->command = MVNETA_TXD_L_DESC | MVNETA_TXD_Z_PAD;
2369 txq->tx_skb[txq->txq_put_index] = skb;
2371 /* Descriptor in the middle: Not First, Not Last */
2372 tx_desc->command = 0;
2373 txq->tx_skb[txq->txq_put_index] = NULL;
2375 mvneta_txq_inc_put(txq);
2381 /* Release all descriptors that were used to map fragments of
2382 * this packet, as well as the corresponding DMA mappings
2384 for (i = i - 1; i >= 0; i--) {
2385 tx_desc = txq->descs + i;
2386 dma_unmap_single(pp->dev->dev.parent,
2387 tx_desc->buf_phys_addr,
2390 mvneta_txq_desc_put(txq);
2396 /* Main tx processing */
2397 static netdev_tx_t mvneta_tx(struct sk_buff *skb, struct net_device *dev)
2399 struct mvneta_port *pp = netdev_priv(dev);
2400 u16 txq_id = skb_get_queue_mapping(skb);
2401 struct mvneta_tx_queue *txq = &pp->txqs[txq_id];
2402 struct mvneta_tx_desc *tx_desc;
2407 if (!netif_running(dev))
2410 if (skb_is_gso(skb)) {
2411 frags = mvneta_tx_tso(skb, dev, txq);
2415 frags = skb_shinfo(skb)->nr_frags + 1;
2417 /* Get a descriptor for the first part of the packet */
2418 tx_desc = mvneta_txq_next_desc_get(txq);
2420 tx_cmd = mvneta_skb_tx_csum(pp, skb);
2422 tx_desc->data_size = skb_headlen(skb);
2424 tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, skb->data,
2427 if (unlikely(dma_mapping_error(dev->dev.parent,
2428 tx_desc->buf_phys_addr))) {
2429 mvneta_txq_desc_put(txq);
2435 /* First and Last descriptor */
2436 tx_cmd |= MVNETA_TXD_FLZ_DESC;
2437 tx_desc->command = tx_cmd;
2438 txq->tx_skb[txq->txq_put_index] = skb;
2439 mvneta_txq_inc_put(txq);
2441 /* First but not Last */
2442 tx_cmd |= MVNETA_TXD_F_DESC;
2443 txq->tx_skb[txq->txq_put_index] = NULL;
2444 mvneta_txq_inc_put(txq);
2445 tx_desc->command = tx_cmd;
2446 /* Continue with other skb fragments */
2447 if (mvneta_tx_frag_process(pp, skb, txq)) {
2448 dma_unmap_single(dev->dev.parent,
2449 tx_desc->buf_phys_addr,
2452 mvneta_txq_desc_put(txq);
2460 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
2461 struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
2463 netdev_tx_sent_queue(nq, len);
2465 txq->count += frags;
2466 if (txq->count >= txq->tx_stop_threshold)
2467 netif_tx_stop_queue(nq);
2469 if (!skb->xmit_more || netif_xmit_stopped(nq) ||
2470 txq->pending + frags > MVNETA_TXQ_DEC_SENT_MASK)
2471 mvneta_txq_pend_desc_add(pp, txq, frags);
2473 txq->pending += frags;
2475 u64_stats_update_begin(&stats->syncp);
2476 stats->tx_packets++;
2477 stats->tx_bytes += len;
2478 u64_stats_update_end(&stats->syncp);
2480 dev->stats.tx_dropped++;
2481 dev_kfree_skb_any(skb);
2484 return NETDEV_TX_OK;
2488 /* Free tx resources, when resetting a port */
2489 static void mvneta_txq_done_force(struct mvneta_port *pp,
2490 struct mvneta_tx_queue *txq)
2493 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
2494 int tx_done = txq->count;
2496 mvneta_txq_bufs_free(pp, txq, tx_done, nq);
2500 txq->txq_put_index = 0;
2501 txq->txq_get_index = 0;
2504 /* Handle tx done - called in softirq context. The <cause_tx_done> argument
2505 * must be a valid cause according to MVNETA_TXQ_INTR_MASK_ALL.
2507 static void mvneta_tx_done_gbe(struct mvneta_port *pp, u32 cause_tx_done)
2509 struct mvneta_tx_queue *txq;
2510 struct netdev_queue *nq;
2511 int cpu = smp_processor_id();
2513 while (cause_tx_done) {
2514 txq = mvneta_tx_done_policy(pp, cause_tx_done);
2516 nq = netdev_get_tx_queue(pp->dev, txq->id);
2517 __netif_tx_lock(nq, cpu);
2520 mvneta_txq_done(pp, txq);
2522 __netif_tx_unlock(nq);
2523 cause_tx_done &= ~((1 << txq->id));
2527 /* Compute crc8 of the specified address, using a unique algorithm ,
2528 * according to hw spec, different than generic crc8 algorithm
2530 static int mvneta_addr_crc(unsigned char *addr)
2535 for (i = 0; i < ETH_ALEN; i++) {
2538 crc = (crc ^ addr[i]) << 8;
2539 for (j = 7; j >= 0; j--) {
2540 if (crc & (0x100 << j))
2548 /* This method controls the net device special MAC multicast support.
2549 * The Special Multicast Table for MAC addresses supports MAC of the form
2550 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
2551 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
2552 * Table entries in the DA-Filter table. This method set the Special
2553 * Multicast Table appropriate entry.
2555 static void mvneta_set_special_mcast_addr(struct mvneta_port *pp,
2556 unsigned char last_byte,
2559 unsigned int smc_table_reg;
2560 unsigned int tbl_offset;
2561 unsigned int reg_offset;
2563 /* Register offset from SMC table base */
2564 tbl_offset = (last_byte / 4);
2565 /* Entry offset within the above reg */
2566 reg_offset = last_byte % 4;
2568 smc_table_reg = mvreg_read(pp, (MVNETA_DA_FILT_SPEC_MCAST
2572 smc_table_reg &= ~(0xff << (8 * reg_offset));
2574 smc_table_reg &= ~(0xff << (8 * reg_offset));
2575 smc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
2578 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + tbl_offset * 4,
2582 /* This method controls the network device Other MAC multicast support.
2583 * The Other Multicast Table is used for multicast of another type.
2584 * A CRC-8 is used as an index to the Other Multicast Table entries
2585 * in the DA-Filter table.
2586 * The method gets the CRC-8 value from the calling routine and
2587 * sets the Other Multicast Table appropriate entry according to the
2590 static void mvneta_set_other_mcast_addr(struct mvneta_port *pp,
2594 unsigned int omc_table_reg;
2595 unsigned int tbl_offset;
2596 unsigned int reg_offset;
2598 tbl_offset = (crc8 / 4) * 4; /* Register offset from OMC table base */
2599 reg_offset = crc8 % 4; /* Entry offset within the above reg */
2601 omc_table_reg = mvreg_read(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset);
2604 /* Clear accepts frame bit at specified Other DA table entry */
2605 omc_table_reg &= ~(0xff << (8 * reg_offset));
2607 omc_table_reg &= ~(0xff << (8 * reg_offset));
2608 omc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
2611 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset, omc_table_reg);
2614 /* The network device supports multicast using two tables:
2615 * 1) Special Multicast Table for MAC addresses of the form
2616 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
2617 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
2618 * Table entries in the DA-Filter table.
2619 * 2) Other Multicast Table for multicast of another type. A CRC-8 value
2620 * is used as an index to the Other Multicast Table entries in the
2623 static int mvneta_mcast_addr_set(struct mvneta_port *pp, unsigned char *p_addr,
2626 unsigned char crc_result = 0;
2628 if (memcmp(p_addr, "\x01\x00\x5e\x00\x00", 5) == 0) {
2629 mvneta_set_special_mcast_addr(pp, p_addr[5], queue);
2633 crc_result = mvneta_addr_crc(p_addr);
2635 if (pp->mcast_count[crc_result] == 0) {
2636 netdev_info(pp->dev, "No valid Mcast for crc8=0x%02x\n",
2641 pp->mcast_count[crc_result]--;
2642 if (pp->mcast_count[crc_result] != 0) {
2643 netdev_info(pp->dev,
2644 "After delete there are %d valid Mcast for crc8=0x%02x\n",
2645 pp->mcast_count[crc_result], crc_result);
2649 pp->mcast_count[crc_result]++;
2651 mvneta_set_other_mcast_addr(pp, crc_result, queue);
2656 /* Configure Fitering mode of Ethernet port */
2657 static void mvneta_rx_unicast_promisc_set(struct mvneta_port *pp,
2660 u32 port_cfg_reg, val;
2662 port_cfg_reg = mvreg_read(pp, MVNETA_PORT_CONFIG);
2664 val = mvreg_read(pp, MVNETA_TYPE_PRIO);
2666 /* Set / Clear UPM bit in port configuration register */
2668 /* Accept all Unicast addresses */
2669 port_cfg_reg |= MVNETA_UNI_PROMISC_MODE;
2670 val |= MVNETA_FORCE_UNI;
2671 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, 0xffff);
2672 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, 0xffffffff);
2674 /* Reject all Unicast addresses */
2675 port_cfg_reg &= ~MVNETA_UNI_PROMISC_MODE;
2676 val &= ~MVNETA_FORCE_UNI;
2679 mvreg_write(pp, MVNETA_PORT_CONFIG, port_cfg_reg);
2680 mvreg_write(pp, MVNETA_TYPE_PRIO, val);
2683 /* register unicast and multicast addresses */
2684 static void mvneta_set_rx_mode(struct net_device *dev)
2686 struct mvneta_port *pp = netdev_priv(dev);
2687 struct netdev_hw_addr *ha;
2689 if (dev->flags & IFF_PROMISC) {
2690 /* Accept all: Multicast + Unicast */
2691 mvneta_rx_unicast_promisc_set(pp, 1);
2692 mvneta_set_ucast_table(pp, pp->rxq_def);
2693 mvneta_set_special_mcast_table(pp, pp->rxq_def);
2694 mvneta_set_other_mcast_table(pp, pp->rxq_def);
2696 /* Accept single Unicast */
2697 mvneta_rx_unicast_promisc_set(pp, 0);
2698 mvneta_set_ucast_table(pp, -1);
2699 mvneta_mac_addr_set(pp, dev->dev_addr, pp->rxq_def);
2701 if (dev->flags & IFF_ALLMULTI) {
2702 /* Accept all multicast */
2703 mvneta_set_special_mcast_table(pp, pp->rxq_def);
2704 mvneta_set_other_mcast_table(pp, pp->rxq_def);
2706 /* Accept only initialized multicast */
2707 mvneta_set_special_mcast_table(pp, -1);
2708 mvneta_set_other_mcast_table(pp, -1);
2710 if (!netdev_mc_empty(dev)) {
2711 netdev_for_each_mc_addr(ha, dev) {
2712 mvneta_mcast_addr_set(pp, ha->addr,
2720 /* Interrupt handling - the callback for request_irq() */
2721 static irqreturn_t mvneta_isr(int irq, void *dev_id)
2723 struct mvneta_port *pp = (struct mvneta_port *)dev_id;
2725 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
2726 napi_schedule(&pp->napi);
2731 /* Interrupt handling - the callback for request_percpu_irq() */
2732 static irqreturn_t mvneta_percpu_isr(int irq, void *dev_id)
2734 struct mvneta_pcpu_port *port = (struct mvneta_pcpu_port *)dev_id;
2736 disable_percpu_irq(port->pp->dev->irq);
2737 napi_schedule(&port->napi);
2742 static void mvneta_link_change(struct mvneta_port *pp)
2744 u32 gmac_stat = mvreg_read(pp, MVNETA_GMAC_STATUS);
2746 phylink_mac_change(pp->phylink, !!(gmac_stat & MVNETA_GMAC_LINK_UP));
2750 * Bits 0 - 7 of the causeRxTx register indicate that are transmitted
2751 * packets on the corresponding TXQ (Bit 0 is for TX queue 1).
2752 * Bits 8 -15 of the cause Rx Tx register indicate that are received
2753 * packets on the corresponding RXQ (Bit 8 is for RX queue 0).
2754 * Each CPU has its own causeRxTx register
2756 static int mvneta_poll(struct napi_struct *napi, int budget)
2761 struct mvneta_port *pp = netdev_priv(napi->dev);
2762 struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
2764 if (!netif_running(pp->dev)) {
2765 napi_complete(napi);
2769 /* Read cause register */
2770 cause_rx_tx = mvreg_read(pp, MVNETA_INTR_NEW_CAUSE);
2771 if (cause_rx_tx & MVNETA_MISCINTR_INTR_MASK) {
2772 u32 cause_misc = mvreg_read(pp, MVNETA_INTR_MISC_CAUSE);
2774 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
2776 if (cause_misc & (MVNETA_CAUSE_PHY_STATUS_CHANGE |
2777 MVNETA_CAUSE_LINK_CHANGE))
2778 mvneta_link_change(pp);
2781 /* Release Tx descriptors */
2782 if (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL) {
2783 mvneta_tx_done_gbe(pp, (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL));
2784 cause_rx_tx &= ~MVNETA_TX_INTR_MASK_ALL;
2787 /* For the case where the last mvneta_poll did not process all
2790 rx_queue = fls(((cause_rx_tx >> 8) & 0xff));
2792 cause_rx_tx |= pp->neta_armada3700 ? pp->cause_rx_tx :
2796 rx_queue = rx_queue - 1;
2798 rx_done = mvneta_rx_hwbm(napi, pp, budget,
2799 &pp->rxqs[rx_queue]);
2801 rx_done = mvneta_rx_swbm(napi, pp, budget,
2802 &pp->rxqs[rx_queue]);
2805 if (rx_done < budget) {
2807 napi_complete_done(napi, rx_done);
2809 if (pp->neta_armada3700) {
2810 unsigned long flags;
2812 local_irq_save(flags);
2813 mvreg_write(pp, MVNETA_INTR_NEW_MASK,
2814 MVNETA_RX_INTR_MASK(rxq_number) |
2815 MVNETA_TX_INTR_MASK(txq_number) |
2816 MVNETA_MISCINTR_INTR_MASK);
2817 local_irq_restore(flags);
2819 enable_percpu_irq(pp->dev->irq, 0);
2823 if (pp->neta_armada3700)
2824 pp->cause_rx_tx = cause_rx_tx;
2826 port->cause_rx_tx = cause_rx_tx;
2831 /* Handle rxq fill: allocates rxq skbs; called when initializing a port */
2832 static int mvneta_rxq_fill(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
2837 for (i = 0; i < num; i++) {
2838 memset(rxq->descs + i, 0, sizeof(struct mvneta_rx_desc));
2839 if (mvneta_rx_refill(pp, rxq->descs + i, rxq,
2842 "%s:rxq %d, %d of %d buffs filled\n",
2843 __func__, rxq->id, i, num);
2848 /* Add this number of RX descriptors as non occupied (ready to
2851 mvneta_rxq_non_occup_desc_add(pp, rxq, i);
2856 /* Free all packets pending transmit from all TXQs and reset TX port */
2857 static void mvneta_tx_reset(struct mvneta_port *pp)
2861 /* free the skb's in the tx ring */
2862 for (queue = 0; queue < txq_number; queue++)
2863 mvneta_txq_done_force(pp, &pp->txqs[queue]);
2865 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
2866 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
2869 static void mvneta_rx_reset(struct mvneta_port *pp)
2871 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
2872 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
2875 /* Rx/Tx queue initialization/cleanup methods */
2877 static int mvneta_rxq_sw_init(struct mvneta_port *pp,
2878 struct mvneta_rx_queue *rxq)
2880 rxq->size = pp->rx_ring_size;
2882 /* Allocate memory for RX descriptors */
2883 rxq->descs = dma_alloc_coherent(pp->dev->dev.parent,
2884 rxq->size * MVNETA_DESC_ALIGNED_SIZE,
2885 &rxq->descs_phys, GFP_KERNEL);
2889 rxq->last_desc = rxq->size - 1;
2894 static void mvneta_rxq_hw_init(struct mvneta_port *pp,
2895 struct mvneta_rx_queue *rxq)
2897 /* Set Rx descriptors queue starting address */
2898 mvreg_write(pp, MVNETA_RXQ_BASE_ADDR_REG(rxq->id), rxq->descs_phys);
2899 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), rxq->size);
2901 /* Set coalescing pkts and time */
2902 mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
2903 mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
2907 mvneta_rxq_offset_set(pp, rxq, 0);
2908 mvneta_rxq_buf_size_set(pp, rxq, pp->frag_size);
2909 mvneta_rxq_bm_disable(pp, rxq);
2910 mvneta_rxq_fill(pp, rxq, rxq->size);
2913 mvneta_rxq_offset_set(pp, rxq,
2914 NET_SKB_PAD - pp->rx_offset_correction);
2916 mvneta_rxq_bm_enable(pp, rxq);
2917 /* Fill RXQ with buffers from RX pool */
2918 mvneta_rxq_long_pool_set(pp, rxq);
2919 mvneta_rxq_short_pool_set(pp, rxq);
2920 mvneta_rxq_non_occup_desc_add(pp, rxq, rxq->size);
2924 /* Create a specified RX queue */
2925 static int mvneta_rxq_init(struct mvneta_port *pp,
2926 struct mvneta_rx_queue *rxq)
2931 ret = mvneta_rxq_sw_init(pp, rxq);
2935 mvneta_rxq_hw_init(pp, rxq);
2940 /* Cleanup Rx queue */
2941 static void mvneta_rxq_deinit(struct mvneta_port *pp,
2942 struct mvneta_rx_queue *rxq)
2944 mvneta_rxq_drop_pkts(pp, rxq);
2947 dev_kfree_skb_any(rxq->skb);
2950 dma_free_coherent(pp->dev->dev.parent,
2951 rxq->size * MVNETA_DESC_ALIGNED_SIZE,
2957 rxq->next_desc_to_proc = 0;
2958 rxq->descs_phys = 0;
2959 rxq->first_to_refill = 0;
2960 rxq->refill_num = 0;
2965 static int mvneta_txq_sw_init(struct mvneta_port *pp,
2966 struct mvneta_tx_queue *txq)
2970 txq->size = pp->tx_ring_size;
2972 /* A queue must always have room for at least one skb.
2973 * Therefore, stop the queue when the free entries reaches
2974 * the maximum number of descriptors per skb.
2976 txq->tx_stop_threshold = txq->size - MVNETA_MAX_SKB_DESCS;
2977 txq->tx_wake_threshold = txq->tx_stop_threshold / 2;
2979 /* Allocate memory for TX descriptors */
2980 txq->descs = dma_alloc_coherent(pp->dev->dev.parent,
2981 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2982 &txq->descs_phys, GFP_KERNEL);
2986 txq->last_desc = txq->size - 1;
2988 txq->tx_skb = kmalloc_array(txq->size, sizeof(*txq->tx_skb),
2991 dma_free_coherent(pp->dev->dev.parent,
2992 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2993 txq->descs, txq->descs_phys);
2997 /* Allocate DMA buffers for TSO MAC/IP/TCP headers */
2998 txq->tso_hdrs = dma_alloc_coherent(pp->dev->dev.parent,
2999 txq->size * TSO_HEADER_SIZE,
3000 &txq->tso_hdrs_phys, GFP_KERNEL);
3001 if (!txq->tso_hdrs) {
3003 dma_free_coherent(pp->dev->dev.parent,
3004 txq->size * MVNETA_DESC_ALIGNED_SIZE,
3005 txq->descs, txq->descs_phys);
3009 /* Setup XPS mapping */
3011 cpu = txq->id % num_present_cpus();
3013 cpu = pp->rxq_def % num_present_cpus();
3014 cpumask_set_cpu(cpu, &txq->affinity_mask);
3015 netif_set_xps_queue(pp->dev, &txq->affinity_mask, txq->id);
3020 static void mvneta_txq_hw_init(struct mvneta_port *pp,
3021 struct mvneta_tx_queue *txq)
3023 /* Set maximum bandwidth for enabled TXQs */
3024 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0x03ffffff);
3025 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0x3fffffff);
3027 /* Set Tx descriptors queue starting address */
3028 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), txq->descs_phys);
3029 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), txq->size);
3031 mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
3034 /* Create and initialize a tx queue */
3035 static int mvneta_txq_init(struct mvneta_port *pp,
3036 struct mvneta_tx_queue *txq)
3040 ret = mvneta_txq_sw_init(pp, txq);
3044 mvneta_txq_hw_init(pp, txq);
3049 /* Free allocated resources when mvneta_txq_init() fails to allocate memory*/
3050 static void mvneta_txq_sw_deinit(struct mvneta_port *pp,
3051 struct mvneta_tx_queue *txq)
3053 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
3058 dma_free_coherent(pp->dev->dev.parent,
3059 txq->size * TSO_HEADER_SIZE,
3060 txq->tso_hdrs, txq->tso_hdrs_phys);
3062 dma_free_coherent(pp->dev->dev.parent,
3063 txq->size * MVNETA_DESC_ALIGNED_SIZE,
3064 txq->descs, txq->descs_phys);
3066 netdev_tx_reset_queue(nq);
3070 txq->next_desc_to_proc = 0;
3071 txq->descs_phys = 0;
3074 static void mvneta_txq_hw_deinit(struct mvneta_port *pp,
3075 struct mvneta_tx_queue *txq)
3077 /* Set minimum bandwidth for disabled TXQs */
3078 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0);
3079 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0);
3081 /* Set Tx descriptors queue starting address and size */
3082 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), 0);
3083 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), 0);
3086 static void mvneta_txq_deinit(struct mvneta_port *pp,
3087 struct mvneta_tx_queue *txq)
3089 mvneta_txq_sw_deinit(pp, txq);
3090 mvneta_txq_hw_deinit(pp, txq);
3093 /* Cleanup all Tx queues */
3094 static void mvneta_cleanup_txqs(struct mvneta_port *pp)
3098 for (queue = 0; queue < txq_number; queue++)
3099 mvneta_txq_deinit(pp, &pp->txqs[queue]);
3102 /* Cleanup all Rx queues */
3103 static void mvneta_cleanup_rxqs(struct mvneta_port *pp)
3107 for (queue = 0; queue < rxq_number; queue++)
3108 mvneta_rxq_deinit(pp, &pp->rxqs[queue]);
3112 /* Init all Rx queues */
3113 static int mvneta_setup_rxqs(struct mvneta_port *pp)
3117 for (queue = 0; queue < rxq_number; queue++) {
3118 int err = mvneta_rxq_init(pp, &pp->rxqs[queue]);
3121 netdev_err(pp->dev, "%s: can't create rxq=%d\n",
3123 mvneta_cleanup_rxqs(pp);
3131 /* Init all tx queues */
3132 static int mvneta_setup_txqs(struct mvneta_port *pp)
3136 for (queue = 0; queue < txq_number; queue++) {
3137 int err = mvneta_txq_init(pp, &pp->txqs[queue]);
3139 netdev_err(pp->dev, "%s: can't create txq=%d\n",
3141 mvneta_cleanup_txqs(pp);
3149 static void mvneta_start_dev(struct mvneta_port *pp)
3153 mvneta_max_rx_size_set(pp, pp->pkt_size);
3154 mvneta_txq_max_tx_size_set(pp, pp->pkt_size);
3156 /* start the Rx/Tx activity */
3157 mvneta_port_enable(pp);
3159 if (!pp->neta_armada3700) {
3160 /* Enable polling on the port */
3161 for_each_online_cpu(cpu) {
3162 struct mvneta_pcpu_port *port =
3163 per_cpu_ptr(pp->ports, cpu);
3165 napi_enable(&port->napi);
3168 napi_enable(&pp->napi);
3171 /* Unmask interrupts. It has to be done from each CPU */
3172 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
3174 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
3175 MVNETA_CAUSE_PHY_STATUS_CHANGE |
3176 MVNETA_CAUSE_LINK_CHANGE);
3178 phylink_start(pp->phylink);
3179 netif_tx_start_all_queues(pp->dev);
3182 static void mvneta_stop_dev(struct mvneta_port *pp)
3186 phylink_stop(pp->phylink);
3188 if (!pp->neta_armada3700) {
3189 for_each_online_cpu(cpu) {
3190 struct mvneta_pcpu_port *port =
3191 per_cpu_ptr(pp->ports, cpu);
3193 napi_disable(&port->napi);
3196 napi_disable(&pp->napi);
3199 netif_carrier_off(pp->dev);
3201 mvneta_port_down(pp);
3202 netif_tx_stop_all_queues(pp->dev);
3204 /* Stop the port activity */
3205 mvneta_port_disable(pp);
3207 /* Clear all ethernet port interrupts */
3208 on_each_cpu(mvneta_percpu_clear_intr_cause, pp, true);
3210 /* Mask all ethernet port interrupts */
3211 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
3213 mvneta_tx_reset(pp);
3214 mvneta_rx_reset(pp);
3217 static void mvneta_percpu_enable(void *arg)
3219 struct mvneta_port *pp = arg;
3221 enable_percpu_irq(pp->dev->irq, IRQ_TYPE_NONE);
3224 static void mvneta_percpu_disable(void *arg)
3226 struct mvneta_port *pp = arg;
3228 disable_percpu_irq(pp->dev->irq);
3231 /* Change the device mtu */
3232 static int mvneta_change_mtu(struct net_device *dev, int mtu)
3234 struct mvneta_port *pp = netdev_priv(dev);
3237 if (!IS_ALIGNED(MVNETA_RX_PKT_SIZE(mtu), 8)) {
3238 netdev_info(dev, "Illegal MTU value %d, rounding to %d\n",
3239 mtu, ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8));
3240 mtu = ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8);
3245 if (!netif_running(dev)) {
3247 mvneta_bm_update_mtu(pp, mtu);
3249 netdev_update_features(dev);
3253 /* The interface is running, so we have to force a
3254 * reallocation of the queues
3256 mvneta_stop_dev(pp);
3257 on_each_cpu(mvneta_percpu_disable, pp, true);
3259 mvneta_cleanup_txqs(pp);
3260 mvneta_cleanup_rxqs(pp);
3263 mvneta_bm_update_mtu(pp, mtu);
3265 pp->pkt_size = MVNETA_RX_PKT_SIZE(dev->mtu);
3267 ret = mvneta_setup_rxqs(pp);
3269 netdev_err(dev, "unable to setup rxqs after MTU change\n");
3273 ret = mvneta_setup_txqs(pp);
3275 netdev_err(dev, "unable to setup txqs after MTU change\n");
3279 on_each_cpu(mvneta_percpu_enable, pp, true);
3280 mvneta_start_dev(pp);
3282 netdev_update_features(dev);
3287 static netdev_features_t mvneta_fix_features(struct net_device *dev,
3288 netdev_features_t features)
3290 struct mvneta_port *pp = netdev_priv(dev);
3292 if (pp->tx_csum_limit && dev->mtu > pp->tx_csum_limit) {
3293 features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
3295 "Disable IP checksum for MTU greater than %dB\n",
3302 /* Get mac address */
3303 static void mvneta_get_mac_addr(struct mvneta_port *pp, unsigned char *addr)
3305 u32 mac_addr_l, mac_addr_h;
3307 mac_addr_l = mvreg_read(pp, MVNETA_MAC_ADDR_LOW);
3308 mac_addr_h = mvreg_read(pp, MVNETA_MAC_ADDR_HIGH);
3309 addr[0] = (mac_addr_h >> 24) & 0xFF;
3310 addr[1] = (mac_addr_h >> 16) & 0xFF;
3311 addr[2] = (mac_addr_h >> 8) & 0xFF;
3312 addr[3] = mac_addr_h & 0xFF;
3313 addr[4] = (mac_addr_l >> 8) & 0xFF;
3314 addr[5] = mac_addr_l & 0xFF;
3317 /* Handle setting mac address */
3318 static int mvneta_set_mac_addr(struct net_device *dev, void *addr)
3320 struct mvneta_port *pp = netdev_priv(dev);
3321 struct sockaddr *sockaddr = addr;
3324 ret = eth_prepare_mac_addr_change(dev, addr);
3327 /* Remove previous address table entry */
3328 mvneta_mac_addr_set(pp, dev->dev_addr, -1);
3330 /* Set new addr in hw */
3331 mvneta_mac_addr_set(pp, sockaddr->sa_data, pp->rxq_def);
3333 eth_commit_mac_addr_change(dev, addr);
3337 static void mvneta_validate(struct net_device *ndev, unsigned long *supported,
3338 struct phylink_link_state *state)
3340 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
3342 /* We only support QSGMII, SGMII, 802.3z and RGMII modes */
3343 if (state->interface != PHY_INTERFACE_MODE_NA &&
3344 state->interface != PHY_INTERFACE_MODE_QSGMII &&
3345 state->interface != PHY_INTERFACE_MODE_SGMII &&
3346 state->interface != PHY_INTERFACE_MODE_2500BASEX &&
3347 !phy_interface_mode_is_8023z(state->interface) &&
3348 !phy_interface_mode_is_rgmii(state->interface)) {
3349 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
3353 /* Allow all the expected bits */
3354 phylink_set(mask, Autoneg);
3355 phylink_set_port_modes(mask);
3357 /* Asymmetric pause is unsupported */
3358 phylink_set(mask, Pause);
3360 /* We cannot use 1Gbps when using the 2.5G interface. */
3361 if (state->interface == PHY_INTERFACE_MODE_2500BASEX) {
3362 phylink_set(mask, 2500baseT_Full);
3363 phylink_set(mask, 2500baseX_Full);
3365 phylink_set(mask, 1000baseT_Full);
3366 phylink_set(mask, 1000baseX_Full);
3369 if (!phy_interface_mode_is_8023z(state->interface)) {
3370 /* 10M and 100M are only supported in non-802.3z mode */
3371 phylink_set(mask, 10baseT_Half);
3372 phylink_set(mask, 10baseT_Full);
3373 phylink_set(mask, 100baseT_Half);
3374 phylink_set(mask, 100baseT_Full);
3377 bitmap_and(supported, supported, mask,
3378 __ETHTOOL_LINK_MODE_MASK_NBITS);
3379 bitmap_and(state->advertising, state->advertising, mask,
3380 __ETHTOOL_LINK_MODE_MASK_NBITS);
3383 static int mvneta_mac_link_state(struct net_device *ndev,
3384 struct phylink_link_state *state)
3386 struct mvneta_port *pp = netdev_priv(ndev);
3389 gmac_stat = mvreg_read(pp, MVNETA_GMAC_STATUS);
3391 if (gmac_stat & MVNETA_GMAC_SPEED_1000)
3392 state->speed = SPEED_1000;
3393 else if (gmac_stat & MVNETA_GMAC_SPEED_100)
3394 state->speed = SPEED_100;
3396 state->speed = SPEED_10;
3398 state->an_complete = !!(gmac_stat & MVNETA_GMAC_AN_COMPLETE);
3399 state->link = !!(gmac_stat & MVNETA_GMAC_LINK_UP);
3400 state->duplex = !!(gmac_stat & MVNETA_GMAC_FULL_DUPLEX);
3403 if (gmac_stat & MVNETA_GMAC_RX_FLOW_CTRL_ENABLE)
3404 state->pause |= MLO_PAUSE_RX;
3405 if (gmac_stat & MVNETA_GMAC_TX_FLOW_CTRL_ENABLE)
3406 state->pause |= MLO_PAUSE_TX;
3411 static void mvneta_mac_an_restart(struct net_device *ndev)
3413 struct mvneta_port *pp = netdev_priv(ndev);
3414 u32 gmac_an = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3416 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
3417 gmac_an | MVNETA_GMAC_INBAND_RESTART_AN);
3418 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
3419 gmac_an & ~MVNETA_GMAC_INBAND_RESTART_AN);
3422 static void mvneta_mac_config(struct net_device *ndev, unsigned int mode,
3423 const struct phylink_link_state *state)
3425 struct mvneta_port *pp = netdev_priv(ndev);
3426 u32 new_ctrl0, gmac_ctrl0 = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
3427 u32 new_ctrl2, gmac_ctrl2 = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
3428 u32 new_ctrl4, gmac_ctrl4 = mvreg_read(pp, MVNETA_GMAC_CTRL_4);
3429 u32 new_clk, gmac_clk = mvreg_read(pp, MVNETA_GMAC_CLOCK_DIVIDER);
3430 u32 new_an, gmac_an = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3432 new_ctrl0 = gmac_ctrl0 & ~MVNETA_GMAC0_PORT_1000BASE_X;
3433 new_ctrl2 = gmac_ctrl2 & ~(MVNETA_GMAC2_INBAND_AN_ENABLE |
3434 MVNETA_GMAC2_PORT_RESET);
3435 new_ctrl4 = gmac_ctrl4 & ~(MVNETA_GMAC4_SHORT_PREAMBLE_ENABLE);
3436 new_clk = gmac_clk & ~MVNETA_GMAC_1MS_CLOCK_ENABLE;
3437 new_an = gmac_an & ~(MVNETA_GMAC_INBAND_AN_ENABLE |
3438 MVNETA_GMAC_INBAND_RESTART_AN |
3439 MVNETA_GMAC_CONFIG_MII_SPEED |
3440 MVNETA_GMAC_CONFIG_GMII_SPEED |
3441 MVNETA_GMAC_AN_SPEED_EN |
3442 MVNETA_GMAC_ADVERT_SYM_FLOW_CTRL |
3443 MVNETA_GMAC_CONFIG_FLOW_CTRL |
3444 MVNETA_GMAC_AN_FLOW_CTRL_EN |
3445 MVNETA_GMAC_CONFIG_FULL_DUPLEX |
3446 MVNETA_GMAC_AN_DUPLEX_EN);
3448 /* Even though it might look weird, when we're configured in
3449 * SGMII or QSGMII mode, the RGMII bit needs to be set.
3451 new_ctrl2 |= MVNETA_GMAC2_PORT_RGMII;
3453 if (state->interface == PHY_INTERFACE_MODE_QSGMII ||
3454 state->interface == PHY_INTERFACE_MODE_SGMII ||
3455 phy_interface_mode_is_8023z(state->interface))
3456 new_ctrl2 |= MVNETA_GMAC2_PCS_ENABLE;
3458 if (phylink_test(state->advertising, Pause))
3459 new_an |= MVNETA_GMAC_ADVERT_SYM_FLOW_CTRL;
3460 if (state->pause & MLO_PAUSE_TXRX_MASK)
3461 new_an |= MVNETA_GMAC_CONFIG_FLOW_CTRL;
3463 if (!phylink_autoneg_inband(mode)) {
3464 /* Phy or fixed speed */
3466 new_an |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
3468 if (state->speed == SPEED_1000 || state->speed == SPEED_2500)
3469 new_an |= MVNETA_GMAC_CONFIG_GMII_SPEED;
3470 else if (state->speed == SPEED_100)
3471 new_an |= MVNETA_GMAC_CONFIG_MII_SPEED;
3472 } else if (state->interface == PHY_INTERFACE_MODE_SGMII) {
3473 /* SGMII mode receives the state from the PHY */
3474 new_ctrl2 |= MVNETA_GMAC2_INBAND_AN_ENABLE;
3475 new_clk |= MVNETA_GMAC_1MS_CLOCK_ENABLE;
3476 new_an = (new_an & ~(MVNETA_GMAC_FORCE_LINK_DOWN |
3477 MVNETA_GMAC_FORCE_LINK_PASS)) |
3478 MVNETA_GMAC_INBAND_AN_ENABLE |
3479 MVNETA_GMAC_AN_SPEED_EN |
3480 MVNETA_GMAC_AN_DUPLEX_EN;
3482 /* 802.3z negotiation - only 1000base-X */
3483 new_ctrl0 |= MVNETA_GMAC0_PORT_1000BASE_X;
3484 new_clk |= MVNETA_GMAC_1MS_CLOCK_ENABLE;
3485 new_an = (new_an & ~(MVNETA_GMAC_FORCE_LINK_DOWN |
3486 MVNETA_GMAC_FORCE_LINK_PASS)) |
3487 MVNETA_GMAC_INBAND_AN_ENABLE |
3488 MVNETA_GMAC_CONFIG_GMII_SPEED |
3489 /* The MAC only supports FD mode */
3490 MVNETA_GMAC_CONFIG_FULL_DUPLEX;
3492 if (state->pause & MLO_PAUSE_AN && state->an_enabled)
3493 new_an |= MVNETA_GMAC_AN_FLOW_CTRL_EN;
3496 /* Armada 370 documentation says we can only change the port mode
3497 * and in-band enable when the link is down, so force it down
3498 * while making these changes. We also do this for GMAC_CTRL2 */
3499 if ((new_ctrl0 ^ gmac_ctrl0) & MVNETA_GMAC0_PORT_1000BASE_X ||
3500 (new_ctrl2 ^ gmac_ctrl2) & MVNETA_GMAC2_INBAND_AN_ENABLE ||
3501 (new_an ^ gmac_an) & MVNETA_GMAC_INBAND_AN_ENABLE) {
3502 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
3503 (gmac_an & ~MVNETA_GMAC_FORCE_LINK_PASS) |
3504 MVNETA_GMAC_FORCE_LINK_DOWN);
3507 /* When at 2.5G, the link partner can send frames with shortened
3510 if (state->speed == SPEED_2500)
3511 new_ctrl4 |= MVNETA_GMAC4_SHORT_PREAMBLE_ENABLE;
3513 if (new_ctrl0 != gmac_ctrl0)
3514 mvreg_write(pp, MVNETA_GMAC_CTRL_0, new_ctrl0);
3515 if (new_ctrl2 != gmac_ctrl2)
3516 mvreg_write(pp, MVNETA_GMAC_CTRL_2, new_ctrl2);
3517 if (new_ctrl4 != gmac_ctrl4)
3518 mvreg_write(pp, MVNETA_GMAC_CTRL_4, new_ctrl4);
3519 if (new_clk != gmac_clk)
3520 mvreg_write(pp, MVNETA_GMAC_CLOCK_DIVIDER, new_clk);
3521 if (new_an != gmac_an)
3522 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, new_an);
3524 if (gmac_ctrl2 & MVNETA_GMAC2_PORT_RESET) {
3525 while ((mvreg_read(pp, MVNETA_GMAC_CTRL_2) &
3526 MVNETA_GMAC2_PORT_RESET) != 0)
3531 static void mvneta_set_eee(struct mvneta_port *pp, bool enable)
3535 lpi_ctl1 = mvreg_read(pp, MVNETA_LPI_CTRL_1);
3537 lpi_ctl1 |= MVNETA_LPI_REQUEST_ENABLE;
3539 lpi_ctl1 &= ~MVNETA_LPI_REQUEST_ENABLE;
3540 mvreg_write(pp, MVNETA_LPI_CTRL_1, lpi_ctl1);
3543 static void mvneta_mac_link_down(struct net_device *ndev, unsigned int mode,
3544 phy_interface_t interface)
3546 struct mvneta_port *pp = netdev_priv(ndev);
3549 mvneta_port_down(pp);
3551 if (!phylink_autoneg_inband(mode)) {
3552 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3553 val &= ~MVNETA_GMAC_FORCE_LINK_PASS;
3554 val |= MVNETA_GMAC_FORCE_LINK_DOWN;
3555 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
3558 pp->eee_active = false;
3559 mvneta_set_eee(pp, false);
3562 static void mvneta_mac_link_up(struct net_device *ndev, unsigned int mode,
3563 phy_interface_t interface,
3564 struct phy_device *phy)
3566 struct mvneta_port *pp = netdev_priv(ndev);
3569 if (!phylink_autoneg_inband(mode)) {
3570 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3571 val &= ~MVNETA_GMAC_FORCE_LINK_DOWN;
3572 val |= MVNETA_GMAC_FORCE_LINK_PASS;
3573 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
3578 if (phy && pp->eee_enabled) {
3579 pp->eee_active = phy_init_eee(phy, 0) >= 0;
3580 mvneta_set_eee(pp, pp->eee_active && pp->tx_lpi_enabled);
3584 static const struct phylink_mac_ops mvneta_phylink_ops = {
3585 .validate = mvneta_validate,
3586 .mac_link_state = mvneta_mac_link_state,
3587 .mac_an_restart = mvneta_mac_an_restart,
3588 .mac_config = mvneta_mac_config,
3589 .mac_link_down = mvneta_mac_link_down,
3590 .mac_link_up = mvneta_mac_link_up,
3593 static int mvneta_mdio_probe(struct mvneta_port *pp)
3595 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
3596 int err = phylink_of_phy_connect(pp->phylink, pp->dn, 0);
3599 netdev_err(pp->dev, "could not attach PHY: %d\n", err);
3601 phylink_ethtool_get_wol(pp->phylink, &wol);
3602 device_set_wakeup_capable(&pp->dev->dev, !!wol.supported);
3607 static void mvneta_mdio_remove(struct mvneta_port *pp)
3609 phylink_disconnect_phy(pp->phylink);
3612 /* Electing a CPU must be done in an atomic way: it should be done
3613 * after or before the removal/insertion of a CPU and this function is
3616 static void mvneta_percpu_elect(struct mvneta_port *pp)
3618 int elected_cpu = 0, max_cpu, cpu, i = 0;
3620 /* Use the cpu associated to the rxq when it is online, in all
3621 * the other cases, use the cpu 0 which can't be offline.
3623 if (cpu_online(pp->rxq_def))
3624 elected_cpu = pp->rxq_def;
3626 max_cpu = num_present_cpus();
3628 for_each_online_cpu(cpu) {
3629 int rxq_map = 0, txq_map = 0;
3632 for (rxq = 0; rxq < rxq_number; rxq++)
3633 if ((rxq % max_cpu) == cpu)
3634 rxq_map |= MVNETA_CPU_RXQ_ACCESS(rxq);
3636 if (cpu == elected_cpu)
3637 /* Map the default receive queue queue to the
3640 rxq_map |= MVNETA_CPU_RXQ_ACCESS(pp->rxq_def);
3642 /* We update the TX queue map only if we have one
3643 * queue. In this case we associate the TX queue to
3644 * the CPU bound to the default RX queue
3646 if (txq_number == 1)
3647 txq_map = (cpu == elected_cpu) ?
3648 MVNETA_CPU_TXQ_ACCESS(1) : 0;
3650 txq_map = mvreg_read(pp, MVNETA_CPU_MAP(cpu)) &
3651 MVNETA_CPU_TXQ_ACCESS_ALL_MASK;
3653 mvreg_write(pp, MVNETA_CPU_MAP(cpu), rxq_map | txq_map);
3655 /* Update the interrupt mask on each CPU according the
3658 smp_call_function_single(cpu, mvneta_percpu_unmask_interrupt,
3665 static int mvneta_cpu_online(unsigned int cpu, struct hlist_node *node)
3668 struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
3670 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
3673 spin_lock(&pp->lock);
3675 * Configuring the driver for a new CPU while the driver is
3676 * stopping is racy, so just avoid it.
3678 if (pp->is_stopped) {
3679 spin_unlock(&pp->lock);
3682 netif_tx_stop_all_queues(pp->dev);
3685 * We have to synchronise on tha napi of each CPU except the one
3686 * just being woken up
3688 for_each_online_cpu(other_cpu) {
3689 if (other_cpu != cpu) {
3690 struct mvneta_pcpu_port *other_port =
3691 per_cpu_ptr(pp->ports, other_cpu);
3693 napi_synchronize(&other_port->napi);
3697 /* Mask all ethernet port interrupts */
3698 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
3699 napi_enable(&port->napi);
3702 * Enable per-CPU interrupts on the CPU that is
3705 mvneta_percpu_enable(pp);
3708 * Enable per-CPU interrupt on the one CPU we care
3711 mvneta_percpu_elect(pp);
3713 /* Unmask all ethernet port interrupts */
3714 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
3715 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
3716 MVNETA_CAUSE_PHY_STATUS_CHANGE |
3717 MVNETA_CAUSE_LINK_CHANGE);
3718 netif_tx_start_all_queues(pp->dev);
3719 spin_unlock(&pp->lock);
3723 static int mvneta_cpu_down_prepare(unsigned int cpu, struct hlist_node *node)
3725 struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
3727 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
3730 * Thanks to this lock we are sure that any pending cpu election is
3733 spin_lock(&pp->lock);
3734 /* Mask all ethernet port interrupts */
3735 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
3736 spin_unlock(&pp->lock);
3738 napi_synchronize(&port->napi);
3739 napi_disable(&port->napi);
3740 /* Disable per-CPU interrupts on the CPU that is brought down. */
3741 mvneta_percpu_disable(pp);
3745 static int mvneta_cpu_dead(unsigned int cpu, struct hlist_node *node)
3747 struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
3750 /* Check if a new CPU must be elected now this on is down */
3751 spin_lock(&pp->lock);
3752 mvneta_percpu_elect(pp);
3753 spin_unlock(&pp->lock);
3754 /* Unmask all ethernet port interrupts */
3755 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
3756 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
3757 MVNETA_CAUSE_PHY_STATUS_CHANGE |
3758 MVNETA_CAUSE_LINK_CHANGE);
3759 netif_tx_start_all_queues(pp->dev);
3763 static int mvneta_open(struct net_device *dev)
3765 struct mvneta_port *pp = netdev_priv(dev);
3768 pp->pkt_size = MVNETA_RX_PKT_SIZE(pp->dev->mtu);
3769 pp->frag_size = PAGE_SIZE;
3771 ret = mvneta_setup_rxqs(pp);
3775 ret = mvneta_setup_txqs(pp);
3777 goto err_cleanup_rxqs;
3779 /* Connect to port interrupt line */
3780 if (pp->neta_armada3700)
3781 ret = request_irq(pp->dev->irq, mvneta_isr, 0,
3784 ret = request_percpu_irq(pp->dev->irq, mvneta_percpu_isr,
3785 dev->name, pp->ports);
3787 netdev_err(pp->dev, "cannot request irq %d\n", pp->dev->irq);
3788 goto err_cleanup_txqs;
3791 if (!pp->neta_armada3700) {
3792 /* Enable per-CPU interrupt on all the CPU to handle our RX
3795 on_each_cpu(mvneta_percpu_enable, pp, true);
3797 pp->is_stopped = false;
3798 /* Register a CPU notifier to handle the case where our CPU
3799 * might be taken offline.
3801 ret = cpuhp_state_add_instance_nocalls(online_hpstate,
3806 ret = cpuhp_state_add_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
3809 goto err_free_online_hp;
3812 ret = mvneta_mdio_probe(pp);
3814 netdev_err(dev, "cannot probe MDIO bus\n");
3815 goto err_free_dead_hp;
3818 mvneta_start_dev(pp);
3823 if (!pp->neta_armada3700)
3824 cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
3827 if (!pp->neta_armada3700)
3828 cpuhp_state_remove_instance_nocalls(online_hpstate,
3831 if (pp->neta_armada3700) {
3832 free_irq(pp->dev->irq, pp);
3834 on_each_cpu(mvneta_percpu_disable, pp, true);
3835 free_percpu_irq(pp->dev->irq, pp->ports);
3838 mvneta_cleanup_txqs(pp);
3840 mvneta_cleanup_rxqs(pp);
3844 /* Stop the port, free port interrupt line */
3845 static int mvneta_stop(struct net_device *dev)
3847 struct mvneta_port *pp = netdev_priv(dev);
3849 if (!pp->neta_armada3700) {
3850 /* Inform that we are stopping so we don't want to setup the
3851 * driver for new CPUs in the notifiers. The code of the
3852 * notifier for CPU online is protected by the same spinlock,
3853 * so when we get the lock, the notifer work is done.
3855 spin_lock(&pp->lock);
3856 pp->is_stopped = true;
3857 spin_unlock(&pp->lock);
3859 mvneta_stop_dev(pp);
3860 mvneta_mdio_remove(pp);
3862 cpuhp_state_remove_instance_nocalls(online_hpstate,
3864 cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
3866 on_each_cpu(mvneta_percpu_disable, pp, true);
3867 free_percpu_irq(dev->irq, pp->ports);
3869 mvneta_stop_dev(pp);
3870 mvneta_mdio_remove(pp);
3871 free_irq(dev->irq, pp);
3874 mvneta_cleanup_rxqs(pp);
3875 mvneta_cleanup_txqs(pp);
3880 static int mvneta_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3882 struct mvneta_port *pp = netdev_priv(dev);
3884 return phylink_mii_ioctl(pp->phylink, ifr, cmd);
3887 /* Ethtool methods */
3889 /* Set link ksettings (phy address, speed) for ethtools */
3891 mvneta_ethtool_set_link_ksettings(struct net_device *ndev,
3892 const struct ethtool_link_ksettings *cmd)
3894 struct mvneta_port *pp = netdev_priv(ndev);
3896 return phylink_ethtool_ksettings_set(pp->phylink, cmd);
3899 /* Get link ksettings for ethtools */
3901 mvneta_ethtool_get_link_ksettings(struct net_device *ndev,
3902 struct ethtool_link_ksettings *cmd)
3904 struct mvneta_port *pp = netdev_priv(ndev);
3906 return phylink_ethtool_ksettings_get(pp->phylink, cmd);
3909 static int mvneta_ethtool_nway_reset(struct net_device *dev)
3911 struct mvneta_port *pp = netdev_priv(dev);
3913 return phylink_ethtool_nway_reset(pp->phylink);
3916 /* Set interrupt coalescing for ethtools */
3917 static int mvneta_ethtool_set_coalesce(struct net_device *dev,
3918 struct ethtool_coalesce *c)
3920 struct mvneta_port *pp = netdev_priv(dev);
3923 for (queue = 0; queue < rxq_number; queue++) {
3924 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
3925 rxq->time_coal = c->rx_coalesce_usecs;
3926 rxq->pkts_coal = c->rx_max_coalesced_frames;
3927 mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
3928 mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
3931 for (queue = 0; queue < txq_number; queue++) {
3932 struct mvneta_tx_queue *txq = &pp->txqs[queue];
3933 txq->done_pkts_coal = c->tx_max_coalesced_frames;
3934 mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
3940 /* get coalescing for ethtools */
3941 static int mvneta_ethtool_get_coalesce(struct net_device *dev,
3942 struct ethtool_coalesce *c)
3944 struct mvneta_port *pp = netdev_priv(dev);
3946 c->rx_coalesce_usecs = pp->rxqs[0].time_coal;
3947 c->rx_max_coalesced_frames = pp->rxqs[0].pkts_coal;
3949 c->tx_max_coalesced_frames = pp->txqs[0].done_pkts_coal;
3954 static void mvneta_ethtool_get_drvinfo(struct net_device *dev,
3955 struct ethtool_drvinfo *drvinfo)
3957 strlcpy(drvinfo->driver, MVNETA_DRIVER_NAME,
3958 sizeof(drvinfo->driver));
3959 strlcpy(drvinfo->version, MVNETA_DRIVER_VERSION,
3960 sizeof(drvinfo->version));
3961 strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
3962 sizeof(drvinfo->bus_info));
3966 static void mvneta_ethtool_get_ringparam(struct net_device *netdev,
3967 struct ethtool_ringparam *ring)
3969 struct mvneta_port *pp = netdev_priv(netdev);
3971 ring->rx_max_pending = MVNETA_MAX_RXD;
3972 ring->tx_max_pending = MVNETA_MAX_TXD;
3973 ring->rx_pending = pp->rx_ring_size;
3974 ring->tx_pending = pp->tx_ring_size;
3977 static int mvneta_ethtool_set_ringparam(struct net_device *dev,
3978 struct ethtool_ringparam *ring)
3980 struct mvneta_port *pp = netdev_priv(dev);
3982 if ((ring->rx_pending == 0) || (ring->tx_pending == 0))
3984 pp->rx_ring_size = ring->rx_pending < MVNETA_MAX_RXD ?
3985 ring->rx_pending : MVNETA_MAX_RXD;
3987 pp->tx_ring_size = clamp_t(u16, ring->tx_pending,
3988 MVNETA_MAX_SKB_DESCS * 2, MVNETA_MAX_TXD);
3989 if (pp->tx_ring_size != ring->tx_pending)
3990 netdev_warn(dev, "TX queue size set to %u (requested %u)\n",
3991 pp->tx_ring_size, ring->tx_pending);
3993 if (netif_running(dev)) {
3995 if (mvneta_open(dev)) {
3997 "error on opening device after ring param change\n");
4005 static void mvneta_ethtool_get_pauseparam(struct net_device *dev,
4006 struct ethtool_pauseparam *pause)
4008 struct mvneta_port *pp = netdev_priv(dev);
4010 phylink_ethtool_get_pauseparam(pp->phylink, pause);
4013 static int mvneta_ethtool_set_pauseparam(struct net_device *dev,
4014 struct ethtool_pauseparam *pause)
4016 struct mvneta_port *pp = netdev_priv(dev);
4018 return phylink_ethtool_set_pauseparam(pp->phylink, pause);
4021 static void mvneta_ethtool_get_strings(struct net_device *netdev, u32 sset,
4024 if (sset == ETH_SS_STATS) {
4027 for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++)
4028 memcpy(data + i * ETH_GSTRING_LEN,
4029 mvneta_statistics[i].name, ETH_GSTRING_LEN);
4033 static void mvneta_ethtool_update_stats(struct mvneta_port *pp)
4035 const struct mvneta_statistic *s;
4036 void __iomem *base = pp->base;
4041 for (i = 0, s = mvneta_statistics;
4042 s < mvneta_statistics + ARRAY_SIZE(mvneta_statistics);
4048 val = readl_relaxed(base + s->offset);
4051 /* Docs say to read low 32-bit then high */
4052 low = readl_relaxed(base + s->offset);
4053 high = readl_relaxed(base + s->offset + 4);
4054 val = (u64)high << 32 | low;
4057 switch (s->offset) {
4058 case ETHTOOL_STAT_EEE_WAKEUP:
4059 val = phylink_get_eee_err(pp->phylink);
4061 case ETHTOOL_STAT_SKB_ALLOC_ERR:
4062 val = pp->rxqs[0].skb_alloc_err;
4064 case ETHTOOL_STAT_REFILL_ERR:
4065 val = pp->rxqs[0].refill_err;
4071 pp->ethtool_stats[i] += val;
4075 static void mvneta_ethtool_get_stats(struct net_device *dev,
4076 struct ethtool_stats *stats, u64 *data)
4078 struct mvneta_port *pp = netdev_priv(dev);
4081 mvneta_ethtool_update_stats(pp);
4083 for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++)
4084 *data++ = pp->ethtool_stats[i];
4087 static int mvneta_ethtool_get_sset_count(struct net_device *dev, int sset)
4089 if (sset == ETH_SS_STATS)
4090 return ARRAY_SIZE(mvneta_statistics);
4094 static u32 mvneta_ethtool_get_rxfh_indir_size(struct net_device *dev)
4096 return MVNETA_RSS_LU_TABLE_SIZE;
4099 static int mvneta_ethtool_get_rxnfc(struct net_device *dev,
4100 struct ethtool_rxnfc *info,
4101 u32 *rules __always_unused)
4103 switch (info->cmd) {
4104 case ETHTOOL_GRXRINGS:
4105 info->data = rxq_number;
4114 static int mvneta_config_rss(struct mvneta_port *pp)
4119 netif_tx_stop_all_queues(pp->dev);
4121 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
4123 if (!pp->neta_armada3700) {
4124 /* We have to synchronise on the napi of each CPU */
4125 for_each_online_cpu(cpu) {
4126 struct mvneta_pcpu_port *pcpu_port =
4127 per_cpu_ptr(pp->ports, cpu);
4129 napi_synchronize(&pcpu_port->napi);
4130 napi_disable(&pcpu_port->napi);
4133 napi_synchronize(&pp->napi);
4134 napi_disable(&pp->napi);
4137 pp->rxq_def = pp->indir[0];
4139 /* Update unicast mapping */
4140 mvneta_set_rx_mode(pp->dev);
4142 /* Update val of portCfg register accordingly with all RxQueue types */
4143 val = MVNETA_PORT_CONFIG_DEFL_VALUE(pp->rxq_def);
4144 mvreg_write(pp, MVNETA_PORT_CONFIG, val);
4146 /* Update the elected CPU matching the new rxq_def */
4147 spin_lock(&pp->lock);
4148 mvneta_percpu_elect(pp);
4149 spin_unlock(&pp->lock);
4151 if (!pp->neta_armada3700) {
4152 /* We have to synchronise on the napi of each CPU */
4153 for_each_online_cpu(cpu) {
4154 struct mvneta_pcpu_port *pcpu_port =
4155 per_cpu_ptr(pp->ports, cpu);
4157 napi_enable(&pcpu_port->napi);
4160 napi_enable(&pp->napi);
4163 netif_tx_start_all_queues(pp->dev);
4168 static int mvneta_ethtool_set_rxfh(struct net_device *dev, const u32 *indir,
4169 const u8 *key, const u8 hfunc)
4171 struct mvneta_port *pp = netdev_priv(dev);
4173 /* Current code for Armada 3700 doesn't support RSS features yet */
4174 if (pp->neta_armada3700)
4177 /* We require at least one supported parameter to be changed
4178 * and no change in any of the unsupported parameters
4181 (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
4187 memcpy(pp->indir, indir, MVNETA_RSS_LU_TABLE_SIZE);
4189 return mvneta_config_rss(pp);
4192 static int mvneta_ethtool_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
4195 struct mvneta_port *pp = netdev_priv(dev);
4197 /* Current code for Armada 3700 doesn't support RSS features yet */
4198 if (pp->neta_armada3700)
4202 *hfunc = ETH_RSS_HASH_TOP;
4207 memcpy(indir, pp->indir, MVNETA_RSS_LU_TABLE_SIZE);
4212 static void mvneta_ethtool_get_wol(struct net_device *dev,
4213 struct ethtool_wolinfo *wol)
4215 struct mvneta_port *pp = netdev_priv(dev);
4217 phylink_ethtool_get_wol(pp->phylink, wol);
4220 static int mvneta_ethtool_set_wol(struct net_device *dev,
4221 struct ethtool_wolinfo *wol)
4223 struct mvneta_port *pp = netdev_priv(dev);
4226 ret = phylink_ethtool_set_wol(pp->phylink, wol);
4228 device_set_wakeup_enable(&dev->dev, !!wol->wolopts);
4233 static int mvneta_ethtool_get_eee(struct net_device *dev,
4234 struct ethtool_eee *eee)
4236 struct mvneta_port *pp = netdev_priv(dev);
4239 lpi_ctl0 = mvreg_read(pp, MVNETA_LPI_CTRL_0);
4241 eee->eee_enabled = pp->eee_enabled;
4242 eee->eee_active = pp->eee_active;
4243 eee->tx_lpi_enabled = pp->tx_lpi_enabled;
4244 eee->tx_lpi_timer = (lpi_ctl0) >> 8; // * scale;
4246 return phylink_ethtool_get_eee(pp->phylink, eee);
4249 static int mvneta_ethtool_set_eee(struct net_device *dev,
4250 struct ethtool_eee *eee)
4252 struct mvneta_port *pp = netdev_priv(dev);
4255 /* The Armada 37x documents do not give limits for this other than
4256 * it being an 8-bit register. */
4257 if (eee->tx_lpi_enabled &&
4258 (eee->tx_lpi_timer < 0 || eee->tx_lpi_timer > 255))
4261 lpi_ctl0 = mvreg_read(pp, MVNETA_LPI_CTRL_0);
4262 lpi_ctl0 &= ~(0xff << 8);
4263 lpi_ctl0 |= eee->tx_lpi_timer << 8;
4264 mvreg_write(pp, MVNETA_LPI_CTRL_0, lpi_ctl0);
4266 pp->eee_enabled = eee->eee_enabled;
4267 pp->tx_lpi_enabled = eee->tx_lpi_enabled;
4269 mvneta_set_eee(pp, eee->tx_lpi_enabled && eee->eee_enabled);
4271 return phylink_ethtool_set_eee(pp->phylink, eee);
4274 static const struct net_device_ops mvneta_netdev_ops = {
4275 .ndo_open = mvneta_open,
4276 .ndo_stop = mvneta_stop,
4277 .ndo_start_xmit = mvneta_tx,
4278 .ndo_set_rx_mode = mvneta_set_rx_mode,
4279 .ndo_set_mac_address = mvneta_set_mac_addr,
4280 .ndo_change_mtu = mvneta_change_mtu,
4281 .ndo_fix_features = mvneta_fix_features,
4282 .ndo_get_stats64 = mvneta_get_stats64,
4283 .ndo_do_ioctl = mvneta_ioctl,
4286 static const struct ethtool_ops mvneta_eth_tool_ops = {
4287 .nway_reset = mvneta_ethtool_nway_reset,
4288 .get_link = ethtool_op_get_link,
4289 .set_coalesce = mvneta_ethtool_set_coalesce,
4290 .get_coalesce = mvneta_ethtool_get_coalesce,
4291 .get_drvinfo = mvneta_ethtool_get_drvinfo,
4292 .get_ringparam = mvneta_ethtool_get_ringparam,
4293 .set_ringparam = mvneta_ethtool_set_ringparam,
4294 .get_pauseparam = mvneta_ethtool_get_pauseparam,
4295 .set_pauseparam = mvneta_ethtool_set_pauseparam,
4296 .get_strings = mvneta_ethtool_get_strings,
4297 .get_ethtool_stats = mvneta_ethtool_get_stats,
4298 .get_sset_count = mvneta_ethtool_get_sset_count,
4299 .get_rxfh_indir_size = mvneta_ethtool_get_rxfh_indir_size,
4300 .get_rxnfc = mvneta_ethtool_get_rxnfc,
4301 .get_rxfh = mvneta_ethtool_get_rxfh,
4302 .set_rxfh = mvneta_ethtool_set_rxfh,
4303 .get_link_ksettings = mvneta_ethtool_get_link_ksettings,
4304 .set_link_ksettings = mvneta_ethtool_set_link_ksettings,
4305 .get_wol = mvneta_ethtool_get_wol,
4306 .set_wol = mvneta_ethtool_set_wol,
4307 .get_eee = mvneta_ethtool_get_eee,
4308 .set_eee = mvneta_ethtool_set_eee,
4312 static int mvneta_init(struct device *dev, struct mvneta_port *pp)
4317 mvneta_port_disable(pp);
4319 /* Set port default values */
4320 mvneta_defaults_set(pp);
4322 pp->txqs = devm_kcalloc(dev, txq_number, sizeof(*pp->txqs), GFP_KERNEL);
4326 /* Initialize TX descriptor rings */
4327 for (queue = 0; queue < txq_number; queue++) {
4328 struct mvneta_tx_queue *txq = &pp->txqs[queue];
4330 txq->size = pp->tx_ring_size;
4331 txq->done_pkts_coal = MVNETA_TXDONE_COAL_PKTS;
4334 pp->rxqs = devm_kcalloc(dev, rxq_number, sizeof(*pp->rxqs), GFP_KERNEL);
4338 /* Create Rx descriptor rings */
4339 for (queue = 0; queue < rxq_number; queue++) {
4340 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
4342 rxq->size = pp->rx_ring_size;
4343 rxq->pkts_coal = MVNETA_RX_COAL_PKTS;
4344 rxq->time_coal = MVNETA_RX_COAL_USEC;
4346 = devm_kmalloc_array(pp->dev->dev.parent,
4348 sizeof(*rxq->buf_virt_addr),
4350 if (!rxq->buf_virt_addr)
4357 /* platform glue : initialize decoding windows */
4358 static void mvneta_conf_mbus_windows(struct mvneta_port *pp,
4359 const struct mbus_dram_target_info *dram)
4365 for (i = 0; i < 6; i++) {
4366 mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
4367 mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
4370 mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
4377 for (i = 0; i < dram->num_cs; i++) {
4378 const struct mbus_dram_window *cs = dram->cs + i;
4380 mvreg_write(pp, MVNETA_WIN_BASE(i),
4381 (cs->base & 0xffff0000) |
4382 (cs->mbus_attr << 8) |
4383 dram->mbus_dram_target_id);
4385 mvreg_write(pp, MVNETA_WIN_SIZE(i),
4386 (cs->size - 1) & 0xffff0000);
4388 win_enable &= ~(1 << i);
4389 win_protect |= 3 << (2 * i);
4392 /* For Armada3700 open default 4GB Mbus window, leaving
4393 * arbitration of target/attribute to a different layer
4396 mvreg_write(pp, MVNETA_WIN_SIZE(0), 0xffff0000);
4397 win_enable &= ~BIT(0);
4401 mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
4402 mvreg_write(pp, MVNETA_ACCESS_PROTECT_ENABLE, win_protect);
4405 /* Power up the port */
4406 static int mvneta_port_power_up(struct mvneta_port *pp, int phy_mode)
4408 /* MAC Cause register should be cleared */
4409 mvreg_write(pp, MVNETA_UNIT_INTR_CAUSE, 0);
4411 if (phy_mode == PHY_INTERFACE_MODE_QSGMII)
4412 mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_QSGMII_SERDES_PROTO);
4413 else if (phy_mode == PHY_INTERFACE_MODE_SGMII ||
4414 phy_mode == PHY_INTERFACE_MODE_1000BASEX)
4415 mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_SGMII_SERDES_PROTO);
4416 else if (!phy_interface_mode_is_rgmii(phy_mode))
4422 /* Device initialization routine */
4423 static int mvneta_probe(struct platform_device *pdev)
4425 struct resource *res;
4426 struct device_node *dn = pdev->dev.of_node;
4427 struct device_node *bm_node;
4428 struct mvneta_port *pp;
4429 struct net_device *dev;
4430 struct phylink *phylink;
4431 const char *dt_mac_addr;
4432 char hw_mac_addr[ETH_ALEN];
4433 const char *mac_from;
4439 dev = alloc_etherdev_mqs(sizeof(struct mvneta_port), txq_number, rxq_number);
4443 dev->irq = irq_of_parse_and_map(dn, 0);
4444 if (dev->irq == 0) {
4446 goto err_free_netdev;
4449 phy_mode = of_get_phy_mode(dn);
4451 dev_err(&pdev->dev, "incorrect phy-mode\n");
4456 phylink = phylink_create(dev, pdev->dev.fwnode, phy_mode,
4457 &mvneta_phylink_ops);
4458 if (IS_ERR(phylink)) {
4459 err = PTR_ERR(phylink);
4463 dev->tx_queue_len = MVNETA_MAX_TXD;
4464 dev->watchdog_timeo = 5 * HZ;
4465 dev->netdev_ops = &mvneta_netdev_ops;
4467 dev->ethtool_ops = &mvneta_eth_tool_ops;
4469 pp = netdev_priv(dev);
4470 spin_lock_init(&pp->lock);
4471 pp->phylink = phylink;
4472 pp->phy_interface = phy_mode;
4475 pp->rxq_def = rxq_def;
4476 pp->indir[0] = rxq_def;
4478 /* Get special SoC configurations */
4479 if (of_device_is_compatible(dn, "marvell,armada-3700-neta"))
4480 pp->neta_armada3700 = true;
4482 pp->clk = devm_clk_get(&pdev->dev, "core");
4483 if (IS_ERR(pp->clk))
4484 pp->clk = devm_clk_get(&pdev->dev, NULL);
4485 if (IS_ERR(pp->clk)) {
4486 err = PTR_ERR(pp->clk);
4487 goto err_free_phylink;
4490 clk_prepare_enable(pp->clk);
4492 pp->clk_bus = devm_clk_get(&pdev->dev, "bus");
4493 if (!IS_ERR(pp->clk_bus))
4494 clk_prepare_enable(pp->clk_bus);
4496 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4497 pp->base = devm_ioremap_resource(&pdev->dev, res);
4498 if (IS_ERR(pp->base)) {
4499 err = PTR_ERR(pp->base);
4503 /* Alloc per-cpu port structure */
4504 pp->ports = alloc_percpu(struct mvneta_pcpu_port);
4510 /* Alloc per-cpu stats */
4511 pp->stats = netdev_alloc_pcpu_stats(struct mvneta_pcpu_stats);
4514 goto err_free_ports;
4517 dt_mac_addr = of_get_mac_address(dn);
4519 mac_from = "device tree";
4520 memcpy(dev->dev_addr, dt_mac_addr, ETH_ALEN);
4522 mvneta_get_mac_addr(pp, hw_mac_addr);
4523 if (is_valid_ether_addr(hw_mac_addr)) {
4524 mac_from = "hardware";
4525 memcpy(dev->dev_addr, hw_mac_addr, ETH_ALEN);
4527 mac_from = "random";
4528 eth_hw_addr_random(dev);
4532 if (!of_property_read_u32(dn, "tx-csum-limit", &tx_csum_limit)) {
4533 if (tx_csum_limit < 0 ||
4534 tx_csum_limit > MVNETA_TX_CSUM_MAX_SIZE) {
4535 tx_csum_limit = MVNETA_TX_CSUM_DEF_SIZE;
4536 dev_info(&pdev->dev,
4537 "Wrong TX csum limit in DT, set to %dB\n",
4538 MVNETA_TX_CSUM_DEF_SIZE);
4540 } else if (of_device_is_compatible(dn, "marvell,armada-370-neta")) {
4541 tx_csum_limit = MVNETA_TX_CSUM_DEF_SIZE;
4543 tx_csum_limit = MVNETA_TX_CSUM_MAX_SIZE;
4546 pp->tx_csum_limit = tx_csum_limit;
4548 pp->dram_target_info = mv_mbus_dram_info();
4549 /* Armada3700 requires setting default configuration of Mbus
4550 * windows, however without using filled mbus_dram_target_info
4553 if (pp->dram_target_info || pp->neta_armada3700)
4554 mvneta_conf_mbus_windows(pp, pp->dram_target_info);
4556 pp->tx_ring_size = MVNETA_MAX_TXD;
4557 pp->rx_ring_size = MVNETA_MAX_RXD;
4560 SET_NETDEV_DEV(dev, &pdev->dev);
4562 pp->id = global_port_id++;
4563 pp->rx_offset_correction = 0; /* not relevant for SW BM */
4565 /* Obtain access to BM resources if enabled and already initialized */
4566 bm_node = of_parse_phandle(dn, "buffer-manager", 0);
4568 pp->bm_priv = mvneta_bm_get(bm_node);
4570 err = mvneta_bm_port_init(pdev, pp);
4572 dev_info(&pdev->dev,
4573 "use SW buffer management\n");
4574 mvneta_bm_put(pp->bm_priv);
4578 /* Set RX packet offset correction for platforms, whose
4579 * NET_SKB_PAD, exceeds 64B. It should be 64B for 64-bit
4580 * platforms and 0B for 32-bit ones.
4582 pp->rx_offset_correction = max(0,
4584 MVNETA_RX_PKT_OFFSET_CORRECTION);
4586 of_node_put(bm_node);
4588 err = mvneta_init(&pdev->dev, pp);
4592 err = mvneta_port_power_up(pp, phy_mode);
4594 dev_err(&pdev->dev, "can't power up port\n");
4598 /* Armada3700 network controller does not support per-cpu
4599 * operation, so only single NAPI should be initialized.
4601 if (pp->neta_armada3700) {
4602 netif_napi_add(dev, &pp->napi, mvneta_poll, NAPI_POLL_WEIGHT);
4604 for_each_present_cpu(cpu) {
4605 struct mvneta_pcpu_port *port =
4606 per_cpu_ptr(pp->ports, cpu);
4608 netif_napi_add(dev, &port->napi, mvneta_poll,
4614 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
4615 NETIF_F_TSO | NETIF_F_RXCSUM;
4616 dev->hw_features |= dev->features;
4617 dev->vlan_features |= dev->features;
4618 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
4619 dev->gso_max_segs = MVNETA_MAX_TSO_SEGS;
4621 /* MTU range: 68 - 9676 */
4622 dev->min_mtu = ETH_MIN_MTU;
4623 /* 9676 == 9700 - 20 and rounding to 8 */
4624 dev->max_mtu = 9676;
4626 err = register_netdev(dev);
4628 dev_err(&pdev->dev, "failed to register\n");
4629 goto err_free_stats;
4632 netdev_info(dev, "Using %s mac address %pM\n", mac_from,
4635 platform_set_drvdata(pdev, pp->dev);
4640 unregister_netdev(dev);
4642 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
4643 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short,
4645 mvneta_bm_put(pp->bm_priv);
4648 free_percpu(pp->stats);
4650 free_percpu(pp->ports);
4652 clk_disable_unprepare(pp->clk_bus);
4653 clk_disable_unprepare(pp->clk);
4656 phylink_destroy(pp->phylink);
4658 irq_dispose_mapping(dev->irq);
4664 /* Device removal routine */
4665 static int mvneta_remove(struct platform_device *pdev)
4667 struct net_device *dev = platform_get_drvdata(pdev);
4668 struct mvneta_port *pp = netdev_priv(dev);
4670 unregister_netdev(dev);
4671 clk_disable_unprepare(pp->clk_bus);
4672 clk_disable_unprepare(pp->clk);
4673 free_percpu(pp->ports);
4674 free_percpu(pp->stats);
4675 irq_dispose_mapping(dev->irq);
4676 phylink_destroy(pp->phylink);
4680 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
4681 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short,
4683 mvneta_bm_put(pp->bm_priv);
4689 #ifdef CONFIG_PM_SLEEP
4690 static int mvneta_suspend(struct device *device)
4693 struct net_device *dev = dev_get_drvdata(device);
4694 struct mvneta_port *pp = netdev_priv(dev);
4696 if (!netif_running(dev))
4699 if (!pp->neta_armada3700) {
4700 spin_lock(&pp->lock);
4701 pp->is_stopped = true;
4702 spin_unlock(&pp->lock);
4704 cpuhp_state_remove_instance_nocalls(online_hpstate,
4706 cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
4711 mvneta_stop_dev(pp);
4714 for (queue = 0; queue < rxq_number; queue++) {
4715 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
4717 mvneta_rxq_drop_pkts(pp, rxq);
4720 for (queue = 0; queue < txq_number; queue++) {
4721 struct mvneta_tx_queue *txq = &pp->txqs[queue];
4723 mvneta_txq_hw_deinit(pp, txq);
4727 netif_device_detach(dev);
4728 clk_disable_unprepare(pp->clk_bus);
4729 clk_disable_unprepare(pp->clk);
4734 static int mvneta_resume(struct device *device)
4736 struct platform_device *pdev = to_platform_device(device);
4737 struct net_device *dev = dev_get_drvdata(device);
4738 struct mvneta_port *pp = netdev_priv(dev);
4741 clk_prepare_enable(pp->clk);
4742 if (!IS_ERR(pp->clk_bus))
4743 clk_prepare_enable(pp->clk_bus);
4744 if (pp->dram_target_info || pp->neta_armada3700)
4745 mvneta_conf_mbus_windows(pp, pp->dram_target_info);
4747 err = mvneta_bm_port_init(pdev, pp);
4749 dev_info(&pdev->dev, "use SW buffer management\n");
4753 mvneta_defaults_set(pp);
4754 err = mvneta_port_power_up(pp, pp->phy_interface);
4756 dev_err(device, "can't power up port\n");
4760 netif_device_attach(dev);
4762 if (!netif_running(dev))
4765 for (queue = 0; queue < rxq_number; queue++) {
4766 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
4768 rxq->next_desc_to_proc = 0;
4769 mvneta_rxq_hw_init(pp, rxq);
4772 for (queue = 0; queue < txq_number; queue++) {
4773 struct mvneta_tx_queue *txq = &pp->txqs[queue];
4775 txq->next_desc_to_proc = 0;
4776 mvneta_txq_hw_init(pp, txq);
4779 if (!pp->neta_armada3700) {
4780 spin_lock(&pp->lock);
4781 pp->is_stopped = false;
4782 spin_unlock(&pp->lock);
4783 cpuhp_state_add_instance_nocalls(online_hpstate,
4785 cpuhp_state_add_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
4790 mvneta_start_dev(pp);
4792 mvneta_set_rx_mode(dev);
4798 static SIMPLE_DEV_PM_OPS(mvneta_pm_ops, mvneta_suspend, mvneta_resume);
4800 static const struct of_device_id mvneta_match[] = {
4801 { .compatible = "marvell,armada-370-neta" },
4802 { .compatible = "marvell,armada-xp-neta" },
4803 { .compatible = "marvell,armada-3700-neta" },
4806 MODULE_DEVICE_TABLE(of, mvneta_match);
4808 static struct platform_driver mvneta_driver = {
4809 .probe = mvneta_probe,
4810 .remove = mvneta_remove,
4812 .name = MVNETA_DRIVER_NAME,
4813 .of_match_table = mvneta_match,
4814 .pm = &mvneta_pm_ops,
4818 static int __init mvneta_driver_init(void)
4822 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "net/mvmeta:online",
4824 mvneta_cpu_down_prepare);
4827 online_hpstate = ret;
4828 ret = cpuhp_setup_state_multi(CPUHP_NET_MVNETA_DEAD, "net/mvneta:dead",
4829 NULL, mvneta_cpu_dead);
4833 ret = platform_driver_register(&mvneta_driver);
4839 cpuhp_remove_multi_state(CPUHP_NET_MVNETA_DEAD);
4841 cpuhp_remove_multi_state(online_hpstate);
4845 module_init(mvneta_driver_init);
4847 static void __exit mvneta_driver_exit(void)
4849 platform_driver_unregister(&mvneta_driver);
4850 cpuhp_remove_multi_state(CPUHP_NET_MVNETA_DEAD);
4851 cpuhp_remove_multi_state(online_hpstate);
4853 module_exit(mvneta_driver_exit);
4855 MODULE_DESCRIPTION("Marvell NETA Ethernet Driver - www.marvell.com");
4857 MODULE_LICENSE("GPL");
4859 module_param(rxq_number, int, 0444);
4860 module_param(txq_number, int, 0444);
4862 module_param(rxq_def, int, 0444);
4863 module_param(rx_copybreak, int, 0644);