blob: c9c2faaf967eca9d877bcdec499054cba544e666 [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
Thomas Petazzonie3a87862014-03-26 00:25:42 +010091#define MVNETA_SERDES_CFG 0x24A0
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +020092#define MVNETA_SGMII_SERDES_PROTO 0x0cc7
Thomas Petazzonie3a87862014-03-26 00:25:42 +010093#define MVNETA_RGMII_SERDES_PROTO 0x0667
Thomas Petazzonic5aff182012-08-17 14:04:28 +030094#define MVNETA_TYPE_PRIO 0x24bc
95#define MVNETA_FORCE_UNI BIT(21)
96#define MVNETA_TXQ_CMD_1 0x24e4
97#define MVNETA_TXQ_CMD 0x2448
98#define MVNETA_TXQ_DISABLE_SHIFT 8
99#define MVNETA_TXQ_ENABLE_MASK 0x000000ff
100#define MVNETA_ACC_MODE 0x2500
101#define MVNETA_CPU_MAP(cpu) (0x2540 + ((cpu) << 2))
102#define MVNETA_CPU_RXQ_ACCESS_ALL_MASK 0x000000ff
103#define MVNETA_CPU_TXQ_ACCESS_ALL_MASK 0x0000ff00
104#define MVNETA_RXQ_TIME_COAL_REG(q) (0x2580 + ((q) << 2))
willy tarreau40ba35e2014-01-16 08:20:10 +0100105
106/* Exception Interrupt Port/Queue Cause register */
107
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300108#define MVNETA_INTR_NEW_CAUSE 0x25a0
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300109#define MVNETA_INTR_NEW_MASK 0x25a4
willy tarreau40ba35e2014-01-16 08:20:10 +0100110
111/* bits 0..7 = TXQ SENT, one bit per queue.
112 * bits 8..15 = RXQ OCCUP, one bit per queue.
113 * bits 16..23 = RXQ FREE, one bit per queue.
114 * bit 29 = OLD_REG_SUM, see old reg ?
115 * bit 30 = TX_ERR_SUM, one bit for 4 ports
116 * bit 31 = MISC_SUM, one bit for 4 ports
117 */
118#define MVNETA_TX_INTR_MASK(nr_txqs) (((1 << nr_txqs) - 1) << 0)
119#define MVNETA_TX_INTR_MASK_ALL (0xff << 0)
120#define MVNETA_RX_INTR_MASK(nr_rxqs) (((1 << nr_rxqs) - 1) << 8)
121#define MVNETA_RX_INTR_MASK_ALL (0xff << 8)
122
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300123#define MVNETA_INTR_OLD_CAUSE 0x25a8
124#define MVNETA_INTR_OLD_MASK 0x25ac
willy tarreau40ba35e2014-01-16 08:20:10 +0100125
126/* Data Path Port/Queue Cause Register */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300127#define MVNETA_INTR_MISC_CAUSE 0x25b0
128#define MVNETA_INTR_MISC_MASK 0x25b4
willy tarreau40ba35e2014-01-16 08:20:10 +0100129
130#define MVNETA_CAUSE_PHY_STATUS_CHANGE BIT(0)
131#define MVNETA_CAUSE_LINK_CHANGE BIT(1)
132#define MVNETA_CAUSE_PTP BIT(4)
133
134#define MVNETA_CAUSE_INTERNAL_ADDR_ERR BIT(7)
135#define MVNETA_CAUSE_RX_OVERRUN BIT(8)
136#define MVNETA_CAUSE_RX_CRC_ERROR BIT(9)
137#define MVNETA_CAUSE_RX_LARGE_PKT BIT(10)
138#define MVNETA_CAUSE_TX_UNDERUN BIT(11)
139#define MVNETA_CAUSE_PRBS_ERR BIT(12)
140#define MVNETA_CAUSE_PSC_SYNC_CHANGE BIT(13)
141#define MVNETA_CAUSE_SERDES_SYNC_ERR BIT(14)
142
143#define MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT 16
144#define MVNETA_CAUSE_BMU_ALLOC_ERR_ALL_MASK (0xF << MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT)
145#define MVNETA_CAUSE_BMU_ALLOC_ERR_MASK(pool) (1 << (MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT + (pool)))
146
147#define MVNETA_CAUSE_TXQ_ERROR_SHIFT 24
148#define MVNETA_CAUSE_TXQ_ERROR_ALL_MASK (0xFF << MVNETA_CAUSE_TXQ_ERROR_SHIFT)
149#define MVNETA_CAUSE_TXQ_ERROR_MASK(q) (1 << (MVNETA_CAUSE_TXQ_ERROR_SHIFT + (q)))
150
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300151#define MVNETA_INTR_ENABLE 0x25b8
152#define MVNETA_TXQ_INTR_ENABLE_ALL_MASK 0x0000ff00
willy tarreau40ba35e2014-01-16 08:20:10 +0100153#define MVNETA_RXQ_INTR_ENABLE_ALL_MASK 0xff000000 // note: neta says it's 0x000000FF
154
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300155#define MVNETA_RXQ_CMD 0x2680
156#define MVNETA_RXQ_DISABLE_SHIFT 8
157#define MVNETA_RXQ_ENABLE_MASK 0x000000ff
158#define MVETH_TXQ_TOKEN_COUNT_REG(q) (0x2700 + ((q) << 4))
159#define MVETH_TXQ_TOKEN_CFG_REG(q) (0x2704 + ((q) << 4))
160#define MVNETA_GMAC_CTRL_0 0x2c00
161#define MVNETA_GMAC_MAX_RX_SIZE_SHIFT 2
162#define MVNETA_GMAC_MAX_RX_SIZE_MASK 0x7ffc
163#define MVNETA_GMAC0_PORT_ENABLE BIT(0)
164#define MVNETA_GMAC_CTRL_2 0x2c08
Thomas Petazzonia79121d2014-03-26 00:25:41 +0100165#define MVNETA_GMAC2_PCS_ENABLE BIT(3)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300166#define MVNETA_GMAC2_PORT_RGMII BIT(4)
167#define MVNETA_GMAC2_PORT_RESET BIT(6)
168#define MVNETA_GMAC_STATUS 0x2c10
169#define MVNETA_GMAC_LINK_UP BIT(0)
170#define MVNETA_GMAC_SPEED_1000 BIT(1)
171#define MVNETA_GMAC_SPEED_100 BIT(2)
172#define MVNETA_GMAC_FULL_DUPLEX BIT(3)
173#define MVNETA_GMAC_RX_FLOW_CTRL_ENABLE BIT(4)
174#define MVNETA_GMAC_TX_FLOW_CTRL_ENABLE BIT(5)
175#define MVNETA_GMAC_RX_FLOW_CTRL_ACTIVE BIT(6)
176#define MVNETA_GMAC_TX_FLOW_CTRL_ACTIVE BIT(7)
177#define MVNETA_GMAC_AUTONEG_CONFIG 0x2c0c
178#define MVNETA_GMAC_FORCE_LINK_DOWN BIT(0)
179#define MVNETA_GMAC_FORCE_LINK_PASS BIT(1)
180#define MVNETA_GMAC_CONFIG_MII_SPEED BIT(5)
181#define MVNETA_GMAC_CONFIG_GMII_SPEED BIT(6)
Thomas Petazzoni71408602013-09-04 16:21:18 +0200182#define MVNETA_GMAC_AN_SPEED_EN BIT(7)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300183#define MVNETA_GMAC_CONFIG_FULL_DUPLEX BIT(12)
Thomas Petazzoni71408602013-09-04 16:21:18 +0200184#define MVNETA_GMAC_AN_DUPLEX_EN BIT(13)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300185#define MVNETA_MIB_COUNTERS_BASE 0x3080
186#define MVNETA_MIB_LATE_COLLISION 0x7c
187#define MVNETA_DA_FILT_SPEC_MCAST 0x3400
188#define MVNETA_DA_FILT_OTH_MCAST 0x3500
189#define MVNETA_DA_FILT_UCAST_BASE 0x3600
190#define MVNETA_TXQ_BASE_ADDR_REG(q) (0x3c00 + ((q) << 2))
191#define MVNETA_TXQ_SIZE_REG(q) (0x3c20 + ((q) << 2))
192#define MVNETA_TXQ_SENT_THRESH_ALL_MASK 0x3fff0000
193#define MVNETA_TXQ_SENT_THRESH_MASK(coal) ((coal) << 16)
194#define MVNETA_TXQ_UPDATE_REG(q) (0x3c60 + ((q) << 2))
195#define MVNETA_TXQ_DEC_SENT_SHIFT 16
196#define MVNETA_TXQ_STATUS_REG(q) (0x3c40 + ((q) << 2))
197#define MVNETA_TXQ_SENT_DESC_SHIFT 16
198#define MVNETA_TXQ_SENT_DESC_MASK 0x3fff0000
199#define MVNETA_PORT_TX_RESET 0x3cf0
200#define MVNETA_PORT_TX_DMA_RESET BIT(0)
201#define MVNETA_TX_MTU 0x3e0c
202#define MVNETA_TX_TOKEN_SIZE 0x3e14
203#define MVNETA_TX_TOKEN_SIZE_MAX 0xffffffff
204#define MVNETA_TXQ_TOKEN_SIZE_REG(q) (0x3e40 + ((q) << 2))
205#define MVNETA_TXQ_TOKEN_SIZE_MAX 0x7fffffff
206
207#define MVNETA_CAUSE_TXQ_SENT_DESC_ALL_MASK 0xff
208
209/* Descriptor ring Macros */
210#define MVNETA_QUEUE_NEXT_DESC(q, index) \
211 (((index) < (q)->last_desc) ? ((index) + 1) : 0)
212
213/* Various constants */
214
215/* Coalescing */
216#define MVNETA_TXDONE_COAL_PKTS 16
217#define MVNETA_RX_COAL_PKTS 32
218#define MVNETA_RX_COAL_USEC 100
219
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300220/* Napi polling weight */
221#define MVNETA_RX_POLL_WEIGHT 64
222
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100223/* The two bytes Marvell header. Either contains a special value used
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300224 * by Marvell switches when a specific hardware mode is enabled (not
225 * supported by this driver) or is filled automatically by zeroes on
226 * the RX side. Those two bytes being at the front of the Ethernet
227 * header, they allow to have the IP header aligned on a 4 bytes
228 * boundary automatically: the hardware skips those two bytes on its
229 * own.
230 */
231#define MVNETA_MH_SIZE 2
232
233#define MVNETA_VLAN_TAG_LEN 4
234
235#define MVNETA_CPU_D_CACHE_LINE_SIZE 32
236#define MVNETA_TX_CSUM_MAX_SIZE 9800
237#define MVNETA_ACC_MODE_EXT 1
238
239/* Timeout constants */
240#define MVNETA_TX_DISABLE_TIMEOUT_MSEC 1000
241#define MVNETA_RX_DISABLE_TIMEOUT_MSEC 1000
242#define MVNETA_TX_FIFO_EMPTY_TIMEOUT 10000
243
244#define MVNETA_TX_MTU_MAX 0x3ffff
245
246/* Max number of Rx descriptors */
247#define MVNETA_MAX_RXD 128
248
249/* Max number of Tx descriptors */
250#define MVNETA_MAX_TXD 532
251
252/* descriptor aligned size */
253#define MVNETA_DESC_ALIGNED_SIZE 32
254
255#define MVNETA_RX_PKT_SIZE(mtu) \
256 ALIGN((mtu) + MVNETA_MH_SIZE + MVNETA_VLAN_TAG_LEN + \
257 ETH_HLEN + ETH_FCS_LEN, \
258 MVNETA_CPU_D_CACHE_LINE_SIZE)
259
260#define MVNETA_RX_BUF_SIZE(pkt_size) ((pkt_size) + NET_SKB_PAD)
261
willy tarreau74c41b02014-01-16 08:20:08 +0100262struct mvneta_pcpu_stats {
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300263 struct u64_stats_sync syncp;
willy tarreau74c41b02014-01-16 08:20:08 +0100264 u64 rx_packets;
265 u64 rx_bytes;
266 u64 tx_packets;
267 u64 tx_bytes;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300268};
269
270struct mvneta_port {
271 int pkt_size;
willy tarreau8ec2cd42014-01-16 08:20:16 +0100272 unsigned int frag_size;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300273 void __iomem *base;
274 struct mvneta_rx_queue *rxqs;
275 struct mvneta_tx_queue *txqs;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300276 struct net_device *dev;
277
278 u32 cause_rx_tx;
279 struct napi_struct napi;
280
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300281 /* Napi weight */
282 int weight;
283
284 /* Core clock */
Thomas Petazzoni189dd622012-11-19 14:15:25 +0100285 struct clk *clk;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300286 u8 mcast_count[256];
287 u16 tx_ring_size;
288 u16 rx_ring_size;
willy tarreau74c41b02014-01-16 08:20:08 +0100289 struct mvneta_pcpu_stats *stats;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300290
291 struct mii_bus *mii_bus;
292 struct phy_device *phy_dev;
293 phy_interface_t phy_interface;
294 struct device_node *phy_node;
295 unsigned int link;
296 unsigned int duplex;
297 unsigned int speed;
298};
299
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100300/* The mvneta_tx_desc and mvneta_rx_desc structures describe the
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300301 * layout of the transmit and reception DMA descriptors, and their
302 * layout is therefore defined by the hardware design
303 */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200304
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300305#define MVNETA_TX_L3_OFF_SHIFT 0
306#define MVNETA_TX_IP_HLEN_SHIFT 8
307#define MVNETA_TX_L4_UDP BIT(16)
308#define MVNETA_TX_L3_IP6 BIT(17)
309#define MVNETA_TXD_IP_CSUM BIT(18)
310#define MVNETA_TXD_Z_PAD BIT(19)
311#define MVNETA_TXD_L_DESC BIT(20)
312#define MVNETA_TXD_F_DESC BIT(21)
313#define MVNETA_TXD_FLZ_DESC (MVNETA_TXD_Z_PAD | \
314 MVNETA_TXD_L_DESC | \
315 MVNETA_TXD_F_DESC)
316#define MVNETA_TX_L4_CSUM_FULL BIT(30)
317#define MVNETA_TX_L4_CSUM_NOT BIT(31)
318
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300319#define MVNETA_RXD_ERR_CRC 0x0
320#define MVNETA_RXD_ERR_SUMMARY BIT(16)
321#define MVNETA_RXD_ERR_OVERRUN BIT(17)
322#define MVNETA_RXD_ERR_LEN BIT(18)
323#define MVNETA_RXD_ERR_RESOURCE (BIT(17) | BIT(18))
324#define MVNETA_RXD_ERR_CODE_MASK (BIT(17) | BIT(18))
325#define MVNETA_RXD_L3_IP4 BIT(25)
326#define MVNETA_RXD_FIRST_LAST_DESC (BIT(26) | BIT(27))
327#define MVNETA_RXD_L4_CSUM_OK BIT(30)
328
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +0200329#if defined(__LITTLE_ENDIAN)
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200330struct mvneta_tx_desc {
331 u32 command; /* Options used by HW for packet transmitting.*/
332 u16 reserverd1; /* csum_l4 (for future use) */
333 u16 data_size; /* Data size of transmitted packet in bytes */
334 u32 buf_phys_addr; /* Physical addr of transmitted buffer */
335 u32 reserved2; /* hw_cmd - (for future use, PMT) */
336 u32 reserved3[4]; /* Reserved - (for future use) */
337};
338
339struct mvneta_rx_desc {
340 u32 status; /* Info about received packet */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300341 u16 reserved1; /* pnc_info - (for future use, PnC) */
342 u16 data_size; /* Size of received packet in bytes */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200343
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300344 u32 buf_phys_addr; /* Physical address of the buffer */
345 u32 reserved2; /* pnc_flow_id (for future use, PnC) */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200346
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300347 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
348 u16 reserved3; /* prefetch_cmd, for future use */
349 u16 reserved4; /* csum_l4 - (for future use, PnC) */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200350
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300351 u32 reserved5; /* pnc_extra PnC (for future use, PnC) */
352 u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */
353};
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +0200354#else
355struct mvneta_tx_desc {
356 u16 data_size; /* Data size of transmitted packet in bytes */
357 u16 reserverd1; /* csum_l4 (for future use) */
358 u32 command; /* Options used by HW for packet transmitting.*/
359 u32 reserved2; /* hw_cmd - (for future use, PMT) */
360 u32 buf_phys_addr; /* Physical addr of transmitted buffer */
361 u32 reserved3[4]; /* Reserved - (for future use) */
362};
363
364struct mvneta_rx_desc {
365 u16 data_size; /* Size of received packet in bytes */
366 u16 reserved1; /* pnc_info - (for future use, PnC) */
367 u32 status; /* Info about received packet */
368
369 u32 reserved2; /* pnc_flow_id (for future use, PnC) */
370 u32 buf_phys_addr; /* Physical address of the buffer */
371
372 u16 reserved4; /* csum_l4 - (for future use, PnC) */
373 u16 reserved3; /* prefetch_cmd, for future use */
374 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
375
376 u32 reserved5; /* pnc_extra PnC (for future use, PnC) */
377 u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */
378};
379#endif
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300380
381struct mvneta_tx_queue {
382 /* Number of this TX queue, in the range 0-7 */
383 u8 id;
384
385 /* Number of TX DMA descriptors in the descriptor ring */
386 int size;
387
388 /* Number of currently used TX DMA descriptor in the
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100389 * descriptor ring
390 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300391 int count;
392
393 /* Array of transmitted skb */
394 struct sk_buff **tx_skb;
395
396 /* Index of last TX DMA descriptor that was inserted */
397 int txq_put_index;
398
399 /* Index of the TX DMA descriptor to be cleaned up */
400 int txq_get_index;
401
402 u32 done_pkts_coal;
403
404 /* Virtual address of the TX DMA descriptors array */
405 struct mvneta_tx_desc *descs;
406
407 /* DMA address of the TX DMA descriptors array */
408 dma_addr_t descs_phys;
409
410 /* Index of the last TX DMA descriptor */
411 int last_desc;
412
413 /* Index of the next TX DMA descriptor to process */
414 int next_desc_to_proc;
415};
416
417struct mvneta_rx_queue {
418 /* rx queue number, in the range 0-7 */
419 u8 id;
420
421 /* num of rx descriptors in the rx descriptor ring */
422 int size;
423
424 /* counter of times when mvneta_refill() failed */
425 int missed;
426
427 u32 pkts_coal;
428 u32 time_coal;
429
430 /* Virtual address of the RX DMA descriptors array */
431 struct mvneta_rx_desc *descs;
432
433 /* DMA address of the RX DMA descriptors array */
434 dma_addr_t descs_phys;
435
436 /* Index of the last RX DMA descriptor */
437 int last_desc;
438
439 /* Index of the next RX DMA descriptor to process */
440 int next_desc_to_proc;
441};
442
443static int rxq_number = 8;
444static int txq_number = 8;
445
446static int rxq_def;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300447
willy tarreauf19fadf2014-01-16 08:20:17 +0100448static int rx_copybreak __read_mostly = 256;
449
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300450#define MVNETA_DRIVER_NAME "mvneta"
451#define MVNETA_DRIVER_VERSION "1.0"
452
453/* Utility/helper methods */
454
455/* Write helper method */
456static void mvreg_write(struct mvneta_port *pp, u32 offset, u32 data)
457{
458 writel(data, pp->base + offset);
459}
460
461/* Read helper method */
462static u32 mvreg_read(struct mvneta_port *pp, u32 offset)
463{
464 return readl(pp->base + offset);
465}
466
467/* Increment txq get counter */
468static void mvneta_txq_inc_get(struct mvneta_tx_queue *txq)
469{
470 txq->txq_get_index++;
471 if (txq->txq_get_index == txq->size)
472 txq->txq_get_index = 0;
473}
474
475/* Increment txq put counter */
476static void mvneta_txq_inc_put(struct mvneta_tx_queue *txq)
477{
478 txq->txq_put_index++;
479 if (txq->txq_put_index == txq->size)
480 txq->txq_put_index = 0;
481}
482
483
484/* Clear all MIB counters */
485static void mvneta_mib_counters_clear(struct mvneta_port *pp)
486{
487 int i;
488 u32 dummy;
489
490 /* Perform dummy reads from MIB counters */
491 for (i = 0; i < MVNETA_MIB_LATE_COLLISION; i += 4)
492 dummy = mvreg_read(pp, (MVNETA_MIB_COUNTERS_BASE + i));
493}
494
495/* Get System Network Statistics */
496struct rtnl_link_stats64 *mvneta_get_stats64(struct net_device *dev,
497 struct rtnl_link_stats64 *stats)
498{
499 struct mvneta_port *pp = netdev_priv(dev);
500 unsigned int start;
willy tarreau74c41b02014-01-16 08:20:08 +0100501 int cpu;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300502
willy tarreau74c41b02014-01-16 08:20:08 +0100503 for_each_possible_cpu(cpu) {
504 struct mvneta_pcpu_stats *cpu_stats;
505 u64 rx_packets;
506 u64 rx_bytes;
507 u64 tx_packets;
508 u64 tx_bytes;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300509
willy tarreau74c41b02014-01-16 08:20:08 +0100510 cpu_stats = per_cpu_ptr(pp->stats, cpu);
511 do {
512 start = u64_stats_fetch_begin_bh(&cpu_stats->syncp);
513 rx_packets = cpu_stats->rx_packets;
514 rx_bytes = cpu_stats->rx_bytes;
515 tx_packets = cpu_stats->tx_packets;
516 tx_bytes = cpu_stats->tx_bytes;
517 } while (u64_stats_fetch_retry_bh(&cpu_stats->syncp, start));
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300518
willy tarreau74c41b02014-01-16 08:20:08 +0100519 stats->rx_packets += rx_packets;
520 stats->rx_bytes += rx_bytes;
521 stats->tx_packets += tx_packets;
522 stats->tx_bytes += tx_bytes;
523 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300524
525 stats->rx_errors = dev->stats.rx_errors;
526 stats->rx_dropped = dev->stats.rx_dropped;
527
528 stats->tx_dropped = dev->stats.tx_dropped;
529
530 return stats;
531}
532
533/* Rx descriptors helper methods */
534
willy tarreau54282132014-01-16 08:20:14 +0100535/* Checks whether the RX descriptor having this status is both the first
536 * and the last descriptor for the RX packet. Each RX packet is currently
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300537 * received through a single RX descriptor, so not having each RX
538 * descriptor with its first and last bits set is an error
539 */
willy tarreau54282132014-01-16 08:20:14 +0100540static int mvneta_rxq_desc_is_first_last(u32 status)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300541{
willy tarreau54282132014-01-16 08:20:14 +0100542 return (status & MVNETA_RXD_FIRST_LAST_DESC) ==
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300543 MVNETA_RXD_FIRST_LAST_DESC;
544}
545
546/* Add number of descriptors ready to receive new packets */
547static void mvneta_rxq_non_occup_desc_add(struct mvneta_port *pp,
548 struct mvneta_rx_queue *rxq,
549 int ndescs)
550{
551 /* Only MVNETA_RXQ_ADD_NON_OCCUPIED_MAX (255) descriptors can
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100552 * be added at once
553 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300554 while (ndescs > MVNETA_RXQ_ADD_NON_OCCUPIED_MAX) {
555 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
556 (MVNETA_RXQ_ADD_NON_OCCUPIED_MAX <<
557 MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
558 ndescs -= MVNETA_RXQ_ADD_NON_OCCUPIED_MAX;
559 }
560
561 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
562 (ndescs << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
563}
564
565/* Get number of RX descriptors occupied by received packets */
566static int mvneta_rxq_busy_desc_num_get(struct mvneta_port *pp,
567 struct mvneta_rx_queue *rxq)
568{
569 u32 val;
570
571 val = mvreg_read(pp, MVNETA_RXQ_STATUS_REG(rxq->id));
572 return val & MVNETA_RXQ_OCCUPIED_ALL_MASK;
573}
574
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100575/* Update num of rx desc called upon return from rx path or
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300576 * from mvneta_rxq_drop_pkts().
577 */
578static void mvneta_rxq_desc_num_update(struct mvneta_port *pp,
579 struct mvneta_rx_queue *rxq,
580 int rx_done, int rx_filled)
581{
582 u32 val;
583
584 if ((rx_done <= 0xff) && (rx_filled <= 0xff)) {
585 val = rx_done |
586 (rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT);
587 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
588 return;
589 }
590
591 /* Only 255 descriptors can be added at once */
592 while ((rx_done > 0) || (rx_filled > 0)) {
593 if (rx_done <= 0xff) {
594 val = rx_done;
595 rx_done = 0;
596 } else {
597 val = 0xff;
598 rx_done -= 0xff;
599 }
600 if (rx_filled <= 0xff) {
601 val |= rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
602 rx_filled = 0;
603 } else {
604 val |= 0xff << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
605 rx_filled -= 0xff;
606 }
607 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
608 }
609}
610
611/* Get pointer to next RX descriptor to be processed by SW */
612static struct mvneta_rx_desc *
613mvneta_rxq_next_desc_get(struct mvneta_rx_queue *rxq)
614{
615 int rx_desc = rxq->next_desc_to_proc;
616
617 rxq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(rxq, rx_desc);
willy tarreau34e41792014-01-16 08:20:15 +0100618 prefetch(rxq->descs + rxq->next_desc_to_proc);
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300619 return rxq->descs + rx_desc;
620}
621
622/* Change maximum receive size of the port. */
623static void mvneta_max_rx_size_set(struct mvneta_port *pp, int max_rx_size)
624{
625 u32 val;
626
627 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
628 val &= ~MVNETA_GMAC_MAX_RX_SIZE_MASK;
629 val |= ((max_rx_size - MVNETA_MH_SIZE) / 2) <<
630 MVNETA_GMAC_MAX_RX_SIZE_SHIFT;
631 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
632}
633
634
635/* Set rx queue offset */
636static void mvneta_rxq_offset_set(struct mvneta_port *pp,
637 struct mvneta_rx_queue *rxq,
638 int offset)
639{
640 u32 val;
641
642 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
643 val &= ~MVNETA_RXQ_PKT_OFFSET_ALL_MASK;
644
645 /* Offset is in */
646 val |= MVNETA_RXQ_PKT_OFFSET_MASK(offset >> 3);
647 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
648}
649
650
651/* Tx descriptors helper methods */
652
653/* Update HW with number of TX descriptors to be sent */
654static void mvneta_txq_pend_desc_add(struct mvneta_port *pp,
655 struct mvneta_tx_queue *txq,
656 int pend_desc)
657{
658 u32 val;
659
660 /* Only 255 descriptors can be added at once ; Assume caller
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100661 * process TX desriptors in quanta less than 256
662 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300663 val = pend_desc;
664 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
665}
666
667/* Get pointer to next TX descriptor to be processed (send) by HW */
668static struct mvneta_tx_desc *
669mvneta_txq_next_desc_get(struct mvneta_tx_queue *txq)
670{
671 int tx_desc = txq->next_desc_to_proc;
672
673 txq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(txq, tx_desc);
674 return txq->descs + tx_desc;
675}
676
677/* Release the last allocated TX descriptor. Useful to handle DMA
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100678 * mapping failures in the TX path.
679 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300680static void mvneta_txq_desc_put(struct mvneta_tx_queue *txq)
681{
682 if (txq->next_desc_to_proc == 0)
683 txq->next_desc_to_proc = txq->last_desc - 1;
684 else
685 txq->next_desc_to_proc--;
686}
687
688/* Set rxq buf size */
689static void mvneta_rxq_buf_size_set(struct mvneta_port *pp,
690 struct mvneta_rx_queue *rxq,
691 int buf_size)
692{
693 u32 val;
694
695 val = mvreg_read(pp, MVNETA_RXQ_SIZE_REG(rxq->id));
696
697 val &= ~MVNETA_RXQ_BUF_SIZE_MASK;
698 val |= ((buf_size >> 3) << MVNETA_RXQ_BUF_SIZE_SHIFT);
699
700 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), val);
701}
702
703/* Disable buffer management (BM) */
704static void mvneta_rxq_bm_disable(struct mvneta_port *pp,
705 struct mvneta_rx_queue *rxq)
706{
707 u32 val;
708
709 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
710 val &= ~MVNETA_RXQ_HW_BUF_ALLOC;
711 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
712}
713
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300714/* Start the Ethernet port RX and TX activity */
715static void mvneta_port_up(struct mvneta_port *pp)
716{
717 int queue;
718 u32 q_map;
719
720 /* Enable all initialized TXs. */
721 mvneta_mib_counters_clear(pp);
722 q_map = 0;
723 for (queue = 0; queue < txq_number; queue++) {
724 struct mvneta_tx_queue *txq = &pp->txqs[queue];
725 if (txq->descs != NULL)
726 q_map |= (1 << queue);
727 }
728 mvreg_write(pp, MVNETA_TXQ_CMD, q_map);
729
730 /* Enable all initialized RXQs. */
731 q_map = 0;
732 for (queue = 0; queue < rxq_number; queue++) {
733 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
734 if (rxq->descs != NULL)
735 q_map |= (1 << queue);
736 }
737
738 mvreg_write(pp, MVNETA_RXQ_CMD, q_map);
739}
740
741/* Stop the Ethernet port activity */
742static void mvneta_port_down(struct mvneta_port *pp)
743{
744 u32 val;
745 int count;
746
747 /* Stop Rx port activity. Check port Rx activity. */
748 val = mvreg_read(pp, MVNETA_RXQ_CMD) & MVNETA_RXQ_ENABLE_MASK;
749
750 /* Issue stop command for active channels only */
751 if (val != 0)
752 mvreg_write(pp, MVNETA_RXQ_CMD,
753 val << MVNETA_RXQ_DISABLE_SHIFT);
754
755 /* Wait for all Rx activity to terminate. */
756 count = 0;
757 do {
758 if (count++ >= MVNETA_RX_DISABLE_TIMEOUT_MSEC) {
759 netdev_warn(pp->dev,
760 "TIMEOUT for RX stopped ! rx_queue_cmd: 0x08%x\n",
761 val);
762 break;
763 }
764 mdelay(1);
765
766 val = mvreg_read(pp, MVNETA_RXQ_CMD);
767 } while (val & 0xff);
768
769 /* Stop Tx port activity. Check port Tx activity. Issue stop
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100770 * command for active channels only
771 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300772 val = (mvreg_read(pp, MVNETA_TXQ_CMD)) & MVNETA_TXQ_ENABLE_MASK;
773
774 if (val != 0)
775 mvreg_write(pp, MVNETA_TXQ_CMD,
776 (val << MVNETA_TXQ_DISABLE_SHIFT));
777
778 /* Wait for all Tx activity to terminate. */
779 count = 0;
780 do {
781 if (count++ >= MVNETA_TX_DISABLE_TIMEOUT_MSEC) {
782 netdev_warn(pp->dev,
783 "TIMEOUT for TX stopped status=0x%08x\n",
784 val);
785 break;
786 }
787 mdelay(1);
788
789 /* Check TX Command reg that all Txqs are stopped */
790 val = mvreg_read(pp, MVNETA_TXQ_CMD);
791
792 } while (val & 0xff);
793
794 /* Double check to verify that TX FIFO is empty */
795 count = 0;
796 do {
797 if (count++ >= MVNETA_TX_FIFO_EMPTY_TIMEOUT) {
798 netdev_warn(pp->dev,
799 "TX FIFO empty timeout status=0x08%x\n",
800 val);
801 break;
802 }
803 mdelay(1);
804
805 val = mvreg_read(pp, MVNETA_PORT_STATUS);
806 } while (!(val & MVNETA_TX_FIFO_EMPTY) &&
807 (val & MVNETA_TX_IN_PRGRS));
808
809 udelay(200);
810}
811
812/* Enable the port by setting the port enable bit of the MAC control register */
813static void mvneta_port_enable(struct mvneta_port *pp)
814{
815 u32 val;
816
817 /* Enable port */
818 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
819 val |= MVNETA_GMAC0_PORT_ENABLE;
820 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
821}
822
823/* Disable the port and wait for about 200 usec before retuning */
824static void mvneta_port_disable(struct mvneta_port *pp)
825{
826 u32 val;
827
828 /* Reset the Enable bit in the Serial Control Register */
829 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
830 val &= ~MVNETA_GMAC0_PORT_ENABLE;
831 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
832
833 udelay(200);
834}
835
836/* Multicast tables methods */
837
838/* Set all entries in Unicast MAC Table; queue==-1 means reject all */
839static void mvneta_set_ucast_table(struct mvneta_port *pp, int queue)
840{
841 int offset;
842 u32 val;
843
844 if (queue == -1) {
845 val = 0;
846 } else {
847 val = 0x1 | (queue << 1);
848 val |= (val << 24) | (val << 16) | (val << 8);
849 }
850
851 for (offset = 0; offset <= 0xc; offset += 4)
852 mvreg_write(pp, MVNETA_DA_FILT_UCAST_BASE + offset, val);
853}
854
855/* Set all entries in Special Multicast MAC Table; queue==-1 means reject all */
856static void mvneta_set_special_mcast_table(struct mvneta_port *pp, int queue)
857{
858 int offset;
859 u32 val;
860
861 if (queue == -1) {
862 val = 0;
863 } else {
864 val = 0x1 | (queue << 1);
865 val |= (val << 24) | (val << 16) | (val << 8);
866 }
867
868 for (offset = 0; offset <= 0xfc; offset += 4)
869 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + offset, val);
870
871}
872
873/* Set all entries in Other Multicast MAC Table. queue==-1 means reject all */
874static void mvneta_set_other_mcast_table(struct mvneta_port *pp, int queue)
875{
876 int offset;
877 u32 val;
878
879 if (queue == -1) {
880 memset(pp->mcast_count, 0, sizeof(pp->mcast_count));
881 val = 0;
882 } else {
883 memset(pp->mcast_count, 1, sizeof(pp->mcast_count));
884 val = 0x1 | (queue << 1);
885 val |= (val << 24) | (val << 16) | (val << 8);
886 }
887
888 for (offset = 0; offset <= 0xfc; offset += 4)
889 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + offset, val);
890}
891
892/* This method sets defaults to the NETA port:
893 * Clears interrupt Cause and Mask registers.
894 * Clears all MAC tables.
895 * Sets defaults to all registers.
896 * Resets RX and TX descriptor rings.
897 * Resets PHY.
898 * This method can be called after mvneta_port_down() to return the port
899 * settings to defaults.
900 */
901static void mvneta_defaults_set(struct mvneta_port *pp)
902{
903 int cpu;
904 int queue;
905 u32 val;
906
907 /* Clear all Cause registers */
908 mvreg_write(pp, MVNETA_INTR_NEW_CAUSE, 0);
909 mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0);
910 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
911
912 /* Mask all interrupts */
913 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
914 mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0);
915 mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0);
916 mvreg_write(pp, MVNETA_INTR_ENABLE, 0);
917
918 /* Enable MBUS Retry bit16 */
919 mvreg_write(pp, MVNETA_MBUS_RETRY, 0x20);
920
921 /* Set CPU queue access map - all CPUs have access to all RX
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100922 * queues and to all TX queues
923 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300924 for (cpu = 0; cpu < CONFIG_NR_CPUS; cpu++)
925 mvreg_write(pp, MVNETA_CPU_MAP(cpu),
926 (MVNETA_CPU_RXQ_ACCESS_ALL_MASK |
927 MVNETA_CPU_TXQ_ACCESS_ALL_MASK));
928
929 /* Reset RX and TX DMAs */
930 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
931 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
932
933 /* Disable Legacy WRR, Disable EJP, Release from reset */
934 mvreg_write(pp, MVNETA_TXQ_CMD_1, 0);
935 for (queue = 0; queue < txq_number; queue++) {
936 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(queue), 0);
937 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(queue), 0);
938 }
939
940 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
941 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
942
943 /* Set Port Acceleration Mode */
944 val = MVNETA_ACC_MODE_EXT;
945 mvreg_write(pp, MVNETA_ACC_MODE, val);
946
947 /* Update val of portCfg register accordingly with all RxQueue types */
948 val = MVNETA_PORT_CONFIG_DEFL_VALUE(rxq_def);
949 mvreg_write(pp, MVNETA_PORT_CONFIG, val);
950
951 val = 0;
952 mvreg_write(pp, MVNETA_PORT_CONFIG_EXTEND, val);
953 mvreg_write(pp, MVNETA_RX_MIN_FRAME_SIZE, 64);
954
955 /* Build PORT_SDMA_CONFIG_REG */
956 val = 0;
957
958 /* Default burst size */
959 val |= MVNETA_TX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
960 val |= MVNETA_RX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +0200961 val |= MVNETA_RX_NO_DATA_SWAP | MVNETA_TX_NO_DATA_SWAP;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300962
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +0200963#if defined(__BIG_ENDIAN)
964 val |= MVNETA_DESC_SWAP;
965#endif
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300966
967 /* Assign port SDMA configuration */
968 mvreg_write(pp, MVNETA_SDMA_CONFIG, val);
969
Thomas Petazzoni71408602013-09-04 16:21:18 +0200970 /* Disable PHY polling in hardware, since we're using the
971 * kernel phylib to do this.
972 */
973 val = mvreg_read(pp, MVNETA_UNIT_CONTROL);
974 val &= ~MVNETA_PHY_POLLING_ENABLE;
975 mvreg_write(pp, MVNETA_UNIT_CONTROL, val);
976
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300977 mvneta_set_ucast_table(pp, -1);
978 mvneta_set_special_mcast_table(pp, -1);
979 mvneta_set_other_mcast_table(pp, -1);
980
981 /* Set port interrupt enable register - default enable all */
982 mvreg_write(pp, MVNETA_INTR_ENABLE,
983 (MVNETA_RXQ_INTR_ENABLE_ALL_MASK
984 | MVNETA_TXQ_INTR_ENABLE_ALL_MASK));
985}
986
987/* Set max sizes for tx queues */
988static void mvneta_txq_max_tx_size_set(struct mvneta_port *pp, int max_tx_size)
989
990{
991 u32 val, size, mtu;
992 int queue;
993
994 mtu = max_tx_size * 8;
995 if (mtu > MVNETA_TX_MTU_MAX)
996 mtu = MVNETA_TX_MTU_MAX;
997
998 /* Set MTU */
999 val = mvreg_read(pp, MVNETA_TX_MTU);
1000 val &= ~MVNETA_TX_MTU_MAX;
1001 val |= mtu;
1002 mvreg_write(pp, MVNETA_TX_MTU, val);
1003
1004 /* TX token size and all TXQs token size must be larger that MTU */
1005 val = mvreg_read(pp, MVNETA_TX_TOKEN_SIZE);
1006
1007 size = val & MVNETA_TX_TOKEN_SIZE_MAX;
1008 if (size < mtu) {
1009 size = mtu;
1010 val &= ~MVNETA_TX_TOKEN_SIZE_MAX;
1011 val |= size;
1012 mvreg_write(pp, MVNETA_TX_TOKEN_SIZE, val);
1013 }
1014 for (queue = 0; queue < txq_number; queue++) {
1015 val = mvreg_read(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue));
1016
1017 size = val & MVNETA_TXQ_TOKEN_SIZE_MAX;
1018 if (size < mtu) {
1019 size = mtu;
1020 val &= ~MVNETA_TXQ_TOKEN_SIZE_MAX;
1021 val |= size;
1022 mvreg_write(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue), val);
1023 }
1024 }
1025}
1026
1027/* Set unicast address */
1028static void mvneta_set_ucast_addr(struct mvneta_port *pp, u8 last_nibble,
1029 int queue)
1030{
1031 unsigned int unicast_reg;
1032 unsigned int tbl_offset;
1033 unsigned int reg_offset;
1034
1035 /* Locate the Unicast table entry */
1036 last_nibble = (0xf & last_nibble);
1037
1038 /* offset from unicast tbl base */
1039 tbl_offset = (last_nibble / 4) * 4;
1040
1041 /* offset within the above reg */
1042 reg_offset = last_nibble % 4;
1043
1044 unicast_reg = mvreg_read(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset));
1045
1046 if (queue == -1) {
1047 /* Clear accepts frame bit at specified unicast DA tbl entry */
1048 unicast_reg &= ~(0xff << (8 * reg_offset));
1049 } else {
1050 unicast_reg &= ~(0xff << (8 * reg_offset));
1051 unicast_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
1052 }
1053
1054 mvreg_write(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset), unicast_reg);
1055}
1056
1057/* Set mac address */
1058static void mvneta_mac_addr_set(struct mvneta_port *pp, unsigned char *addr,
1059 int queue)
1060{
1061 unsigned int mac_h;
1062 unsigned int mac_l;
1063
1064 if (queue != -1) {
1065 mac_l = (addr[4] << 8) | (addr[5]);
1066 mac_h = (addr[0] << 24) | (addr[1] << 16) |
1067 (addr[2] << 8) | (addr[3] << 0);
1068
1069 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, mac_l);
1070 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, mac_h);
1071 }
1072
1073 /* Accept frames of this address */
1074 mvneta_set_ucast_addr(pp, addr[5], queue);
1075}
1076
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001077/* Set the number of packets that will be received before RX interrupt
1078 * will be generated by HW.
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001079 */
1080static void mvneta_rx_pkts_coal_set(struct mvneta_port *pp,
1081 struct mvneta_rx_queue *rxq, u32 value)
1082{
1083 mvreg_write(pp, MVNETA_RXQ_THRESHOLD_REG(rxq->id),
1084 value | MVNETA_RXQ_NON_OCCUPIED(0));
1085 rxq->pkts_coal = value;
1086}
1087
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001088/* Set the time delay in usec before RX interrupt will be generated by
1089 * HW.
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001090 */
1091static void mvneta_rx_time_coal_set(struct mvneta_port *pp,
1092 struct mvneta_rx_queue *rxq, u32 value)
1093{
Thomas Petazzoni189dd622012-11-19 14:15:25 +01001094 u32 val;
1095 unsigned long clk_rate;
1096
1097 clk_rate = clk_get_rate(pp->clk);
1098 val = (clk_rate / 1000000) * value;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001099
1100 mvreg_write(pp, MVNETA_RXQ_TIME_COAL_REG(rxq->id), val);
1101 rxq->time_coal = value;
1102}
1103
1104/* Set threshold for TX_DONE pkts coalescing */
1105static void mvneta_tx_done_pkts_coal_set(struct mvneta_port *pp,
1106 struct mvneta_tx_queue *txq, u32 value)
1107{
1108 u32 val;
1109
1110 val = mvreg_read(pp, MVNETA_TXQ_SIZE_REG(txq->id));
1111
1112 val &= ~MVNETA_TXQ_SENT_THRESH_ALL_MASK;
1113 val |= MVNETA_TXQ_SENT_THRESH_MASK(value);
1114
1115 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), val);
1116
1117 txq->done_pkts_coal = value;
1118}
1119
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001120/* Handle rx descriptor fill by setting buf_cookie and buf_phys_addr */
1121static void mvneta_rx_desc_fill(struct mvneta_rx_desc *rx_desc,
1122 u32 phys_addr, u32 cookie)
1123{
1124 rx_desc->buf_cookie = cookie;
1125 rx_desc->buf_phys_addr = phys_addr;
1126}
1127
1128/* Decrement sent descriptors counter */
1129static void mvneta_txq_sent_desc_dec(struct mvneta_port *pp,
1130 struct mvneta_tx_queue *txq,
1131 int sent_desc)
1132{
1133 u32 val;
1134
1135 /* Only 255 TX descriptors can be updated at once */
1136 while (sent_desc > 0xff) {
1137 val = 0xff << MVNETA_TXQ_DEC_SENT_SHIFT;
1138 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1139 sent_desc = sent_desc - 0xff;
1140 }
1141
1142 val = sent_desc << MVNETA_TXQ_DEC_SENT_SHIFT;
1143 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1144}
1145
1146/* Get number of TX descriptors already sent by HW */
1147static int mvneta_txq_sent_desc_num_get(struct mvneta_port *pp,
1148 struct mvneta_tx_queue *txq)
1149{
1150 u32 val;
1151 int sent_desc;
1152
1153 val = mvreg_read(pp, MVNETA_TXQ_STATUS_REG(txq->id));
1154 sent_desc = (val & MVNETA_TXQ_SENT_DESC_MASK) >>
1155 MVNETA_TXQ_SENT_DESC_SHIFT;
1156
1157 return sent_desc;
1158}
1159
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001160/* Get number of sent descriptors and decrement counter.
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001161 * The number of sent descriptors is returned.
1162 */
1163static int mvneta_txq_sent_desc_proc(struct mvneta_port *pp,
1164 struct mvneta_tx_queue *txq)
1165{
1166 int sent_desc;
1167
1168 /* Get number of sent descriptors */
1169 sent_desc = mvneta_txq_sent_desc_num_get(pp, txq);
1170
1171 /* Decrement sent descriptors counter */
1172 if (sent_desc)
1173 mvneta_txq_sent_desc_dec(pp, txq, sent_desc);
1174
1175 return sent_desc;
1176}
1177
1178/* Set TXQ descriptors fields relevant for CSUM calculation */
1179static u32 mvneta_txq_desc_csum(int l3_offs, int l3_proto,
1180 int ip_hdr_len, int l4_proto)
1181{
1182 u32 command;
1183
1184 /* Fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001185 * G_L4_chk, L4_type; required only for checksum
1186 * calculation
1187 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001188 command = l3_offs << MVNETA_TX_L3_OFF_SHIFT;
1189 command |= ip_hdr_len << MVNETA_TX_IP_HLEN_SHIFT;
1190
1191 if (l3_proto == swab16(ETH_P_IP))
1192 command |= MVNETA_TXD_IP_CSUM;
1193 else
1194 command |= MVNETA_TX_L3_IP6;
1195
1196 if (l4_proto == IPPROTO_TCP)
1197 command |= MVNETA_TX_L4_CSUM_FULL;
1198 else if (l4_proto == IPPROTO_UDP)
1199 command |= MVNETA_TX_L4_UDP | MVNETA_TX_L4_CSUM_FULL;
1200 else
1201 command |= MVNETA_TX_L4_CSUM_NOT;
1202
1203 return command;
1204}
1205
1206
1207/* Display more error info */
1208static void mvneta_rx_error(struct mvneta_port *pp,
1209 struct mvneta_rx_desc *rx_desc)
1210{
1211 u32 status = rx_desc->status;
1212
willy tarreau54282132014-01-16 08:20:14 +01001213 if (!mvneta_rxq_desc_is_first_last(status)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001214 netdev_err(pp->dev,
1215 "bad rx status %08x (buffer oversize), size=%d\n",
willy tarreau54282132014-01-16 08:20:14 +01001216 status, rx_desc->data_size);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001217 return;
1218 }
1219
1220 switch (status & MVNETA_RXD_ERR_CODE_MASK) {
1221 case MVNETA_RXD_ERR_CRC:
1222 netdev_err(pp->dev, "bad rx status %08x (crc error), size=%d\n",
1223 status, rx_desc->data_size);
1224 break;
1225 case MVNETA_RXD_ERR_OVERRUN:
1226 netdev_err(pp->dev, "bad rx status %08x (overrun error), size=%d\n",
1227 status, rx_desc->data_size);
1228 break;
1229 case MVNETA_RXD_ERR_LEN:
1230 netdev_err(pp->dev, "bad rx status %08x (max frame length error), size=%d\n",
1231 status, rx_desc->data_size);
1232 break;
1233 case MVNETA_RXD_ERR_RESOURCE:
1234 netdev_err(pp->dev, "bad rx status %08x (resource error), size=%d\n",
1235 status, rx_desc->data_size);
1236 break;
1237 }
1238}
1239
willy tarreau54282132014-01-16 08:20:14 +01001240/* Handle RX checksum offload based on the descriptor's status */
1241static void mvneta_rx_csum(struct mvneta_port *pp, u32 status,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001242 struct sk_buff *skb)
1243{
willy tarreau54282132014-01-16 08:20:14 +01001244 if ((status & MVNETA_RXD_L3_IP4) &&
1245 (status & MVNETA_RXD_L4_CSUM_OK)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001246 skb->csum = 0;
1247 skb->ip_summed = CHECKSUM_UNNECESSARY;
1248 return;
1249 }
1250
1251 skb->ip_summed = CHECKSUM_NONE;
1252}
1253
willy tarreau6c498972014-01-16 08:20:12 +01001254/* Return tx queue pointer (find last set bit) according to <cause> returned
1255 * form tx_done reg. <cause> must not be null. The return value is always a
1256 * valid queue for matching the first one found in <cause>.
1257 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001258static struct mvneta_tx_queue *mvneta_tx_done_policy(struct mvneta_port *pp,
1259 u32 cause)
1260{
1261 int queue = fls(cause) - 1;
1262
willy tarreau6c498972014-01-16 08:20:12 +01001263 return &pp->txqs[queue];
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001264}
1265
1266/* Free tx queue skbuffs */
1267static void mvneta_txq_bufs_free(struct mvneta_port *pp,
1268 struct mvneta_tx_queue *txq, int num)
1269{
1270 int i;
1271
1272 for (i = 0; i < num; i++) {
1273 struct mvneta_tx_desc *tx_desc = txq->descs +
1274 txq->txq_get_index;
1275 struct sk_buff *skb = txq->tx_skb[txq->txq_get_index];
1276
1277 mvneta_txq_inc_get(txq);
1278
1279 if (!skb)
1280 continue;
1281
1282 dma_unmap_single(pp->dev->dev.parent, tx_desc->buf_phys_addr,
1283 tx_desc->data_size, DMA_TO_DEVICE);
1284 dev_kfree_skb_any(skb);
1285 }
1286}
1287
1288/* Handle end of transmission */
Arnaud Ebalardcd713192014-01-16 08:20:19 +01001289static void mvneta_txq_done(struct mvneta_port *pp,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001290 struct mvneta_tx_queue *txq)
1291{
1292 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
1293 int tx_done;
1294
1295 tx_done = mvneta_txq_sent_desc_proc(pp, txq);
Arnaud Ebalardcd713192014-01-16 08:20:19 +01001296 if (!tx_done)
1297 return;
1298
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001299 mvneta_txq_bufs_free(pp, txq, tx_done);
1300
1301 txq->count -= tx_done;
1302
1303 if (netif_tx_queue_stopped(nq)) {
1304 if (txq->size - txq->count >= MAX_SKB_FRAGS + 1)
1305 netif_tx_wake_queue(nq);
1306 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001307}
1308
willy tarreau8ec2cd42014-01-16 08:20:16 +01001309static void *mvneta_frag_alloc(const struct mvneta_port *pp)
1310{
1311 if (likely(pp->frag_size <= PAGE_SIZE))
1312 return netdev_alloc_frag(pp->frag_size);
1313 else
1314 return kmalloc(pp->frag_size, GFP_ATOMIC);
1315}
1316
1317static void mvneta_frag_free(const struct mvneta_port *pp, void *data)
1318{
1319 if (likely(pp->frag_size <= PAGE_SIZE))
1320 put_page(virt_to_head_page(data));
1321 else
1322 kfree(data);
1323}
1324
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001325/* Refill processing */
1326static int mvneta_rx_refill(struct mvneta_port *pp,
1327 struct mvneta_rx_desc *rx_desc)
1328
1329{
1330 dma_addr_t phys_addr;
willy tarreau8ec2cd42014-01-16 08:20:16 +01001331 void *data;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001332
willy tarreau8ec2cd42014-01-16 08:20:16 +01001333 data = mvneta_frag_alloc(pp);
1334 if (!data)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001335 return -ENOMEM;
1336
willy tarreau8ec2cd42014-01-16 08:20:16 +01001337 phys_addr = dma_map_single(pp->dev->dev.parent, data,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001338 MVNETA_RX_BUF_SIZE(pp->pkt_size),
1339 DMA_FROM_DEVICE);
1340 if (unlikely(dma_mapping_error(pp->dev->dev.parent, phys_addr))) {
willy tarreau8ec2cd42014-01-16 08:20:16 +01001341 mvneta_frag_free(pp, data);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001342 return -ENOMEM;
1343 }
1344
willy tarreau8ec2cd42014-01-16 08:20:16 +01001345 mvneta_rx_desc_fill(rx_desc, phys_addr, (u32)data);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001346 return 0;
1347}
1348
1349/* Handle tx checksum */
1350static u32 mvneta_skb_tx_csum(struct mvneta_port *pp, struct sk_buff *skb)
1351{
1352 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1353 int ip_hdr_len = 0;
1354 u8 l4_proto;
1355
1356 if (skb->protocol == htons(ETH_P_IP)) {
1357 struct iphdr *ip4h = ip_hdr(skb);
1358
1359 /* Calculate IPv4 checksum and L4 checksum */
1360 ip_hdr_len = ip4h->ihl;
1361 l4_proto = ip4h->protocol;
1362 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1363 struct ipv6hdr *ip6h = ipv6_hdr(skb);
1364
1365 /* Read l4_protocol from one of IPv6 extra headers */
1366 if (skb_network_header_len(skb) > 0)
1367 ip_hdr_len = (skb_network_header_len(skb) >> 2);
1368 l4_proto = ip6h->nexthdr;
1369 } else
1370 return MVNETA_TX_L4_CSUM_NOT;
1371
1372 return mvneta_txq_desc_csum(skb_network_offset(skb),
1373 skb->protocol, ip_hdr_len, l4_proto);
1374 }
1375
1376 return MVNETA_TX_L4_CSUM_NOT;
1377}
1378
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001379/* Returns rx queue pointer (find last set bit) according to causeRxTx
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001380 * value
1381 */
1382static struct mvneta_rx_queue *mvneta_rx_policy(struct mvneta_port *pp,
1383 u32 cause)
1384{
1385 int queue = fls(cause >> 8) - 1;
1386
1387 return (queue < 0 || queue >= rxq_number) ? NULL : &pp->rxqs[queue];
1388}
1389
1390/* Drop packets received by the RXQ and free buffers */
1391static void mvneta_rxq_drop_pkts(struct mvneta_port *pp,
1392 struct mvneta_rx_queue *rxq)
1393{
1394 int rx_done, i;
1395
1396 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
1397 for (i = 0; i < rxq->size; i++) {
1398 struct mvneta_rx_desc *rx_desc = rxq->descs + i;
willy tarreau8ec2cd42014-01-16 08:20:16 +01001399 void *data = (void *)rx_desc->buf_cookie;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001400
willy tarreau8ec2cd42014-01-16 08:20:16 +01001401 mvneta_frag_free(pp, data);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001402 dma_unmap_single(pp->dev->dev.parent, rx_desc->buf_phys_addr,
Ezequiel Garciaa328f3a2013-12-05 13:35:37 -03001403 MVNETA_RX_BUF_SIZE(pp->pkt_size), DMA_FROM_DEVICE);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001404 }
1405
1406 if (rx_done)
1407 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
1408}
1409
1410/* Main rx processing */
1411static int mvneta_rx(struct mvneta_port *pp, int rx_todo,
1412 struct mvneta_rx_queue *rxq)
1413{
1414 struct net_device *dev = pp->dev;
1415 int rx_done, rx_filled;
willy tarreaudc4277d2014-01-16 08:20:07 +01001416 u32 rcvd_pkts = 0;
1417 u32 rcvd_bytes = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001418
1419 /* Get number of received packets */
1420 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
1421
1422 if (rx_todo > rx_done)
1423 rx_todo = rx_done;
1424
1425 rx_done = 0;
1426 rx_filled = 0;
1427
1428 /* Fairness NAPI loop */
1429 while (rx_done < rx_todo) {
1430 struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
1431 struct sk_buff *skb;
willy tarreau8ec2cd42014-01-16 08:20:16 +01001432 unsigned char *data;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001433 u32 rx_status;
1434 int rx_bytes, err;
1435
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001436 rx_done++;
1437 rx_filled++;
1438 rx_status = rx_desc->status;
willy tarreauf19fadf2014-01-16 08:20:17 +01001439 rx_bytes = rx_desc->data_size - (ETH_FCS_LEN + MVNETA_MH_SIZE);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001440 data = (unsigned char *)rx_desc->buf_cookie;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001441
willy tarreau54282132014-01-16 08:20:14 +01001442 if (!mvneta_rxq_desc_is_first_last(rx_status) ||
willy tarreauf19fadf2014-01-16 08:20:17 +01001443 (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
1444 err_drop_frame:
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001445 dev->stats.rx_errors++;
1446 mvneta_rx_error(pp, rx_desc);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001447 /* leave the descriptor untouched */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001448 continue;
1449 }
1450
willy tarreauf19fadf2014-01-16 08:20:17 +01001451 if (rx_bytes <= rx_copybreak) {
1452 /* better copy a small frame and not unmap the DMA region */
1453 skb = netdev_alloc_skb_ip_align(dev, rx_bytes);
1454 if (unlikely(!skb))
1455 goto err_drop_frame;
1456
1457 dma_sync_single_range_for_cpu(dev->dev.parent,
1458 rx_desc->buf_phys_addr,
1459 MVNETA_MH_SIZE + NET_SKB_PAD,
1460 rx_bytes,
1461 DMA_FROM_DEVICE);
1462 memcpy(skb_put(skb, rx_bytes),
1463 data + MVNETA_MH_SIZE + NET_SKB_PAD,
1464 rx_bytes);
1465
1466 skb->protocol = eth_type_trans(skb, dev);
1467 mvneta_rx_csum(pp, rx_status, skb);
1468 napi_gro_receive(&pp->napi, skb);
1469
1470 rcvd_pkts++;
1471 rcvd_bytes += rx_bytes;
1472
1473 /* leave the descriptor and buffer untouched */
1474 continue;
1475 }
1476
1477 skb = build_skb(data, pp->frag_size > PAGE_SIZE ? 0 : pp->frag_size);
1478 if (!skb)
1479 goto err_drop_frame;
1480
1481 dma_unmap_single(dev->dev.parent, rx_desc->buf_phys_addr,
Ezequiel Garciaa328f3a2013-12-05 13:35:37 -03001482 MVNETA_RX_BUF_SIZE(pp->pkt_size), DMA_FROM_DEVICE);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001483
willy tarreaudc4277d2014-01-16 08:20:07 +01001484 rcvd_pkts++;
1485 rcvd_bytes += rx_bytes;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001486
1487 /* Linux processing */
willy tarreau8ec2cd42014-01-16 08:20:16 +01001488 skb_reserve(skb, MVNETA_MH_SIZE + NET_SKB_PAD);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001489 skb_put(skb, rx_bytes);
1490
1491 skb->protocol = eth_type_trans(skb, dev);
1492
willy tarreau54282132014-01-16 08:20:14 +01001493 mvneta_rx_csum(pp, rx_status, skb);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001494
1495 napi_gro_receive(&pp->napi, skb);
1496
1497 /* Refill processing */
1498 err = mvneta_rx_refill(pp, rx_desc);
1499 if (err) {
willy tarreauf19fadf2014-01-16 08:20:17 +01001500 netdev_err(dev, "Linux processing - Can't refill\n");
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001501 rxq->missed++;
1502 rx_filled--;
1503 }
1504 }
1505
willy tarreaudc4277d2014-01-16 08:20:07 +01001506 if (rcvd_pkts) {
willy tarreau74c41b02014-01-16 08:20:08 +01001507 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
1508
1509 u64_stats_update_begin(&stats->syncp);
1510 stats->rx_packets += rcvd_pkts;
1511 stats->rx_bytes += rcvd_bytes;
1512 u64_stats_update_end(&stats->syncp);
willy tarreaudc4277d2014-01-16 08:20:07 +01001513 }
1514
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001515 /* Update rxq management counters */
1516 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_filled);
1517
1518 return rx_done;
1519}
1520
1521/* Handle tx fragmentation processing */
1522static int mvneta_tx_frag_process(struct mvneta_port *pp, struct sk_buff *skb,
1523 struct mvneta_tx_queue *txq)
1524{
1525 struct mvneta_tx_desc *tx_desc;
1526 int i;
1527
1528 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1529 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1530 void *addr = page_address(frag->page.p) + frag->page_offset;
1531
1532 tx_desc = mvneta_txq_next_desc_get(txq);
1533 tx_desc->data_size = frag->size;
1534
1535 tx_desc->buf_phys_addr =
1536 dma_map_single(pp->dev->dev.parent, addr,
1537 tx_desc->data_size, DMA_TO_DEVICE);
1538
1539 if (dma_mapping_error(pp->dev->dev.parent,
1540 tx_desc->buf_phys_addr)) {
1541 mvneta_txq_desc_put(txq);
1542 goto error;
1543 }
1544
1545 if (i == (skb_shinfo(skb)->nr_frags - 1)) {
1546 /* Last descriptor */
1547 tx_desc->command = MVNETA_TXD_L_DESC | MVNETA_TXD_Z_PAD;
1548
1549 txq->tx_skb[txq->txq_put_index] = skb;
1550
1551 mvneta_txq_inc_put(txq);
1552 } else {
1553 /* Descriptor in the middle: Not First, Not Last */
1554 tx_desc->command = 0;
1555
1556 txq->tx_skb[txq->txq_put_index] = NULL;
1557 mvneta_txq_inc_put(txq);
1558 }
1559 }
1560
1561 return 0;
1562
1563error:
1564 /* Release all descriptors that were used to map fragments of
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001565 * this packet, as well as the corresponding DMA mappings
1566 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001567 for (i = i - 1; i >= 0; i--) {
1568 tx_desc = txq->descs + i;
1569 dma_unmap_single(pp->dev->dev.parent,
1570 tx_desc->buf_phys_addr,
1571 tx_desc->data_size,
1572 DMA_TO_DEVICE);
1573 mvneta_txq_desc_put(txq);
1574 }
1575
1576 return -ENOMEM;
1577}
1578
1579/* Main tx processing */
1580static int mvneta_tx(struct sk_buff *skb, struct net_device *dev)
1581{
1582 struct mvneta_port *pp = netdev_priv(dev);
Willy Tarreauee40a112013-04-11 23:00:37 +02001583 u16 txq_id = skb_get_queue_mapping(skb);
1584 struct mvneta_tx_queue *txq = &pp->txqs[txq_id];
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001585 struct mvneta_tx_desc *tx_desc;
1586 struct netdev_queue *nq;
1587 int frags = 0;
1588 u32 tx_cmd;
1589
1590 if (!netif_running(dev))
1591 goto out;
1592
1593 frags = skb_shinfo(skb)->nr_frags + 1;
Willy Tarreauee40a112013-04-11 23:00:37 +02001594 nq = netdev_get_tx_queue(dev, txq_id);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001595
1596 /* Get a descriptor for the first part of the packet */
1597 tx_desc = mvneta_txq_next_desc_get(txq);
1598
1599 tx_cmd = mvneta_skb_tx_csum(pp, skb);
1600
1601 tx_desc->data_size = skb_headlen(skb);
1602
1603 tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, skb->data,
1604 tx_desc->data_size,
1605 DMA_TO_DEVICE);
1606 if (unlikely(dma_mapping_error(dev->dev.parent,
1607 tx_desc->buf_phys_addr))) {
1608 mvneta_txq_desc_put(txq);
1609 frags = 0;
1610 goto out;
1611 }
1612
1613 if (frags == 1) {
1614 /* First and Last descriptor */
1615 tx_cmd |= MVNETA_TXD_FLZ_DESC;
1616 tx_desc->command = tx_cmd;
1617 txq->tx_skb[txq->txq_put_index] = skb;
1618 mvneta_txq_inc_put(txq);
1619 } else {
1620 /* First but not Last */
1621 tx_cmd |= MVNETA_TXD_F_DESC;
1622 txq->tx_skb[txq->txq_put_index] = NULL;
1623 mvneta_txq_inc_put(txq);
1624 tx_desc->command = tx_cmd;
1625 /* Continue with other skb fragments */
1626 if (mvneta_tx_frag_process(pp, skb, txq)) {
1627 dma_unmap_single(dev->dev.parent,
1628 tx_desc->buf_phys_addr,
1629 tx_desc->data_size,
1630 DMA_TO_DEVICE);
1631 mvneta_txq_desc_put(txq);
1632 frags = 0;
1633 goto out;
1634 }
1635 }
1636
1637 txq->count += frags;
1638 mvneta_txq_pend_desc_add(pp, txq, frags);
1639
1640 if (txq->size - txq->count < MAX_SKB_FRAGS + 1)
1641 netif_tx_stop_queue(nq);
1642
1643out:
1644 if (frags > 0) {
willy tarreau74c41b02014-01-16 08:20:08 +01001645 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001646
willy tarreau74c41b02014-01-16 08:20:08 +01001647 u64_stats_update_begin(&stats->syncp);
1648 stats->tx_packets++;
1649 stats->tx_bytes += skb->len;
1650 u64_stats_update_end(&stats->syncp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001651 } else {
1652 dev->stats.tx_dropped++;
1653 dev_kfree_skb_any(skb);
1654 }
1655
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001656 return NETDEV_TX_OK;
1657}
1658
1659
1660/* Free tx resources, when resetting a port */
1661static void mvneta_txq_done_force(struct mvneta_port *pp,
1662 struct mvneta_tx_queue *txq)
1663
1664{
1665 int tx_done = txq->count;
1666
1667 mvneta_txq_bufs_free(pp, txq, tx_done);
1668
1669 /* reset txq */
1670 txq->count = 0;
1671 txq->txq_put_index = 0;
1672 txq->txq_get_index = 0;
1673}
1674
willy tarreau6c498972014-01-16 08:20:12 +01001675/* Handle tx done - called in softirq context. The <cause_tx_done> argument
1676 * must be a valid cause according to MVNETA_TXQ_INTR_MASK_ALL.
1677 */
Arnaud Ebalard0713a862014-01-16 08:20:18 +01001678static void mvneta_tx_done_gbe(struct mvneta_port *pp, u32 cause_tx_done)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001679{
1680 struct mvneta_tx_queue *txq;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001681 struct netdev_queue *nq;
1682
willy tarreau6c498972014-01-16 08:20:12 +01001683 while (cause_tx_done) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001684 txq = mvneta_tx_done_policy(pp, cause_tx_done);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001685
1686 nq = netdev_get_tx_queue(pp->dev, txq->id);
1687 __netif_tx_lock(nq, smp_processor_id());
1688
Arnaud Ebalard0713a862014-01-16 08:20:18 +01001689 if (txq->count)
1690 mvneta_txq_done(pp, txq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001691
1692 __netif_tx_unlock(nq);
1693 cause_tx_done &= ~((1 << txq->id));
1694 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001695}
1696
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001697/* Compute crc8 of the specified address, using a unique algorithm ,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001698 * according to hw spec, different than generic crc8 algorithm
1699 */
1700static int mvneta_addr_crc(unsigned char *addr)
1701{
1702 int crc = 0;
1703 int i;
1704
1705 for (i = 0; i < ETH_ALEN; i++) {
1706 int j;
1707
1708 crc = (crc ^ addr[i]) << 8;
1709 for (j = 7; j >= 0; j--) {
1710 if (crc & (0x100 << j))
1711 crc ^= 0x107 << j;
1712 }
1713 }
1714
1715 return crc;
1716}
1717
1718/* This method controls the net device special MAC multicast support.
1719 * The Special Multicast Table for MAC addresses supports MAC of the form
1720 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
1721 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
1722 * Table entries in the DA-Filter table. This method set the Special
1723 * Multicast Table appropriate entry.
1724 */
1725static void mvneta_set_special_mcast_addr(struct mvneta_port *pp,
1726 unsigned char last_byte,
1727 int queue)
1728{
1729 unsigned int smc_table_reg;
1730 unsigned int tbl_offset;
1731 unsigned int reg_offset;
1732
1733 /* Register offset from SMC table base */
1734 tbl_offset = (last_byte / 4);
1735 /* Entry offset within the above reg */
1736 reg_offset = last_byte % 4;
1737
1738 smc_table_reg = mvreg_read(pp, (MVNETA_DA_FILT_SPEC_MCAST
1739 + tbl_offset * 4));
1740
1741 if (queue == -1)
1742 smc_table_reg &= ~(0xff << (8 * reg_offset));
1743 else {
1744 smc_table_reg &= ~(0xff << (8 * reg_offset));
1745 smc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
1746 }
1747
1748 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + tbl_offset * 4,
1749 smc_table_reg);
1750}
1751
1752/* This method controls the network device Other MAC multicast support.
1753 * The Other Multicast Table is used for multicast of another type.
1754 * A CRC-8 is used as an index to the Other Multicast Table entries
1755 * in the DA-Filter table.
1756 * The method gets the CRC-8 value from the calling routine and
1757 * sets the Other Multicast Table appropriate entry according to the
1758 * specified CRC-8 .
1759 */
1760static void mvneta_set_other_mcast_addr(struct mvneta_port *pp,
1761 unsigned char crc8,
1762 int queue)
1763{
1764 unsigned int omc_table_reg;
1765 unsigned int tbl_offset;
1766 unsigned int reg_offset;
1767
1768 tbl_offset = (crc8 / 4) * 4; /* Register offset from OMC table base */
1769 reg_offset = crc8 % 4; /* Entry offset within the above reg */
1770
1771 omc_table_reg = mvreg_read(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset);
1772
1773 if (queue == -1) {
1774 /* Clear accepts frame bit at specified Other DA table entry */
1775 omc_table_reg &= ~(0xff << (8 * reg_offset));
1776 } else {
1777 omc_table_reg &= ~(0xff << (8 * reg_offset));
1778 omc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
1779 }
1780
1781 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset, omc_table_reg);
1782}
1783
1784/* The network device supports multicast using two tables:
1785 * 1) Special Multicast Table for MAC addresses of the form
1786 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
1787 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
1788 * Table entries in the DA-Filter table.
1789 * 2) Other Multicast Table for multicast of another type. A CRC-8 value
1790 * is used as an index to the Other Multicast Table entries in the
1791 * DA-Filter table.
1792 */
1793static int mvneta_mcast_addr_set(struct mvneta_port *pp, unsigned char *p_addr,
1794 int queue)
1795{
1796 unsigned char crc_result = 0;
1797
1798 if (memcmp(p_addr, "\x01\x00\x5e\x00\x00", 5) == 0) {
1799 mvneta_set_special_mcast_addr(pp, p_addr[5], queue);
1800 return 0;
1801 }
1802
1803 crc_result = mvneta_addr_crc(p_addr);
1804 if (queue == -1) {
1805 if (pp->mcast_count[crc_result] == 0) {
1806 netdev_info(pp->dev, "No valid Mcast for crc8=0x%02x\n",
1807 crc_result);
1808 return -EINVAL;
1809 }
1810
1811 pp->mcast_count[crc_result]--;
1812 if (pp->mcast_count[crc_result] != 0) {
1813 netdev_info(pp->dev,
1814 "After delete there are %d valid Mcast for crc8=0x%02x\n",
1815 pp->mcast_count[crc_result], crc_result);
1816 return -EINVAL;
1817 }
1818 } else
1819 pp->mcast_count[crc_result]++;
1820
1821 mvneta_set_other_mcast_addr(pp, crc_result, queue);
1822
1823 return 0;
1824}
1825
1826/* Configure Fitering mode of Ethernet port */
1827static void mvneta_rx_unicast_promisc_set(struct mvneta_port *pp,
1828 int is_promisc)
1829{
1830 u32 port_cfg_reg, val;
1831
1832 port_cfg_reg = mvreg_read(pp, MVNETA_PORT_CONFIG);
1833
1834 val = mvreg_read(pp, MVNETA_TYPE_PRIO);
1835
1836 /* Set / Clear UPM bit in port configuration register */
1837 if (is_promisc) {
1838 /* Accept all Unicast addresses */
1839 port_cfg_reg |= MVNETA_UNI_PROMISC_MODE;
1840 val |= MVNETA_FORCE_UNI;
1841 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, 0xffff);
1842 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, 0xffffffff);
1843 } else {
1844 /* Reject all Unicast addresses */
1845 port_cfg_reg &= ~MVNETA_UNI_PROMISC_MODE;
1846 val &= ~MVNETA_FORCE_UNI;
1847 }
1848
1849 mvreg_write(pp, MVNETA_PORT_CONFIG, port_cfg_reg);
1850 mvreg_write(pp, MVNETA_TYPE_PRIO, val);
1851}
1852
1853/* register unicast and multicast addresses */
1854static void mvneta_set_rx_mode(struct net_device *dev)
1855{
1856 struct mvneta_port *pp = netdev_priv(dev);
1857 struct netdev_hw_addr *ha;
1858
1859 if (dev->flags & IFF_PROMISC) {
1860 /* Accept all: Multicast + Unicast */
1861 mvneta_rx_unicast_promisc_set(pp, 1);
1862 mvneta_set_ucast_table(pp, rxq_def);
1863 mvneta_set_special_mcast_table(pp, rxq_def);
1864 mvneta_set_other_mcast_table(pp, rxq_def);
1865 } else {
1866 /* Accept single Unicast */
1867 mvneta_rx_unicast_promisc_set(pp, 0);
1868 mvneta_set_ucast_table(pp, -1);
1869 mvneta_mac_addr_set(pp, dev->dev_addr, rxq_def);
1870
1871 if (dev->flags & IFF_ALLMULTI) {
1872 /* Accept all multicast */
1873 mvneta_set_special_mcast_table(pp, rxq_def);
1874 mvneta_set_other_mcast_table(pp, rxq_def);
1875 } else {
1876 /* Accept only initialized multicast */
1877 mvneta_set_special_mcast_table(pp, -1);
1878 mvneta_set_other_mcast_table(pp, -1);
1879
1880 if (!netdev_mc_empty(dev)) {
1881 netdev_for_each_mc_addr(ha, dev) {
1882 mvneta_mcast_addr_set(pp, ha->addr,
1883 rxq_def);
1884 }
1885 }
1886 }
1887 }
1888}
1889
1890/* Interrupt handling - the callback for request_irq() */
1891static irqreturn_t mvneta_isr(int irq, void *dev_id)
1892{
1893 struct mvneta_port *pp = (struct mvneta_port *)dev_id;
1894
1895 /* Mask all interrupts */
1896 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
1897
1898 napi_schedule(&pp->napi);
1899
1900 return IRQ_HANDLED;
1901}
1902
1903/* NAPI handler
1904 * Bits 0 - 7 of the causeRxTx register indicate that are transmitted
1905 * packets on the corresponding TXQ (Bit 0 is for TX queue 1).
1906 * Bits 8 -15 of the cause Rx Tx register indicate that are received
1907 * packets on the corresponding RXQ (Bit 8 is for RX queue 0).
1908 * Each CPU has its own causeRxTx register
1909 */
1910static int mvneta_poll(struct napi_struct *napi, int budget)
1911{
1912 int rx_done = 0;
1913 u32 cause_rx_tx;
1914 unsigned long flags;
1915 struct mvneta_port *pp = netdev_priv(napi->dev);
1916
1917 if (!netif_running(pp->dev)) {
1918 napi_complete(napi);
1919 return rx_done;
1920 }
1921
1922 /* Read cause register */
1923 cause_rx_tx = mvreg_read(pp, MVNETA_INTR_NEW_CAUSE) &
willy tarreau71f6d1b2014-01-16 08:20:11 +01001924 (MVNETA_RX_INTR_MASK(rxq_number) | MVNETA_TX_INTR_MASK(txq_number));
1925
1926 /* Release Tx descriptors */
1927 if (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL) {
Arnaud Ebalard0713a862014-01-16 08:20:18 +01001928 mvneta_tx_done_gbe(pp, (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL));
willy tarreau71f6d1b2014-01-16 08:20:11 +01001929 cause_rx_tx &= ~MVNETA_TX_INTR_MASK_ALL;
1930 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001931
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001932 /* For the case where the last mvneta_poll did not process all
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001933 * RX packets
1934 */
1935 cause_rx_tx |= pp->cause_rx_tx;
1936 if (rxq_number > 1) {
willy tarreau71f6d1b2014-01-16 08:20:11 +01001937 while ((cause_rx_tx & MVNETA_RX_INTR_MASK_ALL) && (budget > 0)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001938 int count;
1939 struct mvneta_rx_queue *rxq;
1940 /* get rx queue number from cause_rx_tx */
1941 rxq = mvneta_rx_policy(pp, cause_rx_tx);
1942 if (!rxq)
1943 break;
1944
1945 /* process the packet in that rx queue */
1946 count = mvneta_rx(pp, budget, rxq);
1947 rx_done += count;
1948 budget -= count;
1949 if (budget > 0) {
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001950 /* set off the rx bit of the
1951 * corresponding bit in the cause rx
1952 * tx register, so that next iteration
1953 * will find the next rx queue where
1954 * packets are received on
1955 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001956 cause_rx_tx &= ~((1 << rxq->id) << 8);
1957 }
1958 }
1959 } else {
1960 rx_done = mvneta_rx(pp, budget, &pp->rxqs[rxq_def]);
1961 budget -= rx_done;
1962 }
1963
1964 if (budget > 0) {
1965 cause_rx_tx = 0;
1966 napi_complete(napi);
1967 local_irq_save(flags);
1968 mvreg_write(pp, MVNETA_INTR_NEW_MASK,
willy tarreau71f6d1b2014-01-16 08:20:11 +01001969 MVNETA_RX_INTR_MASK(rxq_number) | MVNETA_TX_INTR_MASK(txq_number));
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001970 local_irq_restore(flags);
1971 }
1972
1973 pp->cause_rx_tx = cause_rx_tx;
1974 return rx_done;
1975}
1976
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001977/* Handle rxq fill: allocates rxq skbs; called when initializing a port */
1978static int mvneta_rxq_fill(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
1979 int num)
1980{
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001981 int i;
1982
1983 for (i = 0; i < num; i++) {
willy tarreaua1a65ab2014-01-16 08:20:13 +01001984 memset(rxq->descs + i, 0, sizeof(struct mvneta_rx_desc));
1985 if (mvneta_rx_refill(pp, rxq->descs + i) != 0) {
1986 netdev_err(pp->dev, "%s:rxq %d, %d of %d buffs filled\n",
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001987 __func__, rxq->id, i, num);
1988 break;
1989 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001990 }
1991
1992 /* Add this number of RX descriptors as non occupied (ready to
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001993 * get packets)
1994 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001995 mvneta_rxq_non_occup_desc_add(pp, rxq, i);
1996
1997 return i;
1998}
1999
2000/* Free all packets pending transmit from all TXQs and reset TX port */
2001static void mvneta_tx_reset(struct mvneta_port *pp)
2002{
2003 int queue;
2004
2005 /* free the skb's in the hal tx ring */
2006 for (queue = 0; queue < txq_number; queue++)
2007 mvneta_txq_done_force(pp, &pp->txqs[queue]);
2008
2009 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
2010 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
2011}
2012
2013static void mvneta_rx_reset(struct mvneta_port *pp)
2014{
2015 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
2016 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
2017}
2018
2019/* Rx/Tx queue initialization/cleanup methods */
2020
2021/* Create a specified RX queue */
2022static int mvneta_rxq_init(struct mvneta_port *pp,
2023 struct mvneta_rx_queue *rxq)
2024
2025{
2026 rxq->size = pp->rx_ring_size;
2027
2028 /* Allocate memory for RX descriptors */
2029 rxq->descs = dma_alloc_coherent(pp->dev->dev.parent,
2030 rxq->size * MVNETA_DESC_ALIGNED_SIZE,
2031 &rxq->descs_phys, GFP_KERNEL);
Joe Perchesd0320f72013-03-14 13:07:21 +00002032 if (rxq->descs == NULL)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002033 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002034
2035 BUG_ON(rxq->descs !=
2036 PTR_ALIGN(rxq->descs, MVNETA_CPU_D_CACHE_LINE_SIZE));
2037
2038 rxq->last_desc = rxq->size - 1;
2039
2040 /* Set Rx descriptors queue starting address */
2041 mvreg_write(pp, MVNETA_RXQ_BASE_ADDR_REG(rxq->id), rxq->descs_phys);
2042 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), rxq->size);
2043
2044 /* Set Offset */
2045 mvneta_rxq_offset_set(pp, rxq, NET_SKB_PAD);
2046
2047 /* Set coalescing pkts and time */
2048 mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
2049 mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
2050
2051 /* Fill RXQ with buffers from RX pool */
2052 mvneta_rxq_buf_size_set(pp, rxq, MVNETA_RX_BUF_SIZE(pp->pkt_size));
2053 mvneta_rxq_bm_disable(pp, rxq);
2054 mvneta_rxq_fill(pp, rxq, rxq->size);
2055
2056 return 0;
2057}
2058
2059/* Cleanup Rx queue */
2060static void mvneta_rxq_deinit(struct mvneta_port *pp,
2061 struct mvneta_rx_queue *rxq)
2062{
2063 mvneta_rxq_drop_pkts(pp, rxq);
2064
2065 if (rxq->descs)
2066 dma_free_coherent(pp->dev->dev.parent,
2067 rxq->size * MVNETA_DESC_ALIGNED_SIZE,
2068 rxq->descs,
2069 rxq->descs_phys);
2070
2071 rxq->descs = NULL;
2072 rxq->last_desc = 0;
2073 rxq->next_desc_to_proc = 0;
2074 rxq->descs_phys = 0;
2075}
2076
2077/* Create and initialize a tx queue */
2078static int mvneta_txq_init(struct mvneta_port *pp,
2079 struct mvneta_tx_queue *txq)
2080{
2081 txq->size = pp->tx_ring_size;
2082
2083 /* Allocate memory for TX descriptors */
2084 txq->descs = dma_alloc_coherent(pp->dev->dev.parent,
2085 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2086 &txq->descs_phys, GFP_KERNEL);
Joe Perchesd0320f72013-03-14 13:07:21 +00002087 if (txq->descs == NULL)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002088 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002089
2090 /* Make sure descriptor address is cache line size aligned */
2091 BUG_ON(txq->descs !=
2092 PTR_ALIGN(txq->descs, MVNETA_CPU_D_CACHE_LINE_SIZE));
2093
2094 txq->last_desc = txq->size - 1;
2095
2096 /* Set maximum bandwidth for enabled TXQs */
2097 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0x03ffffff);
2098 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0x3fffffff);
2099
2100 /* Set Tx descriptors queue starting address */
2101 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), txq->descs_phys);
2102 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), txq->size);
2103
2104 txq->tx_skb = kmalloc(txq->size * sizeof(*txq->tx_skb), GFP_KERNEL);
2105 if (txq->tx_skb == NULL) {
2106 dma_free_coherent(pp->dev->dev.parent,
2107 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2108 txq->descs, txq->descs_phys);
2109 return -ENOMEM;
2110 }
2111 mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
2112
2113 return 0;
2114}
2115
2116/* Free allocated resources when mvneta_txq_init() fails to allocate memory*/
2117static void mvneta_txq_deinit(struct mvneta_port *pp,
2118 struct mvneta_tx_queue *txq)
2119{
2120 kfree(txq->tx_skb);
2121
2122 if (txq->descs)
2123 dma_free_coherent(pp->dev->dev.parent,
2124 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2125 txq->descs, txq->descs_phys);
2126
2127 txq->descs = NULL;
2128 txq->last_desc = 0;
2129 txq->next_desc_to_proc = 0;
2130 txq->descs_phys = 0;
2131
2132 /* Set minimum bandwidth for disabled TXQs */
2133 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0);
2134 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0);
2135
2136 /* Set Tx descriptors queue starting address and size */
2137 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), 0);
2138 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), 0);
2139}
2140
2141/* Cleanup all Tx queues */
2142static void mvneta_cleanup_txqs(struct mvneta_port *pp)
2143{
2144 int queue;
2145
2146 for (queue = 0; queue < txq_number; queue++)
2147 mvneta_txq_deinit(pp, &pp->txqs[queue]);
2148}
2149
2150/* Cleanup all Rx queues */
2151static void mvneta_cleanup_rxqs(struct mvneta_port *pp)
2152{
2153 int queue;
2154
2155 for (queue = 0; queue < rxq_number; queue++)
2156 mvneta_rxq_deinit(pp, &pp->rxqs[queue]);
2157}
2158
2159
2160/* Init all Rx queues */
2161static int mvneta_setup_rxqs(struct mvneta_port *pp)
2162{
2163 int queue;
2164
2165 for (queue = 0; queue < rxq_number; queue++) {
2166 int err = mvneta_rxq_init(pp, &pp->rxqs[queue]);
2167 if (err) {
2168 netdev_err(pp->dev, "%s: can't create rxq=%d\n",
2169 __func__, queue);
2170 mvneta_cleanup_rxqs(pp);
2171 return err;
2172 }
2173 }
2174
2175 return 0;
2176}
2177
2178/* Init all tx queues */
2179static int mvneta_setup_txqs(struct mvneta_port *pp)
2180{
2181 int queue;
2182
2183 for (queue = 0; queue < txq_number; queue++) {
2184 int err = mvneta_txq_init(pp, &pp->txqs[queue]);
2185 if (err) {
2186 netdev_err(pp->dev, "%s: can't create txq=%d\n",
2187 __func__, queue);
2188 mvneta_cleanup_txqs(pp);
2189 return err;
2190 }
2191 }
2192
2193 return 0;
2194}
2195
2196static void mvneta_start_dev(struct mvneta_port *pp)
2197{
2198 mvneta_max_rx_size_set(pp, pp->pkt_size);
2199 mvneta_txq_max_tx_size_set(pp, pp->pkt_size);
2200
2201 /* start the Rx/Tx activity */
2202 mvneta_port_enable(pp);
2203
2204 /* Enable polling on the port */
2205 napi_enable(&pp->napi);
2206
2207 /* Unmask interrupts */
2208 mvreg_write(pp, MVNETA_INTR_NEW_MASK,
willy tarreau71f6d1b2014-01-16 08:20:11 +01002209 MVNETA_RX_INTR_MASK(rxq_number) | MVNETA_TX_INTR_MASK(txq_number));
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002210
2211 phy_start(pp->phy_dev);
2212 netif_tx_start_all_queues(pp->dev);
2213}
2214
2215static void mvneta_stop_dev(struct mvneta_port *pp)
2216{
2217 phy_stop(pp->phy_dev);
2218
2219 napi_disable(&pp->napi);
2220
2221 netif_carrier_off(pp->dev);
2222
2223 mvneta_port_down(pp);
2224 netif_tx_stop_all_queues(pp->dev);
2225
2226 /* Stop the port activity */
2227 mvneta_port_disable(pp);
2228
2229 /* Clear all ethernet port interrupts */
2230 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
2231 mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0);
2232
2233 /* Mask all ethernet port interrupts */
2234 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
2235 mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0);
2236 mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0);
2237
2238 mvneta_tx_reset(pp);
2239 mvneta_rx_reset(pp);
2240}
2241
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002242/* Return positive if MTU is valid */
2243static int mvneta_check_mtu_valid(struct net_device *dev, int mtu)
2244{
2245 if (mtu < 68) {
2246 netdev_err(dev, "cannot change mtu to less than 68\n");
2247 return -EINVAL;
2248 }
2249
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002250 /* 9676 == 9700 - 20 and rounding to 8 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002251 if (mtu > 9676) {
2252 netdev_info(dev, "Illegal MTU value %d, round to 9676\n", mtu);
2253 mtu = 9676;
2254 }
2255
2256 if (!IS_ALIGNED(MVNETA_RX_PKT_SIZE(mtu), 8)) {
2257 netdev_info(dev, "Illegal MTU value %d, rounding to %d\n",
2258 mtu, ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8));
2259 mtu = ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8);
2260 }
2261
2262 return mtu;
2263}
2264
2265/* Change the device mtu */
2266static int mvneta_change_mtu(struct net_device *dev, int mtu)
2267{
2268 struct mvneta_port *pp = netdev_priv(dev);
2269 int ret;
2270
2271 mtu = mvneta_check_mtu_valid(dev, mtu);
2272 if (mtu < 0)
2273 return -EINVAL;
2274
2275 dev->mtu = mtu;
2276
2277 if (!netif_running(dev))
2278 return 0;
2279
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002280 /* The interface is running, so we have to force a
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002281 * reallocation of the RXQs
2282 */
2283 mvneta_stop_dev(pp);
2284
2285 mvneta_cleanup_txqs(pp);
2286 mvneta_cleanup_rxqs(pp);
2287
2288 pp->pkt_size = MVNETA_RX_PKT_SIZE(pp->dev->mtu);
willy tarreau8ec2cd42014-01-16 08:20:16 +01002289 pp->frag_size = SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(pp->pkt_size)) +
2290 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002291
2292 ret = mvneta_setup_rxqs(pp);
2293 if (ret) {
2294 netdev_err(pp->dev, "unable to setup rxqs after MTU change\n");
2295 return ret;
2296 }
2297
2298 mvneta_setup_txqs(pp);
2299
2300 mvneta_start_dev(pp);
2301 mvneta_port_up(pp);
2302
2303 return 0;
2304}
2305
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00002306/* Get mac address */
2307static void mvneta_get_mac_addr(struct mvneta_port *pp, unsigned char *addr)
2308{
2309 u32 mac_addr_l, mac_addr_h;
2310
2311 mac_addr_l = mvreg_read(pp, MVNETA_MAC_ADDR_LOW);
2312 mac_addr_h = mvreg_read(pp, MVNETA_MAC_ADDR_HIGH);
2313 addr[0] = (mac_addr_h >> 24) & 0xFF;
2314 addr[1] = (mac_addr_h >> 16) & 0xFF;
2315 addr[2] = (mac_addr_h >> 8) & 0xFF;
2316 addr[3] = mac_addr_h & 0xFF;
2317 addr[4] = (mac_addr_l >> 8) & 0xFF;
2318 addr[5] = mac_addr_l & 0xFF;
2319}
2320
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002321/* Handle setting mac address */
2322static int mvneta_set_mac_addr(struct net_device *dev, void *addr)
2323{
2324 struct mvneta_port *pp = netdev_priv(dev);
2325 u8 *mac = addr + 2;
2326 int i;
2327
2328 if (netif_running(dev))
2329 return -EBUSY;
2330
2331 /* Remove previous address table entry */
2332 mvneta_mac_addr_set(pp, dev->dev_addr, -1);
2333
2334 /* Set new addr in hw */
2335 mvneta_mac_addr_set(pp, mac, rxq_def);
2336
2337 /* Set addr in the device */
2338 for (i = 0; i < ETH_ALEN; i++)
2339 dev->dev_addr[i] = mac[i];
2340
2341 return 0;
2342}
2343
2344static void mvneta_adjust_link(struct net_device *ndev)
2345{
2346 struct mvneta_port *pp = netdev_priv(ndev);
2347 struct phy_device *phydev = pp->phy_dev;
2348 int status_change = 0;
2349
2350 if (phydev->link) {
2351 if ((pp->speed != phydev->speed) ||
2352 (pp->duplex != phydev->duplex)) {
2353 u32 val;
2354
2355 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
2356 val &= ~(MVNETA_GMAC_CONFIG_MII_SPEED |
2357 MVNETA_GMAC_CONFIG_GMII_SPEED |
Thomas Petazzoni71408602013-09-04 16:21:18 +02002358 MVNETA_GMAC_CONFIG_FULL_DUPLEX |
2359 MVNETA_GMAC_AN_SPEED_EN |
2360 MVNETA_GMAC_AN_DUPLEX_EN);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002361
2362 if (phydev->duplex)
2363 val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
2364
2365 if (phydev->speed == SPEED_1000)
2366 val |= MVNETA_GMAC_CONFIG_GMII_SPEED;
2367 else
2368 val |= MVNETA_GMAC_CONFIG_MII_SPEED;
2369
2370 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
2371
2372 pp->duplex = phydev->duplex;
2373 pp->speed = phydev->speed;
2374 }
2375 }
2376
2377 if (phydev->link != pp->link) {
2378 if (!phydev->link) {
2379 pp->duplex = -1;
2380 pp->speed = 0;
2381 }
2382
2383 pp->link = phydev->link;
2384 status_change = 1;
2385 }
2386
2387 if (status_change) {
2388 if (phydev->link) {
2389 u32 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
2390 val |= (MVNETA_GMAC_FORCE_LINK_PASS |
2391 MVNETA_GMAC_FORCE_LINK_DOWN);
2392 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
2393 mvneta_port_up(pp);
2394 netdev_info(pp->dev, "link up\n");
2395 } else {
2396 mvneta_port_down(pp);
2397 netdev_info(pp->dev, "link down\n");
2398 }
2399 }
2400}
2401
2402static int mvneta_mdio_probe(struct mvneta_port *pp)
2403{
2404 struct phy_device *phy_dev;
2405
2406 phy_dev = of_phy_connect(pp->dev, pp->phy_node, mvneta_adjust_link, 0,
2407 pp->phy_interface);
2408 if (!phy_dev) {
2409 netdev_err(pp->dev, "could not find the PHY\n");
2410 return -ENODEV;
2411 }
2412
2413 phy_dev->supported &= PHY_GBIT_FEATURES;
2414 phy_dev->advertising = phy_dev->supported;
2415
2416 pp->phy_dev = phy_dev;
2417 pp->link = 0;
2418 pp->duplex = 0;
2419 pp->speed = 0;
2420
2421 return 0;
2422}
2423
2424static void mvneta_mdio_remove(struct mvneta_port *pp)
2425{
2426 phy_disconnect(pp->phy_dev);
2427 pp->phy_dev = NULL;
2428}
2429
2430static int mvneta_open(struct net_device *dev)
2431{
2432 struct mvneta_port *pp = netdev_priv(dev);
2433 int ret;
2434
2435 mvneta_mac_addr_set(pp, dev->dev_addr, rxq_def);
2436
2437 pp->pkt_size = MVNETA_RX_PKT_SIZE(pp->dev->mtu);
willy tarreau8ec2cd42014-01-16 08:20:16 +01002438 pp->frag_size = SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(pp->pkt_size)) +
2439 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002440
2441 ret = mvneta_setup_rxqs(pp);
2442 if (ret)
2443 return ret;
2444
2445 ret = mvneta_setup_txqs(pp);
2446 if (ret)
2447 goto err_cleanup_rxqs;
2448
2449 /* Connect to port interrupt line */
2450 ret = request_irq(pp->dev->irq, mvneta_isr, 0,
2451 MVNETA_DRIVER_NAME, pp);
2452 if (ret) {
2453 netdev_err(pp->dev, "cannot request irq %d\n", pp->dev->irq);
2454 goto err_cleanup_txqs;
2455 }
2456
2457 /* In default link is down */
2458 netif_carrier_off(pp->dev);
2459
2460 ret = mvneta_mdio_probe(pp);
2461 if (ret < 0) {
2462 netdev_err(dev, "cannot probe MDIO bus\n");
2463 goto err_free_irq;
2464 }
2465
2466 mvneta_start_dev(pp);
2467
2468 return 0;
2469
2470err_free_irq:
2471 free_irq(pp->dev->irq, pp);
2472err_cleanup_txqs:
2473 mvneta_cleanup_txqs(pp);
2474err_cleanup_rxqs:
2475 mvneta_cleanup_rxqs(pp);
2476 return ret;
2477}
2478
2479/* Stop the port, free port interrupt line */
2480static int mvneta_stop(struct net_device *dev)
2481{
2482 struct mvneta_port *pp = netdev_priv(dev);
2483
2484 mvneta_stop_dev(pp);
2485 mvneta_mdio_remove(pp);
2486 free_irq(dev->irq, pp);
2487 mvneta_cleanup_rxqs(pp);
2488 mvneta_cleanup_txqs(pp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002489
2490 return 0;
2491}
2492
Thomas Petazzoni15f59452013-09-04 16:26:52 +02002493static int mvneta_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2494{
2495 struct mvneta_port *pp = netdev_priv(dev);
2496 int ret;
2497
2498 if (!pp->phy_dev)
2499 return -ENOTSUPP;
2500
2501 ret = phy_mii_ioctl(pp->phy_dev, ifr, cmd);
2502 if (!ret)
2503 mvneta_adjust_link(dev);
2504
2505 return ret;
2506}
2507
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002508/* Ethtool methods */
2509
2510/* Get settings (phy address, speed) for ethtools */
2511int mvneta_ethtool_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2512{
2513 struct mvneta_port *pp = netdev_priv(dev);
2514
2515 if (!pp->phy_dev)
2516 return -ENODEV;
2517
2518 return phy_ethtool_gset(pp->phy_dev, cmd);
2519}
2520
2521/* Set settings (phy address, speed) for ethtools */
2522int mvneta_ethtool_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2523{
2524 struct mvneta_port *pp = netdev_priv(dev);
2525
2526 if (!pp->phy_dev)
2527 return -ENODEV;
2528
2529 return phy_ethtool_sset(pp->phy_dev, cmd);
2530}
2531
2532/* Set interrupt coalescing for ethtools */
2533static int mvneta_ethtool_set_coalesce(struct net_device *dev,
2534 struct ethtool_coalesce *c)
2535{
2536 struct mvneta_port *pp = netdev_priv(dev);
2537 int queue;
2538
2539 for (queue = 0; queue < rxq_number; queue++) {
2540 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
2541 rxq->time_coal = c->rx_coalesce_usecs;
2542 rxq->pkts_coal = c->rx_max_coalesced_frames;
2543 mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
2544 mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
2545 }
2546
2547 for (queue = 0; queue < txq_number; queue++) {
2548 struct mvneta_tx_queue *txq = &pp->txqs[queue];
2549 txq->done_pkts_coal = c->tx_max_coalesced_frames;
2550 mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
2551 }
2552
2553 return 0;
2554}
2555
2556/* get coalescing for ethtools */
2557static int mvneta_ethtool_get_coalesce(struct net_device *dev,
2558 struct ethtool_coalesce *c)
2559{
2560 struct mvneta_port *pp = netdev_priv(dev);
2561
2562 c->rx_coalesce_usecs = pp->rxqs[0].time_coal;
2563 c->rx_max_coalesced_frames = pp->rxqs[0].pkts_coal;
2564
2565 c->tx_max_coalesced_frames = pp->txqs[0].done_pkts_coal;
2566 return 0;
2567}
2568
2569
2570static void mvneta_ethtool_get_drvinfo(struct net_device *dev,
2571 struct ethtool_drvinfo *drvinfo)
2572{
2573 strlcpy(drvinfo->driver, MVNETA_DRIVER_NAME,
2574 sizeof(drvinfo->driver));
2575 strlcpy(drvinfo->version, MVNETA_DRIVER_VERSION,
2576 sizeof(drvinfo->version));
2577 strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
2578 sizeof(drvinfo->bus_info));
2579}
2580
2581
2582static void mvneta_ethtool_get_ringparam(struct net_device *netdev,
2583 struct ethtool_ringparam *ring)
2584{
2585 struct mvneta_port *pp = netdev_priv(netdev);
2586
2587 ring->rx_max_pending = MVNETA_MAX_RXD;
2588 ring->tx_max_pending = MVNETA_MAX_TXD;
2589 ring->rx_pending = pp->rx_ring_size;
2590 ring->tx_pending = pp->tx_ring_size;
2591}
2592
2593static int mvneta_ethtool_set_ringparam(struct net_device *dev,
2594 struct ethtool_ringparam *ring)
2595{
2596 struct mvneta_port *pp = netdev_priv(dev);
2597
2598 if ((ring->rx_pending == 0) || (ring->tx_pending == 0))
2599 return -EINVAL;
2600 pp->rx_ring_size = ring->rx_pending < MVNETA_MAX_RXD ?
2601 ring->rx_pending : MVNETA_MAX_RXD;
2602 pp->tx_ring_size = ring->tx_pending < MVNETA_MAX_TXD ?
2603 ring->tx_pending : MVNETA_MAX_TXD;
2604
2605 if (netif_running(dev)) {
2606 mvneta_stop(dev);
2607 if (mvneta_open(dev)) {
2608 netdev_err(dev,
2609 "error on opening device after ring param change\n");
2610 return -ENOMEM;
2611 }
2612 }
2613
2614 return 0;
2615}
2616
2617static const struct net_device_ops mvneta_netdev_ops = {
2618 .ndo_open = mvneta_open,
2619 .ndo_stop = mvneta_stop,
2620 .ndo_start_xmit = mvneta_tx,
2621 .ndo_set_rx_mode = mvneta_set_rx_mode,
2622 .ndo_set_mac_address = mvneta_set_mac_addr,
2623 .ndo_change_mtu = mvneta_change_mtu,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002624 .ndo_get_stats64 = mvneta_get_stats64,
Thomas Petazzoni15f59452013-09-04 16:26:52 +02002625 .ndo_do_ioctl = mvneta_ioctl,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002626};
2627
2628const struct ethtool_ops mvneta_eth_tool_ops = {
2629 .get_link = ethtool_op_get_link,
2630 .get_settings = mvneta_ethtool_get_settings,
2631 .set_settings = mvneta_ethtool_set_settings,
2632 .set_coalesce = mvneta_ethtool_set_coalesce,
2633 .get_coalesce = mvneta_ethtool_get_coalesce,
2634 .get_drvinfo = mvneta_ethtool_get_drvinfo,
2635 .get_ringparam = mvneta_ethtool_get_ringparam,
2636 .set_ringparam = mvneta_ethtool_set_ringparam,
2637};
2638
2639/* Initialize hw */
Greg KH03ce7582012-12-21 13:42:15 +00002640static int mvneta_init(struct mvneta_port *pp, int phy_addr)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002641{
2642 int queue;
2643
2644 /* Disable port */
2645 mvneta_port_disable(pp);
2646
2647 /* Set port default values */
2648 mvneta_defaults_set(pp);
2649
2650 pp->txqs = kzalloc(txq_number * sizeof(struct mvneta_tx_queue),
2651 GFP_KERNEL);
2652 if (!pp->txqs)
2653 return -ENOMEM;
2654
2655 /* Initialize TX descriptor rings */
2656 for (queue = 0; queue < txq_number; queue++) {
2657 struct mvneta_tx_queue *txq = &pp->txqs[queue];
2658 txq->id = queue;
2659 txq->size = pp->tx_ring_size;
2660 txq->done_pkts_coal = MVNETA_TXDONE_COAL_PKTS;
2661 }
2662
2663 pp->rxqs = kzalloc(rxq_number * sizeof(struct mvneta_rx_queue),
2664 GFP_KERNEL);
2665 if (!pp->rxqs) {
2666 kfree(pp->txqs);
2667 return -ENOMEM;
2668 }
2669
2670 /* Create Rx descriptor rings */
2671 for (queue = 0; queue < rxq_number; queue++) {
2672 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
2673 rxq->id = queue;
2674 rxq->size = pp->rx_ring_size;
2675 rxq->pkts_coal = MVNETA_RX_COAL_PKTS;
2676 rxq->time_coal = MVNETA_RX_COAL_USEC;
2677 }
2678
2679 return 0;
2680}
2681
Thomas Petazzoni70eeaf92012-11-19 14:40:02 +01002682static void mvneta_deinit(struct mvneta_port *pp)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002683{
2684 kfree(pp->txqs);
2685 kfree(pp->rxqs);
2686}
2687
2688/* platform glue : initialize decoding windows */
Greg KH03ce7582012-12-21 13:42:15 +00002689static void mvneta_conf_mbus_windows(struct mvneta_port *pp,
2690 const struct mbus_dram_target_info *dram)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002691{
2692 u32 win_enable;
2693 u32 win_protect;
2694 int i;
2695
2696 for (i = 0; i < 6; i++) {
2697 mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
2698 mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
2699
2700 if (i < 4)
2701 mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
2702 }
2703
2704 win_enable = 0x3f;
2705 win_protect = 0;
2706
2707 for (i = 0; i < dram->num_cs; i++) {
2708 const struct mbus_dram_window *cs = dram->cs + i;
2709 mvreg_write(pp, MVNETA_WIN_BASE(i), (cs->base & 0xffff0000) |
2710 (cs->mbus_attr << 8) | dram->mbus_dram_target_id);
2711
2712 mvreg_write(pp, MVNETA_WIN_SIZE(i),
2713 (cs->size - 1) & 0xffff0000);
2714
2715 win_enable &= ~(1 << i);
2716 win_protect |= 3 << (2 * i);
2717 }
2718
2719 mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
2720}
2721
2722/* Power up the port */
Greg KH03ce7582012-12-21 13:42:15 +00002723static void mvneta_port_power_up(struct mvneta_port *pp, int phy_mode)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002724{
2725 u32 val;
2726
2727 /* MAC Cause register should be cleared */
2728 mvreg_write(pp, MVNETA_UNIT_INTR_CAUSE, 0);
2729
2730 if (phy_mode == PHY_INTERFACE_MODE_SGMII)
Thomas Petazzonie3a87862014-03-26 00:25:42 +01002731 mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_SGMII_SERDES_PROTO);
2732 else
2733 mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_RGMII_SERDES_PROTO);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002734
Thomas Petazzonie3a87862014-03-26 00:25:42 +01002735 val = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
2736
2737 val |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002738
2739 /* Cancel Port Reset */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002740 val &= ~MVNETA_GMAC2_PORT_RESET;
2741 mvreg_write(pp, MVNETA_GMAC_CTRL_2, val);
2742
2743 while ((mvreg_read(pp, MVNETA_GMAC_CTRL_2) &
2744 MVNETA_GMAC2_PORT_RESET) != 0)
2745 continue;
2746}
2747
2748/* Device initialization routine */
Greg KH03ce7582012-12-21 13:42:15 +00002749static int mvneta_probe(struct platform_device *pdev)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002750{
2751 const struct mbus_dram_target_info *dram_target_info;
2752 struct device_node *dn = pdev->dev.of_node;
2753 struct device_node *phy_node;
Thomas Petazzoni189dd622012-11-19 14:15:25 +01002754 u32 phy_addr;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002755 struct mvneta_port *pp;
2756 struct net_device *dev;
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00002757 const char *dt_mac_addr;
2758 char hw_mac_addr[ETH_ALEN];
2759 const char *mac_from;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002760 int phy_mode;
2761 int err;
willy tarreau74c41b02014-01-16 08:20:08 +01002762 int cpu;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002763
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002764 /* Our multiqueue support is not complete, so for now, only
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002765 * allow the usage of the first RX queue
2766 */
2767 if (rxq_def != 0) {
2768 dev_err(&pdev->dev, "Invalid rxq_def argument: %d\n", rxq_def);
2769 return -EINVAL;
2770 }
2771
Willy Tarreauee40a112013-04-11 23:00:37 +02002772 dev = alloc_etherdev_mqs(sizeof(struct mvneta_port), txq_number, rxq_number);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002773 if (!dev)
2774 return -ENOMEM;
2775
2776 dev->irq = irq_of_parse_and_map(dn, 0);
2777 if (dev->irq == 0) {
2778 err = -EINVAL;
2779 goto err_free_netdev;
2780 }
2781
2782 phy_node = of_parse_phandle(dn, "phy", 0);
2783 if (!phy_node) {
2784 dev_err(&pdev->dev, "no associated PHY\n");
2785 err = -ENODEV;
2786 goto err_free_irq;
2787 }
2788
2789 phy_mode = of_get_phy_mode(dn);
2790 if (phy_mode < 0) {
2791 dev_err(&pdev->dev, "incorrect phy-mode\n");
2792 err = -EINVAL;
2793 goto err_free_irq;
2794 }
2795
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002796 dev->tx_queue_len = MVNETA_MAX_TXD;
2797 dev->watchdog_timeo = 5 * HZ;
2798 dev->netdev_ops = &mvneta_netdev_ops;
2799
2800 SET_ETHTOOL_OPS(dev, &mvneta_eth_tool_ops);
2801
2802 pp = netdev_priv(dev);
2803
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002804 pp->weight = MVNETA_RX_POLL_WEIGHT;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002805 pp->phy_node = phy_node;
2806 pp->phy_interface = phy_mode;
2807
Thomas Petazzoni189dd622012-11-19 14:15:25 +01002808 pp->clk = devm_clk_get(&pdev->dev, NULL);
2809 if (IS_ERR(pp->clk)) {
2810 err = PTR_ERR(pp->clk);
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +02002811 goto err_free_irq;
Thomas Petazzoni189dd622012-11-19 14:15:25 +01002812 }
2813
2814 clk_prepare_enable(pp->clk);
2815
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +02002816 pp->base = of_iomap(dn, 0);
2817 if (pp->base == NULL) {
2818 err = -ENOMEM;
2819 goto err_clk;
2820 }
2821
willy tarreau74c41b02014-01-16 08:20:08 +01002822 /* Alloc per-cpu stats */
2823 pp->stats = alloc_percpu(struct mvneta_pcpu_stats);
2824 if (!pp->stats) {
2825 err = -ENOMEM;
2826 goto err_unmap;
2827 }
2828
2829 for_each_possible_cpu(cpu) {
2830 struct mvneta_pcpu_stats *stats;
2831 stats = per_cpu_ptr(pp->stats, cpu);
2832 u64_stats_init(&stats->syncp);
2833 }
2834
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00002835 dt_mac_addr = of_get_mac_address(dn);
Luka Perkov6c7a9a32013-10-30 00:10:01 +01002836 if (dt_mac_addr) {
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00002837 mac_from = "device tree";
2838 memcpy(dev->dev_addr, dt_mac_addr, ETH_ALEN);
2839 } else {
2840 mvneta_get_mac_addr(pp, hw_mac_addr);
2841 if (is_valid_ether_addr(hw_mac_addr)) {
2842 mac_from = "hardware";
2843 memcpy(dev->dev_addr, hw_mac_addr, ETH_ALEN);
2844 } else {
2845 mac_from = "random";
2846 eth_hw_addr_random(dev);
2847 }
2848 }
2849
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002850 pp->tx_ring_size = MVNETA_MAX_TXD;
2851 pp->rx_ring_size = MVNETA_MAX_RXD;
2852
2853 pp->dev = dev;
2854 SET_NETDEV_DEV(dev, &pdev->dev);
2855
2856 err = mvneta_init(pp, phy_addr);
2857 if (err < 0) {
2858 dev_err(&pdev->dev, "can't init eth hal\n");
willy tarreau74c41b02014-01-16 08:20:08 +01002859 goto err_free_stats;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002860 }
2861 mvneta_port_power_up(pp, phy_mode);
2862
2863 dram_target_info = mv_mbus_dram_info();
2864 if (dram_target_info)
2865 mvneta_conf_mbus_windows(pp, dram_target_info);
2866
2867 netif_napi_add(dev, &pp->napi, mvneta_poll, pp->weight);
2868
willy tarreaub50b72d2013-04-06 08:47:01 +00002869 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM;
2870 dev->hw_features |= NETIF_F_SG | NETIF_F_IP_CSUM;
2871 dev->vlan_features |= NETIF_F_SG | NETIF_F_IP_CSUM;
2872 dev->priv_flags |= IFF_UNICAST_FLT;
2873
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002874 err = register_netdev(dev);
2875 if (err < 0) {
2876 dev_err(&pdev->dev, "failed to register\n");
2877 goto err_deinit;
2878 }
2879
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00002880 netdev_info(dev, "Using %s mac address %pM\n", mac_from,
2881 dev->dev_addr);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002882
2883 platform_set_drvdata(pdev, pp->dev);
2884
2885 return 0;
2886
2887err_deinit:
2888 mvneta_deinit(pp);
willy tarreau74c41b02014-01-16 08:20:08 +01002889err_free_stats:
2890 free_percpu(pp->stats);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002891err_unmap:
2892 iounmap(pp->base);
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +02002893err_clk:
2894 clk_disable_unprepare(pp->clk);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002895err_free_irq:
2896 irq_dispose_mapping(dev->irq);
2897err_free_netdev:
2898 free_netdev(dev);
2899 return err;
2900}
2901
2902/* Device removal routine */
Greg KH03ce7582012-12-21 13:42:15 +00002903static int mvneta_remove(struct platform_device *pdev)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002904{
2905 struct net_device *dev = platform_get_drvdata(pdev);
2906 struct mvneta_port *pp = netdev_priv(dev);
2907
2908 unregister_netdev(dev);
2909 mvneta_deinit(pp);
Thomas Petazzoni189dd622012-11-19 14:15:25 +01002910 clk_disable_unprepare(pp->clk);
willy tarreau74c41b02014-01-16 08:20:08 +01002911 free_percpu(pp->stats);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002912 iounmap(pp->base);
2913 irq_dispose_mapping(dev->irq);
2914 free_netdev(dev);
2915
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002916 return 0;
2917}
2918
2919static const struct of_device_id mvneta_match[] = {
2920 { .compatible = "marvell,armada-370-neta" },
2921 { }
2922};
2923MODULE_DEVICE_TABLE(of, mvneta_match);
2924
2925static struct platform_driver mvneta_driver = {
2926 .probe = mvneta_probe,
Greg KH03ce7582012-12-21 13:42:15 +00002927 .remove = mvneta_remove,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002928 .driver = {
2929 .name = MVNETA_DRIVER_NAME,
2930 .of_match_table = mvneta_match,
2931 },
2932};
2933
2934module_platform_driver(mvneta_driver);
2935
2936MODULE_DESCRIPTION("Marvell NETA Ethernet Driver - www.marvell.com");
2937MODULE_AUTHOR("Rami Rosen <rosenr@marvell.com>, Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
2938MODULE_LICENSE("GPL");
2939
2940module_param(rxq_number, int, S_IRUGO);
2941module_param(txq_number, int, S_IRUGO);
2942
2943module_param(rxq_def, int, S_IRUGO);
willy tarreauf19fadf2014-01-16 08:20:17 +01002944module_param(rx_copybreak, int, S_IRUGO | S_IWUSR);