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