Linux 4.19.133
[linux/fpc-iii.git] / drivers / net / ethernet / marvell / mvneta.c
blob30a16cf796c7a6e1ea4dd890a2828bf5e0b9145f
1 /*
2 * Driver for Marvell NETA network card for Armada XP and Armada 370 SoCs.
4 * Copyright (C) 2012 Marvell
6 * Rami Rosen <rosenr@marvell.com>
7 * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
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>
20 #include <linux/io.h>
21 #include <linux/kernel.h>
22 #include <linux/mbus.h>
23 #include <linux/module.h>
24 #include <linux/netdevice.h>
25 #include <linux/of.h>
26 #include <linux/of_address.h>
27 #include <linux/of_irq.h>
28 #include <linux/of_mdio.h>
29 #include <linux/of_net.h>
30 #include <linux/phy.h>
31 #include <linux/phylink.h>
32 #include <linux/platform_device.h>
33 #include <linux/skbuff.h>
34 #include <net/hwbm.h>
35 #include "mvneta_bm.h"
36 #include <net/ip.h>
37 #include <net/ipv6.h>
38 #include <net/tso.h>
40 /* Registers */
41 #define MVNETA_RXQ_CONFIG_REG(q) (0x1400 + ((q) << 2))
42 #define MVNETA_RXQ_HW_BUF_ALLOC BIT(0)
43 #define MVNETA_RXQ_SHORT_POOL_ID_SHIFT 4
44 #define MVNETA_RXQ_SHORT_POOL_ID_MASK 0x30
45 #define MVNETA_RXQ_LONG_POOL_ID_SHIFT 6
46 #define MVNETA_RXQ_LONG_POOL_ID_MASK 0xc0
47 #define MVNETA_RXQ_PKT_OFFSET_ALL_MASK (0xf << 8)
48 #define MVNETA_RXQ_PKT_OFFSET_MASK(offs) ((offs) << 8)
49 #define MVNETA_RXQ_THRESHOLD_REG(q) (0x14c0 + ((q) << 2))
50 #define MVNETA_RXQ_NON_OCCUPIED(v) ((v) << 16)
51 #define MVNETA_RXQ_BASE_ADDR_REG(q) (0x1480 + ((q) << 2))
52 #define MVNETA_RXQ_SIZE_REG(q) (0x14a0 + ((q) << 2))
53 #define MVNETA_RXQ_BUF_SIZE_SHIFT 19
54 #define MVNETA_RXQ_BUF_SIZE_MASK (0x1fff << 19)
55 #define MVNETA_RXQ_STATUS_REG(q) (0x14e0 + ((q) << 2))
56 #define MVNETA_RXQ_OCCUPIED_ALL_MASK 0x3fff
57 #define MVNETA_RXQ_STATUS_UPDATE_REG(q) (0x1500 + ((q) << 2))
58 #define MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT 16
59 #define MVNETA_RXQ_ADD_NON_OCCUPIED_MAX 255
60 #define MVNETA_PORT_POOL_BUFFER_SZ_REG(pool) (0x1700 + ((pool) << 2))
61 #define MVNETA_PORT_POOL_BUFFER_SZ_SHIFT 3
62 #define MVNETA_PORT_POOL_BUFFER_SZ_MASK 0xfff8
63 #define MVNETA_PORT_RX_RESET 0x1cc0
64 #define MVNETA_PORT_RX_DMA_RESET BIT(0)
65 #define MVNETA_PHY_ADDR 0x2000
66 #define MVNETA_PHY_ADDR_MASK 0x1f
67 #define MVNETA_MBUS_RETRY 0x2010
68 #define MVNETA_UNIT_INTR_CAUSE 0x2080
69 #define MVNETA_UNIT_CONTROL 0x20B0
70 #define MVNETA_PHY_POLLING_ENABLE BIT(1)
71 #define MVNETA_WIN_BASE(w) (0x2200 + ((w) << 3))
72 #define MVNETA_WIN_SIZE(w) (0x2204 + ((w) << 3))
73 #define MVNETA_WIN_REMAP(w) (0x2280 + ((w) << 2))
74 #define MVNETA_BASE_ADDR_ENABLE 0x2290
75 #define MVNETA_ACCESS_PROTECT_ENABLE 0x2294
76 #define MVNETA_PORT_CONFIG 0x2400
77 #define MVNETA_UNI_PROMISC_MODE BIT(0)
78 #define MVNETA_DEF_RXQ(q) ((q) << 1)
79 #define MVNETA_DEF_RXQ_ARP(q) ((q) << 4)
80 #define MVNETA_TX_UNSET_ERR_SUM BIT(12)
81 #define MVNETA_DEF_RXQ_TCP(q) ((q) << 16)
82 #define MVNETA_DEF_RXQ_UDP(q) ((q) << 19)
83 #define MVNETA_DEF_RXQ_BPDU(q) ((q) << 22)
84 #define MVNETA_RX_CSUM_WITH_PSEUDO_HDR BIT(25)
85 #define MVNETA_PORT_CONFIG_DEFL_VALUE(q) (MVNETA_DEF_RXQ(q) | \
86 MVNETA_DEF_RXQ_ARP(q) | \
87 MVNETA_DEF_RXQ_TCP(q) | \
88 MVNETA_DEF_RXQ_UDP(q) | \
89 MVNETA_DEF_RXQ_BPDU(q) | \
90 MVNETA_TX_UNSET_ERR_SUM | \
91 MVNETA_RX_CSUM_WITH_PSEUDO_HDR)
92 #define MVNETA_PORT_CONFIG_EXTEND 0x2404
93 #define MVNETA_MAC_ADDR_LOW 0x2414
94 #define MVNETA_MAC_ADDR_HIGH 0x2418
95 #define MVNETA_SDMA_CONFIG 0x241c
96 #define MVNETA_SDMA_BRST_SIZE_16 4
97 #define MVNETA_RX_BRST_SZ_MASK(burst) ((burst) << 1)
98 #define MVNETA_RX_NO_DATA_SWAP BIT(4)
99 #define MVNETA_TX_NO_DATA_SWAP BIT(5)
100 #define MVNETA_DESC_SWAP BIT(6)
101 #define MVNETA_TX_BRST_SZ_MASK(burst) ((burst) << 22)
102 #define MVNETA_PORT_STATUS 0x2444
103 #define MVNETA_TX_IN_PRGRS BIT(1)
104 #define MVNETA_TX_FIFO_EMPTY BIT(8)
105 #define MVNETA_RX_MIN_FRAME_SIZE 0x247c
106 #define MVNETA_SERDES_CFG 0x24A0
107 #define MVNETA_SGMII_SERDES_PROTO 0x0cc7
108 #define MVNETA_QSGMII_SERDES_PROTO 0x0667
109 #define MVNETA_TYPE_PRIO 0x24bc
110 #define MVNETA_FORCE_UNI BIT(21)
111 #define MVNETA_TXQ_CMD_1 0x24e4
112 #define MVNETA_TXQ_CMD 0x2448
113 #define MVNETA_TXQ_DISABLE_SHIFT 8
114 #define MVNETA_TXQ_ENABLE_MASK 0x000000ff
115 #define MVNETA_RX_DISCARD_FRAME_COUNT 0x2484
116 #define MVNETA_OVERRUN_FRAME_COUNT 0x2488
117 #define MVNETA_GMAC_CLOCK_DIVIDER 0x24f4
118 #define MVNETA_GMAC_1MS_CLOCK_ENABLE BIT(31)
119 #define MVNETA_ACC_MODE 0x2500
120 #define MVNETA_BM_ADDRESS 0x2504
121 #define MVNETA_CPU_MAP(cpu) (0x2540 + ((cpu) << 2))
122 #define MVNETA_CPU_RXQ_ACCESS_ALL_MASK 0x000000ff
123 #define MVNETA_CPU_TXQ_ACCESS_ALL_MASK 0x0000ff00
124 #define MVNETA_CPU_RXQ_ACCESS(rxq) BIT(rxq)
125 #define MVNETA_CPU_TXQ_ACCESS(txq) BIT(txq + 8)
126 #define MVNETA_RXQ_TIME_COAL_REG(q) (0x2580 + ((q) << 2))
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_MIB_COUNTERS_BASE 0x3000
225 #define MVNETA_MIB_LATE_COLLISION 0x7c
226 #define MVNETA_DA_FILT_SPEC_MCAST 0x3400
227 #define MVNETA_DA_FILT_OTH_MCAST 0x3500
228 #define MVNETA_DA_FILT_UCAST_BASE 0x3600
229 #define MVNETA_TXQ_BASE_ADDR_REG(q) (0x3c00 + ((q) << 2))
230 #define MVNETA_TXQ_SIZE_REG(q) (0x3c20 + ((q) << 2))
231 #define MVNETA_TXQ_SENT_THRESH_ALL_MASK 0x3fff0000
232 #define MVNETA_TXQ_SENT_THRESH_MASK(coal) ((coal) << 16)
233 #define MVNETA_TXQ_UPDATE_REG(q) (0x3c60 + ((q) << 2))
234 #define MVNETA_TXQ_DEC_SENT_SHIFT 16
235 #define MVNETA_TXQ_DEC_SENT_MASK 0xff
236 #define MVNETA_TXQ_STATUS_REG(q) (0x3c40 + ((q) << 2))
237 #define MVNETA_TXQ_SENT_DESC_SHIFT 16
238 #define MVNETA_TXQ_SENT_DESC_MASK 0x3fff0000
239 #define MVNETA_PORT_TX_RESET 0x3cf0
240 #define MVNETA_PORT_TX_DMA_RESET BIT(0)
241 #define MVNETA_TX_MTU 0x3e0c
242 #define MVNETA_TX_TOKEN_SIZE 0x3e14
243 #define MVNETA_TX_TOKEN_SIZE_MAX 0xffffffff
244 #define MVNETA_TXQ_TOKEN_SIZE_REG(q) (0x3e40 + ((q) << 2))
245 #define MVNETA_TXQ_TOKEN_SIZE_MAX 0x7fffffff
247 #define MVNETA_LPI_CTRL_0 0x2cc0
248 #define MVNETA_LPI_CTRL_1 0x2cc4
249 #define MVNETA_LPI_REQUEST_ENABLE BIT(0)
250 #define MVNETA_LPI_CTRL_2 0x2cc8
251 #define MVNETA_LPI_STATUS 0x2ccc
253 #define MVNETA_CAUSE_TXQ_SENT_DESC_ALL_MASK 0xff
255 /* Descriptor ring Macros */
256 #define MVNETA_QUEUE_NEXT_DESC(q, index) \
257 (((index) < (q)->last_desc) ? ((index) + 1) : 0)
259 /* Various constants */
261 /* Coalescing */
262 #define MVNETA_TXDONE_COAL_PKTS 0 /* interrupt per packet */
263 #define MVNETA_RX_COAL_PKTS 32
264 #define MVNETA_RX_COAL_USEC 100
266 /* The two bytes Marvell header. Either contains a special value used
267 * by Marvell switches when a specific hardware mode is enabled (not
268 * supported by this driver) or is filled automatically by zeroes on
269 * the RX side. Those two bytes being at the front of the Ethernet
270 * header, they allow to have the IP header aligned on a 4 bytes
271 * boundary automatically: the hardware skips those two bytes on its
272 * own.
274 #define MVNETA_MH_SIZE 2
276 #define MVNETA_VLAN_TAG_LEN 4
278 #define MVNETA_TX_CSUM_DEF_SIZE 1600
279 #define MVNETA_TX_CSUM_MAX_SIZE 9800
280 #define MVNETA_ACC_MODE_EXT1 1
281 #define MVNETA_ACC_MODE_EXT2 2
283 #define MVNETA_MAX_DECODE_WIN 6
285 /* Timeout constants */
286 #define MVNETA_TX_DISABLE_TIMEOUT_MSEC 1000
287 #define MVNETA_RX_DISABLE_TIMEOUT_MSEC 1000
288 #define MVNETA_TX_FIFO_EMPTY_TIMEOUT 10000
290 #define MVNETA_TX_MTU_MAX 0x3ffff
292 /* The RSS lookup table actually has 256 entries but we do not use
293 * them yet
295 #define MVNETA_RSS_LU_TABLE_SIZE 1
297 /* Max number of Rx descriptors */
298 #define MVNETA_MAX_RXD 512
300 /* Max number of Tx descriptors */
301 #define MVNETA_MAX_TXD 1024
303 /* Max number of allowed TCP segments for software TSO */
304 #define MVNETA_MAX_TSO_SEGS 100
306 #define MVNETA_MAX_SKB_DESCS (MVNETA_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
308 /* descriptor aligned size */
309 #define MVNETA_DESC_ALIGNED_SIZE 32
311 /* Number of bytes to be taken into account by HW when putting incoming data
312 * to the buffers. It is needed in case NET_SKB_PAD exceeds maximum packet
313 * offset supported in MVNETA_RXQ_CONFIG_REG(q) registers.
315 #define MVNETA_RX_PKT_OFFSET_CORRECTION 64
317 #define MVNETA_RX_PKT_SIZE(mtu) \
318 ALIGN((mtu) + MVNETA_MH_SIZE + MVNETA_VLAN_TAG_LEN + \
319 ETH_HLEN + ETH_FCS_LEN, \
320 cache_line_size())
322 #define IS_TSO_HEADER(txq, addr) \
323 ((addr >= txq->tso_hdrs_phys) && \
324 (addr < txq->tso_hdrs_phys + txq->size * TSO_HEADER_SIZE))
326 #define MVNETA_RX_GET_BM_POOL_ID(rxd) \
327 (((rxd)->status & MVNETA_RXD_BM_POOL_MASK) >> MVNETA_RXD_BM_POOL_SHIFT)
329 enum {
330 ETHTOOL_STAT_EEE_WAKEUP,
331 ETHTOOL_STAT_SKB_ALLOC_ERR,
332 ETHTOOL_STAT_REFILL_ERR,
333 ETHTOOL_MAX_STATS,
336 struct mvneta_statistic {
337 unsigned short offset;
338 unsigned short type;
339 const char name[ETH_GSTRING_LEN];
342 #define T_REG_32 32
343 #define T_REG_64 64
344 #define T_SW 1
346 static const struct mvneta_statistic mvneta_statistics[] = {
347 { 0x3000, T_REG_64, "good_octets_received", },
348 { 0x3010, T_REG_32, "good_frames_received", },
349 { 0x3008, T_REG_32, "bad_octets_received", },
350 { 0x3014, T_REG_32, "bad_frames_received", },
351 { 0x3018, T_REG_32, "broadcast_frames_received", },
352 { 0x301c, T_REG_32, "multicast_frames_received", },
353 { 0x3050, T_REG_32, "unrec_mac_control_received", },
354 { 0x3058, T_REG_32, "good_fc_received", },
355 { 0x305c, T_REG_32, "bad_fc_received", },
356 { 0x3060, T_REG_32, "undersize_received", },
357 { 0x3064, T_REG_32, "fragments_received", },
358 { 0x3068, T_REG_32, "oversize_received", },
359 { 0x306c, T_REG_32, "jabber_received", },
360 { 0x3070, T_REG_32, "mac_receive_error", },
361 { 0x3074, T_REG_32, "bad_crc_event", },
362 { 0x3078, T_REG_32, "collision", },
363 { 0x307c, T_REG_32, "late_collision", },
364 { 0x2484, T_REG_32, "rx_discard", },
365 { 0x2488, T_REG_32, "rx_overrun", },
366 { 0x3020, T_REG_32, "frames_64_octets", },
367 { 0x3024, T_REG_32, "frames_65_to_127_octets", },
368 { 0x3028, T_REG_32, "frames_128_to_255_octets", },
369 { 0x302c, T_REG_32, "frames_256_to_511_octets", },
370 { 0x3030, T_REG_32, "frames_512_to_1023_octets", },
371 { 0x3034, T_REG_32, "frames_1024_to_max_octets", },
372 { 0x3038, T_REG_64, "good_octets_sent", },
373 { 0x3040, T_REG_32, "good_frames_sent", },
374 { 0x3044, T_REG_32, "excessive_collision", },
375 { 0x3048, T_REG_32, "multicast_frames_sent", },
376 { 0x304c, T_REG_32, "broadcast_frames_sent", },
377 { 0x3054, T_REG_32, "fc_sent", },
378 { 0x300c, T_REG_32, "internal_mac_transmit_err", },
379 { ETHTOOL_STAT_EEE_WAKEUP, T_SW, "eee_wakeup_errors", },
380 { ETHTOOL_STAT_SKB_ALLOC_ERR, T_SW, "skb_alloc_errors", },
381 { ETHTOOL_STAT_REFILL_ERR, T_SW, "refill_errors", },
384 struct mvneta_pcpu_stats {
385 struct u64_stats_sync syncp;
386 u64 rx_packets;
387 u64 rx_bytes;
388 u64 rx_dropped;
389 u64 rx_errors;
390 u64 tx_packets;
391 u64 tx_bytes;
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 */
402 u32 cause_rx_tx;
405 struct mvneta_port {
406 u8 id;
407 struct mvneta_pcpu_port __percpu *ports;
408 struct mvneta_pcpu_stats __percpu *stats;
410 int pkt_size;
411 void __iomem *base;
412 struct mvneta_rx_queue *rxqs;
413 struct mvneta_tx_queue *txqs;
414 struct net_device *dev;
415 struct hlist_node node_online;
416 struct hlist_node node_dead;
417 int rxq_def;
418 /* Protect the access to the percpu interrupt registers,
419 * ensuring that the configuration remains coherent.
421 spinlock_t lock;
422 bool is_stopped;
424 u32 cause_rx_tx;
425 struct napi_struct napi;
427 /* Core clock */
428 struct clk *clk;
429 /* AXI clock */
430 struct clk *clk_bus;
431 u8 mcast_count[256];
432 u16 tx_ring_size;
433 u16 rx_ring_size;
435 phy_interface_t phy_interface;
436 struct device_node *dn;
437 unsigned int tx_csum_limit;
438 struct phylink *phylink;
440 struct mvneta_bm *bm_priv;
441 struct mvneta_bm_pool *pool_long;
442 struct mvneta_bm_pool *pool_short;
443 int bm_win_id;
445 bool eee_enabled;
446 bool eee_active;
447 bool tx_lpi_enabled;
449 u64 ethtool_stats[ARRAY_SIZE(mvneta_statistics)];
451 u32 indir[MVNETA_RSS_LU_TABLE_SIZE];
453 /* Flags for special SoC configurations */
454 bool neta_armada3700;
455 u16 rx_offset_correction;
456 const struct mbus_dram_target_info *dram_target_info;
459 /* The mvneta_tx_desc and mvneta_rx_desc structures describe the
460 * layout of the transmit and reception DMA descriptors, and their
461 * layout is therefore defined by the hardware design
464 #define MVNETA_TX_L3_OFF_SHIFT 0
465 #define MVNETA_TX_IP_HLEN_SHIFT 8
466 #define MVNETA_TX_L4_UDP BIT(16)
467 #define MVNETA_TX_L3_IP6 BIT(17)
468 #define MVNETA_TXD_IP_CSUM BIT(18)
469 #define MVNETA_TXD_Z_PAD BIT(19)
470 #define MVNETA_TXD_L_DESC BIT(20)
471 #define MVNETA_TXD_F_DESC BIT(21)
472 #define MVNETA_TXD_FLZ_DESC (MVNETA_TXD_Z_PAD | \
473 MVNETA_TXD_L_DESC | \
474 MVNETA_TXD_F_DESC)
475 #define MVNETA_TX_L4_CSUM_FULL BIT(30)
476 #define MVNETA_TX_L4_CSUM_NOT BIT(31)
478 #define MVNETA_RXD_ERR_CRC 0x0
479 #define MVNETA_RXD_BM_POOL_SHIFT 13
480 #define MVNETA_RXD_BM_POOL_MASK (BIT(13) | BIT(14))
481 #define MVNETA_RXD_ERR_SUMMARY BIT(16)
482 #define MVNETA_RXD_ERR_OVERRUN BIT(17)
483 #define MVNETA_RXD_ERR_LEN BIT(18)
484 #define MVNETA_RXD_ERR_RESOURCE (BIT(17) | BIT(18))
485 #define MVNETA_RXD_ERR_CODE_MASK (BIT(17) | BIT(18))
486 #define MVNETA_RXD_L3_IP4 BIT(25)
487 #define MVNETA_RXD_LAST_DESC BIT(26)
488 #define MVNETA_RXD_FIRST_DESC BIT(27)
489 #define MVNETA_RXD_FIRST_LAST_DESC (MVNETA_RXD_FIRST_DESC | \
490 MVNETA_RXD_LAST_DESC)
491 #define MVNETA_RXD_L4_CSUM_OK BIT(30)
493 #if defined(__LITTLE_ENDIAN)
494 struct mvneta_tx_desc {
495 u32 command; /* Options used by HW for packet transmitting.*/
496 u16 reserverd1; /* csum_l4 (for future use) */
497 u16 data_size; /* Data size of transmitted packet in bytes */
498 u32 buf_phys_addr; /* Physical addr of transmitted buffer */
499 u32 reserved2; /* hw_cmd - (for future use, PMT) */
500 u32 reserved3[4]; /* Reserved - (for future use) */
503 struct mvneta_rx_desc {
504 u32 status; /* Info about received packet */
505 u16 reserved1; /* pnc_info - (for future use, PnC) */
506 u16 data_size; /* Size of received packet in bytes */
508 u32 buf_phys_addr; /* Physical address of the buffer */
509 u32 reserved2; /* pnc_flow_id (for future use, PnC) */
511 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
512 u16 reserved3; /* prefetch_cmd, for future use */
513 u16 reserved4; /* csum_l4 - (for future use, PnC) */
515 u32 reserved5; /* pnc_extra PnC (for future use, PnC) */
516 u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */
518 #else
519 struct mvneta_tx_desc {
520 u16 data_size; /* Data size of transmitted packet in bytes */
521 u16 reserverd1; /* csum_l4 (for future use) */
522 u32 command; /* Options used by HW for packet transmitting.*/
523 u32 reserved2; /* hw_cmd - (for future use, PMT) */
524 u32 buf_phys_addr; /* Physical addr of transmitted buffer */
525 u32 reserved3[4]; /* Reserved - (for future use) */
528 struct mvneta_rx_desc {
529 u16 data_size; /* Size of received packet in bytes */
530 u16 reserved1; /* pnc_info - (for future use, PnC) */
531 u32 status; /* Info about received packet */
533 u32 reserved2; /* pnc_flow_id (for future use, PnC) */
534 u32 buf_phys_addr; /* Physical address of the buffer */
536 u16 reserved4; /* csum_l4 - (for future use, PnC) */
537 u16 reserved3; /* prefetch_cmd, for future use */
538 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
540 u32 reserved5; /* pnc_extra PnC (for future use, PnC) */
541 u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */
543 #endif
545 struct mvneta_tx_queue {
546 /* Number of this TX queue, in the range 0-7 */
547 u8 id;
549 /* Number of TX DMA descriptors in the descriptor ring */
550 int size;
552 /* Number of currently used TX DMA descriptor in the
553 * descriptor ring
555 int count;
556 int pending;
557 int tx_stop_threshold;
558 int tx_wake_threshold;
560 /* Array of transmitted skb */
561 struct sk_buff **tx_skb;
563 /* Index of last TX DMA descriptor that was inserted */
564 int txq_put_index;
566 /* Index of the TX DMA descriptor to be cleaned up */
567 int txq_get_index;
569 u32 done_pkts_coal;
571 /* Virtual address of the TX DMA descriptors array */
572 struct mvneta_tx_desc *descs;
574 /* DMA address of the TX DMA descriptors array */
575 dma_addr_t descs_phys;
577 /* Index of the last TX DMA descriptor */
578 int last_desc;
580 /* Index of the next TX DMA descriptor to process */
581 int next_desc_to_proc;
583 /* DMA buffers for TSO headers */
584 char *tso_hdrs;
586 /* DMA address of TSO headers */
587 dma_addr_t tso_hdrs_phys;
589 /* Affinity mask for CPUs*/
590 cpumask_t affinity_mask;
593 struct mvneta_rx_queue {
594 /* rx queue number, in the range 0-7 */
595 u8 id;
597 /* num of rx descriptors in the rx descriptor ring */
598 int size;
600 u32 pkts_coal;
601 u32 time_coal;
603 /* Virtual address of the RX buffer */
604 void **buf_virt_addr;
606 /* Virtual address of the RX DMA descriptors array */
607 struct mvneta_rx_desc *descs;
609 /* DMA address of the RX DMA descriptors array */
610 dma_addr_t descs_phys;
612 /* Index of the last RX DMA descriptor */
613 int last_desc;
615 /* Index of the next RX DMA descriptor to process */
616 int next_desc_to_proc;
618 /* Index of first RX DMA descriptor to refill */
619 int first_to_refill;
620 u32 refill_num;
622 /* pointer to uncomplete skb buffer */
623 struct sk_buff *skb;
624 int left_size;
626 /* error counters */
627 u32 skb_alloc_err;
628 u32 refill_err;
631 static enum cpuhp_state online_hpstate;
632 /* The hardware supports eight (8) rx queues, but we are only allowing
633 * the first one to be used. Therefore, let's just allocate one queue.
635 static int rxq_number = 8;
636 static int txq_number = 8;
638 static int rxq_def;
640 static int rx_copybreak __read_mostly = 256;
641 static int rx_header_size __read_mostly = 128;
643 /* HW BM need that each port be identify by a unique ID */
644 static int global_port_id;
646 #define MVNETA_DRIVER_NAME "mvneta"
647 #define MVNETA_DRIVER_VERSION "1.0"
649 /* Utility/helper methods */
651 /* Write helper method */
652 static void mvreg_write(struct mvneta_port *pp, u32 offset, u32 data)
654 writel(data, pp->base + offset);
657 /* Read helper method */
658 static u32 mvreg_read(struct mvneta_port *pp, u32 offset)
660 return readl(pp->base + offset);
663 /* Increment txq get counter */
664 static void mvneta_txq_inc_get(struct mvneta_tx_queue *txq)
666 txq->txq_get_index++;
667 if (txq->txq_get_index == txq->size)
668 txq->txq_get_index = 0;
671 /* Increment txq put counter */
672 static void mvneta_txq_inc_put(struct mvneta_tx_queue *txq)
674 txq->txq_put_index++;
675 if (txq->txq_put_index == txq->size)
676 txq->txq_put_index = 0;
680 /* Clear all MIB counters */
681 static void mvneta_mib_counters_clear(struct mvneta_port *pp)
683 int i;
684 u32 dummy;
686 /* Perform dummy reads from MIB counters */
687 for (i = 0; i < MVNETA_MIB_LATE_COLLISION; i += 4)
688 dummy = mvreg_read(pp, (MVNETA_MIB_COUNTERS_BASE + i));
689 dummy = mvreg_read(pp, MVNETA_RX_DISCARD_FRAME_COUNT);
690 dummy = mvreg_read(pp, MVNETA_OVERRUN_FRAME_COUNT);
693 /* Get System Network Statistics */
694 static void
695 mvneta_get_stats64(struct net_device *dev,
696 struct rtnl_link_stats64 *stats)
698 struct mvneta_port *pp = netdev_priv(dev);
699 unsigned int start;
700 int cpu;
702 for_each_possible_cpu(cpu) {
703 struct mvneta_pcpu_stats *cpu_stats;
704 u64 rx_packets;
705 u64 rx_bytes;
706 u64 rx_dropped;
707 u64 rx_errors;
708 u64 tx_packets;
709 u64 tx_bytes;
711 cpu_stats = per_cpu_ptr(pp->stats, cpu);
712 do {
713 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
714 rx_packets = cpu_stats->rx_packets;
715 rx_bytes = cpu_stats->rx_bytes;
716 rx_dropped = cpu_stats->rx_dropped;
717 rx_errors = cpu_stats->rx_errors;
718 tx_packets = cpu_stats->tx_packets;
719 tx_bytes = cpu_stats->tx_bytes;
720 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
722 stats->rx_packets += rx_packets;
723 stats->rx_bytes += rx_bytes;
724 stats->rx_dropped += rx_dropped;
725 stats->rx_errors += rx_errors;
726 stats->tx_packets += tx_packets;
727 stats->tx_bytes += tx_bytes;
730 stats->tx_dropped = dev->stats.tx_dropped;
733 /* Rx descriptors helper methods */
735 /* Checks whether the RX descriptor having this status is both the first
736 * and the last descriptor for the RX packet. Each RX packet is currently
737 * received through a single RX descriptor, so not having each RX
738 * descriptor with its first and last bits set is an error
740 static int mvneta_rxq_desc_is_first_last(u32 status)
742 return (status & MVNETA_RXD_FIRST_LAST_DESC) ==
743 MVNETA_RXD_FIRST_LAST_DESC;
746 /* Add number of descriptors ready to receive new packets */
747 static void mvneta_rxq_non_occup_desc_add(struct mvneta_port *pp,
748 struct mvneta_rx_queue *rxq,
749 int ndescs)
751 /* Only MVNETA_RXQ_ADD_NON_OCCUPIED_MAX (255) descriptors can
752 * be added at once
754 while (ndescs > MVNETA_RXQ_ADD_NON_OCCUPIED_MAX) {
755 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
756 (MVNETA_RXQ_ADD_NON_OCCUPIED_MAX <<
757 MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
758 ndescs -= MVNETA_RXQ_ADD_NON_OCCUPIED_MAX;
761 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
762 (ndescs << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
765 /* Get number of RX descriptors occupied by received packets */
766 static int mvneta_rxq_busy_desc_num_get(struct mvneta_port *pp,
767 struct mvneta_rx_queue *rxq)
769 u32 val;
771 val = mvreg_read(pp, MVNETA_RXQ_STATUS_REG(rxq->id));
772 return val & MVNETA_RXQ_OCCUPIED_ALL_MASK;
775 /* Update num of rx desc called upon return from rx path or
776 * from mvneta_rxq_drop_pkts().
778 static void mvneta_rxq_desc_num_update(struct mvneta_port *pp,
779 struct mvneta_rx_queue *rxq,
780 int rx_done, int rx_filled)
782 u32 val;
784 if ((rx_done <= 0xff) && (rx_filled <= 0xff)) {
785 val = rx_done |
786 (rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT);
787 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
788 return;
791 /* Only 255 descriptors can be added at once */
792 while ((rx_done > 0) || (rx_filled > 0)) {
793 if (rx_done <= 0xff) {
794 val = rx_done;
795 rx_done = 0;
796 } else {
797 val = 0xff;
798 rx_done -= 0xff;
800 if (rx_filled <= 0xff) {
801 val |= rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
802 rx_filled = 0;
803 } else {
804 val |= 0xff << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
805 rx_filled -= 0xff;
807 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
811 /* Get pointer to next RX descriptor to be processed by SW */
812 static struct mvneta_rx_desc *
813 mvneta_rxq_next_desc_get(struct mvneta_rx_queue *rxq)
815 int rx_desc = rxq->next_desc_to_proc;
817 rxq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(rxq, rx_desc);
818 prefetch(rxq->descs + rxq->next_desc_to_proc);
819 return rxq->descs + rx_desc;
822 /* Change maximum receive size of the port. */
823 static void mvneta_max_rx_size_set(struct mvneta_port *pp, int max_rx_size)
825 u32 val;
827 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
828 val &= ~MVNETA_GMAC_MAX_RX_SIZE_MASK;
829 val |= ((max_rx_size - MVNETA_MH_SIZE) / 2) <<
830 MVNETA_GMAC_MAX_RX_SIZE_SHIFT;
831 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
835 /* Set rx queue offset */
836 static void mvneta_rxq_offset_set(struct mvneta_port *pp,
837 struct mvneta_rx_queue *rxq,
838 int offset)
840 u32 val;
842 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
843 val &= ~MVNETA_RXQ_PKT_OFFSET_ALL_MASK;
845 /* Offset is in */
846 val |= MVNETA_RXQ_PKT_OFFSET_MASK(offset >> 3);
847 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
851 /* Tx descriptors helper methods */
853 /* Update HW with number of TX descriptors to be sent */
854 static void mvneta_txq_pend_desc_add(struct mvneta_port *pp,
855 struct mvneta_tx_queue *txq,
856 int pend_desc)
858 u32 val;
860 pend_desc += txq->pending;
862 /* Only 255 Tx descriptors can be added at once */
863 do {
864 val = min(pend_desc, 255);
865 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
866 pend_desc -= val;
867 } while (pend_desc > 0);
868 txq->pending = 0;
871 /* Get pointer to next TX descriptor to be processed (send) by HW */
872 static struct mvneta_tx_desc *
873 mvneta_txq_next_desc_get(struct mvneta_tx_queue *txq)
875 int tx_desc = txq->next_desc_to_proc;
877 txq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(txq, tx_desc);
878 return txq->descs + tx_desc;
881 /* Release the last allocated TX descriptor. Useful to handle DMA
882 * mapping failures in the TX path.
884 static void mvneta_txq_desc_put(struct mvneta_tx_queue *txq)
886 if (txq->next_desc_to_proc == 0)
887 txq->next_desc_to_proc = txq->last_desc - 1;
888 else
889 txq->next_desc_to_proc--;
892 /* Set rxq buf size */
893 static void mvneta_rxq_buf_size_set(struct mvneta_port *pp,
894 struct mvneta_rx_queue *rxq,
895 int buf_size)
897 u32 val;
899 val = mvreg_read(pp, MVNETA_RXQ_SIZE_REG(rxq->id));
901 val &= ~MVNETA_RXQ_BUF_SIZE_MASK;
902 val |= ((buf_size >> 3) << MVNETA_RXQ_BUF_SIZE_SHIFT);
904 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), val);
907 /* Disable buffer management (BM) */
908 static void mvneta_rxq_bm_disable(struct mvneta_port *pp,
909 struct mvneta_rx_queue *rxq)
911 u32 val;
913 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
914 val &= ~MVNETA_RXQ_HW_BUF_ALLOC;
915 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
918 /* Enable buffer management (BM) */
919 static void mvneta_rxq_bm_enable(struct mvneta_port *pp,
920 struct mvneta_rx_queue *rxq)
922 u32 val;
924 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
925 val |= MVNETA_RXQ_HW_BUF_ALLOC;
926 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
929 /* Notify HW about port's assignment of pool for bigger packets */
930 static void mvneta_rxq_long_pool_set(struct mvneta_port *pp,
931 struct mvneta_rx_queue *rxq)
933 u32 val;
935 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
936 val &= ~MVNETA_RXQ_LONG_POOL_ID_MASK;
937 val |= (pp->pool_long->id << MVNETA_RXQ_LONG_POOL_ID_SHIFT);
939 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
942 /* Notify HW about port's assignment of pool for smaller packets */
943 static void mvneta_rxq_short_pool_set(struct mvneta_port *pp,
944 struct mvneta_rx_queue *rxq)
946 u32 val;
948 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
949 val &= ~MVNETA_RXQ_SHORT_POOL_ID_MASK;
950 val |= (pp->pool_short->id << MVNETA_RXQ_SHORT_POOL_ID_SHIFT);
952 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
955 /* Set port's receive buffer size for assigned BM pool */
956 static inline void mvneta_bm_pool_bufsize_set(struct mvneta_port *pp,
957 int buf_size,
958 u8 pool_id)
960 u32 val;
962 if (!IS_ALIGNED(buf_size, 8)) {
963 dev_warn(pp->dev->dev.parent,
964 "illegal buf_size value %d, round to %d\n",
965 buf_size, ALIGN(buf_size, 8));
966 buf_size = ALIGN(buf_size, 8);
969 val = mvreg_read(pp, MVNETA_PORT_POOL_BUFFER_SZ_REG(pool_id));
970 val |= buf_size & MVNETA_PORT_POOL_BUFFER_SZ_MASK;
971 mvreg_write(pp, MVNETA_PORT_POOL_BUFFER_SZ_REG(pool_id), val);
974 /* Configure MBUS window in order to enable access BM internal SRAM */
975 static int mvneta_mbus_io_win_set(struct mvneta_port *pp, u32 base, u32 wsize,
976 u8 target, u8 attr)
978 u32 win_enable, win_protect;
979 int i;
981 win_enable = mvreg_read(pp, MVNETA_BASE_ADDR_ENABLE);
983 if (pp->bm_win_id < 0) {
984 /* Find first not occupied window */
985 for (i = 0; i < MVNETA_MAX_DECODE_WIN; i++) {
986 if (win_enable & (1 << i)) {
987 pp->bm_win_id = i;
988 break;
991 if (i == MVNETA_MAX_DECODE_WIN)
992 return -ENOMEM;
993 } else {
994 i = pp->bm_win_id;
997 mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
998 mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
1000 if (i < 4)
1001 mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
1003 mvreg_write(pp, MVNETA_WIN_BASE(i), (base & 0xffff0000) |
1004 (attr << 8) | target);
1006 mvreg_write(pp, MVNETA_WIN_SIZE(i), (wsize - 1) & 0xffff0000);
1008 win_protect = mvreg_read(pp, MVNETA_ACCESS_PROTECT_ENABLE);
1009 win_protect |= 3 << (2 * i);
1010 mvreg_write(pp, MVNETA_ACCESS_PROTECT_ENABLE, win_protect);
1012 win_enable &= ~(1 << i);
1013 mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
1015 return 0;
1018 static int mvneta_bm_port_mbus_init(struct mvneta_port *pp)
1020 u32 wsize;
1021 u8 target, attr;
1022 int err;
1024 /* Get BM window information */
1025 err = mvebu_mbus_get_io_win_info(pp->bm_priv->bppi_phys_addr, &wsize,
1026 &target, &attr);
1027 if (err < 0)
1028 return err;
1030 pp->bm_win_id = -1;
1032 /* Open NETA -> BM window */
1033 err = mvneta_mbus_io_win_set(pp, pp->bm_priv->bppi_phys_addr, wsize,
1034 target, attr);
1035 if (err < 0) {
1036 netdev_info(pp->dev, "fail to configure mbus window to BM\n");
1037 return err;
1039 return 0;
1042 /* Assign and initialize pools for port. In case of fail
1043 * buffer manager will remain disabled for current port.
1045 static int mvneta_bm_port_init(struct platform_device *pdev,
1046 struct mvneta_port *pp)
1048 struct device_node *dn = pdev->dev.of_node;
1049 u32 long_pool_id, short_pool_id;
1051 if (!pp->neta_armada3700) {
1052 int ret;
1054 ret = mvneta_bm_port_mbus_init(pp);
1055 if (ret)
1056 return ret;
1059 if (of_property_read_u32(dn, "bm,pool-long", &long_pool_id)) {
1060 netdev_info(pp->dev, "missing long pool id\n");
1061 return -EINVAL;
1064 /* Create port's long pool depending on mtu */
1065 pp->pool_long = mvneta_bm_pool_use(pp->bm_priv, long_pool_id,
1066 MVNETA_BM_LONG, pp->id,
1067 MVNETA_RX_PKT_SIZE(pp->dev->mtu));
1068 if (!pp->pool_long) {
1069 netdev_info(pp->dev, "fail to obtain long pool for port\n");
1070 return -ENOMEM;
1073 pp->pool_long->port_map |= 1 << pp->id;
1075 mvneta_bm_pool_bufsize_set(pp, pp->pool_long->buf_size,
1076 pp->pool_long->id);
1078 /* If short pool id is not defined, assume using single pool */
1079 if (of_property_read_u32(dn, "bm,pool-short", &short_pool_id))
1080 short_pool_id = long_pool_id;
1082 /* Create port's short pool */
1083 pp->pool_short = mvneta_bm_pool_use(pp->bm_priv, short_pool_id,
1084 MVNETA_BM_SHORT, pp->id,
1085 MVNETA_BM_SHORT_PKT_SIZE);
1086 if (!pp->pool_short) {
1087 netdev_info(pp->dev, "fail to obtain short pool for port\n");
1088 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
1089 return -ENOMEM;
1092 if (short_pool_id != long_pool_id) {
1093 pp->pool_short->port_map |= 1 << pp->id;
1094 mvneta_bm_pool_bufsize_set(pp, pp->pool_short->buf_size,
1095 pp->pool_short->id);
1098 return 0;
1101 /* Update settings of a pool for bigger packets */
1102 static void mvneta_bm_update_mtu(struct mvneta_port *pp, int mtu)
1104 struct mvneta_bm_pool *bm_pool = pp->pool_long;
1105 struct hwbm_pool *hwbm_pool = &bm_pool->hwbm_pool;
1106 int num;
1108 /* Release all buffers from long pool */
1109 mvneta_bm_bufs_free(pp->bm_priv, bm_pool, 1 << pp->id);
1110 if (hwbm_pool->buf_num) {
1111 WARN(1, "cannot free all buffers in pool %d\n",
1112 bm_pool->id);
1113 goto bm_mtu_err;
1116 bm_pool->pkt_size = MVNETA_RX_PKT_SIZE(mtu);
1117 bm_pool->buf_size = MVNETA_RX_BUF_SIZE(bm_pool->pkt_size);
1118 hwbm_pool->frag_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
1119 SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(bm_pool->pkt_size));
1121 /* Fill entire long pool */
1122 num = hwbm_pool_add(hwbm_pool, hwbm_pool->size, GFP_ATOMIC);
1123 if (num != hwbm_pool->size) {
1124 WARN(1, "pool %d: %d of %d allocated\n",
1125 bm_pool->id, num, hwbm_pool->size);
1126 goto bm_mtu_err;
1128 mvneta_bm_pool_bufsize_set(pp, bm_pool->buf_size, bm_pool->id);
1130 return;
1132 bm_mtu_err:
1133 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
1134 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short, 1 << pp->id);
1136 pp->bm_priv = NULL;
1137 mvreg_write(pp, MVNETA_ACC_MODE, MVNETA_ACC_MODE_EXT1);
1138 netdev_info(pp->dev, "fail to update MTU, fall back to software BM\n");
1141 /* Start the Ethernet port RX and TX activity */
1142 static void mvneta_port_up(struct mvneta_port *pp)
1144 int queue;
1145 u32 q_map;
1147 /* Enable all initialized TXs. */
1148 q_map = 0;
1149 for (queue = 0; queue < txq_number; queue++) {
1150 struct mvneta_tx_queue *txq = &pp->txqs[queue];
1151 if (txq->descs)
1152 q_map |= (1 << queue);
1154 mvreg_write(pp, MVNETA_TXQ_CMD, q_map);
1156 q_map = 0;
1157 /* Enable all initialized RXQs. */
1158 for (queue = 0; queue < rxq_number; queue++) {
1159 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
1161 if (rxq->descs)
1162 q_map |= (1 << queue);
1164 mvreg_write(pp, MVNETA_RXQ_CMD, q_map);
1167 /* Stop the Ethernet port activity */
1168 static void mvneta_port_down(struct mvneta_port *pp)
1170 u32 val;
1171 int count;
1173 /* Stop Rx port activity. Check port Rx activity. */
1174 val = mvreg_read(pp, MVNETA_RXQ_CMD) & MVNETA_RXQ_ENABLE_MASK;
1176 /* Issue stop command for active channels only */
1177 if (val != 0)
1178 mvreg_write(pp, MVNETA_RXQ_CMD,
1179 val << MVNETA_RXQ_DISABLE_SHIFT);
1181 /* Wait for all Rx activity to terminate. */
1182 count = 0;
1183 do {
1184 if (count++ >= MVNETA_RX_DISABLE_TIMEOUT_MSEC) {
1185 netdev_warn(pp->dev,
1186 "TIMEOUT for RX stopped ! rx_queue_cmd: 0x%08x\n",
1187 val);
1188 break;
1190 mdelay(1);
1192 val = mvreg_read(pp, MVNETA_RXQ_CMD);
1193 } while (val & MVNETA_RXQ_ENABLE_MASK);
1195 /* Stop Tx port activity. Check port Tx activity. Issue stop
1196 * command for active channels only
1198 val = (mvreg_read(pp, MVNETA_TXQ_CMD)) & MVNETA_TXQ_ENABLE_MASK;
1200 if (val != 0)
1201 mvreg_write(pp, MVNETA_TXQ_CMD,
1202 (val << MVNETA_TXQ_DISABLE_SHIFT));
1204 /* Wait for all Tx activity to terminate. */
1205 count = 0;
1206 do {
1207 if (count++ >= MVNETA_TX_DISABLE_TIMEOUT_MSEC) {
1208 netdev_warn(pp->dev,
1209 "TIMEOUT for TX stopped status=0x%08x\n",
1210 val);
1211 break;
1213 mdelay(1);
1215 /* Check TX Command reg that all Txqs are stopped */
1216 val = mvreg_read(pp, MVNETA_TXQ_CMD);
1218 } while (val & MVNETA_TXQ_ENABLE_MASK);
1220 /* Double check to verify that TX FIFO is empty */
1221 count = 0;
1222 do {
1223 if (count++ >= MVNETA_TX_FIFO_EMPTY_TIMEOUT) {
1224 netdev_warn(pp->dev,
1225 "TX FIFO empty timeout status=0x%08x\n",
1226 val);
1227 break;
1229 mdelay(1);
1231 val = mvreg_read(pp, MVNETA_PORT_STATUS);
1232 } while (!(val & MVNETA_TX_FIFO_EMPTY) &&
1233 (val & MVNETA_TX_IN_PRGRS));
1235 udelay(200);
1238 /* Enable the port by setting the port enable bit of the MAC control register */
1239 static void mvneta_port_enable(struct mvneta_port *pp)
1241 u32 val;
1243 /* Enable port */
1244 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
1245 val |= MVNETA_GMAC0_PORT_ENABLE;
1246 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
1249 /* Disable the port and wait for about 200 usec before retuning */
1250 static void mvneta_port_disable(struct mvneta_port *pp)
1252 u32 val;
1254 /* Reset the Enable bit in the Serial Control Register */
1255 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
1256 val &= ~MVNETA_GMAC0_PORT_ENABLE;
1257 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
1259 udelay(200);
1262 /* Multicast tables methods */
1264 /* Set all entries in Unicast MAC Table; queue==-1 means reject all */
1265 static void mvneta_set_ucast_table(struct mvneta_port *pp, int queue)
1267 int offset;
1268 u32 val;
1270 if (queue == -1) {
1271 val = 0;
1272 } else {
1273 val = 0x1 | (queue << 1);
1274 val |= (val << 24) | (val << 16) | (val << 8);
1277 for (offset = 0; offset <= 0xc; offset += 4)
1278 mvreg_write(pp, MVNETA_DA_FILT_UCAST_BASE + offset, val);
1281 /* Set all entries in Special Multicast MAC Table; queue==-1 means reject all */
1282 static void mvneta_set_special_mcast_table(struct mvneta_port *pp, int queue)
1284 int offset;
1285 u32 val;
1287 if (queue == -1) {
1288 val = 0;
1289 } else {
1290 val = 0x1 | (queue << 1);
1291 val |= (val << 24) | (val << 16) | (val << 8);
1294 for (offset = 0; offset <= 0xfc; offset += 4)
1295 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + offset, val);
1299 /* Set all entries in Other Multicast MAC Table. queue==-1 means reject all */
1300 static void mvneta_set_other_mcast_table(struct mvneta_port *pp, int queue)
1302 int offset;
1303 u32 val;
1305 if (queue == -1) {
1306 memset(pp->mcast_count, 0, sizeof(pp->mcast_count));
1307 val = 0;
1308 } else {
1309 memset(pp->mcast_count, 1, sizeof(pp->mcast_count));
1310 val = 0x1 | (queue << 1);
1311 val |= (val << 24) | (val << 16) | (val << 8);
1314 for (offset = 0; offset <= 0xfc; offset += 4)
1315 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + offset, val);
1318 static void mvneta_percpu_unmask_interrupt(void *arg)
1320 struct mvneta_port *pp = arg;
1322 /* All the queue are unmasked, but actually only the ones
1323 * mapped to this CPU will be unmasked
1325 mvreg_write(pp, MVNETA_INTR_NEW_MASK,
1326 MVNETA_RX_INTR_MASK_ALL |
1327 MVNETA_TX_INTR_MASK_ALL |
1328 MVNETA_MISCINTR_INTR_MASK);
1331 static void mvneta_percpu_mask_interrupt(void *arg)
1333 struct mvneta_port *pp = arg;
1335 /* All the queue are masked, but actually only the ones
1336 * mapped to this CPU will be masked
1338 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
1339 mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0);
1340 mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0);
1343 static void mvneta_percpu_clear_intr_cause(void *arg)
1345 struct mvneta_port *pp = arg;
1347 /* All the queue are cleared, but actually only the ones
1348 * mapped to this CPU will be cleared
1350 mvreg_write(pp, MVNETA_INTR_NEW_CAUSE, 0);
1351 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
1352 mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0);
1355 /* This method sets defaults to the NETA port:
1356 * Clears interrupt Cause and Mask registers.
1357 * Clears all MAC tables.
1358 * Sets defaults to all registers.
1359 * Resets RX and TX descriptor rings.
1360 * Resets PHY.
1361 * This method can be called after mvneta_port_down() to return the port
1362 * settings to defaults.
1364 static void mvneta_defaults_set(struct mvneta_port *pp)
1366 int cpu;
1367 int queue;
1368 u32 val;
1369 int max_cpu = num_present_cpus();
1371 /* Clear all Cause registers */
1372 on_each_cpu(mvneta_percpu_clear_intr_cause, pp, true);
1374 /* Mask all interrupts */
1375 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
1376 mvreg_write(pp, MVNETA_INTR_ENABLE, 0);
1378 /* Enable MBUS Retry bit16 */
1379 mvreg_write(pp, MVNETA_MBUS_RETRY, 0x20);
1381 /* Set CPU queue access map. CPUs are assigned to the RX and
1382 * TX queues modulo their number. If there is only one TX
1383 * queue then it is assigned to the CPU associated to the
1384 * default RX queue.
1386 for_each_present_cpu(cpu) {
1387 int rxq_map = 0, txq_map = 0;
1388 int rxq, txq;
1389 if (!pp->neta_armada3700) {
1390 for (rxq = 0; rxq < rxq_number; rxq++)
1391 if ((rxq % max_cpu) == cpu)
1392 rxq_map |= MVNETA_CPU_RXQ_ACCESS(rxq);
1394 for (txq = 0; txq < txq_number; txq++)
1395 if ((txq % max_cpu) == cpu)
1396 txq_map |= MVNETA_CPU_TXQ_ACCESS(txq);
1398 /* With only one TX queue we configure a special case
1399 * which will allow to get all the irq on a single
1400 * CPU
1402 if (txq_number == 1)
1403 txq_map = (cpu == pp->rxq_def) ?
1404 MVNETA_CPU_TXQ_ACCESS(1) : 0;
1406 } else {
1407 txq_map = MVNETA_CPU_TXQ_ACCESS_ALL_MASK;
1408 rxq_map = MVNETA_CPU_RXQ_ACCESS_ALL_MASK;
1411 mvreg_write(pp, MVNETA_CPU_MAP(cpu), rxq_map | txq_map);
1414 /* Reset RX and TX DMAs */
1415 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
1416 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
1418 /* Disable Legacy WRR, Disable EJP, Release from reset */
1419 mvreg_write(pp, MVNETA_TXQ_CMD_1, 0);
1420 for (queue = 0; queue < txq_number; queue++) {
1421 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(queue), 0);
1422 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(queue), 0);
1425 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
1426 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
1428 /* Set Port Acceleration Mode */
1429 if (pp->bm_priv)
1430 /* HW buffer management + legacy parser */
1431 val = MVNETA_ACC_MODE_EXT2;
1432 else
1433 /* SW buffer management + legacy parser */
1434 val = MVNETA_ACC_MODE_EXT1;
1435 mvreg_write(pp, MVNETA_ACC_MODE, val);
1437 if (pp->bm_priv)
1438 mvreg_write(pp, MVNETA_BM_ADDRESS, pp->bm_priv->bppi_phys_addr);
1440 /* Update val of portCfg register accordingly with all RxQueue types */
1441 val = MVNETA_PORT_CONFIG_DEFL_VALUE(pp->rxq_def);
1442 mvreg_write(pp, MVNETA_PORT_CONFIG, val);
1444 val = 0;
1445 mvreg_write(pp, MVNETA_PORT_CONFIG_EXTEND, val);
1446 mvreg_write(pp, MVNETA_RX_MIN_FRAME_SIZE, 64);
1448 /* Build PORT_SDMA_CONFIG_REG */
1449 val = 0;
1451 /* Default burst size */
1452 val |= MVNETA_TX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
1453 val |= MVNETA_RX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
1454 val |= MVNETA_RX_NO_DATA_SWAP | MVNETA_TX_NO_DATA_SWAP;
1456 #if defined(__BIG_ENDIAN)
1457 val |= MVNETA_DESC_SWAP;
1458 #endif
1460 /* Assign port SDMA configuration */
1461 mvreg_write(pp, MVNETA_SDMA_CONFIG, val);
1463 /* Disable PHY polling in hardware, since we're using the
1464 * kernel phylib to do this.
1466 val = mvreg_read(pp, MVNETA_UNIT_CONTROL);
1467 val &= ~MVNETA_PHY_POLLING_ENABLE;
1468 mvreg_write(pp, MVNETA_UNIT_CONTROL, val);
1470 mvneta_set_ucast_table(pp, -1);
1471 mvneta_set_special_mcast_table(pp, -1);
1472 mvneta_set_other_mcast_table(pp, -1);
1474 /* Set port interrupt enable register - default enable all */
1475 mvreg_write(pp, MVNETA_INTR_ENABLE,
1476 (MVNETA_RXQ_INTR_ENABLE_ALL_MASK
1477 | MVNETA_TXQ_INTR_ENABLE_ALL_MASK));
1479 mvneta_mib_counters_clear(pp);
1482 /* Set max sizes for tx queues */
1483 static void mvneta_txq_max_tx_size_set(struct mvneta_port *pp, int max_tx_size)
1486 u32 val, size, mtu;
1487 int queue;
1489 mtu = max_tx_size * 8;
1490 if (mtu > MVNETA_TX_MTU_MAX)
1491 mtu = MVNETA_TX_MTU_MAX;
1493 /* Set MTU */
1494 val = mvreg_read(pp, MVNETA_TX_MTU);
1495 val &= ~MVNETA_TX_MTU_MAX;
1496 val |= mtu;
1497 mvreg_write(pp, MVNETA_TX_MTU, val);
1499 /* TX token size and all TXQs token size must be larger that MTU */
1500 val = mvreg_read(pp, MVNETA_TX_TOKEN_SIZE);
1502 size = val & MVNETA_TX_TOKEN_SIZE_MAX;
1503 if (size < mtu) {
1504 size = mtu;
1505 val &= ~MVNETA_TX_TOKEN_SIZE_MAX;
1506 val |= size;
1507 mvreg_write(pp, MVNETA_TX_TOKEN_SIZE, val);
1509 for (queue = 0; queue < txq_number; queue++) {
1510 val = mvreg_read(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue));
1512 size = val & MVNETA_TXQ_TOKEN_SIZE_MAX;
1513 if (size < mtu) {
1514 size = mtu;
1515 val &= ~MVNETA_TXQ_TOKEN_SIZE_MAX;
1516 val |= size;
1517 mvreg_write(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue), val);
1522 /* Set unicast address */
1523 static void mvneta_set_ucast_addr(struct mvneta_port *pp, u8 last_nibble,
1524 int queue)
1526 unsigned int unicast_reg;
1527 unsigned int tbl_offset;
1528 unsigned int reg_offset;
1530 /* Locate the Unicast table entry */
1531 last_nibble = (0xf & last_nibble);
1533 /* offset from unicast tbl base */
1534 tbl_offset = (last_nibble / 4) * 4;
1536 /* offset within the above reg */
1537 reg_offset = last_nibble % 4;
1539 unicast_reg = mvreg_read(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset));
1541 if (queue == -1) {
1542 /* Clear accepts frame bit at specified unicast DA tbl entry */
1543 unicast_reg &= ~(0xff << (8 * reg_offset));
1544 } else {
1545 unicast_reg &= ~(0xff << (8 * reg_offset));
1546 unicast_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
1549 mvreg_write(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset), unicast_reg);
1552 /* Set mac address */
1553 static void mvneta_mac_addr_set(struct mvneta_port *pp, unsigned char *addr,
1554 int queue)
1556 unsigned int mac_h;
1557 unsigned int mac_l;
1559 if (queue != -1) {
1560 mac_l = (addr[4] << 8) | (addr[5]);
1561 mac_h = (addr[0] << 24) | (addr[1] << 16) |
1562 (addr[2] << 8) | (addr[3] << 0);
1564 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, mac_l);
1565 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, mac_h);
1568 /* Accept frames of this address */
1569 mvneta_set_ucast_addr(pp, addr[5], queue);
1572 /* Set the number of packets that will be received before RX interrupt
1573 * will be generated by HW.
1575 static void mvneta_rx_pkts_coal_set(struct mvneta_port *pp,
1576 struct mvneta_rx_queue *rxq, u32 value)
1578 mvreg_write(pp, MVNETA_RXQ_THRESHOLD_REG(rxq->id),
1579 value | MVNETA_RXQ_NON_OCCUPIED(0));
1582 /* Set the time delay in usec before RX interrupt will be generated by
1583 * HW.
1585 static void mvneta_rx_time_coal_set(struct mvneta_port *pp,
1586 struct mvneta_rx_queue *rxq, u32 value)
1588 u32 val;
1589 unsigned long clk_rate;
1591 clk_rate = clk_get_rate(pp->clk);
1592 val = (clk_rate / 1000000) * value;
1594 mvreg_write(pp, MVNETA_RXQ_TIME_COAL_REG(rxq->id), val);
1597 /* Set threshold for TX_DONE pkts coalescing */
1598 static void mvneta_tx_done_pkts_coal_set(struct mvneta_port *pp,
1599 struct mvneta_tx_queue *txq, u32 value)
1601 u32 val;
1603 val = mvreg_read(pp, MVNETA_TXQ_SIZE_REG(txq->id));
1605 val &= ~MVNETA_TXQ_SENT_THRESH_ALL_MASK;
1606 val |= MVNETA_TXQ_SENT_THRESH_MASK(value);
1608 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), val);
1611 /* Handle rx descriptor fill by setting buf_cookie and buf_phys_addr */
1612 static void mvneta_rx_desc_fill(struct mvneta_rx_desc *rx_desc,
1613 u32 phys_addr, void *virt_addr,
1614 struct mvneta_rx_queue *rxq)
1616 int i;
1618 rx_desc->buf_phys_addr = phys_addr;
1619 i = rx_desc - rxq->descs;
1620 rxq->buf_virt_addr[i] = virt_addr;
1623 /* Decrement sent descriptors counter */
1624 static void mvneta_txq_sent_desc_dec(struct mvneta_port *pp,
1625 struct mvneta_tx_queue *txq,
1626 int sent_desc)
1628 u32 val;
1630 /* Only 255 TX descriptors can be updated at once */
1631 while (sent_desc > 0xff) {
1632 val = 0xff << MVNETA_TXQ_DEC_SENT_SHIFT;
1633 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1634 sent_desc = sent_desc - 0xff;
1637 val = sent_desc << MVNETA_TXQ_DEC_SENT_SHIFT;
1638 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1641 /* Get number of TX descriptors already sent by HW */
1642 static int mvneta_txq_sent_desc_num_get(struct mvneta_port *pp,
1643 struct mvneta_tx_queue *txq)
1645 u32 val;
1646 int sent_desc;
1648 val = mvreg_read(pp, MVNETA_TXQ_STATUS_REG(txq->id));
1649 sent_desc = (val & MVNETA_TXQ_SENT_DESC_MASK) >>
1650 MVNETA_TXQ_SENT_DESC_SHIFT;
1652 return sent_desc;
1655 /* Get number of sent descriptors and decrement counter.
1656 * The number of sent descriptors is returned.
1658 static int mvneta_txq_sent_desc_proc(struct mvneta_port *pp,
1659 struct mvneta_tx_queue *txq)
1661 int sent_desc;
1663 /* Get number of sent descriptors */
1664 sent_desc = mvneta_txq_sent_desc_num_get(pp, txq);
1666 /* Decrement sent descriptors counter */
1667 if (sent_desc)
1668 mvneta_txq_sent_desc_dec(pp, txq, sent_desc);
1670 return sent_desc;
1673 /* Set TXQ descriptors fields relevant for CSUM calculation */
1674 static u32 mvneta_txq_desc_csum(int l3_offs, int l3_proto,
1675 int ip_hdr_len, int l4_proto)
1677 u32 command;
1679 /* Fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
1680 * G_L4_chk, L4_type; required only for checksum
1681 * calculation
1683 command = l3_offs << MVNETA_TX_L3_OFF_SHIFT;
1684 command |= ip_hdr_len << MVNETA_TX_IP_HLEN_SHIFT;
1686 if (l3_proto == htons(ETH_P_IP))
1687 command |= MVNETA_TXD_IP_CSUM;
1688 else
1689 command |= MVNETA_TX_L3_IP6;
1691 if (l4_proto == IPPROTO_TCP)
1692 command |= MVNETA_TX_L4_CSUM_FULL;
1693 else if (l4_proto == IPPROTO_UDP)
1694 command |= MVNETA_TX_L4_UDP | MVNETA_TX_L4_CSUM_FULL;
1695 else
1696 command |= MVNETA_TX_L4_CSUM_NOT;
1698 return command;
1702 /* Display more error info */
1703 static void mvneta_rx_error(struct mvneta_port *pp,
1704 struct mvneta_rx_desc *rx_desc)
1706 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
1707 u32 status = rx_desc->status;
1709 /* update per-cpu counter */
1710 u64_stats_update_begin(&stats->syncp);
1711 stats->rx_errors++;
1712 u64_stats_update_end(&stats->syncp);
1714 switch (status & MVNETA_RXD_ERR_CODE_MASK) {
1715 case MVNETA_RXD_ERR_CRC:
1716 netdev_err(pp->dev, "bad rx status %08x (crc error), size=%d\n",
1717 status, rx_desc->data_size);
1718 break;
1719 case MVNETA_RXD_ERR_OVERRUN:
1720 netdev_err(pp->dev, "bad rx status %08x (overrun error), size=%d\n",
1721 status, rx_desc->data_size);
1722 break;
1723 case MVNETA_RXD_ERR_LEN:
1724 netdev_err(pp->dev, "bad rx status %08x (max frame length error), size=%d\n",
1725 status, rx_desc->data_size);
1726 break;
1727 case MVNETA_RXD_ERR_RESOURCE:
1728 netdev_err(pp->dev, "bad rx status %08x (resource error), size=%d\n",
1729 status, rx_desc->data_size);
1730 break;
1734 /* Handle RX checksum offload based on the descriptor's status */
1735 static void mvneta_rx_csum(struct mvneta_port *pp, u32 status,
1736 struct sk_buff *skb)
1738 if ((pp->dev->features & NETIF_F_RXCSUM) &&
1739 (status & MVNETA_RXD_L3_IP4) &&
1740 (status & MVNETA_RXD_L4_CSUM_OK)) {
1741 skb->csum = 0;
1742 skb->ip_summed = CHECKSUM_UNNECESSARY;
1743 return;
1746 skb->ip_summed = CHECKSUM_NONE;
1749 /* Return tx queue pointer (find last set bit) according to <cause> returned
1750 * form tx_done reg. <cause> must not be null. The return value is always a
1751 * valid queue for matching the first one found in <cause>.
1753 static struct mvneta_tx_queue *mvneta_tx_done_policy(struct mvneta_port *pp,
1754 u32 cause)
1756 int queue = fls(cause) - 1;
1758 return &pp->txqs[queue];
1761 /* Free tx queue skbuffs */
1762 static void mvneta_txq_bufs_free(struct mvneta_port *pp,
1763 struct mvneta_tx_queue *txq, int num,
1764 struct netdev_queue *nq)
1766 unsigned int bytes_compl = 0, pkts_compl = 0;
1767 int i;
1769 for (i = 0; i < num; i++) {
1770 struct mvneta_tx_desc *tx_desc = txq->descs +
1771 txq->txq_get_index;
1772 struct sk_buff *skb = txq->tx_skb[txq->txq_get_index];
1774 if (skb) {
1775 bytes_compl += skb->len;
1776 pkts_compl++;
1779 mvneta_txq_inc_get(txq);
1781 if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr))
1782 dma_unmap_single(pp->dev->dev.parent,
1783 tx_desc->buf_phys_addr,
1784 tx_desc->data_size, DMA_TO_DEVICE);
1785 if (!skb)
1786 continue;
1787 dev_kfree_skb_any(skb);
1790 netdev_tx_completed_queue(nq, pkts_compl, bytes_compl);
1793 /* Handle end of transmission */
1794 static void mvneta_txq_done(struct mvneta_port *pp,
1795 struct mvneta_tx_queue *txq)
1797 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
1798 int tx_done;
1800 tx_done = mvneta_txq_sent_desc_proc(pp, txq);
1801 if (!tx_done)
1802 return;
1804 mvneta_txq_bufs_free(pp, txq, tx_done, nq);
1806 txq->count -= tx_done;
1808 if (netif_tx_queue_stopped(nq)) {
1809 if (txq->count <= txq->tx_wake_threshold)
1810 netif_tx_wake_queue(nq);
1814 /* Refill processing for SW buffer management */
1815 /* Allocate page per descriptor */
1816 static int mvneta_rx_refill(struct mvneta_port *pp,
1817 struct mvneta_rx_desc *rx_desc,
1818 struct mvneta_rx_queue *rxq,
1819 gfp_t gfp_mask)
1821 dma_addr_t phys_addr;
1822 struct page *page;
1824 page = __dev_alloc_page(gfp_mask);
1825 if (!page)
1826 return -ENOMEM;
1828 /* map page for use */
1829 phys_addr = dma_map_page(pp->dev->dev.parent, page, 0, PAGE_SIZE,
1830 DMA_FROM_DEVICE);
1831 if (unlikely(dma_mapping_error(pp->dev->dev.parent, phys_addr))) {
1832 __free_page(page);
1833 return -ENOMEM;
1836 phys_addr += pp->rx_offset_correction;
1837 mvneta_rx_desc_fill(rx_desc, phys_addr, page, rxq);
1838 return 0;
1841 /* Handle tx checksum */
1842 static u32 mvneta_skb_tx_csum(struct mvneta_port *pp, struct sk_buff *skb)
1844 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1845 int ip_hdr_len = 0;
1846 __be16 l3_proto = vlan_get_protocol(skb);
1847 u8 l4_proto;
1849 if (l3_proto == htons(ETH_P_IP)) {
1850 struct iphdr *ip4h = ip_hdr(skb);
1852 /* Calculate IPv4 checksum and L4 checksum */
1853 ip_hdr_len = ip4h->ihl;
1854 l4_proto = ip4h->protocol;
1855 } else if (l3_proto == htons(ETH_P_IPV6)) {
1856 struct ipv6hdr *ip6h = ipv6_hdr(skb);
1858 /* Read l4_protocol from one of IPv6 extra headers */
1859 if (skb_network_header_len(skb) > 0)
1860 ip_hdr_len = (skb_network_header_len(skb) >> 2);
1861 l4_proto = ip6h->nexthdr;
1862 } else
1863 return MVNETA_TX_L4_CSUM_NOT;
1865 return mvneta_txq_desc_csum(skb_network_offset(skb),
1866 l3_proto, ip_hdr_len, l4_proto);
1869 return MVNETA_TX_L4_CSUM_NOT;
1872 /* Drop packets received by the RXQ and free buffers */
1873 static void mvneta_rxq_drop_pkts(struct mvneta_port *pp,
1874 struct mvneta_rx_queue *rxq)
1876 int rx_done, i;
1878 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
1879 if (rx_done)
1880 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
1882 if (pp->bm_priv) {
1883 for (i = 0; i < rx_done; i++) {
1884 struct mvneta_rx_desc *rx_desc =
1885 mvneta_rxq_next_desc_get(rxq);
1886 u8 pool_id = MVNETA_RX_GET_BM_POOL_ID(rx_desc);
1887 struct mvneta_bm_pool *bm_pool;
1889 bm_pool = &pp->bm_priv->bm_pools[pool_id];
1890 /* Return dropped buffer to the pool */
1891 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
1892 rx_desc->buf_phys_addr);
1894 return;
1897 for (i = 0; i < rxq->size; i++) {
1898 struct mvneta_rx_desc *rx_desc = rxq->descs + i;
1899 void *data = rxq->buf_virt_addr[i];
1900 if (!data || !(rx_desc->buf_phys_addr))
1901 continue;
1903 dma_unmap_page(pp->dev->dev.parent, rx_desc->buf_phys_addr,
1904 PAGE_SIZE, DMA_FROM_DEVICE);
1905 __free_page(data);
1909 static inline
1910 int mvneta_rx_refill_queue(struct mvneta_port *pp, struct mvneta_rx_queue *rxq)
1912 struct mvneta_rx_desc *rx_desc;
1913 int curr_desc = rxq->first_to_refill;
1914 int i;
1916 for (i = 0; (i < rxq->refill_num) && (i < 64); i++) {
1917 rx_desc = rxq->descs + curr_desc;
1918 if (!(rx_desc->buf_phys_addr)) {
1919 if (mvneta_rx_refill(pp, rx_desc, rxq, GFP_ATOMIC)) {
1920 pr_err("Can't refill queue %d. Done %d from %d\n",
1921 rxq->id, i, rxq->refill_num);
1922 rxq->refill_err++;
1923 break;
1926 curr_desc = MVNETA_QUEUE_NEXT_DESC(rxq, curr_desc);
1928 rxq->refill_num -= i;
1929 rxq->first_to_refill = curr_desc;
1931 return i;
1934 /* Main rx processing when using software buffer management */
1935 static int mvneta_rx_swbm(struct napi_struct *napi,
1936 struct mvneta_port *pp, int budget,
1937 struct mvneta_rx_queue *rxq)
1939 struct net_device *dev = pp->dev;
1940 int rx_todo, rx_proc;
1941 int refill = 0;
1942 u32 rcvd_pkts = 0;
1943 u32 rcvd_bytes = 0;
1945 /* Get number of received packets */
1946 rx_todo = mvneta_rxq_busy_desc_num_get(pp, rxq);
1947 rx_proc = 0;
1949 /* Fairness NAPI loop */
1950 while ((rcvd_pkts < budget) && (rx_proc < rx_todo)) {
1951 struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
1952 unsigned char *data;
1953 struct page *page;
1954 dma_addr_t phys_addr;
1955 u32 rx_status, index;
1956 int rx_bytes, skb_size, copy_size;
1957 int frag_num, frag_size, frag_offset;
1959 index = rx_desc - rxq->descs;
1960 page = (struct page *)rxq->buf_virt_addr[index];
1961 data = page_address(page);
1962 /* Prefetch header */
1963 prefetch(data);
1965 phys_addr = rx_desc->buf_phys_addr;
1966 rx_status = rx_desc->status;
1967 rx_proc++;
1968 rxq->refill_num++;
1970 if (rx_status & MVNETA_RXD_FIRST_DESC) {
1971 /* Check errors only for FIRST descriptor */
1972 if (rx_status & MVNETA_RXD_ERR_SUMMARY) {
1973 mvneta_rx_error(pp, rx_desc);
1974 /* leave the descriptor untouched */
1975 continue;
1977 rx_bytes = rx_desc->data_size -
1978 (ETH_FCS_LEN + MVNETA_MH_SIZE);
1980 /* Allocate small skb for each new packet */
1981 skb_size = max(rx_copybreak, rx_header_size);
1982 rxq->skb = netdev_alloc_skb_ip_align(dev, skb_size);
1983 if (unlikely(!rxq->skb)) {
1984 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
1986 netdev_err(dev,
1987 "Can't allocate skb on queue %d\n",
1988 rxq->id);
1990 rxq->skb_alloc_err++;
1992 u64_stats_update_begin(&stats->syncp);
1993 stats->rx_dropped++;
1994 u64_stats_update_end(&stats->syncp);
1995 continue;
1997 copy_size = min(skb_size, rx_bytes);
1999 /* Copy data from buffer to SKB, skip Marvell header */
2000 memcpy(rxq->skb->data, data + MVNETA_MH_SIZE,
2001 copy_size);
2002 skb_put(rxq->skb, copy_size);
2003 rxq->left_size = rx_bytes - copy_size;
2005 mvneta_rx_csum(pp, rx_status, rxq->skb);
2006 if (rxq->left_size == 0) {
2007 int size = copy_size + MVNETA_MH_SIZE;
2009 dma_sync_single_range_for_cpu(dev->dev.parent,
2010 phys_addr, 0,
2011 size,
2012 DMA_FROM_DEVICE);
2014 /* leave the descriptor and buffer untouched */
2015 } else {
2016 /* refill descriptor with new buffer later */
2017 rx_desc->buf_phys_addr = 0;
2019 frag_num = 0;
2020 frag_offset = copy_size + MVNETA_MH_SIZE;
2021 frag_size = min(rxq->left_size,
2022 (int)(PAGE_SIZE - frag_offset));
2023 skb_add_rx_frag(rxq->skb, frag_num, page,
2024 frag_offset, frag_size,
2025 PAGE_SIZE);
2026 dma_unmap_page(dev->dev.parent, phys_addr,
2027 PAGE_SIZE, DMA_FROM_DEVICE);
2028 rxq->left_size -= frag_size;
2030 } else {
2031 /* Middle or Last descriptor */
2032 if (unlikely(!rxq->skb)) {
2033 pr_debug("no skb for rx_status 0x%x\n",
2034 rx_status);
2035 continue;
2037 if (!rxq->left_size) {
2038 /* last descriptor has only FCS */
2039 /* and can be discarded */
2040 dma_sync_single_range_for_cpu(dev->dev.parent,
2041 phys_addr, 0,
2042 ETH_FCS_LEN,
2043 DMA_FROM_DEVICE);
2044 /* leave the descriptor and buffer untouched */
2045 } else {
2046 /* refill descriptor with new buffer later */
2047 rx_desc->buf_phys_addr = 0;
2049 frag_num = skb_shinfo(rxq->skb)->nr_frags;
2050 frag_offset = 0;
2051 frag_size = min(rxq->left_size,
2052 (int)(PAGE_SIZE - frag_offset));
2053 skb_add_rx_frag(rxq->skb, frag_num, page,
2054 frag_offset, frag_size,
2055 PAGE_SIZE);
2057 dma_unmap_page(dev->dev.parent, phys_addr,
2058 PAGE_SIZE, DMA_FROM_DEVICE);
2060 rxq->left_size -= frag_size;
2062 } /* Middle or Last descriptor */
2064 if (!(rx_status & MVNETA_RXD_LAST_DESC))
2065 /* no last descriptor this time */
2066 continue;
2068 if (rxq->left_size) {
2069 pr_err("get last desc, but left_size (%d) != 0\n",
2070 rxq->left_size);
2071 dev_kfree_skb_any(rxq->skb);
2072 rxq->left_size = 0;
2073 rxq->skb = NULL;
2074 continue;
2076 rcvd_pkts++;
2077 rcvd_bytes += rxq->skb->len;
2079 /* Linux processing */
2080 rxq->skb->protocol = eth_type_trans(rxq->skb, dev);
2082 if (dev->features & NETIF_F_GRO)
2083 napi_gro_receive(napi, rxq->skb);
2084 else
2085 netif_receive_skb(rxq->skb);
2087 /* clean uncomplete skb pointer in queue */
2088 rxq->skb = NULL;
2089 rxq->left_size = 0;
2092 if (rcvd_pkts) {
2093 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
2095 u64_stats_update_begin(&stats->syncp);
2096 stats->rx_packets += rcvd_pkts;
2097 stats->rx_bytes += rcvd_bytes;
2098 u64_stats_update_end(&stats->syncp);
2101 /* return some buffers to hardware queue, one at a time is too slow */
2102 refill = mvneta_rx_refill_queue(pp, rxq);
2104 /* Update rxq management counters */
2105 mvneta_rxq_desc_num_update(pp, rxq, rx_proc, refill);
2107 return rcvd_pkts;
2110 /* Main rx processing when using hardware buffer management */
2111 static int mvneta_rx_hwbm(struct napi_struct *napi,
2112 struct mvneta_port *pp, int rx_todo,
2113 struct mvneta_rx_queue *rxq)
2115 struct net_device *dev = pp->dev;
2116 int rx_done;
2117 u32 rcvd_pkts = 0;
2118 u32 rcvd_bytes = 0;
2120 /* Get number of received packets */
2121 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
2123 if (rx_todo > rx_done)
2124 rx_todo = rx_done;
2126 rx_done = 0;
2128 /* Fairness NAPI loop */
2129 while (rx_done < rx_todo) {
2130 struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
2131 struct mvneta_bm_pool *bm_pool = NULL;
2132 struct sk_buff *skb;
2133 unsigned char *data;
2134 dma_addr_t phys_addr;
2135 u32 rx_status, frag_size;
2136 int rx_bytes, err;
2137 u8 pool_id;
2139 rx_done++;
2140 rx_status = rx_desc->status;
2141 rx_bytes = rx_desc->data_size - (ETH_FCS_LEN + MVNETA_MH_SIZE);
2142 data = (u8 *)(uintptr_t)rx_desc->buf_cookie;
2143 phys_addr = rx_desc->buf_phys_addr;
2144 pool_id = MVNETA_RX_GET_BM_POOL_ID(rx_desc);
2145 bm_pool = &pp->bm_priv->bm_pools[pool_id];
2147 if (!mvneta_rxq_desc_is_first_last(rx_status) ||
2148 (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
2149 err_drop_frame_ret_pool:
2150 /* Return the buffer to the pool */
2151 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
2152 rx_desc->buf_phys_addr);
2153 err_drop_frame:
2154 mvneta_rx_error(pp, rx_desc);
2155 /* leave the descriptor untouched */
2156 continue;
2159 if (rx_bytes <= rx_copybreak) {
2160 /* better copy a small frame and not unmap the DMA region */
2161 skb = netdev_alloc_skb_ip_align(dev, rx_bytes);
2162 if (unlikely(!skb))
2163 goto err_drop_frame_ret_pool;
2165 dma_sync_single_range_for_cpu(&pp->bm_priv->pdev->dev,
2166 rx_desc->buf_phys_addr,
2167 MVNETA_MH_SIZE + NET_SKB_PAD,
2168 rx_bytes,
2169 DMA_FROM_DEVICE);
2170 skb_put_data(skb, data + MVNETA_MH_SIZE + NET_SKB_PAD,
2171 rx_bytes);
2173 skb->protocol = eth_type_trans(skb, dev);
2174 mvneta_rx_csum(pp, rx_status, skb);
2175 napi_gro_receive(napi, skb);
2177 rcvd_pkts++;
2178 rcvd_bytes += rx_bytes;
2180 /* Return the buffer to the pool */
2181 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
2182 rx_desc->buf_phys_addr);
2184 /* leave the descriptor and buffer untouched */
2185 continue;
2188 /* Refill processing */
2189 err = hwbm_pool_refill(&bm_pool->hwbm_pool, GFP_ATOMIC);
2190 if (err) {
2191 netdev_err(dev, "Linux processing - Can't refill\n");
2192 rxq->refill_err++;
2193 goto err_drop_frame_ret_pool;
2196 frag_size = bm_pool->hwbm_pool.frag_size;
2198 skb = build_skb(data, frag_size > PAGE_SIZE ? 0 : frag_size);
2200 /* After refill old buffer has to be unmapped regardless
2201 * the skb is successfully built or not.
2203 dma_unmap_single(&pp->bm_priv->pdev->dev, phys_addr,
2204 bm_pool->buf_size, DMA_FROM_DEVICE);
2205 if (!skb)
2206 goto err_drop_frame;
2208 rcvd_pkts++;
2209 rcvd_bytes += rx_bytes;
2211 /* Linux processing */
2212 skb_reserve(skb, MVNETA_MH_SIZE + NET_SKB_PAD);
2213 skb_put(skb, rx_bytes);
2215 skb->protocol = eth_type_trans(skb, dev);
2217 mvneta_rx_csum(pp, rx_status, skb);
2219 napi_gro_receive(napi, skb);
2222 if (rcvd_pkts) {
2223 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
2225 u64_stats_update_begin(&stats->syncp);
2226 stats->rx_packets += rcvd_pkts;
2227 stats->rx_bytes += rcvd_bytes;
2228 u64_stats_update_end(&stats->syncp);
2231 /* Update rxq management counters */
2232 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
2234 return rx_done;
2237 static inline void
2238 mvneta_tso_put_hdr(struct sk_buff *skb,
2239 struct mvneta_port *pp, struct mvneta_tx_queue *txq)
2241 struct mvneta_tx_desc *tx_desc;
2242 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2244 txq->tx_skb[txq->txq_put_index] = NULL;
2245 tx_desc = mvneta_txq_next_desc_get(txq);
2246 tx_desc->data_size = hdr_len;
2247 tx_desc->command = mvneta_skb_tx_csum(pp, skb);
2248 tx_desc->command |= MVNETA_TXD_F_DESC;
2249 tx_desc->buf_phys_addr = txq->tso_hdrs_phys +
2250 txq->txq_put_index * TSO_HEADER_SIZE;
2251 mvneta_txq_inc_put(txq);
2254 static inline int
2255 mvneta_tso_put_data(struct net_device *dev, struct mvneta_tx_queue *txq,
2256 struct sk_buff *skb, char *data, int size,
2257 bool last_tcp, bool is_last)
2259 struct mvneta_tx_desc *tx_desc;
2261 tx_desc = mvneta_txq_next_desc_get(txq);
2262 tx_desc->data_size = size;
2263 tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, data,
2264 size, DMA_TO_DEVICE);
2265 if (unlikely(dma_mapping_error(dev->dev.parent,
2266 tx_desc->buf_phys_addr))) {
2267 mvneta_txq_desc_put(txq);
2268 return -ENOMEM;
2271 tx_desc->command = 0;
2272 txq->tx_skb[txq->txq_put_index] = NULL;
2274 if (last_tcp) {
2275 /* last descriptor in the TCP packet */
2276 tx_desc->command = MVNETA_TXD_L_DESC;
2278 /* last descriptor in SKB */
2279 if (is_last)
2280 txq->tx_skb[txq->txq_put_index] = skb;
2282 mvneta_txq_inc_put(txq);
2283 return 0;
2286 static int mvneta_tx_tso(struct sk_buff *skb, struct net_device *dev,
2287 struct mvneta_tx_queue *txq)
2289 int total_len, data_left;
2290 int desc_count = 0;
2291 struct mvneta_port *pp = netdev_priv(dev);
2292 struct tso_t tso;
2293 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2294 int i;
2296 /* Count needed descriptors */
2297 if ((txq->count + tso_count_descs(skb)) >= txq->size)
2298 return 0;
2300 if (skb_headlen(skb) < (skb_transport_offset(skb) + tcp_hdrlen(skb))) {
2301 pr_info("*** Is this even possible???!?!?\n");
2302 return 0;
2305 /* Initialize the TSO handler, and prepare the first payload */
2306 tso_start(skb, &tso);
2308 total_len = skb->len - hdr_len;
2309 while (total_len > 0) {
2310 char *hdr;
2312 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
2313 total_len -= data_left;
2314 desc_count++;
2316 /* prepare packet headers: MAC + IP + TCP */
2317 hdr = txq->tso_hdrs + txq->txq_put_index * TSO_HEADER_SIZE;
2318 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
2320 mvneta_tso_put_hdr(skb, pp, txq);
2322 while (data_left > 0) {
2323 int size;
2324 desc_count++;
2326 size = min_t(int, tso.size, data_left);
2328 if (mvneta_tso_put_data(dev, txq, skb,
2329 tso.data, size,
2330 size == data_left,
2331 total_len == 0))
2332 goto err_release;
2333 data_left -= size;
2335 tso_build_data(skb, &tso, size);
2339 return desc_count;
2341 err_release:
2342 /* Release all used data descriptors; header descriptors must not
2343 * be DMA-unmapped.
2345 for (i = desc_count - 1; i >= 0; i--) {
2346 struct mvneta_tx_desc *tx_desc = txq->descs + i;
2347 if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr))
2348 dma_unmap_single(pp->dev->dev.parent,
2349 tx_desc->buf_phys_addr,
2350 tx_desc->data_size,
2351 DMA_TO_DEVICE);
2352 mvneta_txq_desc_put(txq);
2354 return 0;
2357 /* Handle tx fragmentation processing */
2358 static int mvneta_tx_frag_process(struct mvneta_port *pp, struct sk_buff *skb,
2359 struct mvneta_tx_queue *txq)
2361 struct mvneta_tx_desc *tx_desc;
2362 int i, nr_frags = skb_shinfo(skb)->nr_frags;
2364 for (i = 0; i < nr_frags; i++) {
2365 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2366 void *addr = page_address(frag->page.p) + frag->page_offset;
2368 tx_desc = mvneta_txq_next_desc_get(txq);
2369 tx_desc->data_size = frag->size;
2371 tx_desc->buf_phys_addr =
2372 dma_map_single(pp->dev->dev.parent, addr,
2373 tx_desc->data_size, DMA_TO_DEVICE);
2375 if (dma_mapping_error(pp->dev->dev.parent,
2376 tx_desc->buf_phys_addr)) {
2377 mvneta_txq_desc_put(txq);
2378 goto error;
2381 if (i == nr_frags - 1) {
2382 /* Last descriptor */
2383 tx_desc->command = MVNETA_TXD_L_DESC | MVNETA_TXD_Z_PAD;
2384 txq->tx_skb[txq->txq_put_index] = skb;
2385 } else {
2386 /* Descriptor in the middle: Not First, Not Last */
2387 tx_desc->command = 0;
2388 txq->tx_skb[txq->txq_put_index] = NULL;
2390 mvneta_txq_inc_put(txq);
2393 return 0;
2395 error:
2396 /* Release all descriptors that were used to map fragments of
2397 * this packet, as well as the corresponding DMA mappings
2399 for (i = i - 1; i >= 0; i--) {
2400 tx_desc = txq->descs + i;
2401 dma_unmap_single(pp->dev->dev.parent,
2402 tx_desc->buf_phys_addr,
2403 tx_desc->data_size,
2404 DMA_TO_DEVICE);
2405 mvneta_txq_desc_put(txq);
2408 return -ENOMEM;
2411 /* Main tx processing */
2412 static netdev_tx_t mvneta_tx(struct sk_buff *skb, struct net_device *dev)
2414 struct mvneta_port *pp = netdev_priv(dev);
2415 u16 txq_id = skb_get_queue_mapping(skb);
2416 struct mvneta_tx_queue *txq = &pp->txqs[txq_id];
2417 struct mvneta_tx_desc *tx_desc;
2418 int len = skb->len;
2419 int frags = 0;
2420 u32 tx_cmd;
2422 if (!netif_running(dev))
2423 goto out;
2425 if (skb_is_gso(skb)) {
2426 frags = mvneta_tx_tso(skb, dev, txq);
2427 goto out;
2430 frags = skb_shinfo(skb)->nr_frags + 1;
2432 /* Get a descriptor for the first part of the packet */
2433 tx_desc = mvneta_txq_next_desc_get(txq);
2435 tx_cmd = mvneta_skb_tx_csum(pp, skb);
2437 tx_desc->data_size = skb_headlen(skb);
2439 tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, skb->data,
2440 tx_desc->data_size,
2441 DMA_TO_DEVICE);
2442 if (unlikely(dma_mapping_error(dev->dev.parent,
2443 tx_desc->buf_phys_addr))) {
2444 mvneta_txq_desc_put(txq);
2445 frags = 0;
2446 goto out;
2449 if (frags == 1) {
2450 /* First and Last descriptor */
2451 tx_cmd |= MVNETA_TXD_FLZ_DESC;
2452 tx_desc->command = tx_cmd;
2453 txq->tx_skb[txq->txq_put_index] = skb;
2454 mvneta_txq_inc_put(txq);
2455 } else {
2456 /* First but not Last */
2457 tx_cmd |= MVNETA_TXD_F_DESC;
2458 txq->tx_skb[txq->txq_put_index] = NULL;
2459 mvneta_txq_inc_put(txq);
2460 tx_desc->command = tx_cmd;
2461 /* Continue with other skb fragments */
2462 if (mvneta_tx_frag_process(pp, skb, txq)) {
2463 dma_unmap_single(dev->dev.parent,
2464 tx_desc->buf_phys_addr,
2465 tx_desc->data_size,
2466 DMA_TO_DEVICE);
2467 mvneta_txq_desc_put(txq);
2468 frags = 0;
2469 goto out;
2473 out:
2474 if (frags > 0) {
2475 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
2476 struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
2478 netdev_tx_sent_queue(nq, len);
2480 txq->count += frags;
2481 if (txq->count >= txq->tx_stop_threshold)
2482 netif_tx_stop_queue(nq);
2484 if (!skb->xmit_more || netif_xmit_stopped(nq) ||
2485 txq->pending + frags > MVNETA_TXQ_DEC_SENT_MASK)
2486 mvneta_txq_pend_desc_add(pp, txq, frags);
2487 else
2488 txq->pending += frags;
2490 u64_stats_update_begin(&stats->syncp);
2491 stats->tx_packets++;
2492 stats->tx_bytes += len;
2493 u64_stats_update_end(&stats->syncp);
2494 } else {
2495 dev->stats.tx_dropped++;
2496 dev_kfree_skb_any(skb);
2499 return NETDEV_TX_OK;
2503 /* Free tx resources, when resetting a port */
2504 static void mvneta_txq_done_force(struct mvneta_port *pp,
2505 struct mvneta_tx_queue *txq)
2508 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
2509 int tx_done = txq->count;
2511 mvneta_txq_bufs_free(pp, txq, tx_done, nq);
2513 /* reset txq */
2514 txq->count = 0;
2515 txq->txq_put_index = 0;
2516 txq->txq_get_index = 0;
2519 /* Handle tx done - called in softirq context. The <cause_tx_done> argument
2520 * must be a valid cause according to MVNETA_TXQ_INTR_MASK_ALL.
2522 static void mvneta_tx_done_gbe(struct mvneta_port *pp, u32 cause_tx_done)
2524 struct mvneta_tx_queue *txq;
2525 struct netdev_queue *nq;
2527 while (cause_tx_done) {
2528 txq = mvneta_tx_done_policy(pp, cause_tx_done);
2530 nq = netdev_get_tx_queue(pp->dev, txq->id);
2531 __netif_tx_lock(nq, smp_processor_id());
2533 if (txq->count)
2534 mvneta_txq_done(pp, txq);
2536 __netif_tx_unlock(nq);
2537 cause_tx_done &= ~((1 << txq->id));
2541 /* Compute crc8 of the specified address, using a unique algorithm ,
2542 * according to hw spec, different than generic crc8 algorithm
2544 static int mvneta_addr_crc(unsigned char *addr)
2546 int crc = 0;
2547 int i;
2549 for (i = 0; i < ETH_ALEN; i++) {
2550 int j;
2552 crc = (crc ^ addr[i]) << 8;
2553 for (j = 7; j >= 0; j--) {
2554 if (crc & (0x100 << j))
2555 crc ^= 0x107 << j;
2559 return crc;
2562 /* This method controls the net device special MAC multicast support.
2563 * The Special Multicast Table for MAC addresses supports MAC of the form
2564 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
2565 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
2566 * Table entries in the DA-Filter table. This method set the Special
2567 * Multicast Table appropriate entry.
2569 static void mvneta_set_special_mcast_addr(struct mvneta_port *pp,
2570 unsigned char last_byte,
2571 int queue)
2573 unsigned int smc_table_reg;
2574 unsigned int tbl_offset;
2575 unsigned int reg_offset;
2577 /* Register offset from SMC table base */
2578 tbl_offset = (last_byte / 4);
2579 /* Entry offset within the above reg */
2580 reg_offset = last_byte % 4;
2582 smc_table_reg = mvreg_read(pp, (MVNETA_DA_FILT_SPEC_MCAST
2583 + tbl_offset * 4));
2585 if (queue == -1)
2586 smc_table_reg &= ~(0xff << (8 * reg_offset));
2587 else {
2588 smc_table_reg &= ~(0xff << (8 * reg_offset));
2589 smc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
2592 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + tbl_offset * 4,
2593 smc_table_reg);
2596 /* This method controls the network device Other MAC multicast support.
2597 * The Other Multicast Table is used for multicast of another type.
2598 * A CRC-8 is used as an index to the Other Multicast Table entries
2599 * in the DA-Filter table.
2600 * The method gets the CRC-8 value from the calling routine and
2601 * sets the Other Multicast Table appropriate entry according to the
2602 * specified CRC-8 .
2604 static void mvneta_set_other_mcast_addr(struct mvneta_port *pp,
2605 unsigned char crc8,
2606 int queue)
2608 unsigned int omc_table_reg;
2609 unsigned int tbl_offset;
2610 unsigned int reg_offset;
2612 tbl_offset = (crc8 / 4) * 4; /* Register offset from OMC table base */
2613 reg_offset = crc8 % 4; /* Entry offset within the above reg */
2615 omc_table_reg = mvreg_read(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset);
2617 if (queue == -1) {
2618 /* Clear accepts frame bit at specified Other DA table entry */
2619 omc_table_reg &= ~(0xff << (8 * reg_offset));
2620 } else {
2621 omc_table_reg &= ~(0xff << (8 * reg_offset));
2622 omc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
2625 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset, omc_table_reg);
2628 /* The network device supports multicast using two tables:
2629 * 1) Special Multicast Table for MAC addresses of the form
2630 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
2631 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
2632 * Table entries in the DA-Filter table.
2633 * 2) Other Multicast Table for multicast of another type. A CRC-8 value
2634 * is used as an index to the Other Multicast Table entries in the
2635 * DA-Filter table.
2637 static int mvneta_mcast_addr_set(struct mvneta_port *pp, unsigned char *p_addr,
2638 int queue)
2640 unsigned char crc_result = 0;
2642 if (memcmp(p_addr, "\x01\x00\x5e\x00\x00", 5) == 0) {
2643 mvneta_set_special_mcast_addr(pp, p_addr[5], queue);
2644 return 0;
2647 crc_result = mvneta_addr_crc(p_addr);
2648 if (queue == -1) {
2649 if (pp->mcast_count[crc_result] == 0) {
2650 netdev_info(pp->dev, "No valid Mcast for crc8=0x%02x\n",
2651 crc_result);
2652 return -EINVAL;
2655 pp->mcast_count[crc_result]--;
2656 if (pp->mcast_count[crc_result] != 0) {
2657 netdev_info(pp->dev,
2658 "After delete there are %d valid Mcast for crc8=0x%02x\n",
2659 pp->mcast_count[crc_result], crc_result);
2660 return -EINVAL;
2662 } else
2663 pp->mcast_count[crc_result]++;
2665 mvneta_set_other_mcast_addr(pp, crc_result, queue);
2667 return 0;
2670 /* Configure Fitering mode of Ethernet port */
2671 static void mvneta_rx_unicast_promisc_set(struct mvneta_port *pp,
2672 int is_promisc)
2674 u32 port_cfg_reg, val;
2676 port_cfg_reg = mvreg_read(pp, MVNETA_PORT_CONFIG);
2678 val = mvreg_read(pp, MVNETA_TYPE_PRIO);
2680 /* Set / Clear UPM bit in port configuration register */
2681 if (is_promisc) {
2682 /* Accept all Unicast addresses */
2683 port_cfg_reg |= MVNETA_UNI_PROMISC_MODE;
2684 val |= MVNETA_FORCE_UNI;
2685 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, 0xffff);
2686 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, 0xffffffff);
2687 } else {
2688 /* Reject all Unicast addresses */
2689 port_cfg_reg &= ~MVNETA_UNI_PROMISC_MODE;
2690 val &= ~MVNETA_FORCE_UNI;
2693 mvreg_write(pp, MVNETA_PORT_CONFIG, port_cfg_reg);
2694 mvreg_write(pp, MVNETA_TYPE_PRIO, val);
2697 /* register unicast and multicast addresses */
2698 static void mvneta_set_rx_mode(struct net_device *dev)
2700 struct mvneta_port *pp = netdev_priv(dev);
2701 struct netdev_hw_addr *ha;
2703 if (dev->flags & IFF_PROMISC) {
2704 /* Accept all: Multicast + Unicast */
2705 mvneta_rx_unicast_promisc_set(pp, 1);
2706 mvneta_set_ucast_table(pp, pp->rxq_def);
2707 mvneta_set_special_mcast_table(pp, pp->rxq_def);
2708 mvneta_set_other_mcast_table(pp, pp->rxq_def);
2709 } else {
2710 /* Accept single Unicast */
2711 mvneta_rx_unicast_promisc_set(pp, 0);
2712 mvneta_set_ucast_table(pp, -1);
2713 mvneta_mac_addr_set(pp, dev->dev_addr, pp->rxq_def);
2715 if (dev->flags & IFF_ALLMULTI) {
2716 /* Accept all multicast */
2717 mvneta_set_special_mcast_table(pp, pp->rxq_def);
2718 mvneta_set_other_mcast_table(pp, pp->rxq_def);
2719 } else {
2720 /* Accept only initialized multicast */
2721 mvneta_set_special_mcast_table(pp, -1);
2722 mvneta_set_other_mcast_table(pp, -1);
2724 if (!netdev_mc_empty(dev)) {
2725 netdev_for_each_mc_addr(ha, dev) {
2726 mvneta_mcast_addr_set(pp, ha->addr,
2727 pp->rxq_def);
2734 /* Interrupt handling - the callback for request_irq() */
2735 static irqreturn_t mvneta_isr(int irq, void *dev_id)
2737 struct mvneta_port *pp = (struct mvneta_port *)dev_id;
2739 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
2740 napi_schedule(&pp->napi);
2742 return IRQ_HANDLED;
2745 /* Interrupt handling - the callback for request_percpu_irq() */
2746 static irqreturn_t mvneta_percpu_isr(int irq, void *dev_id)
2748 struct mvneta_pcpu_port *port = (struct mvneta_pcpu_port *)dev_id;
2750 disable_percpu_irq(port->pp->dev->irq);
2751 napi_schedule(&port->napi);
2753 return IRQ_HANDLED;
2756 static void mvneta_link_change(struct mvneta_port *pp)
2758 u32 gmac_stat = mvreg_read(pp, MVNETA_GMAC_STATUS);
2760 phylink_mac_change(pp->phylink, !!(gmac_stat & MVNETA_GMAC_LINK_UP));
2763 /* NAPI handler
2764 * Bits 0 - 7 of the causeRxTx register indicate that are transmitted
2765 * packets on the corresponding TXQ (Bit 0 is for TX queue 1).
2766 * Bits 8 -15 of the cause Rx Tx register indicate that are received
2767 * packets on the corresponding RXQ (Bit 8 is for RX queue 0).
2768 * Each CPU has its own causeRxTx register
2770 static int mvneta_poll(struct napi_struct *napi, int budget)
2772 int rx_done = 0;
2773 u32 cause_rx_tx;
2774 int rx_queue;
2775 struct mvneta_port *pp = netdev_priv(napi->dev);
2776 struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
2778 if (!netif_running(pp->dev)) {
2779 napi_complete(napi);
2780 return rx_done;
2783 /* Read cause register */
2784 cause_rx_tx = mvreg_read(pp, MVNETA_INTR_NEW_CAUSE);
2785 if (cause_rx_tx & MVNETA_MISCINTR_INTR_MASK) {
2786 u32 cause_misc = mvreg_read(pp, MVNETA_INTR_MISC_CAUSE);
2788 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
2790 if (cause_misc & (MVNETA_CAUSE_PHY_STATUS_CHANGE |
2791 MVNETA_CAUSE_LINK_CHANGE))
2792 mvneta_link_change(pp);
2795 /* Release Tx descriptors */
2796 if (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL) {
2797 mvneta_tx_done_gbe(pp, (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL));
2798 cause_rx_tx &= ~MVNETA_TX_INTR_MASK_ALL;
2801 /* For the case where the last mvneta_poll did not process all
2802 * RX packets
2804 cause_rx_tx |= pp->neta_armada3700 ? pp->cause_rx_tx :
2805 port->cause_rx_tx;
2807 rx_queue = fls(((cause_rx_tx >> 8) & 0xff));
2808 if (rx_queue) {
2809 rx_queue = rx_queue - 1;
2810 if (pp->bm_priv)
2811 rx_done = mvneta_rx_hwbm(napi, pp, budget,
2812 &pp->rxqs[rx_queue]);
2813 else
2814 rx_done = mvneta_rx_swbm(napi, pp, budget,
2815 &pp->rxqs[rx_queue]);
2818 if (rx_done < budget) {
2819 cause_rx_tx = 0;
2820 napi_complete_done(napi, rx_done);
2822 if (pp->neta_armada3700) {
2823 unsigned long flags;
2825 local_irq_save(flags);
2826 mvreg_write(pp, MVNETA_INTR_NEW_MASK,
2827 MVNETA_RX_INTR_MASK(rxq_number) |
2828 MVNETA_TX_INTR_MASK(txq_number) |
2829 MVNETA_MISCINTR_INTR_MASK);
2830 local_irq_restore(flags);
2831 } else {
2832 enable_percpu_irq(pp->dev->irq, 0);
2836 if (pp->neta_armada3700)
2837 pp->cause_rx_tx = cause_rx_tx;
2838 else
2839 port->cause_rx_tx = cause_rx_tx;
2841 return rx_done;
2844 /* Handle rxq fill: allocates rxq skbs; called when initializing a port */
2845 static int mvneta_rxq_fill(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
2846 int num)
2848 int i;
2850 for (i = 0; i < num; i++) {
2851 memset(rxq->descs + i, 0, sizeof(struct mvneta_rx_desc));
2852 if (mvneta_rx_refill(pp, rxq->descs + i, rxq,
2853 GFP_KERNEL) != 0) {
2854 netdev_err(pp->dev,
2855 "%s:rxq %d, %d of %d buffs filled\n",
2856 __func__, rxq->id, i, num);
2857 break;
2861 /* Add this number of RX descriptors as non occupied (ready to
2862 * get packets)
2864 mvneta_rxq_non_occup_desc_add(pp, rxq, i);
2866 return i;
2869 /* Free all packets pending transmit from all TXQs and reset TX port */
2870 static void mvneta_tx_reset(struct mvneta_port *pp)
2872 int queue;
2874 /* free the skb's in the tx ring */
2875 for (queue = 0; queue < txq_number; queue++)
2876 mvneta_txq_done_force(pp, &pp->txqs[queue]);
2878 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
2879 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
2882 static void mvneta_rx_reset(struct mvneta_port *pp)
2884 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
2885 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
2888 /* Rx/Tx queue initialization/cleanup methods */
2890 static int mvneta_rxq_sw_init(struct mvneta_port *pp,
2891 struct mvneta_rx_queue *rxq)
2893 rxq->size = pp->rx_ring_size;
2895 /* Allocate memory for RX descriptors */
2896 rxq->descs = dma_alloc_coherent(pp->dev->dev.parent,
2897 rxq->size * MVNETA_DESC_ALIGNED_SIZE,
2898 &rxq->descs_phys, GFP_KERNEL);
2899 if (!rxq->descs)
2900 return -ENOMEM;
2902 rxq->last_desc = rxq->size - 1;
2904 return 0;
2907 static void mvneta_rxq_hw_init(struct mvneta_port *pp,
2908 struct mvneta_rx_queue *rxq)
2910 /* Set Rx descriptors queue starting address */
2911 mvreg_write(pp, MVNETA_RXQ_BASE_ADDR_REG(rxq->id), rxq->descs_phys);
2912 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), rxq->size);
2914 /* Set coalescing pkts and time */
2915 mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
2916 mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
2918 if (!pp->bm_priv) {
2919 /* Set Offset */
2920 mvneta_rxq_offset_set(pp, rxq, 0);
2921 mvneta_rxq_buf_size_set(pp, rxq, PAGE_SIZE < SZ_64K ?
2922 PAGE_SIZE :
2923 MVNETA_RX_BUF_SIZE(pp->pkt_size));
2924 mvneta_rxq_bm_disable(pp, rxq);
2925 mvneta_rxq_fill(pp, rxq, rxq->size);
2926 } else {
2927 /* Set Offset */
2928 mvneta_rxq_offset_set(pp, rxq,
2929 NET_SKB_PAD - pp->rx_offset_correction);
2931 mvneta_rxq_bm_enable(pp, rxq);
2932 /* Fill RXQ with buffers from RX pool */
2933 mvneta_rxq_long_pool_set(pp, rxq);
2934 mvneta_rxq_short_pool_set(pp, rxq);
2935 mvneta_rxq_non_occup_desc_add(pp, rxq, rxq->size);
2939 /* Create a specified RX queue */
2940 static int mvneta_rxq_init(struct mvneta_port *pp,
2941 struct mvneta_rx_queue *rxq)
2944 int ret;
2946 ret = mvneta_rxq_sw_init(pp, rxq);
2947 if (ret < 0)
2948 return ret;
2950 mvneta_rxq_hw_init(pp, rxq);
2952 return 0;
2955 /* Cleanup Rx queue */
2956 static void mvneta_rxq_deinit(struct mvneta_port *pp,
2957 struct mvneta_rx_queue *rxq)
2959 mvneta_rxq_drop_pkts(pp, rxq);
2961 if (rxq->skb)
2962 dev_kfree_skb_any(rxq->skb);
2964 if (rxq->descs)
2965 dma_free_coherent(pp->dev->dev.parent,
2966 rxq->size * MVNETA_DESC_ALIGNED_SIZE,
2967 rxq->descs,
2968 rxq->descs_phys);
2970 rxq->descs = NULL;
2971 rxq->last_desc = 0;
2972 rxq->next_desc_to_proc = 0;
2973 rxq->descs_phys = 0;
2974 rxq->first_to_refill = 0;
2975 rxq->refill_num = 0;
2976 rxq->skb = NULL;
2977 rxq->left_size = 0;
2980 static int mvneta_txq_sw_init(struct mvneta_port *pp,
2981 struct mvneta_tx_queue *txq)
2983 int cpu;
2985 txq->size = pp->tx_ring_size;
2987 /* A queue must always have room for at least one skb.
2988 * Therefore, stop the queue when the free entries reaches
2989 * the maximum number of descriptors per skb.
2991 txq->tx_stop_threshold = txq->size - MVNETA_MAX_SKB_DESCS;
2992 txq->tx_wake_threshold = txq->tx_stop_threshold / 2;
2994 /* Allocate memory for TX descriptors */
2995 txq->descs = dma_alloc_coherent(pp->dev->dev.parent,
2996 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2997 &txq->descs_phys, GFP_KERNEL);
2998 if (!txq->descs)
2999 return -ENOMEM;
3001 txq->last_desc = txq->size - 1;
3003 txq->tx_skb = kmalloc_array(txq->size, sizeof(*txq->tx_skb),
3004 GFP_KERNEL);
3005 if (!txq->tx_skb) {
3006 dma_free_coherent(pp->dev->dev.parent,
3007 txq->size * MVNETA_DESC_ALIGNED_SIZE,
3008 txq->descs, txq->descs_phys);
3009 return -ENOMEM;
3012 /* Allocate DMA buffers for TSO MAC/IP/TCP headers */
3013 txq->tso_hdrs = dma_alloc_coherent(pp->dev->dev.parent,
3014 txq->size * TSO_HEADER_SIZE,
3015 &txq->tso_hdrs_phys, GFP_KERNEL);
3016 if (!txq->tso_hdrs) {
3017 kfree(txq->tx_skb);
3018 dma_free_coherent(pp->dev->dev.parent,
3019 txq->size * MVNETA_DESC_ALIGNED_SIZE,
3020 txq->descs, txq->descs_phys);
3021 return -ENOMEM;
3024 /* Setup XPS mapping */
3025 if (txq_number > 1)
3026 cpu = txq->id % num_present_cpus();
3027 else
3028 cpu = pp->rxq_def % num_present_cpus();
3029 cpumask_set_cpu(cpu, &txq->affinity_mask);
3030 netif_set_xps_queue(pp->dev, &txq->affinity_mask, txq->id);
3032 return 0;
3035 static void mvneta_txq_hw_init(struct mvneta_port *pp,
3036 struct mvneta_tx_queue *txq)
3038 /* Set maximum bandwidth for enabled TXQs */
3039 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0x03ffffff);
3040 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0x3fffffff);
3042 /* Set Tx descriptors queue starting address */
3043 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), txq->descs_phys);
3044 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), txq->size);
3046 mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
3049 /* Create and initialize a tx queue */
3050 static int mvneta_txq_init(struct mvneta_port *pp,
3051 struct mvneta_tx_queue *txq)
3053 int ret;
3055 ret = mvneta_txq_sw_init(pp, txq);
3056 if (ret < 0)
3057 return ret;
3059 mvneta_txq_hw_init(pp, txq);
3061 return 0;
3064 /* Free allocated resources when mvneta_txq_init() fails to allocate memory*/
3065 static void mvneta_txq_sw_deinit(struct mvneta_port *pp,
3066 struct mvneta_tx_queue *txq)
3068 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
3070 kfree(txq->tx_skb);
3072 if (txq->tso_hdrs)
3073 dma_free_coherent(pp->dev->dev.parent,
3074 txq->size * TSO_HEADER_SIZE,
3075 txq->tso_hdrs, txq->tso_hdrs_phys);
3076 if (txq->descs)
3077 dma_free_coherent(pp->dev->dev.parent,
3078 txq->size * MVNETA_DESC_ALIGNED_SIZE,
3079 txq->descs, txq->descs_phys);
3081 netdev_tx_reset_queue(nq);
3083 txq->descs = NULL;
3084 txq->last_desc = 0;
3085 txq->next_desc_to_proc = 0;
3086 txq->descs_phys = 0;
3089 static void mvneta_txq_hw_deinit(struct mvneta_port *pp,
3090 struct mvneta_tx_queue *txq)
3092 /* Set minimum bandwidth for disabled TXQs */
3093 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0);
3094 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0);
3096 /* Set Tx descriptors queue starting address and size */
3097 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), 0);
3098 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), 0);
3101 static void mvneta_txq_deinit(struct mvneta_port *pp,
3102 struct mvneta_tx_queue *txq)
3104 mvneta_txq_sw_deinit(pp, txq);
3105 mvneta_txq_hw_deinit(pp, txq);
3108 /* Cleanup all Tx queues */
3109 static void mvneta_cleanup_txqs(struct mvneta_port *pp)
3111 int queue;
3113 for (queue = 0; queue < txq_number; queue++)
3114 mvneta_txq_deinit(pp, &pp->txqs[queue]);
3117 /* Cleanup all Rx queues */
3118 static void mvneta_cleanup_rxqs(struct mvneta_port *pp)
3120 int queue;
3122 for (queue = 0; queue < rxq_number; queue++)
3123 mvneta_rxq_deinit(pp, &pp->rxqs[queue]);
3127 /* Init all Rx queues */
3128 static int mvneta_setup_rxqs(struct mvneta_port *pp)
3130 int queue;
3132 for (queue = 0; queue < rxq_number; queue++) {
3133 int err = mvneta_rxq_init(pp, &pp->rxqs[queue]);
3135 if (err) {
3136 netdev_err(pp->dev, "%s: can't create rxq=%d\n",
3137 __func__, queue);
3138 mvneta_cleanup_rxqs(pp);
3139 return err;
3143 return 0;
3146 /* Init all tx queues */
3147 static int mvneta_setup_txqs(struct mvneta_port *pp)
3149 int queue;
3151 for (queue = 0; queue < txq_number; queue++) {
3152 int err = mvneta_txq_init(pp, &pp->txqs[queue]);
3153 if (err) {
3154 netdev_err(pp->dev, "%s: can't create txq=%d\n",
3155 __func__, queue);
3156 mvneta_cleanup_txqs(pp);
3157 return err;
3161 return 0;
3164 static void mvneta_start_dev(struct mvneta_port *pp)
3166 int cpu;
3168 mvneta_max_rx_size_set(pp, pp->pkt_size);
3169 mvneta_txq_max_tx_size_set(pp, pp->pkt_size);
3171 /* start the Rx/Tx activity */
3172 mvneta_port_enable(pp);
3174 if (!pp->neta_armada3700) {
3175 /* Enable polling on the port */
3176 for_each_online_cpu(cpu) {
3177 struct mvneta_pcpu_port *port =
3178 per_cpu_ptr(pp->ports, cpu);
3180 napi_enable(&port->napi);
3182 } else {
3183 napi_enable(&pp->napi);
3186 /* Unmask interrupts. It has to be done from each CPU */
3187 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
3189 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
3190 MVNETA_CAUSE_PHY_STATUS_CHANGE |
3191 MVNETA_CAUSE_LINK_CHANGE);
3193 phylink_start(pp->phylink);
3194 netif_tx_start_all_queues(pp->dev);
3197 static void mvneta_stop_dev(struct mvneta_port *pp)
3199 unsigned int cpu;
3201 phylink_stop(pp->phylink);
3203 if (!pp->neta_armada3700) {
3204 for_each_online_cpu(cpu) {
3205 struct mvneta_pcpu_port *port =
3206 per_cpu_ptr(pp->ports, cpu);
3208 napi_disable(&port->napi);
3210 } else {
3211 napi_disable(&pp->napi);
3214 netif_carrier_off(pp->dev);
3216 mvneta_port_down(pp);
3217 netif_tx_stop_all_queues(pp->dev);
3219 /* Stop the port activity */
3220 mvneta_port_disable(pp);
3222 /* Clear all ethernet port interrupts */
3223 on_each_cpu(mvneta_percpu_clear_intr_cause, pp, true);
3225 /* Mask all ethernet port interrupts */
3226 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
3228 mvneta_tx_reset(pp);
3229 mvneta_rx_reset(pp);
3232 static void mvneta_percpu_enable(void *arg)
3234 struct mvneta_port *pp = arg;
3236 enable_percpu_irq(pp->dev->irq, IRQ_TYPE_NONE);
3239 static void mvneta_percpu_disable(void *arg)
3241 struct mvneta_port *pp = arg;
3243 disable_percpu_irq(pp->dev->irq);
3246 /* Change the device mtu */
3247 static int mvneta_change_mtu(struct net_device *dev, int mtu)
3249 struct mvneta_port *pp = netdev_priv(dev);
3250 int ret;
3252 if (!IS_ALIGNED(MVNETA_RX_PKT_SIZE(mtu), 8)) {
3253 netdev_info(dev, "Illegal MTU value %d, rounding to %d\n",
3254 mtu, ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8));
3255 mtu = ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8);
3258 dev->mtu = mtu;
3260 if (!netif_running(dev)) {
3261 if (pp->bm_priv)
3262 mvneta_bm_update_mtu(pp, mtu);
3264 netdev_update_features(dev);
3265 return 0;
3268 /* The interface is running, so we have to force a
3269 * reallocation of the queues
3271 mvneta_stop_dev(pp);
3272 on_each_cpu(mvneta_percpu_disable, pp, true);
3274 mvneta_cleanup_txqs(pp);
3275 mvneta_cleanup_rxqs(pp);
3277 if (pp->bm_priv)
3278 mvneta_bm_update_mtu(pp, mtu);
3280 pp->pkt_size = MVNETA_RX_PKT_SIZE(dev->mtu);
3282 ret = mvneta_setup_rxqs(pp);
3283 if (ret) {
3284 netdev_err(dev, "unable to setup rxqs after MTU change\n");
3285 return ret;
3288 ret = mvneta_setup_txqs(pp);
3289 if (ret) {
3290 netdev_err(dev, "unable to setup txqs after MTU change\n");
3291 return ret;
3294 on_each_cpu(mvneta_percpu_enable, pp, true);
3295 mvneta_start_dev(pp);
3297 netdev_update_features(dev);
3299 return 0;
3302 static netdev_features_t mvneta_fix_features(struct net_device *dev,
3303 netdev_features_t features)
3305 struct mvneta_port *pp = netdev_priv(dev);
3307 if (pp->tx_csum_limit && dev->mtu > pp->tx_csum_limit) {
3308 features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
3309 netdev_info(dev,
3310 "Disable IP checksum for MTU greater than %dB\n",
3311 pp->tx_csum_limit);
3314 return features;
3317 /* Get mac address */
3318 static void mvneta_get_mac_addr(struct mvneta_port *pp, unsigned char *addr)
3320 u32 mac_addr_l, mac_addr_h;
3322 mac_addr_l = mvreg_read(pp, MVNETA_MAC_ADDR_LOW);
3323 mac_addr_h = mvreg_read(pp, MVNETA_MAC_ADDR_HIGH);
3324 addr[0] = (mac_addr_h >> 24) & 0xFF;
3325 addr[1] = (mac_addr_h >> 16) & 0xFF;
3326 addr[2] = (mac_addr_h >> 8) & 0xFF;
3327 addr[3] = mac_addr_h & 0xFF;
3328 addr[4] = (mac_addr_l >> 8) & 0xFF;
3329 addr[5] = mac_addr_l & 0xFF;
3332 /* Handle setting mac address */
3333 static int mvneta_set_mac_addr(struct net_device *dev, void *addr)
3335 struct mvneta_port *pp = netdev_priv(dev);
3336 struct sockaddr *sockaddr = addr;
3337 int ret;
3339 ret = eth_prepare_mac_addr_change(dev, addr);
3340 if (ret < 0)
3341 return ret;
3342 /* Remove previous address table entry */
3343 mvneta_mac_addr_set(pp, dev->dev_addr, -1);
3345 /* Set new addr in hw */
3346 mvneta_mac_addr_set(pp, sockaddr->sa_data, pp->rxq_def);
3348 eth_commit_mac_addr_change(dev, addr);
3349 return 0;
3352 static void mvneta_validate(struct net_device *ndev, unsigned long *supported,
3353 struct phylink_link_state *state)
3355 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
3357 /* We only support QSGMII, SGMII, 802.3z and RGMII modes */
3358 if (state->interface != PHY_INTERFACE_MODE_NA &&
3359 state->interface != PHY_INTERFACE_MODE_QSGMII &&
3360 state->interface != PHY_INTERFACE_MODE_SGMII &&
3361 !phy_interface_mode_is_8023z(state->interface) &&
3362 !phy_interface_mode_is_rgmii(state->interface)) {
3363 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
3364 return;
3367 /* Allow all the expected bits */
3368 phylink_set(mask, Autoneg);
3369 phylink_set_port_modes(mask);
3371 /* Asymmetric pause is unsupported */
3372 phylink_set(mask, Pause);
3373 /* Half-duplex at speeds higher than 100Mbit is unsupported */
3374 phylink_set(mask, 1000baseT_Full);
3375 phylink_set(mask, 1000baseX_Full);
3377 if (!phy_interface_mode_is_8023z(state->interface)) {
3378 /* 10M and 100M are only supported in non-802.3z mode */
3379 phylink_set(mask, 10baseT_Half);
3380 phylink_set(mask, 10baseT_Full);
3381 phylink_set(mask, 100baseT_Half);
3382 phylink_set(mask, 100baseT_Full);
3385 bitmap_and(supported, supported, mask,
3386 __ETHTOOL_LINK_MODE_MASK_NBITS);
3387 bitmap_and(state->advertising, state->advertising, mask,
3388 __ETHTOOL_LINK_MODE_MASK_NBITS);
3391 static int mvneta_mac_link_state(struct net_device *ndev,
3392 struct phylink_link_state *state)
3394 struct mvneta_port *pp = netdev_priv(ndev);
3395 u32 gmac_stat;
3397 gmac_stat = mvreg_read(pp, MVNETA_GMAC_STATUS);
3399 if (gmac_stat & MVNETA_GMAC_SPEED_1000)
3400 state->speed = SPEED_1000;
3401 else if (gmac_stat & MVNETA_GMAC_SPEED_100)
3402 state->speed = SPEED_100;
3403 else
3404 state->speed = SPEED_10;
3406 state->an_complete = !!(gmac_stat & MVNETA_GMAC_AN_COMPLETE);
3407 state->link = !!(gmac_stat & MVNETA_GMAC_LINK_UP);
3408 state->duplex = !!(gmac_stat & MVNETA_GMAC_FULL_DUPLEX);
3410 state->pause = 0;
3411 if (gmac_stat & MVNETA_GMAC_RX_FLOW_CTRL_ENABLE)
3412 state->pause |= MLO_PAUSE_RX;
3413 if (gmac_stat & MVNETA_GMAC_TX_FLOW_CTRL_ENABLE)
3414 state->pause |= MLO_PAUSE_TX;
3416 return 1;
3419 static void mvneta_mac_an_restart(struct net_device *ndev)
3421 struct mvneta_port *pp = netdev_priv(ndev);
3422 u32 gmac_an = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3424 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
3425 gmac_an | MVNETA_GMAC_INBAND_RESTART_AN);
3426 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
3427 gmac_an & ~MVNETA_GMAC_INBAND_RESTART_AN);
3430 static void mvneta_mac_config(struct net_device *ndev, unsigned int mode,
3431 const struct phylink_link_state *state)
3433 struct mvneta_port *pp = netdev_priv(ndev);
3434 u32 new_ctrl0, gmac_ctrl0 = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
3435 u32 new_ctrl2, gmac_ctrl2 = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
3436 u32 new_clk, gmac_clk = mvreg_read(pp, MVNETA_GMAC_CLOCK_DIVIDER);
3437 u32 new_an, gmac_an = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3439 new_ctrl0 = gmac_ctrl0 & ~MVNETA_GMAC0_PORT_1000BASE_X;
3440 new_ctrl2 = gmac_ctrl2 & ~(MVNETA_GMAC2_INBAND_AN_ENABLE |
3441 MVNETA_GMAC2_PORT_RESET);
3442 new_clk = gmac_clk & ~MVNETA_GMAC_1MS_CLOCK_ENABLE;
3443 new_an = gmac_an & ~(MVNETA_GMAC_INBAND_AN_ENABLE |
3444 MVNETA_GMAC_INBAND_RESTART_AN |
3445 MVNETA_GMAC_CONFIG_MII_SPEED |
3446 MVNETA_GMAC_CONFIG_GMII_SPEED |
3447 MVNETA_GMAC_AN_SPEED_EN |
3448 MVNETA_GMAC_ADVERT_SYM_FLOW_CTRL |
3449 MVNETA_GMAC_CONFIG_FLOW_CTRL |
3450 MVNETA_GMAC_AN_FLOW_CTRL_EN |
3451 MVNETA_GMAC_CONFIG_FULL_DUPLEX |
3452 MVNETA_GMAC_AN_DUPLEX_EN);
3454 /* Even though it might look weird, when we're configured in
3455 * SGMII or QSGMII mode, the RGMII bit needs to be set.
3457 new_ctrl2 |= MVNETA_GMAC2_PORT_RGMII;
3459 if (state->interface == PHY_INTERFACE_MODE_QSGMII ||
3460 state->interface == PHY_INTERFACE_MODE_SGMII ||
3461 phy_interface_mode_is_8023z(state->interface))
3462 new_ctrl2 |= MVNETA_GMAC2_PCS_ENABLE;
3464 if (phylink_test(state->advertising, Pause))
3465 new_an |= MVNETA_GMAC_ADVERT_SYM_FLOW_CTRL;
3466 if (state->pause & MLO_PAUSE_TXRX_MASK)
3467 new_an |= MVNETA_GMAC_CONFIG_FLOW_CTRL;
3469 if (!phylink_autoneg_inband(mode)) {
3470 /* Phy or fixed speed */
3471 if (state->duplex)
3472 new_an |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
3474 if (state->speed == SPEED_1000)
3475 new_an |= MVNETA_GMAC_CONFIG_GMII_SPEED;
3476 else if (state->speed == SPEED_100)
3477 new_an |= MVNETA_GMAC_CONFIG_MII_SPEED;
3478 } else if (state->interface == PHY_INTERFACE_MODE_SGMII) {
3479 /* SGMII mode receives the state from the PHY */
3480 new_ctrl2 |= MVNETA_GMAC2_INBAND_AN_ENABLE;
3481 new_clk |= MVNETA_GMAC_1MS_CLOCK_ENABLE;
3482 new_an = (new_an & ~(MVNETA_GMAC_FORCE_LINK_DOWN |
3483 MVNETA_GMAC_FORCE_LINK_PASS)) |
3484 MVNETA_GMAC_INBAND_AN_ENABLE |
3485 MVNETA_GMAC_AN_SPEED_EN |
3486 MVNETA_GMAC_AN_DUPLEX_EN;
3487 } else {
3488 /* 802.3z negotiation - only 1000base-X */
3489 new_ctrl0 |= MVNETA_GMAC0_PORT_1000BASE_X;
3490 new_clk |= MVNETA_GMAC_1MS_CLOCK_ENABLE;
3491 new_an = (new_an & ~(MVNETA_GMAC_FORCE_LINK_DOWN |
3492 MVNETA_GMAC_FORCE_LINK_PASS)) |
3493 MVNETA_GMAC_INBAND_AN_ENABLE |
3494 MVNETA_GMAC_CONFIG_GMII_SPEED |
3495 /* The MAC only supports FD mode */
3496 MVNETA_GMAC_CONFIG_FULL_DUPLEX;
3498 if (state->pause & MLO_PAUSE_AN && state->an_enabled)
3499 new_an |= MVNETA_GMAC_AN_FLOW_CTRL_EN;
3502 /* Armada 370 documentation says we can only change the port mode
3503 * and in-band enable when the link is down, so force it down
3504 * while making these changes. We also do this for GMAC_CTRL2 */
3505 if ((new_ctrl0 ^ gmac_ctrl0) & MVNETA_GMAC0_PORT_1000BASE_X ||
3506 (new_ctrl2 ^ gmac_ctrl2) & MVNETA_GMAC2_INBAND_AN_ENABLE ||
3507 (new_an ^ gmac_an) & MVNETA_GMAC_INBAND_AN_ENABLE) {
3508 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
3509 (gmac_an & ~MVNETA_GMAC_FORCE_LINK_PASS) |
3510 MVNETA_GMAC_FORCE_LINK_DOWN);
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_clk != gmac_clk)
3518 mvreg_write(pp, MVNETA_GMAC_CLOCK_DIVIDER, new_clk);
3519 if (new_an != gmac_an)
3520 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, new_an);
3522 if (gmac_ctrl2 & MVNETA_GMAC2_PORT_RESET) {
3523 while ((mvreg_read(pp, MVNETA_GMAC_CTRL_2) &
3524 MVNETA_GMAC2_PORT_RESET) != 0)
3525 continue;
3529 static void mvneta_set_eee(struct mvneta_port *pp, bool enable)
3531 u32 lpi_ctl1;
3533 lpi_ctl1 = mvreg_read(pp, MVNETA_LPI_CTRL_1);
3534 if (enable)
3535 lpi_ctl1 |= MVNETA_LPI_REQUEST_ENABLE;
3536 else
3537 lpi_ctl1 &= ~MVNETA_LPI_REQUEST_ENABLE;
3538 mvreg_write(pp, MVNETA_LPI_CTRL_1, lpi_ctl1);
3541 static void mvneta_mac_link_down(struct net_device *ndev, unsigned int mode,
3542 phy_interface_t interface)
3544 struct mvneta_port *pp = netdev_priv(ndev);
3545 u32 val;
3547 mvneta_port_down(pp);
3549 if (!phylink_autoneg_inband(mode)) {
3550 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3551 val &= ~MVNETA_GMAC_FORCE_LINK_PASS;
3552 val |= MVNETA_GMAC_FORCE_LINK_DOWN;
3553 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
3556 pp->eee_active = false;
3557 mvneta_set_eee(pp, false);
3560 static void mvneta_mac_link_up(struct net_device *ndev, unsigned int mode,
3561 phy_interface_t interface,
3562 struct phy_device *phy)
3564 struct mvneta_port *pp = netdev_priv(ndev);
3565 u32 val;
3567 if (!phylink_autoneg_inband(mode)) {
3568 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3569 val &= ~MVNETA_GMAC_FORCE_LINK_DOWN;
3570 val |= MVNETA_GMAC_FORCE_LINK_PASS;
3571 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
3574 mvneta_port_up(pp);
3576 if (phy && pp->eee_enabled) {
3577 pp->eee_active = phy_init_eee(phy, 0) >= 0;
3578 mvneta_set_eee(pp, pp->eee_active && pp->tx_lpi_enabled);
3582 static const struct phylink_mac_ops mvneta_phylink_ops = {
3583 .validate = mvneta_validate,
3584 .mac_link_state = mvneta_mac_link_state,
3585 .mac_an_restart = mvneta_mac_an_restart,
3586 .mac_config = mvneta_mac_config,
3587 .mac_link_down = mvneta_mac_link_down,
3588 .mac_link_up = mvneta_mac_link_up,
3591 static int mvneta_mdio_probe(struct mvneta_port *pp)
3593 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
3594 int err = phylink_of_phy_connect(pp->phylink, pp->dn, 0);
3596 if (err)
3597 netdev_err(pp->dev, "could not attach PHY: %d\n", err);
3599 phylink_ethtool_get_wol(pp->phylink, &wol);
3600 device_set_wakeup_capable(&pp->dev->dev, !!wol.supported);
3602 return err;
3605 static void mvneta_mdio_remove(struct mvneta_port *pp)
3607 phylink_disconnect_phy(pp->phylink);
3610 /* Electing a CPU must be done in an atomic way: it should be done
3611 * after or before the removal/insertion of a CPU and this function is
3612 * not reentrant.
3614 static void mvneta_percpu_elect(struct mvneta_port *pp)
3616 int elected_cpu = 0, max_cpu, cpu, i = 0;
3618 /* Use the cpu associated to the rxq when it is online, in all
3619 * the other cases, use the cpu 0 which can't be offline.
3621 if (cpu_online(pp->rxq_def))
3622 elected_cpu = pp->rxq_def;
3624 max_cpu = num_present_cpus();
3626 for_each_online_cpu(cpu) {
3627 int rxq_map = 0, txq_map = 0;
3628 int rxq;
3630 for (rxq = 0; rxq < rxq_number; rxq++)
3631 if ((rxq % max_cpu) == cpu)
3632 rxq_map |= MVNETA_CPU_RXQ_ACCESS(rxq);
3634 if (cpu == elected_cpu)
3635 /* Map the default receive queue queue to the
3636 * elected CPU
3638 rxq_map |= MVNETA_CPU_RXQ_ACCESS(pp->rxq_def);
3640 /* We update the TX queue map only if we have one
3641 * queue. In this case we associate the TX queue to
3642 * the CPU bound to the default RX queue
3644 if (txq_number == 1)
3645 txq_map = (cpu == elected_cpu) ?
3646 MVNETA_CPU_TXQ_ACCESS(1) : 0;
3647 else
3648 txq_map = mvreg_read(pp, MVNETA_CPU_MAP(cpu)) &
3649 MVNETA_CPU_TXQ_ACCESS_ALL_MASK;
3651 mvreg_write(pp, MVNETA_CPU_MAP(cpu), rxq_map | txq_map);
3653 /* Update the interrupt mask on each CPU according the
3654 * new mapping
3656 smp_call_function_single(cpu, mvneta_percpu_unmask_interrupt,
3657 pp, true);
3658 i++;
3663 static int mvneta_cpu_online(unsigned int cpu, struct hlist_node *node)
3665 int other_cpu;
3666 struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
3667 node_online);
3668 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
3671 spin_lock(&pp->lock);
3673 * Configuring the driver for a new CPU while the driver is
3674 * stopping is racy, so just avoid it.
3676 if (pp->is_stopped) {
3677 spin_unlock(&pp->lock);
3678 return 0;
3680 netif_tx_stop_all_queues(pp->dev);
3683 * We have to synchronise on tha napi of each CPU except the one
3684 * just being woken up
3686 for_each_online_cpu(other_cpu) {
3687 if (other_cpu != cpu) {
3688 struct mvneta_pcpu_port *other_port =
3689 per_cpu_ptr(pp->ports, other_cpu);
3691 napi_synchronize(&other_port->napi);
3695 /* Mask all ethernet port interrupts */
3696 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
3697 napi_enable(&port->napi);
3700 * Enable per-CPU interrupts on the CPU that is
3701 * brought up.
3703 mvneta_percpu_enable(pp);
3706 * Enable per-CPU interrupt on the one CPU we care
3707 * about.
3709 mvneta_percpu_elect(pp);
3711 /* Unmask all ethernet port interrupts */
3712 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
3713 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
3714 MVNETA_CAUSE_PHY_STATUS_CHANGE |
3715 MVNETA_CAUSE_LINK_CHANGE);
3716 netif_tx_start_all_queues(pp->dev);
3717 spin_unlock(&pp->lock);
3718 return 0;
3721 static int mvneta_cpu_down_prepare(unsigned int cpu, struct hlist_node *node)
3723 struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
3724 node_online);
3725 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
3728 * Thanks to this lock we are sure that any pending cpu election is
3729 * done.
3731 spin_lock(&pp->lock);
3732 /* Mask all ethernet port interrupts */
3733 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
3734 spin_unlock(&pp->lock);
3736 napi_synchronize(&port->napi);
3737 napi_disable(&port->napi);
3738 /* Disable per-CPU interrupts on the CPU that is brought down. */
3739 mvneta_percpu_disable(pp);
3740 return 0;
3743 static int mvneta_cpu_dead(unsigned int cpu, struct hlist_node *node)
3745 struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
3746 node_dead);
3748 /* Check if a new CPU must be elected now this on is down */
3749 spin_lock(&pp->lock);
3750 mvneta_percpu_elect(pp);
3751 spin_unlock(&pp->lock);
3752 /* Unmask all ethernet port interrupts */
3753 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
3754 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
3755 MVNETA_CAUSE_PHY_STATUS_CHANGE |
3756 MVNETA_CAUSE_LINK_CHANGE);
3757 netif_tx_start_all_queues(pp->dev);
3758 return 0;
3761 static int mvneta_open(struct net_device *dev)
3763 struct mvneta_port *pp = netdev_priv(dev);
3764 int ret;
3766 pp->pkt_size = MVNETA_RX_PKT_SIZE(pp->dev->mtu);
3768 ret = mvneta_setup_rxqs(pp);
3769 if (ret)
3770 return ret;
3772 ret = mvneta_setup_txqs(pp);
3773 if (ret)
3774 goto err_cleanup_rxqs;
3776 /* Connect to port interrupt line */
3777 if (pp->neta_armada3700)
3778 ret = request_irq(pp->dev->irq, mvneta_isr, 0,
3779 dev->name, pp);
3780 else
3781 ret = request_percpu_irq(pp->dev->irq, mvneta_percpu_isr,
3782 dev->name, pp->ports);
3783 if (ret) {
3784 netdev_err(pp->dev, "cannot request irq %d\n", pp->dev->irq);
3785 goto err_cleanup_txqs;
3788 if (!pp->neta_armada3700) {
3789 /* Enable per-CPU interrupt on all the CPU to handle our RX
3790 * queue interrupts
3792 on_each_cpu(mvneta_percpu_enable, pp, true);
3794 pp->is_stopped = false;
3795 /* Register a CPU notifier to handle the case where our CPU
3796 * might be taken offline.
3798 ret = cpuhp_state_add_instance_nocalls(online_hpstate,
3799 &pp->node_online);
3800 if (ret)
3801 goto err_free_irq;
3803 ret = cpuhp_state_add_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
3804 &pp->node_dead);
3805 if (ret)
3806 goto err_free_online_hp;
3809 /* In default link is down */
3810 netif_carrier_off(pp->dev);
3812 ret = mvneta_mdio_probe(pp);
3813 if (ret < 0) {
3814 netdev_err(dev, "cannot probe MDIO bus\n");
3815 goto err_free_dead_hp;
3818 mvneta_start_dev(pp);
3820 return 0;
3822 err_free_dead_hp:
3823 if (!pp->neta_armada3700)
3824 cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
3825 &pp->node_dead);
3826 err_free_online_hp:
3827 if (!pp->neta_armada3700)
3828 cpuhp_state_remove_instance_nocalls(online_hpstate,
3829 &pp->node_online);
3830 err_free_irq:
3831 if (pp->neta_armada3700) {
3832 free_irq(pp->dev->irq, pp);
3833 } else {
3834 on_each_cpu(mvneta_percpu_disable, pp, true);
3835 free_percpu_irq(pp->dev->irq, pp->ports);
3837 err_cleanup_txqs:
3838 mvneta_cleanup_txqs(pp);
3839 err_cleanup_rxqs:
3840 mvneta_cleanup_rxqs(pp);
3841 return ret;
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,
3863 &pp->node_online);
3864 cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
3865 &pp->node_dead);
3866 on_each_cpu(mvneta_percpu_disable, pp, true);
3867 free_percpu_irq(dev->irq, pp->ports);
3868 } else {
3869 mvneta_stop_dev(pp);
3870 mvneta_mdio_remove(pp);
3871 free_irq(dev->irq, pp);
3874 mvneta_cleanup_rxqs(pp);
3875 mvneta_cleanup_txqs(pp);
3877 return 0;
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 */
3890 static int
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 */
3900 static int
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);
3921 int queue;
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);
3937 return 0;
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;
3950 return 0;
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))
3983 return -EINVAL;
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)) {
3994 mvneta_stop(dev);
3995 if (mvneta_open(dev)) {
3996 netdev_err(dev,
3997 "error on opening device after ring param change\n");
3998 return -ENOMEM;
4002 return 0;
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,
4022 u8 *data)
4024 if (sset == ETH_SS_STATS) {
4025 int i;
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;
4037 u32 high, low;
4038 u64 val;
4039 int i;
4041 for (i = 0, s = mvneta_statistics;
4042 s < mvneta_statistics + ARRAY_SIZE(mvneta_statistics);
4043 s++, i++) {
4044 val = 0;
4046 switch (s->type) {
4047 case T_REG_32:
4048 val = readl_relaxed(base + s->offset);
4049 break;
4050 case T_REG_64:
4051 /* Docs say to read low 32-bit then high */
4052 low = readl_relaxed(base + s->offset);
4053 high = readl_relaxed(base + s->offset + 4);
4054 val = (u64)high << 32 | low;
4055 break;
4056 case T_SW:
4057 switch (s->offset) {
4058 case ETHTOOL_STAT_EEE_WAKEUP:
4059 val = phylink_get_eee_err(pp->phylink);
4060 break;
4061 case ETHTOOL_STAT_SKB_ALLOC_ERR:
4062 val = pp->rxqs[0].skb_alloc_err;
4063 break;
4064 case ETHTOOL_STAT_REFILL_ERR:
4065 val = pp->rxqs[0].refill_err;
4066 break;
4068 break;
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);
4079 int i;
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);
4091 return -EOPNOTSUPP;
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;
4106 return 0;
4107 case ETHTOOL_GRXFH:
4108 return -EOPNOTSUPP;
4109 default:
4110 return -EOPNOTSUPP;
4114 static int mvneta_config_rss(struct mvneta_port *pp)
4116 int cpu;
4117 u32 val;
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);
4132 } else {
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);
4159 } else {
4160 napi_enable(&pp->napi);
4163 netif_tx_start_all_queues(pp->dev);
4165 return 0;
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)
4175 return -EOPNOTSUPP;
4177 /* We require at least one supported parameter to be changed
4178 * and no change in any of the unsupported parameters
4180 if (key ||
4181 (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
4182 return -EOPNOTSUPP;
4184 if (!indir)
4185 return 0;
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,
4193 u8 *hfunc)
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)
4199 return -EOPNOTSUPP;
4201 if (hfunc)
4202 *hfunc = ETH_RSS_HASH_TOP;
4204 if (!indir)
4205 return 0;
4207 memcpy(indir, pp->indir, MVNETA_RSS_LU_TABLE_SIZE);
4209 return 0;
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);
4224 int ret;
4226 ret = phylink_ethtool_set_wol(pp->phylink, wol);
4227 if (!ret)
4228 device_set_wakeup_enable(&dev->dev, !!wol->wolopts);
4230 return ret;
4233 static int mvneta_ethtool_get_eee(struct net_device *dev,
4234 struct ethtool_eee *eee)
4236 struct mvneta_port *pp = netdev_priv(dev);
4237 u32 lpi_ctl0;
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);
4253 u32 lpi_ctl0;
4255 /* The Armada 37x documents do not give limits for this other than
4256 * it being an 8-bit register. */
4257 if (eee->tx_lpi_enabled &&
4258 (eee->tx_lpi_timer < 0 || eee->tx_lpi_timer > 255))
4259 return -EINVAL;
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,
4311 /* Initialize hw */
4312 static int mvneta_init(struct device *dev, struct mvneta_port *pp)
4314 int queue;
4316 /* Disable port */
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);
4323 if (!pp->txqs)
4324 return -ENOMEM;
4326 /* Initialize TX descriptor rings */
4327 for (queue = 0; queue < txq_number; queue++) {
4328 struct mvneta_tx_queue *txq = &pp->txqs[queue];
4329 txq->id = queue;
4330 txq->size = pp->tx_ring_size;
4331 txq->done_pkts_coal = MVNETA_TXDONE_COAL_PKTS;
4334 pp->rxqs = devm_kcalloc(dev, rxq_number, sizeof(*pp->rxqs), GFP_KERNEL);
4335 if (!pp->rxqs)
4336 return -ENOMEM;
4338 /* Create Rx descriptor rings */
4339 for (queue = 0; queue < rxq_number; queue++) {
4340 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
4341 rxq->id = queue;
4342 rxq->size = pp->rx_ring_size;
4343 rxq->pkts_coal = MVNETA_RX_COAL_PKTS;
4344 rxq->time_coal = MVNETA_RX_COAL_USEC;
4345 rxq->buf_virt_addr
4346 = devm_kmalloc_array(pp->dev->dev.parent,
4347 rxq->size,
4348 sizeof(*rxq->buf_virt_addr),
4349 GFP_KERNEL);
4350 if (!rxq->buf_virt_addr)
4351 return -ENOMEM;
4354 return 0;
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)
4361 u32 win_enable;
4362 u32 win_protect;
4363 int i;
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);
4369 if (i < 4)
4370 mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
4373 win_enable = 0x3f;
4374 win_protect = 0;
4376 if (dram) {
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);
4391 } else {
4392 /* For Armada3700 open default 4GB Mbus window, leaving
4393 * arbitration of target/attribute to a different layer
4394 * of configuration.
4396 mvreg_write(pp, MVNETA_WIN_SIZE(0), 0xffff0000);
4397 win_enable &= ~BIT(0);
4398 win_protect = 3;
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))
4417 return -EINVAL;
4419 return 0;
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;
4434 int tx_csum_limit;
4435 int phy_mode;
4436 int err;
4437 int cpu;
4439 dev = alloc_etherdev_mqs(sizeof(struct mvneta_port), txq_number, rxq_number);
4440 if (!dev)
4441 return -ENOMEM;
4443 dev->irq = irq_of_parse_and_map(dn, 0);
4444 if (dev->irq == 0) {
4445 err = -EINVAL;
4446 goto err_free_netdev;
4449 phy_mode = of_get_phy_mode(dn);
4450 if (phy_mode < 0) {
4451 dev_err(&pdev->dev, "incorrect phy-mode\n");
4452 err = -EINVAL;
4453 goto err_free_irq;
4456 phylink = phylink_create(dev, pdev->dev.fwnode, phy_mode,
4457 &mvneta_phylink_ops);
4458 if (IS_ERR(phylink)) {
4459 err = PTR_ERR(phylink);
4460 goto err_free_irq;
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;
4473 pp->dn = dn;
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);
4500 goto err_clk;
4503 /* Alloc per-cpu port structure */
4504 pp->ports = alloc_percpu(struct mvneta_pcpu_port);
4505 if (!pp->ports) {
4506 err = -ENOMEM;
4507 goto err_clk;
4510 /* Alloc per-cpu stats */
4511 pp->stats = netdev_alloc_pcpu_stats(struct mvneta_pcpu_stats);
4512 if (!pp->stats) {
4513 err = -ENOMEM;
4514 goto err_free_ports;
4517 dt_mac_addr = of_get_mac_address(dn);
4518 if (dt_mac_addr) {
4519 mac_from = "device tree";
4520 memcpy(dev->dev_addr, dt_mac_addr, ETH_ALEN);
4521 } else {
4522 mvneta_get_mac_addr(pp, hw_mac_addr);
4523 if (is_valid_ether_addr(hw_mac_addr)) {
4524 mac_from = "hardware";
4525 memcpy(dev->dev_addr, hw_mac_addr, ETH_ALEN);
4526 } else {
4527 mac_from = "random";
4528 eth_hw_addr_random(dev);
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;
4542 } else {
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
4551 * structure.
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;
4559 pp->dev = dev;
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);
4567 if (bm_node) {
4568 pp->bm_priv = mvneta_bm_get(bm_node);
4569 if (pp->bm_priv) {
4570 err = mvneta_bm_port_init(pdev, pp);
4571 if (err < 0) {
4572 dev_info(&pdev->dev,
4573 "use SW buffer management\n");
4574 mvneta_bm_put(pp->bm_priv);
4575 pp->bm_priv = NULL;
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,
4583 NET_SKB_PAD -
4584 MVNETA_RX_PKT_OFFSET_CORRECTION);
4586 of_node_put(bm_node);
4588 err = mvneta_init(&pdev->dev, pp);
4589 if (err < 0)
4590 goto err_netdev;
4592 err = mvneta_port_power_up(pp, phy_mode);
4593 if (err < 0) {
4594 dev_err(&pdev->dev, "can't power up port\n");
4595 goto err_netdev;
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);
4603 } else {
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,
4609 NAPI_POLL_WEIGHT);
4610 port->pp = pp;
4614 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_TSO;
4615 dev->hw_features |= dev->features;
4616 dev->vlan_features |= dev->features;
4617 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
4618 dev->gso_max_segs = MVNETA_MAX_TSO_SEGS;
4620 /* MTU range: 68 - 9676 */
4621 dev->min_mtu = ETH_MIN_MTU;
4622 /* 9676 == 9700 - 20 and rounding to 8 */
4623 dev->max_mtu = 9676;
4625 err = register_netdev(dev);
4626 if (err < 0) {
4627 dev_err(&pdev->dev, "failed to register\n");
4628 goto err_netdev;
4631 netdev_info(dev, "Using %s mac address %pM\n", mac_from,
4632 dev->dev_addr);
4634 platform_set_drvdata(pdev, pp->dev);
4636 return 0;
4638 err_netdev:
4639 if (pp->bm_priv) {
4640 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
4641 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short,
4642 1 << pp->id);
4643 mvneta_bm_put(pp->bm_priv);
4645 free_percpu(pp->stats);
4646 err_free_ports:
4647 free_percpu(pp->ports);
4648 err_clk:
4649 clk_disable_unprepare(pp->clk_bus);
4650 clk_disable_unprepare(pp->clk);
4651 err_free_phylink:
4652 if (pp->phylink)
4653 phylink_destroy(pp->phylink);
4654 err_free_irq:
4655 irq_dispose_mapping(dev->irq);
4656 err_free_netdev:
4657 free_netdev(dev);
4658 return err;
4661 /* Device removal routine */
4662 static int mvneta_remove(struct platform_device *pdev)
4664 struct net_device *dev = platform_get_drvdata(pdev);
4665 struct mvneta_port *pp = netdev_priv(dev);
4667 unregister_netdev(dev);
4668 clk_disable_unprepare(pp->clk_bus);
4669 clk_disable_unprepare(pp->clk);
4670 free_percpu(pp->ports);
4671 free_percpu(pp->stats);
4672 irq_dispose_mapping(dev->irq);
4673 phylink_destroy(pp->phylink);
4674 free_netdev(dev);
4676 if (pp->bm_priv) {
4677 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
4678 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short,
4679 1 << pp->id);
4680 mvneta_bm_put(pp->bm_priv);
4683 return 0;
4686 #ifdef CONFIG_PM_SLEEP
4687 static int mvneta_suspend(struct device *device)
4689 int queue;
4690 struct net_device *dev = dev_get_drvdata(device);
4691 struct mvneta_port *pp = netdev_priv(dev);
4693 if (!netif_running(dev))
4694 goto clean_exit;
4696 if (!pp->neta_armada3700) {
4697 spin_lock(&pp->lock);
4698 pp->is_stopped = true;
4699 spin_unlock(&pp->lock);
4701 cpuhp_state_remove_instance_nocalls(online_hpstate,
4702 &pp->node_online);
4703 cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
4704 &pp->node_dead);
4707 rtnl_lock();
4708 mvneta_stop_dev(pp);
4709 rtnl_unlock();
4711 for (queue = 0; queue < rxq_number; queue++) {
4712 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
4714 mvneta_rxq_drop_pkts(pp, rxq);
4717 for (queue = 0; queue < txq_number; queue++) {
4718 struct mvneta_tx_queue *txq = &pp->txqs[queue];
4720 mvneta_txq_hw_deinit(pp, txq);
4723 clean_exit:
4724 netif_device_detach(dev);
4725 clk_disable_unprepare(pp->clk_bus);
4726 clk_disable_unprepare(pp->clk);
4728 return 0;
4731 static int mvneta_resume(struct device *device)
4733 struct platform_device *pdev = to_platform_device(device);
4734 struct net_device *dev = dev_get_drvdata(device);
4735 struct mvneta_port *pp = netdev_priv(dev);
4736 int err, queue;
4738 clk_prepare_enable(pp->clk);
4739 if (!IS_ERR(pp->clk_bus))
4740 clk_prepare_enable(pp->clk_bus);
4741 if (pp->dram_target_info || pp->neta_armada3700)
4742 mvneta_conf_mbus_windows(pp, pp->dram_target_info);
4743 if (pp->bm_priv) {
4744 err = mvneta_bm_port_init(pdev, pp);
4745 if (err < 0) {
4746 dev_info(&pdev->dev, "use SW buffer management\n");
4747 pp->bm_priv = NULL;
4750 mvneta_defaults_set(pp);
4751 err = mvneta_port_power_up(pp, pp->phy_interface);
4752 if (err < 0) {
4753 dev_err(device, "can't power up port\n");
4754 return err;
4757 netif_device_attach(dev);
4759 if (!netif_running(dev))
4760 return 0;
4762 for (queue = 0; queue < rxq_number; queue++) {
4763 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
4765 rxq->next_desc_to_proc = 0;
4766 mvneta_rxq_hw_init(pp, rxq);
4769 for (queue = 0; queue < txq_number; queue++) {
4770 struct mvneta_tx_queue *txq = &pp->txqs[queue];
4772 txq->next_desc_to_proc = 0;
4773 mvneta_txq_hw_init(pp, txq);
4776 if (!pp->neta_armada3700) {
4777 spin_lock(&pp->lock);
4778 pp->is_stopped = false;
4779 spin_unlock(&pp->lock);
4780 cpuhp_state_add_instance_nocalls(online_hpstate,
4781 &pp->node_online);
4782 cpuhp_state_add_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
4783 &pp->node_dead);
4786 rtnl_lock();
4787 mvneta_start_dev(pp);
4788 rtnl_unlock();
4789 mvneta_set_rx_mode(dev);
4791 return 0;
4793 #endif
4795 static SIMPLE_DEV_PM_OPS(mvneta_pm_ops, mvneta_suspend, mvneta_resume);
4797 static const struct of_device_id mvneta_match[] = {
4798 { .compatible = "marvell,armada-370-neta" },
4799 { .compatible = "marvell,armada-xp-neta" },
4800 { .compatible = "marvell,armada-3700-neta" },
4803 MODULE_DEVICE_TABLE(of, mvneta_match);
4805 static struct platform_driver mvneta_driver = {
4806 .probe = mvneta_probe,
4807 .remove = mvneta_remove,
4808 .driver = {
4809 .name = MVNETA_DRIVER_NAME,
4810 .of_match_table = mvneta_match,
4811 .pm = &mvneta_pm_ops,
4815 static int __init mvneta_driver_init(void)
4817 int ret;
4819 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "net/mvmeta:online",
4820 mvneta_cpu_online,
4821 mvneta_cpu_down_prepare);
4822 if (ret < 0)
4823 goto out;
4824 online_hpstate = ret;
4825 ret = cpuhp_setup_state_multi(CPUHP_NET_MVNETA_DEAD, "net/mvneta:dead",
4826 NULL, mvneta_cpu_dead);
4827 if (ret)
4828 goto err_dead;
4830 ret = platform_driver_register(&mvneta_driver);
4831 if (ret)
4832 goto err;
4833 return 0;
4835 err:
4836 cpuhp_remove_multi_state(CPUHP_NET_MVNETA_DEAD);
4837 err_dead:
4838 cpuhp_remove_multi_state(online_hpstate);
4839 out:
4840 return ret;
4842 module_init(mvneta_driver_init);
4844 static void __exit mvneta_driver_exit(void)
4846 platform_driver_unregister(&mvneta_driver);
4847 cpuhp_remove_multi_state(CPUHP_NET_MVNETA_DEAD);
4848 cpuhp_remove_multi_state(online_hpstate);
4850 module_exit(mvneta_driver_exit);
4852 MODULE_DESCRIPTION("Marvell NETA Ethernet Driver - www.marvell.com");
4853 MODULE_AUTHOR("Rami Rosen <rosenr@marvell.com>, Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
4854 MODULE_LICENSE("GPL");
4856 module_param(rxq_number, int, 0444);
4857 module_param(txq_number, int, 0444);
4859 module_param(rxq_def, int, 0444);
4860 module_param(rx_copybreak, int, 0644);