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