blob: 0702beb600311386c01ef4df0f48a2a7cacd9c92 [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>
David S. Miller2d39d122014-08-25 20:21:55 -070023#include <linux/if_vlan.h>
Thomas Petazzonic5aff182012-08-17 14:04:28 +030024#include <net/ip.h>
25#include <net/ipv6.h>
Thomas Petazzonic3f0dd32014-03-27 11:39:29 +010026#include <linux/io.h>
Ezequiel Garcia2adb7192014-05-19 13:59:55 -030027#include <net/tso.h>
Thomas Petazzonic5aff182012-08-17 14:04:28 +030028#include <linux/of.h>
29#include <linux/of_irq.h>
30#include <linux/of_mdio.h>
31#include <linux/of_net.h>
32#include <linux/of_address.h>
33#include <linux/phy.h>
Thomas Petazzoni189dd622012-11-19 14:15:25 +010034#include <linux/clk.h>
Thomas Petazzonic5aff182012-08-17 14:04:28 +030035
36/* Registers */
37#define MVNETA_RXQ_CONFIG_REG(q) (0x1400 + ((q) << 2))
38#define MVNETA_RXQ_HW_BUF_ALLOC BIT(1)
39#define MVNETA_RXQ_PKT_OFFSET_ALL_MASK (0xf << 8)
40#define MVNETA_RXQ_PKT_OFFSET_MASK(offs) ((offs) << 8)
41#define MVNETA_RXQ_THRESHOLD_REG(q) (0x14c0 + ((q) << 2))
42#define MVNETA_RXQ_NON_OCCUPIED(v) ((v) << 16)
43#define MVNETA_RXQ_BASE_ADDR_REG(q) (0x1480 + ((q) << 2))
44#define MVNETA_RXQ_SIZE_REG(q) (0x14a0 + ((q) << 2))
45#define MVNETA_RXQ_BUF_SIZE_SHIFT 19
46#define MVNETA_RXQ_BUF_SIZE_MASK (0x1fff << 19)
47#define MVNETA_RXQ_STATUS_REG(q) (0x14e0 + ((q) << 2))
48#define MVNETA_RXQ_OCCUPIED_ALL_MASK 0x3fff
49#define MVNETA_RXQ_STATUS_UPDATE_REG(q) (0x1500 + ((q) << 2))
50#define MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT 16
51#define MVNETA_RXQ_ADD_NON_OCCUPIED_MAX 255
52#define MVNETA_PORT_RX_RESET 0x1cc0
53#define MVNETA_PORT_RX_DMA_RESET BIT(0)
54#define MVNETA_PHY_ADDR 0x2000
55#define MVNETA_PHY_ADDR_MASK 0x1f
56#define MVNETA_MBUS_RETRY 0x2010
57#define MVNETA_UNIT_INTR_CAUSE 0x2080
58#define MVNETA_UNIT_CONTROL 0x20B0
59#define MVNETA_PHY_POLLING_ENABLE BIT(1)
60#define MVNETA_WIN_BASE(w) (0x2200 + ((w) << 3))
61#define MVNETA_WIN_SIZE(w) (0x2204 + ((w) << 3))
62#define MVNETA_WIN_REMAP(w) (0x2280 + ((w) << 2))
63#define MVNETA_BASE_ADDR_ENABLE 0x2290
64#define MVNETA_PORT_CONFIG 0x2400
65#define MVNETA_UNI_PROMISC_MODE BIT(0)
66#define MVNETA_DEF_RXQ(q) ((q) << 1)
67#define MVNETA_DEF_RXQ_ARP(q) ((q) << 4)
68#define MVNETA_TX_UNSET_ERR_SUM BIT(12)
69#define MVNETA_DEF_RXQ_TCP(q) ((q) << 16)
70#define MVNETA_DEF_RXQ_UDP(q) ((q) << 19)
71#define MVNETA_DEF_RXQ_BPDU(q) ((q) << 22)
72#define MVNETA_RX_CSUM_WITH_PSEUDO_HDR BIT(25)
73#define MVNETA_PORT_CONFIG_DEFL_VALUE(q) (MVNETA_DEF_RXQ(q) | \
74 MVNETA_DEF_RXQ_ARP(q) | \
75 MVNETA_DEF_RXQ_TCP(q) | \
76 MVNETA_DEF_RXQ_UDP(q) | \
77 MVNETA_DEF_RXQ_BPDU(q) | \
78 MVNETA_TX_UNSET_ERR_SUM | \
79 MVNETA_RX_CSUM_WITH_PSEUDO_HDR)
80#define MVNETA_PORT_CONFIG_EXTEND 0x2404
81#define MVNETA_MAC_ADDR_LOW 0x2414
82#define MVNETA_MAC_ADDR_HIGH 0x2418
83#define MVNETA_SDMA_CONFIG 0x241c
84#define MVNETA_SDMA_BRST_SIZE_16 4
Thomas Petazzonic5aff182012-08-17 14:04:28 +030085#define MVNETA_RX_BRST_SZ_MASK(burst) ((burst) << 1)
86#define MVNETA_RX_NO_DATA_SWAP BIT(4)
87#define MVNETA_TX_NO_DATA_SWAP BIT(5)
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +020088#define MVNETA_DESC_SWAP BIT(6)
Thomas Petazzonic5aff182012-08-17 14:04:28 +030089#define MVNETA_TX_BRST_SZ_MASK(burst) ((burst) << 22)
90#define MVNETA_PORT_STATUS 0x2444
91#define MVNETA_TX_IN_PRGRS BIT(1)
92#define MVNETA_TX_FIFO_EMPTY BIT(8)
93#define MVNETA_RX_MIN_FRAME_SIZE 0x247c
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +020094#define MVNETA_SERDES_CFG 0x24A0
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +020095#define MVNETA_SGMII_SERDES_PROTO 0x0cc7
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +020096#define MVNETA_QSGMII_SERDES_PROTO 0x0667
Thomas Petazzonic5aff182012-08-17 14:04:28 +030097#define MVNETA_TYPE_PRIO 0x24bc
98#define MVNETA_FORCE_UNI BIT(21)
99#define MVNETA_TXQ_CMD_1 0x24e4
100#define MVNETA_TXQ_CMD 0x2448
101#define MVNETA_TXQ_DISABLE_SHIFT 8
102#define MVNETA_TXQ_ENABLE_MASK 0x000000ff
Stas Sergeev898b2972015-04-01 20:32:49 +0300103#define MVNETA_GMAC_CLOCK_DIVIDER 0x24f4
104#define MVNETA_GMAC_1MS_CLOCK_ENABLE BIT(31)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300105#define MVNETA_ACC_MODE 0x2500
106#define MVNETA_CPU_MAP(cpu) (0x2540 + ((cpu) << 2))
107#define MVNETA_CPU_RXQ_ACCESS_ALL_MASK 0x000000ff
108#define MVNETA_CPU_TXQ_ACCESS_ALL_MASK 0x0000ff00
109#define MVNETA_RXQ_TIME_COAL_REG(q) (0x2580 + ((q) << 2))
willy tarreau40ba35e2014-01-16 08:20:10 +0100110
111/* Exception Interrupt Port/Queue Cause register */
112
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300113#define MVNETA_INTR_NEW_CAUSE 0x25a0
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300114#define MVNETA_INTR_NEW_MASK 0x25a4
willy tarreau40ba35e2014-01-16 08:20:10 +0100115
116/* bits 0..7 = TXQ SENT, one bit per queue.
117 * bits 8..15 = RXQ OCCUP, one bit per queue.
118 * bits 16..23 = RXQ FREE, one bit per queue.
119 * bit 29 = OLD_REG_SUM, see old reg ?
120 * bit 30 = TX_ERR_SUM, one bit for 4 ports
121 * bit 31 = MISC_SUM, one bit for 4 ports
122 */
123#define MVNETA_TX_INTR_MASK(nr_txqs) (((1 << nr_txqs) - 1) << 0)
124#define MVNETA_TX_INTR_MASK_ALL (0xff << 0)
125#define MVNETA_RX_INTR_MASK(nr_rxqs) (((1 << nr_rxqs) - 1) << 8)
126#define MVNETA_RX_INTR_MASK_ALL (0xff << 8)
Stas Sergeev898b2972015-04-01 20:32:49 +0300127#define MVNETA_MISCINTR_INTR_MASK BIT(31)
willy tarreau40ba35e2014-01-16 08:20:10 +0100128
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300129#define MVNETA_INTR_OLD_CAUSE 0x25a8
130#define MVNETA_INTR_OLD_MASK 0x25ac
willy tarreau40ba35e2014-01-16 08:20:10 +0100131
132/* Data Path Port/Queue Cause Register */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300133#define MVNETA_INTR_MISC_CAUSE 0x25b0
134#define MVNETA_INTR_MISC_MASK 0x25b4
willy tarreau40ba35e2014-01-16 08:20:10 +0100135
136#define MVNETA_CAUSE_PHY_STATUS_CHANGE BIT(0)
137#define MVNETA_CAUSE_LINK_CHANGE BIT(1)
138#define MVNETA_CAUSE_PTP BIT(4)
139
140#define MVNETA_CAUSE_INTERNAL_ADDR_ERR BIT(7)
141#define MVNETA_CAUSE_RX_OVERRUN BIT(8)
142#define MVNETA_CAUSE_RX_CRC_ERROR BIT(9)
143#define MVNETA_CAUSE_RX_LARGE_PKT BIT(10)
144#define MVNETA_CAUSE_TX_UNDERUN BIT(11)
145#define MVNETA_CAUSE_PRBS_ERR BIT(12)
146#define MVNETA_CAUSE_PSC_SYNC_CHANGE BIT(13)
147#define MVNETA_CAUSE_SERDES_SYNC_ERR BIT(14)
148
149#define MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT 16
150#define MVNETA_CAUSE_BMU_ALLOC_ERR_ALL_MASK (0xF << MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT)
151#define MVNETA_CAUSE_BMU_ALLOC_ERR_MASK(pool) (1 << (MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT + (pool)))
152
153#define MVNETA_CAUSE_TXQ_ERROR_SHIFT 24
154#define MVNETA_CAUSE_TXQ_ERROR_ALL_MASK (0xFF << MVNETA_CAUSE_TXQ_ERROR_SHIFT)
155#define MVNETA_CAUSE_TXQ_ERROR_MASK(q) (1 << (MVNETA_CAUSE_TXQ_ERROR_SHIFT + (q)))
156
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300157#define MVNETA_INTR_ENABLE 0x25b8
158#define MVNETA_TXQ_INTR_ENABLE_ALL_MASK 0x0000ff00
willy tarreau40ba35e2014-01-16 08:20:10 +0100159#define MVNETA_RXQ_INTR_ENABLE_ALL_MASK 0xff000000 // note: neta says it's 0x000000FF
160
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300161#define MVNETA_RXQ_CMD 0x2680
162#define MVNETA_RXQ_DISABLE_SHIFT 8
163#define MVNETA_RXQ_ENABLE_MASK 0x000000ff
164#define MVETH_TXQ_TOKEN_COUNT_REG(q) (0x2700 + ((q) << 4))
165#define MVETH_TXQ_TOKEN_CFG_REG(q) (0x2704 + ((q) << 4))
166#define MVNETA_GMAC_CTRL_0 0x2c00
167#define MVNETA_GMAC_MAX_RX_SIZE_SHIFT 2
168#define MVNETA_GMAC_MAX_RX_SIZE_MASK 0x7ffc
169#define MVNETA_GMAC0_PORT_ENABLE BIT(0)
170#define MVNETA_GMAC_CTRL_2 0x2c08
Stas Sergeev898b2972015-04-01 20:32:49 +0300171#define MVNETA_GMAC2_INBAND_AN_ENABLE BIT(0)
Thomas Petazzonia79121d2014-03-26 00:25:41 +0100172#define MVNETA_GMAC2_PCS_ENABLE BIT(3)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300173#define MVNETA_GMAC2_PORT_RGMII BIT(4)
174#define MVNETA_GMAC2_PORT_RESET BIT(6)
175#define MVNETA_GMAC_STATUS 0x2c10
176#define MVNETA_GMAC_LINK_UP BIT(0)
177#define MVNETA_GMAC_SPEED_1000 BIT(1)
178#define MVNETA_GMAC_SPEED_100 BIT(2)
179#define MVNETA_GMAC_FULL_DUPLEX BIT(3)
180#define MVNETA_GMAC_RX_FLOW_CTRL_ENABLE BIT(4)
181#define MVNETA_GMAC_TX_FLOW_CTRL_ENABLE BIT(5)
182#define MVNETA_GMAC_RX_FLOW_CTRL_ACTIVE BIT(6)
183#define MVNETA_GMAC_TX_FLOW_CTRL_ACTIVE BIT(7)
184#define MVNETA_GMAC_AUTONEG_CONFIG 0x2c0c
185#define MVNETA_GMAC_FORCE_LINK_DOWN BIT(0)
186#define MVNETA_GMAC_FORCE_LINK_PASS BIT(1)
Stas Sergeev898b2972015-04-01 20:32:49 +0300187#define MVNETA_GMAC_INBAND_AN_ENABLE BIT(2)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300188#define MVNETA_GMAC_CONFIG_MII_SPEED BIT(5)
189#define MVNETA_GMAC_CONFIG_GMII_SPEED BIT(6)
Thomas Petazzoni71408602013-09-04 16:21:18 +0200190#define MVNETA_GMAC_AN_SPEED_EN BIT(7)
Stas Sergeev898b2972015-04-01 20:32:49 +0300191#define MVNETA_GMAC_AN_FLOW_CTRL_EN BIT(11)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300192#define MVNETA_GMAC_CONFIG_FULL_DUPLEX BIT(12)
Thomas Petazzoni71408602013-09-04 16:21:18 +0200193#define MVNETA_GMAC_AN_DUPLEX_EN BIT(13)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300194#define MVNETA_MIB_COUNTERS_BASE 0x3080
195#define MVNETA_MIB_LATE_COLLISION 0x7c
196#define MVNETA_DA_FILT_SPEC_MCAST 0x3400
197#define MVNETA_DA_FILT_OTH_MCAST 0x3500
198#define MVNETA_DA_FILT_UCAST_BASE 0x3600
199#define MVNETA_TXQ_BASE_ADDR_REG(q) (0x3c00 + ((q) << 2))
200#define MVNETA_TXQ_SIZE_REG(q) (0x3c20 + ((q) << 2))
201#define MVNETA_TXQ_SENT_THRESH_ALL_MASK 0x3fff0000
202#define MVNETA_TXQ_SENT_THRESH_MASK(coal) ((coal) << 16)
203#define MVNETA_TXQ_UPDATE_REG(q) (0x3c60 + ((q) << 2))
204#define MVNETA_TXQ_DEC_SENT_SHIFT 16
205#define MVNETA_TXQ_STATUS_REG(q) (0x3c40 + ((q) << 2))
206#define MVNETA_TXQ_SENT_DESC_SHIFT 16
207#define MVNETA_TXQ_SENT_DESC_MASK 0x3fff0000
208#define MVNETA_PORT_TX_RESET 0x3cf0
209#define MVNETA_PORT_TX_DMA_RESET BIT(0)
210#define MVNETA_TX_MTU 0x3e0c
211#define MVNETA_TX_TOKEN_SIZE 0x3e14
212#define MVNETA_TX_TOKEN_SIZE_MAX 0xffffffff
213#define MVNETA_TXQ_TOKEN_SIZE_REG(q) (0x3e40 + ((q) << 2))
214#define MVNETA_TXQ_TOKEN_SIZE_MAX 0x7fffffff
215
216#define MVNETA_CAUSE_TXQ_SENT_DESC_ALL_MASK 0xff
217
218/* Descriptor ring Macros */
219#define MVNETA_QUEUE_NEXT_DESC(q, index) \
220 (((index) < (q)->last_desc) ? ((index) + 1) : 0)
221
222/* Various constants */
223
224/* Coalescing */
willy tarreauaebea2b2014-12-02 08:13:04 +0100225#define MVNETA_TXDONE_COAL_PKTS 1
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300226#define MVNETA_RX_COAL_PKTS 32
227#define MVNETA_RX_COAL_USEC 100
228
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100229/* The two bytes Marvell header. Either contains a special value used
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300230 * by Marvell switches when a specific hardware mode is enabled (not
231 * supported by this driver) or is filled automatically by zeroes on
232 * the RX side. Those two bytes being at the front of the Ethernet
233 * header, they allow to have the IP header aligned on a 4 bytes
234 * boundary automatically: the hardware skips those two bytes on its
235 * own.
236 */
237#define MVNETA_MH_SIZE 2
238
239#define MVNETA_VLAN_TAG_LEN 4
240
241#define MVNETA_CPU_D_CACHE_LINE_SIZE 32
242#define MVNETA_TX_CSUM_MAX_SIZE 9800
243#define MVNETA_ACC_MODE_EXT 1
244
245/* Timeout constants */
246#define MVNETA_TX_DISABLE_TIMEOUT_MSEC 1000
247#define MVNETA_RX_DISABLE_TIMEOUT_MSEC 1000
248#define MVNETA_TX_FIFO_EMPTY_TIMEOUT 10000
249
250#define MVNETA_TX_MTU_MAX 0x3ffff
251
Ezequiel Garcia2adb7192014-05-19 13:59:55 -0300252/* TSO header size */
253#define TSO_HEADER_SIZE 128
254
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300255/* Max number of Rx descriptors */
256#define MVNETA_MAX_RXD 128
257
258/* Max number of Tx descriptors */
259#define MVNETA_MAX_TXD 532
260
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -0300261/* Max number of allowed TCP segments for software TSO */
262#define MVNETA_MAX_TSO_SEGS 100
263
264#define MVNETA_MAX_SKB_DESCS (MVNETA_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
265
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300266/* descriptor aligned size */
267#define MVNETA_DESC_ALIGNED_SIZE 32
268
269#define MVNETA_RX_PKT_SIZE(mtu) \
270 ALIGN((mtu) + MVNETA_MH_SIZE + MVNETA_VLAN_TAG_LEN + \
271 ETH_HLEN + ETH_FCS_LEN, \
272 MVNETA_CPU_D_CACHE_LINE_SIZE)
273
Ezequiel Garcia2e3173a2014-05-30 13:40:07 -0300274#define IS_TSO_HEADER(txq, addr) \
275 ((addr >= txq->tso_hdrs_phys) && \
276 (addr < txq->tso_hdrs_phys + txq->size * TSO_HEADER_SIZE))
277
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300278#define MVNETA_RX_BUF_SIZE(pkt_size) ((pkt_size) + NET_SKB_PAD)
279
willy tarreau74c41b02014-01-16 08:20:08 +0100280struct mvneta_pcpu_stats {
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300281 struct u64_stats_sync syncp;
willy tarreau74c41b02014-01-16 08:20:08 +0100282 u64 rx_packets;
283 u64 rx_bytes;
284 u64 tx_packets;
285 u64 tx_bytes;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300286};
287
Maxime Ripard12bb03b2015-09-25 18:09:36 +0200288struct mvneta_pcpu_port {
289 /* Pointer to the shared port */
290 struct mvneta_port *pp;
291
292 /* Pointer to the CPU-local NAPI struct */
293 struct napi_struct napi;
294
295 /* Cause of the previous interrupt */
296 u32 cause_rx_tx;
297};
298
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300299struct mvneta_port {
Maxime Ripard12bb03b2015-09-25 18:09:36 +0200300 struct mvneta_pcpu_port __percpu *ports;
301 struct mvneta_pcpu_stats __percpu *stats;
302
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300303 int pkt_size;
willy tarreau8ec2cd42014-01-16 08:20:16 +0100304 unsigned int frag_size;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300305 void __iomem *base;
306 struct mvneta_rx_queue *rxqs;
307 struct mvneta_tx_queue *txqs;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300308 struct net_device *dev;
309
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300310 /* Core clock */
Thomas Petazzoni189dd622012-11-19 14:15:25 +0100311 struct clk *clk;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300312 u8 mcast_count[256];
313 u16 tx_ring_size;
314 u16 rx_ring_size;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300315
316 struct mii_bus *mii_bus;
317 struct phy_device *phy_dev;
318 phy_interface_t phy_interface;
319 struct device_node *phy_node;
320 unsigned int link;
321 unsigned int duplex;
322 unsigned int speed;
Simon Guinotb65657f2015-06-30 16:20:22 +0200323 unsigned int tx_csum_limit;
Stas Sergeev898b2972015-04-01 20:32:49 +0300324 int use_inband_status:1;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300325};
326
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100327/* The mvneta_tx_desc and mvneta_rx_desc structures describe the
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300328 * layout of the transmit and reception DMA descriptors, and their
329 * layout is therefore defined by the hardware design
330 */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200331
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300332#define MVNETA_TX_L3_OFF_SHIFT 0
333#define MVNETA_TX_IP_HLEN_SHIFT 8
334#define MVNETA_TX_L4_UDP BIT(16)
335#define MVNETA_TX_L3_IP6 BIT(17)
336#define MVNETA_TXD_IP_CSUM BIT(18)
337#define MVNETA_TXD_Z_PAD BIT(19)
338#define MVNETA_TXD_L_DESC BIT(20)
339#define MVNETA_TXD_F_DESC BIT(21)
340#define MVNETA_TXD_FLZ_DESC (MVNETA_TXD_Z_PAD | \
341 MVNETA_TXD_L_DESC | \
342 MVNETA_TXD_F_DESC)
343#define MVNETA_TX_L4_CSUM_FULL BIT(30)
344#define MVNETA_TX_L4_CSUM_NOT BIT(31)
345
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300346#define MVNETA_RXD_ERR_CRC 0x0
347#define MVNETA_RXD_ERR_SUMMARY BIT(16)
348#define MVNETA_RXD_ERR_OVERRUN BIT(17)
349#define MVNETA_RXD_ERR_LEN BIT(18)
350#define MVNETA_RXD_ERR_RESOURCE (BIT(17) | BIT(18))
351#define MVNETA_RXD_ERR_CODE_MASK (BIT(17) | BIT(18))
352#define MVNETA_RXD_L3_IP4 BIT(25)
353#define MVNETA_RXD_FIRST_LAST_DESC (BIT(26) | BIT(27))
354#define MVNETA_RXD_L4_CSUM_OK BIT(30)
355
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +0200356#if defined(__LITTLE_ENDIAN)
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200357struct mvneta_tx_desc {
358 u32 command; /* Options used by HW for packet transmitting.*/
359 u16 reserverd1; /* csum_l4 (for future use) */
360 u16 data_size; /* Data size of transmitted packet in bytes */
361 u32 buf_phys_addr; /* Physical addr of transmitted buffer */
362 u32 reserved2; /* hw_cmd - (for future use, PMT) */
363 u32 reserved3[4]; /* Reserved - (for future use) */
364};
365
366struct mvneta_rx_desc {
367 u32 status; /* Info about received packet */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300368 u16 reserved1; /* pnc_info - (for future use, PnC) */
369 u16 data_size; /* Size of received packet in bytes */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200370
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300371 u32 buf_phys_addr; /* Physical address of the buffer */
372 u32 reserved2; /* pnc_flow_id (for future use, PnC) */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200373
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300374 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
375 u16 reserved3; /* prefetch_cmd, for future use */
376 u16 reserved4; /* csum_l4 - (for future use, PnC) */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200377
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300378 u32 reserved5; /* pnc_extra PnC (for future use, PnC) */
379 u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */
380};
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +0200381#else
382struct mvneta_tx_desc {
383 u16 data_size; /* Data size of transmitted packet in bytes */
384 u16 reserverd1; /* csum_l4 (for future use) */
385 u32 command; /* Options used by HW for packet transmitting.*/
386 u32 reserved2; /* hw_cmd - (for future use, PMT) */
387 u32 buf_phys_addr; /* Physical addr of transmitted buffer */
388 u32 reserved3[4]; /* Reserved - (for future use) */
389};
390
391struct mvneta_rx_desc {
392 u16 data_size; /* Size of received packet in bytes */
393 u16 reserved1; /* pnc_info - (for future use, PnC) */
394 u32 status; /* Info about received packet */
395
396 u32 reserved2; /* pnc_flow_id (for future use, PnC) */
397 u32 buf_phys_addr; /* Physical address of the buffer */
398
399 u16 reserved4; /* csum_l4 - (for future use, PnC) */
400 u16 reserved3; /* prefetch_cmd, for future use */
401 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
402
403 u32 reserved5; /* pnc_extra PnC (for future use, PnC) */
404 u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */
405};
406#endif
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300407
408struct mvneta_tx_queue {
409 /* Number of this TX queue, in the range 0-7 */
410 u8 id;
411
412 /* Number of TX DMA descriptors in the descriptor ring */
413 int size;
414
415 /* Number of currently used TX DMA descriptor in the
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100416 * descriptor ring
417 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300418 int count;
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -0300419 int tx_stop_threshold;
420 int tx_wake_threshold;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300421
422 /* Array of transmitted skb */
423 struct sk_buff **tx_skb;
424
425 /* Index of last TX DMA descriptor that was inserted */
426 int txq_put_index;
427
428 /* Index of the TX DMA descriptor to be cleaned up */
429 int txq_get_index;
430
431 u32 done_pkts_coal;
432
433 /* Virtual address of the TX DMA descriptors array */
434 struct mvneta_tx_desc *descs;
435
436 /* DMA address of the TX DMA descriptors array */
437 dma_addr_t descs_phys;
438
439 /* Index of the last TX DMA descriptor */
440 int last_desc;
441
442 /* Index of the next TX DMA descriptor to process */
443 int next_desc_to_proc;
Ezequiel Garcia2adb7192014-05-19 13:59:55 -0300444
445 /* DMA buffers for TSO headers */
446 char *tso_hdrs;
447
448 /* DMA address of TSO headers */
449 dma_addr_t tso_hdrs_phys;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300450};
451
452struct mvneta_rx_queue {
453 /* rx queue number, in the range 0-7 */
454 u8 id;
455
456 /* num of rx descriptors in the rx descriptor ring */
457 int size;
458
459 /* counter of times when mvneta_refill() failed */
460 int missed;
461
462 u32 pkts_coal;
463 u32 time_coal;
464
465 /* Virtual address of the RX DMA descriptors array */
466 struct mvneta_rx_desc *descs;
467
468 /* DMA address of the RX DMA descriptors array */
469 dma_addr_t descs_phys;
470
471 /* Index of the last RX DMA descriptor */
472 int last_desc;
473
474 /* Index of the next RX DMA descriptor to process */
475 int next_desc_to_proc;
476};
477
Ezequiel Garciaedadb7f2014-05-22 20:07:01 -0300478/* The hardware supports eight (8) rx queues, but we are only allowing
479 * the first one to be used. Therefore, let's just allocate one queue.
480 */
Maxime Ripardd8936652015-09-25 18:09:37 +0200481static int rxq_number = 8;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300482static int txq_number = 8;
483
484static int rxq_def;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300485
willy tarreauf19fadf2014-01-16 08:20:17 +0100486static int rx_copybreak __read_mostly = 256;
487
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300488#define MVNETA_DRIVER_NAME "mvneta"
489#define MVNETA_DRIVER_VERSION "1.0"
490
491/* Utility/helper methods */
492
493/* Write helper method */
494static void mvreg_write(struct mvneta_port *pp, u32 offset, u32 data)
495{
496 writel(data, pp->base + offset);
497}
498
499/* Read helper method */
500static u32 mvreg_read(struct mvneta_port *pp, u32 offset)
501{
502 return readl(pp->base + offset);
503}
504
505/* Increment txq get counter */
506static void mvneta_txq_inc_get(struct mvneta_tx_queue *txq)
507{
508 txq->txq_get_index++;
509 if (txq->txq_get_index == txq->size)
510 txq->txq_get_index = 0;
511}
512
513/* Increment txq put counter */
514static void mvneta_txq_inc_put(struct mvneta_tx_queue *txq)
515{
516 txq->txq_put_index++;
517 if (txq->txq_put_index == txq->size)
518 txq->txq_put_index = 0;
519}
520
521
522/* Clear all MIB counters */
523static void mvneta_mib_counters_clear(struct mvneta_port *pp)
524{
525 int i;
526 u32 dummy;
527
528 /* Perform dummy reads from MIB counters */
529 for (i = 0; i < MVNETA_MIB_LATE_COLLISION; i += 4)
530 dummy = mvreg_read(pp, (MVNETA_MIB_COUNTERS_BASE + i));
531}
532
533/* Get System Network Statistics */
534struct rtnl_link_stats64 *mvneta_get_stats64(struct net_device *dev,
535 struct rtnl_link_stats64 *stats)
536{
537 struct mvneta_port *pp = netdev_priv(dev);
538 unsigned int start;
willy tarreau74c41b02014-01-16 08:20:08 +0100539 int cpu;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300540
willy tarreau74c41b02014-01-16 08:20:08 +0100541 for_each_possible_cpu(cpu) {
542 struct mvneta_pcpu_stats *cpu_stats;
543 u64 rx_packets;
544 u64 rx_bytes;
545 u64 tx_packets;
546 u64 tx_bytes;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300547
willy tarreau74c41b02014-01-16 08:20:08 +0100548 cpu_stats = per_cpu_ptr(pp->stats, cpu);
549 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700550 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
willy tarreau74c41b02014-01-16 08:20:08 +0100551 rx_packets = cpu_stats->rx_packets;
552 rx_bytes = cpu_stats->rx_bytes;
553 tx_packets = cpu_stats->tx_packets;
554 tx_bytes = cpu_stats->tx_bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700555 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300556
willy tarreau74c41b02014-01-16 08:20:08 +0100557 stats->rx_packets += rx_packets;
558 stats->rx_bytes += rx_bytes;
559 stats->tx_packets += tx_packets;
560 stats->tx_bytes += tx_bytes;
561 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300562
563 stats->rx_errors = dev->stats.rx_errors;
564 stats->rx_dropped = dev->stats.rx_dropped;
565
566 stats->tx_dropped = dev->stats.tx_dropped;
567
568 return stats;
569}
570
571/* Rx descriptors helper methods */
572
willy tarreau54282132014-01-16 08:20:14 +0100573/* Checks whether the RX descriptor having this status is both the first
574 * and the last descriptor for the RX packet. Each RX packet is currently
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300575 * received through a single RX descriptor, so not having each RX
576 * descriptor with its first and last bits set is an error
577 */
willy tarreau54282132014-01-16 08:20:14 +0100578static int mvneta_rxq_desc_is_first_last(u32 status)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300579{
willy tarreau54282132014-01-16 08:20:14 +0100580 return (status & MVNETA_RXD_FIRST_LAST_DESC) ==
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300581 MVNETA_RXD_FIRST_LAST_DESC;
582}
583
584/* Add number of descriptors ready to receive new packets */
585static void mvneta_rxq_non_occup_desc_add(struct mvneta_port *pp,
586 struct mvneta_rx_queue *rxq,
587 int ndescs)
588{
589 /* Only MVNETA_RXQ_ADD_NON_OCCUPIED_MAX (255) descriptors can
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100590 * be added at once
591 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300592 while (ndescs > MVNETA_RXQ_ADD_NON_OCCUPIED_MAX) {
593 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
594 (MVNETA_RXQ_ADD_NON_OCCUPIED_MAX <<
595 MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
596 ndescs -= MVNETA_RXQ_ADD_NON_OCCUPIED_MAX;
597 }
598
599 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
600 (ndescs << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
601}
602
603/* Get number of RX descriptors occupied by received packets */
604static int mvneta_rxq_busy_desc_num_get(struct mvneta_port *pp,
605 struct mvneta_rx_queue *rxq)
606{
607 u32 val;
608
609 val = mvreg_read(pp, MVNETA_RXQ_STATUS_REG(rxq->id));
610 return val & MVNETA_RXQ_OCCUPIED_ALL_MASK;
611}
612
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100613/* Update num of rx desc called upon return from rx path or
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300614 * from mvneta_rxq_drop_pkts().
615 */
616static void mvneta_rxq_desc_num_update(struct mvneta_port *pp,
617 struct mvneta_rx_queue *rxq,
618 int rx_done, int rx_filled)
619{
620 u32 val;
621
622 if ((rx_done <= 0xff) && (rx_filled <= 0xff)) {
623 val = rx_done |
624 (rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT);
625 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
626 return;
627 }
628
629 /* Only 255 descriptors can be added at once */
630 while ((rx_done > 0) || (rx_filled > 0)) {
631 if (rx_done <= 0xff) {
632 val = rx_done;
633 rx_done = 0;
634 } else {
635 val = 0xff;
636 rx_done -= 0xff;
637 }
638 if (rx_filled <= 0xff) {
639 val |= rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
640 rx_filled = 0;
641 } else {
642 val |= 0xff << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
643 rx_filled -= 0xff;
644 }
645 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
646 }
647}
648
649/* Get pointer to next RX descriptor to be processed by SW */
650static struct mvneta_rx_desc *
651mvneta_rxq_next_desc_get(struct mvneta_rx_queue *rxq)
652{
653 int rx_desc = rxq->next_desc_to_proc;
654
655 rxq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(rxq, rx_desc);
willy tarreau34e41792014-01-16 08:20:15 +0100656 prefetch(rxq->descs + rxq->next_desc_to_proc);
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300657 return rxq->descs + rx_desc;
658}
659
660/* Change maximum receive size of the port. */
661static void mvneta_max_rx_size_set(struct mvneta_port *pp, int max_rx_size)
662{
663 u32 val;
664
665 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
666 val &= ~MVNETA_GMAC_MAX_RX_SIZE_MASK;
667 val |= ((max_rx_size - MVNETA_MH_SIZE) / 2) <<
668 MVNETA_GMAC_MAX_RX_SIZE_SHIFT;
669 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
670}
671
672
673/* Set rx queue offset */
674static void mvneta_rxq_offset_set(struct mvneta_port *pp,
675 struct mvneta_rx_queue *rxq,
676 int offset)
677{
678 u32 val;
679
680 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
681 val &= ~MVNETA_RXQ_PKT_OFFSET_ALL_MASK;
682
683 /* Offset is in */
684 val |= MVNETA_RXQ_PKT_OFFSET_MASK(offset >> 3);
685 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
686}
687
688
689/* Tx descriptors helper methods */
690
691/* Update HW with number of TX descriptors to be sent */
692static void mvneta_txq_pend_desc_add(struct mvneta_port *pp,
693 struct mvneta_tx_queue *txq,
694 int pend_desc)
695{
696 u32 val;
697
698 /* Only 255 descriptors can be added at once ; Assume caller
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100699 * process TX desriptors in quanta less than 256
700 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300701 val = pend_desc;
702 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
703}
704
705/* Get pointer to next TX descriptor to be processed (send) by HW */
706static struct mvneta_tx_desc *
707mvneta_txq_next_desc_get(struct mvneta_tx_queue *txq)
708{
709 int tx_desc = txq->next_desc_to_proc;
710
711 txq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(txq, tx_desc);
712 return txq->descs + tx_desc;
713}
714
715/* Release the last allocated TX descriptor. Useful to handle DMA
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100716 * mapping failures in the TX path.
717 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300718static void mvneta_txq_desc_put(struct mvneta_tx_queue *txq)
719{
720 if (txq->next_desc_to_proc == 0)
721 txq->next_desc_to_proc = txq->last_desc - 1;
722 else
723 txq->next_desc_to_proc--;
724}
725
726/* Set rxq buf size */
727static void mvneta_rxq_buf_size_set(struct mvneta_port *pp,
728 struct mvneta_rx_queue *rxq,
729 int buf_size)
730{
731 u32 val;
732
733 val = mvreg_read(pp, MVNETA_RXQ_SIZE_REG(rxq->id));
734
735 val &= ~MVNETA_RXQ_BUF_SIZE_MASK;
736 val |= ((buf_size >> 3) << MVNETA_RXQ_BUF_SIZE_SHIFT);
737
738 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), val);
739}
740
741/* Disable buffer management (BM) */
742static void mvneta_rxq_bm_disable(struct mvneta_port *pp,
743 struct mvneta_rx_queue *rxq)
744{
745 u32 val;
746
747 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
748 val &= ~MVNETA_RXQ_HW_BUF_ALLOC;
749 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
750}
751
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300752/* Start the Ethernet port RX and TX activity */
753static void mvneta_port_up(struct mvneta_port *pp)
754{
755 int queue;
756 u32 q_map;
757
758 /* Enable all initialized TXs. */
759 mvneta_mib_counters_clear(pp);
760 q_map = 0;
761 for (queue = 0; queue < txq_number; queue++) {
762 struct mvneta_tx_queue *txq = &pp->txqs[queue];
763 if (txq->descs != NULL)
764 q_map |= (1 << queue);
765 }
766 mvreg_write(pp, MVNETA_TXQ_CMD, q_map);
767
768 /* Enable all initialized RXQs. */
Maxime Ripardd8936652015-09-25 18:09:37 +0200769 mvreg_write(pp, MVNETA_RXQ_CMD, BIT(rxq_def));
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300770}
771
772/* Stop the Ethernet port activity */
773static void mvneta_port_down(struct mvneta_port *pp)
774{
775 u32 val;
776 int count;
777
778 /* Stop Rx port activity. Check port Rx activity. */
779 val = mvreg_read(pp, MVNETA_RXQ_CMD) & MVNETA_RXQ_ENABLE_MASK;
780
781 /* Issue stop command for active channels only */
782 if (val != 0)
783 mvreg_write(pp, MVNETA_RXQ_CMD,
784 val << MVNETA_RXQ_DISABLE_SHIFT);
785
786 /* Wait for all Rx activity to terminate. */
787 count = 0;
788 do {
789 if (count++ >= MVNETA_RX_DISABLE_TIMEOUT_MSEC) {
790 netdev_warn(pp->dev,
791 "TIMEOUT for RX stopped ! rx_queue_cmd: 0x08%x\n",
792 val);
793 break;
794 }
795 mdelay(1);
796
797 val = mvreg_read(pp, MVNETA_RXQ_CMD);
798 } while (val & 0xff);
799
800 /* Stop Tx port activity. Check port Tx activity. Issue stop
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100801 * command for active channels only
802 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300803 val = (mvreg_read(pp, MVNETA_TXQ_CMD)) & MVNETA_TXQ_ENABLE_MASK;
804
805 if (val != 0)
806 mvreg_write(pp, MVNETA_TXQ_CMD,
807 (val << MVNETA_TXQ_DISABLE_SHIFT));
808
809 /* Wait for all Tx activity to terminate. */
810 count = 0;
811 do {
812 if (count++ >= MVNETA_TX_DISABLE_TIMEOUT_MSEC) {
813 netdev_warn(pp->dev,
814 "TIMEOUT for TX stopped status=0x%08x\n",
815 val);
816 break;
817 }
818 mdelay(1);
819
820 /* Check TX Command reg that all Txqs are stopped */
821 val = mvreg_read(pp, MVNETA_TXQ_CMD);
822
823 } while (val & 0xff);
824
825 /* Double check to verify that TX FIFO is empty */
826 count = 0;
827 do {
828 if (count++ >= MVNETA_TX_FIFO_EMPTY_TIMEOUT) {
829 netdev_warn(pp->dev,
830 "TX FIFO empty timeout status=0x08%x\n",
831 val);
832 break;
833 }
834 mdelay(1);
835
836 val = mvreg_read(pp, MVNETA_PORT_STATUS);
837 } while (!(val & MVNETA_TX_FIFO_EMPTY) &&
838 (val & MVNETA_TX_IN_PRGRS));
839
840 udelay(200);
841}
842
843/* Enable the port by setting the port enable bit of the MAC control register */
844static void mvneta_port_enable(struct mvneta_port *pp)
845{
846 u32 val;
847
848 /* Enable port */
849 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
850 val |= MVNETA_GMAC0_PORT_ENABLE;
851 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
852}
853
854/* Disable the port and wait for about 200 usec before retuning */
855static void mvneta_port_disable(struct mvneta_port *pp)
856{
857 u32 val;
858
859 /* Reset the Enable bit in the Serial Control Register */
860 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
861 val &= ~MVNETA_GMAC0_PORT_ENABLE;
862 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
863
864 udelay(200);
865}
866
867/* Multicast tables methods */
868
869/* Set all entries in Unicast MAC Table; queue==-1 means reject all */
870static void mvneta_set_ucast_table(struct mvneta_port *pp, int queue)
871{
872 int offset;
873 u32 val;
874
875 if (queue == -1) {
876 val = 0;
877 } else {
878 val = 0x1 | (queue << 1);
879 val |= (val << 24) | (val << 16) | (val << 8);
880 }
881
882 for (offset = 0; offset <= 0xc; offset += 4)
883 mvreg_write(pp, MVNETA_DA_FILT_UCAST_BASE + offset, val);
884}
885
886/* Set all entries in Special Multicast MAC Table; queue==-1 means reject all */
887static void mvneta_set_special_mcast_table(struct mvneta_port *pp, int queue)
888{
889 int offset;
890 u32 val;
891
892 if (queue == -1) {
893 val = 0;
894 } else {
895 val = 0x1 | (queue << 1);
896 val |= (val << 24) | (val << 16) | (val << 8);
897 }
898
899 for (offset = 0; offset <= 0xfc; offset += 4)
900 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + offset, val);
901
902}
903
904/* Set all entries in Other Multicast MAC Table. queue==-1 means reject all */
905static void mvneta_set_other_mcast_table(struct mvneta_port *pp, int queue)
906{
907 int offset;
908 u32 val;
909
910 if (queue == -1) {
911 memset(pp->mcast_count, 0, sizeof(pp->mcast_count));
912 val = 0;
913 } else {
914 memset(pp->mcast_count, 1, sizeof(pp->mcast_count));
915 val = 0x1 | (queue << 1);
916 val |= (val << 24) | (val << 16) | (val << 8);
917 }
918
919 for (offset = 0; offset <= 0xfc; offset += 4)
920 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + offset, val);
921}
922
923/* This method sets defaults to the NETA port:
924 * Clears interrupt Cause and Mask registers.
925 * Clears all MAC tables.
926 * Sets defaults to all registers.
927 * Resets RX and TX descriptor rings.
928 * Resets PHY.
929 * This method can be called after mvneta_port_down() to return the port
930 * settings to defaults.
931 */
932static void mvneta_defaults_set(struct mvneta_port *pp)
933{
934 int cpu;
935 int queue;
936 u32 val;
937
938 /* Clear all Cause registers */
939 mvreg_write(pp, MVNETA_INTR_NEW_CAUSE, 0);
940 mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0);
941 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
942
943 /* Mask all interrupts */
944 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
945 mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0);
946 mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0);
947 mvreg_write(pp, MVNETA_INTR_ENABLE, 0);
948
949 /* Enable MBUS Retry bit16 */
950 mvreg_write(pp, MVNETA_MBUS_RETRY, 0x20);
951
952 /* Set CPU queue access map - all CPUs have access to all RX
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100953 * queues and to all TX queues
954 */
Maxime Ripard2502d0e2015-09-25 18:09:35 +0200955 for_each_present_cpu(cpu)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300956 mvreg_write(pp, MVNETA_CPU_MAP(cpu),
957 (MVNETA_CPU_RXQ_ACCESS_ALL_MASK |
958 MVNETA_CPU_TXQ_ACCESS_ALL_MASK));
959
960 /* Reset RX and TX DMAs */
961 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
962 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
963
964 /* Disable Legacy WRR, Disable EJP, Release from reset */
965 mvreg_write(pp, MVNETA_TXQ_CMD_1, 0);
966 for (queue = 0; queue < txq_number; queue++) {
967 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(queue), 0);
968 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(queue), 0);
969 }
970
971 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
972 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
973
974 /* Set Port Acceleration Mode */
975 val = MVNETA_ACC_MODE_EXT;
976 mvreg_write(pp, MVNETA_ACC_MODE, val);
977
978 /* Update val of portCfg register accordingly with all RxQueue types */
979 val = MVNETA_PORT_CONFIG_DEFL_VALUE(rxq_def);
980 mvreg_write(pp, MVNETA_PORT_CONFIG, val);
981
982 val = 0;
983 mvreg_write(pp, MVNETA_PORT_CONFIG_EXTEND, val);
984 mvreg_write(pp, MVNETA_RX_MIN_FRAME_SIZE, 64);
985
986 /* Build PORT_SDMA_CONFIG_REG */
987 val = 0;
988
989 /* Default burst size */
990 val |= MVNETA_TX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
991 val |= MVNETA_RX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +0200992 val |= MVNETA_RX_NO_DATA_SWAP | MVNETA_TX_NO_DATA_SWAP;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300993
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +0200994#if defined(__BIG_ENDIAN)
995 val |= MVNETA_DESC_SWAP;
996#endif
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300997
998 /* Assign port SDMA configuration */
999 mvreg_write(pp, MVNETA_SDMA_CONFIG, val);
1000
Thomas Petazzoni71408602013-09-04 16:21:18 +02001001 /* Disable PHY polling in hardware, since we're using the
1002 * kernel phylib to do this.
1003 */
1004 val = mvreg_read(pp, MVNETA_UNIT_CONTROL);
1005 val &= ~MVNETA_PHY_POLLING_ENABLE;
1006 mvreg_write(pp, MVNETA_UNIT_CONTROL, val);
1007
Stas Sergeev898b2972015-04-01 20:32:49 +03001008 if (pp->use_inband_status) {
1009 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
1010 val &= ~(MVNETA_GMAC_FORCE_LINK_PASS |
1011 MVNETA_GMAC_FORCE_LINK_DOWN |
1012 MVNETA_GMAC_AN_FLOW_CTRL_EN);
1013 val |= MVNETA_GMAC_INBAND_AN_ENABLE |
1014 MVNETA_GMAC_AN_SPEED_EN |
1015 MVNETA_GMAC_AN_DUPLEX_EN;
1016 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
1017 val = mvreg_read(pp, MVNETA_GMAC_CLOCK_DIVIDER);
1018 val |= MVNETA_GMAC_1MS_CLOCK_ENABLE;
1019 mvreg_write(pp, MVNETA_GMAC_CLOCK_DIVIDER, val);
Stas Sergeev538761b2015-06-18 18:36:03 +03001020 } else {
1021 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
1022 val &= ~(MVNETA_GMAC_INBAND_AN_ENABLE |
1023 MVNETA_GMAC_AN_SPEED_EN |
1024 MVNETA_GMAC_AN_DUPLEX_EN);
1025 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
Stas Sergeev898b2972015-04-01 20:32:49 +03001026 }
1027
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001028 mvneta_set_ucast_table(pp, -1);
1029 mvneta_set_special_mcast_table(pp, -1);
1030 mvneta_set_other_mcast_table(pp, -1);
1031
1032 /* Set port interrupt enable register - default enable all */
1033 mvreg_write(pp, MVNETA_INTR_ENABLE,
1034 (MVNETA_RXQ_INTR_ENABLE_ALL_MASK
1035 | MVNETA_TXQ_INTR_ENABLE_ALL_MASK));
1036}
1037
1038/* Set max sizes for tx queues */
1039static void mvneta_txq_max_tx_size_set(struct mvneta_port *pp, int max_tx_size)
1040
1041{
1042 u32 val, size, mtu;
1043 int queue;
1044
1045 mtu = max_tx_size * 8;
1046 if (mtu > MVNETA_TX_MTU_MAX)
1047 mtu = MVNETA_TX_MTU_MAX;
1048
1049 /* Set MTU */
1050 val = mvreg_read(pp, MVNETA_TX_MTU);
1051 val &= ~MVNETA_TX_MTU_MAX;
1052 val |= mtu;
1053 mvreg_write(pp, MVNETA_TX_MTU, val);
1054
1055 /* TX token size and all TXQs token size must be larger that MTU */
1056 val = mvreg_read(pp, MVNETA_TX_TOKEN_SIZE);
1057
1058 size = val & MVNETA_TX_TOKEN_SIZE_MAX;
1059 if (size < mtu) {
1060 size = mtu;
1061 val &= ~MVNETA_TX_TOKEN_SIZE_MAX;
1062 val |= size;
1063 mvreg_write(pp, MVNETA_TX_TOKEN_SIZE, val);
1064 }
1065 for (queue = 0; queue < txq_number; queue++) {
1066 val = mvreg_read(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue));
1067
1068 size = val & MVNETA_TXQ_TOKEN_SIZE_MAX;
1069 if (size < mtu) {
1070 size = mtu;
1071 val &= ~MVNETA_TXQ_TOKEN_SIZE_MAX;
1072 val |= size;
1073 mvreg_write(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue), val);
1074 }
1075 }
1076}
1077
1078/* Set unicast address */
1079static void mvneta_set_ucast_addr(struct mvneta_port *pp, u8 last_nibble,
1080 int queue)
1081{
1082 unsigned int unicast_reg;
1083 unsigned int tbl_offset;
1084 unsigned int reg_offset;
1085
1086 /* Locate the Unicast table entry */
1087 last_nibble = (0xf & last_nibble);
1088
1089 /* offset from unicast tbl base */
1090 tbl_offset = (last_nibble / 4) * 4;
1091
1092 /* offset within the above reg */
1093 reg_offset = last_nibble % 4;
1094
1095 unicast_reg = mvreg_read(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset));
1096
1097 if (queue == -1) {
1098 /* Clear accepts frame bit at specified unicast DA tbl entry */
1099 unicast_reg &= ~(0xff << (8 * reg_offset));
1100 } else {
1101 unicast_reg &= ~(0xff << (8 * reg_offset));
1102 unicast_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
1103 }
1104
1105 mvreg_write(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset), unicast_reg);
1106}
1107
1108/* Set mac address */
1109static void mvneta_mac_addr_set(struct mvneta_port *pp, unsigned char *addr,
1110 int queue)
1111{
1112 unsigned int mac_h;
1113 unsigned int mac_l;
1114
1115 if (queue != -1) {
1116 mac_l = (addr[4] << 8) | (addr[5]);
1117 mac_h = (addr[0] << 24) | (addr[1] << 16) |
1118 (addr[2] << 8) | (addr[3] << 0);
1119
1120 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, mac_l);
1121 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, mac_h);
1122 }
1123
1124 /* Accept frames of this address */
1125 mvneta_set_ucast_addr(pp, addr[5], queue);
1126}
1127
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001128/* Set the number of packets that will be received before RX interrupt
1129 * will be generated by HW.
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001130 */
1131static void mvneta_rx_pkts_coal_set(struct mvneta_port *pp,
1132 struct mvneta_rx_queue *rxq, u32 value)
1133{
1134 mvreg_write(pp, MVNETA_RXQ_THRESHOLD_REG(rxq->id),
1135 value | MVNETA_RXQ_NON_OCCUPIED(0));
1136 rxq->pkts_coal = value;
1137}
1138
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001139/* Set the time delay in usec before RX interrupt will be generated by
1140 * HW.
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001141 */
1142static void mvneta_rx_time_coal_set(struct mvneta_port *pp,
1143 struct mvneta_rx_queue *rxq, u32 value)
1144{
Thomas Petazzoni189dd622012-11-19 14:15:25 +01001145 u32 val;
1146 unsigned long clk_rate;
1147
1148 clk_rate = clk_get_rate(pp->clk);
1149 val = (clk_rate / 1000000) * value;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001150
1151 mvreg_write(pp, MVNETA_RXQ_TIME_COAL_REG(rxq->id), val);
1152 rxq->time_coal = value;
1153}
1154
1155/* Set threshold for TX_DONE pkts coalescing */
1156static void mvneta_tx_done_pkts_coal_set(struct mvneta_port *pp,
1157 struct mvneta_tx_queue *txq, u32 value)
1158{
1159 u32 val;
1160
1161 val = mvreg_read(pp, MVNETA_TXQ_SIZE_REG(txq->id));
1162
1163 val &= ~MVNETA_TXQ_SENT_THRESH_ALL_MASK;
1164 val |= MVNETA_TXQ_SENT_THRESH_MASK(value);
1165
1166 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), val);
1167
1168 txq->done_pkts_coal = value;
1169}
1170
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001171/* Handle rx descriptor fill by setting buf_cookie and buf_phys_addr */
1172static void mvneta_rx_desc_fill(struct mvneta_rx_desc *rx_desc,
1173 u32 phys_addr, u32 cookie)
1174{
1175 rx_desc->buf_cookie = cookie;
1176 rx_desc->buf_phys_addr = phys_addr;
1177}
1178
1179/* Decrement sent descriptors counter */
1180static void mvneta_txq_sent_desc_dec(struct mvneta_port *pp,
1181 struct mvneta_tx_queue *txq,
1182 int sent_desc)
1183{
1184 u32 val;
1185
1186 /* Only 255 TX descriptors can be updated at once */
1187 while (sent_desc > 0xff) {
1188 val = 0xff << MVNETA_TXQ_DEC_SENT_SHIFT;
1189 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1190 sent_desc = sent_desc - 0xff;
1191 }
1192
1193 val = sent_desc << MVNETA_TXQ_DEC_SENT_SHIFT;
1194 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1195}
1196
1197/* Get number of TX descriptors already sent by HW */
1198static int mvneta_txq_sent_desc_num_get(struct mvneta_port *pp,
1199 struct mvneta_tx_queue *txq)
1200{
1201 u32 val;
1202 int sent_desc;
1203
1204 val = mvreg_read(pp, MVNETA_TXQ_STATUS_REG(txq->id));
1205 sent_desc = (val & MVNETA_TXQ_SENT_DESC_MASK) >>
1206 MVNETA_TXQ_SENT_DESC_SHIFT;
1207
1208 return sent_desc;
1209}
1210
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001211/* Get number of sent descriptors and decrement counter.
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001212 * The number of sent descriptors is returned.
1213 */
1214static int mvneta_txq_sent_desc_proc(struct mvneta_port *pp,
1215 struct mvneta_tx_queue *txq)
1216{
1217 int sent_desc;
1218
1219 /* Get number of sent descriptors */
1220 sent_desc = mvneta_txq_sent_desc_num_get(pp, txq);
1221
1222 /* Decrement sent descriptors counter */
1223 if (sent_desc)
1224 mvneta_txq_sent_desc_dec(pp, txq, sent_desc);
1225
1226 return sent_desc;
1227}
1228
1229/* Set TXQ descriptors fields relevant for CSUM calculation */
1230static u32 mvneta_txq_desc_csum(int l3_offs, int l3_proto,
1231 int ip_hdr_len, int l4_proto)
1232{
1233 u32 command;
1234
1235 /* Fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001236 * G_L4_chk, L4_type; required only for checksum
1237 * calculation
1238 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001239 command = l3_offs << MVNETA_TX_L3_OFF_SHIFT;
1240 command |= ip_hdr_len << MVNETA_TX_IP_HLEN_SHIFT;
1241
Thomas Fitzsimmons0a198582014-07-08 19:44:07 -04001242 if (l3_proto == htons(ETH_P_IP))
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001243 command |= MVNETA_TXD_IP_CSUM;
1244 else
1245 command |= MVNETA_TX_L3_IP6;
1246
1247 if (l4_proto == IPPROTO_TCP)
1248 command |= MVNETA_TX_L4_CSUM_FULL;
1249 else if (l4_proto == IPPROTO_UDP)
1250 command |= MVNETA_TX_L4_UDP | MVNETA_TX_L4_CSUM_FULL;
1251 else
1252 command |= MVNETA_TX_L4_CSUM_NOT;
1253
1254 return command;
1255}
1256
1257
1258/* Display more error info */
1259static void mvneta_rx_error(struct mvneta_port *pp,
1260 struct mvneta_rx_desc *rx_desc)
1261{
1262 u32 status = rx_desc->status;
1263
willy tarreau54282132014-01-16 08:20:14 +01001264 if (!mvneta_rxq_desc_is_first_last(status)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001265 netdev_err(pp->dev,
1266 "bad rx status %08x (buffer oversize), size=%d\n",
willy tarreau54282132014-01-16 08:20:14 +01001267 status, rx_desc->data_size);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001268 return;
1269 }
1270
1271 switch (status & MVNETA_RXD_ERR_CODE_MASK) {
1272 case MVNETA_RXD_ERR_CRC:
1273 netdev_err(pp->dev, "bad rx status %08x (crc error), size=%d\n",
1274 status, rx_desc->data_size);
1275 break;
1276 case MVNETA_RXD_ERR_OVERRUN:
1277 netdev_err(pp->dev, "bad rx status %08x (overrun error), size=%d\n",
1278 status, rx_desc->data_size);
1279 break;
1280 case MVNETA_RXD_ERR_LEN:
1281 netdev_err(pp->dev, "bad rx status %08x (max frame length error), size=%d\n",
1282 status, rx_desc->data_size);
1283 break;
1284 case MVNETA_RXD_ERR_RESOURCE:
1285 netdev_err(pp->dev, "bad rx status %08x (resource error), size=%d\n",
1286 status, rx_desc->data_size);
1287 break;
1288 }
1289}
1290
willy tarreau54282132014-01-16 08:20:14 +01001291/* Handle RX checksum offload based on the descriptor's status */
1292static void mvneta_rx_csum(struct mvneta_port *pp, u32 status,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001293 struct sk_buff *skb)
1294{
willy tarreau54282132014-01-16 08:20:14 +01001295 if ((status & MVNETA_RXD_L3_IP4) &&
1296 (status & MVNETA_RXD_L4_CSUM_OK)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001297 skb->csum = 0;
1298 skb->ip_summed = CHECKSUM_UNNECESSARY;
1299 return;
1300 }
1301
1302 skb->ip_summed = CHECKSUM_NONE;
1303}
1304
willy tarreau6c498972014-01-16 08:20:12 +01001305/* Return tx queue pointer (find last set bit) according to <cause> returned
1306 * form tx_done reg. <cause> must not be null. The return value is always a
1307 * valid queue for matching the first one found in <cause>.
1308 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001309static struct mvneta_tx_queue *mvneta_tx_done_policy(struct mvneta_port *pp,
1310 u32 cause)
1311{
1312 int queue = fls(cause) - 1;
1313
willy tarreau6c498972014-01-16 08:20:12 +01001314 return &pp->txqs[queue];
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001315}
1316
1317/* Free tx queue skbuffs */
1318static void mvneta_txq_bufs_free(struct mvneta_port *pp,
1319 struct mvneta_tx_queue *txq, int num)
1320{
1321 int i;
1322
1323 for (i = 0; i < num; i++) {
1324 struct mvneta_tx_desc *tx_desc = txq->descs +
1325 txq->txq_get_index;
1326 struct sk_buff *skb = txq->tx_skb[txq->txq_get_index];
1327
1328 mvneta_txq_inc_get(txq);
1329
Ezequiel Garcia2e3173a2014-05-30 13:40:07 -03001330 if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr))
1331 dma_unmap_single(pp->dev->dev.parent,
1332 tx_desc->buf_phys_addr,
1333 tx_desc->data_size, DMA_TO_DEVICE);
Ezequiel Garciaba7e46e2014-05-30 13:40:06 -03001334 if (!skb)
1335 continue;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001336 dev_kfree_skb_any(skb);
1337 }
1338}
1339
1340/* Handle end of transmission */
Arnaud Ebalardcd713192014-01-16 08:20:19 +01001341static void mvneta_txq_done(struct mvneta_port *pp,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001342 struct mvneta_tx_queue *txq)
1343{
1344 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
1345 int tx_done;
1346
1347 tx_done = mvneta_txq_sent_desc_proc(pp, txq);
Arnaud Ebalardcd713192014-01-16 08:20:19 +01001348 if (!tx_done)
1349 return;
1350
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001351 mvneta_txq_bufs_free(pp, txq, tx_done);
1352
1353 txq->count -= tx_done;
1354
1355 if (netif_tx_queue_stopped(nq)) {
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03001356 if (txq->count <= txq->tx_wake_threshold)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001357 netif_tx_wake_queue(nq);
1358 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001359}
1360
willy tarreau8ec2cd42014-01-16 08:20:16 +01001361static void *mvneta_frag_alloc(const struct mvneta_port *pp)
1362{
1363 if (likely(pp->frag_size <= PAGE_SIZE))
1364 return netdev_alloc_frag(pp->frag_size);
1365 else
1366 return kmalloc(pp->frag_size, GFP_ATOMIC);
1367}
1368
1369static void mvneta_frag_free(const struct mvneta_port *pp, void *data)
1370{
1371 if (likely(pp->frag_size <= PAGE_SIZE))
Alexander Duyck13dc0d22015-05-06 21:12:14 -07001372 skb_free_frag(data);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001373 else
1374 kfree(data);
1375}
1376
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001377/* Refill processing */
1378static int mvneta_rx_refill(struct mvneta_port *pp,
1379 struct mvneta_rx_desc *rx_desc)
1380
1381{
1382 dma_addr_t phys_addr;
willy tarreau8ec2cd42014-01-16 08:20:16 +01001383 void *data;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001384
willy tarreau8ec2cd42014-01-16 08:20:16 +01001385 data = mvneta_frag_alloc(pp);
1386 if (!data)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001387 return -ENOMEM;
1388
willy tarreau8ec2cd42014-01-16 08:20:16 +01001389 phys_addr = dma_map_single(pp->dev->dev.parent, data,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001390 MVNETA_RX_BUF_SIZE(pp->pkt_size),
1391 DMA_FROM_DEVICE);
1392 if (unlikely(dma_mapping_error(pp->dev->dev.parent, phys_addr))) {
willy tarreau8ec2cd42014-01-16 08:20:16 +01001393 mvneta_frag_free(pp, data);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001394 return -ENOMEM;
1395 }
1396
willy tarreau8ec2cd42014-01-16 08:20:16 +01001397 mvneta_rx_desc_fill(rx_desc, phys_addr, (u32)data);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001398 return 0;
1399}
1400
1401/* Handle tx checksum */
1402static u32 mvneta_skb_tx_csum(struct mvneta_port *pp, struct sk_buff *skb)
1403{
1404 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1405 int ip_hdr_len = 0;
Vlad Yasevich817dbfa2014-08-25 10:34:54 -04001406 __be16 l3_proto = vlan_get_protocol(skb);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001407 u8 l4_proto;
1408
Vlad Yasevich817dbfa2014-08-25 10:34:54 -04001409 if (l3_proto == htons(ETH_P_IP)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001410 struct iphdr *ip4h = ip_hdr(skb);
1411
1412 /* Calculate IPv4 checksum and L4 checksum */
1413 ip_hdr_len = ip4h->ihl;
1414 l4_proto = ip4h->protocol;
Vlad Yasevich817dbfa2014-08-25 10:34:54 -04001415 } else if (l3_proto == htons(ETH_P_IPV6)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001416 struct ipv6hdr *ip6h = ipv6_hdr(skb);
1417
1418 /* Read l4_protocol from one of IPv6 extra headers */
1419 if (skb_network_header_len(skb) > 0)
1420 ip_hdr_len = (skb_network_header_len(skb) >> 2);
1421 l4_proto = ip6h->nexthdr;
1422 } else
1423 return MVNETA_TX_L4_CSUM_NOT;
1424
1425 return mvneta_txq_desc_csum(skb_network_offset(skb),
Vlad Yasevich817dbfa2014-08-25 10:34:54 -04001426 l3_proto, ip_hdr_len, l4_proto);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001427 }
1428
1429 return MVNETA_TX_L4_CSUM_NOT;
1430}
1431
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001432/* Drop packets received by the RXQ and free buffers */
1433static void mvneta_rxq_drop_pkts(struct mvneta_port *pp,
1434 struct mvneta_rx_queue *rxq)
1435{
1436 int rx_done, i;
1437
1438 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
1439 for (i = 0; i < rxq->size; i++) {
1440 struct mvneta_rx_desc *rx_desc = rxq->descs + i;
willy tarreau8ec2cd42014-01-16 08:20:16 +01001441 void *data = (void *)rx_desc->buf_cookie;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001442
willy tarreau8ec2cd42014-01-16 08:20:16 +01001443 mvneta_frag_free(pp, data);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001444 dma_unmap_single(pp->dev->dev.parent, rx_desc->buf_phys_addr,
Ezequiel Garciaa328f3a2013-12-05 13:35:37 -03001445 MVNETA_RX_BUF_SIZE(pp->pkt_size), DMA_FROM_DEVICE);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001446 }
1447
1448 if (rx_done)
1449 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
1450}
1451
1452/* Main rx processing */
1453static int mvneta_rx(struct mvneta_port *pp, int rx_todo,
1454 struct mvneta_rx_queue *rxq)
1455{
Maxime Ripard12bb03b2015-09-25 18:09:36 +02001456 struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001457 struct net_device *dev = pp->dev;
Simon Guinota84e3282015-07-19 13:00:53 +02001458 int rx_done;
willy tarreaudc4277d2014-01-16 08:20:07 +01001459 u32 rcvd_pkts = 0;
1460 u32 rcvd_bytes = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001461
1462 /* Get number of received packets */
1463 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
1464
1465 if (rx_todo > rx_done)
1466 rx_todo = rx_done;
1467
1468 rx_done = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001469
1470 /* Fairness NAPI loop */
1471 while (rx_done < rx_todo) {
1472 struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
1473 struct sk_buff *skb;
willy tarreau8ec2cd42014-01-16 08:20:16 +01001474 unsigned char *data;
Simon Guinotdaf158d2015-09-15 22:41:21 +02001475 dma_addr_t phys_addr;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001476 u32 rx_status;
1477 int rx_bytes, err;
1478
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001479 rx_done++;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001480 rx_status = rx_desc->status;
willy tarreauf19fadf2014-01-16 08:20:17 +01001481 rx_bytes = rx_desc->data_size - (ETH_FCS_LEN + MVNETA_MH_SIZE);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001482 data = (unsigned char *)rx_desc->buf_cookie;
Simon Guinotdaf158d2015-09-15 22:41:21 +02001483 phys_addr = rx_desc->buf_phys_addr;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001484
willy tarreau54282132014-01-16 08:20:14 +01001485 if (!mvneta_rxq_desc_is_first_last(rx_status) ||
willy tarreauf19fadf2014-01-16 08:20:17 +01001486 (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
1487 err_drop_frame:
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001488 dev->stats.rx_errors++;
1489 mvneta_rx_error(pp, rx_desc);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001490 /* leave the descriptor untouched */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001491 continue;
1492 }
1493
willy tarreauf19fadf2014-01-16 08:20:17 +01001494 if (rx_bytes <= rx_copybreak) {
1495 /* better copy a small frame and not unmap the DMA region */
1496 skb = netdev_alloc_skb_ip_align(dev, rx_bytes);
1497 if (unlikely(!skb))
1498 goto err_drop_frame;
1499
1500 dma_sync_single_range_for_cpu(dev->dev.parent,
1501 rx_desc->buf_phys_addr,
1502 MVNETA_MH_SIZE + NET_SKB_PAD,
1503 rx_bytes,
1504 DMA_FROM_DEVICE);
1505 memcpy(skb_put(skb, rx_bytes),
1506 data + MVNETA_MH_SIZE + NET_SKB_PAD,
1507 rx_bytes);
1508
1509 skb->protocol = eth_type_trans(skb, dev);
1510 mvneta_rx_csum(pp, rx_status, skb);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02001511 napi_gro_receive(&port->napi, skb);
willy tarreauf19fadf2014-01-16 08:20:17 +01001512
1513 rcvd_pkts++;
1514 rcvd_bytes += rx_bytes;
1515
1516 /* leave the descriptor and buffer untouched */
1517 continue;
1518 }
1519
Simon Guinota84e3282015-07-19 13:00:53 +02001520 /* Refill processing */
1521 err = mvneta_rx_refill(pp, rx_desc);
1522 if (err) {
1523 netdev_err(dev, "Linux processing - Can't refill\n");
1524 rxq->missed++;
1525 goto err_drop_frame;
1526 }
1527
willy tarreauf19fadf2014-01-16 08:20:17 +01001528 skb = build_skb(data, pp->frag_size > PAGE_SIZE ? 0 : pp->frag_size);
1529 if (!skb)
1530 goto err_drop_frame;
1531
Simon Guinotdaf158d2015-09-15 22:41:21 +02001532 dma_unmap_single(dev->dev.parent, phys_addr,
Ezequiel Garciaa328f3a2013-12-05 13:35:37 -03001533 MVNETA_RX_BUF_SIZE(pp->pkt_size), DMA_FROM_DEVICE);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001534
willy tarreaudc4277d2014-01-16 08:20:07 +01001535 rcvd_pkts++;
1536 rcvd_bytes += rx_bytes;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001537
1538 /* Linux processing */
willy tarreau8ec2cd42014-01-16 08:20:16 +01001539 skb_reserve(skb, MVNETA_MH_SIZE + NET_SKB_PAD);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001540 skb_put(skb, rx_bytes);
1541
1542 skb->protocol = eth_type_trans(skb, dev);
1543
willy tarreau54282132014-01-16 08:20:14 +01001544 mvneta_rx_csum(pp, rx_status, skb);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001545
Maxime Ripard12bb03b2015-09-25 18:09:36 +02001546 napi_gro_receive(&port->napi, skb);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001547 }
1548
willy tarreaudc4277d2014-01-16 08:20:07 +01001549 if (rcvd_pkts) {
willy tarreau74c41b02014-01-16 08:20:08 +01001550 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
1551
1552 u64_stats_update_begin(&stats->syncp);
1553 stats->rx_packets += rcvd_pkts;
1554 stats->rx_bytes += rcvd_bytes;
1555 u64_stats_update_end(&stats->syncp);
willy tarreaudc4277d2014-01-16 08:20:07 +01001556 }
1557
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001558 /* Update rxq management counters */
Simon Guinota84e3282015-07-19 13:00:53 +02001559 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001560
1561 return rx_done;
1562}
1563
Ezequiel Garcia2adb7192014-05-19 13:59:55 -03001564static inline void
1565mvneta_tso_put_hdr(struct sk_buff *skb,
1566 struct mvneta_port *pp, struct mvneta_tx_queue *txq)
1567{
1568 struct mvneta_tx_desc *tx_desc;
1569 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1570
1571 txq->tx_skb[txq->txq_put_index] = NULL;
1572 tx_desc = mvneta_txq_next_desc_get(txq);
1573 tx_desc->data_size = hdr_len;
1574 tx_desc->command = mvneta_skb_tx_csum(pp, skb);
1575 tx_desc->command |= MVNETA_TXD_F_DESC;
1576 tx_desc->buf_phys_addr = txq->tso_hdrs_phys +
1577 txq->txq_put_index * TSO_HEADER_SIZE;
1578 mvneta_txq_inc_put(txq);
1579}
1580
1581static inline int
1582mvneta_tso_put_data(struct net_device *dev, struct mvneta_tx_queue *txq,
1583 struct sk_buff *skb, char *data, int size,
1584 bool last_tcp, bool is_last)
1585{
1586 struct mvneta_tx_desc *tx_desc;
1587
1588 tx_desc = mvneta_txq_next_desc_get(txq);
1589 tx_desc->data_size = size;
1590 tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, data,
1591 size, DMA_TO_DEVICE);
1592 if (unlikely(dma_mapping_error(dev->dev.parent,
1593 tx_desc->buf_phys_addr))) {
1594 mvneta_txq_desc_put(txq);
1595 return -ENOMEM;
1596 }
1597
1598 tx_desc->command = 0;
1599 txq->tx_skb[txq->txq_put_index] = NULL;
1600
1601 if (last_tcp) {
1602 /* last descriptor in the TCP packet */
1603 tx_desc->command = MVNETA_TXD_L_DESC;
1604
1605 /* last descriptor in SKB */
1606 if (is_last)
1607 txq->tx_skb[txq->txq_put_index] = skb;
1608 }
1609 mvneta_txq_inc_put(txq);
1610 return 0;
1611}
1612
1613static int mvneta_tx_tso(struct sk_buff *skb, struct net_device *dev,
1614 struct mvneta_tx_queue *txq)
1615{
1616 int total_len, data_left;
1617 int desc_count = 0;
1618 struct mvneta_port *pp = netdev_priv(dev);
1619 struct tso_t tso;
1620 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1621 int i;
1622
1623 /* Count needed descriptors */
1624 if ((txq->count + tso_count_descs(skb)) >= txq->size)
1625 return 0;
1626
1627 if (skb_headlen(skb) < (skb_transport_offset(skb) + tcp_hdrlen(skb))) {
1628 pr_info("*** Is this even possible???!?!?\n");
1629 return 0;
1630 }
1631
1632 /* Initialize the TSO handler, and prepare the first payload */
1633 tso_start(skb, &tso);
1634
1635 total_len = skb->len - hdr_len;
1636 while (total_len > 0) {
1637 char *hdr;
1638
1639 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
1640 total_len -= data_left;
1641 desc_count++;
1642
1643 /* prepare packet headers: MAC + IP + TCP */
1644 hdr = txq->tso_hdrs + txq->txq_put_index * TSO_HEADER_SIZE;
1645 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
1646
1647 mvneta_tso_put_hdr(skb, pp, txq);
1648
1649 while (data_left > 0) {
1650 int size;
1651 desc_count++;
1652
1653 size = min_t(int, tso.size, data_left);
1654
1655 if (mvneta_tso_put_data(dev, txq, skb,
1656 tso.data, size,
1657 size == data_left,
1658 total_len == 0))
1659 goto err_release;
1660 data_left -= size;
1661
1662 tso_build_data(skb, &tso, size);
1663 }
1664 }
1665
1666 return desc_count;
1667
1668err_release:
1669 /* Release all used data descriptors; header descriptors must not
1670 * be DMA-unmapped.
1671 */
1672 for (i = desc_count - 1; i >= 0; i--) {
1673 struct mvneta_tx_desc *tx_desc = txq->descs + i;
Ezequiel Garcia2e3173a2014-05-30 13:40:07 -03001674 if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr))
Ezequiel Garcia2adb7192014-05-19 13:59:55 -03001675 dma_unmap_single(pp->dev->dev.parent,
1676 tx_desc->buf_phys_addr,
1677 tx_desc->data_size,
1678 DMA_TO_DEVICE);
1679 mvneta_txq_desc_put(txq);
1680 }
1681 return 0;
1682}
1683
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001684/* Handle tx fragmentation processing */
1685static int mvneta_tx_frag_process(struct mvneta_port *pp, struct sk_buff *skb,
1686 struct mvneta_tx_queue *txq)
1687{
1688 struct mvneta_tx_desc *tx_desc;
Ezequiel Garcia3d4ea022014-05-22 20:06:57 -03001689 int i, nr_frags = skb_shinfo(skb)->nr_frags;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001690
Ezequiel Garcia3d4ea022014-05-22 20:06:57 -03001691 for (i = 0; i < nr_frags; i++) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001692 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1693 void *addr = page_address(frag->page.p) + frag->page_offset;
1694
1695 tx_desc = mvneta_txq_next_desc_get(txq);
1696 tx_desc->data_size = frag->size;
1697
1698 tx_desc->buf_phys_addr =
1699 dma_map_single(pp->dev->dev.parent, addr,
1700 tx_desc->data_size, DMA_TO_DEVICE);
1701
1702 if (dma_mapping_error(pp->dev->dev.parent,
1703 tx_desc->buf_phys_addr)) {
1704 mvneta_txq_desc_put(txq);
1705 goto error;
1706 }
1707
Ezequiel Garcia3d4ea022014-05-22 20:06:57 -03001708 if (i == nr_frags - 1) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001709 /* Last descriptor */
1710 tx_desc->command = MVNETA_TXD_L_DESC | MVNETA_TXD_Z_PAD;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001711 txq->tx_skb[txq->txq_put_index] = skb;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001712 } else {
1713 /* Descriptor in the middle: Not First, Not Last */
1714 tx_desc->command = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001715 txq->tx_skb[txq->txq_put_index] = NULL;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001716 }
Ezequiel Garcia3d4ea022014-05-22 20:06:57 -03001717 mvneta_txq_inc_put(txq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001718 }
1719
1720 return 0;
1721
1722error:
1723 /* Release all descriptors that were used to map fragments of
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001724 * this packet, as well as the corresponding DMA mappings
1725 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001726 for (i = i - 1; i >= 0; i--) {
1727 tx_desc = txq->descs + i;
1728 dma_unmap_single(pp->dev->dev.parent,
1729 tx_desc->buf_phys_addr,
1730 tx_desc->data_size,
1731 DMA_TO_DEVICE);
1732 mvneta_txq_desc_put(txq);
1733 }
1734
1735 return -ENOMEM;
1736}
1737
1738/* Main tx processing */
1739static int mvneta_tx(struct sk_buff *skb, struct net_device *dev)
1740{
1741 struct mvneta_port *pp = netdev_priv(dev);
Willy Tarreauee40a112013-04-11 23:00:37 +02001742 u16 txq_id = skb_get_queue_mapping(skb);
1743 struct mvneta_tx_queue *txq = &pp->txqs[txq_id];
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001744 struct mvneta_tx_desc *tx_desc;
Eric Dumazet5f478b42014-12-02 04:30:59 -08001745 int len = skb->len;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001746 int frags = 0;
1747 u32 tx_cmd;
1748
1749 if (!netif_running(dev))
1750 goto out;
1751
Ezequiel Garcia2adb7192014-05-19 13:59:55 -03001752 if (skb_is_gso(skb)) {
1753 frags = mvneta_tx_tso(skb, dev, txq);
1754 goto out;
1755 }
1756
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001757 frags = skb_shinfo(skb)->nr_frags + 1;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001758
1759 /* Get a descriptor for the first part of the packet */
1760 tx_desc = mvneta_txq_next_desc_get(txq);
1761
1762 tx_cmd = mvneta_skb_tx_csum(pp, skb);
1763
1764 tx_desc->data_size = skb_headlen(skb);
1765
1766 tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, skb->data,
1767 tx_desc->data_size,
1768 DMA_TO_DEVICE);
1769 if (unlikely(dma_mapping_error(dev->dev.parent,
1770 tx_desc->buf_phys_addr))) {
1771 mvneta_txq_desc_put(txq);
1772 frags = 0;
1773 goto out;
1774 }
1775
1776 if (frags == 1) {
1777 /* First and Last descriptor */
1778 tx_cmd |= MVNETA_TXD_FLZ_DESC;
1779 tx_desc->command = tx_cmd;
1780 txq->tx_skb[txq->txq_put_index] = skb;
1781 mvneta_txq_inc_put(txq);
1782 } else {
1783 /* First but not Last */
1784 tx_cmd |= MVNETA_TXD_F_DESC;
1785 txq->tx_skb[txq->txq_put_index] = NULL;
1786 mvneta_txq_inc_put(txq);
1787 tx_desc->command = tx_cmd;
1788 /* Continue with other skb fragments */
1789 if (mvneta_tx_frag_process(pp, skb, txq)) {
1790 dma_unmap_single(dev->dev.parent,
1791 tx_desc->buf_phys_addr,
1792 tx_desc->data_size,
1793 DMA_TO_DEVICE);
1794 mvneta_txq_desc_put(txq);
1795 frags = 0;
1796 goto out;
1797 }
1798 }
1799
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001800out:
1801 if (frags > 0) {
willy tarreau74c41b02014-01-16 08:20:08 +01001802 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
Ezequiel Garciae19d2dd2014-05-19 13:59:54 -03001803 struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
1804
1805 txq->count += frags;
1806 mvneta_txq_pend_desc_add(pp, txq, frags);
1807
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03001808 if (txq->count >= txq->tx_stop_threshold)
Ezequiel Garciae19d2dd2014-05-19 13:59:54 -03001809 netif_tx_stop_queue(nq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001810
willy tarreau74c41b02014-01-16 08:20:08 +01001811 u64_stats_update_begin(&stats->syncp);
1812 stats->tx_packets++;
Eric Dumazet5f478b42014-12-02 04:30:59 -08001813 stats->tx_bytes += len;
willy tarreau74c41b02014-01-16 08:20:08 +01001814 u64_stats_update_end(&stats->syncp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001815 } else {
1816 dev->stats.tx_dropped++;
1817 dev_kfree_skb_any(skb);
1818 }
1819
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001820 return NETDEV_TX_OK;
1821}
1822
1823
1824/* Free tx resources, when resetting a port */
1825static void mvneta_txq_done_force(struct mvneta_port *pp,
1826 struct mvneta_tx_queue *txq)
1827
1828{
1829 int tx_done = txq->count;
1830
1831 mvneta_txq_bufs_free(pp, txq, tx_done);
1832
1833 /* reset txq */
1834 txq->count = 0;
1835 txq->txq_put_index = 0;
1836 txq->txq_get_index = 0;
1837}
1838
willy tarreau6c498972014-01-16 08:20:12 +01001839/* Handle tx done - called in softirq context. The <cause_tx_done> argument
1840 * must be a valid cause according to MVNETA_TXQ_INTR_MASK_ALL.
1841 */
Arnaud Ebalard0713a862014-01-16 08:20:18 +01001842static void mvneta_tx_done_gbe(struct mvneta_port *pp, u32 cause_tx_done)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001843{
1844 struct mvneta_tx_queue *txq;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001845 struct netdev_queue *nq;
1846
willy tarreau6c498972014-01-16 08:20:12 +01001847 while (cause_tx_done) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001848 txq = mvneta_tx_done_policy(pp, cause_tx_done);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001849
1850 nq = netdev_get_tx_queue(pp->dev, txq->id);
1851 __netif_tx_lock(nq, smp_processor_id());
1852
Arnaud Ebalard0713a862014-01-16 08:20:18 +01001853 if (txq->count)
1854 mvneta_txq_done(pp, txq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001855
1856 __netif_tx_unlock(nq);
1857 cause_tx_done &= ~((1 << txq->id));
1858 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001859}
1860
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001861/* Compute crc8 of the specified address, using a unique algorithm ,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001862 * according to hw spec, different than generic crc8 algorithm
1863 */
1864static int mvneta_addr_crc(unsigned char *addr)
1865{
1866 int crc = 0;
1867 int i;
1868
1869 for (i = 0; i < ETH_ALEN; i++) {
1870 int j;
1871
1872 crc = (crc ^ addr[i]) << 8;
1873 for (j = 7; j >= 0; j--) {
1874 if (crc & (0x100 << j))
1875 crc ^= 0x107 << j;
1876 }
1877 }
1878
1879 return crc;
1880}
1881
1882/* This method controls the net device special MAC multicast support.
1883 * The Special Multicast Table for MAC addresses supports MAC of the form
1884 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
1885 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
1886 * Table entries in the DA-Filter table. This method set the Special
1887 * Multicast Table appropriate entry.
1888 */
1889static void mvneta_set_special_mcast_addr(struct mvneta_port *pp,
1890 unsigned char last_byte,
1891 int queue)
1892{
1893 unsigned int smc_table_reg;
1894 unsigned int tbl_offset;
1895 unsigned int reg_offset;
1896
1897 /* Register offset from SMC table base */
1898 tbl_offset = (last_byte / 4);
1899 /* Entry offset within the above reg */
1900 reg_offset = last_byte % 4;
1901
1902 smc_table_reg = mvreg_read(pp, (MVNETA_DA_FILT_SPEC_MCAST
1903 + tbl_offset * 4));
1904
1905 if (queue == -1)
1906 smc_table_reg &= ~(0xff << (8 * reg_offset));
1907 else {
1908 smc_table_reg &= ~(0xff << (8 * reg_offset));
1909 smc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
1910 }
1911
1912 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + tbl_offset * 4,
1913 smc_table_reg);
1914}
1915
1916/* This method controls the network device Other MAC multicast support.
1917 * The Other Multicast Table is used for multicast of another type.
1918 * A CRC-8 is used as an index to the Other Multicast Table entries
1919 * in the DA-Filter table.
1920 * The method gets the CRC-8 value from the calling routine and
1921 * sets the Other Multicast Table appropriate entry according to the
1922 * specified CRC-8 .
1923 */
1924static void mvneta_set_other_mcast_addr(struct mvneta_port *pp,
1925 unsigned char crc8,
1926 int queue)
1927{
1928 unsigned int omc_table_reg;
1929 unsigned int tbl_offset;
1930 unsigned int reg_offset;
1931
1932 tbl_offset = (crc8 / 4) * 4; /* Register offset from OMC table base */
1933 reg_offset = crc8 % 4; /* Entry offset within the above reg */
1934
1935 omc_table_reg = mvreg_read(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset);
1936
1937 if (queue == -1) {
1938 /* Clear accepts frame bit at specified Other DA table entry */
1939 omc_table_reg &= ~(0xff << (8 * reg_offset));
1940 } else {
1941 omc_table_reg &= ~(0xff << (8 * reg_offset));
1942 omc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
1943 }
1944
1945 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset, omc_table_reg);
1946}
1947
1948/* The network device supports multicast using two tables:
1949 * 1) Special Multicast Table for MAC addresses of the form
1950 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
1951 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
1952 * Table entries in the DA-Filter table.
1953 * 2) Other Multicast Table for multicast of another type. A CRC-8 value
1954 * is used as an index to the Other Multicast Table entries in the
1955 * DA-Filter table.
1956 */
1957static int mvneta_mcast_addr_set(struct mvneta_port *pp, unsigned char *p_addr,
1958 int queue)
1959{
1960 unsigned char crc_result = 0;
1961
1962 if (memcmp(p_addr, "\x01\x00\x5e\x00\x00", 5) == 0) {
1963 mvneta_set_special_mcast_addr(pp, p_addr[5], queue);
1964 return 0;
1965 }
1966
1967 crc_result = mvneta_addr_crc(p_addr);
1968 if (queue == -1) {
1969 if (pp->mcast_count[crc_result] == 0) {
1970 netdev_info(pp->dev, "No valid Mcast for crc8=0x%02x\n",
1971 crc_result);
1972 return -EINVAL;
1973 }
1974
1975 pp->mcast_count[crc_result]--;
1976 if (pp->mcast_count[crc_result] != 0) {
1977 netdev_info(pp->dev,
1978 "After delete there are %d valid Mcast for crc8=0x%02x\n",
1979 pp->mcast_count[crc_result], crc_result);
1980 return -EINVAL;
1981 }
1982 } else
1983 pp->mcast_count[crc_result]++;
1984
1985 mvneta_set_other_mcast_addr(pp, crc_result, queue);
1986
1987 return 0;
1988}
1989
1990/* Configure Fitering mode of Ethernet port */
1991static void mvneta_rx_unicast_promisc_set(struct mvneta_port *pp,
1992 int is_promisc)
1993{
1994 u32 port_cfg_reg, val;
1995
1996 port_cfg_reg = mvreg_read(pp, MVNETA_PORT_CONFIG);
1997
1998 val = mvreg_read(pp, MVNETA_TYPE_PRIO);
1999
2000 /* Set / Clear UPM bit in port configuration register */
2001 if (is_promisc) {
2002 /* Accept all Unicast addresses */
2003 port_cfg_reg |= MVNETA_UNI_PROMISC_MODE;
2004 val |= MVNETA_FORCE_UNI;
2005 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, 0xffff);
2006 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, 0xffffffff);
2007 } else {
2008 /* Reject all Unicast addresses */
2009 port_cfg_reg &= ~MVNETA_UNI_PROMISC_MODE;
2010 val &= ~MVNETA_FORCE_UNI;
2011 }
2012
2013 mvreg_write(pp, MVNETA_PORT_CONFIG, port_cfg_reg);
2014 mvreg_write(pp, MVNETA_TYPE_PRIO, val);
2015}
2016
2017/* register unicast and multicast addresses */
2018static void mvneta_set_rx_mode(struct net_device *dev)
2019{
2020 struct mvneta_port *pp = netdev_priv(dev);
2021 struct netdev_hw_addr *ha;
2022
2023 if (dev->flags & IFF_PROMISC) {
2024 /* Accept all: Multicast + Unicast */
2025 mvneta_rx_unicast_promisc_set(pp, 1);
2026 mvneta_set_ucast_table(pp, rxq_def);
2027 mvneta_set_special_mcast_table(pp, rxq_def);
2028 mvneta_set_other_mcast_table(pp, rxq_def);
2029 } else {
2030 /* Accept single Unicast */
2031 mvneta_rx_unicast_promisc_set(pp, 0);
2032 mvneta_set_ucast_table(pp, -1);
2033 mvneta_mac_addr_set(pp, dev->dev_addr, rxq_def);
2034
2035 if (dev->flags & IFF_ALLMULTI) {
2036 /* Accept all multicast */
2037 mvneta_set_special_mcast_table(pp, rxq_def);
2038 mvneta_set_other_mcast_table(pp, rxq_def);
2039 } else {
2040 /* Accept only initialized multicast */
2041 mvneta_set_special_mcast_table(pp, -1);
2042 mvneta_set_other_mcast_table(pp, -1);
2043
2044 if (!netdev_mc_empty(dev)) {
2045 netdev_for_each_mc_addr(ha, dev) {
2046 mvneta_mcast_addr_set(pp, ha->addr,
2047 rxq_def);
2048 }
2049 }
2050 }
2051 }
2052}
2053
2054/* Interrupt handling - the callback for request_irq() */
2055static irqreturn_t mvneta_isr(int irq, void *dev_id)
2056{
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002057 struct mvneta_pcpu_port *port = (struct mvneta_pcpu_port *)dev_id;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002058
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002059 disable_percpu_irq(port->pp->dev->irq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002060
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002061 napi_schedule(&port->napi);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002062
2063 return IRQ_HANDLED;
2064}
2065
Stas Sergeev898b2972015-04-01 20:32:49 +03002066static int mvneta_fixed_link_update(struct mvneta_port *pp,
2067 struct phy_device *phy)
2068{
2069 struct fixed_phy_status status;
2070 struct fixed_phy_status changed = {};
2071 u32 gmac_stat = mvreg_read(pp, MVNETA_GMAC_STATUS);
2072
2073 status.link = !!(gmac_stat & MVNETA_GMAC_LINK_UP);
2074 if (gmac_stat & MVNETA_GMAC_SPEED_1000)
2075 status.speed = SPEED_1000;
2076 else if (gmac_stat & MVNETA_GMAC_SPEED_100)
2077 status.speed = SPEED_100;
2078 else
2079 status.speed = SPEED_10;
2080 status.duplex = !!(gmac_stat & MVNETA_GMAC_FULL_DUPLEX);
2081 changed.link = 1;
2082 changed.speed = 1;
2083 changed.duplex = 1;
2084 fixed_phy_update_state(phy, &status, &changed);
2085 return 0;
2086}
2087
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002088/* NAPI handler
2089 * Bits 0 - 7 of the causeRxTx register indicate that are transmitted
2090 * packets on the corresponding TXQ (Bit 0 is for TX queue 1).
2091 * Bits 8 -15 of the cause Rx Tx register indicate that are received
2092 * packets on the corresponding RXQ (Bit 8 is for RX queue 0).
2093 * Each CPU has its own causeRxTx register
2094 */
2095static int mvneta_poll(struct napi_struct *napi, int budget)
2096{
2097 int rx_done = 0;
2098 u32 cause_rx_tx;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002099 struct mvneta_port *pp = netdev_priv(napi->dev);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002100 struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002101
2102 if (!netif_running(pp->dev)) {
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002103 napi_complete(&port->napi);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002104 return rx_done;
2105 }
2106
2107 /* Read cause register */
Stas Sergeev898b2972015-04-01 20:32:49 +03002108 cause_rx_tx = mvreg_read(pp, MVNETA_INTR_NEW_CAUSE);
2109 if (cause_rx_tx & MVNETA_MISCINTR_INTR_MASK) {
2110 u32 cause_misc = mvreg_read(pp, MVNETA_INTR_MISC_CAUSE);
2111
2112 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
2113 if (pp->use_inband_status && (cause_misc &
2114 (MVNETA_CAUSE_PHY_STATUS_CHANGE |
2115 MVNETA_CAUSE_LINK_CHANGE |
2116 MVNETA_CAUSE_PSC_SYNC_CHANGE))) {
2117 mvneta_fixed_link_update(pp, pp->phy_dev);
2118 }
2119 }
willy tarreau71f6d1b2014-01-16 08:20:11 +01002120
2121 /* Release Tx descriptors */
2122 if (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL) {
Arnaud Ebalard0713a862014-01-16 08:20:18 +01002123 mvneta_tx_done_gbe(pp, (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL));
willy tarreau71f6d1b2014-01-16 08:20:11 +01002124 cause_rx_tx &= ~MVNETA_TX_INTR_MASK_ALL;
2125 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002126
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002127 /* For the case where the last mvneta_poll did not process all
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002128 * RX packets
2129 */
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002130 cause_rx_tx |= port->cause_rx_tx;
Maxime Ripardd8936652015-09-25 18:09:37 +02002131 rx_done = mvneta_rx(pp, budget, &pp->rxqs[rxq_def]);
2132 budget -= rx_done;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002133
2134 if (budget > 0) {
2135 cause_rx_tx = 0;
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002136 napi_complete(&port->napi);
2137 enable_percpu_irq(pp->dev->irq, 0);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002138 }
2139
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002140 port->cause_rx_tx = cause_rx_tx;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002141 return rx_done;
2142}
2143
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002144/* Handle rxq fill: allocates rxq skbs; called when initializing a port */
2145static int mvneta_rxq_fill(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
2146 int num)
2147{
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002148 int i;
2149
2150 for (i = 0; i < num; i++) {
willy tarreaua1a65ab2014-01-16 08:20:13 +01002151 memset(rxq->descs + i, 0, sizeof(struct mvneta_rx_desc));
2152 if (mvneta_rx_refill(pp, rxq->descs + i) != 0) {
2153 netdev_err(pp->dev, "%s:rxq %d, %d of %d buffs filled\n",
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002154 __func__, rxq->id, i, num);
2155 break;
2156 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002157 }
2158
2159 /* Add this number of RX descriptors as non occupied (ready to
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002160 * get packets)
2161 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002162 mvneta_rxq_non_occup_desc_add(pp, rxq, i);
2163
2164 return i;
2165}
2166
2167/* Free all packets pending transmit from all TXQs and reset TX port */
2168static void mvneta_tx_reset(struct mvneta_port *pp)
2169{
2170 int queue;
2171
Ezequiel Garcia96728502014-05-22 20:06:59 -03002172 /* free the skb's in the tx ring */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002173 for (queue = 0; queue < txq_number; queue++)
2174 mvneta_txq_done_force(pp, &pp->txqs[queue]);
2175
2176 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
2177 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
2178}
2179
2180static void mvneta_rx_reset(struct mvneta_port *pp)
2181{
2182 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
2183 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
2184}
2185
2186/* Rx/Tx queue initialization/cleanup methods */
2187
2188/* Create a specified RX queue */
2189static int mvneta_rxq_init(struct mvneta_port *pp,
2190 struct mvneta_rx_queue *rxq)
2191
2192{
2193 rxq->size = pp->rx_ring_size;
2194
2195 /* Allocate memory for RX descriptors */
2196 rxq->descs = dma_alloc_coherent(pp->dev->dev.parent,
2197 rxq->size * MVNETA_DESC_ALIGNED_SIZE,
2198 &rxq->descs_phys, GFP_KERNEL);
Joe Perchesd0320f72013-03-14 13:07:21 +00002199 if (rxq->descs == NULL)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002200 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002201
2202 BUG_ON(rxq->descs !=
2203 PTR_ALIGN(rxq->descs, MVNETA_CPU_D_CACHE_LINE_SIZE));
2204
2205 rxq->last_desc = rxq->size - 1;
2206
2207 /* Set Rx descriptors queue starting address */
2208 mvreg_write(pp, MVNETA_RXQ_BASE_ADDR_REG(rxq->id), rxq->descs_phys);
2209 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), rxq->size);
2210
2211 /* Set Offset */
2212 mvneta_rxq_offset_set(pp, rxq, NET_SKB_PAD);
2213
2214 /* Set coalescing pkts and time */
2215 mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
2216 mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
2217
2218 /* Fill RXQ with buffers from RX pool */
2219 mvneta_rxq_buf_size_set(pp, rxq, MVNETA_RX_BUF_SIZE(pp->pkt_size));
2220 mvneta_rxq_bm_disable(pp, rxq);
2221 mvneta_rxq_fill(pp, rxq, rxq->size);
2222
2223 return 0;
2224}
2225
2226/* Cleanup Rx queue */
2227static void mvneta_rxq_deinit(struct mvneta_port *pp,
2228 struct mvneta_rx_queue *rxq)
2229{
2230 mvneta_rxq_drop_pkts(pp, rxq);
2231
2232 if (rxq->descs)
2233 dma_free_coherent(pp->dev->dev.parent,
2234 rxq->size * MVNETA_DESC_ALIGNED_SIZE,
2235 rxq->descs,
2236 rxq->descs_phys);
2237
2238 rxq->descs = NULL;
2239 rxq->last_desc = 0;
2240 rxq->next_desc_to_proc = 0;
2241 rxq->descs_phys = 0;
2242}
2243
2244/* Create and initialize a tx queue */
2245static int mvneta_txq_init(struct mvneta_port *pp,
2246 struct mvneta_tx_queue *txq)
2247{
2248 txq->size = pp->tx_ring_size;
2249
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03002250 /* A queue must always have room for at least one skb.
2251 * Therefore, stop the queue when the free entries reaches
2252 * the maximum number of descriptors per skb.
2253 */
2254 txq->tx_stop_threshold = txq->size - MVNETA_MAX_SKB_DESCS;
2255 txq->tx_wake_threshold = txq->tx_stop_threshold / 2;
2256
2257
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002258 /* Allocate memory for TX descriptors */
2259 txq->descs = dma_alloc_coherent(pp->dev->dev.parent,
2260 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2261 &txq->descs_phys, GFP_KERNEL);
Joe Perchesd0320f72013-03-14 13:07:21 +00002262 if (txq->descs == NULL)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002263 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002264
2265 /* Make sure descriptor address is cache line size aligned */
2266 BUG_ON(txq->descs !=
2267 PTR_ALIGN(txq->descs, MVNETA_CPU_D_CACHE_LINE_SIZE));
2268
2269 txq->last_desc = txq->size - 1;
2270
2271 /* Set maximum bandwidth for enabled TXQs */
2272 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0x03ffffff);
2273 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0x3fffffff);
2274
2275 /* Set Tx descriptors queue starting address */
2276 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), txq->descs_phys);
2277 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), txq->size);
2278
2279 txq->tx_skb = kmalloc(txq->size * sizeof(*txq->tx_skb), GFP_KERNEL);
2280 if (txq->tx_skb == NULL) {
2281 dma_free_coherent(pp->dev->dev.parent,
2282 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2283 txq->descs, txq->descs_phys);
2284 return -ENOMEM;
2285 }
Ezequiel Garcia2adb7192014-05-19 13:59:55 -03002286
2287 /* Allocate DMA buffers for TSO MAC/IP/TCP headers */
2288 txq->tso_hdrs = dma_alloc_coherent(pp->dev->dev.parent,
2289 txq->size * TSO_HEADER_SIZE,
2290 &txq->tso_hdrs_phys, GFP_KERNEL);
2291 if (txq->tso_hdrs == NULL) {
2292 kfree(txq->tx_skb);
2293 dma_free_coherent(pp->dev->dev.parent,
2294 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2295 txq->descs, txq->descs_phys);
2296 return -ENOMEM;
2297 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002298 mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
2299
2300 return 0;
2301}
2302
2303/* Free allocated resources when mvneta_txq_init() fails to allocate memory*/
2304static void mvneta_txq_deinit(struct mvneta_port *pp,
2305 struct mvneta_tx_queue *txq)
2306{
2307 kfree(txq->tx_skb);
2308
Ezequiel Garcia2adb7192014-05-19 13:59:55 -03002309 if (txq->tso_hdrs)
2310 dma_free_coherent(pp->dev->dev.parent,
2311 txq->size * TSO_HEADER_SIZE,
2312 txq->tso_hdrs, txq->tso_hdrs_phys);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002313 if (txq->descs)
2314 dma_free_coherent(pp->dev->dev.parent,
2315 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2316 txq->descs, txq->descs_phys);
2317
2318 txq->descs = NULL;
2319 txq->last_desc = 0;
2320 txq->next_desc_to_proc = 0;
2321 txq->descs_phys = 0;
2322
2323 /* Set minimum bandwidth for disabled TXQs */
2324 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0);
2325 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0);
2326
2327 /* Set Tx descriptors queue starting address and size */
2328 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), 0);
2329 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), 0);
2330}
2331
2332/* Cleanup all Tx queues */
2333static void mvneta_cleanup_txqs(struct mvneta_port *pp)
2334{
2335 int queue;
2336
2337 for (queue = 0; queue < txq_number; queue++)
2338 mvneta_txq_deinit(pp, &pp->txqs[queue]);
2339}
2340
2341/* Cleanup all Rx queues */
2342static void mvneta_cleanup_rxqs(struct mvneta_port *pp)
2343{
Maxime Ripardd8936652015-09-25 18:09:37 +02002344 mvneta_rxq_deinit(pp, &pp->rxqs[rxq_def]);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002345}
2346
2347
2348/* Init all Rx queues */
2349static int mvneta_setup_rxqs(struct mvneta_port *pp)
2350{
Maxime Ripardd8936652015-09-25 18:09:37 +02002351 int err = mvneta_rxq_init(pp, &pp->rxqs[rxq_def]);
2352 if (err) {
2353 netdev_err(pp->dev, "%s: can't create rxq=%d\n",
2354 __func__, rxq_def);
2355 mvneta_cleanup_rxqs(pp);
2356 return err;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002357 }
2358
2359 return 0;
2360}
2361
2362/* Init all tx queues */
2363static int mvneta_setup_txqs(struct mvneta_port *pp)
2364{
2365 int queue;
2366
2367 for (queue = 0; queue < txq_number; queue++) {
2368 int err = mvneta_txq_init(pp, &pp->txqs[queue]);
2369 if (err) {
2370 netdev_err(pp->dev, "%s: can't create txq=%d\n",
2371 __func__, queue);
2372 mvneta_cleanup_txqs(pp);
2373 return err;
2374 }
2375 }
2376
2377 return 0;
2378}
2379
2380static void mvneta_start_dev(struct mvneta_port *pp)
2381{
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002382 unsigned int cpu;
2383
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002384 mvneta_max_rx_size_set(pp, pp->pkt_size);
2385 mvneta_txq_max_tx_size_set(pp, pp->pkt_size);
2386
2387 /* start the Rx/Tx activity */
2388 mvneta_port_enable(pp);
2389
2390 /* Enable polling on the port */
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002391 for_each_present_cpu(cpu) {
2392 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
2393
2394 napi_enable(&port->napi);
2395 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002396
2397 /* Unmask interrupts */
2398 mvreg_write(pp, MVNETA_INTR_NEW_MASK,
Stas Sergeev898b2972015-04-01 20:32:49 +03002399 MVNETA_RX_INTR_MASK(rxq_number) |
2400 MVNETA_TX_INTR_MASK(txq_number) |
2401 MVNETA_MISCINTR_INTR_MASK);
2402 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
2403 MVNETA_CAUSE_PHY_STATUS_CHANGE |
2404 MVNETA_CAUSE_LINK_CHANGE |
2405 MVNETA_CAUSE_PSC_SYNC_CHANGE);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002406
2407 phy_start(pp->phy_dev);
2408 netif_tx_start_all_queues(pp->dev);
2409}
2410
2411static void mvneta_stop_dev(struct mvneta_port *pp)
2412{
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002413 unsigned int cpu;
2414
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002415 phy_stop(pp->phy_dev);
2416
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002417 for_each_present_cpu(cpu) {
2418 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
2419
2420 napi_disable(&port->napi);
2421 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002422
2423 netif_carrier_off(pp->dev);
2424
2425 mvneta_port_down(pp);
2426 netif_tx_stop_all_queues(pp->dev);
2427
2428 /* Stop the port activity */
2429 mvneta_port_disable(pp);
2430
2431 /* Clear all ethernet port interrupts */
2432 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
2433 mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0);
2434
2435 /* Mask all ethernet port interrupts */
2436 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
2437 mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0);
2438 mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0);
2439
2440 mvneta_tx_reset(pp);
2441 mvneta_rx_reset(pp);
2442}
2443
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002444/* Return positive if MTU is valid */
2445static int mvneta_check_mtu_valid(struct net_device *dev, int mtu)
2446{
2447 if (mtu < 68) {
2448 netdev_err(dev, "cannot change mtu to less than 68\n");
2449 return -EINVAL;
2450 }
2451
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002452 /* 9676 == 9700 - 20 and rounding to 8 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002453 if (mtu > 9676) {
2454 netdev_info(dev, "Illegal MTU value %d, round to 9676\n", mtu);
2455 mtu = 9676;
2456 }
2457
2458 if (!IS_ALIGNED(MVNETA_RX_PKT_SIZE(mtu), 8)) {
2459 netdev_info(dev, "Illegal MTU value %d, rounding to %d\n",
2460 mtu, ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8));
2461 mtu = ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8);
2462 }
2463
2464 return mtu;
2465}
2466
2467/* Change the device mtu */
2468static int mvneta_change_mtu(struct net_device *dev, int mtu)
2469{
2470 struct mvneta_port *pp = netdev_priv(dev);
2471 int ret;
2472
2473 mtu = mvneta_check_mtu_valid(dev, mtu);
2474 if (mtu < 0)
2475 return -EINVAL;
2476
2477 dev->mtu = mtu;
2478
Simon Guinotb65657f2015-06-30 16:20:22 +02002479 if (!netif_running(dev)) {
2480 netdev_update_features(dev);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002481 return 0;
Simon Guinotb65657f2015-06-30 16:20:22 +02002482 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002483
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002484 /* The interface is running, so we have to force a
Ezequiel Garciaa92dbd92014-05-22 20:06:58 -03002485 * reallocation of the queues
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002486 */
2487 mvneta_stop_dev(pp);
2488
2489 mvneta_cleanup_txqs(pp);
2490 mvneta_cleanup_rxqs(pp);
2491
Ezequiel Garciaa92dbd92014-05-22 20:06:58 -03002492 pp->pkt_size = MVNETA_RX_PKT_SIZE(dev->mtu);
willy tarreau8ec2cd42014-01-16 08:20:16 +01002493 pp->frag_size = SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(pp->pkt_size)) +
2494 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002495
2496 ret = mvneta_setup_rxqs(pp);
2497 if (ret) {
Ezequiel Garciaa92dbd92014-05-22 20:06:58 -03002498 netdev_err(dev, "unable to setup rxqs after MTU change\n");
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002499 return ret;
2500 }
2501
Ezequiel Garciaa92dbd92014-05-22 20:06:58 -03002502 ret = mvneta_setup_txqs(pp);
2503 if (ret) {
2504 netdev_err(dev, "unable to setup txqs after MTU change\n");
2505 return ret;
2506 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002507
2508 mvneta_start_dev(pp);
2509 mvneta_port_up(pp);
2510
Simon Guinotb65657f2015-06-30 16:20:22 +02002511 netdev_update_features(dev);
2512
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002513 return 0;
2514}
2515
Simon Guinotb65657f2015-06-30 16:20:22 +02002516static netdev_features_t mvneta_fix_features(struct net_device *dev,
2517 netdev_features_t features)
2518{
2519 struct mvneta_port *pp = netdev_priv(dev);
2520
2521 if (pp->tx_csum_limit && dev->mtu > pp->tx_csum_limit) {
2522 features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
2523 netdev_info(dev,
2524 "Disable IP checksum for MTU greater than %dB\n",
2525 pp->tx_csum_limit);
2526 }
2527
2528 return features;
2529}
2530
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00002531/* Get mac address */
2532static void mvneta_get_mac_addr(struct mvneta_port *pp, unsigned char *addr)
2533{
2534 u32 mac_addr_l, mac_addr_h;
2535
2536 mac_addr_l = mvreg_read(pp, MVNETA_MAC_ADDR_LOW);
2537 mac_addr_h = mvreg_read(pp, MVNETA_MAC_ADDR_HIGH);
2538 addr[0] = (mac_addr_h >> 24) & 0xFF;
2539 addr[1] = (mac_addr_h >> 16) & 0xFF;
2540 addr[2] = (mac_addr_h >> 8) & 0xFF;
2541 addr[3] = mac_addr_h & 0xFF;
2542 addr[4] = (mac_addr_l >> 8) & 0xFF;
2543 addr[5] = mac_addr_l & 0xFF;
2544}
2545
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002546/* Handle setting mac address */
2547static int mvneta_set_mac_addr(struct net_device *dev, void *addr)
2548{
2549 struct mvneta_port *pp = netdev_priv(dev);
Ezequiel Garciae68de362014-05-22 20:07:00 -03002550 struct sockaddr *sockaddr = addr;
2551 int ret;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002552
Ezequiel Garciae68de362014-05-22 20:07:00 -03002553 ret = eth_prepare_mac_addr_change(dev, addr);
2554 if (ret < 0)
2555 return ret;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002556 /* Remove previous address table entry */
2557 mvneta_mac_addr_set(pp, dev->dev_addr, -1);
2558
2559 /* Set new addr in hw */
Ezequiel Garciae68de362014-05-22 20:07:00 -03002560 mvneta_mac_addr_set(pp, sockaddr->sa_data, rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002561
Ezequiel Garciae68de362014-05-22 20:07:00 -03002562 eth_commit_mac_addr_change(dev, addr);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002563 return 0;
2564}
2565
2566static void mvneta_adjust_link(struct net_device *ndev)
2567{
2568 struct mvneta_port *pp = netdev_priv(ndev);
2569 struct phy_device *phydev = pp->phy_dev;
2570 int status_change = 0;
2571
2572 if (phydev->link) {
2573 if ((pp->speed != phydev->speed) ||
2574 (pp->duplex != phydev->duplex)) {
2575 u32 val;
2576
2577 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
2578 val &= ~(MVNETA_GMAC_CONFIG_MII_SPEED |
2579 MVNETA_GMAC_CONFIG_GMII_SPEED |
Stas Sergeev898b2972015-04-01 20:32:49 +03002580 MVNETA_GMAC_CONFIG_FULL_DUPLEX);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002581
2582 if (phydev->duplex)
2583 val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
2584
2585 if (phydev->speed == SPEED_1000)
2586 val |= MVNETA_GMAC_CONFIG_GMII_SPEED;
Thomas Petazzoni4d12bc62014-07-08 10:49:43 +02002587 else if (phydev->speed == SPEED_100)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002588 val |= MVNETA_GMAC_CONFIG_MII_SPEED;
2589
2590 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
2591
2592 pp->duplex = phydev->duplex;
2593 pp->speed = phydev->speed;
2594 }
2595 }
2596
2597 if (phydev->link != pp->link) {
2598 if (!phydev->link) {
2599 pp->duplex = -1;
2600 pp->speed = 0;
2601 }
2602
2603 pp->link = phydev->link;
2604 status_change = 1;
2605 }
2606
2607 if (status_change) {
2608 if (phydev->link) {
Stas Sergeev898b2972015-04-01 20:32:49 +03002609 if (!pp->use_inband_status) {
2610 u32 val = mvreg_read(pp,
2611 MVNETA_GMAC_AUTONEG_CONFIG);
2612 val &= ~MVNETA_GMAC_FORCE_LINK_DOWN;
2613 val |= MVNETA_GMAC_FORCE_LINK_PASS;
2614 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
2615 val);
2616 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002617 mvneta_port_up(pp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002618 } else {
Stas Sergeev898b2972015-04-01 20:32:49 +03002619 if (!pp->use_inband_status) {
2620 u32 val = mvreg_read(pp,
2621 MVNETA_GMAC_AUTONEG_CONFIG);
2622 val &= ~MVNETA_GMAC_FORCE_LINK_PASS;
2623 val |= MVNETA_GMAC_FORCE_LINK_DOWN;
2624 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
2625 val);
2626 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002627 mvneta_port_down(pp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002628 }
Ezequiel Garcia0089b742014-10-31 12:57:20 -03002629 phy_print_status(phydev);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002630 }
2631}
2632
2633static int mvneta_mdio_probe(struct mvneta_port *pp)
2634{
2635 struct phy_device *phy_dev;
2636
2637 phy_dev = of_phy_connect(pp->dev, pp->phy_node, mvneta_adjust_link, 0,
2638 pp->phy_interface);
2639 if (!phy_dev) {
2640 netdev_err(pp->dev, "could not find the PHY\n");
2641 return -ENODEV;
2642 }
2643
2644 phy_dev->supported &= PHY_GBIT_FEATURES;
2645 phy_dev->advertising = phy_dev->supported;
2646
2647 pp->phy_dev = phy_dev;
2648 pp->link = 0;
2649 pp->duplex = 0;
2650 pp->speed = 0;
2651
2652 return 0;
2653}
2654
2655static void mvneta_mdio_remove(struct mvneta_port *pp)
2656{
2657 phy_disconnect(pp->phy_dev);
2658 pp->phy_dev = NULL;
2659}
2660
2661static int mvneta_open(struct net_device *dev)
2662{
2663 struct mvneta_port *pp = netdev_priv(dev);
2664 int ret;
2665
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002666 pp->pkt_size = MVNETA_RX_PKT_SIZE(pp->dev->mtu);
willy tarreau8ec2cd42014-01-16 08:20:16 +01002667 pp->frag_size = SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(pp->pkt_size)) +
2668 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002669
2670 ret = mvneta_setup_rxqs(pp);
2671 if (ret)
2672 return ret;
2673
2674 ret = mvneta_setup_txqs(pp);
2675 if (ret)
2676 goto err_cleanup_rxqs;
2677
2678 /* Connect to port interrupt line */
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002679 ret = request_percpu_irq(pp->dev->irq, mvneta_isr,
2680 MVNETA_DRIVER_NAME, pp->ports);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002681 if (ret) {
2682 netdev_err(pp->dev, "cannot request irq %d\n", pp->dev->irq);
2683 goto err_cleanup_txqs;
2684 }
2685
2686 /* In default link is down */
2687 netif_carrier_off(pp->dev);
2688
2689 ret = mvneta_mdio_probe(pp);
2690 if (ret < 0) {
2691 netdev_err(dev, "cannot probe MDIO bus\n");
2692 goto err_free_irq;
2693 }
2694
2695 mvneta_start_dev(pp);
2696
2697 return 0;
2698
2699err_free_irq:
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002700 free_percpu_irq(pp->dev->irq, pp->ports);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002701err_cleanup_txqs:
2702 mvneta_cleanup_txqs(pp);
2703err_cleanup_rxqs:
2704 mvneta_cleanup_rxqs(pp);
2705 return ret;
2706}
2707
2708/* Stop the port, free port interrupt line */
2709static int mvneta_stop(struct net_device *dev)
2710{
2711 struct mvneta_port *pp = netdev_priv(dev);
2712
2713 mvneta_stop_dev(pp);
2714 mvneta_mdio_remove(pp);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002715 free_percpu_irq(dev->irq, pp->ports);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002716 mvneta_cleanup_rxqs(pp);
2717 mvneta_cleanup_txqs(pp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002718
2719 return 0;
2720}
2721
Thomas Petazzoni15f59452013-09-04 16:26:52 +02002722static int mvneta_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2723{
2724 struct mvneta_port *pp = netdev_priv(dev);
Thomas Petazzoni15f59452013-09-04 16:26:52 +02002725
2726 if (!pp->phy_dev)
2727 return -ENOTSUPP;
2728
Stas Sergeevecf7b362015-04-01 19:23:29 +03002729 return phy_mii_ioctl(pp->phy_dev, ifr, cmd);
Thomas Petazzoni15f59452013-09-04 16:26:52 +02002730}
2731
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002732/* Ethtool methods */
2733
2734/* Get settings (phy address, speed) for ethtools */
2735int mvneta_ethtool_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2736{
2737 struct mvneta_port *pp = netdev_priv(dev);
2738
2739 if (!pp->phy_dev)
2740 return -ENODEV;
2741
2742 return phy_ethtool_gset(pp->phy_dev, cmd);
2743}
2744
2745/* Set settings (phy address, speed) for ethtools */
2746int mvneta_ethtool_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2747{
2748 struct mvneta_port *pp = netdev_priv(dev);
2749
2750 if (!pp->phy_dev)
2751 return -ENODEV;
2752
2753 return phy_ethtool_sset(pp->phy_dev, cmd);
2754}
2755
2756/* Set interrupt coalescing for ethtools */
2757static int mvneta_ethtool_set_coalesce(struct net_device *dev,
2758 struct ethtool_coalesce *c)
2759{
2760 struct mvneta_port *pp = netdev_priv(dev);
2761 int queue;
2762
2763 for (queue = 0; queue < rxq_number; queue++) {
2764 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
2765 rxq->time_coal = c->rx_coalesce_usecs;
2766 rxq->pkts_coal = c->rx_max_coalesced_frames;
2767 mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
2768 mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
2769 }
2770
2771 for (queue = 0; queue < txq_number; queue++) {
2772 struct mvneta_tx_queue *txq = &pp->txqs[queue];
2773 txq->done_pkts_coal = c->tx_max_coalesced_frames;
2774 mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
2775 }
2776
2777 return 0;
2778}
2779
2780/* get coalescing for ethtools */
2781static int mvneta_ethtool_get_coalesce(struct net_device *dev,
2782 struct ethtool_coalesce *c)
2783{
2784 struct mvneta_port *pp = netdev_priv(dev);
2785
2786 c->rx_coalesce_usecs = pp->rxqs[0].time_coal;
2787 c->rx_max_coalesced_frames = pp->rxqs[0].pkts_coal;
2788
2789 c->tx_max_coalesced_frames = pp->txqs[0].done_pkts_coal;
2790 return 0;
2791}
2792
2793
2794static void mvneta_ethtool_get_drvinfo(struct net_device *dev,
2795 struct ethtool_drvinfo *drvinfo)
2796{
2797 strlcpy(drvinfo->driver, MVNETA_DRIVER_NAME,
2798 sizeof(drvinfo->driver));
2799 strlcpy(drvinfo->version, MVNETA_DRIVER_VERSION,
2800 sizeof(drvinfo->version));
2801 strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
2802 sizeof(drvinfo->bus_info));
2803}
2804
2805
2806static void mvneta_ethtool_get_ringparam(struct net_device *netdev,
2807 struct ethtool_ringparam *ring)
2808{
2809 struct mvneta_port *pp = netdev_priv(netdev);
2810
2811 ring->rx_max_pending = MVNETA_MAX_RXD;
2812 ring->tx_max_pending = MVNETA_MAX_TXD;
2813 ring->rx_pending = pp->rx_ring_size;
2814 ring->tx_pending = pp->tx_ring_size;
2815}
2816
2817static int mvneta_ethtool_set_ringparam(struct net_device *dev,
2818 struct ethtool_ringparam *ring)
2819{
2820 struct mvneta_port *pp = netdev_priv(dev);
2821
2822 if ((ring->rx_pending == 0) || (ring->tx_pending == 0))
2823 return -EINVAL;
2824 pp->rx_ring_size = ring->rx_pending < MVNETA_MAX_RXD ?
2825 ring->rx_pending : MVNETA_MAX_RXD;
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03002826
2827 pp->tx_ring_size = clamp_t(u16, ring->tx_pending,
2828 MVNETA_MAX_SKB_DESCS * 2, MVNETA_MAX_TXD);
2829 if (pp->tx_ring_size != ring->tx_pending)
2830 netdev_warn(dev, "TX queue size set to %u (requested %u)\n",
2831 pp->tx_ring_size, ring->tx_pending);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002832
2833 if (netif_running(dev)) {
2834 mvneta_stop(dev);
2835 if (mvneta_open(dev)) {
2836 netdev_err(dev,
2837 "error on opening device after ring param change\n");
2838 return -ENOMEM;
2839 }
2840 }
2841
2842 return 0;
2843}
2844
2845static const struct net_device_ops mvneta_netdev_ops = {
2846 .ndo_open = mvneta_open,
2847 .ndo_stop = mvneta_stop,
2848 .ndo_start_xmit = mvneta_tx,
2849 .ndo_set_rx_mode = mvneta_set_rx_mode,
2850 .ndo_set_mac_address = mvneta_set_mac_addr,
2851 .ndo_change_mtu = mvneta_change_mtu,
Simon Guinotb65657f2015-06-30 16:20:22 +02002852 .ndo_fix_features = mvneta_fix_features,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002853 .ndo_get_stats64 = mvneta_get_stats64,
Thomas Petazzoni15f59452013-09-04 16:26:52 +02002854 .ndo_do_ioctl = mvneta_ioctl,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002855};
2856
2857const struct ethtool_ops mvneta_eth_tool_ops = {
2858 .get_link = ethtool_op_get_link,
2859 .get_settings = mvneta_ethtool_get_settings,
2860 .set_settings = mvneta_ethtool_set_settings,
2861 .set_coalesce = mvneta_ethtool_set_coalesce,
2862 .get_coalesce = mvneta_ethtool_get_coalesce,
2863 .get_drvinfo = mvneta_ethtool_get_drvinfo,
2864 .get_ringparam = mvneta_ethtool_get_ringparam,
2865 .set_ringparam = mvneta_ethtool_set_ringparam,
2866};
2867
2868/* Initialize hw */
Ezequiel Garcia96728502014-05-22 20:06:59 -03002869static int mvneta_init(struct device *dev, struct mvneta_port *pp)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002870{
2871 int queue;
2872
2873 /* Disable port */
2874 mvneta_port_disable(pp);
2875
2876 /* Set port default values */
2877 mvneta_defaults_set(pp);
2878
Ezequiel Garcia96728502014-05-22 20:06:59 -03002879 pp->txqs = devm_kcalloc(dev, txq_number, sizeof(struct mvneta_tx_queue),
2880 GFP_KERNEL);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002881 if (!pp->txqs)
2882 return -ENOMEM;
2883
2884 /* Initialize TX descriptor rings */
2885 for (queue = 0; queue < txq_number; queue++) {
2886 struct mvneta_tx_queue *txq = &pp->txqs[queue];
2887 txq->id = queue;
2888 txq->size = pp->tx_ring_size;
2889 txq->done_pkts_coal = MVNETA_TXDONE_COAL_PKTS;
2890 }
2891
Ezequiel Garcia96728502014-05-22 20:06:59 -03002892 pp->rxqs = devm_kcalloc(dev, rxq_number, sizeof(struct mvneta_rx_queue),
2893 GFP_KERNEL);
2894 if (!pp->rxqs)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002895 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002896
2897 /* Create Rx descriptor rings */
2898 for (queue = 0; queue < rxq_number; queue++) {
2899 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
2900 rxq->id = queue;
2901 rxq->size = pp->rx_ring_size;
2902 rxq->pkts_coal = MVNETA_RX_COAL_PKTS;
2903 rxq->time_coal = MVNETA_RX_COAL_USEC;
2904 }
2905
2906 return 0;
2907}
2908
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002909/* platform glue : initialize decoding windows */
Greg KH03ce7582012-12-21 13:42:15 +00002910static void mvneta_conf_mbus_windows(struct mvneta_port *pp,
2911 const struct mbus_dram_target_info *dram)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002912{
2913 u32 win_enable;
2914 u32 win_protect;
2915 int i;
2916
2917 for (i = 0; i < 6; i++) {
2918 mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
2919 mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
2920
2921 if (i < 4)
2922 mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
2923 }
2924
2925 win_enable = 0x3f;
2926 win_protect = 0;
2927
2928 for (i = 0; i < dram->num_cs; i++) {
2929 const struct mbus_dram_window *cs = dram->cs + i;
2930 mvreg_write(pp, MVNETA_WIN_BASE(i), (cs->base & 0xffff0000) |
2931 (cs->mbus_attr << 8) | dram->mbus_dram_target_id);
2932
2933 mvreg_write(pp, MVNETA_WIN_SIZE(i),
2934 (cs->size - 1) & 0xffff0000);
2935
2936 win_enable &= ~(1 << i);
2937 win_protect |= 3 << (2 * i);
2938 }
2939
2940 mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
2941}
2942
2943/* Power up the port */
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02002944static int mvneta_port_power_up(struct mvneta_port *pp, int phy_mode)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002945{
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02002946 u32 ctrl;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002947
2948 /* MAC Cause register should be cleared */
2949 mvreg_write(pp, MVNETA_UNIT_INTR_CAUSE, 0);
2950
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02002951 ctrl = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002952
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02002953 /* Even though it might look weird, when we're configured in
2954 * SGMII or QSGMII mode, the RGMII bit needs to be set.
2955 */
2956 switch(phy_mode) {
2957 case PHY_INTERFACE_MODE_QSGMII:
2958 mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_QSGMII_SERDES_PROTO);
2959 ctrl |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII;
2960 break;
2961 case PHY_INTERFACE_MODE_SGMII:
2962 mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_SGMII_SERDES_PROTO);
2963 ctrl |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII;
2964 break;
2965 case PHY_INTERFACE_MODE_RGMII:
2966 case PHY_INTERFACE_MODE_RGMII_ID:
2967 ctrl |= MVNETA_GMAC2_PORT_RGMII;
2968 break;
2969 default:
2970 return -EINVAL;
2971 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002972
Stas Sergeev898b2972015-04-01 20:32:49 +03002973 if (pp->use_inband_status)
2974 ctrl |= MVNETA_GMAC2_INBAND_AN_ENABLE;
2975
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002976 /* Cancel Port Reset */
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02002977 ctrl &= ~MVNETA_GMAC2_PORT_RESET;
2978 mvreg_write(pp, MVNETA_GMAC_CTRL_2, ctrl);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002979
2980 while ((mvreg_read(pp, MVNETA_GMAC_CTRL_2) &
2981 MVNETA_GMAC2_PORT_RESET) != 0)
2982 continue;
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02002983
2984 return 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002985}
2986
2987/* Device initialization routine */
Greg KH03ce7582012-12-21 13:42:15 +00002988static int mvneta_probe(struct platform_device *pdev)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002989{
2990 const struct mbus_dram_target_info *dram_target_info;
Thomas Petazzonic3f0dd32014-03-27 11:39:29 +01002991 struct resource *res;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002992 struct device_node *dn = pdev->dev.of_node;
2993 struct device_node *phy_node;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002994 struct mvneta_port *pp;
2995 struct net_device *dev;
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00002996 const char *dt_mac_addr;
2997 char hw_mac_addr[ETH_ALEN];
2998 const char *mac_from;
Stas Sergeevf8af8e62015-07-20 17:49:58 -07002999 const char *managed;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003000 int phy_mode;
3001 int err;
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003002 int cpu;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003003
Willy Tarreauee40a112013-04-11 23:00:37 +02003004 dev = alloc_etherdev_mqs(sizeof(struct mvneta_port), txq_number, rxq_number);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003005 if (!dev)
3006 return -ENOMEM;
3007
3008 dev->irq = irq_of_parse_and_map(dn, 0);
3009 if (dev->irq == 0) {
3010 err = -EINVAL;
3011 goto err_free_netdev;
3012 }
3013
3014 phy_node = of_parse_phandle(dn, "phy", 0);
3015 if (!phy_node) {
Thomas Petazzoni83895be2014-05-16 16:14:06 +02003016 if (!of_phy_is_fixed_link(dn)) {
3017 dev_err(&pdev->dev, "no PHY specified\n");
3018 err = -ENODEV;
3019 goto err_free_irq;
3020 }
3021
3022 err = of_phy_register_fixed_link(dn);
3023 if (err < 0) {
3024 dev_err(&pdev->dev, "cannot register fixed PHY\n");
3025 goto err_free_irq;
3026 }
3027
3028 /* In the case of a fixed PHY, the DT node associated
3029 * to the PHY is the Ethernet MAC DT node.
3030 */
Uwe Kleine-Königc891c242014-08-07 21:58:46 +02003031 phy_node = of_node_get(dn);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003032 }
3033
3034 phy_mode = of_get_phy_mode(dn);
3035 if (phy_mode < 0) {
3036 dev_err(&pdev->dev, "incorrect phy-mode\n");
3037 err = -EINVAL;
Uwe Kleine-Königc891c242014-08-07 21:58:46 +02003038 goto err_put_phy_node;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003039 }
3040
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003041 dev->tx_queue_len = MVNETA_MAX_TXD;
3042 dev->watchdog_timeo = 5 * HZ;
3043 dev->netdev_ops = &mvneta_netdev_ops;
3044
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00003045 dev->ethtool_ops = &mvneta_eth_tool_ops;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003046
3047 pp = netdev_priv(dev);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003048 pp->phy_node = phy_node;
3049 pp->phy_interface = phy_mode;
Stas Sergeevf8af8e62015-07-20 17:49:58 -07003050
3051 err = of_property_read_string(dn, "managed", &managed);
3052 pp->use_inband_status = (err == 0 &&
3053 strcmp(managed, "in-band-status") == 0);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003054
Thomas Petazzoni189dd622012-11-19 14:15:25 +01003055 pp->clk = devm_clk_get(&pdev->dev, NULL);
3056 if (IS_ERR(pp->clk)) {
3057 err = PTR_ERR(pp->clk);
Uwe Kleine-Königc891c242014-08-07 21:58:46 +02003058 goto err_put_phy_node;
Thomas Petazzoni189dd622012-11-19 14:15:25 +01003059 }
3060
3061 clk_prepare_enable(pp->clk);
3062
Thomas Petazzonic3f0dd32014-03-27 11:39:29 +01003063 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3064 pp->base = devm_ioremap_resource(&pdev->dev, res);
3065 if (IS_ERR(pp->base)) {
3066 err = PTR_ERR(pp->base);
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +02003067 goto err_clk;
3068 }
3069
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003070 /* Alloc per-cpu port structure */
3071 pp->ports = alloc_percpu(struct mvneta_pcpu_port);
3072 if (!pp->ports) {
3073 err = -ENOMEM;
3074 goto err_clk;
3075 }
3076
willy tarreau74c41b02014-01-16 08:20:08 +01003077 /* Alloc per-cpu stats */
WANG Cong1c213bd2014-02-13 11:46:28 -08003078 pp->stats = netdev_alloc_pcpu_stats(struct mvneta_pcpu_stats);
willy tarreau74c41b02014-01-16 08:20:08 +01003079 if (!pp->stats) {
3080 err = -ENOMEM;
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003081 goto err_free_ports;
willy tarreau74c41b02014-01-16 08:20:08 +01003082 }
3083
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00003084 dt_mac_addr = of_get_mac_address(dn);
Luka Perkov6c7a9a32013-10-30 00:10:01 +01003085 if (dt_mac_addr) {
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00003086 mac_from = "device tree";
3087 memcpy(dev->dev_addr, dt_mac_addr, ETH_ALEN);
3088 } else {
3089 mvneta_get_mac_addr(pp, hw_mac_addr);
3090 if (is_valid_ether_addr(hw_mac_addr)) {
3091 mac_from = "hardware";
3092 memcpy(dev->dev_addr, hw_mac_addr, ETH_ALEN);
3093 } else {
3094 mac_from = "random";
3095 eth_hw_addr_random(dev);
3096 }
3097 }
3098
Simon Guinotb65657f2015-06-30 16:20:22 +02003099 if (of_device_is_compatible(dn, "marvell,armada-370-neta"))
3100 pp->tx_csum_limit = 1600;
3101
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003102 pp->tx_ring_size = MVNETA_MAX_TXD;
3103 pp->rx_ring_size = MVNETA_MAX_RXD;
3104
3105 pp->dev = dev;
3106 SET_NETDEV_DEV(dev, &pdev->dev);
3107
Ezequiel Garcia96728502014-05-22 20:06:59 -03003108 err = mvneta_init(&pdev->dev, pp);
3109 if (err < 0)
willy tarreau74c41b02014-01-16 08:20:08 +01003110 goto err_free_stats;
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02003111
3112 err = mvneta_port_power_up(pp, phy_mode);
3113 if (err < 0) {
3114 dev_err(&pdev->dev, "can't power up port\n");
Ezequiel Garcia96728502014-05-22 20:06:59 -03003115 goto err_free_stats;
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02003116 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003117
3118 dram_target_info = mv_mbus_dram_info();
3119 if (dram_target_info)
3120 mvneta_conf_mbus_windows(pp, dram_target_info);
3121
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003122 for_each_present_cpu(cpu) {
3123 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
3124
3125 netif_napi_add(dev, &port->napi, mvneta_poll, NAPI_POLL_WEIGHT);
3126 port->pp = pp;
3127 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003128
Ezequiel Garcia2adb7192014-05-19 13:59:55 -03003129 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO;
Ezequiel Garcia01ef26c2014-05-19 13:59:53 -03003130 dev->hw_features |= dev->features;
3131 dev->vlan_features |= dev->features;
willy tarreaub50b72d2013-04-06 08:47:01 +00003132 dev->priv_flags |= IFF_UNICAST_FLT;
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03003133 dev->gso_max_segs = MVNETA_MAX_TSO_SEGS;
willy tarreaub50b72d2013-04-06 08:47:01 +00003134
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003135 err = register_netdev(dev);
3136 if (err < 0) {
3137 dev_err(&pdev->dev, "failed to register\n");
Ezequiel Garcia96728502014-05-22 20:06:59 -03003138 goto err_free_stats;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003139 }
3140
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00003141 netdev_info(dev, "Using %s mac address %pM\n", mac_from,
3142 dev->dev_addr);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003143
3144 platform_set_drvdata(pdev, pp->dev);
3145
Stas Sergeev898b2972015-04-01 20:32:49 +03003146 if (pp->use_inband_status) {
3147 struct phy_device *phy = of_phy_find_device(dn);
3148
3149 mvneta_fixed_link_update(pp, phy);
Russell King04d53b22015-09-24 20:36:18 +01003150
3151 put_device(&phy->dev);
Stas Sergeev898b2972015-04-01 20:32:49 +03003152 }
3153
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003154 return 0;
3155
willy tarreau74c41b02014-01-16 08:20:08 +01003156err_free_stats:
3157 free_percpu(pp->stats);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003158err_free_ports:
3159 free_percpu(pp->ports);
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +02003160err_clk:
3161 clk_disable_unprepare(pp->clk);
Uwe Kleine-Königc891c242014-08-07 21:58:46 +02003162err_put_phy_node:
3163 of_node_put(phy_node);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003164err_free_irq:
3165 irq_dispose_mapping(dev->irq);
3166err_free_netdev:
3167 free_netdev(dev);
3168 return err;
3169}
3170
3171/* Device removal routine */
Greg KH03ce7582012-12-21 13:42:15 +00003172static int mvneta_remove(struct platform_device *pdev)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003173{
3174 struct net_device *dev = platform_get_drvdata(pdev);
3175 struct mvneta_port *pp = netdev_priv(dev);
3176
3177 unregister_netdev(dev);
Thomas Petazzoni189dd622012-11-19 14:15:25 +01003178 clk_disable_unprepare(pp->clk);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003179 free_percpu(pp->ports);
willy tarreau74c41b02014-01-16 08:20:08 +01003180 free_percpu(pp->stats);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003181 irq_dispose_mapping(dev->irq);
Uwe Kleine-Königc891c242014-08-07 21:58:46 +02003182 of_node_put(pp->phy_node);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003183 free_netdev(dev);
3184
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003185 return 0;
3186}
3187
3188static const struct of_device_id mvneta_match[] = {
3189 { .compatible = "marvell,armada-370-neta" },
Simon Guinotf522a972015-06-30 16:20:20 +02003190 { .compatible = "marvell,armada-xp-neta" },
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003191 { }
3192};
3193MODULE_DEVICE_TABLE(of, mvneta_match);
3194
3195static struct platform_driver mvneta_driver = {
3196 .probe = mvneta_probe,
Greg KH03ce7582012-12-21 13:42:15 +00003197 .remove = mvneta_remove,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003198 .driver = {
3199 .name = MVNETA_DRIVER_NAME,
3200 .of_match_table = mvneta_match,
3201 },
3202};
3203
3204module_platform_driver(mvneta_driver);
3205
3206MODULE_DESCRIPTION("Marvell NETA Ethernet Driver - www.marvell.com");
3207MODULE_AUTHOR("Rami Rosen <rosenr@marvell.com>, Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
3208MODULE_LICENSE("GPL");
3209
3210module_param(rxq_number, int, S_IRUGO);
3211module_param(txq_number, int, S_IRUGO);
3212
3213module_param(rxq_def, int, S_IRUGO);
willy tarreauf19fadf2014-01-16 08:20:17 +01003214module_param(rx_copybreak, int, S_IRUGO | S_IWUSR);