net: mvneta: remove tests for impossible cases in the tx_done path
[deliverable/linux.git] / drivers / net / ethernet / marvell / mvneta.c
CommitLineData
c5aff182
TP
1/*
2 * Driver for Marvell NETA network card for Armada XP and Armada 370 SoCs.
3 *
4 * Copyright (C) 2012 Marvell
5 *
6 * Rami Rosen <rosenr@marvell.com>
7 * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
8 *
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.
12 */
13
14#include <linux/kernel.h>
c5aff182
TP
15#include <linux/netdevice.h>
16#include <linux/etherdevice.h>
17#include <linux/platform_device.h>
18#include <linux/skbuff.h>
19#include <linux/inetdevice.h>
20#include <linux/mbus.h>
21#include <linux/module.h>
22#include <linux/interrupt.h>
23#include <net/ip.h>
24#include <net/ipv6.h>
25#include <linux/of.h>
26#include <linux/of_irq.h>
27#include <linux/of_mdio.h>
28#include <linux/of_net.h>
29#include <linux/of_address.h>
30#include <linux/phy.h>
189dd626 31#include <linux/clk.h>
c5aff182
TP
32
33/* Registers */
34#define MVNETA_RXQ_CONFIG_REG(q) (0x1400 + ((q) << 2))
35#define MVNETA_RXQ_HW_BUF_ALLOC BIT(1)
36#define MVNETA_RXQ_PKT_OFFSET_ALL_MASK (0xf << 8)
37#define MVNETA_RXQ_PKT_OFFSET_MASK(offs) ((offs) << 8)
38#define MVNETA_RXQ_THRESHOLD_REG(q) (0x14c0 + ((q) << 2))
39#define MVNETA_RXQ_NON_OCCUPIED(v) ((v) << 16)
40#define MVNETA_RXQ_BASE_ADDR_REG(q) (0x1480 + ((q) << 2))
41#define MVNETA_RXQ_SIZE_REG(q) (0x14a0 + ((q) << 2))
42#define MVNETA_RXQ_BUF_SIZE_SHIFT 19
43#define MVNETA_RXQ_BUF_SIZE_MASK (0x1fff << 19)
44#define MVNETA_RXQ_STATUS_REG(q) (0x14e0 + ((q) << 2))
45#define MVNETA_RXQ_OCCUPIED_ALL_MASK 0x3fff
46#define MVNETA_RXQ_STATUS_UPDATE_REG(q) (0x1500 + ((q) << 2))
47#define MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT 16
48#define MVNETA_RXQ_ADD_NON_OCCUPIED_MAX 255
49#define MVNETA_PORT_RX_RESET 0x1cc0
50#define MVNETA_PORT_RX_DMA_RESET BIT(0)
51#define MVNETA_PHY_ADDR 0x2000
52#define MVNETA_PHY_ADDR_MASK 0x1f
53#define MVNETA_MBUS_RETRY 0x2010
54#define MVNETA_UNIT_INTR_CAUSE 0x2080
55#define MVNETA_UNIT_CONTROL 0x20B0
56#define MVNETA_PHY_POLLING_ENABLE BIT(1)
57#define MVNETA_WIN_BASE(w) (0x2200 + ((w) << 3))
58#define MVNETA_WIN_SIZE(w) (0x2204 + ((w) << 3))
59#define MVNETA_WIN_REMAP(w) (0x2280 + ((w) << 2))
60#define MVNETA_BASE_ADDR_ENABLE 0x2290
61#define MVNETA_PORT_CONFIG 0x2400
62#define MVNETA_UNI_PROMISC_MODE BIT(0)
63#define MVNETA_DEF_RXQ(q) ((q) << 1)
64#define MVNETA_DEF_RXQ_ARP(q) ((q) << 4)
65#define MVNETA_TX_UNSET_ERR_SUM BIT(12)
66#define MVNETA_DEF_RXQ_TCP(q) ((q) << 16)
67#define MVNETA_DEF_RXQ_UDP(q) ((q) << 19)
68#define MVNETA_DEF_RXQ_BPDU(q) ((q) << 22)
69#define MVNETA_RX_CSUM_WITH_PSEUDO_HDR BIT(25)
70#define MVNETA_PORT_CONFIG_DEFL_VALUE(q) (MVNETA_DEF_RXQ(q) | \
71 MVNETA_DEF_RXQ_ARP(q) | \
72 MVNETA_DEF_RXQ_TCP(q) | \
73 MVNETA_DEF_RXQ_UDP(q) | \
74 MVNETA_DEF_RXQ_BPDU(q) | \
75 MVNETA_TX_UNSET_ERR_SUM | \
76 MVNETA_RX_CSUM_WITH_PSEUDO_HDR)
77#define MVNETA_PORT_CONFIG_EXTEND 0x2404
78#define MVNETA_MAC_ADDR_LOW 0x2414
79#define MVNETA_MAC_ADDR_HIGH 0x2418
80#define MVNETA_SDMA_CONFIG 0x241c
81#define MVNETA_SDMA_BRST_SIZE_16 4
c5aff182
TP
82#define MVNETA_RX_BRST_SZ_MASK(burst) ((burst) << 1)
83#define MVNETA_RX_NO_DATA_SWAP BIT(4)
84#define MVNETA_TX_NO_DATA_SWAP BIT(5)
9ad8fef6 85#define MVNETA_DESC_SWAP BIT(6)
c5aff182
TP
86#define MVNETA_TX_BRST_SZ_MASK(burst) ((burst) << 22)
87#define MVNETA_PORT_STATUS 0x2444
88#define MVNETA_TX_IN_PRGRS BIT(1)
89#define MVNETA_TX_FIFO_EMPTY BIT(8)
90#define MVNETA_RX_MIN_FRAME_SIZE 0x247c
5445eaf3
APR
91#define MVNETA_SGMII_SERDES_CFG 0x24A0
92#define MVNETA_SGMII_SERDES_PROTO 0x0cc7
c5aff182
TP
93#define MVNETA_TYPE_PRIO 0x24bc
94#define MVNETA_FORCE_UNI BIT(21)
95#define MVNETA_TXQ_CMD_1 0x24e4
96#define MVNETA_TXQ_CMD 0x2448
97#define MVNETA_TXQ_DISABLE_SHIFT 8
98#define MVNETA_TXQ_ENABLE_MASK 0x000000ff
99#define MVNETA_ACC_MODE 0x2500
100#define MVNETA_CPU_MAP(cpu) (0x2540 + ((cpu) << 2))
101#define MVNETA_CPU_RXQ_ACCESS_ALL_MASK 0x000000ff
102#define MVNETA_CPU_TXQ_ACCESS_ALL_MASK 0x0000ff00
103#define MVNETA_RXQ_TIME_COAL_REG(q) (0x2580 + ((q) << 2))
40ba35e7 104
105/* Exception Interrupt Port/Queue Cause register */
106
c5aff182 107#define MVNETA_INTR_NEW_CAUSE 0x25a0
c5aff182 108#define MVNETA_INTR_NEW_MASK 0x25a4
40ba35e7 109
110/* bits 0..7 = TXQ SENT, one bit per queue.
111 * bits 8..15 = RXQ OCCUP, one bit per queue.
112 * bits 16..23 = RXQ FREE, one bit per queue.
113 * bit 29 = OLD_REG_SUM, see old reg ?
114 * bit 30 = TX_ERR_SUM, one bit for 4 ports
115 * bit 31 = MISC_SUM, one bit for 4 ports
116 */
117#define MVNETA_TX_INTR_MASK(nr_txqs) (((1 << nr_txqs) - 1) << 0)
118#define MVNETA_TX_INTR_MASK_ALL (0xff << 0)
119#define MVNETA_RX_INTR_MASK(nr_rxqs) (((1 << nr_rxqs) - 1) << 8)
120#define MVNETA_RX_INTR_MASK_ALL (0xff << 8)
121
c5aff182
TP
122#define MVNETA_INTR_OLD_CAUSE 0x25a8
123#define MVNETA_INTR_OLD_MASK 0x25ac
40ba35e7 124
125/* Data Path Port/Queue Cause Register */
c5aff182
TP
126#define MVNETA_INTR_MISC_CAUSE 0x25b0
127#define MVNETA_INTR_MISC_MASK 0x25b4
40ba35e7 128
129#define MVNETA_CAUSE_PHY_STATUS_CHANGE BIT(0)
130#define MVNETA_CAUSE_LINK_CHANGE BIT(1)
131#define MVNETA_CAUSE_PTP BIT(4)
132
133#define MVNETA_CAUSE_INTERNAL_ADDR_ERR BIT(7)
134#define MVNETA_CAUSE_RX_OVERRUN BIT(8)
135#define MVNETA_CAUSE_RX_CRC_ERROR BIT(9)
136#define MVNETA_CAUSE_RX_LARGE_PKT BIT(10)
137#define MVNETA_CAUSE_TX_UNDERUN BIT(11)
138#define MVNETA_CAUSE_PRBS_ERR BIT(12)
139#define MVNETA_CAUSE_PSC_SYNC_CHANGE BIT(13)
140#define MVNETA_CAUSE_SERDES_SYNC_ERR BIT(14)
141
142#define MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT 16
143#define MVNETA_CAUSE_BMU_ALLOC_ERR_ALL_MASK (0xF << MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT)
144#define MVNETA_CAUSE_BMU_ALLOC_ERR_MASK(pool) (1 << (MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT + (pool)))
145
146#define MVNETA_CAUSE_TXQ_ERROR_SHIFT 24
147#define MVNETA_CAUSE_TXQ_ERROR_ALL_MASK (0xFF << MVNETA_CAUSE_TXQ_ERROR_SHIFT)
148#define MVNETA_CAUSE_TXQ_ERROR_MASK(q) (1 << (MVNETA_CAUSE_TXQ_ERROR_SHIFT + (q)))
149
c5aff182
TP
150#define MVNETA_INTR_ENABLE 0x25b8
151#define MVNETA_TXQ_INTR_ENABLE_ALL_MASK 0x0000ff00
40ba35e7 152#define MVNETA_RXQ_INTR_ENABLE_ALL_MASK 0xff000000 // note: neta says it's 0x000000FF
153
c5aff182
TP
154#define MVNETA_RXQ_CMD 0x2680
155#define MVNETA_RXQ_DISABLE_SHIFT 8
156#define MVNETA_RXQ_ENABLE_MASK 0x000000ff
157#define MVETH_TXQ_TOKEN_COUNT_REG(q) (0x2700 + ((q) << 4))
158#define MVETH_TXQ_TOKEN_CFG_REG(q) (0x2704 + ((q) << 4))
159#define MVNETA_GMAC_CTRL_0 0x2c00
160#define MVNETA_GMAC_MAX_RX_SIZE_SHIFT 2
161#define MVNETA_GMAC_MAX_RX_SIZE_MASK 0x7ffc
162#define MVNETA_GMAC0_PORT_ENABLE BIT(0)
163#define MVNETA_GMAC_CTRL_2 0x2c08
164#define MVNETA_GMAC2_PSC_ENABLE BIT(3)
165#define MVNETA_GMAC2_PORT_RGMII BIT(4)
166#define MVNETA_GMAC2_PORT_RESET BIT(6)
167#define MVNETA_GMAC_STATUS 0x2c10
168#define MVNETA_GMAC_LINK_UP BIT(0)
169#define MVNETA_GMAC_SPEED_1000 BIT(1)
170#define MVNETA_GMAC_SPEED_100 BIT(2)
171#define MVNETA_GMAC_FULL_DUPLEX BIT(3)
172#define MVNETA_GMAC_RX_FLOW_CTRL_ENABLE BIT(4)
173#define MVNETA_GMAC_TX_FLOW_CTRL_ENABLE BIT(5)
174#define MVNETA_GMAC_RX_FLOW_CTRL_ACTIVE BIT(6)
175#define MVNETA_GMAC_TX_FLOW_CTRL_ACTIVE BIT(7)
176#define MVNETA_GMAC_AUTONEG_CONFIG 0x2c0c
177#define MVNETA_GMAC_FORCE_LINK_DOWN BIT(0)
178#define MVNETA_GMAC_FORCE_LINK_PASS BIT(1)
179#define MVNETA_GMAC_CONFIG_MII_SPEED BIT(5)
180#define MVNETA_GMAC_CONFIG_GMII_SPEED BIT(6)
71408602 181#define MVNETA_GMAC_AN_SPEED_EN BIT(7)
c5aff182 182#define MVNETA_GMAC_CONFIG_FULL_DUPLEX BIT(12)
71408602 183#define MVNETA_GMAC_AN_DUPLEX_EN BIT(13)
c5aff182
TP
184#define MVNETA_MIB_COUNTERS_BASE 0x3080
185#define MVNETA_MIB_LATE_COLLISION 0x7c
186#define MVNETA_DA_FILT_SPEC_MCAST 0x3400
187#define MVNETA_DA_FILT_OTH_MCAST 0x3500
188#define MVNETA_DA_FILT_UCAST_BASE 0x3600
189#define MVNETA_TXQ_BASE_ADDR_REG(q) (0x3c00 + ((q) << 2))
190#define MVNETA_TXQ_SIZE_REG(q) (0x3c20 + ((q) << 2))
191#define MVNETA_TXQ_SENT_THRESH_ALL_MASK 0x3fff0000
192#define MVNETA_TXQ_SENT_THRESH_MASK(coal) ((coal) << 16)
193#define MVNETA_TXQ_UPDATE_REG(q) (0x3c60 + ((q) << 2))
194#define MVNETA_TXQ_DEC_SENT_SHIFT 16
195#define MVNETA_TXQ_STATUS_REG(q) (0x3c40 + ((q) << 2))
196#define MVNETA_TXQ_SENT_DESC_SHIFT 16
197#define MVNETA_TXQ_SENT_DESC_MASK 0x3fff0000
198#define MVNETA_PORT_TX_RESET 0x3cf0
199#define MVNETA_PORT_TX_DMA_RESET BIT(0)
200#define MVNETA_TX_MTU 0x3e0c
201#define MVNETA_TX_TOKEN_SIZE 0x3e14
202#define MVNETA_TX_TOKEN_SIZE_MAX 0xffffffff
203#define MVNETA_TXQ_TOKEN_SIZE_REG(q) (0x3e40 + ((q) << 2))
204#define MVNETA_TXQ_TOKEN_SIZE_MAX 0x7fffffff
205
206#define MVNETA_CAUSE_TXQ_SENT_DESC_ALL_MASK 0xff
207
208/* Descriptor ring Macros */
209#define MVNETA_QUEUE_NEXT_DESC(q, index) \
210 (((index) < (q)->last_desc) ? ((index) + 1) : 0)
211
212/* Various constants */
213
214/* Coalescing */
215#define MVNETA_TXDONE_COAL_PKTS 16
216#define MVNETA_RX_COAL_PKTS 32
217#define MVNETA_RX_COAL_USEC 100
218
c5aff182
TP
219/* Napi polling weight */
220#define MVNETA_RX_POLL_WEIGHT 64
221
6a20c175 222/* The two bytes Marvell header. Either contains a special value used
c5aff182
TP
223 * by Marvell switches when a specific hardware mode is enabled (not
224 * supported by this driver) or is filled automatically by zeroes on
225 * the RX side. Those two bytes being at the front of the Ethernet
226 * header, they allow to have the IP header aligned on a 4 bytes
227 * boundary automatically: the hardware skips those two bytes on its
228 * own.
229 */
230#define MVNETA_MH_SIZE 2
231
232#define MVNETA_VLAN_TAG_LEN 4
233
234#define MVNETA_CPU_D_CACHE_LINE_SIZE 32
235#define MVNETA_TX_CSUM_MAX_SIZE 9800
236#define MVNETA_ACC_MODE_EXT 1
237
238/* Timeout constants */
239#define MVNETA_TX_DISABLE_TIMEOUT_MSEC 1000
240#define MVNETA_RX_DISABLE_TIMEOUT_MSEC 1000
241#define MVNETA_TX_FIFO_EMPTY_TIMEOUT 10000
242
243#define MVNETA_TX_MTU_MAX 0x3ffff
244
245/* Max number of Rx descriptors */
246#define MVNETA_MAX_RXD 128
247
248/* Max number of Tx descriptors */
249#define MVNETA_MAX_TXD 532
250
251/* descriptor aligned size */
252#define MVNETA_DESC_ALIGNED_SIZE 32
253
254#define MVNETA_RX_PKT_SIZE(mtu) \
255 ALIGN((mtu) + MVNETA_MH_SIZE + MVNETA_VLAN_TAG_LEN + \
256 ETH_HLEN + ETH_FCS_LEN, \
257 MVNETA_CPU_D_CACHE_LINE_SIZE)
258
259#define MVNETA_RX_BUF_SIZE(pkt_size) ((pkt_size) + NET_SKB_PAD)
260
74c41b04 261struct mvneta_pcpu_stats {
c5aff182 262 struct u64_stats_sync syncp;
74c41b04 263 u64 rx_packets;
264 u64 rx_bytes;
265 u64 tx_packets;
266 u64 tx_bytes;
c5aff182
TP
267};
268
269struct mvneta_port {
270 int pkt_size;
271 void __iomem *base;
272 struct mvneta_rx_queue *rxqs;
273 struct mvneta_tx_queue *txqs;
c5aff182
TP
274 struct net_device *dev;
275
276 u32 cause_rx_tx;
277 struct napi_struct napi;
278
c5aff182
TP
279 /* Napi weight */
280 int weight;
281
282 /* Core clock */
189dd626 283 struct clk *clk;
c5aff182
TP
284 u8 mcast_count[256];
285 u16 tx_ring_size;
286 u16 rx_ring_size;
74c41b04 287 struct mvneta_pcpu_stats *stats;
c5aff182
TP
288
289 struct mii_bus *mii_bus;
290 struct phy_device *phy_dev;
291 phy_interface_t phy_interface;
292 struct device_node *phy_node;
293 unsigned int link;
294 unsigned int duplex;
295 unsigned int speed;
296};
297
6a20c175 298/* The mvneta_tx_desc and mvneta_rx_desc structures describe the
c5aff182
TP
299 * layout of the transmit and reception DMA descriptors, and their
300 * layout is therefore defined by the hardware design
301 */
6083ed44 302
c5aff182
TP
303#define MVNETA_TX_L3_OFF_SHIFT 0
304#define MVNETA_TX_IP_HLEN_SHIFT 8
305#define MVNETA_TX_L4_UDP BIT(16)
306#define MVNETA_TX_L3_IP6 BIT(17)
307#define MVNETA_TXD_IP_CSUM BIT(18)
308#define MVNETA_TXD_Z_PAD BIT(19)
309#define MVNETA_TXD_L_DESC BIT(20)
310#define MVNETA_TXD_F_DESC BIT(21)
311#define MVNETA_TXD_FLZ_DESC (MVNETA_TXD_Z_PAD | \
312 MVNETA_TXD_L_DESC | \
313 MVNETA_TXD_F_DESC)
314#define MVNETA_TX_L4_CSUM_FULL BIT(30)
315#define MVNETA_TX_L4_CSUM_NOT BIT(31)
316
c5aff182
TP
317#define MVNETA_RXD_ERR_CRC 0x0
318#define MVNETA_RXD_ERR_SUMMARY BIT(16)
319#define MVNETA_RXD_ERR_OVERRUN BIT(17)
320#define MVNETA_RXD_ERR_LEN BIT(18)
321#define MVNETA_RXD_ERR_RESOURCE (BIT(17) | BIT(18))
322#define MVNETA_RXD_ERR_CODE_MASK (BIT(17) | BIT(18))
323#define MVNETA_RXD_L3_IP4 BIT(25)
324#define MVNETA_RXD_FIRST_LAST_DESC (BIT(26) | BIT(27))
325#define MVNETA_RXD_L4_CSUM_OK BIT(30)
326
9ad8fef6 327#if defined(__LITTLE_ENDIAN)
6083ed44
TP
328struct mvneta_tx_desc {
329 u32 command; /* Options used by HW for packet transmitting.*/
330 u16 reserverd1; /* csum_l4 (for future use) */
331 u16 data_size; /* Data size of transmitted packet in bytes */
332 u32 buf_phys_addr; /* Physical addr of transmitted buffer */
333 u32 reserved2; /* hw_cmd - (for future use, PMT) */
334 u32 reserved3[4]; /* Reserved - (for future use) */
335};
336
337struct mvneta_rx_desc {
338 u32 status; /* Info about received packet */
c5aff182
TP
339 u16 reserved1; /* pnc_info - (for future use, PnC) */
340 u16 data_size; /* Size of received packet in bytes */
6083ed44 341
c5aff182
TP
342 u32 buf_phys_addr; /* Physical address of the buffer */
343 u32 reserved2; /* pnc_flow_id (for future use, PnC) */
6083ed44 344
c5aff182
TP
345 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
346 u16 reserved3; /* prefetch_cmd, for future use */
347 u16 reserved4; /* csum_l4 - (for future use, PnC) */
6083ed44 348
c5aff182
TP
349 u32 reserved5; /* pnc_extra PnC (for future use, PnC) */
350 u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */
351};
9ad8fef6
TP
352#else
353struct mvneta_tx_desc {
354 u16 data_size; /* Data size of transmitted packet in bytes */
355 u16 reserverd1; /* csum_l4 (for future use) */
356 u32 command; /* Options used by HW for packet transmitting.*/
357 u32 reserved2; /* hw_cmd - (for future use, PMT) */
358 u32 buf_phys_addr; /* Physical addr of transmitted buffer */
359 u32 reserved3[4]; /* Reserved - (for future use) */
360};
361
362struct mvneta_rx_desc {
363 u16 data_size; /* Size of received packet in bytes */
364 u16 reserved1; /* pnc_info - (for future use, PnC) */
365 u32 status; /* Info about received packet */
366
367 u32 reserved2; /* pnc_flow_id (for future use, PnC) */
368 u32 buf_phys_addr; /* Physical address of the buffer */
369
370 u16 reserved4; /* csum_l4 - (for future use, PnC) */
371 u16 reserved3; /* prefetch_cmd, for future use */
372 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
373
374 u32 reserved5; /* pnc_extra PnC (for future use, PnC) */
375 u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */
376};
377#endif
c5aff182
TP
378
379struct mvneta_tx_queue {
380 /* Number of this TX queue, in the range 0-7 */
381 u8 id;
382
383 /* Number of TX DMA descriptors in the descriptor ring */
384 int size;
385
386 /* Number of currently used TX DMA descriptor in the
6a20c175
TP
387 * descriptor ring
388 */
c5aff182
TP
389 int count;
390
391 /* Array of transmitted skb */
392 struct sk_buff **tx_skb;
393
394 /* Index of last TX DMA descriptor that was inserted */
395 int txq_put_index;
396
397 /* Index of the TX DMA descriptor to be cleaned up */
398 int txq_get_index;
399
400 u32 done_pkts_coal;
401
402 /* Virtual address of the TX DMA descriptors array */
403 struct mvneta_tx_desc *descs;
404
405 /* DMA address of the TX DMA descriptors array */
406 dma_addr_t descs_phys;
407
408 /* Index of the last TX DMA descriptor */
409 int last_desc;
410
411 /* Index of the next TX DMA descriptor to process */
412 int next_desc_to_proc;
413};
414
415struct mvneta_rx_queue {
416 /* rx queue number, in the range 0-7 */
417 u8 id;
418
419 /* num of rx descriptors in the rx descriptor ring */
420 int size;
421
422 /* counter of times when mvneta_refill() failed */
423 int missed;
424
425 u32 pkts_coal;
426 u32 time_coal;
427
428 /* Virtual address of the RX DMA descriptors array */
429 struct mvneta_rx_desc *descs;
430
431 /* DMA address of the RX DMA descriptors array */
432 dma_addr_t descs_phys;
433
434 /* Index of the last RX DMA descriptor */
435 int last_desc;
436
437 /* Index of the next RX DMA descriptor to process */
438 int next_desc_to_proc;
439};
440
441static int rxq_number = 8;
442static int txq_number = 8;
443
444static int rxq_def;
c5aff182
TP
445
446#define MVNETA_DRIVER_NAME "mvneta"
447#define MVNETA_DRIVER_VERSION "1.0"
448
449/* Utility/helper methods */
450
451/* Write helper method */
452static void mvreg_write(struct mvneta_port *pp, u32 offset, u32 data)
453{
454 writel(data, pp->base + offset);
455}
456
457/* Read helper method */
458static u32 mvreg_read(struct mvneta_port *pp, u32 offset)
459{
460 return readl(pp->base + offset);
461}
462
463/* Increment txq get counter */
464static void mvneta_txq_inc_get(struct mvneta_tx_queue *txq)
465{
466 txq->txq_get_index++;
467 if (txq->txq_get_index == txq->size)
468 txq->txq_get_index = 0;
469}
470
471/* Increment txq put counter */
472static void mvneta_txq_inc_put(struct mvneta_tx_queue *txq)
473{
474 txq->txq_put_index++;
475 if (txq->txq_put_index == txq->size)
476 txq->txq_put_index = 0;
477}
478
479
480/* Clear all MIB counters */
481static void mvneta_mib_counters_clear(struct mvneta_port *pp)
482{
483 int i;
484 u32 dummy;
485
486 /* Perform dummy reads from MIB counters */
487 for (i = 0; i < MVNETA_MIB_LATE_COLLISION; i += 4)
488 dummy = mvreg_read(pp, (MVNETA_MIB_COUNTERS_BASE + i));
489}
490
491/* Get System Network Statistics */
492struct rtnl_link_stats64 *mvneta_get_stats64(struct net_device *dev,
493 struct rtnl_link_stats64 *stats)
494{
495 struct mvneta_port *pp = netdev_priv(dev);
496 unsigned int start;
74c41b04 497 int cpu;
c5aff182 498
74c41b04 499 for_each_possible_cpu(cpu) {
500 struct mvneta_pcpu_stats *cpu_stats;
501 u64 rx_packets;
502 u64 rx_bytes;
503 u64 tx_packets;
504 u64 tx_bytes;
c5aff182 505
74c41b04 506 cpu_stats = per_cpu_ptr(pp->stats, cpu);
507 do {
508 start = u64_stats_fetch_begin_bh(&cpu_stats->syncp);
509 rx_packets = cpu_stats->rx_packets;
510 rx_bytes = cpu_stats->rx_bytes;
511 tx_packets = cpu_stats->tx_packets;
512 tx_bytes = cpu_stats->tx_bytes;
513 } while (u64_stats_fetch_retry_bh(&cpu_stats->syncp, start));
c5aff182 514
74c41b04 515 stats->rx_packets += rx_packets;
516 stats->rx_bytes += rx_bytes;
517 stats->tx_packets += tx_packets;
518 stats->tx_bytes += tx_bytes;
519 }
c5aff182
TP
520
521 stats->rx_errors = dev->stats.rx_errors;
522 stats->rx_dropped = dev->stats.rx_dropped;
523
524 stats->tx_dropped = dev->stats.tx_dropped;
525
526 return stats;
527}
528
529/* Rx descriptors helper methods */
530
6a20c175 531/* Checks whether the given RX descriptor is both the first and the
c5aff182
TP
532 * last descriptor for the RX packet. Each RX packet is currently
533 * received through a single RX descriptor, so not having each RX
534 * descriptor with its first and last bits set is an error
535 */
536static int mvneta_rxq_desc_is_first_last(struct mvneta_rx_desc *desc)
537{
538 return (desc->status & MVNETA_RXD_FIRST_LAST_DESC) ==
539 MVNETA_RXD_FIRST_LAST_DESC;
540}
541
542/* Add number of descriptors ready to receive new packets */
543static void mvneta_rxq_non_occup_desc_add(struct mvneta_port *pp,
544 struct mvneta_rx_queue *rxq,
545 int ndescs)
546{
547 /* Only MVNETA_RXQ_ADD_NON_OCCUPIED_MAX (255) descriptors can
6a20c175
TP
548 * be added at once
549 */
c5aff182
TP
550 while (ndescs > MVNETA_RXQ_ADD_NON_OCCUPIED_MAX) {
551 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
552 (MVNETA_RXQ_ADD_NON_OCCUPIED_MAX <<
553 MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
554 ndescs -= MVNETA_RXQ_ADD_NON_OCCUPIED_MAX;
555 }
556
557 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
558 (ndescs << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
559}
560
561/* Get number of RX descriptors occupied by received packets */
562static int mvneta_rxq_busy_desc_num_get(struct mvneta_port *pp,
563 struct mvneta_rx_queue *rxq)
564{
565 u32 val;
566
567 val = mvreg_read(pp, MVNETA_RXQ_STATUS_REG(rxq->id));
568 return val & MVNETA_RXQ_OCCUPIED_ALL_MASK;
569}
570
6a20c175 571/* Update num of rx desc called upon return from rx path or
c5aff182
TP
572 * from mvneta_rxq_drop_pkts().
573 */
574static void mvneta_rxq_desc_num_update(struct mvneta_port *pp,
575 struct mvneta_rx_queue *rxq,
576 int rx_done, int rx_filled)
577{
578 u32 val;
579
580 if ((rx_done <= 0xff) && (rx_filled <= 0xff)) {
581 val = rx_done |
582 (rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT);
583 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
584 return;
585 }
586
587 /* Only 255 descriptors can be added at once */
588 while ((rx_done > 0) || (rx_filled > 0)) {
589 if (rx_done <= 0xff) {
590 val = rx_done;
591 rx_done = 0;
592 } else {
593 val = 0xff;
594 rx_done -= 0xff;
595 }
596 if (rx_filled <= 0xff) {
597 val |= rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
598 rx_filled = 0;
599 } else {
600 val |= 0xff << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
601 rx_filled -= 0xff;
602 }
603 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
604 }
605}
606
607/* Get pointer to next RX descriptor to be processed by SW */
608static struct mvneta_rx_desc *
609mvneta_rxq_next_desc_get(struct mvneta_rx_queue *rxq)
610{
611 int rx_desc = rxq->next_desc_to_proc;
612
613 rxq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(rxq, rx_desc);
614 return rxq->descs + rx_desc;
615}
616
617/* Change maximum receive size of the port. */
618static void mvneta_max_rx_size_set(struct mvneta_port *pp, int max_rx_size)
619{
620 u32 val;
621
622 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
623 val &= ~MVNETA_GMAC_MAX_RX_SIZE_MASK;
624 val |= ((max_rx_size - MVNETA_MH_SIZE) / 2) <<
625 MVNETA_GMAC_MAX_RX_SIZE_SHIFT;
626 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
627}
628
629
630/* Set rx queue offset */
631static void mvneta_rxq_offset_set(struct mvneta_port *pp,
632 struct mvneta_rx_queue *rxq,
633 int offset)
634{
635 u32 val;
636
637 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
638 val &= ~MVNETA_RXQ_PKT_OFFSET_ALL_MASK;
639
640 /* Offset is in */
641 val |= MVNETA_RXQ_PKT_OFFSET_MASK(offset >> 3);
642 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
643}
644
645
646/* Tx descriptors helper methods */
647
648/* Update HW with number of TX descriptors to be sent */
649static void mvneta_txq_pend_desc_add(struct mvneta_port *pp,
650 struct mvneta_tx_queue *txq,
651 int pend_desc)
652{
653 u32 val;
654
655 /* Only 255 descriptors can be added at once ; Assume caller
6a20c175
TP
656 * process TX desriptors in quanta less than 256
657 */
c5aff182
TP
658 val = pend_desc;
659 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
660}
661
662/* Get pointer to next TX descriptor to be processed (send) by HW */
663static struct mvneta_tx_desc *
664mvneta_txq_next_desc_get(struct mvneta_tx_queue *txq)
665{
666 int tx_desc = txq->next_desc_to_proc;
667
668 txq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(txq, tx_desc);
669 return txq->descs + tx_desc;
670}
671
672/* Release the last allocated TX descriptor. Useful to handle DMA
6a20c175
TP
673 * mapping failures in the TX path.
674 */
c5aff182
TP
675static void mvneta_txq_desc_put(struct mvneta_tx_queue *txq)
676{
677 if (txq->next_desc_to_proc == 0)
678 txq->next_desc_to_proc = txq->last_desc - 1;
679 else
680 txq->next_desc_to_proc--;
681}
682
683/* Set rxq buf size */
684static void mvneta_rxq_buf_size_set(struct mvneta_port *pp,
685 struct mvneta_rx_queue *rxq,
686 int buf_size)
687{
688 u32 val;
689
690 val = mvreg_read(pp, MVNETA_RXQ_SIZE_REG(rxq->id));
691
692 val &= ~MVNETA_RXQ_BUF_SIZE_MASK;
693 val |= ((buf_size >> 3) << MVNETA_RXQ_BUF_SIZE_SHIFT);
694
695 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), val);
696}
697
698/* Disable buffer management (BM) */
699static void mvneta_rxq_bm_disable(struct mvneta_port *pp,
700 struct mvneta_rx_queue *rxq)
701{
702 u32 val;
703
704 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
705 val &= ~MVNETA_RXQ_HW_BUF_ALLOC;
706 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
707}
708
709
710
711/* Sets the RGMII Enable bit (RGMIIEn) in port MAC control register */
03ce758e 712static void mvneta_gmac_rgmii_set(struct mvneta_port *pp, int enable)
c5aff182
TP
713{
714 u32 val;
715
716 val = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
717
718 if (enable)
719 val |= MVNETA_GMAC2_PORT_RGMII;
720 else
721 val &= ~MVNETA_GMAC2_PORT_RGMII;
722
723 mvreg_write(pp, MVNETA_GMAC_CTRL_2, val);
724}
725
726/* Config SGMII port */
03ce758e 727static void mvneta_port_sgmii_config(struct mvneta_port *pp)
c5aff182
TP
728{
729 u32 val;
730
731 val = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
732 val |= MVNETA_GMAC2_PSC_ENABLE;
733 mvreg_write(pp, MVNETA_GMAC_CTRL_2, val);
5445eaf3
APR
734
735 mvreg_write(pp, MVNETA_SGMII_SERDES_CFG, MVNETA_SGMII_SERDES_PROTO);
c5aff182
TP
736}
737
738/* Start the Ethernet port RX and TX activity */
739static void mvneta_port_up(struct mvneta_port *pp)
740{
741 int queue;
742 u32 q_map;
743
744 /* Enable all initialized TXs. */
745 mvneta_mib_counters_clear(pp);
746 q_map = 0;
747 for (queue = 0; queue < txq_number; queue++) {
748 struct mvneta_tx_queue *txq = &pp->txqs[queue];
749 if (txq->descs != NULL)
750 q_map |= (1 << queue);
751 }
752 mvreg_write(pp, MVNETA_TXQ_CMD, q_map);
753
754 /* Enable all initialized RXQs. */
755 q_map = 0;
756 for (queue = 0; queue < rxq_number; queue++) {
757 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
758 if (rxq->descs != NULL)
759 q_map |= (1 << queue);
760 }
761
762 mvreg_write(pp, MVNETA_RXQ_CMD, q_map);
763}
764
765/* Stop the Ethernet port activity */
766static void mvneta_port_down(struct mvneta_port *pp)
767{
768 u32 val;
769 int count;
770
771 /* Stop Rx port activity. Check port Rx activity. */
772 val = mvreg_read(pp, MVNETA_RXQ_CMD) & MVNETA_RXQ_ENABLE_MASK;
773
774 /* Issue stop command for active channels only */
775 if (val != 0)
776 mvreg_write(pp, MVNETA_RXQ_CMD,
777 val << MVNETA_RXQ_DISABLE_SHIFT);
778
779 /* Wait for all Rx activity to terminate. */
780 count = 0;
781 do {
782 if (count++ >= MVNETA_RX_DISABLE_TIMEOUT_MSEC) {
783 netdev_warn(pp->dev,
784 "TIMEOUT for RX stopped ! rx_queue_cmd: 0x08%x\n",
785 val);
786 break;
787 }
788 mdelay(1);
789
790 val = mvreg_read(pp, MVNETA_RXQ_CMD);
791 } while (val & 0xff);
792
793 /* Stop Tx port activity. Check port Tx activity. Issue stop
6a20c175
TP
794 * command for active channels only
795 */
c5aff182
TP
796 val = (mvreg_read(pp, MVNETA_TXQ_CMD)) & MVNETA_TXQ_ENABLE_MASK;
797
798 if (val != 0)
799 mvreg_write(pp, MVNETA_TXQ_CMD,
800 (val << MVNETA_TXQ_DISABLE_SHIFT));
801
802 /* Wait for all Tx activity to terminate. */
803 count = 0;
804 do {
805 if (count++ >= MVNETA_TX_DISABLE_TIMEOUT_MSEC) {
806 netdev_warn(pp->dev,
807 "TIMEOUT for TX stopped status=0x%08x\n",
808 val);
809 break;
810 }
811 mdelay(1);
812
813 /* Check TX Command reg that all Txqs are stopped */
814 val = mvreg_read(pp, MVNETA_TXQ_CMD);
815
816 } while (val & 0xff);
817
818 /* Double check to verify that TX FIFO is empty */
819 count = 0;
820 do {
821 if (count++ >= MVNETA_TX_FIFO_EMPTY_TIMEOUT) {
822 netdev_warn(pp->dev,
823 "TX FIFO empty timeout status=0x08%x\n",
824 val);
825 break;
826 }
827 mdelay(1);
828
829 val = mvreg_read(pp, MVNETA_PORT_STATUS);
830 } while (!(val & MVNETA_TX_FIFO_EMPTY) &&
831 (val & MVNETA_TX_IN_PRGRS));
832
833 udelay(200);
834}
835
836/* Enable the port by setting the port enable bit of the MAC control register */
837static void mvneta_port_enable(struct mvneta_port *pp)
838{
839 u32 val;
840
841 /* Enable port */
842 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
843 val |= MVNETA_GMAC0_PORT_ENABLE;
844 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
845}
846
847/* Disable the port and wait for about 200 usec before retuning */
848static void mvneta_port_disable(struct mvneta_port *pp)
849{
850 u32 val;
851
852 /* Reset the Enable bit in the Serial Control Register */
853 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
854 val &= ~MVNETA_GMAC0_PORT_ENABLE;
855 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
856
857 udelay(200);
858}
859
860/* Multicast tables methods */
861
862/* Set all entries in Unicast MAC Table; queue==-1 means reject all */
863static void mvneta_set_ucast_table(struct mvneta_port *pp, int queue)
864{
865 int offset;
866 u32 val;
867
868 if (queue == -1) {
869 val = 0;
870 } else {
871 val = 0x1 | (queue << 1);
872 val |= (val << 24) | (val << 16) | (val << 8);
873 }
874
875 for (offset = 0; offset <= 0xc; offset += 4)
876 mvreg_write(pp, MVNETA_DA_FILT_UCAST_BASE + offset, val);
877}
878
879/* Set all entries in Special Multicast MAC Table; queue==-1 means reject all */
880static void mvneta_set_special_mcast_table(struct mvneta_port *pp, int queue)
881{
882 int offset;
883 u32 val;
884
885 if (queue == -1) {
886 val = 0;
887 } else {
888 val = 0x1 | (queue << 1);
889 val |= (val << 24) | (val << 16) | (val << 8);
890 }
891
892 for (offset = 0; offset <= 0xfc; offset += 4)
893 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + offset, val);
894
895}
896
897/* Set all entries in Other Multicast MAC Table. queue==-1 means reject all */
898static void mvneta_set_other_mcast_table(struct mvneta_port *pp, int queue)
899{
900 int offset;
901 u32 val;
902
903 if (queue == -1) {
904 memset(pp->mcast_count, 0, sizeof(pp->mcast_count));
905 val = 0;
906 } else {
907 memset(pp->mcast_count, 1, sizeof(pp->mcast_count));
908 val = 0x1 | (queue << 1);
909 val |= (val << 24) | (val << 16) | (val << 8);
910 }
911
912 for (offset = 0; offset <= 0xfc; offset += 4)
913 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + offset, val);
914}
915
916/* This method sets defaults to the NETA port:
917 * Clears interrupt Cause and Mask registers.
918 * Clears all MAC tables.
919 * Sets defaults to all registers.
920 * Resets RX and TX descriptor rings.
921 * Resets PHY.
922 * This method can be called after mvneta_port_down() to return the port
923 * settings to defaults.
924 */
925static void mvneta_defaults_set(struct mvneta_port *pp)
926{
927 int cpu;
928 int queue;
929 u32 val;
930
931 /* Clear all Cause registers */
932 mvreg_write(pp, MVNETA_INTR_NEW_CAUSE, 0);
933 mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0);
934 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
935
936 /* Mask all interrupts */
937 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
938 mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0);
939 mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0);
940 mvreg_write(pp, MVNETA_INTR_ENABLE, 0);
941
942 /* Enable MBUS Retry bit16 */
943 mvreg_write(pp, MVNETA_MBUS_RETRY, 0x20);
944
945 /* Set CPU queue access map - all CPUs have access to all RX
6a20c175
TP
946 * queues and to all TX queues
947 */
c5aff182
TP
948 for (cpu = 0; cpu < CONFIG_NR_CPUS; cpu++)
949 mvreg_write(pp, MVNETA_CPU_MAP(cpu),
950 (MVNETA_CPU_RXQ_ACCESS_ALL_MASK |
951 MVNETA_CPU_TXQ_ACCESS_ALL_MASK));
952
953 /* Reset RX and TX DMAs */
954 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
955 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
956
957 /* Disable Legacy WRR, Disable EJP, Release from reset */
958 mvreg_write(pp, MVNETA_TXQ_CMD_1, 0);
959 for (queue = 0; queue < txq_number; queue++) {
960 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(queue), 0);
961 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(queue), 0);
962 }
963
964 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
965 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
966
967 /* Set Port Acceleration Mode */
968 val = MVNETA_ACC_MODE_EXT;
969 mvreg_write(pp, MVNETA_ACC_MODE, val);
970
971 /* Update val of portCfg register accordingly with all RxQueue types */
972 val = MVNETA_PORT_CONFIG_DEFL_VALUE(rxq_def);
973 mvreg_write(pp, MVNETA_PORT_CONFIG, val);
974
975 val = 0;
976 mvreg_write(pp, MVNETA_PORT_CONFIG_EXTEND, val);
977 mvreg_write(pp, MVNETA_RX_MIN_FRAME_SIZE, 64);
978
979 /* Build PORT_SDMA_CONFIG_REG */
980 val = 0;
981
982 /* Default burst size */
983 val |= MVNETA_TX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
984 val |= MVNETA_RX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
9ad8fef6 985 val |= MVNETA_RX_NO_DATA_SWAP | MVNETA_TX_NO_DATA_SWAP;
c5aff182 986
9ad8fef6
TP
987#if defined(__BIG_ENDIAN)
988 val |= MVNETA_DESC_SWAP;
989#endif
c5aff182
TP
990
991 /* Assign port SDMA configuration */
992 mvreg_write(pp, MVNETA_SDMA_CONFIG, val);
993
71408602
TP
994 /* Disable PHY polling in hardware, since we're using the
995 * kernel phylib to do this.
996 */
997 val = mvreg_read(pp, MVNETA_UNIT_CONTROL);
998 val &= ~MVNETA_PHY_POLLING_ENABLE;
999 mvreg_write(pp, MVNETA_UNIT_CONTROL, val);
1000
c5aff182
TP
1001 mvneta_set_ucast_table(pp, -1);
1002 mvneta_set_special_mcast_table(pp, -1);
1003 mvneta_set_other_mcast_table(pp, -1);
1004
1005 /* Set port interrupt enable register - default enable all */
1006 mvreg_write(pp, MVNETA_INTR_ENABLE,
1007 (MVNETA_RXQ_INTR_ENABLE_ALL_MASK
1008 | MVNETA_TXQ_INTR_ENABLE_ALL_MASK));
1009}
1010
1011/* Set max sizes for tx queues */
1012static void mvneta_txq_max_tx_size_set(struct mvneta_port *pp, int max_tx_size)
1013
1014{
1015 u32 val, size, mtu;
1016 int queue;
1017
1018 mtu = max_tx_size * 8;
1019 if (mtu > MVNETA_TX_MTU_MAX)
1020 mtu = MVNETA_TX_MTU_MAX;
1021
1022 /* Set MTU */
1023 val = mvreg_read(pp, MVNETA_TX_MTU);
1024 val &= ~MVNETA_TX_MTU_MAX;
1025 val |= mtu;
1026 mvreg_write(pp, MVNETA_TX_MTU, val);
1027
1028 /* TX token size and all TXQs token size must be larger that MTU */
1029 val = mvreg_read(pp, MVNETA_TX_TOKEN_SIZE);
1030
1031 size = val & MVNETA_TX_TOKEN_SIZE_MAX;
1032 if (size < mtu) {
1033 size = mtu;
1034 val &= ~MVNETA_TX_TOKEN_SIZE_MAX;
1035 val |= size;
1036 mvreg_write(pp, MVNETA_TX_TOKEN_SIZE, val);
1037 }
1038 for (queue = 0; queue < txq_number; queue++) {
1039 val = mvreg_read(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue));
1040
1041 size = val & MVNETA_TXQ_TOKEN_SIZE_MAX;
1042 if (size < mtu) {
1043 size = mtu;
1044 val &= ~MVNETA_TXQ_TOKEN_SIZE_MAX;
1045 val |= size;
1046 mvreg_write(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue), val);
1047 }
1048 }
1049}
1050
1051/* Set unicast address */
1052static void mvneta_set_ucast_addr(struct mvneta_port *pp, u8 last_nibble,
1053 int queue)
1054{
1055 unsigned int unicast_reg;
1056 unsigned int tbl_offset;
1057 unsigned int reg_offset;
1058
1059 /* Locate the Unicast table entry */
1060 last_nibble = (0xf & last_nibble);
1061
1062 /* offset from unicast tbl base */
1063 tbl_offset = (last_nibble / 4) * 4;
1064
1065 /* offset within the above reg */
1066 reg_offset = last_nibble % 4;
1067
1068 unicast_reg = mvreg_read(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset));
1069
1070 if (queue == -1) {
1071 /* Clear accepts frame bit at specified unicast DA tbl entry */
1072 unicast_reg &= ~(0xff << (8 * reg_offset));
1073 } else {
1074 unicast_reg &= ~(0xff << (8 * reg_offset));
1075 unicast_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
1076 }
1077
1078 mvreg_write(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset), unicast_reg);
1079}
1080
1081/* Set mac address */
1082static void mvneta_mac_addr_set(struct mvneta_port *pp, unsigned char *addr,
1083 int queue)
1084{
1085 unsigned int mac_h;
1086 unsigned int mac_l;
1087
1088 if (queue != -1) {
1089 mac_l = (addr[4] << 8) | (addr[5]);
1090 mac_h = (addr[0] << 24) | (addr[1] << 16) |
1091 (addr[2] << 8) | (addr[3] << 0);
1092
1093 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, mac_l);
1094 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, mac_h);
1095 }
1096
1097 /* Accept frames of this address */
1098 mvneta_set_ucast_addr(pp, addr[5], queue);
1099}
1100
6a20c175
TP
1101/* Set the number of packets that will be received before RX interrupt
1102 * will be generated by HW.
c5aff182
TP
1103 */
1104static void mvneta_rx_pkts_coal_set(struct mvneta_port *pp,
1105 struct mvneta_rx_queue *rxq, u32 value)
1106{
1107 mvreg_write(pp, MVNETA_RXQ_THRESHOLD_REG(rxq->id),
1108 value | MVNETA_RXQ_NON_OCCUPIED(0));
1109 rxq->pkts_coal = value;
1110}
1111
6a20c175
TP
1112/* Set the time delay in usec before RX interrupt will be generated by
1113 * HW.
c5aff182
TP
1114 */
1115static void mvneta_rx_time_coal_set(struct mvneta_port *pp,
1116 struct mvneta_rx_queue *rxq, u32 value)
1117{
189dd626
TP
1118 u32 val;
1119 unsigned long clk_rate;
1120
1121 clk_rate = clk_get_rate(pp->clk);
1122 val = (clk_rate / 1000000) * value;
c5aff182
TP
1123
1124 mvreg_write(pp, MVNETA_RXQ_TIME_COAL_REG(rxq->id), val);
1125 rxq->time_coal = value;
1126}
1127
1128/* Set threshold for TX_DONE pkts coalescing */
1129static void mvneta_tx_done_pkts_coal_set(struct mvneta_port *pp,
1130 struct mvneta_tx_queue *txq, u32 value)
1131{
1132 u32 val;
1133
1134 val = mvreg_read(pp, MVNETA_TXQ_SIZE_REG(txq->id));
1135
1136 val &= ~MVNETA_TXQ_SENT_THRESH_ALL_MASK;
1137 val |= MVNETA_TXQ_SENT_THRESH_MASK(value);
1138
1139 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), val);
1140
1141 txq->done_pkts_coal = value;
1142}
1143
c5aff182
TP
1144/* Handle rx descriptor fill by setting buf_cookie and buf_phys_addr */
1145static void mvneta_rx_desc_fill(struct mvneta_rx_desc *rx_desc,
1146 u32 phys_addr, u32 cookie)
1147{
1148 rx_desc->buf_cookie = cookie;
1149 rx_desc->buf_phys_addr = phys_addr;
1150}
1151
1152/* Decrement sent descriptors counter */
1153static void mvneta_txq_sent_desc_dec(struct mvneta_port *pp,
1154 struct mvneta_tx_queue *txq,
1155 int sent_desc)
1156{
1157 u32 val;
1158
1159 /* Only 255 TX descriptors can be updated at once */
1160 while (sent_desc > 0xff) {
1161 val = 0xff << MVNETA_TXQ_DEC_SENT_SHIFT;
1162 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1163 sent_desc = sent_desc - 0xff;
1164 }
1165
1166 val = sent_desc << MVNETA_TXQ_DEC_SENT_SHIFT;
1167 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1168}
1169
1170/* Get number of TX descriptors already sent by HW */
1171static int mvneta_txq_sent_desc_num_get(struct mvneta_port *pp,
1172 struct mvneta_tx_queue *txq)
1173{
1174 u32 val;
1175 int sent_desc;
1176
1177 val = mvreg_read(pp, MVNETA_TXQ_STATUS_REG(txq->id));
1178 sent_desc = (val & MVNETA_TXQ_SENT_DESC_MASK) >>
1179 MVNETA_TXQ_SENT_DESC_SHIFT;
1180
1181 return sent_desc;
1182}
1183
6a20c175 1184/* Get number of sent descriptors and decrement counter.
c5aff182
TP
1185 * The number of sent descriptors is returned.
1186 */
1187static int mvneta_txq_sent_desc_proc(struct mvneta_port *pp,
1188 struct mvneta_tx_queue *txq)
1189{
1190 int sent_desc;
1191
1192 /* Get number of sent descriptors */
1193 sent_desc = mvneta_txq_sent_desc_num_get(pp, txq);
1194
1195 /* Decrement sent descriptors counter */
1196 if (sent_desc)
1197 mvneta_txq_sent_desc_dec(pp, txq, sent_desc);
1198
1199 return sent_desc;
1200}
1201
1202/* Set TXQ descriptors fields relevant for CSUM calculation */
1203static u32 mvneta_txq_desc_csum(int l3_offs, int l3_proto,
1204 int ip_hdr_len, int l4_proto)
1205{
1206 u32 command;
1207
1208 /* Fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
6a20c175
TP
1209 * G_L4_chk, L4_type; required only for checksum
1210 * calculation
1211 */
c5aff182
TP
1212 command = l3_offs << MVNETA_TX_L3_OFF_SHIFT;
1213 command |= ip_hdr_len << MVNETA_TX_IP_HLEN_SHIFT;
1214
1215 if (l3_proto == swab16(ETH_P_IP))
1216 command |= MVNETA_TXD_IP_CSUM;
1217 else
1218 command |= MVNETA_TX_L3_IP6;
1219
1220 if (l4_proto == IPPROTO_TCP)
1221 command |= MVNETA_TX_L4_CSUM_FULL;
1222 else if (l4_proto == IPPROTO_UDP)
1223 command |= MVNETA_TX_L4_UDP | MVNETA_TX_L4_CSUM_FULL;
1224 else
1225 command |= MVNETA_TX_L4_CSUM_NOT;
1226
1227 return command;
1228}
1229
1230
1231/* Display more error info */
1232static void mvneta_rx_error(struct mvneta_port *pp,
1233 struct mvneta_rx_desc *rx_desc)
1234{
1235 u32 status = rx_desc->status;
1236
1237 if (!mvneta_rxq_desc_is_first_last(rx_desc)) {
1238 netdev_err(pp->dev,
1239 "bad rx status %08x (buffer oversize), size=%d\n",
1240 rx_desc->status, rx_desc->data_size);
1241 return;
1242 }
1243
1244 switch (status & MVNETA_RXD_ERR_CODE_MASK) {
1245 case MVNETA_RXD_ERR_CRC:
1246 netdev_err(pp->dev, "bad rx status %08x (crc error), size=%d\n",
1247 status, rx_desc->data_size);
1248 break;
1249 case MVNETA_RXD_ERR_OVERRUN:
1250 netdev_err(pp->dev, "bad rx status %08x (overrun error), size=%d\n",
1251 status, rx_desc->data_size);
1252 break;
1253 case MVNETA_RXD_ERR_LEN:
1254 netdev_err(pp->dev, "bad rx status %08x (max frame length error), size=%d\n",
1255 status, rx_desc->data_size);
1256 break;
1257 case MVNETA_RXD_ERR_RESOURCE:
1258 netdev_err(pp->dev, "bad rx status %08x (resource error), size=%d\n",
1259 status, rx_desc->data_size);
1260 break;
1261 }
1262}
1263
1264/* Handle RX checksum offload */
1265static void mvneta_rx_csum(struct mvneta_port *pp,
1266 struct mvneta_rx_desc *rx_desc,
1267 struct sk_buff *skb)
1268{
1269 if ((rx_desc->status & MVNETA_RXD_L3_IP4) &&
1270 (rx_desc->status & MVNETA_RXD_L4_CSUM_OK)) {
1271 skb->csum = 0;
1272 skb->ip_summed = CHECKSUM_UNNECESSARY;
1273 return;
1274 }
1275
1276 skb->ip_summed = CHECKSUM_NONE;
1277}
1278
6c498974 1279/* Return tx queue pointer (find last set bit) according to <cause> returned
1280 * form tx_done reg. <cause> must not be null. The return value is always a
1281 * valid queue for matching the first one found in <cause>.
1282 */
c5aff182
TP
1283static struct mvneta_tx_queue *mvneta_tx_done_policy(struct mvneta_port *pp,
1284 u32 cause)
1285{
1286 int queue = fls(cause) - 1;
1287
6c498974 1288 return &pp->txqs[queue];
c5aff182
TP
1289}
1290
1291/* Free tx queue skbuffs */
1292static void mvneta_txq_bufs_free(struct mvneta_port *pp,
1293 struct mvneta_tx_queue *txq, int num)
1294{
1295 int i;
1296
1297 for (i = 0; i < num; i++) {
1298 struct mvneta_tx_desc *tx_desc = txq->descs +
1299 txq->txq_get_index;
1300 struct sk_buff *skb = txq->tx_skb[txq->txq_get_index];
1301
1302 mvneta_txq_inc_get(txq);
1303
1304 if (!skb)
1305 continue;
1306
1307 dma_unmap_single(pp->dev->dev.parent, tx_desc->buf_phys_addr,
1308 tx_desc->data_size, DMA_TO_DEVICE);
1309 dev_kfree_skb_any(skb);
1310 }
1311}
1312
1313/* Handle end of transmission */
1314static int mvneta_txq_done(struct mvneta_port *pp,
1315 struct mvneta_tx_queue *txq)
1316{
1317 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
1318 int tx_done;
1319
1320 tx_done = mvneta_txq_sent_desc_proc(pp, txq);
1321 if (tx_done == 0)
1322 return tx_done;
1323 mvneta_txq_bufs_free(pp, txq, tx_done);
1324
1325 txq->count -= tx_done;
1326
1327 if (netif_tx_queue_stopped(nq)) {
1328 if (txq->size - txq->count >= MAX_SKB_FRAGS + 1)
1329 netif_tx_wake_queue(nq);
1330 }
1331
1332 return tx_done;
1333}
1334
1335/* Refill processing */
1336static int mvneta_rx_refill(struct mvneta_port *pp,
1337 struct mvneta_rx_desc *rx_desc)
1338
1339{
1340 dma_addr_t phys_addr;
1341 struct sk_buff *skb;
1342
1343 skb = netdev_alloc_skb(pp->dev, pp->pkt_size);
1344 if (!skb)
1345 return -ENOMEM;
1346
1347 phys_addr = dma_map_single(pp->dev->dev.parent, skb->head,
1348 MVNETA_RX_BUF_SIZE(pp->pkt_size),
1349 DMA_FROM_DEVICE);
1350 if (unlikely(dma_mapping_error(pp->dev->dev.parent, phys_addr))) {
1351 dev_kfree_skb(skb);
1352 return -ENOMEM;
1353 }
1354
1355 mvneta_rx_desc_fill(rx_desc, phys_addr, (u32)skb);
1356
1357 return 0;
1358}
1359
1360/* Handle tx checksum */
1361static u32 mvneta_skb_tx_csum(struct mvneta_port *pp, struct sk_buff *skb)
1362{
1363 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1364 int ip_hdr_len = 0;
1365 u8 l4_proto;
1366
1367 if (skb->protocol == htons(ETH_P_IP)) {
1368 struct iphdr *ip4h = ip_hdr(skb);
1369
1370 /* Calculate IPv4 checksum and L4 checksum */
1371 ip_hdr_len = ip4h->ihl;
1372 l4_proto = ip4h->protocol;
1373 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1374 struct ipv6hdr *ip6h = ipv6_hdr(skb);
1375
1376 /* Read l4_protocol from one of IPv6 extra headers */
1377 if (skb_network_header_len(skb) > 0)
1378 ip_hdr_len = (skb_network_header_len(skb) >> 2);
1379 l4_proto = ip6h->nexthdr;
1380 } else
1381 return MVNETA_TX_L4_CSUM_NOT;
1382
1383 return mvneta_txq_desc_csum(skb_network_offset(skb),
1384 skb->protocol, ip_hdr_len, l4_proto);
1385 }
1386
1387 return MVNETA_TX_L4_CSUM_NOT;
1388}
1389
6a20c175 1390/* Returns rx queue pointer (find last set bit) according to causeRxTx
c5aff182
TP
1391 * value
1392 */
1393static struct mvneta_rx_queue *mvneta_rx_policy(struct mvneta_port *pp,
1394 u32 cause)
1395{
1396 int queue = fls(cause >> 8) - 1;
1397
1398 return (queue < 0 || queue >= rxq_number) ? NULL : &pp->rxqs[queue];
1399}
1400
1401/* Drop packets received by the RXQ and free buffers */
1402static void mvneta_rxq_drop_pkts(struct mvneta_port *pp,
1403 struct mvneta_rx_queue *rxq)
1404{
1405 int rx_done, i;
1406
1407 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
1408 for (i = 0; i < rxq->size; i++) {
1409 struct mvneta_rx_desc *rx_desc = rxq->descs + i;
1410 struct sk_buff *skb = (struct sk_buff *)rx_desc->buf_cookie;
1411
1412 dev_kfree_skb_any(skb);
1413 dma_unmap_single(pp->dev->dev.parent, rx_desc->buf_phys_addr,
a328f3a0 1414 MVNETA_RX_BUF_SIZE(pp->pkt_size), DMA_FROM_DEVICE);
c5aff182
TP
1415 }
1416
1417 if (rx_done)
1418 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
1419}
1420
1421/* Main rx processing */
1422static int mvneta_rx(struct mvneta_port *pp, int rx_todo,
1423 struct mvneta_rx_queue *rxq)
1424{
1425 struct net_device *dev = pp->dev;
1426 int rx_done, rx_filled;
dc4277dd 1427 u32 rcvd_pkts = 0;
1428 u32 rcvd_bytes = 0;
c5aff182
TP
1429
1430 /* Get number of received packets */
1431 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
1432
1433 if (rx_todo > rx_done)
1434 rx_todo = rx_done;
1435
1436 rx_done = 0;
1437 rx_filled = 0;
1438
1439 /* Fairness NAPI loop */
1440 while (rx_done < rx_todo) {
1441 struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
1442 struct sk_buff *skb;
1443 u32 rx_status;
1444 int rx_bytes, err;
1445
1446 prefetch(rx_desc);
1447 rx_done++;
1448 rx_filled++;
1449 rx_status = rx_desc->status;
1450 skb = (struct sk_buff *)rx_desc->buf_cookie;
1451
1452 if (!mvneta_rxq_desc_is_first_last(rx_desc) ||
1453 (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
1454 dev->stats.rx_errors++;
1455 mvneta_rx_error(pp, rx_desc);
1456 mvneta_rx_desc_fill(rx_desc, rx_desc->buf_phys_addr,
1457 (u32)skb);
1458 continue;
1459 }
1460
1461 dma_unmap_single(pp->dev->dev.parent, rx_desc->buf_phys_addr,
a328f3a0 1462 MVNETA_RX_BUF_SIZE(pp->pkt_size), DMA_FROM_DEVICE);
c5aff182
TP
1463
1464 rx_bytes = rx_desc->data_size -
1465 (ETH_FCS_LEN + MVNETA_MH_SIZE);
dc4277dd 1466 rcvd_pkts++;
1467 rcvd_bytes += rx_bytes;
c5aff182
TP
1468
1469 /* Linux processing */
1470 skb_reserve(skb, MVNETA_MH_SIZE);
1471 skb_put(skb, rx_bytes);
1472
1473 skb->protocol = eth_type_trans(skb, dev);
1474
1475 mvneta_rx_csum(pp, rx_desc, skb);
1476
1477 napi_gro_receive(&pp->napi, skb);
1478
1479 /* Refill processing */
1480 err = mvneta_rx_refill(pp, rx_desc);
1481 if (err) {
1482 netdev_err(pp->dev, "Linux processing - Can't refill\n");
1483 rxq->missed++;
1484 rx_filled--;
1485 }
1486 }
1487
dc4277dd 1488 if (rcvd_pkts) {
74c41b04 1489 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
1490
1491 u64_stats_update_begin(&stats->syncp);
1492 stats->rx_packets += rcvd_pkts;
1493 stats->rx_bytes += rcvd_bytes;
1494 u64_stats_update_end(&stats->syncp);
dc4277dd 1495 }
1496
c5aff182
TP
1497 /* Update rxq management counters */
1498 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_filled);
1499
1500 return rx_done;
1501}
1502
1503/* Handle tx fragmentation processing */
1504static int mvneta_tx_frag_process(struct mvneta_port *pp, struct sk_buff *skb,
1505 struct mvneta_tx_queue *txq)
1506{
1507 struct mvneta_tx_desc *tx_desc;
1508 int i;
1509
1510 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1511 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1512 void *addr = page_address(frag->page.p) + frag->page_offset;
1513
1514 tx_desc = mvneta_txq_next_desc_get(txq);
1515 tx_desc->data_size = frag->size;
1516
1517 tx_desc->buf_phys_addr =
1518 dma_map_single(pp->dev->dev.parent, addr,
1519 tx_desc->data_size, DMA_TO_DEVICE);
1520
1521 if (dma_mapping_error(pp->dev->dev.parent,
1522 tx_desc->buf_phys_addr)) {
1523 mvneta_txq_desc_put(txq);
1524 goto error;
1525 }
1526
1527 if (i == (skb_shinfo(skb)->nr_frags - 1)) {
1528 /* Last descriptor */
1529 tx_desc->command = MVNETA_TXD_L_DESC | MVNETA_TXD_Z_PAD;
1530
1531 txq->tx_skb[txq->txq_put_index] = skb;
1532
1533 mvneta_txq_inc_put(txq);
1534 } else {
1535 /* Descriptor in the middle: Not First, Not Last */
1536 tx_desc->command = 0;
1537
1538 txq->tx_skb[txq->txq_put_index] = NULL;
1539 mvneta_txq_inc_put(txq);
1540 }
1541 }
1542
1543 return 0;
1544
1545error:
1546 /* Release all descriptors that were used to map fragments of
6a20c175
TP
1547 * this packet, as well as the corresponding DMA mappings
1548 */
c5aff182
TP
1549 for (i = i - 1; i >= 0; i--) {
1550 tx_desc = txq->descs + i;
1551 dma_unmap_single(pp->dev->dev.parent,
1552 tx_desc->buf_phys_addr,
1553 tx_desc->data_size,
1554 DMA_TO_DEVICE);
1555 mvneta_txq_desc_put(txq);
1556 }
1557
1558 return -ENOMEM;
1559}
1560
1561/* Main tx processing */
1562static int mvneta_tx(struct sk_buff *skb, struct net_device *dev)
1563{
1564 struct mvneta_port *pp = netdev_priv(dev);
ee40a116
WT
1565 u16 txq_id = skb_get_queue_mapping(skb);
1566 struct mvneta_tx_queue *txq = &pp->txqs[txq_id];
c5aff182
TP
1567 struct mvneta_tx_desc *tx_desc;
1568 struct netdev_queue *nq;
1569 int frags = 0;
1570 u32 tx_cmd;
1571
1572 if (!netif_running(dev))
1573 goto out;
1574
1575 frags = skb_shinfo(skb)->nr_frags + 1;
ee40a116 1576 nq = netdev_get_tx_queue(dev, txq_id);
c5aff182
TP
1577
1578 /* Get a descriptor for the first part of the packet */
1579 tx_desc = mvneta_txq_next_desc_get(txq);
1580
1581 tx_cmd = mvneta_skb_tx_csum(pp, skb);
1582
1583 tx_desc->data_size = skb_headlen(skb);
1584
1585 tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, skb->data,
1586 tx_desc->data_size,
1587 DMA_TO_DEVICE);
1588 if (unlikely(dma_mapping_error(dev->dev.parent,
1589 tx_desc->buf_phys_addr))) {
1590 mvneta_txq_desc_put(txq);
1591 frags = 0;
1592 goto out;
1593 }
1594
1595 if (frags == 1) {
1596 /* First and Last descriptor */
1597 tx_cmd |= MVNETA_TXD_FLZ_DESC;
1598 tx_desc->command = tx_cmd;
1599 txq->tx_skb[txq->txq_put_index] = skb;
1600 mvneta_txq_inc_put(txq);
1601 } else {
1602 /* First but not Last */
1603 tx_cmd |= MVNETA_TXD_F_DESC;
1604 txq->tx_skb[txq->txq_put_index] = NULL;
1605 mvneta_txq_inc_put(txq);
1606 tx_desc->command = tx_cmd;
1607 /* Continue with other skb fragments */
1608 if (mvneta_tx_frag_process(pp, skb, txq)) {
1609 dma_unmap_single(dev->dev.parent,
1610 tx_desc->buf_phys_addr,
1611 tx_desc->data_size,
1612 DMA_TO_DEVICE);
1613 mvneta_txq_desc_put(txq);
1614 frags = 0;
1615 goto out;
1616 }
1617 }
1618
1619 txq->count += frags;
1620 mvneta_txq_pend_desc_add(pp, txq, frags);
1621
1622 if (txq->size - txq->count < MAX_SKB_FRAGS + 1)
1623 netif_tx_stop_queue(nq);
1624
1625out:
1626 if (frags > 0) {
74c41b04 1627 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
c5aff182 1628
74c41b04 1629 u64_stats_update_begin(&stats->syncp);
1630 stats->tx_packets++;
1631 stats->tx_bytes += skb->len;
1632 u64_stats_update_end(&stats->syncp);
c5aff182
TP
1633 } else {
1634 dev->stats.tx_dropped++;
1635 dev_kfree_skb_any(skb);
1636 }
1637
c5aff182
TP
1638 return NETDEV_TX_OK;
1639}
1640
1641
1642/* Free tx resources, when resetting a port */
1643static void mvneta_txq_done_force(struct mvneta_port *pp,
1644 struct mvneta_tx_queue *txq)
1645
1646{
1647 int tx_done = txq->count;
1648
1649 mvneta_txq_bufs_free(pp, txq, tx_done);
1650
1651 /* reset txq */
1652 txq->count = 0;
1653 txq->txq_put_index = 0;
1654 txq->txq_get_index = 0;
1655}
1656
6c498974 1657/* Handle tx done - called in softirq context. The <cause_tx_done> argument
1658 * must be a valid cause according to MVNETA_TXQ_INTR_MASK_ALL.
1659 */
c5aff182
TP
1660static u32 mvneta_tx_done_gbe(struct mvneta_port *pp, u32 cause_tx_done,
1661 int *tx_todo)
1662{
1663 struct mvneta_tx_queue *txq;
1664 u32 tx_done = 0;
1665 struct netdev_queue *nq;
1666
1667 *tx_todo = 0;
6c498974 1668 while (cause_tx_done) {
c5aff182 1669 txq = mvneta_tx_done_policy(pp, cause_tx_done);
c5aff182
TP
1670
1671 nq = netdev_get_tx_queue(pp->dev, txq->id);
1672 __netif_tx_lock(nq, smp_processor_id());
1673
1674 if (txq->count) {
1675 tx_done += mvneta_txq_done(pp, txq);
1676 *tx_todo += txq->count;
1677 }
1678
1679 __netif_tx_unlock(nq);
1680 cause_tx_done &= ~((1 << txq->id));
1681 }
1682
1683 return tx_done;
1684}
1685
6a20c175 1686/* Compute crc8 of the specified address, using a unique algorithm ,
c5aff182
TP
1687 * according to hw spec, different than generic crc8 algorithm
1688 */
1689static int mvneta_addr_crc(unsigned char *addr)
1690{
1691 int crc = 0;
1692 int i;
1693
1694 for (i = 0; i < ETH_ALEN; i++) {
1695 int j;
1696
1697 crc = (crc ^ addr[i]) << 8;
1698 for (j = 7; j >= 0; j--) {
1699 if (crc & (0x100 << j))
1700 crc ^= 0x107 << j;
1701 }
1702 }
1703
1704 return crc;
1705}
1706
1707/* This method controls the net device special MAC multicast support.
1708 * The Special Multicast Table for MAC addresses supports MAC of the form
1709 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
1710 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
1711 * Table entries in the DA-Filter table. This method set the Special
1712 * Multicast Table appropriate entry.
1713 */
1714static void mvneta_set_special_mcast_addr(struct mvneta_port *pp,
1715 unsigned char last_byte,
1716 int queue)
1717{
1718 unsigned int smc_table_reg;
1719 unsigned int tbl_offset;
1720 unsigned int reg_offset;
1721
1722 /* Register offset from SMC table base */
1723 tbl_offset = (last_byte / 4);
1724 /* Entry offset within the above reg */
1725 reg_offset = last_byte % 4;
1726
1727 smc_table_reg = mvreg_read(pp, (MVNETA_DA_FILT_SPEC_MCAST
1728 + tbl_offset * 4));
1729
1730 if (queue == -1)
1731 smc_table_reg &= ~(0xff << (8 * reg_offset));
1732 else {
1733 smc_table_reg &= ~(0xff << (8 * reg_offset));
1734 smc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
1735 }
1736
1737 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + tbl_offset * 4,
1738 smc_table_reg);
1739}
1740
1741/* This method controls the network device Other MAC multicast support.
1742 * The Other Multicast Table is used for multicast of another type.
1743 * A CRC-8 is used as an index to the Other Multicast Table entries
1744 * in the DA-Filter table.
1745 * The method gets the CRC-8 value from the calling routine and
1746 * sets the Other Multicast Table appropriate entry according to the
1747 * specified CRC-8 .
1748 */
1749static void mvneta_set_other_mcast_addr(struct mvneta_port *pp,
1750 unsigned char crc8,
1751 int queue)
1752{
1753 unsigned int omc_table_reg;
1754 unsigned int tbl_offset;
1755 unsigned int reg_offset;
1756
1757 tbl_offset = (crc8 / 4) * 4; /* Register offset from OMC table base */
1758 reg_offset = crc8 % 4; /* Entry offset within the above reg */
1759
1760 omc_table_reg = mvreg_read(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset);
1761
1762 if (queue == -1) {
1763 /* Clear accepts frame bit at specified Other DA table entry */
1764 omc_table_reg &= ~(0xff << (8 * reg_offset));
1765 } else {
1766 omc_table_reg &= ~(0xff << (8 * reg_offset));
1767 omc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
1768 }
1769
1770 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset, omc_table_reg);
1771}
1772
1773/* The network device supports multicast using two tables:
1774 * 1) Special Multicast Table for MAC addresses of the form
1775 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
1776 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
1777 * Table entries in the DA-Filter table.
1778 * 2) Other Multicast Table for multicast of another type. A CRC-8 value
1779 * is used as an index to the Other Multicast Table entries in the
1780 * DA-Filter table.
1781 */
1782static int mvneta_mcast_addr_set(struct mvneta_port *pp, unsigned char *p_addr,
1783 int queue)
1784{
1785 unsigned char crc_result = 0;
1786
1787 if (memcmp(p_addr, "\x01\x00\x5e\x00\x00", 5) == 0) {
1788 mvneta_set_special_mcast_addr(pp, p_addr[5], queue);
1789 return 0;
1790 }
1791
1792 crc_result = mvneta_addr_crc(p_addr);
1793 if (queue == -1) {
1794 if (pp->mcast_count[crc_result] == 0) {
1795 netdev_info(pp->dev, "No valid Mcast for crc8=0x%02x\n",
1796 crc_result);
1797 return -EINVAL;
1798 }
1799
1800 pp->mcast_count[crc_result]--;
1801 if (pp->mcast_count[crc_result] != 0) {
1802 netdev_info(pp->dev,
1803 "After delete there are %d valid Mcast for crc8=0x%02x\n",
1804 pp->mcast_count[crc_result], crc_result);
1805 return -EINVAL;
1806 }
1807 } else
1808 pp->mcast_count[crc_result]++;
1809
1810 mvneta_set_other_mcast_addr(pp, crc_result, queue);
1811
1812 return 0;
1813}
1814
1815/* Configure Fitering mode of Ethernet port */
1816static void mvneta_rx_unicast_promisc_set(struct mvneta_port *pp,
1817 int is_promisc)
1818{
1819 u32 port_cfg_reg, val;
1820
1821 port_cfg_reg = mvreg_read(pp, MVNETA_PORT_CONFIG);
1822
1823 val = mvreg_read(pp, MVNETA_TYPE_PRIO);
1824
1825 /* Set / Clear UPM bit in port configuration register */
1826 if (is_promisc) {
1827 /* Accept all Unicast addresses */
1828 port_cfg_reg |= MVNETA_UNI_PROMISC_MODE;
1829 val |= MVNETA_FORCE_UNI;
1830 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, 0xffff);
1831 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, 0xffffffff);
1832 } else {
1833 /* Reject all Unicast addresses */
1834 port_cfg_reg &= ~MVNETA_UNI_PROMISC_MODE;
1835 val &= ~MVNETA_FORCE_UNI;
1836 }
1837
1838 mvreg_write(pp, MVNETA_PORT_CONFIG, port_cfg_reg);
1839 mvreg_write(pp, MVNETA_TYPE_PRIO, val);
1840}
1841
1842/* register unicast and multicast addresses */
1843static void mvneta_set_rx_mode(struct net_device *dev)
1844{
1845 struct mvneta_port *pp = netdev_priv(dev);
1846 struct netdev_hw_addr *ha;
1847
1848 if (dev->flags & IFF_PROMISC) {
1849 /* Accept all: Multicast + Unicast */
1850 mvneta_rx_unicast_promisc_set(pp, 1);
1851 mvneta_set_ucast_table(pp, rxq_def);
1852 mvneta_set_special_mcast_table(pp, rxq_def);
1853 mvneta_set_other_mcast_table(pp, rxq_def);
1854 } else {
1855 /* Accept single Unicast */
1856 mvneta_rx_unicast_promisc_set(pp, 0);
1857 mvneta_set_ucast_table(pp, -1);
1858 mvneta_mac_addr_set(pp, dev->dev_addr, rxq_def);
1859
1860 if (dev->flags & IFF_ALLMULTI) {
1861 /* Accept all multicast */
1862 mvneta_set_special_mcast_table(pp, rxq_def);
1863 mvneta_set_other_mcast_table(pp, rxq_def);
1864 } else {
1865 /* Accept only initialized multicast */
1866 mvneta_set_special_mcast_table(pp, -1);
1867 mvneta_set_other_mcast_table(pp, -1);
1868
1869 if (!netdev_mc_empty(dev)) {
1870 netdev_for_each_mc_addr(ha, dev) {
1871 mvneta_mcast_addr_set(pp, ha->addr,
1872 rxq_def);
1873 }
1874 }
1875 }
1876 }
1877}
1878
1879/* Interrupt handling - the callback for request_irq() */
1880static irqreturn_t mvneta_isr(int irq, void *dev_id)
1881{
1882 struct mvneta_port *pp = (struct mvneta_port *)dev_id;
1883
1884 /* Mask all interrupts */
1885 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
1886
1887 napi_schedule(&pp->napi);
1888
1889 return IRQ_HANDLED;
1890}
1891
1892/* NAPI handler
1893 * Bits 0 - 7 of the causeRxTx register indicate that are transmitted
1894 * packets on the corresponding TXQ (Bit 0 is for TX queue 1).
1895 * Bits 8 -15 of the cause Rx Tx register indicate that are received
1896 * packets on the corresponding RXQ (Bit 8 is for RX queue 0).
1897 * Each CPU has its own causeRxTx register
1898 */
1899static int mvneta_poll(struct napi_struct *napi, int budget)
1900{
1901 int rx_done = 0;
1902 u32 cause_rx_tx;
1903 unsigned long flags;
1904 struct mvneta_port *pp = netdev_priv(napi->dev);
1905
1906 if (!netif_running(pp->dev)) {
1907 napi_complete(napi);
1908 return rx_done;
1909 }
1910
1911 /* Read cause register */
1912 cause_rx_tx = mvreg_read(pp, MVNETA_INTR_NEW_CAUSE) &
71f6d1b3 1913 (MVNETA_RX_INTR_MASK(rxq_number) | MVNETA_TX_INTR_MASK(txq_number));
1914
1915 /* Release Tx descriptors */
1916 if (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL) {
1917 int tx_todo = 0;
1918
1919 mvneta_tx_done_gbe(pp, (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL), &tx_todo);
1920 cause_rx_tx &= ~MVNETA_TX_INTR_MASK_ALL;
1921 }
c5aff182 1922
6a20c175 1923 /* For the case where the last mvneta_poll did not process all
c5aff182
TP
1924 * RX packets
1925 */
1926 cause_rx_tx |= pp->cause_rx_tx;
1927 if (rxq_number > 1) {
71f6d1b3 1928 while ((cause_rx_tx & MVNETA_RX_INTR_MASK_ALL) && (budget > 0)) {
c5aff182
TP
1929 int count;
1930 struct mvneta_rx_queue *rxq;
1931 /* get rx queue number from cause_rx_tx */
1932 rxq = mvneta_rx_policy(pp, cause_rx_tx);
1933 if (!rxq)
1934 break;
1935
1936 /* process the packet in that rx queue */
1937 count = mvneta_rx(pp, budget, rxq);
1938 rx_done += count;
1939 budget -= count;
1940 if (budget > 0) {
6a20c175
TP
1941 /* set off the rx bit of the
1942 * corresponding bit in the cause rx
1943 * tx register, so that next iteration
1944 * will find the next rx queue where
1945 * packets are received on
1946 */
c5aff182
TP
1947 cause_rx_tx &= ~((1 << rxq->id) << 8);
1948 }
1949 }
1950 } else {
1951 rx_done = mvneta_rx(pp, budget, &pp->rxqs[rxq_def]);
1952 budget -= rx_done;
1953 }
1954
1955 if (budget > 0) {
1956 cause_rx_tx = 0;
1957 napi_complete(napi);
1958 local_irq_save(flags);
1959 mvreg_write(pp, MVNETA_INTR_NEW_MASK,
71f6d1b3 1960 MVNETA_RX_INTR_MASK(rxq_number) | MVNETA_TX_INTR_MASK(txq_number));
c5aff182
TP
1961 local_irq_restore(flags);
1962 }
1963
1964 pp->cause_rx_tx = cause_rx_tx;
1965 return rx_done;
1966}
1967
c5aff182
TP
1968/* Handle rxq fill: allocates rxq skbs; called when initializing a port */
1969static int mvneta_rxq_fill(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
1970 int num)
1971{
1972 struct net_device *dev = pp->dev;
1973 int i;
1974
1975 for (i = 0; i < num; i++) {
1976 struct sk_buff *skb;
1977 struct mvneta_rx_desc *rx_desc;
1978 unsigned long phys_addr;
1979
1980 skb = dev_alloc_skb(pp->pkt_size);
1981 if (!skb) {
1982 netdev_err(dev, "%s:rxq %d, %d of %d buffs filled\n",
1983 __func__, rxq->id, i, num);
1984 break;
1985 }
1986
1987 rx_desc = rxq->descs + i;
1988 memset(rx_desc, 0, sizeof(struct mvneta_rx_desc));
1989 phys_addr = dma_map_single(dev->dev.parent, skb->head,
1990 MVNETA_RX_BUF_SIZE(pp->pkt_size),
1991 DMA_FROM_DEVICE);
1992 if (unlikely(dma_mapping_error(dev->dev.parent, phys_addr))) {
1993 dev_kfree_skb(skb);
1994 break;
1995 }
1996
1997 mvneta_rx_desc_fill(rx_desc, phys_addr, (u32)skb);
1998 }
1999
2000 /* Add this number of RX descriptors as non occupied (ready to
6a20c175
TP
2001 * get packets)
2002 */
c5aff182
TP
2003 mvneta_rxq_non_occup_desc_add(pp, rxq, i);
2004
2005 return i;
2006}
2007
2008/* Free all packets pending transmit from all TXQs and reset TX port */
2009static void mvneta_tx_reset(struct mvneta_port *pp)
2010{
2011 int queue;
2012
2013 /* free the skb's in the hal tx ring */
2014 for (queue = 0; queue < txq_number; queue++)
2015 mvneta_txq_done_force(pp, &pp->txqs[queue]);
2016
2017 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
2018 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
2019}
2020
2021static void mvneta_rx_reset(struct mvneta_port *pp)
2022{
2023 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
2024 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
2025}
2026
2027/* Rx/Tx queue initialization/cleanup methods */
2028
2029/* Create a specified RX queue */
2030static int mvneta_rxq_init(struct mvneta_port *pp,
2031 struct mvneta_rx_queue *rxq)
2032
2033{
2034 rxq->size = pp->rx_ring_size;
2035
2036 /* Allocate memory for RX descriptors */
2037 rxq->descs = dma_alloc_coherent(pp->dev->dev.parent,
2038 rxq->size * MVNETA_DESC_ALIGNED_SIZE,
2039 &rxq->descs_phys, GFP_KERNEL);
d0320f75 2040 if (rxq->descs == NULL)
c5aff182 2041 return -ENOMEM;
c5aff182
TP
2042
2043 BUG_ON(rxq->descs !=
2044 PTR_ALIGN(rxq->descs, MVNETA_CPU_D_CACHE_LINE_SIZE));
2045
2046 rxq->last_desc = rxq->size - 1;
2047
2048 /* Set Rx descriptors queue starting address */
2049 mvreg_write(pp, MVNETA_RXQ_BASE_ADDR_REG(rxq->id), rxq->descs_phys);
2050 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), rxq->size);
2051
2052 /* Set Offset */
2053 mvneta_rxq_offset_set(pp, rxq, NET_SKB_PAD);
2054
2055 /* Set coalescing pkts and time */
2056 mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
2057 mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
2058
2059 /* Fill RXQ with buffers from RX pool */
2060 mvneta_rxq_buf_size_set(pp, rxq, MVNETA_RX_BUF_SIZE(pp->pkt_size));
2061 mvneta_rxq_bm_disable(pp, rxq);
2062 mvneta_rxq_fill(pp, rxq, rxq->size);
2063
2064 return 0;
2065}
2066
2067/* Cleanup Rx queue */
2068static void mvneta_rxq_deinit(struct mvneta_port *pp,
2069 struct mvneta_rx_queue *rxq)
2070{
2071 mvneta_rxq_drop_pkts(pp, rxq);
2072
2073 if (rxq->descs)
2074 dma_free_coherent(pp->dev->dev.parent,
2075 rxq->size * MVNETA_DESC_ALIGNED_SIZE,
2076 rxq->descs,
2077 rxq->descs_phys);
2078
2079 rxq->descs = NULL;
2080 rxq->last_desc = 0;
2081 rxq->next_desc_to_proc = 0;
2082 rxq->descs_phys = 0;
2083}
2084
2085/* Create and initialize a tx queue */
2086static int mvneta_txq_init(struct mvneta_port *pp,
2087 struct mvneta_tx_queue *txq)
2088{
2089 txq->size = pp->tx_ring_size;
2090
2091 /* Allocate memory for TX descriptors */
2092 txq->descs = dma_alloc_coherent(pp->dev->dev.parent,
2093 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2094 &txq->descs_phys, GFP_KERNEL);
d0320f75 2095 if (txq->descs == NULL)
c5aff182 2096 return -ENOMEM;
c5aff182
TP
2097
2098 /* Make sure descriptor address is cache line size aligned */
2099 BUG_ON(txq->descs !=
2100 PTR_ALIGN(txq->descs, MVNETA_CPU_D_CACHE_LINE_SIZE));
2101
2102 txq->last_desc = txq->size - 1;
2103
2104 /* Set maximum bandwidth for enabled TXQs */
2105 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0x03ffffff);
2106 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0x3fffffff);
2107
2108 /* Set Tx descriptors queue starting address */
2109 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), txq->descs_phys);
2110 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), txq->size);
2111
2112 txq->tx_skb = kmalloc(txq->size * sizeof(*txq->tx_skb), GFP_KERNEL);
2113 if (txq->tx_skb == NULL) {
2114 dma_free_coherent(pp->dev->dev.parent,
2115 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2116 txq->descs, txq->descs_phys);
2117 return -ENOMEM;
2118 }
2119 mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
2120
2121 return 0;
2122}
2123
2124/* Free allocated resources when mvneta_txq_init() fails to allocate memory*/
2125static void mvneta_txq_deinit(struct mvneta_port *pp,
2126 struct mvneta_tx_queue *txq)
2127{
2128 kfree(txq->tx_skb);
2129
2130 if (txq->descs)
2131 dma_free_coherent(pp->dev->dev.parent,
2132 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2133 txq->descs, txq->descs_phys);
2134
2135 txq->descs = NULL;
2136 txq->last_desc = 0;
2137 txq->next_desc_to_proc = 0;
2138 txq->descs_phys = 0;
2139
2140 /* Set minimum bandwidth for disabled TXQs */
2141 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0);
2142 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0);
2143
2144 /* Set Tx descriptors queue starting address and size */
2145 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), 0);
2146 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), 0);
2147}
2148
2149/* Cleanup all Tx queues */
2150static void mvneta_cleanup_txqs(struct mvneta_port *pp)
2151{
2152 int queue;
2153
2154 for (queue = 0; queue < txq_number; queue++)
2155 mvneta_txq_deinit(pp, &pp->txqs[queue]);
2156}
2157
2158/* Cleanup all Rx queues */
2159static void mvneta_cleanup_rxqs(struct mvneta_port *pp)
2160{
2161 int queue;
2162
2163 for (queue = 0; queue < rxq_number; queue++)
2164 mvneta_rxq_deinit(pp, &pp->rxqs[queue]);
2165}
2166
2167
2168/* Init all Rx queues */
2169static int mvneta_setup_rxqs(struct mvneta_port *pp)
2170{
2171 int queue;
2172
2173 for (queue = 0; queue < rxq_number; queue++) {
2174 int err = mvneta_rxq_init(pp, &pp->rxqs[queue]);
2175 if (err) {
2176 netdev_err(pp->dev, "%s: can't create rxq=%d\n",
2177 __func__, queue);
2178 mvneta_cleanup_rxqs(pp);
2179 return err;
2180 }
2181 }
2182
2183 return 0;
2184}
2185
2186/* Init all tx queues */
2187static int mvneta_setup_txqs(struct mvneta_port *pp)
2188{
2189 int queue;
2190
2191 for (queue = 0; queue < txq_number; queue++) {
2192 int err = mvneta_txq_init(pp, &pp->txqs[queue]);
2193 if (err) {
2194 netdev_err(pp->dev, "%s: can't create txq=%d\n",
2195 __func__, queue);
2196 mvneta_cleanup_txqs(pp);
2197 return err;
2198 }
2199 }
2200
2201 return 0;
2202}
2203
2204static void mvneta_start_dev(struct mvneta_port *pp)
2205{
2206 mvneta_max_rx_size_set(pp, pp->pkt_size);
2207 mvneta_txq_max_tx_size_set(pp, pp->pkt_size);
2208
2209 /* start the Rx/Tx activity */
2210 mvneta_port_enable(pp);
2211
2212 /* Enable polling on the port */
2213 napi_enable(&pp->napi);
2214
2215 /* Unmask interrupts */
2216 mvreg_write(pp, MVNETA_INTR_NEW_MASK,
71f6d1b3 2217 MVNETA_RX_INTR_MASK(rxq_number) | MVNETA_TX_INTR_MASK(txq_number));
c5aff182
TP
2218
2219 phy_start(pp->phy_dev);
2220 netif_tx_start_all_queues(pp->dev);
2221}
2222
2223static void mvneta_stop_dev(struct mvneta_port *pp)
2224{
2225 phy_stop(pp->phy_dev);
2226
2227 napi_disable(&pp->napi);
2228
2229 netif_carrier_off(pp->dev);
2230
2231 mvneta_port_down(pp);
2232 netif_tx_stop_all_queues(pp->dev);
2233
2234 /* Stop the port activity */
2235 mvneta_port_disable(pp);
2236
2237 /* Clear all ethernet port interrupts */
2238 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
2239 mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0);
2240
2241 /* Mask all ethernet port interrupts */
2242 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
2243 mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0);
2244 mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0);
2245
2246 mvneta_tx_reset(pp);
2247 mvneta_rx_reset(pp);
2248}
2249
c5aff182
TP
2250/* Return positive if MTU is valid */
2251static int mvneta_check_mtu_valid(struct net_device *dev, int mtu)
2252{
2253 if (mtu < 68) {
2254 netdev_err(dev, "cannot change mtu to less than 68\n");
2255 return -EINVAL;
2256 }
2257
6a20c175 2258 /* 9676 == 9700 - 20 and rounding to 8 */
c5aff182
TP
2259 if (mtu > 9676) {
2260 netdev_info(dev, "Illegal MTU value %d, round to 9676\n", mtu);
2261 mtu = 9676;
2262 }
2263
2264 if (!IS_ALIGNED(MVNETA_RX_PKT_SIZE(mtu), 8)) {
2265 netdev_info(dev, "Illegal MTU value %d, rounding to %d\n",
2266 mtu, ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8));
2267 mtu = ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8);
2268 }
2269
2270 return mtu;
2271}
2272
2273/* Change the device mtu */
2274static int mvneta_change_mtu(struct net_device *dev, int mtu)
2275{
2276 struct mvneta_port *pp = netdev_priv(dev);
2277 int ret;
2278
2279 mtu = mvneta_check_mtu_valid(dev, mtu);
2280 if (mtu < 0)
2281 return -EINVAL;
2282
2283 dev->mtu = mtu;
2284
2285 if (!netif_running(dev))
2286 return 0;
2287
6a20c175 2288 /* The interface is running, so we have to force a
c5aff182
TP
2289 * reallocation of the RXQs
2290 */
2291 mvneta_stop_dev(pp);
2292
2293 mvneta_cleanup_txqs(pp);
2294 mvneta_cleanup_rxqs(pp);
2295
2296 pp->pkt_size = MVNETA_RX_PKT_SIZE(pp->dev->mtu);
2297
2298 ret = mvneta_setup_rxqs(pp);
2299 if (ret) {
2300 netdev_err(pp->dev, "unable to setup rxqs after MTU change\n");
2301 return ret;
2302 }
2303
2304 mvneta_setup_txqs(pp);
2305
2306 mvneta_start_dev(pp);
2307 mvneta_port_up(pp);
2308
2309 return 0;
2310}
2311
8cc3e439
TP
2312/* Get mac address */
2313static void mvneta_get_mac_addr(struct mvneta_port *pp, unsigned char *addr)
2314{
2315 u32 mac_addr_l, mac_addr_h;
2316
2317 mac_addr_l = mvreg_read(pp, MVNETA_MAC_ADDR_LOW);
2318 mac_addr_h = mvreg_read(pp, MVNETA_MAC_ADDR_HIGH);
2319 addr[0] = (mac_addr_h >> 24) & 0xFF;
2320 addr[1] = (mac_addr_h >> 16) & 0xFF;
2321 addr[2] = (mac_addr_h >> 8) & 0xFF;
2322 addr[3] = mac_addr_h & 0xFF;
2323 addr[4] = (mac_addr_l >> 8) & 0xFF;
2324 addr[5] = mac_addr_l & 0xFF;
2325}
2326
c5aff182
TP
2327/* Handle setting mac address */
2328static int mvneta_set_mac_addr(struct net_device *dev, void *addr)
2329{
2330 struct mvneta_port *pp = netdev_priv(dev);
2331 u8 *mac = addr + 2;
2332 int i;
2333
2334 if (netif_running(dev))
2335 return -EBUSY;
2336
2337 /* Remove previous address table entry */
2338 mvneta_mac_addr_set(pp, dev->dev_addr, -1);
2339
2340 /* Set new addr in hw */
2341 mvneta_mac_addr_set(pp, mac, rxq_def);
2342
2343 /* Set addr in the device */
2344 for (i = 0; i < ETH_ALEN; i++)
2345 dev->dev_addr[i] = mac[i];
2346
2347 return 0;
2348}
2349
2350static void mvneta_adjust_link(struct net_device *ndev)
2351{
2352 struct mvneta_port *pp = netdev_priv(ndev);
2353 struct phy_device *phydev = pp->phy_dev;
2354 int status_change = 0;
2355
2356 if (phydev->link) {
2357 if ((pp->speed != phydev->speed) ||
2358 (pp->duplex != phydev->duplex)) {
2359 u32 val;
2360
2361 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
2362 val &= ~(MVNETA_GMAC_CONFIG_MII_SPEED |
2363 MVNETA_GMAC_CONFIG_GMII_SPEED |
71408602
TP
2364 MVNETA_GMAC_CONFIG_FULL_DUPLEX |
2365 MVNETA_GMAC_AN_SPEED_EN |
2366 MVNETA_GMAC_AN_DUPLEX_EN);
c5aff182
TP
2367
2368 if (phydev->duplex)
2369 val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
2370
2371 if (phydev->speed == SPEED_1000)
2372 val |= MVNETA_GMAC_CONFIG_GMII_SPEED;
2373 else
2374 val |= MVNETA_GMAC_CONFIG_MII_SPEED;
2375
2376 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
2377
2378 pp->duplex = phydev->duplex;
2379 pp->speed = phydev->speed;
2380 }
2381 }
2382
2383 if (phydev->link != pp->link) {
2384 if (!phydev->link) {
2385 pp->duplex = -1;
2386 pp->speed = 0;
2387 }
2388
2389 pp->link = phydev->link;
2390 status_change = 1;
2391 }
2392
2393 if (status_change) {
2394 if (phydev->link) {
2395 u32 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
2396 val |= (MVNETA_GMAC_FORCE_LINK_PASS |
2397 MVNETA_GMAC_FORCE_LINK_DOWN);
2398 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
2399 mvneta_port_up(pp);
2400 netdev_info(pp->dev, "link up\n");
2401 } else {
2402 mvneta_port_down(pp);
2403 netdev_info(pp->dev, "link down\n");
2404 }
2405 }
2406}
2407
2408static int mvneta_mdio_probe(struct mvneta_port *pp)
2409{
2410 struct phy_device *phy_dev;
2411
2412 phy_dev = of_phy_connect(pp->dev, pp->phy_node, mvneta_adjust_link, 0,
2413 pp->phy_interface);
2414 if (!phy_dev) {
2415 netdev_err(pp->dev, "could not find the PHY\n");
2416 return -ENODEV;
2417 }
2418
2419 phy_dev->supported &= PHY_GBIT_FEATURES;
2420 phy_dev->advertising = phy_dev->supported;
2421
2422 pp->phy_dev = phy_dev;
2423 pp->link = 0;
2424 pp->duplex = 0;
2425 pp->speed = 0;
2426
2427 return 0;
2428}
2429
2430static void mvneta_mdio_remove(struct mvneta_port *pp)
2431{
2432 phy_disconnect(pp->phy_dev);
2433 pp->phy_dev = NULL;
2434}
2435
2436static int mvneta_open(struct net_device *dev)
2437{
2438 struct mvneta_port *pp = netdev_priv(dev);
2439 int ret;
2440
2441 mvneta_mac_addr_set(pp, dev->dev_addr, rxq_def);
2442
2443 pp->pkt_size = MVNETA_RX_PKT_SIZE(pp->dev->mtu);
2444
2445 ret = mvneta_setup_rxqs(pp);
2446 if (ret)
2447 return ret;
2448
2449 ret = mvneta_setup_txqs(pp);
2450 if (ret)
2451 goto err_cleanup_rxqs;
2452
2453 /* Connect to port interrupt line */
2454 ret = request_irq(pp->dev->irq, mvneta_isr, 0,
2455 MVNETA_DRIVER_NAME, pp);
2456 if (ret) {
2457 netdev_err(pp->dev, "cannot request irq %d\n", pp->dev->irq);
2458 goto err_cleanup_txqs;
2459 }
2460
2461 /* In default link is down */
2462 netif_carrier_off(pp->dev);
2463
2464 ret = mvneta_mdio_probe(pp);
2465 if (ret < 0) {
2466 netdev_err(dev, "cannot probe MDIO bus\n");
2467 goto err_free_irq;
2468 }
2469
2470 mvneta_start_dev(pp);
2471
2472 return 0;
2473
2474err_free_irq:
2475 free_irq(pp->dev->irq, pp);
2476err_cleanup_txqs:
2477 mvneta_cleanup_txqs(pp);
2478err_cleanup_rxqs:
2479 mvneta_cleanup_rxqs(pp);
2480 return ret;
2481}
2482
2483/* Stop the port, free port interrupt line */
2484static int mvneta_stop(struct net_device *dev)
2485{
2486 struct mvneta_port *pp = netdev_priv(dev);
2487
2488 mvneta_stop_dev(pp);
2489 mvneta_mdio_remove(pp);
2490 free_irq(dev->irq, pp);
2491 mvneta_cleanup_rxqs(pp);
2492 mvneta_cleanup_txqs(pp);
c5aff182
TP
2493
2494 return 0;
2495}
2496
15f59456
TP
2497static int mvneta_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2498{
2499 struct mvneta_port *pp = netdev_priv(dev);
2500 int ret;
2501
2502 if (!pp->phy_dev)
2503 return -ENOTSUPP;
2504
2505 ret = phy_mii_ioctl(pp->phy_dev, ifr, cmd);
2506 if (!ret)
2507 mvneta_adjust_link(dev);
2508
2509 return ret;
2510}
2511
c5aff182
TP
2512/* Ethtool methods */
2513
2514/* Get settings (phy address, speed) for ethtools */
2515int mvneta_ethtool_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2516{
2517 struct mvneta_port *pp = netdev_priv(dev);
2518
2519 if (!pp->phy_dev)
2520 return -ENODEV;
2521
2522 return phy_ethtool_gset(pp->phy_dev, cmd);
2523}
2524
2525/* Set settings (phy address, speed) for ethtools */
2526int mvneta_ethtool_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2527{
2528 struct mvneta_port *pp = netdev_priv(dev);
2529
2530 if (!pp->phy_dev)
2531 return -ENODEV;
2532
2533 return phy_ethtool_sset(pp->phy_dev, cmd);
2534}
2535
2536/* Set interrupt coalescing for ethtools */
2537static int mvneta_ethtool_set_coalesce(struct net_device *dev,
2538 struct ethtool_coalesce *c)
2539{
2540 struct mvneta_port *pp = netdev_priv(dev);
2541 int queue;
2542
2543 for (queue = 0; queue < rxq_number; queue++) {
2544 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
2545 rxq->time_coal = c->rx_coalesce_usecs;
2546 rxq->pkts_coal = c->rx_max_coalesced_frames;
2547 mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
2548 mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
2549 }
2550
2551 for (queue = 0; queue < txq_number; queue++) {
2552 struct mvneta_tx_queue *txq = &pp->txqs[queue];
2553 txq->done_pkts_coal = c->tx_max_coalesced_frames;
2554 mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
2555 }
2556
2557 return 0;
2558}
2559
2560/* get coalescing for ethtools */
2561static int mvneta_ethtool_get_coalesce(struct net_device *dev,
2562 struct ethtool_coalesce *c)
2563{
2564 struct mvneta_port *pp = netdev_priv(dev);
2565
2566 c->rx_coalesce_usecs = pp->rxqs[0].time_coal;
2567 c->rx_max_coalesced_frames = pp->rxqs[0].pkts_coal;
2568
2569 c->tx_max_coalesced_frames = pp->txqs[0].done_pkts_coal;
2570 return 0;
2571}
2572
2573
2574static void mvneta_ethtool_get_drvinfo(struct net_device *dev,
2575 struct ethtool_drvinfo *drvinfo)
2576{
2577 strlcpy(drvinfo->driver, MVNETA_DRIVER_NAME,
2578 sizeof(drvinfo->driver));
2579 strlcpy(drvinfo->version, MVNETA_DRIVER_VERSION,
2580 sizeof(drvinfo->version));
2581 strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
2582 sizeof(drvinfo->bus_info));
2583}
2584
2585
2586static void mvneta_ethtool_get_ringparam(struct net_device *netdev,
2587 struct ethtool_ringparam *ring)
2588{
2589 struct mvneta_port *pp = netdev_priv(netdev);
2590
2591 ring->rx_max_pending = MVNETA_MAX_RXD;
2592 ring->tx_max_pending = MVNETA_MAX_TXD;
2593 ring->rx_pending = pp->rx_ring_size;
2594 ring->tx_pending = pp->tx_ring_size;
2595}
2596
2597static int mvneta_ethtool_set_ringparam(struct net_device *dev,
2598 struct ethtool_ringparam *ring)
2599{
2600 struct mvneta_port *pp = netdev_priv(dev);
2601
2602 if ((ring->rx_pending == 0) || (ring->tx_pending == 0))
2603 return -EINVAL;
2604 pp->rx_ring_size = ring->rx_pending < MVNETA_MAX_RXD ?
2605 ring->rx_pending : MVNETA_MAX_RXD;
2606 pp->tx_ring_size = ring->tx_pending < MVNETA_MAX_TXD ?
2607 ring->tx_pending : MVNETA_MAX_TXD;
2608
2609 if (netif_running(dev)) {
2610 mvneta_stop(dev);
2611 if (mvneta_open(dev)) {
2612 netdev_err(dev,
2613 "error on opening device after ring param change\n");
2614 return -ENOMEM;
2615 }
2616 }
2617
2618 return 0;
2619}
2620
2621static const struct net_device_ops mvneta_netdev_ops = {
2622 .ndo_open = mvneta_open,
2623 .ndo_stop = mvneta_stop,
2624 .ndo_start_xmit = mvneta_tx,
2625 .ndo_set_rx_mode = mvneta_set_rx_mode,
2626 .ndo_set_mac_address = mvneta_set_mac_addr,
2627 .ndo_change_mtu = mvneta_change_mtu,
c5aff182 2628 .ndo_get_stats64 = mvneta_get_stats64,
15f59456 2629 .ndo_do_ioctl = mvneta_ioctl,
c5aff182
TP
2630};
2631
2632const struct ethtool_ops mvneta_eth_tool_ops = {
2633 .get_link = ethtool_op_get_link,
2634 .get_settings = mvneta_ethtool_get_settings,
2635 .set_settings = mvneta_ethtool_set_settings,
2636 .set_coalesce = mvneta_ethtool_set_coalesce,
2637 .get_coalesce = mvneta_ethtool_get_coalesce,
2638 .get_drvinfo = mvneta_ethtool_get_drvinfo,
2639 .get_ringparam = mvneta_ethtool_get_ringparam,
2640 .set_ringparam = mvneta_ethtool_set_ringparam,
2641};
2642
2643/* Initialize hw */
03ce758e 2644static int mvneta_init(struct mvneta_port *pp, int phy_addr)
c5aff182
TP
2645{
2646 int queue;
2647
2648 /* Disable port */
2649 mvneta_port_disable(pp);
2650
2651 /* Set port default values */
2652 mvneta_defaults_set(pp);
2653
2654 pp->txqs = kzalloc(txq_number * sizeof(struct mvneta_tx_queue),
2655 GFP_KERNEL);
2656 if (!pp->txqs)
2657 return -ENOMEM;
2658
2659 /* Initialize TX descriptor rings */
2660 for (queue = 0; queue < txq_number; queue++) {
2661 struct mvneta_tx_queue *txq = &pp->txqs[queue];
2662 txq->id = queue;
2663 txq->size = pp->tx_ring_size;
2664 txq->done_pkts_coal = MVNETA_TXDONE_COAL_PKTS;
2665 }
2666
2667 pp->rxqs = kzalloc(rxq_number * sizeof(struct mvneta_rx_queue),
2668 GFP_KERNEL);
2669 if (!pp->rxqs) {
2670 kfree(pp->txqs);
2671 return -ENOMEM;
2672 }
2673
2674 /* Create Rx descriptor rings */
2675 for (queue = 0; queue < rxq_number; queue++) {
2676 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
2677 rxq->id = queue;
2678 rxq->size = pp->rx_ring_size;
2679 rxq->pkts_coal = MVNETA_RX_COAL_PKTS;
2680 rxq->time_coal = MVNETA_RX_COAL_USEC;
2681 }
2682
2683 return 0;
2684}
2685
70eeaf98 2686static void mvneta_deinit(struct mvneta_port *pp)
c5aff182
TP
2687{
2688 kfree(pp->txqs);
2689 kfree(pp->rxqs);
2690}
2691
2692/* platform glue : initialize decoding windows */
03ce758e
GK
2693static void mvneta_conf_mbus_windows(struct mvneta_port *pp,
2694 const struct mbus_dram_target_info *dram)
c5aff182
TP
2695{
2696 u32 win_enable;
2697 u32 win_protect;
2698 int i;
2699
2700 for (i = 0; i < 6; i++) {
2701 mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
2702 mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
2703
2704 if (i < 4)
2705 mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
2706 }
2707
2708 win_enable = 0x3f;
2709 win_protect = 0;
2710
2711 for (i = 0; i < dram->num_cs; i++) {
2712 const struct mbus_dram_window *cs = dram->cs + i;
2713 mvreg_write(pp, MVNETA_WIN_BASE(i), (cs->base & 0xffff0000) |
2714 (cs->mbus_attr << 8) | dram->mbus_dram_target_id);
2715
2716 mvreg_write(pp, MVNETA_WIN_SIZE(i),
2717 (cs->size - 1) & 0xffff0000);
2718
2719 win_enable &= ~(1 << i);
2720 win_protect |= 3 << (2 * i);
2721 }
2722
2723 mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
2724}
2725
2726/* Power up the port */
03ce758e 2727static void mvneta_port_power_up(struct mvneta_port *pp, int phy_mode)
c5aff182
TP
2728{
2729 u32 val;
2730
2731 /* MAC Cause register should be cleared */
2732 mvreg_write(pp, MVNETA_UNIT_INTR_CAUSE, 0);
2733
2734 if (phy_mode == PHY_INTERFACE_MODE_SGMII)
2735 mvneta_port_sgmii_config(pp);
2736
2737 mvneta_gmac_rgmii_set(pp, 1);
2738
2739 /* Cancel Port Reset */
2740 val = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
2741 val &= ~MVNETA_GMAC2_PORT_RESET;
2742 mvreg_write(pp, MVNETA_GMAC_CTRL_2, val);
2743
2744 while ((mvreg_read(pp, MVNETA_GMAC_CTRL_2) &
2745 MVNETA_GMAC2_PORT_RESET) != 0)
2746 continue;
2747}
2748
2749/* Device initialization routine */
03ce758e 2750static int mvneta_probe(struct platform_device *pdev)
c5aff182
TP
2751{
2752 const struct mbus_dram_target_info *dram_target_info;
2753 struct device_node *dn = pdev->dev.of_node;
2754 struct device_node *phy_node;
189dd626 2755 u32 phy_addr;
c5aff182
TP
2756 struct mvneta_port *pp;
2757 struct net_device *dev;
8cc3e439
TP
2758 const char *dt_mac_addr;
2759 char hw_mac_addr[ETH_ALEN];
2760 const char *mac_from;
c5aff182
TP
2761 int phy_mode;
2762 int err;
74c41b04 2763 int cpu;
c5aff182 2764
6a20c175 2765 /* Our multiqueue support is not complete, so for now, only
c5aff182
TP
2766 * allow the usage of the first RX queue
2767 */
2768 if (rxq_def != 0) {
2769 dev_err(&pdev->dev, "Invalid rxq_def argument: %d\n", rxq_def);
2770 return -EINVAL;
2771 }
2772
ee40a116 2773 dev = alloc_etherdev_mqs(sizeof(struct mvneta_port), txq_number, rxq_number);
c5aff182
TP
2774 if (!dev)
2775 return -ENOMEM;
2776
2777 dev->irq = irq_of_parse_and_map(dn, 0);
2778 if (dev->irq == 0) {
2779 err = -EINVAL;
2780 goto err_free_netdev;
2781 }
2782
2783 phy_node = of_parse_phandle(dn, "phy", 0);
2784 if (!phy_node) {
2785 dev_err(&pdev->dev, "no associated PHY\n");
2786 err = -ENODEV;
2787 goto err_free_irq;
2788 }
2789
2790 phy_mode = of_get_phy_mode(dn);
2791 if (phy_mode < 0) {
2792 dev_err(&pdev->dev, "incorrect phy-mode\n");
2793 err = -EINVAL;
2794 goto err_free_irq;
2795 }
2796
c5aff182
TP
2797 dev->tx_queue_len = MVNETA_MAX_TXD;
2798 dev->watchdog_timeo = 5 * HZ;
2799 dev->netdev_ops = &mvneta_netdev_ops;
2800
2801 SET_ETHTOOL_OPS(dev, &mvneta_eth_tool_ops);
2802
2803 pp = netdev_priv(dev);
2804
c5aff182 2805 pp->weight = MVNETA_RX_POLL_WEIGHT;
c5aff182
TP
2806 pp->phy_node = phy_node;
2807 pp->phy_interface = phy_mode;
2808
189dd626
TP
2809 pp->clk = devm_clk_get(&pdev->dev, NULL);
2810 if (IS_ERR(pp->clk)) {
2811 err = PTR_ERR(pp->clk);
5445eaf3 2812 goto err_free_irq;
189dd626
TP
2813 }
2814
2815 clk_prepare_enable(pp->clk);
2816
5445eaf3
APR
2817 pp->base = of_iomap(dn, 0);
2818 if (pp->base == NULL) {
2819 err = -ENOMEM;
2820 goto err_clk;
2821 }
2822
74c41b04 2823 /* Alloc per-cpu stats */
2824 pp->stats = alloc_percpu(struct mvneta_pcpu_stats);
2825 if (!pp->stats) {
2826 err = -ENOMEM;
2827 goto err_unmap;
2828 }
2829
2830 for_each_possible_cpu(cpu) {
2831 struct mvneta_pcpu_stats *stats;
2832 stats = per_cpu_ptr(pp->stats, cpu);
2833 u64_stats_init(&stats->syncp);
2834 }
2835
8cc3e439 2836 dt_mac_addr = of_get_mac_address(dn);
6c7a9a3c 2837 if (dt_mac_addr) {
8cc3e439
TP
2838 mac_from = "device tree";
2839 memcpy(dev->dev_addr, dt_mac_addr, ETH_ALEN);
2840 } else {
2841 mvneta_get_mac_addr(pp, hw_mac_addr);
2842 if (is_valid_ether_addr(hw_mac_addr)) {
2843 mac_from = "hardware";
2844 memcpy(dev->dev_addr, hw_mac_addr, ETH_ALEN);
2845 } else {
2846 mac_from = "random";
2847 eth_hw_addr_random(dev);
2848 }
2849 }
2850
c5aff182
TP
2851 pp->tx_ring_size = MVNETA_MAX_TXD;
2852 pp->rx_ring_size = MVNETA_MAX_RXD;
2853
2854 pp->dev = dev;
2855 SET_NETDEV_DEV(dev, &pdev->dev);
2856
2857 err = mvneta_init(pp, phy_addr);
2858 if (err < 0) {
2859 dev_err(&pdev->dev, "can't init eth hal\n");
74c41b04 2860 goto err_free_stats;
c5aff182
TP
2861 }
2862 mvneta_port_power_up(pp, phy_mode);
2863
2864 dram_target_info = mv_mbus_dram_info();
2865 if (dram_target_info)
2866 mvneta_conf_mbus_windows(pp, dram_target_info);
2867
2868 netif_napi_add(dev, &pp->napi, mvneta_poll, pp->weight);
2869
b50b72de 2870 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM;
2871 dev->hw_features |= NETIF_F_SG | NETIF_F_IP_CSUM;
2872 dev->vlan_features |= NETIF_F_SG | NETIF_F_IP_CSUM;
2873 dev->priv_flags |= IFF_UNICAST_FLT;
2874
c5aff182
TP
2875 err = register_netdev(dev);
2876 if (err < 0) {
2877 dev_err(&pdev->dev, "failed to register\n");
2878 goto err_deinit;
2879 }
2880
8cc3e439
TP
2881 netdev_info(dev, "Using %s mac address %pM\n", mac_from,
2882 dev->dev_addr);
c5aff182
TP
2883
2884 platform_set_drvdata(pdev, pp->dev);
2885
2886 return 0;
2887
2888err_deinit:
2889 mvneta_deinit(pp);
74c41b04 2890err_free_stats:
2891 free_percpu(pp->stats);
c5aff182
TP
2892err_unmap:
2893 iounmap(pp->base);
5445eaf3
APR
2894err_clk:
2895 clk_disable_unprepare(pp->clk);
c5aff182
TP
2896err_free_irq:
2897 irq_dispose_mapping(dev->irq);
2898err_free_netdev:
2899 free_netdev(dev);
2900 return err;
2901}
2902
2903/* Device removal routine */
03ce758e 2904static int mvneta_remove(struct platform_device *pdev)
c5aff182
TP
2905{
2906 struct net_device *dev = platform_get_drvdata(pdev);
2907 struct mvneta_port *pp = netdev_priv(dev);
2908
2909 unregister_netdev(dev);
2910 mvneta_deinit(pp);
189dd626 2911 clk_disable_unprepare(pp->clk);
74c41b04 2912 free_percpu(pp->stats);
c5aff182
TP
2913 iounmap(pp->base);
2914 irq_dispose_mapping(dev->irq);
2915 free_netdev(dev);
2916
c5aff182
TP
2917 return 0;
2918}
2919
2920static const struct of_device_id mvneta_match[] = {
2921 { .compatible = "marvell,armada-370-neta" },
2922 { }
2923};
2924MODULE_DEVICE_TABLE(of, mvneta_match);
2925
2926static struct platform_driver mvneta_driver = {
2927 .probe = mvneta_probe,
03ce758e 2928 .remove = mvneta_remove,
c5aff182
TP
2929 .driver = {
2930 .name = MVNETA_DRIVER_NAME,
2931 .of_match_table = mvneta_match,
2932 },
2933};
2934
2935module_platform_driver(mvneta_driver);
2936
2937MODULE_DESCRIPTION("Marvell NETA Ethernet Driver - www.marvell.com");
2938MODULE_AUTHOR("Rami Rosen <rosenr@marvell.com>, Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
2939MODULE_LICENSE("GPL");
2940
2941module_param(rxq_number, int, S_IRUGO);
2942module_param(txq_number, int, S_IRUGO);
2943
2944module_param(rxq_def, int, S_IRUGO);
This page took 0.232789 seconds and 5 git commands to generate.