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