blob: b12a745a0e4c85b28dbe26e568d3fac492ed12c9 [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
Jisheng Zhang0e03f562016-01-20 19:27:22 +080014#include <linux/clk.h>
15#include <linux/cpu.h>
Thomas Petazzonic5aff182012-08-17 14:04:28 +030016#include <linux/etherdevice.h>
Jisheng Zhang0e03f562016-01-20 19:27:22 +080017#include <linux/if_vlan.h>
Thomas Petazzonic5aff182012-08-17 14:04:28 +030018#include <linux/inetdevice.h>
Jisheng Zhang0e03f562016-01-20 19:27:22 +080019#include <linux/interrupt.h>
20#include <linux/io.h>
21#include <linux/kernel.h>
Thomas Petazzonic5aff182012-08-17 14:04:28 +030022#include <linux/mbus.h>
23#include <linux/module.h>
Jisheng Zhang0e03f562016-01-20 19:27:22 +080024#include <linux/netdevice.h>
Thomas Petazzonic5aff182012-08-17 14:04:28 +030025#include <linux/of.h>
Jisheng Zhang0e03f562016-01-20 19:27:22 +080026#include <linux/of_address.h>
Thomas Petazzonic5aff182012-08-17 14:04:28 +030027#include <linux/of_irq.h>
28#include <linux/of_mdio.h>
29#include <linux/of_net.h>
Thomas Petazzonic5aff182012-08-17 14:04:28 +030030#include <linux/phy.h>
Jisheng Zhang0e03f562016-01-20 19:27:22 +080031#include <linux/platform_device.h>
32#include <linux/skbuff.h>
33#include <net/ip.h>
34#include <net/ipv6.h>
35#include <net/tso.h>
Thomas Petazzonic5aff182012-08-17 14:04:28 +030036
37/* Registers */
38#define MVNETA_RXQ_CONFIG_REG(q) (0x1400 + ((q) << 2))
Marcin Wojtase5bdf682015-11-30 13:27:42 +010039#define MVNETA_RXQ_HW_BUF_ALLOC BIT(0)
Thomas Petazzonic5aff182012-08-17 14:04:28 +030040#define MVNETA_RXQ_PKT_OFFSET_ALL_MASK (0xf << 8)
41#define MVNETA_RXQ_PKT_OFFSET_MASK(offs) ((offs) << 8)
42#define MVNETA_RXQ_THRESHOLD_REG(q) (0x14c0 + ((q) << 2))
43#define MVNETA_RXQ_NON_OCCUPIED(v) ((v) << 16)
44#define MVNETA_RXQ_BASE_ADDR_REG(q) (0x1480 + ((q) << 2))
45#define MVNETA_RXQ_SIZE_REG(q) (0x14a0 + ((q) << 2))
46#define MVNETA_RXQ_BUF_SIZE_SHIFT 19
47#define MVNETA_RXQ_BUF_SIZE_MASK (0x1fff << 19)
48#define MVNETA_RXQ_STATUS_REG(q) (0x14e0 + ((q) << 2))
49#define MVNETA_RXQ_OCCUPIED_ALL_MASK 0x3fff
50#define MVNETA_RXQ_STATUS_UPDATE_REG(q) (0x1500 + ((q) << 2))
51#define MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT 16
52#define MVNETA_RXQ_ADD_NON_OCCUPIED_MAX 255
53#define MVNETA_PORT_RX_RESET 0x1cc0
54#define MVNETA_PORT_RX_DMA_RESET BIT(0)
55#define MVNETA_PHY_ADDR 0x2000
56#define MVNETA_PHY_ADDR_MASK 0x1f
57#define MVNETA_MBUS_RETRY 0x2010
58#define MVNETA_UNIT_INTR_CAUSE 0x2080
59#define MVNETA_UNIT_CONTROL 0x20B0
60#define MVNETA_PHY_POLLING_ENABLE BIT(1)
61#define MVNETA_WIN_BASE(w) (0x2200 + ((w) << 3))
62#define MVNETA_WIN_SIZE(w) (0x2204 + ((w) << 3))
63#define MVNETA_WIN_REMAP(w) (0x2280 + ((w) << 2))
64#define MVNETA_BASE_ADDR_ENABLE 0x2290
Marcin Wojtasdb6ba9a2015-11-30 13:27:41 +010065#define MVNETA_ACCESS_PROTECT_ENABLE 0x2294
Thomas Petazzonic5aff182012-08-17 14:04:28 +030066#define MVNETA_PORT_CONFIG 0x2400
67#define MVNETA_UNI_PROMISC_MODE BIT(0)
68#define MVNETA_DEF_RXQ(q) ((q) << 1)
69#define MVNETA_DEF_RXQ_ARP(q) ((q) << 4)
70#define MVNETA_TX_UNSET_ERR_SUM BIT(12)
71#define MVNETA_DEF_RXQ_TCP(q) ((q) << 16)
72#define MVNETA_DEF_RXQ_UDP(q) ((q) << 19)
73#define MVNETA_DEF_RXQ_BPDU(q) ((q) << 22)
74#define MVNETA_RX_CSUM_WITH_PSEUDO_HDR BIT(25)
75#define MVNETA_PORT_CONFIG_DEFL_VALUE(q) (MVNETA_DEF_RXQ(q) | \
76 MVNETA_DEF_RXQ_ARP(q) | \
77 MVNETA_DEF_RXQ_TCP(q) | \
78 MVNETA_DEF_RXQ_UDP(q) | \
79 MVNETA_DEF_RXQ_BPDU(q) | \
80 MVNETA_TX_UNSET_ERR_SUM | \
81 MVNETA_RX_CSUM_WITH_PSEUDO_HDR)
82#define MVNETA_PORT_CONFIG_EXTEND 0x2404
83#define MVNETA_MAC_ADDR_LOW 0x2414
84#define MVNETA_MAC_ADDR_HIGH 0x2418
85#define MVNETA_SDMA_CONFIG 0x241c
86#define MVNETA_SDMA_BRST_SIZE_16 4
Thomas Petazzonic5aff182012-08-17 14:04:28 +030087#define MVNETA_RX_BRST_SZ_MASK(burst) ((burst) << 1)
88#define MVNETA_RX_NO_DATA_SWAP BIT(4)
89#define MVNETA_TX_NO_DATA_SWAP BIT(5)
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +020090#define MVNETA_DESC_SWAP BIT(6)
Thomas Petazzonic5aff182012-08-17 14:04:28 +030091#define MVNETA_TX_BRST_SZ_MASK(burst) ((burst) << 22)
92#define MVNETA_PORT_STATUS 0x2444
93#define MVNETA_TX_IN_PRGRS BIT(1)
94#define MVNETA_TX_FIFO_EMPTY BIT(8)
95#define MVNETA_RX_MIN_FRAME_SIZE 0x247c
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +020096#define MVNETA_SERDES_CFG 0x24A0
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +020097#define MVNETA_SGMII_SERDES_PROTO 0x0cc7
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +020098#define MVNETA_QSGMII_SERDES_PROTO 0x0667
Thomas Petazzonic5aff182012-08-17 14:04:28 +030099#define MVNETA_TYPE_PRIO 0x24bc
100#define MVNETA_FORCE_UNI BIT(21)
101#define MVNETA_TXQ_CMD_1 0x24e4
102#define MVNETA_TXQ_CMD 0x2448
103#define MVNETA_TXQ_DISABLE_SHIFT 8
104#define MVNETA_TXQ_ENABLE_MASK 0x000000ff
Andrew Lunne4839112015-10-22 18:37:36 +0100105#define MVNETA_RX_DISCARD_FRAME_COUNT 0x2484
106#define MVNETA_OVERRUN_FRAME_COUNT 0x2488
Stas Sergeev898b29702015-04-01 20:32:49 +0300107#define MVNETA_GMAC_CLOCK_DIVIDER 0x24f4
108#define MVNETA_GMAC_1MS_CLOCK_ENABLE BIT(31)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300109#define MVNETA_ACC_MODE 0x2500
110#define MVNETA_CPU_MAP(cpu) (0x2540 + ((cpu) << 2))
111#define MVNETA_CPU_RXQ_ACCESS_ALL_MASK 0x000000ff
112#define MVNETA_CPU_TXQ_ACCESS_ALL_MASK 0x0000ff00
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +0100113#define MVNETA_CPU_RXQ_ACCESS(rxq) BIT(rxq)
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +0100114#define MVNETA_CPU_TXQ_ACCESS(txq) BIT(txq + 8)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300115#define MVNETA_RXQ_TIME_COAL_REG(q) (0x2580 + ((q) << 2))
willy tarreau40ba35e2014-01-16 08:20:10 +0100116
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +0100117/* Exception Interrupt Port/Queue Cause register
118 *
119 * Their behavior depend of the mapping done using the PCPX2Q
120 * registers. For a given CPU if the bit associated to a queue is not
121 * set, then for the register a read from this CPU will always return
122 * 0 and a write won't do anything
123 */
willy tarreau40ba35e2014-01-16 08:20:10 +0100124
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300125#define MVNETA_INTR_NEW_CAUSE 0x25a0
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300126#define MVNETA_INTR_NEW_MASK 0x25a4
willy tarreau40ba35e2014-01-16 08:20:10 +0100127
128/* bits 0..7 = TXQ SENT, one bit per queue.
129 * bits 8..15 = RXQ OCCUP, one bit per queue.
130 * bits 16..23 = RXQ FREE, one bit per queue.
131 * bit 29 = OLD_REG_SUM, see old reg ?
132 * bit 30 = TX_ERR_SUM, one bit for 4 ports
133 * bit 31 = MISC_SUM, one bit for 4 ports
134 */
135#define MVNETA_TX_INTR_MASK(nr_txqs) (((1 << nr_txqs) - 1) << 0)
136#define MVNETA_TX_INTR_MASK_ALL (0xff << 0)
137#define MVNETA_RX_INTR_MASK(nr_rxqs) (((1 << nr_rxqs) - 1) << 8)
138#define MVNETA_RX_INTR_MASK_ALL (0xff << 8)
Stas Sergeev898b29702015-04-01 20:32:49 +0300139#define MVNETA_MISCINTR_INTR_MASK BIT(31)
willy tarreau40ba35e2014-01-16 08:20:10 +0100140
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300141#define MVNETA_INTR_OLD_CAUSE 0x25a8
142#define MVNETA_INTR_OLD_MASK 0x25ac
willy tarreau40ba35e2014-01-16 08:20:10 +0100143
144/* Data Path Port/Queue Cause Register */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300145#define MVNETA_INTR_MISC_CAUSE 0x25b0
146#define MVNETA_INTR_MISC_MASK 0x25b4
willy tarreau40ba35e2014-01-16 08:20:10 +0100147
148#define MVNETA_CAUSE_PHY_STATUS_CHANGE BIT(0)
149#define MVNETA_CAUSE_LINK_CHANGE BIT(1)
150#define MVNETA_CAUSE_PTP BIT(4)
151
152#define MVNETA_CAUSE_INTERNAL_ADDR_ERR BIT(7)
153#define MVNETA_CAUSE_RX_OVERRUN BIT(8)
154#define MVNETA_CAUSE_RX_CRC_ERROR BIT(9)
155#define MVNETA_CAUSE_RX_LARGE_PKT BIT(10)
156#define MVNETA_CAUSE_TX_UNDERUN BIT(11)
157#define MVNETA_CAUSE_PRBS_ERR BIT(12)
158#define MVNETA_CAUSE_PSC_SYNC_CHANGE BIT(13)
159#define MVNETA_CAUSE_SERDES_SYNC_ERR BIT(14)
160
161#define MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT 16
162#define MVNETA_CAUSE_BMU_ALLOC_ERR_ALL_MASK (0xF << MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT)
163#define MVNETA_CAUSE_BMU_ALLOC_ERR_MASK(pool) (1 << (MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT + (pool)))
164
165#define MVNETA_CAUSE_TXQ_ERROR_SHIFT 24
166#define MVNETA_CAUSE_TXQ_ERROR_ALL_MASK (0xFF << MVNETA_CAUSE_TXQ_ERROR_SHIFT)
167#define MVNETA_CAUSE_TXQ_ERROR_MASK(q) (1 << (MVNETA_CAUSE_TXQ_ERROR_SHIFT + (q)))
168
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300169#define MVNETA_INTR_ENABLE 0x25b8
170#define MVNETA_TXQ_INTR_ENABLE_ALL_MASK 0x0000ff00
Marcin Wojtasdc1aadf2015-11-30 13:27:43 +0100171#define MVNETA_RXQ_INTR_ENABLE_ALL_MASK 0x000000ff
willy tarreau40ba35e2014-01-16 08:20:10 +0100172
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300173#define MVNETA_RXQ_CMD 0x2680
174#define MVNETA_RXQ_DISABLE_SHIFT 8
175#define MVNETA_RXQ_ENABLE_MASK 0x000000ff
176#define MVETH_TXQ_TOKEN_COUNT_REG(q) (0x2700 + ((q) << 4))
177#define MVETH_TXQ_TOKEN_CFG_REG(q) (0x2704 + ((q) << 4))
178#define MVNETA_GMAC_CTRL_0 0x2c00
179#define MVNETA_GMAC_MAX_RX_SIZE_SHIFT 2
180#define MVNETA_GMAC_MAX_RX_SIZE_MASK 0x7ffc
181#define MVNETA_GMAC0_PORT_ENABLE BIT(0)
182#define MVNETA_GMAC_CTRL_2 0x2c08
Stas Sergeev898b29702015-04-01 20:32:49 +0300183#define MVNETA_GMAC2_INBAND_AN_ENABLE BIT(0)
Thomas Petazzonia79121d2014-03-26 00:25:41 +0100184#define MVNETA_GMAC2_PCS_ENABLE BIT(3)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300185#define MVNETA_GMAC2_PORT_RGMII BIT(4)
186#define MVNETA_GMAC2_PORT_RESET BIT(6)
187#define MVNETA_GMAC_STATUS 0x2c10
188#define MVNETA_GMAC_LINK_UP BIT(0)
189#define MVNETA_GMAC_SPEED_1000 BIT(1)
190#define MVNETA_GMAC_SPEED_100 BIT(2)
191#define MVNETA_GMAC_FULL_DUPLEX BIT(3)
192#define MVNETA_GMAC_RX_FLOW_CTRL_ENABLE BIT(4)
193#define MVNETA_GMAC_TX_FLOW_CTRL_ENABLE BIT(5)
194#define MVNETA_GMAC_RX_FLOW_CTRL_ACTIVE BIT(6)
195#define MVNETA_GMAC_TX_FLOW_CTRL_ACTIVE BIT(7)
196#define MVNETA_GMAC_AUTONEG_CONFIG 0x2c0c
197#define MVNETA_GMAC_FORCE_LINK_DOWN BIT(0)
198#define MVNETA_GMAC_FORCE_LINK_PASS BIT(1)
Stas Sergeev898b29702015-04-01 20:32:49 +0300199#define MVNETA_GMAC_INBAND_AN_ENABLE BIT(2)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300200#define MVNETA_GMAC_CONFIG_MII_SPEED BIT(5)
201#define MVNETA_GMAC_CONFIG_GMII_SPEED BIT(6)
Thomas Petazzoni71408602013-09-04 16:21:18 +0200202#define MVNETA_GMAC_AN_SPEED_EN BIT(7)
Stas Sergeev898b29702015-04-01 20:32:49 +0300203#define MVNETA_GMAC_AN_FLOW_CTRL_EN BIT(11)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300204#define MVNETA_GMAC_CONFIG_FULL_DUPLEX BIT(12)
Thomas Petazzoni71408602013-09-04 16:21:18 +0200205#define MVNETA_GMAC_AN_DUPLEX_EN BIT(13)
Andrew Lunne4839112015-10-22 18:37:36 +0100206#define MVNETA_MIB_COUNTERS_BASE 0x3000
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300207#define MVNETA_MIB_LATE_COLLISION 0x7c
208#define MVNETA_DA_FILT_SPEC_MCAST 0x3400
209#define MVNETA_DA_FILT_OTH_MCAST 0x3500
210#define MVNETA_DA_FILT_UCAST_BASE 0x3600
211#define MVNETA_TXQ_BASE_ADDR_REG(q) (0x3c00 + ((q) << 2))
212#define MVNETA_TXQ_SIZE_REG(q) (0x3c20 + ((q) << 2))
213#define MVNETA_TXQ_SENT_THRESH_ALL_MASK 0x3fff0000
214#define MVNETA_TXQ_SENT_THRESH_MASK(coal) ((coal) << 16)
215#define MVNETA_TXQ_UPDATE_REG(q) (0x3c60 + ((q) << 2))
216#define MVNETA_TXQ_DEC_SENT_SHIFT 16
217#define MVNETA_TXQ_STATUS_REG(q) (0x3c40 + ((q) << 2))
218#define MVNETA_TXQ_SENT_DESC_SHIFT 16
219#define MVNETA_TXQ_SENT_DESC_MASK 0x3fff0000
220#define MVNETA_PORT_TX_RESET 0x3cf0
221#define MVNETA_PORT_TX_DMA_RESET BIT(0)
222#define MVNETA_TX_MTU 0x3e0c
223#define MVNETA_TX_TOKEN_SIZE 0x3e14
224#define MVNETA_TX_TOKEN_SIZE_MAX 0xffffffff
225#define MVNETA_TXQ_TOKEN_SIZE_REG(q) (0x3e40 + ((q) << 2))
226#define MVNETA_TXQ_TOKEN_SIZE_MAX 0x7fffffff
227
228#define MVNETA_CAUSE_TXQ_SENT_DESC_ALL_MASK 0xff
229
230/* Descriptor ring Macros */
231#define MVNETA_QUEUE_NEXT_DESC(q, index) \
232 (((index) < (q)->last_desc) ? ((index) + 1) : 0)
233
234/* Various constants */
235
236/* Coalescing */
willy tarreauaebea2b2014-12-02 08:13:04 +0100237#define MVNETA_TXDONE_COAL_PKTS 1
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300238#define MVNETA_RX_COAL_PKTS 32
239#define MVNETA_RX_COAL_USEC 100
240
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100241/* The two bytes Marvell header. Either contains a special value used
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300242 * by Marvell switches when a specific hardware mode is enabled (not
243 * supported by this driver) or is filled automatically by zeroes on
244 * the RX side. Those two bytes being at the front of the Ethernet
245 * header, they allow to have the IP header aligned on a 4 bytes
246 * boundary automatically: the hardware skips those two bytes on its
247 * own.
248 */
249#define MVNETA_MH_SIZE 2
250
251#define MVNETA_VLAN_TAG_LEN 4
252
253#define MVNETA_CPU_D_CACHE_LINE_SIZE 32
Marcin Wojtas9110ee02015-11-30 13:27:45 +0100254#define MVNETA_TX_CSUM_DEF_SIZE 1600
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300255#define MVNETA_TX_CSUM_MAX_SIZE 9800
256#define MVNETA_ACC_MODE_EXT 1
257
258/* Timeout constants */
259#define MVNETA_TX_DISABLE_TIMEOUT_MSEC 1000
260#define MVNETA_RX_DISABLE_TIMEOUT_MSEC 1000
261#define MVNETA_TX_FIFO_EMPTY_TIMEOUT 10000
262
263#define MVNETA_TX_MTU_MAX 0x3ffff
264
Gregory CLEMENT9a401de2015-12-09 18:23:50 +0100265/* The RSS lookup table actually has 256 entries but we do not use
266 * them yet
267 */
268#define MVNETA_RSS_LU_TABLE_SIZE 1
269
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -0300270/* TSO header size */
271#define TSO_HEADER_SIZE 128
272
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300273/* Max number of Rx descriptors */
274#define MVNETA_MAX_RXD 128
275
276/* Max number of Tx descriptors */
277#define MVNETA_MAX_TXD 532
278
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -0300279/* Max number of allowed TCP segments for software TSO */
280#define MVNETA_MAX_TSO_SEGS 100
281
282#define MVNETA_MAX_SKB_DESCS (MVNETA_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
283
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300284/* descriptor aligned size */
285#define MVNETA_DESC_ALIGNED_SIZE 32
286
287#define MVNETA_RX_PKT_SIZE(mtu) \
288 ALIGN((mtu) + MVNETA_MH_SIZE + MVNETA_VLAN_TAG_LEN + \
289 ETH_HLEN + ETH_FCS_LEN, \
290 MVNETA_CPU_D_CACHE_LINE_SIZE)
291
Ezequiel Garcia2e3173a2014-05-30 13:40:07 -0300292#define IS_TSO_HEADER(txq, addr) \
293 ((addr >= txq->tso_hdrs_phys) && \
294 (addr < txq->tso_hdrs_phys + txq->size * TSO_HEADER_SIZE))
295
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300296#define MVNETA_RX_BUF_SIZE(pkt_size) ((pkt_size) + NET_SKB_PAD)
297
Russell King9b0cdef2015-10-22 18:37:30 +0100298struct mvneta_statistic {
299 unsigned short offset;
300 unsigned short type;
301 const char name[ETH_GSTRING_LEN];
302};
303
304#define T_REG_32 32
305#define T_REG_64 64
306
307static const struct mvneta_statistic mvneta_statistics[] = {
308 { 0x3000, T_REG_64, "good_octets_received", },
309 { 0x3010, T_REG_32, "good_frames_received", },
310 { 0x3008, T_REG_32, "bad_octets_received", },
311 { 0x3014, T_REG_32, "bad_frames_received", },
312 { 0x3018, T_REG_32, "broadcast_frames_received", },
313 { 0x301c, T_REG_32, "multicast_frames_received", },
314 { 0x3050, T_REG_32, "unrec_mac_control_received", },
315 { 0x3058, T_REG_32, "good_fc_received", },
316 { 0x305c, T_REG_32, "bad_fc_received", },
317 { 0x3060, T_REG_32, "undersize_received", },
318 { 0x3064, T_REG_32, "fragments_received", },
319 { 0x3068, T_REG_32, "oversize_received", },
320 { 0x306c, T_REG_32, "jabber_received", },
321 { 0x3070, T_REG_32, "mac_receive_error", },
322 { 0x3074, T_REG_32, "bad_crc_event", },
323 { 0x3078, T_REG_32, "collision", },
324 { 0x307c, T_REG_32, "late_collision", },
325 { 0x2484, T_REG_32, "rx_discard", },
326 { 0x2488, T_REG_32, "rx_overrun", },
327 { 0x3020, T_REG_32, "frames_64_octets", },
328 { 0x3024, T_REG_32, "frames_65_to_127_octets", },
329 { 0x3028, T_REG_32, "frames_128_to_255_octets", },
330 { 0x302c, T_REG_32, "frames_256_to_511_octets", },
331 { 0x3030, T_REG_32, "frames_512_to_1023_octets", },
332 { 0x3034, T_REG_32, "frames_1024_to_max_octets", },
333 { 0x3038, T_REG_64, "good_octets_sent", },
334 { 0x3040, T_REG_32, "good_frames_sent", },
335 { 0x3044, T_REG_32, "excessive_collision", },
336 { 0x3048, T_REG_32, "multicast_frames_sent", },
337 { 0x304c, T_REG_32, "broadcast_frames_sent", },
338 { 0x3054, T_REG_32, "fc_sent", },
339 { 0x300c, T_REG_32, "internal_mac_transmit_err", },
340};
341
willy tarreau74c41b02014-01-16 08:20:08 +0100342struct mvneta_pcpu_stats {
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300343 struct u64_stats_sync syncp;
willy tarreau74c41b02014-01-16 08:20:08 +0100344 u64 rx_packets;
345 u64 rx_bytes;
346 u64 tx_packets;
347 u64 tx_bytes;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300348};
349
Maxime Ripard12bb03b2015-09-25 18:09:36 +0200350struct mvneta_pcpu_port {
351 /* Pointer to the shared port */
352 struct mvneta_port *pp;
353
354 /* Pointer to the CPU-local NAPI struct */
355 struct napi_struct napi;
356
357 /* Cause of the previous interrupt */
358 u32 cause_rx_tx;
359};
360
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300361struct mvneta_port {
Maxime Ripard12bb03b2015-09-25 18:09:36 +0200362 struct mvneta_pcpu_port __percpu *ports;
363 struct mvneta_pcpu_stats __percpu *stats;
364
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300365 int pkt_size;
willy tarreau8ec2cd42014-01-16 08:20:16 +0100366 unsigned int frag_size;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300367 void __iomem *base;
368 struct mvneta_rx_queue *rxqs;
369 struct mvneta_tx_queue *txqs;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300370 struct net_device *dev;
Maxime Ripardf8642882015-09-25 18:09:38 +0200371 struct notifier_block cpu_notifier;
Gregory CLEMENT90b74c02015-12-09 18:23:48 +0100372 int rxq_def;
Gregory CLEMENT58885112016-02-04 22:09:28 +0100373 /* Protect the access to the percpu interrupt registers,
374 * ensuring that the configuration remains coherent.
375 */
376 spinlock_t lock;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300377
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300378 /* Core clock */
Thomas Petazzoni189dd622012-11-19 14:15:25 +0100379 struct clk *clk;
Jisheng Zhang15cc4a42016-01-20 19:27:24 +0800380 /* AXI clock */
381 struct clk *clk_bus;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300382 u8 mcast_count[256];
383 u16 tx_ring_size;
384 u16 rx_ring_size;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300385
386 struct mii_bus *mii_bus;
387 struct phy_device *phy_dev;
388 phy_interface_t phy_interface;
389 struct device_node *phy_node;
390 unsigned int link;
391 unsigned int duplex;
392 unsigned int speed;
Simon Guinotb65657f2015-06-30 16:20:22 +0200393 unsigned int tx_csum_limit;
Stas Sergeev0c0744f2015-12-02 20:35:11 +0300394 unsigned int use_inband_status:1;
Russell King9b0cdef2015-10-22 18:37:30 +0100395
396 u64 ethtool_stats[ARRAY_SIZE(mvneta_statistics)];
Gregory CLEMENT9a401de2015-12-09 18:23:50 +0100397
398 u32 indir[MVNETA_RSS_LU_TABLE_SIZE];
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300399};
400
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100401/* The mvneta_tx_desc and mvneta_rx_desc structures describe the
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300402 * layout of the transmit and reception DMA descriptors, and their
403 * layout is therefore defined by the hardware design
404 */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200405
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300406#define MVNETA_TX_L3_OFF_SHIFT 0
407#define MVNETA_TX_IP_HLEN_SHIFT 8
408#define MVNETA_TX_L4_UDP BIT(16)
409#define MVNETA_TX_L3_IP6 BIT(17)
410#define MVNETA_TXD_IP_CSUM BIT(18)
411#define MVNETA_TXD_Z_PAD BIT(19)
412#define MVNETA_TXD_L_DESC BIT(20)
413#define MVNETA_TXD_F_DESC BIT(21)
414#define MVNETA_TXD_FLZ_DESC (MVNETA_TXD_Z_PAD | \
415 MVNETA_TXD_L_DESC | \
416 MVNETA_TXD_F_DESC)
417#define MVNETA_TX_L4_CSUM_FULL BIT(30)
418#define MVNETA_TX_L4_CSUM_NOT BIT(31)
419
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300420#define MVNETA_RXD_ERR_CRC 0x0
421#define MVNETA_RXD_ERR_SUMMARY BIT(16)
422#define MVNETA_RXD_ERR_OVERRUN BIT(17)
423#define MVNETA_RXD_ERR_LEN BIT(18)
424#define MVNETA_RXD_ERR_RESOURCE (BIT(17) | BIT(18))
425#define MVNETA_RXD_ERR_CODE_MASK (BIT(17) | BIT(18))
426#define MVNETA_RXD_L3_IP4 BIT(25)
427#define MVNETA_RXD_FIRST_LAST_DESC (BIT(26) | BIT(27))
428#define MVNETA_RXD_L4_CSUM_OK BIT(30)
429
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +0200430#if defined(__LITTLE_ENDIAN)
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200431struct mvneta_tx_desc {
432 u32 command; /* Options used by HW for packet transmitting.*/
433 u16 reserverd1; /* csum_l4 (for future use) */
434 u16 data_size; /* Data size of transmitted packet in bytes */
435 u32 buf_phys_addr; /* Physical addr of transmitted buffer */
436 u32 reserved2; /* hw_cmd - (for future use, PMT) */
437 u32 reserved3[4]; /* Reserved - (for future use) */
438};
439
440struct mvneta_rx_desc {
441 u32 status; /* Info about received packet */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300442 u16 reserved1; /* pnc_info - (for future use, PnC) */
443 u16 data_size; /* Size of received packet in bytes */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200444
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300445 u32 buf_phys_addr; /* Physical address of the buffer */
446 u32 reserved2; /* pnc_flow_id (for future use, PnC) */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200447
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300448 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
449 u16 reserved3; /* prefetch_cmd, for future use */
450 u16 reserved4; /* csum_l4 - (for future use, PnC) */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200451
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300452 u32 reserved5; /* pnc_extra PnC (for future use, PnC) */
453 u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */
454};
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +0200455#else
456struct mvneta_tx_desc {
457 u16 data_size; /* Data size of transmitted packet in bytes */
458 u16 reserverd1; /* csum_l4 (for future use) */
459 u32 command; /* Options used by HW for packet transmitting.*/
460 u32 reserved2; /* hw_cmd - (for future use, PMT) */
461 u32 buf_phys_addr; /* Physical addr of transmitted buffer */
462 u32 reserved3[4]; /* Reserved - (for future use) */
463};
464
465struct mvneta_rx_desc {
466 u16 data_size; /* Size of received packet in bytes */
467 u16 reserved1; /* pnc_info - (for future use, PnC) */
468 u32 status; /* Info about received packet */
469
470 u32 reserved2; /* pnc_flow_id (for future use, PnC) */
471 u32 buf_phys_addr; /* Physical address of the buffer */
472
473 u16 reserved4; /* csum_l4 - (for future use, PnC) */
474 u16 reserved3; /* prefetch_cmd, for future use */
475 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
476
477 u32 reserved5; /* pnc_extra PnC (for future use, PnC) */
478 u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */
479};
480#endif
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300481
482struct mvneta_tx_queue {
483 /* Number of this TX queue, in the range 0-7 */
484 u8 id;
485
486 /* Number of TX DMA descriptors in the descriptor ring */
487 int size;
488
489 /* Number of currently used TX DMA descriptor in the
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100490 * descriptor ring
491 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300492 int count;
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -0300493 int tx_stop_threshold;
494 int tx_wake_threshold;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300495
496 /* Array of transmitted skb */
497 struct sk_buff **tx_skb;
498
499 /* Index of last TX DMA descriptor that was inserted */
500 int txq_put_index;
501
502 /* Index of the TX DMA descriptor to be cleaned up */
503 int txq_get_index;
504
505 u32 done_pkts_coal;
506
507 /* Virtual address of the TX DMA descriptors array */
508 struct mvneta_tx_desc *descs;
509
510 /* DMA address of the TX DMA descriptors array */
511 dma_addr_t descs_phys;
512
513 /* Index of the last TX DMA descriptor */
514 int last_desc;
515
516 /* Index of the next TX DMA descriptor to process */
517 int next_desc_to_proc;
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -0300518
519 /* DMA buffers for TSO headers */
520 char *tso_hdrs;
521
522 /* DMA address of TSO headers */
523 dma_addr_t tso_hdrs_phys;
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +0100524
525 /* Affinity mask for CPUs*/
526 cpumask_t affinity_mask;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300527};
528
529struct mvneta_rx_queue {
530 /* rx queue number, in the range 0-7 */
531 u8 id;
532
533 /* num of rx descriptors in the rx descriptor ring */
534 int size;
535
536 /* counter of times when mvneta_refill() failed */
537 int missed;
538
539 u32 pkts_coal;
540 u32 time_coal;
541
542 /* Virtual address of the RX DMA descriptors array */
543 struct mvneta_rx_desc *descs;
544
545 /* DMA address of the RX DMA descriptors array */
546 dma_addr_t descs_phys;
547
548 /* Index of the last RX DMA descriptor */
549 int last_desc;
550
551 /* Index of the next RX DMA descriptor to process */
552 int next_desc_to_proc;
553};
554
Ezequiel Garciaedadb7f2014-05-22 20:07:01 -0300555/* The hardware supports eight (8) rx queues, but we are only allowing
556 * the first one to be used. Therefore, let's just allocate one queue.
557 */
Maxime Ripardd8936652015-09-25 18:09:37 +0200558static int rxq_number = 8;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300559static int txq_number = 8;
560
561static int rxq_def;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300562
willy tarreauf19fadf2014-01-16 08:20:17 +0100563static int rx_copybreak __read_mostly = 256;
564
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300565#define MVNETA_DRIVER_NAME "mvneta"
566#define MVNETA_DRIVER_VERSION "1.0"
567
568/* Utility/helper methods */
569
570/* Write helper method */
571static void mvreg_write(struct mvneta_port *pp, u32 offset, u32 data)
572{
573 writel(data, pp->base + offset);
574}
575
576/* Read helper method */
577static u32 mvreg_read(struct mvneta_port *pp, u32 offset)
578{
579 return readl(pp->base + offset);
580}
581
582/* Increment txq get counter */
583static void mvneta_txq_inc_get(struct mvneta_tx_queue *txq)
584{
585 txq->txq_get_index++;
586 if (txq->txq_get_index == txq->size)
587 txq->txq_get_index = 0;
588}
589
590/* Increment txq put counter */
591static void mvneta_txq_inc_put(struct mvneta_tx_queue *txq)
592{
593 txq->txq_put_index++;
594 if (txq->txq_put_index == txq->size)
595 txq->txq_put_index = 0;
596}
597
598
599/* Clear all MIB counters */
600static void mvneta_mib_counters_clear(struct mvneta_port *pp)
601{
602 int i;
603 u32 dummy;
604
605 /* Perform dummy reads from MIB counters */
606 for (i = 0; i < MVNETA_MIB_LATE_COLLISION; i += 4)
607 dummy = mvreg_read(pp, (MVNETA_MIB_COUNTERS_BASE + i));
Andrew Lunne4839112015-10-22 18:37:36 +0100608 dummy = mvreg_read(pp, MVNETA_RX_DISCARD_FRAME_COUNT);
609 dummy = mvreg_read(pp, MVNETA_OVERRUN_FRAME_COUNT);
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300610}
611
612/* Get System Network Statistics */
613struct rtnl_link_stats64 *mvneta_get_stats64(struct net_device *dev,
614 struct rtnl_link_stats64 *stats)
615{
616 struct mvneta_port *pp = netdev_priv(dev);
617 unsigned int start;
willy tarreau74c41b02014-01-16 08:20:08 +0100618 int cpu;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300619
willy tarreau74c41b02014-01-16 08:20:08 +0100620 for_each_possible_cpu(cpu) {
621 struct mvneta_pcpu_stats *cpu_stats;
622 u64 rx_packets;
623 u64 rx_bytes;
624 u64 tx_packets;
625 u64 tx_bytes;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300626
willy tarreau74c41b02014-01-16 08:20:08 +0100627 cpu_stats = per_cpu_ptr(pp->stats, cpu);
628 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700629 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
willy tarreau74c41b02014-01-16 08:20:08 +0100630 rx_packets = cpu_stats->rx_packets;
631 rx_bytes = cpu_stats->rx_bytes;
632 tx_packets = cpu_stats->tx_packets;
633 tx_bytes = cpu_stats->tx_bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700634 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300635
willy tarreau74c41b02014-01-16 08:20:08 +0100636 stats->rx_packets += rx_packets;
637 stats->rx_bytes += rx_bytes;
638 stats->tx_packets += tx_packets;
639 stats->tx_bytes += tx_bytes;
640 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300641
642 stats->rx_errors = dev->stats.rx_errors;
643 stats->rx_dropped = dev->stats.rx_dropped;
644
645 stats->tx_dropped = dev->stats.tx_dropped;
646
647 return stats;
648}
649
650/* Rx descriptors helper methods */
651
willy tarreau54282132014-01-16 08:20:14 +0100652/* Checks whether the RX descriptor having this status is both the first
653 * and the last descriptor for the RX packet. Each RX packet is currently
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300654 * received through a single RX descriptor, so not having each RX
655 * descriptor with its first and last bits set is an error
656 */
willy tarreau54282132014-01-16 08:20:14 +0100657static int mvneta_rxq_desc_is_first_last(u32 status)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300658{
willy tarreau54282132014-01-16 08:20:14 +0100659 return (status & MVNETA_RXD_FIRST_LAST_DESC) ==
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300660 MVNETA_RXD_FIRST_LAST_DESC;
661}
662
663/* Add number of descriptors ready to receive new packets */
664static void mvneta_rxq_non_occup_desc_add(struct mvneta_port *pp,
665 struct mvneta_rx_queue *rxq,
666 int ndescs)
667{
668 /* Only MVNETA_RXQ_ADD_NON_OCCUPIED_MAX (255) descriptors can
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100669 * be added at once
670 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300671 while (ndescs > MVNETA_RXQ_ADD_NON_OCCUPIED_MAX) {
672 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
673 (MVNETA_RXQ_ADD_NON_OCCUPIED_MAX <<
674 MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
675 ndescs -= MVNETA_RXQ_ADD_NON_OCCUPIED_MAX;
676 }
677
678 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
679 (ndescs << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
680}
681
682/* Get number of RX descriptors occupied by received packets */
683static int mvneta_rxq_busy_desc_num_get(struct mvneta_port *pp,
684 struct mvneta_rx_queue *rxq)
685{
686 u32 val;
687
688 val = mvreg_read(pp, MVNETA_RXQ_STATUS_REG(rxq->id));
689 return val & MVNETA_RXQ_OCCUPIED_ALL_MASK;
690}
691
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100692/* Update num of rx desc called upon return from rx path or
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300693 * from mvneta_rxq_drop_pkts().
694 */
695static void mvneta_rxq_desc_num_update(struct mvneta_port *pp,
696 struct mvneta_rx_queue *rxq,
697 int rx_done, int rx_filled)
698{
699 u32 val;
700
701 if ((rx_done <= 0xff) && (rx_filled <= 0xff)) {
702 val = rx_done |
703 (rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT);
704 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
705 return;
706 }
707
708 /* Only 255 descriptors can be added at once */
709 while ((rx_done > 0) || (rx_filled > 0)) {
710 if (rx_done <= 0xff) {
711 val = rx_done;
712 rx_done = 0;
713 } else {
714 val = 0xff;
715 rx_done -= 0xff;
716 }
717 if (rx_filled <= 0xff) {
718 val |= rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
719 rx_filled = 0;
720 } else {
721 val |= 0xff << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
722 rx_filled -= 0xff;
723 }
724 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
725 }
726}
727
728/* Get pointer to next RX descriptor to be processed by SW */
729static struct mvneta_rx_desc *
730mvneta_rxq_next_desc_get(struct mvneta_rx_queue *rxq)
731{
732 int rx_desc = rxq->next_desc_to_proc;
733
734 rxq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(rxq, rx_desc);
willy tarreau34e41792014-01-16 08:20:15 +0100735 prefetch(rxq->descs + rxq->next_desc_to_proc);
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300736 return rxq->descs + rx_desc;
737}
738
739/* Change maximum receive size of the port. */
740static void mvneta_max_rx_size_set(struct mvneta_port *pp, int max_rx_size)
741{
742 u32 val;
743
744 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
745 val &= ~MVNETA_GMAC_MAX_RX_SIZE_MASK;
746 val |= ((max_rx_size - MVNETA_MH_SIZE) / 2) <<
747 MVNETA_GMAC_MAX_RX_SIZE_SHIFT;
748 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
749}
750
751
752/* Set rx queue offset */
753static void mvneta_rxq_offset_set(struct mvneta_port *pp,
754 struct mvneta_rx_queue *rxq,
755 int offset)
756{
757 u32 val;
758
759 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
760 val &= ~MVNETA_RXQ_PKT_OFFSET_ALL_MASK;
761
762 /* Offset is in */
763 val |= MVNETA_RXQ_PKT_OFFSET_MASK(offset >> 3);
764 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
765}
766
767
768/* Tx descriptors helper methods */
769
770/* Update HW with number of TX descriptors to be sent */
771static void mvneta_txq_pend_desc_add(struct mvneta_port *pp,
772 struct mvneta_tx_queue *txq,
773 int pend_desc)
774{
775 u32 val;
776
777 /* Only 255 descriptors can be added at once ; Assume caller
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100778 * process TX desriptors in quanta less than 256
779 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300780 val = pend_desc;
781 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
782}
783
784/* Get pointer to next TX descriptor to be processed (send) by HW */
785static struct mvneta_tx_desc *
786mvneta_txq_next_desc_get(struct mvneta_tx_queue *txq)
787{
788 int tx_desc = txq->next_desc_to_proc;
789
790 txq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(txq, tx_desc);
791 return txq->descs + tx_desc;
792}
793
794/* Release the last allocated TX descriptor. Useful to handle DMA
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100795 * mapping failures in the TX path.
796 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300797static void mvneta_txq_desc_put(struct mvneta_tx_queue *txq)
798{
799 if (txq->next_desc_to_proc == 0)
800 txq->next_desc_to_proc = txq->last_desc - 1;
801 else
802 txq->next_desc_to_proc--;
803}
804
805/* Set rxq buf size */
806static void mvneta_rxq_buf_size_set(struct mvneta_port *pp,
807 struct mvneta_rx_queue *rxq,
808 int buf_size)
809{
810 u32 val;
811
812 val = mvreg_read(pp, MVNETA_RXQ_SIZE_REG(rxq->id));
813
814 val &= ~MVNETA_RXQ_BUF_SIZE_MASK;
815 val |= ((buf_size >> 3) << MVNETA_RXQ_BUF_SIZE_SHIFT);
816
817 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), val);
818}
819
820/* Disable buffer management (BM) */
821static void mvneta_rxq_bm_disable(struct mvneta_port *pp,
822 struct mvneta_rx_queue *rxq)
823{
824 u32 val;
825
826 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
827 val &= ~MVNETA_RXQ_HW_BUF_ALLOC;
828 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
829}
830
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300831/* Start the Ethernet port RX and TX activity */
832static void mvneta_port_up(struct mvneta_port *pp)
833{
834 int queue;
835 u32 q_map;
836
837 /* Enable all initialized TXs. */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300838 q_map = 0;
839 for (queue = 0; queue < txq_number; queue++) {
840 struct mvneta_tx_queue *txq = &pp->txqs[queue];
841 if (txq->descs != NULL)
842 q_map |= (1 << queue);
843 }
844 mvreg_write(pp, MVNETA_TXQ_CMD, q_map);
845
846 /* Enable all initialized RXQs. */
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +0100847 for (queue = 0; queue < rxq_number; queue++) {
848 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
849
850 if (rxq->descs != NULL)
851 q_map |= (1 << queue);
852 }
853 mvreg_write(pp, MVNETA_RXQ_CMD, q_map);
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300854}
855
856/* Stop the Ethernet port activity */
857static void mvneta_port_down(struct mvneta_port *pp)
858{
859 u32 val;
860 int count;
861
862 /* Stop Rx port activity. Check port Rx activity. */
863 val = mvreg_read(pp, MVNETA_RXQ_CMD) & MVNETA_RXQ_ENABLE_MASK;
864
865 /* Issue stop command for active channels only */
866 if (val != 0)
867 mvreg_write(pp, MVNETA_RXQ_CMD,
868 val << MVNETA_RXQ_DISABLE_SHIFT);
869
870 /* Wait for all Rx activity to terminate. */
871 count = 0;
872 do {
873 if (count++ >= MVNETA_RX_DISABLE_TIMEOUT_MSEC) {
874 netdev_warn(pp->dev,
875 "TIMEOUT for RX stopped ! rx_queue_cmd: 0x08%x\n",
876 val);
877 break;
878 }
879 mdelay(1);
880
881 val = mvreg_read(pp, MVNETA_RXQ_CMD);
882 } while (val & 0xff);
883
884 /* Stop Tx port activity. Check port Tx activity. Issue stop
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100885 * command for active channels only
886 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300887 val = (mvreg_read(pp, MVNETA_TXQ_CMD)) & MVNETA_TXQ_ENABLE_MASK;
888
889 if (val != 0)
890 mvreg_write(pp, MVNETA_TXQ_CMD,
891 (val << MVNETA_TXQ_DISABLE_SHIFT));
892
893 /* Wait for all Tx activity to terminate. */
894 count = 0;
895 do {
896 if (count++ >= MVNETA_TX_DISABLE_TIMEOUT_MSEC) {
897 netdev_warn(pp->dev,
898 "TIMEOUT for TX stopped status=0x%08x\n",
899 val);
900 break;
901 }
902 mdelay(1);
903
904 /* Check TX Command reg that all Txqs are stopped */
905 val = mvreg_read(pp, MVNETA_TXQ_CMD);
906
907 } while (val & 0xff);
908
909 /* Double check to verify that TX FIFO is empty */
910 count = 0;
911 do {
912 if (count++ >= MVNETA_TX_FIFO_EMPTY_TIMEOUT) {
913 netdev_warn(pp->dev,
914 "TX FIFO empty timeout status=0x08%x\n",
915 val);
916 break;
917 }
918 mdelay(1);
919
920 val = mvreg_read(pp, MVNETA_PORT_STATUS);
921 } while (!(val & MVNETA_TX_FIFO_EMPTY) &&
922 (val & MVNETA_TX_IN_PRGRS));
923
924 udelay(200);
925}
926
927/* Enable the port by setting the port enable bit of the MAC control register */
928static void mvneta_port_enable(struct mvneta_port *pp)
929{
930 u32 val;
931
932 /* Enable port */
933 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
934 val |= MVNETA_GMAC0_PORT_ENABLE;
935 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
936}
937
938/* Disable the port and wait for about 200 usec before retuning */
939static void mvneta_port_disable(struct mvneta_port *pp)
940{
941 u32 val;
942
943 /* Reset the Enable bit in the Serial Control Register */
944 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
945 val &= ~MVNETA_GMAC0_PORT_ENABLE;
946 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
947
948 udelay(200);
949}
950
951/* Multicast tables methods */
952
953/* Set all entries in Unicast MAC Table; queue==-1 means reject all */
954static void mvneta_set_ucast_table(struct mvneta_port *pp, int queue)
955{
956 int offset;
957 u32 val;
958
959 if (queue == -1) {
960 val = 0;
961 } else {
962 val = 0x1 | (queue << 1);
963 val |= (val << 24) | (val << 16) | (val << 8);
964 }
965
966 for (offset = 0; offset <= 0xc; offset += 4)
967 mvreg_write(pp, MVNETA_DA_FILT_UCAST_BASE + offset, val);
968}
969
970/* Set all entries in Special Multicast MAC Table; queue==-1 means reject all */
971static void mvneta_set_special_mcast_table(struct mvneta_port *pp, int queue)
972{
973 int offset;
974 u32 val;
975
976 if (queue == -1) {
977 val = 0;
978 } else {
979 val = 0x1 | (queue << 1);
980 val |= (val << 24) | (val << 16) | (val << 8);
981 }
982
983 for (offset = 0; offset <= 0xfc; offset += 4)
984 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + offset, val);
985
986}
987
988/* Set all entries in Other Multicast MAC Table. queue==-1 means reject all */
989static void mvneta_set_other_mcast_table(struct mvneta_port *pp, int queue)
990{
991 int offset;
992 u32 val;
993
994 if (queue == -1) {
995 memset(pp->mcast_count, 0, sizeof(pp->mcast_count));
996 val = 0;
997 } else {
998 memset(pp->mcast_count, 1, sizeof(pp->mcast_count));
999 val = 0x1 | (queue << 1);
1000 val |= (val << 24) | (val << 16) | (val << 8);
1001 }
1002
1003 for (offset = 0; offset <= 0xfc; offset += 4)
1004 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + offset, val);
1005}
1006
Stas Sergeev0c0744f2015-12-02 20:35:11 +03001007static void mvneta_set_autoneg(struct mvneta_port *pp, int enable)
1008{
1009 u32 val;
1010
1011 if (enable) {
1012 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
1013 val &= ~(MVNETA_GMAC_FORCE_LINK_PASS |
1014 MVNETA_GMAC_FORCE_LINK_DOWN |
1015 MVNETA_GMAC_AN_FLOW_CTRL_EN);
1016 val |= MVNETA_GMAC_INBAND_AN_ENABLE |
1017 MVNETA_GMAC_AN_SPEED_EN |
1018 MVNETA_GMAC_AN_DUPLEX_EN;
1019 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
1020
1021 val = mvreg_read(pp, MVNETA_GMAC_CLOCK_DIVIDER);
1022 val |= MVNETA_GMAC_1MS_CLOCK_ENABLE;
1023 mvreg_write(pp, MVNETA_GMAC_CLOCK_DIVIDER, val);
1024
1025 val = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
1026 val |= MVNETA_GMAC2_INBAND_AN_ENABLE;
1027 mvreg_write(pp, MVNETA_GMAC_CTRL_2, val);
1028 } else {
1029 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
1030 val &= ~(MVNETA_GMAC_INBAND_AN_ENABLE |
1031 MVNETA_GMAC_AN_SPEED_EN |
1032 MVNETA_GMAC_AN_DUPLEX_EN);
1033 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
1034
1035 val = mvreg_read(pp, MVNETA_GMAC_CLOCK_DIVIDER);
1036 val &= ~MVNETA_GMAC_1MS_CLOCK_ENABLE;
1037 mvreg_write(pp, MVNETA_GMAC_CLOCK_DIVIDER, val);
1038
1039 val = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
1040 val &= ~MVNETA_GMAC2_INBAND_AN_ENABLE;
1041 mvreg_write(pp, MVNETA_GMAC_CTRL_2, val);
1042 }
1043}
1044
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01001045static void mvneta_percpu_unmask_interrupt(void *arg)
1046{
1047 struct mvneta_port *pp = arg;
1048
1049 /* All the queue are unmasked, but actually only the ones
1050 * mapped to this CPU will be unmasked
1051 */
1052 mvreg_write(pp, MVNETA_INTR_NEW_MASK,
1053 MVNETA_RX_INTR_MASK_ALL |
1054 MVNETA_TX_INTR_MASK_ALL |
1055 MVNETA_MISCINTR_INTR_MASK);
1056}
1057
1058static void mvneta_percpu_mask_interrupt(void *arg)
1059{
1060 struct mvneta_port *pp = arg;
1061
1062 /* All the queue are masked, but actually only the ones
1063 * mapped to this CPU will be masked
1064 */
1065 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
1066 mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0);
1067 mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0);
1068}
1069
1070static void mvneta_percpu_clear_intr_cause(void *arg)
1071{
1072 struct mvneta_port *pp = arg;
1073
1074 /* All the queue are cleared, but actually only the ones
1075 * mapped to this CPU will be cleared
1076 */
1077 mvreg_write(pp, MVNETA_INTR_NEW_CAUSE, 0);
1078 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
1079 mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0);
1080}
1081
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001082/* This method sets defaults to the NETA port:
1083 * Clears interrupt Cause and Mask registers.
1084 * Clears all MAC tables.
1085 * Sets defaults to all registers.
1086 * Resets RX and TX descriptor rings.
1087 * Resets PHY.
1088 * This method can be called after mvneta_port_down() to return the port
1089 * settings to defaults.
1090 */
1091static void mvneta_defaults_set(struct mvneta_port *pp)
1092{
1093 int cpu;
1094 int queue;
1095 u32 val;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001096 int max_cpu = num_present_cpus();
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001097
1098 /* Clear all Cause registers */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01001099 on_each_cpu(mvneta_percpu_clear_intr_cause, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001100
1101 /* Mask all interrupts */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01001102 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001103 mvreg_write(pp, MVNETA_INTR_ENABLE, 0);
1104
1105 /* Enable MBUS Retry bit16 */
1106 mvreg_write(pp, MVNETA_MBUS_RETRY, 0x20);
1107
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01001108 /* Set CPU queue access map. CPUs are assigned to the RX and
1109 * TX queues modulo their number. If there is only one TX
1110 * queue then it is assigned to the CPU associated to the
1111 * default RX queue.
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001112 */
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001113 for_each_present_cpu(cpu) {
1114 int rxq_map = 0, txq_map = 0;
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01001115 int rxq, txq;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001116
1117 for (rxq = 0; rxq < rxq_number; rxq++)
1118 if ((rxq % max_cpu) == cpu)
1119 rxq_map |= MVNETA_CPU_RXQ_ACCESS(rxq);
1120
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01001121 for (txq = 0; txq < txq_number; txq++)
1122 if ((txq % max_cpu) == cpu)
1123 txq_map |= MVNETA_CPU_TXQ_ACCESS(txq);
1124
1125 /* With only one TX queue we configure a special case
1126 * which will allow to get all the irq on a single
1127 * CPU
1128 */
1129 if (txq_number == 1)
1130 txq_map = (cpu == pp->rxq_def) ?
1131 MVNETA_CPU_TXQ_ACCESS(1) : 0;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001132
1133 mvreg_write(pp, MVNETA_CPU_MAP(cpu), rxq_map | txq_map);
1134 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001135
1136 /* Reset RX and TX DMAs */
1137 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
1138 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
1139
1140 /* Disable Legacy WRR, Disable EJP, Release from reset */
1141 mvreg_write(pp, MVNETA_TXQ_CMD_1, 0);
1142 for (queue = 0; queue < txq_number; queue++) {
1143 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(queue), 0);
1144 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(queue), 0);
1145 }
1146
1147 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
1148 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
1149
1150 /* Set Port Acceleration Mode */
1151 val = MVNETA_ACC_MODE_EXT;
1152 mvreg_write(pp, MVNETA_ACC_MODE, val);
1153
1154 /* Update val of portCfg register accordingly with all RxQueue types */
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01001155 val = MVNETA_PORT_CONFIG_DEFL_VALUE(pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001156 mvreg_write(pp, MVNETA_PORT_CONFIG, val);
1157
1158 val = 0;
1159 mvreg_write(pp, MVNETA_PORT_CONFIG_EXTEND, val);
1160 mvreg_write(pp, MVNETA_RX_MIN_FRAME_SIZE, 64);
1161
1162 /* Build PORT_SDMA_CONFIG_REG */
1163 val = 0;
1164
1165 /* Default burst size */
1166 val |= MVNETA_TX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
1167 val |= MVNETA_RX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +02001168 val |= MVNETA_RX_NO_DATA_SWAP | MVNETA_TX_NO_DATA_SWAP;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001169
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +02001170#if defined(__BIG_ENDIAN)
1171 val |= MVNETA_DESC_SWAP;
1172#endif
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001173
1174 /* Assign port SDMA configuration */
1175 mvreg_write(pp, MVNETA_SDMA_CONFIG, val);
1176
Thomas Petazzoni71408602013-09-04 16:21:18 +02001177 /* Disable PHY polling in hardware, since we're using the
1178 * kernel phylib to do this.
1179 */
1180 val = mvreg_read(pp, MVNETA_UNIT_CONTROL);
1181 val &= ~MVNETA_PHY_POLLING_ENABLE;
1182 mvreg_write(pp, MVNETA_UNIT_CONTROL, val);
1183
Stas Sergeev0c0744f2015-12-02 20:35:11 +03001184 mvneta_set_autoneg(pp, pp->use_inband_status);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001185 mvneta_set_ucast_table(pp, -1);
1186 mvneta_set_special_mcast_table(pp, -1);
1187 mvneta_set_other_mcast_table(pp, -1);
1188
1189 /* Set port interrupt enable register - default enable all */
1190 mvreg_write(pp, MVNETA_INTR_ENABLE,
1191 (MVNETA_RXQ_INTR_ENABLE_ALL_MASK
1192 | MVNETA_TXQ_INTR_ENABLE_ALL_MASK));
Andrew Lunne4839112015-10-22 18:37:36 +01001193
1194 mvneta_mib_counters_clear(pp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001195}
1196
1197/* Set max sizes for tx queues */
1198static void mvneta_txq_max_tx_size_set(struct mvneta_port *pp, int max_tx_size)
1199
1200{
1201 u32 val, size, mtu;
1202 int queue;
1203
1204 mtu = max_tx_size * 8;
1205 if (mtu > MVNETA_TX_MTU_MAX)
1206 mtu = MVNETA_TX_MTU_MAX;
1207
1208 /* Set MTU */
1209 val = mvreg_read(pp, MVNETA_TX_MTU);
1210 val &= ~MVNETA_TX_MTU_MAX;
1211 val |= mtu;
1212 mvreg_write(pp, MVNETA_TX_MTU, val);
1213
1214 /* TX token size and all TXQs token size must be larger that MTU */
1215 val = mvreg_read(pp, MVNETA_TX_TOKEN_SIZE);
1216
1217 size = val & MVNETA_TX_TOKEN_SIZE_MAX;
1218 if (size < mtu) {
1219 size = mtu;
1220 val &= ~MVNETA_TX_TOKEN_SIZE_MAX;
1221 val |= size;
1222 mvreg_write(pp, MVNETA_TX_TOKEN_SIZE, val);
1223 }
1224 for (queue = 0; queue < txq_number; queue++) {
1225 val = mvreg_read(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue));
1226
1227 size = val & MVNETA_TXQ_TOKEN_SIZE_MAX;
1228 if (size < mtu) {
1229 size = mtu;
1230 val &= ~MVNETA_TXQ_TOKEN_SIZE_MAX;
1231 val |= size;
1232 mvreg_write(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue), val);
1233 }
1234 }
1235}
1236
1237/* Set unicast address */
1238static void mvneta_set_ucast_addr(struct mvneta_port *pp, u8 last_nibble,
1239 int queue)
1240{
1241 unsigned int unicast_reg;
1242 unsigned int tbl_offset;
1243 unsigned int reg_offset;
1244
1245 /* Locate the Unicast table entry */
1246 last_nibble = (0xf & last_nibble);
1247
1248 /* offset from unicast tbl base */
1249 tbl_offset = (last_nibble / 4) * 4;
1250
1251 /* offset within the above reg */
1252 reg_offset = last_nibble % 4;
1253
1254 unicast_reg = mvreg_read(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset));
1255
1256 if (queue == -1) {
1257 /* Clear accepts frame bit at specified unicast DA tbl entry */
1258 unicast_reg &= ~(0xff << (8 * reg_offset));
1259 } else {
1260 unicast_reg &= ~(0xff << (8 * reg_offset));
1261 unicast_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
1262 }
1263
1264 mvreg_write(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset), unicast_reg);
1265}
1266
1267/* Set mac address */
1268static void mvneta_mac_addr_set(struct mvneta_port *pp, unsigned char *addr,
1269 int queue)
1270{
1271 unsigned int mac_h;
1272 unsigned int mac_l;
1273
1274 if (queue != -1) {
1275 mac_l = (addr[4] << 8) | (addr[5]);
1276 mac_h = (addr[0] << 24) | (addr[1] << 16) |
1277 (addr[2] << 8) | (addr[3] << 0);
1278
1279 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, mac_l);
1280 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, mac_h);
1281 }
1282
1283 /* Accept frames of this address */
1284 mvneta_set_ucast_addr(pp, addr[5], queue);
1285}
1286
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001287/* Set the number of packets that will be received before RX interrupt
1288 * will be generated by HW.
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001289 */
1290static void mvneta_rx_pkts_coal_set(struct mvneta_port *pp,
1291 struct mvneta_rx_queue *rxq, u32 value)
1292{
1293 mvreg_write(pp, MVNETA_RXQ_THRESHOLD_REG(rxq->id),
1294 value | MVNETA_RXQ_NON_OCCUPIED(0));
1295 rxq->pkts_coal = value;
1296}
1297
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001298/* Set the time delay in usec before RX interrupt will be generated by
1299 * HW.
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001300 */
1301static void mvneta_rx_time_coal_set(struct mvneta_port *pp,
1302 struct mvneta_rx_queue *rxq, u32 value)
1303{
Thomas Petazzoni189dd622012-11-19 14:15:25 +01001304 u32 val;
1305 unsigned long clk_rate;
1306
1307 clk_rate = clk_get_rate(pp->clk);
1308 val = (clk_rate / 1000000) * value;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001309
1310 mvreg_write(pp, MVNETA_RXQ_TIME_COAL_REG(rxq->id), val);
1311 rxq->time_coal = value;
1312}
1313
1314/* Set threshold for TX_DONE pkts coalescing */
1315static void mvneta_tx_done_pkts_coal_set(struct mvneta_port *pp,
1316 struct mvneta_tx_queue *txq, u32 value)
1317{
1318 u32 val;
1319
1320 val = mvreg_read(pp, MVNETA_TXQ_SIZE_REG(txq->id));
1321
1322 val &= ~MVNETA_TXQ_SENT_THRESH_ALL_MASK;
1323 val |= MVNETA_TXQ_SENT_THRESH_MASK(value);
1324
1325 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), val);
1326
1327 txq->done_pkts_coal = value;
1328}
1329
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001330/* Handle rx descriptor fill by setting buf_cookie and buf_phys_addr */
1331static void mvneta_rx_desc_fill(struct mvneta_rx_desc *rx_desc,
1332 u32 phys_addr, u32 cookie)
1333{
1334 rx_desc->buf_cookie = cookie;
1335 rx_desc->buf_phys_addr = phys_addr;
1336}
1337
1338/* Decrement sent descriptors counter */
1339static void mvneta_txq_sent_desc_dec(struct mvneta_port *pp,
1340 struct mvneta_tx_queue *txq,
1341 int sent_desc)
1342{
1343 u32 val;
1344
1345 /* Only 255 TX descriptors can be updated at once */
1346 while (sent_desc > 0xff) {
1347 val = 0xff << MVNETA_TXQ_DEC_SENT_SHIFT;
1348 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1349 sent_desc = sent_desc - 0xff;
1350 }
1351
1352 val = sent_desc << MVNETA_TXQ_DEC_SENT_SHIFT;
1353 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1354}
1355
1356/* Get number of TX descriptors already sent by HW */
1357static int mvneta_txq_sent_desc_num_get(struct mvneta_port *pp,
1358 struct mvneta_tx_queue *txq)
1359{
1360 u32 val;
1361 int sent_desc;
1362
1363 val = mvreg_read(pp, MVNETA_TXQ_STATUS_REG(txq->id));
1364 sent_desc = (val & MVNETA_TXQ_SENT_DESC_MASK) >>
1365 MVNETA_TXQ_SENT_DESC_SHIFT;
1366
1367 return sent_desc;
1368}
1369
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001370/* Get number of sent descriptors and decrement counter.
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001371 * The number of sent descriptors is returned.
1372 */
1373static int mvneta_txq_sent_desc_proc(struct mvneta_port *pp,
1374 struct mvneta_tx_queue *txq)
1375{
1376 int sent_desc;
1377
1378 /* Get number of sent descriptors */
1379 sent_desc = mvneta_txq_sent_desc_num_get(pp, txq);
1380
1381 /* Decrement sent descriptors counter */
1382 if (sent_desc)
1383 mvneta_txq_sent_desc_dec(pp, txq, sent_desc);
1384
1385 return sent_desc;
1386}
1387
1388/* Set TXQ descriptors fields relevant for CSUM calculation */
1389static u32 mvneta_txq_desc_csum(int l3_offs, int l3_proto,
1390 int ip_hdr_len, int l4_proto)
1391{
1392 u32 command;
1393
1394 /* Fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001395 * G_L4_chk, L4_type; required only for checksum
1396 * calculation
1397 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001398 command = l3_offs << MVNETA_TX_L3_OFF_SHIFT;
1399 command |= ip_hdr_len << MVNETA_TX_IP_HLEN_SHIFT;
1400
Thomas Fitzsimmons0a198582014-07-08 19:44:07 -04001401 if (l3_proto == htons(ETH_P_IP))
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001402 command |= MVNETA_TXD_IP_CSUM;
1403 else
1404 command |= MVNETA_TX_L3_IP6;
1405
1406 if (l4_proto == IPPROTO_TCP)
1407 command |= MVNETA_TX_L4_CSUM_FULL;
1408 else if (l4_proto == IPPROTO_UDP)
1409 command |= MVNETA_TX_L4_UDP | MVNETA_TX_L4_CSUM_FULL;
1410 else
1411 command |= MVNETA_TX_L4_CSUM_NOT;
1412
1413 return command;
1414}
1415
1416
1417/* Display more error info */
1418static void mvneta_rx_error(struct mvneta_port *pp,
1419 struct mvneta_rx_desc *rx_desc)
1420{
1421 u32 status = rx_desc->status;
1422
willy tarreau54282132014-01-16 08:20:14 +01001423 if (!mvneta_rxq_desc_is_first_last(status)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001424 netdev_err(pp->dev,
1425 "bad rx status %08x (buffer oversize), size=%d\n",
willy tarreau54282132014-01-16 08:20:14 +01001426 status, rx_desc->data_size);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001427 return;
1428 }
1429
1430 switch (status & MVNETA_RXD_ERR_CODE_MASK) {
1431 case MVNETA_RXD_ERR_CRC:
1432 netdev_err(pp->dev, "bad rx status %08x (crc error), size=%d\n",
1433 status, rx_desc->data_size);
1434 break;
1435 case MVNETA_RXD_ERR_OVERRUN:
1436 netdev_err(pp->dev, "bad rx status %08x (overrun error), size=%d\n",
1437 status, rx_desc->data_size);
1438 break;
1439 case MVNETA_RXD_ERR_LEN:
1440 netdev_err(pp->dev, "bad rx status %08x (max frame length error), size=%d\n",
1441 status, rx_desc->data_size);
1442 break;
1443 case MVNETA_RXD_ERR_RESOURCE:
1444 netdev_err(pp->dev, "bad rx status %08x (resource error), size=%d\n",
1445 status, rx_desc->data_size);
1446 break;
1447 }
1448}
1449
willy tarreau54282132014-01-16 08:20:14 +01001450/* Handle RX checksum offload based on the descriptor's status */
1451static void mvneta_rx_csum(struct mvneta_port *pp, u32 status,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001452 struct sk_buff *skb)
1453{
willy tarreau54282132014-01-16 08:20:14 +01001454 if ((status & MVNETA_RXD_L3_IP4) &&
1455 (status & MVNETA_RXD_L4_CSUM_OK)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001456 skb->csum = 0;
1457 skb->ip_summed = CHECKSUM_UNNECESSARY;
1458 return;
1459 }
1460
1461 skb->ip_summed = CHECKSUM_NONE;
1462}
1463
willy tarreau6c498972014-01-16 08:20:12 +01001464/* Return tx queue pointer (find last set bit) according to <cause> returned
1465 * form tx_done reg. <cause> must not be null. The return value is always a
1466 * valid queue for matching the first one found in <cause>.
1467 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001468static struct mvneta_tx_queue *mvneta_tx_done_policy(struct mvneta_port *pp,
1469 u32 cause)
1470{
1471 int queue = fls(cause) - 1;
1472
willy tarreau6c498972014-01-16 08:20:12 +01001473 return &pp->txqs[queue];
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001474}
1475
1476/* Free tx queue skbuffs */
1477static void mvneta_txq_bufs_free(struct mvneta_port *pp,
1478 struct mvneta_tx_queue *txq, int num)
1479{
1480 int i;
1481
1482 for (i = 0; i < num; i++) {
1483 struct mvneta_tx_desc *tx_desc = txq->descs +
1484 txq->txq_get_index;
1485 struct sk_buff *skb = txq->tx_skb[txq->txq_get_index];
1486
1487 mvneta_txq_inc_get(txq);
1488
Ezequiel Garcia2e3173a2014-05-30 13:40:07 -03001489 if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr))
1490 dma_unmap_single(pp->dev->dev.parent,
1491 tx_desc->buf_phys_addr,
1492 tx_desc->data_size, DMA_TO_DEVICE);
Ezequiel Garciaba7e46e2014-05-30 13:40:06 -03001493 if (!skb)
1494 continue;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001495 dev_kfree_skb_any(skb);
1496 }
1497}
1498
1499/* Handle end of transmission */
Arnaud Ebalardcd713192014-01-16 08:20:19 +01001500static void mvneta_txq_done(struct mvneta_port *pp,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001501 struct mvneta_tx_queue *txq)
1502{
1503 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
1504 int tx_done;
1505
1506 tx_done = mvneta_txq_sent_desc_proc(pp, txq);
Arnaud Ebalardcd713192014-01-16 08:20:19 +01001507 if (!tx_done)
1508 return;
1509
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001510 mvneta_txq_bufs_free(pp, txq, tx_done);
1511
1512 txq->count -= tx_done;
1513
1514 if (netif_tx_queue_stopped(nq)) {
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03001515 if (txq->count <= txq->tx_wake_threshold)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001516 netif_tx_wake_queue(nq);
1517 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001518}
1519
willy tarreau8ec2cd42014-01-16 08:20:16 +01001520static void *mvneta_frag_alloc(const struct mvneta_port *pp)
1521{
1522 if (likely(pp->frag_size <= PAGE_SIZE))
1523 return netdev_alloc_frag(pp->frag_size);
1524 else
1525 return kmalloc(pp->frag_size, GFP_ATOMIC);
1526}
1527
1528static void mvneta_frag_free(const struct mvneta_port *pp, void *data)
1529{
1530 if (likely(pp->frag_size <= PAGE_SIZE))
Alexander Duyck13dc0d22015-05-06 21:12:14 -07001531 skb_free_frag(data);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001532 else
1533 kfree(data);
1534}
1535
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001536/* Refill processing */
1537static int mvneta_rx_refill(struct mvneta_port *pp,
1538 struct mvneta_rx_desc *rx_desc)
1539
1540{
1541 dma_addr_t phys_addr;
willy tarreau8ec2cd42014-01-16 08:20:16 +01001542 void *data;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001543
willy tarreau8ec2cd42014-01-16 08:20:16 +01001544 data = mvneta_frag_alloc(pp);
1545 if (!data)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001546 return -ENOMEM;
1547
willy tarreau8ec2cd42014-01-16 08:20:16 +01001548 phys_addr = dma_map_single(pp->dev->dev.parent, data,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001549 MVNETA_RX_BUF_SIZE(pp->pkt_size),
1550 DMA_FROM_DEVICE);
1551 if (unlikely(dma_mapping_error(pp->dev->dev.parent, phys_addr))) {
willy tarreau8ec2cd42014-01-16 08:20:16 +01001552 mvneta_frag_free(pp, data);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001553 return -ENOMEM;
1554 }
1555
willy tarreau8ec2cd42014-01-16 08:20:16 +01001556 mvneta_rx_desc_fill(rx_desc, phys_addr, (u32)data);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001557 return 0;
1558}
1559
1560/* Handle tx checksum */
1561static u32 mvneta_skb_tx_csum(struct mvneta_port *pp, struct sk_buff *skb)
1562{
1563 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1564 int ip_hdr_len = 0;
Vlad Yasevich817dbfa2014-08-25 10:34:54 -04001565 __be16 l3_proto = vlan_get_protocol(skb);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001566 u8 l4_proto;
1567
Vlad Yasevich817dbfa2014-08-25 10:34:54 -04001568 if (l3_proto == htons(ETH_P_IP)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001569 struct iphdr *ip4h = ip_hdr(skb);
1570
1571 /* Calculate IPv4 checksum and L4 checksum */
1572 ip_hdr_len = ip4h->ihl;
1573 l4_proto = ip4h->protocol;
Vlad Yasevich817dbfa2014-08-25 10:34:54 -04001574 } else if (l3_proto == htons(ETH_P_IPV6)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001575 struct ipv6hdr *ip6h = ipv6_hdr(skb);
1576
1577 /* Read l4_protocol from one of IPv6 extra headers */
1578 if (skb_network_header_len(skb) > 0)
1579 ip_hdr_len = (skb_network_header_len(skb) >> 2);
1580 l4_proto = ip6h->nexthdr;
1581 } else
1582 return MVNETA_TX_L4_CSUM_NOT;
1583
1584 return mvneta_txq_desc_csum(skb_network_offset(skb),
Vlad Yasevich817dbfa2014-08-25 10:34:54 -04001585 l3_proto, ip_hdr_len, l4_proto);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001586 }
1587
1588 return MVNETA_TX_L4_CSUM_NOT;
1589}
1590
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001591/* Drop packets received by the RXQ and free buffers */
1592static void mvneta_rxq_drop_pkts(struct mvneta_port *pp,
1593 struct mvneta_rx_queue *rxq)
1594{
1595 int rx_done, i;
1596
1597 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
1598 for (i = 0; i < rxq->size; i++) {
1599 struct mvneta_rx_desc *rx_desc = rxq->descs + i;
willy tarreau8ec2cd42014-01-16 08:20:16 +01001600 void *data = (void *)rx_desc->buf_cookie;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001601
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001602 dma_unmap_single(pp->dev->dev.parent, rx_desc->buf_phys_addr,
Ezequiel Garciaa328f3a2013-12-05 13:35:37 -03001603 MVNETA_RX_BUF_SIZE(pp->pkt_size), DMA_FROM_DEVICE);
Justin Maggard8c94ddb2015-11-09 17:21:05 -08001604 mvneta_frag_free(pp, data);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001605 }
1606
1607 if (rx_done)
1608 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
1609}
1610
1611/* Main rx processing */
1612static int mvneta_rx(struct mvneta_port *pp, int rx_todo,
1613 struct mvneta_rx_queue *rxq)
1614{
Maxime Ripard12bb03b2015-09-25 18:09:36 +02001615 struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001616 struct net_device *dev = pp->dev;
Simon Guinota84e3282015-07-19 13:00:53 +02001617 int rx_done;
willy tarreaudc4277d2014-01-16 08:20:07 +01001618 u32 rcvd_pkts = 0;
1619 u32 rcvd_bytes = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001620
1621 /* Get number of received packets */
1622 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
1623
1624 if (rx_todo > rx_done)
1625 rx_todo = rx_done;
1626
1627 rx_done = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001628
1629 /* Fairness NAPI loop */
1630 while (rx_done < rx_todo) {
1631 struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
1632 struct sk_buff *skb;
willy tarreau8ec2cd42014-01-16 08:20:16 +01001633 unsigned char *data;
Simon Guinotdaf158d2015-09-15 22:41:21 +02001634 dma_addr_t phys_addr;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001635 u32 rx_status;
1636 int rx_bytes, err;
1637
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001638 rx_done++;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001639 rx_status = rx_desc->status;
willy tarreauf19fadf2014-01-16 08:20:17 +01001640 rx_bytes = rx_desc->data_size - (ETH_FCS_LEN + MVNETA_MH_SIZE);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001641 data = (unsigned char *)rx_desc->buf_cookie;
Simon Guinotdaf158d2015-09-15 22:41:21 +02001642 phys_addr = rx_desc->buf_phys_addr;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001643
willy tarreau54282132014-01-16 08:20:14 +01001644 if (!mvneta_rxq_desc_is_first_last(rx_status) ||
willy tarreauf19fadf2014-01-16 08:20:17 +01001645 (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
1646 err_drop_frame:
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001647 dev->stats.rx_errors++;
1648 mvneta_rx_error(pp, rx_desc);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001649 /* leave the descriptor untouched */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001650 continue;
1651 }
1652
willy tarreauf19fadf2014-01-16 08:20:17 +01001653 if (rx_bytes <= rx_copybreak) {
1654 /* better copy a small frame and not unmap the DMA region */
1655 skb = netdev_alloc_skb_ip_align(dev, rx_bytes);
1656 if (unlikely(!skb))
1657 goto err_drop_frame;
1658
1659 dma_sync_single_range_for_cpu(dev->dev.parent,
1660 rx_desc->buf_phys_addr,
1661 MVNETA_MH_SIZE + NET_SKB_PAD,
1662 rx_bytes,
1663 DMA_FROM_DEVICE);
1664 memcpy(skb_put(skb, rx_bytes),
1665 data + MVNETA_MH_SIZE + NET_SKB_PAD,
1666 rx_bytes);
1667
1668 skb->protocol = eth_type_trans(skb, dev);
1669 mvneta_rx_csum(pp, rx_status, skb);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02001670 napi_gro_receive(&port->napi, skb);
willy tarreauf19fadf2014-01-16 08:20:17 +01001671
1672 rcvd_pkts++;
1673 rcvd_bytes += rx_bytes;
1674
1675 /* leave the descriptor and buffer untouched */
1676 continue;
1677 }
1678
Simon Guinota84e3282015-07-19 13:00:53 +02001679 /* Refill processing */
1680 err = mvneta_rx_refill(pp, rx_desc);
1681 if (err) {
1682 netdev_err(dev, "Linux processing - Can't refill\n");
1683 rxq->missed++;
1684 goto err_drop_frame;
1685 }
1686
willy tarreauf19fadf2014-01-16 08:20:17 +01001687 skb = build_skb(data, pp->frag_size > PAGE_SIZE ? 0 : pp->frag_size);
willy tarreauf19fadf2014-01-16 08:20:17 +01001688
Marcin Wojtas26c17a172015-11-30 13:27:44 +01001689 /* After refill old buffer has to be unmapped regardless
1690 * the skb is successfully built or not.
1691 */
Simon Guinotdaf158d2015-09-15 22:41:21 +02001692 dma_unmap_single(dev->dev.parent, phys_addr,
Ezequiel Garciaa328f3a2013-12-05 13:35:37 -03001693 MVNETA_RX_BUF_SIZE(pp->pkt_size), DMA_FROM_DEVICE);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001694
Marcin Wojtas26c17a172015-11-30 13:27:44 +01001695 if (!skb)
1696 goto err_drop_frame;
1697
willy tarreaudc4277d2014-01-16 08:20:07 +01001698 rcvd_pkts++;
1699 rcvd_bytes += rx_bytes;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001700
1701 /* Linux processing */
willy tarreau8ec2cd42014-01-16 08:20:16 +01001702 skb_reserve(skb, MVNETA_MH_SIZE + NET_SKB_PAD);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001703 skb_put(skb, rx_bytes);
1704
1705 skb->protocol = eth_type_trans(skb, dev);
1706
willy tarreau54282132014-01-16 08:20:14 +01001707 mvneta_rx_csum(pp, rx_status, skb);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001708
Maxime Ripard12bb03b2015-09-25 18:09:36 +02001709 napi_gro_receive(&port->napi, skb);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001710 }
1711
willy tarreaudc4277d2014-01-16 08:20:07 +01001712 if (rcvd_pkts) {
willy tarreau74c41b02014-01-16 08:20:08 +01001713 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
1714
1715 u64_stats_update_begin(&stats->syncp);
1716 stats->rx_packets += rcvd_pkts;
1717 stats->rx_bytes += rcvd_bytes;
1718 u64_stats_update_end(&stats->syncp);
willy tarreaudc4277d2014-01-16 08:20:07 +01001719 }
1720
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001721 /* Update rxq management counters */
Simon Guinota84e3282015-07-19 13:00:53 +02001722 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001723
1724 return rx_done;
1725}
1726
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03001727static inline void
1728mvneta_tso_put_hdr(struct sk_buff *skb,
1729 struct mvneta_port *pp, struct mvneta_tx_queue *txq)
1730{
1731 struct mvneta_tx_desc *tx_desc;
1732 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1733
1734 txq->tx_skb[txq->txq_put_index] = NULL;
1735 tx_desc = mvneta_txq_next_desc_get(txq);
1736 tx_desc->data_size = hdr_len;
1737 tx_desc->command = mvneta_skb_tx_csum(pp, skb);
1738 tx_desc->command |= MVNETA_TXD_F_DESC;
1739 tx_desc->buf_phys_addr = txq->tso_hdrs_phys +
1740 txq->txq_put_index * TSO_HEADER_SIZE;
1741 mvneta_txq_inc_put(txq);
1742}
1743
1744static inline int
1745mvneta_tso_put_data(struct net_device *dev, struct mvneta_tx_queue *txq,
1746 struct sk_buff *skb, char *data, int size,
1747 bool last_tcp, bool is_last)
1748{
1749 struct mvneta_tx_desc *tx_desc;
1750
1751 tx_desc = mvneta_txq_next_desc_get(txq);
1752 tx_desc->data_size = size;
1753 tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, data,
1754 size, DMA_TO_DEVICE);
1755 if (unlikely(dma_mapping_error(dev->dev.parent,
1756 tx_desc->buf_phys_addr))) {
1757 mvneta_txq_desc_put(txq);
1758 return -ENOMEM;
1759 }
1760
1761 tx_desc->command = 0;
1762 txq->tx_skb[txq->txq_put_index] = NULL;
1763
1764 if (last_tcp) {
1765 /* last descriptor in the TCP packet */
1766 tx_desc->command = MVNETA_TXD_L_DESC;
1767
1768 /* last descriptor in SKB */
1769 if (is_last)
1770 txq->tx_skb[txq->txq_put_index] = skb;
1771 }
1772 mvneta_txq_inc_put(txq);
1773 return 0;
1774}
1775
1776static int mvneta_tx_tso(struct sk_buff *skb, struct net_device *dev,
1777 struct mvneta_tx_queue *txq)
1778{
1779 int total_len, data_left;
1780 int desc_count = 0;
1781 struct mvneta_port *pp = netdev_priv(dev);
1782 struct tso_t tso;
1783 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1784 int i;
1785
1786 /* Count needed descriptors */
1787 if ((txq->count + tso_count_descs(skb)) >= txq->size)
1788 return 0;
1789
1790 if (skb_headlen(skb) < (skb_transport_offset(skb) + tcp_hdrlen(skb))) {
1791 pr_info("*** Is this even possible???!?!?\n");
1792 return 0;
1793 }
1794
1795 /* Initialize the TSO handler, and prepare the first payload */
1796 tso_start(skb, &tso);
1797
1798 total_len = skb->len - hdr_len;
1799 while (total_len > 0) {
1800 char *hdr;
1801
1802 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
1803 total_len -= data_left;
1804 desc_count++;
1805
1806 /* prepare packet headers: MAC + IP + TCP */
1807 hdr = txq->tso_hdrs + txq->txq_put_index * TSO_HEADER_SIZE;
1808 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
1809
1810 mvneta_tso_put_hdr(skb, pp, txq);
1811
1812 while (data_left > 0) {
1813 int size;
1814 desc_count++;
1815
1816 size = min_t(int, tso.size, data_left);
1817
1818 if (mvneta_tso_put_data(dev, txq, skb,
1819 tso.data, size,
1820 size == data_left,
1821 total_len == 0))
1822 goto err_release;
1823 data_left -= size;
1824
1825 tso_build_data(skb, &tso, size);
1826 }
1827 }
1828
1829 return desc_count;
1830
1831err_release:
1832 /* Release all used data descriptors; header descriptors must not
1833 * be DMA-unmapped.
1834 */
1835 for (i = desc_count - 1; i >= 0; i--) {
1836 struct mvneta_tx_desc *tx_desc = txq->descs + i;
Ezequiel Garcia2e3173a2014-05-30 13:40:07 -03001837 if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr))
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03001838 dma_unmap_single(pp->dev->dev.parent,
1839 tx_desc->buf_phys_addr,
1840 tx_desc->data_size,
1841 DMA_TO_DEVICE);
1842 mvneta_txq_desc_put(txq);
1843 }
1844 return 0;
1845}
1846
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001847/* Handle tx fragmentation processing */
1848static int mvneta_tx_frag_process(struct mvneta_port *pp, struct sk_buff *skb,
1849 struct mvneta_tx_queue *txq)
1850{
1851 struct mvneta_tx_desc *tx_desc;
Ezequiel Garcia3d4ea022014-05-22 20:06:57 -03001852 int i, nr_frags = skb_shinfo(skb)->nr_frags;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001853
Ezequiel Garcia3d4ea022014-05-22 20:06:57 -03001854 for (i = 0; i < nr_frags; i++) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001855 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1856 void *addr = page_address(frag->page.p) + frag->page_offset;
1857
1858 tx_desc = mvneta_txq_next_desc_get(txq);
1859 tx_desc->data_size = frag->size;
1860
1861 tx_desc->buf_phys_addr =
1862 dma_map_single(pp->dev->dev.parent, addr,
1863 tx_desc->data_size, DMA_TO_DEVICE);
1864
1865 if (dma_mapping_error(pp->dev->dev.parent,
1866 tx_desc->buf_phys_addr)) {
1867 mvneta_txq_desc_put(txq);
1868 goto error;
1869 }
1870
Ezequiel Garcia3d4ea022014-05-22 20:06:57 -03001871 if (i == nr_frags - 1) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001872 /* Last descriptor */
1873 tx_desc->command = MVNETA_TXD_L_DESC | MVNETA_TXD_Z_PAD;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001874 txq->tx_skb[txq->txq_put_index] = skb;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001875 } else {
1876 /* Descriptor in the middle: Not First, Not Last */
1877 tx_desc->command = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001878 txq->tx_skb[txq->txq_put_index] = NULL;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001879 }
Ezequiel Garcia3d4ea022014-05-22 20:06:57 -03001880 mvneta_txq_inc_put(txq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001881 }
1882
1883 return 0;
1884
1885error:
1886 /* Release all descriptors that were used to map fragments of
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001887 * this packet, as well as the corresponding DMA mappings
1888 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001889 for (i = i - 1; i >= 0; i--) {
1890 tx_desc = txq->descs + i;
1891 dma_unmap_single(pp->dev->dev.parent,
1892 tx_desc->buf_phys_addr,
1893 tx_desc->data_size,
1894 DMA_TO_DEVICE);
1895 mvneta_txq_desc_put(txq);
1896 }
1897
1898 return -ENOMEM;
1899}
1900
1901/* Main tx processing */
1902static int mvneta_tx(struct sk_buff *skb, struct net_device *dev)
1903{
1904 struct mvneta_port *pp = netdev_priv(dev);
Willy Tarreauee40a112013-04-11 23:00:37 +02001905 u16 txq_id = skb_get_queue_mapping(skb);
1906 struct mvneta_tx_queue *txq = &pp->txqs[txq_id];
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001907 struct mvneta_tx_desc *tx_desc;
Eric Dumazet5f478b42014-12-02 04:30:59 -08001908 int len = skb->len;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001909 int frags = 0;
1910 u32 tx_cmd;
1911
1912 if (!netif_running(dev))
1913 goto out;
1914
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03001915 if (skb_is_gso(skb)) {
1916 frags = mvneta_tx_tso(skb, dev, txq);
1917 goto out;
1918 }
1919
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001920 frags = skb_shinfo(skb)->nr_frags + 1;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001921
1922 /* Get a descriptor for the first part of the packet */
1923 tx_desc = mvneta_txq_next_desc_get(txq);
1924
1925 tx_cmd = mvneta_skb_tx_csum(pp, skb);
1926
1927 tx_desc->data_size = skb_headlen(skb);
1928
1929 tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, skb->data,
1930 tx_desc->data_size,
1931 DMA_TO_DEVICE);
1932 if (unlikely(dma_mapping_error(dev->dev.parent,
1933 tx_desc->buf_phys_addr))) {
1934 mvneta_txq_desc_put(txq);
1935 frags = 0;
1936 goto out;
1937 }
1938
1939 if (frags == 1) {
1940 /* First and Last descriptor */
1941 tx_cmd |= MVNETA_TXD_FLZ_DESC;
1942 tx_desc->command = tx_cmd;
1943 txq->tx_skb[txq->txq_put_index] = skb;
1944 mvneta_txq_inc_put(txq);
1945 } else {
1946 /* First but not Last */
1947 tx_cmd |= MVNETA_TXD_F_DESC;
1948 txq->tx_skb[txq->txq_put_index] = NULL;
1949 mvneta_txq_inc_put(txq);
1950 tx_desc->command = tx_cmd;
1951 /* Continue with other skb fragments */
1952 if (mvneta_tx_frag_process(pp, skb, txq)) {
1953 dma_unmap_single(dev->dev.parent,
1954 tx_desc->buf_phys_addr,
1955 tx_desc->data_size,
1956 DMA_TO_DEVICE);
1957 mvneta_txq_desc_put(txq);
1958 frags = 0;
1959 goto out;
1960 }
1961 }
1962
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001963out:
1964 if (frags > 0) {
willy tarreau74c41b02014-01-16 08:20:08 +01001965 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
Ezequiel Garciae19d2dd2014-05-19 13:59:54 -03001966 struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
1967
1968 txq->count += frags;
1969 mvneta_txq_pend_desc_add(pp, txq, frags);
1970
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03001971 if (txq->count >= txq->tx_stop_threshold)
Ezequiel Garciae19d2dd2014-05-19 13:59:54 -03001972 netif_tx_stop_queue(nq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001973
willy tarreau74c41b02014-01-16 08:20:08 +01001974 u64_stats_update_begin(&stats->syncp);
1975 stats->tx_packets++;
Eric Dumazet5f478b42014-12-02 04:30:59 -08001976 stats->tx_bytes += len;
willy tarreau74c41b02014-01-16 08:20:08 +01001977 u64_stats_update_end(&stats->syncp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001978 } else {
1979 dev->stats.tx_dropped++;
1980 dev_kfree_skb_any(skb);
1981 }
1982
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001983 return NETDEV_TX_OK;
1984}
1985
1986
1987/* Free tx resources, when resetting a port */
1988static void mvneta_txq_done_force(struct mvneta_port *pp,
1989 struct mvneta_tx_queue *txq)
1990
1991{
1992 int tx_done = txq->count;
1993
1994 mvneta_txq_bufs_free(pp, txq, tx_done);
1995
1996 /* reset txq */
1997 txq->count = 0;
1998 txq->txq_put_index = 0;
1999 txq->txq_get_index = 0;
2000}
2001
willy tarreau6c498972014-01-16 08:20:12 +01002002/* Handle tx done - called in softirq context. The <cause_tx_done> argument
2003 * must be a valid cause according to MVNETA_TXQ_INTR_MASK_ALL.
2004 */
Arnaud Ebalard0713a862014-01-16 08:20:18 +01002005static void mvneta_tx_done_gbe(struct mvneta_port *pp, u32 cause_tx_done)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002006{
2007 struct mvneta_tx_queue *txq;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002008 struct netdev_queue *nq;
2009
willy tarreau6c498972014-01-16 08:20:12 +01002010 while (cause_tx_done) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002011 txq = mvneta_tx_done_policy(pp, cause_tx_done);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002012
2013 nq = netdev_get_tx_queue(pp->dev, txq->id);
2014 __netif_tx_lock(nq, smp_processor_id());
2015
Arnaud Ebalard0713a862014-01-16 08:20:18 +01002016 if (txq->count)
2017 mvneta_txq_done(pp, txq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002018
2019 __netif_tx_unlock(nq);
2020 cause_tx_done &= ~((1 << txq->id));
2021 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002022}
2023
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002024/* Compute crc8 of the specified address, using a unique algorithm ,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002025 * according to hw spec, different than generic crc8 algorithm
2026 */
2027static int mvneta_addr_crc(unsigned char *addr)
2028{
2029 int crc = 0;
2030 int i;
2031
2032 for (i = 0; i < ETH_ALEN; i++) {
2033 int j;
2034
2035 crc = (crc ^ addr[i]) << 8;
2036 for (j = 7; j >= 0; j--) {
2037 if (crc & (0x100 << j))
2038 crc ^= 0x107 << j;
2039 }
2040 }
2041
2042 return crc;
2043}
2044
2045/* This method controls the net device special MAC multicast support.
2046 * The Special Multicast Table for MAC addresses supports MAC of the form
2047 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
2048 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
2049 * Table entries in the DA-Filter table. This method set the Special
2050 * Multicast Table appropriate entry.
2051 */
2052static void mvneta_set_special_mcast_addr(struct mvneta_port *pp,
2053 unsigned char last_byte,
2054 int queue)
2055{
2056 unsigned int smc_table_reg;
2057 unsigned int tbl_offset;
2058 unsigned int reg_offset;
2059
2060 /* Register offset from SMC table base */
2061 tbl_offset = (last_byte / 4);
2062 /* Entry offset within the above reg */
2063 reg_offset = last_byte % 4;
2064
2065 smc_table_reg = mvreg_read(pp, (MVNETA_DA_FILT_SPEC_MCAST
2066 + tbl_offset * 4));
2067
2068 if (queue == -1)
2069 smc_table_reg &= ~(0xff << (8 * reg_offset));
2070 else {
2071 smc_table_reg &= ~(0xff << (8 * reg_offset));
2072 smc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
2073 }
2074
2075 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + tbl_offset * 4,
2076 smc_table_reg);
2077}
2078
2079/* This method controls the network device Other MAC multicast support.
2080 * The Other Multicast Table is used for multicast of another type.
2081 * A CRC-8 is used as an index to the Other Multicast Table entries
2082 * in the DA-Filter table.
2083 * The method gets the CRC-8 value from the calling routine and
2084 * sets the Other Multicast Table appropriate entry according to the
2085 * specified CRC-8 .
2086 */
2087static void mvneta_set_other_mcast_addr(struct mvneta_port *pp,
2088 unsigned char crc8,
2089 int queue)
2090{
2091 unsigned int omc_table_reg;
2092 unsigned int tbl_offset;
2093 unsigned int reg_offset;
2094
2095 tbl_offset = (crc8 / 4) * 4; /* Register offset from OMC table base */
2096 reg_offset = crc8 % 4; /* Entry offset within the above reg */
2097
2098 omc_table_reg = mvreg_read(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset);
2099
2100 if (queue == -1) {
2101 /* Clear accepts frame bit at specified Other DA table entry */
2102 omc_table_reg &= ~(0xff << (8 * reg_offset));
2103 } else {
2104 omc_table_reg &= ~(0xff << (8 * reg_offset));
2105 omc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
2106 }
2107
2108 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset, omc_table_reg);
2109}
2110
2111/* The network device supports multicast using two tables:
2112 * 1) Special Multicast Table for MAC addresses of the form
2113 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
2114 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
2115 * Table entries in the DA-Filter table.
2116 * 2) Other Multicast Table for multicast of another type. A CRC-8 value
2117 * is used as an index to the Other Multicast Table entries in the
2118 * DA-Filter table.
2119 */
2120static int mvneta_mcast_addr_set(struct mvneta_port *pp, unsigned char *p_addr,
2121 int queue)
2122{
2123 unsigned char crc_result = 0;
2124
2125 if (memcmp(p_addr, "\x01\x00\x5e\x00\x00", 5) == 0) {
2126 mvneta_set_special_mcast_addr(pp, p_addr[5], queue);
2127 return 0;
2128 }
2129
2130 crc_result = mvneta_addr_crc(p_addr);
2131 if (queue == -1) {
2132 if (pp->mcast_count[crc_result] == 0) {
2133 netdev_info(pp->dev, "No valid Mcast for crc8=0x%02x\n",
2134 crc_result);
2135 return -EINVAL;
2136 }
2137
2138 pp->mcast_count[crc_result]--;
2139 if (pp->mcast_count[crc_result] != 0) {
2140 netdev_info(pp->dev,
2141 "After delete there are %d valid Mcast for crc8=0x%02x\n",
2142 pp->mcast_count[crc_result], crc_result);
2143 return -EINVAL;
2144 }
2145 } else
2146 pp->mcast_count[crc_result]++;
2147
2148 mvneta_set_other_mcast_addr(pp, crc_result, queue);
2149
2150 return 0;
2151}
2152
2153/* Configure Fitering mode of Ethernet port */
2154static void mvneta_rx_unicast_promisc_set(struct mvneta_port *pp,
2155 int is_promisc)
2156{
2157 u32 port_cfg_reg, val;
2158
2159 port_cfg_reg = mvreg_read(pp, MVNETA_PORT_CONFIG);
2160
2161 val = mvreg_read(pp, MVNETA_TYPE_PRIO);
2162
2163 /* Set / Clear UPM bit in port configuration register */
2164 if (is_promisc) {
2165 /* Accept all Unicast addresses */
2166 port_cfg_reg |= MVNETA_UNI_PROMISC_MODE;
2167 val |= MVNETA_FORCE_UNI;
2168 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, 0xffff);
2169 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, 0xffffffff);
2170 } else {
2171 /* Reject all Unicast addresses */
2172 port_cfg_reg &= ~MVNETA_UNI_PROMISC_MODE;
2173 val &= ~MVNETA_FORCE_UNI;
2174 }
2175
2176 mvreg_write(pp, MVNETA_PORT_CONFIG, port_cfg_reg);
2177 mvreg_write(pp, MVNETA_TYPE_PRIO, val);
2178}
2179
2180/* register unicast and multicast addresses */
2181static void mvneta_set_rx_mode(struct net_device *dev)
2182{
2183 struct mvneta_port *pp = netdev_priv(dev);
2184 struct netdev_hw_addr *ha;
2185
2186 if (dev->flags & IFF_PROMISC) {
2187 /* Accept all: Multicast + Unicast */
2188 mvneta_rx_unicast_promisc_set(pp, 1);
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01002189 mvneta_set_ucast_table(pp, pp->rxq_def);
2190 mvneta_set_special_mcast_table(pp, pp->rxq_def);
2191 mvneta_set_other_mcast_table(pp, pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002192 } else {
2193 /* Accept single Unicast */
2194 mvneta_rx_unicast_promisc_set(pp, 0);
2195 mvneta_set_ucast_table(pp, -1);
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01002196 mvneta_mac_addr_set(pp, dev->dev_addr, pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002197
2198 if (dev->flags & IFF_ALLMULTI) {
2199 /* Accept all multicast */
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01002200 mvneta_set_special_mcast_table(pp, pp->rxq_def);
2201 mvneta_set_other_mcast_table(pp, pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002202 } else {
2203 /* Accept only initialized multicast */
2204 mvneta_set_special_mcast_table(pp, -1);
2205 mvneta_set_other_mcast_table(pp, -1);
2206
2207 if (!netdev_mc_empty(dev)) {
2208 netdev_for_each_mc_addr(ha, dev) {
2209 mvneta_mcast_addr_set(pp, ha->addr,
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01002210 pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002211 }
2212 }
2213 }
2214 }
2215}
2216
2217/* Interrupt handling - the callback for request_irq() */
2218static irqreturn_t mvneta_isr(int irq, void *dev_id)
2219{
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002220 struct mvneta_pcpu_port *port = (struct mvneta_pcpu_port *)dev_id;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002221
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002222 disable_percpu_irq(port->pp->dev->irq);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002223 napi_schedule(&port->napi);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002224
2225 return IRQ_HANDLED;
2226}
2227
Stas Sergeev898b29702015-04-01 20:32:49 +03002228static int mvneta_fixed_link_update(struct mvneta_port *pp,
2229 struct phy_device *phy)
2230{
2231 struct fixed_phy_status status;
2232 struct fixed_phy_status changed = {};
2233 u32 gmac_stat = mvreg_read(pp, MVNETA_GMAC_STATUS);
2234
2235 status.link = !!(gmac_stat & MVNETA_GMAC_LINK_UP);
2236 if (gmac_stat & MVNETA_GMAC_SPEED_1000)
2237 status.speed = SPEED_1000;
2238 else if (gmac_stat & MVNETA_GMAC_SPEED_100)
2239 status.speed = SPEED_100;
2240 else
2241 status.speed = SPEED_10;
2242 status.duplex = !!(gmac_stat & MVNETA_GMAC_FULL_DUPLEX);
2243 changed.link = 1;
2244 changed.speed = 1;
2245 changed.duplex = 1;
2246 fixed_phy_update_state(phy, &status, &changed);
2247 return 0;
2248}
2249
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002250/* NAPI handler
2251 * Bits 0 - 7 of the causeRxTx register indicate that are transmitted
2252 * packets on the corresponding TXQ (Bit 0 is for TX queue 1).
2253 * Bits 8 -15 of the cause Rx Tx register indicate that are received
2254 * packets on the corresponding RXQ (Bit 8 is for RX queue 0).
2255 * Each CPU has its own causeRxTx register
2256 */
2257static int mvneta_poll(struct napi_struct *napi, int budget)
2258{
2259 int rx_done = 0;
2260 u32 cause_rx_tx;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002261 int rx_queue;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002262 struct mvneta_port *pp = netdev_priv(napi->dev);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002263 struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002264
2265 if (!netif_running(pp->dev)) {
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002266 napi_complete(&port->napi);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002267 return rx_done;
2268 }
2269
2270 /* Read cause register */
Stas Sergeev898b29702015-04-01 20:32:49 +03002271 cause_rx_tx = mvreg_read(pp, MVNETA_INTR_NEW_CAUSE);
2272 if (cause_rx_tx & MVNETA_MISCINTR_INTR_MASK) {
2273 u32 cause_misc = mvreg_read(pp, MVNETA_INTR_MISC_CAUSE);
2274
2275 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
2276 if (pp->use_inband_status && (cause_misc &
2277 (MVNETA_CAUSE_PHY_STATUS_CHANGE |
2278 MVNETA_CAUSE_LINK_CHANGE |
2279 MVNETA_CAUSE_PSC_SYNC_CHANGE))) {
2280 mvneta_fixed_link_update(pp, pp->phy_dev);
2281 }
2282 }
willy tarreau71f6d1b2014-01-16 08:20:11 +01002283
2284 /* Release Tx descriptors */
2285 if (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL) {
Arnaud Ebalard0713a862014-01-16 08:20:18 +01002286 mvneta_tx_done_gbe(pp, (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL));
willy tarreau71f6d1b2014-01-16 08:20:11 +01002287 cause_rx_tx &= ~MVNETA_TX_INTR_MASK_ALL;
2288 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002289
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002290 /* For the case where the last mvneta_poll did not process all
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002291 * RX packets
2292 */
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002293 rx_queue = fls(((cause_rx_tx >> 8) & 0xff));
2294
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002295 cause_rx_tx |= port->cause_rx_tx;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002296
2297 if (rx_queue) {
2298 rx_queue = rx_queue - 1;
2299 rx_done = mvneta_rx(pp, budget, &pp->rxqs[rx_queue]);
2300 }
2301
Maxime Ripardd8936652015-09-25 18:09:37 +02002302 budget -= rx_done;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002303
2304 if (budget > 0) {
2305 cause_rx_tx = 0;
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002306 napi_complete(&port->napi);
2307 enable_percpu_irq(pp->dev->irq, 0);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002308 }
2309
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002310 port->cause_rx_tx = cause_rx_tx;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002311 return rx_done;
2312}
2313
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002314/* Handle rxq fill: allocates rxq skbs; called when initializing a port */
2315static int mvneta_rxq_fill(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
2316 int num)
2317{
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002318 int i;
2319
2320 for (i = 0; i < num; i++) {
willy tarreaua1a65ab2014-01-16 08:20:13 +01002321 memset(rxq->descs + i, 0, sizeof(struct mvneta_rx_desc));
2322 if (mvneta_rx_refill(pp, rxq->descs + i) != 0) {
2323 netdev_err(pp->dev, "%s:rxq %d, %d of %d buffs filled\n",
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002324 __func__, rxq->id, i, num);
2325 break;
2326 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002327 }
2328
2329 /* Add this number of RX descriptors as non occupied (ready to
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002330 * get packets)
2331 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002332 mvneta_rxq_non_occup_desc_add(pp, rxq, i);
2333
2334 return i;
2335}
2336
2337/* Free all packets pending transmit from all TXQs and reset TX port */
2338static void mvneta_tx_reset(struct mvneta_port *pp)
2339{
2340 int queue;
2341
Ezequiel Garcia96728502014-05-22 20:06:59 -03002342 /* free the skb's in the tx ring */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002343 for (queue = 0; queue < txq_number; queue++)
2344 mvneta_txq_done_force(pp, &pp->txqs[queue]);
2345
2346 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
2347 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
2348}
2349
2350static void mvneta_rx_reset(struct mvneta_port *pp)
2351{
2352 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
2353 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
2354}
2355
2356/* Rx/Tx queue initialization/cleanup methods */
2357
2358/* Create a specified RX queue */
2359static int mvneta_rxq_init(struct mvneta_port *pp,
2360 struct mvneta_rx_queue *rxq)
2361
2362{
2363 rxq->size = pp->rx_ring_size;
2364
2365 /* Allocate memory for RX descriptors */
2366 rxq->descs = dma_alloc_coherent(pp->dev->dev.parent,
2367 rxq->size * MVNETA_DESC_ALIGNED_SIZE,
2368 &rxq->descs_phys, GFP_KERNEL);
Joe Perchesd0320f72013-03-14 13:07:21 +00002369 if (rxq->descs == NULL)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002370 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002371
2372 BUG_ON(rxq->descs !=
2373 PTR_ALIGN(rxq->descs, MVNETA_CPU_D_CACHE_LINE_SIZE));
2374
2375 rxq->last_desc = rxq->size - 1;
2376
2377 /* Set Rx descriptors queue starting address */
2378 mvreg_write(pp, MVNETA_RXQ_BASE_ADDR_REG(rxq->id), rxq->descs_phys);
2379 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), rxq->size);
2380
2381 /* Set Offset */
2382 mvneta_rxq_offset_set(pp, rxq, NET_SKB_PAD);
2383
2384 /* Set coalescing pkts and time */
2385 mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
2386 mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
2387
2388 /* Fill RXQ with buffers from RX pool */
2389 mvneta_rxq_buf_size_set(pp, rxq, MVNETA_RX_BUF_SIZE(pp->pkt_size));
2390 mvneta_rxq_bm_disable(pp, rxq);
2391 mvneta_rxq_fill(pp, rxq, rxq->size);
2392
2393 return 0;
2394}
2395
2396/* Cleanup Rx queue */
2397static void mvneta_rxq_deinit(struct mvneta_port *pp,
2398 struct mvneta_rx_queue *rxq)
2399{
2400 mvneta_rxq_drop_pkts(pp, rxq);
2401
2402 if (rxq->descs)
2403 dma_free_coherent(pp->dev->dev.parent,
2404 rxq->size * MVNETA_DESC_ALIGNED_SIZE,
2405 rxq->descs,
2406 rxq->descs_phys);
2407
2408 rxq->descs = NULL;
2409 rxq->last_desc = 0;
2410 rxq->next_desc_to_proc = 0;
2411 rxq->descs_phys = 0;
2412}
2413
2414/* Create and initialize a tx queue */
2415static int mvneta_txq_init(struct mvneta_port *pp,
2416 struct mvneta_tx_queue *txq)
2417{
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01002418 int cpu;
2419
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002420 txq->size = pp->tx_ring_size;
2421
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03002422 /* A queue must always have room for at least one skb.
2423 * Therefore, stop the queue when the free entries reaches
2424 * the maximum number of descriptors per skb.
2425 */
2426 txq->tx_stop_threshold = txq->size - MVNETA_MAX_SKB_DESCS;
2427 txq->tx_wake_threshold = txq->tx_stop_threshold / 2;
2428
2429
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002430 /* Allocate memory for TX descriptors */
2431 txq->descs = dma_alloc_coherent(pp->dev->dev.parent,
2432 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2433 &txq->descs_phys, GFP_KERNEL);
Joe Perchesd0320f72013-03-14 13:07:21 +00002434 if (txq->descs == NULL)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002435 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002436
2437 /* Make sure descriptor address is cache line size aligned */
2438 BUG_ON(txq->descs !=
2439 PTR_ALIGN(txq->descs, MVNETA_CPU_D_CACHE_LINE_SIZE));
2440
2441 txq->last_desc = txq->size - 1;
2442
2443 /* Set maximum bandwidth for enabled TXQs */
2444 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0x03ffffff);
2445 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0x3fffffff);
2446
2447 /* Set Tx descriptors queue starting address */
2448 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), txq->descs_phys);
2449 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), txq->size);
2450
2451 txq->tx_skb = kmalloc(txq->size * sizeof(*txq->tx_skb), GFP_KERNEL);
2452 if (txq->tx_skb == NULL) {
2453 dma_free_coherent(pp->dev->dev.parent,
2454 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2455 txq->descs, txq->descs_phys);
2456 return -ENOMEM;
2457 }
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03002458
2459 /* Allocate DMA buffers for TSO MAC/IP/TCP headers */
2460 txq->tso_hdrs = dma_alloc_coherent(pp->dev->dev.parent,
2461 txq->size * TSO_HEADER_SIZE,
2462 &txq->tso_hdrs_phys, GFP_KERNEL);
2463 if (txq->tso_hdrs == NULL) {
2464 kfree(txq->tx_skb);
2465 dma_free_coherent(pp->dev->dev.parent,
2466 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2467 txq->descs, txq->descs_phys);
2468 return -ENOMEM;
2469 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002470 mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
2471
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01002472 /* Setup XPS mapping */
2473 if (txq_number > 1)
2474 cpu = txq->id % num_present_cpus();
2475 else
2476 cpu = pp->rxq_def % num_present_cpus();
2477 cpumask_set_cpu(cpu, &txq->affinity_mask);
2478 netif_set_xps_queue(pp->dev, &txq->affinity_mask, txq->id);
2479
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002480 return 0;
2481}
2482
2483/* Free allocated resources when mvneta_txq_init() fails to allocate memory*/
2484static void mvneta_txq_deinit(struct mvneta_port *pp,
2485 struct mvneta_tx_queue *txq)
2486{
2487 kfree(txq->tx_skb);
2488
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03002489 if (txq->tso_hdrs)
2490 dma_free_coherent(pp->dev->dev.parent,
2491 txq->size * TSO_HEADER_SIZE,
2492 txq->tso_hdrs, txq->tso_hdrs_phys);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002493 if (txq->descs)
2494 dma_free_coherent(pp->dev->dev.parent,
2495 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2496 txq->descs, txq->descs_phys);
2497
2498 txq->descs = NULL;
2499 txq->last_desc = 0;
2500 txq->next_desc_to_proc = 0;
2501 txq->descs_phys = 0;
2502
2503 /* Set minimum bandwidth for disabled TXQs */
2504 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0);
2505 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0);
2506
2507 /* Set Tx descriptors queue starting address and size */
2508 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), 0);
2509 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), 0);
2510}
2511
2512/* Cleanup all Tx queues */
2513static void mvneta_cleanup_txqs(struct mvneta_port *pp)
2514{
2515 int queue;
2516
2517 for (queue = 0; queue < txq_number; queue++)
2518 mvneta_txq_deinit(pp, &pp->txqs[queue]);
2519}
2520
2521/* Cleanup all Rx queues */
2522static void mvneta_cleanup_rxqs(struct mvneta_port *pp)
2523{
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002524 int queue;
2525
2526 for (queue = 0; queue < txq_number; queue++)
2527 mvneta_rxq_deinit(pp, &pp->rxqs[queue]);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002528}
2529
2530
2531/* Init all Rx queues */
2532static int mvneta_setup_rxqs(struct mvneta_port *pp)
2533{
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002534 int queue;
2535
2536 for (queue = 0; queue < rxq_number; queue++) {
2537 int err = mvneta_rxq_init(pp, &pp->rxqs[queue]);
2538
2539 if (err) {
2540 netdev_err(pp->dev, "%s: can't create rxq=%d\n",
2541 __func__, queue);
2542 mvneta_cleanup_rxqs(pp);
2543 return err;
2544 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002545 }
2546
2547 return 0;
2548}
2549
2550/* Init all tx queues */
2551static int mvneta_setup_txqs(struct mvneta_port *pp)
2552{
2553 int queue;
2554
2555 for (queue = 0; queue < txq_number; queue++) {
2556 int err = mvneta_txq_init(pp, &pp->txqs[queue]);
2557 if (err) {
2558 netdev_err(pp->dev, "%s: can't create txq=%d\n",
2559 __func__, queue);
2560 mvneta_cleanup_txqs(pp);
2561 return err;
2562 }
2563 }
2564
2565 return 0;
2566}
2567
2568static void mvneta_start_dev(struct mvneta_port *pp)
2569{
Gregory CLEMENT6b125d62016-02-04 22:09:25 +01002570 int cpu;
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002571
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002572 mvneta_max_rx_size_set(pp, pp->pkt_size);
2573 mvneta_txq_max_tx_size_set(pp, pp->pkt_size);
2574
2575 /* start the Rx/Tx activity */
2576 mvneta_port_enable(pp);
2577
2578 /* Enable polling on the port */
Gregory CLEMENT129219e2016-02-04 22:09:23 +01002579 for_each_online_cpu(cpu) {
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002580 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
2581
2582 napi_enable(&port->napi);
2583 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002584
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002585 /* Unmask interrupts. It has to be done from each CPU */
Gregory CLEMENT6b125d62016-02-04 22:09:25 +01002586 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
2587
Stas Sergeev898b29702015-04-01 20:32:49 +03002588 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
2589 MVNETA_CAUSE_PHY_STATUS_CHANGE |
2590 MVNETA_CAUSE_LINK_CHANGE |
2591 MVNETA_CAUSE_PSC_SYNC_CHANGE);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002592
2593 phy_start(pp->phy_dev);
2594 netif_tx_start_all_queues(pp->dev);
2595}
2596
2597static void mvneta_stop_dev(struct mvneta_port *pp)
2598{
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002599 unsigned int cpu;
2600
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002601 phy_stop(pp->phy_dev);
2602
Gregory CLEMENT129219e2016-02-04 22:09:23 +01002603 for_each_online_cpu(cpu) {
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002604 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
2605
2606 napi_disable(&port->napi);
2607 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002608
2609 netif_carrier_off(pp->dev);
2610
2611 mvneta_port_down(pp);
2612 netif_tx_stop_all_queues(pp->dev);
2613
2614 /* Stop the port activity */
2615 mvneta_port_disable(pp);
2616
2617 /* Clear all ethernet port interrupts */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01002618 on_each_cpu(mvneta_percpu_clear_intr_cause, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002619
2620 /* Mask all ethernet port interrupts */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01002621 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002622
2623 mvneta_tx_reset(pp);
2624 mvneta_rx_reset(pp);
2625}
2626
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002627/* Return positive if MTU is valid */
2628static int mvneta_check_mtu_valid(struct net_device *dev, int mtu)
2629{
2630 if (mtu < 68) {
2631 netdev_err(dev, "cannot change mtu to less than 68\n");
2632 return -EINVAL;
2633 }
2634
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002635 /* 9676 == 9700 - 20 and rounding to 8 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002636 if (mtu > 9676) {
2637 netdev_info(dev, "Illegal MTU value %d, round to 9676\n", mtu);
2638 mtu = 9676;
2639 }
2640
2641 if (!IS_ALIGNED(MVNETA_RX_PKT_SIZE(mtu), 8)) {
2642 netdev_info(dev, "Illegal MTU value %d, rounding to %d\n",
2643 mtu, ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8));
2644 mtu = ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8);
2645 }
2646
2647 return mtu;
2648}
2649
2650/* Change the device mtu */
2651static int mvneta_change_mtu(struct net_device *dev, int mtu)
2652{
2653 struct mvneta_port *pp = netdev_priv(dev);
2654 int ret;
2655
2656 mtu = mvneta_check_mtu_valid(dev, mtu);
2657 if (mtu < 0)
2658 return -EINVAL;
2659
2660 dev->mtu = mtu;
2661
Simon Guinotb65657f2015-06-30 16:20:22 +02002662 if (!netif_running(dev)) {
2663 netdev_update_features(dev);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002664 return 0;
Simon Guinotb65657f2015-06-30 16:20:22 +02002665 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002666
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002667 /* The interface is running, so we have to force a
Ezequiel Garciaa92dbd92014-05-22 20:06:58 -03002668 * reallocation of the queues
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002669 */
2670 mvneta_stop_dev(pp);
2671
2672 mvneta_cleanup_txqs(pp);
2673 mvneta_cleanup_rxqs(pp);
2674
Ezequiel Garciaa92dbd92014-05-22 20:06:58 -03002675 pp->pkt_size = MVNETA_RX_PKT_SIZE(dev->mtu);
willy tarreau8ec2cd42014-01-16 08:20:16 +01002676 pp->frag_size = SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(pp->pkt_size)) +
2677 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002678
2679 ret = mvneta_setup_rxqs(pp);
2680 if (ret) {
Ezequiel Garciaa92dbd92014-05-22 20:06:58 -03002681 netdev_err(dev, "unable to setup rxqs after MTU change\n");
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002682 return ret;
2683 }
2684
Ezequiel Garciaa92dbd92014-05-22 20:06:58 -03002685 ret = mvneta_setup_txqs(pp);
2686 if (ret) {
2687 netdev_err(dev, "unable to setup txqs after MTU change\n");
2688 return ret;
2689 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002690
2691 mvneta_start_dev(pp);
2692 mvneta_port_up(pp);
2693
Simon Guinotb65657f2015-06-30 16:20:22 +02002694 netdev_update_features(dev);
2695
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002696 return 0;
2697}
2698
Simon Guinotb65657f2015-06-30 16:20:22 +02002699static netdev_features_t mvneta_fix_features(struct net_device *dev,
2700 netdev_features_t features)
2701{
2702 struct mvneta_port *pp = netdev_priv(dev);
2703
2704 if (pp->tx_csum_limit && dev->mtu > pp->tx_csum_limit) {
2705 features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
2706 netdev_info(dev,
2707 "Disable IP checksum for MTU greater than %dB\n",
2708 pp->tx_csum_limit);
2709 }
2710
2711 return features;
2712}
2713
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00002714/* Get mac address */
2715static void mvneta_get_mac_addr(struct mvneta_port *pp, unsigned char *addr)
2716{
2717 u32 mac_addr_l, mac_addr_h;
2718
2719 mac_addr_l = mvreg_read(pp, MVNETA_MAC_ADDR_LOW);
2720 mac_addr_h = mvreg_read(pp, MVNETA_MAC_ADDR_HIGH);
2721 addr[0] = (mac_addr_h >> 24) & 0xFF;
2722 addr[1] = (mac_addr_h >> 16) & 0xFF;
2723 addr[2] = (mac_addr_h >> 8) & 0xFF;
2724 addr[3] = mac_addr_h & 0xFF;
2725 addr[4] = (mac_addr_l >> 8) & 0xFF;
2726 addr[5] = mac_addr_l & 0xFF;
2727}
2728
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002729/* Handle setting mac address */
2730static int mvneta_set_mac_addr(struct net_device *dev, void *addr)
2731{
2732 struct mvneta_port *pp = netdev_priv(dev);
Ezequiel Garciae68de362014-05-22 20:07:00 -03002733 struct sockaddr *sockaddr = addr;
2734 int ret;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002735
Ezequiel Garciae68de362014-05-22 20:07:00 -03002736 ret = eth_prepare_mac_addr_change(dev, addr);
2737 if (ret < 0)
2738 return ret;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002739 /* Remove previous address table entry */
2740 mvneta_mac_addr_set(pp, dev->dev_addr, -1);
2741
2742 /* Set new addr in hw */
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01002743 mvneta_mac_addr_set(pp, sockaddr->sa_data, pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002744
Ezequiel Garciae68de362014-05-22 20:07:00 -03002745 eth_commit_mac_addr_change(dev, addr);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002746 return 0;
2747}
2748
2749static void mvneta_adjust_link(struct net_device *ndev)
2750{
2751 struct mvneta_port *pp = netdev_priv(ndev);
2752 struct phy_device *phydev = pp->phy_dev;
2753 int status_change = 0;
2754
2755 if (phydev->link) {
2756 if ((pp->speed != phydev->speed) ||
2757 (pp->duplex != phydev->duplex)) {
2758 u32 val;
2759
2760 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
2761 val &= ~(MVNETA_GMAC_CONFIG_MII_SPEED |
2762 MVNETA_GMAC_CONFIG_GMII_SPEED |
Stas Sergeev898b29702015-04-01 20:32:49 +03002763 MVNETA_GMAC_CONFIG_FULL_DUPLEX);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002764
2765 if (phydev->duplex)
2766 val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
2767
2768 if (phydev->speed == SPEED_1000)
2769 val |= MVNETA_GMAC_CONFIG_GMII_SPEED;
Thomas Petazzoni4d12bc62014-07-08 10:49:43 +02002770 else if (phydev->speed == SPEED_100)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002771 val |= MVNETA_GMAC_CONFIG_MII_SPEED;
2772
2773 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
2774
2775 pp->duplex = phydev->duplex;
2776 pp->speed = phydev->speed;
2777 }
2778 }
2779
2780 if (phydev->link != pp->link) {
2781 if (!phydev->link) {
2782 pp->duplex = -1;
2783 pp->speed = 0;
2784 }
2785
2786 pp->link = phydev->link;
2787 status_change = 1;
2788 }
2789
2790 if (status_change) {
2791 if (phydev->link) {
Stas Sergeev898b29702015-04-01 20:32:49 +03002792 if (!pp->use_inband_status) {
2793 u32 val = mvreg_read(pp,
2794 MVNETA_GMAC_AUTONEG_CONFIG);
2795 val &= ~MVNETA_GMAC_FORCE_LINK_DOWN;
2796 val |= MVNETA_GMAC_FORCE_LINK_PASS;
2797 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
2798 val);
2799 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002800 mvneta_port_up(pp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002801 } else {
Stas Sergeev898b29702015-04-01 20:32:49 +03002802 if (!pp->use_inband_status) {
2803 u32 val = mvreg_read(pp,
2804 MVNETA_GMAC_AUTONEG_CONFIG);
2805 val &= ~MVNETA_GMAC_FORCE_LINK_PASS;
2806 val |= MVNETA_GMAC_FORCE_LINK_DOWN;
2807 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
2808 val);
2809 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002810 mvneta_port_down(pp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002811 }
Ezequiel Garcia0089b742014-10-31 12:57:20 -03002812 phy_print_status(phydev);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002813 }
2814}
2815
2816static int mvneta_mdio_probe(struct mvneta_port *pp)
2817{
2818 struct phy_device *phy_dev;
2819
2820 phy_dev = of_phy_connect(pp->dev, pp->phy_node, mvneta_adjust_link, 0,
2821 pp->phy_interface);
2822 if (!phy_dev) {
2823 netdev_err(pp->dev, "could not find the PHY\n");
2824 return -ENODEV;
2825 }
2826
2827 phy_dev->supported &= PHY_GBIT_FEATURES;
2828 phy_dev->advertising = phy_dev->supported;
2829
2830 pp->phy_dev = phy_dev;
2831 pp->link = 0;
2832 pp->duplex = 0;
2833 pp->speed = 0;
2834
2835 return 0;
2836}
2837
2838static void mvneta_mdio_remove(struct mvneta_port *pp)
2839{
2840 phy_disconnect(pp->phy_dev);
2841 pp->phy_dev = NULL;
2842}
2843
Maxime Ripardf8642882015-09-25 18:09:38 +02002844static void mvneta_percpu_enable(void *arg)
2845{
2846 struct mvneta_port *pp = arg;
2847
2848 enable_percpu_irq(pp->dev->irq, IRQ_TYPE_NONE);
2849}
2850
2851static void mvneta_percpu_disable(void *arg)
2852{
2853 struct mvneta_port *pp = arg;
2854
2855 disable_percpu_irq(pp->dev->irq);
2856}
2857
2858static void mvneta_percpu_elect(struct mvneta_port *pp)
2859{
Gregory CLEMENTcad5d842016-02-04 22:09:24 +01002860 int elected_cpu = 0, max_cpu, cpu, i = 0;
Maxime Ripardf8642882015-09-25 18:09:38 +02002861
Gregory CLEMENT58885112016-02-04 22:09:28 +01002862 /* Electing a CPU must be done in an atomic way: it should be
2863 * done after or before the removal/insertion of a CPU and
2864 * this function is not reentrant.
2865 */
2866 spin_lock(&pp->lock);
2867
Gregory CLEMENTcad5d842016-02-04 22:09:24 +01002868 /* Use the cpu associated to the rxq when it is online, in all
2869 * the other cases, use the cpu 0 which can't be offline.
2870 */
2871 if (cpu_online(pp->rxq_def))
2872 elected_cpu = pp->rxq_def;
2873
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002874 max_cpu = num_present_cpus();
Maxime Ripardf8642882015-09-25 18:09:38 +02002875
2876 for_each_online_cpu(cpu) {
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002877 int rxq_map = 0, txq_map = 0;
2878 int rxq;
2879
2880 for (rxq = 0; rxq < rxq_number; rxq++)
2881 if ((rxq % max_cpu) == cpu)
2882 rxq_map |= MVNETA_CPU_RXQ_ACCESS(rxq);
2883
Gregory CLEMENTcad5d842016-02-04 22:09:24 +01002884 if (cpu == elected_cpu)
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01002885 /* Map the default receive queue queue to the
2886 * elected CPU
Maxime Ripardf8642882015-09-25 18:09:38 +02002887 */
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002888 rxq_map |= MVNETA_CPU_RXQ_ACCESS(pp->rxq_def);
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01002889
2890 /* We update the TX queue map only if we have one
2891 * queue. In this case we associate the TX queue to
2892 * the CPU bound to the default RX queue
2893 */
2894 if (txq_number == 1)
Gregory CLEMENTcad5d842016-02-04 22:09:24 +01002895 txq_map = (cpu == elected_cpu) ?
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01002896 MVNETA_CPU_TXQ_ACCESS(1) : 0;
2897 else
2898 txq_map = mvreg_read(pp, MVNETA_CPU_MAP(cpu)) &
2899 MVNETA_CPU_TXQ_ACCESS_ALL_MASK;
2900
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002901 mvreg_write(pp, MVNETA_CPU_MAP(cpu), rxq_map | txq_map);
2902
2903 /* Update the interrupt mask on each CPU according the
2904 * new mapping
2905 */
2906 smp_call_function_single(cpu, mvneta_percpu_unmask_interrupt,
2907 pp, true);
Maxime Ripardf8642882015-09-25 18:09:38 +02002908 i++;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002909
Maxime Ripardf8642882015-09-25 18:09:38 +02002910 }
Gregory CLEMENT58885112016-02-04 22:09:28 +01002911 spin_unlock(&pp->lock);
Maxime Ripardf8642882015-09-25 18:09:38 +02002912};
2913
2914static int mvneta_percpu_notifier(struct notifier_block *nfb,
2915 unsigned long action, void *hcpu)
2916{
2917 struct mvneta_port *pp = container_of(nfb, struct mvneta_port,
2918 cpu_notifier);
2919 int cpu = (unsigned long)hcpu, other_cpu;
2920 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
2921
2922 switch (action) {
2923 case CPU_ONLINE:
2924 case CPU_ONLINE_FROZEN:
2925 netif_tx_stop_all_queues(pp->dev);
2926
2927 /* We have to synchronise on tha napi of each CPU
2928 * except the one just being waked up
2929 */
2930 for_each_online_cpu(other_cpu) {
2931 if (other_cpu != cpu) {
2932 struct mvneta_pcpu_port *other_port =
2933 per_cpu_ptr(pp->ports, other_cpu);
2934
2935 napi_synchronize(&other_port->napi);
2936 }
2937 }
2938
2939 /* Mask all ethernet port interrupts */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01002940 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
Maxime Ripardf8642882015-09-25 18:09:38 +02002941 napi_enable(&port->napi);
2942
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002943
2944 /* Enable per-CPU interrupts on the CPU that is
2945 * brought up.
2946 */
2947 smp_call_function_single(cpu, mvneta_percpu_enable,
2948 pp, true);
2949
Maxime Ripardf8642882015-09-25 18:09:38 +02002950 /* Enable per-CPU interrupt on the one CPU we care
2951 * about.
2952 */
2953 mvneta_percpu_elect(pp);
2954
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01002955 /* Unmask all ethernet port interrupts */
2956 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
Maxime Ripardf8642882015-09-25 18:09:38 +02002957 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
2958 MVNETA_CAUSE_PHY_STATUS_CHANGE |
2959 MVNETA_CAUSE_LINK_CHANGE |
2960 MVNETA_CAUSE_PSC_SYNC_CHANGE);
2961 netif_tx_start_all_queues(pp->dev);
2962 break;
2963 case CPU_DOWN_PREPARE:
2964 case CPU_DOWN_PREPARE_FROZEN:
2965 netif_tx_stop_all_queues(pp->dev);
Gregory CLEMENT58885112016-02-04 22:09:28 +01002966 /* Thanks to this lock we are sure that any pending
2967 * cpu election is done
2968 */
2969 spin_lock(&pp->lock);
Maxime Ripardf8642882015-09-25 18:09:38 +02002970 /* Mask all ethernet port interrupts */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01002971 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
Gregory CLEMENT58885112016-02-04 22:09:28 +01002972 spin_unlock(&pp->lock);
Maxime Ripardf8642882015-09-25 18:09:38 +02002973
2974 napi_synchronize(&port->napi);
2975 napi_disable(&port->napi);
2976 /* Disable per-CPU interrupts on the CPU that is
2977 * brought down.
2978 */
2979 smp_call_function_single(cpu, mvneta_percpu_disable,
2980 pp, true);
2981
2982 break;
2983 case CPU_DEAD:
2984 case CPU_DEAD_FROZEN:
2985 /* Check if a new CPU must be elected now this on is down */
2986 mvneta_percpu_elect(pp);
2987 /* Unmask all ethernet port interrupts */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01002988 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
Maxime Ripardf8642882015-09-25 18:09:38 +02002989 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
2990 MVNETA_CAUSE_PHY_STATUS_CHANGE |
2991 MVNETA_CAUSE_LINK_CHANGE |
2992 MVNETA_CAUSE_PSC_SYNC_CHANGE);
2993 netif_tx_start_all_queues(pp->dev);
2994 break;
2995 }
2996
2997 return NOTIFY_OK;
2998}
2999
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003000static int mvneta_open(struct net_device *dev)
3001{
3002 struct mvneta_port *pp = netdev_priv(dev);
Gregory CLEMENT6b125d62016-02-04 22:09:25 +01003003 int ret;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003004
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003005 pp->pkt_size = MVNETA_RX_PKT_SIZE(pp->dev->mtu);
willy tarreau8ec2cd42014-01-16 08:20:16 +01003006 pp->frag_size = SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(pp->pkt_size)) +
3007 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003008
3009 ret = mvneta_setup_rxqs(pp);
3010 if (ret)
3011 return ret;
3012
3013 ret = mvneta_setup_txqs(pp);
3014 if (ret)
3015 goto err_cleanup_rxqs;
3016
3017 /* Connect to port interrupt line */
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003018 ret = request_percpu_irq(pp->dev->irq, mvneta_isr,
3019 MVNETA_DRIVER_NAME, pp->ports);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003020 if (ret) {
3021 netdev_err(pp->dev, "cannot request irq %d\n", pp->dev->irq);
3022 goto err_cleanup_txqs;
3023 }
3024
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003025 /* Enable per-CPU interrupt on all the CPU to handle our RX
3026 * queue interrupts
3027 */
Gregory CLEMENT6b125d62016-02-04 22:09:25 +01003028 on_each_cpu(mvneta_percpu_enable, pp, true);
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003029
Maxime Ripardf8642882015-09-25 18:09:38 +02003030
3031 /* Register a CPU notifier to handle the case where our CPU
3032 * might be taken offline.
3033 */
3034 register_cpu_notifier(&pp->cpu_notifier);
3035
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003036 /* In default link is down */
3037 netif_carrier_off(pp->dev);
3038
3039 ret = mvneta_mdio_probe(pp);
3040 if (ret < 0) {
3041 netdev_err(dev, "cannot probe MDIO bus\n");
3042 goto err_free_irq;
3043 }
3044
3045 mvneta_start_dev(pp);
3046
3047 return 0;
3048
3049err_free_irq:
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003050 free_percpu_irq(pp->dev->irq, pp->ports);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003051err_cleanup_txqs:
3052 mvneta_cleanup_txqs(pp);
3053err_cleanup_rxqs:
3054 mvneta_cleanup_rxqs(pp);
3055 return ret;
3056}
3057
3058/* Stop the port, free port interrupt line */
3059static int mvneta_stop(struct net_device *dev)
3060{
3061 struct mvneta_port *pp = netdev_priv(dev);
3062
3063 mvneta_stop_dev(pp);
3064 mvneta_mdio_remove(pp);
Maxime Ripardf8642882015-09-25 18:09:38 +02003065 unregister_cpu_notifier(&pp->cpu_notifier);
Gregory CLEMENT129219e2016-02-04 22:09:23 +01003066 on_each_cpu(mvneta_percpu_disable, pp, true);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003067 free_percpu_irq(dev->irq, pp->ports);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003068 mvneta_cleanup_rxqs(pp);
3069 mvneta_cleanup_txqs(pp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003070
3071 return 0;
3072}
3073
Thomas Petazzoni15f59452013-09-04 16:26:52 +02003074static int mvneta_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3075{
3076 struct mvneta_port *pp = netdev_priv(dev);
Thomas Petazzoni15f59452013-09-04 16:26:52 +02003077
3078 if (!pp->phy_dev)
3079 return -ENOTSUPP;
3080
Stas Sergeevecf7b362015-04-01 19:23:29 +03003081 return phy_mii_ioctl(pp->phy_dev, ifr, cmd);
Thomas Petazzoni15f59452013-09-04 16:26:52 +02003082}
3083
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003084/* Ethtool methods */
3085
3086/* Get settings (phy address, speed) for ethtools */
3087int mvneta_ethtool_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
3088{
3089 struct mvneta_port *pp = netdev_priv(dev);
3090
3091 if (!pp->phy_dev)
3092 return -ENODEV;
3093
3094 return phy_ethtool_gset(pp->phy_dev, cmd);
3095}
3096
3097/* Set settings (phy address, speed) for ethtools */
3098int mvneta_ethtool_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
3099{
3100 struct mvneta_port *pp = netdev_priv(dev);
Stas Sergeev0c0744f2015-12-02 20:35:11 +03003101 struct phy_device *phydev = pp->phy_dev;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003102
Stas Sergeev0c0744f2015-12-02 20:35:11 +03003103 if (!phydev)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003104 return -ENODEV;
3105
Stas Sergeev0c0744f2015-12-02 20:35:11 +03003106 if ((cmd->autoneg == AUTONEG_ENABLE) != pp->use_inband_status) {
3107 u32 val;
3108
3109 mvneta_set_autoneg(pp, cmd->autoneg == AUTONEG_ENABLE);
3110
3111 if (cmd->autoneg == AUTONEG_DISABLE) {
3112 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3113 val &= ~(MVNETA_GMAC_CONFIG_MII_SPEED |
3114 MVNETA_GMAC_CONFIG_GMII_SPEED |
3115 MVNETA_GMAC_CONFIG_FULL_DUPLEX);
3116
3117 if (phydev->duplex)
3118 val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
3119
3120 if (phydev->speed == SPEED_1000)
3121 val |= MVNETA_GMAC_CONFIG_GMII_SPEED;
3122 else if (phydev->speed == SPEED_100)
3123 val |= MVNETA_GMAC_CONFIG_MII_SPEED;
3124
3125 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
3126 }
3127
3128 pp->use_inband_status = (cmd->autoneg == AUTONEG_ENABLE);
3129 netdev_info(pp->dev, "autoneg status set to %i\n",
3130 pp->use_inband_status);
3131
3132 if (netif_running(dev)) {
3133 mvneta_port_down(pp);
3134 mvneta_port_up(pp);
3135 }
3136 }
3137
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003138 return phy_ethtool_sset(pp->phy_dev, cmd);
3139}
3140
3141/* Set interrupt coalescing for ethtools */
3142static int mvneta_ethtool_set_coalesce(struct net_device *dev,
3143 struct ethtool_coalesce *c)
3144{
3145 struct mvneta_port *pp = netdev_priv(dev);
3146 int queue;
3147
3148 for (queue = 0; queue < rxq_number; queue++) {
3149 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
3150 rxq->time_coal = c->rx_coalesce_usecs;
3151 rxq->pkts_coal = c->rx_max_coalesced_frames;
3152 mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
3153 mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
3154 }
3155
3156 for (queue = 0; queue < txq_number; queue++) {
3157 struct mvneta_tx_queue *txq = &pp->txqs[queue];
3158 txq->done_pkts_coal = c->tx_max_coalesced_frames;
3159 mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
3160 }
3161
3162 return 0;
3163}
3164
3165/* get coalescing for ethtools */
3166static int mvneta_ethtool_get_coalesce(struct net_device *dev,
3167 struct ethtool_coalesce *c)
3168{
3169 struct mvneta_port *pp = netdev_priv(dev);
3170
3171 c->rx_coalesce_usecs = pp->rxqs[0].time_coal;
3172 c->rx_max_coalesced_frames = pp->rxqs[0].pkts_coal;
3173
3174 c->tx_max_coalesced_frames = pp->txqs[0].done_pkts_coal;
3175 return 0;
3176}
3177
3178
3179static void mvneta_ethtool_get_drvinfo(struct net_device *dev,
3180 struct ethtool_drvinfo *drvinfo)
3181{
3182 strlcpy(drvinfo->driver, MVNETA_DRIVER_NAME,
3183 sizeof(drvinfo->driver));
3184 strlcpy(drvinfo->version, MVNETA_DRIVER_VERSION,
3185 sizeof(drvinfo->version));
3186 strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
3187 sizeof(drvinfo->bus_info));
3188}
3189
3190
3191static void mvneta_ethtool_get_ringparam(struct net_device *netdev,
3192 struct ethtool_ringparam *ring)
3193{
3194 struct mvneta_port *pp = netdev_priv(netdev);
3195
3196 ring->rx_max_pending = MVNETA_MAX_RXD;
3197 ring->tx_max_pending = MVNETA_MAX_TXD;
3198 ring->rx_pending = pp->rx_ring_size;
3199 ring->tx_pending = pp->tx_ring_size;
3200}
3201
3202static int mvneta_ethtool_set_ringparam(struct net_device *dev,
3203 struct ethtool_ringparam *ring)
3204{
3205 struct mvneta_port *pp = netdev_priv(dev);
3206
3207 if ((ring->rx_pending == 0) || (ring->tx_pending == 0))
3208 return -EINVAL;
3209 pp->rx_ring_size = ring->rx_pending < MVNETA_MAX_RXD ?
3210 ring->rx_pending : MVNETA_MAX_RXD;
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03003211
3212 pp->tx_ring_size = clamp_t(u16, ring->tx_pending,
3213 MVNETA_MAX_SKB_DESCS * 2, MVNETA_MAX_TXD);
3214 if (pp->tx_ring_size != ring->tx_pending)
3215 netdev_warn(dev, "TX queue size set to %u (requested %u)\n",
3216 pp->tx_ring_size, ring->tx_pending);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003217
3218 if (netif_running(dev)) {
3219 mvneta_stop(dev);
3220 if (mvneta_open(dev)) {
3221 netdev_err(dev,
3222 "error on opening device after ring param change\n");
3223 return -ENOMEM;
3224 }
3225 }
3226
3227 return 0;
3228}
3229
Russell King9b0cdef2015-10-22 18:37:30 +01003230static void mvneta_ethtool_get_strings(struct net_device *netdev, u32 sset,
3231 u8 *data)
3232{
3233 if (sset == ETH_SS_STATS) {
3234 int i;
3235
3236 for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++)
3237 memcpy(data + i * ETH_GSTRING_LEN,
3238 mvneta_statistics[i].name, ETH_GSTRING_LEN);
3239 }
3240}
3241
3242static void mvneta_ethtool_update_stats(struct mvneta_port *pp)
3243{
3244 const struct mvneta_statistic *s;
3245 void __iomem *base = pp->base;
3246 u32 high, low, val;
Jisheng Zhang2c832292016-01-20 16:36:25 +08003247 u64 val64;
Russell King9b0cdef2015-10-22 18:37:30 +01003248 int i;
3249
3250 for (i = 0, s = mvneta_statistics;
3251 s < mvneta_statistics + ARRAY_SIZE(mvneta_statistics);
3252 s++, i++) {
Russell King9b0cdef2015-10-22 18:37:30 +01003253 switch (s->type) {
3254 case T_REG_32:
3255 val = readl_relaxed(base + s->offset);
Jisheng Zhang2c832292016-01-20 16:36:25 +08003256 pp->ethtool_stats[i] += val;
Russell King9b0cdef2015-10-22 18:37:30 +01003257 break;
3258 case T_REG_64:
3259 /* Docs say to read low 32-bit then high */
3260 low = readl_relaxed(base + s->offset);
3261 high = readl_relaxed(base + s->offset + 4);
Jisheng Zhang2c832292016-01-20 16:36:25 +08003262 val64 = (u64)high << 32 | low;
3263 pp->ethtool_stats[i] += val64;
Russell King9b0cdef2015-10-22 18:37:30 +01003264 break;
3265 }
Russell King9b0cdef2015-10-22 18:37:30 +01003266 }
3267}
3268
3269static void mvneta_ethtool_get_stats(struct net_device *dev,
3270 struct ethtool_stats *stats, u64 *data)
3271{
3272 struct mvneta_port *pp = netdev_priv(dev);
3273 int i;
3274
3275 mvneta_ethtool_update_stats(pp);
3276
3277 for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++)
3278 *data++ = pp->ethtool_stats[i];
3279}
3280
3281static int mvneta_ethtool_get_sset_count(struct net_device *dev, int sset)
3282{
3283 if (sset == ETH_SS_STATS)
3284 return ARRAY_SIZE(mvneta_statistics);
3285 return -EOPNOTSUPP;
3286}
3287
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01003288static u32 mvneta_ethtool_get_rxfh_indir_size(struct net_device *dev)
3289{
3290 return MVNETA_RSS_LU_TABLE_SIZE;
3291}
3292
3293static int mvneta_ethtool_get_rxnfc(struct net_device *dev,
3294 struct ethtool_rxnfc *info,
3295 u32 *rules __always_unused)
3296{
3297 switch (info->cmd) {
3298 case ETHTOOL_GRXRINGS:
3299 info->data = rxq_number;
3300 return 0;
3301 case ETHTOOL_GRXFH:
3302 return -EOPNOTSUPP;
3303 default:
3304 return -EOPNOTSUPP;
3305 }
3306}
3307
3308static int mvneta_config_rss(struct mvneta_port *pp)
3309{
3310 int cpu;
3311 u32 val;
3312
3313 netif_tx_stop_all_queues(pp->dev);
3314
Gregory CLEMENT6b125d62016-02-04 22:09:25 +01003315 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01003316
3317 /* We have to synchronise on the napi of each CPU */
3318 for_each_online_cpu(cpu) {
3319 struct mvneta_pcpu_port *pcpu_port =
3320 per_cpu_ptr(pp->ports, cpu);
3321
3322 napi_synchronize(&pcpu_port->napi);
3323 napi_disable(&pcpu_port->napi);
3324 }
3325
3326 pp->rxq_def = pp->indir[0];
3327
3328 /* Update unicast mapping */
3329 mvneta_set_rx_mode(pp->dev);
3330
3331 /* Update val of portCfg register accordingly with all RxQueue types */
3332 val = MVNETA_PORT_CONFIG_DEFL_VALUE(pp->rxq_def);
3333 mvreg_write(pp, MVNETA_PORT_CONFIG, val);
3334
3335 /* Update the elected CPU matching the new rxq_def */
3336 mvneta_percpu_elect(pp);
3337
3338 /* We have to synchronise on the napi of each CPU */
3339 for_each_online_cpu(cpu) {
3340 struct mvneta_pcpu_port *pcpu_port =
3341 per_cpu_ptr(pp->ports, cpu);
3342
3343 napi_enable(&pcpu_port->napi);
3344 }
3345
3346 netif_tx_start_all_queues(pp->dev);
3347
3348 return 0;
3349}
3350
3351static int mvneta_ethtool_set_rxfh(struct net_device *dev, const u32 *indir,
3352 const u8 *key, const u8 hfunc)
3353{
3354 struct mvneta_port *pp = netdev_priv(dev);
3355 /* We require at least one supported parameter to be changed
3356 * and no change in any of the unsupported parameters
3357 */
3358 if (key ||
3359 (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
3360 return -EOPNOTSUPP;
3361
3362 if (!indir)
3363 return 0;
3364
3365 memcpy(pp->indir, indir, MVNETA_RSS_LU_TABLE_SIZE);
3366
3367 return mvneta_config_rss(pp);
3368}
3369
3370static int mvneta_ethtool_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
3371 u8 *hfunc)
3372{
3373 struct mvneta_port *pp = netdev_priv(dev);
3374
3375 if (hfunc)
3376 *hfunc = ETH_RSS_HASH_TOP;
3377
3378 if (!indir)
3379 return 0;
3380
3381 memcpy(indir, pp->indir, MVNETA_RSS_LU_TABLE_SIZE);
3382
3383 return 0;
3384}
3385
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003386static const struct net_device_ops mvneta_netdev_ops = {
3387 .ndo_open = mvneta_open,
3388 .ndo_stop = mvneta_stop,
3389 .ndo_start_xmit = mvneta_tx,
3390 .ndo_set_rx_mode = mvneta_set_rx_mode,
3391 .ndo_set_mac_address = mvneta_set_mac_addr,
3392 .ndo_change_mtu = mvneta_change_mtu,
Simon Guinotb65657f2015-06-30 16:20:22 +02003393 .ndo_fix_features = mvneta_fix_features,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003394 .ndo_get_stats64 = mvneta_get_stats64,
Thomas Petazzoni15f59452013-09-04 16:26:52 +02003395 .ndo_do_ioctl = mvneta_ioctl,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003396};
3397
3398const struct ethtool_ops mvneta_eth_tool_ops = {
3399 .get_link = ethtool_op_get_link,
3400 .get_settings = mvneta_ethtool_get_settings,
3401 .set_settings = mvneta_ethtool_set_settings,
3402 .set_coalesce = mvneta_ethtool_set_coalesce,
3403 .get_coalesce = mvneta_ethtool_get_coalesce,
3404 .get_drvinfo = mvneta_ethtool_get_drvinfo,
3405 .get_ringparam = mvneta_ethtool_get_ringparam,
3406 .set_ringparam = mvneta_ethtool_set_ringparam,
Russell King9b0cdef2015-10-22 18:37:30 +01003407 .get_strings = mvneta_ethtool_get_strings,
3408 .get_ethtool_stats = mvneta_ethtool_get_stats,
3409 .get_sset_count = mvneta_ethtool_get_sset_count,
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01003410 .get_rxfh_indir_size = mvneta_ethtool_get_rxfh_indir_size,
3411 .get_rxnfc = mvneta_ethtool_get_rxnfc,
3412 .get_rxfh = mvneta_ethtool_get_rxfh,
3413 .set_rxfh = mvneta_ethtool_set_rxfh,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003414};
3415
3416/* Initialize hw */
Ezequiel Garcia96728502014-05-22 20:06:59 -03003417static int mvneta_init(struct device *dev, struct mvneta_port *pp)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003418{
3419 int queue;
3420
3421 /* Disable port */
3422 mvneta_port_disable(pp);
3423
3424 /* Set port default values */
3425 mvneta_defaults_set(pp);
3426
Ezequiel Garcia96728502014-05-22 20:06:59 -03003427 pp->txqs = devm_kcalloc(dev, txq_number, sizeof(struct mvneta_tx_queue),
3428 GFP_KERNEL);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003429 if (!pp->txqs)
3430 return -ENOMEM;
3431
3432 /* Initialize TX descriptor rings */
3433 for (queue = 0; queue < txq_number; queue++) {
3434 struct mvneta_tx_queue *txq = &pp->txqs[queue];
3435 txq->id = queue;
3436 txq->size = pp->tx_ring_size;
3437 txq->done_pkts_coal = MVNETA_TXDONE_COAL_PKTS;
3438 }
3439
Ezequiel Garcia96728502014-05-22 20:06:59 -03003440 pp->rxqs = devm_kcalloc(dev, rxq_number, sizeof(struct mvneta_rx_queue),
3441 GFP_KERNEL);
3442 if (!pp->rxqs)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003443 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003444
3445 /* Create Rx descriptor rings */
3446 for (queue = 0; queue < rxq_number; queue++) {
3447 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
3448 rxq->id = queue;
3449 rxq->size = pp->rx_ring_size;
3450 rxq->pkts_coal = MVNETA_RX_COAL_PKTS;
3451 rxq->time_coal = MVNETA_RX_COAL_USEC;
3452 }
3453
3454 return 0;
3455}
3456
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003457/* platform glue : initialize decoding windows */
Greg KH03ce7582012-12-21 13:42:15 +00003458static void mvneta_conf_mbus_windows(struct mvneta_port *pp,
3459 const struct mbus_dram_target_info *dram)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003460{
3461 u32 win_enable;
3462 u32 win_protect;
3463 int i;
3464
3465 for (i = 0; i < 6; i++) {
3466 mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
3467 mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
3468
3469 if (i < 4)
3470 mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
3471 }
3472
3473 win_enable = 0x3f;
3474 win_protect = 0;
3475
3476 for (i = 0; i < dram->num_cs; i++) {
3477 const struct mbus_dram_window *cs = dram->cs + i;
3478 mvreg_write(pp, MVNETA_WIN_BASE(i), (cs->base & 0xffff0000) |
3479 (cs->mbus_attr << 8) | dram->mbus_dram_target_id);
3480
3481 mvreg_write(pp, MVNETA_WIN_SIZE(i),
3482 (cs->size - 1) & 0xffff0000);
3483
3484 win_enable &= ~(1 << i);
3485 win_protect |= 3 << (2 * i);
3486 }
3487
3488 mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
Marcin Wojtasdb6ba9a2015-11-30 13:27:41 +01003489 mvreg_write(pp, MVNETA_ACCESS_PROTECT_ENABLE, win_protect);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003490}
3491
3492/* Power up the port */
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02003493static int mvneta_port_power_up(struct mvneta_port *pp, int phy_mode)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003494{
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02003495 u32 ctrl;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003496
3497 /* MAC Cause register should be cleared */
3498 mvreg_write(pp, MVNETA_UNIT_INTR_CAUSE, 0);
3499
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02003500 ctrl = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003501
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02003502 /* Even though it might look weird, when we're configured in
3503 * SGMII or QSGMII mode, the RGMII bit needs to be set.
3504 */
3505 switch(phy_mode) {
3506 case PHY_INTERFACE_MODE_QSGMII:
3507 mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_QSGMII_SERDES_PROTO);
3508 ctrl |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII;
3509 break;
3510 case PHY_INTERFACE_MODE_SGMII:
3511 mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_SGMII_SERDES_PROTO);
3512 ctrl |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII;
3513 break;
3514 case PHY_INTERFACE_MODE_RGMII:
3515 case PHY_INTERFACE_MODE_RGMII_ID:
3516 ctrl |= MVNETA_GMAC2_PORT_RGMII;
3517 break;
3518 default:
3519 return -EINVAL;
3520 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003521
3522 /* Cancel Port Reset */
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02003523 ctrl &= ~MVNETA_GMAC2_PORT_RESET;
3524 mvreg_write(pp, MVNETA_GMAC_CTRL_2, ctrl);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003525
3526 while ((mvreg_read(pp, MVNETA_GMAC_CTRL_2) &
3527 MVNETA_GMAC2_PORT_RESET) != 0)
3528 continue;
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02003529
3530 return 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003531}
3532
3533/* Device initialization routine */
Greg KH03ce7582012-12-21 13:42:15 +00003534static int mvneta_probe(struct platform_device *pdev)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003535{
3536 const struct mbus_dram_target_info *dram_target_info;
Thomas Petazzonic3f0dd32014-03-27 11:39:29 +01003537 struct resource *res;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003538 struct device_node *dn = pdev->dev.of_node;
3539 struct device_node *phy_node;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003540 struct mvneta_port *pp;
3541 struct net_device *dev;
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00003542 const char *dt_mac_addr;
3543 char hw_mac_addr[ETH_ALEN];
3544 const char *mac_from;
Stas Sergeevf8af8e62015-07-20 17:49:58 -07003545 const char *managed;
Marcin Wojtas9110ee02015-11-30 13:27:45 +01003546 int tx_csum_limit;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003547 int phy_mode;
3548 int err;
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003549 int cpu;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003550
Willy Tarreauee40a112013-04-11 23:00:37 +02003551 dev = alloc_etherdev_mqs(sizeof(struct mvneta_port), txq_number, rxq_number);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003552 if (!dev)
3553 return -ENOMEM;
3554
3555 dev->irq = irq_of_parse_and_map(dn, 0);
3556 if (dev->irq == 0) {
3557 err = -EINVAL;
3558 goto err_free_netdev;
3559 }
3560
3561 phy_node = of_parse_phandle(dn, "phy", 0);
3562 if (!phy_node) {
Thomas Petazzoni83895be2014-05-16 16:14:06 +02003563 if (!of_phy_is_fixed_link(dn)) {
3564 dev_err(&pdev->dev, "no PHY specified\n");
3565 err = -ENODEV;
3566 goto err_free_irq;
3567 }
3568
3569 err = of_phy_register_fixed_link(dn);
3570 if (err < 0) {
3571 dev_err(&pdev->dev, "cannot register fixed PHY\n");
3572 goto err_free_irq;
3573 }
3574
3575 /* In the case of a fixed PHY, the DT node associated
3576 * to the PHY is the Ethernet MAC DT node.
3577 */
Uwe Kleine-Königc891c242014-08-07 21:58:46 +02003578 phy_node = of_node_get(dn);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003579 }
3580
3581 phy_mode = of_get_phy_mode(dn);
3582 if (phy_mode < 0) {
3583 dev_err(&pdev->dev, "incorrect phy-mode\n");
3584 err = -EINVAL;
Uwe Kleine-Königc891c242014-08-07 21:58:46 +02003585 goto err_put_phy_node;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003586 }
3587
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003588 dev->tx_queue_len = MVNETA_MAX_TXD;
3589 dev->watchdog_timeo = 5 * HZ;
3590 dev->netdev_ops = &mvneta_netdev_ops;
3591
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00003592 dev->ethtool_ops = &mvneta_eth_tool_ops;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003593
3594 pp = netdev_priv(dev);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003595 pp->phy_node = phy_node;
3596 pp->phy_interface = phy_mode;
Stas Sergeevf8af8e62015-07-20 17:49:58 -07003597
3598 err = of_property_read_string(dn, "managed", &managed);
3599 pp->use_inband_status = (err == 0 &&
3600 strcmp(managed, "in-band-status") == 0);
Maxime Ripardf8642882015-09-25 18:09:38 +02003601 pp->cpu_notifier.notifier_call = mvneta_percpu_notifier;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003602
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01003603 pp->rxq_def = rxq_def;
3604
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01003605 pp->indir[0] = rxq_def;
3606
Jisheng Zhang2804ba42016-01-20 19:27:23 +08003607 pp->clk = devm_clk_get(&pdev->dev, "core");
3608 if (IS_ERR(pp->clk))
3609 pp->clk = devm_clk_get(&pdev->dev, NULL);
Thomas Petazzoni189dd622012-11-19 14:15:25 +01003610 if (IS_ERR(pp->clk)) {
3611 err = PTR_ERR(pp->clk);
Uwe Kleine-Königc891c242014-08-07 21:58:46 +02003612 goto err_put_phy_node;
Thomas Petazzoni189dd622012-11-19 14:15:25 +01003613 }
3614
3615 clk_prepare_enable(pp->clk);
3616
Jisheng Zhang15cc4a42016-01-20 19:27:24 +08003617 pp->clk_bus = devm_clk_get(&pdev->dev, "bus");
3618 if (!IS_ERR(pp->clk_bus))
3619 clk_prepare_enable(pp->clk_bus);
3620
Thomas Petazzonic3f0dd32014-03-27 11:39:29 +01003621 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3622 pp->base = devm_ioremap_resource(&pdev->dev, res);
3623 if (IS_ERR(pp->base)) {
3624 err = PTR_ERR(pp->base);
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +02003625 goto err_clk;
3626 }
3627
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003628 /* Alloc per-cpu port structure */
3629 pp->ports = alloc_percpu(struct mvneta_pcpu_port);
3630 if (!pp->ports) {
3631 err = -ENOMEM;
3632 goto err_clk;
3633 }
3634
willy tarreau74c41b02014-01-16 08:20:08 +01003635 /* Alloc per-cpu stats */
WANG Cong1c213bd2014-02-13 11:46:28 -08003636 pp->stats = netdev_alloc_pcpu_stats(struct mvneta_pcpu_stats);
willy tarreau74c41b02014-01-16 08:20:08 +01003637 if (!pp->stats) {
3638 err = -ENOMEM;
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003639 goto err_free_ports;
willy tarreau74c41b02014-01-16 08:20:08 +01003640 }
3641
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00003642 dt_mac_addr = of_get_mac_address(dn);
Luka Perkov6c7a9a32013-10-30 00:10:01 +01003643 if (dt_mac_addr) {
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00003644 mac_from = "device tree";
3645 memcpy(dev->dev_addr, dt_mac_addr, ETH_ALEN);
3646 } else {
3647 mvneta_get_mac_addr(pp, hw_mac_addr);
3648 if (is_valid_ether_addr(hw_mac_addr)) {
3649 mac_from = "hardware";
3650 memcpy(dev->dev_addr, hw_mac_addr, ETH_ALEN);
3651 } else {
3652 mac_from = "random";
3653 eth_hw_addr_random(dev);
3654 }
3655 }
3656
Marcin Wojtas9110ee02015-11-30 13:27:45 +01003657 if (!of_property_read_u32(dn, "tx-csum-limit", &tx_csum_limit)) {
3658 if (tx_csum_limit < 0 ||
3659 tx_csum_limit > MVNETA_TX_CSUM_MAX_SIZE) {
3660 tx_csum_limit = MVNETA_TX_CSUM_DEF_SIZE;
3661 dev_info(&pdev->dev,
3662 "Wrong TX csum limit in DT, set to %dB\n",
3663 MVNETA_TX_CSUM_DEF_SIZE);
3664 }
3665 } else if (of_device_is_compatible(dn, "marvell,armada-370-neta")) {
3666 tx_csum_limit = MVNETA_TX_CSUM_DEF_SIZE;
3667 } else {
3668 tx_csum_limit = MVNETA_TX_CSUM_MAX_SIZE;
3669 }
3670
3671 pp->tx_csum_limit = tx_csum_limit;
Simon Guinotb65657f2015-06-30 16:20:22 +02003672
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003673 pp->tx_ring_size = MVNETA_MAX_TXD;
3674 pp->rx_ring_size = MVNETA_MAX_RXD;
3675
3676 pp->dev = dev;
3677 SET_NETDEV_DEV(dev, &pdev->dev);
3678
Ezequiel Garcia96728502014-05-22 20:06:59 -03003679 err = mvneta_init(&pdev->dev, pp);
3680 if (err < 0)
willy tarreau74c41b02014-01-16 08:20:08 +01003681 goto err_free_stats;
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02003682
3683 err = mvneta_port_power_up(pp, phy_mode);
3684 if (err < 0) {
3685 dev_err(&pdev->dev, "can't power up port\n");
Ezequiel Garcia96728502014-05-22 20:06:59 -03003686 goto err_free_stats;
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02003687 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003688
3689 dram_target_info = mv_mbus_dram_info();
3690 if (dram_target_info)
3691 mvneta_conf_mbus_windows(pp, dram_target_info);
3692
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003693 for_each_present_cpu(cpu) {
3694 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
3695
3696 netif_napi_add(dev, &port->napi, mvneta_poll, NAPI_POLL_WEIGHT);
3697 port->pp = pp;
3698 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003699
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03003700 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO;
Ezequiel Garcia01ef26c2014-05-19 13:59:53 -03003701 dev->hw_features |= dev->features;
3702 dev->vlan_features |= dev->features;
willy tarreaub50b72d2013-04-06 08:47:01 +00003703 dev->priv_flags |= IFF_UNICAST_FLT;
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03003704 dev->gso_max_segs = MVNETA_MAX_TSO_SEGS;
willy tarreaub50b72d2013-04-06 08:47:01 +00003705
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003706 err = register_netdev(dev);
3707 if (err < 0) {
3708 dev_err(&pdev->dev, "failed to register\n");
Ezequiel Garcia96728502014-05-22 20:06:59 -03003709 goto err_free_stats;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003710 }
3711
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00003712 netdev_info(dev, "Using %s mac address %pM\n", mac_from,
3713 dev->dev_addr);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003714
3715 platform_set_drvdata(pdev, pp->dev);
3716
Stas Sergeev898b29702015-04-01 20:32:49 +03003717 if (pp->use_inband_status) {
3718 struct phy_device *phy = of_phy_find_device(dn);
3719
3720 mvneta_fixed_link_update(pp, phy);
Russell King04d53b22015-09-24 20:36:18 +01003721
Andrew Lunne5a03bf2016-01-06 20:11:16 +01003722 put_device(&phy->mdio.dev);
Stas Sergeev898b29702015-04-01 20:32:49 +03003723 }
3724
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003725 return 0;
3726
willy tarreau74c41b02014-01-16 08:20:08 +01003727err_free_stats:
3728 free_percpu(pp->stats);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003729err_free_ports:
3730 free_percpu(pp->ports);
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +02003731err_clk:
Jisheng Zhang15cc4a42016-01-20 19:27:24 +08003732 clk_disable_unprepare(pp->clk_bus);
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +02003733 clk_disable_unprepare(pp->clk);
Uwe Kleine-Königc891c242014-08-07 21:58:46 +02003734err_put_phy_node:
3735 of_node_put(phy_node);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003736err_free_irq:
3737 irq_dispose_mapping(dev->irq);
3738err_free_netdev:
3739 free_netdev(dev);
3740 return err;
3741}
3742
3743/* Device removal routine */
Greg KH03ce7582012-12-21 13:42:15 +00003744static int mvneta_remove(struct platform_device *pdev)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003745{
3746 struct net_device *dev = platform_get_drvdata(pdev);
3747 struct mvneta_port *pp = netdev_priv(dev);
3748
3749 unregister_netdev(dev);
Jisheng Zhang15cc4a42016-01-20 19:27:24 +08003750 clk_disable_unprepare(pp->clk_bus);
Thomas Petazzoni189dd622012-11-19 14:15:25 +01003751 clk_disable_unprepare(pp->clk);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003752 free_percpu(pp->ports);
willy tarreau74c41b02014-01-16 08:20:08 +01003753 free_percpu(pp->stats);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003754 irq_dispose_mapping(dev->irq);
Uwe Kleine-Königc891c242014-08-07 21:58:46 +02003755 of_node_put(pp->phy_node);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003756 free_netdev(dev);
3757
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003758 return 0;
3759}
3760
3761static const struct of_device_id mvneta_match[] = {
3762 { .compatible = "marvell,armada-370-neta" },
Simon Guinotf522a972015-06-30 16:20:20 +02003763 { .compatible = "marvell,armada-xp-neta" },
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003764 { }
3765};
3766MODULE_DEVICE_TABLE(of, mvneta_match);
3767
3768static struct platform_driver mvneta_driver = {
3769 .probe = mvneta_probe,
Greg KH03ce7582012-12-21 13:42:15 +00003770 .remove = mvneta_remove,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003771 .driver = {
3772 .name = MVNETA_DRIVER_NAME,
3773 .of_match_table = mvneta_match,
3774 },
3775};
3776
3777module_platform_driver(mvneta_driver);
3778
3779MODULE_DESCRIPTION("Marvell NETA Ethernet Driver - www.marvell.com");
3780MODULE_AUTHOR("Rami Rosen <rosenr@marvell.com>, Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
3781MODULE_LICENSE("GPL");
3782
3783module_param(rxq_number, int, S_IRUGO);
3784module_param(txq_number, int, S_IRUGO);
3785
3786module_param(rxq_def, int, S_IRUGO);
willy tarreauf19fadf2014-01-16 08:20:17 +01003787module_param(rx_copybreak, int, S_IRUGO | S_IWUSR);