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>
21 #include <linux/kernel.h>
22 #include <linux/mbus.h>
23 #include <linux/module.h>
24 #include <linux/netdevice.h>
26 #include <linux/of_address.h>
27 #include <linux/of_irq.h>
28 #include <linux/of_mdio.h>
29 #include <linux/of_net.h>
30 #include <linux/phy.h>
31 #include <linux/phylink.h>
32 #include <linux/platform_device.h>
33 #include <linux/skbuff.h>
35 #include "mvneta_bm.h"
41 #define MVNETA_RXQ_CONFIG_REG(q) (0x1400 + ((q) << 2))
42 #define MVNETA_RXQ_HW_BUF_ALLOC BIT(0)
43 #define MVNETA_RXQ_SHORT_POOL_ID_SHIFT 4
44 #define MVNETA_RXQ_SHORT_POOL_ID_MASK 0x30
45 #define MVNETA_RXQ_LONG_POOL_ID_SHIFT 6
46 #define MVNETA_RXQ_LONG_POOL_ID_MASK 0xc0
47 #define MVNETA_RXQ_PKT_OFFSET_ALL_MASK (0xf << 8)
48 #define MVNETA_RXQ_PKT_OFFSET_MASK(offs) ((offs) << 8)
49 #define MVNETA_RXQ_THRESHOLD_REG(q) (0x14c0 + ((q) << 2))
50 #define MVNETA_RXQ_NON_OCCUPIED(v) ((v) << 16)
51 #define MVNETA_RXQ_BASE_ADDR_REG(q) (0x1480 + ((q) << 2))
52 #define MVNETA_RXQ_SIZE_REG(q) (0x14a0 + ((q) << 2))
53 #define MVNETA_RXQ_BUF_SIZE_SHIFT 19
54 #define MVNETA_RXQ_BUF_SIZE_MASK (0x1fff << 19)
55 #define MVNETA_RXQ_STATUS_REG(q) (0x14e0 + ((q) << 2))
56 #define MVNETA_RXQ_OCCUPIED_ALL_MASK 0x3fff
57 #define MVNETA_RXQ_STATUS_UPDATE_REG(q) (0x1500 + ((q) << 2))
58 #define MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT 16
59 #define MVNETA_RXQ_ADD_NON_OCCUPIED_MAX 255
60 #define MVNETA_PORT_POOL_BUFFER_SZ_REG(pool) (0x1700 + ((pool) << 2))
61 #define MVNETA_PORT_POOL_BUFFER_SZ_SHIFT 3
62 #define MVNETA_PORT_POOL_BUFFER_SZ_MASK 0xfff8
63 #define MVNETA_PORT_RX_RESET 0x1cc0
64 #define MVNETA_PORT_RX_DMA_RESET BIT(0)
65 #define MVNETA_PHY_ADDR 0x2000
66 #define MVNETA_PHY_ADDR_MASK 0x1f
67 #define MVNETA_MBUS_RETRY 0x2010
68 #define MVNETA_UNIT_INTR_CAUSE 0x2080
69 #define MVNETA_UNIT_CONTROL 0x20B0
70 #define MVNETA_PHY_POLLING_ENABLE BIT(1)
71 #define MVNETA_WIN_BASE(w) (0x2200 + ((w) << 3))
72 #define MVNETA_WIN_SIZE(w) (0x2204 + ((w) << 3))
73 #define MVNETA_WIN_REMAP(w) (0x2280 + ((w) << 2))
74 #define MVNETA_BASE_ADDR_ENABLE 0x2290
75 #define MVNETA_ACCESS_PROTECT_ENABLE 0x2294
76 #define MVNETA_PORT_CONFIG 0x2400
77 #define MVNETA_UNI_PROMISC_MODE BIT(0)
78 #define MVNETA_DEF_RXQ(q) ((q) << 1)
79 #define MVNETA_DEF_RXQ_ARP(q) ((q) << 4)
80 #define MVNETA_TX_UNSET_ERR_SUM BIT(12)
81 #define MVNETA_DEF_RXQ_TCP(q) ((q) << 16)
82 #define MVNETA_DEF_RXQ_UDP(q) ((q) << 19)
83 #define MVNETA_DEF_RXQ_BPDU(q) ((q) << 22)
84 #define MVNETA_RX_CSUM_WITH_PSEUDO_HDR BIT(25)
85 #define MVNETA_PORT_CONFIG_DEFL_VALUE(q) (MVNETA_DEF_RXQ(q) | \
86 MVNETA_DEF_RXQ_ARP(q) | \
87 MVNETA_DEF_RXQ_TCP(q) | \
88 MVNETA_DEF_RXQ_UDP(q) | \
89 MVNETA_DEF_RXQ_BPDU(q) | \
90 MVNETA_TX_UNSET_ERR_SUM | \
91 MVNETA_RX_CSUM_WITH_PSEUDO_HDR)
92 #define MVNETA_PORT_CONFIG_EXTEND 0x2404
93 #define MVNETA_MAC_ADDR_LOW 0x2414
94 #define MVNETA_MAC_ADDR_HIGH 0x2418
95 #define MVNETA_SDMA_CONFIG 0x241c
96 #define MVNETA_SDMA_BRST_SIZE_16 4
97 #define MVNETA_RX_BRST_SZ_MASK(burst) ((burst) << 1)
98 #define MVNETA_RX_NO_DATA_SWAP BIT(4)
99 #define MVNETA_TX_NO_DATA_SWAP BIT(5)
100 #define MVNETA_DESC_SWAP BIT(6)
101 #define MVNETA_TX_BRST_SZ_MASK(burst) ((burst) << 22)
102 #define MVNETA_PORT_STATUS 0x2444
103 #define MVNETA_TX_IN_PRGRS BIT(1)
104 #define MVNETA_TX_FIFO_EMPTY BIT(8)
105 #define MVNETA_RX_MIN_FRAME_SIZE 0x247c
106 #define MVNETA_SERDES_CFG 0x24A0
107 #define MVNETA_SGMII_SERDES_PROTO 0x0cc7
108 #define MVNETA_QSGMII_SERDES_PROTO 0x0667
109 #define MVNETA_TYPE_PRIO 0x24bc
110 #define MVNETA_FORCE_UNI BIT(21)
111 #define MVNETA_TXQ_CMD_1 0x24e4
112 #define MVNETA_TXQ_CMD 0x2448
113 #define MVNETA_TXQ_DISABLE_SHIFT 8
114 #define MVNETA_TXQ_ENABLE_MASK 0x000000ff
115 #define MVNETA_RX_DISCARD_FRAME_COUNT 0x2484
116 #define MVNETA_OVERRUN_FRAME_COUNT 0x2488
117 #define MVNETA_GMAC_CLOCK_DIVIDER 0x24f4
118 #define MVNETA_GMAC_1MS_CLOCK_ENABLE BIT(31)
119 #define MVNETA_ACC_MODE 0x2500
120 #define MVNETA_BM_ADDRESS 0x2504
121 #define MVNETA_CPU_MAP(cpu) (0x2540 + ((cpu) << 2))
122 #define MVNETA_CPU_RXQ_ACCESS_ALL_MASK 0x000000ff
123 #define MVNETA_CPU_TXQ_ACCESS_ALL_MASK 0x0000ff00
124 #define MVNETA_CPU_RXQ_ACCESS(rxq) BIT(rxq)
125 #define MVNETA_CPU_TXQ_ACCESS(txq) BIT(txq + 8)
126 #define MVNETA_RXQ_TIME_COAL_REG(q) (0x2580 + ((q) << 2))
128 /* Exception Interrupt Port/Queue Cause register
130 * Their behavior depend of the mapping done using the PCPX2Q
131 * registers. For a given CPU if the bit associated to a queue is not
132 * set, then for the register a read from this CPU will always return
133 * 0 and a write won't do anything
136 #define MVNETA_INTR_NEW_CAUSE 0x25a0
137 #define MVNETA_INTR_NEW_MASK 0x25a4
139 /* bits 0..7 = TXQ SENT, one bit per queue.
140 * bits 8..15 = RXQ OCCUP, one bit per queue.
141 * bits 16..23 = RXQ FREE, one bit per queue.
142 * bit 29 = OLD_REG_SUM, see old reg ?
143 * bit 30 = TX_ERR_SUM, one bit for 4 ports
144 * bit 31 = MISC_SUM, one bit for 4 ports
146 #define MVNETA_TX_INTR_MASK(nr_txqs) (((1 << nr_txqs) - 1) << 0)
147 #define MVNETA_TX_INTR_MASK_ALL (0xff << 0)
148 #define MVNETA_RX_INTR_MASK(nr_rxqs) (((1 << nr_rxqs) - 1) << 8)
149 #define MVNETA_RX_INTR_MASK_ALL (0xff << 8)
150 #define MVNETA_MISCINTR_INTR_MASK BIT(31)
152 #define MVNETA_INTR_OLD_CAUSE 0x25a8
153 #define MVNETA_INTR_OLD_MASK 0x25ac
155 /* Data Path Port/Queue Cause Register */
156 #define MVNETA_INTR_MISC_CAUSE 0x25b0
157 #define MVNETA_INTR_MISC_MASK 0x25b4
159 #define MVNETA_CAUSE_PHY_STATUS_CHANGE BIT(0)
160 #define MVNETA_CAUSE_LINK_CHANGE BIT(1)
161 #define MVNETA_CAUSE_PTP BIT(4)
163 #define MVNETA_CAUSE_INTERNAL_ADDR_ERR BIT(7)
164 #define MVNETA_CAUSE_RX_OVERRUN BIT(8)
165 #define MVNETA_CAUSE_RX_CRC_ERROR BIT(9)
166 #define MVNETA_CAUSE_RX_LARGE_PKT BIT(10)
167 #define MVNETA_CAUSE_TX_UNDERUN BIT(11)
168 #define MVNETA_CAUSE_PRBS_ERR BIT(12)
169 #define MVNETA_CAUSE_PSC_SYNC_CHANGE BIT(13)
170 #define MVNETA_CAUSE_SERDES_SYNC_ERR BIT(14)
172 #define MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT 16
173 #define MVNETA_CAUSE_BMU_ALLOC_ERR_ALL_MASK (0xF << MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT)
174 #define MVNETA_CAUSE_BMU_ALLOC_ERR_MASK(pool) (1 << (MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT + (pool)))
176 #define MVNETA_CAUSE_TXQ_ERROR_SHIFT 24
177 #define MVNETA_CAUSE_TXQ_ERROR_ALL_MASK (0xFF << MVNETA_CAUSE_TXQ_ERROR_SHIFT)
178 #define MVNETA_CAUSE_TXQ_ERROR_MASK(q) (1 << (MVNETA_CAUSE_TXQ_ERROR_SHIFT + (q)))
180 #define MVNETA_INTR_ENABLE 0x25b8
181 #define MVNETA_TXQ_INTR_ENABLE_ALL_MASK 0x0000ff00
182 #define MVNETA_RXQ_INTR_ENABLE_ALL_MASK 0x000000ff
184 #define MVNETA_RXQ_CMD 0x2680
185 #define MVNETA_RXQ_DISABLE_SHIFT 8
186 #define MVNETA_RXQ_ENABLE_MASK 0x000000ff
187 #define MVETH_TXQ_TOKEN_COUNT_REG(q) (0x2700 + ((q) << 4))
188 #define MVETH_TXQ_TOKEN_CFG_REG(q) (0x2704 + ((q) << 4))
189 #define MVNETA_GMAC_CTRL_0 0x2c00
190 #define MVNETA_GMAC_MAX_RX_SIZE_SHIFT 2
191 #define MVNETA_GMAC_MAX_RX_SIZE_MASK 0x7ffc
192 #define MVNETA_GMAC0_PORT_1000BASE_X BIT(1)
193 #define MVNETA_GMAC0_PORT_ENABLE BIT(0)
194 #define MVNETA_GMAC_CTRL_2 0x2c08
195 #define MVNETA_GMAC2_INBAND_AN_ENABLE BIT(0)
196 #define MVNETA_GMAC2_PCS_ENABLE BIT(3)
197 #define MVNETA_GMAC2_PORT_RGMII BIT(4)
198 #define MVNETA_GMAC2_PORT_RESET BIT(6)
199 #define MVNETA_GMAC_STATUS 0x2c10
200 #define MVNETA_GMAC_LINK_UP BIT(0)
201 #define MVNETA_GMAC_SPEED_1000 BIT(1)
202 #define MVNETA_GMAC_SPEED_100 BIT(2)
203 #define MVNETA_GMAC_FULL_DUPLEX BIT(3)
204 #define MVNETA_GMAC_RX_FLOW_CTRL_ENABLE BIT(4)
205 #define MVNETA_GMAC_TX_FLOW_CTRL_ENABLE BIT(5)
206 #define MVNETA_GMAC_RX_FLOW_CTRL_ACTIVE BIT(6)
207 #define MVNETA_GMAC_TX_FLOW_CTRL_ACTIVE BIT(7)
208 #define MVNETA_GMAC_AN_COMPLETE BIT(11)
209 #define MVNETA_GMAC_SYNC_OK BIT(14)
210 #define MVNETA_GMAC_AUTONEG_CONFIG 0x2c0c
211 #define MVNETA_GMAC_FORCE_LINK_DOWN BIT(0)
212 #define MVNETA_GMAC_FORCE_LINK_PASS BIT(1)
213 #define MVNETA_GMAC_INBAND_AN_ENABLE BIT(2)
214 #define MVNETA_GMAC_AN_BYPASS_ENABLE BIT(3)
215 #define MVNETA_GMAC_INBAND_RESTART_AN BIT(4)
216 #define MVNETA_GMAC_CONFIG_MII_SPEED BIT(5)
217 #define MVNETA_GMAC_CONFIG_GMII_SPEED BIT(6)
218 #define MVNETA_GMAC_AN_SPEED_EN BIT(7)
219 #define MVNETA_GMAC_CONFIG_FLOW_CTRL BIT(8)
220 #define MVNETA_GMAC_ADVERT_SYM_FLOW_CTRL BIT(9)
221 #define MVNETA_GMAC_AN_FLOW_CTRL_EN BIT(11)
222 #define MVNETA_GMAC_CONFIG_FULL_DUPLEX BIT(12)
223 #define MVNETA_GMAC_AN_DUPLEX_EN BIT(13)
224 #define MVNETA_MIB_COUNTERS_BASE 0x3000
225 #define MVNETA_MIB_LATE_COLLISION 0x7c
226 #define MVNETA_DA_FILT_SPEC_MCAST 0x3400
227 #define MVNETA_DA_FILT_OTH_MCAST 0x3500
228 #define MVNETA_DA_FILT_UCAST_BASE 0x3600
229 #define MVNETA_TXQ_BASE_ADDR_REG(q) (0x3c00 + ((q) << 2))
230 #define MVNETA_TXQ_SIZE_REG(q) (0x3c20 + ((q) << 2))
231 #define MVNETA_TXQ_SENT_THRESH_ALL_MASK 0x3fff0000
232 #define MVNETA_TXQ_SENT_THRESH_MASK(coal) ((coal) << 16)
233 #define MVNETA_TXQ_UPDATE_REG(q) (0x3c60 + ((q) << 2))
234 #define MVNETA_TXQ_DEC_SENT_SHIFT 16
235 #define MVNETA_TXQ_DEC_SENT_MASK 0xff
236 #define MVNETA_TXQ_STATUS_REG(q) (0x3c40 + ((q) << 2))
237 #define MVNETA_TXQ_SENT_DESC_SHIFT 16
238 #define MVNETA_TXQ_SENT_DESC_MASK 0x3fff0000
239 #define MVNETA_PORT_TX_RESET 0x3cf0
240 #define MVNETA_PORT_TX_DMA_RESET BIT(0)
241 #define MVNETA_TX_MTU 0x3e0c
242 #define MVNETA_TX_TOKEN_SIZE 0x3e14
243 #define MVNETA_TX_TOKEN_SIZE_MAX 0xffffffff
244 #define MVNETA_TXQ_TOKEN_SIZE_REG(q) (0x3e40 + ((q) << 2))
245 #define MVNETA_TXQ_TOKEN_SIZE_MAX 0x7fffffff
247 #define MVNETA_LPI_CTRL_0 0x2cc0
248 #define MVNETA_LPI_CTRL_1 0x2cc4
249 #define MVNETA_LPI_REQUEST_ENABLE BIT(0)
250 #define MVNETA_LPI_CTRL_2 0x2cc8
251 #define MVNETA_LPI_STATUS 0x2ccc
253 #define MVNETA_CAUSE_TXQ_SENT_DESC_ALL_MASK 0xff
255 /* Descriptor ring Macros */
256 #define MVNETA_QUEUE_NEXT_DESC(q, index) \
257 (((index) < (q)->last_desc) ? ((index) + 1) : 0)
259 /* Various constants */
262 #define MVNETA_TXDONE_COAL_PKTS 0 /* interrupt per packet */
263 #define MVNETA_RX_COAL_PKTS 32
264 #define MVNETA_RX_COAL_USEC 100
266 /* The two bytes Marvell header. Either contains a special value used
267 * by Marvell switches when a specific hardware mode is enabled (not
268 * supported by this driver) or is filled automatically by zeroes on
269 * the RX side. Those two bytes being at the front of the Ethernet
270 * header, they allow to have the IP header aligned on a 4 bytes
271 * boundary automatically: the hardware skips those two bytes on its
274 #define MVNETA_MH_SIZE 2
276 #define MVNETA_VLAN_TAG_LEN 4
278 #define MVNETA_TX_CSUM_DEF_SIZE 1600
279 #define MVNETA_TX_CSUM_MAX_SIZE 9800
280 #define MVNETA_ACC_MODE_EXT1 1
281 #define MVNETA_ACC_MODE_EXT2 2
283 #define MVNETA_MAX_DECODE_WIN 6
285 /* Timeout constants */
286 #define MVNETA_TX_DISABLE_TIMEOUT_MSEC 1000
287 #define MVNETA_RX_DISABLE_TIMEOUT_MSEC 1000
288 #define MVNETA_TX_FIFO_EMPTY_TIMEOUT 10000
290 #define MVNETA_TX_MTU_MAX 0x3ffff
292 /* The RSS lookup table actually has 256 entries but we do not use
295 #define MVNETA_RSS_LU_TABLE_SIZE 1
297 /* Max number of Rx descriptors */
298 #define MVNETA_MAX_RXD 128
300 /* Max number of Tx descriptors */
301 #define MVNETA_MAX_TXD 532
303 /* Max number of allowed TCP segments for software TSO */
304 #define MVNETA_MAX_TSO_SEGS 100
306 #define MVNETA_MAX_SKB_DESCS (MVNETA_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
308 /* descriptor aligned size */
309 #define MVNETA_DESC_ALIGNED_SIZE 32
311 /* Number of bytes to be taken into account by HW when putting incoming data
312 * to the buffers. It is needed in case NET_SKB_PAD exceeds maximum packet
313 * offset supported in MVNETA_RXQ_CONFIG_REG(q) registers.
315 #define MVNETA_RX_PKT_OFFSET_CORRECTION 64
317 #define MVNETA_RX_PKT_SIZE(mtu) \
318 ALIGN((mtu) + MVNETA_MH_SIZE + MVNETA_VLAN_TAG_LEN + \
319 ETH_HLEN + ETH_FCS_LEN, \
322 #define IS_TSO_HEADER(txq, addr) \
323 ((addr >= txq->tso_hdrs_phys) && \
324 (addr < txq->tso_hdrs_phys + txq->size * TSO_HEADER_SIZE))
326 #define MVNETA_RX_GET_BM_POOL_ID(rxd) \
327 (((rxd)->status & MVNETA_RXD_BM_POOL_MASK) >> MVNETA_RXD_BM_POOL_SHIFT)
330 ETHTOOL_STAT_EEE_WAKEUP
,
334 struct mvneta_statistic
{
335 unsigned short offset
;
337 const char name
[ETH_GSTRING_LEN
];
344 static const struct mvneta_statistic mvneta_statistics
[] = {
345 { 0x3000, T_REG_64
, "good_octets_received", },
346 { 0x3010, T_REG_32
, "good_frames_received", },
347 { 0x3008, T_REG_32
, "bad_octets_received", },
348 { 0x3014, T_REG_32
, "bad_frames_received", },
349 { 0x3018, T_REG_32
, "broadcast_frames_received", },
350 { 0x301c, T_REG_32
, "multicast_frames_received", },
351 { 0x3050, T_REG_32
, "unrec_mac_control_received", },
352 { 0x3058, T_REG_32
, "good_fc_received", },
353 { 0x305c, T_REG_32
, "bad_fc_received", },
354 { 0x3060, T_REG_32
, "undersize_received", },
355 { 0x3064, T_REG_32
, "fragments_received", },
356 { 0x3068, T_REG_32
, "oversize_received", },
357 { 0x306c, T_REG_32
, "jabber_received", },
358 { 0x3070, T_REG_32
, "mac_receive_error", },
359 { 0x3074, T_REG_32
, "bad_crc_event", },
360 { 0x3078, T_REG_32
, "collision", },
361 { 0x307c, T_REG_32
, "late_collision", },
362 { 0x2484, T_REG_32
, "rx_discard", },
363 { 0x2488, T_REG_32
, "rx_overrun", },
364 { 0x3020, T_REG_32
, "frames_64_octets", },
365 { 0x3024, T_REG_32
, "frames_65_to_127_octets", },
366 { 0x3028, T_REG_32
, "frames_128_to_255_octets", },
367 { 0x302c, T_REG_32
, "frames_256_to_511_octets", },
368 { 0x3030, T_REG_32
, "frames_512_to_1023_octets", },
369 { 0x3034, T_REG_32
, "frames_1024_to_max_octets", },
370 { 0x3038, T_REG_64
, "good_octets_sent", },
371 { 0x3040, T_REG_32
, "good_frames_sent", },
372 { 0x3044, T_REG_32
, "excessive_collision", },
373 { 0x3048, T_REG_32
, "multicast_frames_sent", },
374 { 0x304c, T_REG_32
, "broadcast_frames_sent", },
375 { 0x3054, T_REG_32
, "fc_sent", },
376 { 0x300c, T_REG_32
, "internal_mac_transmit_err", },
377 { ETHTOOL_STAT_EEE_WAKEUP
, T_SW
, "eee_wakeup_errors", },
380 struct mvneta_pcpu_stats
{
381 struct u64_stats_sync syncp
;
388 struct mvneta_pcpu_port
{
389 /* Pointer to the shared port */
390 struct mvneta_port
*pp
;
392 /* Pointer to the CPU-local NAPI struct */
393 struct napi_struct napi
;
395 /* Cause of the previous interrupt */
401 struct mvneta_pcpu_port __percpu
*ports
;
402 struct mvneta_pcpu_stats __percpu
*stats
;
405 unsigned int frag_size
;
407 struct mvneta_rx_queue
*rxqs
;
408 struct mvneta_tx_queue
*txqs
;
409 struct net_device
*dev
;
410 struct hlist_node node_online
;
411 struct hlist_node node_dead
;
413 /* Protect the access to the percpu interrupt registers,
414 * ensuring that the configuration remains coherent.
420 struct napi_struct napi
;
430 phy_interface_t phy_interface
;
431 struct device_node
*dn
;
432 unsigned int tx_csum_limit
;
433 struct phylink
*phylink
;
435 struct mvneta_bm
*bm_priv
;
436 struct mvneta_bm_pool
*pool_long
;
437 struct mvneta_bm_pool
*pool_short
;
444 u64 ethtool_stats
[ARRAY_SIZE(mvneta_statistics
)];
446 u32 indir
[MVNETA_RSS_LU_TABLE_SIZE
];
448 /* Flags for special SoC configurations */
449 bool neta_armada3700
;
450 u16 rx_offset_correction
;
451 const struct mbus_dram_target_info
*dram_target_info
;
454 /* The mvneta_tx_desc and mvneta_rx_desc structures describe the
455 * layout of the transmit and reception DMA descriptors, and their
456 * layout is therefore defined by the hardware design
459 #define MVNETA_TX_L3_OFF_SHIFT 0
460 #define MVNETA_TX_IP_HLEN_SHIFT 8
461 #define MVNETA_TX_L4_UDP BIT(16)
462 #define MVNETA_TX_L3_IP6 BIT(17)
463 #define MVNETA_TXD_IP_CSUM BIT(18)
464 #define MVNETA_TXD_Z_PAD BIT(19)
465 #define MVNETA_TXD_L_DESC BIT(20)
466 #define MVNETA_TXD_F_DESC BIT(21)
467 #define MVNETA_TXD_FLZ_DESC (MVNETA_TXD_Z_PAD | \
468 MVNETA_TXD_L_DESC | \
470 #define MVNETA_TX_L4_CSUM_FULL BIT(30)
471 #define MVNETA_TX_L4_CSUM_NOT BIT(31)
473 #define MVNETA_RXD_ERR_CRC 0x0
474 #define MVNETA_RXD_BM_POOL_SHIFT 13
475 #define MVNETA_RXD_BM_POOL_MASK (BIT(13) | BIT(14))
476 #define MVNETA_RXD_ERR_SUMMARY BIT(16)
477 #define MVNETA_RXD_ERR_OVERRUN BIT(17)
478 #define MVNETA_RXD_ERR_LEN BIT(18)
479 #define MVNETA_RXD_ERR_RESOURCE (BIT(17) | BIT(18))
480 #define MVNETA_RXD_ERR_CODE_MASK (BIT(17) | BIT(18))
481 #define MVNETA_RXD_L3_IP4 BIT(25)
482 #define MVNETA_RXD_FIRST_LAST_DESC (BIT(26) | BIT(27))
483 #define MVNETA_RXD_L4_CSUM_OK BIT(30)
485 #if defined(__LITTLE_ENDIAN)
486 struct mvneta_tx_desc
{
487 u32 command
; /* Options used by HW for packet transmitting.*/
488 u16 reserverd1
; /* csum_l4 (for future use) */
489 u16 data_size
; /* Data size of transmitted packet in bytes */
490 u32 buf_phys_addr
; /* Physical addr of transmitted buffer */
491 u32 reserved2
; /* hw_cmd - (for future use, PMT) */
492 u32 reserved3
[4]; /* Reserved - (for future use) */
495 struct mvneta_rx_desc
{
496 u32 status
; /* Info about received packet */
497 u16 reserved1
; /* pnc_info - (for future use, PnC) */
498 u16 data_size
; /* Size of received packet in bytes */
500 u32 buf_phys_addr
; /* Physical address of the buffer */
501 u32 reserved2
; /* pnc_flow_id (for future use, PnC) */
503 u32 buf_cookie
; /* cookie for access to RX buffer in rx path */
504 u16 reserved3
; /* prefetch_cmd, for future use */
505 u16 reserved4
; /* csum_l4 - (for future use, PnC) */
507 u32 reserved5
; /* pnc_extra PnC (for future use, PnC) */
508 u32 reserved6
; /* hw_cmd (for future use, PnC and HWF) */
511 struct mvneta_tx_desc
{
512 u16 data_size
; /* Data size of transmitted packet in bytes */
513 u16 reserverd1
; /* csum_l4 (for future use) */
514 u32 command
; /* Options used by HW for packet transmitting.*/
515 u32 reserved2
; /* hw_cmd - (for future use, PMT) */
516 u32 buf_phys_addr
; /* Physical addr of transmitted buffer */
517 u32 reserved3
[4]; /* Reserved - (for future use) */
520 struct mvneta_rx_desc
{
521 u16 data_size
; /* Size of received packet in bytes */
522 u16 reserved1
; /* pnc_info - (for future use, PnC) */
523 u32 status
; /* Info about received packet */
525 u32 reserved2
; /* pnc_flow_id (for future use, PnC) */
526 u32 buf_phys_addr
; /* Physical address of the buffer */
528 u16 reserved4
; /* csum_l4 - (for future use, PnC) */
529 u16 reserved3
; /* prefetch_cmd, for future use */
530 u32 buf_cookie
; /* cookie for access to RX buffer in rx path */
532 u32 reserved5
; /* pnc_extra PnC (for future use, PnC) */
533 u32 reserved6
; /* hw_cmd (for future use, PnC and HWF) */
537 struct mvneta_tx_queue
{
538 /* Number of this TX queue, in the range 0-7 */
541 /* Number of TX DMA descriptors in the descriptor ring */
544 /* Number of currently used TX DMA descriptor in the
549 int tx_stop_threshold
;
550 int tx_wake_threshold
;
552 /* Array of transmitted skb */
553 struct sk_buff
**tx_skb
;
555 /* Index of last TX DMA descriptor that was inserted */
558 /* Index of the TX DMA descriptor to be cleaned up */
563 /* Virtual address of the TX DMA descriptors array */
564 struct mvneta_tx_desc
*descs
;
566 /* DMA address of the TX DMA descriptors array */
567 dma_addr_t descs_phys
;
569 /* Index of the last TX DMA descriptor */
572 /* Index of the next TX DMA descriptor to process */
573 int next_desc_to_proc
;
575 /* DMA buffers for TSO headers */
578 /* DMA address of TSO headers */
579 dma_addr_t tso_hdrs_phys
;
581 /* Affinity mask for CPUs*/
582 cpumask_t affinity_mask
;
585 struct mvneta_rx_queue
{
586 /* rx queue number, in the range 0-7 */
589 /* num of rx descriptors in the rx descriptor ring */
592 /* counter of times when mvneta_refill() failed */
598 /* Virtual address of the RX buffer */
599 void **buf_virt_addr
;
601 /* Virtual address of the RX DMA descriptors array */
602 struct mvneta_rx_desc
*descs
;
604 /* DMA address of the RX DMA descriptors array */
605 dma_addr_t descs_phys
;
607 /* Index of the last RX DMA descriptor */
610 /* Index of the next RX DMA descriptor to process */
611 int next_desc_to_proc
;
614 static enum cpuhp_state online_hpstate
;
615 /* The hardware supports eight (8) rx queues, but we are only allowing
616 * the first one to be used. Therefore, let's just allocate one queue.
618 static int rxq_number
= 8;
619 static int txq_number
= 8;
623 static int rx_copybreak __read_mostly
= 256;
625 /* HW BM need that each port be identify by a unique ID */
626 static int global_port_id
;
628 #define MVNETA_DRIVER_NAME "mvneta"
629 #define MVNETA_DRIVER_VERSION "1.0"
631 /* Utility/helper methods */
633 /* Write helper method */
634 static void mvreg_write(struct mvneta_port
*pp
, u32 offset
, u32 data
)
636 writel(data
, pp
->base
+ offset
);
639 /* Read helper method */
640 static u32
mvreg_read(struct mvneta_port
*pp
, u32 offset
)
642 return readl(pp
->base
+ offset
);
645 /* Increment txq get counter */
646 static void mvneta_txq_inc_get(struct mvneta_tx_queue
*txq
)
648 txq
->txq_get_index
++;
649 if (txq
->txq_get_index
== txq
->size
)
650 txq
->txq_get_index
= 0;
653 /* Increment txq put counter */
654 static void mvneta_txq_inc_put(struct mvneta_tx_queue
*txq
)
656 txq
->txq_put_index
++;
657 if (txq
->txq_put_index
== txq
->size
)
658 txq
->txq_put_index
= 0;
662 /* Clear all MIB counters */
663 static void mvneta_mib_counters_clear(struct mvneta_port
*pp
)
668 /* Perform dummy reads from MIB counters */
669 for (i
= 0; i
< MVNETA_MIB_LATE_COLLISION
; i
+= 4)
670 dummy
= mvreg_read(pp
, (MVNETA_MIB_COUNTERS_BASE
+ i
));
671 dummy
= mvreg_read(pp
, MVNETA_RX_DISCARD_FRAME_COUNT
);
672 dummy
= mvreg_read(pp
, MVNETA_OVERRUN_FRAME_COUNT
);
675 /* Get System Network Statistics */
677 mvneta_get_stats64(struct net_device
*dev
,
678 struct rtnl_link_stats64
*stats
)
680 struct mvneta_port
*pp
= netdev_priv(dev
);
684 for_each_possible_cpu(cpu
) {
685 struct mvneta_pcpu_stats
*cpu_stats
;
691 cpu_stats
= per_cpu_ptr(pp
->stats
, cpu
);
693 start
= u64_stats_fetch_begin_irq(&cpu_stats
->syncp
);
694 rx_packets
= cpu_stats
->rx_packets
;
695 rx_bytes
= cpu_stats
->rx_bytes
;
696 tx_packets
= cpu_stats
->tx_packets
;
697 tx_bytes
= cpu_stats
->tx_bytes
;
698 } while (u64_stats_fetch_retry_irq(&cpu_stats
->syncp
, start
));
700 stats
->rx_packets
+= rx_packets
;
701 stats
->rx_bytes
+= rx_bytes
;
702 stats
->tx_packets
+= tx_packets
;
703 stats
->tx_bytes
+= tx_bytes
;
706 stats
->rx_errors
= dev
->stats
.rx_errors
;
707 stats
->rx_dropped
= dev
->stats
.rx_dropped
;
709 stats
->tx_dropped
= dev
->stats
.tx_dropped
;
712 /* Rx descriptors helper methods */
714 /* Checks whether the RX descriptor having this status is both the first
715 * and the last descriptor for the RX packet. Each RX packet is currently
716 * received through a single RX descriptor, so not having each RX
717 * descriptor with its first and last bits set is an error
719 static int mvneta_rxq_desc_is_first_last(u32 status
)
721 return (status
& MVNETA_RXD_FIRST_LAST_DESC
) ==
722 MVNETA_RXD_FIRST_LAST_DESC
;
725 /* Add number of descriptors ready to receive new packets */
726 static void mvneta_rxq_non_occup_desc_add(struct mvneta_port
*pp
,
727 struct mvneta_rx_queue
*rxq
,
730 /* Only MVNETA_RXQ_ADD_NON_OCCUPIED_MAX (255) descriptors can
733 while (ndescs
> MVNETA_RXQ_ADD_NON_OCCUPIED_MAX
) {
734 mvreg_write(pp
, MVNETA_RXQ_STATUS_UPDATE_REG(rxq
->id
),
735 (MVNETA_RXQ_ADD_NON_OCCUPIED_MAX
<<
736 MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT
));
737 ndescs
-= MVNETA_RXQ_ADD_NON_OCCUPIED_MAX
;
740 mvreg_write(pp
, MVNETA_RXQ_STATUS_UPDATE_REG(rxq
->id
),
741 (ndescs
<< MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT
));
744 /* Get number of RX descriptors occupied by received packets */
745 static int mvneta_rxq_busy_desc_num_get(struct mvneta_port
*pp
,
746 struct mvneta_rx_queue
*rxq
)
750 val
= mvreg_read(pp
, MVNETA_RXQ_STATUS_REG(rxq
->id
));
751 return val
& MVNETA_RXQ_OCCUPIED_ALL_MASK
;
754 /* Update num of rx desc called upon return from rx path or
755 * from mvneta_rxq_drop_pkts().
757 static void mvneta_rxq_desc_num_update(struct mvneta_port
*pp
,
758 struct mvneta_rx_queue
*rxq
,
759 int rx_done
, int rx_filled
)
763 if ((rx_done
<= 0xff) && (rx_filled
<= 0xff)) {
765 (rx_filled
<< MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT
);
766 mvreg_write(pp
, MVNETA_RXQ_STATUS_UPDATE_REG(rxq
->id
), val
);
770 /* Only 255 descriptors can be added at once */
771 while ((rx_done
> 0) || (rx_filled
> 0)) {
772 if (rx_done
<= 0xff) {
779 if (rx_filled
<= 0xff) {
780 val
|= rx_filled
<< MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT
;
783 val
|= 0xff << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT
;
786 mvreg_write(pp
, MVNETA_RXQ_STATUS_UPDATE_REG(rxq
->id
), val
);
790 /* Get pointer to next RX descriptor to be processed by SW */
791 static struct mvneta_rx_desc
*
792 mvneta_rxq_next_desc_get(struct mvneta_rx_queue
*rxq
)
794 int rx_desc
= rxq
->next_desc_to_proc
;
796 rxq
->next_desc_to_proc
= MVNETA_QUEUE_NEXT_DESC(rxq
, rx_desc
);
797 prefetch(rxq
->descs
+ rxq
->next_desc_to_proc
);
798 return rxq
->descs
+ rx_desc
;
801 /* Change maximum receive size of the port. */
802 static void mvneta_max_rx_size_set(struct mvneta_port
*pp
, int max_rx_size
)
806 val
= mvreg_read(pp
, MVNETA_GMAC_CTRL_0
);
807 val
&= ~MVNETA_GMAC_MAX_RX_SIZE_MASK
;
808 val
|= ((max_rx_size
- MVNETA_MH_SIZE
) / 2) <<
809 MVNETA_GMAC_MAX_RX_SIZE_SHIFT
;
810 mvreg_write(pp
, MVNETA_GMAC_CTRL_0
, val
);
814 /* Set rx queue offset */
815 static void mvneta_rxq_offset_set(struct mvneta_port
*pp
,
816 struct mvneta_rx_queue
*rxq
,
821 val
= mvreg_read(pp
, MVNETA_RXQ_CONFIG_REG(rxq
->id
));
822 val
&= ~MVNETA_RXQ_PKT_OFFSET_ALL_MASK
;
825 val
|= MVNETA_RXQ_PKT_OFFSET_MASK(offset
>> 3);
826 mvreg_write(pp
, MVNETA_RXQ_CONFIG_REG(rxq
->id
), val
);
830 /* Tx descriptors helper methods */
832 /* Update HW with number of TX descriptors to be sent */
833 static void mvneta_txq_pend_desc_add(struct mvneta_port
*pp
,
834 struct mvneta_tx_queue
*txq
,
839 pend_desc
+= txq
->pending
;
841 /* Only 255 Tx descriptors can be added at once */
843 val
= min(pend_desc
, 255);
844 mvreg_write(pp
, MVNETA_TXQ_UPDATE_REG(txq
->id
), val
);
846 } while (pend_desc
> 0);
850 /* Get pointer to next TX descriptor to be processed (send) by HW */
851 static struct mvneta_tx_desc
*
852 mvneta_txq_next_desc_get(struct mvneta_tx_queue
*txq
)
854 int tx_desc
= txq
->next_desc_to_proc
;
856 txq
->next_desc_to_proc
= MVNETA_QUEUE_NEXT_DESC(txq
, tx_desc
);
857 return txq
->descs
+ tx_desc
;
860 /* Release the last allocated TX descriptor. Useful to handle DMA
861 * mapping failures in the TX path.
863 static void mvneta_txq_desc_put(struct mvneta_tx_queue
*txq
)
865 if (txq
->next_desc_to_proc
== 0)
866 txq
->next_desc_to_proc
= txq
->last_desc
- 1;
868 txq
->next_desc_to_proc
--;
871 /* Set rxq buf size */
872 static void mvneta_rxq_buf_size_set(struct mvneta_port
*pp
,
873 struct mvneta_rx_queue
*rxq
,
878 val
= mvreg_read(pp
, MVNETA_RXQ_SIZE_REG(rxq
->id
));
880 val
&= ~MVNETA_RXQ_BUF_SIZE_MASK
;
881 val
|= ((buf_size
>> 3) << MVNETA_RXQ_BUF_SIZE_SHIFT
);
883 mvreg_write(pp
, MVNETA_RXQ_SIZE_REG(rxq
->id
), val
);
886 /* Disable buffer management (BM) */
887 static void mvneta_rxq_bm_disable(struct mvneta_port
*pp
,
888 struct mvneta_rx_queue
*rxq
)
892 val
= mvreg_read(pp
, MVNETA_RXQ_CONFIG_REG(rxq
->id
));
893 val
&= ~MVNETA_RXQ_HW_BUF_ALLOC
;
894 mvreg_write(pp
, MVNETA_RXQ_CONFIG_REG(rxq
->id
), val
);
897 /* Enable buffer management (BM) */
898 static void mvneta_rxq_bm_enable(struct mvneta_port
*pp
,
899 struct mvneta_rx_queue
*rxq
)
903 val
= mvreg_read(pp
, MVNETA_RXQ_CONFIG_REG(rxq
->id
));
904 val
|= MVNETA_RXQ_HW_BUF_ALLOC
;
905 mvreg_write(pp
, MVNETA_RXQ_CONFIG_REG(rxq
->id
), val
);
908 /* Notify HW about port's assignment of pool for bigger packets */
909 static void mvneta_rxq_long_pool_set(struct mvneta_port
*pp
,
910 struct mvneta_rx_queue
*rxq
)
914 val
= mvreg_read(pp
, MVNETA_RXQ_CONFIG_REG(rxq
->id
));
915 val
&= ~MVNETA_RXQ_LONG_POOL_ID_MASK
;
916 val
|= (pp
->pool_long
->id
<< MVNETA_RXQ_LONG_POOL_ID_SHIFT
);
918 mvreg_write(pp
, MVNETA_RXQ_CONFIG_REG(rxq
->id
), val
);
921 /* Notify HW about port's assignment of pool for smaller packets */
922 static void mvneta_rxq_short_pool_set(struct mvneta_port
*pp
,
923 struct mvneta_rx_queue
*rxq
)
927 val
= mvreg_read(pp
, MVNETA_RXQ_CONFIG_REG(rxq
->id
));
928 val
&= ~MVNETA_RXQ_SHORT_POOL_ID_MASK
;
929 val
|= (pp
->pool_short
->id
<< MVNETA_RXQ_SHORT_POOL_ID_SHIFT
);
931 mvreg_write(pp
, MVNETA_RXQ_CONFIG_REG(rxq
->id
), val
);
934 /* Set port's receive buffer size for assigned BM pool */
935 static inline void mvneta_bm_pool_bufsize_set(struct mvneta_port
*pp
,
941 if (!IS_ALIGNED(buf_size
, 8)) {
942 dev_warn(pp
->dev
->dev
.parent
,
943 "illegal buf_size value %d, round to %d\n",
944 buf_size
, ALIGN(buf_size
, 8));
945 buf_size
= ALIGN(buf_size
, 8);
948 val
= mvreg_read(pp
, MVNETA_PORT_POOL_BUFFER_SZ_REG(pool_id
));
949 val
|= buf_size
& MVNETA_PORT_POOL_BUFFER_SZ_MASK
;
950 mvreg_write(pp
, MVNETA_PORT_POOL_BUFFER_SZ_REG(pool_id
), val
);
953 /* Configure MBUS window in order to enable access BM internal SRAM */
954 static int mvneta_mbus_io_win_set(struct mvneta_port
*pp
, u32 base
, u32 wsize
,
957 u32 win_enable
, win_protect
;
960 win_enable
= mvreg_read(pp
, MVNETA_BASE_ADDR_ENABLE
);
962 if (pp
->bm_win_id
< 0) {
963 /* Find first not occupied window */
964 for (i
= 0; i
< MVNETA_MAX_DECODE_WIN
; i
++) {
965 if (win_enable
& (1 << i
)) {
970 if (i
== MVNETA_MAX_DECODE_WIN
)
976 mvreg_write(pp
, MVNETA_WIN_BASE(i
), 0);
977 mvreg_write(pp
, MVNETA_WIN_SIZE(i
), 0);
980 mvreg_write(pp
, MVNETA_WIN_REMAP(i
), 0);
982 mvreg_write(pp
, MVNETA_WIN_BASE(i
), (base
& 0xffff0000) |
983 (attr
<< 8) | target
);
985 mvreg_write(pp
, MVNETA_WIN_SIZE(i
), (wsize
- 1) & 0xffff0000);
987 win_protect
= mvreg_read(pp
, MVNETA_ACCESS_PROTECT_ENABLE
);
988 win_protect
|= 3 << (2 * i
);
989 mvreg_write(pp
, MVNETA_ACCESS_PROTECT_ENABLE
, win_protect
);
991 win_enable
&= ~(1 << i
);
992 mvreg_write(pp
, MVNETA_BASE_ADDR_ENABLE
, win_enable
);
997 static int mvneta_bm_port_mbus_init(struct mvneta_port
*pp
)
1003 /* Get BM window information */
1004 err
= mvebu_mbus_get_io_win_info(pp
->bm_priv
->bppi_phys_addr
, &wsize
,
1011 /* Open NETA -> BM window */
1012 err
= mvneta_mbus_io_win_set(pp
, pp
->bm_priv
->bppi_phys_addr
, wsize
,
1015 netdev_info(pp
->dev
, "fail to configure mbus window to BM\n");
1021 /* Assign and initialize pools for port. In case of fail
1022 * buffer manager will remain disabled for current port.
1024 static int mvneta_bm_port_init(struct platform_device
*pdev
,
1025 struct mvneta_port
*pp
)
1027 struct device_node
*dn
= pdev
->dev
.of_node
;
1028 u32 long_pool_id
, short_pool_id
;
1030 if (!pp
->neta_armada3700
) {
1033 ret
= mvneta_bm_port_mbus_init(pp
);
1038 if (of_property_read_u32(dn
, "bm,pool-long", &long_pool_id
)) {
1039 netdev_info(pp
->dev
, "missing long pool id\n");
1043 /* Create port's long pool depending on mtu */
1044 pp
->pool_long
= mvneta_bm_pool_use(pp
->bm_priv
, long_pool_id
,
1045 MVNETA_BM_LONG
, pp
->id
,
1046 MVNETA_RX_PKT_SIZE(pp
->dev
->mtu
));
1047 if (!pp
->pool_long
) {
1048 netdev_info(pp
->dev
, "fail to obtain long pool for port\n");
1052 pp
->pool_long
->port_map
|= 1 << pp
->id
;
1054 mvneta_bm_pool_bufsize_set(pp
, pp
->pool_long
->buf_size
,
1057 /* If short pool id is not defined, assume using single pool */
1058 if (of_property_read_u32(dn
, "bm,pool-short", &short_pool_id
))
1059 short_pool_id
= long_pool_id
;
1061 /* Create port's short pool */
1062 pp
->pool_short
= mvneta_bm_pool_use(pp
->bm_priv
, short_pool_id
,
1063 MVNETA_BM_SHORT
, pp
->id
,
1064 MVNETA_BM_SHORT_PKT_SIZE
);
1065 if (!pp
->pool_short
) {
1066 netdev_info(pp
->dev
, "fail to obtain short pool for port\n");
1067 mvneta_bm_pool_destroy(pp
->bm_priv
, pp
->pool_long
, 1 << pp
->id
);
1071 if (short_pool_id
!= long_pool_id
) {
1072 pp
->pool_short
->port_map
|= 1 << pp
->id
;
1073 mvneta_bm_pool_bufsize_set(pp
, pp
->pool_short
->buf_size
,
1074 pp
->pool_short
->id
);
1080 /* Update settings of a pool for bigger packets */
1081 static void mvneta_bm_update_mtu(struct mvneta_port
*pp
, int mtu
)
1083 struct mvneta_bm_pool
*bm_pool
= pp
->pool_long
;
1084 struct hwbm_pool
*hwbm_pool
= &bm_pool
->hwbm_pool
;
1087 /* Release all buffers from long pool */
1088 mvneta_bm_bufs_free(pp
->bm_priv
, bm_pool
, 1 << pp
->id
);
1089 if (hwbm_pool
->buf_num
) {
1090 WARN(1, "cannot free all buffers in pool %d\n",
1095 bm_pool
->pkt_size
= MVNETA_RX_PKT_SIZE(mtu
);
1096 bm_pool
->buf_size
= MVNETA_RX_BUF_SIZE(bm_pool
->pkt_size
);
1097 hwbm_pool
->frag_size
= SKB_DATA_ALIGN(sizeof(struct skb_shared_info
)) +
1098 SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(bm_pool
->pkt_size
));
1100 /* Fill entire long pool */
1101 num
= hwbm_pool_add(hwbm_pool
, hwbm_pool
->size
, GFP_ATOMIC
);
1102 if (num
!= hwbm_pool
->size
) {
1103 WARN(1, "pool %d: %d of %d allocated\n",
1104 bm_pool
->id
, num
, hwbm_pool
->size
);
1107 mvneta_bm_pool_bufsize_set(pp
, bm_pool
->buf_size
, bm_pool
->id
);
1112 mvneta_bm_pool_destroy(pp
->bm_priv
, pp
->pool_long
, 1 << pp
->id
);
1113 mvneta_bm_pool_destroy(pp
->bm_priv
, pp
->pool_short
, 1 << pp
->id
);
1116 mvreg_write(pp
, MVNETA_ACC_MODE
, MVNETA_ACC_MODE_EXT1
);
1117 netdev_info(pp
->dev
, "fail to update MTU, fall back to software BM\n");
1120 /* Start the Ethernet port RX and TX activity */
1121 static void mvneta_port_up(struct mvneta_port
*pp
)
1126 /* Enable all initialized TXs. */
1128 for (queue
= 0; queue
< txq_number
; queue
++) {
1129 struct mvneta_tx_queue
*txq
= &pp
->txqs
[queue
];
1131 q_map
|= (1 << queue
);
1133 mvreg_write(pp
, MVNETA_TXQ_CMD
, q_map
);
1135 /* Enable all initialized RXQs. */
1136 for (queue
= 0; queue
< rxq_number
; queue
++) {
1137 struct mvneta_rx_queue
*rxq
= &pp
->rxqs
[queue
];
1140 q_map
|= (1 << queue
);
1142 mvreg_write(pp
, MVNETA_RXQ_CMD
, q_map
);
1145 /* Stop the Ethernet port activity */
1146 static void mvneta_port_down(struct mvneta_port
*pp
)
1151 /* Stop Rx port activity. Check port Rx activity. */
1152 val
= mvreg_read(pp
, MVNETA_RXQ_CMD
) & MVNETA_RXQ_ENABLE_MASK
;
1154 /* Issue stop command for active channels only */
1156 mvreg_write(pp
, MVNETA_RXQ_CMD
,
1157 val
<< MVNETA_RXQ_DISABLE_SHIFT
);
1159 /* Wait for all Rx activity to terminate. */
1162 if (count
++ >= MVNETA_RX_DISABLE_TIMEOUT_MSEC
) {
1163 netdev_warn(pp
->dev
,
1164 "TIMEOUT for RX stopped ! rx_queue_cmd: 0x%08x\n",
1170 val
= mvreg_read(pp
, MVNETA_RXQ_CMD
);
1171 } while (val
& MVNETA_RXQ_ENABLE_MASK
);
1173 /* Stop Tx port activity. Check port Tx activity. Issue stop
1174 * command for active channels only
1176 val
= (mvreg_read(pp
, MVNETA_TXQ_CMD
)) & MVNETA_TXQ_ENABLE_MASK
;
1179 mvreg_write(pp
, MVNETA_TXQ_CMD
,
1180 (val
<< MVNETA_TXQ_DISABLE_SHIFT
));
1182 /* Wait for all Tx activity to terminate. */
1185 if (count
++ >= MVNETA_TX_DISABLE_TIMEOUT_MSEC
) {
1186 netdev_warn(pp
->dev
,
1187 "TIMEOUT for TX stopped status=0x%08x\n",
1193 /* Check TX Command reg that all Txqs are stopped */
1194 val
= mvreg_read(pp
, MVNETA_TXQ_CMD
);
1196 } while (val
& MVNETA_TXQ_ENABLE_MASK
);
1198 /* Double check to verify that TX FIFO is empty */
1201 if (count
++ >= MVNETA_TX_FIFO_EMPTY_TIMEOUT
) {
1202 netdev_warn(pp
->dev
,
1203 "TX FIFO empty timeout status=0x%08x\n",
1209 val
= mvreg_read(pp
, MVNETA_PORT_STATUS
);
1210 } while (!(val
& MVNETA_TX_FIFO_EMPTY
) &&
1211 (val
& MVNETA_TX_IN_PRGRS
));
1216 /* Enable the port by setting the port enable bit of the MAC control register */
1217 static void mvneta_port_enable(struct mvneta_port
*pp
)
1222 val
= mvreg_read(pp
, MVNETA_GMAC_CTRL_0
);
1223 val
|= MVNETA_GMAC0_PORT_ENABLE
;
1224 mvreg_write(pp
, MVNETA_GMAC_CTRL_0
, val
);
1227 /* Disable the port and wait for about 200 usec before retuning */
1228 static void mvneta_port_disable(struct mvneta_port
*pp
)
1232 /* Reset the Enable bit in the Serial Control Register */
1233 val
= mvreg_read(pp
, MVNETA_GMAC_CTRL_0
);
1234 val
&= ~MVNETA_GMAC0_PORT_ENABLE
;
1235 mvreg_write(pp
, MVNETA_GMAC_CTRL_0
, val
);
1240 /* Multicast tables methods */
1242 /* Set all entries in Unicast MAC Table; queue==-1 means reject all */
1243 static void mvneta_set_ucast_table(struct mvneta_port
*pp
, int queue
)
1251 val
= 0x1 | (queue
<< 1);
1252 val
|= (val
<< 24) | (val
<< 16) | (val
<< 8);
1255 for (offset
= 0; offset
<= 0xc; offset
+= 4)
1256 mvreg_write(pp
, MVNETA_DA_FILT_UCAST_BASE
+ offset
, val
);
1259 /* Set all entries in Special Multicast MAC Table; queue==-1 means reject all */
1260 static void mvneta_set_special_mcast_table(struct mvneta_port
*pp
, int queue
)
1268 val
= 0x1 | (queue
<< 1);
1269 val
|= (val
<< 24) | (val
<< 16) | (val
<< 8);
1272 for (offset
= 0; offset
<= 0xfc; offset
+= 4)
1273 mvreg_write(pp
, MVNETA_DA_FILT_SPEC_MCAST
+ offset
, val
);
1277 /* Set all entries in Other Multicast MAC Table. queue==-1 means reject all */
1278 static void mvneta_set_other_mcast_table(struct mvneta_port
*pp
, int queue
)
1284 memset(pp
->mcast_count
, 0, sizeof(pp
->mcast_count
));
1287 memset(pp
->mcast_count
, 1, sizeof(pp
->mcast_count
));
1288 val
= 0x1 | (queue
<< 1);
1289 val
|= (val
<< 24) | (val
<< 16) | (val
<< 8);
1292 for (offset
= 0; offset
<= 0xfc; offset
+= 4)
1293 mvreg_write(pp
, MVNETA_DA_FILT_OTH_MCAST
+ offset
, val
);
1296 static void mvneta_percpu_unmask_interrupt(void *arg
)
1298 struct mvneta_port
*pp
= arg
;
1300 /* All the queue are unmasked, but actually only the ones
1301 * mapped to this CPU will be unmasked
1303 mvreg_write(pp
, MVNETA_INTR_NEW_MASK
,
1304 MVNETA_RX_INTR_MASK_ALL
|
1305 MVNETA_TX_INTR_MASK_ALL
|
1306 MVNETA_MISCINTR_INTR_MASK
);
1309 static void mvneta_percpu_mask_interrupt(void *arg
)
1311 struct mvneta_port
*pp
= arg
;
1313 /* All the queue are masked, but actually only the ones
1314 * mapped to this CPU will be masked
1316 mvreg_write(pp
, MVNETA_INTR_NEW_MASK
, 0);
1317 mvreg_write(pp
, MVNETA_INTR_OLD_MASK
, 0);
1318 mvreg_write(pp
, MVNETA_INTR_MISC_MASK
, 0);
1321 static void mvneta_percpu_clear_intr_cause(void *arg
)
1323 struct mvneta_port
*pp
= arg
;
1325 /* All the queue are cleared, but actually only the ones
1326 * mapped to this CPU will be cleared
1328 mvreg_write(pp
, MVNETA_INTR_NEW_CAUSE
, 0);
1329 mvreg_write(pp
, MVNETA_INTR_MISC_CAUSE
, 0);
1330 mvreg_write(pp
, MVNETA_INTR_OLD_CAUSE
, 0);
1333 /* This method sets defaults to the NETA port:
1334 * Clears interrupt Cause and Mask registers.
1335 * Clears all MAC tables.
1336 * Sets defaults to all registers.
1337 * Resets RX and TX descriptor rings.
1339 * This method can be called after mvneta_port_down() to return the port
1340 * settings to defaults.
1342 static void mvneta_defaults_set(struct mvneta_port
*pp
)
1347 int max_cpu
= num_present_cpus();
1349 /* Clear all Cause registers */
1350 on_each_cpu(mvneta_percpu_clear_intr_cause
, pp
, true);
1352 /* Mask all interrupts */
1353 on_each_cpu(mvneta_percpu_mask_interrupt
, pp
, true);
1354 mvreg_write(pp
, MVNETA_INTR_ENABLE
, 0);
1356 /* Enable MBUS Retry bit16 */
1357 mvreg_write(pp
, MVNETA_MBUS_RETRY
, 0x20);
1359 /* Set CPU queue access map. CPUs are assigned to the RX and
1360 * TX queues modulo their number. If there is only one TX
1361 * queue then it is assigned to the CPU associated to the
1364 for_each_present_cpu(cpu
) {
1365 int rxq_map
= 0, txq_map
= 0;
1367 if (!pp
->neta_armada3700
) {
1368 for (rxq
= 0; rxq
< rxq_number
; rxq
++)
1369 if ((rxq
% max_cpu
) == cpu
)
1370 rxq_map
|= MVNETA_CPU_RXQ_ACCESS(rxq
);
1372 for (txq
= 0; txq
< txq_number
; txq
++)
1373 if ((txq
% max_cpu
) == cpu
)
1374 txq_map
|= MVNETA_CPU_TXQ_ACCESS(txq
);
1376 /* With only one TX queue we configure a special case
1377 * which will allow to get all the irq on a single
1380 if (txq_number
== 1)
1381 txq_map
= (cpu
== pp
->rxq_def
) ?
1382 MVNETA_CPU_TXQ_ACCESS(1) : 0;
1385 txq_map
= MVNETA_CPU_TXQ_ACCESS_ALL_MASK
;
1386 rxq_map
= MVNETA_CPU_RXQ_ACCESS_ALL_MASK
;
1389 mvreg_write(pp
, MVNETA_CPU_MAP(cpu
), rxq_map
| txq_map
);
1392 /* Reset RX and TX DMAs */
1393 mvreg_write(pp
, MVNETA_PORT_RX_RESET
, MVNETA_PORT_RX_DMA_RESET
);
1394 mvreg_write(pp
, MVNETA_PORT_TX_RESET
, MVNETA_PORT_TX_DMA_RESET
);
1396 /* Disable Legacy WRR, Disable EJP, Release from reset */
1397 mvreg_write(pp
, MVNETA_TXQ_CMD_1
, 0);
1398 for (queue
= 0; queue
< txq_number
; queue
++) {
1399 mvreg_write(pp
, MVETH_TXQ_TOKEN_COUNT_REG(queue
), 0);
1400 mvreg_write(pp
, MVETH_TXQ_TOKEN_CFG_REG(queue
), 0);
1403 mvreg_write(pp
, MVNETA_PORT_TX_RESET
, 0);
1404 mvreg_write(pp
, MVNETA_PORT_RX_RESET
, 0);
1406 /* Set Port Acceleration Mode */
1408 /* HW buffer management + legacy parser */
1409 val
= MVNETA_ACC_MODE_EXT2
;
1411 /* SW buffer management + legacy parser */
1412 val
= MVNETA_ACC_MODE_EXT1
;
1413 mvreg_write(pp
, MVNETA_ACC_MODE
, val
);
1416 mvreg_write(pp
, MVNETA_BM_ADDRESS
, pp
->bm_priv
->bppi_phys_addr
);
1418 /* Update val of portCfg register accordingly with all RxQueue types */
1419 val
= MVNETA_PORT_CONFIG_DEFL_VALUE(pp
->rxq_def
);
1420 mvreg_write(pp
, MVNETA_PORT_CONFIG
, val
);
1423 mvreg_write(pp
, MVNETA_PORT_CONFIG_EXTEND
, val
);
1424 mvreg_write(pp
, MVNETA_RX_MIN_FRAME_SIZE
, 64);
1426 /* Build PORT_SDMA_CONFIG_REG */
1429 /* Default burst size */
1430 val
|= MVNETA_TX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16
);
1431 val
|= MVNETA_RX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16
);
1432 val
|= MVNETA_RX_NO_DATA_SWAP
| MVNETA_TX_NO_DATA_SWAP
;
1434 #if defined(__BIG_ENDIAN)
1435 val
|= MVNETA_DESC_SWAP
;
1438 /* Assign port SDMA configuration */
1439 mvreg_write(pp
, MVNETA_SDMA_CONFIG
, val
);
1441 /* Disable PHY polling in hardware, since we're using the
1442 * kernel phylib to do this.
1444 val
= mvreg_read(pp
, MVNETA_UNIT_CONTROL
);
1445 val
&= ~MVNETA_PHY_POLLING_ENABLE
;
1446 mvreg_write(pp
, MVNETA_UNIT_CONTROL
, val
);
1448 mvneta_set_ucast_table(pp
, -1);
1449 mvneta_set_special_mcast_table(pp
, -1);
1450 mvneta_set_other_mcast_table(pp
, -1);
1452 /* Set port interrupt enable register - default enable all */
1453 mvreg_write(pp
, MVNETA_INTR_ENABLE
,
1454 (MVNETA_RXQ_INTR_ENABLE_ALL_MASK
1455 | MVNETA_TXQ_INTR_ENABLE_ALL_MASK
));
1457 mvneta_mib_counters_clear(pp
);
1460 /* Set max sizes for tx queues */
1461 static void mvneta_txq_max_tx_size_set(struct mvneta_port
*pp
, int max_tx_size
)
1467 mtu
= max_tx_size
* 8;
1468 if (mtu
> MVNETA_TX_MTU_MAX
)
1469 mtu
= MVNETA_TX_MTU_MAX
;
1472 val
= mvreg_read(pp
, MVNETA_TX_MTU
);
1473 val
&= ~MVNETA_TX_MTU_MAX
;
1475 mvreg_write(pp
, MVNETA_TX_MTU
, val
);
1477 /* TX token size and all TXQs token size must be larger that MTU */
1478 val
= mvreg_read(pp
, MVNETA_TX_TOKEN_SIZE
);
1480 size
= val
& MVNETA_TX_TOKEN_SIZE_MAX
;
1483 val
&= ~MVNETA_TX_TOKEN_SIZE_MAX
;
1485 mvreg_write(pp
, MVNETA_TX_TOKEN_SIZE
, val
);
1487 for (queue
= 0; queue
< txq_number
; queue
++) {
1488 val
= mvreg_read(pp
, MVNETA_TXQ_TOKEN_SIZE_REG(queue
));
1490 size
= val
& MVNETA_TXQ_TOKEN_SIZE_MAX
;
1493 val
&= ~MVNETA_TXQ_TOKEN_SIZE_MAX
;
1495 mvreg_write(pp
, MVNETA_TXQ_TOKEN_SIZE_REG(queue
), val
);
1500 /* Set unicast address */
1501 static void mvneta_set_ucast_addr(struct mvneta_port
*pp
, u8 last_nibble
,
1504 unsigned int unicast_reg
;
1505 unsigned int tbl_offset
;
1506 unsigned int reg_offset
;
1508 /* Locate the Unicast table entry */
1509 last_nibble
= (0xf & last_nibble
);
1511 /* offset from unicast tbl base */
1512 tbl_offset
= (last_nibble
/ 4) * 4;
1514 /* offset within the above reg */
1515 reg_offset
= last_nibble
% 4;
1517 unicast_reg
= mvreg_read(pp
, (MVNETA_DA_FILT_UCAST_BASE
+ tbl_offset
));
1520 /* Clear accepts frame bit at specified unicast DA tbl entry */
1521 unicast_reg
&= ~(0xff << (8 * reg_offset
));
1523 unicast_reg
&= ~(0xff << (8 * reg_offset
));
1524 unicast_reg
|= ((0x01 | (queue
<< 1)) << (8 * reg_offset
));
1527 mvreg_write(pp
, (MVNETA_DA_FILT_UCAST_BASE
+ tbl_offset
), unicast_reg
);
1530 /* Set mac address */
1531 static void mvneta_mac_addr_set(struct mvneta_port
*pp
, unsigned char *addr
,
1538 mac_l
= (addr
[4] << 8) | (addr
[5]);
1539 mac_h
= (addr
[0] << 24) | (addr
[1] << 16) |
1540 (addr
[2] << 8) | (addr
[3] << 0);
1542 mvreg_write(pp
, MVNETA_MAC_ADDR_LOW
, mac_l
);
1543 mvreg_write(pp
, MVNETA_MAC_ADDR_HIGH
, mac_h
);
1546 /* Accept frames of this address */
1547 mvneta_set_ucast_addr(pp
, addr
[5], queue
);
1550 /* Set the number of packets that will be received before RX interrupt
1551 * will be generated by HW.
1553 static void mvneta_rx_pkts_coal_set(struct mvneta_port
*pp
,
1554 struct mvneta_rx_queue
*rxq
, u32 value
)
1556 mvreg_write(pp
, MVNETA_RXQ_THRESHOLD_REG(rxq
->id
),
1557 value
| MVNETA_RXQ_NON_OCCUPIED(0));
1558 rxq
->pkts_coal
= value
;
1561 /* Set the time delay in usec before RX interrupt will be generated by
1564 static void mvneta_rx_time_coal_set(struct mvneta_port
*pp
,
1565 struct mvneta_rx_queue
*rxq
, u32 value
)
1568 unsigned long clk_rate
;
1570 clk_rate
= clk_get_rate(pp
->clk
);
1571 val
= (clk_rate
/ 1000000) * value
;
1573 mvreg_write(pp
, MVNETA_RXQ_TIME_COAL_REG(rxq
->id
), val
);
1574 rxq
->time_coal
= value
;
1577 /* Set threshold for TX_DONE pkts coalescing */
1578 static void mvneta_tx_done_pkts_coal_set(struct mvneta_port
*pp
,
1579 struct mvneta_tx_queue
*txq
, u32 value
)
1583 val
= mvreg_read(pp
, MVNETA_TXQ_SIZE_REG(txq
->id
));
1585 val
&= ~MVNETA_TXQ_SENT_THRESH_ALL_MASK
;
1586 val
|= MVNETA_TXQ_SENT_THRESH_MASK(value
);
1588 mvreg_write(pp
, MVNETA_TXQ_SIZE_REG(txq
->id
), val
);
1590 txq
->done_pkts_coal
= value
;
1593 /* Handle rx descriptor fill by setting buf_cookie and buf_phys_addr */
1594 static void mvneta_rx_desc_fill(struct mvneta_rx_desc
*rx_desc
,
1595 u32 phys_addr
, void *virt_addr
,
1596 struct mvneta_rx_queue
*rxq
)
1600 rx_desc
->buf_phys_addr
= phys_addr
;
1601 i
= rx_desc
- rxq
->descs
;
1602 rxq
->buf_virt_addr
[i
] = virt_addr
;
1605 /* Decrement sent descriptors counter */
1606 static void mvneta_txq_sent_desc_dec(struct mvneta_port
*pp
,
1607 struct mvneta_tx_queue
*txq
,
1612 /* Only 255 TX descriptors can be updated at once */
1613 while (sent_desc
> 0xff) {
1614 val
= 0xff << MVNETA_TXQ_DEC_SENT_SHIFT
;
1615 mvreg_write(pp
, MVNETA_TXQ_UPDATE_REG(txq
->id
), val
);
1616 sent_desc
= sent_desc
- 0xff;
1619 val
= sent_desc
<< MVNETA_TXQ_DEC_SENT_SHIFT
;
1620 mvreg_write(pp
, MVNETA_TXQ_UPDATE_REG(txq
->id
), val
);
1623 /* Get number of TX descriptors already sent by HW */
1624 static int mvneta_txq_sent_desc_num_get(struct mvneta_port
*pp
,
1625 struct mvneta_tx_queue
*txq
)
1630 val
= mvreg_read(pp
, MVNETA_TXQ_STATUS_REG(txq
->id
));
1631 sent_desc
= (val
& MVNETA_TXQ_SENT_DESC_MASK
) >>
1632 MVNETA_TXQ_SENT_DESC_SHIFT
;
1637 /* Get number of sent descriptors and decrement counter.
1638 * The number of sent descriptors is returned.
1640 static int mvneta_txq_sent_desc_proc(struct mvneta_port
*pp
,
1641 struct mvneta_tx_queue
*txq
)
1645 /* Get number of sent descriptors */
1646 sent_desc
= mvneta_txq_sent_desc_num_get(pp
, txq
);
1648 /* Decrement sent descriptors counter */
1650 mvneta_txq_sent_desc_dec(pp
, txq
, sent_desc
);
1655 /* Set TXQ descriptors fields relevant for CSUM calculation */
1656 static u32
mvneta_txq_desc_csum(int l3_offs
, int l3_proto
,
1657 int ip_hdr_len
, int l4_proto
)
1661 /* Fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
1662 * G_L4_chk, L4_type; required only for checksum
1665 command
= l3_offs
<< MVNETA_TX_L3_OFF_SHIFT
;
1666 command
|= ip_hdr_len
<< MVNETA_TX_IP_HLEN_SHIFT
;
1668 if (l3_proto
== htons(ETH_P_IP
))
1669 command
|= MVNETA_TXD_IP_CSUM
;
1671 command
|= MVNETA_TX_L3_IP6
;
1673 if (l4_proto
== IPPROTO_TCP
)
1674 command
|= MVNETA_TX_L4_CSUM_FULL
;
1675 else if (l4_proto
== IPPROTO_UDP
)
1676 command
|= MVNETA_TX_L4_UDP
| MVNETA_TX_L4_CSUM_FULL
;
1678 command
|= MVNETA_TX_L4_CSUM_NOT
;
1684 /* Display more error info */
1685 static void mvneta_rx_error(struct mvneta_port
*pp
,
1686 struct mvneta_rx_desc
*rx_desc
)
1688 u32 status
= rx_desc
->status
;
1690 if (!mvneta_rxq_desc_is_first_last(status
)) {
1692 "bad rx status %08x (buffer oversize), size=%d\n",
1693 status
, rx_desc
->data_size
);
1697 switch (status
& MVNETA_RXD_ERR_CODE_MASK
) {
1698 case MVNETA_RXD_ERR_CRC
:
1699 netdev_err(pp
->dev
, "bad rx status %08x (crc error), size=%d\n",
1700 status
, rx_desc
->data_size
);
1702 case MVNETA_RXD_ERR_OVERRUN
:
1703 netdev_err(pp
->dev
, "bad rx status %08x (overrun error), size=%d\n",
1704 status
, rx_desc
->data_size
);
1706 case MVNETA_RXD_ERR_LEN
:
1707 netdev_err(pp
->dev
, "bad rx status %08x (max frame length error), size=%d\n",
1708 status
, rx_desc
->data_size
);
1710 case MVNETA_RXD_ERR_RESOURCE
:
1711 netdev_err(pp
->dev
, "bad rx status %08x (resource error), size=%d\n",
1712 status
, rx_desc
->data_size
);
1717 /* Handle RX checksum offload based on the descriptor's status */
1718 static void mvneta_rx_csum(struct mvneta_port
*pp
, u32 status
,
1719 struct sk_buff
*skb
)
1721 if ((status
& MVNETA_RXD_L3_IP4
) &&
1722 (status
& MVNETA_RXD_L4_CSUM_OK
)) {
1724 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
1728 skb
->ip_summed
= CHECKSUM_NONE
;
1731 /* Return tx queue pointer (find last set bit) according to <cause> returned
1732 * form tx_done reg. <cause> must not be null. The return value is always a
1733 * valid queue for matching the first one found in <cause>.
1735 static struct mvneta_tx_queue
*mvneta_tx_done_policy(struct mvneta_port
*pp
,
1738 int queue
= fls(cause
) - 1;
1740 return &pp
->txqs
[queue
];
1743 /* Free tx queue skbuffs */
1744 static void mvneta_txq_bufs_free(struct mvneta_port
*pp
,
1745 struct mvneta_tx_queue
*txq
, int num
,
1746 struct netdev_queue
*nq
)
1748 unsigned int bytes_compl
= 0, pkts_compl
= 0;
1751 for (i
= 0; i
< num
; i
++) {
1752 struct mvneta_tx_desc
*tx_desc
= txq
->descs
+
1754 struct sk_buff
*skb
= txq
->tx_skb
[txq
->txq_get_index
];
1757 bytes_compl
+= skb
->len
;
1761 mvneta_txq_inc_get(txq
);
1763 if (!IS_TSO_HEADER(txq
, tx_desc
->buf_phys_addr
))
1764 dma_unmap_single(pp
->dev
->dev
.parent
,
1765 tx_desc
->buf_phys_addr
,
1766 tx_desc
->data_size
, DMA_TO_DEVICE
);
1769 dev_kfree_skb_any(skb
);
1772 netdev_tx_completed_queue(nq
, pkts_compl
, bytes_compl
);
1775 /* Handle end of transmission */
1776 static void mvneta_txq_done(struct mvneta_port
*pp
,
1777 struct mvneta_tx_queue
*txq
)
1779 struct netdev_queue
*nq
= netdev_get_tx_queue(pp
->dev
, txq
->id
);
1782 tx_done
= mvneta_txq_sent_desc_proc(pp
, txq
);
1786 mvneta_txq_bufs_free(pp
, txq
, tx_done
, nq
);
1788 txq
->count
-= tx_done
;
1790 if (netif_tx_queue_stopped(nq
)) {
1791 if (txq
->count
<= txq
->tx_wake_threshold
)
1792 netif_tx_wake_queue(nq
);
1796 void *mvneta_frag_alloc(unsigned int frag_size
)
1798 if (likely(frag_size
<= PAGE_SIZE
))
1799 return netdev_alloc_frag(frag_size
);
1801 return kmalloc(frag_size
, GFP_ATOMIC
);
1803 EXPORT_SYMBOL_GPL(mvneta_frag_alloc
);
1805 void mvneta_frag_free(unsigned int frag_size
, void *data
)
1807 if (likely(frag_size
<= PAGE_SIZE
))
1808 skb_free_frag(data
);
1812 EXPORT_SYMBOL_GPL(mvneta_frag_free
);
1814 /* Refill processing for SW buffer management */
1815 static int mvneta_rx_refill(struct mvneta_port
*pp
,
1816 struct mvneta_rx_desc
*rx_desc
,
1817 struct mvneta_rx_queue
*rxq
)
1820 dma_addr_t phys_addr
;
1823 data
= mvneta_frag_alloc(pp
->frag_size
);
1827 phys_addr
= dma_map_single(pp
->dev
->dev
.parent
, data
,
1828 MVNETA_RX_BUF_SIZE(pp
->pkt_size
),
1830 if (unlikely(dma_mapping_error(pp
->dev
->dev
.parent
, phys_addr
))) {
1831 mvneta_frag_free(pp
->frag_size
, data
);
1835 phys_addr
+= pp
->rx_offset_correction
;
1836 mvneta_rx_desc_fill(rx_desc
, phys_addr
, data
, rxq
);
1840 /* Handle tx checksum */
1841 static u32
mvneta_skb_tx_csum(struct mvneta_port
*pp
, struct sk_buff
*skb
)
1843 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
1845 __be16 l3_proto
= vlan_get_protocol(skb
);
1848 if (l3_proto
== htons(ETH_P_IP
)) {
1849 struct iphdr
*ip4h
= ip_hdr(skb
);
1851 /* Calculate IPv4 checksum and L4 checksum */
1852 ip_hdr_len
= ip4h
->ihl
;
1853 l4_proto
= ip4h
->protocol
;
1854 } else if (l3_proto
== htons(ETH_P_IPV6
)) {
1855 struct ipv6hdr
*ip6h
= ipv6_hdr(skb
);
1857 /* Read l4_protocol from one of IPv6 extra headers */
1858 if (skb_network_header_len(skb
) > 0)
1859 ip_hdr_len
= (skb_network_header_len(skb
) >> 2);
1860 l4_proto
= ip6h
->nexthdr
;
1862 return MVNETA_TX_L4_CSUM_NOT
;
1864 return mvneta_txq_desc_csum(skb_network_offset(skb
),
1865 l3_proto
, ip_hdr_len
, l4_proto
);
1868 return MVNETA_TX_L4_CSUM_NOT
;
1871 /* Drop packets received by the RXQ and free buffers */
1872 static void mvneta_rxq_drop_pkts(struct mvneta_port
*pp
,
1873 struct mvneta_rx_queue
*rxq
)
1877 rx_done
= mvneta_rxq_busy_desc_num_get(pp
, rxq
);
1879 mvneta_rxq_desc_num_update(pp
, rxq
, rx_done
, rx_done
);
1882 for (i
= 0; i
< rx_done
; i
++) {
1883 struct mvneta_rx_desc
*rx_desc
=
1884 mvneta_rxq_next_desc_get(rxq
);
1885 u8 pool_id
= MVNETA_RX_GET_BM_POOL_ID(rx_desc
);
1886 struct mvneta_bm_pool
*bm_pool
;
1888 bm_pool
= &pp
->bm_priv
->bm_pools
[pool_id
];
1889 /* Return dropped buffer to the pool */
1890 mvneta_bm_pool_put_bp(pp
->bm_priv
, bm_pool
,
1891 rx_desc
->buf_phys_addr
);
1896 for (i
= 0; i
< rxq
->size
; i
++) {
1897 struct mvneta_rx_desc
*rx_desc
= rxq
->descs
+ i
;
1898 void *data
= rxq
->buf_virt_addr
[i
];
1900 dma_unmap_single(pp
->dev
->dev
.parent
, rx_desc
->buf_phys_addr
,
1901 MVNETA_RX_BUF_SIZE(pp
->pkt_size
), DMA_FROM_DEVICE
);
1902 mvneta_frag_free(pp
->frag_size
, data
);
1906 /* Main rx processing when using software buffer management */
1907 static int mvneta_rx_swbm(struct mvneta_port
*pp
, int rx_todo
,
1908 struct mvneta_rx_queue
*rxq
)
1910 struct mvneta_pcpu_port
*port
= this_cpu_ptr(pp
->ports
);
1911 struct net_device
*dev
= pp
->dev
;
1916 /* Get number of received packets */
1917 rx_done
= mvneta_rxq_busy_desc_num_get(pp
, rxq
);
1919 if (rx_todo
> rx_done
)
1924 /* Fairness NAPI loop */
1925 while (rx_done
< rx_todo
) {
1926 struct mvneta_rx_desc
*rx_desc
= mvneta_rxq_next_desc_get(rxq
);
1927 struct sk_buff
*skb
;
1928 unsigned char *data
;
1929 dma_addr_t phys_addr
;
1930 u32 rx_status
, frag_size
;
1931 int rx_bytes
, err
, index
;
1934 rx_status
= rx_desc
->status
;
1935 rx_bytes
= rx_desc
->data_size
- (ETH_FCS_LEN
+ MVNETA_MH_SIZE
);
1936 index
= rx_desc
- rxq
->descs
;
1937 data
= rxq
->buf_virt_addr
[index
];
1938 phys_addr
= rx_desc
->buf_phys_addr
;
1940 if (!mvneta_rxq_desc_is_first_last(rx_status
) ||
1941 (rx_status
& MVNETA_RXD_ERR_SUMMARY
)) {
1942 mvneta_rx_error(pp
, rx_desc
);
1944 dev
->stats
.rx_errors
++;
1945 /* leave the descriptor untouched */
1949 if (rx_bytes
<= rx_copybreak
) {
1950 /* better copy a small frame and not unmap the DMA region */
1951 skb
= netdev_alloc_skb_ip_align(dev
, rx_bytes
);
1953 goto err_drop_frame
;
1955 dma_sync_single_range_for_cpu(dev
->dev
.parent
,
1957 MVNETA_MH_SIZE
+ NET_SKB_PAD
,
1960 skb_put_data(skb
, data
+ MVNETA_MH_SIZE
+ NET_SKB_PAD
,
1963 skb
->protocol
= eth_type_trans(skb
, dev
);
1964 mvneta_rx_csum(pp
, rx_status
, skb
);
1965 napi_gro_receive(&port
->napi
, skb
);
1968 rcvd_bytes
+= rx_bytes
;
1970 /* leave the descriptor and buffer untouched */
1974 /* Refill processing */
1975 err
= mvneta_rx_refill(pp
, rx_desc
, rxq
);
1977 netdev_err(dev
, "Linux processing - Can't refill\n");
1979 goto err_drop_frame
;
1982 frag_size
= pp
->frag_size
;
1984 skb
= build_skb(data
, frag_size
> PAGE_SIZE
? 0 : frag_size
);
1986 /* After refill old buffer has to be unmapped regardless
1987 * the skb is successfully built or not.
1989 dma_unmap_single(dev
->dev
.parent
, phys_addr
,
1990 MVNETA_RX_BUF_SIZE(pp
->pkt_size
),
1994 goto err_drop_frame
;
1997 rcvd_bytes
+= rx_bytes
;
1999 /* Linux processing */
2000 skb_reserve(skb
, MVNETA_MH_SIZE
+ NET_SKB_PAD
);
2001 skb_put(skb
, rx_bytes
);
2003 skb
->protocol
= eth_type_trans(skb
, dev
);
2005 mvneta_rx_csum(pp
, rx_status
, skb
);
2007 napi_gro_receive(&port
->napi
, skb
);
2011 struct mvneta_pcpu_stats
*stats
= this_cpu_ptr(pp
->stats
);
2013 u64_stats_update_begin(&stats
->syncp
);
2014 stats
->rx_packets
+= rcvd_pkts
;
2015 stats
->rx_bytes
+= rcvd_bytes
;
2016 u64_stats_update_end(&stats
->syncp
);
2019 /* Update rxq management counters */
2020 mvneta_rxq_desc_num_update(pp
, rxq
, rx_done
, rx_done
);
2025 /* Main rx processing when using hardware buffer management */
2026 static int mvneta_rx_hwbm(struct mvneta_port
*pp
, int rx_todo
,
2027 struct mvneta_rx_queue
*rxq
)
2029 struct mvneta_pcpu_port
*port
= this_cpu_ptr(pp
->ports
);
2030 struct net_device
*dev
= pp
->dev
;
2035 /* Get number of received packets */
2036 rx_done
= mvneta_rxq_busy_desc_num_get(pp
, rxq
);
2038 if (rx_todo
> rx_done
)
2043 /* Fairness NAPI loop */
2044 while (rx_done
< rx_todo
) {
2045 struct mvneta_rx_desc
*rx_desc
= mvneta_rxq_next_desc_get(rxq
);
2046 struct mvneta_bm_pool
*bm_pool
= NULL
;
2047 struct sk_buff
*skb
;
2048 unsigned char *data
;
2049 dma_addr_t phys_addr
;
2050 u32 rx_status
, frag_size
;
2055 rx_status
= rx_desc
->status
;
2056 rx_bytes
= rx_desc
->data_size
- (ETH_FCS_LEN
+ MVNETA_MH_SIZE
);
2057 data
= (u8
*)(uintptr_t)rx_desc
->buf_cookie
;
2058 phys_addr
= rx_desc
->buf_phys_addr
;
2059 pool_id
= MVNETA_RX_GET_BM_POOL_ID(rx_desc
);
2060 bm_pool
= &pp
->bm_priv
->bm_pools
[pool_id
];
2062 if (!mvneta_rxq_desc_is_first_last(rx_status
) ||
2063 (rx_status
& MVNETA_RXD_ERR_SUMMARY
)) {
2064 err_drop_frame_ret_pool
:
2065 /* Return the buffer to the pool */
2066 mvneta_bm_pool_put_bp(pp
->bm_priv
, bm_pool
,
2067 rx_desc
->buf_phys_addr
);
2069 dev
->stats
.rx_errors
++;
2070 mvneta_rx_error(pp
, rx_desc
);
2071 /* leave the descriptor untouched */
2075 if (rx_bytes
<= rx_copybreak
) {
2076 /* better copy a small frame and not unmap the DMA region */
2077 skb
= netdev_alloc_skb_ip_align(dev
, rx_bytes
);
2079 goto err_drop_frame_ret_pool
;
2081 dma_sync_single_range_for_cpu(dev
->dev
.parent
,
2082 rx_desc
->buf_phys_addr
,
2083 MVNETA_MH_SIZE
+ NET_SKB_PAD
,
2086 skb_put_data(skb
, data
+ MVNETA_MH_SIZE
+ NET_SKB_PAD
,
2089 skb
->protocol
= eth_type_trans(skb
, dev
);
2090 mvneta_rx_csum(pp
, rx_status
, skb
);
2091 napi_gro_receive(&port
->napi
, skb
);
2094 rcvd_bytes
+= rx_bytes
;
2096 /* Return the buffer to the pool */
2097 mvneta_bm_pool_put_bp(pp
->bm_priv
, bm_pool
,
2098 rx_desc
->buf_phys_addr
);
2100 /* leave the descriptor and buffer untouched */
2104 /* Refill processing */
2105 err
= hwbm_pool_refill(&bm_pool
->hwbm_pool
, GFP_ATOMIC
);
2107 netdev_err(dev
, "Linux processing - Can't refill\n");
2109 goto err_drop_frame_ret_pool
;
2112 frag_size
= bm_pool
->hwbm_pool
.frag_size
;
2114 skb
= build_skb(data
, frag_size
> PAGE_SIZE
? 0 : frag_size
);
2116 /* After refill old buffer has to be unmapped regardless
2117 * the skb is successfully built or not.
2119 dma_unmap_single(&pp
->bm_priv
->pdev
->dev
, phys_addr
,
2120 bm_pool
->buf_size
, DMA_FROM_DEVICE
);
2122 goto err_drop_frame
;
2125 rcvd_bytes
+= rx_bytes
;
2127 /* Linux processing */
2128 skb_reserve(skb
, MVNETA_MH_SIZE
+ NET_SKB_PAD
);
2129 skb_put(skb
, rx_bytes
);
2131 skb
->protocol
= eth_type_trans(skb
, dev
);
2133 mvneta_rx_csum(pp
, rx_status
, skb
);
2135 napi_gro_receive(&port
->napi
, skb
);
2139 struct mvneta_pcpu_stats
*stats
= this_cpu_ptr(pp
->stats
);
2141 u64_stats_update_begin(&stats
->syncp
);
2142 stats
->rx_packets
+= rcvd_pkts
;
2143 stats
->rx_bytes
+= rcvd_bytes
;
2144 u64_stats_update_end(&stats
->syncp
);
2147 /* Update rxq management counters */
2148 mvneta_rxq_desc_num_update(pp
, rxq
, rx_done
, rx_done
);
2154 mvneta_tso_put_hdr(struct sk_buff
*skb
,
2155 struct mvneta_port
*pp
, struct mvneta_tx_queue
*txq
)
2157 struct mvneta_tx_desc
*tx_desc
;
2158 int hdr_len
= skb_transport_offset(skb
) + tcp_hdrlen(skb
);
2160 txq
->tx_skb
[txq
->txq_put_index
] = NULL
;
2161 tx_desc
= mvneta_txq_next_desc_get(txq
);
2162 tx_desc
->data_size
= hdr_len
;
2163 tx_desc
->command
= mvneta_skb_tx_csum(pp
, skb
);
2164 tx_desc
->command
|= MVNETA_TXD_F_DESC
;
2165 tx_desc
->buf_phys_addr
= txq
->tso_hdrs_phys
+
2166 txq
->txq_put_index
* TSO_HEADER_SIZE
;
2167 mvneta_txq_inc_put(txq
);
2171 mvneta_tso_put_data(struct net_device
*dev
, struct mvneta_tx_queue
*txq
,
2172 struct sk_buff
*skb
, char *data
, int size
,
2173 bool last_tcp
, bool is_last
)
2175 struct mvneta_tx_desc
*tx_desc
;
2177 tx_desc
= mvneta_txq_next_desc_get(txq
);
2178 tx_desc
->data_size
= size
;
2179 tx_desc
->buf_phys_addr
= dma_map_single(dev
->dev
.parent
, data
,
2180 size
, DMA_TO_DEVICE
);
2181 if (unlikely(dma_mapping_error(dev
->dev
.parent
,
2182 tx_desc
->buf_phys_addr
))) {
2183 mvneta_txq_desc_put(txq
);
2187 tx_desc
->command
= 0;
2188 txq
->tx_skb
[txq
->txq_put_index
] = NULL
;
2191 /* last descriptor in the TCP packet */
2192 tx_desc
->command
= MVNETA_TXD_L_DESC
;
2194 /* last descriptor in SKB */
2196 txq
->tx_skb
[txq
->txq_put_index
] = skb
;
2198 mvneta_txq_inc_put(txq
);
2202 static int mvneta_tx_tso(struct sk_buff
*skb
, struct net_device
*dev
,
2203 struct mvneta_tx_queue
*txq
)
2205 int total_len
, data_left
;
2207 struct mvneta_port
*pp
= netdev_priv(dev
);
2209 int hdr_len
= skb_transport_offset(skb
) + tcp_hdrlen(skb
);
2212 /* Count needed descriptors */
2213 if ((txq
->count
+ tso_count_descs(skb
)) >= txq
->size
)
2216 if (skb_headlen(skb
) < (skb_transport_offset(skb
) + tcp_hdrlen(skb
))) {
2217 pr_info("*** Is this even possible???!?!?\n");
2221 /* Initialize the TSO handler, and prepare the first payload */
2222 tso_start(skb
, &tso
);
2224 total_len
= skb
->len
- hdr_len
;
2225 while (total_len
> 0) {
2228 data_left
= min_t(int, skb_shinfo(skb
)->gso_size
, total_len
);
2229 total_len
-= data_left
;
2232 /* prepare packet headers: MAC + IP + TCP */
2233 hdr
= txq
->tso_hdrs
+ txq
->txq_put_index
* TSO_HEADER_SIZE
;
2234 tso_build_hdr(skb
, hdr
, &tso
, data_left
, total_len
== 0);
2236 mvneta_tso_put_hdr(skb
, pp
, txq
);
2238 while (data_left
> 0) {
2242 size
= min_t(int, tso
.size
, data_left
);
2244 if (mvneta_tso_put_data(dev
, txq
, skb
,
2251 tso_build_data(skb
, &tso
, size
);
2258 /* Release all used data descriptors; header descriptors must not
2261 for (i
= desc_count
- 1; i
>= 0; i
--) {
2262 struct mvneta_tx_desc
*tx_desc
= txq
->descs
+ i
;
2263 if (!IS_TSO_HEADER(txq
, tx_desc
->buf_phys_addr
))
2264 dma_unmap_single(pp
->dev
->dev
.parent
,
2265 tx_desc
->buf_phys_addr
,
2268 mvneta_txq_desc_put(txq
);
2273 /* Handle tx fragmentation processing */
2274 static int mvneta_tx_frag_process(struct mvneta_port
*pp
, struct sk_buff
*skb
,
2275 struct mvneta_tx_queue
*txq
)
2277 struct mvneta_tx_desc
*tx_desc
;
2278 int i
, nr_frags
= skb_shinfo(skb
)->nr_frags
;
2280 for (i
= 0; i
< nr_frags
; i
++) {
2281 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
2282 void *addr
= page_address(frag
->page
.p
) + frag
->page_offset
;
2284 tx_desc
= mvneta_txq_next_desc_get(txq
);
2285 tx_desc
->data_size
= frag
->size
;
2287 tx_desc
->buf_phys_addr
=
2288 dma_map_single(pp
->dev
->dev
.parent
, addr
,
2289 tx_desc
->data_size
, DMA_TO_DEVICE
);
2291 if (dma_mapping_error(pp
->dev
->dev
.parent
,
2292 tx_desc
->buf_phys_addr
)) {
2293 mvneta_txq_desc_put(txq
);
2297 if (i
== nr_frags
- 1) {
2298 /* Last descriptor */
2299 tx_desc
->command
= MVNETA_TXD_L_DESC
| MVNETA_TXD_Z_PAD
;
2300 txq
->tx_skb
[txq
->txq_put_index
] = skb
;
2302 /* Descriptor in the middle: Not First, Not Last */
2303 tx_desc
->command
= 0;
2304 txq
->tx_skb
[txq
->txq_put_index
] = NULL
;
2306 mvneta_txq_inc_put(txq
);
2312 /* Release all descriptors that were used to map fragments of
2313 * this packet, as well as the corresponding DMA mappings
2315 for (i
= i
- 1; i
>= 0; i
--) {
2316 tx_desc
= txq
->descs
+ i
;
2317 dma_unmap_single(pp
->dev
->dev
.parent
,
2318 tx_desc
->buf_phys_addr
,
2321 mvneta_txq_desc_put(txq
);
2327 /* Main tx processing */
2328 static int mvneta_tx(struct sk_buff
*skb
, struct net_device
*dev
)
2330 struct mvneta_port
*pp
= netdev_priv(dev
);
2331 u16 txq_id
= skb_get_queue_mapping(skb
);
2332 struct mvneta_tx_queue
*txq
= &pp
->txqs
[txq_id
];
2333 struct mvneta_tx_desc
*tx_desc
;
2338 if (!netif_running(dev
))
2341 if (skb_is_gso(skb
)) {
2342 frags
= mvneta_tx_tso(skb
, dev
, txq
);
2346 frags
= skb_shinfo(skb
)->nr_frags
+ 1;
2348 /* Get a descriptor for the first part of the packet */
2349 tx_desc
= mvneta_txq_next_desc_get(txq
);
2351 tx_cmd
= mvneta_skb_tx_csum(pp
, skb
);
2353 tx_desc
->data_size
= skb_headlen(skb
);
2355 tx_desc
->buf_phys_addr
= dma_map_single(dev
->dev
.parent
, skb
->data
,
2358 if (unlikely(dma_mapping_error(dev
->dev
.parent
,
2359 tx_desc
->buf_phys_addr
))) {
2360 mvneta_txq_desc_put(txq
);
2366 /* First and Last descriptor */
2367 tx_cmd
|= MVNETA_TXD_FLZ_DESC
;
2368 tx_desc
->command
= tx_cmd
;
2369 txq
->tx_skb
[txq
->txq_put_index
] = skb
;
2370 mvneta_txq_inc_put(txq
);
2372 /* First but not Last */
2373 tx_cmd
|= MVNETA_TXD_F_DESC
;
2374 txq
->tx_skb
[txq
->txq_put_index
] = NULL
;
2375 mvneta_txq_inc_put(txq
);
2376 tx_desc
->command
= tx_cmd
;
2377 /* Continue with other skb fragments */
2378 if (mvneta_tx_frag_process(pp
, skb
, txq
)) {
2379 dma_unmap_single(dev
->dev
.parent
,
2380 tx_desc
->buf_phys_addr
,
2383 mvneta_txq_desc_put(txq
);
2391 struct mvneta_pcpu_stats
*stats
= this_cpu_ptr(pp
->stats
);
2392 struct netdev_queue
*nq
= netdev_get_tx_queue(dev
, txq_id
);
2394 netdev_tx_sent_queue(nq
, len
);
2396 txq
->count
+= frags
;
2397 if (txq
->count
>= txq
->tx_stop_threshold
)
2398 netif_tx_stop_queue(nq
);
2400 if (!skb
->xmit_more
|| netif_xmit_stopped(nq
) ||
2401 txq
->pending
+ frags
> MVNETA_TXQ_DEC_SENT_MASK
)
2402 mvneta_txq_pend_desc_add(pp
, txq
, frags
);
2404 txq
->pending
+= frags
;
2406 u64_stats_update_begin(&stats
->syncp
);
2407 stats
->tx_packets
++;
2408 stats
->tx_bytes
+= len
;
2409 u64_stats_update_end(&stats
->syncp
);
2411 dev
->stats
.tx_dropped
++;
2412 dev_kfree_skb_any(skb
);
2415 return NETDEV_TX_OK
;
2419 /* Free tx resources, when resetting a port */
2420 static void mvneta_txq_done_force(struct mvneta_port
*pp
,
2421 struct mvneta_tx_queue
*txq
)
2424 struct netdev_queue
*nq
= netdev_get_tx_queue(pp
->dev
, txq
->id
);
2425 int tx_done
= txq
->count
;
2427 mvneta_txq_bufs_free(pp
, txq
, tx_done
, nq
);
2431 txq
->txq_put_index
= 0;
2432 txq
->txq_get_index
= 0;
2435 /* Handle tx done - called in softirq context. The <cause_tx_done> argument
2436 * must be a valid cause according to MVNETA_TXQ_INTR_MASK_ALL.
2438 static void mvneta_tx_done_gbe(struct mvneta_port
*pp
, u32 cause_tx_done
)
2440 struct mvneta_tx_queue
*txq
;
2441 struct netdev_queue
*nq
;
2443 while (cause_tx_done
) {
2444 txq
= mvneta_tx_done_policy(pp
, cause_tx_done
);
2446 nq
= netdev_get_tx_queue(pp
->dev
, txq
->id
);
2447 __netif_tx_lock(nq
, smp_processor_id());
2450 mvneta_txq_done(pp
, txq
);
2452 __netif_tx_unlock(nq
);
2453 cause_tx_done
&= ~((1 << txq
->id
));
2457 /* Compute crc8 of the specified address, using a unique algorithm ,
2458 * according to hw spec, different than generic crc8 algorithm
2460 static int mvneta_addr_crc(unsigned char *addr
)
2465 for (i
= 0; i
< ETH_ALEN
; i
++) {
2468 crc
= (crc
^ addr
[i
]) << 8;
2469 for (j
= 7; j
>= 0; j
--) {
2470 if (crc
& (0x100 << j
))
2478 /* This method controls the net device special MAC multicast support.
2479 * The Special Multicast Table for MAC addresses supports MAC of the form
2480 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
2481 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
2482 * Table entries in the DA-Filter table. This method set the Special
2483 * Multicast Table appropriate entry.
2485 static void mvneta_set_special_mcast_addr(struct mvneta_port
*pp
,
2486 unsigned char last_byte
,
2489 unsigned int smc_table_reg
;
2490 unsigned int tbl_offset
;
2491 unsigned int reg_offset
;
2493 /* Register offset from SMC table base */
2494 tbl_offset
= (last_byte
/ 4);
2495 /* Entry offset within the above reg */
2496 reg_offset
= last_byte
% 4;
2498 smc_table_reg
= mvreg_read(pp
, (MVNETA_DA_FILT_SPEC_MCAST
2502 smc_table_reg
&= ~(0xff << (8 * reg_offset
));
2504 smc_table_reg
&= ~(0xff << (8 * reg_offset
));
2505 smc_table_reg
|= ((0x01 | (queue
<< 1)) << (8 * reg_offset
));
2508 mvreg_write(pp
, MVNETA_DA_FILT_SPEC_MCAST
+ tbl_offset
* 4,
2512 /* This method controls the network device Other MAC multicast support.
2513 * The Other Multicast Table is used for multicast of another type.
2514 * A CRC-8 is used as an index to the Other Multicast Table entries
2515 * in the DA-Filter table.
2516 * The method gets the CRC-8 value from the calling routine and
2517 * sets the Other Multicast Table appropriate entry according to the
2520 static void mvneta_set_other_mcast_addr(struct mvneta_port
*pp
,
2524 unsigned int omc_table_reg
;
2525 unsigned int tbl_offset
;
2526 unsigned int reg_offset
;
2528 tbl_offset
= (crc8
/ 4) * 4; /* Register offset from OMC table base */
2529 reg_offset
= crc8
% 4; /* Entry offset within the above reg */
2531 omc_table_reg
= mvreg_read(pp
, MVNETA_DA_FILT_OTH_MCAST
+ tbl_offset
);
2534 /* Clear accepts frame bit at specified Other DA table entry */
2535 omc_table_reg
&= ~(0xff << (8 * reg_offset
));
2537 omc_table_reg
&= ~(0xff << (8 * reg_offset
));
2538 omc_table_reg
|= ((0x01 | (queue
<< 1)) << (8 * reg_offset
));
2541 mvreg_write(pp
, MVNETA_DA_FILT_OTH_MCAST
+ tbl_offset
, omc_table_reg
);
2544 /* The network device supports multicast using two tables:
2545 * 1) Special Multicast Table for MAC addresses of the form
2546 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
2547 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
2548 * Table entries in the DA-Filter table.
2549 * 2) Other Multicast Table for multicast of another type. A CRC-8 value
2550 * is used as an index to the Other Multicast Table entries in the
2553 static int mvneta_mcast_addr_set(struct mvneta_port
*pp
, unsigned char *p_addr
,
2556 unsigned char crc_result
= 0;
2558 if (memcmp(p_addr
, "\x01\x00\x5e\x00\x00", 5) == 0) {
2559 mvneta_set_special_mcast_addr(pp
, p_addr
[5], queue
);
2563 crc_result
= mvneta_addr_crc(p_addr
);
2565 if (pp
->mcast_count
[crc_result
] == 0) {
2566 netdev_info(pp
->dev
, "No valid Mcast for crc8=0x%02x\n",
2571 pp
->mcast_count
[crc_result
]--;
2572 if (pp
->mcast_count
[crc_result
] != 0) {
2573 netdev_info(pp
->dev
,
2574 "After delete there are %d valid Mcast for crc8=0x%02x\n",
2575 pp
->mcast_count
[crc_result
], crc_result
);
2579 pp
->mcast_count
[crc_result
]++;
2581 mvneta_set_other_mcast_addr(pp
, crc_result
, queue
);
2586 /* Configure Fitering mode of Ethernet port */
2587 static void mvneta_rx_unicast_promisc_set(struct mvneta_port
*pp
,
2590 u32 port_cfg_reg
, val
;
2592 port_cfg_reg
= mvreg_read(pp
, MVNETA_PORT_CONFIG
);
2594 val
= mvreg_read(pp
, MVNETA_TYPE_PRIO
);
2596 /* Set / Clear UPM bit in port configuration register */
2598 /* Accept all Unicast addresses */
2599 port_cfg_reg
|= MVNETA_UNI_PROMISC_MODE
;
2600 val
|= MVNETA_FORCE_UNI
;
2601 mvreg_write(pp
, MVNETA_MAC_ADDR_LOW
, 0xffff);
2602 mvreg_write(pp
, MVNETA_MAC_ADDR_HIGH
, 0xffffffff);
2604 /* Reject all Unicast addresses */
2605 port_cfg_reg
&= ~MVNETA_UNI_PROMISC_MODE
;
2606 val
&= ~MVNETA_FORCE_UNI
;
2609 mvreg_write(pp
, MVNETA_PORT_CONFIG
, port_cfg_reg
);
2610 mvreg_write(pp
, MVNETA_TYPE_PRIO
, val
);
2613 /* register unicast and multicast addresses */
2614 static void mvneta_set_rx_mode(struct net_device
*dev
)
2616 struct mvneta_port
*pp
= netdev_priv(dev
);
2617 struct netdev_hw_addr
*ha
;
2619 if (dev
->flags
& IFF_PROMISC
) {
2620 /* Accept all: Multicast + Unicast */
2621 mvneta_rx_unicast_promisc_set(pp
, 1);
2622 mvneta_set_ucast_table(pp
, pp
->rxq_def
);
2623 mvneta_set_special_mcast_table(pp
, pp
->rxq_def
);
2624 mvneta_set_other_mcast_table(pp
, pp
->rxq_def
);
2626 /* Accept single Unicast */
2627 mvneta_rx_unicast_promisc_set(pp
, 0);
2628 mvneta_set_ucast_table(pp
, -1);
2629 mvneta_mac_addr_set(pp
, dev
->dev_addr
, pp
->rxq_def
);
2631 if (dev
->flags
& IFF_ALLMULTI
) {
2632 /* Accept all multicast */
2633 mvneta_set_special_mcast_table(pp
, pp
->rxq_def
);
2634 mvneta_set_other_mcast_table(pp
, pp
->rxq_def
);
2636 /* Accept only initialized multicast */
2637 mvneta_set_special_mcast_table(pp
, -1);
2638 mvneta_set_other_mcast_table(pp
, -1);
2640 if (!netdev_mc_empty(dev
)) {
2641 netdev_for_each_mc_addr(ha
, dev
) {
2642 mvneta_mcast_addr_set(pp
, ha
->addr
,
2650 /* Interrupt handling - the callback for request_irq() */
2651 static irqreturn_t
mvneta_isr(int irq
, void *dev_id
)
2653 struct mvneta_port
*pp
= (struct mvneta_port
*)dev_id
;
2655 mvreg_write(pp
, MVNETA_INTR_NEW_MASK
, 0);
2656 napi_schedule(&pp
->napi
);
2661 /* Interrupt handling - the callback for request_percpu_irq() */
2662 static irqreturn_t
mvneta_percpu_isr(int irq
, void *dev_id
)
2664 struct mvneta_pcpu_port
*port
= (struct mvneta_pcpu_port
*)dev_id
;
2666 disable_percpu_irq(port
->pp
->dev
->irq
);
2667 napi_schedule(&port
->napi
);
2672 static void mvneta_link_change(struct mvneta_port
*pp
)
2674 u32 gmac_stat
= mvreg_read(pp
, MVNETA_GMAC_STATUS
);
2676 phylink_mac_change(pp
->phylink
, !!(gmac_stat
& MVNETA_GMAC_LINK_UP
));
2680 * Bits 0 - 7 of the causeRxTx register indicate that are transmitted
2681 * packets on the corresponding TXQ (Bit 0 is for TX queue 1).
2682 * Bits 8 -15 of the cause Rx Tx register indicate that are received
2683 * packets on the corresponding RXQ (Bit 8 is for RX queue 0).
2684 * Each CPU has its own causeRxTx register
2686 static int mvneta_poll(struct napi_struct
*napi
, int budget
)
2691 struct mvneta_port
*pp
= netdev_priv(napi
->dev
);
2692 struct mvneta_pcpu_port
*port
= this_cpu_ptr(pp
->ports
);
2694 if (!netif_running(pp
->dev
)) {
2695 napi_complete(napi
);
2699 /* Read cause register */
2700 cause_rx_tx
= mvreg_read(pp
, MVNETA_INTR_NEW_CAUSE
);
2701 if (cause_rx_tx
& MVNETA_MISCINTR_INTR_MASK
) {
2702 u32 cause_misc
= mvreg_read(pp
, MVNETA_INTR_MISC_CAUSE
);
2704 mvreg_write(pp
, MVNETA_INTR_MISC_CAUSE
, 0);
2706 if (cause_misc
& (MVNETA_CAUSE_PHY_STATUS_CHANGE
|
2707 MVNETA_CAUSE_LINK_CHANGE
))
2708 mvneta_link_change(pp
);
2711 /* Release Tx descriptors */
2712 if (cause_rx_tx
& MVNETA_TX_INTR_MASK_ALL
) {
2713 mvneta_tx_done_gbe(pp
, (cause_rx_tx
& MVNETA_TX_INTR_MASK_ALL
));
2714 cause_rx_tx
&= ~MVNETA_TX_INTR_MASK_ALL
;
2717 /* For the case where the last mvneta_poll did not process all
2720 rx_queue
= fls(((cause_rx_tx
>> 8) & 0xff));
2722 cause_rx_tx
|= pp
->neta_armada3700
? pp
->cause_rx_tx
:
2726 rx_queue
= rx_queue
- 1;
2728 rx_done
= mvneta_rx_hwbm(pp
, budget
, &pp
->rxqs
[rx_queue
]);
2730 rx_done
= mvneta_rx_swbm(pp
, budget
, &pp
->rxqs
[rx_queue
]);
2733 if (rx_done
< budget
) {
2735 napi_complete_done(napi
, rx_done
);
2737 if (pp
->neta_armada3700
) {
2738 unsigned long flags
;
2740 local_irq_save(flags
);
2741 mvreg_write(pp
, MVNETA_INTR_NEW_MASK
,
2742 MVNETA_RX_INTR_MASK(rxq_number
) |
2743 MVNETA_TX_INTR_MASK(txq_number
) |
2744 MVNETA_MISCINTR_INTR_MASK
);
2745 local_irq_restore(flags
);
2747 enable_percpu_irq(pp
->dev
->irq
, 0);
2751 if (pp
->neta_armada3700
)
2752 pp
->cause_rx_tx
= cause_rx_tx
;
2754 port
->cause_rx_tx
= cause_rx_tx
;
2759 /* Handle rxq fill: allocates rxq skbs; called when initializing a port */
2760 static int mvneta_rxq_fill(struct mvneta_port
*pp
, struct mvneta_rx_queue
*rxq
,
2765 for (i
= 0; i
< num
; i
++) {
2766 memset(rxq
->descs
+ i
, 0, sizeof(struct mvneta_rx_desc
));
2767 if (mvneta_rx_refill(pp
, rxq
->descs
+ i
, rxq
) != 0) {
2768 netdev_err(pp
->dev
, "%s:rxq %d, %d of %d buffs filled\n",
2769 __func__
, rxq
->id
, i
, num
);
2774 /* Add this number of RX descriptors as non occupied (ready to
2777 mvneta_rxq_non_occup_desc_add(pp
, rxq
, i
);
2782 /* Free all packets pending transmit from all TXQs and reset TX port */
2783 static void mvneta_tx_reset(struct mvneta_port
*pp
)
2787 /* free the skb's in the tx ring */
2788 for (queue
= 0; queue
< txq_number
; queue
++)
2789 mvneta_txq_done_force(pp
, &pp
->txqs
[queue
]);
2791 mvreg_write(pp
, MVNETA_PORT_TX_RESET
, MVNETA_PORT_TX_DMA_RESET
);
2792 mvreg_write(pp
, MVNETA_PORT_TX_RESET
, 0);
2795 static void mvneta_rx_reset(struct mvneta_port
*pp
)
2797 mvreg_write(pp
, MVNETA_PORT_RX_RESET
, MVNETA_PORT_RX_DMA_RESET
);
2798 mvreg_write(pp
, MVNETA_PORT_RX_RESET
, 0);
2801 /* Rx/Tx queue initialization/cleanup methods */
2803 /* Create a specified RX queue */
2804 static int mvneta_rxq_init(struct mvneta_port
*pp
,
2805 struct mvneta_rx_queue
*rxq
)
2808 rxq
->size
= pp
->rx_ring_size
;
2810 /* Allocate memory for RX descriptors */
2811 rxq
->descs
= dma_alloc_coherent(pp
->dev
->dev
.parent
,
2812 rxq
->size
* MVNETA_DESC_ALIGNED_SIZE
,
2813 &rxq
->descs_phys
, GFP_KERNEL
);
2817 rxq
->last_desc
= rxq
->size
- 1;
2819 /* Set Rx descriptors queue starting address */
2820 mvreg_write(pp
, MVNETA_RXQ_BASE_ADDR_REG(rxq
->id
), rxq
->descs_phys
);
2821 mvreg_write(pp
, MVNETA_RXQ_SIZE_REG(rxq
->id
), rxq
->size
);
2824 mvneta_rxq_offset_set(pp
, rxq
, NET_SKB_PAD
- pp
->rx_offset_correction
);
2826 /* Set coalescing pkts and time */
2827 mvneta_rx_pkts_coal_set(pp
, rxq
, rxq
->pkts_coal
);
2828 mvneta_rx_time_coal_set(pp
, rxq
, rxq
->time_coal
);
2831 /* Fill RXQ with buffers from RX pool */
2832 mvneta_rxq_buf_size_set(pp
, rxq
,
2833 MVNETA_RX_BUF_SIZE(pp
->pkt_size
));
2834 mvneta_rxq_bm_disable(pp
, rxq
);
2835 mvneta_rxq_fill(pp
, rxq
, rxq
->size
);
2837 mvneta_rxq_bm_enable(pp
, rxq
);
2838 mvneta_rxq_long_pool_set(pp
, rxq
);
2839 mvneta_rxq_short_pool_set(pp
, rxq
);
2840 mvneta_rxq_non_occup_desc_add(pp
, rxq
, rxq
->size
);
2846 /* Cleanup Rx queue */
2847 static void mvneta_rxq_deinit(struct mvneta_port
*pp
,
2848 struct mvneta_rx_queue
*rxq
)
2850 mvneta_rxq_drop_pkts(pp
, rxq
);
2853 dma_free_coherent(pp
->dev
->dev
.parent
,
2854 rxq
->size
* MVNETA_DESC_ALIGNED_SIZE
,
2860 rxq
->next_desc_to_proc
= 0;
2861 rxq
->descs_phys
= 0;
2864 /* Create and initialize a tx queue */
2865 static int mvneta_txq_init(struct mvneta_port
*pp
,
2866 struct mvneta_tx_queue
*txq
)
2870 txq
->size
= pp
->tx_ring_size
;
2872 /* A queue must always have room for at least one skb.
2873 * Therefore, stop the queue when the free entries reaches
2874 * the maximum number of descriptors per skb.
2876 txq
->tx_stop_threshold
= txq
->size
- MVNETA_MAX_SKB_DESCS
;
2877 txq
->tx_wake_threshold
= txq
->tx_stop_threshold
/ 2;
2880 /* Allocate memory for TX descriptors */
2881 txq
->descs
= dma_alloc_coherent(pp
->dev
->dev
.parent
,
2882 txq
->size
* MVNETA_DESC_ALIGNED_SIZE
,
2883 &txq
->descs_phys
, GFP_KERNEL
);
2887 txq
->last_desc
= txq
->size
- 1;
2889 /* Set maximum bandwidth for enabled TXQs */
2890 mvreg_write(pp
, MVETH_TXQ_TOKEN_CFG_REG(txq
->id
), 0x03ffffff);
2891 mvreg_write(pp
, MVETH_TXQ_TOKEN_COUNT_REG(txq
->id
), 0x3fffffff);
2893 /* Set Tx descriptors queue starting address */
2894 mvreg_write(pp
, MVNETA_TXQ_BASE_ADDR_REG(txq
->id
), txq
->descs_phys
);
2895 mvreg_write(pp
, MVNETA_TXQ_SIZE_REG(txq
->id
), txq
->size
);
2897 txq
->tx_skb
= kmalloc_array(txq
->size
, sizeof(*txq
->tx_skb
),
2900 dma_free_coherent(pp
->dev
->dev
.parent
,
2901 txq
->size
* MVNETA_DESC_ALIGNED_SIZE
,
2902 txq
->descs
, txq
->descs_phys
);
2906 /* Allocate DMA buffers for TSO MAC/IP/TCP headers */
2907 txq
->tso_hdrs
= dma_alloc_coherent(pp
->dev
->dev
.parent
,
2908 txq
->size
* TSO_HEADER_SIZE
,
2909 &txq
->tso_hdrs_phys
, GFP_KERNEL
);
2910 if (!txq
->tso_hdrs
) {
2912 dma_free_coherent(pp
->dev
->dev
.parent
,
2913 txq
->size
* MVNETA_DESC_ALIGNED_SIZE
,
2914 txq
->descs
, txq
->descs_phys
);
2917 mvneta_tx_done_pkts_coal_set(pp
, txq
, txq
->done_pkts_coal
);
2919 /* Setup XPS mapping */
2921 cpu
= txq
->id
% num_present_cpus();
2923 cpu
= pp
->rxq_def
% num_present_cpus();
2924 cpumask_set_cpu(cpu
, &txq
->affinity_mask
);
2925 netif_set_xps_queue(pp
->dev
, &txq
->affinity_mask
, txq
->id
);
2930 /* Free allocated resources when mvneta_txq_init() fails to allocate memory*/
2931 static void mvneta_txq_deinit(struct mvneta_port
*pp
,
2932 struct mvneta_tx_queue
*txq
)
2934 struct netdev_queue
*nq
= netdev_get_tx_queue(pp
->dev
, txq
->id
);
2939 dma_free_coherent(pp
->dev
->dev
.parent
,
2940 txq
->size
* TSO_HEADER_SIZE
,
2941 txq
->tso_hdrs
, txq
->tso_hdrs_phys
);
2943 dma_free_coherent(pp
->dev
->dev
.parent
,
2944 txq
->size
* MVNETA_DESC_ALIGNED_SIZE
,
2945 txq
->descs
, txq
->descs_phys
);
2947 netdev_tx_reset_queue(nq
);
2951 txq
->next_desc_to_proc
= 0;
2952 txq
->descs_phys
= 0;
2954 /* Set minimum bandwidth for disabled TXQs */
2955 mvreg_write(pp
, MVETH_TXQ_TOKEN_CFG_REG(txq
->id
), 0);
2956 mvreg_write(pp
, MVETH_TXQ_TOKEN_COUNT_REG(txq
->id
), 0);
2958 /* Set Tx descriptors queue starting address and size */
2959 mvreg_write(pp
, MVNETA_TXQ_BASE_ADDR_REG(txq
->id
), 0);
2960 mvreg_write(pp
, MVNETA_TXQ_SIZE_REG(txq
->id
), 0);
2963 /* Cleanup all Tx queues */
2964 static void mvneta_cleanup_txqs(struct mvneta_port
*pp
)
2968 for (queue
= 0; queue
< txq_number
; queue
++)
2969 mvneta_txq_deinit(pp
, &pp
->txqs
[queue
]);
2972 /* Cleanup all Rx queues */
2973 static void mvneta_cleanup_rxqs(struct mvneta_port
*pp
)
2977 for (queue
= 0; queue
< rxq_number
; queue
++)
2978 mvneta_rxq_deinit(pp
, &pp
->rxqs
[queue
]);
2982 /* Init all Rx queues */
2983 static int mvneta_setup_rxqs(struct mvneta_port
*pp
)
2987 for (queue
= 0; queue
< rxq_number
; queue
++) {
2988 int err
= mvneta_rxq_init(pp
, &pp
->rxqs
[queue
]);
2991 netdev_err(pp
->dev
, "%s: can't create rxq=%d\n",
2993 mvneta_cleanup_rxqs(pp
);
3001 /* Init all tx queues */
3002 static int mvneta_setup_txqs(struct mvneta_port
*pp
)
3006 for (queue
= 0; queue
< txq_number
; queue
++) {
3007 int err
= mvneta_txq_init(pp
, &pp
->txqs
[queue
]);
3009 netdev_err(pp
->dev
, "%s: can't create txq=%d\n",
3011 mvneta_cleanup_txqs(pp
);
3019 static void mvneta_start_dev(struct mvneta_port
*pp
)
3023 mvneta_max_rx_size_set(pp
, pp
->pkt_size
);
3024 mvneta_txq_max_tx_size_set(pp
, pp
->pkt_size
);
3026 /* start the Rx/Tx activity */
3027 mvneta_port_enable(pp
);
3029 if (!pp
->neta_armada3700
) {
3030 /* Enable polling on the port */
3031 for_each_online_cpu(cpu
) {
3032 struct mvneta_pcpu_port
*port
=
3033 per_cpu_ptr(pp
->ports
, cpu
);
3035 napi_enable(&port
->napi
);
3038 napi_enable(&pp
->napi
);
3041 /* Unmask interrupts. It has to be done from each CPU */
3042 on_each_cpu(mvneta_percpu_unmask_interrupt
, pp
, true);
3044 mvreg_write(pp
, MVNETA_INTR_MISC_MASK
,
3045 MVNETA_CAUSE_PHY_STATUS_CHANGE
|
3046 MVNETA_CAUSE_LINK_CHANGE
);
3048 phylink_start(pp
->phylink
);
3049 netif_tx_start_all_queues(pp
->dev
);
3052 static void mvneta_stop_dev(struct mvneta_port
*pp
)
3056 phylink_stop(pp
->phylink
);
3058 if (!pp
->neta_armada3700
) {
3059 for_each_online_cpu(cpu
) {
3060 struct mvneta_pcpu_port
*port
=
3061 per_cpu_ptr(pp
->ports
, cpu
);
3063 napi_disable(&port
->napi
);
3066 napi_disable(&pp
->napi
);
3069 netif_carrier_off(pp
->dev
);
3071 mvneta_port_down(pp
);
3072 netif_tx_stop_all_queues(pp
->dev
);
3074 /* Stop the port activity */
3075 mvneta_port_disable(pp
);
3077 /* Clear all ethernet port interrupts */
3078 on_each_cpu(mvneta_percpu_clear_intr_cause
, pp
, true);
3080 /* Mask all ethernet port interrupts */
3081 on_each_cpu(mvneta_percpu_mask_interrupt
, pp
, true);
3083 mvneta_tx_reset(pp
);
3084 mvneta_rx_reset(pp
);
3087 static void mvneta_percpu_enable(void *arg
)
3089 struct mvneta_port
*pp
= arg
;
3091 enable_percpu_irq(pp
->dev
->irq
, IRQ_TYPE_NONE
);
3094 static void mvneta_percpu_disable(void *arg
)
3096 struct mvneta_port
*pp
= arg
;
3098 disable_percpu_irq(pp
->dev
->irq
);
3101 /* Change the device mtu */
3102 static int mvneta_change_mtu(struct net_device
*dev
, int mtu
)
3104 struct mvneta_port
*pp
= netdev_priv(dev
);
3107 if (!IS_ALIGNED(MVNETA_RX_PKT_SIZE(mtu
), 8)) {
3108 netdev_info(dev
, "Illegal MTU value %d, rounding to %d\n",
3109 mtu
, ALIGN(MVNETA_RX_PKT_SIZE(mtu
), 8));
3110 mtu
= ALIGN(MVNETA_RX_PKT_SIZE(mtu
), 8);
3115 if (!netif_running(dev
)) {
3117 mvneta_bm_update_mtu(pp
, mtu
);
3119 netdev_update_features(dev
);
3123 /* The interface is running, so we have to force a
3124 * reallocation of the queues
3126 mvneta_stop_dev(pp
);
3127 on_each_cpu(mvneta_percpu_disable
, pp
, true);
3129 mvneta_cleanup_txqs(pp
);
3130 mvneta_cleanup_rxqs(pp
);
3133 mvneta_bm_update_mtu(pp
, mtu
);
3135 pp
->pkt_size
= MVNETA_RX_PKT_SIZE(dev
->mtu
);
3136 pp
->frag_size
= SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(pp
->pkt_size
)) +
3137 SKB_DATA_ALIGN(sizeof(struct skb_shared_info
));
3139 ret
= mvneta_setup_rxqs(pp
);
3141 netdev_err(dev
, "unable to setup rxqs after MTU change\n");
3145 ret
= mvneta_setup_txqs(pp
);
3147 netdev_err(dev
, "unable to setup txqs after MTU change\n");
3151 on_each_cpu(mvneta_percpu_enable
, pp
, true);
3152 mvneta_start_dev(pp
);
3155 netdev_update_features(dev
);
3160 static netdev_features_t
mvneta_fix_features(struct net_device
*dev
,
3161 netdev_features_t features
)
3163 struct mvneta_port
*pp
= netdev_priv(dev
);
3165 if (pp
->tx_csum_limit
&& dev
->mtu
> pp
->tx_csum_limit
) {
3166 features
&= ~(NETIF_F_IP_CSUM
| NETIF_F_TSO
);
3168 "Disable IP checksum for MTU greater than %dB\n",
3175 /* Get mac address */
3176 static void mvneta_get_mac_addr(struct mvneta_port
*pp
, unsigned char *addr
)
3178 u32 mac_addr_l
, mac_addr_h
;
3180 mac_addr_l
= mvreg_read(pp
, MVNETA_MAC_ADDR_LOW
);
3181 mac_addr_h
= mvreg_read(pp
, MVNETA_MAC_ADDR_HIGH
);
3182 addr
[0] = (mac_addr_h
>> 24) & 0xFF;
3183 addr
[1] = (mac_addr_h
>> 16) & 0xFF;
3184 addr
[2] = (mac_addr_h
>> 8) & 0xFF;
3185 addr
[3] = mac_addr_h
& 0xFF;
3186 addr
[4] = (mac_addr_l
>> 8) & 0xFF;
3187 addr
[5] = mac_addr_l
& 0xFF;
3190 /* Handle setting mac address */
3191 static int mvneta_set_mac_addr(struct net_device
*dev
, void *addr
)
3193 struct mvneta_port
*pp
= netdev_priv(dev
);
3194 struct sockaddr
*sockaddr
= addr
;
3197 ret
= eth_prepare_mac_addr_change(dev
, addr
);
3200 /* Remove previous address table entry */
3201 mvneta_mac_addr_set(pp
, dev
->dev_addr
, -1);
3203 /* Set new addr in hw */
3204 mvneta_mac_addr_set(pp
, sockaddr
->sa_data
, pp
->rxq_def
);
3206 eth_commit_mac_addr_change(dev
, addr
);
3210 static void mvneta_validate(struct net_device
*ndev
, unsigned long *supported
,
3211 struct phylink_link_state
*state
)
3213 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask
) = { 0, };
3215 /* We only support QSGMII, SGMII, 802.3z and RGMII modes */
3216 if (state
->interface
!= PHY_INTERFACE_MODE_NA
&&
3217 state
->interface
!= PHY_INTERFACE_MODE_QSGMII
&&
3218 state
->interface
!= PHY_INTERFACE_MODE_SGMII
&&
3219 !phy_interface_mode_is_8023z(state
->interface
) &&
3220 !phy_interface_mode_is_rgmii(state
->interface
)) {
3221 bitmap_zero(supported
, __ETHTOOL_LINK_MODE_MASK_NBITS
);
3225 /* Allow all the expected bits */
3226 phylink_set(mask
, Autoneg
);
3227 phylink_set_port_modes(mask
);
3229 /* Asymmetric pause is unsupported */
3230 phylink_set(mask
, Pause
);
3231 /* Half-duplex at speeds higher than 100Mbit is unsupported */
3232 phylink_set(mask
, 1000baseT_Full
);
3233 phylink_set(mask
, 1000baseX_Full
);
3235 if (!phy_interface_mode_is_8023z(state
->interface
)) {
3236 /* 10M and 100M are only supported in non-802.3z mode */
3237 phylink_set(mask
, 10baseT_Half
);
3238 phylink_set(mask
, 10baseT_Full
);
3239 phylink_set(mask
, 100baseT_Half
);
3240 phylink_set(mask
, 100baseT_Full
);
3243 bitmap_and(supported
, supported
, mask
,
3244 __ETHTOOL_LINK_MODE_MASK_NBITS
);
3245 bitmap_and(state
->advertising
, state
->advertising
, mask
,
3246 __ETHTOOL_LINK_MODE_MASK_NBITS
);
3249 static int mvneta_mac_link_state(struct net_device
*ndev
,
3250 struct phylink_link_state
*state
)
3252 struct mvneta_port
*pp
= netdev_priv(ndev
);
3255 gmac_stat
= mvreg_read(pp
, MVNETA_GMAC_STATUS
);
3257 if (gmac_stat
& MVNETA_GMAC_SPEED_1000
)
3258 state
->speed
= SPEED_1000
;
3259 else if (gmac_stat
& MVNETA_GMAC_SPEED_100
)
3260 state
->speed
= SPEED_100
;
3262 state
->speed
= SPEED_10
;
3264 state
->an_complete
= !!(gmac_stat
& MVNETA_GMAC_AN_COMPLETE
);
3265 state
->link
= !!(gmac_stat
& MVNETA_GMAC_LINK_UP
);
3266 state
->duplex
= !!(gmac_stat
& MVNETA_GMAC_FULL_DUPLEX
);
3269 if (gmac_stat
& MVNETA_GMAC_RX_FLOW_CTRL_ENABLE
)
3270 state
->pause
|= MLO_PAUSE_RX
;
3271 if (gmac_stat
& MVNETA_GMAC_TX_FLOW_CTRL_ENABLE
)
3272 state
->pause
|= MLO_PAUSE_TX
;
3277 static void mvneta_mac_an_restart(struct net_device
*ndev
)
3279 struct mvneta_port
*pp
= netdev_priv(ndev
);
3280 u32 gmac_an
= mvreg_read(pp
, MVNETA_GMAC_AUTONEG_CONFIG
);
3282 mvreg_write(pp
, MVNETA_GMAC_AUTONEG_CONFIG
,
3283 gmac_an
| MVNETA_GMAC_INBAND_RESTART_AN
);
3284 mvreg_write(pp
, MVNETA_GMAC_AUTONEG_CONFIG
,
3285 gmac_an
& ~MVNETA_GMAC_INBAND_RESTART_AN
);
3288 static void mvneta_mac_config(struct net_device
*ndev
, unsigned int mode
,
3289 const struct phylink_link_state
*state
)
3291 struct mvneta_port
*pp
= netdev_priv(ndev
);
3292 u32 new_ctrl0
, gmac_ctrl0
= mvreg_read(pp
, MVNETA_GMAC_CTRL_0
);
3293 u32 new_ctrl2
, gmac_ctrl2
= mvreg_read(pp
, MVNETA_GMAC_CTRL_2
);
3294 u32 new_clk
, gmac_clk
= mvreg_read(pp
, MVNETA_GMAC_CLOCK_DIVIDER
);
3295 u32 new_an
, gmac_an
= mvreg_read(pp
, MVNETA_GMAC_AUTONEG_CONFIG
);
3297 new_ctrl0
= gmac_ctrl0
& ~MVNETA_GMAC0_PORT_1000BASE_X
;
3298 new_ctrl2
= gmac_ctrl2
& ~(MVNETA_GMAC2_INBAND_AN_ENABLE
|
3299 MVNETA_GMAC2_PORT_RESET
);
3300 new_clk
= gmac_clk
& ~MVNETA_GMAC_1MS_CLOCK_ENABLE
;
3301 new_an
= gmac_an
& ~(MVNETA_GMAC_INBAND_AN_ENABLE
|
3302 MVNETA_GMAC_INBAND_RESTART_AN
|
3303 MVNETA_GMAC_CONFIG_MII_SPEED
|
3304 MVNETA_GMAC_CONFIG_GMII_SPEED
|
3305 MVNETA_GMAC_AN_SPEED_EN
|
3306 MVNETA_GMAC_ADVERT_SYM_FLOW_CTRL
|
3307 MVNETA_GMAC_CONFIG_FLOW_CTRL
|
3308 MVNETA_GMAC_AN_FLOW_CTRL_EN
|
3309 MVNETA_GMAC_CONFIG_FULL_DUPLEX
|
3310 MVNETA_GMAC_AN_DUPLEX_EN
);
3312 /* Even though it might look weird, when we're configured in
3313 * SGMII or QSGMII mode, the RGMII bit needs to be set.
3315 new_ctrl2
|= MVNETA_GMAC2_PORT_RGMII
;
3317 if (state
->interface
== PHY_INTERFACE_MODE_QSGMII
||
3318 state
->interface
== PHY_INTERFACE_MODE_SGMII
||
3319 phy_interface_mode_is_8023z(state
->interface
))
3320 new_ctrl2
|= MVNETA_GMAC2_PCS_ENABLE
;
3322 if (phylink_test(state
->advertising
, Pause
))
3323 new_an
|= MVNETA_GMAC_ADVERT_SYM_FLOW_CTRL
;
3324 if (state
->pause
& MLO_PAUSE_TXRX_MASK
)
3325 new_an
|= MVNETA_GMAC_CONFIG_FLOW_CTRL
;
3327 if (!phylink_autoneg_inband(mode
)) {
3328 /* Phy or fixed speed */
3330 new_an
|= MVNETA_GMAC_CONFIG_FULL_DUPLEX
;
3332 if (state
->speed
== SPEED_1000
)
3333 new_an
|= MVNETA_GMAC_CONFIG_GMII_SPEED
;
3334 else if (state
->speed
== SPEED_100
)
3335 new_an
|= MVNETA_GMAC_CONFIG_MII_SPEED
;
3336 } else if (state
->interface
== PHY_INTERFACE_MODE_SGMII
) {
3337 /* SGMII mode receives the state from the PHY */
3338 new_ctrl2
|= MVNETA_GMAC2_INBAND_AN_ENABLE
;
3339 new_clk
|= MVNETA_GMAC_1MS_CLOCK_ENABLE
;
3340 new_an
= (new_an
& ~(MVNETA_GMAC_FORCE_LINK_DOWN
|
3341 MVNETA_GMAC_FORCE_LINK_PASS
)) |
3342 MVNETA_GMAC_INBAND_AN_ENABLE
|
3343 MVNETA_GMAC_AN_SPEED_EN
|
3344 MVNETA_GMAC_AN_DUPLEX_EN
;
3346 /* 802.3z negotiation - only 1000base-X */
3347 new_ctrl0
|= MVNETA_GMAC0_PORT_1000BASE_X
;
3348 new_clk
|= MVNETA_GMAC_1MS_CLOCK_ENABLE
;
3349 new_an
= (new_an
& ~(MVNETA_GMAC_FORCE_LINK_DOWN
|
3350 MVNETA_GMAC_FORCE_LINK_PASS
)) |
3351 MVNETA_GMAC_INBAND_AN_ENABLE
|
3352 MVNETA_GMAC_CONFIG_GMII_SPEED
|
3353 /* The MAC only supports FD mode */
3354 MVNETA_GMAC_CONFIG_FULL_DUPLEX
;
3356 if (state
->pause
& MLO_PAUSE_AN
&& state
->an_enabled
)
3357 new_an
|= MVNETA_GMAC_AN_FLOW_CTRL_EN
;
3360 /* Armada 370 documentation says we can only change the port mode
3361 * and in-band enable when the link is down, so force it down
3362 * while making these changes. We also do this for GMAC_CTRL2 */
3363 if ((new_ctrl0
^ gmac_ctrl0
) & MVNETA_GMAC0_PORT_1000BASE_X
||
3364 (new_ctrl2
^ gmac_ctrl2
) & MVNETA_GMAC2_INBAND_AN_ENABLE
||
3365 (new_an
^ gmac_an
) & MVNETA_GMAC_INBAND_AN_ENABLE
) {
3366 mvreg_write(pp
, MVNETA_GMAC_AUTONEG_CONFIG
,
3367 (gmac_an
& ~MVNETA_GMAC_FORCE_LINK_PASS
) |
3368 MVNETA_GMAC_FORCE_LINK_DOWN
);
3371 if (new_ctrl0
!= gmac_ctrl0
)
3372 mvreg_write(pp
, MVNETA_GMAC_CTRL_0
, new_ctrl0
);
3373 if (new_ctrl2
!= gmac_ctrl2
)
3374 mvreg_write(pp
, MVNETA_GMAC_CTRL_2
, new_ctrl2
);
3375 if (new_clk
!= gmac_clk
)
3376 mvreg_write(pp
, MVNETA_GMAC_CLOCK_DIVIDER
, new_clk
);
3377 if (new_an
!= gmac_an
)
3378 mvreg_write(pp
, MVNETA_GMAC_AUTONEG_CONFIG
, new_an
);
3380 if (gmac_ctrl2
& MVNETA_GMAC2_PORT_RESET
) {
3381 while ((mvreg_read(pp
, MVNETA_GMAC_CTRL_2
) &
3382 MVNETA_GMAC2_PORT_RESET
) != 0)
3387 static void mvneta_set_eee(struct mvneta_port
*pp
, bool enable
)
3391 lpi_ctl1
= mvreg_read(pp
, MVNETA_LPI_CTRL_1
);
3393 lpi_ctl1
|= MVNETA_LPI_REQUEST_ENABLE
;
3395 lpi_ctl1
&= ~MVNETA_LPI_REQUEST_ENABLE
;
3396 mvreg_write(pp
, MVNETA_LPI_CTRL_1
, lpi_ctl1
);
3399 static void mvneta_mac_link_down(struct net_device
*ndev
, unsigned int mode
)
3401 struct mvneta_port
*pp
= netdev_priv(ndev
);
3404 mvneta_port_down(pp
);
3406 if (!phylink_autoneg_inband(mode
)) {
3407 val
= mvreg_read(pp
, MVNETA_GMAC_AUTONEG_CONFIG
);
3408 val
&= ~MVNETA_GMAC_FORCE_LINK_PASS
;
3409 val
|= MVNETA_GMAC_FORCE_LINK_DOWN
;
3410 mvreg_write(pp
, MVNETA_GMAC_AUTONEG_CONFIG
, val
);
3413 pp
->eee_active
= false;
3414 mvneta_set_eee(pp
, false);
3417 static void mvneta_mac_link_up(struct net_device
*ndev
, unsigned int mode
,
3418 struct phy_device
*phy
)
3420 struct mvneta_port
*pp
= netdev_priv(ndev
);
3423 if (!phylink_autoneg_inband(mode
)) {
3424 val
= mvreg_read(pp
, MVNETA_GMAC_AUTONEG_CONFIG
);
3425 val
&= ~MVNETA_GMAC_FORCE_LINK_DOWN
;
3426 val
|= MVNETA_GMAC_FORCE_LINK_PASS
;
3427 mvreg_write(pp
, MVNETA_GMAC_AUTONEG_CONFIG
, val
);
3432 if (phy
&& pp
->eee_enabled
) {
3433 pp
->eee_active
= phy_init_eee(phy
, 0) >= 0;
3434 mvneta_set_eee(pp
, pp
->eee_active
&& pp
->tx_lpi_enabled
);
3438 static const struct phylink_mac_ops mvneta_phylink_ops
= {
3439 .validate
= mvneta_validate
,
3440 .mac_link_state
= mvneta_mac_link_state
,
3441 .mac_an_restart
= mvneta_mac_an_restart
,
3442 .mac_config
= mvneta_mac_config
,
3443 .mac_link_down
= mvneta_mac_link_down
,
3444 .mac_link_up
= mvneta_mac_link_up
,
3447 static int mvneta_mdio_probe(struct mvneta_port
*pp
)
3449 struct ethtool_wolinfo wol
= { .cmd
= ETHTOOL_GWOL
};
3450 int err
= phylink_of_phy_connect(pp
->phylink
, pp
->dn
, 0);
3453 netdev_err(pp
->dev
, "could not attach PHY: %d\n", err
);
3455 phylink_ethtool_get_wol(pp
->phylink
, &wol
);
3456 device_set_wakeup_capable(&pp
->dev
->dev
, !!wol
.supported
);
3461 static void mvneta_mdio_remove(struct mvneta_port
*pp
)
3463 phylink_disconnect_phy(pp
->phylink
);
3466 /* Electing a CPU must be done in an atomic way: it should be done
3467 * after or before the removal/insertion of a CPU and this function is
3470 static void mvneta_percpu_elect(struct mvneta_port
*pp
)
3472 int elected_cpu
= 0, max_cpu
, cpu
, i
= 0;
3474 /* Use the cpu associated to the rxq when it is online, in all
3475 * the other cases, use the cpu 0 which can't be offline.
3477 if (cpu_online(pp
->rxq_def
))
3478 elected_cpu
= pp
->rxq_def
;
3480 max_cpu
= num_present_cpus();
3482 for_each_online_cpu(cpu
) {
3483 int rxq_map
= 0, txq_map
= 0;
3486 for (rxq
= 0; rxq
< rxq_number
; rxq
++)
3487 if ((rxq
% max_cpu
) == cpu
)
3488 rxq_map
|= MVNETA_CPU_RXQ_ACCESS(rxq
);
3490 if (cpu
== elected_cpu
)
3491 /* Map the default receive queue queue to the
3494 rxq_map
|= MVNETA_CPU_RXQ_ACCESS(pp
->rxq_def
);
3496 /* We update the TX queue map only if we have one
3497 * queue. In this case we associate the TX queue to
3498 * the CPU bound to the default RX queue
3500 if (txq_number
== 1)
3501 txq_map
= (cpu
== elected_cpu
) ?
3502 MVNETA_CPU_TXQ_ACCESS(1) : 0;
3504 txq_map
= mvreg_read(pp
, MVNETA_CPU_MAP(cpu
)) &
3505 MVNETA_CPU_TXQ_ACCESS_ALL_MASK
;
3507 mvreg_write(pp
, MVNETA_CPU_MAP(cpu
), rxq_map
| txq_map
);
3509 /* Update the interrupt mask on each CPU according the
3512 smp_call_function_single(cpu
, mvneta_percpu_unmask_interrupt
,
3519 static int mvneta_cpu_online(unsigned int cpu
, struct hlist_node
*node
)
3522 struct mvneta_port
*pp
= hlist_entry_safe(node
, struct mvneta_port
,
3524 struct mvneta_pcpu_port
*port
= per_cpu_ptr(pp
->ports
, cpu
);
3527 spin_lock(&pp
->lock
);
3529 * Configuring the driver for a new CPU while the driver is
3530 * stopping is racy, so just avoid it.
3532 if (pp
->is_stopped
) {
3533 spin_unlock(&pp
->lock
);
3536 netif_tx_stop_all_queues(pp
->dev
);
3539 * We have to synchronise on tha napi of each CPU except the one
3540 * just being woken up
3542 for_each_online_cpu(other_cpu
) {
3543 if (other_cpu
!= cpu
) {
3544 struct mvneta_pcpu_port
*other_port
=
3545 per_cpu_ptr(pp
->ports
, other_cpu
);
3547 napi_synchronize(&other_port
->napi
);
3551 /* Mask all ethernet port interrupts */
3552 on_each_cpu(mvneta_percpu_mask_interrupt
, pp
, true);
3553 napi_enable(&port
->napi
);
3556 * Enable per-CPU interrupts on the CPU that is
3559 mvneta_percpu_enable(pp
);
3562 * Enable per-CPU interrupt on the one CPU we care
3565 mvneta_percpu_elect(pp
);
3567 /* Unmask all ethernet port interrupts */
3568 on_each_cpu(mvneta_percpu_unmask_interrupt
, pp
, true);
3569 mvreg_write(pp
, MVNETA_INTR_MISC_MASK
,
3570 MVNETA_CAUSE_PHY_STATUS_CHANGE
|
3571 MVNETA_CAUSE_LINK_CHANGE
);
3572 netif_tx_start_all_queues(pp
->dev
);
3573 spin_unlock(&pp
->lock
);
3577 static int mvneta_cpu_down_prepare(unsigned int cpu
, struct hlist_node
*node
)
3579 struct mvneta_port
*pp
= hlist_entry_safe(node
, struct mvneta_port
,
3581 struct mvneta_pcpu_port
*port
= per_cpu_ptr(pp
->ports
, cpu
);
3584 * Thanks to this lock we are sure that any pending cpu election is
3587 spin_lock(&pp
->lock
);
3588 /* Mask all ethernet port interrupts */
3589 on_each_cpu(mvneta_percpu_mask_interrupt
, pp
, true);
3590 spin_unlock(&pp
->lock
);
3592 napi_synchronize(&port
->napi
);
3593 napi_disable(&port
->napi
);
3594 /* Disable per-CPU interrupts on the CPU that is brought down. */
3595 mvneta_percpu_disable(pp
);
3599 static int mvneta_cpu_dead(unsigned int cpu
, struct hlist_node
*node
)
3601 struct mvneta_port
*pp
= hlist_entry_safe(node
, struct mvneta_port
,
3604 /* Check if a new CPU must be elected now this on is down */
3605 spin_lock(&pp
->lock
);
3606 mvneta_percpu_elect(pp
);
3607 spin_unlock(&pp
->lock
);
3608 /* Unmask all ethernet port interrupts */
3609 on_each_cpu(mvneta_percpu_unmask_interrupt
, pp
, true);
3610 mvreg_write(pp
, MVNETA_INTR_MISC_MASK
,
3611 MVNETA_CAUSE_PHY_STATUS_CHANGE
|
3612 MVNETA_CAUSE_LINK_CHANGE
);
3613 netif_tx_start_all_queues(pp
->dev
);
3617 static int mvneta_open(struct net_device
*dev
)
3619 struct mvneta_port
*pp
= netdev_priv(dev
);
3622 pp
->pkt_size
= MVNETA_RX_PKT_SIZE(pp
->dev
->mtu
);
3623 pp
->frag_size
= SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(pp
->pkt_size
)) +
3624 SKB_DATA_ALIGN(sizeof(struct skb_shared_info
));
3626 ret
= mvneta_setup_rxqs(pp
);
3630 ret
= mvneta_setup_txqs(pp
);
3632 goto err_cleanup_rxqs
;
3634 /* Connect to port interrupt line */
3635 if (pp
->neta_armada3700
)
3636 ret
= request_irq(pp
->dev
->irq
, mvneta_isr
, 0,
3639 ret
= request_percpu_irq(pp
->dev
->irq
, mvneta_percpu_isr
,
3640 dev
->name
, pp
->ports
);
3642 netdev_err(pp
->dev
, "cannot request irq %d\n", pp
->dev
->irq
);
3643 goto err_cleanup_txqs
;
3646 if (!pp
->neta_armada3700
) {
3647 /* Enable per-CPU interrupt on all the CPU to handle our RX
3650 on_each_cpu(mvneta_percpu_enable
, pp
, true);
3652 pp
->is_stopped
= false;
3653 /* Register a CPU notifier to handle the case where our CPU
3654 * might be taken offline.
3656 ret
= cpuhp_state_add_instance_nocalls(online_hpstate
,
3661 ret
= cpuhp_state_add_instance_nocalls(CPUHP_NET_MVNETA_DEAD
,
3664 goto err_free_online_hp
;
3667 /* In default link is down */
3668 netif_carrier_off(pp
->dev
);
3670 ret
= mvneta_mdio_probe(pp
);
3672 netdev_err(dev
, "cannot probe MDIO bus\n");
3673 goto err_free_dead_hp
;
3676 mvneta_start_dev(pp
);
3681 if (!pp
->neta_armada3700
)
3682 cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD
,
3685 if (!pp
->neta_armada3700
)
3686 cpuhp_state_remove_instance_nocalls(online_hpstate
,
3689 if (pp
->neta_armada3700
) {
3690 free_irq(pp
->dev
->irq
, pp
);
3692 on_each_cpu(mvneta_percpu_disable
, pp
, true);
3693 free_percpu_irq(pp
->dev
->irq
, pp
->ports
);
3696 mvneta_cleanup_txqs(pp
);
3698 mvneta_cleanup_rxqs(pp
);
3702 /* Stop the port, free port interrupt line */
3703 static int mvneta_stop(struct net_device
*dev
)
3705 struct mvneta_port
*pp
= netdev_priv(dev
);
3707 if (!pp
->neta_armada3700
) {
3708 /* Inform that we are stopping so we don't want to setup the
3709 * driver for new CPUs in the notifiers. The code of the
3710 * notifier for CPU online is protected by the same spinlock,
3711 * so when we get the lock, the notifer work is done.
3713 spin_lock(&pp
->lock
);
3714 pp
->is_stopped
= true;
3715 spin_unlock(&pp
->lock
);
3717 mvneta_stop_dev(pp
);
3718 mvneta_mdio_remove(pp
);
3720 cpuhp_state_remove_instance_nocalls(online_hpstate
,
3722 cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD
,
3724 on_each_cpu(mvneta_percpu_disable
, pp
, true);
3725 free_percpu_irq(dev
->irq
, pp
->ports
);
3727 mvneta_stop_dev(pp
);
3728 mvneta_mdio_remove(pp
);
3729 free_irq(dev
->irq
, pp
);
3732 mvneta_cleanup_rxqs(pp
);
3733 mvneta_cleanup_txqs(pp
);
3738 static int mvneta_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
3740 struct mvneta_port
*pp
= netdev_priv(dev
);
3742 return phylink_mii_ioctl(pp
->phylink
, ifr
, cmd
);
3745 /* Ethtool methods */
3747 /* Set link ksettings (phy address, speed) for ethtools */
3749 mvneta_ethtool_set_link_ksettings(struct net_device
*ndev
,
3750 const struct ethtool_link_ksettings
*cmd
)
3752 struct mvneta_port
*pp
= netdev_priv(ndev
);
3754 return phylink_ethtool_ksettings_set(pp
->phylink
, cmd
);
3757 /* Get link ksettings for ethtools */
3759 mvneta_ethtool_get_link_ksettings(struct net_device
*ndev
,
3760 struct ethtool_link_ksettings
*cmd
)
3762 struct mvneta_port
*pp
= netdev_priv(ndev
);
3764 return phylink_ethtool_ksettings_get(pp
->phylink
, cmd
);
3767 static int mvneta_ethtool_nway_reset(struct net_device
*dev
)
3769 struct mvneta_port
*pp
= netdev_priv(dev
);
3771 return phylink_ethtool_nway_reset(pp
->phylink
);
3774 /* Set interrupt coalescing for ethtools */
3775 static int mvneta_ethtool_set_coalesce(struct net_device
*dev
,
3776 struct ethtool_coalesce
*c
)
3778 struct mvneta_port
*pp
= netdev_priv(dev
);
3781 for (queue
= 0; queue
< rxq_number
; queue
++) {
3782 struct mvneta_rx_queue
*rxq
= &pp
->rxqs
[queue
];
3783 rxq
->time_coal
= c
->rx_coalesce_usecs
;
3784 rxq
->pkts_coal
= c
->rx_max_coalesced_frames
;
3785 mvneta_rx_pkts_coal_set(pp
, rxq
, rxq
->pkts_coal
);
3786 mvneta_rx_time_coal_set(pp
, rxq
, rxq
->time_coal
);
3789 for (queue
= 0; queue
< txq_number
; queue
++) {
3790 struct mvneta_tx_queue
*txq
= &pp
->txqs
[queue
];
3791 txq
->done_pkts_coal
= c
->tx_max_coalesced_frames
;
3792 mvneta_tx_done_pkts_coal_set(pp
, txq
, txq
->done_pkts_coal
);
3798 /* get coalescing for ethtools */
3799 static int mvneta_ethtool_get_coalesce(struct net_device
*dev
,
3800 struct ethtool_coalesce
*c
)
3802 struct mvneta_port
*pp
= netdev_priv(dev
);
3804 c
->rx_coalesce_usecs
= pp
->rxqs
[0].time_coal
;
3805 c
->rx_max_coalesced_frames
= pp
->rxqs
[0].pkts_coal
;
3807 c
->tx_max_coalesced_frames
= pp
->txqs
[0].done_pkts_coal
;
3812 static void mvneta_ethtool_get_drvinfo(struct net_device
*dev
,
3813 struct ethtool_drvinfo
*drvinfo
)
3815 strlcpy(drvinfo
->driver
, MVNETA_DRIVER_NAME
,
3816 sizeof(drvinfo
->driver
));
3817 strlcpy(drvinfo
->version
, MVNETA_DRIVER_VERSION
,
3818 sizeof(drvinfo
->version
));
3819 strlcpy(drvinfo
->bus_info
, dev_name(&dev
->dev
),
3820 sizeof(drvinfo
->bus_info
));
3824 static void mvneta_ethtool_get_ringparam(struct net_device
*netdev
,
3825 struct ethtool_ringparam
*ring
)
3827 struct mvneta_port
*pp
= netdev_priv(netdev
);
3829 ring
->rx_max_pending
= MVNETA_MAX_RXD
;
3830 ring
->tx_max_pending
= MVNETA_MAX_TXD
;
3831 ring
->rx_pending
= pp
->rx_ring_size
;
3832 ring
->tx_pending
= pp
->tx_ring_size
;
3835 static int mvneta_ethtool_set_ringparam(struct net_device
*dev
,
3836 struct ethtool_ringparam
*ring
)
3838 struct mvneta_port
*pp
= netdev_priv(dev
);
3840 if ((ring
->rx_pending
== 0) || (ring
->tx_pending
== 0))
3842 pp
->rx_ring_size
= ring
->rx_pending
< MVNETA_MAX_RXD
?
3843 ring
->rx_pending
: MVNETA_MAX_RXD
;
3845 pp
->tx_ring_size
= clamp_t(u16
, ring
->tx_pending
,
3846 MVNETA_MAX_SKB_DESCS
* 2, MVNETA_MAX_TXD
);
3847 if (pp
->tx_ring_size
!= ring
->tx_pending
)
3848 netdev_warn(dev
, "TX queue size set to %u (requested %u)\n",
3849 pp
->tx_ring_size
, ring
->tx_pending
);
3851 if (netif_running(dev
)) {
3853 if (mvneta_open(dev
)) {
3855 "error on opening device after ring param change\n");
3863 static void mvneta_ethtool_get_pauseparam(struct net_device
*dev
,
3864 struct ethtool_pauseparam
*pause
)
3866 struct mvneta_port
*pp
= netdev_priv(dev
);
3868 phylink_ethtool_get_pauseparam(pp
->phylink
, pause
);
3871 static int mvneta_ethtool_set_pauseparam(struct net_device
*dev
,
3872 struct ethtool_pauseparam
*pause
)
3874 struct mvneta_port
*pp
= netdev_priv(dev
);
3876 return phylink_ethtool_set_pauseparam(pp
->phylink
, pause
);
3879 static void mvneta_ethtool_get_strings(struct net_device
*netdev
, u32 sset
,
3882 if (sset
== ETH_SS_STATS
) {
3885 for (i
= 0; i
< ARRAY_SIZE(mvneta_statistics
); i
++)
3886 memcpy(data
+ i
* ETH_GSTRING_LEN
,
3887 mvneta_statistics
[i
].name
, ETH_GSTRING_LEN
);
3891 static void mvneta_ethtool_update_stats(struct mvneta_port
*pp
)
3893 const struct mvneta_statistic
*s
;
3894 void __iomem
*base
= pp
->base
;
3899 for (i
= 0, s
= mvneta_statistics
;
3900 s
< mvneta_statistics
+ ARRAY_SIZE(mvneta_statistics
);
3906 val
= readl_relaxed(base
+ s
->offset
);
3909 /* Docs say to read low 32-bit then high */
3910 low
= readl_relaxed(base
+ s
->offset
);
3911 high
= readl_relaxed(base
+ s
->offset
+ 4);
3912 val
= (u64
)high
<< 32 | low
;
3915 switch (s
->offset
) {
3916 case ETHTOOL_STAT_EEE_WAKEUP
:
3917 val
= phylink_get_eee_err(pp
->phylink
);
3923 pp
->ethtool_stats
[i
] += val
;
3927 static void mvneta_ethtool_get_stats(struct net_device
*dev
,
3928 struct ethtool_stats
*stats
, u64
*data
)
3930 struct mvneta_port
*pp
= netdev_priv(dev
);
3933 mvneta_ethtool_update_stats(pp
);
3935 for (i
= 0; i
< ARRAY_SIZE(mvneta_statistics
); i
++)
3936 *data
++ = pp
->ethtool_stats
[i
];
3939 static int mvneta_ethtool_get_sset_count(struct net_device
*dev
, int sset
)
3941 if (sset
== ETH_SS_STATS
)
3942 return ARRAY_SIZE(mvneta_statistics
);
3946 static u32
mvneta_ethtool_get_rxfh_indir_size(struct net_device
*dev
)
3948 return MVNETA_RSS_LU_TABLE_SIZE
;
3951 static int mvneta_ethtool_get_rxnfc(struct net_device
*dev
,
3952 struct ethtool_rxnfc
*info
,
3953 u32
*rules __always_unused
)
3955 switch (info
->cmd
) {
3956 case ETHTOOL_GRXRINGS
:
3957 info
->data
= rxq_number
;
3966 static int mvneta_config_rss(struct mvneta_port
*pp
)
3971 netif_tx_stop_all_queues(pp
->dev
);
3973 on_each_cpu(mvneta_percpu_mask_interrupt
, pp
, true);
3975 /* We have to synchronise on the napi of each CPU */
3976 for_each_online_cpu(cpu
) {
3977 struct mvneta_pcpu_port
*pcpu_port
=
3978 per_cpu_ptr(pp
->ports
, cpu
);
3980 napi_synchronize(&pcpu_port
->napi
);
3981 napi_disable(&pcpu_port
->napi
);
3984 pp
->rxq_def
= pp
->indir
[0];
3986 /* Update unicast mapping */
3987 mvneta_set_rx_mode(pp
->dev
);
3989 /* Update val of portCfg register accordingly with all RxQueue types */
3990 val
= MVNETA_PORT_CONFIG_DEFL_VALUE(pp
->rxq_def
);
3991 mvreg_write(pp
, MVNETA_PORT_CONFIG
, val
);
3993 /* Update the elected CPU matching the new rxq_def */
3994 spin_lock(&pp
->lock
);
3995 mvneta_percpu_elect(pp
);
3996 spin_unlock(&pp
->lock
);
3998 /* We have to synchronise on the napi of each CPU */
3999 for_each_online_cpu(cpu
) {
4000 struct mvneta_pcpu_port
*pcpu_port
=
4001 per_cpu_ptr(pp
->ports
, cpu
);
4003 napi_enable(&pcpu_port
->napi
);
4006 netif_tx_start_all_queues(pp
->dev
);
4011 static int mvneta_ethtool_set_rxfh(struct net_device
*dev
, const u32
*indir
,
4012 const u8
*key
, const u8 hfunc
)
4014 struct mvneta_port
*pp
= netdev_priv(dev
);
4016 /* Current code for Armada 3700 doesn't support RSS features yet */
4017 if (pp
->neta_armada3700
)
4020 /* We require at least one supported parameter to be changed
4021 * and no change in any of the unsupported parameters
4024 (hfunc
!= ETH_RSS_HASH_NO_CHANGE
&& hfunc
!= ETH_RSS_HASH_TOP
))
4030 memcpy(pp
->indir
, indir
, MVNETA_RSS_LU_TABLE_SIZE
);
4032 return mvneta_config_rss(pp
);
4035 static int mvneta_ethtool_get_rxfh(struct net_device
*dev
, u32
*indir
, u8
*key
,
4038 struct mvneta_port
*pp
= netdev_priv(dev
);
4040 /* Current code for Armada 3700 doesn't support RSS features yet */
4041 if (pp
->neta_armada3700
)
4045 *hfunc
= ETH_RSS_HASH_TOP
;
4050 memcpy(indir
, pp
->indir
, MVNETA_RSS_LU_TABLE_SIZE
);
4055 static void mvneta_ethtool_get_wol(struct net_device
*dev
,
4056 struct ethtool_wolinfo
*wol
)
4058 struct mvneta_port
*pp
= netdev_priv(dev
);
4060 phylink_ethtool_get_wol(pp
->phylink
, wol
);
4063 static int mvneta_ethtool_set_wol(struct net_device
*dev
,
4064 struct ethtool_wolinfo
*wol
)
4066 struct mvneta_port
*pp
= netdev_priv(dev
);
4069 ret
= phylink_ethtool_set_wol(pp
->phylink
, wol
);
4071 device_set_wakeup_enable(&dev
->dev
, !!wol
->wolopts
);
4076 static int mvneta_ethtool_get_module_info(struct net_device
*dev
,
4077 struct ethtool_modinfo
*modinfo
)
4079 struct mvneta_port
*pp
= netdev_priv(dev
);
4081 return phylink_ethtool_get_module_info(pp
->phylink
, modinfo
);
4084 static int mvneta_ethtool_get_module_eeprom(struct net_device
*dev
,
4085 struct ethtool_eeprom
*ee
, u8
*buf
)
4087 struct mvneta_port
*pp
= netdev_priv(dev
);
4089 return phylink_ethtool_get_module_eeprom(pp
->phylink
, ee
, buf
);
4092 static int mvneta_ethtool_get_eee(struct net_device
*dev
,
4093 struct ethtool_eee
*eee
)
4095 struct mvneta_port
*pp
= netdev_priv(dev
);
4098 lpi_ctl0
= mvreg_read(pp
, MVNETA_LPI_CTRL_0
);
4100 eee
->eee_enabled
= pp
->eee_enabled
;
4101 eee
->eee_active
= pp
->eee_active
;
4102 eee
->tx_lpi_enabled
= pp
->tx_lpi_enabled
;
4103 eee
->tx_lpi_timer
= (lpi_ctl0
) >> 8; // * scale;
4105 return phylink_ethtool_get_eee(pp
->phylink
, eee
);
4108 static int mvneta_ethtool_set_eee(struct net_device
*dev
,
4109 struct ethtool_eee
*eee
)
4111 struct mvneta_port
*pp
= netdev_priv(dev
);
4114 /* The Armada 37x documents do not give limits for this other than
4115 * it being an 8-bit register. */
4116 if (eee
->tx_lpi_enabled
&&
4117 (eee
->tx_lpi_timer
< 0 || eee
->tx_lpi_timer
> 255))
4120 lpi_ctl0
= mvreg_read(pp
, MVNETA_LPI_CTRL_0
);
4121 lpi_ctl0
&= ~(0xff << 8);
4122 lpi_ctl0
|= eee
->tx_lpi_timer
<< 8;
4123 mvreg_write(pp
, MVNETA_LPI_CTRL_0
, lpi_ctl0
);
4125 pp
->eee_enabled
= eee
->eee_enabled
;
4126 pp
->tx_lpi_enabled
= eee
->tx_lpi_enabled
;
4128 mvneta_set_eee(pp
, eee
->tx_lpi_enabled
&& eee
->eee_enabled
);
4130 return phylink_ethtool_set_eee(pp
->phylink
, eee
);
4133 static const struct net_device_ops mvneta_netdev_ops
= {
4134 .ndo_open
= mvneta_open
,
4135 .ndo_stop
= mvneta_stop
,
4136 .ndo_start_xmit
= mvneta_tx
,
4137 .ndo_set_rx_mode
= mvneta_set_rx_mode
,
4138 .ndo_set_mac_address
= mvneta_set_mac_addr
,
4139 .ndo_change_mtu
= mvneta_change_mtu
,
4140 .ndo_fix_features
= mvneta_fix_features
,
4141 .ndo_get_stats64
= mvneta_get_stats64
,
4142 .ndo_do_ioctl
= mvneta_ioctl
,
4145 static const struct ethtool_ops mvneta_eth_tool_ops
= {
4146 .nway_reset
= mvneta_ethtool_nway_reset
,
4147 .get_link
= ethtool_op_get_link
,
4148 .set_coalesce
= mvneta_ethtool_set_coalesce
,
4149 .get_coalesce
= mvneta_ethtool_get_coalesce
,
4150 .get_drvinfo
= mvneta_ethtool_get_drvinfo
,
4151 .get_ringparam
= mvneta_ethtool_get_ringparam
,
4152 .set_ringparam
= mvneta_ethtool_set_ringparam
,
4153 .get_pauseparam
= mvneta_ethtool_get_pauseparam
,
4154 .set_pauseparam
= mvneta_ethtool_set_pauseparam
,
4155 .get_strings
= mvneta_ethtool_get_strings
,
4156 .get_ethtool_stats
= mvneta_ethtool_get_stats
,
4157 .get_sset_count
= mvneta_ethtool_get_sset_count
,
4158 .get_rxfh_indir_size
= mvneta_ethtool_get_rxfh_indir_size
,
4159 .get_rxnfc
= mvneta_ethtool_get_rxnfc
,
4160 .get_rxfh
= mvneta_ethtool_get_rxfh
,
4161 .set_rxfh
= mvneta_ethtool_set_rxfh
,
4162 .get_link_ksettings
= mvneta_ethtool_get_link_ksettings
,
4163 .set_link_ksettings
= mvneta_ethtool_set_link_ksettings
,
4164 .get_wol
= mvneta_ethtool_get_wol
,
4165 .set_wol
= mvneta_ethtool_set_wol
,
4166 .get_module_info
= mvneta_ethtool_get_module_info
,
4167 .get_module_eeprom
= mvneta_ethtool_get_module_eeprom
,
4168 .get_eee
= mvneta_ethtool_get_eee
,
4169 .set_eee
= mvneta_ethtool_set_eee
,
4173 static int mvneta_init(struct device
*dev
, struct mvneta_port
*pp
)
4178 mvneta_port_disable(pp
);
4180 /* Set port default values */
4181 mvneta_defaults_set(pp
);
4183 pp
->txqs
= devm_kcalloc(dev
, txq_number
, sizeof(*pp
->txqs
), GFP_KERNEL
);
4187 /* Initialize TX descriptor rings */
4188 for (queue
= 0; queue
< txq_number
; queue
++) {
4189 struct mvneta_tx_queue
*txq
= &pp
->txqs
[queue
];
4191 txq
->size
= pp
->tx_ring_size
;
4192 txq
->done_pkts_coal
= MVNETA_TXDONE_COAL_PKTS
;
4195 pp
->rxqs
= devm_kcalloc(dev
, rxq_number
, sizeof(*pp
->rxqs
), GFP_KERNEL
);
4199 /* Create Rx descriptor rings */
4200 for (queue
= 0; queue
< rxq_number
; queue
++) {
4201 struct mvneta_rx_queue
*rxq
= &pp
->rxqs
[queue
];
4203 rxq
->size
= pp
->rx_ring_size
;
4204 rxq
->pkts_coal
= MVNETA_RX_COAL_PKTS
;
4205 rxq
->time_coal
= MVNETA_RX_COAL_USEC
;
4207 = devm_kmalloc_array(pp
->dev
->dev
.parent
,
4209 sizeof(*rxq
->buf_virt_addr
),
4211 if (!rxq
->buf_virt_addr
)
4218 /* platform glue : initialize decoding windows */
4219 static void mvneta_conf_mbus_windows(struct mvneta_port
*pp
,
4220 const struct mbus_dram_target_info
*dram
)
4226 for (i
= 0; i
< 6; i
++) {
4227 mvreg_write(pp
, MVNETA_WIN_BASE(i
), 0);
4228 mvreg_write(pp
, MVNETA_WIN_SIZE(i
), 0);
4231 mvreg_write(pp
, MVNETA_WIN_REMAP(i
), 0);
4238 for (i
= 0; i
< dram
->num_cs
; i
++) {
4239 const struct mbus_dram_window
*cs
= dram
->cs
+ i
;
4241 mvreg_write(pp
, MVNETA_WIN_BASE(i
),
4242 (cs
->base
& 0xffff0000) |
4243 (cs
->mbus_attr
<< 8) |
4244 dram
->mbus_dram_target_id
);
4246 mvreg_write(pp
, MVNETA_WIN_SIZE(i
),
4247 (cs
->size
- 1) & 0xffff0000);
4249 win_enable
&= ~(1 << i
);
4250 win_protect
|= 3 << (2 * i
);
4253 /* For Armada3700 open default 4GB Mbus window, leaving
4254 * arbitration of target/attribute to a different layer
4257 mvreg_write(pp
, MVNETA_WIN_SIZE(0), 0xffff0000);
4258 win_enable
&= ~BIT(0);
4262 mvreg_write(pp
, MVNETA_BASE_ADDR_ENABLE
, win_enable
);
4263 mvreg_write(pp
, MVNETA_ACCESS_PROTECT_ENABLE
, win_protect
);
4266 /* Power up the port */
4267 static int mvneta_port_power_up(struct mvneta_port
*pp
, int phy_mode
)
4269 /* MAC Cause register should be cleared */
4270 mvreg_write(pp
, MVNETA_UNIT_INTR_CAUSE
, 0);
4272 if (phy_mode
== PHY_INTERFACE_MODE_QSGMII
)
4273 mvreg_write(pp
, MVNETA_SERDES_CFG
, MVNETA_QSGMII_SERDES_PROTO
);
4274 else if (phy_mode
== PHY_INTERFACE_MODE_SGMII
||
4275 phy_mode
== PHY_INTERFACE_MODE_1000BASEX
)
4276 mvreg_write(pp
, MVNETA_SERDES_CFG
, MVNETA_SGMII_SERDES_PROTO
);
4277 else if (!phy_interface_mode_is_rgmii(phy_mode
))
4283 /* Device initialization routine */
4284 static int mvneta_probe(struct platform_device
*pdev
)
4286 struct resource
*res
;
4287 struct device_node
*dn
= pdev
->dev
.of_node
;
4288 struct device_node
*bm_node
;
4289 struct mvneta_port
*pp
;
4290 struct net_device
*dev
;
4291 struct phylink
*phylink
;
4292 const char *dt_mac_addr
;
4293 char hw_mac_addr
[ETH_ALEN
];
4294 const char *mac_from
;
4300 dev
= alloc_etherdev_mqs(sizeof(struct mvneta_port
), txq_number
, rxq_number
);
4304 dev
->irq
= irq_of_parse_and_map(dn
, 0);
4305 if (dev
->irq
== 0) {
4307 goto err_free_netdev
;
4310 phy_mode
= of_get_phy_mode(dn
);
4312 dev_err(&pdev
->dev
, "incorrect phy-mode\n");
4317 phylink
= phylink_create(dev
, pdev
->dev
.fwnode
, phy_mode
,
4318 &mvneta_phylink_ops
);
4319 if (IS_ERR(phylink
)) {
4320 err
= PTR_ERR(phylink
);
4324 dev
->tx_queue_len
= MVNETA_MAX_TXD
;
4325 dev
->watchdog_timeo
= 5 * HZ
;
4326 dev
->netdev_ops
= &mvneta_netdev_ops
;
4328 dev
->ethtool_ops
= &mvneta_eth_tool_ops
;
4330 pp
= netdev_priv(dev
);
4331 spin_lock_init(&pp
->lock
);
4332 pp
->phylink
= phylink
;
4333 pp
->phy_interface
= phy_mode
;
4336 pp
->rxq_def
= rxq_def
;
4338 /* Set RX packet offset correction for platforms, whose
4339 * NET_SKB_PAD, exceeds 64B. It should be 64B for 64-bit
4340 * platforms and 0B for 32-bit ones.
4342 pp
->rx_offset_correction
=
4343 max(0, NET_SKB_PAD
- MVNETA_RX_PKT_OFFSET_CORRECTION
);
4345 pp
->indir
[0] = rxq_def
;
4347 /* Get special SoC configurations */
4348 if (of_device_is_compatible(dn
, "marvell,armada-3700-neta"))
4349 pp
->neta_armada3700
= true;
4351 pp
->clk
= devm_clk_get(&pdev
->dev
, "core");
4352 if (IS_ERR(pp
->clk
))
4353 pp
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
4354 if (IS_ERR(pp
->clk
)) {
4355 err
= PTR_ERR(pp
->clk
);
4356 goto err_free_phylink
;
4359 clk_prepare_enable(pp
->clk
);
4361 pp
->clk_bus
= devm_clk_get(&pdev
->dev
, "bus");
4362 if (!IS_ERR(pp
->clk_bus
))
4363 clk_prepare_enable(pp
->clk_bus
);
4365 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
4366 pp
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
4367 if (IS_ERR(pp
->base
)) {
4368 err
= PTR_ERR(pp
->base
);
4372 /* Alloc per-cpu port structure */
4373 pp
->ports
= alloc_percpu(struct mvneta_pcpu_port
);
4379 /* Alloc per-cpu stats */
4380 pp
->stats
= netdev_alloc_pcpu_stats(struct mvneta_pcpu_stats
);
4383 goto err_free_ports
;
4386 dt_mac_addr
= of_get_mac_address(dn
);
4388 mac_from
= "device tree";
4389 memcpy(dev
->dev_addr
, dt_mac_addr
, ETH_ALEN
);
4391 mvneta_get_mac_addr(pp
, hw_mac_addr
);
4392 if (is_valid_ether_addr(hw_mac_addr
)) {
4393 mac_from
= "hardware";
4394 memcpy(dev
->dev_addr
, hw_mac_addr
, ETH_ALEN
);
4396 mac_from
= "random";
4397 eth_hw_addr_random(dev
);
4401 if (!of_property_read_u32(dn
, "tx-csum-limit", &tx_csum_limit
)) {
4402 if (tx_csum_limit
< 0 ||
4403 tx_csum_limit
> MVNETA_TX_CSUM_MAX_SIZE
) {
4404 tx_csum_limit
= MVNETA_TX_CSUM_DEF_SIZE
;
4405 dev_info(&pdev
->dev
,
4406 "Wrong TX csum limit in DT, set to %dB\n",
4407 MVNETA_TX_CSUM_DEF_SIZE
);
4409 } else if (of_device_is_compatible(dn
, "marvell,armada-370-neta")) {
4410 tx_csum_limit
= MVNETA_TX_CSUM_DEF_SIZE
;
4412 tx_csum_limit
= MVNETA_TX_CSUM_MAX_SIZE
;
4415 pp
->tx_csum_limit
= tx_csum_limit
;
4417 pp
->dram_target_info
= mv_mbus_dram_info();
4418 /* Armada3700 requires setting default configuration of Mbus
4419 * windows, however without using filled mbus_dram_target_info
4422 if (pp
->dram_target_info
|| pp
->neta_armada3700
)
4423 mvneta_conf_mbus_windows(pp
, pp
->dram_target_info
);
4425 pp
->tx_ring_size
= MVNETA_MAX_TXD
;
4426 pp
->rx_ring_size
= MVNETA_MAX_RXD
;
4429 SET_NETDEV_DEV(dev
, &pdev
->dev
);
4431 pp
->id
= global_port_id
++;
4433 /* Obtain access to BM resources if enabled and already initialized */
4434 bm_node
= of_parse_phandle(dn
, "buffer-manager", 0);
4435 if (bm_node
&& bm_node
->data
) {
4436 pp
->bm_priv
= bm_node
->data
;
4437 err
= mvneta_bm_port_init(pdev
, pp
);
4439 dev_info(&pdev
->dev
, "use SW buffer management\n");
4443 of_node_put(bm_node
);
4445 err
= mvneta_init(&pdev
->dev
, pp
);
4449 err
= mvneta_port_power_up(pp
, phy_mode
);
4451 dev_err(&pdev
->dev
, "can't power up port\n");
4455 /* Armada3700 network controller does not support per-cpu
4456 * operation, so only single NAPI should be initialized.
4458 if (pp
->neta_armada3700
) {
4459 netif_napi_add(dev
, &pp
->napi
, mvneta_poll
, NAPI_POLL_WEIGHT
);
4461 for_each_present_cpu(cpu
) {
4462 struct mvneta_pcpu_port
*port
=
4463 per_cpu_ptr(pp
->ports
, cpu
);
4465 netif_napi_add(dev
, &port
->napi
, mvneta_poll
,
4471 dev
->features
= NETIF_F_SG
| NETIF_F_IP_CSUM
| NETIF_F_IPV6_CSUM
| NETIF_F_TSO
;
4472 dev
->hw_features
|= dev
->features
;
4473 dev
->vlan_features
|= dev
->features
;
4474 dev
->priv_flags
|= IFF_LIVE_ADDR_CHANGE
;
4475 dev
->gso_max_segs
= MVNETA_MAX_TSO_SEGS
;
4477 /* MTU range: 68 - 9676 */
4478 dev
->min_mtu
= ETH_MIN_MTU
;
4479 /* 9676 == 9700 - 20 and rounding to 8 */
4480 dev
->max_mtu
= 9676;
4482 err
= register_netdev(dev
);
4484 dev_err(&pdev
->dev
, "failed to register\n");
4485 goto err_free_stats
;
4488 netdev_info(dev
, "Using %s mac address %pM\n", mac_from
,
4491 platform_set_drvdata(pdev
, pp
->dev
);
4496 unregister_netdev(dev
);
4498 mvneta_bm_pool_destroy(pp
->bm_priv
, pp
->pool_long
, 1 << pp
->id
);
4499 mvneta_bm_pool_destroy(pp
->bm_priv
, pp
->pool_short
,
4503 free_percpu(pp
->stats
);
4505 free_percpu(pp
->ports
);
4507 clk_disable_unprepare(pp
->clk_bus
);
4508 clk_disable_unprepare(pp
->clk
);
4511 phylink_destroy(pp
->phylink
);
4513 irq_dispose_mapping(dev
->irq
);
4519 /* Device removal routine */
4520 static int mvneta_remove(struct platform_device
*pdev
)
4522 struct net_device
*dev
= platform_get_drvdata(pdev
);
4523 struct mvneta_port
*pp
= netdev_priv(dev
);
4525 unregister_netdev(dev
);
4526 clk_disable_unprepare(pp
->clk_bus
);
4527 clk_disable_unprepare(pp
->clk
);
4528 free_percpu(pp
->ports
);
4529 free_percpu(pp
->stats
);
4530 irq_dispose_mapping(dev
->irq
);
4531 phylink_destroy(pp
->phylink
);
4535 mvneta_bm_pool_destroy(pp
->bm_priv
, pp
->pool_long
, 1 << pp
->id
);
4536 mvneta_bm_pool_destroy(pp
->bm_priv
, pp
->pool_short
,
4543 #ifdef CONFIG_PM_SLEEP
4544 static int mvneta_suspend(struct device
*device
)
4546 struct net_device
*dev
= dev_get_drvdata(device
);
4547 struct mvneta_port
*pp
= netdev_priv(dev
);
4550 if (netif_running(dev
))
4553 netif_device_detach(dev
);
4554 clk_disable_unprepare(pp
->clk_bus
);
4555 clk_disable_unprepare(pp
->clk
);
4559 static int mvneta_resume(struct device
*device
)
4561 struct platform_device
*pdev
= to_platform_device(device
);
4562 struct net_device
*dev
= dev_get_drvdata(device
);
4563 struct mvneta_port
*pp
= netdev_priv(dev
);
4566 clk_prepare_enable(pp
->clk
);
4567 if (!IS_ERR(pp
->clk_bus
))
4568 clk_prepare_enable(pp
->clk_bus
);
4569 if (pp
->dram_target_info
|| pp
->neta_armada3700
)
4570 mvneta_conf_mbus_windows(pp
, pp
->dram_target_info
);
4572 err
= mvneta_bm_port_init(pdev
, pp
);
4574 dev_info(&pdev
->dev
, "use SW buffer management\n");
4578 mvneta_defaults_set(pp
);
4579 err
= mvneta_port_power_up(pp
, pp
->phy_interface
);
4581 dev_err(device
, "can't power up port\n");
4585 netif_device_attach(dev
);
4587 if (netif_running(dev
)) {
4589 mvneta_set_rx_mode(dev
);
4597 static SIMPLE_DEV_PM_OPS(mvneta_pm_ops
, mvneta_suspend
, mvneta_resume
);
4599 static const struct of_device_id mvneta_match
[] = {
4600 { .compatible
= "marvell,armada-370-neta" },
4601 { .compatible
= "marvell,armada-xp-neta" },
4602 { .compatible
= "marvell,armada-3700-neta" },
4605 MODULE_DEVICE_TABLE(of
, mvneta_match
);
4607 static struct platform_driver mvneta_driver
= {
4608 .probe
= mvneta_probe
,
4609 .remove
= mvneta_remove
,
4611 .name
= MVNETA_DRIVER_NAME
,
4612 .of_match_table
= mvneta_match
,
4613 .pm
= &mvneta_pm_ops
,
4617 static int __init
mvneta_driver_init(void)
4621 ret
= cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN
, "net/mvmeta:online",
4623 mvneta_cpu_down_prepare
);
4626 online_hpstate
= ret
;
4627 ret
= cpuhp_setup_state_multi(CPUHP_NET_MVNETA_DEAD
, "net/mvneta:dead",
4628 NULL
, mvneta_cpu_dead
);
4632 ret
= platform_driver_register(&mvneta_driver
);
4638 cpuhp_remove_multi_state(CPUHP_NET_MVNETA_DEAD
);
4640 cpuhp_remove_multi_state(online_hpstate
);
4644 module_init(mvneta_driver_init
);
4646 static void __exit
mvneta_driver_exit(void)
4648 platform_driver_unregister(&mvneta_driver
);
4649 cpuhp_remove_multi_state(CPUHP_NET_MVNETA_DEAD
);
4650 cpuhp_remove_multi_state(online_hpstate
);
4652 module_exit(mvneta_driver_exit
);
4654 MODULE_DESCRIPTION("Marvell NETA Ethernet Driver - www.marvell.com");
4655 MODULE_AUTHOR("Rami Rosen <rosenr@marvell.com>, Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
4656 MODULE_LICENSE("GPL");
4658 module_param(rxq_number
, int, S_IRUGO
);
4659 module_param(txq_number
, int, S_IRUGO
);
4661 module_param(rxq_def
, int, S_IRUGO
);
4662 module_param(rx_copybreak
, int, S_IRUGO
| S_IWUSR
);