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