x86/mm/pat: Don't report PAT on CPUs that don't support it
[linux/fpc-iii.git] / drivers / net / ethernet / marvell / mvneta.c
blob61dd4462411c03511d6121d75ca65dc36c7f688f
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/phy_fixed.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_ENABLE BIT(0)
193 #define MVNETA_GMAC_CTRL_2 0x2c08
194 #define MVNETA_GMAC2_INBAND_AN_ENABLE BIT(0)
195 #define MVNETA_GMAC2_PCS_ENABLE BIT(3)
196 #define MVNETA_GMAC2_PORT_RGMII BIT(4)
197 #define MVNETA_GMAC2_PORT_RESET BIT(6)
198 #define MVNETA_GMAC_STATUS 0x2c10
199 #define MVNETA_GMAC_LINK_UP BIT(0)
200 #define MVNETA_GMAC_SPEED_1000 BIT(1)
201 #define MVNETA_GMAC_SPEED_100 BIT(2)
202 #define MVNETA_GMAC_FULL_DUPLEX BIT(3)
203 #define MVNETA_GMAC_RX_FLOW_CTRL_ENABLE BIT(4)
204 #define MVNETA_GMAC_TX_FLOW_CTRL_ENABLE BIT(5)
205 #define MVNETA_GMAC_RX_FLOW_CTRL_ACTIVE BIT(6)
206 #define MVNETA_GMAC_TX_FLOW_CTRL_ACTIVE BIT(7)
207 #define MVNETA_GMAC_AUTONEG_CONFIG 0x2c0c
208 #define MVNETA_GMAC_FORCE_LINK_DOWN BIT(0)
209 #define MVNETA_GMAC_FORCE_LINK_PASS BIT(1)
210 #define MVNETA_GMAC_INBAND_AN_ENABLE BIT(2)
211 #define MVNETA_GMAC_CONFIG_MII_SPEED BIT(5)
212 #define MVNETA_GMAC_CONFIG_GMII_SPEED BIT(6)
213 #define MVNETA_GMAC_AN_SPEED_EN BIT(7)
214 #define MVNETA_GMAC_AN_FLOW_CTRL_EN BIT(11)
215 #define MVNETA_GMAC_CONFIG_FULL_DUPLEX BIT(12)
216 #define MVNETA_GMAC_AN_DUPLEX_EN BIT(13)
217 #define MVNETA_MIB_COUNTERS_BASE 0x3000
218 #define MVNETA_MIB_LATE_COLLISION 0x7c
219 #define MVNETA_DA_FILT_SPEC_MCAST 0x3400
220 #define MVNETA_DA_FILT_OTH_MCAST 0x3500
221 #define MVNETA_DA_FILT_UCAST_BASE 0x3600
222 #define MVNETA_TXQ_BASE_ADDR_REG(q) (0x3c00 + ((q) << 2))
223 #define MVNETA_TXQ_SIZE_REG(q) (0x3c20 + ((q) << 2))
224 #define MVNETA_TXQ_SENT_THRESH_ALL_MASK 0x3fff0000
225 #define MVNETA_TXQ_SENT_THRESH_MASK(coal) ((coal) << 16)
226 #define MVNETA_TXQ_UPDATE_REG(q) (0x3c60 + ((q) << 2))
227 #define MVNETA_TXQ_DEC_SENT_SHIFT 16
228 #define MVNETA_TXQ_DEC_SENT_MASK 0xff
229 #define MVNETA_TXQ_STATUS_REG(q) (0x3c40 + ((q) << 2))
230 #define MVNETA_TXQ_SENT_DESC_SHIFT 16
231 #define MVNETA_TXQ_SENT_DESC_MASK 0x3fff0000
232 #define MVNETA_PORT_TX_RESET 0x3cf0
233 #define MVNETA_PORT_TX_DMA_RESET BIT(0)
234 #define MVNETA_TX_MTU 0x3e0c
235 #define MVNETA_TX_TOKEN_SIZE 0x3e14
236 #define MVNETA_TX_TOKEN_SIZE_MAX 0xffffffff
237 #define MVNETA_TXQ_TOKEN_SIZE_REG(q) (0x3e40 + ((q) << 2))
238 #define MVNETA_TXQ_TOKEN_SIZE_MAX 0x7fffffff
240 #define MVNETA_CAUSE_TXQ_SENT_DESC_ALL_MASK 0xff
242 /* Descriptor ring Macros */
243 #define MVNETA_QUEUE_NEXT_DESC(q, index) \
244 (((index) < (q)->last_desc) ? ((index) + 1) : 0)
246 /* Various constants */
248 /* Coalescing */
249 #define MVNETA_TXDONE_COAL_PKTS 0 /* interrupt per packet */
250 #define MVNETA_RX_COAL_PKTS 32
251 #define MVNETA_RX_COAL_USEC 100
253 /* The two bytes Marvell header. Either contains a special value used
254 * by Marvell switches when a specific hardware mode is enabled (not
255 * supported by this driver) or is filled automatically by zeroes on
256 * the RX side. Those two bytes being at the front of the Ethernet
257 * header, they allow to have the IP header aligned on a 4 bytes
258 * boundary automatically: the hardware skips those two bytes on its
259 * own.
261 #define MVNETA_MH_SIZE 2
263 #define MVNETA_VLAN_TAG_LEN 4
265 #define MVNETA_TX_CSUM_DEF_SIZE 1600
266 #define MVNETA_TX_CSUM_MAX_SIZE 9800
267 #define MVNETA_ACC_MODE_EXT1 1
268 #define MVNETA_ACC_MODE_EXT2 2
270 #define MVNETA_MAX_DECODE_WIN 6
272 /* Timeout constants */
273 #define MVNETA_TX_DISABLE_TIMEOUT_MSEC 1000
274 #define MVNETA_RX_DISABLE_TIMEOUT_MSEC 1000
275 #define MVNETA_TX_FIFO_EMPTY_TIMEOUT 10000
277 #define MVNETA_TX_MTU_MAX 0x3ffff
279 /* The RSS lookup table actually has 256 entries but we do not use
280 * them yet
282 #define MVNETA_RSS_LU_TABLE_SIZE 1
284 /* TSO header size */
285 #define TSO_HEADER_SIZE 128
287 /* Max number of Rx descriptors */
288 #define MVNETA_MAX_RXD 128
290 /* Max number of Tx descriptors */
291 #define MVNETA_MAX_TXD 532
293 /* Max number of allowed TCP segments for software TSO */
294 #define MVNETA_MAX_TSO_SEGS 100
296 #define MVNETA_MAX_SKB_DESCS (MVNETA_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
298 /* descriptor aligned size */
299 #define MVNETA_DESC_ALIGNED_SIZE 32
301 /* Number of bytes to be taken into account by HW when putting incoming data
302 * to the buffers. It is needed in case NET_SKB_PAD exceeds maximum packet
303 * offset supported in MVNETA_RXQ_CONFIG_REG(q) registers.
305 #define MVNETA_RX_PKT_OFFSET_CORRECTION 64
307 #define MVNETA_RX_PKT_SIZE(mtu) \
308 ALIGN((mtu) + MVNETA_MH_SIZE + MVNETA_VLAN_TAG_LEN + \
309 ETH_HLEN + ETH_FCS_LEN, \
310 cache_line_size())
312 #define IS_TSO_HEADER(txq, addr) \
313 ((addr >= txq->tso_hdrs_phys) && \
314 (addr < txq->tso_hdrs_phys + txq->size * TSO_HEADER_SIZE))
316 #define MVNETA_RX_GET_BM_POOL_ID(rxd) \
317 (((rxd)->status & MVNETA_RXD_BM_POOL_MASK) >> MVNETA_RXD_BM_POOL_SHIFT)
319 struct mvneta_statistic {
320 unsigned short offset;
321 unsigned short type;
322 const char name[ETH_GSTRING_LEN];
325 #define T_REG_32 32
326 #define T_REG_64 64
328 static const struct mvneta_statistic mvneta_statistics[] = {
329 { 0x3000, T_REG_64, "good_octets_received", },
330 { 0x3010, T_REG_32, "good_frames_received", },
331 { 0x3008, T_REG_32, "bad_octets_received", },
332 { 0x3014, T_REG_32, "bad_frames_received", },
333 { 0x3018, T_REG_32, "broadcast_frames_received", },
334 { 0x301c, T_REG_32, "multicast_frames_received", },
335 { 0x3050, T_REG_32, "unrec_mac_control_received", },
336 { 0x3058, T_REG_32, "good_fc_received", },
337 { 0x305c, T_REG_32, "bad_fc_received", },
338 { 0x3060, T_REG_32, "undersize_received", },
339 { 0x3064, T_REG_32, "fragments_received", },
340 { 0x3068, T_REG_32, "oversize_received", },
341 { 0x306c, T_REG_32, "jabber_received", },
342 { 0x3070, T_REG_32, "mac_receive_error", },
343 { 0x3074, T_REG_32, "bad_crc_event", },
344 { 0x3078, T_REG_32, "collision", },
345 { 0x307c, T_REG_32, "late_collision", },
346 { 0x2484, T_REG_32, "rx_discard", },
347 { 0x2488, T_REG_32, "rx_overrun", },
348 { 0x3020, T_REG_32, "frames_64_octets", },
349 { 0x3024, T_REG_32, "frames_65_to_127_octets", },
350 { 0x3028, T_REG_32, "frames_128_to_255_octets", },
351 { 0x302c, T_REG_32, "frames_256_to_511_octets", },
352 { 0x3030, T_REG_32, "frames_512_to_1023_octets", },
353 { 0x3034, T_REG_32, "frames_1024_to_max_octets", },
354 { 0x3038, T_REG_64, "good_octets_sent", },
355 { 0x3040, T_REG_32, "good_frames_sent", },
356 { 0x3044, T_REG_32, "excessive_collision", },
357 { 0x3048, T_REG_32, "multicast_frames_sent", },
358 { 0x304c, T_REG_32, "broadcast_frames_sent", },
359 { 0x3054, T_REG_32, "fc_sent", },
360 { 0x300c, T_REG_32, "internal_mac_transmit_err", },
363 struct mvneta_pcpu_stats {
364 struct u64_stats_sync syncp;
365 u64 rx_packets;
366 u64 rx_bytes;
367 u64 tx_packets;
368 u64 tx_bytes;
371 struct mvneta_pcpu_port {
372 /* Pointer to the shared port */
373 struct mvneta_port *pp;
375 /* Pointer to the CPU-local NAPI struct */
376 struct napi_struct napi;
378 /* Cause of the previous interrupt */
379 u32 cause_rx_tx;
382 struct mvneta_port {
383 u8 id;
384 struct mvneta_pcpu_port __percpu *ports;
385 struct mvneta_pcpu_stats __percpu *stats;
387 int pkt_size;
388 unsigned int frag_size;
389 void __iomem *base;
390 struct mvneta_rx_queue *rxqs;
391 struct mvneta_tx_queue *txqs;
392 struct net_device *dev;
393 struct hlist_node node_online;
394 struct hlist_node node_dead;
395 int rxq_def;
396 /* Protect the access to the percpu interrupt registers,
397 * ensuring that the configuration remains coherent.
399 spinlock_t lock;
400 bool is_stopped;
402 u32 cause_rx_tx;
403 struct napi_struct napi;
405 /* Core clock */
406 struct clk *clk;
407 /* AXI clock */
408 struct clk *clk_bus;
409 u8 mcast_count[256];
410 u16 tx_ring_size;
411 u16 rx_ring_size;
413 struct mii_bus *mii_bus;
414 phy_interface_t phy_interface;
415 struct device_node *phy_node;
416 unsigned int link;
417 unsigned int duplex;
418 unsigned int speed;
419 unsigned int tx_csum_limit;
420 unsigned int use_inband_status:1;
422 struct mvneta_bm *bm_priv;
423 struct mvneta_bm_pool *pool_long;
424 struct mvneta_bm_pool *pool_short;
425 int bm_win_id;
427 u64 ethtool_stats[ARRAY_SIZE(mvneta_statistics)];
429 u32 indir[MVNETA_RSS_LU_TABLE_SIZE];
431 /* Flags for special SoC configurations */
432 bool neta_armada3700;
433 u16 rx_offset_correction;
436 /* The mvneta_tx_desc and mvneta_rx_desc structures describe the
437 * layout of the transmit and reception DMA descriptors, and their
438 * layout is therefore defined by the hardware design
441 #define MVNETA_TX_L3_OFF_SHIFT 0
442 #define MVNETA_TX_IP_HLEN_SHIFT 8
443 #define MVNETA_TX_L4_UDP BIT(16)
444 #define MVNETA_TX_L3_IP6 BIT(17)
445 #define MVNETA_TXD_IP_CSUM BIT(18)
446 #define MVNETA_TXD_Z_PAD BIT(19)
447 #define MVNETA_TXD_L_DESC BIT(20)
448 #define MVNETA_TXD_F_DESC BIT(21)
449 #define MVNETA_TXD_FLZ_DESC (MVNETA_TXD_Z_PAD | \
450 MVNETA_TXD_L_DESC | \
451 MVNETA_TXD_F_DESC)
452 #define MVNETA_TX_L4_CSUM_FULL BIT(30)
453 #define MVNETA_TX_L4_CSUM_NOT BIT(31)
455 #define MVNETA_RXD_ERR_CRC 0x0
456 #define MVNETA_RXD_BM_POOL_SHIFT 13
457 #define MVNETA_RXD_BM_POOL_MASK (BIT(13) | BIT(14))
458 #define MVNETA_RXD_ERR_SUMMARY BIT(16)
459 #define MVNETA_RXD_ERR_OVERRUN BIT(17)
460 #define MVNETA_RXD_ERR_LEN BIT(18)
461 #define MVNETA_RXD_ERR_RESOURCE (BIT(17) | BIT(18))
462 #define MVNETA_RXD_ERR_CODE_MASK (BIT(17) | BIT(18))
463 #define MVNETA_RXD_L3_IP4 BIT(25)
464 #define MVNETA_RXD_FIRST_LAST_DESC (BIT(26) | BIT(27))
465 #define MVNETA_RXD_L4_CSUM_OK BIT(30)
467 #if defined(__LITTLE_ENDIAN)
468 struct mvneta_tx_desc {
469 u32 command; /* Options used by HW for packet transmitting.*/
470 u16 reserverd1; /* csum_l4 (for future use) */
471 u16 data_size; /* Data size of transmitted packet in bytes */
472 u32 buf_phys_addr; /* Physical addr of transmitted buffer */
473 u32 reserved2; /* hw_cmd - (for future use, PMT) */
474 u32 reserved3[4]; /* Reserved - (for future use) */
477 struct mvneta_rx_desc {
478 u32 status; /* Info about received packet */
479 u16 reserved1; /* pnc_info - (for future use, PnC) */
480 u16 data_size; /* Size of received packet in bytes */
482 u32 buf_phys_addr; /* Physical address of the buffer */
483 u32 reserved2; /* pnc_flow_id (for future use, PnC) */
485 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
486 u16 reserved3; /* prefetch_cmd, for future use */
487 u16 reserved4; /* csum_l4 - (for future use, PnC) */
489 u32 reserved5; /* pnc_extra PnC (for future use, PnC) */
490 u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */
492 #else
493 struct mvneta_tx_desc {
494 u16 data_size; /* Data size of transmitted packet in bytes */
495 u16 reserverd1; /* csum_l4 (for future use) */
496 u32 command; /* Options used by HW for packet transmitting.*/
497 u32 reserved2; /* hw_cmd - (for future use, PMT) */
498 u32 buf_phys_addr; /* Physical addr of transmitted buffer */
499 u32 reserved3[4]; /* Reserved - (for future use) */
502 struct mvneta_rx_desc {
503 u16 data_size; /* Size of received packet in bytes */
504 u16 reserved1; /* pnc_info - (for future use, PnC) */
505 u32 status; /* Info about received packet */
507 u32 reserved2; /* pnc_flow_id (for future use, PnC) */
508 u32 buf_phys_addr; /* Physical address of the buffer */
510 u16 reserved4; /* csum_l4 - (for future use, PnC) */
511 u16 reserved3; /* prefetch_cmd, for future use */
512 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
514 u32 reserved5; /* pnc_extra PnC (for future use, PnC) */
515 u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */
517 #endif
519 struct mvneta_tx_queue {
520 /* Number of this TX queue, in the range 0-7 */
521 u8 id;
523 /* Number of TX DMA descriptors in the descriptor ring */
524 int size;
526 /* Number of currently used TX DMA descriptor in the
527 * descriptor ring
529 int count;
530 int pending;
531 int tx_stop_threshold;
532 int tx_wake_threshold;
534 /* Array of transmitted skb */
535 struct sk_buff **tx_skb;
537 /* Index of last TX DMA descriptor that was inserted */
538 int txq_put_index;
540 /* Index of the TX DMA descriptor to be cleaned up */
541 int txq_get_index;
543 u32 done_pkts_coal;
545 /* Virtual address of the TX DMA descriptors array */
546 struct mvneta_tx_desc *descs;
548 /* DMA address of the TX DMA descriptors array */
549 dma_addr_t descs_phys;
551 /* Index of the last TX DMA descriptor */
552 int last_desc;
554 /* Index of the next TX DMA descriptor to process */
555 int next_desc_to_proc;
557 /* DMA buffers for TSO headers */
558 char *tso_hdrs;
560 /* DMA address of TSO headers */
561 dma_addr_t tso_hdrs_phys;
563 /* Affinity mask for CPUs*/
564 cpumask_t affinity_mask;
567 struct mvneta_rx_queue {
568 /* rx queue number, in the range 0-7 */
569 u8 id;
571 /* num of rx descriptors in the rx descriptor ring */
572 int size;
574 /* counter of times when mvneta_refill() failed */
575 int missed;
577 u32 pkts_coal;
578 u32 time_coal;
580 /* Virtual address of the RX buffer */
581 void **buf_virt_addr;
583 /* Virtual address of the RX DMA descriptors array */
584 struct mvneta_rx_desc *descs;
586 /* DMA address of the RX DMA descriptors array */
587 dma_addr_t descs_phys;
589 /* Index of the last RX DMA descriptor */
590 int last_desc;
592 /* Index of the next RX DMA descriptor to process */
593 int next_desc_to_proc;
596 static enum cpuhp_state online_hpstate;
597 /* The hardware supports eight (8) rx queues, but we are only allowing
598 * the first one to be used. Therefore, let's just allocate one queue.
600 static int rxq_number = 8;
601 static int txq_number = 8;
603 static int rxq_def;
605 static int rx_copybreak __read_mostly = 256;
607 /* HW BM need that each port be identify by a unique ID */
608 static int global_port_id;
610 #define MVNETA_DRIVER_NAME "mvneta"
611 #define MVNETA_DRIVER_VERSION "1.0"
613 /* Utility/helper methods */
615 /* Write helper method */
616 static void mvreg_write(struct mvneta_port *pp, u32 offset, u32 data)
618 writel(data, pp->base + offset);
621 /* Read helper method */
622 static u32 mvreg_read(struct mvneta_port *pp, u32 offset)
624 return readl(pp->base + offset);
627 /* Increment txq get counter */
628 static void mvneta_txq_inc_get(struct mvneta_tx_queue *txq)
630 txq->txq_get_index++;
631 if (txq->txq_get_index == txq->size)
632 txq->txq_get_index = 0;
635 /* Increment txq put counter */
636 static void mvneta_txq_inc_put(struct mvneta_tx_queue *txq)
638 txq->txq_put_index++;
639 if (txq->txq_put_index == txq->size)
640 txq->txq_put_index = 0;
644 /* Clear all MIB counters */
645 static void mvneta_mib_counters_clear(struct mvneta_port *pp)
647 int i;
648 u32 dummy;
650 /* Perform dummy reads from MIB counters */
651 for (i = 0; i < MVNETA_MIB_LATE_COLLISION; i += 4)
652 dummy = mvreg_read(pp, (MVNETA_MIB_COUNTERS_BASE + i));
653 dummy = mvreg_read(pp, MVNETA_RX_DISCARD_FRAME_COUNT);
654 dummy = mvreg_read(pp, MVNETA_OVERRUN_FRAME_COUNT);
657 /* Get System Network Statistics */
658 static void
659 mvneta_get_stats64(struct net_device *dev,
660 struct rtnl_link_stats64 *stats)
662 struct mvneta_port *pp = netdev_priv(dev);
663 unsigned int start;
664 int cpu;
666 for_each_possible_cpu(cpu) {
667 struct mvneta_pcpu_stats *cpu_stats;
668 u64 rx_packets;
669 u64 rx_bytes;
670 u64 tx_packets;
671 u64 tx_bytes;
673 cpu_stats = per_cpu_ptr(pp->stats, cpu);
674 do {
675 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
676 rx_packets = cpu_stats->rx_packets;
677 rx_bytes = cpu_stats->rx_bytes;
678 tx_packets = cpu_stats->tx_packets;
679 tx_bytes = cpu_stats->tx_bytes;
680 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
682 stats->rx_packets += rx_packets;
683 stats->rx_bytes += rx_bytes;
684 stats->tx_packets += tx_packets;
685 stats->tx_bytes += tx_bytes;
688 stats->rx_errors = dev->stats.rx_errors;
689 stats->rx_dropped = dev->stats.rx_dropped;
691 stats->tx_dropped = dev->stats.tx_dropped;
694 /* Rx descriptors helper methods */
696 /* Checks whether the RX descriptor having this status is both the first
697 * and the last descriptor for the RX packet. Each RX packet is currently
698 * received through a single RX descriptor, so not having each RX
699 * descriptor with its first and last bits set is an error
701 static int mvneta_rxq_desc_is_first_last(u32 status)
703 return (status & MVNETA_RXD_FIRST_LAST_DESC) ==
704 MVNETA_RXD_FIRST_LAST_DESC;
707 /* Add number of descriptors ready to receive new packets */
708 static void mvneta_rxq_non_occup_desc_add(struct mvneta_port *pp,
709 struct mvneta_rx_queue *rxq,
710 int ndescs)
712 /* Only MVNETA_RXQ_ADD_NON_OCCUPIED_MAX (255) descriptors can
713 * be added at once
715 while (ndescs > MVNETA_RXQ_ADD_NON_OCCUPIED_MAX) {
716 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
717 (MVNETA_RXQ_ADD_NON_OCCUPIED_MAX <<
718 MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
719 ndescs -= MVNETA_RXQ_ADD_NON_OCCUPIED_MAX;
722 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
723 (ndescs << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
726 /* Get number of RX descriptors occupied by received packets */
727 static int mvneta_rxq_busy_desc_num_get(struct mvneta_port *pp,
728 struct mvneta_rx_queue *rxq)
730 u32 val;
732 val = mvreg_read(pp, MVNETA_RXQ_STATUS_REG(rxq->id));
733 return val & MVNETA_RXQ_OCCUPIED_ALL_MASK;
736 /* Update num of rx desc called upon return from rx path or
737 * from mvneta_rxq_drop_pkts().
739 static void mvneta_rxq_desc_num_update(struct mvneta_port *pp,
740 struct mvneta_rx_queue *rxq,
741 int rx_done, int rx_filled)
743 u32 val;
745 if ((rx_done <= 0xff) && (rx_filled <= 0xff)) {
746 val = rx_done |
747 (rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT);
748 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
749 return;
752 /* Only 255 descriptors can be added at once */
753 while ((rx_done > 0) || (rx_filled > 0)) {
754 if (rx_done <= 0xff) {
755 val = rx_done;
756 rx_done = 0;
757 } else {
758 val = 0xff;
759 rx_done -= 0xff;
761 if (rx_filled <= 0xff) {
762 val |= rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
763 rx_filled = 0;
764 } else {
765 val |= 0xff << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
766 rx_filled -= 0xff;
768 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
772 /* Get pointer to next RX descriptor to be processed by SW */
773 static struct mvneta_rx_desc *
774 mvneta_rxq_next_desc_get(struct mvneta_rx_queue *rxq)
776 int rx_desc = rxq->next_desc_to_proc;
778 rxq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(rxq, rx_desc);
779 prefetch(rxq->descs + rxq->next_desc_to_proc);
780 return rxq->descs + rx_desc;
783 /* Change maximum receive size of the port. */
784 static void mvneta_max_rx_size_set(struct mvneta_port *pp, int max_rx_size)
786 u32 val;
788 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
789 val &= ~MVNETA_GMAC_MAX_RX_SIZE_MASK;
790 val |= ((max_rx_size - MVNETA_MH_SIZE) / 2) <<
791 MVNETA_GMAC_MAX_RX_SIZE_SHIFT;
792 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
796 /* Set rx queue offset */
797 static void mvneta_rxq_offset_set(struct mvneta_port *pp,
798 struct mvneta_rx_queue *rxq,
799 int offset)
801 u32 val;
803 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
804 val &= ~MVNETA_RXQ_PKT_OFFSET_ALL_MASK;
806 /* Offset is in */
807 val |= MVNETA_RXQ_PKT_OFFSET_MASK(offset >> 3);
808 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
812 /* Tx descriptors helper methods */
814 /* Update HW with number of TX descriptors to be sent */
815 static void mvneta_txq_pend_desc_add(struct mvneta_port *pp,
816 struct mvneta_tx_queue *txq,
817 int pend_desc)
819 u32 val;
821 /* Only 255 descriptors can be added at once ; Assume caller
822 * process TX desriptors in quanta less than 256
824 val = pend_desc + txq->pending;
825 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
826 txq->pending = 0;
829 /* Get pointer to next TX descriptor to be processed (send) by HW */
830 static struct mvneta_tx_desc *
831 mvneta_txq_next_desc_get(struct mvneta_tx_queue *txq)
833 int tx_desc = txq->next_desc_to_proc;
835 txq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(txq, tx_desc);
836 return txq->descs + tx_desc;
839 /* Release the last allocated TX descriptor. Useful to handle DMA
840 * mapping failures in the TX path.
842 static void mvneta_txq_desc_put(struct mvneta_tx_queue *txq)
844 if (txq->next_desc_to_proc == 0)
845 txq->next_desc_to_proc = txq->last_desc - 1;
846 else
847 txq->next_desc_to_proc--;
850 /* Set rxq buf size */
851 static void mvneta_rxq_buf_size_set(struct mvneta_port *pp,
852 struct mvneta_rx_queue *rxq,
853 int buf_size)
855 u32 val;
857 val = mvreg_read(pp, MVNETA_RXQ_SIZE_REG(rxq->id));
859 val &= ~MVNETA_RXQ_BUF_SIZE_MASK;
860 val |= ((buf_size >> 3) << MVNETA_RXQ_BUF_SIZE_SHIFT);
862 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), val);
865 /* Disable buffer management (BM) */
866 static void mvneta_rxq_bm_disable(struct mvneta_port *pp,
867 struct mvneta_rx_queue *rxq)
869 u32 val;
871 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
872 val &= ~MVNETA_RXQ_HW_BUF_ALLOC;
873 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
876 /* Enable buffer management (BM) */
877 static void mvneta_rxq_bm_enable(struct mvneta_port *pp,
878 struct mvneta_rx_queue *rxq)
880 u32 val;
882 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
883 val |= MVNETA_RXQ_HW_BUF_ALLOC;
884 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
887 /* Notify HW about port's assignment of pool for bigger packets */
888 static void mvneta_rxq_long_pool_set(struct mvneta_port *pp,
889 struct mvneta_rx_queue *rxq)
891 u32 val;
893 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
894 val &= ~MVNETA_RXQ_LONG_POOL_ID_MASK;
895 val |= (pp->pool_long->id << MVNETA_RXQ_LONG_POOL_ID_SHIFT);
897 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
900 /* Notify HW about port's assignment of pool for smaller packets */
901 static void mvneta_rxq_short_pool_set(struct mvneta_port *pp,
902 struct mvneta_rx_queue *rxq)
904 u32 val;
906 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
907 val &= ~MVNETA_RXQ_SHORT_POOL_ID_MASK;
908 val |= (pp->pool_short->id << MVNETA_RXQ_SHORT_POOL_ID_SHIFT);
910 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
913 /* Set port's receive buffer size for assigned BM pool */
914 static inline void mvneta_bm_pool_bufsize_set(struct mvneta_port *pp,
915 int buf_size,
916 u8 pool_id)
918 u32 val;
920 if (!IS_ALIGNED(buf_size, 8)) {
921 dev_warn(pp->dev->dev.parent,
922 "illegal buf_size value %d, round to %d\n",
923 buf_size, ALIGN(buf_size, 8));
924 buf_size = ALIGN(buf_size, 8);
927 val = mvreg_read(pp, MVNETA_PORT_POOL_BUFFER_SZ_REG(pool_id));
928 val |= buf_size & MVNETA_PORT_POOL_BUFFER_SZ_MASK;
929 mvreg_write(pp, MVNETA_PORT_POOL_BUFFER_SZ_REG(pool_id), val);
932 /* Configure MBUS window in order to enable access BM internal SRAM */
933 static int mvneta_mbus_io_win_set(struct mvneta_port *pp, u32 base, u32 wsize,
934 u8 target, u8 attr)
936 u32 win_enable, win_protect;
937 int i;
939 win_enable = mvreg_read(pp, MVNETA_BASE_ADDR_ENABLE);
941 if (pp->bm_win_id < 0) {
942 /* Find first not occupied window */
943 for (i = 0; i < MVNETA_MAX_DECODE_WIN; i++) {
944 if (win_enable & (1 << i)) {
945 pp->bm_win_id = i;
946 break;
949 if (i == MVNETA_MAX_DECODE_WIN)
950 return -ENOMEM;
951 } else {
952 i = pp->bm_win_id;
955 mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
956 mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
958 if (i < 4)
959 mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
961 mvreg_write(pp, MVNETA_WIN_BASE(i), (base & 0xffff0000) |
962 (attr << 8) | target);
964 mvreg_write(pp, MVNETA_WIN_SIZE(i), (wsize - 1) & 0xffff0000);
966 win_protect = mvreg_read(pp, MVNETA_ACCESS_PROTECT_ENABLE);
967 win_protect |= 3 << (2 * i);
968 mvreg_write(pp, MVNETA_ACCESS_PROTECT_ENABLE, win_protect);
970 win_enable &= ~(1 << i);
971 mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
973 return 0;
976 static int mvneta_bm_port_mbus_init(struct mvneta_port *pp)
978 u32 wsize;
979 u8 target, attr;
980 int err;
982 /* Get BM window information */
983 err = mvebu_mbus_get_io_win_info(pp->bm_priv->bppi_phys_addr, &wsize,
984 &target, &attr);
985 if (err < 0)
986 return err;
988 pp->bm_win_id = -1;
990 /* Open NETA -> BM window */
991 err = mvneta_mbus_io_win_set(pp, pp->bm_priv->bppi_phys_addr, wsize,
992 target, attr);
993 if (err < 0) {
994 netdev_info(pp->dev, "fail to configure mbus window to BM\n");
995 return err;
997 return 0;
1000 /* Assign and initialize pools for port. In case of fail
1001 * buffer manager will remain disabled for current port.
1003 static int mvneta_bm_port_init(struct platform_device *pdev,
1004 struct mvneta_port *pp)
1006 struct device_node *dn = pdev->dev.of_node;
1007 u32 long_pool_id, short_pool_id;
1009 if (!pp->neta_armada3700) {
1010 int ret;
1012 ret = mvneta_bm_port_mbus_init(pp);
1013 if (ret)
1014 return ret;
1017 if (of_property_read_u32(dn, "bm,pool-long", &long_pool_id)) {
1018 netdev_info(pp->dev, "missing long pool id\n");
1019 return -EINVAL;
1022 /* Create port's long pool depending on mtu */
1023 pp->pool_long = mvneta_bm_pool_use(pp->bm_priv, long_pool_id,
1024 MVNETA_BM_LONG, pp->id,
1025 MVNETA_RX_PKT_SIZE(pp->dev->mtu));
1026 if (!pp->pool_long) {
1027 netdev_info(pp->dev, "fail to obtain long pool for port\n");
1028 return -ENOMEM;
1031 pp->pool_long->port_map |= 1 << pp->id;
1033 mvneta_bm_pool_bufsize_set(pp, pp->pool_long->buf_size,
1034 pp->pool_long->id);
1036 /* If short pool id is not defined, assume using single pool */
1037 if (of_property_read_u32(dn, "bm,pool-short", &short_pool_id))
1038 short_pool_id = long_pool_id;
1040 /* Create port's short pool */
1041 pp->pool_short = mvneta_bm_pool_use(pp->bm_priv, short_pool_id,
1042 MVNETA_BM_SHORT, pp->id,
1043 MVNETA_BM_SHORT_PKT_SIZE);
1044 if (!pp->pool_short) {
1045 netdev_info(pp->dev, "fail to obtain short pool for port\n");
1046 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
1047 return -ENOMEM;
1050 if (short_pool_id != long_pool_id) {
1051 pp->pool_short->port_map |= 1 << pp->id;
1052 mvneta_bm_pool_bufsize_set(pp, pp->pool_short->buf_size,
1053 pp->pool_short->id);
1056 return 0;
1059 /* Update settings of a pool for bigger packets */
1060 static void mvneta_bm_update_mtu(struct mvneta_port *pp, int mtu)
1062 struct mvneta_bm_pool *bm_pool = pp->pool_long;
1063 struct hwbm_pool *hwbm_pool = &bm_pool->hwbm_pool;
1064 int num;
1066 /* Release all buffers from long pool */
1067 mvneta_bm_bufs_free(pp->bm_priv, bm_pool, 1 << pp->id);
1068 if (hwbm_pool->buf_num) {
1069 WARN(1, "cannot free all buffers in pool %d\n",
1070 bm_pool->id);
1071 goto bm_mtu_err;
1074 bm_pool->pkt_size = MVNETA_RX_PKT_SIZE(mtu);
1075 bm_pool->buf_size = MVNETA_RX_BUF_SIZE(bm_pool->pkt_size);
1076 hwbm_pool->frag_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
1077 SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(bm_pool->pkt_size));
1079 /* Fill entire long pool */
1080 num = hwbm_pool_add(hwbm_pool, hwbm_pool->size, GFP_ATOMIC);
1081 if (num != hwbm_pool->size) {
1082 WARN(1, "pool %d: %d of %d allocated\n",
1083 bm_pool->id, num, hwbm_pool->size);
1084 goto bm_mtu_err;
1086 mvneta_bm_pool_bufsize_set(pp, bm_pool->buf_size, bm_pool->id);
1088 return;
1090 bm_mtu_err:
1091 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
1092 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short, 1 << pp->id);
1094 pp->bm_priv = NULL;
1095 mvreg_write(pp, MVNETA_ACC_MODE, MVNETA_ACC_MODE_EXT1);
1096 netdev_info(pp->dev, "fail to update MTU, fall back to software BM\n");
1099 /* Start the Ethernet port RX and TX activity */
1100 static void mvneta_port_up(struct mvneta_port *pp)
1102 int queue;
1103 u32 q_map;
1105 /* Enable all initialized TXs. */
1106 q_map = 0;
1107 for (queue = 0; queue < txq_number; queue++) {
1108 struct mvneta_tx_queue *txq = &pp->txqs[queue];
1109 if (txq->descs != NULL)
1110 q_map |= (1 << queue);
1112 mvreg_write(pp, MVNETA_TXQ_CMD, q_map);
1114 /* Enable all initialized RXQs. */
1115 for (queue = 0; queue < rxq_number; queue++) {
1116 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
1118 if (rxq->descs != NULL)
1119 q_map |= (1 << queue);
1121 mvreg_write(pp, MVNETA_RXQ_CMD, q_map);
1124 /* Stop the Ethernet port activity */
1125 static void mvneta_port_down(struct mvneta_port *pp)
1127 u32 val;
1128 int count;
1130 /* Stop Rx port activity. Check port Rx activity. */
1131 val = mvreg_read(pp, MVNETA_RXQ_CMD) & MVNETA_RXQ_ENABLE_MASK;
1133 /* Issue stop command for active channels only */
1134 if (val != 0)
1135 mvreg_write(pp, MVNETA_RXQ_CMD,
1136 val << MVNETA_RXQ_DISABLE_SHIFT);
1138 /* Wait for all Rx activity to terminate. */
1139 count = 0;
1140 do {
1141 if (count++ >= MVNETA_RX_DISABLE_TIMEOUT_MSEC) {
1142 netdev_warn(pp->dev,
1143 "TIMEOUT for RX stopped ! rx_queue_cmd: 0x%08x\n",
1144 val);
1145 break;
1147 mdelay(1);
1149 val = mvreg_read(pp, MVNETA_RXQ_CMD);
1150 } while (val & MVNETA_RXQ_ENABLE_MASK);
1152 /* Stop Tx port activity. Check port Tx activity. Issue stop
1153 * command for active channels only
1155 val = (mvreg_read(pp, MVNETA_TXQ_CMD)) & MVNETA_TXQ_ENABLE_MASK;
1157 if (val != 0)
1158 mvreg_write(pp, MVNETA_TXQ_CMD,
1159 (val << MVNETA_TXQ_DISABLE_SHIFT));
1161 /* Wait for all Tx activity to terminate. */
1162 count = 0;
1163 do {
1164 if (count++ >= MVNETA_TX_DISABLE_TIMEOUT_MSEC) {
1165 netdev_warn(pp->dev,
1166 "TIMEOUT for TX stopped status=0x%08x\n",
1167 val);
1168 break;
1170 mdelay(1);
1172 /* Check TX Command reg that all Txqs are stopped */
1173 val = mvreg_read(pp, MVNETA_TXQ_CMD);
1175 } while (val & MVNETA_TXQ_ENABLE_MASK);
1177 /* Double check to verify that TX FIFO is empty */
1178 count = 0;
1179 do {
1180 if (count++ >= MVNETA_TX_FIFO_EMPTY_TIMEOUT) {
1181 netdev_warn(pp->dev,
1182 "TX FIFO empty timeout status=0x%08x\n",
1183 val);
1184 break;
1186 mdelay(1);
1188 val = mvreg_read(pp, MVNETA_PORT_STATUS);
1189 } while (!(val & MVNETA_TX_FIFO_EMPTY) &&
1190 (val & MVNETA_TX_IN_PRGRS));
1192 udelay(200);
1195 /* Enable the port by setting the port enable bit of the MAC control register */
1196 static void mvneta_port_enable(struct mvneta_port *pp)
1198 u32 val;
1200 /* Enable port */
1201 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
1202 val |= MVNETA_GMAC0_PORT_ENABLE;
1203 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
1206 /* Disable the port and wait for about 200 usec before retuning */
1207 static void mvneta_port_disable(struct mvneta_port *pp)
1209 u32 val;
1211 /* Reset the Enable bit in the Serial Control Register */
1212 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
1213 val &= ~MVNETA_GMAC0_PORT_ENABLE;
1214 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
1216 udelay(200);
1219 /* Multicast tables methods */
1221 /* Set all entries in Unicast MAC Table; queue==-1 means reject all */
1222 static void mvneta_set_ucast_table(struct mvneta_port *pp, int queue)
1224 int offset;
1225 u32 val;
1227 if (queue == -1) {
1228 val = 0;
1229 } else {
1230 val = 0x1 | (queue << 1);
1231 val |= (val << 24) | (val << 16) | (val << 8);
1234 for (offset = 0; offset <= 0xc; offset += 4)
1235 mvreg_write(pp, MVNETA_DA_FILT_UCAST_BASE + offset, val);
1238 /* Set all entries in Special Multicast MAC Table; queue==-1 means reject all */
1239 static void mvneta_set_special_mcast_table(struct mvneta_port *pp, int queue)
1241 int offset;
1242 u32 val;
1244 if (queue == -1) {
1245 val = 0;
1246 } else {
1247 val = 0x1 | (queue << 1);
1248 val |= (val << 24) | (val << 16) | (val << 8);
1251 for (offset = 0; offset <= 0xfc; offset += 4)
1252 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + offset, val);
1256 /* Set all entries in Other Multicast MAC Table. queue==-1 means reject all */
1257 static void mvneta_set_other_mcast_table(struct mvneta_port *pp, int queue)
1259 int offset;
1260 u32 val;
1262 if (queue == -1) {
1263 memset(pp->mcast_count, 0, sizeof(pp->mcast_count));
1264 val = 0;
1265 } else {
1266 memset(pp->mcast_count, 1, sizeof(pp->mcast_count));
1267 val = 0x1 | (queue << 1);
1268 val |= (val << 24) | (val << 16) | (val << 8);
1271 for (offset = 0; offset <= 0xfc; offset += 4)
1272 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + offset, val);
1275 static void mvneta_set_autoneg(struct mvneta_port *pp, int enable)
1277 u32 val;
1279 if (enable) {
1280 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
1281 val &= ~(MVNETA_GMAC_FORCE_LINK_PASS |
1282 MVNETA_GMAC_FORCE_LINK_DOWN |
1283 MVNETA_GMAC_AN_FLOW_CTRL_EN);
1284 val |= MVNETA_GMAC_INBAND_AN_ENABLE |
1285 MVNETA_GMAC_AN_SPEED_EN |
1286 MVNETA_GMAC_AN_DUPLEX_EN;
1287 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
1289 val = mvreg_read(pp, MVNETA_GMAC_CLOCK_DIVIDER);
1290 val |= MVNETA_GMAC_1MS_CLOCK_ENABLE;
1291 mvreg_write(pp, MVNETA_GMAC_CLOCK_DIVIDER, val);
1293 val = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
1294 val |= MVNETA_GMAC2_INBAND_AN_ENABLE;
1295 mvreg_write(pp, MVNETA_GMAC_CTRL_2, val);
1296 } else {
1297 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
1298 val &= ~(MVNETA_GMAC_INBAND_AN_ENABLE |
1299 MVNETA_GMAC_AN_SPEED_EN |
1300 MVNETA_GMAC_AN_DUPLEX_EN);
1301 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
1303 val = mvreg_read(pp, MVNETA_GMAC_CLOCK_DIVIDER);
1304 val &= ~MVNETA_GMAC_1MS_CLOCK_ENABLE;
1305 mvreg_write(pp, MVNETA_GMAC_CLOCK_DIVIDER, val);
1307 val = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
1308 val &= ~MVNETA_GMAC2_INBAND_AN_ENABLE;
1309 mvreg_write(pp, MVNETA_GMAC_CTRL_2, val);
1313 static void mvneta_percpu_unmask_interrupt(void *arg)
1315 struct mvneta_port *pp = arg;
1317 /* All the queue are unmasked, but actually only the ones
1318 * mapped to this CPU will be unmasked
1320 mvreg_write(pp, MVNETA_INTR_NEW_MASK,
1321 MVNETA_RX_INTR_MASK_ALL |
1322 MVNETA_TX_INTR_MASK_ALL |
1323 MVNETA_MISCINTR_INTR_MASK);
1326 static void mvneta_percpu_mask_interrupt(void *arg)
1328 struct mvneta_port *pp = arg;
1330 /* All the queue are masked, but actually only the ones
1331 * mapped to this CPU will be masked
1333 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
1334 mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0);
1335 mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0);
1338 static void mvneta_percpu_clear_intr_cause(void *arg)
1340 struct mvneta_port *pp = arg;
1342 /* All the queue are cleared, but actually only the ones
1343 * mapped to this CPU will be cleared
1345 mvreg_write(pp, MVNETA_INTR_NEW_CAUSE, 0);
1346 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
1347 mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0);
1350 /* This method sets defaults to the NETA port:
1351 * Clears interrupt Cause and Mask registers.
1352 * Clears all MAC tables.
1353 * Sets defaults to all registers.
1354 * Resets RX and TX descriptor rings.
1355 * Resets PHY.
1356 * This method can be called after mvneta_port_down() to return the port
1357 * settings to defaults.
1359 static void mvneta_defaults_set(struct mvneta_port *pp)
1361 int cpu;
1362 int queue;
1363 u32 val;
1364 int max_cpu = num_present_cpus();
1366 /* Clear all Cause registers */
1367 on_each_cpu(mvneta_percpu_clear_intr_cause, pp, true);
1369 /* Mask all interrupts */
1370 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
1371 mvreg_write(pp, MVNETA_INTR_ENABLE, 0);
1373 /* Enable MBUS Retry bit16 */
1374 mvreg_write(pp, MVNETA_MBUS_RETRY, 0x20);
1376 /* Set CPU queue access map. CPUs are assigned to the RX and
1377 * TX queues modulo their number. If there is only one TX
1378 * queue then it is assigned to the CPU associated to the
1379 * default RX queue.
1381 for_each_present_cpu(cpu) {
1382 int rxq_map = 0, txq_map = 0;
1383 int rxq, txq;
1384 if (!pp->neta_armada3700) {
1385 for (rxq = 0; rxq < rxq_number; rxq++)
1386 if ((rxq % max_cpu) == cpu)
1387 rxq_map |= MVNETA_CPU_RXQ_ACCESS(rxq);
1389 for (txq = 0; txq < txq_number; txq++)
1390 if ((txq % max_cpu) == cpu)
1391 txq_map |= MVNETA_CPU_TXQ_ACCESS(txq);
1393 /* With only one TX queue we configure a special case
1394 * which will allow to get all the irq on a single
1395 * CPU
1397 if (txq_number == 1)
1398 txq_map = (cpu == pp->rxq_def) ?
1399 MVNETA_CPU_TXQ_ACCESS(1) : 0;
1401 } else {
1402 txq_map = MVNETA_CPU_TXQ_ACCESS_ALL_MASK;
1403 rxq_map = MVNETA_CPU_RXQ_ACCESS_ALL_MASK;
1406 mvreg_write(pp, MVNETA_CPU_MAP(cpu), rxq_map | txq_map);
1409 /* Reset RX and TX DMAs */
1410 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
1411 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
1413 /* Disable Legacy WRR, Disable EJP, Release from reset */
1414 mvreg_write(pp, MVNETA_TXQ_CMD_1, 0);
1415 for (queue = 0; queue < txq_number; queue++) {
1416 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(queue), 0);
1417 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(queue), 0);
1420 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
1421 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
1423 /* Set Port Acceleration Mode */
1424 if (pp->bm_priv)
1425 /* HW buffer management + legacy parser */
1426 val = MVNETA_ACC_MODE_EXT2;
1427 else
1428 /* SW buffer management + legacy parser */
1429 val = MVNETA_ACC_MODE_EXT1;
1430 mvreg_write(pp, MVNETA_ACC_MODE, val);
1432 if (pp->bm_priv)
1433 mvreg_write(pp, MVNETA_BM_ADDRESS, pp->bm_priv->bppi_phys_addr);
1435 /* Update val of portCfg register accordingly with all RxQueue types */
1436 val = MVNETA_PORT_CONFIG_DEFL_VALUE(pp->rxq_def);
1437 mvreg_write(pp, MVNETA_PORT_CONFIG, val);
1439 val = 0;
1440 mvreg_write(pp, MVNETA_PORT_CONFIG_EXTEND, val);
1441 mvreg_write(pp, MVNETA_RX_MIN_FRAME_SIZE, 64);
1443 /* Build PORT_SDMA_CONFIG_REG */
1444 val = 0;
1446 /* Default burst size */
1447 val |= MVNETA_TX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
1448 val |= MVNETA_RX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
1449 val |= MVNETA_RX_NO_DATA_SWAP | MVNETA_TX_NO_DATA_SWAP;
1451 #if defined(__BIG_ENDIAN)
1452 val |= MVNETA_DESC_SWAP;
1453 #endif
1455 /* Assign port SDMA configuration */
1456 mvreg_write(pp, MVNETA_SDMA_CONFIG, val);
1458 /* Disable PHY polling in hardware, since we're using the
1459 * kernel phylib to do this.
1461 val = mvreg_read(pp, MVNETA_UNIT_CONTROL);
1462 val &= ~MVNETA_PHY_POLLING_ENABLE;
1463 mvreg_write(pp, MVNETA_UNIT_CONTROL, val);
1465 mvneta_set_autoneg(pp, pp->use_inband_status);
1466 mvneta_set_ucast_table(pp, -1);
1467 mvneta_set_special_mcast_table(pp, -1);
1468 mvneta_set_other_mcast_table(pp, -1);
1470 /* Set port interrupt enable register - default enable all */
1471 mvreg_write(pp, MVNETA_INTR_ENABLE,
1472 (MVNETA_RXQ_INTR_ENABLE_ALL_MASK
1473 | MVNETA_TXQ_INTR_ENABLE_ALL_MASK));
1475 mvneta_mib_counters_clear(pp);
1478 /* Set max sizes for tx queues */
1479 static void mvneta_txq_max_tx_size_set(struct mvneta_port *pp, int max_tx_size)
1482 u32 val, size, mtu;
1483 int queue;
1485 mtu = max_tx_size * 8;
1486 if (mtu > MVNETA_TX_MTU_MAX)
1487 mtu = MVNETA_TX_MTU_MAX;
1489 /* Set MTU */
1490 val = mvreg_read(pp, MVNETA_TX_MTU);
1491 val &= ~MVNETA_TX_MTU_MAX;
1492 val |= mtu;
1493 mvreg_write(pp, MVNETA_TX_MTU, val);
1495 /* TX token size and all TXQs token size must be larger that MTU */
1496 val = mvreg_read(pp, MVNETA_TX_TOKEN_SIZE);
1498 size = val & MVNETA_TX_TOKEN_SIZE_MAX;
1499 if (size < mtu) {
1500 size = mtu;
1501 val &= ~MVNETA_TX_TOKEN_SIZE_MAX;
1502 val |= size;
1503 mvreg_write(pp, MVNETA_TX_TOKEN_SIZE, val);
1505 for (queue = 0; queue < txq_number; queue++) {
1506 val = mvreg_read(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue));
1508 size = val & MVNETA_TXQ_TOKEN_SIZE_MAX;
1509 if (size < mtu) {
1510 size = mtu;
1511 val &= ~MVNETA_TXQ_TOKEN_SIZE_MAX;
1512 val |= size;
1513 mvreg_write(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue), val);
1518 /* Set unicast address */
1519 static void mvneta_set_ucast_addr(struct mvneta_port *pp, u8 last_nibble,
1520 int queue)
1522 unsigned int unicast_reg;
1523 unsigned int tbl_offset;
1524 unsigned int reg_offset;
1526 /* Locate the Unicast table entry */
1527 last_nibble = (0xf & last_nibble);
1529 /* offset from unicast tbl base */
1530 tbl_offset = (last_nibble / 4) * 4;
1532 /* offset within the above reg */
1533 reg_offset = last_nibble % 4;
1535 unicast_reg = mvreg_read(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset));
1537 if (queue == -1) {
1538 /* Clear accepts frame bit at specified unicast DA tbl entry */
1539 unicast_reg &= ~(0xff << (8 * reg_offset));
1540 } else {
1541 unicast_reg &= ~(0xff << (8 * reg_offset));
1542 unicast_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
1545 mvreg_write(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset), unicast_reg);
1548 /* Set mac address */
1549 static void mvneta_mac_addr_set(struct mvneta_port *pp, unsigned char *addr,
1550 int queue)
1552 unsigned int mac_h;
1553 unsigned int mac_l;
1555 if (queue != -1) {
1556 mac_l = (addr[4] << 8) | (addr[5]);
1557 mac_h = (addr[0] << 24) | (addr[1] << 16) |
1558 (addr[2] << 8) | (addr[3] << 0);
1560 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, mac_l);
1561 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, mac_h);
1564 /* Accept frames of this address */
1565 mvneta_set_ucast_addr(pp, addr[5], queue);
1568 /* Set the number of packets that will be received before RX interrupt
1569 * will be generated by HW.
1571 static void mvneta_rx_pkts_coal_set(struct mvneta_port *pp,
1572 struct mvneta_rx_queue *rxq, u32 value)
1574 mvreg_write(pp, MVNETA_RXQ_THRESHOLD_REG(rxq->id),
1575 value | MVNETA_RXQ_NON_OCCUPIED(0));
1576 rxq->pkts_coal = value;
1579 /* Set the time delay in usec before RX interrupt will be generated by
1580 * HW.
1582 static void mvneta_rx_time_coal_set(struct mvneta_port *pp,
1583 struct mvneta_rx_queue *rxq, u32 value)
1585 u32 val;
1586 unsigned long clk_rate;
1588 clk_rate = clk_get_rate(pp->clk);
1589 val = (clk_rate / 1000000) * value;
1591 mvreg_write(pp, MVNETA_RXQ_TIME_COAL_REG(rxq->id), val);
1592 rxq->time_coal = value;
1595 /* Set threshold for TX_DONE pkts coalescing */
1596 static void mvneta_tx_done_pkts_coal_set(struct mvneta_port *pp,
1597 struct mvneta_tx_queue *txq, u32 value)
1599 u32 val;
1601 val = mvreg_read(pp, MVNETA_TXQ_SIZE_REG(txq->id));
1603 val &= ~MVNETA_TXQ_SENT_THRESH_ALL_MASK;
1604 val |= MVNETA_TXQ_SENT_THRESH_MASK(value);
1606 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), val);
1608 txq->done_pkts_coal = value;
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 u32 status = rx_desc->status;
1708 if (!mvneta_rxq_desc_is_first_last(status)) {
1709 netdev_err(pp->dev,
1710 "bad rx status %08x (buffer oversize), size=%d\n",
1711 status, rx_desc->data_size);
1712 return;
1715 switch (status & MVNETA_RXD_ERR_CODE_MASK) {
1716 case MVNETA_RXD_ERR_CRC:
1717 netdev_err(pp->dev, "bad rx status %08x (crc error), size=%d\n",
1718 status, rx_desc->data_size);
1719 break;
1720 case MVNETA_RXD_ERR_OVERRUN:
1721 netdev_err(pp->dev, "bad rx status %08x (overrun error), size=%d\n",
1722 status, rx_desc->data_size);
1723 break;
1724 case MVNETA_RXD_ERR_LEN:
1725 netdev_err(pp->dev, "bad rx status %08x (max frame length error), size=%d\n",
1726 status, rx_desc->data_size);
1727 break;
1728 case MVNETA_RXD_ERR_RESOURCE:
1729 netdev_err(pp->dev, "bad rx status %08x (resource error), size=%d\n",
1730 status, rx_desc->data_size);
1731 break;
1735 /* Handle RX checksum offload based on the descriptor's status */
1736 static void mvneta_rx_csum(struct mvneta_port *pp, u32 status,
1737 struct sk_buff *skb)
1739 if ((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 void *mvneta_frag_alloc(unsigned int frag_size)
1816 if (likely(frag_size <= PAGE_SIZE))
1817 return netdev_alloc_frag(frag_size);
1818 else
1819 return kmalloc(frag_size, GFP_ATOMIC);
1821 EXPORT_SYMBOL_GPL(mvneta_frag_alloc);
1823 void mvneta_frag_free(unsigned int frag_size, void *data)
1825 if (likely(frag_size <= PAGE_SIZE))
1826 skb_free_frag(data);
1827 else
1828 kfree(data);
1830 EXPORT_SYMBOL_GPL(mvneta_frag_free);
1832 /* Refill processing for SW buffer management */
1833 static int mvneta_rx_refill(struct mvneta_port *pp,
1834 struct mvneta_rx_desc *rx_desc,
1835 struct mvneta_rx_queue *rxq)
1838 dma_addr_t phys_addr;
1839 void *data;
1841 data = mvneta_frag_alloc(pp->frag_size);
1842 if (!data)
1843 return -ENOMEM;
1845 phys_addr = dma_map_single(pp->dev->dev.parent, data,
1846 MVNETA_RX_BUF_SIZE(pp->pkt_size),
1847 DMA_FROM_DEVICE);
1848 if (unlikely(dma_mapping_error(pp->dev->dev.parent, phys_addr))) {
1849 mvneta_frag_free(pp->frag_size, data);
1850 return -ENOMEM;
1853 phys_addr += pp->rx_offset_correction;
1854 mvneta_rx_desc_fill(rx_desc, phys_addr, data, rxq);
1855 return 0;
1858 /* Handle tx checksum */
1859 static u32 mvneta_skb_tx_csum(struct mvneta_port *pp, struct sk_buff *skb)
1861 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1862 int ip_hdr_len = 0;
1863 __be16 l3_proto = vlan_get_protocol(skb);
1864 u8 l4_proto;
1866 if (l3_proto == htons(ETH_P_IP)) {
1867 struct iphdr *ip4h = ip_hdr(skb);
1869 /* Calculate IPv4 checksum and L4 checksum */
1870 ip_hdr_len = ip4h->ihl;
1871 l4_proto = ip4h->protocol;
1872 } else if (l3_proto == htons(ETH_P_IPV6)) {
1873 struct ipv6hdr *ip6h = ipv6_hdr(skb);
1875 /* Read l4_protocol from one of IPv6 extra headers */
1876 if (skb_network_header_len(skb) > 0)
1877 ip_hdr_len = (skb_network_header_len(skb) >> 2);
1878 l4_proto = ip6h->nexthdr;
1879 } else
1880 return MVNETA_TX_L4_CSUM_NOT;
1882 return mvneta_txq_desc_csum(skb_network_offset(skb),
1883 l3_proto, ip_hdr_len, l4_proto);
1886 return MVNETA_TX_L4_CSUM_NOT;
1889 /* Drop packets received by the RXQ and free buffers */
1890 static void mvneta_rxq_drop_pkts(struct mvneta_port *pp,
1891 struct mvneta_rx_queue *rxq)
1893 int rx_done, i;
1895 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
1896 if (rx_done)
1897 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
1899 if (pp->bm_priv) {
1900 for (i = 0; i < rx_done; i++) {
1901 struct mvneta_rx_desc *rx_desc =
1902 mvneta_rxq_next_desc_get(rxq);
1903 u8 pool_id = MVNETA_RX_GET_BM_POOL_ID(rx_desc);
1904 struct mvneta_bm_pool *bm_pool;
1906 bm_pool = &pp->bm_priv->bm_pools[pool_id];
1907 /* Return dropped buffer to the pool */
1908 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
1909 rx_desc->buf_phys_addr);
1911 return;
1914 for (i = 0; i < rxq->size; i++) {
1915 struct mvneta_rx_desc *rx_desc = rxq->descs + i;
1916 void *data = rxq->buf_virt_addr[i];
1918 dma_unmap_single(pp->dev->dev.parent, rx_desc->buf_phys_addr,
1919 MVNETA_RX_BUF_SIZE(pp->pkt_size), DMA_FROM_DEVICE);
1920 mvneta_frag_free(pp->frag_size, data);
1924 /* Main rx processing when using software buffer management */
1925 static int mvneta_rx_swbm(struct mvneta_port *pp, int rx_todo,
1926 struct mvneta_rx_queue *rxq)
1928 struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
1929 struct net_device *dev = pp->dev;
1930 int rx_done;
1931 u32 rcvd_pkts = 0;
1932 u32 rcvd_bytes = 0;
1934 /* Get number of received packets */
1935 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
1937 if (rx_todo > rx_done)
1938 rx_todo = rx_done;
1940 rx_done = 0;
1942 /* Fairness NAPI loop */
1943 while (rx_done < rx_todo) {
1944 struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
1945 struct sk_buff *skb;
1946 unsigned char *data;
1947 dma_addr_t phys_addr;
1948 u32 rx_status, frag_size;
1949 int rx_bytes, err, index;
1951 rx_done++;
1952 rx_status = rx_desc->status;
1953 rx_bytes = rx_desc->data_size - (ETH_FCS_LEN + MVNETA_MH_SIZE);
1954 index = rx_desc - rxq->descs;
1955 data = rxq->buf_virt_addr[index];
1956 phys_addr = rx_desc->buf_phys_addr;
1958 if (!mvneta_rxq_desc_is_first_last(rx_status) ||
1959 (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
1960 err_drop_frame:
1961 dev->stats.rx_errors++;
1962 mvneta_rx_error(pp, rx_desc);
1963 /* leave the descriptor untouched */
1964 continue;
1967 if (rx_bytes <= rx_copybreak) {
1968 /* better copy a small frame and not unmap the DMA region */
1969 skb = netdev_alloc_skb_ip_align(dev, rx_bytes);
1970 if (unlikely(!skb))
1971 goto err_drop_frame;
1973 dma_sync_single_range_for_cpu(dev->dev.parent,
1974 phys_addr,
1975 MVNETA_MH_SIZE + NET_SKB_PAD,
1976 rx_bytes,
1977 DMA_FROM_DEVICE);
1978 memcpy(skb_put(skb, rx_bytes),
1979 data + MVNETA_MH_SIZE + NET_SKB_PAD,
1980 rx_bytes);
1982 skb->protocol = eth_type_trans(skb, dev);
1983 mvneta_rx_csum(pp, rx_status, skb);
1984 napi_gro_receive(&port->napi, skb);
1986 rcvd_pkts++;
1987 rcvd_bytes += rx_bytes;
1989 /* leave the descriptor and buffer untouched */
1990 continue;
1993 /* Refill processing */
1994 err = mvneta_rx_refill(pp, rx_desc, rxq);
1995 if (err) {
1996 netdev_err(dev, "Linux processing - Can't refill\n");
1997 rxq->missed++;
1998 goto err_drop_frame;
2001 frag_size = pp->frag_size;
2003 skb = build_skb(data, frag_size > PAGE_SIZE ? 0 : frag_size);
2005 /* After refill old buffer has to be unmapped regardless
2006 * the skb is successfully built or not.
2008 dma_unmap_single(dev->dev.parent, phys_addr,
2009 MVNETA_RX_BUF_SIZE(pp->pkt_size),
2010 DMA_FROM_DEVICE);
2012 if (!skb)
2013 goto err_drop_frame;
2015 rcvd_pkts++;
2016 rcvd_bytes += rx_bytes;
2018 /* Linux processing */
2019 skb_reserve(skb, MVNETA_MH_SIZE + NET_SKB_PAD);
2020 skb_put(skb, rx_bytes);
2022 skb->protocol = eth_type_trans(skb, dev);
2024 mvneta_rx_csum(pp, rx_status, skb);
2026 napi_gro_receive(&port->napi, skb);
2029 if (rcvd_pkts) {
2030 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
2032 u64_stats_update_begin(&stats->syncp);
2033 stats->rx_packets += rcvd_pkts;
2034 stats->rx_bytes += rcvd_bytes;
2035 u64_stats_update_end(&stats->syncp);
2038 /* Update rxq management counters */
2039 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
2041 return rx_done;
2044 /* Main rx processing when using hardware buffer management */
2045 static int mvneta_rx_hwbm(struct mvneta_port *pp, int rx_todo,
2046 struct mvneta_rx_queue *rxq)
2048 struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
2049 struct net_device *dev = pp->dev;
2050 int rx_done;
2051 u32 rcvd_pkts = 0;
2052 u32 rcvd_bytes = 0;
2054 /* Get number of received packets */
2055 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
2057 if (rx_todo > rx_done)
2058 rx_todo = rx_done;
2060 rx_done = 0;
2062 /* Fairness NAPI loop */
2063 while (rx_done < rx_todo) {
2064 struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
2065 struct mvneta_bm_pool *bm_pool = NULL;
2066 struct sk_buff *skb;
2067 unsigned char *data;
2068 dma_addr_t phys_addr;
2069 u32 rx_status, frag_size;
2070 int rx_bytes, err;
2071 u8 pool_id;
2073 rx_done++;
2074 rx_status = rx_desc->status;
2075 rx_bytes = rx_desc->data_size - (ETH_FCS_LEN + MVNETA_MH_SIZE);
2076 data = (u8 *)(uintptr_t)rx_desc->buf_cookie;
2077 phys_addr = rx_desc->buf_phys_addr;
2078 pool_id = MVNETA_RX_GET_BM_POOL_ID(rx_desc);
2079 bm_pool = &pp->bm_priv->bm_pools[pool_id];
2081 if (!mvneta_rxq_desc_is_first_last(rx_status) ||
2082 (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
2083 err_drop_frame_ret_pool:
2084 /* Return the buffer to the pool */
2085 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
2086 rx_desc->buf_phys_addr);
2087 err_drop_frame:
2088 dev->stats.rx_errors++;
2089 mvneta_rx_error(pp, rx_desc);
2090 /* leave the descriptor untouched */
2091 continue;
2094 if (rx_bytes <= rx_copybreak) {
2095 /* better copy a small frame and not unmap the DMA region */
2096 skb = netdev_alloc_skb_ip_align(dev, rx_bytes);
2097 if (unlikely(!skb))
2098 goto err_drop_frame_ret_pool;
2100 dma_sync_single_range_for_cpu(dev->dev.parent,
2101 rx_desc->buf_phys_addr,
2102 MVNETA_MH_SIZE + NET_SKB_PAD,
2103 rx_bytes,
2104 DMA_FROM_DEVICE);
2105 memcpy(skb_put(skb, rx_bytes),
2106 data + MVNETA_MH_SIZE + NET_SKB_PAD,
2107 rx_bytes);
2109 skb->protocol = eth_type_trans(skb, dev);
2110 mvneta_rx_csum(pp, rx_status, skb);
2111 napi_gro_receive(&port->napi, skb);
2113 rcvd_pkts++;
2114 rcvd_bytes += rx_bytes;
2116 /* Return the buffer to the pool */
2117 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
2118 rx_desc->buf_phys_addr);
2120 /* leave the descriptor and buffer untouched */
2121 continue;
2124 /* Refill processing */
2125 err = hwbm_pool_refill(&bm_pool->hwbm_pool, GFP_ATOMIC);
2126 if (err) {
2127 netdev_err(dev, "Linux processing - Can't refill\n");
2128 rxq->missed++;
2129 goto err_drop_frame_ret_pool;
2132 frag_size = bm_pool->hwbm_pool.frag_size;
2134 skb = build_skb(data, frag_size > PAGE_SIZE ? 0 : frag_size);
2136 /* After refill old buffer has to be unmapped regardless
2137 * the skb is successfully built or not.
2139 dma_unmap_single(&pp->bm_priv->pdev->dev, phys_addr,
2140 bm_pool->buf_size, DMA_FROM_DEVICE);
2141 if (!skb)
2142 goto err_drop_frame;
2144 rcvd_pkts++;
2145 rcvd_bytes += rx_bytes;
2147 /* Linux processing */
2148 skb_reserve(skb, MVNETA_MH_SIZE + NET_SKB_PAD);
2149 skb_put(skb, rx_bytes);
2151 skb->protocol = eth_type_trans(skb, dev);
2153 mvneta_rx_csum(pp, rx_status, skb);
2155 napi_gro_receive(&port->napi, skb);
2158 if (rcvd_pkts) {
2159 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
2161 u64_stats_update_begin(&stats->syncp);
2162 stats->rx_packets += rcvd_pkts;
2163 stats->rx_bytes += rcvd_bytes;
2164 u64_stats_update_end(&stats->syncp);
2167 /* Update rxq management counters */
2168 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
2170 return rx_done;
2173 static inline void
2174 mvneta_tso_put_hdr(struct sk_buff *skb,
2175 struct mvneta_port *pp, struct mvneta_tx_queue *txq)
2177 struct mvneta_tx_desc *tx_desc;
2178 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2180 txq->tx_skb[txq->txq_put_index] = NULL;
2181 tx_desc = mvneta_txq_next_desc_get(txq);
2182 tx_desc->data_size = hdr_len;
2183 tx_desc->command = mvneta_skb_tx_csum(pp, skb);
2184 tx_desc->command |= MVNETA_TXD_F_DESC;
2185 tx_desc->buf_phys_addr = txq->tso_hdrs_phys +
2186 txq->txq_put_index * TSO_HEADER_SIZE;
2187 mvneta_txq_inc_put(txq);
2190 static inline int
2191 mvneta_tso_put_data(struct net_device *dev, struct mvneta_tx_queue *txq,
2192 struct sk_buff *skb, char *data, int size,
2193 bool last_tcp, bool is_last)
2195 struct mvneta_tx_desc *tx_desc;
2197 tx_desc = mvneta_txq_next_desc_get(txq);
2198 tx_desc->data_size = size;
2199 tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, data,
2200 size, DMA_TO_DEVICE);
2201 if (unlikely(dma_mapping_error(dev->dev.parent,
2202 tx_desc->buf_phys_addr))) {
2203 mvneta_txq_desc_put(txq);
2204 return -ENOMEM;
2207 tx_desc->command = 0;
2208 txq->tx_skb[txq->txq_put_index] = NULL;
2210 if (last_tcp) {
2211 /* last descriptor in the TCP packet */
2212 tx_desc->command = MVNETA_TXD_L_DESC;
2214 /* last descriptor in SKB */
2215 if (is_last)
2216 txq->tx_skb[txq->txq_put_index] = skb;
2218 mvneta_txq_inc_put(txq);
2219 return 0;
2222 static int mvneta_tx_tso(struct sk_buff *skb, struct net_device *dev,
2223 struct mvneta_tx_queue *txq)
2225 int total_len, data_left;
2226 int desc_count = 0;
2227 struct mvneta_port *pp = netdev_priv(dev);
2228 struct tso_t tso;
2229 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2230 int i;
2232 /* Count needed descriptors */
2233 if ((txq->count + tso_count_descs(skb)) >= txq->size)
2234 return 0;
2236 if (skb_headlen(skb) < (skb_transport_offset(skb) + tcp_hdrlen(skb))) {
2237 pr_info("*** Is this even possible???!?!?\n");
2238 return 0;
2241 /* Initialize the TSO handler, and prepare the first payload */
2242 tso_start(skb, &tso);
2244 total_len = skb->len - hdr_len;
2245 while (total_len > 0) {
2246 char *hdr;
2248 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
2249 total_len -= data_left;
2250 desc_count++;
2252 /* prepare packet headers: MAC + IP + TCP */
2253 hdr = txq->tso_hdrs + txq->txq_put_index * TSO_HEADER_SIZE;
2254 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
2256 mvneta_tso_put_hdr(skb, pp, txq);
2258 while (data_left > 0) {
2259 int size;
2260 desc_count++;
2262 size = min_t(int, tso.size, data_left);
2264 if (mvneta_tso_put_data(dev, txq, skb,
2265 tso.data, size,
2266 size == data_left,
2267 total_len == 0))
2268 goto err_release;
2269 data_left -= size;
2271 tso_build_data(skb, &tso, size);
2275 return desc_count;
2277 err_release:
2278 /* Release all used data descriptors; header descriptors must not
2279 * be DMA-unmapped.
2281 for (i = desc_count - 1; i >= 0; i--) {
2282 struct mvneta_tx_desc *tx_desc = txq->descs + i;
2283 if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr))
2284 dma_unmap_single(pp->dev->dev.parent,
2285 tx_desc->buf_phys_addr,
2286 tx_desc->data_size,
2287 DMA_TO_DEVICE);
2288 mvneta_txq_desc_put(txq);
2290 return 0;
2293 /* Handle tx fragmentation processing */
2294 static int mvneta_tx_frag_process(struct mvneta_port *pp, struct sk_buff *skb,
2295 struct mvneta_tx_queue *txq)
2297 struct mvneta_tx_desc *tx_desc;
2298 int i, nr_frags = skb_shinfo(skb)->nr_frags;
2300 for (i = 0; i < nr_frags; i++) {
2301 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2302 void *addr = page_address(frag->page.p) + frag->page_offset;
2304 tx_desc = mvneta_txq_next_desc_get(txq);
2305 tx_desc->data_size = frag->size;
2307 tx_desc->buf_phys_addr =
2308 dma_map_single(pp->dev->dev.parent, addr,
2309 tx_desc->data_size, DMA_TO_DEVICE);
2311 if (dma_mapping_error(pp->dev->dev.parent,
2312 tx_desc->buf_phys_addr)) {
2313 mvneta_txq_desc_put(txq);
2314 goto error;
2317 if (i == nr_frags - 1) {
2318 /* Last descriptor */
2319 tx_desc->command = MVNETA_TXD_L_DESC | MVNETA_TXD_Z_PAD;
2320 txq->tx_skb[txq->txq_put_index] = skb;
2321 } else {
2322 /* Descriptor in the middle: Not First, Not Last */
2323 tx_desc->command = 0;
2324 txq->tx_skb[txq->txq_put_index] = NULL;
2326 mvneta_txq_inc_put(txq);
2329 return 0;
2331 error:
2332 /* Release all descriptors that were used to map fragments of
2333 * this packet, as well as the corresponding DMA mappings
2335 for (i = i - 1; i >= 0; i--) {
2336 tx_desc = txq->descs + i;
2337 dma_unmap_single(pp->dev->dev.parent,
2338 tx_desc->buf_phys_addr,
2339 tx_desc->data_size,
2340 DMA_TO_DEVICE);
2341 mvneta_txq_desc_put(txq);
2344 return -ENOMEM;
2347 /* Main tx processing */
2348 static int mvneta_tx(struct sk_buff *skb, struct net_device *dev)
2350 struct mvneta_port *pp = netdev_priv(dev);
2351 u16 txq_id = skb_get_queue_mapping(skb);
2352 struct mvneta_tx_queue *txq = &pp->txqs[txq_id];
2353 struct mvneta_tx_desc *tx_desc;
2354 int len = skb->len;
2355 int frags = 0;
2356 u32 tx_cmd;
2358 if (!netif_running(dev))
2359 goto out;
2361 if (skb_is_gso(skb)) {
2362 frags = mvneta_tx_tso(skb, dev, txq);
2363 goto out;
2366 frags = skb_shinfo(skb)->nr_frags + 1;
2368 /* Get a descriptor for the first part of the packet */
2369 tx_desc = mvneta_txq_next_desc_get(txq);
2371 tx_cmd = mvneta_skb_tx_csum(pp, skb);
2373 tx_desc->data_size = skb_headlen(skb);
2375 tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, skb->data,
2376 tx_desc->data_size,
2377 DMA_TO_DEVICE);
2378 if (unlikely(dma_mapping_error(dev->dev.parent,
2379 tx_desc->buf_phys_addr))) {
2380 mvneta_txq_desc_put(txq);
2381 frags = 0;
2382 goto out;
2385 if (frags == 1) {
2386 /* First and Last descriptor */
2387 tx_cmd |= MVNETA_TXD_FLZ_DESC;
2388 tx_desc->command = tx_cmd;
2389 txq->tx_skb[txq->txq_put_index] = skb;
2390 mvneta_txq_inc_put(txq);
2391 } else {
2392 /* First but not Last */
2393 tx_cmd |= MVNETA_TXD_F_DESC;
2394 txq->tx_skb[txq->txq_put_index] = NULL;
2395 mvneta_txq_inc_put(txq);
2396 tx_desc->command = tx_cmd;
2397 /* Continue with other skb fragments */
2398 if (mvneta_tx_frag_process(pp, skb, txq)) {
2399 dma_unmap_single(dev->dev.parent,
2400 tx_desc->buf_phys_addr,
2401 tx_desc->data_size,
2402 DMA_TO_DEVICE);
2403 mvneta_txq_desc_put(txq);
2404 frags = 0;
2405 goto out;
2409 out:
2410 if (frags > 0) {
2411 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
2412 struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
2414 netdev_tx_sent_queue(nq, len);
2416 txq->count += frags;
2417 if (txq->count >= txq->tx_stop_threshold)
2418 netif_tx_stop_queue(nq);
2420 if (!skb->xmit_more || netif_xmit_stopped(nq) ||
2421 txq->pending + frags > MVNETA_TXQ_DEC_SENT_MASK)
2422 mvneta_txq_pend_desc_add(pp, txq, frags);
2423 else
2424 txq->pending += frags;
2426 u64_stats_update_begin(&stats->syncp);
2427 stats->tx_packets++;
2428 stats->tx_bytes += len;
2429 u64_stats_update_end(&stats->syncp);
2430 } else {
2431 dev->stats.tx_dropped++;
2432 dev_kfree_skb_any(skb);
2435 return NETDEV_TX_OK;
2439 /* Free tx resources, when resetting a port */
2440 static void mvneta_txq_done_force(struct mvneta_port *pp,
2441 struct mvneta_tx_queue *txq)
2444 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
2445 int tx_done = txq->count;
2447 mvneta_txq_bufs_free(pp, txq, tx_done, nq);
2449 /* reset txq */
2450 txq->count = 0;
2451 txq->txq_put_index = 0;
2452 txq->txq_get_index = 0;
2455 /* Handle tx done - called in softirq context. The <cause_tx_done> argument
2456 * must be a valid cause according to MVNETA_TXQ_INTR_MASK_ALL.
2458 static void mvneta_tx_done_gbe(struct mvneta_port *pp, u32 cause_tx_done)
2460 struct mvneta_tx_queue *txq;
2461 struct netdev_queue *nq;
2463 while (cause_tx_done) {
2464 txq = mvneta_tx_done_policy(pp, cause_tx_done);
2466 nq = netdev_get_tx_queue(pp->dev, txq->id);
2467 __netif_tx_lock(nq, smp_processor_id());
2469 if (txq->count)
2470 mvneta_txq_done(pp, txq);
2472 __netif_tx_unlock(nq);
2473 cause_tx_done &= ~((1 << txq->id));
2477 /* Compute crc8 of the specified address, using a unique algorithm ,
2478 * according to hw spec, different than generic crc8 algorithm
2480 static int mvneta_addr_crc(unsigned char *addr)
2482 int crc = 0;
2483 int i;
2485 for (i = 0; i < ETH_ALEN; i++) {
2486 int j;
2488 crc = (crc ^ addr[i]) << 8;
2489 for (j = 7; j >= 0; j--) {
2490 if (crc & (0x100 << j))
2491 crc ^= 0x107 << j;
2495 return crc;
2498 /* This method controls the net device special MAC multicast support.
2499 * The Special Multicast Table for MAC addresses supports MAC of the form
2500 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
2501 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
2502 * Table entries in the DA-Filter table. This method set the Special
2503 * Multicast Table appropriate entry.
2505 static void mvneta_set_special_mcast_addr(struct mvneta_port *pp,
2506 unsigned char last_byte,
2507 int queue)
2509 unsigned int smc_table_reg;
2510 unsigned int tbl_offset;
2511 unsigned int reg_offset;
2513 /* Register offset from SMC table base */
2514 tbl_offset = (last_byte / 4);
2515 /* Entry offset within the above reg */
2516 reg_offset = last_byte % 4;
2518 smc_table_reg = mvreg_read(pp, (MVNETA_DA_FILT_SPEC_MCAST
2519 + tbl_offset * 4));
2521 if (queue == -1)
2522 smc_table_reg &= ~(0xff << (8 * reg_offset));
2523 else {
2524 smc_table_reg &= ~(0xff << (8 * reg_offset));
2525 smc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
2528 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + tbl_offset * 4,
2529 smc_table_reg);
2532 /* This method controls the network device Other MAC multicast support.
2533 * The Other Multicast Table is used for multicast of another type.
2534 * A CRC-8 is used as an index to the Other Multicast Table entries
2535 * in the DA-Filter table.
2536 * The method gets the CRC-8 value from the calling routine and
2537 * sets the Other Multicast Table appropriate entry according to the
2538 * specified CRC-8 .
2540 static void mvneta_set_other_mcast_addr(struct mvneta_port *pp,
2541 unsigned char crc8,
2542 int queue)
2544 unsigned int omc_table_reg;
2545 unsigned int tbl_offset;
2546 unsigned int reg_offset;
2548 tbl_offset = (crc8 / 4) * 4; /* Register offset from OMC table base */
2549 reg_offset = crc8 % 4; /* Entry offset within the above reg */
2551 omc_table_reg = mvreg_read(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset);
2553 if (queue == -1) {
2554 /* Clear accepts frame bit at specified Other DA table entry */
2555 omc_table_reg &= ~(0xff << (8 * reg_offset));
2556 } else {
2557 omc_table_reg &= ~(0xff << (8 * reg_offset));
2558 omc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
2561 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset, omc_table_reg);
2564 /* The network device supports multicast using two tables:
2565 * 1) Special Multicast Table for MAC addresses of the form
2566 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
2567 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
2568 * Table entries in the DA-Filter table.
2569 * 2) Other Multicast Table for multicast of another type. A CRC-8 value
2570 * is used as an index to the Other Multicast Table entries in the
2571 * DA-Filter table.
2573 static int mvneta_mcast_addr_set(struct mvneta_port *pp, unsigned char *p_addr,
2574 int queue)
2576 unsigned char crc_result = 0;
2578 if (memcmp(p_addr, "\x01\x00\x5e\x00\x00", 5) == 0) {
2579 mvneta_set_special_mcast_addr(pp, p_addr[5], queue);
2580 return 0;
2583 crc_result = mvneta_addr_crc(p_addr);
2584 if (queue == -1) {
2585 if (pp->mcast_count[crc_result] == 0) {
2586 netdev_info(pp->dev, "No valid Mcast for crc8=0x%02x\n",
2587 crc_result);
2588 return -EINVAL;
2591 pp->mcast_count[crc_result]--;
2592 if (pp->mcast_count[crc_result] != 0) {
2593 netdev_info(pp->dev,
2594 "After delete there are %d valid Mcast for crc8=0x%02x\n",
2595 pp->mcast_count[crc_result], crc_result);
2596 return -EINVAL;
2598 } else
2599 pp->mcast_count[crc_result]++;
2601 mvneta_set_other_mcast_addr(pp, crc_result, queue);
2603 return 0;
2606 /* Configure Fitering mode of Ethernet port */
2607 static void mvneta_rx_unicast_promisc_set(struct mvneta_port *pp,
2608 int is_promisc)
2610 u32 port_cfg_reg, val;
2612 port_cfg_reg = mvreg_read(pp, MVNETA_PORT_CONFIG);
2614 val = mvreg_read(pp, MVNETA_TYPE_PRIO);
2616 /* Set / Clear UPM bit in port configuration register */
2617 if (is_promisc) {
2618 /* Accept all Unicast addresses */
2619 port_cfg_reg |= MVNETA_UNI_PROMISC_MODE;
2620 val |= MVNETA_FORCE_UNI;
2621 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, 0xffff);
2622 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, 0xffffffff);
2623 } else {
2624 /* Reject all Unicast addresses */
2625 port_cfg_reg &= ~MVNETA_UNI_PROMISC_MODE;
2626 val &= ~MVNETA_FORCE_UNI;
2629 mvreg_write(pp, MVNETA_PORT_CONFIG, port_cfg_reg);
2630 mvreg_write(pp, MVNETA_TYPE_PRIO, val);
2633 /* register unicast and multicast addresses */
2634 static void mvneta_set_rx_mode(struct net_device *dev)
2636 struct mvneta_port *pp = netdev_priv(dev);
2637 struct netdev_hw_addr *ha;
2639 if (dev->flags & IFF_PROMISC) {
2640 /* Accept all: Multicast + Unicast */
2641 mvneta_rx_unicast_promisc_set(pp, 1);
2642 mvneta_set_ucast_table(pp, pp->rxq_def);
2643 mvneta_set_special_mcast_table(pp, pp->rxq_def);
2644 mvneta_set_other_mcast_table(pp, pp->rxq_def);
2645 } else {
2646 /* Accept single Unicast */
2647 mvneta_rx_unicast_promisc_set(pp, 0);
2648 mvneta_set_ucast_table(pp, -1);
2649 mvneta_mac_addr_set(pp, dev->dev_addr, pp->rxq_def);
2651 if (dev->flags & IFF_ALLMULTI) {
2652 /* Accept all multicast */
2653 mvneta_set_special_mcast_table(pp, pp->rxq_def);
2654 mvneta_set_other_mcast_table(pp, pp->rxq_def);
2655 } else {
2656 /* Accept only initialized multicast */
2657 mvneta_set_special_mcast_table(pp, -1);
2658 mvneta_set_other_mcast_table(pp, -1);
2660 if (!netdev_mc_empty(dev)) {
2661 netdev_for_each_mc_addr(ha, dev) {
2662 mvneta_mcast_addr_set(pp, ha->addr,
2663 pp->rxq_def);
2670 /* Interrupt handling - the callback for request_irq() */
2671 static irqreturn_t mvneta_isr(int irq, void *dev_id)
2673 struct mvneta_port *pp = (struct mvneta_port *)dev_id;
2675 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
2676 napi_schedule(&pp->napi);
2678 return IRQ_HANDLED;
2681 /* Interrupt handling - the callback for request_percpu_irq() */
2682 static irqreturn_t mvneta_percpu_isr(int irq, void *dev_id)
2684 struct mvneta_pcpu_port *port = (struct mvneta_pcpu_port *)dev_id;
2686 disable_percpu_irq(port->pp->dev->irq);
2687 napi_schedule(&port->napi);
2689 return IRQ_HANDLED;
2692 static int mvneta_fixed_link_update(struct mvneta_port *pp,
2693 struct phy_device *phy)
2695 struct fixed_phy_status status;
2696 struct fixed_phy_status changed = {};
2697 u32 gmac_stat = mvreg_read(pp, MVNETA_GMAC_STATUS);
2699 status.link = !!(gmac_stat & MVNETA_GMAC_LINK_UP);
2700 if (gmac_stat & MVNETA_GMAC_SPEED_1000)
2701 status.speed = SPEED_1000;
2702 else if (gmac_stat & MVNETA_GMAC_SPEED_100)
2703 status.speed = SPEED_100;
2704 else
2705 status.speed = SPEED_10;
2706 status.duplex = !!(gmac_stat & MVNETA_GMAC_FULL_DUPLEX);
2707 changed.link = 1;
2708 changed.speed = 1;
2709 changed.duplex = 1;
2710 fixed_phy_update_state(phy, &status, &changed);
2711 return 0;
2714 /* NAPI handler
2715 * Bits 0 - 7 of the causeRxTx register indicate that are transmitted
2716 * packets on the corresponding TXQ (Bit 0 is for TX queue 1).
2717 * Bits 8 -15 of the cause Rx Tx register indicate that are received
2718 * packets on the corresponding RXQ (Bit 8 is for RX queue 0).
2719 * Each CPU has its own causeRxTx register
2721 static int mvneta_poll(struct napi_struct *napi, int budget)
2723 int rx_done = 0;
2724 u32 cause_rx_tx;
2725 int rx_queue;
2726 struct mvneta_port *pp = netdev_priv(napi->dev);
2727 struct net_device *ndev = pp->dev;
2728 struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
2730 if (!netif_running(pp->dev)) {
2731 napi_complete(napi);
2732 return rx_done;
2735 /* Read cause register */
2736 cause_rx_tx = mvreg_read(pp, MVNETA_INTR_NEW_CAUSE);
2737 if (cause_rx_tx & MVNETA_MISCINTR_INTR_MASK) {
2738 u32 cause_misc = mvreg_read(pp, MVNETA_INTR_MISC_CAUSE);
2740 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
2741 if (pp->use_inband_status && (cause_misc &
2742 (MVNETA_CAUSE_PHY_STATUS_CHANGE |
2743 MVNETA_CAUSE_LINK_CHANGE |
2744 MVNETA_CAUSE_PSC_SYNC_CHANGE))) {
2745 mvneta_fixed_link_update(pp, ndev->phydev);
2749 /* Release Tx descriptors */
2750 if (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL) {
2751 mvneta_tx_done_gbe(pp, (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL));
2752 cause_rx_tx &= ~MVNETA_TX_INTR_MASK_ALL;
2755 /* For the case where the last mvneta_poll did not process all
2756 * RX packets
2758 rx_queue = fls(((cause_rx_tx >> 8) & 0xff));
2760 cause_rx_tx |= pp->neta_armada3700 ? pp->cause_rx_tx :
2761 port->cause_rx_tx;
2763 if (rx_queue) {
2764 rx_queue = rx_queue - 1;
2765 if (pp->bm_priv)
2766 rx_done = mvneta_rx_hwbm(pp, budget, &pp->rxqs[rx_queue]);
2767 else
2768 rx_done = mvneta_rx_swbm(pp, budget, &pp->rxqs[rx_queue]);
2771 if (rx_done < budget) {
2772 cause_rx_tx = 0;
2773 napi_complete_done(napi, rx_done);
2775 if (pp->neta_armada3700) {
2776 unsigned long flags;
2778 local_irq_save(flags);
2779 mvreg_write(pp, MVNETA_INTR_NEW_MASK,
2780 MVNETA_RX_INTR_MASK(rxq_number) |
2781 MVNETA_TX_INTR_MASK(txq_number) |
2782 MVNETA_MISCINTR_INTR_MASK);
2783 local_irq_restore(flags);
2784 } else {
2785 enable_percpu_irq(pp->dev->irq, 0);
2789 if (pp->neta_armada3700)
2790 pp->cause_rx_tx = cause_rx_tx;
2791 else
2792 port->cause_rx_tx = cause_rx_tx;
2794 return rx_done;
2797 /* Handle rxq fill: allocates rxq skbs; called when initializing a port */
2798 static int mvneta_rxq_fill(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
2799 int num)
2801 int i;
2803 for (i = 0; i < num; i++) {
2804 memset(rxq->descs + i, 0, sizeof(struct mvneta_rx_desc));
2805 if (mvneta_rx_refill(pp, rxq->descs + i, rxq) != 0) {
2806 netdev_err(pp->dev, "%s:rxq %d, %d of %d buffs filled\n",
2807 __func__, rxq->id, i, num);
2808 break;
2812 /* Add this number of RX descriptors as non occupied (ready to
2813 * get packets)
2815 mvneta_rxq_non_occup_desc_add(pp, rxq, i);
2817 return i;
2820 /* Free all packets pending transmit from all TXQs and reset TX port */
2821 static void mvneta_tx_reset(struct mvneta_port *pp)
2823 int queue;
2825 /* free the skb's in the tx ring */
2826 for (queue = 0; queue < txq_number; queue++)
2827 mvneta_txq_done_force(pp, &pp->txqs[queue]);
2829 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
2830 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
2833 static void mvneta_rx_reset(struct mvneta_port *pp)
2835 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
2836 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
2839 /* Rx/Tx queue initialization/cleanup methods */
2841 /* Create a specified RX queue */
2842 static int mvneta_rxq_init(struct mvneta_port *pp,
2843 struct mvneta_rx_queue *rxq)
2846 rxq->size = pp->rx_ring_size;
2848 /* Allocate memory for RX descriptors */
2849 rxq->descs = dma_alloc_coherent(pp->dev->dev.parent,
2850 rxq->size * MVNETA_DESC_ALIGNED_SIZE,
2851 &rxq->descs_phys, GFP_KERNEL);
2852 if (rxq->descs == NULL)
2853 return -ENOMEM;
2855 rxq->last_desc = rxq->size - 1;
2857 /* Set Rx descriptors queue starting address */
2858 mvreg_write(pp, MVNETA_RXQ_BASE_ADDR_REG(rxq->id), rxq->descs_phys);
2859 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), rxq->size);
2861 /* Set Offset */
2862 mvneta_rxq_offset_set(pp, rxq, NET_SKB_PAD - pp->rx_offset_correction);
2864 /* Set coalescing pkts and time */
2865 mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
2866 mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
2868 if (!pp->bm_priv) {
2869 /* Fill RXQ with buffers from RX pool */
2870 mvneta_rxq_buf_size_set(pp, rxq,
2871 MVNETA_RX_BUF_SIZE(pp->pkt_size));
2872 mvneta_rxq_bm_disable(pp, rxq);
2873 mvneta_rxq_fill(pp, rxq, rxq->size);
2874 } else {
2875 mvneta_rxq_bm_enable(pp, rxq);
2876 mvneta_rxq_long_pool_set(pp, rxq);
2877 mvneta_rxq_short_pool_set(pp, rxq);
2878 mvneta_rxq_non_occup_desc_add(pp, rxq, rxq->size);
2881 return 0;
2884 /* Cleanup Rx queue */
2885 static void mvneta_rxq_deinit(struct mvneta_port *pp,
2886 struct mvneta_rx_queue *rxq)
2888 mvneta_rxq_drop_pkts(pp, rxq);
2890 if (rxq->descs)
2891 dma_free_coherent(pp->dev->dev.parent,
2892 rxq->size * MVNETA_DESC_ALIGNED_SIZE,
2893 rxq->descs,
2894 rxq->descs_phys);
2896 rxq->descs = NULL;
2897 rxq->last_desc = 0;
2898 rxq->next_desc_to_proc = 0;
2899 rxq->descs_phys = 0;
2902 /* Create and initialize a tx queue */
2903 static int mvneta_txq_init(struct mvneta_port *pp,
2904 struct mvneta_tx_queue *txq)
2906 int cpu;
2908 txq->size = pp->tx_ring_size;
2910 /* A queue must always have room for at least one skb.
2911 * Therefore, stop the queue when the free entries reaches
2912 * the maximum number of descriptors per skb.
2914 txq->tx_stop_threshold = txq->size - MVNETA_MAX_SKB_DESCS;
2915 txq->tx_wake_threshold = txq->tx_stop_threshold / 2;
2918 /* Allocate memory for TX descriptors */
2919 txq->descs = dma_alloc_coherent(pp->dev->dev.parent,
2920 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2921 &txq->descs_phys, GFP_KERNEL);
2922 if (txq->descs == NULL)
2923 return -ENOMEM;
2925 txq->last_desc = txq->size - 1;
2927 /* Set maximum bandwidth for enabled TXQs */
2928 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0x03ffffff);
2929 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0x3fffffff);
2931 /* Set Tx descriptors queue starting address */
2932 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), txq->descs_phys);
2933 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), txq->size);
2935 txq->tx_skb = kmalloc(txq->size * sizeof(*txq->tx_skb), GFP_KERNEL);
2936 if (txq->tx_skb == NULL) {
2937 dma_free_coherent(pp->dev->dev.parent,
2938 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2939 txq->descs, txq->descs_phys);
2940 return -ENOMEM;
2943 /* Allocate DMA buffers for TSO MAC/IP/TCP headers */
2944 txq->tso_hdrs = dma_alloc_coherent(pp->dev->dev.parent,
2945 txq->size * TSO_HEADER_SIZE,
2946 &txq->tso_hdrs_phys, GFP_KERNEL);
2947 if (txq->tso_hdrs == NULL) {
2948 kfree(txq->tx_skb);
2949 dma_free_coherent(pp->dev->dev.parent,
2950 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2951 txq->descs, txq->descs_phys);
2952 return -ENOMEM;
2954 mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
2956 /* Setup XPS mapping */
2957 if (txq_number > 1)
2958 cpu = txq->id % num_present_cpus();
2959 else
2960 cpu = pp->rxq_def % num_present_cpus();
2961 cpumask_set_cpu(cpu, &txq->affinity_mask);
2962 netif_set_xps_queue(pp->dev, &txq->affinity_mask, txq->id);
2964 return 0;
2967 /* Free allocated resources when mvneta_txq_init() fails to allocate memory*/
2968 static void mvneta_txq_deinit(struct mvneta_port *pp,
2969 struct mvneta_tx_queue *txq)
2971 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
2973 kfree(txq->tx_skb);
2975 if (txq->tso_hdrs)
2976 dma_free_coherent(pp->dev->dev.parent,
2977 txq->size * TSO_HEADER_SIZE,
2978 txq->tso_hdrs, txq->tso_hdrs_phys);
2979 if (txq->descs)
2980 dma_free_coherent(pp->dev->dev.parent,
2981 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2982 txq->descs, txq->descs_phys);
2984 netdev_tx_reset_queue(nq);
2986 txq->descs = NULL;
2987 txq->last_desc = 0;
2988 txq->next_desc_to_proc = 0;
2989 txq->descs_phys = 0;
2991 /* Set minimum bandwidth for disabled TXQs */
2992 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0);
2993 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0);
2995 /* Set Tx descriptors queue starting address and size */
2996 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), 0);
2997 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), 0);
3000 /* Cleanup all Tx queues */
3001 static void mvneta_cleanup_txqs(struct mvneta_port *pp)
3003 int queue;
3005 for (queue = 0; queue < txq_number; queue++)
3006 mvneta_txq_deinit(pp, &pp->txqs[queue]);
3009 /* Cleanup all Rx queues */
3010 static void mvneta_cleanup_rxqs(struct mvneta_port *pp)
3012 int queue;
3014 for (queue = 0; queue < txq_number; queue++)
3015 mvneta_rxq_deinit(pp, &pp->rxqs[queue]);
3019 /* Init all Rx queues */
3020 static int mvneta_setup_rxqs(struct mvneta_port *pp)
3022 int queue;
3024 for (queue = 0; queue < rxq_number; queue++) {
3025 int err = mvneta_rxq_init(pp, &pp->rxqs[queue]);
3027 if (err) {
3028 netdev_err(pp->dev, "%s: can't create rxq=%d\n",
3029 __func__, queue);
3030 mvneta_cleanup_rxqs(pp);
3031 return err;
3035 return 0;
3038 /* Init all tx queues */
3039 static int mvneta_setup_txqs(struct mvneta_port *pp)
3041 int queue;
3043 for (queue = 0; queue < txq_number; queue++) {
3044 int err = mvneta_txq_init(pp, &pp->txqs[queue]);
3045 if (err) {
3046 netdev_err(pp->dev, "%s: can't create txq=%d\n",
3047 __func__, queue);
3048 mvneta_cleanup_txqs(pp);
3049 return err;
3053 return 0;
3056 static void mvneta_start_dev(struct mvneta_port *pp)
3058 int cpu;
3059 struct net_device *ndev = pp->dev;
3061 mvneta_max_rx_size_set(pp, pp->pkt_size);
3062 mvneta_txq_max_tx_size_set(pp, pp->pkt_size);
3064 /* start the Rx/Tx activity */
3065 mvneta_port_enable(pp);
3067 if (!pp->neta_armada3700) {
3068 /* Enable polling on the port */
3069 for_each_online_cpu(cpu) {
3070 struct mvneta_pcpu_port *port =
3071 per_cpu_ptr(pp->ports, cpu);
3073 napi_enable(&port->napi);
3075 } else {
3076 napi_enable(&pp->napi);
3079 /* Unmask interrupts. It has to be done from each CPU */
3080 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
3082 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
3083 MVNETA_CAUSE_PHY_STATUS_CHANGE |
3084 MVNETA_CAUSE_LINK_CHANGE |
3085 MVNETA_CAUSE_PSC_SYNC_CHANGE);
3087 phy_start(ndev->phydev);
3088 netif_tx_start_all_queues(pp->dev);
3091 static void mvneta_stop_dev(struct mvneta_port *pp)
3093 unsigned int cpu;
3094 struct net_device *ndev = pp->dev;
3096 phy_stop(ndev->phydev);
3098 if (!pp->neta_armada3700) {
3099 for_each_online_cpu(cpu) {
3100 struct mvneta_pcpu_port *port =
3101 per_cpu_ptr(pp->ports, cpu);
3103 napi_disable(&port->napi);
3105 } else {
3106 napi_disable(&pp->napi);
3109 netif_carrier_off(pp->dev);
3111 mvneta_port_down(pp);
3112 netif_tx_stop_all_queues(pp->dev);
3114 /* Stop the port activity */
3115 mvneta_port_disable(pp);
3117 /* Clear all ethernet port interrupts */
3118 on_each_cpu(mvneta_percpu_clear_intr_cause, pp, true);
3120 /* Mask all ethernet port interrupts */
3121 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
3123 mvneta_tx_reset(pp);
3124 mvneta_rx_reset(pp);
3127 static void mvneta_percpu_enable(void *arg)
3129 struct mvneta_port *pp = arg;
3131 enable_percpu_irq(pp->dev->irq, IRQ_TYPE_NONE);
3134 static void mvneta_percpu_disable(void *arg)
3136 struct mvneta_port *pp = arg;
3138 disable_percpu_irq(pp->dev->irq);
3141 /* Change the device mtu */
3142 static int mvneta_change_mtu(struct net_device *dev, int mtu)
3144 struct mvneta_port *pp = netdev_priv(dev);
3145 int ret;
3147 if (!IS_ALIGNED(MVNETA_RX_PKT_SIZE(mtu), 8)) {
3148 netdev_info(dev, "Illegal MTU value %d, rounding to %d\n",
3149 mtu, ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8));
3150 mtu = ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8);
3153 dev->mtu = mtu;
3155 if (!netif_running(dev)) {
3156 if (pp->bm_priv)
3157 mvneta_bm_update_mtu(pp, mtu);
3159 netdev_update_features(dev);
3160 return 0;
3163 /* The interface is running, so we have to force a
3164 * reallocation of the queues
3166 mvneta_stop_dev(pp);
3167 on_each_cpu(mvneta_percpu_disable, pp, true);
3169 mvneta_cleanup_txqs(pp);
3170 mvneta_cleanup_rxqs(pp);
3172 if (pp->bm_priv)
3173 mvneta_bm_update_mtu(pp, mtu);
3175 pp->pkt_size = MVNETA_RX_PKT_SIZE(dev->mtu);
3176 pp->frag_size = SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(pp->pkt_size)) +
3177 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
3179 ret = mvneta_setup_rxqs(pp);
3180 if (ret) {
3181 netdev_err(dev, "unable to setup rxqs after MTU change\n");
3182 return ret;
3185 ret = mvneta_setup_txqs(pp);
3186 if (ret) {
3187 netdev_err(dev, "unable to setup txqs after MTU change\n");
3188 return ret;
3191 on_each_cpu(mvneta_percpu_enable, pp, true);
3192 mvneta_start_dev(pp);
3193 mvneta_port_up(pp);
3195 netdev_update_features(dev);
3197 return 0;
3200 static netdev_features_t mvneta_fix_features(struct net_device *dev,
3201 netdev_features_t features)
3203 struct mvneta_port *pp = netdev_priv(dev);
3205 if (pp->tx_csum_limit && dev->mtu > pp->tx_csum_limit) {
3206 features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
3207 netdev_info(dev,
3208 "Disable IP checksum for MTU greater than %dB\n",
3209 pp->tx_csum_limit);
3212 return features;
3215 /* Get mac address */
3216 static void mvneta_get_mac_addr(struct mvneta_port *pp, unsigned char *addr)
3218 u32 mac_addr_l, mac_addr_h;
3220 mac_addr_l = mvreg_read(pp, MVNETA_MAC_ADDR_LOW);
3221 mac_addr_h = mvreg_read(pp, MVNETA_MAC_ADDR_HIGH);
3222 addr[0] = (mac_addr_h >> 24) & 0xFF;
3223 addr[1] = (mac_addr_h >> 16) & 0xFF;
3224 addr[2] = (mac_addr_h >> 8) & 0xFF;
3225 addr[3] = mac_addr_h & 0xFF;
3226 addr[4] = (mac_addr_l >> 8) & 0xFF;
3227 addr[5] = mac_addr_l & 0xFF;
3230 /* Handle setting mac address */
3231 static int mvneta_set_mac_addr(struct net_device *dev, void *addr)
3233 struct mvneta_port *pp = netdev_priv(dev);
3234 struct sockaddr *sockaddr = addr;
3235 int ret;
3237 ret = eth_prepare_mac_addr_change(dev, addr);
3238 if (ret < 0)
3239 return ret;
3240 /* Remove previous address table entry */
3241 mvneta_mac_addr_set(pp, dev->dev_addr, -1);
3243 /* Set new addr in hw */
3244 mvneta_mac_addr_set(pp, sockaddr->sa_data, pp->rxq_def);
3246 eth_commit_mac_addr_change(dev, addr);
3247 return 0;
3250 static void mvneta_adjust_link(struct net_device *ndev)
3252 struct mvneta_port *pp = netdev_priv(ndev);
3253 struct phy_device *phydev = ndev->phydev;
3254 int status_change = 0;
3256 if (phydev->link) {
3257 if ((pp->speed != phydev->speed) ||
3258 (pp->duplex != phydev->duplex)) {
3259 u32 val;
3261 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3262 val &= ~(MVNETA_GMAC_CONFIG_MII_SPEED |
3263 MVNETA_GMAC_CONFIG_GMII_SPEED |
3264 MVNETA_GMAC_CONFIG_FULL_DUPLEX);
3266 if (phydev->duplex)
3267 val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
3269 if (phydev->speed == SPEED_1000)
3270 val |= MVNETA_GMAC_CONFIG_GMII_SPEED;
3271 else if (phydev->speed == SPEED_100)
3272 val |= MVNETA_GMAC_CONFIG_MII_SPEED;
3274 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
3276 pp->duplex = phydev->duplex;
3277 pp->speed = phydev->speed;
3281 if (phydev->link != pp->link) {
3282 if (!phydev->link) {
3283 pp->duplex = -1;
3284 pp->speed = 0;
3287 pp->link = phydev->link;
3288 status_change = 1;
3291 if (status_change) {
3292 if (phydev->link) {
3293 if (!pp->use_inband_status) {
3294 u32 val = mvreg_read(pp,
3295 MVNETA_GMAC_AUTONEG_CONFIG);
3296 val &= ~MVNETA_GMAC_FORCE_LINK_DOWN;
3297 val |= MVNETA_GMAC_FORCE_LINK_PASS;
3298 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
3299 val);
3301 mvneta_port_up(pp);
3302 } else {
3303 if (!pp->use_inband_status) {
3304 u32 val = mvreg_read(pp,
3305 MVNETA_GMAC_AUTONEG_CONFIG);
3306 val &= ~MVNETA_GMAC_FORCE_LINK_PASS;
3307 val |= MVNETA_GMAC_FORCE_LINK_DOWN;
3308 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
3309 val);
3311 mvneta_port_down(pp);
3313 phy_print_status(phydev);
3317 static int mvneta_mdio_probe(struct mvneta_port *pp)
3319 struct phy_device *phy_dev;
3321 phy_dev = of_phy_connect(pp->dev, pp->phy_node, mvneta_adjust_link, 0,
3322 pp->phy_interface);
3323 if (!phy_dev) {
3324 netdev_err(pp->dev, "could not find the PHY\n");
3325 return -ENODEV;
3328 phy_dev->supported &= PHY_GBIT_FEATURES;
3329 phy_dev->advertising = phy_dev->supported;
3331 pp->link = 0;
3332 pp->duplex = 0;
3333 pp->speed = 0;
3335 return 0;
3338 static void mvneta_mdio_remove(struct mvneta_port *pp)
3340 struct net_device *ndev = pp->dev;
3342 phy_disconnect(ndev->phydev);
3345 /* Electing a CPU must be done in an atomic way: it should be done
3346 * after or before the removal/insertion of a CPU and this function is
3347 * not reentrant.
3349 static void mvneta_percpu_elect(struct mvneta_port *pp)
3351 int elected_cpu = 0, max_cpu, cpu, i = 0;
3353 /* Use the cpu associated to the rxq when it is online, in all
3354 * the other cases, use the cpu 0 which can't be offline.
3356 if (cpu_online(pp->rxq_def))
3357 elected_cpu = pp->rxq_def;
3359 max_cpu = num_present_cpus();
3361 for_each_online_cpu(cpu) {
3362 int rxq_map = 0, txq_map = 0;
3363 int rxq;
3365 for (rxq = 0; rxq < rxq_number; rxq++)
3366 if ((rxq % max_cpu) == cpu)
3367 rxq_map |= MVNETA_CPU_RXQ_ACCESS(rxq);
3369 if (cpu == elected_cpu)
3370 /* Map the default receive queue queue to the
3371 * elected CPU
3373 rxq_map |= MVNETA_CPU_RXQ_ACCESS(pp->rxq_def);
3375 /* We update the TX queue map only if we have one
3376 * queue. In this case we associate the TX queue to
3377 * the CPU bound to the default RX queue
3379 if (txq_number == 1)
3380 txq_map = (cpu == elected_cpu) ?
3381 MVNETA_CPU_TXQ_ACCESS(1) : 0;
3382 else
3383 txq_map = mvreg_read(pp, MVNETA_CPU_MAP(cpu)) &
3384 MVNETA_CPU_TXQ_ACCESS_ALL_MASK;
3386 mvreg_write(pp, MVNETA_CPU_MAP(cpu), rxq_map | txq_map);
3388 /* Update the interrupt mask on each CPU according the
3389 * new mapping
3391 smp_call_function_single(cpu, mvneta_percpu_unmask_interrupt,
3392 pp, true);
3393 i++;
3398 static int mvneta_cpu_online(unsigned int cpu, struct hlist_node *node)
3400 int other_cpu;
3401 struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
3402 node_online);
3403 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
3406 spin_lock(&pp->lock);
3408 * Configuring the driver for a new CPU while the driver is
3409 * stopping is racy, so just avoid it.
3411 if (pp->is_stopped) {
3412 spin_unlock(&pp->lock);
3413 return 0;
3415 netif_tx_stop_all_queues(pp->dev);
3418 * We have to synchronise on tha napi of each CPU except the one
3419 * just being woken up
3421 for_each_online_cpu(other_cpu) {
3422 if (other_cpu != cpu) {
3423 struct mvneta_pcpu_port *other_port =
3424 per_cpu_ptr(pp->ports, other_cpu);
3426 napi_synchronize(&other_port->napi);
3430 /* Mask all ethernet port interrupts */
3431 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
3432 napi_enable(&port->napi);
3435 * Enable per-CPU interrupts on the CPU that is
3436 * brought up.
3438 mvneta_percpu_enable(pp);
3441 * Enable per-CPU interrupt on the one CPU we care
3442 * about.
3444 mvneta_percpu_elect(pp);
3446 /* Unmask all ethernet port interrupts */
3447 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
3448 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
3449 MVNETA_CAUSE_PHY_STATUS_CHANGE |
3450 MVNETA_CAUSE_LINK_CHANGE |
3451 MVNETA_CAUSE_PSC_SYNC_CHANGE);
3452 netif_tx_start_all_queues(pp->dev);
3453 spin_unlock(&pp->lock);
3454 return 0;
3457 static int mvneta_cpu_down_prepare(unsigned int cpu, struct hlist_node *node)
3459 struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
3460 node_online);
3461 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
3464 * Thanks to this lock we are sure that any pending cpu election is
3465 * done.
3467 spin_lock(&pp->lock);
3468 /* Mask all ethernet port interrupts */
3469 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
3470 spin_unlock(&pp->lock);
3472 napi_synchronize(&port->napi);
3473 napi_disable(&port->napi);
3474 /* Disable per-CPU interrupts on the CPU that is brought down. */
3475 mvneta_percpu_disable(pp);
3476 return 0;
3479 static int mvneta_cpu_dead(unsigned int cpu, struct hlist_node *node)
3481 struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
3482 node_dead);
3484 /* Check if a new CPU must be elected now this on is down */
3485 spin_lock(&pp->lock);
3486 mvneta_percpu_elect(pp);
3487 spin_unlock(&pp->lock);
3488 /* Unmask all ethernet port interrupts */
3489 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
3490 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
3491 MVNETA_CAUSE_PHY_STATUS_CHANGE |
3492 MVNETA_CAUSE_LINK_CHANGE |
3493 MVNETA_CAUSE_PSC_SYNC_CHANGE);
3494 netif_tx_start_all_queues(pp->dev);
3495 return 0;
3498 static int mvneta_open(struct net_device *dev)
3500 struct mvneta_port *pp = netdev_priv(dev);
3501 int ret;
3503 pp->pkt_size = MVNETA_RX_PKT_SIZE(pp->dev->mtu);
3504 pp->frag_size = SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(pp->pkt_size)) +
3505 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
3507 ret = mvneta_setup_rxqs(pp);
3508 if (ret)
3509 return ret;
3511 ret = mvneta_setup_txqs(pp);
3512 if (ret)
3513 goto err_cleanup_rxqs;
3515 /* Connect to port interrupt line */
3516 if (pp->neta_armada3700)
3517 ret = request_irq(pp->dev->irq, mvneta_isr, 0,
3518 dev->name, pp);
3519 else
3520 ret = request_percpu_irq(pp->dev->irq, mvneta_percpu_isr,
3521 dev->name, pp->ports);
3522 if (ret) {
3523 netdev_err(pp->dev, "cannot request irq %d\n", pp->dev->irq);
3524 goto err_cleanup_txqs;
3527 if (!pp->neta_armada3700) {
3528 /* Enable per-CPU interrupt on all the CPU to handle our RX
3529 * queue interrupts
3531 on_each_cpu(mvneta_percpu_enable, pp, true);
3533 pp->is_stopped = false;
3534 /* Register a CPU notifier to handle the case where our CPU
3535 * might be taken offline.
3537 ret = cpuhp_state_add_instance_nocalls(online_hpstate,
3538 &pp->node_online);
3539 if (ret)
3540 goto err_free_irq;
3542 ret = cpuhp_state_add_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
3543 &pp->node_dead);
3544 if (ret)
3545 goto err_free_online_hp;
3548 /* In default link is down */
3549 netif_carrier_off(pp->dev);
3551 ret = mvneta_mdio_probe(pp);
3552 if (ret < 0) {
3553 netdev_err(dev, "cannot probe MDIO bus\n");
3554 goto err_free_dead_hp;
3557 mvneta_start_dev(pp);
3559 return 0;
3561 err_free_dead_hp:
3562 if (!pp->neta_armada3700)
3563 cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
3564 &pp->node_dead);
3565 err_free_online_hp:
3566 if (!pp->neta_armada3700)
3567 cpuhp_state_remove_instance_nocalls(online_hpstate,
3568 &pp->node_online);
3569 err_free_irq:
3570 if (pp->neta_armada3700) {
3571 free_irq(pp->dev->irq, pp);
3572 } else {
3573 on_each_cpu(mvneta_percpu_disable, pp, true);
3574 free_percpu_irq(pp->dev->irq, pp->ports);
3576 err_cleanup_txqs:
3577 mvneta_cleanup_txqs(pp);
3578 err_cleanup_rxqs:
3579 mvneta_cleanup_rxqs(pp);
3580 return ret;
3583 /* Stop the port, free port interrupt line */
3584 static int mvneta_stop(struct net_device *dev)
3586 struct mvneta_port *pp = netdev_priv(dev);
3588 if (!pp->neta_armada3700) {
3589 /* Inform that we are stopping so we don't want to setup the
3590 * driver for new CPUs in the notifiers. The code of the
3591 * notifier for CPU online is protected by the same spinlock,
3592 * so when we get the lock, the notifer work is done.
3594 spin_lock(&pp->lock);
3595 pp->is_stopped = true;
3596 spin_unlock(&pp->lock);
3598 mvneta_stop_dev(pp);
3599 mvneta_mdio_remove(pp);
3601 cpuhp_state_remove_instance_nocalls(online_hpstate,
3602 &pp->node_online);
3603 cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
3604 &pp->node_dead);
3605 on_each_cpu(mvneta_percpu_disable, pp, true);
3606 free_percpu_irq(dev->irq, pp->ports);
3607 } else {
3608 mvneta_stop_dev(pp);
3609 mvneta_mdio_remove(pp);
3610 free_irq(dev->irq, pp);
3613 mvneta_cleanup_rxqs(pp);
3614 mvneta_cleanup_txqs(pp);
3616 return 0;
3619 static int mvneta_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3621 if (!dev->phydev)
3622 return -ENOTSUPP;
3624 return phy_mii_ioctl(dev->phydev, ifr, cmd);
3627 /* Ethtool methods */
3629 /* Set link ksettings (phy address, speed) for ethtools */
3630 static int
3631 mvneta_ethtool_set_link_ksettings(struct net_device *ndev,
3632 const struct ethtool_link_ksettings *cmd)
3634 struct mvneta_port *pp = netdev_priv(ndev);
3635 struct phy_device *phydev = ndev->phydev;
3637 if (!phydev)
3638 return -ENODEV;
3640 if ((cmd->base.autoneg == AUTONEG_ENABLE) != pp->use_inband_status) {
3641 u32 val;
3643 mvneta_set_autoneg(pp, cmd->base.autoneg == AUTONEG_ENABLE);
3645 if (cmd->base.autoneg == AUTONEG_DISABLE) {
3646 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3647 val &= ~(MVNETA_GMAC_CONFIG_MII_SPEED |
3648 MVNETA_GMAC_CONFIG_GMII_SPEED |
3649 MVNETA_GMAC_CONFIG_FULL_DUPLEX);
3651 if (phydev->duplex)
3652 val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
3654 if (phydev->speed == SPEED_1000)
3655 val |= MVNETA_GMAC_CONFIG_GMII_SPEED;
3656 else if (phydev->speed == SPEED_100)
3657 val |= MVNETA_GMAC_CONFIG_MII_SPEED;
3659 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
3662 pp->use_inband_status = (cmd->base.autoneg == AUTONEG_ENABLE);
3663 netdev_info(pp->dev, "autoneg status set to %i\n",
3664 pp->use_inband_status);
3666 if (netif_running(ndev)) {
3667 mvneta_port_down(pp);
3668 mvneta_port_up(pp);
3672 return phy_ethtool_ksettings_set(ndev->phydev, cmd);
3675 /* Set interrupt coalescing for ethtools */
3676 static int mvneta_ethtool_set_coalesce(struct net_device *dev,
3677 struct ethtool_coalesce *c)
3679 struct mvneta_port *pp = netdev_priv(dev);
3680 int queue;
3682 for (queue = 0; queue < rxq_number; queue++) {
3683 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
3684 rxq->time_coal = c->rx_coalesce_usecs;
3685 rxq->pkts_coal = c->rx_max_coalesced_frames;
3686 mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
3687 mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
3690 for (queue = 0; queue < txq_number; queue++) {
3691 struct mvneta_tx_queue *txq = &pp->txqs[queue];
3692 txq->done_pkts_coal = c->tx_max_coalesced_frames;
3693 mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
3696 return 0;
3699 /* get coalescing for ethtools */
3700 static int mvneta_ethtool_get_coalesce(struct net_device *dev,
3701 struct ethtool_coalesce *c)
3703 struct mvneta_port *pp = netdev_priv(dev);
3705 c->rx_coalesce_usecs = pp->rxqs[0].time_coal;
3706 c->rx_max_coalesced_frames = pp->rxqs[0].pkts_coal;
3708 c->tx_max_coalesced_frames = pp->txqs[0].done_pkts_coal;
3709 return 0;
3713 static void mvneta_ethtool_get_drvinfo(struct net_device *dev,
3714 struct ethtool_drvinfo *drvinfo)
3716 strlcpy(drvinfo->driver, MVNETA_DRIVER_NAME,
3717 sizeof(drvinfo->driver));
3718 strlcpy(drvinfo->version, MVNETA_DRIVER_VERSION,
3719 sizeof(drvinfo->version));
3720 strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
3721 sizeof(drvinfo->bus_info));
3725 static void mvneta_ethtool_get_ringparam(struct net_device *netdev,
3726 struct ethtool_ringparam *ring)
3728 struct mvneta_port *pp = netdev_priv(netdev);
3730 ring->rx_max_pending = MVNETA_MAX_RXD;
3731 ring->tx_max_pending = MVNETA_MAX_TXD;
3732 ring->rx_pending = pp->rx_ring_size;
3733 ring->tx_pending = pp->tx_ring_size;
3736 static int mvneta_ethtool_set_ringparam(struct net_device *dev,
3737 struct ethtool_ringparam *ring)
3739 struct mvneta_port *pp = netdev_priv(dev);
3741 if ((ring->rx_pending == 0) || (ring->tx_pending == 0))
3742 return -EINVAL;
3743 pp->rx_ring_size = ring->rx_pending < MVNETA_MAX_RXD ?
3744 ring->rx_pending : MVNETA_MAX_RXD;
3746 pp->tx_ring_size = clamp_t(u16, ring->tx_pending,
3747 MVNETA_MAX_SKB_DESCS * 2, MVNETA_MAX_TXD);
3748 if (pp->tx_ring_size != ring->tx_pending)
3749 netdev_warn(dev, "TX queue size set to %u (requested %u)\n",
3750 pp->tx_ring_size, ring->tx_pending);
3752 if (netif_running(dev)) {
3753 mvneta_stop(dev);
3754 if (mvneta_open(dev)) {
3755 netdev_err(dev,
3756 "error on opening device after ring param change\n");
3757 return -ENOMEM;
3761 return 0;
3764 static void mvneta_ethtool_get_strings(struct net_device *netdev, u32 sset,
3765 u8 *data)
3767 if (sset == ETH_SS_STATS) {
3768 int i;
3770 for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++)
3771 memcpy(data + i * ETH_GSTRING_LEN,
3772 mvneta_statistics[i].name, ETH_GSTRING_LEN);
3776 static void mvneta_ethtool_update_stats(struct mvneta_port *pp)
3778 const struct mvneta_statistic *s;
3779 void __iomem *base = pp->base;
3780 u32 high, low, val;
3781 u64 val64;
3782 int i;
3784 for (i = 0, s = mvneta_statistics;
3785 s < mvneta_statistics + ARRAY_SIZE(mvneta_statistics);
3786 s++, i++) {
3787 switch (s->type) {
3788 case T_REG_32:
3789 val = readl_relaxed(base + s->offset);
3790 pp->ethtool_stats[i] += val;
3791 break;
3792 case T_REG_64:
3793 /* Docs say to read low 32-bit then high */
3794 low = readl_relaxed(base + s->offset);
3795 high = readl_relaxed(base + s->offset + 4);
3796 val64 = (u64)high << 32 | low;
3797 pp->ethtool_stats[i] += val64;
3798 break;
3803 static void mvneta_ethtool_get_stats(struct net_device *dev,
3804 struct ethtool_stats *stats, u64 *data)
3806 struct mvneta_port *pp = netdev_priv(dev);
3807 int i;
3809 mvneta_ethtool_update_stats(pp);
3811 for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++)
3812 *data++ = pp->ethtool_stats[i];
3815 static int mvneta_ethtool_get_sset_count(struct net_device *dev, int sset)
3817 if (sset == ETH_SS_STATS)
3818 return ARRAY_SIZE(mvneta_statistics);
3819 return -EOPNOTSUPP;
3822 static u32 mvneta_ethtool_get_rxfh_indir_size(struct net_device *dev)
3824 return MVNETA_RSS_LU_TABLE_SIZE;
3827 static int mvneta_ethtool_get_rxnfc(struct net_device *dev,
3828 struct ethtool_rxnfc *info,
3829 u32 *rules __always_unused)
3831 switch (info->cmd) {
3832 case ETHTOOL_GRXRINGS:
3833 info->data = rxq_number;
3834 return 0;
3835 case ETHTOOL_GRXFH:
3836 return -EOPNOTSUPP;
3837 default:
3838 return -EOPNOTSUPP;
3842 static int mvneta_config_rss(struct mvneta_port *pp)
3844 int cpu;
3845 u32 val;
3847 netif_tx_stop_all_queues(pp->dev);
3849 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
3851 /* We have to synchronise on the napi of each CPU */
3852 for_each_online_cpu(cpu) {
3853 struct mvneta_pcpu_port *pcpu_port =
3854 per_cpu_ptr(pp->ports, cpu);
3856 napi_synchronize(&pcpu_port->napi);
3857 napi_disable(&pcpu_port->napi);
3860 pp->rxq_def = pp->indir[0];
3862 /* Update unicast mapping */
3863 mvneta_set_rx_mode(pp->dev);
3865 /* Update val of portCfg register accordingly with all RxQueue types */
3866 val = MVNETA_PORT_CONFIG_DEFL_VALUE(pp->rxq_def);
3867 mvreg_write(pp, MVNETA_PORT_CONFIG, val);
3869 /* Update the elected CPU matching the new rxq_def */
3870 spin_lock(&pp->lock);
3871 mvneta_percpu_elect(pp);
3872 spin_unlock(&pp->lock);
3874 /* We have to synchronise on the napi of each CPU */
3875 for_each_online_cpu(cpu) {
3876 struct mvneta_pcpu_port *pcpu_port =
3877 per_cpu_ptr(pp->ports, cpu);
3879 napi_enable(&pcpu_port->napi);
3882 netif_tx_start_all_queues(pp->dev);
3884 return 0;
3887 static int mvneta_ethtool_set_rxfh(struct net_device *dev, const u32 *indir,
3888 const u8 *key, const u8 hfunc)
3890 struct mvneta_port *pp = netdev_priv(dev);
3892 /* Current code for Armada 3700 doesn't support RSS features yet */
3893 if (pp->neta_armada3700)
3894 return -EOPNOTSUPP;
3896 /* We require at least one supported parameter to be changed
3897 * and no change in any of the unsupported parameters
3899 if (key ||
3900 (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
3901 return -EOPNOTSUPP;
3903 if (!indir)
3904 return 0;
3906 memcpy(pp->indir, indir, MVNETA_RSS_LU_TABLE_SIZE);
3908 return mvneta_config_rss(pp);
3911 static int mvneta_ethtool_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
3912 u8 *hfunc)
3914 struct mvneta_port *pp = netdev_priv(dev);
3916 /* Current code for Armada 3700 doesn't support RSS features yet */
3917 if (pp->neta_armada3700)
3918 return -EOPNOTSUPP;
3920 if (hfunc)
3921 *hfunc = ETH_RSS_HASH_TOP;
3923 if (!indir)
3924 return 0;
3926 memcpy(indir, pp->indir, MVNETA_RSS_LU_TABLE_SIZE);
3928 return 0;
3931 static void mvneta_ethtool_get_wol(struct net_device *dev,
3932 struct ethtool_wolinfo *wol)
3934 wol->supported = 0;
3935 wol->wolopts = 0;
3937 if (dev->phydev)
3938 phy_ethtool_get_wol(dev->phydev, wol);
3941 static int mvneta_ethtool_set_wol(struct net_device *dev,
3942 struct ethtool_wolinfo *wol)
3944 if (!dev->phydev)
3945 return -EOPNOTSUPP;
3947 return phy_ethtool_set_wol(dev->phydev, wol);
3950 static const struct net_device_ops mvneta_netdev_ops = {
3951 .ndo_open = mvneta_open,
3952 .ndo_stop = mvneta_stop,
3953 .ndo_start_xmit = mvneta_tx,
3954 .ndo_set_rx_mode = mvneta_set_rx_mode,
3955 .ndo_set_mac_address = mvneta_set_mac_addr,
3956 .ndo_change_mtu = mvneta_change_mtu,
3957 .ndo_fix_features = mvneta_fix_features,
3958 .ndo_get_stats64 = mvneta_get_stats64,
3959 .ndo_do_ioctl = mvneta_ioctl,
3962 static const struct ethtool_ops mvneta_eth_tool_ops = {
3963 .nway_reset = phy_ethtool_nway_reset,
3964 .get_link = ethtool_op_get_link,
3965 .set_coalesce = mvneta_ethtool_set_coalesce,
3966 .get_coalesce = mvneta_ethtool_get_coalesce,
3967 .get_drvinfo = mvneta_ethtool_get_drvinfo,
3968 .get_ringparam = mvneta_ethtool_get_ringparam,
3969 .set_ringparam = mvneta_ethtool_set_ringparam,
3970 .get_strings = mvneta_ethtool_get_strings,
3971 .get_ethtool_stats = mvneta_ethtool_get_stats,
3972 .get_sset_count = mvneta_ethtool_get_sset_count,
3973 .get_rxfh_indir_size = mvneta_ethtool_get_rxfh_indir_size,
3974 .get_rxnfc = mvneta_ethtool_get_rxnfc,
3975 .get_rxfh = mvneta_ethtool_get_rxfh,
3976 .set_rxfh = mvneta_ethtool_set_rxfh,
3977 .get_link_ksettings = phy_ethtool_get_link_ksettings,
3978 .set_link_ksettings = mvneta_ethtool_set_link_ksettings,
3979 .get_wol = mvneta_ethtool_get_wol,
3980 .set_wol = mvneta_ethtool_set_wol,
3983 /* Initialize hw */
3984 static int mvneta_init(struct device *dev, struct mvneta_port *pp)
3986 int queue;
3988 /* Disable port */
3989 mvneta_port_disable(pp);
3991 /* Set port default values */
3992 mvneta_defaults_set(pp);
3994 pp->txqs = devm_kcalloc(dev, txq_number, sizeof(struct mvneta_tx_queue),
3995 GFP_KERNEL);
3996 if (!pp->txqs)
3997 return -ENOMEM;
3999 /* Initialize TX descriptor rings */
4000 for (queue = 0; queue < txq_number; queue++) {
4001 struct mvneta_tx_queue *txq = &pp->txqs[queue];
4002 txq->id = queue;
4003 txq->size = pp->tx_ring_size;
4004 txq->done_pkts_coal = MVNETA_TXDONE_COAL_PKTS;
4007 pp->rxqs = devm_kcalloc(dev, rxq_number, sizeof(struct mvneta_rx_queue),
4008 GFP_KERNEL);
4009 if (!pp->rxqs)
4010 return -ENOMEM;
4012 /* Create Rx descriptor rings */
4013 for (queue = 0; queue < rxq_number; queue++) {
4014 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
4015 rxq->id = queue;
4016 rxq->size = pp->rx_ring_size;
4017 rxq->pkts_coal = MVNETA_RX_COAL_PKTS;
4018 rxq->time_coal = MVNETA_RX_COAL_USEC;
4019 rxq->buf_virt_addr = devm_kmalloc(pp->dev->dev.parent,
4020 rxq->size * sizeof(void *),
4021 GFP_KERNEL);
4022 if (!rxq->buf_virt_addr)
4023 return -ENOMEM;
4026 return 0;
4029 /* platform glue : initialize decoding windows */
4030 static void mvneta_conf_mbus_windows(struct mvneta_port *pp,
4031 const struct mbus_dram_target_info *dram)
4033 u32 win_enable;
4034 u32 win_protect;
4035 int i;
4037 for (i = 0; i < 6; i++) {
4038 mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
4039 mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
4041 if (i < 4)
4042 mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
4045 win_enable = 0x3f;
4046 win_protect = 0;
4048 if (dram) {
4049 for (i = 0; i < dram->num_cs; i++) {
4050 const struct mbus_dram_window *cs = dram->cs + i;
4052 mvreg_write(pp, MVNETA_WIN_BASE(i),
4053 (cs->base & 0xffff0000) |
4054 (cs->mbus_attr << 8) |
4055 dram->mbus_dram_target_id);
4057 mvreg_write(pp, MVNETA_WIN_SIZE(i),
4058 (cs->size - 1) & 0xffff0000);
4060 win_enable &= ~(1 << i);
4061 win_protect |= 3 << (2 * i);
4063 } else {
4064 /* For Armada3700 open default 4GB Mbus window, leaving
4065 * arbitration of target/attribute to a different layer
4066 * of configuration.
4068 mvreg_write(pp, MVNETA_WIN_SIZE(0), 0xffff0000);
4069 win_enable &= ~BIT(0);
4070 win_protect = 3;
4073 mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
4074 mvreg_write(pp, MVNETA_ACCESS_PROTECT_ENABLE, win_protect);
4077 /* Power up the port */
4078 static int mvneta_port_power_up(struct mvneta_port *pp, int phy_mode)
4080 u32 ctrl;
4082 /* MAC Cause register should be cleared */
4083 mvreg_write(pp, MVNETA_UNIT_INTR_CAUSE, 0);
4085 ctrl = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
4087 /* Even though it might look weird, when we're configured in
4088 * SGMII or QSGMII mode, the RGMII bit needs to be set.
4090 switch(phy_mode) {
4091 case PHY_INTERFACE_MODE_QSGMII:
4092 mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_QSGMII_SERDES_PROTO);
4093 ctrl |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII;
4094 break;
4095 case PHY_INTERFACE_MODE_SGMII:
4096 mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_SGMII_SERDES_PROTO);
4097 ctrl |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII;
4098 break;
4099 case PHY_INTERFACE_MODE_RGMII:
4100 case PHY_INTERFACE_MODE_RGMII_ID:
4101 ctrl |= MVNETA_GMAC2_PORT_RGMII;
4102 break;
4103 default:
4104 return -EINVAL;
4107 /* Cancel Port Reset */
4108 ctrl &= ~MVNETA_GMAC2_PORT_RESET;
4109 mvreg_write(pp, MVNETA_GMAC_CTRL_2, ctrl);
4111 while ((mvreg_read(pp, MVNETA_GMAC_CTRL_2) &
4112 MVNETA_GMAC2_PORT_RESET) != 0)
4113 continue;
4115 return 0;
4118 /* Device initialization routine */
4119 static int mvneta_probe(struct platform_device *pdev)
4121 const struct mbus_dram_target_info *dram_target_info;
4122 struct resource *res;
4123 struct device_node *dn = pdev->dev.of_node;
4124 struct device_node *phy_node;
4125 struct device_node *bm_node;
4126 struct mvneta_port *pp;
4127 struct net_device *dev;
4128 const char *dt_mac_addr;
4129 char hw_mac_addr[ETH_ALEN];
4130 const char *mac_from;
4131 const char *managed;
4132 int tx_csum_limit;
4133 int phy_mode;
4134 int err;
4135 int cpu;
4137 dev = alloc_etherdev_mqs(sizeof(struct mvneta_port), txq_number, rxq_number);
4138 if (!dev)
4139 return -ENOMEM;
4141 dev->irq = irq_of_parse_and_map(dn, 0);
4142 if (dev->irq == 0) {
4143 err = -EINVAL;
4144 goto err_free_netdev;
4147 phy_node = of_parse_phandle(dn, "phy", 0);
4148 if (!phy_node) {
4149 if (!of_phy_is_fixed_link(dn)) {
4150 dev_err(&pdev->dev, "no PHY specified\n");
4151 err = -ENODEV;
4152 goto err_free_irq;
4155 err = of_phy_register_fixed_link(dn);
4156 if (err < 0) {
4157 dev_err(&pdev->dev, "cannot register fixed PHY\n");
4158 goto err_free_irq;
4161 /* In the case of a fixed PHY, the DT node associated
4162 * to the PHY is the Ethernet MAC DT node.
4164 phy_node = of_node_get(dn);
4167 phy_mode = of_get_phy_mode(dn);
4168 if (phy_mode < 0) {
4169 dev_err(&pdev->dev, "incorrect phy-mode\n");
4170 err = -EINVAL;
4171 goto err_put_phy_node;
4174 dev->tx_queue_len = MVNETA_MAX_TXD;
4175 dev->watchdog_timeo = 5 * HZ;
4176 dev->netdev_ops = &mvneta_netdev_ops;
4178 dev->ethtool_ops = &mvneta_eth_tool_ops;
4180 pp = netdev_priv(dev);
4181 spin_lock_init(&pp->lock);
4182 pp->phy_node = phy_node;
4183 pp->phy_interface = phy_mode;
4185 err = of_property_read_string(dn, "managed", &managed);
4186 pp->use_inband_status = (err == 0 &&
4187 strcmp(managed, "in-band-status") == 0);
4189 pp->rxq_def = rxq_def;
4191 /* Set RX packet offset correction for platforms, whose
4192 * NET_SKB_PAD, exceeds 64B. It should be 64B for 64-bit
4193 * platforms and 0B for 32-bit ones.
4195 pp->rx_offset_correction =
4196 max(0, NET_SKB_PAD - MVNETA_RX_PKT_OFFSET_CORRECTION);
4198 pp->indir[0] = rxq_def;
4200 /* Get special SoC configurations */
4201 if (of_device_is_compatible(dn, "marvell,armada-3700-neta"))
4202 pp->neta_armada3700 = true;
4204 pp->clk = devm_clk_get(&pdev->dev, "core");
4205 if (IS_ERR(pp->clk))
4206 pp->clk = devm_clk_get(&pdev->dev, NULL);
4207 if (IS_ERR(pp->clk)) {
4208 err = PTR_ERR(pp->clk);
4209 goto err_put_phy_node;
4212 clk_prepare_enable(pp->clk);
4214 pp->clk_bus = devm_clk_get(&pdev->dev, "bus");
4215 if (!IS_ERR(pp->clk_bus))
4216 clk_prepare_enable(pp->clk_bus);
4218 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4219 pp->base = devm_ioremap_resource(&pdev->dev, res);
4220 if (IS_ERR(pp->base)) {
4221 err = PTR_ERR(pp->base);
4222 goto err_clk;
4225 /* Alloc per-cpu port structure */
4226 pp->ports = alloc_percpu(struct mvneta_pcpu_port);
4227 if (!pp->ports) {
4228 err = -ENOMEM;
4229 goto err_clk;
4232 /* Alloc per-cpu stats */
4233 pp->stats = netdev_alloc_pcpu_stats(struct mvneta_pcpu_stats);
4234 if (!pp->stats) {
4235 err = -ENOMEM;
4236 goto err_free_ports;
4239 dt_mac_addr = of_get_mac_address(dn);
4240 if (dt_mac_addr) {
4241 mac_from = "device tree";
4242 memcpy(dev->dev_addr, dt_mac_addr, ETH_ALEN);
4243 } else {
4244 mvneta_get_mac_addr(pp, hw_mac_addr);
4245 if (is_valid_ether_addr(hw_mac_addr)) {
4246 mac_from = "hardware";
4247 memcpy(dev->dev_addr, hw_mac_addr, ETH_ALEN);
4248 } else {
4249 mac_from = "random";
4250 eth_hw_addr_random(dev);
4254 if (!of_property_read_u32(dn, "tx-csum-limit", &tx_csum_limit)) {
4255 if (tx_csum_limit < 0 ||
4256 tx_csum_limit > MVNETA_TX_CSUM_MAX_SIZE) {
4257 tx_csum_limit = MVNETA_TX_CSUM_DEF_SIZE;
4258 dev_info(&pdev->dev,
4259 "Wrong TX csum limit in DT, set to %dB\n",
4260 MVNETA_TX_CSUM_DEF_SIZE);
4262 } else if (of_device_is_compatible(dn, "marvell,armada-370-neta")) {
4263 tx_csum_limit = MVNETA_TX_CSUM_DEF_SIZE;
4264 } else {
4265 tx_csum_limit = MVNETA_TX_CSUM_MAX_SIZE;
4268 pp->tx_csum_limit = tx_csum_limit;
4270 dram_target_info = mv_mbus_dram_info();
4271 /* Armada3700 requires setting default configuration of Mbus
4272 * windows, however without using filled mbus_dram_target_info
4273 * structure.
4275 if (dram_target_info || pp->neta_armada3700)
4276 mvneta_conf_mbus_windows(pp, dram_target_info);
4278 pp->tx_ring_size = MVNETA_MAX_TXD;
4279 pp->rx_ring_size = MVNETA_MAX_RXD;
4281 pp->dev = dev;
4282 SET_NETDEV_DEV(dev, &pdev->dev);
4284 pp->id = global_port_id++;
4286 /* Obtain access to BM resources if enabled and already initialized */
4287 bm_node = of_parse_phandle(dn, "buffer-manager", 0);
4288 if (bm_node && bm_node->data) {
4289 pp->bm_priv = bm_node->data;
4290 err = mvneta_bm_port_init(pdev, pp);
4291 if (err < 0) {
4292 dev_info(&pdev->dev, "use SW buffer management\n");
4293 pp->bm_priv = NULL;
4296 of_node_put(bm_node);
4298 err = mvneta_init(&pdev->dev, pp);
4299 if (err < 0)
4300 goto err_netdev;
4302 err = mvneta_port_power_up(pp, phy_mode);
4303 if (err < 0) {
4304 dev_err(&pdev->dev, "can't power up port\n");
4305 goto err_netdev;
4308 /* Armada3700 network controller does not support per-cpu
4309 * operation, so only single NAPI should be initialized.
4311 if (pp->neta_armada3700) {
4312 netif_napi_add(dev, &pp->napi, mvneta_poll, NAPI_POLL_WEIGHT);
4313 } else {
4314 for_each_present_cpu(cpu) {
4315 struct mvneta_pcpu_port *port =
4316 per_cpu_ptr(pp->ports, cpu);
4318 netif_napi_add(dev, &port->napi, mvneta_poll,
4319 NAPI_POLL_WEIGHT);
4320 port->pp = pp;
4324 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO;
4325 dev->hw_features |= dev->features;
4326 dev->vlan_features |= dev->features;
4327 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
4328 dev->gso_max_segs = MVNETA_MAX_TSO_SEGS;
4330 /* MTU range: 68 - 9676 */
4331 dev->min_mtu = ETH_MIN_MTU;
4332 /* 9676 == 9700 - 20 and rounding to 8 */
4333 dev->max_mtu = 9676;
4335 err = register_netdev(dev);
4336 if (err < 0) {
4337 dev_err(&pdev->dev, "failed to register\n");
4338 goto err_free_stats;
4341 netdev_info(dev, "Using %s mac address %pM\n", mac_from,
4342 dev->dev_addr);
4344 platform_set_drvdata(pdev, pp->dev);
4346 if (pp->use_inband_status) {
4347 struct phy_device *phy = of_phy_find_device(dn);
4349 mvneta_fixed_link_update(pp, phy);
4351 put_device(&phy->mdio.dev);
4354 return 0;
4356 err_netdev:
4357 unregister_netdev(dev);
4358 if (pp->bm_priv) {
4359 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
4360 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short,
4361 1 << pp->id);
4363 err_free_stats:
4364 free_percpu(pp->stats);
4365 err_free_ports:
4366 free_percpu(pp->ports);
4367 err_clk:
4368 clk_disable_unprepare(pp->clk_bus);
4369 clk_disable_unprepare(pp->clk);
4370 err_put_phy_node:
4371 of_node_put(phy_node);
4372 if (of_phy_is_fixed_link(dn))
4373 of_phy_deregister_fixed_link(dn);
4374 err_free_irq:
4375 irq_dispose_mapping(dev->irq);
4376 err_free_netdev:
4377 free_netdev(dev);
4378 return err;
4381 /* Device removal routine */
4382 static int mvneta_remove(struct platform_device *pdev)
4384 struct net_device *dev = platform_get_drvdata(pdev);
4385 struct device_node *dn = pdev->dev.of_node;
4386 struct mvneta_port *pp = netdev_priv(dev);
4388 unregister_netdev(dev);
4389 clk_disable_unprepare(pp->clk_bus);
4390 clk_disable_unprepare(pp->clk);
4391 free_percpu(pp->ports);
4392 free_percpu(pp->stats);
4393 if (of_phy_is_fixed_link(dn))
4394 of_phy_deregister_fixed_link(dn);
4395 irq_dispose_mapping(dev->irq);
4396 of_node_put(pp->phy_node);
4397 free_netdev(dev);
4399 if (pp->bm_priv) {
4400 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
4401 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short,
4402 1 << pp->id);
4405 return 0;
4408 static const struct of_device_id mvneta_match[] = {
4409 { .compatible = "marvell,armada-370-neta" },
4410 { .compatible = "marvell,armada-xp-neta" },
4411 { .compatible = "marvell,armada-3700-neta" },
4414 MODULE_DEVICE_TABLE(of, mvneta_match);
4416 static struct platform_driver mvneta_driver = {
4417 .probe = mvneta_probe,
4418 .remove = mvneta_remove,
4419 .driver = {
4420 .name = MVNETA_DRIVER_NAME,
4421 .of_match_table = mvneta_match,
4425 static int __init mvneta_driver_init(void)
4427 int ret;
4429 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "net/mvmeta:online",
4430 mvneta_cpu_online,
4431 mvneta_cpu_down_prepare);
4432 if (ret < 0)
4433 goto out;
4434 online_hpstate = ret;
4435 ret = cpuhp_setup_state_multi(CPUHP_NET_MVNETA_DEAD, "net/mvneta:dead",
4436 NULL, mvneta_cpu_dead);
4437 if (ret)
4438 goto err_dead;
4440 ret = platform_driver_register(&mvneta_driver);
4441 if (ret)
4442 goto err;
4443 return 0;
4445 err:
4446 cpuhp_remove_multi_state(CPUHP_NET_MVNETA_DEAD);
4447 err_dead:
4448 cpuhp_remove_multi_state(online_hpstate);
4449 out:
4450 return ret;
4452 module_init(mvneta_driver_init);
4454 static void __exit mvneta_driver_exit(void)
4456 platform_driver_unregister(&mvneta_driver);
4457 cpuhp_remove_multi_state(CPUHP_NET_MVNETA_DEAD);
4458 cpuhp_remove_multi_state(online_hpstate);
4460 module_exit(mvneta_driver_exit);
4462 MODULE_DESCRIPTION("Marvell NETA Ethernet Driver - www.marvell.com");
4463 MODULE_AUTHOR("Rami Rosen <rosenr@marvell.com>, Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
4464 MODULE_LICENSE("GPL");
4466 module_param(rxq_number, int, S_IRUGO);
4467 module_param(txq_number, int, S_IRUGO);
4469 module_param(rxq_def, int, S_IRUGO);
4470 module_param(rx_copybreak, int, S_IRUGO | S_IWUSR);