blob: bcce0b4377225803dbc3300b1ef13da7f78fb692 [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>
Thomas Petazzonic3f0dd32014-03-27 11:39:29 +010025#include <linux/io.h>
Thomas Petazzonic5aff182012-08-17 14:04:28 +030026#include <linux/of.h>
27#include <linux/of_irq.h>
28#include <linux/of_mdio.h>
29#include <linux/of_net.h>
30#include <linux/of_address.h>
31#include <linux/phy.h>
Thomas Petazzoni189dd622012-11-19 14:15:25 +010032#include <linux/clk.h>
Thomas Petazzonic5aff182012-08-17 14:04:28 +030033
34/* Registers */
35#define MVNETA_RXQ_CONFIG_REG(q) (0x1400 + ((q) << 2))
36#define MVNETA_RXQ_HW_BUF_ALLOC BIT(1)
37#define MVNETA_RXQ_PKT_OFFSET_ALL_MASK (0xf << 8)
38#define MVNETA_RXQ_PKT_OFFSET_MASK(offs) ((offs) << 8)
39#define MVNETA_RXQ_THRESHOLD_REG(q) (0x14c0 + ((q) << 2))
40#define MVNETA_RXQ_NON_OCCUPIED(v) ((v) << 16)
41#define MVNETA_RXQ_BASE_ADDR_REG(q) (0x1480 + ((q) << 2))
42#define MVNETA_RXQ_SIZE_REG(q) (0x14a0 + ((q) << 2))
43#define MVNETA_RXQ_BUF_SIZE_SHIFT 19
44#define MVNETA_RXQ_BUF_SIZE_MASK (0x1fff << 19)
45#define MVNETA_RXQ_STATUS_REG(q) (0x14e0 + ((q) << 2))
46#define MVNETA_RXQ_OCCUPIED_ALL_MASK 0x3fff
47#define MVNETA_RXQ_STATUS_UPDATE_REG(q) (0x1500 + ((q) << 2))
48#define MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT 16
49#define MVNETA_RXQ_ADD_NON_OCCUPIED_MAX 255
50#define MVNETA_PORT_RX_RESET 0x1cc0
51#define MVNETA_PORT_RX_DMA_RESET BIT(0)
52#define MVNETA_PHY_ADDR 0x2000
53#define MVNETA_PHY_ADDR_MASK 0x1f
54#define MVNETA_MBUS_RETRY 0x2010
55#define MVNETA_UNIT_INTR_CAUSE 0x2080
56#define MVNETA_UNIT_CONTROL 0x20B0
57#define MVNETA_PHY_POLLING_ENABLE BIT(1)
58#define MVNETA_WIN_BASE(w) (0x2200 + ((w) << 3))
59#define MVNETA_WIN_SIZE(w) (0x2204 + ((w) << 3))
60#define MVNETA_WIN_REMAP(w) (0x2280 + ((w) << 2))
61#define MVNETA_BASE_ADDR_ENABLE 0x2290
62#define MVNETA_PORT_CONFIG 0x2400
63#define MVNETA_UNI_PROMISC_MODE BIT(0)
64#define MVNETA_DEF_RXQ(q) ((q) << 1)
65#define MVNETA_DEF_RXQ_ARP(q) ((q) << 4)
66#define MVNETA_TX_UNSET_ERR_SUM BIT(12)
67#define MVNETA_DEF_RXQ_TCP(q) ((q) << 16)
68#define MVNETA_DEF_RXQ_UDP(q) ((q) << 19)
69#define MVNETA_DEF_RXQ_BPDU(q) ((q) << 22)
70#define MVNETA_RX_CSUM_WITH_PSEUDO_HDR BIT(25)
71#define MVNETA_PORT_CONFIG_DEFL_VALUE(q) (MVNETA_DEF_RXQ(q) | \
72 MVNETA_DEF_RXQ_ARP(q) | \
73 MVNETA_DEF_RXQ_TCP(q) | \
74 MVNETA_DEF_RXQ_UDP(q) | \
75 MVNETA_DEF_RXQ_BPDU(q) | \
76 MVNETA_TX_UNSET_ERR_SUM | \
77 MVNETA_RX_CSUM_WITH_PSEUDO_HDR)
78#define MVNETA_PORT_CONFIG_EXTEND 0x2404
79#define MVNETA_MAC_ADDR_LOW 0x2414
80#define MVNETA_MAC_ADDR_HIGH 0x2418
81#define MVNETA_SDMA_CONFIG 0x241c
82#define MVNETA_SDMA_BRST_SIZE_16 4
Thomas Petazzonic5aff182012-08-17 14:04:28 +030083#define MVNETA_RX_BRST_SZ_MASK(burst) ((burst) << 1)
84#define MVNETA_RX_NO_DATA_SWAP BIT(4)
85#define MVNETA_TX_NO_DATA_SWAP BIT(5)
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +020086#define MVNETA_DESC_SWAP BIT(6)
Thomas Petazzonic5aff182012-08-17 14:04:28 +030087#define MVNETA_TX_BRST_SZ_MASK(burst) ((burst) << 22)
88#define MVNETA_PORT_STATUS 0x2444
89#define MVNETA_TX_IN_PRGRS BIT(1)
90#define MVNETA_TX_FIFO_EMPTY BIT(8)
91#define MVNETA_RX_MIN_FRAME_SIZE 0x247c
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +020092#define MVNETA_SGMII_SERDES_CFG 0x24A0
93#define MVNETA_SGMII_SERDES_PROTO 0x0cc7
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
165#define MVNETA_GMAC2_PSC_ENABLE BIT(3)
166#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 {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700512 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
willy tarreau74c41b02014-01-16 08:20:08 +0100513 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;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700517 } while (u64_stats_fetch_retry_irq(&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
714
715
716/* Sets the RGMII Enable bit (RGMIIEn) in port MAC control register */
Greg KH03ce7582012-12-21 13:42:15 +0000717static void mvneta_gmac_rgmii_set(struct mvneta_port *pp, int enable)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300718{
719 u32 val;
720
721 val = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
722
723 if (enable)
724 val |= MVNETA_GMAC2_PORT_RGMII;
725 else
726 val &= ~MVNETA_GMAC2_PORT_RGMII;
727
728 mvreg_write(pp, MVNETA_GMAC_CTRL_2, val);
729}
730
731/* Config SGMII port */
Greg KH03ce7582012-12-21 13:42:15 +0000732static void mvneta_port_sgmii_config(struct mvneta_port *pp)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300733{
734 u32 val;
735
736 val = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
737 val |= MVNETA_GMAC2_PSC_ENABLE;
738 mvreg_write(pp, MVNETA_GMAC_CTRL_2, val);
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +0200739
740 mvreg_write(pp, MVNETA_SGMII_SERDES_CFG, MVNETA_SGMII_SERDES_PROTO);
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300741}
742
743/* Start the Ethernet port RX and TX activity */
744static void mvneta_port_up(struct mvneta_port *pp)
745{
746 int queue;
747 u32 q_map;
748
749 /* Enable all initialized TXs. */
750 mvneta_mib_counters_clear(pp);
751 q_map = 0;
752 for (queue = 0; queue < txq_number; queue++) {
753 struct mvneta_tx_queue *txq = &pp->txqs[queue];
754 if (txq->descs != NULL)
755 q_map |= (1 << queue);
756 }
757 mvreg_write(pp, MVNETA_TXQ_CMD, q_map);
758
759 /* Enable all initialized RXQs. */
760 q_map = 0;
761 for (queue = 0; queue < rxq_number; queue++) {
762 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
763 if (rxq->descs != NULL)
764 q_map |= (1 << queue);
765 }
766
767 mvreg_write(pp, MVNETA_RXQ_CMD, q_map);
768}
769
770/* Stop the Ethernet port activity */
771static void mvneta_port_down(struct mvneta_port *pp)
772{
773 u32 val;
774 int count;
775
776 /* Stop Rx port activity. Check port Rx activity. */
777 val = mvreg_read(pp, MVNETA_RXQ_CMD) & MVNETA_RXQ_ENABLE_MASK;
778
779 /* Issue stop command for active channels only */
780 if (val != 0)
781 mvreg_write(pp, MVNETA_RXQ_CMD,
782 val << MVNETA_RXQ_DISABLE_SHIFT);
783
784 /* Wait for all Rx activity to terminate. */
785 count = 0;
786 do {
787 if (count++ >= MVNETA_RX_DISABLE_TIMEOUT_MSEC) {
788 netdev_warn(pp->dev,
789 "TIMEOUT for RX stopped ! rx_queue_cmd: 0x08%x\n",
790 val);
791 break;
792 }
793 mdelay(1);
794
795 val = mvreg_read(pp, MVNETA_RXQ_CMD);
796 } while (val & 0xff);
797
798 /* Stop Tx port activity. Check port Tx activity. Issue stop
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100799 * command for active channels only
800 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300801 val = (mvreg_read(pp, MVNETA_TXQ_CMD)) & MVNETA_TXQ_ENABLE_MASK;
802
803 if (val != 0)
804 mvreg_write(pp, MVNETA_TXQ_CMD,
805 (val << MVNETA_TXQ_DISABLE_SHIFT));
806
807 /* Wait for all Tx activity to terminate. */
808 count = 0;
809 do {
810 if (count++ >= MVNETA_TX_DISABLE_TIMEOUT_MSEC) {
811 netdev_warn(pp->dev,
812 "TIMEOUT for TX stopped status=0x%08x\n",
813 val);
814 break;
815 }
816 mdelay(1);
817
818 /* Check TX Command reg that all Txqs are stopped */
819 val = mvreg_read(pp, MVNETA_TXQ_CMD);
820
821 } while (val & 0xff);
822
823 /* Double check to verify that TX FIFO is empty */
824 count = 0;
825 do {
826 if (count++ >= MVNETA_TX_FIFO_EMPTY_TIMEOUT) {
827 netdev_warn(pp->dev,
828 "TX FIFO empty timeout status=0x08%x\n",
829 val);
830 break;
831 }
832 mdelay(1);
833
834 val = mvreg_read(pp, MVNETA_PORT_STATUS);
835 } while (!(val & MVNETA_TX_FIFO_EMPTY) &&
836 (val & MVNETA_TX_IN_PRGRS));
837
838 udelay(200);
839}
840
841/* Enable the port by setting the port enable bit of the MAC control register */
842static void mvneta_port_enable(struct mvneta_port *pp)
843{
844 u32 val;
845
846 /* Enable port */
847 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
848 val |= MVNETA_GMAC0_PORT_ENABLE;
849 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
850}
851
852/* Disable the port and wait for about 200 usec before retuning */
853static void mvneta_port_disable(struct mvneta_port *pp)
854{
855 u32 val;
856
857 /* Reset the Enable bit in the Serial Control Register */
858 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
859 val &= ~MVNETA_GMAC0_PORT_ENABLE;
860 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
861
862 udelay(200);
863}
864
865/* Multicast tables methods */
866
867/* Set all entries in Unicast MAC Table; queue==-1 means reject all */
868static void mvneta_set_ucast_table(struct mvneta_port *pp, int queue)
869{
870 int offset;
871 u32 val;
872
873 if (queue == -1) {
874 val = 0;
875 } else {
876 val = 0x1 | (queue << 1);
877 val |= (val << 24) | (val << 16) | (val << 8);
878 }
879
880 for (offset = 0; offset <= 0xc; offset += 4)
881 mvreg_write(pp, MVNETA_DA_FILT_UCAST_BASE + offset, val);
882}
883
884/* Set all entries in Special Multicast MAC Table; queue==-1 means reject all */
885static void mvneta_set_special_mcast_table(struct mvneta_port *pp, int queue)
886{
887 int offset;
888 u32 val;
889
890 if (queue == -1) {
891 val = 0;
892 } else {
893 val = 0x1 | (queue << 1);
894 val |= (val << 24) | (val << 16) | (val << 8);
895 }
896
897 for (offset = 0; offset <= 0xfc; offset += 4)
898 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + offset, val);
899
900}
901
902/* Set all entries in Other Multicast MAC Table. queue==-1 means reject all */
903static void mvneta_set_other_mcast_table(struct mvneta_port *pp, int queue)
904{
905 int offset;
906 u32 val;
907
908 if (queue == -1) {
909 memset(pp->mcast_count, 0, sizeof(pp->mcast_count));
910 val = 0;
911 } else {
912 memset(pp->mcast_count, 1, sizeof(pp->mcast_count));
913 val = 0x1 | (queue << 1);
914 val |= (val << 24) | (val << 16) | (val << 8);
915 }
916
917 for (offset = 0; offset <= 0xfc; offset += 4)
918 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + offset, val);
919}
920
921/* This method sets defaults to the NETA port:
922 * Clears interrupt Cause and Mask registers.
923 * Clears all MAC tables.
924 * Sets defaults to all registers.
925 * Resets RX and TX descriptor rings.
926 * Resets PHY.
927 * This method can be called after mvneta_port_down() to return the port
928 * settings to defaults.
929 */
930static void mvneta_defaults_set(struct mvneta_port *pp)
931{
932 int cpu;
933 int queue;
934 u32 val;
935
936 /* Clear all Cause registers */
937 mvreg_write(pp, MVNETA_INTR_NEW_CAUSE, 0);
938 mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0);
939 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
940
941 /* Mask all interrupts */
942 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
943 mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0);
944 mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0);
945 mvreg_write(pp, MVNETA_INTR_ENABLE, 0);
946
947 /* Enable MBUS Retry bit16 */
948 mvreg_write(pp, MVNETA_MBUS_RETRY, 0x20);
949
950 /* Set CPU queue access map - all CPUs have access to all RX
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100951 * queues and to all TX queues
952 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300953 for (cpu = 0; cpu < CONFIG_NR_CPUS; cpu++)
954 mvreg_write(pp, MVNETA_CPU_MAP(cpu),
955 (MVNETA_CPU_RXQ_ACCESS_ALL_MASK |
956 MVNETA_CPU_TXQ_ACCESS_ALL_MASK));
957
958 /* Reset RX and TX DMAs */
959 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
960 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
961
962 /* Disable Legacy WRR, Disable EJP, Release from reset */
963 mvreg_write(pp, MVNETA_TXQ_CMD_1, 0);
964 for (queue = 0; queue < txq_number; queue++) {
965 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(queue), 0);
966 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(queue), 0);
967 }
968
969 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
970 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
971
972 /* Set Port Acceleration Mode */
973 val = MVNETA_ACC_MODE_EXT;
974 mvreg_write(pp, MVNETA_ACC_MODE, val);
975
976 /* Update val of portCfg register accordingly with all RxQueue types */
977 val = MVNETA_PORT_CONFIG_DEFL_VALUE(rxq_def);
978 mvreg_write(pp, MVNETA_PORT_CONFIG, val);
979
980 val = 0;
981 mvreg_write(pp, MVNETA_PORT_CONFIG_EXTEND, val);
982 mvreg_write(pp, MVNETA_RX_MIN_FRAME_SIZE, 64);
983
984 /* Build PORT_SDMA_CONFIG_REG */
985 val = 0;
986
987 /* Default burst size */
988 val |= MVNETA_TX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
989 val |= MVNETA_RX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +0200990 val |= MVNETA_RX_NO_DATA_SWAP | MVNETA_TX_NO_DATA_SWAP;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300991
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +0200992#if defined(__BIG_ENDIAN)
993 val |= MVNETA_DESC_SWAP;
994#endif
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300995
996 /* Assign port SDMA configuration */
997 mvreg_write(pp, MVNETA_SDMA_CONFIG, val);
998
Thomas Petazzoni71408602013-09-04 16:21:18 +0200999 /* Disable PHY polling in hardware, since we're using the
1000 * kernel phylib to do this.
1001 */
1002 val = mvreg_read(pp, MVNETA_UNIT_CONTROL);
1003 val &= ~MVNETA_PHY_POLLING_ENABLE;
1004 mvreg_write(pp, MVNETA_UNIT_CONTROL, val);
1005
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001006 mvneta_set_ucast_table(pp, -1);
1007 mvneta_set_special_mcast_table(pp, -1);
1008 mvneta_set_other_mcast_table(pp, -1);
1009
1010 /* Set port interrupt enable register - default enable all */
1011 mvreg_write(pp, MVNETA_INTR_ENABLE,
1012 (MVNETA_RXQ_INTR_ENABLE_ALL_MASK
1013 | MVNETA_TXQ_INTR_ENABLE_ALL_MASK));
1014}
1015
1016/* Set max sizes for tx queues */
1017static void mvneta_txq_max_tx_size_set(struct mvneta_port *pp, int max_tx_size)
1018
1019{
1020 u32 val, size, mtu;
1021 int queue;
1022
1023 mtu = max_tx_size * 8;
1024 if (mtu > MVNETA_TX_MTU_MAX)
1025 mtu = MVNETA_TX_MTU_MAX;
1026
1027 /* Set MTU */
1028 val = mvreg_read(pp, MVNETA_TX_MTU);
1029 val &= ~MVNETA_TX_MTU_MAX;
1030 val |= mtu;
1031 mvreg_write(pp, MVNETA_TX_MTU, val);
1032
1033 /* TX token size and all TXQs token size must be larger that MTU */
1034 val = mvreg_read(pp, MVNETA_TX_TOKEN_SIZE);
1035
1036 size = val & MVNETA_TX_TOKEN_SIZE_MAX;
1037 if (size < mtu) {
1038 size = mtu;
1039 val &= ~MVNETA_TX_TOKEN_SIZE_MAX;
1040 val |= size;
1041 mvreg_write(pp, MVNETA_TX_TOKEN_SIZE, val);
1042 }
1043 for (queue = 0; queue < txq_number; queue++) {
1044 val = mvreg_read(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue));
1045
1046 size = val & MVNETA_TXQ_TOKEN_SIZE_MAX;
1047 if (size < mtu) {
1048 size = mtu;
1049 val &= ~MVNETA_TXQ_TOKEN_SIZE_MAX;
1050 val |= size;
1051 mvreg_write(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue), val);
1052 }
1053 }
1054}
1055
1056/* Set unicast address */
1057static void mvneta_set_ucast_addr(struct mvneta_port *pp, u8 last_nibble,
1058 int queue)
1059{
1060 unsigned int unicast_reg;
1061 unsigned int tbl_offset;
1062 unsigned int reg_offset;
1063
1064 /* Locate the Unicast table entry */
1065 last_nibble = (0xf & last_nibble);
1066
1067 /* offset from unicast tbl base */
1068 tbl_offset = (last_nibble / 4) * 4;
1069
1070 /* offset within the above reg */
1071 reg_offset = last_nibble % 4;
1072
1073 unicast_reg = mvreg_read(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset));
1074
1075 if (queue == -1) {
1076 /* Clear accepts frame bit at specified unicast DA tbl entry */
1077 unicast_reg &= ~(0xff << (8 * reg_offset));
1078 } else {
1079 unicast_reg &= ~(0xff << (8 * reg_offset));
1080 unicast_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
1081 }
1082
1083 mvreg_write(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset), unicast_reg);
1084}
1085
1086/* Set mac address */
1087static void mvneta_mac_addr_set(struct mvneta_port *pp, unsigned char *addr,
1088 int queue)
1089{
1090 unsigned int mac_h;
1091 unsigned int mac_l;
1092
1093 if (queue != -1) {
1094 mac_l = (addr[4] << 8) | (addr[5]);
1095 mac_h = (addr[0] << 24) | (addr[1] << 16) |
1096 (addr[2] << 8) | (addr[3] << 0);
1097
1098 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, mac_l);
1099 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, mac_h);
1100 }
1101
1102 /* Accept frames of this address */
1103 mvneta_set_ucast_addr(pp, addr[5], queue);
1104}
1105
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001106/* Set the number of packets that will be received before RX interrupt
1107 * will be generated by HW.
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001108 */
1109static void mvneta_rx_pkts_coal_set(struct mvneta_port *pp,
1110 struct mvneta_rx_queue *rxq, u32 value)
1111{
1112 mvreg_write(pp, MVNETA_RXQ_THRESHOLD_REG(rxq->id),
1113 value | MVNETA_RXQ_NON_OCCUPIED(0));
1114 rxq->pkts_coal = value;
1115}
1116
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001117/* Set the time delay in usec before RX interrupt will be generated by
1118 * HW.
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001119 */
1120static void mvneta_rx_time_coal_set(struct mvneta_port *pp,
1121 struct mvneta_rx_queue *rxq, u32 value)
1122{
Thomas Petazzoni189dd622012-11-19 14:15:25 +01001123 u32 val;
1124 unsigned long clk_rate;
1125
1126 clk_rate = clk_get_rate(pp->clk);
1127 val = (clk_rate / 1000000) * value;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001128
1129 mvreg_write(pp, MVNETA_RXQ_TIME_COAL_REG(rxq->id), val);
1130 rxq->time_coal = value;
1131}
1132
1133/* Set threshold for TX_DONE pkts coalescing */
1134static void mvneta_tx_done_pkts_coal_set(struct mvneta_port *pp,
1135 struct mvneta_tx_queue *txq, u32 value)
1136{
1137 u32 val;
1138
1139 val = mvreg_read(pp, MVNETA_TXQ_SIZE_REG(txq->id));
1140
1141 val &= ~MVNETA_TXQ_SENT_THRESH_ALL_MASK;
1142 val |= MVNETA_TXQ_SENT_THRESH_MASK(value);
1143
1144 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), val);
1145
1146 txq->done_pkts_coal = value;
1147}
1148
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001149/* Handle rx descriptor fill by setting buf_cookie and buf_phys_addr */
1150static void mvneta_rx_desc_fill(struct mvneta_rx_desc *rx_desc,
1151 u32 phys_addr, u32 cookie)
1152{
1153 rx_desc->buf_cookie = cookie;
1154 rx_desc->buf_phys_addr = phys_addr;
1155}
1156
1157/* Decrement sent descriptors counter */
1158static void mvneta_txq_sent_desc_dec(struct mvneta_port *pp,
1159 struct mvneta_tx_queue *txq,
1160 int sent_desc)
1161{
1162 u32 val;
1163
1164 /* Only 255 TX descriptors can be updated at once */
1165 while (sent_desc > 0xff) {
1166 val = 0xff << MVNETA_TXQ_DEC_SENT_SHIFT;
1167 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1168 sent_desc = sent_desc - 0xff;
1169 }
1170
1171 val = sent_desc << MVNETA_TXQ_DEC_SENT_SHIFT;
1172 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1173}
1174
1175/* Get number of TX descriptors already sent by HW */
1176static int mvneta_txq_sent_desc_num_get(struct mvneta_port *pp,
1177 struct mvneta_tx_queue *txq)
1178{
1179 u32 val;
1180 int sent_desc;
1181
1182 val = mvreg_read(pp, MVNETA_TXQ_STATUS_REG(txq->id));
1183 sent_desc = (val & MVNETA_TXQ_SENT_DESC_MASK) >>
1184 MVNETA_TXQ_SENT_DESC_SHIFT;
1185
1186 return sent_desc;
1187}
1188
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001189/* Get number of sent descriptors and decrement counter.
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001190 * The number of sent descriptors is returned.
1191 */
1192static int mvneta_txq_sent_desc_proc(struct mvneta_port *pp,
1193 struct mvneta_tx_queue *txq)
1194{
1195 int sent_desc;
1196
1197 /* Get number of sent descriptors */
1198 sent_desc = mvneta_txq_sent_desc_num_get(pp, txq);
1199
1200 /* Decrement sent descriptors counter */
1201 if (sent_desc)
1202 mvneta_txq_sent_desc_dec(pp, txq, sent_desc);
1203
1204 return sent_desc;
1205}
1206
1207/* Set TXQ descriptors fields relevant for CSUM calculation */
1208static u32 mvneta_txq_desc_csum(int l3_offs, int l3_proto,
1209 int ip_hdr_len, int l4_proto)
1210{
1211 u32 command;
1212
1213 /* Fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001214 * G_L4_chk, L4_type; required only for checksum
1215 * calculation
1216 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001217 command = l3_offs << MVNETA_TX_L3_OFF_SHIFT;
1218 command |= ip_hdr_len << MVNETA_TX_IP_HLEN_SHIFT;
1219
1220 if (l3_proto == swab16(ETH_P_IP))
1221 command |= MVNETA_TXD_IP_CSUM;
1222 else
1223 command |= MVNETA_TX_L3_IP6;
1224
1225 if (l4_proto == IPPROTO_TCP)
1226 command |= MVNETA_TX_L4_CSUM_FULL;
1227 else if (l4_proto == IPPROTO_UDP)
1228 command |= MVNETA_TX_L4_UDP | MVNETA_TX_L4_CSUM_FULL;
1229 else
1230 command |= MVNETA_TX_L4_CSUM_NOT;
1231
1232 return command;
1233}
1234
1235
1236/* Display more error info */
1237static void mvneta_rx_error(struct mvneta_port *pp,
1238 struct mvneta_rx_desc *rx_desc)
1239{
1240 u32 status = rx_desc->status;
1241
willy tarreau54282132014-01-16 08:20:14 +01001242 if (!mvneta_rxq_desc_is_first_last(status)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001243 netdev_err(pp->dev,
1244 "bad rx status %08x (buffer oversize), size=%d\n",
willy tarreau54282132014-01-16 08:20:14 +01001245 status, rx_desc->data_size);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001246 return;
1247 }
1248
1249 switch (status & MVNETA_RXD_ERR_CODE_MASK) {
1250 case MVNETA_RXD_ERR_CRC:
1251 netdev_err(pp->dev, "bad rx status %08x (crc error), size=%d\n",
1252 status, rx_desc->data_size);
1253 break;
1254 case MVNETA_RXD_ERR_OVERRUN:
1255 netdev_err(pp->dev, "bad rx status %08x (overrun error), size=%d\n",
1256 status, rx_desc->data_size);
1257 break;
1258 case MVNETA_RXD_ERR_LEN:
1259 netdev_err(pp->dev, "bad rx status %08x (max frame length error), size=%d\n",
1260 status, rx_desc->data_size);
1261 break;
1262 case MVNETA_RXD_ERR_RESOURCE:
1263 netdev_err(pp->dev, "bad rx status %08x (resource error), size=%d\n",
1264 status, rx_desc->data_size);
1265 break;
1266 }
1267}
1268
willy tarreau54282132014-01-16 08:20:14 +01001269/* Handle RX checksum offload based on the descriptor's status */
1270static void mvneta_rx_csum(struct mvneta_port *pp, u32 status,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001271 struct sk_buff *skb)
1272{
willy tarreau54282132014-01-16 08:20:14 +01001273 if ((status & MVNETA_RXD_L3_IP4) &&
1274 (status & MVNETA_RXD_L4_CSUM_OK)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001275 skb->csum = 0;
1276 skb->ip_summed = CHECKSUM_UNNECESSARY;
1277 return;
1278 }
1279
1280 skb->ip_summed = CHECKSUM_NONE;
1281}
1282
willy tarreau6c498972014-01-16 08:20:12 +01001283/* Return tx queue pointer (find last set bit) according to <cause> returned
1284 * form tx_done reg. <cause> must not be null. The return value is always a
1285 * valid queue for matching the first one found in <cause>.
1286 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001287static struct mvneta_tx_queue *mvneta_tx_done_policy(struct mvneta_port *pp,
1288 u32 cause)
1289{
1290 int queue = fls(cause) - 1;
1291
willy tarreau6c498972014-01-16 08:20:12 +01001292 return &pp->txqs[queue];
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001293}
1294
1295/* Free tx queue skbuffs */
1296static void mvneta_txq_bufs_free(struct mvneta_port *pp,
1297 struct mvneta_tx_queue *txq, int num)
1298{
1299 int i;
1300
1301 for (i = 0; i < num; i++) {
1302 struct mvneta_tx_desc *tx_desc = txq->descs +
1303 txq->txq_get_index;
1304 struct sk_buff *skb = txq->tx_skb[txq->txq_get_index];
1305
1306 mvneta_txq_inc_get(txq);
1307
1308 if (!skb)
1309 continue;
1310
1311 dma_unmap_single(pp->dev->dev.parent, tx_desc->buf_phys_addr,
1312 tx_desc->data_size, DMA_TO_DEVICE);
1313 dev_kfree_skb_any(skb);
1314 }
1315}
1316
1317/* Handle end of transmission */
Arnaud Ebalardcd713192014-01-16 08:20:19 +01001318static void mvneta_txq_done(struct mvneta_port *pp,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001319 struct mvneta_tx_queue *txq)
1320{
1321 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
1322 int tx_done;
1323
1324 tx_done = mvneta_txq_sent_desc_proc(pp, txq);
Arnaud Ebalardcd713192014-01-16 08:20:19 +01001325 if (!tx_done)
1326 return;
1327
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001328 mvneta_txq_bufs_free(pp, txq, tx_done);
1329
1330 txq->count -= tx_done;
1331
1332 if (netif_tx_queue_stopped(nq)) {
1333 if (txq->size - txq->count >= MAX_SKB_FRAGS + 1)
1334 netif_tx_wake_queue(nq);
1335 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001336}
1337
willy tarreau8ec2cd42014-01-16 08:20:16 +01001338static void *mvneta_frag_alloc(const struct mvneta_port *pp)
1339{
1340 if (likely(pp->frag_size <= PAGE_SIZE))
1341 return netdev_alloc_frag(pp->frag_size);
1342 else
1343 return kmalloc(pp->frag_size, GFP_ATOMIC);
1344}
1345
1346static void mvneta_frag_free(const struct mvneta_port *pp, void *data)
1347{
1348 if (likely(pp->frag_size <= PAGE_SIZE))
1349 put_page(virt_to_head_page(data));
1350 else
1351 kfree(data);
1352}
1353
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001354/* Refill processing */
1355static int mvneta_rx_refill(struct mvneta_port *pp,
1356 struct mvneta_rx_desc *rx_desc)
1357
1358{
1359 dma_addr_t phys_addr;
willy tarreau8ec2cd42014-01-16 08:20:16 +01001360 void *data;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001361
willy tarreau8ec2cd42014-01-16 08:20:16 +01001362 data = mvneta_frag_alloc(pp);
1363 if (!data)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001364 return -ENOMEM;
1365
willy tarreau8ec2cd42014-01-16 08:20:16 +01001366 phys_addr = dma_map_single(pp->dev->dev.parent, data,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001367 MVNETA_RX_BUF_SIZE(pp->pkt_size),
1368 DMA_FROM_DEVICE);
1369 if (unlikely(dma_mapping_error(pp->dev->dev.parent, phys_addr))) {
willy tarreau8ec2cd42014-01-16 08:20:16 +01001370 mvneta_frag_free(pp, data);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001371 return -ENOMEM;
1372 }
1373
willy tarreau8ec2cd42014-01-16 08:20:16 +01001374 mvneta_rx_desc_fill(rx_desc, phys_addr, (u32)data);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001375 return 0;
1376}
1377
1378/* Handle tx checksum */
1379static u32 mvneta_skb_tx_csum(struct mvneta_port *pp, struct sk_buff *skb)
1380{
1381 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1382 int ip_hdr_len = 0;
1383 u8 l4_proto;
1384
1385 if (skb->protocol == htons(ETH_P_IP)) {
1386 struct iphdr *ip4h = ip_hdr(skb);
1387
1388 /* Calculate IPv4 checksum and L4 checksum */
1389 ip_hdr_len = ip4h->ihl;
1390 l4_proto = ip4h->protocol;
1391 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1392 struct ipv6hdr *ip6h = ipv6_hdr(skb);
1393
1394 /* Read l4_protocol from one of IPv6 extra headers */
1395 if (skb_network_header_len(skb) > 0)
1396 ip_hdr_len = (skb_network_header_len(skb) >> 2);
1397 l4_proto = ip6h->nexthdr;
1398 } else
1399 return MVNETA_TX_L4_CSUM_NOT;
1400
1401 return mvneta_txq_desc_csum(skb_network_offset(skb),
1402 skb->protocol, ip_hdr_len, l4_proto);
1403 }
1404
1405 return MVNETA_TX_L4_CSUM_NOT;
1406}
1407
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001408/* Returns rx queue pointer (find last set bit) according to causeRxTx
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001409 * value
1410 */
1411static struct mvneta_rx_queue *mvneta_rx_policy(struct mvneta_port *pp,
1412 u32 cause)
1413{
1414 int queue = fls(cause >> 8) - 1;
1415
1416 return (queue < 0 || queue >= rxq_number) ? NULL : &pp->rxqs[queue];
1417}
1418
1419/* Drop packets received by the RXQ and free buffers */
1420static void mvneta_rxq_drop_pkts(struct mvneta_port *pp,
1421 struct mvneta_rx_queue *rxq)
1422{
1423 int rx_done, i;
1424
1425 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
1426 for (i = 0; i < rxq->size; i++) {
1427 struct mvneta_rx_desc *rx_desc = rxq->descs + i;
willy tarreau8ec2cd42014-01-16 08:20:16 +01001428 void *data = (void *)rx_desc->buf_cookie;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001429
willy tarreau8ec2cd42014-01-16 08:20:16 +01001430 mvneta_frag_free(pp, data);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001431 dma_unmap_single(pp->dev->dev.parent, rx_desc->buf_phys_addr,
Ezequiel Garciaa328f3a2013-12-05 13:35:37 -03001432 MVNETA_RX_BUF_SIZE(pp->pkt_size), DMA_FROM_DEVICE);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001433 }
1434
1435 if (rx_done)
1436 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
1437}
1438
1439/* Main rx processing */
1440static int mvneta_rx(struct mvneta_port *pp, int rx_todo,
1441 struct mvneta_rx_queue *rxq)
1442{
1443 struct net_device *dev = pp->dev;
1444 int rx_done, rx_filled;
willy tarreaudc4277d2014-01-16 08:20:07 +01001445 u32 rcvd_pkts = 0;
1446 u32 rcvd_bytes = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001447
1448 /* Get number of received packets */
1449 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
1450
1451 if (rx_todo > rx_done)
1452 rx_todo = rx_done;
1453
1454 rx_done = 0;
1455 rx_filled = 0;
1456
1457 /* Fairness NAPI loop */
1458 while (rx_done < rx_todo) {
1459 struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
1460 struct sk_buff *skb;
willy tarreau8ec2cd42014-01-16 08:20:16 +01001461 unsigned char *data;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001462 u32 rx_status;
1463 int rx_bytes, err;
1464
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001465 rx_done++;
1466 rx_filled++;
1467 rx_status = rx_desc->status;
willy tarreauf19fadf2014-01-16 08:20:17 +01001468 rx_bytes = rx_desc->data_size - (ETH_FCS_LEN + MVNETA_MH_SIZE);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001469 data = (unsigned char *)rx_desc->buf_cookie;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001470
willy tarreau54282132014-01-16 08:20:14 +01001471 if (!mvneta_rxq_desc_is_first_last(rx_status) ||
willy tarreauf19fadf2014-01-16 08:20:17 +01001472 (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
1473 err_drop_frame:
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001474 dev->stats.rx_errors++;
1475 mvneta_rx_error(pp, rx_desc);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001476 /* leave the descriptor untouched */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001477 continue;
1478 }
1479
willy tarreauf19fadf2014-01-16 08:20:17 +01001480 if (rx_bytes <= rx_copybreak) {
1481 /* better copy a small frame and not unmap the DMA region */
1482 skb = netdev_alloc_skb_ip_align(dev, rx_bytes);
1483 if (unlikely(!skb))
1484 goto err_drop_frame;
1485
1486 dma_sync_single_range_for_cpu(dev->dev.parent,
1487 rx_desc->buf_phys_addr,
1488 MVNETA_MH_SIZE + NET_SKB_PAD,
1489 rx_bytes,
1490 DMA_FROM_DEVICE);
1491 memcpy(skb_put(skb, rx_bytes),
1492 data + MVNETA_MH_SIZE + NET_SKB_PAD,
1493 rx_bytes);
1494
1495 skb->protocol = eth_type_trans(skb, dev);
1496 mvneta_rx_csum(pp, rx_status, skb);
1497 napi_gro_receive(&pp->napi, skb);
1498
1499 rcvd_pkts++;
1500 rcvd_bytes += rx_bytes;
1501
1502 /* leave the descriptor and buffer untouched */
1503 continue;
1504 }
1505
1506 skb = build_skb(data, pp->frag_size > PAGE_SIZE ? 0 : pp->frag_size);
1507 if (!skb)
1508 goto err_drop_frame;
1509
1510 dma_unmap_single(dev->dev.parent, rx_desc->buf_phys_addr,
Ezequiel Garciaa328f3a2013-12-05 13:35:37 -03001511 MVNETA_RX_BUF_SIZE(pp->pkt_size), DMA_FROM_DEVICE);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001512
willy tarreaudc4277d2014-01-16 08:20:07 +01001513 rcvd_pkts++;
1514 rcvd_bytes += rx_bytes;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001515
1516 /* Linux processing */
willy tarreau8ec2cd42014-01-16 08:20:16 +01001517 skb_reserve(skb, MVNETA_MH_SIZE + NET_SKB_PAD);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001518 skb_put(skb, rx_bytes);
1519
1520 skb->protocol = eth_type_trans(skb, dev);
1521
willy tarreau54282132014-01-16 08:20:14 +01001522 mvneta_rx_csum(pp, rx_status, skb);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001523
1524 napi_gro_receive(&pp->napi, skb);
1525
1526 /* Refill processing */
1527 err = mvneta_rx_refill(pp, rx_desc);
1528 if (err) {
willy tarreauf19fadf2014-01-16 08:20:17 +01001529 netdev_err(dev, "Linux processing - Can't refill\n");
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001530 rxq->missed++;
1531 rx_filled--;
1532 }
1533 }
1534
willy tarreaudc4277d2014-01-16 08:20:07 +01001535 if (rcvd_pkts) {
willy tarreau74c41b02014-01-16 08:20:08 +01001536 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
1537
1538 u64_stats_update_begin(&stats->syncp);
1539 stats->rx_packets += rcvd_pkts;
1540 stats->rx_bytes += rcvd_bytes;
1541 u64_stats_update_end(&stats->syncp);
willy tarreaudc4277d2014-01-16 08:20:07 +01001542 }
1543
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001544 /* Update rxq management counters */
1545 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_filled);
1546
1547 return rx_done;
1548}
1549
1550/* Handle tx fragmentation processing */
1551static int mvneta_tx_frag_process(struct mvneta_port *pp, struct sk_buff *skb,
1552 struct mvneta_tx_queue *txq)
1553{
1554 struct mvneta_tx_desc *tx_desc;
1555 int i;
1556
1557 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1558 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1559 void *addr = page_address(frag->page.p) + frag->page_offset;
1560
1561 tx_desc = mvneta_txq_next_desc_get(txq);
1562 tx_desc->data_size = frag->size;
1563
1564 tx_desc->buf_phys_addr =
1565 dma_map_single(pp->dev->dev.parent, addr,
1566 tx_desc->data_size, DMA_TO_DEVICE);
1567
1568 if (dma_mapping_error(pp->dev->dev.parent,
1569 tx_desc->buf_phys_addr)) {
1570 mvneta_txq_desc_put(txq);
1571 goto error;
1572 }
1573
1574 if (i == (skb_shinfo(skb)->nr_frags - 1)) {
1575 /* Last descriptor */
1576 tx_desc->command = MVNETA_TXD_L_DESC | MVNETA_TXD_Z_PAD;
1577
1578 txq->tx_skb[txq->txq_put_index] = skb;
1579
1580 mvneta_txq_inc_put(txq);
1581 } else {
1582 /* Descriptor in the middle: Not First, Not Last */
1583 tx_desc->command = 0;
1584
1585 txq->tx_skb[txq->txq_put_index] = NULL;
1586 mvneta_txq_inc_put(txq);
1587 }
1588 }
1589
1590 return 0;
1591
1592error:
1593 /* Release all descriptors that were used to map fragments of
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001594 * this packet, as well as the corresponding DMA mappings
1595 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001596 for (i = i - 1; i >= 0; i--) {
1597 tx_desc = txq->descs + i;
1598 dma_unmap_single(pp->dev->dev.parent,
1599 tx_desc->buf_phys_addr,
1600 tx_desc->data_size,
1601 DMA_TO_DEVICE);
1602 mvneta_txq_desc_put(txq);
1603 }
1604
1605 return -ENOMEM;
1606}
1607
1608/* Main tx processing */
1609static int mvneta_tx(struct sk_buff *skb, struct net_device *dev)
1610{
1611 struct mvneta_port *pp = netdev_priv(dev);
Willy Tarreauee40a112013-04-11 23:00:37 +02001612 u16 txq_id = skb_get_queue_mapping(skb);
1613 struct mvneta_tx_queue *txq = &pp->txqs[txq_id];
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001614 struct mvneta_tx_desc *tx_desc;
1615 struct netdev_queue *nq;
1616 int frags = 0;
1617 u32 tx_cmd;
1618
1619 if (!netif_running(dev))
1620 goto out;
1621
1622 frags = skb_shinfo(skb)->nr_frags + 1;
Willy Tarreauee40a112013-04-11 23:00:37 +02001623 nq = netdev_get_tx_queue(dev, txq_id);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001624
1625 /* Get a descriptor for the first part of the packet */
1626 tx_desc = mvneta_txq_next_desc_get(txq);
1627
1628 tx_cmd = mvneta_skb_tx_csum(pp, skb);
1629
1630 tx_desc->data_size = skb_headlen(skb);
1631
1632 tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, skb->data,
1633 tx_desc->data_size,
1634 DMA_TO_DEVICE);
1635 if (unlikely(dma_mapping_error(dev->dev.parent,
1636 tx_desc->buf_phys_addr))) {
1637 mvneta_txq_desc_put(txq);
1638 frags = 0;
1639 goto out;
1640 }
1641
1642 if (frags == 1) {
1643 /* First and Last descriptor */
1644 tx_cmd |= MVNETA_TXD_FLZ_DESC;
1645 tx_desc->command = tx_cmd;
1646 txq->tx_skb[txq->txq_put_index] = skb;
1647 mvneta_txq_inc_put(txq);
1648 } else {
1649 /* First but not Last */
1650 tx_cmd |= MVNETA_TXD_F_DESC;
1651 txq->tx_skb[txq->txq_put_index] = NULL;
1652 mvneta_txq_inc_put(txq);
1653 tx_desc->command = tx_cmd;
1654 /* Continue with other skb fragments */
1655 if (mvneta_tx_frag_process(pp, skb, txq)) {
1656 dma_unmap_single(dev->dev.parent,
1657 tx_desc->buf_phys_addr,
1658 tx_desc->data_size,
1659 DMA_TO_DEVICE);
1660 mvneta_txq_desc_put(txq);
1661 frags = 0;
1662 goto out;
1663 }
1664 }
1665
1666 txq->count += frags;
1667 mvneta_txq_pend_desc_add(pp, txq, frags);
1668
1669 if (txq->size - txq->count < MAX_SKB_FRAGS + 1)
1670 netif_tx_stop_queue(nq);
1671
1672out:
1673 if (frags > 0) {
willy tarreau74c41b02014-01-16 08:20:08 +01001674 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001675
willy tarreau74c41b02014-01-16 08:20:08 +01001676 u64_stats_update_begin(&stats->syncp);
1677 stats->tx_packets++;
1678 stats->tx_bytes += skb->len;
1679 u64_stats_update_end(&stats->syncp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001680 } else {
1681 dev->stats.tx_dropped++;
1682 dev_kfree_skb_any(skb);
1683 }
1684
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001685 return NETDEV_TX_OK;
1686}
1687
1688
1689/* Free tx resources, when resetting a port */
1690static void mvneta_txq_done_force(struct mvneta_port *pp,
1691 struct mvneta_tx_queue *txq)
1692
1693{
1694 int tx_done = txq->count;
1695
1696 mvneta_txq_bufs_free(pp, txq, tx_done);
1697
1698 /* reset txq */
1699 txq->count = 0;
1700 txq->txq_put_index = 0;
1701 txq->txq_get_index = 0;
1702}
1703
willy tarreau6c498972014-01-16 08:20:12 +01001704/* Handle tx done - called in softirq context. The <cause_tx_done> argument
1705 * must be a valid cause according to MVNETA_TXQ_INTR_MASK_ALL.
1706 */
Arnaud Ebalard0713a862014-01-16 08:20:18 +01001707static void mvneta_tx_done_gbe(struct mvneta_port *pp, u32 cause_tx_done)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001708{
1709 struct mvneta_tx_queue *txq;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001710 struct netdev_queue *nq;
1711
willy tarreau6c498972014-01-16 08:20:12 +01001712 while (cause_tx_done) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001713 txq = mvneta_tx_done_policy(pp, cause_tx_done);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001714
1715 nq = netdev_get_tx_queue(pp->dev, txq->id);
1716 __netif_tx_lock(nq, smp_processor_id());
1717
Arnaud Ebalard0713a862014-01-16 08:20:18 +01001718 if (txq->count)
1719 mvneta_txq_done(pp, txq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001720
1721 __netif_tx_unlock(nq);
1722 cause_tx_done &= ~((1 << txq->id));
1723 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001724}
1725
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001726/* Compute crc8 of the specified address, using a unique algorithm ,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001727 * according to hw spec, different than generic crc8 algorithm
1728 */
1729static int mvneta_addr_crc(unsigned char *addr)
1730{
1731 int crc = 0;
1732 int i;
1733
1734 for (i = 0; i < ETH_ALEN; i++) {
1735 int j;
1736
1737 crc = (crc ^ addr[i]) << 8;
1738 for (j = 7; j >= 0; j--) {
1739 if (crc & (0x100 << j))
1740 crc ^= 0x107 << j;
1741 }
1742 }
1743
1744 return crc;
1745}
1746
1747/* This method controls the net device special MAC multicast support.
1748 * The Special Multicast Table for MAC addresses supports MAC of the form
1749 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
1750 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
1751 * Table entries in the DA-Filter table. This method set the Special
1752 * Multicast Table appropriate entry.
1753 */
1754static void mvneta_set_special_mcast_addr(struct mvneta_port *pp,
1755 unsigned char last_byte,
1756 int queue)
1757{
1758 unsigned int smc_table_reg;
1759 unsigned int tbl_offset;
1760 unsigned int reg_offset;
1761
1762 /* Register offset from SMC table base */
1763 tbl_offset = (last_byte / 4);
1764 /* Entry offset within the above reg */
1765 reg_offset = last_byte % 4;
1766
1767 smc_table_reg = mvreg_read(pp, (MVNETA_DA_FILT_SPEC_MCAST
1768 + tbl_offset * 4));
1769
1770 if (queue == -1)
1771 smc_table_reg &= ~(0xff << (8 * reg_offset));
1772 else {
1773 smc_table_reg &= ~(0xff << (8 * reg_offset));
1774 smc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
1775 }
1776
1777 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + tbl_offset * 4,
1778 smc_table_reg);
1779}
1780
1781/* This method controls the network device Other MAC multicast support.
1782 * The Other Multicast Table is used for multicast of another type.
1783 * A CRC-8 is used as an index to the Other Multicast Table entries
1784 * in the DA-Filter table.
1785 * The method gets the CRC-8 value from the calling routine and
1786 * sets the Other Multicast Table appropriate entry according to the
1787 * specified CRC-8 .
1788 */
1789static void mvneta_set_other_mcast_addr(struct mvneta_port *pp,
1790 unsigned char crc8,
1791 int queue)
1792{
1793 unsigned int omc_table_reg;
1794 unsigned int tbl_offset;
1795 unsigned int reg_offset;
1796
1797 tbl_offset = (crc8 / 4) * 4; /* Register offset from OMC table base */
1798 reg_offset = crc8 % 4; /* Entry offset within the above reg */
1799
1800 omc_table_reg = mvreg_read(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset);
1801
1802 if (queue == -1) {
1803 /* Clear accepts frame bit at specified Other DA table entry */
1804 omc_table_reg &= ~(0xff << (8 * reg_offset));
1805 } else {
1806 omc_table_reg &= ~(0xff << (8 * reg_offset));
1807 omc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
1808 }
1809
1810 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset, omc_table_reg);
1811}
1812
1813/* The network device supports multicast using two tables:
1814 * 1) Special Multicast Table for MAC addresses of the form
1815 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
1816 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
1817 * Table entries in the DA-Filter table.
1818 * 2) Other Multicast Table for multicast of another type. A CRC-8 value
1819 * is used as an index to the Other Multicast Table entries in the
1820 * DA-Filter table.
1821 */
1822static int mvneta_mcast_addr_set(struct mvneta_port *pp, unsigned char *p_addr,
1823 int queue)
1824{
1825 unsigned char crc_result = 0;
1826
1827 if (memcmp(p_addr, "\x01\x00\x5e\x00\x00", 5) == 0) {
1828 mvneta_set_special_mcast_addr(pp, p_addr[5], queue);
1829 return 0;
1830 }
1831
1832 crc_result = mvneta_addr_crc(p_addr);
1833 if (queue == -1) {
1834 if (pp->mcast_count[crc_result] == 0) {
1835 netdev_info(pp->dev, "No valid Mcast for crc8=0x%02x\n",
1836 crc_result);
1837 return -EINVAL;
1838 }
1839
1840 pp->mcast_count[crc_result]--;
1841 if (pp->mcast_count[crc_result] != 0) {
1842 netdev_info(pp->dev,
1843 "After delete there are %d valid Mcast for crc8=0x%02x\n",
1844 pp->mcast_count[crc_result], crc_result);
1845 return -EINVAL;
1846 }
1847 } else
1848 pp->mcast_count[crc_result]++;
1849
1850 mvneta_set_other_mcast_addr(pp, crc_result, queue);
1851
1852 return 0;
1853}
1854
1855/* Configure Fitering mode of Ethernet port */
1856static void mvneta_rx_unicast_promisc_set(struct mvneta_port *pp,
1857 int is_promisc)
1858{
1859 u32 port_cfg_reg, val;
1860
1861 port_cfg_reg = mvreg_read(pp, MVNETA_PORT_CONFIG);
1862
1863 val = mvreg_read(pp, MVNETA_TYPE_PRIO);
1864
1865 /* Set / Clear UPM bit in port configuration register */
1866 if (is_promisc) {
1867 /* Accept all Unicast addresses */
1868 port_cfg_reg |= MVNETA_UNI_PROMISC_MODE;
1869 val |= MVNETA_FORCE_UNI;
1870 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, 0xffff);
1871 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, 0xffffffff);
1872 } else {
1873 /* Reject all Unicast addresses */
1874 port_cfg_reg &= ~MVNETA_UNI_PROMISC_MODE;
1875 val &= ~MVNETA_FORCE_UNI;
1876 }
1877
1878 mvreg_write(pp, MVNETA_PORT_CONFIG, port_cfg_reg);
1879 mvreg_write(pp, MVNETA_TYPE_PRIO, val);
1880}
1881
1882/* register unicast and multicast addresses */
1883static void mvneta_set_rx_mode(struct net_device *dev)
1884{
1885 struct mvneta_port *pp = netdev_priv(dev);
1886 struct netdev_hw_addr *ha;
1887
1888 if (dev->flags & IFF_PROMISC) {
1889 /* Accept all: Multicast + Unicast */
1890 mvneta_rx_unicast_promisc_set(pp, 1);
1891 mvneta_set_ucast_table(pp, rxq_def);
1892 mvneta_set_special_mcast_table(pp, rxq_def);
1893 mvneta_set_other_mcast_table(pp, rxq_def);
1894 } else {
1895 /* Accept single Unicast */
1896 mvneta_rx_unicast_promisc_set(pp, 0);
1897 mvneta_set_ucast_table(pp, -1);
1898 mvneta_mac_addr_set(pp, dev->dev_addr, rxq_def);
1899
1900 if (dev->flags & IFF_ALLMULTI) {
1901 /* Accept all multicast */
1902 mvneta_set_special_mcast_table(pp, rxq_def);
1903 mvneta_set_other_mcast_table(pp, rxq_def);
1904 } else {
1905 /* Accept only initialized multicast */
1906 mvneta_set_special_mcast_table(pp, -1);
1907 mvneta_set_other_mcast_table(pp, -1);
1908
1909 if (!netdev_mc_empty(dev)) {
1910 netdev_for_each_mc_addr(ha, dev) {
1911 mvneta_mcast_addr_set(pp, ha->addr,
1912 rxq_def);
1913 }
1914 }
1915 }
1916 }
1917}
1918
1919/* Interrupt handling - the callback for request_irq() */
1920static irqreturn_t mvneta_isr(int irq, void *dev_id)
1921{
1922 struct mvneta_port *pp = (struct mvneta_port *)dev_id;
1923
1924 /* Mask all interrupts */
1925 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
1926
1927 napi_schedule(&pp->napi);
1928
1929 return IRQ_HANDLED;
1930}
1931
1932/* NAPI handler
1933 * Bits 0 - 7 of the causeRxTx register indicate that are transmitted
1934 * packets on the corresponding TXQ (Bit 0 is for TX queue 1).
1935 * Bits 8 -15 of the cause Rx Tx register indicate that are received
1936 * packets on the corresponding RXQ (Bit 8 is for RX queue 0).
1937 * Each CPU has its own causeRxTx register
1938 */
1939static int mvneta_poll(struct napi_struct *napi, int budget)
1940{
1941 int rx_done = 0;
1942 u32 cause_rx_tx;
1943 unsigned long flags;
1944 struct mvneta_port *pp = netdev_priv(napi->dev);
1945
1946 if (!netif_running(pp->dev)) {
1947 napi_complete(napi);
1948 return rx_done;
1949 }
1950
1951 /* Read cause register */
1952 cause_rx_tx = mvreg_read(pp, MVNETA_INTR_NEW_CAUSE) &
willy tarreau71f6d1b2014-01-16 08:20:11 +01001953 (MVNETA_RX_INTR_MASK(rxq_number) | MVNETA_TX_INTR_MASK(txq_number));
1954
1955 /* Release Tx descriptors */
1956 if (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL) {
Arnaud Ebalard0713a862014-01-16 08:20:18 +01001957 mvneta_tx_done_gbe(pp, (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL));
willy tarreau71f6d1b2014-01-16 08:20:11 +01001958 cause_rx_tx &= ~MVNETA_TX_INTR_MASK_ALL;
1959 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001960
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001961 /* For the case where the last mvneta_poll did not process all
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001962 * RX packets
1963 */
1964 cause_rx_tx |= pp->cause_rx_tx;
1965 if (rxq_number > 1) {
willy tarreau71f6d1b2014-01-16 08:20:11 +01001966 while ((cause_rx_tx & MVNETA_RX_INTR_MASK_ALL) && (budget > 0)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001967 int count;
1968 struct mvneta_rx_queue *rxq;
1969 /* get rx queue number from cause_rx_tx */
1970 rxq = mvneta_rx_policy(pp, cause_rx_tx);
1971 if (!rxq)
1972 break;
1973
1974 /* process the packet in that rx queue */
1975 count = mvneta_rx(pp, budget, rxq);
1976 rx_done += count;
1977 budget -= count;
1978 if (budget > 0) {
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001979 /* set off the rx bit of the
1980 * corresponding bit in the cause rx
1981 * tx register, so that next iteration
1982 * will find the next rx queue where
1983 * packets are received on
1984 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001985 cause_rx_tx &= ~((1 << rxq->id) << 8);
1986 }
1987 }
1988 } else {
1989 rx_done = mvneta_rx(pp, budget, &pp->rxqs[rxq_def]);
1990 budget -= rx_done;
1991 }
1992
1993 if (budget > 0) {
1994 cause_rx_tx = 0;
1995 napi_complete(napi);
1996 local_irq_save(flags);
1997 mvreg_write(pp, MVNETA_INTR_NEW_MASK,
willy tarreau71f6d1b2014-01-16 08:20:11 +01001998 MVNETA_RX_INTR_MASK(rxq_number) | MVNETA_TX_INTR_MASK(txq_number));
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001999 local_irq_restore(flags);
2000 }
2001
2002 pp->cause_rx_tx = cause_rx_tx;
2003 return rx_done;
2004}
2005
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002006/* Handle rxq fill: allocates rxq skbs; called when initializing a port */
2007static int mvneta_rxq_fill(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
2008 int num)
2009{
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002010 int i;
2011
2012 for (i = 0; i < num; i++) {
willy tarreaua1a65ab2014-01-16 08:20:13 +01002013 memset(rxq->descs + i, 0, sizeof(struct mvneta_rx_desc));
2014 if (mvneta_rx_refill(pp, rxq->descs + i) != 0) {
2015 netdev_err(pp->dev, "%s:rxq %d, %d of %d buffs filled\n",
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002016 __func__, rxq->id, i, num);
2017 break;
2018 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002019 }
2020
2021 /* Add this number of RX descriptors as non occupied (ready to
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002022 * get packets)
2023 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002024 mvneta_rxq_non_occup_desc_add(pp, rxq, i);
2025
2026 return i;
2027}
2028
2029/* Free all packets pending transmit from all TXQs and reset TX port */
2030static void mvneta_tx_reset(struct mvneta_port *pp)
2031{
2032 int queue;
2033
2034 /* free the skb's in the hal tx ring */
2035 for (queue = 0; queue < txq_number; queue++)
2036 mvneta_txq_done_force(pp, &pp->txqs[queue]);
2037
2038 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
2039 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
2040}
2041
2042static void mvneta_rx_reset(struct mvneta_port *pp)
2043{
2044 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
2045 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
2046}
2047
2048/* Rx/Tx queue initialization/cleanup methods */
2049
2050/* Create a specified RX queue */
2051static int mvneta_rxq_init(struct mvneta_port *pp,
2052 struct mvneta_rx_queue *rxq)
2053
2054{
2055 rxq->size = pp->rx_ring_size;
2056
2057 /* Allocate memory for RX descriptors */
2058 rxq->descs = dma_alloc_coherent(pp->dev->dev.parent,
2059 rxq->size * MVNETA_DESC_ALIGNED_SIZE,
2060 &rxq->descs_phys, GFP_KERNEL);
Joe Perchesd0320f72013-03-14 13:07:21 +00002061 if (rxq->descs == NULL)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002062 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002063
2064 BUG_ON(rxq->descs !=
2065 PTR_ALIGN(rxq->descs, MVNETA_CPU_D_CACHE_LINE_SIZE));
2066
2067 rxq->last_desc = rxq->size - 1;
2068
2069 /* Set Rx descriptors queue starting address */
2070 mvreg_write(pp, MVNETA_RXQ_BASE_ADDR_REG(rxq->id), rxq->descs_phys);
2071 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), rxq->size);
2072
2073 /* Set Offset */
2074 mvneta_rxq_offset_set(pp, rxq, NET_SKB_PAD);
2075
2076 /* Set coalescing pkts and time */
2077 mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
2078 mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
2079
2080 /* Fill RXQ with buffers from RX pool */
2081 mvneta_rxq_buf_size_set(pp, rxq, MVNETA_RX_BUF_SIZE(pp->pkt_size));
2082 mvneta_rxq_bm_disable(pp, rxq);
2083 mvneta_rxq_fill(pp, rxq, rxq->size);
2084
2085 return 0;
2086}
2087
2088/* Cleanup Rx queue */
2089static void mvneta_rxq_deinit(struct mvneta_port *pp,
2090 struct mvneta_rx_queue *rxq)
2091{
2092 mvneta_rxq_drop_pkts(pp, rxq);
2093
2094 if (rxq->descs)
2095 dma_free_coherent(pp->dev->dev.parent,
2096 rxq->size * MVNETA_DESC_ALIGNED_SIZE,
2097 rxq->descs,
2098 rxq->descs_phys);
2099
2100 rxq->descs = NULL;
2101 rxq->last_desc = 0;
2102 rxq->next_desc_to_proc = 0;
2103 rxq->descs_phys = 0;
2104}
2105
2106/* Create and initialize a tx queue */
2107static int mvneta_txq_init(struct mvneta_port *pp,
2108 struct mvneta_tx_queue *txq)
2109{
2110 txq->size = pp->tx_ring_size;
2111
2112 /* Allocate memory for TX descriptors */
2113 txq->descs = dma_alloc_coherent(pp->dev->dev.parent,
2114 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2115 &txq->descs_phys, GFP_KERNEL);
Joe Perchesd0320f72013-03-14 13:07:21 +00002116 if (txq->descs == NULL)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002117 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002118
2119 /* Make sure descriptor address is cache line size aligned */
2120 BUG_ON(txq->descs !=
2121 PTR_ALIGN(txq->descs, MVNETA_CPU_D_CACHE_LINE_SIZE));
2122
2123 txq->last_desc = txq->size - 1;
2124
2125 /* Set maximum bandwidth for enabled TXQs */
2126 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0x03ffffff);
2127 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0x3fffffff);
2128
2129 /* Set Tx descriptors queue starting address */
2130 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), txq->descs_phys);
2131 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), txq->size);
2132
2133 txq->tx_skb = kmalloc(txq->size * sizeof(*txq->tx_skb), GFP_KERNEL);
2134 if (txq->tx_skb == NULL) {
2135 dma_free_coherent(pp->dev->dev.parent,
2136 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2137 txq->descs, txq->descs_phys);
2138 return -ENOMEM;
2139 }
2140 mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
2141
2142 return 0;
2143}
2144
2145/* Free allocated resources when mvneta_txq_init() fails to allocate memory*/
2146static void mvneta_txq_deinit(struct mvneta_port *pp,
2147 struct mvneta_tx_queue *txq)
2148{
2149 kfree(txq->tx_skb);
2150
2151 if (txq->descs)
2152 dma_free_coherent(pp->dev->dev.parent,
2153 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2154 txq->descs, txq->descs_phys);
2155
2156 txq->descs = NULL;
2157 txq->last_desc = 0;
2158 txq->next_desc_to_proc = 0;
2159 txq->descs_phys = 0;
2160
2161 /* Set minimum bandwidth for disabled TXQs */
2162 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0);
2163 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0);
2164
2165 /* Set Tx descriptors queue starting address and size */
2166 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), 0);
2167 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), 0);
2168}
2169
2170/* Cleanup all Tx queues */
2171static void mvneta_cleanup_txqs(struct mvneta_port *pp)
2172{
2173 int queue;
2174
2175 for (queue = 0; queue < txq_number; queue++)
2176 mvneta_txq_deinit(pp, &pp->txqs[queue]);
2177}
2178
2179/* Cleanup all Rx queues */
2180static void mvneta_cleanup_rxqs(struct mvneta_port *pp)
2181{
2182 int queue;
2183
2184 for (queue = 0; queue < rxq_number; queue++)
2185 mvneta_rxq_deinit(pp, &pp->rxqs[queue]);
2186}
2187
2188
2189/* Init all Rx queues */
2190static int mvneta_setup_rxqs(struct mvneta_port *pp)
2191{
2192 int queue;
2193
2194 for (queue = 0; queue < rxq_number; queue++) {
2195 int err = mvneta_rxq_init(pp, &pp->rxqs[queue]);
2196 if (err) {
2197 netdev_err(pp->dev, "%s: can't create rxq=%d\n",
2198 __func__, queue);
2199 mvneta_cleanup_rxqs(pp);
2200 return err;
2201 }
2202 }
2203
2204 return 0;
2205}
2206
2207/* Init all tx queues */
2208static int mvneta_setup_txqs(struct mvneta_port *pp)
2209{
2210 int queue;
2211
2212 for (queue = 0; queue < txq_number; queue++) {
2213 int err = mvneta_txq_init(pp, &pp->txqs[queue]);
2214 if (err) {
2215 netdev_err(pp->dev, "%s: can't create txq=%d\n",
2216 __func__, queue);
2217 mvneta_cleanup_txqs(pp);
2218 return err;
2219 }
2220 }
2221
2222 return 0;
2223}
2224
2225static void mvneta_start_dev(struct mvneta_port *pp)
2226{
2227 mvneta_max_rx_size_set(pp, pp->pkt_size);
2228 mvneta_txq_max_tx_size_set(pp, pp->pkt_size);
2229
2230 /* start the Rx/Tx activity */
2231 mvneta_port_enable(pp);
2232
2233 /* Enable polling on the port */
2234 napi_enable(&pp->napi);
2235
2236 /* Unmask interrupts */
2237 mvreg_write(pp, MVNETA_INTR_NEW_MASK,
willy tarreau71f6d1b2014-01-16 08:20:11 +01002238 MVNETA_RX_INTR_MASK(rxq_number) | MVNETA_TX_INTR_MASK(txq_number));
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002239
2240 phy_start(pp->phy_dev);
2241 netif_tx_start_all_queues(pp->dev);
2242}
2243
2244static void mvneta_stop_dev(struct mvneta_port *pp)
2245{
2246 phy_stop(pp->phy_dev);
2247
2248 napi_disable(&pp->napi);
2249
2250 netif_carrier_off(pp->dev);
2251
2252 mvneta_port_down(pp);
2253 netif_tx_stop_all_queues(pp->dev);
2254
2255 /* Stop the port activity */
2256 mvneta_port_disable(pp);
2257
2258 /* Clear all ethernet port interrupts */
2259 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
2260 mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0);
2261
2262 /* Mask all ethernet port interrupts */
2263 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
2264 mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0);
2265 mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0);
2266
2267 mvneta_tx_reset(pp);
2268 mvneta_rx_reset(pp);
2269}
2270
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002271/* Return positive if MTU is valid */
2272static int mvneta_check_mtu_valid(struct net_device *dev, int mtu)
2273{
2274 if (mtu < 68) {
2275 netdev_err(dev, "cannot change mtu to less than 68\n");
2276 return -EINVAL;
2277 }
2278
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002279 /* 9676 == 9700 - 20 and rounding to 8 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002280 if (mtu > 9676) {
2281 netdev_info(dev, "Illegal MTU value %d, round to 9676\n", mtu);
2282 mtu = 9676;
2283 }
2284
2285 if (!IS_ALIGNED(MVNETA_RX_PKT_SIZE(mtu), 8)) {
2286 netdev_info(dev, "Illegal MTU value %d, rounding to %d\n",
2287 mtu, ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8));
2288 mtu = ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8);
2289 }
2290
2291 return mtu;
2292}
2293
2294/* Change the device mtu */
2295static int mvneta_change_mtu(struct net_device *dev, int mtu)
2296{
2297 struct mvneta_port *pp = netdev_priv(dev);
2298 int ret;
2299
2300 mtu = mvneta_check_mtu_valid(dev, mtu);
2301 if (mtu < 0)
2302 return -EINVAL;
2303
2304 dev->mtu = mtu;
2305
2306 if (!netif_running(dev))
2307 return 0;
2308
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002309 /* The interface is running, so we have to force a
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002310 * reallocation of the RXQs
2311 */
2312 mvneta_stop_dev(pp);
2313
2314 mvneta_cleanup_txqs(pp);
2315 mvneta_cleanup_rxqs(pp);
2316
2317 pp->pkt_size = MVNETA_RX_PKT_SIZE(pp->dev->mtu);
willy tarreau8ec2cd42014-01-16 08:20:16 +01002318 pp->frag_size = SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(pp->pkt_size)) +
2319 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002320
2321 ret = mvneta_setup_rxqs(pp);
2322 if (ret) {
2323 netdev_err(pp->dev, "unable to setup rxqs after MTU change\n");
2324 return ret;
2325 }
2326
2327 mvneta_setup_txqs(pp);
2328
2329 mvneta_start_dev(pp);
2330 mvneta_port_up(pp);
2331
2332 return 0;
2333}
2334
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00002335/* Get mac address */
2336static void mvneta_get_mac_addr(struct mvneta_port *pp, unsigned char *addr)
2337{
2338 u32 mac_addr_l, mac_addr_h;
2339
2340 mac_addr_l = mvreg_read(pp, MVNETA_MAC_ADDR_LOW);
2341 mac_addr_h = mvreg_read(pp, MVNETA_MAC_ADDR_HIGH);
2342 addr[0] = (mac_addr_h >> 24) & 0xFF;
2343 addr[1] = (mac_addr_h >> 16) & 0xFF;
2344 addr[2] = (mac_addr_h >> 8) & 0xFF;
2345 addr[3] = mac_addr_h & 0xFF;
2346 addr[4] = (mac_addr_l >> 8) & 0xFF;
2347 addr[5] = mac_addr_l & 0xFF;
2348}
2349
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002350/* Handle setting mac address */
2351static int mvneta_set_mac_addr(struct net_device *dev, void *addr)
2352{
2353 struct mvneta_port *pp = netdev_priv(dev);
2354 u8 *mac = addr + 2;
2355 int i;
2356
2357 if (netif_running(dev))
2358 return -EBUSY;
2359
2360 /* Remove previous address table entry */
2361 mvneta_mac_addr_set(pp, dev->dev_addr, -1);
2362
2363 /* Set new addr in hw */
2364 mvneta_mac_addr_set(pp, mac, rxq_def);
2365
2366 /* Set addr in the device */
2367 for (i = 0; i < ETH_ALEN; i++)
2368 dev->dev_addr[i] = mac[i];
2369
2370 return 0;
2371}
2372
2373static void mvneta_adjust_link(struct net_device *ndev)
2374{
2375 struct mvneta_port *pp = netdev_priv(ndev);
2376 struct phy_device *phydev = pp->phy_dev;
2377 int status_change = 0;
2378
2379 if (phydev->link) {
2380 if ((pp->speed != phydev->speed) ||
2381 (pp->duplex != phydev->duplex)) {
2382 u32 val;
2383
2384 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
2385 val &= ~(MVNETA_GMAC_CONFIG_MII_SPEED |
2386 MVNETA_GMAC_CONFIG_GMII_SPEED |
Thomas Petazzoni71408602013-09-04 16:21:18 +02002387 MVNETA_GMAC_CONFIG_FULL_DUPLEX |
2388 MVNETA_GMAC_AN_SPEED_EN |
2389 MVNETA_GMAC_AN_DUPLEX_EN);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002390
2391 if (phydev->duplex)
2392 val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
2393
2394 if (phydev->speed == SPEED_1000)
2395 val |= MVNETA_GMAC_CONFIG_GMII_SPEED;
2396 else
2397 val |= MVNETA_GMAC_CONFIG_MII_SPEED;
2398
2399 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
2400
2401 pp->duplex = phydev->duplex;
2402 pp->speed = phydev->speed;
2403 }
2404 }
2405
2406 if (phydev->link != pp->link) {
2407 if (!phydev->link) {
2408 pp->duplex = -1;
2409 pp->speed = 0;
2410 }
2411
2412 pp->link = phydev->link;
2413 status_change = 1;
2414 }
2415
2416 if (status_change) {
2417 if (phydev->link) {
2418 u32 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
2419 val |= (MVNETA_GMAC_FORCE_LINK_PASS |
2420 MVNETA_GMAC_FORCE_LINK_DOWN);
2421 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
2422 mvneta_port_up(pp);
2423 netdev_info(pp->dev, "link up\n");
2424 } else {
2425 mvneta_port_down(pp);
2426 netdev_info(pp->dev, "link down\n");
2427 }
2428 }
2429}
2430
2431static int mvneta_mdio_probe(struct mvneta_port *pp)
2432{
2433 struct phy_device *phy_dev;
2434
2435 phy_dev = of_phy_connect(pp->dev, pp->phy_node, mvneta_adjust_link, 0,
2436 pp->phy_interface);
2437 if (!phy_dev) {
2438 netdev_err(pp->dev, "could not find the PHY\n");
2439 return -ENODEV;
2440 }
2441
2442 phy_dev->supported &= PHY_GBIT_FEATURES;
2443 phy_dev->advertising = phy_dev->supported;
2444
2445 pp->phy_dev = phy_dev;
2446 pp->link = 0;
2447 pp->duplex = 0;
2448 pp->speed = 0;
2449
2450 return 0;
2451}
2452
2453static void mvneta_mdio_remove(struct mvneta_port *pp)
2454{
2455 phy_disconnect(pp->phy_dev);
2456 pp->phy_dev = NULL;
2457}
2458
2459static int mvneta_open(struct net_device *dev)
2460{
2461 struct mvneta_port *pp = netdev_priv(dev);
2462 int ret;
2463
2464 mvneta_mac_addr_set(pp, dev->dev_addr, rxq_def);
2465
2466 pp->pkt_size = MVNETA_RX_PKT_SIZE(pp->dev->mtu);
willy tarreau8ec2cd42014-01-16 08:20:16 +01002467 pp->frag_size = SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(pp->pkt_size)) +
2468 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002469
2470 ret = mvneta_setup_rxqs(pp);
2471 if (ret)
2472 return ret;
2473
2474 ret = mvneta_setup_txqs(pp);
2475 if (ret)
2476 goto err_cleanup_rxqs;
2477
2478 /* Connect to port interrupt line */
2479 ret = request_irq(pp->dev->irq, mvneta_isr, 0,
2480 MVNETA_DRIVER_NAME, pp);
2481 if (ret) {
2482 netdev_err(pp->dev, "cannot request irq %d\n", pp->dev->irq);
2483 goto err_cleanup_txqs;
2484 }
2485
2486 /* In default link is down */
2487 netif_carrier_off(pp->dev);
2488
2489 ret = mvneta_mdio_probe(pp);
2490 if (ret < 0) {
2491 netdev_err(dev, "cannot probe MDIO bus\n");
2492 goto err_free_irq;
2493 }
2494
2495 mvneta_start_dev(pp);
2496
2497 return 0;
2498
2499err_free_irq:
2500 free_irq(pp->dev->irq, pp);
2501err_cleanup_txqs:
2502 mvneta_cleanup_txqs(pp);
2503err_cleanup_rxqs:
2504 mvneta_cleanup_rxqs(pp);
2505 return ret;
2506}
2507
2508/* Stop the port, free port interrupt line */
2509static int mvneta_stop(struct net_device *dev)
2510{
2511 struct mvneta_port *pp = netdev_priv(dev);
2512
2513 mvneta_stop_dev(pp);
2514 mvneta_mdio_remove(pp);
2515 free_irq(dev->irq, pp);
2516 mvneta_cleanup_rxqs(pp);
2517 mvneta_cleanup_txqs(pp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002518
2519 return 0;
2520}
2521
Thomas Petazzoni15f59452013-09-04 16:26:52 +02002522static int mvneta_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2523{
2524 struct mvneta_port *pp = netdev_priv(dev);
2525 int ret;
2526
2527 if (!pp->phy_dev)
2528 return -ENOTSUPP;
2529
2530 ret = phy_mii_ioctl(pp->phy_dev, ifr, cmd);
2531 if (!ret)
2532 mvneta_adjust_link(dev);
2533
2534 return ret;
2535}
2536
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002537/* Ethtool methods */
2538
2539/* Get settings (phy address, speed) for ethtools */
2540int mvneta_ethtool_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2541{
2542 struct mvneta_port *pp = netdev_priv(dev);
2543
2544 if (!pp->phy_dev)
2545 return -ENODEV;
2546
2547 return phy_ethtool_gset(pp->phy_dev, cmd);
2548}
2549
2550/* Set settings (phy address, speed) for ethtools */
2551int mvneta_ethtool_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2552{
2553 struct mvneta_port *pp = netdev_priv(dev);
2554
2555 if (!pp->phy_dev)
2556 return -ENODEV;
2557
2558 return phy_ethtool_sset(pp->phy_dev, cmd);
2559}
2560
2561/* Set interrupt coalescing for ethtools */
2562static int mvneta_ethtool_set_coalesce(struct net_device *dev,
2563 struct ethtool_coalesce *c)
2564{
2565 struct mvneta_port *pp = netdev_priv(dev);
2566 int queue;
2567
2568 for (queue = 0; queue < rxq_number; queue++) {
2569 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
2570 rxq->time_coal = c->rx_coalesce_usecs;
2571 rxq->pkts_coal = c->rx_max_coalesced_frames;
2572 mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
2573 mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
2574 }
2575
2576 for (queue = 0; queue < txq_number; queue++) {
2577 struct mvneta_tx_queue *txq = &pp->txqs[queue];
2578 txq->done_pkts_coal = c->tx_max_coalesced_frames;
2579 mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
2580 }
2581
2582 return 0;
2583}
2584
2585/* get coalescing for ethtools */
2586static int mvneta_ethtool_get_coalesce(struct net_device *dev,
2587 struct ethtool_coalesce *c)
2588{
2589 struct mvneta_port *pp = netdev_priv(dev);
2590
2591 c->rx_coalesce_usecs = pp->rxqs[0].time_coal;
2592 c->rx_max_coalesced_frames = pp->rxqs[0].pkts_coal;
2593
2594 c->tx_max_coalesced_frames = pp->txqs[0].done_pkts_coal;
2595 return 0;
2596}
2597
2598
2599static void mvneta_ethtool_get_drvinfo(struct net_device *dev,
2600 struct ethtool_drvinfo *drvinfo)
2601{
2602 strlcpy(drvinfo->driver, MVNETA_DRIVER_NAME,
2603 sizeof(drvinfo->driver));
2604 strlcpy(drvinfo->version, MVNETA_DRIVER_VERSION,
2605 sizeof(drvinfo->version));
2606 strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
2607 sizeof(drvinfo->bus_info));
2608}
2609
2610
2611static void mvneta_ethtool_get_ringparam(struct net_device *netdev,
2612 struct ethtool_ringparam *ring)
2613{
2614 struct mvneta_port *pp = netdev_priv(netdev);
2615
2616 ring->rx_max_pending = MVNETA_MAX_RXD;
2617 ring->tx_max_pending = MVNETA_MAX_TXD;
2618 ring->rx_pending = pp->rx_ring_size;
2619 ring->tx_pending = pp->tx_ring_size;
2620}
2621
2622static int mvneta_ethtool_set_ringparam(struct net_device *dev,
2623 struct ethtool_ringparam *ring)
2624{
2625 struct mvneta_port *pp = netdev_priv(dev);
2626
2627 if ((ring->rx_pending == 0) || (ring->tx_pending == 0))
2628 return -EINVAL;
2629 pp->rx_ring_size = ring->rx_pending < MVNETA_MAX_RXD ?
2630 ring->rx_pending : MVNETA_MAX_RXD;
2631 pp->tx_ring_size = ring->tx_pending < MVNETA_MAX_TXD ?
2632 ring->tx_pending : MVNETA_MAX_TXD;
2633
2634 if (netif_running(dev)) {
2635 mvneta_stop(dev);
2636 if (mvneta_open(dev)) {
2637 netdev_err(dev,
2638 "error on opening device after ring param change\n");
2639 return -ENOMEM;
2640 }
2641 }
2642
2643 return 0;
2644}
2645
2646static const struct net_device_ops mvneta_netdev_ops = {
2647 .ndo_open = mvneta_open,
2648 .ndo_stop = mvneta_stop,
2649 .ndo_start_xmit = mvneta_tx,
2650 .ndo_set_rx_mode = mvneta_set_rx_mode,
2651 .ndo_set_mac_address = mvneta_set_mac_addr,
2652 .ndo_change_mtu = mvneta_change_mtu,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002653 .ndo_get_stats64 = mvneta_get_stats64,
Thomas Petazzoni15f59452013-09-04 16:26:52 +02002654 .ndo_do_ioctl = mvneta_ioctl,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002655};
2656
2657const struct ethtool_ops mvneta_eth_tool_ops = {
2658 .get_link = ethtool_op_get_link,
2659 .get_settings = mvneta_ethtool_get_settings,
2660 .set_settings = mvneta_ethtool_set_settings,
2661 .set_coalesce = mvneta_ethtool_set_coalesce,
2662 .get_coalesce = mvneta_ethtool_get_coalesce,
2663 .get_drvinfo = mvneta_ethtool_get_drvinfo,
2664 .get_ringparam = mvneta_ethtool_get_ringparam,
2665 .set_ringparam = mvneta_ethtool_set_ringparam,
2666};
2667
2668/* Initialize hw */
Greg KH03ce7582012-12-21 13:42:15 +00002669static int mvneta_init(struct mvneta_port *pp, int phy_addr)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002670{
2671 int queue;
2672
2673 /* Disable port */
2674 mvneta_port_disable(pp);
2675
2676 /* Set port default values */
2677 mvneta_defaults_set(pp);
2678
2679 pp->txqs = kzalloc(txq_number * sizeof(struct mvneta_tx_queue),
2680 GFP_KERNEL);
2681 if (!pp->txqs)
2682 return -ENOMEM;
2683
2684 /* Initialize TX descriptor rings */
2685 for (queue = 0; queue < txq_number; queue++) {
2686 struct mvneta_tx_queue *txq = &pp->txqs[queue];
2687 txq->id = queue;
2688 txq->size = pp->tx_ring_size;
2689 txq->done_pkts_coal = MVNETA_TXDONE_COAL_PKTS;
2690 }
2691
2692 pp->rxqs = kzalloc(rxq_number * sizeof(struct mvneta_rx_queue),
2693 GFP_KERNEL);
2694 if (!pp->rxqs) {
2695 kfree(pp->txqs);
2696 return -ENOMEM;
2697 }
2698
2699 /* Create Rx descriptor rings */
2700 for (queue = 0; queue < rxq_number; queue++) {
2701 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
2702 rxq->id = queue;
2703 rxq->size = pp->rx_ring_size;
2704 rxq->pkts_coal = MVNETA_RX_COAL_PKTS;
2705 rxq->time_coal = MVNETA_RX_COAL_USEC;
2706 }
2707
2708 return 0;
2709}
2710
Thomas Petazzoni70eeaf92012-11-19 14:40:02 +01002711static void mvneta_deinit(struct mvneta_port *pp)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002712{
2713 kfree(pp->txqs);
2714 kfree(pp->rxqs);
2715}
2716
2717/* platform glue : initialize decoding windows */
Greg KH03ce7582012-12-21 13:42:15 +00002718static void mvneta_conf_mbus_windows(struct mvneta_port *pp,
2719 const struct mbus_dram_target_info *dram)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002720{
2721 u32 win_enable;
2722 u32 win_protect;
2723 int i;
2724
2725 for (i = 0; i < 6; i++) {
2726 mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
2727 mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
2728
2729 if (i < 4)
2730 mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
2731 }
2732
2733 win_enable = 0x3f;
2734 win_protect = 0;
2735
2736 for (i = 0; i < dram->num_cs; i++) {
2737 const struct mbus_dram_window *cs = dram->cs + i;
2738 mvreg_write(pp, MVNETA_WIN_BASE(i), (cs->base & 0xffff0000) |
2739 (cs->mbus_attr << 8) | dram->mbus_dram_target_id);
2740
2741 mvreg_write(pp, MVNETA_WIN_SIZE(i),
2742 (cs->size - 1) & 0xffff0000);
2743
2744 win_enable &= ~(1 << i);
2745 win_protect |= 3 << (2 * i);
2746 }
2747
2748 mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
2749}
2750
2751/* Power up the port */
Greg KH03ce7582012-12-21 13:42:15 +00002752static void mvneta_port_power_up(struct mvneta_port *pp, int phy_mode)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002753{
2754 u32 val;
2755
2756 /* MAC Cause register should be cleared */
2757 mvreg_write(pp, MVNETA_UNIT_INTR_CAUSE, 0);
2758
2759 if (phy_mode == PHY_INTERFACE_MODE_SGMII)
2760 mvneta_port_sgmii_config(pp);
2761
2762 mvneta_gmac_rgmii_set(pp, 1);
2763
2764 /* Cancel Port Reset */
2765 val = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
2766 val &= ~MVNETA_GMAC2_PORT_RESET;
2767 mvreg_write(pp, MVNETA_GMAC_CTRL_2, val);
2768
2769 while ((mvreg_read(pp, MVNETA_GMAC_CTRL_2) &
2770 MVNETA_GMAC2_PORT_RESET) != 0)
2771 continue;
2772}
2773
2774/* Device initialization routine */
Greg KH03ce7582012-12-21 13:42:15 +00002775static int mvneta_probe(struct platform_device *pdev)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002776{
2777 const struct mbus_dram_target_info *dram_target_info;
Thomas Petazzonic3f0dd32014-03-27 11:39:29 +01002778 struct resource *res;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002779 struct device_node *dn = pdev->dev.of_node;
2780 struct device_node *phy_node;
Thomas Petazzoni189dd622012-11-19 14:15:25 +01002781 u32 phy_addr;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002782 struct mvneta_port *pp;
2783 struct net_device *dev;
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00002784 const char *dt_mac_addr;
2785 char hw_mac_addr[ETH_ALEN];
2786 const char *mac_from;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002787 int phy_mode;
2788 int err;
2789
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002790 /* Our multiqueue support is not complete, so for now, only
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002791 * allow the usage of the first RX queue
2792 */
2793 if (rxq_def != 0) {
2794 dev_err(&pdev->dev, "Invalid rxq_def argument: %d\n", rxq_def);
2795 return -EINVAL;
2796 }
2797
Willy Tarreauee40a112013-04-11 23:00:37 +02002798 dev = alloc_etherdev_mqs(sizeof(struct mvneta_port), txq_number, rxq_number);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002799 if (!dev)
2800 return -ENOMEM;
2801
2802 dev->irq = irq_of_parse_and_map(dn, 0);
2803 if (dev->irq == 0) {
2804 err = -EINVAL;
2805 goto err_free_netdev;
2806 }
2807
2808 phy_node = of_parse_phandle(dn, "phy", 0);
2809 if (!phy_node) {
2810 dev_err(&pdev->dev, "no associated PHY\n");
2811 err = -ENODEV;
2812 goto err_free_irq;
2813 }
2814
2815 phy_mode = of_get_phy_mode(dn);
2816 if (phy_mode < 0) {
2817 dev_err(&pdev->dev, "incorrect phy-mode\n");
2818 err = -EINVAL;
2819 goto err_free_irq;
2820 }
2821
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002822 dev->tx_queue_len = MVNETA_MAX_TXD;
2823 dev->watchdog_timeo = 5 * HZ;
2824 dev->netdev_ops = &mvneta_netdev_ops;
2825
2826 SET_ETHTOOL_OPS(dev, &mvneta_eth_tool_ops);
2827
2828 pp = netdev_priv(dev);
2829
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002830 pp->weight = MVNETA_RX_POLL_WEIGHT;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002831 pp->phy_node = phy_node;
2832 pp->phy_interface = phy_mode;
2833
Thomas Petazzoni189dd622012-11-19 14:15:25 +01002834 pp->clk = devm_clk_get(&pdev->dev, NULL);
2835 if (IS_ERR(pp->clk)) {
2836 err = PTR_ERR(pp->clk);
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +02002837 goto err_free_irq;
Thomas Petazzoni189dd622012-11-19 14:15:25 +01002838 }
2839
2840 clk_prepare_enable(pp->clk);
2841
Thomas Petazzonic3f0dd32014-03-27 11:39:29 +01002842 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2843 pp->base = devm_ioremap_resource(&pdev->dev, res);
2844 if (IS_ERR(pp->base)) {
2845 err = PTR_ERR(pp->base);
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +02002846 goto err_clk;
2847 }
2848
willy tarreau74c41b02014-01-16 08:20:08 +01002849 /* Alloc per-cpu stats */
WANG Cong1c213bd2014-02-13 11:46:28 -08002850 pp->stats = netdev_alloc_pcpu_stats(struct mvneta_pcpu_stats);
willy tarreau74c41b02014-01-16 08:20:08 +01002851 if (!pp->stats) {
2852 err = -ENOMEM;
Thomas Petazzonic3f0dd32014-03-27 11:39:29 +01002853 goto err_clk;
willy tarreau74c41b02014-01-16 08:20:08 +01002854 }
2855
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00002856 dt_mac_addr = of_get_mac_address(dn);
Luka Perkov6c7a9a32013-10-30 00:10:01 +01002857 if (dt_mac_addr) {
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00002858 mac_from = "device tree";
2859 memcpy(dev->dev_addr, dt_mac_addr, ETH_ALEN);
2860 } else {
2861 mvneta_get_mac_addr(pp, hw_mac_addr);
2862 if (is_valid_ether_addr(hw_mac_addr)) {
2863 mac_from = "hardware";
2864 memcpy(dev->dev_addr, hw_mac_addr, ETH_ALEN);
2865 } else {
2866 mac_from = "random";
2867 eth_hw_addr_random(dev);
2868 }
2869 }
2870
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002871 pp->tx_ring_size = MVNETA_MAX_TXD;
2872 pp->rx_ring_size = MVNETA_MAX_RXD;
2873
2874 pp->dev = dev;
2875 SET_NETDEV_DEV(dev, &pdev->dev);
2876
2877 err = mvneta_init(pp, phy_addr);
2878 if (err < 0) {
2879 dev_err(&pdev->dev, "can't init eth hal\n");
willy tarreau74c41b02014-01-16 08:20:08 +01002880 goto err_free_stats;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002881 }
2882 mvneta_port_power_up(pp, phy_mode);
2883
2884 dram_target_info = mv_mbus_dram_info();
2885 if (dram_target_info)
2886 mvneta_conf_mbus_windows(pp, dram_target_info);
2887
2888 netif_napi_add(dev, &pp->napi, mvneta_poll, pp->weight);
2889
willy tarreaub50b72d2013-04-06 08:47:01 +00002890 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM;
2891 dev->hw_features |= NETIF_F_SG | NETIF_F_IP_CSUM;
2892 dev->vlan_features |= NETIF_F_SG | NETIF_F_IP_CSUM;
2893 dev->priv_flags |= IFF_UNICAST_FLT;
2894
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002895 err = register_netdev(dev);
2896 if (err < 0) {
2897 dev_err(&pdev->dev, "failed to register\n");
2898 goto err_deinit;
2899 }
2900
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00002901 netdev_info(dev, "Using %s mac address %pM\n", mac_from,
2902 dev->dev_addr);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002903
2904 platform_set_drvdata(pdev, pp->dev);
2905
2906 return 0;
2907
2908err_deinit:
2909 mvneta_deinit(pp);
willy tarreau74c41b02014-01-16 08:20:08 +01002910err_free_stats:
2911 free_percpu(pp->stats);
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +02002912err_clk:
2913 clk_disable_unprepare(pp->clk);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002914err_free_irq:
2915 irq_dispose_mapping(dev->irq);
2916err_free_netdev:
2917 free_netdev(dev);
2918 return err;
2919}
2920
2921/* Device removal routine */
Greg KH03ce7582012-12-21 13:42:15 +00002922static int mvneta_remove(struct platform_device *pdev)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002923{
2924 struct net_device *dev = platform_get_drvdata(pdev);
2925 struct mvneta_port *pp = netdev_priv(dev);
2926
2927 unregister_netdev(dev);
2928 mvneta_deinit(pp);
Thomas Petazzoni189dd622012-11-19 14:15:25 +01002929 clk_disable_unprepare(pp->clk);
willy tarreau74c41b02014-01-16 08:20:08 +01002930 free_percpu(pp->stats);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002931 irq_dispose_mapping(dev->irq);
2932 free_netdev(dev);
2933
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002934 return 0;
2935}
2936
2937static const struct of_device_id mvneta_match[] = {
2938 { .compatible = "marvell,armada-370-neta" },
2939 { }
2940};
2941MODULE_DEVICE_TABLE(of, mvneta_match);
2942
2943static struct platform_driver mvneta_driver = {
2944 .probe = mvneta_probe,
Greg KH03ce7582012-12-21 13:42:15 +00002945 .remove = mvneta_remove,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002946 .driver = {
2947 .name = MVNETA_DRIVER_NAME,
2948 .of_match_table = mvneta_match,
2949 },
2950};
2951
2952module_platform_driver(mvneta_driver);
2953
2954MODULE_DESCRIPTION("Marvell NETA Ethernet Driver - www.marvell.com");
2955MODULE_AUTHOR("Rami Rosen <rosenr@marvell.com>, Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
2956MODULE_LICENSE("GPL");
2957
2958module_param(rxq_number, int, S_IRUGO);
2959module_param(txq_number, int, S_IRUGO);
2960
2961module_param(rxq_def, int, S_IRUGO);
willy tarreauf19fadf2014-01-16 08:20:17 +01002962module_param(rx_copybreak, int, S_IRUGO | S_IWUSR);