blob: df75a2302740bb60c21bb534301bfe754be6067b [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
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100531/* Checks whether the given RX descriptor is both the first and the
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300532 * last descriptor for the RX packet. Each RX packet is currently
533 * received through a single RX descriptor, so not having each RX
534 * descriptor with its first and last bits set is an error
535 */
536static int mvneta_rxq_desc_is_first_last(struct mvneta_rx_desc *desc)
537{
538 return (desc->status & MVNETA_RXD_FIRST_LAST_DESC) ==
539 MVNETA_RXD_FIRST_LAST_DESC;
540}
541
542/* Add number of descriptors ready to receive new packets */
543static void mvneta_rxq_non_occup_desc_add(struct mvneta_port *pp,
544 struct mvneta_rx_queue *rxq,
545 int ndescs)
546{
547 /* Only MVNETA_RXQ_ADD_NON_OCCUPIED_MAX (255) descriptors can
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);
614 return rxq->descs + rx_desc;
615}
616
617/* Change maximum receive size of the port. */
618static void mvneta_max_rx_size_set(struct mvneta_port *pp, int max_rx_size)
619{
620 u32 val;
621
622 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
623 val &= ~MVNETA_GMAC_MAX_RX_SIZE_MASK;
624 val |= ((max_rx_size - MVNETA_MH_SIZE) / 2) <<
625 MVNETA_GMAC_MAX_RX_SIZE_SHIFT;
626 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
627}
628
629
630/* Set rx queue offset */
631static void mvneta_rxq_offset_set(struct mvneta_port *pp,
632 struct mvneta_rx_queue *rxq,
633 int offset)
634{
635 u32 val;
636
637 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
638 val &= ~MVNETA_RXQ_PKT_OFFSET_ALL_MASK;
639
640 /* Offset is in */
641 val |= MVNETA_RXQ_PKT_OFFSET_MASK(offset >> 3);
642 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
643}
644
645
646/* Tx descriptors helper methods */
647
648/* Update HW with number of TX descriptors to be sent */
649static void mvneta_txq_pend_desc_add(struct mvneta_port *pp,
650 struct mvneta_tx_queue *txq,
651 int pend_desc)
652{
653 u32 val;
654
655 /* Only 255 descriptors can be added at once ; Assume caller
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100656 * process TX desriptors in quanta less than 256
657 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300658 val = pend_desc;
659 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
660}
661
662/* Get pointer to next TX descriptor to be processed (send) by HW */
663static struct mvneta_tx_desc *
664mvneta_txq_next_desc_get(struct mvneta_tx_queue *txq)
665{
666 int tx_desc = txq->next_desc_to_proc;
667
668 txq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(txq, tx_desc);
669 return txq->descs + tx_desc;
670}
671
672/* Release the last allocated TX descriptor. Useful to handle DMA
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100673 * mapping failures in the TX path.
674 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300675static void mvneta_txq_desc_put(struct mvneta_tx_queue *txq)
676{
677 if (txq->next_desc_to_proc == 0)
678 txq->next_desc_to_proc = txq->last_desc - 1;
679 else
680 txq->next_desc_to_proc--;
681}
682
683/* Set rxq buf size */
684static void mvneta_rxq_buf_size_set(struct mvneta_port *pp,
685 struct mvneta_rx_queue *rxq,
686 int buf_size)
687{
688 u32 val;
689
690 val = mvreg_read(pp, MVNETA_RXQ_SIZE_REG(rxq->id));
691
692 val &= ~MVNETA_RXQ_BUF_SIZE_MASK;
693 val |= ((buf_size >> 3) << MVNETA_RXQ_BUF_SIZE_SHIFT);
694
695 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), val);
696}
697
698/* Disable buffer management (BM) */
699static void mvneta_rxq_bm_disable(struct mvneta_port *pp,
700 struct mvneta_rx_queue *rxq)
701{
702 u32 val;
703
704 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
705 val &= ~MVNETA_RXQ_HW_BUF_ALLOC;
706 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
707}
708
709
710
711/* Sets the RGMII Enable bit (RGMIIEn) in port MAC control register */
Greg KH03ce7582012-12-21 13:42:15 +0000712static void mvneta_gmac_rgmii_set(struct mvneta_port *pp, int enable)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300713{
714 u32 val;
715
716 val = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
717
718 if (enable)
719 val |= MVNETA_GMAC2_PORT_RGMII;
720 else
721 val &= ~MVNETA_GMAC2_PORT_RGMII;
722
723 mvreg_write(pp, MVNETA_GMAC_CTRL_2, val);
724}
725
726/* Config SGMII port */
Greg KH03ce7582012-12-21 13:42:15 +0000727static void mvneta_port_sgmii_config(struct mvneta_port *pp)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300728{
729 u32 val;
730
731 val = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
732 val |= MVNETA_GMAC2_PSC_ENABLE;
733 mvreg_write(pp, MVNETA_GMAC_CTRL_2, val);
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +0200734
735 mvreg_write(pp, MVNETA_SGMII_SERDES_CFG, MVNETA_SGMII_SERDES_PROTO);
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300736}
737
738/* Start the Ethernet port RX and TX activity */
739static void mvneta_port_up(struct mvneta_port *pp)
740{
741 int queue;
742 u32 q_map;
743
744 /* Enable all initialized TXs. */
745 mvneta_mib_counters_clear(pp);
746 q_map = 0;
747 for (queue = 0; queue < txq_number; queue++) {
748 struct mvneta_tx_queue *txq = &pp->txqs[queue];
749 if (txq->descs != NULL)
750 q_map |= (1 << queue);
751 }
752 mvreg_write(pp, MVNETA_TXQ_CMD, q_map);
753
754 /* Enable all initialized RXQs. */
755 q_map = 0;
756 for (queue = 0; queue < rxq_number; queue++) {
757 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
758 if (rxq->descs != NULL)
759 q_map |= (1 << queue);
760 }
761
762 mvreg_write(pp, MVNETA_RXQ_CMD, q_map);
763}
764
765/* Stop the Ethernet port activity */
766static void mvneta_port_down(struct mvneta_port *pp)
767{
768 u32 val;
769 int count;
770
771 /* Stop Rx port activity. Check port Rx activity. */
772 val = mvreg_read(pp, MVNETA_RXQ_CMD) & MVNETA_RXQ_ENABLE_MASK;
773
774 /* Issue stop command for active channels only */
775 if (val != 0)
776 mvreg_write(pp, MVNETA_RXQ_CMD,
777 val << MVNETA_RXQ_DISABLE_SHIFT);
778
779 /* Wait for all Rx activity to terminate. */
780 count = 0;
781 do {
782 if (count++ >= MVNETA_RX_DISABLE_TIMEOUT_MSEC) {
783 netdev_warn(pp->dev,
784 "TIMEOUT for RX stopped ! rx_queue_cmd: 0x08%x\n",
785 val);
786 break;
787 }
788 mdelay(1);
789
790 val = mvreg_read(pp, MVNETA_RXQ_CMD);
791 } while (val & 0xff);
792
793 /* Stop Tx port activity. Check port Tx activity. Issue stop
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100794 * command for active channels only
795 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300796 val = (mvreg_read(pp, MVNETA_TXQ_CMD)) & MVNETA_TXQ_ENABLE_MASK;
797
798 if (val != 0)
799 mvreg_write(pp, MVNETA_TXQ_CMD,
800 (val << MVNETA_TXQ_DISABLE_SHIFT));
801
802 /* Wait for all Tx activity to terminate. */
803 count = 0;
804 do {
805 if (count++ >= MVNETA_TX_DISABLE_TIMEOUT_MSEC) {
806 netdev_warn(pp->dev,
807 "TIMEOUT for TX stopped status=0x%08x\n",
808 val);
809 break;
810 }
811 mdelay(1);
812
813 /* Check TX Command reg that all Txqs are stopped */
814 val = mvreg_read(pp, MVNETA_TXQ_CMD);
815
816 } while (val & 0xff);
817
818 /* Double check to verify that TX FIFO is empty */
819 count = 0;
820 do {
821 if (count++ >= MVNETA_TX_FIFO_EMPTY_TIMEOUT) {
822 netdev_warn(pp->dev,
823 "TX FIFO empty timeout status=0x08%x\n",
824 val);
825 break;
826 }
827 mdelay(1);
828
829 val = mvreg_read(pp, MVNETA_PORT_STATUS);
830 } while (!(val & MVNETA_TX_FIFO_EMPTY) &&
831 (val & MVNETA_TX_IN_PRGRS));
832
833 udelay(200);
834}
835
836/* Enable the port by setting the port enable bit of the MAC control register */
837static void mvneta_port_enable(struct mvneta_port *pp)
838{
839 u32 val;
840
841 /* Enable port */
842 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
843 val |= MVNETA_GMAC0_PORT_ENABLE;
844 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
845}
846
847/* Disable the port and wait for about 200 usec before retuning */
848static void mvneta_port_disable(struct mvneta_port *pp)
849{
850 u32 val;
851
852 /* Reset the Enable bit in the Serial Control Register */
853 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
854 val &= ~MVNETA_GMAC0_PORT_ENABLE;
855 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
856
857 udelay(200);
858}
859
860/* Multicast tables methods */
861
862/* Set all entries in Unicast MAC Table; queue==-1 means reject all */
863static void mvneta_set_ucast_table(struct mvneta_port *pp, int queue)
864{
865 int offset;
866 u32 val;
867
868 if (queue == -1) {
869 val = 0;
870 } else {
871 val = 0x1 | (queue << 1);
872 val |= (val << 24) | (val << 16) | (val << 8);
873 }
874
875 for (offset = 0; offset <= 0xc; offset += 4)
876 mvreg_write(pp, MVNETA_DA_FILT_UCAST_BASE + offset, val);
877}
878
879/* Set all entries in Special Multicast MAC Table; queue==-1 means reject all */
880static void mvneta_set_special_mcast_table(struct mvneta_port *pp, int queue)
881{
882 int offset;
883 u32 val;
884
885 if (queue == -1) {
886 val = 0;
887 } else {
888 val = 0x1 | (queue << 1);
889 val |= (val << 24) | (val << 16) | (val << 8);
890 }
891
892 for (offset = 0; offset <= 0xfc; offset += 4)
893 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + offset, val);
894
895}
896
897/* Set all entries in Other Multicast MAC Table. queue==-1 means reject all */
898static void mvneta_set_other_mcast_table(struct mvneta_port *pp, int queue)
899{
900 int offset;
901 u32 val;
902
903 if (queue == -1) {
904 memset(pp->mcast_count, 0, sizeof(pp->mcast_count));
905 val = 0;
906 } else {
907 memset(pp->mcast_count, 1, sizeof(pp->mcast_count));
908 val = 0x1 | (queue << 1);
909 val |= (val << 24) | (val << 16) | (val << 8);
910 }
911
912 for (offset = 0; offset <= 0xfc; offset += 4)
913 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + offset, val);
914}
915
916/* This method sets defaults to the NETA port:
917 * Clears interrupt Cause and Mask registers.
918 * Clears all MAC tables.
919 * Sets defaults to all registers.
920 * Resets RX and TX descriptor rings.
921 * Resets PHY.
922 * This method can be called after mvneta_port_down() to return the port
923 * settings to defaults.
924 */
925static void mvneta_defaults_set(struct mvneta_port *pp)
926{
927 int cpu;
928 int queue;
929 u32 val;
930
931 /* Clear all Cause registers */
932 mvreg_write(pp, MVNETA_INTR_NEW_CAUSE, 0);
933 mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0);
934 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
935
936 /* Mask all interrupts */
937 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
938 mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0);
939 mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0);
940 mvreg_write(pp, MVNETA_INTR_ENABLE, 0);
941
942 /* Enable MBUS Retry bit16 */
943 mvreg_write(pp, MVNETA_MBUS_RETRY, 0x20);
944
945 /* Set CPU queue access map - all CPUs have access to all RX
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100946 * queues and to all TX queues
947 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300948 for (cpu = 0; cpu < CONFIG_NR_CPUS; cpu++)
949 mvreg_write(pp, MVNETA_CPU_MAP(cpu),
950 (MVNETA_CPU_RXQ_ACCESS_ALL_MASK |
951 MVNETA_CPU_TXQ_ACCESS_ALL_MASK));
952
953 /* Reset RX and TX DMAs */
954 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
955 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
956
957 /* Disable Legacy WRR, Disable EJP, Release from reset */
958 mvreg_write(pp, MVNETA_TXQ_CMD_1, 0);
959 for (queue = 0; queue < txq_number; queue++) {
960 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(queue), 0);
961 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(queue), 0);
962 }
963
964 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
965 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
966
967 /* Set Port Acceleration Mode */
968 val = MVNETA_ACC_MODE_EXT;
969 mvreg_write(pp, MVNETA_ACC_MODE, val);
970
971 /* Update val of portCfg register accordingly with all RxQueue types */
972 val = MVNETA_PORT_CONFIG_DEFL_VALUE(rxq_def);
973 mvreg_write(pp, MVNETA_PORT_CONFIG, val);
974
975 val = 0;
976 mvreg_write(pp, MVNETA_PORT_CONFIG_EXTEND, val);
977 mvreg_write(pp, MVNETA_RX_MIN_FRAME_SIZE, 64);
978
979 /* Build PORT_SDMA_CONFIG_REG */
980 val = 0;
981
982 /* Default burst size */
983 val |= MVNETA_TX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
984 val |= MVNETA_RX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +0200985 val |= MVNETA_RX_NO_DATA_SWAP | MVNETA_TX_NO_DATA_SWAP;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300986
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +0200987#if defined(__BIG_ENDIAN)
988 val |= MVNETA_DESC_SWAP;
989#endif
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300990
991 /* Assign port SDMA configuration */
992 mvreg_write(pp, MVNETA_SDMA_CONFIG, val);
993
Thomas Petazzoni71408602013-09-04 16:21:18 +0200994 /* Disable PHY polling in hardware, since we're using the
995 * kernel phylib to do this.
996 */
997 val = mvreg_read(pp, MVNETA_UNIT_CONTROL);
998 val &= ~MVNETA_PHY_POLLING_ENABLE;
999 mvreg_write(pp, MVNETA_UNIT_CONTROL, val);
1000
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001001 mvneta_set_ucast_table(pp, -1);
1002 mvneta_set_special_mcast_table(pp, -1);
1003 mvneta_set_other_mcast_table(pp, -1);
1004
1005 /* Set port interrupt enable register - default enable all */
1006 mvreg_write(pp, MVNETA_INTR_ENABLE,
1007 (MVNETA_RXQ_INTR_ENABLE_ALL_MASK
1008 | MVNETA_TXQ_INTR_ENABLE_ALL_MASK));
1009}
1010
1011/* Set max sizes for tx queues */
1012static void mvneta_txq_max_tx_size_set(struct mvneta_port *pp, int max_tx_size)
1013
1014{
1015 u32 val, size, mtu;
1016 int queue;
1017
1018 mtu = max_tx_size * 8;
1019 if (mtu > MVNETA_TX_MTU_MAX)
1020 mtu = MVNETA_TX_MTU_MAX;
1021
1022 /* Set MTU */
1023 val = mvreg_read(pp, MVNETA_TX_MTU);
1024 val &= ~MVNETA_TX_MTU_MAX;
1025 val |= mtu;
1026 mvreg_write(pp, MVNETA_TX_MTU, val);
1027
1028 /* TX token size and all TXQs token size must be larger that MTU */
1029 val = mvreg_read(pp, MVNETA_TX_TOKEN_SIZE);
1030
1031 size = val & MVNETA_TX_TOKEN_SIZE_MAX;
1032 if (size < mtu) {
1033 size = mtu;
1034 val &= ~MVNETA_TX_TOKEN_SIZE_MAX;
1035 val |= size;
1036 mvreg_write(pp, MVNETA_TX_TOKEN_SIZE, val);
1037 }
1038 for (queue = 0; queue < txq_number; queue++) {
1039 val = mvreg_read(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue));
1040
1041 size = val & MVNETA_TXQ_TOKEN_SIZE_MAX;
1042 if (size < mtu) {
1043 size = mtu;
1044 val &= ~MVNETA_TXQ_TOKEN_SIZE_MAX;
1045 val |= size;
1046 mvreg_write(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue), val);
1047 }
1048 }
1049}
1050
1051/* Set unicast address */
1052static void mvneta_set_ucast_addr(struct mvneta_port *pp, u8 last_nibble,
1053 int queue)
1054{
1055 unsigned int unicast_reg;
1056 unsigned int tbl_offset;
1057 unsigned int reg_offset;
1058
1059 /* Locate the Unicast table entry */
1060 last_nibble = (0xf & last_nibble);
1061
1062 /* offset from unicast tbl base */
1063 tbl_offset = (last_nibble / 4) * 4;
1064
1065 /* offset within the above reg */
1066 reg_offset = last_nibble % 4;
1067
1068 unicast_reg = mvreg_read(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset));
1069
1070 if (queue == -1) {
1071 /* Clear accepts frame bit at specified unicast DA tbl entry */
1072 unicast_reg &= ~(0xff << (8 * reg_offset));
1073 } else {
1074 unicast_reg &= ~(0xff << (8 * reg_offset));
1075 unicast_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
1076 }
1077
1078 mvreg_write(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset), unicast_reg);
1079}
1080
1081/* Set mac address */
1082static void mvneta_mac_addr_set(struct mvneta_port *pp, unsigned char *addr,
1083 int queue)
1084{
1085 unsigned int mac_h;
1086 unsigned int mac_l;
1087
1088 if (queue != -1) {
1089 mac_l = (addr[4] << 8) | (addr[5]);
1090 mac_h = (addr[0] << 24) | (addr[1] << 16) |
1091 (addr[2] << 8) | (addr[3] << 0);
1092
1093 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, mac_l);
1094 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, mac_h);
1095 }
1096
1097 /* Accept frames of this address */
1098 mvneta_set_ucast_addr(pp, addr[5], queue);
1099}
1100
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001101/* Set the number of packets that will be received before RX interrupt
1102 * will be generated by HW.
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001103 */
1104static void mvneta_rx_pkts_coal_set(struct mvneta_port *pp,
1105 struct mvneta_rx_queue *rxq, u32 value)
1106{
1107 mvreg_write(pp, MVNETA_RXQ_THRESHOLD_REG(rxq->id),
1108 value | MVNETA_RXQ_NON_OCCUPIED(0));
1109 rxq->pkts_coal = value;
1110}
1111
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001112/* Set the time delay in usec before RX interrupt will be generated by
1113 * HW.
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001114 */
1115static void mvneta_rx_time_coal_set(struct mvneta_port *pp,
1116 struct mvneta_rx_queue *rxq, u32 value)
1117{
Thomas Petazzoni189dd622012-11-19 14:15:25 +01001118 u32 val;
1119 unsigned long clk_rate;
1120
1121 clk_rate = clk_get_rate(pp->clk);
1122 val = (clk_rate / 1000000) * value;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001123
1124 mvreg_write(pp, MVNETA_RXQ_TIME_COAL_REG(rxq->id), val);
1125 rxq->time_coal = value;
1126}
1127
1128/* Set threshold for TX_DONE pkts coalescing */
1129static void mvneta_tx_done_pkts_coal_set(struct mvneta_port *pp,
1130 struct mvneta_tx_queue *txq, u32 value)
1131{
1132 u32 val;
1133
1134 val = mvreg_read(pp, MVNETA_TXQ_SIZE_REG(txq->id));
1135
1136 val &= ~MVNETA_TXQ_SENT_THRESH_ALL_MASK;
1137 val |= MVNETA_TXQ_SENT_THRESH_MASK(value);
1138
1139 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), val);
1140
1141 txq->done_pkts_coal = value;
1142}
1143
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001144/* Handle rx descriptor fill by setting buf_cookie and buf_phys_addr */
1145static void mvneta_rx_desc_fill(struct mvneta_rx_desc *rx_desc,
1146 u32 phys_addr, u32 cookie)
1147{
1148 rx_desc->buf_cookie = cookie;
1149 rx_desc->buf_phys_addr = phys_addr;
1150}
1151
1152/* Decrement sent descriptors counter */
1153static void mvneta_txq_sent_desc_dec(struct mvneta_port *pp,
1154 struct mvneta_tx_queue *txq,
1155 int sent_desc)
1156{
1157 u32 val;
1158
1159 /* Only 255 TX descriptors can be updated at once */
1160 while (sent_desc > 0xff) {
1161 val = 0xff << MVNETA_TXQ_DEC_SENT_SHIFT;
1162 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1163 sent_desc = sent_desc - 0xff;
1164 }
1165
1166 val = sent_desc << MVNETA_TXQ_DEC_SENT_SHIFT;
1167 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1168}
1169
1170/* Get number of TX descriptors already sent by HW */
1171static int mvneta_txq_sent_desc_num_get(struct mvneta_port *pp,
1172 struct mvneta_tx_queue *txq)
1173{
1174 u32 val;
1175 int sent_desc;
1176
1177 val = mvreg_read(pp, MVNETA_TXQ_STATUS_REG(txq->id));
1178 sent_desc = (val & MVNETA_TXQ_SENT_DESC_MASK) >>
1179 MVNETA_TXQ_SENT_DESC_SHIFT;
1180
1181 return sent_desc;
1182}
1183
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001184/* Get number of sent descriptors and decrement counter.
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001185 * The number of sent descriptors is returned.
1186 */
1187static int mvneta_txq_sent_desc_proc(struct mvneta_port *pp,
1188 struct mvneta_tx_queue *txq)
1189{
1190 int sent_desc;
1191
1192 /* Get number of sent descriptors */
1193 sent_desc = mvneta_txq_sent_desc_num_get(pp, txq);
1194
1195 /* Decrement sent descriptors counter */
1196 if (sent_desc)
1197 mvneta_txq_sent_desc_dec(pp, txq, sent_desc);
1198
1199 return sent_desc;
1200}
1201
1202/* Set TXQ descriptors fields relevant for CSUM calculation */
1203static u32 mvneta_txq_desc_csum(int l3_offs, int l3_proto,
1204 int ip_hdr_len, int l4_proto)
1205{
1206 u32 command;
1207
1208 /* Fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001209 * G_L4_chk, L4_type; required only for checksum
1210 * calculation
1211 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001212 command = l3_offs << MVNETA_TX_L3_OFF_SHIFT;
1213 command |= ip_hdr_len << MVNETA_TX_IP_HLEN_SHIFT;
1214
1215 if (l3_proto == swab16(ETH_P_IP))
1216 command |= MVNETA_TXD_IP_CSUM;
1217 else
1218 command |= MVNETA_TX_L3_IP6;
1219
1220 if (l4_proto == IPPROTO_TCP)
1221 command |= MVNETA_TX_L4_CSUM_FULL;
1222 else if (l4_proto == IPPROTO_UDP)
1223 command |= MVNETA_TX_L4_UDP | MVNETA_TX_L4_CSUM_FULL;
1224 else
1225 command |= MVNETA_TX_L4_CSUM_NOT;
1226
1227 return command;
1228}
1229
1230
1231/* Display more error info */
1232static void mvneta_rx_error(struct mvneta_port *pp,
1233 struct mvneta_rx_desc *rx_desc)
1234{
1235 u32 status = rx_desc->status;
1236
1237 if (!mvneta_rxq_desc_is_first_last(rx_desc)) {
1238 netdev_err(pp->dev,
1239 "bad rx status %08x (buffer oversize), size=%d\n",
1240 rx_desc->status, rx_desc->data_size);
1241 return;
1242 }
1243
1244 switch (status & MVNETA_RXD_ERR_CODE_MASK) {
1245 case MVNETA_RXD_ERR_CRC:
1246 netdev_err(pp->dev, "bad rx status %08x (crc error), size=%d\n",
1247 status, rx_desc->data_size);
1248 break;
1249 case MVNETA_RXD_ERR_OVERRUN:
1250 netdev_err(pp->dev, "bad rx status %08x (overrun error), size=%d\n",
1251 status, rx_desc->data_size);
1252 break;
1253 case MVNETA_RXD_ERR_LEN:
1254 netdev_err(pp->dev, "bad rx status %08x (max frame length error), size=%d\n",
1255 status, rx_desc->data_size);
1256 break;
1257 case MVNETA_RXD_ERR_RESOURCE:
1258 netdev_err(pp->dev, "bad rx status %08x (resource error), size=%d\n",
1259 status, rx_desc->data_size);
1260 break;
1261 }
1262}
1263
1264/* Handle RX checksum offload */
1265static void mvneta_rx_csum(struct mvneta_port *pp,
1266 struct mvneta_rx_desc *rx_desc,
1267 struct sk_buff *skb)
1268{
1269 if ((rx_desc->status & MVNETA_RXD_L3_IP4) &&
1270 (rx_desc->status & MVNETA_RXD_L4_CSUM_OK)) {
1271 skb->csum = 0;
1272 skb->ip_summed = CHECKSUM_UNNECESSARY;
1273 return;
1274 }
1275
1276 skb->ip_summed = CHECKSUM_NONE;
1277}
1278
1279/* Return tx queue pointer (find last set bit) according to causeTxDone reg */
1280static struct mvneta_tx_queue *mvneta_tx_done_policy(struct mvneta_port *pp,
1281 u32 cause)
1282{
1283 int queue = fls(cause) - 1;
1284
1285 return (queue < 0 || queue >= txq_number) ? NULL : &pp->txqs[queue];
1286}
1287
1288/* Free tx queue skbuffs */
1289static void mvneta_txq_bufs_free(struct mvneta_port *pp,
1290 struct mvneta_tx_queue *txq, int num)
1291{
1292 int i;
1293
1294 for (i = 0; i < num; i++) {
1295 struct mvneta_tx_desc *tx_desc = txq->descs +
1296 txq->txq_get_index;
1297 struct sk_buff *skb = txq->tx_skb[txq->txq_get_index];
1298
1299 mvneta_txq_inc_get(txq);
1300
1301 if (!skb)
1302 continue;
1303
1304 dma_unmap_single(pp->dev->dev.parent, tx_desc->buf_phys_addr,
1305 tx_desc->data_size, DMA_TO_DEVICE);
1306 dev_kfree_skb_any(skb);
1307 }
1308}
1309
1310/* Handle end of transmission */
1311static int mvneta_txq_done(struct mvneta_port *pp,
1312 struct mvneta_tx_queue *txq)
1313{
1314 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
1315 int tx_done;
1316
1317 tx_done = mvneta_txq_sent_desc_proc(pp, txq);
1318 if (tx_done == 0)
1319 return tx_done;
1320 mvneta_txq_bufs_free(pp, txq, tx_done);
1321
1322 txq->count -= tx_done;
1323
1324 if (netif_tx_queue_stopped(nq)) {
1325 if (txq->size - txq->count >= MAX_SKB_FRAGS + 1)
1326 netif_tx_wake_queue(nq);
1327 }
1328
1329 return tx_done;
1330}
1331
1332/* Refill processing */
1333static int mvneta_rx_refill(struct mvneta_port *pp,
1334 struct mvneta_rx_desc *rx_desc)
1335
1336{
1337 dma_addr_t phys_addr;
1338 struct sk_buff *skb;
1339
1340 skb = netdev_alloc_skb(pp->dev, pp->pkt_size);
1341 if (!skb)
1342 return -ENOMEM;
1343
1344 phys_addr = dma_map_single(pp->dev->dev.parent, skb->head,
1345 MVNETA_RX_BUF_SIZE(pp->pkt_size),
1346 DMA_FROM_DEVICE);
1347 if (unlikely(dma_mapping_error(pp->dev->dev.parent, phys_addr))) {
1348 dev_kfree_skb(skb);
1349 return -ENOMEM;
1350 }
1351
1352 mvneta_rx_desc_fill(rx_desc, phys_addr, (u32)skb);
1353
1354 return 0;
1355}
1356
1357/* Handle tx checksum */
1358static u32 mvneta_skb_tx_csum(struct mvneta_port *pp, struct sk_buff *skb)
1359{
1360 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1361 int ip_hdr_len = 0;
1362 u8 l4_proto;
1363
1364 if (skb->protocol == htons(ETH_P_IP)) {
1365 struct iphdr *ip4h = ip_hdr(skb);
1366
1367 /* Calculate IPv4 checksum and L4 checksum */
1368 ip_hdr_len = ip4h->ihl;
1369 l4_proto = ip4h->protocol;
1370 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1371 struct ipv6hdr *ip6h = ipv6_hdr(skb);
1372
1373 /* Read l4_protocol from one of IPv6 extra headers */
1374 if (skb_network_header_len(skb) > 0)
1375 ip_hdr_len = (skb_network_header_len(skb) >> 2);
1376 l4_proto = ip6h->nexthdr;
1377 } else
1378 return MVNETA_TX_L4_CSUM_NOT;
1379
1380 return mvneta_txq_desc_csum(skb_network_offset(skb),
1381 skb->protocol, ip_hdr_len, l4_proto);
1382 }
1383
1384 return MVNETA_TX_L4_CSUM_NOT;
1385}
1386
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001387/* Returns rx queue pointer (find last set bit) according to causeRxTx
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001388 * value
1389 */
1390static struct mvneta_rx_queue *mvneta_rx_policy(struct mvneta_port *pp,
1391 u32 cause)
1392{
1393 int queue = fls(cause >> 8) - 1;
1394
1395 return (queue < 0 || queue >= rxq_number) ? NULL : &pp->rxqs[queue];
1396}
1397
1398/* Drop packets received by the RXQ and free buffers */
1399static void mvneta_rxq_drop_pkts(struct mvneta_port *pp,
1400 struct mvneta_rx_queue *rxq)
1401{
1402 int rx_done, i;
1403
1404 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
1405 for (i = 0; i < rxq->size; i++) {
1406 struct mvneta_rx_desc *rx_desc = rxq->descs + i;
1407 struct sk_buff *skb = (struct sk_buff *)rx_desc->buf_cookie;
1408
1409 dev_kfree_skb_any(skb);
1410 dma_unmap_single(pp->dev->dev.parent, rx_desc->buf_phys_addr,
Ezequiel Garciaa328f3a2013-12-05 13:35:37 -03001411 MVNETA_RX_BUF_SIZE(pp->pkt_size), DMA_FROM_DEVICE);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001412 }
1413
1414 if (rx_done)
1415 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
1416}
1417
1418/* Main rx processing */
1419static int mvneta_rx(struct mvneta_port *pp, int rx_todo,
1420 struct mvneta_rx_queue *rxq)
1421{
1422 struct net_device *dev = pp->dev;
1423 int rx_done, rx_filled;
willy tarreaudc4277d2014-01-16 08:20:07 +01001424 u32 rcvd_pkts = 0;
1425 u32 rcvd_bytes = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001426
1427 /* Get number of received packets */
1428 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
1429
1430 if (rx_todo > rx_done)
1431 rx_todo = rx_done;
1432
1433 rx_done = 0;
1434 rx_filled = 0;
1435
1436 /* Fairness NAPI loop */
1437 while (rx_done < rx_todo) {
1438 struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
1439 struct sk_buff *skb;
1440 u32 rx_status;
1441 int rx_bytes, err;
1442
1443 prefetch(rx_desc);
1444 rx_done++;
1445 rx_filled++;
1446 rx_status = rx_desc->status;
1447 skb = (struct sk_buff *)rx_desc->buf_cookie;
1448
1449 if (!mvneta_rxq_desc_is_first_last(rx_desc) ||
1450 (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
1451 dev->stats.rx_errors++;
1452 mvneta_rx_error(pp, rx_desc);
1453 mvneta_rx_desc_fill(rx_desc, rx_desc->buf_phys_addr,
1454 (u32)skb);
1455 continue;
1456 }
1457
1458 dma_unmap_single(pp->dev->dev.parent, rx_desc->buf_phys_addr,
Ezequiel Garciaa328f3a2013-12-05 13:35:37 -03001459 MVNETA_RX_BUF_SIZE(pp->pkt_size), DMA_FROM_DEVICE);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001460
1461 rx_bytes = rx_desc->data_size -
1462 (ETH_FCS_LEN + MVNETA_MH_SIZE);
willy tarreaudc4277d2014-01-16 08:20:07 +01001463 rcvd_pkts++;
1464 rcvd_bytes += rx_bytes;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001465
1466 /* Linux processing */
1467 skb_reserve(skb, MVNETA_MH_SIZE);
1468 skb_put(skb, rx_bytes);
1469
1470 skb->protocol = eth_type_trans(skb, dev);
1471
1472 mvneta_rx_csum(pp, rx_desc, skb);
1473
1474 napi_gro_receive(&pp->napi, skb);
1475
1476 /* Refill processing */
1477 err = mvneta_rx_refill(pp, rx_desc);
1478 if (err) {
1479 netdev_err(pp->dev, "Linux processing - Can't refill\n");
1480 rxq->missed++;
1481 rx_filled--;
1482 }
1483 }
1484
willy tarreaudc4277d2014-01-16 08:20:07 +01001485 if (rcvd_pkts) {
willy tarreau74c41b02014-01-16 08:20:08 +01001486 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
1487
1488 u64_stats_update_begin(&stats->syncp);
1489 stats->rx_packets += rcvd_pkts;
1490 stats->rx_bytes += rcvd_bytes;
1491 u64_stats_update_end(&stats->syncp);
willy tarreaudc4277d2014-01-16 08:20:07 +01001492 }
1493
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001494 /* Update rxq management counters */
1495 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_filled);
1496
1497 return rx_done;
1498}
1499
1500/* Handle tx fragmentation processing */
1501static int mvneta_tx_frag_process(struct mvneta_port *pp, struct sk_buff *skb,
1502 struct mvneta_tx_queue *txq)
1503{
1504 struct mvneta_tx_desc *tx_desc;
1505 int i;
1506
1507 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1508 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1509 void *addr = page_address(frag->page.p) + frag->page_offset;
1510
1511 tx_desc = mvneta_txq_next_desc_get(txq);
1512 tx_desc->data_size = frag->size;
1513
1514 tx_desc->buf_phys_addr =
1515 dma_map_single(pp->dev->dev.parent, addr,
1516 tx_desc->data_size, DMA_TO_DEVICE);
1517
1518 if (dma_mapping_error(pp->dev->dev.parent,
1519 tx_desc->buf_phys_addr)) {
1520 mvneta_txq_desc_put(txq);
1521 goto error;
1522 }
1523
1524 if (i == (skb_shinfo(skb)->nr_frags - 1)) {
1525 /* Last descriptor */
1526 tx_desc->command = MVNETA_TXD_L_DESC | MVNETA_TXD_Z_PAD;
1527
1528 txq->tx_skb[txq->txq_put_index] = skb;
1529
1530 mvneta_txq_inc_put(txq);
1531 } else {
1532 /* Descriptor in the middle: Not First, Not Last */
1533 tx_desc->command = 0;
1534
1535 txq->tx_skb[txq->txq_put_index] = NULL;
1536 mvneta_txq_inc_put(txq);
1537 }
1538 }
1539
1540 return 0;
1541
1542error:
1543 /* Release all descriptors that were used to map fragments of
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001544 * this packet, as well as the corresponding DMA mappings
1545 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001546 for (i = i - 1; i >= 0; i--) {
1547 tx_desc = txq->descs + i;
1548 dma_unmap_single(pp->dev->dev.parent,
1549 tx_desc->buf_phys_addr,
1550 tx_desc->data_size,
1551 DMA_TO_DEVICE);
1552 mvneta_txq_desc_put(txq);
1553 }
1554
1555 return -ENOMEM;
1556}
1557
1558/* Main tx processing */
1559static int mvneta_tx(struct sk_buff *skb, struct net_device *dev)
1560{
1561 struct mvneta_port *pp = netdev_priv(dev);
Willy Tarreauee40a112013-04-11 23:00:37 +02001562 u16 txq_id = skb_get_queue_mapping(skb);
1563 struct mvneta_tx_queue *txq = &pp->txqs[txq_id];
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001564 struct mvneta_tx_desc *tx_desc;
1565 struct netdev_queue *nq;
1566 int frags = 0;
1567 u32 tx_cmd;
1568
1569 if (!netif_running(dev))
1570 goto out;
1571
1572 frags = skb_shinfo(skb)->nr_frags + 1;
Willy Tarreauee40a112013-04-11 23:00:37 +02001573 nq = netdev_get_tx_queue(dev, txq_id);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001574
1575 /* Get a descriptor for the first part of the packet */
1576 tx_desc = mvneta_txq_next_desc_get(txq);
1577
1578 tx_cmd = mvneta_skb_tx_csum(pp, skb);
1579
1580 tx_desc->data_size = skb_headlen(skb);
1581
1582 tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, skb->data,
1583 tx_desc->data_size,
1584 DMA_TO_DEVICE);
1585 if (unlikely(dma_mapping_error(dev->dev.parent,
1586 tx_desc->buf_phys_addr))) {
1587 mvneta_txq_desc_put(txq);
1588 frags = 0;
1589 goto out;
1590 }
1591
1592 if (frags == 1) {
1593 /* First and Last descriptor */
1594 tx_cmd |= MVNETA_TXD_FLZ_DESC;
1595 tx_desc->command = tx_cmd;
1596 txq->tx_skb[txq->txq_put_index] = skb;
1597 mvneta_txq_inc_put(txq);
1598 } else {
1599 /* First but not Last */
1600 tx_cmd |= MVNETA_TXD_F_DESC;
1601 txq->tx_skb[txq->txq_put_index] = NULL;
1602 mvneta_txq_inc_put(txq);
1603 tx_desc->command = tx_cmd;
1604 /* Continue with other skb fragments */
1605 if (mvneta_tx_frag_process(pp, skb, txq)) {
1606 dma_unmap_single(dev->dev.parent,
1607 tx_desc->buf_phys_addr,
1608 tx_desc->data_size,
1609 DMA_TO_DEVICE);
1610 mvneta_txq_desc_put(txq);
1611 frags = 0;
1612 goto out;
1613 }
1614 }
1615
1616 txq->count += frags;
1617 mvneta_txq_pend_desc_add(pp, txq, frags);
1618
1619 if (txq->size - txq->count < MAX_SKB_FRAGS + 1)
1620 netif_tx_stop_queue(nq);
1621
1622out:
1623 if (frags > 0) {
willy tarreau74c41b02014-01-16 08:20:08 +01001624 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001625
willy tarreau74c41b02014-01-16 08:20:08 +01001626 u64_stats_update_begin(&stats->syncp);
1627 stats->tx_packets++;
1628 stats->tx_bytes += skb->len;
1629 u64_stats_update_end(&stats->syncp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001630 } else {
1631 dev->stats.tx_dropped++;
1632 dev_kfree_skb_any(skb);
1633 }
1634
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001635 return NETDEV_TX_OK;
1636}
1637
1638
1639/* Free tx resources, when resetting a port */
1640static void mvneta_txq_done_force(struct mvneta_port *pp,
1641 struct mvneta_tx_queue *txq)
1642
1643{
1644 int tx_done = txq->count;
1645
1646 mvneta_txq_bufs_free(pp, txq, tx_done);
1647
1648 /* reset txq */
1649 txq->count = 0;
1650 txq->txq_put_index = 0;
1651 txq->txq_get_index = 0;
1652}
1653
1654/* handle tx done - called from tx done timer callback */
1655static u32 mvneta_tx_done_gbe(struct mvneta_port *pp, u32 cause_tx_done,
1656 int *tx_todo)
1657{
1658 struct mvneta_tx_queue *txq;
1659 u32 tx_done = 0;
1660 struct netdev_queue *nq;
1661
1662 *tx_todo = 0;
1663 while (cause_tx_done != 0) {
1664 txq = mvneta_tx_done_policy(pp, cause_tx_done);
1665 if (!txq)
1666 break;
1667
1668 nq = netdev_get_tx_queue(pp->dev, txq->id);
1669 __netif_tx_lock(nq, smp_processor_id());
1670
1671 if (txq->count) {
1672 tx_done += mvneta_txq_done(pp, txq);
1673 *tx_todo += txq->count;
1674 }
1675
1676 __netif_tx_unlock(nq);
1677 cause_tx_done &= ~((1 << txq->id));
1678 }
1679
1680 return tx_done;
1681}
1682
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001683/* Compute crc8 of the specified address, using a unique algorithm ,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001684 * according to hw spec, different than generic crc8 algorithm
1685 */
1686static int mvneta_addr_crc(unsigned char *addr)
1687{
1688 int crc = 0;
1689 int i;
1690
1691 for (i = 0; i < ETH_ALEN; i++) {
1692 int j;
1693
1694 crc = (crc ^ addr[i]) << 8;
1695 for (j = 7; j >= 0; j--) {
1696 if (crc & (0x100 << j))
1697 crc ^= 0x107 << j;
1698 }
1699 }
1700
1701 return crc;
1702}
1703
1704/* This method controls the net device special MAC multicast support.
1705 * The Special Multicast Table for MAC addresses supports MAC of the form
1706 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
1707 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
1708 * Table entries in the DA-Filter table. This method set the Special
1709 * Multicast Table appropriate entry.
1710 */
1711static void mvneta_set_special_mcast_addr(struct mvneta_port *pp,
1712 unsigned char last_byte,
1713 int queue)
1714{
1715 unsigned int smc_table_reg;
1716 unsigned int tbl_offset;
1717 unsigned int reg_offset;
1718
1719 /* Register offset from SMC table base */
1720 tbl_offset = (last_byte / 4);
1721 /* Entry offset within the above reg */
1722 reg_offset = last_byte % 4;
1723
1724 smc_table_reg = mvreg_read(pp, (MVNETA_DA_FILT_SPEC_MCAST
1725 + tbl_offset * 4));
1726
1727 if (queue == -1)
1728 smc_table_reg &= ~(0xff << (8 * reg_offset));
1729 else {
1730 smc_table_reg &= ~(0xff << (8 * reg_offset));
1731 smc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
1732 }
1733
1734 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + tbl_offset * 4,
1735 smc_table_reg);
1736}
1737
1738/* This method controls the network device Other MAC multicast support.
1739 * The Other Multicast Table is used for multicast of another type.
1740 * A CRC-8 is used as an index to the Other Multicast Table entries
1741 * in the DA-Filter table.
1742 * The method gets the CRC-8 value from the calling routine and
1743 * sets the Other Multicast Table appropriate entry according to the
1744 * specified CRC-8 .
1745 */
1746static void mvneta_set_other_mcast_addr(struct mvneta_port *pp,
1747 unsigned char crc8,
1748 int queue)
1749{
1750 unsigned int omc_table_reg;
1751 unsigned int tbl_offset;
1752 unsigned int reg_offset;
1753
1754 tbl_offset = (crc8 / 4) * 4; /* Register offset from OMC table base */
1755 reg_offset = crc8 % 4; /* Entry offset within the above reg */
1756
1757 omc_table_reg = mvreg_read(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset);
1758
1759 if (queue == -1) {
1760 /* Clear accepts frame bit at specified Other DA table entry */
1761 omc_table_reg &= ~(0xff << (8 * reg_offset));
1762 } else {
1763 omc_table_reg &= ~(0xff << (8 * reg_offset));
1764 omc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
1765 }
1766
1767 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset, omc_table_reg);
1768}
1769
1770/* The network device supports multicast using two tables:
1771 * 1) Special Multicast Table for MAC addresses of the form
1772 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
1773 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
1774 * Table entries in the DA-Filter table.
1775 * 2) Other Multicast Table for multicast of another type. A CRC-8 value
1776 * is used as an index to the Other Multicast Table entries in the
1777 * DA-Filter table.
1778 */
1779static int mvneta_mcast_addr_set(struct mvneta_port *pp, unsigned char *p_addr,
1780 int queue)
1781{
1782 unsigned char crc_result = 0;
1783
1784 if (memcmp(p_addr, "\x01\x00\x5e\x00\x00", 5) == 0) {
1785 mvneta_set_special_mcast_addr(pp, p_addr[5], queue);
1786 return 0;
1787 }
1788
1789 crc_result = mvneta_addr_crc(p_addr);
1790 if (queue == -1) {
1791 if (pp->mcast_count[crc_result] == 0) {
1792 netdev_info(pp->dev, "No valid Mcast for crc8=0x%02x\n",
1793 crc_result);
1794 return -EINVAL;
1795 }
1796
1797 pp->mcast_count[crc_result]--;
1798 if (pp->mcast_count[crc_result] != 0) {
1799 netdev_info(pp->dev,
1800 "After delete there are %d valid Mcast for crc8=0x%02x\n",
1801 pp->mcast_count[crc_result], crc_result);
1802 return -EINVAL;
1803 }
1804 } else
1805 pp->mcast_count[crc_result]++;
1806
1807 mvneta_set_other_mcast_addr(pp, crc_result, queue);
1808
1809 return 0;
1810}
1811
1812/* Configure Fitering mode of Ethernet port */
1813static void mvneta_rx_unicast_promisc_set(struct mvneta_port *pp,
1814 int is_promisc)
1815{
1816 u32 port_cfg_reg, val;
1817
1818 port_cfg_reg = mvreg_read(pp, MVNETA_PORT_CONFIG);
1819
1820 val = mvreg_read(pp, MVNETA_TYPE_PRIO);
1821
1822 /* Set / Clear UPM bit in port configuration register */
1823 if (is_promisc) {
1824 /* Accept all Unicast addresses */
1825 port_cfg_reg |= MVNETA_UNI_PROMISC_MODE;
1826 val |= MVNETA_FORCE_UNI;
1827 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, 0xffff);
1828 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, 0xffffffff);
1829 } else {
1830 /* Reject all Unicast addresses */
1831 port_cfg_reg &= ~MVNETA_UNI_PROMISC_MODE;
1832 val &= ~MVNETA_FORCE_UNI;
1833 }
1834
1835 mvreg_write(pp, MVNETA_PORT_CONFIG, port_cfg_reg);
1836 mvreg_write(pp, MVNETA_TYPE_PRIO, val);
1837}
1838
1839/* register unicast and multicast addresses */
1840static void mvneta_set_rx_mode(struct net_device *dev)
1841{
1842 struct mvneta_port *pp = netdev_priv(dev);
1843 struct netdev_hw_addr *ha;
1844
1845 if (dev->flags & IFF_PROMISC) {
1846 /* Accept all: Multicast + Unicast */
1847 mvneta_rx_unicast_promisc_set(pp, 1);
1848 mvneta_set_ucast_table(pp, rxq_def);
1849 mvneta_set_special_mcast_table(pp, rxq_def);
1850 mvneta_set_other_mcast_table(pp, rxq_def);
1851 } else {
1852 /* Accept single Unicast */
1853 mvneta_rx_unicast_promisc_set(pp, 0);
1854 mvneta_set_ucast_table(pp, -1);
1855 mvneta_mac_addr_set(pp, dev->dev_addr, rxq_def);
1856
1857 if (dev->flags & IFF_ALLMULTI) {
1858 /* Accept all multicast */
1859 mvneta_set_special_mcast_table(pp, rxq_def);
1860 mvneta_set_other_mcast_table(pp, rxq_def);
1861 } else {
1862 /* Accept only initialized multicast */
1863 mvneta_set_special_mcast_table(pp, -1);
1864 mvneta_set_other_mcast_table(pp, -1);
1865
1866 if (!netdev_mc_empty(dev)) {
1867 netdev_for_each_mc_addr(ha, dev) {
1868 mvneta_mcast_addr_set(pp, ha->addr,
1869 rxq_def);
1870 }
1871 }
1872 }
1873 }
1874}
1875
1876/* Interrupt handling - the callback for request_irq() */
1877static irqreturn_t mvneta_isr(int irq, void *dev_id)
1878{
1879 struct mvneta_port *pp = (struct mvneta_port *)dev_id;
1880
1881 /* Mask all interrupts */
1882 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
1883
1884 napi_schedule(&pp->napi);
1885
1886 return IRQ_HANDLED;
1887}
1888
1889/* NAPI handler
1890 * Bits 0 - 7 of the causeRxTx register indicate that are transmitted
1891 * packets on the corresponding TXQ (Bit 0 is for TX queue 1).
1892 * Bits 8 -15 of the cause Rx Tx register indicate that are received
1893 * packets on the corresponding RXQ (Bit 8 is for RX queue 0).
1894 * Each CPU has its own causeRxTx register
1895 */
1896static int mvneta_poll(struct napi_struct *napi, int budget)
1897{
1898 int rx_done = 0;
1899 u32 cause_rx_tx;
1900 unsigned long flags;
1901 struct mvneta_port *pp = netdev_priv(napi->dev);
1902
1903 if (!netif_running(pp->dev)) {
1904 napi_complete(napi);
1905 return rx_done;
1906 }
1907
1908 /* Read cause register */
1909 cause_rx_tx = mvreg_read(pp, MVNETA_INTR_NEW_CAUSE) &
willy tarreau71f6d1b2014-01-16 08:20:11 +01001910 (MVNETA_RX_INTR_MASK(rxq_number) | MVNETA_TX_INTR_MASK(txq_number));
1911
1912 /* Release Tx descriptors */
1913 if (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL) {
1914 int tx_todo = 0;
1915
1916 mvneta_tx_done_gbe(pp, (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL), &tx_todo);
1917 cause_rx_tx &= ~MVNETA_TX_INTR_MASK_ALL;
1918 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001919
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001920 /* For the case where the last mvneta_poll did not process all
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001921 * RX packets
1922 */
1923 cause_rx_tx |= pp->cause_rx_tx;
1924 if (rxq_number > 1) {
willy tarreau71f6d1b2014-01-16 08:20:11 +01001925 while ((cause_rx_tx & MVNETA_RX_INTR_MASK_ALL) && (budget > 0)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001926 int count;
1927 struct mvneta_rx_queue *rxq;
1928 /* get rx queue number from cause_rx_tx */
1929 rxq = mvneta_rx_policy(pp, cause_rx_tx);
1930 if (!rxq)
1931 break;
1932
1933 /* process the packet in that rx queue */
1934 count = mvneta_rx(pp, budget, rxq);
1935 rx_done += count;
1936 budget -= count;
1937 if (budget > 0) {
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001938 /* set off the rx bit of the
1939 * corresponding bit in the cause rx
1940 * tx register, so that next iteration
1941 * will find the next rx queue where
1942 * packets are received on
1943 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001944 cause_rx_tx &= ~((1 << rxq->id) << 8);
1945 }
1946 }
1947 } else {
1948 rx_done = mvneta_rx(pp, budget, &pp->rxqs[rxq_def]);
1949 budget -= rx_done;
1950 }
1951
1952 if (budget > 0) {
1953 cause_rx_tx = 0;
1954 napi_complete(napi);
1955 local_irq_save(flags);
1956 mvreg_write(pp, MVNETA_INTR_NEW_MASK,
willy tarreau71f6d1b2014-01-16 08:20:11 +01001957 MVNETA_RX_INTR_MASK(rxq_number) | MVNETA_TX_INTR_MASK(txq_number));
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001958 local_irq_restore(flags);
1959 }
1960
1961 pp->cause_rx_tx = cause_rx_tx;
1962 return rx_done;
1963}
1964
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001965/* Handle rxq fill: allocates rxq skbs; called when initializing a port */
1966static int mvneta_rxq_fill(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
1967 int num)
1968{
1969 struct net_device *dev = pp->dev;
1970 int i;
1971
1972 for (i = 0; i < num; i++) {
1973 struct sk_buff *skb;
1974 struct mvneta_rx_desc *rx_desc;
1975 unsigned long phys_addr;
1976
1977 skb = dev_alloc_skb(pp->pkt_size);
1978 if (!skb) {
1979 netdev_err(dev, "%s:rxq %d, %d of %d buffs filled\n",
1980 __func__, rxq->id, i, num);
1981 break;
1982 }
1983
1984 rx_desc = rxq->descs + i;
1985 memset(rx_desc, 0, sizeof(struct mvneta_rx_desc));
1986 phys_addr = dma_map_single(dev->dev.parent, skb->head,
1987 MVNETA_RX_BUF_SIZE(pp->pkt_size),
1988 DMA_FROM_DEVICE);
1989 if (unlikely(dma_mapping_error(dev->dev.parent, phys_addr))) {
1990 dev_kfree_skb(skb);
1991 break;
1992 }
1993
1994 mvneta_rx_desc_fill(rx_desc, phys_addr, (u32)skb);
1995 }
1996
1997 /* Add this number of RX descriptors as non occupied (ready to
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001998 * get packets)
1999 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002000 mvneta_rxq_non_occup_desc_add(pp, rxq, i);
2001
2002 return i;
2003}
2004
2005/* Free all packets pending transmit from all TXQs and reset TX port */
2006static void mvneta_tx_reset(struct mvneta_port *pp)
2007{
2008 int queue;
2009
2010 /* free the skb's in the hal tx ring */
2011 for (queue = 0; queue < txq_number; queue++)
2012 mvneta_txq_done_force(pp, &pp->txqs[queue]);
2013
2014 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
2015 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
2016}
2017
2018static void mvneta_rx_reset(struct mvneta_port *pp)
2019{
2020 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
2021 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
2022}
2023
2024/* Rx/Tx queue initialization/cleanup methods */
2025
2026/* Create a specified RX queue */
2027static int mvneta_rxq_init(struct mvneta_port *pp,
2028 struct mvneta_rx_queue *rxq)
2029
2030{
2031 rxq->size = pp->rx_ring_size;
2032
2033 /* Allocate memory for RX descriptors */
2034 rxq->descs = dma_alloc_coherent(pp->dev->dev.parent,
2035 rxq->size * MVNETA_DESC_ALIGNED_SIZE,
2036 &rxq->descs_phys, GFP_KERNEL);
Joe Perchesd0320f72013-03-14 13:07:21 +00002037 if (rxq->descs == NULL)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002038 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002039
2040 BUG_ON(rxq->descs !=
2041 PTR_ALIGN(rxq->descs, MVNETA_CPU_D_CACHE_LINE_SIZE));
2042
2043 rxq->last_desc = rxq->size - 1;
2044
2045 /* Set Rx descriptors queue starting address */
2046 mvreg_write(pp, MVNETA_RXQ_BASE_ADDR_REG(rxq->id), rxq->descs_phys);
2047 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), rxq->size);
2048
2049 /* Set Offset */
2050 mvneta_rxq_offset_set(pp, rxq, NET_SKB_PAD);
2051
2052 /* Set coalescing pkts and time */
2053 mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
2054 mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
2055
2056 /* Fill RXQ with buffers from RX pool */
2057 mvneta_rxq_buf_size_set(pp, rxq, MVNETA_RX_BUF_SIZE(pp->pkt_size));
2058 mvneta_rxq_bm_disable(pp, rxq);
2059 mvneta_rxq_fill(pp, rxq, rxq->size);
2060
2061 return 0;
2062}
2063
2064/* Cleanup Rx queue */
2065static void mvneta_rxq_deinit(struct mvneta_port *pp,
2066 struct mvneta_rx_queue *rxq)
2067{
2068 mvneta_rxq_drop_pkts(pp, rxq);
2069
2070 if (rxq->descs)
2071 dma_free_coherent(pp->dev->dev.parent,
2072 rxq->size * MVNETA_DESC_ALIGNED_SIZE,
2073 rxq->descs,
2074 rxq->descs_phys);
2075
2076 rxq->descs = NULL;
2077 rxq->last_desc = 0;
2078 rxq->next_desc_to_proc = 0;
2079 rxq->descs_phys = 0;
2080}
2081
2082/* Create and initialize a tx queue */
2083static int mvneta_txq_init(struct mvneta_port *pp,
2084 struct mvneta_tx_queue *txq)
2085{
2086 txq->size = pp->tx_ring_size;
2087
2088 /* Allocate memory for TX descriptors */
2089 txq->descs = dma_alloc_coherent(pp->dev->dev.parent,
2090 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2091 &txq->descs_phys, GFP_KERNEL);
Joe Perchesd0320f72013-03-14 13:07:21 +00002092 if (txq->descs == NULL)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002093 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002094
2095 /* Make sure descriptor address is cache line size aligned */
2096 BUG_ON(txq->descs !=
2097 PTR_ALIGN(txq->descs, MVNETA_CPU_D_CACHE_LINE_SIZE));
2098
2099 txq->last_desc = txq->size - 1;
2100
2101 /* Set maximum bandwidth for enabled TXQs */
2102 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0x03ffffff);
2103 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0x3fffffff);
2104
2105 /* Set Tx descriptors queue starting address */
2106 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), txq->descs_phys);
2107 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), txq->size);
2108
2109 txq->tx_skb = kmalloc(txq->size * sizeof(*txq->tx_skb), GFP_KERNEL);
2110 if (txq->tx_skb == NULL) {
2111 dma_free_coherent(pp->dev->dev.parent,
2112 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2113 txq->descs, txq->descs_phys);
2114 return -ENOMEM;
2115 }
2116 mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
2117
2118 return 0;
2119}
2120
2121/* Free allocated resources when mvneta_txq_init() fails to allocate memory*/
2122static void mvneta_txq_deinit(struct mvneta_port *pp,
2123 struct mvneta_tx_queue *txq)
2124{
2125 kfree(txq->tx_skb);
2126
2127 if (txq->descs)
2128 dma_free_coherent(pp->dev->dev.parent,
2129 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2130 txq->descs, txq->descs_phys);
2131
2132 txq->descs = NULL;
2133 txq->last_desc = 0;
2134 txq->next_desc_to_proc = 0;
2135 txq->descs_phys = 0;
2136
2137 /* Set minimum bandwidth for disabled TXQs */
2138 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0);
2139 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0);
2140
2141 /* Set Tx descriptors queue starting address and size */
2142 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), 0);
2143 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), 0);
2144}
2145
2146/* Cleanup all Tx queues */
2147static void mvneta_cleanup_txqs(struct mvneta_port *pp)
2148{
2149 int queue;
2150
2151 for (queue = 0; queue < txq_number; queue++)
2152 mvneta_txq_deinit(pp, &pp->txqs[queue]);
2153}
2154
2155/* Cleanup all Rx queues */
2156static void mvneta_cleanup_rxqs(struct mvneta_port *pp)
2157{
2158 int queue;
2159
2160 for (queue = 0; queue < rxq_number; queue++)
2161 mvneta_rxq_deinit(pp, &pp->rxqs[queue]);
2162}
2163
2164
2165/* Init all Rx queues */
2166static int mvneta_setup_rxqs(struct mvneta_port *pp)
2167{
2168 int queue;
2169
2170 for (queue = 0; queue < rxq_number; queue++) {
2171 int err = mvneta_rxq_init(pp, &pp->rxqs[queue]);
2172 if (err) {
2173 netdev_err(pp->dev, "%s: can't create rxq=%d\n",
2174 __func__, queue);
2175 mvneta_cleanup_rxqs(pp);
2176 return err;
2177 }
2178 }
2179
2180 return 0;
2181}
2182
2183/* Init all tx queues */
2184static int mvneta_setup_txqs(struct mvneta_port *pp)
2185{
2186 int queue;
2187
2188 for (queue = 0; queue < txq_number; queue++) {
2189 int err = mvneta_txq_init(pp, &pp->txqs[queue]);
2190 if (err) {
2191 netdev_err(pp->dev, "%s: can't create txq=%d\n",
2192 __func__, queue);
2193 mvneta_cleanup_txqs(pp);
2194 return err;
2195 }
2196 }
2197
2198 return 0;
2199}
2200
2201static void mvneta_start_dev(struct mvneta_port *pp)
2202{
2203 mvneta_max_rx_size_set(pp, pp->pkt_size);
2204 mvneta_txq_max_tx_size_set(pp, pp->pkt_size);
2205
2206 /* start the Rx/Tx activity */
2207 mvneta_port_enable(pp);
2208
2209 /* Enable polling on the port */
2210 napi_enable(&pp->napi);
2211
2212 /* Unmask interrupts */
2213 mvreg_write(pp, MVNETA_INTR_NEW_MASK,
willy tarreau71f6d1b2014-01-16 08:20:11 +01002214 MVNETA_RX_INTR_MASK(rxq_number) | MVNETA_TX_INTR_MASK(txq_number));
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002215
2216 phy_start(pp->phy_dev);
2217 netif_tx_start_all_queues(pp->dev);
2218}
2219
2220static void mvneta_stop_dev(struct mvneta_port *pp)
2221{
2222 phy_stop(pp->phy_dev);
2223
2224 napi_disable(&pp->napi);
2225
2226 netif_carrier_off(pp->dev);
2227
2228 mvneta_port_down(pp);
2229 netif_tx_stop_all_queues(pp->dev);
2230
2231 /* Stop the port activity */
2232 mvneta_port_disable(pp);
2233
2234 /* Clear all ethernet port interrupts */
2235 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
2236 mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0);
2237
2238 /* Mask all ethernet port interrupts */
2239 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
2240 mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0);
2241 mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0);
2242
2243 mvneta_tx_reset(pp);
2244 mvneta_rx_reset(pp);
2245}
2246
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002247/* Return positive if MTU is valid */
2248static int mvneta_check_mtu_valid(struct net_device *dev, int mtu)
2249{
2250 if (mtu < 68) {
2251 netdev_err(dev, "cannot change mtu to less than 68\n");
2252 return -EINVAL;
2253 }
2254
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002255 /* 9676 == 9700 - 20 and rounding to 8 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002256 if (mtu > 9676) {
2257 netdev_info(dev, "Illegal MTU value %d, round to 9676\n", mtu);
2258 mtu = 9676;
2259 }
2260
2261 if (!IS_ALIGNED(MVNETA_RX_PKT_SIZE(mtu), 8)) {
2262 netdev_info(dev, "Illegal MTU value %d, rounding to %d\n",
2263 mtu, ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8));
2264 mtu = ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8);
2265 }
2266
2267 return mtu;
2268}
2269
2270/* Change the device mtu */
2271static int mvneta_change_mtu(struct net_device *dev, int mtu)
2272{
2273 struct mvneta_port *pp = netdev_priv(dev);
2274 int ret;
2275
2276 mtu = mvneta_check_mtu_valid(dev, mtu);
2277 if (mtu < 0)
2278 return -EINVAL;
2279
2280 dev->mtu = mtu;
2281
2282 if (!netif_running(dev))
2283 return 0;
2284
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002285 /* The interface is running, so we have to force a
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002286 * reallocation of the RXQs
2287 */
2288 mvneta_stop_dev(pp);
2289
2290 mvneta_cleanup_txqs(pp);
2291 mvneta_cleanup_rxqs(pp);
2292
2293 pp->pkt_size = MVNETA_RX_PKT_SIZE(pp->dev->mtu);
2294
2295 ret = mvneta_setup_rxqs(pp);
2296 if (ret) {
2297 netdev_err(pp->dev, "unable to setup rxqs after MTU change\n");
2298 return ret;
2299 }
2300
2301 mvneta_setup_txqs(pp);
2302
2303 mvneta_start_dev(pp);
2304 mvneta_port_up(pp);
2305
2306 return 0;
2307}
2308
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00002309/* Get mac address */
2310static void mvneta_get_mac_addr(struct mvneta_port *pp, unsigned char *addr)
2311{
2312 u32 mac_addr_l, mac_addr_h;
2313
2314 mac_addr_l = mvreg_read(pp, MVNETA_MAC_ADDR_LOW);
2315 mac_addr_h = mvreg_read(pp, MVNETA_MAC_ADDR_HIGH);
2316 addr[0] = (mac_addr_h >> 24) & 0xFF;
2317 addr[1] = (mac_addr_h >> 16) & 0xFF;
2318 addr[2] = (mac_addr_h >> 8) & 0xFF;
2319 addr[3] = mac_addr_h & 0xFF;
2320 addr[4] = (mac_addr_l >> 8) & 0xFF;
2321 addr[5] = mac_addr_l & 0xFF;
2322}
2323
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002324/* Handle setting mac address */
2325static int mvneta_set_mac_addr(struct net_device *dev, void *addr)
2326{
2327 struct mvneta_port *pp = netdev_priv(dev);
2328 u8 *mac = addr + 2;
2329 int i;
2330
2331 if (netif_running(dev))
2332 return -EBUSY;
2333
2334 /* Remove previous address table entry */
2335 mvneta_mac_addr_set(pp, dev->dev_addr, -1);
2336
2337 /* Set new addr in hw */
2338 mvneta_mac_addr_set(pp, mac, rxq_def);
2339
2340 /* Set addr in the device */
2341 for (i = 0; i < ETH_ALEN; i++)
2342 dev->dev_addr[i] = mac[i];
2343
2344 return 0;
2345}
2346
2347static void mvneta_adjust_link(struct net_device *ndev)
2348{
2349 struct mvneta_port *pp = netdev_priv(ndev);
2350 struct phy_device *phydev = pp->phy_dev;
2351 int status_change = 0;
2352
2353 if (phydev->link) {
2354 if ((pp->speed != phydev->speed) ||
2355 (pp->duplex != phydev->duplex)) {
2356 u32 val;
2357
2358 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
2359 val &= ~(MVNETA_GMAC_CONFIG_MII_SPEED |
2360 MVNETA_GMAC_CONFIG_GMII_SPEED |
Thomas Petazzoni71408602013-09-04 16:21:18 +02002361 MVNETA_GMAC_CONFIG_FULL_DUPLEX |
2362 MVNETA_GMAC_AN_SPEED_EN |
2363 MVNETA_GMAC_AN_DUPLEX_EN);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002364
2365 if (phydev->duplex)
2366 val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
2367
2368 if (phydev->speed == SPEED_1000)
2369 val |= MVNETA_GMAC_CONFIG_GMII_SPEED;
2370 else
2371 val |= MVNETA_GMAC_CONFIG_MII_SPEED;
2372
2373 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
2374
2375 pp->duplex = phydev->duplex;
2376 pp->speed = phydev->speed;
2377 }
2378 }
2379
2380 if (phydev->link != pp->link) {
2381 if (!phydev->link) {
2382 pp->duplex = -1;
2383 pp->speed = 0;
2384 }
2385
2386 pp->link = phydev->link;
2387 status_change = 1;
2388 }
2389
2390 if (status_change) {
2391 if (phydev->link) {
2392 u32 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
2393 val |= (MVNETA_GMAC_FORCE_LINK_PASS |
2394 MVNETA_GMAC_FORCE_LINK_DOWN);
2395 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
2396 mvneta_port_up(pp);
2397 netdev_info(pp->dev, "link up\n");
2398 } else {
2399 mvneta_port_down(pp);
2400 netdev_info(pp->dev, "link down\n");
2401 }
2402 }
2403}
2404
2405static int mvneta_mdio_probe(struct mvneta_port *pp)
2406{
2407 struct phy_device *phy_dev;
2408
2409 phy_dev = of_phy_connect(pp->dev, pp->phy_node, mvneta_adjust_link, 0,
2410 pp->phy_interface);
2411 if (!phy_dev) {
2412 netdev_err(pp->dev, "could not find the PHY\n");
2413 return -ENODEV;
2414 }
2415
2416 phy_dev->supported &= PHY_GBIT_FEATURES;
2417 phy_dev->advertising = phy_dev->supported;
2418
2419 pp->phy_dev = phy_dev;
2420 pp->link = 0;
2421 pp->duplex = 0;
2422 pp->speed = 0;
2423
2424 return 0;
2425}
2426
2427static void mvneta_mdio_remove(struct mvneta_port *pp)
2428{
2429 phy_disconnect(pp->phy_dev);
2430 pp->phy_dev = NULL;
2431}
2432
2433static int mvneta_open(struct net_device *dev)
2434{
2435 struct mvneta_port *pp = netdev_priv(dev);
2436 int ret;
2437
2438 mvneta_mac_addr_set(pp, dev->dev_addr, rxq_def);
2439
2440 pp->pkt_size = MVNETA_RX_PKT_SIZE(pp->dev->mtu);
2441
2442 ret = mvneta_setup_rxqs(pp);
2443 if (ret)
2444 return ret;
2445
2446 ret = mvneta_setup_txqs(pp);
2447 if (ret)
2448 goto err_cleanup_rxqs;
2449
2450 /* Connect to port interrupt line */
2451 ret = request_irq(pp->dev->irq, mvneta_isr, 0,
2452 MVNETA_DRIVER_NAME, pp);
2453 if (ret) {
2454 netdev_err(pp->dev, "cannot request irq %d\n", pp->dev->irq);
2455 goto err_cleanup_txqs;
2456 }
2457
2458 /* In default link is down */
2459 netif_carrier_off(pp->dev);
2460
2461 ret = mvneta_mdio_probe(pp);
2462 if (ret < 0) {
2463 netdev_err(dev, "cannot probe MDIO bus\n");
2464 goto err_free_irq;
2465 }
2466
2467 mvneta_start_dev(pp);
2468
2469 return 0;
2470
2471err_free_irq:
2472 free_irq(pp->dev->irq, pp);
2473err_cleanup_txqs:
2474 mvneta_cleanup_txqs(pp);
2475err_cleanup_rxqs:
2476 mvneta_cleanup_rxqs(pp);
2477 return ret;
2478}
2479
2480/* Stop the port, free port interrupt line */
2481static int mvneta_stop(struct net_device *dev)
2482{
2483 struct mvneta_port *pp = netdev_priv(dev);
2484
2485 mvneta_stop_dev(pp);
2486 mvneta_mdio_remove(pp);
2487 free_irq(dev->irq, pp);
2488 mvneta_cleanup_rxqs(pp);
2489 mvneta_cleanup_txqs(pp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002490
2491 return 0;
2492}
2493
Thomas Petazzoni15f59452013-09-04 16:26:52 +02002494static int mvneta_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2495{
2496 struct mvneta_port *pp = netdev_priv(dev);
2497 int ret;
2498
2499 if (!pp->phy_dev)
2500 return -ENOTSUPP;
2501
2502 ret = phy_mii_ioctl(pp->phy_dev, ifr, cmd);
2503 if (!ret)
2504 mvneta_adjust_link(dev);
2505
2506 return ret;
2507}
2508
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002509/* Ethtool methods */
2510
2511/* Get settings (phy address, speed) for ethtools */
2512int mvneta_ethtool_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2513{
2514 struct mvneta_port *pp = netdev_priv(dev);
2515
2516 if (!pp->phy_dev)
2517 return -ENODEV;
2518
2519 return phy_ethtool_gset(pp->phy_dev, cmd);
2520}
2521
2522/* Set settings (phy address, speed) for ethtools */
2523int mvneta_ethtool_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2524{
2525 struct mvneta_port *pp = netdev_priv(dev);
2526
2527 if (!pp->phy_dev)
2528 return -ENODEV;
2529
2530 return phy_ethtool_sset(pp->phy_dev, cmd);
2531}
2532
2533/* Set interrupt coalescing for ethtools */
2534static int mvneta_ethtool_set_coalesce(struct net_device *dev,
2535 struct ethtool_coalesce *c)
2536{
2537 struct mvneta_port *pp = netdev_priv(dev);
2538 int queue;
2539
2540 for (queue = 0; queue < rxq_number; queue++) {
2541 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
2542 rxq->time_coal = c->rx_coalesce_usecs;
2543 rxq->pkts_coal = c->rx_max_coalesced_frames;
2544 mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
2545 mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
2546 }
2547
2548 for (queue = 0; queue < txq_number; queue++) {
2549 struct mvneta_tx_queue *txq = &pp->txqs[queue];
2550 txq->done_pkts_coal = c->tx_max_coalesced_frames;
2551 mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
2552 }
2553
2554 return 0;
2555}
2556
2557/* get coalescing for ethtools */
2558static int mvneta_ethtool_get_coalesce(struct net_device *dev,
2559 struct ethtool_coalesce *c)
2560{
2561 struct mvneta_port *pp = netdev_priv(dev);
2562
2563 c->rx_coalesce_usecs = pp->rxqs[0].time_coal;
2564 c->rx_max_coalesced_frames = pp->rxqs[0].pkts_coal;
2565
2566 c->tx_max_coalesced_frames = pp->txqs[0].done_pkts_coal;
2567 return 0;
2568}
2569
2570
2571static void mvneta_ethtool_get_drvinfo(struct net_device *dev,
2572 struct ethtool_drvinfo *drvinfo)
2573{
2574 strlcpy(drvinfo->driver, MVNETA_DRIVER_NAME,
2575 sizeof(drvinfo->driver));
2576 strlcpy(drvinfo->version, MVNETA_DRIVER_VERSION,
2577 sizeof(drvinfo->version));
2578 strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
2579 sizeof(drvinfo->bus_info));
2580}
2581
2582
2583static void mvneta_ethtool_get_ringparam(struct net_device *netdev,
2584 struct ethtool_ringparam *ring)
2585{
2586 struct mvneta_port *pp = netdev_priv(netdev);
2587
2588 ring->rx_max_pending = MVNETA_MAX_RXD;
2589 ring->tx_max_pending = MVNETA_MAX_TXD;
2590 ring->rx_pending = pp->rx_ring_size;
2591 ring->tx_pending = pp->tx_ring_size;
2592}
2593
2594static int mvneta_ethtool_set_ringparam(struct net_device *dev,
2595 struct ethtool_ringparam *ring)
2596{
2597 struct mvneta_port *pp = netdev_priv(dev);
2598
2599 if ((ring->rx_pending == 0) || (ring->tx_pending == 0))
2600 return -EINVAL;
2601 pp->rx_ring_size = ring->rx_pending < MVNETA_MAX_RXD ?
2602 ring->rx_pending : MVNETA_MAX_RXD;
2603 pp->tx_ring_size = ring->tx_pending < MVNETA_MAX_TXD ?
2604 ring->tx_pending : MVNETA_MAX_TXD;
2605
2606 if (netif_running(dev)) {
2607 mvneta_stop(dev);
2608 if (mvneta_open(dev)) {
2609 netdev_err(dev,
2610 "error on opening device after ring param change\n");
2611 return -ENOMEM;
2612 }
2613 }
2614
2615 return 0;
2616}
2617
2618static const struct net_device_ops mvneta_netdev_ops = {
2619 .ndo_open = mvneta_open,
2620 .ndo_stop = mvneta_stop,
2621 .ndo_start_xmit = mvneta_tx,
2622 .ndo_set_rx_mode = mvneta_set_rx_mode,
2623 .ndo_set_mac_address = mvneta_set_mac_addr,
2624 .ndo_change_mtu = mvneta_change_mtu,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002625 .ndo_get_stats64 = mvneta_get_stats64,
Thomas Petazzoni15f59452013-09-04 16:26:52 +02002626 .ndo_do_ioctl = mvneta_ioctl,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002627};
2628
2629const struct ethtool_ops mvneta_eth_tool_ops = {
2630 .get_link = ethtool_op_get_link,
2631 .get_settings = mvneta_ethtool_get_settings,
2632 .set_settings = mvneta_ethtool_set_settings,
2633 .set_coalesce = mvneta_ethtool_set_coalesce,
2634 .get_coalesce = mvneta_ethtool_get_coalesce,
2635 .get_drvinfo = mvneta_ethtool_get_drvinfo,
2636 .get_ringparam = mvneta_ethtool_get_ringparam,
2637 .set_ringparam = mvneta_ethtool_set_ringparam,
2638};
2639
2640/* Initialize hw */
Greg KH03ce7582012-12-21 13:42:15 +00002641static int mvneta_init(struct mvneta_port *pp, int phy_addr)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002642{
2643 int queue;
2644
2645 /* Disable port */
2646 mvneta_port_disable(pp);
2647
2648 /* Set port default values */
2649 mvneta_defaults_set(pp);
2650
2651 pp->txqs = kzalloc(txq_number * sizeof(struct mvneta_tx_queue),
2652 GFP_KERNEL);
2653 if (!pp->txqs)
2654 return -ENOMEM;
2655
2656 /* Initialize TX descriptor rings */
2657 for (queue = 0; queue < txq_number; queue++) {
2658 struct mvneta_tx_queue *txq = &pp->txqs[queue];
2659 txq->id = queue;
2660 txq->size = pp->tx_ring_size;
2661 txq->done_pkts_coal = MVNETA_TXDONE_COAL_PKTS;
2662 }
2663
2664 pp->rxqs = kzalloc(rxq_number * sizeof(struct mvneta_rx_queue),
2665 GFP_KERNEL);
2666 if (!pp->rxqs) {
2667 kfree(pp->txqs);
2668 return -ENOMEM;
2669 }
2670
2671 /* Create Rx descriptor rings */
2672 for (queue = 0; queue < rxq_number; queue++) {
2673 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
2674 rxq->id = queue;
2675 rxq->size = pp->rx_ring_size;
2676 rxq->pkts_coal = MVNETA_RX_COAL_PKTS;
2677 rxq->time_coal = MVNETA_RX_COAL_USEC;
2678 }
2679
2680 return 0;
2681}
2682
Thomas Petazzoni70eeaf92012-11-19 14:40:02 +01002683static void mvneta_deinit(struct mvneta_port *pp)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002684{
2685 kfree(pp->txqs);
2686 kfree(pp->rxqs);
2687}
2688
2689/* platform glue : initialize decoding windows */
Greg KH03ce7582012-12-21 13:42:15 +00002690static void mvneta_conf_mbus_windows(struct mvneta_port *pp,
2691 const struct mbus_dram_target_info *dram)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002692{
2693 u32 win_enable;
2694 u32 win_protect;
2695 int i;
2696
2697 for (i = 0; i < 6; i++) {
2698 mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
2699 mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
2700
2701 if (i < 4)
2702 mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
2703 }
2704
2705 win_enable = 0x3f;
2706 win_protect = 0;
2707
2708 for (i = 0; i < dram->num_cs; i++) {
2709 const struct mbus_dram_window *cs = dram->cs + i;
2710 mvreg_write(pp, MVNETA_WIN_BASE(i), (cs->base & 0xffff0000) |
2711 (cs->mbus_attr << 8) | dram->mbus_dram_target_id);
2712
2713 mvreg_write(pp, MVNETA_WIN_SIZE(i),
2714 (cs->size - 1) & 0xffff0000);
2715
2716 win_enable &= ~(1 << i);
2717 win_protect |= 3 << (2 * i);
2718 }
2719
2720 mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
2721}
2722
2723/* Power up the port */
Greg KH03ce7582012-12-21 13:42:15 +00002724static void mvneta_port_power_up(struct mvneta_port *pp, int phy_mode)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002725{
2726 u32 val;
2727
2728 /* MAC Cause register should be cleared */
2729 mvreg_write(pp, MVNETA_UNIT_INTR_CAUSE, 0);
2730
2731 if (phy_mode == PHY_INTERFACE_MODE_SGMII)
2732 mvneta_port_sgmii_config(pp);
2733
2734 mvneta_gmac_rgmii_set(pp, 1);
2735
2736 /* Cancel Port Reset */
2737 val = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
2738 val &= ~MVNETA_GMAC2_PORT_RESET;
2739 mvreg_write(pp, MVNETA_GMAC_CTRL_2, val);
2740
2741 while ((mvreg_read(pp, MVNETA_GMAC_CTRL_2) &
2742 MVNETA_GMAC2_PORT_RESET) != 0)
2743 continue;
2744}
2745
2746/* Device initialization routine */
Greg KH03ce7582012-12-21 13:42:15 +00002747static int mvneta_probe(struct platform_device *pdev)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002748{
2749 const struct mbus_dram_target_info *dram_target_info;
2750 struct device_node *dn = pdev->dev.of_node;
2751 struct device_node *phy_node;
Thomas Petazzoni189dd622012-11-19 14:15:25 +01002752 u32 phy_addr;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002753 struct mvneta_port *pp;
2754 struct net_device *dev;
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00002755 const char *dt_mac_addr;
2756 char hw_mac_addr[ETH_ALEN];
2757 const char *mac_from;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002758 int phy_mode;
2759 int err;
willy tarreau74c41b02014-01-16 08:20:08 +01002760 int cpu;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002761
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002762 /* Our multiqueue support is not complete, so for now, only
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002763 * allow the usage of the first RX queue
2764 */
2765 if (rxq_def != 0) {
2766 dev_err(&pdev->dev, "Invalid rxq_def argument: %d\n", rxq_def);
2767 return -EINVAL;
2768 }
2769
Willy Tarreauee40a112013-04-11 23:00:37 +02002770 dev = alloc_etherdev_mqs(sizeof(struct mvneta_port), txq_number, rxq_number);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002771 if (!dev)
2772 return -ENOMEM;
2773
2774 dev->irq = irq_of_parse_and_map(dn, 0);
2775 if (dev->irq == 0) {
2776 err = -EINVAL;
2777 goto err_free_netdev;
2778 }
2779
2780 phy_node = of_parse_phandle(dn, "phy", 0);
2781 if (!phy_node) {
2782 dev_err(&pdev->dev, "no associated PHY\n");
2783 err = -ENODEV;
2784 goto err_free_irq;
2785 }
2786
2787 phy_mode = of_get_phy_mode(dn);
2788 if (phy_mode < 0) {
2789 dev_err(&pdev->dev, "incorrect phy-mode\n");
2790 err = -EINVAL;
2791 goto err_free_irq;
2792 }
2793
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002794 dev->tx_queue_len = MVNETA_MAX_TXD;
2795 dev->watchdog_timeo = 5 * HZ;
2796 dev->netdev_ops = &mvneta_netdev_ops;
2797
2798 SET_ETHTOOL_OPS(dev, &mvneta_eth_tool_ops);
2799
2800 pp = netdev_priv(dev);
2801
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002802 pp->weight = MVNETA_RX_POLL_WEIGHT;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002803 pp->phy_node = phy_node;
2804 pp->phy_interface = phy_mode;
2805
Thomas Petazzoni189dd622012-11-19 14:15:25 +01002806 pp->clk = devm_clk_get(&pdev->dev, NULL);
2807 if (IS_ERR(pp->clk)) {
2808 err = PTR_ERR(pp->clk);
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +02002809 goto err_free_irq;
Thomas Petazzoni189dd622012-11-19 14:15:25 +01002810 }
2811
2812 clk_prepare_enable(pp->clk);
2813
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +02002814 pp->base = of_iomap(dn, 0);
2815 if (pp->base == NULL) {
2816 err = -ENOMEM;
2817 goto err_clk;
2818 }
2819
willy tarreau74c41b02014-01-16 08:20:08 +01002820 /* Alloc per-cpu stats */
2821 pp->stats = alloc_percpu(struct mvneta_pcpu_stats);
2822 if (!pp->stats) {
2823 err = -ENOMEM;
2824 goto err_unmap;
2825 }
2826
2827 for_each_possible_cpu(cpu) {
2828 struct mvneta_pcpu_stats *stats;
2829 stats = per_cpu_ptr(pp->stats, cpu);
2830 u64_stats_init(&stats->syncp);
2831 }
2832
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00002833 dt_mac_addr = of_get_mac_address(dn);
Luka Perkov6c7a9a32013-10-30 00:10:01 +01002834 if (dt_mac_addr) {
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00002835 mac_from = "device tree";
2836 memcpy(dev->dev_addr, dt_mac_addr, ETH_ALEN);
2837 } else {
2838 mvneta_get_mac_addr(pp, hw_mac_addr);
2839 if (is_valid_ether_addr(hw_mac_addr)) {
2840 mac_from = "hardware";
2841 memcpy(dev->dev_addr, hw_mac_addr, ETH_ALEN);
2842 } else {
2843 mac_from = "random";
2844 eth_hw_addr_random(dev);
2845 }
2846 }
2847
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002848 pp->tx_ring_size = MVNETA_MAX_TXD;
2849 pp->rx_ring_size = MVNETA_MAX_RXD;
2850
2851 pp->dev = dev;
2852 SET_NETDEV_DEV(dev, &pdev->dev);
2853
2854 err = mvneta_init(pp, phy_addr);
2855 if (err < 0) {
2856 dev_err(&pdev->dev, "can't init eth hal\n");
willy tarreau74c41b02014-01-16 08:20:08 +01002857 goto err_free_stats;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002858 }
2859 mvneta_port_power_up(pp, phy_mode);
2860
2861 dram_target_info = mv_mbus_dram_info();
2862 if (dram_target_info)
2863 mvneta_conf_mbus_windows(pp, dram_target_info);
2864
2865 netif_napi_add(dev, &pp->napi, mvneta_poll, pp->weight);
2866
willy tarreaub50b72d2013-04-06 08:47:01 +00002867 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM;
2868 dev->hw_features |= NETIF_F_SG | NETIF_F_IP_CSUM;
2869 dev->vlan_features |= NETIF_F_SG | NETIF_F_IP_CSUM;
2870 dev->priv_flags |= IFF_UNICAST_FLT;
2871
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002872 err = register_netdev(dev);
2873 if (err < 0) {
2874 dev_err(&pdev->dev, "failed to register\n");
2875 goto err_deinit;
2876 }
2877
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00002878 netdev_info(dev, "Using %s mac address %pM\n", mac_from,
2879 dev->dev_addr);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002880
2881 platform_set_drvdata(pdev, pp->dev);
2882
2883 return 0;
2884
2885err_deinit:
2886 mvneta_deinit(pp);
willy tarreau74c41b02014-01-16 08:20:08 +01002887err_free_stats:
2888 free_percpu(pp->stats);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002889err_unmap:
2890 iounmap(pp->base);
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +02002891err_clk:
2892 clk_disable_unprepare(pp->clk);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002893err_free_irq:
2894 irq_dispose_mapping(dev->irq);
2895err_free_netdev:
2896 free_netdev(dev);
2897 return err;
2898}
2899
2900/* Device removal routine */
Greg KH03ce7582012-12-21 13:42:15 +00002901static int mvneta_remove(struct platform_device *pdev)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002902{
2903 struct net_device *dev = platform_get_drvdata(pdev);
2904 struct mvneta_port *pp = netdev_priv(dev);
2905
2906 unregister_netdev(dev);
2907 mvneta_deinit(pp);
Thomas Petazzoni189dd622012-11-19 14:15:25 +01002908 clk_disable_unprepare(pp->clk);
willy tarreau74c41b02014-01-16 08:20:08 +01002909 free_percpu(pp->stats);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002910 iounmap(pp->base);
2911 irq_dispose_mapping(dev->irq);
2912 free_netdev(dev);
2913
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002914 return 0;
2915}
2916
2917static const struct of_device_id mvneta_match[] = {
2918 { .compatible = "marvell,armada-370-neta" },
2919 { }
2920};
2921MODULE_DEVICE_TABLE(of, mvneta_match);
2922
2923static struct platform_driver mvneta_driver = {
2924 .probe = mvneta_probe,
Greg KH03ce7582012-12-21 13:42:15 +00002925 .remove = mvneta_remove,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002926 .driver = {
2927 .name = MVNETA_DRIVER_NAME,
2928 .of_match_table = mvneta_match,
2929 },
2930};
2931
2932module_platform_driver(mvneta_driver);
2933
2934MODULE_DESCRIPTION("Marvell NETA Ethernet Driver - www.marvell.com");
2935MODULE_AUTHOR("Rami Rosen <rosenr@marvell.com>, Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
2936MODULE_LICENSE("GPL");
2937
2938module_param(rxq_number, int, S_IRUGO);
2939module_param(txq_number, int, S_IRUGO);
2940
2941module_param(rxq_def, int, S_IRUGO);